summaryrefslogtreecommitdiff
path: root/pkgtools/pkg_install/files/admin/audit.c
blob: fe6fd14c9e8b0c46571d512413ab4a059ca9c4b3 (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
/*	$NetBSD: audit.c,v 1.8.2.7 2009/02/02 11:55:16 joerg Exp $	*/

#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <nbcompat.h>
#if HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
__RCSID("$NetBSD: audit.c,v 1.8.2.7 2009/02/02 11:55:16 joerg Exp $");

/*-
 * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_ERR_H
#include <err.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
#if HAVE_STDIO_H
#include <stdio.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#endif
#ifdef NETBSD
#include <unistd.h>
#else
#include <nbcompat/unistd.h>
#endif

#include <fetch.h>

#include "admin.h"
#include "lib.h"

static int check_eol = 0;
static int check_signature = 0;
static const char *limit_vul_types = NULL;

static struct pkg_vulnerabilities *pv;

static void
parse_options(int argc, char **argv)
{
	int ch;

	optreset = 1;
	/*
	 * optind == 0 is interpreted as partial reset request
	 * by GNU getopt, so compensate against this and cleanup
	 * at the end.
	 */
	optind = 1;
	++argc;
	--argv;

	while ((ch = getopt(argc, argv, "est:")) != -1) {
		switch (ch) {
		case 'e':
			check_eol = 1;
			break;
		case 's':
			check_signature = 1;
			break;
		case 't':
			limit_vul_types = optarg;
			break;
		default:
			usage();
			/* NOTREACHED */
		}
	}

	--optind; /* See above comment. */
}

static int
check_exact_pkg(const char *pkg)
{
	return audit_package(pv, pkg, limit_vul_types, check_eol,
	    quiet ? 0 : 1);
}

static int
check_batch_exact_pkgs(const char *fname)
{
	FILE *f;
	char buf[4096], *line, *eol;
	int ret;

	ret = 0;
	if (strcmp(fname, "-") == 0)
		f = stdin;
	else {
		f = fopen(fname, "r");
		if (f == NULL)
			err(EXIT_FAILURE, "Failed to open input file %s",
			    fname);
	}
	while ((line = fgets(buf, sizeof(buf), f)) != NULL) {
		eol = line + strlen(line);
		if (eol == line)
			continue;
		--eol;
		if (*eol == '\n') {
			if (eol == line)
				continue;
			*eol = '\0';
		}
		ret |= check_exact_pkg(line);
	}
	if (f != stdin)
		fclose(f);

	return ret;
}

static int
check_one_installed_pkg(const char *pkg, void *cookie)
{
	int *ret = cookie;

	*ret |= check_exact_pkg(pkg);
	return 0;
}

static int
check_installed_pattern(const char *pattern)
{
	int ret = 0;

	match_installed_pkgs(pattern, check_one_installed_pkg, &ret);

	return ret;
}

static void
check_and_read_pkg_vulnerabilities(void)
{
	struct stat st;
	time_t now;

	if (pkg_vulnerabilities_file == NULL)
		errx(EXIT_FAILURE, "PKG_VULNERABILITIES is not set");

	if (verbose >= 1) {
		if (stat(pkg_vulnerabilities_file, &st) == -1) {
			if (errno == ENOENT)
				errx(EXIT_FAILURE,
				    "pkg-vulnerabilities not found, run %s -d",
				    getprogname());
			errx(EXIT_FAILURE, "pkg-vulnerabilities not readable");
		}
		now = time(NULL);
		now -= st.st_mtime;
		if (now < 0)
			warnx("pkg-vulnerabilities is from the future");
		else if (now > 86400 * 7)
			warnx("pkg-vulnerabilities is out of day (%ld days old)",
			    (long)(now / 86400));
		else if (verbose >= 2)
			warnx("pkg-vulnerabilities is %ld day%s old",
			    (long)(now / 86400), now / 86400 == 1 ? "" : "s");
	}

	pv = read_pkg_vulnerabilities(pkg_vulnerabilities_file, 0, check_signature);
}

void
audit_pkgdb(int argc, char **argv)
{
	int rv;

	parse_options(argc, argv);
	argv += optind;

	check_and_read_pkg_vulnerabilities();

	rv = 0;
	if (*argv == NULL)
		rv |= check_installed_pattern("*");
	else {
		for (; *argv != NULL; ++argv)
			rv |= check_installed_pattern(*argv);
	}
	free_pkg_vulnerabilities(pv);

	if (rv == 0 && verbose >= 1)
		fputs("No vulnerabilities found\n", stderr);
	exit(rv ? EXIT_FAILURE : EXIT_SUCCESS);
}

void
audit_pkg(int argc, char **argv)
{
	int rv;

	parse_options(argc, argv);
	argv += optind;

	check_and_read_pkg_vulnerabilities();
	rv = 0;
	for (; *argv != NULL; ++argv)
		rv |= check_exact_pkg(*argv);

	free_pkg_vulnerabilities(pv);

	if (rv == 0 && verbose >= 1)
		fputs("No vulnerabilities found\n", stderr);
	exit(rv ? EXIT_FAILURE : EXIT_SUCCESS);
}

void
audit_batch(int argc, char **argv)
{
	int rv;

	parse_options(argc, argv);
	argv += optind;

	check_and_read_pkg_vulnerabilities();
	rv = 0;
	for (; *argv != NULL; ++argv)
		rv |= check_batch_exact_pkgs(*argv);
	free_pkg_vulnerabilities(pv);

	if (rv == 0 && verbose >= 1)
		fputs("No vulnerabilities found\n", stderr);
	exit(rv ? EXIT_FAILURE : EXIT_SUCCESS);
}

void
check_pkg_vulnerabilities(int argc, char **argv)
{
	parse_options(argc, argv);
	if (argc != optind + 1)
		usage();

	pv = read_pkg_vulnerabilities(argv[optind], 0, check_signature);
	free_pkg_vulnerabilities(pv);
}

void
fetch_pkg_vulnerabilities(int argc, char **argv)
{
	struct pkg_vulnerabilities *pv_check;
	char *buf, *decompressed_input;
	size_t buf_len, buf_fetched, decompressed_len;
	ssize_t cur_fetched;
	struct url_stat st;
	fetchIO *f;
	int fd;

	parse_options(argc, argv);
	if (argc != optind)
		usage();

	if (verbose >= 2)
		fprintf(stderr, "Fetching %s\n", pkg_vulnerabilities_url);

	f = fetchXGetURL(pkg_vulnerabilities_url, &st, fetch_flags);
	if (f == NULL)
		errx(EXIT_FAILURE, "Could not fetch vulnerability file: %s",
		    fetchLastErrString);

	if (st.size > SSIZE_MAX - 1)
		errx(EXIT_FAILURE, "pkg-vulnerabilities is too large");

	buf_len = st.size;
	buf = xmalloc(buf_len + 1);
	buf_fetched = 0;

	while (buf_fetched < buf_len) {
		cur_fetched = fetchIO_read(f, buf + buf_fetched,
		    buf_len - buf_fetched);
		if (cur_fetched == 0)
			errx(EXIT_FAILURE,
			    "Truncated pkg-vulnerabilities received");
		else if (cur_fetched == -1)
			errx(EXIT_FAILURE,
			    "IO error while fetching pkg-vulnerabilities: %s",
			    fetchLastErrString);
		buf_fetched += cur_fetched;
	}
	
	buf[buf_len] = '\0';

	if (decompress_buffer(buf, buf_len, &decompressed_input,
	    &decompressed_len)) {
		pv_check = parse_pkg_vulnerabilities(decompressed_input,
		    decompressed_len, check_signature);
		free(decompressed_input);
	} else {
		pv_check = parse_pkg_vulnerabilities(buf, buf_len,
		    check_signature);
	}
	free_pkg_vulnerabilities(pv_check);

	fd = open(pkg_vulnerabilities_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
	if (fd == -1)
		err(EXIT_FAILURE, "Cannot create pkg-vulnerability file %s",
		    pkg_vulnerabilities_file);

	if (write(fd, buf, buf_len) != buf_len)
		err(EXIT_FAILURE, "Cannot write pkg-vulnerability file");
	if (close(fd) == -1)
		err(EXIT_FAILURE, "Cannot close pkg-vulnerability file after write");

	free(buf);

	exit(EXIT_SUCCESS);
}

static int
check_pkg_history_pattern(const char *pkg, const char *pattern)
{
	const char *delim, *end_base;

	if ((delim = strchr(pattern, '*')) != NULL) {
		if ((end_base = strrchr(pattern, '-')) == NULL)
			errx(EXIT_FAILURE, "Missing - in wildcard pattern %s",
			    pattern);
		if ((delim = strchr(pattern, '>')) != NULL ||
		    (delim = strchr(pattern, '<')) != NULL)
			errx(EXIT_FAILURE,
			    "Mixed relational and wildcard patterns in %s",
			    pattern);
	} else if ((delim = strchr(pattern, '>')) != NULL) {
		end_base = delim;
		if ((delim = strchr(pattern, '<')) != NULL && delim < end_base)
			errx(EXIT_FAILURE, "Inverted operators in %s",
			    pattern);
	} else if ((delim = strchr(pattern, '<')) != NULL) {
		end_base = delim;
	} else if ((end_base = strrchr(pattern, '-')) == NULL) {
		errx(EXIT_FAILURE, "Missing - in absolute pattern %s",
		    pattern);
	}

	if (strncmp(pkg, pattern, end_base - pattern) != 0)
		return 0;
	if (pkg[end_base - pattern] != '\0')
		return 0;

	return 1;
}

static int
check_pkg_history1(const char *pkg, const char *pattern)
{
	const char *open_brace, *close_brace, *inner_brace, *suffix, *iter;
	size_t prefix_len, suffix_len, middle_len;
	char *expanded_pkg;

	open_brace = strchr(pattern, '{');
	if (open_brace == NULL) {
		if ((close_brace = strchr(pattern, '}')) != NULL)
			errx(EXIT_FAILURE, "Unbalanced {} in pattern %s",
			    pattern);
		return check_pkg_history_pattern(pkg, pattern);
	}
	close_brace = strchr(open_brace, '}');
	if (strchr(pattern, '}') != close_brace)
		errx(EXIT_FAILURE, "Unbalanced {} in pattern %s",
		    pattern);

	while ((inner_brace = strchr(open_brace + 1, '{')) != NULL) {
		if (inner_brace >= close_brace)
			break;
		open_brace = inner_brace;
	}

	expanded_pkg = xmalloc(strlen(pattern)); /* {} are going away... */

	prefix_len = open_brace - pattern;
	suffix = close_brace + 1;
	suffix_len = strlen(suffix) + 1;
	memcpy(expanded_pkg, pattern, prefix_len);

	++open_brace;

	do {
		iter = strchr(open_brace, ',');
		if (iter == NULL || iter > close_brace)
			iter = close_brace;

		middle_len = iter - open_brace;
		memcpy(expanded_pkg + prefix_len, open_brace, middle_len);
		memcpy(expanded_pkg + prefix_len + middle_len, suffix,
		    suffix_len);
		if (check_pkg_history1(pkg, expanded_pkg)) {
			free(expanded_pkg);
			return 1;
		}
		open_brace = iter + 1;
	} while (iter < close_brace);

	free(expanded_pkg);
	return 0;
}

static void
check_pkg_history(const char *pkg)
{
	size_t i;

	for (i = 0; i < pv->entries; ++i) {
		if (!quick_pkg_match(pv->vulnerability[i], pkg))
			continue;
		if (strcmp("eol", pv->classification[i]) == 0)
			continue;
		if (check_pkg_history1(pkg, pv->vulnerability[i]) == 0)
			continue;

		printf("%s %s %s\n", pv->vulnerability[i],
		    pv->classification[i], pv->advisory[i]);
	}
}

void
audit_history(int argc, char **argv)
{
	parse_options(argc, argv);
	argv += optind;

	check_and_read_pkg_vulnerabilities();
	for (; *argv != NULL; ++argv)
		check_pkg_history(*argv);

	free_pkg_vulnerabilities(pv);
	exit(EXIT_SUCCESS);
}