Hi,

I'm using libssh2 (incidentally, together with curl) to upload a file
to a remote host using the SCP protocol
Now, if the remote side returns an error message such as "Permission
denied" or "No such file or directory", it is not reported by libssh2.
Instead, the generic message "Invalid ACK response from remote" is
reported.

The attached patch reads the remote error message, and reports it in a
way similar to the command-line scp(1) command.

To see its effect, when using curl normally, this is what happens on a
"Permission denied" error:

Without the patch:

curl -T /etc/shadow scp://user:[EMAIL PROTECTED]/etc/shadow
...
curl: (2) failed init


With the patch:

curl -T /etc/shadow scp://user:[EMAIL PROTECTED]/etc/shadow
...
curl: (79) scp: /etc/shadow: Permission denied


PS: To make curl print the error, I've generated a simple patch to
curl as well, which I'll post on the curl-library list.

-- Gavrie
Index: src/scp.c
===================================================================
RCS file: /cvsroot/libssh2/libssh2/src/scp.c,v
retrieving revision 1.14
diff -u -r1.14 scp.c
--- src/scp.c	12 Jun 2007 18:27:50 -0000	1.14
+++ src/scp.c	4 Jul 2007 11:57:45 -0000
@@ -583,9 +583,27 @@
         libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block waiting for response", 0);
         return NULL;
     }
-    else if ((rc <= 0) || (session->scpSend_response[0] != 0)) {
-        libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, "Invalid ACK response from remote", 0);
-        goto scp_send_error;
+    else {
+        if (rc <= 0) {
+            libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, "Invalid ACK response from remote", 0);
+            goto scp_send_error;
+        }
+        if (session->scpSend_response[0] != 0) {
+            /* Handle error sent by remote scp */
+            char err_msg[1024];
+            char *p_err_msg = err_msg;
+            /* Read the remote error message */
+            do {
+                rc = libssh2_channel_read_ex(session->scpSend_channel, 0, 
+                    (char *)session->scpSend_response, 1);
+                if (rc <= 0)
+                    break;
+                *p_err_msg++ = session->scpSend_response[0];
+            } while ((p_err_msg - err_msg) < sizeof(err_msg));
+            *p_err_msg = '\0';
+            libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, err_msg, 0);
+            goto scp_send_error;
+        }
     }
     
     session->scpSend_state = libssh2_NB_state_idle;
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
libssh2-devel mailing list
libssh2-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libssh2-devel

Reply via email to