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