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