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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
|
/** \ingroup rpmcli
* \file lib/verify.c
* Verify installed payload files from package metadata.
*/
#include "system.h"
#include "psm.h"
#include <rpmcli.h>
#include "legacy.h" /* XXX mdfile(), uidToUname(), gnameToGid */
#include "ugid.h"
#include "misc.h" /* XXX for uidToUname() and gnameToGid() */
#include "debug.h"
/*@access rpmTransactionSet*/
/*@access TFI_t*/
/*@access PSM_t*/
/*@access FD_t*/ /* XXX compared with NULL */
#define S_ISDEV(m) (S_ISBLK((m)) || S_ISCHR((m)))
int rpmVerifyFile(const char * root, Header h, int filenum,
rpmVerifyAttrs * result, rpmVerifyAttrs omitMask)
{
HGE_t hge = (HGE_t)headerGetEntryMinMemory;
HFD_t hfd = headerFreeData;
int_32 * fileFlags;
rpmfileAttrs fileAttrs = RPMFILE_NONE;
int_32 * verifyFlags;
rpmVerifyAttrs flags = RPMVERIFY_ALL;
unsigned short * modeList;
const char * fileStatesList;
const char * filespec = NULL;
int count;
int rc;
struct stat sb;
rc = hge(h, RPMTAG_FILEMODES, NULL, (void **) &modeList, &count);
if (hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fileFlags, NULL))
fileAttrs = fileFlags[filenum];
if (hge(h, RPMTAG_FILEVERIFYFLAGS, NULL, (void **) &verifyFlags, NULL))
flags = verifyFlags[filenum];
{
const char ** baseNames;
const char ** dirNames;
int_32 * dirIndexes;
rpmTagType bnt, dnt;
if (hge(h, RPMTAG_BASENAMES, &bnt, (void **) &baseNames, NULL)
&& hge(h, RPMTAG_DIRNAMES, &dnt, (void **) &dirNames, NULL)
&& hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &dirIndexes, NULL))
{
int nb = (strlen(dirNames[dirIndexes[filenum]]) +
strlen(baseNames[filenum]) + strlen(root) + 5);
char * t = alloca(nb);
filespec = t;
*t = '\0';
if (root && !(root[0] == '/' && root[1] == '\0')) {
t = stpcpy(t, root);
while (t > filespec && t[-1] == '/') {
--t;
*t = '\0';
}
}
t = stpcpy(t, dirNames[dirIndexes[filenum]]);
t = stpcpy(t, baseNames[filenum]);
}
baseNames = hfd(baseNames, bnt);
dirNames = hfd(dirNames, dnt);
}
*result = RPMVERIFY_NONE;
/*
* Check to see if the file was installed - if not pretend all is OK.
*/
if (hge(h, RPMTAG_FILESTATES, NULL, (void **) &fileStatesList, NULL) &&
fileStatesList != NULL)
{
rpmfileState fstate = fileStatesList[filenum];
switch (fstate) {
case RPMFILE_STATE_NETSHARED:
case RPMFILE_STATE_REPLACED:
case RPMFILE_STATE_NOTINSTALLED:
return 0;
/*@notreached@*/ break;
case RPMFILE_STATE_NORMAL:
break;
}
}
if (filespec == NULL) {
*result |= RPMVERIFY_LSTATFAIL;
return 1;
}
if (Lstat(filespec, &sb) != 0) {
*result |= RPMVERIFY_LSTATFAIL;
return 1;
}
/*
* Not all attributes of non-regular files can be verified.
*/
if (S_ISDIR(sb.st_mode))
flags &= ~(RPMVERIFY_MD5 | RPMVERIFY_FILESIZE | RPMVERIFY_MTIME |
RPMVERIFY_LINKTO);
else if (S_ISLNK(sb.st_mode)) {
flags &= ~(RPMVERIFY_MD5 | RPMVERIFY_FILESIZE | RPMVERIFY_MTIME |
RPMVERIFY_MODE);
#if CHOWN_FOLLOWS_SYMLINK
flags &= ~(RPMVERIFY_USER | RPMVERIFY_GROUP);
#endif
}
else if (S_ISFIFO(sb.st_mode))
flags &= ~(RPMVERIFY_MD5 | RPMVERIFY_FILESIZE | RPMVERIFY_MTIME |
RPMVERIFY_LINKTO);
else if (S_ISCHR(sb.st_mode))
flags &= ~(RPMVERIFY_MD5 | RPMVERIFY_FILESIZE | RPMVERIFY_MTIME |
RPMVERIFY_LINKTO);
else if (S_ISBLK(sb.st_mode))
flags &= ~(RPMVERIFY_MD5 | RPMVERIFY_FILESIZE | RPMVERIFY_MTIME |
RPMVERIFY_LINKTO);
else
flags &= ~(RPMVERIFY_LINKTO);
/*
* Content checks of %ghost files are meaningless.
*/
if (fileAttrs & RPMFILE_GHOST)
flags &= ~(RPMVERIFY_MD5 | RPMVERIFY_FILESIZE | RPMVERIFY_MTIME |
RPMVERIFY_LINKTO);
/*
* Don't verify any features in omitMask.
*/
flags &= ~(omitMask | RPMVERIFY_LSTATFAIL|RPMVERIFY_READFAIL|RPMVERIFY_READLINKFAIL);
if (flags & RPMVERIFY_MD5) {
unsigned char md5sum[40];
const char ** md5List;
rpmTagType mdt;
if (!hge(h, RPMTAG_FILEMD5S, &mdt, (void **) &md5List, NULL))
*result |= RPMVERIFY_MD5;
else {
rc = mdfile(filespec, md5sum);
if (rc)
*result |= (RPMVERIFY_READFAIL|RPMVERIFY_MD5);
else if (strcmp(md5sum, md5List[filenum]))
*result |= RPMVERIFY_MD5;
}
md5List = hfd(md5List, mdt);
}
if (flags & RPMVERIFY_LINKTO) {
char linkto[1024];
int size = 0;
const char ** linktoList;
rpmTagType ltt;
if (!hge(h, RPMTAG_FILELINKTOS, <t, (void **) &linktoList, NULL)
|| (size = Readlink(filespec, linkto, sizeof(linkto)-1)) == -1)
*result |= (RPMVERIFY_READLINKFAIL|RPMVERIFY_LINKTO);
else {
linkto[size] = '\0';
if (strcmp(linkto, linktoList[filenum]))
*result |= RPMVERIFY_LINKTO;
}
linktoList = hfd(linktoList, ltt);
}
if (flags & RPMVERIFY_FILESIZE) {
int_32 * sizeList;
if (!hge(h, RPMTAG_FILESIZES, NULL, (void **) &sizeList, NULL)
|| sizeList[filenum] != sb.st_size)
*result |= RPMVERIFY_FILESIZE;
}
if (flags & RPMVERIFY_MODE) {
unsigned short metamode = modeList[filenum];
unsigned short filemode;
/*
* Platforms (like AIX) where sizeof(unsigned short) != sizeof(mode_t)
* need the (unsigned short) cast here.
*/
filemode = (unsigned short)sb.st_mode;
/*
* Comparing the type of %ghost files is meaningless, but perms are OK.
*/
if (fileAttrs & RPMFILE_GHOST) {
metamode &= ~0xf000;
filemode &= ~0xf000;
}
if (metamode != filemode)
*result |= RPMVERIFY_MODE;
}
if (flags & RPMVERIFY_RDEV) {
if (S_ISCHR(modeList[filenum]) != S_ISCHR(sb.st_mode) ||
S_ISBLK(modeList[filenum]) != S_ISBLK(sb.st_mode))
{
*result |= RPMVERIFY_RDEV;
} else if (S_ISDEV(modeList[filenum]) && S_ISDEV(sb.st_mode)) {
unsigned short * rdevList;
if (!hge(h, RPMTAG_FILERDEVS, NULL, (void **) &rdevList, NULL)
|| rdevList[filenum] != sb.st_rdev)
*result |= RPMVERIFY_RDEV;
}
}
if (flags & RPMVERIFY_MTIME) {
int_32 * mtimeList;
if (!hge(h, RPMTAG_FILEMTIMES, NULL, (void **) &mtimeList, NULL)
|| mtimeList[filenum] != sb.st_mtime)
*result |= RPMVERIFY_MTIME;
}
if (flags & RPMVERIFY_USER) {
const char * name;
const char ** unameList;
int_32 * uidList;
rpmTagType unt;
if (hge(h, RPMTAG_FILEUSERNAME, &unt, (void **) &unameList, NULL)) {
name = uidToUname(sb.st_uid);
if (!name || strcmp(unameList[filenum], name))
*result |= RPMVERIFY_USER;
unameList = hfd(unameList, unt);
} else if (hge(h, RPMTAG_FILEUIDS, NULL, (void **) &uidList, NULL)) {
if (uidList[filenum] != sb.st_uid)
*result |= RPMVERIFY_GROUP;
} else {
rpmError(RPMERR_INTERNAL, _("package lacks both user name and id "
"lists (this should never happen)\n"));
*result |= RPMVERIFY_GROUP;
}
}
if (flags & RPMVERIFY_GROUP) {
const char ** gnameList;
int_32 * gidList;
rpmTagType gnt;
gid_t gid;
if (hge(h, RPMTAG_FILEGROUPNAME, &gnt, (void **) &gnameList, NULL)) {
rc = gnameToGid(gnameList[filenum], &gid);
if (rc || (gid != sb.st_gid))
*result |= RPMVERIFY_GROUP;
gnameList = hfd(gnameList, gnt);
} else if (hge(h, RPMTAG_FILEGIDS, NULL, (void **) &gidList, NULL)) {
if (gidList[filenum] != sb.st_gid)
*result |= RPMVERIFY_GROUP;
} else {
rpmError(RPMERR_INTERNAL, _("package lacks both group name and id "
"lists (this should never happen)\n"));
*result |= RPMVERIFY_GROUP;
}
}
return 0;
}
/**
* Return exit code from running verify script from header.
* @param qva parsed query/verify options
* @param ts transaction set
* @param h header
* @param scriptFd file handle to use for stderr (or NULL)
* @return 0 on success
*/
static int rpmVerifyScript(/*@unused@*/ QVA_t qva, rpmTransactionSet ts,
Header h, /*@null@*/ FD_t scriptFd)
/*@globals rpmGlobalMacroContext,
fileSystem, internalState @*/
/*@modifies ts, h, rpmGlobalMacroContext,
fileSystem, internalState @*/
{
TFI_t fi = xcalloc(1, sizeof(*fi));
PSM_t psm = memset(alloca(sizeof(*psm)), 0, sizeof(*psm));
int rc;
psm->ts = rpmtsLink(ts);
if (scriptFd != NULL) {
/*@-type@*/ /* FIX: ??? */
ts->scriptFd = fdLink(scriptFd, "rpmVerifyScript");
/*@=type@*/
}
fi->magic = TFIMAGIC;
loadFi(ts, fi, h, 1);
memset(psm, 0, sizeof(*psm));
/*@-assignexpose@*/
psm->fi = fi;
/*@=assignexpose@*/
psm->stepName = "verify";
psm->scriptTag = RPMTAG_VERIFYSCRIPT;
psm->progTag = RPMTAG_VERIFYSCRIPTPROG;
rc = psmStage(psm, PSM_SCRIPT);
freeFi(fi);
fi = _free(fi);
if (scriptFd != NULL) {
/*@-type@*/ /* FIX: ??? */
ts->scriptFd = fdFree(ts->scriptFd, "rpmVerifyScript");
/*@=type@*/
ts->scriptFd = NULL;
}
rpmtransClean(ts); /* XXX this is sure to cause heartburn */
psm->ts = rpmtsUnlink(ts);
return rc;
}
int rpmVerifyDigest(Header h)
{
HGE_t hge = (HGE_t)headerGetEntry; /* XXX headerGetEntryMinMemory? */
HFD_t hfd = headerFreeData;
void * uh = NULL;
rpmTagType uht;
int_32 uhc;
const char * hdigest;
rpmTagType hdt;
int ec = 0; /* assume no problems */
/* Retrieve header digest. */
if (!hge(h, RPMTAG_SHA1HEADER, &hdt, (void **) &hdigest, NULL)
&&!hge(h, RPMTAG_SHA1RHN, &hdt, (void **) &hdigest, NULL))
{
return 0;
}
/* Regenerate original header. */
if (!hge(h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc))
return 0;
if (hdigest == NULL || uh == NULL)
return 0;
/* Compute header digest. */
{ DIGEST_CTX ctx = rpmDigestInit(PGPHASHALGO_SHA1, RPMDIGEST_NONE);
const char * digest;
size_t digestlen;
(void) rpmDigestUpdate(ctx, uh, uhc);
(void) rpmDigestFinal(ctx, (void **)&digest, &digestlen, 1);
/* XXX can't happen: report NULL malloc return as a digest failure. */
ec = (digest == NULL || strcmp(hdigest, digest)) ? 1 : 0;
digest = _free(digest);
}
uh = hfd(uh, uht);
hdigest = hfd(hdigest, hdt);
return ec;
}
/**
* Check file info from header against what's actually installed.
* @param qva parsed query/verify options
* @param ts transaction set
* @param h header
* @return 0 no problems, 1 problems found
*/
static int verifyHeader(QVA_t qva, /*@unused@*/ rpmTransactionSet ts, Header h)
/*@globals fileSystem@*/
/*@modifies h, fileSystem @*/
{
HGE_t hge = (HGE_t)headerGetEntryMinMemory;
char buf[BUFSIZ];
char * t, * te;
const char ** fileNames = NULL;
int count;
int_32 * fileFlags = NULL;
rpmVerifyAttrs verifyResult = 0;
/*@-type@*/ /* FIX: union? */
rpmVerifyAttrs omitMask = ((qva->qva_flags & VERIFY_ATTRS) ^ VERIFY_ATTRS);
/*@=type@*/
int ec = 0; /* assume no problems */
int i;
te = t = buf;
*te = '\0';
if (!hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fileFlags, NULL))
goto exit;
if (!headerIsEntry(h, RPMTAG_BASENAMES))
goto exit;
rpmBuildFileList(h, &fileNames, &count);
for (i = 0; i < count; i++) {
rpmfileAttrs fileAttrs;
int rc;
fileAttrs = fileFlags[i];
/* If not verifying %ghost, skip ghost files. */
if (!(qva->qva_fflags & RPMFILE_GHOST)
&& (fileAttrs & RPMFILE_GHOST))
continue;
rc = rpmVerifyFile(ts->rootDir, h, i, &verifyResult, omitMask);
if (rc) {
/*@-internalglobs@*/ /* FIX: shrug */
if (!(fileAttrs & RPMFILE_MISSINGOK) || rpmIsVerbose()) {
sprintf(te, _("missing %s"), fileNames[i]);
te += strlen(te);
ec = rc;
}
/*@=internalglobs@*/
} else if (verifyResult) {
const char * size, * md5, * link, * mtime, * mode;
const char * group, * user, * rdev;
/*@observer@*/ static const char *const aok = ".";
/*@observer@*/ static const char *const unknown = "?";
ec = 1;
#define _verify(_RPMVERIFY_F, _C) \
((verifyResult & _RPMVERIFY_F) ? _C : aok)
#define _verifylink(_RPMVERIFY_F, _C) \
((verifyResult & RPMVERIFY_READLINKFAIL) ? unknown : \
(verifyResult & _RPMVERIFY_F) ? _C : aok)
#define _verifyfile(_RPMVERIFY_F, _C) \
((verifyResult & RPMVERIFY_READFAIL) ? unknown : \
(verifyResult & _RPMVERIFY_F) ? _C : aok)
md5 = _verifyfile(RPMVERIFY_MD5, "5");
size = _verify(RPMVERIFY_FILESIZE, "S");
link = _verifylink(RPMVERIFY_LINKTO, "L");
mtime = _verify(RPMVERIFY_MTIME, "T");
rdev = _verify(RPMVERIFY_RDEV, "D");
user = _verify(RPMVERIFY_USER, "U");
group = _verify(RPMVERIFY_GROUP, "G");
mode = _verify(RPMVERIFY_MODE, "M");
#undef _verify
#undef _verifylink
#undef _verifyfile
sprintf(te, "%s%s%s%s%s%s%s%s %c %s",
size, mode, md5, rdev, link, user, group, mtime,
((fileAttrs & RPMFILE_CONFIG) ? 'c' :
(fileAttrs & RPMFILE_DOC) ? 'd' :
(fileAttrs & RPMFILE_GHOST) ? 'g' :
(fileAttrs & RPMFILE_LICENSE) ? 'l' :
(fileAttrs & RPMFILE_README) ? 'r' : ' '),
fileNames[i]);
te += strlen(te);
}
if (te > t) {
*te++ = '\n';
*te = '\0';
rpmMessage(RPMMESS_NORMAL, "%s", t);
te = t = buf;
*t = '\0';
}
}
exit:
fileNames = _free(fileNames);
return ec;
}
/**
* Check installed package dependencies for problems.
* @param qva parsed query/verify options
* @param ts transaction set
* @param h header
* @return 0 no problems, 1 problems found
*/
static int verifyDependencies(/*@unused@*/ QVA_t qva, rpmTransactionSet ts,
Header h)
/*@globals fileSystem@*/
/*@modifies ts, h, fileSystem @*/
{
rpmDependencyConflict conflicts;
int numConflicts;
int rc = 0; /* assume no problems */
int i;
rpmtransClean(ts);
(void) rpmtransAddPackage(ts, h, NULL, NULL, 0, NULL);
(void) rpmdepCheck(ts, &conflicts, &numConflicts);
/*@-branchstate@*/
if (numConflicts) {
const char *n, *v, *r;
char * t, * te;
int nb = 512;
(void) headerNVR(h, &n, &v, &r);
/*@-type@*/ /* FIX: rpmDependencyConflict usage */
for (i = 0; i < numConflicts; i++) {
nb += strlen(conflicts[i].needsName) + sizeof(", ") - 1;
if (conflicts[i].needsFlags)
nb += strlen(conflicts[i].needsVersion) + 5;
}
te = t = alloca(nb);
*te = '\0';
sprintf(te, _("Unsatisfied dependencies for %s-%s-%s: "), n, v, r);
te += strlen(te);
for (i = 0; i < numConflicts; i++) {
if (i) te = stpcpy(te, ", ");
te = stpcpy(te, conflicts[i].needsName);
if (conflicts[i].needsFlags) {
int flags = conflicts[i].needsFlags;
*te++ = ' ';
if (flags & RPMSENSE_LESS) *te++ = '<';
if (flags & RPMSENSE_GREATER) *te++ = '>';
if (flags & RPMSENSE_EQUAL) *te++ = '=';
*te++ = ' ';
te = stpcpy(te, conflicts[i].needsVersion);
}
}
conflicts = rpmdepFreeConflicts(conflicts, numConflicts);
/*@=type@*/
if (te > t) {
*te++ = '\n';
*te = '\0';
rpmMessage(RPMMESS_NORMAL, "%s", t);
te = t;
*t = '\0';
}
rc = 1;
}
/*@=branchstate@*/
rpmtransClean(ts);
return rc;
}
int showVerifyPackage(QVA_t qva, rpmTransactionSet ts, Header h)
{
int ec = 0;
int rc;
if (qva->qva_flags & VERIFY_DIGEST) {
/* XXX Wire transaction set here, python bindings prevent. */
if ((rc = rpmVerifyDigest(h)) != 0) {
const char *n, *v, *r;
(void) headerNVR(h, &n, &v, &r);
rpmMessage(RPMMESS_NORMAL,
_("%s-%s-%s: immutable header region digest check failed\n"),
n, v, r);
ec = rc;
}
}
if (qva->qva_flags & VERIFY_DEPS) {
if ((rc = verifyDependencies(qva, ts, h)) != 0)
ec = rc;
}
if (qva->qva_flags & VERIFY_FILES) {
if ((rc = verifyHeader(qva, ts, h)) != 0)
ec = rc;
}
if (qva->qva_flags & VERIFY_SCRIPT) {
FD_t fdo = fdDup(STDOUT_FILENO);
if ((rc = rpmVerifyScript(qva, ts, h, fdo)) != 0)
ec = rc;
if (fdo)
rc = Fclose(fdo);
}
return ec;
}
int rpmcliVerify(rpmTransactionSet ts, QVA_t qva, const char ** argv)
{
const char * arg;
int ec = 0;
if (qva->qva_showPackage == NULL)
qva->qva_showPackage = showVerifyPackage;
switch (qva->qva_source) {
case RPMQV_RPM:
if (!(qva->qva_flags & VERIFY_DEPS))
break;
/*@fallthrough@*/
default:
if (rpmtsOpenDB(ts, O_RDONLY))
return 1; /* XXX W2DO? */
break;
}
if (qva->qva_source == RPMQV_ALL) {
/*@-nullpass@*/ /* FIX: argv can be NULL, cast to pass argv array */
ec = rpmQueryVerify(qva, ts, (const char *) argv);
/*@=nullpass@*/
} else {
if (argv != NULL)
while ((arg = *argv++) != NULL) {
ec += rpmQueryVerify(qva, ts, arg);
rpmtransClean(ts);
}
}
if (qva->qva_showPackage == showVerifyPackage)
qva->qva_showPackage = NULL;
return ec;
}
|