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

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