cURL / Mailing Lists / curl-library / Single Mail

curl-library

Changes to ssh.c

From: James Housley <jim_at_thehousleys.net>
Date: Mon, 25 Jun 2007 09:24:52 -0400

Now that 7.16.3 is out the door I can start talking about more
changes to ssh.c. In the changes below there are 3 different sets of
patches, sorry.

The first difference is using libssh2_sftp_readdir_ex() to read the
directory text entry from the server, which includes the user and
group names instead of numbers. That one is simple.

The second is using Curl_debug() instead of infof(). This one I
would like some clarification on, because I am sure there are lots of
places that infof() is being used when it shouldn't be.

The third is "PWD". The curl interface to libssh2 doesn't allow the
external program to find the "home" directory that you log into. I
added a very simple command to postquote to honor the FTP "PWD"
command. But, that brings up a question. Should there be an option
to have SFTP simulate FTP commands and responses to make programs
that use them be able to seamlessly as possible use the SFTP protocol?

Lastly, will start converting the rest of ssh.c to operate in a state
machine so ssh.c won't block curl when used in multi_*()

Jim

--- curl/lib/ssh.c 2007-06-20 07:30:35.000000000 -0400
+++ FeedForAll/Cocoa/libcurl/ssh.c 2007-06-20 07:59:15.000000000 -0400
@@ -18,7 +18,7 @@
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
OF ANY
* KIND, either express or implied.
*
-* $Id: ssh.c,v 1.53 2007-06-20 11:30:35 jehousley Exp $
+* $Id: ssh.c,v 1.5 2007/06/20 11:59:15 housley Exp $
************************************************************************
***/
/* #define CURL_LIBSSH2_DEBUG */
@@ -1447,6 +1447,9 @@
         * data to read from the network, instead of "hanging" here.
         */
        char filename[PATH_MAX+1];
+#if (LIBSSH2_APINO >= 200706151200)
+ char longentry[PATH_MAX+1];
+#endif /* (LIBSSH2_APINO >= 200706151200) */
        int len, totalLen, currLen;
        char *line;
@@ -1475,7 +1478,12 @@
#endif /* !(LIBSSH2_APINO >= 200706012030) */
        do {
-#if (LIBSSH2_APINO >= 200706012030)
+#if (LIBSSH2_APINO >= 200706151200)
+ while ((len = libssh2_sftp_readdir_ex(sftp->sftp_handle,
filename,
+ PATH_MAX, longentry,
+ PATH_MAX, &attrs)) ==
+ LIBSSH2_ERROR_EAGAIN);
+#elif (LIBSSH2_APINO >= 200706012030)
          while ((len = libssh2_sftp_readdir(sftp->sftp_handle,
filename,
                                             PATH_MAX, &attrs)) ==
                 LIBSSH2_ERROR_EAGAIN);
@@ -1490,10 +1498,25 @@
              if ((attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) &&
                  ((attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
                   LIBSSH2_SFTP_S_IFDIR)) {
- infof(data, "%s\n", filename);
+ res = Curl_client_write(conn, CLIENTWRITE_BODY,
filename, 0);
+
+ /* output debug output if that is requested */
+ if (data->set.verbose) {
+ Curl_debug(data, CURLINFO_DATA_OUT, filename, len,
conn);
+ }
              }
            }
            else {
+#if (LIBSSH2_APINO >= 200706151200)
+ currLen = strlen(longentry);
+ totalLen = 80 + currLen;
+ line = (char *)calloc(totalLen, 1);
+ if (!line) {
+ return CURLE_OUT_OF_MEMORY;
+ }
+
+ memcpy(line, longentry, currLen);
+#else /* !(LIBSSH2_APINO >= 200706151200) */
              totalLen = 80 + len;
              line = (char *)malloc(totalLen);
              if (!line)
@@ -1584,6 +1607,7 @@
              }
              currLen += snprintf(line+currLen, totalLen-currLen, " %s",
                                  filename);
+#endif /* !(LIBSSH2_APINO >= 200706151200) */
              if ((attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) &&
                  ((attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
                   LIBSSH2_SFTP_S_IFLNK)) {
@@ -1608,10 +1632,22 @@
              currLen += snprintf(line+currLen, totalLen-currLen, "\n");
              res = Curl_client_write(conn, CLIENTWRITE_BODY, line, 0);
+
+ /* output debug output if that is requested */
+ if (data->set.verbose) {
+ Curl_debug(data, CURLINFO_DATA_OUT, line, currLen, conn);
+ }
              free(line);
            }
          }
+ else if (len == 0) {
+ break;
+ }
          else if (len <= 0) {
+ err = libssh2_sftp_last_error(sftp->sftp_session);
+ failf(conn->data, "Could not open remote file for reading:
%s :: %d",
+ sftp_libssh2_strerror(err),
+ libssh2_session_last_error(sftp->ssh_session, NULL,
NULL, 0));
            break;
          }
        } while (1);
@@ -1749,6 +1785,12 @@
    int ret;
    (void)premature; /* not used */
+ /* Before we shut down, see if there are any post-quote commands
to send: */
+ if(!status && !premature && conn->data->set.postquote) {
+ infof(conn->data, "Sending postquote commands\n");
+ rc = sftp_sendquote(conn, conn->data->set.postquote);
+ }
+
    Curl_safefree(sftp->path);
    sftp->path = NULL;
@@ -1769,12 +1811,6 @@
#endif /* !(LIBSSH2_APINO >= 200706012030) */
    }
- /* Before we shut down, see if there are any post-quote commands
to send: */
- if(!status && !premature && conn->data->set.postquote) {
- infof(conn->data, "Sending postquote commands\n");
- rc = sftp_sendquote(conn, conn->data->set.postquote);
- }
-
    if (sftp->sftp_session) {
#if (LIBSSH2_APINO >= 200706012030)
      while ((ret = libssh2_sftp_shutdown(sftp->sftp_session)) ==
@@ -2018,7 +2054,20 @@
    int ret;
    while (item) {
- if (item->data) {
+ /*
+ * Support some of the "FTP" commands
+ */
+ if (curl_strnequal(item->data, "PWD", 3)) {
+ /* output debug output if that is requested */
+ if (data->set.verbose) {
+ char tmp[PATH_MAX+1];
+
+ Curl_debug(data, CURLINFO_HEADER_OUT, "PWD\n", 4, conn);
+ snprintf(tmp, PATH_MAX, "257 \"%s\" is current directory.
\n", data->reqdata.proto.ssh->path);
+ Curl_debug(data, CURLINFO_HEADER_IN, tmp, strlen(tmp), conn);
+ }
+ }
+ else if (item->data) {
        char *path1 = NULL;
        char *path2 = NULL;

--
/"\   ASCII Ribbon Campaign  .
\ / - NO HTML/RTF in e-mail  .
  X  - NO Word docs in e-mail .
/ \ -----------------------------------------------------------------
                      http://www.FreeBSD.org     The Power to Serve
jim@TheHousleys.Net  http://www.TheHousleys.net
---------------------------------------------------------------------
In theory there is no difference between theory and practice.
In practice there is no similarity.
       -- From the "I wish I'd said that" archives.
Received on 2007-06-25