2 * lib/krb5/os/locate_kdc.c
4 * Copyright 1990,2000,2001,2002,2003,2004,2006,2008 Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
27 * get socket addresses for KDC.
30 #include "fake-addrinfo.h"
34 #ifdef KRB5_DNS_LOOKUP
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <arpa/nameser.h>
48 /* for old Unixes and friends ... */
49 #ifndef MAXHOSTNAMELEN
50 #define MAXHOSTNAMELEN 64
53 #define MAX_DNS_NAMELEN (15*(MAXHOSTNAMELEN + 1)+1)
55 #if KRB5_DNS_LOOKUP_KDC
56 #define DEFAULT_LOOKUP_KDC 1
58 #define DEFAULT_LOOKUP_KDC 0
60 #if KRB5_DNS_LOOKUP_REALM
61 #define DEFAULT_LOOKUP_REALM 1
63 #define DEFAULT_LOOKUP_REALM 0
67 maybe_use_dns (krb5_context context, const char *name, int defalt)
73 code = profile_get_string(context->profile, KRB5_CONF_LIBDEFAULTS,
75 if (value == 0 && code == 0)
76 code = profile_get_string(context->profile, KRB5_CONF_LIBDEFAULTS,
77 KRB5_CONF_DNS_FALLBACK, 0, 0, &value);
84 use_dns = _krb5_conf_boolean(value);
85 profile_release_string(value);
90 _krb5_use_dns_kdc(krb5_context context)
92 return maybe_use_dns (context, KRB5_CONF_DNS_LOOKUP_KDC, DEFAULT_LOOKUP_KDC);
96 _krb5_use_dns_realm(krb5_context context)
98 return maybe_use_dns (context, KRB5_CONF_DNS_LOOKUP_REALM, DEFAULT_LOOKUP_REALM);
101 #endif /* KRB5_DNS_LOOKUP */
104 krb5int_grow_addrlist (struct addrlist *lp, int nmore)
107 int newspace = lp->space + nmore;
108 size_t newsize = newspace * sizeof (*lp->addrs);
111 newaddrs = realloc (lp->addrs, newsize);
112 if (newaddrs == NULL)
114 lp->addrs = newaddrs;
115 for (i = lp->space; i < newspace; i++) {
116 lp->addrs[i].ai = NULL;
117 lp->addrs[i].freefn = NULL;
118 lp->addrs[i].data = NULL;
120 lp->space = newspace;
123 #define grow_list krb5int_grow_addrlist
125 /* Free up everything pointed to by the addrlist structure, but don't
126 free the structure itself. */
128 krb5int_free_addrlist (struct addrlist *lp)
131 for (i = 0; i < lp->naddrs; i++)
132 if (lp->addrs[i].freefn)
133 (lp->addrs[i].freefn)(lp->addrs[i].data);
136 lp->naddrs = lp->space = 0;
138 #define free_list krb5int_free_addrlist
140 static int translate_ai_error (int err)
149 /* All of these indicate bad inputs to getaddrinfo. */
152 /* Translate to standard errno code. */
155 /* Translate to standard errno code. */
157 #ifdef EAI_ADDRFAMILY
160 #if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME
164 /* Name not known or no address data, but no error. Do
169 /* An argument buffer overflowed. */
170 return EINVAL; /* XXX */
174 /* System error, obviously. */
178 /* An error code we haven't handled? */
184 static inline void Tprintf(const char *fmt, ...)
189 vfprintf(stderr, fmt, ap);
195 extern void krb5int_debug_fprint(const char *, ...);
196 #define dprint krb5int_debug_fprint
197 #define print_addrlist krb5int_print_addrlist
198 extern void print_addrlist (const struct addrlist *a);
200 static inline void dprint(const char *fmt, ...) { }
201 static inline void print_addrlist(const struct addrlist *a) { }
204 static int add_addrinfo_to_list (struct addrlist *lp, struct addrinfo *a,
205 void (*freefn)(void *), void *data)
209 dprint("\tadding %p=%A to %p (naddrs=%d space=%d)\n", a, a, lp,
210 lp->naddrs, lp->space);
212 if (lp->naddrs == lp->space) {
213 err = grow_list (lp, 1);
215 Tprintf ("grow_list failed %d\n", err);
219 Tprintf("setting element %d\n", lp->naddrs);
220 lp->addrs[lp->naddrs].ai = a;
221 lp->addrs[lp->naddrs].freefn = freefn;
222 lp->addrs[lp->naddrs].data = data;
224 Tprintf ("\tcount is now %d: ", lp->naddrs);
230 #define add_host_to_list krb5int_add_host_to_list
232 static void call_freeaddrinfo(void *data)
234 /* Strict interpretation of the C standard says we can't assume
235 that the ABI for f(void*) and f(struct foo *) will be
236 compatible. Use this stub just to be paranoid. */
241 krb5int_add_host_to_list (struct addrlist *lp, const char *hostname,
242 int port, int secport,
243 int socktype, int family)
245 struct addrinfo *addrs, *a, *anext, hint;
247 char portbuf[10], secportbuf[10];
248 void (*freefn)(void *);
250 Tprintf ("adding hostname %s, ports %d,%d, family %d, socktype %d\n",
251 hostname, ntohs (port), ntohs (secport),
254 memset(&hint, 0, sizeof(hint));
255 hint.ai_family = family;
256 hint.ai_socktype = socktype;
257 #ifdef AI_NUMERICSERV
258 hint.ai_flags = AI_NUMERICSERV;
260 if (snprintf(portbuf, sizeof(portbuf), "%d", ntohs(port)) >= sizeof(portbuf))
263 if (snprintf(secportbuf, sizeof(secportbuf), "%d", ntohs(secport)) >= sizeof(secportbuf))
265 err = getaddrinfo (hostname, portbuf, &hint, &addrs);
267 Tprintf ("\tgetaddrinfo(\"%s\", \"%s\", ...)\n\treturns %d: %s\n",
268 hostname, portbuf, err, gai_strerror (err));
269 return translate_ai_error (err);
271 freefn = call_freeaddrinfo;
273 for (a = addrs; a != 0 && err == 0; a = anext, freefn = 0) {
275 err = add_addrinfo_to_list (lp, a, freefn, a);
277 if (err || secport == 0)
280 socktype = SOCK_DGRAM;
281 else if (socktype != SOCK_DGRAM)
283 hint.ai_family = AF_INET;
284 err = getaddrinfo (hostname, secportbuf, &hint, &addrs);
286 err = translate_ai_error (err);
289 freefn = call_freeaddrinfo;
290 for (a = addrs; a != 0 && err == 0; a = anext, freefn = 0) {
292 err = add_addrinfo_to_list (lp, a, freefn, a);
295 /* XXX Memory leaks possible here if add_addrinfo_to_list fails. */
300 * returns count of number of addresses found
301 * if master is non-NULL, it is filled in with the index of
305 static krb5_error_code
306 krb5_locate_srv_conf_1(krb5_context context, const krb5_data *realm,
307 const char * name, struct addrlist *addrlist,
308 int get_masters, int socktype,
309 int udpport, int sec_udpport, int family)
311 const char *realm_srv_names[4];
312 char **masterlist, **hostlist, *host, *port, *cp;
313 krb5_error_code code;
314 int i, j, count, ismaster;
316 Tprintf ("looking in krb5.conf for realm %s entry %s; ports %d,%d\n",
317 realm->data, name, ntohs (udpport), ntohs (sec_udpport));
319 if ((host = malloc(realm->length + 1)) == NULL)
322 strncpy(host, realm->data, realm->length);
323 host[realm->length] = '\0';
328 realm_srv_names[0] = KRB5_CONF_REALMS;
329 realm_srv_names[1] = host;
330 realm_srv_names[2] = name;
331 realm_srv_names[3] = 0;
333 code = profile_get_values(context->profile, realm_srv_names, &hostlist);
336 Tprintf ("config file lookup failed: %s\n",
337 error_message(code));
338 if (code == PROF_NO_SECTION || code == PROF_NO_RELATION)
339 code = KRB5_REALM_UNKNOWN;
345 while (hostlist && hostlist[count])
347 Tprintf ("found %d entries under 'kdc'\n", count);
350 profile_free_list(hostlist);
352 addrlist->naddrs = 0;
357 realm_srv_names[0] = KRB5_CONF_REALMS;
358 realm_srv_names[1] = host;
359 realm_srv_names[2] = KRB5_CONF_ADMIN_SERVER;
360 realm_srv_names[3] = 0;
362 code = profile_get_values(context->profile, realm_srv_names,
368 for (i=0; masterlist[i]; i++) {
369 host = masterlist[i];
372 * Strip off excess whitespace
374 cp = strchr(host, ' ');
377 cp = strchr(host, '\t');
380 cp = strchr(host, ':');
389 /* at this point, if master is non-NULL, then either the master kdc
390 is required, and there is one, or the master kdc is not required,
391 and there may or may not be one. */
393 #ifdef HAVE_NETINET_IN_H
398 for (i=0; hostlist[i]; i++) {
402 Tprintf ("entry %d is '%s'\n", i, host);
404 * Strip off excess whitespace
406 cp = strchr(host, ' ');
409 cp = strchr(host, '\t');
412 port = strchr(host, ':');
420 for (j=0; masterlist[j]; j++) {
421 if (strcasecmp(hostlist[i], masterlist[j]) == 0) {
427 if (get_masters && !ismaster)
434 l = strtoul (port, &endptr, 10);
435 if (endptr == NULL || *endptr != 0)
440 /* L is unsigned, don't need to check <0. */
451 code = add_host_to_list (addrlist, hostlist[i], p1, p2,
454 code = add_host_to_list (addrlist, hostlist[i], p1, p2,
457 code = add_host_to_list (addrlist, hostlist[i], p1, p2,
458 SOCK_STREAM, family);
461 Tprintf ("error %d (%s) returned from add_host_to_list\n", code,
462 error_message (code));
464 profile_free_list (hostlist);
466 profile_free_list (masterlist);
472 profile_free_list(hostlist);
474 profile_free_list(masterlist);
480 static krb5_error_code
481 krb5_locate_srv_conf(krb5_context context, const krb5_data *realm,
482 const char *name, struct addrlist *al, int get_masters,
483 int udpport, int sec_udpport)
487 ret = krb5_locate_srv_conf_1 (context, realm, name, al,
488 get_masters, 0, udpport, sec_udpport, 0);
491 if (al->naddrs == 0) /* Couldn't resolve any KDC names */
492 return KRB5_REALM_CANT_RESOLVE;
497 #ifdef KRB5_DNS_LOOKUP
498 static krb5_error_code
499 krb5_locate_srv_dns_1 (const krb5_data *realm,
501 const char *protocol,
502 struct addrlist *addrlist,
505 struct srv_dns_entry *head = NULL;
506 struct srv_dns_entry *entry = NULL, *next;
507 krb5_error_code code = 0;
509 code = krb5int_make_srv_query_realm(realm, service, protocol, &head);
514 * Okay! Now we've got a linked list of entries sorted by
515 * priority. Start looking up A records and returning
522 /* Check for the "." case indicating no support. */
523 if (head->next == 0 && head->host[0] == 0) {
526 return KRB5_ERR_NO_SERVICE;
529 Tprintf ("walking answer list:\n");
530 for (entry = head; entry != NULL; entry = next) {
531 Tprintf ("\tport=%d host=%s\n", entry->port, entry->host);
533 code = add_host_to_list (addrlist, entry->host, htons (entry->port), 0,
534 (strcmp("_tcp", protocol)
536 : SOCK_STREAM), family);
549 krb5int_free_srv_dns_data(head);
554 #include <krb5/locate_plugin.h>
557 static const char *objdirs[] = { KRB5_PLUGIN_BUNDLE_DIR, LIBDIR "/krb5/plugins/libkrb5", NULL }; /* should be a list */
559 static const char *objdirs[] = { LIBDIR "/krb5/plugins/libkrb5", NULL };
562 struct module_callback_data {
568 module_callback (void *cbdata, int socktype, struct sockaddr *sa)
570 struct module_callback_data *d = cbdata;
574 struct sockaddr_in sin;
575 #ifdef KRB5_USE_INET6
576 struct sockaddr_in6 sin6;
581 if (socktype != SOCK_STREAM && socktype != SOCK_DGRAM)
583 if (sa->sa_family != AF_INET
584 #ifdef KRB5_USE_INET6
585 && sa->sa_family != AF_INET6
589 x = calloc (1, sizeof (*x));
594 x->ai.ai_addr = (struct sockaddr *) &x->u;
595 x->ai.ai_socktype = socktype;
596 x->ai.ai_family = sa->sa_family;
597 if (sa->sa_family == AF_INET) {
598 x->u.sin = *(struct sockaddr_in *)sa;
599 x->ai.ai_addrlen = sizeof(struct sockaddr_in);
601 #ifdef KRB5_USE_INET6
602 if (sa->sa_family == AF_INET6) {
603 x->u.sin6 = *(struct sockaddr_in6 *)sa;
604 x->ai.ai_addrlen = sizeof(struct sockaddr_in6);
607 if (add_addrinfo_to_list (d->lp, &x->ai, free, x) != 0) {
608 /* Assumes only error is ENOMEM. */
615 static krb5_error_code
616 module_locate_server (krb5_context ctx, const krb5_data *realm,
617 struct addrlist *addrlist,
618 enum locate_service_type svc, int socktype, int family)
620 struct krb5plugin_service_locate_result *res = NULL;
621 krb5_error_code code;
622 struct krb5plugin_service_locate_ftable *vtbl = NULL;
624 char *realmz; /* NUL-terminated realm */
626 struct module_callback_data cbdata = { 0, };
629 Tprintf("in module_locate_server\n");
630 cbdata.lp = addrlist;
631 if (!PLUGIN_DIR_OPEN (&ctx->libkrb5_plugins)) {
633 code = krb5int_open_plugin_dirs (objdirs, NULL, &ctx->libkrb5_plugins,
636 return KRB5_PLUGIN_NO_HANDLE;
639 code = krb5int_get_plugin_dir_data (&ctx->libkrb5_plugins,
640 "service_locator", &ptrs, &ctx->err);
642 Tprintf("error looking up plugin symbols: %s\n",
643 (msg = krb5_get_error_message(ctx, code)));
644 krb5_free_error_message(ctx, msg);
645 return KRB5_PLUGIN_NO_HANDLE;
648 if (realm->length >= UINT_MAX) {
649 krb5int_free_plugin_dir_data(ptrs);
652 realmz = malloc(realm->length + 1);
653 if (realmz == NULL) {
654 krb5int_free_plugin_dir_data(ptrs);
657 memcpy(realmz, realm->data, realm->length);
658 realmz[realm->length] = '\0';
659 for (i = 0; ptrs[i]; i++) {
663 Tprintf("element %d is %p\n", i, ptrs[i]);
665 /* For now, don't keep the plugin data alive. For long-lived
666 contexts, it may be desirable to change that later. */
667 code = vtbl->init(ctx, &blob);
671 code = vtbl->lookup(blob, svc, realmz, socktype, family,
672 module_callback, &cbdata);
674 if (code == KRB5_PLUGIN_NO_HANDLE) {
675 /* Module passes, keep going. */
677 Tprintf("plugin doesn't handle this realm (KRB5_PLUGIN_NO_HANDLE)\n");
681 /* Module encountered an actual error. */
682 Tprintf("plugin lookup routine returned error %d: %s\n",
683 code, error_message(code));
685 krb5int_free_plugin_dir_data (ptrs);
690 if (ptrs[i] == NULL) {
691 Tprintf("ran off end of plugin list\n");
693 krb5int_free_plugin_dir_data (ptrs);
694 return KRB5_PLUGIN_NO_HANDLE;
696 Tprintf("stopped with plugin #%d, res=%p\n", i, res);
698 /* Got something back, yippee. */
699 Tprintf("now have %d addrs in list %p\n", addrlist->naddrs, addrlist);
700 print_addrlist(addrlist);
702 krb5int_free_plugin_dir_data (ptrs);
706 static krb5_error_code
707 prof_locate_server (krb5_context context, const krb5_data *realm,
708 struct addrlist *addrlist,
709 enum locate_service_type svc, int socktype, int family)
711 const char *profname;
712 int dflport1, dflport2 = 0;
713 struct servent *serv;
716 case locate_service_kdc:
717 profname = KRB5_CONF_KDC;
718 /* We used to use /etc/services for these, but enough systems
719 have old, crufty, wrong settings that this is probably
722 dflport1 = htons(KRB5_DEFAULT_PORT);
723 dflport2 = htons(KRB5_DEFAULT_SEC_PORT);
725 case locate_service_master_kdc:
726 profname = KRB5_CONF_MASTER_KDC;
728 case locate_service_kadmin:
729 profname = KRB5_CONF_ADMIN_SERVER;
730 dflport1 = htons(DEFAULT_KADM5_PORT);
732 case locate_service_krb524:
733 profname = KRB5_CONF_KRB524_SERVER;
734 serv = getservbyname(KRB524_SERVICE, "udp");
735 dflport1 = serv ? serv->s_port : htons (KRB524_PORT);
737 case locate_service_kpasswd:
738 profname = KRB5_CONF_KPASSWD_SERVER;
739 dflport1 = htons(DEFAULT_KPASSWD_PORT);
742 return EBUSY; /* XXX */
745 return krb5_locate_srv_conf_1 (context, realm, profname, addrlist,
747 dflport1, dflport2, family);
750 static krb5_error_code
751 dns_locate_server (krb5_context context, const krb5_data *realm,
752 struct addrlist *addrlist,
753 enum locate_service_type svc, int socktype, int family)
756 int use_dns = _krb5_use_dns_kdc(context);
757 krb5_error_code code;
760 return KRB5_PLUGIN_NO_HANDLE;
763 case locate_service_kdc:
764 dnsname = "_kerberos";
766 case locate_service_master_kdc:
767 dnsname = "_kerberos-master";
769 case locate_service_kadmin:
770 dnsname = "_kerberos-adm";
772 case locate_service_krb524:
775 case locate_service_kpasswd:
776 dnsname = "_kpasswd";
779 return KRB5_PLUGIN_NO_HANDLE;
783 if (socktype == SOCK_DGRAM || socktype == 0) {
784 code = krb5_locate_srv_dns_1(realm, dnsname, "_udp", addrlist, family);
786 Tprintf("dns udp lookup returned error %d\n", code);
788 if ((socktype == SOCK_STREAM || socktype == 0) && code == 0) {
789 code = krb5_locate_srv_dns_1(realm, dnsname, "_tcp", addrlist, family);
791 Tprintf("dns tcp lookup returned error %d\n", code);
797 * Wrapper function for the various backends
801 krb5int_locate_server (krb5_context context, const krb5_data *realm,
802 struct addrlist *addrlist,
803 enum locate_service_type svc,
804 int socktype, int family)
806 krb5_error_code code;
807 struct addrlist al = ADDRLIST_INIT;
811 if (realm == NULL || realm->data == NULL || realm->data[0] == 0) {
812 krb5_set_error_message(context, KRB5_REALM_CANT_RESOLVE,
813 "Cannot find KDC for invalid realm name \"\"");
814 return KRB5_REALM_CANT_RESOLVE;
817 code = module_locate_server(context, realm, &al, svc, socktype, family);
818 Tprintf("module_locate_server returns %d\n", code);
819 if (code == KRB5_PLUGIN_NO_HANDLE) {
821 * We always try the local file before DNS. Note that there
822 * is no way to indicate "service not available" via the
826 code = prof_locate_server(context, realm, &al, svc, socktype, family);
828 #ifdef KRB5_DNS_LOOKUP
829 if (code) { /* Try DNS for all profile errors? */
830 krb5_error_code code2;
831 code2 = dns_locate_server(context, realm, &al, svc, socktype,
833 if (code2 != KRB5_PLUGIN_NO_HANDLE)
836 #endif /* KRB5_DNS_LOOKUP */
838 /* We could put more heuristics here, like looking up a hostname
839 of "kerberos."+REALM, etc. */
842 Tprintf ("krb5int_locate_server found %d addresses\n",
845 Tprintf ("krb5int_locate_server returning error code %d/%s\n",
846 code, error_message(code));
852 if (al.naddrs == 0) { /* No good servers */
855 krb5_set_error_message(context, KRB5_REALM_CANT_RESOLVE,
856 "Cannot resolve network address for KDC in realm \"%.*s\"",
857 realm->length, realm->data);
859 return KRB5_REALM_CANT_RESOLVE;
866 krb5_locate_kdc(krb5_context context, const krb5_data *realm,
867 struct addrlist *addrlist,
868 int get_masters, int socktype, int family)
870 return krb5int_locate_server(context, realm, addrlist,
872 ? locate_service_master_kdc
873 : locate_service_kdc),