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);
499 krb5_error_code KRB5_LIB_FUNCTION
500 krb5_unparse_name(krb5_context context,
501 krb5_const_principal principal,
504 return unparse_name(context, principal, name, 0);
507 krb5_error_code KRB5_LIB_FUNCTION
508 krb5_unparse_name_flags(krb5_context context,
509 krb5_const_principal principal,
513 return unparse_name(context, principal, name, flags);
516 krb5_error_code KRB5_LIB_FUNCTION
517 krb5_unparse_name_short(krb5_context context,
518 krb5_const_principal principal,
521 return unparse_name(context, principal, name, KRB5_PRINCIPAL_UNPARSE_SHORT);
524 #if 0 /* not implemented */
526 krb5_error_code KRB5_LIB_FUNCTION
527 krb5_unparse_name_ext(krb5_context context,
528 krb5_const_principal principal,
532 krb5_abortx(context, "unimplemented krb5_unparse_name_ext called");
537 krb5_error_code KRB5_LIB_FUNCTION
538 krb5_principal_set_realm(krb5_context context,
539 krb5_principal principal,
540 krb5_const_realm realm)
542 if (princ_realm(principal))
543 free(princ_realm(principal));
545 princ_realm(principal) = strdup(realm);
546 if (princ_realm(principal) == NULL) {
547 krb5_set_error_message(context, ENOMEM,
548 N_("malloc: out of memory", ""));
555 krb5_error_code KRB5_LIB_FUNCTION
556 krb5_build_principal(krb5_context context,
557 krb5_principal *principal,
559 krb5_const_realm realm,
565 ret = krb5_build_principal_va(context, principal, rlen, realm, ap);
570 static krb5_error_code
571 append_component(krb5_context context, krb5_principal p,
575 heim_general_string *tmp;
576 size_t len = princ_num_comp(p);
578 tmp = realloc(princ_comp(p), (len + 1) * sizeof(*tmp));
580 krb5_set_error_message(context, ENOMEM,
581 N_("malloc: out of memory", ""));
585 princ_ncomp(p, len) = malloc(comp_len + 1);
586 if (princ_ncomp(p, len) == NULL) {
587 krb5_set_error_message(context, ENOMEM,
588 N_("malloc: out of memory", ""));
591 memcpy (princ_ncomp(p, len), comp, comp_len);
592 princ_ncomp(p, len)[comp_len] = '\0';
598 va_ext_princ(krb5_context context, krb5_principal p, va_list ap)
603 len = va_arg(ap, int);
606 s = va_arg(ap, const char*);
607 append_component(context, p, s, len);
612 va_princ(krb5_context context, krb5_principal p, va_list ap)
616 s = va_arg(ap, const char*);
619 append_component(context, p, s, strlen(s));
624 static krb5_error_code
625 build_principal(krb5_context context,
626 krb5_principal *principal,
628 krb5_const_realm realm,
629 void (*func)(krb5_context, krb5_principal, va_list),
634 p = calloc(1, sizeof(*p));
636 krb5_set_error_message(context, ENOMEM,
637 N_("malloc: out of memory", ""));
640 princ_type(p) = KRB5_NT_PRINCIPAL;
642 princ_realm(p) = strdup(realm);
643 if(p->realm == NULL){
645 krb5_set_error_message(context, ENOMEM,
646 N_("malloc: out of memory", ""));
650 (*func)(context, p, ap);
655 krb5_error_code KRB5_LIB_FUNCTION
656 krb5_make_principal(krb5_context context,
657 krb5_principal *principal,
658 krb5_const_realm realm,
665 ret = krb5_get_default_realm(context, &r);
671 ret = krb5_build_principal_va(context, principal, strlen(realm), realm, ap);
678 krb5_error_code KRB5_LIB_FUNCTION
679 krb5_build_principal_va(krb5_context context,
680 krb5_principal *principal,
682 krb5_const_realm realm,
685 return build_principal(context, principal, rlen, realm, va_princ, ap);
688 krb5_error_code KRB5_LIB_FUNCTION
689 krb5_build_principal_va_ext(krb5_context context,
690 krb5_principal *principal,
692 krb5_const_realm realm,
695 return build_principal(context, principal, rlen, realm, va_ext_princ, ap);
699 krb5_error_code KRB5_LIB_FUNCTION
700 krb5_build_principal_ext(krb5_context context,
701 krb5_principal *principal,
703 krb5_const_realm realm,
709 ret = krb5_build_principal_va_ext(context, principal, rlen, realm, ap);
715 krb5_error_code KRB5_LIB_FUNCTION
716 krb5_copy_principal(krb5_context context,
717 krb5_const_principal inprinc,
718 krb5_principal *outprinc)
720 krb5_principal p = malloc(sizeof(*p));
722 krb5_set_error_message(context, ENOMEM,
723 N_("malloc: out of memory", ""));
726 if(copy_Principal(inprinc, p)) {
728 krb5_set_error_message(context, ENOMEM,
729 N_("malloc: out of memory", ""));
737 * return TRUE iff princ1 == princ2 (without considering the realm)
740 krb5_boolean KRB5_LIB_FUNCTION
741 krb5_principal_compare_any_realm(krb5_context context,
742 krb5_const_principal princ1,
743 krb5_const_principal princ2)
746 if(princ_num_comp(princ1) != princ_num_comp(princ2))
748 for(i = 0; i < princ_num_comp(princ1); i++){
749 if(strcmp(princ_ncomp(princ1, i), princ_ncomp(princ2, i)) != 0)
755 krb5_boolean KRB5_LIB_FUNCTION
756 _krb5_principal_compare_PrincipalName(krb5_context context,
757 krb5_const_principal princ1,
758 PrincipalName *princ2)
761 if (princ_num_comp(princ1) != princ2->name_string.len)
763 for(i = 0; i < princ_num_comp(princ1); i++){
764 if(strcmp(princ_ncomp(princ1, i), princ2->name_string.val[i]) != 0)
772 * return TRUE iff princ1 == princ2
775 krb5_boolean KRB5_LIB_FUNCTION
776 krb5_principal_compare(krb5_context context,
777 krb5_const_principal princ1,
778 krb5_const_principal princ2)
780 if(!krb5_realm_compare(context, princ1, princ2))
782 return krb5_principal_compare_any_realm(context, princ1, princ2);
786 * return TRUE iff realm(princ1) == realm(princ2)
789 krb5_boolean KRB5_LIB_FUNCTION
790 krb5_realm_compare(krb5_context context,
791 krb5_const_principal princ1,
792 krb5_const_principal princ2)
794 return strcmp(princ_realm(princ1), princ_realm(princ2)) == 0;
798 * return TRUE iff princ matches pattern
801 krb5_boolean KRB5_LIB_FUNCTION
802 krb5_principal_match(krb5_context context,
803 krb5_const_principal princ,
804 krb5_const_principal pattern)
807 if(princ_num_comp(princ) != princ_num_comp(pattern))
809 if(fnmatch(princ_realm(pattern), princ_realm(princ), 0) != 0)
811 for(i = 0; i < princ_num_comp(princ); i++){
812 if(fnmatch(princ_ncomp(pattern, i), princ_ncomp(princ, i), 0) != 0)
818 #if defined(KRB4) || !defined(HEIMDAL_SMALLER)
820 static struct v4_name_convert {
823 } default_v4_name_convert[] = {
825 { "hprop", "hprop" },
838 * return the converted instance name of `name' in `realm'.
839 * look in the configuration file and then in the default set above.
840 * return NULL if no conversion is appropriate.
844 get_name_conversion(krb5_context context, const char *realm, const char *name)
846 struct v4_name_convert *q;
849 p = krb5_config_get_string(context, NULL, "realms", realm,
850 "v4_name_convert", "host", name, NULL);
852 p = krb5_config_get_string(context, NULL, "libdefaults",
853 "v4_name_convert", "host", name, NULL);
857 /* XXX should be possible to override default list */
858 p = krb5_config_get_string(context, NULL,
867 p = krb5_config_get_string(context, NULL,
875 for(q = default_v4_name_convert; q->from; q++)
876 if(strcmp(q->from, name) == 0)
882 * convert the v4 principal `name.instance@realm' to a v5 principal in `princ'.
883 * if `resolve', use DNS.
884 * if `func', use that function for validating the conversion
887 krb5_error_code KRB5_LIB_FUNCTION
888 krb5_425_conv_principal_ext2(krb5_context context,
890 const char *instance,
892 krb5_boolean (*func)(krb5_context,
893 void *, krb5_principal),
895 krb5_boolean resolve,
896 krb5_principal *princ)
901 char host[MAXHOSTNAMELEN];
902 char local_hostname[MAXHOSTNAMELEN];
904 /* do the following: if the name is found in the
905 `v4_name_convert:host' part, is assumed to be a `host' type
906 principal, and the instance is looked up in the
907 `v4_instance_convert' part. if not found there the name is
908 (optionally) looked up as a hostname, and if that doesn't yield
909 anything, the `default_domain' is appended to the instance
914 if(instance[0] == 0){
918 p = get_name_conversion(context, realm, name);
922 p = krb5_config_get_string(context, NULL, "realms", realm,
923 "v4_instance_convert", instance, NULL);
926 ret = krb5_make_principal(context, &pr, realm, name, instance, NULL);
929 if(func == NULL || (*func)(context, funcctx, pr)){
933 krb5_free_principal(context, pr);
935 krb5_clear_error_message (context);
936 return HEIM_ERR_V4_PRINC_NO_CONV;
939 krb5_boolean passed = FALSE;
942 struct rk_dns_reply *r;
944 r = rk_dns_lookup(instance, "aaaa");
946 if (r->head && r->head->type == rk_ns_t_aaaa) {
947 inst = strdup(r->head->domain);
952 r = rk_dns_lookup(instance, "a");
954 if(r->head && r->head->type == rk_ns_t_a) {
955 inst = strdup(r->head->domain);
962 struct addrinfo hints, *ai;
964 memset (&hints, 0, sizeof(hints));
965 hints.ai_flags = AI_CANONNAME;
966 ret = getaddrinfo(instance, NULL, &hints, &ai);
968 const struct addrinfo *a;
969 for (a = ai; a != NULL; a = a->ai_next) {
970 if (a->ai_canonname != NULL) {
971 inst = strdup (a->ai_canonname);
981 krb5_set_error_message(context, ENOMEM,
982 N_("malloc: out of memory", ""));
986 ret = krb5_make_principal(context, &pr, realm, name, inst,
990 if(func == NULL || (*func)(context, funcctx, pr)){
994 krb5_free_principal(context, pr);
999 snprintf(host, sizeof(host), "%s.%s", instance, realm);
1001 ret = krb5_make_principal(context, &pr, realm, name, host, NULL);
1004 if((*func)(context, funcctx, pr)){
1008 krb5_free_principal(context, pr);
1012 * if the instance is the first component of the local hostname,
1013 * the converted host should be the long hostname.
1017 gethostname (local_hostname, sizeof(local_hostname)) == 0 &&
1018 strncmp(instance, local_hostname, strlen(instance)) == 0 &&
1019 local_hostname[strlen(instance)] == '.') {
1020 strlcpy(host, local_hostname, sizeof(host));
1025 char **domains, **d;
1026 domains = krb5_config_get_strings(context, NULL, "realms", realm,
1027 "v4_domains", NULL);
1028 for(d = domains; d && *d; d++){
1029 snprintf(host, sizeof(host), "%s.%s", instance, *d);
1030 ret = krb5_make_principal(context, &pr, realm, name, host, NULL);
1032 krb5_config_free_strings(domains);
1035 if(func == NULL || (*func)(context, funcctx, pr)){
1037 krb5_config_free_strings(domains);
1040 krb5_free_principal(context, pr);
1042 krb5_config_free_strings(domains);
1046 p = krb5_config_get_string(context, NULL, "realms", realm,
1047 "default_domain", NULL);
1049 /* this should be an error, just faking a name is not good */
1050 krb5_clear_error_message (context);
1051 return HEIM_ERR_V4_PRINC_NO_CONV;
1056 snprintf(host, sizeof(host), "%s.%s", instance, p);
1058 ret = krb5_make_principal(context, &pr, realm, name, host, NULL);
1061 if(func == NULL || (*func)(context, funcctx, pr)){
1065 krb5_free_principal(context, pr);
1066 krb5_clear_error_message (context);
1067 return HEIM_ERR_V4_PRINC_NO_CONV;
1069 p = krb5_config_get_string(context, NULL,
1077 p = krb5_config_get_string(context, NULL,
1086 ret = krb5_make_principal(context, &pr, realm, name, instance, NULL);
1089 if(func == NULL || (*func)(context, funcctx, pr)){
1093 krb5_free_principal(context, pr);
1094 krb5_clear_error_message (context);
1095 return HEIM_ERR_V4_PRINC_NO_CONV;
1100 #ifndef HEIMDAL_SMALLER
1103 check_list(const krb5_config_binding *l, const char *name, const char **out)
1106 if (l->type != krb5_config_string)
1108 if(strcmp(name, l->u.string) == 0) {
1118 name_convert(krb5_context context, const char *name, const char *realm,
1121 const krb5_config_binding *l;
1122 l = krb5_config_get_list (context,
1129 if(l && check_list(l, name, out))
1130 return KRB5_NT_SRV_HST;
1131 l = krb5_config_get_list (context,
1137 if(l && check_list(l, name, out))
1138 return KRB5_NT_SRV_HST;
1139 l = krb5_config_get_list (context,
1146 if(l && check_list(l, name, out))
1147 return KRB5_NT_UNKNOWN;
1148 l = krb5_config_get_list (context,
1154 if(l && check_list(l, name, out))
1155 return KRB5_NT_UNKNOWN;
1157 /* didn't find it in config file, try built-in list */
1160 struct v4_name_convert *q;
1161 for(q = default_v4_name_convert; q->from; q++) {
1162 if(strcmp(name, q->to) == 0) {
1164 return KRB5_NT_SRV_HST;
1173 * convert the v5 principal in `principal' into a v4 corresponding one
1174 * in `name, instance, realm'
1175 * this is limited interface since there's no length given for these
1176 * three parameters. They have to be 40 bytes each (ANAME_SZ).
1179 krb5_error_code KRB5_LIB_FUNCTION
1180 krb5_524_conv_principal(krb5_context context,
1181 const krb5_principal principal,
1186 const char *n, *i, *r;
1188 int type = princ_type(principal);
1189 const int aname_sz = 40;
1191 r = principal->realm;
1193 switch(principal->name.name_string.len){
1195 n = principal->name.name_string.val[0];
1199 n = principal->name.name_string.val[0];
1200 i = principal->name.name_string.val[1];
1203 krb5_set_error_message(context, KRB5_PARSE_MALFORMED,
1204 N_("cannot convert a %d "
1205 "component principal", ""),
1206 principal->name.name_string.len);
1207 return KRB5_PARSE_MALFORMED;
1212 int t = name_convert(context, n, r, &tmp);
1219 if(type == KRB5_NT_SRV_HST){
1222 strlcpy (tmpinst, i, sizeof(tmpinst));
1223 p = strchr(tmpinst, '.');
1229 if (strlcpy (name, n, aname_sz) >= aname_sz) {
1230 krb5_set_error_message(context, KRB5_PARSE_MALFORMED,
1231 N_("too long name component to convert", ""));
1232 return KRB5_PARSE_MALFORMED;
1234 if (strlcpy (instance, i, aname_sz) >= aname_sz) {
1235 krb5_set_error_message(context, KRB5_PARSE_MALFORMED,
1236 N_("too long instance component to convert", ""));
1237 return KRB5_PARSE_MALFORMED;
1239 if (strlcpy (realm, r, aname_sz) >= aname_sz) {
1240 krb5_set_error_message(context, KRB5_PARSE_MALFORMED,
1241 N_("too long realm component to convert", ""));
1242 return KRB5_PARSE_MALFORMED;
1247 #endif /* !HEIMDAL_SMALLER */
1250 * Create a principal for the service running on hostname. If
1251 * KRB5_NT_SRV_HST is used, the hostname is canonization using DNS (or
1252 * some other service), this is potentially insecure.
1254 * @param context A Kerberos context.
1255 * @param hostname hostname to use
1256 * @param sname Service name to use
1257 * @param type name type of pricipal, use KRB5_NT_SRV_HST or KRB5_NT_UNKNOWN.
1258 * @param ret_princ return principal, free with krb5_free_principal().
1260 * @return An krb5 error code, see krb5_get_error_message().
1262 * @ingroup krb5_principal
1265 krb5_error_code KRB5_LIB_FUNCTION
1266 krb5_sname_to_principal (krb5_context context,
1267 const char *hostname,
1270 krb5_principal *ret_princ)
1272 krb5_error_code ret;
1273 char localhost[MAXHOSTNAMELEN];
1274 char **realms, *host = NULL;
1276 if(type != KRB5_NT_SRV_HST && type != KRB5_NT_UNKNOWN) {
1277 krb5_set_error_message(context, KRB5_SNAME_UNSUPP_NAMETYPE,
1278 N_("unsupported name type %d", ""),
1280 return KRB5_SNAME_UNSUPP_NAMETYPE;
1282 if(hostname == NULL) {
1283 ret = gethostname(localhost, sizeof(localhost) - 1);
1286 krb5_set_error_message(context, ret,
1287 N_("Failed to get local hostname", ""));
1290 localhost[sizeof(localhost) - 1] = '\0';
1291 hostname = localhost;
1295 if(type == KRB5_NT_SRV_HST) {
1296 ret = krb5_expand_hostname_realms (context, hostname,
1303 ret = krb5_get_host_realm(context, hostname, &realms);
1308 ret = krb5_make_principal(context, ret_princ, realms[0], sname,
1312 krb5_free_host_realm(context, realms);
1316 static const struct {
1320 { "UNKNOWN", KRB5_NT_UNKNOWN },
1321 { "PRINCIPAL", KRB5_NT_PRINCIPAL },
1322 { "SRV_INST", KRB5_NT_SRV_INST },
1323 { "SRV_HST", KRB5_NT_SRV_HST },
1324 { "SRV_XHST", KRB5_NT_SRV_XHST },
1325 { "UID", KRB5_NT_UID },
1326 { "X500_PRINCIPAL", KRB5_NT_X500_PRINCIPAL },
1327 { "SMTP_NAME", KRB5_NT_SMTP_NAME },
1328 { "ENTERPRISE_PRINCIPAL", KRB5_NT_ENTERPRISE_PRINCIPAL },
1329 { "ENT_PRINCIPAL_AND_ID", KRB5_NT_ENT_PRINCIPAL_AND_ID },
1330 { "MS_PRINCIPAL", KRB5_NT_MS_PRINCIPAL },
1331 { "MS_PRINCIPAL_AND_ID", KRB5_NT_MS_PRINCIPAL_AND_ID },
1336 krb5_parse_nametype(krb5_context context, const char *str, int32_t *nametype)
1340 for(i = 0; nametypes[i].type; i++) {
1341 if (strcasecmp(nametypes[i].type, str) == 0) {
1342 *nametype = nametypes[i].value;
1346 krb5_set_error_message(context, KRB5_PARSE_MALFORMED,
1347 N_("Failed to find name type %s", ""), str);
1348 return KRB5_PARSE_MALFORMED;