Debian Codegraph @

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

raw symbols SCIP
1 #ifdef HAVE_CONFIG_H
2 #include <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"
12 #include "dpkginfo-helper.h"
13
14 #define DPKG_STATUS_BUFFER_SIZE 4096
15
16 static char* trimleft(char *str)
17 {
18 while (isspace((unsigned char)*str))
19 str++;
20
21 if (*str == 0)
22 return str;
23
24 return str;
25 }
26
27 static int version(struct dpkginfo_reply_t *reply)
28 {
29 char *evr, *epoch, *version, *release;
30
31 if (reply->evr == NULL)
32 return -1;
33
34 evr = strdup(reply->evr);
35 if (evr == NULL)
36 return -1;
37
38 if ((epoch = strchr(evr, ':')) != NULL) {
39 *epoch++ = '\0';
40 reply->epoch = strdup(evr);
41 if (reply->epoch == NULL)
42 goto err;
43 } else {
44 reply->epoch = strdup("0");
45 if (reply->epoch == NULL)
46 goto err;
47 epoch = evr;
48 }
49
50 version = epoch;
51 if ((release = strchr(version, '-')) != NULL) {
52 *release++ = '\0';
53 reply->release = strdup(release);
54 if (reply->release == NULL)
55 goto err;
56 }
57 reply->version = strdup(version);
58 if (reply->version == NULL)
59 goto err;
60
61 free(evr);
62 return 0;
63 err:
64 free(evr);
65 return -1;
66 }
67
68 struct dpkginfo_reply_t* dpkginfo_get_by_name(const char *name, int *err)
69 {
70 FILE *f;
71 char buf[DPKG_STATUS_BUFFER_SIZE], path[PATH_MAX], *root, *key, *value, *p;
72 struct dpkginfo_reply_t *reply;
73
74 *err = 0;
75 reply = NULL;
76
77 root = getenv("OSCAP_PROBE_ROOT");
78 if (root != NULL)
79 snprintf(path, PATH_MAX, "%s/var/lib/dpkg/status", root);
80 else
81 snprintf(path, PATH_MAX, "/var/lib/dpkg/status");
82
83 f = fopen(path, "r");
84 if (f == NULL) {
85 dW("%s not found.", path);
86 *err = -1;
87 return NULL;
88 }
89
90 dD("Searching package \"%s\".", name);
91
92 while (fgets(buf, DPKG_STATUS_BUFFER_SIZE, f)) {
93 if (buf[0] == '\n') {
94 // New package entry.
95 if (reply != NULL) {
96 if (reply->name != NULL) {
97 // Package found.
98 goto out;
99 } else {
100 // Package not found yet.
101 dpkginfo_free_reply(reply);
102 reply = NULL;
103 }
104 }
105 continue;
106 }
107 if (isspace(buf[0])) {
108 // Ignore line beginning by a space.
109 continue;
110 }
111 buf[strcspn(buf, "\n")] = 0;
112 key = buf;
113 value = strchr(buf, ':');
114 if (value == NULL) {
115 // Ignore truncated line.
116 continue;
117 }
118 *value++ = '\0';
119 value = trimleft(value);
120 // Package should be the first line.
121 if (strcmp(key, "Package") == 0) {
122 if (reply != NULL)
123 continue;
124 reply = calloc(1, sizeof(*reply));
125 if (reply == NULL)
126 goto err;
127 if (strcmp(value, name) == 0) {
128 reply->name = strdup(value);
129 if (reply->name == NULL)
130 goto err;
131 }
132 } else if (reply != NULL) {
133 if (strcmp(key, "Status") == 0) {
134 if (strncmp(value, "install", 7) != 0) {
135 // Package deinstalled.
136 dD("Package \"%s\" has been deinstalled.", name);
137 dpkginfo_free_reply(reply);
138 reply = NULL;
139 continue;
140 }
141 } else if (strcmp(key, "Architecture") == 0) {
142 reply->arch = strdup(value);
143 if (reply->arch == NULL)
144 goto err;
145 } else if (strcmp(key, "Version") == 0) {
146 reply->evr = strdup(value);
147 if (reply->evr == NULL)
148 goto err;
149 if (version(reply) < 0)
150 goto err;
151 } else if (strcmp(key, "Provides") == 0) {
152 // Handle virtual packages.
153 char *s = strtok(value, ",");
154 while (s != NULL) {
155 s = trimleft(s);
156 // Ignore version.
157 p = strchr(s, ' ');
158 if (p != NULL)
159 *p++ = '\0';
160 if (strcmp(s, name) == 0) {
161 reply->name = strdup(value);
162 if (reply->name == NULL)
163 goto err;
164 break;
165 }
166 s = strtok(NULL, ",");
167 }
168 }
169 }
170 }
171
172 // Reached end of file.
173
174 out:
175 if (reply != NULL) {
176 // Package found.
177 dD("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 fclose(f);
182 return reply;
183 err:
184 dW("Insufficient memory available to allocate duplicate string.");
185 fclose(f);
186 dpkginfo_free_reply(reply);
187 *err = -1;
188 return NULL;
189 }
190
191 void dpkginfo_free_reply(struct dpkginfo_reply_t *reply)
192 {
193 if (reply) {
194 free(reply->name);
195 free(reply->arch);
196 free(reply->epoch);
197 free(reply->release);
198 free(reply->version);
199 free(reply->evr);
200 free(reply);
201 }
202 }