Imported Upstream version 1.8.6p8
[debian/sudo] / plugins / sudoers / match_addr.c
1 /*
2  * Copyright (c) 1996, 1998-2005, 2007-2011
3  *      Todd C. Miller <Todd.Miller@courtesan.com>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
17  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18  *
19  * Sponsored in part by the Defense Advanced Research Projects
20  * Agency (DARPA) and Air Force Research Laboratory, Air Force
21  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22  */
23
24 #include <config.h>
25
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <stdio.h>
29 #ifdef STDC_HEADERS
30 # include <stdlib.h>
31 # include <stddef.h>
32 #else
33 # ifdef HAVE_STDLIB_H
34 #  include <stdlib.h>
35 # endif
36 #endif /* STDC_HEADERS */
37 #ifdef HAVE_STRING_H
38 # include <string.h>
39 #endif /* HAVE_STRING_H */
40 #ifdef HAVE_STRINGS_H
41 # include <strings.h>
42 #endif /* HAVE_STRINGS_H */
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif /* HAVE_UNISTD_H */
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <netdb.h>
49
50 #include "sudoers.h"
51 #include "interfaces.h"
52
53 static bool
54 addr_matches_if(char *n)
55 {
56     union sudo_in_addr_un addr;
57     struct interface *ifp;
58 #ifdef HAVE_STRUCT_IN6_ADDR
59     int j;
60 #endif
61     int family;
62     debug_decl(addr_matches_if, SUDO_DEBUG_MATCH)
63
64 #ifdef HAVE_STRUCT_IN6_ADDR
65     if (inet_pton(AF_INET6, n, &addr.ip6) > 0) {
66         family = AF_INET6;
67     } else
68 #endif /* HAVE_STRUCT_IN6_ADDR */
69     {
70         family = AF_INET;
71         addr.ip4.s_addr = inet_addr(n);
72     }
73
74     for (ifp = interfaces; ifp != NULL; ifp = ifp->next) {
75         if (ifp->family != family)
76             continue;
77         switch (family) {
78             case AF_INET:
79                 if (ifp->addr.ip4.s_addr == addr.ip4.s_addr ||
80                     (ifp->addr.ip4.s_addr & ifp->netmask.ip4.s_addr)
81                     == addr.ip4.s_addr)
82                     debug_return_bool(true);
83                 break;
84 #ifdef HAVE_STRUCT_IN6_ADDR
85             case AF_INET6:
86                 if (memcmp(ifp->addr.ip6.s6_addr, addr.ip6.s6_addr,
87                     sizeof(addr.ip6.s6_addr)) == 0)
88                     debug_return_bool(true);
89                 for (j = 0; j < sizeof(addr.ip6.s6_addr); j++) {
90                     if ((ifp->addr.ip6.s6_addr[j] & ifp->netmask.ip6.s6_addr[j]) != addr.ip6.s6_addr[j])
91                         break;
92                 }
93                 if (j == sizeof(addr.ip6.s6_addr))
94                     debug_return_bool(true);
95                 break;
96 #endif /* HAVE_STRUCT_IN6_ADDR */
97         }
98     }
99
100     debug_return_bool(false);
101 }
102
103 static bool
104 addr_matches_if_netmask(char *n, char *m)
105 {
106     int i;
107     union sudo_in_addr_un addr, mask;
108     struct interface *ifp;
109 #ifdef HAVE_STRUCT_IN6_ADDR
110     int j;
111 #endif
112     int family;
113     debug_decl(addr_matches_if, SUDO_DEBUG_MATCH)
114
115 #ifdef HAVE_STRUCT_IN6_ADDR
116     if (inet_pton(AF_INET6, n, &addr.ip6) > 0)
117         family = AF_INET6;
118     else
119 #endif /* HAVE_STRUCT_IN6_ADDR */
120     {
121         family = AF_INET;
122         addr.ip4.s_addr = inet_addr(n);
123     }
124
125     if (family == AF_INET) {
126         if (strchr(m, '.')) {
127             mask.ip4.s_addr = inet_addr(m);
128         } else {
129             i = atoi(m);
130             if (i == 0)
131                 mask.ip4.s_addr = 0;
132             else if (i == 32)
133                 mask.ip4.s_addr = 0xffffffff;
134             else
135                 mask.ip4.s_addr = 0xffffffff - (1 << (32 - i)) + 1;
136             mask.ip4.s_addr = htonl(mask.ip4.s_addr);
137         }
138         addr.ip4.s_addr &= mask.ip4.s_addr;
139     }
140 #ifdef HAVE_STRUCT_IN6_ADDR
141     else {
142         if (inet_pton(AF_INET6, m, &mask.ip6) <= 0) {
143             j = atoi(m);
144             for (i = 0; i < sizeof(addr.ip6.s6_addr); i++) {
145                 if (j < i * 8)
146                     mask.ip6.s6_addr[i] = 0;
147                 else if (i * 8 + 8 <= j)
148                     mask.ip6.s6_addr[i] = 0xff;
149                 else
150                     mask.ip6.s6_addr[i] = 0xff00 >> (j - i * 8);
151                 addr.ip6.s6_addr[i] &= mask.ip6.s6_addr[i];
152             }
153         }
154     }
155 #endif /* HAVE_STRUCT_IN6_ADDR */
156
157     for (ifp = interfaces; ifp != NULL; ifp = ifp->next) {
158         if (ifp->family != family)
159             continue;
160         switch (family) {
161             case AF_INET:
162                 if ((ifp->addr.ip4.s_addr & mask.ip4.s_addr) == addr.ip4.s_addr)
163                     debug_return_bool(true);
164                 break;
165 #ifdef HAVE_STRUCT_IN6_ADDR
166             case AF_INET6:
167                 for (j = 0; j < sizeof(addr.ip6.s6_addr); j++) {
168                     if ((ifp->addr.ip6.s6_addr[j] & mask.ip6.s6_addr[j]) != addr.ip6.s6_addr[j])
169                         break;
170                 }
171                 if (j == sizeof(addr.ip6.s6_addr))
172                     debug_return_bool(true);
173                 break;
174 #endif /* HAVE_STRUCT_IN6_ADDR */
175         }
176     }
177
178     debug_return_bool(false);
179 }
180
181 /*
182  * Returns true if "n" is one of our ip addresses or if
183  * "n" is a network that we are on, else returns false.
184  */
185 bool
186 addr_matches(char *n)
187 {
188     char *m;
189     bool retval;
190     debug_decl(addr_matches, SUDO_DEBUG_MATCH)
191
192     /* If there's an explicit netmask, use it. */
193     if ((m = strchr(n, '/'))) {
194         *m++ = '\0';
195         retval = addr_matches_if_netmask(n, m);
196         *(m - 1) = '/';
197     } else
198         retval = addr_matches_if(n);
199
200     debug_return_bool(retval);
201 }