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
34 #include "krb5_locl.h"
35 #ifdef HAVE_RES_SEARCH
38 #ifdef HAVE_ARPA_NAMESER_H
39 #include <arpa/nameser.h>
44 __RCSID("$Heimdal: principal.c 21741 2007-07-31 16:00:37Z lha $"
47 #define princ_num_comp(P) ((P)->name.name_string.len)
48 #define princ_type(P) ((P)->name.name_type)
49 #define princ_comp(P) ((P)->name.name_string.val)
50 #define princ_ncomp(P, N) ((P)->name.name_string.val[(N)])
51 #define princ_realm(P) ((P)->realm)
53 void KRB5_LIB_FUNCTION
54 krb5_free_principal(krb5_context context
,
63 void KRB5_LIB_FUNCTION
64 krb5_principal_set_type(krb5_context context
,
65 krb5_principal principal
,
68 princ_type(principal
) = type
;
72 krb5_principal_get_type(krb5_context context
,
73 krb5_const_principal principal
)
75 return princ_type(principal
);
78 const char* KRB5_LIB_FUNCTION
79 krb5_principal_get_realm(krb5_context context
,
80 krb5_const_principal principal
)
82 return princ_realm(principal
);
85 const char* KRB5_LIB_FUNCTION
86 krb5_principal_get_comp_string(krb5_context context
,
87 krb5_const_principal principal
,
88 unsigned int component
)
90 if(component
>= princ_num_comp(principal
))
92 return princ_ncomp(principal
, component
);
95 krb5_error_code KRB5_LIB_FUNCTION
96 krb5_parse_name_flags(krb5_context context
,
99 krb5_principal
*principal
)
102 heim_general_string
*comp
;
103 heim_general_string realm
= NULL
;
115 int enterprise
= (flags
& KRB5_PRINCIPAL_PARSE_ENTERPRISE
);
119 #define RFLAGS (KRB5_PRINCIPAL_PARSE_NO_REALM|KRB5_PRINCIPAL_PARSE_MUST_REALM)
121 if ((flags
& RFLAGS
) == RFLAGS
) {
122 krb5_set_error_string(context
, "Can't require both realm and "
123 "no realm at the same time");
124 return KRB5_ERR_NO_SERVICE
;
128 /* count number of component,
129 * enterprise names only have one component
133 for(p
= name
; *p
; p
++){
136 krb5_set_error_string (context
,
137 "trailing \\ in principal name");
138 return KRB5_PARSE_MALFORMED
;
147 comp
= calloc(ncomp
, sizeof(*comp
));
149 krb5_set_error_string (context
, "malloc: out of memory");
154 p
= start
= q
= s
= strdup(name
);
157 krb5_set_error_string (context
, "malloc: out of memory");
173 krb5_set_error_string (context
,
174 "trailing \\ in principal name");
175 ret
= KRB5_PARSE_MALFORMED
;
178 }else if(enterprise
&& first_at
) {
181 }else if((c
== '/' && !enterprise
) || c
== '@'){
183 krb5_set_error_string (context
,
184 "part after realm in principal name");
185 ret
= KRB5_PARSE_MALFORMED
;
188 comp
[n
] = malloc(q
- start
+ 1);
189 if (comp
[n
] == NULL
) {
190 krb5_set_error_string (context
, "malloc: out of memory");
194 memcpy(comp
[n
], start
, q
- start
);
195 comp
[n
][q
- start
] = 0;
203 if(got_realm
&& (c
== ':' || c
== '/' || c
== '\0')) {
204 krb5_set_error_string (context
,
205 "part after realm in principal name");
206 ret
= KRB5_PARSE_MALFORMED
;
212 if (flags
& KRB5_PRINCIPAL_PARSE_NO_REALM
) {
213 krb5_set_error_string (context
, "realm found in 'short' principal "
214 "expected to be without one");
215 ret
= KRB5_PARSE_MALFORMED
;
218 realm
= malloc(q
- start
+ 1);
220 krb5_set_error_string (context
, "malloc: out of memory");
224 memcpy(realm
, start
, q
- start
);
225 realm
[q
- start
] = 0;
227 if (flags
& KRB5_PRINCIPAL_PARSE_MUST_REALM
) {
228 krb5_set_error_string (context
, "realm NOT found in principal "
229 "expected to be with one");
230 ret
= KRB5_PARSE_MALFORMED
;
232 } else if (flags
& KRB5_PRINCIPAL_PARSE_NO_REALM
) {
235 ret
= krb5_get_default_realm (context
, &realm
);
240 comp
[n
] = malloc(q
- start
+ 1);
241 if (comp
[n
] == NULL
) {
242 krb5_set_error_string (context
, "malloc: out of memory");
246 memcpy(comp
[n
], start
, q
- start
);
247 comp
[n
][q
- start
] = 0;
250 *principal
= malloc(sizeof(**principal
));
251 if (*principal
== NULL
) {
252 krb5_set_error_string (context
, "malloc: out of memory");
257 (*principal
)->name
.name_type
= KRB5_NT_ENTERPRISE_PRINCIPAL
;
259 (*principal
)->name
.name_type
= KRB5_NT_PRINCIPAL
;
260 (*principal
)->name
.name_string
.val
= comp
;
261 princ_num_comp(*principal
) = n
;
262 (*principal
)->realm
= realm
;
275 krb5_error_code KRB5_LIB_FUNCTION
276 krb5_parse_name(krb5_context context
,
278 krb5_principal
*principal
)
280 return krb5_parse_name_flags(context
, name
, 0, principal
);
283 static const char quotable_chars
[] = " \n\t\b\\/@";
284 static const char replace_chars
[] = " ntb\\/@";
285 static const char nq_chars
[] = " \\/@";
287 #define add_char(BASE, INDEX, LEN, C) do { if((INDEX) < (LEN)) (BASE)[(INDEX)++] = (C); }while(0);
290 quote_string(const char *s
, char *out
, size_t idx
, size_t len
, int display
)
293 for(p
= s
; *p
&& idx
< len
; p
++){
294 q
= strchr(quotable_chars
, *p
);
296 add_char(out
, idx
, len
, replace_chars
[q
- quotable_chars
]);
298 add_char(out
, idx
, len
, '\\');
299 add_char(out
, idx
, len
, replace_chars
[q
- quotable_chars
]);
301 add_char(out
, idx
, len
, *p
);
309 static krb5_error_code
310 unparse_name_fixed(krb5_context context
,
311 krb5_const_principal principal
,
318 int short_form
= (flags
& KRB5_PRINCIPAL_UNPARSE_SHORT
) != 0;
319 int no_realm
= (flags
& KRB5_PRINCIPAL_UNPARSE_NO_REALM
) != 0;
320 int display
= (flags
& KRB5_PRINCIPAL_UNPARSE_DISPLAY
) != 0;
322 if (!no_realm
&& princ_realm(principal
) == NULL
) {
323 krb5_set_error_string(context
, "Realm missing from principal, "
328 for(i
= 0; i
< princ_num_comp(principal
); i
++){
330 add_char(name
, idx
, len
, '/');
331 idx
= quote_string(princ_ncomp(principal
, i
), name
, idx
, len
, display
);
333 krb5_set_error_string(context
, "Out of space printing principal");
337 /* add realm if different from default realm */
338 if(short_form
&& !no_realm
) {
341 ret
= krb5_get_default_realm(context
, &r
);
344 if(strcmp(princ_realm(principal
), r
) != 0)
348 if(!short_form
&& !no_realm
) {
349 add_char(name
, idx
, len
, '@');
350 idx
= quote_string(princ_realm(principal
), name
, idx
, len
, display
);
352 krb5_set_error_string(context
,
353 "Out of space printing realm of principal");
360 krb5_error_code KRB5_LIB_FUNCTION
361 krb5_unparse_name_fixed(krb5_context context
,
362 krb5_const_principal principal
,
366 return unparse_name_fixed(context
, principal
, name
, len
, 0);
369 krb5_error_code KRB5_LIB_FUNCTION
370 krb5_unparse_name_fixed_short(krb5_context context
,
371 krb5_const_principal principal
,
375 return unparse_name_fixed(context
, principal
, name
, len
,
376 KRB5_PRINCIPAL_UNPARSE_SHORT
);
379 krb5_error_code KRB5_LIB_FUNCTION
380 krb5_unparse_name_fixed_flags(krb5_context context
,
381 krb5_const_principal principal
,
386 return unparse_name_fixed(context
, principal
, name
, len
, flags
);
389 static krb5_error_code
390 unparse_name(krb5_context context
,
391 krb5_const_principal principal
,
395 size_t len
= 0, plen
;
399 if (princ_realm(principal
)) {
400 plen
= strlen(princ_realm(principal
));
402 if(strcspn(princ_realm(principal
), quotable_chars
) == plen
)
408 for(i
= 0; i
< princ_num_comp(principal
); i
++){
409 plen
= strlen(princ_ncomp(principal
, i
));
410 if(strcspn(princ_ncomp(principal
, i
), quotable_chars
) == plen
)
419 krb5_set_error_string (context
, "malloc: out of memory");
422 ret
= unparse_name_fixed(context
, principal
, *name
, len
, flags
);
430 krb5_error_code KRB5_LIB_FUNCTION
431 krb5_unparse_name(krb5_context context
,
432 krb5_const_principal principal
,
435 return unparse_name(context
, principal
, name
, 0);
438 krb5_error_code KRB5_LIB_FUNCTION
439 krb5_unparse_name_flags(krb5_context context
,
440 krb5_const_principal principal
,
444 return unparse_name(context
, principal
, name
, flags
);
447 krb5_error_code KRB5_LIB_FUNCTION
448 krb5_unparse_name_short(krb5_context context
,
449 krb5_const_principal principal
,
452 return unparse_name(context
, principal
, name
, KRB5_PRINCIPAL_UNPARSE_SHORT
);
455 #if 0 /* not implemented */
457 krb5_error_code KRB5_LIB_FUNCTION
458 krb5_unparse_name_ext(krb5_context context
,
459 krb5_const_principal principal
,
463 krb5_abortx(context
, "unimplemented krb5_unparse_name_ext called");
468 krb5_realm
* KRB5_LIB_FUNCTION
469 krb5_princ_realm(krb5_context context
,
470 krb5_principal principal
)
472 return &princ_realm(principal
);
476 void KRB5_LIB_FUNCTION
477 krb5_princ_set_realm(krb5_context context
,
478 krb5_principal principal
,
481 princ_realm(principal
) = *realm
;
485 krb5_error_code KRB5_LIB_FUNCTION
486 krb5_build_principal(krb5_context context
,
487 krb5_principal
*principal
,
489 krb5_const_realm realm
,
495 ret
= krb5_build_principal_va(context
, principal
, rlen
, realm
, ap
);
500 static krb5_error_code
501 append_component(krb5_context context
, krb5_principal p
,
505 heim_general_string
*tmp
;
506 size_t len
= princ_num_comp(p
);
508 tmp
= realloc(princ_comp(p
), (len
+ 1) * sizeof(*tmp
));
510 krb5_set_error_string (context
, "malloc: out of memory");
514 princ_ncomp(p
, len
) = malloc(comp_len
+ 1);
515 if (princ_ncomp(p
, len
) == NULL
) {
516 krb5_set_error_string (context
, "malloc: out of memory");
519 memcpy (princ_ncomp(p
, len
), comp
, comp_len
);
520 princ_ncomp(p
, len
)[comp_len
] = '\0';
526 va_ext_princ(krb5_context context
, krb5_principal p
, va_list ap
)
531 len
= va_arg(ap
, int);
534 s
= va_arg(ap
, const char*);
535 append_component(context
, p
, s
, len
);
540 va_princ(krb5_context context
, krb5_principal p
, va_list ap
)
544 s
= va_arg(ap
, const char*);
547 append_component(context
, p
, s
, strlen(s
));
552 static krb5_error_code
553 build_principal(krb5_context context
,
554 krb5_principal
*principal
,
556 krb5_const_realm realm
,
557 void (*func
)(krb5_context
, krb5_principal
, va_list),
562 p
= calloc(1, sizeof(*p
));
564 krb5_set_error_string (context
, "malloc: out of memory");
567 princ_type(p
) = KRB5_NT_PRINCIPAL
;
569 princ_realm(p
) = strdup(realm
);
570 if(p
->realm
== NULL
){
572 krb5_set_error_string (context
, "malloc: out of memory");
576 (*func
)(context
, p
, ap
);
581 krb5_error_code KRB5_LIB_FUNCTION
582 krb5_make_principal(krb5_context context
,
583 krb5_principal
*principal
,
584 krb5_const_realm realm
,
591 ret
= krb5_get_default_realm(context
, &r
);
597 ret
= krb5_build_principal_va(context
, principal
, strlen(realm
), realm
, ap
);
604 krb5_error_code KRB5_LIB_FUNCTION
605 krb5_build_principal_va(krb5_context context
,
606 krb5_principal
*principal
,
608 krb5_const_realm realm
,
611 return build_principal(context
, principal
, rlen
, realm
, va_princ
, ap
);
614 krb5_error_code KRB5_LIB_FUNCTION
615 krb5_build_principal_va_ext(krb5_context context
,
616 krb5_principal
*principal
,
618 krb5_const_realm realm
,
621 return build_principal(context
, principal
, rlen
, realm
, va_ext_princ
, ap
);
625 krb5_error_code KRB5_LIB_FUNCTION
626 krb5_build_principal_ext(krb5_context context
,
627 krb5_principal
*principal
,
629 krb5_const_realm realm
,
635 ret
= krb5_build_principal_va_ext(context
, principal
, rlen
, realm
, ap
);
641 krb5_error_code KRB5_LIB_FUNCTION
642 krb5_copy_principal(krb5_context context
,
643 krb5_const_principal inprinc
,
644 krb5_principal
*outprinc
)
646 krb5_principal p
= malloc(sizeof(*p
));
648 krb5_set_error_string (context
, "malloc: out of memory");
651 if(copy_Principal(inprinc
, p
)) {
653 krb5_set_error_string (context
, "malloc: out of memory");
661 * return TRUE iff princ1 == princ2 (without considering the realm)
664 krb5_boolean KRB5_LIB_FUNCTION
665 krb5_principal_compare_any_realm(krb5_context context
,
666 krb5_const_principal princ1
,
667 krb5_const_principal princ2
)
670 if(princ_num_comp(princ1
) != princ_num_comp(princ2
))
672 for(i
= 0; i
< princ_num_comp(princ1
); i
++){
673 if(strcmp(princ_ncomp(princ1
, i
), princ_ncomp(princ2
, i
)) != 0)
680 * return TRUE iff princ1 == princ2
683 krb5_boolean KRB5_LIB_FUNCTION
684 krb5_principal_compare(krb5_context context
,
685 krb5_const_principal princ1
,
686 krb5_const_principal princ2
)
688 if(!krb5_realm_compare(context
, princ1
, princ2
))
690 return krb5_principal_compare_any_realm(context
, princ1
, princ2
);
694 * return TRUE iff realm(princ1) == realm(princ2)
697 krb5_boolean KRB5_LIB_FUNCTION
698 krb5_realm_compare(krb5_context context
,
699 krb5_const_principal princ1
,
700 krb5_const_principal princ2
)
702 return strcmp(princ_realm(princ1
), princ_realm(princ2
)) == 0;
706 * return TRUE iff princ matches pattern
709 krb5_boolean KRB5_LIB_FUNCTION
710 krb5_principal_match(krb5_context context
,
711 krb5_const_principal princ
,
712 krb5_const_principal pattern
)
715 if(princ_num_comp(princ
) != princ_num_comp(pattern
))
717 if(fnmatch(princ_realm(pattern
), princ_realm(princ
), 0) != 0)
719 for(i
= 0; i
< princ_num_comp(princ
); i
++){
720 if(fnmatch(princ_ncomp(pattern
, i
), princ_ncomp(princ
, i
), 0) != 0)
727 static struct v4_name_convert
{
730 } default_v4_name_convert
[] = {
732 { "hprop", "hprop" },
741 * return the converted instance name of `name' in `realm'.
742 * look in the configuration file and then in the default set above.
743 * return NULL if no conversion is appropriate.
747 get_name_conversion(krb5_context context
, const char *realm
, const char *name
)
749 struct v4_name_convert
*q
;
752 p
= krb5_config_get_string(context
, NULL
, "realms", realm
,
753 "v4_name_convert", "host", name
, NULL
);
755 p
= krb5_config_get_string(context
, NULL
, "libdefaults",
756 "v4_name_convert", "host", name
, NULL
);
760 /* XXX should be possible to override default list */
761 p
= krb5_config_get_string(context
, NULL
,
770 p
= krb5_config_get_string(context
, NULL
,
778 for(q
= default_v4_name_convert
; q
->from
; q
++)
779 if(strcmp(q
->from
, name
) == 0)
785 * convert the v4 principal `name.instance@realm' to a v5 principal in `princ'.
786 * if `resolve', use DNS.
787 * if `func', use that function for validating the conversion
790 krb5_error_code KRB5_LIB_FUNCTION
791 krb5_425_conv_principal_ext2(krb5_context context
,
793 const char *instance
,
795 krb5_boolean (*func
)(krb5_context
,
796 void *, krb5_principal
),
798 krb5_boolean resolve
,
799 krb5_principal
*princ
)
804 char host
[MAXHOSTNAMELEN
];
805 char local_hostname
[MAXHOSTNAMELEN
];
807 /* do the following: if the name is found in the
808 `v4_name_convert:host' part, is assumed to be a `host' type
809 principal, and the instance is looked up in the
810 `v4_instance_convert' part. if not found there the name is
811 (optionally) looked up as a hostname, and if that doesn't yield
812 anything, the `default_domain' is appended to the instance
817 if(instance
[0] == 0){
821 p
= get_name_conversion(context
, realm
, name
);
825 p
= krb5_config_get_string(context
, NULL
, "realms", realm
,
826 "v4_instance_convert", instance
, NULL
);
829 ret
= krb5_make_principal(context
, &pr
, realm
, name
, instance
, NULL
);
830 if(func
== NULL
|| (*func
)(context
, funcctx
, pr
)){
834 krb5_free_principal(context
, pr
);
836 krb5_clear_error_string (context
);
837 return HEIM_ERR_V4_PRINC_NO_CONV
;
840 krb5_boolean passed
= FALSE
;
845 r
= dns_lookup(instance
, "aaaa");
847 if (r
->head
&& r
->head
->type
== T_AAAA
) {
848 inst
= strdup(r
->head
->domain
);
853 r
= dns_lookup(instance
, "a");
855 if(r
->head
&& r
->head
->type
== T_A
) {
856 inst
= strdup(r
->head
->domain
);
863 struct addrinfo hints
, *ai
;
865 memset (&hints
, 0, sizeof(hints
));
866 hints
.ai_flags
= AI_CANONNAME
;
867 ret
= getaddrinfo(instance
, NULL
, &hints
, &ai
);
869 const struct addrinfo
*a
;
870 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
871 if (a
->ai_canonname
!= NULL
) {
872 inst
= strdup (a
->ai_canonname
);
882 krb5_set_error_string (context
, "malloc: out of memory");
886 ret
= krb5_make_principal(context
, &pr
, realm
, name
, inst
,
890 if(func
== NULL
|| (*func
)(context
, funcctx
, pr
)){
894 krb5_free_principal(context
, pr
);
899 snprintf(host
, sizeof(host
), "%s.%s", instance
, realm
);
901 ret
= krb5_make_principal(context
, &pr
, realm
, name
, host
, NULL
);
902 if((*func
)(context
, funcctx
, pr
)){
906 krb5_free_principal(context
, pr
);
910 * if the instance is the first component of the local hostname,
911 * the converted host should be the long hostname.
915 gethostname (local_hostname
, sizeof(local_hostname
)) == 0 &&
916 strncmp(instance
, local_hostname
, strlen(instance
)) == 0 &&
917 local_hostname
[strlen(instance
)] == '.') {
918 strlcpy(host
, local_hostname
, sizeof(host
));
924 domains
= krb5_config_get_strings(context
, NULL
, "realms", realm
,
926 for(d
= domains
; d
&& *d
; d
++){
927 snprintf(host
, sizeof(host
), "%s.%s", instance
, *d
);
928 ret
= krb5_make_principal(context
, &pr
, realm
, name
, host
, NULL
);
929 if(func
== NULL
|| (*func
)(context
, funcctx
, pr
)){
931 krb5_config_free_strings(domains
);
934 krb5_free_principal(context
, pr
);
936 krb5_config_free_strings(domains
);
940 p
= krb5_config_get_string(context
, NULL
, "realms", realm
,
941 "default_domain", NULL
);
943 /* this should be an error, just faking a name is not good */
944 krb5_clear_error_string (context
);
945 return HEIM_ERR_V4_PRINC_NO_CONV
;
950 snprintf(host
, sizeof(host
), "%s.%s", instance
, p
);
952 ret
= krb5_make_principal(context
, &pr
, realm
, name
, host
, NULL
);
953 if(func
== NULL
|| (*func
)(context
, funcctx
, pr
)){
957 krb5_free_principal(context
, pr
);
958 krb5_clear_error_string (context
);
959 return HEIM_ERR_V4_PRINC_NO_CONV
;
961 p
= krb5_config_get_string(context
, NULL
,
969 p
= krb5_config_get_string(context
, NULL
,
978 ret
= krb5_make_principal(context
, &pr
, realm
, name
, instance
, NULL
);
979 if(func
== NULL
|| (*func
)(context
, funcctx
, pr
)){
983 krb5_free_principal(context
, pr
);
984 krb5_clear_error_string (context
);
985 return HEIM_ERR_V4_PRINC_NO_CONV
;
989 convert_func(krb5_context conxtext
, void *funcctx
, krb5_principal principal
)
991 krb5_boolean (*func
)(krb5_context
, krb5_principal
) = funcctx
;
992 return (*func
)(conxtext
, principal
);
995 krb5_error_code KRB5_LIB_FUNCTION
996 krb5_425_conv_principal_ext(krb5_context context
,
998 const char *instance
,
1000 krb5_boolean (*func
)(krb5_context
, krb5_principal
),
1001 krb5_boolean resolve
,
1002 krb5_principal
*principal
)
1004 return krb5_425_conv_principal_ext2(context
,
1008 func
? convert_func
: NULL
,
1016 krb5_error_code KRB5_LIB_FUNCTION
1017 krb5_425_conv_principal(krb5_context context
,
1019 const char *instance
,
1021 krb5_principal
*princ
)
1023 krb5_boolean resolve
= krb5_config_get_bool(context
,
1026 "v4_instance_resolve",
1029 return krb5_425_conv_principal_ext(context
, name
, instance
, realm
,
1030 NULL
, resolve
, princ
);
1035 check_list(const krb5_config_binding
*l
, const char *name
, const char **out
)
1038 if (l
->type
!= krb5_config_string
)
1040 if(strcmp(name
, l
->u
.string
) == 0) {
1050 name_convert(krb5_context context
, const char *name
, const char *realm
,
1053 const krb5_config_binding
*l
;
1054 l
= krb5_config_get_list (context
,
1061 if(l
&& check_list(l
, name
, out
))
1062 return KRB5_NT_SRV_HST
;
1063 l
= krb5_config_get_list (context
,
1069 if(l
&& check_list(l
, name
, out
))
1070 return KRB5_NT_SRV_HST
;
1071 l
= krb5_config_get_list (context
,
1078 if(l
&& check_list(l
, name
, out
))
1079 return KRB5_NT_UNKNOWN
;
1080 l
= krb5_config_get_list (context
,
1086 if(l
&& check_list(l
, name
, out
))
1087 return KRB5_NT_UNKNOWN
;
1089 /* didn't find it in config file, try built-in list */
1091 struct v4_name_convert
*q
;
1092 for(q
= default_v4_name_convert
; q
->from
; q
++) {
1093 if(strcmp(name
, q
->to
) == 0) {
1095 return KRB5_NT_SRV_HST
;
1103 * convert the v5 principal in `principal' into a v4 corresponding one
1104 * in `name, instance, realm'
1105 * this is limited interface since there's no length given for these
1106 * three parameters. They have to be 40 bytes each (ANAME_SZ).
1109 krb5_error_code KRB5_LIB_FUNCTION
1110 krb5_524_conv_principal(krb5_context context
,
1111 const krb5_principal principal
,
1116 const char *n
, *i
, *r
;
1118 int type
= princ_type(principal
);
1119 const int aname_sz
= 40;
1121 r
= principal
->realm
;
1123 switch(principal
->name
.name_string
.len
){
1125 n
= principal
->name
.name_string
.val
[0];
1129 n
= principal
->name
.name_string
.val
[0];
1130 i
= principal
->name
.name_string
.val
[1];
1133 krb5_set_error_string (context
,
1134 "cannot convert a %d component principal",
1135 principal
->name
.name_string
.len
);
1136 return KRB5_PARSE_MALFORMED
;
1141 int t
= name_convert(context
, n
, r
, &tmp
);
1148 if(type
== KRB5_NT_SRV_HST
){
1151 strlcpy (tmpinst
, i
, sizeof(tmpinst
));
1152 p
= strchr(tmpinst
, '.');
1158 if (strlcpy (name
, n
, aname_sz
) >= aname_sz
) {
1159 krb5_set_error_string (context
,
1160 "too long name component to convert");
1161 return KRB5_PARSE_MALFORMED
;
1163 if (strlcpy (instance
, i
, aname_sz
) >= aname_sz
) {
1164 krb5_set_error_string (context
,
1165 "too long instance component to convert");
1166 return KRB5_PARSE_MALFORMED
;
1168 if (strlcpy (realm
, r
, aname_sz
) >= aname_sz
) {
1169 krb5_set_error_string (context
,
1170 "too long realm component to convert");
1171 return KRB5_PARSE_MALFORMED
;
1177 * Create a principal in `ret_princ' for the service `sname' running
1178 * on host `hostname'. */
1180 krb5_error_code KRB5_LIB_FUNCTION
1181 krb5_sname_to_principal (krb5_context context
,
1182 const char *hostname
,
1185 krb5_principal
*ret_princ
)
1187 krb5_error_code ret
;
1188 char localhost
[MAXHOSTNAMELEN
];
1189 char **realms
, *host
= NULL
;
1191 if(type
!= KRB5_NT_SRV_HST
&& type
!= KRB5_NT_UNKNOWN
) {
1192 krb5_set_error_string (context
, "unsupported name type %d",
1194 return KRB5_SNAME_UNSUPP_NAMETYPE
;
1196 if(hostname
== NULL
) {
1197 gethostname(localhost
, sizeof(localhost
));
1198 hostname
= localhost
;
1202 if(type
== KRB5_NT_SRV_HST
) {
1203 ret
= krb5_expand_hostname_realms (context
, hostname
,
1210 ret
= krb5_get_host_realm(context
, hostname
, &realms
);
1215 ret
= krb5_make_principal(context
, ret_princ
, realms
[0], sname
,
1219 krb5_free_host_realm(context
, realms
);
1223 static const struct {
1227 { "UNKNOWN", KRB5_NT_UNKNOWN
},
1228 { "PRINCIPAL", KRB5_NT_PRINCIPAL
},
1229 { "SRV_INST", KRB5_NT_SRV_INST
},
1230 { "SRV_HST", KRB5_NT_SRV_HST
},
1231 { "SRV_XHST", KRB5_NT_SRV_XHST
},
1232 { "UID", KRB5_NT_UID
},
1233 { "X500_PRINCIPAL", KRB5_NT_X500_PRINCIPAL
},
1234 { "SMTP_NAME", KRB5_NT_SMTP_NAME
},
1235 { "ENTERPRISE_PRINCIPAL", KRB5_NT_ENTERPRISE_PRINCIPAL
},
1236 { "ENT_PRINCIPAL_AND_ID", KRB5_NT_ENT_PRINCIPAL_AND_ID
},
1237 { "MS_PRINCIPAL", KRB5_NT_MS_PRINCIPAL
},
1238 { "MS_PRINCIPAL_AND_ID", KRB5_NT_MS_PRINCIPAL_AND_ID
},
1243 krb5_parse_nametype(krb5_context context
, const char *str
, int32_t *nametype
)
1247 for(i
= 0; nametypes
[i
].type
; i
++) {
1248 if (strcasecmp(nametypes
[i
].type
, str
) == 0) {
1249 *nametype
= nametypes
[i
].value
;
1253 krb5_set_error_string(context
, "Failed to find name type %s", str
);
1254 return KRB5_PARSE_MALFORMED
;