Blame SOURCES/0139-ureport-add-functionality-to-use-consumer-certificat.patch

562801
From d8459ff67af8566583a4b33d151a182d48722078 Mon Sep 17 00:00:00 2001
562801
From: Matej Habrnal <mhabrnal@redhat.com>
562801
Date: Wed, 27 May 2015 14:45:20 +0200
562801
Subject: [PATCH] ureport: add functionality to use consumer certificate
562801
562801
reporter-ureport uses consumer certificate instead of entitlement certificate,
562801
if the rhsm authentication is enabled. Also uses
562801
https://cert-api.access.redhat.com/rs/telemetry/abrt to report those
562801
autenticated uReports.
562801
562801
Related to rhbz#1223805
562801
562801
Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
562801
---
562801
 src/lib/ureport.c        | 121 +++++++++++++----------------------------------
562801
 src/plugins/ureport.conf |   2 +-
562801
 2 files changed, 33 insertions(+), 90 deletions(-)
562801
562801
diff --git a/src/lib/ureport.c b/src/lib/ureport.c
562801
index 5065a52..990ace6 100644
562801
--- a/src/lib/ureport.c
562801
+++ b/src/lib/ureport.c
562801
@@ -31,13 +31,11 @@
562801
 
562801
 #define BTHASH_URL_SFX "reports/bthash/"
562801
 
562801
-#define RHSM_WEB_SERVICE_URL "https://api.access.redhat.com/rs/telemetry/abrt"
562801
+#define RHSM_WEB_SERVICE_URL "https://cert-api.access.redhat.com/rs/telemetry/abrt"
562801
 
562801
-#define RHSMENT_PEM_DIR_PATH "/etc/pki/entitlement"
562801
-#define RHSMENT_ENT_DATA_BEGIN_TAG "-----BEGIN ENTITLEMENT DATA-----"
562801
-#define RHSMENT_ENT_DATA_END_TAG "-----END ENTITLEMENT DATA-----"
562801
-#define RHSMENT_SIG_DATA_BEGIN_TAG "-----BEGIN RSA SIGNATURE-----"
562801
-#define RHSMENT_SIG_DATA_END_TAG "-----END RSA SIGNATURE-----"
562801
+#define RHSMCON_PEM_DIR_PATH "/etc/pki/consumer"
562801
+#define RHSMCON_CERT_NAME "cert.pem"
562801
+#define RHSMCON_KEY_NAME "key.pem"
562801
 
562801
 
562801
 static char *
562801
@@ -71,14 +69,14 @@ ureport_server_config_set_url(struct ureport_server_config *config,
562801
 }
562801
 
562801
 static char *
562801
-rhsm_config_get_entitlement_cert_dir(void)
562801
+rhsm_config_get_consumer_cert_dir(void)
562801
 {
562801
-    char *result = getenv("LIBREPORT_DEBUG_RHSMENT_PEM_DIR_PATH");
562801
+    char *result = getenv("LIBREPORT_DEBUG_RHSMCON_PEM_DIR_PATH");
562801
     if (result != NULL)
562801
         return xstrdup(result);
562801
 
562801
     result = run_in_shell_and_save_output(0,
562801
-            "python -c \"from rhsm.config import initConfig; print(initConfig().get('rhsm', 'entitlementCertDir'))\"",
562801
+            "python -c \"from rhsm.config import initConfig; print(initConfig().get('rhsm', 'consumerCertDir'))\"",
562801
             NULL, NULL);
562801
 
562801
     /* run_in_shell_and_save_output always returns non-NULL */
562801
@@ -93,8 +91,19 @@ rhsm_config_get_entitlement_cert_dir(void)
562801
     return result;
562801
 error:
562801
     free(result);
562801
-    error_msg("Failed to get 'rhsm':'entitlementCertDir' from rhsm.config python module. Using "RHSMENT_PEM_DIR_PATH);
562801
-    return xstrdup(RHSMENT_PEM_DIR_PATH);
562801
+    error_msg("Failed to get 'rhsm':'consumerCertDir' from rhsm.config python module. Using "RHSMCON_PEM_DIR_PATH);
562801
+    return xstrdup(RHSMCON_PEM_DIR_PATH);
562801
+}
562801
+
562801
+static bool
562801
+certificate_exist(char *cert_name)
562801
+{
562801
+    if (access(cert_name, F_OK) != 0)
562801
+    {
562801
+        log_notice("RHSM consumer certificate '%s' does not exist.", cert_name);
562801
+        return false;
562801
+    }
562801
+    return true;
562801
 }
562801
 
562801
 void
562801
@@ -119,93 +128,27 @@ ureport_server_config_set_client_auth(struct ureport_server_config *config,
562801
         if (config->ur_url == NULL)
562801
             ureport_server_config_set_url(config, xstrdup(RHSM_WEB_SERVICE_URL));
562801
 
562801
-        char *rhsm_dir = rhsm_config_get_entitlement_cert_dir();
562801
-        if (rhsm_dir == NULL)
562801
-        {
562801
-            log_notice("Not using client authentication");
562801
-            return;
562801
-        }
562801
+        /* always returns non-NULL */
562801
+        char *rhsm_dir = rhsm_config_get_consumer_cert_dir();
562801
 
562801
-        GList *certs = get_file_list(rhsm_dir, "pem");
562801
-        if (g_list_length(certs) < 2)
562801
-        {
562801
-            g_list_free_full(certs, (GDestroyNotify)free_file_obj);
562801
+        char *cert_full_name = concat_path_file(rhsm_dir, RHSMCON_CERT_NAME);
562801
+        char *key_full_name = concat_path_file(rhsm_dir, RHSMCON_KEY_NAME);
562801
 
562801
-            log_notice("'%s' does not contain a cert-key files pair", rhsm_dir);
562801
-            log_notice("Not using client authentication");
562801
-            free(rhsm_dir);
562801
-            return;
562801
-        }
562801
-
562801
-        /* Use the last non-key file found. */
562801
-        file_obj_t *cert = NULL;
562801
-        for (GList *iter = certs; iter != NULL; iter = g_list_next(iter))
562801
+        if (certificate_exist(cert_full_name) && certificate_exist(key_full_name))
562801
         {
562801
-            file_obj_t *tmp = (file_obj_t *)iter->data;
562801
-            const char *file_name = fo_get_filename(tmp);
562801
-
562801
-            if (suffixcmp(file_name, "-key"))
562801
-                cert = tmp;
562801
+            config->ur_client_cert = cert_full_name;
562801
+            config->ur_client_key = key_full_name;
562801
+            log_debug("Using cert files: '%s' : '%s'", config->ur_client_cert, config->ur_client_key);
562801
         }
562801
-
562801
-        if (cert == NULL)
562801
+        else
562801
         {
562801
-            g_list_free_full(certs, (GDestroyNotify)free_file_obj);
562801
-
562801
-            log_notice("'%s' does not contain a cert file (only keys)", rhsm_dir);
562801
-            log_notice("Not using client authentication");
562801
-            free(rhsm_dir);
562801
-            return;
562801
+            free(cert_full_name);
562801
+            free(key_full_name);
562801
+            log_notice("Using the default configuration for uReports.");
562801
         }
562801
 
562801
-        config->ur_client_cert = xstrdup(fo_get_fullpath(cert));
562801
-        /* Yes, the key file may not exists. I over took this code from
562801
-         * sos-uploader and they are pretty happy with this approach, so why
562801
-         * shouldn't we?. */
562801
-        config->ur_client_key = xasprintf("%s/%s-key.pem", rhsm_dir, fo_get_filename(cert));
562801
         free(rhsm_dir);
562801
 
562801
-        log_debug("Using cert files: '%s' : '%s'", config->ur_client_cert, config->ur_client_key);
562801
-
562801
-        g_list_free_full(certs, (GDestroyNotify)free_file_obj);
562801
-
562801
-        char *certdata = xmalloc_open_read_close(config->ur_client_cert, /*no size limit*/NULL);
562801
-        if (certdata != NULL)
562801
-        {
562801
-            char *ent_data = xstrdup_between(certdata,
562801
-                    RHSMENT_ENT_DATA_BEGIN_TAG, RHSMENT_ENT_DATA_END_TAG);
562801
-
562801
-            char *sig_data = xstrdup_between(certdata,
562801
-                    RHSMENT_SIG_DATA_BEGIN_TAG, RHSMENT_SIG_DATA_END_TAG);
562801
-
562801
-            if (ent_data != NULL && sig_data != NULL)
562801
-            {
562801
-                ent_data = strremovech(ent_data, '\n');
562801
-                insert_map_string(config->ur_http_headers,
562801
-                        xstrdup("X-RH-Entitlement-Data"),
562801
-                        xasprintf(RHSMENT_ENT_DATA_BEGIN_TAG"%s"RHSMENT_ENT_DATA_END_TAG, ent_data));
562801
-
562801
-                sig_data = strremovech(sig_data, '\n');
562801
-                insert_map_string(config->ur_http_headers,
562801
-                        xstrdup("X-RH-Entitlement-Sig"),
562801
-                        xasprintf(RHSMENT_SIG_DATA_BEGIN_TAG"%s"RHSMENT_SIG_DATA_END_TAG, sig_data));
562801
-            }
562801
-            else
562801
-            {
562801
-                log_notice("Cert file '%s' doesn't contain Entitlement and RSA Signature sections", config->ur_client_cert);
562801
-                log_notice("Not using client authentication");
562801
-
562801
-                free(config->ur_client_cert);
562801
-                config->ur_client_cert = NULL;
562801
-
562801
-                free(config->ur_client_key);
562801
-                config->ur_client_key = NULL;
562801
-            }
562801
-
562801
-            free(sig_data);
562801
-            free(ent_data);
562801
-            free(certdata);
562801
-        }
562801
     }
562801
     else if (strcmp(client_auth, "puppet") == 0)
562801
     {
562801
diff --git a/src/plugins/ureport.conf b/src/plugins/ureport.conf
562801
index e04bf56..2256a7f 100644
562801
--- a/src/plugins/ureport.conf
562801
+++ b/src/plugins/ureport.conf
562801
@@ -22,7 +22,7 @@ AuthDataItems = hostname, machineid
562801
 # 'IncludeAuthData' to 'yes'.
562801
 # None (default):
562801
 # SSLClientAuth =
562801
-# Using RH subscription management entitlement certificate:
562801
+# Using RH subscription management consumer certificate:
562801
 # SSLClientAuth = rhsm
562801
 # Using Puppet certificate:
562801
 # SSLClientAuth = puppet
562801
-- 
562801
2.4.3
562801