aboutsummaryrefslogtreecommitdiffstats
path: root/src/audit.c
blob: fe72c88d6e4b898f290e7168337875c82430fb93 (plain) (blame)
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
/* audit.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
 * Copyright (C) 2008 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation. See http://www.gnu.org/ for details.
 */

#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include "apk_applet.h"
#include "apk_database.h"

#define AUDIT_BACKUP BIT(0)
#define AUDIT_SYSTEM BIT(1)

struct audit_ctx {
	int type;
	struct apk_database *db;
};

static int audit_directory(apk_hash_item item, void *ctx)
{
	struct apk_db_dir *dbd = (struct apk_db_dir *) item;
	struct apk_db_file *dbf;
	struct audit_ctx *actx = (struct audit_ctx *) ctx;
	struct apk_database *db = actx->db;
	struct dirent *de;
	struct apk_file_info fi;
	apk_blob_t bdir = APK_BLOB_PTR_LEN(dbd->name, dbd->namelen);
	char tmp[512], reason;
	DIR *dir;

	if (!(actx->type & AUDIT_SYSTEM) && !(dbd->flags & APK_DBDIRF_PROTECTED))
		return 0;
	if (!(actx->type & AUDIT_BACKUP) && (dbd->flags & APK_DBDIRF_PROTECTED))
		return 0;

	dir = opendir(dbd->name);
	if (dir == NULL)
		return 0;

	while ((de = readdir(dir)) != NULL) {
		if (strcmp(de->d_name, ".") == 0 ||
		    strcmp(de->d_name, "..") == 0)
			continue;

		snprintf(tmp, sizeof(tmp), "%s/%s", dbd->name, de->d_name);

		if (apk_file_get_info(tmp, APK_CHECKSUM_NONE, &fi) < 0)
			continue;

		if (S_ISDIR(fi.mode)) {
			if (apk_db_dir_query(db, APK_BLOB_STR(tmp)) != NULL)
				continue;

			reason = 'D';
		} else {
			dbf = apk_db_file_query(db, bdir, APK_BLOB_STR(de->d_name));
			if (dbf != NULL) {
				if (apk_file_get_info(tmp, dbf->csum.type, &fi) < 0)
					continue;

				if (apk_checksum_compare(&fi.csum, &dbf->csum) == 0)
					continue;

				reason = 'U';
			} else {
				reason = 'A';
			}
		}
		if (apk_verbosity < 1)
			printf("%s\n", tmp);
		else
			printf("%c %s\n", reason, tmp);
	}

	closedir(dir);
	return 0;
}

static int audit_parse(void *ctx, int optch, int optindex, const char *optarg)
{
	struct audit_ctx *actx = (struct audit_ctx *) ctx;

	switch (optch) {
	case 0x10000:
		actx->type |= AUDIT_BACKUP;
		break;
	case 0x10001:
		actx->type |= AUDIT_SYSTEM;
		break;
	default:
		return -1;
	}
	return 0;
}

static int audit_main(void *ctx, int argc, char **argv)
{
	struct audit_ctx *actx = (struct audit_ctx *) ctx;
	struct apk_database db;
	int r;

	if (actx->type == 0)
		return -EINVAL;

	r = apk_db_open(&db, apk_root, APK_OPENF_READ);
	if (r != 0) {
		apk_error("APK database not present");
		return 1;
	}

	actx->db = &db;

	fchdir(db.root_fd);
	r = apk_hash_foreach(&db.installed.dirs, audit_directory, actx);

	apk_db_close(&db);
	return r;
}

static struct apk_option audit_options[] = {
	{ 0x10000, "backup",
	  "List all modified configuration files that need to be backed up" },
	{ 0x10001, "system", "List all modified non-protected files" },
};

static struct apk_applet apk_audit = {
	.name = "audit",
	.help = "Audit the filesystem for changes compared to installed "
		"database.",
	.context_size = sizeof(struct audit_ctx),
	.num_options = ARRAY_SIZE(audit_options),
	.options = audit_options,
	.parse = audit_parse,
	.main = audit_main,
};

APK_DEFINE_APPLET(apk_audit);