Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / heimdal / lib / krb5 / transited.c
1 /*
2  * Copyright (c) 1997 - 2001 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,v 1.9 2002/09/09 14:03:03 nectar Exp $");
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 *tmp;
73         tmp = from;
74         from = to;
75         to = tmp;
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             tmp->next = path;
91             path = tmp;
92             path->realm = strdup(p);
93             if(path->realm == NULL){
94                 r->next = path; /* XXX */
95                 krb5_set_error_string (context, "malloc: out of memory");
96                 return ENOMEM;;
97             }
98         }
99     }else if(strncmp(from, to, strlen(to)) == 0){
100         p = from + strlen(from);
101         while(1){
102             while(p >= from && *p != '/') p--;
103             if(p == from)
104                 return KRB5KDC_ERR_POLICY;
105             if(strncmp(to, from, p - from) == 0)
106                 break;
107             tmp = calloc(1, sizeof(*tmp));
108             tmp->next = path;
109             path = tmp;
110             path->realm = malloc(p - from + 1);
111             if(path->realm == NULL){
112                 r->next = path; /* XXX */
113                 krb5_set_error_string (context, "malloc: out of memory");
114                 return ENOMEM;
115             }
116             memcpy(path->realm, from, p - from);
117             path->realm[p - from] = '\0';
118             p--;
119         }
120     } else {
121         krb5_clear_error_string (context);
122         return KRB5KDC_ERR_POLICY;
123     }
124     r->next = path;
125     
126     return 0;
127 }
128
129 static int
130 make_paths(krb5_context context,
131            struct tr_realm *realms, const char *client_realm, 
132            const char *server_realm)
133 {
134     struct tr_realm *r;
135     int ret;
136     const char *prev_realm = client_realm;
137     const char *next_realm = NULL;
138     for(r = realms; r; r = r->next){
139         /* it *might* be that you can have more than one empty
140            component in a row, at least that's how I interpret the
141            "," exception in 1510 */
142         if(r->realm[0] == '\0'){
143             while(r->next && r->next->realm[0] == '\0')
144                 r = r->next;
145             if(r->next)
146                 next_realm = r->next->realm;
147             else
148                 next_realm = server_realm;
149             ret = make_path(context, r, prev_realm, next_realm);
150             if(ret){
151                 free_realms(realms);
152                 return ret;
153             }
154         }
155         prev_realm = r->realm;
156     }
157     return 0;
158 }
159
160 static int
161 expand_realms(krb5_context context,
162               struct tr_realm *realms, const char *client_realm)
163 {
164     struct tr_realm *r;
165     const char *prev_realm = NULL;
166     for(r = realms; r; r = r->next){
167         if(r->trailing_dot){
168             char *tmp;
169             if(prev_realm == NULL)
170                 prev_realm = client_realm;
171             tmp = realloc(r->realm, strlen(r->realm) + strlen(prev_realm) + 1);
172             if(tmp == NULL){
173                 free_realms(realms);
174                 krb5_set_error_string (context, "malloc: out of memory");
175                 return ENOMEM;
176             }
177             r->realm = tmp;
178             strcat(r->realm, prev_realm);
179         }else if(r->leading_slash && !r->leading_space && prev_realm){
180             /* yet another exception: if you use x500-names, the
181                leading realm doesn't have to be "quoted" with a space */
182             char *tmp;
183             tmp = malloc(strlen(r->realm) + strlen(prev_realm) + 1);
184             if(tmp == NULL){
185                 free_realms(realms);
186                 krb5_set_error_string (context, "malloc: out of memory");
187                 return ENOMEM;
188             }
189             strcpy(tmp, prev_realm);
190             strcat(tmp, r->realm);
191             free(r->realm);
192             r->realm = tmp;
193         }
194         prev_realm = r->realm;
195     }
196     return 0;
197 }
198
199 static struct tr_realm *
200 make_realm(char *realm)
201 {
202     struct tr_realm *r;
203     char *p, *q;
204     int quote = 0;
205     r = calloc(1, sizeof(*r));
206     if(r == NULL){
207         free(realm);
208         return NULL;
209     }
210     r->realm = realm;
211     for(p = q = r->realm; *p; p++){
212         if(p == r->realm && *p == ' '){
213             r->leading_space = 1;
214             continue;
215         }
216         if(q == r->realm && *p == '/')
217             r->leading_slash = 1;
218         if(quote){
219             *q++ = *p;
220             quote = 0;
221             continue;
222         }
223         if(*p == '\\'){
224             quote = 1;
225             continue;
226         }
227         if(p[0] == '.' && p[1] == '\0')
228             r->trailing_dot = 1;
229         *q++ = *p;
230     }
231     *q = '\0';
232     return r;
233 }
234
235 static struct tr_realm*
236 append_realm(struct tr_realm *head, struct tr_realm *r)
237 {
238     struct tr_realm *p;
239     if(head == NULL){
240         r->next = NULL;
241         return r;
242     }
243     p = head;
244     while(p->next) p = p->next;
245     p->next = r;
246     return head;
247 }
248
249 static int
250 decode_realms(krb5_context context,
251               const char *tr, int length, struct tr_realm **realms)
252 {
253     struct tr_realm *r = NULL;
254
255     char *tmp;
256     int quote = 0;
257     const char *start = tr;
258     int i;
259
260     for(i = 0; i < length; i++){
261         if(quote){
262             quote = 0;
263             continue;
264         }
265         if(tr[i] == '\\'){
266             quote = 1;
267             continue;
268         }
269         if(tr[i] == ','){
270             tmp = malloc(tr + i - start + 1);
271             memcpy(tmp, start, tr + i - start);
272             tmp[tr + i - start] = '\0';
273             r = make_realm(tmp);
274             if(r == NULL){
275                 free_realms(*realms);
276                 krb5_set_error_string (context, "malloc: out of memory");
277                 return ENOMEM;
278             }
279             *realms = append_realm(*realms, r);
280             start = tr + i + 1;
281         }
282     }
283     tmp = malloc(tr + i - start + 1);
284     memcpy(tmp, start, tr + i - start);
285     tmp[tr + i - start] = '\0';
286     r = make_realm(tmp);
287     if(r == NULL){
288         free_realms(*realms);
289         krb5_set_error_string (context, "malloc: out of memory");
290         return ENOMEM;
291     }
292     *realms = append_realm(*realms, r);
293     
294     return 0;
295 }
296
297
298 krb5_error_code
299 krb5_domain_x500_decode(krb5_context context,
300                         krb5_data tr, char ***realms, int *num_realms, 
301                         const char *client_realm, const char *server_realm)
302 {
303     struct tr_realm *r = NULL;
304     struct tr_realm *p, **q;
305     int ret;
306     
307     /* split string in components */
308     ret = decode_realms(context, tr.data, tr.length, &r);
309     if(ret)
310         return ret;
311     
312     /* apply prefix rule */
313     ret = expand_realms(context, r, client_realm);
314     if(ret)
315         return ret;
316     
317     ret = make_paths(context, r, client_realm, server_realm);
318     if(ret)
319         return ret;
320     
321     /* remove empty components and count realms */
322     q = &r;
323     *num_realms = 0;
324     for(p = r; p; ){
325         if(p->realm[0] == '\0'){
326             free(p->realm);
327             *q = p->next;
328             free(p);
329             p = *q;
330         }else{
331             q = &p->next;
332             p = p->next;
333             (*num_realms)++;
334         }
335     }
336     if (*num_realms < 0 || *num_realms + 1 > UINT_MAX/sizeof(**realms))
337         return ERANGE;
338
339     {
340         char **R;
341         R = malloc((*num_realms + 1) * sizeof(*R));
342         if (R == NULL)
343             return ENOMEM;
344         *realms = R;
345         while(r){
346             *R++ = r->realm;
347             p = r->next;
348             free(r);
349             r = p;
350         }
351     }
352     return 0;
353 }
354
355 krb5_error_code
356 krb5_domain_x500_encode(char **realms, int num_realms, krb5_data *encoding)
357 {
358     char *s = NULL;
359     int len = 0;
360     int i;
361     for(i = 0; i < num_realms; i++){
362         len += strlen(realms[i]);
363         if(realms[i][0] == '/')
364             len++;
365     }
366     len += num_realms - 1;
367     s = malloc(len + 1);
368     *s = '\0';
369     for(i = 0; i < num_realms; i++){
370         if(i && i < num_realms - 1)
371             strcat(s, ",");
372         if(realms[i][0] == '/')
373             strcat(s, " ");
374         strcat(s, realms[i]);
375     }
376     encoding->data = s;
377     encoding->length = strlen(s);
378     return 0;
379 }
380
381 krb5_error_code
382 krb5_check_transited_realms(krb5_context context,
383                             const char *const *realms, 
384                             int num_realms, 
385                             int *bad_realm)
386 {
387     int i;
388     int ret = 0;
389     char **bad_realms = krb5_config_get_strings(context, NULL, 
390                                                 "libdefaults", 
391                                                 "transited_realms_reject", 
392                                                 NULL);
393     if(bad_realms == NULL)
394         return 0;
395
396     for(i = 0; i < num_realms; i++) {
397         char **p;
398         for(p = bad_realms; *p; p++)
399             if(strcmp(*p, realms[i]) == 0) {
400                 krb5_set_error_string (context, "no transit through realm %s",
401                                        *p);
402                 ret = KRB5KRB_AP_ERR_ILL_CR_TKT;
403                 if(bad_realm)
404                     *bad_realm = i;
405                 break;
406             }
407     }
408     krb5_config_free_strings(bad_realms);
409     return ret;
410 }
411
412 #if 0
413 int
414 main(int argc, char **argv)
415 {
416     krb5_data x;
417     char **r;
418     int num, i;
419     x.data = argv[1];
420     x.length = strlen(x.data);
421     if(domain_expand(x, &r, &num, argv[2], argv[3]))
422         exit(1);
423     for(i = 0; i < num; i++)
424         printf("%s\n", r[i]);
425     return 0;
426 }
427 #endif
428