Blame SOURCES/0091-rhtsupport-re-prompt-for-credentials.patch

28bab8
From 097869274a0fe107587226c117a4b7288d37cea0 Mon Sep 17 00:00:00 2001
28bab8
From: Jakub Filak <jfilak@redhat.com>
28bab8
Date: Fri, 26 Sep 2014 18:40:46 +0200
28bab8
Subject: [LIBREPORT PATCH 91/93] rhtsupport: re-prompt for credentials
28bab8
28bab8
Resolves rhbz#1104313
28bab8
28bab8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
28bab8
---
28bab8
 src/include/ureport.h             |  11 ++++
28bab8
 src/lib/ureport.c                 |   2 +-
28bab8
 src/plugins/reporter-rhtsupport.c | 117 ++++++++++++++++++++++++++++++--------
28bab8
 3 files changed, 106 insertions(+), 24 deletions(-)
28bab8
28bab8
diff --git a/src/include/ureport.h b/src/include/ureport.h
28bab8
index 8bb1f6c..104e8d0 100644
28bab8
--- a/src/include/ureport.h
28bab8
+++ b/src/include/ureport.h
28bab8
@@ -216,6 +216,17 @@ struct ureport_server_response *
28bab8
 ureport_submit(const char *json_ureport, struct ureport_server_config *config);
28bab8
 
28bab8
 /*
28bab8
+ * Build a new uReport attachement from give arguments
28bab8
+ *
28bab8
+ * @param bthash ID of uReport
28bab8
+ * @param type Type of attachement recognized by uReport Server
28bab8
+ * @param data Attached data
28bab8
+ * @returm Malloced JSON string
28bab8
+ */
28bab8
+char *
28bab8
+ureport_json_attachment_new(const char *bthash, const char *type, const char *data);
28bab8
+
28bab8
+/*
28bab8
  * Attach given string to uReport
28bab8
  *
28bab8
  * @param bthash uReport identifier
28bab8
diff --git a/src/lib/ureport.c b/src/lib/ureport.c
28bab8
index f906f3e..7e71c51 100644
28bab8
--- a/src/lib/ureport.c
28bab8
+++ b/src/lib/ureport.c
28bab8
@@ -742,7 +742,7 @@ ureport_submit(const char *json, struct ureport_server_config *config)
28bab8
     return resp;
28bab8
 }
28bab8
 
28bab8
-static char *
28bab8
+char *
28bab8
 ureport_json_attachment_new(const char *bthash, const char *type, const char *data)
28bab8
 {
28bab8
     struct json_object *attachment = json_object_new_object();
28bab8
diff --git a/src/plugins/reporter-rhtsupport.c b/src/plugins/reporter-rhtsupport.c
28bab8
index 47e544d..c063f3f 100644
28bab8
--- a/src/plugins/reporter-rhtsupport.c
28bab8
+++ b/src/plugins/reporter-rhtsupport.c
28bab8
@@ -26,6 +26,22 @@
28bab8
 
28bab8
 #define QUERY_HINTS_IF_SMALLER_THAN  (8*1024*1024)
28bab8
 
28bab8
+static void ask_rh_credentials(char **login, char **password);
28bab8
+
28bab8
+#define INVALID_CREDENTIALS_LOOP(l, p, r, fncall) \
28bab8
+    do {\
28bab8
+        r = fncall;\
28bab8
+        if (r->error == 0 || r->http_resp_code != 401 ) { break; }\
28bab8
+        ask_rh_credentials(&l, &p);\
28bab8
+        free_rhts_result(r);\
28bab8
+    } while (1)
28bab8
+
28bab8
+#define STRCPY_IF_NOT_EQUAL(dest, src) \
28bab8
+    do { if (strcmp(dest, src) != 0 ) { \
28bab8
+        free(dest); \
28bab8
+        dest = xstrdup(src); \
28bab8
+    } } while (0)
28bab8
+
28bab8
 static report_result_t *get_reported_to(const char *dump_dir_name)
28bab8
 {
28bab8
     struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0);
28bab8
@@ -170,6 +186,38 @@ ret_clean:
28bab8
 }
28bab8
 
28bab8
 static
28bab8
+struct ureport_server_response *ureport_do_post_credentials(const char *json, struct ureport_server_config *config, const char *action)
28bab8
+{
28bab8
+    struct post_state *post_state = NULL;
28bab8
+    while (1)
28bab8
+    {
28bab8
+        post_state = ureport_do_post(json, config, action);
28bab8
+
28bab8
+        if (post_state == NULL)
28bab8
+        {
28bab8
+            error_msg(_("Failed on submitting the problem"));
28bab8
+            return NULL;
28bab8
+        }
28bab8
+
28bab8
+        if (post_state->http_resp_code != 401)
28bab8
+            break;
28bab8
+
28bab8
+        free_post_state(post_state);
28bab8
+
28bab8
+        char *login = NULL;
28bab8
+        char *password = NULL;
28bab8
+        ask_rh_credentials(&login, &password);
28bab8
+        ureport_server_config_set_basic_auth(config, login, password);
28bab8
+        free(password);
28bab8
+        free(login);
28bab8
+    }
28bab8
+
28bab8
+    struct ureport_server_response *resp = ureport_server_response_from_reply(post_state, config);
28bab8
+    free(post_state);
28bab8
+    return resp;
28bab8
+}
28bab8
+
28bab8
+static
28bab8
 char *submit_ureport(const char *dump_dir_name, struct ureport_server_config *conf)
28bab8
 {
28bab8
     struct dump_dir *dd = dd_opendir(dump_dir_name, DD_OPEN_READONLY);
28bab8
@@ -191,7 +239,7 @@ char *submit_ureport(const char *dump_dir_name, struct ureport_server_config *co
28bab8
     if (json == NULL)
28bab8
         return NULL;
28bab8
 
28bab8
-    struct ureport_server_response *resp = ureport_submit(json, conf);
28bab8
+    struct ureport_server_response *resp = ureport_do_post_credentials(json, conf, UREPORT_SUBMIT_ACTION);
28bab8
     free(json);
28bab8
     if (resp == NULL)
28bab8
         return NULL;
28bab8
@@ -215,9 +263,14 @@ char *submit_ureport(const char *dump_dir_name, struct ureport_server_config *co
28bab8
 }
28bab8
 
28bab8
 static
28bab8
-bool check_for_hints(const char *url, const char *login, const char *password, bool ssl_verify, const char *tempfile)
28bab8
+bool check_for_hints(const char *url, char **login, char **password, bool ssl_verify, const char *tempfile)
28bab8
 {
28bab8
-    rhts_result_t *result = get_rhts_hints(url, login, password, ssl_verify, tempfile);
28bab8
+    rhts_result_t *result = NULL;
28bab8
+
28bab8
+    INVALID_CREDENTIALS_LOOP((*login), (*password),
28bab8
+            result, get_rhts_hints(url, *login, *password, ssl_verify, tempfile)
28bab8
+    );
28bab8
+
28bab8
 #if 0 /* testing */
28bab8
     log("ERR:%d", result->error);
28bab8
     log("MSG:'%s'", result->msg);
28bab8
@@ -291,6 +344,19 @@ char *ask_rh_password(const char *message)
28bab8
 }
28bab8
 
28bab8
 static
28bab8
+void ask_rh_credentials(char **login, char **password)
28bab8
+{
28bab8
+    free(*login);
28bab8
+    free(*password);
28bab8
+
28bab8
+    *login = ask_rh_login(_("Invalid password or login. Please enter your Red Hat login:"));
28bab8
+
28bab8
+    char *question = xasprintf(_("Invalid password or login. Please enter the password for '%s':"), *login);
28bab8
+    *password = ask_rh_password(question);
28bab8
+    free(question);
28bab8
+}
28bab8
+
28bab8
+static
28bab8
 char *get_param_string(const char *name, map_string_t *settings, const char *dflt)
28bab8
 {
28bab8
     char *envname = xasprintf("RHTSupport_%s", name);
28bab8
@@ -584,13 +650,17 @@ int main(int argc, char **argv)
28bab8
             log(_("Sending ABRT crash statistics data"));
28bab8
 
28bab8
             bthash = submit_ureport(dump_dir_name, &urconf);
28bab8
+
28bab8
+            /* Ensure that we will use the updated credentials */
28bab8
+            STRCPY_IF_NOT_EQUAL(login, urconf.ur_username);
28bab8
+            STRCPY_IF_NOT_EQUAL(password, urconf.ur_password);
28bab8
         }
28bab8
 
28bab8
         if (tempfile_size <= QUERY_HINTS_IF_SMALLER_THAN)
28bab8
         {
28bab8
             /* Check for hints and show them if we have something */
28bab8
             log(_("Checking for hints"));
28bab8
-            if (check_for_hints(base_api_url, login, password, ssl_verify, tempfile))
28bab8
+            if (check_for_hints(base_api_url, &login, &password, ssl_verify, tempfile))
28bab8
             {
28bab8
                 ureport_server_config_destroy(&urconf);
28bab8
                 free_map_string(ursettings);
28bab8
@@ -613,15 +683,9 @@ int main(int argc, char **argv)
28bab8
             error_msg_and_die(_("Can't determine RH Support Product from problem data."));
28bab8
         }
28bab8
 
28bab8
-        result = create_new_case(url,
28bab8
-                login,
28bab8
-                password,
28bab8
-                ssl_verify,
28bab8
-                product,
28bab8
-                version,
28bab8
-                summary,
28bab8
-                dsc,
28bab8
-                package
28bab8
+        INVALID_CREDENTIALS_LOOP(login, password,
28bab8
+                result, create_new_case(url, login, password, ssl_verify,
28bab8
+                                        product, version, summary, dsc, package)
28bab8
         );
28bab8
 
28bab8
         free(version);
28bab8
@@ -673,7 +737,19 @@ int main(int argc, char **argv)
28bab8
         if (bthash)
28bab8
         {
28bab8
             log(_("Linking ABRT crash statistics record with the case"));
28bab8
-            ureport_attach_string(bthash, "RHCID", result->url, &urconf);
28bab8
+
28bab8
+            /* Make sure we use the current credentials */
28bab8
+            ureport_server_config_set_basic_auth(&urconf, login, password);
28bab8
+
28bab8
+            /* Do attach */
28bab8
+            char *json = ureport_json_attachment_new(bthash, "RHCID", result->url);
28bab8
+            struct ureport_server_response *resp = ureport_do_post_credentials(json, &urconf, UREPORT_ATTACH_ACTION);
28bab8
+            ureport_server_response_free(resp);
28bab8
+            free(json);
28bab8
+
28bab8
+            /* Update the credentials */
28bab8
+            STRCPY_IF_NOT_EQUAL(login, urconf.ur_username);
28bab8
+            STRCPY_IF_NOT_EQUAL(password, urconf.ur_password);
28bab8
         }
28bab8
 
28bab8
         url = result->url;
28bab8
@@ -705,10 +781,8 @@ int main(int argc, char **argv)
28bab8
             remote_filename
28bab8
         );
28bab8
         free(remote_filename);
28bab8
-        result_atch = add_comment_to_case(url,
28bab8
-                login, password,
28bab8
-                ssl_verify,
28bab8
-                comment_text
28bab8
+        INVALID_CREDENTIALS_LOOP(login, password,
28bab8
+                result_atch, add_comment_to_case(url, login, password, ssl_verify, comment_text)
28bab8
         );
28bab8
         free(comment_text);
28bab8
     }
28bab8
@@ -716,11 +790,8 @@ int main(int argc, char **argv)
28bab8
     {
28bab8
         /* Attach the tarball of -d DIR */
28bab8
         log(_("Attaching problem data to case '%s'"), url);
28bab8
-        result_atch = attach_file_to_case(url,
28bab8
-                login, password,
28bab8
-                ssl_verify,
28bab8
-                tempfile
28bab8
-
28bab8
+        INVALID_CREDENTIALS_LOOP(login, password,
28bab8
+                result_atch, attach_file_to_case(url, login, password, ssl_verify, tempfile)
28bab8
         );
28bab8
     }
28bab8
     if (result_atch->error)
28bab8
-- 
28bab8
1.8.3.1
28bab8