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

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

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