store packages with security issues and their optional updates only
[strongswan.git] / src / libimcv / plugins / imv_os / pacman.c
1 /*
2 * Copyright (C) 2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #define _GNU_SOURCE
17 #include <getopt.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <syslog.h>
23
24 #include <library.h>
25 #include <utils/debug.h>
26
27 /**
28 * global debug output variables
29 */
30 static int debug_level = 1;
31 static bool stderr_quiet = TRUE;
32
33 /**
34 * pacman dbg function
35 */
36 static void pacman_dbg(debug_t group, level_t level, char *fmt, ...)
37 {
38 int priority = LOG_INFO;
39 char buffer[8192];
40 char *current = buffer, *next;
41 va_list args;
42
43 if (level <= debug_level)
44 {
45 if (!stderr_quiet)
46 {
47 va_start(args, fmt);
48 vfprintf(stderr, fmt, args);
49 fprintf(stderr, "\n");
50 va_end(args);
51 }
52
53 /* write in memory buffer first */
54 va_start(args, fmt);
55 vsnprintf(buffer, sizeof(buffer), fmt, args);
56 va_end(args);
57
58 /* do a syslog with every line */
59 while (current)
60 {
61 next = strchr(current, '\n');
62 if (next)
63 {
64 *(next++) = '\0';
65 }
66 syslog(priority, "%s\n", current);
67 current = next;
68 }
69 }
70 }
71
72 /**
73 * atexit handler to close everything on shutdown
74 */
75 static void cleanup(void)
76 {
77 closelog();
78 library_deinit();
79 }
80
81 static void usage(void)
82 {
83 printf("Usage:\n"
84 "ipsec pacman --file <filename> --package <name>\n");
85 }
86
87 /**
88 * Process a package file and store updates in the database
89 */
90 static void process_packages(char *filename, char *product, bool update)
91 {
92 char *uri, line[12288], *pos;
93 int count = 0, errored = 0, vulnerable = 0;
94 int new_packages = 0, new_versions = 0;
95 u_int32_t pid = 0;
96 enumerator_t *e;
97 database_t *db;
98 FILE *file;
99
100 /* opening package file */
101 printf("loading\"%s\"\n", filename);
102 file = fopen(filename, "r");
103 if (!file)
104 {
105 fprintf(stderr, "could not open \"%s\"", filename);
106 exit(EXIT_FAILURE);
107 }
108
109 /* connect package database */
110 uri = lib->settings->get_str(lib->settings, "pacman.database", NULL);
111 if (!uri)
112 {
113 fprintf(stderr, "database URI pacman.database not set\n");
114 fclose(file);
115 exit(EXIT_FAILURE);
116 }
117 db = lib->db->create(lib->db, uri);
118 if (!db)
119 {
120 fprintf(stderr, "could not connect to database '%s'\n", uri);
121 fclose(file);
122 exit(EXIT_FAILURE);
123 }
124
125 /* check if product is already in database */
126 e = db->query(db, "SELECT id FROM products WHERE name = ?",
127 DB_TEXT, product, DB_INT);
128 if (e)
129 {
130 if (!e->enumerate(e, &pid))
131 {
132 pid = 0;
133 }
134 e->destroy(e);
135 }
136 if (!pid)
137 {
138 if (db->execute(db, &pid, "INSERT INTO products (name) VALUES (?)",
139 DB_TEXT, product) != 1)
140 {
141 fprintf(stderr, "could not store product '%s' to database\n",
142 product);
143 fclose(file);
144 db->destroy(db);
145 exit(EXIT_FAILURE);
146 }
147 }
148
149 while (fgets(line, sizeof(line), file))
150 {
151 char *package, *version, *current_version;
152 bool security, update_version = TRUE;
153 int current_security;
154 u_int32_t gid = 0, vid = 0;
155
156 count++;
157 if (count == 1 || count == 3)
158 {
159 printf("%s", line);
160 }
161 if (count < 7)
162 {
163 continue;
164 }
165
166 /* look for the package name */
167 pos = strchr(line, ' ');
168 if (!pos)
169 {
170 fprintf(stderr, "could not extract package name from '%.*s'",
171 strlen(line)-1, line);
172 errored++;
173 continue;
174 }
175 *pos++ = '\0';
176 package = line;
177
178 /* look for version string in parentheses */
179 if (*pos == '(')
180 {
181 version = ++pos;
182 pos = strchr(pos, ')');
183 if (pos)
184 {
185 *pos++ = '\0';
186 }
187 else
188 {
189 fprintf(stderr, "could not extract package version from '%.*s'\n",
190 strlen(line)-1, line);
191 errored++;
192 continue;
193 }
194 }
195 else
196 {
197 /* no version information, skip entry */
198 continue;
199 }
200 security = (strstr(pos, "[security]") != NULL);
201 if (security)
202 {
203 vulnerable++;
204 }
205
206 /* handle non-security packages in update mode only */
207 if (!update && !security)
208 {
209 continue;
210 }
211
212 /* check if package is already in database */
213 e = db->query(db, "SELECT id FROM packages WHERE name = ?",
214 DB_TEXT, package, DB_INT);
215 if (e)
216 {
217 if (!e->enumerate(e, &gid))
218 {
219 gid = 0;
220 }
221 e->destroy(e);
222 }
223 if (!gid && security)
224 {
225 if (db->execute(db, &gid, "INSERT INTO packages (name) VALUES (?)",
226 DB_TEXT, package) != 1)
227 {
228 fprintf(stderr, "could not store package '%s' to database\n",
229 package);
230 fclose(file);
231 db->destroy(db);
232 exit(EXIT_FAILURE);
233 }
234 new_packages++;
235 }
236
237 /* check for package versions already in database */
238 e = db->query(db, "SELECT id, release, security FROM versions "
239 "WHERE package = ? AND product = ?",
240 DB_INT, gid, DB_INT, pid, DB_INT, DB_TEXT, DB_INT);
241 if (e)
242 {
243 while (e->enumerate(e, &vid, &current_version, &current_security))
244 {
245 if (streq(version, current_version))
246 {
247 update_version = FALSE;
248 }
249 }
250 e->destroy(e);
251 }
252 if ((!vid && security) || (vid && update_version))
253 {
254 printf("'%s' (%s) %s\n", package, version, security ? "[s]" : "");
255
256 if (db->execute(db, &gid,
257 "INSERT INTO versions (package, product, release, security) "
258 "VALUES (?, ?, ?, ?)", DB_INT, gid, DB_INT, pid,
259 DB_TEXT, version, DB_INT, security) != 1)
260 {
261 fprintf(stderr, "could not store version '%s' to database\n",
262 version);
263 fclose(file);
264 db->destroy(db);
265 exit(EXIT_FAILURE);
266 }
267 new_versions++;
268 }
269 }
270
271 fclose(file);
272 db->destroy(db);
273 printf("processed %d packages, %d vulnerable, %d errored, "
274 "%d new packages, %d new versions\n", count - 6, vulnerable,
275 errored, new_packages, new_versions);
276 }
277
278 static void do_args(int argc, char *argv[])
279 {
280 char *filename = NULL, *product = NULL;
281 bool update = FALSE;
282
283 /* reinit getopt state */
284 optind = 0;
285
286 while (TRUE)
287 {
288 int c;
289
290 struct option long_opts[] = {
291 { "help", no_argument, NULL, 'h' },
292 { "file", required_argument, NULL, 'f' },
293 { "product", required_argument, NULL, 'p' },
294 { "update", no_argument, NULL, 'u' },
295 { 0,0,0,0 }
296 };
297
298 c = getopt_long(argc, argv, "", long_opts, NULL);
299 switch (c)
300 {
301 case EOF:
302 break;
303 case 'h':
304 usage();
305 exit(EXIT_SUCCESS);
306 case 'f':
307 filename = optarg;
308 continue;
309 case 'p':
310 product = optarg;
311 continue;
312 case 'u':
313 update = TRUE;
314 continue;
315 }
316 break;
317 }
318
319 if (filename && product)
320 {
321 process_packages(filename, product, update);
322 }
323 else
324 {
325 usage();
326 exit(EXIT_FAILURE);
327 }
328 }
329
330 int main(int argc, char *argv[])
331 {
332 /* enable attest debugging hook */
333 dbg = pacman_dbg;
334 openlog("pacman", 0, LOG_DEBUG);
335
336 atexit(cleanup);
337
338 /* initialize library */
339 if (!library_init(NULL))
340 {
341 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
342 }
343 if (!lib->plugins->load(lib->plugins, NULL,
344 lib->settings->get_str(lib->settings, "attest.load", "sqlite")))
345 {
346 exit(SS_RC_INITIALIZATION_FAILED);
347 }
348 do_args(argc, argv);
349
350 exit(EXIT_SUCCESS);
351 }
352