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