2 * Copyright (c) 2001 - 2003 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"
36 #include "locate_plugin.h"
39 string_to_proto(const char *string)
41 if(strcasecmp(string, "udp") == 0)
42 return KRB5_KRBHST_UDP;
43 else if(strcasecmp(string, "tcp") == 0)
44 return KRB5_KRBHST_TCP;
45 else if(strcasecmp(string, "http") == 0)
46 return KRB5_KRBHST_HTTP;
51 * set `res' and `count' to the result of looking up SRV RR in DNS for
52 * `proto', `proto', `realm' using `dns_type'.
53 * if `port' != 0, force that port number
56 static krb5_error_code
57 srv_find_realm(krb5_context context, krb5_krbhst_info ***res, int *count,
58 const char *realm, const char *dns_type,
59 const char *proto, const char *service, int port)
62 struct rk_dns_reply *r;
63 struct rk_resource_record *rr;
71 proto_num = string_to_proto(proto);
73 krb5_set_error_message(context, EINVAL,
74 N_("unknown protocol `%s' to lookup", ""),
79 if(proto_num == KRB5_KRBHST_HTTP)
80 def_port = ntohs(krb5_getportbyname (context, "http", "tcp", 80));
82 def_port = ntohs(krb5_getportbyname (context, service, proto, 88));
86 snprintf(domain, sizeof(domain), "_%s._%s.%s.", service, proto, realm);
88 r = rk_dns_lookup(domain, dns_type);
90 _krb5_debug(context, 0,
91 "DNS lookup failed domain: %s", domain);
92 return KRB5_KDC_UNREACH;
95 for(num_srv = 0, rr = r->head; rr; rr = rr->next)
96 if(rr->type == rk_ns_t_srv)
100 _krb5_debug(context, 0,
101 "DNS SRV RR lookup domain nodata: %s", domain);
102 return KRB5_KDC_UNREACH;
105 *res = malloc(num_srv * sizeof(**res));
108 krb5_set_error_message(context, ENOMEM,
109 N_("malloc: out of memory", ""));
115 for(num_srv = 0, rr = r->head; rr; rr = rr->next)
116 if(rr->type == rk_ns_t_srv) {
117 krb5_krbhst_info *hi;
118 size_t len = strlen(rr->u.srv->target);
120 hi = calloc(1, sizeof(*hi) + len);
123 while(--num_srv >= 0)
124 free((*res)[num_srv]);
129 (*res)[num_srv++] = hi;
131 hi->proto = proto_num;
133 hi->def_port = def_port;
137 hi->port = rr->u.srv->port;
139 strlcpy(hi->hostname, rr->u.srv->target, len + 1);
149 struct krb5_krbhst_data {
153 int port; /* hardwired port number if != 0 */
157 #define KD_SRV_HTTP 8
158 #define KD_FALLBACK 16
159 #define KD_CONFIG_EXISTS 32
160 #define KD_LARGE_MSG 64
161 #define KD_PLUGIN 128
162 krb5_error_code (*get_next)(krb5_context, struct krb5_krbhst_data *,
165 unsigned int fallback_count;
167 struct krb5_krbhst_info *hosts, **index, **end;
171 krbhst_empty(const struct krb5_krbhst_data *kd)
173 return kd->index == &kd->hosts;
177 * Return the default protocol for the `kd' (either TCP or UDP)
181 krbhst_get_default_proto(struct krb5_krbhst_data *kd)
183 if (kd->flags & KD_LARGE_MSG)
184 return KRB5_KRBHST_TCP;
185 return KRB5_KRBHST_UDP;
193 _krb5_krbhst_get_realm(krb5_krbhst_handle handle)
195 return handle->realm;
199 * parse `spec' into a krb5_krbhst_info, defaulting the port to `def_port'
200 * and forcing it to `port' if port != 0
203 static struct krb5_krbhst_info*
204 parse_hostspec(krb5_context context, struct krb5_krbhst_data *kd,
205 const char *spec, int def_port, int port)
207 const char *p = spec, *q;
208 struct krb5_krbhst_info *hi;
210 hi = calloc(1, sizeof(*hi) + strlen(spec));
214 hi->proto = krbhst_get_default_proto(kd);
216 if(strncmp(p, "http://", 7) == 0){
217 hi->proto = KRB5_KRBHST_HTTP;
219 } else if(strncmp(p, "http/", 5) == 0) {
220 hi->proto = KRB5_KRBHST_HTTP;
222 def_port = ntohs(krb5_getportbyname (context, "http", "tcp", 80));
223 }else if(strncmp(p, "tcp/", 4) == 0){
224 hi->proto = KRB5_KRBHST_TCP;
226 } else if(strncmp(p, "udp/", 4) == 0) {
230 if (p[0] == '[' && (q = strchr(p, ']')) != NULL) {
231 /* if address looks like [foo:bar] or [foo:bar]: its a ipv6
232 adress, strip of [] */
233 memcpy(hi->hostname, &p[1], q - p - 1);
234 hi->hostname[q - p - 1] = '\0';
239 } else if(strsep_copy(&p, ":", hi->hostname, strlen(spec) + 1) < 0) {
240 /* copy everything before : */
244 /* get rid of trailing /, and convert to lower case */
245 hi->hostname[strcspn(hi->hostname, "/")] = '\0';
246 strlwr(hi->hostname);
248 hi->port = hi->def_port = def_port;
249 if(p != NULL && p[0]) {
251 hi->port = strtol(p, &end, 0);
263 _krb5_free_krbhst_info(krb5_krbhst_info *hi)
266 freeaddrinfo(hi->ai);
271 _krb5_krbhost_info_move(krb5_context context,
272 krb5_krbhst_info *from,
273 krb5_krbhst_info **to)
275 size_t hostnamelen = strlen(from->hostname);
276 /* trailing NUL is included in structure */
277 *to = calloc(1, sizeof(**to) + hostnamelen);
279 krb5_set_error_message(context, ENOMEM,
280 N_("malloc: out of memory", ""));
284 (*to)->proto = from->proto;
285 (*to)->port = from->port;
286 (*to)->def_port = from->def_port;
287 (*to)->ai = from->ai;
290 memcpy((*to)->hostname, from->hostname, hostnamelen + 1);
296 append_host_hostinfo(struct krb5_krbhst_data *kd, struct krb5_krbhst_info *host)
298 struct krb5_krbhst_info *h;
300 for(h = kd->hosts; h; h = h->next)
301 if(h->proto == host->proto &&
302 h->port == host->port &&
303 strcmp(h->hostname, host->hostname) == 0) {
304 _krb5_free_krbhst_info(host);
308 kd->end = &host->next;
311 static krb5_error_code
312 append_host_string(krb5_context context, struct krb5_krbhst_data *kd,
313 const char *host, int def_port, int port)
315 struct krb5_krbhst_info *hi;
317 hi = parse_hostspec(context, kd, host, def_port, port);
321 append_host_hostinfo(kd, hi);
326 * return a readable representation of `host' in `hostname, hostlen'
329 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
330 krb5_krbhst_format_string(krb5_context context, const krb5_krbhst_info *host,
331 char *hostname, size_t hostlen)
333 const char *proto = "";
334 char portstr[7] = "";
335 if(host->proto == KRB5_KRBHST_TCP)
337 else if(host->proto == KRB5_KRBHST_HTTP)
339 if(host->port != host->def_port)
340 snprintf(portstr, sizeof(portstr), ":%d", host->port);
341 snprintf(hostname, hostlen, "%s%s%s", proto, host->hostname, portstr);
346 * create a getaddrinfo `hints' based on `proto'
350 make_hints(struct addrinfo *hints, int proto)
352 memset(hints, 0, sizeof(*hints));
353 hints->ai_family = AF_UNSPEC;
355 case KRB5_KRBHST_UDP :
356 hints->ai_socktype = SOCK_DGRAM;
358 case KRB5_KRBHST_HTTP :
359 case KRB5_KRBHST_TCP :
360 hints->ai_socktype = SOCK_STREAM;
366 * Return an `struct addrinfo *' for a KDC host.
368 * Returns an the struct addrinfo in in that corresponds to the
369 * information in `host'. free:ing is handled by krb5_krbhst_free, so
370 * the returned ai must not be released.
375 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
376 krb5_krbhst_get_addrinfo(krb5_context context, krb5_krbhst_info *host,
377 struct addrinfo **ai)
381 if (host->ai == NULL) {
382 struct addrinfo hints;
383 char portstr[NI_MAXSERV];
384 char *hostname = host->hostname;
386 snprintf (portstr, sizeof(portstr), "%d", host->port);
387 make_hints(&hints, host->proto);
390 * First try this as an IP address, this allows us to add a
391 * dot at the end to stop using the search domains.
394 hints.ai_flags |= AI_NUMERICHOST | AI_NUMERICSERV;
396 ret = getaddrinfo(host->hostname, portstr, &hints, &host->ai);
401 * If the hostname contains a dot, assumes it's a FQDN and
402 * don't use search domains since that might be painfully slow
403 * when machine is disconnected from that network.
406 hints.ai_flags &= ~(AI_NUMERICHOST);
408 if (strchr(hostname, '.') && hostname[strlen(hostname) - 1] != '.') {
409 ret = asprintf(&hostname, "%s.", host->hostname);
410 if (ret < 0 || hostname == NULL)
414 ret = getaddrinfo(hostname, portstr, &hints, &host->ai);
415 if (hostname != host->hostname)
418 ret = krb5_eai_to_heim_errno(ret, errno);
428 get_next(struct krb5_krbhst_data *kd, krb5_krbhst_info **host)
430 struct krb5_krbhst_info *hi = *kd->index;
433 kd->index = &(*kd->index)->next;
440 srv_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
441 const char *proto, const char *service)
444 krb5_krbhst_info **res;
447 ret = srv_find_realm(context, &res, &count, kd->realm, "SRV", proto, service,
449 _krb5_debug(context, 2, "searching DNS for realm %s %s.%s -> %d",
450 kd->realm, proto, service, ret);
453 for(i = 0; i < count; i++)
454 append_host_hostinfo(kd, res[i]);
459 * read the configuration for `conf_string', defaulting to kd->def_port and
460 * forcing it to `kd->port' if kd->port != 0
464 config_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
465 const char *conf_string)
469 hostlist = krb5_config_get_strings(context, NULL,
470 "realms", kd->realm, conf_string, NULL);
472 _krb5_debug(context, 2, "configuration file for realm %s%s found",
473 kd->realm, hostlist ? "" : " not");
477 kd->flags |= KD_CONFIG_EXISTS;
478 for(i = 0; hostlist && hostlist[i] != NULL; i++)
479 append_host_string(context, kd, hostlist[i], kd->def_port, kd->port);
481 krb5_config_free_strings(hostlist);
485 * as a fallback, look for `serv_string.kd->realm' (typically
486 * kerberos.REALM, kerberos-1.REALM, ...
487 * `port' is the default port for the service, and `proto' the
491 static krb5_error_code
492 fallback_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
493 const char *serv_string, int port, int proto)
498 struct addrinfo hints;
499 char portstr[NI_MAXSERV];
501 _krb5_debug(context, 2, "fallback lookup %d for realm %s (service %s)",
502 kd->fallback_count, kd->realm, serv_string);
505 * Don't try forever in case the DNS server keep returning us
506 * entries (like wildcard entries or the .nu TLD)
508 if(kd->fallback_count >= 5) {
509 kd->flags |= KD_FALLBACK;
513 if(kd->fallback_count == 0)
514 ret = asprintf(&host, "%s.%s.", serv_string, kd->realm);
516 ret = asprintf(&host, "%s-%d.%s.",
517 serv_string, kd->fallback_count, kd->realm);
519 if (ret < 0 || host == NULL)
522 make_hints(&hints, proto);
523 snprintf(portstr, sizeof(portstr), "%d", port);
524 ret = getaddrinfo(host, portstr, &hints, &ai);
526 /* no more hosts, so we're done here */
528 kd->flags |= KD_FALLBACK;
530 struct krb5_krbhst_info *hi;
531 size_t hostlen = strlen(host);
533 hi = calloc(1, sizeof(*hi) + hostlen);
540 hi->port = hi->def_port = port;
542 memmove(hi->hostname, host, hostlen);
543 hi->hostname[hostlen] = '\0';
545 append_host_hostinfo(kd, hi);
546 kd->fallback_count++;
552 * Fetch hosts from plugin
555 static krb5_error_code
556 add_locate(void *ctx, int type, struct sockaddr *addr)
558 struct krb5_krbhst_info *hi;
559 struct krb5_krbhst_data *kd = ctx;
560 char host[NI_MAXHOST], port[NI_MAXSERV];
561 struct addrinfo hints, *ai;
566 socklen = socket_sockaddr_size(addr);
568 ret = getnameinfo(addr, socklen, host, sizeof(host), port, sizeof(port),
569 NI_NUMERICHOST|NI_NUMERICSERV);
573 make_hints(&hints, krbhst_get_default_proto(kd));
574 ret = getaddrinfo(host, port, &hints, &ai);
578 hostlen = strlen(host);
580 hi = calloc(1, sizeof(*hi) + hostlen);
584 hi->proto = krbhst_get_default_proto(kd);
585 hi->port = hi->def_port = socket_get_port(addr);
587 memmove(hi->hostname, host, hostlen);
588 hi->hostname[hostlen] = '\0';
589 append_host_hostinfo(kd, hi);
595 plugin_get_hosts(krb5_context context,
596 struct krb5_krbhst_data *kd,
597 enum locate_service_type type)
599 struct krb5_plugin *list = NULL, *e;
602 ret = _krb5_plugin_find(context, PLUGIN_TYPE_DATA,
603 KRB5_PLUGIN_LOCATE, &list);
604 if(ret != 0 || list == NULL)
607 for (e = list; e != NULL; e = _krb5_plugin_get_next(e)) {
608 krb5plugin_service_locate_ftable *service;
611 service = _krb5_plugin_get_symbol(e);
612 if (service->minor_version != 0)
615 (*service->init)(context, &ctx);
616 ret = (*service->lookup)(ctx, type, kd->realm, 0, 0, add_locate, kd);
617 (*service->fini)(ctx);
618 if (ret && ret != KRB5_PLUGIN_NO_HANDLE) {
619 krb5_set_error_message(context, ret,
620 N_("Locate plugin failed to lookup realm %s: %d", ""),
623 } else if (ret == 0) {
624 _krb5_debug(context, 2, "plugin found result for realm %s", kd->realm);
625 kd->flags |= KD_CONFIG_EXISTS;
629 _krb5_plugin_free(list);
636 static krb5_error_code
637 kdc_get_next(krb5_context context,
638 struct krb5_krbhst_data *kd,
639 krb5_krbhst_info **host)
643 if ((kd->flags & KD_PLUGIN) == 0) {
644 plugin_get_hosts(context, kd, locate_service_kdc);
645 kd->flags |= KD_PLUGIN;
646 if(get_next(kd, host))
650 if((kd->flags & KD_CONFIG) == 0) {
651 config_get_hosts(context, kd, "kdc");
652 kd->flags |= KD_CONFIG;
653 if(get_next(kd, host))
657 if (kd->flags & KD_CONFIG_EXISTS) {
658 _krb5_debug(context, 1,
659 "Configuration exists for realm %s, wont go to DNS",
661 return KRB5_KDC_UNREACH;
664 if(context->srv_lookup) {
665 if((kd->flags & KD_SRV_UDP) == 0 && (kd->flags & KD_LARGE_MSG) == 0) {
666 srv_get_hosts(context, kd, "udp", "kerberos");
667 kd->flags |= KD_SRV_UDP;
668 if(get_next(kd, host))
672 if((kd->flags & KD_SRV_TCP) == 0) {
673 srv_get_hosts(context, kd, "tcp", "kerberos");
674 kd->flags |= KD_SRV_TCP;
675 if(get_next(kd, host))
678 if((kd->flags & KD_SRV_HTTP) == 0) {
679 srv_get_hosts(context, kd, "http", "kerberos");
680 kd->flags |= KD_SRV_HTTP;
681 if(get_next(kd, host))
686 while((kd->flags & KD_FALLBACK) == 0) {
687 ret = fallback_get_hosts(context, kd, "kerberos",
689 krbhst_get_default_proto(kd));
692 if(get_next(kd, host))
696 _krb5_debug(context, 0, "No KDC entries found for %s", kd->realm);
698 return KRB5_KDC_UNREACH; /* XXX */
701 static krb5_error_code
702 admin_get_next(krb5_context context,
703 struct krb5_krbhst_data *kd,
704 krb5_krbhst_info **host)
708 if ((kd->flags & KD_PLUGIN) == 0) {
709 plugin_get_hosts(context, kd, locate_service_kadmin);
710 kd->flags |= KD_PLUGIN;
711 if(get_next(kd, host))
715 if((kd->flags & KD_CONFIG) == 0) {
716 config_get_hosts(context, kd, "admin_server");
717 kd->flags |= KD_CONFIG;
718 if(get_next(kd, host))
722 if (kd->flags & KD_CONFIG_EXISTS) {
723 _krb5_debug(context, 1,
724 "Configuration exists for realm %s, wont go to DNS",
726 return KRB5_KDC_UNREACH;
729 if(context->srv_lookup) {
730 if((kd->flags & KD_SRV_TCP) == 0) {
731 srv_get_hosts(context, kd, "tcp", "kerberos-adm");
732 kd->flags |= KD_SRV_TCP;
733 if(get_next(kd, host))
739 && (kd->flags & KD_FALLBACK) == 0) {
740 ret = fallback_get_hosts(context, kd, "kerberos",
742 krbhst_get_default_proto(kd));
745 kd->flags |= KD_FALLBACK;
746 if(get_next(kd, host))
750 _krb5_debug(context, 0, "No admin entries found for realm %s", kd->realm);
752 return KRB5_KDC_UNREACH; /* XXX */
755 static krb5_error_code
756 kpasswd_get_next(krb5_context context,
757 struct krb5_krbhst_data *kd,
758 krb5_krbhst_info **host)
762 if ((kd->flags & KD_PLUGIN) == 0) {
763 plugin_get_hosts(context, kd, locate_service_kpasswd);
764 kd->flags |= KD_PLUGIN;
765 if(get_next(kd, host))
769 if((kd->flags & KD_CONFIG) == 0) {
770 config_get_hosts(context, kd, "kpasswd_server");
771 kd->flags |= KD_CONFIG;
772 if(get_next(kd, host))
776 if (kd->flags & KD_CONFIG_EXISTS) {
777 _krb5_debug(context, 1,
778 "Configuration exists for realm %s, wont go to DNS",
780 return KRB5_KDC_UNREACH;
783 if(context->srv_lookup) {
784 if((kd->flags & KD_SRV_UDP) == 0) {
785 srv_get_hosts(context, kd, "udp", "kpasswd");
786 kd->flags |= KD_SRV_UDP;
787 if(get_next(kd, host))
790 if((kd->flags & KD_SRV_TCP) == 0) {
791 srv_get_hosts(context, kd, "tcp", "kpasswd");
792 kd->flags |= KD_SRV_TCP;
793 if(get_next(kd, host))
798 /* no matches -> try admin */
800 if (krbhst_empty(kd)) {
802 kd->port = kd->def_port;
803 kd->get_next = admin_get_next;
804 ret = (*kd->get_next)(context, kd, host);
806 (*host)->proto = krbhst_get_default_proto(kd);
810 _krb5_debug(context, 0, "No kpasswd entries found for realm %s", kd->realm);
812 return KRB5_KDC_UNREACH;
815 static krb5_error_code
816 krb524_get_next(krb5_context context,
817 struct krb5_krbhst_data *kd,
818 krb5_krbhst_info **host)
820 if ((kd->flags & KD_PLUGIN) == 0) {
821 plugin_get_hosts(context, kd, locate_service_krb524);
822 kd->flags |= KD_PLUGIN;
823 if(get_next(kd, host))
827 if((kd->flags & KD_CONFIG) == 0) {
828 config_get_hosts(context, kd, "krb524_server");
829 if(get_next(kd, host))
831 kd->flags |= KD_CONFIG;
834 if (kd->flags & KD_CONFIG_EXISTS) {
835 _krb5_debug(context, 1,
836 "Configuration exists for realm %s, wont go to DNS",
838 return KRB5_KDC_UNREACH;
841 if(context->srv_lookup) {
842 if((kd->flags & KD_SRV_UDP) == 0) {
843 srv_get_hosts(context, kd, "udp", "krb524");
844 kd->flags |= KD_SRV_UDP;
845 if(get_next(kd, host))
849 if((kd->flags & KD_SRV_TCP) == 0) {
850 srv_get_hosts(context, kd, "tcp", "krb524");
851 kd->flags |= KD_SRV_TCP;
852 if(get_next(kd, host))
857 /* no matches -> try kdc */
859 if (krbhst_empty(kd)) {
861 kd->port = kd->def_port;
862 kd->get_next = kdc_get_next;
863 return (*kd->get_next)(context, kd, host);
866 _krb5_debug(context, 0, "No kpasswd entries found for realm %s", kd->realm);
868 return KRB5_KDC_UNREACH;
871 static struct krb5_krbhst_data*
872 common_init(krb5_context context,
877 struct krb5_krbhst_data *kd;
879 if((kd = calloc(1, sizeof(*kd))) == NULL)
882 if((kd->realm = strdup(realm)) == NULL) {
887 _krb5_debug(context, 2, "Trying to find service %s for realm %s flags %x",
888 service, realm, flags);
890 /* For 'realms' without a . do not even think of going to DNS */
891 if (!strchr(realm, '.'))
892 kd->flags |= KD_CONFIG_EXISTS;
894 if (flags & KRB5_KRBHST_FLAGS_LARGE_MSG)
895 kd->flags |= KD_LARGE_MSG;
896 kd->end = kd->index = &kd->hosts;
901 * initialize `handle' to look for hosts of type `type' in realm `realm'
904 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
905 krb5_krbhst_init(krb5_context context,
908 krb5_krbhst_handle *handle)
910 return krb5_krbhst_init_flags(context, realm, type, 0, handle);
913 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
914 krb5_krbhst_init_flags(krb5_context context,
918 krb5_krbhst_handle *handle)
920 struct krb5_krbhst_data *kd;
921 krb5_error_code (*next)(krb5_context, struct krb5_krbhst_data *,
922 krb5_krbhst_info **);
927 case KRB5_KRBHST_KDC:
929 def_port = ntohs(krb5_getportbyname (context, "kerberos", "udp", 88));
932 case KRB5_KRBHST_ADMIN:
933 next = admin_get_next;
934 def_port = ntohs(krb5_getportbyname (context, "kerberos-adm",
938 case KRB5_KRBHST_CHANGEPW:
939 next = kpasswd_get_next;
940 def_port = ntohs(krb5_getportbyname (context, "kpasswd", "udp",
942 service = "change_password";
944 case KRB5_KRBHST_KRB524:
945 next = krb524_get_next;
946 def_port = ntohs(krb5_getportbyname (context, "krb524", "udp", 4444));
950 krb5_set_error_message(context, ENOTTY,
951 N_("unknown krbhst type (%u)", ""), type);
954 if((kd = common_init(context, service, realm, flags)) == NULL)
957 kd->def_port = def_port;
963 * return the next host information from `handle' in `host'
966 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
967 krb5_krbhst_next(krb5_context context,
968 krb5_krbhst_handle handle,
969 krb5_krbhst_info **host)
971 if(get_next(handle, host))
974 return (*handle->get_next)(context, handle, host);
978 * return the next host information from `handle' as a host name
979 * in `hostname' (or length `hostlen)
982 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
983 krb5_krbhst_next_as_string(krb5_context context,
984 krb5_krbhst_handle handle,
989 krb5_krbhst_info *host;
990 ret = krb5_krbhst_next(context, handle, &host);
993 return krb5_krbhst_format_string(context, host, hostname, hostlen);
997 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
998 krb5_krbhst_reset(krb5_context context, krb5_krbhst_handle handle)
1000 handle->index = &handle->hosts;
1003 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1004 krb5_krbhst_free(krb5_context context, krb5_krbhst_handle handle)
1006 krb5_krbhst_info *h, *next;
1011 for (h = handle->hosts; h != NULL; h = next) {
1013 _krb5_free_krbhst_info(h);
1016 free(handle->realm);
1020 /* backwards compatibility ahead */
1022 static krb5_error_code
1023 gethostlist(krb5_context context, const char *realm,
1024 unsigned int type, char ***hostlist)
1026 krb5_error_code ret;
1028 krb5_krbhst_handle handle;
1029 char host[MAXHOSTNAMELEN];
1030 krb5_krbhst_info *hostinfo;
1032 ret = krb5_krbhst_init(context, realm, type, &handle);
1036 while(krb5_krbhst_next(context, handle, &hostinfo) == 0)
1039 krb5_set_error_message(context, KRB5_KDC_UNREACH,
1040 N_("No KDC found for realm %s", ""), realm);
1041 return KRB5_KDC_UNREACH;
1043 *hostlist = calloc(nhost + 1, sizeof(**hostlist));
1044 if(*hostlist == NULL) {
1045 krb5_krbhst_free(context, handle);
1049 krb5_krbhst_reset(context, handle);
1051 while(krb5_krbhst_next_as_string(context, handle,
1052 host, sizeof(host)) == 0) {
1053 if(((*hostlist)[nhost++] = strdup(host)) == NULL) {
1054 krb5_free_krbhst(context, *hostlist);
1055 krb5_krbhst_free(context, handle);
1059 (*hostlist)[nhost] = NULL;
1060 krb5_krbhst_free(context, handle);
1065 * return an malloced list of kadmin-hosts for `realm' in `hostlist'
1068 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1069 krb5_get_krb_admin_hst (krb5_context context,
1070 const krb5_realm *realm,
1073 return gethostlist(context, *realm, KRB5_KRBHST_ADMIN, hostlist);
1077 * return an malloced list of changepw-hosts for `realm' in `hostlist'
1080 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1081 krb5_get_krb_changepw_hst (krb5_context context,
1082 const krb5_realm *realm,
1085 return gethostlist(context, *realm, KRB5_KRBHST_CHANGEPW, hostlist);
1089 * return an malloced list of 524-hosts for `realm' in `hostlist'
1092 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1093 krb5_get_krb524hst (krb5_context context,
1094 const krb5_realm *realm,
1097 return gethostlist(context, *realm, KRB5_KRBHST_KRB524, hostlist);
1102 * return an malloced list of KDC's for `realm' in `hostlist'
1105 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1106 krb5_get_krbhst (krb5_context context,
1107 const krb5_realm *realm,
1110 return gethostlist(context, *realm, KRB5_KRBHST_KDC, hostlist);
1114 * free all the memory allocated in `hostlist'
1117 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1118 krb5_free_krbhst (krb5_context context,
1123 for (p = hostlist; *p; ++p)