5 * Copyright 1990 by the 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. M.I.T. makes no representations about the suitability of
21 * this software for any purpose. It is provided "as is" without express
22 * or implied warranty.
25 * get socket addresses for KDC.
28 #if !defined(lint) && !defined(SABER)
29 static char rcsid_locate_kdc_c[] =
31 #endif /* !lint & !SABER */
33 #include <krb5/krb5.h>
34 #include <krb5/osconf.h>
36 #include <krb5/ext-proto.h>
38 #include <krb5/los-proto.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
43 #include <netinet/in.h>
49 extern char *krb5_kdc_udp_portname;
50 extern char *krb5_kdc_sec_udp_portname;
54 * returns count of number of addresses found
58 krb5_locate_kdc(realm, addr_pp, naddrs)
59 const krb5_data *realm;
60 struct sockaddr **addr_pp;
66 struct sockaddr *addr_p;
67 struct sockaddr_in *sin_p;
71 u_short udpport = KRB5_DEFAULT_PORT;
72 u_short sec_udpport = KRB5_DEFAULT_SEC_PORT;
77 if (code = krb5_get_krbhst (realm, &hostlist))
81 if (sp = getservbyname(krb5_kdc_udp_portname, "udp"))
83 if (krb5_kdc_sec_udp_portname)
84 if (sp = getservbyname(krb5_kdc_sec_udp_portname, "udp")) {
85 #ifdef KRB5_TRY_SECONDARY_PORT_FIRST
86 sec_udpport = udpport;
89 sec_udpport = sp->s_port;
95 while (hostlist[count])
108 addr_p = (struct sockaddr *)malloc (sizeof (struct sockaddr) * count);
110 for (i=0, out=0; hostlist[i]; i++) {
111 hp = gethostbyname(hostlist[i]);
113 switch (hp->h_addrtype) {
116 if (udpport) /* must have gotten a port # */
117 for (j=0; hp->h_addr_list[j]; j++) {
118 sin_p = (struct sockaddr_in *) &addr_p[out++];
119 memset ((char *)sin_p, 0, sizeof(struct sockaddr));
120 sin_p->sin_family = hp->h_addrtype;
121 sin_p->sin_port = udpport;
122 memcpy((char *)&sin_p->sin_addr,
123 (char *)hp->h_addr_list[j],
124 sizeof(struct in_addr));
127 addr_p = (struct sockaddr *)
128 realloc ((char *)addr_p,
129 sizeof(struct sockaddr) * count);
132 addr_p[out] = addr_p[out-1];
133 sin_p = (struct sockaddr_in *) &addr_p[out++];
134 sin_p->sin_port = sec_udpport;
137 addr_p = (struct sockaddr *)
138 realloc ((char *)addr_p,
139 sizeof(struct sockaddr) * count);
152 free ((char *)hostlist);
154 * XXX need to distinguish between
155 * "can't resolve KDC name" and
156 * "can't find any KDC names"