Blame SOURCES/0102-ureport-get-rhsm-entitlement-cert-dir-from-rhsm-conf.patch

4b6aa8
From 2b20c9f91342da7744ae40ee623735ab95f83219 Mon Sep 17 00:00:00 2001
4b6aa8
From: Jakub Filak <jfilak@redhat.com>
4b6aa8
Date: Wed, 22 Oct 2014 14:27:00 +0200
4b6aa8
Subject: [LIBREPORT PATCH 102/105] ureport: get rhsm entitlement cert dir from
4b6aa8
 rhsm conf
4b6aa8
4b6aa8
Related #1140224
4b6aa8
4b6aa8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
4b6aa8
---
4b6aa8
 src/lib/ureport.c | 46 +++++++++++++++++++++++++++++++++++++++++-----
4b6aa8
 1 file changed, 41 insertions(+), 5 deletions(-)
4b6aa8
4b6aa8
diff --git a/src/lib/ureport.c b/src/lib/ureport.c
4b6aa8
index 5782b4e..41f4531 100644
4b6aa8
--- a/src/lib/ureport.c
4b6aa8
+++ b/src/lib/ureport.c
4b6aa8
@@ -32,7 +32,6 @@
4b6aa8
 
4b6aa8
 #define RHSM_WEB_SERVICE_URL "https://api.access.redhat.com/rs/telemetry/abrt"
4b6aa8
 
4b6aa8
-#define RHSMENT_PEM_DIR_PATH "/etc/pki/entitlement"
4b6aa8
 #define RHSMENT_ENT_DATA_BEGIN_TAG "-----BEGIN ENTITLEMENT DATA-----"
4b6aa8
 #define RHSMENT_ENT_DATA_END_TAG "-----END ENTITLEMENT DATA-----"
4b6aa8
 #define RHSMENT_SIG_DATA_BEGIN_TAG "-----BEGIN RSA SIGNATURE-----"
4b6aa8
@@ -69,6 +68,33 @@ ureport_server_config_set_url(struct ureport_server_config *config,
4b6aa8
     config->ur_url = server_url;
4b6aa8
 }
4b6aa8
 
4b6aa8
+static char *
4b6aa8
+rhsm_config_get_entitlement_cert_dir(void)
4b6aa8
+{
4b6aa8
+    char *result = getenv("LIBREPORT_DEBUG_RHSMENT_PEM_DIR_PATH");
4b6aa8
+    if (result != NULL)
4b6aa8
+        return xstrdup(result);
4b6aa8
+
4b6aa8
+    result = run_in_shell_and_save_output(0,
4b6aa8
+            "python -c \"from rhsm.config import initConfig; print(initConfig().get('rhsm', 'entitlementCertDir'))\"",
4b6aa8
+            NULL, NULL);
4b6aa8
+
4b6aa8
+    /* run_in_shell_and_save_output always returns non-NULL */
4b6aa8
+    if (result[0] != '/')
4b6aa8
+        goto error;
4b6aa8
+
4b6aa8
+    char *newline = strchrnul(result, '\n');
4b6aa8
+    if (!newline)
4b6aa8
+        goto error;
4b6aa8
+
4b6aa8
+    *newline = '\0';
4b6aa8
+    return result;
4b6aa8
+error:
4b6aa8
+    error_msg("Failed to get 'rhsm':'entitlementCertDir' from rhsm.config python module.");
4b6aa8
+    free(result);
4b6aa8
+    return NULL;
4b6aa8
+}
4b6aa8
+
4b6aa8
 void
4b6aa8
 ureport_server_config_set_client_auth(struct ureport_server_config *config,
4b6aa8
                                       const char *client_auth)
4b6aa8
@@ -91,13 +117,21 @@ ureport_server_config_set_client_auth(struct ureport_server_config *config,
4b6aa8
         if (config->ur_url == NULL)
4b6aa8
             ureport_server_config_set_url(config, xstrdup(RHSM_WEB_SERVICE_URL));
4b6aa8
 
4b6aa8
-        GList *certs = get_file_list(RHSMENT_PEM_DIR_PATH, "pem");
4b6aa8
+        char *rhsm_dir = rhsm_config_get_entitlement_cert_dir();
4b6aa8
+        if (rhsm_dir == NULL)
4b6aa8
+        {
4b6aa8
+            log_notice("Not using client authentication");
4b6aa8
+            return;
4b6aa8
+        }
4b6aa8
+
4b6aa8
+        GList *certs = get_file_list(rhsm_dir, "pem");
4b6aa8
         if (g_list_length(certs) < 2)
4b6aa8
         {
4b6aa8
             g_list_free_full(certs, (GDestroyNotify)free_file_obj);
4b6aa8
 
4b6aa8
-            log_notice(RHSMENT_PEM_DIR_PATH" does not contain unique cert-key files pair");
4b6aa8
+            log_notice("'%s' does not contain a cert-key files pair", rhsm_dir);
4b6aa8
             log_notice("Not using client authentication");
4b6aa8
+            free(rhsm_dir);
4b6aa8
             return;
4b6aa8
         }
4b6aa8
 
4b6aa8
@@ -116,8 +150,9 @@ ureport_server_config_set_client_auth(struct ureport_server_config *config,
4b6aa8
         {
4b6aa8
             g_list_free_full(certs, (GDestroyNotify)free_file_obj);
4b6aa8
 
4b6aa8
-            log_notice(RHSMENT_PEM_DIR_PATH" contains only key files");
4b6aa8
+            log_notice("'%s' does not contain a cert file (only keys)", rhsm_dir);
4b6aa8
             log_notice("Not using client authentication");
4b6aa8
+            free(rhsm_dir);
4b6aa8
             return;
4b6aa8
         }
4b6aa8
 
4b6aa8
@@ -125,7 +160,8 @@ ureport_server_config_set_client_auth(struct ureport_server_config *config,
4b6aa8
         /* Yes, the key file may not exists. I over took this code from
4b6aa8
          * sos-uploader and they are pretty happy with this approach, so why
4b6aa8
          * shouldn't we?. */
4b6aa8
-        config->ur_client_key = xasprintf("%s/%s-key.pem", RHSMENT_PEM_DIR_PATH, fo_get_filename(cert));
4b6aa8
+        config->ur_client_key = xasprintf("%s/%s-key.pem", rhsm_dir, fo_get_filename(cert));
4b6aa8
+        free(rhsm_dir);
4b6aa8
 
4b6aa8
         log_debug("Using cert files: '%s' : '%s'", config->ur_client_cert, config->ur_client_key);
4b6aa8
 
4b6aa8
-- 
4b6aa8
1.8.3.1
4b6aa8