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

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