Blame SOURCES/0124-ureport-introduce-HTTPAuth.patch

562801
From bedb4f5a00ceaff0d55ecfe81ada9d0e983ca347 Mon Sep 17 00:00:00 2001
562801
From: Jakub Filak <jfilak@redhat.com>
562801
Date: Fri, 19 Dec 2014 00:19:34 +0100
562801
Subject: [LIBREPORT PATCH 124/124] ureport: introduce HTTPAuth
562801
562801
Read HTTP Basic Authentication credentials from the configuration file.
562801
562801
HTTPAuth has priority to SSLClientAuth. When both are set to some value
562801
the latter is ignored.
562801
562801
HTTPAuth configuration option values:
562801
 - "rhts-credentials" : a place holder for Login= and Password= from
562801
                        rhtsupport.conf
562801
 - "<username>:<password>"
562801
 - "<username>" : a prompt will be issue for password (export
562801
                  REPORT_CLIENT_NONINTERACTIVE=1 env variable to tell
562801
                  libreport that it must not wait for user input).
562801
562801
Related: #1140224
562801
562801
Signed-off-by: Jakub Filak <jfilak@redhat.com>
562801
---
562801
 doc/reporter-ureport.txt                       |  17 +++
562801
 src/include/ureport.h                          |  17 +++
562801
 src/lib/ureport.c                              |  68 +++++++++++-
562801
 src/plugins/reporter-ureport.c                 |  11 +-
562801
 src/plugins/ureport.conf                       |  10 +-
562801
 tests/Makefile.am                              |   2 +-
562801
 tests/ureport-rhts-credentials/rhtsupport.conf |   2 +
562801
 tests/ureport.at                               | 145 +++++++++++++++++++++++++
562801
 8 files changed, 264 insertions(+), 8 deletions(-)
562801
 create mode 100644 tests/ureport-rhts-credentials/rhtsupport.conf
562801
562801
diff --git a/doc/reporter-ureport.txt b/doc/reporter-ureport.txt
562801
index 1a67441..420adcf 100644
562801
--- a/doc/reporter-ureport.txt
562801
+++ b/doc/reporter-ureport.txt
562801
@@ -44,6 +44,19 @@ Configuration file lines should have 'PARAM = VALUE' format. The parameters are:
562801
    '<cert_path>:<key_path>';;
562801
       Manually supply paths to certificate and the corresponding key in PEM format.
562801
 
562801
+'HTTPAuth'::
562801
+   Use the configured values to as HTTP Basic Authentication credentials.
562801
+   Assigning any value to this option changes the default value of
562801
+   IncludeAuthData to yes.
562801
+
562801
+   Possible values are::
562801
+
562801
+   'rhts-credentials';;
562801
+      Uses Login= and Password= values from /etc/libreport/plugins/rhtsupport.conf.
562801
+
562801
+   '<user_name>:<password>';;
562801
+      Manually supply credentials.
562801
+
562801
 'ContactEmail'::
562801
    Email address attached to a bthash on the server.
562801
 
562801
@@ -93,6 +106,10 @@ OPTIONS
562801
    Enables client authentication. See 'SSLClientAuth' configuration file
562801
    option for list of possible values.
562801
 
562801
+-h, --http-auth CREDENTIALS::
562801
+   Enables client authentication via HTTP Authentication. See 'HTTPAuth'
562801
+   configuration file option for list of possible values.
562801
+
562801
 -v::
562801
    Be more verbose. Can be given multiple times.
562801
 
562801
diff --git a/src/include/ureport.h b/src/include/ureport.h
562801
index 104e8d0..780b898 100644
562801
--- a/src/include/ureport.h
562801
+++ b/src/include/ureport.h
562801
@@ -126,6 +126,23 @@ ureport_server_config_set_basic_auth(struct ureport_server_config *config,
562801
                                      const char *username, const char *password);
562801
 
562801
 /*
562801
+ * Configure user name and password for HTTP Basic authentication according to
562801
+ * user preferences.
562801
+ *
562801
+ *  "rhts-credentials" - Uses Login= and Password= from rhtsupport.conf
562801
+ *  "<user_name>:<password>" - Manually supply user name and password.
562801
+ *  "<user_name>" - Manually supply user name and be asked for password.
562801
+ *
562801
+ * The function uses ask_password() function from client.h
562801
+ *
562801
+ * @param config Configured structure
562801
+ * @param http_auth_pref User HTTP Authentication preferences
562801
+ */
562801
+void
562801
+ureport_server_config_load_basic_auth(struct ureport_server_config *config,
562801
+                                      const char *http_auth_pref);
562801
+
562801
+/*
562801
  * uReport server response
562801
  */
562801
 struct ureport_server_response
562801
diff --git a/src/lib/ureport.c b/src/lib/ureport.c
562801
index fef3922..5065a52 100644
562801
--- a/src/lib/ureport.c
562801
+++ b/src/lib/ureport.c
562801
@@ -23,6 +23,7 @@
562801
 #include <satyr/report.h>
562801
 
562801
 #include "internal_libreport.h"
562801
+#include "client.h"
562801
 #include "ureport.h"
562801
 #include "libreport_curl.h"
562801
 
562801
@@ -249,18 +250,79 @@ ureport_server_config_set_basic_auth(struct ureport_server_config *config,
562801
 }
562801
 
562801
 void
562801
+ureport_server_config_load_basic_auth(struct ureport_server_config *config,
562801
+                                      const char *http_auth_pref)
562801
+{
562801
+    if (http_auth_pref == NULL)
562801
+        return;
562801
+
562801
+    map_string_t *settings = NULL;
562801
+
562801
+    char *tmp_password = NULL;
562801
+    char *tmp_username = NULL;
562801
+    const char *username = NULL;
562801
+    const char *password = NULL;
562801
+
562801
+    if (strcmp(http_auth_pref, "rhts-credentials") == 0)
562801
+    {
562801
+        settings = new_map_string();
562801
+
562801
+        if (!load_plugin_conf_file("rhtsupport.conf", settings, /*skip key w/o values:*/ false))
562801
+            error_msg_and_die("Could not get RHTSupport credentials");
562801
+
562801
+        username = get_map_string_item_or_NULL(settings, "Login");
562801
+        password = get_map_string_item_or_NULL(settings, "Password");
562801
+
562801
+        if (config->ur_url == NULL)
562801
+            ureport_server_config_set_url(config, xstrdup(RHSM_WEB_SERVICE_URL));
562801
+    }
562801
+    else
562801
+    {
562801
+        username = tmp_username = xstrdup(http_auth_pref);
562801
+        password = strchr(tmp_username, ':');
562801
+
562801
+        if (password != NULL)
562801
+            /* It is "char *", see strchr() few lines above. */
562801
+            *((char *)(password++)) = '\0';
562801
+    }
562801
+
562801
+    if (password == NULL)
562801
+    {
562801
+        char *message = xasprintf("Please provide uReport server password for user '%s':", username);
562801
+        password = tmp_password = ask_password(message);
562801
+        free(message);
562801
+
562801
+        if (password == NULL)
562801
+            error_msg_and_die("Cannot continue without uReport server password!");
562801
+    }
562801
+
562801
+    ureport_server_config_set_basic_auth(config, username, password);
562801
+
562801
+    free(tmp_password);
562801
+    free(tmp_username);
562801
+    free_map_string(settings);
562801
+}
562801
+
562801
+void
562801
 ureport_server_config_load(struct ureport_server_config *config,
562801
                            map_string_t *settings)
562801
 {
562801
     UREPORT_OPTION_VALUE_FROM_CONF(settings, "URL", config->ur_url, xstrdup);
562801
     UREPORT_OPTION_VALUE_FROM_CONF(settings, "SSLVerify", config->ur_ssl_verify, string_to_bool);
562801
 
562801
+    const char *http_auth_pref = NULL;
562801
+    UREPORT_OPTION_VALUE_FROM_CONF(settings, "HTTPAuth", http_auth_pref, (const char *));
562801
+    ureport_server_config_load_basic_auth(config, http_auth_pref);
562801
+
562801
     const char *client_auth = NULL;
562801
-    UREPORT_OPTION_VALUE_FROM_CONF(settings, "SSLClientAuth", client_auth, (const char *));
562801
-    ureport_server_config_set_client_auth(config, client_auth);
562801
+    if (http_auth_pref == NULL)
562801
+    {
562801
+        UREPORT_OPTION_VALUE_FROM_CONF(settings, "SSLClientAuth", client_auth, (const char *));
562801
+        ureport_server_config_set_client_auth(config, client_auth);
562801
+    }
562801
 
562801
     /* If SSLClientAuth is configured, include the auth items by default. */
562801
-    bool include_auth = !!config->ur_client_cert;
562801
+    bool include_auth = config->ur_client_cert != NULL || config->ur_username != NULL;
562801
     UREPORT_OPTION_VALUE_FROM_CONF(settings, "IncludeAuthData", include_auth, string_to_bool);
562801
 
562801
     if (include_auth)
562801
diff --git a/src/plugins/reporter-ureport.c b/src/plugins/reporter-ureport.c
562801
index f15d56d..22efb76 100644
562801
--- a/src/plugins/reporter-ureport.c
562801
+++ b/src/plugins/reporter-ureport.c
562801
@@ -43,7 +43,8 @@ int main(int argc, char **argv)
562801
         OPT_u = 1 << 2,
562801
         OPT_k = 1 << 3,
562801
         OPT_t = 1 << 4,
562801
-        OPT_i = 1 << 5,
562801
+        OPT_h = 1 << 5,
562801
+        OPT_i = 1 << 6,
562801
     };
562801
 
562801
     int ret = 1; /* "failure" (for now) */
562801
@@ -51,6 +52,7 @@ int main(int argc, char **argv)
562801
     const char *conf_file = UREPORT_CONF_FILE_PATH;
562801
     const char *arg_server_url = NULL;
562801
     const char *client_auth = NULL;
562801
+    const char *http_auth = NULL;
562801
     GList *auth_items = NULL;
562801
     const char *dump_dir_path = ".";
562801
     const char *ureport_hash = NULL;
562801
@@ -67,6 +69,7 @@ int main(int argc, char **argv)
562801
         OPT_BOOL('k', "insecure", &insecure,
562801
                           _("Allow insecure connection to ureport server")),
562801
         OPT_STRING('t', "auth", &client_auth, "SOURCE", _("Use client authentication")),
562801
+        OPT_STRING('h', "http-auth", &http_auth, "CREDENTIALS", _("Use HTTP Authentication")),
562801
         OPT_LIST('i', "auth_items", &auth_items, "AUTH_ITEMS", _("Additional files included in 'auth' key")),
562801
         OPT_STRING('c', NULL, &conf_file, "FILE", _("Configuration file")),
562801
         OPT_STRING('a', "attach", &ureport_hash, "BTHASH",
562801
@@ -85,8 +88,8 @@ int main(int argc, char **argv)
562801
     };
562801
 
562801
     const char *program_usage_string = _(
562801
-        "& [-v] [-c FILE] [-u URL] [-k] [-t SOURCE] [-A -a bthash -B -b bug-id -E -e email] [-d DIR]\n"
562801
-        "& [-v] [-c FILE] [-u URL] [-k] [-t SOURCE] [-i AUTH_ITEMS]\\\n"
562801
+        "& [-v] [-c FILE] [-u URL] [-k] [-t SOURCE] [-h CREDENTIALS] [-A -a bthash -B -b bug-id -E -e email] [-d DIR]\n"
562801
+        "& [-v] [-c FILE] [-u URL] [-k] [-t SOURCE] [-h CREDENTIALS] [-i AUTH_ITEMS]\\\n"
562801
         "  [-A -a bthash -B -b bug-id -E -e email] [-d DIR]\n"
562801
         "\n"
562801
         "Upload micro report or add an attachment to a micro report\n"
562801
@@ -107,6 +110,8 @@ int main(int argc, char **argv)
562801
         config.ur_ssl_verify = !insecure;
562801
     if (opts & OPT_t)
562801
         ureport_server_config_set_client_auth(&config, client_auth);
562801
+    if (opts & OPT_h)
562801
+        ureport_server_config_load_basic_auth(&config, http_auth);
562801
     if (opts & OPT_i)
562801
     {
562801
         g_list_free_full(config.ur_prefs.urp_auth_items, free);
562801
diff --git a/src/plugins/ureport.conf b/src/plugins/ureport.conf
562801
index 42323d4..e04bf56 100644
562801
--- a/src/plugins/ureport.conf
562801
+++ b/src/plugins/ureport.conf
562801
@@ -23,8 +23,16 @@ AuthDataItems = hostname, machineid
562801
 # None (default):
562801
 # SSLClientAuth =
562801
 # Using RH subscription management entitlement certificate:
562801
-SSLClientAuth = rhsm
562801
+# SSLClientAuth = rhsm
562801
 # Using Puppet certificate:
562801
 # SSLClientAuth = puppet
562801
 # Using custom certificate:
562801
 # SSLClientAuth = /path/to/cert.pem:/path/to/key.pem
562801
+
562801
+# HTTP Basic authentication credentials.
562801
+# Assingning any value to 'HTTPAuth' changes the default value of
562801
+# 'IncludeAuthData' to 'yes'.
562801
+# Use Login= and Password= from /etc/libreport/plugins/rhtsupport.conf:
562801
+# HTTPAuth = rhts-credentials
562801
+# Use username and password:
562801
+# HTTPAuth = username:password
562801
diff --git a/tests/Makefile.am b/tests/Makefile.am
562801
index 1cfc206..a680f05 100644
562801
--- a/tests/Makefile.am
562801
+++ b/tests/Makefile.am
562801
@@ -49,7 +49,7 @@ TESTSUITE = $(srcdir)/testsuite
562801
 MAINTAINERCLEANFILES = Makefile.in $(TESTSUITE)
562801
 check_DATA = atconfig atlocal $(TESTSUITE)
562801
 DISTCLEANFILES = atconfig
562801
-EXTRA_DIST += atlocal.in conf ureport
562801
+EXTRA_DIST += atlocal.in conf ureport ureport-rhts-credentials
562801
 
562801
 atconfig: $(top_builddir)/config.status
562801
 	(cd ${top_builddir} && ./config.status ${subdir}/atconfig)
562801
diff --git a/tests/ureport-rhts-credentials/rhtsupport.conf b/tests/ureport-rhts-credentials/rhtsupport.conf
562801
new file mode 100644
562801
index 0000000..c30f743
562801
--- /dev/null
562801
+++ b/tests/ureport-rhts-credentials/rhtsupport.conf
562801
@@ -0,0 +1,2 @@
562801
+Login = rhn-user-name
562801
+Password = rhn-password
562801
diff --git a/tests/ureport.at b/tests/ureport.at
562801
index 76e2f7a..3a824a2 100644
562801
--- a/tests/ureport.at
562801
+++ b/tests/ureport.at
562801
@@ -109,6 +109,8 @@ AT_TESTFUN([ureport_server_config_load],
562801
 #include "ureport.h"
562801
 #include <assert.h>
562801
 
562801
+#define TESTING_CERTS_CORRECT_DIR_PATH "../../ureport/certs/correct"
562801
+
562801
 int main(void)
562801
 {
562801
     g_verbose=3;
562801
@@ -248,6 +250,73 @@ int main(void)
562801
     ureport_server_config_destroy(&config);
562801
     free_map_string(settings);
562801
 
562801
+    /* value from env */
562801
+    /* HTTPAuth set to 'username:password' */
562801
+    /* SSLClientAuth set to 'rhsm' */
562801
+    ureport_server_config_init(&config);
562801
+
562801
+    settings = new_map_string();
562801
+
562801
+    setenv("uReport_SSLClientAuth", "rhsm", 1);
562801
+    setenv("uReport_HTTPAuth", "username:password", 1);
562801
+    setenv("uReport_AuthDataItems", "hostname, time", 1);
562801
+
562801
+    setenv("LIBREPORT_DEBUG_RHSMENT_PEM_DIR_PATH", TESTING_CERTS_CORRECT_DIR_PATH, 1);
562801
+
562801
+    ureport_server_config_load(&config, settings);
562801
+
562801
+    assert(strcmp(config.ur_username, "username") == 0);
562801
+    assert(strcmp(config.ur_password, "password") == 0);
562801
+
562801
+    assert(config.ur_client_cert == NULL);
562801
+    assert(config.ur_client_key == NULL);
562801
+    assert(size_map_string(config.ur_http_headers) == 0);
562801
+
562801
+    l = config.ur_prefs.urp_auth_items;
562801
+    assert(strcmp(l->data, "hostname") == 0);
562801
+    assert(strcmp(l->next->data, "time") == 0);
562801
+
562801
+    unsetenv("LIBREPORT_DEBUG_RHSMENT_PEM_DIR_PATH");
562801
+
562801
+    unsetenv("uReport_SSLClientAuth");
562801
+    unsetenv("uReport_HTTPAuth");
562801
+    unsetenv("uReport_AuthDataItems");
562801
+
562801
+    free_map_string(settings);
562801
+
562801
+    ureport_server_config_destroy(&config);
562801
+
562801
+    /* value from settings */
562801
+    /* HTTPAuth set to 'username:password' */
562801
+    /* SSLClientAuth set to 'rhsm' */
562801
+    ureport_server_config_init(&config);
562801
+
562801
+    settings = new_map_string();
562801
+    insert_map_string(settings, xstrdup("SSLClientAuth"), xstrdup("rhsm"));
562801
+    insert_map_string(settings, xstrdup("HTTPAuth"), xstrdup("rhn-username:rhn-password"));
562801
+    insert_map_string(settings, xstrdup("AuthDataItems"), xstrdup("hostname, type"));
562801
+
562801
+    setenv("LIBREPORT_DEBUG_RHSMENT_PEM_DIR_PATH", TESTING_CERTS_CORRECT_DIR_PATH, 1);
562801
+
562801
+    ureport_server_config_load(&config, settings);
562801
+
562801
+    assert(strcmp(config.ur_username, "rhn-username") == 0);
562801
+    assert(strcmp(config.ur_password, "rhn-password") == 0);
562801
+
562801
+    assert(config.ur_client_cert == NULL);
562801
+    assert(config.ur_client_key == NULL);
562801
+    assert(size_map_string(config.ur_http_headers) == 0);
562801
+
562801
+    l = config.ur_prefs.urp_auth_items;
562801
+    assert(strcmp(l->data, "hostname") == 0);
562801
+    assert(strcmp(l->next->data, "type") == 0);
562801
+
562801
+    unsetenv("LIBREPORT_DEBUG_RHSMENT_PEM_DIR_PATH");
562801
+
562801
+    free_map_string(settings);
562801
+
562801
+    ureport_server_config_destroy(&config);
562801
+
562801
     return 0;
562801
 }
562801
 ]])
562801
@@ -1133,3 +1202,79 @@ int main(void)
562801
 }
562801
 ]])
562801
 
562801
+
562801
+## ------------------------------------- ##
562801
+## ureport_server_config_load_basic_auth ##
562801
+## ------------------------------------- ##
562801
+
562801
+AT_TESTFUN([ureport_server_config_load_basic_auth],
562801
+[[
562801
+#include "internal_libreport.h"
562801
+#include "ureport.h"
562801
+#include <assert.h>
562801
+#include "libreport_curl.h"
562801
+#include "problem_data.h"
562801
+
562801
+int main(void)
562801
+{
562801
+    g_verbose=3;
562801
+
562801
+    {
562801
+        struct ureport_server_config config;
562801
+        ureport_server_config_init(&config);
562801
+
562801
+        ureport_server_config_load_basic_auth(&config, "username:password");
562801
+
562801
+        assert(strcmp(config.ur_username, "username") == 0);
562801
+        assert(strcmp(config.ur_password, "password") == 0);
562801
+
562801
+        ureport_server_config_destroy(&config);
562801
+    }
562801
+
562801
+    {
562801
+        struct ureport_server_config config;
562801
+        ureport_server_config_init(&config);
562801
+
562801
+        setenv("LIBREPORT_DEBUG_PLUGINS_CONF_DIR", "../../ureport-rhts-credentials/", 1);
562801
+
562801
+        ureport_server_config_load_basic_auth(&config, "rhts-credentials");
562801
+
562801
+        assert(strcmp(config.ur_username, "rhn-user-name") == 0);
562801
+        assert(strcmp(config.ur_password, "rhn-password") == 0);
562801
+        assert(strcmp(config.ur_url, "https://api.access.redhat.com/rs/telemetry/abrt") == 0);
562801
+
562801
+        unsetenv("LIBREPORT_DEBUG_PLUGINS_CONF_DIR");
562801
+        ureport_server_config_destroy(&config);
562801
+    }
562801
+
562801
+    {
562801
+        pid_t pid = fork();
562801
+        if (pid < 0)
562801
+        {
562801
+            perror_msg("fork");
562801
+            return -1;
562801
+        }
562801
+
562801
+        if (pid == 0)
562801
+        {
562801
+            struct ureport_server_config config;
562801
+            ureport_server_config_init(&config);
562801
+
562801
+            setenv("REPORT_CLIENT_NONINTERACTIVE", "1", 1);
562801
+            ureport_server_config_load_basic_auth(&config, "username");
562801
+
562801
+            ureport_server_config_destroy(&config);
562801
+
562801
+            exit(0);
562801
+        }
562801
+
562801
+        int status;
562801
+        wait(&status);
562801
+
562801
+        assert(WIFEXITED(status));
562801
+        assert(WEXITSTATUS(status) != 0);
562801
+    }
562801
+
562801
+    return 0;
562801
+}
562801
+]])
562801
-- 
562801
1.8.3.1
562801