]> granicus.if.org Git - sudo/blob - plugins/sudoers/match_addr.c
4310e05ddc52c52f7bc4e85d74c0c03f08538b8c
[sudo] / plugins / sudoers / match_addr.c
1 /*
2  * Copyright (c) 1996, 1998-2005, 2007-2015
3  *      Todd C. Miller <Todd.Miller@sudo.ws>
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  *
17  * Sponsored in part by the Defense Advanced Research Projects
18  * Agency (DARPA) and Air Force Research Laboratory, Air Force
19  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
20  */
21
22 /*
23  * This is an open source non-commercial project. Dear PVS-Studio, please check it.
24  * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
25  */
26
27 #include <config.h>
28
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #ifdef HAVE_STRING_H
34 # include <string.h>
35 #endif /* HAVE_STRING_H */
36 #ifdef HAVE_STRINGS_H
37 # include <strings.h>
38 #endif /* HAVE_STRINGS_H */
39 #include <unistd.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42 #ifdef NEED_RESOLV_H
43 # include <arpa/nameser.h>
44 # include <resolv.h>
45 #endif /* NEED_RESOLV_H */
46
47 #include "sudoers.h"
48 #include "interfaces.h"
49
50 static bool
51 addr_matches_if(const char *n)
52 {
53     union sudo_in_addr_un addr;
54     struct interface *ifp;
55 #ifdef HAVE_STRUCT_IN6_ADDR
56     unsigned int j;
57 #endif
58     unsigned int family;
59     debug_decl(addr_matches_if, SUDOERS_DEBUG_MATCH)
60
61 #ifdef HAVE_STRUCT_IN6_ADDR
62     if (inet_pton(AF_INET6, n, &addr.ip6) == 1) {
63         family = AF_INET6;
64     } else
65 #endif /* HAVE_STRUCT_IN6_ADDR */
66     if (inet_pton(AF_INET, n, &addr.ip4) == 1) {
67         family = AF_INET;
68     } else {
69         debug_return_bool(false);
70     }
71
72     SLIST_FOREACH(ifp, get_interfaces(), entries) {
73         if (ifp->family != family)
74             continue;
75         switch (family) {
76             case AF_INET:
77                 if (ifp->addr.ip4.s_addr == addr.ip4.s_addr ||
78                     (ifp->addr.ip4.s_addr & ifp->netmask.ip4.s_addr)
79                     == addr.ip4.s_addr)
80                     debug_return_bool(true);
81                 break;
82 #ifdef HAVE_STRUCT_IN6_ADDR
83             case AF_INET6:
84                 if (memcmp(ifp->addr.ip6.s6_addr, addr.ip6.s6_addr,
85                     sizeof(addr.ip6.s6_addr)) == 0)
86                     debug_return_bool(true);
87                 for (j = 0; j < sizeof(addr.ip6.s6_addr); j++) {
88                     if ((ifp->addr.ip6.s6_addr[j] & ifp->netmask.ip6.s6_addr[j]) != addr.ip6.s6_addr[j])
89                         break;
90                 }
91                 if (j == sizeof(addr.ip6.s6_addr))
92                     debug_return_bool(true);
93                 break;
94 #endif /* HAVE_STRUCT_IN6_ADDR */
95         }
96     }
97
98     debug_return_bool(false);
99 }
100
101 static bool
102 addr_matches_if_netmask(const char *n, const char *m)
103 {
104     unsigned int i;
105     union sudo_in_addr_un addr, mask;
106     struct interface *ifp;
107 #ifdef HAVE_STRUCT_IN6_ADDR
108     unsigned int j;
109 #endif
110     unsigned int family;
111     const char *errstr;
112     debug_decl(addr_matches_if, SUDOERS_DEBUG_MATCH)
113
114 #ifdef HAVE_STRUCT_IN6_ADDR
115     if (inet_pton(AF_INET6, n, &addr.ip6) == 1)
116         family = AF_INET6;
117     else
118 #endif /* HAVE_STRUCT_IN6_ADDR */
119     if (inet_pton(AF_INET, n, &addr.ip4) == 1) {
120         family = AF_INET;
121     } else {
122         debug_return_bool(false);
123     }
124
125     if (family == AF_INET) {
126         if (strchr(m, '.')) {
127             if (inet_pton(AF_INET, m, &mask.ip4) != 1) {
128                 sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
129                     "IPv4 netmask %s: %s", m, "invalid value");
130                 debug_return_bool(false);
131             }
132         } else {
133             i = strtonum(m, 1, 32, &errstr);
134             if (errstr != NULL) {
135                 sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
136                     "IPv4 netmask %s: %s", m, errstr);
137                 debug_return_bool(false);
138             }
139             mask.ip4.s_addr = htonl(0xffffffffU << (32 - i));
140         }
141         addr.ip4.s_addr &= mask.ip4.s_addr;
142     }
143 #ifdef HAVE_STRUCT_IN6_ADDR
144     else {
145         if (inet_pton(AF_INET6, m, &mask.ip6) != 1) {
146             j = strtonum(m, 1, 128, &errstr);
147             if (errstr != NULL) {
148                 sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
149                     "IPv6 netmask %s: %s", m, errstr);
150                 debug_return_bool(false);
151             }
152             for (i = 0; i < sizeof(addr.ip6.s6_addr); i++) {
153                 if (j < i * 8)
154                     mask.ip6.s6_addr[i] = 0;
155                 else if (i * 8 + 8 <= j)
156                     mask.ip6.s6_addr[i] = 0xff;
157                 else
158                     mask.ip6.s6_addr[i] = 0xff00 >> (j - i * 8);
159                 addr.ip6.s6_addr[i] &= mask.ip6.s6_addr[i];
160             }
161         }
162     }
163 #endif /* HAVE_STRUCT_IN6_ADDR */
164
165     SLIST_FOREACH(ifp, get_interfaces(), entries) {
166         if (ifp->family != family)
167             continue;
168         switch (family) {
169             case AF_INET:
170                 if ((ifp->addr.ip4.s_addr & mask.ip4.s_addr) == addr.ip4.s_addr)
171                     debug_return_bool(true);
172                 break;
173 #ifdef HAVE_STRUCT_IN6_ADDR
174             case AF_INET6:
175                 for (j = 0; j < sizeof(addr.ip6.s6_addr); j++) {
176                     if ((ifp->addr.ip6.s6_addr[j] & mask.ip6.s6_addr[j]) != addr.ip6.s6_addr[j])
177                         break;
178                 }
179                 if (j == sizeof(addr.ip6.s6_addr))
180                     debug_return_bool(true);
181                 break;
182 #endif /* HAVE_STRUCT_IN6_ADDR */
183         }
184     }
185
186     debug_return_bool(false);
187 }
188
189 /*
190  * Returns true if "n" is one of our ip addresses or if
191  * "n" is a network that we are on, else returns false.
192  */
193 bool
194 addr_matches(char *n)
195 {
196     char *m;
197     bool rc;
198     debug_decl(addr_matches, SUDOERS_DEBUG_MATCH)
199
200     /* If there's an explicit netmask, use it. */
201     if ((m = strchr(n, '/'))) {
202         *m++ = '\0';
203         rc = addr_matches_if_netmask(n, m);
204         *(m - 1) = '/';
205     } else
206         rc = addr_matches_if(n);
207
208     sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
209         "IP address %s matches local host: %s", n, rc ? "true" : "false");
210     debug_return_bool(rc);
211 }