2 * Copyright (c) 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5_locl.h"
37 RCSID("$Id: krbhst.c,v 1.41 2002/08/16 18:48:19 nectar Exp $");
40 string_to_proto(const char *string)
42 if(strcasecmp(string, "udp") == 0)
43 return KRB5_KRBHST_UDP;
44 else if(strcasecmp(string, "tcp") == 0)
45 return KRB5_KRBHST_TCP;
46 else if(strcasecmp(string, "http") == 0)
47 return KRB5_KRBHST_HTTP;
52 * set `res' and `count' to the result of looking up SRV RR in DNS for
53 * `proto', `proto', `realm' using `dns_type'.
54 * if `port' != 0, force that port number
57 static krb5_error_code
58 srv_find_realm(krb5_context context, krb5_krbhst_info ***res, int *count,
59 const char *realm, const char *dns_type,
60 const char *proto, const char *service, int port)
64 struct resource_record *rr;
69 proto_num = string_to_proto(proto);
71 krb5_set_error_string(context, "unknown protocol `%s'", proto);
75 if(proto_num == KRB5_KRBHST_HTTP)
76 def_port = ntohs(krb5_getportbyname (context, "http", "tcp", 80));
78 def_port = ntohs(krb5_getportbyname (context, service, proto, 88));
82 snprintf(domain, sizeof(domain), "_%s._%s.%s.", service, proto, realm);
84 r = dns_lookup(domain, dns_type);
88 return KRB5_KDC_UNREACH;
91 for(num_srv = 0, rr = r->head; rr; rr = rr->next)
95 *res = malloc(num_srv * sizeof(**res));
98 krb5_set_error_string(context, "malloc: out of memory");
104 for(num_srv = 0, rr = r->head; rr; rr = rr->next)
105 if(rr->type == T_SRV) {
106 krb5_krbhst_info *hi;
107 hi = calloc(1, sizeof(*hi) + strlen(rr->u.srv->target));
110 while(--num_srv >= 0)
111 free((*res)[num_srv]);
115 (*res)[num_srv++] = hi;
117 hi->proto = proto_num;
119 hi->def_port = def_port;
123 hi->port = rr->u.srv->port;
125 strcpy(hi->hostname, rr->u.srv->target);
135 struct krb5_krbhst_data {
139 int port; /* hardwired port number if != 0 */
143 #define KD_SRV_HTTP 8
144 #define KD_FALLBACK 16
145 #define KD_CONFIG_EXISTS 32
147 krb5_error_code (*get_next)(krb5_context, struct krb5_krbhst_data *,
150 unsigned int fallback_count;
152 struct krb5_krbhst_info *hosts, **index, **end;
156 krbhst_empty(const struct krb5_krbhst_data *kd)
158 return kd->index == &kd->hosts;
162 * parse `spec' into a krb5_krbhst_info, defaulting the port to `def_port'
163 * and forcing it to `port' if port != 0
166 static struct krb5_krbhst_info*
167 parse_hostspec(krb5_context context, const char *spec, int def_port, int port)
169 const char *p = spec;
170 struct krb5_krbhst_info *hi;
172 hi = calloc(1, sizeof(*hi) + strlen(spec));
176 hi->proto = KRB5_KRBHST_UDP;
178 if(strncmp(p, "http://", 7) == 0){
179 hi->proto = KRB5_KRBHST_HTTP;
181 } else if(strncmp(p, "http/", 5) == 0) {
182 hi->proto = KRB5_KRBHST_HTTP;
184 def_port = ntohs(krb5_getportbyname (context, "http", "tcp", 80));
185 }else if(strncmp(p, "tcp/", 4) == 0){
186 hi->proto = KRB5_KRBHST_TCP;
188 } else if(strncmp(p, "udp/", 4) == 0) {
192 if(strsep_copy(&p, ":", hi->hostname, strlen(spec) + 1) < 0) {
196 /* get rid of trailing /, and convert to lower case */
197 hi->hostname[strcspn(hi->hostname, "/")] = '\0';
198 strlwr(hi->hostname);
200 hi->port = hi->def_port = def_port;
203 hi->port = strtol(p, &end, 0);
215 free_krbhst_info(krb5_krbhst_info *hi)
218 freeaddrinfo(hi->ai);
223 append_host_hostinfo(struct krb5_krbhst_data *kd, struct krb5_krbhst_info *host)
225 struct krb5_krbhst_info *h;
227 for(h = kd->hosts; h; h = h->next)
228 if(h->proto == host->proto &&
229 h->port == host->port &&
230 strcmp(h->hostname, host->hostname) == 0) {
231 free_krbhst_info(host);
235 kd->end = &host->next;
238 static krb5_error_code
239 append_host_string(krb5_context context, struct krb5_krbhst_data *kd,
240 const char *host, int def_port, int port)
242 struct krb5_krbhst_info *hi;
244 hi = parse_hostspec(context, host, def_port, port);
248 append_host_hostinfo(kd, hi);
253 * return a readable representation of `host' in `hostname, hostlen'
257 krb5_krbhst_format_string(krb5_context context, const krb5_krbhst_info *host,
258 char *hostname, size_t hostlen)
260 const char *proto = "";
261 char portstr[7] = "";
262 if(host->proto == KRB5_KRBHST_TCP)
264 else if(host->proto == KRB5_KRBHST_HTTP)
266 if(host->port != host->def_port)
267 snprintf(portstr, sizeof(portstr), ":%d", host->port);
268 snprintf(hostname, hostlen, "%s%s%s", proto, host->hostname, portstr);
273 * create a getaddrinfo `hints' based on `proto'
277 make_hints(struct addrinfo *hints, int proto)
279 memset(hints, 0, sizeof(*hints));
280 hints->ai_family = AF_UNSPEC;
282 case KRB5_KRBHST_UDP :
283 hints->ai_socktype = SOCK_DGRAM;
285 case KRB5_KRBHST_HTTP :
286 case KRB5_KRBHST_TCP :
287 hints->ai_socktype = SOCK_STREAM;
293 * return an `struct addrinfo *' in `ai' corresponding to the information
294 * in `host'. free:ing is handled by krb5_krbhst_free.
298 krb5_krbhst_get_addrinfo(krb5_context context, krb5_krbhst_info *host,
299 struct addrinfo **ai)
301 struct addrinfo hints;
302 char portstr[NI_MAXSERV];
305 if (host->ai == NULL) {
306 make_hints(&hints, host->proto);
307 snprintf (portstr, sizeof(portstr), "%d", host->port);
308 ret = getaddrinfo(host->hostname, portstr, &hints, &host->ai);
310 return krb5_eai_to_heim_errno(ret, errno);
317 get_next(struct krb5_krbhst_data *kd, krb5_krbhst_info **host)
319 struct krb5_krbhst_info *hi = *kd->index;
322 kd->index = &(*kd->index)->next;
329 srv_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
330 const char *proto, const char *service)
332 krb5_krbhst_info **res;
335 srv_find_realm(context, &res, &count, kd->realm, "SRV", proto, service,
337 for(i = 0; i < count; i++)
338 append_host_hostinfo(kd, res[i]);
343 * read the configuration for `conf_string', defaulting to kd->def_port and
344 * forcing it to `kd->port' if kd->port != 0
348 config_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
349 const char *conf_string)
354 hostlist = krb5_config_get_strings(context, NULL,
355 "realms", kd->realm, conf_string, NULL);
359 kd->flags |= KD_CONFIG_EXISTS;
360 for(i = 0; hostlist && hostlist[i] != NULL; i++)
361 append_host_string(context, kd, hostlist[i], kd->def_port, kd->port);
363 krb5_config_free_strings(hostlist);
367 * as a fallback, look for `serv_string.kd->realm' (typically
368 * kerberos.REALM, kerberos-1.REALM, ...
369 * `port' is the default port for the service, and `proto' the
373 static krb5_error_code
374 fallback_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
375 const char *serv_string, int port, int proto)
380 struct addrinfo hints;
381 char portstr[NI_MAXSERV];
383 if(kd->fallback_count == 0)
384 asprintf(&host, "%s.%s.", serv_string, kd->realm);
386 asprintf(&host, "%s-%d.%s.",
387 serv_string, kd->fallback_count, kd->realm);
392 make_hints(&hints, proto);
393 snprintf(portstr, sizeof(portstr), "%d", port);
394 ret = getaddrinfo(host, portstr, &hints, &ai);
396 /* no more hosts, so we're done here */
398 kd->flags |= KD_FALLBACK;
400 struct krb5_krbhst_info *hi;
401 size_t hostlen = strlen(host);
403 hi = calloc(1, sizeof(*hi) + hostlen);
410 hi->port = hi->def_port = port;
412 memmove(hi->hostname, host, hostlen - 1);
413 hi->hostname[hostlen - 1] = '\0';
415 append_host_hostinfo(kd, hi);
416 kd->fallback_count++;
421 static krb5_error_code
422 kdc_get_next(krb5_context context,
423 struct krb5_krbhst_data *kd,
424 krb5_krbhst_info **host)
428 if((kd->flags & KD_CONFIG) == 0) {
429 config_get_hosts(context, kd, "kdc");
430 kd->flags |= KD_CONFIG;
431 if(get_next(kd, host))
435 if (kd->flags & KD_CONFIG_EXISTS)
436 return KRB5_KDC_UNREACH; /* XXX */
438 if(context->srv_lookup) {
439 if((kd->flags & KD_SRV_UDP) == 0) {
440 srv_get_hosts(context, kd, "udp", "kerberos");
441 kd->flags |= KD_SRV_UDP;
442 if(get_next(kd, host))
446 if((kd->flags & KD_SRV_TCP) == 0) {
447 srv_get_hosts(context, kd, "tcp", "kerberos");
448 kd->flags |= KD_SRV_TCP;
449 if(get_next(kd, host))
452 if((kd->flags & KD_SRV_HTTP) == 0) {
453 srv_get_hosts(context, kd, "http", "kerberos");
454 kd->flags |= KD_SRV_HTTP;
455 if(get_next(kd, host))
460 while((kd->flags & KD_FALLBACK) == 0) {
461 ret = fallback_get_hosts(context, kd, "kerberos",
462 kd->def_port, KRB5_KRBHST_UDP);
465 if(get_next(kd, host))
469 return KRB5_KDC_UNREACH; /* XXX */
472 static krb5_error_code
473 admin_get_next(krb5_context context,
474 struct krb5_krbhst_data *kd,
475 krb5_krbhst_info **host)
479 if((kd->flags & KD_CONFIG) == 0) {
480 config_get_hosts(context, kd, "admin_server");
481 kd->flags |= KD_CONFIG;
482 if(get_next(kd, host))
486 if (kd->flags & KD_CONFIG_EXISTS)
487 return KRB5_KDC_UNREACH; /* XXX */
489 if(context->srv_lookup) {
490 if((kd->flags & KD_SRV_TCP) == 0) {
491 srv_get_hosts(context, kd, "tcp", "kerberos-adm");
492 kd->flags |= KD_SRV_TCP;
493 if(get_next(kd, host))
499 && (kd->flags & KD_FALLBACK) == 0) {
500 ret = fallback_get_hosts(context, kd, "kerberos",
501 kd->def_port, KRB5_KRBHST_UDP);
504 kd->flags |= KD_FALLBACK;
505 if(get_next(kd, host))
509 return KRB5_KDC_UNREACH; /* XXX */
512 static krb5_error_code
513 kpasswd_get_next(krb5_context context,
514 struct krb5_krbhst_data *kd,
515 krb5_krbhst_info **host)
519 if((kd->flags & KD_CONFIG) == 0) {
520 config_get_hosts(context, kd, "kpasswd_server");
521 if(get_next(kd, host))
525 if (kd->flags & KD_CONFIG_EXISTS)
526 return KRB5_KDC_UNREACH; /* XXX */
528 if(context->srv_lookup) {
529 if((kd->flags & KD_SRV_UDP) == 0) {
530 srv_get_hosts(context, kd, "udp", "kpasswd");
531 kd->flags |= KD_SRV_UDP;
532 if(get_next(kd, host))
537 /* no matches -> try admin */
539 if (krbhst_empty(kd)) {
541 kd->port = kd->def_port;
542 kd->get_next = admin_get_next;
543 ret = (*kd->get_next)(context, kd, host);
545 (*host)->proto = KRB5_KRBHST_UDP;
549 return KRB5_KDC_UNREACH; /* XXX */
552 static krb5_error_code
553 krb524_get_next(krb5_context context,
554 struct krb5_krbhst_data *kd,
555 krb5_krbhst_info **host)
557 if((kd->flags & KD_CONFIG) == 0) {
558 config_get_hosts(context, kd, "krb524_server");
559 if(get_next(kd, host))
561 kd->flags |= KD_CONFIG;
564 if (kd->flags & KD_CONFIG_EXISTS)
565 return KRB5_KDC_UNREACH; /* XXX */
567 if(context->srv_lookup) {
568 if((kd->flags & KD_SRV_UDP) == 0) {
569 srv_get_hosts(context, kd, "udp", "krb524");
570 kd->flags |= KD_SRV_UDP;
571 if(get_next(kd, host))
575 if((kd->flags & KD_SRV_TCP) == 0) {
576 srv_get_hosts(context, kd, "tcp", "krb524");
577 kd->flags |= KD_SRV_TCP;
578 if(get_next(kd, host))
583 /* no matches -> try kdc */
585 if (krbhst_empty(kd)) {
587 kd->port = kd->def_port;
588 kd->get_next = kdc_get_next;
589 return (*kd->get_next)(context, kd, host);
592 return KRB5_KDC_UNREACH; /* XXX */
595 static struct krb5_krbhst_data*
596 common_init(krb5_context context,
599 struct krb5_krbhst_data *kd;
601 if((kd = calloc(1, sizeof(*kd))) == NULL)
604 if((kd->realm = strdup(realm)) == NULL) {
609 kd->end = kd->index = &kd->hosts;
614 * initialize `handle' to look for hosts of type `type' in realm `realm'
618 krb5_krbhst_init(krb5_context context,
621 krb5_krbhst_handle *handle)
623 struct krb5_krbhst_data *kd;
624 krb5_error_code (*get_next)(krb5_context, struct krb5_krbhst_data *,
625 krb5_krbhst_info **);
629 case KRB5_KRBHST_KDC:
630 get_next = kdc_get_next;
631 def_port = ntohs(krb5_getportbyname (context, "kerberos", "udp", 88));
633 case KRB5_KRBHST_ADMIN:
634 get_next = admin_get_next;
635 def_port = ntohs(krb5_getportbyname (context, "kerberos-adm",
638 case KRB5_KRBHST_CHANGEPW:
639 get_next = kpasswd_get_next;
640 def_port = ntohs(krb5_getportbyname (context, "kpasswd", "udp",
643 case KRB5_KRBHST_KRB524:
644 get_next = krb524_get_next;
645 def_port = ntohs(krb5_getportbyname (context, "krb524", "udp", 4444));
648 krb5_set_error_string(context, "unknown krbhst type (%u)", type);
651 if((kd = common_init(context, realm)) == NULL)
653 kd->get_next = get_next;
654 kd->def_port = def_port;
660 * return the next host information from `handle' in `host'
664 krb5_krbhst_next(krb5_context context,
665 krb5_krbhst_handle handle,
666 krb5_krbhst_info **host)
668 if(get_next(handle, host))
671 return (*handle->get_next)(context, handle, host);
675 * return the next host information from `handle' as a host name
676 * in `hostname' (or length `hostlen)
680 krb5_krbhst_next_as_string(krb5_context context,
681 krb5_krbhst_handle handle,
686 krb5_krbhst_info *host;
687 ret = krb5_krbhst_next(context, handle, &host);
690 return krb5_krbhst_format_string(context, host, hostname, hostlen);
695 krb5_krbhst_reset(krb5_context context, krb5_krbhst_handle handle)
697 handle->index = &handle->hosts;
701 krb5_krbhst_free(krb5_context context, krb5_krbhst_handle handle)
703 krb5_krbhst_info *h, *next;
708 for (h = handle->hosts; h != NULL; h = next) {
717 /* backwards compatibility ahead */
719 static krb5_error_code
720 gethostlist(krb5_context context, const char *realm,
721 unsigned int type, char ***hostlist)
725 krb5_krbhst_handle handle;
726 char host[MAXHOSTNAMELEN];
727 krb5_krbhst_info *hostinfo;
729 ret = krb5_krbhst_init(context, realm, type, &handle);
733 while(krb5_krbhst_next(context, handle, &hostinfo) == 0)
736 return KRB5_KDC_UNREACH;
737 *hostlist = calloc(nhost + 1, sizeof(**hostlist));
738 if(*hostlist == NULL) {
739 krb5_krbhst_free(context, handle);
743 krb5_krbhst_reset(context, handle);
745 while(krb5_krbhst_next_as_string(context, handle,
746 host, sizeof(host)) == 0) {
747 if(((*hostlist)[nhost++] = strdup(host)) == NULL) {
748 krb5_free_krbhst(context, *hostlist);
749 krb5_krbhst_free(context, handle);
753 (*hostlist)[nhost++] = NULL;
754 krb5_krbhst_free(context, handle);
759 * return an malloced list of kadmin-hosts for `realm' in `hostlist'
763 krb5_get_krb_admin_hst (krb5_context context,
764 const krb5_realm *realm,
767 return gethostlist(context, *realm, KRB5_KRBHST_ADMIN, hostlist);
771 * return an malloced list of changepw-hosts for `realm' in `hostlist'
775 krb5_get_krb_changepw_hst (krb5_context context,
776 const krb5_realm *realm,
779 return gethostlist(context, *realm, KRB5_KRBHST_CHANGEPW, hostlist);
783 * return an malloced list of 524-hosts for `realm' in `hostlist'
787 krb5_get_krb524hst (krb5_context context,
788 const krb5_realm *realm,
791 return gethostlist(context, *realm, KRB5_KRBHST_KRB524, hostlist);
796 * return an malloced list of KDC's for `realm' in `hostlist'
800 krb5_get_krbhst (krb5_context context,
801 const krb5_realm *realm,
804 return gethostlist(context, *realm, KRB5_KRBHST_KDC, hostlist);
808 * free all the memory allocated in `hostlist'
812 krb5_free_krbhst (krb5_context context,
817 for (p = hostlist; *p; ++p)