summaryrefslogtreecommitdiff
path: root/src/pacman/check.c
blob: 5c7d3631eab4da687d1619c93dac4e8a5b45d590 (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
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
/*
 *  check.c
 *
 *  Copyright (c) 2012-2017 Pacman Development Team <pacman-dev@archlinux.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <limits.h>
#include <string.h>
#include <errno.h>

/* pacman */
#include "check.h"
#include "conf.h"
#include "util.h"

static int check_file_exists(const char *pkgname, char *filepath, size_t rootlen,
                struct stat *st)
{
        /* use lstat to prevent errors from symlinks */
        if(llstat(filepath, st) != 0) {
                if(alpm_option_match_noextract(config->handle, filepath + rootlen) == 0) {
                        /* NoExtract */
                        return -1;
                } else {
                        if(config->quiet) {
                                printf("%s %s\n", pkgname, filepath);
                        } else {
                                pm_printf(ALPM_LOG_WARNING, "%s%s (%s)\n",
                                                pkgname, filepath, strerror(errno));
                        }
                        return 1;
                }
        }

        return 0;
}

static int check_file_type(const char *pkgname, const char *filepath,
                struct stat *st, struct archive_entry *entry)
{
        mode_t archive_type = archive_entry_filetype(entry);
        mode_t file_type = st->st_mode;

        if((archive_type == AE_IFREG && !S_ISREG(file_type)) ||
                        (archive_type == AE_IFDIR && !S_ISDIR(file_type)) ||
                        (archive_type == AE_IFLNK && !S_ISLNK(file_type))) {
                if(config->quiet) {
                        printf("%s %s\n", pkgname, filepath);
                } else {
                        pm_printf(ALPM_LOG_WARNING, _("%s%s (File type mismatch)\n"),
                                        pkgname, filepath);
                }
                return 1;
        }

        return 0;
}

static int check_file_permissions(const char *pkgname, const char *filepath,
                struct stat *st, struct archive_entry *entry)
{
        int errors = 0;
        mode_t fsmode;

        /* uid */
        if(st->st_uid != archive_entry_uid(entry)) {
                errors++;
                if(!config->quiet) {
                        pm_printf(ALPM_LOG_WARNING, _("%s%s (UID mismatch)\n"),
                                        pkgname, filepath);
                }
        }

        /* gid */
        if(st->st_gid != archive_entry_gid(entry)) {
                errors++;
                if(!config->quiet) {
                        pm_printf(ALPM_LOG_WARNING, _("%s%s (GID mismatch)\n"),
                                        pkgname, filepath);
                }
        }

        /* mode */
        fsmode = st->st_mode & (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
        if(fsmode != (~AE_IFMT & archive_entry_mode(entry))) {
                errors++;
                if(!config->quiet) {
                        pm_printf(ALPM_LOG_WARNING, _("%s%s (Permissions mismatch)\n"),
                                        pkgname, filepath);
                }
        }

        return (errors != 0 ? 1 : 0);
}

static int check_file_time(const char *pkgname, const char *filepath,
                struct stat *st, struct archive_entry *entry, int backup)
{
        if(st->st_mtime != archive_entry_mtime(entry)) {
                if(backup) {
                        if(!config->quiet) {
                                printf("%s%s%s: ", config->colstr.title, _("backup file"),
                                                config->colstr.nocolor);
                                printf(_("%s%s (Modification time mismatch)\n"),
                                                pkgname, filepath);
                        }
                        return 0;
                }
                if(!config->quiet) {
                        pm_printf(ALPM_LOG_WARNING, _("%s%s (Modification time mismatch)\n"),
                                        pkgname, filepath);
                }
                return 1;
        }

        return 0;
}

static int check_file_link(const char *pkgname, const char *filepath,
                struct stat *st, struct archive_entry *entry)
{
        size_t length = st->st_size + 1;
        char link[length];

        if(readlink(filepath, link, length) != st->st_size) {
                /* this should not happen */
                pm_printf(ALPM_LOG_ERROR, _("unable to read symlink contents: %s\n"), filepath);
                return 1;
        }
        link[length - 1] = '\0';

        if(strcmp(link, archive_entry_symlink(entry)) != 0) {
                if(!config->quiet) {
                        pm_printf(ALPM_LOG_WARNING, _("%s%s (Symlink path mismatch)\n"),
                                        pkgname, filepath);
                }
                return 1;
        }

        return 0;
}

static int check_file_size(const char *pkgname, const char *filepath,
                struct stat *st, struct archive_entry *entry, int backup)
{
        if(st->st_size != archive_entry_size(entry)) {
                if(backup) {
                        if(!config->quiet) {
                                printf("%s%s%s: ", config->colstr.title, _("backup file"),
                                                config->colstr.nocolor);
                                printf(_("%s%s (Size mismatch)\n"),
                                                pkgname, filepath);
                        }
                        return 0;
                }
                if(!config->quiet) {
                        pm_printf(ALPM_LOG_WARNING, _("%s%s (Size mismatch)\n"),
                                        pkgname, filepath);
                }
                return 1;
        }

        return 0;
}

/* placeholders - libarchive currently does not read checksums from mtree files
static int check_file_md5sum(const char *pkgname, const char *filepath,
                struct stat *st, struct archive_entry *entry, int backup)
{
        return 0;
}

static int check_file_sha256sum(const char *pkgname, const char *filepath,
                struct stat *st, struct archive_entry *entry, int backup)
{
        return 0;
}
*/

/* Loop through the files of the package to check if they exist. */
int check_pkg_fast(alpm_pkg_t *pkg)
{
        const char *root, *pkgname;
        size_t errors = 0;
        size_t rootlen;
        char filepath[PATH_MAX];
        alpm_filelist_t *filelist;
        size_t i;

        root = alpm_option_get_root(config->handle);
        rootlen = strlen(root);
        if(rootlen + 1 > PATH_MAX) {
                /* we are in trouble here */
                pm_printf(ALPM_LOG_ERROR, _("path too long: %s%s\n"), root, "");
                return 1;
        }
        strcpy(filepath, root);

        pkgname = alpm_pkg_get_name(pkg);
        filelist = alpm_pkg_get_files(pkg);
        for(i = 0; i < filelist->count; i++) {
                const alpm_file_t *file = filelist->files + i;
                struct stat st;
                int exists;
                const char *path = file->name;
                size_t plen = strlen(path);

                if(rootlen + 1 + plen > PATH_MAX) {
                        pm_printf(ALPM_LOG_WARNING, _("path too long: %s%s\n"), root, path);
                        continue;
                }
                strcpy(filepath + rootlen, path);

                exists = check_file_exists(pkgname, filepath, rootlen, &st);
                if(exists == 0) {
                        int expect_dir = path[plen - 1] == '/' ? 1 : 0;
                        int is_dir = S_ISDIR(st.st_mode) ? 1 : 0;
                        if(expect_dir != is_dir) {
                                pm_printf(ALPM_LOG_WARNING, _("%s%s (File type mismatch)\n"),
                                                pkgname, filepath);
                                ++errors;
                        }
                } else if(exists == 1) {
                        ++errors;
                }
        }

        if(!config->quiet) {
                printf(_n("%s%jd total file, ""%s%jd total files, ",
                                        (unsigned long)filelist->count), pkgname, (intmax_t)filelist->count);
                printf(_n("%jd missing file\n""%jd missing files\n",
                                        (unsigned long)errors), (intmax_t)errors);
        }

        return (errors != 0 ? 1 : 0);
}

/* Loop though files in a package and perform full file property checking. */
int check_pkg_full(alpm_pkg_t *pkg)
{
        const char *root, *pkgname;
        size_t errors = 0;
        size_t rootlen;
        struct archive *mtree;
        struct archive_entry *entry = NULL;
        size_t file_count = 0;
        const alpm_list_t *lp;

        root = alpm_option_get_root(config->handle);
        rootlen = strlen(root);
        if(rootlen + 1 > PATH_MAX) {
                /* we are in trouble here */
                pm_printf(ALPM_LOG_ERROR, _("path too long: %s%s\n"), root, "");
                return 1;
        }

        pkgname = alpm_pkg_get_name(pkg);
        mtree = alpm_pkg_mtree_open(pkg);
        if(mtree == NULL) {
                /* TODO: check error to confirm failure due to no mtree file */
                if(!config->quiet) {
                        printf(_("%s: no mtree file\n"), pkgname);
                }
                return 0;
        }

        while(alpm_pkg_mtree_next(pkg, mtree, &entry) == ARCHIVE_OK) {
                struct stat st;
                const char *path = archive_entry_pathname(entry);
                char filepath[PATH_MAX];
                int filepath_len;
                mode_t type;
                size_t file_errors = 0;
                int backup = 0;
                int exists;

                /* strip leading "./" from path entries */
                if(path[0] == '.' && path[1] == '/') {
                        path += 2;
                }

                if(*path == '.') {
                        const char *dbfile = NULL;

                        if(strcmp(path, ".INSTALL") == 0) {
                                dbfile = "install";
                        } else if(strcmp(path, ".CHANGELOG") == 0) {
                                dbfile = "changelog";
                        } else {
                                continue;
                        }

                        /* Do not append root directory as alpm_option_get_dbpath is already
                         * an absoute path */
                        filepath_len = snprintf(filepath, PATH_MAX, "%slocal/%s-%s/%s",
                                        alpm_option_get_dbpath(config->handle),
                                        pkgname, alpm_pkg_get_version(pkg), dbfile);
                        if(filepath_len >= PATH_MAX) {
                                pm_printf(ALPM_LOG_WARNING, _("path too long: %slocal/%s-%s/%s\n"),
                                                alpm_option_get_dbpath(config->handle),
                                                pkgname, alpm_pkg_get_version(pkg), dbfile);
                                continue;
                        }
                } else {
                        filepath_len = snprintf(filepath, PATH_MAX, "%s%s", root, path);
                        if(filepath_len >= PATH_MAX) {
                                pm_printf(ALPM_LOG_WARNING, _("path too long: %s%s\n"), root, path);
                                continue;
                        }
                }

                file_count++;

                exists = check_file_exists(pkgname, filepath, rootlen, &st);
                if(exists == 1) {
                        errors++;
                        continue;
                } else if(exists == -1) {
                        /* NoExtract */
                        continue;
                }

                type = archive_entry_filetype(entry);

                if(type != AE_IFDIR && type != AE_IFREG && type != AE_IFLNK) {
                        pm_printf(ALPM_LOG_WARNING, _("file type not recognized: %s%s\n"), root, path);
                        continue;
                }

                if(check_file_type(pkgname, filepath, &st, entry) == 1) {
                        errors++;
                        continue;
                }

                file_errors += check_file_permissions(pkgname, filepath, &st, entry);

                if(type == AE_IFLNK) {
                        file_errors += check_file_link(pkgname, filepath, &st, entry);
                }

                /* the following checks are expected to fail if a backup file has been
                   modified */
                for(lp = alpm_pkg_get_backup(pkg); lp; lp = lp->next) {
                        alpm_backup_t *bl = lp->data;

                        if(strcmp(path, bl->name) == 0) {
                                backup = 1;
                                break;
                        }
                }

                if(type != AE_IFDIR) {
                        /* file or symbolic link */
                        file_errors += check_file_time(pkgname, filepath, &st, entry, backup);
                }

                if(type == AE_IFREG) {
                        file_errors += check_file_size(pkgname, filepath, &st, entry, backup);
                        /* file_errors += check_file_md5sum(pkgname, filepath, &st, entry, backup); */
                }

                if(config->quiet && file_errors) {
                        printf("%s %s\n", pkgname, filepath);
                }

                errors += (file_errors != 0 ? 1 : 0);
        }

        alpm_pkg_mtree_close(pkg, mtree);

        if(!config->quiet) {
                printf(_n("%s%jd total file, ""%s%jd total files, ",
                                        (unsigned long)file_count), pkgname, (intmax_t)file_count);
                printf(_n("%jd altered file\n""%jd altered files\n",
                                        (unsigned long)errors), (intmax_t)errors);
        }

        return (errors != 0 ? 1 : 0);
}

/* vim: set noet: */