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