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