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

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