Debian Codegraph @

src/OVAL/results/oval_cmp_ip_address.c

raw symbols SCIP
1 /**
2 * @file oval_resultTest.c
3 * \brief Open Vulnerability and Assessment Language
4 *
5 * See more details at http://oval.mitre.org/
6 */
7
8 /*
9 * Copyright 2009--2013 Red Hat Inc., Durham, North Carolina.
10 * All Rights Reserved.
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 *
26 * Authors:
27 * Tomas Heinrich <[email protected]>
28 * Šimon Lukašík
29 */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdint.h>
39 #if defined(OS_WINDOWS)
40 #include <winsock2.h>
41 #include <ws2tcpip.h>
42 #elif defined(OS_FREEBSD)
43 #include <arpa/inet.h>
44 #include <netinet/in.h>
45 #include <sys/socket.h>
46 #else
47 #include <arpa/inet.h>
48 #include <sys/socket.h>
49 #endif
50
51 #include "oval_definitions.h"
52 #include "oval_types.h"
53
54 #include "common/util.h"
55 #include "common/debug_priv.h"
56 #include "oval_cmp_ip_address_impl.h"
57
58 static inline void ipv4addr_mask(struct in_addr *ip_addr, uint32_t netmask);
59 static inline void ipv6addr_mask(struct in6_addr *addr, int prefix_len);
60 static inline int ipv4addr_parse(const char *oval_ipv4_string, uint32_t *netmask_out, struct in_addr *ip_out);
61 static inline int ipv6addr_parse(const char *oval_ipv6_string, uint32_t *len_out, struct in6_addr *ip_out);
62
63
64 static inline int ipaddr_cmp(int af, const void *addr1, const void *addr2)
65 {
66 if (af == AF_INET)
67 return memcmp(addr1, addr2, sizeof(struct in_addr));
68 else {
69 assert(af == AF_INET6);
70 return memcmp(addr1, addr2, sizeof(struct in6_addr));
71 }
72 }
73
74 static inline void ipaddr_mask(int af, const void *ip_addr, uint32_t mask)
75 {
76 if (af == AF_INET)
77 ipv4addr_mask((struct in_addr *) ip_addr, mask);
78 else if (af == AF_INET6)
79 ipv6addr_mask((struct in6_addr *) ip_addr, mask);
80 else
81 assert(false);
82
83 }
84
85 static inline int ipaddr_parse(int af, const char *oval_ip_string, uint32_t *mask_out, void * ip_out)
86 {
87 if (af == AF_INET)
88 return ipv4addr_parse(oval_ip_string, mask_out, ip_out);
89 assert (af == AF_INET6);
90 return ipv6addr_parse(oval_ip_string, mask_out, ip_out);
91 }
92
93 oval_result_t oval_ipaddr_cmp(int af, const char *s1, const char *s2, oval_operation_t op)
94 {
95 oval_result_t result = OVAL_RESULT_ERROR;
96 uint32_t mask1 = 0, mask2 = 0;
97 char addr1[INET6_ADDRSTRLEN];
98 char addr2[INET6_ADDRSTRLEN];
99
100 if (ipaddr_parse(af, s1, &mask1, &addr1) || ipaddr_parse(af, s2, &mask2, &addr2)) {
101 return result;
102 }
103
104 switch (op) {
105 case OVAL_OPERATION_EQUALS:
106 if (!ipaddr_cmp(af, &addr1, &addr2) && mask1 == mask2)
107 result = OVAL_RESULT_TRUE;
108 else
109 result = OVAL_RESULT_FALSE;
110 break;
111 case OVAL_OPERATION_NOT_EQUAL:
112 if (ipaddr_cmp(af, &addr1, &addr2) || mask1 != mask2)
113 result = OVAL_RESULT_TRUE;
114 else
115 result = OVAL_RESULT_FALSE;
116 break;
117 case OVAL_OPERATION_SUBSET_OF:
118 /* This asserts that every IP address in the set of IP addresses
119 * on the system (add2, mask2) must be present in the set of IP
120 * addresses defined in the stated entity (addr1, mask1). */
121 if (mask1 > mask2) {
122 /* The bigger the netmask (IPv4) or prefix-length (IPv6) is
123 * the less IP addresses there are in the range. */
124 result = OVAL_RESULT_FALSE;
125 break;
126 }
127
128 /* Otherwise, compare the first bits defined by mask1 */
129 ipaddr_mask(af, &addr1, mask1);
130 ipaddr_mask(af, &addr2, mask1);
131 if (ipaddr_cmp(af, &addr1, &addr2) == 0)
132 result = OVAL_RESULT_TRUE;
133 else
134 result = OVAL_RESULT_FALSE;
135 break;
136 case OVAL_OPERATION_GREATER_THAN:
137 if (mask1 != mask2) {
138 return OVAL_RESULT_ERROR;
139 }
140 ipaddr_mask(af, &addr1, mask1);
141 ipaddr_mask(af, &addr2, mask2);
142 if (ipaddr_cmp(af, &addr1, &addr2) < 0)
143 result = OVAL_RESULT_TRUE;
144 else
145 result = OVAL_RESULT_FALSE;
146 break;
147 case OVAL_OPERATION_GREATER_THAN_OR_EQUAL:
148 if (mask1 != mask2) {
149 return OVAL_RESULT_ERROR;
150 }
151 ipaddr_mask(af, &addr1, mask1);
152 ipaddr_mask(af, &addr2, mask2);
153 if (ipaddr_cmp(af, &addr1, &addr2) <= 0)
154 result = OVAL_RESULT_TRUE;
155 else
156 result = OVAL_RESULT_FALSE;
157 break;
158 case OVAL_OPERATION_SUPERSET_OF:
159 /* This asserts that every IP address in the set of IP addresses defined in
160 * the stated entity (addr1, mask1) is present in the set of IP addresses
161 * on the system. (addr2, mask2). */
162 if (mask1 < mask2) {
163 /* The smaller the netmask (IPv4) or prefix-length (IPv6) is
164 * the more IP addresses there are in the range */
165 result = OVAL_RESULT_FALSE;
166 break;
167 }
168
169 /* Otherwise, compare the first bits defined by mask2 */
170 ipaddr_mask(af, &addr1, mask2);
171 ipaddr_mask(af, &addr2, mask2);
172 if (ipaddr_cmp(af, &addr1, &addr2) == 0)
173 result = OVAL_RESULT_TRUE;
174 else
175 result = OVAL_RESULT_FALSE;
176 break;
177 case OVAL_OPERATION_LESS_THAN:
178 if (mask1 != mask2) {
179 return OVAL_RESULT_ERROR;
180 }
181 ipaddr_mask(af, &addr1, mask1);
182 ipaddr_mask(af, &addr2, mask2);
183 if (ipaddr_cmp(af, &addr1, &addr2) > 0)
184 result = OVAL_RESULT_TRUE;
185 else
186 result = OVAL_RESULT_FALSE;
187 break;
188 case OVAL_OPERATION_LESS_THAN_OR_EQUAL:
189 if (mask1 != mask2) {
190 return OVAL_RESULT_ERROR;
191 }
192 ipaddr_mask(af, &addr1, mask1);
193 ipaddr_mask(af, &addr2, mask2);
194 if (ipaddr_cmp(af, &addr1, &addr2) >= 0)
195 result = OVAL_RESULT_TRUE;
196 else
197 result = OVAL_RESULT_FALSE;
198 break;
199 default:
200 dE("Unexpected compare operation: %d.", op);
201 assert(false);
202 }
203
204 return result;
205 }
206
207 static inline int ipv4addr_parse(const char *oval_ipv4_string, uint32_t *netmask_out, struct in_addr *ip_out)
208 {
209 char *s, *pfx;
210 int result = -1;
211
212 s = strdup(oval_ipv4_string);
213 pfx = strchr(s, '/');
214 if (pfx) {
215 int cnt;
216 unsigned char nm[4];
217
218 *pfx++ = '\0';
219 cnt = sscanf(pfx, "%hhu.%hhu.%hhu.%hhu", &nm[0], &nm[1], &nm[2], &nm[3]);
220 if (cnt == 4) { /* netmask */
221 *netmask_out = (nm[0] << 24) + (nm[1] << 16) + (nm[2] << 8) + nm[3];
222 } else if (cnt == 1 && nm[0] <= 32) { /* prefix */
223 *netmask_out = (~0u) << (32u - nm[0]);
224 } else {
225 dW("Invalid prefix or netmask.");
226 free(s);
227 return -1;
228 }
229 } else {
230 *netmask_out = ~0;
231 }
232
233 if (inet_pton(AF_INET, s, ip_out) <= 0)
234 dW("inet_pton() failed.");
235 else
236 result = 0;
237
238 free(s);
239 return result;
240 }
241
242 static inline void ipv4addr_mask(struct in_addr *ip_addr, uint32_t netmask)
243 {
244 ip_addr->s_addr &= htonl(netmask);
245 }
246
247 static inline int ipv6addr_parse(const char *oval_ipv6_string, uint32_t *len_out, struct in6_addr *ip_out)
248 {
249 char *s, *pfx;
250 int result = -1;
251
252 s = strdup(oval_ipv6_string);
253 pfx = strchr(s, '/');
254 if (pfx) {
255 *pfx++ = '\0';
256 *len_out = strtol(pfx, NULL, 10);
257 } else {
258 *len_out = 128;
259 }
260
261 if (inet_pton(AF_INET6, s, ip_out) <= 0)
262 dW("inet_pton() failed.");
263 else
264 result = 0;
265
266 free(s);
267 return result;
268 }
269
270 static inline void ipv6addr_mask(struct in6_addr *addr, int prefix_len)
271 {
272 assert(128 >= prefix_len);
273
274 uint8_t mask = (~0u) << (8u - (prefix_len % 8));
275
276 /* First n (prefix_len/8 - 1) bytes are left untouched. */
277 for (unsigned int i = prefix_len/8; i < 128/8; i++)
278 {
279 /* The (n+1) byte is masked according to the prefix_len */
280 addr->s6_addr[i] &= mask;
281 /* The rest will be zeroed. */
282 mask = 0;
283 }
284 }