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

1.1       millert     1: /*
1.4       millert     2:  * Copyright (c) 1996, 1998-2003 Todd C. Miller <Todd.Miller@courtesan.com>
1.1       millert     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:
1.2       millert    46: #include <sys/types.h>
                     47: #include <sys/socket.h>
                     48: #include <sys/param.h>
                     49: #include <sys/time.h>
                     50: #include <sys/ioctl.h>
                     51: #if defined(HAVE_SYS_SOCKIO_H) && !defined(SIOCGIFCONF)
                     52: # include <sys/sockio.h>
                     53: #endif
1.1       millert    54: #include <stdio.h>
                     55: #ifdef STDC_HEADERS
1.2       millert    56: # include <stdlib.h>
                     57: # include <stddef.h>
                     58: #else
                     59: # ifdef HAVE_STDLIB_H
                     60: #  include <stdlib.h>
                     61: # endif
1.1       millert    62: #endif /* STDC_HEADERS */
1.2       millert    63: #ifdef HAVE_STRING_H
                     64: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
                     65: #  include <memory.h>
                     66: # endif
                     67: # include <string.h>
                     68: #else
                     69: # ifdef HAVE_STRINGS_H
                     70: #  include <strings.h>
                     71: # endif
                     72: #endif /* HAVE_STRING_H */
1.1       millert    73: #ifdef HAVE_UNISTD_H
1.2       millert    74: # include <unistd.h>
1.1       millert    75: #endif /* HAVE_UNISTD_H */
1.5     ! millert    76: #ifdef HAVE_ERR_H
        !            77: # include <err.h>
        !            78: #else
        !            79: # include "emul/err.h"
        !            80: #endif /* HAVE_ERR_H */
1.1       millert    81: #include <netdb.h>
                     82: #ifdef _ISC
1.2       millert    83: # include <sys/stream.h>
                     84: # include <sys/sioctl.h>
                     85: # include <sys/stropts.h>
                     86: # define STRSET(cmd, param, len) {strioctl.ic_cmd=(cmd);\
1.1       millert    87:                                 strioctl.ic_dp=(param);\
                     88:                                 strioctl.ic_timout=0;\
                     89:                                 strioctl.ic_len=(len);}
                     90: #endif /* _ISC */
                     91: #ifdef _MIPS
1.2       millert    92: # include <net/soioctl.h>
1.1       millert    93: #endif /* _MIPS */
                     94: #include <netinet/in.h>
                     95: #include <arpa/inet.h>
                     96: #include <net/if.h>
1.2       millert    97: #ifdef HAVE_GETIFADDRS
                     98: # include <ifaddrs.h>
                     99: #endif
1.1       millert   100:
                    101: #include "sudo.h"
                    102: #include "interfaces.h"
                    103:
                    104: #ifndef lint
1.5     ! millert   105: static const char rcsid[] = "$Sudo: interfaces.c,v 1.69 2003/04/02 18:25:19 millert Exp $";
1.1       millert   106: #endif /* lint */
                    107:
                    108:
1.2       millert   109: #ifdef HAVE_GETIFADDRS
                    110:
                    111: /*
                    112:  * Allocate and fill in the interfaces global variable with the
                    113:  * machine's ip addresses and netmasks.
                    114:  */
                    115: void
                    116: load_interfaces()
                    117: {
                    118:     struct ifaddrs *ifa, *ifaddrs;
                    119:     /* XXX - sockaddr_in6 sin6; */
                    120:     struct sockaddr_in *sin;
                    121:     int i;
                    122:
                    123:     if (getifaddrs(&ifaddrs))
                    124:        return;
                    125:
                    126:     /* Allocate space for the interfaces list. */
1.4       millert   127:     for (ifa = ifaddrs; ifa != NULL; ifa = ifa -> ifa_next) {
1.2       millert   128:        /* Skip interfaces marked "down" and "loopback". */
                    129:        if (ifa->ifa_addr == NULL || !(ifa->ifa_flags & IFF_UP) ||
                    130:            (ifa->ifa_flags & IFF_LOOPBACK))
                    131:            continue;
                    132:
                    133:        switch(ifa->ifa_addr->sa_family) {
                    134:            /* XXX - AF_INET6 */
                    135:            case AF_INET:
                    136:                num_interfaces++;
                    137:                break;
                    138:        }
                    139:     }
1.4       millert   140:     if (num_interfaces == 0)
                    141:        return;
1.2       millert   142:     interfaces =
1.4       millert   143:        (struct interface *) emalloc2(num_interfaces, sizeof(struct interface));
1.2       millert   144:
                    145:     /* Store the ip addr / netmask pairs. */
1.4       millert   146:     for (ifa = ifaddrs, i = 0; ifa != NULL; ifa = ifa -> ifa_next) {
1.2       millert   147:        /* Skip interfaces marked "down" and "loopback". */
                    148:        if (ifa->ifa_addr == NULL || !(ifa->ifa_flags & IFF_UP) ||
                    149:            (ifa->ifa_flags & IFF_LOOPBACK))
                    150:                continue;
                    151:
                    152:        switch(ifa->ifa_addr->sa_family) {
                    153:            /* XXX - AF_INET6 */
                    154:            case AF_INET:
                    155:                sin = (struct sockaddr_in *)ifa->ifa_addr;
                    156:                memcpy(&interfaces[i].addr, &sin->sin_addr,
                    157:                    sizeof(struct in_addr));
                    158:                sin = (struct sockaddr_in *)ifa->ifa_netmask;
                    159:                memcpy(&interfaces[i].netmask, &sin->sin_addr,
                    160:                    sizeof(struct in_addr));
                    161:                i++;
                    162:                break;
                    163:        }
                    164:     }
1.3       millert   165: #ifdef HAVE_FREEIFADDRS
1.2       millert   166:     freeifaddrs(ifaddrs);
1.3       millert   167: #else
                    168:     free(ifaddrs);
                    169: #endif
1.2       millert   170: }
                    171:
                    172: #elif defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
                    173:
1.1       millert   174: /*
                    175:  * Allocate and fill in the interfaces global variable with the
                    176:  * machine's ip addresses and netmasks.
                    177:  */
                    178: void
                    179: load_interfaces()
                    180: {
                    181:     struct ifconf *ifconf;
                    182:     struct ifreq *ifr, ifr_tmp;
                    183:     struct sockaddr_in *sin;
                    184:     int sock, n, i;
                    185:     size_t len = sizeof(struct ifconf) + BUFSIZ;
                    186:     char *previfname = "", *ifconf_buf = NULL;
                    187: #ifdef _ISC
                    188:     struct strioctl strioctl;
                    189: #endif /* _ISC */
                    190:
                    191:     sock = socket(AF_INET, SOCK_DGRAM, 0);
1.5     ! millert   192:     if (sock < 0)
        !           193:        err(1, "cannot open socket");
1.1       millert   194:
                    195:     /*
1.4       millert   196:      * Get interface configuration or return (leaving num_interfaces == 0)
1.1       millert   197:      */
                    198:     for (;;) {
                    199:        ifconf_buf = erealloc(ifconf_buf, len);
                    200:        ifconf = (struct ifconf *) ifconf_buf;
                    201:        ifconf->ifc_len = len - sizeof(struct ifconf);
                    202:        ifconf->ifc_buf = (caddr_t) (ifconf_buf + sizeof(struct ifconf));
                    203:
                    204:        /* Networking may not be installed in kernel... */
                    205: #ifdef _ISC
                    206:        STRSET(SIOCGIFCONF, (caddr_t) ifconf, len);
                    207:        if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
                    208: #else
                    209:        if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0) {
                    210: #endif /* _ISC */
                    211:            free(ifconf_buf);
                    212:            (void) close(sock);
                    213:            return;
                    214:        }
                    215:
                    216:        /* Break out of loop if we have a big enough buffer. */
                    217:        if (ifconf->ifc_len + sizeof(struct ifreq) < len)
                    218:            break;
                    219:        len += BUFSIZ;
                    220:     }
                    221:
                    222:     /* Allocate space for the maximum number of interfaces that could exist. */
1.4       millert   223:     if ((n = ifconf->ifc_len / sizeof(struct ifreq)) == 0)
                    224:        return;
                    225:     interfaces = (struct interface *) emalloc2(n, sizeof(struct interface));
1.1       millert   226:
                    227:     /* For each interface, store the ip address and netmask. */
                    228:     for (i = 0; i < ifconf->ifc_len; ) {
                    229:        /* Get a pointer to the current interface. */
                    230:        ifr = (struct ifreq *) &ifconf->ifc_buf[i];
                    231:
                    232:        /* Set i to the subscript of the next interface. */
                    233:        i += sizeof(struct ifreq);
                    234: #ifdef HAVE_SA_LEN
                    235:        if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
                    236:            i += ifr->ifr_addr.sa_len - sizeof(struct sockaddr);
                    237: #endif /* HAVE_SA_LEN */
                    238:
                    239:        /* Skip duplicates and interfaces with NULL addresses. */
                    240:        sin = (struct sockaddr_in *) &ifr->ifr_addr;
                    241:        if (sin->sin_addr.s_addr == 0 ||
                    242:            strncmp(previfname, ifr->ifr_name, sizeof(ifr->ifr_name) - 1) == 0)
                    243:            continue;
                    244:
                    245:        if (ifr->ifr_addr.sa_family != AF_INET)
                    246:                continue;
                    247:
                    248: #ifdef SIOCGIFFLAGS
                    249:        memset(&ifr_tmp, 0, sizeof(ifr_tmp));
                    250:        strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
                    251:        if (ioctl(sock, SIOCGIFFLAGS, (caddr_t) &ifr_tmp) < 0)
                    252: #endif
                    253:            ifr_tmp = *ifr;
                    254:
                    255:        /* Skip interfaces marked "down" and "loopback". */
                    256:        if (!(ifr_tmp.ifr_flags & IFF_UP) || (ifr_tmp.ifr_flags & IFF_LOOPBACK))
                    257:                continue;
                    258:
                    259:        sin = (struct sockaddr_in *) &ifr->ifr_addr;
                    260:        interfaces[num_interfaces].addr.s_addr = sin->sin_addr.s_addr;
                    261:
                    262:        /* Stash the name of the interface we saved. */
                    263:        previfname = ifr->ifr_name;
                    264:
                    265:        /* Get the netmask. */
                    266:        (void) memset(&ifr_tmp, 0, sizeof(ifr_tmp));
                    267:        strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
                    268: #ifdef SIOCGIFNETMASK
                    269: #ifdef _ISC
                    270:        STRSET(SIOCGIFNETMASK, (caddr_t) &ifr_tmp, sizeof(ifr_tmp));
                    271:        if (ioctl(sock, I_STR, (caddr_t) &strioctl) == 0) {
                    272: #else
                    273:        if (ioctl(sock, SIOCGIFNETMASK, (caddr_t) &ifr_tmp) == 0) {
                    274: #endif /* _ISC */
                    275:            sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
                    276:
                    277:            interfaces[num_interfaces].netmask.s_addr = sin->sin_addr.s_addr;
                    278:        } else {
                    279: #else
                    280:        {
                    281: #endif /* SIOCGIFNETMASK */
                    282:            if (IN_CLASSC(interfaces[num_interfaces].addr.s_addr))
                    283:                interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSC_NET);
                    284:            else if (IN_CLASSB(interfaces[num_interfaces].addr.s_addr))
                    285:                interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSB_NET);
                    286:            else
                    287:                interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSA_NET);
                    288:        }
                    289:
                    290:        /* Only now can we be sure it was a good/interesting interface. */
                    291:        num_interfaces++;
                    292:     }
                    293:
                    294:     /* If the expected size < real size, realloc the array. */
                    295:     if (n != num_interfaces) {
                    296:        if (num_interfaces != 0)
1.4       millert   297:            interfaces = (struct interface *) erealloc3(interfaces,
                    298:                num_interfaces, sizeof(struct interface));
1.1       millert   299:        else
                    300:            free(interfaces);
                    301:     }
                    302:     free(ifconf_buf);
                    303:     (void) close(sock);
                    304: }
                    305:
                    306: #else /* !SIOCGIFCONF || STUB_LOAD_INTERFACES */
                    307:
                    308: /*
                    309:  * Stub function for those without SIOCGIFCONF
                    310:  */
                    311: void
                    312: load_interfaces()
                    313: {
                    314:     return;
                    315: }
                    316:
                    317: #endif /* SIOCGIFCONF && !STUB_LOAD_INTERFACES */
1.2       millert   318:
                    319: void
                    320: dump_interfaces()
                    321: {
                    322:     int i;
                    323:
                    324:     puts("Local IP address and netmask pairs:");
                    325:     for (i = 0; i < num_interfaces; i++)
                    326:        printf("\t%s / 0x%x\n", inet_ntoa(interfaces[i].addr),
1.5     ! millert   327:            (unsigned int)ntohl(interfaces[i].netmask.s_addr));
1.2       millert   328: }