Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ pkg_install_SOURCES= external/automatic.c external/dewey.c
pkg_install_SOURCES+= external/fexec.c external/iterate.c external/lpkg.c
pkg_install_SOURCES+= external/opattern.c external/pkgdb.c external/plist.c
pkg_install_SOURCES+= external/var.c external/xwrapper.c
pkg_install_SOURCES+= external/sha2.c

pkgin_SOURCES= actions.c autoremove.c depends.c download.c fsops.c impact.c
pkgin_SOURCES+= main.c order.c pkg_check.c pkg_infos.c pkg_install.c
pkgin_SOURCES+= pkg_str.c pkgindb.c pkgindb_queries.c pkglist.c preferred.c
pkgin_SOURCES+= pkg_str.c pkghash.c pkgindb.c pkgindb_queries.c pkglist.c preferred.c
pkgin_SOURCES+= selection.c sqlite_callbacks.c summary.c tools.c
pkgin_SOURCES+= $(openssh_SOURCES) $(pkg_install_SOURCES)

Expand Down
18 changes: 15 additions & 3 deletions actions.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <sqlite3.h>
#include "pkgin.h"
#include "pkghash.h"
#include <sys/ioctl.h>
#include <time.h>
#include <unistd.h>
Expand Down Expand Up @@ -99,6 +100,13 @@ pkg_download(Plisthead *installhead)
continue;
}

if (p->ipkg->rpkg->hash) {
if (!pkghash_verify_file(p->ipkg->rpkg->hash,
pkgurl))
errx(EXIT_FAILURE, "Hash mismatch %s",
p->ipkg->pkgfs);
}

if (symlink(pkgurl, p->ipkg->pkgfs) < 0) {
errx(EXIT_FAILURE,
"Failed to create symlink %s",
Expand All @@ -116,7 +124,8 @@ pkg_download(Plisthead *installhead)
err(EXIT_FAILURE, MSG_ERR_OPEN, p->ipkg->pkgfs);

p->ipkg->file_size =
download_pkg(p->ipkg->pkgurl, fp, i++, count);
download_pkg(p->ipkg->pkgurl, fp, i++, count,
p->ipkg->rpkg->hash);

if (p->ipkg->file_size == -1) {
(void) fclose(fp);
Expand Down Expand Up @@ -748,12 +757,15 @@ pkgin_install(char **pkgargs, int do_inst, int upgrade)

/*
* If the binary package has not already been downloaded, or
* its size does not match pkg_summary, then mark it to be
* its size/hash do not match pkg_summary, then mark it to be
* downloaded.
*/
if (stat(p->pkgfs, &st) < 0 || st.st_size != p->rpkg->file_size)
p->download = 1;
else {
else if (p->rpkg->hash != NULL &&
pkghash_verify_file(p->rpkg->hash, p->pkgfs)) {
/* exact match, no further check needed */
} else {
/*
* If the cached package has the correct size, we must
* verify that the BUILD_DATE has not changed, in case
Expand Down
32 changes: 28 additions & 4 deletions download.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/

#include "pkgin.h"
#include "pkghash.h"
#include "external/progressmeter.h"

extern char fetchflags[3];
Expand Down Expand Up @@ -159,7 +160,8 @@ sum_close(struct archive *a, void *data)
* Download a package to the local cache.
*/
off_t
download_pkg(char *pkg_url, FILE *fp, int cur, int total)
download_pkg(char *pkg_url, FILE *fp, int cur, int total,
const struct pkghash *hash)
{
struct url_stat st;
size_t size, wrote;
Expand All @@ -168,6 +170,7 @@ download_pkg(char *pkg_url, FILE *fp, int cur, int total)
struct url *url;
fetchIO *f = NULL;
char buf[4096];
struct pkghash_ctx *hash_ctx = NULL;
char *pkg, *ptr, *msg = NULL;

if ((url = fetchParseURL(pkg_url)) == NULL)
Expand All @@ -177,7 +180,8 @@ download_pkg(char *pkg_url, FILE *fp, int cur, int total)
fprintf(stderr, "download error: %s %s\n", pkg_url,
fetchLastErrString);
fetchFreeURL(url);
return -1;
written = -1;
goto out;
}
fetchFreeURL(url);

Expand All @@ -194,6 +198,9 @@ download_pkg(char *pkg_url, FILE *fp, int cur, int total)
start_progress_meter(msg, st.size, &statsize);
}

if (hash)
hash_ctx = pkghash_verify_init(hash);

while (written < st.size) {
if ((fetched = fetchIO_read(f, buf, sizeof(buf))) == 0)
break;
Expand All @@ -202,12 +209,17 @@ download_pkg(char *pkg_url, FILE *fp, int cur, int total)
if (fetched < 0) {
fprintf(stderr, "download error: %s",
fetchLastErrString);
return -1;
written = -1;
goto out;
}

statsize += fetched;
size = (size_t)fetched;

if (hash) {
pkghash_verify_update(hash_ctx, (const void *)buf,
size);
}
for (ptr = buf; size > 0; ptr += wrote, size -= wrote) {
if ((wrote = fwrite(ptr, 1, size, fp)) < size) {
if (ferror(fp) && errno == EINTR)
Expand All @@ -230,8 +242,20 @@ download_pkg(char *pkg_url, FILE *fp, int cur, int total)

if (written != st.size) {
fprintf(stderr, "download error: %s truncated\n", pkg_url);
return -1;
written = -1;
goto out;
}

if (hash) {
if (!pkghash_verify_final(hash_ctx)) {
fprintf(stderr, "download error: %s corrupted\n",
pkg_url);
written = -1;
goto out;
}
}

out: if (hash)
free(hash_ctx);
return written;
}
Loading