Add roken/rename.c to fix non-standard rename()
[heimdal.git] / lib / krb5 / krbhst.c
blob59656d6d969e743121772fee3bc02d98317e534d
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;
186 const char *
187 _krb5_krbhst_get_realm(krb5_krbhst_handle handle)
189 return handle->realm;
193 * parse `spec' into a krb5_krbhst_info, defaulting the port to `def_port'
194 * and forcing it to `port' if port != 0
197 static struct krb5_krbhst_info*
198 parse_hostspec(krb5_context context, struct krb5_krbhst_data *kd,
199 const char *spec, int def_port, int port)
201 const char *p = spec, *q;
202 struct krb5_krbhst_info *hi;
204 hi = calloc(1, sizeof(*hi) + strlen(spec));
205 if(hi == NULL)
206 return NULL;
208 hi->proto = krbhst_get_default_proto(kd);
210 if(strncmp(p, "http://", 7) == 0){
211 hi->proto = KRB5_KRBHST_HTTP;
212 p += 7;
213 } else if(strncmp(p, "http/", 5) == 0) {
214 hi->proto = KRB5_KRBHST_HTTP;
215 p += 5;
216 def_port = ntohs(krb5_getportbyname (context, "http", "tcp", 80));
217 }else if(strncmp(p, "tcp/", 4) == 0){
218 hi->proto = KRB5_KRBHST_TCP;
219 p += 4;
220 } else if(strncmp(p, "udp/", 4) == 0) {
221 p += 4;
224 if (p[0] == '[' && (q = strchr(p, ']')) != NULL) {
225 /* if address looks like [foo:bar] or [foo:bar]: its a ipv6
226 adress, strip of [] */
227 memcpy(hi->hostname, &p[1], q - p - 1);
228 hi->hostname[q - p - 1] = '\0';
229 p = q + 1;
230 /* get trailing : */
231 if (p[0] == ':')
232 p++;
233 } else if(strsep_copy(&p, ":", hi->hostname, strlen(spec) + 1) < 0) {
234 /* copy everything before : */
235 free(hi);
236 return NULL;
238 /* get rid of trailing /, and convert to lower case */
239 hi->hostname[strcspn(hi->hostname, "/")] = '\0';
240 strlwr(hi->hostname);
242 hi->port = hi->def_port = def_port;
243 if(p != NULL && p[0]) {
244 char *end;
245 hi->port = strtol(p, &end, 0);
246 if(end == p) {
247 free(hi);
248 return NULL;
251 if (port)
252 hi->port = port;
253 return hi;
256 void
257 _krb5_free_krbhst_info(krb5_krbhst_info *hi)
259 if (hi->ai != NULL)
260 freeaddrinfo(hi->ai);
261 free(hi);
264 krb5_error_code
265 _krb5_krbhost_info_move(krb5_context context,
266 krb5_krbhst_info *from,
267 krb5_krbhst_info **to)
269 size_t hostnamelen = strlen(from->hostname);
270 /* trailing NUL is included in structure */
271 *to = calloc(1, sizeof(**to) + hostnamelen);
272 if(*to == NULL) {
273 krb5_set_error_message(context, ENOMEM,
274 N_("malloc: out of memory", ""));
275 return ENOMEM;
278 (*to)->proto = from->proto;
279 (*to)->port = from->port;
280 (*to)->def_port = from->def_port;
281 (*to)->ai = from->ai;
282 from->ai = NULL;
283 (*to)->next = NULL;
284 memcpy((*to)->hostname, from->hostname, hostnamelen + 1);
285 return 0;
289 static void
290 append_host_hostinfo(struct krb5_krbhst_data *kd, struct krb5_krbhst_info *host)
292 struct krb5_krbhst_info *h;
294 for(h = kd->hosts; h; h = h->next)
295 if(h->proto == host->proto &&
296 h->port == host->port &&
297 strcmp(h->hostname, host->hostname) == 0) {
298 _krb5_free_krbhst_info(host);
299 return;
301 *kd->end = host;
302 kd->end = &host->next;
305 static krb5_error_code
306 append_host_string(krb5_context context, struct krb5_krbhst_data *kd,
307 const char *host, int def_port, int port)
309 struct krb5_krbhst_info *hi;
311 hi = parse_hostspec(context, kd, host, def_port, port);
312 if(hi == NULL)
313 return ENOMEM;
315 append_host_hostinfo(kd, hi);
316 return 0;
320 * return a readable representation of `host' in `hostname, hostlen'
323 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
324 krb5_krbhst_format_string(krb5_context context, const krb5_krbhst_info *host,
325 char *hostname, size_t hostlen)
327 const char *proto = "";
328 char portstr[7] = "";
329 if(host->proto == KRB5_KRBHST_TCP)
330 proto = "tcp/";
331 else if(host->proto == KRB5_KRBHST_HTTP)
332 proto = "http://";
333 if(host->port != host->def_port)
334 snprintf(portstr, sizeof(portstr), ":%d", host->port);
335 snprintf(hostname, hostlen, "%s%s%s", proto, host->hostname, portstr);
336 return 0;
340 * create a getaddrinfo `hints' based on `proto'
343 static void
344 make_hints(struct addrinfo *hints, int proto)
346 memset(hints, 0, sizeof(*hints));
347 hints->ai_family = AF_UNSPEC;
348 switch(proto) {
349 case KRB5_KRBHST_UDP :
350 hints->ai_socktype = SOCK_DGRAM;
351 break;
352 case KRB5_KRBHST_HTTP :
353 case KRB5_KRBHST_TCP :
354 hints->ai_socktype = SOCK_STREAM;
355 break;
360 * return an `struct addrinfo *' in `ai' corresponding to the information
361 * in `host'. free:ing is handled by krb5_krbhst_free.
364 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
365 krb5_krbhst_get_addrinfo(krb5_context context, krb5_krbhst_info *host,
366 struct addrinfo **ai)
368 struct addrinfo hints;
369 char portstr[NI_MAXSERV];
370 int ret;
372 if (host->ai == NULL) {
373 make_hints(&hints, host->proto);
374 snprintf (portstr, sizeof(portstr), "%d", host->port);
375 ret = getaddrinfo(host->hostname, portstr, &hints, &host->ai);
376 if (ret)
377 return krb5_eai_to_heim_errno(ret, errno);
379 *ai = host->ai;
380 return 0;
383 static krb5_boolean
384 get_next(struct krb5_krbhst_data *kd, krb5_krbhst_info **host)
386 struct krb5_krbhst_info *hi = *kd->index;
387 if(hi != NULL) {
388 *host = hi;
389 kd->index = &(*kd->index)->next;
390 return TRUE;
392 return FALSE;
395 static void
396 srv_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
397 const char *proto, const char *service)
399 krb5_error_code ret;
400 krb5_krbhst_info **res;
401 int count, i;
403 ret = srv_find_realm(context, &res, &count, kd->realm, "SRV", proto, service,
404 kd->port);
405 _krb5_debug(context, 2, "searching DNS for realm %s %s.%s -> %d",
406 kd->realm, proto, service, ret);
407 if (ret)
408 return;
409 for(i = 0; i < count; i++)
410 append_host_hostinfo(kd, res[i]);
411 free(res);
415 * read the configuration for `conf_string', defaulting to kd->def_port and
416 * forcing it to `kd->port' if kd->port != 0
419 static void
420 config_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
421 const char *conf_string)
423 int i;
424 char **hostlist;
425 hostlist = krb5_config_get_strings(context, NULL,
426 "realms", kd->realm, conf_string, NULL);
428 _krb5_debug(context, 2, "configuration file for realm %s%s found",
429 kd->realm, hostlist ? "" : " not");
431 if(hostlist == NULL)
432 return;
433 kd->flags |= KD_CONFIG_EXISTS;
434 for(i = 0; hostlist && hostlist[i] != NULL; i++)
435 append_host_string(context, kd, hostlist[i], kd->def_port, kd->port);
437 krb5_config_free_strings(hostlist);
441 * as a fallback, look for `serv_string.kd->realm' (typically
442 * kerberos.REALM, kerberos-1.REALM, ...
443 * `port' is the default port for the service, and `proto' the
444 * protocol
447 static krb5_error_code
448 fallback_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
449 const char *serv_string, int port, int proto)
451 char *host = NULL;
452 int ret;
453 struct addrinfo *ai;
454 struct addrinfo hints;
455 char portstr[NI_MAXSERV];
457 _krb5_debug(context, 2, "fallback lookup %d for realm %s (service %s)",
458 kd->fallback_count, kd->realm, serv_string);
461 * Don't try forever in case the DNS server keep returning us
462 * entries (like wildcard entries or the .nu TLD)
464 if(kd->fallback_count >= 5) {
465 kd->flags |= KD_FALLBACK;
466 return 0;
469 if(kd->fallback_count == 0)
470 ret = asprintf(&host, "%s.%s.", serv_string, kd->realm);
471 else
472 ret = asprintf(&host, "%s-%d.%s.",
473 serv_string, kd->fallback_count, kd->realm);
475 if (ret < 0 || host == NULL)
476 return ENOMEM;
478 make_hints(&hints, proto);
479 snprintf(portstr, sizeof(portstr), "%d", port);
480 ret = getaddrinfo(host, portstr, &hints, &ai);
481 if (ret) {
482 /* no more hosts, so we're done here */
483 free(host);
484 kd->flags |= KD_FALLBACK;
485 } else {
486 struct krb5_krbhst_info *hi;
487 size_t hostlen = strlen(host);
489 hi = calloc(1, sizeof(*hi) + hostlen);
490 if(hi == NULL) {
491 free(host);
492 return ENOMEM;
495 hi->proto = proto;
496 hi->port = hi->def_port = port;
497 hi->ai = ai;
498 memmove(hi->hostname, host, hostlen);
499 hi->hostname[hostlen] = '\0';
500 free(host);
501 append_host_hostinfo(kd, hi);
502 kd->fallback_count++;
504 return 0;
508 * Fetch hosts from plugin
511 static krb5_error_code
512 add_locate(void *ctx, int type, struct sockaddr *addr)
514 struct krb5_krbhst_info *hi;
515 struct krb5_krbhst_data *kd = ctx;
516 char host[NI_MAXHOST], port[NI_MAXSERV];
517 struct addrinfo hints, *ai;
518 socklen_t socklen;
519 size_t hostlen;
520 int ret;
522 socklen = socket_sockaddr_size(addr);
524 ret = getnameinfo(addr, socklen, host, sizeof(host), port, sizeof(port),
525 NI_NUMERICHOST|NI_NUMERICSERV);
526 if (ret != 0)
527 return 0;
529 make_hints(&hints, krbhst_get_default_proto(kd));
530 ret = getaddrinfo(host, port, &hints, &ai);
531 if (ret)
532 return 0;
534 hostlen = strlen(host);
536 hi = calloc(1, sizeof(*hi) + hostlen);
537 if(hi == NULL)
538 return ENOMEM;
540 hi->proto = krbhst_get_default_proto(kd);
541 hi->port = hi->def_port = socket_get_port(addr);
542 hi->ai = ai;
543 memmove(hi->hostname, host, hostlen);
544 hi->hostname[hostlen] = '\0';
545 append_host_hostinfo(kd, hi);
547 return 0;
550 static void
551 plugin_get_hosts(krb5_context context,
552 struct krb5_krbhst_data *kd,
553 enum locate_service_type type)
555 struct krb5_plugin *list = NULL, *e;
556 krb5_error_code ret;
558 ret = _krb5_plugin_find(context, PLUGIN_TYPE_DATA,
559 KRB5_PLUGIN_LOCATE, &list);
560 if(ret != 0 || list == NULL)
561 return;
563 for (e = list; e != NULL; e = _krb5_plugin_get_next(e)) {
564 krb5plugin_service_locate_ftable *service;
565 void *ctx;
567 service = _krb5_plugin_get_symbol(e);
568 if (service->minor_version != 0)
569 continue;
571 (*service->init)(context, &ctx);
572 ret = (*service->lookup)(ctx, type, kd->realm, 0, 0, add_locate, kd);
573 (*service->fini)(ctx);
574 if (ret && ret != KRB5_PLUGIN_NO_HANDLE) {
575 krb5_set_error_message(context, ret,
576 N_("Locate plugin failed to lookup realm %s: %d", ""),
577 kd->realm, ret);
578 break;
579 } else if (ret == 0) {
580 _krb5_debug(context, 2, "plugin found result for realm %s", kd->realm);
581 kd->flags |= KD_CONFIG_EXISTS;
585 _krb5_plugin_free(list);
592 static krb5_error_code
593 kdc_get_next(krb5_context context,
594 struct krb5_krbhst_data *kd,
595 krb5_krbhst_info **host)
597 krb5_error_code ret;
599 if ((kd->flags & KD_PLUGIN) == 0) {
600 plugin_get_hosts(context, kd, locate_service_kdc);
601 kd->flags |= KD_PLUGIN;
602 if(get_next(kd, host))
603 return 0;
606 if((kd->flags & KD_CONFIG) == 0) {
607 config_get_hosts(context, kd, "kdc");
608 kd->flags |= KD_CONFIG;
609 if(get_next(kd, host))
610 return 0;
613 if (kd->flags & KD_CONFIG_EXISTS) {
614 _krb5_debug(context, 1,
615 "Configuration exists for realm %s, wont go to DNS",
616 kd->realm);
617 return KRB5_KDC_UNREACH;
620 if(context->srv_lookup) {
621 if((kd->flags & KD_SRV_UDP) == 0 && (kd->flags & KD_LARGE_MSG) == 0) {
622 srv_get_hosts(context, kd, "udp", "kerberos");
623 kd->flags |= KD_SRV_UDP;
624 if(get_next(kd, host))
625 return 0;
628 if((kd->flags & KD_SRV_TCP) == 0) {
629 srv_get_hosts(context, kd, "tcp", "kerberos");
630 kd->flags |= KD_SRV_TCP;
631 if(get_next(kd, host))
632 return 0;
634 if((kd->flags & KD_SRV_HTTP) == 0) {
635 srv_get_hosts(context, kd, "http", "kerberos");
636 kd->flags |= KD_SRV_HTTP;
637 if(get_next(kd, host))
638 return 0;
642 while((kd->flags & KD_FALLBACK) == 0) {
643 ret = fallback_get_hosts(context, kd, "kerberos",
644 kd->def_port,
645 krbhst_get_default_proto(kd));
646 if(ret)
647 return ret;
648 if(get_next(kd, host))
649 return 0;
652 _krb5_debug(context, 0, "No KDC entries found for %s", kd->realm);
654 return KRB5_KDC_UNREACH; /* XXX */
657 static krb5_error_code
658 admin_get_next(krb5_context context,
659 struct krb5_krbhst_data *kd,
660 krb5_krbhst_info **host)
662 krb5_error_code ret;
664 if ((kd->flags & KD_PLUGIN) == 0) {
665 plugin_get_hosts(context, kd, locate_service_kadmin);
666 kd->flags |= KD_PLUGIN;
667 if(get_next(kd, host))
668 return 0;
671 if((kd->flags & KD_CONFIG) == 0) {
672 config_get_hosts(context, kd, "admin_server");
673 kd->flags |= KD_CONFIG;
674 if(get_next(kd, host))
675 return 0;
678 if (kd->flags & KD_CONFIG_EXISTS) {
679 _krb5_debug(context, 1,
680 "Configuration exists for realm %s, wont go to DNS",
681 kd->realm);
682 return KRB5_KDC_UNREACH;
685 if(context->srv_lookup) {
686 if((kd->flags & KD_SRV_TCP) == 0) {
687 srv_get_hosts(context, kd, "tcp", "kerberos-adm");
688 kd->flags |= KD_SRV_TCP;
689 if(get_next(kd, host))
690 return 0;
694 if (krbhst_empty(kd)
695 && (kd->flags & KD_FALLBACK) == 0) {
696 ret = fallback_get_hosts(context, kd, "kerberos",
697 kd->def_port,
698 krbhst_get_default_proto(kd));
699 if(ret)
700 return ret;
701 kd->flags |= KD_FALLBACK;
702 if(get_next(kd, host))
703 return 0;
706 _krb5_debug(context, 0, "No admin entries found for realm %s", kd->realm);
708 return KRB5_KDC_UNREACH; /* XXX */
711 static krb5_error_code
712 kpasswd_get_next(krb5_context context,
713 struct krb5_krbhst_data *kd,
714 krb5_krbhst_info **host)
716 krb5_error_code ret;
718 if ((kd->flags & KD_PLUGIN) == 0) {
719 plugin_get_hosts(context, kd, locate_service_kpasswd);
720 kd->flags |= KD_PLUGIN;
721 if(get_next(kd, host))
722 return 0;
725 if((kd->flags & KD_CONFIG) == 0) {
726 config_get_hosts(context, kd, "kpasswd_server");
727 kd->flags |= KD_CONFIG;
728 if(get_next(kd, host))
729 return 0;
732 if (kd->flags & KD_CONFIG_EXISTS) {
733 _krb5_debug(context, 1,
734 "Configuration exists for realm %s, wont go to DNS",
735 kd->realm);
736 return KRB5_KDC_UNREACH;
739 if(context->srv_lookup) {
740 if((kd->flags & KD_SRV_UDP) == 0) {
741 srv_get_hosts(context, kd, "udp", "kpasswd");
742 kd->flags |= KD_SRV_UDP;
743 if(get_next(kd, host))
744 return 0;
746 if((kd->flags & KD_SRV_TCP) == 0) {
747 srv_get_hosts(context, kd, "tcp", "kpasswd");
748 kd->flags |= KD_SRV_TCP;
749 if(get_next(kd, host))
750 return 0;
754 /* no matches -> try admin */
756 if (krbhst_empty(kd)) {
757 kd->flags = 0;
758 kd->port = kd->def_port;
759 kd->get_next = admin_get_next;
760 ret = (*kd->get_next)(context, kd, host);
761 if (ret == 0)
762 (*host)->proto = krbhst_get_default_proto(kd);
763 return ret;
766 _krb5_debug(context, 0, "No kpasswd entries found for realm %s", kd->realm);
768 return KRB5_KDC_UNREACH;
771 static krb5_error_code
772 krb524_get_next(krb5_context context,
773 struct krb5_krbhst_data *kd,
774 krb5_krbhst_info **host)
776 if ((kd->flags & KD_PLUGIN) == 0) {
777 plugin_get_hosts(context, kd, locate_service_krb524);
778 kd->flags |= KD_PLUGIN;
779 if(get_next(kd, host))
780 return 0;
783 if((kd->flags & KD_CONFIG) == 0) {
784 config_get_hosts(context, kd, "krb524_server");
785 if(get_next(kd, host))
786 return 0;
787 kd->flags |= KD_CONFIG;
790 if (kd->flags & KD_CONFIG_EXISTS) {
791 _krb5_debug(context, 1,
792 "Configuration exists for realm %s, wont go to DNS",
793 kd->realm);
794 return KRB5_KDC_UNREACH;
797 if(context->srv_lookup) {
798 if((kd->flags & KD_SRV_UDP) == 0) {
799 srv_get_hosts(context, kd, "udp", "krb524");
800 kd->flags |= KD_SRV_UDP;
801 if(get_next(kd, host))
802 return 0;
805 if((kd->flags & KD_SRV_TCP) == 0) {
806 srv_get_hosts(context, kd, "tcp", "krb524");
807 kd->flags |= KD_SRV_TCP;
808 if(get_next(kd, host))
809 return 0;
813 /* no matches -> try kdc */
815 if (krbhst_empty(kd)) {
816 kd->flags = 0;
817 kd->port = kd->def_port;
818 kd->get_next = kdc_get_next;
819 return (*kd->get_next)(context, kd, host);
822 _krb5_debug(context, 0, "No kpasswd entries found for realm %s", kd->realm);
824 return KRB5_KDC_UNREACH;
827 static struct krb5_krbhst_data*
828 common_init(krb5_context context,
829 const char *service,
830 const char *realm,
831 int flags)
833 struct krb5_krbhst_data *kd;
835 if((kd = calloc(1, sizeof(*kd))) == NULL)
836 return NULL;
838 if((kd->realm = strdup(realm)) == NULL) {
839 free(kd);
840 return NULL;
843 _krb5_debug(context, 2, "Trying to find service %s for realm %s flags %x",
844 service, realm, flags);
846 /* For 'realms' without a . do not even think of going to DNS */
847 if (!strchr(realm, '.'))
848 kd->flags |= KD_CONFIG_EXISTS;
850 if (flags & KRB5_KRBHST_FLAGS_LARGE_MSG)
851 kd->flags |= KD_LARGE_MSG;
852 kd->end = kd->index = &kd->hosts;
853 return kd;
857 * initialize `handle' to look for hosts of type `type' in realm `realm'
860 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
861 krb5_krbhst_init(krb5_context context,
862 const char *realm,
863 unsigned int type,
864 krb5_krbhst_handle *handle)
866 return krb5_krbhst_init_flags(context, realm, type, 0, handle);
869 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
870 krb5_krbhst_init_flags(krb5_context context,
871 const char *realm,
872 unsigned int type,
873 int flags,
874 krb5_krbhst_handle *handle)
876 struct krb5_krbhst_data *kd;
877 krb5_error_code (*next)(krb5_context, struct krb5_krbhst_data *,
878 krb5_krbhst_info **);
879 int def_port;
880 const char *service;
882 switch(type) {
883 case KRB5_KRBHST_KDC:
884 next = kdc_get_next;
885 def_port = ntohs(krb5_getportbyname (context, "kerberos", "udp", 88));
886 service = "kdc";
887 break;
888 case KRB5_KRBHST_ADMIN:
889 next = admin_get_next;
890 def_port = ntohs(krb5_getportbyname (context, "kerberos-adm",
891 "tcp", 749));
892 service = "admin";
893 break;
894 case KRB5_KRBHST_CHANGEPW:
895 next = kpasswd_get_next;
896 def_port = ntohs(krb5_getportbyname (context, "kpasswd", "udp",
897 KPASSWD_PORT));
898 service = "change_password";
899 break;
900 case KRB5_KRBHST_KRB524:
901 next = krb524_get_next;
902 def_port = ntohs(krb5_getportbyname (context, "krb524", "udp", 4444));
903 service = "524";
904 break;
905 default:
906 krb5_set_error_message(context, ENOTTY,
907 N_("unknown krbhst type (%u)", ""), type);
908 return ENOTTY;
910 if((kd = common_init(context, service, realm, flags)) == NULL)
911 return ENOMEM;
912 kd->get_next = next;
913 kd->def_port = def_port;
914 *handle = kd;
915 return 0;
919 * return the next host information from `handle' in `host'
922 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
923 krb5_krbhst_next(krb5_context context,
924 krb5_krbhst_handle handle,
925 krb5_krbhst_info **host)
927 if(get_next(handle, host))
928 return 0;
930 return (*handle->get_next)(context, handle, host);
934 * return the next host information from `handle' as a host name
935 * in `hostname' (or length `hostlen)
938 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
939 krb5_krbhst_next_as_string(krb5_context context,
940 krb5_krbhst_handle handle,
941 char *hostname,
942 size_t hostlen)
944 krb5_error_code ret;
945 krb5_krbhst_info *host;
946 ret = krb5_krbhst_next(context, handle, &host);
947 if(ret)
948 return ret;
949 return krb5_krbhst_format_string(context, host, hostname, hostlen);
953 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
954 krb5_krbhst_reset(krb5_context context, krb5_krbhst_handle handle)
956 handle->index = &handle->hosts;
959 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
960 krb5_krbhst_free(krb5_context context, krb5_krbhst_handle handle)
962 krb5_krbhst_info *h, *next;
964 if (handle == NULL)
965 return;
967 for (h = handle->hosts; h != NULL; h = next) {
968 next = h->next;
969 _krb5_free_krbhst_info(h);
972 free(handle->realm);
973 free(handle);
976 /* backwards compatibility ahead */
978 static krb5_error_code
979 gethostlist(krb5_context context, const char *realm,
980 unsigned int type, char ***hostlist)
982 krb5_error_code ret;
983 int nhost = 0;
984 krb5_krbhst_handle handle;
985 char host[MAXHOSTNAMELEN];
986 krb5_krbhst_info *hostinfo;
988 ret = krb5_krbhst_init(context, realm, type, &handle);
989 if (ret)
990 return ret;
992 while(krb5_krbhst_next(context, handle, &hostinfo) == 0)
993 nhost++;
994 if(nhost == 0) {
995 krb5_set_error_message(context, KRB5_KDC_UNREACH,
996 N_("No KDC found for realm %s", ""), realm);
997 return KRB5_KDC_UNREACH;
999 *hostlist = calloc(nhost + 1, sizeof(**hostlist));
1000 if(*hostlist == NULL) {
1001 krb5_krbhst_free(context, handle);
1002 return ENOMEM;
1005 krb5_krbhst_reset(context, handle);
1006 nhost = 0;
1007 while(krb5_krbhst_next_as_string(context, handle,
1008 host, sizeof(host)) == 0) {
1009 if(((*hostlist)[nhost++] = strdup(host)) == NULL) {
1010 krb5_free_krbhst(context, *hostlist);
1011 krb5_krbhst_free(context, handle);
1012 return ENOMEM;
1015 (*hostlist)[nhost] = NULL;
1016 krb5_krbhst_free(context, handle);
1017 return 0;
1021 * return an malloced list of kadmin-hosts for `realm' in `hostlist'
1024 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1025 krb5_get_krb_admin_hst (krb5_context context,
1026 const krb5_realm *realm,
1027 char ***hostlist)
1029 return gethostlist(context, *realm, KRB5_KRBHST_ADMIN, hostlist);
1033 * return an malloced list of changepw-hosts for `realm' in `hostlist'
1036 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1037 krb5_get_krb_changepw_hst (krb5_context context,
1038 const krb5_realm *realm,
1039 char ***hostlist)
1041 return gethostlist(context, *realm, KRB5_KRBHST_CHANGEPW, hostlist);
1045 * return an malloced list of 524-hosts for `realm' in `hostlist'
1048 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1049 krb5_get_krb524hst (krb5_context context,
1050 const krb5_realm *realm,
1051 char ***hostlist)
1053 return gethostlist(context, *realm, KRB5_KRBHST_KRB524, hostlist);
1058 * return an malloced list of KDC's for `realm' in `hostlist'
1061 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1062 krb5_get_krbhst (krb5_context context,
1063 const krb5_realm *realm,
1064 char ***hostlist)
1066 return gethostlist(context, *realm, KRB5_KRBHST_KDC, hostlist);
1070 * free all the memory allocated in `hostlist'
1073 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1074 krb5_free_krbhst (krb5_context context,
1075 char **hostlist)
1077 char **p;
1079 for (p = hostlist; *p; ++p)
1080 free (*p);
1081 free (hostlist);
1082 return 0;