2 * Copyright (c) 1997 - 2001, 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
34 #include "krb5_locl.h"
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
43 unsigned leading_space
:1;
44 unsigned leading_slash
:1;
45 unsigned trailing_dot
:1;
46 struct tr_realm
*next
;
50 free_realms(struct tr_realm
*r
)
62 make_path(krb5_context context
, struct tr_realm
*r
,
63 const char *from
, const char *to
)
68 if(strlen(from
) < strlen(to
)){
75 if(strcmp(from
+ strlen(from
) - strlen(to
), to
) == 0){
80 krb5_clear_error_message (context
);
81 return KRB5KDC_ERR_POLICY
;
84 if(strcmp(p
, to
) == 0)
86 tmp
= calloc(1, sizeof(*tmp
));
88 return krb5_enomem(context
);
91 tmp
->realm
= strdup(p
);
92 if(tmp
->realm
== NULL
){
95 return krb5_enomem(context
);
98 }else if(strncmp(from
, to
, strlen(to
)) == 0){
99 p
= from
+ strlen(from
);
101 while(p
>= from
&& *p
!= '/') p
--;
103 return KRB5KDC_ERR_POLICY
;
105 if(strncmp(to
, from
, p
- from
) == 0)
107 tmp
= calloc(1, sizeof(*tmp
));
109 return krb5_enomem(context
);
112 tmp
->realm
= malloc(p
- from
+ 1);
113 if(tmp
->realm
== NULL
){
116 return krb5_enomem(context
);
118 memcpy(tmp
->realm
, from
, p
- from
);
119 tmp
->realm
[p
- from
] = '\0';
123 krb5_clear_error_message (context
);
124 return KRB5KDC_ERR_POLICY
;
131 make_paths(krb5_context context
,
132 struct tr_realm
*realms
, const char *client_realm
,
133 const char *server_realm
)
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')
147 next_realm
= r
->next
->realm
;
149 next_realm
= server_realm
;
150 ret
= make_path(context
, r
, prev_realm
, next_realm
);
156 prev_realm
= r
->realm
;
162 expand_realms(krb5_context context
,
163 struct tr_realm
*realms
, const char *client_realm
)
166 const char *prev_realm
= NULL
;
167 for(r
= realms
; r
; r
= r
->next
){
172 if(prev_realm
== NULL
)
173 prev_realm
= client_realm
;
175 len
= strlen(r
->realm
) + strlen(prev_realm
) + 1;
177 tmp
= realloc(r
->realm
, len
);
180 return krb5_enomem(context
);
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 */
188 size_t len
= strlen(r
->realm
) + strlen(prev_realm
) + 1;
193 return krb5_enomem(context
);
195 strlcpy(tmp
, prev_realm
, len
);
196 strlcat(tmp
, r
->realm
, len
);
200 prev_realm
= r
->realm
;
205 static struct tr_realm
*
206 make_realm(char *realm
)
211 r
= calloc(1, sizeof(*r
));
217 for(p
= q
= r
->realm
; *p
; p
++){
218 if(p
== r
->realm
&& *p
== ' '){
219 r
->leading_space
= 1;
222 if(q
== r
->realm
&& *p
== '/')
223 r
->leading_slash
= 1;
233 if(p
[0] == '.' && p
[1] == '\0')
241 static struct tr_realm
*
242 append_realm(struct tr_realm
*head
, struct tr_realm
*r
)
250 while(p
->next
) p
= p
->next
;
256 decode_realms(krb5_context context
,
257 const char *tr
, int length
, struct tr_realm
**realms
)
259 struct tr_realm
*r
= NULL
;
263 const char *start
= tr
;
266 for(i
= 0; i
< length
; i
++){
276 tmp
= malloc(tr
+ i
- start
+ 1);
278 return krb5_enomem(context
);
279 memcpy(tmp
, start
, tr
+ i
- start
);
280 tmp
[tr
+ i
- start
] = '\0';
283 free_realms(*realms
);
284 return krb5_enomem(context
);
286 *realms
= append_realm(*realms
, r
);
290 tmp
= malloc(tr
+ i
- start
+ 1);
293 return krb5_enomem(context
);
295 memcpy(tmp
, start
, tr
+ i
- start
);
296 tmp
[tr
+ i
- start
] = '\0';
299 free_realms(*realms
);
300 return krb5_enomem(context
);
302 *realms
= append_realm(*realms
, r
);
308 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
309 krb5_domain_x500_decode(krb5_context context
,
310 krb5_data tr
, char ***realms
, unsigned int *num_realms
,
311 const char *client_realm
, const char *server_realm
)
313 struct tr_realm
*r
= NULL
;
314 struct tr_realm
*p
, **q
;
323 /* split string in components */
324 ret
= decode_realms(context
, tr
.data
, tr
.length
, &r
);
328 /* apply prefix rule */
329 ret
= expand_realms(context
, r
, client_realm
);
333 ret
= make_paths(context
, r
, client_realm
, server_realm
);
337 /* remove empty components and count realms */
340 if((*q
)->realm
[0] == '\0'){
350 if (*num_realms
+ 1 > UINT_MAX
/sizeof(**realms
))
355 R
= malloc((*num_realms
+ 1) * sizeof(*R
));
357 return krb5_enomem(context
);
369 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
370 krb5_domain_x500_encode(char **realms
, unsigned int num_realms
,
376 krb5_data_zero(encoding
);
379 for(i
= 0; i
< num_realms
; i
++){
380 len
+= strlen(realms
[i
]);
381 if(realms
[i
][0] == '/')
384 len
+= num_realms
- 1;
389 for(i
= 0; i
< num_realms
; i
++){
391 strlcat(s
, ",", len
+ 1);
392 if(realms
[i
][0] == '/')
393 strlcat(s
, " ", len
+ 1);
394 strlcat(s
, realms
[i
], len
+ 1);
397 encoding
->length
= strlen(s
);
401 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
402 _krb5_free_capath(krb5_context context
, char **capath
)
406 for (s
= capath
; s
&& *s
; ++s
)
412 const char *local_realm
;
413 const char *server_realm
;
414 const char *lr
; /* Pointer into tail of local realm */
415 const char *sr
; /* Pointer into tail of server realm */
416 size_t llen
; /* Length of local_realm */
417 size_t slen
; /* Length of server_realm */
418 size_t len
; /* Length of common suffix */
419 size_t num
; /* Path element count */
423 * Step up from local_realm to common suffix, or else down to server_realm.
426 hier_next(struct hier_iter
*state
)
428 const char *lr
= state
->lr
;
429 const char *sr
= state
->sr
;
430 const char *lsuffix
= state
->local_realm
+ state
->llen
- state
->len
;
431 const char *server_realm
= state
->server_realm
;
436 return state
->lr
= lr
;
440 while (--sr
>= server_realm
)
441 if (sr
== server_realm
|| sr
[-1] == '.')
442 return state
->sr
= sr
;
449 hier_init(struct hier_iter
*state
, const char *local_realm
, const char *server_realm
)
457 state
->local_realm
= local_realm
;
458 state
->server_realm
= server_realm
;
459 state
->llen
= llen
= strlen(local_realm
);
460 state
->slen
= slen
= strlen(server_realm
);
464 if (slen
== 0 || llen
== 0)
467 /* Find first difference from the back */
468 for (lr
= local_realm
+ llen
, sr
= server_realm
+ slen
;
469 lr
!= local_realm
&& sr
!= server_realm
;
471 if (lr
[-1] != sr
[-1])
474 len
= llen
- (lr
- local_realm
);
477 /* Nothing in common? */
481 /* Everything in common? */
482 if (llen
== slen
&& lr
== local_realm
)
485 /* Is one realm is a suffix of the other? */
486 if ((llen
< slen
&& lr
== local_realm
&& sr
[-1] == '.') ||
487 (llen
> slen
&& sr
== server_realm
&& lr
[-1] == '.'))
488 len
= llen
- (lr
- local_realm
);
491 /* `lr` starts at local realm and walks up the tree to common suffix */
492 state
->lr
= local_realm
;
493 /* `sr` starts at common suffix in server realm and walks down the tree */
494 state
->sr
= server_realm
+ slen
- len
;
496 /* Count elements and reset */
497 while (hier_next(state
) != NULL
)
499 state
->lr
= local_realm
;
500 state
->sr
= server_realm
+ slen
- len
;
504 * Find a referral path from client_realm to server_realm via local_realm.
505 * Either via [capaths] or hierarchicaly.
507 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
508 _krb5_find_capath(krb5_context context
,
509 const char *client_realm
,
510 const char *local_realm
,
511 const char *server_realm
,
512 krb5_boolean use_hierarchical
,
518 struct hier_iter hier_state
;
525 confpath
= krb5_config_get_strings(context
, NULL
, "capaths",
526 client_realm
, server_realm
, NULL
);
527 if (confpath
== NULL
)
528 confpath
= krb5_config_get_strings(context
, NULL
, "capaths",
529 local_realm
, server_realm
, NULL
);
531 * With a [capaths] setting from the client to the server we look for our
532 * own realm in the list. If our own realm is not present, we return the
533 * full list. Otherwise, we return our realm's successors, or possibly
534 * NULL. Ignoring a [capaths] settings risks loops plus would violate
535 * explicit policy and the principle of least surpise.
537 if (confpath
!= NULL
) {
538 char **start
= confpath
;
542 for (rp
= start
; *rp
; rp
++)
543 if (strcmp(*rp
, local_realm
) == 0)
548 krb5_config_free_strings(confpath
);
552 capath
= calloc(n
+ 1, sizeof(*capath
));
553 if (capath
== NULL
) {
554 krb5_config_free_strings(confpath
);
555 return krb5_enomem(context
);
558 for (i
= 0, rp
= start
; *rp
; rp
++) {
559 if ((capath
[i
++] = strdup(*rp
)) == NULL
) {
560 _krb5_free_capath(context
, capath
);
561 krb5_config_free_strings(confpath
);
562 return krb5_enomem(context
);
565 krb5_config_free_strings(confpath
);
572 /* The use_hierarchical flag makes hierarchical path lookup unconditional */
573 if (! use_hierarchical
&&
574 ! krb5_config_get_bool_default(context
, NULL
, TRUE
, "libdefaults",
575 "allow_hierarchical_capaths", NULL
))
579 * When validating transit paths, local_realm == client_realm. Otherwise,
580 * with hierarchical referrals, they may differ, and we may be building a
581 * path forward from our own realm!
583 hier_init(&hier_state
, local_realm
, server_realm
);
584 if (hier_state
.num
== 0)
587 rp
= capath
= calloc(hier_state
.num
+ 1, sizeof(*capath
));
589 return krb5_enomem(context
);
590 while ((r
= hier_next(&hier_state
)) != NULL
) {
591 if ((*rp
++ = strdup(r
)) == NULL
) {
592 _krb5_free_capath(context
, capath
);
593 return krb5_enomem(context
);
599 *npath
= hier_state
.num
;
603 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
604 krb5_check_transited(krb5_context context
,
605 krb5_const_realm client_realm
,
606 krb5_const_realm server_realm
,
608 unsigned int num_realms
,
611 krb5_error_code ret
= 0;
612 char **capath
= NULL
;
613 size_t num_capath
= 0;
617 /* In transit checks hierarchical capaths are optional */
618 ret
= _krb5_find_capath(context
, client_realm
, client_realm
, server_realm
,
619 FALSE
, &capath
, &num_capath
);
623 for (i
= 0; i
< num_realms
; i
++) {
624 for (j
= 0; j
< num_capath
; ++j
) {
625 if (strcmp(realms
[i
], capath
[j
]) == 0)
628 if (j
== num_capath
) {
629 _krb5_free_capath(context
, capath
);
630 krb5_set_error_message (context
, KRB5KRB_AP_ERR_ILL_CR_TKT
,
631 N_("no transit allowed "
632 "through realm %s from %s to %s", ""),
633 realms
[i
], client_realm
, server_realm
);
636 return KRB5KRB_AP_ERR_ILL_CR_TKT
;
640 _krb5_free_capath(context
, capath
);
644 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
645 krb5_check_transited_realms(krb5_context context
,
646 const char *const *realms
,
647 unsigned int num_realms
,
652 char **bad_realms
= krb5_config_get_strings(context
, NULL
,
654 "transited_realms_reject",
656 if(bad_realms
== NULL
)
659 for(i
= 0; i
< num_realms
; i
++) {
661 for(p
= bad_realms
; *p
; p
++)
662 if(strcmp(*p
, realms
[i
]) == 0) {
663 ret
= KRB5KRB_AP_ERR_ILL_CR_TKT
;
664 krb5_set_error_message (context
, ret
,
665 N_("no transit allowed "
666 "through realm %s", ""),
673 krb5_config_free_strings(bad_realms
);
679 main(int argc
, char **argv
)
685 x
.length
= strlen(x
.data
);
686 if(domain_expand(x
, &r
, &num
, argv
[2], argv
[3]))
688 for(i
= 0; i
< num
; i
++)
689 printf("%s\n", r
[i
]);