Blame SOURCES/0080-auto-reporting-add-options-to-specify-auth-type.patch

baab13
From 2b16db2fea5552225437ac7d622706b597b7a71d Mon Sep 17 00:00:00 2001
baab13
From: Jakub Filak <jfilak@redhat.com>
baab13
Date: Fri, 19 Dec 2014 00:41:16 +0100
baab13
Subject: [ABRT PATCH 80/82] auto-reporting: add options to specify auth type
baab13
baab13
See abrt-auto-reporting man page for more details about this commit.
baab13
baab13
Related: #1174833
baab13
baab13
Signed-off-by: Jakub Filak <jfilak@redhat.com>
baab13
---
baab13
 doc/abrt-auto-reporting.txt      |  41 ++++++-
baab13
 src/daemon/abrt-auto-reporting.c | 258 ++++++++++++++++++++++++++++++++++++---
baab13
 2 files changed, 280 insertions(+), 19 deletions(-)
baab13
baab13
diff --git a/doc/abrt-auto-reporting.txt b/doc/abrt-auto-reporting.txt
baab13
index 1cc534e..2a27945 100644
baab13
--- a/doc/abrt-auto-reporting.txt
baab13
+++ b/doc/abrt-auto-reporting.txt
baab13
@@ -3,11 +3,14 @@ abrt-auto-reporting(1)
baab13
 
baab13
 NAME
baab13
 ----
baab13
-abrt-auto-reporting - Get or modify a value of the auto reporting option
baab13
+abrt-auto-reporting - Get or modify the auto reporting option values
baab13
 
baab13
 SYNOPSIS
baab13
 --------
baab13
-'abrt-auto-reporting' [-v] [ enabled | yes | 1 | disabled | no | 0 ]
baab13
+'abrt-auto-reporting' [-v] [ enabled | yes | 1 | on | disabled | no | 0 | off ]
baab13
+                      [ [--anonymous] |
baab13
+                        [--username USERNAME [--password PASSWORD] ] |
baab13
+                        [--certificate SOURCE] ]
baab13
 
baab13
 DESCRIPTION
baab13
 -----------
baab13
@@ -23,6 +26,9 @@ process and will be persistent.
baab13
    ABRT uploads an uReport which was generated for a detected problem
baab13
    immediately after the detection phase.
baab13
 
baab13
+Reads and saves the authentication configuration options in
baab13
+/etc/libreport/plugins/ureport.conf and /etc/libreport/plugins/rhtsupport.conf
baab13
+
baab13
 uReport description
baab13
 ~~~~~~~~~~~~~~~~~~~
baab13
 ABRT supports uReports for four types of crashes: crashes of C/C++ programs
baab13
@@ -51,6 +57,14 @@ for kernel oopses::
baab13
     these are list of loaded kernel modules, list of taint flags, and full text
baab13
     of the kernel oops.
baab13
 
baab13
+The authenticated uReports also contains *hostname* and *machineid* to enable a
baab13
+server side filtering at https://access.redhat.com/.
baab13
+
baab13
+The authenticated uReports have the benefit of rich server replies which may
baab13
+include a solution for the submitted crash. The authentication is done using
baab13
+either Red Hat Subscription Certificates or Red Hat Customer Portal
baab13
+credentials.
baab13
+
baab13
 'Warning':
baab13
 The full text of a kernel oops might contain information like the
baab13
 identification of the host hardware type. You should disable the autoreporting
baab13
@@ -62,9 +76,30 @@ OPTIONS
baab13
 -v, --verbose::
baab13
    Be more verbose. Can be given multiple times.
baab13
 
baab13
+-a, --anonymous::
baab13
+   Turns the authentication off by clearing both 'SSLClientAuth' and 'HTTPAuth'
baab13
+   configuration options in /etc/libreport/plugins/ureport.conf
baab13
+
baab13
+-u, --username USERNAME::
baab13
+   Turns HTTP Authentication on by setting 'HTTPAuth' configuration option to
baab13
+   *rhts-credentials* in /etc/libreport/plugins/ureport.conf and storing
baab13
+   USERNAME and PASSWORD in /etc/libreport/plugins/rhtsupport.conf
baab13
+   Also turns the SSL Client Authentication off, because these methods cannot
baab13
+   be used together.
baab13
+
baab13
+-p, --password PASSWORD::
baab13
+   Password for HTTP Authentication. If not provided, a prompt asking for it
baab13
+   will be issued.
baab13
+
baab13
+-c, --certificate SOURCE::
baab13
+   Turns SSL Client Authentication on by setting 'SSLClientAuth' configuration
baab13
+   option to SOURCE in /etc/libreport/plugins/ureport.conf.
baab13
+   Also turns the HTTP Authentication off, because these methods cannot
baab13
+   be used together.
baab13
+
baab13
 SEE ALSO
baab13
 --------
baab13
-abrt.conf(5)
baab13
+abrt.conf(5), ureport.conf(5), rhtsupport.conf(5)
baab13
 
baab13
 AUTHORS
baab13
 -------
baab13
diff --git a/src/daemon/abrt-auto-reporting.c b/src/daemon/abrt-auto-reporting.c
baab13
index 0909bed..f50c4c2 100644
baab13
--- a/src/daemon/abrt-auto-reporting.c
baab13
+++ b/src/daemon/abrt-auto-reporting.c
baab13
@@ -17,6 +17,7 @@
baab13
 */
baab13
 
baab13
 #include "libabrt.h"
baab13
+#include "client.h"
baab13
 
baab13
 #include <stdio.h>
baab13
 
baab13
@@ -26,13 +27,24 @@
baab13
 #define STATE_MANUAL "disabled"
baab13
 #define STATE_AUTO "enabled"
baab13
 
baab13
-const char *const REPORTING_STATES[6][2] = {
baab13
+#define RHTS_NAME "rhtsupport.conf"
baab13
+#define RHTS_USERNAME_OPTION "Login"
baab13
+#define RHTS_PASSWORD_OPTION "Password"
baab13
+
baab13
+#define UREPORT_NAME "ureport.conf"
baab13
+#define UREPORT_HTTP_AUTH_OPTION "HTTPAuth"
baab13
+#define UREPORT_CLIENT_AUTH_OPTION "SSLClientAuth"
baab13
+#define UREPORT_RTHS_CREDENTIALS_AUTH "rhts-credentials"
baab13
+
baab13
+const char *const REPORTING_STATES[8][2] = {
baab13
     {STATE_MANUAL, "no" },
baab13
     {STATE_AUTO,   "yes"},
baab13
     {"no",         "no" },
baab13
     {"yes",        "yes"},
baab13
     {"0",          "no" },
baab13
     {"1",          "yes"},
baab13
+    {"off",        "no" },
baab13
+    {"on",         "yes"},
baab13
 };
baab13
 
baab13
 static int
baab13
@@ -52,6 +64,77 @@ set_abrt_reporting(map_string_t *conf, const char *opt_value)
baab13
     return 1;
baab13
 }
baab13
 
baab13
+static int
baab13
+set_ureport_http_auth(map_string_t *conf, const char *opt_value)
baab13
+{
baab13
+    const char *const cur_value = get_map_string_item_or_NULL(conf, UREPORT_HTTP_AUTH_OPTION);
baab13
+
baab13
+    if (cur_value == NULL || strcmp(cur_value, opt_value) != 0)
baab13
+    {
baab13
+        replace_map_string_item(conf, xstrdup(UREPORT_HTTP_AUTH_OPTION), xstrdup(opt_value));
baab13
+        remove_map_string_item(conf, UREPORT_CLIENT_AUTH_OPTION);
baab13
+
baab13
+        return save_plugin_conf_file(UREPORT_NAME, conf);
baab13
+    }
baab13
+
baab13
+    /* No changes needed -> success */
baab13
+    return 1;
baab13
+}
baab13
+
baab13
+static int
baab13
+set_ureport_client_auth(map_string_t *conf, const char *opt_value)
baab13
+{
baab13
+    const char *const cur_value = get_map_string_item_or_NULL(conf, UREPORT_CLIENT_AUTH_OPTION);
baab13
+
baab13
+    if (cur_value == NULL || strcmp(cur_value, opt_value) != 0)
baab13
+    {
baab13
+        replace_map_string_item(conf, xstrdup(UREPORT_CLIENT_AUTH_OPTION), xstrdup(opt_value));
baab13
+        remove_map_string_item(conf, UREPORT_HTTP_AUTH_OPTION);
baab13
+
baab13
+        return save_plugin_conf_file(UREPORT_NAME, conf);
baab13
+    }
baab13
+
baab13
+    /* No changes needed -> success */
baab13
+    return 1;
baab13
+}
baab13
+
baab13
+static int
baab13
+clear_ureport_auth(map_string_t *conf)
baab13
+{
baab13
+    const char *const http_cur_value = get_map_string_item_or_NULL(conf, UREPORT_HTTP_AUTH_OPTION);
baab13
+    const char *const ssl_cur_value = get_map_string_item_or_NULL(conf, UREPORT_CLIENT_AUTH_OPTION);
baab13
+
baab13
+    if (http_cur_value != NULL || ssl_cur_value != NULL)
baab13
+    {
baab13
+        remove_map_string_item(conf, UREPORT_HTTP_AUTH_OPTION);
baab13
+        remove_map_string_item(conf, UREPORT_CLIENT_AUTH_OPTION);
baab13
+
baab13
+        return save_plugin_conf_file(UREPORT_NAME, conf);
baab13
+    }
baab13
+
baab13
+    /* No changes needed -> success */
baab13
+    return 1;
baab13
+}
baab13
+
baab13
+static int
baab13
+set_rhts_credentials(map_string_t *conf, const char *username, const char *password)
baab13
+{
baab13
+    const char *const username_cur_value = get_map_string_item_or_NULL(conf, RHTS_USERNAME_OPTION);
baab13
+    const char *const password_cur_value = get_map_string_item_or_NULL(conf, RHTS_PASSWORD_OPTION);
baab13
+
baab13
+    if (  (username_cur_value == NULL || strcmp(username_cur_value, username) != 0)
baab13
+       || (password_cur_value == NULL || strcmp(password_cur_value, password) != 0))
baab13
+    {
baab13
+        replace_map_string_item(conf, xstrdup(RHTS_USERNAME_OPTION), xstrdup(username));
baab13
+        replace_map_string_item(conf, xstrdup(RHTS_PASSWORD_OPTION), xstrdup(password));
baab13
+
baab13
+        return save_plugin_conf_file(RHTS_NAME, conf);
baab13
+    }
baab13
+
baab13
+    /* No changes needed -> success */
baab13
+    return 1;
baab13
+}
baab13
+
baab13
 static const char *
baab13
 get_abrt_reporting(map_string_t *conf)
baab13
 {
baab13
@@ -60,6 +143,18 @@ get_abrt_reporting(map_string_t *conf)
baab13
     return REPORTING_STATES[index][0];
baab13
 }
baab13
 
baab13
+static const char *
baab13
+get_ureport_http_auth(map_string_t *conf)
baab13
+{
baab13
+    return get_map_string_item_or_NULL(conf, UREPORT_HTTP_AUTH_OPTION);
baab13
+}
baab13
+
baab13
+static const char *
baab13
+get_ureport_client_auth(map_string_t *conf)
baab13
+{
baab13
+    return get_map_string_item_or_NULL(conf, UREPORT_CLIENT_AUTH_OPTION);
baab13
+}
baab13
+
baab13
 int main(int argc, char *argv[])
baab13
 {
baab13
     setlocale(LC_ALL, "");
baab13
@@ -78,7 +173,8 @@ int main(int argc, char *argv[])
baab13
 
baab13
     abrt_init(argv);
baab13
     const char *program_usage_string = _(
baab13
-            "& [ "STATE_MANUAL" | "STATE_AUTO" | yes | no | 1 | 0 ]\n"
baab13
+            "& [ "STATE_MANUAL" | "STATE_AUTO" | yes | no | 1 | 0 ] \\\n"
baab13
+            "  [[--anonymous] | [--username USERNAME [--password PASSWORD]] | [--certificate SOURCE]]\n"
baab13
             "\n"
baab13
             "Get or modify a value of the auto-reporting option. The changes will take\n"
baab13
             "effect immediately and will be persistent.\n"
baab13
@@ -94,36 +190,72 @@ int main(int argc, char *argv[])
baab13
             "contains identification of the operating system, versions of the RPM packages\n"
baab13
             "involved in the crash, and whether the program ran under a root user.\n"
baab13
             "\n"
baab13
-            "See abrt-auto-reporting(1) for more details.\n"
baab13
+            "See abrt-auto-reporting(1), reporter-ureport(1) and reporter-rhtsupport(1)\n"
baab13
+            "for more details.\n"
baab13
     );
baab13
 
baab13
+    enum {
baab13
+        OPT_v = 1 << 0,
baab13
+        OPT_a = 1 << 1,
baab13
+        OPT_u = 1 << 2,
baab13
+        OPT_p = 1 << 3,
baab13
+        OPT_c = 1 << 4,
baab13
+    };
baab13
+
baab13
+    bool anonymous = false;
baab13
+    const char *username = NULL;
baab13
+    const char *password = NULL;
baab13
+    const char *certificate = NULL;
baab13
+
baab13
     /* Keep enum above and order of options below in sync! */
baab13
     struct options program_options[] = {
baab13
         OPT__VERBOSE(&g_verbose),
baab13
+        OPT_BOOL  (  'a', "anonymous",   &anonymous,               _("Turns the authentication off")),
baab13
+        OPT_STRING(  'u', "username",    &username,    "USERNAME", _("Red Hat Support user name")),
baab13
+        OPT_STRING(  'p', "password",    &password,    "PASSWORD", _("Red Hat Support password, if not given, a prompt for it will be issued")),
baab13
+        OPT_STRING(  'c', "certificate", &certificate, "SOURCE",   _("uReport SSL certificate paths or certificate type")),
baab13
         OPT_END()
baab13
     };
baab13
 
baab13
-    const unsigned optind = parse_opts(argc, argv, program_options, program_usage_string);
baab13
+    const unsigned opts = parse_opts(argc, argv, program_options, program_usage_string);
baab13
 
baab13
     argv += optind;
baab13
     argc -= optind;
baab13
 
baab13
-    if (argc > 2)
baab13
+    if ((opts & OPT_p) && !(opts & OPT_u))
baab13
     {
baab13
-        error_msg(_("Invalid number of arguments"));
baab13
+        error_msg(_("You also need to specify --username for --password"));
baab13
         show_usage_and_die(program_usage_string, program_options);
baab13
     }
baab13
 
baab13
-    int exit_code = EXIT_FAILURE;
baab13
+    if ((opts & OPT_u) && (opts & OPT_c))
baab13
+    {
baab13
+        error_msg(_("You can use either --username or --certificate"));
baab13
+        show_usage_and_die(program_usage_string, program_options);
baab13
+    }
baab13
 
baab13
-    map_string_t *conf = new_map_string();
baab13
-    if (!load_abrt_conf_file(CONF_NAME, conf))
baab13
-        goto finito;
baab13
+    if ((opts & OPT_u) && (opts & OPT_a))
baab13
+    {
baab13
+        error_msg(_("You can use either --username or --anonymous"));
baab13
+        show_usage_and_die(program_usage_string, program_options);
baab13
+    }
baab13
+
baab13
+    if ((opts & OPT_a) && (opts & OPT_c))
baab13
+    {
baab13
+        error_msg(_("You can use either --anonymous or --certificate"));
baab13
+        show_usage_and_die(program_usage_string, program_options);
baab13
+    }
baab13
+
baab13
+    if (argc > 1)
baab13
+    {
baab13
+        error_msg(_("Invalid number of arguments"));
baab13
+        show_usage_and_die(program_usage_string, program_options);
baab13
+    }
baab13
 
baab13
-    if (argc == 2)
baab13
+    const char *opt_value = NULL;
baab13
+    if (argc == 1)
baab13
     {
baab13
-        const char *const new_value = argv[1];
baab13
-        const char *opt_value = NULL;
baab13
+        const char *const new_value = argv[0];
baab13
         for (int i = 0; i < sizeof(REPORTING_STATES)/sizeof(REPORTING_STATES[0]); ++i)
baab13
         {
baab13
             if (strcasecmp(new_value, REPORTING_STATES[i][0]) == 0)
baab13
@@ -138,15 +270,109 @@ int main(int argc, char *argv[])
baab13
             error_msg(_("Unknown option value: '%s'\n"), new_value);
baab13
             show_usage_and_die(program_usage_string, program_options);
baab13
         }
baab13
+    }
baab13
+
baab13
+    int exit_code = EXIT_FAILURE;
baab13
+
baab13
+    map_string_t *conf = new_map_string();
baab13
+    map_string_t *rhts_conf = new_map_string();
baab13
+    map_string_t *rhts_conf_bck = NULL;
baab13
+    map_string_t *ureport_conf = new_map_string();
baab13
+    map_string_t *ureport_conf_bck = NULL;
baab13
+
baab13
+    if (!load_abrt_conf_file(CONF_NAME, conf))
baab13
+        goto finito;
baab13
 
baab13
-        exit_code = set_abrt_reporting(conf, opt_value) ? EXIT_SUCCESS : EXIT_FAILURE;
baab13
+    if (!load_plugin_conf_file(RHTS_NAME, rhts_conf, false))
baab13
         goto finito;
baab13
+
baab13
+    if (!load_plugin_conf_file(UREPORT_NAME, ureport_conf, false))
baab13
+        goto finito;
baab13
+
baab13
+    if ((opts & OPT_a))
baab13
+    {
baab13
+        ureport_conf_bck = clone_map_string(ureport_conf);
baab13
+
baab13
+        if (!clear_ureport_auth(ureport_conf))
baab13
+            goto finito;
baab13
+    }
baab13
+
baab13
+    if ((opts & OPT_u))
baab13
+    {
baab13
+        char *tmp_password = NULL;
baab13
+        if (!(opts & OPT_p))
baab13
+        {
baab13
+            password = tmp_password = ask_password(_("Password:"));
baab13
+            if (tmp_password == NULL)
baab13
+            {
baab13
+                error_msg(_("Cannot continue without password\n"));
baab13
+                goto finito;
baab13
+            }
baab13
+        }
baab13
+
baab13
+        ureport_conf_bck = clone_map_string(ureport_conf);
baab13
+
baab13
+        if (!set_ureport_http_auth(ureport_conf, UREPORT_RTHS_CREDENTIALS_AUTH))
baab13
+            goto finito;
baab13
+
baab13
+        rhts_conf_bck = clone_map_string(rhts_conf);
baab13
+
baab13
+        if (!set_rhts_credentials(rhts_conf, username, password))
baab13
+        {
baab13
+            save_plugin_conf_file(UREPORT_NAME, ureport_conf_bck);
baab13
+            goto finito;
baab13
+        }
baab13
+
baab13
+        free(tmp_password);
baab13
+    }
baab13
+
baab13
+    if ((opts & OPT_c))
baab13
+    {
baab13
+        ureport_conf_bck = clone_map_string(ureport_conf);
baab13
+
baab13
+        if (!set_ureport_client_auth(ureport_conf, certificate))
baab13
+            goto finito;
baab13
+    }
baab13
+
baab13
+    if (argc == 0)
baab13
+    {
baab13
+        printf("%s", get_abrt_reporting(conf));
baab13
+        exit_code = EXIT_SUCCESS;
baab13
+
baab13
+        if (g_verbose >= 1)
baab13
+        {
baab13
+            const char *tmp = get_ureport_http_auth(ureport_conf);
baab13
+            if (tmp != NULL)
baab13
+                /* Print only the part before ':' of a string like "username:password" */
baab13
+                printf(" %s (%*s)", _("HTTP Authenticated auto reporting"), (int)(strchrnul(tmp, ':') - tmp), tmp);
baab13
+            else if ((tmp = get_ureport_client_auth(ureport_conf)) != NULL)
baab13
+                printf(" %s (%s)", _("SSL Client Authenticated auto reporting"), tmp);
baab13
+            else
baab13
+                printf(" %s", _("anonymous auto reporting"));
baab13
+        }
baab13
+
baab13
+        putchar('\n');
baab13
+
baab13
+        goto finito;
baab13
+    }
baab13
+
baab13
+    exit_code = set_abrt_reporting(conf, opt_value) ? EXIT_SUCCESS : EXIT_FAILURE;
baab13
+
baab13
+    if (exit_code == EXIT_FAILURE)
baab13
+    {
baab13
+        if (ureport_conf_bck != NULL)
baab13
+            save_plugin_conf_file(UREPORT_NAME, ureport_conf_bck);
baab13
+
baab13
+        if (rhts_conf_bck != NULL)
baab13
+            save_plugin_conf_file(RHTS_NAME, rhts_conf_bck);
baab13
     }
baab13
 
baab13
-    printf("%s\n", get_abrt_reporting(conf));
baab13
-    exit_code = EXIT_SUCCESS;
baab13
 
baab13
 finito:
baab13
+    free_map_string(ureport_conf);
baab13
+    free_map_string(ureport_conf_bck);
baab13
+    free_map_string(rhts_conf);
baab13
+    free_map_string(rhts_conf_bck);
baab13
     free_map_string(conf);
baab13
     return exit_code;
baab13
 }
baab13
-- 
baab13
1.8.3.1
baab13