Debian Codegraph @

src/OVAL/probes/unix/linux/rpm-helper.c

raw symbols SCIP
1 /*
2 * Copyright 2016 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 */
20
21 #include "rpm-helper.h"
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #ifdef RPM46_FOUND
28 int rpmErrorCb (rpmlogRec rec, rpmlogCallbackData data)
29 {
30 dE("RPM: %s", rpmlogRecMessage(rec));
31 return RPMLOG_DEFAULT;
32 }
33 #endif
34
35 #ifndef HAVE_RPMVERIFYFILE
36 int rpmVerifyFile(const rpmts ts, const rpmfi fi,
37 rpmVerifyAttrs * res, rpmVerifyAttrs omitMask)
38 {
39 rpmVerifyAttrs vfy = rpmfiVerify(fi, omitMask);
40 if (res)
41 *res = vfy;
42
43 return (vfy & RPMVERIFY_LSTATFAIL) ? 1 : 0;
44 }
45 #endif
46
47 void rpmLibsPreload()
48 {
49 // Don't load rpmrc files. The are useless for us,
50 // because we only need to preload libraries
51 const char* rcfiles = "";
52 rpmReadConfigFiles(rcfiles, NULL);
53 }
54
55 void set_rpm_db_path()
56 {
57 /*
58 * Fedora >=36 changed the default dbpath in librpm from /var/lib/rpm to /usr/lib/sysimage/rpm.
59 * See: https://fedoraproject.org/wiki/Changes/RelocateRPMToUsr
60 *
61 * Therefore, when running openscap on a Fedora >=36 system scanning another systems (such as RHEL, SLES, Fedora<36)
62 * openscap's librpm will try to read the rpm db from /usr/lib/sysimage/rpm which doesn't exist and therefore won't work.
63 * On many systems, /var/lib/rpm is still a symlink to /usr/lib/sysimage/rpm, so using /var/lib/rpm can work there.
64 * However, on some systems, eg. bootc images, /var/lib/rpm isn't a symlink and doesn't contain the RPM database.
65 *
66 * We will first try if the "new" location /usr/lib/sysimage/rpm exists, and use it only if it exists.
67 * If it doesn't exist, we will fall back to the "old" location /var/lib/rpm.
68 */
69
70 struct stat sb;
71 const char *dbpath;
72 const char *prefix = getenv("OSCAP_PROBE_ROOT");
73 char *path_with_prefix = oscap_path_join(prefix, "/usr/lib/sysimage/rpm");
74 if (stat(path_with_prefix, &sb) == 0) {
75 dbpath = "/usr/lib/sysimage/rpm";
76 } else {
77 dbpath = "/var/lib/rpm";
78 }
79 free(path_with_prefix);
80 dI("Using %s as rpm database.", dbpath);
81 rpmPushMacro(NULL, "_dbpath", NULL, dbpath, RMIL_CMDLINE);
82 }