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