Artifactspublic-v0

curl

master
ply4 git calls29 ms305 KiB3 cachedprotocol v2
3212 lines94 KiBRaw
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 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.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 * SPDX-License-Identifier: curl
25 *
26 ***************************************************************************/
27#include "curl_setup.h"
29#ifdef USE_LIBSSH
31#ifdef HAVE_NETINET_IN_H
32#include <netinet/in.h>
33#endif
34#ifdef HAVE_ARPA_INET_H
35#include <arpa/inet.h>
36#endif
37#ifdef HAVE_NETDB_H
38#include <netdb.h>
39#endif
40#ifdef __VMS
41#include <in.h>
42#include <inet.h>
43#endif
45#include "urldata.h"
46#include "sendf.h"
47#include "curl_trc.h"
48#include "progress.h"
49#include "transfer.h"
50#include "vssh/ssh.h"
51#include "url.h"
52#include "cfilters.h"
53#include "connect.h"
54#include "parsedate.h" /* for the week day and month names */
55#include "curlx/strparse.h"
56#include "multiif.h"
57#include "select.h"
58#include "vssh/vssh.h"
59#include "curlx/base64.h" /* for curlx_base64_encode() */
61#ifdef HAVE_UNISTD_H
62#include <unistd.h>
63#endif
64#ifdef HAVE_FCNTL_H
65#include <fcntl.h>
66#endif
68/* A recent macro provided by libssh. Or make our own. */
69#ifndef SSH_STRING_FREE_CHAR
70#define SSH_STRING_FREE_CHAR(x) \
71 do { \
72 if(x) { \
73 ssh_string_free_char(x); \
74 x = NULL; \
75 } \
76 } while(0)
77#endif
79/* These stat values may not be the same as the user's S_IFMT / S_IFLNK */
80#ifndef SSH_S_IFMT
81#define SSH_S_IFMT 00170000
82#endif
83#ifndef SSH_S_IFLNK
84#define SSH_S_IFLNK 0120000
85#endif
87static CURLcode sftp_error_to_CURLE(int err)
89 switch(err) {
90 case SSH_FX_OK:
91 return CURLE_OK;
93 case SSH_FX_NO_SUCH_FILE:
94 case SSH_FX_NO_SUCH_PATH:
95 return CURLE_REMOTE_FILE_NOT_FOUND;
97 case SSH_FX_PERMISSION_DENIED:
98 case SSH_FX_WRITE_PROTECT:
99 return CURLE_REMOTE_ACCESS_DENIED;
101 case SSH_FX_FILE_ALREADY_EXISTS:
102 return CURLE_REMOTE_FILE_EXISTS;
104 default:
105 break;
106 }
108 return CURLE_SSH;
111/* Multiple options:
112 * 1. CURL_EASY_STR(data, STRING_SSH_HOST_PUBLIC_KEY_SHA256) is set
113 * with a SHA256 hash.
114 * 2. CURL_EASY_STR(data, STRING_SSH_HOST_PUBLIC_KEY_MD5) is set
115 * with an MD5 hash (90s style auth, not sure we should have it here)
116 * 3. data->set.ssh_keyfunc callback is set. Then we do trust on first
117 * use. We even save on knownhosts if CURLKHSTAT_FINE_ADD_TO_FILE
118 * is returned by it.
119 * 4. none of the above. We only accept if it is present on known hosts.
120 *
121 * Returns SSH_OK or SSH_ERROR.
122 */
123static int myssh_is_known(struct Curl_easy *data, struct ssh_conn *sshc)
125 int rc;
126 ssh_key pubkey;
127 unsigned char *hash_sha256 = NULL;
128 size_t hlen_sha256;
129 unsigned char *hash_md5 = NULL;
130 size_t hlen_md5;
131 char *found_base64 = NULL;
132 char *known_base64 = NULL;
133 int vstate;
134 enum curl_khmatch keymatch;
135 struct curl_khkey foundkey;
136 struct curl_khkey *knownkeyp = NULL;
137 curl_sshkeycallback func = data->set.ssh_keyfunc;
138 struct ssh_knownhosts_entry *knownhostsentry = NULL;
139 struct curl_khkey knownkey;
141 rc = ssh_get_server_publickey(sshc->ssh_session, &pubkey);
143 if(rc != SSH_OK)
144 return rc;
146 if(CURL_EASY_STR(data, STRING_SSH_HOST_PUBLIC_KEY_SHA256)) {
147 const char *pubkey_sha256 =
148 CURL_EASY_STR(data, STRING_SSH_HOST_PUBLIC_KEY_SHA256);
149 char *fingerprint_b64 = NULL;
150 size_t fingerprint_b64_len;
151 size_t pub_pos = 0;
152 size_t b64_pos = 0;
154 rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA256,
155 &hash_sha256, &hlen_sha256);
156 if(rc != SSH_OK || hlen_sha256 != 32) {
157 failf(data, "Denied establishing ssh session: "
158 "SHA256 fingerprint not available");
159 goto cleanup;
160 }
162 if(curlx_base64_encode((const uint8_t *)hash_sha256, 32, &fingerprint_b64,
163 &fingerprint_b64_len) != CURLE_OK) {
164 rc = SSH_ERROR;
165 goto cleanup;
166 }
168 infof(data, "SSH SHA256 fingerprint: %s", fingerprint_b64);
170 /* Find the position of any = padding characters in the public key */
171 while((pubkey_sha256[pub_pos] != '=') && pubkey_sha256[pub_pos]) {
172 pub_pos++;
173 }
175 /* Find the position of any = padding characters in the base64 coded
176 * hostkey fingerprint */
177 while((fingerprint_b64[b64_pos] != '=') && fingerprint_b64[b64_pos]) {
178 b64_pos++;
179 }
181 /* Before we authenticate we check the hostkey's SHA256 fingerprint
182 * against a known fingerprint, if available.
183 */
184 if((pub_pos != b64_pos) ||
185 strncmp(fingerprint_b64, pubkey_sha256, pub_pos)) {
186 failf(data,
187 "Denied establishing ssh session: mismatch SHA256 fingerprint. "
188 "Remote %s is not equal to %s", fingerprint_b64, pubkey_sha256);
189 curlx_free(fingerprint_b64);
190 rc = SSH_ERROR;
191 goto cleanup;
192 }
194 curlx_free(fingerprint_b64);
196 rc = SSH_OK;
197 goto cleanup;
198 }
200 if(CURL_EASY_STR(data, STRING_SSH_HOST_PUBLIC_KEY_MD5)) {
201 const char *pubkey_md5 =
202 CURL_EASY_STR(data, STRING_SSH_HOST_PUBLIC_KEY_MD5);
203 char md5buffer[33];
204 int i;
206 rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_MD5,
207 &hash_md5, &hlen_md5);
208 if(rc != SSH_OK || hlen_md5 != 16) {
209 failf(data,
210 "Denied establishing ssh session: MD5 fingerprint not available");
211 goto cleanup;
212 }
214 for(i = 0; i < 16; i++)
215 curl_msnprintf(&md5buffer[i * 2], 3, "%02x", hash_md5[i]);
217 infof(data, "SSH MD5 fingerprint: %s", md5buffer);
219 if(!curl_strequal(md5buffer, pubkey_md5)) {
220 failf(data,
221 "Denied establishing ssh session: mismatch MD5 fingerprint. "
222 "Remote %s is not equal to %s", md5buffer, pubkey_md5);
223 rc = SSH_ERROR;
224 goto cleanup;
225 }
227 rc = SSH_OK;
228 goto cleanup;
229 }
231 if(CURL_EASY_STR(data, STRING_SSH_KNOWNHOSTS)) {
233 /* Get the known_key from the known hosts file */
234 vstate = ssh_session_get_known_hosts_entry(sshc->ssh_session,
235 &knownhostsentry);
237 /* Case an entry was found in a known hosts file */
238 if(knownhostsentry) {
239 if(knownhostsentry->publickey) {
240 rc = ssh_pki_export_pubkey_base64(knownhostsentry->publickey,
241 &known_base64);
242 if(rc != SSH_OK) {
243 goto cleanup;
244 }
245 knownkey.key = known_base64;
246 knownkey.len = strlen(known_base64);
248 switch(ssh_key_type(knownhostsentry->publickey)) {
249 case SSH_KEYTYPE_RSA:
250 knownkey.keytype = CURLKHTYPE_RSA;
251 break;
252 case SSH_KEYTYPE_RSA1:
253 knownkey.keytype = CURLKHTYPE_RSA1;
254 break;
255 case SSH_KEYTYPE_ECDSA:
256 case SSH_KEYTYPE_ECDSA_P256:
257 case SSH_KEYTYPE_ECDSA_P384:
258 case SSH_KEYTYPE_ECDSA_P521:
259 knownkey.keytype = CURLKHTYPE_ECDSA;
260 break;
261 case SSH_KEYTYPE_ED25519:
262 knownkey.keytype = CURLKHTYPE_ED25519;
263 break;
264 case SSH_KEYTYPE_DSS:
265 knownkey.keytype = CURLKHTYPE_DSS;
266 break;
267 default:
268 rc = SSH_ERROR;
269 goto cleanup;
270 }
271 knownkeyp = &knownkey;
272 }
273 }
275 switch(vstate) {
276 case SSH_KNOWN_HOSTS_OK:
277 keymatch = CURLKHMATCH_OK;
278 break;
279 case SSH_KNOWN_HOSTS_OTHER:
280 keymatch = CURLKHMATCH_MISMATCH;
281 break;
282 case SSH_KNOWN_HOSTS_NOT_FOUND:
283 case SSH_KNOWN_HOSTS_UNKNOWN:
284 case SSH_KNOWN_HOSTS_ERROR:
285 keymatch = CURLKHMATCH_MISSING;
286 break;
287 default:
288 keymatch = CURLKHMATCH_MISMATCH;
289 break;
290 }
292 if(func) { /* use callback to determine action */
293 struct Curl_mapi_guard guard;
295 rc = ssh_pki_export_pubkey_base64(pubkey, &found_base64);
296 if(rc != SSH_OK)
297 goto cleanup;
299 foundkey.key = found_base64;
300 foundkey.len = strlen(found_base64);
302 switch(ssh_key_type(pubkey)) {
303 case SSH_KEYTYPE_RSA:
304 foundkey.keytype = CURLKHTYPE_RSA;
305 break;
306 case SSH_KEYTYPE_RSA1:
307 foundkey.keytype = CURLKHTYPE_RSA1;
308 break;
309 case SSH_KEYTYPE_ECDSA:
310 case SSH_KEYTYPE_ECDSA_P256:
311 case SSH_KEYTYPE_ECDSA_P384:
312 case SSH_KEYTYPE_ECDSA_P521:
313 foundkey.keytype = CURLKHTYPE_ECDSA;
314 break;
315 case SSH_KEYTYPE_ED25519:
316 foundkey.keytype = CURLKHTYPE_ED25519;
317 break;
318 case SSH_KEYTYPE_DSS:
319 foundkey.keytype = CURLKHTYPE_DSS;
320 break;
321 default:
322 rc = SSH_ERROR;
323 goto cleanup;
324 }
326 CURL_CBAPI_START(&guard, data, easy_ssh_keyfunc);
327 rc = func(data, knownkeyp, /* from the knownhosts file */
328 &foundkey, /* from the remote host */
329 keymatch, data->set.ssh_keyfunc_userp);
330 CURL_CBAPI_END(&guard);
332 switch(rc) {
333 case CURLKHSTAT_FINE_ADD_TO_FILE:
334 rc = ssh_session_update_known_hosts(sshc->ssh_session);
335 if(rc != SSH_OK) {
336 goto cleanup;
337 }
338 break;
339 case CURLKHSTAT_FINE:
340 break;
341 default: /* REJECT/DEFER */
342 rc = SSH_ERROR;
343 goto cleanup;
344 }
345 }
346 else {
347 if(keymatch != CURLKHMATCH_OK) {
348 rc = SSH_ERROR;
349 goto cleanup;
350 }
351 }
352 }
353 rc = SSH_OK;
355cleanup:
356 if(found_base64) {
357 /* !checksrc! disable BANNEDFUNC 1 */
358 free(found_base64); /* allocated by libssh, deallocate with system free */
359 }
360 if(known_base64) {
361 /* !checksrc! disable BANNEDFUNC 1 */
362 free(known_base64); /* allocated by libssh, deallocate with system free */
363 }
364 if(hash_sha256)
365 ssh_clean_pubkey_hash(&hash_sha256);
366 if(hash_md5)
367 ssh_clean_pubkey_hash(&hash_md5);
368 ssh_key_free(pubkey);
369 if(knownhostsentry) {
370 ssh_knownhosts_entry_free(knownhostsentry);
371 }
372 return rc;
375static int myssh_to_ERROR(struct Curl_easy *data,
376 struct ssh_conn *sshc,
377 CURLcode result)
379 myssh_to(data, sshc, SSH_SESSION_DISCONNECT);
380 sshc->actualcode = result;
381 return SSH_ERROR;
384static int myssh_to_SFTP_CLOSE(struct Curl_easy *data,
385 struct ssh_conn *sshc)
387 myssh_to(data, sshc, SSH_SFTP_CLOSE);
388 sshc->actualcode = sftp_error_to_CURLE(sftp_get_error(sshc->sftp_session));
389 return SSH_ERROR;
392static int myssh_to_PASSWD_AUTH(struct Curl_easy *data,
393 struct ssh_conn *sshc)
395 if(sshc->auth_methods & SSH_AUTH_METHOD_PASSWORD) {
396 myssh_to(data, sshc, SSH_AUTH_PASS_INIT);
397 return SSH_OK;
398 }
399 return myssh_to_ERROR(data, sshc, CURLE_LOGIN_DENIED);
402static int myssh_to_KEY_AUTH(struct Curl_easy *data,
403 struct ssh_conn *sshc)
405 if(sshc->auth_methods & SSH_AUTH_METHOD_INTERACTIVE) {
406 myssh_to(data, sshc, SSH_AUTH_KEY_INIT);
407 return SSH_OK;
408 }
409 return myssh_to_PASSWD_AUTH(data, sshc);
412static int myssh_to_GSSAPI_AUTH(struct Curl_easy *data,
413 struct ssh_conn *sshc)
415 if(sshc->auth_methods & SSH_AUTH_METHOD_GSSAPI_MIC) {
416 myssh_to(data, sshc, SSH_AUTH_GSSAPI);
417 return SSH_OK;
418 }
419 return myssh_to_KEY_AUTH(data, sshc);
422/* The key file did not authenticate. A file the user named
423 (CURLOPT_SSH_PRIVATE_KEYFILE) ends public key auth here. The default guess
424 from Curl_ssh_setup_pkey() does not: let libssh try ssh-agent and its
425 default key files, the way the libssh2 backend goes on to the agent. The
426 agent search needs CURLSSH_AUTH_AGENT, as in myssh_in_AUTHLIST(). */
427static int myssh_to_PKEY_AUTO(struct Curl_easy *data,
428 struct ssh_conn *sshc)
430 if(CURL_EASY_STR(data, STRING_SSH_PRIVATE_KEY) ||
431 !(data->set.ssh_auth_types & CURLSSH_AUTH_AGENT))
432 return myssh_to_GSSAPI_AUTH(data, sshc);
433 myssh_to(data, sshc, SSH_AUTH_PKEY_AUTO);
434 return SSH_OK;
437static int myssh_in_SFTP_READDIR_INIT(struct Curl_easy *data,
438 struct ssh_conn *sshc,
439 struct SSHPROTO *sshp)
441 if(!sshp)
442 return myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
443 Curl_pgrsSetDownloadSize(data, -1);
444 if(data->req.no_body) {
445 myssh_to(data, sshc, SSH_STOP);
446 return SSH_NO_ERROR;
447 }
449 /*
450 * This is a directory that we are trying to get, so produce a directory
451 * listing
452 */
453 sshc->sftp_dir = sftp_opendir(sshc->sftp_session,
454 sshp->path);
455 if(!sshc->sftp_dir) {
456 failf(data, "Could not open directory for reading: %s",
457 ssh_get_error(sshc->ssh_session));
458 return myssh_to_SFTP_CLOSE(data, sshc);
459 }
460 myssh_to(data, sshc, SSH_SFTP_READDIR);
461 return SSH_NO_ERROR;
464static int myssh_in_SFTP_READDIR(struct Curl_easy *data,
465 struct ssh_conn *sshc,
466 struct SSHPROTO *sshp)
468 CURLcode result = CURLE_OK;
470 if(!sshp)
471 return myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
472 curlx_dyn_reset(&sshc->readdir_buf);
473 if(sshc->readdir_attrs)
474 sftp_attributes_free(sshc->readdir_attrs);
476 sshc->readdir_attrs = sftp_readdir(sshc->sftp_session, sshc->sftp_dir);
477 if(sshc->readdir_attrs) {
478 sshc->readdir_filename = sshc->readdir_attrs->name;
479 sshc->readdir_longentry = sshc->readdir_attrs->longname;
480 sshc->readdir_len = strlen(sshc->readdir_filename);
482 if(data->set.list_only) {
483 char *tmpLine;
485 tmpLine = curl_maprintf("%s\n", sshc->readdir_filename);
486 if(!tmpLine) {
487 myssh_to(data, sshc, SSH_SFTP_CLOSE);
488 sshc->actualcode = CURLE_OUT_OF_MEMORY;
489 return SSH_ERROR;
490 }
491 result = Curl_client_write(data, CLIENTWRITE_BODY,
492 tmpLine, sshc->readdir_len + 1);
493 curlx_free(tmpLine);
495 if(result) {
496 myssh_to(data, sshc, SSH_STOP);
497 sshc->actualcode = result;
498 return SSH_NO_ERROR;
499 }
500 }
501 else {
502 if(curlx_dyn_add(&sshc->readdir_buf, sshc->readdir_longentry)) {
503 sshc->actualcode = CURLE_OUT_OF_MEMORY;
504 myssh_to(data, sshc, SSH_STOP);
505 return SSH_ERROR;
506 }
508 if((sshc->readdir_attrs->flags & SSH_FILEXFER_ATTR_PERMISSIONS) &&
509 ((sshc->readdir_attrs->permissions & SSH_S_IFMT) ==
510 SSH_S_IFLNK)) {
511 sshc->readdir_linkPath = curl_maprintf("%s%s", sshp->path,
512 sshc->readdir_filename);
514 if(!sshc->readdir_linkPath) {
515 myssh_to(data, sshc, SSH_SFTP_CLOSE);
516 sshc->actualcode = CURLE_OUT_OF_MEMORY;
517 return SSH_ERROR;
518 }
520 myssh_to(data, sshc, SSH_SFTP_READDIR_LINK);
521 return SSH_NO_ERROR;
522 }
523 myssh_to(data, sshc, SSH_SFTP_READDIR_BOTTOM);
524 return SSH_NO_ERROR;
525 }
526 }
527 else if(sftp_dir_eof(sshc->sftp_dir)) {
528 myssh_to(data, sshc, SSH_SFTP_READDIR_DONE);
529 }
530 else {
531 failf(data, "Could not open remote directory for reading: %s",
532 ssh_get_error(sshc->ssh_session));
533 return myssh_to_SFTP_CLOSE(data, sshc);
534 }
535 return SSH_NO_ERROR;
538static int myssh_in_SFTP_READDIR_LINK(struct Curl_easy *data,
539 struct ssh_conn *sshc)
541 if(sshc->readdir_link_attrs)
542 sftp_attributes_free(sshc->readdir_link_attrs);
544 sshc->readdir_link_attrs = sftp_lstat(sshc->sftp_session,
545 sshc->readdir_linkPath);
546 if(!sshc->readdir_link_attrs) {
547 failf(data, "Could not read symlink for reading: %s",
548 ssh_get_error(sshc->ssh_session));
549 return myssh_to_SFTP_CLOSE(data, sshc);
550 }
552 if(!sshc->readdir_link_attrs->name) {
553 sshc->readdir_tmp = sftp_readlink(sshc->sftp_session,
554 sshc->readdir_linkPath);
555 if(!sshc->readdir_tmp)
556 sshc->readdir_len = 0;
557 else
558 sshc->readdir_len = strlen(sshc->readdir_tmp);
559 sshc->readdir_longentry = NULL;
560 sshc->readdir_filename = sshc->readdir_tmp;
561 }
562 else {
563 sshc->readdir_len = strlen(sshc->readdir_link_attrs->name);
564 sshc->readdir_filename = sshc->readdir_link_attrs->name;
565 sshc->readdir_longentry = sshc->readdir_link_attrs->longname;
566 }
568 curlx_safefree(sshc->readdir_linkPath);
570 if(curlx_dyn_addf(&sshc->readdir_buf, " -> %s", sshc->readdir_filename)) {
571 /* Not using:
572 * return myssh_to_SFTP_CLOSE(data, sshc);
573 *
574 * as that assumes an sftp related error while
575 * assigning sshc->actualcode whereas the current
576 * error is curlx_dyn_addf() related.
577 */
578 myssh_to(data, sshc, SSH_SFTP_CLOSE);
579 sshc->actualcode = CURLE_OUT_OF_MEMORY;
580 return SSH_ERROR;
581 }
583 sftp_attributes_free(sshc->readdir_link_attrs);
584 sshc->readdir_link_attrs = NULL;
585 sshc->readdir_filename = NULL;
586 sshc->readdir_longentry = NULL;
588 myssh_to(data, sshc, SSH_SFTP_READDIR_BOTTOM);
589 return SSH_NO_ERROR;
592static int myssh_in_SFTP_READDIR_BOTTOM(struct Curl_easy *data,
593 struct ssh_conn *sshc)
595 CURLcode result;
597 if(curlx_dyn_addn(&sshc->readdir_buf, "\n", 1))
598 result = CURLE_OUT_OF_MEMORY;
599 else
600 result = Curl_client_write(data, CLIENTWRITE_BODY,
601 curlx_dyn_ptr(&sshc->readdir_buf),
602 curlx_dyn_len(&sshc->readdir_buf));
604 ssh_string_free_char(sshc->readdir_tmp);
605 sshc->readdir_tmp = NULL;
607 if(result) {
608 myssh_to(data, sshc, SSH_STOP);
609 sshc->actualcode = result;
610 }
611 else
612 myssh_to(data, sshc, SSH_SFTP_READDIR);
613 return SSH_NO_ERROR;
616static int myssh_in_SFTP_READDIR_DONE(struct Curl_easy *data,
617 struct ssh_conn *sshc)
619 sftp_closedir(sshc->sftp_dir);
620 sshc->sftp_dir = NULL;
622 /* no data to transfer */
623 Curl_xfer_setup_nop(data);
624 myssh_to(data, sshc, SSH_STOP);
625 return SSH_NO_ERROR;
628static void myssh_quote_error(struct Curl_easy *data, struct ssh_conn *sshc,
629 const char *cmd)
631 if(cmd)
632 failf(data, "%s command failed: %s", cmd,
633 ssh_get_error(sshc->ssh_session));
634 curlx_safefree(sshc->quote_path1);
635 curlx_safefree(sshc->quote_path2);
636 myssh_to(data, sshc, SSH_SFTP_CLOSE);
637 sshc->nextstate = SSH_NO_STATE;
638 sshc->actualcode = CURLE_QUOTE_ERROR;
641static int myssh_in_SFTP_QUOTE_STATVFS(struct Curl_easy *data,
642 struct ssh_conn *sshc)
644 sftp_statvfs_t statvfs;
646 statvfs = sftp_statvfs(sshc->sftp_session, sshc->quote_path1);
647 if(!statvfs && !sshc->acceptfail) {
648 myssh_quote_error(data, sshc, "statvfs");
649 return SSH_OK;
650 }
651 else if(statvfs) {
652#ifdef _MSC_VER
653#define CURL_LIBSSH_VFS_SIZE_MASK "I64u"
654#elif defined(__MINGW32__) && (__MINGW64_VERSION_MAJOR <= 6)
655#define CURL_LIBSSH_VFS_SIZE_MASK "llu"
656#else
657#define CURL_LIBSSH_VFS_SIZE_MASK PRIu64
658#endif
659 CURLcode result = CURLE_OK;
660 char *tmp = curl_maprintf("statvfs:\n"
661 "f_bsize: %" CURL_LIBSSH_VFS_SIZE_MASK "\n"
662 "f_frsize: %" CURL_LIBSSH_VFS_SIZE_MASK "\n"
663 "f_blocks: %" CURL_LIBSSH_VFS_SIZE_MASK "\n"
664 "f_bfree: %" CURL_LIBSSH_VFS_SIZE_MASK "\n"
665 "f_bavail: %" CURL_LIBSSH_VFS_SIZE_MASK "\n"
666 "f_files: %" CURL_LIBSSH_VFS_SIZE_MASK "\n"
667 "f_ffree: %" CURL_LIBSSH_VFS_SIZE_MASK "\n"
668 "f_favail: %" CURL_LIBSSH_VFS_SIZE_MASK "\n"
669 "f_fsid: %" CURL_LIBSSH_VFS_SIZE_MASK "\n"
670 "f_flag: %" CURL_LIBSSH_VFS_SIZE_MASK "\n"
671 "f_namemax: %" CURL_LIBSSH_VFS_SIZE_MASK "\n",
672 statvfs->f_bsize, statvfs->f_frsize,
673 statvfs->f_blocks, statvfs->f_bfree,
674 statvfs->f_bavail, statvfs->f_files,
675 statvfs->f_ffree, statvfs->f_favail,
676 statvfs->f_fsid, statvfs->f_flag,
677 statvfs->f_namemax);
678 sftp_statvfs_free(statvfs);
680 if(!tmp)
681 result = CURLE_OUT_OF_MEMORY;
683 if(!result) {
684 result = Curl_client_write(data, CLIENTWRITE_HEADER, tmp, strlen(tmp));
685 curlx_free(tmp);
686 }
687 if(result) {
688 myssh_to(data, sshc, SSH_SFTP_CLOSE);
689 sshc->nextstate = SSH_NO_STATE;
690 sshc->actualcode = result;
691 }
692 }
693 myssh_to(data, sshc, SSH_SFTP_NEXT_QUOTE);
694 return SSH_OK;
697static int myssh_auth_interactive(struct connectdata *conn,
698 struct ssh_conn *sshc)
700 int rc;
701 int nprompts;
703restart:
704 switch(sshc->kbd_state) {
705 case 0:
706 rc = ssh_userauth_kbdint(sshc->ssh_session, NULL, NULL);
707 if(rc == SSH_AUTH_AGAIN)
708 return SSH_AGAIN;
710 if(rc != SSH_AUTH_INFO)
711 return SSH_ERROR;
713 nprompts = ssh_userauth_kbdint_getnprompts(sshc->ssh_session);
714 if(nprompts != 1)
715 return SSH_ERROR;
717 rc = ssh_userauth_kbdint_setanswer(sshc->ssh_session, 0,
718 Curl_creds_passwd(conn->creds));
719 if(rc < 0)
720 return SSH_ERROR;
722 FALLTHROUGH();
723 case 1:
724 sshc->kbd_state = 1;
726 rc = ssh_userauth_kbdint(sshc->ssh_session, NULL, NULL);
727 if(rc == SSH_AUTH_AGAIN)
728 return SSH_AGAIN;
729 else if(rc == SSH_AUTH_SUCCESS)
730 rc = SSH_OK;
731 else if(rc == SSH_AUTH_INFO) {
732 nprompts = ssh_userauth_kbdint_getnprompts(sshc->ssh_session);
733 if(nprompts)
734 return SSH_ERROR;
736 sshc->kbd_state = 2;
737 goto restart;
738 }
739 else
740 rc = SSH_ERROR;
741 break;
742 case 2:
743 sshc->kbd_state = 2;
745 rc = ssh_userauth_kbdint(sshc->ssh_session, NULL, NULL);
746 if(rc == SSH_AUTH_AGAIN)
747 return SSH_AGAIN;
748 else if(rc == SSH_AUTH_SUCCESS)
749 rc = SSH_OK;
750 else
751 rc = SSH_ERROR;
753 break;
754 default:
755 return SSH_ERROR;
756 }
758 sshc->kbd_state = 0;
759 return rc;
762static void myssh_state_init(struct Curl_easy *data,
763 struct ssh_conn *sshc)
765 sshc->secondCreateDirs = 0;
766 sshc->nextstate = SSH_NO_STATE;
767 sshc->actualcode = CURLE_OK;
769#if 0
770 ssh_set_log_level(SSH_LOG_PROTOCOL);
771#endif
773 /* Set libssh to non-blocking, since everything internally is
774 non-blocking */
775 ssh_set_blocking(sshc->ssh_session, 0);
777 myssh_to(data, sshc, SSH_S_STARTUP);
780static void myssh_block2waitfor(struct connectdata *conn,
781 struct ssh_conn *sshc,
782 bool block)
784 (void)conn;
785 if(block) {
786 int dir = ssh_get_poll_flags(sshc->ssh_session);
787 /* translate the libssh define bits into our own bit defines */
788 sshc->waitfor =
789 ((dir & SSH_READ_PENDING) ? REQ_IO_RECV : 0) |
790 ((dir & SSH_WRITE_PENDING) ? REQ_IO_SEND : 0);
791 }
792 else
793 sshc->waitfor = 0;
796static int myssh_in_S_STARTUP(struct Curl_easy *data,
797 struct ssh_conn *sshc)
799 struct connectdata *conn = data->conn;
800 int rc = ssh_connect(sshc->ssh_session);
802 myssh_block2waitfor(conn, sshc, (rc == SSH_AGAIN));
803 if(rc == SSH_AGAIN) {
804 CURL_TRC_SSH(data, "connect -> EAGAIN");
805 }
806 else if(rc != SSH_OK) {
807 failf(data, "Failure establishing ssh session");
808 rc = myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
809 }
810 else
811 myssh_to(data, sshc, SSH_HOSTKEY);
813 return rc;
816static int myssh_in_AUTHLIST(struct Curl_easy *data,
817 struct ssh_conn *sshc)
819 int rc;
820 sshc->authed = FALSE;
822 rc = ssh_userauth_none(sshc->ssh_session, NULL);
823 if(rc == SSH_AUTH_AGAIN)
824 return SSH_AGAIN;
826 if(rc == SSH_AUTH_SUCCESS) {
827 sshc->authed = TRUE;
828 infof(data, "Authenticated with none");
829 myssh_to(data, sshc, SSH_AUTH_DONE);
830 return rc;
831 }
832 else if(rc == SSH_AUTH_ERROR) {
833 rc = myssh_to_ERROR(data, sshc, CURLE_LOGIN_DENIED);
834 return rc;
835 }
837 sshc->auth_methods =
838 (unsigned int)ssh_userauth_list(sshc->ssh_session, NULL);
839 if(sshc->auth_methods)
840 infof(data, "SSH authentication methods available: %s%s%s%s",
841 sshc->auth_methods & SSH_AUTH_METHOD_PUBLICKEY ?
842 "public key, " : "",
843 sshc->auth_methods & SSH_AUTH_METHOD_GSSAPI_MIC ?
844 "GSSAPI, " : "",
845 sshc->auth_methods & SSH_AUTH_METHOD_INTERACTIVE ?
846 "keyboard-interactive, " : "",
847 sshc->auth_methods & SSH_AUTH_METHOD_PASSWORD ?
848 "password" : "");
849 /* For public key auth we need either the private key or
850 CURLSSH_AUTH_AGENT. */
851 if((sshc->auth_methods & SSH_AUTH_METHOD_PUBLICKEY) &&
852 ((sshc->priv_key && sshc->priv_key[0]) ||
853 (data->set.ssh_auth_types & CURLSSH_AUTH_AGENT))) {
854 myssh_to(data, sshc, SSH_AUTH_PKEY_INIT);
855 infof(data, "Authentication using SSH public key file");
856 }
857 else if(sshc->auth_methods & SSH_AUTH_METHOD_GSSAPI_MIC) {
858 myssh_to(data, sshc, SSH_AUTH_GSSAPI);
859 }
860 else if(sshc->auth_methods & SSH_AUTH_METHOD_INTERACTIVE) {
861 myssh_to(data, sshc, SSH_AUTH_KEY_INIT);
862 }
863 else if(sshc->auth_methods & SSH_AUTH_METHOD_PASSWORD) {
864 myssh_to(data, sshc, SSH_AUTH_PASS_INIT);
865 }
866 else { /* unsupported authentication method */
867 rc = myssh_to_ERROR(data, sshc, CURLE_LOGIN_DENIED);
868 }
869 return rc;
872static int myssh_in_AUTH_PKEY_INIT(struct Curl_easy *data,
873 struct ssh_conn *sshc)
875 int rc;
876 if(!(data->set.ssh_auth_types & CURLSSH_AUTH_PUBLICKEY)) {
877 rc = myssh_to_GSSAPI_AUTH(data, sshc);
878 return rc;
879 }
881 /* Two choices, (1) a private key file, named by the user or the default
882 * guess from Curl_ssh_setup_pkey(), (2) libssh's own search of ssh-agent
883 * and the default key files. */
884 if(sshc->priv_key && sshc->priv_key[0]) {
885 if(sshc->pubkey && !sshc->passphrase) {
886 rc = ssh_userauth_try_publickey(sshc->ssh_session, NULL, sshc->pubkey);
887 if(rc == SSH_AUTH_AGAIN)
888 return SSH_AGAIN;
890 if(rc != SSH_OK) {
891 rc = myssh_to_PKEY_AUTO(data, sshc);
892 return rc;
893 }
894 }
896 rc = ssh_pki_import_privkey_file(sshc->priv_key,
897 sshc->passphrase, NULL,
898 NULL, &sshc->privkey);
899 if(rc != SSH_OK) {
900 if(CURL_EASY_STR(data, STRING_SSH_PRIVATE_KEY)) {
901 failf(data, "Could not load private key file %s", sshc->priv_key);
902 rc = myssh_to_ERROR(data, sshc, CURLE_LOGIN_DENIED);
903 return rc;
904 }
905 infof(data, "Could not load private key file %s", sshc->priv_key);
906 rc = myssh_to_PKEY_AUTO(data, sshc);
907 return rc;
908 }
910 myssh_to(data, sshc, SSH_AUTH_PKEY);
911 }
912 else
913 myssh_to(data, sshc, SSH_AUTH_PKEY_AUTO);
914 return SSH_OK;
917static int myssh_in_AUTH_PKEY(struct Curl_easy *data,
918 struct ssh_conn *sshc)
920 int rc = ssh_userauth_publickey(sshc->ssh_session, NULL, sshc->privkey);
921 if(rc == SSH_AUTH_AGAIN)
922 return SSH_AGAIN;
923 else if(rc == SSH_AUTH_SUCCESS) {
924 sshc->authed = TRUE;
925 infof(data, "Completed public key authentication");
926 myssh_to(data, sshc, SSH_AUTH_DONE);
927 return SSH_OK;
928 }
929 else {
930 infof(data, "Failed public key authentication (rc: %d)", rc);
931 return myssh_to_PKEY_AUTO(data, sshc);
932 }
935static int myssh_in_AUTH_PKEY_AUTO(struct Curl_easy *data,
936 struct ssh_conn *sshc)
938 int rc = ssh_userauth_publickey_auto(sshc->ssh_session, NULL,
939 sshc->passphrase);
940 if(rc == SSH_AUTH_AGAIN)
941 return SSH_AGAIN;
942 else if(rc == SSH_AUTH_SUCCESS) {
943 sshc->authed = TRUE;
944 infof(data, "Completed public key authentication");
945 myssh_to(data, sshc, SSH_AUTH_DONE);
946 return SSH_OK;
947 }
948 return myssh_to_GSSAPI_AUTH(data, sshc);
951static int myssh_in_AUTH_GSSAPI(struct Curl_easy *data,
952 struct ssh_conn *sshc)
954 int rc;
955 if(!(data->set.ssh_auth_types & CURLSSH_AUTH_GSSAPI))
956 return myssh_to_KEY_AUTH(data, sshc);
958 rc = ssh_userauth_gssapi(sshc->ssh_session);
959 if(rc == SSH_AUTH_AGAIN)
960 return SSH_AGAIN;
962 if(rc == SSH_AUTH_SUCCESS) {
963 sshc->authed = TRUE;
964 infof(data, "Completed gssapi authentication");
965 myssh_to(data, sshc, SSH_AUTH_DONE);
966 return SSH_OK;
967 }
969 return myssh_to_KEY_AUTH(data, sshc);
972static int myssh_in_AUTH_KEY_INIT(struct Curl_easy *data,
973 struct ssh_conn *sshc)
975 if(data->set.ssh_auth_types & CURLSSH_AUTH_KEYBOARD) {
976 myssh_to(data, sshc, SSH_AUTH_KEY);
977 return SSH_NO_ERROR;
978 }
979 return myssh_to_PASSWD_AUTH(data, sshc);
982static int myssh_in_AUTH_KEY(struct Curl_easy *data,
983 struct ssh_conn *sshc)
985 /* keyboard-interactive authentication */
986 int rc = myssh_auth_interactive(data->conn, sshc);
987 if(rc == SSH_AGAIN)
988 return rc;
989 else if(rc == SSH_OK) {
990 sshc->authed = TRUE;
991 infof(data, "completed keyboard interactive authentication");
992 myssh_to(data, sshc, SSH_AUTH_DONE);
993 return SSH_NO_ERROR;
994 }
995 else
996 return myssh_to_PASSWD_AUTH(data, sshc);
999static int myssh_in_AUTH_PASS_INIT(struct Curl_easy *data,
1000 struct ssh_conn *sshc)
1002 if(!(data->set.ssh_auth_types & CURLSSH_AUTH_PASSWORD))
1003 return myssh_to_ERROR(data, sshc, CURLE_LOGIN_DENIED);
1004 myssh_to(data, sshc, SSH_AUTH_PASS);
1005 return SSH_NO_ERROR;
1008static int myssh_in_AUTH_PASS(struct Curl_easy *data,
1009 struct ssh_conn *sshc)
1011 int rc = ssh_userauth_password(sshc->ssh_session, NULL,
1012 Curl_creds_passwd(data->conn->creds));
1013 if(rc == SSH_AUTH_AGAIN)
1014 return SSH_AGAIN;
1015 else if(rc == SSH_AUTH_SUCCESS) {
1016 sshc->authed = TRUE;
1017 infof(data, "Completed password authentication");
1018 myssh_to(data, sshc, SSH_AUTH_DONE);
1019 return SSH_NO_ERROR;
1021 return myssh_to_ERROR(data, sshc, CURLE_LOGIN_DENIED);
1024static int myssh_in_AUTH_DONE(struct Curl_easy *data,
1025 struct ssh_conn *sshc)
1027 struct connectdata *conn = data->conn;
1028 if(!sshc->authed) {
1029 failf(data, "Authentication failure");
1030 return myssh_to_ERROR(data, sshc, CURLE_LOGIN_DENIED);
1033 /* At this point we have an authenticated ssh session. */
1034 infof(data, "Authentication complete");
1035 Curl_pgrsTime(data, TIMER_APPCONNECT); /* SSH is connected */
1036 conn->recv_idx = FIRSTSOCKET;
1037 conn->send_idx = -1;
1039 if(conn->scheme->protocol == CURLPROTO_SFTP) {
1040 myssh_to(data, sshc, SSH_SFTP_INIT);
1041 return SSH_NO_ERROR;
1043 infof(data, "SSH CONNECT phase done");
1044 myssh_to(data, sshc, SSH_STOP);
1045 return SSH_NO_ERROR;
1048static int myssh_in_UPLOAD_INIT(struct Curl_easy *data,
1049 struct ssh_conn *sshc,
1050 struct SSHPROTO *sshp)
1052 int flags;
1053 int rc = 0;
1055 if(!sshp)
1056 return myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
1058 if(data->state.resume_from) {
1059 sftp_attributes attrs;
1061 if(data->state.resume_from < 0) {
1062 attrs = sftp_stat(sshc->sftp_session, sshp->path);
1063 if(attrs) {
1064 curl_off_t size = attrs->size;
1065 sftp_attributes_free(attrs);
1066 if(size < 0) {
1067 failf(data, "Bad file size (%" FMT_OFF_T ")", size);
1068 rc = myssh_to_ERROR(data, sshc, CURLE_BAD_DOWNLOAD_RESUME);
1069 return rc;
1071 data->state.resume_from = size;
1073 else {
1074 data->state.resume_from = 0;
1079 if(data->set.remote_append) {
1080 /* True append mode: create if nonexisting */
1081 flags = O_WRONLY | O_CREAT | O_APPEND;
1083 else if(data->state.resume_from > 0) {
1084 /*
1085 * Resume MUST NOT use O_APPEND. Many SFTP servers/impls force all
1086 * writes to EOF when O_APPEND is set, ignoring a prior seek().
1087 * Open write-only and seek to the resume offset instead.
1088 */
1089 flags = O_WRONLY;
1091 else
1092 /* Clear file before writing (normal behavior) */
1093 flags = O_WRONLY | O_CREAT | O_TRUNC;
1095 if(sshc->sftp_file)
1096 sftp_close(sshc->sftp_file);
1097 sshc->sftp_file =
1098 sftp_open(sshc->sftp_session, sshp->path,
1099 flags, (mode_t)data->set.new_file_perms);
1100 if(!sshc->sftp_file) {
1101 int err = sftp_get_error(sshc->sftp_session);
1103 if((err == SSH_FX_NO_SUCH_FILE ||
1104 err == SSH_FX_FAILURE ||
1105 err == SSH_FX_NO_SUCH_PATH) &&
1106 data->set.ftp_create_missing_dirs &&
1107 (strlen(sshp->path) > 1)) {
1108 /* try to create the path remotely */
1109 rc = 0;
1110 sshc->secondCreateDirs = 1;
1111 myssh_to(data, sshc, SSH_SFTP_CREATE_DIRS_INIT);
1112 return rc;
1114 else {
1115 rc = myssh_to_SFTP_CLOSE(data, sshc);
1116 return rc;
1120 /* If we have a restart point then we need to seek to the correct
1121 position. Skip if in explicit remote append mode. */
1122 if(data->state.resume_from > 0 && !data->set.remote_append) {
1123 int seekerr = CURL_SEEKFUNC_OK;
1124 /* Let's read off the proper amount of bytes from the input. */
1125 if(data->set.seek_func) {
1126 struct Curl_mapi_guard guard;
1127 CURL_CBAPI_START(&guard, data, easy_seek_func);
1128 seekerr = data->set.seek_func(data->set.seek_client,
1129 data->state.resume_from, SEEK_SET);
1130 CURL_CBAPI_END(&guard);
1133 if(seekerr != CURL_SEEKFUNC_OK) {
1134 curl_off_t passed = 0;
1136 if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
1137 failf(data, "Could not seek stream");
1138 rc = myssh_to_ERROR(data, sshc, CURLE_FTP_COULDNT_USE_REST);
1139 return rc;
1141 /* seekerr == CURL_SEEKFUNC_CANTSEEK (cannot seek to offset) */
1142 do {
1143 char scratch[4 * 1024];
1144 size_t readthisamountnow =
1145 (data->state.resume_from - passed >
1146 (curl_off_t)sizeof(scratch)) ?
1147 sizeof(scratch) : curlx_sotouz(data->state.resume_from - passed);
1149 size_t actuallyread =
1150 data->state.fread_func(scratch, 1,
1151 readthisamountnow, data->state.in);
1153 passed += actuallyread;
1154 if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
1155 /* this checks for greater-than only to make sure that the
1156 CURL_READFUNC_ABORT return code still aborts */
1157 failf(data, "Failed to read data");
1158 rc = myssh_to_ERROR(data, sshc, CURLE_FTP_COULDNT_USE_REST);
1159 return rc;
1161 } while(passed < data->state.resume_from);
1164 /* now, decrease the size of the read */
1165 if(data->state.infilesize > 0) {
1166 if(data->state.resume_from > data->state.infilesize) {
1167 failf(data, "Resume point beyond size");
1168 return myssh_to_ERROR(data, sshc, CURLE_BAD_FUNCTION_ARGUMENT);
1170 data->state.infilesize -= data->state.resume_from;
1171 data->req.size = data->state.infilesize;
1172 Curl_pgrsSetUploadSize(data, data->state.infilesize);
1175 rc = sftp_seek64(sshc->sftp_file, data->state.resume_from);
1176 if(rc) {
1177 rc = myssh_to_SFTP_CLOSE(data, sshc);
1178 return rc;
1181 if(data->state.infilesize > 0) {
1182 data->req.size = data->state.infilesize;
1183 Curl_pgrsSetUploadSize(data, data->state.infilesize);
1185 /* upload data */
1186 Curl_xfer_setup_send(data, FIRSTSOCKET);
1188 /* not set by Curl_xfer_setup to preserve io_flags */
1189 data->conn->recv_idx = FIRSTSOCKET;
1191 /* since we do not really wait for anything at this point, we want the
1192 state machine to move on as soon as possible so we mark this as dirty */
1193 Curl_multi_mark_dirty(data);
1194#if LIBSSH_VERSION_INT > SSH_VERSION_INT(0, 11, 0)
1195 sshc->sftp_send_state = 0;
1196#endif
1197 myssh_to(data, sshc, SSH_STOP);
1198 return rc;
1201static int myssh_in_SFTP_DOWNLOAD_INIT(struct Curl_easy *data,
1202 struct ssh_conn *sshc,
1203 struct SSHPROTO *sshp)
1205 if(!sshp)
1206 return myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
1207 /* Work on getting the specified file */
1208 if(sshc->sftp_file)
1209 sftp_close(sshc->sftp_file);
1211 sshc->sftp_file = sftp_open(sshc->sftp_session, sshp->path,
1212 O_RDONLY, (mode_t)data->set.new_file_perms);
1213 if(!sshc->sftp_file) {
1214 failf(data, "Could not open remote file for reading: %s",
1215 ssh_get_error(sshc->ssh_session));
1217 return myssh_to_SFTP_CLOSE(data, sshc);
1219 sftp_file_set_nonblocking(sshc->sftp_file);
1220 myssh_to(data, sshc, SSH_SFTP_DOWNLOAD_STAT);
1221 return SSH_NO_ERROR;
1224static int myssh_in_SFTP_DOWNLOAD_STAT(struct Curl_easy *data,
1225 struct ssh_conn *sshc)
1227 curl_off_t size;
1228 int rc = 0;
1229 sftp_attributes attrs = sftp_fstat(sshc->sftp_file);
1230 if(!attrs ||
1231 !(attrs->flags & SSH_FILEXFER_ATTR_SIZE) ||
1232 (attrs->size == 0)) {
1233 /*
1234 * sftp_fstat did not return an error, so maybe the server
1235 * does not support stat()
1236 * OR the server does not return a file size with a stat()
1237 * OR file size is 0
1238 */
1239 data->req.size = -1;
1240 data->req.maxdownload = -1;
1241 Curl_pgrsSetDownloadSize(data, -1);
1242 size = 0;
1243 if(attrs)
1244 sftp_attributes_free(attrs);
1246 else {
1247 size = attrs->size;
1249 sftp_attributes_free(attrs);
1251 if(size < 0) {
1252 failf(data, "Bad file size (%" FMT_OFF_T ")", size);
1253 return myssh_to_ERROR(data, sshc, CURLE_BAD_DOWNLOAD_RESUME);
1255 if(data->state.use_range) {
1256 curl_off_t from;
1257 CURLcode result = Curl_ssh_range(data, data->state.range, size,
1258 &from, &size);
1259 if(result)
1260 return myssh_to_ERROR(data, sshc, result);
1262 rc = sftp_seek64(sshc->sftp_file, from);
1263 if(rc)
1264 return myssh_to_SFTP_CLOSE(data, sshc);
1266 data->req.size = size;
1267 data->req.maxdownload = size;
1268 Curl_pgrsSetDownloadSize(data, size);
1271 /* We can resume if we can seek to the resume position */
1272 if(data->state.resume_from) {
1273 if(data->state.resume_from < 0) {
1274 /* We are supposed to download the last abs(from) bytes */
1275 if(size < -data->state.resume_from) {
1276 failf(data, "Offset (%" FMT_OFF_T ") was beyond file size (%"
1277 FMT_OFF_T ")", data->state.resume_from, size);
1278 return myssh_to_ERROR(data, sshc, CURLE_BAD_DOWNLOAD_RESUME);
1280 /* download from where? */
1281 data->state.resume_from += size;
1283 else {
1284 if(size < data->state.resume_from) {
1285 failf(data, "Offset (%" FMT_OFF_T
1286 ") was beyond file size (%" FMT_OFF_T ")",
1287 data->state.resume_from, size);
1288 return myssh_to_ERROR(data, sshc, CURLE_BAD_DOWNLOAD_RESUME);
1291 /* Now store the number of bytes we are expected to download */
1292 data->req.size = size - data->state.resume_from;
1293 data->req.maxdownload = size - data->state.resume_from;
1294 Curl_pgrsSetDownloadSize(data, size - data->state.resume_from);
1296 rc = sftp_seek64(sshc->sftp_file, data->state.resume_from);
1297 if(rc)
1298 return myssh_to_SFTP_CLOSE(data, sshc);
1301 /* Setup the actual download */
1302 if(data->req.size == 0) {
1303 /* no data to transfer */
1304 Curl_xfer_setup_nop(data);
1305 infof(data, "File already completely downloaded");
1306 myssh_to(data, sshc, SSH_STOP);
1307 return rc;
1309 Curl_xfer_setup_recv(data, FIRSTSOCKET, data->req.size);
1311 /* not set by Curl_xfer_setup to preserve io_flags */
1312 data->conn->send_idx = 0;
1314 sshc->sftp_recv_state = 0;
1315 myssh_to(data, sshc, SSH_STOP);
1317 return rc;
1320static int myssh_in_SFTP_CLOSE(struct Curl_easy *data,
1321 struct ssh_conn *sshc,
1322 struct SSHPROTO *sshp)
1324 if(!sshp)
1325 return myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
1326 if(sshc->sftp_file) {
1327 sftp_close(sshc->sftp_file);
1328 sshc->sftp_file = NULL;
1330 curlx_safefree(sshp->path);
1332 CURL_TRC_SSH(data, "SFTP DONE done");
1334 /* Check if nextstate is set and move .nextstate could be POSTQUOTE_INIT
1335 After nextstate is executed, the control should come back to
1336 SSH_SFTP_CLOSE to pass the correct result back */
1337 if(sshc->nextstate != SSH_NO_STATE &&
1338 sshc->nextstate != SSH_SFTP_CLOSE) {
1339 myssh_to(data, sshc, sshc->nextstate);
1340 sshc->nextstate = SSH_SFTP_CLOSE;
1342 else {
1343 myssh_to(data, sshc, SSH_STOP);
1345 return SSH_NO_ERROR;
1348static int myssh_in_SFTP_SHUTDOWN(struct Curl_easy *data,
1349 struct ssh_conn *sshc)
1351 /* during times we get here due to a broken transfer and then the
1352 sftp_handle might not have been taken down so make sure that is done
1353 before we proceed */
1354 ssh_set_blocking(sshc->ssh_session, 0);
1355#if LIBSSH_VERSION_INT > SSH_VERSION_INT(0, 11, 0)
1356 SFTP_AIO_FREE(sshc->sftp_send_aio);
1357 SFTP_AIO_FREE(sshc->sftp_recv_aio);
1358#endif
1360 if(sshc->sftp_file) {
1361 sftp_close(sshc->sftp_file);
1362 sshc->sftp_file = NULL;
1365 if(sshc->sftp_session) {
1366 sftp_free(sshc->sftp_session);
1367 sshc->sftp_session = NULL;
1370 SSH_STRING_FREE_CHAR(sshc->homedir);
1372 myssh_to(data, sshc, SSH_SESSION_DISCONNECT);
1373 return SSH_NO_ERROR;
1376static int myssh_in_SFTP_INIT(struct Curl_easy *data,
1377 struct ssh_conn *sshc)
1379 int rc;
1380 ssh_set_blocking(sshc->ssh_session, 1);
1382 sshc->sftp_session = sftp_new(sshc->ssh_session);
1383 if(!sshc->sftp_session) {
1384 failf(data, "Failure initializing sftp session: %s",
1385 ssh_get_error(sshc->ssh_session));
1386 return myssh_to_ERROR(data, sshc, CURLE_COULDNT_CONNECT);
1389 rc = sftp_init(sshc->sftp_session);
1390 if(rc != SSH_OK) {
1391 failf(data, "Failure initializing sftp session: %s",
1392 ssh_get_error(sshc->ssh_session));
1393 return myssh_to_ERROR(data, sshc, sftp_error_to_CURLE(SSH_FX_FAILURE));
1395 myssh_to(data, sshc, SSH_SFTP_REALPATH);
1396 return SSH_NO_ERROR;
1399static int myssh_in_SFTP_REALPATH(struct Curl_easy *data,
1400 struct ssh_conn *sshc)
1402 /* Get the "home" directory */
1403 sshc->homedir = sftp_canonicalize_path(sshc->sftp_session, ".");
1404 if(!sshc->homedir)
1405 return myssh_to_ERROR(data, sshc, CURLE_COULDNT_CONNECT);
1407 curlx_free(data->state.most_recent_ftp_entrypath);
1408 data->state.most_recent_ftp_entrypath = curlx_strdup(sshc->homedir);
1409 if(!data->state.most_recent_ftp_entrypath)
1410 return myssh_to_ERROR(data, sshc, CURLE_OUT_OF_MEMORY);
1412 /* This is the last step in the SFTP connect phase. Do note that while
1413 we get the homedir here, we get the "workingpath" in the DO action
1414 since the homedir remains the same between request but the
1415 working path does not. */
1416 CURL_TRC_SSH(data, "CONNECT phase done");
1417 myssh_to(data, sshc, SSH_STOP);
1418 return SSH_NO_ERROR;
1421static int myssh_in_SFTP_QUOTE_INIT(struct Curl_easy *data,
1422 struct ssh_conn *sshc,
1423 struct SSHPROTO *sshp)
1425 CURLcode result;
1427 if(!sshp)
1428 return myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
1430 result = Curl_getworkingpath(data, sshc->homedir, &sshp->path);
1431 if(result) {
1432 sshc->actualcode = result;
1433 myssh_to(data, sshc, SSH_STOP);
1435 else if(data->set.quote) {
1436 infof(data, "Sending quote commands");
1437 sshc->quote_item = data->set.quote;
1438 myssh_to(data, sshc, SSH_SFTP_QUOTE);
1440 else
1441 myssh_to(data, sshc, SSH_SFTP_GETINFO);
1442 return SSH_NO_ERROR;
1445static int myssh_in_SFTP_POSTQUOTE_INIT(struct Curl_easy *data,
1446 struct ssh_conn *sshc)
1448 if(data->set.postquote) {
1449 infof(data, "Sending quote commands");
1450 sshc->quote_item = data->set.postquote;
1451 myssh_to(data, sshc, SSH_SFTP_QUOTE);
1453 else {
1454 myssh_to(data, sshc, SSH_STOP);
1456 return SSH_NO_ERROR;
1459static int quote_error(struct Curl_easy *data,
1460 struct ssh_conn *sshc)
1462 failf(data, "Suspicious data after the command line");
1463 curlx_safefree(sshc->quote_path1);
1464 curlx_safefree(sshc->quote_path2);
1465 myssh_to(data, sshc, SSH_SFTP_CLOSE);
1466 sshc->nextstate = SSH_NO_STATE;
1467 sshc->actualcode = CURLE_QUOTE_ERROR;
1468 return SSH_NO_ERROR;
1471static int myssh_in_SFTP_QUOTE(struct Curl_easy *data,
1472 struct ssh_conn *sshc,
1473 struct SSHPROTO *sshp)
1475 const char *cp;
1476 CURLcode result;
1478 /*
1479 * Support some of the "FTP" commands
1480 */
1481 const char *cmd = sshc->quote_item->data;
1482 sshc->acceptfail = FALSE;
1484 if(!sshp)
1485 return myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
1487 /* if a command starts with an asterisk, which a legal SFTP command never
1488 can, the command is allowed to fail without it causing any
1489 aborts or cancels etc. It causes libcurl to act as if the command
1490 is successful, whatever the server responds. */
1492 if(cmd[0] == '*') {
1493 cmd++;
1494 sshc->acceptfail = TRUE;
1497 if(curl_strequal("pwd", cmd)) {
1498 /* output debug output if that is requested */
1499 char *tmp = curl_maprintf("257 \"%s\" is current directory.\n",
1500 sshp->path);
1501 if(!tmp) {
1502 sshc->actualcode = CURLE_OUT_OF_MEMORY;
1503 myssh_to(data, sshc, SSH_SFTP_CLOSE);
1504 sshc->nextstate = SSH_NO_STATE;
1505 return SSH_NO_ERROR;
1507 Curl_debug(data, CURLINFO_HEADER_OUT, "PWD\n", 4);
1508 Curl_debug(data, CURLINFO_HEADER_IN, tmp, strlen(tmp));
1510 /* this sends an FTP-like "header" to the header callback so that
1511 the current directory can be read similar to how it is read when
1512 using ordinary FTP. */
1513 result = Curl_client_write(data, CLIENTWRITE_HEADER, tmp, strlen(tmp));
1514 curlx_free(tmp);
1515 if(result) {
1516 myssh_to(data, sshc, SSH_SFTP_CLOSE);
1517 sshc->nextstate = SSH_NO_STATE;
1518 sshc->actualcode = result;
1520 else
1521 myssh_to(data, sshc, SSH_SFTP_NEXT_QUOTE);
1522 return SSH_NO_ERROR;
1525 /*
1526 * the arguments following the command must be separated from the
1527 * command with a space so we can check for it unconditionally
1528 */
1529 cp = strchr(cmd, ' ');
1530 if(!cp) {
1531 failf(data, "Syntax error in SFTP command. Supply parameter(s)");
1532 myssh_to(data, sshc, SSH_SFTP_CLOSE);
1533 sshc->nextstate = SSH_NO_STATE;
1534 sshc->actualcode = CURLE_QUOTE_ERROR;
1535 return SSH_NO_ERROR;
1538 /*
1539 * also, every command takes at least one argument so we get that
1540 * first argument right now
1541 */
1542 result = Curl_get_pathname(&cp, &sshc->quote_path1, sshc->homedir);
1543 if(result) {
1544 if(result == CURLE_OUT_OF_MEMORY)
1545 failf(data, "Out of memory");
1546 else
1547 failf(data, "Syntax error: Bad first parameter");
1548 myssh_to(data, sshc, SSH_SFTP_CLOSE);
1549 sshc->nextstate = SSH_NO_STATE;
1550 sshc->actualcode = result;
1551 return SSH_NO_ERROR;
1554 /*
1555 * SFTP is a binary protocol, so we do not send text commands
1556 * to the server. Instead, we scan for commands used by
1557 * OpenSSH's sftp program and call the appropriate libssh
1558 * functions.
1559 */
1560 if(!strncmp(cmd, "chgrp ", 6) ||
1561 !strncmp(cmd, "chmod ", 6) ||
1562 !strncmp(cmd, "chown ", 6) ||
1563 !strncmp(cmd, "atime ", 6) ||
1564 !strncmp(cmd, "mtime ", 6)) {
1565 /* attribute change */
1567 /* sshc->quote_path1 contains the mode to set */
1568 /* get the destination */
1569 result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
1570 if(result) {
1571 if(result == CURLE_OUT_OF_MEMORY)
1572 failf(data, "Out of memory");
1573 else
1574 failf(data, "Syntax error in chgrp/chmod/chown/atime/mtime: "
1575 "Bad second parameter");
1576 curlx_safefree(sshc->quote_path1);
1577 myssh_to(data, sshc, SSH_SFTP_CLOSE);
1578 sshc->nextstate = SSH_NO_STATE;
1579 sshc->actualcode = result;
1580 return SSH_NO_ERROR;
1582 if(*cp)
1583 return quote_error(data, sshc);
1584 sshc->quote_attrs = NULL;
1585 myssh_to(data, sshc, SSH_SFTP_QUOTE_STAT);
1586 return SSH_NO_ERROR;
1588 if(!strncmp(cmd, "ln ", 3) ||
1589 !strncmp(cmd, "symlink ", 8)) {
1590 /* symbolic linking */
1591 /* sshc->quote_path1 is the source */
1592 /* get the destination */
1593 result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
1594 if(result) {
1595 if(result == CURLE_OUT_OF_MEMORY)
1596 failf(data, "Out of memory");
1597 else
1598 failf(data, "Syntax error in ln/symlink: Bad second parameter");
1599 curlx_safefree(sshc->quote_path1);
1600 myssh_to(data, sshc, SSH_SFTP_CLOSE);
1601 sshc->nextstate = SSH_NO_STATE;
1602 sshc->actualcode = result;
1603 return SSH_NO_ERROR;
1605 if(*cp)
1606 return quote_error(data, sshc);
1607 myssh_to(data, sshc, SSH_SFTP_QUOTE_SYMLINK);
1608 return SSH_NO_ERROR;
1610 else if(!strncmp(cmd, "mkdir ", 6)) {
1611 if(*cp)
1612 return quote_error(data, sshc);
1613 /* create directory */
1614 myssh_to(data, sshc, SSH_SFTP_QUOTE_MKDIR);
1615 return SSH_NO_ERROR;
1617 else if(!strncmp(cmd, "rename ", 7)) {
1618 /* rename file */
1619 /* first param is the source path */
1620 /* second param is the dest. path */
1621 result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
1622 if(result) {
1623 if(result == CURLE_OUT_OF_MEMORY)
1624 failf(data, "Out of memory");
1625 else
1626 failf(data, "Syntax error in rename: Bad second parameter");
1627 curlx_safefree(sshc->quote_path1);
1628 myssh_to(data, sshc, SSH_SFTP_CLOSE);
1629 sshc->nextstate = SSH_NO_STATE;
1630 sshc->actualcode = result;
1631 return SSH_NO_ERROR;
1633 if(*cp)
1634 return quote_error(data, sshc);
1635 myssh_to(data, sshc, SSH_SFTP_QUOTE_RENAME);
1636 return SSH_NO_ERROR;
1638 else if(!strncmp(cmd, "rmdir ", 6)) {
1639 /* delete directory */
1640 if(*cp)
1641 return quote_error(data, sshc);
1642 myssh_to(data, sshc, SSH_SFTP_QUOTE_RMDIR);
1643 return SSH_NO_ERROR;
1645 else if(!strncmp(cmd, "rm ", 3)) {
1646 if(*cp)
1647 return quote_error(data, sshc);
1648 myssh_to(data, sshc, SSH_SFTP_QUOTE_UNLINK);
1649 return SSH_NO_ERROR;
1651#ifdef HAS_STATVFS_SUPPORT
1652 else if(!strncmp(cmd, "statvfs ", 8)) {
1653 if(*cp)
1654 return quote_error(data, sshc);
1655 myssh_to(data, sshc, SSH_SFTP_QUOTE_STATVFS);
1656 return SSH_NO_ERROR;
1658#endif
1660 failf(data, "Unknown SFTP command");
1661 curlx_safefree(sshc->quote_path1);
1662 curlx_safefree(sshc->quote_path2);
1663 myssh_to(data, sshc, SSH_SFTP_CLOSE);
1664 sshc->nextstate = SSH_NO_STATE;
1665 sshc->actualcode = CURLE_QUOTE_ERROR;
1666 return SSH_NO_ERROR;
1669static int myssh_in_SFTP_NEXT_QUOTE(struct Curl_easy *data,
1670 struct ssh_conn *sshc)
1672 curlx_safefree(sshc->quote_path1);
1673 curlx_safefree(sshc->quote_path2);
1675 sshc->quote_item = sshc->quote_item->next;
1677 if(sshc->quote_item) {
1678 myssh_to(data, sshc, SSH_SFTP_QUOTE);
1680 else {
1681 if(sshc->nextstate != SSH_NO_STATE) {
1682 myssh_to(data, sshc, sshc->nextstate);
1683 sshc->nextstate = SSH_NO_STATE;
1685 else {
1686 myssh_to(data, sshc, SSH_SFTP_GETINFO);
1689 return SSH_NO_ERROR;
1692static int myssh_in_SFTP_QUOTE_STAT(struct Curl_easy *data,
1693 struct ssh_conn *sshc)
1695 const char *cmd = sshc->quote_item->data;
1696 sshc->acceptfail = FALSE;
1698 /* if a command starts with an asterisk, which a legal SFTP command never
1699 can, the command is allowed to fail without it causing any
1700 aborts or cancels etc. It causes libcurl to act as if the command
1701 is successful, whatever the server responds. */
1703 if(cmd[0] == '*') {
1704 cmd++;
1705 sshc->acceptfail = TRUE;
1708 /* We read the file attributes, store them in sshc->quote_attrs
1709 * and modify them accordingly to command. Then we switch to
1710 * QUOTE_SETSTAT state to write new ones.
1711 */
1713 if(sshc->quote_attrs)
1714 sftp_attributes_free(sshc->quote_attrs);
1715 sshc->quote_attrs = sftp_stat(sshc->sftp_session, sshc->quote_path2);
1716 if(!sshc->quote_attrs) {
1717 failf(data, "Attempt to get SFTP stats failed: %d",
1718 sftp_get_error(sshc->sftp_session));
1719 myssh_quote_error(data, sshc, NULL);
1720 return SSH_NO_ERROR;
1723 /* Now set the new attributes... */
1724 if(!strncmp(cmd, "chgrp", 5)) {
1725 const char *p = sshc->quote_path1;
1726 curl_off_t gid;
1727 if(curlx_str_number(&p, &gid, UINT_MAX)) {
1728 failf(data, "Syntax error: chgrp gid not a number");
1729 myssh_quote_error(data, sshc, NULL);
1730 return SSH_NO_ERROR;
1732 sshc->quote_attrs->gid = (uint32_t)gid;
1733 sshc->quote_attrs->flags |= SSH_FILEXFER_ATTR_UIDGID;
1735 else if(!strncmp(cmd, "chmod", 5)) {
1736 curl_off_t perms;
1737 const char *p = sshc->quote_path1;
1738 if(curlx_str_octal(&p, &perms, 07777)) {
1739 failf(data, "Syntax error: chmod permissions not a number");
1740 myssh_quote_error(data, sshc, NULL);
1741 return SSH_NO_ERROR;
1743 sshc->quote_attrs->permissions = (mode_t)perms;
1744 sshc->quote_attrs->flags |= SSH_FILEXFER_ATTR_PERMISSIONS;
1746 else if(!strncmp(cmd, "chown", 5)) {
1747 const char *p = sshc->quote_path1;
1748 curl_off_t uid;
1749 if(curlx_str_number(&p, &uid, UINT_MAX)) {
1750 failf(data, "Syntax error: chown uid not a number");
1751 myssh_quote_error(data, sshc, NULL);
1752 return SSH_NO_ERROR;
1754 sshc->quote_attrs->uid = (uint32_t)uid;
1755 sshc->quote_attrs->flags |= SSH_FILEXFER_ATTR_UIDGID;
1757 else if(!strncmp(cmd, "atime", 5) ||
1758 !strncmp(cmd, "mtime", 5)) {
1759 time_t date;
1760 bool fail = FALSE;
1761 if(Curl_getdate_capped(sshc->quote_path1, &date)) {
1762 failf(data, "incorrect date format for %.*s", 5, cmd);
1763 fail = TRUE;
1765#if SIZEOF_TIME_T > 4
1766 else if(date > 0xffffffff) {
1767 failf(data, "date overflow");
1768 fail = TRUE; /* avoid setting a capped time */
1770#endif
1771 if(fail) {
1772 myssh_quote_error(data, sshc, NULL);
1773 return SSH_NO_ERROR;
1775 if(!strncmp(cmd, "atime", 5))
1776 sshc->quote_attrs->atime = (uint32_t)date;
1777 else /* mtime */
1778 sshc->quote_attrs->mtime = (uint32_t)date;
1780 sshc->quote_attrs->flags |= SSH_FILEXFER_ATTR_ACMODTIME;
1783 /* Now send the completed structure... */
1784 myssh_to(data, sshc, SSH_SFTP_QUOTE_SETSTAT);
1785 return SSH_NO_ERROR;
1788static void conn_forget_socket(struct Curl_easy *data, int8_t sockindex)
1790 struct connectdata *conn = data->conn;
1791 if(conn && CONN_SOCK_IDX_VALID(sockindex)) {
1792 struct Curl_cfilter *cf = conn->cfilter[sockindex];
1793 if(cf)
1794 (void)Curl_conn_cf_cntrl(cf, data, TRUE, CF_CTRL_FORGET_SOCKET, 0, NULL);
1795 fake_sclose(conn->sock[sockindex]);
1796 conn->sock[sockindex] = CURL_SOCKET_BAD;
1800static void myssh_SESSION_DISCONNECT(struct Curl_easy *data,
1801 struct ssh_conn *sshc)
1803 /* during weird times when we have been prematurely aborted, the channel
1804 is still alive when we reach this state and we MUST kill the channel
1805 properly first */
1806 if(sshc->scp_session) {
1807 ssh_scp_free(sshc->scp_session);
1808 sshc->scp_session = NULL;
1811 if(sshc->sftp_file) {
1812 sftp_close(sshc->sftp_file);
1813 sshc->sftp_file = NULL;
1815 if(sshc->sftp_session) {
1816 sftp_free(sshc->sftp_session);
1817 sshc->sftp_session = NULL;
1820 ssh_disconnect(sshc->ssh_session);
1821 if(!ssh_version(SSH_VERSION_INT(0, 10, 0))) {
1822 /* conn->sock[FIRSTSOCKET] is closed by ssh_disconnect behind our back,
1823 tell the connection to forget about it. This libssh
1824 bug is fixed in 0.10.0. */
1825 conn_forget_socket(data, FIRSTSOCKET);
1828 SSH_STRING_FREE_CHAR(sshc->homedir);
1830 myssh_to(data, sshc, SSH_SESSION_FREE);
1833static int myssh_SSH_SCP_DOWNLOAD(struct Curl_easy *data,
1834 struct ssh_conn *sshc)
1836 uint64_t bytecount;
1837 int rc = ssh_scp_pull_request(sshc->scp_session);
1838 if(rc != SSH_SCP_REQUEST_NEWFILE) {
1839 const char *err_msg = ssh_get_error(sshc->ssh_session);
1840 failf(data, "%s", err_msg);
1841 return myssh_to_ERROR(data, sshc, CURLE_REMOTE_FILE_NOT_FOUND);
1844 /* download data */
1845 bytecount = ssh_scp_request_get_size64(sshc->scp_session);
1846 if(bytecount > CURL_OFF_T_MAX)
1847 /* unlikely to be a real file */
1848 return myssh_to_ERROR(data, sshc, CURLE_TOO_LARGE);
1849 data->req.maxdownload = (curl_off_t)bytecount;
1850 Curl_xfer_setup_recv(data, FIRSTSOCKET, data->req.maxdownload);
1852 /* not set by Curl_xfer_setup to preserve io_flags */
1853 data->conn->send_idx = 0;
1855 myssh_to(data, sshc, SSH_STOP);
1856 return 0;
1859static int myssh_in_TRANS_INIT(struct Curl_easy *data, struct ssh_conn *sshc,
1860 struct SSHPROTO *sshp)
1862 CURLcode result;
1863 int rc = 0;
1864 if(!sshp)
1865 return myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
1867 result = Curl_getworkingpath(data, sshc->homedir, &sshp->path);
1868 if(result) {
1869 sshc->actualcode = result;
1870 myssh_to(data, sshc, SSH_STOP);
1871 return 0;
1874 /* Functions from the SCP subsystem cannot handle/return SSH_AGAIN */
1875 ssh_set_blocking(sshc->ssh_session, 1);
1877 if(data->state.upload) {
1878 if(data->state.infilesize < 0) {
1879 failf(data, "SCP requires a known file size for upload");
1880 return myssh_to_ERROR(data, sshc, CURLE_UPLOAD_FAILED);
1883 sshc->scp_session =
1884 ssh_scp_new(sshc->ssh_session, SSH_SCP_WRITE, sshp->path);
1885 myssh_to(data, sshc, SSH_SCP_UPLOAD_INIT);
1887 else {
1888 sshc->scp_session =
1889 ssh_scp_new(sshc->ssh_session, SSH_SCP_READ, sshp->path);
1890 myssh_to(data, sshc, SSH_SCP_DOWNLOAD_INIT);
1893 if(!sshc->scp_session) {
1894 const char *err_msg = ssh_get_error(sshc->ssh_session);
1895 failf(data, "%s", err_msg);
1896 rc = myssh_to_ERROR(data, sshc, CURLE_UPLOAD_FAILED);
1898 return rc;
1901static void sshc_cleanup(struct ssh_conn *sshc)
1903 if(sshc->initialized) {
1904 if(sshc->sftp_file) {
1905 sftp_close(sshc->sftp_file);
1906 sshc->sftp_file = NULL;
1908 if(sshc->sftp_session) {
1909 sftp_free(sshc->sftp_session);
1910 sshc->sftp_session = NULL;
1912 if(sshc->ssh_session) {
1913 ssh_free(sshc->ssh_session);
1914 sshc->ssh_session = NULL;
1917 /* worst-case scenario cleanup */
1918 DEBUGASSERT(!sshc->ssh_session);
1919 DEBUGASSERT(!sshc->scp_session);
1921 if(sshc->readdir_tmp) {
1922 ssh_string_free_char(sshc->readdir_tmp);
1923 sshc->readdir_tmp = NULL;
1925 if(sshc->quote_attrs) {
1926 sftp_attributes_free(sshc->quote_attrs);
1927 sshc->quote_attrs = NULL;
1929 if(sshc->readdir_attrs) {
1930 sftp_attributes_free(sshc->readdir_attrs);
1931 sshc->readdir_attrs = NULL;
1933 if(sshc->readdir_link_attrs) {
1934 sftp_attributes_free(sshc->readdir_link_attrs);
1935 sshc->readdir_link_attrs = NULL;
1937 if(sshc->privkey) {
1938 ssh_key_free(sshc->privkey);
1939 sshc->privkey = NULL;
1941 if(sshc->pubkey) {
1942 ssh_key_free(sshc->pubkey);
1943 sshc->pubkey = NULL;
1946 curlx_safefree(sshc->passphrase);
1947 curlx_safefree(sshc->pub_key);
1948 curlx_safefree(sshc->priv_key);
1949 curlx_safefree(sshc->quote_path1);
1950 curlx_safefree(sshc->quote_path2);
1951 curlx_dyn_free(&sshc->readdir_buf);
1952 curlx_safefree(sshc->readdir_linkPath);
1953 SSH_STRING_FREE_CHAR(sshc->homedir);
1954 sshc->initialized = FALSE;
1958static int myssh_in_SFTP_QUOTE_SETSTAT(struct Curl_easy *data,
1959 struct ssh_conn *sshc)
1961 int rc = sftp_setstat(sshc->sftp_session, sshc->quote_path2,
1962 sshc->quote_attrs);
1963 if(rc == SSH_AGAIN)
1964 return rc;
1965 if(rc && !sshc->acceptfail) {
1966 myssh_quote_error(data, sshc, "setstat");
1967 return rc;
1969 myssh_to(data, sshc, SSH_SFTP_NEXT_QUOTE);
1970 return SSH_NO_ERROR;
1973static int myssh_in_SFTP_QUOTE_SYMLINK(struct Curl_easy *data,
1974 struct ssh_conn *sshc)
1976 int rc = sftp_symlink(sshc->sftp_session, sshc->quote_path2,
1977 sshc->quote_path1);
1978 if(rc == SSH_AGAIN)
1979 return rc;
1980 if(rc && !sshc->acceptfail) {
1981 myssh_quote_error(data, sshc, "symlink");
1982 return rc;
1984 myssh_to(data, sshc, SSH_SFTP_NEXT_QUOTE);
1985 return SSH_NO_ERROR;
1988static int myssh_in_SFTP_QUOTE_MKDIR(struct Curl_easy *data,
1989 struct ssh_conn *sshc)
1991 int rc = sftp_mkdir(sshc->sftp_session, sshc->quote_path1,
1992 (mode_t)data->set.new_directory_perms);
1993 if(rc == SSH_AGAIN)
1994 return rc;
1995 if(rc && !sshc->acceptfail) {
1996 myssh_quote_error(data, sshc, "mkdir");
1997 return rc;
1999 myssh_to(data, sshc, SSH_SFTP_NEXT_QUOTE);
2000 return SSH_NO_ERROR;
2003static int myssh_in_SFTP_QUOTE_RENAME(struct Curl_easy *data,
2004 struct ssh_conn *sshc)
2006 int rc = sftp_rename(sshc->sftp_session, sshc->quote_path1,
2007 sshc->quote_path2);
2008 if(rc == SSH_AGAIN)
2009 return rc;
2010 if(rc && !sshc->acceptfail) {
2011 myssh_quote_error(data, sshc, "rename");
2012 return rc;
2014 myssh_to(data, sshc, SSH_SFTP_NEXT_QUOTE);
2015 return SSH_NO_ERROR;
2018static int myssh_in_SFTP_QUOTE_RMDIR(struct Curl_easy *data,
2019 struct ssh_conn *sshc)
2021 int rc = sftp_rmdir(sshc->sftp_session, sshc->quote_path1);
2022 if(rc == SSH_AGAIN)
2023 return rc;
2024 if(rc && !sshc->acceptfail) {
2025 myssh_quote_error(data, sshc, "rmdir");
2026 return rc;
2028 myssh_to(data, sshc, SSH_SFTP_NEXT_QUOTE);
2029 return SSH_NO_ERROR;
2032static int myssh_in_SFTP_QUOTE_UNLINK(struct Curl_easy *data,
2033 struct ssh_conn *sshc)
2035 int rc = sftp_unlink(sshc->sftp_session, sshc->quote_path1);
2036 if(rc == SSH_AGAIN)
2037 return rc;
2038 if(rc && !sshc->acceptfail) {
2039 myssh_quote_error(data, sshc, "rm");
2040 return rc;
2042 myssh_to(data, sshc, SSH_SFTP_NEXT_QUOTE);
2043 return SSH_NO_ERROR;
2046static int myssh_in_SFTP_GETINFO(struct Curl_easy *data,
2047 struct ssh_conn *sshc)
2049 if(data->set.get_filetime)
2050 myssh_to(data, sshc, SSH_SFTP_FILETIME);
2051 else
2052 myssh_to(data, sshc, SSH_SFTP_TRANS_INIT);
2053 return SSH_NO_ERROR;
2056static int myssh_in_SFTP_FILETIME(struct Curl_easy *data,
2057 struct ssh_conn *sshc,
2058 struct SSHPROTO *sshp)
2060 sftp_attributes attrs;
2061 if(!sshp)
2062 return myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
2064 attrs = sftp_stat(sshc->sftp_session, sshp->path);
2065 if(attrs) {
2066 data->info.filetime = attrs->mtime;
2067 sftp_attributes_free(attrs);
2070 myssh_to(data, sshc, SSH_SFTP_TRANS_INIT);
2071 return SSH_NO_ERROR;
2074static int myssh_in_SFTP_TRANS_INIT(struct Curl_easy *data,
2075 struct ssh_conn *sshc,
2076 struct SSHPROTO *sshp)
2078 if(!sshp)
2079 return myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
2080 if(data->state.upload)
2081 myssh_to(data, sshc, SSH_SFTP_UPLOAD_INIT);
2082 else {
2083 size_t path_len = strlen(sshp->path);
2085 if(path_len && sshp->path[path_len - 1] == '/')
2086 myssh_to(data, sshc, SSH_SFTP_READDIR_INIT);
2087 else
2088 myssh_to(data, sshc, SSH_SFTP_DOWNLOAD_INIT);
2091 return SSH_NO_ERROR;
2094static int myssh_in_SFTP_CREATE_DIRS_INIT(struct Curl_easy *data,
2095 struct ssh_conn *sshc,
2096 struct SSHPROTO *sshp)
2098 if(!sshp)
2099 return myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
2101 if(strlen(sshp->path) > 1) {
2102 sshc->slash_pos = sshp->path + 1; /* ignore the leading '/' */
2103 myssh_to(data, sshc, SSH_SFTP_CREATE_DIRS);
2105 else {
2106 myssh_to(data, sshc, SSH_SFTP_UPLOAD_INIT);
2108 return SSH_NO_ERROR;
2111static int myssh_in_SFTP_CREATE_DIRS(struct Curl_easy *data,
2112 struct ssh_conn *sshc,
2113 struct SSHPROTO *sshp)
2115 if(!sshp)
2116 return myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
2118 sshc->slash_pos = strchr(sshc->slash_pos, '/');
2119 if(sshc->slash_pos) {
2120 *sshc->slash_pos = 0;
2122 infof(data, "Creating directory '%s'", sshp->path);
2123 myssh_to(data, sshc, SSH_SFTP_CREATE_DIRS_MKDIR);
2124 return SSH_NO_ERROR;
2126 myssh_to(data, sshc, SSH_SFTP_UPLOAD_INIT);
2127 return SSH_NO_ERROR;
2130static int myssh_in_SFTP_CREATE_DIRS_MKDIR(struct Curl_easy *data,
2131 struct ssh_conn *sshc,
2132 struct SSHPROTO *sshp)
2134 int rc;
2135 int err;
2136 /* 'mode' - parameter is preliminary - default to 0644 */
2137 if(!sshp)
2138 return myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
2140 rc = sftp_mkdir(sshc->sftp_session, sshp->path,
2141 (mode_t)data->set.new_directory_perms);
2142 if(rc == SSH_AGAIN)
2143 return rc;
2144 *sshc->slash_pos = '/';
2145 ++sshc->slash_pos;
2146 if(rc < 0) {
2147 /*
2148 * Abort if failure was not that the directory already
2149 * exists or the permission was denied (creation might
2150 * succeed further down the path) - retry on unspecific
2151 * FAILURE also
2152 */
2153 err = sftp_get_error(sshc->sftp_session);
2154 if((err != SSH_FX_FILE_ALREADY_EXISTS) &&
2155 (err != SSH_FX_FAILURE) &&
2156 (err != SSH_FX_PERMISSION_DENIED)) {
2157 return myssh_to_SFTP_CLOSE(data, sshc);
2159 rc = 0; /* clear rc and continue */
2161 myssh_to(data, sshc, SSH_SFTP_CREATE_DIRS);
2162 return rc;
2165static int myssh_in_SCP_UPLOAD_INIT(struct Curl_easy *data,
2166 struct ssh_conn *sshc,
2167 struct SSHPROTO *sshp)
2169 int rc;
2170 if(!sshp)
2171 return myssh_to_ERROR(data, sshc, CURLE_FAILED_INIT);
2173 rc = ssh_scp_init(sshc->scp_session);
2174 if(rc != SSH_OK) {
2175 const char *err_msg = ssh_get_error(sshc->ssh_session);
2176 failf(data, "%s", err_msg);
2177 return myssh_to_ERROR(data, sshc, CURLE_UPLOAD_FAILED);
2180 rc = ssh_scp_push_file64(sshc->scp_session, sshp->path,
2181 (uint64_t)data->state.infilesize,
2182 (int)data->set.new_file_perms);
2184 if(rc != SSH_OK) {
2185 const char *err_msg = ssh_get_error(sshc->ssh_session);
2186 failf(data, "%s", err_msg);
2187 return myssh_to_ERROR(data, sshc, CURLE_UPLOAD_FAILED);
2190 /* upload data */
2191 Curl_xfer_setup_send(data, FIRSTSOCKET);
2193 /* not set by Curl_xfer_setup to preserve io_flags */
2194 data->conn->recv_idx = FIRSTSOCKET;
2196 myssh_to(data, sshc, SSH_STOP);
2197 return SSH_NO_ERROR;
2200static int myssh_in_SCP_DOWNLOAD_INIT(struct Curl_easy *data,
2201 struct ssh_conn *sshc)
2203 int rc = ssh_scp_init(sshc->scp_session);
2204 if(rc != SSH_OK) {
2205 const char *err_msg = ssh_get_error(sshc->ssh_session);
2206 failf(data, "%s", err_msg);
2207 return myssh_to_ERROR(data, sshc,
2208 CURLE_COULDNT_CONNECT);
2210 myssh_to(data, sshc, SSH_SCP_DOWNLOAD);
2211 return SSH_NO_ERROR;
2214static int myssh_in_SCP_DONE(struct Curl_easy *data,
2215 struct ssh_conn *sshc)
2217 if(data->state.upload)
2218 myssh_to(data, sshc, SSH_SCP_SEND_EOF);
2219 else
2220 myssh_to(data, sshc, SSH_SCP_CHANNEL_FREE);
2221 return SSH_NO_ERROR;
2224static int myssh_in_SCP_SEND_EOF(struct Curl_easy *data,
2225 struct ssh_conn *sshc)
2227 if(sshc->scp_session) {
2228 int rc = ssh_scp_close(sshc->scp_session);
2229 if(rc == SSH_AGAIN) {
2230 /* Currently the ssh_scp_close handles waiting for
2231 * EOF in blocking way.
2232 */
2233 return SSH_AGAIN;
2235 if(rc != SSH_OK) {
2236 infof(data,
2237 "Failed to close libssh scp channel: %s",
2238 ssh_get_error(sshc->ssh_session));
2242 myssh_to(data, sshc, SSH_SCP_CHANNEL_FREE);
2243 return SSH_NO_ERROR;
2246static int myssh_in_SCP_CHANNEL_FREE(struct Curl_easy *data,
2247 struct ssh_conn *sshc)
2249 if(sshc->scp_session) {
2250 ssh_scp_free(sshc->scp_session);
2251 sshc->scp_session = NULL;
2253 CURL_TRC_SSH(data, "SCP DONE phase complete");
2255 ssh_set_blocking(sshc->ssh_session, 0);
2257 myssh_to(data, sshc, SSH_SESSION_DISCONNECT);
2258 return SSH_NO_ERROR;
2261static CURLcode myssh_in_SESSION_FREE(struct Curl_easy *data,
2262 struct ssh_conn *sshc)
2264 CURLcode result;
2265 sshc_cleanup(sshc);
2266 /* the code we are about to return */
2267 result = sshc->actualcode;
2268 memset(sshc, 0, sizeof(struct ssh_conn));
2269 connclose(data->conn);
2270 sshc->state = SSH_SESSION_FREE; /* current */
2271 sshc->nextstate = SSH_NO_STATE;
2272 myssh_to(data, sshc, SSH_STOP);
2273 return result;
2277 * myssh_statemachine() runs the SSH state machine as far as it can without
2278 * blocking and without reaching the end. The data the pointer 'block' points
2279 * to is set to TRUE if the libssh function returns SSH_AGAIN
2280 * meaning it wants to be called again when the socket is ready
2281 */
2282static CURLcode myssh_statemachine(struct Curl_easy *data,
2283 struct ssh_conn *sshc,
2284 struct SSHPROTO *sshp,
2285 bool *block)
2287 CURLcode result = CURLE_OK;
2288 int rc = SSH_NO_ERROR;
2290 *block = FALSE; /* we are not blocking by default */
2291 do {
2293 switch(sshc->state) {
2294 case SSH_INIT:
2295 myssh_state_init(data, sshc);
2296 FALLTHROUGH();
2297 case SSH_S_STARTUP:
2298 rc = myssh_in_S_STARTUP(data, sshc);
2299 if(rc)
2300 break;
2301 FALLTHROUGH();
2302 case SSH_HOSTKEY:
2303 rc = myssh_is_known(data, sshc);
2304 if(rc != SSH_OK) {
2305 rc = myssh_to_ERROR(data, sshc, CURLE_PEER_FAILED_VERIFICATION);
2306 break;
2308 myssh_to(data, sshc, SSH_AUTHLIST);
2309 FALLTHROUGH();
2310 case SSH_AUTHLIST:
2311 rc = myssh_in_AUTHLIST(data, sshc);
2312 break;
2313 case SSH_AUTH_PKEY_INIT:
2314 rc = myssh_in_AUTH_PKEY_INIT(data, sshc);
2315 break;
2316 case SSH_AUTH_PKEY:
2317 rc = myssh_in_AUTH_PKEY(data, sshc);
2318 break;
2319 case SSH_AUTH_PKEY_AUTO:
2320 rc = myssh_in_AUTH_PKEY_AUTO(data, sshc);
2321 break;
2322 case SSH_AUTH_GSSAPI:
2323 rc = myssh_in_AUTH_GSSAPI(data, sshc);
2324 break;
2325 case SSH_AUTH_KEY_INIT:
2326 rc = myssh_in_AUTH_KEY_INIT(data, sshc);
2327 break;
2328 case SSH_AUTH_KEY:
2329 rc = myssh_in_AUTH_KEY(data, sshc);
2330 break;
2331 case SSH_AUTH_PASS_INIT:
2332 rc = myssh_in_AUTH_PASS_INIT(data, sshc);
2333 break;
2334 case SSH_AUTH_PASS:
2335 rc = myssh_in_AUTH_PASS(data, sshc);
2336 break;
2337 case SSH_AUTH_DONE:
2338 rc = myssh_in_AUTH_DONE(data, sshc);
2339 break;
2340 case SSH_SFTP_INIT:
2341 rc = myssh_in_SFTP_INIT(data, sshc);
2342 break;
2343 case SSH_SFTP_REALPATH:
2344 rc = myssh_in_SFTP_REALPATH(data, sshc);
2345 break;
2346 case SSH_SFTP_QUOTE_INIT:
2347 rc = myssh_in_SFTP_QUOTE_INIT(data, sshc, sshp);
2348 break;
2349 case SSH_SFTP_POSTQUOTE_INIT:
2350 rc = myssh_in_SFTP_POSTQUOTE_INIT(data, sshc);
2351 break;
2352 case SSH_SFTP_QUOTE:
2353 rc = myssh_in_SFTP_QUOTE(data, sshc, sshp);
2354 break;
2355 case SSH_SFTP_NEXT_QUOTE:
2356 rc = myssh_in_SFTP_NEXT_QUOTE(data, sshc);
2357 break;
2358 case SSH_SFTP_QUOTE_STAT:
2359 rc = myssh_in_SFTP_QUOTE_STAT(data, sshc);
2360 break;
2361 case SSH_SFTP_QUOTE_SETSTAT:
2362 rc = myssh_in_SFTP_QUOTE_SETSTAT(data, sshc);
2363 break;
2364 case SSH_SFTP_QUOTE_SYMLINK:
2365 rc = myssh_in_SFTP_QUOTE_SYMLINK(data, sshc);
2366 break;
2367 case SSH_SFTP_QUOTE_MKDIR:
2368 rc = myssh_in_SFTP_QUOTE_MKDIR(data, sshc);
2369 break;
2370 case SSH_SFTP_QUOTE_RENAME:
2371 rc = myssh_in_SFTP_QUOTE_RENAME(data, sshc);
2372 break;
2373 case SSH_SFTP_QUOTE_RMDIR:
2374 rc = myssh_in_SFTP_QUOTE_RMDIR(data, sshc);
2375 break;
2376 case SSH_SFTP_QUOTE_UNLINK:
2377 rc = myssh_in_SFTP_QUOTE_UNLINK(data, sshc);
2378 break;
2379 case SSH_SFTP_QUOTE_STATVFS:
2380 rc = myssh_in_SFTP_QUOTE_STATVFS(data, sshc);
2381 break;
2382 case SSH_SFTP_GETINFO:
2383 rc = myssh_in_SFTP_GETINFO(data, sshc);
2384 break;
2385 case SSH_SFTP_FILETIME:
2386 rc = myssh_in_SFTP_FILETIME(data, sshc, sshp);
2387 break;
2388 case SSH_SFTP_TRANS_INIT:
2389 rc = myssh_in_SFTP_TRANS_INIT(data, sshc, sshp);
2390 break;
2391 case SSH_SFTP_UPLOAD_INIT:
2392 rc = myssh_in_UPLOAD_INIT(data, sshc, sshp);
2393 break;
2394 case SSH_SFTP_CREATE_DIRS_INIT:
2395 rc = myssh_in_SFTP_CREATE_DIRS_INIT(data, sshc, sshp);
2396 break;
2397 case SSH_SFTP_CREATE_DIRS:
2398 rc = myssh_in_SFTP_CREATE_DIRS(data, sshc, sshp);
2399 break;
2400 case SSH_SFTP_CREATE_DIRS_MKDIR:
2401 rc = myssh_in_SFTP_CREATE_DIRS_MKDIR(data, sshc, sshp);
2402 break;
2403 case SSH_SFTP_READDIR_INIT:
2404 rc = myssh_in_SFTP_READDIR_INIT(data, sshc, sshp);
2405 break;
2406 case SSH_SFTP_READDIR:
2407 rc = myssh_in_SFTP_READDIR(data, sshc, sshp);
2408 break;
2409 case SSH_SFTP_READDIR_LINK:
2410 rc = myssh_in_SFTP_READDIR_LINK(data, sshc);
2411 break;
2412 case SSH_SFTP_READDIR_BOTTOM:
2413 rc = myssh_in_SFTP_READDIR_BOTTOM(data, sshc);
2414 break;
2415 case SSH_SFTP_READDIR_DONE:
2416 rc = myssh_in_SFTP_READDIR_DONE(data, sshc);
2417 break;
2418 case SSH_SFTP_DOWNLOAD_INIT:
2419 rc = myssh_in_SFTP_DOWNLOAD_INIT(data, sshc, sshp);
2420 break;
2421 case SSH_SFTP_DOWNLOAD_STAT:
2422 rc = myssh_in_SFTP_DOWNLOAD_STAT(data, sshc);
2423 break;
2424 case SSH_SFTP_CLOSE:
2425 rc = myssh_in_SFTP_CLOSE(data, sshc, sshp);
2426 break;
2427 case SSH_SFTP_SHUTDOWN:
2428 rc = myssh_in_SFTP_SHUTDOWN(data, sshc);
2429 break;
2430 case SSH_SCP_TRANS_INIT:
2431 rc = myssh_in_TRANS_INIT(data, sshc, sshp);
2432 break;
2433 case SSH_SCP_UPLOAD_INIT:
2434 rc = myssh_in_SCP_UPLOAD_INIT(data, sshc, sshp);
2435 break;
2436 case SSH_SCP_DOWNLOAD_INIT:
2437 rc = myssh_in_SCP_DOWNLOAD_INIT(data, sshc);
2438 if(rc)
2439 break;
2440 FALLTHROUGH();
2441 case SSH_SCP_DOWNLOAD:
2442 rc = myssh_SSH_SCP_DOWNLOAD(data, sshc);
2443 break;
2444 case SSH_SCP_DONE:
2445 rc = myssh_in_SCP_DONE(data, sshc);
2446 break;
2447 case SSH_SCP_SEND_EOF:
2448 rc = myssh_in_SCP_SEND_EOF(data, sshc);
2449 break;
2450 case SSH_SCP_CHANNEL_FREE:
2451 myssh_in_SCP_CHANNEL_FREE(data, sshc);
2452 FALLTHROUGH();
2453 case SSH_SESSION_DISCONNECT:
2454 myssh_SESSION_DISCONNECT(data, sshc);
2455 FALLTHROUGH();
2456 case SSH_SESSION_FREE:
2457 result = myssh_in_SESSION_FREE(data, sshc);
2458 break;
2459 case SSH_QUIT:
2460 default:
2461 /* internal error */
2462 sshc->nextstate = SSH_NO_STATE;
2463 myssh_to(data, sshc, SSH_STOP);
2464 break;
2466 /* break the loop only on STOP or SSH_AGAIN. If `rc` is some
2467 * other error code, we will have progressed the state accordingly. */
2468 } while((rc != SSH_AGAIN) && (sshc->state != SSH_STOP));
2470 if(rc == SSH_AGAIN) {
2471 /* we would block, we need to wait for the socket to be ready (in the
2472 right direction too)! */
2473 *block = TRUE;
2475 if(!result && (sshc->state == SSH_STOP))
2476 result = sshc->actualcode;
2477 CURL_TRC_SSH(data, "[%s] statemachine() -> %d, block=%d",
2478 Curl_ssh_statename(sshc->state), (int)result, *block);
2479 return result;
2482/* called repeatedly until done from multi.c */
2483static CURLcode myssh_multi_statemach(struct Curl_easy *data,
2484 bool *done)
2486 struct connectdata *conn = data->conn;
2487 struct ssh_conn *sshc = Curl_conn_meta_get(conn, CURL_META_SSH_CONN);
2488 struct SSHPROTO *sshp = Curl_meta_get(data, CURL_META_SSH_EASY);
2489 bool block; /* we store the status and use that to provide
2490 a Curl_ssh_pollset() implementation */
2491 CURLcode result;
2493 if(!sshc || !sshp)
2494 return CURLE_FAILED_INIT;
2495 result = myssh_statemachine(data, sshc, sshp, &block);
2496 *done = (sshc->state == SSH_STOP);
2497 myssh_block2waitfor(conn, sshc, block);
2499 return result;
2502static CURLcode myssh_block_statemach(struct Curl_easy *data,
2503 struct ssh_conn *sshc,
2504 struct SSHPROTO *sshp,
2505 bool disconnect)
2507 struct connectdata *conn = data->conn;
2508 CURLcode result = CURLE_OK;
2510 while((sshc->state != SSH_STOP) && !result) {
2511 bool block;
2512 timediff_t left_ms = 1000;
2514 result = myssh_statemachine(data, sshc, sshp, &block);
2515 if(result)
2516 break;
2518 if(!disconnect) {
2519 result = Curl_pgrsCheck(data);
2520 if(result)
2521 break;
2523 left_ms = Curl_timeleft_ms(data);
2524 if(left_ms < 0) {
2525 failf(data, "Operation timed out");
2526 return CURLE_OPERATION_TIMEDOUT;
2530 if(block) {
2531 curl_socket_t fd_read = conn->sock[FIRSTSOCKET];
2532 /* wait for the socket to become ready */
2533 (void)SOCKET_READABLE(fd_read,
2534 (!left_ms || left_ms > 1000) ? 1000 : left_ms);
2538 return result;
2541static void myssh_easy_dtor(const void *key, size_t klen, void *entry)
2543 struct SSHPROTO *sshp = entry;
2544 (void)key;
2545 (void)klen;
2546 curlx_safefree(sshp->path);
2547 curlx_free(sshp);
2550static void myssh_conn_dtor(const void *key, size_t klen, void *entry)
2552 struct ssh_conn *sshc = entry;
2553 (void)key;
2554 (void)klen;
2555 sshc_cleanup(sshc);
2556 curlx_free(sshc);
2560 * SSH setup connection
2561 */
2562static CURLcode myssh_setup_connection(struct Curl_easy *data,
2563 struct connectdata *conn)
2565 struct SSHPROTO *sshp;
2566 struct ssh_conn *sshc;
2568 sshc = curlx_calloc(1, sizeof(*sshc));
2569 if(!sshc)
2570 return CURLE_OUT_OF_MEMORY;
2572 curlx_dyn_init(&sshc->readdir_buf, CURL_PATH_MAX * 2);
2573 sshc->initialized = TRUE;
2574 if(Curl_conn_meta_set(conn, CURL_META_SSH_CONN, sshc, myssh_conn_dtor))
2575 return CURLE_OUT_OF_MEMORY;
2577 sshp = curlx_calloc(1, sizeof(*sshp));
2578 if(!sshp ||
2579 Curl_meta_set(data, CURL_META_SSH_EASY, sshp, myssh_easy_dtor))
2580 return CURLE_OUT_OF_MEMORY;
2582 return Curl_ssh_setup_pkey(data, sshc);
2585static Curl_recv scp_recv, sftp_recv;
2586static Curl_send scp_send, sftp_send;
2589 * Curl_ssh_connect() gets called from Curl_protocol_connect() to allow us to
2590 * do protocol-specific actions at connect-time.
2591 */
2592static CURLcode myssh_connect(struct Curl_easy *data, bool *done)
2594 CURLcode result;
2595 struct connectdata *conn = data->conn;
2596 struct ssh_conn *sshc = Curl_conn_meta_get(conn, CURL_META_SSH_CONN);
2597 struct SSHPROTO *ssh = Curl_meta_get(data, CURL_META_SSH_EASY);
2598 curl_socket_t sock = conn->sock[FIRSTSOCKET];
2599 int rc;
2601 if(!sshc || !ssh)
2602 return CURLE_FAILED_INIT;
2604 CURL_TRC_SSH(data, "myssh_connect");
2605 if(conn->scheme->protocol & CURLPROTO_SCP) {
2606 conn->recv[FIRSTSOCKET] = scp_recv;
2607 conn->send[FIRSTSOCKET] = scp_send;
2609 else {
2610 conn->recv[FIRSTSOCKET] = sftp_recv;
2611 conn->send[FIRSTSOCKET] = sftp_send;
2614 sshc->ssh_session = ssh_new();
2615 if(!sshc->ssh_session) {
2616 failf(data, "Failure initializing ssh session");
2617 return CURLE_FAILED_INIT;
2620 /* For IPv6 origins, use the `user_hostname` that has the "[]" enclosure.
2621 * Otherwise, use `hostname` that is IDN converted. */
2622 rc = ssh_options_set(sshc->ssh_session, SSH_OPTIONS_HOST,
2623 conn->origin->ipv6 ?
2624 conn->origin->user_hostname : conn->origin->hostname);
2626 if(rc != SSH_OK) {
2627 failf(data, "Could not set remote host");
2628 return CURLE_FAILED_INIT;
2631 rc = ssh_options_parse_config(sshc->ssh_session, NULL);
2632 if(rc != SSH_OK) {
2633 infof(data, "Could not parse SSH configuration files");
2634 /* ignore */
2637 CURL_TRC_SSH(data, "myssh_connect, set socket=%" FMT_SOCKET_T, sock);
2638 rc = ssh_options_set(sshc->ssh_session, SSH_OPTIONS_FD, &sock);
2639 if(rc != SSH_OK) {
2640 failf(data, "Could not set socket");
2641 return CURLE_FAILED_INIT;
2644 if(Curl_creds_has_user(conn->creds)) {
2645 infof(data, "User: %s", conn->creds->user);
2646 rc = ssh_options_set(sshc->ssh_session, SSH_OPTIONS_USER,
2647 conn->creds->user);
2648 if(rc != SSH_OK) {
2649 failf(data, "Could not set user");
2650 return CURLE_FAILED_INIT;
2654 if(CURL_EASY_STR(data, STRING_SSH_KNOWNHOSTS)) {
2655 infof(data, "Known hosts: %s",
2656 CURL_EASY_STR(data, STRING_SSH_KNOWNHOSTS));
2657 rc = ssh_options_set(sshc->ssh_session, SSH_OPTIONS_KNOWNHOSTS,
2658 CURL_EASY_STR(data, STRING_SSH_KNOWNHOSTS));
2659 if(rc == SSH_OK)
2660 /* libssh has two separate options for this. Set both to the same file
2661 to avoid surprises */
2662 rc = ssh_options_set(sshc->ssh_session, SSH_OPTIONS_GLOBAL_KNOWNHOSTS,
2663 CURL_EASY_STR(data, STRING_SSH_KNOWNHOSTS));
2664 if(rc != SSH_OK) {
2665 failf(data, "Could not set known hosts file path");
2666 return CURLE_FAILED_INIT;
2670 if(conn->origin->port) {
2671 rc = ssh_options_set(sshc->ssh_session, SSH_OPTIONS_PORT,
2672 &conn->origin->port);
2673 if(rc != SSH_OK) {
2674 failf(data, "Could not set remote port");
2675 return CURLE_FAILED_INIT;
2679 if(data->set.ssh_compression) {
2680 rc = ssh_options_set(sshc->ssh_session, SSH_OPTIONS_COMPRESSION,
2681 "zlib,zlib@openssh.com,none");
2682 if(rc != SSH_OK) {
2683 failf(data, "Could not set compression");
2684 return CURLE_FAILED_INIT;
2688 sshc->privkey = NULL;
2689 sshc->pubkey = NULL;
2691 if(sshc->pub_key) {
2692 rc = ssh_pki_import_pubkey_file(sshc->pub_key, &sshc->pubkey);
2693 if(rc != SSH_OK) {
2694 failf(data, "Could not load public key file");
2695 return CURLE_FAILED_INIT;
2699 /* we do not verify here, we do it at the state machine,
2700 * after connection */
2702 myssh_to(data, sshc, SSH_INIT);
2704 result = myssh_multi_statemach(data, done);
2706 return result;
2709/* called from multi.c while DOing */
2710static CURLcode scp_doing(struct Curl_easy *data, bool *dophase_done)
2712 CURLcode result;
2714 result = myssh_multi_statemach(data, dophase_done);
2716 if(*dophase_done) {
2717 CURL_TRC_SSH(data, "DO phase is complete");
2719 return result;
2723 ***********************************************************************
2725 * scp_perform()
2727 * This is the actual DO function for SCP. Get a file according to
2728 * the options previously setup.
2729 */
2731static CURLcode scp_perform(struct Curl_easy *data,
2732 bool *connected, bool *dophase_done)
2734 CURLcode result = CURLE_OK;
2735 struct ssh_conn *sshc = Curl_conn_meta_get(data->conn, CURL_META_SSH_CONN);
2737 CURL_TRC_SSH(data, "DO phase starts");
2739 *dophase_done = FALSE; /* not done yet */
2740 if(!sshc)
2741 return CURLE_FAILED_INIT;
2743 /* start the first command in the DO phase */
2744 myssh_to(data, sshc, SSH_SCP_TRANS_INIT);
2746 result = myssh_multi_statemach(data, dophase_done);
2748 *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
2750 if(*dophase_done) {
2751 CURL_TRC_SSH(data, "DO phase is complete");
2754 return result;
2757/* BLOCKING, but the function is using the state machine so the only reason
2758 this is still blocking is that the multi interface code has no support for
2759 disconnecting operations that takes a while */
2760static CURLcode scp_disconnect(struct Curl_easy *data,
2761 struct connectdata *conn,
2762 bool dead_connection)
2764 CURLcode result = CURLE_OK;
2765 struct ssh_conn *sshc = Curl_conn_meta_get(conn, CURL_META_SSH_CONN);
2766 struct SSHPROTO *sshp = Curl_meta_get(data, CURL_META_SSH_EASY);
2767 (void)dead_connection;
2769 if(sshc && sshc->ssh_session) {
2770 /* only if there is a session still around to use! */
2771 myssh_to(data, sshc, SSH_SESSION_DISCONNECT);
2772 result = myssh_block_statemach(data, sshc, sshp, TRUE);
2775 return result;
2778/* generic done function for both SCP and SFTP called from their specific
2779 done functions */
2780static CURLcode myssh_done(struct Curl_easy *data,
2781 struct ssh_conn *sshc,
2782 CURLcode status)
2784 CURLcode result = CURLE_OK;
2785 struct SSHPROTO *sshp = Curl_meta_get(data, CURL_META_SSH_EASY);
2787 if(!status && sshp) {
2788 /* run the state-machine */
2789 result = myssh_block_statemach(data, sshc, sshp, FALSE);
2791 else
2792 result = status;
2794 if(Curl_pgrsDone(data, Curl_pgrs_now(data)))
2795 return CURLE_ABORTED_BY_CALLBACK;
2797 CURL_REQ_CLEAR_IO(data);
2798 return result;
2801static CURLcode scp_done(struct Curl_easy *data, CURLcode status,
2802 bool premature)
2804 struct ssh_conn *sshc = Curl_conn_meta_get(data->conn, CURL_META_SSH_CONN);
2805 (void)premature;
2807 if(!sshc)
2808 return CURLE_FAILED_INIT;
2809 if(!status)
2810 myssh_to(data, sshc, SSH_SCP_DONE);
2812 return myssh_done(data, sshc, status);
2815static CURLcode scp_send(struct Curl_easy *data, int8_t sockindex,
2816 const uint8_t *mem, size_t len, bool eos,
2817 size_t *pnwritten)
2819 int rc;
2820 struct connectdata *conn = data->conn;
2821 struct ssh_conn *sshc = Curl_conn_meta_get(conn, CURL_META_SSH_CONN);
2823 (void)sockindex; /* we only support SCP on the fixed known primary socket */
2824 (void)eos;
2825 *pnwritten = 0;
2827 if(!sshc)
2828 return CURLE_FAILED_INIT;
2830 rc = ssh_scp_write(sshc->scp_session, mem, len);
2832#if 0
2833 /* The following code is misleading, mostly added as wishful thinking
2834 * that libssh at some point would implement non-blocking ssh_scp_write/read.
2835 * Currently rc can only be number of bytes read or SSH_ERROR. */
2836 myssh_block2waitfor(conn, sshc, (rc == SSH_AGAIN));
2838 if(rc == SSH_AGAIN)
2839 return CURLE_AGAIN;
2840 else
2841#endif
2842 if(rc != SSH_OK)
2843 return CURLE_SSH;
2845 *pnwritten = len;
2846 return CURLE_OK;
2849static CURLcode scp_recv(struct Curl_easy *data, int8_t sockindex,
2850 char *mem, size_t len, size_t *pnread)
2852 struct connectdata *conn = data->conn;
2853 struct ssh_conn *sshc = Curl_conn_meta_get(conn, CURL_META_SSH_CONN);
2854 int nread;
2856 (void)sockindex; /* we only support SCP on the fixed known primary socket */
2857 *pnread = 0;
2859 if(!sshc)
2860 return CURLE_FAILED_INIT;
2862 /* libssh returns int */
2863 nread = ssh_scp_read(sshc->scp_session, mem, len);
2864 if(nread == SSH_ERROR)
2865 return CURLE_SSH;
2866#if 0
2867 /* The following code is misleading, mostly added as wishful thinking
2868 * that libssh at some point would implement non-blocking ssh_scp_write/read.
2869 * Currently rc can only be SSH_OK or SSH_ERROR. */
2871 myssh_block2waitfor(conn, sshc, (nread == SSH_AGAIN));
2872 if(nread == SSH_AGAIN)
2873 return CURLE_AGAIN;
2874#endif
2875 *pnread = (size_t)nread;
2876 return CURLE_OK;
2880 * =============== SFTP ===============
2881 */
2884 ***********************************************************************
2886 * sftp_perform()
2888 * This is the actual DO function for SFTP. Get a file/directory according to
2889 * the options previously setup.
2890 */
2892static CURLcode sftp_perform(struct Curl_easy *data,
2893 bool *connected,
2894 bool *dophase_done)
2896 struct ssh_conn *sshc = Curl_conn_meta_get(data->conn, CURL_META_SSH_CONN);
2897 CURLcode result = CURLE_OK;
2899 CURL_TRC_SSH(data, "DO phase starts");
2901 *dophase_done = FALSE; /* not done yet */
2902 if(!sshc)
2903 return CURLE_FAILED_INIT;
2905 /* start the first command in the DO phase */
2906 myssh_to(data, sshc, SSH_SFTP_QUOTE_INIT);
2908 /* run the state-machine */
2909 result = myssh_multi_statemach(data, dophase_done);
2911 *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
2913 if(*dophase_done) {
2914 CURL_TRC_SSH(data, "DO phase is complete");
2917 return result;
2920/* called from multi.c while DOing */
2921static CURLcode sftp_doing(struct Curl_easy *data,
2922 bool *dophase_done)
2924 CURLcode result = myssh_multi_statemach(data, dophase_done);
2925 if(*dophase_done) {
2926 CURL_TRC_SSH(data, "DO phase is complete");
2928 return result;
2931/* BLOCKING, but the function is using the state machine so the only reason
2932 this is still blocking is that the multi interface code has no support for
2933 disconnecting operations that takes a while */
2934static CURLcode sftp_disconnect(struct Curl_easy *data,
2935 struct connectdata *conn,
2936 bool dead_connection)
2938 struct ssh_conn *sshc = Curl_conn_meta_get(conn, CURL_META_SSH_CONN);
2939 struct SSHPROTO *sshp = Curl_meta_get(data, CURL_META_SSH_EASY);
2940 CURLcode result = CURLE_OK;
2941 (void)dead_connection;
2943 CURL_TRC_SSH(data, "DISCONNECT starts now");
2945 if(sshc && sshc->ssh_session) {
2946 /* only if there is a session still around to use! */
2947 myssh_to(data, sshc, SSH_SFTP_SHUTDOWN);
2948 result = myssh_block_statemach(data, sshc, sshp, TRUE);
2951 CURL_TRC_SSH(data, "DISCONNECT is done");
2952 return result;
2955static CURLcode sftp_done(struct Curl_easy *data, CURLcode status,
2956 bool premature)
2958 struct connectdata *conn = data->conn;
2959 struct ssh_conn *sshc = Curl_conn_meta_get(conn, CURL_META_SSH_CONN);
2961 if(!sshc)
2962 return CURLE_FAILED_INIT;
2963 if(!status) {
2964 /* Post quote commands are executed after the SFTP_CLOSE state to avoid
2965 errors that could happen due to open file handles during POSTQUOTE
2966 operation */
2967 if(!premature && data->set.postquote && !conn->bits.retry)
2968 sshc->nextstate = SSH_SFTP_POSTQUOTE_INIT;
2969 myssh_to(data, sshc, SSH_SFTP_CLOSE);
2971 return myssh_done(data, sshc, status);
2974/* return number of sent bytes */
2975static CURLcode sftp_send(struct Curl_easy *data, int8_t sockindex,
2976 const uint8_t *mem, size_t len, bool eos,
2977 size_t *pnwritten)
2979 struct connectdata *conn = data->conn;
2980 struct ssh_conn *sshc = Curl_conn_meta_get(conn, CURL_META_SSH_CONN);
2981 ssize_t nwrite;
2983 (void)sockindex;
2984 (void)eos;
2985 *pnwritten = 0;
2987 if(!sshc)
2988 return CURLE_FAILED_INIT;
2990#if LIBSSH_VERSION_INT > SSH_VERSION_INT(0, 11, 0)
2991 switch(sshc->sftp_send_state) {
2992 case 0:
2993 sftp_file_set_nonblocking(sshc->sftp_file);
2994 if(sftp_aio_begin_write(sshc->sftp_file, mem, len,
2995 &sshc->sftp_send_aio) == SSH_ERROR) {
2996 return CURLE_SEND_ERROR;
2998 sshc->sftp_send_state = 1;
2999 FALLTHROUGH();
3000 case 1:
3001 nwrite = sftp_aio_wait_write(&sshc->sftp_send_aio);
3002 myssh_block2waitfor(conn, sshc, (nwrite == SSH_AGAIN) ? TRUE : FALSE);
3003 if(nwrite == SSH_AGAIN)
3004 return CURLE_AGAIN;
3005 else if(nwrite < 0)
3006 return CURLE_SEND_ERROR;
3008 /*
3009 * sftp_aio_wait_write() would free sftp_send_aio and
3010 * assign it NULL in all cases except when it returns
3011 * SSH_AGAIN.
3012 */
3014 sshc->sftp_send_state = 0;
3015 *pnwritten = (size_t)nwrite;
3016 return CURLE_OK;
3017 default:
3018 /* we never reach here */
3019 return CURLE_SEND_ERROR;
3021#else
3022 /*
3023 * limit the writes to the maximum specified in Section 3 of
3024 * https://datatracker.ietf.org/doc/html/draft-ietf-secsh-filexfer-02
3026 * libssh started applying appropriate read/write length limits
3027 * internally since version 0.11.0, hence such an operation is
3028 * not needed for versions after (and including) 0.11.0.
3029 */
3030 if(len > 32768)
3031 len = 32768;
3033 nwrite = sftp_write(sshc->sftp_file, mem, len);
3035 myssh_block2waitfor(conn, sshc, FALSE);
3037#if 0 /* not returned by libssh on write */
3038 if(nwrite == SSH_AGAIN) {
3039 *err = CURLE_AGAIN;
3040 nwrite = 0;
3042 else
3043#endif
3044 if(nwrite < 0)
3045 return CURLE_SSH;
3047 *pnwritten = (size_t)nwrite;
3048 return CURLE_OK;
3049#endif
3053 * Return number of received (decrypted) bytes
3054 * or <0 on error
3055 */
3056static CURLcode sftp_recv(struct Curl_easy *data, int8_t sockindex,
3057 char *mem, size_t len, size_t *pnread)
3059 struct connectdata *conn = data->conn;
3060 struct ssh_conn *sshc = Curl_conn_meta_get(conn, CURL_META_SSH_CONN);
3061 ssize_t nread;
3063 (void)sockindex;
3064 *pnread = 0;
3066 DEBUGASSERT(len < CURL_MAX_READ_SIZE);
3067 if(!sshc)
3068 return CURLE_FAILED_INIT;
3070 switch(sshc->sftp_recv_state) {
3071 case 0:
3072#if LIBSSH_VERSION_INT > SSH_VERSION_INT(0, 11, 0)
3073 if(sftp_aio_begin_read(sshc->sftp_file, len,
3074 &sshc->sftp_recv_aio) == SSH_ERROR) {
3075 return CURLE_RECV_ERROR;
3077#else
3078 sshc->sftp_file_index =
3079 sftp_async_read_begin(sshc->sftp_file, (uint32_t)len);
3080 if(sshc->sftp_file_index < 0)
3081 return CURLE_RECV_ERROR;
3082#endif
3084 FALLTHROUGH();
3085 case 1:
3086 sshc->sftp_recv_state = 1;
3088#if LIBSSH_VERSION_INT > SSH_VERSION_INT(0, 11, 0)
3089 nread = sftp_aio_wait_read(&sshc->sftp_recv_aio, mem, len);
3090#else
3091 nread = sftp_async_read(sshc->sftp_file, mem, (uint32_t)len,
3092 (uint32_t)sshc->sftp_file_index);
3093#endif
3095 myssh_block2waitfor(conn, sshc, (nread == SSH_AGAIN));
3097 if(nread == SSH_AGAIN)
3098 return CURLE_AGAIN;
3099 else if(nread < 0)
3100 return CURLE_RECV_ERROR;
3102 /*
3103 * sftp_aio_wait_read() would free sftp_recv_aio and
3104 * assign it NULL in all cases except when it returns
3105 * SSH_AGAIN.
3106 */
3108 sshc->sftp_recv_state = 0;
3109 *pnread = (size_t)nread;
3110 return CURLE_OK;
3112 default:
3113 /* we never reach here */
3114 return CURLE_RECV_ERROR;
3118static CURLcode myssh_do_it(struct Curl_easy *data, bool *done)
3120 CURLcode result;
3121 bool connected = FALSE;
3122 struct connectdata *conn = data->conn;
3123 struct ssh_conn *sshc = Curl_conn_meta_get(conn, CURL_META_SSH_CONN);
3125 *done = FALSE; /* default to false */
3126 if(!sshc)
3127 return CURLE_FAILED_INIT;
3129 data->req.size = -1; /* make sure this is unknown at this point */
3131 sshc->actualcode = CURLE_OK; /* reset error code */
3132 sshc->secondCreateDirs = 0; /* reset the create directory attempt state
3133 variable */
3135 Curl_pgrsReset(data);
3137 if(conn->scheme->protocol & CURLPROTO_SCP)
3138 result = scp_perform(data, &connected, done);
3139 else
3140 result = sftp_perform(data, &connected, done);
3142 return result;
3145CURLcode Curl_ssh_init(void)
3147 if(ssh_init()) {
3148 DEBUGF(curl_mfprintf(stderr, "Error: libssh_init failed\n"));
3149 return CURLE_FAILED_INIT;
3151 return CURLE_OK;
3154void Curl_ssh_cleanup(void)
3156 (void)ssh_finalize();
3159void Curl_ssh_version(char *buffer, size_t buflen)
3161 (void)curl_msnprintf(buffer, buflen, "libssh/%s", ssh_version(0));
3165 * SCP.
3166 */
3167const struct Curl_protocol Curl_protocol_scp = {
3168 myssh_setup_connection, /* setup_connection */
3169 ZERO_NULL, /* setup_filters */
3170 myssh_do_it, /* do_it */
3171 scp_done, /* done */
3172 ZERO_NULL, /* do_more */
3173 myssh_connect, /* connect_it */
3174 myssh_multi_statemach, /* connecting */
3175 scp_doing, /* doing */
3176 Curl_ssh_pollset, /* proto_pollset */
3177 Curl_ssh_pollset, /* doing_pollset */
3178 ZERO_NULL, /* domore_pollset */
3179 Curl_ssh_pollset, /* perform_pollset */
3180 scp_disconnect, /* disconnect */
3181 ZERO_NULL, /* write_resp */
3182 ZERO_NULL, /* write_resp_hd */
3183 ZERO_NULL, /* connection_is_dead */
3184 ZERO_NULL, /* attach connection */
3185 ZERO_NULL, /* follow */
3189 * SFTP.
3190 */
3191const struct Curl_protocol Curl_protocol_sftp = {
3192 myssh_setup_connection, /* setup_connection */
3193 ZERO_NULL, /* setup_filters */
3194 myssh_do_it, /* do_it */
3195 sftp_done, /* done */
3196 ZERO_NULL, /* do_more */
3197 myssh_connect, /* connect_it */
3198 myssh_multi_statemach, /* connecting */
3199 sftp_doing, /* doing */
3200 Curl_ssh_pollset, /* proto_pollset */
3201 Curl_ssh_pollset, /* doing_pollset */
3202 ZERO_NULL, /* domore_pollset */
3203 Curl_ssh_pollset, /* perform_pollset */
3204 sftp_disconnect, /* disconnect */
3205 ZERO_NULL, /* write_resp */
3206 ZERO_NULL, /* write_resp_hd */
3207 ZERO_NULL, /* connection_is_dead */
3208 ZERO_NULL, /* attach connection */
3209 ZERO_NULL, /* follow */
3212#endif /* USE_LIBSSH */
Diagnostics4 git calls · 29 ms
CallStatusSizeTime029 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=23
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch20018 KiB · hit23 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=18629
git.response.cache=HIT
git.response.duration_ms=23
git.fetch.wants=16c2056b707f593f6f39d9d946475e332c880a3b
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=18629
git.pkt.payload.max=18608
git.sideband.band1.bytes=18607
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e25fc10b2f224d
Request
dfd871b3…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format