1 /* $OpenBSD: sftp-client.c,v 1.162 2022/03/31 03:07:03 djm Exp $ */
3 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 /* XXX: signed vs unsigned */
20 /* XXX: remove all logging, only return status codes */
21 /* XXX: copy between two remote sites */
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_STATVFS_H
27 #include <sys/statvfs.h>
29 #include "openbsd-compat/sys-queue.h"
30 #ifdef HAVE_SYS_STAT_H
31 # include <sys/stat.h>
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
43 # ifdef HAVE_SYS_POLL_H
44 # include <sys/poll.h>
60 #include "progressmeter.h"
65 #include "sftp-common.h"
66 #include "sftp-client.h"
68 extern volatile sig_atomic_t interrupted;
69 extern int showprogress;
71 /* Default size of buffer for up/download */
72 #define DEFAULT_COPY_BUFLEN 32768
74 /* Default number of concurrent outstanding requests */
75 #define DEFAULT_NUM_REQUESTS 64
77 /* Minimum amount of data to read at a time */
78 #define MIN_READ_SIZE 512
80 /* Maximum depth to descend in directory trees */
81 #define MAX_DIR_DEPTH 64
83 /* Directory separator characters */
85 # define SFTP_DIRECTORY_CHARS "/\\"
86 #else /* HAVE_CYGWIN */
87 # define SFTP_DIRECTORY_CHARS "/"
88 #endif /* HAVE_CYGWIN */
93 u_int download_buflen;
98 #define SFTP_EXT_POSIX_RENAME 0x00000001
99 #define SFTP_EXT_STATVFS 0x00000002
100 #define SFTP_EXT_FSTATVFS 0x00000004
101 #define SFTP_EXT_HARDLINK 0x00000008
102 #define SFTP_EXT_FSYNC 0x00000010
103 #define SFTP_EXT_LSETSTAT 0x00000020
104 #define SFTP_EXT_LIMITS 0x00000040
105 #define SFTP_EXT_PATH_EXPAND 0x00000080
106 #define SFTP_EXT_COPY_DATA 0x00000100
108 u_int64_t limit_kbps;
109 struct bwlimit bwlimit_in, bwlimit_out;
112 /* Tracks in-progress requests during file transfers */
117 TAILQ_ENTRY(request) tq;
119 TAILQ_HEAD(requests, request);
122 get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
123 const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
125 static struct request *
126 request_enqueue(struct requests *requests, u_int id, size_t len,
131 req = xcalloc(1, sizeof(*req));
134 req->offset = offset;
135 TAILQ_INSERT_TAIL(requests, req, tq);
139 static struct request *
140 request_find(struct requests *requests, u_int id)
144 for (req = TAILQ_FIRST(requests);
145 req != NULL && req->id != id;
146 req = TAILQ_NEXT(req, tq))
153 sftpio(void *_bwlimit, size_t amount)
155 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
157 refresh_progress_meter(0);
159 bandwidth_limit(bwlimit, amount);
164 send_msg(struct sftp_conn *conn, struct sshbuf *m)
169 if (sshbuf_len(m) > SFTP_MAX_MSG_LENGTH)
170 fatal("Outbound message too long %zu", sshbuf_len(m));
172 /* Send length first */
173 put_u32(mlen, sshbuf_len(m));
174 iov[0].iov_base = mlen;
175 iov[0].iov_len = sizeof(mlen);
176 iov[1].iov_base = (u_char *)sshbuf_ptr(m);
177 iov[1].iov_len = sshbuf_len(m);
179 if (atomiciov6(writev, conn->fd_out, iov, 2, sftpio,
180 conn->limit_kbps > 0 ? &conn->bwlimit_out : NULL) !=
181 sshbuf_len(m) + sizeof(mlen))
182 fatal("Couldn't send packet: %s", strerror(errno));
188 get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
195 if ((r = sshbuf_reserve(m, 4, &p)) != 0)
196 fatal_fr(r, "reserve");
197 if (atomicio6(read, conn->fd_in, p, 4, sftpio,
198 conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL) != 4) {
199 if (errno == EPIPE || errno == ECONNRESET)
200 fatal("Connection closed");
202 fatal("Couldn't read packet: %s", strerror(errno));
205 if ((r = sshbuf_get_u32(m, &msg_len)) != 0)
206 fatal_fr(r, "sshbuf_get_u32");
207 if (msg_len > SFTP_MAX_MSG_LENGTH) {
208 do_log2(initial ? SYSLOG_LEVEL_ERROR : SYSLOG_LEVEL_FATAL,
209 "Received message too long %u", msg_len);
210 fatal("Ensure the remote shell produces no output "
211 "for non-interactive sessions.");
214 if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
215 fatal_fr(r, "reserve");
216 if (atomicio6(read, conn->fd_in, p, msg_len, sftpio,
217 conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL)
220 fatal("Connection closed");
222 fatal("Read packet: %s", strerror(errno));
227 get_msg(struct sftp_conn *conn, struct sshbuf *m)
229 get_msg_extended(conn, m, 0);
233 send_string_request(struct sftp_conn *conn, u_int id, u_int code, const char *s,
239 if ((msg = sshbuf_new()) == NULL)
240 fatal_f("sshbuf_new failed");
241 if ((r = sshbuf_put_u8(msg, code)) != 0 ||
242 (r = sshbuf_put_u32(msg, id)) != 0 ||
243 (r = sshbuf_put_string(msg, s, len)) != 0)
244 fatal_fr(r, "compose");
246 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
251 send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
252 const void *s, u_int len, Attrib *a)
257 if ((msg = sshbuf_new()) == NULL)
258 fatal_f("sshbuf_new failed");
259 if ((r = sshbuf_put_u8(msg, code)) != 0 ||
260 (r = sshbuf_put_u32(msg, id)) != 0 ||
261 (r = sshbuf_put_string(msg, s, len)) != 0 ||
262 (r = encode_attrib(msg, a)) != 0)
263 fatal_fr(r, "compose");
265 debug3("Sent message fd %d T:%u I:%u F:0x%04x M:%05o",
266 conn->fd_out, code, id, a->flags, a->perm);
271 get_status(struct sftp_conn *conn, u_int expected_id)
278 if ((msg = sshbuf_new()) == NULL)
279 fatal_f("sshbuf_new failed");
281 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
282 (r = sshbuf_get_u32(msg, &id)) != 0)
283 fatal_fr(r, "compose");
285 if (id != expected_id)
286 fatal("ID mismatch (%u != %u)", id, expected_id);
287 if (type != SSH2_FXP_STATUS)
288 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
289 SSH2_FXP_STATUS, type);
291 if ((r = sshbuf_get_u32(msg, &status)) != 0)
292 fatal_fr(r, "parse");
295 debug3("SSH2_FXP_STATUS %u", status);
301 get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
302 const char *errfmt, ...)
312 va_start(args, errfmt);
314 vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
317 if ((msg = sshbuf_new()) == NULL)
318 fatal_f("sshbuf_new failed");
320 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
321 (r = sshbuf_get_u32(msg, &id)) != 0)
322 fatal_fr(r, "parse");
324 if (id != expected_id)
325 fatal("%s: ID mismatch (%u != %u)",
326 errfmt == NULL ? __func__ : errmsg, id, expected_id);
327 if (type == SSH2_FXP_STATUS) {
328 if ((r = sshbuf_get_u32(msg, &status)) != 0)
329 fatal_fr(r, "parse status");
331 error("%s: %s", errmsg, fx2txt(status));
334 } else if (type != SSH2_FXP_HANDLE)
335 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
336 errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
338 if ((r = sshbuf_get_string(msg, &handle, len)) != 0)
339 fatal_fr(r, "parse handle");
345 /* XXX returning &static is error-prone. Refactor to fill *Attrib argument */
347 get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
355 if ((msg = sshbuf_new()) == NULL)
356 fatal_f("sshbuf_new failed");
359 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
360 (r = sshbuf_get_u32(msg, &id)) != 0)
361 fatal_fr(r, "parse");
363 if (id != expected_id)
364 fatal("ID mismatch (%u != %u)", id, expected_id);
365 if (type == SSH2_FXP_STATUS) {
368 if ((r = sshbuf_get_u32(msg, &status)) != 0)
369 fatal_fr(r, "parse status");
371 debug("stat remote: %s", fx2txt(status));
373 error("stat remote: %s", fx2txt(status));
376 } else if (type != SSH2_FXP_ATTRS) {
377 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
378 SSH2_FXP_ATTRS, type);
380 if ((r = decode_attrib(msg, &a)) != 0) {
381 error_fr(r, "decode_attrib");
385 debug3("Received stat reply T:%u I:%u F:0x%04x M:%05o",
386 type, id, a.flags, a.perm);
393 get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
394 u_int expected_id, int quiet)
402 if ((msg = sshbuf_new()) == NULL)
403 fatal_f("sshbuf_new failed");
406 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
407 (r = sshbuf_get_u32(msg, &id)) != 0)
408 fatal_fr(r, "parse");
410 debug3("Received statvfs reply T:%u I:%u", type, id);
411 if (id != expected_id)
412 fatal("ID mismatch (%u != %u)", id, expected_id);
413 if (type == SSH2_FXP_STATUS) {
416 if ((r = sshbuf_get_u32(msg, &status)) != 0)
417 fatal_fr(r, "parse status");
419 debug("remote statvfs: %s", fx2txt(status));
421 error("remote statvfs: %s", fx2txt(status));
424 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
425 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
426 SSH2_FXP_EXTENDED_REPLY, type);
429 memset(st, 0, sizeof(*st));
430 if ((r = sshbuf_get_u64(msg, &st->f_bsize)) != 0 ||
431 (r = sshbuf_get_u64(msg, &st->f_frsize)) != 0 ||
432 (r = sshbuf_get_u64(msg, &st->f_blocks)) != 0 ||
433 (r = sshbuf_get_u64(msg, &st->f_bfree)) != 0 ||
434 (r = sshbuf_get_u64(msg, &st->f_bavail)) != 0 ||
435 (r = sshbuf_get_u64(msg, &st->f_files)) != 0 ||
436 (r = sshbuf_get_u64(msg, &st->f_ffree)) != 0 ||
437 (r = sshbuf_get_u64(msg, &st->f_favail)) != 0 ||
438 (r = sshbuf_get_u64(msg, &st->f_fsid)) != 0 ||
439 (r = sshbuf_get_u64(msg, &flag)) != 0 ||
440 (r = sshbuf_get_u64(msg, &st->f_namemax)) != 0)
441 fatal_fr(r, "parse statvfs");
443 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
444 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
452 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
453 u_int64_t limit_kbps)
457 struct sftp_conn *ret;
460 ret = xcalloc(1, sizeof(*ret));
463 ret->fd_out = fd_out;
464 ret->download_buflen = ret->upload_buflen =
465 transfer_buflen ? transfer_buflen : DEFAULT_COPY_BUFLEN;
467 num_requests ? num_requests : DEFAULT_NUM_REQUESTS;
471 if ((msg = sshbuf_new()) == NULL)
472 fatal_f("sshbuf_new failed");
473 if ((r = sshbuf_put_u8(msg, SSH2_FXP_INIT)) != 0 ||
474 (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0)
475 fatal_fr(r, "parse");
479 get_msg_extended(ret, msg, 1);
481 /* Expecting a VERSION reply */
482 if ((r = sshbuf_get_u8(msg, &type)) != 0)
483 fatal_fr(r, "parse type");
484 if (type != SSH2_FXP_VERSION) {
485 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
491 if ((r = sshbuf_get_u32(msg, &ret->version)) != 0)
492 fatal_fr(r, "parse version");
494 debug2("Remote version: %u", ret->version);
496 /* Check for extensions */
497 while (sshbuf_len(msg) > 0) {
503 if ((r = sshbuf_get_cstring(msg, &name, NULL)) != 0 ||
504 (r = sshbuf_get_string(msg, &value, &vlen)) != 0)
505 fatal_fr(r, "parse extension");
506 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
507 strcmp((char *)value, "1") == 0) {
508 ret->exts |= SFTP_EXT_POSIX_RENAME;
510 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
511 strcmp((char *)value, "2") == 0) {
512 ret->exts |= SFTP_EXT_STATVFS;
514 } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
515 strcmp((char *)value, "2") == 0) {
516 ret->exts |= SFTP_EXT_FSTATVFS;
518 } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
519 strcmp((char *)value, "1") == 0) {
520 ret->exts |= SFTP_EXT_HARDLINK;
522 } else if (strcmp(name, "fsync@openssh.com") == 0 &&
523 strcmp((char *)value, "1") == 0) {
524 ret->exts |= SFTP_EXT_FSYNC;
526 } else if (strcmp(name, "lsetstat@openssh.com") == 0 &&
527 strcmp((char *)value, "1") == 0) {
528 ret->exts |= SFTP_EXT_LSETSTAT;
530 } else if (strcmp(name, "limits@openssh.com") == 0 &&
531 strcmp((char *)value, "1") == 0) {
532 ret->exts |= SFTP_EXT_LIMITS;
534 } else if (strcmp(name, "expand-path@openssh.com") == 0 &&
535 strcmp((char *)value, "1") == 0) {
536 ret->exts |= SFTP_EXT_PATH_EXPAND;
538 } else if (strcmp(name, "copy-data") == 0 &&
539 strcmp((char *)value, "1") == 0) {
540 ret->exts |= SFTP_EXT_COPY_DATA;
544 debug2("Server supports extension \"%s\" revision %s",
547 debug2("Unrecognised server extension \"%s\"", name);
555 /* Query the server for its limits */
556 if (ret->exts & SFTP_EXT_LIMITS) {
557 struct sftp_limits limits;
558 if (do_limits(ret, &limits) != 0)
559 fatal_f("limits failed");
561 /* If the caller did not specify, find a good value */
562 if (transfer_buflen == 0) {
563 ret->download_buflen = limits.read_length;
564 ret->upload_buflen = limits.write_length;
565 debug("Using server download size %u", ret->download_buflen);
566 debug("Using server upload size %u", ret->upload_buflen);
569 /* Use the server limit to scale down our value only */
570 if (num_requests == 0 && limits.open_handles) {
572 MINIMUM(DEFAULT_NUM_REQUESTS, limits.open_handles);
573 debug("Server handle limit %llu; using %u",
574 (unsigned long long)limits.open_handles,
579 /* Some filexfer v.0 servers don't support large packets */
580 if (ret->version == 0) {
581 ret->download_buflen = MINIMUM(ret->download_buflen, 20480);
582 ret->upload_buflen = MINIMUM(ret->upload_buflen, 20480);
585 ret->limit_kbps = limit_kbps;
586 if (ret->limit_kbps > 0) {
587 bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
588 ret->download_buflen);
589 bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
597 sftp_proto_version(struct sftp_conn *conn)
599 return conn->version;
603 do_limits(struct sftp_conn *conn, struct sftp_limits *limits)
610 if ((conn->exts & SFTP_EXT_LIMITS) == 0) {
611 error("Server does not support limits@openssh.com extension");
615 if ((msg = sshbuf_new()) == NULL)
616 fatal_f("sshbuf_new failed");
619 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
620 (r = sshbuf_put_u32(msg, id)) != 0 ||
621 (r = sshbuf_put_cstring(msg, "limits@openssh.com")) != 0)
622 fatal_fr(r, "compose");
624 debug3("Sent message limits@openssh.com I:%u", id);
628 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
629 (r = sshbuf_get_u32(msg, &msg_id)) != 0)
630 fatal_fr(r, "parse");
632 debug3("Received limits reply T:%u I:%u", type, msg_id);
634 fatal("ID mismatch (%u != %u)", msg_id, id);
635 if (type != SSH2_FXP_EXTENDED_REPLY) {
636 debug_f("expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
637 SSH2_FXP_EXTENDED_REPLY, type);
638 /* Disable the limits extension */
639 conn->exts &= ~SFTP_EXT_LIMITS;
644 memset(limits, 0, sizeof(*limits));
645 if ((r = sshbuf_get_u64(msg, &limits->packet_length)) != 0 ||
646 (r = sshbuf_get_u64(msg, &limits->read_length)) != 0 ||
647 (r = sshbuf_get_u64(msg, &limits->write_length)) != 0 ||
648 (r = sshbuf_get_u64(msg, &limits->open_handles)) != 0)
649 fatal_fr(r, "parse limits");
657 do_close(struct sftp_conn *conn, const u_char *handle, u_int handle_len)
663 if ((msg = sshbuf_new()) == NULL)
664 fatal_f("sshbuf_new failed");
667 if ((r = sshbuf_put_u8(msg, SSH2_FXP_CLOSE)) != 0 ||
668 (r = sshbuf_put_u32(msg, id)) != 0 ||
669 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
670 fatal_fr(r, "parse");
672 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
674 status = get_status(conn, id);
675 if (status != SSH2_FX_OK)
676 error("close remote: %s", fx2txt(status));
680 return status == SSH2_FX_OK ? 0 : -1;
685 do_lsreaddir(struct sftp_conn *conn, const char *path, int print_flag,
689 u_int count, id, i, expected_id, ents = 0;
691 u_char type, *handle;
692 int status = SSH2_FX_FAILURE;
700 if ((msg = sshbuf_new()) == NULL)
701 fatal_f("sshbuf_new failed");
702 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPENDIR)) != 0 ||
703 (r = sshbuf_put_u32(msg, id)) != 0 ||
704 (r = sshbuf_put_cstring(msg, path)) != 0)
705 fatal_fr(r, "compose OPENDIR");
708 handle = get_handle(conn, id, &handle_len,
709 "remote readdir(\"%s\")", path);
710 if (handle == NULL) {
717 *dir = xcalloc(1, sizeof(**dir));
721 for (; !interrupted;) {
722 id = expected_id = conn->msg_id++;
724 debug3("Sending SSH2_FXP_READDIR I:%u", id);
727 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READDIR)) != 0 ||
728 (r = sshbuf_put_u32(msg, id)) != 0 ||
729 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
730 fatal_fr(r, "compose READDIR");
737 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
738 (r = sshbuf_get_u32(msg, &id)) != 0)
739 fatal_fr(r, "parse");
741 debug3("Received reply T:%u I:%u", type, id);
743 if (id != expected_id)
744 fatal("ID mismatch (%u != %u)", id, expected_id);
746 if (type == SSH2_FXP_STATUS) {
749 if ((r = sshbuf_get_u32(msg, &rstatus)) != 0)
750 fatal_fr(r, "parse status");
751 debug3("Received SSH2_FXP_STATUS %d", rstatus);
752 if (rstatus == SSH2_FX_EOF)
754 error("Couldn't read directory: %s", fx2txt(rstatus));
756 } else if (type != SSH2_FXP_NAME)
757 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
758 SSH2_FXP_NAME, type);
760 if ((r = sshbuf_get_u32(msg, &count)) != 0)
761 fatal_fr(r, "parse count");
762 if (count > SSHBUF_SIZE_MAX)
763 fatal_f("nonsensical number of entries");
766 debug3("Received %d SSH2_FXP_NAME responses", count);
767 for (i = 0; i < count; i++) {
768 char *filename, *longname;
771 if ((r = sshbuf_get_cstring(msg, &filename,
773 (r = sshbuf_get_cstring(msg, &longname,
775 fatal_fr(r, "parse filenames");
776 if ((r = decode_attrib(msg, &a)) != 0) {
777 error_fr(r, "couldn't decode attrib");
784 mprintf("%s\n", longname);
787 * Directory entries should never contain '/'
788 * These can be used to attack recursive ops
789 * (e.g. send '../../../../etc/passwd')
791 if (strpbrk(filename, SFTP_DIRECTORY_CHARS) != NULL) {
792 error("Server sent suspect path \"%s\" "
793 "during readdir of \"%s\"", filename, path);
795 *dir = xreallocarray(*dir, ents + 2, sizeof(**dir));
796 (*dir)[ents] = xcalloc(1, sizeof(***dir));
797 (*dir)[ents]->filename = xstrdup(filename);
798 (*dir)[ents]->longname = xstrdup(longname);
799 memcpy(&(*dir)[ents]->a, &a, sizeof(a));
800 (*dir)[++ents] = NULL;
810 do_close(conn, handle, handle_len);
813 if (status != 0 && dir != NULL) {
814 /* Don't return results on error */
815 free_sftp_dirents(*dir);
817 } else if (interrupted && dir != NULL && *dir != NULL) {
818 /* Don't return partial matches on interrupt */
819 free_sftp_dirents(*dir);
820 *dir = xcalloc(1, sizeof(**dir));
824 return status == SSH2_FX_OK ? 0 : -1;
828 do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir)
830 return(do_lsreaddir(conn, path, 0, dir));
833 void free_sftp_dirents(SFTP_DIRENT **s)
839 for (i = 0; s[i]; i++) {
840 free(s[i]->filename);
841 free(s[i]->longname);
848 do_rm(struct sftp_conn *conn, const char *path)
852 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
855 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
856 status = get_status(conn, id);
857 if (status != SSH2_FX_OK)
858 error("remote delete %s: %s", path, fx2txt(status));
859 return status == SSH2_FX_OK ? 0 : -1;
863 do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag)
867 debug2("Sending SSH2_FXP_MKDIR \"%s\"", path);
870 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
873 status = get_status(conn, id);
874 if (status != SSH2_FX_OK && print_flag)
875 error("remote mkdir \"%s\": %s", path, fx2txt(status));
877 return status == SSH2_FX_OK ? 0 : -1;
881 do_rmdir(struct sftp_conn *conn, const char *path)
885 debug2("Sending SSH2_FXP_RMDIR \"%s\"", path);
888 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
891 status = get_status(conn, id);
892 if (status != SSH2_FX_OK)
893 error("remote rmdir \"%s\": %s", path, fx2txt(status));
895 return status == SSH2_FX_OK ? 0 : -1;
899 do_stat(struct sftp_conn *conn, const char *path, int quiet)
903 debug2("Sending SSH2_FXP_STAT \"%s\"", path);
907 send_string_request(conn, id,
908 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
911 return(get_decode_stat(conn, id, quiet));
915 do_lstat(struct sftp_conn *conn, const char *path, int quiet)
919 if (conn->version == 0) {
921 debug("Server version does not support lstat operation");
923 logit("Server version does not support lstat operation");
924 return(do_stat(conn, path, quiet));
928 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
931 return(get_decode_stat(conn, id, quiet));
936 do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
941 debug2("Sending SSH2_FXP_FSTAT \"%s\"");
944 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
947 return(get_decode_stat(conn, id, quiet));
952 do_setstat(struct sftp_conn *conn, const char *path, Attrib *a)
956 debug2("Sending SSH2_FXP_SETSTAT \"%s\"", path);
959 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
962 status = get_status(conn, id);
963 if (status != SSH2_FX_OK)
964 error("remote setstat \"%s\": %s", path, fx2txt(status));
966 return status == SSH2_FX_OK ? 0 : -1;
970 do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
975 debug2("Sending SSH2_FXP_FSETSTAT");
978 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
981 status = get_status(conn, id);
982 if (status != SSH2_FX_OK)
983 error("remote fsetstat: %s", fx2txt(status));
985 return status == SSH2_FX_OK ? 0 : -1;
988 /* Implements both the realpath and expand-path operations */
990 do_realpath_expand(struct sftp_conn *conn, const char *path, int expand)
993 u_int expected_id, count, id;
994 char *filename, *longname;
998 const char *what = "SSH2_FXP_REALPATH";
1001 what = "expand-path@openssh.com";
1002 if ((msg = sshbuf_new()) == NULL)
1003 fatal_f("sshbuf_new failed");
1005 expected_id = id = conn->msg_id++;
1007 debug2("Sending SSH2_FXP_EXTENDED(expand-path@openssh.com) "
1009 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1010 (r = sshbuf_put_u32(msg, id)) != 0 ||
1011 (r = sshbuf_put_cstring(msg,
1012 "expand-path@openssh.com")) != 0 ||
1013 (r = sshbuf_put_cstring(msg, path)) != 0)
1014 fatal_fr(r, "compose %s", what);
1015 send_msg(conn, msg);
1017 debug2("Sending SSH2_FXP_REALPATH \"%s\"", path);
1018 send_string_request(conn, id, SSH2_FXP_REALPATH,
1019 path, strlen(path));
1022 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1023 (r = sshbuf_get_u32(msg, &id)) != 0)
1024 fatal_fr(r, "parse");
1026 if (id != expected_id)
1027 fatal("ID mismatch (%u != %u)", id, expected_id);
1029 if (type == SSH2_FXP_STATUS) {
1033 if ((r = sshbuf_get_u32(msg, &status)) != 0 ||
1034 (r = sshbuf_get_cstring(msg, &errmsg, NULL)) != 0)
1035 fatal_fr(r, "parse status");
1036 error("%s %s: %s", expand ? "expand" : "realpath",
1037 path, *errmsg == '\0' ? fx2txt(status) : errmsg);
1041 } else if (type != SSH2_FXP_NAME)
1042 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1043 SSH2_FXP_NAME, type);
1045 if ((r = sshbuf_get_u32(msg, &count)) != 0)
1046 fatal_fr(r, "parse count");
1048 fatal("Got multiple names (%d) from %s", count, what);
1050 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
1051 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
1052 (r = decode_attrib(msg, &a)) != 0)
1053 fatal_fr(r, "parse filename/attrib");
1055 debug3("%s %s -> %s", what, path, filename);
1065 do_realpath(struct sftp_conn *conn, const char *path)
1067 return do_realpath_expand(conn, path, 0);
1071 can_expand_path(struct sftp_conn *conn)
1073 return (conn->exts & SFTP_EXT_PATH_EXPAND) != 0;
1077 do_expand_path(struct sftp_conn *conn, const char *path)
1079 if (!can_expand_path(conn)) {
1080 debug3_f("no server support, fallback to realpath");
1081 return do_realpath_expand(conn, path, 0);
1083 return do_realpath_expand(conn, path, 1);
1087 do_copy(struct sftp_conn *conn, const char *oldpath, const char *newpath)
1091 u_char *old_handle, *new_handle;
1092 u_int mode, status, id;
1093 size_t old_handle_len, new_handle_len;
1096 /* Return if the extension is not supported */
1097 if ((conn->exts & SFTP_EXT_COPY_DATA) == 0) {
1098 error("Server does not support copy-data extension");
1102 /* Make sure the file exists, and we can copy its perms */
1103 if ((a = do_stat(conn, oldpath, 0)) == NULL)
1106 /* Do not preserve set[ug]id here, as we do not preserve ownership */
1107 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
1108 mode = a->perm & 0777;
1110 if (!S_ISREG(a->perm)) {
1111 error("Cannot copy non-regular file: %s", oldpath);
1115 /* NB: The user's umask will apply to this */
1119 /* Set up the new perms for the new file */
1122 a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1124 if ((msg = sshbuf_new()) == NULL)
1125 fatal("%s: sshbuf_new failed", __func__);
1127 attrib_clear(&junk); /* Send empty attributes */
1129 /* Open the old file for reading */
1130 id = conn->msg_id++;
1131 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1132 (r = sshbuf_put_u32(msg, id)) != 0 ||
1133 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
1134 (r = sshbuf_put_u32(msg, SSH2_FXF_READ)) != 0 ||
1135 (r = encode_attrib(msg, &junk)) != 0)
1136 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1137 send_msg(conn, msg);
1138 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, oldpath);
1142 old_handle = get_handle(conn, id, &old_handle_len,
1143 "remote open(\"%s\")", oldpath);
1144 if (old_handle == NULL) {
1149 /* Open the new file for writing */
1150 id = conn->msg_id++;
1151 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1152 (r = sshbuf_put_u32(msg, id)) != 0 ||
1153 (r = sshbuf_put_cstring(msg, newpath)) != 0 ||
1154 (r = sshbuf_put_u32(msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|
1155 SSH2_FXF_TRUNC)) != 0 ||
1156 (r = encode_attrib(msg, a)) != 0)
1157 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1158 send_msg(conn, msg);
1159 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, newpath);
1163 new_handle = get_handle(conn, id, &new_handle_len,
1164 "remote open(\"%s\")", newpath);
1165 if (new_handle == NULL) {
1171 /* Copy the file data */
1172 id = conn->msg_id++;
1173 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1174 (r = sshbuf_put_u32(msg, id)) != 0 ||
1175 (r = sshbuf_put_cstring(msg, "copy-data")) != 0 ||
1176 (r = sshbuf_put_string(msg, old_handle, old_handle_len)) != 0 ||
1177 (r = sshbuf_put_u64(msg, 0)) != 0 ||
1178 (r = sshbuf_put_u64(msg, 0)) != 0 ||
1179 (r = sshbuf_put_string(msg, new_handle, new_handle_len)) != 0 ||
1180 (r = sshbuf_put_u64(msg, 0)) != 0)
1181 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1182 send_msg(conn, msg);
1183 debug3("Sent message copy-data \"%s\" 0 0 -> \"%s\" 0",
1186 status = get_status(conn, id);
1187 if (status != SSH2_FX_OK)
1188 error("Couldn't copy file \"%s\" to \"%s\": %s", oldpath,
1189 newpath, fx2txt(status));
1191 /* Clean up everything */
1193 do_close(conn, old_handle, old_handle_len);
1194 do_close(conn, new_handle, new_handle_len);
1198 return status == SSH2_FX_OK ? 0 : -1;
1202 do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath,
1207 int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
1209 if ((msg = sshbuf_new()) == NULL)
1210 fatal_f("sshbuf_new failed");
1212 /* Send rename request */
1213 id = conn->msg_id++;
1215 debug2("Sending SSH2_FXP_EXTENDED(posix-rename@openssh.com) "
1216 "\"%s\" to \"%s\"", oldpath, newpath);
1217 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1218 (r = sshbuf_put_u32(msg, id)) != 0 ||
1219 (r = sshbuf_put_cstring(msg,
1220 "posix-rename@openssh.com")) != 0)
1221 fatal_fr(r, "compose posix-rename");
1223 debug2("Sending SSH2_FXP_RENAME \"%s\" to \"%s\"",
1225 if ((r = sshbuf_put_u8(msg, SSH2_FXP_RENAME)) != 0 ||
1226 (r = sshbuf_put_u32(msg, id)) != 0)
1227 fatal_fr(r, "compose rename");
1229 if ((r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
1230 (r = sshbuf_put_cstring(msg, newpath)) != 0)
1231 fatal_fr(r, "compose paths");
1232 send_msg(conn, msg);
1233 debug3("Sent message %s \"%s\" -> \"%s\"",
1234 use_ext ? "posix-rename@openssh.com" :
1235 "SSH2_FXP_RENAME", oldpath, newpath);
1238 status = get_status(conn, id);
1239 if (status != SSH2_FX_OK)
1240 error("remote rename \"%s\" to \"%s\": %s", oldpath,
1241 newpath, fx2txt(status));
1243 return status == SSH2_FX_OK ? 0 : -1;
1247 do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
1253 if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
1254 error("Server does not support hardlink@openssh.com extension");
1257 debug2("Sending SSH2_FXP_EXTENDED(hardlink@openssh.com) "
1258 "\"%s\" to \"%s\"", oldpath, newpath);
1260 if ((msg = sshbuf_new()) == NULL)
1261 fatal_f("sshbuf_new failed");
1263 /* Send link request */
1264 id = conn->msg_id++;
1265 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1266 (r = sshbuf_put_u32(msg, id)) != 0 ||
1267 (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
1268 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
1269 (r = sshbuf_put_cstring(msg, newpath)) != 0)
1270 fatal_fr(r, "compose");
1271 send_msg(conn, msg);
1272 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
1276 status = get_status(conn, id);
1277 if (status != SSH2_FX_OK)
1278 error("remote link \"%s\" to \"%s\": %s", oldpath,
1279 newpath, fx2txt(status));
1281 return status == SSH2_FX_OK ? 0 : -1;
1285 do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
1291 if (conn->version < 3) {
1292 error("This server does not support the symlink operation");
1293 return(SSH2_FX_OP_UNSUPPORTED);
1295 debug2("Sending SSH2_FXP_SYMLINK \"%s\" to \"%s\"", oldpath, newpath);
1297 if ((msg = sshbuf_new()) == NULL)
1298 fatal_f("sshbuf_new failed");
1300 /* Send symlink request */
1301 id = conn->msg_id++;
1302 if ((r = sshbuf_put_u8(msg, SSH2_FXP_SYMLINK)) != 0 ||
1303 (r = sshbuf_put_u32(msg, id)) != 0 ||
1304 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
1305 (r = sshbuf_put_cstring(msg, newpath)) != 0)
1306 fatal_fr(r, "compose");
1307 send_msg(conn, msg);
1308 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
1312 status = get_status(conn, id);
1313 if (status != SSH2_FX_OK)
1314 error("remote symlink file \"%s\" to \"%s\": %s", oldpath,
1315 newpath, fx2txt(status));
1317 return status == SSH2_FX_OK ? 0 : -1;
1321 do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len)
1327 /* Silently return if the extension is not supported */
1328 if ((conn->exts & SFTP_EXT_FSYNC) == 0)
1330 debug2("Sending SSH2_FXP_EXTENDED(fsync@openssh.com)");
1332 /* Send fsync request */
1333 if ((msg = sshbuf_new()) == NULL)
1334 fatal_f("sshbuf_new failed");
1335 id = conn->msg_id++;
1336 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1337 (r = sshbuf_put_u32(msg, id)) != 0 ||
1338 (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
1339 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1340 fatal_fr(r, "compose");
1341 send_msg(conn, msg);
1342 debug3("Sent message fsync@openssh.com I:%u", id);
1345 status = get_status(conn, id);
1346 if (status != SSH2_FX_OK)
1347 error("remote fsync: %s", fx2txt(status));
1349 return status == SSH2_FX_OK ? 0 : -1;
1354 do_readlink(struct sftp_conn *conn, const char *path)
1357 u_int expected_id, count, id;
1358 char *filename, *longname;
1363 debug2("Sending SSH2_FXP_READLINK \"%s\"", path);
1365 expected_id = id = conn->msg_id++;
1366 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
1368 if ((msg = sshbuf_new()) == NULL)
1369 fatal_f("sshbuf_new failed");
1372 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1373 (r = sshbuf_get_u32(msg, &id)) != 0)
1374 fatal_fr(r, "parse");
1376 if (id != expected_id)
1377 fatal("ID mismatch (%u != %u)", id, expected_id);
1379 if (type == SSH2_FXP_STATUS) {
1382 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1383 fatal_fr(r, "parse status");
1384 error("Couldn't readlink: %s", fx2txt(status));
1387 } else if (type != SSH2_FXP_NAME)
1388 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1389 SSH2_FXP_NAME, type);
1391 if ((r = sshbuf_get_u32(msg, &count)) != 0)
1392 fatal_fr(r, "parse count");
1394 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
1396 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
1397 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
1398 (r = decode_attrib(msg, &a)) != 0)
1399 fatal_fr(r, "parse filenames/attrib");
1401 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
1412 do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
1419 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
1420 error("Server does not support statvfs@openssh.com extension");
1424 debug2("Sending SSH2_FXP_EXTENDED(statvfs@openssh.com) \"%s\"", path);
1426 id = conn->msg_id++;
1428 if ((msg = sshbuf_new()) == NULL)
1429 fatal_f("sshbuf_new failed");
1430 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1431 (r = sshbuf_put_u32(msg, id)) != 0 ||
1432 (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
1433 (r = sshbuf_put_cstring(msg, path)) != 0)
1434 fatal_fr(r, "compose");
1435 send_msg(conn, msg);
1438 return get_decode_statvfs(conn, st, id, quiet);
1443 do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
1444 struct sftp_statvfs *st, int quiet)
1449 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
1450 error("Server does not support fstatvfs@openssh.com extension");
1454 debug2("Sending SSH2_FXP_EXTENDED(fstatvfs@openssh.com)");
1456 id = conn->msg_id++;
1458 if ((msg = sshbuf_new()) == NULL)
1459 fatal_f("sshbuf_new failed");
1460 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1461 (r = sshbuf_put_u32(msg, id)) != 0 ||
1462 (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
1463 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1464 fatal_fr(r, "compose");
1465 send_msg(conn, msg);
1468 return get_decode_statvfs(conn, st, id, quiet);
1473 do_lsetstat(struct sftp_conn *conn, const char *path, Attrib *a)
1479 if ((conn->exts & SFTP_EXT_LSETSTAT) == 0) {
1480 error("Server does not support lsetstat@openssh.com extension");
1484 debug2("Sending SSH2_FXP_EXTENDED(lsetstat@openssh.com) \"%s\"", path);
1486 id = conn->msg_id++;
1487 if ((msg = sshbuf_new()) == NULL)
1488 fatal_f("sshbuf_new failed");
1489 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1490 (r = sshbuf_put_u32(msg, id)) != 0 ||
1491 (r = sshbuf_put_cstring(msg, "lsetstat@openssh.com")) != 0 ||
1492 (r = sshbuf_put_cstring(msg, path)) != 0 ||
1493 (r = encode_attrib(msg, a)) != 0)
1494 fatal_fr(r, "compose");
1495 send_msg(conn, msg);
1498 status = get_status(conn, id);
1499 if (status != SSH2_FX_OK)
1500 error("remote lsetstat \"%s\": %s", path, fx2txt(status));
1502 return status == SSH2_FX_OK ? 0 : -1;
1506 send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
1507 u_int len, const u_char *handle, u_int handle_len)
1512 if ((msg = sshbuf_new()) == NULL)
1513 fatal_f("sshbuf_new failed");
1514 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READ)) != 0 ||
1515 (r = sshbuf_put_u32(msg, id)) != 0 ||
1516 (r = sshbuf_put_string(msg, handle, handle_len)) != 0 ||
1517 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1518 (r = sshbuf_put_u32(msg, len)) != 0)
1519 fatal_fr(r, "compose");
1520 send_msg(conn, msg);
1525 send_open(struct sftp_conn *conn, const char *path, const char *tag,
1526 u_int openmode, Attrib *a, u_char **handlep, size_t *handle_lenp)
1535 debug2("Sending SSH2_FXP_OPEN \"%s\"", path);
1541 attrib_clear(&junk); /* Send empty attributes */
1544 /* Send open request */
1545 if ((msg = sshbuf_new()) == NULL)
1546 fatal_f("sshbuf_new failed");
1547 id = conn->msg_id++;
1548 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1549 (r = sshbuf_put_u32(msg, id)) != 0 ||
1550 (r = sshbuf_put_cstring(msg, path)) != 0 ||
1551 (r = sshbuf_put_u32(msg, openmode)) != 0 ||
1552 (r = encode_attrib(msg, a)) != 0)
1553 fatal_fr(r, "compose %s open", tag);
1554 send_msg(conn, msg);
1556 debug3("Sent %s message SSH2_FXP_OPEN I:%u P:%s M:0x%04x",
1557 tag, id, path, openmode);
1558 if ((handle = get_handle(conn, id, &handle_len,
1559 "%s open \"%s\"", tag, path)) == NULL)
1563 *handle_lenp = handle_len;
1568 progress_meter_path(const char *path)
1570 const char *progresspath;
1572 if ((progresspath = strrchr(path, '/')) == NULL)
1575 if (*progresspath == '\0')
1577 return progresspath;
1581 do_download(struct sftp_conn *conn, const char *remote_path,
1582 const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
1587 int local_fd = -1, write_error;
1588 int read_error, write_errno, lmodified = 0, reordered = 0, r;
1589 u_int64_t offset = 0, size, highwater;
1590 u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
1591 off_t progress_counter;
1594 struct requests requests;
1595 struct request *req;
1598 debug2_f("download remote \"%s\" to local \"%s\"",
1599 remote_path, local_path);
1601 TAILQ_INIT(&requests);
1603 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1606 /* Do not preserve set[ug]id here, as we do not preserve ownership */
1607 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1608 mode = a->perm & 0777;
1612 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1613 (!S_ISREG(a->perm))) {
1614 error("download %s: not a regular file", remote_path);
1618 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1623 buflen = conn->download_buflen;
1625 /* Send open request */
1626 if (send_open(conn, remote_path, "remote", SSH2_FXF_READ, NULL,
1627 &handle, &handle_len) != 0)
1630 local_fd = open(local_path,
1631 O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
1632 if (local_fd == -1) {
1633 error("open local \"%s\": %s", local_path, strerror(errno));
1636 offset = highwater = 0;
1638 if (fstat(local_fd, &st) == -1) {
1639 error("stat local \"%s\": %s",
1640 local_path, strerror(errno));
1643 if (st.st_size < 0) {
1644 error("\"%s\" has negative size", local_path);
1647 if ((u_int64_t)st.st_size > size) {
1648 error("Unable to resume download of \"%s\": "
1649 "local file is larger than remote", local_path);
1651 do_close(conn, handle, handle_len);
1657 offset = highwater = st.st_size;
1660 /* Read from remote and write to local */
1661 write_error = read_error = write_errno = num_req = 0;
1663 progress_counter = offset;
1665 if (showprogress && size != 0) {
1666 start_progress_meter(progress_meter_path(remote_path),
1667 size, &progress_counter);
1670 if ((msg = sshbuf_new()) == NULL)
1671 fatal_f("sshbuf_new failed");
1673 while (num_req > 0 || max_req > 0) {
1678 * Simulate EOF on interrupt: stop sending new requests and
1679 * allow outstanding requests to drain gracefully
1682 if (num_req == 0) /* If we haven't started yet... */
1687 /* Send some more requests */
1688 while (num_req < max_req) {
1689 debug3("Request range %llu -> %llu (%d/%d)",
1690 (unsigned long long)offset,
1691 (unsigned long long)offset + buflen - 1,
1693 req = request_enqueue(&requests, conn->msg_id++,
1697 send_read_request(conn, req->id, req->offset,
1698 req->len, handle, handle_len);
1703 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1704 (r = sshbuf_get_u32(msg, &id)) != 0)
1705 fatal_fr(r, "parse");
1706 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1708 /* Find the request in our queue */
1709 if ((req = request_find(&requests, id)) == NULL)
1710 fatal("Unexpected reply %u", id);
1713 case SSH2_FXP_STATUS:
1714 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1715 fatal_fr(r, "parse status");
1716 if (status != SSH2_FX_EOF)
1719 TAILQ_REMOVE(&requests, req, tq);
1724 if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1725 fatal_fr(r, "parse data");
1726 debug3("Received data %llu -> %llu",
1727 (unsigned long long)req->offset,
1728 (unsigned long long)req->offset + len - 1);
1730 fatal("Received more data than asked for "
1731 "%zu > %zu", len, req->len);
1733 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1734 atomicio(vwrite, local_fd, data, len) != len) &&
1736 write_errno = errno;
1740 else if (!reordered && req->offset <= highwater)
1741 highwater = req->offset + len;
1742 else if (!reordered && req->offset > highwater)
1744 progress_counter += len;
1747 if (len == req->len) {
1748 TAILQ_REMOVE(&requests, req, tq);
1752 /* Resend the request for the missing data */
1753 debug3("Short data block, re-requesting "
1754 "%llu -> %llu (%2d)",
1755 (unsigned long long)req->offset + len,
1756 (unsigned long long)req->offset +
1757 req->len - 1, num_req);
1758 req->id = conn->msg_id++;
1761 send_read_request(conn, req->id,
1762 req->offset, req->len, handle, handle_len);
1763 /* Reduce the request size */
1765 buflen = MAXIMUM(MIN_READ_SIZE, len);
1767 if (max_req > 0) { /* max_req = 0 iff EOF received */
1768 if (size > 0 && offset > size) {
1769 /* Only one request at a time
1770 * after the expected EOF */
1771 debug3("Finish at %llu (%2d)",
1772 (unsigned long long)offset,
1775 } else if (max_req < conn->num_requests) {
1781 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1782 SSH2_FXP_DATA, type);
1786 if (showprogress && size)
1787 stop_progress_meter();
1790 if (TAILQ_FIRST(&requests) != NULL)
1791 fatal("Transfer complete, but requests still in queue");
1792 /* Truncate at highest contiguous point to avoid holes on interrupt */
1793 if (read_error || write_error || interrupted) {
1794 if (reordered && resume_flag) {
1795 error("Unable to resume download of \"%s\": "
1796 "server reordered requests", local_path);
1798 debug("truncating at %llu", (unsigned long long)highwater);
1799 if (ftruncate(local_fd, highwater) == -1)
1800 error("local ftruncate \"%s\": %s", local_path,
1804 error("read remote \"%s\" : %s", remote_path, fx2txt(status));
1806 do_close(conn, handle, handle_len);
1807 } else if (write_error) {
1808 error("write local \"%s\": %s", local_path,
1809 strerror(write_errno));
1810 status = SSH2_FX_FAILURE;
1811 do_close(conn, handle, handle_len);
1813 if (do_close(conn, handle, handle_len) != 0 || interrupted)
1814 status = SSH2_FX_FAILURE;
1816 status = SSH2_FX_OK;
1817 /* Override umask and utimes if asked */
1819 if (preserve_flag && fchmod(local_fd, mode) == -1)
1821 if (preserve_flag && chmod(local_path, mode) == -1)
1822 #endif /* HAVE_FCHMOD */
1823 error("local chmod \"%s\": %s", local_path,
1825 if (preserve_flag &&
1826 (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1827 struct timeval tv[2];
1828 tv[0].tv_sec = a->atime;
1829 tv[1].tv_sec = a->mtime;
1830 tv[0].tv_usec = tv[1].tv_usec = 0;
1831 if (utimes(local_path, tv) == -1)
1832 error("local set times \"%s\": %s",
1833 local_path, strerror(errno));
1835 if (resume_flag && !lmodified)
1836 logit("File \"%s\" was not modified", local_path);
1837 else if (fsync_flag) {
1838 debug("syncing \"%s\"", local_path);
1839 if (fsync(local_fd) == -1)
1840 error("local sync \"%s\": %s",
1841 local_path, strerror(errno));
1848 return status == SSH2_FX_OK ? 0 : -1;
1852 download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1853 int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
1854 int resume_flag, int fsync_flag, int follow_link_flag)
1857 SFTP_DIRENT **dir_entries;
1858 char *filename, *new_src = NULL, *new_dst = NULL;
1859 mode_t mode = 0777, tmpmode = mode;
1861 if (depth >= MAX_DIR_DEPTH) {
1862 error("Maximum directory depth exceeded: %d levels", depth);
1866 debug2_f("download dir remote \"%s\" to local \"%s\"", src, dst);
1868 if (dirattrib == NULL &&
1869 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1870 error("stat remote \"%s\" directory failed", src);
1873 if (!S_ISDIR(dirattrib->perm)) {
1874 error("\"%s\" is not a directory", src);
1877 if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
1878 mprintf("Retrieving %s\n", src);
1880 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
1881 mode = dirattrib->perm & 01777;
1882 tmpmode = mode | (S_IWUSR|S_IXUSR);
1884 debug("download remote \"%s\": server "
1885 "did not send permissions", dst);
1888 if (mkdir(dst, tmpmode) == -1 && errno != EEXIST) {
1889 error("mkdir %s: %s", dst, strerror(errno));
1893 if (do_readdir(conn, src, &dir_entries) == -1) {
1894 error("remote readdir \"%s\" failed", src);
1898 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1902 filename = dir_entries[i]->filename;
1903 new_dst = path_append(dst, filename);
1904 new_src = path_append(src, filename);
1906 if (S_ISDIR(dir_entries[i]->a.perm)) {
1907 if (strcmp(filename, ".") == 0 ||
1908 strcmp(filename, "..") == 0)
1910 if (download_dir_internal(conn, new_src, new_dst,
1911 depth + 1, &(dir_entries[i]->a), preserve_flag,
1912 print_flag, resume_flag,
1913 fsync_flag, follow_link_flag) == -1)
1915 } else if (S_ISREG(dir_entries[i]->a.perm) ||
1916 (follow_link_flag && S_ISLNK(dir_entries[i]->a.perm))) {
1918 * If this is a symlink then don't send the link's
1919 * Attrib. do_download() will do a FXP_STAT operation
1920 * and get the link target's attributes.
1922 if (do_download(conn, new_src, new_dst,
1923 S_ISLNK(dir_entries[i]->a.perm) ? NULL :
1924 &(dir_entries[i]->a),
1925 preserve_flag, resume_flag, fsync_flag) == -1) {
1926 error("Download of file %s to %s failed",
1931 logit("download \"%s\": not a regular file", new_src);
1937 if (preserve_flag) {
1938 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1939 struct timeval tv[2];
1940 tv[0].tv_sec = dirattrib->atime;
1941 tv[1].tv_sec = dirattrib->mtime;
1942 tv[0].tv_usec = tv[1].tv_usec = 0;
1943 if (utimes(dst, tv) == -1)
1944 error("local set times on \"%s\": %s",
1945 dst, strerror(errno));
1947 debug("Server did not send times for directory "
1951 if (mode != tmpmode && chmod(dst, mode) == -1)
1952 error("local chmod directory \"%s\": %s", dst,
1955 free_sftp_dirents(dir_entries);
1961 download_dir(struct sftp_conn *conn, const char *src, const char *dst,
1962 Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1963 int fsync_flag, int follow_link_flag)
1968 if ((src_canon = do_realpath(conn, src)) == NULL) {
1969 error("download \"%s\": path canonicalization failed", src);
1973 ret = download_dir_internal(conn, src_canon, dst, 0,
1974 dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag,
1981 do_upload(struct sftp_conn *conn, const char *local_path,
1982 const char *remote_path, int preserve_flag, int resume, int fsync_flag)
1985 u_int status = SSH2_FX_OK;
1988 off_t offset, progress_counter;
1989 u_char *handle, *data;
1992 Attrib a, *c = NULL;
1995 struct request *ack = NULL;
1996 struct requests acks;
1999 debug2_f("upload local \"%s\" to remote \"%s\"",
2000 local_path, remote_path);
2004 if ((local_fd = open(local_path, O_RDONLY)) == -1) {
2005 error("open local \"%s\": %s", local_path, strerror(errno));
2008 if (fstat(local_fd, &sb) == -1) {
2009 error("fstat local \"%s\": %s", local_path, strerror(errno));
2013 if (!S_ISREG(sb.st_mode)) {
2014 error("local \"%s\" is not a regular file", local_path);
2018 stat_to_attrib(&sb, &a);
2020 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
2021 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
2024 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
2027 /* Get remote file size if it exists */
2028 if ((c = do_stat(conn, remote_path, 0)) == NULL) {
2033 if ((off_t)c->size >= sb.st_size) {
2034 error("resume \"%s\": destination file "
2035 "same size or larger", local_path);
2040 if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
2046 /* Send open request */
2047 if (send_open(conn, remote_path, "dest", SSH2_FXF_WRITE|SSH2_FXF_CREAT|
2048 (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC),
2049 &a, &handle, &handle_len) != 0) {
2055 startid = ackid = id + 1;
2056 data = xmalloc(conn->upload_buflen);
2058 /* Read from local and write to remote */
2059 offset = progress_counter = (resume ? c->size : 0);
2061 start_progress_meter(progress_meter_path(local_path),
2062 sb.st_size, &progress_counter);
2065 if ((msg = sshbuf_new()) == NULL)
2066 fatal_f("sshbuf_new failed");
2071 * Can't use atomicio here because it returns 0 on EOF,
2072 * thus losing the last block of the file.
2073 * Simulate an EOF on interrupt, allowing ACKs from the
2076 if (interrupted || status != SSH2_FX_OK)
2079 len = read(local_fd, data, conn->upload_buflen);
2080 while ((len == -1) &&
2081 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
2084 fatal("read local \"%s\": %s",
2085 local_path, strerror(errno));
2086 } else if (len != 0) {
2087 ack = request_enqueue(&acks, ++id, len, offset);
2089 if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
2090 (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
2091 (r = sshbuf_put_string(msg, handle,
2092 handle_len)) != 0 ||
2093 (r = sshbuf_put_u64(msg, offset)) != 0 ||
2094 (r = sshbuf_put_string(msg, data, len)) != 0)
2095 fatal_fr(r, "compose");
2096 send_msg(conn, msg);
2097 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
2098 id, (unsigned long long)offset, len);
2099 } else if (TAILQ_FIRST(&acks) == NULL)
2103 fatal("Unexpected ACK %u", id);
2105 if (id == startid || len == 0 ||
2106 id - ackid >= conn->num_requests) {
2111 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
2112 (r = sshbuf_get_u32(msg, &rid)) != 0)
2113 fatal_fr(r, "parse");
2115 if (type != SSH2_FXP_STATUS)
2116 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
2117 "got %d", SSH2_FXP_STATUS, type);
2119 if ((r = sshbuf_get_u32(msg, &status)) != 0)
2120 fatal_fr(r, "parse status");
2121 debug3("SSH2_FXP_STATUS %u", status);
2123 /* Find the request in our queue */
2124 if ((ack = request_find(&acks, rid)) == NULL)
2125 fatal("Can't find request for ID %u", rid);
2126 TAILQ_REMOVE(&acks, ack, tq);
2127 debug3("In write loop, ack for %u %zu bytes at %lld",
2128 ack->id, ack->len, (unsigned long long)ack->offset);
2130 progress_counter += ack->len;
2135 fatal_f("offset < 0");
2140 stop_progress_meter();
2143 if (status != SSH2_FX_OK) {
2144 error("write remote \"%s\": %s", remote_path, fx2txt(status));
2145 status = SSH2_FX_FAILURE;
2148 if (close(local_fd) == -1) {
2149 error("close local \"%s\": %s", local_path, strerror(errno));
2150 status = SSH2_FX_FAILURE;
2153 /* Override umask and utimes if asked */
2155 do_fsetstat(conn, handle, handle_len, &a);
2158 (void)do_fsync(conn, handle, handle_len);
2160 if (do_close(conn, handle, handle_len) != 0)
2161 status = SSH2_FX_FAILURE;
2165 return status == SSH2_FX_OK ? 0 : -1;
2169 upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
2170 int depth, int preserve_flag, int print_flag, int resume, int fsync_flag,
2171 int follow_link_flag)
2176 char *filename, *new_src = NULL, *new_dst = NULL;
2178 Attrib a, *dirattrib;
2179 u_int32_t saved_perm;
2181 debug2_f("upload local dir \"%s\" to remote \"%s\"", src, dst);
2183 if (depth >= MAX_DIR_DEPTH) {
2184 error("Maximum directory depth exceeded: %d levels", depth);
2188 if (stat(src, &sb) == -1) {
2189 error("stat local \"%s\": %s", src, strerror(errno));
2192 if (!S_ISDIR(sb.st_mode)) {
2193 error("\"%s\" is not a directory", src);
2196 if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
2197 mprintf("Entering %s\n", src);
2199 stat_to_attrib(&sb, &a);
2200 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
2201 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
2204 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
2207 * sftp lacks a portable status value to match errno EEXIST,
2208 * so if we get a failure back then we must check whether
2209 * the path already existed and is a directory. Ensure we can
2210 * write to the directory we create for the duration of the transfer.
2212 saved_perm = a.perm;
2213 a.perm |= (S_IWUSR|S_IXUSR);
2214 if (do_mkdir(conn, dst, &a, 0) != 0) {
2215 if ((dirattrib = do_stat(conn, dst, 0)) == NULL)
2217 if (!S_ISDIR(dirattrib->perm)) {
2218 error("\"%s\" exists but is not a directory", dst);
2222 a.perm = saved_perm;
2224 if ((dirp = opendir(src)) == NULL) {
2225 error("local opendir \"%s\": %s", src, strerror(errno));
2229 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
2234 filename = dp->d_name;
2235 new_dst = path_append(dst, filename);
2236 new_src = path_append(src, filename);
2238 if (lstat(new_src, &sb) == -1) {
2239 logit("local lstat \"%s\": %s", filename,
2242 } else if (S_ISDIR(sb.st_mode)) {
2243 if (strcmp(filename, ".") == 0 ||
2244 strcmp(filename, "..") == 0)
2247 if (upload_dir_internal(conn, new_src, new_dst,
2248 depth + 1, preserve_flag, print_flag, resume,
2249 fsync_flag, follow_link_flag) == -1)
2251 } else if (S_ISREG(sb.st_mode) ||
2252 (follow_link_flag && S_ISLNK(sb.st_mode))) {
2253 if (do_upload(conn, new_src, new_dst,
2254 preserve_flag, resume, fsync_flag) == -1) {
2255 error("upload \"%s\" to \"%s\" failed",
2260 logit("%s: not a regular file", filename);
2265 do_setstat(conn, dst, &a);
2267 (void) closedir(dirp);
2272 upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
2273 int preserve_flag, int print_flag, int resume, int fsync_flag,
2274 int follow_link_flag)
2279 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
2280 error("upload \"%s\": path canonicalization failed", dst);
2284 ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
2285 print_flag, resume, fsync_flag, follow_link_flag);
2292 handle_dest_replies(struct sftp_conn *to, const char *to_path, int synchronous,
2293 u_int *nreqsp, u_int *write_errorp)
2301 if ((msg = sshbuf_new()) == NULL)
2302 fatal_f("sshbuf_new failed");
2304 /* Try to eat replies from the upload side */
2305 while (*nreqsp > 0) {
2306 debug3_f("%u outstanding replies", *nreqsp);
2308 /* Bail out if no data is ready to be read */
2310 pfd.events = POLLIN;
2311 if ((r = poll(&pfd, 1, 0)) == -1) {
2314 fatal_f("poll: %s", strerror(errno));
2316 break; /* fd not ready */
2321 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
2322 (r = sshbuf_get_u32(msg, &id)) != 0)
2323 fatal_fr(r, "dest parse");
2324 debug3("Received dest reply T:%u I:%u R:%u", type, id, *nreqsp);
2325 if (type != SSH2_FXP_STATUS) {
2326 fatal_f("Expected SSH2_FXP_STATUS(%d) packet, got %d",
2327 SSH2_FXP_STATUS, type);
2329 if ((r = sshbuf_get_u32(msg, &status)) != 0)
2330 fatal_fr(r, "parse dest status");
2331 debug3("dest SSH2_FXP_STATUS %u", status);
2332 if (status != SSH2_FX_OK) {
2333 /* record first error */
2334 if (*write_errorp == 0)
2335 *write_errorp = status;
2338 * XXX this doesn't do full reply matching like do_upload and
2339 * so cannot gracefully truncate terminated uploads at a
2340 * high-water mark. ATM the only caller of this function (scp)
2341 * doesn't support transfer resumption, so this doesn't matter
2344 * To be safe, do_crossload truncates the destination file to
2345 * zero length on upload failure, since we can't trust the
2346 * server not to have reordered replies that could have
2347 * inserted holes where none existed in the source file.
2349 * XXX we could get a more accutate progress bar if we updated
2350 * the counter based on the reply from the destination...
2354 debug3_f("done: %u outstanding replies", *nreqsp);
2359 do_crossload(struct sftp_conn *from, struct sftp_conn *to,
2360 const char *from_path, const char *to_path,
2361 Attrib *a, int preserve_flag)
2364 int write_error, read_error, r;
2365 u_int64_t offset = 0, size;
2366 u_int id, buflen, num_req, max_req, status = SSH2_FX_OK;
2367 u_int num_upload_req;
2368 off_t progress_counter;
2369 u_char *from_handle, *to_handle;
2370 size_t from_handle_len, to_handle_len;
2371 struct requests requests;
2372 struct request *req;
2375 debug2_f("crossload src \"%s\" to dst \"%s\"", from_path, to_path);
2377 TAILQ_INIT(&requests);
2379 if (a == NULL && (a = do_stat(from, from_path, 0)) == NULL)
2382 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
2383 (!S_ISREG(a->perm))) {
2384 error("download \"%s\": not a regular file", from_path);
2387 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
2392 buflen = from->download_buflen;
2393 if (buflen > to->upload_buflen)
2394 buflen = to->upload_buflen;
2396 /* Send open request to read side */
2397 if (send_open(from, from_path, "origin", SSH2_FXF_READ, NULL,
2398 &from_handle, &from_handle_len) != 0)
2401 /* Send open request to write side */
2402 a->flags &= ~SSH2_FILEXFER_ATTR_SIZE;
2403 a->flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
2406 a->flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
2407 if (send_open(to, to_path, "dest",
2408 SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC, a,
2409 &to_handle, &to_handle_len) != 0) {
2410 do_close(from, from_handle, from_handle_len);
2414 /* Read from remote "from" and write to remote "to" */
2416 write_error = read_error = num_req = num_upload_req = 0;
2418 progress_counter = 0;
2420 if (showprogress && size != 0) {
2421 start_progress_meter(progress_meter_path(from_path),
2422 size, &progress_counter);
2424 if ((msg = sshbuf_new()) == NULL)
2425 fatal_f("sshbuf_new failed");
2426 while (num_req > 0 || max_req > 0) {
2431 * Simulate EOF on interrupt: stop sending new requests and
2432 * allow outstanding requests to drain gracefully
2435 if (num_req == 0) /* If we haven't started yet... */
2440 /* Send some more requests */
2441 while (num_req < max_req) {
2442 debug3("Request range %llu -> %llu (%d/%d)",
2443 (unsigned long long)offset,
2444 (unsigned long long)offset + buflen - 1,
2446 req = request_enqueue(&requests, from->msg_id++,
2450 send_read_request(from, req->id, req->offset,
2451 req->len, from_handle, from_handle_len);
2454 /* Try to eat replies from the upload side (nonblocking) */
2455 handle_dest_replies(to, to_path, 0,
2456 &num_upload_req, &write_error);
2460 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
2461 (r = sshbuf_get_u32(msg, &id)) != 0)
2462 fatal_fr(r, "parse");
2463 debug3("Received origin reply T:%u I:%u R:%d",
2466 /* Find the request in our queue */
2467 if ((req = request_find(&requests, id)) == NULL)
2468 fatal("Unexpected reply %u", id);
2471 case SSH2_FXP_STATUS:
2472 if ((r = sshbuf_get_u32(msg, &status)) != 0)
2473 fatal_fr(r, "parse status");
2474 if (status != SSH2_FX_EOF)
2477 TAILQ_REMOVE(&requests, req, tq);
2482 if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
2483 fatal_fr(r, "parse data");
2484 debug3("Received data %llu -> %llu",
2485 (unsigned long long)req->offset,
2486 (unsigned long long)req->offset + len - 1);
2488 fatal("Received more data than asked for "
2489 "%zu > %zu", len, req->len);
2491 /* Write this chunk out to the destination */
2493 if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
2494 (r = sshbuf_put_u32(msg, to->msg_id++)) != 0 ||
2495 (r = sshbuf_put_string(msg, to_handle,
2496 to_handle_len)) != 0 ||
2497 (r = sshbuf_put_u64(msg, req->offset)) != 0 ||
2498 (r = sshbuf_put_string(msg, data, len)) != 0)
2499 fatal_fr(r, "compose write");
2501 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%zu",
2502 id, (unsigned long long)offset, len);
2504 progress_counter += len;
2507 if (len == req->len) {
2508 TAILQ_REMOVE(&requests, req, tq);
2512 /* Resend the request for the missing data */
2513 debug3("Short data block, re-requesting "
2514 "%llu -> %llu (%2d)",
2515 (unsigned long long)req->offset + len,
2516 (unsigned long long)req->offset +
2517 req->len - 1, num_req);
2518 req->id = from->msg_id++;
2521 send_read_request(from, req->id,
2522 req->offset, req->len,
2523 from_handle, from_handle_len);
2524 /* Reduce the request size */
2526 buflen = MAXIMUM(MIN_READ_SIZE, len);
2528 if (max_req > 0) { /* max_req = 0 iff EOF received */
2529 if (size > 0 && offset > size) {
2530 /* Only one request at a time
2531 * after the expected EOF */
2532 debug3("Finish at %llu (%2d)",
2533 (unsigned long long)offset,
2536 } else if (max_req < from->num_requests) {
2542 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
2543 SSH2_FXP_DATA, type);
2547 if (showprogress && size)
2548 stop_progress_meter();
2550 /* Drain replies from the server (blocking) */
2551 debug3_f("waiting for %u replies from destination", num_upload_req);
2552 handle_dest_replies(to, to_path, 1, &num_upload_req, &write_error);
2555 if (TAILQ_FIRST(&requests) != NULL)
2556 fatal("Transfer complete, but requests still in queue");
2557 /* Truncate at 0 length on interrupt or error to avoid holes at dest */
2558 if (read_error || write_error || interrupted) {
2559 debug("truncating \"%s\" at 0", to_path);
2560 do_close(to, to_handle, to_handle_len);
2562 if (send_open(to, to_path, "dest",
2563 SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC, a,
2564 &to_handle, &to_handle_len) != 0) {
2565 error("dest truncate \"%s\" failed", to_path);
2570 error("read origin \"%s\": %s", from_path, fx2txt(status));
2572 do_close(from, from_handle, from_handle_len);
2573 if (to_handle != NULL)
2574 do_close(to, to_handle, to_handle_len);
2575 } else if (write_error) {
2576 error("write dest \"%s\": %s", to_path, fx2txt(write_error));
2577 status = SSH2_FX_FAILURE;
2578 do_close(from, from_handle, from_handle_len);
2579 if (to_handle != NULL)
2580 do_close(to, to_handle, to_handle_len);
2582 if (do_close(from, from_handle, from_handle_len) != 0 ||
2586 status = SSH2_FX_OK;
2587 if (to_handle != NULL) {
2588 /* Need to resend utimes after write */
2590 do_fsetstat(to, to_handle, to_handle_len, a);
2591 do_close(to, to_handle, to_handle_len);
2598 return status == SSH2_FX_OK ? 0 : -1;
2602 crossload_dir_internal(struct sftp_conn *from, struct sftp_conn *to,
2603 const char *from_path, const char *to_path,
2604 int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
2605 int follow_link_flag)
2608 SFTP_DIRENT **dir_entries;
2609 char *filename, *new_from_path = NULL, *new_to_path = NULL;
2613 debug2_f("crossload dir src \"%s\" to dst \"%s\"", from_path, to_path);
2615 if (depth >= MAX_DIR_DEPTH) {
2616 error("Maximum directory depth exceeded: %d levels", depth);
2620 if (dirattrib == NULL &&
2621 (dirattrib = do_stat(from, from_path, 1)) == NULL) {
2622 error("stat remote \"%s\" failed", from_path);
2625 if (!S_ISDIR(dirattrib->perm)) {
2626 error("\"%s\" is not a directory", from_path);
2629 if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
2630 mprintf("Retrieving %s\n", from_path);
2632 curdir = *dirattrib; /* dirattrib will be clobbered */
2633 curdir.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
2634 curdir.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
2635 if ((curdir.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) == 0) {
2636 debug("Origin did not send permissions for "
2637 "directory \"%s\"", to_path);
2638 curdir.perm = S_IWUSR|S_IXUSR;
2639 curdir.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
2641 /* We need to be able to write to the directory while we transfer it */
2642 mode = curdir.perm & 01777;
2643 curdir.perm = mode | (S_IWUSR|S_IXUSR);
2646 * sftp lacks a portable status value to match errno EEXIST,
2647 * so if we get a failure back then we must check whether
2648 * the path already existed and is a directory. Ensure we can
2649 * write to the directory we create for the duration of the transfer.
2651 if (do_mkdir(to, to_path, &curdir, 0) != 0) {
2652 if ((dirattrib = do_stat(to, to_path, 0)) == NULL)
2654 if (!S_ISDIR(dirattrib->perm)) {
2655 error("\"%s\" exists but is not a directory", to_path);
2661 if (do_readdir(from, from_path, &dir_entries) == -1) {
2662 error("origin readdir \"%s\" failed", from_path);
2666 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
2667 free(new_from_path);
2670 filename = dir_entries[i]->filename;
2671 new_from_path = path_append(from_path, filename);
2672 new_to_path = path_append(to_path, filename);
2674 if (S_ISDIR(dir_entries[i]->a.perm)) {
2675 if (strcmp(filename, ".") == 0 ||
2676 strcmp(filename, "..") == 0)
2678 if (crossload_dir_internal(from, to,
2679 new_from_path, new_to_path,
2680 depth + 1, &(dir_entries[i]->a), preserve_flag,
2681 print_flag, follow_link_flag) == -1)
2683 } else if (S_ISREG(dir_entries[i]->a.perm) ||
2684 (follow_link_flag && S_ISLNK(dir_entries[i]->a.perm))) {
2686 * If this is a symlink then don't send the link's
2687 * Attrib. do_download() will do a FXP_STAT operation
2688 * and get the link target's attributes.
2690 if (do_crossload(from, to, new_from_path, new_to_path,
2691 S_ISLNK(dir_entries[i]->a.perm) ? NULL :
2692 &(dir_entries[i]->a), preserve_flag) == -1) {
2693 error("crossload \"%s\" to \"%s\" failed",
2694 new_from_path, new_to_path);
2698 logit("origin \"%s\": not a regular file",
2703 free(new_from_path);
2705 do_setstat(to, to_path, &curdir);
2707 free_sftp_dirents(dir_entries);
2713 crossload_dir(struct sftp_conn *from, struct sftp_conn *to,
2714 const char *from_path, const char *to_path,
2715 Attrib *dirattrib, int preserve_flag, int print_flag, int follow_link_flag)
2717 char *from_path_canon;
2720 if ((from_path_canon = do_realpath(from, from_path)) == NULL) {
2721 error("crossload \"%s\": path canonicalization failed",
2726 ret = crossload_dir_internal(from, to, from_path_canon, to_path, 0,
2727 dirattrib, preserve_flag, print_flag, follow_link_flag);
2728 free(from_path_canon);
2733 path_append(const char *p1, const char *p2)
2736 size_t len = strlen(p1) + strlen(p2) + 2;
2739 strlcpy(ret, p1, len);
2740 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
2741 strlcat(ret, "/", len);
2742 strlcat(ret, p2, len);
2748 make_absolute(char *p, const char *pwd)
2753 if (p && !path_absolute(p)) {
2754 abs_str = path_append(pwd, p);
2762 remote_is_dir(struct sftp_conn *conn, const char *path)
2766 /* XXX: report errors? */
2767 if ((a = do_stat(conn, path, 1)) == NULL)
2769 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
2771 return(S_ISDIR(a->perm));
2776 local_is_dir(const char *path)
2780 /* XXX: report errors? */
2781 if (stat(path, &sb) == -1)
2784 return(S_ISDIR(sb.st_mode));
2787 /* Check whether path returned from glob(..., GLOB_MARK, ...) is a directory */
2789 globpath_is_dir(const char *pathname)
2791 size_t l = strlen(pathname);
2793 return l > 0 && pathname[l - 1] == '/';