xbps-pkgdb: fixed symlinks check; this now detects modified symlinks correctly.

This commit is contained in:
Juan RP
2012-11-19 21:12:04 +01:00
parent ca26c20dd5
commit 78cd625c28
2 changed files with 98 additions and 59 deletions

@@ -145,9 +145,7 @@ check_pkg_integrity(struct xbps_handle *xhp,
#define RUN_PKG_CHECK(x, name, arg) \ #define RUN_PKG_CHECK(x, name, arg) \
do { \ do { \
rv = check_pkg_##name(x, pkgname, arg); \ rv = check_pkg_##name(x, pkgname, arg); \
if (rv) \ if (rv == -1) { \
return rv; \
else if (rv == -1) { \
xbps_error_printf("%s: the %s test " \ xbps_error_printf("%s: the %s test " \
"returned error!\n", pkgname, #name); \ "returned error!\n", pkgname, #name); \
return rv; \ return rv; \
@@ -163,7 +161,7 @@ do { \
#undef RUN_PKG_CHECK #undef RUN_PKG_CHECK
if (pkgd == NULL) if ((rv == 0) && (pkgd == NULL))
(void)xbps_pkgdb_update(xhp, true); (void)xbps_pkgdb_update(xhp, true);
return 0; return 0;

@@ -45,70 +45,111 @@
* *
* returns 0 if test ran successfully, 1 otherwise and -1 on error. * returns 0 if test ran successfully, 1 otherwise and -1 on error.
*/ */
static char *
symlink_target(const char *pkgname, const char *path)
{
struct stat sb;
char *lnk;
ssize_t r;
if (lstat(path, &sb) == -1) {
xbps_error_printf("%s: lstat failed for %s\n", pkgname, path);
return NULL;
}
lnk = malloc(sb.st_size + 1);
assert(lnk);
r = readlink(path, lnk, sb.st_size + 1);
if (r < 0 || r > sb.st_size) {
xbps_error_printf("%s: readlink failed for %s\n", pkgname, path);
return NULL;
}
lnk[sb.st_size] = '\0';
return lnk;
}
int int
check_pkg_symlinks(struct xbps_handle *xhp, const char *pkgname, void *arg) check_pkg_symlinks(struct xbps_handle *xhp, const char *pkgname, void *arg)
{ {
prop_array_t array; prop_array_t array;
prop_object_t obj; prop_object_t obj;
prop_object_iterator_t iter; prop_dictionary_t filesd = arg;
prop_dictionary_t pkg_filesd = arg; size_t i;
const char *file, *tgt = NULL; const char *file, *tgt = NULL;
char *path, *buf, *buf2, *buf3, *dname, *path_target; char *path, *p, *buf, *buf2, *lnk, *dname, *tgt_path;
bool broken = false, test_broken = false; int rv;
bool broken = false;
array = prop_dictionary_get(pkg_filesd, "links"); array = prop_dictionary_get(filesd, "links");
if ((prop_object_type(array) == PROP_TYPE_ARRAY) && if (array == NULL)
prop_array_count(array) > 0) { return false;
iter = xbps_array_iter_from_dict(pkg_filesd, "links");
if (iter == NULL)
return -1;
while ((obj = prop_object_iterator_next(iter))) { for (i = 0; i < prop_array_count(array); i++) {
if (!prop_dictionary_get_cstring_nocopy(obj, "target", &tgt)) obj = prop_array_get(array, i);
if (!prop_dictionary_get_cstring_nocopy(obj, "target", &tgt)) {
xbps_warn_printf("%s: `%s' symlink with "
"empty target object!\n", pkgname, file);
continue; continue;
}
prop_dictionary_get_cstring_nocopy(obj, "file", &file); prop_dictionary_get_cstring_nocopy(obj, "file", &file);
if (strcmp(tgt, "") == 0) { if (strcmp(tgt, "") == 0) {
xbps_warn_printf("%s: `%s' symlink with " xbps_warn_printf("%s: `%s' symlink with "
"empty target object!\n", pkgname, file); "empty target object!\n", pkgname, file);
continue; continue;
} }
path = xbps_xasprintf("%s/%s", xhp->rootdir, file);
if ((buf = realpath(path, NULL)) == NULL) { if (strcmp(xhp->rootdir, "/")) {
xbps_error_printf("%s: broken symlink `%s': " tgt_path = xbps_xasprintf("%s%s", xhp->rootdir, tgt);
"%s\n", pkgname, file, strerror(errno)); path = xbps_xasprintf("%s%s", xhp->rootdir, file);
test_broken = true; } else {
path = strdup(file);
tgt_path = strdup(tgt);
}
lnk = symlink_target(pkgname, path);
if (lnk == NULL) {
free(path);
continue; continue;
} }
if (strncmp(tgt, "../", 3) == 0) { p = strdup(path);
/* relative symlink target */ assert(p);
dname = dirname(path); dname = dirname(p);
buf2 = xbps_xasprintf("%s/%s", dname, tgt); buf = xbps_xasprintf("%s/%s", dname, lnk);
buf3 = realpath(buf2, NULL);
assert(buf3); buf2 = realpath(path, NULL);
free(buf2); if (buf2 == NULL) {
path_target = buf3; xbps_warn_printf("%s: broken symlink %s (target: %s)\n",
pkgname, file, tgt);
free(buf);
free(lnk);
continue;
}
rv = 1;
if (lnk[0] != '/') {
/* relative symlink */
if ((strcmp(lnk, tgt) == 0) ||
(strcmp(buf, tgt_path) == 0) ||
(strcmp(buf2, tgt_path) == 0))
rv = 0;
} else { } else {
path_target = buf; /* absolute symlink */
if (strcmp(lnk, tgt) == 0)
rv = 0;
} }
if (strcmp(buf, path_target)) {
xbps_error_printf("%s: modified symlink `%s' " if (rv) {
"points to: `%s' (shall be: `%s')\n", xbps_error_printf("%s: modified symlink %s "
pkgname, file, buf, path_target); "points to %s (shall be %s)\n",
test_broken = true; pkgname, file, lnk, tgt);
broken = true;
} }
free(p);
free(buf); free(buf);
free(path); free(path);
if (buf3) free(tgt_path);
free(buf3);
path = buf = buf2 = buf3 = NULL;
}
prop_object_iterator_release(iter);
}
if (test_broken) {
xbps_error_printf("%s: symlinks check FAILED.\n", pkgname);
broken = true;
} }
return broken; return broken;
} }