2 * Copyright (c) 1996, 1998-2005, 2007-2013
3 * Todd C. Miller <Todd.Miller@courtesan.com>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Sponsored in part by the Defense Advanced Research Projects
18 * Agency (DARPA) and Air Force Research Laboratory, Air Force
19 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
23 * Suppress a warning w/ gcc on Digital UN*X.
24 * The system headers should really do this....
26 #if defined(__osf__) && !defined(__cplusplus)
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <sys/ioctl.h>
37 #if defined(HAVE_SYS_SOCKIO_H) && !defined(SIOCGIFCONF)
38 # include <sys/sockio.h>
48 #endif /* STDC_HEADERS */
50 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
54 #endif /* HAVE_STRING_H */
57 #endif /* HAVE_STRINGS_H */
60 #endif /* HAVE_UNISTD_H */
64 # include <sys/stream.h>
65 # include <sys/sioctl.h>
66 # include <sys/stropts.h>
67 # define STRSET(cmd, param, len) {strioctl.ic_cmd=(cmd);\
68 strioctl.ic_dp=(param);\
69 strioctl.ic_timout=0;\
70 strioctl.ic_len=(len);}
73 # include <net/soioctl.h>
75 #include <netinet/in.h>
76 #include <arpa/inet.h>
78 #ifdef HAVE_GETIFADDRS
85 #include "sudo_debug.h"
87 #define DEFAULT_TEXT_DOMAIN "sudo"
90 /* Minix apparently lacks IFF_LOOPBACK */
92 # define IFF_LOOPBACK 0
95 #ifndef INET6_ADDRSTRLEN
96 # define INET6_ADDRSTRLEN 46
99 #ifdef HAVE_GETIFADDRS
102 * Fill in the interfaces string with the machine's ip addresses and netmasks
103 * and return the number of interfaces found.
106 get_net_ifs(char **addrinfo)
108 struct ifaddrs *ifa, *ifaddrs;
109 struct sockaddr_in *sin;
110 #ifdef HAVE_STRUCT_IN6_ADDR
111 struct sockaddr_in6 *sin6;
112 char addrbuf[INET6_ADDRSTRLEN];
114 int ailen, i, len, num_interfaces = 0;
116 debug_decl(get_net_ifs, SUDO_DEBUG_NETIF)
118 if (getifaddrs(&ifaddrs))
121 /* Allocate space for the interfaces info string. */
122 for (ifa = ifaddrs; ifa != NULL; ifa = ifa -> ifa_next) {
123 /* Skip interfaces marked "down" and "loopback". */
124 if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL ||
125 !ISSET(ifa->ifa_flags, IFF_UP) || ISSET(ifa->ifa_flags, IFF_LOOPBACK))
128 switch (ifa->ifa_addr->sa_family) {
130 #ifdef HAVE_STRUCT_IN6_ADDR
137 if (num_interfaces == 0)
139 ailen = num_interfaces * 2 * INET6_ADDRSTRLEN;
140 *addrinfo = cp = emalloc(ailen);
142 /* Store the IP addr/netmask pairs. */
143 for (ifa = ifaddrs, i = 0; ifa != NULL; ifa = ifa -> ifa_next) {
144 /* Skip interfaces marked "down" and "loopback". */
145 if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL ||
146 !ISSET(ifa->ifa_flags, IFF_UP) || ISSET(ifa->ifa_flags, IFF_LOOPBACK))
149 switch (ifa->ifa_addr->sa_family) {
151 sin = (struct sockaddr_in *)ifa->ifa_addr;
152 len = snprintf(cp, ailen - (*addrinfo - cp),
153 "%s%s/", cp == *addrinfo ? "" : " ",
154 inet_ntoa(sin->sin_addr));
155 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
156 warningx(_("load_interfaces: overflow detected"));
161 sin = (struct sockaddr_in *)ifa->ifa_netmask;
162 len = snprintf(cp, ailen - (*addrinfo - cp),
163 "%s", inet_ntoa(sin->sin_addr));
164 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
165 warningx(_("load_interfaces: overflow detected"));
170 #ifdef HAVE_STRUCT_IN6_ADDR
172 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
173 inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
174 len = snprintf(cp, ailen - (*addrinfo - cp),
175 "%s%s/", cp == *addrinfo ? "" : " ", addrbuf);
176 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
177 warningx(_("load_interfaces: overflow detected"));
182 sin6 = (struct sockaddr_in6 *)ifa->ifa_netmask;
183 inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
184 len = snprintf(cp, ailen - (*addrinfo - cp), "%s", addrbuf);
185 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
186 warningx(_("load_interfaces: overflow detected"));
191 #endif /* HAVE_STRUCT_IN6_ADDR */
196 #ifdef HAVE_FREEIFADDRS
197 freeifaddrs(ifaddrs);
201 debug_return_int(num_interfaces);
204 #elif defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
207 * Allocate and fill in the interfaces global variable with the
208 * machine's ip addresses and netmasks.
211 get_net_ifs(char **addrinfo)
213 struct ifconf *ifconf;
214 struct ifreq *ifr, ifr_tmp;
215 struct sockaddr_in *sin;
216 int ailen, i, len, n, sock, num_interfaces = 0;
217 size_t buflen = sizeof(struct ifconf) + BUFSIZ;
218 char *cp, *previfname = "", *ifconf_buf = NULL;
220 struct strioctl strioctl;
222 debug_decl(get_net_ifs, SUDO_DEBUG_NETIF)
224 sock = socket(AF_INET, SOCK_DGRAM, 0);
226 fatal(_("unable to open socket"));
229 * Get interface configuration or return.
232 ifconf_buf = emalloc(buflen);
233 ifconf = (struct ifconf *) ifconf_buf;
234 ifconf->ifc_len = buflen - sizeof(struct ifconf);
235 ifconf->ifc_buf = (caddr_t) (ifconf_buf + sizeof(struct ifconf));
238 STRSET(SIOCGIFCONF, (caddr_t) ifconf, buflen);
239 if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0)
241 /* Note that some kernels return EINVAL if the buffer is too small */
242 if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0 && errno != EINVAL)
246 /* Break out of loop if we have a big enough buffer. */
247 if (ifconf->ifc_len + sizeof(struct ifreq) < buflen)
253 /* Allocate space for the maximum number of interfaces that could exist. */
254 if ((n = ifconf->ifc_len / sizeof(struct ifreq)) == 0)
256 ailen = n * 2 * INET6_ADDRSTRLEN;
257 *addrinfo = cp = emalloc(ailen);
259 /* For each interface, store the ip address and netmask. */
260 for (i = 0; i < ifconf->ifc_len; ) {
261 /* Get a pointer to the current interface. */
262 ifr = (struct ifreq *) &ifconf->ifc_buf[i];
264 /* Set i to the subscript of the next interface. */
265 i += sizeof(struct ifreq);
266 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
267 if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
268 i += ifr->ifr_addr.sa_len - sizeof(struct sockaddr);
269 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
271 /* Skip duplicates and interfaces with NULL addresses. */
272 sin = (struct sockaddr_in *) &ifr->ifr_addr;
273 if (sin->sin_addr.s_addr == 0 ||
274 strncmp(previfname, ifr->ifr_name, sizeof(ifr->ifr_name) - 1) == 0)
277 if (ifr->ifr_addr.sa_family != AF_INET)
281 memset(&ifr_tmp, 0, sizeof(ifr_tmp));
282 strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
283 if (ioctl(sock, SIOCGIFFLAGS, (caddr_t) &ifr_tmp) < 0)
287 /* Skip interfaces marked "down" and "loopback". */
288 if (!ISSET(ifr_tmp.ifr_flags, IFF_UP) ||
289 ISSET(ifr_tmp.ifr_flags, IFF_LOOPBACK))
292 sin = (struct sockaddr_in *) &ifr->ifr_addr;
293 len = snprintf(cp, ailen - (*addrinfo - cp),
294 "%s%s/", cp == *addrinfo ? "" : " ",
295 inet_ntoa(sin->sin_addr));
296 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
297 warningx(_("load_interfaces: overflow detected"));
302 /* Stash the name of the interface we saved. */
303 previfname = ifr->ifr_name;
305 /* Get the netmask. */
306 memset(&ifr_tmp, 0, sizeof(ifr_tmp));
307 strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
309 STRSET(SIOCGIFNETMASK, (caddr_t) &ifr_tmp, sizeof(ifr_tmp));
310 if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
312 if (ioctl(sock, SIOCGIFNETMASK, (caddr_t) &ifr_tmp) < 0) {
314 sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
315 sin->sin_addr.s_addr = htonl(IN_CLASSC_NET);
317 sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
318 len = snprintf(cp, ailen - (*addrinfo - cp),
319 "%s", inet_ntoa(sin->sin_addr));
320 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
321 warningx(_("load_interfaces: overflow detected"));
332 debug_return_int(num_interfaces);
335 #else /* !SIOCGIFCONF || STUB_LOAD_INTERFACES */
338 * Stub function for those without SIOCGIFCONF or getifaddrs()
341 get_net_ifs(char **addrinfo)
343 debug_decl(get_net_ifs, SUDO_DEBUG_NETIF)
347 #endif /* SIOCGIFCONF && !STUB_LOAD_INTERFACES */