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