2012-11-02 19:34:25 +05:30
|
|
|
/*-
|
2015-02-22 23:15:47 +05:30
|
|
|
* Copyright (c) 2011-2015 Juan Romero Pardines.
|
2012-11-02 19:34:25 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
2013-06-20 16:01:02 +05:30
|
|
|
#include <xbps.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks package integrity of an installed package.
|
|
|
|
* The following tasks are processed in that order:
|
|
|
|
*
|
|
|
|
* o Check for missing installed files.
|
|
|
|
*
|
|
|
|
* o Check the hash for all installed files, except
|
|
|
|
* configuration files (which is expected if they are modified).
|
|
|
|
*
|
2015-02-22 23:15:47 +05:30
|
|
|
* o Compares stored file modification time.
|
|
|
|
*
|
2012-11-02 19:34:25 +05:30
|
|
|
* Return 0 if test ran successfully, 1 otherwise and -1 on error.
|
|
|
|
*/
|
2015-02-22 23:15:47 +05:30
|
|
|
static bool
|
|
|
|
check_file_mtime(xbps_dictionary_t d, const char *pkg, const char *path)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
uint64_t mtime = 0;
|
2019-06-27 20:39:43 +05:30
|
|
|
const char *file = NULL;
|
2015-02-22 23:15:47 +05:30
|
|
|
|
|
|
|
/* if obj is not there, skip silently */
|
|
|
|
if (!xbps_dictionary_get_uint64(d, "mtime", &mtime))
|
|
|
|
return false;
|
|
|
|
|
2015-02-27 15:08:50 +05:30
|
|
|
/* if file is mutable, we don't care if it does not match */
|
|
|
|
if (xbps_dictionary_get(d, "mutable"))
|
|
|
|
return false;
|
|
|
|
|
2015-02-22 23:15:47 +05:30
|
|
|
if (stat(path, &sb) == -1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if ((uint64_t)sb.st_mtime != mtime) {
|
|
|
|
xbps_dictionary_get_cstring_nocopy(d, "file", &file);
|
|
|
|
xbps_error_printf("%s: %s mtime mismatch "
|
|
|
|
"(current: %ju, stored %ju)\n",
|
|
|
|
pkg, file, (uint64_t)sb.st_mtime, mtime);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
int
|
2012-11-17 22:13:54 +05:30
|
|
|
check_pkg_files(struct xbps_handle *xhp, const char *pkgname, void *arg)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_t array;
|
|
|
|
xbps_object_t obj;
|
|
|
|
xbps_object_iterator_t iter;
|
|
|
|
xbps_dictionary_t pkg_filesd = arg;
|
2019-06-27 20:39:43 +05:30
|
|
|
const char *file = NULL, *sha256 = NULL;
|
2012-11-02 19:34:25 +05:30
|
|
|
char *path;
|
2015-02-26 16:31:20 +05:30
|
|
|
bool mutable, test_broken = false;
|
|
|
|
int rv = 0, errors = 0;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
array = xbps_dictionary_get(pkg_filesd, "files");
|
|
|
|
if (array != NULL && xbps_array_count(array) > 0) {
|
2012-11-02 19:34:25 +05:30
|
|
|
iter = xbps_array_iter_from_dict(pkg_filesd, "files");
|
|
|
|
if (iter == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
while ((obj = xbps_object_iterator_next(iter))) {
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "file", &file);
|
2012-11-02 19:34:25 +05:30
|
|
|
path = xbps_xasprintf("%s/%s", xhp->rootdir, file);
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(obj,
|
2012-11-02 19:34:25 +05:30
|
|
|
"sha256", &sha256);
|
|
|
|
rv = xbps_file_hash_check(path, sha256);
|
|
|
|
switch (rv) {
|
|
|
|
case 0:
|
2015-02-22 23:15:47 +05:30
|
|
|
if (check_file_mtime(obj, pkgname, path)) {
|
|
|
|
test_broken = true;
|
|
|
|
}
|
2015-02-23 12:56:20 +05:30
|
|
|
free(path);
|
2012-11-02 19:34:25 +05:30
|
|
|
break;
|
|
|
|
case ENOENT:
|
|
|
|
xbps_error_printf("%s: unexistent file %s.\n",
|
|
|
|
pkgname, file);
|
2015-02-23 12:56:20 +05:30
|
|
|
free(path);
|
2012-11-02 19:34:25 +05:30
|
|
|
test_broken = true;
|
|
|
|
break;
|
|
|
|
case ERANGE:
|
|
|
|
mutable = false;
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_bool(obj,
|
2012-11-02 19:34:25 +05:30
|
|
|
"mutable", &mutable);
|
|
|
|
if (!mutable) {
|
|
|
|
xbps_error_printf("%s: hash mismatch "
|
|
|
|
"for %s.\n", pkgname, file);
|
|
|
|
test_broken = true;
|
|
|
|
}
|
2015-02-23 12:56:20 +05:30
|
|
|
free(path);
|
2012-11-02 19:34:25 +05:30
|
|
|
break;
|
|
|
|
default:
|
|
|
|
xbps_error_printf(
|
|
|
|
"%s: can't check `%s' (%s)\n",
|
|
|
|
pkgname, file, strerror(rv));
|
2015-02-23 12:56:20 +05:30
|
|
|
free(path);
|
2012-11-02 19:34:25 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_iterator_release(iter);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
if (test_broken) {
|
|
|
|
xbps_error_printf("%s: files check FAILED.\n", pkgname);
|
|
|
|
test_broken = false;
|
2015-02-26 16:31:20 +05:30
|
|
|
errors++;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for missing configuration files.
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
array = xbps_dictionary_get(pkg_filesd, "conf_files");
|
|
|
|
if (array != NULL && xbps_array_count(array) > 0) {
|
2012-11-02 19:34:25 +05:30
|
|
|
iter = xbps_array_iter_from_dict(pkg_filesd, "conf_files");
|
|
|
|
if (iter == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
while ((obj = xbps_object_iterator_next(iter))) {
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "file", &file);
|
2012-11-02 19:34:25 +05:30
|
|
|
path = xbps_xasprintf("%s/%s", xhp->rootdir, file);
|
2013-04-04 13:47:00 +05:30
|
|
|
if (access(path, R_OK) == -1) {
|
2012-11-02 19:34:25 +05:30
|
|
|
if (errno == ENOENT) {
|
|
|
|
xbps_error_printf(
|
|
|
|
"%s: unexistent file %s\n",
|
|
|
|
pkgname, file);
|
|
|
|
test_broken = true;
|
|
|
|
} else
|
|
|
|
xbps_error_printf(
|
|
|
|
"%s: can't check `%s' (%s)\n",
|
|
|
|
pkgname, file,
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_iterator_release(iter);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
if (test_broken) {
|
|
|
|
xbps_error_printf("%s: conf files check FAILED.\n", pkgname);
|
2015-02-26 16:31:20 +05:30
|
|
|
errors++;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
|
2015-02-26 16:31:20 +05:30
|
|
|
return errors ? -1 : 0;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|