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;
109 int KRB5_LIB_FUNCTION
110 krb5_principal_get_type(krb5_context context,
111 krb5_const_principal principal)
113 return princ_type(principal);
116 const char* KRB5_LIB_FUNCTION
117 krb5_principal_get_realm(krb5_context context,
118 krb5_const_principal principal)
120 return princ_realm(principal);
123 const char* KRB5_LIB_FUNCTION
124 krb5_principal_get_comp_string(krb5_context context,
125 krb5_const_principal principal,
126 unsigned int component)
128 if(component >= princ_num_comp(principal))
130 return princ_ncomp(principal, component);
134 * Get number of component is principal.
136 * @param context Kerberos 5 context
137 * @param principal principal to query
139 * @return number of components in string
141 * @ingroup krb5_principal
144 unsigned int KRB5_LIB_FUNCTION
145 krb5_principal_get_num_comp(krb5_context context,
146 krb5_const_principal principal)
148 return princ_num_comp(principal);
151 krb5_error_code KRB5_LIB_FUNCTION
152 krb5_parse_name_flags(krb5_context context,
155 krb5_principal *principal)
158 heim_general_string *comp;
159 heim_general_string realm = NULL;
171 int enterprise = (flags & KRB5_PRINCIPAL_PARSE_ENTERPRISE);
175 #define RFLAGS (KRB5_PRINCIPAL_PARSE_NO_REALM|KRB5_PRINCIPAL_PARSE_REQUIRE_REALM)
177 if ((flags & RFLAGS) == RFLAGS) {
178 krb5_set_error_message(context, KRB5_ERR_NO_SERVICE,
179 N_("Can't require both realm and "
180 "no realm at the same time", ""));
181 return KRB5_ERR_NO_SERVICE;
185 /* count number of component,
186 * enterprise names only have one component
190 for(p = name; *p; p++){
193 krb5_set_error_message(context, KRB5_PARSE_MALFORMED,
194 N_("trailing \\ in principal name", ""));
195 return KRB5_PARSE_MALFORMED;
204 comp = calloc(ncomp, sizeof(*comp));
206 krb5_set_error_message(context, ENOMEM,
207 N_("malloc: out of memory", ""));
212 p = start = q = s = strdup(name);
215 krb5_set_error_message(context, ENOMEM,
216 N_("malloc: out of memory", ""));
232 ret = KRB5_PARSE_MALFORMED;
233 krb5_set_error_message(context, ret,
234 N_("trailing \\ in principal name", ""));
237 }else if(enterprise && first_at) {
240 }else if((c == '/' && !enterprise) || c == '@'){
242 ret = KRB5_PARSE_MALFORMED;
243 krb5_set_error_message(context, ret,
244 N_("part after realm in principal name", ""));
247 comp[n] = malloc(q - start + 1);
248 if (comp[n] == NULL) {
250 krb5_set_error_message(context, ret,
251 N_("malloc: out of memory", ""));
254 memcpy(comp[n], start, q - start);
255 comp[n][q - start] = 0;
263 if(got_realm && (c == '/' || c == '\0')) {
264 ret = KRB5_PARSE_MALFORMED;
265 krb5_set_error_message(context, ret,
266 N_("part after realm in principal name", ""));
272 if (flags & KRB5_PRINCIPAL_PARSE_NO_REALM) {
273 ret = KRB5_PARSE_MALFORMED;
274 krb5_set_error_message(context, ret,
275 N_("realm found in 'short' principal "
276 "expected to be without one", ""));
279 realm = malloc(q - start + 1);
282 krb5_set_error_message(context, ret,
283 N_("malloc: out of memory", ""));
286 memcpy(realm, start, q - start);
287 realm[q - start] = 0;
289 if (flags & KRB5_PRINCIPAL_PARSE_REQUIRE_REALM) {
290 ret = KRB5_PARSE_MALFORMED;
291 krb5_set_error_message(context, ret,
292 N_("realm NOT found in principal "
293 "expected to be with one", ""));
295 } else if (flags & KRB5_PRINCIPAL_PARSE_NO_REALM) {
298 ret = krb5_get_default_realm (context, &realm);
303 comp[n] = malloc(q - start + 1);
304 if (comp[n] == NULL) {
306 krb5_set_error_message(context, ret,
307 N_("malloc: out of memory", ""));
310 memcpy(comp[n], start, q - start);
311 comp[n][q - start] = 0;
314 *principal = malloc(sizeof(**principal));
315 if (*principal == NULL) {
317 krb5_set_error_message(context, ret,
318 N_("malloc: out of memory", ""));
322 (*principal)->name.name_type = KRB5_NT_ENTERPRISE_PRINCIPAL;
324 (*principal)->name.name_type = KRB5_NT_PRINCIPAL;
325 (*principal)->name.name_string.val = comp;
326 princ_num_comp(*principal) = n;
327 (*principal)->realm = realm;
340 krb5_error_code KRB5_LIB_FUNCTION
341 krb5_parse_name(krb5_context context,
343 krb5_principal *principal)
345 return krb5_parse_name_flags(context, name, 0, principal);
348 static const char quotable_chars[] = " \n\t\b\\/@";
349 static const char replace_chars[] = " ntb\\/@";
350 static const char nq_chars[] = " \\/@";
352 #define add_char(BASE, INDEX, LEN, C) do { if((INDEX) < (LEN)) (BASE)[(INDEX)++] = (C); }while(0);
355 quote_string(const char *s, char *out, size_t idx, size_t len, int display)
358 for(p = s; *p && idx < len; p++){
359 q = strchr(quotable_chars, *p);
361 add_char(out, idx, len, replace_chars[q - quotable_chars]);
363 add_char(out, idx, len, '\\');
364 add_char(out, idx, len, replace_chars[q - quotable_chars]);
366 add_char(out, idx, len, *p);
374 static krb5_error_code
375 unparse_name_fixed(krb5_context context,
376 krb5_const_principal principal,
383 int short_form = (flags & KRB5_PRINCIPAL_UNPARSE_SHORT) != 0;
384 int no_realm = (flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) != 0;
385 int display = (flags & KRB5_PRINCIPAL_UNPARSE_DISPLAY) != 0;
387 if (!no_realm && princ_realm(principal) == NULL) {
388 krb5_set_error_message(context, ERANGE,
389 N_("Realm missing from principal, "
390 "can't unparse", ""));
394 for(i = 0; i < princ_num_comp(principal); i++){
396 add_char(name, idx, len, '/');
397 idx = quote_string(princ_ncomp(principal, i), name, idx, len, display);
399 krb5_set_error_message(context, ERANGE,
400 N_("Out of space printing principal", ""));
404 /* add realm if different from default realm */
405 if(short_form && !no_realm) {
408 ret = krb5_get_default_realm(context, &r);
411 if(strcmp(princ_realm(principal), r) != 0)
415 if(!short_form && !no_realm) {
416 add_char(name, idx, len, '@');
417 idx = quote_string(princ_realm(principal), name, idx, len, display);
419 krb5_set_error_message(context, ERANGE,
420 N_("Out of space printing "
421 "realm of principal", ""));
428 krb5_error_code KRB5_LIB_FUNCTION
429 krb5_unparse_name_fixed(krb5_context context,
430 krb5_const_principal principal,
434 return unparse_name_fixed(context, principal, name, len, 0);
437 krb5_error_code KRB5_LIB_FUNCTION
438 krb5_unparse_name_fixed_short(krb5_context context,
439 krb5_const_principal principal,
443 return unparse_name_fixed(context, principal, name, len,
444 KRB5_PRINCIPAL_UNPARSE_SHORT);
447 krb5_error_code KRB5_LIB_FUNCTION
448 krb5_unparse_name_fixed_flags(krb5_context context,
449 krb5_const_principal principal,
454 return unparse_name_fixed(context, principal, name, len, flags);
457 static krb5_error_code
458 unparse_name(krb5_context context,
459 krb5_const_principal principal,
463 size_t len = 0, plen;
467 if (princ_realm(principal)) {
468 plen = strlen(princ_realm(principal));
470 if(strcspn(princ_realm(principal), quotable_chars) == plen)
476 for(i = 0; i < princ_num_comp(principal); i++){
477 plen = strlen(princ_ncomp(principal, i));
478 if(strcspn(princ_ncomp(principal, i), quotable_chars) == plen)
487 krb5_set_error_message(context, ENOMEM,
488 N_("malloc: out of memory", ""));
491 ret = unparse_name_fixed(context, principal, *name, len, flags);
500 * Unparse the Kerberos name into a string
502 * @param context Kerberos 5 context
503 * @param principal principal to query
504 * @param name resulting string, free with krb5_xfree()
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(krb5_context context,
513 krb5_const_principal principal,
516 return unparse_name(context, principal, name, 0);
520 * Unparse the Kerberos name into a string
522 * @param context Kerberos 5 context
523 * @param principal principal to query
524 * @param flags flag to determine the behavior
525 * @param name resulting string, free with krb5_xfree()
527 * @return An krb5 error code, see krb5_get_error_message().
529 * @ingroup krb5_principal
532 krb5_error_code KRB5_LIB_FUNCTION
533 krb5_unparse_name_flags(krb5_context context,
534 krb5_const_principal principal,
538 return unparse_name(context, principal, name, flags);
541 krb5_error_code KRB5_LIB_FUNCTION
542 krb5_unparse_name_short(krb5_context context,
543 krb5_const_principal principal,
546 return unparse_name(context, principal, name, KRB5_PRINCIPAL_UNPARSE_SHORT);
549 #if 0 /* not implemented */
551 krb5_error_code KRB5_LIB_FUNCTION
552 krb5_unparse_name_ext(krb5_context context,
553 krb5_const_principal principal,
557 krb5_abortx(context, "unimplemented krb5_unparse_name_ext called");
562 krb5_error_code KRB5_LIB_FUNCTION
563 krb5_principal_set_realm(krb5_context context,
564 krb5_principal principal,
565 krb5_const_realm realm)
567 if (princ_realm(principal))
568 free(princ_realm(principal));
570 princ_realm(principal) = strdup(realm);
571 if (princ_realm(principal) == NULL) {
572 krb5_set_error_message(context, ENOMEM,
573 N_("malloc: out of memory", ""));
580 krb5_error_code KRB5_LIB_FUNCTION
581 krb5_build_principal(krb5_context context,
582 krb5_principal *principal,
584 krb5_const_realm realm,
590 ret = krb5_build_principal_va(context, principal, rlen, realm, ap);
595 static krb5_error_code
596 append_component(krb5_context context, krb5_principal p,
600 heim_general_string *tmp;
601 size_t len = princ_num_comp(p);
603 tmp = realloc(princ_comp(p), (len + 1) * sizeof(*tmp));
605 krb5_set_error_message(context, ENOMEM,
606 N_("malloc: out of memory", ""));
610 princ_ncomp(p, len) = malloc(comp_len + 1);
611 if (princ_ncomp(p, len) == NULL) {
612 krb5_set_error_message(context, ENOMEM,
613 N_("malloc: out of memory", ""));
616 memcpy (princ_ncomp(p, len), comp, comp_len);
617 princ_ncomp(p, len)[comp_len] = '\0';
623 va_ext_princ(krb5_context context, krb5_principal p, va_list ap)
628 len = va_arg(ap, int);
631 s = va_arg(ap, const char*);
632 append_component(context, p, s, len);
637 va_princ(krb5_context context, krb5_principal p, va_list ap)
641 s = va_arg(ap, const char*);
644 append_component(context, p, s, strlen(s));
648 static krb5_error_code
649 build_principal(krb5_context context,
650 krb5_principal *principal,
652 krb5_const_realm realm,
653 void (*func)(krb5_context, krb5_principal, va_list),
658 p = calloc(1, sizeof(*p));
660 krb5_set_error_message(context, ENOMEM,
661 N_("malloc: out of memory", ""));
664 princ_type(p) = KRB5_NT_PRINCIPAL;
666 princ_realm(p) = strdup(realm);
667 if(p->realm == NULL){
669 krb5_set_error_message(context, ENOMEM,
670 N_("malloc: out of memory", ""));
674 (*func)(context, p, ap);
679 krb5_error_code KRB5_LIB_FUNCTION
680 krb5_make_principal(krb5_context context,
681 krb5_principal *principal,
682 krb5_const_realm realm,
689 ret = krb5_get_default_realm(context, &r);
695 ret = krb5_build_principal_va(context, principal, strlen(realm), realm, ap);
702 krb5_error_code KRB5_LIB_FUNCTION
703 krb5_build_principal_va(krb5_context context,
704 krb5_principal *principal,
706 krb5_const_realm realm,
709 return build_principal(context, principal, rlen, realm, va_princ, ap);
712 krb5_error_code KRB5_LIB_FUNCTION
713 krb5_build_principal_va_ext(krb5_context context,
714 krb5_principal *principal,
716 krb5_const_realm realm,
719 return build_principal(context, principal, rlen, realm, va_ext_princ, ap);
723 krb5_error_code KRB5_LIB_FUNCTION
724 krb5_build_principal_ext(krb5_context context,
725 krb5_principal *principal,
727 krb5_const_realm realm,
733 ret = krb5_build_principal_va_ext(context, principal, rlen, realm, ap);
739 krb5_error_code KRB5_LIB_FUNCTION
740 krb5_copy_principal(krb5_context context,
741 krb5_const_principal inprinc,
742 krb5_principal *outprinc)
744 krb5_principal p = malloc(sizeof(*p));
746 krb5_set_error_message(context, ENOMEM,
747 N_("malloc: out of memory", ""));
750 if(copy_Principal(inprinc, p)) {
752 krb5_set_error_message(context, ENOMEM,
753 N_("malloc: out of memory", ""));
761 * Return TRUE iff princ1 == princ2 (without considering the realm)
763 * @param context Kerberos 5 context
764 * @param princ1 first principal to compare
765 * @param princ2 second principal to compare
767 * @return non zero if equal, 0 if not
769 * @ingroup krb5_principal
772 krb5_boolean KRB5_LIB_FUNCTION
773 krb5_principal_compare_any_realm(krb5_context context,
774 krb5_const_principal princ1,
775 krb5_const_principal princ2)
778 if(princ_num_comp(princ1) != princ_num_comp(princ2))
780 for(i = 0; i < princ_num_comp(princ1); i++){
781 if(strcmp(princ_ncomp(princ1, i), princ_ncomp(princ2, i)) != 0)
787 krb5_boolean KRB5_LIB_FUNCTION
788 _krb5_principal_compare_PrincipalName(krb5_context context,
789 krb5_const_principal princ1,
790 PrincipalName *princ2)
793 if (princ_num_comp(princ1) != princ2->name_string.len)
795 for(i = 0; i < princ_num_comp(princ1); i++){
796 if(strcmp(princ_ncomp(princ1, i), princ2->name_string.val[i]) != 0)
804 * return TRUE iff princ1 == princ2
807 krb5_boolean KRB5_LIB_FUNCTION
808 krb5_principal_compare(krb5_context context,
809 krb5_const_principal princ1,
810 krb5_const_principal princ2)
812 if(!krb5_realm_compare(context, princ1, princ2))
814 return krb5_principal_compare_any_realm(context, princ1, princ2);
818 * return TRUE iff realm(princ1) == realm(princ2)
821 krb5_boolean KRB5_LIB_FUNCTION
822 krb5_realm_compare(krb5_context context,
823 krb5_const_principal princ1,
824 krb5_const_principal princ2)
826 return strcmp(princ_realm(princ1), princ_realm(princ2)) == 0;
830 * return TRUE iff princ matches pattern
833 krb5_boolean KRB5_LIB_FUNCTION
834 krb5_principal_match(krb5_context context,
835 krb5_const_principal princ,
836 krb5_const_principal pattern)
839 if(princ_num_comp(princ) != princ_num_comp(pattern))
841 if(fnmatch(princ_realm(pattern), princ_realm(princ), 0) != 0)
843 for(i = 0; i < princ_num_comp(princ); i++){
844 if(fnmatch(princ_ncomp(pattern, i), princ_ncomp(princ, i), 0) != 0)
850 #if defined(KRB4) || !defined(HEIMDAL_SMALLER)
852 static struct v4_name_convert {
855 } default_v4_name_convert[] = {
857 { "hprop", "hprop" },
870 * return the converted instance name of `name' in `realm'.
871 * look in the configuration file and then in the default set above.
872 * return NULL if no conversion is appropriate.
876 get_name_conversion(krb5_context context, const char *realm, const char *name)
878 struct v4_name_convert *q;
881 p = krb5_config_get_string(context, NULL, "realms", realm,
882 "v4_name_convert", "host", name, NULL);
884 p = krb5_config_get_string(context, NULL, "libdefaults",
885 "v4_name_convert", "host", name, NULL);
889 /* XXX should be possible to override default list */
890 p = krb5_config_get_string(context, NULL,
899 p = krb5_config_get_string(context, NULL,
907 for(q = default_v4_name_convert; q->from; q++)
908 if(strcmp(q->from, name) == 0)
914 * convert the v4 principal `name.instance@realm' to a v5 principal in `princ'.
915 * if `resolve', use DNS.
916 * if `func', use that function for validating the conversion
919 krb5_error_code KRB5_LIB_FUNCTION
920 krb5_425_conv_principal_ext2(krb5_context context,
922 const char *instance,
924 krb5_boolean (*func)(krb5_context,
925 void *, krb5_principal),
927 krb5_boolean resolve,
928 krb5_principal *princ)
933 char host[MAXHOSTNAMELEN];
934 char local_hostname[MAXHOSTNAMELEN];
936 /* do the following: if the name is found in the
937 `v4_name_convert:host' part, is assumed to be a `host' type
938 principal, and the instance is looked up in the
939 `v4_instance_convert' part. if not found there the name is
940 (optionally) looked up as a hostname, and if that doesn't yield
941 anything, the `default_domain' is appended to the instance
946 if(instance[0] == 0){
950 p = get_name_conversion(context, realm, name);
954 p = krb5_config_get_string(context, NULL, "realms", realm,
955 "v4_instance_convert", instance, NULL);
958 ret = krb5_make_principal(context, &pr, realm, name, instance, NULL);
961 if(func == NULL || (*func)(context, funcctx, pr)){
965 krb5_free_principal(context, pr);
967 krb5_clear_error_message (context);
968 return HEIM_ERR_V4_PRINC_NO_CONV;
971 krb5_boolean passed = FALSE;
974 struct rk_dns_reply *r;
976 r = rk_dns_lookup(instance, "aaaa");
978 if (r->head && r->head->type == rk_ns_t_aaaa) {
979 inst = strdup(r->head->domain);
984 r = rk_dns_lookup(instance, "a");
986 if(r->head && r->head->type == rk_ns_t_a) {
987 inst = strdup(r->head->domain);
994 struct addrinfo hints, *ai;
996 memset (&hints, 0, sizeof(hints));
997 hints.ai_flags = AI_CANONNAME;
998 ret = getaddrinfo(instance, NULL, &hints, &ai);
1000 const struct addrinfo *a;
1001 for (a = ai; a != NULL; a = a->ai_next) {
1002 if (a->ai_canonname != NULL) {
1003 inst = strdup (a->ai_canonname);
1013 krb5_set_error_message(context, ENOMEM,
1014 N_("malloc: out of memory", ""));
1018 ret = krb5_make_principal(context, &pr, realm, name, inst,
1022 if(func == NULL || (*func)(context, funcctx, pr)){
1026 krb5_free_principal(context, pr);
1031 snprintf(host, sizeof(host), "%s.%s", instance, realm);
1033 ret = krb5_make_principal(context, &pr, realm, name, host, NULL);
1036 if((*func)(context, funcctx, pr)){
1040 krb5_free_principal(context, pr);
1044 * if the instance is the first component of the local hostname,
1045 * the converted host should be the long hostname.
1049 gethostname (local_hostname, sizeof(local_hostname)) == 0 &&
1050 strncmp(instance, local_hostname, strlen(instance)) == 0 &&
1051 local_hostname[strlen(instance)] == '.') {
1052 strlcpy(host, local_hostname, sizeof(host));
1057 char **domains, **d;
1058 domains = krb5_config_get_strings(context, NULL, "realms", realm,
1059 "v4_domains", NULL);
1060 for(d = domains; d && *d; d++){
1061 snprintf(host, sizeof(host), "%s.%s", instance, *d);
1062 ret = krb5_make_principal(context, &pr, realm, name, host, NULL);
1064 krb5_config_free_strings(domains);
1067 if(func == NULL || (*func)(context, funcctx, pr)){
1069 krb5_config_free_strings(domains);
1072 krb5_free_principal(context, pr);
1074 krb5_config_free_strings(domains);
1078 p = krb5_config_get_string(context, NULL, "realms", realm,
1079 "default_domain", NULL);
1081 /* this should be an error, just faking a name is not good */
1082 krb5_clear_error_message (context);
1083 return HEIM_ERR_V4_PRINC_NO_CONV;
1088 snprintf(host, sizeof(host), "%s.%s", instance, p);
1090 ret = krb5_make_principal(context, &pr, realm, name, host, NULL);
1093 if(func == NULL || (*func)(context, funcctx, pr)){
1097 krb5_free_principal(context, pr);
1098 krb5_clear_error_message (context);
1099 return HEIM_ERR_V4_PRINC_NO_CONV;
1101 p = krb5_config_get_string(context, NULL,
1109 p = krb5_config_get_string(context, NULL,
1118 ret = krb5_make_principal(context, &pr, realm, name, instance, NULL);
1121 if(func == NULL || (*func)(context, funcctx, pr)){
1125 krb5_free_principal(context, pr);
1126 krb5_clear_error_message (context);
1127 return HEIM_ERR_V4_PRINC_NO_CONV;
1132 #ifndef HEIMDAL_SMALLER
1135 check_list(const krb5_config_binding *l, const char *name, const char **out)
1138 if (l->type != krb5_config_string)
1140 if(strcmp(name, l->u.string) == 0) {
1150 name_convert(krb5_context context, const char *name, const char *realm,
1153 const krb5_config_binding *l;
1154 l = krb5_config_get_list (context,
1161 if(l && check_list(l, name, out))
1162 return KRB5_NT_SRV_HST;
1163 l = krb5_config_get_list (context,
1169 if(l && check_list(l, name, out))
1170 return KRB5_NT_SRV_HST;
1171 l = krb5_config_get_list (context,
1178 if(l && check_list(l, name, out))
1179 return KRB5_NT_UNKNOWN;
1180 l = krb5_config_get_list (context,
1186 if(l && check_list(l, name, out))
1187 return KRB5_NT_UNKNOWN;
1189 /* didn't find it in config file, try built-in list */
1192 struct v4_name_convert *q;
1193 for(q = default_v4_name_convert; q->from; q++) {
1194 if(strcmp(name, q->to) == 0) {
1196 return KRB5_NT_SRV_HST;
1205 * convert the v5 principal in `principal' into a v4 corresponding one
1206 * in `name, instance, realm'
1207 * this is limited interface since there's no length given for these
1208 * three parameters. They have to be 40 bytes each (ANAME_SZ).
1211 krb5_error_code KRB5_LIB_FUNCTION
1212 krb5_524_conv_principal(krb5_context context,
1213 const krb5_principal principal,
1218 const char *n, *i, *r;
1220 int type = princ_type(principal);
1221 const int aname_sz = 40;
1223 r = principal->realm;
1225 switch(principal->name.name_string.len){
1227 n = principal->name.name_string.val[0];
1231 n = principal->name.name_string.val[0];
1232 i = principal->name.name_string.val[1];
1235 krb5_set_error_message(context, KRB5_PARSE_MALFORMED,
1236 N_("cannot convert a %d "
1237 "component principal", ""),
1238 principal->name.name_string.len);
1239 return KRB5_PARSE_MALFORMED;
1244 int t = name_convert(context, n, r, &tmp);
1251 if(type == KRB5_NT_SRV_HST){
1254 strlcpy (tmpinst, i, sizeof(tmpinst));
1255 p = strchr(tmpinst, '.');
1261 if (strlcpy (name, n, aname_sz) >= aname_sz) {
1262 krb5_set_error_message(context, KRB5_PARSE_MALFORMED,
1263 N_("too long name component to convert", ""));
1264 return KRB5_PARSE_MALFORMED;
1266 if (strlcpy (instance, i, aname_sz) >= aname_sz) {
1267 krb5_set_error_message(context, KRB5_PARSE_MALFORMED,
1268 N_("too long instance component to convert", ""));
1269 return KRB5_PARSE_MALFORMED;
1271 if (strlcpy (realm, r, aname_sz) >= aname_sz) {
1272 krb5_set_error_message(context, KRB5_PARSE_MALFORMED,
1273 N_("too long realm component to convert", ""));
1274 return KRB5_PARSE_MALFORMED;
1279 #endif /* !HEIMDAL_SMALLER */
1282 * Create a principal for the service running on hostname. If
1283 * KRB5_NT_SRV_HST is used, the hostname is canonization using DNS (or
1284 * some other service), this is potentially insecure.
1286 * @param context A Kerberos context.
1287 * @param hostname hostname to use
1288 * @param sname Service name to use
1289 * @param type name type of pricipal, use KRB5_NT_SRV_HST or KRB5_NT_UNKNOWN.
1290 * @param ret_princ return principal, free with krb5_free_principal().
1292 * @return An krb5 error code, see krb5_get_error_message().
1294 * @ingroup krb5_principal
1297 krb5_error_code KRB5_LIB_FUNCTION
1298 krb5_sname_to_principal (krb5_context context,
1299 const char *hostname,
1302 krb5_principal *ret_princ)
1304 krb5_error_code ret;
1305 char localhost[MAXHOSTNAMELEN];
1306 char **realms, *host = NULL;
1308 if(type != KRB5_NT_SRV_HST && type != KRB5_NT_UNKNOWN) {
1309 krb5_set_error_message(context, KRB5_SNAME_UNSUPP_NAMETYPE,
1310 N_("unsupported name type %d", ""),
1312 return KRB5_SNAME_UNSUPP_NAMETYPE;
1314 if(hostname == NULL) {
1315 ret = gethostname(localhost, sizeof(localhost) - 1);
1318 krb5_set_error_message(context, ret,
1319 N_("Failed to get local hostname", ""));
1322 localhost[sizeof(localhost) - 1] = '\0';
1323 hostname = localhost;
1327 if(type == KRB5_NT_SRV_HST) {
1328 ret = krb5_expand_hostname_realms (context, hostname,
1335 ret = krb5_get_host_realm(context, hostname, &realms);
1340 ret = krb5_make_principal(context, ret_princ, realms[0], sname,
1344 krb5_free_host_realm(context, realms);
1348 static const struct {
1352 { "UNKNOWN", KRB5_NT_UNKNOWN },
1353 { "PRINCIPAL", KRB5_NT_PRINCIPAL },
1354 { "SRV_INST", KRB5_NT_SRV_INST },
1355 { "SRV_HST", KRB5_NT_SRV_HST },
1356 { "SRV_XHST", KRB5_NT_SRV_XHST },
1357 { "UID", KRB5_NT_UID },
1358 { "X500_PRINCIPAL", KRB5_NT_X500_PRINCIPAL },
1359 { "SMTP_NAME", KRB5_NT_SMTP_NAME },
1360 { "ENTERPRISE_PRINCIPAL", KRB5_NT_ENTERPRISE_PRINCIPAL },
1361 { "ENT_PRINCIPAL_AND_ID", KRB5_NT_ENT_PRINCIPAL_AND_ID },
1362 { "MS_PRINCIPAL", KRB5_NT_MS_PRINCIPAL },
1363 { "MS_PRINCIPAL_AND_ID", KRB5_NT_MS_PRINCIPAL_AND_ID },
1368 krb5_parse_nametype(krb5_context context, const char *str, int32_t *nametype)
1372 for(i = 0; nametypes[i].type; i++) {
1373 if (strcasecmp(nametypes[i].type, str) == 0) {
1374 *nametype = nametypes[i].value;
1378 krb5_set_error_message(context, KRB5_PARSE_MALFORMED,
1379 N_("Failed to find name type %s", ""), str);
1380 return KRB5_PARSE_MALFORMED;