2012-11-02 19:34:25 +05:30
|
|
|
/*-
|
2015-02-26 16:31:20 +05:30
|
|
|
* Copyright (c) 2009-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.
|
|
|
|
*/
|
|
|
|
|
2012-12-14 08:01:24 +05:30
|
|
|
#include <sys/param.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2012-12-14 08:01:24 +05:30
|
|
|
#include <assert.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-06-20 16:01:02 +05:30
|
|
|
#include <xbps.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
#include "defs.h"
|
|
|
|
|
2013-07-27 13:17:16 +05:30
|
|
|
static int
|
2018-07-18 07:54:26 +05:30
|
|
|
pkgdb_cb(struct xbps_handle *xhp UNUSED,
|
2013-08-29 14:15:30 +05:30
|
|
|
xbps_object_t obj,
|
2018-07-18 07:54:26 +05:30
|
|
|
const char *key UNUSED,
|
2015-02-26 16:31:20 +05:30
|
|
|
void *arg,
|
2018-07-18 07:54:26 +05:30
|
|
|
bool *done UNUSED)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2019-06-27 20:39:43 +05:30
|
|
|
const char *pkgver = NULL;
|
2020-02-09 00:01:29 +05:30
|
|
|
char pkgname[XBPS_NAME_SIZE];
|
2015-02-26 16:31:20 +05:30
|
|
|
int rv, *errors = (int *)arg;
|
2012-11-17 22:13:54 +05:30
|
|
|
|
2013-07-27 13:17:16 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
|
|
|
if (xhp->flags & XBPS_FLAG_VERBOSE)
|
|
|
|
printf("Checking %s ...\n", pkgver);
|
2012-11-17 22:13:54 +05:30
|
|
|
|
2020-02-09 00:01:29 +05:30
|
|
|
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
|
|
|
|
abort();
|
|
|
|
}
|
2015-02-26 16:31:20 +05:30
|
|
|
if ((rv = check_pkg_integrity(xhp, obj, pkgname)) != 0)
|
|
|
|
*errors += 1;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2015-02-26 16:31:20 +05:30
|
|
|
return 0;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
check_pkg_integrity_all(struct xbps_handle *xhp)
|
|
|
|
{
|
2015-02-26 16:31:20 +05:30
|
|
|
int errors = 0;
|
|
|
|
xbps_pkgdb_foreach_cb_multi(xhp, pkgdb_cb, &errors);
|
|
|
|
return errors ? -1 : 0;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
check_pkg_integrity(struct xbps_handle *xhp,
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t pkgd,
|
2012-11-17 22:13:54 +05:30
|
|
|
const char *pkgname)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2019-03-13 19:21:37 +05:30
|
|
|
xbps_dictionary_t opkgd, filesd;
|
2012-11-16 21:25:35 +05:30
|
|
|
const char *sha256;
|
|
|
|
char *buf;
|
2015-02-26 16:31:20 +05:30
|
|
|
int rv = 0, errors = 0;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2014-09-11 03:42:12 +05:30
|
|
|
filesd = opkgd = NULL;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
|
|
|
/* find real pkg by name */
|
2012-11-16 21:25:35 +05:30
|
|
|
opkgd = pkgd;
|
2012-11-30 22:10:52 +05:30
|
|
|
if (opkgd == NULL) {
|
|
|
|
if (((opkgd = xbps_pkgdb_get_pkg(xhp, pkgname)) == NULL) &&
|
|
|
|
((opkgd = xbps_pkgdb_get_virtualpkg(xhp, pkgname)) == NULL)) {
|
2012-11-02 19:34:25 +05:30
|
|
|
printf("Package %s is not installed.\n", pkgname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
2014-09-11 11:30:37 +05:30
|
|
|
* Check pkg files metadata signature.
|
2012-11-02 19:34:25 +05:30
|
|
|
*/
|
2014-09-11 11:30:37 +05:30
|
|
|
if (xbps_dictionary_get_cstring_nocopy(opkgd, "metafile-sha256", &sha256)) {
|
2014-09-11 03:42:12 +05:30
|
|
|
buf = xbps_xasprintf("%s/.%s-files.plist",
|
2012-11-16 21:25:35 +05:30
|
|
|
xhp->metadir, pkgname);
|
2015-05-28 13:11:00 +05:30
|
|
|
assert(buf);
|
2015-05-28 13:45:05 +05:30
|
|
|
filesd = xbps_plist_dictionary_from_file(xhp, buf);
|
2015-05-28 13:14:01 +05:30
|
|
|
if (filesd == NULL) {
|
|
|
|
fprintf(stderr, "%s: cannot read %s, ignoring...\n",
|
|
|
|
pkgname, buf);
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-16 21:25:35 +05:30
|
|
|
rv = xbps_file_hash_check(buf, sha256);
|
|
|
|
free(buf);
|
2014-09-11 11:30:37 +05:30
|
|
|
if (rv == ENOENT) {
|
|
|
|
xbps_dictionary_remove(opkgd, "metafile-sha256");
|
|
|
|
fprintf(stderr, "%s: unexistent metafile, "
|
|
|
|
"updating pkgdb.\n", pkgname);
|
|
|
|
} else if (rv == ERANGE) {
|
2014-09-11 03:42:12 +05:30
|
|
|
xbps_object_release(filesd);
|
2012-11-16 21:25:35 +05:30
|
|
|
fprintf(stderr, "%s: metadata file has been "
|
|
|
|
"modified!\n", pkgname);
|
2012-11-17 22:13:54 +05:30
|
|
|
return 1;
|
2012-11-16 21:25:35 +05:30
|
|
|
}
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
|
2015-02-26 16:31:20 +05:30
|
|
|
#define RUN_PKG_CHECK(x, name, arg) \
|
|
|
|
do { \
|
|
|
|
if ((rv = check_pkg_##name(x, pkgname, arg)) != 0) { \
|
|
|
|
errors++; \
|
|
|
|
} \
|
2012-11-02 19:34:25 +05:30
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* Execute pkg checks */
|
2014-09-11 03:42:12 +05:30
|
|
|
RUN_PKG_CHECK(xhp, files, filesd);
|
|
|
|
RUN_PKG_CHECK(xhp, symlinks, filesd);
|
|
|
|
RUN_PKG_CHECK(xhp, rundeps, opkgd);
|
2012-11-17 22:13:54 +05:30
|
|
|
RUN_PKG_CHECK(xhp, unneeded, opkgd);
|
2019-03-13 19:21:37 +05:30
|
|
|
RUN_PKG_CHECK(xhp, alternatives, opkgd);
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2014-09-11 11:30:37 +05:30
|
|
|
if (filesd)
|
|
|
|
xbps_object_release(filesd);
|
2012-12-14 10:34:58 +05:30
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
#undef RUN_PKG_CHECK
|
|
|
|
|
2015-02-26 16:31:20 +05:30
|
|
|
return errors ? EXIT_FAILURE : EXIT_SUCCESS;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|