summaryrefslogtreecommitdiff
path: root/src/sec-updater/sec-updater.c
blob: e1d2baea2f21eb8d73b82fc41a1d51fa81798943 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
 * Copyright (C) 2012-2017 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#define _GNU_SOURCE
#include <getopt.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <time.h>
#include <sys/stat.h>
#include <stdlib.h>

#include <library.h>
#include <utils/debug.h>

#define EXIT_NO_UPDATES		80
#define TMP_DEB_FILE		"/tmp/sec-updater.deb"
#define TMP_TAG_FILE		"/tmp/sec-updater.tag"
#define SWID_GEN_CMD		"/usr/local/bin/swid_generator"
#define TNC_MANAGE_CMD		"/var/www/tnc/manage.py"

typedef enum sec_update_state_t sec_update_state_t;

enum sec_update_state_t {
	SEC_UPDATE_STATE_BEGIN_PACKAGE,
	SEC_UPDATE_STATE_VERSION,
	SEC_UPDATE_STATE_FILENAME,
	SEC_UPDATE_STATE_END_PACKAGE
};

typedef struct stats_t stats_t;

struct stats_t {
	time_t release;
	int product;
	int packages;
	int new_versions;
	int updated_versions;
};

/**
 * global debug output variables
 */
static int debug_level = 1;
static bool stderr_quiet = FALSE;

/**
 * sec_updater dbg function
 */
static void sec_updater_dbg(debug_t group, level_t level, char *fmt, ...)
{
	int priority = LOG_INFO;
	char buffer[8192];
	char *current = buffer, *next;
	va_list args;

	if (level <= debug_level)
	{
		if (!stderr_quiet)
		{
			va_start(args, fmt);
			vfprintf(stderr, fmt, args);
			fprintf(stderr, "\n");
			va_end(args);
		}

		/* write in memory buffer first */
		va_start(args, fmt);
		vsnprintf(buffer, sizeof(buffer), fmt, args);
		va_end(args);

		/* do a syslog with every line */
		while (current)
		{
			next = strchr(current, '\n');
			if (next)
			{
				*(next++) = '\0';
			}
			syslog(priority, "%s\n", current);
			current = next;
		}
	}
}

/**
 * atexit handler to close everything on shutdown
 */
static void cleanup(void)
{
	closelog();
	library_deinit();
}

static void usage(void)
{
	printf("\
Usage:\n\
  sec-updater --help\n\
  sec-updater [--debug <level>] [--quiet]  [--security] --os <string>\n\
               --arch <string> --uri <uri> --file <filename>\n\n\
  Options:\n\
    --help             print usage information\n\
    --debug <level>    set debug level\n\
    --quiet            suppress debug output to stderr\n\
    --os <string>      operating system\n\
    --arch <string>    hw architecture\n\
    --security         set when parsing a file with security updates\n\
    --file <filename>  package information file to parse\n\
    --uri <uri>        uri where to download deb package from\n");
 }

/**
 * Update the package database
 */
static bool update_database(database_t *db, char *package, char *version,
							bool security, stats_t *stats, bool *new)
{
	int pid = 0, vid = 0, sec_flag;
	bool first = TRUE, found = FALSE;
	char *release;
	enumerator_t *e;

	/* increment package count */
	stats->packages++;

	/* set new output variable */
	*new = FALSE;

	/* check if package is already in database */
	e = db->query(db, "SELECT id FROM packages WHERE name = ?",
					  DB_TEXT, package, DB_INT);
	if (!e)
	{
		return FALSE;
	}
	if (!e->enumerate(e, &pid))
	{
		pid = 0;
	}
	e->destroy(e);

	if (!pid)
	{
		return TRUE;
	}

	/* retrieve all package versions stored in database */
	e = db->query(db,
			"SELECT id, release, security FROM versions "
			"WHERE product = ? AND package = ?",
			 DB_INT, stats->product,  DB_INT, pid, DB_INT, DB_TEXT, DB_INT);
	if (!e)
	{
		return FALSE;
	}

	while (e->enumerate(e, &vid, &release, &sec_flag))
	{
		char command[BUF_LEN];
		char found_char = ' ';
		bool update_version = FALSE;

		if (streq(version, release))
		{
			found = TRUE;
			found_char = '*';
		}
		else if (security)
		{
			 snprintf(command, BUF_LEN, "dpkg --compare-versions %s lt %s",
										 release, version);
			if (system(command) == 0)
			{
				found_char = '!';
				if (!sec_flag)
				{
					if (db->execute(db, NULL, "UPDATE versions "
						"SET security = 1 WHERE id = ?", DB_INT, vid) != 1)
					{
						DBG1(DBG_IMV, "  could not update version");
						e->destroy(e);
						return FALSE;
					}
					update_version = TRUE;
					stats->updated_versions++;
				}
			}
		}
		if (debug_level < 2 && !update_version)
		{
			continue;
		}
		if (first)
		{
			DBG1(DBG_IMV, "%s", package);
			first = FALSE;
		}
		DBG1(DBG_IMV, "  %c%s %s", found_char , sec_flag ? "s" : " ", release);
	}
	e->destroy(e);

	if (!found)
	{
		if (first)
		{
			DBG1(DBG_IMV, "%s", package);
		}
		DBG1(DBG_IMV, "  +  %s", version);

		if (db->execute(db, &vid,
			"INSERT INTO versions "
			"(package, product, release, security, time) "
			"VALUES (?, ?, ?, 0, ?)", DB_INT, pid, DB_INT, stats->product,
			DB_TEXT, version, DB_INT, stats->release) != 1)
		{
			DBG1(DBG_IMV, "  could not store version to database");
			return FALSE;
		}
		stats->new_versions++;
		*new = TRUE;
	}

	return TRUE;
}

/**
 * Process a package file and store updates in the database
 */
static int process_packages(char *path, char *os, char *arch, char *uri,
							bool security)
{
	char line[BUF_LEN], product[BUF_LEN], command[BUF_LEN];
	char *db_uri, *download_uri = NULL, *swid_regid, *swid_entity;
	char *pos, *package = NULL, *version = NULL, *filename = NULL;
	char *swid_gen_cmd, *tnc_manage_cmd, *tmp_deb_file, *tmp_tag_file;
	sec_update_state_t state;
	enumerator_t *e;
	database_t *db;
	int len, pid;
	chunk_t deb = chunk_empty;
	FILE *file;
	stats_t stats;
	bool success = TRUE, new;

	/* initialize statistics */
	memset(&stats, 0x00, sizeof(stats_t));

	/* Set release date to current time */
	stats.release = time(NULL);

	/* opening package file */
	file = fopen(path, "r");
	if (!file)
	{
		DBG1(DBG_IMV, "  could not open \"%s\"", path);
		exit(EXIT_FAILURE);
	}

	/* connect package database */
	db_uri = lib->settings->get_str(lib->settings, "sec-updater.database", NULL);
	if (!db_uri)
	{
		DBG1(DBG_IMV, "database URI sec-updater.database not set");
		fclose(file);
		exit(EXIT_FAILURE);
	}
	db = lib->db->create(lib->db, db_uri);
	if (!db)
	{
		DBG1(DBG_IMV, "could not connect to database '%s'", db_uri);
		fclose(file);
		exit(EXIT_FAILURE);
	}

	/* form product name by concatenating os and arch strings */
	snprintf(product, BUF_LEN, "%s %s", os, arch);

	/* check if product is already in database */
	e = db->query(db, "SELECT id FROM products WHERE name = ?",
				  DB_TEXT, product, DB_INT);
	if (e)
	{
		if (e->enumerate(e, &pid))
		{
			stats.product = pid;
		}
		e->destroy(e);
	}
	if (!stats.product)
	{
		if (db->execute(db, &pid, "INSERT INTO products (name) VALUES (?)",
						DB_TEXT, product) != 1)
		{
			DBG1(DBG_IMV, "could not store product '%s' to database",
							 product);
			fclose(file);
			db->destroy(db);
			exit(EXIT_FAILURE);
		}
		stats.product = pid;
	}

	/* get settings for the loop */
	swid_regid = lib->settings->get_str(lib->settings,
						"sec-updater.swid_gen.tag_creator.regid",
						"strongswan.org");
	swid_entity = lib->settings->get_str(lib->settings,
						"sec-updater.swid_gen.tag_creator.name",
						"strongSwan Project");
	swid_gen_cmd = lib->settings->get_str(lib->settings,
						"sec-updater.swid_gen.command", SWID_GEN_CMD);
	tnc_manage_cmd = lib->settings->get_str(lib->settings,
						"sec-updater.tnc_manage_command", TNC_MANAGE_CMD);
	tmp_deb_file = lib->settings->get_str(lib->settings,
						"sec-updater.tmp.deb_file", TMP_DEB_FILE);
	tmp_tag_file = lib->settings->get_str(lib->settings,
						"sec-updater.tmp.tag_file", TMP_TAG_FILE);

	state = SEC_UPDATE_STATE_BEGIN_PACKAGE;

	while (fgets(line, sizeof(line), file))
	{
		/* set read pointer to beginning of line */
		pos = line;

		switch (state)
		{
			case SEC_UPDATE_STATE_BEGIN_PACKAGE:
				pos = strstr(pos, "Package: ");
				if (!pos)
				{
					continue;
				}
				pos += 9;
				package = pos;
				pos = strchr(pos, '\n');
				if (pos)
				{
					package = strndup(package, pos - package);
					state = SEC_UPDATE_STATE_VERSION;
				}
				break;
			case SEC_UPDATE_STATE_VERSION:
				pos = strstr(pos, "Version: ");
				if (!pos)
				{
					continue;
				}
				pos += 9;
				version = pos;
				pos = strchr(pos, '\n');
				if (pos)
				{
					version = strndup(version, pos - version);
					success = update_database(db, package, version, security,
											  &stats, &new);
					state = (success && new) ? SEC_UPDATE_STATE_FILENAME :
											   SEC_UPDATE_STATE_END_PACKAGE;
				}
				break;
			case SEC_UPDATE_STATE_FILENAME:
				pos = strstr(pos, "Filename: ");
				if (!pos)
				{
					continue;
				}
				state = SEC_UPDATE_STATE_END_PACKAGE;

				pos += 10;
				filename = pos;
				pos = strchr(pos, '\n');
				if (!pos)
				{
					break;
				}
				len = pos - filename;
				if (asprintf(&download_uri, "%s/%.*s", uri, len, filename) == -1)
				{
					break;
				}

				/* retrieve deb package file from linux repository */
				if (lib->fetcher->fetch(lib->fetcher, download_uri,
												&deb, FETCH_END) != SUCCESS)
				{
					DBG1(DBG_IMV, "     %s failed", download_uri);
					break;
				}
				DBG1(DBG_IMV, "     %s (%u bytes)", download_uri, deb.len);

				/* store deb package file to temporary location */
				if (!chunk_write(deb, tmp_deb_file, 0022, TRUE))
				{
					DBG1(DBG_IMV, "     save to '%s' failed", tmp_deb_file);
					break;
				}

				/* generate SWID tag for downloaded deb package */
				snprintf(command, BUF_LEN, "%s swid --full --package-file %s "
						 "--regid %s --entity-name '%s' --os '%s' --arch '%s' "
						 ">> %s", swid_gen_cmd, tmp_deb_file, swid_regid,
						 swid_entity, os, arch, tmp_tag_file);
				if (system(command) != 0)
				{
					DBG1(DBG_IMV, "     tag generation failed");
					break;
				}
				break;
			case SEC_UPDATE_STATE_END_PACKAGE:
				if (*pos != '\n')
				{
					continue;
				}
				free(package);
				free(version);
				free(download_uri);
				chunk_free(&deb);
				package = version = download_uri = NULL;

				if (!success)
				{
					fclose(file);
					db->destroy(db);
					exit(EXIT_FAILURE);
				}
				state = SEC_UPDATE_STATE_BEGIN_PACKAGE;
		}
	}

	free(package);
	free(version);
	free(download_uri);
	fclose(file);
	db->destroy(db);

	/* import swid tags into strongTNC */
	if (stats.new_versions > 0)
	{
		snprintf(command, BUF_LEN, "%s importswid %s",
				 tnc_manage_cmd, tmp_tag_file);
		if (system(command) != 0)
		{
			DBG1(DBG_IMV, "tag import failed");
		}
		snprintf(command, BUF_LEN, "rm %s %s",
				 tmp_deb_file, tmp_tag_file);
		if (system(command) != 0)
		{
			DBG1(DBG_IMV, "removing temporary files failed");
		}
	}

	DBG1(DBG_IMV, "processed \"%s\": %d packages, %d new versions, "
				  "%d updated versions", path, stats.packages,
				   stats.new_versions, stats.updated_versions);

	return (stats.new_versions + stats.updated_versions) ?
			EXIT_SUCCESS : EXIT_NO_UPDATES;
}

static int do_args(int argc, char *argv[])
{
	char *filename = NULL, *arch = NULL, *os = NULL, *uri = NULL;
	bool security = FALSE;

	/* reinit getopt state */
	optind = 0;

	while (TRUE)
	{
		int c;

		struct option long_opts[] = {
			{ "help", no_argument, NULL, 'h' },
			{ "arch", required_argument, NULL, 'a' },
			{ "debug", required_argument, NULL, 'd' },
			{ "file", required_argument, NULL, 'f' },
			{ "os", required_argument, NULL, 'o' },
			{ "quiet", no_argument, NULL, 'q' },
			{ "security", no_argument, NULL, 's' },
			{ "uri", required_argument, NULL, 'u' },
			{ 0,0,0,0 }
		};

		c = getopt_long(argc, argv, "ha:d:f:o:qsu:", long_opts, NULL);
		switch (c)
		{
			case EOF:
				break;
			case 'h':
				usage();
				exit(EXIT_SUCCESS);
			case 'a':
				arch = optarg;
				continue;
			case 'd':
				debug_level = atoi(optarg);
				continue;
			case 'f':
				filename = optarg;
				continue;
			case 'o':
				os = optarg;
				continue;
			case 'q':
				stderr_quiet = TRUE;
				continue;
			case 's':
				security = TRUE;
				continue;
			case 'u':
				uri = optarg;
				continue;
		}
		break;
	}

	if (filename && os && arch && uri)
	{
		return process_packages(filename, os, arch, uri, security);
	}
	else
	{
		usage();
		exit(EXIT_FAILURE);
	}
}

int main(int argc, char *argv[])
{
	/* enable attest debugging hook */
	dbg = sec_updater_dbg;
	openlog("sec-updater", 0, LOG_DEBUG);

	atexit(cleanup);

	/* initialize library */
	if (!library_init(NULL, "sec-updater"))
	{
		exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
	}
	if (!lib->plugins->load(lib->plugins,
			lib->settings->get_str(lib->settings, "sec-updater.load",
												  "sqlite curl")))
	{
		exit(SS_RC_INITIALIZATION_FAILED);
	}
	exit(do_args(argc, argv));
}