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 krb5_set_error_message(context
, ENOMEM
,
89 N_("malloc: out of memory", ""));
94 tmp
->realm
= strdup(p
);
95 if(tmp
->realm
== NULL
){
98 krb5_set_error_message(context
, ENOMEM
,
99 N_("malloc: out of memory", ""));
103 }else if(strncmp(from
, to
, strlen(to
)) == 0){
104 p
= from
+ strlen(from
);
106 while(p
>= from
&& *p
!= '/') p
--;
108 return KRB5KDC_ERR_POLICY
;
110 if(strncmp(to
, from
, p
- from
) == 0)
112 tmp
= calloc(1, sizeof(*tmp
));
114 krb5_set_error_message(context
, ENOMEM
,
115 N_("malloc: out of memory", ""));
120 tmp
->realm
= malloc(p
- from
+ 1);
121 if(tmp
->realm
== NULL
){
124 krb5_set_error_message(context
, ENOMEM
,
125 N_("malloc: out of memory", ""));
128 memcpy(tmp
->realm
, from
, p
- from
);
129 tmp
->realm
[p
- from
] = '\0';
133 krb5_clear_error_message (context
);
134 return KRB5KDC_ERR_POLICY
;
141 make_paths(krb5_context context
,
142 struct tr_realm
*realms
, const char *client_realm
,
143 const char *server_realm
)
147 const char *prev_realm
= client_realm
;
148 const char *next_realm
= NULL
;
149 for(r
= realms
; r
; r
= r
->next
){
150 /* it *might* be that you can have more than one empty
151 component in a row, at least that's how I interpret the
152 "," exception in 1510 */
153 if(r
->realm
[0] == '\0'){
154 while(r
->next
&& r
->next
->realm
[0] == '\0')
157 next_realm
= r
->next
->realm
;
159 next_realm
= server_realm
;
160 ret
= make_path(context
, r
, prev_realm
, next_realm
);
166 prev_realm
= r
->realm
;
172 expand_realms(krb5_context context
,
173 struct tr_realm
*realms
, const char *client_realm
)
176 const char *prev_realm
= NULL
;
177 for(r
= realms
; r
; r
= r
->next
){
182 if(prev_realm
== NULL
)
183 prev_realm
= client_realm
;
185 len
= strlen(r
->realm
) + strlen(prev_realm
) + 1;
187 tmp
= realloc(r
->realm
, len
);
190 krb5_set_error_message(context
, ENOMEM
,
191 N_("malloc: out of memory", ""));
195 strlcat(r
->realm
, prev_realm
, len
);
196 }else if(r
->leading_slash
&& !r
->leading_space
&& prev_realm
){
197 /* yet another exception: if you use x500-names, the
198 leading realm doesn't have to be "quoted" with a space */
200 size_t len
= strlen(r
->realm
) + strlen(prev_realm
) + 1;
205 krb5_set_error_message(context
, ENOMEM
,
206 N_("malloc: out of memory", ""));
209 strlcpy(tmp
, prev_realm
, len
);
210 strlcat(tmp
, r
->realm
, len
);
214 prev_realm
= r
->realm
;
219 static struct tr_realm
*
220 make_realm(char *realm
)
225 r
= calloc(1, sizeof(*r
));
231 for(p
= q
= r
->realm
; *p
; p
++){
232 if(p
== r
->realm
&& *p
== ' '){
233 r
->leading_space
= 1;
236 if(q
== r
->realm
&& *p
== '/')
237 r
->leading_slash
= 1;
247 if(p
[0] == '.' && p
[1] == '\0')
255 static struct tr_realm
*
256 append_realm(struct tr_realm
*head
, struct tr_realm
*r
)
264 while(p
->next
) p
= p
->next
;
270 decode_realms(krb5_context context
,
271 const char *tr
, int length
, struct tr_realm
**realms
)
273 struct tr_realm
*r
= NULL
;
277 const char *start
= tr
;
280 for(i
= 0; i
< length
; i
++){
290 tmp
= malloc(tr
+ i
- start
+ 1);
292 krb5_set_error_message(context
, ENOMEM
,
293 N_("malloc: out of memory", ""));
296 memcpy(tmp
, start
, tr
+ i
- start
);
297 tmp
[tr
+ i
- start
] = '\0';
300 free_realms(*realms
);
301 krb5_set_error_message(context
, ENOMEM
,
302 N_("malloc: out of memory", ""));
305 *realms
= append_realm(*realms
, r
);
309 tmp
= malloc(tr
+ i
- start
+ 1);
312 krb5_set_error_message(context
, ENOMEM
,
313 N_("malloc: out of memory", ""));
316 memcpy(tmp
, start
, tr
+ i
- start
);
317 tmp
[tr
+ i
- start
] = '\0';
320 free_realms(*realms
);
321 krb5_set_error_message(context
, ENOMEM
,
322 N_("malloc: out of memory", ""));
325 *realms
= append_realm(*realms
, r
);
331 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
332 krb5_domain_x500_decode(krb5_context context
,
333 krb5_data tr
, char ***realms
, unsigned int *num_realms
,
334 const char *client_realm
, const char *server_realm
)
336 struct tr_realm
*r
= NULL
;
337 struct tr_realm
*p
, **q
;
346 /* split string in components */
347 ret
= decode_realms(context
, tr
.data
, tr
.length
, &r
);
351 /* apply prefix rule */
352 ret
= expand_realms(context
, r
, client_realm
);
356 ret
= make_paths(context
, r
, client_realm
, server_realm
);
360 /* remove empty components and count realms */
363 if((*q
)->realm
[0] == '\0'){
373 if (*num_realms
< 0 || *num_realms
+ 1 > UINT_MAX
/sizeof(**realms
))
378 R
= malloc((*num_realms
+ 1) * sizeof(*R
));
392 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
393 krb5_domain_x500_encode(char **realms
, unsigned int num_realms
,
399 krb5_data_zero(encoding
);
402 for(i
= 0; i
< num_realms
; i
++){
403 len
+= strlen(realms
[i
]);
404 if(realms
[i
][0] == '/')
407 len
+= num_realms
- 1;
412 for(i
= 0; i
< num_realms
; i
++){
413 if(i
&& i
< num_realms
- 1)
414 strlcat(s
, ",", len
+ 1);
415 if(realms
[i
][0] == '/')
416 strlcat(s
, " ", len
+ 1);
417 strlcat(s
, realms
[i
], len
+ 1);
420 encoding
->length
= strlen(s
);
424 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
425 krb5_check_transited(krb5_context context
,
426 krb5_const_realm client_realm
,
427 krb5_const_realm server_realm
,
429 unsigned int num_realms
,
439 tr_realms
= krb5_config_get_strings(context
, NULL
,
444 for(i
= 0; i
< num_realms
; i
++) {
445 for(p
= tr_realms
; p
&& *p
; p
++) {
446 if(strcmp(*p
, realms
[i
]) == 0)
449 if(p
== NULL
|| *p
== NULL
) {
450 krb5_config_free_strings(tr_realms
);
451 krb5_set_error_message (context
, KRB5KRB_AP_ERR_ILL_CR_TKT
,
452 N_("no transit allowed "
453 "through realm %s", ""),
457 return KRB5KRB_AP_ERR_ILL_CR_TKT
;
460 krb5_config_free_strings(tr_realms
);
464 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
465 krb5_check_transited_realms(krb5_context context
,
466 const char *const *realms
,
467 unsigned int num_realms
,
472 char **bad_realms
= krb5_config_get_strings(context
, NULL
,
474 "transited_realms_reject",
476 if(bad_realms
== NULL
)
479 for(i
= 0; i
< num_realms
; i
++) {
481 for(p
= bad_realms
; *p
; p
++)
482 if(strcmp(*p
, realms
[i
]) == 0) {
483 ret
= KRB5KRB_AP_ERR_ILL_CR_TKT
;
484 krb5_set_error_message (context
, ret
,
485 N_("no transit allowed "
486 "through realm %s", ""),
493 krb5_config_free_strings(bad_realms
);
499 main(int argc
, char **argv
)
505 x
.length
= strlen(x
.data
);
506 if(domain_expand(x
, &r
, &num
, argv
[2], argv
[3]))
508 for(i
= 0; i
< num
; i
++)
509 printf("%s\n", r
[i
]);