Patch to include non-loopback addresses from loopback interfaces.
[heimdal.git] / lib / krb5 / get_addrs.c
blob8c692ecec205b7b5a6e107f77ffbe77bf59b73ef
1 /*
2 * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "krb5_locl.h"
36 #ifdef __osf__
37 /* hate */
38 struct rtentry;
39 struct mbuf;
40 #endif
41 #ifdef HAVE_NET_IF_H
42 #include <net/if.h>
43 #endif
44 #include <ifaddrs.h>
46 static krb5_error_code
47 gethostname_fallback (krb5_context context, krb5_addresses *res)
49 krb5_error_code ret;
50 char hostname[MAXHOSTNAMELEN];
51 struct hostent *hostent;
53 if (gethostname (hostname, sizeof(hostname))) {
54 ret = errno;
55 krb5_set_error_message(context, ret, "gethostname: %s", strerror(ret));
56 return ret;
58 hostent = roken_gethostbyname (hostname);
59 if (hostent == NULL) {
60 ret = errno;
61 krb5_set_error_message (context, ret, "gethostbyname %s: %s",
62 hostname, strerror(ret));
63 return ret;
65 res->len = 1;
66 res->val = malloc (sizeof(*res->val));
67 if (res->val == NULL) {
68 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
69 return ENOMEM;
71 res->val[0].addr_type = hostent->h_addrtype;
72 res->val[0].address.data = NULL;
73 res->val[0].address.length = 0;
74 ret = krb5_data_copy (&res->val[0].address,
75 hostent->h_addr,
76 hostent->h_length);
77 if (ret) {
78 free (res->val);
79 return ret;
81 return 0;
84 enum {
85 LOOP = 1, /* do include loopback interfaces XXX remove */
86 LOOP_IF_NONE = 2, /* include loopback if no other if's */
87 EXTRA_ADDRESSES = 4, /* include extra addresses */
88 SCAN_INTERFACES = 8 /* scan interfaces for addresses */
92 * Try to figure out the addresses of all configured interfaces with a
93 * lot of magic ioctls.
96 static krb5_error_code
97 find_all_addresses (krb5_context context, krb5_addresses *res, int flags)
99 struct sockaddr sa_zero;
100 struct ifaddrs *ifa0, *ifa;
101 krb5_error_code ret = ENXIO;
102 unsigned int num, idx;
103 krb5_addresses ignore_addresses;
105 if (getifaddrs(&ifa0) == -1) {
106 ret = errno;
107 krb5_set_error_message(context, ret, "getifaddrs: %s", strerror(ret));
108 return (ret);
111 memset(&sa_zero, 0, sizeof(sa_zero));
113 /* First, count all the ifaddrs. */
114 for (ifa = ifa0, num = 0; ifa != NULL; ifa = ifa->ifa_next, num++)
115 /* nothing */;
117 if (num == 0) {
118 freeifaddrs(ifa0);
119 krb5_set_error_message(context, ENXIO, N_("no addresses found", ""));
120 return (ENXIO);
123 if (flags & EXTRA_ADDRESSES) {
124 /* we'll remove the addresses we don't care about */
125 ret = krb5_get_ignore_addresses(context, &ignore_addresses);
126 if(ret)
127 return ret;
130 /* Allocate storage for them. */
131 res->val = calloc(num, sizeof(*res->val));
132 if (res->val == NULL) {
133 krb5_free_addresses(context, &ignore_addresses);
134 freeifaddrs(ifa0);
135 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
136 return ENOMEM;
139 /* Now traverse the list. */
140 for (ifa = ifa0, idx = 0; ifa != NULL; ifa = ifa->ifa_next) {
141 if ((ifa->ifa_flags & IFF_UP) == 0)
142 continue;
143 if (ifa->ifa_addr == NULL)
144 continue;
145 if (memcmp(ifa->ifa_addr, &sa_zero, sizeof(sa_zero)) == 0)
146 continue;
147 if (krb5_sockaddr_uninteresting(ifa->ifa_addr))
148 continue;
149 if (krb5_sockaddr_is_loopback(ifa->ifa_addr))
150 continue;
151 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0 && (flags & LOOP) == 0)
152 /* We'll deal with the LOOP_IF_NONE case later. */
153 continue;
155 ret = krb5_sockaddr2address(context, ifa->ifa_addr, &res->val[idx]);
156 if (ret) {
158 * The most likely error here is going to be "Program
159 * lacks support for address type". This is no big
160 * deal -- just continue, and we'll listen on the
161 * addresses who's type we *do* support.
163 continue;
165 /* possibly skip this address? */
166 if((flags & EXTRA_ADDRESSES) &&
167 krb5_address_search(context, &res->val[idx], &ignore_addresses)) {
168 krb5_free_address(context, &res->val[idx]);
169 flags &= ~LOOP_IF_NONE; /* we actually found an address,
170 so don't add any loop-back
171 addresses */
172 continue;
175 idx++;
179 * If no addresses were found, and LOOP_IF_NONE is set, then find
180 * the loopback addresses and add them to our list.
182 if ((flags & LOOP_IF_NONE) != 0 && idx == 0) {
183 for (ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next) {
184 if ((ifa->ifa_flags & IFF_UP) == 0)
185 continue;
186 if (ifa->ifa_addr == NULL)
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 /* XXX Do we really want to allow loopback addresses here? */
194 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) {
195 ret = krb5_sockaddr2address(context,
196 ifa->ifa_addr, &res->val[idx]);
197 if (ret) {
199 * See comment above.
201 continue;
203 if((flags & EXTRA_ADDRESSES) &&
204 krb5_address_search(context, &res->val[idx],
205 &ignore_addresses)) {
206 krb5_free_address(context, &res->val[idx]);
207 continue;
209 idx++;
214 if (flags & EXTRA_ADDRESSES)
215 krb5_free_addresses(context, &ignore_addresses);
216 freeifaddrs(ifa0);
217 if (ret) {
218 free(res->val);
219 res->val = NULL;
220 } else
221 res->len = idx; /* Now a count. */
222 return (ret);
225 static krb5_error_code
226 get_addrs_int (krb5_context context, krb5_addresses *res, int flags)
228 krb5_error_code ret = -1;
230 res->len = 0;
231 res->val = NULL;
233 if (flags & SCAN_INTERFACES) {
234 ret = find_all_addresses (context, res, flags);
235 if(ret || res->len == 0)
236 ret = gethostname_fallback (context, res);
237 } else {
238 ret = 0;
241 if(ret == 0 && (flags & EXTRA_ADDRESSES)) {
242 krb5_addresses a;
243 /* append user specified addresses */
244 ret = krb5_get_extra_addresses(context, &a);
245 if(ret) {
246 krb5_free_addresses(context, res);
247 return ret;
249 ret = krb5_append_addresses(context, res, &a);
250 if(ret) {
251 krb5_free_addresses(context, res);
252 return ret;
254 krb5_free_addresses(context, &a);
256 if(res->len == 0) {
257 free(res->val);
258 res->val = NULL;
260 return ret;
264 * Try to get all addresses, but return the one corresponding to
265 * `hostname' if we fail.
267 * Only include loopback address if there are no other.
270 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
271 krb5_get_all_client_addrs (krb5_context context, krb5_addresses *res)
273 int flags = LOOP | LOOP_IF_NONE | EXTRA_ADDRESSES;
275 if (context->scan_interfaces)
276 flags |= SCAN_INTERFACES;
278 return get_addrs_int (context, res, flags);
282 * Try to get all local addresses that a server should listen to.
283 * If that fails, we return the address corresponding to `hostname'.
286 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
287 krb5_get_all_server_addrs (krb5_context context, krb5_addresses *res)
289 return get_addrs_int (context, res, LOOP | SCAN_INTERFACES);