blob: dc4d8c4e06d4497e8c2ff039313c8a2906cf6e89 [file] [log] [blame]
djm@openbsd.orgf6a59a22018-05-25 04:25:46 +00001/* $OpenBSD: sftp-client.c,v 1.129 2018/05/25 04:25:46 djm Exp $ */
Damien Miller33804262001-02-04 23:20:18 +11002/*
Damien Miller4e60ed72004-02-17 17:07:59 +11003 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
Damien Miller33804262001-02-04 23:20:18 +11004 *
Damien Miller4e60ed72004-02-17 17:07:59 +11005 * 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.
Damien Miller33804262001-02-04 23:20:18 +11008 *
Damien Miller4e60ed72004-02-17 17:07:59 +11009 * 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.
Damien Miller33804262001-02-04 23:20:18 +110016 */
17
18/* XXX: memleaks */
19/* XXX: signed vs unsigned */
Damien Miller3db5f532002-02-13 14:10:32 +110020/* XXX: remove all logging, only return status codes */
Damien Miller33804262001-02-04 23:20:18 +110021/* XXX: copy between two remote sites */
22
23#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110024
25#include <sys/types.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100026#ifdef HAVE_SYS_STATVFS_H
Damien Millerd671e5a2008-05-19 14:53:33 +100027#include <sys/statvfs.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100028#endif
Damien Millerd7834352006-08-05 12:39:39 +100029#include "openbsd-compat/sys-queue.h"
Damien Millerf17883e2006-03-15 11:45:54 +110030#ifdef HAVE_SYS_STAT_H
31# include <sys/stat.h>
32#endif
Damien Miller9aec9192006-08-05 10:57:45 +100033#ifdef HAVE_SYS_TIME_H
34# include <sys/time.h>
35#endif
Damien Millerd7834352006-08-05 12:39:39 +100036#include <sys/uio.h>
Damien Miller57cf6382006-07-10 21:13:46 +100037
Darren Tucker1b0dd172009-10-07 08:37:48 +110038#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100039#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100040#include <fcntl.h>
41#include <signal.h>
Damien Millerd7834352006-08-05 12:39:39 +100042#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100043#include <stdio.h>
Damien Miller0600c702013-11-21 13:55:43 +110044#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100045#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100046#include <unistd.h>
Damien Miller16a13332002-02-13 14:03:56 +110047
Damien Miller33804262001-02-04 23:20:18 +110048#include "xmalloc.h"
djm@openbsd.org7d845f42015-01-14 13:54:13 +000049#include "ssherr.h"
50#include "sshbuf.h"
Damien Miller33804262001-02-04 23:20:18 +110051#include "log.h"
52#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110053#include "progressmeter.h"
Damien Miller3f941882006-03-31 23:13:02 +110054#include "misc.h"
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +000055#include "utf8.h"
Damien Miller33804262001-02-04 23:20:18 +110056
57#include "sftp.h"
58#include "sftp-common.h"
59#include "sftp-client.h"
60
Darren Tuckercdf547a2004-05-24 10:12:19 +100061extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110062extern int showprogress;
63
Damien Miller0c8d8f62006-03-15 11:34:25 +110064/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110065#define MIN_READ_SIZE 512
66
Darren Tucker1b0dd172009-10-07 08:37:48 +110067/* Maximum depth to descend in directory trees */
68#define MAX_DIR_DEPTH 64
69
Damien Miller89f04852017-03-20 11:53:34 +110070/* Directory separator characters */
71#ifdef HAVE_CYGWIN
72# define SFTP_DIRECTORY_CHARS "/\\"
73#else /* HAVE_CYGWIN */
74# define SFTP_DIRECTORY_CHARS "/"
75#endif /* HAVE_CYGWIN */
76
Damien Miller3db5f532002-02-13 14:10:32 +110077struct sftp_conn {
78 int fd_in;
79 int fd_out;
80 u_int transfer_buflen;
81 u_int num_requests;
82 u_int version;
83 u_int msg_id;
Damien Millerd671e5a2008-05-19 14:53:33 +100084#define SFTP_EXT_POSIX_RENAME 0x00000001
85#define SFTP_EXT_STATVFS 0x00000002
86#define SFTP_EXT_FSTATVFS 0x00000004
Darren Tuckeraf1f9092010-12-05 09:02:47 +110087#define SFTP_EXT_HARDLINK 0x00000008
Damien Millerf29238e2013-10-17 11:48:52 +110088#define SFTP_EXT_FSYNC 0x00000010
Damien Miller7a3e1d02008-03-27 10:59:57 +110089 u_int exts;
Damien Miller65e42f82010-09-24 22:15:11 +100090 u_int64_t limit_kbps;
91 struct bwlimit bwlimit_in, bwlimit_out;
Damien Miller3db5f532002-02-13 14:10:32 +110092};
Ben Lindstrom288cc392001-02-09 02:58:04 +000093
djm@openbsd.org7d845f42015-01-14 13:54:13 +000094static u_char *
95get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
Damien Miller65e42f82010-09-24 22:15:11 +100096 const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
97
98/* ARGSUSED */
99static int
100sftpio(void *_bwlimit, size_t amount)
101{
102 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
103
104 bandwidth_limit(bwlimit, amount);
105 return 0;
106}
Darren Tuckerc22f0902009-10-07 08:24:19 +1100107
Ben Lindstrombba81212001-06-25 05:01:22 +0000108static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000109send_msg(struct sftp_conn *conn, struct sshbuf *m)
Damien Miller33804262001-02-04 23:20:18 +1100110{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100111 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +1000112 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +1100113
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000114 if (sshbuf_len(m) > SFTP_MAX_MSG_LENGTH)
115 fatal("Outbound message too long %zu", sshbuf_len(m));
Damien Miller33804262001-02-04 23:20:18 +1100116
Damien Millera7f3aaa2003-01-10 21:43:58 +1100117 /* Send length first */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000118 put_u32(mlen, sshbuf_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +1000119 iov[0].iov_base = mlen;
120 iov[0].iov_len = sizeof(mlen);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000121 iov[1].iov_base = (u_char *)sshbuf_ptr(m);
122 iov[1].iov_len = sshbuf_len(m);
Damien Millerd7834352006-08-05 12:39:39 +1000123
Damien Miller65e42f82010-09-24 22:15:11 +1000124 if (atomiciov6(writev, conn->fd_out, iov, 2,
Damien Miller0d032412013-07-25 11:56:52 +1000125 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_out) !=
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000126 sshbuf_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +1100127 fatal("Couldn't send packet: %s", strerror(errno));
128
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000129 sshbuf_reset(m);
Damien Miller33804262001-02-04 23:20:18 +1100130}
131
Ben Lindstrombba81212001-06-25 05:01:22 +0000132static void
dtucker@openbsd.org3dde09a2017-11-28 21:10:22 +0000133get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
Damien Miller33804262001-02-04 23:20:18 +1100134{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100135 u_int msg_len;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000136 u_char *p;
137 int r;
Damien Miller33804262001-02-04 23:20:18 +1100138
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000139 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,
Damien Miller65e42f82010-09-24 22:15:11 +1000142 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in) != 4) {
djm@openbsd.org4b3ecbb2017-08-11 04:41:08 +0000143 if (errno == EPIPE || errno == ECONNRESET)
Damien Millerb253cc42005-05-26 12:23:44 +1000144 fatal("Connection closed");
145 else
146 fatal("Couldn't read packet: %s", strerror(errno));
147 }
Damien Miller33804262001-02-04 23:20:18 +1100148
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000149 if ((r = sshbuf_get_u32(m, &msg_len)) != 0)
150 fatal("%s: buffer error: %s", __func__, ssh_err(r));
dtucker@openbsd.org3dde09a2017-11-28 21:10:22 +0000151 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.");
156 }
Damien Miller33804262001-02-04 23:20:18 +1100157
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000158 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,
Damien Miller65e42f82010-09-24 22:15:11 +1000161 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in)
162 != msg_len) {
Damien Millerb253cc42005-05-26 12:23:44 +1000163 if (errno == EPIPE)
164 fatal("Connection closed");
165 else
166 fatal("Read packet: %s", strerror(errno));
167 }
Damien Miller33804262001-02-04 23:20:18 +1100168}
169
Ben Lindstrombba81212001-06-25 05:01:22 +0000170static void
dtucker@openbsd.org3dde09a2017-11-28 21:10:22 +0000171get_msg(struct sftp_conn *conn, struct sshbuf *m)
172{
173 get_msg_extended(conn, m, 0);
174}
175
176static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000177send_string_request(struct sftp_conn *conn, u_int id, u_int code, const char *s,
Damien Miller33804262001-02-04 23:20:18 +1100178 u_int len)
179{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000180 struct sshbuf *msg;
181 int r;
Damien Miller33804262001-02-04 23:20:18 +1100182
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000183 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));
189 send_msg(conn, msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000190 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000191 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100192}
193
Ben Lindstrombba81212001-06-25 05:01:22 +0000194static void
Damien Miller65e42f82010-09-24 22:15:11 +1000195send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000196 const void *s, u_int len, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100197{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000198 struct sshbuf *msg;
199 int r;
Damien Miller33804262001-02-04 23:20:18 +1100200
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000201 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));
208 send_msg(conn, msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000209 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000210 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100211}
212
Ben Lindstrombba81212001-06-25 05:01:22 +0000213static u_int
Damien Miller65e42f82010-09-24 22:15:11 +1000214get_status(struct sftp_conn *conn, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100215{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000216 struct sshbuf *msg;
217 u_char type;
218 u_int id, status;
219 int r;
Damien Miller33804262001-02-04 23:20:18 +1100220
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000221 if ((msg = sshbuf_new()) == NULL)
222 fatal("%s: sshbuf_new failed", __func__);
223 get_msg(conn, msg);
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));
Damien Miller33804262001-02-04 23:20:18 +1100227
228 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000229 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100230 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000231 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100232 SSH2_FXP_STATUS, type);
233
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000234 if ((r = sshbuf_get_u32(msg, &status)) != 0)
235 fatal("%s: buffer error: %s", __func__, ssh_err(r));
236 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100237
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000238 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100239
Damien Miller65e42f82010-09-24 22:15:11 +1000240 return status;
Damien Miller33804262001-02-04 23:20:18 +1100241}
242
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000243static u_char *
244get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
Damien Miller65e42f82010-09-24 22:15:11 +1000245 const char *errfmt, ...)
Damien Miller33804262001-02-04 23:20:18 +1100246{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000247 struct sshbuf *msg;
248 u_int id, status;
249 u_char type;
250 u_char *handle;
251 char errmsg[256];
Darren Tuckerc22f0902009-10-07 08:24:19 +1100252 va_list args;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000253 int r;
Darren Tuckerc22f0902009-10-07 08:24:19 +1100254
255 va_start(args, errfmt);
256 if (errfmt != NULL)
257 vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
258 va_end(args);
Damien Miller33804262001-02-04 23:20:18 +1100259
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000260 if ((msg = sshbuf_new()) == NULL)
261 fatal("%s: sshbuf_new failed", __func__);
262 get_msg(conn, msg);
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));
Damien Miller33804262001-02-04 23:20:18 +1100266
267 if (id != expected_id)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100268 fatal("%s: ID mismatch (%u != %u)",
269 errfmt == NULL ? __func__ : errmsg, id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100270 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000271 if ((r = sshbuf_get_u32(msg, &status)) != 0)
272 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerc22f0902009-10-07 08:24:19 +1100273 if (errfmt != NULL)
274 error("%s: %s", errmsg, fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000275 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100276 return(NULL);
277 } else if (type != SSH2_FXP_HANDLE)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100278 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
279 errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
Damien Miller33804262001-02-04 23:20:18 +1100280
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000281 if ((r = sshbuf_get_string(msg, &handle, len)) != 0)
282 fatal("%s: buffer error: %s", __func__, ssh_err(r));
283 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100284
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000285 return handle;
Damien Miller33804262001-02-04 23:20:18 +1100286}
287
Ben Lindstrombba81212001-06-25 05:01:22 +0000288static Attrib *
Damien Miller65e42f82010-09-24 22:15:11 +1000289get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100290{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000291 struct sshbuf *msg;
292 u_int id;
293 u_char type;
294 int r;
295 static Attrib a;
Damien Miller33804262001-02-04 23:20:18 +1100296
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000297 if ((msg = sshbuf_new()) == NULL)
298 fatal("%s: sshbuf_new failed", __func__);
299 get_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100300
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000301 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));
Damien Miller33804262001-02-04 23:20:18 +1100304
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000305 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100306 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000307 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100308 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000309 u_int status;
Damien Miller33804262001-02-04 23:20:18 +1100310
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000311 if ((r = sshbuf_get_u32(msg, &status)) != 0)
312 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000313 if (quiet)
314 debug("Couldn't stat remote file: %s", fx2txt(status));
315 else
316 error("Couldn't stat remote file: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000317 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100318 return(NULL);
319 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000320 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100321 SSH2_FXP_ATTRS, type);
322 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000323 if ((r = decode_attrib(msg, &a)) != 0) {
324 error("%s: couldn't decode attrib: %s", __func__, ssh_err(r));
325 sshbuf_free(msg);
326 return NULL;
327 }
328 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100329
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000330 return &a;
Damien Miller33804262001-02-04 23:20:18 +1100331}
332
Damien Millerd671e5a2008-05-19 14:53:33 +1000333static int
Damien Miller65e42f82010-09-24 22:15:11 +1000334get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
335 u_int expected_id, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000336{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000337 struct sshbuf *msg;
338 u_char type;
339 u_int id;
340 u_int64_t flag;
341 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +1000342
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000343 if ((msg = sshbuf_new()) == NULL)
344 fatal("%s: sshbuf_new failed", __func__);
345 get_msg(conn, msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000346
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000347 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));
Damien Millerd671e5a2008-05-19 14:53:33 +1000350
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) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000355 u_int status;
Damien Millerd671e5a2008-05-19 14:53:33 +1000356
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000357 if ((r = sshbuf_get_u32(msg, &status)) != 0)
358 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000359 if (quiet)
360 debug("Couldn't statvfs: %s", fx2txt(status));
361 else
362 error("Couldn't statvfs: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000363 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000364 return -1;
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);
368 }
369
Damien Miller1d2c4562014-02-04 11:18:20 +1100370 memset(st, 0, sizeof(*st));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000371 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));
Damien Millerd671e5a2008-05-19 14:53:33 +1000383
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;
386
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000387 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000388
389 return 0;
390}
391
Damien Miller3db5f532002-02-13 14:10:32 +1100392struct sftp_conn *
Damien Miller65e42f82010-09-24 22:15:11 +1000393do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
394 u_int64_t limit_kbps)
Damien Miller33804262001-02-04 23:20:18 +1100395{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000396 u_char type;
397 struct sshbuf *msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100398 struct sftp_conn *ret;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000399 int r;
Damien Miller33804262001-02-04 23:20:18 +1100400
Damien Millerfec029f2013-08-21 02:42:12 +1000401 ret = xcalloc(1, sizeof(*ret));
402 ret->msg_id = 1;
Damien Miller65e42f82010-09-24 22:15:11 +1000403 ret->fd_in = fd_in;
404 ret->fd_out = fd_out;
405 ret->transfer_buflen = transfer_buflen;
406 ret->num_requests = num_requests;
407 ret->exts = 0;
408 ret->limit_kbps = 0;
409
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000410 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));
415 send_msg(ret, msg);
Damien Miller33804262001-02-04 23:20:18 +1100416
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000417 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +1100418
dtucker@openbsd.org3dde09a2017-11-28 21:10:22 +0000419 get_msg_extended(ret, msg, 1);
Damien Miller33804262001-02-04 23:20:18 +1100420
Kevin Stevesef4eea92001-02-05 12:42:17 +0000421 /* Expecting a VERSION reply */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000422 if ((r = sshbuf_get_u8(msg, &type)) != 0)
423 fatal("%s: buffer error: %s", __func__, ssh_err(r));
424 if (type != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000425 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100426 type);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000427 sshbuf_free(msg);
jsg@openbsd.org64a89ec2015-05-23 14:28:37 +0000428 free(ret);
Damien Miller3db5f532002-02-13 14:10:32 +1100429 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100430 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000431 if ((r = sshbuf_get_u32(msg, &ret->version)) != 0)
432 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100433
Damien Miller65e42f82010-09-24 22:15:11 +1000434 debug2("Remote version: %u", ret->version);
Damien Miller33804262001-02-04 23:20:18 +1100435
436 /* Check for extensions */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000437 while (sshbuf_len(msg) > 0) {
438 char *name;
439 u_char *value;
440 size_t vlen;
Darren Tuckera64ab332008-06-13 07:01:29 +1000441 int known = 0;
Damien Miller33804262001-02-04 23:20:18 +1100442
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000443 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));
Damien Millerd671e5a2008-05-19 14:53:33 +1000446 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000447 strcmp((char *)value, "1") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000448 ret->exts |= SFTP_EXT_POSIX_RENAME;
Darren Tuckera64ab332008-06-13 07:01:29 +1000449 known = 1;
450 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000451 strcmp((char *)value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000452 ret->exts |= SFTP_EXT_STATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000453 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100454 } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000455 strcmp((char *)value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000456 ret->exts |= SFTP_EXT_FSTATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000457 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100458 } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000459 strcmp((char *)value, "1") == 0) {
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100460 ret->exts |= SFTP_EXT_HARDLINK;
461 known = 1;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000462 } else if (strcmp(name, "fsync@openssh.com") == 0 &&
463 strcmp((char *)value, "1") == 0) {
464 ret->exts |= SFTP_EXT_FSYNC;
465 known = 1;
Darren Tuckera64ab332008-06-13 07:01:29 +1000466 }
467 if (known) {
468 debug2("Server supports extension \"%s\" revision %s",
469 name, value);
470 } else {
471 debug2("Unrecognised server extension \"%s\"", name);
472 }
Darren Tuckera627d422013-06-02 07:31:17 +1000473 free(name);
474 free(value);
Damien Miller33804262001-02-04 23:20:18 +1100475 }
476
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000477 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +1100478
Damien Miller3db5f532002-02-13 14:10:32 +1100479 /* Some filexfer v.0 servers don't support large packets */
Damien Miller65e42f82010-09-24 22:15:11 +1000480 if (ret->version == 0)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000481 ret->transfer_buflen = MINIMUM(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100482
Damien Miller65e42f82010-09-24 22:15:11 +1000483 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);
489 }
490
491 return ret;
Damien Miller3db5f532002-02-13 14:10:32 +1100492}
493
494u_int
495sftp_proto_version(struct sftp_conn *conn)
496{
Damien Miller65e42f82010-09-24 22:15:11 +1000497 return conn->version;
Damien Miller33804262001-02-04 23:20:18 +1100498}
499
500int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000501do_close(struct sftp_conn *conn, const u_char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100502{
503 u_int id, status;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000504 struct sshbuf *msg;
505 int r;
Damien Miller33804262001-02-04 23:20:18 +1100506
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000507 if ((msg = sshbuf_new()) == NULL)
508 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100509
Damien Miller3db5f532002-02-13 14:10:32 +1100510 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000511 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));
515 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000516 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100517
Damien Miller65e42f82010-09-24 22:15:11 +1000518 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100519 if (status != SSH2_FX_OK)
520 error("Couldn't close file: %s", fx2txt(status));
521
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000522 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100523
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000524 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100525}
526
Damien Miller4870afd2001-03-14 10:27:09 +1100527
Ben Lindstrombba81212001-06-25 05:01:22 +0000528static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000529do_lsreaddir(struct sftp_conn *conn, const char *path, int print_flag,
Damien Miller4870afd2001-03-14 10:27:09 +1100530 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100531{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000532 struct sshbuf *msg;
533 u_int count, id, i, expected_id, ents = 0;
534 size_t handle_len;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000535 u_char type, *handle;
Damien Miller39392072013-12-07 10:31:08 +1100536 int status = SSH2_FX_FAILURE;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000537 int r;
Damien Miller39392072013-12-07 10:31:08 +1100538
539 if (dir)
540 *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +1100541
Damien Miller3db5f532002-02-13 14:10:32 +1100542 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100543
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000544 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));
550 send_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100551
Damien Miller65e42f82010-09-24 22:15:11 +1000552 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +1100553 "remote readdir(\"%s\")", path);
Damien Miller57c38ac2011-09-22 21:42:45 +1000554 if (handle == NULL) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000555 sshbuf_free(msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000556 return -1;
Damien Miller57c38ac2011-09-22 21:42:45 +1000557 }
Damien Miller33804262001-02-04 23:20:18 +1100558
Damien Miller4870afd2001-03-14 10:27:09 +1100559 if (dir) {
560 ents = 0;
Damien Miller6c81fee2013-11-08 12:19:55 +1100561 *dir = xcalloc(1, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100562 (*dir)[0] = NULL;
563 }
Damien Miller4870afd2001-03-14 10:27:09 +1100564
Darren Tuckercdf547a2004-05-24 10:12:19 +1000565 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100566 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100567
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000568 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100569
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000570 sshbuf_reset(msg);
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));
575 send_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100576
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000577 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +1100578
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000579 get_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100580
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000581 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));
Damien Miller33804262001-02-04 23:20:18 +1100584
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000585 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100586
587 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000588 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100589
590 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000591 u_int rstatus;
592
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)
Damien Miller33804262001-02-04 23:20:18 +1100598 break;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000599 error("Couldn't read directory: %s", fx2txt(rstatus));
Damien Miller39392072013-12-07 10:31:08 +1100600 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100601 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000602 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100603 SSH2_FXP_NAME, type);
604
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000605 if ((r = sshbuf_get_u32(msg, &count)) != 0)
606 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgc7995f22017-01-03 05:46:51 +0000607 if (count > SSHBUF_SIZE_MAX)
608 fatal("%s: nonsensical number of entries", __func__);
Damien Millerd7686fd2001-02-10 00:40:03 +1100609 if (count == 0)
610 break;
611 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100612 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100613 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000614 Attrib a;
Damien Miller33804262001-02-04 23:20:18 +1100615
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000616 if ((r = sshbuf_get_cstring(msg, &filename,
617 NULL)) != 0 ||
618 (r = sshbuf_get_cstring(msg, &longname,
619 NULL)) != 0)
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));
625 free(filename);
626 free(longname);
627 sshbuf_free(msg);
628 return -1;
629 }
Damien Miller33804262001-02-04 23:20:18 +1100630
Damien Millerbda5c842013-10-15 12:05:58 +1100631 if (print_flag)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000632 mprintf("%s\n", longname);
Damien Miller4870afd2001-03-14 10:27:09 +1100633
Darren Tucker1b0dd172009-10-07 08:37:48 +1100634 /*
635 * Directory entries should never contain '/'
636 * These can be used to attack recursive ops
637 * (e.g. send '../../../../etc/passwd')
638 */
Damien Miller89f04852017-03-20 11:53:34 +1100639 if (strpbrk(filename, SFTP_DIRECTORY_CHARS) != NULL) {
Darren Tucker1b0dd172009-10-07 08:37:48 +1100640 error("Server sent suspect path \"%s\" "
641 "during readdir of \"%s\"", filename, path);
Damien Miller39392072013-12-07 10:31:08 +1100642 } else if (dir) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000643 *dir = xreallocarray(*dir, ents + 2, sizeof(**dir));
Damien Miller6c81fee2013-11-08 12:19:55 +1100644 (*dir)[ents] = xcalloc(1, sizeof(***dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100645 (*dir)[ents]->filename = xstrdup(filename);
646 (*dir)[ents]->longname = xstrdup(longname);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000647 memcpy(&(*dir)[ents]->a, &a, sizeof(a));
Damien Miller4870afd2001-03-14 10:27:09 +1100648 (*dir)[++ents] = NULL;
649 }
Darren Tuckera627d422013-06-02 07:31:17 +1000650 free(filename);
651 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100652 }
653 }
Damien Miller39392072013-12-07 10:31:08 +1100654 status = 0;
Damien Miller33804262001-02-04 23:20:18 +1100655
Damien Miller39392072013-12-07 10:31:08 +1100656 out:
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000657 sshbuf_free(msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100658 do_close(conn, handle, handle_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000659 free(handle);
Damien Miller33804262001-02-04 23:20:18 +1100660
Damien Miller39392072013-12-07 10:31:08 +1100661 if (status != 0 && dir != NULL) {
662 /* Don't return results on error */
663 free_sftp_dirents(*dir);
664 *dir = NULL;
665 } else if (interrupted && dir != NULL && *dir != NULL) {
666 /* Don't return partial matches on interrupt */
Darren Tuckercdf547a2004-05-24 10:12:19 +1000667 free_sftp_dirents(*dir);
Damien Miller6c81fee2013-11-08 12:19:55 +1100668 *dir = xcalloc(1, sizeof(**dir));
Darren Tuckercdf547a2004-05-24 10:12:19 +1000669 **dir = NULL;
670 }
671
djm@openbsd.orgf6a59a22018-05-25 04:25:46 +0000672 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100673}
674
675int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000676do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100677{
Damien Miller3db5f532002-02-13 14:10:32 +1100678 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100679}
680
681void free_sftp_dirents(SFTP_DIRENT **s)
682{
683 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100684
Damien Miller39392072013-12-07 10:31:08 +1100685 if (s == NULL)
686 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100687 for (i = 0; s[i]; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000688 free(s[i]->filename);
689 free(s[i]->longname);
690 free(s[i]);
Damien Miller4870afd2001-03-14 10:27:09 +1100691 }
Darren Tuckera627d422013-06-02 07:31:17 +1000692 free(s);
Damien Miller4870afd2001-03-14 10:27:09 +1100693}
694
695int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000696do_rm(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100697{
698 u_int status, id;
699
700 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
701
Damien Miller3db5f532002-02-13 14:10:32 +1100702 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000703 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
704 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100705 if (status != SSH2_FX_OK)
706 error("Couldn't delete file: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000707 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100708}
709
710int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000711do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100712{
713 u_int status, id;
714
Damien Miller3db5f532002-02-13 14:10:32 +1100715 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000716 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100717 strlen(path), a);
718
Damien Miller65e42f82010-09-24 22:15:11 +1000719 status = get_status(conn, id);
Damien Millerbda5c842013-10-15 12:05:58 +1100720 if (status != SSH2_FX_OK && print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100721 error("Couldn't create directory: %s", fx2txt(status));
722
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000723 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100724}
725
726int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000727do_rmdir(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100728{
729 u_int status, id;
730
Damien Miller3db5f532002-02-13 14:10:32 +1100731 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000732 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100733 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100734
Damien Miller65e42f82010-09-24 22:15:11 +1000735 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100736 if (status != SSH2_FX_OK)
737 error("Couldn't remove directory: %s", fx2txt(status));
738
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000739 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100740}
741
742Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000743do_stat(struct sftp_conn *conn, const char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100744{
745 u_int id;
746
Damien Miller3db5f532002-02-13 14:10:32 +1100747 id = conn->msg_id++;
748
Damien Miller65e42f82010-09-24 22:15:11 +1000749 send_string_request(conn, id,
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000750 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100751 path, strlen(path));
752
Damien Miller65e42f82010-09-24 22:15:11 +1000753 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100754}
755
756Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000757do_lstat(struct sftp_conn *conn, const char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100758{
759 u_int id;
760
Damien Miller3db5f532002-02-13 14:10:32 +1100761 if (conn->version == 0) {
762 if (quiet)
763 debug("Server version does not support lstat operation");
764 else
Damien Miller996acd22003-04-09 20:59:48 +1000765 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000766 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100767 }
768
769 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000770 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100771 strlen(path));
772
Damien Miller65e42f82010-09-24 22:15:11 +1000773 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100774}
775
Damien Millercfe23d32008-02-10 22:20:44 +1100776#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100777Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000778do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
779 int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100780{
781 u_int id;
782
Damien Miller3db5f532002-02-13 14:10:32 +1100783 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000784 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
Damien Miller3db5f532002-02-13 14:10:32 +1100785 handle_len);
786
Damien Miller65e42f82010-09-24 22:15:11 +1000787 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100788}
Damien Millercfe23d32008-02-10 22:20:44 +1100789#endif
Damien Miller33804262001-02-04 23:20:18 +1100790
791int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000792do_setstat(struct sftp_conn *conn, const char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100793{
794 u_int status, id;
795
Damien Miller3db5f532002-02-13 14:10:32 +1100796 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000797 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100798 strlen(path), a);
799
Damien Miller65e42f82010-09-24 22:15:11 +1000800 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100801 if (status != SSH2_FX_OK)
802 error("Couldn't setstat on \"%s\": %s", path,
803 fx2txt(status));
804
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000805 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100806}
807
808int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000809do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100810 Attrib *a)
811{
812 u_int status, id;
813
Damien Miller3db5f532002-02-13 14:10:32 +1100814 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000815 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100816 handle_len, a);
817
Damien Miller65e42f82010-09-24 22:15:11 +1000818 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100819 if (status != SSH2_FX_OK)
820 error("Couldn't fsetstat: %s", fx2txt(status));
821
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000822 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100823}
824
825char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000826do_realpath(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100827{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000828 struct sshbuf *msg;
829 u_int expected_id, count, id;
Damien Miller33804262001-02-04 23:20:18 +1100830 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000831 Attrib a;
832 u_char type;
833 int r;
Damien Miller33804262001-02-04 23:20:18 +1100834
Damien Miller3db5f532002-02-13 14:10:32 +1100835 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000836 send_string_request(conn, id, SSH2_FXP_REALPATH, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100837 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100838
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000839 if ((msg = sshbuf_new()) == NULL)
840 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100841
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000842 get_msg(conn, msg);
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));
Damien Miller33804262001-02-04 23:20:18 +1100846
847 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000848 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100849
850 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000851 u_int status;
Damien Miller33804262001-02-04 23:20:18 +1100852
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000853 if ((r = sshbuf_get_u32(msg, &status)) != 0)
854 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf29238e2013-10-17 11:48:52 +1100855 error("Couldn't canonicalize: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000856 sshbuf_free(msg);
Damien Miller49566312010-06-26 09:38:23 +1000857 return NULL;
Damien Miller33804262001-02-04 23:20:18 +1100858 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000859 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100860 SSH2_FXP_NAME, type);
861
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000862 if ((r = sshbuf_get_u32(msg, &count)) != 0)
863 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100864 if (count != 1)
865 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
866
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000867 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));
Damien Miller33804262001-02-04 23:20:18 +1100871
Darren Tucker4908d442012-07-02 22:15:38 +1000872 debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000873 (unsigned long)a.size);
Damien Miller33804262001-02-04 23:20:18 +1100874
Darren Tuckera627d422013-06-02 07:31:17 +1000875 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100876
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000877 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100878
879 return(filename);
880}
881
882int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000883do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath,
Damien Millerc7dba122013-08-21 02:41:15 +1000884 int force_legacy)
Damien Miller33804262001-02-04 23:20:18 +1100885{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000886 struct sshbuf *msg;
Damien Miller33804262001-02-04 23:20:18 +1100887 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000888 int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
Damien Miller33804262001-02-04 23:20:18 +1100889
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000890 if ((msg = sshbuf_new()) == NULL)
891 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100892
893 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100894 id = conn->msg_id++;
Damien Millerc7dba122013-08-21 02:41:15 +1000895 if (use_ext) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000896 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));
Damien Miller7a3e1d02008-03-27 10:59:57 +1100901 } else {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000902 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));
Damien Miller7a3e1d02008-03-27 10:59:57 +1100905 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000906 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));
909 send_msg(conn, msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100910 debug3("Sent message %s \"%s\" -> \"%s\"",
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000911 use_ext ? "posix-rename@openssh.com" :
912 "SSH2_FXP_RENAME", oldpath, newpath);
913 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100914
Damien Miller65e42f82010-09-24 22:15:11 +1000915 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100916 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100917 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
918 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100919
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000920 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100921}
922
923int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000924do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100925{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000926 struct sshbuf *msg;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100927 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000928 int r;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100929
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100930 if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
931 error("Server does not support hardlink@openssh.com extension");
932 return -1;
933 }
934
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000935 if ((msg = sshbuf_new()) == NULL)
936 fatal("%s: sshbuf_new failed", __func__);
Damien Miller3decdba2011-09-22 21:41:05 +1000937
938 /* Send link request */
939 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000940 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));
946 send_msg(conn, msg);
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100947 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
948 oldpath, newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000949 sshbuf_free(msg);
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100950
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));
955
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000956 return status == SSH2_FX_OK ? 0 : -1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100957}
958
959int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000960do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100961{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000962 struct sshbuf *msg;
Damien Miller058316f2001-03-08 10:08:49 +1100963 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000964 int r;
Damien Miller058316f2001-03-08 10:08:49 +1100965
Damien Miller3db5f532002-02-13 14:10:32 +1100966 if (conn->version < 3) {
967 error("This server does not support the symlink operation");
968 return(SSH2_FX_OP_UNSUPPORTED);
969 }
970
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000971 if ((msg = sshbuf_new()) == NULL)
972 fatal("%s: sshbuf_new failed", __func__);
Damien Miller058316f2001-03-08 10:08:49 +1100973
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000974 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100975 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000976 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));
981 send_msg(conn, msg);
Damien Miller058316f2001-03-08 10:08:49 +1100982 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
983 newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000984 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +1100985
Damien Miller65e42f82010-09-24 22:15:11 +1000986 status = get_status(conn, id);
Damien Miller058316f2001-03-08 10:08:49 +1100987 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000988 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100989 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100990
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000991 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller058316f2001-03-08 10:08:49 +1100992}
993
Damien Millerf29238e2013-10-17 11:48:52 +1100994int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000995do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len)
Damien Millerf29238e2013-10-17 11:48:52 +1100996{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000997 struct sshbuf *msg;
Damien Millerf29238e2013-10-17 11:48:52 +1100998 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000999 int r;
Damien Millerf29238e2013-10-17 11:48:52 +11001000
1001 /* Silently return if the extension is not supported */
1002 if ((conn->exts & SFTP_EXT_FSYNC) == 0)
1003 return -1;
1004
Damien Millerf29238e2013-10-17 11:48:52 +11001005 /* Send fsync request */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001006 if ((msg = sshbuf_new()) == NULL)
1007 fatal("%s: sshbuf_new failed", __func__);
Damien Millerf29238e2013-10-17 11:48:52 +11001008 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001009 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);
Damien Millerf29238e2013-10-17 11:48:52 +11001015 debug3("Sent message fsync@openssh.com I:%u", id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001016 sshbuf_free(msg);
Damien Millerf29238e2013-10-17 11:48:52 +11001017
1018 status = get_status(conn, id);
1019 if (status != SSH2_FX_OK)
1020 error("Couldn't sync file: %s", fx2txt(status));
1021
djm@openbsd.orgf6a59a22018-05-25 04:25:46 +00001022 return status == SSH2_FX_OK ? 0 : -1;
Damien Millerf29238e2013-10-17 11:48:52 +11001023}
1024
Damien Millercfe23d32008-02-10 22:20:44 +11001025#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +11001026char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001027do_readlink(struct sftp_conn *conn, const char *path)
Damien Miller058316f2001-03-08 10:08:49 +11001028{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001029 struct sshbuf *msg;
1030 u_int expected_id, count, id;
Damien Miller058316f2001-03-08 10:08:49 +11001031 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001032 Attrib a;
1033 u_char type;
1034 int r;
Damien Miller058316f2001-03-08 10:08:49 +11001035
Damien Miller3db5f532002-02-13 14:10:32 +11001036 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +10001037 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +11001038
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001039 if ((msg = sshbuf_new()) == NULL)
1040 fatal("%s: sshbuf_new failed", __func__);
Damien Miller058316f2001-03-08 10:08:49 +11001041
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001042 get_msg(conn, msg);
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));
Damien Miller058316f2001-03-08 10:08:49 +11001046
1047 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001048 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +11001049
1050 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001051 u_int status;
Damien Miller058316f2001-03-08 10:08:49 +11001052
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001053 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1054 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001055 error("Couldn't readlink: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001056 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +11001057 return(NULL);
1058 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001059 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +11001060 SSH2_FXP_NAME, type);
1061
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001062 if ((r = sshbuf_get_u32(msg, &count)) != 0)
1063 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001064 if (count != 1)
1065 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
1066
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001067 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));
Damien Miller058316f2001-03-08 10:08:49 +11001071
1072 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
1073
Darren Tuckera627d422013-06-02 07:31:17 +10001074 free(longname);
Damien Miller058316f2001-03-08 10:08:49 +11001075
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001076 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +11001077
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001078 return filename;
Damien Miller058316f2001-03-08 10:08:49 +11001079}
Damien Millercfe23d32008-02-10 22:20:44 +11001080#endif
Damien Miller058316f2001-03-08 10:08:49 +11001081
Damien Millerd671e5a2008-05-19 14:53:33 +10001082int
Darren Tucker7b598892008-06-09 22:49:36 +10001083do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
Damien Millerd671e5a2008-05-19 14:53:33 +10001084 int quiet)
1085{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001086 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +10001087 u_int id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001088 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +10001089
1090 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
1091 error("Server does not support statvfs@openssh.com extension");
1092 return -1;
1093 }
1094
1095 id = conn->msg_id++;
1096
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001097 if ((msg = sshbuf_new()) == NULL)
1098 fatal("%s: sshbuf_new failed", __func__);
1099 sshbuf_reset(msg);
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);
1106 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +10001107
Damien Miller65e42f82010-09-24 22:15:11 +10001108 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +10001109}
1110
1111#ifdef notyet
1112int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001113do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
Darren Tucker7b598892008-06-09 22:49:36 +10001114 struct sftp_statvfs *st, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +10001115{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001116 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +10001117 u_int id;
1118
1119 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
1120 error("Server does not support fstatvfs@openssh.com extension");
1121 return -1;
1122 }
1123
1124 id = conn->msg_id++;
1125
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001126 if ((msg = sshbuf_new()) == NULL)
1127 fatal("%s: sshbuf_new failed", __func__);
1128 sshbuf_reset(msg);
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);
1135 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +10001136
Damien Miller65e42f82010-09-24 22:15:11 +10001137 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +10001138}
1139#endif
1140
Damien Miller16a13332002-02-13 14:03:56 +11001141static void
Damien Miller65e42f82010-09-24 22:15:11 +10001142send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001143 u_int len, const u_char *handle, u_int handle_len)
Damien Miller16a13332002-02-13 14:03:56 +11001144{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001145 struct sshbuf *msg;
1146 int r;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001147
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001148 if ((msg = sshbuf_new()) == NULL)
1149 fatal("%s: sshbuf_new failed", __func__);
1150 sshbuf_reset(msg);
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);
1158 sshbuf_free(msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001159}
Damien Miller16a13332002-02-13 14:03:56 +11001160
Damien Miller058316f2001-03-08 10:08:49 +11001161int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001162do_download(struct sftp_conn *conn, const char *remote_path,
1163 const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
1164 int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001165{
Darren Tucker1b0dd172009-10-07 08:37:48 +11001166 Attrib junk;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001167 struct sshbuf *msg;
1168 u_char *handle;
1169 int local_fd = -1, write_error;
1170 int read_error, write_errno, reordered = 0, r;
Damien Miller0d032412013-07-25 11:56:52 +10001171 u_int64_t offset = 0, size, highwater;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001172 u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
Damien Miller62d57f62003-01-10 21:43:24 +11001173 off_t progress_counter;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001174 size_t handle_len;
Damien Miller0d032412013-07-25 11:56:52 +10001175 struct stat st;
Damien Miller16a13332002-02-13 14:03:56 +11001176 struct request {
1177 u_int id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001178 size_t len;
Damien Miller16a13332002-02-13 14:03:56 +11001179 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001180 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +11001181 };
1182 TAILQ_HEAD(reqhead, request) requests;
1183 struct request *req;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001184 u_char type;
Damien Miller16a13332002-02-13 14:03:56 +11001185
1186 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +11001187
Darren Tucker1b0dd172009-10-07 08:37:48 +11001188 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1189 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001190
Damien Miller9e720282008-06-29 22:46:35 +10001191 /* Do not preserve set[ug]id here, as we do not preserve ownership */
Damien Miller33804262001-02-04 23:20:18 +11001192 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +11001193 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +11001194 else
1195 mode = 0666;
1196
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001197 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +11001198 (!S_ISREG(a->perm))) {
1199 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001200 return(-1);
1201 }
1202
Damien Miller16a13332002-02-13 14:03:56 +11001203 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1204 size = a->size;
1205 else
1206 size = 0;
1207
Damien Miller3db5f532002-02-13 14:10:32 +11001208 buflen = conn->transfer_buflen;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001209 if ((msg = sshbuf_new()) == NULL)
1210 fatal("%s: sshbuf_new failed", __func__);
1211
1212 attrib_clear(&junk); /* Send empty attributes */
Damien Miller33804262001-02-04 23:20:18 +11001213
1214 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001215 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001216 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);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001223 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001224
Damien Miller65e42f82010-09-24 22:15:11 +10001225 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001226 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001227 if (handle == NULL) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001228 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +11001229 return(-1);
1230 }
1231
Damien Millerbda5c842013-10-15 12:05:58 +11001232 local_fd = open(local_path,
1233 O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
Damien Miller3db5f532002-02-13 14:10:32 +11001234 if (local_fd == -1) {
1235 error("Couldn't open local file \"%s\" for writing: %s",
1236 local_path, strerror(errno));
Damien Miller0d032412013-07-25 11:56:52 +10001237 goto fail;
1238 }
1239 offset = highwater = 0;
Damien Millerbda5c842013-10-15 12:05:58 +11001240 if (resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001241 if (fstat(local_fd, &st) == -1) {
1242 error("Unable to stat local file \"%s\": %s",
1243 local_path, strerror(errno));
1244 goto fail;
1245 }
Damien Millerc3d483f2014-01-17 11:20:26 +11001246 if (st.st_size < 0) {
1247 error("\"%s\" has negative size", local_path);
1248 goto fail;
1249 }
1250 if ((u_int64_t)st.st_size > size) {
Damien Miller0d032412013-07-25 11:56:52 +10001251 error("Unable to resume download of \"%s\": "
1252 "local file is larger than remote", local_path);
1253 fail:
1254 do_close(conn, handle, handle_len);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001255 sshbuf_free(msg);
Damien Miller0d032412013-07-25 11:56:52 +10001256 free(handle);
Damien Miller9275df32013-12-05 10:26:32 +11001257 if (local_fd != -1)
1258 close(local_fd);
Damien Miller0d032412013-07-25 11:56:52 +10001259 return -1;
1260 }
1261 offset = highwater = st.st_size;
Damien Miller3db5f532002-02-13 14:10:32 +11001262 }
1263
Damien Miller33804262001-02-04 23:20:18 +11001264 /* Read from remote and write to local */
Damien Miller0d032412013-07-25 11:56:52 +10001265 write_error = read_error = write_errno = num_req = 0;
Damien Miller16a13332002-02-13 14:03:56 +11001266 max_req = 1;
Damien Miller0d032412013-07-25 11:56:52 +10001267 progress_counter = offset;
Damien Miller62d57f62003-01-10 21:43:24 +11001268
Damien Miller9ba30692004-03-08 23:12:02 +11001269 if (showprogress && size != 0)
1270 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001271
Damien Miller16a13332002-02-13 14:03:56 +11001272 while (num_req > 0 || max_req > 0) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001273 u_char *data;
1274 size_t len;
Damien Miller33804262001-02-04 23:20:18 +11001275
Darren Tuckercdf547a2004-05-24 10:12:19 +10001276 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001277 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001278 * allow outstanding requests to drain gracefully
1279 */
1280 if (interrupted) {
1281 if (num_req == 0) /* If we haven't started yet... */
1282 break;
1283 max_req = 0;
1284 }
1285
Damien Miller16a13332002-02-13 14:03:56 +11001286 /* Send some more requests */
1287 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001288 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +00001289 (unsigned long long)offset,
1290 (unsigned long long)offset + buflen - 1,
1291 num_req, max_req);
Damien Miller6c81fee2013-11-08 12:19:55 +11001292 req = xcalloc(1, sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +11001293 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001294 req->len = buflen;
1295 req->offset = offset;
1296 offset += buflen;
1297 num_req++;
1298 TAILQ_INSERT_TAIL(&requests, req, tq);
Damien Miller65e42f82010-09-24 22:15:11 +10001299 send_read_request(conn, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001300 req->len, handle, handle_len);
1301 }
Damien Miller33804262001-02-04 23:20:18 +11001302
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001303 sshbuf_reset(msg);
1304 get_msg(conn, msg);
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));
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001308 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001309
Damien Miller16a13332002-02-13 14:03:56 +11001310 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001311 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001312 req != NULL && req->id != id;
1313 req = TAILQ_NEXT(req, tq))
1314 ;
1315 if (req == NULL)
1316 fatal("Unexpected reply %u", id);
1317
1318 switch (type) {
1319 case SSH2_FXP_STATUS:
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001320 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1321 fatal("%s: buffer error: %s",
1322 __func__, ssh_err(r));
Damien Miller16a13332002-02-13 14:03:56 +11001323 if (status != SSH2_FX_EOF)
1324 read_error = 1;
1325 max_req = 0;
1326 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001327 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001328 num_req--;
1329 break;
1330 case SSH2_FXP_DATA:
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001331 if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1332 fatal("%s: buffer error: %s",
1333 __func__, ssh_err(r));
Ben Lindstromeb505452002-03-22 01:03:15 +00001334 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001335 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001336 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001337 if (len > req->len)
1338 fatal("Received more data than asked for "
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001339 "%zu > %zu", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001340 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001341 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001342 !write_error) {
1343 write_errno = errno;
1344 write_error = 1;
1345 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001346 }
Damien Miller0d032412013-07-25 11:56:52 +10001347 else if (!reordered && req->offset <= highwater)
1348 highwater = req->offset + len;
1349 else if (!reordered && req->offset > highwater)
1350 reordered = 1;
Damien Miller62d57f62003-01-10 21:43:24 +11001351 progress_counter += len;
Darren Tuckera627d422013-06-02 07:31:17 +10001352 free(data);
Damien Miller16a13332002-02-13 14:03:56 +11001353
1354 if (len == req->len) {
1355 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001356 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001357 num_req--;
1358 } else {
1359 /* Resend the request for the missing data */
1360 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001361 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001362 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001363 (unsigned long long)req->offset +
1364 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001365 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001366 req->len -= len;
1367 req->offset += len;
Damien Miller65e42f82010-09-24 22:15:11 +10001368 send_read_request(conn, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001369 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001370 /* Reduce the request size */
1371 if (len < buflen)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001372 buflen = MAXIMUM(MIN_READ_SIZE, len);
Damien Miller16a13332002-02-13 14:03:56 +11001373 }
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)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001379 (unsigned long long)offset,
1380 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001381 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001382 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001383 ++max_req;
1384 }
1385 }
1386 break;
1387 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001388 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001389 SSH2_FXP_DATA, type);
1390 }
Damien Miller33804262001-02-04 23:20:18 +11001391 }
Damien Miller33804262001-02-04 23:20:18 +11001392
Damien Miller62d57f62003-01-10 21:43:24 +11001393 if (showprogress && size)
1394 stop_progress_meter();
1395
Damien Miller16a13332002-02-13 14:03:56 +11001396 /* Sanity check */
1397 if (TAILQ_FIRST(&requests) != NULL)
1398 fatal("Transfer complete, but requests still in queue");
Damien Miller0d032412013-07-25 11:56:52 +10001399 /* Truncate at highest contiguous point to avoid holes on interrupt */
1400 if (read_error || write_error || interrupted) {
Damien Millerbda5c842013-10-15 12:05:58 +11001401 if (reordered && resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001402 error("Unable to resume download of \"%s\": "
1403 "server reordered requests", local_path);
1404 }
1405 debug("truncating at %llu", (unsigned long long)highwater);
djm@openbsd.org9cc68422015-05-28 04:50:53 +00001406 if (ftruncate(local_fd, highwater) == -1)
1407 error("ftruncate \"%s\": %s", local_path,
1408 strerror(errno));
Damien Miller0d032412013-07-25 11:56:52 +10001409 }
Damien Miller16a13332002-02-13 14:03:56 +11001410 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001411 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001412 remote_path, fx2txt(status));
Damien Millerfec029f2013-08-21 02:42:12 +10001413 status = -1;
Damien Miller3db5f532002-02-13 14:10:32 +11001414 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001415 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001416 error("Couldn't write to \"%s\": %s", local_path,
1417 strerror(write_errno));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001418 status = SSH2_FX_FAILURE;
Damien Miller3db5f532002-02-13 14:10:32 +11001419 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001420 } else {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001421 if (do_close(conn, handle, handle_len) != 0 || interrupted)
1422 status = SSH2_FX_FAILURE;
1423 else
1424 status = SSH2_FX_OK;
Damien Miller16a13332002-02-13 14:03:56 +11001425 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001426#ifdef HAVE_FCHMOD
Damien Millerbda5c842013-10-15 12:05:58 +11001427 if (preserve_flag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001428#else
Damien Millerbda5c842013-10-15 12:05:58 +11001429 if (preserve_flag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001430#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001431 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001432 strerror(errno));
Damien Millerbda5c842013-10-15 12:05:58 +11001433 if (preserve_flag &&
1434 (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
Damien Miller16a13332002-02-13 14:03:56 +11001435 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",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001441 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001442 }
Damien Millerf29238e2013-10-17 11:48:52 +11001443 if (fsync_flag) {
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));
1448 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001449 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001450 close(local_fd);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001451 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001452 free(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001453
djm@openbsd.orgf6a59a22018-05-25 04:25:46 +00001454 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +11001455}
1456
Darren Tucker1b0dd172009-10-07 08:37:48 +11001457static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001458download_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)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001461{
1462 int i, ret = 0;
1463 SFTP_DIRENT **dir_entries;
1464 char *filename, *new_src, *new_dst;
1465 mode_t mode = 0777;
1466
1467 if (depth >= MAX_DIR_DEPTH) {
1468 error("Maximum directory depth exceeded: %d levels", depth);
1469 return -1;
1470 }
1471
1472 if (dirattrib == NULL &&
1473 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1474 error("Unable to stat remote directory \"%s\"", src);
1475 return -1;
1476 }
1477 if (!S_ISDIR(dirattrib->perm)) {
1478 error("\"%s\" is not a directory", src);
1479 return -1;
1480 }
Damien Millerbda5c842013-10-15 12:05:58 +11001481 if (print_flag)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001482 mprintf("Retrieving %s\n", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001483
1484 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1485 mode = dirattrib->perm & 01777;
1486 else {
1487 debug("Server did not send permissions for "
1488 "directory \"%s\"", dst);
1489 }
1490
1491 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1492 error("mkdir %s: %s", dst, strerror(errno));
1493 return -1;
1494 }
1495
1496 if (do_readdir(conn, src, &dir_entries) == -1) {
1497 error("%s: Failed to get directory contents", src);
1498 return -1;
1499 }
1500
1501 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1502 filename = dir_entries[i]->filename;
1503
1504 new_dst = path_append(dst, filename);
1505 new_src = path_append(src, filename);
1506
1507 if (S_ISDIR(dir_entries[i]->a.perm)) {
1508 if (strcmp(filename, ".") == 0 ||
1509 strcmp(filename, "..") == 0)
1510 continue;
1511 if (download_dir_internal(conn, new_src, new_dst,
Damien Millerbda5c842013-10-15 12:05:58 +11001512 depth + 1, &(dir_entries[i]->a), preserve_flag,
Damien Millerf29238e2013-10-17 11:48:52 +11001513 print_flag, resume_flag, fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001514 ret = -1;
1515 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1516 if (do_download(conn, new_src, new_dst,
Damien Millerf29238e2013-10-17 11:48:52 +11001517 &(dir_entries[i]->a), preserve_flag,
1518 resume_flag, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001519 error("Download of file %s to %s failed",
1520 new_src, new_dst);
1521 ret = -1;
1522 }
1523 } else
1524 logit("%s: not a regular file\n", new_src);
1525
Darren Tuckera627d422013-06-02 07:31:17 +10001526 free(new_dst);
1527 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001528 }
1529
Damien Millerbda5c842013-10-15 12:05:58 +11001530 if (preserve_flag) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001531 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1532 struct timeval tv[2];
1533 tv[0].tv_sec = dirattrib->atime;
1534 tv[1].tv_sec = dirattrib->mtime;
1535 tv[0].tv_usec = tv[1].tv_usec = 0;
1536 if (utimes(dst, tv) == -1)
1537 error("Can't set times on \"%s\": %s",
1538 dst, strerror(errno));
1539 } else
1540 debug("Server did not send times for directory "
1541 "\"%s\"", dst);
1542 }
1543
1544 free_sftp_dirents(dir_entries);
1545
1546 return ret;
1547}
1548
1549int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001550download_dir(struct sftp_conn *conn, const char *src, const char *dst,
1551 Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1552 int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001553{
1554 char *src_canon;
1555 int ret;
1556
1557 if ((src_canon = do_realpath(conn, src)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001558 error("Unable to canonicalize path \"%s\"", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001559 return -1;
1560 }
1561
Damien Millerbda5c842013-10-15 12:05:58 +11001562 ret = download_dir_internal(conn, src_canon, dst, 0,
Damien Millerf29238e2013-10-17 11:48:52 +11001563 dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag);
Darren Tuckera627d422013-06-02 07:31:17 +10001564 free(src_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001565 return ret;
1566}
1567
Damien Miller33804262001-02-04 23:20:18 +11001568int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001569do_upload(struct sftp_conn *conn, const char *local_path,
1570 const char *remote_path, int preserve_flag, int resume, int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001571{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001572 int r, local_fd;
1573 u_int status = SSH2_FX_OK;
1574 u_int id;
1575 u_char type;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001576 off_t offset, progress_counter;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001577 u_char *handle, *data;
1578 struct sshbuf *msg;
Damien Miller33804262001-02-04 23:20:18 +11001579 struct stat sb;
Damien Millerd8accc02014-05-15 13:46:25 +10001580 Attrib a, *c = NULL;
Damien Miller16a13332002-02-13 14:03:56 +11001581 u_int32_t startid;
1582 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001583 struct outstanding_ack {
1584 u_int id;
1585 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001586 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001587 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001588 };
1589 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001590 struct outstanding_ack *ack = NULL;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001591 size_t handle_len;
Damien Miller5873dfd2002-02-13 14:04:37 +11001592
1593 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001594
1595 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1596 error("Couldn't open local file \"%s\" for reading: %s",
1597 local_path, strerror(errno));
1598 return(-1);
1599 }
1600 if (fstat(local_fd, &sb) == -1) {
1601 error("Couldn't fstat local file \"%s\": %s",
1602 local_path, strerror(errno));
1603 close(local_fd);
1604 return(-1);
1605 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001606 if (!S_ISREG(sb.st_mode)) {
1607 error("%s is not a regular file", local_path);
1608 close(local_fd);
1609 return(-1);
1610 }
Damien Miller33804262001-02-04 23:20:18 +11001611 stat_to_attrib(&sb, &a);
1612
1613 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1614 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1615 a.perm &= 0777;
Damien Millerbda5c842013-10-15 12:05:58 +11001616 if (!preserve_flag)
Damien Miller33804262001-02-04 23:20:18 +11001617 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1618
Damien Millerd8accc02014-05-15 13:46:25 +10001619 if (resume) {
1620 /* Get remote file size if it exists */
1621 if ((c = do_stat(conn, remote_path, 0)) == NULL) {
djm@openbsd.org34a01b22016-04-08 08:19:17 +00001622 close(local_fd);
Damien Millerd8accc02014-05-15 13:46:25 +10001623 return -1;
1624 }
1625
1626 if ((off_t)c->size >= sb.st_size) {
1627 error("destination file bigger or same size as "
1628 "source file");
1629 close(local_fd);
1630 return -1;
1631 }
1632
1633 if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
1634 close(local_fd);
1635 return -1;
1636 }
1637 }
1638
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001639 if ((msg = sshbuf_new()) == NULL)
1640 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001641
1642 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001643 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001644 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1645 (r = sshbuf_put_u32(msg, id)) != 0 ||
1646 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1647 (r = sshbuf_put_u32(msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|
1648 (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC))) != 0 ||
1649 (r = encode_attrib(msg, &a)) != 0)
1650 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1651 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001652 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001653
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001654 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +11001655
Damien Miller65e42f82010-09-24 22:15:11 +10001656 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001657 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001658 if (handle == NULL) {
1659 close(local_fd);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001660 sshbuf_free(msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001661 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001662 }
1663
Damien Miller16a13332002-02-13 14:03:56 +11001664 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001665 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001666
Damien Miller33804262001-02-04 23:20:18 +11001667 /* Read from local and write to remote */
Damien Millerd8accc02014-05-15 13:46:25 +10001668 offset = progress_counter = (resume ? c->size : 0);
Damien Miller62d57f62003-01-10 21:43:24 +11001669 if (showprogress)
Darren Tuckerc9a19912013-06-02 08:37:05 +10001670 start_progress_meter(local_path, sb.st_size,
1671 &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001672
Damien Miller9f0f5c62001-12-21 14:45:46 +11001673 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001674 int len;
Damien Miller33804262001-02-04 23:20:18 +11001675
1676 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001677 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001678 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001679 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001680 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001681 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001682 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001683 len = 0;
1684 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001685 len = read(local_fd, data, conn->transfer_buflen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001686 while ((len == -1) &&
1687 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
Damien Miller33804262001-02-04 23:20:18 +11001688
1689 if (len == -1)
1690 fatal("Couldn't read from \"%s\": %s", local_path,
1691 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001692
1693 if (len != 0) {
Damien Miller6c81fee2013-11-08 12:19:55 +11001694 ack = xcalloc(1, sizeof(*ack));
Damien Miller5873dfd2002-02-13 14:04:37 +11001695 ack->id = ++id;
1696 ack->offset = offset;
1697 ack->len = len;
1698 TAILQ_INSERT_TAIL(&acks, ack, tq);
1699
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001700 sshbuf_reset(msg);
1701 if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
1702 (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
1703 (r = sshbuf_put_string(msg, handle,
1704 handle_len)) != 0 ||
1705 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1706 (r = sshbuf_put_string(msg, data, len)) != 0)
1707 fatal("%s: buffer error: %s",
1708 __func__, ssh_err(r));
1709 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001710 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001711 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001712 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001713 break;
1714
Damien Miller5873dfd2002-02-13 14:04:37 +11001715 if (ack == NULL)
1716 fatal("Unexpected ACK %u", id);
1717
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001718 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001719 id - ackid >= conn->num_requests) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001720 u_int rid;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001721
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001722 sshbuf_reset(msg);
1723 get_msg(conn, msg);
1724 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1725 (r = sshbuf_get_u32(msg, &rid)) != 0)
1726 fatal("%s: buffer error: %s",
1727 __func__, ssh_err(r));
Damien Miller5873dfd2002-02-13 14:04:37 +11001728
1729 if (type != SSH2_FXP_STATUS)
1730 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1731 "got %d", SSH2_FXP_STATUS, type);
1732
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001733 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1734 fatal("%s: buffer error: %s",
1735 __func__, ssh_err(r));
1736 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller5873dfd2002-02-13 14:04:37 +11001737
1738 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001739 for (ack = TAILQ_FIRST(&acks);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001740 ack != NULL && ack->id != rid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001741 ack = TAILQ_NEXT(ack, tq))
1742 ;
1743 if (ack == NULL)
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001744 fatal("Can't find request for ID %u", rid);
Damien Miller5873dfd2002-02-13 14:04:37 +11001745 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001746 debug3("In write loop, ack for %u %u bytes at %lld",
1747 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001748 ++ackid;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001749 progress_counter += ack->len;
Darren Tuckera627d422013-06-02 07:31:17 +10001750 free(ack);
Damien Miller33804262001-02-04 23:20:18 +11001751 }
Damien Miller33804262001-02-04 23:20:18 +11001752 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001753 if (offset < 0)
1754 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001755 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001756 sshbuf_free(msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001757
Damien Miller62d57f62003-01-10 21:43:24 +11001758 if (showprogress)
1759 stop_progress_meter();
Darren Tuckera627d422013-06-02 07:31:17 +10001760 free(data);
Damien Miller33804262001-02-04 23:20:18 +11001761
Damien Milleracdf25b2008-02-10 22:27:24 +11001762 if (status != SSH2_FX_OK) {
1763 error("Couldn't write to remote file \"%s\": %s",
1764 remote_path, fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001765 status = SSH2_FX_FAILURE;
Damien Milleracdf25b2008-02-10 22:27:24 +11001766 }
1767
Damien Miller33804262001-02-04 23:20:18 +11001768 if (close(local_fd) == -1) {
1769 error("Couldn't close local file \"%s\": %s", local_path,
1770 strerror(errno));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001771 status = SSH2_FX_FAILURE;
Damien Miller33804262001-02-04 23:20:18 +11001772 }
1773
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001774 /* Override umask and utimes if asked */
Damien Millerbda5c842013-10-15 12:05:58 +11001775 if (preserve_flag)
Damien Miller3db5f532002-02-13 14:10:32 +11001776 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001777
Damien Millerf29238e2013-10-17 11:48:52 +11001778 if (fsync_flag)
1779 (void)do_fsync(conn, handle, handle_len);
1780
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001781 if (do_close(conn, handle, handle_len) != 0)
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001782 status = SSH2_FX_FAILURE;
1783
Darren Tuckera627d422013-06-02 07:31:17 +10001784 free(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001785
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001786 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +11001787}
Darren Tucker1b0dd172009-10-07 08:37:48 +11001788
1789static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001790upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1791 int depth, int preserve_flag, int print_flag, int resume, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001792{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001793 int ret = 0;
Darren Tucker1b0dd172009-10-07 08:37:48 +11001794 DIR *dirp;
1795 struct dirent *dp;
1796 char *filename, *new_src, *new_dst;
1797 struct stat sb;
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001798 Attrib a, *dirattrib;
Darren Tucker1b0dd172009-10-07 08:37:48 +11001799
1800 if (depth >= MAX_DIR_DEPTH) {
1801 error("Maximum directory depth exceeded: %d levels", depth);
1802 return -1;
1803 }
1804
1805 if (stat(src, &sb) == -1) {
1806 error("Couldn't stat directory \"%s\": %s",
1807 src, strerror(errno));
1808 return -1;
1809 }
1810 if (!S_ISDIR(sb.st_mode)) {
1811 error("\"%s\" is not a directory", src);
1812 return -1;
1813 }
Damien Millerbda5c842013-10-15 12:05:58 +11001814 if (print_flag)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001815 mprintf("Entering %s\n", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001816
1817 attrib_clear(&a);
1818 stat_to_attrib(&sb, &a);
1819 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1820 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1821 a.perm &= 01777;
Damien Millerbda5c842013-10-15 12:05:58 +11001822 if (!preserve_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001823 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
Damien Miller0d032412013-07-25 11:56:52 +10001824
Darren Tucker1b0dd172009-10-07 08:37:48 +11001825 /*
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001826 * sftp lacks a portable status value to match errno EEXIST,
1827 * so if we get a failure back then we must check whether
1828 * the path already existed and is a directory.
Darren Tucker1b0dd172009-10-07 08:37:48 +11001829 */
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001830 if (do_mkdir(conn, dst, &a, 0) != 0) {
1831 if ((dirattrib = do_stat(conn, dst, 0)) == NULL)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001832 return -1;
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001833 if (!S_ISDIR(dirattrib->perm)) {
1834 error("\"%s\" exists but is not a directory", dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001835 return -1;
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001836 }
Darren Tucker1b0dd172009-10-07 08:37:48 +11001837 }
1838
1839 if ((dirp = opendir(src)) == NULL) {
1840 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1841 return -1;
1842 }
Damien Miller0d032412013-07-25 11:56:52 +10001843
Darren Tucker1b0dd172009-10-07 08:37:48 +11001844 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1845 if (dp->d_ino == 0)
1846 continue;
1847 filename = dp->d_name;
1848 new_dst = path_append(dst, filename);
1849 new_src = path_append(src, filename);
1850
Darren Tucker438b4732009-10-11 21:52:10 +11001851 if (lstat(new_src, &sb) == -1) {
1852 logit("%s: lstat failed: %s", filename,
1853 strerror(errno));
1854 ret = -1;
1855 } else if (S_ISDIR(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001856 if (strcmp(filename, ".") == 0 ||
1857 strcmp(filename, "..") == 0)
1858 continue;
1859
1860 if (upload_dir_internal(conn, new_src, new_dst,
Damien Millerd8accc02014-05-15 13:46:25 +10001861 depth + 1, preserve_flag, print_flag, resume,
Damien Millerf29238e2013-10-17 11:48:52 +11001862 fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001863 ret = -1;
Darren Tucker438b4732009-10-11 21:52:10 +11001864 } else if (S_ISREG(sb.st_mode)) {
Damien Millerbda5c842013-10-15 12:05:58 +11001865 if (do_upload(conn, new_src, new_dst,
Damien Millerd8accc02014-05-15 13:46:25 +10001866 preserve_flag, resume, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001867 error("Uploading of file %s to %s failed!",
1868 new_src, new_dst);
1869 ret = -1;
1870 }
1871 } else
1872 logit("%s: not a regular file\n", filename);
Darren Tuckera627d422013-06-02 07:31:17 +10001873 free(new_dst);
1874 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001875 }
1876
1877 do_setstat(conn, dst, &a);
1878
1879 (void) closedir(dirp);
1880 return ret;
1881}
1882
1883int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001884upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
1885 int preserve_flag, int print_flag, int resume, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001886{
1887 char *dst_canon;
1888 int ret;
1889
1890 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001891 error("Unable to canonicalize path \"%s\"", dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001892 return -1;
1893 }
1894
Damien Miller2f93d052013-10-15 12:06:27 +11001895 ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
Damien Millerd8accc02014-05-15 13:46:25 +10001896 print_flag, resume, fsync_flag);
Damien Millerf29238e2013-10-17 11:48:52 +11001897
Darren Tuckera627d422013-06-02 07:31:17 +10001898 free(dst_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001899 return ret;
1900}
1901
1902char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001903path_append(const char *p1, const char *p2)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001904{
1905 char *ret;
1906 size_t len = strlen(p1) + strlen(p2) + 2;
1907
1908 ret = xmalloc(len);
1909 strlcpy(ret, p1, len);
1910 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1911 strlcat(ret, "/", len);
1912 strlcat(ret, p2, len);
1913
1914 return(ret);
1915}
1916