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

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