Debian Codegraph @

lib/krb5/principal.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Copyright (c) 1997-2007 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 /**
35 * @page krb5_principal_intro The principal handing functions.
36 *
37 * A Kerberos principal is a email address looking string that
38 * contains two parts separated by @. The second part is the kerberos
39 * realm the principal belongs to and the first is a list of 0 or
40 * more components. For example
41 * @verbatim
42 [email protected]
43 host/[email protected]
44 host/[email protected]
45 @endverbatim
46 *
47 * See the library functions here: @ref krb5_principal
48 */
49
50 #include "krb5_locl.h"
51 #ifdef HAVE_RES_SEARCH
52 #define USE_RESOLVER
53 #endif
54 #ifdef HAVE_ARPA_NAMESER_H
55 #include <arpa/nameser.h>
56 #endif
57 #include <fnmatch.h>
58 #include "resolve.h"
59
60 #define princ_num_comp(P) ((P)->name.name_string.len)
61 #define princ_type(P) ((P)->name.name_type)
62 #define princ_comp(P) ((P)->name.name_string.val)
63 #define princ_ncomp(P, N) ((P)->name.name_string.val[(N)])
64 #define princ_realm(P) ((P)->realm)
65
66 static krb5_error_code
67 set_default_princ_type(krb5_principal p, NAME_TYPE defnt)
68 {
69 if (princ_num_comp(p) > 1 && strcmp(princ_ncomp(p, 0), KRB5_TGS_NAME) == 0)
70 princ_type(p) = KRB5_NT_SRV_INST;
71 else if (princ_num_comp(p) > 1 && strcmp(princ_ncomp(p, 0), "host") == 0)
72 princ_type(p) = KRB5_NT_SRV_HST;
73 else if (princ_num_comp(p) > 1 && strcmp(princ_ncomp(p, 0), "kca_service") == 0)
74 princ_type(p) = KRB5_NT_SRV_HST;
75 else if (princ_num_comp(p) == 2 &&
76 strcmp(princ_ncomp(p, 0), KRB5_WELLKNOWN_NAME) == 0)
77 princ_type(p) = KRB5_NT_WELLKNOWN;
78 else if (princ_num_comp(p) == 1 && strchr(princ_ncomp(p, 0), '@') != NULL)
79 princ_type(p) = KRB5_NT_SMTP_NAME;
80 else
81 princ_type(p) = defnt;
82 return 0;
83 }
84
85 static krb5_error_code append_component(krb5_context, krb5_principal,
86 const char *, size_t);
87
88 /**
89 * Frees a Kerberos principal allocated by the library with
90 * krb5_parse_name(), krb5_make_principal() or any other related
91 * principal functions.
92 *
93 * @param context A Kerberos context.
94 * @param p a principal to free.
95 *
96 * @return An krb5 error code, see krb5_get_error_message().
97 *
98 * @ingroup krb5_principal
99 */
100
101 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
102 krb5_free_principal(krb5_context context,
103 krb5_principal p)
104 {
105 if(p){
106 free_Principal(p);
107 free(p);
108 }
109 }
110
111 /**
112 * Set the type of the principal
113 *
114 * @param context A Kerberos context.
115 * @param principal principal to set the type for
116 * @param type the new type
117 *
118 * @return An krb5 error code, see krb5_get_error_message().
119 *
120 * @ingroup krb5_principal
121 */
122
123 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
124 krb5_principal_set_type(krb5_context context,
125 krb5_principal principal,
126 int type)
127 {
128 princ_type(principal) = type;
129 }
130
131 /**
132 * Get the type of the principal
133 *
134 * @param context A Kerberos context.
135 * @param principal principal to get the type for
136 *
137 * @return the type of principal
138 *
139 * @ingroup krb5_principal
140 */
141
142 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
143 krb5_principal_get_type(krb5_context context,
144 krb5_const_principal principal)
145 {
146 return princ_type(principal);
147 }
148
149 /**
150 * Get the realm of the principal
151 *
152 * @param context A Kerberos context.
153 * @param principal principal to get the realm for
154 *
155 * @return realm of the principal, don't free or use after krb5_principal is freed
156 *
157 * @ingroup krb5_principal
158 */
159
160 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
161 krb5_principal_get_realm(krb5_context context,
162 krb5_const_principal principal)
163 {
164 return princ_realm(principal);
165 }
166
167 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
168 krb5_principal_get_comp_string(krb5_context context,
169 krb5_const_principal principal,
170 unsigned int component)
171 {
172 if(component >= princ_num_comp(principal))
173 return NULL;
174 return princ_ncomp(principal, component);
175 }
176
177 /**
178 * Get number of component is principal.
179 *
180 * @param context Kerberos 5 context
181 * @param principal principal to query
182 *
183 * @return number of components in string
184 *
185 * @ingroup krb5_principal
186 */
187
188 KRB5_LIB_FUNCTION unsigned int KRB5_LIB_CALL
189 krb5_principal_get_num_comp(krb5_context context,
190 krb5_const_principal principal)
191 {
192 return princ_num_comp(principal);
193 }
194
195 /**
196 * Parse a name into a krb5_principal structure, flags controls the behavior.
197 *
198 * @param context Kerberos 5 context
199 * @param name name to parse into a Kerberos principal
200 * @param flags flags to control the behavior
201 * @param principal returned principal, free with krb5_free_principal().
202 *
203 * @return An krb5 error code, see krb5_get_error_message().
204 *
205 * @ingroup krb5_principal
206 */
207
208 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
209 krb5_parse_name_flags(krb5_context context,
210 const char *name,
211 int flags,
212 krb5_principal *principal)
213 {
214 krb5_error_code ret;
215 heim_general_string *comp;
216 heim_general_string realm = NULL;
217 int ncomp;
218
219 const char *p;
220 char *q;
221 char *s;
222 char *start;
223
224 int n;
225 char c;
226 int got_realm = 0;
227 int first_at = 1;
228 int no_realm = flags & KRB5_PRINCIPAL_PARSE_NO_REALM;
229 int require_realm = flags & KRB5_PRINCIPAL_PARSE_REQUIRE_REALM;
230 int enterprise = flags & KRB5_PRINCIPAL_PARSE_ENTERPRISE;
231 int ignore_realm = flags & KRB5_PRINCIPAL_PARSE_IGNORE_REALM;
232 int no_def_realm = flags & KRB5_PRINCIPAL_PARSE_NO_DEF_REALM;
233
234 *principal = NULL;
235
236 if (no_realm && require_realm) {
237 krb5_set_error_message(context, KRB5_ERR_NO_SERVICE,
238 N_("Can't require both realm and "
239 "no realm at the same time", ""));
240 return KRB5_ERR_NO_SERVICE;
241 }
242
243 /* count number of component,
244 * enterprise names only have one component
245 */
246 ncomp = 1;
247 if (!enterprise) {
248 for (p = name; *p; p++) {
249 if (*p=='\\') {
250 if (!p[1]) {
251 krb5_set_error_message(context, KRB5_PARSE_MALFORMED,
252 N_("trailing \\ in principal name", ""));
253 return KRB5_PARSE_MALFORMED;
254 }
255 p++;
256 } else if (*p == '/')
257 ncomp++;
258 else if (*p == '@')
259 break;
260 }
261 }
262 comp = calloc(ncomp, sizeof(*comp));
263 if (comp == NULL)
264 return krb5_enomem(context);
265
266 n = 0;
267 p = start = q = s = strdup(name);
268 if (start == NULL) {
269 free(comp);
270 return krb5_enomem(context);
271 }
272 while (*p) {
273 c = *p++;
274 if (c == '\\') {
275 c = *p++;
276 if (c == 'n')
277 c = '\n';
278 else if (c == 't')
279 c = '\t';
280 else if (c == 'b')
281 c = '\b';
282 else if (c == '0')
283 c = '\0';
284 else if (c == '\0') {
285 ret = KRB5_PARSE_MALFORMED;
286 krb5_set_error_message(context, ret,
287 N_("trailing \\ in principal name", ""));
288 goto exit;
289 }
290 } else if (enterprise && first_at) {
291 if (c == '@')
292 first_at = 0;
293 } else if ((c == '/' && !enterprise) || c == '@') {
294 if (got_realm) {
295 ret = KRB5_PARSE_MALFORMED;
296 krb5_set_error_message(context, ret,
297 N_("part after realm in principal name", ""));
298 goto exit;
299 } else {
300 comp[n] = malloc(q - start + 1);
301 if (comp[n] == NULL) {
302 ret = krb5_enomem(context);
303 goto exit;
304 }
305 memcpy(comp[n], start, q - start);
306 comp[n][q - start] = 0;
307 n++;
308 }
309 if (c == '@')
310 got_realm = 1;
311 start = q;
312 continue;
313 }
314 if (got_realm && (c == '/' || c == '\0')) {
315 ret = KRB5_PARSE_MALFORMED;
316 krb5_set_error_message(context, ret,
317 N_("part after realm in principal name", ""));
318 goto exit;
319 }
320 *q++ = c;
321 }
322 if (got_realm) {
323 if (no_realm) {
324 ret = KRB5_PARSE_MALFORMED;
325 krb5_set_error_message(context, ret,
326 N_("realm found in 'short' principal "
327 "expected to be without one", ""));
328 goto exit;
329 }
330 if (!ignore_realm) {
331 realm = malloc(q - start + 1);
332 if (realm == NULL) {
333 ret = krb5_enomem(context);
334 goto exit;
335 }
336 memcpy(realm, start, q - start);
337 realm[q - start] = 0;
338 }
339 } else {
340 if (require_realm) {
341 ret = KRB5_PARSE_MALFORMED;
342 krb5_set_error_message(context, ret,
343 N_("realm NOT found in principal "
344 "expected to be with one", ""));
345 goto exit;
346 } else if (no_realm || no_def_realm) {
347 realm = NULL;
348 } else {
349 ret = krb5_get_default_realm(context, &realm);
350 if (ret)
351 goto exit;
352 }
353
354 comp[n] = malloc(q - start + 1);
355 if (comp[n] == NULL) {
356 ret = krb5_enomem(context);
357 goto exit;
358 }
359 memcpy(comp[n], start, q - start);
360 comp[n][q - start] = 0;
361 n++;
362 }
363 *principal = calloc(1, sizeof(**principal));
364 if (*principal == NULL) {
365 ret = krb5_enomem(context);
366 goto exit;
367 }
368 (*principal)->name.name_string.val = comp;
369 princ_num_comp(*principal) = n;
370 (*principal)->realm = realm;
371 if (enterprise)
372 princ_type(*principal) = KRB5_NT_ENTERPRISE_PRINCIPAL;
373 else
374 set_default_princ_type(*principal, KRB5_NT_PRINCIPAL);
375 free(s);
376 return 0;
377 exit:
378 while (n>0) {
379 free(comp[--n]);
380 }
381 free(comp);
382 krb5_free_default_realm(context, realm);
383 free(s);
384 return ret;
385 }
386
387 /**
388 * Parse a name into a krb5_principal structure
389 *
390 * @param context Kerberos 5 context
391 * @param name name to parse into a Kerberos principal
392 * @param principal returned principal, free with krb5_free_principal().
393 *
394 * @return An krb5 error code, see krb5_get_error_message().
395 *
396 * @ingroup krb5_principal
397 */
398
399 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
400 krb5_parse_name(krb5_context context,
401 const char *name,
402 krb5_principal *principal)
403 {
404 return krb5_parse_name_flags(context, name, 0, principal);
405 }
406
407 static const char quotable_chars[] = " \n\t\b\\/@";
408 static const char replace_chars[] = " ntb\\/@";
409
410 #define add_char(BASE, INDEX, LEN, C) do { if((INDEX) < (LEN)) (BASE)[(INDEX)++] = (C); }while(0);
411
412 static size_t
413 quote_string(const char *s, char *out, size_t idx, size_t len, int display)
414 {
415 const char *p, *q;
416 for(p = s; *p && idx < len; p++){
417 q = strchr(quotable_chars, *p);
418 if (q && display) {
419 add_char(out, idx, len, replace_chars[q - quotable_chars]);
420 } else if (q) {
421 add_char(out, idx, len, '\\');
422 add_char(out, idx, len, replace_chars[q - quotable_chars]);
423 }else
424 add_char(out, idx, len, *p);
425 }
426 if(idx < len)
427 out[idx] = '\0';
428 return idx;
429 }
430
431
432 static krb5_error_code
433 unparse_name_fixed(krb5_context context,
434 krb5_const_principal principal,
435 char *name,
436 size_t len,
437 int flags)
438 {
439 size_t idx = 0;
440 size_t i;
441 int short_form = (flags & KRB5_PRINCIPAL_UNPARSE_SHORT) != 0;
442 int no_realm = (flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) != 0;
443 int display = (flags & KRB5_PRINCIPAL_UNPARSE_DISPLAY) != 0;
444
445 if (!no_realm && princ_realm(principal) == NULL) {
446 krb5_set_error_message(context, ERANGE,
447 N_("Realm missing from principal, "
448 "can't unparse", ""));
449 return ERANGE;
450 }
451
452 for(i = 0; i < princ_num_comp(principal); i++){
453 if(i)
454 add_char(name, idx, len, '/');
455 idx = quote_string(princ_ncomp(principal, i), name, idx, len, display);
456 if(idx == len) {
457 krb5_set_error_message(context, ERANGE,
458 N_("Out of space printing principal", ""));
459 return ERANGE;
460 }
461 }
462 /* add realm if different from default realm */
463 if(short_form && !no_realm) {
464 krb5_realm r;
465 krb5_error_code ret;
466 ret = krb5_get_default_realm(context, &r);
467 if(ret)
468 return ret;
469 if(strcmp(princ_realm(principal), r) != 0)
470 short_form = 0;
471 krb5_free_default_realm(context, r);
472 }
473 if(!short_form && !no_realm) {
474 add_char(name, idx, len, '@');
475 idx = quote_string(princ_realm(principal), name, idx, len, display);
476 if(idx == len) {
477 krb5_set_error_message(context, ERANGE,
478 N_("Out of space printing "
479 "realm of principal", ""));
480 return ERANGE;
481 }
482 }
483 return 0;
484 }
485
486 /**
487 * Unparse the principal name to a fixed buffer
488 *
489 * @param context A Kerberos context.
490 * @param principal principal to unparse
491 * @param name buffer to write name to
492 * @param len length of buffer
493 *
494 * @return An krb5 error code, see krb5_get_error_message().
495 *
496 * @ingroup krb5_principal
497 */
498
499 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
500 krb5_unparse_name_fixed(krb5_context context,
501 krb5_const_principal principal,
502 char *name,
503 size_t len)
504 {
505 return unparse_name_fixed(context, principal, name, len, 0);
506 }
507
508 /**
509 * Unparse the principal name to a fixed buffer. The realm is skipped
510 * if its a default realm.
511 *
512 * @param context A Kerberos context.
513 * @param principal principal to unparse
514 * @param name buffer to write name to
515 * @param len length of buffer
516 *
517 * @return An krb5 error code, see krb5_get_error_message().
518 *
519 * @ingroup krb5_principal
520 */
521
522 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
523 krb5_unparse_name_fixed_short(krb5_context context,
524 krb5_const_principal principal,
525 char *name,
526 size_t len)
527 {
528 return unparse_name_fixed(context, principal, name, len,
529 KRB5_PRINCIPAL_UNPARSE_SHORT);
530 }
531
532 /**
533 * Unparse the principal name with unparse flags to a fixed buffer.
534 *
535 * @param context A Kerberos context.
536 * @param principal principal to unparse
537 * @param flags unparse flags
538 * @param name buffer to write name to
539 * @param len length of buffer
540 *
541 * @return An krb5 error code, see krb5_get_error_message().
542 *
543 * @ingroup krb5_principal
544 */
545
546 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
547 krb5_unparse_name_fixed_flags(krb5_context context,
548 krb5_const_principal principal,
549 int flags,
550 char *name,
551 size_t len)
552 {
553 return unparse_name_fixed(context, principal, name, len, flags);
554 }
555
556 static krb5_error_code
557 unparse_name(krb5_context context,
558 krb5_const_principal principal,
559 char **name,
560 int flags)
561 {
562 size_t len = 0, plen;
563 size_t i;
564 krb5_error_code ret;
565 /* count length */
566 if (princ_realm(principal)) {
567 plen = strlen(princ_realm(principal));
568
569 if(strcspn(princ_realm(principal), quotable_chars) == plen)
570 len += plen;
571 else
572 len += 2*plen;
573 len++; /* '@' */
574 }
575 for(i = 0; i < princ_num_comp(principal); i++){
576 plen = strlen(princ_ncomp(principal, i));
577 if(strcspn(princ_ncomp(principal, i), quotable_chars) == plen)
578 len += plen;
579 else
580 len += 2*plen;
581 len++;
582 }
583 len++; /* '\0' */
584 *name = malloc(len);
585 if(*name == NULL)
586 return krb5_enomem(context);
587 ret = unparse_name_fixed(context, principal, *name, len, flags);
588 if(ret) {
589 free(*name);
590 *name = NULL;
591 }
592 return ret;
593 }
594
595 /**
596 * Unparse the Kerberos name into a string
597 *
598 * @param context Kerberos 5 context
599 * @param principal principal to query
600 * @param name resulting string, free with krb5_xfree()
601 *
602 * @return An krb5 error code, see krb5_get_error_message().
603 *
604 * @ingroup krb5_principal
605 */
606
607 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
608 krb5_unparse_name(krb5_context context,
609 krb5_const_principal principal,
610 char **name)
611 {
612 return unparse_name(context, principal, name, 0);
613 }
614
615 /**
616 * Unparse the Kerberos name into a string
617 *
618 * @param context Kerberos 5 context
619 * @param principal principal to query
620 * @param flags flag to determine the behavior
621 * @param name resulting string, free with krb5_xfree()
622 *
623 * @return An krb5 error code, see krb5_get_error_message().
624 *
625 * @ingroup krb5_principal
626 */
627
628 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
629 krb5_unparse_name_flags(krb5_context context,
630 krb5_const_principal principal,
631 int flags,
632 char **name)
633 {
634 return unparse_name(context, principal, name, flags);
635 }
636
637 /**
638 * Unparse the principal name to a allocated buffer. The realm is
639 * skipped if its a default realm.
640 *
641 * @param context A Kerberos context.
642 * @param principal principal to unparse
643 * @param name returned buffer, free with krb5_xfree()
644 *
645 * @return An krb5 error code, see krb5_get_error_message().
646 *
647 * @ingroup krb5_principal
648 */
649
650 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
651 krb5_unparse_name_short(krb5_context context,
652 krb5_const_principal principal,
653 char **name)
654 {
655 return unparse_name(context, principal, name, KRB5_PRINCIPAL_UNPARSE_SHORT);
656 }
657
658 /**
659 * Set a new realm for a principal, and as a side-effect free the
660 * previous realm.
661 *
662 * @param context A Kerberos context.
663 * @param principal principal set the realm for
664 * @param realm the new realm to set
665 *
666 * @return An krb5 error code, see krb5_get_error_message().
667 *
668 * @ingroup krb5_principal
669 */
670
671 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
672 krb5_principal_set_realm(krb5_context context,
673 krb5_principal principal,
674 krb5_const_realm realm)
675 {
676 if (princ_realm(principal))
677 free(princ_realm(principal));
678
679 if (realm == NULL)
680 princ_realm(principal) = NULL;
681 else if ((princ_realm(principal) = strdup(realm)) == NULL)
682 return krb5_enomem(context);
683 return 0;
684 }
685
686 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
687 krb5_principal_set_comp_string(krb5_context context,
688 krb5_principal principal,
689 unsigned int k,
690 const char *component)
691 {
692 char *s;
693 size_t i;
694
695 for (i = princ_num_comp(principal); i <= k; i++)
696 append_component(context, principal, "", 0);
697 s = strdup(component);
698 if (s == NULL)
699 return krb5_enomem(context);
700 free(princ_ncomp(principal, k));
701 princ_ncomp(principal, k) = s;
702 return 0;
703 }
704
705 #ifndef HEIMDAL_SMALLER
706 /**
707 * Build a principal using vararg style building
708 *
709 * @param context A Kerberos context.
710 * @param principal returned principal
711 * @param rlen length of realm
712 * @param realm realm name
713 * @param ... a list of components ended with NULL.
714 *
715 * @return An krb5 error code, see krb5_get_error_message().
716 *
717 * @ingroup krb5_principal
718 */
719
720 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
721 krb5_build_principal(krb5_context context,
722 krb5_principal *principal,
723 int rlen,
724 krb5_const_realm realm,
725 ...)
726 {
727 krb5_error_code ret;
728 va_list ap;
729 va_start(ap, realm);
730 ret = krb5_build_principal_va(context, principal, rlen, realm, ap);
731 va_end(ap);
732 return ret;
733 }
734 #endif
735
736 /**
737 * Build a principal using vararg style building
738 *
739 * @param context A Kerberos context.
740 * @param principal returned principal
741 * @param realm realm name
742 * @param ... a list of components ended with NULL.
743 *
744 * @return An krb5 error code, see krb5_get_error_message().
745 *
746 * @ingroup krb5_principal
747 */
748
749 /* coverity[+alloc : arg-*1] */
750 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
751 krb5_make_principal(krb5_context context,
752 krb5_principal *principal,
753 krb5_const_realm realm,
754 ...)
755 {
756 krb5_error_code ret;
757 krb5_realm r = NULL;
758 va_list ap;
759 if(realm == NULL) {
760 ret = krb5_get_default_realm(context, &r);
761 if(ret)
762 return ret;
763 realm = r;
764 }
765 va_start(ap, realm);
766 ret = krb5_build_principal_va(context, principal, strlen(realm), realm, ap);
767 va_end(ap);
768 if(r)
769 krb5_free_default_realm(context, r);
770 return ret;
771 }
772
773 static krb5_error_code
774 append_component(krb5_context context, krb5_principal p,
775 const char *comp,
776 size_t comp_len)
777 {
778 heim_general_string *tmp;
779 size_t len = princ_num_comp(p);
780
781 tmp = realloc(princ_comp(p), (len + 1) * sizeof(*tmp));
782 if(tmp == NULL)
783 return krb5_enomem(context);
784 princ_comp(p) = tmp;
785 princ_ncomp(p, len) = malloc(comp_len + 1);
786 if (princ_ncomp(p, len) == NULL)
787 return krb5_enomem(context);
788 memcpy (princ_ncomp(p, len), comp, comp_len);
789 princ_ncomp(p, len)[comp_len] = '\0';
790 princ_num_comp(p)++;
791 return 0;
792 }
793
794 static krb5_error_code
795 va_ext_princ(krb5_context context, krb5_principal p, va_list ap)
796 {
797 krb5_error_code ret = 0;
798
799 while (1){
800 const char *s;
801 int len;
802
803 if ((len = va_arg(ap, int)) == 0)
804 break;
805 s = va_arg(ap, const char*);
806 if ((ret = append_component(context, p, s, len)) != 0)
807 break;
808 }
809 return ret;
810 }
811
812 static krb5_error_code
813 va_princ(krb5_context context, krb5_principal p, va_list ap)
814 {
815 krb5_error_code ret = 0;
816
817 while (1){
818 const char *s;
819
820 if ((s = va_arg(ap, const char*)) == NULL)
821 break;
822 if ((ret = append_component(context, p, s, strlen(s))) != 0)
823 break;
824 }
825 return ret;
826 }
827
828 static krb5_error_code
829 build_principal(krb5_context context,
830 krb5_principal *principal,
831 int rlen,
832 krb5_const_realm realm,
833 krb5_error_code (*func)(krb5_context, krb5_principal, va_list),
834 va_list ap)
835 {
836 krb5_error_code ret;
837 krb5_principal p;
838
839 *principal = NULL;
840 p = calloc(1, sizeof(*p));
841 if (p == NULL)
842 return krb5_enomem(context);
843
844 princ_realm(p) = strdup(realm);
845 if (p->realm == NULL) {
846 free(p);
847 return krb5_enomem(context);
848 }
849
850 ret = func(context, p, ap);
851 if (ret == 0) {
852 *principal = p;
853 set_default_princ_type(p, KRB5_NT_PRINCIPAL);
854 } else
855 krb5_free_principal(context, p);
856 return ret;
857 }
858
859 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
860 krb5_build_principal_va(krb5_context context,
861 krb5_principal *principal,
862 int rlen,
863 krb5_const_realm realm,
864 va_list ap)
865 {
866 return build_principal(context, principal, rlen, realm, va_princ, ap);
867 }
868
869 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
870 krb5_build_principal_va_ext(krb5_context context,
871 krb5_principal *principal,
872 int rlen,
873 krb5_const_realm realm,
874 va_list ap)
875 {
876 return build_principal(context, principal, rlen, realm, va_ext_princ, ap);
877 }
878
879
880 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
881 krb5_build_principal_ext(krb5_context context,
882 krb5_principal *principal,
883 int rlen,
884 krb5_const_realm realm,
885 ...)
886 {
887 krb5_error_code ret;
888 va_list ap;
889 va_start(ap, realm);
890 ret = krb5_build_principal_va_ext(context, principal, rlen, realm, ap);
891 va_end(ap);
892 return ret;
893 }
894
895 /**
896 * Copy a principal
897 *
898 * @param context A Kerberos context.
899 * @param inprinc principal to copy
900 * @param outprinc copied principal, free with krb5_free_principal()
901 *
902 * @return An krb5 error code, see krb5_get_error_message().
903 *
904 * @ingroup krb5_principal
905 */
906
907
908 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
909 krb5_copy_principal(krb5_context context,
910 krb5_const_principal inprinc,
911 krb5_principal *outprinc)
912 {
913 krb5_principal p = malloc(sizeof(*p));
914 if (p == NULL)
915 return krb5_enomem(context);
916 if(copy_Principal(inprinc, p)) {
917 free(p);
918 return krb5_enomem(context);
919 }
920 *outprinc = p;
921 return 0;
922 }
923
924 /**
925 * Return TRUE iff princ1 == princ2 (without considering the realm)
926 *
927 * @param context Kerberos 5 context
928 * @param princ1 first principal to compare
929 * @param princ2 second principal to compare
930 *
931 * @return non zero if equal, 0 if not
932 *
933 * @ingroup krb5_principal
934 * @see krb5_principal_compare()
935 * @see krb5_realm_compare()
936 */
937
938 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
939 krb5_principal_compare_any_realm(krb5_context context,
940 krb5_const_principal princ1,
941 krb5_const_principal princ2)
942 {
943 size_t i;
944 if(princ_num_comp(princ1) != princ_num_comp(princ2))
945 return FALSE;
946 for(i = 0; i < princ_num_comp(princ1); i++){
947 if(strcmp(princ_ncomp(princ1, i), princ_ncomp(princ2, i)) != 0)
948 return FALSE;
949 }
950 return TRUE;
951 }
952
953 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
954 _krb5_principal_compare_PrincipalName(krb5_context context,
955 krb5_const_principal princ1,
956 PrincipalName *princ2)
957 {
958 size_t i;
959 if (princ_num_comp(princ1) != princ2->name_string.len)
960 return FALSE;
961 for(i = 0; i < princ_num_comp(princ1); i++){
962 if(strcmp(princ_ncomp(princ1, i), princ2->name_string.val[i]) != 0)
963 return FALSE;
964 }
965 return TRUE;
966 }
967
968
969 /**
970 * Compares the two principals, including realm of the principals and returns
971 * TRUE if they are the same and FALSE if not.
972 *
973 * @param context Kerberos 5 context
974 * @param princ1 first principal to compare
975 * @param princ2 second principal to compare
976 *
977 * @ingroup krb5_principal
978 * @see krb5_principal_compare_any_realm()
979 * @see krb5_realm_compare()
980 */
981
982 /*
983 * return TRUE iff princ1 == princ2
984 */
985
986 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
987 krb5_principal_compare(krb5_context context,
988 krb5_const_principal princ1,
989 krb5_const_principal princ2)
990 {
991 if (!krb5_realm_compare(context, princ1, princ2))
992 return FALSE;
993 return krb5_principal_compare_any_realm(context, princ1, princ2);
994 }
995
996 /**
997 * return TRUE iff realm(princ1) == realm(princ2)
998 *
999 * @param context Kerberos 5 context
1000 * @param princ1 first principal to compare
1001 * @param princ2 second principal to compare
1002 *
1003 * @ingroup krb5_principal
1004 * @see krb5_principal_compare_any_realm()
1005 * @see krb5_principal_compare()
1006 */
1007
1008 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1009 krb5_realm_compare(krb5_context context,
1010 krb5_const_principal princ1,
1011 krb5_const_principal princ2)
1012 {
1013 return strcmp(princ_realm(princ1), princ_realm(princ2)) == 0;
1014 }
1015
1016 /**
1017 * return TRUE iff princ matches pattern
1018 *
1019 * @ingroup krb5_principal
1020 */
1021
1022 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1023 krb5_principal_match(krb5_context context,
1024 krb5_const_principal princ,
1025 krb5_const_principal pattern)
1026 {
1027 size_t i;
1028 if(princ_num_comp(princ) != princ_num_comp(pattern))
1029 return FALSE;
1030 if(fnmatch(princ_realm(pattern), princ_realm(princ), 0) != 0)
1031 return FALSE;
1032 for(i = 0; i < princ_num_comp(princ); i++){
1033 if(fnmatch(princ_ncomp(pattern, i), princ_ncomp(princ, i), 0) != 0)
1034 return FALSE;
1035 }
1036 return TRUE;
1037 }
1038
1039 /*
1040 * This is the original krb5_sname_to_principal(), renamed to be a
1041 * helper of the new one.
1042 */
1043 static krb5_error_code
1044 krb5_sname_to_principal_old(krb5_context context,
1045 const char *realm,
1046 const char *hostname,
1047 const char *sname,
1048 int32_t type,
1049 krb5_principal *ret_princ)
1050 {
1051 krb5_error_code ret;
1052 char localhost[MAXHOSTNAMELEN];
1053 char **realms = NULL, *host = NULL;
1054
1055 if(type != KRB5_NT_SRV_HST && type != KRB5_NT_UNKNOWN) {
1056 krb5_set_error_message(context, KRB5_SNAME_UNSUPP_NAMETYPE,
1057 N_("unsupported name type %d", ""),
1058 (int)type);
1059 return KRB5_SNAME_UNSUPP_NAMETYPE;
1060 }
1061 if(hostname == NULL) {
1062 ret = gethostname(localhost, sizeof(localhost) - 1);
1063 if (ret != 0) {
1064 ret = errno;
1065 krb5_set_error_message(context, ret,
1066 N_("Failed to get local hostname", ""));
1067 return ret;
1068 }
1069 localhost[sizeof(localhost) - 1] = '\0';
1070 hostname = localhost;
1071 }
1072 if(sname == NULL)
1073 sname = "host";
1074 if(type == KRB5_NT_SRV_HST) {
1075 if (realm)
1076 ret = krb5_expand_hostname(context, hostname, &host);
1077 else
1078 ret = krb5_expand_hostname_realms(context, hostname,
1079 &host, &realms);
1080 if (ret)
1081 return ret;
1082 strlwr(host);
1083 hostname = host;
1084 if (!realm)
1085 realm = realms[0];
1086 } else if (!realm) {
1087 ret = krb5_get_host_realm(context, hostname, &realms);
1088 if(ret)
1089 return ret;
1090 realm = realms[0];
1091 }
1092
1093 ret = krb5_make_principal(context, ret_princ, realm, sname,
1094 hostname, NULL);
1095 if(host)
1096 free(host);
1097 if (realms)
1098 krb5_free_host_realm(context, realms);
1099 return ret;
1100 }
1101
1102 static const struct {
1103 const char *type;
1104 int32_t value;
1105 } nametypes[] = {
1106 { "UNKNOWN", KRB5_NT_UNKNOWN },
1107 { "PRINCIPAL", KRB5_NT_PRINCIPAL },
1108 { "SRV_INST", KRB5_NT_SRV_INST },
1109 { "SRV_HST", KRB5_NT_SRV_HST },
1110 { "SRV_XHST", KRB5_NT_SRV_XHST },
1111 { "UID", KRB5_NT_UID },
1112 { "X500_PRINCIPAL", KRB5_NT_X500_PRINCIPAL },
1113 { "SMTP_NAME", KRB5_NT_SMTP_NAME },
1114 { "ENTERPRISE_PRINCIPAL", KRB5_NT_ENTERPRISE_PRINCIPAL },
1115 { "WELLKNOWN", KRB5_NT_WELLKNOWN },
1116 { "SRV_HST_DOMAIN", KRB5_NT_SRV_HST_DOMAIN },
1117 { "ENT_PRINCIPAL_AND_ID", KRB5_NT_ENT_PRINCIPAL_AND_ID },
1118 { "MS_PRINCIPAL", KRB5_NT_MS_PRINCIPAL },
1119 { "MS_PRINCIPAL_AND_ID", KRB5_NT_MS_PRINCIPAL_AND_ID },
1120 { "SRV_HST_NEEDS_CANON", KRB5_NT_SRV_HST_NEEDS_CANON },
1121 { NULL, 0 }
1122 };
1123
1124 /**
1125 * Parse nametype string and return a nametype integer
1126 *
1127 * @ingroup krb5_principal
1128 */
1129
1130 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1131 krb5_parse_nametype(krb5_context context, const char *str, int32_t *nametype)
1132 {
1133 size_t i;
1134
1135 for(i = 0; nametypes[i].type; i++) {
1136 if (strcasecmp(nametypes[i].type, str) == 0) {
1137 *nametype = nametypes[i].value;
1138 return 0;
1139 }
1140 }
1141 krb5_set_error_message(context, KRB5_PARSE_MALFORMED,
1142 N_("Failed to find name type %s", ""), str);
1143 return KRB5_PARSE_MALFORMED;
1144 }
1145
1146 /**
1147 * Returns true if name is Kerberos NULL name
1148 *
1149 * @ingroup krb5_principal
1150 */
1151
1152 krb5_boolean KRB5_LIB_FUNCTION
1153 krb5_principal_is_null(krb5_context context, krb5_const_principal principal)
1154 {
1155 if (principal->name.name_type == KRB5_NT_WELLKNOWN &&
1156 principal->name.name_string.len == 2 &&
1157 strcmp(principal->name.name_string.val[0], "WELLKNOWN") == 0 &&
1158 strcmp(principal->name.name_string.val[1], "NULL") == 0)
1159 return TRUE;
1160 return FALSE;
1161 }
1162
1163 const char _krb5_wellknown_lkdc[] = "WELLKNOWN:COM.APPLE.LKDC";
1164 static const char lkdc_prefix[] = "LKDC:";
1165
1166 /**
1167 * Returns true if name is Kerberos an LKDC realm
1168 *
1169 * @ingroup krb5_principal
1170 */
1171
1172 krb5_boolean KRB5_LIB_FUNCTION
1173 krb5_realm_is_lkdc(const char *realm)
1174 {
1175
1176 return strncmp(realm, lkdc_prefix, sizeof(lkdc_prefix)-1) == 0 ||
1177 strncmp(realm, _krb5_wellknown_lkdc, sizeof(_krb5_wellknown_lkdc) - 1) == 0;
1178 }
1179
1180 /**
1181 * Returns true if name is Kerberos an LKDC realm
1182 *
1183 * @ingroup krb5_principal
1184 */
1185
1186 krb5_boolean KRB5_LIB_FUNCTION
1187 krb5_principal_is_lkdc(krb5_context context, krb5_const_principal principal)
1188 {
1189 return krb5_realm_is_lkdc(principal->realm);
1190 }
1191
1192 /**
1193 * Returns true if name is Kerberos an LKDC realm
1194 *
1195 * @ingroup krb5_principal
1196 */
1197
1198 krb5_boolean KRB5_LIB_FUNCTION
1199 krb5_principal_is_pku2u(krb5_context context, krb5_const_principal principal)
1200 {
1201 return strcmp(principal->realm, KRB5_PKU2U_REALM_NAME) == 0;
1202 }
1203
1204 /**
1205 * Check if the cname part of the principal is a krbtgt principal
1206 *
1207 * @ingroup krb5_principal
1208 */
1209
1210 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1211 krb5_principal_is_krbtgt(krb5_context context, krb5_const_principal p)
1212 {
1213 return p->name.name_string.len == 2 &&
1214 strcmp(p->name.name_string.val[0], KRB5_TGS_NAME) == 0;
1215 }
1216
1217 /**
1218 * Returns true iff name is an WELLKNOWN:ORG.H5L.HOSTBASED-SERVICE
1219 *
1220 * @ingroup krb5_principal
1221 */
1222
1223 krb5_boolean KRB5_LIB_FUNCTION
1224 krb5_principal_is_gss_hostbased_service(krb5_context context,
1225 krb5_const_principal principal)
1226 {
1227 if (principal == NULL)
1228 return FALSE;
1229 if (principal->name.name_string.len != 2)
1230 return FALSE;
1231 if (strcmp(principal->name.name_string.val[1], KRB5_GSS_HOSTBASED_SERVICE_NAME) != 0)
1232 return FALSE;
1233 return TRUE;
1234 }
1235
1236 /**
1237 * Check if the cname part of the principal is a initial or renewed krbtgt principal
1238 *
1239 * @ingroup krb5_principal
1240 */
1241
1242 krb5_boolean KRB5_LIB_FUNCTION
1243 krb5_principal_is_root_krbtgt(krb5_context context, krb5_const_principal p)
1244 {
1245 return p->name.name_string.len == 2 &&
1246 strcmp(p->name.name_string.val[0], KRB5_TGS_NAME) == 0 &&
1247 strcmp(p->name.name_string.val[1], p->realm) == 0;
1248 }
1249
1250 /**
1251 * Returns true iff name is WELLKNOWN/ANONYMOUS
1252 *
1253 * @ingroup krb5_principal
1254 */
1255
1256 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1257 krb5_principal_is_anonymous(krb5_context context,
1258 krb5_const_principal p,
1259 unsigned int flags)
1260 {
1261 /*
1262 * Heimdal versions 7.5 and below left the name-type at KRB5_NT_PRINCIPAL
1263 * even with anonymous pkinit responses. To retain interoperability with
1264 * legacy KDCs, the name-type is not checked by the client after requesting
1265 * a fully anonymous ticket.
1266 */
1267 if (!(flags & KRB5_ANON_IGNORE_NAME_TYPE) &&
1268 p->name.name_type != KRB5_NT_WELLKNOWN &&
1269 p->name.name_type != KRB5_NT_UNKNOWN)
1270 return FALSE;
1271
1272 if (p->name.name_string.len != 2 ||
1273 strcmp(p->name.name_string.val[0], KRB5_WELLKNOWN_NAME) != 0 ||
1274 strcmp(p->name.name_string.val[1], KRB5_ANON_NAME) != 0)
1275 return FALSE;
1276
1277 /*
1278 * While unauthenticated clients SHOULD get "WELLKNOWN:ANONYMOUS" as their
1279 * realm, Heimdal KDCs prior to 7.0 returned the requested realm. While
1280 * such tickets might lead *servers* to unwittingly grant access to fully
1281 * anonymous clients, trusting that the client was authenticated to the
1282 * realm in question, doing it right is the KDC's job, the client should
1283 * not refuse such a ticket.
1284 *
1285 * If we ever do decide to enforce WELLKNOWN:ANONYMOUS for unauthenticated
1286 * clients, it is essential that calls that pass KRB5_ANON_MATCH_ANY still
1287 * ignore the realm, as in that case either case matches one of the two
1288 * possible conditions.
1289 */
1290 if (flags & KRB5_ANON_MATCH_UNAUTHENTICATED)
1291 return TRUE;
1292
1293 /*
1294 * Finally, authenticated clients that asked to be only anonymized do
1295 * legitimately expect a non-anon realm.
1296 */
1297 return strcmp(p->realm, KRB5_ANON_REALM) != 0;
1298 }
1299
1300 static int
1301 tolower_ascii(int c)
1302 {
1303 if (c >= 'A' || c <= 'Z')
1304 return 'a' + (c - 'A');
1305 return c;
1306 }
1307
1308 typedef enum krb5_name_canon_rule_type {
1309 KRB5_NCRT_BOGUS = 0,
1310 KRB5_NCRT_AS_IS,
1311 KRB5_NCRT_QUALIFY,
1312 KRB5_NCRT_NSS
1313 } krb5_name_canon_rule_type;
1314
1315 #ifdef UINT8_MAX
1316 #define MAXDOTS UINT8_MAX
1317 #else
1318 #define MAXDOTS (255U)
1319 #endif
1320 #ifdef UINT16_MAX
1321 #define MAXORDER UINT16_MAX
1322 #else
1323 #define MAXORDER (65535U)
1324 #endif
1325
1326 struct krb5_name_canon_rule_data {
1327 krb5_name_canon_rule_type type;
1328 krb5_name_canon_rule_options options;
1329 uint8_t mindots; /* match this many dots or more */
1330 uint8_t maxdots; /* match no more than this many dots */
1331 uint16_t explicit_order; /* given order */
1332 uint16_t order; /* actual order */
1333 char *match_domain; /* match this stem */
1334 char *match_realm; /* match this realm */
1335 char *domain; /* qualify with this domain */
1336 char *realm; /* qualify with this realm */
1337 };
1338
1339 /**
1340 * Create a principal for the given service running on the given
1341 * hostname. If KRB5_NT_SRV_HST is used, the hostname is canonicalized
1342 * according the configured name canonicalization rules, with
1343 * canonicalization delayed in some cases. One rule involves DNS, which
1344 * is insecure unless DNSSEC is used, but we don't use DNSSEC-capable
1345 * resolver APIs here, so that if DNSSEC is used we wouldn't know it.
1346 *
1347 * Canonicalization is immediate (not delayed) only when there is only
1348 * one canonicalization rule and that rule indicates that we should do a
1349 * host lookup by name (i.e., DNS).
1350 *
1351 * @param context A Kerberos context.
1352 * @param hostname hostname to use
1353 * @param sname Service name to use
1354 * @param type name type of principal, use KRB5_NT_SRV_HST or KRB5_NT_UNKNOWN.
1355 * @param ret_princ return principal, free with krb5_free_principal().
1356 *
1357 * @return An krb5 error code, see krb5_get_error_message().
1358 *
1359 * @ingroup krb5_principal
1360 */
1361
1362 /* coverity[+alloc : arg-*4] */
1363 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1364 krb5_sname_to_principal(krb5_context context,
1365 const char *hostname,
1366 const char *sname,
1367 int32_t type,
1368 krb5_principal *ret_princ)
1369 {
1370 char *realm, *remote_host;
1371 krb5_error_code ret;
1372 register char *cp;
1373 char localname[MAXHOSTNAMELEN];
1374
1375 *ret_princ = NULL;
1376
1377 if ((type != KRB5_NT_UNKNOWN) &&
1378 (type != KRB5_NT_SRV_HST))
1379 return KRB5_SNAME_UNSUPP_NAMETYPE;
1380
1381 /* if hostname is NULL, use local hostname */
1382 if (hostname == NULL) {
1383 if (gethostname(localname, MAXHOSTNAMELEN))
1384 return errno;
1385 hostname = localname;
1386 }
1387
1388 /* if sname is NULL, use "host" */
1389 if (sname == NULL)
1390 sname = "host";
1391
1392 remote_host = strdup(hostname);
1393 if (remote_host == NULL)
1394 return krb5_enomem(context);
1395
1396 if (type == KRB5_NT_SRV_HST) {
1397 krb5_name_canon_rule rules;
1398
1399 /* Lower-case the hostname, because that's the convention */
1400 for (cp = remote_host; *cp; cp++)
1401 if (isupper((int) (*cp)))
1402 *cp = tolower((int) (*cp));
1403
1404 /*
1405 * If there is only one name canon rule and it says to
1406 * canonicalize the old way, do that now, as we used to.
1407 */
1408 ret = _krb5_get_name_canon_rules(context, &rules);
1409 if (ret) {
1410 _krb5_debug(context, 5, "Failed to get name canon rules: ret = %d",
1411 ret);
1412 free(remote_host);
1413 return ret;
1414 }
1415 if (rules[0].type == KRB5_NCRT_NSS &&
1416 rules[1].type == KRB5_NCRT_BOGUS) {
1417 _krb5_debug(context, 5, "Using nss for name canon immediately");
1418 ret = krb5_sname_to_principal_old(context, rules[0].realm,
1419 remote_host, sname,
1420 KRB5_NT_SRV_HST, ret_princ);
1421 free(remote_host);
1422 return ret;
1423 }
1424 }
1425
1426 /* Remove trailing dots */
1427 if (remote_host[0]) {
1428 for (cp = remote_host + strlen(remote_host)-1;
1429 *cp == '.' && cp > remote_host;
1430 cp--) {
1431 *cp = '\0';
1432 }
1433 }
1434
1435 realm = ""; /* "Referral realm" */
1436
1437 ret = krb5_build_principal(context, ret_princ, strlen(realm),
1438 realm, sname, remote_host,
1439 (char *)0);
1440
1441 if (ret == 0 && type == KRB5_NT_SRV_HST) {
1442 /*
1443 * Hostname canonicalization is done elsewhere (in
1444 * krb5_get_credentials() and krb5_kt_get_entry()).
1445 *
1446 * We overload the name type to indicate to those functions that
1447 * this principal name requires canonicalization.
1448 *
1449 * We can't use the empty realm to denote the need to
1450 * canonicalize the hostname too: it would mean that users who
1451 * want to assert knowledge of a service's realm must also know
1452 * the canonical hostname, but in practice they don't.
1453 */
1454 (*ret_princ)->name.name_type = KRB5_NT_SRV_HST_NEEDS_CANON;
1455
1456 _krb5_debug(context, 5, "Building a delayed canon principal for %s/%s@",
1457 sname, remote_host);
1458 }
1459
1460 free(remote_host);
1461 return ret;
1462 }
1463
1464 static void
1465 tolower_str(char *s)
1466 {
1467 for (; *s != '\0'; s++) {
1468 if (isupper(*s))
1469 *s = tolower_ascii(*s);
1470 }
1471 }
1472
1473 static krb5_error_code
1474 rule_parse_token(krb5_context context, krb5_name_canon_rule rule,
1475 const char *tok)
1476 {
1477 long int n;
1478 int needs_type = rule->type == KRB5_NCRT_BOGUS;
1479
1480 /*
1481 * Rules consist of a sequence of tokens, some of which indicate
1482 * what type of rule the rule is, and some of which set rule options
1483 * or ancilliary data. Last rule type token wins.
1484 */
1485
1486 /* Rule type tokens: */
1487 if (needs_type && strcmp(tok, "as-is") == 0) {
1488 rule->type = KRB5_NCRT_AS_IS;
1489 } else if (needs_type && strcmp(tok, "qualify") == 0) {
1490 rule->type = KRB5_NCRT_QUALIFY;
1491 } else if (needs_type && strcmp(tok, "nss") == 0) {
1492 rule->type = KRB5_NCRT_NSS;
1493 /* Rule options: */
1494 } else if (strcmp(tok, "use_fast") == 0) {
1495 rule->options |= KRB5_NCRO_USE_FAST;
1496 } else if (strcmp(tok, "use_dnssec") == 0) {
1497 rule->options |= KRB5_NCRO_USE_DNSSEC;
1498 } else if (strcmp(tok, "ccache_only") == 0) {
1499 rule->options |= KRB5_NCRO_GC_ONLY;
1500 } else if (strcmp(tok, "no_referrals") == 0) {
1501 rule->options |= KRB5_NCRO_NO_REFERRALS;
1502 } else if (strcmp(tok, "use_referrals") == 0) {
1503 rule->options &= ~KRB5_NCRO_NO_REFERRALS;
1504 if (rule->realm == NULL) {
1505 rule->realm = strdup("");
1506 if (rule->realm == NULL)
1507 return krb5_enomem(context);
1508 }
1509 } else if (strcmp(tok, "lookup_realm") == 0) {
1510 rule->options |= KRB5_NCRO_LOOKUP_REALM;
1511 free(rule->realm);
1512 rule->realm = NULL;
1513 /* Rule ancilliary data: */
1514 } else if (strncmp(tok, "domain=", strlen("domain=")) == 0) {
1515 free(rule->domain);
1516 rule->domain = strdup(tok + strlen("domain="));
1517 if (rule->domain == NULL)
1518 return krb5_enomem(context);
1519 tolower_str(rule->domain);
1520 } else if (strncmp(tok, "realm=", strlen("realm=")) == 0) {
1521 free(rule->realm);
1522 rule->realm = strdup(tok + strlen("realm="));
1523 if (rule->realm == NULL)
1524 return krb5_enomem(context);
1525 } else if (strncmp(tok, "match_domain=", strlen("match_domain=")) == 0) {
1526 free(rule->match_domain);
1527 rule->match_domain = strdup(tok + strlen("match_domain="));
1528 if (rule->match_domain == NULL)
1529 return krb5_enomem(context);
1530 tolower_str(rule->match_domain);
1531 } else if (strncmp(tok, "match_realm=", strlen("match_realm=")) == 0) {
1532 free(rule->match_realm);
1533 rule->match_realm = strdup(tok + strlen("match_realm="));
1534 if (rule->match_realm == NULL)
1535 return krb5_enomem(context);
1536 } else if (strncmp(tok, "mindots=", strlen("mindots=")) == 0) {
1537 errno = 0;
1538 n = strtol(tok + strlen("mindots="), NULL, 10);
1539 if (errno == 0 && n > 0 && n <= MAXDOTS)
1540 rule->mindots = n;
1541 } else if (strncmp(tok, "maxdots=", strlen("maxdots=")) == 0) {
1542 errno = 0;
1543 n = strtol(tok + strlen("maxdots="), NULL, 10);
1544 if (errno == 0 && n > 0 && n <= MAXDOTS)
1545 rule->maxdots = n;
1546 } else if (strncmp(tok, "order=", strlen("order=")) == 0) {
1547 errno = 0;
1548 n = strtol(tok + strlen("order="), NULL, 10);
1549 if (errno == 0 && n > 0 && n <= MAXORDER)
1550 rule->explicit_order = n;
1551 } else {
1552 _krb5_debug(context, 5,
1553 "Unrecognized name canonicalization rule token %s", tok);
1554 return EINVAL;
1555 }
1556 return 0;
1557 }
1558
1559 static int
1560 rule_cmp(const void *a, const void *b)
1561 {
1562 krb5_const_name_canon_rule left = a;
1563 krb5_const_name_canon_rule right = b;
1564
1565 if (left->type == KRB5_NCRT_BOGUS &&
1566 right->type == KRB5_NCRT_BOGUS)
1567 return 0;
1568 if (left->type == KRB5_NCRT_BOGUS)
1569 return 1;
1570 if (right->type == KRB5_NCRT_BOGUS)
1571 return -1;
1572 if (left->explicit_order < right->explicit_order)
1573 return -1;
1574 if (left->explicit_order > right->explicit_order)
1575 return 1;
1576 return left->order - right->order;
1577 }
1578
1579 static krb5_error_code
1580 parse_name_canon_rules(krb5_context context, char **rulestrs,
1581 krb5_name_canon_rule *rules)
1582 {
1583 krb5_error_code ret;
1584 char *tok;
1585 char *cp;
1586 char **cpp;
1587 size_t n;
1588 size_t i, k;
1589 int do_sort = 0;
1590 krb5_name_canon_rule r;
1591
1592 *rules = NULL;
1593
1594 for (n =0, cpp = rulestrs; cpp != NULL && *cpp != NULL; cpp++)
1595 n++;
1596
1597 n += 2; /* Always at least one rule; two for the default case */
1598
1599 if ((r = calloc(n, sizeof (*r))) == NULL)
1600 return krb5_enomem(context);
1601
1602 for (k = 0; k < n; k++) {
1603 r[k].type = KRB5_NCRT_BOGUS;
1604 r[k].match_domain = NULL;
1605 r[k].match_realm = NULL;
1606 r[k].domain = NULL;
1607 r[k].realm = NULL;
1608 }
1609
1610 for (i = 0, k = 0; i < n && rulestrs != NULL && rulestrs[i] != NULL; i++) {
1611 cp = rulestrs[i];
1612 r[k].explicit_order = MAXORDER; /* mark order, see below */
1613 r[k].maxdots = MAXDOTS;
1614 r[k].order = k; /* default order */
1615
1616 /* Tokenize and parse value */
1617 do {
1618 tok = cp;
1619 cp = strchr(cp, ':'); /* XXX use strtok_r() */
1620 if (cp)
1621 *cp++ = '\0'; /* delimit token */
1622 ret = rule_parse_token(context, &r[k], tok);
1623 if (ret == EINVAL) {
1624 r[k].type = KRB5_NCRT_BOGUS;
1625 break;
1626 }
1627 if (ret) {
1628 _krb5_free_name_canon_rules(context, r);
1629 return ret;
1630 }
1631 } while (cp && *cp);
1632 if (r[k].explicit_order != MAXORDER)
1633 do_sort = 1;
1634
1635 /* Validate parsed rule */
1636 if (r[k].type == KRB5_NCRT_BOGUS ||
1637 (r[k].type == KRB5_NCRT_QUALIFY && !r[k].domain) ||
1638 (r[k].type == KRB5_NCRT_NSS && r[k].domain)) {
1639 /* Invalid rule; mark it so and clean up */
1640 r[k].type = KRB5_NCRT_BOGUS;
1641 free(r[k].match_domain);
1642 free(r[k].match_realm);
1643 free(r[k].domain);
1644 free(r[k].realm);
1645 r[k].realm = NULL;
1646 r[k].domain = NULL;
1647 r[k].match_domain = NULL;
1648 r[k].match_realm = NULL;
1649 _krb5_debug(context, 5,
1650 "Ignoring invalid name canonicalization rule %lu",
1651 (unsigned long)i);
1652 continue;
1653 }
1654 k++; /* good rule */
1655 }
1656
1657 if (do_sort) {
1658 /*
1659 * Note that we make make this a stable sort by using appareance
1660 * and explicit order.
1661 */
1662 qsort(r, n, sizeof(r[0]), rule_cmp);
1663 }
1664
1665 if (r[0].type == KRB5_NCRT_BOGUS) {
1666 /* No rules, or no valid rules */
1667 r[0].type = KRB5_NCRT_NSS;
1668 }
1669
1670 *rules = r;
1671 return 0; /* We don't communicate bad rule errors here */
1672 }
1673
1674 /*
1675 * This exists only because the hostname canonicalization behavior in Heimdal
1676 * (and other implementations of Kerberos) has been to use getaddrinfo(),
1677 * unsafe though it is, for ages. We can't fix it in one day.
1678 */
1679 static void
1680 make_rules_safe(krb5_context context, krb5_name_canon_rule rules)
1681 {
1682 /*
1683 * If the only rule were to use the name service (getaddrinfo()) then we're
1684 * bound to fail. We could try to convert that rule to an as-is rule, but
1685 * when we do get a validating resolver we'd be unhappy that we did such a
1686 * conversion. Better let the user get failures and make them think about
1687 * their naming rules.
1688 */
1689 if (rules == NULL)
1690 return;
1691 for (; rules[0].type != KRB5_NCRT_BOGUS; rules++) {
1692 if (rules->type == KRB5_NCRT_NSS)
1693 rules->options |= KRB5_NCRO_USE_DNSSEC;
1694 else
1695 rules->options |= KRB5_NCRO_USE_FAST;
1696 }
1697 }
1698
1699 /**
1700 * This function returns an array of host-based service name
1701 * canonicalization rules. The array of rules is organized as a list.
1702 * See the definition of krb5_name_canon_rule.
1703 *
1704 * @param context A Kerberos context.
1705 * @param rules Output location for array of rules.
1706 */
1707 KRB5_LIB_FUNCTION krb5_error_code
1708 _krb5_get_name_canon_rules(krb5_context context, krb5_name_canon_rule *rules)
1709 {
1710 krb5_error_code ret;
1711 char **values = NULL;
1712
1713 *rules = context->name_canon_rules;
1714 if (*rules != NULL)
1715 return 0;
1716
1717 values = krb5_config_get_strings(context, NULL,
1718 "libdefaults", "name_canon_rules", NULL);
1719 ret = parse_name_canon_rules(context, values, rules);
1720 krb5_config_free_strings(values);
1721 if (ret)
1722 return ret;
1723
1724 if (krb5_config_get_bool_default(context, NULL, FALSE,
1725 "libdefaults", "safe_name_canon", NULL))
1726 make_rules_safe(context, *rules);
1727
1728 heim_assert(rules != NULL && (*rules)[0].type != KRB5_NCRT_BOGUS,
1729 "internal error in parsing principal name "
1730 "canonicalization rules");
1731
1732 /* Memoize */
1733 context->name_canon_rules = *rules;
1734
1735 return 0;
1736 }
1737
1738 static krb5_error_code
1739 get_host_realm(krb5_context context, const char *hostname, char **realm)
1740 {
1741 krb5_error_code ret;
1742 char **hrealms = NULL;
1743
1744 *realm = NULL;
1745 ret = krb5_get_host_realm(context, hostname, &hrealms);
1746 if (ret)
1747 return ret;
1748 if (hrealms == NULL)
1749 return KRB5_ERR_HOST_REALM_UNKNOWN; /* krb5_set_error() already done */
1750 if (hrealms[0] == NULL) {
1751 krb5_free_host_realm(context, hrealms);
1752 return KRB5_ERR_HOST_REALM_UNKNOWN; /* krb5_set_error() already done */
1753 }
1754 *realm = strdup(hrealms[0]);
1755 krb5_free_host_realm(context, hrealms);
1756 if (*realm == NULL)
1757 return krb5_enomem(context);
1758 return 0;
1759 }
1760
1761 static int
1762 is_domain_suffix(const char *domain, const char *suffix)
1763 {
1764 size_t dlen = strlen(domain);
1765 size_t slen = strlen(suffix);
1766
1767 if (dlen < slen + 2)
1768 return 0;
1769
1770 if (strcasecmp(domain + (dlen - slen), suffix) != 0)
1771 return 0;
1772
1773 if (domain[(dlen - slen) - 1] != '.')
1774 return 0;
1775 return 1;
1776 }
1777
1778 /*
1779 * Applies a name canonicalization rule to a principal.
1780 *
1781 * Returns zero and no out_princ if the rule does not match.
1782 * Returns zero and an out_princ if the rule does match.
1783 */
1784 static krb5_error_code
1785 apply_name_canon_rule(krb5_context context, krb5_name_canon_rule rules,
1786 size_t rule_idx, krb5_const_principal in_princ,
1787 krb5_principal *out_princ,
1788 krb5_name_canon_rule_options *rule_opts)
1789 {
1790 krb5_name_canon_rule rule = &rules[rule_idx];
1791 krb5_error_code ret;
1792 unsigned int ndots = 0;
1793 krb5_principal nss = NULL;
1794 const char *sname = NULL;
1795 const char *orig_hostname = NULL;
1796 const char *new_hostname = NULL;
1797 const char *new_realm = NULL;
1798 const char *port = "";
1799 const char *cp;
1800 char *hostname_sans_port = NULL;
1801 char *hostname_with_port = NULL;
1802 char *tmp_hostname = NULL;
1803 char *tmp_realm = NULL;
1804
1805 *out_princ = NULL; /* Signal no match */
1806
1807 if (rule_opts != NULL)
1808 *rule_opts = rule->options;
1809
1810 if (rule->type == KRB5_NCRT_BOGUS)
1811 return 0; /* rule doesn't apply */
1812
1813 sname = krb5_principal_get_comp_string(context, in_princ, 0);
1814 orig_hostname = krb5_principal_get_comp_string(context, in_princ, 1);
1815
1816 /*
1817 * Some apps want to use the very non-standard svc/hostname:port@REALM
1818 * form. We do our best to support that here :(
1819 */
1820 port = strchr(orig_hostname, ':');
1821 if (port != NULL) {
1822 hostname_sans_port = strndup(orig_hostname, port - orig_hostname);
1823 if (hostname_sans_port == NULL)
1824 return krb5_enomem(context);
1825 orig_hostname = hostname_sans_port;
1826 }
1827
1828 _krb5_debug(context, 5, N_("Applying a name rule (type %d) to %s", ""),
1829 rule->type, orig_hostname);
1830
1831 if (rule->mindots > 0 || rule->maxdots > 0) {
1832 for (cp = strchr(orig_hostname, '.'); cp && *cp; cp = strchr(cp + 1, '.'))
1833 ndots++;
1834 }
1835 if (rule->mindots > 0 && ndots < rule->mindots)
1836 return 0;
1837 if (ndots > rule->maxdots)
1838 return 0;
1839
1840 if (rule->match_domain != NULL &&
1841 !is_domain_suffix(orig_hostname, rule->match_domain))
1842 return 0;
1843
1844 if (rule->match_realm != NULL &&
1845 strcmp(rule->match_realm, in_princ->realm) != 0)
1846 return 0;
1847
1848 new_realm = rule->realm;
1849 switch (rule->type) {
1850 case KRB5_NCRT_AS_IS:
1851 break;
1852
1853 case KRB5_NCRT_QUALIFY:
1854 heim_assert(rule->domain != NULL,
1855 "missing domain for qualify name canon rule");
1856 if (asprintf(&tmp_hostname, "%s.%s", orig_hostname,
1857 rule->domain) == -1 || tmp_hostname == NULL) {
1858 ret = krb5_enomem(context);
1859 goto out;
1860 }
1861 new_hostname = tmp_hostname;
1862 break;
1863
1864 case KRB5_NCRT_NSS:
1865 if ((rule->options & KRB5_NCRO_USE_DNSSEC)) {
1866 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1867 krb5_set_error_message(context, ret,
1868 "Secure hostname resolution not supported");
1869 goto out;
1870 }
1871 _krb5_debug(context, 5, "Using name service lookups");
1872 ret = krb5_sname_to_principal_old(context, rule->realm,
1873 orig_hostname, sname,
1874 KRB5_NT_SRV_HST,
1875 &nss);
1876 if (rules[rule_idx + 1].type != KRB5_NCRT_BOGUS &&
1877 (ret == KRB5_ERR_BAD_HOSTNAME ||
1878 ret == KRB5_ERR_HOST_REALM_UNKNOWN)) {
1879 /*
1880 * Bad hostname / realm unknown -> rule inapplicable if
1881 * there's more rules. If it's the last rule then we want
1882 * to return all errors from krb5_sname_to_principal_old()
1883 * here.
1884 */
1885 ret = 0;
1886 goto out;
1887 }
1888 if (ret)
1889 goto out;
1890
1891 new_hostname = krb5_principal_get_comp_string(context, nss, 1);
1892 new_realm = krb5_principal_get_realm(context, nss);
1893 break;
1894
1895 default:
1896 /* Can't happen */
1897 ret = 0;
1898 goto out;
1899 }
1900
1901 /*
1902 * This rule applies.
1903 *
1904 * Copy in_princ and mutate the copy per the matched rule.
1905 *
1906 * This way we apply to principals with two or more components, such as
1907 * domain-based names.
1908 */
1909 ret = krb5_copy_principal(context, in_princ, out_princ);
1910 if (ret)
1911 goto out;
1912
1913 if (new_realm == NULL && (rule->options & KRB5_NCRO_LOOKUP_REALM) != 0) {
1914 ret = get_host_realm(context, new_hostname, &tmp_realm);
1915 if (ret)
1916 goto out;
1917 new_realm = tmp_realm;
1918 }
1919
1920 /* If we stripped off a :port, add it back in */
1921 if (port != NULL && new_hostname != NULL) {
1922 if (asprintf(&hostname_with_port, "%s%s", new_hostname, port) == -1 ||
1923 hostname_with_port == NULL) {
1924 ret = krb5_enomem(context);
1925 goto out;
1926 }
1927 new_hostname = hostname_with_port;
1928 }
1929
1930 if (new_realm != NULL)
1931 krb5_principal_set_realm(context, *out_princ, new_realm);
1932 if (new_hostname != NULL)
1933 krb5_principal_set_comp_string(context, *out_princ, 1, new_hostname);
1934 if (princ_type(*out_princ) == KRB5_NT_SRV_HST_NEEDS_CANON)
1935 princ_type(*out_princ) = KRB5_NT_SRV_HST;
1936
1937 /* Trace rule application */
1938 {
1939 krb5_error_code ret2;
1940 char *unparsed;
1941
1942 ret2 = krb5_unparse_name(context, *out_princ, &unparsed);
1943 if (ret2) {
1944 _krb5_debug(context, 5,
1945 N_("Couldn't unparse canonicalized princicpal (%d)",
1946 ""),
1947 ret);
1948 } else {
1949 _krb5_debug(context, 5,
1950 N_("Name canon rule application yields %s", ""),
1951 unparsed);
1952 free(unparsed);
1953 }
1954 }
1955
1956 out:
1957 free(hostname_sans_port);
1958 free(hostname_with_port);
1959 free(tmp_hostname);
1960 free(tmp_realm);
1961 krb5_free_principal(context, nss);
1962 if (ret)
1963 krb5_set_error_message(context, ret,
1964 N_("Name canon rule application failed", ""));
1965 return ret;
1966 }
1967
1968 /**
1969 * Free name canonicalization rules
1970 */
1971 KRB5_LIB_FUNCTION void
1972 _krb5_free_name_canon_rules(krb5_context context, krb5_name_canon_rule rules)
1973 {
1974 size_t k;
1975
1976 if (rules == NULL)
1977 return;
1978
1979 for (k = 0; rules[k].type != KRB5_NCRT_BOGUS; k++) {
1980 free(rules[k].match_domain);
1981 free(rules[k].match_realm);
1982 free(rules[k].domain);
1983 free(rules[k].realm);
1984 }
1985 free(rules);
1986 }
1987
1988 struct krb5_name_canon_iterator_data {
1989 krb5_name_canon_rule rules;
1990 krb5_const_principal in_princ; /* given princ */
1991 krb5_const_principal out_princ; /* princ to be output */
1992 krb5_principal tmp_princ; /* to be freed */
1993 int is_trivial; /* no canon to be done */
1994 int done; /* no more rules to be applied */
1995 size_t cursor; /* current/next rule */
1996 };
1997
1998 /**
1999 * Initialize name canonicalization iterator.
2000 *
2001 * @param context Kerberos context
2002 * @param in_princ principal name to be canonicalized OR
2003 * @param iter output iterator object
2004 */
2005 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2006 krb5_name_canon_iterator_start(krb5_context context,
2007 krb5_const_principal in_princ,
2008 krb5_name_canon_iterator *iter)
2009 {
2010 krb5_error_code ret;
2011 krb5_name_canon_iterator state;
2012
2013 *iter = NULL;
2014
2015 state = calloc(1, sizeof (*state));
2016 if (state == NULL)
2017 return krb5_enomem(context);
2018 state->in_princ = in_princ;
2019
2020 if (princ_type(state->in_princ) == KRB5_NT_SRV_HST_NEEDS_CANON) {
2021 ret = _krb5_get_name_canon_rules(context, &state->rules);
2022 if (ret)
2023 goto out;
2024 } else {
2025 /* Name needs no canon -> trivial iterator: in_princ is canonical */
2026 state->is_trivial = 1;
2027 }
2028
2029 *iter = state;
2030 return 0;
2031
2032 out:
2033 krb5_free_name_canon_iterator(context, state);
2034 return krb5_enomem(context);
2035 }
2036
2037 /*
2038 * Helper for name canon iteration.
2039 */
2040 static krb5_error_code
2041 name_canon_iterate(krb5_context context,
2042 krb5_name_canon_iterator *iter,
2043 krb5_name_canon_rule_options *rule_opts)
2044 {
2045 krb5_error_code ret;
2046 krb5_name_canon_iterator state = *iter;
2047
2048 if (rule_opts)
2049 *rule_opts = 0;
2050
2051 if (state == NULL)
2052 return 0;
2053
2054 if (state->done) {
2055 krb5_free_name_canon_iterator(context, state);
2056 *iter = NULL;
2057 return 0;
2058 }
2059
2060 if (state->is_trivial && !state->done) {
2061 state->out_princ = state->in_princ;
2062 state->done = 1;
2063 return 0;
2064 }
2065
2066 heim_assert(state->rules != NULL &&
2067 state->rules[state->cursor].type != KRB5_NCRT_BOGUS,
2068 "Internal error during name canonicalization");
2069
2070 do {
2071 krb5_free_principal(context, state->tmp_princ);
2072 ret = apply_name_canon_rule(context, state->rules, state->cursor,
2073 state->in_princ, &state->tmp_princ, rule_opts);
2074 if (ret) {
2075 krb5_free_name_canon_iterator(context, state);
2076 *iter = NULL;
2077 return ret;
2078 }
2079 state->cursor++;
2080 } while (state->tmp_princ == NULL &&
2081 state->rules[state->cursor].type != KRB5_NCRT_BOGUS);
2082
2083 if (state->rules[state->cursor].type == KRB5_NCRT_BOGUS)
2084 state->done = 1;
2085
2086 state->out_princ = state->tmp_princ;
2087 if (state->tmp_princ == NULL) {
2088 krb5_free_name_canon_iterator(context, state);
2089 *iter = NULL;
2090 return 0;
2091 }
2092 return 0;
2093 }
2094
2095 /**
2096 * Iteratively apply name canon rules, outputing a principal and rule
2097 * options each time. Iteration completes when the @iter is NULL on
2098 * return or when an error is returned. Callers must free the iterator
2099 * if they abandon it mid-way.
2100 *
2101 * @param context Kerberos context
2102 * @param iter name canon rule iterator (input/output)
2103 * @param try_princ output principal name
2104 * @param rule_opts output rule options
2105 */
2106 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2107 krb5_name_canon_iterate(krb5_context context,
2108 krb5_name_canon_iterator *iter,
2109 krb5_const_principal *try_princ,
2110 krb5_name_canon_rule_options *rule_opts)
2111 {
2112 krb5_error_code ret;
2113
2114 *try_princ = NULL;
2115
2116 ret = name_canon_iterate(context, iter, rule_opts);
2117 if (*iter)
2118 *try_princ = (*iter)->out_princ;
2119 return ret;
2120 }
2121
2122 /**
2123 * Free a name canonicalization rule iterator.
2124 */
2125 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
2126 krb5_free_name_canon_iterator(krb5_context context,
2127 krb5_name_canon_iterator iter)
2128 {
2129 if (iter == NULL)
2130 return;
2131 if (iter->tmp_princ)
2132 krb5_free_principal(context, iter->tmp_princ);
2133 free(iter);
2134 }