Merge commit 'upstream/1.7.2p2'
[debian/sudo] / interfaces.c
1 /*
2  * Copyright (c) 1996, 1998-2005, 2007-2009
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 #else
56 # ifdef HAVE_STRINGS_H
57 #  include <strings.h>
58 # endif
59 #endif /* HAVE_STRING_H */
60 #ifdef HAVE_UNISTD_H
61 # include <unistd.h>
62 #endif /* HAVE_UNISTD_H */
63 #include <netdb.h>
64 #include <errno.h>
65 #ifdef _ISC
66 # include <sys/stream.h>
67 # include <sys/sioctl.h>
68 # include <sys/stropts.h>
69 # define STRSET(cmd, param, len) {strioctl.ic_cmd=(cmd);\
70                                  strioctl.ic_dp=(param);\
71                                  strioctl.ic_timout=0;\
72                                  strioctl.ic_len=(len);}
73 #endif /* _ISC */
74 #ifdef _MIPS
75 # include <net/soioctl.h>
76 #endif /* _MIPS */
77 #include <netinet/in.h>
78 #include <arpa/inet.h>
79 #include <net/if.h>
80 #ifdef HAVE_GETIFADDRS
81 # include <ifaddrs.h>
82 #endif
83
84 #include "sudo.h"
85 #include "interfaces.h"
86
87 #ifndef lint
88 __unused static const char rcsid[] = "$Sudo: interfaces.c,v 1.87 2009/05/25 12:02:41 millert Exp $";
89 #endif /* lint */
90
91 /* Minix apparently lacks IFF_LOOPBACK */
92 #ifndef IFF_LOOPBACK
93 # define IFF_LOOPBACK   0
94 #endif
95
96 #ifdef HAVE_GETIFADDRS
97
98 /*
99  * Allocate and fill in the interfaces global variable with the
100  * machine's ip addresses and netmasks.
101  */
102 void
103 load_interfaces()
104 {
105     struct ifaddrs *ifa, *ifaddrs;
106     struct sockaddr_in *sin;
107 #ifdef HAVE_IN6_ADDR
108     struct sockaddr_in6 *sin6;
109 #endif
110     int i;
111
112     if (getifaddrs(&ifaddrs))
113         return;
114
115     /* Allocate space for the interfaces list. */
116     for (ifa = ifaddrs; ifa != NULL; ifa = ifa -> ifa_next) {
117         /* Skip interfaces marked "down" and "loopback". */
118         if (ifa->ifa_addr == NULL || !ISSET(ifa->ifa_flags, IFF_UP) ||
119             ISSET(ifa->ifa_flags, IFF_LOOPBACK))
120             continue;
121
122         switch(ifa->ifa_addr->sa_family) {
123             case AF_INET:
124 #ifdef HAVE_IN6_ADDR
125             case AF_INET6:
126 #endif
127                 num_interfaces++;
128                 break;
129         }
130     }
131     if (num_interfaces == 0)
132         return;
133     interfaces =
134         (struct interface *) emalloc2(num_interfaces, sizeof(struct interface));
135
136     /* Store the ip addr / netmask pairs. */
137     for (ifa = ifaddrs, i = 0; ifa != NULL; ifa = ifa -> ifa_next) {
138         /* Skip interfaces marked "down" and "loopback". */
139         if (ifa->ifa_addr == NULL || !ISSET(ifa->ifa_flags, IFF_UP) ||
140             ISSET(ifa->ifa_flags, IFF_LOOPBACK))
141                 continue;
142
143         switch(ifa->ifa_addr->sa_family) {
144             case AF_INET:
145                 sin = (struct sockaddr_in *)ifa->ifa_addr;
146                 if (sin == NULL)
147                     continue;
148                 memcpy(&interfaces[i].addr, &sin->sin_addr,
149                     sizeof(struct in_addr));
150                 sin = (struct sockaddr_in *)ifa->ifa_netmask;
151                 if (sin == NULL)
152                     continue;
153                 memcpy(&interfaces[i].netmask, &sin->sin_addr,
154                     sizeof(struct in_addr));
155                 interfaces[i].family = AF_INET;
156                 i++;
157                 break;
158 #ifdef HAVE_IN6_ADDR
159             case AF_INET6:
160                 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
161                 if (sin6 == NULL)
162                     continue;
163                 memcpy(&interfaces[i].addr, &sin6->sin6_addr,
164                     sizeof(struct in6_addr));
165                 sin6 = (struct sockaddr_in6 *)ifa->ifa_netmask;
166                 if (sin6 == NULL)
167                     continue;
168                 memcpy(&interfaces[i].netmask, &sin6->sin6_addr,
169                     sizeof(struct in6_addr));
170                 interfaces[i].family = AF_INET6;
171                 i++;
172                 break;
173 #endif /* HAVE_IN6_ADDR */
174         }
175     }
176 #ifdef HAVE_FREEIFADDRS
177     freeifaddrs(ifaddrs);
178 #else
179     efree(ifaddrs);
180 #endif
181 }
182
183 #elif defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
184
185 /*
186  * Allocate and fill in the interfaces global variable with the
187  * machine's ip addresses and netmasks.
188  */
189 void
190 load_interfaces()
191 {
192     struct ifconf *ifconf;
193     struct ifreq *ifr, ifr_tmp;
194     struct sockaddr_in *sin;
195     int sock, n, i;
196     size_t len = sizeof(struct ifconf) + BUFSIZ;
197     char *previfname = "", *ifconf_buf = NULL;
198 #ifdef _ISC
199     struct strioctl strioctl;
200 #endif /* _ISC */
201
202     sock = socket(AF_INET, SOCK_DGRAM, 0);
203     if (sock < 0)
204         error(1, "cannot open socket");
205
206     /*
207      * Get interface configuration or return (leaving num_interfaces == 0)
208      */
209     for (;;) {
210         ifconf_buf = erealloc(ifconf_buf, len);
211         ifconf = (struct ifconf *) ifconf_buf;
212         ifconf->ifc_len = len - sizeof(struct ifconf);
213         ifconf->ifc_buf = (caddr_t) (ifconf_buf + sizeof(struct ifconf));
214
215 #ifdef _ISC
216         STRSET(SIOCGIFCONF, (caddr_t) ifconf, len);
217         if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
218 #else
219         /* Note that some kernels return EINVAL if the buffer is too small */
220         if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0 && errno != EINVAL) {
221 #endif /* _ISC */
222             efree(ifconf_buf);
223             (void) close(sock);
224             return;
225         }
226
227         /* Break out of loop if we have a big enough buffer. */
228         if (ifconf->ifc_len + sizeof(struct ifreq) < len)
229             break;
230         len += BUFSIZ;
231     }
232
233     /* Allocate space for the maximum number of interfaces that could exist. */
234     if ((n = ifconf->ifc_len / sizeof(struct ifreq)) == 0)
235         return;
236     interfaces = (struct interface *) emalloc2(n, sizeof(struct interface));
237
238     /* For each interface, store the ip address and netmask. */
239     for (i = 0; i < ifconf->ifc_len; ) {
240         /* Get a pointer to the current interface. */
241         ifr = (struct ifreq *) &ifconf->ifc_buf[i];
242
243         /* Set i to the subscript of the next interface. */
244         i += sizeof(struct ifreq);
245 #ifdef HAVE_SA_LEN
246         if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
247             i += ifr->ifr_addr.sa_len - sizeof(struct sockaddr);
248 #endif /* HAVE_SA_LEN */
249
250         /* Skip duplicates and interfaces with NULL addresses. */
251         sin = (struct sockaddr_in *) &ifr->ifr_addr;
252         if (sin->sin_addr.s_addr == 0 ||
253             strncmp(previfname, ifr->ifr_name, sizeof(ifr->ifr_name) - 1) == 0)
254             continue;
255
256         if (ifr->ifr_addr.sa_family != AF_INET)
257                 continue;
258
259 #ifdef SIOCGIFFLAGS
260         zero_bytes(&ifr_tmp, sizeof(ifr_tmp));
261         strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
262         if (ioctl(sock, SIOCGIFFLAGS, (caddr_t) &ifr_tmp) < 0)
263 #endif
264             ifr_tmp = *ifr;
265         
266         /* Skip interfaces marked "down" and "loopback". */
267         if (!ISSET(ifr_tmp.ifr_flags, IFF_UP) ||
268             ISSET(ifr_tmp.ifr_flags, IFF_LOOPBACK))
269                 continue;
270
271         sin = (struct sockaddr_in *) &ifr->ifr_addr;
272         interfaces[num_interfaces].addr.ip4.s_addr = sin->sin_addr.s_addr;
273
274         /* Stash the name of the interface we saved. */
275         previfname = ifr->ifr_name;
276
277         /* Get the netmask. */
278         zero_bytes(&ifr_tmp, sizeof(ifr_tmp));
279         strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
280 #ifdef SIOCGIFNETMASK
281 #ifdef _ISC
282         STRSET(SIOCGIFNETMASK, (caddr_t) &ifr_tmp, sizeof(ifr_tmp));
283         if (ioctl(sock, I_STR, (caddr_t) &strioctl) == 0) {
284 #else
285         if (ioctl(sock, SIOCGIFNETMASK, (caddr_t) &ifr_tmp) == 0) {
286 #endif /* _ISC */
287             sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
288
289             interfaces[num_interfaces].netmask.ip4.s_addr = sin->sin_addr.s_addr;
290         } else {
291 #else
292         {
293 #endif /* SIOCGIFNETMASK */
294             if (IN_CLASSC(interfaces[num_interfaces].addr.ip4.s_addr))
295                 interfaces[num_interfaces].netmask.ip4.s_addr = htonl(IN_CLASSC_NET);
296             else if (IN_CLASSB(interfaces[num_interfaces].addr.ip4.s_addr))
297                 interfaces[num_interfaces].netmask.ip4.s_addr = htonl(IN_CLASSB_NET);
298             else
299                 interfaces[num_interfaces].netmask.ip4.s_addr = htonl(IN_CLASSA_NET);
300         }
301
302         /* Only now can we be sure it was a good/interesting interface. */
303         interfaces[num_interfaces].family = AF_INET;
304         num_interfaces++;
305     }
306
307     /* If the expected size < real size, realloc the array. */
308     if (n != num_interfaces) {
309         if (num_interfaces != 0)
310             interfaces = (struct interface *) erealloc3(interfaces,
311                 num_interfaces, sizeof(struct interface));
312         else
313             efree(interfaces);
314     }
315     efree(ifconf_buf);
316     (void) close(sock);
317 }
318
319 #else /* !SIOCGIFCONF || STUB_LOAD_INTERFACES */
320
321 /*
322  * Stub function for those without SIOCGIFCONF
323  */
324 void
325 load_interfaces()
326 {
327     return;
328 }
329
330 #endif /* SIOCGIFCONF && !STUB_LOAD_INTERFACES */
331
332 void
333 dump_interfaces()
334 {
335     int i;
336 #ifdef HAVE_IN6_ADDR
337     char addrbuf[INET6_ADDRSTRLEN], maskbuf[INET6_ADDRSTRLEN];
338 #endif
339
340     puts("Local IP address and netmask pairs:");
341     for (i = 0; i < num_interfaces; i++) {
342         switch(interfaces[i].family) {
343             case AF_INET:
344                 printf("\t%s / ", inet_ntoa(interfaces[i].addr.ip4));
345                 puts(inet_ntoa(interfaces[i].netmask.ip4));
346                 break;
347 #ifdef HAVE_IN6_ADDR
348             case AF_INET6:
349                 inet_ntop(AF_INET6, &interfaces[i].addr.ip6,
350                     addrbuf, sizeof(addrbuf));
351                 inet_ntop(AF_INET6, &interfaces[i].netmask.ip6,
352                     maskbuf, sizeof(maskbuf));
353                 printf("\t%s / %s\n", addrbuf, maskbuf);
354                 break;
355 #endif /* HAVE_IN6_ADDR */
356         }
357     }
358 }