Blame SOURCES/0214-Save-Vendor-and-GPG-Fingerprint.patch

06486d
From 95ea12b9a4be68cc25f0811e693c7a024b5e3d4b Mon Sep 17 00:00:00 2001
06486d
From: Matej Habrnal <mhabrnal@redhat.com>
06486d
Date: Tue, 9 Feb 2016 16:53:21 +0100
06486d
Subject: [PATCH] Save Vendor and GPG Fingerprint
06486d
06486d
Red Hat keys can be found at:
06486d
  https://access.redhat.com/security/team/key
06486d
06486d
Related: #1258482
06486d
---
06486d
 src/daemon/abrt-action-save-package-data.c    | 34 ++++++++++++++++++++-----
06486d
 src/daemon/abrt-action-save-package-data.conf |  7 ++++++
06486d
 src/daemon/rpm.c                              | 36 +++++++++++++++++++--------
06486d
 src/daemon/rpm.h                              | 15 +++++++++++
06486d
 src/plugins/abrt-action-save-kernel-data      |  6 +++++
06486d
 5 files changed, 82 insertions(+), 16 deletions(-)
06486d
06486d
diff --git a/src/daemon/abrt-action-save-package-data.c b/src/daemon/abrt-action-save-package-data.c
06486d
index 97d5f5e..ef2007e 100644
06486d
--- a/src/daemon/abrt-action-save-package-data.c
06486d
+++ b/src/daemon/abrt-action-save-package-data.c
06486d
@@ -224,6 +224,7 @@ static int SavePackageDescriptionToDebugDump(const char *dump_dir_name)
06486d
     char *cmdline = NULL;
06486d
     char *executable = NULL;
06486d
     char *package_short_name = NULL;
06486d
+    char *fingerprint = NULL;
06486d
     struct pkg_envra *pkg_name = NULL;
06486d
     char *component = NULL;
06486d
     int error = 1;
06486d
@@ -311,13 +312,12 @@ static int SavePackageDescriptionToDebugDump(const char *dump_dir_name)
06486d
         goto ret; /* return 1 (failure) */
06486d
     }
06486d
 
06486d
-    if (settings_bOpenGPGCheck)
06486d
+    fingerprint = rpm_get_fingerprint(package_short_name);
06486d
+    if (!(fingerprint != NULL && rpm_fingerprint_is_imported(fingerprint))
06486d
+         && settings_bOpenGPGCheck)
06486d
     {
06486d
-        if (!rpm_chk_fingerprint(package_short_name))
06486d
-        {
06486d
-            log("Package '%s' isn't signed with proper key", package_short_name);
06486d
-            goto ret; /* return 1 (failure) */
06486d
-        }
06486d
+        log("Package '%s' isn't signed with proper key", package_short_name);
06486d
+        goto ret; /* return 1 (failure) */
06486d
         /* We used to also check the integrity of the executable here:
06486d
          *  if (!CheckHash(package_short_name.c_str(), executable)) BOOM();
06486d
          * Checking the MD5 sum requires to run prelink to "un-prelink" the
06486d
@@ -340,6 +340,27 @@ static int SavePackageDescriptionToDebugDump(const char *dump_dir_name)
06486d
         dd_save_text(dd, FILENAME_PKG_VERSION, pkg_name->p_version);
06486d
         dd_save_text(dd, FILENAME_PKG_RELEASE, pkg_name->p_release);
06486d
         dd_save_text(dd, FILENAME_PKG_ARCH, pkg_name->p_arch);
06486d
+        dd_save_text(dd, FILENAME_PKG_VENDOR, pkg_name->p_vendor);
06486d
+
06486d
+        if (fingerprint)
06486d
+        {
06486d
+            /* 16 character + 3 spaces + 1 '\0' + 2 Bytes for errors :) */
06486d
+            char key_fingerprint[22] = {0};
06486d
+
06486d
+            /* The condition is just a defense against errors */
06486d
+            for (size_t i = 0, j = 0; j < sizeof(key_fingerprint) - 2; )
06486d
+            {
06486d
+                key_fingerprint[j++] = toupper(fingerprint[i++]);
06486d
+
06486d
+                if (fingerprint[i] == '\0')
06486d
+                    break;
06486d
+
06486d
+                if (!(i & (0x3)))
06486d
+                    key_fingerprint[j++] = ' ';
06486d
+            }
06486d
+
06486d
+            dd_save_text(dd, FILENAME_PKG_FINGERPRINT, key_fingerprint);
06486d
+        }
06486d
     }
06486d
 
06486d
     if (component)
06486d
@@ -355,6 +376,7 @@ static int SavePackageDescriptionToDebugDump(const char *dump_dir_name)
06486d
     free(package_short_name);
06486d
     free_pkg_envra(pkg_name);
06486d
     free(component);
06486d
+    free(fingerprint);
06486d
 
06486d
     return error;
06486d
 }
06486d
diff --git a/src/daemon/abrt-action-save-package-data.conf b/src/daemon/abrt-action-save-package-data.conf
06486d
index 3d35bb6..bf97264 100644
06486d
--- a/src/daemon/abrt-action-save-package-data.conf
06486d
+++ b/src/daemon/abrt-action-save-package-data.conf
06486d
@@ -3,6 +3,13 @@
06486d
 # the list of public keys used to check the signature is
06486d
 # in the file gpg_keys
06486d
 #
06486d
+# How can I check the GPG key used to sign an installed pacakge on
06486d
+# Red hat Enterprise Linux:
06486d
+# https://access.redhat.com/solutions/1120013
06486d
+#
06486d
+# Product Signing (GPG) Keys:
06486d
+# https://access.redhat.com/security/team/key
06486d
+#
06486d
 OpenGPGCheck = yes
06486d
 
06486d
 # Blacklisted packages
06486d
diff --git a/src/daemon/rpm.c b/src/daemon/rpm.c
06486d
index b69992c..d3d3d0a 100644
06486d
--- a/src/daemon/rpm.c
06486d
+++ b/src/daemon/rpm.c
06486d
@@ -99,7 +99,22 @@ void rpm_load_gpgkey(const char* filename)
06486d
 
06486d
 int rpm_chk_fingerprint(const char* pkg)
06486d
 {
06486d
-    int ret = 0;
06486d
+    char *fingerprint = rpm_get_fingerprint(pkg);
06486d
+    int res = 0;
06486d
+    if (fingerprint)
06486d
+        res = rpm_fingerprint_is_imported(fingerprint);
06486d
+    free(fingerprint);
06486d
+    return res;
06486d
+}
06486d
+
06486d
+int rpm_fingerprint_is_imported(const char* fingerprint)
06486d
+{
06486d
+    return !!g_list_find_custom(list_fingerprints, fingerprint, (GCompareFunc)g_strcmp0);
06486d
+}
06486d
+
06486d
+char *rpm_get_fingerprint(const char *pkg)
06486d
+{
06486d
+    char *fingerprint = NULL;
06486d
     char *pgpsig = NULL;
06486d
     const char *errmsg = NULL;
06486d
 
06486d
@@ -117,20 +132,15 @@ int rpm_chk_fingerprint(const char* pkg)
06486d
         goto error;
06486d
     }
06486d
 
06486d
-    {
06486d
-        char *pgpsig_tmp = strstr(pgpsig, " Key ID ");
06486d
-        if (pgpsig_tmp)
06486d
-        {
06486d
-            pgpsig_tmp += sizeof(" Key ID ") - 1;
06486d
-            ret = g_list_find_custom(list_fingerprints, pgpsig_tmp, (GCompareFunc)g_strcmp0) != NULL;
06486d
-        }
06486d
-    }
06486d
+    char *pgpsig_tmp = strstr(pgpsig, " Key ID ");
06486d
+    if (pgpsig_tmp)
06486d
+        fingerprint = xstrdup(pgpsig_tmp + sizeof(" Key ID ") - 1);
06486d
 
06486d
 error:
06486d
     free(pgpsig);
06486d
     rpmdbFreeIterator(iter);
06486d
     rpmtsFree(ts);
06486d
-    return ret;
06486d
+    return fingerprint;
06486d
 }
06486d
 
06486d
 /*
06486d
@@ -244,6 +254,7 @@ pkg_add_id(name);
06486d
 pkg_add_id(version);
06486d
 pkg_add_id(release);
06486d
 pkg_add_id(arch);
06486d
+pkg_add_id(vendor);
06486d
 
06486d
 // caller is responsible to free returned value
06486d
 struct pkg_envra *rpm_get_package_nvr(const char *filename, const char *rootdir_or_NULL)
06486d
@@ -314,6 +325,10 @@ struct pkg_envra *rpm_get_package_nvr(const char *filename, const char *rootdir_
06486d
     if (r)
06486d
         goto error;
06486d
 
06486d
+    r = pkg_add_vendor(header, p);
06486d
+    if (r)
06486d
+        goto error;
06486d
+
06486d
     p->p_nvr = xasprintf("%s-%s-%s", p->p_name, p->p_version, p->p_release);
06486d
 
06486d
     rpmdbFreeIterator(iter);
06486d
@@ -334,6 +349,7 @@ void free_pkg_envra(struct pkg_envra *p)
06486d
     if (!p)
06486d
         return;
06486d
 
06486d
+    free(p->p_vendor);
06486d
     free(p->p_epoch);
06486d
     free(p->p_name);
06486d
     free(p->p_version);
06486d
diff --git a/src/daemon/rpm.h b/src/daemon/rpm.h
06486d
index 1b90368..89aa088 100644
06486d
--- a/src/daemon/rpm.h
06486d
+++ b/src/daemon/rpm.h
06486d
@@ -38,6 +38,7 @@ struct pkg_envra {
06486d
     char *p_version;
06486d
     char *p_release;
06486d
     char *p_arch;
06486d
+    char *p_vendor;
06486d
 };
06486d
 
06486d
 void free_pkg_envra(struct pkg_envra *p);
06486d
@@ -69,6 +70,20 @@ void rpm_load_gpgkey(const char* filename);
06486d
 int rpm_chk_fingerprint(const char* pkg);
06486d
 
06486d
 /**
06486d
+ * A function, which checks if the given finger print is imported.
06486d
+ * @param pkg A package name.
06486d
+ * @return 1 if imported, otherwise (not-imported, or error) 0
06486d
+ */
06486d
+int rpm_fingerprint_is_imported(const char* fingerprint);
06486d
+
06486d
+/**
06486d
+ * A function, which returns package's finger print
06486d
+ * @param pkg A package name.
06486d
+ * @return NULL if not-valid, otherwise malloced NULL-terminated string.
06486d
+ */
06486d
+char *rpm_get_fingerprint(const char* pkg);
06486d
+
06486d
+/**
06486d
  * Gets a package name. This package contains particular
06486d
  * file. If the file doesn't belong to any package, empty string is
06486d
  * returned.
06486d
diff --git a/src/plugins/abrt-action-save-kernel-data b/src/plugins/abrt-action-save-kernel-data
06486d
index 7df85cf..5f1ddc7 100755
06486d
--- a/src/plugins/abrt-action-save-kernel-data
06486d
+++ b/src/plugins/abrt-action-save-kernel-data
06486d
@@ -29,3 +29,9 @@ rpm -q --qf "%{release}\n" "$package" > pkg_release
06486d
 epoch="$( rpm -q --qf "%{epoch}" "$package" )"
06486d
 test "$epoch" = "(none)"  && epoch=0
06486d
 echo "$epoch" > pkg_epoch
06486d
+rpm -q --qf "%{vendor}\n" "$package" > pkg_vendor
06486d
+
06486d
+FINGERPRINT=$(rpm -q --qf "%|SIGGPG?{%{SIGGPG:pgpsig}}:{%{SIGPGP:pgpsig}}|" "$package" 2>/dev/null | tail -1)
06486d
+if [ -n "$FINGERPRINT" -a "_(none)" != "_$FINGERPRINT" ]; then
06486d
+    echo $FINGERPRINT | sed 's/.*Key ID \(....\)\(....\)\(....\)\(....\)$/\U\1 \U\2 \U\3 \U\4/' > pkg_fingerprint
06486d
+fi
06486d
-- 
06486d
1.8.3.1
06486d