various: more informative error messages

cute-signatures
Timo Teras 2009-07-22 14:56:27 +03:00
parent 0a7991f70d
commit 93f0b3524c
11 changed files with 92 additions and 62 deletions

View File

@ -120,10 +120,10 @@ static int add_main(void *ctx, int argc, char **argv)
struct apk_sign_ctx sctx; struct apk_sign_ctx sctx;
apk_sign_ctx_init(&sctx, APK_SIGN_VERIFY, NULL); apk_sign_ctx_init(&sctx, APK_SIGN_VERIFY, NULL);
pkg = apk_pkg_read(&db, argv[i], &sctx); r = apk_pkg_read(&db, argv[i], &sctx, &pkg);
apk_sign_ctx_free(&sctx); apk_sign_ctx_free(&sctx);
if (pkg == NULL) { if (r != 0) {
apk_error("Unable to read '%s'", argv[i]); apk_error("%s: %s", argv[i], apk_error_str(r));
goto err; goto err;
} }

View File

@ -50,6 +50,24 @@ static struct apk_option generic_options[] = {
required_argument, "TIME" }, required_argument, "TIME" },
}; };
const char *apk_error_str(int error)
{
if (error < 0)
error = -error;
switch (error) {
case ENOKEY:
return "UNTRUSTED signature";
case EKEYREJECTED:
return "BAD signature";
case EIO:
return "IO ERROR";
case EBADMSG:
return "BAD archive";
default:
return strerror(error);
}
}
void apk_log(const char *prefix, const char *format, ...) void apk_log(const char *prefix, const char *format, ...)
{ {
va_list va; va_list va;

View File

@ -65,6 +65,7 @@ extern unsigned int apk_flags;
#define apk_message(args...) do { if (apk_verbosity > 0) { apk_log(NULL, args); } } while (0) #define apk_message(args...) do { if (apk_verbosity > 0) { apk_log(NULL, args); } } while (0)
void apk_log(const char *prefix, const char *format, ...); void apk_log(const char *prefix, const char *format, ...);
const char *apk_error_str(int error);
static inline size_t apk_calc_installed_size(size_t size) static inline size_t apk_calc_installed_size(size_t size)
{ {

View File

@ -31,7 +31,7 @@ struct apk_file_info {
}; };
struct apk_istream { struct apk_istream {
size_t (*read)(void *stream, void *ptr, size_t size); ssize_t (*read)(void *stream, void *ptr, size_t size);
void (*close)(void *stream); void (*close)(void *stream);
}; };
@ -44,7 +44,7 @@ struct apk_bstream {
}; };
struct apk_ostream { struct apk_ostream {
size_t (*write)(void *stream, const void *buf, size_t size); ssize_t (*write)(void *stream, const void *buf, size_t size);
void (*close)(void *stream); void (*close)(void *stream);
}; };

View File

@ -121,8 +121,8 @@ int apk_deps_write(struct apk_dependency_array *deps, struct apk_ostream *os);
int apk_script_type(const char *name); int apk_script_type(const char *name);
struct apk_package *apk_pkg_new(void); struct apk_package *apk_pkg_new(void);
struct apk_package *apk_pkg_read(struct apk_database *db, const char *name, int apk_pkg_read(struct apk_database *db, const char *name,
struct apk_sign_ctx *ctx); struct apk_sign_ctx *ctx, struct apk_package **pkg);
void apk_pkg_free(struct apk_package *pkg); void apk_pkg_free(struct apk_package *pkg);
int apk_pkg_parse_name(apk_blob_t apkname, apk_blob_t *name, apk_blob_t *version); int apk_pkg_parse_name(apk_blob_t apkname, apk_blob_t *name, apk_blob_t *version);

View File

@ -81,27 +81,28 @@ struct apk_tar_entry_istream {
struct apk_checksum *csum; struct apk_checksum *csum;
}; };
static size_t tar_entry_read(void *stream, void *ptr, size_t size) static ssize_t tar_entry_read(void *stream, void *ptr, size_t size)
{ {
struct apk_tar_entry_istream *teis = struct apk_tar_entry_istream *teis =
container_of(stream, struct apk_tar_entry_istream, is); container_of(stream, struct apk_tar_entry_istream, is);
ssize_t r;
if (size > teis->bytes_left) if (size > teis->bytes_left)
size = teis->bytes_left; size = teis->bytes_left;
size = teis->tar_is->read(teis->tar_is, ptr, size); r = teis->tar_is->read(teis->tar_is, ptr, size);
if (size < 0) if (r < 0)
return -1; return r;
teis->bytes_left -= size; teis->bytes_left -= r;
if (teis->csum == NULL) if (teis->csum == NULL)
return size; return r;
EVP_DigestUpdate(&teis->mdctx, ptr, size); EVP_DigestUpdate(&teis->mdctx, ptr, r);
if (teis->bytes_left == 0) { if (teis->bytes_left == 0) {
teis->csum->type = EVP_MD_CTX_size(&teis->mdctx); teis->csum->type = EVP_MD_CTX_size(&teis->mdctx);
EVP_DigestFinal_ex(&teis->mdctx, teis->csum->data, NULL); EVP_DigestFinal_ex(&teis->mdctx, teis->csum->data, NULL);
} }
return size; return r;
} }
static void tar_entry_close(void *stream) static void tar_entry_close(void *stream)
@ -221,13 +222,13 @@ int apk_tar_parse(struct apk_istream *is, apk_archive_entry_parser parser,
if (r == 512) { if (r == 512) {
while ((r = is->read(is, &buf, 512)) == 512) { while ((r = is->read(is, &buf, 512)) == 512) {
if (buf.name[0] != 0) if (buf.name[0] != 0)
return -1; return -EBADMSG;
} }
} }
/* Check that there was no partial record */ /* Check that there was no partial record */
if (r > 0) if (r > 0)
r = -1; r = -EBADMSG;
return r; return r;

View File

@ -1582,8 +1582,9 @@ static int apk_db_unpack_pkg(struct apk_database *db,
tar->close(tar); tar->close(tar);
if (r != 0) { if (r != 0) {
apk_error("%s-%s: package integrity check failed", apk_error("%s-%s: %s",
newpkg->name->name, newpkg->version); newpkg->name->name, newpkg->version,
apk_error_str(r));
goto err; goto err;
} }
r = apk_db_run_pending_script(&ctx); r = apk_db_run_pending_script(&ctx);

View File

@ -8,6 +8,7 @@
* by the Free Software Foundation. See http://www.gnu.org/ for details. * by the Free Software Foundation. See http://www.gnu.org/ for details.
*/ */
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
@ -28,7 +29,7 @@ struct apk_gzip_istream {
void *cbprev; void *cbprev;
}; };
static size_t gzi_read(void *stream, void *ptr, size_t size) static ssize_t gzi_read(void *stream, void *ptr, size_t size)
{ {
struct apk_gzip_istream *gis = struct apk_gzip_istream *gis =
container_of(stream, struct apk_gzip_istream, is); container_of(stream, struct apk_gzip_istream, is);
@ -61,7 +62,7 @@ static size_t gzi_read(void *stream, void *ptr, size_t size)
gis->zs.avail_in = blob.len; gis->zs.avail_in = blob.len;
gis->zs.next_in = (void *) gis->cbprev; gis->zs.next_in = (void *) gis->cbprev;
if (gis->zs.avail_in < 0) { if (gis->zs.avail_in < 0) {
gis->err = -1; gis->err = -EIO;
goto ret; goto ret;
} else if (gis->zs.avail_in == 0) { } else if (gis->zs.avail_in == 0) {
gis->err = 1; gis->err = 1;
@ -69,7 +70,7 @@ static size_t gzi_read(void *stream, void *ptr, size_t size)
r = gis->cb(gis->cbctx, APK_MPART_END, r = gis->cb(gis->cbctx, APK_MPART_END,
APK_BLOB_NULL); APK_BLOB_NULL);
if (r > 0) if (r > 0)
r = -1; r = -ECANCELED;
if (r != 0) if (r != 0)
gis->err = r; gis->err = r;
} }
@ -85,9 +86,9 @@ static size_t gzi_read(void *stream, void *ptr, size_t size)
r = gis->cb(gis->cbctx, APK_MPART_BOUNDARY, r = gis->cb(gis->cbctx, APK_MPART_BOUNDARY,
APK_BLOB_PTR_LEN(gis->cbprev, APK_BLOB_PTR_LEN(gis->cbprev,
(void *)gis->zs.next_in - gis->cbprev)); (void *)gis->zs.next_in - gis->cbprev));
if (r > 0)
r = -ECANCELED;
if (r != 0) { if (r != 0) {
if (r > 0)
r = -1;
gis->err = r; gis->err = r;
goto ret; goto ret;
} }
@ -95,12 +96,12 @@ static size_t gzi_read(void *stream, void *ptr, size_t size)
} }
inflateEnd(&gis->zs); inflateEnd(&gis->zs);
if (inflateInit2(&gis->zs, 15+32) != Z_OK) if (inflateInit2(&gis->zs, 15+32) != Z_OK)
return -1; return -ENOMEM;
break; break;
case Z_OK: case Z_OK:
break; break;
default: default:
gis->err = -1; gis->err = -EIO;
break; break;
} }
} }
@ -159,12 +160,11 @@ struct apk_gzip_ostream {
z_stream zs; z_stream zs;
}; };
static size_t gzo_write(void *stream, const void *ptr, size_t size) static ssize_t gzo_write(void *stream, const void *ptr, size_t size)
{ {
struct apk_gzip_ostream *gos = (struct apk_gzip_ostream *) stream; struct apk_gzip_ostream *gos = (struct apk_gzip_ostream *) stream;
unsigned char buffer[1024]; unsigned char buffer[1024];
size_t have; ssize_t have, r;
int r;
gos->zs.avail_in = size; gos->zs.avail_in = size;
gos->zs.next_in = (void *) ptr; gos->zs.next_in = (void *) ptr;
@ -173,12 +173,12 @@ static size_t gzo_write(void *stream, const void *ptr, size_t size)
gos->zs.next_out = buffer; gos->zs.next_out = buffer;
r = deflate(&gos->zs, Z_NO_FLUSH); r = deflate(&gos->zs, Z_NO_FLUSH);
if (r == Z_STREAM_ERROR) if (r == Z_STREAM_ERROR)
return -1; return -EIO;
have = sizeof(buffer) - gos->zs.avail_out; have = sizeof(buffer) - gos->zs.avail_out;
if (have != 0) { if (have != 0) {
r = gos->output->write(gos->output, buffer, have); r = gos->output->write(gos->output, buffer, have);
if (r != have) if (r != have)
return -1; return -EIO;
} }
} }

View File

@ -155,7 +155,7 @@ static int index_main(void *ctx, int argc, char **argv)
if (!found) { if (!found) {
struct apk_sign_ctx sctx; struct apk_sign_ctx sctx;
apk_sign_ctx_init(&sctx, ictx->method, NULL); apk_sign_ctx_init(&sctx, ictx->method, NULL);
if (apk_pkg_read(&db, argv[i], &sctx) != NULL) if (apk_pkg_read(&db, argv[i], &sctx, NULL) == 0)
newpkgs++; newpkgs++;
apk_sign_ctx_free(&sctx); apk_sign_ctx_free(&sctx);
} }

View File

@ -9,6 +9,7 @@
* by the Free Software Foundation. See http://www.gnu.org/ for details. * by the Free Software Foundation. See http://www.gnu.org/ for details.
*/ */
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
@ -23,22 +24,22 @@ struct apk_fd_istream {
int fd; int fd;
}; };
static size_t fdi_read(void *stream, void *ptr, size_t size) static ssize_t fdi_read(void *stream, void *ptr, size_t size)
{ {
struct apk_fd_istream *fis = struct apk_fd_istream *fis =
container_of(stream, struct apk_fd_istream, is); container_of(stream, struct apk_fd_istream, is);
size_t i = 0, r; ssize_t i = 0, r;
if (ptr == NULL) { if (ptr == NULL) {
if (lseek(fis->fd, size, SEEK_CUR) < 0) if (lseek(fis->fd, size, SEEK_CUR) < 0)
return -1; return -errno;
return size; return size;
} }
while (i < size) { while (i < size) {
r = read(fis->fd, ptr + i, size - i); r = read(fis->fd, ptr + i, size - i);
if (r < 0) if (r < 0)
return r; return -errno;
if (r == 0) if (r == 0)
return i; return i;
i += r; i += r;
@ -121,7 +122,7 @@ size_t apk_istream_splice(void *stream, int fd, size_t size,
buf = malloc(bufsz); buf = malloc(bufsz);
if (buf == NULL) if (buf == NULL)
return -1; return -ENOMEM;
while (done < size) { while (done < size) {
if (done != 0 && cb != NULL) if (done != 0 && cb != NULL)
@ -134,7 +135,8 @@ size_t apk_istream_splice(void *stream, int fd, size_t size,
if (r < 0) if (r < 0)
goto err; goto err;
if (write(fd, buf, r) != r) { if (write(fd, buf, r) != r) {
r = -1; if (r < 0)
r = -errno;
goto err; goto err;
} }
done += r; done += r;
@ -447,7 +449,7 @@ int apk_file_get_info(const char *filename, int checksum, struct apk_file_info *
struct apk_bstream *bs; struct apk_bstream *bs;
if (stat(filename, &st) != 0) if (stat(filename, &st) != 0)
return -1; return -errno;
*fi = (struct apk_file_info) { *fi = (struct apk_file_info) {
.size = st.st_size, .size = st.st_size,
@ -492,14 +494,14 @@ struct apk_fd_ostream {
char buffer[1024]; char buffer[1024];
}; };
static size_t safe_write(int fd, const void *ptr, size_t size) static ssize_t safe_write(int fd, const void *ptr, size_t size)
{ {
size_t i = 0, r; ssize_t i = 0, r;
while (i < size) { while (i < size) {
r = write(fd, ptr + i, size - i); r = write(fd, ptr + i, size - i);
if (r < 0) if (r < 0)
return r; return -errno;
if (r == 0) if (r == 0)
return i; return i;
i += r; i += r;
@ -508,26 +510,30 @@ static size_t safe_write(int fd, const void *ptr, size_t size)
return i; return i;
} }
static int fdo_flush(struct apk_fd_ostream *fos) static ssize_t fdo_flush(struct apk_fd_ostream *fos)
{ {
ssize_t r;
if (fos->bytes == 0) if (fos->bytes == 0)
return 0; return 0;
if (safe_write(fos->fd, fos->buffer, fos->bytes) != fos->bytes) if ((r = safe_write(fos->fd, fos->buffer, fos->bytes)) != fos->bytes)
return -1; return r;
fos->bytes = 0; fos->bytes = 0;
return 0; return 0;
} }
static size_t fdo_write(void *stream, const void *ptr, size_t size) static ssize_t fdo_write(void *stream, const void *ptr, size_t size)
{ {
struct apk_fd_ostream *fos = struct apk_fd_ostream *fos =
container_of(stream, struct apk_fd_ostream, os); container_of(stream, struct apk_fd_ostream, os);
ssize_t r;
if (size + fos->bytes >= sizeof(fos->buffer)) { if (size + fos->bytes >= sizeof(fos->buffer)) {
if (fdo_flush(fos)) r = fdo_flush(fos);
return -1; if (r != 0)
return r;
if (size >= sizeof(fos->buffer) / 2) if (size >= sizeof(fos->buffer) / 2)
return safe_write(fos->fd, ptr, size); return safe_write(fos->fd, ptr, size);
} }
@ -586,7 +592,7 @@ struct apk_counter_ostream {
off_t *counter; off_t *counter;
}; };
static size_t co_write(void *stream, const void *ptr, size_t size) static ssize_t co_write(void *stream, const void *ptr, size_t size)
{ {
struct apk_counter_ostream *cos = struct apk_counter_ostream *cos =
container_of(stream, struct apk_counter_ostream, os); container_of(stream, struct apk_counter_ostream, os);

View File

@ -693,8 +693,8 @@ static int read_info_entry(void *ctx, const struct apk_file_info *ae,
return 0; return 0;
} }
struct apk_package *apk_pkg_read(struct apk_database *db, const char *file, int apk_pkg_read(struct apk_database *db, const char *file,
struct apk_sign_ctx *sctx) struct apk_sign_ctx *sctx, struct apk_package **pkg)
{ {
struct read_info_ctx ctx; struct read_info_ctx ctx;
struct apk_file_info fi; struct apk_file_info fi;
@ -704,16 +704,17 @@ struct apk_package *apk_pkg_read(struct apk_database *db, const char *file,
int r; int r;
if (realpath(file, realfile) < 0) if (realpath(file, realfile) < 0)
return NULL; return -errno;
if (apk_file_get_info(realfile, APK_CHECKSUM_NONE, &fi) < 0) r = apk_file_get_info(realfile, APK_CHECKSUM_NONE, &fi);
return NULL; if (r != 0)
return r;
memset(&ctx, 0, sizeof(ctx)); memset(&ctx, 0, sizeof(ctx));
ctx.sctx = sctx; ctx.sctx = sctx;
ctx.pkg = apk_pkg_new(); ctx.pkg = apk_pkg_new();
r = -ENOMEM;
if (ctx.pkg == NULL) if (ctx.pkg == NULL)
return NULL; goto err;
bs = apk_bstream_from_file(realfile); bs = apk_bstream_from_file(realfile);
if (bs == NULL) if (bs == NULL)
goto err; goto err;
@ -727,11 +728,10 @@ struct apk_package *apk_pkg_read(struct apk_database *db, const char *file,
tar->close(tar); tar->close(tar);
if (r < 0 && r != -ECANCELED) if (r < 0 && r != -ECANCELED)
goto err; goto err;
if (ctx.pkg->name == NULL) if (ctx.pkg->name == NULL) {
goto err; r = -EBADMSG;
if (sctx->action == APK_SIGN_VERIFY && !sctx->data_verified &&
!(apk_flags & APK_FORCE))
goto err; goto err;
}
if (sctx->action != APK_SIGN_VERIFY) if (sctx->action != APK_SIGN_VERIFY)
ctx.pkg->csum = sctx->identity; ctx.pkg->csum = sctx->identity;
@ -744,10 +744,13 @@ struct apk_package *apk_pkg_read(struct apk_database *db, const char *file,
} }
ctx.pkg->filename = strdup(realfile); ctx.pkg->filename = strdup(realfile);
return apk_db_pkg_add(db, ctx.pkg); ctx.pkg = apk_db_pkg_add(db, ctx.pkg);
if (pkg != NULL)
*pkg = ctx.pkg;
r = 0;
err: err:
apk_pkg_free(ctx.pkg); apk_pkg_free(ctx.pkg);
return NULL; return r;
} }
void apk_pkg_free(struct apk_package *pkg) void apk_pkg_free(struct apk_package *pkg)