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

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