Merge commit 'upstream/1.8.1p2'
[debian/sudo] / src / net_ifs.c
1 /*
2  * Copyright (c) 1996, 1998-2005, 2007-2010
3  *      Todd C. Miller <Todd.Miller@courtesan.com>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * Sponsored in part by the Defense Advanced Research Projects
18  * Agency (DARPA) and Air Force Research Laboratory, Air Force
19  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
20  */
21
22 /*
23  * Suppress a warning w/ gcc on Digital UN*X.
24  * The system headers should really do this....
25  */
26 #if defined(__osf__) && !defined(__cplusplus)
27 struct mbuf;
28 struct rtentry;
29 #endif
30
31 #include <config.h>
32
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/ioctl.h>
38 #if defined(HAVE_SYS_SOCKIO_H) && !defined(SIOCGIFCONF)
39 # include <sys/sockio.h>
40 #endif
41 #include <stdio.h>
42 #ifdef STDC_HEADERS
43 # include <stdlib.h>
44 # include <stddef.h>
45 #else
46 # ifdef HAVE_STDLIB_H
47 #  include <stdlib.h>
48 # endif
49 #endif /* STDC_HEADERS */
50 #ifdef HAVE_STRING_H
51 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
52 #  include <memory.h>
53 # endif
54 # include <string.h>
55 #endif /* HAVE_STRING_H */
56 #ifdef HAVE_STRINGS_H
57 # include <strings.h>
58 #endif /* HAVE_STRINGS_H */
59 #ifdef HAVE_UNISTD_H
60 # include <unistd.h>
61 #endif /* HAVE_UNISTD_H */
62 #include <netdb.h>
63 #include <errno.h>
64 #ifdef _ISC
65 # include <sys/stream.h>
66 # include <sys/sioctl.h>
67 # include <sys/stropts.h>
68 # define STRSET(cmd, param, len) {strioctl.ic_cmd=(cmd);\
69                                  strioctl.ic_dp=(param);\
70                                  strioctl.ic_timout=0;\
71                                  strioctl.ic_len=(len);}
72 #endif /* _ISC */
73 #ifdef _MIPS
74 # include <net/soioctl.h>
75 #endif /* _MIPS */
76 #include <netinet/in.h>
77 #include <arpa/inet.h>
78 #include <net/if.h>
79 #ifdef HAVE_GETIFADDRS
80 # include <ifaddrs.h>
81 #endif
82
83 #include "missing.h"
84 #include "alloc.h"
85 #include "error.h"
86
87 /* Minix apparently lacks IFF_LOOPBACK */
88 #ifndef IFF_LOOPBACK
89 # define IFF_LOOPBACK   0
90 #endif
91
92 #ifndef INET6_ADDRSTRLEN
93 # define INET6_ADDRSTRLEN 46
94 #endif
95
96 #ifdef HAVE_GETIFADDRS
97
98 /*
99  * Fill in the interfaces string with the machine's ip addresses and netmasks
100  * and return the number of interfaces found.
101  */
102 int
103 get_net_ifs(char **addrinfo)
104 {
105     struct ifaddrs *ifa, *ifaddrs;
106     struct sockaddr_in *sin;
107 #ifdef HAVE_IN6_ADDR
108     struct sockaddr_in6 *sin6;
109     char addrbuf[INET6_ADDRSTRLEN];
110 #endif
111     int ailen, i, len, num_interfaces = 0;
112     char *cp;
113
114     if (getifaddrs(&ifaddrs))
115         return 0;
116
117     /* Allocate space for the interfaces info string. */
118     for (ifa = ifaddrs; ifa != NULL; ifa = ifa -> ifa_next) {
119         /* Skip interfaces marked "down" and "loopback". */
120         if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL ||
121             !ISSET(ifa->ifa_flags, IFF_UP) || ISSET(ifa->ifa_flags, IFF_LOOPBACK))
122             continue;
123
124         switch (ifa->ifa_addr->sa_family) {
125             case AF_INET:
126 #ifdef HAVE_IN6_ADDR
127             case AF_INET6:
128 #endif
129                 num_interfaces++;
130                 break;
131         }
132     }
133     if (num_interfaces == 0)
134         return 0;
135     ailen = num_interfaces * 2 * INET6_ADDRSTRLEN;
136     *addrinfo = cp = emalloc(ailen);
137
138     /* Store the IP addr/netmask pairs. */
139     for (ifa = ifaddrs, i = 0; ifa != NULL; ifa = ifa -> ifa_next) {
140         /* Skip interfaces marked "down" and "loopback". */
141         if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL ||
142             !ISSET(ifa->ifa_flags, IFF_UP) || ISSET(ifa->ifa_flags, IFF_LOOPBACK))
143                 continue;
144
145         switch (ifa->ifa_addr->sa_family) {
146             case AF_INET:
147                 sin = (struct sockaddr_in *)ifa->ifa_addr;
148                 len = snprintf(cp, ailen - (*addrinfo - cp),
149                     "%s%s/", cp == *addrinfo ? "" : " ",
150                     inet_ntoa(sin->sin_addr));
151                 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
152                     warningx("load_interfaces: overflow detected");
153                     goto done;
154                 }
155                 cp += len;
156
157                 sin = (struct sockaddr_in *)ifa->ifa_netmask;
158                 len = snprintf(cp, ailen - (*addrinfo - cp),
159                     "%s", inet_ntoa(sin->sin_addr));
160                 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
161                     warningx("load_interfaces: overflow detected");
162                     goto done;
163                 }
164                 cp += len;
165                 break;
166 #ifdef HAVE_IN6_ADDR
167             case AF_INET6:
168                 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
169                 inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
170                 len = snprintf(cp, ailen - (*addrinfo - cp),
171                     "%s%s/", cp == *addrinfo ? "" : " ", addrbuf);
172                 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
173                     warningx("load_interfaces: overflow detected");
174                     goto done;
175                 }
176                 cp += len;
177
178                 sin6 = (struct sockaddr_in6 *)ifa->ifa_netmask;
179                 inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
180                 len = snprintf(cp, ailen - (*addrinfo - cp), "%s", addrbuf);
181                 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
182                     warningx("load_interfaces: overflow detected");
183                     goto done;
184                 }
185                 cp += len;
186                 break;
187 #endif /* HAVE_IN6_ADDR */
188         }
189     }
190
191 done:
192 #ifdef HAVE_FREEIFADDRS
193     freeifaddrs(ifaddrs);
194 #else
195     efree(ifaddrs);
196 #endif
197     return num_interfaces;
198 }
199
200 #elif defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
201
202 /*
203  * Allocate and fill in the interfaces global variable with the
204  * machine's ip addresses and netmasks.
205  */
206 int
207 get_net_ifs(char **addrinfo)
208 {
209     struct ifconf *ifconf;
210     struct ifreq *ifr, ifr_tmp;
211     struct sockaddr_in *sin;
212     int ailen, i, len, n, sock, num_interfaces = 0;
213     size_t buflen = sizeof(struct ifconf) + BUFSIZ;
214     char *cp, *previfname = "", *ifconf_buf = NULL;
215 #ifdef _ISC
216     struct strioctl strioctl;
217 #endif /* _ISC */
218
219     sock = socket(AF_INET, SOCK_DGRAM, 0);
220     if (sock < 0)
221         error(1, "cannot open socket");
222
223     /*
224      * Get interface configuration or return.
225      */
226     for (;;) {
227         ifconf_buf = emalloc(buflen);
228         ifconf = (struct ifconf *) ifconf_buf;
229         ifconf->ifc_len = buflen - sizeof(struct ifconf);
230         ifconf->ifc_buf = (caddr_t) (ifconf_buf + sizeof(struct ifconf));
231
232 #ifdef _ISC
233         STRSET(SIOCGIFCONF, (caddr_t) ifconf, buflen);
234         if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0)
235 #else
236         /* Note that some kernels return EINVAL if the buffer is too small */
237         if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0 && errno != EINVAL)
238 #endif /* _ISC */
239             goto done;
240
241         /* Break out of loop if we have a big enough buffer. */
242         if (ifconf->ifc_len + sizeof(struct ifreq) < buflen)
243             break;
244         buflen += BUFSIZ;
245         efree(ifconf_buf);
246     }
247
248     /* Allocate space for the maximum number of interfaces that could exist. */
249     if ((n = ifconf->ifc_len / sizeof(struct ifreq)) == 0)
250         return 0;
251     ailen = n * 2 * INET6_ADDRSTRLEN;
252     *addrinfo = cp = emalloc(ailen);
253
254     /* For each interface, store the ip address and netmask. */
255     for (i = 0; i < ifconf->ifc_len; ) {
256         /* Get a pointer to the current interface. */
257         ifr = (struct ifreq *) &ifconf->ifc_buf[i];
258
259         /* Set i to the subscript of the next interface. */
260         i += sizeof(struct ifreq);
261 #ifdef HAVE_SA_LEN
262         if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
263             i += ifr->ifr_addr.sa_len - sizeof(struct sockaddr);
264 #endif /* HAVE_SA_LEN */
265
266         /* Skip duplicates and interfaces with NULL addresses. */
267         sin = (struct sockaddr_in *) &ifr->ifr_addr;
268         if (sin->sin_addr.s_addr == 0 ||
269             strncmp(previfname, ifr->ifr_name, sizeof(ifr->ifr_name) - 1) == 0)
270             continue;
271
272         if (ifr->ifr_addr.sa_family != AF_INET)
273                 continue;
274
275 #ifdef SIOCGIFFLAGS
276         memset(&ifr_tmp, 0, sizeof(ifr_tmp));
277         strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
278         if (ioctl(sock, SIOCGIFFLAGS, (caddr_t) &ifr_tmp) < 0)
279 #endif
280             ifr_tmp = *ifr;
281         
282         /* Skip interfaces marked "down" and "loopback". */
283         if (!ISSET(ifr_tmp.ifr_flags, IFF_UP) ||
284             ISSET(ifr_tmp.ifr_flags, IFF_LOOPBACK))
285                 continue;
286
287         sin = (struct sockaddr_in *) &ifr->ifr_addr;
288         len = snprintf(cp, ailen - (*addrinfo - cp),
289             "%s%s/", cp == *addrinfo ? "" : " ",
290             inet_ntoa(sin->sin_addr));
291         if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
292             warningx("load_interfaces: overflow detected");
293             goto done;
294         }
295         cp += len;
296
297         /* Stash the name of the interface we saved. */
298         previfname = ifr->ifr_name;
299
300         /* Get the netmask. */
301         memset(&ifr_tmp, 0, sizeof(ifr_tmp));
302         strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
303 #ifdef _ISC
304         STRSET(SIOCGIFNETMASK, (caddr_t) &ifr_tmp, sizeof(ifr_tmp));
305         if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
306 #else
307         if (ioctl(sock, SIOCGIFNETMASK, (caddr_t) &ifr_tmp) < 0) {
308 #endif /* _ISC */
309             sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
310             sin->sin_addr.s_addr = htonl(IN_CLASSC_NET);
311         }
312         sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
313         len = snprintf(cp, ailen - (*addrinfo - cp),
314             "%s", inet_ntoa(sin->sin_addr));
315         if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
316             warningx("load_interfaces: overflow detected");
317             goto done;
318         }
319         cp += len;
320         num_interfaces++;
321     }
322
323 done:
324     efree(ifconf_buf);
325     (void) close(sock);
326
327     return num_interfaces;
328 }
329
330 #else /* !SIOCGIFCONF || STUB_LOAD_INTERFACES */
331
332 /*
333  * Stub function for those without SIOCGIFCONF or getifaddrs()
334  */
335 int
336 get_net_ifs(char **addrinfo)
337 {
338     return 0;
339 }
340
341 #endif /* SIOCGIFCONF && !STUB_LOAD_INTERFACES */