doc: Move Perl version baseline as the first perl coding style subsection
[dpkg.git] / src / main / verify.c
blob8abe0996422d65fb5eeffdf490db29073d2f6195
1 /*
2 * dpkg - main program for package management
3 * verify.c - verify package integrity
5 * Copyright © 2012-2015 Guillem Jover <guillem@debian.org>
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 #include <config.h>
22 #include <compat.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <stdio.h>
32 #include <dpkg/i18n.h>
33 #include <dpkg/dpkg.h>
34 #include <dpkg/dpkg-db.h>
35 #include <dpkg/options.h>
36 #include <dpkg/db-ctrl.h>
37 #include <dpkg/db-fsys.h>
38 #include <dpkg/buffer.h>
40 #include "main.h"
43 enum verify_result {
44 VERIFY_NONE,
45 VERIFY_PASS,
46 VERIFY_FAIL,
49 struct verify_checks {
50 int exists_errno;
51 enum verify_result exists;
52 enum verify_result mode;
53 enum verify_result md5sum;
56 typedef void verify_output_func(struct fsys_namenode *, struct verify_checks *);
58 static int
59 verify_result_rpm(enum verify_result result, int check)
61 switch (result) {
62 case VERIFY_FAIL:
63 return check;
64 case VERIFY_PASS:
65 return '.';
66 case VERIFY_NONE:
67 default:
68 return '?';
72 static void
73 verify_output_rpm(struct fsys_namenode *namenode, struct verify_checks *checks)
75 char result[9];
76 char *error = NULL;
77 int attr;
79 memset(result, '?', sizeof(result));
81 if (checks->exists == VERIFY_FAIL) {
82 memcpy(result, "missing ", sizeof(result));
83 if (checks->exists_errno != ENOENT)
84 m_asprintf(&error, " (%s)", strerror(checks->exists_errno));
85 } else {
86 result[1] = verify_result_rpm(checks->mode, 'M');
87 result[2] = verify_result_rpm(checks->md5sum, '5');
90 if (namenode->flags & FNNF_OLD_CONFF)
91 attr = 'c';
92 else
93 attr = ' ';
95 printf("%.9s %c %s%s\n", result, attr, namenode->name, error ? error : "");
97 free(error);
100 static verify_output_func *verify_output = verify_output_rpm;
102 bool
103 verify_set_output(const char *name)
105 if (strcmp(name, "rpm") == 0)
106 verify_output = verify_output_rpm;
107 else
108 return false;
110 return true;
113 static int
114 verify_digest(const char *filename, struct fsys_namenode *fnn,
115 struct verify_checks *checks)
117 static int fd;
119 fd = open(filename, O_RDONLY);
121 if (fd >= 0) {
122 struct dpkg_error err;
123 char hash[MD5HASHLEN + 1];
125 push_cleanup(cu_closefd, ehflag_bombout, 1, &fd);
126 if (fd_md5(fd, hash, -1, &err) < 0)
127 ohshit(_("cannot compute MD5 digest for file '%s': %s"),
128 filename, err.str);
129 pop_cleanup(ehflag_normaltidy); /* fd = open(cdr.buf) */
130 close(fd);
132 if (strcmp(hash, fnn->newhash) == 0) {
133 checks->md5sum = VERIFY_PASS;
134 return 0;
135 } else {
136 checks->md5sum = VERIFY_FAIL;
138 } else {
139 checks->md5sum = VERIFY_NONE;
142 return -1;
145 static int
146 verify_file(const char *filename, struct fsys_namenode *fnn,
147 struct pkginfo *pkg, struct verify_checks *checks)
149 struct stat st;
150 int failures = 0;
152 if (lstat(filename, &st) < 0) {
153 checks->exists_errno = errno;
154 checks->exists = VERIFY_FAIL;
155 return 1;
157 checks->exists = VERIFY_PASS;
159 if (fnn->newhash == NULL && fnn->oldhash != NULL)
160 fnn->newhash = fnn->oldhash;
162 if (fnn->newhash != NULL) {
163 /* Mode check heuristic: If we know its digest, the pathname
164 * must be a regular file. */
165 if (!S_ISREG(st.st_mode)) {
166 checks->mode = VERIFY_FAIL;
167 failures++;
170 if (verify_digest(filename, fnn, checks) < 0)
171 failures++;
174 return failures;
177 static void
178 verify_package(struct pkginfo *pkg)
180 struct fsys_namenode_list *file;
181 struct varbuf filename = VARBUF_INIT;
183 ensure_packagefiles_available(pkg);
184 parse_filehash(pkg, &pkg->installed);
185 pkg_conffiles_mark_old(pkg);
187 for (file = pkg->files; file; file = file->next) {
188 struct verify_checks checks;
189 struct fsys_namenode *fnn;
191 fnn = namenodetouse(file->namenode, pkg, &pkg->installed);
193 varbuf_reset(&filename);
194 varbuf_add_str(&filename, dpkg_fsys_get_dir());
195 varbuf_add_str(&filename, fnn->name);
196 varbuf_end_str(&filename);
198 memset(&checks, 0, sizeof(checks));
200 if (verify_file(filename.buf, fnn, pkg, &checks) > 0)
201 verify_output(fnn, &checks);
204 varbuf_destroy(&filename);
208 verify(const char *const *argv)
210 struct pkginfo *pkg;
211 int rc = 0;
213 modstatdb_open(msdbrw_readonly);
214 ensure_diversions();
216 if (!*argv) {
217 struct pkg_hash_iter *iter;
219 iter = pkg_hash_iter_new();
220 while ((pkg = pkg_hash_iter_next_pkg(iter)))
221 verify_package(pkg);
222 pkg_hash_iter_free(iter);
223 } else {
224 const char *thisarg;
226 while ((thisarg = *argv++)) {
227 pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
228 if (pkg->status == PKG_STAT_NOTINSTALLED) {
229 notice(_("package '%s' is not installed"),
230 pkg_name(pkg, pnaw_nonambig));
231 rc = 1;
232 continue;
235 verify_package(pkg);
239 modstatdb_shutdown();
241 m_output(stdout, _("<standard output>"));
243 return rc;