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

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