Debian Codegraph @

deps/curl/lib/vssh/libssh.c

raw
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2017 - 2020 Red Hat, Inc.
9 *
10 * Authors: Nikos Mavrogiannopoulos, Tomas Mraz, Stanislav Zidek,
11 * Robert Kolcun, Andreas Schneider
12 *
13 * This software is licensed as described in the file COPYING, which
14 * you should have received as part of this distribution. The terms
15 * are also available at https://curl.haxx.se/docs/copyright.html.
16 *
17 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
18 * copies of the Software, and permit persons to whom the Software is
19 * furnished to do so, under the terms of the COPYING file.
20 *
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
23 *
24 ***************************************************************************/
25
26 #include "curl_setup.h"
27
28 #ifdef USE_LIBSSH
29
30 #include <limits.h>
31
32 #include <libssh/libssh.h>
33 #include <libssh/sftp.h>
34
35 #ifdef HAVE_FCNTL_H
36 #include <fcntl.h>
37 #endif
38
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
41 #endif
42 #ifdef HAVE_ARPA_INET_H
43 #include <arpa/inet.h>
44 #endif
45 #ifdef HAVE_UTSNAME_H
46 #include <sys/utsname.h>
47 #endif
48 #ifdef HAVE_NETDB_H
49 #include <netdb.h>
50 #endif
51 #ifdef __VMS
52 #include <in.h>
53 #include <inet.h>
54 #endif
55
56 #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
57 #undef in_addr_t
58 #define in_addr_t unsigned long
59 #endif
60
61 #include <curl/curl.h>
62 #include "urldata.h"
63 #include "sendf.h"
64 #include "hostip.h"
65 #include "progress.h"
66 #include "transfer.h"
67 #include "escape.h"
68 #include "http.h" /* for HTTP proxy tunnel stuff */
69 #include "ssh.h"
70 #include "url.h"
71 #include "speedcheck.h"
72 #include "getinfo.h"
73 #include "strdup.h"
74 #include "strcase.h"
75 #include "vtls/vtls.h"
76 #include "connect.h"
77 #include "strerror.h"
78 #include "inet_ntop.h"
79 #include "parsedate.h" /* for the week day and month names */
80 #include "sockaddr.h" /* required for Curl_sockaddr_storage */
81 #include "strtoofft.h"
82 #include "multiif.h"
83 #include "select.h"
84 #include "warnless.h"
85
86 /* for permission and open flags */
87 #include <sys/types.h>
88 #include <sys/stat.h>
89 #include <unistd.h>
90 #include <fcntl.h>
91
92 /* The last 3 #include files should be in this order */
93 #include "curl_printf.h"
94 #include "curl_memory.h"
95 #include "memdebug.h"
96 #include "curl_path.h"
97
98 /* A recent macro provided by libssh. Or make our own. */
99 #ifndef SSH_STRING_FREE_CHAR
100 #define SSH_STRING_FREE_CHAR(x) \
101 do { \
102 if(x) { \
103 ssh_string_free_char(x); \
104 x = NULL; \
105 } \
106 } while(0)
107 #endif
108
109 /* Local functions: */
110 static CURLcode myssh_connect(struct connectdata *conn, bool *done);
111 static CURLcode myssh_multi_statemach(struct connectdata *conn,
112 bool *done);
113 static CURLcode myssh_do_it(struct connectdata *conn, bool *done);
114
115 static CURLcode scp_done(struct connectdata *conn,
116 CURLcode, bool premature);
117 static CURLcode scp_doing(struct connectdata *conn, bool *dophase_done);
118 static CURLcode scp_disconnect(struct connectdata *conn,
119 bool dead_connection);
120
121 static CURLcode sftp_done(struct connectdata *conn,
122 CURLcode, bool premature);
123 static CURLcode sftp_doing(struct connectdata *conn,
124 bool *dophase_done);
125 static CURLcode sftp_disconnect(struct connectdata *conn, bool dead);
126 static
127 CURLcode sftp_perform(struct connectdata *conn,
128 bool *connected,
129 bool *dophase_done);
130
131 static void sftp_quote(struct connectdata *conn);
132 static void sftp_quote_stat(struct connectdata *conn);
133 static int myssh_getsock(struct connectdata *conn, curl_socket_t *sock);
134 static int myssh_perform_getsock(const struct connectdata *conn,
135 curl_socket_t *sock);
136
137 static CURLcode myssh_setup_connection(struct connectdata *conn);
138
139 /*
140 * SCP protocol handler.
141 */
142
143 const struct Curl_handler Curl_handler_scp = {
144 "SCP", /* scheme */
145 myssh_setup_connection, /* setup_connection */
146 myssh_do_it, /* do_it */
147 scp_done, /* done */
148 ZERO_NULL, /* do_more */
149 myssh_connect, /* connect_it */
150 myssh_multi_statemach, /* connecting */
151 scp_doing, /* doing */
152 myssh_getsock, /* proto_getsock */
153 myssh_getsock, /* doing_getsock */
154 ZERO_NULL, /* domore_getsock */
155 myssh_perform_getsock, /* perform_getsock */
156 scp_disconnect, /* disconnect */
157 ZERO_NULL, /* readwrite */
158 ZERO_NULL, /* connection_check */
159 PORT_SSH, /* defport */
160 CURLPROTO_SCP, /* protocol */
161 PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY /* flags */
162 };
163
164 /*
165 * SFTP protocol handler.
166 */
167
168 const struct Curl_handler Curl_handler_sftp = {
169 "SFTP", /* scheme */
170 myssh_setup_connection, /* setup_connection */
171 myssh_do_it, /* do_it */
172 sftp_done, /* done */
173 ZERO_NULL, /* do_more */
174 myssh_connect, /* connect_it */
175 myssh_multi_statemach, /* connecting */
176 sftp_doing, /* doing */
177 myssh_getsock, /* proto_getsock */
178 myssh_getsock, /* doing_getsock */
179 ZERO_NULL, /* domore_getsock */
180 myssh_perform_getsock, /* perform_getsock */
181 sftp_disconnect, /* disconnect */
182 ZERO_NULL, /* readwrite */
183 ZERO_NULL, /* connection_check */
184 PORT_SSH, /* defport */
185 CURLPROTO_SFTP, /* protocol */
186 PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
187 | PROTOPT_NOURLQUERY /* flags */
188 };
189
190 static CURLcode sftp_error_to_CURLE(int err)
191 {
192 switch(err) {
193 case SSH_FX_OK:
194 return CURLE_OK;
195
196 case SSH_FX_NO_SUCH_FILE:
197 case SSH_FX_NO_SUCH_PATH:
198 return CURLE_REMOTE_FILE_NOT_FOUND;
199
200 case SSH_FX_PERMISSION_DENIED:
201 case SSH_FX_WRITE_PROTECT:
202 return CURLE_REMOTE_ACCESS_DENIED;
203
204 case SSH_FX_FILE_ALREADY_EXISTS:
205 return CURLE_REMOTE_FILE_EXISTS;
206
207 default:
208 break;
209 }
210
211 return CURLE_SSH;
212 }
213
214 #ifndef DEBUGBUILD
215 #define state(x,y) mystate(x,y)
216 #else
217 #define state(x,y) mystate(x,y, __LINE__)
218 #endif
219
220 /*
221 * SSH State machine related code
222 */
223 /* This is the ONLY way to change SSH state! */
224 static void mystate(struct connectdata *conn, sshstate nowstate
225 #ifdef DEBUGBUILD
226 , int lineno
227 #endif
228 )
229 {
230 struct ssh_conn *sshc = &conn->proto.sshc;
231 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
232 /* for debug purposes */
233 static const char *const names[] = {
234 "SSH_STOP",
235 "SSH_INIT",
236 "SSH_S_STARTUP",
237 "SSH_HOSTKEY",
238 "SSH_AUTHLIST",
239 "SSH_AUTH_PKEY_INIT",
240 "SSH_AUTH_PKEY",
241 "SSH_AUTH_PASS_INIT",
242 "SSH_AUTH_PASS",
243 "SSH_AUTH_AGENT_INIT",
244 "SSH_AUTH_AGENT_LIST",
245 "SSH_AUTH_AGENT",
246 "SSH_AUTH_HOST_INIT",
247 "SSH_AUTH_HOST",
248 "SSH_AUTH_KEY_INIT",
249 "SSH_AUTH_KEY",
250 "SSH_AUTH_GSSAPI",
251 "SSH_AUTH_DONE",
252 "SSH_SFTP_INIT",
253 "SSH_SFTP_REALPATH",
254 "SSH_SFTP_QUOTE_INIT",
255 "SSH_SFTP_POSTQUOTE_INIT",
256 "SSH_SFTP_QUOTE",
257 "SSH_SFTP_NEXT_QUOTE",
258 "SSH_SFTP_QUOTE_STAT",
259 "SSH_SFTP_QUOTE_SETSTAT",
260 "SSH_SFTP_QUOTE_SYMLINK",
261 "SSH_SFTP_QUOTE_MKDIR",
262 "SSH_SFTP_QUOTE_RENAME",
263 "SSH_SFTP_QUOTE_RMDIR",
264 "SSH_SFTP_QUOTE_UNLINK",
265 "SSH_SFTP_QUOTE_STATVFS",
266 "SSH_SFTP_GETINFO",
267 "SSH_SFTP_FILETIME",
268 "SSH_SFTP_TRANS_INIT",
269 "SSH_SFTP_UPLOAD_INIT",
270 "SSH_SFTP_CREATE_DIRS_INIT",
271 "SSH_SFTP_CREATE_DIRS",
272 "SSH_SFTP_CREATE_DIRS_MKDIR",
273 "SSH_SFTP_READDIR_INIT",
274 "SSH_SFTP_READDIR",
275 "SSH_SFTP_READDIR_LINK",
276 "SSH_SFTP_READDIR_BOTTOM",
277 "SSH_SFTP_READDIR_DONE",
278 "SSH_SFTP_DOWNLOAD_INIT",
279 "SSH_SFTP_DOWNLOAD_STAT",
280 "SSH_SFTP_CLOSE",
281 "SSH_SFTP_SHUTDOWN",
282 "SSH_SCP_TRANS_INIT",
283 "SSH_SCP_UPLOAD_INIT",
284 "SSH_SCP_DOWNLOAD_INIT",
285 "SSH_SCP_DOWNLOAD",
286 "SSH_SCP_DONE",
287 "SSH_SCP_SEND_EOF",
288 "SSH_SCP_WAIT_EOF",
289 "SSH_SCP_WAIT_CLOSE",
290 "SSH_SCP_CHANNEL_FREE",
291 "SSH_SESSION_DISCONNECT",
292 "SSH_SESSION_FREE",
293 "QUIT"
294 };
295
296
297 if(sshc->state != nowstate) {
298 infof(conn->data, "SSH %p state change from %s to %s (line %d)\n",
299 (void *) sshc, names[sshc->state], names[nowstate],
300 lineno);
301 }
302 #endif
303
304 sshc->state = nowstate;
305 }
306
307 /* Multiple options:
308 * 1. data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5] is set with an MD5
309 * hash (90s style auth, not sure we should have it here)
310 * 2. data->set.ssh_keyfunc callback is set. Then we do trust on first
311 * use. We even save on knownhosts if CURLKHSTAT_FINE_ADD_TO_FILE
312 * is returned by it.
313 * 3. none of the above. We only accept if it is present on known hosts.
314 *
315 * Returns SSH_OK or SSH_ERROR.
316 */
317 static int myssh_is_known(struct connectdata *conn)
318 {
319 int rc;
320 struct Curl_easy *data = conn->data;
321 struct ssh_conn *sshc = &conn->proto.sshc;
322 ssh_key pubkey;
323 size_t hlen;
324 unsigned char *hash = NULL;
325 char *found_base64 = NULL;
326 char *known_base64 = NULL;
327 int vstate;
328 enum curl_khmatch keymatch;
329 struct curl_khkey foundkey;
330 struct curl_khkey *knownkeyp = NULL;
331 curl_sshkeycallback func =
332 data->set.ssh_keyfunc;
333
334 #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0,9,0)
335 struct ssh_knownhosts_entry *knownhostsentry = NULL;
336 struct curl_khkey knownkey;
337 #endif
338
339 #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0,8,0)
340 rc = ssh_get_server_publickey(sshc->ssh_session, &pubkey);
341 #else
342 rc = ssh_get_publickey(sshc->ssh_session, &pubkey);
343 #endif
344 if(rc != SSH_OK)
345 return rc;
346
347 if(data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5]) {
348 rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_MD5,
349 &hash, &hlen);
350 if(rc != SSH_OK)
351 goto cleanup;
352
353 if(hlen != strlen(data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5]) ||
354 memcmp(&data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5], hash, hlen)) {
355 rc = SSH_ERROR;
356 goto cleanup;
357 }
358
359 rc = SSH_OK;
360 goto cleanup;
361 }
362
363 if(data->set.ssl.primary.verifyhost != TRUE) {
364 rc = SSH_OK;
365 goto cleanup;
366 }
367
368 #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0,9,0)
369 /* Get the known_key from the known hosts file */
370 vstate = ssh_session_get_known_hosts_entry(sshc->ssh_session,
371 &knownhostsentry);
372
373 /* Case an entry was found in a known hosts file */
374 if(knownhostsentry) {
375 if(knownhostsentry->publickey) {
376 rc = ssh_pki_export_pubkey_base64(knownhostsentry->publickey,
377 &known_base64);
378 if(rc != SSH_OK) {
379 goto cleanup;
380 }
381 knownkey.key = known_base64;
382 knownkey.len = strlen(known_base64);
383
384 switch(ssh_key_type(knownhostsentry->publickey)) {
385 case SSH_KEYTYPE_RSA:
386 knownkey.keytype = CURLKHTYPE_RSA;
387 break;
388 case SSH_KEYTYPE_RSA1:
389 knownkey.keytype = CURLKHTYPE_RSA1;
390 break;
391 case SSH_KEYTYPE_ECDSA:
392 knownkey.keytype = CURLKHTYPE_ECDSA;
393 break;
394 case SSH_KEYTYPE_ED25519:
395 knownkey.keytype = CURLKHTYPE_ED25519;
396 break;
397 case SSH_KEYTYPE_DSS:
398 knownkey.keytype = CURLKHTYPE_DSS;
399 break;
400 default:
401 rc = SSH_ERROR;
402 goto cleanup;
403 }
404 knownkeyp = &knownkey;
405 }
406 }
407
408 switch(vstate) {
409 case SSH_KNOWN_HOSTS_OK:
410 keymatch = CURLKHMATCH_OK;
411 break;
412 case SSH_KNOWN_HOSTS_OTHER:
413 /* fallthrough */
414 case SSH_KNOWN_HOSTS_NOT_FOUND:
415 /* fallthrough */
416 case SSH_KNOWN_HOSTS_UNKNOWN:
417 /* fallthrough */
418 case SSH_KNOWN_HOSTS_ERROR:
419 keymatch = CURLKHMATCH_MISSING;
420 break;
421 default:
422 keymatch = CURLKHMATCH_MISMATCH;
423 break;
424 }
425
426 #else
427 vstate = ssh_is_server_known(sshc->ssh_session);
428 switch(vstate) {
429 case SSH_SERVER_KNOWN_OK:
430 keymatch = CURLKHMATCH_OK;
431 break;
432 case SSH_SERVER_FILE_NOT_FOUND:
433 /* fallthrough */
434 case SSH_SERVER_NOT_KNOWN:
435 keymatch = CURLKHMATCH_MISSING;
436 break;
437 default:
438 keymatch = CURLKHMATCH_MISMATCH;
439 break;
440 }
441 #endif
442
443 if(func) { /* use callback to determine action */
444 rc = ssh_pki_export_pubkey_base64(pubkey, &found_base64);
445 if(rc != SSH_OK)
446 goto cleanup;
447
448 foundkey.key = found_base64;
449 foundkey.len = strlen(found_base64);
450
451 switch(ssh_key_type(pubkey)) {
452 case SSH_KEYTYPE_RSA:
453 foundkey.keytype = CURLKHTYPE_RSA;
454 break;
455 case SSH_KEYTYPE_RSA1:
456 foundkey.keytype = CURLKHTYPE_RSA1;
457 break;
458 case SSH_KEYTYPE_ECDSA:
459 foundkey.keytype = CURLKHTYPE_ECDSA;
460 break;
461 #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0,7,0)
462 case SSH_KEYTYPE_ED25519:
463 foundkey.keytype = CURLKHTYPE_ED25519;
464 break;
465 #endif
466 case SSH_KEYTYPE_DSS:
467 foundkey.keytype = CURLKHTYPE_DSS;
468 break;
469 default:
470 rc = SSH_ERROR;
471 goto cleanup;
472 }
473
474 Curl_set_in_callback(data, true);
475 rc = func(data, knownkeyp, /* from the knownhosts file */
476 &foundkey, /* from the remote host */
477 keymatch, data->set.ssh_keyfunc_userp);
478 Curl_set_in_callback(data, false);
479
480 switch(rc) {
481 case CURLKHSTAT_FINE_ADD_TO_FILE:
482 #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0,8,0)
483 rc = ssh_session_update_known_hosts(sshc->ssh_session);
484 #else
485 rc = ssh_write_knownhost(sshc->ssh_session);
486 #endif
487 if(rc != SSH_OK) {
488 goto cleanup;
489 }
490 break;
491 case CURLKHSTAT_FINE:
492 break;
493 default: /* REJECT/DEFER */
494 rc = SSH_ERROR;
495 goto cleanup;
496 }
497 }
498 else {
499 if(keymatch != CURLKHMATCH_OK) {
500 rc = SSH_ERROR;
501 goto cleanup;
502 }
503 }
504 rc = SSH_OK;
505
506 cleanup:
507 if(found_base64) {
508 free(found_base64);
509 }
510 if(known_base64) {
511 free(known_base64);
512 }
513 if(hash)
514 ssh_clean_pubkey_hash(&hash);
515 ssh_key_free(pubkey);
516 #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0,9,0)
517 if(knownhostsentry) {
518 ssh_knownhosts_entry_free(knownhostsentry);
519 }
520 #endif
521 return rc;
522 }
523
524 #define MOVE_TO_ERROR_STATE(_r) { \
525 state(conn, SSH_SESSION_DISCONNECT); \
526 sshc->actualcode = _r; \
527 rc = SSH_ERROR; \
528 break; \
529 }
530
531 #define MOVE_TO_SFTP_CLOSE_STATE() { \
532 state(conn, SSH_SFTP_CLOSE); \
533 sshc->actualcode = sftp_error_to_CURLE(sftp_get_error(sshc->sftp_session)); \
534 rc = SSH_ERROR; \
535 break; \
536 }
537
538 #define MOVE_TO_LAST_AUTH \
539 if(sshc->auth_methods & SSH_AUTH_METHOD_PASSWORD) { \
540 rc = SSH_OK; \
541 state(conn, SSH_AUTH_PASS_INIT); \
542 break; \
543 } \
544 else { \
545 MOVE_TO_ERROR_STATE(CURLE_LOGIN_DENIED); \
546 }
547
548 #define MOVE_TO_TERTIARY_AUTH \
549 if(sshc->auth_methods & SSH_AUTH_METHOD_INTERACTIVE) { \
550 rc = SSH_OK; \
551 state(conn, SSH_AUTH_KEY_INIT); \
552 break; \
553 } \
554 else { \
555 MOVE_TO_LAST_AUTH; \
556 }
557
558 #define MOVE_TO_SECONDARY_AUTH \
559 if(sshc->auth_methods & SSH_AUTH_METHOD_GSSAPI_MIC) { \
560 rc = SSH_OK; \
561 state(conn, SSH_AUTH_GSSAPI); \
562 break; \
563 } \
564 else { \
565 MOVE_TO_TERTIARY_AUTH; \
566 }
567
568 static
569 int myssh_auth_interactive(struct connectdata *conn)
570 {
571 int rc;
572 struct ssh_conn *sshc = &conn->proto.sshc;
573 int nprompts;
574
575 restart:
576 switch(sshc->kbd_state) {
577 case 0:
578 rc = ssh_userauth_kbdint(sshc->ssh_session, NULL, NULL);
579 if(rc == SSH_AUTH_AGAIN)
580 return SSH_AGAIN;
581
582 if(rc != SSH_AUTH_INFO)
583 return SSH_ERROR;
584
585 nprompts = ssh_userauth_kbdint_getnprompts(sshc->ssh_session);
586 if(nprompts != 1)
587 return SSH_ERROR;
588
589 rc = ssh_userauth_kbdint_setanswer(sshc->ssh_session, 0, conn->passwd);
590 if(rc < 0)
591 return SSH_ERROR;
592
593 /* FALLTHROUGH */
594 case 1:
595 sshc->kbd_state = 1;
596
597 rc = ssh_userauth_kbdint(sshc->ssh_session, NULL, NULL);
598 if(rc == SSH_AUTH_AGAIN)
599 return SSH_AGAIN;
600 else if(rc == SSH_AUTH_SUCCESS)
601 rc = SSH_OK;
602 else if(rc == SSH_AUTH_INFO) {
603 nprompts = ssh_userauth_kbdint_getnprompts(sshc->ssh_session);
604 if(nprompts != 0)
605 return SSH_ERROR;
606
607 sshc->kbd_state = 2;
608 goto restart;
609 }
610 else
611 rc = SSH_ERROR;
612 break;
613 case 2:
614 sshc->kbd_state = 2;
615
616 rc = ssh_userauth_kbdint(sshc->ssh_session, NULL, NULL);
617 if(rc == SSH_AUTH_AGAIN)
618 return SSH_AGAIN;
619 else if(rc == SSH_AUTH_SUCCESS)
620 rc = SSH_OK;
621 else
622 rc = SSH_ERROR;
623
624 break;
625 default:
626 return SSH_ERROR;
627 }
628
629 sshc->kbd_state = 0;
630 return rc;
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 libssh function returns SSH_AGAIN
637 * meaning it wants to be called again when the socket is ready
638 */
639 static CURLcode myssh_statemach_act(struct connectdata *conn, bool *block)
640 {
641 CURLcode result = CURLE_OK;
642 struct Curl_easy *data = conn->data;
643 struct SSHPROTO *protop = data->req.protop;
644 struct ssh_conn *sshc = &conn->proto.sshc;
645 curl_socket_t sock = conn->sock[FIRSTSOCKET];
646 int rc = SSH_NO_ERROR, err;
647 char *new_readdir_line;
648 int seekerr = CURL_SEEKFUNC_OK;
649 const char *err_msg;
650 *block = 0; /* we're not blocking by default */
651
652 do {
653
654 switch(sshc->state) {
655 case SSH_INIT:
656 sshc->secondCreateDirs = 0;
657 sshc->nextstate = SSH_NO_STATE;
658 sshc->actualcode = CURLE_OK;
659
660 #if 0
661 ssh_set_log_level(SSH_LOG_PROTOCOL);
662 #endif
663
664 /* Set libssh to non-blocking, since everything internally is
665 non-blocking */
666 ssh_set_blocking(sshc->ssh_session, 0);
667
668 state(conn, SSH_S_STARTUP);
669 /* FALLTHROUGH */
670
671 case SSH_S_STARTUP:
672 rc = ssh_connect(sshc->ssh_session);
673 if(rc == SSH_AGAIN)
674 break;
675
676 if(rc != SSH_OK) {
677 failf(data, "Failure establishing ssh session");
678 MOVE_TO_ERROR_STATE(CURLE_FAILED_INIT);
679 }
680
681 state(conn, SSH_HOSTKEY);
682
683 /* FALLTHROUGH */
684 case SSH_HOSTKEY:
685
686 rc = myssh_is_known(conn);
687 if(rc != SSH_OK) {
688 MOVE_TO_ERROR_STATE(CURLE_PEER_FAILED_VERIFICATION);
689 }
690
691 state(conn, SSH_AUTHLIST);
692 /* FALLTHROUGH */
693 case SSH_AUTHLIST:{
694 sshc->authed = FALSE;
695
696 rc = ssh_userauth_none(sshc->ssh_session, NULL);
697 if(rc == SSH_AUTH_AGAIN) {
698 rc = SSH_AGAIN;
699 break;
700 }
701
702 if(rc == SSH_AUTH_SUCCESS) {
703 sshc->authed = TRUE;
704 infof(data, "Authenticated with none\n");
705 state(conn, SSH_AUTH_DONE);
706 break;
707 }
708 else if(rc == SSH_AUTH_ERROR) {
709 MOVE_TO_ERROR_STATE(CURLE_LOGIN_DENIED);
710 }
711
712 sshc->auth_methods = ssh_userauth_list(sshc->ssh_session, NULL);
713 if(sshc->auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
714 state(conn, SSH_AUTH_PKEY_INIT);
715 infof(data, "Authentication using SSH public key file\n");
716 }
717 else if(sshc->auth_methods & SSH_AUTH_METHOD_GSSAPI_MIC) {
718 state(conn, SSH_AUTH_GSSAPI);
719 }
720 else if(sshc->auth_methods & SSH_AUTH_METHOD_INTERACTIVE) {
721 state(conn, SSH_AUTH_KEY_INIT);
722 }
723 else if(sshc->auth_methods & SSH_AUTH_METHOD_PASSWORD) {
724 state(conn, SSH_AUTH_PASS_INIT);
725 }
726 else { /* unsupported authentication method */
727 MOVE_TO_ERROR_STATE(CURLE_LOGIN_DENIED);
728 }
729
730 break;
731 }
732 case SSH_AUTH_PKEY_INIT:
733 if(!(data->set.ssh_auth_types & CURLSSH_AUTH_PUBLICKEY)) {
734 MOVE_TO_SECONDARY_AUTH;
735 }
736
737 /* Two choices, (1) private key was given on CMD,
738 * (2) use the "default" keys. */
739 if(data->set.str[STRING_SSH_PRIVATE_KEY]) {
740 if(sshc->pubkey && !data->set.ssl.key_passwd) {
741 rc = ssh_userauth_try_publickey(sshc->ssh_session, NULL,
742 sshc->pubkey);
743 if(rc == SSH_AUTH_AGAIN) {
744 rc = SSH_AGAIN;
745 break;
746 }
747
748 if(rc != SSH_OK) {
749 MOVE_TO_SECONDARY_AUTH;
750 }
751 }
752
753 rc = ssh_pki_import_privkey_file(data->
754 set.str[STRING_SSH_PRIVATE_KEY],
755 data->set.ssl.key_passwd, NULL,
756 NULL, &sshc->privkey);
757 if(rc != SSH_OK) {
758 failf(data, "Could not load private key file %s",
759 data->set.str[STRING_SSH_PRIVATE_KEY]);
760 MOVE_TO_ERROR_STATE(CURLE_LOGIN_DENIED);
761 break;
762 }
763
764 state(conn, SSH_AUTH_PKEY);
765 break;
766
767 }
768 else {
769 rc = ssh_userauth_publickey_auto(sshc->ssh_session, NULL,
770 data->set.ssl.key_passwd);
771 if(rc == SSH_AUTH_AGAIN) {
772 rc = SSH_AGAIN;
773 break;
774 }
775 if(rc == SSH_AUTH_SUCCESS) {
776 rc = SSH_OK;
777 sshc->authed = TRUE;
778 infof(data, "Completed public key authentication\n");
779 state(conn, SSH_AUTH_DONE);
780 break;
781 }
782
783 MOVE_TO_SECONDARY_AUTH;
784 }
785 break;
786 case SSH_AUTH_PKEY:
787 rc = ssh_userauth_publickey(sshc->ssh_session, NULL, sshc->privkey);
788 if(rc == SSH_AUTH_AGAIN) {
789 rc = SSH_AGAIN;
790 break;
791 }
792
793 if(rc == SSH_AUTH_SUCCESS) {
794 sshc->authed = TRUE;
795 infof(data, "Completed public key authentication\n");
796 state(conn, SSH_AUTH_DONE);
797 break;
798 }
799 else {
800 infof(data, "Failed public key authentication (rc: %d)\n", rc);
801 MOVE_TO_SECONDARY_AUTH;
802 }
803 break;
804
805 case SSH_AUTH_GSSAPI:
806 if(!(data->set.ssh_auth_types & CURLSSH_AUTH_GSSAPI)) {
807 MOVE_TO_TERTIARY_AUTH;
808 }
809
810 rc = ssh_userauth_gssapi(sshc->ssh_session);
811 if(rc == SSH_AUTH_AGAIN) {
812 rc = SSH_AGAIN;
813 break;
814 }
815
816 if(rc == SSH_AUTH_SUCCESS) {
817 rc = SSH_OK;
818 sshc->authed = TRUE;
819 infof(data, "Completed gssapi authentication\n");
820 state(conn, SSH_AUTH_DONE);
821 break;
822 }
823
824 MOVE_TO_TERTIARY_AUTH;
825 break;
826
827 case SSH_AUTH_KEY_INIT:
828 if(data->set.ssh_auth_types & CURLSSH_AUTH_KEYBOARD) {
829 state(conn, SSH_AUTH_KEY);
830 }
831 else {
832 MOVE_TO_LAST_AUTH;
833 }
834 break;
835
836 case SSH_AUTH_KEY:
837
838 /* Authentication failed. Continue with keyboard-interactive now. */
839 rc = myssh_auth_interactive(conn);
840 if(rc == SSH_AGAIN) {
841 break;
842 }
843 if(rc == SSH_OK) {
844 sshc->authed = TRUE;
845 infof(data, "completed keyboard interactive authentication\n");
846 }
847 state(conn, SSH_AUTH_DONE);
848 break;
849
850 case SSH_AUTH_PASS_INIT:
851 if(!(data->set.ssh_auth_types & CURLSSH_AUTH_PASSWORD)) {
852 /* Host key authentication is intentionally not implemented */
853 MOVE_TO_ERROR_STATE(CURLE_LOGIN_DENIED);
854 }
855 state(conn, SSH_AUTH_PASS);
856 /* FALLTHROUGH */
857
858 case SSH_AUTH_PASS:
859 rc = ssh_userauth_password(sshc->ssh_session, NULL, conn->passwd);
860 if(rc == SSH_AUTH_AGAIN) {
861 rc = SSH_AGAIN;
862 break;
863 }
864
865 if(rc == SSH_AUTH_SUCCESS) {
866 sshc->authed = TRUE;
867 infof(data, "Completed password authentication\n");
868 state(conn, SSH_AUTH_DONE);
869 }
870 else {
871 MOVE_TO_ERROR_STATE(CURLE_LOGIN_DENIED);
872 }
873 break;
874
875 case SSH_AUTH_DONE:
876 if(!sshc->authed) {
877 failf(data, "Authentication failure");
878 MOVE_TO_ERROR_STATE(CURLE_LOGIN_DENIED);
879 break;
880 }
881
882 /*
883 * At this point we have an authenticated ssh session.
884 */
885 infof(data, "Authentication complete\n");
886
887 Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSH is connected */
888
889 conn->sockfd = sock;
890 conn->writesockfd = CURL_SOCKET_BAD;
891
892 if(conn->handler->protocol == CURLPROTO_SFTP) {
893 state(conn, SSH_SFTP_INIT);
894 break;
895 }
896 infof(data, "SSH CONNECT phase done\n");
897 state(conn, SSH_STOP);
898 break;
899
900 case SSH_SFTP_INIT:
901 ssh_set_blocking(sshc->ssh_session, 1);
902
903 sshc->sftp_session = sftp_new(sshc->ssh_session);
904 if(!sshc->sftp_session) {
905 failf(data, "Failure initializing sftp session: %s",
906 ssh_get_error(sshc->ssh_session));
907 MOVE_TO_ERROR_STATE(CURLE_COULDNT_CONNECT);
908 break;
909 }
910
911 rc = sftp_init(sshc->sftp_session);
912 if(rc != SSH_OK) {
913 rc = sftp_get_error(sshc->sftp_session);
914 failf(data, "Failure initializing sftp session: %s",
915 ssh_get_error(sshc->ssh_session));
916 MOVE_TO_ERROR_STATE(sftp_error_to_CURLE(rc));
917 break;
918 }
919 state(conn, SSH_SFTP_REALPATH);
920 /* FALLTHROUGH */
921 case SSH_SFTP_REALPATH:
922 /*
923 * Get the "home" directory
924 */
925 sshc->homedir = sftp_canonicalize_path(sshc->sftp_session, ".");
926 if(sshc->homedir == NULL) {
927 MOVE_TO_ERROR_STATE(CURLE_COULDNT_CONNECT);
928 }
929 conn->data->state.most_recent_ftp_entrypath = sshc->homedir;
930
931 /* This is the last step in the SFTP connect phase. Do note that while
932 we get the homedir here, we get the "workingpath" in the DO action
933 since the homedir will remain the same between request but the
934 working path will not. */
935 DEBUGF(infof(data, "SSH CONNECT phase done\n"));
936 state(conn, SSH_STOP);
937 break;
938
939 case SSH_SFTP_QUOTE_INIT:
940
941 result = Curl_getworkingpath(conn, sshc->homedir, &protop->path);
942 if(result) {
943 sshc->actualcode = result;
944 state(conn, SSH_STOP);
945 break;
946 }
947
948 if(data->set.quote) {
949 infof(data, "Sending quote commands\n");
950 sshc->quote_item = data->set.quote;
951 state(conn, SSH_SFTP_QUOTE);
952 }
953 else {
954 state(conn, SSH_SFTP_GETINFO);
955 }
956 break;
957
958 case SSH_SFTP_POSTQUOTE_INIT:
959 if(data->set.postquote) {
960 infof(data, "Sending quote commands\n");
961 sshc->quote_item = data->set.postquote;
962 state(conn, SSH_SFTP_QUOTE);
963 }
964 else {
965 state(conn, SSH_STOP);
966 }
967 break;
968
969 case SSH_SFTP_QUOTE:
970 /* Send any quote commands */
971 sftp_quote(conn);
972 break;
973
974 case SSH_SFTP_NEXT_QUOTE:
975 Curl_safefree(sshc->quote_path1);
976 Curl_safefree(sshc->quote_path2);
977
978 sshc->quote_item = sshc->quote_item->next;
979
980 if(sshc->quote_item) {
981 state(conn, SSH_SFTP_QUOTE);
982 }
983 else {
984 if(sshc->nextstate != SSH_NO_STATE) {
985 state(conn, sshc->nextstate);
986 sshc->nextstate = SSH_NO_STATE;
987 }
988 else {
989 state(conn, SSH_SFTP_GETINFO);
990 }
991 }
992 break;
993
994 case SSH_SFTP_QUOTE_STAT:
995 sftp_quote_stat(conn);
996 break;
997
998 case SSH_SFTP_QUOTE_SETSTAT:
999 rc = sftp_setstat(sshc->sftp_session, sshc->quote_path2,
1000 sshc->quote_attrs);
1001 if(rc != 0 && !sshc->acceptfail) {
1002 Curl_safefree(sshc->quote_path1);
1003 Curl_safefree(sshc->quote_path2);
1004 failf(data, "Attempt to set SFTP stats failed: %s",
1005 ssh_get_error(sshc->ssh_session));
1006 state(conn, SSH_SFTP_CLOSE);
1007 sshc->nextstate = SSH_NO_STATE;
1008 sshc->actualcode = CURLE_QUOTE_ERROR;
1009 /* sshc->actualcode = sftp_error_to_CURLE(err);
1010 * we do not send the actual error; we return
1011 * the error the libssh2 backend is returning */
1012 break;
1013 }
1014 state(conn, SSH_SFTP_NEXT_QUOTE);
1015 break;
1016
1017 case SSH_SFTP_QUOTE_SYMLINK:
1018 rc = sftp_symlink(sshc->sftp_session, sshc->quote_path2,
1019 sshc->quote_path1);
1020 if(rc != 0 && !sshc->acceptfail) {
1021 Curl_safefree(sshc->quote_path1);
1022 Curl_safefree(sshc->quote_path2);
1023 failf(data, "symlink command failed: %s",
1024 ssh_get_error(sshc->ssh_session));
1025 state(conn, SSH_SFTP_CLOSE);
1026 sshc->nextstate = SSH_NO_STATE;
1027 sshc->actualcode = CURLE_QUOTE_ERROR;
1028 break;
1029 }
1030 state(conn, SSH_SFTP_NEXT_QUOTE);
1031 break;
1032
1033 case SSH_SFTP_QUOTE_MKDIR:
1034 rc = sftp_mkdir(sshc->sftp_session, sshc->quote_path1,
1035 (mode_t)data->set.new_directory_perms);
1036 if(rc != 0 && !sshc->acceptfail) {
1037 Curl_safefree(sshc->quote_path1);
1038 failf(data, "mkdir command failed: %s",
1039 ssh_get_error(sshc->ssh_session));
1040 state(conn, SSH_SFTP_CLOSE);
1041 sshc->nextstate = SSH_NO_STATE;
1042 sshc->actualcode = CURLE_QUOTE_ERROR;
1043 break;
1044 }
1045 state(conn, SSH_SFTP_NEXT_QUOTE);
1046 break;
1047
1048 case SSH_SFTP_QUOTE_RENAME:
1049 rc = sftp_rename(sshc->sftp_session, sshc->quote_path1,
1050 sshc->quote_path2);
1051 if(rc != 0 && !sshc->acceptfail) {
1052 Curl_safefree(sshc->quote_path1);
1053 Curl_safefree(sshc->quote_path2);
1054 failf(data, "rename command failed: %s",
1055 ssh_get_error(sshc->ssh_session));
1056 state(conn, SSH_SFTP_CLOSE);
1057 sshc->nextstate = SSH_NO_STATE;
1058 sshc->actualcode = CURLE_QUOTE_ERROR;
1059 break;
1060 }
1061 state(conn, SSH_SFTP_NEXT_QUOTE);
1062 break;
1063
1064 case SSH_SFTP_QUOTE_RMDIR:
1065 rc = sftp_rmdir(sshc->sftp_session, sshc->quote_path1);
1066 if(rc != 0 && !sshc->acceptfail) {
1067 Curl_safefree(sshc->quote_path1);
1068 failf(data, "rmdir command failed: %s",
1069 ssh_get_error(sshc->ssh_session));
1070 state(conn, SSH_SFTP_CLOSE);
1071 sshc->nextstate = SSH_NO_STATE;
1072 sshc->actualcode = CURLE_QUOTE_ERROR;
1073 break;
1074 }
1075 state(conn, SSH_SFTP_NEXT_QUOTE);
1076 break;
1077
1078 case SSH_SFTP_QUOTE_UNLINK:
1079 rc = sftp_unlink(sshc->sftp_session, sshc->quote_path1);
1080 if(rc != 0 && !sshc->acceptfail) {
1081 Curl_safefree(sshc->quote_path1);
1082 failf(data, "rm command failed: %s",
1083 ssh_get_error(sshc->ssh_session));
1084 state(conn, SSH_SFTP_CLOSE);
1085 sshc->nextstate = SSH_NO_STATE;
1086 sshc->actualcode = CURLE_QUOTE_ERROR;
1087 break;
1088 }
1089 state(conn, SSH_SFTP_NEXT_QUOTE);
1090 break;
1091
1092 case SSH_SFTP_QUOTE_STATVFS:
1093 {
1094 sftp_statvfs_t statvfs;
1095
1096 statvfs = sftp_statvfs(sshc->sftp_session, sshc->quote_path1);
1097 if(!statvfs && !sshc->acceptfail) {
1098 Curl_safefree(sshc->quote_path1);
1099 failf(data, "statvfs command failed: %s",
1100 ssh_get_error(sshc->ssh_session));
1101 state(conn, SSH_SFTP_CLOSE);
1102 sshc->nextstate = SSH_NO_STATE;
1103 sshc->actualcode = CURLE_QUOTE_ERROR;
1104 break;
1105 }
1106 else if(statvfs) {
1107 char *tmp = aprintf("statvfs:\n"
1108 "f_bsize: %llu\n" "f_frsize: %llu\n"
1109 "f_blocks: %llu\n" "f_bfree: %llu\n"
1110 "f_bavail: %llu\n" "f_files: %llu\n"
1111 "f_ffree: %llu\n" "f_favail: %llu\n"
1112 "f_fsid: %llu\n" "f_flag: %llu\n"
1113 "f_namemax: %llu\n",
1114 statvfs->f_bsize, statvfs->f_frsize,
1115 statvfs->f_blocks, statvfs->f_bfree,
1116 statvfs->f_bavail, statvfs->f_files,
1117 statvfs->f_ffree, statvfs->f_favail,
1118 statvfs->f_fsid, statvfs->f_flag,
1119 statvfs->f_namemax);
1120 sftp_statvfs_free(statvfs);
1121
1122 if(!tmp) {
1123 result = CURLE_OUT_OF_MEMORY;
1124 state(conn, SSH_SFTP_CLOSE);
1125 sshc->nextstate = SSH_NO_STATE;
1126 break;
1127 }
1128
1129 result = Curl_client_write(conn, CLIENTWRITE_HEADER, tmp, strlen(tmp));
1130 free(tmp);
1131 if(result) {
1132 state(conn, SSH_SFTP_CLOSE);
1133 sshc->nextstate = SSH_NO_STATE;
1134 sshc->actualcode = result;
1135 }
1136 }
1137 state(conn, SSH_SFTP_NEXT_QUOTE);
1138 break;
1139 }
1140
1141 case SSH_SFTP_GETINFO:
1142 if(data->set.get_filetime) {
1143 state(conn, SSH_SFTP_FILETIME);
1144 }
1145 else {
1146 state(conn, SSH_SFTP_TRANS_INIT);
1147 }
1148 break;
1149
1150 case SSH_SFTP_FILETIME:
1151 {
1152 sftp_attributes attrs;
1153
1154 attrs = sftp_stat(sshc->sftp_session, protop->path);
1155 if(attrs != 0) {
1156 data->info.filetime = attrs->mtime;
1157 sftp_attributes_free(attrs);
1158 }
1159
1160 state(conn, SSH_SFTP_TRANS_INIT);
1161 break;
1162 }
1163
1164 case SSH_SFTP_TRANS_INIT:
1165 if(data->set.upload)
1166 state(conn, SSH_SFTP_UPLOAD_INIT);
1167 else {
1168 if(protop->path[strlen(protop->path)-1] == '/')
1169 state(conn, SSH_SFTP_READDIR_INIT);
1170 else
1171 state(conn, SSH_SFTP_DOWNLOAD_INIT);
1172 }
1173 break;
1174
1175 case SSH_SFTP_UPLOAD_INIT:
1176 {
1177 int flags;
1178
1179 if(data->state.resume_from != 0) {
1180 sftp_attributes attrs;
1181
1182 if(data->state.resume_from < 0) {
1183 attrs = sftp_stat(sshc->sftp_session, protop->path);
1184 if(attrs != 0) {
1185 curl_off_t size = attrs->size;
1186 if(size < 0) {
1187 failf(data, "Bad file size (%" CURL_FORMAT_CURL_OFF_T ")", size);
1188 MOVE_TO_ERROR_STATE(CURLE_BAD_DOWNLOAD_RESUME);
1189 }
1190 data->state.resume_from = attrs->size;
1191
1192 sftp_attributes_free(attrs);
1193 }
1194 else {
1195 data->state.resume_from = 0;
1196 }
1197 }
1198 }
1199
1200 if(data->set.ftp_append)
1201 /* Try to open for append, but create if nonexisting */
1202 flags = O_WRONLY|O_CREAT|O_APPEND;
1203 else if(data->state.resume_from > 0)
1204 /* If we have restart position then open for append */
1205 flags = O_WRONLY|O_APPEND;
1206 else
1207 /* Clear file before writing (normal behaviour) */
1208 flags = O_WRONLY|O_CREAT|O_TRUNC;
1209
1210 if(sshc->sftp_file)
1211 sftp_close(sshc->sftp_file);
1212 sshc->sftp_file =
1213 sftp_open(sshc->sftp_session, protop->path,
1214 flags, (mode_t)data->set.new_file_perms);
1215 if(!sshc->sftp_file) {
1216 err = sftp_get_error(sshc->sftp_session);
1217
1218 if(((err == SSH_FX_NO_SUCH_FILE || err == SSH_FX_FAILURE ||
1219 err == SSH_FX_NO_SUCH_PATH)) &&
1220 (data->set.ftp_create_missing_dirs &&
1221 (strlen(protop->path) > 1))) {
1222 /* try to create the path remotely */
1223 rc = 0;
1224 sshc->secondCreateDirs = 1;
1225 state(conn, SSH_SFTP_CREATE_DIRS_INIT);
1226 break;
1227 }
1228 else {
1229 MOVE_TO_SFTP_CLOSE_STATE();
1230 }
1231 }
1232
1233 /* If we have a restart point then we need to seek to the correct
1234 position. */
1235 if(data->state.resume_from > 0) {
1236 /* Let's read off the proper amount of bytes from the input. */
1237 if(conn->seek_func) {
1238 Curl_set_in_callback(data, true);
1239 seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
1240 SEEK_SET);
1241 Curl_set_in_callback(data, false);
1242 }
1243
1244 if(seekerr != CURL_SEEKFUNC_OK) {
1245 curl_off_t passed = 0;
1246
1247 if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
1248 failf(data, "Could not seek stream");
1249 return CURLE_FTP_COULDNT_USE_REST;
1250 }
1251 /* seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
1252 do {
1253 size_t readthisamountnow =
1254 (data->state.resume_from - passed > data->set.buffer_size) ?
1255 (size_t)data->set.buffer_size :
1256 curlx_sotouz(data->state.resume_from - passed);
1257
1258 size_t actuallyread =
1259 data->state.fread_func(data->state.buffer, 1,
1260 readthisamountnow, data->state.in);
1261
1262 passed += actuallyread;
1263 if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
1264 /* this checks for greater-than only to make sure that the
1265 CURL_READFUNC_ABORT return code still aborts */
1266 failf(data, "Failed to read data");
1267 MOVE_TO_ERROR_STATE(CURLE_FTP_COULDNT_USE_REST);
1268 }
1269 } while(passed < data->state.resume_from);
1270 }
1271
1272 /* now, decrease the size of the read */
1273 if(data->state.infilesize > 0) {
1274 data->state.infilesize -= data->state.resume_from;
1275 data->req.size = data->state.infilesize;
1276 Curl_pgrsSetUploadSize(data, data->state.infilesize);
1277 }
1278
1279 rc = sftp_seek64(sshc->sftp_file, data->state.resume_from);
1280 if(rc != 0) {
1281 MOVE_TO_SFTP_CLOSE_STATE();
1282 }
1283 }
1284 if(data->state.infilesize > 0) {
1285 data->req.size = data->state.infilesize;
1286 Curl_pgrsSetUploadSize(data, data->state.infilesize);
1287 }
1288 /* upload data */
1289 Curl_setup_transfer(data, -1, -1, FALSE, FIRSTSOCKET);
1290
1291 /* not set by Curl_setup_transfer to preserve keepon bits */
1292 conn->sockfd = conn->writesockfd;
1293
1294 /* store this original bitmask setup to use later on if we can't
1295 figure out a "real" bitmask */
1296 sshc->orig_waitfor = data->req.keepon;
1297
1298 /* we want to use the _sending_ function even when the socket turns
1299 out readable as the underlying libssh sftp send function will deal
1300 with both accordingly */
1301 conn->cselect_bits = CURL_CSELECT_OUT;
1302
1303 /* since we don't really wait for anything at this point, we want the
1304 state machine to move on as soon as possible so we set a very short
1305 timeout here */
1306 Curl_expire(data, 0, EXPIRE_RUN_NOW);
1307
1308 state(conn, SSH_STOP);
1309 break;
1310 }
1311
1312 case SSH_SFTP_CREATE_DIRS_INIT:
1313 if(strlen(protop->path) > 1) {
1314 sshc->slash_pos = protop->path + 1; /* ignore the leading '/' */
1315 state(conn, SSH_SFTP_CREATE_DIRS);
1316 }
1317 else {
1318 state(conn, SSH_SFTP_UPLOAD_INIT);
1319 }
1320 break;
1321
1322 case SSH_SFTP_CREATE_DIRS:
1323 sshc->slash_pos = strchr(sshc->slash_pos, '/');
1324 if(sshc->slash_pos) {
1325 *sshc->slash_pos = 0;
1326
1327 infof(data, "Creating directory '%s'\n", protop->path);
1328 state(conn, SSH_SFTP_CREATE_DIRS_MKDIR);
1329 break;
1330 }
1331 state(conn, SSH_SFTP_UPLOAD_INIT);
1332 break;
1333
1334 case SSH_SFTP_CREATE_DIRS_MKDIR:
1335 /* 'mode' - parameter is preliminary - default to 0644 */
1336 rc = sftp_mkdir(sshc->sftp_session, protop->path,
1337 (mode_t)data->set.new_directory_perms);
1338 *sshc->slash_pos = '/';
1339 ++sshc->slash_pos;
1340 if(rc < 0) {
1341 /*
1342 * Abort if failure wasn't that the dir already exists or the
1343 * permission was denied (creation might succeed further down the
1344 * path) - retry on unspecific FAILURE also
1345 */
1346 err = sftp_get_error(sshc->sftp_session);
1347 if((err != SSH_FX_FILE_ALREADY_EXISTS) &&
1348 (err != SSH_FX_FAILURE) &&
1349 (err != SSH_FX_PERMISSION_DENIED)) {
1350 MOVE_TO_SFTP_CLOSE_STATE();
1351 }
1352 rc = 0; /* clear rc and continue */
1353 }
1354 state(conn, SSH_SFTP_CREATE_DIRS);
1355 break;
1356
1357 case SSH_SFTP_READDIR_INIT:
1358 Curl_pgrsSetDownloadSize(data, -1);
1359 if(data->set.opt_no_body) {
1360 state(conn, SSH_STOP);
1361 break;
1362 }
1363
1364 /*
1365 * This is a directory that we are trying to get, so produce a directory
1366 * listing
1367 */
1368 sshc->sftp_dir = sftp_opendir(sshc->sftp_session,
1369 protop->path);
1370 if(!sshc->sftp_dir) {
1371 failf(data, "Could not open directory for reading: %s",
1372 ssh_get_error(sshc->ssh_session));
1373 MOVE_TO_SFTP_CLOSE_STATE();
1374 }
1375 state(conn, SSH_SFTP_READDIR);
1376 break;
1377
1378 case SSH_SFTP_READDIR:
1379
1380 if(sshc->readdir_attrs)
1381 sftp_attributes_free(sshc->readdir_attrs);
1382
1383 sshc->readdir_attrs = sftp_readdir(sshc->sftp_session, sshc->sftp_dir);
1384 if(sshc->readdir_attrs) {
1385 sshc->readdir_filename = sshc->readdir_attrs->name;
1386 sshc->readdir_longentry = sshc->readdir_attrs->longname;
1387 sshc->readdir_len = strlen(sshc->readdir_filename);
1388
1389 if(data->set.ftp_list_only) {
1390 char *tmpLine;
1391
1392 tmpLine = aprintf("%s\n", sshc->readdir_filename);
1393 if(tmpLine == NULL) {
1394 state(conn, SSH_SFTP_CLOSE);
1395 sshc->actualcode = CURLE_OUT_OF_MEMORY;
1396 break;
1397 }
1398 result = Curl_client_write(conn, CLIENTWRITE_BODY,
1399 tmpLine, sshc->readdir_len + 1);
1400 free(tmpLine);
1401
1402 if(result) {
1403 state(conn, SSH_STOP);
1404 break;
1405 }
1406 /* since this counts what we send to the client, we include the
1407 newline in this counter */
1408 data->req.bytecount += sshc->readdir_len + 1;
1409
1410 /* output debug output if that is requested */
1411 if(data->set.verbose) {
1412 Curl_debug(data, CURLINFO_DATA_OUT,
1413 (char *)sshc->readdir_filename,
1414 sshc->readdir_len);
1415 }
1416 }
1417 else {
1418 sshc->readdir_currLen = strlen(sshc->readdir_longentry);
1419 sshc->readdir_totalLen = 80 + sshc->readdir_currLen;
1420 sshc->readdir_line = calloc(sshc->readdir_totalLen, 1);
1421 if(!sshc->readdir_line) {
1422 state(conn, SSH_SFTP_CLOSE);
1423 sshc->actualcode = CURLE_OUT_OF_MEMORY;
1424 break;
1425 }
1426
1427 memcpy(sshc->readdir_line, sshc->readdir_longentry,
1428 sshc->readdir_currLen);
1429 if((sshc->readdir_attrs->flags & SSH_FILEXFER_ATTR_PERMISSIONS) &&
1430 ((sshc->readdir_attrs->permissions & S_IFMT) ==
1431 S_IFLNK)) {
1432 sshc->readdir_linkPath = malloc(PATH_MAX + 1);
1433 if(sshc->readdir_linkPath == NULL) {
1434 state(conn, SSH_SFTP_CLOSE);
1435 sshc->actualcode = CURLE_OUT_OF_MEMORY;
1436 break;
1437 }
1438
1439 msnprintf(sshc->readdir_linkPath, PATH_MAX, "%s%s", protop->path,
1440 sshc->readdir_filename);
1441
1442 state(conn, SSH_SFTP_READDIR_LINK);
1443 break;
1444 }
1445 state(conn, SSH_SFTP_READDIR_BOTTOM);
1446 break;
1447 }
1448 }
1449 else if(sftp_dir_eof(sshc->sftp_dir)) {
1450 state(conn, SSH_SFTP_READDIR_DONE);
1451 break;
1452 }
1453 else {
1454 failf(data, "Could not open remote file for reading: %s",
1455 ssh_get_error(sshc->ssh_session));
1456 MOVE_TO_SFTP_CLOSE_STATE();
1457 break;
1458 }
1459 break;
1460
1461 case SSH_SFTP_READDIR_LINK:
1462 if(sshc->readdir_link_attrs)
1463 sftp_attributes_free(sshc->readdir_link_attrs);
1464
1465 sshc->readdir_link_attrs = sftp_lstat(sshc->sftp_session,
1466 sshc->readdir_linkPath);
1467 if(sshc->readdir_link_attrs == 0) {
1468 failf(data, "Could not read symlink for reading: %s",
1469 ssh_get_error(sshc->ssh_session));
1470 MOVE_TO_SFTP_CLOSE_STATE();
1471 }
1472
1473 if(sshc->readdir_link_attrs->name == NULL) {
1474 sshc->readdir_tmp = sftp_readlink(sshc->sftp_session,
1475 sshc->readdir_linkPath);
1476 if(sshc->readdir_filename == NULL)
1477 sshc->readdir_len = 0;
1478 else
1479 sshc->readdir_len = strlen(sshc->readdir_tmp);
1480 sshc->readdir_longentry = NULL;
1481 sshc->readdir_filename = sshc->readdir_tmp;
1482 }
1483 else {
1484 sshc->readdir_len = strlen(sshc->readdir_link_attrs->name);
1485 sshc->readdir_filename = sshc->readdir_link_attrs->name;
1486 sshc->readdir_longentry = sshc->readdir_link_attrs->longname;
1487 }
1488
1489 Curl_safefree(sshc->readdir_linkPath);
1490
1491 /* get room for the filename and extra output */
1492 sshc->readdir_totalLen += 4 + sshc->readdir_len;
1493 new_readdir_line = Curl_saferealloc(sshc->readdir_line,
1494 sshc->readdir_totalLen);
1495 if(!new_readdir_line) {
1496 sshc->readdir_line = NULL;
1497 state(conn, SSH_SFTP_CLOSE);
1498 sshc->actualcode = CURLE_OUT_OF_MEMORY;
1499 break;
1500 }
1501 sshc->readdir_line = new_readdir_line;
1502
1503 sshc->readdir_currLen += msnprintf(sshc->readdir_line +
1504 sshc->readdir_currLen,
1505 sshc->readdir_totalLen -
1506 sshc->readdir_currLen,
1507 " -> %s",
1508 sshc->readdir_filename);
1509
1510 sftp_attributes_free(sshc->readdir_link_attrs);
1511 sshc->readdir_link_attrs = NULL;
1512 sshc->readdir_filename = NULL;
1513 sshc->readdir_longentry = NULL;
1514
1515 state(conn, SSH_SFTP_READDIR_BOTTOM);
1516 /* FALLTHROUGH */
1517 case SSH_SFTP_READDIR_BOTTOM:
1518 sshc->readdir_currLen += msnprintf(sshc->readdir_line +
1519 sshc->readdir_currLen,
1520 sshc->readdir_totalLen -
1521 sshc->readdir_currLen, "\n");
1522 result = Curl_client_write(conn, CLIENTWRITE_BODY,
1523 sshc->readdir_line,
1524 sshc->readdir_currLen);
1525
1526 if(!result) {
1527
1528 /* output debug output if that is requested */
1529 if(data->set.verbose) {
1530 Curl_debug(data, CURLINFO_DATA_OUT, sshc->readdir_line,
1531 sshc->readdir_currLen);
1532 }
1533 data->req.bytecount += sshc->readdir_currLen;
1534 }
1535 Curl_safefree(sshc->readdir_line);
1536 ssh_string_free_char(sshc->readdir_tmp);
1537 sshc->readdir_tmp = NULL;
1538
1539 if(result) {
1540 state(conn, SSH_STOP);
1541 }
1542 else
1543 state(conn, SSH_SFTP_READDIR);
1544 break;
1545
1546 case SSH_SFTP_READDIR_DONE:
1547 sftp_closedir(sshc->sftp_dir);
1548 sshc->sftp_dir = NULL;
1549
1550 /* no data to transfer */
1551 Curl_setup_transfer(data, -1, -1, FALSE, -1);
1552 state(conn, SSH_STOP);
1553 break;
1554
1555 case SSH_SFTP_DOWNLOAD_INIT:
1556 /*
1557 * Work on getting the specified file
1558 */
1559 if(sshc->sftp_file)
1560 sftp_close(sshc->sftp_file);
1561
1562 sshc->sftp_file = sftp_open(sshc->sftp_session, protop->path,
1563 O_RDONLY, (mode_t)data->set.new_file_perms);
1564 if(!sshc->sftp_file) {
1565 failf(data, "Could not open remote file for reading: %s",
1566 ssh_get_error(sshc->ssh_session));
1567
1568 MOVE_TO_SFTP_CLOSE_STATE();
1569 }
1570
1571 state(conn, SSH_SFTP_DOWNLOAD_STAT);
1572 break;
1573
1574 case SSH_SFTP_DOWNLOAD_STAT:
1575 {
1576 sftp_attributes attrs;
1577 curl_off_t size;
1578
1579 attrs = sftp_fstat(sshc->sftp_file);
1580 if(!attrs ||
1581 !(attrs->flags & SSH_FILEXFER_ATTR_SIZE) ||
1582 (attrs->size == 0)) {
1583 /*
1584 * sftp_fstat didn't return an error, so maybe the server
1585 * just doesn't support stat()
1586 * OR the server doesn't return a file size with a stat()
1587 * OR file size is 0
1588 */
1589 data->req.size = -1;
1590 data->req.maxdownload = -1;
1591 Curl_pgrsSetDownloadSize(data, -1);
1592 size = 0;
1593 }
1594 else {
1595 size = attrs->size;
1596
1597 sftp_attributes_free(attrs);
1598
1599 if(size < 0) {
1600 failf(data, "Bad file size (%" CURL_FORMAT_CURL_OFF_T ")", size);
1601 return CURLE_BAD_DOWNLOAD_RESUME;
1602 }
1603 if(conn->data->state.use_range) {
1604 curl_off_t from, to;
1605 char *ptr;
1606 char *ptr2;
1607 CURLofft to_t;
1608 CURLofft from_t;
1609
1610 from_t = curlx_strtoofft(conn->data->state.range, &ptr, 0, &from);
1611 if(from_t == CURL_OFFT_FLOW) {
1612 return CURLE_RANGE_ERROR;
1613 }
1614 while(*ptr && (ISSPACE(*ptr) || (*ptr == '-')))
1615 ptr++;
1616 to_t = curlx_strtoofft(ptr, &ptr2, 0, &to);
1617 if(to_t == CURL_OFFT_FLOW) {
1618 return CURLE_RANGE_ERROR;
1619 }
1620 if((to_t == CURL_OFFT_INVAL) /* no "to" value given */
1621 || (to >= size)) {
1622 to = size - 1;
1623 }
1624 if(from_t) {
1625 /* from is relative to end of file */
1626 from = size - to;
1627 to = size - 1;
1628 }
1629 if(from > size) {
1630 failf(data, "Offset (%"
1631 CURL_FORMAT_CURL_OFF_T ") was beyond file size (%"
1632 CURL_FORMAT_CURL_OFF_T ")", from, size);
1633 return CURLE_BAD_DOWNLOAD_RESUME;
1634 }
1635 if(from > to) {
1636 from = to;
1637 size = 0;
1638 }
1639 else {
1640 size = to - from + 1;
1641 }
1642
1643 rc = sftp_seek64(sshc->sftp_file, from);
1644 if(rc != 0) {
1645 MOVE_TO_SFTP_CLOSE_STATE();
1646 }
1647 }
1648 data->req.size = size;
1649 data->req.maxdownload = size;
1650 Curl_pgrsSetDownloadSize(data, size);
1651 }
1652
1653 /* We can resume if we can seek to the resume position */
1654 if(data->state.resume_from) {
1655 if(data->state.resume_from < 0) {
1656 /* We're supposed to download the last abs(from) bytes */
1657 if((curl_off_t)size < -data->state.resume_from) {
1658 failf(data, "Offset (%"
1659 CURL_FORMAT_CURL_OFF_T ") was beyond file size (%"
1660 CURL_FORMAT_CURL_OFF_T ")",
1661 data->state.resume_from, size);
1662 return CURLE_BAD_DOWNLOAD_RESUME;
1663 }
1664 /* download from where? */
1665 data->state.resume_from += size;
1666 }
1667 else {
1668 if((curl_off_t)size < data->state.resume_from) {
1669 failf(data, "Offset (%" CURL_FORMAT_CURL_OFF_T
1670 ") was beyond file size (%" CURL_FORMAT_CURL_OFF_T ")",
1671 data->state.resume_from, size);
1672 return CURLE_BAD_DOWNLOAD_RESUME;
1673 }
1674 }
1675 /* Now store the number of bytes we are expected to download */
1676 data->req.size = size - data->state.resume_from;
1677 data->req.maxdownload = size - data->state.resume_from;
1678 Curl_pgrsSetDownloadSize(data,
1679 size - data->state.resume_from);
1680
1681 rc = sftp_seek64(sshc->sftp_file, data->state.resume_from);
1682 if(rc != 0) {
1683 MOVE_TO_SFTP_CLOSE_STATE();
1684 }
1685 }
1686 }
1687
1688 /* Setup the actual download */
1689 if(data->req.size == 0) {
1690 /* no data to transfer */
1691 Curl_setup_transfer(data, -1, -1, FALSE, -1);
1692 infof(data, "File already completely downloaded\n");
1693 state(conn, SSH_STOP);
1694 break;
1695 }
1696 Curl_setup_transfer(data, FIRSTSOCKET, data->req.size, FALSE, -1);
1697
1698 /* not set by Curl_setup_transfer to preserve keepon bits */
1699 conn->writesockfd = conn->sockfd;
1700
1701 /* we want to use the _receiving_ function even when the socket turns
1702 out writableable as the underlying libssh recv function will deal
1703 with both accordingly */
1704 conn->cselect_bits = CURL_CSELECT_IN;
1705
1706 if(result) {
1707 /* this should never occur; the close state should be entered
1708 at the time the error occurs */
1709 state(conn, SSH_SFTP_CLOSE);
1710 sshc->actualcode = result;
1711 }
1712 else {
1713 sshc->sftp_recv_state = 0;
1714 state(conn, SSH_STOP);
1715 }
1716 break;
1717
1718 case SSH_SFTP_CLOSE:
1719 if(sshc->sftp_file) {
1720 sftp_close(sshc->sftp_file);
1721 sshc->sftp_file = NULL;
1722 }
1723 Curl_safefree(protop->path);
1724
1725 DEBUGF(infof(data, "SFTP DONE done\n"));
1726
1727 /* Check if nextstate is set and move .nextstate could be POSTQUOTE_INIT
1728 After nextstate is executed, the control should come back to
1729 SSH_SFTP_CLOSE to pass the correct result back */
1730 if(sshc->nextstate != SSH_NO_STATE &&
1731 sshc->nextstate != SSH_SFTP_CLOSE) {
1732 state(conn, sshc->nextstate);
1733 sshc->nextstate = SSH_SFTP_CLOSE;
1734 }
1735 else {
1736 state(conn, SSH_STOP);
1737 result = sshc->actualcode;
1738 }
1739 break;
1740
1741 case SSH_SFTP_SHUTDOWN:
1742 /* during times we get here due to a broken transfer and then the
1743 sftp_handle might not have been taken down so make sure that is done
1744 before we proceed */
1745
1746 if(sshc->sftp_file) {
1747 sftp_close(sshc->sftp_file);
1748 sshc->sftp_file = NULL;
1749 }
1750
1751 if(sshc->sftp_session) {
1752 sftp_free(sshc->sftp_session);
1753 sshc->sftp_session = NULL;
1754 }
1755
1756 SSH_STRING_FREE_CHAR(sshc->homedir);
1757 conn->data->state.most_recent_ftp_entrypath = NULL;
1758
1759 state(conn, SSH_SESSION_DISCONNECT);
1760 break;
1761
1762
1763 case SSH_SCP_TRANS_INIT:
1764 result = Curl_getworkingpath(conn, sshc->homedir, &protop->path);
1765 if(result) {
1766 sshc->actualcode = result;
1767 state(conn, SSH_STOP);
1768 break;
1769 }
1770
1771 /* Functions from the SCP subsystem cannot handle/return SSH_AGAIN */
1772 ssh_set_blocking(sshc->ssh_session, 1);
1773
1774 if(data->set.upload) {
1775 if(data->state.infilesize < 0) {
1776 failf(data, "SCP requires a known file size for upload");
1777 sshc->actualcode = CURLE_UPLOAD_FAILED;
1778 MOVE_TO_ERROR_STATE(CURLE_UPLOAD_FAILED);
1779 }
1780
1781 sshc->scp_session =
1782 ssh_scp_new(sshc->ssh_session, SSH_SCP_WRITE, protop->path);
1783 state(conn, SSH_SCP_UPLOAD_INIT);
1784 }
1785 else {
1786 sshc->scp_session =
1787 ssh_scp_new(sshc->ssh_session, SSH_SCP_READ, protop->path);
1788 state(conn, SSH_SCP_DOWNLOAD_INIT);
1789 }
1790
1791 if(!sshc->scp_session) {
1792 err_msg = ssh_get_error(sshc->ssh_session);
1793 failf(conn->data, "%s", err_msg);
1794 MOVE_TO_ERROR_STATE(CURLE_UPLOAD_FAILED);
1795 }
1796
1797 break;
1798
1799 case SSH_SCP_UPLOAD_INIT:
1800
1801 rc = ssh_scp_init(sshc->scp_session);
1802 if(rc != SSH_OK) {
1803 err_msg = ssh_get_error(sshc->ssh_session);
1804 failf(conn->data, "%s", err_msg);
1805 MOVE_TO_ERROR_STATE(CURLE_UPLOAD_FAILED);
1806 }
1807
1808 rc = ssh_scp_push_file(sshc->scp_session, protop->path,
1809 data->state.infilesize,
1810 (int)data->set.new_file_perms);
1811 if(rc != SSH_OK) {
1812 err_msg = ssh_get_error(sshc->ssh_session);
1813 failf(conn->data, "%s", err_msg);
1814 MOVE_TO_ERROR_STATE(CURLE_UPLOAD_FAILED);
1815 }
1816
1817 /* upload data */
1818 Curl_setup_transfer(data, -1, data->req.size, FALSE, FIRSTSOCKET);
1819
1820 /* not set by Curl_setup_transfer to preserve keepon bits */
1821 conn->sockfd = conn->writesockfd;
1822
1823 /* store this original bitmask setup to use later on if we can't
1824 figure out a "real" bitmask */
1825 sshc->orig_waitfor = data->req.keepon;
1826
1827 /* we want to use the _sending_ function even when the socket turns
1828 out readable as the underlying libssh scp send function will deal
1829 with both accordingly */
1830 conn->cselect_bits = CURL_CSELECT_OUT;
1831
1832 state(conn, SSH_STOP);
1833
1834 break;
1835
1836 case SSH_SCP_DOWNLOAD_INIT:
1837
1838 rc = ssh_scp_init(sshc->scp_session);
1839 if(rc != SSH_OK) {
1840 err_msg = ssh_get_error(sshc->ssh_session);
1841 failf(conn->data, "%s", err_msg);
1842 MOVE_TO_ERROR_STATE(CURLE_COULDNT_CONNECT);
1843 }
1844 state(conn, SSH_SCP_DOWNLOAD);
1845 /* FALLTHROUGH */
1846
1847 case SSH_SCP_DOWNLOAD:{
1848 curl_off_t bytecount;
1849
1850 rc = ssh_scp_pull_request(sshc->scp_session);
1851 if(rc != SSH_SCP_REQUEST_NEWFILE) {
1852 err_msg = ssh_get_error(sshc->ssh_session);
1853 failf(conn->data, "%s", err_msg);
1854 MOVE_TO_ERROR_STATE(CURLE_REMOTE_FILE_NOT_FOUND);
1855 break;
1856 }
1857
1858 /* download data */
1859 bytecount = ssh_scp_request_get_size(sshc->scp_session);
1860 data->req.maxdownload = (curl_off_t) bytecount;
1861 Curl_setup_transfer(data, FIRSTSOCKET, bytecount, FALSE, -1);
1862
1863 /* not set by Curl_setup_transfer to preserve keepon bits */
1864 conn->writesockfd = conn->sockfd;
1865
1866 /* we want to use the _receiving_ function even when the socket turns
1867 out writableable as the underlying libssh recv function will deal
1868 with both accordingly */
1869 conn->cselect_bits = CURL_CSELECT_IN;
1870
1871 state(conn, SSH_STOP);
1872 break;
1873 }
1874 case SSH_SCP_DONE:
1875 if(data->set.upload)
1876 state(conn, SSH_SCP_SEND_EOF);
1877 else
1878 state(conn, SSH_SCP_CHANNEL_FREE);
1879 break;
1880
1881 case SSH_SCP_SEND_EOF:
1882 if(sshc->scp_session) {
1883 rc = ssh_scp_close(sshc->scp_session);
1884 if(rc == SSH_AGAIN) {
1885 /* Currently the ssh_scp_close handles waiting for EOF in
1886 * blocking way.
1887 */
1888 break;
1889 }
1890 if(rc != SSH_OK) {
1891 infof(data, "Failed to close libssh scp channel: %s\n",
1892 ssh_get_error(sshc->ssh_session));
1893 }
1894 }
1895
1896 state(conn, SSH_SCP_CHANNEL_FREE);
1897 break;
1898
1899 case SSH_SCP_CHANNEL_FREE:
1900 if(sshc->scp_session) {
1901 ssh_scp_free(sshc->scp_session);
1902 sshc->scp_session = NULL;
1903 }
1904 DEBUGF(infof(data, "SCP DONE phase complete\n"));
1905
1906 ssh_set_blocking(sshc->ssh_session, 0);
1907
1908 state(conn, SSH_SESSION_DISCONNECT);
1909 /* FALLTHROUGH */
1910
1911 case SSH_SESSION_DISCONNECT:
1912 /* during weird times when we've been prematurely aborted, the channel
1913 is still alive when we reach this state and we MUST kill the channel
1914 properly first */
1915 if(sshc->scp_session) {
1916 ssh_scp_free(sshc->scp_session);
1917 sshc->scp_session = NULL;
1918 }
1919
1920 ssh_disconnect(sshc->ssh_session);
1921
1922 SSH_STRING_FREE_CHAR(sshc->homedir);
1923 conn->data->state.most_recent_ftp_entrypath = NULL;
1924
1925 state(conn, SSH_SESSION_FREE);
1926 /* FALLTHROUGH */
1927 case SSH_SESSION_FREE:
1928 if(sshc->ssh_session) {
1929 ssh_free(sshc->ssh_session);
1930 sshc->ssh_session = NULL;
1931 }
1932
1933 /* worst-case scenario cleanup */
1934
1935 DEBUGASSERT(sshc->ssh_session == NULL);
1936 DEBUGASSERT(sshc->scp_session == NULL);
1937
1938 if(sshc->readdir_tmp) {
1939 ssh_string_free_char(sshc->readdir_tmp);
1940 sshc->readdir_tmp = NULL;
1941 }
1942
1943 if(sshc->quote_attrs)
1944 sftp_attributes_free(sshc->quote_attrs);
1945
1946 if(sshc->readdir_attrs)
1947 sftp_attributes_free(sshc->readdir_attrs);
1948
1949 if(sshc->readdir_link_attrs)
1950 sftp_attributes_free(sshc->readdir_link_attrs);
1951
1952 if(sshc->privkey)
1953 ssh_key_free(sshc->privkey);
1954 if(sshc->pubkey)
1955 ssh_key_free(sshc->pubkey);
1956
1957 Curl_safefree(sshc->rsa_pub);
1958 Curl_safefree(sshc->rsa);
1959 Curl_safefree(sshc->quote_path1);
1960 Curl_safefree(sshc->quote_path2);
1961 Curl_safefree(sshc->readdir_line);
1962 Curl_safefree(sshc->readdir_linkPath);
1963 SSH_STRING_FREE_CHAR(sshc->homedir);
1964
1965 /* the code we are about to return */
1966 result = sshc->actualcode;
1967
1968 memset(sshc, 0, sizeof(struct ssh_conn));
1969
1970 connclose(conn, "SSH session free");
1971 sshc->state = SSH_SESSION_FREE; /* current */
1972 sshc->nextstate = SSH_NO_STATE;
1973 state(conn, SSH_STOP);
1974 break;
1975
1976 case SSH_QUIT:
1977 /* fallthrough, just stop! */
1978 default:
1979 /* internal error */
1980 sshc->nextstate = SSH_NO_STATE;
1981 state(conn, SSH_STOP);
1982 break;
1983
1984 }
1985 } while(!rc && (sshc->state != SSH_STOP));
1986
1987
1988 if(rc == SSH_AGAIN) {
1989 /* we would block, we need to wait for the socket to be ready (in the
1990 right direction too)! */
1991 *block = TRUE;
1992 }
1993
1994 return result;
1995 }
1996
1997
1998 /* called by the multi interface to figure out what socket(s) to wait for and
1999 for what actions in the DO_DONE, PERFORM and WAITPERFORM states */
2000 static int myssh_perform_getsock(const struct connectdata *conn,
2001 curl_socket_t *sock)
2002 {
2003 int bitmap = GETSOCK_BLANK;
2004 sock[0] = conn->sock[FIRSTSOCKET];
2005
2006 if(conn->waitfor & KEEP_RECV)
2007 bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
2008
2009 if(conn->waitfor & KEEP_SEND)
2010 bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
2011
2012 return bitmap;
2013 }
2014
2015 /* Generic function called by the multi interface to figure out what socket(s)
2016 to wait for and for what actions during the DOING and PROTOCONNECT states*/
2017 static int myssh_getsock(struct connectdata *conn,
2018 curl_socket_t *sock)
2019 {
2020 /* if we know the direction we can use the generic *_getsock() function even
2021 for the protocol_connect and doing states */
2022 return myssh_perform_getsock(conn, sock);
2023 }
2024
2025 static void myssh_block2waitfor(struct connectdata *conn, bool block)
2026 {
2027 struct ssh_conn *sshc = &conn->proto.sshc;
2028
2029 /* If it didn't block, or nothing was returned by ssh_get_poll_flags
2030 * have the original set */
2031 conn->waitfor = sshc->orig_waitfor;
2032
2033 if(block) {
2034 int dir = ssh_get_poll_flags(sshc->ssh_session);
2035 if(dir & SSH_READ_PENDING) {
2036 /* translate the libssh define bits into our own bit defines */
2037 conn->waitfor = KEEP_RECV;
2038 }
2039 else if(dir & SSH_WRITE_PENDING) {
2040 conn->waitfor = KEEP_SEND;
2041 }
2042 }
2043 }
2044
2045 /* called repeatedly until done from multi.c */
2046 static CURLcode myssh_multi_statemach(struct connectdata *conn,
2047 bool *done)
2048 {
2049 struct ssh_conn *sshc = &conn->proto.sshc;
2050 bool block; /* we store the status and use that to provide a ssh_getsock()
2051 implementation */
2052 CURLcode result = myssh_statemach_act(conn, &block);
2053
2054 *done = (sshc->state == SSH_STOP) ? TRUE : FALSE;
2055 myssh_block2waitfor(conn, block);
2056
2057 return result;
2058 }
2059
2060 static CURLcode myssh_block_statemach(struct connectdata *conn,
2061 bool disconnect)
2062 {
2063 struct ssh_conn *sshc = &conn->proto.sshc;
2064 CURLcode result = CURLE_OK;
2065 struct Curl_easy *data = conn->data;
2066
2067 while((sshc->state != SSH_STOP) && !result) {
2068 bool block;
2069 timediff_t left = 1000;
2070 struct curltime now = Curl_now();
2071
2072 result = myssh_statemach_act(conn, &block);
2073 if(result)
2074 break;
2075
2076 if(!disconnect) {
2077 if(Curl_pgrsUpdate(conn))
2078 return CURLE_ABORTED_BY_CALLBACK;
2079
2080 result = Curl_speedcheck(data, now);
2081 if(result)
2082 break;
2083
2084 left = Curl_timeleft(data, NULL, FALSE);
2085 if(left < 0) {
2086 failf(data, "Operation timed out");
2087 return CURLE_OPERATION_TIMEDOUT;
2088 }
2089 }
2090
2091 if(block) {
2092 curl_socket_t fd_read = conn->sock[FIRSTSOCKET];
2093 /* wait for the socket to become ready */
2094 (void) Curl_socket_check(fd_read, CURL_SOCKET_BAD,
2095 CURL_SOCKET_BAD, left > 1000 ? 1000 : left);
2096 }
2097
2098 }
2099
2100 return result;
2101 }
2102
2103 /*
2104 * SSH setup connection
2105 */
2106 static CURLcode myssh_setup_connection(struct connectdata *conn)
2107 {
2108 struct SSHPROTO *ssh;
2109
2110 conn->data->req.protop = ssh = calloc(1, sizeof(struct SSHPROTO));
2111 if(!ssh)
2112 return CURLE_OUT_OF_MEMORY;
2113
2114 return CURLE_OK;
2115 }
2116
2117 static Curl_recv scp_recv, sftp_recv;
2118 static Curl_send scp_send, sftp_send;
2119
2120 /*
2121 * Curl_ssh_connect() gets called from Curl_protocol_connect() to allow us to
2122 * do protocol-specific actions at connect-time.
2123 */
2124 static CURLcode myssh_connect(struct connectdata *conn, bool *done)
2125 {
2126 struct ssh_conn *ssh;
2127 CURLcode result;
2128 curl_socket_t sock = conn->sock[FIRSTSOCKET];
2129 struct Curl_easy *data = conn->data;
2130
2131 /* initialize per-handle data if not already */
2132 if(!data->req.protop)
2133 myssh_setup_connection(conn);
2134
2135 /* We default to persistent connections. We set this already in this connect
2136 function to make the re-use checks properly be able to check this bit. */
2137 connkeep(conn, "SSH default");
2138
2139 if(conn->handler->protocol & CURLPROTO_SCP) {
2140 conn->recv[FIRSTSOCKET] = scp_recv;
2141 conn->send[FIRSTSOCKET] = scp_send;
2142 }
2143 else {
2144 conn->recv[FIRSTSOCKET] = sftp_recv;
2145 conn->send[FIRSTSOCKET] = sftp_send;
2146 }
2147
2148 ssh = &conn->proto.sshc;
2149
2150 ssh->ssh_session = ssh_new();
2151 if(ssh->ssh_session == NULL) {
2152 failf(data, "Failure initialising ssh session");
2153 return CURLE_FAILED_INIT;
2154 }
2155
2156 ssh_options_set(ssh->ssh_session, SSH_OPTIONS_FD, &sock);
2157
2158 if(conn->user) {
2159 infof(data, "User: %s\n", conn->user);
2160 ssh_options_set(ssh->ssh_session, SSH_OPTIONS_USER, conn->user);
2161 }
2162
2163 if(data->set.str[STRING_SSH_KNOWNHOSTS]) {
2164 infof(data, "Known hosts: %s\n", data->set.str[STRING_SSH_KNOWNHOSTS]);
2165 ssh_options_set(ssh->ssh_session, SSH_OPTIONS_KNOWNHOSTS,
2166 data->set.str[STRING_SSH_KNOWNHOSTS]);
2167 }
2168
2169 ssh_options_set(ssh->ssh_session, SSH_OPTIONS_HOST, conn->host.name);
2170 if(conn->remote_port)
2171 ssh_options_set(ssh->ssh_session, SSH_OPTIONS_PORT,
2172 &conn->remote_port);
2173
2174 if(data->set.ssh_compression) {
2175 ssh_options_set(ssh->ssh_session, SSH_OPTIONS_COMPRESSION,
2176 "zlib,[email protected],none");
2177 }
2178
2179 ssh->privkey = NULL;
2180 ssh->pubkey = NULL;
2181
2182 if(data->set.str[STRING_SSH_PUBLIC_KEY]) {
2183 int rc = ssh_pki_import_pubkey_file(data->set.str[STRING_SSH_PUBLIC_KEY],
2184 &ssh->pubkey);
2185 if(rc != SSH_OK) {
2186 failf(data, "Could not load public key file");
2187 /* ignore */
2188 }
2189 }
2190
2191 /* we do not verify here, we do it at the state machine,
2192 * after connection */
2193
2194 state(conn, SSH_INIT);
2195
2196 result = myssh_multi_statemach(conn, done);
2197
2198 return result;
2199 }
2200
2201 /* called from multi.c while DOing */
2202 static CURLcode scp_doing(struct connectdata *conn, bool *dophase_done)
2203 {
2204 CURLcode result;
2205
2206 result = myssh_multi_statemach(conn, dophase_done);
2207
2208 if(*dophase_done) {
2209 DEBUGF(infof(conn->data, "DO phase is complete\n"));
2210 }
2211 return result;
2212 }
2213
2214 /*
2215 ***********************************************************************
2216 *
2217 * scp_perform()
2218 *
2219 * This is the actual DO function for SCP. Get a file according to
2220 * the options previously setup.
2221 */
2222
2223 static
2224 CURLcode scp_perform(struct connectdata *conn,
2225 bool *connected, bool *dophase_done)
2226 {
2227 CURLcode result = CURLE_OK;
2228
2229 DEBUGF(infof(conn->data, "DO phase starts\n"));
2230
2231 *dophase_done = FALSE; /* not done yet */
2232
2233 /* start the first command in the DO phase */
2234 state(conn, SSH_SCP_TRANS_INIT);
2235
2236 result = myssh_multi_statemach(conn, dophase_done);
2237
2238 *connected = conn->bits.tcpconnect[FIRSTSOCKET];
2239
2240 if(*dophase_done) {
2241 DEBUGF(infof(conn->data, "DO phase is complete\n"));
2242 }
2243
2244 return result;
2245 }
2246
2247 static CURLcode myssh_do_it(struct connectdata *conn, bool *done)
2248 {
2249 CURLcode result;
2250 bool connected = 0;
2251 struct Curl_easy *data = conn->data;
2252 struct ssh_conn *sshc = &conn->proto.sshc;
2253
2254 *done = FALSE; /* default to false */
2255
2256 data->req.size = -1; /* make sure this is unknown at this point */
2257
2258 sshc->actualcode = CURLE_OK; /* reset error code */
2259 sshc->secondCreateDirs = 0; /* reset the create dir attempt state
2260 variable */
2261
2262 Curl_pgrsSetUploadCounter(data, 0);
2263 Curl_pgrsSetDownloadCounter(data, 0);
2264 Curl_pgrsSetUploadSize(data, -1);
2265 Curl_pgrsSetDownloadSize(data, -1);
2266
2267 if(conn->handler->protocol & CURLPROTO_SCP)
2268 result = scp_perform(conn, &connected, done);
2269 else
2270 result = sftp_perform(conn, &connected, done);
2271
2272 return result;
2273 }
2274
2275 /* BLOCKING, but the function is using the state machine so the only reason
2276 this is still blocking is that the multi interface code has no support for
2277 disconnecting operations that takes a while */
2278 static CURLcode scp_disconnect(struct connectdata *conn,
2279 bool dead_connection)
2280 {
2281 CURLcode result = CURLE_OK;
2282 struct ssh_conn *ssh = &conn->proto.sshc;
2283 (void) dead_connection;
2284
2285 if(ssh->ssh_session) {
2286 /* only if there's a session still around to use! */
2287
2288 state(conn, SSH_SESSION_DISCONNECT);
2289
2290 result = myssh_block_statemach(conn, TRUE);
2291 }
2292
2293 return result;
2294 }
2295
2296 /* generic done function for both SCP and SFTP called from their specific
2297 done functions */
2298 static CURLcode myssh_done(struct connectdata *conn, CURLcode status)
2299 {
2300 CURLcode result = CURLE_OK;
2301 struct SSHPROTO *protop = conn->data->req.protop;
2302
2303 if(!status) {
2304 /* run the state-machine */
2305 result = myssh_block_statemach(conn, FALSE);
2306 }
2307 else
2308 result = status;
2309
2310 if(protop)
2311 Curl_safefree(protop->path);
2312 if(Curl_pgrsDone(conn))
2313 return CURLE_ABORTED_BY_CALLBACK;
2314
2315 conn->data->req.keepon = 0; /* clear all bits */
2316 return result;
2317 }
2318
2319
2320 static CURLcode scp_done(struct connectdata *conn, CURLcode status,
2321 bool premature)
2322 {
2323 (void) premature; /* not used */
2324
2325 if(!status)
2326 state(conn, SSH_SCP_DONE);
2327
2328 return myssh_done(conn, status);
2329
2330 }
2331
2332 static ssize_t scp_send(struct connectdata *conn, int sockindex,
2333 const void *mem, size_t len, CURLcode *err)
2334 {
2335 int rc;
2336 (void) sockindex; /* we only support SCP on the fixed known primary socket */
2337 (void) err;
2338
2339 rc = ssh_scp_write(conn->proto.sshc.scp_session, mem, len);
2340
2341 #if 0
2342 /* The following code is misleading, mostly added as wishful thinking
2343 * that libssh at some point will implement non-blocking ssh_scp_write/read.
2344 * Currently rc can only be number of bytes read or SSH_ERROR. */
2345 myssh_block2waitfor(conn, (rc == SSH_AGAIN) ? TRUE : FALSE);
2346
2347 if(rc == SSH_AGAIN) {
2348 *err = CURLE_AGAIN;
2349 return 0;
2350 }
2351 else
2352 #endif
2353 if(rc != SSH_OK) {
2354 *err = CURLE_SSH;
2355 return -1;
2356 }
2357
2358 return len;
2359 }
2360
2361 static ssize_t scp_recv(struct connectdata *conn, int sockindex,
2362 char *mem, size_t len, CURLcode *err)
2363 {
2364 ssize_t nread;
2365 (void) err;
2366 (void) sockindex; /* we only support SCP on the fixed known primary socket */
2367
2368 /* libssh returns int */
2369 nread = ssh_scp_read(conn->proto.sshc.scp_session, mem, len);
2370
2371 #if 0
2372 /* The following code is misleading, mostly added as wishful thinking
2373 * that libssh at some point will implement non-blocking ssh_scp_write/read.
2374 * Currently rc can only be SSH_OK or SSH_ERROR. */
2375
2376 myssh_block2waitfor(conn, (nread == SSH_AGAIN) ? TRUE : FALSE);
2377 if(nread == SSH_AGAIN) {
2378 *err = CURLE_AGAIN;
2379 nread = -1;
2380 }
2381 #endif
2382
2383 return nread;
2384 }
2385
2386 /*
2387 * =============== SFTP ===============
2388 */
2389
2390 /*
2391 ***********************************************************************
2392 *
2393 * sftp_perform()
2394 *
2395 * This is the actual DO function for SFTP. Get a file/directory according to
2396 * the options previously setup.
2397 */
2398
2399 static
2400 CURLcode sftp_perform(struct connectdata *conn,
2401 bool *connected,
2402 bool *dophase_done)
2403 {
2404 CURLcode result = CURLE_OK;
2405
2406 DEBUGF(infof(conn->data, "DO phase starts\n"));
2407
2408 *dophase_done = FALSE; /* not done yet */
2409
2410 /* start the first command in the DO phase */
2411 state(conn, SSH_SFTP_QUOTE_INIT);
2412
2413 /* run the state-machine */
2414 result = myssh_multi_statemach(conn, dophase_done);
2415
2416 *connected = conn->bits.tcpconnect[FIRSTSOCKET];
2417
2418 if(*dophase_done) {
2419 DEBUGF(infof(conn->data, "DO phase is complete\n"));
2420 }
2421
2422 return result;
2423 }
2424
2425 /* called from multi.c while DOing */
2426 static CURLcode sftp_doing(struct connectdata *conn,
2427 bool *dophase_done)
2428 {
2429 CURLcode result = myssh_multi_statemach(conn, dophase_done);
2430 if(*dophase_done) {
2431 DEBUGF(infof(conn->data, "DO phase is complete\n"));
2432 }
2433 return result;
2434 }
2435
2436 /* BLOCKING, but the function is using the state machine so the only reason
2437 this is still blocking is that the multi interface code has no support for
2438 disconnecting operations that takes a while */
2439 static CURLcode sftp_disconnect(struct connectdata *conn, bool dead_connection)
2440 {
2441 CURLcode result = CURLE_OK;
2442 (void) dead_connection;
2443
2444 DEBUGF(infof(conn->data, "SSH DISCONNECT starts now\n"));
2445
2446 if(conn->proto.sshc.ssh_session) {
2447 /* only if there's a session still around to use! */
2448 state(conn, SSH_SFTP_SHUTDOWN);
2449 result = myssh_block_statemach(conn, TRUE);
2450 }
2451
2452 DEBUGF(infof(conn->data, "SSH DISCONNECT is done\n"));
2453
2454 return result;
2455
2456 }
2457
2458 static CURLcode sftp_done(struct connectdata *conn, CURLcode status,
2459 bool premature)
2460 {
2461 struct ssh_conn *sshc = &conn->proto.sshc;
2462
2463 if(!status) {
2464 /* Post quote commands are executed after the SFTP_CLOSE state to avoid
2465 errors that could happen due to open file handles during POSTQUOTE
2466 operation */
2467 if(!premature && conn->data->set.postquote && !conn->bits.retry)
2468 sshc->nextstate = SSH_SFTP_POSTQUOTE_INIT;
2469 state(conn, SSH_SFTP_CLOSE);
2470 }
2471 return myssh_done(conn, status);
2472 }
2473
2474 /* return number of sent bytes */
2475 static ssize_t sftp_send(struct connectdata *conn, int sockindex,
2476 const void *mem, size_t len, CURLcode *err)
2477 {
2478 ssize_t nwrite;
2479 (void)sockindex;
2480
2481 nwrite = sftp_write(conn->proto.sshc.sftp_file, mem, len);
2482
2483 myssh_block2waitfor(conn, FALSE);
2484
2485 #if 0 /* not returned by libssh on write */
2486 if(nwrite == SSH_AGAIN) {
2487 *err = CURLE_AGAIN;
2488 nwrite = 0;
2489 }
2490 else
2491 #endif
2492 if(nwrite < 0) {
2493 *err = CURLE_SSH;
2494 nwrite = -1;
2495 }
2496
2497 return nwrite;
2498 }
2499
2500 /*
2501 * Return number of received (decrypted) bytes
2502 * or <0 on error
2503 */
2504 static ssize_t sftp_recv(struct connectdata *conn, int sockindex,
2505 char *mem, size_t len, CURLcode *err)
2506 {
2507 ssize_t nread;
2508 (void)sockindex;
2509
2510 DEBUGASSERT(len < CURL_MAX_READ_SIZE);
2511
2512 switch(conn->proto.sshc.sftp_recv_state) {
2513 case 0:
2514 conn->proto.sshc.sftp_file_index =
2515 sftp_async_read_begin(conn->proto.sshc.sftp_file,
2516 (uint32_t)len);
2517 if(conn->proto.sshc.sftp_file_index < 0) {
2518 *err = CURLE_RECV_ERROR;
2519 return -1;
2520 }
2521
2522 /* FALLTHROUGH */
2523 case 1:
2524 conn->proto.sshc.sftp_recv_state = 1;
2525
2526 nread = sftp_async_read(conn->proto.sshc.sftp_file,
2527 mem, (uint32_t)len,
2528 conn->proto.sshc.sftp_file_index);
2529
2530 myssh_block2waitfor(conn, (nread == SSH_AGAIN)?TRUE:FALSE);
2531
2532 if(nread == SSH_AGAIN) {
2533 *err = CURLE_AGAIN;
2534 return -1;
2535 }
2536 else if(nread < 0) {
2537 *err = CURLE_RECV_ERROR;
2538 return -1;
2539 }
2540
2541 conn->proto.sshc.sftp_recv_state = 0;
2542 return nread;
2543
2544 default:
2545 /* we never reach here */
2546 return -1;
2547 }
2548 }
2549
2550 static void sftp_quote(struct connectdata *conn)
2551 {
2552 const char *cp;
2553 struct Curl_easy *data = conn->data;
2554 struct SSHPROTO *protop = data->req.protop;
2555 struct ssh_conn *sshc = &conn->proto.sshc;
2556 CURLcode result;
2557
2558 /*
2559 * Support some of the "FTP" commands
2560 */
2561 char *cmd = sshc->quote_item->data;
2562 sshc->acceptfail = FALSE;
2563
2564 /* if a command starts with an asterisk, which a legal SFTP command never
2565 can, the command will be allowed to fail without it causing any
2566 aborts or cancels etc. It will cause libcurl to act as if the command
2567 is successful, whatever the server reponds. */
2568
2569 if(cmd[0] == '*') {
2570 cmd++;
2571 sshc->acceptfail = TRUE;
2572 }
2573
2574 if(strcasecompare("pwd", cmd)) {
2575 /* output debug output if that is requested */
2576 char *tmp = aprintf("257 \"%s\" is current directory.\n",
2577 protop->path);
2578 if(!tmp) {
2579 sshc->actualcode = CURLE_OUT_OF_MEMORY;
2580 state(conn, SSH_SFTP_CLOSE);
2581 sshc->nextstate = SSH_NO_STATE;
2582 return;
2583 }
2584 if(data->set.verbose) {
2585 Curl_debug(data, CURLINFO_HEADER_OUT, (char *) "PWD\n", 4);
2586 Curl_debug(data, CURLINFO_HEADER_IN, tmp, strlen(tmp));
2587 }
2588 /* this sends an FTP-like "header" to the header callback so that the
2589 current directory can be read very similar to how it is read when
2590 using ordinary FTP. */
2591 result = Curl_client_write(conn, CLIENTWRITE_HEADER, tmp, strlen(tmp));
2592 free(tmp);
2593 if(result) {
2594 state(conn, SSH_SFTP_CLOSE);
2595 sshc->nextstate = SSH_NO_STATE;
2596 sshc->actualcode = result;
2597 }
2598 else
2599 state(conn, SSH_SFTP_NEXT_QUOTE);
2600 return;
2601 }
2602
2603 /*
2604 * the arguments following the command must be separated from the
2605 * command with a space so we can check for it unconditionally
2606 */
2607 cp = strchr(cmd, ' ');
2608 if(cp == NULL) {
2609 failf(data, "Syntax error in SFTP command. Supply parameter(s)!");
2610 state(conn, SSH_SFTP_CLOSE);
2611 sshc->nextstate = SSH_NO_STATE;
2612 sshc->actualcode = CURLE_QUOTE_ERROR;
2613 return;
2614 }
2615
2616 /*
2617 * also, every command takes at least one argument so we get that
2618 * first argument right now
2619 */
2620 result = Curl_get_pathname(&cp, &sshc->quote_path1, sshc->homedir);
2621 if(result) {
2622 if(result == CURLE_OUT_OF_MEMORY)
2623 failf(data, "Out of memory");
2624 else
2625 failf(data, "Syntax error: Bad first parameter");
2626 state(conn, SSH_SFTP_CLOSE);
2627 sshc->nextstate = SSH_NO_STATE;
2628 sshc->actualcode = result;
2629 return;
2630 }
2631
2632 /*
2633 * SFTP is a binary protocol, so we don't send text commands
2634 * to the server. Instead, we scan for commands used by
2635 * OpenSSH's sftp program and call the appropriate libssh
2636 * functions.
2637 */
2638 if(strncasecompare(cmd, "chgrp ", 6) ||
2639 strncasecompare(cmd, "chmod ", 6) ||
2640 strncasecompare(cmd, "chown ", 6)) {
2641 /* attribute change */
2642
2643 /* sshc->quote_path1 contains the mode to set */
2644 /* get the destination */
2645 result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
2646 if(result) {
2647 if(result == CURLE_OUT_OF_MEMORY)
2648 failf(data, "Out of memory");
2649 else
2650 failf(data, "Syntax error in chgrp/chmod/chown: "
2651 "Bad second parameter");
2652 Curl_safefree(sshc->quote_path1);
2653 state(conn, SSH_SFTP_CLOSE);
2654 sshc->nextstate = SSH_NO_STATE;
2655 sshc->actualcode = result;
2656 return;
2657 }
2658 sshc->quote_attrs = NULL;
2659 state(conn, SSH_SFTP_QUOTE_STAT);
2660 return;
2661 }
2662 if(strncasecompare(cmd, "ln ", 3) ||
2663 strncasecompare(cmd, "symlink ", 8)) {
2664 /* symbolic linking */
2665 /* sshc->quote_path1 is the source */
2666 /* get the destination */
2667 result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
2668 if(result) {
2669 if(result == CURLE_OUT_OF_MEMORY)
2670 failf(data, "Out of memory");
2671 else
2672 failf(data, "Syntax error in ln/symlink: Bad second parameter");
2673 Curl_safefree(sshc->quote_path1);
2674 state(conn, SSH_SFTP_CLOSE);
2675 sshc->nextstate = SSH_NO_STATE;
2676 sshc->actualcode = result;
2677 return;
2678 }
2679 state(conn, SSH_SFTP_QUOTE_SYMLINK);
2680 return;
2681 }
2682 else if(strncasecompare(cmd, "mkdir ", 6)) {
2683 /* create dir */
2684 state(conn, SSH_SFTP_QUOTE_MKDIR);
2685 return;
2686 }
2687 else if(strncasecompare(cmd, "rename ", 7)) {
2688 /* rename file */
2689 /* first param is the source path */
2690 /* second param is the dest. path */
2691 result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
2692 if(result) {
2693 if(result == CURLE_OUT_OF_MEMORY)
2694 failf(data, "Out of memory");
2695 else
2696 failf(data, "Syntax error in rename: Bad second parameter");
2697 Curl_safefree(sshc->quote_path1);
2698 state(conn, SSH_SFTP_CLOSE);
2699 sshc->nextstate = SSH_NO_STATE;
2700 sshc->actualcode = result;
2701 return;
2702 }
2703 state(conn, SSH_SFTP_QUOTE_RENAME);
2704 return;
2705 }
2706 else if(strncasecompare(cmd, "rmdir ", 6)) {
2707 /* delete dir */
2708 state(conn, SSH_SFTP_QUOTE_RMDIR);
2709 return;
2710 }
2711 else if(strncasecompare(cmd, "rm ", 3)) {
2712 state(conn, SSH_SFTP_QUOTE_UNLINK);
2713 return;
2714 }
2715 #ifdef HAS_STATVFS_SUPPORT
2716 else if(strncasecompare(cmd, "statvfs ", 8)) {
2717 state(conn, SSH_SFTP_QUOTE_STATVFS);
2718 return;
2719 }
2720 #endif
2721
2722 failf(data, "Unknown SFTP command");
2723 Curl_safefree(sshc->quote_path1);
2724 Curl_safefree(sshc->quote_path2);
2725 state(conn, SSH_SFTP_CLOSE);
2726 sshc->nextstate = SSH_NO_STATE;
2727 sshc->actualcode = CURLE_QUOTE_ERROR;
2728 }
2729
2730 static void sftp_quote_stat(struct connectdata *conn)
2731 {
2732 struct Curl_easy *data = conn->data;
2733 struct ssh_conn *sshc = &conn->proto.sshc;
2734 char *cmd = sshc->quote_item->data;
2735 sshc->acceptfail = FALSE;
2736
2737 /* if a command starts with an asterisk, which a legal SFTP command never
2738 can, the command will be allowed to fail without it causing any
2739 aborts or cancels etc. It will cause libcurl to act as if the command
2740 is successful, whatever the server reponds. */
2741
2742 if(cmd[0] == '*') {
2743 cmd++;
2744 sshc->acceptfail = TRUE;
2745 }
2746
2747 /* We read the file attributes, store them in sshc->quote_attrs
2748 * and modify them accordingly to command. Then we switch to
2749 * QUOTE_SETSTAT state to write new ones.
2750 */
2751
2752 if(sshc->quote_attrs)
2753 sftp_attributes_free(sshc->quote_attrs);
2754 sshc->quote_attrs = sftp_stat(sshc->sftp_session, sshc->quote_path2);
2755 if(sshc->quote_attrs == NULL) {
2756 Curl_safefree(sshc->quote_path1);
2757 Curl_safefree(sshc->quote_path2);
2758 failf(data, "Attempt to get SFTP stats failed: %d",
2759 sftp_get_error(sshc->sftp_session));
2760 state(conn, SSH_SFTP_CLOSE);
2761 sshc->nextstate = SSH_NO_STATE;
2762 sshc->actualcode = CURLE_QUOTE_ERROR;
2763 return;
2764 }
2765
2766 /* Now set the new attributes... */
2767 if(strncasecompare(cmd, "chgrp", 5)) {
2768 sshc->quote_attrs->gid = (uint32_t)strtoul(sshc->quote_path1, NULL, 10);
2769 if(sshc->quote_attrs->gid == 0 && !ISDIGIT(sshc->quote_path1[0]) &&
2770 !sshc->acceptfail) {
2771 Curl_safefree(sshc->quote_path1);
2772 Curl_safefree(sshc->quote_path2);
2773 failf(data, "Syntax error: chgrp gid not a number");
2774 state(conn, SSH_SFTP_CLOSE);
2775 sshc->nextstate = SSH_NO_STATE;
2776 sshc->actualcode = CURLE_QUOTE_ERROR;
2777 return;
2778 }
2779 sshc->quote_attrs->flags |= SSH_FILEXFER_ATTR_UIDGID;
2780 }
2781 else if(strncasecompare(cmd, "chmod", 5)) {
2782 mode_t perms;
2783 perms = (mode_t)strtoul(sshc->quote_path1, NULL, 8);
2784 /* permissions are octal */
2785 if(perms == 0 && !ISDIGIT(sshc->quote_path1[0])) {
2786 Curl_safefree(sshc->quote_path1);
2787 Curl_safefree(sshc->quote_path2);
2788 failf(data, "Syntax error: chmod permissions not a number");
2789 state(conn, SSH_SFTP_CLOSE);
2790 sshc->nextstate = SSH_NO_STATE;
2791 sshc->actualcode = CURLE_QUOTE_ERROR;
2792 return;
2793 }
2794 sshc->quote_attrs->permissions = perms;
2795 sshc->quote_attrs->flags |= SSH_FILEXFER_ATTR_PERMISSIONS;
2796 }
2797 else if(strncasecompare(cmd, "chown", 5)) {
2798 sshc->quote_attrs->uid = (uint32_t)strtoul(sshc->quote_path1, NULL, 10);
2799 if(sshc->quote_attrs->uid == 0 && !ISDIGIT(sshc->quote_path1[0]) &&
2800 !sshc->acceptfail) {
2801 Curl_safefree(sshc->quote_path1);
2802 Curl_safefree(sshc->quote_path2);
2803 failf(data, "Syntax error: chown uid not a number");
2804 state(conn, SSH_SFTP_CLOSE);
2805 sshc->nextstate = SSH_NO_STATE;
2806 sshc->actualcode = CURLE_QUOTE_ERROR;
2807 return;
2808 }
2809 sshc->quote_attrs->flags |= SSH_FILEXFER_ATTR_UIDGID;
2810 }
2811
2812 /* Now send the completed structure... */
2813 state(conn, SSH_SFTP_QUOTE_SETSTAT);
2814 return;
2815 }
2816
2817 CURLcode Curl_ssh_init(void)
2818 {
2819 if(ssh_init()) {
2820 DEBUGF(fprintf(stderr, "Error: libssh_init failed\n"));
2821 return CURLE_FAILED_INIT;
2822 }
2823 return CURLE_OK;
2824 }
2825
2826 void Curl_ssh_cleanup(void)
2827 {
2828 (void)ssh_finalize();
2829 }
2830
2831 size_t Curl_ssh_version(char *buffer, size_t buflen)
2832 {
2833 return msnprintf(buffer, buflen, "libssh/%s", CURL_LIBSSH_VERSION);
2834 }
2835
2836 #endif /* USE_LIBSSH */