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 RCSID("$Id: transited.c 21745 2007-07-31 16:11:25Z lha $");
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
45 unsigned leading_space:1;
46 unsigned leading_slash:1;
47 unsigned trailing_dot:1;
48 struct tr_realm *next;
52 free_realms(struct tr_realm *r)
64 make_path(krb5_context context, struct tr_realm *r,
65 const char *from, const char *to)
68 struct tr_realm *path = r->next;
71 if(strlen(from) < strlen(to)){
78 if(strcmp(from + strlen(from) - strlen(to), to) == 0){
83 krb5_clear_error_string (context);
84 return KRB5KDC_ERR_POLICY;
87 if(strcmp(p, to) == 0)
89 tmp = calloc(1, sizeof(*tmp));
91 krb5_set_error_string (context, "malloc: out of memory");
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");
103 }else if(strncmp(from, to, strlen(to)) == 0){
104 p = from + strlen(from);
106 while(p >= from && *p != '/') p--;
108 r->next = path; /* XXX */
109 return KRB5KDC_ERR_POLICY;
111 if(strncmp(to, from, p - from) == 0)
113 tmp = calloc(1, sizeof(*tmp));
115 krb5_set_error_string (context, "malloc: out of memory");
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");
126 memcpy(path->realm, from, p - from);
127 path->realm[p - from] = '\0';
131 krb5_clear_error_string (context);
132 return KRB5KDC_ERR_POLICY;
140 make_paths(krb5_context context,
141 struct tr_realm *realms, const char *client_realm,
142 const char *server_realm)
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')
156 next_realm = r->next->realm;
158 next_realm = server_realm;
159 ret = make_path(context, r, prev_realm, next_realm);
165 prev_realm = r->realm;
171 expand_realms(krb5_context context,
172 struct tr_realm *realms, const char *client_realm)
175 const char *prev_realm = NULL;
176 for(r = realms; r; r = r->next){
181 if(prev_realm == NULL)
182 prev_realm = client_realm;
184 len = strlen(r->realm) + strlen(prev_realm) + 1;
186 tmp = realloc(r->realm, len);
189 krb5_set_error_string (context, "malloc: out of memory");
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 */
198 size_t len = strlen(r->realm) + strlen(prev_realm) + 1;
203 krb5_set_error_string (context, "malloc: out of memory");
206 strlcpy(tmp, prev_realm, len);
207 strlcat(tmp, r->realm, len);
211 prev_realm = r->realm;
216 static struct tr_realm *
217 make_realm(char *realm)
222 r = calloc(1, sizeof(*r));
228 for(p = q = r->realm; *p; p++){
229 if(p == r->realm && *p == ' '){
230 r->leading_space = 1;
233 if(q == r->realm && *p == '/')
234 r->leading_slash = 1;
244 if(p[0] == '.' && p[1] == '\0')
252 static struct tr_realm*
253 append_realm(struct tr_realm *head, struct tr_realm *r)
261 while(p->next) p = p->next;
267 decode_realms(krb5_context context,
268 const char *tr, int length, struct tr_realm **realms)
270 struct tr_realm *r = NULL;
274 const char *start = tr;
277 for(i = 0; i < length; i++){
287 tmp = malloc(tr + i - start + 1);
289 krb5_set_error_string (context, "malloc: out of memory");
292 memcpy(tmp, start, tr + i - start);
293 tmp[tr + i - start] = '\0';
296 free_realms(*realms);
297 krb5_set_error_string (context, "malloc: out of memory");
300 *realms = append_realm(*realms, r);
304 tmp = malloc(tr + i - start + 1);
307 krb5_set_error_string (context, "malloc: out of memory");
310 memcpy(tmp, start, tr + i - start);
311 tmp[tr + i - start] = '\0';
314 free_realms(*realms);
315 krb5_set_error_string (context, "malloc: out of memory");
318 *realms = append_realm(*realms, r);
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)
329 struct tr_realm *r = NULL;
330 struct tr_realm *p, **q;
339 /* split string in components */
340 ret = decode_realms(context, tr.data, tr.length, &r);
344 /* apply prefix rule */
345 ret = expand_realms(context, r, client_realm);
349 ret = make_paths(context, r, client_realm, server_realm);
353 /* remove empty components and count realms */
357 if(p->realm[0] == '\0'){
368 if (*num_realms < 0 || *num_realms + 1 > UINT_MAX/sizeof(**realms))
373 R = malloc((*num_realms + 1) * sizeof(*R));
387 krb5_error_code KRB5_LIB_FUNCTION
388 krb5_domain_x500_encode(char **realms, int num_realms, krb5_data *encoding)
393 krb5_data_zero(encoding);
396 for(i = 0; i < num_realms; i++){
397 len += strlen(realms[i]);
398 if(realms[i][0] == '/')
401 len += num_realms - 1;
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);
414 encoding->length = strlen(s);
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,
433 tr_realms = krb5_config_get_strings(context, NULL,
438 for(i = 0; i < num_realms; i++) {
439 for(p = tr_realms; p && *p; p++) {
440 if(strcmp(*p, realms[i]) == 0)
443 if(p == NULL || *p == NULL) {
444 krb5_config_free_strings(tr_realms);
445 krb5_set_error_string (context, "no transit through realm %s",
449 return KRB5KRB_AP_ERR_ILL_CR_TKT;
452 krb5_config_free_strings(tr_realms);
456 krb5_error_code KRB5_LIB_FUNCTION
457 krb5_check_transited_realms(krb5_context context,
458 const char *const *realms,
464 char **bad_realms = krb5_config_get_strings(context, NULL,
466 "transited_realms_reject",
468 if(bad_realms == NULL)
471 for(i = 0; i < num_realms; i++) {
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",
477 ret = KRB5KRB_AP_ERR_ILL_CR_TKT;
483 krb5_config_free_strings(bad_realms);
489 main(int argc, char **argv)
495 x.length = strlen(x.data);
496 if(domain_expand(x, &r, &num, argv[2], argv[3]))
498 for(i = 0; i < num; i++)
499 printf("%s\n", r[i]);