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

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