2 * Copyright (c) 1997-2007 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
35 * @page krb5_principal_intro The principal handing functions.
37 * A Kerberos principal is a email address looking string that
38 * contains to parts separeted by a @. The later part is the kerbero
39 * realm the principal belongs to and the former is a list of 0 or
40 * more components. For example
43 host/hummel.it.su.se@SU.SE
47 * See the library functions here: @ref krb5_principal
50 #include "krb5_locl.h"
51 #ifdef HAVE_RES_SEARCH
54 #ifdef HAVE_ARPA_NAMESER_H
55 #include <arpa/nameser.h>
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)
67 * Frees a Kerberos principal allocated by the library with
68 * krb5_parse_name(), krb5_make_principal() or any other related
69 * principal functions.
71 * @param context A Kerberos context.
72 * @param p a principal to free.
74 * @return An krb5 error code, see krb5_get_error_message().
76 * @ingroup krb5_principal
79 void KRB5_LIB_FUNCTION
80 krb5_free_principal(krb5_context context
,
90 * Set the type of the principal
92 * @param context A Kerberos context.
93 * @param principal principal to set the type for
94 * @param type the new type
96 * @return An krb5 error code, see krb5_get_error_message().
98 * @ingroup krb5_principal
101 void KRB5_LIB_FUNCTION
102 krb5_principal_set_type(krb5_context context
,
103 krb5_principal principal
,
106 princ_type(principal
) = type
;
110 * Get the type of the principal
112 * @param context A Kerberos context.
113 * @param principal principal to get the type for
115 * @return the type of principal
117 * @ingroup krb5_principal
120 int KRB5_LIB_FUNCTION
121 krb5_principal_get_type(krb5_context context
,
122 krb5_const_principal principal
)
124 return princ_type(principal
);
128 * Get the realm of the principal
130 * @param context A Kerberos context.
131 * @param principal principal to get the realm for
133 * @return realm of the principal, don't free or use after krb5_principal is freed
135 * @ingroup krb5_principal
138 const char* KRB5_LIB_FUNCTION
139 krb5_principal_get_realm(krb5_context context
,
140 krb5_const_principal principal
)
142 return princ_realm(principal
);
145 const char* KRB5_LIB_FUNCTION
146 krb5_principal_get_comp_string(krb5_context context
,
147 krb5_const_principal principal
,
148 unsigned int component
)
150 if(component
>= princ_num_comp(principal
))
152 return princ_ncomp(principal
, component
);
156 * Get number of component is principal.
158 * @param context Kerberos 5 context
159 * @param principal principal to query
161 * @return number of components in string
163 * @ingroup krb5_principal
166 unsigned int KRB5_LIB_FUNCTION
167 krb5_principal_get_num_comp(krb5_context context
,
168 krb5_const_principal principal
)
170 return princ_num_comp(principal
);
174 * Parse a name into a krb5_principal structure, flags controls the behavior.
176 * @param context Kerberos 5 context
177 * @param name name to parse into a Kerberos principal
178 * @param flags flags to control the behavior
179 * @param principal returned principal, free with krb5_free_principal().
181 * @return An krb5 error code, see krb5_get_error_message().
183 * @ingroup krb5_principal
186 krb5_error_code KRB5_LIB_FUNCTION
187 krb5_parse_name_flags(krb5_context context
,
190 krb5_principal
*principal
)
193 heim_general_string
*comp
;
194 heim_general_string realm
= NULL
;
206 int enterprise
= (flags
& KRB5_PRINCIPAL_PARSE_ENTERPRISE
);
210 #define RFLAGS (KRB5_PRINCIPAL_PARSE_NO_REALM|KRB5_PRINCIPAL_PARSE_REQUIRE_REALM)
212 if ((flags
& RFLAGS
) == RFLAGS
) {
213 krb5_set_error_message(context
, KRB5_ERR_NO_SERVICE
,
214 N_("Can't require both realm and "
215 "no realm at the same time", ""));
216 return KRB5_ERR_NO_SERVICE
;
220 /* count number of component,
221 * enterprise names only have one component
225 for(p
= name
; *p
; p
++){
228 krb5_set_error_message(context
, KRB5_PARSE_MALFORMED
,
229 N_("trailing \\ in principal name", ""));
230 return KRB5_PARSE_MALFORMED
;
239 comp
= calloc(ncomp
, sizeof(*comp
));
241 krb5_set_error_message(context
, ENOMEM
,
242 N_("malloc: out of memory", ""));
247 p
= start
= q
= s
= strdup(name
);
250 krb5_set_error_message(context
, ENOMEM
,
251 N_("malloc: out of memory", ""));
267 ret
= KRB5_PARSE_MALFORMED
;
268 krb5_set_error_message(context
, ret
,
269 N_("trailing \\ in principal name", ""));
272 }else if(enterprise
&& first_at
) {
275 }else if((c
== '/' && !enterprise
) || c
== '@'){
277 ret
= KRB5_PARSE_MALFORMED
;
278 krb5_set_error_message(context
, ret
,
279 N_("part after realm in principal name", ""));
282 comp
[n
] = malloc(q
- start
+ 1);
283 if (comp
[n
] == NULL
) {
285 krb5_set_error_message(context
, ret
,
286 N_("malloc: out of memory", ""));
289 memcpy(comp
[n
], start
, q
- start
);
290 comp
[n
][q
- start
] = 0;
298 if(got_realm
&& (c
== '/' || c
== '\0')) {
299 ret
= KRB5_PARSE_MALFORMED
;
300 krb5_set_error_message(context
, ret
,
301 N_("part after realm in principal name", ""));
307 if (flags
& KRB5_PRINCIPAL_PARSE_NO_REALM
) {
308 ret
= KRB5_PARSE_MALFORMED
;
309 krb5_set_error_message(context
, ret
,
310 N_("realm found in 'short' principal "
311 "expected to be without one", ""));
314 realm
= malloc(q
- start
+ 1);
317 krb5_set_error_message(context
, ret
,
318 N_("malloc: out of memory", ""));
321 memcpy(realm
, start
, q
- start
);
322 realm
[q
- start
] = 0;
324 if (flags
& KRB5_PRINCIPAL_PARSE_REQUIRE_REALM
) {
325 ret
= KRB5_PARSE_MALFORMED
;
326 krb5_set_error_message(context
, ret
,
327 N_("realm NOT found in principal "
328 "expected to be with one", ""));
330 } else if (flags
& KRB5_PRINCIPAL_PARSE_NO_REALM
) {
333 ret
= krb5_get_default_realm (context
, &realm
);
338 comp
[n
] = malloc(q
- start
+ 1);
339 if (comp
[n
] == NULL
) {
341 krb5_set_error_message(context
, ret
,
342 N_("malloc: out of memory", ""));
345 memcpy(comp
[n
], start
, q
- start
);
346 comp
[n
][q
- start
] = 0;
349 *principal
= malloc(sizeof(**principal
));
350 if (*principal
== NULL
) {
352 krb5_set_error_message(context
, ret
,
353 N_("malloc: out of memory", ""));
357 (*principal
)->name
.name_type
= KRB5_NT_ENTERPRISE_PRINCIPAL
;
359 (*principal
)->name
.name_type
= KRB5_NT_PRINCIPAL
;
360 (*principal
)->name
.name_string
.val
= comp
;
361 princ_num_comp(*principal
) = n
;
362 (*principal
)->realm
= realm
;
376 * Parse a name into a krb5_principal structure
378 * @param context Kerberos 5 context
379 * @param name name to parse into a Kerberos principal
380 * @param principal returned principal, free with krb5_free_principal().
382 * @return An krb5 error code, see krb5_get_error_message().
384 * @ingroup krb5_principal
387 krb5_error_code KRB5_LIB_FUNCTION
388 krb5_parse_name(krb5_context context
,
390 krb5_principal
*principal
)
392 return krb5_parse_name_flags(context
, name
, 0, principal
);
395 static const char quotable_chars
[] = " \n\t\b\\/@";
396 static const char replace_chars
[] = " ntb\\/@";
397 static const char nq_chars
[] = " \\/@";
399 #define add_char(BASE, INDEX, LEN, C) do { if((INDEX) < (LEN)) (BASE)[(INDEX)++] = (C); }while(0);
402 quote_string(const char *s
, char *out
, size_t idx
, size_t len
, int display
)
405 for(p
= s
; *p
&& idx
< len
; p
++){
406 q
= strchr(quotable_chars
, *p
);
408 add_char(out
, idx
, len
, replace_chars
[q
- quotable_chars
]);
410 add_char(out
, idx
, len
, '\\');
411 add_char(out
, idx
, len
, replace_chars
[q
- quotable_chars
]);
413 add_char(out
, idx
, len
, *p
);
421 static krb5_error_code
422 unparse_name_fixed(krb5_context context
,
423 krb5_const_principal principal
,
430 int short_form
= (flags
& KRB5_PRINCIPAL_UNPARSE_SHORT
) != 0;
431 int no_realm
= (flags
& KRB5_PRINCIPAL_UNPARSE_NO_REALM
) != 0;
432 int display
= (flags
& KRB5_PRINCIPAL_UNPARSE_DISPLAY
) != 0;
434 if (!no_realm
&& princ_realm(principal
) == NULL
) {
435 krb5_set_error_message(context
, ERANGE
,
436 N_("Realm missing from principal, "
437 "can't unparse", ""));
441 for(i
= 0; i
< princ_num_comp(principal
); i
++){
443 add_char(name
, idx
, len
, '/');
444 idx
= quote_string(princ_ncomp(principal
, i
), name
, idx
, len
, display
);
446 krb5_set_error_message(context
, ERANGE
,
447 N_("Out of space printing principal", ""));
451 /* add realm if different from default realm */
452 if(short_form
&& !no_realm
) {
455 ret
= krb5_get_default_realm(context
, &r
);
458 if(strcmp(princ_realm(principal
), r
) != 0)
462 if(!short_form
&& !no_realm
) {
463 add_char(name
, idx
, len
, '@');
464 idx
= quote_string(princ_realm(principal
), name
, idx
, len
, display
);
466 krb5_set_error_message(context
, ERANGE
,
467 N_("Out of space printing "
468 "realm of principal", ""));
476 * Unparse the principal name to a fixed buffer
478 * @param context A Kerberos context.
479 * @param principal principal to unparse
480 * @param name buffer to write name to
481 * @param len length of buffer
483 * @return An krb5 error code, see krb5_get_error_message().
485 * @ingroup krb5_principal
488 krb5_error_code KRB5_LIB_FUNCTION
489 krb5_unparse_name_fixed(krb5_context context
,
490 krb5_const_principal principal
,
494 return unparse_name_fixed(context
, principal
, name
, len
, 0);
498 * Unparse the principal name to a fixed buffer. The realm is skipped
499 * if its a default realm.
501 * @param context A Kerberos context.
502 * @param principal principal to unparse
503 * @param name buffer to write name to
504 * @param len length of buffer
506 * @return An krb5 error code, see krb5_get_error_message().
508 * @ingroup krb5_principal
511 krb5_error_code KRB5_LIB_FUNCTION
512 krb5_unparse_name_fixed_short(krb5_context context
,
513 krb5_const_principal principal
,
517 return unparse_name_fixed(context
, principal
, name
, len
,
518 KRB5_PRINCIPAL_UNPARSE_SHORT
);
522 * Unparse the principal name with unparse flags to a fixed buffer.
524 * @param context A Kerberos context.
525 * @param principal principal to unparse
526 * @param flags unparse flags
527 * @param name buffer to write name to
528 * @param len length of buffer
530 * @return An krb5 error code, see krb5_get_error_message().
532 * @ingroup krb5_principal
535 krb5_error_code KRB5_LIB_FUNCTION
536 krb5_unparse_name_fixed_flags(krb5_context context
,
537 krb5_const_principal principal
,
542 return unparse_name_fixed(context
, principal
, name
, len
, flags
);
545 static krb5_error_code
546 unparse_name(krb5_context context
,
547 krb5_const_principal principal
,
551 size_t len
= 0, plen
;
555 if (princ_realm(principal
)) {
556 plen
= strlen(princ_realm(principal
));
558 if(strcspn(princ_realm(principal
), quotable_chars
) == plen
)
564 for(i
= 0; i
< princ_num_comp(principal
); i
++){
565 plen
= strlen(princ_ncomp(principal
, i
));
566 if(strcspn(princ_ncomp(principal
, i
), quotable_chars
) == plen
)
575 krb5_set_error_message(context
, ENOMEM
,
576 N_("malloc: out of memory", ""));
579 ret
= unparse_name_fixed(context
, principal
, *name
, len
, flags
);
588 * Unparse the Kerberos name into a string
590 * @param context Kerberos 5 context
591 * @param principal principal to query
592 * @param name resulting string, free with krb5_xfree()
594 * @return An krb5 error code, see krb5_get_error_message().
596 * @ingroup krb5_principal
599 krb5_error_code KRB5_LIB_FUNCTION
600 krb5_unparse_name(krb5_context context
,
601 krb5_const_principal principal
,
604 return unparse_name(context
, principal
, name
, 0);
608 * Unparse the Kerberos name into a string
610 * @param context Kerberos 5 context
611 * @param principal principal to query
612 * @param flags flag to determine the behavior
613 * @param name resulting string, free with krb5_xfree()
615 * @return An krb5 error code, see krb5_get_error_message().
617 * @ingroup krb5_principal
620 krb5_error_code KRB5_LIB_FUNCTION
621 krb5_unparse_name_flags(krb5_context context
,
622 krb5_const_principal principal
,
626 return unparse_name(context
, principal
, name
, flags
);
630 * Unparse the principal name to a allocated buffer. The realm is
631 * skipped if its a default realm.
633 * @param context A Kerberos context.
634 * @param principal principal to unparse
635 * @param name returned buffer, free with krb5_xfree()
637 * @return An krb5 error code, see krb5_get_error_message().
639 * @ingroup krb5_principal
642 krb5_error_code KRB5_LIB_FUNCTION
643 krb5_unparse_name_short(krb5_context context
,
644 krb5_const_principal principal
,
647 return unparse_name(context
, principal
, name
, KRB5_PRINCIPAL_UNPARSE_SHORT
);
651 * Set a new realm for a principal, and as a side-effect free the
654 * @param context A Kerberos context.
655 * @param principal principal set the realm for
656 * @param realm the new realm to set
658 * @return An krb5 error code, see krb5_get_error_message().
660 * @ingroup krb5_principal
663 krb5_error_code KRB5_LIB_FUNCTION
664 krb5_principal_set_realm(krb5_context context
,
665 krb5_principal principal
,
666 krb5_const_realm realm
)
668 if (princ_realm(principal
))
669 free(princ_realm(principal
));
671 princ_realm(principal
) = strdup(realm
);
672 if (princ_realm(principal
) == NULL
) {
673 krb5_set_error_message(context
, ENOMEM
,
674 N_("malloc: out of memory", ""));
680 #ifndef HEIMDAL_SMALLER
682 * Build a principal using vararg style building
684 * @param context A Kerberos context.
685 * @param principal returned principal
686 * @param rlen length of realm
687 * @param realm realm name
688 * @param ... a list of components ended with NULL.
690 * @return An krb5 error code, see krb5_get_error_message().
692 * @ingroup krb5_principal
695 krb5_error_code KRB5_LIB_FUNCTION
696 krb5_build_principal(krb5_context context
,
697 krb5_principal
*principal
,
699 krb5_const_realm realm
,
705 ret
= krb5_build_principal_va(context
, principal
, rlen
, realm
, ap
);
712 * Build a principal using vararg style building
714 * @param context A Kerberos context.
715 * @param principal returned principal
716 * @param realm realm name
717 * @param ... a list of components ended with NULL.
719 * @return An krb5 error code, see krb5_get_error_message().
721 * @ingroup krb5_principal
724 krb5_error_code KRB5_LIB_FUNCTION
725 krb5_make_principal(krb5_context context
,
726 krb5_principal
*principal
,
727 krb5_const_realm realm
,
734 ret
= krb5_get_default_realm(context
, &r
);
740 ret
= krb5_build_principal_va(context
, principal
, strlen(realm
), realm
, ap
);
747 static krb5_error_code
748 append_component(krb5_context context
, krb5_principal p
,
752 heim_general_string
*tmp
;
753 size_t len
= princ_num_comp(p
);
755 tmp
= realloc(princ_comp(p
), (len
+ 1) * sizeof(*tmp
));
757 krb5_set_error_message(context
, ENOMEM
,
758 N_("malloc: out of memory", ""));
762 princ_ncomp(p
, len
) = malloc(comp_len
+ 1);
763 if (princ_ncomp(p
, len
) == NULL
) {
764 krb5_set_error_message(context
, ENOMEM
,
765 N_("malloc: out of memory", ""));
768 memcpy (princ_ncomp(p
, len
), comp
, comp_len
);
769 princ_ncomp(p
, len
)[comp_len
] = '\0';
775 va_ext_princ(krb5_context context
, krb5_principal p
, va_list ap
)
780 len
= va_arg(ap
, int);
783 s
= va_arg(ap
, const char*);
784 append_component(context
, p
, s
, len
);
789 va_princ(krb5_context context
, krb5_principal p
, va_list ap
)
793 s
= va_arg(ap
, const char*);
796 append_component(context
, p
, s
, strlen(s
));
800 static krb5_error_code
801 build_principal(krb5_context context
,
802 krb5_principal
*principal
,
804 krb5_const_realm realm
,
805 void (*func
)(krb5_context
, krb5_principal
, va_list),
810 p
= calloc(1, sizeof(*p
));
812 krb5_set_error_message(context
, ENOMEM
,
813 N_("malloc: out of memory", ""));
816 princ_type(p
) = KRB5_NT_PRINCIPAL
;
818 princ_realm(p
) = strdup(realm
);
819 if(p
->realm
== NULL
){
821 krb5_set_error_message(context
, ENOMEM
,
822 N_("malloc: out of memory", ""));
826 (*func
)(context
, p
, ap
);
832 krb5_error_code KRB5_LIB_FUNCTION
833 krb5_build_principal_va(krb5_context context
,
834 krb5_principal
*principal
,
836 krb5_const_realm realm
,
839 return build_principal(context
, principal
, rlen
, realm
, va_princ
, ap
);
842 krb5_error_code KRB5_LIB_FUNCTION
843 krb5_build_principal_va_ext(krb5_context context
,
844 krb5_principal
*principal
,
846 krb5_const_realm realm
,
849 return build_principal(context
, principal
, rlen
, realm
, va_ext_princ
, ap
);
853 krb5_error_code KRB5_LIB_FUNCTION
854 krb5_build_principal_ext(krb5_context context
,
855 krb5_principal
*principal
,
857 krb5_const_realm realm
,
863 ret
= krb5_build_principal_va_ext(context
, principal
, rlen
, realm
, ap
);
871 * @param context A Kerberos context.
872 * @param inprinc principal to copy
873 * @param outprinc copied principal, free with krb5_free_principal()
875 * @return An krb5 error code, see krb5_get_error_message().
877 * @ingroup krb5_principal
881 krb5_error_code KRB5_LIB_FUNCTION
882 krb5_copy_principal(krb5_context context
,
883 krb5_const_principal inprinc
,
884 krb5_principal
*outprinc
)
886 krb5_principal p
= malloc(sizeof(*p
));
888 krb5_set_error_message(context
, ENOMEM
,
889 N_("malloc: out of memory", ""));
892 if(copy_Principal(inprinc
, p
)) {
894 krb5_set_error_message(context
, ENOMEM
,
895 N_("malloc: out of memory", ""));
903 * Return TRUE iff princ1 == princ2 (without considering the realm)
905 * @param context Kerberos 5 context
906 * @param princ1 first principal to compare
907 * @param princ2 second principal to compare
909 * @return non zero if equal, 0 if not
911 * @ingroup krb5_principal
912 * @see krb5_principal_compare()
913 * @see krb5_realm_compare()
916 krb5_boolean KRB5_LIB_FUNCTION
917 krb5_principal_compare_any_realm(krb5_context context
,
918 krb5_const_principal princ1
,
919 krb5_const_principal princ2
)
922 if(princ_num_comp(princ1
) != princ_num_comp(princ2
))
924 for(i
= 0; i
< princ_num_comp(princ1
); i
++){
925 if(strcmp(princ_ncomp(princ1
, i
), princ_ncomp(princ2
, i
)) != 0)
931 krb5_boolean KRB5_LIB_FUNCTION
932 _krb5_principal_compare_PrincipalName(krb5_context context
,
933 krb5_const_principal princ1
,
934 PrincipalName
*princ2
)
937 if (princ_num_comp(princ1
) != princ2
->name_string
.len
)
939 for(i
= 0; i
< princ_num_comp(princ1
); i
++){
940 if(strcmp(princ_ncomp(princ1
, i
), princ2
->name_string
.val
[i
]) != 0)
948 * Compares the two principals, including realm of the principals and returns
949 * TRUE if they are the same and FALSE if not.
951 * @param context Kerberos 5 context
952 * @param princ1 first principal to compare
953 * @param princ2 second principal to compare
955 * @ingroup krb5_principal
956 * @see krb5_principal_compare_any_realm()
957 * @see krb5_realm_compare()
961 * return TRUE iff princ1 == princ2
964 krb5_boolean KRB5_LIB_FUNCTION
965 krb5_principal_compare(krb5_context context
,
966 krb5_const_principal princ1
,
967 krb5_const_principal princ2
)
969 if(!krb5_realm_compare(context
, princ1
, princ2
))
971 return krb5_principal_compare_any_realm(context
, princ1
, princ2
);
975 * return TRUE iff realm(princ1) == realm(princ2)
977 * @param context Kerberos 5 context
978 * @param princ1 first principal to compare
979 * @param princ2 second principal to compare
981 * @ingroup krb5_principal
982 * @see krb5_principal_compare_any_realm()
983 * @see krb5_principal_compare()
986 krb5_boolean KRB5_LIB_FUNCTION
987 krb5_realm_compare(krb5_context context
,
988 krb5_const_principal princ1
,
989 krb5_const_principal princ2
)
991 return strcmp(princ_realm(princ1
), princ_realm(princ2
)) == 0;
995 * return TRUE iff princ matches pattern
997 * @ingroup krb5_principal
1000 krb5_boolean KRB5_LIB_FUNCTION
1001 krb5_principal_match(krb5_context context
,
1002 krb5_const_principal princ
,
1003 krb5_const_principal pattern
)
1006 if(princ_num_comp(princ
) != princ_num_comp(pattern
))
1008 if(fnmatch(princ_realm(pattern
), princ_realm(princ
), 0) != 0)
1010 for(i
= 0; i
< princ_num_comp(princ
); i
++){
1011 if(fnmatch(princ_ncomp(pattern
, i
), princ_ncomp(princ
, i
), 0) != 0)
1017 #if defined(KRB4) || !defined(HEIMDAL_SMALLER)
1019 static struct v4_name_convert
{
1022 } default_v4_name_convert
[] = {
1024 { "hprop", "hprop" },
1037 * return the converted instance name of `name' in `realm'.
1038 * look in the configuration file and then in the default set above.
1039 * return NULL if no conversion is appropriate.
1043 get_name_conversion(krb5_context context
, const char *realm
, const char *name
)
1045 struct v4_name_convert
*q
;
1048 p
= krb5_config_get_string(context
, NULL
, "realms", realm
,
1049 "v4_name_convert", "host", name
, NULL
);
1051 p
= krb5_config_get_string(context
, NULL
, "libdefaults",
1052 "v4_name_convert", "host", name
, NULL
);
1056 /* XXX should be possible to override default list */
1057 p
= krb5_config_get_string(context
, NULL
,
1066 p
= krb5_config_get_string(context
, NULL
,
1074 for(q
= default_v4_name_convert
; q
->from
; q
++)
1075 if(strcmp(q
->from
, name
) == 0)
1081 * convert the v4 principal `name.instance@realm' to a v5 principal in `princ'.
1082 * if `resolve', use DNS.
1083 * if `func', use that function for validating the conversion
1086 krb5_error_code KRB5_LIB_FUNCTION
1087 krb5_425_conv_principal_ext2(krb5_context context
,
1089 const char *instance
,
1091 krb5_boolean (*func
)(krb5_context
,
1092 void *, krb5_principal
),
1094 krb5_boolean resolve
,
1095 krb5_principal
*princ
)
1098 krb5_error_code ret
;
1100 char host
[MAXHOSTNAMELEN
];
1101 char local_hostname
[MAXHOSTNAMELEN
];
1103 /* do the following: if the name is found in the
1104 `v4_name_convert:host' part, is assumed to be a `host' type
1105 principal, and the instance is looked up in the
1106 `v4_instance_convert' part. if not found there the name is
1107 (optionally) looked up as a hostname, and if that doesn't yield
1108 anything, the `default_domain' is appended to the instance
1111 if(instance
== NULL
)
1113 if(instance
[0] == 0){
1117 p
= get_name_conversion(context
, realm
, name
);
1121 p
= krb5_config_get_string(context
, NULL
, "realms", realm
,
1122 "v4_instance_convert", instance
, NULL
);
1125 ret
= krb5_make_principal(context
, &pr
, realm
, name
, instance
, NULL
);
1128 if(func
== NULL
|| (*func
)(context
, funcctx
, pr
)){
1132 krb5_free_principal(context
, pr
);
1134 krb5_clear_error_message (context
);
1135 return HEIM_ERR_V4_PRINC_NO_CONV
;
1138 krb5_boolean passed
= FALSE
;
1141 struct rk_dns_reply
*r
;
1143 r
= rk_dns_lookup(instance
, "aaaa");
1145 if (r
->head
&& r
->head
->type
== rk_ns_t_aaaa
) {
1146 inst
= strdup(r
->head
->domain
);
1149 rk_dns_free_data(r
);
1151 r
= rk_dns_lookup(instance
, "a");
1153 if(r
->head
&& r
->head
->type
== rk_ns_t_a
) {
1154 inst
= strdup(r
->head
->domain
);
1157 rk_dns_free_data(r
);
1161 struct addrinfo hints
, *ai
;
1163 memset (&hints
, 0, sizeof(hints
));
1164 hints
.ai_flags
= AI_CANONNAME
;
1165 ret
= getaddrinfo(instance
, NULL
, &hints
, &ai
);
1167 const struct addrinfo
*a
;
1168 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
1169 if (a
->ai_canonname
!= NULL
) {
1170 inst
= strdup (a
->ai_canonname
);
1180 krb5_set_error_message(context
, ENOMEM
,
1181 N_("malloc: out of memory", ""));
1185 ret
= krb5_make_principal(context
, &pr
, realm
, name
, inst
,
1189 if(func
== NULL
|| (*func
)(context
, funcctx
, pr
)){
1193 krb5_free_principal(context
, pr
);
1198 snprintf(host
, sizeof(host
), "%s.%s", instance
, realm
);
1200 ret
= krb5_make_principal(context
, &pr
, realm
, name
, host
, NULL
);
1203 if((*func
)(context
, funcctx
, pr
)){
1207 krb5_free_principal(context
, pr
);
1211 * if the instance is the first component of the local hostname,
1212 * the converted host should be the long hostname.
1216 gethostname (local_hostname
, sizeof(local_hostname
)) == 0 &&
1217 strncmp(instance
, local_hostname
, strlen(instance
)) == 0 &&
1218 local_hostname
[strlen(instance
)] == '.') {
1219 strlcpy(host
, local_hostname
, sizeof(host
));
1224 char **domains
, **d
;
1225 domains
= krb5_config_get_strings(context
, NULL
, "realms", realm
,
1226 "v4_domains", NULL
);
1227 for(d
= domains
; d
&& *d
; d
++){
1228 snprintf(host
, sizeof(host
), "%s.%s", instance
, *d
);
1229 ret
= krb5_make_principal(context
, &pr
, realm
, name
, host
, NULL
);
1231 krb5_config_free_strings(domains
);
1234 if(func
== NULL
|| (*func
)(context
, funcctx
, pr
)){
1236 krb5_config_free_strings(domains
);
1239 krb5_free_principal(context
, pr
);
1241 krb5_config_free_strings(domains
);
1245 p
= krb5_config_get_string(context
, NULL
, "realms", realm
,
1246 "default_domain", NULL
);
1248 /* this should be an error, just faking a name is not good */
1249 krb5_clear_error_message (context
);
1250 return HEIM_ERR_V4_PRINC_NO_CONV
;
1255 snprintf(host
, sizeof(host
), "%s.%s", instance
, p
);
1257 ret
= krb5_make_principal(context
, &pr
, realm
, name
, host
, NULL
);
1260 if(func
== NULL
|| (*func
)(context
, funcctx
, pr
)){
1264 krb5_free_principal(context
, pr
);
1265 krb5_clear_error_message (context
);
1266 return HEIM_ERR_V4_PRINC_NO_CONV
;
1268 p
= krb5_config_get_string(context
, NULL
,
1276 p
= krb5_config_get_string(context
, NULL
,
1285 ret
= krb5_make_principal(context
, &pr
, realm
, name
, instance
, NULL
);
1288 if(func
== NULL
|| (*func
)(context
, funcctx
, pr
)){
1292 krb5_free_principal(context
, pr
);
1293 krb5_clear_error_message (context
);
1294 return HEIM_ERR_V4_PRINC_NO_CONV
;
1299 #ifndef HEIMDAL_SMALLER
1302 check_list(const krb5_config_binding
*l
, const char *name
, const char **out
)
1305 if (l
->type
!= krb5_config_string
)
1307 if(strcmp(name
, l
->u
.string
) == 0) {
1317 name_convert(krb5_context context
, const char *name
, const char *realm
,
1320 const krb5_config_binding
*l
;
1321 l
= krb5_config_get_list (context
,
1328 if(l
&& check_list(l
, name
, out
))
1329 return KRB5_NT_SRV_HST
;
1330 l
= krb5_config_get_list (context
,
1336 if(l
&& check_list(l
, name
, out
))
1337 return KRB5_NT_SRV_HST
;
1338 l
= krb5_config_get_list (context
,
1345 if(l
&& check_list(l
, name
, out
))
1346 return KRB5_NT_UNKNOWN
;
1347 l
= krb5_config_get_list (context
,
1353 if(l
&& check_list(l
, name
, out
))
1354 return KRB5_NT_UNKNOWN
;
1356 /* didn't find it in config file, try built-in list */
1359 struct v4_name_convert
*q
;
1360 for(q
= default_v4_name_convert
; q
->from
; q
++) {
1361 if(strcmp(name
, q
->to
) == 0) {
1363 return KRB5_NT_SRV_HST
;
1372 * convert the v5 principal in `principal' into a v4 corresponding one
1373 * in `name, instance, realm'
1374 * this is limited interface since there's no length given for these
1375 * three parameters. They have to be 40 bytes each (ANAME_SZ).
1378 krb5_error_code KRB5_LIB_FUNCTION
1379 krb5_524_conv_principal(krb5_context context
,
1380 const krb5_principal principal
,
1385 const char *n
, *i
, *r
;
1387 int type
= princ_type(principal
);
1388 const int aname_sz
= 40;
1390 r
= principal
->realm
;
1392 switch(principal
->name
.name_string
.len
){
1394 n
= principal
->name
.name_string
.val
[0];
1398 n
= principal
->name
.name_string
.val
[0];
1399 i
= principal
->name
.name_string
.val
[1];
1402 krb5_set_error_message(context
, KRB5_PARSE_MALFORMED
,
1403 N_("cannot convert a %d "
1404 "component principal", ""),
1405 principal
->name
.name_string
.len
);
1406 return KRB5_PARSE_MALFORMED
;
1411 int t
= name_convert(context
, n
, r
, &tmp
);
1418 if(type
== KRB5_NT_SRV_HST
){
1421 strlcpy (tmpinst
, i
, sizeof(tmpinst
));
1422 p
= strchr(tmpinst
, '.');
1428 if (strlcpy (name
, n
, aname_sz
) >= aname_sz
) {
1429 krb5_set_error_message(context
, KRB5_PARSE_MALFORMED
,
1430 N_("too long name component to convert", ""));
1431 return KRB5_PARSE_MALFORMED
;
1433 if (strlcpy (instance
, i
, aname_sz
) >= aname_sz
) {
1434 krb5_set_error_message(context
, KRB5_PARSE_MALFORMED
,
1435 N_("too long instance component to convert", ""));
1436 return KRB5_PARSE_MALFORMED
;
1438 if (strlcpy (realm
, r
, aname_sz
) >= aname_sz
) {
1439 krb5_set_error_message(context
, KRB5_PARSE_MALFORMED
,
1440 N_("too long realm component to convert", ""));
1441 return KRB5_PARSE_MALFORMED
;
1446 #endif /* !HEIMDAL_SMALLER */
1449 * Create a principal for the service running on hostname. If
1450 * KRB5_NT_SRV_HST is used, the hostname is canonization using DNS (or
1451 * some other service), this is potentially insecure.
1453 * @param context A Kerberos context.
1454 * @param hostname hostname to use
1455 * @param sname Service name to use
1456 * @param type name type of pricipal, use KRB5_NT_SRV_HST or KRB5_NT_UNKNOWN.
1457 * @param ret_princ return principal, free with krb5_free_principal().
1459 * @return An krb5 error code, see krb5_get_error_message().
1461 * @ingroup krb5_principal
1464 krb5_error_code KRB5_LIB_FUNCTION
1465 krb5_sname_to_principal (krb5_context context
,
1466 const char *hostname
,
1469 krb5_principal
*ret_princ
)
1471 krb5_error_code ret
;
1472 char localhost
[MAXHOSTNAMELEN
];
1473 char **realms
, *host
= NULL
;
1475 if(type
!= KRB5_NT_SRV_HST
&& type
!= KRB5_NT_UNKNOWN
) {
1476 krb5_set_error_message(context
, KRB5_SNAME_UNSUPP_NAMETYPE
,
1477 N_("unsupported name type %d", ""),
1479 return KRB5_SNAME_UNSUPP_NAMETYPE
;
1481 if(hostname
== NULL
) {
1482 ret
= gethostname(localhost
, sizeof(localhost
) - 1);
1485 krb5_set_error_message(context
, ret
,
1486 N_("Failed to get local hostname", ""));
1489 localhost
[sizeof(localhost
) - 1] = '\0';
1490 hostname
= localhost
;
1494 if(type
== KRB5_NT_SRV_HST
) {
1495 ret
= krb5_expand_hostname_realms (context
, hostname
,
1502 ret
= krb5_get_host_realm(context
, hostname
, &realms
);
1507 ret
= krb5_make_principal(context
, ret_princ
, realms
[0], sname
,
1511 krb5_free_host_realm(context
, realms
);
1515 static const struct {
1519 { "UNKNOWN", KRB5_NT_UNKNOWN
},
1520 { "PRINCIPAL", KRB5_NT_PRINCIPAL
},
1521 { "SRV_INST", KRB5_NT_SRV_INST
},
1522 { "SRV_HST", KRB5_NT_SRV_HST
},
1523 { "SRV_XHST", KRB5_NT_SRV_XHST
},
1524 { "UID", KRB5_NT_UID
},
1525 { "X500_PRINCIPAL", KRB5_NT_X500_PRINCIPAL
},
1526 { "SMTP_NAME", KRB5_NT_SMTP_NAME
},
1527 { "ENTERPRISE_PRINCIPAL", KRB5_NT_ENTERPRISE_PRINCIPAL
},
1528 { "ENT_PRINCIPAL_AND_ID", KRB5_NT_ENT_PRINCIPAL_AND_ID
},
1529 { "MS_PRINCIPAL", KRB5_NT_MS_PRINCIPAL
},
1530 { "MS_PRINCIPAL_AND_ID", KRB5_NT_MS_PRINCIPAL_AND_ID
},
1535 * Parse nametype string and return a nametype integer
1537 * @ingroup krb5_principal
1541 krb5_parse_nametype(krb5_context context
, const char *str
, int32_t *nametype
)
1545 for(i
= 0; nametypes
[i
].type
; i
++) {
1546 if (strcasecmp(nametypes
[i
].type
, str
) == 0) {
1547 *nametype
= nametypes
[i
].value
;
1551 krb5_set_error_message(context
, KRB5_PARSE_MALFORMED
,
1552 N_("Failed to find name type %s", ""), str
);
1553 return KRB5_PARSE_MALFORMED
;