1 /*
2  *  check.c
3  *
4  *  Copyright (c) 2012-2018 Pacman Development Team <pacman-dev@archlinux.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <limits.h>
21 #include <string.h>
22 #include <errno.h>
23 
24 /* pacman */
25 #include "check.h"
26 #include "conf.h"
27 #include "util.h"
28 
check_file_exists(const char * pkgname,char * filepath,size_t rootlen,struct stat * st)29 static int check_file_exists(const char *pkgname, char *filepath, size_t rootlen,
30 		struct stat *st)
31 {
32 	/* use lstat to prevent errors from symlinks */
33 	if(llstat(filepath, st) != 0) {
34 		if(alpm_option_match_noextract(config->handle, filepath + rootlen) == 0) {
35 			/* NoExtract */
36 			return -1;
37 		} else {
38 			if(config->quiet) {
39 				printf("%s %s\n", pkgname, filepath);
40 			} else {
41 				pm_printf(ALPM_LOG_WARNING, "%s: %s (%s)\n",
42 						pkgname, filepath, strerror(errno));
43 			}
44 			return 1;
45 		}
46 	}
47 
48 	return 0;
49 }
50 
check_file_type(const char * pkgname,const char * filepath,struct stat * st,struct archive_entry * entry)51 static int check_file_type(const char *pkgname, const char *filepath,
52 		struct stat *st, struct archive_entry *entry)
53 {
54 	mode_t archive_type = archive_entry_filetype(entry);
55 	mode_t file_type = st->st_mode;
56 
57 	if((archive_type == AE_IFREG && !S_ISREG(file_type)) ||
58 			(archive_type == AE_IFDIR && !S_ISDIR(file_type)) ||
59 			(archive_type == AE_IFLNK && !S_ISLNK(file_type))) {
60 		if(config->quiet) {
61 			printf("%s %s\n", pkgname, filepath);
62 		} else {
63 			pm_printf(ALPM_LOG_WARNING, _("%s: %s (File type mismatch)\n"),
64 					pkgname, filepath);
65 		}
66 		return 1;
67 	}
68 
69 	return 0;
70 }
71 
check_file_permissions(const char * pkgname,const char * filepath,struct stat * st,struct archive_entry * entry)72 static int check_file_permissions(const char *pkgname, const char *filepath,
73 		struct stat *st, struct archive_entry *entry)
74 {
75 	int errors = 0;
76 	mode_t fsmode;
77 
78 	/* uid */
79 	if(st->st_uid != archive_entry_uid(entry)) {
80 		errors++;
81 		if(!config->quiet) {
82 			pm_printf(ALPM_LOG_WARNING, _("%s: %s (UID mismatch)\n"),
83 					pkgname, filepath);
84 		}
85 	}
86 
87 	/* gid */
88 	if(st->st_gid != archive_entry_gid(entry)) {
89 		errors++;
90 		if(!config->quiet) {
91 			pm_printf(ALPM_LOG_WARNING, _("%s: %s (GID mismatch)\n"),
92 					pkgname, filepath);
93 		}
94 	}
95 
96 	/* mode */
97 	fsmode = st->st_mode & (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
98 	if(fsmode != (~AE_IFMT & archive_entry_mode(entry))) {
99 		errors++;
100 		if(!config->quiet) {
101 			pm_printf(ALPM_LOG_WARNING, _("%s: %s (Permissions mismatch)\n"),
102 					pkgname, filepath);
103 		}
104 	}
105 
106 	return (errors != 0 ? 1 : 0);
107 }
108 
check_file_time(const char * pkgname,const char * filepath,struct stat * st,struct archive_entry * entry,int backup)109 static int check_file_time(const char *pkgname, const char *filepath,
110 		struct stat *st, struct archive_entry *entry, int backup)
111 {
112 	if(st->st_mtime != archive_entry_mtime(entry)) {
113 		if(backup) {
114 			if(!config->quiet) {
115 				printf("%s%s%s: ", config->colstr.title, _("backup file"),
116 						config->colstr.nocolor);
117 				printf(_("%s: %s (Modification time mismatch)\n"),
118 						pkgname, filepath);
119 			}
120 			return 0;
121 		}
122 		if(!config->quiet) {
123 			pm_printf(ALPM_LOG_WARNING, _("%s: %s (Modification time mismatch)\n"),
124 					pkgname, filepath);
125 		}
126 		return 1;
127 	}
128 
129 	return 0;
130 }
131 
check_file_link(const char * pkgname,const char * filepath,struct stat * st,struct archive_entry * entry)132 static int check_file_link(const char *pkgname, const char *filepath,
133 		struct stat *st, struct archive_entry *entry)
134 {
135 	size_t length = st->st_size + 1;
136 	char link[length];
137 
138 	if(readlink(filepath, link, length) != st->st_size) {
139 		/* this should not happen */
140 		pm_printf(ALPM_LOG_ERROR, _("unable to read symlink contents: %s\n"), filepath);
141 		return 1;
142 	}
143 	link[length - 1] = '\0';
144 
145 	if(strcmp(link, archive_entry_symlink(entry)) != 0) {
146 		if(!config->quiet) {
147 			pm_printf(ALPM_LOG_WARNING, _("%s: %s (Symlink path mismatch)\n"),
148 					pkgname, filepath);
149 		}
150 		return 1;
151 	}
152 
153 	return 0;
154 }
155 
check_file_size(const char * pkgname,const char * filepath,struct stat * st,struct archive_entry * entry,int backup)156 static int check_file_size(const char *pkgname, const char *filepath,
157 		struct stat *st, struct archive_entry *entry, int backup)
158 {
159 	if(st->st_size != archive_entry_size(entry)) {
160 		if(backup) {
161 			if(!config->quiet) {
162 				printf("%s%s%s: ", config->colstr.title, _("backup file"),
163 						config->colstr.nocolor);
164 				printf(_("%s: %s (Size mismatch)\n"),
165 						pkgname, filepath);
166 			}
167 			return 0;
168 		}
169 		if(!config->quiet) {
170 			pm_printf(ALPM_LOG_WARNING, _("%s: %s (Size mismatch)\n"),
171 					pkgname, filepath);
172 		}
173 		return 1;
174 	}
175 
176 	return 0;
177 }
178 
179 /* placeholders - libarchive currently does not read checksums from mtree files
180 static int check_file_md5sum(const char *pkgname, const char *filepath,
181 		struct stat *st, struct archive_entry *entry, int backup)
182 {
183 	return 0;
184 }
185 
186 static int check_file_sha256sum(const char *pkgname, const char *filepath,
187 		struct stat *st, struct archive_entry *entry, int backup)
188 {
189 	return 0;
190 }
191 */
192 
193 /* Loop through the files of the package to check if they exist. */
check_pkg_fast(alpm_pkg_t * pkg)194 int check_pkg_fast(alpm_pkg_t *pkg)
195 {
196 	const char *root, *pkgname;
197 	size_t errors = 0;
198 	size_t rootlen;
199 	char filepath[PATH_MAX];
200 	alpm_filelist_t *filelist;
201 	size_t i;
202 
203 	root = alpm_option_get_root(config->handle);
204 	rootlen = strlen(root);
205 	if(rootlen + 1 > PATH_MAX) {
206 		/* we are in trouble here */
207 		pm_printf(ALPM_LOG_ERROR, _("path too long: %s%s\n"), root, "");
208 		return 1;
209 	}
210 	strcpy(filepath, root);
211 
212 	pkgname = alpm_pkg_get_name(pkg);
213 	filelist = alpm_pkg_get_files(pkg);
214 	for(i = 0; i < filelist->count; i++) {
215 		const alpm_file_t *file = filelist->files + i;
216 		struct stat st;
217 		int exists;
218 		const char *path = file->name;
219 		size_t plen = strlen(path);
220 
221 		if(rootlen + 1 + plen > PATH_MAX) {
222 			pm_printf(ALPM_LOG_WARNING, _("path too long: %s%s\n"), root, path);
223 			continue;
224 		}
225 		strcpy(filepath + rootlen, path);
226 
227 		exists = check_file_exists(pkgname, filepath, rootlen, &st);
228 		if(exists == 0) {
229 			int expect_dir = path[plen - 1] == '/' ? 1 : 0;
230 			int is_dir = S_ISDIR(st.st_mode) ? 1 : 0;
231 			if(expect_dir != is_dir) {
232 				pm_printf(ALPM_LOG_WARNING, _("%s: %s (File type mismatch)\n"),
233 						pkgname, filepath);
234 				++errors;
235 			}
236 		} else if(exists == 1) {
237 			++errors;
238 		}
239 	}
240 
241 	if(!config->quiet) {
242 		printf(_n("%s: %jd total file, ", "%s: %jd total files, ",
243 					(unsigned long)filelist->count), pkgname, (intmax_t)filelist->count);
244 		printf(_n("%jd missing file\n", "%jd missing files\n",
245 					(unsigned long)errors), (intmax_t)errors);
246 	}
247 
248 	return (errors != 0 ? 1 : 0);
249 }
250 
251 /* Loop though files in a package and perform full file property checking. */
check_pkg_full(alpm_pkg_t * pkg)252 int check_pkg_full(alpm_pkg_t *pkg)
253 {
254 	const char *root, *pkgname;
255 	size_t errors = 0;
256 	size_t rootlen;
257 	struct archive *mtree;
258 	struct archive_entry *entry = NULL;
259 	size_t file_count = 0;
260 	const alpm_list_t *lp;
261 
262 	root = alpm_option_get_root(config->handle);
263 	rootlen = strlen(root);
264 	if(rootlen + 1 > PATH_MAX) {
265 		/* we are in trouble here */
266 		pm_printf(ALPM_LOG_ERROR, _("path too long: %s%s\n"), root, "");
267 		return 1;
268 	}
269 
270 	pkgname = alpm_pkg_get_name(pkg);
271 	mtree = alpm_pkg_mtree_open(pkg);
272 	if(mtree == NULL) {
273 		/* TODO: check error to confirm failure due to no mtree file */
274 		if(!config->quiet) {
275 			printf(_("%s: no mtree file\n"), pkgname);
276 		}
277 		return 0;
278 	}
279 
280 	while(alpm_pkg_mtree_next(pkg, mtree, &entry) == ARCHIVE_OK) {
281 		struct stat st;
282 		const char *path = archive_entry_pathname(entry);
283 		char filepath[PATH_MAX];
284 		int filepath_len;
285 		mode_t type;
286 		size_t file_errors = 0;
287 		int backup = 0;
288 		int exists;
289 
290 		/* strip leading "./" from path entries */
291 		if(path[0] == '.' && path[1] == '/') {
292 			path += 2;
293 		}
294 
295 		if(*path == '.') {
296 			const char *dbfile = NULL;
297 
298 			if(strcmp(path, ".INSTALL") == 0) {
299 				dbfile = "install";
300 			} else if(strcmp(path, ".CHANGELOG") == 0) {
301 				dbfile = "changelog";
302 			} else {
303 				continue;
304 			}
305 
306 			/* Do not append root directory as alpm_option_get_dbpath is already
307 			 * an absoute path */
308 			filepath_len = snprintf(filepath, PATH_MAX, "%slocal/%s-%s/%s",
309 					alpm_option_get_dbpath(config->handle),
310 					pkgname, alpm_pkg_get_version(pkg), dbfile);
311 			if(filepath_len >= PATH_MAX) {
312 				pm_printf(ALPM_LOG_WARNING, _("path too long: %slocal/%s-%s/%s\n"),
313 						alpm_option_get_dbpath(config->handle),
314 						pkgname, alpm_pkg_get_version(pkg), dbfile);
315 				continue;
316 			}
317 		} else {
318 			filepath_len = snprintf(filepath, PATH_MAX, "%s%s", root, path);
319 			if(filepath_len >= PATH_MAX) {
320 				pm_printf(ALPM_LOG_WARNING, _("path too long: %s%s\n"), root, path);
321 				continue;
322 			}
323 		}
324 
325 		file_count++;
326 
327 		exists = check_file_exists(pkgname, filepath, rootlen, &st);
328 		if(exists == 1) {
329 			errors++;
330 			continue;
331 		} else if(exists == -1) {
332 			/* NoExtract */
333 			continue;
334 		}
335 
336 		type = archive_entry_filetype(entry);
337 
338 		if(type != AE_IFDIR && type != AE_IFREG && type != AE_IFLNK) {
339 			pm_printf(ALPM_LOG_WARNING, _("file type not recognized: %s%s\n"), root, path);
340 			continue;
341 		}
342 
343 		if(check_file_type(pkgname, filepath, &st, entry) == 1) {
344 			errors++;
345 			continue;
346 		}
347 
348 		file_errors += check_file_permissions(pkgname, filepath, &st, entry);
349 
350 		if(type == AE_IFLNK) {
351 			file_errors += check_file_link(pkgname, filepath, &st, entry);
352 		}
353 
354 		/* the following checks are expected to fail if a backup file has been
355 		   modified */
356 		for(lp = alpm_pkg_get_backup(pkg); lp; lp = lp->next) {
357 			alpm_backup_t *bl = lp->data;
358 
359 			if(strcmp(path, bl->name) == 0) {
360 				backup = 1;
361 				break;
362 			}
363 		}
364 
365 		if(type != AE_IFDIR) {
366 			/* file or symbolic link */
367 			file_errors += check_file_time(pkgname, filepath, &st, entry, backup);
368 		}
369 
370 		if(type == AE_IFREG) {
371 			file_errors += check_file_size(pkgname, filepath, &st, entry, backup);
372 			/* file_errors += check_file_md5sum(pkgname, filepath, &st, entry, backup); */
373 		}
374 
375 		if(config->quiet && file_errors) {
376 			printf("%s %s\n", pkgname, filepath);
377 		}
378 
379 		errors += (file_errors != 0 ? 1 : 0);
380 	}
381 
382 	alpm_pkg_mtree_close(pkg, mtree);
383 
384 	if(!config->quiet) {
385 		printf(_n("%s: %jd total file, ", "%s: %jd total files, ",
386 					(unsigned long)file_count), pkgname, (intmax_t)file_count);
387 		printf(_n("%jd altered file\n", "%jd altered files\n",
388 					(unsigned long)errors), (intmax_t)errors);
389 	}
390 
391 	return (errors != 0 ? 1 : 0);
392 }
393