1 /* $OpenBSD: sftp-client.c,v 1.130 2018/07/31 03:07:24 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>
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
91 struct bwlimit bwlimit_in, bwlimit_out;
95 get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
96 const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
100 sftpio(void *_bwlimit, size_t amount)
102 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
104 bandwidth_limit(bwlimit, amount);
109 send_msg(struct sftp_conn *conn, struct sshbuf *m)
114 if (sshbuf_len(m) > SFTP_MAX_MSG_LENGTH)
115 fatal("Outbound message too long %zu", sshbuf_len(m));
117 /* Send length first */
118 put_u32(mlen, sshbuf_len(m));
119 iov[0].iov_base = mlen;
120 iov[0].iov_len = sizeof(mlen);
121 iov[1].iov_base = (u_char *)sshbuf_ptr(m);
122 iov[1].iov_len = sshbuf_len(m);
124 if (atomiciov6(writev, conn->fd_out, iov, 2,
125 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_out) !=
126 sshbuf_len(m) + sizeof(mlen))
127 fatal("Couldn't send packet: %s", strerror(errno));
133 get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
139 if ((r = sshbuf_reserve(m, 4, &p)) != 0)
140 fatal("%s: buffer error: %s", __func__, ssh_err(r));
141 if (atomicio6(read, conn->fd_in, p, 4,
142 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in) != 4) {
143 if (errno == EPIPE || errno == ECONNRESET)
144 fatal("Connection closed");
146 fatal("Couldn't read packet: %s", strerror(errno));
149 if ((r = sshbuf_get_u32(m, &msg_len)) != 0)
150 fatal("%s: buffer error: %s", __func__, ssh_err(r));
151 if (msg_len > SFTP_MAX_MSG_LENGTH) {
152 do_log2(initial ? SYSLOG_LEVEL_ERROR : SYSLOG_LEVEL_FATAL,
153 "Received message too long %u", msg_len);
154 fatal("Ensure the remote shell produces no output "
155 "for non-interactive sessions.");
158 if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
159 fatal("%s: buffer error: %s", __func__, ssh_err(r));
160 if (atomicio6(read, conn->fd_in, p, msg_len,
161 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in)
164 fatal("Connection closed");
166 fatal("Read packet: %s", strerror(errno));
171 get_msg(struct sftp_conn *conn, struct sshbuf *m)
173 get_msg_extended(conn, m, 0);
177 send_string_request(struct sftp_conn *conn, u_int id, u_int code, const char *s,
183 if ((msg = sshbuf_new()) == NULL)
184 fatal("%s: sshbuf_new failed", __func__);
185 if ((r = sshbuf_put_u8(msg, code)) != 0 ||
186 (r = sshbuf_put_u32(msg, id)) != 0 ||
187 (r = sshbuf_put_string(msg, s, len)) != 0)
188 fatal("%s: buffer error: %s", __func__, ssh_err(r));
190 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
195 send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
196 const void *s, u_int len, Attrib *a)
201 if ((msg = sshbuf_new()) == NULL)
202 fatal("%s: sshbuf_new failed", __func__);
203 if ((r = sshbuf_put_u8(msg, code)) != 0 ||
204 (r = sshbuf_put_u32(msg, id)) != 0 ||
205 (r = sshbuf_put_string(msg, s, len)) != 0 ||
206 (r = encode_attrib(msg, a)) != 0)
207 fatal("%s: buffer error: %s", __func__, ssh_err(r));
209 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
214 get_status(struct sftp_conn *conn, u_int expected_id)
221 if ((msg = sshbuf_new()) == NULL)
222 fatal("%s: sshbuf_new failed", __func__);
224 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
225 (r = sshbuf_get_u32(msg, &id)) != 0)
226 fatal("%s: buffer error: %s", __func__, ssh_err(r));
228 if (id != expected_id)
229 fatal("ID mismatch (%u != %u)", id, expected_id);
230 if (type != SSH2_FXP_STATUS)
231 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
232 SSH2_FXP_STATUS, type);
234 if ((r = sshbuf_get_u32(msg, &status)) != 0)
235 fatal("%s: buffer error: %s", __func__, ssh_err(r));
238 debug3("SSH2_FXP_STATUS %u", status);
244 get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
245 const char *errfmt, ...)
255 va_start(args, errfmt);
257 vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
260 if ((msg = sshbuf_new()) == NULL)
261 fatal("%s: sshbuf_new failed", __func__);
263 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
264 (r = sshbuf_get_u32(msg, &id)) != 0)
265 fatal("%s: buffer error: %s", __func__, ssh_err(r));
267 if (id != expected_id)
268 fatal("%s: ID mismatch (%u != %u)",
269 errfmt == NULL ? __func__ : errmsg, id, expected_id);
270 if (type == SSH2_FXP_STATUS) {
271 if ((r = sshbuf_get_u32(msg, &status)) != 0)
272 fatal("%s: buffer error: %s", __func__, ssh_err(r));
274 error("%s: %s", errmsg, fx2txt(status));
277 } else if (type != SSH2_FXP_HANDLE)
278 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
279 errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
281 if ((r = sshbuf_get_string(msg, &handle, len)) != 0)
282 fatal("%s: buffer error: %s", __func__, ssh_err(r));
289 get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
297 if ((msg = sshbuf_new()) == NULL)
298 fatal("%s: sshbuf_new failed", __func__);
301 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
302 (r = sshbuf_get_u32(msg, &id)) != 0)
303 fatal("%s: buffer error: %s", __func__, ssh_err(r));
305 debug3("Received stat reply T:%u I:%u", type, id);
306 if (id != expected_id)
307 fatal("ID mismatch (%u != %u)", id, expected_id);
308 if (type == SSH2_FXP_STATUS) {
311 if ((r = sshbuf_get_u32(msg, &status)) != 0)
312 fatal("%s: buffer error: %s", __func__, ssh_err(r));
314 debug("Couldn't stat remote file: %s", fx2txt(status));
316 error("Couldn't stat remote file: %s", fx2txt(status));
319 } else if (type != SSH2_FXP_ATTRS) {
320 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
321 SSH2_FXP_ATTRS, type);
323 if ((r = decode_attrib(msg, &a)) != 0) {
324 error("%s: couldn't decode attrib: %s", __func__, ssh_err(r));
334 get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
335 u_int expected_id, int quiet)
343 if ((msg = sshbuf_new()) == NULL)
344 fatal("%s: sshbuf_new failed", __func__);
347 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
348 (r = sshbuf_get_u32(msg, &id)) != 0)
349 fatal("%s: buffer error: %s", __func__, ssh_err(r));
351 debug3("Received statvfs reply T:%u I:%u", type, id);
352 if (id != expected_id)
353 fatal("ID mismatch (%u != %u)", id, expected_id);
354 if (type == SSH2_FXP_STATUS) {
357 if ((r = sshbuf_get_u32(msg, &status)) != 0)
358 fatal("%s: buffer error: %s", __func__, ssh_err(r));
360 debug("Couldn't statvfs: %s", fx2txt(status));
362 error("Couldn't statvfs: %s", fx2txt(status));
365 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
366 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
367 SSH2_FXP_EXTENDED_REPLY, type);
370 memset(st, 0, sizeof(*st));
371 if ((r = sshbuf_get_u64(msg, &st->f_bsize)) != 0 ||
372 (r = sshbuf_get_u64(msg, &st->f_frsize)) != 0 ||
373 (r = sshbuf_get_u64(msg, &st->f_blocks)) != 0 ||
374 (r = sshbuf_get_u64(msg, &st->f_bfree)) != 0 ||
375 (r = sshbuf_get_u64(msg, &st->f_bavail)) != 0 ||
376 (r = sshbuf_get_u64(msg, &st->f_files)) != 0 ||
377 (r = sshbuf_get_u64(msg, &st->f_ffree)) != 0 ||
378 (r = sshbuf_get_u64(msg, &st->f_favail)) != 0 ||
379 (r = sshbuf_get_u64(msg, &st->f_fsid)) != 0 ||
380 (r = sshbuf_get_u64(msg, &flag)) != 0 ||
381 (r = sshbuf_get_u64(msg, &st->f_namemax)) != 0)
382 fatal("%s: buffer error: %s", __func__, ssh_err(r));
384 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
385 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
393 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
394 u_int64_t limit_kbps)
398 struct sftp_conn *ret;
401 ret = xcalloc(1, sizeof(*ret));
404 ret->fd_out = fd_out;
405 ret->transfer_buflen = transfer_buflen;
406 ret->num_requests = num_requests;
410 if ((msg = sshbuf_new()) == NULL)
411 fatal("%s: sshbuf_new failed", __func__);
412 if ((r = sshbuf_put_u8(msg, SSH2_FXP_INIT)) != 0 ||
413 (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0)
414 fatal("%s: buffer error: %s", __func__, ssh_err(r));
419 get_msg_extended(ret, msg, 1);
421 /* Expecting a VERSION reply */
422 if ((r = sshbuf_get_u8(msg, &type)) != 0)
423 fatal("%s: buffer error: %s", __func__, ssh_err(r));
424 if (type != SSH2_FXP_VERSION) {
425 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
431 if ((r = sshbuf_get_u32(msg, &ret->version)) != 0)
432 fatal("%s: buffer error: %s", __func__, ssh_err(r));
434 debug2("Remote version: %u", ret->version);
436 /* Check for extensions */
437 while (sshbuf_len(msg) > 0) {
443 if ((r = sshbuf_get_cstring(msg, &name, NULL)) != 0 ||
444 (r = sshbuf_get_string(msg, &value, &vlen)) != 0)
445 fatal("%s: buffer error: %s", __func__, ssh_err(r));
446 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
447 strcmp((char *)value, "1") == 0) {
448 ret->exts |= SFTP_EXT_POSIX_RENAME;
450 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
451 strcmp((char *)value, "2") == 0) {
452 ret->exts |= SFTP_EXT_STATVFS;
454 } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
455 strcmp((char *)value, "2") == 0) {
456 ret->exts |= SFTP_EXT_FSTATVFS;
458 } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
459 strcmp((char *)value, "1") == 0) {
460 ret->exts |= SFTP_EXT_HARDLINK;
462 } else if (strcmp(name, "fsync@openssh.com") == 0 &&
463 strcmp((char *)value, "1") == 0) {
464 ret->exts |= SFTP_EXT_FSYNC;
468 debug2("Server supports extension \"%s\" revision %s",
471 debug2("Unrecognised server extension \"%s\"", name);
479 /* Some filexfer v.0 servers don't support large packets */
480 if (ret->version == 0)
481 ret->transfer_buflen = MINIMUM(ret->transfer_buflen, 20480);
483 ret->limit_kbps = limit_kbps;
484 if (ret->limit_kbps > 0) {
485 bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
486 ret->transfer_buflen);
487 bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
488 ret->transfer_buflen);
495 sftp_proto_version(struct sftp_conn *conn)
497 return conn->version;
501 do_close(struct sftp_conn *conn, const u_char *handle, u_int handle_len)
507 if ((msg = sshbuf_new()) == NULL)
508 fatal("%s: sshbuf_new failed", __func__);
511 if ((r = sshbuf_put_u8(msg, SSH2_FXP_CLOSE)) != 0 ||
512 (r = sshbuf_put_u32(msg, id)) != 0 ||
513 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
514 fatal("%s: buffer error: %s", __func__, ssh_err(r));
516 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
518 status = get_status(conn, id);
519 if (status != SSH2_FX_OK)
520 error("Couldn't close file: %s", fx2txt(status));
524 return status == SSH2_FX_OK ? 0 : -1;
529 do_lsreaddir(struct sftp_conn *conn, const char *path, int print_flag,
533 u_int count, id, i, expected_id, ents = 0;
535 u_char type, *handle;
536 int status = SSH2_FX_FAILURE;
544 if ((msg = sshbuf_new()) == NULL)
545 fatal("%s: sshbuf_new failed", __func__);
546 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPENDIR)) != 0 ||
547 (r = sshbuf_put_u32(msg, id)) != 0 ||
548 (r = sshbuf_put_cstring(msg, path)) != 0)
549 fatal("%s: buffer error: %s", __func__, ssh_err(r));
552 handle = get_handle(conn, id, &handle_len,
553 "remote readdir(\"%s\")", path);
554 if (handle == NULL) {
561 *dir = xcalloc(1, sizeof(**dir));
565 for (; !interrupted;) {
566 id = expected_id = conn->msg_id++;
568 debug3("Sending SSH2_FXP_READDIR I:%u", id);
571 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READDIR)) != 0 ||
572 (r = sshbuf_put_u32(msg, id)) != 0 ||
573 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
574 fatal("%s: buffer error: %s", __func__, ssh_err(r));
581 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
582 (r = sshbuf_get_u32(msg, &id)) != 0)
583 fatal("%s: buffer error: %s", __func__, ssh_err(r));
585 debug3("Received reply T:%u I:%u", type, id);
587 if (id != expected_id)
588 fatal("ID mismatch (%u != %u)", id, expected_id);
590 if (type == SSH2_FXP_STATUS) {
593 if ((r = sshbuf_get_u32(msg, &rstatus)) != 0)
594 fatal("%s: buffer error: %s",
595 __func__, ssh_err(r));
596 debug3("Received SSH2_FXP_STATUS %d", rstatus);
597 if (rstatus == SSH2_FX_EOF)
599 error("Couldn't read directory: %s", fx2txt(rstatus));
601 } else if (type != SSH2_FXP_NAME)
602 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
603 SSH2_FXP_NAME, type);
605 if ((r = sshbuf_get_u32(msg, &count)) != 0)
606 fatal("%s: buffer error: %s", __func__, ssh_err(r));
607 if (count > SSHBUF_SIZE_MAX)
608 fatal("%s: nonsensical number of entries", __func__);
611 debug3("Received %d SSH2_FXP_NAME responses", count);
612 for (i = 0; i < count; i++) {
613 char *filename, *longname;
616 if ((r = sshbuf_get_cstring(msg, &filename,
618 (r = sshbuf_get_cstring(msg, &longname,
620 fatal("%s: buffer error: %s",
621 __func__, ssh_err(r));
622 if ((r = decode_attrib(msg, &a)) != 0) {
623 error("%s: couldn't decode attrib: %s",
624 __func__, ssh_err(r));
632 mprintf("%s\n", longname);
635 * Directory entries should never contain '/'
636 * These can be used to attack recursive ops
637 * (e.g. send '../../../../etc/passwd')
639 if (strpbrk(filename, SFTP_DIRECTORY_CHARS) != NULL) {
640 error("Server sent suspect path \"%s\" "
641 "during readdir of \"%s\"", filename, path);
643 *dir = xreallocarray(*dir, ents + 2, sizeof(**dir));
644 (*dir)[ents] = xcalloc(1, sizeof(***dir));
645 (*dir)[ents]->filename = xstrdup(filename);
646 (*dir)[ents]->longname = xstrdup(longname);
647 memcpy(&(*dir)[ents]->a, &a, sizeof(a));
648 (*dir)[++ents] = NULL;
658 do_close(conn, handle, handle_len);
661 if (status != 0 && dir != NULL) {
662 /* Don't return results on error */
663 free_sftp_dirents(*dir);
665 } else if (interrupted && dir != NULL && *dir != NULL) {
666 /* Don't return partial matches on interrupt */
667 free_sftp_dirents(*dir);
668 *dir = xcalloc(1, sizeof(**dir));
672 return status == SSH2_FX_OK ? 0 : -1;
676 do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir)
678 return(do_lsreaddir(conn, path, 0, dir));
681 void free_sftp_dirents(SFTP_DIRENT **s)
687 for (i = 0; s[i]; i++) {
688 free(s[i]->filename);
689 free(s[i]->longname);
696 do_rm(struct sftp_conn *conn, const char *path)
700 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
703 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
704 status = get_status(conn, id);
705 if (status != SSH2_FX_OK)
706 error("Couldn't delete file: %s", fx2txt(status));
707 return status == SSH2_FX_OK ? 0 : -1;
711 do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag)
716 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
719 status = get_status(conn, id);
720 if (status != SSH2_FX_OK && print_flag)
721 error("Couldn't create directory: %s", fx2txt(status));
723 return status == SSH2_FX_OK ? 0 : -1;
727 do_rmdir(struct sftp_conn *conn, const char *path)
732 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
735 status = get_status(conn, id);
736 if (status != SSH2_FX_OK)
737 error("Couldn't remove directory: %s", fx2txt(status));
739 return status == SSH2_FX_OK ? 0 : -1;
743 do_stat(struct sftp_conn *conn, const char *path, int quiet)
749 send_string_request(conn, id,
750 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
753 return(get_decode_stat(conn, id, quiet));
757 do_lstat(struct sftp_conn *conn, const char *path, int quiet)
761 if (conn->version == 0) {
763 debug("Server version does not support lstat operation");
765 logit("Server version does not support lstat operation");
766 return(do_stat(conn, path, quiet));
770 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
773 return(get_decode_stat(conn, id, quiet));
778 do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
784 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
787 return(get_decode_stat(conn, id, quiet));
792 do_setstat(struct sftp_conn *conn, const char *path, Attrib *a)
797 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
800 status = get_status(conn, id);
801 if (status != SSH2_FX_OK)
802 error("Couldn't setstat on \"%s\": %s", path,
805 return status == SSH2_FX_OK ? 0 : -1;
809 do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
815 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
818 status = get_status(conn, id);
819 if (status != SSH2_FX_OK)
820 error("Couldn't fsetstat: %s", fx2txt(status));
822 return status == SSH2_FX_OK ? 0 : -1;
826 do_realpath(struct sftp_conn *conn, const char *path)
829 u_int expected_id, count, id;
830 char *filename, *longname;
835 expected_id = id = conn->msg_id++;
836 send_string_request(conn, id, SSH2_FXP_REALPATH, path,
839 if ((msg = sshbuf_new()) == NULL)
840 fatal("%s: sshbuf_new failed", __func__);
843 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
844 (r = sshbuf_get_u32(msg, &id)) != 0)
845 fatal("%s: buffer error: %s", __func__, ssh_err(r));
847 if (id != expected_id)
848 fatal("ID mismatch (%u != %u)", id, expected_id);
850 if (type == SSH2_FXP_STATUS) {
853 if ((r = sshbuf_get_u32(msg, &status)) != 0)
854 fatal("%s: buffer error: %s", __func__, ssh_err(r));
855 error("Couldn't canonicalize: %s", fx2txt(status));
858 } else if (type != SSH2_FXP_NAME)
859 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
860 SSH2_FXP_NAME, type);
862 if ((r = sshbuf_get_u32(msg, &count)) != 0)
863 fatal("%s: buffer error: %s", __func__, ssh_err(r));
865 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
867 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
868 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
869 (r = decode_attrib(msg, &a)) != 0)
870 fatal("%s: buffer error: %s", __func__, ssh_err(r));
872 debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename,
873 (unsigned long)a.size);
883 do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath,
888 int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
890 if ((msg = sshbuf_new()) == NULL)
891 fatal("%s: sshbuf_new failed", __func__);
893 /* Send rename request */
896 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
897 (r = sshbuf_put_u32(msg, id)) != 0 ||
898 (r = sshbuf_put_cstring(msg,
899 "posix-rename@openssh.com")) != 0)
900 fatal("%s: buffer error: %s", __func__, ssh_err(r));
902 if ((r = sshbuf_put_u8(msg, SSH2_FXP_RENAME)) != 0 ||
903 (r = sshbuf_put_u32(msg, id)) != 0)
904 fatal("%s: buffer error: %s", __func__, ssh_err(r));
906 if ((r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
907 (r = sshbuf_put_cstring(msg, newpath)) != 0)
908 fatal("%s: buffer error: %s", __func__, ssh_err(r));
910 debug3("Sent message %s \"%s\" -> \"%s\"",
911 use_ext ? "posix-rename@openssh.com" :
912 "SSH2_FXP_RENAME", oldpath, newpath);
915 status = get_status(conn, id);
916 if (status != SSH2_FX_OK)
917 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
918 newpath, fx2txt(status));
920 return status == SSH2_FX_OK ? 0 : -1;
924 do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
930 if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
931 error("Server does not support hardlink@openssh.com extension");
935 if ((msg = sshbuf_new()) == NULL)
936 fatal("%s: sshbuf_new failed", __func__);
938 /* Send link request */
940 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
941 (r = sshbuf_put_u32(msg, id)) != 0 ||
942 (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
943 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
944 (r = sshbuf_put_cstring(msg, newpath)) != 0)
945 fatal("%s: buffer error: %s", __func__, ssh_err(r));
947 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
951 status = get_status(conn, id);
952 if (status != SSH2_FX_OK)
953 error("Couldn't link file \"%s\" to \"%s\": %s", oldpath,
954 newpath, fx2txt(status));
956 return status == SSH2_FX_OK ? 0 : -1;
960 do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
966 if (conn->version < 3) {
967 error("This server does not support the symlink operation");
968 return(SSH2_FX_OP_UNSUPPORTED);
971 if ((msg = sshbuf_new()) == NULL)
972 fatal("%s: sshbuf_new failed", __func__);
974 /* Send symlink request */
976 if ((r = sshbuf_put_u8(msg, SSH2_FXP_SYMLINK)) != 0 ||
977 (r = sshbuf_put_u32(msg, id)) != 0 ||
978 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
979 (r = sshbuf_put_cstring(msg, newpath)) != 0)
980 fatal("%s: buffer error: %s", __func__, ssh_err(r));
982 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
986 status = get_status(conn, id);
987 if (status != SSH2_FX_OK)
988 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
989 newpath, fx2txt(status));
991 return status == SSH2_FX_OK ? 0 : -1;
995 do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len)
1001 /* Silently return if the extension is not supported */
1002 if ((conn->exts & SFTP_EXT_FSYNC) == 0)
1005 /* Send fsync request */
1006 if ((msg = sshbuf_new()) == NULL)
1007 fatal("%s: sshbuf_new failed", __func__);
1008 id = conn->msg_id++;
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, "fsync@openssh.com")) != 0 ||
1012 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1013 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1014 send_msg(conn, msg);
1015 debug3("Sent message fsync@openssh.com I:%u", id);
1018 status = get_status(conn, id);
1019 if (status != SSH2_FX_OK)
1020 error("Couldn't sync file: %s", fx2txt(status));
1022 return status == SSH2_FX_OK ? 0 : -1;
1027 do_readlink(struct sftp_conn *conn, const char *path)
1030 u_int expected_id, count, id;
1031 char *filename, *longname;
1036 expected_id = id = conn->msg_id++;
1037 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
1039 if ((msg = sshbuf_new()) == NULL)
1040 fatal("%s: sshbuf_new failed", __func__);
1043 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1044 (r = sshbuf_get_u32(msg, &id)) != 0)
1045 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1047 if (id != expected_id)
1048 fatal("ID mismatch (%u != %u)", id, expected_id);
1050 if (type == SSH2_FXP_STATUS) {
1053 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1054 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1055 error("Couldn't readlink: %s", fx2txt(status));
1058 } else if (type != SSH2_FXP_NAME)
1059 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1060 SSH2_FXP_NAME, type);
1062 if ((r = sshbuf_get_u32(msg, &count)) != 0)
1063 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1065 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
1067 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
1068 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
1069 (r = decode_attrib(msg, &a)) != 0)
1070 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1072 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
1083 do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
1090 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
1091 error("Server does not support statvfs@openssh.com extension");
1095 id = conn->msg_id++;
1097 if ((msg = sshbuf_new()) == NULL)
1098 fatal("%s: sshbuf_new failed", __func__);
1100 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1101 (r = sshbuf_put_u32(msg, id)) != 0 ||
1102 (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
1103 (r = sshbuf_put_cstring(msg, path)) != 0)
1104 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1105 send_msg(conn, msg);
1108 return get_decode_statvfs(conn, st, id, quiet);
1113 do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
1114 struct sftp_statvfs *st, int quiet)
1119 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
1120 error("Server does not support fstatvfs@openssh.com extension");
1124 id = conn->msg_id++;
1126 if ((msg = sshbuf_new()) == NULL)
1127 fatal("%s: sshbuf_new failed", __func__);
1129 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1130 (r = sshbuf_put_u32(msg, id)) != 0 ||
1131 (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
1132 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1133 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1134 send_msg(conn, msg);
1137 return get_decode_statvfs(conn, st, id, quiet);
1142 send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
1143 u_int len, const u_char *handle, u_int handle_len)
1148 if ((msg = sshbuf_new()) == NULL)
1149 fatal("%s: sshbuf_new failed", __func__);
1151 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READ)) != 0 ||
1152 (r = sshbuf_put_u32(msg, id)) != 0 ||
1153 (r = sshbuf_put_string(msg, handle, handle_len)) != 0 ||
1154 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1155 (r = sshbuf_put_u32(msg, len)) != 0)
1156 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1157 send_msg(conn, msg);
1162 do_download(struct sftp_conn *conn, const char *remote_path,
1163 const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
1169 int local_fd = -1, write_error;
1170 int read_error, write_errno, reordered = 0, r;
1171 u_int64_t offset = 0, size, highwater;
1172 u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
1173 off_t progress_counter;
1180 TAILQ_ENTRY(request) tq;
1182 TAILQ_HEAD(reqhead, request) requests;
1183 struct request *req;
1186 TAILQ_INIT(&requests);
1188 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1191 /* Do not preserve set[ug]id here, as we do not preserve ownership */
1192 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1193 mode = a->perm & 0777;
1197 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1198 (!S_ISREG(a->perm))) {
1199 error("Cannot download non-regular file: %s", remote_path);
1203 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1208 buflen = conn->transfer_buflen;
1209 if ((msg = sshbuf_new()) == NULL)
1210 fatal("%s: sshbuf_new failed", __func__);
1212 attrib_clear(&junk); /* Send empty attributes */
1214 /* Send open request */
1215 id = conn->msg_id++;
1216 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1217 (r = sshbuf_put_u32(msg, id)) != 0 ||
1218 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1219 (r = sshbuf_put_u32(msg, SSH2_FXF_READ)) != 0 ||
1220 (r = encode_attrib(msg, &junk)) != 0)
1221 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1222 send_msg(conn, msg);
1223 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1225 handle = get_handle(conn, id, &handle_len,
1226 "remote open(\"%s\")", remote_path);
1227 if (handle == NULL) {
1232 local_fd = open(local_path,
1233 O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
1234 if (local_fd == -1) {
1235 error("Couldn't open local file \"%s\" for writing: %s",
1236 local_path, strerror(errno));
1239 offset = highwater = 0;
1241 if (fstat(local_fd, &st) == -1) {
1242 error("Unable to stat local file \"%s\": %s",
1243 local_path, strerror(errno));
1246 if (st.st_size < 0) {
1247 error("\"%s\" has negative size", local_path);
1250 if ((u_int64_t)st.st_size > size) {
1251 error("Unable to resume download of \"%s\": "
1252 "local file is larger than remote", local_path);
1254 do_close(conn, handle, handle_len);
1261 offset = highwater = st.st_size;
1264 /* Read from remote and write to local */
1265 write_error = read_error = write_errno = num_req = 0;
1267 progress_counter = offset;
1269 if (showprogress && size != 0)
1270 start_progress_meter(remote_path, size, &progress_counter);
1272 while (num_req > 0 || max_req > 0) {
1277 * Simulate EOF on interrupt: stop sending new requests and
1278 * allow outstanding requests to drain gracefully
1281 if (num_req == 0) /* If we haven't started yet... */
1286 /* Send some more requests */
1287 while (num_req < max_req) {
1288 debug3("Request range %llu -> %llu (%d/%d)",
1289 (unsigned long long)offset,
1290 (unsigned long long)offset + buflen - 1,
1292 req = xcalloc(1, sizeof(*req));
1293 req->id = conn->msg_id++;
1295 req->offset = offset;
1298 TAILQ_INSERT_TAIL(&requests, req, tq);
1299 send_read_request(conn, req->id, req->offset,
1300 req->len, handle, handle_len);
1305 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1306 (r = sshbuf_get_u32(msg, &id)) != 0)
1307 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1308 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1310 /* Find the request in our queue */
1311 for (req = TAILQ_FIRST(&requests);
1312 req != NULL && req->id != id;
1313 req = TAILQ_NEXT(req, tq))
1316 fatal("Unexpected reply %u", id);
1319 case SSH2_FXP_STATUS:
1320 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1321 fatal("%s: buffer error: %s",
1322 __func__, ssh_err(r));
1323 if (status != SSH2_FX_EOF)
1326 TAILQ_REMOVE(&requests, req, tq);
1331 if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1332 fatal("%s: buffer error: %s",
1333 __func__, ssh_err(r));
1334 debug3("Received data %llu -> %llu",
1335 (unsigned long long)req->offset,
1336 (unsigned long long)req->offset + len - 1);
1338 fatal("Received more data than asked for "
1339 "%zu > %zu", len, req->len);
1340 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1341 atomicio(vwrite, local_fd, data, len) != len) &&
1343 write_errno = errno;
1347 else if (!reordered && req->offset <= highwater)
1348 highwater = req->offset + len;
1349 else if (!reordered && req->offset > highwater)
1351 progress_counter += len;
1354 if (len == req->len) {
1355 TAILQ_REMOVE(&requests, req, tq);
1359 /* Resend the request for the missing data */
1360 debug3("Short data block, re-requesting "
1361 "%llu -> %llu (%2d)",
1362 (unsigned long long)req->offset + len,
1363 (unsigned long long)req->offset +
1364 req->len - 1, num_req);
1365 req->id = conn->msg_id++;
1368 send_read_request(conn, req->id,
1369 req->offset, req->len, handle, handle_len);
1370 /* Reduce the request size */
1372 buflen = MAXIMUM(MIN_READ_SIZE, len);
1374 if (max_req > 0) { /* max_req = 0 iff EOF received */
1375 if (size > 0 && offset > size) {
1376 /* Only one request at a time
1377 * after the expected EOF */
1378 debug3("Finish at %llu (%2d)",
1379 (unsigned long long)offset,
1382 } else if (max_req <= conn->num_requests) {
1388 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1389 SSH2_FXP_DATA, type);
1393 if (showprogress && size)
1394 stop_progress_meter();
1397 if (TAILQ_FIRST(&requests) != NULL)
1398 fatal("Transfer complete, but requests still in queue");
1399 /* Truncate at highest contiguous point to avoid holes on interrupt */
1400 if (read_error || write_error || interrupted) {
1401 if (reordered && resume_flag) {
1402 error("Unable to resume download of \"%s\": "
1403 "server reordered requests", local_path);
1405 debug("truncating at %llu", (unsigned long long)highwater);
1406 if (ftruncate(local_fd, highwater) == -1)
1407 error("ftruncate \"%s\": %s", local_path,
1411 error("Couldn't read from remote file \"%s\" : %s",
1412 remote_path, fx2txt(status));
1414 do_close(conn, handle, handle_len);
1415 } else if (write_error) {
1416 error("Couldn't write to \"%s\": %s", local_path,
1417 strerror(write_errno));
1418 status = SSH2_FX_FAILURE;
1419 do_close(conn, handle, handle_len);
1421 if (do_close(conn, handle, handle_len) != 0 || interrupted)
1422 status = SSH2_FX_FAILURE;
1424 status = SSH2_FX_OK;
1425 /* Override umask and utimes if asked */
1427 if (preserve_flag && fchmod(local_fd, mode) == -1)
1429 if (preserve_flag && chmod(local_path, mode) == -1)
1430 #endif /* HAVE_FCHMOD */
1431 error("Couldn't set mode on \"%s\": %s", local_path,
1433 if (preserve_flag &&
1434 (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1435 struct timeval tv[2];
1436 tv[0].tv_sec = a->atime;
1437 tv[1].tv_sec = a->mtime;
1438 tv[0].tv_usec = tv[1].tv_usec = 0;
1439 if (utimes(local_path, tv) == -1)
1440 error("Can't set times on \"%s\": %s",
1441 local_path, strerror(errno));
1444 debug("syncing \"%s\"", local_path);
1445 if (fsync(local_fd) == -1)
1446 error("Couldn't sync file \"%s\": %s",
1447 local_path, strerror(errno));
1454 return status == SSH2_FX_OK ? 0 : -1;
1458 download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1459 int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
1460 int resume_flag, int fsync_flag)
1463 SFTP_DIRENT **dir_entries;
1464 char *filename, *new_src = NULL, *new_dst = NULL;
1467 if (depth >= MAX_DIR_DEPTH) {
1468 error("Maximum directory depth exceeded: %d levels", depth);
1472 if (dirattrib == NULL &&
1473 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1474 error("Unable to stat remote directory \"%s\"", src);
1477 if (!S_ISDIR(dirattrib->perm)) {
1478 error("\"%s\" is not a directory", src);
1482 mprintf("Retrieving %s\n", src);
1484 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1485 mode = dirattrib->perm & 01777;
1487 debug("Server did not send permissions for "
1488 "directory \"%s\"", dst);
1491 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1492 error("mkdir %s: %s", dst, strerror(errno));
1496 if (do_readdir(conn, src, &dir_entries) == -1) {
1497 error("%s: Failed to get directory contents", src);
1501 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1505 filename = dir_entries[i]->filename;
1506 new_dst = path_append(dst, filename);
1507 new_src = path_append(src, filename);
1509 if (S_ISDIR(dir_entries[i]->a.perm)) {
1510 if (strcmp(filename, ".") == 0 ||
1511 strcmp(filename, "..") == 0)
1513 if (download_dir_internal(conn, new_src, new_dst,
1514 depth + 1, &(dir_entries[i]->a), preserve_flag,
1515 print_flag, resume_flag, fsync_flag) == -1)
1517 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1518 if (do_download(conn, new_src, new_dst,
1519 &(dir_entries[i]->a), preserve_flag,
1520 resume_flag, fsync_flag) == -1) {
1521 error("Download of file %s to %s failed",
1526 logit("%s: not a regular file\n", new_src);
1532 if (preserve_flag) {
1533 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1534 struct timeval tv[2];
1535 tv[0].tv_sec = dirattrib->atime;
1536 tv[1].tv_sec = dirattrib->mtime;
1537 tv[0].tv_usec = tv[1].tv_usec = 0;
1538 if (utimes(dst, tv) == -1)
1539 error("Can't set times on \"%s\": %s",
1540 dst, strerror(errno));
1542 debug("Server did not send times for directory "
1546 free_sftp_dirents(dir_entries);
1552 download_dir(struct sftp_conn *conn, const char *src, const char *dst,
1553 Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1559 if ((src_canon = do_realpath(conn, src)) == NULL) {
1560 error("Unable to canonicalize path \"%s\"", src);
1564 ret = download_dir_internal(conn, src_canon, dst, 0,
1565 dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag);
1571 do_upload(struct sftp_conn *conn, const char *local_path,
1572 const char *remote_path, int preserve_flag, int resume, int fsync_flag)
1575 u_int status = SSH2_FX_OK;
1578 off_t offset, progress_counter;
1579 u_char *handle, *data;
1582 Attrib a, *c = NULL;
1585 struct outstanding_ack {
1589 TAILQ_ENTRY(outstanding_ack) tq;
1591 TAILQ_HEAD(ackhead, outstanding_ack) acks;
1592 struct outstanding_ack *ack = NULL;
1597 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1598 error("Couldn't open local file \"%s\" for reading: %s",
1599 local_path, strerror(errno));
1602 if (fstat(local_fd, &sb) == -1) {
1603 error("Couldn't fstat local file \"%s\": %s",
1604 local_path, strerror(errno));
1608 if (!S_ISREG(sb.st_mode)) {
1609 error("%s is not a regular file", local_path);
1613 stat_to_attrib(&sb, &a);
1615 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1616 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1619 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1622 /* Get remote file size if it exists */
1623 if ((c = do_stat(conn, remote_path, 0)) == NULL) {
1628 if ((off_t)c->size >= sb.st_size) {
1629 error("destination file bigger or same size as "
1635 if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
1641 if ((msg = sshbuf_new()) == NULL)
1642 fatal("%s: sshbuf_new failed", __func__);
1644 /* Send open request */
1645 id = conn->msg_id++;
1646 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1647 (r = sshbuf_put_u32(msg, id)) != 0 ||
1648 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1649 (r = sshbuf_put_u32(msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|
1650 (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC))) != 0 ||
1651 (r = encode_attrib(msg, &a)) != 0)
1652 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1653 send_msg(conn, msg);
1654 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1658 handle = get_handle(conn, id, &handle_len,
1659 "remote open(\"%s\")", remote_path);
1660 if (handle == NULL) {
1666 startid = ackid = id + 1;
1667 data = xmalloc(conn->transfer_buflen);
1669 /* Read from local and write to remote */
1670 offset = progress_counter = (resume ? c->size : 0);
1672 start_progress_meter(local_path, sb.st_size,
1679 * Can't use atomicio here because it returns 0 on EOF,
1680 * thus losing the last block of the file.
1681 * Simulate an EOF on interrupt, allowing ACKs from the
1684 if (interrupted || status != SSH2_FX_OK)
1687 len = read(local_fd, data, conn->transfer_buflen);
1688 while ((len == -1) &&
1689 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
1692 fatal("Couldn't read from \"%s\": %s", local_path,
1696 ack = xcalloc(1, sizeof(*ack));
1698 ack->offset = offset;
1700 TAILQ_INSERT_TAIL(&acks, ack, tq);
1703 if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
1704 (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
1705 (r = sshbuf_put_string(msg, handle,
1706 handle_len)) != 0 ||
1707 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1708 (r = sshbuf_put_string(msg, data, len)) != 0)
1709 fatal("%s: buffer error: %s",
1710 __func__, ssh_err(r));
1711 send_msg(conn, msg);
1712 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1713 id, (unsigned long long)offset, len);
1714 } else if (TAILQ_FIRST(&acks) == NULL)
1718 fatal("Unexpected ACK %u", id);
1720 if (id == startid || len == 0 ||
1721 id - ackid >= conn->num_requests) {
1726 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1727 (r = sshbuf_get_u32(msg, &rid)) != 0)
1728 fatal("%s: buffer error: %s",
1729 __func__, ssh_err(r));
1731 if (type != SSH2_FXP_STATUS)
1732 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1733 "got %d", SSH2_FXP_STATUS, type);
1735 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1736 fatal("%s: buffer error: %s",
1737 __func__, ssh_err(r));
1738 debug3("SSH2_FXP_STATUS %u", status);
1740 /* Find the request in our queue */
1741 for (ack = TAILQ_FIRST(&acks);
1742 ack != NULL && ack->id != rid;
1743 ack = TAILQ_NEXT(ack, tq))
1746 fatal("Can't find request for ID %u", rid);
1747 TAILQ_REMOVE(&acks, ack, tq);
1748 debug3("In write loop, ack for %u %u bytes at %lld",
1749 ack->id, ack->len, (long long)ack->offset);
1751 progress_counter += ack->len;
1756 fatal("%s: offset < 0", __func__);
1761 stop_progress_meter();
1764 if (status != SSH2_FX_OK) {
1765 error("Couldn't write to remote file \"%s\": %s",
1766 remote_path, fx2txt(status));
1767 status = SSH2_FX_FAILURE;
1770 if (close(local_fd) == -1) {
1771 error("Couldn't close local file \"%s\": %s", local_path,
1773 status = SSH2_FX_FAILURE;
1776 /* Override umask and utimes if asked */
1778 do_fsetstat(conn, handle, handle_len, &a);
1781 (void)do_fsync(conn, handle, handle_len);
1783 if (do_close(conn, handle, handle_len) != 0)
1784 status = SSH2_FX_FAILURE;
1788 return status == SSH2_FX_OK ? 0 : -1;
1792 upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1793 int depth, int preserve_flag, int print_flag, int resume, int fsync_flag)
1798 char *filename, *new_src = NULL, *new_dst = NULL;
1800 Attrib a, *dirattrib;
1802 if (depth >= MAX_DIR_DEPTH) {
1803 error("Maximum directory depth exceeded: %d levels", depth);
1807 if (stat(src, &sb) == -1) {
1808 error("Couldn't stat directory \"%s\": %s",
1809 src, strerror(errno));
1812 if (!S_ISDIR(sb.st_mode)) {
1813 error("\"%s\" is not a directory", src);
1817 mprintf("Entering %s\n", src);
1820 stat_to_attrib(&sb, &a);
1821 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1822 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1825 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1828 * sftp lacks a portable status value to match errno EEXIST,
1829 * so if we get a failure back then we must check whether
1830 * the path already existed and is a directory.
1832 if (do_mkdir(conn, dst, &a, 0) != 0) {
1833 if ((dirattrib = do_stat(conn, dst, 0)) == NULL)
1835 if (!S_ISDIR(dirattrib->perm)) {
1836 error("\"%s\" exists but is not a directory", dst);
1841 if ((dirp = opendir(src)) == NULL) {
1842 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1846 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1851 filename = dp->d_name;
1852 new_dst = path_append(dst, filename);
1853 new_src = path_append(src, filename);
1855 if (lstat(new_src, &sb) == -1) {
1856 logit("%s: lstat failed: %s", filename,
1859 } else if (S_ISDIR(sb.st_mode)) {
1860 if (strcmp(filename, ".") == 0 ||
1861 strcmp(filename, "..") == 0)
1864 if (upload_dir_internal(conn, new_src, new_dst,
1865 depth + 1, preserve_flag, print_flag, resume,
1868 } else if (S_ISREG(sb.st_mode)) {
1869 if (do_upload(conn, new_src, new_dst,
1870 preserve_flag, resume, fsync_flag) == -1) {
1871 error("Uploading of file %s to %s failed!",
1876 logit("%s: not a regular file\n", filename);
1881 do_setstat(conn, dst, &a);
1883 (void) closedir(dirp);
1888 upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
1889 int preserve_flag, int print_flag, int resume, int fsync_flag)
1894 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1895 error("Unable to canonicalize path \"%s\"", dst);
1899 ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
1900 print_flag, resume, fsync_flag);
1907 path_append(const char *p1, const char *p2)
1910 size_t len = strlen(p1) + strlen(p2) + 2;
1913 strlcpy(ret, p1, len);
1914 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1915 strlcat(ret, "/", len);
1916 strlcat(ret, p2, len);