Imported Upstream version 1.8.2
[debian/sudo] / plugins / sudoers / interfaces.c
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;
64
65     addrinfo = estrdup(ai);
66     for (addr = strtok(addrinfo, " \t"); addr != NULL; addr = strtok(NULL, " \t")) {
67         /* Separate addr and mask. */
68         if ((mask = strchr(addr, '/')) == NULL)
69             continue;
70         *mask++ = '\0';
71
72         /* Parse addr and store in list. */
73         ifp = emalloc(sizeof(*ifp));
74         if (strchr(addr, ':')) {
75             /* IPv6 */
76 #ifdef HAVE_IN6_ADDR
77             ifp->family = AF_INET6;
78             if (inet_pton(AF_INET6, addr, &ifp->addr.ip6) != 1 ||
79                 inet_pton(AF_INET6, mask, &ifp->netmask.ip6) != 1)
80 #endif
81             {
82                 efree(ifp);
83                 continue;
84             }
85         } else {
86             /* IPv4 */
87             ifp->family = AF_INET;
88             ifp->addr.ip4.s_addr = inet_addr(addr);
89             ifp->netmask.ip4.s_addr = inet_addr(mask);
90             if (ifp->addr.ip4.s_addr == INADDR_NONE ||
91                 ifp->netmask.ip4.s_addr == INADDR_NONE) {
92                 efree(ifp);
93                 continue;
94             }
95         }
96         ifp->next = interfaces;
97         interfaces = ifp;
98     }
99     efree(addrinfo);
100 }
101
102 void
103 dump_interfaces(const char *ai)
104 {
105     char *cp, *addrinfo;
106
107     addrinfo = estrdup(ai);
108
109     sudo_printf(SUDO_CONV_INFO_MSG, _("Local IP address and netmask pairs:\n"));
110     for (cp = strtok(addrinfo, " \t"); cp != NULL; cp = strtok(NULL, " \t"))
111         sudo_printf(SUDO_CONV_INFO_MSG, "\t%s\n", cp);
112
113     efree(addrinfo);
114 }