remove gcc34
[dragonfly.git] / crypto / heimdal-0.6.3 / lib / krb5 / transited.c
blob8f48ff1d93fd227e7d2a8a6f3cf6c948a5d0c210
1 /*
2 * Copyright (c) 1997 - 2001, 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
36 RCSID("$Id: transited.c,v 1.10.2.3 2003/10/22 06:07:41 lha Exp $");
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 */
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;
51 static void
52 free_realms(struct tr_realm *r)
54 struct tr_realm *p;
55 while(r){
56 p = r;
57 r = r->next;
58 free(p->realm);
59 free(p);
63 static int
64 make_path(krb5_context context, struct tr_realm *r,
65 const char *from, const char *to)
67 const char *p;
68 struct tr_realm *path = r->next;
69 struct tr_realm *tmp;
71 if(strlen(from) < strlen(to)){
72 const char *tmp;
73 tmp = from;
74 from = to;
75 to = tmp;
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;
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;;
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;
116 memcpy(path->realm, from, p - from);
117 path->realm[p - from] = '\0';
118 p--;
120 } else {
121 krb5_clear_error_string (context);
122 return KRB5KDC_ERR_POLICY;
124 r->next = path;
126 return 0;
129 static int
130 make_paths(krb5_context context,
131 struct tr_realm *realms, const char *client_realm,
132 const char *server_realm)
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;
155 prev_realm = r->realm;
157 return 0;
160 static int
161 expand_realms(krb5_context context,
162 struct tr_realm *realms, const char *client_realm)
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 size_t len = strlen(r->realm) + strlen(prev_realm) + 1;
171 if(prev_realm == NULL)
172 prev_realm = client_realm;
173 tmp = realloc(r->realm, len);
174 if(tmp == NULL){
175 free_realms(realms);
176 krb5_set_error_string (context, "malloc: out of memory");
177 return ENOMEM;
179 r->realm = tmp;
180 strlcat(r->realm, prev_realm, len);
181 }else if(r->leading_slash && !r->leading_space && prev_realm){
182 /* yet another exception: if you use x500-names, the
183 leading realm doesn't have to be "quoted" with a space */
184 char *tmp;
185 size_t len = strlen(r->realm) + strlen(prev_realm) + 1;
187 tmp = malloc(len);
188 if(tmp == NULL){
189 free_realms(realms);
190 krb5_set_error_string (context, "malloc: out of memory");
191 return ENOMEM;
193 strlcpy(tmp, prev_realm, len);
194 strlcat(tmp, r->realm, len);
195 free(r->realm);
196 r->realm = tmp;
198 prev_realm = r->realm;
200 return 0;
203 static struct tr_realm *
204 make_realm(char *realm)
206 struct tr_realm *r;
207 char *p, *q;
208 int quote = 0;
209 r = calloc(1, sizeof(*r));
210 if(r == NULL){
211 free(realm);
212 return NULL;
214 r->realm = realm;
215 for(p = q = r->realm; *p; p++){
216 if(p == r->realm && *p == ' '){
217 r->leading_space = 1;
218 continue;
220 if(q == r->realm && *p == '/')
221 r->leading_slash = 1;
222 if(quote){
223 *q++ = *p;
224 quote = 0;
225 continue;
227 if(*p == '\\'){
228 quote = 1;
229 continue;
231 if(p[0] == '.' && p[1] == '\0')
232 r->trailing_dot = 1;
233 *q++ = *p;
235 *q = '\0';
236 return r;
239 static struct tr_realm*
240 append_realm(struct tr_realm *head, struct tr_realm *r)
242 struct tr_realm *p;
243 if(head == NULL){
244 r->next = NULL;
245 return r;
247 p = head;
248 while(p->next) p = p->next;
249 p->next = r;
250 return head;
253 static int
254 decode_realms(krb5_context context,
255 const char *tr, int length, struct tr_realm **realms)
257 struct tr_realm *r = NULL;
259 char *tmp;
260 int quote = 0;
261 const char *start = tr;
262 int i;
264 for(i = 0; i < length; i++){
265 if(quote){
266 quote = 0;
267 continue;
269 if(tr[i] == '\\'){
270 quote = 1;
271 continue;
273 if(tr[i] == ','){
274 tmp = malloc(tr + i - start + 1);
275 memcpy(tmp, start, tr + i - start);
276 tmp[tr + i - start] = '\0';
277 r = make_realm(tmp);
278 if(r == NULL){
279 free_realms(*realms);
280 krb5_set_error_string (context, "malloc: out of memory");
281 return ENOMEM;
283 *realms = append_realm(*realms, r);
284 start = tr + i + 1;
287 tmp = malloc(tr + i - start + 1);
288 memcpy(tmp, start, tr + i - start);
289 tmp[tr + i - start] = '\0';
290 r = make_realm(tmp);
291 if(r == NULL){
292 free_realms(*realms);
293 krb5_set_error_string (context, "malloc: out of memory");
294 return ENOMEM;
296 *realms = append_realm(*realms, r);
298 return 0;
302 krb5_error_code
303 krb5_domain_x500_decode(krb5_context context,
304 krb5_data tr, char ***realms, int *num_realms,
305 const char *client_realm, const char *server_realm)
307 struct tr_realm *r = NULL;
308 struct tr_realm *p, **q;
309 int ret;
311 if(tr.length == 0) {
312 *realms = NULL;
313 *num_realms = 0;
314 return 0;
317 /* split string in components */
318 ret = decode_realms(context, tr.data, tr.length, &r);
319 if(ret)
320 return ret;
322 /* apply prefix rule */
323 ret = expand_realms(context, r, client_realm);
324 if(ret)
325 return ret;
327 ret = make_paths(context, r, client_realm, server_realm);
328 if(ret)
329 return ret;
331 /* remove empty components and count realms */
332 q = &r;
333 *num_realms = 0;
334 for(p = r; p; ){
335 if(p->realm[0] == '\0'){
336 free(p->realm);
337 *q = p->next;
338 free(p);
339 p = *q;
340 }else{
341 q = &p->next;
342 p = p->next;
343 (*num_realms)++;
346 if (*num_realms < 0 || *num_realms + 1 > UINT_MAX/sizeof(**realms))
347 return ERANGE;
350 char **R;
351 R = malloc((*num_realms + 1) * sizeof(*R));
352 if (R == NULL)
353 return ENOMEM;
354 *realms = R;
355 while(r){
356 *R++ = r->realm;
357 p = r->next;
358 free(r);
359 r = p;
362 return 0;
365 krb5_error_code
366 krb5_domain_x500_encode(char **realms, int num_realms, krb5_data *encoding)
368 char *s = NULL;
369 int len = 0;
370 int i;
371 krb5_data_zero(encoding);
372 if (num_realms == 0)
373 return 0;
374 for(i = 0; i < num_realms; i++){
375 len += strlen(realms[i]);
376 if(realms[i][0] == '/')
377 len++;
379 len += num_realms - 1;
380 s = malloc(len + 1);
381 if (s == NULL)
382 return ENOMEM;
383 *s = '\0';
384 for(i = 0; i < num_realms; i++){
385 if(i && i < num_realms - 1)
386 strlcat(s, ",", len + 1);
387 if(realms[i][0] == '/')
388 strlcat(s, " ", len + 1);
389 strlcat(s, realms[i], len + 1);
391 encoding->data = s;
392 encoding->length = strlen(s);
393 return 0;
396 krb5_error_code
397 krb5_check_transited(krb5_context context,
398 krb5_const_realm client_realm,
399 krb5_const_realm server_realm,
400 krb5_realm *realms,
401 int num_realms,
402 int *bad_realm)
404 char **tr_realms;
405 char **p;
406 int i;
408 if(num_realms == 0)
409 return 0;
411 tr_realms = krb5_config_get_strings(context, NULL,
412 "capaths",
413 client_realm,
414 server_realm,
415 NULL);
416 for(i = 0; i < num_realms; i++) {
417 for(p = tr_realms; p && *p; p++) {
418 if(strcmp(*p, realms[i]) == 0)
419 break;
421 if(p == NULL || *p == NULL) {
422 krb5_config_free_strings(tr_realms);
423 krb5_set_error_string (context, "no transit through realm %s",
424 realms[i]);
425 if(bad_realm)
426 *bad_realm = i;
427 return KRB5KRB_AP_ERR_ILL_CR_TKT;
430 krb5_config_free_strings(tr_realms);
431 return 0;
434 krb5_error_code
435 krb5_check_transited_realms(krb5_context context,
436 const char *const *realms,
437 int num_realms,
438 int *bad_realm)
440 int i;
441 int ret = 0;
442 char **bad_realms = krb5_config_get_strings(context, NULL,
443 "libdefaults",
444 "transited_realms_reject",
445 NULL);
446 if(bad_realms == NULL)
447 return 0;
449 for(i = 0; i < num_realms; i++) {
450 char **p;
451 for(p = bad_realms; *p; p++)
452 if(strcmp(*p, realms[i]) == 0) {
453 krb5_set_error_string (context, "no transit through realm %s",
454 *p);
455 ret = KRB5KRB_AP_ERR_ILL_CR_TKT;
456 if(bad_realm)
457 *bad_realm = i;
458 break;
461 krb5_config_free_strings(bad_realms);
462 return ret;
465 #if 0
467 main(int argc, char **argv)
469 krb5_data x;
470 char **r;
471 int num, i;
472 x.data = argv[1];
473 x.length = strlen(x.data);
474 if(domain_expand(x, &r, &num, argv[2], argv[3]))
475 exit(1);
476 for(i = 0; i < num; i++)
477 printf("%s\n", r[i]);
478 return 0;
480 #endif