Debian Codegraph @

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

raw symbols SCIP
1 #cxx . . $ `<file>/src/OVAL/probes/unix/linux/dpkginfo-helper.c`/ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`!
2 #include <config.h>cxx . . $ `<file>/build/config.h`/
3 #endif
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <ctype.h>
9 #include <limits.h>
10
11 #include "debug_priv.h"cxx . . $ `<file>/src/common/debug_priv.h`/
12 #include "dpkginfo-helper.h"cxx . . $ `<file>/src/OVAL/probes/unix/linux/dpkginfo-helper.h`/
13
14 #define DPKG_STATUS_BUFFER_SIZEcxx . . $ `src/OVAL/probes/unix/linux/dpkginfo-helper.c:14:9`! 4096
15
16 static char* trimleftcxx . . $ trimleft(7a9722d75b371431).(char *str)
17 {
18 while (isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!((unsigned char)*str))
19 str++;
20
21 if (*str == 0)
22 return str;
23
24 return str;
25 }
26
27 static int versioncxx . . $ version(9281a3d9b173ce2f).(struct dpkginfo_reply_tcxx . . $ dpkginfo_reply_t# *reply)
28 {
29 char *evr, *epoch, *version, *release;
30
31 if (reply->evrcxx . . $ dpkginfo_reply_t#evr. == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
32 return -1;
33
34 evr = strdupcxx . . $ strdup(a8b8e32347182212).(reply->evrcxx . . $ dpkginfo_reply_t#evr.);
35 if (evr == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
36 return -1;
37
38 if ((epoch = strchrcxx . . $ strchr(2050848944e00d54).(evr, ':')) != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
39 *epoch++ = '\0';
40 reply->epochcxx . . $ dpkginfo_reply_t#epoch. = strdupcxx . . $ strdup(a8b8e32347182212).(evr);
41 if (reply->epochcxx . . $ dpkginfo_reply_t#epoch. == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
42 goto err;
43 } else {
44 reply->epochcxx . . $ dpkginfo_reply_t#epoch. = strdupcxx . . $ strdup(a8b8e32347182212).("0");
45 if (reply->epochcxx . . $ dpkginfo_reply_t#epoch. == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
46 goto err;
47 epoch = evr;
48 }
49
50 version = epoch;
51 if ((release = strchrcxx . . $ strchr(2050848944e00d54).(version, '-')) != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
52 *release++ = '\0';
53 reply->releasecxx . . $ dpkginfo_reply_t#release. = strdupcxx . . $ strdup(a8b8e32347182212).(release);
54 if (reply->releasecxx . . $ dpkginfo_reply_t#release. == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
55 goto err;
56 }
57 reply->versioncxx . . $ dpkginfo_reply_t#version. = strdupcxx . . $ strdup(a8b8e32347182212).(version);
58 if (reply->versioncxx . . $ dpkginfo_reply_t#version. == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
59 goto err;
60
61 freecxx . . $ free(5d22bdacc48458e8).(evr);
62 return 0;
63 err:
64 freecxx . . $ free(5d22bdacc48458e8).(evr);
65 return -1;
66 }
67
68 struct dpkginfo_reply_tcxx . . $ dpkginfo_reply_t#* dpkginfo_get_by_namecxx . . $ dpkginfo_get_by_name(2e3b51866dbd47b4).(const char *name, int *err)
69 {
70 FILEcxx . . $ FILE# *f;
71 char buf[DPKG_STATUS_BUFFER_SIZEcxx . . $ `src/OVAL/probes/unix/linux/dpkginfo-helper.c:14:9`!], path[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!], *root, *key, *value, *p;
72 struct dpkginfo_reply_tcxx . . $ dpkginfo_reply_t# *reply;
73
74 *err = 0;
75 reply = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
76
77 root = getenvcxx . . $ getenv(a8b8e32347182212).("OSCAP_PROBE_ROOT");
78 if (root != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
79 snprintfcxx . . $ snprintf(ea502d73e74c754c).(path, PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!, "%s/var/lib/dpkg/status", root);
80 else
81 snprintfcxx . . $ snprintf(ea502d73e74c754c).(path, PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!, "/var/lib/dpkg/status");
82
83 f = fopencxx . . $ fopen(82ec0787cd37e0a3).(path, "r");
84 if (f == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
85 dWcxx . . $ `src/common/debug_priv.h:62:9`!("%s not found.", path);
86 *err = -1;
87 return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
88 }
89
90 dDcxx . . $ `src/common/debug_priv.h:64:9`!("Searching package \"%s\".", name);
91
92 while (fgetscxx . . $ fgets(efb0aaba7ac866ae).(buf, DPKG_STATUS_BUFFER_SIZEcxx . . $ `src/OVAL/probes/unix/linux/dpkginfo-helper.c:14:9`!, f)) {
93 if (buf[0] == '\n') {
94 // New package entry.
95 if (reply != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
96 if (reply->namecxx . . $ dpkginfo_reply_t#name. != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
97 // Package found.
98 goto out;
99 } else {
100 // Package not found yet.
101 dpkginfo_free_replycxx . . $ dpkginfo_free_reply(d06f8a2c2281da9b).(reply);
102 reply = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
103 }
104 }
105 continue;
106 }
107 if (isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(buf[0])) {
108 // Ignore line beginning by a space.
109 continue;
110 }
111 buf[strcspncxx . . $ strcspn(335c1dc86f5c867f).(buf, "\n")] = 0;
112 key = buf;
113 value = strchrcxx . . $ strchr(2050848944e00d54).(buf, ':');
114 if (value == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
115 // Ignore truncated line.
116 continue;
117 }
118 *value++ = '\0';
119 value = trimleftcxx . . $ trimleft(7a9722d75b371431).(value);
120 // Package should be the first line.
121 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(key, "Package") == 0) {
122 if (reply != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
123 continue;
124 reply = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*reply));
125 if (reply == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
126 goto err;
127 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(value, name) == 0) {
128 reply->namecxx . . $ dpkginfo_reply_t#name. = strdupcxx . . $ strdup(a8b8e32347182212).(value);
129 if (reply->namecxx . . $ dpkginfo_reply_t#name. == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
130 goto err;
131 }
132 } else if (reply != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
133 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(key, "Status") == 0) {
134 if (strncmpcxx . . $ strncmp(fd278af2f295fa2e).(value, "install", 7) != 0) {
135 // Package deinstalled.
136 dDcxx . . $ `src/common/debug_priv.h:64:9`!("Package \"%s\" has been deinstalled.", name);
137 dpkginfo_free_replycxx . . $ dpkginfo_free_reply(d06f8a2c2281da9b).(reply);
138 reply = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
139 continue;
140 }
141 } else if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(key, "Architecture") == 0) {
142 reply->archcxx . . $ dpkginfo_reply_t#arch. = strdupcxx . . $ strdup(a8b8e32347182212).(value);
143 if (reply->archcxx . . $ dpkginfo_reply_t#arch. == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
144 goto err;
145 } else if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(key, "Version") == 0) {
146 reply->evrcxx . . $ dpkginfo_reply_t#evr. = strdupcxx . . $ strdup(a8b8e32347182212).(value);
147 if (reply->evrcxx . . $ dpkginfo_reply_t#evr. == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
148 goto err;
149 if (versioncxx . . $ version(9281a3d9b173ce2f).(reply) < 0)
150 goto err;
151 } else if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(key, "Provides") == 0) {
152 // Handle virtual packages.
153 char *s = strtokcxx . . $ strtok(b7b3a83273496486).(value, ",");
154 while (s != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
155 s = trimleftcxx . . $ trimleft(7a9722d75b371431).(s);
156 // Ignore version.
157 p = strchrcxx . . $ strchr(2050848944e00d54).(s, ' ');
158 if (p != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
159 *p++ = '\0';
160 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(s, name) == 0) {
161 reply->namecxx . . $ dpkginfo_reply_t#name. = strdupcxx . . $ strdup(a8b8e32347182212).(value);
162 if (reply->namecxx . . $ dpkginfo_reply_t#name. == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
163 goto err;
164 break;
165 }
166 s = strtokcxx . . $ strtok(b7b3a83273496486).(NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, ",");
167 }
168 }
169 }
170 }
171
172 // Reached end of file.
173
174 out:
175 if (reply != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
176 // Package found.
177 dDcxx . . $ `src/common/debug_priv.h:64:9`!("Package \"%s\" found (arch=%s evr=%s epoch=%s version=%s release=%s).",
178 name, reply->arch, reply->evr, reply->epoch, reply->version, reply->release);
179 *err = 1;
180 }
181 fclosecxx . . $ fclose(89bc1efcdac0ca22).(f);
182 return reply;
183 err:
184 dWcxx . . $ `src/common/debug_priv.h:62:9`!("Insufficient memory available to allocate duplicate string.");
185 fclosecxx . . $ fclose(89bc1efcdac0ca22).(f);
186 dpkginfo_free_replycxx . . $ dpkginfo_free_reply(d06f8a2c2281da9b).(reply);
187 *err = -1;
188 return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
189 }
190
191 void dpkginfo_free_replycxx . . $ dpkginfo_free_reply(d06f8a2c2281da9b).(struct dpkginfo_reply_tcxx . . $ dpkginfo_reply_t# *reply)
192 {
193 if (reply) {
194 freecxx . . $ free(5d22bdacc48458e8).(reply->namecxx . . $ dpkginfo_reply_t#name.);
195 freecxx . . $ free(5d22bdacc48458e8).(reply->archcxx . . $ dpkginfo_reply_t#arch.);
196 freecxx . . $ free(5d22bdacc48458e8).(reply->epochcxx . . $ dpkginfo_reply_t#epoch.);
197 freecxx . . $ free(5d22bdacc48458e8).(reply->releasecxx . . $ dpkginfo_reply_t#release.);
198 freecxx . . $ free(5d22bdacc48458e8).(reply->versioncxx . . $ dpkginfo_reply_t#version.);
199 freecxx . . $ free(5d22bdacc48458e8).(reply->evrcxx . . $ dpkginfo_reply_t#evr.);
200 freecxx . . $ free(5d22bdacc48458e8).(reply);
201 }
202 }