xref: /freebsd/crypto/heimdal/lib/krb5/get_addrs.c (revision 71fe318b852b8dfb3e799cb12ef184750f7f8eac)
1 /*
2  * Copyright (c) 1997 - 2002 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33 
34 #include "krb5_locl.h"
35 
36 RCSID("$Id: get_addrs.c,v 1.44 2002/08/16 20:50:15 joda Exp $");
37 
38 #ifdef __osf__
39 /* hate */
40 struct rtentry;
41 struct mbuf;
42 #endif
43 #ifdef HAVE_NET_IF_H
44 #include <net/if.h>
45 #endif
46 #include <ifaddrs.h>
47 
48 static krb5_error_code
49 gethostname_fallback (krb5_context context, krb5_addresses *res)
50 {
51     krb5_error_code ret;
52     char hostname[MAXHOSTNAMELEN];
53     struct hostent *hostent;
54 
55     if (gethostname (hostname, sizeof(hostname))) {
56 	ret = errno;
57 	krb5_set_error_string (context, "gethostname: %s", strerror(ret));
58 	return ret;
59     }
60     hostent = roken_gethostbyname (hostname);
61     if (hostent == NULL) {
62 	ret = errno;
63 	krb5_set_error_string (context, "gethostbyname %s: %s",
64 			       hostname, strerror(ret));
65 	return ret;
66     }
67     res->len = 1;
68     res->val = malloc (sizeof(*res->val));
69     if (res->val == NULL) {
70 	krb5_set_error_string(context, "malloc: out of memory");
71 	return ENOMEM;
72     }
73     res->val[0].addr_type = hostent->h_addrtype;
74     res->val[0].address.data = NULL;
75     res->val[0].address.length = 0;
76     ret = krb5_data_copy (&res->val[0].address,
77 			  hostent->h_addr,
78 			  hostent->h_length);
79     if (ret) {
80 	free (res->val);
81 	return ret;
82     }
83     return 0;
84 }
85 
86 enum {
87     LOOP            = 1,	/* do include loopback interfaces */
88     LOOP_IF_NONE    = 2,	/* include loopback if no other if's */
89     EXTRA_ADDRESSES = 4,	/* include extra addresses */
90     SCAN_INTERFACES = 8		/* scan interfaces for addresses */
91 };
92 
93 /*
94  * Try to figure out the addresses of all configured interfaces with a
95  * lot of magic ioctls.
96  */
97 
98 static krb5_error_code
99 find_all_addresses (krb5_context context, krb5_addresses *res, int flags)
100 {
101     struct sockaddr sa_zero;
102     struct ifaddrs *ifa0, *ifa;
103     krb5_error_code ret = ENXIO;
104     int num, idx;
105     krb5_addresses ignore_addresses;
106 
107     res->val = NULL;
108 
109     if (getifaddrs(&ifa0) == -1) {
110 	ret = errno;
111 	krb5_set_error_string(context, "getifaddrs: %s", strerror(ret));
112 	return (ret);
113     }
114 
115     memset(&sa_zero, 0, sizeof(sa_zero));
116 
117     /* First, count all the ifaddrs. */
118     for (ifa = ifa0, num = 0; ifa != NULL; ifa = ifa->ifa_next, num++)
119 	/* nothing */;
120 
121     if (num == 0) {
122 	freeifaddrs(ifa0);
123 	krb5_set_error_string(context, "no addresses found");
124 	return (ENXIO);
125     }
126 
127     if (flags & EXTRA_ADDRESSES) {
128 	/* we'll remove the addresses we don't care about */
129 	ret = krb5_get_ignore_addresses(context, &ignore_addresses);
130 	if(ret)
131 	    return ret;
132     }
133 
134     /* Allocate storage for them. */
135     res->val = calloc(num, sizeof(*res->val));
136     if (res->val == NULL) {
137 	krb5_free_addresses(context, &ignore_addresses);
138 	freeifaddrs(ifa0);
139 	krb5_set_error_string (context, "malloc: out of memory");
140 	return (ENOMEM);
141     }
142 
143     /* Now traverse the list. */
144     for (ifa = ifa0, idx = 0; ifa != NULL; ifa = ifa->ifa_next) {
145 	if ((ifa->ifa_flags & IFF_UP) == 0)
146 	    continue;
147 	if (memcmp(ifa->ifa_addr, &sa_zero, sizeof(sa_zero)) == 0)
148 	    continue;
149 	if (krb5_sockaddr_uninteresting(ifa->ifa_addr))
150 	    continue;
151 	if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) {
152 	    /* We'll deal with the LOOP_IF_NONE case later. */
153 	    if ((flags & LOOP) == 0)
154 		continue;
155 	}
156 
157 	ret = krb5_sockaddr2address(context, ifa->ifa_addr, &res->val[idx]);
158 	if (ret) {
159 	    /*
160 	     * The most likely error here is going to be "Program
161 	     * lacks support for address type".  This is no big
162 	     * deal -- just continue, and we'll listen on the
163 	     * addresses who's type we *do* support.
164 	     */
165 	    continue;
166 	}
167 	/* possibly skip this address? */
168 	if((flags & EXTRA_ADDRESSES) &&
169 	   krb5_address_search(context, &res->val[idx], &ignore_addresses)) {
170 	    krb5_free_address(context, &res->val[idx]);
171 	    flags &= ~LOOP_IF_NONE; /* we actually found an address,
172                                        so don't add any loop-back
173                                        addresses */
174 	    continue;
175 	}
176 
177 	idx++;
178     }
179 
180     /*
181      * If no addresses were found, and LOOP_IF_NONE is set, then find
182      * the loopback addresses and add them to our list.
183      */
184     if ((flags & LOOP_IF_NONE) != 0 && idx == 0) {
185 	for (ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next) {
186 	    if ((ifa->ifa_flags & IFF_UP) == 0)
187 		continue;
188 	    if (memcmp(ifa->ifa_addr, &sa_zero, sizeof(sa_zero)) == 0)
189 		continue;
190 	    if (krb5_sockaddr_uninteresting(ifa->ifa_addr))
191 		continue;
192 
193 	    if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) {
194 		ret = krb5_sockaddr2address(context,
195 					    ifa->ifa_addr, &res->val[idx]);
196 		if (ret) {
197 		    /*
198 		     * See comment above.
199 		     */
200 		    continue;
201 		}
202 		if((flags & EXTRA_ADDRESSES) &&
203 		   krb5_address_search(context, &res->val[idx],
204 				       &ignore_addresses)) {
205 		    krb5_free_address(context, &res->val[idx]);
206 		    continue;
207 		}
208 		idx++;
209 	    }
210 	}
211     }
212 
213     if (flags & EXTRA_ADDRESSES)
214 	krb5_free_addresses(context, &ignore_addresses);
215     freeifaddrs(ifa0);
216     if (ret)
217 	free(res->val);
218     else
219 	res->len = idx;        /* Now a count. */
220     return (ret);
221 }
222 
223 static krb5_error_code
224 get_addrs_int (krb5_context context, krb5_addresses *res, int flags)
225 {
226     krb5_error_code ret = -1;
227 
228     if (flags & SCAN_INTERFACES) {
229 	ret = find_all_addresses (context, res, flags);
230 	if(ret || res->len == 0)
231 	    ret = gethostname_fallback (context, res);
232     } else {
233 	res->len = 0;
234 	res->val = NULL;
235 	ret = 0;
236     }
237 
238     if(ret == 0 && (flags & EXTRA_ADDRESSES)) {
239 	krb5_addresses a;
240 	/* append user specified addresses */
241 	ret = krb5_get_extra_addresses(context, &a);
242 	if(ret) {
243 	    krb5_free_addresses(context, res);
244 	    return ret;
245 	}
246 	ret = krb5_append_addresses(context, res, &a);
247 	if(ret) {
248 	    krb5_free_addresses(context, res);
249 	    return ret;
250 	}
251 	krb5_free_addresses(context, &a);
252     }
253     if(res->len == 0) {
254 	free(res->val);
255 	res->val = NULL;
256     }
257     return ret;
258 }
259 
260 /*
261  * Try to get all addresses, but return the one corresponding to
262  * `hostname' if we fail.
263  *
264  * Only include loopback address if there are no other.
265  */
266 
267 krb5_error_code
268 krb5_get_all_client_addrs (krb5_context context, krb5_addresses *res)
269 {
270     int flags = LOOP_IF_NONE | EXTRA_ADDRESSES;
271 
272     if (context->scan_interfaces)
273 	flags |= SCAN_INTERFACES;
274 
275     return get_addrs_int (context, res, flags);
276 }
277 
278 /*
279  * Try to get all local addresses that a server should listen to.
280  * If that fails, we return the address corresponding to `hostname'.
281  */
282 
283 krb5_error_code
284 krb5_get_all_server_addrs (krb5_context context, krb5_addresses *res)
285 {
286     return get_addrs_int (context, res, LOOP | SCAN_INTERFACES);
287 }
288