2009-08-17 22:37:20 +05:30
|
|
|
/*-
|
2010-01-25 20:45:33 +05:30
|
|
|
* Copyright (c) 2009-2010 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>
|
|
|
|
|
|
|
|
#include <xbps_api.h>
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks package integrity of an installed package. This
|
|
|
|
* consists in four tasks:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 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
|
|
|
|
xbps_check_pkg_integrity_all(void)
|
|
|
|
{
|
|
|
|
prop_dictionary_t d;
|
|
|
|
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
|
|
|
int rv = 0;
|
|
|
|
size_t npkgs = 0, nbrokenpkgs = 0;
|
|
|
|
|
2009-11-26 07:52:50 +05:30
|
|
|
d = xbps_regpkgs_dictionary_init();
|
2009-10-06 04:12:37 +05:30
|
|
|
if (d == NULL)
|
|
|
|
return ENODEV;
|
|
|
|
|
|
|
|
iter = xbps_get_array_iter_from_dict(d, "packages");
|
2009-11-26 07:52:50 +05:30
|
|
|
if (iter == NULL) {
|
|
|
|
rv = ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-10-06 04:12:37 +05:30
|
|
|
|
|
|
|
while ((obj = prop_object_iterator_next(iter)) != NULL) {
|
2009-11-23 15:16:51 +05:30
|
|
|
if (!prop_dictionary_get_cstring_nocopy(obj,
|
|
|
|
"pkgname", &pkgname)) {
|
|
|
|
prop_object_iterator_release(iter);
|
2009-11-26 07:52:50 +05:30
|
|
|
rv = errno;
|
|
|
|
goto out;
|
2009-11-23 15:16:51 +05:30
|
|
|
}
|
|
|
|
if (!prop_dictionary_get_cstring_nocopy(obj,
|
|
|
|
"version", &version)) {
|
|
|
|
prop_object_iterator_release(iter);
|
2009-11-26 07:52:50 +05:30
|
|
|
rv = errno;
|
|
|
|
goto out;
|
2009-11-23 15:16:51 +05:30
|
|
|
}
|
2009-11-01 12:02:06 +05:30
|
|
|
printf("Checking %s-%s ...\n", pkgname, version);
|
2009-10-06 04:12:37 +05:30
|
|
|
if ((rv = xbps_check_pkg_integrity(pkgname)) != 0)
|
|
|
|
nbrokenpkgs++;
|
|
|
|
npkgs++;
|
2009-11-01 12:02:06 +05:30
|
|
|
printf("\033[1A\033[K");
|
2009-10-06 04:12:37 +05:30
|
|
|
}
|
|
|
|
printf("%zu package%s processed: %zu broken.\n", npkgs,
|
|
|
|
npkgs == 1 ? "" : "s", nbrokenpkgs);
|
|
|
|
|
2009-11-26 07:52:50 +05:30
|
|
|
out:
|
|
|
|
if (iter)
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
|
|
|
|
xbps_regpkgs_dictionary_release();
|
|
|
|
|
2009-10-06 04:12:37 +05:30
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
int
|
|
|
|
xbps_check_pkg_integrity(const char *pkgname)
|
|
|
|
{
|
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;
|
2009-10-27 16:45:47 +05:30
|
|
|
const char *file, *sha256, *reqpkg;
|
2009-08-17 22:37:20 +05:30
|
|
|
char *path;
|
|
|
|
int rv = 0;
|
2009-10-06 04:12:37 +05:30
|
|
|
bool broken = false, files_broken = false;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for props.plist metadata file.
|
|
|
|
*/
|
2009-10-27 16:45:47 +05:30
|
|
|
path = xbps_xasprintf("%s/%s/metadata/%s/%s", xbps_get_rootdir(),
|
2009-08-17 22:37:20 +05:30
|
|
|
XBPS_META_PATH, pkgname, XBPS_PKGPROPS);
|
|
|
|
if (path == NULL) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-04-20 17:52:38 +05:30
|
|
|
propsd = prop_dictionary_internalize_from_zfile(path);
|
2009-08-17 22:37:20 +05:30
|
|
|
free(path);
|
|
|
|
if (propsd == NULL) {
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"E: %s: unexistent %s metadata file.\n", pkgname,
|
2009-08-17 22:37:20 +05:30
|
|
|
XBPS_PKGPROPS);
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
} else if (prop_object_type(propsd) != PROP_TYPE_DICTIONARY) {
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"E: %s: invalid %s metadata file.\n", pkgname,
|
2009-08-17 22:37:20 +05:30
|
|
|
XBPS_PKGPROPS);
|
|
|
|
rv = EINVAL;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
} else if (prop_dictionary_count(propsd) == 0) {
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"E: %s: incomplete %s metadata file.\n", pkgname,
|
2009-08-17 22:37:20 +05:30
|
|
|
XBPS_PKGPROPS);
|
|
|
|
rv = EINVAL;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for files.plist metadata file.
|
|
|
|
*/
|
2009-10-27 16:45:47 +05:30
|
|
|
path = xbps_xasprintf("%s/%s/metadata/%s/%s", xbps_get_rootdir(),
|
2009-08-17 22:37:20 +05:30
|
|
|
XBPS_META_PATH, pkgname, XBPS_PKGFILES);
|
|
|
|
if (path == NULL) {
|
|
|
|
rv = errno;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
|
2010-04-20 17:52:38 +05:30
|
|
|
filesd = prop_dictionary_internalize_from_zfile(path);
|
2009-08-17 22:37:20 +05:30
|
|
|
free(path);
|
|
|
|
if (filesd == NULL) {
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"E: %s: unexistent %s metadata file.\n", pkgname,
|
2009-08-17 22:37:20 +05:30
|
|
|
XBPS_PKGPROPS);
|
|
|
|
rv = ENOENT;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
} else if (prop_object_type(filesd) != PROP_TYPE_DICTIONARY) {
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"E: %s: invalid %s metadata file.\n", pkgname,
|
2009-08-17 22:37:20 +05:30
|
|
|
XBPS_PKGFILES);
|
|
|
|
rv = EINVAL;
|
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) {
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"E: %s: incomplete %s metadata file.\n", pkgname,
|
2009-08-17 22:37:20 +05:30
|
|
|
XBPS_PKGFILES);
|
|
|
|
rv = EINVAL;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
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) {
|
|
|
|
iter = xbps_get_array_iter_from_dict(filesd, "files");
|
|
|
|
if (iter == NULL) {
|
|
|
|
rv = ENOMEM;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
while ((obj = prop_object_iterator_next(iter))) {
|
2009-11-23 15:16:51 +05:30
|
|
|
if (!prop_dictionary_get_cstring_nocopy(obj,
|
|
|
|
"file", &file)) {
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
rv = errno;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-11-23 15:16:51 +05:30
|
|
|
}
|
2009-10-27 16:45:47 +05:30
|
|
|
path = xbps_xasprintf("%s/%s",
|
|
|
|
xbps_get_rootdir(), file);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (path == NULL) {
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
rv = errno;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
2009-11-23 15:16:51 +05:30
|
|
|
if (!prop_dictionary_get_cstring_nocopy(obj,
|
|
|
|
"sha256", &sha256)) {
|
|
|
|
free(path);
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
rv = errno;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-11-23 15:16:51 +05:30
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
rv = xbps_check_file_hash(path, sha256);
|
|
|
|
switch (rv) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case ENOENT:
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr, "%s: unexistent file %s.\n",
|
2009-08-17 22:37:20 +05:30
|
|
|
pkgname, file);
|
|
|
|
files_broken = true;
|
|
|
|
break;
|
|
|
|
case ERANGE:
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr, "%s: hash mismatch for %s.\n",
|
2009-08-17 22:37:20 +05:30
|
|
|
pkgname, file);
|
|
|
|
files_broken = true;
|
|
|
|
break;
|
|
|
|
default:
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"%s: unexpected error for %s (%s)\n",
|
2009-08-17 22:37:20 +05:30
|
|
|
pkgname, file, strerror(rv));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
2009-10-06 04:12:37 +05:30
|
|
|
if (files_broken) {
|
|
|
|
broken = true;
|
2009-08-17 22:37:20 +05:30
|
|
|
printf("%s: files check FAILED.\n", pkgname);
|
2009-10-06 04:12:37 +05:30
|
|
|
}
|
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) {
|
|
|
|
iter = xbps_get_array_iter_from_dict(filesd, "conf_files");
|
|
|
|
if (iter == NULL) {
|
|
|
|
rv = ENOMEM;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
while ((obj = prop_object_iterator_next(iter))) {
|
2009-11-23 15:16:51 +05:30
|
|
|
if (!prop_dictionary_get_cstring_nocopy(obj,
|
|
|
|
"file", &file)) {
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
rv = errno;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-11-23 15:16:51 +05:30
|
|
|
}
|
2009-10-27 16:45:47 +05:30
|
|
|
path = xbps_xasprintf("%s/%s",
|
|
|
|
xbps_get_rootdir(), file);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (path == NULL) {
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
rv = ENOMEM;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
if ((rv = access(path, R_OK)) == -1) {
|
|
|
|
if (errno == ENOENT) {
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"%s: unexistent file %s\n",
|
2009-08-17 22:37:20 +05:30
|
|
|
pkgname, file);
|
2009-10-06 04:12:37 +05:30
|
|
|
broken = true;
|
2009-08-17 22:37:20 +05:30
|
|
|
} else
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"%s: unexpected error for "
|
2009-08-17 22:37:20 +05:30
|
|
|
"%s (%s)\n", pkgname, file,
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
if (rv != 0)
|
|
|
|
printf("%s: configuration files check FAILED.\n",
|
|
|
|
pkgname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for missing run time dependencies.
|
|
|
|
*/
|
|
|
|
if (xbps_pkg_has_rundeps(propsd)) {
|
|
|
|
iter = xbps_get_array_iter_from_dict(propsd, "run_depends");
|
|
|
|
if (iter == NULL) {
|
|
|
|
rv = ENOMEM;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
while ((obj = prop_object_iterator_next(iter))) {
|
|
|
|
reqpkg = prop_string_cstring_nocopy(obj);
|
2009-11-23 15:16:51 +05:30
|
|
|
if (reqpkg == NULL) {
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
rv = EINVAL;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-11-23 15:16:51 +05:30
|
|
|
}
|
2009-11-07 09:26:20 +05:30
|
|
|
if (xbps_check_is_installed_pkg(reqpkg) <= 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
rv = ENOENT;
|
|
|
|
printf("%s: dependency not satisfied: %s\n",
|
|
|
|
pkgname, reqpkg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
2009-10-06 04:12:37 +05:30
|
|
|
if (rv == ENOENT) {
|
2009-08-17 22:37:20 +05:30
|
|
|
printf("%s: run-time dependency check FAILED.\n",
|
|
|
|
pkgname);
|
2009-10-06 04:12:37 +05:30
|
|
|
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);
|
|
|
|
if (pkgd)
|
|
|
|
prop_object_release(pkgd);
|
2009-10-06 04:12:37 +05:30
|
|
|
if (broken)
|
|
|
|
rv = EINVAL;
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
return rv;
|
|
|
|
}
|