Blame SOURCES/0089-lib-handle-access-denials-in-upload_file.patch

28bab8
From f7a5699843a9f96dbdd796da2f891989dbc0f5fd Mon Sep 17 00:00:00 2001
28bab8
From: Jakub Filak <jfilak@redhat.com>
28bab8
Date: Tue, 30 Sep 2014 01:43:08 +0200
28bab8
Subject: [LIBREPORT PATCH 89/93] lib: handle access denials in upload_file()
28bab8
28bab8
curl provides an error code which can be used to detect invalid
28bab8
credentials.
28bab8
28bab8
This patch adds a support for re-trying upload with new credentials.
28bab8
28bab8
Related to rhbz#1066486
28bab8
28bab8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
28bab8
---
28bab8
 src/include/libreport_curl.h | 11 +++++++
28bab8
 src/lib/curl.c               | 70 +++++++++++++++++++++++++++++++++++++++++---
28bab8
 2 files changed, 77 insertions(+), 4 deletions(-)
28bab8
28bab8
diff --git a/src/include/libreport_curl.h b/src/include/libreport_curl.h
28bab8
index 7d6fa02..4b41ecc 100644
28bab8
--- a/src/include/libreport_curl.h
28bab8
+++ b/src/include/libreport_curl.h
28bab8
@@ -119,9 +119,20 @@ post_file_as_form(post_state_t *state,
28bab8
                      filename, POST_DATA_FROMFILE_AS_FORM_DATA);
28bab8
 }
28bab8
 
28bab8
+enum {
28bab8
+    UPLOAD_FILE_NOFLAGS = 0,
28bab8
+    UPLOAD_FILE_HANDLE_ACCESS_DENIALS = 1 << 0,
28bab8
+};
28bab8
+
28bab8
 #define upload_file libreport_upload_file
28bab8
 char *upload_file(const char *url, const char *filename);
28bab8
 
28bab8
+#define upload_file_ext libreport_upload_file_ext
28bab8
+char *upload_file_ext(post_state_t *post_state,
28bab8
+                const char *url,
28bab8
+                const char *filename,
28bab8
+                int flags);
28bab8
+
28bab8
 #ifdef __cplusplus
28bab8
 }
28bab8
 #endif
28bab8
diff --git a/src/lib/curl.c b/src/lib/curl.c
28bab8
index 5ca18dd..f7321b5 100644
28bab8
--- a/src/lib/curl.c
28bab8
+++ b/src/lib/curl.c
28bab8
@@ -17,6 +17,7 @@
28bab8
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28bab8
 */
28bab8
 #include "internal_libreport.h"
28bab8
+#include "client.h"
28bab8
 #include "libreport_curl.h"
28bab8
 #include "proxies.h"
28bab8
 
28bab8
@@ -586,6 +587,15 @@ post(post_state_t *state,
28bab8
  */
28bab8
 char *upload_file(const char *url, const char *filename)
28bab8
 {
28bab8
+    post_state_t *state = new_post_state(POST_WANT_ERROR_MSG);
28bab8
+    char *retval = upload_file_ext(state, url, filename, UPLOAD_FILE_NOFLAGS);
28bab8
+    free_post_state(state);
28bab8
+
28bab8
+    return retval;
28bab8
+}
28bab8
+
28bab8
+char *upload_file_ext(post_state_t *state, const char *url, const char *filename, int flags)
28bab8
+{
28bab8
     /* we don't want to print the whole url as it may contain password
28bab8
      * rhbz#856960
28bab8
      * there can be '@' in the login or password so let's try to find the
28bab8
@@ -597,8 +607,6 @@ char *upload_file(const char *url, const char *filename)
28bab8
     else
28bab8
         clean_url = url;
28bab8
 
28bab8
-    log(_("Sending %s to %s"), filename, clean_url);
28bab8
-
28bab8
     char *whole_url;
28bab8
     unsigned len = strlen(url);
28bab8
     if (len > 0 && url[len-1] == '/')
28bab8
@@ -606,7 +614,25 @@ char *upload_file(const char *url, const char *filename)
28bab8
     else
28bab8
         whole_url = xstrdup(url);
28bab8
 
28bab8
-    post_state_t *state = new_post_state(POST_WANT_ERROR_MSG);
28bab8
+
28bab8
+    const char *username_bck = state->username;
28bab8
+    const char *password_bck = state->password;
28bab8
+    char *username = NULL;
28bab8
+    char *password = NULL;
28bab8
+
28bab8
+    /* work around bug in libssh2(curl with scp://)
28bab8
+     * libssh2_aget_disconnect() calls close(0)
28bab8
+     * https://bugzilla.redhat.com/show_bug.cgi?id=1147717
28bab8
+     */
28bab8
+    int stdin_bck = dup(0);
28bab8
+
28bab8
+    /*
28bab8
+     * Well, goto seems to be the most elegant syntax form here :(
28bab8
+     * This label is used to re-try the upload with an updated credentials.
28bab8
+     */
28bab8
+  do_post:
28bab8
+
28bab8
+    log(_("Sending %s to %s"), filename, clean_url);
28bab8
     post(state,
28bab8
                 whole_url,
28bab8
                 /*content_type:*/ "application/octet-stream",
28bab8
@@ -615,6 +641,8 @@ char *upload_file(const char *url, const char *filename)
28bab8
                 POST_DATA_FROMFILE_PUT
28bab8
     );
28bab8
 
28bab8
+    dup2(stdin_bck, 0);
28bab8
+
28bab8
     int error = (state->curl_result != 0);
28bab8
     if (error)
28bab8
     {
28bab8
@@ -623,6 +651,34 @@ char *upload_file(const char *url, const char *filename)
28bab8
         else
28bab8
             /* for example, when source file can't be opened */
28bab8
             error_msg("Error while uploading");
28bab8
+
28bab8
+        if ((flags & UPLOAD_FILE_HANDLE_ACCESS_DENIALS) &&
28bab8
+                (state->curl_result == CURLE_LOGIN_DENIED
28bab8
+                 || state->curl_result == CURLE_REMOTE_ACCESS_DENIED))
28bab8
+        {
28bab8
+            char *msg = xasprintf(_("Please enter user name for '%s':"), clean_url);
28bab8
+            free(username);
28bab8
+            username = ask(msg);
28bab8
+            free(msg);
28bab8
+            if (username != NULL && username[0] != '\0')
28bab8
+            {
28bab8
+                msg = xasprintf(_("Please enter password for '%s':"), username);
28bab8
+                free(password);
28bab8
+                password = ask_password(msg);
28bab8
+                free(msg);
28bab8
+                /* What about empty password? */
28bab8
+                if (password != NULL && password[0] != '\0')
28bab8
+                {
28bab8
+                    state->username = username;
28bab8
+                    state->password = password;
28bab8
+                    /*
28bab8
+                     * Re-try with new credentials
28bab8
+                     */
28bab8
+                    goto do_post;
28bab8
+                }
28bab8
+            }
28bab8
+        }
28bab8
+
28bab8
         free(whole_url);
28bab8
         whole_url = NULL;
28bab8
     }
28bab8
@@ -632,7 +688,13 @@ char *upload_file(const char *url, const char *filename)
28bab8
         log(_("Successfully sent %s to %s"), filename, clean_url);
28bab8
     }
28bab8
 
28bab8
-    free_post_state(state);
28bab8
+    close(stdin_bck);
28bab8
+
28bab8
+    free(password);
28bab8
+    free(username);
28bab8
+
28bab8
+    state->username = username_bck;
28bab8
+    state->password = password_bck;
28bab8
 
28bab8
     return whole_url;
28bab8
 }
28bab8
-- 
28bab8
1.8.3.1
28bab8