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