1 /* 2 * Copyright (c) 1997 - 2001, 2003 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include "krb5_locl.h" 35 36 RCSID("$Id: transited.c 21745 2007-07-31 16:11:25Z lha $"); 37 38 /* this is an attempt at one of the most horrible `compression' 39 schemes that has ever been invented; it's so amazingly brain-dead 40 that words can not describe it, and all this just to save a few 41 silly bytes */ 42 43 struct tr_realm { 44 char *realm; 45 unsigned leading_space:1; 46 unsigned leading_slash:1; 47 unsigned trailing_dot:1; 48 struct tr_realm *next; 49 }; 50 51 static void 52 free_realms(struct tr_realm *r) 53 { 54 struct tr_realm *p; 55 while(r){ 56 p = r; 57 r = r->next; 58 free(p->realm); 59 free(p); 60 } 61 } 62 63 static int 64 make_path(krb5_context context, struct tr_realm *r, 65 const char *from, const char *to) 66 { 67 const char *p; 68 struct tr_realm *path = r->next; 69 struct tr_realm *tmp; 70 71 if(strlen(from) < strlen(to)){ 72 const char *str; 73 str = from; 74 from = to; 75 to = str; 76 } 77 78 if(strcmp(from + strlen(from) - strlen(to), to) == 0){ 79 p = from; 80 while(1){ 81 p = strchr(p, '.'); 82 if(p == NULL) { 83 krb5_clear_error_string (context); 84 return KRB5KDC_ERR_POLICY; 85 } 86 p++; 87 if(strcmp(p, to) == 0) 88 break; 89 tmp = calloc(1, sizeof(*tmp)); 90 if(tmp == NULL){ 91 krb5_set_error_string (context, "malloc: out of memory"); 92 return ENOMEM; 93 } 94 tmp->next = path; 95 path = tmp; 96 path->realm = strdup(p); 97 if(path->realm == NULL){ 98 r->next = path; /* XXX */ 99 krb5_set_error_string (context, "malloc: out of memory"); 100 return ENOMEM;; 101 } 102 } 103 }else if(strncmp(from, to, strlen(to)) == 0){ 104 p = from + strlen(from); 105 while(1){ 106 while(p >= from && *p != '/') p--; 107 if(p == from) { 108 r->next = path; /* XXX */ 109 return KRB5KDC_ERR_POLICY; 110 } 111 if(strncmp(to, from, p - from) == 0) 112 break; 113 tmp = calloc(1, sizeof(*tmp)); 114 if(tmp == NULL){ 115 krb5_set_error_string (context, "malloc: out of memory"); 116 return ENOMEM; 117 } 118 tmp->next = path; 119 path = tmp; 120 path->realm = malloc(p - from + 1); 121 if(path->realm == NULL){ 122 r->next = path; /* XXX */ 123 krb5_set_error_string (context, "malloc: out of memory"); 124 return ENOMEM; 125 } 126 memcpy(path->realm, from, p - from); 127 path->realm[p - from] = '\0'; 128 p--; 129 } 130 } else { 131 krb5_clear_error_string (context); 132 return KRB5KDC_ERR_POLICY; 133 } 134 r->next = path; 135 136 return 0; 137 } 138 139 static int 140 make_paths(krb5_context context, 141 struct tr_realm *realms, const char *client_realm, 142 const char *server_realm) 143 { 144 struct tr_realm *r; 145 int ret; 146 const char *prev_realm = client_realm; 147 const char *next_realm = NULL; 148 for(r = realms; r; r = r->next){ 149 /* it *might* be that you can have more than one empty 150 component in a row, at least that's how I interpret the 151 "," exception in 1510 */ 152 if(r->realm[0] == '\0'){ 153 while(r->next && r->next->realm[0] == '\0') 154 r = r->next; 155 if(r->next) 156 next_realm = r->next->realm; 157 else 158 next_realm = server_realm; 159 ret = make_path(context, r, prev_realm, next_realm); 160 if(ret){ 161 free_realms(realms); 162 return ret; 163 } 164 } 165 prev_realm = r->realm; 166 } 167 return 0; 168 } 169 170 static int 171 expand_realms(krb5_context context, 172 struct tr_realm *realms, const char *client_realm) 173 { 174 struct tr_realm *r; 175 const char *prev_realm = NULL; 176 for(r = realms; r; r = r->next){ 177 if(r->trailing_dot){ 178 char *tmp; 179 size_t len; 180 181 if(prev_realm == NULL) 182 prev_realm = client_realm; 183 184 len = strlen(r->realm) + strlen(prev_realm) + 1; 185 186 tmp = realloc(r->realm, len); 187 if(tmp == NULL){ 188 free_realms(realms); 189 krb5_set_error_string (context, "malloc: out of memory"); 190 return ENOMEM; 191 } 192 r->realm = tmp; 193 strlcat(r->realm, prev_realm, len); 194 }else if(r->leading_slash && !r->leading_space && prev_realm){ 195 /* yet another exception: if you use x500-names, the 196 leading realm doesn't have to be "quoted" with a space */ 197 char *tmp; 198 size_t len = strlen(r->realm) + strlen(prev_realm) + 1; 199 200 tmp = malloc(len); 201 if(tmp == NULL){ 202 free_realms(realms); 203 krb5_set_error_string (context, "malloc: out of memory"); 204 return ENOMEM; 205 } 206 strlcpy(tmp, prev_realm, len); 207 strlcat(tmp, r->realm, len); 208 free(r->realm); 209 r->realm = tmp; 210 } 211 prev_realm = r->realm; 212 } 213 return 0; 214 } 215 216 static struct tr_realm * 217 make_realm(char *realm) 218 { 219 struct tr_realm *r; 220 char *p, *q; 221 int quote = 0; 222 r = calloc(1, sizeof(*r)); 223 if(r == NULL){ 224 free(realm); 225 return NULL; 226 } 227 r->realm = realm; 228 for(p = q = r->realm; *p; p++){ 229 if(p == r->realm && *p == ' '){ 230 r->leading_space = 1; 231 continue; 232 } 233 if(q == r->realm && *p == '/') 234 r->leading_slash = 1; 235 if(quote){ 236 *q++ = *p; 237 quote = 0; 238 continue; 239 } 240 if(*p == '\\'){ 241 quote = 1; 242 continue; 243 } 244 if(p[0] == '.' && p[1] == '\0') 245 r->trailing_dot = 1; 246 *q++ = *p; 247 } 248 *q = '\0'; 249 return r; 250 } 251 252 static struct tr_realm* 253 append_realm(struct tr_realm *head, struct tr_realm *r) 254 { 255 struct tr_realm *p; 256 if(head == NULL){ 257 r->next = NULL; 258 return r; 259 } 260 p = head; 261 while(p->next) p = p->next; 262 p->next = r; 263 return head; 264 } 265 266 static int 267 decode_realms(krb5_context context, 268 const char *tr, int length, struct tr_realm **realms) 269 { 270 struct tr_realm *r = NULL; 271 272 char *tmp; 273 int quote = 0; 274 const char *start = tr; 275 int i; 276 277 for(i = 0; i < length; i++){ 278 if(quote){ 279 quote = 0; 280 continue; 281 } 282 if(tr[i] == '\\'){ 283 quote = 1; 284 continue; 285 } 286 if(tr[i] == ','){ 287 tmp = malloc(tr + i - start + 1); 288 if(tmp == NULL){ 289 krb5_set_error_string (context, "malloc: out of memory"); 290 return ENOMEM; 291 } 292 memcpy(tmp, start, tr + i - start); 293 tmp[tr + i - start] = '\0'; 294 r = make_realm(tmp); 295 if(r == NULL){ 296 free_realms(*realms); 297 krb5_set_error_string (context, "malloc: out of memory"); 298 return ENOMEM; 299 } 300 *realms = append_realm(*realms, r); 301 start = tr + i + 1; 302 } 303 } 304 tmp = malloc(tr + i - start + 1); 305 if(tmp == NULL){ 306 free(*realms); 307 krb5_set_error_string (context, "malloc: out of memory"); 308 return ENOMEM; 309 } 310 memcpy(tmp, start, tr + i - start); 311 tmp[tr + i - start] = '\0'; 312 r = make_realm(tmp); 313 if(r == NULL){ 314 free_realms(*realms); 315 krb5_set_error_string (context, "malloc: out of memory"); 316 return ENOMEM; 317 } 318 *realms = append_realm(*realms, r); 319 320 return 0; 321 } 322 323 324 krb5_error_code KRB5_LIB_FUNCTION 325 krb5_domain_x500_decode(krb5_context context, 326 krb5_data tr, char ***realms, int *num_realms, 327 const char *client_realm, const char *server_realm) 328 { 329 struct tr_realm *r = NULL; 330 struct tr_realm *p, **q; 331 int ret; 332 333 if(tr.length == 0) { 334 *realms = NULL; 335 *num_realms = 0; 336 return 0; 337 } 338 339 /* split string in components */ 340 ret = decode_realms(context, tr.data, tr.length, &r); 341 if(ret) 342 return ret; 343 344 /* apply prefix rule */ 345 ret = expand_realms(context, r, client_realm); 346 if(ret) 347 return ret; 348 349 ret = make_paths(context, r, client_realm, server_realm); 350 if(ret) 351 return ret; 352 353 /* remove empty components and count realms */ 354 q = &r; 355 *num_realms = 0; 356 for(p = r; p; ){ 357 if(p->realm[0] == '\0'){ 358 free(p->realm); 359 *q = p->next; 360 free(p); 361 p = *q; 362 }else{ 363 q = &p->next; 364 p = p->next; 365 (*num_realms)++; 366 } 367 } 368 if (*num_realms < 0 || *num_realms + 1 > UINT_MAX/sizeof(**realms)) 369 return ERANGE; 370 371 { 372 char **R; 373 R = malloc((*num_realms + 1) * sizeof(*R)); 374 if (R == NULL) 375 return ENOMEM; 376 *realms = R; 377 while(r){ 378 *R++ = r->realm; 379 p = r->next; 380 free(r); 381 r = p; 382 } 383 } 384 return 0; 385 } 386 387 krb5_error_code KRB5_LIB_FUNCTION 388 krb5_domain_x500_encode(char **realms, int num_realms, krb5_data *encoding) 389 { 390 char *s = NULL; 391 int len = 0; 392 int i; 393 krb5_data_zero(encoding); 394 if (num_realms == 0) 395 return 0; 396 for(i = 0; i < num_realms; i++){ 397 len += strlen(realms[i]); 398 if(realms[i][0] == '/') 399 len++; 400 } 401 len += num_realms - 1; 402 s = malloc(len + 1); 403 if (s == NULL) 404 return ENOMEM; 405 *s = '\0'; 406 for(i = 0; i < num_realms; i++){ 407 if(i && i < num_realms - 1) 408 strlcat(s, ",", len + 1); 409 if(realms[i][0] == '/') 410 strlcat(s, " ", len + 1); 411 strlcat(s, realms[i], len + 1); 412 } 413 encoding->data = s; 414 encoding->length = strlen(s); 415 return 0; 416 } 417 418 krb5_error_code KRB5_LIB_FUNCTION 419 krb5_check_transited(krb5_context context, 420 krb5_const_realm client_realm, 421 krb5_const_realm server_realm, 422 krb5_realm *realms, 423 int num_realms, 424 int *bad_realm) 425 { 426 char **tr_realms; 427 char **p; 428 int i; 429 430 if(num_realms == 0) 431 return 0; 432 433 tr_realms = krb5_config_get_strings(context, NULL, 434 "capaths", 435 client_realm, 436 server_realm, 437 NULL); 438 for(i = 0; i < num_realms; i++) { 439 for(p = tr_realms; p && *p; p++) { 440 if(strcmp(*p, realms[i]) == 0) 441 break; 442 } 443 if(p == NULL || *p == NULL) { 444 krb5_config_free_strings(tr_realms); 445 krb5_set_error_string (context, "no transit through realm %s", 446 realms[i]); 447 if(bad_realm) 448 *bad_realm = i; 449 return KRB5KRB_AP_ERR_ILL_CR_TKT; 450 } 451 } 452 krb5_config_free_strings(tr_realms); 453 return 0; 454 } 455 456 krb5_error_code KRB5_LIB_FUNCTION 457 krb5_check_transited_realms(krb5_context context, 458 const char *const *realms, 459 int num_realms, 460 int *bad_realm) 461 { 462 int i; 463 int ret = 0; 464 char **bad_realms = krb5_config_get_strings(context, NULL, 465 "libdefaults", 466 "transited_realms_reject", 467 NULL); 468 if(bad_realms == NULL) 469 return 0; 470 471 for(i = 0; i < num_realms; i++) { 472 char **p; 473 for(p = bad_realms; *p; p++) 474 if(strcmp(*p, realms[i]) == 0) { 475 krb5_set_error_string (context, "no transit through realm %s", 476 *p); 477 ret = KRB5KRB_AP_ERR_ILL_CR_TKT; 478 if(bad_realm) 479 *bad_realm = i; 480 break; 481 } 482 } 483 krb5_config_free_strings(bad_realms); 484 return ret; 485 } 486 487 #if 0 488 int 489 main(int argc, char **argv) 490 { 491 krb5_data x; 492 char **r; 493 int num, i; 494 x.data = argv[1]; 495 x.length = strlen(x.data); 496 if(domain_expand(x, &r, &num, argv[2], argv[3])) 497 exit(1); 498 for(i = 0; i < num; i++) 499 printf("%s\n", r[i]); 500 return 0; 501 } 502 #endif 503 504