Sprinkle _krb5_debug() for more info about what the framework is actually doing behin...
[heimdal.git] / lib / krb5 / krbhst.c
blob4914086548cf46db02d54aa91a13873b805c75a4
1 /*
2 * Copyright (c) 2001 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
35 #include <resolve.h>
36 #include "locate_plugin.h"
38 static int
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;
47 return -1;
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)
61 char domain[1024];
62 struct rk_dns_reply *r;
63 struct rk_resource_record *rr;
64 int num_srv;
65 int proto_num;
66 int def_port;
68 *res = NULL;
69 *count = 0;
71 proto_num = string_to_proto(proto);
72 if(proto_num < 0) {
73 krb5_set_error_message(context, EINVAL,
74 N_("unknown protocol `%s' to lookup", ""),
75 proto);
76 return EINVAL;
79 if(proto_num == KRB5_KRBHST_HTTP)
80 def_port = ntohs(krb5_getportbyname (context, "http", "tcp", 80));
81 else if(port == 0)
82 def_port = ntohs(krb5_getportbyname (context, service, proto, 88));
83 else
84 def_port = port;
86 snprintf(domain, sizeof(domain), "_%s._%s.%s.", service, proto, realm);
88 r = rk_dns_lookup(domain, dns_type);
89 if(r == NULL) {
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)
97 num_srv++;
99 *res = malloc(num_srv * sizeof(**res));
100 if(*res == NULL) {
101 rk_dns_free_data(r);
102 krb5_set_error_message(context, ENOMEM,
103 N_("malloc: out of memory", ""));
104 return ENOMEM;
107 rk_dns_srv_order(r);
109 for(num_srv = 0, rr = r->head; rr; rr = rr->next)
110 if(rr->type == rk_ns_t_srv) {
111 krb5_krbhst_info *hi;
112 size_t len = strlen(rr->u.srv->target);
114 hi = calloc(1, sizeof(*hi) + len);
115 if(hi == NULL) {
116 rk_dns_free_data(r);
117 while(--num_srv >= 0)
118 free((*res)[num_srv]);
119 free(*res);
120 *res = NULL;
121 return ENOMEM;
123 (*res)[num_srv++] = hi;
125 hi->proto = proto_num;
127 hi->def_port = def_port;
128 if (port != 0)
129 hi->port = port;
130 else
131 hi->port = rr->u.srv->port;
133 strlcpy(hi->hostname, rr->u.srv->target, len + 1);
136 *count = num_srv;
138 rk_dns_free_data(r);
139 return 0;
143 struct krb5_krbhst_data {
144 char *realm;
145 unsigned int flags;
146 int def_port;
147 int port; /* hardwired port number if != 0 */
148 #define KD_CONFIG 1
149 #define KD_SRV_UDP 2
150 #define KD_SRV_TCP 4
151 #define KD_SRV_HTTP 8
152 #define KD_FALLBACK 16
153 #define KD_CONFIG_EXISTS 32
154 #define KD_LARGE_MSG 64
155 #define KD_PLUGIN 128
156 krb5_error_code (*get_next)(krb5_context, struct krb5_krbhst_data *,
157 krb5_krbhst_info**);
159 unsigned int fallback_count;
161 struct krb5_krbhst_info *hosts, **index, **end;
164 static krb5_boolean
165 krbhst_empty(const struct krb5_krbhst_data *kd)
167 return kd->index == &kd->hosts;
171 * Return the default protocol for the `kd' (either TCP or UDP)
174 static int
175 krbhst_get_default_proto(struct krb5_krbhst_data *kd)
177 if (kd->flags & KD_LARGE_MSG)
178 return KRB5_KRBHST_TCP;
179 return KRB5_KRBHST_UDP;
184 * parse `spec' into a krb5_krbhst_info, defaulting the port to `def_port'
185 * and forcing it to `port' if port != 0
188 static struct krb5_krbhst_info*
189 parse_hostspec(krb5_context context, struct krb5_krbhst_data *kd,
190 const char *spec, int def_port, int port)
192 const char *p = spec, *q;
193 struct krb5_krbhst_info *hi;
195 hi = calloc(1, sizeof(*hi) + strlen(spec));
196 if(hi == NULL)
197 return NULL;
199 hi->proto = krbhst_get_default_proto(kd);
201 if(strncmp(p, "http://", 7) == 0){
202 hi->proto = KRB5_KRBHST_HTTP;
203 p += 7;
204 } else if(strncmp(p, "http/", 5) == 0) {
205 hi->proto = KRB5_KRBHST_HTTP;
206 p += 5;
207 def_port = ntohs(krb5_getportbyname (context, "http", "tcp", 80));
208 }else if(strncmp(p, "tcp/", 4) == 0){
209 hi->proto = KRB5_KRBHST_TCP;
210 p += 4;
211 } else if(strncmp(p, "udp/", 4) == 0) {
212 p += 4;
215 if (p[0] == '[' && (q = strchr(p, ']')) != NULL) {
216 /* if address looks like [foo:bar] or [foo:bar]: its a ipv6
217 adress, strip of [] */
218 memcpy(hi->hostname, &p[1], q - p - 1);
219 hi->hostname[q - p - 1] = '\0';
220 p = q + 1;
221 /* get trailing : */
222 if (p[0] == ':')
223 p++;
224 } else if(strsep_copy(&p, ":", hi->hostname, strlen(spec) + 1) < 0) {
225 /* copy everything before : */
226 free(hi);
227 return NULL;
229 /* get rid of trailing /, and convert to lower case */
230 hi->hostname[strcspn(hi->hostname, "/")] = '\0';
231 strlwr(hi->hostname);
233 hi->port = hi->def_port = def_port;
234 if(p != NULL && p[0]) {
235 char *end;
236 hi->port = strtol(p, &end, 0);
237 if(end == p) {
238 free(hi);
239 return NULL;
242 if (port)
243 hi->port = port;
244 return hi;
247 void
248 _krb5_free_krbhst_info(krb5_krbhst_info *hi)
250 if (hi->ai != NULL)
251 freeaddrinfo(hi->ai);
252 free(hi);
255 krb5_error_code
256 _krb5_krbhost_info_move(krb5_context context,
257 krb5_krbhst_info *from,
258 krb5_krbhst_info **to)
260 size_t hostnamelen = strlen(from->hostname);
261 /* trailing NUL is included in structure */
262 *to = calloc(1, sizeof(**to) + hostnamelen);
263 if(*to == NULL) {
264 krb5_set_error_message(context, ENOMEM,
265 N_("malloc: out of memory", ""));
266 return ENOMEM;
269 (*to)->proto = from->proto;
270 (*to)->port = from->port;
271 (*to)->def_port = from->def_port;
272 (*to)->ai = from->ai;
273 from->ai = NULL;
274 (*to)->next = NULL;
275 memcpy((*to)->hostname, from->hostname, hostnamelen + 1);
276 return 0;
280 static void
281 append_host_hostinfo(struct krb5_krbhst_data *kd, struct krb5_krbhst_info *host)
283 struct krb5_krbhst_info *h;
285 for(h = kd->hosts; h; h = h->next)
286 if(h->proto == host->proto &&
287 h->port == host->port &&
288 strcmp(h->hostname, host->hostname) == 0) {
289 _krb5_free_krbhst_info(host);
290 return;
292 *kd->end = host;
293 kd->end = &host->next;
296 static krb5_error_code
297 append_host_string(krb5_context context, struct krb5_krbhst_data *kd,
298 const char *host, int def_port, int port)
300 struct krb5_krbhst_info *hi;
302 hi = parse_hostspec(context, kd, host, def_port, port);
303 if(hi == NULL)
304 return ENOMEM;
306 append_host_hostinfo(kd, hi);
307 return 0;
311 * return a readable representation of `host' in `hostname, hostlen'
314 krb5_error_code KRB5_LIB_FUNCTION
315 krb5_krbhst_format_string(krb5_context context, const krb5_krbhst_info *host,
316 char *hostname, size_t hostlen)
318 const char *proto = "";
319 char portstr[7] = "";
320 if(host->proto == KRB5_KRBHST_TCP)
321 proto = "tcp/";
322 else if(host->proto == KRB5_KRBHST_HTTP)
323 proto = "http://";
324 if(host->port != host->def_port)
325 snprintf(portstr, sizeof(portstr), ":%d", host->port);
326 snprintf(hostname, hostlen, "%s%s%s", proto, host->hostname, portstr);
327 return 0;
331 * create a getaddrinfo `hints' based on `proto'
334 static void
335 make_hints(struct addrinfo *hints, int proto)
337 memset(hints, 0, sizeof(*hints));
338 hints->ai_family = AF_UNSPEC;
339 switch(proto) {
340 case KRB5_KRBHST_UDP :
341 hints->ai_socktype = SOCK_DGRAM;
342 break;
343 case KRB5_KRBHST_HTTP :
344 case KRB5_KRBHST_TCP :
345 hints->ai_socktype = SOCK_STREAM;
346 break;
351 * return an `struct addrinfo *' in `ai' corresponding to the information
352 * in `host'. free:ing is handled by krb5_krbhst_free.
355 krb5_error_code KRB5_LIB_FUNCTION
356 krb5_krbhst_get_addrinfo(krb5_context context, krb5_krbhst_info *host,
357 struct addrinfo **ai)
359 struct addrinfo hints;
360 char portstr[NI_MAXSERV];
361 int ret;
363 if (host->ai == NULL) {
364 make_hints(&hints, host->proto);
365 snprintf (portstr, sizeof(portstr), "%d", host->port);
366 ret = getaddrinfo(host->hostname, portstr, &hints, &host->ai);
367 if (ret)
368 return krb5_eai_to_heim_errno(ret, errno);
370 *ai = host->ai;
371 return 0;
374 static krb5_boolean
375 get_next(struct krb5_krbhst_data *kd, krb5_krbhst_info **host)
377 struct krb5_krbhst_info *hi = *kd->index;
378 if(hi != NULL) {
379 *host = hi;
380 kd->index = &(*kd->index)->next;
381 return TRUE;
383 return FALSE;
386 static void
387 srv_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
388 const char *proto, const char *service)
390 krb5_error_code ret;
391 krb5_krbhst_info **res;
392 int count, i;
394 ret = srv_find_realm(context, &res, &count, kd->realm, "SRV", proto, service,
395 kd->port);
396 _krb5_debug(context, 2, "searching DNS for realm %s %s.%s -> %d",
397 kd->realm, proto, service, ret);
398 if (ret)
399 return;
400 for(i = 0; i < count; i++)
401 append_host_hostinfo(kd, res[i]);
402 free(res);
406 * read the configuration for `conf_string', defaulting to kd->def_port and
407 * forcing it to `kd->port' if kd->port != 0
410 static void
411 config_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
412 const char *conf_string)
414 int i;
415 char **hostlist;
416 hostlist = krb5_config_get_strings(context, NULL,
417 "realms", kd->realm, conf_string, NULL);
419 _krb5_debug(context, 2, "configuration file for realm %s%s found",
420 kd->realm, hostlist ? "" : " not");
422 if(hostlist == NULL)
423 return;
424 kd->flags |= KD_CONFIG_EXISTS;
425 for(i = 0; hostlist && hostlist[i] != NULL; i++)
426 append_host_string(context, kd, hostlist[i], kd->def_port, kd->port);
428 krb5_config_free_strings(hostlist);
432 * as a fallback, look for `serv_string.kd->realm' (typically
433 * kerberos.REALM, kerberos-1.REALM, ...
434 * `port' is the default port for the service, and `proto' the
435 * protocol
438 static krb5_error_code
439 fallback_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
440 const char *serv_string, int port, int proto)
442 char *host;
443 int ret;
444 struct addrinfo *ai;
445 struct addrinfo hints;
446 char portstr[NI_MAXSERV];
448 _krb5_debug(context, 2, "fallback lookup %d for realm %s (service %s)",
449 kd->fallback_count, kd->realm, serv_string);
452 * Don't try forever in case the DNS server keep returning us
453 * entries (like wildcard entries or the .nu TLD)
455 if(kd->fallback_count >= 5) {
456 kd->flags |= KD_FALLBACK;
457 return 0;
460 if(kd->fallback_count == 0)
461 asprintf(&host, "%s.%s.", serv_string, kd->realm);
462 else
463 asprintf(&host, "%s-%d.%s.",
464 serv_string, kd->fallback_count, kd->realm);
466 if (host == NULL)
467 return ENOMEM;
469 make_hints(&hints, proto);
470 snprintf(portstr, sizeof(portstr), "%d", port);
471 ret = getaddrinfo(host, portstr, &hints, &ai);
472 if (ret) {
473 /* no more hosts, so we're done here */
474 free(host);
475 kd->flags |= KD_FALLBACK;
476 } else {
477 struct krb5_krbhst_info *hi;
478 size_t hostlen = strlen(host);
480 hi = calloc(1, sizeof(*hi) + hostlen);
481 if(hi == NULL) {
482 free(host);
483 return ENOMEM;
486 hi->proto = proto;
487 hi->port = hi->def_port = port;
488 hi->ai = ai;
489 memmove(hi->hostname, host, hostlen);
490 hi->hostname[hostlen] = '\0';
491 free(host);
492 append_host_hostinfo(kd, hi);
493 kd->fallback_count++;
495 return 0;
499 * Fetch hosts from plugin
502 static krb5_error_code
503 add_locate(void *ctx, int type, struct sockaddr *addr)
505 struct krb5_krbhst_info *hi;
506 struct krb5_krbhst_data *kd = ctx;
507 char host[NI_MAXHOST], port[NI_MAXSERV];
508 struct addrinfo hints, *ai;
509 socklen_t socklen;
510 size_t hostlen;
511 int ret;
513 socklen = socket_sockaddr_size(addr);
515 ret = getnameinfo(addr, socklen, host, sizeof(host), port, sizeof(port),
516 NI_NUMERICHOST|NI_NUMERICSERV);
517 if (ret != 0)
518 return 0;
520 make_hints(&hints, krbhst_get_default_proto(kd));
521 ret = getaddrinfo(host, port, &hints, &ai);
522 if (ret)
523 return 0;
525 hostlen = strlen(host);
527 hi = calloc(1, sizeof(*hi) + hostlen);
528 if(hi == NULL)
529 return ENOMEM;
531 hi->proto = krbhst_get_default_proto(kd);
532 hi->port = hi->def_port = socket_get_port(addr);
533 hi->ai = ai;
534 memmove(hi->hostname, host, hostlen);
535 hi->hostname[hostlen] = '\0';
536 append_host_hostinfo(kd, hi);
538 return 0;
541 static void
542 plugin_get_hosts(krb5_context context,
543 struct krb5_krbhst_data *kd,
544 enum locate_service_type type)
546 struct krb5_plugin *list = NULL, *e;
547 krb5_error_code ret;
549 ret = _krb5_plugin_find(context, PLUGIN_TYPE_DATA,
550 KRB5_PLUGIN_LOCATE, &list);
551 if(ret != 0 || list == NULL)
552 return;
554 for (e = list; e != NULL; e = _krb5_plugin_get_next(e)) {
555 krb5plugin_service_locate_ftable *service;
556 void *ctx;
558 service = _krb5_plugin_get_symbol(e);
559 if (service->minor_version != 0)
560 continue;
562 (*service->init)(context, &ctx);
563 ret = (*service->lookup)(ctx, type, kd->realm, 0, 0, add_locate, kd);
564 (*service->fini)(ctx);
565 if (ret && ret != KRB5_PLUGIN_NO_HANDLE) {
566 krb5_set_error_message(context, ret,
567 N_("Locate plugin failed to lookup realm %s: %d", ""),
568 kd->realm, ret);
569 break;
570 } else if (ret == 0) {
571 _krb5_debug(context, 2, "plugin found result for realm %s", kd->realm);
572 kd->flags |= KD_CONFIG_EXISTS;
576 _krb5_plugin_free(list);
583 static krb5_error_code
584 kdc_get_next(krb5_context context,
585 struct krb5_krbhst_data *kd,
586 krb5_krbhst_info **host)
588 krb5_error_code ret;
590 if ((kd->flags & KD_PLUGIN) == 0) {
591 plugin_get_hosts(context, kd, locate_service_kdc);
592 kd->flags |= KD_PLUGIN;
593 if(get_next(kd, host))
594 return 0;
597 if((kd->flags & KD_CONFIG) == 0) {
598 config_get_hosts(context, kd, "kdc");
599 kd->flags |= KD_CONFIG;
600 if(get_next(kd, host))
601 return 0;
604 if (kd->flags & KD_CONFIG_EXISTS) {
605 _krb5_debug(context, 1,
606 "Configuration exists for realm %s, wont go to DNS",
607 kd->realm);
608 return KRB5_KDC_UNREACH;
611 if(context->srv_lookup) {
612 if((kd->flags & KD_SRV_UDP) == 0 && (kd->flags & KD_LARGE_MSG) == 0) {
613 srv_get_hosts(context, kd, "udp", "kerberos");
614 kd->flags |= KD_SRV_UDP;
615 if(get_next(kd, host))
616 return 0;
619 if((kd->flags & KD_SRV_TCP) == 0) {
620 srv_get_hosts(context, kd, "tcp", "kerberos");
621 kd->flags |= KD_SRV_TCP;
622 if(get_next(kd, host))
623 return 0;
625 if((kd->flags & KD_SRV_HTTP) == 0) {
626 srv_get_hosts(context, kd, "http", "kerberos");
627 kd->flags |= KD_SRV_HTTP;
628 if(get_next(kd, host))
629 return 0;
633 while((kd->flags & KD_FALLBACK) == 0) {
634 ret = fallback_get_hosts(context, kd, "kerberos",
635 kd->def_port,
636 krbhst_get_default_proto(kd));
637 if(ret)
638 return ret;
639 if(get_next(kd, host))
640 return 0;
643 _krb5_debug(context, 0, "No KDC entries found for %s", kd->realm);
645 return KRB5_KDC_UNREACH; /* XXX */
648 static krb5_error_code
649 admin_get_next(krb5_context context,
650 struct krb5_krbhst_data *kd,
651 krb5_krbhst_info **host)
653 krb5_error_code ret;
655 if ((kd->flags & KD_PLUGIN) == 0) {
656 plugin_get_hosts(context, kd, locate_service_kadmin);
657 kd->flags |= KD_PLUGIN;
658 if(get_next(kd, host))
659 return 0;
662 if((kd->flags & KD_CONFIG) == 0) {
663 config_get_hosts(context, kd, "admin_server");
664 kd->flags |= KD_CONFIG;
665 if(get_next(kd, host))
666 return 0;
669 if (kd->flags & KD_CONFIG_EXISTS) {
670 _krb5_debug(context, 1,
671 "Configuration exists for realm %s, wont go to DNS",
672 kd->realm);
673 return KRB5_KDC_UNREACH;
676 if(context->srv_lookup) {
677 if((kd->flags & KD_SRV_TCP) == 0) {
678 srv_get_hosts(context, kd, "tcp", "kerberos-adm");
679 kd->flags |= KD_SRV_TCP;
680 if(get_next(kd, host))
681 return 0;
685 if (krbhst_empty(kd)
686 && (kd->flags & KD_FALLBACK) == 0) {
687 ret = fallback_get_hosts(context, kd, "kerberos",
688 kd->def_port,
689 krbhst_get_default_proto(kd));
690 if(ret)
691 return ret;
692 kd->flags |= KD_FALLBACK;
693 if(get_next(kd, host))
694 return 0;
697 _krb5_debug(context, 0, "No admin entries found for realm %s", kd->realm);
699 return KRB5_KDC_UNREACH; /* XXX */
702 static krb5_error_code
703 kpasswd_get_next(krb5_context context,
704 struct krb5_krbhst_data *kd,
705 krb5_krbhst_info **host)
707 krb5_error_code ret;
709 if ((kd->flags & KD_PLUGIN) == 0) {
710 plugin_get_hosts(context, kd, locate_service_kpasswd);
711 kd->flags |= KD_PLUGIN;
712 if(get_next(kd, host))
713 return 0;
716 if((kd->flags & KD_CONFIG) == 0) {
717 config_get_hosts(context, kd, "kpasswd_server");
718 kd->flags |= KD_CONFIG;
719 if(get_next(kd, host))
720 return 0;
723 if (kd->flags & KD_CONFIG_EXISTS) {
724 _krb5_debug(context, 1,
725 "Configuration exists for realm %s, wont go to DNS",
726 kd->realm);
727 return KRB5_KDC_UNREACH;
730 if(context->srv_lookup) {
731 if((kd->flags & KD_SRV_UDP) == 0) {
732 srv_get_hosts(context, kd, "udp", "kpasswd");
733 kd->flags |= KD_SRV_UDP;
734 if(get_next(kd, host))
735 return 0;
737 if((kd->flags & KD_SRV_TCP) == 0) {
738 srv_get_hosts(context, kd, "tcp", "kpasswd");
739 kd->flags |= KD_SRV_TCP;
740 if(get_next(kd, host))
741 return 0;
745 /* no matches -> try admin */
747 if (krbhst_empty(kd)) {
748 kd->flags = 0;
749 kd->port = kd->def_port;
750 kd->get_next = admin_get_next;
751 ret = (*kd->get_next)(context, kd, host);
752 if (ret == 0)
753 (*host)->proto = krbhst_get_default_proto(kd);
754 return ret;
757 _krb5_debug(context, 0, "No kpasswd entries found for realm %s", kd->realm);
759 return KRB5_KDC_UNREACH;
762 static krb5_error_code
763 krb524_get_next(krb5_context context,
764 struct krb5_krbhst_data *kd,
765 krb5_krbhst_info **host)
767 if ((kd->flags & KD_PLUGIN) == 0) {
768 plugin_get_hosts(context, kd, locate_service_krb524);
769 kd->flags |= KD_PLUGIN;
770 if(get_next(kd, host))
771 return 0;
774 if((kd->flags & KD_CONFIG) == 0) {
775 config_get_hosts(context, kd, "krb524_server");
776 if(get_next(kd, host))
777 return 0;
778 kd->flags |= KD_CONFIG;
781 if (kd->flags & KD_CONFIG_EXISTS) {
782 _krb5_debug(context, 1,
783 "Configuration exists for realm %s, wont go to DNS",
784 kd->realm);
785 return KRB5_KDC_UNREACH;
788 if(context->srv_lookup) {
789 if((kd->flags & KD_SRV_UDP) == 0) {
790 srv_get_hosts(context, kd, "udp", "krb524");
791 kd->flags |= KD_SRV_UDP;
792 if(get_next(kd, host))
793 return 0;
796 if((kd->flags & KD_SRV_TCP) == 0) {
797 srv_get_hosts(context, kd, "tcp", "krb524");
798 kd->flags |= KD_SRV_TCP;
799 if(get_next(kd, host))
800 return 0;
804 /* no matches -> try kdc */
806 if (krbhst_empty(kd)) {
807 kd->flags = 0;
808 kd->port = kd->def_port;
809 kd->get_next = kdc_get_next;
810 return (*kd->get_next)(context, kd, host);
813 _krb5_debug(context, 0, "No kpasswd entries found for realm %s", kd->realm);
815 return KRB5_KDC_UNREACH;
818 static struct krb5_krbhst_data*
819 common_init(krb5_context context,
820 const char *service,
821 const char *realm,
822 int flags)
824 struct krb5_krbhst_data *kd;
826 if((kd = calloc(1, sizeof(*kd))) == NULL)
827 return NULL;
829 if((kd->realm = strdup(realm)) == NULL) {
830 free(kd);
831 return NULL;
834 _krb5_debug(context, 2, "Trying to find service %s for realm %s flags %x",
835 service, realm, flags);
837 /* For 'realms' without a . do not even think of going to DNS */
838 if (!strchr(realm, '.'))
839 kd->flags |= KD_CONFIG_EXISTS;
841 if (flags & KRB5_KRBHST_FLAGS_LARGE_MSG)
842 kd->flags |= KD_LARGE_MSG;
843 kd->end = kd->index = &kd->hosts;
844 return kd;
848 * initialize `handle' to look for hosts of type `type' in realm `realm'
851 krb5_error_code KRB5_LIB_FUNCTION
852 krb5_krbhst_init(krb5_context context,
853 const char *realm,
854 unsigned int type,
855 krb5_krbhst_handle *handle)
857 return krb5_krbhst_init_flags(context, realm, type, 0, handle);
860 krb5_error_code KRB5_LIB_FUNCTION
861 krb5_krbhst_init_flags(krb5_context context,
862 const char *realm,
863 unsigned int type,
864 int flags,
865 krb5_krbhst_handle *handle)
867 struct krb5_krbhst_data *kd;
868 krb5_error_code (*next)(krb5_context, struct krb5_krbhst_data *,
869 krb5_krbhst_info **);
870 int def_port;
871 const char *service;
873 switch(type) {
874 case KRB5_KRBHST_KDC:
875 next = kdc_get_next;
876 def_port = ntohs(krb5_getportbyname (context, "kerberos", "udp", 88));
877 service = "kdc";
878 break;
879 case KRB5_KRBHST_ADMIN:
880 next = admin_get_next;
881 def_port = ntohs(krb5_getportbyname (context, "kerberos-adm",
882 "tcp", 749));
883 service = "admin";
884 break;
885 case KRB5_KRBHST_CHANGEPW:
886 next = kpasswd_get_next;
887 def_port = ntohs(krb5_getportbyname (context, "kpasswd", "udp",
888 KPASSWD_PORT));
889 service = "change_password";
890 break;
891 case KRB5_KRBHST_KRB524:
892 next = krb524_get_next;
893 def_port = ntohs(krb5_getportbyname (context, "krb524", "udp", 4444));
894 service = "524";
895 break;
896 default:
897 krb5_set_error_message(context, ENOTTY,
898 N_("unknown krbhst type (%u)", ""), type);
899 return ENOTTY;
901 if((kd = common_init(context, service, realm, flags)) == NULL)
902 return ENOMEM;
903 kd->get_next = next;
904 kd->def_port = def_port;
905 *handle = kd;
906 return 0;
910 * return the next host information from `handle' in `host'
913 krb5_error_code KRB5_LIB_FUNCTION
914 krb5_krbhst_next(krb5_context context,
915 krb5_krbhst_handle handle,
916 krb5_krbhst_info **host)
918 if(get_next(handle, host))
919 return 0;
921 return (*handle->get_next)(context, handle, host);
925 * return the next host information from `handle' as a host name
926 * in `hostname' (or length `hostlen)
929 krb5_error_code KRB5_LIB_FUNCTION
930 krb5_krbhst_next_as_string(krb5_context context,
931 krb5_krbhst_handle handle,
932 char *hostname,
933 size_t hostlen)
935 krb5_error_code ret;
936 krb5_krbhst_info *host;
937 ret = krb5_krbhst_next(context, handle, &host);
938 if(ret)
939 return ret;
940 return krb5_krbhst_format_string(context, host, hostname, hostlen);
944 void KRB5_LIB_FUNCTION
945 krb5_krbhst_reset(krb5_context context, krb5_krbhst_handle handle)
947 handle->index = &handle->hosts;
950 void KRB5_LIB_FUNCTION
951 krb5_krbhst_free(krb5_context context, krb5_krbhst_handle handle)
953 krb5_krbhst_info *h, *next;
955 if (handle == NULL)
956 return;
958 for (h = handle->hosts; h != NULL; h = next) {
959 next = h->next;
960 _krb5_free_krbhst_info(h);
963 free(handle->realm);
964 free(handle);
967 /* backwards compatibility ahead */
969 static krb5_error_code
970 gethostlist(krb5_context context, const char *realm,
971 unsigned int type, char ***hostlist)
973 krb5_error_code ret;
974 int nhost = 0;
975 krb5_krbhst_handle handle;
976 char host[MAXHOSTNAMELEN];
977 krb5_krbhst_info *hostinfo;
979 ret = krb5_krbhst_init(context, realm, type, &handle);
980 if (ret)
981 return ret;
983 while(krb5_krbhst_next(context, handle, &hostinfo) == 0)
984 nhost++;
985 if(nhost == 0) {
986 krb5_set_error_message(context, KRB5_KDC_UNREACH,
987 N_("No KDC found for realm %s", ""), realm);
988 return KRB5_KDC_UNREACH;
990 *hostlist = calloc(nhost + 1, sizeof(**hostlist));
991 if(*hostlist == NULL) {
992 krb5_krbhst_free(context, handle);
993 return ENOMEM;
996 krb5_krbhst_reset(context, handle);
997 nhost = 0;
998 while(krb5_krbhst_next_as_string(context, handle,
999 host, sizeof(host)) == 0) {
1000 if(((*hostlist)[nhost++] = strdup(host)) == NULL) {
1001 krb5_free_krbhst(context, *hostlist);
1002 krb5_krbhst_free(context, handle);
1003 return ENOMEM;
1006 (*hostlist)[nhost] = NULL;
1007 krb5_krbhst_free(context, handle);
1008 return 0;
1012 * return an malloced list of kadmin-hosts for `realm' in `hostlist'
1015 krb5_error_code KRB5_LIB_FUNCTION
1016 krb5_get_krb_admin_hst (krb5_context context,
1017 const krb5_realm *realm,
1018 char ***hostlist)
1020 return gethostlist(context, *realm, KRB5_KRBHST_ADMIN, hostlist);
1024 * return an malloced list of changepw-hosts for `realm' in `hostlist'
1027 krb5_error_code KRB5_LIB_FUNCTION
1028 krb5_get_krb_changepw_hst (krb5_context context,
1029 const krb5_realm *realm,
1030 char ***hostlist)
1032 return gethostlist(context, *realm, KRB5_KRBHST_CHANGEPW, hostlist);
1036 * return an malloced list of 524-hosts for `realm' in `hostlist'
1039 krb5_error_code KRB5_LIB_FUNCTION
1040 krb5_get_krb524hst (krb5_context context,
1041 const krb5_realm *realm,
1042 char ***hostlist)
1044 return gethostlist(context, *realm, KRB5_KRBHST_KRB524, hostlist);
1049 * return an malloced list of KDC's for `realm' in `hostlist'
1052 krb5_error_code KRB5_LIB_FUNCTION
1053 krb5_get_krbhst (krb5_context context,
1054 const krb5_realm *realm,
1055 char ***hostlist)
1057 return gethostlist(context, *realm, KRB5_KRBHST_KDC, hostlist);
1061 * free all the memory allocated in `hostlist'
1064 krb5_error_code KRB5_LIB_FUNCTION
1065 krb5_free_krbhst (krb5_context context,
1066 char **hostlist)
1068 char **p;
1070 for (p = hostlist; *p; ++p)
1071 free (*p);
1072 free (hostlist);
1073 return 0;