2 * Copyright (c) 2001 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "krb5_locl.h"
38 #include "locate_plugin.h"
41 string_to_proto(const char *string
)
43 if(strcasecmp(string
, "udp") == 0)
44 return KRB5_KRBHST_UDP
;
45 else if(strcasecmp(string
, "tcp") == 0)
46 return KRB5_KRBHST_TCP
;
47 else if(strcasecmp(string
, "http") == 0)
48 return KRB5_KRBHST_HTTP
;
53 is_invalid_tld_srv_target(const char *target
)
55 return (strncmp("your-dns-needs-immediate-attention.",
57 && strchr(&target
[35], '.') == NULL
);
61 * set `res' and `count' to the result of looking up SRV RR in DNS for
62 * `proto', `proto', `realm' using `dns_type'.
63 * if `port' != 0, force that port number
66 static krb5_error_code
67 srv_find_realm(krb5_context context
, krb5_krbhst_info
***res
, int *count
,
68 const char *realm
, const char *dns_type
,
69 const char *proto
, const char *service
, int port
)
72 struct rk_dns_reply
*r
;
73 struct rk_resource_record
*rr
;
81 proto_num
= string_to_proto(proto
);
83 krb5_set_error_message(context
, EINVAL
,
84 N_("unknown protocol `%s' to lookup", ""),
89 if(proto_num
== KRB5_KRBHST_HTTP
)
90 def_port
= ntohs(krb5_getportbyname (context
, "http", "tcp", 80));
92 def_port
= ntohs(krb5_getportbyname (context
, service
, proto
, 88));
96 snprintf(domain
, sizeof(domain
), "_%s._%s.%s.", service
, proto
, realm
);
98 r
= rk_dns_lookup(domain
, dns_type
);
100 _krb5_debug(context
, 0,
101 "DNS lookup failed domain: %s", domain
);
102 return KRB5_KDC_UNREACH
;
105 for(num_srv
= 0, rr
= r
->head
; rr
; rr
= rr
->next
)
106 if(rr
->type
== rk_ns_t_srv
)
109 *res
= malloc(num_srv
* sizeof(**res
));
112 return krb5_enomem(context
);
117 for(num_srv
= 0, rr
= r
->head
; rr
; rr
= rr
->next
)
118 if(rr
->type
== rk_ns_t_srv
) {
119 krb5_krbhst_info
*hi
= NULL
;
123 /* Test for top-level domain controlled interruptions */
124 if (!is_invalid_tld_srv_target(rr
->u
.srv
->target
)) {
126 len
= strlen(rr
->u
.srv
->target
);
127 hi
= calloc(1, sizeof(*hi
) + len
);
131 while(--num_srv
>= 0)
132 free((*res
)[num_srv
]);
137 "Domain lookup failed: "
138 "Realm %s needs immediate attention "
139 "see https://icann.org/namecollision",
141 return KRB5_KDC_UNREACH
;
143 return krb5_enomem(context
);
145 (*res
)[num_srv
++] = hi
;
147 hi
->proto
= proto_num
;
149 hi
->def_port
= def_port
;
153 hi
->port
= rr
->u
.srv
->port
;
155 strlcpy(hi
->hostname
, rr
->u
.srv
->target
, len
+ 1);
165 struct krb5_krbhst_data
{
169 int port
; /* hardwired port number if != 0 */
173 #define KD_SRV_HTTP 8
174 #define KD_FALLBACK 16
175 #define KD_CONFIG_EXISTS 32
176 #define KD_LARGE_MSG 64
177 #define KD_PLUGIN 128
178 #define KD_HOSTNAMES 256
179 krb5_error_code (*get_next
)(krb5_context
, struct krb5_krbhst_data
*,
183 unsigned int fallback_count
;
185 struct krb5_krbhst_info
*hosts
, **index
, **end
;
189 krbhst_empty(const struct krb5_krbhst_data
*kd
)
191 return kd
->index
== &kd
->hosts
;
195 * Return the default protocol for the `kd' (either TCP or UDP)
199 krbhst_get_default_proto(struct krb5_krbhst_data
*kd
)
201 if (kd
->flags
& KD_LARGE_MSG
)
202 return KRB5_KRBHST_TCP
;
203 return KRB5_KRBHST_UDP
;
207 krbhst_get_default_port(struct krb5_krbhst_data
*kd
)
216 KRB5_LIB_FUNCTION
const char * KRB5_LIB_CALL
217 _krb5_krbhst_get_realm(krb5_krbhst_handle handle
)
219 return handle
->realm
;
223 * parse `spec' into a krb5_krbhst_info, defaulting the port to `def_port'
224 * and forcing it to `port' if port != 0
227 static struct krb5_krbhst_info
*
228 parse_hostspec(krb5_context context
, struct krb5_krbhst_data
*kd
,
229 const char *spec
, int def_port
, int port
)
231 const char *p
= spec
, *q
;
232 struct krb5_krbhst_info
*hi
;
234 hi
= calloc(1, sizeof(*hi
) + strlen(spec
));
238 hi
->proto
= krbhst_get_default_proto(kd
);
240 if(strncmp(p
, "http://", 7) == 0){
241 hi
->proto
= KRB5_KRBHST_HTTP
;
243 } else if(strncmp(p
, "http/", 5) == 0) {
244 hi
->proto
= KRB5_KRBHST_HTTP
;
246 def_port
= ntohs(krb5_getportbyname (context
, "http", "tcp", 80));
247 }else if(strncmp(p
, "tcp/", 4) == 0){
248 hi
->proto
= KRB5_KRBHST_TCP
;
250 } else if(strncmp(p
, "udp/", 4) == 0) {
251 hi
->proto
= KRB5_KRBHST_UDP
;
255 if (p
[0] == '[' && (q
= strchr(p
, ']')) != NULL
) {
256 /* if address looks like [foo:bar] or [foo:bar]: its a ipv6
257 adress, strip of [] */
258 memcpy(hi
->hostname
, &p
[1], q
- p
- 1);
259 hi
->hostname
[q
- p
- 1] = '\0';
264 } else if(strsep_copy(&p
, ":", hi
->hostname
, strlen(spec
) + 1) < 0) {
265 /* copy everything before : */
269 /* get rid of trailing /, and convert to lower case */
270 hi
->hostname
[strcspn(hi
->hostname
, "/")] = '\0';
271 strlwr(hi
->hostname
);
273 hi
->port
= hi
->def_port
= def_port
;
274 if(p
!= NULL
&& p
[0]) {
276 hi
->port
= strtol(p
, &end
, 0);
287 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
288 _krb5_free_krbhst_info(krb5_krbhst_info
*hi
)
291 freeaddrinfo(hi
->ai
);
295 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
296 _krb5_krbhost_info_move(krb5_context context
,
297 krb5_krbhst_info
*from
,
298 krb5_krbhst_info
**to
)
300 size_t hostnamelen
= strlen(from
->hostname
);
301 /* trailing NUL is included in structure */
302 *to
= calloc(1, sizeof(**to
) + hostnamelen
);
304 return krb5_enomem(context
);
306 (*to
)->proto
= from
->proto
;
307 (*to
)->port
= from
->port
;
308 (*to
)->def_port
= from
->def_port
;
309 (*to
)->ai
= from
->ai
;
312 memcpy((*to
)->hostname
, from
->hostname
, hostnamelen
+ 1);
318 append_host_hostinfo(struct krb5_krbhst_data
*kd
, struct krb5_krbhst_info
*host
)
320 struct krb5_krbhst_info
*h
;
322 for(h
= kd
->hosts
; h
; h
= h
->next
)
323 if(h
->proto
== host
->proto
&&
324 h
->port
== host
->port
&&
325 strcmp(h
->hostname
, host
->hostname
) == 0) {
326 _krb5_free_krbhst_info(host
);
330 kd
->end
= &host
->next
;
333 static krb5_error_code
334 append_host_string(krb5_context context
, struct krb5_krbhst_data
*kd
,
335 const char *host
, int def_port
, int port
)
337 struct krb5_krbhst_info
*hi
;
339 hi
= parse_hostspec(context
, kd
, host
, def_port
, port
);
341 return krb5_enomem(context
);
343 append_host_hostinfo(kd
, hi
);
348 * return a readable representation of `host' in `hostname, hostlen'
351 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
352 krb5_krbhst_format_string(krb5_context context
, const krb5_krbhst_info
*host
,
353 char *hostname
, size_t hostlen
)
355 const char *proto
= "";
356 char portstr
[7] = "";
357 if(host
->proto
== KRB5_KRBHST_TCP
)
359 else if(host
->proto
== KRB5_KRBHST_HTTP
)
361 if(host
->port
!= host
->def_port
)
362 snprintf(portstr
, sizeof(portstr
), ":%d", host
->port
);
363 snprintf(hostname
, hostlen
, "%s%s%s", proto
, host
->hostname
, portstr
);
368 * create a getaddrinfo `hints' based on `proto'
372 make_hints(struct addrinfo
*hints
, int proto
)
374 memset(hints
, 0, sizeof(*hints
));
375 hints
->ai_family
= AF_UNSPEC
;
377 case KRB5_KRBHST_UDP
:
378 hints
->ai_socktype
= SOCK_DGRAM
;
380 case KRB5_KRBHST_HTTP
:
381 case KRB5_KRBHST_TCP
:
382 hints
->ai_socktype
= SOCK_STREAM
;
388 * Return an `struct addrinfo *' for a KDC host.
390 * Returns an the struct addrinfo in in that corresponds to the
391 * information in `host'. free:ing is handled by krb5_krbhst_free, so
392 * the returned ai must not be released.
397 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
398 krb5_krbhst_get_addrinfo(krb5_context context
, krb5_krbhst_info
*host
,
399 struct addrinfo
**ai
)
403 if (host
->ai
== NULL
) {
404 struct addrinfo hints
;
405 char portstr
[NI_MAXSERV
];
407 snprintf (portstr
, sizeof(portstr
), "%d", host
->port
);
408 make_hints(&hints
, host
->proto
);
410 ret
= getaddrinfo(host
->hostname
, portstr
, &hints
, &host
->ai
);
412 ret
= krb5_eai_to_heim_errno(ret
, errno
);
422 get_next(struct krb5_krbhst_data
*kd
, krb5_krbhst_info
**host
)
424 struct krb5_krbhst_info
*hi
= *kd
->index
;
427 kd
->index
= &(*kd
->index
)->next
;
434 srv_get_hosts(krb5_context context
, struct krb5_krbhst_data
*kd
,
435 const char *proto
, const char *service
)
438 krb5_krbhst_info
**res
;
441 if (krb5_realm_is_lkdc(kd
->realm
))
444 ret
= srv_find_realm(context
, &res
, &count
, kd
->realm
, "SRV", proto
, service
,
446 _krb5_debug(context
, 2, "searching DNS for realm %s %s.%s -> %d",
447 kd
->realm
, proto
, service
, ret
);
450 for(i
= 0; i
< count
; i
++)
451 append_host_hostinfo(kd
, res
[i
]);
456 * read the configuration for `conf_string', defaulting to kd->def_port and
457 * forcing it to `kd->port' if kd->port != 0
461 config_get_hosts(krb5_context context
, struct krb5_krbhst_data
*kd
,
462 const char *conf_string
)
466 hostlist
= krb5_config_get_strings(context
, NULL
,
467 "realms", kd
->realm
, conf_string
, NULL
);
469 _krb5_debug(context
, 2, "configuration file for realm %s%s found",
470 kd
->realm
, hostlist
? "" : " not");
474 kd
->flags
|= KD_CONFIG_EXISTS
;
475 for(i
= 0; hostlist
&& hostlist
[i
] != NULL
; i
++)
476 append_host_string(context
, kd
, hostlist
[i
], kd
->def_port
, kd
->port
);
478 krb5_config_free_strings(hostlist
);
482 * as a fallback, look for `serv_string.kd->realm' (typically
483 * kerberos.REALM, kerberos-1.REALM, ...
484 * `port' is the default port for the service, and `proto' the
488 static krb5_error_code
489 fallback_get_hosts(krb5_context context
, struct krb5_krbhst_data
*kd
,
490 const char *serv_string
, int port
, int proto
)
495 struct addrinfo hints
;
496 char portstr
[NI_MAXSERV
];
498 ret
= krb5_config_get_bool_default(context
, NULL
, KRB5_FALLBACK_DEFAULT
,
499 "libdefaults", "use_fallback", NULL
);
501 kd
->flags
|= KD_FALLBACK
;
505 _krb5_debug(context
, 2, "fallback lookup %d for realm %s (service %s)",
506 kd
->fallback_count
, kd
->realm
, serv_string
);
509 * Don't try forever in case the DNS server keep returning us
510 * entries (like wildcard entries or the .nu TLD)
512 * Also don't try LKDC realms since fallback wont work on them at all.
514 if(kd
->fallback_count
>= 5 || krb5_realm_is_lkdc(kd
->realm
)) {
515 kd
->flags
|= KD_FALLBACK
;
519 if(kd
->fallback_count
== 0)
520 ret
= asprintf(&host
, "%s.%s.", serv_string
, kd
->realm
);
522 ret
= asprintf(&host
, "%s-%d.%s.",
523 serv_string
, kd
->fallback_count
, kd
->realm
);
525 if (ret
< 0 || host
== NULL
)
526 return krb5_enomem(context
);
528 make_hints(&hints
, proto
);
529 snprintf(portstr
, sizeof(portstr
), "%d", port
);
530 ret
= getaddrinfo(host
, portstr
, &hints
, &ai
);
532 /* no more hosts, so we're done here */
534 kd
->flags
|= KD_FALLBACK
;
536 struct krb5_krbhst_info
*hi
;
539 /* Check for ICANN gTLD Name Collision address (127.0.53.53) */
540 if (ai
->ai_family
== AF_INET
) {
541 struct sockaddr_in
*sin
= (struct sockaddr_in
*)ai
->ai_addr
;
542 if (sin
->sin_addr
.s_addr
== htonl(0x7f003535)) {
544 "Fallback lookup failed: "
545 "Realm %s needs immediate attention "
546 "see https://icann.org/namecollision",
548 return KRB5_KDC_UNREACH
;
552 hostlen
= strlen(host
);
553 hi
= calloc(1, sizeof(*hi
) + hostlen
);
556 return krb5_enomem(context
);
560 hi
->port
= hi
->def_port
= port
;
562 memmove(hi
->hostname
, host
, hostlen
);
563 hi
->hostname
[hostlen
] = '\0';
565 append_host_hostinfo(kd
, hi
);
566 kd
->fallback_count
++;
572 * Fetch hosts from plugin
575 static krb5_error_code
576 add_plugin_host(struct krb5_krbhst_data
*kd
,
582 struct krb5_krbhst_info
*hi
;
583 struct addrinfo hints
, *ai
;
587 make_hints(&hints
, proto
);
588 ret
= getaddrinfo(host
, port
, &hints
, &ai
);
592 hostlen
= strlen(host
);
594 hi
= calloc(1, sizeof(*hi
) + hostlen
);
599 hi
->port
= hi
->def_port
= portnum
;
601 memmove(hi
->hostname
, host
, hostlen
);
602 hi
->hostname
[hostlen
] = '\0';
603 append_host_hostinfo(kd
, hi
);
608 static krb5_error_code
609 add_locate(void *ctx
, int type
, struct sockaddr
*addr
)
611 struct krb5_krbhst_data
*kd
= ctx
;
612 char host
[NI_MAXHOST
], port
[NI_MAXSERV
];
617 socklen
= socket_sockaddr_size(addr
);
618 portnum
= socket_get_port(addr
);
620 ret
= getnameinfo(addr
, socklen
, host
, sizeof(host
), port
, sizeof(port
),
621 NI_NUMERICHOST
|NI_NUMERICSERV
);
626 snprintf(port
, sizeof(port
), "%d", kd
->port
);
627 else if (atoi(port
) == 0)
628 snprintf(port
, sizeof(port
), "%d", krbhst_get_default_port(kd
));
630 proto
= krbhst_get_default_proto(kd
);
632 ret
= add_plugin_host(kd
, host
, port
, portnum
, proto
);
637 * This is really kind of broken and should be solved a different
638 * way, some sites block UDP, and we don't, in the general case,
639 * fall back to TCP, that should also be done. But since that
640 * should require us to invert the whole "find kdc" stack, let put
644 if (proto
== KRB5_KRBHST_UDP
) {
645 ret
= add_plugin_host(kd
, host
, port
, portnum
, KRB5_KRBHST_TCP
);
654 enum locate_service_type type
;
655 struct krb5_krbhst_data
*kd
;
659 static KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
660 plcallback(krb5_context context
,
661 const void *plug
, void *plugctx
, void *userctx
)
663 const krb5plugin_service_locate_ftable
*locate
= plug
;
664 struct plctx
*plctx
= userctx
;
666 if (locate
->minor_version
>= KRB5_PLUGIN_LOCATE_VERSION_2
)
667 return locate
->lookup(plugctx
, plctx
->flags
, plctx
->type
, plctx
->kd
->realm
, 0, 0, add_locate
, plctx
->kd
);
669 if (plctx
->flags
& KRB5_PLF_ALLOW_HOMEDIR
)
670 return locate
->old_lookup(plugctx
, plctx
->type
, plctx
->kd
->realm
, 0, 0, add_locate
, plctx
->kd
);
672 return KRB5_PLUGIN_NO_HANDLE
;
676 plugin_get_hosts(krb5_context context
,
677 struct krb5_krbhst_data
*kd
,
678 enum locate_service_type type
)
680 struct plctx ctx
= { type
, kd
, 0 };
682 if (_krb5_homedir_access(context
))
683 ctx
.flags
|= KRB5_PLF_ALLOW_HOMEDIR
;
685 _krb5_plugin_run_f(context
, "krb5", KRB5_PLUGIN_LOCATE
,
686 KRB5_PLUGIN_LOCATE_VERSION_0
,
687 0, &ctx
, plcallback
);
695 hostnames_get_hosts(krb5_context context
,
696 struct krb5_krbhst_data
*kd
,
699 kd
->flags
|= KD_HOSTNAMES
;
701 append_host_string(context
, kd
, kd
->hostname
, kd
->def_port
, kd
->port
);
709 static krb5_error_code
710 kdc_get_next(krb5_context context
,
711 struct krb5_krbhst_data
*kd
,
712 krb5_krbhst_info
**host
)
716 if ((kd
->flags
& KD_HOSTNAMES
) == 0) {
717 hostnames_get_hosts(context
, kd
, "kdc");
718 if(get_next(kd
, host
))
722 if ((kd
->flags
& KD_PLUGIN
) == 0) {
723 plugin_get_hosts(context
, kd
, locate_service_kdc
);
724 kd
->flags
|= KD_PLUGIN
;
725 if(get_next(kd
, host
))
729 if((kd
->flags
& KD_CONFIG
) == 0) {
730 config_get_hosts(context
, kd
, "kdc");
731 kd
->flags
|= KD_CONFIG
;
732 if(get_next(kd
, host
))
736 if (kd
->flags
& KD_CONFIG_EXISTS
) {
737 _krb5_debug(context
, 1,
738 "Configuration exists for realm %s, wont go to DNS",
740 return KRB5_KDC_UNREACH
;
743 if(context
->srv_lookup
) {
744 if((kd
->flags
& KD_SRV_UDP
) == 0 && (kd
->flags
& KD_LARGE_MSG
) == 0) {
745 srv_get_hosts(context
, kd
, "udp", "kerberos");
746 kd
->flags
|= KD_SRV_UDP
;
747 if(get_next(kd
, host
))
751 if((kd
->flags
& KD_SRV_TCP
) == 0) {
752 srv_get_hosts(context
, kd
, "tcp", "kerberos");
753 kd
->flags
|= KD_SRV_TCP
;
754 if(get_next(kd
, host
))
757 if((kd
->flags
& KD_SRV_HTTP
) == 0) {
758 srv_get_hosts(context
, kd
, "http", "kerberos");
759 kd
->flags
|= KD_SRV_HTTP
;
760 if(get_next(kd
, host
))
765 while((kd
->flags
& KD_FALLBACK
) == 0) {
766 ret
= fallback_get_hosts(context
, kd
, "kerberos",
768 krbhst_get_default_proto(kd
));
771 if(get_next(kd
, host
))
775 _krb5_debug(context
, 0, "No KDC entries found for %s", kd
->realm
);
777 return KRB5_KDC_UNREACH
; /* XXX */
780 static krb5_error_code
781 admin_get_next(krb5_context context
,
782 struct krb5_krbhst_data
*kd
,
783 krb5_krbhst_info
**host
)
787 if ((kd
->flags
& KD_PLUGIN
) == 0) {
788 plugin_get_hosts(context
, kd
, locate_service_kadmin
);
789 kd
->flags
|= KD_PLUGIN
;
790 if(get_next(kd
, host
))
794 if((kd
->flags
& KD_CONFIG
) == 0) {
795 config_get_hosts(context
, kd
, "admin_server");
796 kd
->flags
|= KD_CONFIG
;
797 if(get_next(kd
, host
))
801 if (kd
->flags
& KD_CONFIG_EXISTS
) {
802 _krb5_debug(context
, 1,
803 "Configuration exists for realm %s, wont go to DNS",
805 return KRB5_KDC_UNREACH
;
808 if(context
->srv_lookup
) {
809 if((kd
->flags
& KD_SRV_TCP
) == 0) {
810 srv_get_hosts(context
, kd
, "tcp", "kerberos-adm");
811 kd
->flags
|= KD_SRV_TCP
;
812 if(get_next(kd
, host
))
818 && (kd
->flags
& KD_FALLBACK
) == 0) {
819 ret
= fallback_get_hosts(context
, kd
, "kerberos",
821 krbhst_get_default_proto(kd
));
824 kd
->flags
|= KD_FALLBACK
;
825 if(get_next(kd
, host
))
829 _krb5_debug(context
, 0, "No admin entries found for realm %s", kd
->realm
);
831 return KRB5_KDC_UNREACH
; /* XXX */
834 static krb5_error_code
835 kpasswd_get_next(krb5_context context
,
836 struct krb5_krbhst_data
*kd
,
837 krb5_krbhst_info
**host
)
841 if ((kd
->flags
& KD_PLUGIN
) == 0) {
842 plugin_get_hosts(context
, kd
, locate_service_kpasswd
);
843 kd
->flags
|= KD_PLUGIN
;
844 if(get_next(kd
, host
))
848 if((kd
->flags
& KD_CONFIG
) == 0) {
849 config_get_hosts(context
, kd
, "kpasswd_server");
850 kd
->flags
|= KD_CONFIG
;
851 if(get_next(kd
, host
))
855 if (kd
->flags
& KD_CONFIG_EXISTS
) {
856 _krb5_debug(context
, 1,
857 "Configuration exists for realm %s, wont go to DNS",
859 return KRB5_KDC_UNREACH
;
862 if(context
->srv_lookup
) {
863 if((kd
->flags
& KD_SRV_UDP
) == 0) {
864 srv_get_hosts(context
, kd
, "udp", "kpasswd");
865 kd
->flags
|= KD_SRV_UDP
;
866 if(get_next(kd
, host
))
869 if((kd
->flags
& KD_SRV_TCP
) == 0) {
870 srv_get_hosts(context
, kd
, "tcp", "kpasswd");
871 kd
->flags
|= KD_SRV_TCP
;
872 if(get_next(kd
, host
))
877 /* no matches -> try admin */
879 if (krbhst_empty(kd
)) {
881 kd
->port
= kd
->def_port
;
882 kd
->get_next
= admin_get_next
;
883 ret
= (*kd
->get_next
)(context
, kd
, host
);
885 (*host
)->proto
= krbhst_get_default_proto(kd
);
889 _krb5_debug(context
, 0, "No kpasswd entries found for realm %s", kd
->realm
);
891 return KRB5_KDC_UNREACH
;
895 krbhost_dealloc(void *ptr
)
897 struct krb5_krbhst_data
*handle
= (struct krb5_krbhst_data
*)ptr
;
898 krb5_krbhst_info
*h
, *next
;
900 for (h
= handle
->hosts
; h
!= NULL
; h
= next
) {
902 _krb5_free_krbhst_info(h
);
904 if (handle
->hostname
)
905 free(handle
->hostname
);
910 static struct krb5_krbhst_data
*
911 common_init(krb5_context context
,
916 struct krb5_krbhst_data
*kd
;
918 if ((kd
= heim_alloc(sizeof(*kd
), "krbhst-context", krbhost_dealloc
)) == NULL
)
921 if((kd
->realm
= strdup(realm
)) == NULL
) {
926 _krb5_debug(context
, 2, "Trying to find service %s for realm %s flags %x",
927 service
, realm
, flags
);
929 /* For 'realms' without a . do not even think of going to DNS */
930 if (!strchr(realm
, '.'))
931 kd
->flags
|= KD_CONFIG_EXISTS
;
933 if (flags
& KRB5_KRBHST_FLAGS_LARGE_MSG
)
934 kd
->flags
|= KD_LARGE_MSG
;
935 kd
->end
= kd
->index
= &kd
->hosts
;
940 * initialize `handle' to look for hosts of type `type' in realm `realm'
943 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
944 krb5_krbhst_init(krb5_context context
,
947 krb5_krbhst_handle
*handle
)
949 return krb5_krbhst_init_flags(context
, realm
, type
, 0, handle
);
952 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
953 krb5_krbhst_init_flags(krb5_context context
,
957 krb5_krbhst_handle
*handle
)
959 struct krb5_krbhst_data
*kd
;
960 krb5_error_code (*next
)(krb5_context
, struct krb5_krbhst_data
*,
961 krb5_krbhst_info
**);
968 case KRB5_KRBHST_KDC
:
970 def_port
= ntohs(krb5_getportbyname (context
, "kerberos", "udp", 88));
973 case KRB5_KRBHST_ADMIN
:
974 next
= admin_get_next
;
975 def_port
= ntohs(krb5_getportbyname (context
, "kerberos-adm",
979 case KRB5_KRBHST_CHANGEPW
:
980 next
= kpasswd_get_next
;
981 def_port
= ntohs(krb5_getportbyname (context
, "kpasswd", "udp",
983 service
= "change_password";
986 krb5_set_error_message(context
, ENOTTY
,
987 N_("unknown krbhst type (%u)", ""), type
);
990 if((kd
= common_init(context
, service
, realm
, flags
)) == NULL
)
993 kd
->def_port
= def_port
;
999 * return the next host information from `handle' in `host'
1002 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1003 krb5_krbhst_next(krb5_context context
,
1004 krb5_krbhst_handle handle
,
1005 krb5_krbhst_info
**host
)
1007 if(get_next(handle
, host
))
1010 return (*handle
->get_next
)(context
, handle
, host
);
1014 * return the next host information from `handle' as a host name
1015 * in `hostname' (or length `hostlen)
1018 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1019 krb5_krbhst_next_as_string(krb5_context context
,
1020 krb5_krbhst_handle handle
,
1024 krb5_error_code ret
;
1025 krb5_krbhst_info
*host
;
1026 ret
= krb5_krbhst_next(context
, handle
, &host
);
1029 return krb5_krbhst_format_string(context
, host
, hostname
, hostlen
);
1036 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1037 krb5_krbhst_set_hostname(krb5_context context
,
1038 krb5_krbhst_handle handle
,
1039 const char *hostname
)
1041 if (handle
->hostname
)
1042 free(handle
->hostname
);
1043 handle
->hostname
= strdup(hostname
);
1044 if (handle
->hostname
== NULL
)
1049 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1050 krb5_krbhst_reset(krb5_context context
, krb5_krbhst_handle handle
)
1052 handle
->index
= &handle
->hosts
;
1055 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1056 krb5_krbhst_free(krb5_context context
, krb5_krbhst_handle handle
)
1058 heim_release(handle
);
1061 #ifndef HEIMDAL_SMALLER
1063 /* backwards compatibility ahead */
1065 static krb5_error_code
1066 gethostlist(krb5_context context
, const char *realm
,
1067 unsigned int type
, char ***hostlist
)
1069 krb5_error_code ret
;
1071 krb5_krbhst_handle handle
;
1072 char host
[MAXHOSTNAMELEN
];
1073 krb5_krbhst_info
*hostinfo
;
1075 ret
= krb5_krbhst_init(context
, realm
, type
, &handle
);
1079 while(krb5_krbhst_next(context
, handle
, &hostinfo
) == 0)
1082 krb5_set_error_message(context
, KRB5_KDC_UNREACH
,
1083 N_("No KDC found for realm %s", ""), realm
);
1084 return KRB5_KDC_UNREACH
;
1086 *hostlist
= calloc(nhost
+ 1, sizeof(**hostlist
));
1087 if(*hostlist
== NULL
) {
1088 krb5_krbhst_free(context
, handle
);
1089 return krb5_enomem(context
);
1092 krb5_krbhst_reset(context
, handle
);
1094 while(krb5_krbhst_next_as_string(context
, handle
,
1095 host
, sizeof(host
)) == 0) {
1096 if(((*hostlist
)[nhost
++] = strdup(host
)) == NULL
) {
1097 krb5_free_krbhst(context
, *hostlist
);
1098 krb5_krbhst_free(context
, handle
);
1099 return krb5_enomem(context
);
1102 (*hostlist
)[nhost
] = NULL
;
1103 krb5_krbhst_free(context
, handle
);
1108 * return an malloced list of kadmin-hosts for `realm' in `hostlist'
1111 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1112 krb5_get_krb_admin_hst (krb5_context context
,
1113 const krb5_realm
*realm
,
1116 return gethostlist(context
, *realm
, KRB5_KRBHST_ADMIN
, hostlist
);
1120 * return an malloced list of changepw-hosts for `realm' in `hostlist'
1123 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1124 krb5_get_krb_changepw_hst (krb5_context context
,
1125 const krb5_realm
*realm
,
1128 return gethostlist(context
, *realm
, KRB5_KRBHST_CHANGEPW
, hostlist
);
1132 * return an malloced list of 524-hosts for `realm' in `hostlist'
1135 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1136 krb5_get_krb524hst (krb5_context context
,
1137 const krb5_realm
*realm
,
1140 return gethostlist(context
, *realm
, KRB5_KRBHST_KRB524
, hostlist
);
1144 * return an malloced list of KDC's for `realm' in `hostlist'
1147 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1148 krb5_get_krbhst (krb5_context context
,
1149 const krb5_realm
*realm
,
1152 return gethostlist(context
, *realm
, KRB5_KRBHST_KDC
, hostlist
);
1156 * free all the memory allocated in `hostlist'
1159 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1160 krb5_free_krbhst (krb5_context context
,
1165 for (p
= hostlist
; *p
; ++p
)
1171 #endif /* HEIMDAL_SMALLER */