1*d3273b5bSchristos /* $NetBSD: get_addrs.c,v 1.2 2017/01/28 21:31:49 christos Exp $ */
2ca1c9b0cSelric
3ca1c9b0cSelric /*
4ca1c9b0cSelric * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
5ca1c9b0cSelric * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric * All rights reserved.
7ca1c9b0cSelric *
8ca1c9b0cSelric * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric * modification, are permitted provided that the following conditions
10ca1c9b0cSelric * are met:
11ca1c9b0cSelric *
12ca1c9b0cSelric * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric * notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric *
15ca1c9b0cSelric * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric * notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric * documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric *
19ca1c9b0cSelric * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric * may be used to endorse or promote products derived from this software
21ca1c9b0cSelric * without specific prior written permission.
22ca1c9b0cSelric *
23ca1c9b0cSelric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric * SUCH DAMAGE.
34ca1c9b0cSelric */
35ca1c9b0cSelric
36ca1c9b0cSelric #include "krb5_locl.h"
37ca1c9b0cSelric
38ca1c9b0cSelric #ifdef __osf__
39ca1c9b0cSelric /* hate */
40ca1c9b0cSelric struct rtentry;
41ca1c9b0cSelric struct mbuf;
42ca1c9b0cSelric #endif
43ca1c9b0cSelric #ifdef HAVE_NET_IF_H
44ca1c9b0cSelric #include <net/if.h>
45ca1c9b0cSelric #endif
46ca1c9b0cSelric #include <ifaddrs.h>
47ca1c9b0cSelric
48ca1c9b0cSelric static krb5_error_code
gethostname_fallback(krb5_context context,krb5_addresses * res)49ca1c9b0cSelric gethostname_fallback (krb5_context context, krb5_addresses *res)
50ca1c9b0cSelric {
51ca1c9b0cSelric krb5_error_code ret;
52ca1c9b0cSelric char hostname[MAXHOSTNAMELEN];
53ca1c9b0cSelric struct hostent *hostent;
54ca1c9b0cSelric
55ca1c9b0cSelric if (gethostname (hostname, sizeof(hostname))) {
56ca1c9b0cSelric ret = errno;
57ca1c9b0cSelric krb5_set_error_message(context, ret, "gethostname: %s", strerror(ret));
58ca1c9b0cSelric return ret;
59ca1c9b0cSelric }
60ca1c9b0cSelric hostent = roken_gethostbyname (hostname);
61ca1c9b0cSelric if (hostent == NULL) {
62ca1c9b0cSelric ret = errno;
63ca1c9b0cSelric krb5_set_error_message (context, ret, "gethostbyname %s: %s",
64ca1c9b0cSelric hostname, strerror(ret));
65ca1c9b0cSelric return ret;
66ca1c9b0cSelric }
67ca1c9b0cSelric res->len = 1;
68ca1c9b0cSelric res->val = malloc (sizeof(*res->val));
69b9d004c6Schristos if (res->val == NULL)
70b9d004c6Schristos return krb5_enomem(context);
71ca1c9b0cSelric res->val[0].addr_type = hostent->h_addrtype;
72ca1c9b0cSelric res->val[0].address.data = NULL;
73ca1c9b0cSelric res->val[0].address.length = 0;
74ca1c9b0cSelric ret = krb5_data_copy (&res->val[0].address,
75ca1c9b0cSelric hostent->h_addr,
76ca1c9b0cSelric hostent->h_length);
77ca1c9b0cSelric if (ret) {
78ca1c9b0cSelric free (res->val);
79ca1c9b0cSelric return ret;
80ca1c9b0cSelric }
81ca1c9b0cSelric return 0;
82ca1c9b0cSelric }
83ca1c9b0cSelric
84ca1c9b0cSelric enum {
85ca1c9b0cSelric LOOP = 1, /* do include loopback addrs */
86ca1c9b0cSelric LOOP_IF_NONE = 2, /* include loopback addrs if no others */
87ca1c9b0cSelric EXTRA_ADDRESSES = 4, /* include extra addresses */
88ca1c9b0cSelric SCAN_INTERFACES = 8 /* scan interfaces for addresses */
89ca1c9b0cSelric };
90ca1c9b0cSelric
91ca1c9b0cSelric /*
92ca1c9b0cSelric * Try to figure out the addresses of all configured interfaces with a
93ca1c9b0cSelric * lot of magic ioctls.
94ca1c9b0cSelric */
95ca1c9b0cSelric
96ca1c9b0cSelric static krb5_error_code
find_all_addresses(krb5_context context,krb5_addresses * res,int flags)97ca1c9b0cSelric find_all_addresses (krb5_context context, krb5_addresses *res, int flags)
98ca1c9b0cSelric {
99ca1c9b0cSelric struct sockaddr sa_zero;
100ca1c9b0cSelric struct ifaddrs *ifa0, *ifa;
101ca1c9b0cSelric krb5_error_code ret = ENXIO;
102ca1c9b0cSelric unsigned int num, idx;
103ca1c9b0cSelric krb5_addresses ignore_addresses;
104ca1c9b0cSelric
105ca1c9b0cSelric if (getifaddrs(&ifa0) == -1) {
106ca1c9b0cSelric ret = errno;
107ca1c9b0cSelric krb5_set_error_message(context, ret, "getifaddrs: %s", strerror(ret));
108ca1c9b0cSelric return (ret);
109ca1c9b0cSelric }
110ca1c9b0cSelric
111ca1c9b0cSelric memset(&sa_zero, 0, sizeof(sa_zero));
112ca1c9b0cSelric
113ca1c9b0cSelric /* First, count all the ifaddrs. */
114ca1c9b0cSelric for (ifa = ifa0, num = 0; ifa != NULL; ifa = ifa->ifa_next, num++)
115ca1c9b0cSelric /* nothing */;
116ca1c9b0cSelric
117ca1c9b0cSelric if (num == 0) {
118ca1c9b0cSelric freeifaddrs(ifa0);
119ca1c9b0cSelric krb5_set_error_message(context, ENXIO, N_("no addresses found", ""));
120ca1c9b0cSelric return (ENXIO);
121ca1c9b0cSelric }
122ca1c9b0cSelric
123ca1c9b0cSelric if (flags & EXTRA_ADDRESSES) {
124ca1c9b0cSelric /* we'll remove the addresses we don't care about */
125ca1c9b0cSelric ret = krb5_get_ignore_addresses(context, &ignore_addresses);
126ca1c9b0cSelric if(ret)
127ca1c9b0cSelric return ret;
128ca1c9b0cSelric }
129ca1c9b0cSelric
130ca1c9b0cSelric /* Allocate storage for them. */
131ca1c9b0cSelric res->val = calloc(num, sizeof(*res->val));
132ca1c9b0cSelric if (res->val == NULL) {
133b9d004c6Schristos if (flags & EXTRA_ADDRESSES)
134ca1c9b0cSelric krb5_free_addresses(context, &ignore_addresses);
135ca1c9b0cSelric freeifaddrs(ifa0);
136b9d004c6Schristos return krb5_enomem(context);
137ca1c9b0cSelric }
138ca1c9b0cSelric
139ca1c9b0cSelric /* Now traverse the list. */
140ca1c9b0cSelric for (ifa = ifa0, idx = 0; ifa != NULL; ifa = ifa->ifa_next) {
141ca1c9b0cSelric if ((ifa->ifa_flags & IFF_UP) == 0)
142ca1c9b0cSelric continue;
143ca1c9b0cSelric if (ifa->ifa_addr == NULL)
144ca1c9b0cSelric continue;
145ca1c9b0cSelric if (memcmp(ifa->ifa_addr, &sa_zero, sizeof(sa_zero)) == 0)
146ca1c9b0cSelric continue;
147ca1c9b0cSelric if (krb5_sockaddr_uninteresting(ifa->ifa_addr))
148ca1c9b0cSelric continue;
149ca1c9b0cSelric if (krb5_sockaddr_is_loopback(ifa->ifa_addr) && (flags & LOOP) == 0)
150ca1c9b0cSelric /* We'll deal with the LOOP_IF_NONE case later. */
151ca1c9b0cSelric continue;
152ca1c9b0cSelric
153ca1c9b0cSelric ret = krb5_sockaddr2address(context, ifa->ifa_addr, &res->val[idx]);
154ca1c9b0cSelric if (ret) {
155ca1c9b0cSelric /*
156ca1c9b0cSelric * The most likely error here is going to be "Program
157ca1c9b0cSelric * lacks support for address type". This is no big
158ca1c9b0cSelric * deal -- just continue, and we'll listen on the
159ca1c9b0cSelric * addresses who's type we *do* support.
160ca1c9b0cSelric */
161ca1c9b0cSelric continue;
162ca1c9b0cSelric }
163ca1c9b0cSelric /* possibly skip this address? */
164ca1c9b0cSelric if((flags & EXTRA_ADDRESSES) &&
165ca1c9b0cSelric krb5_address_search(context, &res->val[idx], &ignore_addresses)) {
166ca1c9b0cSelric krb5_free_address(context, &res->val[idx]);
167ca1c9b0cSelric flags &= ~LOOP_IF_NONE; /* we actually found an address,
168ca1c9b0cSelric so don't add any loop-back
169ca1c9b0cSelric addresses */
170ca1c9b0cSelric continue;
171ca1c9b0cSelric }
172ca1c9b0cSelric
173ca1c9b0cSelric idx++;
174ca1c9b0cSelric }
175ca1c9b0cSelric
176ca1c9b0cSelric /*
177ca1c9b0cSelric * If no addresses were found, and LOOP_IF_NONE is set, then find
178ca1c9b0cSelric * the loopback addresses and add them to our list.
179ca1c9b0cSelric */
180ca1c9b0cSelric if ((flags & LOOP_IF_NONE) != 0 && idx == 0) {
181ca1c9b0cSelric for (ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next) {
182ca1c9b0cSelric if ((ifa->ifa_flags & IFF_UP) == 0)
183ca1c9b0cSelric continue;
184ca1c9b0cSelric if (ifa->ifa_addr == NULL)
185ca1c9b0cSelric continue;
186ca1c9b0cSelric if (memcmp(ifa->ifa_addr, &sa_zero, sizeof(sa_zero)) == 0)
187ca1c9b0cSelric continue;
188ca1c9b0cSelric if (krb5_sockaddr_uninteresting(ifa->ifa_addr))
189ca1c9b0cSelric continue;
190ca1c9b0cSelric if (!krb5_sockaddr_is_loopback(ifa->ifa_addr))
191ca1c9b0cSelric continue;
192ca1c9b0cSelric if ((ifa->ifa_flags & IFF_LOOPBACK) == 0)
193ca1c9b0cSelric /* Presumably loopback addrs are only used on loopback ifs! */
194ca1c9b0cSelric continue;
195ca1c9b0cSelric ret = krb5_sockaddr2address(context,
196ca1c9b0cSelric ifa->ifa_addr, &res->val[idx]);
197ca1c9b0cSelric if (ret)
198ca1c9b0cSelric continue; /* We don't consider this failure fatal */
199ca1c9b0cSelric if((flags & EXTRA_ADDRESSES) &&
200ca1c9b0cSelric krb5_address_search(context, &res->val[idx],
201ca1c9b0cSelric &ignore_addresses)) {
202ca1c9b0cSelric krb5_free_address(context, &res->val[idx]);
203ca1c9b0cSelric continue;
204ca1c9b0cSelric }
205ca1c9b0cSelric idx++;
206ca1c9b0cSelric }
207ca1c9b0cSelric }
208ca1c9b0cSelric
209ca1c9b0cSelric if (flags & EXTRA_ADDRESSES)
210ca1c9b0cSelric krb5_free_addresses(context, &ignore_addresses);
211ca1c9b0cSelric freeifaddrs(ifa0);
212ca1c9b0cSelric if (ret) {
213ca1c9b0cSelric free(res->val);
214ca1c9b0cSelric res->val = NULL;
215ca1c9b0cSelric } else
216ca1c9b0cSelric res->len = idx; /* Now a count. */
217ca1c9b0cSelric return (ret);
218ca1c9b0cSelric }
219ca1c9b0cSelric
220ca1c9b0cSelric static krb5_error_code
get_addrs_int(krb5_context context,krb5_addresses * res,int flags)221ca1c9b0cSelric get_addrs_int (krb5_context context, krb5_addresses *res, int flags)
222ca1c9b0cSelric {
223ca1c9b0cSelric krb5_error_code ret = -1;
224ca1c9b0cSelric
225ca1c9b0cSelric res->len = 0;
226ca1c9b0cSelric res->val = NULL;
227ca1c9b0cSelric
228ca1c9b0cSelric if (flags & SCAN_INTERFACES) {
229ca1c9b0cSelric ret = find_all_addresses (context, res, flags);
230ca1c9b0cSelric if(ret || res->len == 0)
231ca1c9b0cSelric ret = gethostname_fallback (context, res);
232ca1c9b0cSelric } else {
233ca1c9b0cSelric ret = 0;
234ca1c9b0cSelric }
235ca1c9b0cSelric
236ca1c9b0cSelric if(ret == 0 && (flags & EXTRA_ADDRESSES)) {
237ca1c9b0cSelric krb5_addresses a;
238ca1c9b0cSelric /* append user specified addresses */
239ca1c9b0cSelric ret = krb5_get_extra_addresses(context, &a);
240ca1c9b0cSelric if(ret) {
241ca1c9b0cSelric krb5_free_addresses(context, res);
242ca1c9b0cSelric return ret;
243ca1c9b0cSelric }
244ca1c9b0cSelric ret = krb5_append_addresses(context, res, &a);
245ca1c9b0cSelric if(ret) {
246ca1c9b0cSelric krb5_free_addresses(context, res);
247ca1c9b0cSelric return ret;
248ca1c9b0cSelric }
249ca1c9b0cSelric krb5_free_addresses(context, &a);
250ca1c9b0cSelric }
251ca1c9b0cSelric if(res->len == 0) {
252ca1c9b0cSelric free(res->val);
253ca1c9b0cSelric res->val = NULL;
254ca1c9b0cSelric }
255ca1c9b0cSelric return ret;
256ca1c9b0cSelric }
257ca1c9b0cSelric
258ca1c9b0cSelric /*
259ca1c9b0cSelric * Try to get all addresses, but return the one corresponding to
260ca1c9b0cSelric * `hostname' if we fail.
261ca1c9b0cSelric *
262ca1c9b0cSelric * Only include loopback address if there are no other.
263ca1c9b0cSelric */
264ca1c9b0cSelric
265ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_get_all_client_addrs(krb5_context context,krb5_addresses * res)266ca1c9b0cSelric krb5_get_all_client_addrs (krb5_context context, krb5_addresses *res)
267ca1c9b0cSelric {
268ca1c9b0cSelric int flags = LOOP_IF_NONE | EXTRA_ADDRESSES;
269ca1c9b0cSelric
270ca1c9b0cSelric if (context->scan_interfaces)
271ca1c9b0cSelric flags |= SCAN_INTERFACES;
272ca1c9b0cSelric
273ca1c9b0cSelric return get_addrs_int (context, res, flags);
274ca1c9b0cSelric }
275ca1c9b0cSelric
276ca1c9b0cSelric /*
277ca1c9b0cSelric * Try to get all local addresses that a server should listen to.
278ca1c9b0cSelric * If that fails, we return the address corresponding to `hostname'.
279ca1c9b0cSelric */
280ca1c9b0cSelric
281ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_get_all_server_addrs(krb5_context context,krb5_addresses * res)282ca1c9b0cSelric krb5_get_all_server_addrs (krb5_context context, krb5_addresses *res)
283ca1c9b0cSelric {
284ca1c9b0cSelric return get_addrs_int (context, res, LOOP | SCAN_INTERFACES);
285ca1c9b0cSelric }
286