summaryrefslogtreecommitdiffstats
path: root/src/audit.c
blob: d61b3219819af0fdeaab5151e993107f650c8838 (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
/* 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 <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include "apk_applet.h"
#include "apk_database.h"

struct audit_ctx {
	int (*audit)(struct apk_database *db);
};

static int audit_file(struct apk_database *db, struct apk_db_file *dbf,
		      const char *name)
{
	struct apk_file_info fi;

	if (apk_file_get_info(db->root_fd, name, APK_FI_NOFOLLOW | dbf->csum.type, &fi) != 0)
		return 1;

	if (dbf->csum.type != APK_CHECKSUM_NONE &&
	    apk_checksum_compare(&fi.csum, &dbf->csum) == 0)
		return 0;

	if (S_ISLNK(fi.mode) && dbf->csum.type == APK_CHECKSUM_NONE)
		return 0;

	return 1;
}

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

	dir = fdopendir(openat(db->root_fd, dbd->name, O_RDONLY));
	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(db->root_fd, tmp, APK_FI_NOFOLLOW, &fi) < 0)
			continue;

		if ((dbd->flags & APK_DBDIRF_SYMLINKS_ONLY) &&
		    !S_ISLNK(fi.mode))
			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 (audit_file(db, dbf, tmp) == 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_backup(struct apk_database *db)
{
	return apk_hash_foreach(&db->installed.dirs, audit_directory, &db);
}

static int audit_system(struct apk_database *db)
{
	struct apk_package *pkg;
	struct apk_db_dir_instance *diri;
	struct apk_db_file *file;
	struct hlist_node *dn, *fn;
	char name[PATH_MAX];
	int done;

	list_for_each_entry(pkg, &db->installed.packages, installed_pkgs_list) {
		hlist_for_each_entry(diri, dn, &pkg->owned_dirs, pkg_dirs_list) {
			if (diri->dir->flags & APK_DBDIRF_PROTECTED)
				continue;

			done = 0;
			hlist_for_each_entry(file, fn, &diri->owned_files,
					     diri_files_list) {

				snprintf(name, sizeof(name), "%s/%s",
					 diri->dir->name, file->name);

				if (audit_file(db, file, name) == 0)
					continue;

				if (apk_verbosity < 1) {
					printf("%s\n", pkg->name->name);
					done = 1;
					break;
				}

				printf("M %s\n", name);
			}
			if (done)
				break;
		}
	}

	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->audit = audit_backup;
		break;
	case 0x10001:
		actx->audit = 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->audit == NULL)
		return -EINVAL;

	r = apk_db_open(&db, apk_root, APK_OPENF_READ);
	if (r != 0) {
		apk_error("APK database not present");
		return r;
	}
	r = actx->audit(&db);
	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", "Verify checksums of all installed files "
		             "(-q to print only modfied packages)" },
};

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);