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