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

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