[BACK]Return to interfaces.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sudo

Annotation of src/usr.bin/sudo/interfaces.c, Revision 1.15

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