1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3 * lib/krb5/os/locate_kdc.c
5 * Copyright 1990,2000,2001,2002,2003,2004,2006,2008 Massachusetts Institute of Technology.
8 * Export of this software from the United States of America may
9 * require a specific license from the United States Government.
10 * It is the responsibility of any person or organization contemplating
11 * export to obtain such a license before exporting.
13 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14 * distribute this software and its documentation for any purpose and
15 * without fee is hereby granted, provided that the above copyright
16 * notice appear in all copies and that both that copyright notice and
17 * this permission notice appear in supporting documentation, and that
18 * the name of M.I.T. not be used in advertising or publicity pertaining
19 * to distribution of the software without specific, written prior
20 * permission. Furthermore if you modify this software you must label
21 * your software as modified software and not distribute it in such a
22 * fashion that it might be confused with the original M.I.T. software.
23 * M.I.T. makes no representations about the suitability of
24 * this software for any purpose. It is provided "as is" without express
25 * or implied warranty.
28 * get socket addresses for KDC.
31 #include "fake-addrinfo.h"
35 #ifdef KRB5_DNS_LOOKUP
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <arpa/nameser.h>
49 /* for old Unixes and friends ... */
50 #ifndef MAXHOSTNAMELEN
51 #define MAXHOSTNAMELEN 64
54 #define MAX_DNS_NAMELEN (15*(MAXHOSTNAMELEN + 1)+1)
56 #if KRB5_DNS_LOOKUP_KDC
57 #define DEFAULT_LOOKUP_KDC 1
59 #define DEFAULT_LOOKUP_KDC 0
61 #if KRB5_DNS_LOOKUP_REALM
62 #define DEFAULT_LOOKUP_REALM 1
64 #define DEFAULT_LOOKUP_REALM 0
68 maybe_use_dns (krb5_context context, const char *name, int defalt)
74 code = profile_get_string(context->profile, KRB5_CONF_LIBDEFAULTS,
76 if (value == 0 && code == 0)
77 code = profile_get_string(context->profile, KRB5_CONF_LIBDEFAULTS,
78 KRB5_CONF_DNS_FALLBACK, 0, 0, &value);
85 use_dns = _krb5_conf_boolean(value);
86 profile_release_string(value);
91 _krb5_use_dns_kdc(krb5_context context)
93 return maybe_use_dns (context, KRB5_CONF_DNS_LOOKUP_KDC, DEFAULT_LOOKUP_KDC);
97 _krb5_use_dns_realm(krb5_context context)
99 return maybe_use_dns (context, KRB5_CONF_DNS_LOOKUP_REALM, DEFAULT_LOOKUP_REALM);
102 #endif /* KRB5_DNS_LOOKUP */
105 krb5int_grow_addrlist (struct addrlist *lp, int nmore)
108 size_t newspace = lp->space + nmore;
109 size_t newsize = newspace * sizeof (*lp->addrs);
112 newaddrs = realloc (lp->addrs, newsize);
113 if (newaddrs == NULL)
115 lp->addrs = newaddrs;
116 for (i = lp->space; i < newspace; i++) {
117 lp->addrs[i].ai = NULL;
118 lp->addrs[i].freefn = NULL;
119 lp->addrs[i].data = NULL;
121 lp->space = newspace;
124 #define grow_list krb5int_grow_addrlist
126 /* Free up everything pointed to by the addrlist structure, but don't
127 free the structure itself. */
129 krb5int_free_addrlist (struct addrlist *lp)
132 for (i = 0; i < lp->naddrs; i++)
133 if (lp->addrs[i].freefn)
134 (lp->addrs[i].freefn)(lp->addrs[i].data);
137 lp->naddrs = lp->space = 0;
139 #define free_list krb5int_free_addrlist
142 translate_ai_error (int err)
151 /* All of these indicate bad inputs to getaddrinfo. */
154 /* Translate to standard errno code. */
157 /* Translate to standard errno code. */
159 #ifdef EAI_ADDRFAMILY
162 #if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME
166 /* Name not known or no address data, but no error. Do
171 /* An argument buffer overflowed. */
172 return EINVAL; /* XXX */
176 /* System error, obviously. */
180 /* An error code we haven't handled? */
187 Tprintf(const char *fmt, ...)
192 vfprintf(stderr, fmt, ap);
198 extern void krb5int_debug_fprint(const char *, ...);
199 #define dprint krb5int_debug_fprint
200 #define print_addrlist krb5int_print_addrlist
201 extern void print_addrlist (const struct addrlist *a);
203 static inline void dprint(const char *fmt, ...) { }
204 static inline void print_addrlist(const struct addrlist *a) { }
208 add_addrinfo_to_list(struct addrlist *lp, struct addrinfo *a,
209 void (*freefn)(void *), void *data)
213 dprint("\tadding %p=%A to %p (naddrs=%d space=%d)\n", a, a, lp,
214 lp->naddrs, lp->space);
216 if (lp->naddrs == lp->space) {
217 err = grow_list (lp, 1);
219 Tprintf ("grow_list failed %d\n", err);
223 Tprintf("setting element %d\n", lp->naddrs);
224 lp->addrs[lp->naddrs].ai = a;
225 lp->addrs[lp->naddrs].freefn = freefn;
226 lp->addrs[lp->naddrs].data = data;
228 Tprintf ("\tcount is now %lu: ", (unsigned long) lp->naddrs);
234 #define add_host_to_list krb5int_add_host_to_list
237 call_freeaddrinfo(void *data)
239 /* Strict interpretation of the C standard says we can't assume
240 that the ABI for f(void*) and f(struct foo *) will be
241 compatible. Use this stub just to be paranoid. */
246 krb5int_add_host_to_list (struct addrlist *lp, const char *hostname,
247 int port, int secport,
248 int socktype, int family)
250 struct addrinfo *addrs, *a, *anext, hint;
252 char portbuf[10], secportbuf[10];
253 void (*freefn)(void *);
255 Tprintf ("adding hostname %s, ports %d,%d, family %d, socktype %d\n",
256 hostname, ntohs (port), ntohs (secport),
259 memset(&hint, 0, sizeof(hint));
260 hint.ai_family = family;
261 hint.ai_socktype = socktype;
262 #ifdef AI_NUMERICSERV
263 hint.ai_flags = AI_NUMERICSERV;
265 result = snprintf(portbuf, sizeof(portbuf), "%d", ntohs(port));
266 if (SNPRINTF_OVERFLOW(result, sizeof(portbuf)))
269 result = snprintf(secportbuf, sizeof(secportbuf), "%d", ntohs(secport));
270 if (SNPRINTF_OVERFLOW(result, sizeof(secportbuf)))
272 err = getaddrinfo (hostname, portbuf, &hint, &addrs);
274 Tprintf ("\tgetaddrinfo(\"%s\", \"%s\", ...)\n\treturns %d: %s\n",
275 hostname, portbuf, err, gai_strerror (err));
276 return translate_ai_error (err);
278 freefn = call_freeaddrinfo;
280 for (a = addrs; a != 0 && err == 0; a = anext, freefn = 0) {
282 err = add_addrinfo_to_list (lp, a, freefn, a);
284 if (err || secport == 0)
287 socktype = SOCK_DGRAM;
288 else if (socktype != SOCK_DGRAM)
290 hint.ai_family = AF_INET;
291 err = getaddrinfo (hostname, secportbuf, &hint, &addrs);
293 err = translate_ai_error (err);
296 freefn = call_freeaddrinfo;
297 for (a = addrs; a != 0 && err == 0; a = anext, freefn = 0) {
299 err = add_addrinfo_to_list (lp, a, freefn, a);
302 /* XXX Memory leaks possible here if add_addrinfo_to_list fails. */
307 * returns count of number of addresses found
308 * if master is non-NULL, it is filled in with the index of
312 static krb5_error_code
313 krb5_locate_srv_conf_1(krb5_context context, const krb5_data *realm,
314 const char * name, struct addrlist *addrlist,
315 int get_masters, int socktype,
316 int udpport, int sec_udpport, int family)
318 const char *realm_srv_names[4];
319 char **masterlist, **hostlist, *host, *port, *cp;
320 krb5_error_code code;
321 int i, j, count, ismaster;
323 Tprintf ("looking in krb5.conf for realm %s entry %s; ports %d,%d\n",
324 realm->data, name, ntohs (udpport), ntohs (sec_udpport));
326 if ((host = malloc(realm->length + 1)) == NULL)
329 strncpy(host, realm->data, realm->length);
330 host[realm->length] = '\0';
335 realm_srv_names[0] = KRB5_CONF_REALMS;
336 realm_srv_names[1] = host;
337 realm_srv_names[2] = name;
338 realm_srv_names[3] = 0;
340 code = profile_get_values(context->profile, realm_srv_names, &hostlist);
343 Tprintf ("config file lookup failed: %s\n",
344 error_message(code));
345 if (code == PROF_NO_SECTION || code == PROF_NO_RELATION)
346 code = KRB5_REALM_UNKNOWN;
352 while (hostlist && hostlist[count])
354 Tprintf ("found %d entries under 'kdc'\n", count);
357 profile_free_list(hostlist);
359 addrlist->naddrs = 0;
364 realm_srv_names[0] = KRB5_CONF_REALMS;
365 realm_srv_names[1] = host;
366 realm_srv_names[2] = KRB5_CONF_ADMIN_SERVER;
367 realm_srv_names[3] = 0;
369 code = profile_get_values(context->profile, realm_srv_names,
375 for (i=0; masterlist[i]; i++) {
376 host = masterlist[i];
379 * Strip off excess whitespace
381 cp = strchr(host, ' ');
384 cp = strchr(host, '\t');
387 cp = strchr(host, ':');
396 /* at this point, if master is non-NULL, then either the master kdc
397 is required, and there is one, or the master kdc is not required,
398 and there may or may not be one. */
400 #ifdef HAVE_NETINET_IN_H
405 for (i=0; hostlist[i]; i++) {
409 Tprintf ("entry %d is '%s'\n", i, host);
411 * Strip off excess whitespace
413 cp = strchr(host, ' ');
416 cp = strchr(host, '\t');
419 port = strchr(host, ':');
427 for (j=0; masterlist[j]; j++) {
428 if (strcasecmp(hostlist[i], masterlist[j]) == 0) {
434 if (get_masters && !ismaster)
441 l = strtoul (port, &endptr, 10);
442 if (endptr == NULL || *endptr != 0)
447 /* L is unsigned, don't need to check <0. */
458 code = add_host_to_list (addrlist, hostlist[i], p1, p2,
461 code = add_host_to_list (addrlist, hostlist[i], p1, p2,
464 code = add_host_to_list (addrlist, hostlist[i], p1, p2,
465 SOCK_STREAM, family);
468 Tprintf ("error %d (%s) returned from add_host_to_list\n", code,
469 error_message (code));
471 profile_free_list (hostlist);
473 profile_free_list (masterlist);
479 profile_free_list(hostlist);
481 profile_free_list(masterlist);
487 static krb5_error_code
488 krb5_locate_srv_conf(krb5_context context, const krb5_data *realm,
489 const char *name, struct addrlist *al, int get_masters,
490 int udpport, int sec_udpport)
494 ret = krb5_locate_srv_conf_1 (context, realm, name, al,
495 get_masters, 0, udpport, sec_udpport, 0);
498 if (al->naddrs == 0) /* Couldn't resolve any KDC names */
499 return KRB5_REALM_CANT_RESOLVE;
504 #ifdef KRB5_DNS_LOOKUP
505 static krb5_error_code
506 krb5_locate_srv_dns_1 (const krb5_data *realm,
508 const char *protocol,
509 struct addrlist *addrlist,
512 struct srv_dns_entry *head = NULL;
513 struct srv_dns_entry *entry = NULL, *next;
514 krb5_error_code code = 0;
516 code = krb5int_make_srv_query_realm(realm, service, protocol, &head);
521 * Okay! Now we've got a linked list of entries sorted by
522 * priority. Start looking up A records and returning
529 /* Check for the "." case indicating no support. */
530 if (head->next == 0 && head->host[0] == 0) {
533 return KRB5_ERR_NO_SERVICE;
536 Tprintf ("walking answer list:\n");
537 for (entry = head; entry != NULL; entry = next) {
538 Tprintf ("\tport=%d host=%s\n", entry->port, entry->host);
540 code = add_host_to_list (addrlist, entry->host, htons (entry->port), 0,
541 (strcmp("_tcp", protocol)
543 : SOCK_STREAM), family);
556 krb5int_free_srv_dns_data(head);
561 #include <krb5/locate_plugin.h>
564 static const char *objdirs[] = { KRB5_PLUGIN_BUNDLE_DIR, LIBDIR "/krb5/plugins/libkrb5", NULL }; /* should be a list */
566 static const char *objdirs[] = { LIBDIR "/krb5/plugins/libkrb5", NULL };
569 struct module_callback_data {
575 module_callback (void *cbdata, int socktype, struct sockaddr *sa)
577 struct module_callback_data *d = cbdata;
581 struct sockaddr_in sin;
582 #ifdef KRB5_USE_INET6
583 struct sockaddr_in6 sin6;
588 if (socktype != SOCK_STREAM && socktype != SOCK_DGRAM)
590 if (sa->sa_family != AF_INET
591 #ifdef KRB5_USE_INET6
592 && sa->sa_family != AF_INET6
596 x = calloc (1, sizeof (*x));
601 x->ai.ai_addr = (struct sockaddr *) &x->u;
602 x->ai.ai_socktype = socktype;
603 x->ai.ai_family = sa->sa_family;
604 if (sa->sa_family == AF_INET) {
605 x->u.sin = *(struct sockaddr_in *)sa;
606 x->ai.ai_addrlen = sizeof(struct sockaddr_in);
608 #ifdef KRB5_USE_INET6
609 if (sa->sa_family == AF_INET6) {
610 x->u.sin6 = *(struct sockaddr_in6 *)sa;
611 x->ai.ai_addrlen = sizeof(struct sockaddr_in6);
614 if (add_addrinfo_to_list (d->lp, &x->ai, free, x) != 0) {
615 /* Assumes only error is ENOMEM. */
622 static krb5_error_code
623 module_locate_server (krb5_context ctx, const krb5_data *realm,
624 struct addrlist *addrlist,
625 enum locate_service_type svc, int socktype, int family)
627 struct krb5plugin_service_locate_result *res = NULL;
628 krb5_error_code code;
629 struct krb5plugin_service_locate_ftable *vtbl = NULL;
631 char *realmz; /* NUL-terminated realm */
633 struct module_callback_data cbdata = { 0, };
636 Tprintf("in module_locate_server\n");
637 cbdata.lp = addrlist;
638 if (!PLUGIN_DIR_OPEN (&ctx->libkrb5_plugins)) {
640 code = krb5int_open_plugin_dirs (objdirs, NULL, &ctx->libkrb5_plugins,
643 return KRB5_PLUGIN_NO_HANDLE;
646 code = krb5int_get_plugin_dir_data (&ctx->libkrb5_plugins,
647 "service_locator", &ptrs, &ctx->err);
649 Tprintf("error looking up plugin symbols: %s\n",
650 (msg = krb5_get_error_message(ctx, code)));
651 krb5_free_error_message(ctx, msg);
652 return KRB5_PLUGIN_NO_HANDLE;
655 if (realm->length >= UINT_MAX) {
656 krb5int_free_plugin_dir_data(ptrs);
659 realmz = malloc(realm->length + 1);
660 if (realmz == NULL) {
661 krb5int_free_plugin_dir_data(ptrs);
664 memcpy(realmz, realm->data, realm->length);
665 realmz[realm->length] = '\0';
666 for (i = 0; ptrs[i]; i++) {
670 Tprintf("element %d is %p\n", i, ptrs[i]);
672 /* For now, don't keep the plugin data alive. For long-lived
673 contexts, it may be desirable to change that later. */
674 code = vtbl->init(ctx, &blob);
678 code = vtbl->lookup(blob, svc, realmz, socktype, family,
679 module_callback, &cbdata);
681 if (code == KRB5_PLUGIN_NO_HANDLE) {
682 /* Module passes, keep going. */
684 Tprintf("plugin doesn't handle this realm (KRB5_PLUGIN_NO_HANDLE)\n");
688 /* Module encountered an actual error. */
689 Tprintf("plugin lookup routine returned error %d: %s\n",
690 code, error_message(code));
692 krb5int_free_plugin_dir_data (ptrs);
697 if (ptrs[i] == NULL) {
698 Tprintf("ran off end of plugin list\n");
700 krb5int_free_plugin_dir_data (ptrs);
701 return KRB5_PLUGIN_NO_HANDLE;
703 Tprintf("stopped with plugin #%d, res=%p\n", i, res);
705 /* Got something back, yippee. */
706 Tprintf("now have %lu addrs in list %p\n",
707 (unsigned long) addrlist->naddrs, addrlist);
708 print_addrlist(addrlist);
710 krb5int_free_plugin_dir_data (ptrs);
714 static krb5_error_code
715 prof_locate_server (krb5_context context, const krb5_data *realm,
716 struct addrlist *addrlist,
717 enum locate_service_type svc, int socktype, int family)
719 const char *profname;
720 int dflport1, dflport2 = 0;
721 struct servent *serv;
724 case locate_service_kdc:
725 profname = KRB5_CONF_KDC;
726 /* We used to use /etc/services for these, but enough systems
727 have old, crufty, wrong settings that this is probably
730 dflport1 = htons(KRB5_DEFAULT_PORT);
731 dflport2 = htons(KRB5_DEFAULT_SEC_PORT);
733 case locate_service_master_kdc:
734 profname = KRB5_CONF_MASTER_KDC;
736 case locate_service_kadmin:
737 profname = KRB5_CONF_ADMIN_SERVER;
738 dflport1 = htons(DEFAULT_KADM5_PORT);
740 case locate_service_krb524:
741 profname = KRB5_CONF_KRB524_SERVER;
742 serv = getservbyname(KRB524_SERVICE, "udp");
743 dflport1 = serv ? serv->s_port : htons (KRB524_PORT);
745 case locate_service_kpasswd:
746 profname = KRB5_CONF_KPASSWD_SERVER;
747 dflport1 = htons(DEFAULT_KPASSWD_PORT);
750 return EBUSY; /* XXX */
753 return krb5_locate_srv_conf_1 (context, realm, profname, addrlist,
755 dflport1, dflport2, family);
758 static krb5_error_code
759 dns_locate_server (krb5_context context, const krb5_data *realm,
760 struct addrlist *addrlist,
761 enum locate_service_type svc, int socktype, int family)
764 int use_dns = _krb5_use_dns_kdc(context);
765 krb5_error_code code;
768 return KRB5_PLUGIN_NO_HANDLE;
771 case locate_service_kdc:
772 dnsname = "_kerberos";
774 case locate_service_master_kdc:
775 dnsname = "_kerberos-master";
777 case locate_service_kadmin:
778 dnsname = "_kerberos-adm";
780 case locate_service_krb524:
783 case locate_service_kpasswd:
784 dnsname = "_kpasswd";
787 return KRB5_PLUGIN_NO_HANDLE;
791 if (socktype == SOCK_DGRAM || socktype == 0) {
792 code = krb5_locate_srv_dns_1(realm, dnsname, "_udp", addrlist, family);
794 Tprintf("dns udp lookup returned error %d\n", code);
796 if ((socktype == SOCK_STREAM || socktype == 0) && code == 0) {
797 code = krb5_locate_srv_dns_1(realm, dnsname, "_tcp", addrlist, family);
799 Tprintf("dns tcp lookup returned error %d\n", code);
805 * Wrapper function for the various backends
809 krb5int_locate_server (krb5_context context, const krb5_data *realm,
810 struct addrlist *addrlist,
811 enum locate_service_type svc,
812 int socktype, int family)
814 krb5_error_code code;
815 struct addrlist al = ADDRLIST_INIT;
819 if (realm == NULL || realm->data == NULL || realm->data[0] == 0) {
820 krb5_set_error_message(context, KRB5_REALM_CANT_RESOLVE,
821 "Cannot find KDC for invalid realm name \"\"");
822 return KRB5_REALM_CANT_RESOLVE;
825 code = module_locate_server(context, realm, &al, svc, socktype, family);
826 Tprintf("module_locate_server returns %d\n", code);
827 if (code == KRB5_PLUGIN_NO_HANDLE) {
829 * We always try the local file before DNS. Note that there
830 * is no way to indicate "service not available" via the
834 code = prof_locate_server(context, realm, &al, svc, socktype, family);
836 #ifdef KRB5_DNS_LOOKUP
837 if (code) { /* Try DNS for all profile errors? */
838 krb5_error_code code2;
839 code2 = dns_locate_server(context, realm, &al, svc, socktype,
841 if (code2 != KRB5_PLUGIN_NO_HANDLE)
844 #endif /* KRB5_DNS_LOOKUP */
846 /* We could put more heuristics here, like looking up a hostname
847 of "kerberos."+REALM, etc. */
850 Tprintf ("krb5int_locate_server found %d addresses\n",
853 Tprintf ("krb5int_locate_server returning error code %d/%s\n",
854 code, error_message(code));
860 if (al.naddrs == 0) { /* No good servers */
863 krb5_set_error_message(context, KRB5_REALM_CANT_RESOLVE,
864 "Cannot resolve network address for KDC in realm \"%.*s\"",
865 realm->length, realm->data);
867 return KRB5_REALM_CANT_RESOLVE;
874 krb5_locate_kdc(krb5_context context, const krb5_data *realm,
875 struct addrlist *addrlist,
876 int get_masters, int socktype, int family)
878 return krb5int_locate_server(context, realm, addrlist,
880 ? locate_service_master_kdc
881 : locate_service_kdc),