alexk / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone
629b27
From aabaa6c6587c37b84a1b9cfd98bff31f1b69345e Mon Sep 17 00:00:00 2001
629b27
From: chantra <chantr4@gmail.com>
629b27
Date: Wed, 16 Feb 2022 17:00:09 -0800
629b27
Subject: [PATCH 24/30] [rpm2extents] create footer struct and helpers
629b27
629b27
new extents_footer and extents_footer_offset struct with a function to
629b27
load offsets from an FD.
629b27
Change existing code to use new struct/functions
629b27
---
629b27
 lib/rpmchecksig.c         | 16 +++-------------
629b27
 lib/rpmextents.c          | 26 +++++++++++++++++---------
629b27
 lib/rpmextents_internal.h | 31 ++++++++++++++++++++++++++++++-
629b27
 rpm2extents.c             | 23 ++++-------------------
629b27
 4 files changed, 54 insertions(+), 42 deletions(-)
629b27
629b27
diff --git a/lib/rpmchecksig.c b/lib/rpmchecksig.c
629b27
index dc1726a18..729f79f9f 100644
629b27
--- a/lib/rpmchecksig.c
629b27
+++ b/lib/rpmchecksig.c
629b27
@@ -223,29 +223,19 @@ exit:
629b27
 
629b27
 static int rpmpkgVerifySigsTranscoded(FD_t fd){
629b27
     rpm_loff_t current;
629b27
-    uint64_t magic;
629b27
-    rpm_loff_t offset;
629b27
     int32_t rc;
629b27
     size_t len;
629b27
     uint64_t content_len;
629b27
     char *content = NULL;
629b27
+    struct extents_footer_t footer;
629b27
 
629b27
     current = Ftell(fd);
629b27
 
629b27
-    if(Fseek(fd, -(sizeof(magic) + 3 * sizeof(offset) ), SEEK_END) < 0) {
629b27
-	rpmlog(RPMLOG_ERR, _("rpmpkgVerifySigsTranscoded: failed to seek for offset\n"));
629b27
+    if(extentsFooterFromFD(fd, &footer) != RPMRC_OK) {
629b27
 	rc = -1;
629b27
 	goto exit;
629b27
     }
629b27
-
629b27
-    len = sizeof(offset);
629b27
-    if (Fread(&offset, len, 1, fd) != len) {
629b27
-	rpmlog(RPMLOG_ERR, _("rpmpkgVerifySigsTranscoded: Failed to read Signature Verification offset\n"));
629b27
-	rc = -1;
629b27
-	goto exit;
629b27
-    }
629b27
-
629b27
-    if(Fseek(fd,  offset, SEEK_SET) < 0) {
629b27
+    if(Fseek(fd, footer.offsets.checksig_offset, SEEK_SET) < 0) {
629b27
 	rpmlog(RPMLOG_ERR, _("rpmpkgVerifySigsTranscoded: Failed to seek signature verification offset\n"));
629b27
 	rc = -1;
629b27
 	goto exit;
629b27
diff --git a/lib/rpmextents.c b/lib/rpmextents.c
629b27
index 015277751..46b7aadff 100644
629b27
--- a/lib/rpmextents.c
629b27
+++ b/lib/rpmextents.c
629b27
@@ -3,13 +3,16 @@
629b27
 
629b27
 #include <rpm/rpmlog.h>
629b27
 #include <rpm/rpmio.h>
629b27
+#include <string.h>
629b27
+#include <errno.h>
629b27
+
629b27
 
629b27
 #include "lib/rpmextents_internal.h"
629b27
 
629b27
-rpmRC isTranscodedRpm(FD_t fd) {
629b27
+rpmRC extentsFooterFromFD(FD_t fd, struct extents_footer_t *footer) {
629b27
+
629b27
     rpmRC rc = RPMRC_NOTFOUND;
629b27
     rpm_loff_t current;
629b27
-    extents_magic_t magic;
629b27
     size_t len;
629b27
 
629b27
     // If the file is not seekable, we cannot detect whether or not it is transcoded.
629b27
@@ -18,19 +21,19 @@ rpmRC isTranscodedRpm(FD_t fd) {
629b27
     }
629b27
     current = Ftell(fd);
629b27
 
629b27
-    if(Fseek(fd, -(sizeof(magic)), SEEK_END) < 0) {
629b27
-	rpmlog(RPMLOG_ERR, _("isTranscodedRpm: failed to seek for magic\n"));
629b27
+    len = sizeof(struct extents_footer_t);
629b27
+    if(Fseek(fd, -len, SEEK_END) < 0) {
629b27
+	rpmlog(RPMLOG_ERR, _("isTranscodedRpm: failed to seek for footer: %s\n"), strerror(errno));
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
-    len = sizeof(magic);
629b27
-    if (Fread(&magic, len, 1, fd) != len) {
629b27
-	rpmlog(RPMLOG_ERR, _("isTranscodedRpm: unable to read magic\n"));
629b27
+    if (Fread(footer, len, 1, fd) != len) {
629b27
+	rpmlog(RPMLOG_ERR, _("isTranscodedRpm: unable to read footer\n"));
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
-    if (magic != EXTENTS_MAGIC) {
629b27
-	rpmlog(RPMLOG_DEBUG, _("isTranscodedRpm: not transcoded\n"));
629b27
+    if (footer->magic != EXTENTS_MAGIC) {
629b27
+	rpmlog(RPMLOG_ERR, _("isTranscodedRpm: not transcoded\n"));
629b27
 	rc = RPMRC_NOTFOUND;
629b27
 	goto exit;
629b27
     }
629b27
@@ -43,4 +46,9 @@ exit:
629b27
     return rc;
629b27
 }
629b27
 
629b27
+rpmRC isTranscodedRpm(FD_t fd) {
629b27
+    struct extents_footer_t footer;
629b27
+    return extentsFooterFromFD(fd, &footer);
629b27
+}
629b27
+
629b27
 
629b27
diff --git a/lib/rpmextents_internal.h b/lib/rpmextents_internal.h
629b27
index 57cecfc31..f0c29c807 100644
629b27
--- a/lib/rpmextents_internal.h
629b27
+++ b/lib/rpmextents_internal.h
629b27
@@ -7,6 +7,10 @@ extern "C" {
629b27
 
629b27
 #include <stdint.h>
629b27
 
629b27
+/** \ingroup rpmextents
629b27
+ * RPM extents library
629b27
+ */
629b27
+
629b27
 /* magic value at end of file (64 bits) that indicates this is a transcoded
629b27
  * rpm.
629b27
  */
629b27
@@ -14,9 +18,34 @@ extern "C" {
629b27
 
629b27
 typedef uint64_t extents_magic_t;
629b27
 
629b27
+struct __attribute__ ((__packed__)) extents_footer_offsets_t {
629b27
+    off64_t checksig_offset;
629b27
+    off64_t table_offset;
629b27
+    off64_t csum_offset;
629b27
+};
629b27
+
629b27
+struct __attribute__ ((__packed__)) extents_footer_t {
629b27
+    struct extents_footer_offsets_t offsets;
629b27
+    extents_magic_t magic;
629b27
+};
629b27
+
629b27
+
629b27
+/** \ingroup rpmextents
629b27
+ * Read the RPM Extents footer from a file descriptor.
629b27
+ * @param fd		The FD_t of the transcoded RPM
629b27
+ * @param[out] footer	A pointer to an allocated extents_footer_t with a copy of the footer.
629b27
+ * @return		RPMRC_OK on success, RPMRC_NOTFOUND if not a transcoded file, RPMRC_FAIL on any failure.
629b27
+ */
629b27
+rpmRC extentsFooterFromFD(FD_t fd, struct extents_footer_t *footer);
629b27
+
629b27
+/** \ingroup rpmextents
629b27
+ * Check if a RPM is a transcoded RPM
629b27
+ * @param fd	The FD_t of the transcoded RPM
629b27
+ * return	RPMRC_OK on success, RPMRC_NOTFOUND if not a transcoded file, RPMRC_FAIL on any failure.
629b27
+ */
629b27
 rpmRC isTranscodedRpm(FD_t fd);
629b27
 
629b27
 #ifdef __cplusplus
629b27
 }
629b27
 #endif
629b27
-#endif
629b27
+#endif /* _RPMEXTENTS_INTERNAL_H */
629b27
diff --git a/rpm2extents.c b/rpm2extents.c
629b27
index e1a19fedb..7dd5128de 100644
629b27
--- a/rpm2extents.c
629b27
+++ b/rpm2extents.c
629b27
@@ -405,25 +405,10 @@ static rpmRC process_package(FD_t fdi, FD_t digestori, FD_t validationi)
629b27
 	goto exit;
629b27
     }
629b27
     zeros = _free(zeros);
629b27
-    if (Fwrite(&validation_pos, len, 1, fdo) != len) {
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
-	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
-	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
-	rpmlog(RPMLOG_ERR, _("Unable to write magic\n"));
629b27
+    struct extents_footer_t footer = {.offsets = {validation_pos, digest_table_pos, digest_pos}, .magic = EXTENTS_MAGIC};
629b27
+    len = sizeof(footer);
629b27
+    if (Fwrite(&footer, len, 1, fdo) != len) {
629b27
+	rpmlog(RPMLOG_ERR, _("Unable to write footer\n"));
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
-- 
629b27
2.35.1
629b27