ssh: improve the logic for detecting blocking direction
[platform/upstream/curl.git] / lib / ssh.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 /* #define CURL_LIBSSH2_DEBUG */
24
25 #include "curl_setup.h"
26
27 #ifdef USE_LIBSSH2
28
29 #ifdef HAVE_LIMITS_H
30 #  include <limits.h>
31 #endif
32
33 #include <libssh2.h>
34 #include <libssh2_sftp.h>
35
36 #ifdef HAVE_FCNTL_H
37 #include <fcntl.h>
38 #endif
39
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
42 #endif
43 #ifdef HAVE_ARPA_INET_H
44 #include <arpa/inet.h>
45 #endif
46 #ifdef HAVE_UTSNAME_H
47 #include <sys/utsname.h>
48 #endif
49 #ifdef HAVE_NETDB_H
50 #include <netdb.h>
51 #endif
52 #ifdef __VMS
53 #include <in.h>
54 #include <inet.h>
55 #endif
56
57 #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
58 #undef in_addr_t
59 #define in_addr_t unsigned long
60 #endif
61
62 #include <curl/curl.h>
63 #include "urldata.h"
64 #include "sendf.h"
65 #include "hostip.h"
66 #include "progress.h"
67 #include "transfer.h"
68 #include "escape.h"
69 #include "http.h" /* for HTTP proxy tunnel stuff */
70 #include "ssh.h"
71 #include "url.h"
72 #include "speedcheck.h"
73 #include "getinfo.h"
74
75 #include "strequal.h"
76 #include "sslgen.h"
77 #include "connect.h"
78 #include "strerror.h"
79 #include "inet_ntop.h"
80 #include "parsedate.h" /* for the week day and month names */
81 #include "sockaddr.h" /* required for Curl_sockaddr_storage */
82 #include "strtoofft.h"
83 #include "multiif.h"
84 #include "select.h"
85 #include "warnless.h"
86
87 #define _MPRINTF_REPLACE /* use our functions only */
88 #include <curl/mprintf.h>
89
90 #include "curl_memory.h"
91 /* The last #include file should be: */
92 #include "memdebug.h"
93
94 #ifdef WIN32
95 #  undef  PATH_MAX
96 #  define PATH_MAX MAX_PATH
97 #endif
98
99 #ifndef PATH_MAX
100 #define PATH_MAX 1024 /* just an extra precaution since there are systems that
101                          have their definition hidden well */
102 #endif
103
104 #define sftp_libssh2_last_error(s) curlx_ultosi(libssh2_sftp_last_error(s))
105
106 #define sftp_libssh2_realpath(s,p,t,m) \
107         libssh2_sftp_symlink_ex((s), (p), curlx_uztoui(strlen(p)), \
108                                 (t), (m), LIBSSH2_SFTP_REALPATH)
109
110 /* Local functions: */
111 static const char *sftp_libssh2_strerror(int err);
112 static LIBSSH2_ALLOC_FUNC(my_libssh2_malloc);
113 static LIBSSH2_REALLOC_FUNC(my_libssh2_realloc);
114 static LIBSSH2_FREE_FUNC(my_libssh2_free);
115
116 static CURLcode get_pathname(const char **cpp, char **path);
117
118 static CURLcode ssh_connect(struct connectdata *conn, bool *done);
119 static CURLcode ssh_multi_statemach(struct connectdata *conn, bool *done);
120 static CURLcode ssh_do(struct connectdata *conn, bool *done);
121
122 static CURLcode ssh_getworkingpath(struct connectdata *conn,
123                                    char *homedir, /* when SFTP is used */
124                                    char **path);
125
126 static CURLcode scp_done(struct connectdata *conn,
127                          CURLcode, bool premature);
128 static CURLcode scp_doing(struct connectdata *conn,
129                           bool *dophase_done);
130 static CURLcode scp_disconnect(struct connectdata *conn, bool dead_connection);
131
132 static CURLcode sftp_done(struct connectdata *conn,
133                           CURLcode, bool premature);
134 static CURLcode sftp_doing(struct connectdata *conn,
135                            bool *dophase_done);
136 static CURLcode sftp_disconnect(struct connectdata *conn, bool dead);
137 static
138 CURLcode sftp_perform(struct connectdata *conn,
139                       bool *connected,
140                       bool *dophase_done);
141
142 static int ssh_getsock(struct connectdata *conn,
143                        curl_socket_t *sock, /* points to numsocks number
144                                                of sockets */
145                        int numsocks);
146
147 static int ssh_perform_getsock(const struct connectdata *conn,
148                                curl_socket_t *sock, /* points to numsocks
149                                                        number of sockets */
150                                int numsocks);
151
152 static CURLcode ssh_setup_connection(struct connectdata *conn);
153
154 /*
155  * SCP protocol handler.
156  */
157
158 const struct Curl_handler Curl_handler_scp = {
159   "SCP",                                /* scheme */
160   ssh_setup_connection,                 /* setup_connection */
161   ssh_do,                               /* do_it */
162   scp_done,                             /* done */
163   ZERO_NULL,                            /* do_more */
164   ssh_connect,                          /* connect_it */
165   ssh_multi_statemach,                  /* connecting */
166   scp_doing,                            /* doing */
167   ssh_getsock,                          /* proto_getsock */
168   ssh_getsock,                          /* doing_getsock */
169   ZERO_NULL,                            /* domore_getsock */
170   ssh_perform_getsock,                  /* perform_getsock */
171   scp_disconnect,                       /* disconnect */
172   ZERO_NULL,                            /* readwrite */
173   PORT_SSH,                             /* defport */
174   CURLPROTO_SCP,                        /* protocol */
175   PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
176   | PROTOPT_NOURLQUERY                  /* flags */
177 };
178
179
180 /*
181  * SFTP protocol handler.
182  */
183
184 const struct Curl_handler Curl_handler_sftp = {
185   "SFTP",                               /* scheme */
186   ssh_setup_connection,                 /* setup_connection */
187   ssh_do,                               /* do_it */
188   sftp_done,                            /* done */
189   ZERO_NULL,                            /* do_more */
190   ssh_connect,                          /* connect_it */
191   ssh_multi_statemach,                  /* connecting */
192   sftp_doing,                           /* doing */
193   ssh_getsock,                          /* proto_getsock */
194   ssh_getsock,                          /* doing_getsock */
195   ZERO_NULL,                            /* domore_getsock */
196   ssh_perform_getsock,                  /* perform_getsock */
197   sftp_disconnect,                      /* disconnect */
198   ZERO_NULL,                            /* readwrite */
199   PORT_SSH,                             /* defport */
200   CURLPROTO_SFTP,                       /* protocol */
201   PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
202   | PROTOPT_NOURLQUERY                  /* flags */
203 };
204
205 static void
206 kbd_callback(const char *name, int name_len, const char *instruction,
207              int instruction_len, int num_prompts,
208              const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
209              LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
210              void **abstract)
211 {
212   struct connectdata *conn = (struct connectdata *)*abstract;
213
214 #ifdef CURL_LIBSSH2_DEBUG
215   fprintf(stderr, "name=%s\n", name);
216   fprintf(stderr, "name_len=%d\n", name_len);
217   fprintf(stderr, "instruction=%s\n", instruction);
218   fprintf(stderr, "instruction_len=%d\n", instruction_len);
219   fprintf(stderr, "num_prompts=%d\n", num_prompts);
220 #else
221   (void)name;
222   (void)name_len;
223   (void)instruction;
224   (void)instruction_len;
225 #endif  /* CURL_LIBSSH2_DEBUG */
226   if(num_prompts == 1) {
227     responses[0].text = strdup(conn->passwd);
228     responses[0].length = curlx_uztoui(strlen(conn->passwd));
229   }
230   (void)prompts;
231   (void)abstract;
232 } /* kbd_callback */
233
234 static CURLcode sftp_libssh2_error_to_CURLE(int err)
235 {
236   switch (err) {
237     case LIBSSH2_FX_OK:
238       return CURLE_OK;
239
240     case LIBSSH2_FX_NO_SUCH_FILE:
241     case LIBSSH2_FX_NO_SUCH_PATH:
242       return CURLE_REMOTE_FILE_NOT_FOUND;
243
244     case LIBSSH2_FX_PERMISSION_DENIED:
245     case LIBSSH2_FX_WRITE_PROTECT:
246     case LIBSSH2_FX_LOCK_CONFlICT:
247       return CURLE_REMOTE_ACCESS_DENIED;
248
249     case LIBSSH2_FX_NO_SPACE_ON_FILESYSTEM:
250     case LIBSSH2_FX_QUOTA_EXCEEDED:
251       return CURLE_REMOTE_DISK_FULL;
252
253     case LIBSSH2_FX_FILE_ALREADY_EXISTS:
254       return CURLE_REMOTE_FILE_EXISTS;
255
256     case LIBSSH2_FX_DIR_NOT_EMPTY:
257       return CURLE_QUOTE_ERROR;
258
259     default:
260       break;
261   }
262
263   return CURLE_SSH;
264 }
265
266 static CURLcode libssh2_session_error_to_CURLE(int err)
267 {
268   switch (err) {
269     /* Ordered by order of appearance in libssh2.h */
270     case LIBSSH2_ERROR_NONE:
271       return CURLE_OK;
272
273     case LIBSSH2_ERROR_SOCKET_NONE:
274       return CURLE_COULDNT_CONNECT;
275
276     case LIBSSH2_ERROR_ALLOC:
277       return CURLE_OUT_OF_MEMORY;
278
279     case LIBSSH2_ERROR_SOCKET_SEND:
280       return CURLE_SEND_ERROR;
281
282     case LIBSSH2_ERROR_HOSTKEY_INIT:
283     case LIBSSH2_ERROR_HOSTKEY_SIGN:
284     case LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED:
285     case LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED:
286       return CURLE_PEER_FAILED_VERIFICATION;
287
288     case LIBSSH2_ERROR_PASSWORD_EXPIRED:
289       return CURLE_LOGIN_DENIED;
290
291     case LIBSSH2_ERROR_SOCKET_TIMEOUT:
292     case LIBSSH2_ERROR_TIMEOUT:
293       return CURLE_OPERATION_TIMEDOUT;
294
295     case LIBSSH2_ERROR_EAGAIN:
296       return CURLE_AGAIN;
297   }
298
299   /* TODO: map some more of the libssh2 errors to the more appropriate CURLcode
300      error code, and possibly add a few new SSH-related one. We must however
301      not return or even depend on libssh2 errors in the public libcurl API */
302
303   return CURLE_SSH;
304 }
305
306 static LIBSSH2_ALLOC_FUNC(my_libssh2_malloc)
307 {
308   (void)abstract; /* arg not used */
309   return malloc(count);
310 }
311
312 static LIBSSH2_REALLOC_FUNC(my_libssh2_realloc)
313 {
314   (void)abstract; /* arg not used */
315   return realloc(ptr, count);
316 }
317
318 static LIBSSH2_FREE_FUNC(my_libssh2_free)
319 {
320   (void)abstract; /* arg not used */
321   if(ptr) /* ssh2 agent sometimes call free with null ptr */
322     free(ptr);
323 }
324
325 /*
326  * SSH State machine related code
327  */
328 /* This is the ONLY way to change SSH state! */
329 static void state(struct connectdata *conn, sshstate nowstate)
330 {
331 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
332   /* for debug purposes */
333   static const char * const names[] = {
334     "SSH_STOP",
335     "SSH_INIT",
336     "SSH_S_STARTUP",
337     "SSH_HOSTKEY",
338     "SSH_AUTHLIST",
339     "SSH_AUTH_PKEY_INIT",
340     "SSH_AUTH_PKEY",
341     "SSH_AUTH_PASS_INIT",
342     "SSH_AUTH_PASS",
343     "SSH_AUTH_AGENT_INIT",
344     "SSH_AUTH_AGENT_LIST",
345     "SSH_AUTH_AGENT",
346     "SSH_AUTH_HOST_INIT",
347     "SSH_AUTH_HOST",
348     "SSH_AUTH_KEY_INIT",
349     "SSH_AUTH_KEY",
350     "SSH_AUTH_DONE",
351     "SSH_SFTP_INIT",
352     "SSH_SFTP_REALPATH",
353     "SSH_SFTP_QUOTE_INIT",
354     "SSH_SFTP_POSTQUOTE_INIT",
355     "SSH_SFTP_QUOTE",
356     "SSH_SFTP_NEXT_QUOTE",
357     "SSH_SFTP_QUOTE_STAT",
358     "SSH_SFTP_QUOTE_SETSTAT",
359     "SSH_SFTP_QUOTE_SYMLINK",
360     "SSH_SFTP_QUOTE_MKDIR",
361     "SSH_SFTP_QUOTE_RENAME",
362     "SSH_SFTP_QUOTE_RMDIR",
363     "SSH_SFTP_QUOTE_UNLINK",
364     "SSH_SFTP_TRANS_INIT",
365     "SSH_SFTP_UPLOAD_INIT",
366     "SSH_SFTP_CREATE_DIRS_INIT",
367     "SSH_SFTP_CREATE_DIRS",
368     "SSH_SFTP_CREATE_DIRS_MKDIR",
369     "SSH_SFTP_READDIR_INIT",
370     "SSH_SFTP_READDIR",
371     "SSH_SFTP_READDIR_LINK",
372     "SSH_SFTP_READDIR_BOTTOM",
373     "SSH_SFTP_READDIR_DONE",
374     "SSH_SFTP_DOWNLOAD_INIT",
375     "SSH_SFTP_DOWNLOAD_STAT",
376     "SSH_SFTP_CLOSE",
377     "SSH_SFTP_SHUTDOWN",
378     "SSH_SCP_TRANS_INIT",
379     "SSH_SCP_UPLOAD_INIT",
380     "SSH_SCP_DOWNLOAD_INIT",
381     "SSH_SCP_DONE",
382     "SSH_SCP_SEND_EOF",
383     "SSH_SCP_WAIT_EOF",
384     "SSH_SCP_WAIT_CLOSE",
385     "SSH_SCP_CHANNEL_FREE",
386     "SSH_SESSION_DISCONNECT",
387     "SSH_SESSION_FREE",
388     "QUIT"
389   };
390 #endif
391   struct ssh_conn *sshc = &conn->proto.sshc;
392
393 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
394   if(sshc->state != nowstate) {
395     infof(conn->data, "SFTP %p state change from %s to %s\n",
396           (void *)sshc, names[sshc->state], names[nowstate]);
397   }
398 #endif
399
400   sshc->state = nowstate;
401 }
402
403 /* figure out the path to work with in this particular request */
404 static CURLcode ssh_getworkingpath(struct connectdata *conn,
405                                    char *homedir,  /* when SFTP is used */
406                                    char **path) /* returns the  allocated
407                                                    real path to work with */
408 {
409   struct SessionHandle *data = conn->data;
410   char *real_path = NULL;
411   char *working_path;
412   int working_path_len;
413
414   working_path = curl_easy_unescape(data, data->state.path, 0,
415                                     &working_path_len);
416   if(!working_path)
417     return CURLE_OUT_OF_MEMORY;
418
419   /* Check for /~/ , indicating relative to the user's home directory */
420   if(conn->handler->protocol & CURLPROTO_SCP) {
421     real_path = malloc(working_path_len+1);
422     if(real_path == NULL) {
423       free(working_path);
424       return CURLE_OUT_OF_MEMORY;
425     }
426     if((working_path_len > 3) && (!memcmp(working_path, "/~/", 3)))
427       /* It is referenced to the home directory, so strip the leading '/~/' */
428       memcpy(real_path, working_path+3, 4 + working_path_len-3);
429     else
430       memcpy(real_path, working_path, 1 + working_path_len);
431   }
432   else if(conn->handler->protocol & CURLPROTO_SFTP) {
433     if((working_path_len > 1) && (working_path[1] == '~')) {
434       size_t homelen = strlen(homedir);
435       real_path = malloc(homelen + working_path_len + 1);
436       if(real_path == NULL) {
437         free(working_path);
438         return CURLE_OUT_OF_MEMORY;
439       }
440       /* It is referenced to the home directory, so strip the
441          leading '/' */
442       memcpy(real_path, homedir, homelen);
443       real_path[homelen] = '/';
444       real_path[homelen+1] = '\0';
445       if(working_path_len > 3) {
446         memcpy(real_path+homelen+1, working_path + 3,
447                1 + working_path_len -3);
448       }
449     }
450     else {
451       real_path = malloc(working_path_len+1);
452       if(real_path == NULL) {
453         free(working_path);
454         return CURLE_OUT_OF_MEMORY;
455       }
456       memcpy(real_path, working_path, 1+working_path_len);
457     }
458   }
459
460   free(working_path);
461
462   /* store the pointer for the caller to receive */
463   *path = real_path;
464
465   return CURLE_OK;
466 }
467
468 #ifdef HAVE_LIBSSH2_KNOWNHOST_API
469 static int sshkeycallback(CURL *easy,
470                           const struct curl_khkey *knownkey, /* known */
471                           const struct curl_khkey *foundkey, /* found */
472                           enum curl_khmatch match,
473                           void *clientp)
474 {
475   (void)easy;
476   (void)knownkey;
477   (void)foundkey;
478   (void)clientp;
479
480   /* we only allow perfect matches, and we reject everything else */
481   return (match != CURLKHMATCH_OK)?CURLKHSTAT_REJECT:CURLKHSTAT_FINE;
482 }
483 #endif
484
485 /*
486  * Earlier libssh2 versions didn't have the ability to seek to 64bit positions
487  * with 32bit size_t.
488  */
489 #ifdef HAVE_LIBSSH2_SFTP_SEEK64
490 #define SFTP_SEEK(x,y) libssh2_sftp_seek64(x, (libssh2_uint64_t)y)
491 #else
492 #define SFTP_SEEK(x,y) libssh2_sftp_seek(x, (size_t)y)
493 #endif
494
495 /*
496  * Earlier libssh2 versions didn't do SCP properly beyond 32bit sizes on 32bit
497  * architectures so we check of the necessary function is present.
498  */
499 #ifndef HAVE_LIBSSH2_SCP_SEND64
500 #define SCP_SEND(a,b,c,d) libssh2_scp_send_ex(a, b, (int)(c), (size_t)d, 0, 0)
501 #else
502 #define SCP_SEND(a,b,c,d) libssh2_scp_send64(a, b, (int)(c),            \
503                                              (libssh2_uint64_t)d, 0, 0)
504 #endif
505
506 /*
507  * libssh2 1.2.8 fixed the problem with 32bit ints used for sockets on win64.
508  */
509 #ifdef HAVE_LIBSSH2_SESSION_HANDSHAKE
510 #define libssh2_session_startup(x,y) libssh2_session_handshake(x,y)
511 #endif
512
513 static CURLcode ssh_knownhost(struct connectdata *conn)
514 {
515   CURLcode result = CURLE_OK;
516
517 #ifdef HAVE_LIBSSH2_KNOWNHOST_API
518   struct SessionHandle *data = conn->data;
519
520   if(data->set.str[STRING_SSH_KNOWNHOSTS]) {
521     /* we're asked to verify the host against a file */
522     struct ssh_conn *sshc = &conn->proto.sshc;
523     int rc;
524     int keytype;
525     size_t keylen;
526     const char *remotekey = libssh2_session_hostkey(sshc->ssh_session,
527                                                     &keylen, &keytype);
528     int keycheck = LIBSSH2_KNOWNHOST_CHECK_FAILURE;
529     int keybit = 0;
530
531     if(remotekey) {
532       /*
533        * A subject to figure out is what host name we need to pass in here.
534        * What host name does OpenSSH store in its file if an IDN name is
535        * used?
536        */
537       struct libssh2_knownhost *host;
538       enum curl_khmatch keymatch;
539       curl_sshkeycallback func =
540         data->set.ssh_keyfunc?data->set.ssh_keyfunc:sshkeycallback;
541       struct curl_khkey knownkey;
542       struct curl_khkey *knownkeyp = NULL;
543       struct curl_khkey foundkey;
544
545       keybit = (keytype == LIBSSH2_HOSTKEY_TYPE_RSA)?
546         LIBSSH2_KNOWNHOST_KEY_SSHRSA:LIBSSH2_KNOWNHOST_KEY_SSHDSS;
547
548       keycheck = libssh2_knownhost_check(sshc->kh,
549                                          conn->host.name,
550                                          remotekey, keylen,
551                                          LIBSSH2_KNOWNHOST_TYPE_PLAIN|
552                                          LIBSSH2_KNOWNHOST_KEYENC_RAW|
553                                          keybit,
554                                          &host);
555
556       infof(data, "SSH host check: %d, key: %s\n", keycheck,
557             (keycheck <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH)?
558             host->key:"<none>");
559
560       /* setup 'knownkey' */
561       if(keycheck <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH) {
562         knownkey.key = host->key;
563         knownkey.len = 0;
564         knownkey.keytype = (keytype == LIBSSH2_HOSTKEY_TYPE_RSA)?
565           CURLKHTYPE_RSA : CURLKHTYPE_DSS;
566         knownkeyp = &knownkey;
567       }
568
569       /* setup 'foundkey' */
570       foundkey.key = remotekey;
571       foundkey.len = keylen;
572       foundkey.keytype = (keytype == LIBSSH2_HOSTKEY_TYPE_RSA)?
573         CURLKHTYPE_RSA : CURLKHTYPE_DSS;
574
575       /*
576        * if any of the LIBSSH2_KNOWNHOST_CHECK_* defines and the
577        * curl_khmatch enum are ever modified, we need to introduce a
578        * translation table here!
579        */
580       keymatch = (enum curl_khmatch)keycheck;
581
582       /* Ask the callback how to behave */
583       rc = func(data, knownkeyp, /* from the knownhosts file */
584                 &foundkey, /* from the remote host */
585                 keymatch, data->set.ssh_keyfunc_userp);
586     }
587     else
588       /* no remotekey means failure! */
589       rc = CURLKHSTAT_REJECT;
590
591     switch(rc) {
592     default: /* unknown return codes will equal reject */
593     case CURLKHSTAT_REJECT:
594       state(conn, SSH_SESSION_FREE);
595     case CURLKHSTAT_DEFER:
596       /* DEFER means bail out but keep the SSH_HOSTKEY state */
597       result = sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION;
598       break;
599     case CURLKHSTAT_FINE:
600     case CURLKHSTAT_FINE_ADD_TO_FILE:
601       /* proceed */
602       if(keycheck != LIBSSH2_KNOWNHOST_CHECK_MATCH) {
603         /* the found host+key didn't match but has been told to be fine
604            anyway so we add it in memory */
605         int addrc = libssh2_knownhost_add(sshc->kh,
606                                           conn->host.name, NULL,
607                                           remotekey, keylen,
608                                           LIBSSH2_KNOWNHOST_TYPE_PLAIN|
609                                           LIBSSH2_KNOWNHOST_KEYENC_RAW|
610                                           keybit, NULL);
611         if(addrc)
612           infof(data, "Warning adding the known host %s failed!\n",
613                 conn->host.name);
614         else if(rc == CURLKHSTAT_FINE_ADD_TO_FILE) {
615           /* now we write the entire in-memory list of known hosts to the
616              known_hosts file */
617           int wrc =
618             libssh2_knownhost_writefile(sshc->kh,
619                                         data->set.str[STRING_SSH_KNOWNHOSTS],
620                                         LIBSSH2_KNOWNHOST_FILE_OPENSSH);
621           if(wrc) {
622             infof(data, "Warning, writing %s failed!\n",
623                   data->set.str[STRING_SSH_KNOWNHOSTS]);
624           }
625         }
626       }
627       break;
628     }
629   }
630 #else /* HAVE_LIBSSH2_KNOWNHOST_API */
631   (void)conn;
632 #endif
633   return result;
634 }
635
636 static CURLcode ssh_check_fingerprint(struct connectdata *conn)
637 {
638   struct ssh_conn *sshc = &conn->proto.sshc;
639   struct SessionHandle *data = conn->data;
640   const char *pubkey_md5 = data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5];
641   char md5buffer[33];
642   int i;
643
644   const char *fingerprint = libssh2_hostkey_hash(sshc->ssh_session,
645       LIBSSH2_HOSTKEY_HASH_MD5);
646
647   if(fingerprint) {
648     /* The fingerprint points to static storage (!), don't free() it. */
649     for(i = 0; i < 16; i++)
650       snprintf(&md5buffer[i*2], 3, "%02x", (unsigned char) fingerprint[i]);
651     infof(data, "SSH MD5 fingerprint: %s\n", md5buffer);
652   }
653
654   /* Before we authenticate we check the hostkey's MD5 fingerprint
655    * against a known fingerprint, if available.
656    */
657   if(pubkey_md5 && strlen(pubkey_md5) == 32) {
658     if(!fingerprint || !strequal(md5buffer, pubkey_md5)) {
659       if(fingerprint)
660         failf(data,
661             "Denied establishing ssh session: mismatch md5 fingerprint. "
662             "Remote %s is not equal to %s", md5buffer, pubkey_md5);
663       else
664         failf(data,
665             "Denied establishing ssh session: md5 fingerprint not available");
666       state(conn, SSH_SESSION_FREE);
667       sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION;
668       return sshc->actualcode;
669     }
670     else {
671       infof(data, "MD5 checksum match!\n");
672       /* as we already matched, we skip the check for known hosts */
673       return CURLE_OK;
674     }
675   }
676   else
677     return ssh_knownhost(conn);
678 }
679
680 /*
681  * ssh_statemach_act() runs the SSH state machine as far as it can without
682  * blocking and without reaching the end.  The data the pointer 'block' points
683  * to will be set to TRUE if the libssh2 function returns LIBSSH2_ERROR_EAGAIN
684  * meaning it wants to be called again when the socket is ready
685  */
686
687 static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
688 {
689   CURLcode result = CURLE_OK;
690   struct SessionHandle *data = conn->data;
691   struct SSHPROTO *sftp_scp = data->req.protop;
692   struct ssh_conn *sshc = &conn->proto.sshc;
693   curl_socket_t sock = conn->sock[FIRSTSOCKET];
694   char *new_readdir_line;
695   int rc = LIBSSH2_ERROR_NONE;
696   int err;
697   int seekerr = CURL_SEEKFUNC_OK;
698   *block = 0; /* we're not blocking by default */
699
700   do {
701
702     switch(sshc->state) {
703     case SSH_INIT:
704       sshc->secondCreateDirs = 0;
705       sshc->nextstate = SSH_NO_STATE;
706       sshc->actualcode = CURLE_OK;
707
708       /* Set libssh2 to non-blocking, since everything internally is
709          non-blocking */
710       libssh2_session_set_blocking(sshc->ssh_session, 0);
711
712       state(conn, SSH_S_STARTUP);
713       /* fall-through */
714
715     case SSH_S_STARTUP:
716       rc = libssh2_session_startup(sshc->ssh_session, (int)sock);
717       if(rc == LIBSSH2_ERROR_EAGAIN) {
718         break;
719       }
720       else if(rc) {
721         failf(data, "Failure establishing ssh session");
722         state(conn, SSH_SESSION_FREE);
723         sshc->actualcode = CURLE_FAILED_INIT;
724         break;
725       }
726
727       state(conn, SSH_HOSTKEY);
728
729       /* fall-through */
730     case SSH_HOSTKEY:
731       /*
732        * Before we authenticate we should check the hostkey's fingerprint
733        * against our known hosts. How that is handled (reading from file,
734        * whatever) is up to us.
735        */
736       result = ssh_check_fingerprint(conn);
737       if(result == CURLE_OK)
738         state(conn, SSH_AUTHLIST);
739       break;
740
741     case SSH_AUTHLIST:
742       /*
743        * Figure out authentication methods
744        * NB: As soon as we have provided a username to an openssh server we
745        * must never change it later. Thus, always specify the correct username
746        * here, even though the libssh2 docs kind of indicate that it should be
747        * possible to get a 'generic' list (not user-specific) of authentication
748        * methods, presumably with a blank username. That won't work in my
749        * experience.
750        * So always specify it here.
751        */
752       sshc->authlist = libssh2_userauth_list(sshc->ssh_session,
753                                              conn->user,
754                                              curlx_uztoui(strlen(conn->user)));
755
756       if(!sshc->authlist) {
757         if((err = libssh2_session_last_errno(sshc->ssh_session)) ==
758            LIBSSH2_ERROR_EAGAIN) {
759           rc = LIBSSH2_ERROR_EAGAIN;
760           break;
761         }
762         else {
763           state(conn, SSH_SESSION_FREE);
764           sshc->actualcode = libssh2_session_error_to_CURLE(err);
765           break;
766         }
767       }
768       infof(data, "SSH authentication methods available: %s\n",
769             sshc->authlist);
770
771       state(conn, SSH_AUTH_PKEY_INIT);
772       break;
773
774     case SSH_AUTH_PKEY_INIT:
775       /*
776        * Check the supported auth types in the order I feel is most secure
777        * with the requested type of authentication
778        */
779       sshc->authed = FALSE;
780
781       if((data->set.ssh_auth_types & CURLSSH_AUTH_PUBLICKEY) &&
782          (strstr(sshc->authlist, "publickey") != NULL)) {
783         char *home = NULL;
784         bool rsa_pub_empty_but_ok = FALSE;
785
786         sshc->rsa_pub = sshc->rsa = NULL;
787
788         /* To ponder about: should really the lib be messing about with the
789            HOME environment variable etc? */
790         home = curl_getenv("HOME");
791
792         if(data->set.str[STRING_SSH_PUBLIC_KEY] &&
793            !*data->set.str[STRING_SSH_PUBLIC_KEY])
794            rsa_pub_empty_but_ok = true;
795         else if(data->set.str[STRING_SSH_PUBLIC_KEY])
796           sshc->rsa_pub = aprintf("%s", data->set.str[STRING_SSH_PUBLIC_KEY]);
797         else if(home)
798           sshc->rsa_pub = aprintf("%s/.ssh/id_dsa.pub", home);
799         else
800           /* as a final resort, try current dir! */
801           sshc->rsa_pub = strdup("id_dsa.pub");
802
803         if(!rsa_pub_empty_but_ok && (sshc->rsa_pub == NULL)) {
804           Curl_safefree(home);
805           state(conn, SSH_SESSION_FREE);
806           sshc->actualcode = CURLE_OUT_OF_MEMORY;
807           break;
808         }
809
810         if(data->set.str[STRING_SSH_PRIVATE_KEY])
811           sshc->rsa = aprintf("%s", data->set.str[STRING_SSH_PRIVATE_KEY]);
812         else if(home)
813           sshc->rsa = aprintf("%s/.ssh/id_dsa", home);
814         else
815           /* as a final resort, try current dir! */
816           sshc->rsa = strdup("id_dsa");
817
818         if(sshc->rsa == NULL) {
819           Curl_safefree(home);
820           Curl_safefree(sshc->rsa_pub);
821           state(conn, SSH_SESSION_FREE);
822           sshc->actualcode = CURLE_OUT_OF_MEMORY;
823           break;
824         }
825
826         sshc->passphrase = data->set.str[STRING_KEY_PASSWD];
827         if(!sshc->passphrase)
828           sshc->passphrase = "";
829
830         Curl_safefree(home);
831
832         infof(data, "Using ssh public key file %s\n", sshc->rsa_pub);
833         infof(data, "Using ssh private key file %s\n", sshc->rsa);
834
835         state(conn, SSH_AUTH_PKEY);
836       }
837       else {
838         state(conn, SSH_AUTH_PASS_INIT);
839       }
840       break;
841
842     case SSH_AUTH_PKEY:
843       /* The function below checks if the files exists, no need to stat() here.
844        */
845       rc = libssh2_userauth_publickey_fromfile_ex(sshc->ssh_session,
846                                                   conn->user,
847                                                   curlx_uztoui(
848                                                     strlen(conn->user)),
849                                                   sshc->rsa_pub,
850                                                   sshc->rsa, sshc->passphrase);
851       if(rc == LIBSSH2_ERROR_EAGAIN) {
852         break;
853       }
854
855       Curl_safefree(sshc->rsa_pub);
856       Curl_safefree(sshc->rsa);
857
858       if(rc == 0) {
859         sshc->authed = TRUE;
860         infof(data, "Initialized SSH public key authentication\n");
861         state(conn, SSH_AUTH_DONE);
862       }
863       else {
864         char *err_msg;
865         (void)libssh2_session_last_error(sshc->ssh_session,
866                                          &err_msg, NULL, 0);
867         infof(data, "SSH public key authentication failed: %s\n", err_msg);
868         state(conn, SSH_AUTH_PASS_INIT);
869       }
870       break;
871
872     case SSH_AUTH_PASS_INIT:
873       if((data->set.ssh_auth_types & CURLSSH_AUTH_PASSWORD) &&
874          (strstr(sshc->authlist, "password") != NULL)) {
875         state(conn, SSH_AUTH_PASS);
876       }
877       else {
878         state(conn, SSH_AUTH_HOST_INIT);
879       }
880       break;
881
882     case SSH_AUTH_PASS:
883       rc = libssh2_userauth_password_ex(sshc->ssh_session, conn->user,
884                                         curlx_uztoui(strlen(conn->user)),
885                                         conn->passwd,
886                                         curlx_uztoui(strlen(conn->passwd)),
887                                         NULL);
888       if(rc == LIBSSH2_ERROR_EAGAIN) {
889         break;
890       }
891       else if(rc == 0) {
892         sshc->authed = TRUE;
893         infof(data, "Initialized password authentication\n");
894         state(conn, SSH_AUTH_DONE);
895       }
896       else {
897         state(conn, SSH_AUTH_HOST_INIT);
898       }
899       break;
900
901     case SSH_AUTH_HOST_INIT:
902       if((data->set.ssh_auth_types & CURLSSH_AUTH_HOST) &&
903          (strstr(sshc->authlist, "hostbased") != NULL)) {
904         state(conn, SSH_AUTH_HOST);
905       }
906       else {
907         state(conn, SSH_AUTH_AGENT_INIT);
908       }
909       break;
910
911     case SSH_AUTH_HOST:
912       state(conn, SSH_AUTH_AGENT_INIT);
913       break;
914
915     case SSH_AUTH_AGENT_INIT:
916 #ifdef HAVE_LIBSSH2_AGENT_API
917       if((data->set.ssh_auth_types & CURLSSH_AUTH_AGENT)
918          && (strstr(sshc->authlist, "publickey") != NULL)) {
919
920         /* Connect to the ssh-agent */
921         /* The agent could be shared by a curl thread i believe
922            but nothing obvious as keys can be added/removed at any time */
923         if(!sshc->ssh_agent) {
924           sshc->ssh_agent = libssh2_agent_init(sshc->ssh_session);
925           if(!sshc->ssh_agent) {
926             infof(data, "Could not create agent object\n");
927
928             state(conn, SSH_AUTH_KEY_INIT);
929           }
930         }
931
932         rc = libssh2_agent_connect(sshc->ssh_agent);
933         if(rc == LIBSSH2_ERROR_EAGAIN)
934           break;
935         if(rc < 0) {
936           infof(data, "Failure connecting to agent\n");
937           state(conn, SSH_AUTH_KEY_INIT);
938         }
939         else {
940           state(conn, SSH_AUTH_AGENT_LIST);
941         }
942       }
943       else
944 #endif /* HAVE_LIBSSH2_AGENT_API */
945         state(conn, SSH_AUTH_KEY_INIT);
946       break;
947
948     case SSH_AUTH_AGENT_LIST:
949 #ifdef HAVE_LIBSSH2_AGENT_API
950       rc = libssh2_agent_list_identities(sshc->ssh_agent);
951
952       if(rc == LIBSSH2_ERROR_EAGAIN)
953         break;
954       if(rc < 0) {
955         infof(data, "Failure requesting identities to agent\n");
956         state(conn, SSH_AUTH_KEY_INIT);
957       }
958       else {
959         state(conn, SSH_AUTH_AGENT);
960         sshc->sshagent_prev_identity = NULL;
961       }
962 #endif
963       break;
964
965     case SSH_AUTH_AGENT:
966 #ifdef HAVE_LIBSSH2_AGENT_API
967       /* as prev_identity evolves only after an identity user auth finished we
968          can safely request it again as long as EAGAIN is returned here or by
969          libssh2_agent_userauth */
970       rc = libssh2_agent_get_identity(sshc->ssh_agent,
971                                       &sshc->sshagent_identity,
972                                       sshc->sshagent_prev_identity);
973       if(rc == LIBSSH2_ERROR_EAGAIN)
974         break;
975
976       if(rc == 0) {
977         rc = libssh2_agent_userauth(sshc->ssh_agent, conn->user,
978                                     sshc->sshagent_identity);
979
980         if(rc < 0) {
981           if(rc != LIBSSH2_ERROR_EAGAIN) {
982             /* tried and failed? go to next identity */
983             sshc->sshagent_prev_identity = sshc->sshagent_identity;
984           }
985           break;
986         }
987       }
988
989       if(rc < 0)
990         infof(data, "Failure requesting identities to agent\n");
991       else if(rc == 1)
992         infof(data, "No identity would match\n");
993
994       if(rc == LIBSSH2_ERROR_NONE) {
995         sshc->authed = TRUE;
996         infof(data, "Agent based authentication successful\n");
997         state(conn, SSH_AUTH_DONE);
998       }
999       else
1000         state(conn, SSH_AUTH_KEY_INIT);
1001 #endif
1002       break;
1003
1004     case SSH_AUTH_KEY_INIT:
1005       if((data->set.ssh_auth_types & CURLSSH_AUTH_KEYBOARD)
1006          && (strstr(sshc->authlist, "keyboard-interactive") != NULL)) {
1007         state(conn, SSH_AUTH_KEY);
1008       }
1009       else {
1010         state(conn, SSH_AUTH_DONE);
1011       }
1012       break;
1013
1014     case SSH_AUTH_KEY:
1015       /* Authentication failed. Continue with keyboard-interactive now. */
1016       rc = libssh2_userauth_keyboard_interactive_ex(sshc->ssh_session,
1017                                                     conn->user,
1018                                                     curlx_uztoui(
1019                                                       strlen(conn->user)),
1020                                                     &kbd_callback);
1021       if(rc == LIBSSH2_ERROR_EAGAIN) {
1022         break;
1023       }
1024       else if(rc == 0) {
1025         sshc->authed = TRUE;
1026         infof(data, "Initialized keyboard interactive authentication\n");
1027       }
1028       state(conn, SSH_AUTH_DONE);
1029       break;
1030
1031     case SSH_AUTH_DONE:
1032       if(!sshc->authed) {
1033         failf(data, "Authentication failure");
1034         state(conn, SSH_SESSION_FREE);
1035         sshc->actualcode = CURLE_LOGIN_DENIED;
1036         break;
1037       }
1038
1039       /*
1040        * At this point we have an authenticated ssh session.
1041        */
1042       infof(data, "Authentication complete\n");
1043
1044       Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSH is connected */
1045
1046       conn->sockfd = sock;
1047       conn->writesockfd = CURL_SOCKET_BAD;
1048
1049       if(conn->handler->protocol == CURLPROTO_SFTP) {
1050         state(conn, SSH_SFTP_INIT);
1051         break;
1052       }
1053       infof(data, "SSH CONNECT phase done\n");
1054       state(conn, SSH_STOP);
1055       break;
1056
1057     case SSH_SFTP_INIT:
1058       /*
1059        * Start the libssh2 sftp session
1060        */
1061       sshc->sftp_session = libssh2_sftp_init(sshc->ssh_session);
1062       if(!sshc->sftp_session) {
1063         if(libssh2_session_last_errno(sshc->ssh_session) ==
1064            LIBSSH2_ERROR_EAGAIN) {
1065           rc = LIBSSH2_ERROR_EAGAIN;
1066           break;
1067         }
1068         else {
1069           char *err_msg;
1070
1071           (void)libssh2_session_last_error(sshc->ssh_session,
1072                                            &err_msg, NULL, 0);
1073           failf(data, "Failure initializing sftp session: %s", err_msg);
1074           state(conn, SSH_SESSION_FREE);
1075           sshc->actualcode = CURLE_FAILED_INIT;
1076           break;
1077         }
1078       }
1079       state(conn, SSH_SFTP_REALPATH);
1080       break;
1081
1082     case SSH_SFTP_REALPATH:
1083     {
1084       char tempHome[PATH_MAX];
1085
1086       /*
1087        * Get the "home" directory
1088        */
1089       rc = sftp_libssh2_realpath(sshc->sftp_session, ".",
1090                                  tempHome, PATH_MAX-1);
1091       if(rc == LIBSSH2_ERROR_EAGAIN) {
1092         break;
1093       }
1094       else if(rc > 0) {
1095         /* It seems that this string is not always NULL terminated */
1096         tempHome[rc] = '\0';
1097         sshc->homedir = strdup(tempHome);
1098         if(!sshc->homedir) {
1099           state(conn, SSH_SFTP_CLOSE);
1100           sshc->actualcode = CURLE_OUT_OF_MEMORY;
1101           break;
1102         }
1103         conn->data->state.most_recent_ftp_entrypath = sshc->homedir;
1104       }
1105       else {
1106         /* Return the error type */
1107         err = sftp_libssh2_last_error(sshc->sftp_session);
1108         result = sftp_libssh2_error_to_CURLE(err);
1109         sshc->actualcode = result?result:CURLE_SSH;
1110         DEBUGF(infof(data, "error = %d makes libcurl = %d\n",
1111                      err, (int)result));
1112         state(conn, SSH_STOP);
1113         break;
1114       }
1115     }
1116     /* This is the last step in the SFTP connect phase. Do note that while
1117        we get the homedir here, we get the "workingpath" in the DO action
1118        since the homedir will remain the same between request but the
1119        working path will not. */
1120     DEBUGF(infof(data, "SSH CONNECT phase done\n"));
1121     state(conn, SSH_STOP);
1122     break;
1123
1124     case SSH_SFTP_QUOTE_INIT:
1125
1126       result = ssh_getworkingpath(conn, sshc->homedir, &sftp_scp->path);
1127       if(result) {
1128         sshc->actualcode = result;
1129         state(conn, SSH_STOP);
1130         break;
1131       }
1132
1133       if(data->set.quote) {
1134         infof(data, "Sending quote commands\n");
1135         sshc->quote_item = data->set.quote;
1136         state(conn, SSH_SFTP_QUOTE);
1137       }
1138       else {
1139         state(conn, SSH_SFTP_TRANS_INIT);
1140       }
1141       break;
1142
1143     case SSH_SFTP_POSTQUOTE_INIT:
1144       if(data->set.postquote) {
1145         infof(data, "Sending quote commands\n");
1146         sshc->quote_item = data->set.postquote;
1147         state(conn, SSH_SFTP_QUOTE);
1148       }
1149       else {
1150         state(conn, SSH_STOP);
1151       }
1152       break;
1153
1154     case SSH_SFTP_QUOTE:
1155       /* Send any quote commands */
1156     {
1157       const char *cp;
1158
1159       /*
1160        * Support some of the "FTP" commands
1161        */
1162       char *cmd = sshc->quote_item->data;
1163       sshc->acceptfail = FALSE;
1164
1165       /* if a command starts with an asterisk, which a legal SFTP command never
1166          can, the command will be allowed to fail without it causing any
1167          aborts or cancels etc. It will cause libcurl to act as if the command
1168          is successful, whatever the server reponds. */
1169
1170       if(cmd[0] == '*') {
1171         cmd++;
1172         sshc->acceptfail = TRUE;
1173       }
1174
1175       if(curl_strequal("pwd", cmd)) {
1176         /* output debug output if that is requested */
1177         char *tmp = aprintf("257 \"%s\" is current directory.\n",
1178                             sftp_scp->path);
1179         if(!tmp) {
1180           result = CURLE_OUT_OF_MEMORY;
1181           state(conn, SSH_SFTP_CLOSE);
1182           sshc->nextstate = SSH_NO_STATE;
1183           break;
1184         }
1185         if(data->set.verbose) {
1186           Curl_debug(data, CURLINFO_HEADER_OUT, (char *)"PWD\n", 4, conn);
1187           Curl_debug(data, CURLINFO_HEADER_IN, tmp, strlen(tmp), conn);
1188         }
1189         /* this sends an FTP-like "header" to the header callback so that the
1190            current directory can be read very similar to how it is read when
1191            using ordinary FTP. */
1192         result = Curl_client_write(conn, CLIENTWRITE_HEADER, tmp, strlen(tmp));
1193         free(tmp);
1194         state(conn, SSH_SFTP_NEXT_QUOTE);
1195         break;
1196       }
1197       else if(cmd) {
1198         /*
1199          * the arguments following the command must be separated from the
1200          * command with a space so we can check for it unconditionally
1201          */
1202         cp = strchr(cmd, ' ');
1203         if(cp == NULL) {
1204           failf(data, "Syntax error in SFTP command. Supply parameter(s)!");
1205           state(conn, SSH_SFTP_CLOSE);
1206           sshc->nextstate = SSH_NO_STATE;
1207           sshc->actualcode = CURLE_QUOTE_ERROR;
1208           break;
1209         }
1210
1211         /*
1212          * also, every command takes at least one argument so we get that
1213          * first argument right now
1214          */
1215         result = get_pathname(&cp, &sshc->quote_path1);
1216         if(result) {
1217           if(result == CURLE_OUT_OF_MEMORY)
1218             failf(data, "Out of memory");
1219           else
1220             failf(data, "Syntax error: Bad first parameter");
1221           state(conn, SSH_SFTP_CLOSE);
1222           sshc->nextstate = SSH_NO_STATE;
1223           sshc->actualcode = result;
1224           break;
1225         }
1226
1227         /*
1228          * SFTP is a binary protocol, so we don't send text commands to
1229          * the server. Instead, we scan for commands for commands used by
1230          * OpenSSH's sftp program and call the appropriate libssh2
1231          * functions.
1232          */
1233         if(curl_strnequal(cmd, "chgrp ", 6) ||
1234            curl_strnequal(cmd, "chmod ", 6) ||
1235            curl_strnequal(cmd, "chown ", 6) ) {
1236           /* attribute change */
1237
1238           /* sshc->quote_path1 contains the mode to set */
1239           /* get the destination */
1240           result = get_pathname(&cp, &sshc->quote_path2);
1241           if(result) {
1242             if(result == CURLE_OUT_OF_MEMORY)
1243               failf(data, "Out of memory");
1244             else
1245               failf(data, "Syntax error in chgrp/chmod/chown: "
1246                     "Bad second parameter");
1247             Curl_safefree(sshc->quote_path1);
1248             state(conn, SSH_SFTP_CLOSE);
1249             sshc->nextstate = SSH_NO_STATE;
1250             sshc->actualcode = result;
1251             break;
1252           }
1253           memset(&sshc->quote_attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
1254           state(conn, SSH_SFTP_QUOTE_STAT);
1255           break;
1256         }
1257         else if(curl_strnequal(cmd, "ln ", 3) ||
1258                 curl_strnequal(cmd, "symlink ", 8)) {
1259           /* symbolic linking */
1260           /* sshc->quote_path1 is the source */
1261           /* get the destination */
1262           result = get_pathname(&cp, &sshc->quote_path2);
1263           if(result) {
1264             if(result == CURLE_OUT_OF_MEMORY)
1265               failf(data, "Out of memory");
1266             else
1267               failf(data,
1268                     "Syntax error in ln/symlink: Bad second parameter");
1269             Curl_safefree(sshc->quote_path1);
1270             state(conn, SSH_SFTP_CLOSE);
1271             sshc->nextstate = SSH_NO_STATE;
1272             sshc->actualcode = result;
1273             break;
1274           }
1275           state(conn, SSH_SFTP_QUOTE_SYMLINK);
1276           break;
1277         }
1278         else if(curl_strnequal(cmd, "mkdir ", 6)) {
1279           /* create dir */
1280           state(conn, SSH_SFTP_QUOTE_MKDIR);
1281           break;
1282         }
1283         else if(curl_strnequal(cmd, "rename ", 7)) {
1284           /* rename file */
1285           /* first param is the source path */
1286           /* second param is the dest. path */
1287           result = get_pathname(&cp, &sshc->quote_path2);
1288           if(result) {
1289             if(result == CURLE_OUT_OF_MEMORY)
1290               failf(data, "Out of memory");
1291             else
1292               failf(data, "Syntax error in rename: Bad second parameter");
1293             Curl_safefree(sshc->quote_path1);
1294             state(conn, SSH_SFTP_CLOSE);
1295             sshc->nextstate = SSH_NO_STATE;
1296             sshc->actualcode = result;
1297             break;
1298           }
1299           state(conn, SSH_SFTP_QUOTE_RENAME);
1300           break;
1301         }
1302         else if(curl_strnequal(cmd, "rmdir ", 6)) {
1303           /* delete dir */
1304           state(conn, SSH_SFTP_QUOTE_RMDIR);
1305           break;
1306         }
1307         else if(curl_strnequal(cmd, "rm ", 3)) {
1308           state(conn, SSH_SFTP_QUOTE_UNLINK);
1309           break;
1310         }
1311
1312         failf(data, "Unknown SFTP command");
1313         Curl_safefree(sshc->quote_path1);
1314         Curl_safefree(sshc->quote_path2);
1315         state(conn, SSH_SFTP_CLOSE);
1316         sshc->nextstate = SSH_NO_STATE;
1317         sshc->actualcode = CURLE_QUOTE_ERROR;
1318         break;
1319       }
1320     }
1321     if(!sshc->quote_item) {
1322       state(conn, SSH_SFTP_TRANS_INIT);
1323     }
1324     break;
1325
1326     case SSH_SFTP_NEXT_QUOTE:
1327       Curl_safefree(sshc->quote_path1);
1328       Curl_safefree(sshc->quote_path2);
1329
1330       sshc->quote_item = sshc->quote_item->next;
1331
1332       if(sshc->quote_item) {
1333         state(conn, SSH_SFTP_QUOTE);
1334       }
1335       else {
1336         if(sshc->nextstate != SSH_NO_STATE) {
1337           state(conn, sshc->nextstate);
1338           sshc->nextstate = SSH_NO_STATE;
1339         }
1340         else {
1341           state(conn, SSH_SFTP_TRANS_INIT);
1342         }
1343       }
1344       break;
1345
1346     case SSH_SFTP_QUOTE_STAT:
1347     {
1348       char *cmd = sshc->quote_item->data;
1349       sshc->acceptfail = FALSE;
1350
1351       /* if a command starts with an asterisk, which a legal SFTP command never
1352          can, the command will be allowed to fail without it causing any
1353          aborts or cancels etc. It will cause libcurl to act as if the command
1354          is successful, whatever the server reponds. */
1355
1356       if(cmd[0] == '*') {
1357         cmd++;
1358         sshc->acceptfail = TRUE;
1359       }
1360
1361       if(!curl_strnequal(cmd, "chmod", 5)) {
1362         /* Since chown and chgrp only set owner OR group but libssh2 wants to
1363          * set them both at once, we need to obtain the current ownership
1364          * first.  This takes an extra protocol round trip.
1365          */
1366         rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshc->quote_path2,
1367                                   curlx_uztoui(strlen(sshc->quote_path2)),
1368                                   LIBSSH2_SFTP_STAT,
1369                                   &sshc->quote_attrs);
1370         if(rc == LIBSSH2_ERROR_EAGAIN) {
1371           break;
1372         }
1373         else if(rc != 0 && !sshc->acceptfail) { /* get those attributes */
1374           err = sftp_libssh2_last_error(sshc->sftp_session);
1375           Curl_safefree(sshc->quote_path1);
1376           Curl_safefree(sshc->quote_path2);
1377           failf(data, "Attempt to get SFTP stats failed: %s",
1378                 sftp_libssh2_strerror(err));
1379           state(conn, SSH_SFTP_CLOSE);
1380           sshc->nextstate = SSH_NO_STATE;
1381           sshc->actualcode = CURLE_QUOTE_ERROR;
1382           break;
1383         }
1384       }
1385
1386       /* Now set the new attributes... */
1387       if(curl_strnequal(cmd, "chgrp", 5)) {
1388         sshc->quote_attrs.gid = strtoul(sshc->quote_path1, NULL, 10);
1389         sshc->quote_attrs.flags = LIBSSH2_SFTP_ATTR_UIDGID;
1390         if(sshc->quote_attrs.gid == 0 && !ISDIGIT(sshc->quote_path1[0]) &&
1391            !sshc->acceptfail) {
1392           Curl_safefree(sshc->quote_path1);
1393           Curl_safefree(sshc->quote_path2);
1394           failf(data, "Syntax error: chgrp gid not a number");
1395           state(conn, SSH_SFTP_CLOSE);
1396           sshc->nextstate = SSH_NO_STATE;
1397           sshc->actualcode = CURLE_QUOTE_ERROR;
1398           break;
1399         }
1400       }
1401       else if(curl_strnequal(cmd, "chmod", 5)) {
1402         sshc->quote_attrs.permissions = strtoul(sshc->quote_path1, NULL, 8);
1403         sshc->quote_attrs.flags = LIBSSH2_SFTP_ATTR_PERMISSIONS;
1404         /* permissions are octal */
1405         if(sshc->quote_attrs.permissions == 0 &&
1406            !ISDIGIT(sshc->quote_path1[0])) {
1407           Curl_safefree(sshc->quote_path1);
1408           Curl_safefree(sshc->quote_path2);
1409           failf(data, "Syntax error: chmod permissions not a number");
1410           state(conn, SSH_SFTP_CLOSE);
1411           sshc->nextstate = SSH_NO_STATE;
1412           sshc->actualcode = CURLE_QUOTE_ERROR;
1413           break;
1414         }
1415       }
1416       else if(curl_strnequal(cmd, "chown", 5)) {
1417         sshc->quote_attrs.uid = strtoul(sshc->quote_path1, NULL, 10);
1418         sshc->quote_attrs.flags = LIBSSH2_SFTP_ATTR_UIDGID;
1419         if(sshc->quote_attrs.uid == 0 && !ISDIGIT(sshc->quote_path1[0]) &&
1420            !sshc->acceptfail) {
1421           Curl_safefree(sshc->quote_path1);
1422           Curl_safefree(sshc->quote_path2);
1423           failf(data, "Syntax error: chown uid not a number");
1424           state(conn, SSH_SFTP_CLOSE);
1425           sshc->nextstate = SSH_NO_STATE;
1426           sshc->actualcode = CURLE_QUOTE_ERROR;
1427           break;
1428         }
1429       }
1430
1431       /* Now send the completed structure... */
1432       state(conn, SSH_SFTP_QUOTE_SETSTAT);
1433       break;
1434     }
1435
1436     case SSH_SFTP_QUOTE_SETSTAT:
1437       rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshc->quote_path2,
1438                                 curlx_uztoui(strlen(sshc->quote_path2)),
1439                                 LIBSSH2_SFTP_SETSTAT,
1440                                 &sshc->quote_attrs);
1441       if(rc == LIBSSH2_ERROR_EAGAIN) {
1442         break;
1443       }
1444       else if(rc != 0 && !sshc->acceptfail) {
1445         err = sftp_libssh2_last_error(sshc->sftp_session);
1446         Curl_safefree(sshc->quote_path1);
1447         Curl_safefree(sshc->quote_path2);
1448         failf(data, "Attempt to set SFTP stats failed: %s",
1449               sftp_libssh2_strerror(err));
1450         state(conn, SSH_SFTP_CLOSE);
1451         sshc->nextstate = SSH_NO_STATE;
1452         sshc->actualcode = CURLE_QUOTE_ERROR;
1453         break;
1454       }
1455       state(conn, SSH_SFTP_NEXT_QUOTE);
1456       break;
1457
1458     case SSH_SFTP_QUOTE_SYMLINK:
1459       rc = libssh2_sftp_symlink_ex(sshc->sftp_session, sshc->quote_path1,
1460                                    curlx_uztoui(strlen(sshc->quote_path1)),
1461                                    sshc->quote_path2,
1462                                    curlx_uztoui(strlen(sshc->quote_path2)),
1463                                    LIBSSH2_SFTP_SYMLINK);
1464       if(rc == LIBSSH2_ERROR_EAGAIN) {
1465         break;
1466       }
1467       else if(rc != 0 && !sshc->acceptfail) {
1468         err = sftp_libssh2_last_error(sshc->sftp_session);
1469         Curl_safefree(sshc->quote_path1);
1470         Curl_safefree(sshc->quote_path2);
1471         failf(data, "symlink command failed: %s",
1472               sftp_libssh2_strerror(err));
1473         state(conn, SSH_SFTP_CLOSE);
1474         sshc->nextstate = SSH_NO_STATE;
1475         sshc->actualcode = CURLE_QUOTE_ERROR;
1476         break;
1477       }
1478       state(conn, SSH_SFTP_NEXT_QUOTE);
1479       break;
1480
1481     case SSH_SFTP_QUOTE_MKDIR:
1482       rc = libssh2_sftp_mkdir_ex(sshc->sftp_session, sshc->quote_path1,
1483                                  curlx_uztoui(strlen(sshc->quote_path1)),
1484                                  data->set.new_directory_perms);
1485       if(rc == LIBSSH2_ERROR_EAGAIN) {
1486         break;
1487       }
1488       else if(rc != 0 && !sshc->acceptfail) {
1489         err = sftp_libssh2_last_error(sshc->sftp_session);
1490         Curl_safefree(sshc->quote_path1);
1491         failf(data, "mkdir command failed: %s", sftp_libssh2_strerror(err));
1492         state(conn, SSH_SFTP_CLOSE);
1493         sshc->nextstate = SSH_NO_STATE;
1494         sshc->actualcode = CURLE_QUOTE_ERROR;
1495         break;
1496       }
1497       state(conn, SSH_SFTP_NEXT_QUOTE);
1498       break;
1499
1500     case SSH_SFTP_QUOTE_RENAME:
1501       rc = libssh2_sftp_rename_ex(sshc->sftp_session, sshc->quote_path1,
1502                                   curlx_uztoui(strlen(sshc->quote_path1)),
1503                                   sshc->quote_path2,
1504                                   curlx_uztoui(strlen(sshc->quote_path2)),
1505                                   LIBSSH2_SFTP_RENAME_OVERWRITE |
1506                                   LIBSSH2_SFTP_RENAME_ATOMIC |
1507                                   LIBSSH2_SFTP_RENAME_NATIVE);
1508
1509       if(rc == LIBSSH2_ERROR_EAGAIN) {
1510         break;
1511       }
1512       else if(rc != 0 && !sshc->acceptfail) {
1513         err = sftp_libssh2_last_error(sshc->sftp_session);
1514         Curl_safefree(sshc->quote_path1);
1515         Curl_safefree(sshc->quote_path2);
1516         failf(data, "rename command failed: %s", sftp_libssh2_strerror(err));
1517         state(conn, SSH_SFTP_CLOSE);
1518         sshc->nextstate = SSH_NO_STATE;
1519         sshc->actualcode = CURLE_QUOTE_ERROR;
1520         break;
1521       }
1522       state(conn, SSH_SFTP_NEXT_QUOTE);
1523       break;
1524
1525     case SSH_SFTP_QUOTE_RMDIR:
1526       rc = libssh2_sftp_rmdir_ex(sshc->sftp_session, sshc->quote_path1,
1527                                  curlx_uztoui(strlen(sshc->quote_path1)));
1528       if(rc == LIBSSH2_ERROR_EAGAIN) {
1529         break;
1530       }
1531       else if(rc != 0 && !sshc->acceptfail) {
1532         err = sftp_libssh2_last_error(sshc->sftp_session);
1533         Curl_safefree(sshc->quote_path1);
1534         failf(data, "rmdir command failed: %s", sftp_libssh2_strerror(err));
1535         state(conn, SSH_SFTP_CLOSE);
1536         sshc->nextstate = SSH_NO_STATE;
1537         sshc->actualcode = CURLE_QUOTE_ERROR;
1538         break;
1539       }
1540       state(conn, SSH_SFTP_NEXT_QUOTE);
1541       break;
1542
1543     case SSH_SFTP_QUOTE_UNLINK:
1544       rc = libssh2_sftp_unlink_ex(sshc->sftp_session, sshc->quote_path1,
1545                                   curlx_uztoui(strlen(sshc->quote_path1)));
1546       if(rc == LIBSSH2_ERROR_EAGAIN) {
1547         break;
1548       }
1549       else if(rc != 0 && !sshc->acceptfail) {
1550         err = sftp_libssh2_last_error(sshc->sftp_session);
1551         Curl_safefree(sshc->quote_path1);
1552         failf(data, "rm command failed: %s", sftp_libssh2_strerror(err));
1553         state(conn, SSH_SFTP_CLOSE);
1554         sshc->nextstate = SSH_NO_STATE;
1555         sshc->actualcode = CURLE_QUOTE_ERROR;
1556         break;
1557       }
1558       state(conn, SSH_SFTP_NEXT_QUOTE);
1559       break;
1560
1561     case SSH_SFTP_TRANS_INIT:
1562       if(data->set.upload)
1563         state(conn, SSH_SFTP_UPLOAD_INIT);
1564       else {
1565         if(data->set.opt_no_body)
1566           state(conn, SSH_STOP);
1567         else if(sftp_scp->path[strlen(sftp_scp->path)-1] == '/')
1568           state(conn, SSH_SFTP_READDIR_INIT);
1569         else
1570           state(conn, SSH_SFTP_DOWNLOAD_INIT);
1571       }
1572       break;
1573
1574     case SSH_SFTP_UPLOAD_INIT:
1575     {
1576       unsigned long flags;
1577       /*
1578        * NOTE!!!  libssh2 requires that the destination path is a full path
1579        *          that includes the destination file and name OR ends in a "/"
1580        *          If this is not done the destination file will be named the
1581        *          same name as the last directory in the path.
1582        */
1583
1584       if(data->state.resume_from != 0) {
1585         LIBSSH2_SFTP_ATTRIBUTES attrs;
1586         if(data->state.resume_from < 0) {
1587           rc = libssh2_sftp_stat_ex(sshc->sftp_session, sftp_scp->path,
1588                                     curlx_uztoui(strlen(sftp_scp->path)),
1589                                     LIBSSH2_SFTP_STAT, &attrs);
1590           if(rc == LIBSSH2_ERROR_EAGAIN) {
1591             break;
1592           }
1593           else if(rc) {
1594             data->state.resume_from = 0;
1595           }
1596           else {
1597             curl_off_t size = attrs.filesize;
1598             if(size < 0) {
1599               failf(data, "Bad file size (%" FORMAT_OFF_T ")", size);
1600               return CURLE_BAD_DOWNLOAD_RESUME;
1601             }
1602             data->state.resume_from = attrs.filesize;
1603           }
1604         }
1605       }
1606
1607       if(data->set.ftp_append)
1608         /* Try to open for append, but create if nonexisting */
1609         flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_APPEND;
1610       else if(data->state.resume_from > 0)
1611         /* If we have restart position then open for append */
1612         flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_APPEND;
1613       else
1614         /* Clear file before writing (normal behaviour) */
1615         flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC;
1616
1617       sshc->sftp_handle =
1618         libssh2_sftp_open_ex(sshc->sftp_session, sftp_scp->path,
1619                              curlx_uztoui(strlen(sftp_scp->path)),
1620                              flags, data->set.new_file_perms,
1621                              LIBSSH2_SFTP_OPENFILE);
1622
1623       if(!sshc->sftp_handle) {
1624         rc = libssh2_session_last_errno(sshc->ssh_session);
1625
1626         if(LIBSSH2_ERROR_EAGAIN == rc)
1627           break;
1628         else {
1629           if(LIBSSH2_ERROR_SFTP_PROTOCOL == rc)
1630             /* only when there was an SFTP protocol error can we extract
1631                the sftp error! */
1632             err = sftp_libssh2_last_error(sshc->sftp_session);
1633           else
1634             err = -1; /* not an sftp error at all */
1635
1636           if(sshc->secondCreateDirs) {
1637             state(conn, SSH_SFTP_CLOSE);
1638             sshc->actualcode = err>= LIBSSH2_FX_OK?
1639               sftp_libssh2_error_to_CURLE(err):CURLE_SSH;
1640             failf(data, "Creating the dir/file failed: %s",
1641                   sftp_libssh2_strerror(err));
1642             break;
1643           }
1644           else if(((err == LIBSSH2_FX_NO_SUCH_FILE) ||
1645                    (err == LIBSSH2_FX_FAILURE) ||
1646                    (err == LIBSSH2_FX_NO_SUCH_PATH)) &&
1647                   (data->set.ftp_create_missing_dirs &&
1648                    (strlen(sftp_scp->path) > 1))) {
1649             /* try to create the path remotely */
1650             sshc->secondCreateDirs = 1;
1651             state(conn, SSH_SFTP_CREATE_DIRS_INIT);
1652             break;
1653           }
1654           state(conn, SSH_SFTP_CLOSE);
1655           sshc->actualcode = err>= LIBSSH2_FX_OK?
1656             sftp_libssh2_error_to_CURLE(err):CURLE_SSH;
1657           if(!sshc->actualcode) {
1658             /* Sometimes, for some reason libssh2_sftp_last_error() returns
1659                zero even though libssh2_sftp_open() failed previously! We need
1660                to work around that! */
1661             sshc->actualcode = CURLE_SSH;
1662             err=-1;
1663           }
1664           failf(data, "Upload failed: %s (%d/%d)",
1665                 err>= LIBSSH2_FX_OK?sftp_libssh2_strerror(err):"ssh error",
1666                 err, rc);
1667           break;
1668         }
1669       }
1670
1671       /* If we have restart point then we need to seek to the correct
1672          position. */
1673       if(data->state.resume_from > 0) {
1674         /* Let's read off the proper amount of bytes from the input. */
1675         if(conn->seek_func) {
1676           seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
1677                                     SEEK_SET);
1678         }
1679
1680         if(seekerr != CURL_SEEKFUNC_OK) {
1681
1682           if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
1683             failf(data, "Could not seek stream");
1684             return CURLE_FTP_COULDNT_USE_REST;
1685           }
1686           /* seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
1687           else {
1688             curl_off_t passed=0;
1689             do {
1690               size_t readthisamountnow =
1691                 (data->state.resume_from - passed > CURL_OFF_T_C(BUFSIZE)) ?
1692                 BUFSIZE : curlx_sotouz(data->state.resume_from - passed);
1693
1694               size_t actuallyread =
1695                 conn->fread_func(data->state.buffer, 1, readthisamountnow,
1696                                  conn->fread_in);
1697
1698               passed += actuallyread;
1699               if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
1700                 /* this checks for greater-than only to make sure that the
1701                    CURL_READFUNC_ABORT return code still aborts */
1702                 failf(data, "Failed to read data");
1703                 return CURLE_FTP_COULDNT_USE_REST;
1704               }
1705             } while(passed < data->state.resume_from);
1706           }
1707         }
1708
1709         /* now, decrease the size of the read */
1710         if(data->set.infilesize > 0) {
1711           data->set.infilesize -= data->state.resume_from;
1712           data->req.size = data->set.infilesize;
1713           Curl_pgrsSetUploadSize(data, data->set.infilesize);
1714         }
1715
1716         SFTP_SEEK(sshc->sftp_handle, data->state.resume_from);
1717       }
1718       if(data->set.infilesize > 0) {
1719         data->req.size = data->set.infilesize;
1720         Curl_pgrsSetUploadSize(data, data->set.infilesize);
1721       }
1722       /* upload data */
1723       Curl_setup_transfer(conn, -1, -1, FALSE, NULL, FIRSTSOCKET, NULL);
1724
1725       /* not set by Curl_setup_transfer to preserve keepon bits */
1726       conn->sockfd = conn->writesockfd;
1727
1728       if(result) {
1729         state(conn, SSH_SFTP_CLOSE);
1730         sshc->actualcode = result;
1731       }
1732       else {
1733         /* store this original bitmask setup to use later on if we can't
1734            figure out a "real" bitmask */
1735         sshc->orig_waitfor = data->req.keepon;
1736
1737         /* we want to use the _sending_ function even when the socket turns
1738            out readable as the underlying libssh2 sftp send function will deal
1739            with both accordingly */
1740         conn->cselect_bits = CURL_CSELECT_OUT;
1741
1742         /* since we don't really wait for anything at this point, we want the
1743            state machine to move on as soon as possible so we set a very short
1744            timeout here */
1745         Curl_expire(data, 1);
1746
1747         state(conn, SSH_STOP);
1748       }
1749       break;
1750     }
1751
1752     case SSH_SFTP_CREATE_DIRS_INIT:
1753       if(strlen(sftp_scp->path) > 1) {
1754         sshc->slash_pos = sftp_scp->path + 1; /* ignore the leading '/' */
1755         state(conn, SSH_SFTP_CREATE_DIRS);
1756       }
1757       else {
1758         state(conn, SSH_SFTP_UPLOAD_INIT);
1759       }
1760       break;
1761
1762     case SSH_SFTP_CREATE_DIRS:
1763       if((sshc->slash_pos = strchr(sshc->slash_pos, '/')) != NULL) {
1764         *sshc->slash_pos = 0;
1765
1766         infof(data, "Creating directory '%s'\n", sftp_scp->path);
1767         state(conn, SSH_SFTP_CREATE_DIRS_MKDIR);
1768         break;
1769       }
1770       else {
1771         state(conn, SSH_SFTP_UPLOAD_INIT);
1772       }
1773       break;
1774
1775     case SSH_SFTP_CREATE_DIRS_MKDIR:
1776       /* 'mode' - parameter is preliminary - default to 0644 */
1777       rc = libssh2_sftp_mkdir_ex(sshc->sftp_session, sftp_scp->path,
1778                                  curlx_uztoui(strlen(sftp_scp->path)),
1779                                  data->set.new_directory_perms);
1780       if(rc == LIBSSH2_ERROR_EAGAIN) {
1781         break;
1782       }
1783       *sshc->slash_pos = '/';
1784       ++sshc->slash_pos;
1785       if(rc == -1) {
1786         /*
1787          * Abort if failure wasn't that the dir already exists or the
1788          * permission was denied (creation might succeed further down the
1789          * path) - retry on unspecific FAILURE also
1790          */
1791         err = sftp_libssh2_last_error(sshc->sftp_session);
1792         if((err != LIBSSH2_FX_FILE_ALREADY_EXISTS) &&
1793            (err != LIBSSH2_FX_FAILURE) &&
1794            (err != LIBSSH2_FX_PERMISSION_DENIED)) {
1795           result = sftp_libssh2_error_to_CURLE(err);
1796           state(conn, SSH_SFTP_CLOSE);
1797           sshc->actualcode = result?result:CURLE_SSH;
1798           break;
1799         }
1800       }
1801       state(conn, SSH_SFTP_CREATE_DIRS);
1802       break;
1803
1804     case SSH_SFTP_READDIR_INIT:
1805       /*
1806        * This is a directory that we are trying to get, so produce a directory
1807        * listing
1808        */
1809       sshc->sftp_handle = libssh2_sftp_open_ex(sshc->sftp_session,
1810                                                sftp_scp->path,
1811                                                curlx_uztoui(
1812                                                  strlen(sftp_scp->path)),
1813                                                0, 0, LIBSSH2_SFTP_OPENDIR);
1814       if(!sshc->sftp_handle) {
1815         if(libssh2_session_last_errno(sshc->ssh_session) ==
1816            LIBSSH2_ERROR_EAGAIN) {
1817           rc = LIBSSH2_ERROR_EAGAIN;
1818           break;
1819         }
1820         else {
1821           err = sftp_libssh2_last_error(sshc->sftp_session);
1822           failf(data, "Could not open directory for reading: %s",
1823                 sftp_libssh2_strerror(err));
1824           state(conn, SSH_SFTP_CLOSE);
1825           result = sftp_libssh2_error_to_CURLE(err);
1826           sshc->actualcode = result?result:CURLE_SSH;
1827           break;
1828         }
1829       }
1830       if((sshc->readdir_filename = malloc(PATH_MAX+1)) == NULL) {
1831         state(conn, SSH_SFTP_CLOSE);
1832         sshc->actualcode = CURLE_OUT_OF_MEMORY;
1833         break;
1834       }
1835       if((sshc->readdir_longentry = malloc(PATH_MAX+1)) == NULL) {
1836         Curl_safefree(sshc->readdir_filename);
1837         state(conn, SSH_SFTP_CLOSE);
1838         sshc->actualcode = CURLE_OUT_OF_MEMORY;
1839         break;
1840       }
1841       state(conn, SSH_SFTP_READDIR);
1842       break;
1843
1844     case SSH_SFTP_READDIR:
1845       sshc->readdir_len = libssh2_sftp_readdir_ex(sshc->sftp_handle,
1846                                                   sshc->readdir_filename,
1847                                                   PATH_MAX,
1848                                                   sshc->readdir_longentry,
1849                                                   PATH_MAX,
1850                                                   &sshc->readdir_attrs);
1851       if(sshc->readdir_len == LIBSSH2_ERROR_EAGAIN) {
1852         rc = LIBSSH2_ERROR_EAGAIN;
1853         break;
1854       }
1855       if(sshc->readdir_len > 0) {
1856         sshc->readdir_filename[sshc->readdir_len] = '\0';
1857
1858         if(data->set.ftp_list_only) {
1859           char *tmpLine;
1860
1861           tmpLine = aprintf("%s\n", sshc->readdir_filename);
1862           if(tmpLine == NULL) {
1863             state(conn, SSH_SFTP_CLOSE);
1864             sshc->actualcode = CURLE_OUT_OF_MEMORY;
1865             break;
1866           }
1867           result = Curl_client_write(conn, CLIENTWRITE_BODY,
1868                                      tmpLine, sshc->readdir_len+1);
1869           Curl_safefree(tmpLine);
1870
1871           if(result) {
1872             state(conn, SSH_STOP);
1873             break;
1874           }
1875           /* since this counts what we send to the client, we include the
1876              newline in this counter */
1877           data->req.bytecount += sshc->readdir_len+1;
1878
1879           /* output debug output if that is requested */
1880           if(data->set.verbose) {
1881             Curl_debug(data, CURLINFO_DATA_OUT, sshc->readdir_filename,
1882                        sshc->readdir_len, conn);
1883           }
1884         }
1885         else {
1886           sshc->readdir_currLen = (int)strlen(sshc->readdir_longentry);
1887           sshc->readdir_totalLen = 80 + sshc->readdir_currLen;
1888           sshc->readdir_line = calloc(sshc->readdir_totalLen, 1);
1889           if(!sshc->readdir_line) {
1890             Curl_safefree(sshc->readdir_filename);
1891             Curl_safefree(sshc->readdir_longentry);
1892             state(conn, SSH_SFTP_CLOSE);
1893             sshc->actualcode = CURLE_OUT_OF_MEMORY;
1894             break;
1895           }
1896
1897           memcpy(sshc->readdir_line, sshc->readdir_longentry,
1898                  sshc->readdir_currLen);
1899           if((sshc->readdir_attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) &&
1900              ((sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
1901               LIBSSH2_SFTP_S_IFLNK)) {
1902             sshc->readdir_linkPath = malloc(PATH_MAX + 1);
1903             if(sshc->readdir_linkPath == NULL) {
1904               Curl_safefree(sshc->readdir_filename);
1905               Curl_safefree(sshc->readdir_longentry);
1906               state(conn, SSH_SFTP_CLOSE);
1907               sshc->actualcode = CURLE_OUT_OF_MEMORY;
1908               break;
1909             }
1910
1911             snprintf(sshc->readdir_linkPath, PATH_MAX, "%s%s", sftp_scp->path,
1912                      sshc->readdir_filename);
1913             state(conn, SSH_SFTP_READDIR_LINK);
1914             break;
1915           }
1916           state(conn, SSH_SFTP_READDIR_BOTTOM);
1917           break;
1918         }
1919       }
1920       else if(sshc->readdir_len == 0) {
1921         Curl_safefree(sshc->readdir_filename);
1922         Curl_safefree(sshc->readdir_longentry);
1923         state(conn, SSH_SFTP_READDIR_DONE);
1924         break;
1925       }
1926       else if(sshc->readdir_len <= 0) {
1927         err = sftp_libssh2_last_error(sshc->sftp_session);
1928         result = sftp_libssh2_error_to_CURLE(err);
1929         sshc->actualcode = result?result:CURLE_SSH;
1930         failf(data, "Could not open remote file for reading: %s :: %d",
1931               sftp_libssh2_strerror(err),
1932               libssh2_session_last_errno(sshc->ssh_session));
1933         Curl_safefree(sshc->readdir_filename);
1934         Curl_safefree(sshc->readdir_longentry);
1935         state(conn, SSH_SFTP_CLOSE);
1936         break;
1937       }
1938       break;
1939
1940     case SSH_SFTP_READDIR_LINK:
1941       sshc->readdir_len =
1942         libssh2_sftp_symlink_ex(sshc->sftp_session,
1943                                 sshc->readdir_linkPath,
1944                                 curlx_uztoui(strlen(sshc->readdir_linkPath)),
1945                                 sshc->readdir_filename,
1946                                 PATH_MAX, LIBSSH2_SFTP_READLINK);
1947       if(sshc->readdir_len == LIBSSH2_ERROR_EAGAIN) {
1948         rc = LIBSSH2_ERROR_EAGAIN;
1949         break;
1950       }
1951       Curl_safefree(sshc->readdir_linkPath);
1952
1953       /* get room for the filename and extra output */
1954       sshc->readdir_totalLen += 4 + sshc->readdir_len;
1955       new_readdir_line = realloc(sshc->readdir_line, sshc->readdir_totalLen);
1956       if(!new_readdir_line) {
1957         Curl_safefree(sshc->readdir_line);
1958         Curl_safefree(sshc->readdir_filename);
1959         Curl_safefree(sshc->readdir_longentry);
1960         state(conn, SSH_SFTP_CLOSE);
1961         sshc->actualcode = CURLE_OUT_OF_MEMORY;
1962         break;
1963       }
1964       sshc->readdir_line = new_readdir_line;
1965
1966       sshc->readdir_currLen += snprintf(sshc->readdir_line +
1967                                         sshc->readdir_currLen,
1968                                         sshc->readdir_totalLen -
1969                                         sshc->readdir_currLen,
1970                                         " -> %s",
1971                                         sshc->readdir_filename);
1972
1973       state(conn, SSH_SFTP_READDIR_BOTTOM);
1974       break;
1975
1976     case SSH_SFTP_READDIR_BOTTOM:
1977       sshc->readdir_currLen += snprintf(sshc->readdir_line +
1978                                         sshc->readdir_currLen,
1979                                         sshc->readdir_totalLen -
1980                                         sshc->readdir_currLen, "\n");
1981       result = Curl_client_write(conn, CLIENTWRITE_BODY,
1982                                  sshc->readdir_line,
1983                                  sshc->readdir_currLen);
1984
1985       if(result == CURLE_OK) {
1986
1987         /* output debug output if that is requested */
1988         if(data->set.verbose) {
1989           Curl_debug(data, CURLINFO_DATA_OUT, sshc->readdir_line,
1990                      sshc->readdir_currLen, conn);
1991         }
1992         data->req.bytecount += sshc->readdir_currLen;
1993       }
1994       Curl_safefree(sshc->readdir_line);
1995       if(result) {
1996         state(conn, SSH_STOP);
1997       }
1998       else
1999         state(conn, SSH_SFTP_READDIR);
2000       break;
2001
2002     case SSH_SFTP_READDIR_DONE:
2003       if(libssh2_sftp_closedir(sshc->sftp_handle) ==
2004          LIBSSH2_ERROR_EAGAIN) {
2005         rc = LIBSSH2_ERROR_EAGAIN;
2006         break;
2007       }
2008       sshc->sftp_handle = NULL;
2009       Curl_safefree(sshc->readdir_filename);
2010       Curl_safefree(sshc->readdir_longentry);
2011
2012       /* no data to transfer */
2013       Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
2014       state(conn, SSH_STOP);
2015       break;
2016
2017     case SSH_SFTP_DOWNLOAD_INIT:
2018       /*
2019        * Work on getting the specified file
2020        */
2021       sshc->sftp_handle =
2022         libssh2_sftp_open_ex(sshc->sftp_session, sftp_scp->path,
2023                              curlx_uztoui(strlen(sftp_scp->path)),
2024                              LIBSSH2_FXF_READ, data->set.new_file_perms,
2025                              LIBSSH2_SFTP_OPENFILE);
2026       if(!sshc->sftp_handle) {
2027         if(libssh2_session_last_errno(sshc->ssh_session) ==
2028            LIBSSH2_ERROR_EAGAIN) {
2029           rc = LIBSSH2_ERROR_EAGAIN;
2030           break;
2031         }
2032         else {
2033           err = sftp_libssh2_last_error(sshc->sftp_session);
2034           failf(data, "Could not open remote file for reading: %s",
2035                 sftp_libssh2_strerror(err));
2036           state(conn, SSH_SFTP_CLOSE);
2037           result = sftp_libssh2_error_to_CURLE(err);
2038           sshc->actualcode = result?result:CURLE_SSH;
2039           break;
2040         }
2041       }
2042       state(conn, SSH_SFTP_DOWNLOAD_STAT);
2043       break;
2044
2045     case SSH_SFTP_DOWNLOAD_STAT:
2046     {
2047       LIBSSH2_SFTP_ATTRIBUTES attrs;
2048
2049       rc = libssh2_sftp_stat_ex(sshc->sftp_session, sftp_scp->path,
2050                                 curlx_uztoui(strlen(sftp_scp->path)),
2051                                 LIBSSH2_SFTP_STAT, &attrs);
2052       if(rc == LIBSSH2_ERROR_EAGAIN) {
2053         break;
2054       }
2055       else if(rc) {
2056         /*
2057          * libssh2_sftp_open() didn't return an error, so maybe the server
2058          * just doesn't support stat()
2059          */
2060         data->req.size = -1;
2061         data->req.maxdownload = -1;
2062       }
2063       else {
2064         curl_off_t size = attrs.filesize;
2065
2066         if(size < 0) {
2067           failf(data, "Bad file size (%" FORMAT_OFF_T ")", size);
2068           return CURLE_BAD_DOWNLOAD_RESUME;
2069         }
2070         if(conn->data->state.use_range) {
2071           curl_off_t from, to;
2072           char *ptr;
2073           char *ptr2;
2074
2075           from=curlx_strtoofft(conn->data->state.range, &ptr, 0);
2076           while(*ptr && (ISSPACE(*ptr) || (*ptr=='-')))
2077             ptr++;
2078           to=curlx_strtoofft(ptr, &ptr2, 0);
2079           if((ptr == ptr2) /* no "to" value given */
2080              || (to >= size)) {
2081             to = size - 1;
2082           }
2083           if(from < 0) {
2084             /* from is relative to end of file */
2085             from += size;
2086           }
2087           if(from >= size) {
2088             failf(data, "Offset (%"
2089                   FORMAT_OFF_T ") was beyond file size (%" FORMAT_OFF_T ")",
2090                   from, attrs.filesize);
2091             return CURLE_BAD_DOWNLOAD_RESUME;
2092           }
2093           if(from > to) {
2094             from = to;
2095             size = 0;
2096           }
2097           else {
2098             size = to - from + 1;
2099           }
2100
2101           SFTP_SEEK(conn->proto.sshc.sftp_handle, from);
2102         }
2103         data->req.size = size;
2104         data->req.maxdownload = size;
2105         Curl_pgrsSetDownloadSize(data, size);
2106       }
2107
2108       /* We can resume if we can seek to the resume position */
2109       if(data->state.resume_from) {
2110         if(data->state.resume_from < 0) {
2111           /* We're supposed to download the last abs(from) bytes */
2112           if((curl_off_t)attrs.filesize < -data->state.resume_from) {
2113             failf(data, "Offset (%"
2114                   FORMAT_OFF_T ") was beyond file size (%" FORMAT_OFF_T ")",
2115                   data->state.resume_from, attrs.filesize);
2116             return CURLE_BAD_DOWNLOAD_RESUME;
2117           }
2118           /* download from where? */
2119           data->state.resume_from += attrs.filesize;
2120         }
2121         else {
2122           if((curl_off_t)attrs.filesize < data->state.resume_from) {
2123             failf(data, "Offset (%" FORMAT_OFF_T
2124                   ") was beyond file size (%" FORMAT_OFF_T ")",
2125                   data->state.resume_from, attrs.filesize);
2126             return CURLE_BAD_DOWNLOAD_RESUME;
2127           }
2128         }
2129         /* Does a completed file need to be seeked and started or closed ? */
2130         /* Now store the number of bytes we are expected to download */
2131         data->req.size = attrs.filesize - data->state.resume_from;
2132         data->req.maxdownload = attrs.filesize - data->state.resume_from;
2133         Curl_pgrsSetDownloadSize(data,
2134                                  attrs.filesize - data->state.resume_from);
2135         SFTP_SEEK(sshc->sftp_handle, data->state.resume_from);
2136       }
2137     }
2138     /* Setup the actual download */
2139     if(data->req.size == 0) {
2140       /* no data to transfer */
2141       Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
2142       infof(data, "File already completely downloaded\n");
2143       state(conn, SSH_STOP);
2144       break;
2145     }
2146     else {
2147       Curl_setup_transfer(conn, FIRSTSOCKET, data->req.size,
2148                           FALSE, NULL, -1, NULL);
2149
2150       /* not set by Curl_setup_transfer to preserve keepon bits */
2151       conn->writesockfd = conn->sockfd;
2152
2153       /* we want to use the _receiving_ function even when the socket turns
2154          out writableable as the underlying libssh2 recv function will deal
2155          with both accordingly */
2156       conn->cselect_bits = CURL_CSELECT_IN;
2157     }
2158     if(result) {
2159       state(conn, SSH_SFTP_CLOSE);
2160       sshc->actualcode = result;
2161     }
2162     else {
2163       state(conn, SSH_STOP);
2164     }
2165     break;
2166
2167     case SSH_SFTP_CLOSE:
2168       if(sshc->sftp_handle) {
2169         rc = libssh2_sftp_close(sshc->sftp_handle);
2170         if(rc == LIBSSH2_ERROR_EAGAIN) {
2171           break;
2172         }
2173         else if(rc < 0) {
2174           infof(data, "Failed to close libssh2 file\n");
2175         }
2176         sshc->sftp_handle = NULL;
2177       }
2178       if(sftp_scp)
2179         Curl_safefree(sftp_scp->path);
2180
2181       DEBUGF(infof(data, "SFTP DONE done\n"));
2182
2183       /* Check if nextstate is set and move .nextstate could be POSTQUOTE_INIT
2184          After nextstate is executed,the control should come back to
2185          SSH_SFTP_CLOSE to pass the correct result back  */
2186       if(sshc->nextstate != SSH_NO_STATE) {
2187         state(conn, sshc->nextstate);
2188         sshc->nextstate = SSH_SFTP_CLOSE;
2189       }
2190       else {
2191         state(conn, SSH_STOP);
2192         result = sshc->actualcode;
2193       }
2194       break;
2195
2196     case SSH_SFTP_SHUTDOWN:
2197       /* during times we get here due to a broken transfer and then the
2198          sftp_handle might not have been taken down so make sure that is done
2199          before we proceed */
2200
2201       if(sshc->sftp_handle) {
2202         rc = libssh2_sftp_close(sshc->sftp_handle);
2203         if(rc == LIBSSH2_ERROR_EAGAIN) {
2204           break;
2205         }
2206         else if(rc < 0) {
2207           infof(data, "Failed to close libssh2 file\n");
2208         }
2209         sshc->sftp_handle = NULL;
2210       }
2211       if(sshc->sftp_session) {
2212         rc = libssh2_sftp_shutdown(sshc->sftp_session);
2213         if(rc == LIBSSH2_ERROR_EAGAIN) {
2214           break;
2215         }
2216         else if(rc < 0) {
2217           infof(data, "Failed to stop libssh2 sftp subsystem\n");
2218         }
2219         sshc->sftp_session = NULL;
2220       }
2221
2222       Curl_safefree(sshc->homedir);
2223       conn->data->state.most_recent_ftp_entrypath = NULL;
2224
2225       state(conn, SSH_SESSION_DISCONNECT);
2226       break;
2227
2228     case SSH_SCP_TRANS_INIT:
2229       result = ssh_getworkingpath(conn, sshc->homedir, &sftp_scp->path);
2230       if(result) {
2231         sshc->actualcode = result;
2232         state(conn, SSH_STOP);
2233         break;
2234       }
2235
2236       if(data->set.upload) {
2237         if(data->set.infilesize < 0) {
2238           failf(data, "SCP requires a known file size for upload");
2239           sshc->actualcode = CURLE_UPLOAD_FAILED;
2240           state(conn, SSH_SCP_CHANNEL_FREE);
2241           break;
2242         }
2243         state(conn, SSH_SCP_UPLOAD_INIT);
2244       }
2245       else {
2246         state(conn, SSH_SCP_DOWNLOAD_INIT);
2247       }
2248       break;
2249
2250     case SSH_SCP_UPLOAD_INIT:
2251       /*
2252        * libssh2 requires that the destination path is a full path that
2253        * includes the destination file and name OR ends in a "/" .  If this is
2254        * not done the destination file will be named the same name as the last
2255        * directory in the path.
2256        */
2257       sshc->ssh_channel =
2258         SCP_SEND(sshc->ssh_session, sftp_scp->path, data->set.new_file_perms,
2259                  data->set.infilesize);
2260       if(!sshc->ssh_channel) {
2261         if(libssh2_session_last_errno(sshc->ssh_session) ==
2262            LIBSSH2_ERROR_EAGAIN) {
2263           rc = LIBSSH2_ERROR_EAGAIN;
2264           break;
2265         }
2266         else {
2267           int ssh_err;
2268           char *err_msg;
2269
2270           ssh_err = (int)(libssh2_session_last_error(sshc->ssh_session,
2271                                                      &err_msg, NULL, 0));
2272           failf(conn->data, "%s", err_msg);
2273           state(conn, SSH_SCP_CHANNEL_FREE);
2274           sshc->actualcode = libssh2_session_error_to_CURLE(ssh_err);
2275           break;
2276         }
2277       }
2278
2279       /* upload data */
2280       Curl_setup_transfer(conn, -1, data->req.size, FALSE, NULL,
2281                           FIRSTSOCKET, NULL);
2282
2283       /* not set by Curl_setup_transfer to preserve keepon bits */
2284       conn->sockfd = conn->writesockfd;
2285
2286       if(result) {
2287         state(conn, SSH_SCP_CHANNEL_FREE);
2288         sshc->actualcode = result;
2289       }
2290       else {
2291         /* store this original bitmask setup to use later on if we can't
2292            figure out a "real" bitmask */
2293         sshc->orig_waitfor = data->req.keepon;
2294
2295         /* we want to use the _sending_ function even when the socket turns
2296            out readable as the underlying libssh2 scp send function will deal
2297            with both accordingly */
2298         conn->cselect_bits = CURL_CSELECT_OUT;
2299
2300         state(conn, SSH_STOP);
2301       }
2302       break;
2303
2304     case SSH_SCP_DOWNLOAD_INIT:
2305     {
2306       /*
2307        * We must check the remote file; if it is a directory no values will
2308        * be set in sb
2309        */
2310       struct stat sb;
2311       curl_off_t bytecount;
2312
2313       /* clear the struct scp recv will fill in */
2314       memset(&sb, 0, sizeof(struct stat));
2315
2316       /* get a fresh new channel from the ssh layer */
2317       sshc->ssh_channel = libssh2_scp_recv(sshc->ssh_session,
2318                                            sftp_scp->path, &sb);
2319       if(!sshc->ssh_channel) {
2320         if(libssh2_session_last_errno(sshc->ssh_session) ==
2321            LIBSSH2_ERROR_EAGAIN) {
2322           rc = LIBSSH2_ERROR_EAGAIN;
2323           break;
2324         }
2325         else {
2326           int ssh_err;
2327           char *err_msg;
2328
2329           ssh_err = (int)(libssh2_session_last_error(sshc->ssh_session,
2330                                                      &err_msg, NULL, 0));
2331           failf(conn->data, "%s", err_msg);
2332           state(conn, SSH_SCP_CHANNEL_FREE);
2333           sshc->actualcode = libssh2_session_error_to_CURLE(ssh_err);
2334           break;
2335         }
2336       }
2337
2338       /* download data */
2339       bytecount = (curl_off_t)sb.st_size;
2340       data->req.maxdownload =  (curl_off_t)sb.st_size;
2341       Curl_setup_transfer(conn, FIRSTSOCKET, bytecount, FALSE, NULL, -1, NULL);
2342
2343       /* not set by Curl_setup_transfer to preserve keepon bits */
2344       conn->writesockfd = conn->sockfd;
2345
2346       /* we want to use the _receiving_ function even when the socket turns
2347          out writableable as the underlying libssh2 recv function will deal
2348          with both accordingly */
2349       conn->cselect_bits = CURL_CSELECT_IN;
2350
2351       if(result) {
2352         state(conn, SSH_SCP_CHANNEL_FREE);
2353         sshc->actualcode = result;
2354       }
2355       else
2356         state(conn, SSH_STOP);
2357     }
2358     break;
2359
2360     case SSH_SCP_DONE:
2361       if(data->set.upload)
2362         state(conn, SSH_SCP_SEND_EOF);
2363       else
2364         state(conn, SSH_SCP_CHANNEL_FREE);
2365       break;
2366
2367     case SSH_SCP_SEND_EOF:
2368       if(sshc->ssh_channel) {
2369         rc = libssh2_channel_send_eof(sshc->ssh_channel);
2370         if(rc == LIBSSH2_ERROR_EAGAIN) {
2371           break;
2372         }
2373         else if(rc) {
2374           infof(data, "Failed to send libssh2 channel EOF\n");
2375         }
2376       }
2377       state(conn, SSH_SCP_WAIT_EOF);
2378       break;
2379
2380     case SSH_SCP_WAIT_EOF:
2381       if(sshc->ssh_channel) {
2382         rc = libssh2_channel_wait_eof(sshc->ssh_channel);
2383         if(rc == LIBSSH2_ERROR_EAGAIN) {
2384           break;
2385         }
2386         else if(rc) {
2387           infof(data, "Failed to get channel EOF: %d\n", rc);
2388         }
2389       }
2390       state(conn, SSH_SCP_WAIT_CLOSE);
2391       break;
2392
2393     case SSH_SCP_WAIT_CLOSE:
2394       if(sshc->ssh_channel) {
2395         rc = libssh2_channel_wait_closed(sshc->ssh_channel);
2396         if(rc == LIBSSH2_ERROR_EAGAIN) {
2397           break;
2398         }
2399         else if(rc) {
2400           infof(data, "Channel failed to close: %d\n", rc);
2401         }
2402       }
2403       state(conn, SSH_SCP_CHANNEL_FREE);
2404       break;
2405
2406     case SSH_SCP_CHANNEL_FREE:
2407       if(sshc->ssh_channel) {
2408         rc = libssh2_channel_free(sshc->ssh_channel);
2409         if(rc == LIBSSH2_ERROR_EAGAIN) {
2410           break;
2411         }
2412         else if(rc < 0) {
2413           infof(data, "Failed to free libssh2 scp subsystem\n");
2414         }
2415         sshc->ssh_channel = NULL;
2416       }
2417       DEBUGF(infof(data, "SCP DONE phase complete\n"));
2418 #if 0 /* PREV */
2419       state(conn, SSH_SESSION_DISCONNECT);
2420 #endif
2421       state(conn, SSH_STOP);
2422       result = sshc->actualcode;
2423       break;
2424
2425     case SSH_SESSION_DISCONNECT:
2426       /* during weird times when we've been prematurely aborted, the channel
2427          is still alive when we reach this state and we MUST kill the channel
2428          properly first */
2429       if(sshc->ssh_channel) {
2430         rc = libssh2_channel_free(sshc->ssh_channel);
2431         if(rc == LIBSSH2_ERROR_EAGAIN) {
2432           break;
2433         }
2434         else if(rc < 0) {
2435           infof(data, "Failed to free libssh2 scp subsystem\n");
2436         }
2437         sshc->ssh_channel = NULL;
2438       }
2439
2440       if(sshc->ssh_session) {
2441         rc = libssh2_session_disconnect(sshc->ssh_session, "Shutdown");
2442         if(rc == LIBSSH2_ERROR_EAGAIN) {
2443           break;
2444         }
2445         else if(rc < 0) {
2446           infof(data, "Failed to disconnect libssh2 session\n");
2447         }
2448       }
2449
2450       Curl_safefree(sshc->homedir);
2451       conn->data->state.most_recent_ftp_entrypath = NULL;
2452
2453       state(conn, SSH_SESSION_FREE);
2454       break;
2455
2456     case SSH_SESSION_FREE:
2457 #ifdef HAVE_LIBSSH2_KNOWNHOST_API
2458       if(sshc->kh) {
2459         libssh2_knownhost_free(sshc->kh);
2460         sshc->kh = NULL;
2461       }
2462 #endif
2463
2464 #ifdef HAVE_LIBSSH2_AGENT_API
2465       if(sshc->ssh_agent) {
2466         rc = libssh2_agent_disconnect(sshc->ssh_agent);
2467         if(rc == LIBSSH2_ERROR_EAGAIN) {
2468           break;
2469         }
2470         else if(rc < 0) {
2471           infof(data, "Failed to disconnect from libssh2 agent\n");
2472         }
2473         libssh2_agent_free (sshc->ssh_agent);
2474         sshc->ssh_agent = NULL;
2475
2476         /* NB: there is no need to free identities, they are part of internal
2477            agent stuff */
2478         sshc->sshagent_identity = NULL;
2479         sshc->sshagent_prev_identity = NULL;
2480       }
2481 #endif
2482
2483       if(sshc->ssh_session) {
2484         rc = libssh2_session_free(sshc->ssh_session);
2485         if(rc == LIBSSH2_ERROR_EAGAIN) {
2486           break;
2487         }
2488         else if(rc < 0) {
2489           infof(data, "Failed to free libssh2 session\n");
2490         }
2491         sshc->ssh_session = NULL;
2492       }
2493
2494       /* worst-case scenario cleanup */
2495
2496       DEBUGASSERT(sshc->ssh_session == NULL);
2497       DEBUGASSERT(sshc->ssh_channel == NULL);
2498       DEBUGASSERT(sshc->sftp_session == NULL);
2499       DEBUGASSERT(sshc->sftp_handle == NULL);
2500 #ifdef HAVE_LIBSSH2_KNOWNHOST_API
2501       DEBUGASSERT(sshc->kh == NULL);
2502 #endif
2503 #ifdef HAVE_LIBSSH2_AGENT_API
2504       DEBUGASSERT(sshc->ssh_agent == NULL);
2505 #endif
2506
2507       Curl_safefree(sshc->rsa_pub);
2508       Curl_safefree(sshc->rsa);
2509
2510       Curl_safefree(sshc->quote_path1);
2511       Curl_safefree(sshc->quote_path2);
2512
2513       Curl_safefree(sshc->homedir);
2514
2515       Curl_safefree(sshc->readdir_filename);
2516       Curl_safefree(sshc->readdir_longentry);
2517       Curl_safefree(sshc->readdir_line);
2518       Curl_safefree(sshc->readdir_linkPath);
2519
2520       /* the code we are about to return */
2521       result = sshc->actualcode;
2522
2523       memset(sshc, 0, sizeof(struct ssh_conn));
2524
2525       conn->bits.close = TRUE;
2526       sshc->state = SSH_SESSION_FREE; /* current */
2527       sshc->nextstate = SSH_NO_STATE;
2528       state(conn, SSH_STOP);
2529       break;
2530
2531     case SSH_QUIT:
2532       /* fallthrough, just stop! */
2533     default:
2534       /* internal error */
2535       sshc->nextstate = SSH_NO_STATE;
2536       state(conn, SSH_STOP);
2537       break;
2538     }
2539
2540   } while(!rc && (sshc->state != SSH_STOP));
2541
2542   if(rc == LIBSSH2_ERROR_EAGAIN) {
2543     /* we would block, we need to wait for the socket to be ready (in the
2544        right direction too)! */
2545     *block = TRUE;
2546   }
2547
2548   return result;
2549 }
2550
2551 /* called by the multi interface to figure out what socket(s) to wait for and
2552    for what actions in the DO_DONE, PERFORM and WAITPERFORM states */
2553 static int ssh_perform_getsock(const struct connectdata *conn,
2554                                curl_socket_t *sock, /* points to numsocks
2555                                                        number of sockets */
2556                                int numsocks)
2557 {
2558 #ifdef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
2559   int bitmap = GETSOCK_BLANK;
2560   (void)numsocks;
2561
2562   sock[0] = conn->sock[FIRSTSOCKET];
2563
2564   if(conn->waitfor & KEEP_RECV)
2565     bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
2566
2567   if(conn->waitfor & KEEP_SEND)
2568     bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
2569
2570   return bitmap;
2571 #else
2572   /* if we don't know the direction we can use the generic *_getsock()
2573      function even for the protocol_connect and doing states */
2574   return Curl_single_getsock(conn, sock, numsocks);
2575 #endif
2576 }
2577
2578 /* Generic function called by the multi interface to figure out what socket(s)
2579    to wait for and for what actions during the DOING and PROTOCONNECT states*/
2580 static int ssh_getsock(struct connectdata *conn,
2581                        curl_socket_t *sock, /* points to numsocks number
2582                                                of sockets */
2583                        int numsocks)
2584 {
2585 #ifndef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
2586   (void)conn;
2587   (void)sock;
2588   (void)numsocks;
2589   /* if we don't know any direction we can just play along as we used to and
2590      not provide any sensible info */
2591   return GETSOCK_BLANK;
2592 #else
2593   /* if we know the direction we can use the generic *_getsock() function even
2594      for the protocol_connect and doing states */
2595   return ssh_perform_getsock(conn, sock, numsocks);
2596 #endif
2597 }
2598
2599 #ifdef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
2600 /*
2601  * When one of the libssh2 functions has returned LIBSSH2_ERROR_EAGAIN this
2602  * function is used to figure out in what direction and stores this info so
2603  * that the multi interface can take advantage of it. Make sure to call this
2604  * function in all cases so that when it _doesn't_ return EAGAIN we can
2605  * restore the default wait bits.
2606  */
2607 static void ssh_block2waitfor(struct connectdata *conn, bool block)
2608 {
2609   struct ssh_conn *sshc = &conn->proto.sshc;
2610   int dir;
2611   if(block && (dir = libssh2_session_block_directions(sshc->ssh_session))) {
2612     /* translate the libssh2 define bits into our own bit defines */
2613     conn->waitfor = ((dir&LIBSSH2_SESSION_BLOCK_INBOUND)?KEEP_RECV:0) |
2614       ((dir&LIBSSH2_SESSION_BLOCK_OUTBOUND)?KEEP_SEND:0);
2615   }
2616   else
2617     /* It didn't block or libssh2 didn't reveal in which direction, put back
2618        the original set */
2619     conn->waitfor = sshc->orig_waitfor;
2620 }
2621 #else
2622   /* no libssh2 directional support so we simply don't know */
2623 #define ssh_block2waitfor(x,y) Curl_nop_stmt
2624 #endif
2625
2626 /* called repeatedly until done from multi.c */
2627 static CURLcode ssh_multi_statemach(struct connectdata *conn, bool *done)
2628 {
2629   struct ssh_conn *sshc = &conn->proto.sshc;
2630   CURLcode result = CURLE_OK;
2631   bool block; /* we store the status and use that to provide a ssh_getsock()
2632                  implementation */
2633
2634   result = ssh_statemach_act(conn, &block);
2635   *done = (sshc->state == SSH_STOP) ? TRUE : FALSE;
2636   ssh_block2waitfor(conn, block);
2637
2638   return result;
2639 }
2640
2641 static CURLcode ssh_block_statemach(struct connectdata *conn,
2642                                    bool duringconnect)
2643 {
2644   struct ssh_conn *sshc = &conn->proto.sshc;
2645   CURLcode result = CURLE_OK;
2646   struct SessionHandle *data = conn->data;
2647
2648   while((sshc->state != SSH_STOP) && !result) {
2649     bool block;
2650     long left;
2651
2652     result = ssh_statemach_act(conn, &block);
2653     if(result)
2654       break;
2655
2656     if(Curl_pgrsUpdate(conn))
2657       return CURLE_ABORTED_BY_CALLBACK;
2658     else {
2659       struct timeval now = Curl_tvnow();
2660       result = Curl_speedcheck(data, now);
2661       if(result)
2662         break;
2663     }
2664
2665     left = Curl_timeleft(data, NULL, duringconnect);
2666     if(left < 0) {
2667       failf(data, "Operation timed out");
2668       return CURLE_OPERATION_TIMEDOUT;
2669     }
2670
2671 #ifdef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
2672     if((CURLE_OK == result) && block) {
2673       int dir = libssh2_session_block_directions(sshc->ssh_session);
2674       curl_socket_t sock = conn->sock[FIRSTSOCKET];
2675       curl_socket_t fd_read = CURL_SOCKET_BAD;
2676       curl_socket_t fd_write = CURL_SOCKET_BAD;
2677       if(LIBSSH2_SESSION_BLOCK_INBOUND & dir)
2678         fd_read = sock;
2679       if(LIBSSH2_SESSION_BLOCK_OUTBOUND & dir)
2680         fd_write = sock;
2681       /* wait for the socket to become ready */
2682       Curl_socket_ready(fd_read, fd_write,
2683                         left>1000?1000:left); /* ignore result */
2684     }
2685 #endif
2686
2687   }
2688
2689   return result;
2690 }
2691
2692 /*
2693  * SSH setup and connection
2694  */
2695 static CURLcode ssh_setup_connection(struct connectdata *conn)
2696 {
2697   struct SSHPROTO *ssh;
2698
2699   conn->data->req.protop = ssh = calloc(1, sizeof(struct SSHPROTO));
2700   if(!ssh)
2701     return CURLE_OUT_OF_MEMORY;
2702
2703   return CURLE_OK;
2704 }
2705
2706 static Curl_recv scp_recv, sftp_recv;
2707 static Curl_send scp_send, sftp_send;
2708
2709 /*
2710  * Curl_ssh_connect() gets called from Curl_protocol_connect() to allow us to
2711  * do protocol-specific actions at connect-time.
2712  */
2713 static CURLcode ssh_connect(struct connectdata *conn, bool *done)
2714 {
2715 #ifdef CURL_LIBSSH2_DEBUG
2716   curl_socket_t sock;
2717 #endif
2718   struct ssh_conn *ssh;
2719   CURLcode result;
2720   struct SessionHandle *data = conn->data;
2721
2722   /* We default to persistent connections. We set this already in this connect
2723      function to make the re-use checks properly be able to check this bit. */
2724   conn->bits.close = FALSE;
2725
2726   if(conn->handler->protocol & CURLPROTO_SCP) {
2727     conn->recv[FIRSTSOCKET] = scp_recv;
2728     conn->send[FIRSTSOCKET] = scp_send;
2729   }
2730   else {
2731     conn->recv[FIRSTSOCKET] = sftp_recv;
2732     conn->send[FIRSTSOCKET] = sftp_send;
2733   }
2734   ssh = &conn->proto.sshc;
2735
2736 #ifdef CURL_LIBSSH2_DEBUG
2737   if(conn->user) {
2738     infof(data, "User: %s\n", conn->user);
2739   }
2740   if(conn->passwd) {
2741     infof(data, "Password: %s\n", conn->passwd);
2742   }
2743   sock = conn->sock[FIRSTSOCKET];
2744 #endif /* CURL_LIBSSH2_DEBUG */
2745
2746   ssh->ssh_session = libssh2_session_init_ex(my_libssh2_malloc,
2747                                              my_libssh2_free,
2748                                              my_libssh2_realloc, conn);
2749   if(ssh->ssh_session == NULL) {
2750     failf(data, "Failure initialising ssh session");
2751     return CURLE_FAILED_INIT;
2752   }
2753
2754 #ifdef HAVE_LIBSSH2_KNOWNHOST_API
2755   if(data->set.str[STRING_SSH_KNOWNHOSTS]) {
2756     int rc;
2757     ssh->kh = libssh2_knownhost_init(ssh->ssh_session);
2758     if(!ssh->kh) {
2759       /* eeek. TODO: free the ssh_session! */
2760       return CURLE_FAILED_INIT;
2761     }
2762
2763     /* read all known hosts from there */
2764     rc = libssh2_knownhost_readfile(ssh->kh,
2765                                     data->set.str[STRING_SSH_KNOWNHOSTS],
2766                                     LIBSSH2_KNOWNHOST_FILE_OPENSSH);
2767     if(rc < 0)
2768       infof(data, "Failed to read known hosts from %s\n",
2769             data->set.str[STRING_SSH_KNOWNHOSTS]);
2770   }
2771 #endif /* HAVE_LIBSSH2_KNOWNHOST_API */
2772
2773 #ifdef CURL_LIBSSH2_DEBUG
2774   libssh2_trace(ssh->ssh_session, ~0);
2775   infof(data, "SSH socket: %d\n", (int)sock);
2776 #endif /* CURL_LIBSSH2_DEBUG */
2777
2778   state(conn, SSH_INIT);
2779
2780   result = ssh_multi_statemach(conn, done);
2781
2782   return result;
2783 }
2784
2785 /*
2786  ***********************************************************************
2787  *
2788  * scp_perform()
2789  *
2790  * This is the actual DO function for SCP. Get a file according to
2791  * the options previously setup.
2792  */
2793
2794 static
2795 CURLcode scp_perform(struct connectdata *conn,
2796                       bool *connected,
2797                       bool *dophase_done)
2798 {
2799   CURLcode result = CURLE_OK;
2800
2801   DEBUGF(infof(conn->data, "DO phase starts\n"));
2802
2803   *dophase_done = FALSE; /* not done yet */
2804
2805   /* start the first command in the DO phase */
2806   state(conn, SSH_SCP_TRANS_INIT);
2807
2808   /* run the state-machine */
2809   result = ssh_multi_statemach(conn, dophase_done);
2810
2811   *connected = conn->bits.tcpconnect[FIRSTSOCKET];
2812
2813   if(*dophase_done) {
2814     DEBUGF(infof(conn->data, "DO phase is complete\n"));
2815   }
2816
2817   return result;
2818 }
2819
2820 /* called from multi.c while DOing */
2821 static CURLcode scp_doing(struct connectdata *conn,
2822                                bool *dophase_done)
2823 {
2824   CURLcode result;
2825   result = ssh_multi_statemach(conn, dophase_done);
2826
2827   if(*dophase_done) {
2828     DEBUGF(infof(conn->data, "DO phase is complete\n"));
2829   }
2830   return result;
2831 }
2832
2833 /*
2834  * The DO function is generic for both protocols. There was previously two
2835  * separate ones but this way means less duplicated code.
2836  */
2837
2838 static CURLcode ssh_do(struct connectdata *conn, bool *done)
2839 {
2840   CURLcode res;
2841   bool connected = 0;
2842   struct SessionHandle *data = conn->data;
2843   struct ssh_conn *sshc = &conn->proto.sshc;
2844
2845   *done = FALSE; /* default to false */
2846
2847   data->req.size = -1; /* make sure this is unknown at this point */
2848
2849   sshc->actualcode = CURLE_OK; /* reset error code */
2850   sshc->secondCreateDirs =0;   /* reset the create dir attempt state
2851                                   variable */
2852
2853   Curl_pgrsSetUploadCounter(data, 0);
2854   Curl_pgrsSetDownloadCounter(data, 0);
2855   Curl_pgrsSetUploadSize(data, 0);
2856   Curl_pgrsSetDownloadSize(data, 0);
2857
2858   if(conn->handler->protocol & CURLPROTO_SCP)
2859     res = scp_perform(conn, &connected,  done);
2860   else
2861     res = sftp_perform(conn, &connected,  done);
2862
2863   return res;
2864 }
2865
2866 /* BLOCKING, but the function is using the state machine so the only reason
2867    this is still blocking is that the multi interface code has no support for
2868    disconnecting operations that takes a while */
2869 static CURLcode scp_disconnect(struct connectdata *conn, bool dead_connection)
2870 {
2871   CURLcode result = CURLE_OK;
2872   struct ssh_conn *ssh = &conn->proto.sshc;
2873   (void) dead_connection;
2874
2875   Curl_safefree(conn->data->req.protop);
2876
2877   if(ssh->ssh_session) {
2878     /* only if there's a session still around to use! */
2879
2880     state(conn, SSH_SESSION_DISCONNECT);
2881
2882     result = ssh_block_statemach(conn, FALSE);
2883   }
2884
2885   return result;
2886 }
2887
2888 /* generic done function for both SCP and SFTP called from their specific
2889    done functions */
2890 static CURLcode ssh_done(struct connectdata *conn, CURLcode status)
2891 {
2892   CURLcode result = CURLE_OK;
2893   struct SSHPROTO *sftp_scp = conn->data->req.protop;
2894
2895   if(status == CURLE_OK) {
2896     /* run the state-machine
2897
2898        TODO: when the multi interface is used, this _really_ should be using
2899        the ssh_multi_statemach function but we have no general support for
2900        non-blocking DONE operations, not in the multi state machine and with
2901        Curl_done() invokes on several places in the code!
2902     */
2903     result = ssh_block_statemach(conn, FALSE);
2904   }
2905   else
2906     result = status;
2907
2908   if(sftp_scp)
2909     Curl_safefree(sftp_scp->path);
2910   if(Curl_pgrsDone(conn))
2911     return CURLE_ABORTED_BY_CALLBACK;
2912
2913   conn->data->req.keepon = 0; /* clear all bits */
2914   return result;
2915 }
2916
2917
2918 static CURLcode scp_done(struct connectdata *conn, CURLcode status,
2919                          bool premature)
2920 {
2921   (void)premature; /* not used */
2922
2923   if(status == CURLE_OK)
2924     state(conn, SSH_SCP_DONE);
2925
2926   return ssh_done(conn, status);
2927
2928 }
2929
2930 /* return number of received (decrypted) bytes */
2931 static ssize_t scp_send(struct connectdata *conn, int sockindex,
2932                         const void *mem, size_t len, CURLcode *err)
2933 {
2934   ssize_t nwrite;
2935   (void)sockindex; /* we only support SCP on the fixed known primary socket */
2936
2937   /* libssh2_channel_write() returns int! */
2938   nwrite = (ssize_t)
2939     libssh2_channel_write(conn->proto.sshc.ssh_channel, mem, len);
2940
2941   ssh_block2waitfor(conn, (nwrite == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
2942
2943   if(nwrite == LIBSSH2_ERROR_EAGAIN) {
2944     *err = CURLE_AGAIN;
2945     nwrite = 0;
2946   }
2947   else if(nwrite < LIBSSH2_ERROR_NONE) {
2948     *err = libssh2_session_error_to_CURLE((int)nwrite);
2949     nwrite = -1;
2950   }
2951
2952   return nwrite;
2953 }
2954
2955 /*
2956  * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
2957  * a regular CURLcode value.
2958  */
2959 static ssize_t scp_recv(struct connectdata *conn, int sockindex,
2960                         char *mem, size_t len, CURLcode *err)
2961 {
2962   ssize_t nread;
2963   (void)sockindex; /* we only support SCP on the fixed known primary socket */
2964
2965   /* libssh2_channel_read() returns int */
2966   nread = (ssize_t)
2967     libssh2_channel_read(conn->proto.sshc.ssh_channel, mem, len);
2968
2969   ssh_block2waitfor(conn, (nread == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
2970   if(nread == LIBSSH2_ERROR_EAGAIN) {
2971     *err = CURLE_AGAIN;
2972     nread = -1;
2973   }
2974
2975   return nread;
2976 }
2977
2978 /*
2979  * =============== SFTP ===============
2980  */
2981
2982 /*
2983  ***********************************************************************
2984  *
2985  * sftp_perform()
2986  *
2987  * This is the actual DO function for SFTP. Get a file/directory according to
2988  * the options previously setup.
2989  */
2990
2991 static
2992 CURLcode sftp_perform(struct connectdata *conn,
2993                       bool *connected,
2994                       bool *dophase_done)
2995 {
2996   CURLcode result = CURLE_OK;
2997
2998   DEBUGF(infof(conn->data, "DO phase starts\n"));
2999
3000   *dophase_done = FALSE; /* not done yet */
3001
3002   /* start the first command in the DO phase */
3003   state(conn, SSH_SFTP_QUOTE_INIT);
3004
3005   /* run the state-machine */
3006   result = ssh_multi_statemach(conn, dophase_done);
3007
3008   *connected = conn->bits.tcpconnect[FIRSTSOCKET];
3009
3010   if(*dophase_done) {
3011     DEBUGF(infof(conn->data, "DO phase is complete\n"));
3012   }
3013
3014   return result;
3015 }
3016
3017 /* called from multi.c while DOing */
3018 static CURLcode sftp_doing(struct connectdata *conn,
3019                            bool *dophase_done)
3020 {
3021   CURLcode result;
3022   result = ssh_multi_statemach(conn, dophase_done);
3023
3024   if(*dophase_done) {
3025     DEBUGF(infof(conn->data, "DO phase is complete\n"));
3026   }
3027   return result;
3028 }
3029
3030 /* BLOCKING, but the function is using the state machine so the only reason
3031    this is still blocking is that the multi interface code has no support for
3032    disconnecting operations that takes a while */
3033 static CURLcode sftp_disconnect(struct connectdata *conn, bool dead_connection)
3034 {
3035   CURLcode result = CURLE_OK;
3036   (void) dead_connection;
3037
3038   DEBUGF(infof(conn->data, "SSH DISCONNECT starts now\n"));
3039
3040   Curl_safefree(conn->data->req.protop);
3041
3042   if(conn->proto.sshc.ssh_session) {
3043     /* only if there's a session still around to use! */
3044     state(conn, SSH_SFTP_SHUTDOWN);
3045     result = ssh_block_statemach(conn, FALSE);
3046   }
3047
3048   DEBUGF(infof(conn->data, "SSH DISCONNECT is done\n"));
3049
3050   return result;
3051
3052 }
3053
3054 static CURLcode sftp_done(struct connectdata *conn, CURLcode status,
3055                                bool premature)
3056 {
3057   struct ssh_conn *sshc = &conn->proto.sshc;
3058
3059   if(status == CURLE_OK) {
3060     /* Post quote commands are executed after the SFTP_CLOSE state to avoid
3061        errors that could happen due to open file handles during POSTQUOTE
3062        operation */
3063     if(!status && !premature && conn->data->set.postquote) {
3064       sshc->nextstate = SSH_SFTP_POSTQUOTE_INIT;
3065       state(conn, SSH_SFTP_CLOSE);
3066     }
3067     else
3068       state(conn, SSH_SFTP_CLOSE);
3069   }
3070   return ssh_done(conn, status);
3071 }
3072
3073 /* return number of sent bytes */
3074 static ssize_t sftp_send(struct connectdata *conn, int sockindex,
3075                          const void *mem, size_t len, CURLcode *err)
3076 {
3077   ssize_t nwrite;   /* libssh2_sftp_write() used to return size_t in 0.14
3078                        but is changed to ssize_t in 0.15. These days we don't
3079                        support libssh2 0.15*/
3080   (void)sockindex;
3081
3082   nwrite = libssh2_sftp_write(conn->proto.sshc.sftp_handle, mem, len);
3083
3084   ssh_block2waitfor(conn, (nwrite == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
3085
3086   if(nwrite == LIBSSH2_ERROR_EAGAIN) {
3087     *err = CURLE_AGAIN;
3088     nwrite = 0;
3089   }
3090   else if(nwrite < LIBSSH2_ERROR_NONE) {
3091     *err = libssh2_session_error_to_CURLE((int)nwrite);
3092     nwrite = -1;
3093   }
3094
3095   return nwrite;
3096 }
3097
3098 /*
3099  * Return number of received (decrypted) bytes
3100  */
3101 static ssize_t sftp_recv(struct connectdata *conn, int sockindex,
3102                          char *mem, size_t len, CURLcode *err)
3103 {
3104   ssize_t nread;
3105   (void)sockindex;
3106
3107   nread = libssh2_sftp_read(conn->proto.sshc.sftp_handle, mem, len);
3108
3109   ssh_block2waitfor(conn, (nread == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
3110
3111   if(nread == LIBSSH2_ERROR_EAGAIN) {
3112     *err = CURLE_AGAIN;
3113     nread = -1;
3114   }
3115   return nread;
3116 }
3117
3118 /* The get_pathname() function is being borrowed from OpenSSH sftp.c
3119    version 4.6p1. */
3120 /*
3121  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
3122  *
3123  * Permission to use, copy, modify, and distribute this software for any
3124  * purpose with or without fee is hereby granted, provided that the above
3125  * copyright notice and this permission notice appear in all copies.
3126  *
3127  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
3128  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
3129  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
3130  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
3131  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
3132  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
3133  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3134  */
3135 static CURLcode
3136 get_pathname(const char **cpp, char **path)
3137 {
3138   const char *cp = *cpp, *end;
3139   char quot;
3140   unsigned int i, j;
3141   static const char WHITESPACE[] = " \t\r\n";
3142
3143   cp += strspn(cp, WHITESPACE);
3144   if(!*cp) {
3145     *cpp = cp;
3146     *path = NULL;
3147     return CURLE_QUOTE_ERROR;
3148   }
3149
3150   *path = malloc(strlen(cp) + 1);
3151   if(*path == NULL)
3152     return CURLE_OUT_OF_MEMORY;
3153
3154   /* Check for quoted filenames */
3155   if(*cp == '\"' || *cp == '\'') {
3156     quot = *cp++;
3157
3158     /* Search for terminating quote, unescape some chars */
3159     for(i = j = 0; i <= strlen(cp); i++) {
3160       if(cp[i] == quot) {  /* Found quote */
3161         i++;
3162         (*path)[j] = '\0';
3163         break;
3164       }
3165       if(cp[i] == '\0') {  /* End of string */
3166         /*error("Unterminated quote");*/
3167         goto fail;
3168       }
3169       if(cp[i] == '\\') {  /* Escaped characters */
3170         i++;
3171         if(cp[i] != '\'' && cp[i] != '\"' &&
3172             cp[i] != '\\') {
3173           /*error("Bad escaped character '\\%c'",
3174               cp[i]);*/
3175           goto fail;
3176         }
3177       }
3178       (*path)[j++] = cp[i];
3179     }
3180
3181     if(j == 0) {
3182       /*error("Empty quotes");*/
3183       goto fail;
3184     }
3185     *cpp = cp + i + strspn(cp + i, WHITESPACE);
3186   }
3187   else {
3188     /* Read to end of filename */
3189     end = strpbrk(cp, WHITESPACE);
3190     if(end == NULL)
3191       end = strchr(cp, '\0');
3192     *cpp = end + strspn(end, WHITESPACE);
3193
3194     memcpy(*path, cp, end - cp);
3195     (*path)[end - cp] = '\0';
3196   }
3197   return CURLE_OK;
3198
3199   fail:
3200     Curl_safefree(*path);
3201     return CURLE_QUOTE_ERROR;
3202 }
3203
3204
3205 static const char *sftp_libssh2_strerror(int err)
3206 {
3207   switch (err) {
3208     case LIBSSH2_FX_NO_SUCH_FILE:
3209       return "No such file or directory";
3210
3211     case LIBSSH2_FX_PERMISSION_DENIED:
3212       return "Permission denied";
3213
3214     case LIBSSH2_FX_FAILURE:
3215       return "Operation failed";
3216
3217     case LIBSSH2_FX_BAD_MESSAGE:
3218       return "Bad message from SFTP server";
3219
3220     case LIBSSH2_FX_NO_CONNECTION:
3221       return "Not connected to SFTP server";
3222
3223     case LIBSSH2_FX_CONNECTION_LOST:
3224       return "Connection to SFTP server lost";
3225
3226     case LIBSSH2_FX_OP_UNSUPPORTED:
3227       return "Operation not supported by SFTP server";
3228
3229     case LIBSSH2_FX_INVALID_HANDLE:
3230       return "Invalid handle";
3231
3232     case LIBSSH2_FX_NO_SUCH_PATH:
3233       return "No such file or directory";
3234
3235     case LIBSSH2_FX_FILE_ALREADY_EXISTS:
3236       return "File already exists";
3237
3238     case LIBSSH2_FX_WRITE_PROTECT:
3239       return "File is write protected";
3240
3241     case LIBSSH2_FX_NO_MEDIA:
3242       return "No media";
3243
3244     case LIBSSH2_FX_NO_SPACE_ON_FILESYSTEM:
3245       return "Disk full";
3246
3247     case LIBSSH2_FX_QUOTA_EXCEEDED:
3248       return "User quota exceeded";
3249
3250     case LIBSSH2_FX_UNKNOWN_PRINCIPLE:
3251       return "Unknown principle";
3252
3253     case LIBSSH2_FX_LOCK_CONFlICT:
3254       return "File lock conflict";
3255
3256     case LIBSSH2_FX_DIR_NOT_EMPTY:
3257       return "Directory not empty";
3258
3259     case LIBSSH2_FX_NOT_A_DIRECTORY:
3260       return "Not a directory";
3261
3262     case LIBSSH2_FX_INVALID_FILENAME:
3263       return "Invalid filename";
3264
3265     case LIBSSH2_FX_LINK_LOOP:
3266       return "Link points to itself";
3267   }
3268   return "Unknown error in libssh2";
3269 }
3270
3271 #endif /* USE_LIBSSH2 */