1 /* $OpenBSD: sftp-client.c,v 1.133 2019/01/24 16:52:17 dtucker 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>
53 #include "progressmeter.h"
58 #include "sftp-common.h"
59 #include "sftp-client.h"
61 extern volatile sig_atomic_t interrupted;
62 extern int showprogress;
64 /* Minimum amount of data to read at a time */
65 #define MIN_READ_SIZE 512
67 /* Maximum depth to descend in directory trees */
68 #define MAX_DIR_DEPTH 64
70 /* Directory separator characters */
72 # define SFTP_DIRECTORY_CHARS "/\\"
73 #else /* HAVE_CYGWIN */
74 # define SFTP_DIRECTORY_CHARS "/"
75 #endif /* HAVE_CYGWIN */
80 u_int transfer_buflen;
84 #define SFTP_EXT_POSIX_RENAME 0x00000001
85 #define SFTP_EXT_STATVFS 0x00000002
86 #define SFTP_EXT_FSTATVFS 0x00000004
87 #define SFTP_EXT_HARDLINK 0x00000008
88 #define SFTP_EXT_FSYNC 0x00000010
89 #define SFTP_EXT_LSETSTAT 0x00000020
92 struct bwlimit bwlimit_in, bwlimit_out;
96 get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
97 const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
101 sftpio(void *_bwlimit, size_t amount)
103 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
105 refresh_progress_meter(0);
107 bandwidth_limit(bwlimit, amount);
112 send_msg(struct sftp_conn *conn, struct sshbuf *m)
117 if (sshbuf_len(m) > SFTP_MAX_MSG_LENGTH)
118 fatal("Outbound message too long %zu", sshbuf_len(m));
120 /* Send length first */
121 put_u32(mlen, sshbuf_len(m));
122 iov[0].iov_base = mlen;
123 iov[0].iov_len = sizeof(mlen);
124 iov[1].iov_base = (u_char *)sshbuf_ptr(m);
125 iov[1].iov_len = sshbuf_len(m);
127 if (atomiciov6(writev, conn->fd_out, iov, 2, sftpio,
128 conn->limit_kbps > 0 ? &conn->bwlimit_out : NULL) !=
129 sshbuf_len(m) + sizeof(mlen))
130 fatal("Couldn't send packet: %s", strerror(errno));
136 get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
142 if ((r = sshbuf_reserve(m, 4, &p)) != 0)
143 fatal("%s: buffer error: %s", __func__, ssh_err(r));
144 if (atomicio6(read, conn->fd_in, p, 4, sftpio,
145 conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL) != 4) {
146 if (errno == EPIPE || errno == ECONNRESET)
147 fatal("Connection closed");
149 fatal("Couldn't read packet: %s", strerror(errno));
152 if ((r = sshbuf_get_u32(m, &msg_len)) != 0)
153 fatal("%s: buffer error: %s", __func__, ssh_err(r));
154 if (msg_len > SFTP_MAX_MSG_LENGTH) {
155 do_log2(initial ? SYSLOG_LEVEL_ERROR : SYSLOG_LEVEL_FATAL,
156 "Received message too long %u", msg_len);
157 fatal("Ensure the remote shell produces no output "
158 "for non-interactive sessions.");
161 if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
162 fatal("%s: buffer error: %s", __func__, ssh_err(r));
163 if (atomicio6(read, conn->fd_in, p, msg_len, sftpio,
164 conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL)
167 fatal("Connection closed");
169 fatal("Read packet: %s", strerror(errno));
174 get_msg(struct sftp_conn *conn, struct sshbuf *m)
176 get_msg_extended(conn, m, 0);
180 send_string_request(struct sftp_conn *conn, u_int id, u_int code, const char *s,
186 if ((msg = sshbuf_new()) == NULL)
187 fatal("%s: sshbuf_new failed", __func__);
188 if ((r = sshbuf_put_u8(msg, code)) != 0 ||
189 (r = sshbuf_put_u32(msg, id)) != 0 ||
190 (r = sshbuf_put_string(msg, s, len)) != 0)
191 fatal("%s: buffer error: %s", __func__, ssh_err(r));
193 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
198 send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
199 const void *s, u_int len, Attrib *a)
204 if ((msg = sshbuf_new()) == NULL)
205 fatal("%s: sshbuf_new failed", __func__);
206 if ((r = sshbuf_put_u8(msg, code)) != 0 ||
207 (r = sshbuf_put_u32(msg, id)) != 0 ||
208 (r = sshbuf_put_string(msg, s, len)) != 0 ||
209 (r = encode_attrib(msg, a)) != 0)
210 fatal("%s: buffer error: %s", __func__, ssh_err(r));
212 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
217 get_status(struct sftp_conn *conn, u_int expected_id)
224 if ((msg = sshbuf_new()) == NULL)
225 fatal("%s: sshbuf_new failed", __func__);
227 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
228 (r = sshbuf_get_u32(msg, &id)) != 0)
229 fatal("%s: buffer error: %s", __func__, ssh_err(r));
231 if (id != expected_id)
232 fatal("ID mismatch (%u != %u)", id, expected_id);
233 if (type != SSH2_FXP_STATUS)
234 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
235 SSH2_FXP_STATUS, type);
237 if ((r = sshbuf_get_u32(msg, &status)) != 0)
238 fatal("%s: buffer error: %s", __func__, ssh_err(r));
241 debug3("SSH2_FXP_STATUS %u", status);
247 get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
248 const char *errfmt, ...)
258 va_start(args, errfmt);
260 vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
263 if ((msg = sshbuf_new()) == NULL)
264 fatal("%s: sshbuf_new failed", __func__);
266 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
267 (r = sshbuf_get_u32(msg, &id)) != 0)
268 fatal("%s: buffer error: %s", __func__, ssh_err(r));
270 if (id != expected_id)
271 fatal("%s: ID mismatch (%u != %u)",
272 errfmt == NULL ? __func__ : errmsg, id, expected_id);
273 if (type == SSH2_FXP_STATUS) {
274 if ((r = sshbuf_get_u32(msg, &status)) != 0)
275 fatal("%s: buffer error: %s", __func__, ssh_err(r));
277 error("%s: %s", errmsg, fx2txt(status));
280 } else if (type != SSH2_FXP_HANDLE)
281 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
282 errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
284 if ((r = sshbuf_get_string(msg, &handle, len)) != 0)
285 fatal("%s: buffer error: %s", __func__, ssh_err(r));
292 get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
300 if ((msg = sshbuf_new()) == NULL)
301 fatal("%s: sshbuf_new failed", __func__);
304 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
305 (r = sshbuf_get_u32(msg, &id)) != 0)
306 fatal("%s: buffer error: %s", __func__, ssh_err(r));
308 debug3("Received stat reply T:%u I:%u", type, id);
309 if (id != expected_id)
310 fatal("ID mismatch (%u != %u)", id, expected_id);
311 if (type == SSH2_FXP_STATUS) {
314 if ((r = sshbuf_get_u32(msg, &status)) != 0)
315 fatal("%s: buffer error: %s", __func__, ssh_err(r));
317 debug("Couldn't stat remote file: %s", fx2txt(status));
319 error("Couldn't stat remote file: %s", fx2txt(status));
322 } else if (type != SSH2_FXP_ATTRS) {
323 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
324 SSH2_FXP_ATTRS, type);
326 if ((r = decode_attrib(msg, &a)) != 0) {
327 error("%s: couldn't decode attrib: %s", __func__, ssh_err(r));
337 get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
338 u_int expected_id, int quiet)
346 if ((msg = sshbuf_new()) == NULL)
347 fatal("%s: sshbuf_new failed", __func__);
350 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
351 (r = sshbuf_get_u32(msg, &id)) != 0)
352 fatal("%s: buffer error: %s", __func__, ssh_err(r));
354 debug3("Received statvfs reply T:%u I:%u", type, id);
355 if (id != expected_id)
356 fatal("ID mismatch (%u != %u)", id, expected_id);
357 if (type == SSH2_FXP_STATUS) {
360 if ((r = sshbuf_get_u32(msg, &status)) != 0)
361 fatal("%s: buffer error: %s", __func__, ssh_err(r));
363 debug("Couldn't statvfs: %s", fx2txt(status));
365 error("Couldn't statvfs: %s", fx2txt(status));
368 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
369 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
370 SSH2_FXP_EXTENDED_REPLY, type);
373 memset(st, 0, sizeof(*st));
374 if ((r = sshbuf_get_u64(msg, &st->f_bsize)) != 0 ||
375 (r = sshbuf_get_u64(msg, &st->f_frsize)) != 0 ||
376 (r = sshbuf_get_u64(msg, &st->f_blocks)) != 0 ||
377 (r = sshbuf_get_u64(msg, &st->f_bfree)) != 0 ||
378 (r = sshbuf_get_u64(msg, &st->f_bavail)) != 0 ||
379 (r = sshbuf_get_u64(msg, &st->f_files)) != 0 ||
380 (r = sshbuf_get_u64(msg, &st->f_ffree)) != 0 ||
381 (r = sshbuf_get_u64(msg, &st->f_favail)) != 0 ||
382 (r = sshbuf_get_u64(msg, &st->f_fsid)) != 0 ||
383 (r = sshbuf_get_u64(msg, &flag)) != 0 ||
384 (r = sshbuf_get_u64(msg, &st->f_namemax)) != 0)
385 fatal("%s: buffer error: %s", __func__, ssh_err(r));
387 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
388 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
396 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
397 u_int64_t limit_kbps)
401 struct sftp_conn *ret;
404 ret = xcalloc(1, sizeof(*ret));
407 ret->fd_out = fd_out;
408 ret->transfer_buflen = transfer_buflen;
409 ret->num_requests = num_requests;
413 if ((msg = sshbuf_new()) == NULL)
414 fatal("%s: sshbuf_new failed", __func__);
415 if ((r = sshbuf_put_u8(msg, SSH2_FXP_INIT)) != 0 ||
416 (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0)
417 fatal("%s: buffer error: %s", __func__, ssh_err(r));
422 get_msg_extended(ret, msg, 1);
424 /* Expecting a VERSION reply */
425 if ((r = sshbuf_get_u8(msg, &type)) != 0)
426 fatal("%s: buffer error: %s", __func__, ssh_err(r));
427 if (type != SSH2_FXP_VERSION) {
428 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
434 if ((r = sshbuf_get_u32(msg, &ret->version)) != 0)
435 fatal("%s: buffer error: %s", __func__, ssh_err(r));
437 debug2("Remote version: %u", ret->version);
439 /* Check for extensions */
440 while (sshbuf_len(msg) > 0) {
446 if ((r = sshbuf_get_cstring(msg, &name, NULL)) != 0 ||
447 (r = sshbuf_get_string(msg, &value, &vlen)) != 0)
448 fatal("%s: buffer error: %s", __func__, ssh_err(r));
449 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
450 strcmp((char *)value, "1") == 0) {
451 ret->exts |= SFTP_EXT_POSIX_RENAME;
453 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
454 strcmp((char *)value, "2") == 0) {
455 ret->exts |= SFTP_EXT_STATVFS;
457 } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
458 strcmp((char *)value, "2") == 0) {
459 ret->exts |= SFTP_EXT_FSTATVFS;
461 } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
462 strcmp((char *)value, "1") == 0) {
463 ret->exts |= SFTP_EXT_HARDLINK;
465 } else if (strcmp(name, "fsync@openssh.com") == 0 &&
466 strcmp((char *)value, "1") == 0) {
467 ret->exts |= SFTP_EXT_FSYNC;
469 } else if (strcmp(name, "lsetstat@openssh.com") == 0 &&
470 strcmp((char *)value, "1") == 0) {
471 ret->exts |= SFTP_EXT_LSETSTAT;
475 debug2("Server supports extension \"%s\" revision %s",
478 debug2("Unrecognised server extension \"%s\"", name);
486 /* Some filexfer v.0 servers don't support large packets */
487 if (ret->version == 0)
488 ret->transfer_buflen = MINIMUM(ret->transfer_buflen, 20480);
490 ret->limit_kbps = limit_kbps;
491 if (ret->limit_kbps > 0) {
492 bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
493 ret->transfer_buflen);
494 bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
495 ret->transfer_buflen);
502 sftp_proto_version(struct sftp_conn *conn)
504 return conn->version;
508 do_close(struct sftp_conn *conn, const u_char *handle, u_int handle_len)
514 if ((msg = sshbuf_new()) == NULL)
515 fatal("%s: sshbuf_new failed", __func__);
518 if ((r = sshbuf_put_u8(msg, SSH2_FXP_CLOSE)) != 0 ||
519 (r = sshbuf_put_u32(msg, id)) != 0 ||
520 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
521 fatal("%s: buffer error: %s", __func__, ssh_err(r));
523 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
525 status = get_status(conn, id);
526 if (status != SSH2_FX_OK)
527 error("Couldn't close file: %s", fx2txt(status));
531 return status == SSH2_FX_OK ? 0 : -1;
536 do_lsreaddir(struct sftp_conn *conn, const char *path, int print_flag,
540 u_int count, id, i, expected_id, ents = 0;
542 u_char type, *handle;
543 int status = SSH2_FX_FAILURE;
551 if ((msg = sshbuf_new()) == NULL)
552 fatal("%s: sshbuf_new failed", __func__);
553 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPENDIR)) != 0 ||
554 (r = sshbuf_put_u32(msg, id)) != 0 ||
555 (r = sshbuf_put_cstring(msg, path)) != 0)
556 fatal("%s: buffer error: %s", __func__, ssh_err(r));
559 handle = get_handle(conn, id, &handle_len,
560 "remote readdir(\"%s\")", path);
561 if (handle == NULL) {
568 *dir = xcalloc(1, sizeof(**dir));
572 for (; !interrupted;) {
573 id = expected_id = conn->msg_id++;
575 debug3("Sending SSH2_FXP_READDIR I:%u", id);
578 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READDIR)) != 0 ||
579 (r = sshbuf_put_u32(msg, id)) != 0 ||
580 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
581 fatal("%s: buffer error: %s", __func__, ssh_err(r));
588 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
589 (r = sshbuf_get_u32(msg, &id)) != 0)
590 fatal("%s: buffer error: %s", __func__, ssh_err(r));
592 debug3("Received reply T:%u I:%u", type, id);
594 if (id != expected_id)
595 fatal("ID mismatch (%u != %u)", id, expected_id);
597 if (type == SSH2_FXP_STATUS) {
600 if ((r = sshbuf_get_u32(msg, &rstatus)) != 0)
601 fatal("%s: buffer error: %s",
602 __func__, ssh_err(r));
603 debug3("Received SSH2_FXP_STATUS %d", rstatus);
604 if (rstatus == SSH2_FX_EOF)
606 error("Couldn't read directory: %s", fx2txt(rstatus));
608 } else if (type != SSH2_FXP_NAME)
609 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
610 SSH2_FXP_NAME, type);
612 if ((r = sshbuf_get_u32(msg, &count)) != 0)
613 fatal("%s: buffer error: %s", __func__, ssh_err(r));
614 if (count > SSHBUF_SIZE_MAX)
615 fatal("%s: nonsensical number of entries", __func__);
618 debug3("Received %d SSH2_FXP_NAME responses", count);
619 for (i = 0; i < count; i++) {
620 char *filename, *longname;
623 if ((r = sshbuf_get_cstring(msg, &filename,
625 (r = sshbuf_get_cstring(msg, &longname,
627 fatal("%s: buffer error: %s",
628 __func__, ssh_err(r));
629 if ((r = decode_attrib(msg, &a)) != 0) {
630 error("%s: couldn't decode attrib: %s",
631 __func__, ssh_err(r));
639 mprintf("%s\n", longname);
642 * Directory entries should never contain '/'
643 * These can be used to attack recursive ops
644 * (e.g. send '../../../../etc/passwd')
646 if (strpbrk(filename, SFTP_DIRECTORY_CHARS) != NULL) {
647 error("Server sent suspect path \"%s\" "
648 "during readdir of \"%s\"", filename, path);
650 *dir = xreallocarray(*dir, ents + 2, sizeof(**dir));
651 (*dir)[ents] = xcalloc(1, sizeof(***dir));
652 (*dir)[ents]->filename = xstrdup(filename);
653 (*dir)[ents]->longname = xstrdup(longname);
654 memcpy(&(*dir)[ents]->a, &a, sizeof(a));
655 (*dir)[++ents] = NULL;
665 do_close(conn, handle, handle_len);
668 if (status != 0 && dir != NULL) {
669 /* Don't return results on error */
670 free_sftp_dirents(*dir);
672 } else if (interrupted && dir != NULL && *dir != NULL) {
673 /* Don't return partial matches on interrupt */
674 free_sftp_dirents(*dir);
675 *dir = xcalloc(1, sizeof(**dir));
679 return status == SSH2_FX_OK ? 0 : -1;
683 do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir)
685 return(do_lsreaddir(conn, path, 0, dir));
688 void free_sftp_dirents(SFTP_DIRENT **s)
694 for (i = 0; s[i]; i++) {
695 free(s[i]->filename);
696 free(s[i]->longname);
703 do_rm(struct sftp_conn *conn, const char *path)
707 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
710 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
711 status = get_status(conn, id);
712 if (status != SSH2_FX_OK)
713 error("Couldn't delete file: %s", fx2txt(status));
714 return status == SSH2_FX_OK ? 0 : -1;
718 do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag)
723 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
726 status = get_status(conn, id);
727 if (status != SSH2_FX_OK && print_flag)
728 error("Couldn't create directory: %s", fx2txt(status));
730 return status == SSH2_FX_OK ? 0 : -1;
734 do_rmdir(struct sftp_conn *conn, const char *path)
739 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
742 status = get_status(conn, id);
743 if (status != SSH2_FX_OK)
744 error("Couldn't remove directory: %s", fx2txt(status));
746 return status == SSH2_FX_OK ? 0 : -1;
750 do_stat(struct sftp_conn *conn, const char *path, int quiet)
756 send_string_request(conn, id,
757 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
760 return(get_decode_stat(conn, id, quiet));
764 do_lstat(struct sftp_conn *conn, const char *path, int quiet)
768 if (conn->version == 0) {
770 debug("Server version does not support lstat operation");
772 logit("Server version does not support lstat operation");
773 return(do_stat(conn, path, quiet));
777 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
780 return(get_decode_stat(conn, id, quiet));
785 do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
791 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
794 return(get_decode_stat(conn, id, quiet));
799 do_setstat(struct sftp_conn *conn, const char *path, Attrib *a)
804 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
807 status = get_status(conn, id);
808 if (status != SSH2_FX_OK)
809 error("Couldn't setstat on \"%s\": %s", path,
812 return status == SSH2_FX_OK ? 0 : -1;
816 do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
822 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
825 status = get_status(conn, id);
826 if (status != SSH2_FX_OK)
827 error("Couldn't fsetstat: %s", fx2txt(status));
829 return status == SSH2_FX_OK ? 0 : -1;
833 do_realpath(struct sftp_conn *conn, const char *path)
836 u_int expected_id, count, id;
837 char *filename, *longname;
842 expected_id = id = conn->msg_id++;
843 send_string_request(conn, id, SSH2_FXP_REALPATH, path,
846 if ((msg = sshbuf_new()) == NULL)
847 fatal("%s: sshbuf_new failed", __func__);
850 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
851 (r = sshbuf_get_u32(msg, &id)) != 0)
852 fatal("%s: buffer error: %s", __func__, ssh_err(r));
854 if (id != expected_id)
855 fatal("ID mismatch (%u != %u)", id, expected_id);
857 if (type == SSH2_FXP_STATUS) {
860 if ((r = sshbuf_get_u32(msg, &status)) != 0)
861 fatal("%s: buffer error: %s", __func__, ssh_err(r));
862 error("Couldn't canonicalize: %s", fx2txt(status));
865 } else if (type != SSH2_FXP_NAME)
866 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
867 SSH2_FXP_NAME, type);
869 if ((r = sshbuf_get_u32(msg, &count)) != 0)
870 fatal("%s: buffer error: %s", __func__, ssh_err(r));
872 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
874 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
875 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
876 (r = decode_attrib(msg, &a)) != 0)
877 fatal("%s: buffer error: %s", __func__, ssh_err(r));
879 debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename,
880 (unsigned long)a.size);
890 do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath,
895 int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
897 if ((msg = sshbuf_new()) == NULL)
898 fatal("%s: sshbuf_new failed", __func__);
900 /* Send rename request */
903 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
904 (r = sshbuf_put_u32(msg, id)) != 0 ||
905 (r = sshbuf_put_cstring(msg,
906 "posix-rename@openssh.com")) != 0)
907 fatal("%s: buffer error: %s", __func__, ssh_err(r));
909 if ((r = sshbuf_put_u8(msg, SSH2_FXP_RENAME)) != 0 ||
910 (r = sshbuf_put_u32(msg, id)) != 0)
911 fatal("%s: buffer error: %s", __func__, ssh_err(r));
913 if ((r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
914 (r = sshbuf_put_cstring(msg, newpath)) != 0)
915 fatal("%s: buffer error: %s", __func__, ssh_err(r));
917 debug3("Sent message %s \"%s\" -> \"%s\"",
918 use_ext ? "posix-rename@openssh.com" :
919 "SSH2_FXP_RENAME", oldpath, newpath);
922 status = get_status(conn, id);
923 if (status != SSH2_FX_OK)
924 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
925 newpath, fx2txt(status));
927 return status == SSH2_FX_OK ? 0 : -1;
931 do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
937 if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
938 error("Server does not support hardlink@openssh.com extension");
942 if ((msg = sshbuf_new()) == NULL)
943 fatal("%s: sshbuf_new failed", __func__);
945 /* Send link request */
947 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
948 (r = sshbuf_put_u32(msg, id)) != 0 ||
949 (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
950 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
951 (r = sshbuf_put_cstring(msg, newpath)) != 0)
952 fatal("%s: buffer error: %s", __func__, ssh_err(r));
954 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
958 status = get_status(conn, id);
959 if (status != SSH2_FX_OK)
960 error("Couldn't link file \"%s\" to \"%s\": %s", oldpath,
961 newpath, fx2txt(status));
963 return status == SSH2_FX_OK ? 0 : -1;
967 do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
973 if (conn->version < 3) {
974 error("This server does not support the symlink operation");
975 return(SSH2_FX_OP_UNSUPPORTED);
978 if ((msg = sshbuf_new()) == NULL)
979 fatal("%s: sshbuf_new failed", __func__);
981 /* Send symlink request */
983 if ((r = sshbuf_put_u8(msg, SSH2_FXP_SYMLINK)) != 0 ||
984 (r = sshbuf_put_u32(msg, id)) != 0 ||
985 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
986 (r = sshbuf_put_cstring(msg, newpath)) != 0)
987 fatal("%s: buffer error: %s", __func__, ssh_err(r));
989 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
993 status = get_status(conn, id);
994 if (status != SSH2_FX_OK)
995 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
996 newpath, fx2txt(status));
998 return status == SSH2_FX_OK ? 0 : -1;
1002 do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len)
1008 /* Silently return if the extension is not supported */
1009 if ((conn->exts & SFTP_EXT_FSYNC) == 0)
1012 /* Send fsync request */
1013 if ((msg = sshbuf_new()) == NULL)
1014 fatal("%s: sshbuf_new failed", __func__);
1015 id = conn->msg_id++;
1016 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1017 (r = sshbuf_put_u32(msg, id)) != 0 ||
1018 (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
1019 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1020 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1021 send_msg(conn, msg);
1022 debug3("Sent message fsync@openssh.com I:%u", id);
1025 status = get_status(conn, id);
1026 if (status != SSH2_FX_OK)
1027 error("Couldn't sync file: %s", fx2txt(status));
1029 return status == SSH2_FX_OK ? 0 : -1;
1034 do_readlink(struct sftp_conn *conn, const char *path)
1037 u_int expected_id, count, id;
1038 char *filename, *longname;
1043 expected_id = id = conn->msg_id++;
1044 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
1046 if ((msg = sshbuf_new()) == NULL)
1047 fatal("%s: sshbuf_new failed", __func__);
1050 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1051 (r = sshbuf_get_u32(msg, &id)) != 0)
1052 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1054 if (id != expected_id)
1055 fatal("ID mismatch (%u != %u)", id, expected_id);
1057 if (type == SSH2_FXP_STATUS) {
1060 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1061 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1062 error("Couldn't readlink: %s", fx2txt(status));
1065 } else if (type != SSH2_FXP_NAME)
1066 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1067 SSH2_FXP_NAME, type);
1069 if ((r = sshbuf_get_u32(msg, &count)) != 0)
1070 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1072 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
1074 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
1075 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
1076 (r = decode_attrib(msg, &a)) != 0)
1077 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1079 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
1090 do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
1097 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
1098 error("Server does not support statvfs@openssh.com extension");
1102 id = conn->msg_id++;
1104 if ((msg = sshbuf_new()) == NULL)
1105 fatal("%s: sshbuf_new failed", __func__);
1106 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1107 (r = sshbuf_put_u32(msg, id)) != 0 ||
1108 (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
1109 (r = sshbuf_put_cstring(msg, path)) != 0)
1110 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1111 send_msg(conn, msg);
1114 return get_decode_statvfs(conn, st, id, quiet);
1119 do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
1120 struct sftp_statvfs *st, int quiet)
1125 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
1126 error("Server does not support fstatvfs@openssh.com extension");
1130 id = conn->msg_id++;
1132 if ((msg = sshbuf_new()) == NULL)
1133 fatal("%s: sshbuf_new failed", __func__);
1134 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1135 (r = sshbuf_put_u32(msg, id)) != 0 ||
1136 (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
1137 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1138 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1139 send_msg(conn, msg);
1142 return get_decode_statvfs(conn, st, id, quiet);
1147 do_lsetstat(struct sftp_conn *conn, const char *path, Attrib *a)
1153 if ((conn->exts & SFTP_EXT_LSETSTAT) == 0) {
1154 error("Server does not support lsetstat@openssh.com extension");
1158 id = conn->msg_id++;
1159 if ((msg = sshbuf_new()) == NULL)
1160 fatal("%s: sshbuf_new failed", __func__);
1161 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1162 (r = sshbuf_put_u32(msg, id)) != 0 ||
1163 (r = sshbuf_put_cstring(msg, "lsetstat@openssh.com")) != 0 ||
1164 (r = sshbuf_put_cstring(msg, path)) != 0 ||
1165 (r = encode_attrib(msg, a)) != 0)
1166 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1167 send_msg(conn, msg);
1170 status = get_status(conn, id);
1171 if (status != SSH2_FX_OK)
1172 error("Couldn't setstat on \"%s\": %s", path,
1175 return status == SSH2_FX_OK ? 0 : -1;
1179 send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
1180 u_int len, const u_char *handle, u_int handle_len)
1185 if ((msg = sshbuf_new()) == NULL)
1186 fatal("%s: sshbuf_new failed", __func__);
1187 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READ)) != 0 ||
1188 (r = sshbuf_put_u32(msg, id)) != 0 ||
1189 (r = sshbuf_put_string(msg, handle, handle_len)) != 0 ||
1190 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1191 (r = sshbuf_put_u32(msg, len)) != 0)
1192 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1193 send_msg(conn, msg);
1198 do_download(struct sftp_conn *conn, const char *remote_path,
1199 const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
1205 int local_fd = -1, write_error;
1206 int read_error, write_errno, reordered = 0, r;
1207 u_int64_t offset = 0, size, highwater;
1208 u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
1209 off_t progress_counter;
1216 TAILQ_ENTRY(request) tq;
1218 TAILQ_HEAD(reqhead, request) requests;
1219 struct request *req;
1222 TAILQ_INIT(&requests);
1224 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1227 /* Do not preserve set[ug]id here, as we do not preserve ownership */
1228 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1229 mode = a->perm & 0777;
1233 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1234 (!S_ISREG(a->perm))) {
1235 error("Cannot download non-regular file: %s", remote_path);
1239 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1244 buflen = conn->transfer_buflen;
1245 if ((msg = sshbuf_new()) == NULL)
1246 fatal("%s: sshbuf_new failed", __func__);
1248 attrib_clear(&junk); /* Send empty attributes */
1250 /* Send open request */
1251 id = conn->msg_id++;
1252 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1253 (r = sshbuf_put_u32(msg, id)) != 0 ||
1254 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1255 (r = sshbuf_put_u32(msg, SSH2_FXF_READ)) != 0 ||
1256 (r = encode_attrib(msg, &junk)) != 0)
1257 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1258 send_msg(conn, msg);
1259 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1261 handle = get_handle(conn, id, &handle_len,
1262 "remote open(\"%s\")", remote_path);
1263 if (handle == NULL) {
1268 local_fd = open(local_path,
1269 O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
1270 if (local_fd == -1) {
1271 error("Couldn't open local file \"%s\" for writing: %s",
1272 local_path, strerror(errno));
1275 offset = highwater = 0;
1277 if (fstat(local_fd, &st) == -1) {
1278 error("Unable to stat local file \"%s\": %s",
1279 local_path, strerror(errno));
1282 if (st.st_size < 0) {
1283 error("\"%s\" has negative size", local_path);
1286 if ((u_int64_t)st.st_size > size) {
1287 error("Unable to resume download of \"%s\": "
1288 "local file is larger than remote", local_path);
1290 do_close(conn, handle, handle_len);
1297 offset = highwater = st.st_size;
1300 /* Read from remote and write to local */
1301 write_error = read_error = write_errno = num_req = 0;
1303 progress_counter = offset;
1305 if (showprogress && size != 0)
1306 start_progress_meter(remote_path, size, &progress_counter);
1308 while (num_req > 0 || max_req > 0) {
1313 * Simulate EOF on interrupt: stop sending new requests and
1314 * allow outstanding requests to drain gracefully
1317 if (num_req == 0) /* If we haven't started yet... */
1322 /* Send some more requests */
1323 while (num_req < max_req) {
1324 debug3("Request range %llu -> %llu (%d/%d)",
1325 (unsigned long long)offset,
1326 (unsigned long long)offset + buflen - 1,
1328 req = xcalloc(1, sizeof(*req));
1329 req->id = conn->msg_id++;
1331 req->offset = offset;
1334 TAILQ_INSERT_TAIL(&requests, req, tq);
1335 send_read_request(conn, req->id, req->offset,
1336 req->len, handle, handle_len);
1341 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1342 (r = sshbuf_get_u32(msg, &id)) != 0)
1343 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1344 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1346 /* Find the request in our queue */
1347 for (req = TAILQ_FIRST(&requests);
1348 req != NULL && req->id != id;
1349 req = TAILQ_NEXT(req, tq))
1352 fatal("Unexpected reply %u", id);
1355 case SSH2_FXP_STATUS:
1356 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1357 fatal("%s: buffer error: %s",
1358 __func__, ssh_err(r));
1359 if (status != SSH2_FX_EOF)
1362 TAILQ_REMOVE(&requests, req, tq);
1367 if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1368 fatal("%s: buffer error: %s",
1369 __func__, ssh_err(r));
1370 debug3("Received data %llu -> %llu",
1371 (unsigned long long)req->offset,
1372 (unsigned long long)req->offset + len - 1);
1374 fatal("Received more data than asked for "
1375 "%zu > %zu", len, req->len);
1376 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1377 atomicio(vwrite, local_fd, data, len) != len) &&
1379 write_errno = errno;
1383 else if (!reordered && req->offset <= highwater)
1384 highwater = req->offset + len;
1385 else if (!reordered && req->offset > highwater)
1387 progress_counter += len;
1390 if (len == req->len) {
1391 TAILQ_REMOVE(&requests, req, tq);
1395 /* Resend the request for the missing data */
1396 debug3("Short data block, re-requesting "
1397 "%llu -> %llu (%2d)",
1398 (unsigned long long)req->offset + len,
1399 (unsigned long long)req->offset +
1400 req->len - 1, num_req);
1401 req->id = conn->msg_id++;
1404 send_read_request(conn, req->id,
1405 req->offset, req->len, handle, handle_len);
1406 /* Reduce the request size */
1408 buflen = MAXIMUM(MIN_READ_SIZE, len);
1410 if (max_req > 0) { /* max_req = 0 iff EOF received */
1411 if (size > 0 && offset > size) {
1412 /* Only one request at a time
1413 * after the expected EOF */
1414 debug3("Finish at %llu (%2d)",
1415 (unsigned long long)offset,
1418 } else if (max_req <= conn->num_requests) {
1424 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1425 SSH2_FXP_DATA, type);
1429 if (showprogress && size)
1430 stop_progress_meter();
1433 if (TAILQ_FIRST(&requests) != NULL)
1434 fatal("Transfer complete, but requests still in queue");
1435 /* Truncate at highest contiguous point to avoid holes on interrupt */
1436 if (read_error || write_error || interrupted) {
1437 if (reordered && resume_flag) {
1438 error("Unable to resume download of \"%s\": "
1439 "server reordered requests", local_path);
1441 debug("truncating at %llu", (unsigned long long)highwater);
1442 if (ftruncate(local_fd, highwater) == -1)
1443 error("ftruncate \"%s\": %s", local_path,
1447 error("Couldn't read from remote file \"%s\" : %s",
1448 remote_path, fx2txt(status));
1450 do_close(conn, handle, handle_len);
1451 } else if (write_error) {
1452 error("Couldn't write to \"%s\": %s", local_path,
1453 strerror(write_errno));
1454 status = SSH2_FX_FAILURE;
1455 do_close(conn, handle, handle_len);
1457 if (do_close(conn, handle, handle_len) != 0 || interrupted)
1458 status = SSH2_FX_FAILURE;
1460 status = SSH2_FX_OK;
1461 /* Override umask and utimes if asked */
1463 if (preserve_flag && fchmod(local_fd, mode) == -1)
1465 if (preserve_flag && chmod(local_path, mode) == -1)
1466 #endif /* HAVE_FCHMOD */
1467 error("Couldn't set mode on \"%s\": %s", local_path,
1469 if (preserve_flag &&
1470 (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1471 struct timeval tv[2];
1472 tv[0].tv_sec = a->atime;
1473 tv[1].tv_sec = a->mtime;
1474 tv[0].tv_usec = tv[1].tv_usec = 0;
1475 if (utimes(local_path, tv) == -1)
1476 error("Can't set times on \"%s\": %s",
1477 local_path, strerror(errno));
1480 debug("syncing \"%s\"", local_path);
1481 if (fsync(local_fd) == -1)
1482 error("Couldn't sync file \"%s\": %s",
1483 local_path, strerror(errno));
1490 return status == SSH2_FX_OK ? 0 : -1;
1494 download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1495 int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
1496 int resume_flag, int fsync_flag)
1499 SFTP_DIRENT **dir_entries;
1500 char *filename, *new_src = NULL, *new_dst = NULL;
1503 if (depth >= MAX_DIR_DEPTH) {
1504 error("Maximum directory depth exceeded: %d levels", depth);
1508 if (dirattrib == NULL &&
1509 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1510 error("Unable to stat remote directory \"%s\"", src);
1513 if (!S_ISDIR(dirattrib->perm)) {
1514 error("\"%s\" is not a directory", src);
1518 mprintf("Retrieving %s\n", src);
1520 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1521 mode = dirattrib->perm & 01777;
1523 debug("Server did not send permissions for "
1524 "directory \"%s\"", dst);
1527 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1528 error("mkdir %s: %s", dst, strerror(errno));
1532 if (do_readdir(conn, src, &dir_entries) == -1) {
1533 error("%s: Failed to get directory contents", src);
1537 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1541 filename = dir_entries[i]->filename;
1542 new_dst = path_append(dst, filename);
1543 new_src = path_append(src, filename);
1545 if (S_ISDIR(dir_entries[i]->a.perm)) {
1546 if (strcmp(filename, ".") == 0 ||
1547 strcmp(filename, "..") == 0)
1549 if (download_dir_internal(conn, new_src, new_dst,
1550 depth + 1, &(dir_entries[i]->a), preserve_flag,
1551 print_flag, resume_flag, fsync_flag) == -1)
1553 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1554 if (do_download(conn, new_src, new_dst,
1555 &(dir_entries[i]->a), preserve_flag,
1556 resume_flag, fsync_flag) == -1) {
1557 error("Download of file %s to %s failed",
1562 logit("%s: not a regular file\n", new_src);
1568 if (preserve_flag) {
1569 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1570 struct timeval tv[2];
1571 tv[0].tv_sec = dirattrib->atime;
1572 tv[1].tv_sec = dirattrib->mtime;
1573 tv[0].tv_usec = tv[1].tv_usec = 0;
1574 if (utimes(dst, tv) == -1)
1575 error("Can't set times on \"%s\": %s",
1576 dst, strerror(errno));
1578 debug("Server did not send times for directory "
1582 free_sftp_dirents(dir_entries);
1588 download_dir(struct sftp_conn *conn, const char *src, const char *dst,
1589 Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1595 if ((src_canon = do_realpath(conn, src)) == NULL) {
1596 error("Unable to canonicalize path \"%s\"", src);
1600 ret = download_dir_internal(conn, src_canon, dst, 0,
1601 dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag);
1607 do_upload(struct sftp_conn *conn, const char *local_path,
1608 const char *remote_path, int preserve_flag, int resume, int fsync_flag)
1611 u_int status = SSH2_FX_OK;
1614 off_t offset, progress_counter;
1615 u_char *handle, *data;
1618 Attrib a, *c = NULL;
1621 struct outstanding_ack {
1625 TAILQ_ENTRY(outstanding_ack) tq;
1627 TAILQ_HEAD(ackhead, outstanding_ack) acks;
1628 struct outstanding_ack *ack = NULL;
1633 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1634 error("Couldn't open local file \"%s\" for reading: %s",
1635 local_path, strerror(errno));
1638 if (fstat(local_fd, &sb) == -1) {
1639 error("Couldn't fstat local file \"%s\": %s",
1640 local_path, strerror(errno));
1644 if (!S_ISREG(sb.st_mode)) {
1645 error("%s is not a regular file", local_path);
1649 stat_to_attrib(&sb, &a);
1651 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1652 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1655 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1658 /* Get remote file size if it exists */
1659 if ((c = do_stat(conn, remote_path, 0)) == NULL) {
1664 if ((off_t)c->size >= sb.st_size) {
1665 error("destination file bigger or same size as "
1671 if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
1677 if ((msg = sshbuf_new()) == NULL)
1678 fatal("%s: sshbuf_new failed", __func__);
1680 /* Send open request */
1681 id = conn->msg_id++;
1682 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1683 (r = sshbuf_put_u32(msg, id)) != 0 ||
1684 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1685 (r = sshbuf_put_u32(msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|
1686 (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC))) != 0 ||
1687 (r = encode_attrib(msg, &a)) != 0)
1688 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1689 send_msg(conn, msg);
1690 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1694 handle = get_handle(conn, id, &handle_len,
1695 "remote open(\"%s\")", remote_path);
1696 if (handle == NULL) {
1702 startid = ackid = id + 1;
1703 data = xmalloc(conn->transfer_buflen);
1705 /* Read from local and write to remote */
1706 offset = progress_counter = (resume ? c->size : 0);
1708 start_progress_meter(local_path, sb.st_size,
1715 * Can't use atomicio here because it returns 0 on EOF,
1716 * thus losing the last block of the file.
1717 * Simulate an EOF on interrupt, allowing ACKs from the
1720 if (interrupted || status != SSH2_FX_OK)
1723 len = read(local_fd, data, conn->transfer_buflen);
1724 while ((len == -1) &&
1725 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
1728 fatal("Couldn't read from \"%s\": %s", local_path,
1732 ack = xcalloc(1, sizeof(*ack));
1734 ack->offset = offset;
1736 TAILQ_INSERT_TAIL(&acks, ack, tq);
1739 if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
1740 (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
1741 (r = sshbuf_put_string(msg, handle,
1742 handle_len)) != 0 ||
1743 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1744 (r = sshbuf_put_string(msg, data, len)) != 0)
1745 fatal("%s: buffer error: %s",
1746 __func__, ssh_err(r));
1747 send_msg(conn, msg);
1748 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1749 id, (unsigned long long)offset, len);
1750 } else if (TAILQ_FIRST(&acks) == NULL)
1754 fatal("Unexpected ACK %u", id);
1756 if (id == startid || len == 0 ||
1757 id - ackid >= conn->num_requests) {
1762 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1763 (r = sshbuf_get_u32(msg, &rid)) != 0)
1764 fatal("%s: buffer error: %s",
1765 __func__, ssh_err(r));
1767 if (type != SSH2_FXP_STATUS)
1768 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1769 "got %d", SSH2_FXP_STATUS, type);
1771 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1772 fatal("%s: buffer error: %s",
1773 __func__, ssh_err(r));
1774 debug3("SSH2_FXP_STATUS %u", status);
1776 /* Find the request in our queue */
1777 for (ack = TAILQ_FIRST(&acks);
1778 ack != NULL && ack->id != rid;
1779 ack = TAILQ_NEXT(ack, tq))
1782 fatal("Can't find request for ID %u", rid);
1783 TAILQ_REMOVE(&acks, ack, tq);
1784 debug3("In write loop, ack for %u %u bytes at %lld",
1785 ack->id, ack->len, (long long)ack->offset);
1787 progress_counter += ack->len;
1792 fatal("%s: offset < 0", __func__);
1797 stop_progress_meter();
1800 if (status != SSH2_FX_OK) {
1801 error("Couldn't write to remote file \"%s\": %s",
1802 remote_path, fx2txt(status));
1803 status = SSH2_FX_FAILURE;
1806 if (close(local_fd) == -1) {
1807 error("Couldn't close local file \"%s\": %s", local_path,
1809 status = SSH2_FX_FAILURE;
1812 /* Override umask and utimes if asked */
1814 do_fsetstat(conn, handle, handle_len, &a);
1817 (void)do_fsync(conn, handle, handle_len);
1819 if (do_close(conn, handle, handle_len) != 0)
1820 status = SSH2_FX_FAILURE;
1824 return status == SSH2_FX_OK ? 0 : -1;
1828 upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1829 int depth, int preserve_flag, int print_flag, int resume, int fsync_flag)
1834 char *filename, *new_src = NULL, *new_dst = NULL;
1836 Attrib a, *dirattrib;
1838 if (depth >= MAX_DIR_DEPTH) {
1839 error("Maximum directory depth exceeded: %d levels", depth);
1843 if (stat(src, &sb) == -1) {
1844 error("Couldn't stat directory \"%s\": %s",
1845 src, strerror(errno));
1848 if (!S_ISDIR(sb.st_mode)) {
1849 error("\"%s\" is not a directory", src);
1853 mprintf("Entering %s\n", src);
1856 stat_to_attrib(&sb, &a);
1857 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1858 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1861 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1864 * sftp lacks a portable status value to match errno EEXIST,
1865 * so if we get a failure back then we must check whether
1866 * the path already existed and is a directory.
1868 if (do_mkdir(conn, dst, &a, 0) != 0) {
1869 if ((dirattrib = do_stat(conn, dst, 0)) == NULL)
1871 if (!S_ISDIR(dirattrib->perm)) {
1872 error("\"%s\" exists but is not a directory", dst);
1877 if ((dirp = opendir(src)) == NULL) {
1878 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1882 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1887 filename = dp->d_name;
1888 new_dst = path_append(dst, filename);
1889 new_src = path_append(src, filename);
1891 if (lstat(new_src, &sb) == -1) {
1892 logit("%s: lstat failed: %s", filename,
1895 } else if (S_ISDIR(sb.st_mode)) {
1896 if (strcmp(filename, ".") == 0 ||
1897 strcmp(filename, "..") == 0)
1900 if (upload_dir_internal(conn, new_src, new_dst,
1901 depth + 1, preserve_flag, print_flag, resume,
1904 } else if (S_ISREG(sb.st_mode)) {
1905 if (do_upload(conn, new_src, new_dst,
1906 preserve_flag, resume, fsync_flag) == -1) {
1907 error("Uploading of file %s to %s failed!",
1912 logit("%s: not a regular file\n", filename);
1917 do_setstat(conn, dst, &a);
1919 (void) closedir(dirp);
1924 upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
1925 int preserve_flag, int print_flag, int resume, int fsync_flag)
1930 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1931 error("Unable to canonicalize path \"%s\"", dst);
1935 ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
1936 print_flag, resume, fsync_flag);
1943 path_append(const char *p1, const char *p2)
1946 size_t len = strlen(p1) + strlen(p2) + 2;
1949 strlcpy(ret, p1, len);
1950 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1951 strlcat(ret, "/", len);
1952 strlcat(ret, p2, len);