Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/lib/libalpm/be_package.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libalpm/be_package.c')
-rw-r--r--lib/libalpm/be_package.c96
1 files changed, 54 insertions, 42 deletions
diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c
index 6dfddd61..20445756 100644
--- a/lib/libalpm/be_package.c
+++ b/lib/libalpm/be_package.c
@@ -20,10 +20,8 @@
#include "config.h"
-#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <limits.h>
#include <errno.h>
/* libarchive */
@@ -47,7 +45,7 @@ static void *_package_changelog_open(pmpkg_t *pkg)
{
ALPM_LOG_FUNC;
- ASSERT(pkg != NULL, return(NULL));
+ ASSERT(pkg != NULL, return NULL);
struct archive *archive = NULL;
struct archive_entry *entry;
@@ -60,7 +58,7 @@ static void *_package_changelog_open(pmpkg_t *pkg)
archive_read_support_compression_all(archive);
archive_read_support_format_all(archive);
- if (archive_read_open_filename(archive, pkgfile,
+ if(archive_read_open_filename(archive, pkgfile,
ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
RET_ERR(PM_ERR_PKG_OPEN, NULL);
}
@@ -69,14 +67,14 @@ static void *_package_changelog_open(pmpkg_t *pkg)
const char *entry_name = archive_entry_pathname(entry);
if(strcmp(entry_name, ".CHANGELOG") == 0) {
- return(archive);
+ return archive;
}
}
/* we didn't find a changelog */
archive_read_finish(archive);
errno = ENOENT;
- return(NULL);
+ return NULL;
}
/**
@@ -91,24 +89,16 @@ static void *_package_changelog_open(pmpkg_t *pkg)
static size_t _package_changelog_read(void *ptr, size_t size,
const pmpkg_t *pkg, const void *fp)
{
- ssize_t sret = archive_read_data((struct archive*)fp, ptr, size);
+ ssize_t sret = archive_read_data((struct archive *)fp, ptr, size);
/* Report error (negative values) */
if(sret < 0) {
pm_errno = PM_ERR_LIBARCHIVE;
- return(0);
+ return 0;
} else {
- return((size_t)sret);
+ return (size_t)sret;
}
}
-/*
-static int _package_changelog_feof(const pmpkg_t *pkg, void *fp)
-{
- // note: this doesn't quite work, no feof in libarchive
- return( archive_read_data((struct archive*)fp, NULL, 0) );
-}
-*/
-
/**
* Close a package changelog for reading. Similar to fclose in functionality,
* except that the 'file stream' is from an archive.
@@ -118,7 +108,7 @@ static int _package_changelog_feof(const pmpkg_t *pkg, void *fp)
*/
static int _package_changelog_close(const pmpkg_t *pkg, void *fp)
{
- return( archive_read_finish((struct archive *)fp) );
+ return archive_read_finish((struct archive *)fp);
}
/** Package file operations struct accessor. We implement this as a method
@@ -137,7 +127,7 @@ static struct pkg_operations *get_file_pkg_ops(void)
file_pkg_ops.changelog_close = _package_changelog_close;
file_pkg_ops_initialized = 1;
}
- return(&file_pkg_ops);
+ return &file_pkg_ops;
}
/**
@@ -226,7 +216,7 @@ static int parse_descfile(struct archive *a, pmpkg_t *newpkg)
line[0] = '\0';
}
- return(0);
+ return 0;
}
/**
@@ -236,9 +226,10 @@ static int parse_descfile(struct archive *a, pmpkg_t *newpkg)
* through the full archive
* @return An information filled pmpkg_t struct
*/
-static pmpkg_t *pkg_load(const char *pkgfile, int full)
+pmpkg_t *_alpm_pkg_load_internal(const char *pkgfile, int full,
+ const char *md5sum, const char *base64_sig, pgp_verify_t check_sig)
{
- int ret = ARCHIVE_OK;
+ int ret;
int config = 0;
struct archive *archive;
struct archive_entry *entry;
@@ -251,31 +242,54 @@ static pmpkg_t *pkg_load(const char *pkgfile, int full)
RET_ERR(PM_ERR_WRONG_ARGS, NULL);
}
- if(stat(pkgfile, &st) != 0) {
+ /* attempt to stat the package file, ensure it exists */
+ if(stat(pkgfile, &st) == 0) {
+ newpkg = _alpm_pkg_new();
+ if(newpkg == NULL) {
+ RET_ERR(PM_ERR_MEMORY, NULL);
+ }
+ newpkg->filename = strdup(pkgfile);
+ newpkg->size = st.st_size;
+ } else {
+ /* couldn't stat the pkgfile, return an error */
RET_ERR(PM_ERR_PKG_OPEN, NULL);
}
+ /* first steps- validate the package file */
+ _alpm_log(PM_LOG_DEBUG, "md5sum: %s\n", md5sum);
+ if(md5sum) {
+ _alpm_log(PM_LOG_DEBUG, "checking md5sum for %s\n", pkgfile);
+ if(_alpm_test_md5sum(pkgfile, md5sum) != 0) {
+ alpm_pkg_free(newpkg);
+ RET_ERR(PM_ERR_PKG_INVALID, NULL);
+ }
+ }
+
+ _alpm_log(PM_LOG_DEBUG, "base64_sig: %s\n", base64_sig);
+ if(check_sig != PM_PGP_VERIFY_NEVER) {
+ _alpm_log(PM_LOG_DEBUG, "checking signature for %s\n", pkgfile);
+ ret = _alpm_gpgme_checksig(pkgfile, base64_sig);
+ if((check_sig == PM_PGP_VERIFY_ALWAYS && ret != 0) ||
+ (check_sig == PM_PGP_VERIFY_OPTIONAL && ret == 1)) {
+ RET_ERR(PM_ERR_SIG_INVALID, NULL);
+ }
+ }
+
+ /* next- try to create an archive object to read in the package */
if((archive = archive_read_new()) == NULL) {
+ alpm_pkg_free(newpkg);
RET_ERR(PM_ERR_LIBARCHIVE, NULL);
}
archive_read_support_compression_all(archive);
archive_read_support_format_all(archive);
- if (archive_read_open_filename(archive, pkgfile,
+ if(archive_read_open_filename(archive, pkgfile,
ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
+ alpm_pkg_free(newpkg);
RET_ERR(PM_ERR_PKG_OPEN, NULL);
}
- newpkg = _alpm_pkg_new();
- if(newpkg == NULL) {
- archive_read_finish(archive);
- RET_ERR(PM_ERR_MEMORY, NULL);
- }
-
- newpkg->filename = strdup(pkgfile);
- newpkg->size = st.st_size;
-
_alpm_log(PM_LOG_DEBUG, "starting package load for %s\n", pkgfile);
/* If full is false, only read through the archive until we find our needed
@@ -340,7 +354,6 @@ static pmpkg_t *pkg_load(const char *pkgfile, int full)
/* internal fields for package struct */
newpkg->origin = PKG_FROM_FILE;
- /* TODO eventually kill/move this? */
newpkg->origin_data.file = strdup(pkgfile);
newpkg->ops = get_file_pkg_ops();
@@ -356,7 +369,7 @@ static pmpkg_t *pkg_load(const char *pkgfile, int full)
newpkg->infolevel = INFRQ_BASE | INFRQ_DESC;
}
- return(newpkg);
+ return newpkg;
pkg_invalid:
pm_errno = PM_ERR_PKG_INVALID;
@@ -364,25 +377,24 @@ error:
_alpm_pkg_free(newpkg);
archive_read_finish(archive);
- return(NULL);
+ return NULL;
}
-int SYMEXPORT alpm_pkg_load(const char *filename, int full, pmpkg_t **pkg)
+int SYMEXPORT alpm_pkg_load(const char *filename, int full,
+ pgp_verify_t check_sig, pmpkg_t **pkg)
{
ALPM_LOG_FUNC;
/* Sanity checks */
- ASSERT(filename != NULL && strlen(filename) != 0,
- RET_ERR(PM_ERR_WRONG_ARGS, -1));
ASSERT(pkg != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
- *pkg = pkg_load(filename, full);
+ *pkg = _alpm_pkg_load_internal(filename, full, NULL, NULL, check_sig);
if(*pkg == NULL) {
/* pm_errno is set by pkg_load */
- return(-1);
+ return -1;
}
- return(0);
+ return 0;
}
/* vim: set ts=2 sw=2 noet: */