chantra / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone
629b27
From a2c959085250d186c54130b78af076095c3d3cd3 Mon Sep 17 00:00:00 2001
629b27
From: chantra <chantr4@gmail.com>
629b27
Date: Tue, 15 Feb 2022 09:11:33 -0800
629b27
Subject: [PATCH 23/30] [rpm2extents] Improve logging
629b27
629b27
```
629b27
$ ls -l tests/data/RPMS/hello-2.0-1.x86_64.rpm
629b27
-rw-r--r--. 1 chantra chantra 9915 Jan 19 09:10 tests/data/RPMS/hello-2.0-1.x86_64.rpm
629b27
$ cp tests/data/RPMS/hello-2.0-1.x86_64.rpm ~/trunc-hello-2.0-1.x86_64.rpm
629b27
$ truncate -s 9000 ~/trunc-hello-2.0-1.x86_64.rpm
629b27
$ ls -l ~/trunc-hello-2.0-1.x86_64.rpm
629b27
-rw-r--r--. 1 chantra chantra 9000 Feb 15 09:13 /home/chantra/trunc-hello-2.0-1.x86_64.rpm
629b27
$ cat ~/trunc-hello-2.0-1.x86_64.rpm| ./rpm2extents SHA256 > /dev/null
629b27
error: rpmfiArchiveReadToFile failed while extracting "/usr/bin/hello" with RC -32784: cpio: read failed - Illegal seek
629b27
error: Package processor failed: -32784
629b27
error: Unable to write input length 9000: 32, Broken pipe
629b27
error: Failed to write digests: 32, Broken pipe
629b27
error: Validator failed with RC 2
629b27
```
629b27
Before:
629b27
```
629b27
$ cat ~/trunc-hello-2.0-1.x86_64.rpm| ./rpm2extents SHA256 > /dev/null
629b27
rpmfiArchiveReadToFile failed with -32784
629b27
Validator failed
629b27
Unable to write input length 9000
629b27
Failed to write digestsValidator failed
629b27
```
629b27
---
629b27
 rpm2extents.c | 120 ++++++++++++++++++++++++++++++--------------------
629b27
 1 file changed, 72 insertions(+), 48 deletions(-)
629b27
629b27
diff --git a/rpm2extents.c b/rpm2extents.c
629b27
index a326e3857..e1a19fedb 100644
629b27
--- a/rpm2extents.c
629b27
+++ b/rpm2extents.c
629b27
@@ -84,13 +84,15 @@ static int FDWriteDigests(
629b27
 
629b27
     len = sizeof(fdilength);
629b27
     if (Fwrite(&fdilength, len, 1, fdo) != len) {
629b27
-	fprintf(stderr, _("Unable to write input length %zd\n"), fdilength);
629b27
+	rpmlog(RPMLOG_ERR, _("Unable to write input length %zd: %d, %s\n"),
629b27
+	       fdilength, errno, strerror(errno));
629b27
 	goto exit;
629b27
     }
629b27
     len = sizeof(algos_len);
629b27
     if (Fwrite(&algos_len, len, 1, fdo) != len) {
629b27
 	algo_digest_len = (uint32_t)filedigest_len;
629b27
-	fprintf(stderr, _("Unable to write number of digests\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("Unable to write number of digests: %d, %s\n"),
629b27
+	       errno, strerror(errno));
629b27
 	goto exit;
629b27
     }
629b27
     for (algo = 0; algo < algos_len; algo++) {
629b27
@@ -102,24 +104,28 @@ static int FDWriteDigests(
629b27
 
629b27
 	len = sizeof(algo_name_len);
629b27
 	if (Fwrite(&algo_name_len, len, 1, fdo) != len) {
629b27
-	    fprintf(stderr,
629b27
-		    _("Unable to write digest algo name length\n"));
629b27
+	    rpmlog(RPMLOG_ERR,
629b27
+		   _("Unable to write digest algo name length: %d, %s\n"),
629b27
+		   errno, strerror(errno));
629b27
 	    goto exit;
629b27
 	}
629b27
 	len = sizeof(algo_digest_len);
629b27
 	if (Fwrite(&algo_digest_len, len, 1, fdo) != len) {
629b27
-	    fprintf(stderr,
629b27
-		    _("Unable to write number of bytes for digest\n"));
629b27
+	    rpmlog(RPMLOG_ERR,
629b27
+		   _("Unable to write number of bytes for digest: %d, %s\n"),
629b27
+		   errno, strerror(errno));
629b27
 	     goto exit;
629b27
 	}
629b27
 	if (Fwrite(algo_name, algo_name_len, 1, fdo) != algo_name_len) {
629b27
-	    fprintf(stderr, _("Unable to write digest algo name\n"));
629b27
+	    rpmlog(RPMLOG_ERR, _("Unable to write digest algo name: %d, %s\n"),
629b27
+		   errno, strerror(errno));
629b27
 	    goto exit;
629b27
 	}
629b27
 	if (Fwrite(filedigest, algo_digest_len, 1, fdo ) != algo_digest_len) {
629b27
-	    fprintf(stderr,
629b27
-		    _("Unable to write digest value %u, %zu\n"),
629b27
-		    algo_digest_len, filedigest_len);
629b27
+	    rpmlog(RPMLOG_ERR,
629b27
+		   _("Unable to write digest value %u, %zu: %d, %s\n"),
629b27
+		   algo_digest_len, filedigest_len,
629b27
+		   errno, strerror(errno));
629b27
 	    goto exit;
629b27
 	}
629b27
     }
629b27
@@ -133,22 +139,29 @@ static rpmRC FDWriteSignaturesValidation(FD_t fdo, int rpmvsrc, char *msg) {
629b27
     rpmRC rc = RPMRC_FAIL;
629b27
 
629b27
     if(rpmvsrc){
629b27
-	fprintf(stderr, _("Error verifying package signatures\n"));
629b27
+	rpmlog(RPMLOG_WARNING,
629b27
+	       _("Error verifying package signatures:\n%s\n"), msg);
629b27
     }
629b27
 
629b27
     len = sizeof(rpmvsrc);
629b27
     if (Fwrite(&rpmvsrc, len, 1, fdo) != len) {
629b27
-	fprintf(stderr, _("Unable to write signature verification RC code %d\n"), rpmvsrc);
629b27
+	rpmlog(RPMLOG_ERR,
629b27
+	       _("Unable to write signature verification RC code %d: %d, %s\n"),
629b27
+	       rpmvsrc, errno, strerror(errno));
629b27
 	goto exit;
629b27
     }
629b27
     size_t content_len = msg ? strlen(msg) : 0;
629b27
     len = sizeof(content_len);
629b27
     if (Fwrite(&content_len, len, 1, fdo) != len) {
629b27
-	fprintf(stderr, _("Unable to write signature verification output length %zd\n"), content_len);
629b27
+	rpmlog(RPMLOG_ERR,
629b27
+	       _("Unable to write signature verification output length %zd: %d, %s\n"),
629b27
+	       content_len, errno, strerror(errno));
629b27
 	goto exit;
629b27
     }
629b27
     if (Fwrite(msg, content_len, 1, fdo) != content_len) {
629b27
-	fprintf(stderr, _("Unable to write signature verification output %s\n"), msg);
629b27
+	rpmlog(RPMLOG_ERR,
629b27
+	       _("Unable to write signature verification output %s: %d, %s\n"),
629b27
+	       msg, errno, strerror(errno));
629b27
 	goto exit;
629b27
     }
629b27
 
629b27
@@ -174,12 +187,16 @@ static rpmRC validator(FD_t fdi, FD_t digesto, FD_t sigo,
629b27
 
629b27
     // Write result of digest computation
629b27
     if(FDWriteDigests(fdi, digesto, algos, algos_len) != RPMRC_OK) {
629b27
-	fprintf(stderr, _("Failed to write digests"));
629b27
+	rpmlog(RPMLOG_ERR, _("Failed to write digests: %d, %s\n"),
629b27
+	       errno, strerror(errno));
629b27
 	goto exit;
629b27
     }
629b27
 
629b27
     // Write result of signature validation.
629b27
     if(FDWriteSignaturesValidation(sigo, rpmvsrc, msg)) {
629b27
+	rpmlog(RPMLOG_ERR,
629b27
+	       _("Failed to write signature verification result: %d, %s\n"),
629b27
+	       errno, strerror(errno));
629b27
 	goto exit;
629b27
     }
629b27
     rc = RPMRC_OK;
629b27
@@ -226,24 +243,24 @@ static rpmRC process_package(FD_t fdi, FD_t digestori, FD_t validationi)
629b27
     fdo = fdDup(STDOUT_FILENO);
629b27
 
629b27
     if (rpmReadPackageRaw(fdi, &sigh, &h)) {
629b27
-	fprintf(stderr, _("Error reading package\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("Error reading package\n"));
629b27
 	exit(EXIT_FAILURE);
629b27
     }
629b27
 
629b27
     if (rpmLeadWrite(fdo, h))
629b27
     {
629b27
-	fprintf(stderr, _("Unable to write package lead: %s\n"),
629b27
+	rpmlog(RPMLOG_ERR, _("Unable to write package lead: %s\n"),
629b27
 		Fstrerror(fdo));
629b27
 	exit(EXIT_FAILURE);
629b27
     }
629b27
 
629b27
     if (rpmWriteSignature(fdo, sigh)) {
629b27
-	fprintf(stderr, _("Unable to write signature: %s\n"), Fstrerror(fdo));
629b27
+	rpmlog(RPMLOG_ERR, _("Unable to write signature: %s\n"), Fstrerror(fdo));
629b27
 	exit(EXIT_FAILURE);
629b27
     }
629b27
 
629b27
     if (headerWrite(fdo, h, HEADER_MAGIC_YES)) {
629b27
-	fprintf(stderr, _("Unable to write headers: %s\n"), Fstrerror(fdo));
629b27
+	rpmlog(RPMLOG_ERR, _("Unable to write headers: %s\n"), Fstrerror(fdo));
629b27
 	exit(EXIT_FAILURE);
629b27
     }
629b27
 
629b27
@@ -257,7 +274,7 @@ static rpmRC process_package(FD_t fdi, FD_t digestori, FD_t validationi)
629b27
     free(rpmio_flags);
629b27
 
629b27
     if (gzdi == NULL) {
629b27
-	fprintf(stderr, _("cannot re-open payload: %s\n"), Fstrerror(gzdi));
629b27
+	rpmlog(RPMLOG_ERR, _("cannot re-open payload: %s\n"), Fstrerror(gzdi));
629b27
 	exit(EXIT_FAILURE);
629b27
     }
629b27
 
629b27
@@ -300,7 +317,7 @@ static rpmRC process_package(FD_t fdi, FD_t digestori, FD_t validationi)
629b27
 	}
629b27
 	pad = pad_to(pos, fundamental_block_size);
629b27
 	if (Fwrite(zeros, sizeof(char), pad, fdo) != pad) {
629b27
-	    fprintf(stderr, _("Unable to write padding\n"));
629b27
+	    rpmlog(RPMLOG_ERR, _("Unable to write padding\n"));
629b27
 	    rc = RPMRC_FAIL;
629b27
 	    goto exit;
629b27
 	}
629b27
@@ -313,7 +330,12 @@ static rpmRC process_package(FD_t fdi, FD_t digestori, FD_t validationi)
629b27
 	size = rpmfiFSize(fi);
629b27
 	rc = rpmfiArchiveReadToFile(fi, fdo, 0);
629b27
 	if (rc != RPMRC_OK) {
629b27
-	    fprintf(stderr, _("rpmfiArchiveReadToFile failed with %d\n"), rc);
629b27
+	    char *errstr = rpmfileStrerror(rc);
629b27
+	    rpmlog(RPMLOG_ERR,
629b27
+		   _("rpmfiArchiveReadToFile failed while extracting "\
629b27
+		   "\"%s\" with RC %d: %s\n"),
629b27
+		   rpmfiFN(fi), rc, errstr);
629b27
+	    free(errstr);
629b27
 	    goto exit;
629b27
 	}
629b27
 	pos += size;
629b27
@@ -326,7 +348,7 @@ static rpmRC process_package(FD_t fdi, FD_t digestori, FD_t validationi)
629b27
     validation_pos = pos;
629b27
     ssize_t validation_len = ufdCopy(validationi, fdo);
629b27
     if (validation_len == -1) {
629b27
-	fprintf(stderr, _("validation output ufdCopy failed\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("validation output ufdCopy failed\n"));
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
@@ -335,25 +357,25 @@ static rpmRC process_package(FD_t fdi, FD_t digestori, FD_t validationi)
629b27
 
629b27
     len = sizeof(offset_ix);
629b27
     if (Fwrite(&offset_ix, len, 1, fdo) != len) {
629b27
-	fprintf(stderr, _("Unable to write length of table\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("Unable to write length of table\n"));
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
     len = sizeof(diglen);
629b27
     if (Fwrite(&diglen, len, 1, fdo) != len) {
629b27
-	fprintf(stderr, _("Unable to write length of digest\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("Unable to write length of digest\n"));
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
     len = sizeof(rpm_loff_t);
629b27
     for (int x = 0; x < offset_ix; x++) {
629b27
 	if (Fwrite(offsets[x].digest, diglen, 1, fdo) != diglen) {
629b27
-	    fprintf(stderr, _("Unable to write digest\n"));
629b27
+	    rpmlog(RPMLOG_ERR, _("Unable to write digest\n"));
629b27
 	    rc = RPMRC_FAIL;
629b27
 	    goto exit;
629b27
 	}
629b27
 	if (Fwrite(&offsets[x].pos, len, 1, fdo) != len) {
629b27
-	    fprintf(stderr, _("Unable to write offset\n"));
629b27
+	    rpmlog(RPMLOG_ERR, _("Unable to write offset\n"));
629b27
 	    rc = RPMRC_FAIL;
629b27
 	    goto exit;
629b27
 	}
629b27
@@ -365,7 +387,7 @@ static rpmRC process_package(FD_t fdi, FD_t digestori, FD_t validationi)
629b27
 
629b27
     ssize_t digest_len = ufdCopy(digestori, fdo);
629b27
     if (digest_len == -1) {
629b27
-	fprintf(stderr, _("digest table ufdCopy failed\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("digest table ufdCopy failed\n"));
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
@@ -378,30 +400,30 @@ static rpmRC process_package(FD_t fdi, FD_t digestori, FD_t validationi)
629b27
     pad = pad_to((validation_pos + validation_len + 2 * sizeof(rpm_loff_t) +
629b27
 		 sizeof(uint64_t)), fundamental_block_size);
629b27
     if (Fwrite(zeros, sizeof(char), pad, fdo) != pad) {
629b27
-	fprintf(stderr, _("Unable to write final padding\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("Unable to write final padding\n"));
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
     zeros = _free(zeros);
629b27
     if (Fwrite(&validation_pos, len, 1, fdo) != len) {
629b27
-	fprintf(stderr, _("Unable to write offset of validation output\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("Unable to write offset of validation output\n"));
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
     if (Fwrite(&digest_table_pos, len, 1, fdo) != len) {
629b27
-	fprintf(stderr, _("Unable to write offset of digest table\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("Unable to write offset of digest table\n"));
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
     if (Fwrite(&digest_pos, len, 1, fdo) != len) {
629b27
-	fprintf(stderr, _("Unable to write offset of validation table\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("Unable to write offset of validation table\n"));
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
     extents_magic_t magic = EXTENTS_MAGIC;
629b27
     len = sizeof(magic);
629b27
     if (Fwrite(&magic, len, 1, fdo) != len) {
629b27
-	fprintf(stderr, _("Unable to write magic\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("Unable to write magic\n"));
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
@@ -426,7 +448,9 @@ static off_t ufdTee(FD_t sfd, FD_t *fds, int len)
629b27
 	    for(int i=0; i < len; i++) {
629b27
 		wrbytes = Fwrite(buf, sizeof(buf[0]), rdbytes, fds[i]);
629b27
 		if (wrbytes != rdbytes) {
629b27
-		    fprintf(stderr, "Error wriing to FD %d: %s\n", i, Fstrerror(fds[i]));
629b27
+		    rpmlog(RPMLOG_ERR,
629b27
+			   _("Error wriing to FD %d: %s\n"),
629b27
+			   i, Fstrerror(fds[i]));
629b27
 		    total = -1;
629b27
 		    break;
629b27
 		}
629b27
@@ -460,22 +484,22 @@ static rpmRC teeRpm(FD_t fdi, uint8_t algos[], uint32_t algos_len) {
629b27
     FD_t fds[2];
629b27
 
629b27
      if (pipe(processorpipefd) == -1) {
629b27
-	fprintf(stderr, _("Processor pipe failure\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("Processor pipe failure\n"));
629b27
 	return RPMRC_FAIL;
629b27
     }
629b27
 
629b27
     if (pipe(validatorpipefd) == -1) {
629b27
-	fprintf(stderr, _("Validator pipe failure\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("Validator pipe failure\n"));
629b27
 	return RPMRC_FAIL;
629b27
     }
629b27
 
629b27
     if (pipe(meta_digestpipefd) == -1) {
629b27
-	fprintf(stderr, _("Meta digest pipe failure\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("Meta digest pipe failure\n"));
629b27
 	return RPMRC_FAIL;
629b27
     }
629b27
 
629b27
     if (pipe(meta_rpmsignpipefd) == -1) {
629b27
-	fprintf(stderr, _("Meta rpm signature pipe failure\n"));
629b27
+	rpmlog(RPMLOG_ERR, _("Meta rpm signature pipe failure\n"));
629b27
 	return RPMRC_FAIL;
629b27
     }
629b27
 
629b27
@@ -494,7 +518,7 @@ static rpmRC teeRpm(FD_t fdi, uint8_t algos[], uint32_t algos_len) {
629b27
 	close(meta_rpmsignpipefd[1]);
629b27
 	rc = validator(fdi, digesto, sigo, algos, algos_len);
629b27
 	if(rc != RPMRC_OK) {
629b27
-	    fprintf(stderr, _("Validator failed\n"));
629b27
+	    rpmlog(RPMLOG_ERR, _("Validator failed with RC %d\n"), rc);
629b27
 	}
629b27
 	Fclose(fdi);
629b27
 	Fclose(digesto);
629b27
@@ -522,7 +546,7 @@ static rpmRC teeRpm(FD_t fdi, uint8_t algos[], uint32_t algos_len) {
629b27
 
629b27
 	    rc = process_package(fdi, digestori, sigi);
629b27
 	    if(rc != RPMRC_OK) {
629b27
-		fprintf(stderr, _("Validator failed\n"));
629b27
+		rpmlog(RPMLOG_ERR, _("Package processor failed: %d\n"), rc);
629b27
 	    }
629b27
 	    Fclose(digestori);
629b27
 	    Fclose(sigi);
629b27
@@ -552,19 +576,19 @@ static rpmRC teeRpm(FD_t fdi, uint8_t algos[], uint32_t algos_len) {
629b27
 	    rc = RPMRC_OK;
629b27
 	    offt = ufdTee(fdi, fds, 2);
629b27
 	    if(offt == -1){
629b27
-		fprintf(stderr, _("Failed to tee RPM\n"));
629b27
+		rpmlog(RPMLOG_ERR, _("Failed to tee RPM\n"));
629b27
 		rc = RPMRC_FAIL;
629b27
 	    }
629b27
 	    Fclose(fds[0]);
629b27
 	    Fclose(fds[1]);
629b27
 	    w = waitpid(cpids[0], &wstatus, 0);
629b27
 	    if (w == -1) {
629b27
-		fprintf(stderr, _("waitpid cpids[0] failed\n"));
629b27
+		rpmlog(RPMLOG_ERR, _("waitpid cpids[0] failed\n"));
629b27
 		rc = RPMRC_FAIL;
629b27
 	    }
629b27
 	    w = waitpid(cpids[1], &wstatus, 0);
629b27
 	    if (w == -1) {
629b27
-		fprintf(stderr, _("waitpid cpids[1] failed\n"));
629b27
+		rpmlog(RPMLOG_ERR, _("waitpid cpids[1] failed\n"));
629b27
 		rc = RPMRC_FAIL;
629b27
 	    }
629b27
 	}
629b27
@@ -585,8 +609,8 @@ int main(int argc, char *argv[]) {
629b27
     poptSetOtherOptionHelp(optCon, "[OPTIONS]* <DIGESTALGO>");
629b27
 
629b27
     if (poptPeekArg(optCon) == NULL) {
629b27
-	fprintf(stderr,
629b27
-		_("Need at least one DIGESTALGO parameter, e.g. 'SHA256'\n"));
629b27
+	rpmlog(RPMLOG_ERR,
629b27
+	       _("Need at least one DIGESTALGO parameter, e.g. 'SHA256'\n"));
629b27
 	poptPrintUsage(optCon, stderr, 0);
629b27
 	exit(EXIT_FAILURE);
629b27
     }
629b27
@@ -598,9 +622,9 @@ int main(int argc, char *argv[]) {
629b27
     for (int x = 0; x < nb_algos; x++) {
629b27
 	if (pgpStringVal(PGPVAL_HASHALGO, args[x], &algos[x]) != 0)
629b27
 	{
629b27
-	    fprintf(stderr,
629b27
-		    _("Unable to resolve '%s' as a digest algorithm, exiting\n"),
629b27
-		    args[x]);
629b27
+	    rpmlog(RPMLOG_ERR,
629b27
+		   _("Unable to resolve '%s' as a digest algorithm, exiting\n"),
629b27
+		   args[x]);
629b27
 	    exit(EXIT_FAILURE);
629b27
 	}
629b27
     }
629b27
-- 
629b27
2.35.1
629b27