Radish alpha
H
HardenedBSD Package Manager
Radicle
Git (anonymous pull)
Log in to clone via SSH
Prepare for multiple checksum formats supported for files
Baptiste Daroussin committed 11 years ago
commit a4038bb9fed2069e19c86c8fbeb68effd4e336da
parent fc983e36a4e7fc990deb910205fab1b6e3a1a73b
4 files changed +61 -16
modified libpkg/pkg.c
@@ -1617,31 +1617,16 @@ int
pkg_test_filesum(struct pkg *pkg)
{
	struct pkg_file *f = NULL;
-
	struct stat	 st;
-
	char *sha256;
	int rc = EPKG_OK;

	assert(pkg != NULL);

	while (pkg_files(pkg, &f) == EPKG_OK) {
		if (f->sum[0] != '\0') {
-
			if (lstat(f->path, &st) == -1) {
-
				pkg_emit_errno("pkg_create_from_dir", "lstat failed");
-
				return (EPKG_FATAL);
-
			}
-
			if (S_ISLNK(st.st_mode))
-
				sha256 = pkg_checksum_symlink(f->path, NULL,
-
				    PKG_HASH_TYPE_SHA256_HEX);
-
			else
-
				sha256 = pkg_checksum_file(f->path,
-
				    PKG_HASH_TYPE_SHA256_HEX);
-
			if (sha256 == NULL)
-
				return (EPKG_FATAL);
-
			if (strcmp(sha256, f->sum) != 0) {
+
			if (!pkg_checksum_validate_file(f->path, f->sum)) {
				pkg_emit_file_mismatch(pkg, f, f->sum);
				rc = EPKG_FATAL;
			}
-
			free(sha256);
		}
	}

modified libpkg/pkg_checksum.c
@@ -24,6 +24,8 @@

#include <assert.h>

+
#include <sys/stat.h>
+

#include <fcntl.h>
#include "pkg.h"
#include "private/pkg.h"
@@ -301,7 +303,23 @@ pkg_checksum_is_valid(const char *cksum, size_t clen)
	return (true);
}

+
/* <hashtype>$<hash> */
+
pkg_checksum_type_t
+
pkg_checksum_file_get_type(const char *cksum, size_t clen)
+
{
+
	unsigned int value;
+

+
	if (strchr(cksum, PKG_CKSUM_SEPARATOR) == NULL)
+
		return (PKG_HASH_TYPE_UNKNOWN);
+

+
	value = strtoul(cksum, NULL, 10);
+
	if (value < PKG_HASH_TYPE_UNKNOWN)
+
		return (value);
+

+
	return (PKG_HASH_TYPE_UNKNOWN);
+
}

+
/* <version>$<hashtype>$<hash> */
pkg_checksum_type_t
pkg_checksum_get_type(const char *cksum, size_t clen)
{
@@ -704,3 +722,41 @@ pkg_checksum_symlinkat(int fd, const char *path, const char *root, pkg_checksum_

	return (pkg_checksum_symlink_readlink(linkbuf, linklen, root, type));
}
+

+
bool
+
pkg_checksum_validate_file(const char *path, const char *sum)
+
{
+
	struct stat st;
+
	char *newsum;
+
	pkg_checksum_type_t type;
+

+
	type = pkg_checksum_file_get_type(sum, strlen(sum));
+
	if (type == PKG_HASH_TYPE_UNKNOWN) {
+
		type = PKG_HASH_TYPE_SHA256_HEX;
+
	} else {
+
		sum = strchr(sum, PKG_CKSUM_SEPARATOR);
+
		sum++;
+
	}
+

+
	if (lstat(path, &st) == -1) {
+
		pkg_emit_errno("pkg_create_from_dir", "lstat");
+
		return (false);
+
	}
+

+
	if (S_ISLNK(st.st_mode))
+
		newsum = pkg_checksum_symlink(path, NULL, type);
+
	else
+
		newsum = pkg_checksum_file(path, type);
+

+
	if (newsum == NULL)
+
		return (false);
+

+
	if (strcmp(sum, newsum) != 0) {
+
		free(newsum);
+
		return (false);
+
	}
+

+
	free(newsum);
+

+
	return (true);
+
}
modified libpkg/private/pkg.h
@@ -638,9 +638,11 @@ unsigned char *pkg_checksum_symlink(const char *path, const char *root,
    pkg_checksum_type_t type);
unsigned char *pkg_checksum_symlinkat(int fd, const char *path,
    const char *root, pkg_checksum_type_t type);
+
bool pkg_checksum_validate_file(const char *path, const  char *sum);

bool pkg_checksum_is_valid(const char *cksum, size_t clen);
pkg_checksum_type_t pkg_checksum_get_type(const char *cksum, size_t clen);
+
pkg_checksum_type_t pkg_checksum_file_get_type(const char *cksum, size_t clen);
pkg_checksum_type_t pkg_checksum_type_from_string(const char *name);
const char* pkg_checksum_type_to_string(pkg_checksum_type_t type);
size_t pkg_checksum_type_size(pkg_checksum_type_t type);
modified tests/lib/checksum.c
@@ -46,6 +46,8 @@ ATF_TC_BODY(check_symlinks, tc)
	sum = pkg_checksum_symlink("bar", NULL, PKG_HASH_TYPE_SHA256_HEX);
	ATF_REQUIRE_STREQ(sum, "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae");

+
	ATF_CHECK(pkg_checksum_validate_file("bar", "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"));
+

	free(sum);
}