-
Notifications
You must be signed in to change notification settings - Fork 30
/
qcheck.c
504 lines (448 loc) · 13.4 KB
/
qcheck.c
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
/*
* Copyright 2005-2024 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
*
* Copyright 2005-2010 Ned Ludd - <solar@gentoo.org>
* Copyright 2005-2014 Mike Frysinger - <vapier@gentoo.org>
* Copyright 2018- Fabian Groffen - <grobian@gentoo.org>
*/
#include "main.h"
#include "applets.h"
#include <sys/types.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "atom.h"
#include "contents.h"
#include "copy_file.h"
#include "hash.h"
#include "prelink.h"
#include "tree.h"
#include "xarray.h"
#include "xasprintf.h"
#include "xregex.h"
#define QCHECK_FORMAT "%[CATEGORY]%[PN]"
#define QCHECK_FORMAT_VERBOSE "%[CATEGORY]%[PF]"
#define QCHECK_FLAGS "F:s:uABHTPp" COMMON_FLAGS
static struct option const qcheck_long_opts[] = {
{"format", a_argument, NULL, 'F'},
{"skip", a_argument, NULL, 's'},
{"update", no_argument, NULL, 'u'},
{"noafk", no_argument, NULL, 'A'},
{"badonly", no_argument, NULL, 'B'},
{"nohash", no_argument, NULL, 'H'},
{"nomtime", no_argument, NULL, 'T'},
{"skip-protected", no_argument, NULL, 'P'},
{"prelink", no_argument, NULL, 'p'},
COMMON_LONG_OPTS
};
static const char * const qcheck_opts_help[] = {
"Custom output format (default: " QCHECK_FORMAT ")",
"Ignore files matching the regular expression <arg>",
"Update missing files, chksum and mtimes for packages",
"Ignore missing files",
"Only print pkgs containing bad files",
"Ignore differing/unknown file chksums",
"Ignore differing file mtimes",
"Ignore files in CONFIG_PROTECT-ed paths",
"Undo prelink when calculating checksums",
COMMON_OPTS_HELP
};
#define qcheck_usage(ret) usage(ret, QCHECK_FLAGS, qcheck_long_opts, qcheck_opts_help, NULL, lookup_applet_idx("qcheck"))
#define qcprintf(fmt, args...) do { if (!state->bad_only) printf(fmt, ## args); } while (0)
struct qcheck_opt_state {
array_t *atoms;
array_t *regex_arr;
bool bad_only;
bool qc_update;
bool chk_afk;
bool chk_hash;
bool chk_mtime;
bool chk_config_protect;
bool undo_prelink;
const char *fmt;
};
static int
qcheck_cb(tree_pkg_ctx *pkg_ctx, void *priv)
{
struct qcheck_opt_state *state = priv;
struct stat st;
depend_atom *atom;
FILE *fp_contents_update = NULL;
size_t num_files = 0;
size_t num_files_ok = 0;
size_t num_files_unknown = 0;
size_t num_files_ignored = 0;
char *buffer;
char *line;
char *savep;
char *eprefix = NULL;
size_t eprefix_len = 0;
int cp_argc;
int cpm_argc;
char **cp_argv;
char **cpm_argv;
/* get CONTENTS from meta */
line = tree_pkg_meta_get(pkg_ctx, CONTENTS);
if (line == NULL)
return EXIT_FAILURE;
atom = tree_get_atom(pkg_ctx, false);
qcprintf("%sing %s ...\n",
(state->qc_update ? "Updat" : "Check"),
atom_format(state->fmt, atom));
/* Open contents_update, if needed */
if (state->qc_update) {
char tempfile[] = "qcheck-tmp-XXXXXX";
mode_t mask;
int fd;
mask = umask(0077);
fd = mkstemp(tempfile);
umask(mask);
if (fd == -1 || (fp_contents_update = fdopen(fd, "w+")) == NULL) {
if (fd >= 0)
close(fd);
warnp("unable to temp file");
return EXIT_FAILURE;
}
/* like tmpfile() does, but Coverity thinks it is unsafe */
unlink(tempfile);
}
if (!state->chk_config_protect) {
makeargv(config_protect, &cp_argc, &cp_argv);
makeargv(config_protect_mask, &cpm_argc, &cpm_argv);
eprefix = tree_pkg_meta_get(pkg_ctx, EPREFIX);
if (eprefix != NULL)
eprefix_len = strlen(eprefix);
}
buffer = NULL;
for (; (line = strtok_r(line, "\n", &savep)) != NULL; line = NULL) {
contents_entry *entry;
free(buffer);
buffer = xstrdup(line);
entry = contents_parse_line(line);
if (!entry)
continue;
num_files++;
/* handle skips */
if (array_cnt(state->regex_arr)) {
size_t n;
regex_t *regex;
array_for_each(state->regex_arr, n, regex)
if (!regexec(regex, entry->name, 0, NULL, 0))
break;
if (n < array_cnt(state->regex_arr)) {
num_files--;
num_files_ignored++;
if (verbose)
qcprintf(" %sSKIP%s %s: matches regex\n",
YELLOW, NORM, entry->name);
if (state->qc_update)
fprintf(fp_contents_update, "%s\n", buffer);
continue;
}
}
/* handle CONFIG_PROTECT-ed files */
if (!state->chk_config_protect) {
int i;
char *p;
/* compute path without EPREFIX */
p = entry->name;
if (strlen(p) > eprefix_len)
p += eprefix_len;
/* if in CONFIG_PROTECT_MASK, handle like normal */
for (i = 1; i < cpm_argc; ++i) {
if (strncmp(cpm_argv[i], p, strlen(cpm_argv[i])) == 0)
break;
}
if (i == cpm_argc) {
/* not explicitly unmasked, check if it's protected */
for (i = 1; i < cp_argc; ++i) {
if (strncmp(cp_argv[i], p, strlen(cp_argv[i])) == 0) {
num_files--;
num_files_ignored++;
if (verbose)
qcprintf(" %sSKIP%s %s: protected via %s\n",
YELLOW, NORM, entry->name, cp_argv[i]);
if (state->qc_update)
fprintf(fp_contents_update, "%s\n", buffer);
break;
}
}
if (i != cp_argc)
continue;
}
}
/* check file existence */
if (fstatat(pkg_ctx->cat_ctx->ctx->portroot_fd, entry->name + 1,
&st, AT_SYMLINK_NOFOLLOW) != 0)
{
if (state->chk_afk) {
if (errno == ENOENT)
qcprintf(" %sAFK%s: %s\n", RED, NORM, entry->name);
else
qcprintf(" %sERROR (%s)%s: %s\n", RED,
strerror(errno), NORM, entry->name);
} else {
num_files--;
num_files_ignored++;
if (verbose)
qcprintf(" %sSKIP%s %s: %s\n",
YELLOW, NORM, entry->name, strerror(errno));
if (state->qc_update)
fprintf(fp_contents_update, "%s\n", buffer);
}
continue;
}
/* for certain combinations of flags and filetypes, a file
* won't get checks and should be ignored */
if (!state->chk_mtime && entry->type == CONTENTS_SYM) {
num_files--;
num_files_ignored++;
if (verbose)
qcprintf(" %sSKIP%s %s: symlink and no mtime check\n",
YELLOW, NORM, entry->name);
if (state->qc_update)
fprintf(fp_contents_update, "%s\n", buffer);
continue;
}
/* Digest checks only work on regular files
* Note: We don't check for state->chk_hash when entering
* but rather in digest-check #3, because we only succeed
* tests/qcheck/list04.good if when chk_hash is false, we
* do check hashes, but only print mismatched digests as
* 'ignored file'. */
if (entry->digest && S_ISREG(st.st_mode)) {
char *f_digest;
int hash_algo;
/* Validate digest (handles MD5 / SHA1)
* Digest-check 1/3:
* should we check digests? */
switch (strlen(entry->digest)) {
case 32: hash_algo = (int)HASH_MD5; break;
case 40: hash_algo = (int)HASH_SHA1; break;
default: hash_algo = 0; break;
}
if (hash_algo == 0) {
if (state->chk_hash) {
qcprintf(" %sUNKNOWN DIGEST%s: '%s' for '%s'\n",
RED, NORM, entry->digest, entry->name);
num_files_unknown++;
} else {
num_files--;
num_files_ignored++;
if (verbose)
qcprintf(" %sSKIP%s %s: unknown digest\n",
YELLOW, NORM, entry->name);
if (state->qc_update)
fprintf(fp_contents_update, "%s\n", buffer);
}
continue;
}
/* compute hash for file */
hash_cb_t hash_cb =
state->undo_prelink ? hash_cb_prelink_undo : NULL;
f_digest = hash_file_at_cb(
pkg_ctx->cat_ctx->ctx->portroot_fd,
entry->name + 1, hash_algo, hash_cb);
/* Digest-check 2/3:
* do we have digest of the file? */
if (f_digest == NULL) {
num_files_unknown++;
if (state->qc_update)
fprintf(fp_contents_update, "%s\n", buffer);
if (verbose)
qcprintf(" %sPERM %4o%s: %s\n",
RED, (unsigned int)(st.st_mode & 07777),
NORM, entry->name);
continue;
}
/* Digest-check 3/3:
* does the digest equal what portage recorded? */
if (strcmp(entry->digest, f_digest) != 0) {
if (state->chk_hash) {
const char *digest_disp;
if (state->qc_update)
fprintf(fp_contents_update, "obj %s %s %llu\n",
entry->name, f_digest,
(long long int)st.st_mtime);
switch (hash_algo) {
case HASH_MD5: digest_disp = "MD5"; break;
case HASH_SHA1: digest_disp = "SHA1"; break;
default: digest_disp = "UNK"; break;
}
qcprintf(" %s%s-DIGEST%s: %s",
RED, digest_disp, NORM, entry->name);
if (verbose)
qcprintf(" (recorded '%s' != actual '%s')",
entry->digest, f_digest);
qcprintf("\n");
} else {
num_files--;
num_files_ignored++;
if (verbose)
qcprintf(" %sSKIP%s %s: digest mismatch "
"but check disabled\n",
YELLOW, NORM, entry->name);
if (state->qc_update)
fprintf(fp_contents_update, "%s\n", buffer);
}
continue;
}
}
/* validate mtimes */
if (entry->mtime && entry->mtime != st.st_mtime) {
if (state->chk_mtime) {
qcprintf(" %sMTIME%s: %s", RED, NORM, entry->name);
if (verbose)
qcprintf(" (recorded '%llu' != actual '%llu')",
(long long int)entry->mtime,
(long long int)st.st_mtime);
qcprintf("\n");
/* Update mtime */
if (state->qc_update) {
if (entry->type == CONTENTS_SYM) {
fprintf(fp_contents_update, "sym %s -> %s %llu\n",
entry->name, entry->sym_target,
(long long int)st.st_mtime);
} else {
fprintf(fp_contents_update, "obj %s %s %llu\n",
entry->name, entry->digest,
(long long int)st.st_mtime);
}
}
continue;
} else {
num_files--;
num_files_ignored++;
if (verbose)
qcprintf(" %sSKIP%s %s: mtime mismatch "
"but check disabled\n",
YELLOW, NORM, entry->name);
if (state->qc_update)
fprintf(fp_contents_update, "%s\n", buffer);
continue;
}
}
/* success! */
if (state->qc_update)
fprintf(fp_contents_update, "%s\n", buffer);
num_files_ok++;
}
free(buffer);
if (!state->chk_config_protect) {
freeargv(cp_argc, cp_argv);
freeargv(cpm_argc, cpm_argv);
}
if (state->qc_update) {
int fd_contents;
FILE *fp_contents;
/* O_TRUNC truncates, but file owner and mode are unchanged */
fd_contents = openat(pkg_ctx->fd, "CONTENTS", O_WRONLY | O_TRUNC);
if (fd_contents < 0 ||
(fp_contents = fdopen(fd_contents, "w")) == NULL)
{
fclose(fp_contents_update);
warn("could not open CONTENTS for writing");
return EXIT_FAILURE;
}
/* rewind tempfile */
fseek(fp_contents_update, 0, SEEK_SET);
copy_file(fp_contents_update, fp_contents);
fclose(fp_contents_update);
fclose(fp_contents);
if (!verbose)
return EXIT_SUCCESS;
}
if (state->bad_only && num_files_ok != num_files)
printf("%s\n", atom_format(state->fmt, atom));
qcprintf(" %2$s*%1$s %3$s%4$zu%1$s out of %3$s%5$zu%1$s file%6$s are good",
NORM, BOLD, BLUE, num_files_ok, num_files,
(num_files != 1 ? "s" : ""));
if (num_files_unknown)
qcprintf(" (Unable to digest %2$s%3$zu%1$s file%4$s)",
NORM, BLUE, num_files_unknown,
(num_files_unknown > 1 ? "s" : ""));
if (num_files_ignored)
qcprintf(" (%2$s%3$zu%1$s file%4$s ignored)",
NORM, BLUE, num_files_ignored,
(num_files_ignored > 1 ? "s were" : " was"));
qcprintf("\n");
if (num_files_ok != num_files && !state->qc_update)
return EXIT_FAILURE;
else
return EXIT_SUCCESS;
}
int qcheck_main(int argc, char **argv)
{
size_t i;
int ret;
tree_ctx *vdb;
DECLARE_ARRAY(regex_arr);
depend_atom *atom;
DECLARE_ARRAY(atoms);
struct qcheck_opt_state state = {
.atoms = atoms,
.regex_arr = regex_arr,
.bad_only = false,
.qc_update = false,
.chk_afk = true,
.chk_hash = true,
.chk_mtime = true,
.chk_config_protect = true,
.undo_prelink = false,
.fmt = NULL,
};
while ((ret = GETOPT_LONG(QCHECK, qcheck, "")) != -1) {
switch (ret) {
COMMON_GETOPTS_CASES(qcheck)
case 's': {
regex_t preg;
xregcomp(&preg, optarg, REG_EXTENDED | REG_NOSUB);
xarraypush(regex_arr, &preg, sizeof(preg));
break;
}
case 'u': state.qc_update = true; break;
case 'A': state.chk_afk = false; break;
case 'B': state.bad_only = true; break;
case 'H': state.chk_hash = false; break;
case 'T': state.chk_mtime = false; break;
case 'P': state.chk_config_protect = false; break;
case 'p': state.undo_prelink = prelink_available(); break;
case 'F': state.fmt = optarg; break;
}
}
if (state.fmt == NULL)
state.fmt = verbose ? QCHECK_FORMAT_VERBOSE : QCHECK_FORMAT;
argc -= optind;
argv += optind;
for (i = 0; i < (size_t)argc; ++i) {
atom = atom_explode(argv[i]);
if (!atom)
warn("invalid atom: %s", argv[i]);
else
xarraypush_ptr(atoms, atom);
}
vdb = tree_open_vdb(portroot, portvdb);
ret = -1;
if (vdb != NULL) {
if (array_cnt(atoms) != 0) {
ret = 0;
array_for_each(atoms, i, atom) {
ret |= tree_foreach_pkg_sorted(vdb, qcheck_cb, &state, atom);
}
} else {
ret = tree_foreach_pkg_sorted(vdb, qcheck_cb, &state, NULL);
}
tree_close(vdb);
}
if (array_cnt(regex_arr) > 0) {
void *preg;
array_for_each(regex_arr, i, preg)
regfree(preg);
}
xarrayfree(regex_arr);
array_for_each(atoms, i, atom)
atom_implode(atom);
xarrayfree_int(atoms);
return ret != 0;
}