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

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