[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.8

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.8     ! millert    92: __unused static const char rcsid[] = "$Sudo: interfaces.c,v 1.72.2.4 2007/06/14 16:24:09 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:     /* XXX - sockaddr_in6 sin6; */
                    107:     struct sockaddr_in *sin;
                    108:     int i;
                    109:
                    110:     if (getifaddrs(&ifaddrs))
                    111:        return;
                    112:
                    113:     /* Allocate space for the interfaces list. */
1.4       millert   114:     for (ifa = ifaddrs; ifa != NULL; ifa = ifa -> ifa_next) {
1.2       millert   115:        /* Skip interfaces marked "down" and "loopback". */
1.7       millert   116:        if (ifa->ifa_addr == NULL || !ISSET(ifa->ifa_flags, IFF_UP) ||
                    117:            ISSET(ifa->ifa_flags, IFF_LOOPBACK))
1.2       millert   118:            continue;
                    119:
                    120:        switch(ifa->ifa_addr->sa_family) {
                    121:            /* XXX - AF_INET6 */
                    122:            case AF_INET:
                    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:            /* XXX - AF_INET6 */
                    141:            case AF_INET:
                    142:                sin = (struct sockaddr_in *)ifa->ifa_addr;
                    143:                memcpy(&interfaces[i].addr, &sin->sin_addr,
                    144:                    sizeof(struct in_addr));
                    145:                sin = (struct sockaddr_in *)ifa->ifa_netmask;
                    146:                memcpy(&interfaces[i].netmask, &sin->sin_addr,
                    147:                    sizeof(struct in_addr));
                    148:                i++;
                    149:                break;
                    150:        }
                    151:     }
1.3       millert   152: #ifdef HAVE_FREEIFADDRS
1.2       millert   153:     freeifaddrs(ifaddrs);
1.3       millert   154: #else
1.8     ! millert   155:     efree(ifaddrs);
1.3       millert   156: #endif
1.2       millert   157: }
                    158:
                    159: #elif defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
                    160:
1.1       millert   161: /*
                    162:  * Allocate and fill in the interfaces global variable with the
                    163:  * machine's ip addresses and netmasks.
                    164:  */
                    165: void
                    166: load_interfaces()
                    167: {
                    168:     struct ifconf *ifconf;
                    169:     struct ifreq *ifr, ifr_tmp;
                    170:     struct sockaddr_in *sin;
                    171:     int sock, n, i;
                    172:     size_t len = sizeof(struct ifconf) + BUFSIZ;
                    173:     char *previfname = "", *ifconf_buf = NULL;
                    174: #ifdef _ISC
                    175:     struct strioctl strioctl;
                    176: #endif /* _ISC */
                    177:
                    178:     sock = socket(AF_INET, SOCK_DGRAM, 0);
1.5       millert   179:     if (sock < 0)
                    180:        err(1, "cannot open socket");
1.1       millert   181:
                    182:     /*
1.4       millert   183:      * Get interface configuration or return (leaving num_interfaces == 0)
1.1       millert   184:      */
                    185:     for (;;) {
                    186:        ifconf_buf = erealloc(ifconf_buf, len);
                    187:        ifconf = (struct ifconf *) ifconf_buf;
                    188:        ifconf->ifc_len = len - sizeof(struct ifconf);
                    189:        ifconf->ifc_buf = (caddr_t) (ifconf_buf + sizeof(struct ifconf));
                    190:
                    191: #ifdef _ISC
                    192:        STRSET(SIOCGIFCONF, (caddr_t) ifconf, len);
                    193:        if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
                    194: #else
1.8     ! millert   195:        /* Note that some kernels return EINVAL if the buffer is too small */
        !           196:        if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0 && errno != EINVAL) {
1.1       millert   197: #endif /* _ISC */
1.8     ! millert   198:            efree(ifconf_buf);
1.1       millert   199:            (void) close(sock);
                    200:            return;
                    201:        }
                    202:
                    203:        /* Break out of loop if we have a big enough buffer. */
                    204:        if (ifconf->ifc_len + sizeof(struct ifreq) < len)
                    205:            break;
                    206:        len += BUFSIZ;
                    207:     }
                    208:
                    209:     /* Allocate space for the maximum number of interfaces that could exist. */
1.4       millert   210:     if ((n = ifconf->ifc_len / sizeof(struct ifreq)) == 0)
                    211:        return;
                    212:     interfaces = (struct interface *) emalloc2(n, sizeof(struct interface));
1.1       millert   213:
                    214:     /* For each interface, store the ip address and netmask. */
                    215:     for (i = 0; i < ifconf->ifc_len; ) {
                    216:        /* Get a pointer to the current interface. */
                    217:        ifr = (struct ifreq *) &ifconf->ifc_buf[i];
                    218:
                    219:        /* Set i to the subscript of the next interface. */
                    220:        i += sizeof(struct ifreq);
                    221: #ifdef HAVE_SA_LEN
                    222:        if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
                    223:            i += ifr->ifr_addr.sa_len - sizeof(struct sockaddr);
                    224: #endif /* HAVE_SA_LEN */
                    225:
                    226:        /* Skip duplicates and interfaces with NULL addresses. */
                    227:        sin = (struct sockaddr_in *) &ifr->ifr_addr;
                    228:        if (sin->sin_addr.s_addr == 0 ||
                    229:            strncmp(previfname, ifr->ifr_name, sizeof(ifr->ifr_name) - 1) == 0)
                    230:            continue;
                    231:
                    232:        if (ifr->ifr_addr.sa_family != AF_INET)
                    233:                continue;
                    234:
                    235: #ifdef SIOCGIFFLAGS
                    236:        memset(&ifr_tmp, 0, sizeof(ifr_tmp));
                    237:        strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
                    238:        if (ioctl(sock, SIOCGIFFLAGS, (caddr_t) &ifr_tmp) < 0)
                    239: #endif
                    240:            ifr_tmp = *ifr;
                    241:
                    242:        /* Skip interfaces marked "down" and "loopback". */
1.7       millert   243:        if (!ISSET(ifr_tmp.ifr_flags, IFF_UP) ||
                    244:            ISSET(ifr_tmp.ifr_flags, IFF_LOOPBACK))
1.1       millert   245:                continue;
                    246:
                    247:        sin = (struct sockaddr_in *) &ifr->ifr_addr;
                    248:        interfaces[num_interfaces].addr.s_addr = sin->sin_addr.s_addr;
                    249:
                    250:        /* Stash the name of the interface we saved. */
                    251:        previfname = ifr->ifr_name;
                    252:
                    253:        /* Get the netmask. */
                    254:        (void) memset(&ifr_tmp, 0, sizeof(ifr_tmp));
                    255:        strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
                    256: #ifdef SIOCGIFNETMASK
                    257: #ifdef _ISC
                    258:        STRSET(SIOCGIFNETMASK, (caddr_t) &ifr_tmp, sizeof(ifr_tmp));
                    259:        if (ioctl(sock, I_STR, (caddr_t) &strioctl) == 0) {
                    260: #else
                    261:        if (ioctl(sock, SIOCGIFNETMASK, (caddr_t) &ifr_tmp) == 0) {
                    262: #endif /* _ISC */
                    263:            sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
                    264:
                    265:            interfaces[num_interfaces].netmask.s_addr = sin->sin_addr.s_addr;
                    266:        } else {
                    267: #else
                    268:        {
                    269: #endif /* SIOCGIFNETMASK */
                    270:            if (IN_CLASSC(interfaces[num_interfaces].addr.s_addr))
                    271:                interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSC_NET);
                    272:            else if (IN_CLASSB(interfaces[num_interfaces].addr.s_addr))
                    273:                interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSB_NET);
                    274:            else
                    275:                interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSA_NET);
                    276:        }
                    277:
                    278:        /* Only now can we be sure it was a good/interesting interface. */
                    279:        num_interfaces++;
                    280:     }
                    281:
                    282:     /* If the expected size < real size, realloc the array. */
                    283:     if (n != num_interfaces) {
                    284:        if (num_interfaces != 0)
1.4       millert   285:            interfaces = (struct interface *) erealloc3(interfaces,
                    286:                num_interfaces, sizeof(struct interface));
1.1       millert   287:        else
1.8     ! millert   288:            efree(interfaces);
1.1       millert   289:     }
1.8     ! millert   290:     efree(ifconf_buf);
1.1       millert   291:     (void) close(sock);
                    292: }
                    293:
                    294: #else /* !SIOCGIFCONF || STUB_LOAD_INTERFACES */
                    295:
                    296: /*
                    297:  * Stub function for those without SIOCGIFCONF
                    298:  */
                    299: void
                    300: load_interfaces()
                    301: {
                    302:     return;
                    303: }
                    304:
                    305: #endif /* SIOCGIFCONF && !STUB_LOAD_INTERFACES */
1.2       millert   306:
                    307: void
                    308: dump_interfaces()
                    309: {
                    310:     int i;
                    311:
                    312:     puts("Local IP address and netmask pairs:");
                    313:     for (i = 0; i < num_interfaces; i++)
                    314:        printf("\t%s / 0x%x\n", inet_ntoa(interfaces[i].addr),
1.5       millert   315:            (unsigned int)ntohl(interfaces[i].netmask.s_addr));
1.2       millert   316: }