pull up r24055 from trunk
[krb5.git] / src / lib / krb5 / os / locate_kdc.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * lib/krb5/os/locate_kdc.c
4  *
5  * Copyright 1990,2000,2001,2002,2003,2004,2006,2008 Massachusetts Institute of Technology.
6  * All Rights Reserved.
7  *
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.
12  *
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.
26  *
27  *
28  * get socket addresses for KDC.
29  */
30
31 #include "fake-addrinfo.h"
32 #include "k5-int.h"
33 #include "os-proto.h"
34 #include <stdio.h>
35 #ifdef KRB5_DNS_LOOKUP
36 #ifdef WSHELPER
37 #include <wshelper.h>
38 #else /* WSHELPER */
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <arpa/nameser.h>
42 #include <resolv.h>
43 #include <netdb.h>
44 #endif /* WSHELPER */
45 #ifndef T_SRV
46 #define T_SRV 33
47 #endif /* T_SRV */
48
49 /* for old Unixes and friends ... */
50 #ifndef MAXHOSTNAMELEN
51 #define MAXHOSTNAMELEN 64
52 #endif
53
54 #define MAX_DNS_NAMELEN (15*(MAXHOSTNAMELEN + 1)+1)
55
56 #if KRB5_DNS_LOOKUP_KDC
57 #define DEFAULT_LOOKUP_KDC 1
58 #else
59 #define DEFAULT_LOOKUP_KDC 0
60 #endif
61 #if KRB5_DNS_LOOKUP_REALM
62 #define DEFAULT_LOOKUP_REALM 1
63 #else
64 #define DEFAULT_LOOKUP_REALM 0
65 #endif
66
67 static int
68 maybe_use_dns (krb5_context context, const char *name, int defalt)
69 {
70     krb5_error_code code;
71     char * value = NULL;
72     int use_dns = 0;
73
74     code = profile_get_string(context->profile, KRB5_CONF_LIBDEFAULTS,
75                               name, 0, 0, &value);
76     if (value == 0 && code == 0)
77         code = profile_get_string(context->profile, KRB5_CONF_LIBDEFAULTS,
78                                   KRB5_CONF_DNS_FALLBACK, 0, 0, &value);
79     if (code)
80         return defalt;
81
82     if (value == 0)
83         return defalt;
84
85     use_dns = _krb5_conf_boolean(value);
86     profile_release_string(value);
87     return use_dns;
88 }
89
90 int
91 _krb5_use_dns_kdc(krb5_context context)
92 {
93     return maybe_use_dns (context, KRB5_CONF_DNS_LOOKUP_KDC, DEFAULT_LOOKUP_KDC);
94 }
95
96 int
97 _krb5_use_dns_realm(krb5_context context)
98 {
99     return maybe_use_dns (context, KRB5_CONF_DNS_LOOKUP_REALM, DEFAULT_LOOKUP_REALM);
100 }
101
102 #endif /* KRB5_DNS_LOOKUP */
103
104 int
105 krb5int_grow_addrlist (struct addrlist *lp, int nmore)
106 {
107     size_t i;
108     size_t newspace = lp->space + nmore;
109     size_t newsize = newspace * sizeof (*lp->addrs);
110     void *newaddrs;
111
112     newaddrs = realloc (lp->addrs, newsize);
113     if (newaddrs == NULL)
114         return ENOMEM;
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;
120     }
121     lp->space = newspace;
122     return 0;
123 }
124 #define grow_list krb5int_grow_addrlist
125
126 /* Free up everything pointed to by the addrlist structure, but don't
127    free the structure itself.  */
128 void
129 krb5int_free_addrlist (struct addrlist *lp)
130 {
131     size_t i;
132     for (i = 0; i < lp->naddrs; i++)
133         if (lp->addrs[i].freefn)
134             (lp->addrs[i].freefn)(lp->addrs[i].data);
135     free (lp->addrs);
136     lp->addrs = NULL;
137     lp->naddrs = lp->space = 0;
138 }
139 #define free_list krb5int_free_addrlist
140
141 static int
142 translate_ai_error (int err)
143 {
144     switch (err) {
145     case 0:
146         return 0;
147     case EAI_BADFLAGS:
148     case EAI_FAMILY:
149     case EAI_SOCKTYPE:
150     case EAI_SERVICE:
151         /* All of these indicate bad inputs to getaddrinfo.  */
152         return EINVAL;
153     case EAI_AGAIN:
154         /* Translate to standard errno code.  */
155         return EAGAIN;
156     case EAI_MEMORY:
157         /* Translate to standard errno code.  */
158         return ENOMEM;
159 #ifdef EAI_ADDRFAMILY
160     case EAI_ADDRFAMILY:
161 #endif
162 #if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME
163     case EAI_NODATA:
164 #endif
165     case EAI_NONAME:
166         /* Name not known or no address data, but no error.  Do
167            nothing more.  */
168         return 0;
169 #ifdef EAI_OVERFLOW
170     case EAI_OVERFLOW:
171         /* An argument buffer overflowed.  */
172         return EINVAL;          /* XXX */
173 #endif
174 #ifdef EAI_SYSTEM
175     case EAI_SYSTEM:
176         /* System error, obviously.  */
177         return errno;
178 #endif
179     default:
180         /* An error code we haven't handled?  */
181         return EINVAL;
182     }
183 }
184
185 #include <stdarg.h>
186 static inline void
187 Tprintf(const char *fmt, ...)
188 {
189 #ifdef TEST
190     va_list ap;
191     va_start(ap, fmt);
192     vfprintf(stderr, fmt, ap);
193     va_end(ap);
194 #endif
195 }
196
197 #if 0
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);
202 #else
203 static inline void dprint(const char *fmt, ...) { }
204 static inline void print_addrlist(const struct addrlist *a) { }
205 #endif
206
207 static int
208 add_addrinfo_to_list(struct addrlist *lp, struct addrinfo *a,
209                      void (*freefn)(void *), void *data)
210 {
211     int err;
212
213     dprint("\tadding %p=%A to %p (naddrs=%d space=%d)\n", a, a, lp,
214            lp->naddrs, lp->space);
215
216     if (lp->naddrs == lp->space) {
217         err = grow_list (lp, 1);
218         if (err) {
219             Tprintf ("grow_list failed %d\n", err);
220             return err;
221         }
222     }
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;
227     lp->naddrs++;
228     Tprintf ("\tcount is now %lu: ", (unsigned long) lp->naddrs);
229     print_addrlist(lp);
230     Tprintf("\n");
231     return 0;
232 }
233
234 #define add_host_to_list krb5int_add_host_to_list
235
236 static void
237 call_freeaddrinfo(void *data)
238 {
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.  */
242     freeaddrinfo(data);
243 }
244
245 int
246 krb5int_add_host_to_list (struct addrlist *lp, const char *hostname,
247                           int port, int secport,
248                           int socktype, int family)
249 {
250     struct addrinfo *addrs, *a, *anext, hint;
251     int err, result;
252     char portbuf[10], secportbuf[10];
253     void (*freefn)(void *);
254
255     Tprintf ("adding hostname %s, ports %d,%d, family %d, socktype %d\n",
256              hostname, ntohs (port), ntohs (secport),
257              family, socktype);
258
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;
264 #endif
265     result = snprintf(portbuf, sizeof(portbuf), "%d", ntohs(port));
266     if (SNPRINTF_OVERFLOW(result, sizeof(portbuf)))
267         /* XXX */
268         return EINVAL;
269     result = snprintf(secportbuf, sizeof(secportbuf), "%d", ntohs(secport));
270     if (SNPRINTF_OVERFLOW(result, sizeof(secportbuf)))
271         return EINVAL;
272     err = getaddrinfo (hostname, portbuf, &hint, &addrs);
273     if (err) {
274         Tprintf ("\tgetaddrinfo(\"%s\", \"%s\", ...)\n\treturns %d: %s\n",
275                  hostname, portbuf, err, gai_strerror (err));
276         return translate_ai_error (err);
277     }
278     freefn = call_freeaddrinfo;
279     anext = 0;
280     for (a = addrs; a != 0 && err == 0; a = anext, freefn = 0) {
281         anext = a->ai_next;
282         err = add_addrinfo_to_list (lp, a, freefn, a);
283     }
284     if (err || secport == 0)
285         goto egress;
286     if (socktype == 0)
287         socktype = SOCK_DGRAM;
288     else if (socktype != SOCK_DGRAM)
289         goto egress;
290     hint.ai_family = AF_INET;
291     err = getaddrinfo (hostname, secportbuf, &hint, &addrs);
292     if (err) {
293         err = translate_ai_error (err);
294         goto egress;
295     }
296     freefn = call_freeaddrinfo;
297     for (a = addrs; a != 0 && err == 0; a = anext, freefn = 0) {
298         anext = a->ai_next;
299         err = add_addrinfo_to_list (lp, a, freefn, a);
300     }
301 egress:
302     /* XXX Memory leaks possible here if add_addrinfo_to_list fails.  */
303     return err;
304 }
305
306 /*
307  * returns count of number of addresses found
308  * if master is non-NULL, it is filled in with the index of
309  * the master kdc
310  */
311
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)
317 {
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;
322
323     Tprintf ("looking in krb5.conf for realm %s entry %s; ports %d,%d\n",
324              realm->data, name, ntohs (udpport), ntohs (sec_udpport));
325
326     if ((host = malloc(realm->length + 1)) == NULL)
327         return ENOMEM;
328
329     strncpy(host, realm->data, realm->length);
330     host[realm->length] = '\0';
331     hostlist = 0;
332
333     masterlist = NULL;
334
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;
339
340     code = profile_get_values(context->profile, realm_srv_names, &hostlist);
341
342     if (code) {
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;
347         free(host);
348         return code;
349     }
350
351     count = 0;
352     while (hostlist && hostlist[count])
353         count++;
354     Tprintf ("found %d entries under 'kdc'\n", count);
355
356     if (count == 0) {
357         profile_free_list(hostlist);
358         free(host);
359         addrlist->naddrs = 0;
360         return 0;
361     }
362
363     if (get_masters) {
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;
368
369         code = profile_get_values(context->profile, realm_srv_names,
370                                   &masterlist);
371
372         free(host);
373
374         if (code == 0) {
375             for (i=0; masterlist[i]; i++) {
376                 host = masterlist[i];
377                 /* Strip off excess characters. */
378                 if (*host == '[' && (cp = strchr(host, ']')))
379                     *(cp + 1) = '\0';
380                 else
381                     *(host + strcspn(host, " \t:")) = '\0';
382             }
383         }
384     } else {
385         free(host);
386     }
387
388     /* at this point, if master is non-NULL, then either the master kdc
389        is required, and there is one, or the master kdc is not required,
390        and there may or may not be one. */
391
392 #ifdef HAVE_NETINET_IN_H
393     if (sec_udpport)
394         count = count * 2;
395 #endif
396
397     for (i=0; hostlist[i]; i++) {
398         int p1, p2;
399
400         host = hostlist[i];
401         Tprintf ("entry %d is '%s'\n", i, host);
402         /* Find port number, and strip off any excess characters. */
403         if (*host == '[' && (cp = strchr(host, ']')))
404             cp = cp + 1;
405         else
406             cp = host + strcspn(host, " \t:");
407         port = (*cp == ':') ? cp + 1 : NULL;
408         *cp = '\0';
409
410         ismaster = 0;
411         if (masterlist) {
412             for (j=0; masterlist[j]; j++) {
413                 if (strcasecmp(hostlist[i], masterlist[j]) == 0) {
414                     ismaster = 1;
415                 }
416             }
417         }
418
419         if (get_masters && !ismaster)
420             continue;
421
422         if (port) {
423             unsigned long l;
424 #ifdef HAVE_STROUL
425             char *endptr;
426             l = strtoul (port, &endptr, 10);
427             if (endptr == NULL || *endptr != 0)
428                 return EINVAL;
429 #else
430             l = atoi (port);
431 #endif
432             /* L is unsigned, don't need to check <0.  */
433             if (l > 65535)
434                 return EINVAL;
435             p1 = htons (l);
436             p2 = 0;
437         } else {
438             p1 = udpport;
439             p2 = sec_udpport;
440         }
441
442         /* If the hostname was in brackets, strip those off now. */
443         if (*host == '[' && (cp = strchr(host, ']'))) {
444             host++;
445             *cp = '\0';
446         }
447
448         if (socktype != 0)
449             code = add_host_to_list(addrlist, host, p1, p2, socktype, family);
450         else {
451             code = add_host_to_list(addrlist, host, p1, p2, SOCK_DGRAM,
452                                     family);
453             if (code == 0)
454                 code = add_host_to_list(addrlist, host, p1, p2, SOCK_STREAM,
455                                         family);
456         }
457         if (code) {
458             Tprintf ("error %d (%s) returned from add_host_to_list\n", code,
459                      error_message (code));
460             if (hostlist)
461                 profile_free_list (hostlist);
462             if (masterlist)
463                 profile_free_list (masterlist);
464             return code;
465         }
466     }
467
468     if (hostlist)
469         profile_free_list(hostlist);
470     if (masterlist)
471         profile_free_list(masterlist);
472
473     return 0;
474 }
475
476 #ifdef TEST
477 static krb5_error_code
478 krb5_locate_srv_conf(krb5_context context, const krb5_data *realm,
479                      const char *name, struct addrlist *al, int get_masters,
480                      int udpport, int sec_udpport)
481 {
482     krb5_error_code ret;
483
484     ret = krb5_locate_srv_conf_1 (context, realm, name, al,
485                                   get_masters, 0, udpport, sec_udpport, 0);
486     if (ret)
487         return ret;
488     if (al->naddrs == 0)        /* Couldn't resolve any KDC names */
489         return KRB5_REALM_CANT_RESOLVE;
490     return 0;
491 }
492 #endif
493
494 #ifdef KRB5_DNS_LOOKUP
495 static krb5_error_code
496 krb5_locate_srv_dns_1 (const krb5_data *realm,
497                        const char *service,
498                        const char *protocol,
499                        struct addrlist *addrlist,
500                        int family)
501 {
502     struct srv_dns_entry *head = NULL;
503     struct srv_dns_entry *entry = NULL, *next;
504     krb5_error_code code = 0;
505
506     code = krb5int_make_srv_query_realm(realm, service, protocol, &head);
507     if (code)
508         return 0;
509
510     /*
511      * Okay!  Now we've got a linked list of entries sorted by
512      * priority.  Start looking up A records and returning
513      * addresses.
514      */
515
516     if (head == NULL)
517         return 0;
518
519     /* Check for the "." case indicating no support.  */
520     if (head->next == 0 && head->host[0] == 0) {
521         free(head->host);
522         free(head);
523         return KRB5_ERR_NO_SERVICE;
524     }
525
526     Tprintf ("walking answer list:\n");
527     for (entry = head; entry != NULL; entry = next) {
528         Tprintf ("\tport=%d host=%s\n", entry->port, entry->host);
529         next = entry->next;
530         code = add_host_to_list (addrlist, entry->host, htons (entry->port), 0,
531                                  (strcmp("_tcp", protocol)
532                                   ? SOCK_DGRAM
533                                   : SOCK_STREAM), family);
534         if (code) {
535             break;
536         }
537         if (entry == head) {
538             free(entry->host);
539             free(entry);
540             head = next;
541             entry = 0;
542         }
543     }
544     Tprintf ("[end]\n");
545
546     krb5int_free_srv_dns_data(head);
547     return code;
548 }
549 #endif
550
551 #include <krb5/locate_plugin.h>
552
553 #if TARGET_OS_MAC
554 static const char *objdirs[] = { KRB5_PLUGIN_BUNDLE_DIR, LIBDIR "/krb5/plugins/libkrb5", NULL }; /* should be a list */
555 #else
556 static const char *objdirs[] = { LIBDIR "/krb5/plugins/libkrb5", NULL };
557 #endif
558
559 struct module_callback_data {
560     int out_of_mem;
561     struct addrlist *lp;
562 };
563
564 static int
565 module_callback (void *cbdata, int socktype, struct sockaddr *sa)
566 {
567     struct module_callback_data *d = cbdata;
568     struct {
569         struct addrinfo ai;
570         union {
571             struct sockaddr_in sin;
572 #ifdef KRB5_USE_INET6
573             struct sockaddr_in6 sin6;
574 #endif
575         } u;
576     } *x;
577
578     if (socktype != SOCK_STREAM && socktype != SOCK_DGRAM)
579         return 0;
580     if (sa->sa_family != AF_INET
581 #ifdef KRB5_USE_INET6
582         && sa->sa_family != AF_INET6
583 #endif
584     )
585         return 0;
586     x = calloc (1, sizeof (*x));
587     if (x == 0) {
588         d->out_of_mem = 1;
589         return 1;
590     }
591     x->ai.ai_addr = (struct sockaddr *) &x->u;
592     x->ai.ai_socktype = socktype;
593     x->ai.ai_family = sa->sa_family;
594     if (sa->sa_family == AF_INET) {
595         x->u.sin = *(struct sockaddr_in *)sa;
596         x->ai.ai_addrlen = sizeof(struct sockaddr_in);
597     }
598 #ifdef KRB5_USE_INET6
599     if (sa->sa_family == AF_INET6) {
600         x->u.sin6 = *(struct sockaddr_in6 *)sa;
601         x->ai.ai_addrlen = sizeof(struct sockaddr_in6);
602     }
603 #endif
604     if (add_addrinfo_to_list (d->lp, &x->ai, free, x) != 0) {
605         /* Assumes only error is ENOMEM.  */
606         d->out_of_mem = 1;
607         return 1;
608     }
609     return 0;
610 }
611
612 static krb5_error_code
613 module_locate_server (krb5_context ctx, const krb5_data *realm,
614                       struct addrlist *addrlist,
615                       enum locate_service_type svc, int socktype, int family)
616 {
617     struct krb5plugin_service_locate_result *res = NULL;
618     krb5_error_code code;
619     struct krb5plugin_service_locate_ftable *vtbl = NULL;
620     void **ptrs;
621     char *realmz;               /* NUL-terminated realm */
622     int i;
623     struct module_callback_data cbdata = { 0, };
624     const char *msg;
625
626     Tprintf("in module_locate_server\n");
627     cbdata.lp = addrlist;
628     if (!PLUGIN_DIR_OPEN (&ctx->libkrb5_plugins)) {
629
630         code = krb5int_open_plugin_dirs (objdirs, NULL, &ctx->libkrb5_plugins,
631                                          &ctx->err);
632         if (code)
633             return KRB5_PLUGIN_NO_HANDLE;
634     }
635
636     code = krb5int_get_plugin_dir_data (&ctx->libkrb5_plugins,
637                                         "service_locator", &ptrs, &ctx->err);
638     if (code) {
639         Tprintf("error looking up plugin symbols: %s\n",
640                 (msg = krb5_get_error_message(ctx, code)));
641         krb5_free_error_message(ctx, msg);
642         return KRB5_PLUGIN_NO_HANDLE;
643     }
644
645     if (realm->length >= UINT_MAX) {
646         krb5int_free_plugin_dir_data(ptrs);
647         return ENOMEM;
648     }
649     realmz = malloc(realm->length + 1);
650     if (realmz == NULL) {
651         krb5int_free_plugin_dir_data(ptrs);
652         return ENOMEM;
653     }
654     memcpy(realmz, realm->data, realm->length);
655     realmz[realm->length] = '\0';
656     for (i = 0; ptrs[i]; i++) {
657         void *blob;
658
659         vtbl = ptrs[i];
660         Tprintf("element %d is %p\n", i, ptrs[i]);
661
662         /* For now, don't keep the plugin data alive.  For long-lived
663            contexts, it may be desirable to change that later.  */
664         code = vtbl->init(ctx, &blob);
665         if (code)
666             continue;
667
668         code = vtbl->lookup(blob, svc, realmz, socktype, family,
669                             module_callback, &cbdata);
670         vtbl->fini(blob);
671         if (code == KRB5_PLUGIN_NO_HANDLE) {
672             /* Module passes, keep going.  */
673             /* XXX */
674             Tprintf("plugin doesn't handle this realm (KRB5_PLUGIN_NO_HANDLE)\n");
675             continue;
676         }
677         if (code != 0) {
678             /* Module encountered an actual error.  */
679             Tprintf("plugin lookup routine returned error %d: %s\n",
680                     code, error_message(code));
681             free(realmz);
682             krb5int_free_plugin_dir_data (ptrs);
683             return code;
684         }
685         break;
686     }
687     if (ptrs[i] == NULL) {
688         Tprintf("ran off end of plugin list\n");
689         free(realmz);
690         krb5int_free_plugin_dir_data (ptrs);
691         return KRB5_PLUGIN_NO_HANDLE;
692     }
693     Tprintf("stopped with plugin #%d, res=%p\n", i, res);
694
695     /* Got something back, yippee.  */
696     Tprintf("now have %lu addrs in list %p\n",
697             (unsigned long) addrlist->naddrs, addrlist);
698     print_addrlist(addrlist);
699     free(realmz);
700     krb5int_free_plugin_dir_data (ptrs);
701     return 0;
702 }
703
704 static krb5_error_code
705 prof_locate_server (krb5_context context, const krb5_data *realm,
706                     struct addrlist *addrlist,
707                     enum locate_service_type svc, int socktype, int family)
708 {
709     const char *profname;
710     int dflport1, dflport2 = 0;
711     struct servent *serv;
712
713     switch (svc) {
714     case locate_service_kdc:
715         profname = KRB5_CONF_KDC;
716         /* We used to use /etc/services for these, but enough systems
717            have old, crufty, wrong settings that this is probably
718            better.  */
719     kdc_ports:
720         dflport1 = htons(KRB5_DEFAULT_PORT);
721         dflport2 = htons(KRB5_DEFAULT_SEC_PORT);
722         break;
723     case locate_service_master_kdc:
724         profname = KRB5_CONF_MASTER_KDC;
725         goto kdc_ports;
726     case locate_service_kadmin:
727         profname = KRB5_CONF_ADMIN_SERVER;
728         dflport1 = htons(DEFAULT_KADM5_PORT);
729         break;
730     case locate_service_krb524:
731         profname = KRB5_CONF_KRB524_SERVER;
732         serv = getservbyname(KRB524_SERVICE, "udp");
733         dflport1 = serv ? serv->s_port : htons (KRB524_PORT);
734         break;
735     case locate_service_kpasswd:
736         profname = KRB5_CONF_KPASSWD_SERVER;
737         dflport1 = htons(DEFAULT_KPASSWD_PORT);
738         break;
739     default:
740         return EBUSY;           /* XXX */
741     }
742
743     return krb5_locate_srv_conf_1 (context, realm, profname, addrlist,
744                                    0, socktype,
745                                    dflport1, dflport2, family);
746 }
747
748 static krb5_error_code
749 dns_locate_server (krb5_context context, const krb5_data *realm,
750                    struct addrlist *addrlist,
751                    enum locate_service_type svc, int socktype, int family)
752 {
753     const char *dnsname;
754     int use_dns = _krb5_use_dns_kdc(context);
755     krb5_error_code code;
756
757     if (!use_dns)
758         return KRB5_PLUGIN_NO_HANDLE;
759
760     switch (svc) {
761     case locate_service_kdc:
762         dnsname = "_kerberos";
763         break;
764     case locate_service_master_kdc:
765         dnsname = "_kerberos-master";
766         break;
767     case locate_service_kadmin:
768         dnsname = "_kerberos-adm";
769         break;
770     case locate_service_krb524:
771         dnsname = "_krb524";
772         break;
773     case locate_service_kpasswd:
774         dnsname = "_kpasswd";
775         break;
776     default:
777         return KRB5_PLUGIN_NO_HANDLE;
778     }
779
780     code = 0;
781     if (socktype == SOCK_DGRAM || socktype == 0) {
782         code = krb5_locate_srv_dns_1(realm, dnsname, "_udp", addrlist, family);
783         if (code)
784             Tprintf("dns udp lookup returned error %d\n", code);
785     }
786     if ((socktype == SOCK_STREAM || socktype == 0) && code == 0) {
787         code = krb5_locate_srv_dns_1(realm, dnsname, "_tcp", addrlist, family);
788         if (code)
789             Tprintf("dns tcp lookup returned error %d\n", code);
790     }
791     return code;
792 }
793
794 /*
795  * Wrapper function for the various backends
796  */
797
798 krb5_error_code
799 krb5int_locate_server (krb5_context context, const krb5_data *realm,
800                        struct addrlist *addrlist,
801                        enum locate_service_type svc,
802                        int socktype, int family)
803 {
804     krb5_error_code code;
805     struct addrlist al = ADDRLIST_INIT;
806
807     *addrlist = al;
808
809     if (realm == NULL || realm->data == NULL || realm->data[0] == 0) {
810         krb5_set_error_message(context, KRB5_REALM_CANT_RESOLVE,
811                                "Cannot find KDC for invalid realm name \"\"");
812         return KRB5_REALM_CANT_RESOLVE;
813     }
814
815     code = module_locate_server(context, realm, &al, svc, socktype, family);
816     Tprintf("module_locate_server returns %d\n", code);
817     if (code == KRB5_PLUGIN_NO_HANDLE) {
818         /*
819          * We always try the local file before DNS.  Note that there
820          * is no way to indicate "service not available" via the
821          * config file.
822          */
823
824         code = prof_locate_server(context, realm, &al, svc, socktype, family);
825
826 #ifdef KRB5_DNS_LOOKUP
827         if (code) {             /* Try DNS for all profile errors?  */
828             krb5_error_code code2;
829             code2 = dns_locate_server(context, realm, &al, svc, socktype,
830                                       family);
831             if (code2 != KRB5_PLUGIN_NO_HANDLE)
832                 code = code2;
833         }
834 #endif /* KRB5_DNS_LOOKUP */
835
836         /* We could put more heuristics here, like looking up a hostname
837            of "kerberos."+REALM, etc.  */
838     }
839     if (code == 0)
840         Tprintf ("krb5int_locate_server found %d addresses\n",
841                  al.naddrs);
842     else
843         Tprintf ("krb5int_locate_server returning error code %d/%s\n",
844                  code, error_message(code));
845     if (code != 0) {
846         if (al.space)
847             free_list (&al);
848         return code;
849     }
850     if (al.naddrs == 0) {       /* No good servers */
851         if (al.space)
852             free_list (&al);
853         krb5_set_error_message(context, KRB5_REALM_CANT_RESOLVE,
854                                "Cannot resolve network address for KDC in realm \"%.*s\"",
855                                realm->length, realm->data);
856
857         return KRB5_REALM_CANT_RESOLVE;
858     }
859     *addrlist = al;
860     return 0;
861 }
862
863 krb5_error_code
864 krb5_locate_kdc(krb5_context context, const krb5_data *realm,
865                 struct addrlist *addrlist,
866                 int get_masters, int socktype, int family)
867 {
868     return krb5int_locate_server(context, realm, addrlist,
869                                  (get_masters
870                                   ? locate_service_master_kdc
871                                   : locate_service_kdc),
872                                  socktype, family);
873 }