Annotation of embedaddon/sudo/plugins/sudoers/interfaces.c, revision 1.1.1.4

1.1       misho       1: /*
1.1.1.4 ! misho       2:  * Copyright (c) 2010-2013 Todd C. Miller <Todd.Miller@courtesan.com>
1.1       misho       3:  *
                      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.
                     15:  */
                     16: 
                     17: #include <config.h>
                     18: 
                     19: #include <sys/types.h>
                     20: #include <sys/socket.h>
                     21: #include <stdio.h>
                     22: #ifdef STDC_HEADERS
                     23: # include <stdlib.h>
                     24: # include <stddef.h>
                     25: #else
                     26: # ifdef HAVE_STDLIB_H
                     27: #  include <stdlib.h>
                     28: # endif
                     29: #endif /* STDC_HEADERS */
                     30: #ifdef HAVE_STRING_H
                     31: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
                     32: #  include <memory.h>
                     33: # endif
                     34: # include <string.h>
                     35: #endif /* HAVE_STRING_H */
                     36: #ifdef HAVE_STRINGS_H
                     37: # include <strings.h>
                     38: #endif /* HAVE_STRINGS_H */
                     39: #ifdef HAVE_UNISTD_H
                     40: # include <unistd.h>
                     41: #endif /* HAVE_UNISTD_H */
                     42: #include <netinet/in.h>  
                     43: #include <arpa/inet.h>
                     44: #include <netdb.h>
                     45: #include <errno.h>
                     46: 
                     47: #include "sudoers.h"
                     48: #include "interfaces.h"
                     49: 
                     50: #ifndef INADDR_NONE
                     51: # define INADDR_NONE ((unsigned int)-1)
                     52: #endif
                     53: 
1.1.1.4 ! misho      54: static struct interface_list interfaces;
1.1.1.3   misho      55: 
1.1       misho      56: /*
                     57:  * Parse a space-delimited list of IP address/netmask pairs and
                     58:  * store in a list of interface structures.
                     59:  */
                     60: void
                     61: set_interfaces(const char *ai)
                     62: {
                     63:     char *addrinfo, *addr, *mask;
                     64:     struct interface *ifp;
1.1.1.2   misho      65:     debug_decl(set_interfaces, SUDO_DEBUG_NETIF)
1.1       misho      66: 
                     67:     addrinfo = estrdup(ai);
                     68:     for (addr = strtok(addrinfo, " \t"); addr != NULL; addr = strtok(NULL, " \t")) {
                     69:        /* Separate addr and mask. */
                     70:        if ((mask = strchr(addr, '/')) == NULL)
                     71:            continue;
                     72:        *mask++ = '\0';
                     73: 
                     74:        /* Parse addr and store in list. */
1.1.1.2   misho      75:        ifp = ecalloc(1, sizeof(*ifp));
1.1       misho      76:        if (strchr(addr, ':')) {
                     77:            /* IPv6 */
1.1.1.2   misho      78: #ifdef HAVE_STRUCT_IN6_ADDR
1.1       misho      79:            ifp->family = AF_INET6;
                     80:            if (inet_pton(AF_INET6, addr, &ifp->addr.ip6) != 1 ||
                     81:                inet_pton(AF_INET6, mask, &ifp->netmask.ip6) != 1)
                     82: #endif
                     83:            {
                     84:                efree(ifp);
                     85:                continue;
                     86:            }
                     87:        } else {
                     88:            /* IPv4 */
                     89:            ifp->family = AF_INET;
1.1.1.4 ! misho      90:            if (inet_pton(AF_INET, addr, &ifp->addr.ip4) != 1 ||
        !            91:                inet_pton(AF_INET, mask, &ifp->netmask.ip4) != 1) {
1.1       misho      92:                efree(ifp);
                     93:                continue;
                     94:            }
                     95:        }
1.1.1.4 ! misho      96:        SLIST_INSERT_HEAD(&interfaces, ifp, entries);
1.1       misho      97:     }
                     98:     efree(addrinfo);
1.1.1.2   misho      99:     debug_return;
1.1       misho     100: }
                    101: 
1.1.1.4 ! misho     102: struct interface_list *
1.1.1.3   misho     103: get_interfaces(void)
                    104: {
1.1.1.4 ! misho     105:     return &interfaces;
1.1.1.3   misho     106: }
                    107: 
1.1       misho     108: void
                    109: dump_interfaces(const char *ai)
                    110: {
                    111:     char *cp, *addrinfo;
1.1.1.2   misho     112:     debug_decl(set_interfaces, SUDO_DEBUG_NETIF)
1.1       misho     113: 
                    114:     addrinfo = estrdup(ai);
                    115: 
                    116:     sudo_printf(SUDO_CONV_INFO_MSG, _("Local IP address and netmask pairs:\n"));
                    117:     for (cp = strtok(addrinfo, " \t"); cp != NULL; cp = strtok(NULL, " \t"))
                    118:        sudo_printf(SUDO_CONV_INFO_MSG, "\t%s\n", cp);
                    119: 
                    120:     efree(addrinfo);
1.1.1.2   misho     121:     debug_return;
1.1       misho     122: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>