summaryrefslogtreecommitdiff
path: root/verify.c
blob: dbc4b5b0bbbb5fe92cb4868e8d7adf79e6ef7199 (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
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "lib/messages.h"
#include "lib/package.h"
#include "query.h"
#include "rpmlib.h"
#include "verify.h"

static void verifyHeader(char * prefix, Header h);
static void verifyMatches(char * prefix, rpmdb db, dbIndexSet matches);

static void verifyHeader(char * prefix, Header h) {
    char ** fileList;
    int count, type;
    int verifyResult;
    int i;
    char * size, * md5, * link, * mtime, * mode;
    char * group, * user, * rdev;
    int_32 * fileFlagsList;

    getEntry(h, RPMTAG_FILEFLAGS, &type, (void **) &fileFlagsList, &count);

    if (getEntry(h, RPMTAG_FILENAMES, &type, (void **) &fileList, &count)) {
	for (i = 0; i < count; i++) {
	    if (rpmVerifyFile(prefix, h, i, &verifyResult))
		printf("missing    %s\n", fileList[i]);
	    else {
		size = md5 = link = mtime = mode = ".";
		user = group = rdev = ".";

		if (!verifyResult) continue;
	    
		if (verifyResult & VERIFY_MD5)
		    md5 = "5";
		if (verifyResult & VERIFY_FILESIZE)
		    size = "S";
		if (verifyResult & VERIFY_LINKTO)
		    link = "L";
		if (verifyResult & VERIFY_MTIME)
		    mtime = "T";
		if (verifyResult & VERIFY_RDEV)
		    rdev = "D";
		if (verifyResult & VERIFY_USER)
		    user = "U";
		if (verifyResult & VERIFY_GROUP)
		    group = "G";
		if (verifyResult & VERIFY_MODE)
		    mode = "M";

		printf("%s%s%s%s%s%s%s%s %c %s\n",
		       size, mode, md5, rdev, link, user, group, mtime, 
		       fileFlagsList[i] & RPMFILE_CONFIG ? 'c' : ' ', 
		       fileList[i]);
	    }
	}
	
	free(fileList);
    }
}

static void verifyMatches(char * prefix, rpmdb db, dbIndexSet matches) {
    int i;
    Header h;

    for (i = 0; i < matches.count; i++) {
	if (matches.recs[i].recOffset) {
	    message(MESS_DEBUG, "verifying record number %d\n",
			matches.recs[i].recOffset);
	    
	    h = rpmdbGetRecord(db, matches.recs[i].recOffset);
	    if (!h) {
		fprintf(stderr, "error: could not read database record\n");
	    } else {
		verifyHeader(prefix, h);
		freeHeader(h);
	    }
	}
    }
}

void doVerify(char * prefix, enum verifysources source, char ** argv) {
    Header h;
    int offset;
    int fd;
    int rc;
    int isSource;
    rpmdb db;
    dbIndexSet matches;
    char * arg;

    if (source != VERIFY_SRPM && source != VERIFY_RPM) {
	if (rpmdbOpen(prefix, &db, O_RDONLY, 0644)) {
	    exit(1);
	}
    }

    if (source != VERIFY_SRPM && source != VERIFY_RPM) {
	if (rpmdbOpen(prefix, &db, O_RDONLY, 0644)) {
	    exit(1);
	}
    }

    if (source == VERIFY_EVERY) {
	offset = rpmdbFirstRecNum(db);
	while (offset) {
	    h = rpmdbGetRecord(db, offset);
	    if (!h) {
		fprintf(stderr, "could not read database record!\n");
		exit(1);
	    }
	    verifyHeader(prefix, h);
	    freeHeader(h);
	    offset = rpmdbNextRecNum(db, offset);
	}
    } else {
	while (*argv) {
	    arg = *argv++;

	    switch (source) {
	      case VERIFY_SRPM:
	      case VERIFY_RPM:
		fd = open(arg, O_RDONLY);
		if (fd < 0) {
		    fprintf(stderr, "open of %s failed: %s\n", arg, 
			    strerror(errno));
		} else {
		    rc = pkgReadHeader(fd, &h, &isSource);
		    close(fd);
		    switch (rc) {
			case 0:
			    verifyHeader(prefix, h);
			    freeHeader(h);
			    break;
			case 1:
			    fprintf(stderr, "%s is not an RPM\n", arg);
		    }
		}
			
		break;

	      case VERIFY_SGROUP:
	      case VERIFY_GRP:
		if (rpmdbFindByGroup(db, arg, &matches)) {
		    fprintf(stderr, "group %s does not contain any pacakges\n", 
				     arg);
		} else {
		    verifyMatches(prefix, db, matches);
		    freeDBIndexRecord(matches);
		}
		break;

	      case VERIFY_SPATH:
	      case VERIFY_PATH:
		if (rpmdbFindByFile(db, arg, &matches)) {
		    fprintf(stderr, "file %s is not owned by any package\n", 
				arg);
		} else {
		    verifyMatches(prefix, db, matches);
		    freeDBIndexRecord(matches);
		}
		break;

	      case VERIFY_SPACKAGE:
	      case VERIFY_PACKAGE:
		rc = findPackageByLabel(db, arg, &matches);
		if (rc == 1) 
		    fprintf(stderr, "package %s is not installed\n", arg);
		else if (rc == 2) {
		    fprintf(stderr, "error looking for package %s\n", arg);
		} else {
		    verifyMatches(prefix, db, matches);
		    freeDBIndexRecord(matches);
		}
		break;

		case VERIFY_EVERY:
		    ; /* nop */
	    }
	}
    }
   
    if (source != VERIFY_SRPM && source != VERIFY_RPM) {
	rpmdbClose(db);
    }
}