2009-08-17 22:37:20 +05:30
|
|
|
/*-
|
2011-01-25 08:03:51 +05:30
|
|
|
* Copyright (c) 2009-2011 Juan Romero Pardines.
|
2009-08-17 22:37:20 +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>
|
2010-11-13 07:48:58 +05:30
|
|
|
#include <assert.h>
|
2010-11-19 18:10:13 +05:30
|
|
|
#include <unistd.h>
|
2011-06-01 11:01:38 +05:30
|
|
|
#include <sys/param.h>
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
#include <xbps_api.h>
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks package integrity of an installed package. This
|
2011-06-01 11:01:38 +05:30
|
|
|
* consists in five tasks:
|
2009-08-17 22:37:20 +05:30
|
|
|
*
|
|
|
|
* o Check for metadata files (files.plist and props.plist),
|
|
|
|
* we only check if the file exists and its dictionary can
|
|
|
|
* be externalized and is not empty.
|
|
|
|
* o Check for missing installed files.
|
2011-06-01 11:01:38 +05:30
|
|
|
* o Check for target file in symlinks, so that we can check that
|
|
|
|
* they have not been modified.
|
2009-08-17 22:37:20 +05:30
|
|
|
* o Check the hash for all installed files, except
|
|
|
|
* configuration files (which is expected if they are modified).
|
|
|
|
* o Check for missing run time dependencies.
|
|
|
|
*/
|
|
|
|
|
2009-10-06 04:12:37 +05:30
|
|
|
int
|
2011-07-27 20:43:54 +05:30
|
|
|
check_pkg_integrity_all(void)
|
2009-10-06 04:12:37 +05:30
|
|
|
{
|
2011-06-04 17:07:53 +05:30
|
|
|
const struct xbps_handle *xhp;
|
2009-10-06 04:12:37 +05:30
|
|
|
prop_object_t obj;
|
2009-11-26 07:52:50 +05:30
|
|
|
prop_object_iterator_t iter = NULL;
|
2009-11-01 12:02:06 +05:30
|
|
|
const char *pkgname, *version;
|
2009-10-06 04:12:37 +05:30
|
|
|
size_t npkgs = 0, nbrokenpkgs = 0;
|
|
|
|
|
2011-06-04 17:07:53 +05:30
|
|
|
xhp = xbps_handle_get();
|
|
|
|
iter = xbps_array_iter_from_dict(xhp->regpkgdb_dictionary, "packages");
|
2011-07-28 19:54:16 +05:30
|
|
|
if (iter == NULL)
|
|
|
|
return -1;
|
2009-10-06 04:12:37 +05:30
|
|
|
|
|
|
|
while ((obj = prop_object_iterator_next(iter)) != NULL) {
|
2010-11-06 11:14:00 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
|
2009-11-01 12:02:06 +05:30
|
|
|
printf("Checking %s-%s ...\n", pkgname, version);
|
2011-07-28 19:54:16 +05:30
|
|
|
if (check_pkg_integrity(pkgname) != 0)
|
2009-10-06 04:12:37 +05:30
|
|
|
nbrokenpkgs++;
|
2011-07-28 19:54:16 +05:30
|
|
|
|
2009-10-06 04:12:37 +05:30
|
|
|
npkgs++;
|
|
|
|
}
|
2011-07-28 19:54:16 +05:30
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
|
2009-10-06 04:12:37 +05:30
|
|
|
printf("%zu package%s processed: %zu broken.\n", npkgs,
|
|
|
|
npkgs == 1 ? "" : "s", nbrokenpkgs);
|
|
|
|
|
2011-07-28 19:54:16 +05:30
|
|
|
return 0;
|
2009-10-06 04:12:37 +05:30
|
|
|
}
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
int
|
2011-07-27 20:43:54 +05:30
|
|
|
check_pkg_integrity(const char *pkgname)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2011-07-27 20:43:54 +05:30
|
|
|
struct xbps_handle *xhp;
|
2010-04-29 02:41:07 +05:30
|
|
|
prop_dictionary_t pkgd, propsd = NULL, filesd = NULL;
|
2009-08-17 22:37:20 +05:30
|
|
|
prop_array_t array;
|
|
|
|
prop_object_t obj;
|
|
|
|
prop_object_iterator_t iter;
|
2011-06-01 11:01:38 +05:30
|
|
|
const char *file, *sha256, *reqpkg, *tgt = NULL;
|
|
|
|
char *path, buf[PATH_MAX];
|
2009-08-17 22:37:20 +05:30
|
|
|
int rv = 0;
|
2011-07-28 19:54:16 +05:30
|
|
|
bool broken = false, test_broken = false;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
2011-02-21 22:12:47 +05:30
|
|
|
xhp = xbps_handle_get();
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
pkgd = xbps_find_pkg_dict_installed(pkgname, false);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (pkgd == NULL) {
|
|
|
|
printf("Package %s is not installed.\n", pkgname);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-11-08 06:32:35 +05:30
|
|
|
prop_object_release(pkgd);
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for props.plist metadata file.
|
|
|
|
*/
|
2011-06-01 13:07:32 +05:30
|
|
|
propsd = xbps_dictionary_from_metadata_plist(pkgname, XBPS_PKGPROPS);
|
2011-01-25 08:03:51 +05:30
|
|
|
if (prop_object_type(propsd) != PROP_TYPE_DICTIONARY) {
|
2011-07-28 19:54:16 +05:30
|
|
|
xbps_error_printf("%s: unexistent %s or invalid metadata "
|
|
|
|
"file.\n", pkgname, XBPS_PKGPROPS);
|
|
|
|
broken = true;
|
2009-08-17 22:37:20 +05:30
|
|
|
goto out;
|
|
|
|
} else if (prop_dictionary_count(propsd) == 0) {
|
2011-07-28 19:54:16 +05:30
|
|
|
xbps_error_printf("%s: incomplete %s metadata file.\n",
|
|
|
|
pkgname, XBPS_PKGPROPS);
|
|
|
|
broken = true;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for files.plist metadata file.
|
|
|
|
*/
|
2011-06-01 13:07:32 +05:30
|
|
|
filesd = xbps_dictionary_from_metadata_plist(pkgname, XBPS_PKGFILES);
|
2011-01-25 08:03:51 +05:30
|
|
|
if (prop_object_type(filesd) != PROP_TYPE_DICTIONARY) {
|
2011-07-28 19:54:16 +05:30
|
|
|
xbps_error_printf("%s: unexistent %s or invalid metadata "
|
|
|
|
"file.\n", pkgname, XBPS_PKGFILES);
|
|
|
|
broken = true;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
} else if (prop_dictionary_count(filesd) == 0) {
|
2011-07-28 19:54:16 +05:30
|
|
|
xbps_error_printf("%s: incomplete %s metadata file.\n",
|
|
|
|
pkgname, XBPS_PKGFILES);
|
|
|
|
broken = true;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
|
2011-06-01 11:01:38 +05:30
|
|
|
/*
|
|
|
|
* Check for target files in symlinks.
|
|
|
|
*/
|
|
|
|
array = prop_dictionary_get(filesd, "links");
|
|
|
|
if ((prop_object_type(array) == PROP_TYPE_ARRAY) &&
|
|
|
|
prop_array_count(array) > 0) {
|
2011-06-01 13:07:32 +05:30
|
|
|
iter = xbps_array_iter_from_dict(filesd, "links");
|
2011-07-28 19:54:16 +05:30
|
|
|
if (iter == NULL)
|
|
|
|
abort();
|
|
|
|
|
2011-06-01 11:01:38 +05:30
|
|
|
while ((obj = prop_object_iterator_next(iter))) {
|
|
|
|
if (!prop_dictionary_get_cstring_nocopy(obj, "target", &tgt))
|
|
|
|
continue;
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "file", &file);
|
2011-06-22 13:03:39 +05:30
|
|
|
if (strcmp(tgt, "") == 0) {
|
2011-07-28 19:54:16 +05:30
|
|
|
xbps_warn_printf("%s: `%s' symlink with "
|
|
|
|
"empty target object!\n", pkgname, file);
|
2011-06-22 13:03:39 +05:30
|
|
|
continue;
|
|
|
|
}
|
2011-10-17 16:07:15 +05:30
|
|
|
path = xbps_xasprintf("%s/%s",
|
|
|
|
prop_string_cstring_nocopy(xhp->rootdir), file);
|
2011-07-28 19:54:16 +05:30
|
|
|
if (path == NULL)
|
|
|
|
abort();
|
|
|
|
|
|
|
|
memset(&buf, 0, sizeof(buf));
|
2011-09-28 14:36:24 +05:30
|
|
|
if (realpath(path, buf) == NULL) {
|
|
|
|
xbps_error_printf("%s: broken symlink `%s': "
|
|
|
|
"%s\n", pkgname, file, strerror(errno));
|
|
|
|
test_broken = true;
|
|
|
|
continue;
|
|
|
|
}
|
2011-07-28 19:54:16 +05:30
|
|
|
|
|
|
|
free(path);
|
2011-10-17 16:07:15 +05:30
|
|
|
if (!prop_string_equals_cstring(xhp->rootdir, "/") &&
|
|
|
|
strstr(buf, prop_string_cstring_nocopy(xhp->rootdir)))
|
|
|
|
path = buf + prop_string_size(xhp->rootdir);
|
2011-07-28 19:54:16 +05:30
|
|
|
else
|
|
|
|
path = buf;
|
|
|
|
|
|
|
|
if (strcmp(path, tgt)) {
|
|
|
|
xbps_error_printf("%s: modified symlink `%s', "
|
2011-06-01 11:01:38 +05:30
|
|
|
"target: `%s' (shall be: `%s')\n",
|
2011-07-28 19:54:16 +05:30
|
|
|
pkgname, file, tgt, path);
|
|
|
|
test_broken = true;
|
2011-06-01 11:01:38 +05:30
|
|
|
}
|
2011-07-28 19:54:16 +05:30
|
|
|
path = NULL;
|
2011-06-01 11:01:38 +05:30
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
}
|
2011-07-28 19:54:16 +05:30
|
|
|
if (test_broken) {
|
|
|
|
test_broken = false;
|
|
|
|
xbps_error_printf("%s: links check FAILED.\n", pkgname);
|
|
|
|
broken = true;
|
|
|
|
}
|
2011-06-01 11:01:38 +05:30
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
|
|
|
* Check for missing files and its hash.
|
|
|
|
*/
|
|
|
|
array = prop_dictionary_get(filesd, "files");
|
|
|
|
if ((prop_object_type(array) == PROP_TYPE_ARRAY) &&
|
|
|
|
prop_array_count(array) > 0) {
|
2011-06-01 13:07:32 +05:30
|
|
|
iter = xbps_array_iter_from_dict(filesd, "files");
|
2011-07-28 19:54:16 +05:30
|
|
|
if (iter == NULL)
|
|
|
|
abort();
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
while ((obj = prop_object_iterator_next(iter))) {
|
2010-11-06 11:14:00 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "file", &file);
|
2011-10-17 16:07:15 +05:30
|
|
|
path = xbps_xasprintf("%s/%s",
|
|
|
|
prop_string_cstring_nocopy(xhp->rootdir), file);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (path == NULL) {
|
|
|
|
prop_object_iterator_release(iter);
|
2011-07-28 19:54:16 +05:30
|
|
|
abort();
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
2010-11-06 11:14:00 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj,
|
|
|
|
"sha256", &sha256);
|
2011-06-01 13:07:32 +05:30
|
|
|
rv = xbps_file_hash_check(path, sha256);
|
2009-08-17 22:37:20 +05:30
|
|
|
switch (rv) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case ENOENT:
|
2011-07-28 19:54:16 +05:30
|
|
|
xbps_error_printf("%s: unexistent file %s.\n",
|
2009-08-17 22:37:20 +05:30
|
|
|
pkgname, file);
|
2011-07-28 19:54:16 +05:30
|
|
|
test_broken = true;
|
2009-08-17 22:37:20 +05:30
|
|
|
break;
|
|
|
|
case ERANGE:
|
2011-07-28 19:54:16 +05:30
|
|
|
xbps_error_printf("%s: hash mismatch for %s.\n",
|
2009-08-17 22:37:20 +05:30
|
|
|
pkgname, file);
|
2011-07-28 19:54:16 +05:30
|
|
|
test_broken = true;
|
2009-08-17 22:37:20 +05:30
|
|
|
break;
|
|
|
|
default:
|
2011-07-28 19:54:16 +05:30
|
|
|
xbps_error_printf(
|
2011-06-06 12:39:30 +05:30
|
|
|
"%s: can't check `%s' (%s)\n",
|
2009-08-17 22:37:20 +05:30
|
|
|
pkgname, file, strerror(rv));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
2011-07-28 19:54:16 +05:30
|
|
|
}
|
|
|
|
if (test_broken) {
|
|
|
|
test_broken = false;
|
|
|
|
broken = true;
|
|
|
|
xbps_error_printf("%s: files check FAILED.\n", pkgname);
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for missing configuration files.
|
|
|
|
*/
|
|
|
|
array = prop_dictionary_get(filesd, "conf_files");
|
|
|
|
if (array && prop_object_type(array) == PROP_TYPE_ARRAY &&
|
|
|
|
prop_array_count(array) > 0) {
|
2011-06-01 13:07:32 +05:30
|
|
|
iter = xbps_array_iter_from_dict(filesd, "conf_files");
|
2011-07-28 19:54:16 +05:30
|
|
|
if (iter == NULL)
|
|
|
|
abort();
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
while ((obj = prop_object_iterator_next(iter))) {
|
2010-11-06 11:14:00 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "file", &file);
|
2011-10-17 16:07:15 +05:30
|
|
|
path = xbps_xasprintf("%s/%s",
|
|
|
|
prop_string_cstring_nocopy(xhp->rootdir), file);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (path == NULL) {
|
|
|
|
prop_object_iterator_release(iter);
|
2011-07-28 19:54:16 +05:30
|
|
|
abort();
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
if ((rv = access(path, R_OK)) == -1) {
|
|
|
|
if (errno == ENOENT) {
|
2011-07-28 19:54:16 +05:30
|
|
|
xbps_error_printf(
|
2010-01-15 19:49:16 +05:30
|
|
|
"%s: unexistent file %s\n",
|
2009-08-17 22:37:20 +05:30
|
|
|
pkgname, file);
|
2011-07-28 19:54:16 +05:30
|
|
|
test_broken = true;
|
2009-08-17 22:37:20 +05:30
|
|
|
} else
|
2011-07-28 19:54:16 +05:30
|
|
|
xbps_error_printf(
|
2011-06-06 12:39:30 +05:30
|
|
|
"%s: can't check `%s' (%s)\n",
|
|
|
|
pkgname, file,
|
2009-08-17 22:37:20 +05:30
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
2011-07-28 19:54:16 +05:30
|
|
|
}
|
|
|
|
if (test_broken) {
|
|
|
|
test_broken = false;
|
|
|
|
xbps_error_printf("%s: conf files check FAILED.\n", pkgname);
|
|
|
|
broken = true;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for missing run time dependencies.
|
|
|
|
*/
|
2011-07-28 19:54:16 +05:30
|
|
|
if (!xbps_pkg_has_rundeps(propsd))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
iter = xbps_array_iter_from_dict(propsd, "run_depends");
|
|
|
|
if (iter == NULL)
|
|
|
|
abort();
|
|
|
|
|
|
|
|
while ((obj = prop_object_iterator_next(iter))) {
|
|
|
|
reqpkg = prop_string_cstring_nocopy(obj);
|
|
|
|
if (reqpkg == NULL) {
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
abort();
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
2011-07-28 19:54:16 +05:30
|
|
|
if (xbps_check_is_installed_pkg_by_pattern(reqpkg) <= 0) {
|
|
|
|
xbps_error_printf("%s: dependency not satisfied: %s\n",
|
|
|
|
pkgname, reqpkg);
|
|
|
|
test_broken = true;
|
2009-10-06 04:12:37 +05:30
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
2011-07-28 19:54:16 +05:30
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
if (test_broken) {
|
|
|
|
xbps_error_printf("%s: rundeps check FAILED.\n", pkgname);
|
|
|
|
broken = true;
|
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
out:
|
2010-01-25 20:45:33 +05:30
|
|
|
if (filesd)
|
|
|
|
prop_object_release(filesd);
|
|
|
|
if (propsd)
|
|
|
|
prop_object_release(propsd);
|
2009-10-06 04:12:37 +05:30
|
|
|
if (broken)
|
2011-07-28 19:54:16 +05:30
|
|
|
return -1;
|
2009-10-06 04:12:37 +05:30
|
|
|
|
2011-07-28 19:54:16 +05:30
|
|
|
return 0;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|