Annotation of src/usr.bin/sudo/interfaces.c, Revision 1.6
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.
1.6 ! millert 33: *
! 34: * Sponsored in part by the Defense Advanced Research Projects
! 35: * Agency (DARPA) and Air Force Research Laboratory, Air Force
! 36: * Materiel Command, USAF, under agreement number F39502-99-1-0512.
1.1 millert 37: */
38:
39: /*
40: * Supress a warning w/ gcc on Digital UN*X.
41: * The system headers should really do this....
42: */
43: #if defined(__osf__) && !defined(__cplusplus)
44: struct mbuf;
45: struct rtentry;
46: #endif
47:
48: #include "config.h"
49:
1.2 millert 50: #include <sys/types.h>
51: #include <sys/socket.h>
52: #include <sys/param.h>
53: #include <sys/time.h>
54: #include <sys/ioctl.h>
55: #if defined(HAVE_SYS_SOCKIO_H) && !defined(SIOCGIFCONF)
56: # include <sys/sockio.h>
57: #endif
1.1 millert 58: #include <stdio.h>
59: #ifdef STDC_HEADERS
1.2 millert 60: # include <stdlib.h>
61: # include <stddef.h>
62: #else
63: # ifdef HAVE_STDLIB_H
64: # include <stdlib.h>
65: # endif
1.1 millert 66: #endif /* STDC_HEADERS */
1.2 millert 67: #ifdef HAVE_STRING_H
68: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
69: # include <memory.h>
70: # endif
71: # include <string.h>
72: #else
73: # ifdef HAVE_STRINGS_H
74: # include <strings.h>
75: # endif
76: #endif /* HAVE_STRING_H */
1.1 millert 77: #ifdef HAVE_UNISTD_H
1.2 millert 78: # include <unistd.h>
1.1 millert 79: #endif /* HAVE_UNISTD_H */
1.5 millert 80: #ifdef HAVE_ERR_H
81: # include <err.h>
82: #else
83: # include "emul/err.h"
84: #endif /* HAVE_ERR_H */
1.1 millert 85: #include <netdb.h>
86: #ifdef _ISC
1.2 millert 87: # include <sys/stream.h>
88: # include <sys/sioctl.h>
89: # include <sys/stropts.h>
90: # define STRSET(cmd, param, len) {strioctl.ic_cmd=(cmd);\
1.1 millert 91: strioctl.ic_dp=(param);\
92: strioctl.ic_timout=0;\
93: strioctl.ic_len=(len);}
94: #endif /* _ISC */
95: #ifdef _MIPS
1.2 millert 96: # include <net/soioctl.h>
1.1 millert 97: #endif /* _MIPS */
98: #include <netinet/in.h>
99: #include <arpa/inet.h>
100: #include <net/if.h>
1.2 millert 101: #ifdef HAVE_GETIFADDRS
102: # include <ifaddrs.h>
103: #endif
1.1 millert 104:
105: #include "sudo.h"
106: #include "interfaces.h"
107:
108: #ifndef lint
1.6 ! millert 109: static const char rcsid[] = "$Sudo: interfaces.c,v 1.70 2003/04/16 00:42:10 millert Exp $";
1.1 millert 110: #endif /* lint */
111:
112:
1.2 millert 113: #ifdef HAVE_GETIFADDRS
114:
115: /*
116: * Allocate and fill in the interfaces global variable with the
117: * machine's ip addresses and netmasks.
118: */
119: void
120: load_interfaces()
121: {
122: struct ifaddrs *ifa, *ifaddrs;
123: /* XXX - sockaddr_in6 sin6; */
124: struct sockaddr_in *sin;
125: int i;
126:
127: if (getifaddrs(&ifaddrs))
128: return;
129:
130: /* Allocate space for the interfaces list. */
1.4 millert 131: for (ifa = ifaddrs; ifa != NULL; ifa = ifa -> ifa_next) {
1.2 millert 132: /* Skip interfaces marked "down" and "loopback". */
133: if (ifa->ifa_addr == NULL || !(ifa->ifa_flags & IFF_UP) ||
134: (ifa->ifa_flags & IFF_LOOPBACK))
135: continue;
136:
137: switch(ifa->ifa_addr->sa_family) {
138: /* XXX - AF_INET6 */
139: case AF_INET:
140: num_interfaces++;
141: break;
142: }
143: }
1.4 millert 144: if (num_interfaces == 0)
145: return;
1.2 millert 146: interfaces =
1.4 millert 147: (struct interface *) emalloc2(num_interfaces, sizeof(struct interface));
1.2 millert 148:
149: /* Store the ip addr / netmask pairs. */
1.4 millert 150: for (ifa = ifaddrs, i = 0; ifa != NULL; ifa = ifa -> ifa_next) {
1.2 millert 151: /* Skip interfaces marked "down" and "loopback". */
152: if (ifa->ifa_addr == NULL || !(ifa->ifa_flags & IFF_UP) ||
153: (ifa->ifa_flags & IFF_LOOPBACK))
154: continue;
155:
156: switch(ifa->ifa_addr->sa_family) {
157: /* XXX - AF_INET6 */
158: case AF_INET:
159: sin = (struct sockaddr_in *)ifa->ifa_addr;
160: memcpy(&interfaces[i].addr, &sin->sin_addr,
161: sizeof(struct in_addr));
162: sin = (struct sockaddr_in *)ifa->ifa_netmask;
163: memcpy(&interfaces[i].netmask, &sin->sin_addr,
164: sizeof(struct in_addr));
165: i++;
166: break;
167: }
168: }
1.3 millert 169: #ifdef HAVE_FREEIFADDRS
1.2 millert 170: freeifaddrs(ifaddrs);
1.3 millert 171: #else
172: free(ifaddrs);
173: #endif
1.2 millert 174: }
175:
176: #elif defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
177:
1.1 millert 178: /*
179: * Allocate and fill in the interfaces global variable with the
180: * machine's ip addresses and netmasks.
181: */
182: void
183: load_interfaces()
184: {
185: struct ifconf *ifconf;
186: struct ifreq *ifr, ifr_tmp;
187: struct sockaddr_in *sin;
188: int sock, n, i;
189: size_t len = sizeof(struct ifconf) + BUFSIZ;
190: char *previfname = "", *ifconf_buf = NULL;
191: #ifdef _ISC
192: struct strioctl strioctl;
193: #endif /* _ISC */
194:
195: sock = socket(AF_INET, SOCK_DGRAM, 0);
1.5 millert 196: if (sock < 0)
197: err(1, "cannot open socket");
1.1 millert 198:
199: /*
1.4 millert 200: * Get interface configuration or return (leaving num_interfaces == 0)
1.1 millert 201: */
202: for (;;) {
203: ifconf_buf = erealloc(ifconf_buf, len);
204: ifconf = (struct ifconf *) ifconf_buf;
205: ifconf->ifc_len = len - sizeof(struct ifconf);
206: ifconf->ifc_buf = (caddr_t) (ifconf_buf + sizeof(struct ifconf));
207:
208: /* Networking may not be installed in kernel... */
209: #ifdef _ISC
210: STRSET(SIOCGIFCONF, (caddr_t) ifconf, len);
211: if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
212: #else
213: if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0) {
214: #endif /* _ISC */
215: free(ifconf_buf);
216: (void) close(sock);
217: return;
218: }
219:
220: /* Break out of loop if we have a big enough buffer. */
221: if (ifconf->ifc_len + sizeof(struct ifreq) < len)
222: break;
223: len += BUFSIZ;
224: }
225:
226: /* Allocate space for the maximum number of interfaces that could exist. */
1.4 millert 227: if ((n = ifconf->ifc_len / sizeof(struct ifreq)) == 0)
228: return;
229: interfaces = (struct interface *) emalloc2(n, sizeof(struct interface));
1.1 millert 230:
231: /* For each interface, store the ip address and netmask. */
232: for (i = 0; i < ifconf->ifc_len; ) {
233: /* Get a pointer to the current interface. */
234: ifr = (struct ifreq *) &ifconf->ifc_buf[i];
235:
236: /* Set i to the subscript of the next interface. */
237: i += sizeof(struct ifreq);
238: #ifdef HAVE_SA_LEN
239: if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
240: i += ifr->ifr_addr.sa_len - sizeof(struct sockaddr);
241: #endif /* HAVE_SA_LEN */
242:
243: /* Skip duplicates and interfaces with NULL addresses. */
244: sin = (struct sockaddr_in *) &ifr->ifr_addr;
245: if (sin->sin_addr.s_addr == 0 ||
246: strncmp(previfname, ifr->ifr_name, sizeof(ifr->ifr_name) - 1) == 0)
247: continue;
248:
249: if (ifr->ifr_addr.sa_family != AF_INET)
250: continue;
251:
252: #ifdef SIOCGIFFLAGS
253: memset(&ifr_tmp, 0, sizeof(ifr_tmp));
254: strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
255: if (ioctl(sock, SIOCGIFFLAGS, (caddr_t) &ifr_tmp) < 0)
256: #endif
257: ifr_tmp = *ifr;
258:
259: /* Skip interfaces marked "down" and "loopback". */
260: if (!(ifr_tmp.ifr_flags & IFF_UP) || (ifr_tmp.ifr_flags & IFF_LOOPBACK))
261: continue;
262:
263: sin = (struct sockaddr_in *) &ifr->ifr_addr;
264: interfaces[num_interfaces].addr.s_addr = sin->sin_addr.s_addr;
265:
266: /* Stash the name of the interface we saved. */
267: previfname = ifr->ifr_name;
268:
269: /* Get the netmask. */
270: (void) memset(&ifr_tmp, 0, sizeof(ifr_tmp));
271: strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
272: #ifdef SIOCGIFNETMASK
273: #ifdef _ISC
274: STRSET(SIOCGIFNETMASK, (caddr_t) &ifr_tmp, sizeof(ifr_tmp));
275: if (ioctl(sock, I_STR, (caddr_t) &strioctl) == 0) {
276: #else
277: if (ioctl(sock, SIOCGIFNETMASK, (caddr_t) &ifr_tmp) == 0) {
278: #endif /* _ISC */
279: sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
280:
281: interfaces[num_interfaces].netmask.s_addr = sin->sin_addr.s_addr;
282: } else {
283: #else
284: {
285: #endif /* SIOCGIFNETMASK */
286: if (IN_CLASSC(interfaces[num_interfaces].addr.s_addr))
287: interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSC_NET);
288: else if (IN_CLASSB(interfaces[num_interfaces].addr.s_addr))
289: interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSB_NET);
290: else
291: interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSA_NET);
292: }
293:
294: /* Only now can we be sure it was a good/interesting interface. */
295: num_interfaces++;
296: }
297:
298: /* If the expected size < real size, realloc the array. */
299: if (n != num_interfaces) {
300: if (num_interfaces != 0)
1.4 millert 301: interfaces = (struct interface *) erealloc3(interfaces,
302: num_interfaces, sizeof(struct interface));
1.1 millert 303: else
304: free(interfaces);
305: }
306: free(ifconf_buf);
307: (void) close(sock);
308: }
309:
310: #else /* !SIOCGIFCONF || STUB_LOAD_INTERFACES */
311:
312: /*
313: * Stub function for those without SIOCGIFCONF
314: */
315: void
316: load_interfaces()
317: {
318: return;
319: }
320:
321: #endif /* SIOCGIFCONF && !STUB_LOAD_INTERFACES */
1.2 millert 322:
323: void
324: dump_interfaces()
325: {
326: int i;
327:
328: puts("Local IP address and netmask pairs:");
329: for (i = 0; i < num_interfaces; i++)
330: printf("\t%s / 0x%x\n", inet_ntoa(interfaces[i].addr),
1.5 millert 331: (unsigned int)ntohl(interfaces[i].netmask.s_addr));
1.2 millert 332: }