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