Blame SOURCES/0012-ureport-add-support-for-client-side-authentication.patch

28bab8
From c0e4f8f27f0becd93c7abd9f20224232d5f1a5cf Mon Sep 17 00:00:00 2001
28bab8
From: Martin Milata <mmilata@redhat.com>
28bab8
Date: Thu, 16 Jan 2014 20:02:05 +0100
28bab8
Subject: [LIBREPORT PATCH 12/14] ureport: add support for client-side
28bab8
 authentication
28bab8
28bab8
Please note that the libreport_curl api is changed and since we're not
28bab8
bumping sonames ABRT has to explicitly depend on this version in spec.
28bab8
28bab8
Related to rhbz#1053042.
28bab8
28bab8
Signed-off-by: Martin Milata <mmilata@redhat.com>
28bab8
---
28bab8
 doc/reporter-ureport.txt     | 18 ++++++++++
28bab8
 src/include/libreport_curl.h |  2 ++
28bab8
 src/lib/curl.c               |  7 ++++
28bab8
 src/lib/json.c               | 48 ++++++++++++-------------
28bab8
 src/lib/ureport.h            |  7 ++--
28bab8
 src/plugins/ureport.c        | 85 ++++++++++++++++++++++++++++++++++++++++++--
28bab8
 src/plugins/ureport.conf     | 10 ++++++
28bab8
 7 files changed, 149 insertions(+), 28 deletions(-)
28bab8
28bab8
diff --git a/doc/reporter-ureport.txt b/doc/reporter-ureport.txt
28bab8
index b739b6d..54823ae 100644
28bab8
--- a/doc/reporter-ureport.txt
28bab8
+++ b/doc/reporter-ureport.txt
28bab8
@@ -27,6 +27,20 @@ Configuration file lines should have 'PARAM = VALUE' format. The parameters are:
28bab8
 'SSLVerify'::
28bab8
    Use no/false/off/0 to disable verification of server's SSL certificate. (default: yes)
28bab8
 
28bab8
+'SSLClientAuth'::
28bab8
+   If this option is set, client-side SSL certificate is used to authenticate
28bab8
+   to the server so that it knows which machine it came from. Possible values
28bab8
+   are:
28bab8
+
28bab8
+   'rhsm';;
28bab8
+      Uses the system certificate that is used for Red Hat subscription management.
28bab8
+
28bab8
+   'puppet';;
28bab8
+      Uses the certificate that is used by the Puppet configuration management tool.
28bab8
+
28bab8
+   '<cert_path>:<key_path>';;
28bab8
+      Manually supply paths to certificate and the corresponding key in PEM format.
28bab8
+
28bab8
 'ContactEmail'::
28bab8
    Email address attached to a bthash on the server.
28bab8
 
28bab8
@@ -61,6 +75,10 @@ OPTIONS
28bab8
 -k, --insecure::
28bab8
    Allow insecure connection to ureport server
28bab8
 
28bab8
+-t, --auth SOURCE::
28bab8
+   Enables client authentication. See 'SSLClientAuth' configuration file
28bab8
+   option for list of possible values.
28bab8
+
28bab8
 -v::
28bab8
    Be more verbose. Can be given multiple times.
28bab8
 
28bab8
diff --git a/src/include/libreport_curl.h b/src/include/libreport_curl.h
28bab8
index 4cd855f..7d6fa02 100644
28bab8
--- a/src/include/libreport_curl.h
28bab8
+++ b/src/include/libreport_curl.h
28bab8
@@ -35,6 +35,8 @@ typedef struct post_state {
28bab8
     int         flags;
28bab8
     const char  *username;
28bab8
     const char  *password;
28bab8
+    const char  *client_cert_path;
28bab8
+    const char  *client_key_path;
28bab8
     /* Results of POST transaction: */
28bab8
     int         http_resp_code;
28bab8
     /* cast from CURLcode enum.
28bab8
diff --git a/src/lib/curl.c b/src/lib/curl.c
28bab8
index 6722b4a..662a2cf 100644
28bab8
--- a/src/lib/curl.c
28bab8
+++ b/src/lib/curl.c
28bab8
@@ -532,6 +532,13 @@ post(post_state_t *state,
28bab8
         xcurl_easy_setopt_long(handle, CURLOPT_SSL_VERIFYPEER, 0);
28bab8
         xcurl_easy_setopt_long(handle, CURLOPT_SSL_VERIFYHOST, 0);
28bab8
     }
28bab8
+    if (state->client_cert_path && state->client_key_path)
28bab8
+    {
28bab8
+        xcurl_easy_setopt_ptr(handle, CURLOPT_SSLCERTTYPE, "PEM");
28bab8
+        xcurl_easy_setopt_ptr(handle, CURLOPT_SSLKEYTYPE, "PEM");
28bab8
+        xcurl_easy_setopt_ptr(handle, CURLOPT_SSLCERT, state->client_cert_path);
28bab8
+        xcurl_easy_setopt_ptr(handle, CURLOPT_SSLKEY, state->client_key_path);
28bab8
+    }
28bab8
 
28bab8
     // This is the place where everything happens.
28bab8
     // Here errors are not limited to "out of memory", can't just die.
28bab8
diff --git a/src/lib/json.c b/src/lib/json.c
28bab8
index eb8e5ed..66db537 100644
28bab8
--- a/src/lib/json.c
28bab8
+++ b/src/lib/json.c
28bab8
@@ -68,7 +68,7 @@ char *new_json_attachment(const char *bthash, const char *type, const char *data
28bab8
     return result;
28bab8
 }
28bab8
 
28bab8
-struct post_state *post_ureport(const char *json_ureport, struct ureport_server_config *config)
28bab8
+struct post_state *post_ureport(const char *json, struct ureport_server_config *config)
28bab8
 {
28bab8
     int flags = POST_WANT_BODY | POST_WANT_ERROR_MSG;
28bab8
 
28bab8
@@ -77,6 +77,12 @@ struct post_state *post_ureport(const char *json_ureport, struct ureport_server_
28bab8
 
28bab8
     struct post_state *post_state = new_post_state(flags);
28bab8
 
28bab8
+    if (config->ur_client_cert && config->ur_client_key)
28bab8
+    {
28bab8
+        post_state->client_cert_path = config->ur_client_cert;
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
@@ -84,30 +90,24 @@ struct post_state *post_ureport(const char *json_ureport, struct ureport_server_
28bab8
     };
28bab8
 
28bab8
     post_string_as_form_data(post_state, config->ur_url, "application/json",
28bab8
-                     headers, json_ureport);
28bab8
+                     headers, json);
28bab8
 
28bab8
-    return post_state;
28bab8
-}
28bab8
+    /* Client authentication failed. Try again without client auth.
28bab8
+     * CURLE_SSL_CONNECT_ERROR - cert not found/server doesnt trust the CA
28bab8
+     * CURLE_SSL_CERTPROBLEM - malformed certificate/no permission
28bab8
+     */
28bab8
+    if ((post_state->curl_result == CURLE_SSL_CONNECT_ERROR
28bab8
+         || post_state->curl_result == CURLE_SSL_CERTPROBLEM)
28bab8
+            && config->ur_client_cert && config->ur_client_key)
28bab8
+    {
28bab8
+        warn_msg("Authentication failed. Retrying unauthenticated.");
28bab8
+        free_post_state(post_state);
28bab8
+        post_state = new_post_state(flags);
28bab8
 
28bab8
-static
28bab8
-struct post_state *ureport_attach(const char *json_attachment,
28bab8
-                                  struct ureport_server_config *config)
28bab8
-{
28bab8
-    int flags = POST_WANT_BODY | POST_WANT_ERROR_MSG;
28bab8
+        post_string_as_form_data(post_state, config->ur_url, "application/json",
28bab8
+                         headers, json);
28bab8
 
28bab8
-    if (config->ur_ssl_verify)
28bab8
-        flags |= POST_WANT_SSL_VERIFY;
28bab8
-
28bab8
-    struct post_state *post_state = new_post_state(flags);
28bab8
-
28bab8
-    static const char *headers[] = {
28bab8
-        "Accept: application/json",
28bab8
-        "Connection: close",
28bab8
-        NULL,
28bab8
-    };
28bab8
-
28bab8
-    post_string_as_form_data(post_state, config->ur_url, "application/json",
28bab8
-                             headers, json_attachment);
28bab8
+    }
28bab8
 
28bab8
     return post_state;
28bab8
 }
28bab8
@@ -117,7 +117,7 @@ struct post_state *ureport_attach_rhbz(const char *bthash, int rhbz_bug_id,
28bab8
 {
28bab8
     char *str_bug_id = xasprintf("%d", rhbz_bug_id);
28bab8
     char *json_attachment = new_json_attachment(bthash, "RHBZ", str_bug_id);
28bab8
-    struct post_state *post_state = ureport_attach(json_attachment, config);
28bab8
+    struct post_state *post_state = post_ureport(json_attachment, config);
28bab8
     free(str_bug_id);
28bab8
     free(json_attachment);
28bab8
 
28bab8
@@ -128,7 +128,7 @@ struct post_state *ureport_attach_email(const char *bthash, const char *email,
28bab8
                                        struct ureport_server_config *config)
28bab8
 {
28bab8
     char *json_attachment = new_json_attachment(bthash, "email", email);
28bab8
-    struct post_state *post_state = ureport_attach(json_attachment, config);
28bab8
+    struct post_state *post_state = post_ureport(json_attachment, config);
28bab8
     free(json_attachment);
28bab8
 
28bab8
     return post_state;
28bab8
diff --git a/src/lib/ureport.h b/src/lib/ureport.h
28bab8
index 4cc4e10..16f40f1 100644
28bab8
--- a/src/lib/ureport.h
28bab8
+++ b/src/lib/ureport.h
28bab8
@@ -30,8 +30,11 @@ extern "C" {
28bab8
  */
28bab8
 struct ureport_server_config
28bab8
 {
28bab8
-    const char *ur_url; ///< Web service URL
28bab8
-    bool ur_ssl_verify; ///< Verify HOST and PEER certificates
28bab8
+    const char *ur_url;   ///< Web service URL
28bab8
+    bool ur_ssl_verify;   ///< Verify HOST and PEER certificates
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
 };
28bab8
 
28bab8
 struct abrt_post_state;
28bab8
diff --git a/src/plugins/ureport.c b/src/plugins/ureport.c
28bab8
index 0168744..b57eada 100644
28bab8
--- a/src/plugins/ureport.c
28bab8
+++ b/src/plugins/ureport.c
28bab8
@@ -28,10 +28,73 @@
28bab8
 #define ATTACH_URL_SFX "reports/attach/"
28bab8
 #define BTHASH_URL_SFX "reports/bthash/"
28bab8
 
28bab8
+#define RHSM_CERT_PATH "/etc/pki/consumer/cert.pem"
28bab8
+#define RHSM_KEY_PATH "/etc/pki/consumer/key.pem"
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
 
28bab8
+static char *puppet_config_print(const char *key)
28bab8
+{
28bab8
+    char *command = xasprintf("puppet config print %s", key);
28bab8
+    char *result = run_in_shell_and_save_output(0, command, NULL, NULL);
28bab8
+    free(command);
28bab8
+
28bab8
+    /* run_in_shell_and_save_output always returns non-NULL */
28bab8
+    if (result[0] != '/')
28bab8
+        goto error;
28bab8
+
28bab8
+    char *newline = strchrnul(result, '\n');
28bab8
+    if (!newline)
28bab8
+        goto error;
28bab8
+
28bab8
+    *newline = '\0';
28bab8
+    return result;
28bab8
+error:
28bab8
+    free(result);
28bab8
+    error_msg_and_die("Unable to determine puppet %s path (puppet not installed?)", key);
28bab8
+}
28bab8
+
28bab8
+static void parse_client_auth_paths(struct ureport_server_config *config, const char *client_auth)
28bab8
+{
28bab8
+    if (client_auth == NULL)
28bab8
+        return;
28bab8
+
28bab8
+    if (strcmp(client_auth, "") == 0)
28bab8
+    {
28bab8
+        config->ur_client_cert = NULL;
28bab8
+        config->ur_client_key = NULL;
28bab8
+        log_notice("Not using client authentication");
28bab8
+    }
28bab8
+    else if (strcmp(client_auth, "rhsm") == 0)
28bab8
+    {
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, "puppet") == 0)
28bab8
+    {
28bab8
+        config->ur_client_cert = puppet_config_print("hostcert");
28bab8
+        config->ur_client_key = puppet_config_print("hostprivkey");
28bab8
+    }
28bab8
+    else
28bab8
+    {
28bab8
+        char *scratch = xstrdup(client_auth);
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
+
28bab8
+    if (config->ur_client_cert && config->ur_client_key)
28bab8
+    {
28bab8
+        log_notice("Using client certificate: %s", config->ur_client_cert);
28bab8
+        log_notice("Using client private key: %s", config->ur_client_key);
28bab8
+    }
28bab8
+}
28bab8
+
28bab8
 /*
28bab8
  * Loads uReport configuration from various sources.
28bab8
  *
28bab8
@@ -44,6 +107,10 @@ static void load_ureport_server_config(struct ureport_server_config *config, map
28bab8
 {
28bab8
     VALUE_FROM_CONF("URL", config->ur_url, (const char *));
28bab8
     VALUE_FROM_CONF("SSLVerify", config->ur_ssl_verify, string_to_bool);
28bab8
+
28bab8
+    const char *client_auth = NULL;
28bab8
+    VALUE_FROM_CONF("SSLClientAuth", client_auth, (const char *));
28bab8
+    parse_client_auth_paths(config, client_auth);
28bab8
 }
28bab8
 
28bab8
 struct ureport_server_response {
28bab8
@@ -243,7 +310,12 @@ static struct ureport_server_response *ureport_server_parse_json(json_object *js
28bab8
 
28bab8
 static struct ureport_server_response *get_server_response(post_state_t *post_state, struct ureport_server_config *config)
28bab8
 {
28bab8
-    if (post_state->errmsg[0] != '\0')
28bab8
+    /* Previously, the condition here was (post_state->errmsg[0] != '\0')
28bab8
+     * however when the server asks for optional client authentication and we do not have the certificates,
28bab8
+     * then post_state->errmsg contains "NSS: client certificate not found (nickname not specified)" even though
28bab8
+     * the request succeeded.
28bab8
+     */
28bab8
+    if (post_state->curl_result != CURLE_OK)
28bab8
     {
28bab8
         error_msg(_("Failed to upload uReport to the server '%s' with curl: %s"), config->ur_url, post_state->errmsg);
28bab8
         return NULL;
28bab8
@@ -349,6 +421,8 @@ int main(int argc, char **argv)
28bab8
     struct ureport_server_config config = {
28bab8
         .ur_url = NULL,
28bab8
         .ur_ssl_verify = true,
28bab8
+        .ur_client_cert = NULL,
28bab8
+        .ur_client_key = NULL,
28bab8
     };
28bab8
 
28bab8
     enum {
28bab8
@@ -356,12 +430,14 @@ int main(int argc, char **argv)
28bab8
         OPT_d = 1 << 1,
28bab8
         OPT_u = 1 << 2,
28bab8
         OPT_k = 1 << 3,
28bab8
+        OPT_t = 1 << 4,
28bab8
     };
28bab8
 
28bab8
     int ret = 1; /* "failure" (for now) */
28bab8
     bool insecure = !config.ur_ssl_verify;
28bab8
     const char *conf_file = CONF_FILE_PATH;
28bab8
     const char *arg_server_url = NULL;
28bab8
+    const char *client_auth = NULL;
28bab8
     const char *dump_dir_path = ".";
28bab8
     const char *ureport_hash = NULL;
28bab8
     bool ureport_hash_from_rt = false;
28bab8
@@ -376,6 +452,7 @@ int main(int argc, char **argv)
28bab8
         OPT_STRING('u', "url", &arg_server_url, "URL", _("Specify server URL")),
28bab8
         OPT_BOOL('k', "insecure", &insecure,
28bab8
                           _("Allow insecure connection to ureport server")),
28bab8
+        OPT_STRING('t', "auth", &client_auth, "SOURCE", _("Use client authentication")),
28bab8
         OPT_STRING('c', NULL, &conf_file, "FILE", _("Configuration file")),
28bab8
         OPT_STRING('a', "attach", &ureport_hash, "BTHASH",
28bab8
                           _("bthash of uReport to attach (conflicts with -A)")),
28bab8
@@ -393,7 +470,7 @@ int main(int argc, char **argv)
28bab8
     };
28bab8
 
28bab8
     const char *program_usage_string = _(
28bab8
-        "& [-v] [-c FILE] [-u URL] [-k] [-A -a bthash -B -b bug-id -E -e email] [-d DIR]\n"
28bab8
+        "& [-v] [-c FILE] [-u URL] [-k] [-t SOURCE] [-A -a bthash -B -b bug-id -E -e email] [-d DIR]\n"
28bab8
         "\n"
28bab8
         "Upload micro report or add an attachment to a micro report\n"
28bab8
         "\n"
28bab8
@@ -411,6 +488,8 @@ int main(int argc, char **argv)
28bab8
         config.ur_url = arg_server_url;
28bab8
     if (opts & OPT_k)
28bab8
         config.ur_ssl_verify = !insecure;
28bab8
+    if (opts & OPT_t)
28bab8
+        parse_client_auth_paths(&config, client_auth);
28bab8
 
28bab8
     if (!config.ur_url)
28bab8
         error_msg_and_die("You need to specify server URL");
28bab8
@@ -580,6 +659,8 @@ format_err:
28bab8
 
28bab8
 finalize:
28bab8
     free_map_string(settings);
28bab8
+    free(config.ur_client_cert);
28bab8
+    free(config.ur_client_key);
28bab8
 
28bab8
     return ret;
28bab8
 }
28bab8
diff --git a/src/plugins/ureport.conf b/src/plugins/ureport.conf
28bab8
index 1f3b33a..13b6386 100644
28bab8
--- a/src/plugins/ureport.conf
28bab8
+++ b/src/plugins/ureport.conf
28bab8
@@ -6,3 +6,13 @@ URL = http://bug-report.itos.redhat.com
28bab8
 
28bab8
 # Contact email attached to an uploaded uReport if required
28bab8
 # ContactEmail = foo@example.com
28bab8
+
28bab8
+# Client-side authentication
28bab8
+# None (default):
28bab8
+# SSLClientAuth =
28bab8
+# Using RH subscription management certificate:
28bab8
+# SSLClientAuth = rhsm
28bab8
+# Using Puppet certificate:
28bab8
+# SSLClientAuth = puppet
28bab8
+# Using custom certificate:
28bab8
+# SSLClientAuth = /path/to/cert.pem:/path/to/key.pem
28bab8
-- 
28bab8
1.8.3.1
28bab8