Debian Codegraph @

src/OVAL/probes/unix/linux/rpminfo_probe.c

raw symbols SCIP
1 /**
2 * @file rpminfo_probe.c
3 * @brief rpminfo probe
4 * @author "Daniel Kopecek" <[email protected]>
5 *
6 * 2010/06/13 [email protected]
7 * This probe is able to process a rpminfo_object as defined in OVAL 5.4 and 5.5.
8 *
9 */
10
11 /*
12 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
13 * All Rights Reserved.
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 *
29 * Authors:
30 * "Daniel Kopecek" <[email protected]>
31 */
32
33 /*
34 * rpminfo probe:
35 *
36 * rpminfo_object(string name)
37 *
38 * rpminfo_state(string name,
39 * string arch,
40 * string epoch,
41 * string release,
42 * string version,
43 * string evr,
44 * string signature_keyid
45 * string extended_status OVAL >= 5.10
46 * )
47 */
48
49 #ifdef HAVE_CONFIG_H
50 #include <config.h>
51 #endif
52
53 #include <stdio.h>
54 #include <string.h>
55 #include <errno.h>
56 #include <assert.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <fcntl.h>
60 #include <regex.h>
61
62 /* RPM headers */
63 #include "rpm-helper.h"
64
65 /* SEAP */
66 #include "_seap.h"
67 #include <probe-api.h>
68 #include <probe/probe.h>
69 #include <probe/option.h>
70 #include "probe/entcmp.h"
71 #include "common/debug_priv.h"
72 #include "rpminfo_probe.h"
73
74
75 struct rpminfo_req {
76 char *name;
77 oval_operation_t op;
78 };
79
80 struct rpminfo_rep {
81 char *name;
82 char *arch;
83 char *epoch;
84 char *release;
85 char *version;
86 char *evr;
87 char *signature_keyid;
88 char extended_name[1024];
89 };
90
91 #define RPMINFO_LOCK RPM_MUTEX_LOCK(&g_rpm->mutex)
92
93 #define RPMINFO_UNLOCK RPM_MUTEX_UNLOCK(&g_rpm->mutex)
94
95 static const char g_keyid_regex_string[] = "Key ID [a-fA-F0-9]{16}";
96
97 static void __rpminfo_rep_free (struct rpminfo_rep *ptr)
98 {
99 free (ptr->name);
100 free (ptr->arch);
101 free (ptr->epoch);
102 free (ptr->release);
103 free (ptr->version);
104 free (ptr->evr);
105 free (ptr->signature_keyid);
106 }
107
108 static void pkgh2rep(Header h, struct rpminfo_rep *r, regex_t *keyid_regex)
109 {
110 errmsg_t rpmerr;
111 char *str, *sid;
112 char *epoch_override = NULL;
113 size_t len;
114 regmatch_t keyid_match[1];
115
116 if (h == NULL || r == NULL) {
117 return;
118 }
119
120 r->name = headerFormat (h, "%{NAME}", &rpmerr);
121 r->arch = headerFormat (h, "%{ARCH}", &rpmerr);
122 r->epoch = headerFormat (h, "%{EPOCH}", &rpmerr);
123 r->release = headerFormat (h, "%{RELEASE}", &rpmerr);
124 r->version = headerFormat (h, "%{VERSION}", &rpmerr);
125 epoch_override = oscap_streq(r->epoch, "(none)") ? "0" : r->epoch;
126 snprintf(r->extended_name, 1024, "%s-%s:%s-%s.%s", r->name, epoch_override, r->version, r->release, r->arch);
127
128 len = (strlen(epoch_override) +
129 strlen (r->release) +
130 strlen (r->version) + 2);
131
132 str = malloc (sizeof (char) * (len + 1));
133 snprintf (str, len + 1, "%s:%s-%s",
134 epoch_override,
135 r->version,
136 r->release);
137
138 r->evr = str;
139
140 str = headerFormat (
141 h,
142 "%|DSAHEADER?{%{DSAHEADER:pgpsig}}:{%|RSAHEADER?{%{RSAHEADER:pgpsig}}:{%|SIGGPG?{%{SIGGPG:pgpsig}}:{%|SIGPGP?{%{SIGPGP:pgpsig}}:{(none)}|}|}|}|",
143 &rpmerr);
144
145 if (regexec(keyid_regex, str, 1, keyid_match, 0) != 0) {
146 sid = NULL;
147 dD("Failed to extract the Key ID value: regex=\"%s\", string=\"%s\"",
148 g_keyid_regex_string, str);
149 } else {
150 size_t keyid_start, keyid_length;
151
152 if (keyid_match[0].rm_so < 0 || keyid_match[0].rm_eo < 0)
153 sid = NULL;
154 else {
155 keyid_start = keyid_match[0].rm_so + strlen("Key ID ");
156 keyid_length = keyid_match[0].rm_eo - keyid_start;
157 sid = str + keyid_start;
158 sid[keyid_length] = '\0';
159 }
160 }
161
162 r->signature_keyid = strdup(sid != NULL ? sid : "0");
163 free (str);
164 }
165
166 /*
167 * req - Structure containing the name of the package.
168 * rep - Pointer to rpminfo_rep structure pointer. An
169 * array of rpminfo_rep structures will be allocated
170 * here.
171 *
172 * The return value on error is -1. Otherwise the number of
173 * rpminfo_rep structures allocated in *rep is returned.
174 */
175 static int get_rpminfo(struct rpminfo_req *req, struct rpminfo_rep **rep, struct rpm_probe_global *g_rpm)
176 {
177 rpmdbMatchIterator match;
178 Header pkgh;
179 int ret = 0, i;
180 regex_t keyid_regex;
181
182 if (regcomp(&keyid_regex, g_keyid_regex_string, REG_EXTENDED) != 0) {
183 dE("regcomp(%s) failed.", g_keyid_regex_string);
184 return -1;
185 }
186
187 RPMINFO_LOCK;
188
189 ret = -1;
190
191 switch (req->op) {
192 case OVAL_OPERATION_EQUALS:
193 match = rpmtsInitIterator(g_rpm->rpmts, RPMTAG_NAME, (const void *)req->name, 0);
194
195 if (match == NULL) {
196 ret = 0;
197 goto ret;
198 }
199
200 ret = rpmdbGetIteratorCount (match);
201
202 break;
203 case OVAL_OPERATION_NOT_EQUAL:
204 match = rpmtsInitIterator(g_rpm->rpmts, RPMDBI_PACKAGES, NULL, 0);
205 if (match == NULL) {
206 ret = 0;
207 goto ret;
208 }
209
210 if (rpmdbSetIteratorRE (match, RPMTAG_NAME, RPMMIRE_GLOB, "*") != 0)
211 {
212 ret = -1;
213 goto ret;
214 }
215
216 break;
217 case OVAL_OPERATION_PATTERN_MATCH:
218 match = rpmtsInitIterator(g_rpm->rpmts, RPMDBI_PACKAGES, NULL, 0);
219
220 if (match == NULL) {
221 ret = 0;
222 goto ret;
223 }
224
225 if (rpmdbSetIteratorRE (match, RPMTAG_NAME, RPMMIRE_REGEX,
226 (const char *)req->name) != 0)
227 {
228 ret = -1;
229 goto ret;
230 }
231
232 break;
233 default:
234 /* not supported */
235 ret = -1;
236 goto ret;
237 }
238
239 if (ret != -1) {
240 /*
241 * We can allocate all memory needed now because we know the number
242 * of results.
243 */
244 void *new_rep = realloc (*rep, sizeof (struct rpminfo_rep) * ret);
245 if (new_rep == NULL) {
246 ret = -2;
247 goto ret;
248 }
249 (*rep) = new_rep;
250 for (i = 0; i < ret; ++i) {
251 pkgh = rpmdbNextIterator (match);
252
253 if (pkgh != NULL)
254 pkgh2rep(pkgh, (*rep) + i, &keyid_regex);
255 else {
256 /* XXX: emit warning */
257 memset((*rep) + i, 0, sizeof(struct rpminfo_rep));
258 break;
259 }
260 }
261 } else {
262 ret = 0;
263
264 while ((pkgh = rpmdbNextIterator (match)) != NULL) {
265 void *new_rep = realloc (*rep, sizeof (struct rpminfo_rep) * ++ret);
266 if (new_rep == NULL) {
267 ret = -2;
268 goto ret;
269 }
270 (*rep) = new_rep;
271 pkgh2rep(pkgh, (*rep) + (ret - 1), &keyid_regex);
272 }
273 }
274
275 match = rpmdbFreeIterator (match);
276 ret:
277 RPMINFO_UNLOCK;
278
279 regfree(&keyid_regex);
280 return (ret);
281 }
282
283 int rpminfo_probe_offline_mode_supported()
284 {
285 return PROBE_OFFLINE_CHROOT;
286 }
287
288 void *rpminfo_probe_init(void)
289 {
290 #ifdef RPM46_FOUND
291 rpmlogSetCallback(rpmErrorCb, NULL);
292 #endif
293 struct rpm_probe_global *g_rpm = malloc(sizeof(struct rpm_probe_global));
294 if (rpmReadConfigFiles ((const char *)NULL, (const char *)NULL) != 0) {
295 dD("rpmReadConfigFiles failed: %u, %s.", errno, strerror (errno));
296 g_rpm->rpmts = NULL;
297 return ((void *)g_rpm);
298 }
299
300 set_rpm_db_path();
301 g_rpm->rpmts = rpmtsCreate();
302 pthread_mutex_init (&(g_rpm->mutex), NULL);
303
304 return ((void *)g_rpm);
305 }
306
307 void rpminfo_probe_fini (void *ptr)
308 {
309 struct rpm_probe_global *r = (struct rpm_probe_global *)ptr;
310
311 rpmFreeCrypto();
312 rpmFreeRpmrc();
313 rpmFreeMacros(NULL);
314 rpmlogClose();
315
316 if (r == NULL)
317 return;
318
319
320 if (r->rpmts == NULL)
321 return;
322
323 rpmtsFree(r->rpmts);
324 pthread_mutex_destroy (&(r->mutex));
325
326 free(r);
327 return;
328 }
329
330 static int collect_rpm_files(SEXP_t *item, const struct rpminfo_rep *rep, struct rpm_probe_global *g_rpm)
331 {
332 SEXP_t *value;
333 rpmdbMatchIterator ts;
334 Header pkgh;
335 rpmfi fi;
336 rpmTag tag[2] = { RPMTAG_BASENAMES, RPMTAG_DIRNAMES };
337 int i, ret = 0;
338
339 ts = rpmtsInitIterator(g_rpm->rpmts, RPMDBI_PACKAGES, NULL, 0);
340 if (ts == NULL) {
341 return -1;
342 }
343
344 if (rpmdbSetIteratorRE(ts, RPMTAG_NAME, RPMMIRE_STRCMP, rep->name) != 0) {
345 ret = -1;
346 goto cleanup;
347 }
348 char *epoch_override = oscap_streq(rep->epoch, "(none)") ? "0" : rep->epoch;
349 if (rpmdbSetIteratorRE(ts, RPMTAG_EPOCH, RPMMIRE_STRCMP, epoch_override) != 0) {
350 ret = -1;
351 goto cleanup;
352 }
353 if (rpmdbSetIteratorRE(ts, RPMTAG_VERSION, RPMMIRE_STRCMP, rep->version) != 0) {
354 ret = -1;
355 goto cleanup;
356 }
357 if (rpmdbSetIteratorRE(ts, RPMTAG_RELEASE, RPMMIRE_STRCMP, rep->release) != 0) {
358 ret = -1;
359 goto cleanup;
360 }
361 if (rpmdbSetIteratorRE(ts, RPMTAG_ARCH, RPMMIRE_STRCMP, rep->arch) != 0) {
362 ret = -1;
363 goto cleanup;
364 }
365
366 while ((pkgh = rpmdbNextIterator(ts)) != NULL) {
367 /*
368 * Inspect package files & directories
369 */
370 for (i = 0; i < 2; ++i) {
371 fi = rpmfiNew(g_rpm->rpmts, pkgh, tag[i], 1);
372
373 while (rpmfiNext(fi) != -1) {
374 const char *filepath;
375 filepath = rpmfiFN(fi);
376 value = probe_entval_from_cstr(
377 OVAL_DATATYPE_STRING,
378 filepath,
379 strlen(filepath)
380 );
381 if (value != NULL) {
382 probe_item_ent_add(item, "filepath", NULL, value);
383 SEXP_free(value);
384 }
385 }
386 rpmfiFree(fi);
387 }
388
389 }
390 cleanup:
391 ts = rpmdbFreeIterator(ts);
392 return ret;
393 }
394
395 int rpminfo_probe_main(probe_ctx *ctx, void *arg)
396 {
397 SEXP_t *val, *item, *ent, *probe_in;
398 oval_schema_version_t over;
399 int rpmret, i;
400
401 struct rpminfo_req request_st;
402 struct rpminfo_rep *reply_st;
403
404 // arg is NULL if regex compilation failed
405 if (arg == NULL) {
406 return PROBE_EINIT;
407 }
408
409 struct rpm_probe_global *g_rpm = (struct rpm_probe_global *)arg;
410
411 // There was no rpm config files
412 if (g_rpm->rpmts == NULL) {
413 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_NOT_APPLICABLE);
414 return 0;
415 }
416
417 if (ctx->offline_mode & PROBE_OFFLINE_OWN) {
418 const char* root = getenv("OSCAP_PROBE_ROOT");
419 rpmtsSetRootDir(g_rpm->rpmts, root);
420 }
421
422 probe_in = probe_ctx_getobject(ctx);
423 if (probe_in == NULL)
424 return PROBE_ENOOBJ;
425
426 over = probe_obj_get_platform_schema_version(probe_in);
427
428 ent = probe_obj_getent (probe_in, "name", 1);
429
430 if (ent == NULL) {
431 return (PROBE_ENOENT);
432 }
433
434 val = probe_ent_getval (ent);
435
436 if (val == NULL) {
437 dD("%s: no value", "name");
438 SEXP_free (ent);
439 return (PROBE_ENOVAL);
440 }
441
442 request_st.name = SEXP_string_cstr (val);
443 SEXP_free (val);
444
445 val = probe_ent_getattrval (ent, "operation");
446
447 if (val == NULL) {
448 request_st.op = OVAL_OPERATION_EQUALS;
449 } else {
450 request_st.op = (oval_operation_t) SEXP_number_geti_32 (val);
451
452 switch (request_st.op) {
453 case OVAL_OPERATION_EQUALS:
454 case OVAL_OPERATION_NOT_EQUAL:
455 case OVAL_OPERATION_PATTERN_MATCH:
456 break;
457 default:
458 SEXP_free (val);
459 SEXP_free (ent);
460 free (request_st.name);
461 return (PROBE_EOPNOTSUPP);
462 }
463
464 SEXP_free (val);
465 }
466
467 if (request_st.name == NULL) {
468 SEXP_free (ent);
469 switch (errno) {
470 case EINVAL:
471 dD("%s: invalid value type", "name");
472 return PROBE_EINVAL;
473 break;
474 case EFAULT:
475 dD("%s: element not found", "name");
476 return PROBE_ENOELM;
477 break;
478 default:
479 return PROBE_EUNKNOWN;
480 }
481 }
482
483 reply_st = NULL;
484
485 /* get info from RPM db */
486 switch (rpmret = get_rpminfo(&request_st, &reply_st, g_rpm)) {
487 case 0: /* Not found */
488 dI("Package \"%s\" not found.", request_st.name);
489 break;
490 case -1: /* Error */
491 dD("get_rpminfo failed");
492
493 item = probe_item_create(OVAL_LINUX_RPM_INFO, NULL,
494 "name", OVAL_DATATYPE_STRING, request_st.name,
495 NULL);
496
497 probe_item_setstatus (item, SYSCHAR_STATUS_ERROR);
498 probe_item_collect(ctx, item);
499 break;
500 default: /* Ok */
501 _A(rpmret >= 0);
502 _A(reply_st != NULL);
503 {
504 SEXP_t *name;
505
506 for (i = 0; i < rpmret; ++i) {
507 name = SEXP_string_newf("%s", reply_st[i].name);
508
509 if (probe_entobj_cmp(ent, name) != OVAL_RESULT_TRUE) {
510 SEXP_free(name);
511 continue;
512 }
513
514 item = probe_item_create(OVAL_LINUX_RPM_INFO, NULL,
515 "name", OVAL_DATATYPE_SEXP, name,
516 "arch", OVAL_DATATYPE_STRING, reply_st[i].arch,
517 "epoch", OVAL_DATATYPE_STRING, reply_st[i].epoch,
518 "release", OVAL_DATATYPE_STRING, reply_st[i].release,
519 "version", OVAL_DATATYPE_STRING, reply_st[i].version,
520 "evr", OVAL_DATATYPE_EVR_STRING, reply_st[i].evr,
521 "signature_keyid", OVAL_DATATYPE_STRING, reply_st[i].signature_keyid,
522 NULL);
523
524 /* OVAL 5.10 added extended_name and filepaths behavior */
525 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.10)) >= 0) {
526 SEXP_t *value, *bh_value;
527 value = probe_entval_from_cstr(
528 OVAL_DATATYPE_STRING,
529 reply_st[i].extended_name,
530 strlen(reply_st[i].extended_name)
531 );
532 probe_item_ent_add(item, "extended_name", NULL, value);
533 SEXP_free(value);
534
535 /*
536 * Parse behaviors
537 */
538 value = probe_obj_getent(probe_in, "behaviors", 1);
539 if (value != NULL) {
540 bh_value = probe_ent_getattrval(value, "filepaths");
541 if (bh_value != NULL) {
542 if (SEXP_strcmp(bh_value, "true") == 0) {
543 /* collect package files */
544 collect_rpm_files(item, &reply_st[i], g_rpm);
545
546 }
547 SEXP_free(bh_value);
548 }
549 SEXP_free(value);
550 }
551
552 }
553
554
555 SEXP_free(name);
556 __rpminfo_rep_free (&(reply_st[i]));
557
558 if (probe_item_collect(ctx, item) < 0) {
559 SEXP_free(ent);
560 return PROBE_EUNKNOWN;
561 }
562 }
563
564 free (reply_st);
565 }
566 }
567
568 SEXP_free(ent);
569 free(request_st.name);
570
571 return 0;
572 }