No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / get_addrs.c
blob1294f045a11ce0dd357f12d93274296aeee43b20
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 __RCSID("$Heimdal: get_addrs.c 13863 2004-05-25 21:46:46Z lha $"
37 "$NetBSD$");
39 #ifdef __osf__
40 /* hate */
41 struct rtentry;
42 struct mbuf;
43 #endif
44 #ifdef HAVE_NET_IF_H
45 #include <net/if.h>
46 #endif
47 #include <ifaddrs.h>
49 static krb5_error_code
50 gethostname_fallback (krb5_context context, krb5_addresses *res)
52 krb5_error_code ret;
53 char hostname[MAXHOSTNAMELEN];
54 struct hostent *hostent;
56 if (gethostname (hostname, sizeof(hostname))) {
57 ret = errno;
58 krb5_set_error_string (context, "gethostname: %s", strerror(ret));
59 return ret;
61 hostent = roken_gethostbyname (hostname);
62 if (hostent == NULL) {
63 ret = errno;
64 krb5_set_error_string (context, "gethostbyname %s: %s",
65 hostname, strerror(ret));
66 return ret;
68 res->len = 1;
69 res->val = malloc (sizeof(*res->val));
70 if (res->val == NULL) {
71 krb5_set_error_string(context, "malloc: out of memory");
72 return ENOMEM;
74 res->val[0].addr_type = hostent->h_addrtype;
75 res->val[0].address.data = NULL;
76 res->val[0].address.length = 0;
77 ret = krb5_data_copy (&res->val[0].address,
78 hostent->h_addr,
79 hostent->h_length);
80 if (ret) {
81 free (res->val);
82 return ret;
84 return 0;
87 enum {
88 LOOP = 1, /* do include loopback interfaces */
89 LOOP_IF_NONE = 2, /* include loopback if no other if's */
90 EXTRA_ADDRESSES = 4, /* include extra addresses */
91 SCAN_INTERFACES = 8 /* scan interfaces for addresses */
95 * Try to figure out the addresses of all configured interfaces with a
96 * lot of magic ioctls.
99 static krb5_error_code
100 find_all_addresses (krb5_context context, krb5_addresses *res, int flags)
102 struct sockaddr sa_zero;
103 struct ifaddrs *ifa0, *ifa;
104 krb5_error_code ret = ENXIO;
105 int num, idx;
106 krb5_addresses ignore_addresses;
108 res->val = NULL;
110 if (getifaddrs(&ifa0) == -1) {
111 ret = errno;
112 krb5_set_error_string(context, "getifaddrs: %s", strerror(ret));
113 return (ret);
116 memset(&sa_zero, 0, sizeof(sa_zero));
118 /* First, count all the ifaddrs. */
119 for (ifa = ifa0, num = 0; ifa != NULL; ifa = ifa->ifa_next, num++)
120 /* nothing */;
122 if (num == 0) {
123 freeifaddrs(ifa0);
124 krb5_set_error_string(context, "no addresses found");
125 return (ENXIO);
128 if (flags & EXTRA_ADDRESSES) {
129 /* we'll remove the addresses we don't care about */
130 ret = krb5_get_ignore_addresses(context, &ignore_addresses);
131 if(ret)
132 return ret;
135 /* Allocate storage for them. */
136 res->val = calloc(num, sizeof(*res->val));
137 if (res->val == NULL) {
138 krb5_free_addresses(context, &ignore_addresses);
139 freeifaddrs(ifa0);
140 krb5_set_error_string (context, "malloc: out of memory");
141 return (ENOMEM);
144 /* Now traverse the list. */
145 for (ifa = ifa0, idx = 0; ifa != NULL; ifa = ifa->ifa_next) {
146 if ((ifa->ifa_flags & IFF_UP) == 0)
147 continue;
148 if (ifa->ifa_addr == NULL)
149 continue;
150 if (memcmp(ifa->ifa_addr, &sa_zero, sizeof(sa_zero)) == 0)
151 continue;
152 if (krb5_sockaddr_uninteresting(ifa->ifa_addr))
153 continue;
154 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) {
155 /* We'll deal with the LOOP_IF_NONE case later. */
156 if ((flags & LOOP) == 0)
157 continue;
160 ret = krb5_sockaddr2address(context, ifa->ifa_addr, &res->val[idx]);
161 if (ret) {
163 * The most likely error here is going to be "Program
164 * lacks support for address type". This is no big
165 * deal -- just continue, and we'll listen on the
166 * addresses who's type we *do* support.
168 continue;
170 /* possibly skip this address? */
171 if((flags & EXTRA_ADDRESSES) &&
172 krb5_address_search(context, &res->val[idx], &ignore_addresses)) {
173 krb5_free_address(context, &res->val[idx]);
174 flags &= ~LOOP_IF_NONE; /* we actually found an address,
175 so don't add any loop-back
176 addresses */
177 continue;
180 idx++;
184 * If no addresses were found, and LOOP_IF_NONE is set, then find
185 * the loopback addresses and add them to our list.
187 if ((flags & LOOP_IF_NONE) != 0 && idx == 0) {
188 for (ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next) {
189 if ((ifa->ifa_flags & IFF_UP) == 0)
190 continue;
191 if (ifa->ifa_addr == NULL)
192 continue;
193 if (memcmp(ifa->ifa_addr, &sa_zero, sizeof(sa_zero)) == 0)
194 continue;
195 if (krb5_sockaddr_uninteresting(ifa->ifa_addr))
196 continue;
198 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) {
199 ret = krb5_sockaddr2address(context,
200 ifa->ifa_addr, &res->val[idx]);
201 if (ret) {
203 * See comment above.
205 continue;
207 if((flags & EXTRA_ADDRESSES) &&
208 krb5_address_search(context, &res->val[idx],
209 &ignore_addresses)) {
210 krb5_free_address(context, &res->val[idx]);
211 continue;
213 idx++;
218 if (flags & EXTRA_ADDRESSES)
219 krb5_free_addresses(context, &ignore_addresses);
220 freeifaddrs(ifa0);
221 if (ret)
222 free(res->val);
223 else
224 res->len = idx; /* Now a count. */
225 return (ret);
228 static krb5_error_code
229 get_addrs_int (krb5_context context, krb5_addresses *res, int flags)
231 krb5_error_code ret = -1;
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 res->len = 0;
239 res->val = NULL;
240 ret = 0;
243 if(ret == 0 && (flags & EXTRA_ADDRESSES)) {
244 krb5_addresses a;
245 /* append user specified addresses */
246 ret = krb5_get_extra_addresses(context, &a);
247 if(ret) {
248 krb5_free_addresses(context, res);
249 return ret;
251 ret = krb5_append_addresses(context, res, &a);
252 if(ret) {
253 krb5_free_addresses(context, res);
254 return ret;
256 krb5_free_addresses(context, &a);
258 if(res->len == 0) {
259 free(res->val);
260 res->val = NULL;
262 return ret;
266 * Try to get all addresses, but return the one corresponding to
267 * `hostname' if we fail.
269 * Only include loopback address if there are no other.
272 krb5_error_code KRB5_LIB_FUNCTION
273 krb5_get_all_client_addrs (krb5_context context, krb5_addresses *res)
275 int flags = LOOP_IF_NONE | EXTRA_ADDRESSES;
277 if (context->scan_interfaces)
278 flags |= SCAN_INTERFACES;
280 return get_addrs_int (context, res, flags);
284 * Try to get all local addresses that a server should listen to.
285 * If that fails, we return the address corresponding to `hostname'.
288 krb5_error_code KRB5_LIB_FUNCTION
289 krb5_get_all_server_addrs (krb5_context context, krb5_addresses *res)
291 return get_addrs_int (context, res, LOOP | SCAN_INTERFACES);