| 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 |
/* this is an attempt at one of the most horrible `compression' |
| 37 |
schemes that has ever been invented; it's so amazingly brain-dead |
| 38 |
that words can not describe it, and all this just to save a few |
| 39 |
silly bytes */ |
| 40 |
|
| 41 |
struct tr_realm { |
| 42 |
char *realm; |
| 43 |
unsigned leading_space:1; |
| 44 |
unsigned leading_slash:1; |
| 45 |
unsigned trailing_dot:1; |
| 46 |
struct tr_realm *next; |
| 47 |
}; |
| 48 |
|
| 49 |
static void |
| 50 |
free_realms(struct tr_realm *r) |
| 51 |
{ |
| 52 |
struct tr_realm *p; |
| 53 |
while(r){ |
| 54 |
p = r; |
| 55 |
r = r->next; |
| 56 |
free(p->realm); |
| 57 |
free(p); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
static int |
| 62 |
make_path(krb5_context context, struct tr_realm *r, |
| 63 |
const char *from, const char *to) |
| 64 |
{ |
| 65 |
struct tr_realm *tmp; |
| 66 |
const char *p; |
| 67 |
|
| 68 |
if(strlen(from) < strlen(to)){ |
| 69 |
const char *str; |
| 70 |
str = from; |
| 71 |
from = to; |
| 72 |
to = str; |
| 73 |
} |
| 74 |
|
| 75 |
if(strcmp(from + strlen(from) - strlen(to), to) == 0){ |
| 76 |
p = from; |
| 77 |
while(1){ |
| 78 |
p = strchr(p, '.'); |
| 79 |
if(p == NULL) { |
| 80 |
krb5_clear_error_message (context); |
| 81 |
return KRB5KDC_ERR_POLICY; |
| 82 |
} |
| 83 |
p++; |
| 84 |
if(strcmp(p, to) == 0) |
| 85 |
break; |
| 86 |
tmp = calloc(1, sizeof(*tmp)); |
| 87 |
if(tmp == NULL) |
| 88 |
return krb5_enomem(context); |
| 89 |
tmp->next = r->next; |
| 90 |
r->next = tmp; |
| 91 |
tmp->realm = strdup(p); |
| 92 |
if(tmp->realm == NULL){ |
| 93 |
r->next = tmp->next; |
| 94 |
free(tmp); |
| 95 |
return krb5_enomem(context); |
| 96 |
} |
| 97 |
} |
| 98 |
}else if(strncmp(from, to, strlen(to)) == 0){ |
| 99 |
p = from + strlen(from); |
| 100 |
while(1){ |
| 101 |
while(p >= from && *p != '/') p--; |
| 102 |
if(p == from) |
| 103 |
return KRB5KDC_ERR_POLICY; |
| 104 |
|
| 105 |
if(strncmp(to, from, p - from) == 0) |
| 106 |
break; |
| 107 |
tmp = calloc(1, sizeof(*tmp)); |
| 108 |
if(tmp == NULL) |
| 109 |
return krb5_enomem(context); |
| 110 |
tmp->next = r->next; |
| 111 |
r->next = tmp; |
| 112 |
tmp->realm = malloc(p - from + 1); |
| 113 |
if(tmp->realm == NULL){ |
| 114 |
r->next = tmp->next; |
| 115 |
free(tmp); |
| 116 |
return krb5_enomem(context); |
| 117 |
} |
| 118 |
memcpy(tmp->realm, from, p - from); |
| 119 |
tmp->realm[p - from] = '\0'; |
| 120 |
p--; |
| 121 |
} |
| 122 |
} else { |
| 123 |
krb5_clear_error_message (context); |
| 124 |
return KRB5KDC_ERR_POLICY; |
| 125 |
} |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
static int |
| 131 |
make_paths(krb5_context context, |
| 132 |
struct tr_realm *realms, const char *client_realm, |
| 133 |
const char *server_realm) |
| 134 |
{ |
| 135 |
struct tr_realm *r; |
| 136 |
int ret; |
| 137 |
const char *prev_realm = client_realm; |
| 138 |
const char *next_realm = NULL; |
| 139 |
for(r = realms; r; r = r->next){ |
| 140 |
/* it *might* be that you can have more than one empty |
| 141 |
component in a row, at least that's how I interpret the |
| 142 |
"," exception in 1510 */ |
| 143 |
if(r->realm[0] == '\0'){ |
| 144 |
while(r->next && r->next->realm[0] == '\0') |
| 145 |
r = r->next; |
| 146 |
if(r->next) |
| 147 |
next_realm = r->next->realm; |
| 148 |
else |
| 149 |
next_realm = server_realm; |
| 150 |
ret = make_path(context, r, prev_realm, next_realm); |
| 151 |
if(ret){ |
| 152 |
free_realms(realms); |
| 153 |
return ret; |
| 154 |
} |
| 155 |
} |
| 156 |
prev_realm = r->realm; |
| 157 |
} |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
static int |
| 162 |
expand_realms(krb5_context context, |
| 163 |
struct tr_realm *realms, const char *client_realm) |
| 164 |
{ |
| 165 |
struct tr_realm *r; |
| 166 |
const char *prev_realm = NULL; |
| 167 |
for(r = realms; r; r = r->next){ |
| 168 |
if(r->trailing_dot){ |
| 169 |
char *tmp; |
| 170 |
size_t len; |
| 171 |
|
| 172 |
if(prev_realm == NULL) |
| 173 |
prev_realm = client_realm; |
| 174 |
|
| 175 |
len = strlen(r->realm) + strlen(prev_realm) + 1; |
| 176 |
|
| 177 |
tmp = realloc(r->realm, len); |
| 178 |
if(tmp == NULL){ |
| 179 |
free_realms(realms); |
| 180 |
return krb5_enomem(context); |
| 181 |
} |
| 182 |
r->realm = tmp; |
| 183 |
strlcat(r->realm, prev_realm, len); |
| 184 |
}else if(r->leading_slash && !r->leading_space && prev_realm){ |
| 185 |
/* yet another exception: if you use x500-names, the |
| 186 |
leading realm doesn't have to be "quoted" with a space */ |
| 187 |
char *tmp; |
| 188 |
size_t len = strlen(r->realm) + strlen(prev_realm) + 1; |
| 189 |
|
| 190 |
tmp = malloc(len); |
| 191 |
if(tmp == NULL){ |
| 192 |
free_realms(realms); |
| 193 |
return krb5_enomem(context); |
| 194 |
} |
| 195 |
strlcpy(tmp, prev_realm, len); |
| 196 |
strlcat(tmp, r->realm, len); |
| 197 |
free(r->realm); |
| 198 |
r->realm = tmp; |
| 199 |
} |
| 200 |
prev_realm = r->realm; |
| 201 |
} |
| 202 |
return 0; |
| 203 |
} |
| 204 |
|
| 205 |
static struct tr_realm * |
| 206 |
make_realm(char *realm) |
| 207 |
{ |
| 208 |
struct tr_realm *r; |
| 209 |
char *p, *q; |
| 210 |
int quote = 0; |
| 211 |
r = calloc(1, sizeof(*r)); |
| 212 |
if(r == NULL){ |
| 213 |
free(realm); |
| 214 |
return NULL; |
| 215 |
} |
| 216 |
r->realm = realm; |
| 217 |
for(p = q = r->realm; *p; p++){ |
| 218 |
if(p == r->realm && *p == ' '){ |
| 219 |
r->leading_space = 1; |
| 220 |
continue; |
| 221 |
} |
| 222 |
if(q == r->realm && *p == '/') |
| 223 |
r->leading_slash = 1; |
| 224 |
if(quote){ |
| 225 |
*q++ = *p; |
| 226 |
quote = 0; |
| 227 |
continue; |
| 228 |
} |
| 229 |
if(*p == '\\'){ |
| 230 |
quote = 1; |
| 231 |
continue; |
| 232 |
} |
| 233 |
if(p[0] == '.' && p[1] == '\0') |
| 234 |
r->trailing_dot = 1; |
| 235 |
*q++ = *p; |
| 236 |
} |
| 237 |
*q = '\0'; |
| 238 |
return r; |
| 239 |
} |
| 240 |
|
| 241 |
static struct tr_realm* |
| 242 |
append_realm(struct tr_realm *head, struct tr_realm *r) |
| 243 |
{ |
| 244 |
struct tr_realm *p; |
| 245 |
if(head == NULL){ |
| 246 |
r->next = NULL; |
| 247 |
return r; |
| 248 |
} |
| 249 |
p = head; |
| 250 |
while(p->next) p = p->next; |
| 251 |
p->next = r; |
| 252 |
return head; |
| 253 |
} |
| 254 |
|
| 255 |
static int |
| 256 |
decode_realms(krb5_context context, |
| 257 |
const char *tr, int length, struct tr_realm **realms) |
| 258 |
{ |
| 259 |
struct tr_realm *r = NULL; |
| 260 |
|
| 261 |
char *tmp; |
| 262 |
int quote = 0; |
| 263 |
const char *start = tr; |
| 264 |
int i; |
| 265 |
|
| 266 |
for(i = 0; i < length; i++){ |
| 267 |
if(quote){ |
| 268 |
quote = 0; |
| 269 |
continue; |
| 270 |
} |
| 271 |
if(tr[i] == '\\'){ |
| 272 |
quote = 1; |
| 273 |
continue; |
| 274 |
} |
| 275 |
if(tr[i] == ','){ |
| 276 |
tmp = malloc(tr + i - start + 1); |
| 277 |
if(tmp == NULL) |
| 278 |
return krb5_enomem(context); |
| 279 |
memcpy(tmp, start, tr + i - start); |
| 280 |
tmp[tr + i - start] = '\0'; |
| 281 |
r = make_realm(tmp); |
| 282 |
if(r == NULL){ |
| 283 |
free_realms(*realms); |
| 284 |
*realms = NULL; |
| 285 |
return krb5_enomem(context); |
| 286 |
} |
| 287 |
*realms = append_realm(*realms, r); |
| 288 |
start = tr + i + 1; |
| 289 |
} |
| 290 |
} |
| 291 |
tmp = malloc(tr + i - start + 1); |
| 292 |
if(tmp == NULL){ |
| 293 |
free_realms(*realms); |
| 294 |
*realms = NULL; |
| 295 |
return krb5_enomem(context); |
| 296 |
} |
| 297 |
memcpy(tmp, start, tr + i - start); |
| 298 |
tmp[tr + i - start] = '\0'; |
| 299 |
r = make_realm(tmp); |
| 300 |
if(r == NULL){ |
| 301 |
free_realms(*realms); |
| 302 |
*realms = NULL; |
| 303 |
return krb5_enomem(context); |
| 304 |
} |
| 305 |
*realms = append_realm(*realms, r); |
| 306 |
|
| 307 |
return 0; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 312 |
krb5_domain_x500_decode(krb5_context context, |
| 313 |
krb5_data tr, char ***realms, unsigned int *num_realms, |
| 314 |
const char *client_realm, const char *server_realm) |
| 315 |
{ |
| 316 |
struct tr_realm *r = NULL; |
| 317 |
struct tr_realm *p, **q; |
| 318 |
int ret; |
| 319 |
|
| 320 |
if(tr.length == 0) { |
| 321 |
*realms = NULL; |
| 322 |
*num_realms = 0; |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
/* split string in components */ |
| 327 |
ret = decode_realms(context, tr.data, tr.length, &r); |
| 328 |
if(ret) |
| 329 |
return ret; |
| 330 |
|
| 331 |
/* apply prefix rule */ |
| 332 |
ret = expand_realms(context, r, client_realm); |
| 333 |
if(ret) |
| 334 |
return ret; |
| 335 |
|
| 336 |
ret = make_paths(context, r, client_realm, server_realm); |
| 337 |
if(ret) |
| 338 |
return ret; |
| 339 |
|
| 340 |
/* remove empty components and count realms */ |
| 341 |
*num_realms = 0; |
| 342 |
for(q = &r; *q; ){ |
| 343 |
if((*q)->realm[0] == '\0'){ |
| 344 |
p = *q; |
| 345 |
*q = (*q)->next; |
| 346 |
free(p->realm); |
| 347 |
free(p); |
| 348 |
}else{ |
| 349 |
q = &(*q)->next; |
| 350 |
(*num_realms)++; |
| 351 |
} |
| 352 |
} |
| 353 |
if (*num_realms + 1 > UINT_MAX/sizeof(**realms)) |
| 354 |
return ERANGE; |
| 355 |
|
| 356 |
{ |
| 357 |
char **R; |
| 358 |
R = malloc((*num_realms + 1) * sizeof(*R)); |
| 359 |
if (R == NULL) |
| 360 |
return krb5_enomem(context); |
| 361 |
*realms = R; |
| 362 |
while(r){ |
| 363 |
*R++ = r->realm; |
| 364 |
p = r->next; |
| 365 |
free(r); |
| 366 |
r = p; |
| 367 |
} |
| 368 |
} |
| 369 |
return 0; |
| 370 |
} |
| 371 |
|
| 372 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 373 |
krb5_domain_x500_encode(char **realms, unsigned int num_realms, |
| 374 |
krb5_data *encoding) |
| 375 |
{ |
| 376 |
char *s = NULL; |
| 377 |
int len = 0; |
| 378 |
unsigned int i; |
| 379 |
krb5_data_zero(encoding); |
| 380 |
if (num_realms == 0) |
| 381 |
return 0; |
| 382 |
for(i = 0; i < num_realms; i++){ |
| 383 |
len += strlen(realms[i]); |
| 384 |
if(realms[i][0] == '/') |
| 385 |
len++; |
| 386 |
} |
| 387 |
len += num_realms - 1; |
| 388 |
s = malloc(len + 1); |
| 389 |
if (s == NULL) |
| 390 |
return ENOMEM; |
| 391 |
*s = '\0'; |
| 392 |
for(i = 0; i < num_realms; i++){ |
| 393 |
if(i) |
| 394 |
strlcat(s, ",", len + 1); |
| 395 |
if(realms[i][0] == '/') |
| 396 |
strlcat(s, " ", len + 1); |
| 397 |
strlcat(s, realms[i], len + 1); |
| 398 |
} |
| 399 |
encoding->data = s; |
| 400 |
encoding->length = strlen(s); |
| 401 |
return 0; |
| 402 |
} |
| 403 |
|
| 404 |
KRB5_LIB_FUNCTION void KRB5_LIB_CALL |
| 405 |
_krb5_free_capath(krb5_context context, char **capath) |
| 406 |
{ |
| 407 |
char **s; |
| 408 |
|
| 409 |
for (s = capath; s && *s; ++s) |
| 410 |
free(*s); |
| 411 |
free(capath); |
| 412 |
} |
| 413 |
|
| 414 |
struct hier_iter { |
| 415 |
const char *local_realm; |
| 416 |
const char *server_realm; |
| 417 |
const char *lr; /* Pointer into tail of local realm */ |
| 418 |
const char *sr; /* Pointer into tail of server realm */ |
| 419 |
size_t llen; /* Length of local_realm */ |
| 420 |
size_t slen; /* Length of server_realm */ |
| 421 |
size_t len; /* Length of common suffix */ |
| 422 |
size_t num; /* Path element count */ |
| 423 |
}; |
| 424 |
|
| 425 |
/* |
| 426 |
* Step up from local_realm to common suffix, or else down to server_realm. |
| 427 |
*/ |
| 428 |
static const char * |
| 429 |
hier_next(struct hier_iter *state) |
| 430 |
{ |
| 431 |
const char *lr = state->lr; |
| 432 |
const char *sr = state->sr; |
| 433 |
const char *lsuffix = state->local_realm + state->llen - state->len; |
| 434 |
const char *server_realm = state->server_realm; |
| 435 |
|
| 436 |
if (lr != NULL) { |
| 437 |
while (lr < lsuffix) |
| 438 |
if (*lr++ == '.') |
| 439 |
return state->lr = lr; |
| 440 |
state->lr = NULL; |
| 441 |
} |
| 442 |
if (sr != NULL) { |
| 443 |
while (--sr >= server_realm) |
| 444 |
if (sr == server_realm || sr[-1] == '.') |
| 445 |
return state->sr = sr; |
| 446 |
state->sr = NULL; |
| 447 |
} |
| 448 |
return NULL; |
| 449 |
} |
| 450 |
|
| 451 |
static void |
| 452 |
hier_init(struct hier_iter *state, const char *local_realm, const char *server_realm) |
| 453 |
{ |
| 454 |
size_t llen; |
| 455 |
size_t slen; |
| 456 |
size_t len = 0; |
| 457 |
const char *lr; |
| 458 |
const char *sr; |
| 459 |
|
| 460 |
state->local_realm = local_realm; |
| 461 |
state->server_realm = server_realm; |
| 462 |
state->llen = llen = strlen(local_realm); |
| 463 |
state->slen = slen = strlen(server_realm); |
| 464 |
state->len = 0; |
| 465 |
state->num = 0; |
| 466 |
|
| 467 |
if (slen == 0 || llen == 0) |
| 468 |
return; |
| 469 |
|
| 470 |
/* Find first difference from the back */ |
| 471 |
for (lr = local_realm + llen, sr = server_realm + slen; |
| 472 |
lr != local_realm && sr != server_realm; |
| 473 |
--lr, --sr) { |
| 474 |
if (lr[-1] != sr[-1]) |
| 475 |
break; |
| 476 |
if (lr[-1] == '.') |
| 477 |
len = llen - (lr - local_realm); |
| 478 |
} |
| 479 |
|
| 480 |
/* Nothing in common? */ |
| 481 |
if (*lr == '\0') |
| 482 |
return; |
| 483 |
|
| 484 |
/* Everything in common? */ |
| 485 |
if (llen == slen && lr == local_realm) |
| 486 |
return; |
| 487 |
|
| 488 |
/* Is one realm is a suffix of the other? */ |
| 489 |
if ((llen < slen && lr == local_realm && sr[-1] == '.') || |
| 490 |
(llen > slen && sr == server_realm && lr[-1] == '.')) |
| 491 |
len = llen - (lr - local_realm); |
| 492 |
|
| 493 |
state->len = len; |
| 494 |
/* `lr` starts at local realm and walks up the tree to common suffix */ |
| 495 |
state->lr = local_realm; |
| 496 |
/* `sr` starts at common suffix in server realm and walks down the tree */ |
| 497 |
state->sr = server_realm + slen - len; |
| 498 |
|
| 499 |
/* Count elements and reset */ |
| 500 |
while (hier_next(state) != NULL) |
| 501 |
++state->num; |
| 502 |
state->lr = local_realm; |
| 503 |
state->sr = server_realm + slen - len; |
| 504 |
} |
| 505 |
|
| 506 |
/* |
| 507 |
* Find a referral path from client_realm to server_realm via local_realm. |
| 508 |
* Either via [capaths] or hierarchicaly. |
| 509 |
*/ |
| 510 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 511 |
_krb5_find_capath(krb5_context context, |
| 512 |
const char *client_realm, |
| 513 |
const char *local_realm, |
| 514 |
const char *server_realm, |
| 515 |
krb5_boolean use_hierarchical, |
| 516 |
char ***rpath, |
| 517 |
size_t *npath) |
| 518 |
{ |
| 519 |
char **confpath; |
| 520 |
char **capath; |
| 521 |
struct hier_iter hier_state; |
| 522 |
char **rp; |
| 523 |
const char *r; |
| 524 |
|
| 525 |
*rpath = NULL; |
| 526 |
*npath = 0; |
| 527 |
|
| 528 |
confpath = krb5_config_get_strings(context, NULL, "capaths", |
| 529 |
client_realm, server_realm, NULL); |
| 530 |
if (confpath == NULL) |
| 531 |
confpath = krb5_config_get_strings(context, NULL, "capaths", |
| 532 |
local_realm, server_realm, NULL); |
| 533 |
/* |
| 534 |
* With a [capaths] setting from the client to the server we look for our |
| 535 |
* own realm in the list. If our own realm is not present, we return the |
| 536 |
* full list. Otherwise, we return our realm's successors, or possibly |
| 537 |
* NULL. Ignoring a [capaths] settings risks loops plus would violate |
| 538 |
* explicit policy and the principle of least surpise. |
| 539 |
*/ |
| 540 |
if (confpath != NULL) { |
| 541 |
char **start = confpath; |
| 542 |
size_t i; |
| 543 |
size_t n; |
| 544 |
|
| 545 |
for (rp = start; *rp; rp++) |
| 546 |
if (strcmp(*rp, local_realm) == 0) |
| 547 |
start = rp+1; |
| 548 |
n = rp - start; |
| 549 |
|
| 550 |
if (n == 0) { |
| 551 |
krb5_config_free_strings(confpath); |
| 552 |
return 0; |
| 553 |
} |
| 554 |
|
| 555 |
capath = calloc(n + 1, sizeof(*capath)); |
| 556 |
if (capath == NULL) { |
| 557 |
krb5_config_free_strings(confpath); |
| 558 |
return krb5_enomem(context); |
| 559 |
} |
| 560 |
|
| 561 |
for (i = 0, rp = start; *rp; rp++) { |
| 562 |
if ((capath[i++] = strdup(*rp)) == NULL) { |
| 563 |
_krb5_free_capath(context, capath); |
| 564 |
krb5_config_free_strings(confpath); |
| 565 |
return krb5_enomem(context); |
| 566 |
} |
| 567 |
} |
| 568 |
krb5_config_free_strings(confpath); |
| 569 |
capath[i] = NULL; |
| 570 |
*rpath = capath; |
| 571 |
*npath = n; |
| 572 |
return 0; |
| 573 |
} |
| 574 |
|
| 575 |
/* The use_hierarchical flag makes hierarchical path lookup unconditional */ |
| 576 |
if (! use_hierarchical && |
| 577 |
! krb5_config_get_bool_default(context, NULL, TRUE, "libdefaults", |
| 578 |
"allow_hierarchical_capaths", NULL)) |
| 579 |
return 0; |
| 580 |
|
| 581 |
/* |
| 582 |
* When validating transit paths, local_realm == client_realm. Otherwise, |
| 583 |
* with hierarchical referrals, they may differ, and we may be building a |
| 584 |
* path forward from our own realm! |
| 585 |
*/ |
| 586 |
hier_init(&hier_state, local_realm, server_realm); |
| 587 |
if (hier_state.num == 0) |
| 588 |
return 0; |
| 589 |
|
| 590 |
rp = capath = calloc(hier_state.num + 1, sizeof(*capath)); |
| 591 |
if (capath == NULL) |
| 592 |
return krb5_enomem(context); |
| 593 |
while ((r = hier_next(&hier_state)) != NULL) { |
| 594 |
if ((*rp++ = strdup(r)) == NULL) { |
| 595 |
_krb5_free_capath(context, capath); |
| 596 |
return krb5_enomem(context); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
*rp = NULL; |
| 601 |
*rpath = capath; |
| 602 |
*npath = hier_state.num; |
| 603 |
return 0; |
| 604 |
} |
| 605 |
|
| 606 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 607 |
krb5_check_transited(krb5_context context, |
| 608 |
krb5_const_realm client_realm, |
| 609 |
krb5_const_realm server_realm, |
| 610 |
krb5_realm *realms, |
| 611 |
unsigned int num_realms, |
| 612 |
int *bad_realm) |
| 613 |
{ |
| 614 |
krb5_error_code ret = 0; |
| 615 |
char **capath = NULL; |
| 616 |
size_t num_capath = 0; |
| 617 |
size_t i = 0; |
| 618 |
size_t j = 0; |
| 619 |
|
| 620 |
/* In transit checks hierarchical capaths are optional */ |
| 621 |
ret = _krb5_find_capath(context, client_realm, client_realm, server_realm, |
| 622 |
FALSE, &capath, &num_capath); |
| 623 |
if (ret) |
| 624 |
return ret; |
| 625 |
|
| 626 |
for (i = 0; i < num_realms; i++) { |
| 627 |
for (j = 0; j < num_capath; ++j) { |
| 628 |
if (strcmp(realms[i], capath[j]) == 0) |
| 629 |
break; |
| 630 |
} |
| 631 |
if (j == num_capath) { |
| 632 |
_krb5_free_capath(context, capath); |
| 633 |
krb5_set_error_message (context, KRB5KRB_AP_ERR_ILL_CR_TKT, |
| 634 |
N_("no transit allowed " |
| 635 |
"through realm %s from %s to %s", ""), |
| 636 |
realms[i], client_realm, server_realm); |
| 637 |
if (bad_realm) |
| 638 |
*bad_realm = i; |
| 639 |
return KRB5KRB_AP_ERR_ILL_CR_TKT; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
_krb5_free_capath(context, capath); |
| 644 |
return 0; |
| 645 |
} |
| 646 |
|
| 647 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 648 |
krb5_check_transited_realms(krb5_context context, |
| 649 |
const char *const *realms, |
| 650 |
unsigned int num_realms, |
| 651 |
int *bad_realm) |
| 652 |
{ |
| 653 |
size_t i; |
| 654 |
int ret = 0; |
| 655 |
char **bad_realms = krb5_config_get_strings(context, NULL, |
| 656 |
"libdefaults", |
| 657 |
"transited_realms_reject", |
| 658 |
NULL); |
| 659 |
if(bad_realms == NULL) |
| 660 |
return 0; |
| 661 |
|
| 662 |
for(i = 0; i < num_realms; i++) { |
| 663 |
char **p; |
| 664 |
for(p = bad_realms; *p; p++) |
| 665 |
if(strcmp(*p, realms[i]) == 0) { |
| 666 |
ret = KRB5KRB_AP_ERR_ILL_CR_TKT; |
| 667 |
krb5_set_error_message (context, ret, |
| 668 |
N_("no transit allowed " |
| 669 |
"through realm %s", ""), |
| 670 |
*p); |
| 671 |
if(bad_realm) |
| 672 |
*bad_realm = i; |
| 673 |
break; |
| 674 |
} |
| 675 |
} |
| 676 |
krb5_config_free_strings(bad_realms); |
| 677 |
return ret; |
| 678 |
} |
| 679 |
|
| 680 |
#if 0 |
| 681 |
int |
| 682 |
main(int argc, char **argv) |
| 683 |
{ |
| 684 |
krb5_data x; |
| 685 |
char **r; |
| 686 |
int num, i; |
| 687 |
x.data = argv[1]; |
| 688 |
x.length = strlen(x.data); |
| 689 |
if(domain_expand(x, &r, &num, argv[2], argv[3])) |
| 690 |
exit(1); |
| 691 |
for(i = 0; i < num; i++) |
| 692 |
printf("%s\n", r[i]); |
| 693 |
return 0; |
| 694 |
} |
| 695 |
#endif |