Blame SOURCES/0067-ureport-use-additional-HTTP-headers-with-rhsm-entitl.patch

28bab8
From 4f61f5d9eec75e471c8176dac0c21f4361e40ee6 Mon Sep 17 00:00:00 2001
28bab8
From: Jakub Filak <jfilak@redhat.com>
28bab8
Date: Thu, 11 Sep 2014 12:33:05 +0200
28bab8
Subject: [LIBREPORT PATCH 67/93] ureport: use additional HTTP headers with
28bab8
 'rhsm-entitlement' cert auth
28bab8
28bab8
Related to rhbz#1140224
28bab8
28bab8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
28bab8
28bab8
Conflicts:
28bab8
	src/plugins/ureport.conf
28bab8
---
28bab8
 src/lib/json.c           | 29 +++++++++++----
28bab8
 src/lib/ureport.h        |  1 +
28bab8
 src/plugins/ureport.c    | 94 +++++++++++++++++++++++++++++++++++++++++++++++-
28bab8
 src/plugins/ureport.conf |  4 ++-
28bab8
 4 files changed, 119 insertions(+), 9 deletions(-)
28bab8
28bab8
diff --git a/src/lib/json.c b/src/lib/json.c
28bab8
index 8935ef8..6fbbf39 100644
28bab8
--- a/src/lib/json.c
28bab8
+++ b/src/lib/json.c
28bab8
@@ -113,14 +113,25 @@ struct post_state *post_ureport(const char *json, struct ureport_server_config *
28bab8
         post_state->client_key_path = config->ur_client_key;
28bab8
     }
28bab8
 
28bab8
-    static const char *headers[] = {
28bab8
-        "Accept: application/json",
28bab8
-        "Connection: close",
28bab8
-        NULL,
28bab8
-    };
28bab8
+    char **headers = xmalloc(sizeof(char *) * (3 + size_map_string(config->ur_http_headers)));
28bab8
+    headers[0] = (char *)"Accept: application/json";
28bab8
+    headers[1] = (char *)"Connection: close";
28bab8
+    headers[2] = NULL;
28bab8
+
28bab8
+    if (config->ur_http_headers != NULL)
28bab8
+    {
28bab8
+        unsigned i = 2;
28bab8
+        const char *header;
28bab8
+        const char *value;
28bab8
+        map_string_iter_t iter;
28bab8
+        init_map_string_iter(&iter, config->ur_http_headers);
28bab8
+        while (next_map_string_iter(&iter, &header, &value))
28bab8
+            headers[i++] = xasprintf("%s: %s", header, value);
28bab8
+        headers[i] = NULL;
28bab8
+    }
28bab8
 
28bab8
     post_string_as_form_data(post_state, config->ur_url, "application/json",
28bab8
-                     headers, json);
28bab8
+                    (const char **)headers, json);
28bab8
 
28bab8
     /* Client authentication failed. Try again without client auth.
28bab8
      * CURLE_SSL_CONNECT_ERROR - cert not found/server doesnt trust the CA
28bab8
@@ -135,10 +146,14 @@ struct post_state *post_ureport(const char *json, struct ureport_server_config *
28bab8
         post_state = new_post_state(flags);
28bab8
 
28bab8
         post_string_as_form_data(post_state, config->ur_url, "application/json",
28bab8
-                         headers, json);
28bab8
+                         (const char **)headers, json);
28bab8
 
28bab8
     }
28bab8
 
28bab8
+    for (unsigned i = size_map_string(config->ur_http_headers); i != 0; --i)
28bab8
+        free(headers[i + 1]);
28bab8
+    free(headers);
28bab8
+
28bab8
     return post_state;
28bab8
 }
28bab8
 
28bab8
diff --git a/src/lib/ureport.h b/src/lib/ureport.h
28bab8
index ca1d538..319aca9 100644
28bab8
--- a/src/lib/ureport.h
28bab8
+++ b/src/lib/ureport.h
28bab8
@@ -43,6 +43,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
+    map_string_t *ur_http_headers; ///< Additional HTTP headers
28bab8
 
28bab8
     struct ureport_preferences ur_prefs; ///< configuration for uReport generation
28bab8
 };
28bab8
diff --git a/src/plugins/ureport.c b/src/plugins/ureport.c
28bab8
index d23cc79..9c69cad 100644
28bab8
--- a/src/plugins/ureport.c
28bab8
+++ b/src/plugins/ureport.c
28bab8
@@ -31,6 +31,12 @@
28bab8
 #define RHSM_CERT_PATH "/etc/pki/consumer/cert.pem"
28bab8
 #define RHSM_KEY_PATH "/etc/pki/consumer/key.pem"
28bab8
 
28bab8
+#define RHAP_PEM_DIR_PATH "/etc/pki/entitlement"
28bab8
+#define RHAP_ENT_DATA_BEGIN_TAG "-----BEGIN ENTITLEMENT DATA-----"
28bab8
+#define RHAP_ENT_DATA_END_TAG "-----END ENTITLEMENT DATA-----"
28bab8
+#define RHAP_SIG_DATA_BEGIN_TAG "-----BEGIN RSA SIGNATURE-----"
28bab8
+#define RHAP_SIG_DATA_END_TAG "-----END RSA SIGNATURE-----"
28bab8
+
28bab8
 #define VALUE_FROM_CONF(opt, var, tr) do { const char *value = getenv("uReport_"opt); \
28bab8
         if (!value) { value = get_map_string_item_or_NULL(settings, opt); } if (value) { var = tr(value); } \
28bab8
     } while(0)
28bab8
@@ -72,6 +78,88 @@ static void parse_client_auth_paths(struct ureport_server_config *config, const
28bab8
         config->ur_client_cert = xstrdup(RHSM_CERT_PATH);
28bab8
         config->ur_client_key = xstrdup(RHSM_KEY_PATH);
28bab8
     }
28bab8
+    else if (strcmp(client_auth, "rhsm-entitlement") == 0)
28bab8
+    {
28bab8
+        GList *certs = get_file_list(RHAP_PEM_DIR_PATH, "pem");
28bab8
+        if (g_list_length(certs) != 2)
28bab8
+        {
28bab8
+            log_notice(RHAP_PEM_DIR_PATH" does not contain unique cert-key files pair");
28bab8
+            log_notice("Not using client authentication");
28bab8
+            return;
28bab8
+        }
28bab8
+
28bab8
+        const char *cert = NULL;
28bab8
+        const char *key = NULL;
28bab8
+
28bab8
+        file_obj_t *fst = (file_obj_t *)certs->data;
28bab8
+        file_obj_t *scn = (file_obj_t *)certs->next->data;
28bab8
+
28bab8
+        if (strlen(fo_get_filename(fst)) < strlen(fo_get_filename(scn)))
28bab8
+        {
28bab8
+            cert = fo_get_filename(fst);
28bab8
+            key = fo_get_filename(scn);
28bab8
+
28bab8
+            config->ur_client_cert = xstrdup(fo_get_fullpath(fst));
28bab8
+            config->ur_client_key = xstrdup(fo_get_fullpath(scn));
28bab8
+        }
28bab8
+        else
28bab8
+        {
28bab8
+            cert = fo_get_filename(scn);
28bab8
+            key = fo_get_filename(fst);
28bab8
+
28bab8
+            config->ur_client_cert = xstrdup(fo_get_fullpath(scn));
28bab8
+            config->ur_client_key = xstrdup(fo_get_fullpath(fst));
28bab8
+        }
28bab8
+
28bab8
+        const bool iscomplement = prefixcmp(key, cert) != 0 || strcmp("-key", key + strlen(cert)) != 0;
28bab8
+        g_list_free_full(certs, (GDestroyNotify)free_file_obj);
28bab8
+
28bab8
+        if (iscomplement)
28bab8
+        {
28bab8
+            log_notice("Key file '%s' isn't complement to cert file '%s'",
28bab8
+                    config->ur_client_key, config->ur_client_cert);
28bab8
+            log_notice("Not using client authentication");
28bab8
+
28bab8
+            free(config->ur_client_cert);
28bab8
+            free(config->ur_client_key);
28bab8
+            config->ur_client_cert = NULL;
28bab8
+            config->ur_client_key = NULL;
28bab8
+
28bab8
+            return;
28bab8
+        }
28bab8
+
28bab8
+        char *certdata = xmalloc_open_read_close(config->ur_client_cert, /*no size limit*/NULL);
28bab8
+        if (certdata != NULL)
28bab8
+        {
28bab8
+            char *ent_data = xstrdup_between(certdata,
28bab8
+                    RHAP_ENT_DATA_BEGIN_TAG, RHAP_ENT_DATA_END_TAG);
28bab8
+
28bab8
+            char *sig_data = xstrdup_between(certdata,
28bab8
+                    RHAP_SIG_DATA_BEGIN_TAG, RHAP_SIG_DATA_END_TAG);
28bab8
+
28bab8
+            if (ent_data != NULL && sig_data != NULL)
28bab8
+            {
28bab8
+                ent_data = strremovech(ent_data, '\n');
28bab8
+                insert_map_string(config->ur_http_headers,
28bab8
+                        xstrdup("X-RH-Entitlement-Data"),
28bab8
+                        xasprintf(RHAP_ENT_DATA_BEGIN_TAG"%s"RHAP_ENT_DATA_END_TAG, ent_data));
28bab8
+
28bab8
+                sig_data = strremovech(sig_data, '\n');
28bab8
+                insert_map_string(config->ur_http_headers,
28bab8
+                        xstrdup("X-RH-Entitlement-Sig"),
28bab8
+                        xasprintf(RHAP_SIG_DATA_BEGIN_TAG"%s"RHAP_SIG_DATA_END_TAG, sig_data));
28bab8
+            }
28bab8
+            else
28bab8
+            {
28bab8
+                log_notice("Cert file '%s' doesn't contain Entitlement and RSA Signature sections", config->ur_client_cert);
28bab8
+                log_notice("Not using HTTP authentication headers");
28bab8
+            }
28bab8
+
28bab8
+            free(sig_data);
28bab8
+            free(ent_data);
28bab8
+            free(certdata);
28bab8
+        }
28bab8
+    }
28bab8
     else if (strcmp(client_auth, "puppet") == 0)
28bab8
     {
28bab8
         config->ur_client_cert = puppet_config_print("hostcert");
28bab8
@@ -83,7 +171,6 @@ static void parse_client_auth_paths(struct ureport_server_config *config, const
28bab8
         config->ur_client_cert = xstrdup(strtok(scratch, ":"));
28bab8
         config->ur_client_key = xstrdup(strtok(NULL, ":"));
28bab8
         free(scratch);
28bab8
-
28bab8
         if (config->ur_client_cert == NULL || config->ur_client_key == NULL)
28bab8
             error_msg_and_die("Invalid client authentication specification");
28bab8
     }
28bab8
@@ -426,11 +513,14 @@ int main(int argc, char **argv)
28bab8
         .ur_ssl_verify = true,
28bab8
         .ur_client_cert = NULL,
28bab8
         .ur_client_key = NULL,
28bab8
+        .ur_http_headers = NULL,
28bab8
         {
28bab8
             .urp_auth_items = NULL,
28bab8
         },
28bab8
     };
28bab8
 
28bab8
+    config.ur_http_headers = new_map_string();
28bab8
+
28bab8
     enum {
28bab8
         OPT_v = 1 << 0,
28bab8
         OPT_d = 1 << 1,
28bab8
@@ -677,6 +767,8 @@ finalize:
28bab8
     if (config.ur_prefs.urp_auth_items != auth_items)
28bab8
         g_list_free_full(config.ur_prefs.urp_auth_items, free);
28bab8
 
28bab8
+    free_map_string(config.ur_http_headers);
28bab8
+
28bab8
     free_map_string(settings);
28bab8
     free(config.ur_client_cert);
28bab8
     free(config.ur_client_key);
28bab8
diff --git a/src/plugins/ureport.conf b/src/plugins/ureport.conf
28bab8
index 8abeb26..fc0dc21 100644
28bab8
--- a/src/plugins/ureport.conf
28bab8
+++ b/src/plugins/ureport.conf
28bab8
@@ -19,7 +19,9 @@ AuthDataItems = hostname, machineid
28bab8
 # None (default):
28bab8
 # SSLClientAuth =
28bab8
 # Using RH subscription management certificate:
28bab8
-SSLClientAuth = rhsm
28bab8
+# SSLClientAuth = rhsm
28bab8
+# Using RH subscription management entitlement certificate:
28bab8
+# SSLClientAuth = rhsm-entitlement
28bab8
 # Using Puppet certificate:
28bab8
 # SSLClientAuth = puppet
28bab8
 # Using custom certificate:
28bab8
-- 
28bab8
1.8.3.1
28bab8