Blame SOURCES/0155-ureport-use-Red-Hat-Certificate-Authority-to-make-rh.patch

28bab8
From fc56c987058558d47d6bfe64ec11d2819b7886fe Mon Sep 17 00:00:00 2001
28bab8
From: Matej Habrnal <mhabrnal@redhat.com>
28bab8
Date: Thu, 3 Sep 2015 13:55:07 +0200
28bab8
Subject: [PATCH] ureport: use Red Hat Certificate Authority to make rhsm cert
28bab8
 trusted
28bab8
28bab8
In the case we use authenticated auto reporting by rhsm the cert is not trusted
28bab8
and it breaks Auto-reporting feature. This commit feeds curl with the
28bab8
cert-api.access.redhat.com.pem file which make the cert trusted.
28bab8
28bab8
Related to rhbz#1223805
28bab8
28bab8
Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
28bab8
---
28bab8
 src/include/ureport.h |  1 +
28bab8
 src/lib/ureport.c     | 42 ++++++++++++++++++++++++++++++++++++++++++
28bab8
 2 files changed, 43 insertions(+)
28bab8
28bab8
diff --git a/src/include/ureport.h b/src/include/ureport.h
28bab8
index 780b898..a1d03f6 100644
28bab8
--- a/src/include/ureport.h
28bab8
+++ b/src/include/ureport.h
28bab8
@@ -52,6 +52,7 @@ struct ureport_server_config
28bab8
     char *ur_client_cert; ///< Path to certificate used for client
28bab8
                           ///< authentication (or NULL)
28bab8
     char *ur_client_key;  ///< Private key for the certificate
28bab8
+    char *ur_cert_authority_cert; ///< Certificate authority certificate
28bab8
     char *ur_username;    ///< username for basic HTTP auth
28bab8
     char *ur_password;    ///< password for basic HTTP auth
28bab8
     map_string_t *ur_http_headers; ///< Additional HTTP headers
28bab8
diff --git a/src/lib/ureport.c b/src/lib/ureport.c
28bab8
index 990ace6..76bcc95 100644
28bab8
--- a/src/lib/ureport.c
28bab8
+++ b/src/lib/ureport.c
28bab8
@@ -37,6 +37,12 @@
28bab8
 #define RHSMCON_CERT_NAME "cert.pem"
28bab8
 #define RHSMCON_KEY_NAME "key.pem"
28bab8
 
28bab8
+/* Using the same template as for RHSM certificate, macro for cert dir path and
28bab8
+ * macro for cert name. Cert path can be easily modified for example by reading
28bab8
+ * an environment variable LIBREPORT_DEBUG_AUTHORITY_CERT_DIR_PATH
28bab8
+ */
28bab8
+#define CERT_AUTHORITY_CERT_PATH "/etc/redhat-access-insights"
28bab8
+#define CERT_AUTHORITY_CERT_NAME "cert-api.access.redhat.com.pem"
28bab8
 
28bab8
 static char *
28bab8
 puppet_config_print(const char *key)
28bab8
@@ -106,6 +112,17 @@ certificate_exist(char *cert_name)
28bab8
     return true;
28bab8
 }
28bab8
 
28bab8
+static bool
28bab8
+cert_authority_cert_exist(char *cert_name)
28bab8
+{
28bab8
+    if (access(cert_name, F_OK) != 0)
28bab8
+    {
28bab8
+        log_notice("Certs validating the server '%s' does not exist.", cert_name);
28bab8
+        return false;
28bab8
+    }
28bab8
+    return true;
28bab8
+}
28bab8
+
28bab8
 void
28bab8
 ureport_server_config_set_client_auth(struct ureport_server_config *config,
28bab8
                                       const char *client_auth)
28bab8
@@ -134,6 +151,16 @@ ureport_server_config_set_client_auth(struct ureport_server_config *config,
28bab8
         char *cert_full_name = concat_path_file(rhsm_dir, RHSMCON_CERT_NAME);
28bab8
         char *key_full_name = concat_path_file(rhsm_dir, RHSMCON_KEY_NAME);
28bab8
 
28bab8
+        /* get authority certificate dir path from environment variable, if it
28bab8
+         * is not set, use CERT_AUTHORITY_CERT_PATH
28bab8
+         */
28bab8
+        const char *authority_cert_dir_path = getenv("LIBREPORT_DEBUG_AUTHORITY_CERT_DIR_PATH");
28bab8
+        if (authority_cert_dir_path == NULL)
28bab8
+           authority_cert_dir_path = CERT_AUTHORITY_CERT_PATH;
28bab8
+
28bab8
+        char *cert_authority_cert_full_name = concat_path_file(authority_cert_dir_path,
28bab8
+                                                                 CERT_AUTHORITY_CERT_NAME);
28bab8
+
28bab8
         if (certificate_exist(cert_full_name) && certificate_exist(key_full_name))
28bab8
         {
28bab8
             config->ur_client_cert = cert_full_name;
28bab8
@@ -147,6 +174,16 @@ ureport_server_config_set_client_auth(struct ureport_server_config *config,
28bab8
             log_notice("Using the default configuration for uReports.");
28bab8
         }
28bab8
 
28bab8
+        if (cert_authority_cert_exist(cert_authority_cert_full_name))
28bab8
+        {
28bab8
+            config->ur_cert_authority_cert = cert_authority_cert_full_name;
28bab8
+            log_debug("Using validating server cert: '%s'", config->ur_cert_authority_cert);
28bab8
+        }
28bab8
+        else
28bab8
+        {
28bab8
+            free(cert_authority_cert_full_name);
28bab8
+        }
28bab8
+
28bab8
         free(rhsm_dir);
28bab8
 
28bab8
     }
28bab8
@@ -286,6 +323,7 @@ ureport_server_config_init(struct ureport_server_config *config)
28bab8
     config->ur_ssl_verify = true;
28bab8
     config->ur_client_cert = NULL;
28bab8
     config->ur_client_key = NULL;
28bab8
+    config->ur_cert_authority_cert = NULL;
28bab8
     config->ur_username = NULL;
28bab8
     config->ur_password = NULL;
28bab8
     config->ur_http_headers = new_map_string();
28bab8
@@ -304,6 +342,9 @@ ureport_server_config_destroy(struct ureport_server_config *config)
28bab8
     free(config->ur_client_key);
28bab8
     config->ur_client_key = DESTROYED_POINTER;
28bab8
 
28bab8
+    free(config->ur_cert_authority_cert);
28bab8
+    config->ur_cert_authority_cert = DESTROYED_POINTER;
28bab8
+
28bab8
     free(config->ur_username);
28bab8
     config->ur_username = DESTROYED_POINTER;
28bab8
 
28bab8
@@ -701,6 +742,7 @@ ureport_do_post(const char *json, struct ureport_server_config *config,
28bab8
     {
28bab8
         post_state->client_cert_path = config->ur_client_cert;
28bab8
         post_state->client_key_path = config->ur_client_key;
28bab8
+        post_state->cert_authority_cert_path = config->ur_cert_authority_cert;
28bab8
     }
28bab8
     else if (config->ur_username && config->ur_password)
28bab8
     {
28bab8
-- 
28bab8
2.4.3
28bab8