Annotation of src/usr.bin/sudo/interfaces.c, Revision 1.1.1.1
1.1 millert 1: /*
2: * Copyright (c) 1996, 1998, 1999 Todd C. Miller <Todd.Miller@courtesan.com>
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:
46: #include <stdio.h>
47: #ifdef STDC_HEADERS
48: #include <stdlib.h>
49: #endif /* STDC_HEADERS */
50: #ifdef HAVE_UNISTD_H
51: #include <unistd.h>
52: #endif /* HAVE_UNISTD_H */
53: #ifdef HAVE_STRING_H
54: #include <string.h>
55: #endif /* HAVE_STRING_H */
56: #ifdef HAVE_STRINGS_H
57: #include <strings.h>
58: #endif /* HAVE_STRINGS_H */
59: #include <netdb.h>
60: #include <errno.h>
61: #include <sys/types.h>
62: #include <sys/socket.h>
63: #include <sys/param.h>
64: #include <sys/time.h>
65: #include <sys/ioctl.h>
66: #if defined(HAVE_SYS_SOCKIO_H) && !defined(SIOCGIFCONF)
67: #include <sys/sockio.h>
68: #endif
69: #ifdef _ISC
70: #include <sys/stream.h>
71: #include <sys/sioctl.h>
72: #include <sys/stropts.h>
73: #include <net/errno.h>
74: #define STRSET(cmd, param, len) {strioctl.ic_cmd=(cmd);\
75: strioctl.ic_dp=(param);\
76: strioctl.ic_timout=0;\
77: strioctl.ic_len=(len);}
78: #endif /* _ISC */
79: #ifdef _MIPS
80: #include <net/soioctl.h>
81: #endif /* _MIPS */
82: #include <netinet/in.h>
83: #include <arpa/inet.h>
84: #include <net/if.h>
85:
86: #include "sudo.h"
87: #include "interfaces.h"
88:
89: #ifndef lint
90: static const char rcsid[] = "$Sudo: interfaces.c,v 1.59 1999/08/12 16:24:09 millert Exp $";
91: #endif /* lint */
92:
93:
94: #if defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
95: /*
96: * Allocate and fill in the interfaces global variable with the
97: * machine's ip addresses and netmasks.
98: */
99: void
100: load_interfaces()
101: {
102: struct ifconf *ifconf;
103: struct ifreq *ifr, ifr_tmp;
104: struct sockaddr_in *sin;
105: int sock, n, i;
106: size_t len = sizeof(struct ifconf) + BUFSIZ;
107: char *previfname = "", *ifconf_buf = NULL;
108: #ifdef _ISC
109: struct strioctl strioctl;
110: #endif /* _ISC */
111:
112: sock = socket(AF_INET, SOCK_DGRAM, 0);
113: if (sock < 0) {
114: (void) fprintf(stderr, "%s: cannot open socket: %s\n",
115: Argv[0], strerror(errno));
116: exit(1);
117: }
118:
119: /*
120: * Get interface configuration or return (leaving num_interfaces 0)
121: */
122: for (;;) {
123: ifconf_buf = erealloc(ifconf_buf, len);
124: ifconf = (struct ifconf *) ifconf_buf;
125: ifconf->ifc_len = len - sizeof(struct ifconf);
126: ifconf->ifc_buf = (caddr_t) (ifconf_buf + sizeof(struct ifconf));
127:
128: /* Networking may not be installed in kernel... */
129: #ifdef _ISC
130: STRSET(SIOCGIFCONF, (caddr_t) ifconf, len);
131: if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
132: #else
133: if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0) {
134: #endif /* _ISC */
135: free(ifconf_buf);
136: (void) close(sock);
137: return;
138: }
139:
140: /* Break out of loop if we have a big enough buffer. */
141: if (ifconf->ifc_len + sizeof(struct ifreq) < len)
142: break;
143: len += BUFSIZ;
144: }
145:
146: /* Allocate space for the maximum number of interfaces that could exist. */
147: n = ifconf->ifc_len / sizeof(struct ifreq);
148: interfaces = (struct interface *) emalloc(sizeof(struct interface) * n);
149:
150: /* For each interface, store the ip address and netmask. */
151: for (i = 0; i < ifconf->ifc_len; ) {
152: /* Get a pointer to the current interface. */
153: ifr = (struct ifreq *) &ifconf->ifc_buf[i];
154:
155: /* Set i to the subscript of the next interface. */
156: i += sizeof(struct ifreq);
157: #ifdef HAVE_SA_LEN
158: if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
159: i += ifr->ifr_addr.sa_len - sizeof(struct sockaddr);
160: #endif /* HAVE_SA_LEN */
161:
162: /* Skip duplicates and interfaces with NULL addresses. */
163: sin = (struct sockaddr_in *) &ifr->ifr_addr;
164: if (sin->sin_addr.s_addr == 0 ||
165: strncmp(previfname, ifr->ifr_name, sizeof(ifr->ifr_name) - 1) == 0)
166: continue;
167:
168: if (ifr->ifr_addr.sa_family != AF_INET)
169: continue;
170:
171: #ifdef SIOCGIFFLAGS
172: memset(&ifr_tmp, 0, sizeof(ifr_tmp));
173: strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
174: if (ioctl(sock, SIOCGIFFLAGS, (caddr_t) &ifr_tmp) < 0)
175: #endif
176: ifr_tmp = *ifr;
177:
178: /* Skip interfaces marked "down" and "loopback". */
179: if (!(ifr_tmp.ifr_flags & IFF_UP) || (ifr_tmp.ifr_flags & IFF_LOOPBACK))
180: continue;
181:
182: sin = (struct sockaddr_in *) &ifr->ifr_addr;
183: interfaces[num_interfaces].addr.s_addr = sin->sin_addr.s_addr;
184:
185: /* Stash the name of the interface we saved. */
186: previfname = ifr->ifr_name;
187:
188: /* Get the netmask. */
189: (void) memset(&ifr_tmp, 0, sizeof(ifr_tmp));
190: strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
191: #ifdef SIOCGIFNETMASK
192: #ifdef _ISC
193: STRSET(SIOCGIFNETMASK, (caddr_t) &ifr_tmp, sizeof(ifr_tmp));
194: if (ioctl(sock, I_STR, (caddr_t) &strioctl) == 0) {
195: #else
196: if (ioctl(sock, SIOCGIFNETMASK, (caddr_t) &ifr_tmp) == 0) {
197: #endif /* _ISC */
198: sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
199:
200: interfaces[num_interfaces].netmask.s_addr = sin->sin_addr.s_addr;
201: } else {
202: #else
203: {
204: #endif /* SIOCGIFNETMASK */
205: if (IN_CLASSC(interfaces[num_interfaces].addr.s_addr))
206: interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSC_NET);
207: else if (IN_CLASSB(interfaces[num_interfaces].addr.s_addr))
208: interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSB_NET);
209: else
210: interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSA_NET);
211: }
212:
213: /* Only now can we be sure it was a good/interesting interface. */
214: num_interfaces++;
215: }
216:
217: /* If the expected size < real size, realloc the array. */
218: if (n != num_interfaces) {
219: if (num_interfaces != 0)
220: interfaces = (struct interface *) erealloc(interfaces,
221: sizeof(struct interface) * num_interfaces);
222: else
223: free(interfaces);
224: }
225: free(ifconf_buf);
226: (void) close(sock);
227: }
228:
229: #else /* !SIOCGIFCONF || STUB_LOAD_INTERFACES */
230:
231: /*
232: * Stub function for those without SIOCGIFCONF
233: */
234: void
235: load_interfaces()
236: {
237: return;
238: }
239:
240: #endif /* SIOCGIFCONF && !STUB_LOAD_INTERFACES */