Debian Codegraph @

src/OVAL/results/oval_cmp_evr_string.c

raw symbols SCIP
1 /*
2 * Copyright 2009--2013 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * "Peter Vrabec" <[email protected]>
21 * Šimon Lukašík
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <math.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <limits.h>
34 #include "oval_cmp_evr_string_impl.h"
35 #include "oval_definitions.h"
36 #include "oval_types.h"
37
38 #include "common/_error.h"
39
40 #ifdef HAVE_RPMVERCMP
41 #ifdef RPM418_FOUND
42 #include <rpm/rpmver.h>
43 #include <rpm/rpmstring.h>
44 #else
45 #include <rpm/rpmlib.h>
46 #endif
47 #else
48 #ifdef OS_WINDOWS
49 #include <malloc.h>
50 #elif !defined(OS_FREEBSD)
51 #include <alloca.h>
52 #endif
53 static int rpmvercmp(const char *a, const char *b);
54 static int risdigit(int c) {
55 // locale independent
56 return (c >= '0' && c <= '9');
57 }
58 #endif
59
60 static inline int rpmevrcmp(const char *a, const char *b);
61 static int compare_values(const char *str1, const char *str2);
62 static void parseEVR(char *evr, const char **ep, const char **vp, const char **rp);
63
64 oval_result_t oval_evr_string_cmp(const char *state, const char *sys, oval_operation_t operation)
65 {
66 if (state == NULL || sys == NULL) {
67 return OVAL_RESULT_ERROR;
68 }
69 int result = rpmevrcmp(sys, state);
70
71 if (operation == OVAL_OPERATION_EQUALS) {
72 return ((result == 0) ? OVAL_RESULT_TRUE : OVAL_RESULT_FALSE);
73 } else if (operation == OVAL_OPERATION_NOT_EQUAL) {
74 return ((result != 0) ? OVAL_RESULT_TRUE : OVAL_RESULT_FALSE);
75 } else if (operation == OVAL_OPERATION_GREATER_THAN) {
76 return ((result == 1) ? OVAL_RESULT_TRUE : OVAL_RESULT_FALSE);
77 } else if (operation == OVAL_OPERATION_GREATER_THAN_OR_EQUAL) {
78 return ((result != -1) ? OVAL_RESULT_TRUE : OVAL_RESULT_FALSE);
79 } else if (operation == OVAL_OPERATION_LESS_THAN) {
80 return ((result == -1) ? OVAL_RESULT_TRUE : OVAL_RESULT_FALSE);
81 } else if (operation == OVAL_OPERATION_LESS_THAN_OR_EQUAL) {
82 return ((result != 1) ? OVAL_RESULT_TRUE : OVAL_RESULT_FALSE);
83 }
84
85 oscap_seterr(OSCAP_EFAMILY_OVAL, "Invalid type of operation in rpm version comparison: %d.", operation);
86 return OVAL_RESULT_ERROR;
87 }
88
89 static inline int rpmevrcmp(const char *a, const char *b)
90 {
91 /* This mimics rpmevrcmp which is not exported by rpmlib version 4.
92 * Code inspired by rpm.labelCompare() from rpm4/python/header-py.c
93 */
94 const char *a_epoch = NULL, *a_version = NULL, *a_release = NULL;
95 const char *b_epoch = NULL, *b_version = NULL, *b_release = NULL;
96 char *a_copy, *b_copy;
97 int result;
98
99 a_copy = oscap_strdup(a);
100 b_copy = oscap_strdup(b);
101 parseEVR(a_copy, &a_epoch, &a_version, &a_release);
102 parseEVR(b_copy, &b_epoch, &b_version, &b_release);
103
104 result = compare_values(a_epoch, b_epoch);
105 if (!result) {
106 result = compare_values(a_version, b_version);
107 if (!result)
108 result = compare_values(a_release, b_release);
109 }
110
111 free(a_copy);
112 free(b_copy);
113 return result;
114 }
115
116 static int compare_values(const char *str1, const char *str2)
117 {
118 /*
119 * Code copied from rpm4/python/header-py.c
120 */
121 if (!str1 && !str2)
122 return 0;
123 else if (str1 && !str2)
124 return 1;
125 else if (!str1 && str2)
126 return -1;
127 return rpmvercmp(str1, str2);
128 }
129
130 static void parseEVR(char *evr, const char **ep, const char **vp, const char **rp)
131 {
132 /*
133 * Code copied from rpm4/lib/rpmds.c
134 */
135 const char *epoch;
136 const char *version; /* assume only version is present */
137 const char *release;
138 char *s, *se;
139
140 if (!evr)
141 return;
142
143 s = evr;
144 while (*s && risdigit(*s)) s++; /* s points to epoch terminator */
145 se = strrchr(s, '-'); /* se points to version terminator */
146
147 if (*s == ':') {
148 epoch = evr;
149 *s++ = '\0';
150 version = s;
151 if (*epoch == '\0')
152 epoch = "0";
153 } else {
154 epoch = NULL; /* XXX disable epoch compare if missing */
155 version = evr;
156 }
157 if (se) {
158 *se++ = '\0';
159 release = se;
160 } else {
161 release = NULL;
162 }
163
164 if (ep) *ep = epoch;
165 if (vp) *vp = version;
166 if (rp) *rp = release;
167 }
168
169 #ifndef HAVE_RPMVERCMP
170 /*
171 * code from http://rpm.org/api/4.4.2.2/rpmvercmp_8c-source.html
172 */
173
174 /* compare alpha and numeric segments of two versions */
175 /* return 1: a is newer than b */
176 /* 0: a and b are the same version */
177 /* -1: b is newer than a */
178 static int rpmvercmp(const char *a, const char *b)
179 {
180 char oldch1, oldch2;
181 char *str1, *str2;
182 char *one, *two;
183 int rc;
184 int isnum;
185
186 /* easy comparison to see if versions are identical */
187 if (!strcmp(a, b))
188 return 0;
189
190 /* TODO: make new malloca */
191 str1 = alloca(strlen(a) + 1);
192 str2 = alloca(strlen(b) + 1);
193
194 strcpy(str1, a);
195 strcpy(str2, b);
196
197 one = str1;
198 two = str2;
199
200 /* loop through each version segment of str1 and str2 and compare them */
201 while (*one && *two) {
202 while (*one && !isalnum(*one))
203 one++;
204 while (*two && !isalnum(*two))
205 two++;
206
207 /* If we ran to the end of either, we are finished with the loop */
208 if (!(*one && *two))
209 break;
210
211 str1 = one;
212 str2 = two;
213
214 /* grab first completely alpha or completely numeric segment */
215 /* leave one and two pointing to the start of the alpha or numeric */
216 /* segment and walk str1 and str2 to end of segment */
217 if (isdigit(*str1)) {
218 while (*str1 && isdigit(*str1))
219 str1++;
220 while (*str2 && isdigit(*str2))
221 str2++;
222 isnum = 1;
223 } else {
224 while (*str1 && isalpha(*str1))
225 str1++;
226 while (*str2 && isalpha(*str2))
227 str2++;
228 isnum = 0;
229 }
230
231 /* save character at the end of the alpha or numeric segment */
232 /* so that they can be restored after the comparison */
233 oldch1 = *str1;
234 *str1 = '\0';
235 oldch2 = *str2;
236 *str2 = '\0';
237
238 /* this cannot happen, as we previously tested to make sure that */
239 /* the first string has a non-null segment */
240 if (one == str1)
241 return -1; /* arbitrary */
242
243 /* take care of the case where the two version segments are */
244 /* different types: one numeric, the other alpha (i.e. empty) */
245 /* numeric segments are always newer than alpha segments */
246 /* result_test See patch #60884 (and details) from bugzilla #50977. */
247 if (two == str2)
248 return (isnum ? 1 : -1);
249
250 if (isnum) {
251 /* this used to be done by converting the digit segments */
252 /* to ints using atoi() - it's changed because long */
253 /* digit segments can overflow an int - this should fix that. */
254
255 /* throw away any leading zeros - it's a number, right? */
256 while (*one == '0')
257 one++;
258 while (*two == '0')
259 two++;
260
261 /* whichever number has more digits wins */
262 if (strlen(one) > strlen(two))
263 return 1;
264 if (strlen(two) > strlen(one))
265 return -1;
266 }
267
268 /* strcmp will return which one is greater - even if the two */
269 /* segments are alpha or if they are numeric. don't return */
270 /* if they are equal because there might be more segments to */
271 /* compare */
272 rc = strcmp(one, two);
273 if (rc)
274 return (rc < 1 ? -1 : 1);
275
276 /* restore character that was replaced by null above */
277 *str1 = oldch1;
278 one = str1;
279 *str2 = oldch2;
280 two = str2;
281 }
282 /* this catches the case where all numeric and alpha segments have */
283 /* compared identically but the segment sepparating characters were */
284 /* different */
285 if ((!*one) && (!*two))
286 return 0;
287
288 /* whichever version still has characters left over wins */
289 if (!*one)
290 return -1;
291 else
292 return 1;
293 }
294 #endif
295
296 /*
297 * based on code from dpkg: lib/dpkg/version.c
298 * Mino changes to use isdigit() and isalpha()
299 */
300 /**
301 * Give a weight to the character to order in the version comparison.
302 *
303 * @param c An ASCII character.
304 */
305 static int order(int c)
306 {
307 if (isdigit(c))
308 return 0;
309 else if (isalpha(c))
310 return c;
311 else if (c == '~')
312 return -1;
313 else if (c)
314 return c + 256;
315 else
316 return 0;
317 }
318
319 /*
320 * based on code from dpkg: lib/dpkg/version.c
321 * Minor changes to use isdigit()
322 */
323 static int verrevcmp(const char *a, const char *b)
324 {
325 if (a == NULL)
326 a = "";
327 if (b == NULL)
328 b = "";
329
330 while (*a || *b) {
331 int first_diff = 0;
332
333 while ((*a && !isdigit(*a)) || (*b && !isdigit(*b))) {
334 int ac = order(*a);
335 int bc = order(*b);
336
337 if (ac != bc)
338 return ac - bc;
339
340 a++;
341 b++;
342 }
343 while (*a == '0')
344 a++;
345 while (*b == '0')
346 b++;
347 while (isdigit(*a) && isdigit(*b)) {
348 if (!first_diff)
349 first_diff = *a - *b;
350 a++;
351 b++;
352 }
353
354 if (isdigit(*a))
355 return 1;
356 if (isdigit(*b))
357 return -1;
358 if (first_diff)
359 return first_diff;
360 }
361
362 return 0;
363 }
364
365 /*
366 * Code copied from lib/dpkg/version.c
367 */
368 /**
369 * Compares two Debian versions.
370 *
371 * This function follows the convention of the comparator functions used by
372 * qsort().
373 *
374 * @see deb-version(5)
375 *
376 * @param a The first version.
377 * @param b The second version.
378 *
379 * @retval 0 If a and b are equal.
380 * @retval <0 If a is smaller than b.
381 * @retval >0 If a is greater than b.
382 */
383 static int dpkg_version_compare(struct dpkg_version *a, struct dpkg_version *b)
384 {
385 int rc;
386
387 if (a->epoch > b->epoch)
388 return 1;
389 if (a->epoch < b->epoch)
390 return -1;
391
392 rc = verrevcmp(a->version, b->version);
393 if (rc)
394 return rc;
395
396 return verrevcmp(a->revision, b->revision);
397 }
398
399 oval_result_t oval_debian_evr_string_cmp(const char *state, const char *sys, oval_operation_t operation)
400 {
401 struct dpkg_version a, b;
402 const char *a_epoch = NULL, *a_version = NULL, *a_release = NULL;
403 const char *b_epoch = NULL, *b_version = NULL, *b_release = NULL;
404 char *a_copy, *b_copy;
405 long aux;
406
407 a_copy = oscap_strdup(sys);
408 b_copy = oscap_strdup(state);
409 parseEVR(a_copy, &a_epoch, &a_version, &a_release);
410 parseEVR(b_copy, &b_epoch, &b_version, &b_release);
411
412 if (!a_epoch)
413 a_epoch = "0";
414 if (!b_epoch)
415 b_epoch = "0";
416
417 aux = strtol(a_epoch, NULL, 10);
418 if (aux < INT_MIN || aux > INT_MAX) {
419 free(a_copy);
420 free(b_copy);
421 return OVAL_RESULT_ERROR; // Outside int range
422 }
423 a.epoch = (int) aux;
424
425 aux = strtol(b_epoch, NULL, 10);
426 if (aux < INT_MIN || aux > INT_MAX) {
427 free(a_copy);
428 free(b_copy);
429 return OVAL_RESULT_ERROR; // Outside int range
430 }
431 b.epoch = (int) aux;
432
433 a.version = a_version;
434 a.revision = a_release;
435 b.version = b_version;
436 b.revision = b_release;
437 int result = dpkg_version_compare(&a, &b);
438
439 free(a_copy);
440 free(b_copy);
441 switch (operation) {
442 case OVAL_OPERATION_EQUALS:
443 return ((result == 0) ? OVAL_RESULT_TRUE : OVAL_RESULT_FALSE);
444 case OVAL_OPERATION_NOT_EQUAL:
445 return ((result != 0) ? OVAL_RESULT_TRUE : OVAL_RESULT_FALSE);
446 case OVAL_OPERATION_GREATER_THAN:
447 return ((result > 0) ? OVAL_RESULT_TRUE : OVAL_RESULT_FALSE);
448 case OVAL_OPERATION_GREATER_THAN_OR_EQUAL:
449 return ((result >= 0) ? OVAL_RESULT_TRUE : OVAL_RESULT_FALSE);
450 case OVAL_OPERATION_LESS_THAN:
451 return ((result < 0) ? OVAL_RESULT_TRUE : OVAL_RESULT_FALSE);
452 case OVAL_OPERATION_LESS_THAN_OR_EQUAL:
453 return ((result <= 0) ? OVAL_RESULT_TRUE : OVAL_RESULT_FALSE);
454 default:
455 oscap_seterr(OSCAP_EFAMILY_OVAL, "Invalid type of operation in dpkg version comparison: %d.", operation);
456 }
457
458 return OVAL_RESULT_ERROR;
459 }
460
461 oval_result_t oval_versiontype_cmp(const char *state, const char *syschar, oval_operation_t operation)
462 {
463 int state_idx = 0;
464 int sys_idx = 0;
465 int result = -1;
466 /* int is_equal = 1; */
467 for (state_idx = 0, sys_idx = 0; (((state[state_idx]) || (syschar[sys_idx])) && (result == -1));) { // keep going as long as there is data in either the state or sysitem
468 int tmp_state_int, tmp_sys_int;
469 tmp_state_int = atoi(&state[state_idx]); // look at the current data field (if we're at the end, atoi should return 0)
470 tmp_sys_int = atoi(&syschar[sys_idx]);
471 /* o rly?
472 if (tmp_state_int != tmp_sys_int)
473 is_equal = 0; // we might need this later (if we don't terminate early)
474 */
475 if (operation == OVAL_OPERATION_EQUALS) {
476 if (tmp_state_int != tmp_sys_int)
477 return (OVAL_RESULT_FALSE);
478 } else if (operation == OVAL_OPERATION_NOT_EQUAL) {
479 if (tmp_state_int != tmp_sys_int)
480 return (OVAL_RESULT_TRUE);
481 } else if ((operation == OVAL_OPERATION_GREATER_THAN)
482 || (operation == OVAL_OPERATION_GREATER_THAN_OR_EQUAL)) {
483 if (tmp_sys_int > tmp_state_int)
484 return (OVAL_RESULT_TRUE);
485 if (tmp_sys_int < tmp_state_int)
486 return (OVAL_RESULT_FALSE);
487 } else if ((operation == OVAL_OPERATION_LESS_THAN)
488 || (operation == OVAL_OPERATION_LESS_THAN_OR_EQUAL)) {
489 if (tmp_sys_int < tmp_state_int)
490 return (OVAL_RESULT_TRUE);
491 if (tmp_sys_int > tmp_state_int)
492 return (OVAL_RESULT_FALSE);
493 } else {
494 oscap_seterr(OSCAP_EFAMILY_OVAL, "Invalid type of operation in version comparison: %d.", operation);
495 return OVAL_RESULT_ERROR;
496 }
497
498 if (state[state_idx])
499 ++state_idx;
500 /* move to the next field within the version string (if there is one) */
501 while ((state[state_idx]) && (isdigit(state[state_idx])))
502 ++state_idx;
503 if ((state[state_idx]) && (!isdigit(state[state_idx])))
504 ++state_idx;
505
506 if (syschar[sys_idx])
507 ++sys_idx;
508 /* move to the next field within the version string (if there is one) */
509 while ((syschar[sys_idx]) && (isdigit(syschar[sys_idx])))
510 ++sys_idx;
511 if ((syschar[sys_idx]) && (!isdigit(syschar[sys_idx])))
512 ++sys_idx;
513 }
514
515 // OK, we did not terminate early, and we're out of data, so we now know what to return
516 if (operation == OVAL_OPERATION_EQUALS) {
517 return (OVAL_RESULT_TRUE);
518 } else if (operation == OVAL_OPERATION_NOT_EQUAL) {
519 return (OVAL_RESULT_FALSE);
520 } else if (operation == OVAL_OPERATION_GREATER_THAN) {
521 return (OVAL_RESULT_FALSE);
522 } else if (operation == OVAL_OPERATION_GREATER_THAN_OR_EQUAL) {
523 return (OVAL_RESULT_TRUE);
524 } else if (operation == OVAL_OPERATION_LESS_THAN) {
525 return (OVAL_RESULT_FALSE);
526 } else if (operation == OVAL_OPERATION_LESS_THAN_OR_EQUAL) {
527 return (OVAL_RESULT_TRUE);
528 } // we have already filtered out the invalid ones
529 assert(0);
530 return OVAL_RESULT_ERROR;
531
532 }