Imported Upstream version 1.8.7
[debian/sudo] / plugins / sudoers / interfaces.c
1 /*
2  * Copyright (c) 2010-2012 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 <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
54 static struct interface *interfaces;
55
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;
65     debug_decl(set_interfaces, SUDO_DEBUG_NETIF)
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. */
75         ifp = ecalloc(1, sizeof(*ifp));
76         if (strchr(addr, ':')) {
77             /* IPv6 */
78 #ifdef HAVE_STRUCT_IN6_ADDR
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;
90             ifp->addr.ip4.s_addr = inet_addr(addr);
91             ifp->netmask.ip4.s_addr = inet_addr(mask);
92             if (ifp->addr.ip4.s_addr == INADDR_NONE ||
93                 ifp->netmask.ip4.s_addr == INADDR_NONE) {
94                 efree(ifp);
95                 continue;
96             }
97         }
98         ifp->next = interfaces;
99         interfaces = ifp;
100     }
101     efree(addrinfo);
102     debug_return;
103 }
104
105 struct interface *
106 get_interfaces(void)
107 {
108     return interfaces;
109 }
110
111 void
112 dump_interfaces(const char *ai)
113 {
114     char *cp, *addrinfo;
115     debug_decl(set_interfaces, SUDO_DEBUG_NETIF)
116
117     addrinfo = estrdup(ai);
118
119     sudo_printf(SUDO_CONV_INFO_MSG, _("Local IP address and netmask pairs:\n"));
120     for (cp = strtok(addrinfo, " \t"); cp != NULL; cp = strtok(NULL, " \t"))
121         sudo_printf(SUDO_CONV_INFO_MSG, "\t%s\n", cp);
122
123     efree(addrinfo);
124     debug_return;
125 }