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_Hcxx . . $ `<built-in>:409:9`!
32 #include <config.h>cxx . . $ `<file>/build/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"cxx . . $ `<file>/src/OVAL/public/oval_definitions.h`/
52 #include "oval_types.h"cxx . . $ `<file>/src/OVAL/public/oval_types.h`/
53
54 #include "common/util.h"cxx . . $ `<file>/src/common/util.h`/
55 #include "common/debug_priv.h"cxx . . $ `<file>/src/common/debug_priv.h`/
56 #include "oval_cmp_ip_address_impl.h"cxx . . $ `<file>/src/OVAL/results/oval_cmp_ip_address_impl.h`/
57
58 static inline void ipv4addr_maskcxx . . $ ipv4addr_mask(7cb0f3b2dce9e5d8).(struct in_addrcxx . . $ in_addr# *ip_addr, uint32_tcxx . . $ uint32_t# netmask);
59 static inline void ipv6addr_maskcxx . . $ ipv6addr_mask(4fd71685b6320a2).(struct in6_addrcxx . . $ in6_addr# *addr, int prefix_len);
60 static inline int ipv4addr_parsecxx . . $ ipv4addr_parse(ceb151ea223f6e2a).(const char *oval_ipv4_string, uint32_tcxx . . $ uint32_t# *netmask_out, struct in_addrcxx . . $ in_addr# *ip_out);
61 static inline int ipv6addr_parsecxx . . $ ipv6addr_parse(9881f6607b848b71).(const char *oval_ipv6_string, uint32_tcxx . . $ uint32_t# *len_out, struct in6_addrcxx . . $ in6_addr# *ip_out);
62
63
64 static inline int ipaddr_cmpcxx . . $ ipaddr_cmp(860aed8b209fc08f).(int af, const void *addr1, const void *addr2)
65 {
66 if (af == AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!)
67 return memcmpcxx . . $ memcmp(36f949851efa3055).(addr1, addr2, sizeof(struct in_addrcxx . . $ in_addr#));
68 else {
69 assertcxx . . $ `/usr/include/assert.h:50:10`!(af == AF_INET6);
70 return memcmpcxx . . $ memcmp(36f949851efa3055).(addr1, addr2, sizeof(struct in6_addrcxx . . $ in6_addr#));
71 }
72 }
73
74 static inline void ipaddr_maskcxx . . $ ipaddr_mask(236b8fea641ffdaa).(int af, const void *ip_addr, uint32_tcxx . . $ uint32_t# mask)
75 {
76 if (af == AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!)
77 ipv4addr_maskcxx . . $ ipv4addr_mask(7cb0f3b2dce9e5d8).((struct in_addrcxx . . $ in_addr# *) ip_addr, mask);
78 else if (af == AF_INET6cxx . . $ `<external>/8962547315054503424/socket.h:105:9`!)
79 ipv6addr_maskcxx . . $ ipv6addr_mask(4fd71685b6320a2).((struct in6_addrcxx . . $ in6_addr# *) ip_addr, mask);
80 else
81 assertcxx . . $ `/usr/include/assert.h:50:10`!(false);
82
83 }
84
85 static inline int ipaddr_parsecxx . . $ ipaddr_parse(35cd60eae54dae5b).(int af, const char *oval_ip_string, uint32_tcxx . . $ uint32_t# *mask_out, void * ip_out)
86 {
87 if (af == AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!)
88 return ipv4addr_parsecxx . . $ ipv4addr_parse(ceb151ea223f6e2a).(oval_ip_string, mask_out, ip_out);
89 assertcxx . . $ `/usr/include/assert.h:50:10`! (af == AF_INET6);
90 return ipv6addr_parsecxx . . $ ipv6addr_parse(9881f6607b848b71).(oval_ip_string, mask_out, ip_out);
91 }
92
93 oval_result_tcxx . . $ oval_result_t# oval_ipaddr_cmpcxx . . $ oval_ipaddr_cmp(70c1ad763aee40a3).(int af, const char *s1, const char *s2, oval_operation_tcxx . . $ oval_operation_t# op)
94 {
95 oval_result_tcxx . . $ oval_result_t# result = OVAL_RESULT_ERRORcxx . . $ OVAL_RESULT_ERROR.;
96 uint32_tcxx . . $ uint32_t# mask1 = 0, mask2 = 0;
97 char addr1[INET6_ADDRSTRLENcxx . . $ `<external>/9581057695539925927/in.h:245:9`!];
98 char addr2[INET6_ADDRSTRLENcxx . . $ `<external>/9581057695539925927/in.h:245:9`!];
99
100 if (ipaddr_parsecxx . . $ ipaddr_parse(35cd60eae54dae5b).(af, s1, &mask1, &addr1) || ipaddr_parsecxx . . $ ipaddr_parse(35cd60eae54dae5b).(af, s2, &mask2, &addr2)) {
101 return result;
102 }
103
104 switch (op) {
105 case OVAL_OPERATION_EQUALScxx . . $ OVAL_OPERATION_EQUALS.:
106 if (!ipaddr_cmpcxx . . $ ipaddr_cmp(860aed8b209fc08f).(af, &addr1, &addr2) && mask1 == mask2)
107 result = OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE.;
108 else
109 result = OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
110 break;
111 case OVAL_OPERATION_NOT_EQUALcxx . . $ OVAL_OPERATION_NOT_EQUAL.:
112 if (ipaddr_cmpcxx . . $ ipaddr_cmp(860aed8b209fc08f).(af, &addr1, &addr2) || mask1 != mask2)
113 result = OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE.;
114 else
115 result = OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
116 break;
117 case OVAL_OPERATION_SUBSET_OFcxx . . $ 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_FALSEcxx . . $ OVAL_RESULT_FALSE.;
125 break;
126 }
127
128 /* Otherwise, compare the first bits defined by mask1 */
129 ipaddr_maskcxx . . $ ipaddr_mask(236b8fea641ffdaa).(af, &addr1, mask1);
130 ipaddr_maskcxx . . $ ipaddr_mask(236b8fea641ffdaa).(af, &addr2, mask1);
131 if (ipaddr_cmpcxx . . $ ipaddr_cmp(860aed8b209fc08f).(af, &addr1, &addr2) == 0)
132 result = OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE.;
133 else
134 result = OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
135 break;
136 case OVAL_OPERATION_GREATER_THANcxx . . $ OVAL_OPERATION_GREATER_THAN.:
137 if (mask1 != mask2) {
138 return OVAL_RESULT_ERRORcxx . . $ OVAL_RESULT_ERROR.;
139 }
140 ipaddr_maskcxx . . $ ipaddr_mask(236b8fea641ffdaa).(af, &addr1, mask1);
141 ipaddr_maskcxx . . $ ipaddr_mask(236b8fea641ffdaa).(af, &addr2, mask2);
142 if (ipaddr_cmpcxx . . $ ipaddr_cmp(860aed8b209fc08f).(af, &addr1, &addr2) < 0)
143 result = OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE.;
144 else
145 result = OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
146 break;
147 case OVAL_OPERATION_GREATER_THAN_OR_EQUALcxx . . $ OVAL_OPERATION_GREATER_THAN_OR_EQUAL.:
148 if (mask1 != mask2) {
149 return OVAL_RESULT_ERRORcxx . . $ OVAL_RESULT_ERROR.;
150 }
151 ipaddr_maskcxx . . $ ipaddr_mask(236b8fea641ffdaa).(af, &addr1, mask1);
152 ipaddr_maskcxx . . $ ipaddr_mask(236b8fea641ffdaa).(af, &addr2, mask2);
153 if (ipaddr_cmpcxx . . $ ipaddr_cmp(860aed8b209fc08f).(af, &addr1, &addr2) <= 0)
154 result = OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE.;
155 else
156 result = OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
157 break;
158 case OVAL_OPERATION_SUPERSET_OFcxx . . $ 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_FALSEcxx . . $ OVAL_RESULT_FALSE.;
166 break;
167 }
168
169 /* Otherwise, compare the first bits defined by mask2 */
170 ipaddr_maskcxx . . $ ipaddr_mask(236b8fea641ffdaa).(af, &addr1, mask2);
171 ipaddr_maskcxx . . $ ipaddr_mask(236b8fea641ffdaa).(af, &addr2, mask2);
172 if (ipaddr_cmpcxx . . $ ipaddr_cmp(860aed8b209fc08f).(af, &addr1, &addr2) == 0)
173 result = OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE.;
174 else
175 result = OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
176 break;
177 case OVAL_OPERATION_LESS_THANcxx . . $ OVAL_OPERATION_LESS_THAN.:
178 if (mask1 != mask2) {
179 return OVAL_RESULT_ERRORcxx . . $ OVAL_RESULT_ERROR.;
180 }
181 ipaddr_maskcxx . . $ ipaddr_mask(236b8fea641ffdaa).(af, &addr1, mask1);
182 ipaddr_maskcxx . . $ ipaddr_mask(236b8fea641ffdaa).(af, &addr2, mask2);
183 if (ipaddr_cmpcxx . . $ ipaddr_cmp(860aed8b209fc08f).(af, &addr1, &addr2) > 0)
184 result = OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE.;
185 else
186 result = OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
187 break;
188 case OVAL_OPERATION_LESS_THAN_OR_EQUALcxx . . $ OVAL_OPERATION_LESS_THAN_OR_EQUAL.:
189 if (mask1 != mask2) {
190 return OVAL_RESULT_ERRORcxx . . $ OVAL_RESULT_ERROR.;
191 }
192 ipaddr_maskcxx . . $ ipaddr_mask(236b8fea641ffdaa).(af, &addr1, mask1);
193 ipaddr_maskcxx . . $ ipaddr_mask(236b8fea641ffdaa).(af, &addr2, mask2);
194 if (ipaddr_cmpcxx . . $ ipaddr_cmp(860aed8b209fc08f).(af, &addr1, &addr2) >= 0)
195 result = OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE.;
196 else
197 result = OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
198 break;
199 default:
200 dEcxx . . $ `src/common/debug_priv.h:63:9`!("Unexpected compare operation: %d.", op);
201 assertcxx . . $ `/usr/include/assert.h:50:10`!(false);
202 }
203
204 return result;
205 }
206
207 static inline int ipv4addr_parsecxx . . $ ipv4addr_parse(ceb151ea223f6e2a).(const char *oval_ipv4_string, uint32_tcxx . . $ uint32_t# *netmask_out, struct in_addrcxx . . $ in_addr# *ip_out)
208 {
209 char *s, *pfx;
210 int result = -1;
211
212 s = strdupcxx . . $ strdup(a8b8e32347182212).(oval_ipv4_string);
213 pfx = strchrcxx . . $ strchr(2050848944e00d54).(s, '/');
214 if (pfx) {
215 int cnt;
216 unsigned char nm[4];
217
218 *pfx++ = '\0';
219 cnt = sscanfcxx . . $ sscanf(5e8a2c12db2ea735).(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 dWcxx . . $ `src/common/debug_priv.h:62:9`!("Invalid prefix or netmask.");
226 freecxx . . $ free(5d22bdacc48458e8).(s);
227 return -1;
228 }
229 } else {
230 *netmask_out = ~0;
231 }
232
233 if (inet_ptoncxx . . $ inet_pton(8d303d671d73812).(AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, s, ip_out) <= 0)
234 dWcxx . . $ `src/common/debug_priv.h:62:9`!("inet_pton() failed.");
235 else
236 result = 0;
237
238 freecxx . . $ free(5d22bdacc48458e8).(s);
239 return result;
240 }
241
242 static inline void ipv4addr_maskcxx . . $ ipv4addr_mask(7cb0f3b2dce9e5d8).(struct in_addrcxx . . $ in_addr# *ip_addr, uint32_tcxx . . $ uint32_t# netmask)
243 {
244 ip_addr->s_addrcxx . . $ in_addr#s_addr. &= htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(netmask);
245 }
246
247 static inline int ipv6addr_parsecxx . . $ ipv6addr_parse(9881f6607b848b71).(const char *oval_ipv6_string, uint32_tcxx . . $ uint32_t# *len_out, struct in6_addrcxx . . $ in6_addr# *ip_out)
248 {
249 char *s, *pfx;
250 int result = -1;
251
252 s = strdupcxx . . $ strdup(a8b8e32347182212).(oval_ipv6_string);
253 pfx = strchrcxx . . $ strchr(2050848944e00d54).(s, '/');
254 if (pfx) {
255 *pfx++ = '\0';
256 *len_out = strtolcxx . . $ strtol(44bbb87609b212fc).(pfx, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 10);
257 } else {
258 *len_out = 128;
259 }
260
261 if (inet_ptoncxx . . $ inet_pton(8d303d671d73812).(AF_INET6cxx . . $ `<external>/8962547315054503424/socket.h:105:9`!, s, ip_out) <= 0)
262 dWcxx . . $ `src/common/debug_priv.h:62:9`!("inet_pton() failed.");
263 else
264 result = 0;
265
266 freecxx . . $ free(5d22bdacc48458e8).(s);
267 return result;
268 }
269
270 static inline void ipv6addr_maskcxx . . $ ipv6addr_mask(4fd71685b6320a2).(struct in6_addrcxx . . $ in6_addr# *addr, int prefix_len)
271 {
272 assertcxx . . $ `/usr/include/assert.h:50:10`!(128 >= prefix_len);
273
274 uint8_tcxx . . $ 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_addrcxx . . $ `<external>/9581057695539925927/in.h:231:9`![i] &= mask;
281 /* The rest will be zeroed. */
282 mask = 0;
283 }
284 }