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

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