hdb: read_master_key use free_master_key on error
[heimdal.git] / lib / krb5 / krbhst.c
blob51cd3dcc7ebae6496e6a0a3446ee24ab5d9ed9f9
1 /*
2 * Copyright (c) 2001 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "krb5_locl.h"
37 #include <resolve.h>
38 #include "locate_plugin.h"
40 static int
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;
49 return -1;
52 static int
53 is_invalid_tld_srv_target(const char *target)
55 return (strncmp("your-dns-needs-immediate-attention.",
56 target, 35) == 0
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)
71 char domain[1024];
72 struct rk_dns_reply *r;
73 struct rk_resource_record *rr;
74 int num_srv;
75 int proto_num;
76 int def_port;
78 *res = NULL;
79 *count = 0;
81 proto_num = string_to_proto(proto);
82 if(proto_num < 0) {
83 krb5_set_error_message(context, EINVAL,
84 N_("unknown protocol `%s' to lookup", ""),
85 proto);
86 return EINVAL;
89 if(proto_num == KRB5_KRBHST_HTTP)
90 def_port = ntohs(krb5_getportbyname (context, "http", "tcp", 80));
91 else if(port == 0)
92 def_port = ntohs(krb5_getportbyname (context, service, proto, 88));
93 else
94 def_port = port;
96 snprintf(domain, sizeof(domain), "_%s._%s.%s.", service, proto, realm);
98 r = rk_dns_lookup(domain, dns_type);
99 if(r == NULL) {
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)
107 num_srv++;
109 *res = malloc(num_srv * sizeof(**res));
110 if(*res == NULL) {
111 rk_dns_free_data(r);
112 return krb5_enomem(context);
115 rk_dns_srv_order(r);
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;
120 size_t len;
121 int invalid_tld = 1;
123 /* Test for top-level domain controlled interruptions */
124 if (!is_invalid_tld_srv_target(rr->u.srv->target)) {
125 invalid_tld = 0;
126 len = strlen(rr->u.srv->target);
127 hi = calloc(1, sizeof(*hi) + len);
129 if(hi == NULL) {
130 rk_dns_free_data(r);
131 while(--num_srv >= 0)
132 free((*res)[num_srv]);
133 free(*res);
134 *res = NULL;
135 if (invalid_tld) {
136 krb5_warnx(context,
137 "Domain lookup failed: "
138 "Realm %s needs immediate attention "
139 "see https://icann.org/namecollision",
140 realm);
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;
150 if (port != 0)
151 hi->port = port;
152 else
153 hi->port = rr->u.srv->port;
155 strlcpy(hi->hostname, rr->u.srv->target, len + 1);
158 *count = num_srv;
160 rk_dns_free_data(r);
161 return 0;
165 struct krb5_krbhst_data {
166 char *realm;
167 unsigned int flags;
168 int def_port;
169 int port; /* hardwired port number if != 0 */
170 #define KD_CONFIG 1
171 #define KD_SRV_UDP 2
172 #define KD_SRV_TCP 4
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 *,
180 krb5_krbhst_info**);
182 char *hostname;
183 unsigned int fallback_count;
185 struct krb5_krbhst_info *hosts, **index, **end;
188 static krb5_boolean
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)
198 static int
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;
206 static int
207 krbhst_get_default_port(struct krb5_krbhst_data *kd)
209 return kd->def_port;
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));
235 if(hi == NULL)
236 return NULL;
238 hi->proto = krbhst_get_default_proto(kd);
240 if(strncmp(p, "http://", 7) == 0){
241 hi->proto = KRB5_KRBHST_HTTP;
242 p += 7;
243 } else if(strncmp(p, "http/", 5) == 0) {
244 hi->proto = KRB5_KRBHST_HTTP;
245 p += 5;
246 def_port = ntohs(krb5_getportbyname (context, "http", "tcp", 80));
247 }else if(strncmp(p, "tcp/", 4) == 0){
248 hi->proto = KRB5_KRBHST_TCP;
249 p += 4;
250 } else if(strncmp(p, "udp/", 4) == 0) {
251 hi->proto = KRB5_KRBHST_UDP;
252 p += 4;
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';
260 p = q + 1;
261 /* get trailing : */
262 if (p[0] == ':')
263 p++;
264 } else if(strsep_copy(&p, ":", hi->hostname, strlen(spec) + 1) < 0) {
265 /* copy everything before : */
266 free(hi);
267 return NULL;
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]) {
275 char *end;
276 hi->port = strtol(p, &end, 0);
277 if(end == p) {
278 free(hi);
279 return NULL;
282 if (port)
283 hi->port = port;
284 return hi;
287 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
288 _krb5_free_krbhst_info(krb5_krbhst_info *hi)
290 if (hi->ai != NULL)
291 freeaddrinfo(hi->ai);
292 free(hi);
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);
303 if (*to == NULL)
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;
310 from->ai = NULL;
311 (*to)->next = NULL;
312 memcpy((*to)->hostname, from->hostname, hostnamelen + 1);
313 return 0;
317 static void
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);
327 return;
329 *kd->end = 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);
340 if(hi == NULL)
341 return krb5_enomem(context);
343 append_host_hostinfo(kd, hi);
344 return 0;
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)
358 proto = "tcp/";
359 else if(host->proto == KRB5_KRBHST_HTTP)
360 proto = "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);
364 return 0;
368 * create a getaddrinfo `hints' based on `proto'
371 static void
372 make_hints(struct addrinfo *hints, int proto)
374 memset(hints, 0, sizeof(*hints));
375 hints->ai_family = AF_UNSPEC;
376 switch(proto) {
377 case KRB5_KRBHST_UDP :
378 hints->ai_socktype = SOCK_DGRAM;
379 break;
380 case KRB5_KRBHST_HTTP :
381 case KRB5_KRBHST_TCP :
382 hints->ai_socktype = SOCK_STREAM;
383 break;
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.
394 * @ingroup krb5
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)
401 int ret = 0;
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);
411 if (ret) {
412 ret = krb5_eai_to_heim_errno(ret, errno);
413 goto out;
416 out:
417 *ai = host->ai;
418 return ret;
421 static krb5_boolean
422 get_next(struct krb5_krbhst_data *kd, krb5_krbhst_info **host)
424 struct krb5_krbhst_info *hi = *kd->index;
425 if(hi != NULL) {
426 *host = hi;
427 kd->index = &(*kd->index)->next;
428 return TRUE;
430 return FALSE;
433 static void
434 srv_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
435 const char *proto, const char *service)
437 krb5_error_code ret;
438 krb5_krbhst_info **res;
439 int count, i;
441 if (krb5_realm_is_lkdc(kd->realm))
442 return;
444 ret = srv_find_realm(context, &res, &count, kd->realm, "SRV", proto, service,
445 kd->port);
446 _krb5_debug(context, 2, "searching DNS for realm %s %s.%s -> %d",
447 kd->realm, proto, service, ret);
448 if (ret)
449 return;
450 for(i = 0; i < count; i++)
451 append_host_hostinfo(kd, res[i]);
452 free(res);
456 * read the configuration for `conf_string', defaulting to kd->def_port and
457 * forcing it to `kd->port' if kd->port != 0
460 static void
461 config_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
462 const char *conf_string)
464 int i;
465 char **hostlist;
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");
472 if(hostlist == NULL)
473 return;
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
485 * protocol
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)
492 char *host = NULL;
493 int ret;
494 struct addrinfo *ai;
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);
500 if (!ret) {
501 kd->flags |= KD_FALLBACK;
502 return 0;
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;
516 return 0;
519 if(kd->fallback_count == 0)
520 ret = asprintf(&host, "%s.%s.", serv_string, kd->realm);
521 else
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);
531 if (ret) {
532 /* no more hosts, so we're done here */
533 free(host);
534 kd->flags |= KD_FALLBACK;
535 } else {
536 struct krb5_krbhst_info *hi;
537 size_t hostlen;
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)) {
543 krb5_warnx(context,
544 "Fallback lookup failed: "
545 "Realm %s needs immediate attention "
546 "see https://icann.org/namecollision",
547 kd->realm);
548 return KRB5_KDC_UNREACH;
552 hostlen = strlen(host);
553 hi = calloc(1, sizeof(*hi) + hostlen);
554 if(hi == NULL) {
555 free(host);
556 return krb5_enomem(context);
559 hi->proto = proto;
560 hi->port = hi->def_port = port;
561 hi->ai = ai;
562 memmove(hi->hostname, host, hostlen);
563 hi->hostname[hostlen] = '\0';
564 free(host);
565 append_host_hostinfo(kd, hi);
566 kd->fallback_count++;
568 return 0;
572 * Fetch hosts from plugin
575 static krb5_error_code
576 add_plugin_host(struct krb5_krbhst_data *kd,
577 const char *host,
578 const char *port,
579 int portnum,
580 int proto)
582 struct krb5_krbhst_info *hi;
583 struct addrinfo hints, *ai;
584 size_t hostlen;
585 int ret;
587 make_hints(&hints, proto);
588 ret = getaddrinfo(host, port, &hints, &ai);
589 if (ret)
590 return 0;
592 hostlen = strlen(host);
594 hi = calloc(1, sizeof(*hi) + hostlen);
595 if(hi == NULL)
596 return ENOMEM;
598 hi->proto = proto;
599 hi->port = hi->def_port = portnum;
600 hi->ai = ai;
601 memmove(hi->hostname, host, hostlen);
602 hi->hostname[hostlen] = '\0';
603 append_host_hostinfo(kd, hi);
605 return 0;
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];
613 socklen_t socklen;
614 krb5_error_code ret;
615 int proto, portnum;
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);
622 if (ret != 0)
623 return 0;
625 if (kd->port)
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);
633 if (ret)
634 return ret;
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
641 * this in for now.
644 if (proto == KRB5_KRBHST_UDP) {
645 ret = add_plugin_host(kd, host, port, portnum, KRB5_KRBHST_TCP);
646 if (ret)
647 return ret;
650 return 0;
653 struct plctx {
654 enum locate_service_type type;
655 struct krb5_krbhst_data *kd;
656 unsigned long flags;
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;
675 static void
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);
694 static void
695 hostnames_get_hosts(krb5_context context,
696 struct krb5_krbhst_data *kd,
697 const char *type)
699 kd->flags |= KD_HOSTNAMES;
700 if (kd->hostname)
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)
714 krb5_error_code ret;
716 if ((kd->flags & KD_HOSTNAMES) == 0) {
717 hostnames_get_hosts(context, kd, "kdc");
718 if(get_next(kd, host))
719 return 0;
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))
726 return 0;
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))
733 return 0;
736 if (kd->flags & KD_CONFIG_EXISTS) {
737 _krb5_debug(context, 1,
738 "Configuration exists for realm %s, wont go to DNS",
739 kd->realm);
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))
748 return 0;
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))
755 return 0;
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))
761 return 0;
765 while((kd->flags & KD_FALLBACK) == 0) {
766 ret = fallback_get_hosts(context, kd, "kerberos",
767 kd->def_port,
768 krbhst_get_default_proto(kd));
769 if(ret)
770 return ret;
771 if(get_next(kd, host))
772 return 0;
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)
785 krb5_error_code ret;
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))
791 return 0;
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))
798 return 0;
801 if (kd->flags & KD_CONFIG_EXISTS) {
802 _krb5_debug(context, 1,
803 "Configuration exists for realm %s, wont go to DNS",
804 kd->realm);
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))
813 return 0;
817 if (krbhst_empty(kd)
818 && (kd->flags & KD_FALLBACK) == 0) {
819 ret = fallback_get_hosts(context, kd, "kerberos",
820 kd->def_port,
821 krbhst_get_default_proto(kd));
822 if(ret)
823 return ret;
824 kd->flags |= KD_FALLBACK;
825 if(get_next(kd, host))
826 return 0;
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)
839 krb5_error_code ret;
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))
845 return 0;
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))
852 return 0;
855 if (kd->flags & KD_CONFIG_EXISTS) {
856 _krb5_debug(context, 1,
857 "Configuration exists for realm %s, wont go to DNS",
858 kd->realm);
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))
867 return 0;
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))
873 return 0;
877 /* no matches -> try admin */
879 if (krbhst_empty(kd)) {
880 kd->flags = 0;
881 kd->port = kd->def_port;
882 kd->get_next = admin_get_next;
883 ret = (*kd->get_next)(context, kd, host);
884 if (ret == 0)
885 (*host)->proto = krbhst_get_default_proto(kd);
886 return ret;
889 _krb5_debug(context, 0, "No kpasswd entries found for realm %s", kd->realm);
891 return KRB5_KDC_UNREACH;
894 static void
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) {
901 next = h->next;
902 _krb5_free_krbhst_info(h);
904 if (handle->hostname)
905 free(handle->hostname);
907 free(handle->realm);
910 static struct krb5_krbhst_data*
911 common_init(krb5_context context,
912 const char *service,
913 const char *realm,
914 int flags)
916 struct krb5_krbhst_data *kd;
918 if ((kd = heim_alloc(sizeof(*kd), "krbhst-context", krbhost_dealloc)) == NULL)
919 return NULL;
921 if((kd->realm = strdup(realm)) == NULL) {
922 heim_release(kd);
923 return 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;
936 return kd;
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,
945 const char *realm,
946 unsigned int type,
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,
954 const char *realm,
955 unsigned int type,
956 int flags,
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 **);
962 int def_port;
963 const char *service;
965 *handle = NULL;
967 switch(type) {
968 case KRB5_KRBHST_KDC:
969 next = kdc_get_next;
970 def_port = ntohs(krb5_getportbyname (context, "kerberos", "udp", 88));
971 service = "kdc";
972 break;
973 case KRB5_KRBHST_ADMIN:
974 next = admin_get_next;
975 def_port = ntohs(krb5_getportbyname (context, "kerberos-adm",
976 "tcp", 749));
977 service = "admin";
978 break;
979 case KRB5_KRBHST_CHANGEPW:
980 next = kpasswd_get_next;
981 def_port = ntohs(krb5_getportbyname (context, "kpasswd", "udp",
982 KPASSWD_PORT));
983 service = "change_password";
984 break;
985 default:
986 krb5_set_error_message(context, ENOTTY,
987 N_("unknown krbhst type (%u)", ""), type);
988 return ENOTTY;
990 if((kd = common_init(context, service, realm, flags)) == NULL)
991 return ENOMEM;
992 kd->get_next = next;
993 kd->def_port = def_port;
994 *handle = kd;
995 return 0;
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))
1008 return 0;
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,
1021 char *hostname,
1022 size_t hostlen)
1024 krb5_error_code ret;
1025 krb5_krbhst_info *host;
1026 ret = krb5_krbhst_next(context, handle, &host);
1027 if(ret)
1028 return ret;
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)
1045 return ENOMEM;
1046 return 0;
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;
1070 int nhost = 0;
1071 krb5_krbhst_handle handle;
1072 char host[MAXHOSTNAMELEN];
1073 krb5_krbhst_info *hostinfo;
1075 ret = krb5_krbhst_init(context, realm, type, &handle);
1076 if (ret)
1077 return ret;
1079 while(krb5_krbhst_next(context, handle, &hostinfo) == 0)
1080 nhost++;
1081 if(nhost == 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);
1093 nhost = 0;
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);
1104 return 0;
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,
1114 char ***hostlist)
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,
1126 char ***hostlist)
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,
1138 char ***hostlist)
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,
1150 char ***hostlist)
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,
1161 char **hostlist)
1163 char **p;
1165 for (p = hostlist; *p; ++p)
1166 free (*p);
1167 free (hostlist);
1168 return 0;
1171 #endif /* HEIMDAL_SMALLER */