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