krb5: Red Hat gssproxy FILE ccache remove cred compatibility
[heimdal.git] / lib / krb5 / krbhst.c
blobe5e8054864749c99eb189b06c96840957a73496b
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, const char *sitename,
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 if (sitename)
97 snprintf(domain, sizeof(domain), "_%s._%s.%s._sites.%s.",
98 service, proto, sitename, realm);
99 else
100 snprintf(domain, sizeof(domain), "_%s._%s.%s.", service, proto, realm);
102 r = rk_dns_lookup(domain, dns_type);
103 if(r == NULL) {
104 _krb5_debug(context, 0,
105 "DNS lookup failed domain: %s", domain);
106 return KRB5_KDC_UNREACH;
109 for(num_srv = 0, rr = r->head; rr; rr = rr->next)
110 if(rr->type == rk_ns_t_srv)
111 num_srv++;
113 if (num_srv == 0) {
114 _krb5_debug(context, 0,
115 "DNS SRV RR lookup domain nodata: %s", domain);
116 rk_dns_free_data(r);
117 return KRB5_KDC_UNREACH;
120 *res = malloc(num_srv * sizeof(**res));
121 if(*res == NULL) {
122 rk_dns_free_data(r);
123 return krb5_enomem(context);
126 rk_dns_srv_order(r);
128 for(num_srv = 0, rr = r->head; rr; rr = rr->next)
129 if(rr->type == rk_ns_t_srv) {
130 krb5_krbhst_info *hi = NULL;
131 size_t len;
132 int invalid_tld = 1;
134 /* Test for top-level domain controlled interruptions */
135 if (!is_invalid_tld_srv_target(rr->u.srv->target)) {
136 invalid_tld = 0;
137 len = strlen(rr->u.srv->target);
138 hi = calloc(1, sizeof(*hi) + len);
140 if(hi == NULL) {
141 rk_dns_free_data(r);
142 while(--num_srv >= 0)
143 free((*res)[num_srv]);
144 free(*res);
145 *res = NULL;
146 if (invalid_tld) {
147 krb5_warnx(context,
148 "Domain lookup failed: "
149 "Realm %s needs immediate attention "
150 "see https://icann.org/namecollision",
151 realm);
152 return KRB5_KDC_UNREACH;
154 return krb5_enomem(context);
156 (*res)[num_srv++] = hi;
158 hi->proto = proto_num;
160 hi->def_port = def_port;
161 if (port != 0)
162 hi->port = port;
163 else
164 hi->port = rr->u.srv->port;
166 strlcpy(hi->hostname, rr->u.srv->target, len + 1);
169 *count = num_srv;
171 rk_dns_free_data(r);
172 return 0;
176 struct krb5_krbhst_data {
177 const char *config_param;
178 const char *srv_label;
179 char *realm;
180 unsigned int flags;
181 int def_port;
182 int port; /* hardwired port number if != 0 */
183 #define KD_CONFIG 0x0001
184 #define KD_SRV_UDP 0x0002
185 #define KD_SRV_TCP 0x0004
186 #define KD_SITE_SRV_UDP 0x0008
187 #define KD_SITE_SRV_TCP 0x0010
188 #define KD_SRV_HTTP 0x0020
189 #define KD_SRV_KKDCP 0x0040
190 #define KD_FALLBACK 0x0080
191 #define KD_CONFIG_EXISTS 0x0100
192 #define KD_LARGE_MSG 0x0200
193 #define KD_PLUGIN 0x0400
194 #define KD_HOSTNAMES 0x0800
195 krb5_error_code (*get_next)(krb5_context, struct krb5_krbhst_data *,
196 krb5_krbhst_info**);
198 char *hostname;
199 char *sitename;
200 unsigned int fallback_count;
202 struct krb5_krbhst_info *hosts, **index, **end;
205 static krb5_boolean
206 krbhst_empty(const struct krb5_krbhst_data *kd)
208 return kd->index == &kd->hosts;
212 * Return the default protocol for the `kd' (either TCP or UDP)
215 static int
216 krbhst_get_default_proto(struct krb5_krbhst_data *kd)
218 if (kd->flags & KD_LARGE_MSG)
219 return KRB5_KRBHST_TCP;
220 return KRB5_KRBHST_UDP;
223 static int
224 krbhst_get_default_port(struct krb5_krbhst_data *kd)
226 return kd->def_port;
233 KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
234 _krb5_krbhst_get_realm(krb5_krbhst_handle handle)
236 return handle->realm;
240 * parse `spec' into a krb5_krbhst_info, defaulting the port to `def_port'
241 * and forcing it to `port' if port != 0
244 static struct krb5_krbhst_info*
245 parse_hostspec(krb5_context context, struct krb5_krbhst_data *kd,
246 const char *spec, int def_port, int port)
248 const char *p = spec, *q;
249 struct krb5_krbhst_info *hi;
251 hi = calloc(1, sizeof(*hi) + strlen(spec));
252 if(hi == NULL)
253 return NULL;
255 hi->proto = krbhst_get_default_proto(kd);
257 if(strncmp(p, "http://", 7) == 0){
258 hi->proto = KRB5_KRBHST_HTTP;
259 p += 7;
260 } else if(strncmp(p, "http/", 5) == 0) {
261 hi->proto = KRB5_KRBHST_HTTP;
262 p += 5;
263 def_port = ntohs(krb5_getportbyname (context, "http", "tcp", 80));
264 }else if(strncmp(p, "tcp/", 4) == 0){
265 hi->proto = KRB5_KRBHST_TCP;
266 p += 4;
267 } else if(strncmp(p, "udp/", 4) == 0) {
268 hi->proto = KRB5_KRBHST_UDP;
269 p += 4;
272 if (p[0] == '[' && (q = strchr(p, ']')) != NULL) {
273 /* if address looks like [foo:bar] or [foo:bar]: its a ipv6
274 address, strip of [] */
275 memcpy(hi->hostname, &p[1], q - p - 1);
276 hi->hostname[q - p - 1] = '\0';
277 p = q + 1;
278 /* get trailing : */
279 if (p[0] == ':')
280 p++;
281 } else if(strsep_copy(&p, ":", hi->hostname, strlen(spec) + 1) < 0) {
282 /* copy everything before : */
283 free(hi);
284 return NULL;
286 /* get rid of trailing /, and convert to lower case */
287 hi->hostname[strcspn(hi->hostname, "/")] = '\0';
288 strlwr(hi->hostname);
290 hi->port = hi->def_port = def_port;
291 if(p != NULL && p[0]) {
292 char *end;
293 hi->port = strtol(p, &end, 0);
294 if(end == p) {
295 free(hi);
296 return NULL;
299 if (port)
300 hi->port = port;
301 return hi;
304 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
305 _krb5_free_krbhst_info(krb5_krbhst_info *hi)
307 if (hi->ai != NULL)
308 freeaddrinfo(hi->ai);
309 free(hi);
312 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
313 _krb5_krbhost_info_move(krb5_context context,
314 krb5_krbhst_info *from,
315 krb5_krbhst_info **to)
317 size_t hostnamelen = strlen(from->hostname);
318 /* trailing NUL is included in structure */
319 *to = calloc(1, sizeof(**to) + hostnamelen);
320 if (*to == NULL)
321 return krb5_enomem(context);
323 (*to)->proto = from->proto;
324 (*to)->port = from->port;
325 (*to)->def_port = from->def_port;
326 (*to)->ai = from->ai;
327 from->ai = NULL;
328 (*to)->next = NULL;
329 memcpy((*to)->hostname, from->hostname, hostnamelen + 1);
330 return 0;
334 static void
335 append_host_hostinfo(struct krb5_krbhst_data *kd, struct krb5_krbhst_info *host)
337 struct krb5_krbhst_info *h;
339 for(h = kd->hosts; h; h = h->next)
340 if(h->proto == host->proto &&
341 h->port == host->port &&
342 strcmp(h->hostname, host->hostname) == 0) {
343 _krb5_free_krbhst_info(host);
344 return;
347 * We should always initialize kd->end in common_init(), but static
348 * analyzers may not see that we do, and the compiler might conclude
349 * there's UB here.
351 if (kd->end)
352 *kd->end = host;
353 kd->end = &host->next;
356 static krb5_error_code
357 append_host_string(krb5_context context, struct krb5_krbhst_data *kd,
358 const char *host, int def_port, int port)
360 struct krb5_krbhst_info *hi;
362 hi = parse_hostspec(context, kd, host, def_port, port);
363 if(hi == NULL)
364 return krb5_enomem(context);
366 append_host_hostinfo(kd, hi);
367 return 0;
371 * return a readable representation of `host' in `hostname, hostlen'
374 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
375 krb5_krbhst_format_string(krb5_context context, const krb5_krbhst_info *host,
376 char *hostname, size_t hostlen)
378 const char *proto = "";
379 if(host->proto == KRB5_KRBHST_TCP)
380 proto = "tcp/";
381 else if(host->proto == KRB5_KRBHST_HTTP)
382 proto = "http://";
383 if (host->port != host->def_port)
384 snprintf(hostname, hostlen, "%s%s:%d", proto, host->hostname, (int)host->port);
385 else
386 snprintf(hostname, hostlen, "%s%s", proto, host->hostname);
387 return 0;
391 * create a getaddrinfo `hints' based on `proto'
394 static void
395 make_hints(struct addrinfo *hints, int proto)
397 memset(hints, 0, sizeof(*hints));
398 hints->ai_family = AF_UNSPEC;
399 switch(proto) {
400 case KRB5_KRBHST_UDP :
401 hints->ai_socktype = SOCK_DGRAM;
402 break;
403 case KRB5_KRBHST_HTTP :
404 case KRB5_KRBHST_TCP :
405 hints->ai_socktype = SOCK_STREAM;
406 break;
411 * Return an `struct addrinfo *' for a KDC host.
413 * Returns an the struct addrinfo in in that corresponds to the
414 * information in `host'. free:ing is handled by krb5_krbhst_free, so
415 * the returned ai must not be released.
417 * @ingroup krb5
420 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
421 krb5_krbhst_get_addrinfo(krb5_context context, krb5_krbhst_info *host,
422 struct addrinfo **ai)
424 int ret = 0;
426 if (host->ai == NULL) {
427 struct addrinfo hints;
428 char portstr[NI_MAXSERV];
430 snprintf (portstr, sizeof(portstr), "%d", host->port);
431 make_hints(&hints, host->proto);
433 if (krb5_config_get_bool(context, NULL, "libdefaults", "block_dns",
434 NULL)) {
435 hints.ai_flags &= ~AI_CANONNAME;
436 hints.ai_flags |= AI_NUMERICHOST|AI_NUMERICSERV;
438 ret = getaddrinfo(host->hostname, portstr, &hints, &host->ai);
439 if (ret) {
440 ret = krb5_eai_to_heim_errno(ret, errno);
441 goto out;
444 out:
445 *ai = host->ai;
446 return ret;
449 static krb5_boolean
450 get_next(struct krb5_krbhst_data *kd, krb5_krbhst_info **host)
452 struct krb5_krbhst_info *hi = kd ? *kd->index : NULL;
453 if(hi != NULL) {
454 *host = hi;
455 kd->index = &(*kd->index)->next;
456 return TRUE;
458 return FALSE;
461 static void
462 srv_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
463 const char *sitename, const char *proto, const char *service)
465 krb5_error_code ret;
466 krb5_krbhst_info **res;
467 int count, i;
469 if (krb5_realm_is_lkdc(kd->realm))
470 return;
472 ret = srv_find_realm(context, &res, &count, kd->realm, "SRV",
473 sitename, proto, service, kd->port);
474 _krb5_debug(context, 2, "searching DNS for realm %s %s.%s -> %d",
475 kd->realm, proto, service, ret);
476 if (ret)
477 return;
478 for(i = 0; i < count; i++)
479 append_host_hostinfo(kd, res[i]);
480 free(res);
484 * read the configuration for `conf_string', defaulting to kd->def_port and
485 * forcing it to `kd->port' if kd->port != 0
488 static void
489 config_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
490 const char *conf_string)
492 int i;
493 char **hostlist;
494 hostlist = krb5_config_get_strings(context, NULL,
495 "realms", kd->realm, conf_string, NULL);
497 _krb5_debug(context, 2, "configuration file for realm %s%s found",
498 kd->realm, hostlist ? "" : " not");
500 if(hostlist == NULL)
501 return;
502 kd->flags |= KD_CONFIG_EXISTS;
503 for(i = 0; hostlist && hostlist[i] != NULL; i++)
504 append_host_string(context, kd, hostlist[i], kd->def_port, kd->port);
506 krb5_config_free_strings(hostlist);
510 * as a fallback, look for `serv_string.kd->realm' (typically
511 * kerberos.REALM, kerberos-1.REALM, ...
512 * `port' is the default port for the service, and `proto' the
513 * protocol
516 static krb5_error_code
517 fallback_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
518 const char *serv_string, int port, int proto)
520 char *host = NULL;
521 int ret;
522 struct addrinfo *ai;
523 struct addrinfo hints;
524 char portstr[NI_MAXSERV];
526 ret = krb5_config_get_bool_default(context, NULL, KRB5_FALLBACK_DEFAULT,
527 "libdefaults", "use_fallback", NULL);
528 if (!ret) {
529 kd->flags |= KD_FALLBACK;
530 return 0;
533 _krb5_debug(context, 2, "fallback lookup %d for realm %s (service %s)",
534 kd->fallback_count, kd->realm, serv_string);
537 * Don't try forever in case the DNS server keep returning us
538 * entries (like wildcard entries or the .nu TLD)
540 * Also don't try LKDC realms since fallback wont work on them at all.
542 if(kd->fallback_count >= 5 || krb5_realm_is_lkdc(kd->realm)) {
543 kd->flags |= KD_FALLBACK;
544 return 0;
547 if(kd->fallback_count == 0)
548 ret = asprintf(&host, "%s.%s.", serv_string, kd->realm);
549 else
550 ret = asprintf(&host, "%s-%d.%s.",
551 serv_string, kd->fallback_count, kd->realm);
553 if (ret < 0 || host == NULL)
554 return krb5_enomem(context);
556 make_hints(&hints, proto);
557 snprintf(portstr, sizeof(portstr), "%d", port);
558 if (krb5_config_get_bool(context, NULL, "libdefaults", "block_dns",
559 NULL)) {
560 hints.ai_flags &= ~AI_CANONNAME;
561 hints.ai_flags |= AI_NUMERICHOST|AI_NUMERICSERV;
563 ret = getaddrinfo(host, portstr, &hints, &ai);
564 if (ret) {
565 /* no more hosts, so we're done here */
566 free(host);
567 kd->flags |= KD_FALLBACK;
568 } else {
569 struct krb5_krbhst_info *hi;
570 size_t hostlen;
572 /* Check for ICANN gTLD Name Collision address (127.0.53.53) */
573 if (ai->ai_family == AF_INET) {
574 struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
575 if (sin->sin_addr.s_addr == htonl(0x7f003535)) {
576 krb5_warnx(context,
577 "Fallback lookup failed: "
578 "Realm %s needs immediate attention "
579 "see https://icann.org/namecollision",
580 kd->realm);
581 free(host);
582 freeaddrinfo(ai);
583 return KRB5_KDC_UNREACH;
587 hostlen = strlen(host);
588 hi = calloc(1, sizeof(*hi) + hostlen);
589 if(hi == NULL) {
590 free(host);
591 freeaddrinfo(ai);
592 return krb5_enomem(context);
595 hi->proto = proto;
596 hi->port = hi->def_port = port;
597 hi->ai = ai;
598 memmove(hi->hostname, host, hostlen);
599 hi->hostname[hostlen] = '\0';
600 free(host);
601 append_host_hostinfo(kd, hi);
602 kd->fallback_count++;
604 return 0;
608 * Fetch hosts from plugin
611 static krb5_error_code
612 add_plugin_host(struct krb5_krbhst_data *kd,
613 const char *host,
614 const char *port,
615 int portnum,
616 int proto)
618 struct krb5_krbhst_info *hi;
619 struct addrinfo hints, *ai;
620 size_t hostlen;
621 int ret;
623 make_hints(&hints, proto);
624 ret = getaddrinfo(host, port, &hints, &ai);
625 if (ret)
626 return 0;
628 hostlen = strlen(host);
630 hi = calloc(1, sizeof(*hi) + hostlen);
631 if (hi == NULL) {
632 freeaddrinfo(ai);
633 return ENOMEM;
636 hi->proto = proto;
637 hi->port = hi->def_port = portnum;
638 hi->ai = ai;
639 memmove(hi->hostname, host, hostlen);
640 hi->hostname[hostlen] = '\0';
641 append_host_hostinfo(kd, hi);
643 return 0;
646 static krb5_error_code
647 add_locate(void *ctx, int type, struct sockaddr *addr)
649 struct krb5_krbhst_data *kd = ctx;
650 char host[NI_MAXHOST], port[NI_MAXSERV];
651 socklen_t socklen;
652 krb5_error_code ret;
653 int proto, portnum;
655 socklen = socket_sockaddr_size(addr);
656 portnum = socket_get_port(addr);
658 ret = getnameinfo(addr, socklen, host, sizeof(host), port, sizeof(port),
659 NI_NUMERICHOST|NI_NUMERICSERV|NI_NUMERICSCOPE);
660 if (ret != 0)
661 return 0;
663 if (kd->port)
664 snprintf(port, sizeof(port), "%d", kd->port);
665 else if (atoi(port) == 0)
666 snprintf(port, sizeof(port), "%d", krbhst_get_default_port(kd));
668 proto = krbhst_get_default_proto(kd);
670 ret = add_plugin_host(kd, host, port, portnum, proto);
671 if (ret)
672 return ret;
675 * This is really kind of broken and should be solved a different
676 * way, some sites block UDP, and we don't, in the general case,
677 * fall back to TCP, that should also be done. But since that
678 * should require us to invert the whole "find kdc" stack, let put
679 * this in for now.
682 if (proto == KRB5_KRBHST_UDP) {
683 ret = add_plugin_host(kd, host, port, portnum, KRB5_KRBHST_TCP);
684 if (ret)
685 return ret;
688 return 0;
691 struct plctx {
692 enum locate_service_type type;
693 struct krb5_krbhst_data *kd;
694 unsigned long flags;
697 static KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
698 plcallback(krb5_context context,
699 const void *plug, void *plugctx, void *userctx)
701 const krb5plugin_service_locate_ftable *locate = plug;
702 struct plctx *plctx = userctx;
704 if (locate->minor_version >= KRB5_PLUGIN_LOCATE_VERSION_2)
705 return locate->lookup(plugctx, plctx->flags, plctx->type, plctx->kd->realm, 0, 0, add_locate, plctx->kd);
707 if (plctx->flags & KRB5_PLF_ALLOW_HOMEDIR)
708 return locate->old_lookup(plugctx, plctx->type, plctx->kd->realm, 0, 0, add_locate, plctx->kd);
710 return KRB5_PLUGIN_NO_HANDLE;
713 static const char *const locate_plugin_deps[] = { "krb5", NULL };
715 static const struct heim_plugin_data
716 locate_plugin_data = {
717 "krb5",
718 KRB5_PLUGIN_LOCATE,
719 KRB5_PLUGIN_LOCATE_VERSION_0,
720 locate_plugin_deps,
721 krb5_get_instance
724 static void
725 plugin_get_hosts(krb5_context context,
726 struct krb5_krbhst_data *kd,
727 enum locate_service_type type)
729 struct plctx ctx = { type, kd, 0 };
732 * XXX Need a way to pass this through -- unsure if any of this is
733 * useful without DNS, though.
735 if (krb5_config_get_bool(context, NULL, "libdefaults", "block_dns", NULL))
736 return;
738 if (_krb5_homedir_access(context))
739 ctx.flags |= KRB5_PLF_ALLOW_HOMEDIR;
741 _krb5_plugin_run_f(context, &locate_plugin_data,
742 0, &ctx, plcallback);
749 static void
750 hostnames_get_hosts(krb5_context context,
751 struct krb5_krbhst_data *kd,
752 const char *type)
754 kd->flags |= KD_HOSTNAMES;
755 if (kd->hostname)
756 append_host_string(context, kd, kd->hostname, kd->def_port, kd->port);
764 static krb5_error_code
765 kdc_get_next(krb5_context context,
766 struct krb5_krbhst_data *kd,
767 krb5_krbhst_info **host)
769 krb5_error_code ret;
771 if ((kd->flags & KD_HOSTNAMES) == 0) {
772 hostnames_get_hosts(context, kd, "kdc");
773 if(get_next(kd, host))
774 return 0;
777 if ((kd->flags & KD_PLUGIN) == 0) {
778 plugin_get_hosts(context, kd, locate_service_kdc);
779 kd->flags |= KD_PLUGIN;
780 if(get_next(kd, host))
781 return 0;
784 if((kd->flags & KD_CONFIG) == 0) {
785 config_get_hosts(context, kd, kd->config_param);
786 kd->flags |= KD_CONFIG;
787 if(get_next(kd, host))
788 return 0;
791 if (kd->flags & KD_CONFIG_EXISTS) {
792 _krb5_debug(context, 1,
793 "Configuration exists for realm %s, wont go to DNS",
794 kd->realm);
795 return KRB5_KDC_UNREACH;
798 if (!krb5_config_get_bool(context, NULL, "libdefaults", "block_dns",
799 NULL) &&
800 context->srv_lookup) {
801 if(kd->sitename && (kd->flags & KD_SITE_SRV_TCP) == 0) {
802 srv_get_hosts(context, kd, kd->sitename, "tcp", "kerberos");
803 kd->flags |= KD_SITE_SRV_TCP;
804 if(get_next(kd, host))
805 return 0;
808 if((kd->flags & KD_SRV_UDP) == 0 && (kd->flags & KD_LARGE_MSG) == 0) {
809 srv_get_hosts(context, kd, NULL, "udp", kd->srv_label);
810 kd->flags |= KD_SRV_UDP;
811 if(get_next(kd, host))
812 return 0;
815 if((kd->flags & KD_SRV_TCP) == 0) {
816 srv_get_hosts(context, kd, NULL, "tcp", kd->srv_label);
817 kd->flags |= KD_SRV_TCP;
818 if(get_next(kd, host))
819 return 0;
821 if((kd->flags & KD_SRV_HTTP) == 0) {
822 srv_get_hosts(context, kd, NULL, "http", kd->srv_label);
823 kd->flags |= KD_SRV_HTTP;
824 if(get_next(kd, host))
825 return 0;
829 while((kd->flags & KD_FALLBACK) == 0) {
830 ret = fallback_get_hosts(context, kd, "kerberos",
831 kd->def_port,
832 krbhst_get_default_proto(kd));
833 if(ret)
834 return ret;
835 if(get_next(kd, host))
836 return 0;
839 _krb5_debug(context, 0, "No KDC entries found for %s", kd->realm);
841 return KRB5_KDC_UNREACH; /* XXX */
844 static krb5_error_code
845 admin_get_next(krb5_context context,
846 struct krb5_krbhst_data *kd,
847 krb5_krbhst_info **host)
849 krb5_error_code ret;
851 if ((kd->flags & KD_PLUGIN) == 0) {
852 plugin_get_hosts(context, kd, locate_service_kadmin);
853 kd->flags |= KD_PLUGIN;
854 if(get_next(kd, host))
855 return 0;
858 if((kd->flags & KD_CONFIG) == 0) {
859 config_get_hosts(context, kd, kd->config_param);
860 kd->flags |= KD_CONFIG;
861 if(get_next(kd, host))
862 return 0;
865 if (kd->flags & KD_CONFIG_EXISTS) {
866 _krb5_debug(context, 1,
867 "Configuration exists for realm %s, wont go to DNS",
868 kd->realm);
869 return KRB5_KDC_UNREACH;
872 if (!krb5_config_get_bool(context, NULL, "libdefaults", "block_dns",
873 NULL) &&
874 context->srv_lookup) {
875 if((kd->flags & KD_SRV_TCP) == 0) {
876 srv_get_hosts(context, kd, NULL, "tcp", kd->srv_label);
877 kd->flags |= KD_SRV_TCP;
878 if(get_next(kd, host))
879 return 0;
883 if (krbhst_empty(kd)
884 && (kd->flags & KD_FALLBACK) == 0) {
885 ret = fallback_get_hosts(context, kd, "kerberos",
886 kd->def_port,
887 krbhst_get_default_proto(kd));
888 if(ret)
889 return ret;
890 kd->flags |= KD_FALLBACK;
891 if(get_next(kd, host))
892 return 0;
895 _krb5_debug(context, 0, "No admin entries found for realm %s", kd->realm);
897 return KRB5_KDC_UNREACH; /* XXX */
900 static krb5_error_code
901 kpasswd_get_next(krb5_context context,
902 struct krb5_krbhst_data *kd,
903 krb5_krbhst_info **host)
905 krb5_error_code ret;
907 if ((kd->flags & KD_PLUGIN) == 0) {
908 plugin_get_hosts(context, kd, locate_service_kpasswd);
909 kd->flags |= KD_PLUGIN;
910 if(get_next(kd, host))
911 return 0;
914 if((kd->flags & KD_CONFIG) == 0) {
915 config_get_hosts(context, kd, kd->config_param);
916 kd->flags |= KD_CONFIG;
917 if(get_next(kd, host))
918 return 0;
921 if (kd->flags & KD_CONFIG_EXISTS) {
922 _krb5_debug(context, 1,
923 "Configuration exists for realm %s, wont go to DNS",
924 kd->realm);
925 return KRB5_KDC_UNREACH;
928 if (!krb5_config_get_bool(context, NULL, "libdefaults", "block_dns",
929 NULL) &&
930 context->srv_lookup) {
931 if((kd->flags & KD_SRV_UDP) == 0) {
932 srv_get_hosts(context, kd, NULL, "udp", kd->srv_label);
933 kd->flags |= KD_SRV_UDP;
934 if(get_next(kd, host))
935 return 0;
937 if((kd->flags & KD_SRV_TCP) == 0) {
938 srv_get_hosts(context, kd, NULL, "tcp", kd->srv_label);
939 kd->flags |= KD_SRV_TCP;
940 if(get_next(kd, host))
941 return 0;
945 /* no matches -> try admin */
947 if (krbhst_empty(kd)) {
948 kd->flags = 0;
949 kd->port = kd->def_port;
950 kd->get_next = admin_get_next;
951 ret = (*kd->get_next)(context, kd, host);
952 if (ret == 0)
953 (*host)->proto = krbhst_get_default_proto(kd);
954 return ret;
957 _krb5_debug(context, 0, "No kpasswd entries found for realm %s", kd->realm);
959 return KRB5_KDC_UNREACH;
962 static void KRB5_CALLCONV
963 krbhost_dealloc(void *ptr)
965 struct krb5_krbhst_data *handle = (struct krb5_krbhst_data *)ptr;
966 krb5_krbhst_info *h, *next;
968 for (h = handle->hosts; h != NULL; h = next) {
969 next = h->next;
970 _krb5_free_krbhst_info(h);
972 if (handle->hostname)
973 free(handle->hostname);
974 if (handle->sitename)
975 free(handle->sitename);
977 free(handle->realm);
980 static struct krb5_krbhst_data*
981 common_init(krb5_context context,
982 const char *config_param,
983 const char *srv_label,
984 const char *service,
985 const char *realm,
986 int flags)
988 struct krb5_krbhst_data *kd;
990 if ((kd = heim_alloc(sizeof(*kd), "krbhst-context", krbhost_dealloc)) == NULL)
991 return NULL;
993 if((kd->realm = strdup(realm)) == NULL) {
994 heim_release(kd);
995 return NULL;
998 kd->config_param = config_param;
999 kd->srv_label = srv_label;
1001 _krb5_debug(context, 2, "Trying to find service %s for realm %s flags %x",
1002 service, realm, flags);
1004 /* For 'realms' without a . do not even think of going to DNS */
1005 if (!strchr(realm, '.'))
1006 kd->flags |= KD_CONFIG_EXISTS;
1008 if (flags & KRB5_KRBHST_FLAGS_LARGE_MSG)
1009 kd->flags |= KD_LARGE_MSG;
1010 kd->end = kd->index = &kd->hosts;
1011 return kd;
1015 * initialize `handle' to look for hosts of type `type' in realm `realm'
1018 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1019 krb5_krbhst_init(krb5_context context,
1020 const char *realm,
1021 unsigned int type,
1022 krb5_krbhst_handle *handle)
1024 return krb5_krbhst_init_flags(context, realm, type, 0, handle);
1027 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1028 krb5_krbhst_init_flags(krb5_context context,
1029 const char *realm,
1030 unsigned int type,
1031 int flags,
1032 krb5_krbhst_handle *handle)
1034 struct krb5_krbhst_data *kd;
1035 krb5_error_code (*next)(krb5_context, struct krb5_krbhst_data *,
1036 krb5_krbhst_info **);
1037 int def_port;
1038 const char *config_param;
1039 const char *srv_label;
1040 const char *service;
1042 *handle = NULL;
1044 switch(type) {
1045 case KRB5_KRBHST_KDC:
1046 next = kdc_get_next;
1047 def_port = ntohs(krb5_getportbyname(context, "kerberos", "udp", 88));
1048 config_param = "kdc";
1049 srv_label = "kerberos";
1050 service = "kdc";
1051 break;
1052 case KRB5_KRBHST_ADMIN:
1053 next = admin_get_next;
1054 def_port = ntohs(krb5_getportbyname(context, "kerberos-adm",
1055 "tcp", 749));
1056 config_param = "admin_server";
1057 srv_label = "kerberos-adm";
1058 service = "admin";
1059 break;
1060 case KRB5_KRBHST_READONLY_ADMIN:
1061 next = admin_get_next;
1062 def_port = ntohs(krb5_getportbyname(context, "kerberos-adm",
1063 "tcp", 749));
1064 config_param = "readonly_admin_server";
1065 srv_label = "kerberos-adm-readonly";
1066 service = "admin";
1067 break;
1068 case KRB5_KRBHST_CHANGEPW:
1069 next = kpasswd_get_next;
1070 def_port = ntohs(krb5_getportbyname(context, "kpasswd", "udp",
1071 KPASSWD_PORT));
1072 config_param = "kpasswd_server";
1073 srv_label = "kpasswd";
1074 service = "change_password";
1075 break;
1076 case KRB5_KRBHST_TKTBRIDGEAP:
1077 next = kdc_get_next;
1078 def_port = ntohs(krb5_getportbyname(context, "kerberos", "tcp", 88));
1079 config_param = "tktbridgeap";
1080 srv_label = "kerberos-tkt-bridge";
1081 service = "kdc";
1082 break;
1083 default:
1084 krb5_set_error_message(context, ENOTTY,
1085 N_("unknown krbhst type (%u)", ""), type);
1086 return ENOTTY;
1088 if((kd = common_init(context, config_param, srv_label, service, realm,
1089 flags)) == NULL)
1090 return ENOMEM;
1091 kd->get_next = next;
1092 kd->def_port = def_port;
1093 *handle = kd;
1094 return 0;
1098 * return the next host information from `handle' in `host'
1101 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1102 krb5_krbhst_next(krb5_context context,
1103 krb5_krbhst_handle handle,
1104 krb5_krbhst_info **host)
1106 if(get_next(handle, host))
1107 return 0;
1109 return (*handle->get_next)(context, handle, host);
1113 * return the next host information from `handle' as a host name
1114 * in `hostname' (or length `hostlen)
1117 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1118 krb5_krbhst_next_as_string(krb5_context context,
1119 krb5_krbhst_handle handle,
1120 char *hostname,
1121 size_t hostlen)
1123 krb5_error_code ret;
1124 krb5_krbhst_info *host;
1125 ret = krb5_krbhst_next(context, handle, &host);
1126 if(ret)
1127 return ret;
1128 return krb5_krbhst_format_string(context, host, hostname, hostlen);
1135 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1136 krb5_krbhst_set_hostname(krb5_context context,
1137 krb5_krbhst_handle handle,
1138 const char *hostname)
1140 if (handle->hostname)
1141 free(handle->hostname);
1142 handle->hostname = strdup(hostname);
1143 if (handle->hostname == NULL)
1144 return ENOMEM;
1145 return 0;
1148 krb5_error_code KRB5_LIB_FUNCTION
1149 krb5_krbhst_set_sitename(krb5_context context,
1150 krb5_krbhst_handle handle,
1151 const char *sitename)
1153 if (handle->sitename)
1154 free(handle->sitename);
1155 handle->sitename = strdup(sitename);
1156 if (handle->sitename == NULL)
1157 return krb5_enomem(context);
1158 return 0;
1161 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1162 krb5_krbhst_reset(krb5_context context, krb5_krbhst_handle handle)
1164 handle->index = &handle->hosts;
1167 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1168 krb5_krbhst_free(krb5_context context, krb5_krbhst_handle handle)
1170 heim_release(handle);
1173 #ifndef HEIMDAL_SMALLER
1175 /* backwards compatibility ahead */
1177 static krb5_error_code
1178 gethostlist(krb5_context context, const char *realm,
1179 unsigned int type, char ***hostlist)
1181 krb5_error_code ret;
1182 int nhost = 0;
1183 krb5_krbhst_handle handle;
1184 char host[MAXHOSTNAMELEN];
1185 krb5_krbhst_info *hostinfo;
1187 ret = krb5_krbhst_init(context, realm, type, &handle);
1188 if (ret)
1189 return ret;
1191 while (krb5_krbhst_next(context, handle, &hostinfo) == 0)
1192 nhost++;
1193 if (nhost == 0) {
1194 krb5_set_error_message(context, KRB5_KDC_UNREACH,
1195 N_("No KDC found for realm %s", ""), realm);
1196 krb5_krbhst_free(context, handle);
1197 return KRB5_KDC_UNREACH;
1199 *hostlist = calloc(nhost + 1, sizeof(**hostlist));
1200 if (*hostlist == NULL) {
1201 krb5_krbhst_free(context, handle);
1202 return krb5_enomem(context);
1205 krb5_krbhst_reset(context, handle);
1206 nhost = 0;
1207 while (krb5_krbhst_next_as_string(context, handle,
1208 host, sizeof(host)) == 0) {
1209 if (((*hostlist)[nhost++] = strdup(host)) == NULL) {
1210 krb5_free_krbhst(context, *hostlist);
1211 krb5_krbhst_free(context, handle);
1212 return krb5_enomem(context);
1215 (*hostlist)[nhost] = NULL;
1216 krb5_krbhst_free(context, handle);
1217 return 0;
1221 * Return a malloced list of kadmin-hosts for `realm' in `hostlist'
1224 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1225 krb5_get_krb_admin_hst(krb5_context context,
1226 const krb5_realm *realm,
1227 char ***hostlist)
1229 return gethostlist(context, *realm, KRB5_KRBHST_ADMIN, hostlist);
1233 * Return a malloced list of writable kadmin-hosts for `realm' in `hostlist'
1236 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1237 krb5_get_krb_readonly_admin_hst(krb5_context context,
1238 const krb5_realm *realm,
1239 char ***hostlist)
1241 return gethostlist(context, *realm, KRB5_KRBHST_READONLY_ADMIN, hostlist);
1245 * return an malloced list of changepw-hosts for `realm' in `hostlist'
1248 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1249 krb5_get_krb_changepw_hst (krb5_context context,
1250 const krb5_realm *realm,
1251 char ***hostlist)
1253 return gethostlist(context, *realm, KRB5_KRBHST_CHANGEPW, hostlist);
1257 * return an malloced list of 524-hosts for `realm' in `hostlist'
1260 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1261 krb5_get_krb524hst (krb5_context context,
1262 const krb5_realm *realm,
1263 char ***hostlist)
1265 return gethostlist(context, *realm, KRB5_KRBHST_KRB524, hostlist);
1269 * return an malloced list of KDC's for `realm' in `hostlist'
1272 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1273 krb5_get_krbhst (krb5_context context,
1274 const krb5_realm *realm,
1275 char ***hostlist)
1277 return gethostlist(context, *realm, KRB5_KRBHST_KDC, hostlist);
1281 * free all the memory allocated in `hostlist'
1284 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1285 krb5_free_krbhst (krb5_context context,
1286 char **hostlist)
1288 char **p;
1290 for (p = hostlist; *p; ++p)
1291 free (*p);
1292 free (hostlist);
1293 return 0;
1296 #endif /* HEIMDAL_SMALLER */