blob: 72f7fff7aa22277c772fa96878d83d426b94dec5 [file] [log] [blame]
djm@openbsd.org07f2c7f2019-10-04 04:31:59 +00001/* $OpenBSD: sftp-client.c,v 1.135 2019/10/04 04:31:59 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
djm@openbsd.org60d8c842019-01-16 23:23:45 +000089#define SFTP_EXT_LSETSTAT 0x00000020
Damien Miller7a3e1d02008-03-27 10:59:57 +110090 u_int exts;
Damien Miller65e42f82010-09-24 22:15:11 +100091 u_int64_t limit_kbps;
92 struct bwlimit bwlimit_in, bwlimit_out;
Damien Miller3db5f532002-02-13 14:10:32 +110093};
Ben Lindstrom288cc392001-02-09 02:58:04 +000094
djm@openbsd.org7d845f42015-01-14 13:54:13 +000095static u_char *
96get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
Damien Miller65e42f82010-09-24 22:15:11 +100097 const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
98
99/* ARGSUSED */
100static int
101sftpio(void *_bwlimit, size_t amount)
102{
103 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
104
dtucker@openbsd.orgbdc6c632019-01-24 16:52:17 +0000105 refresh_progress_meter(0);
dtucker@openbsd.org8976f1c2019-01-23 08:01:46 +0000106 if (bwlimit != NULL)
107 bandwidth_limit(bwlimit, amount);
Damien Miller65e42f82010-09-24 22:15:11 +1000108 return 0;
109}
Darren Tuckerc22f0902009-10-07 08:24:19 +1100110
Ben Lindstrombba81212001-06-25 05:01:22 +0000111static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000112send_msg(struct sftp_conn *conn, struct sshbuf *m)
Damien Miller33804262001-02-04 23:20:18 +1100113{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100114 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +1000115 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +1100116
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000117 if (sshbuf_len(m) > SFTP_MAX_MSG_LENGTH)
118 fatal("Outbound message too long %zu", sshbuf_len(m));
Damien Miller33804262001-02-04 23:20:18 +1100119
Damien Millera7f3aaa2003-01-10 21:43:58 +1100120 /* Send length first */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000121 put_u32(mlen, sshbuf_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +1000122 iov[0].iov_base = mlen;
123 iov[0].iov_len = sizeof(mlen);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000124 iov[1].iov_base = (u_char *)sshbuf_ptr(m);
125 iov[1].iov_len = sshbuf_len(m);
Damien Millerd7834352006-08-05 12:39:39 +1000126
dtucker@openbsd.org8976f1c2019-01-23 08:01:46 +0000127 if (atomiciov6(writev, conn->fd_out, iov, 2, sftpio,
128 conn->limit_kbps > 0 ? &conn->bwlimit_out : NULL) !=
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000129 sshbuf_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +1100130 fatal("Couldn't send packet: %s", strerror(errno));
131
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000132 sshbuf_reset(m);
Damien Miller33804262001-02-04 23:20:18 +1100133}
134
Ben Lindstrombba81212001-06-25 05:01:22 +0000135static void
dtucker@openbsd.org3dde09a2017-11-28 21:10:22 +0000136get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
Damien Miller33804262001-02-04 23:20:18 +1100137{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100138 u_int msg_len;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000139 u_char *p;
140 int r;
Damien Miller33804262001-02-04 23:20:18 +1100141
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000142 if ((r = sshbuf_reserve(m, 4, &p)) != 0)
143 fatal("%s: buffer error: %s", __func__, ssh_err(r));
dtucker@openbsd.org8976f1c2019-01-23 08:01:46 +0000144 if (atomicio6(read, conn->fd_in, p, 4, sftpio,
145 conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL) != 4) {
djm@openbsd.org4b3ecbb2017-08-11 04:41:08 +0000146 if (errno == EPIPE || errno == ECONNRESET)
Damien Millerb253cc42005-05-26 12:23:44 +1000147 fatal("Connection closed");
148 else
149 fatal("Couldn't read packet: %s", strerror(errno));
150 }
Damien Miller33804262001-02-04 23:20:18 +1100151
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000152 if ((r = sshbuf_get_u32(m, &msg_len)) != 0)
153 fatal("%s: buffer error: %s", __func__, ssh_err(r));
dtucker@openbsd.org3dde09a2017-11-28 21:10:22 +0000154 if (msg_len > SFTP_MAX_MSG_LENGTH) {
155 do_log2(initial ? SYSLOG_LEVEL_ERROR : SYSLOG_LEVEL_FATAL,
156 "Received message too long %u", msg_len);
157 fatal("Ensure the remote shell produces no output "
158 "for non-interactive sessions.");
159 }
Damien Miller33804262001-02-04 23:20:18 +1100160
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000161 if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
162 fatal("%s: buffer error: %s", __func__, ssh_err(r));
dtucker@openbsd.org8976f1c2019-01-23 08:01:46 +0000163 if (atomicio6(read, conn->fd_in, p, msg_len, sftpio,
164 conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL)
Damien Miller65e42f82010-09-24 22:15:11 +1000165 != msg_len) {
Damien Millerb253cc42005-05-26 12:23:44 +1000166 if (errno == EPIPE)
167 fatal("Connection closed");
168 else
169 fatal("Read packet: %s", strerror(errno));
170 }
Damien Miller33804262001-02-04 23:20:18 +1100171}
172
Ben Lindstrombba81212001-06-25 05:01:22 +0000173static void
dtucker@openbsd.org3dde09a2017-11-28 21:10:22 +0000174get_msg(struct sftp_conn *conn, struct sshbuf *m)
175{
176 get_msg_extended(conn, m, 0);
177}
178
179static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000180send_string_request(struct sftp_conn *conn, u_int id, u_int code, const char *s,
Damien Miller33804262001-02-04 23:20:18 +1100181 u_int len)
182{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000183 struct sshbuf *msg;
184 int r;
Damien Miller33804262001-02-04 23:20:18 +1100185
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000186 if ((msg = sshbuf_new()) == NULL)
187 fatal("%s: sshbuf_new failed", __func__);
188 if ((r = sshbuf_put_u8(msg, code)) != 0 ||
189 (r = sshbuf_put_u32(msg, id)) != 0 ||
190 (r = sshbuf_put_string(msg, s, len)) != 0)
191 fatal("%s: buffer error: %s", __func__, ssh_err(r));
192 send_msg(conn, msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000193 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000194 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100195}
196
Ben Lindstrombba81212001-06-25 05:01:22 +0000197static void
Damien Miller65e42f82010-09-24 22:15:11 +1000198send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000199 const void *s, u_int len, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100200{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000201 struct sshbuf *msg;
202 int r;
Damien Miller33804262001-02-04 23:20:18 +1100203
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000204 if ((msg = sshbuf_new()) == NULL)
205 fatal("%s: sshbuf_new failed", __func__);
206 if ((r = sshbuf_put_u8(msg, code)) != 0 ||
207 (r = sshbuf_put_u32(msg, id)) != 0 ||
208 (r = sshbuf_put_string(msg, s, len)) != 0 ||
209 (r = encode_attrib(msg, a)) != 0)
210 fatal("%s: buffer error: %s", __func__, ssh_err(r));
211 send_msg(conn, msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000212 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000213 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100214}
215
Ben Lindstrombba81212001-06-25 05:01:22 +0000216static u_int
Damien Miller65e42f82010-09-24 22:15:11 +1000217get_status(struct sftp_conn *conn, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100218{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000219 struct sshbuf *msg;
220 u_char type;
221 u_int id, status;
222 int r;
Damien Miller33804262001-02-04 23:20:18 +1100223
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000224 if ((msg = sshbuf_new()) == NULL)
225 fatal("%s: sshbuf_new failed", __func__);
226 get_msg(conn, msg);
227 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
228 (r = sshbuf_get_u32(msg, &id)) != 0)
229 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100230
231 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000232 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100233 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000234 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100235 SSH2_FXP_STATUS, type);
236
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000237 if ((r = sshbuf_get_u32(msg, &status)) != 0)
238 fatal("%s: buffer error: %s", __func__, ssh_err(r));
239 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100240
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000241 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100242
Damien Miller65e42f82010-09-24 22:15:11 +1000243 return status;
Damien Miller33804262001-02-04 23:20:18 +1100244}
245
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000246static u_char *
247get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
Damien Miller65e42f82010-09-24 22:15:11 +1000248 const char *errfmt, ...)
Damien Miller33804262001-02-04 23:20:18 +1100249{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000250 struct sshbuf *msg;
251 u_int id, status;
252 u_char type;
253 u_char *handle;
254 char errmsg[256];
Darren Tuckerc22f0902009-10-07 08:24:19 +1100255 va_list args;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000256 int r;
Darren Tuckerc22f0902009-10-07 08:24:19 +1100257
258 va_start(args, errfmt);
259 if (errfmt != NULL)
260 vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
261 va_end(args);
Damien Miller33804262001-02-04 23:20:18 +1100262
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000263 if ((msg = sshbuf_new()) == NULL)
264 fatal("%s: sshbuf_new failed", __func__);
265 get_msg(conn, msg);
266 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
267 (r = sshbuf_get_u32(msg, &id)) != 0)
268 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100269
270 if (id != expected_id)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100271 fatal("%s: ID mismatch (%u != %u)",
272 errfmt == NULL ? __func__ : errmsg, id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100273 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000274 if ((r = sshbuf_get_u32(msg, &status)) != 0)
275 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerc22f0902009-10-07 08:24:19 +1100276 if (errfmt != NULL)
277 error("%s: %s", errmsg, fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000278 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100279 return(NULL);
280 } else if (type != SSH2_FXP_HANDLE)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100281 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
282 errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
Damien Miller33804262001-02-04 23:20:18 +1100283
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000284 if ((r = sshbuf_get_string(msg, &handle, len)) != 0)
285 fatal("%s: buffer error: %s", __func__, ssh_err(r));
286 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100287
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000288 return handle;
Damien Miller33804262001-02-04 23:20:18 +1100289}
290
Ben Lindstrombba81212001-06-25 05:01:22 +0000291static Attrib *
Damien Miller65e42f82010-09-24 22:15:11 +1000292get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100293{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000294 struct sshbuf *msg;
295 u_int id;
296 u_char type;
297 int r;
298 static Attrib a;
Damien Miller33804262001-02-04 23:20:18 +1100299
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000300 if ((msg = sshbuf_new()) == NULL)
301 fatal("%s: sshbuf_new failed", __func__);
302 get_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100303
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000304 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
305 (r = sshbuf_get_u32(msg, &id)) != 0)
306 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100307
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000308 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100309 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000310 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100311 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000312 u_int status;
Damien Miller33804262001-02-04 23:20:18 +1100313
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000314 if ((r = sshbuf_get_u32(msg, &status)) != 0)
315 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000316 if (quiet)
317 debug("Couldn't stat remote file: %s", fx2txt(status));
318 else
319 error("Couldn't stat remote file: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000320 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100321 return(NULL);
322 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000323 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100324 SSH2_FXP_ATTRS, type);
325 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000326 if ((r = decode_attrib(msg, &a)) != 0) {
327 error("%s: couldn't decode attrib: %s", __func__, ssh_err(r));
328 sshbuf_free(msg);
329 return NULL;
330 }
331 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100332
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000333 return &a;
Damien Miller33804262001-02-04 23:20:18 +1100334}
335
Damien Millerd671e5a2008-05-19 14:53:33 +1000336static int
Damien Miller65e42f82010-09-24 22:15:11 +1000337get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
338 u_int expected_id, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000339{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000340 struct sshbuf *msg;
341 u_char type;
342 u_int id;
343 u_int64_t flag;
344 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +1000345
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000346 if ((msg = sshbuf_new()) == NULL)
347 fatal("%s: sshbuf_new failed", __func__);
348 get_msg(conn, msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000349
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000350 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
351 (r = sshbuf_get_u32(msg, &id)) != 0)
352 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000353
354 debug3("Received statvfs reply T:%u I:%u", type, id);
355 if (id != expected_id)
356 fatal("ID mismatch (%u != %u)", id, expected_id);
357 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000358 u_int status;
Damien Millerd671e5a2008-05-19 14:53:33 +1000359
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000360 if ((r = sshbuf_get_u32(msg, &status)) != 0)
361 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000362 if (quiet)
363 debug("Couldn't statvfs: %s", fx2txt(status));
364 else
365 error("Couldn't statvfs: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000366 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000367 return -1;
368 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
369 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
370 SSH2_FXP_EXTENDED_REPLY, type);
371 }
372
Damien Miller1d2c4562014-02-04 11:18:20 +1100373 memset(st, 0, sizeof(*st));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000374 if ((r = sshbuf_get_u64(msg, &st->f_bsize)) != 0 ||
375 (r = sshbuf_get_u64(msg, &st->f_frsize)) != 0 ||
376 (r = sshbuf_get_u64(msg, &st->f_blocks)) != 0 ||
377 (r = sshbuf_get_u64(msg, &st->f_bfree)) != 0 ||
378 (r = sshbuf_get_u64(msg, &st->f_bavail)) != 0 ||
379 (r = sshbuf_get_u64(msg, &st->f_files)) != 0 ||
380 (r = sshbuf_get_u64(msg, &st->f_ffree)) != 0 ||
381 (r = sshbuf_get_u64(msg, &st->f_favail)) != 0 ||
382 (r = sshbuf_get_u64(msg, &st->f_fsid)) != 0 ||
383 (r = sshbuf_get_u64(msg, &flag)) != 0 ||
384 (r = sshbuf_get_u64(msg, &st->f_namemax)) != 0)
385 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000386
387 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
388 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
389
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000390 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000391
392 return 0;
393}
394
Damien Miller3db5f532002-02-13 14:10:32 +1100395struct sftp_conn *
Damien Miller65e42f82010-09-24 22:15:11 +1000396do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
397 u_int64_t limit_kbps)
Damien Miller33804262001-02-04 23:20:18 +1100398{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000399 u_char type;
400 struct sshbuf *msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100401 struct sftp_conn *ret;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000402 int r;
Damien Miller33804262001-02-04 23:20:18 +1100403
Damien Millerfec029f2013-08-21 02:42:12 +1000404 ret = xcalloc(1, sizeof(*ret));
405 ret->msg_id = 1;
Damien Miller65e42f82010-09-24 22:15:11 +1000406 ret->fd_in = fd_in;
407 ret->fd_out = fd_out;
408 ret->transfer_buflen = transfer_buflen;
409 ret->num_requests = num_requests;
410 ret->exts = 0;
411 ret->limit_kbps = 0;
412
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000413 if ((msg = sshbuf_new()) == NULL)
414 fatal("%s: sshbuf_new failed", __func__);
415 if ((r = sshbuf_put_u8(msg, SSH2_FXP_INIT)) != 0 ||
416 (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0)
417 fatal("%s: buffer error: %s", __func__, ssh_err(r));
418 send_msg(ret, msg);
Damien Miller33804262001-02-04 23:20:18 +1100419
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000420 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +1100421
dtucker@openbsd.org3dde09a2017-11-28 21:10:22 +0000422 get_msg_extended(ret, msg, 1);
Damien Miller33804262001-02-04 23:20:18 +1100423
Kevin Stevesef4eea92001-02-05 12:42:17 +0000424 /* Expecting a VERSION reply */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000425 if ((r = sshbuf_get_u8(msg, &type)) != 0)
426 fatal("%s: buffer error: %s", __func__, ssh_err(r));
427 if (type != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000428 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100429 type);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000430 sshbuf_free(msg);
jsg@openbsd.org64a89ec2015-05-23 14:28:37 +0000431 free(ret);
Damien Miller3db5f532002-02-13 14:10:32 +1100432 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100433 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000434 if ((r = sshbuf_get_u32(msg, &ret->version)) != 0)
435 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100436
Damien Miller65e42f82010-09-24 22:15:11 +1000437 debug2("Remote version: %u", ret->version);
Damien Miller33804262001-02-04 23:20:18 +1100438
439 /* Check for extensions */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000440 while (sshbuf_len(msg) > 0) {
441 char *name;
442 u_char *value;
443 size_t vlen;
Darren Tuckera64ab332008-06-13 07:01:29 +1000444 int known = 0;
Damien Miller33804262001-02-04 23:20:18 +1100445
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000446 if ((r = sshbuf_get_cstring(msg, &name, NULL)) != 0 ||
447 (r = sshbuf_get_string(msg, &value, &vlen)) != 0)
448 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000449 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000450 strcmp((char *)value, "1") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000451 ret->exts |= SFTP_EXT_POSIX_RENAME;
Darren Tuckera64ab332008-06-13 07:01:29 +1000452 known = 1;
453 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000454 strcmp((char *)value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000455 ret->exts |= SFTP_EXT_STATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000456 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100457 } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000458 strcmp((char *)value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000459 ret->exts |= SFTP_EXT_FSTATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000460 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100461 } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000462 strcmp((char *)value, "1") == 0) {
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100463 ret->exts |= SFTP_EXT_HARDLINK;
464 known = 1;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000465 } else if (strcmp(name, "fsync@openssh.com") == 0 &&
466 strcmp((char *)value, "1") == 0) {
467 ret->exts |= SFTP_EXT_FSYNC;
468 known = 1;
djm@openbsd.org60d8c842019-01-16 23:23:45 +0000469 } else if (strcmp(name, "lsetstat@openssh.com") == 0 &&
470 strcmp((char *)value, "1") == 0) {
471 ret->exts |= SFTP_EXT_LSETSTAT;
472 known = 1;
Darren Tuckera64ab332008-06-13 07:01:29 +1000473 }
474 if (known) {
475 debug2("Server supports extension \"%s\" revision %s",
476 name, value);
477 } else {
478 debug2("Unrecognised server extension \"%s\"", name);
479 }
Darren Tuckera627d422013-06-02 07:31:17 +1000480 free(name);
481 free(value);
Damien Miller33804262001-02-04 23:20:18 +1100482 }
483
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000484 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +1100485
Damien Miller3db5f532002-02-13 14:10:32 +1100486 /* Some filexfer v.0 servers don't support large packets */
Damien Miller65e42f82010-09-24 22:15:11 +1000487 if (ret->version == 0)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000488 ret->transfer_buflen = MINIMUM(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100489
Damien Miller65e42f82010-09-24 22:15:11 +1000490 ret->limit_kbps = limit_kbps;
491 if (ret->limit_kbps > 0) {
492 bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
493 ret->transfer_buflen);
494 bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
495 ret->transfer_buflen);
496 }
497
498 return ret;
Damien Miller3db5f532002-02-13 14:10:32 +1100499}
500
501u_int
502sftp_proto_version(struct sftp_conn *conn)
503{
Damien Miller65e42f82010-09-24 22:15:11 +1000504 return conn->version;
Damien Miller33804262001-02-04 23:20:18 +1100505}
506
507int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000508do_close(struct sftp_conn *conn, const u_char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100509{
510 u_int id, status;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000511 struct sshbuf *msg;
512 int r;
Damien Miller33804262001-02-04 23:20:18 +1100513
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000514 if ((msg = sshbuf_new()) == NULL)
515 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100516
Damien Miller3db5f532002-02-13 14:10:32 +1100517 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000518 if ((r = sshbuf_put_u8(msg, SSH2_FXP_CLOSE)) != 0 ||
519 (r = sshbuf_put_u32(msg, id)) != 0 ||
520 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
521 fatal("%s: buffer error: %s", __func__, ssh_err(r));
522 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000523 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100524
Damien Miller65e42f82010-09-24 22:15:11 +1000525 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100526 if (status != SSH2_FX_OK)
527 error("Couldn't close file: %s", fx2txt(status));
528
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000529 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100530
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000531 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100532}
533
Damien Miller4870afd2001-03-14 10:27:09 +1100534
Ben Lindstrombba81212001-06-25 05:01:22 +0000535static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000536do_lsreaddir(struct sftp_conn *conn, const char *path, int print_flag,
Damien Miller4870afd2001-03-14 10:27:09 +1100537 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100538{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000539 struct sshbuf *msg;
540 u_int count, id, i, expected_id, ents = 0;
541 size_t handle_len;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000542 u_char type, *handle;
Damien Miller39392072013-12-07 10:31:08 +1100543 int status = SSH2_FX_FAILURE;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000544 int r;
Damien Miller39392072013-12-07 10:31:08 +1100545
546 if (dir)
547 *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +1100548
Damien Miller3db5f532002-02-13 14:10:32 +1100549 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100550
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000551 if ((msg = sshbuf_new()) == NULL)
552 fatal("%s: sshbuf_new failed", __func__);
553 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPENDIR)) != 0 ||
554 (r = sshbuf_put_u32(msg, id)) != 0 ||
555 (r = sshbuf_put_cstring(msg, path)) != 0)
556 fatal("%s: buffer error: %s", __func__, ssh_err(r));
557 send_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100558
Damien Miller65e42f82010-09-24 22:15:11 +1000559 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +1100560 "remote readdir(\"%s\")", path);
Damien Miller57c38ac2011-09-22 21:42:45 +1000561 if (handle == NULL) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000562 sshbuf_free(msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000563 return -1;
Damien Miller57c38ac2011-09-22 21:42:45 +1000564 }
Damien Miller33804262001-02-04 23:20:18 +1100565
Damien Miller4870afd2001-03-14 10:27:09 +1100566 if (dir) {
567 ents = 0;
Damien Miller6c81fee2013-11-08 12:19:55 +1100568 *dir = xcalloc(1, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100569 (*dir)[0] = NULL;
570 }
Damien Miller4870afd2001-03-14 10:27:09 +1100571
Darren Tuckercdf547a2004-05-24 10:12:19 +1000572 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100573 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100574
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000575 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100576
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000577 sshbuf_reset(msg);
578 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READDIR)) != 0 ||
579 (r = sshbuf_put_u32(msg, id)) != 0 ||
580 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
581 fatal("%s: buffer error: %s", __func__, ssh_err(r));
582 send_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100583
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000584 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +1100585
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000586 get_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100587
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000588 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
589 (r = sshbuf_get_u32(msg, &id)) != 0)
590 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100591
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000592 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100593
594 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000595 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100596
597 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000598 u_int rstatus;
599
600 if ((r = sshbuf_get_u32(msg, &rstatus)) != 0)
601 fatal("%s: buffer error: %s",
602 __func__, ssh_err(r));
603 debug3("Received SSH2_FXP_STATUS %d", rstatus);
604 if (rstatus == SSH2_FX_EOF)
Damien Miller33804262001-02-04 23:20:18 +1100605 break;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000606 error("Couldn't read directory: %s", fx2txt(rstatus));
Damien Miller39392072013-12-07 10:31:08 +1100607 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100608 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000609 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100610 SSH2_FXP_NAME, type);
611
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000612 if ((r = sshbuf_get_u32(msg, &count)) != 0)
613 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgc7995f22017-01-03 05:46:51 +0000614 if (count > SSHBUF_SIZE_MAX)
615 fatal("%s: nonsensical number of entries", __func__);
Damien Millerd7686fd2001-02-10 00:40:03 +1100616 if (count == 0)
617 break;
618 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100619 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100620 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000621 Attrib a;
Damien Miller33804262001-02-04 23:20:18 +1100622
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000623 if ((r = sshbuf_get_cstring(msg, &filename,
624 NULL)) != 0 ||
625 (r = sshbuf_get_cstring(msg, &longname,
626 NULL)) != 0)
627 fatal("%s: buffer error: %s",
628 __func__, ssh_err(r));
629 if ((r = decode_attrib(msg, &a)) != 0) {
630 error("%s: couldn't decode attrib: %s",
631 __func__, ssh_err(r));
632 free(filename);
633 free(longname);
djm@openbsd.org07f2c7f2019-10-04 04:31:59 +0000634 goto out;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000635 }
Damien Miller33804262001-02-04 23:20:18 +1100636
Damien Millerbda5c842013-10-15 12:05:58 +1100637 if (print_flag)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000638 mprintf("%s\n", longname);
Damien Miller4870afd2001-03-14 10:27:09 +1100639
Darren Tucker1b0dd172009-10-07 08:37:48 +1100640 /*
641 * Directory entries should never contain '/'
642 * These can be used to attack recursive ops
643 * (e.g. send '../../../../etc/passwd')
644 */
Damien Miller89f04852017-03-20 11:53:34 +1100645 if (strpbrk(filename, SFTP_DIRECTORY_CHARS) != NULL) {
Darren Tucker1b0dd172009-10-07 08:37:48 +1100646 error("Server sent suspect path \"%s\" "
647 "during readdir of \"%s\"", filename, path);
Damien Miller39392072013-12-07 10:31:08 +1100648 } else if (dir) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000649 *dir = xreallocarray(*dir, ents + 2, sizeof(**dir));
Damien Miller6c81fee2013-11-08 12:19:55 +1100650 (*dir)[ents] = xcalloc(1, sizeof(***dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100651 (*dir)[ents]->filename = xstrdup(filename);
652 (*dir)[ents]->longname = xstrdup(longname);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000653 memcpy(&(*dir)[ents]->a, &a, sizeof(a));
Damien Miller4870afd2001-03-14 10:27:09 +1100654 (*dir)[++ents] = NULL;
655 }
Darren Tuckera627d422013-06-02 07:31:17 +1000656 free(filename);
657 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100658 }
659 }
Damien Miller39392072013-12-07 10:31:08 +1100660 status = 0;
Damien Miller33804262001-02-04 23:20:18 +1100661
Damien Miller39392072013-12-07 10:31:08 +1100662 out:
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000663 sshbuf_free(msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100664 do_close(conn, handle, handle_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000665 free(handle);
Damien Miller33804262001-02-04 23:20:18 +1100666
Damien Miller39392072013-12-07 10:31:08 +1100667 if (status != 0 && dir != NULL) {
668 /* Don't return results on error */
669 free_sftp_dirents(*dir);
670 *dir = NULL;
671 } else if (interrupted && dir != NULL && *dir != NULL) {
672 /* Don't return partial matches on interrupt */
Darren Tuckercdf547a2004-05-24 10:12:19 +1000673 free_sftp_dirents(*dir);
Damien Miller6c81fee2013-11-08 12:19:55 +1100674 *dir = xcalloc(1, sizeof(**dir));
Darren Tuckercdf547a2004-05-24 10:12:19 +1000675 **dir = NULL;
676 }
677
djm@openbsd.orgf6a59a22018-05-25 04:25:46 +0000678 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100679}
680
681int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000682do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100683{
Damien Miller3db5f532002-02-13 14:10:32 +1100684 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100685}
686
687void free_sftp_dirents(SFTP_DIRENT **s)
688{
689 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100690
Damien Miller39392072013-12-07 10:31:08 +1100691 if (s == NULL)
692 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100693 for (i = 0; s[i]; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000694 free(s[i]->filename);
695 free(s[i]->longname);
696 free(s[i]);
Damien Miller4870afd2001-03-14 10:27:09 +1100697 }
Darren Tuckera627d422013-06-02 07:31:17 +1000698 free(s);
Damien Miller4870afd2001-03-14 10:27:09 +1100699}
700
701int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000702do_rm(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100703{
704 u_int status, id;
705
706 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
707
Damien Miller3db5f532002-02-13 14:10:32 +1100708 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000709 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
710 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100711 if (status != SSH2_FX_OK)
712 error("Couldn't delete file: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000713 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100714}
715
716int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000717do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100718{
719 u_int status, id;
720
Damien Miller3db5f532002-02-13 14:10:32 +1100721 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000722 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100723 strlen(path), a);
724
Damien Miller65e42f82010-09-24 22:15:11 +1000725 status = get_status(conn, id);
Damien Millerbda5c842013-10-15 12:05:58 +1100726 if (status != SSH2_FX_OK && print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100727 error("Couldn't create directory: %s", fx2txt(status));
728
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000729 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100730}
731
732int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000733do_rmdir(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100734{
735 u_int status, id;
736
Damien Miller3db5f532002-02-13 14:10:32 +1100737 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000738 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100739 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100740
Damien Miller65e42f82010-09-24 22:15:11 +1000741 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100742 if (status != SSH2_FX_OK)
743 error("Couldn't remove directory: %s", fx2txt(status));
744
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000745 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100746}
747
748Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000749do_stat(struct sftp_conn *conn, const char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100750{
751 u_int id;
752
Damien Miller3db5f532002-02-13 14:10:32 +1100753 id = conn->msg_id++;
754
Damien Miller65e42f82010-09-24 22:15:11 +1000755 send_string_request(conn, id,
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000756 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100757 path, strlen(path));
758
Damien Miller65e42f82010-09-24 22:15:11 +1000759 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100760}
761
762Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000763do_lstat(struct sftp_conn *conn, const char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100764{
765 u_int id;
766
Damien Miller3db5f532002-02-13 14:10:32 +1100767 if (conn->version == 0) {
768 if (quiet)
769 debug("Server version does not support lstat operation");
770 else
Damien Miller996acd22003-04-09 20:59:48 +1000771 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000772 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100773 }
774
775 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000776 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100777 strlen(path));
778
Damien Miller65e42f82010-09-24 22:15:11 +1000779 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100780}
781
Damien Millercfe23d32008-02-10 22:20:44 +1100782#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100783Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000784do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
785 int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100786{
787 u_int id;
788
Damien Miller3db5f532002-02-13 14:10:32 +1100789 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000790 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
Damien Miller3db5f532002-02-13 14:10:32 +1100791 handle_len);
792
Damien Miller65e42f82010-09-24 22:15:11 +1000793 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100794}
Damien Millercfe23d32008-02-10 22:20:44 +1100795#endif
Damien Miller33804262001-02-04 23:20:18 +1100796
797int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000798do_setstat(struct sftp_conn *conn, const char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100799{
800 u_int status, id;
801
Damien Miller3db5f532002-02-13 14:10:32 +1100802 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000803 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100804 strlen(path), a);
805
Damien Miller65e42f82010-09-24 22:15:11 +1000806 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100807 if (status != SSH2_FX_OK)
808 error("Couldn't setstat on \"%s\": %s", path,
809 fx2txt(status));
810
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000811 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100812}
813
814int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000815do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100816 Attrib *a)
817{
818 u_int status, id;
819
Damien Miller3db5f532002-02-13 14:10:32 +1100820 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000821 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100822 handle_len, a);
823
Damien Miller65e42f82010-09-24 22:15:11 +1000824 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100825 if (status != SSH2_FX_OK)
826 error("Couldn't fsetstat: %s", fx2txt(status));
827
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000828 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100829}
830
831char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000832do_realpath(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100833{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000834 struct sshbuf *msg;
835 u_int expected_id, count, id;
Damien Miller33804262001-02-04 23:20:18 +1100836 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000837 Attrib a;
838 u_char type;
839 int r;
Damien Miller33804262001-02-04 23:20:18 +1100840
Damien Miller3db5f532002-02-13 14:10:32 +1100841 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000842 send_string_request(conn, id, SSH2_FXP_REALPATH, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100843 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100844
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000845 if ((msg = sshbuf_new()) == NULL)
846 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100847
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000848 get_msg(conn, msg);
849 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
850 (r = sshbuf_get_u32(msg, &id)) != 0)
851 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100852
853 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000854 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100855
856 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000857 u_int status;
Damien Miller33804262001-02-04 23:20:18 +1100858
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000859 if ((r = sshbuf_get_u32(msg, &status)) != 0)
860 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf29238e2013-10-17 11:48:52 +1100861 error("Couldn't canonicalize: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000862 sshbuf_free(msg);
Damien Miller49566312010-06-26 09:38:23 +1000863 return NULL;
Damien Miller33804262001-02-04 23:20:18 +1100864 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000865 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100866 SSH2_FXP_NAME, type);
867
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000868 if ((r = sshbuf_get_u32(msg, &count)) != 0)
869 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100870 if (count != 1)
871 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
872
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000873 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
874 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
875 (r = decode_attrib(msg, &a)) != 0)
876 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100877
Darren Tucker4908d442012-07-02 22:15:38 +1000878 debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000879 (unsigned long)a.size);
Damien Miller33804262001-02-04 23:20:18 +1100880
Darren Tuckera627d422013-06-02 07:31:17 +1000881 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100882
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000883 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100884
885 return(filename);
886}
887
888int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000889do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath,
Damien Millerc7dba122013-08-21 02:41:15 +1000890 int force_legacy)
Damien Miller33804262001-02-04 23:20:18 +1100891{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000892 struct sshbuf *msg;
Damien Miller33804262001-02-04 23:20:18 +1100893 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000894 int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
Damien Miller33804262001-02-04 23:20:18 +1100895
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000896 if ((msg = sshbuf_new()) == NULL)
897 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100898
899 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100900 id = conn->msg_id++;
Damien Millerc7dba122013-08-21 02:41:15 +1000901 if (use_ext) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000902 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
903 (r = sshbuf_put_u32(msg, id)) != 0 ||
904 (r = sshbuf_put_cstring(msg,
905 "posix-rename@openssh.com")) != 0)
906 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7a3e1d02008-03-27 10:59:57 +1100907 } else {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000908 if ((r = sshbuf_put_u8(msg, SSH2_FXP_RENAME)) != 0 ||
909 (r = sshbuf_put_u32(msg, id)) != 0)
910 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7a3e1d02008-03-27 10:59:57 +1100911 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000912 if ((r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
913 (r = sshbuf_put_cstring(msg, newpath)) != 0)
914 fatal("%s: buffer error: %s", __func__, ssh_err(r));
915 send_msg(conn, msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100916 debug3("Sent message %s \"%s\" -> \"%s\"",
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000917 use_ext ? "posix-rename@openssh.com" :
918 "SSH2_FXP_RENAME", oldpath, newpath);
919 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100920
Damien Miller65e42f82010-09-24 22:15:11 +1000921 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100922 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100923 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
924 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100925
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000926 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100927}
928
929int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000930do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100931{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000932 struct sshbuf *msg;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100933 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000934 int r;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100935
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100936 if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
937 error("Server does not support hardlink@openssh.com extension");
938 return -1;
939 }
940
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000941 if ((msg = sshbuf_new()) == NULL)
942 fatal("%s: sshbuf_new failed", __func__);
Damien Miller3decdba2011-09-22 21:41:05 +1000943
944 /* Send link request */
945 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000946 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
947 (r = sshbuf_put_u32(msg, id)) != 0 ||
948 (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
949 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
950 (r = sshbuf_put_cstring(msg, newpath)) != 0)
951 fatal("%s: buffer error: %s", __func__, ssh_err(r));
952 send_msg(conn, msg);
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100953 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
954 oldpath, newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000955 sshbuf_free(msg);
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100956
957 status = get_status(conn, id);
958 if (status != SSH2_FX_OK)
959 error("Couldn't link file \"%s\" to \"%s\": %s", oldpath,
960 newpath, fx2txt(status));
961
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000962 return status == SSH2_FX_OK ? 0 : -1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100963}
964
965int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000966do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100967{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000968 struct sshbuf *msg;
Damien Miller058316f2001-03-08 10:08:49 +1100969 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000970 int r;
Damien Miller058316f2001-03-08 10:08:49 +1100971
Damien Miller3db5f532002-02-13 14:10:32 +1100972 if (conn->version < 3) {
973 error("This server does not support the symlink operation");
974 return(SSH2_FX_OP_UNSUPPORTED);
975 }
976
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000977 if ((msg = sshbuf_new()) == NULL)
978 fatal("%s: sshbuf_new failed", __func__);
Damien Miller058316f2001-03-08 10:08:49 +1100979
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000980 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100981 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000982 if ((r = sshbuf_put_u8(msg, SSH2_FXP_SYMLINK)) != 0 ||
983 (r = sshbuf_put_u32(msg, id)) != 0 ||
984 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
985 (r = sshbuf_put_cstring(msg, newpath)) != 0)
986 fatal("%s: buffer error: %s", __func__, ssh_err(r));
987 send_msg(conn, msg);
Damien Miller058316f2001-03-08 10:08:49 +1100988 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
989 newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000990 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +1100991
Damien Miller65e42f82010-09-24 22:15:11 +1000992 status = get_status(conn, id);
Damien Miller058316f2001-03-08 10:08:49 +1100993 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000994 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100995 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100996
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000997 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller058316f2001-03-08 10:08:49 +1100998}
999
Damien Millerf29238e2013-10-17 11:48:52 +11001000int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001001do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len)
Damien Millerf29238e2013-10-17 11:48:52 +11001002{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001003 struct sshbuf *msg;
Damien Millerf29238e2013-10-17 11:48:52 +11001004 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001005 int r;
Damien Millerf29238e2013-10-17 11:48:52 +11001006
1007 /* Silently return if the extension is not supported */
1008 if ((conn->exts & SFTP_EXT_FSYNC) == 0)
1009 return -1;
1010
Damien Millerf29238e2013-10-17 11:48:52 +11001011 /* Send fsync request */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001012 if ((msg = sshbuf_new()) == NULL)
1013 fatal("%s: sshbuf_new failed", __func__);
Damien Millerf29238e2013-10-17 11:48:52 +11001014 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001015 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1016 (r = sshbuf_put_u32(msg, id)) != 0 ||
1017 (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
1018 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1019 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1020 send_msg(conn, msg);
Damien Millerf29238e2013-10-17 11:48:52 +11001021 debug3("Sent message fsync@openssh.com I:%u", id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001022 sshbuf_free(msg);
Damien Millerf29238e2013-10-17 11:48:52 +11001023
1024 status = get_status(conn, id);
1025 if (status != SSH2_FX_OK)
1026 error("Couldn't sync file: %s", fx2txt(status));
1027
djm@openbsd.orgf6a59a22018-05-25 04:25:46 +00001028 return status == SSH2_FX_OK ? 0 : -1;
Damien Millerf29238e2013-10-17 11:48:52 +11001029}
1030
Damien Millercfe23d32008-02-10 22:20:44 +11001031#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +11001032char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001033do_readlink(struct sftp_conn *conn, const char *path)
Damien Miller058316f2001-03-08 10:08:49 +11001034{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001035 struct sshbuf *msg;
1036 u_int expected_id, count, id;
Damien Miller058316f2001-03-08 10:08:49 +11001037 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001038 Attrib a;
1039 u_char type;
1040 int r;
Damien Miller058316f2001-03-08 10:08:49 +11001041
Damien Miller3db5f532002-02-13 14:10:32 +11001042 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +10001043 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +11001044
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001045 if ((msg = sshbuf_new()) == NULL)
1046 fatal("%s: sshbuf_new failed", __func__);
Damien Miller058316f2001-03-08 10:08:49 +11001047
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001048 get_msg(conn, msg);
1049 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1050 (r = sshbuf_get_u32(msg, &id)) != 0)
1051 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001052
1053 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001054 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +11001055
1056 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001057 u_int status;
Damien Miller058316f2001-03-08 10:08:49 +11001058
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001059 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1060 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001061 error("Couldn't readlink: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001062 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +11001063 return(NULL);
1064 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001065 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +11001066 SSH2_FXP_NAME, type);
1067
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001068 if ((r = sshbuf_get_u32(msg, &count)) != 0)
1069 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001070 if (count != 1)
1071 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
1072
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001073 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
1074 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
1075 (r = decode_attrib(msg, &a)) != 0)
1076 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001077
1078 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
1079
Darren Tuckera627d422013-06-02 07:31:17 +10001080 free(longname);
Damien Miller058316f2001-03-08 10:08:49 +11001081
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001082 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +11001083
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001084 return filename;
Damien Miller058316f2001-03-08 10:08:49 +11001085}
Damien Millercfe23d32008-02-10 22:20:44 +11001086#endif
Damien Miller058316f2001-03-08 10:08:49 +11001087
Damien Millerd671e5a2008-05-19 14:53:33 +10001088int
Darren Tucker7b598892008-06-09 22:49:36 +10001089do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
Damien Millerd671e5a2008-05-19 14:53:33 +10001090 int quiet)
1091{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001092 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +10001093 u_int id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001094 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +10001095
1096 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
1097 error("Server does not support statvfs@openssh.com extension");
1098 return -1;
1099 }
1100
1101 id = conn->msg_id++;
1102
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001103 if ((msg = sshbuf_new()) == NULL)
1104 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001105 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1106 (r = sshbuf_put_u32(msg, id)) != 0 ||
1107 (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
1108 (r = sshbuf_put_cstring(msg, path)) != 0)
1109 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1110 send_msg(conn, msg);
1111 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +10001112
Damien Miller65e42f82010-09-24 22:15:11 +10001113 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +10001114}
1115
1116#ifdef notyet
1117int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001118do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
Darren Tucker7b598892008-06-09 22:49:36 +10001119 struct sftp_statvfs *st, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +10001120{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001121 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +10001122 u_int id;
1123
1124 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
1125 error("Server does not support fstatvfs@openssh.com extension");
1126 return -1;
1127 }
1128
1129 id = conn->msg_id++;
1130
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001131 if ((msg = sshbuf_new()) == NULL)
1132 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001133 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1134 (r = sshbuf_put_u32(msg, id)) != 0 ||
1135 (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
1136 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1137 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1138 send_msg(conn, msg);
1139 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +10001140
Damien Miller65e42f82010-09-24 22:15:11 +10001141 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +10001142}
1143#endif
1144
djm@openbsd.org60d8c842019-01-16 23:23:45 +00001145int
1146do_lsetstat(struct sftp_conn *conn, const char *path, Attrib *a)
1147{
1148 struct sshbuf *msg;
1149 u_int status, id;
1150 int r;
1151
1152 if ((conn->exts & SFTP_EXT_LSETSTAT) == 0) {
1153 error("Server does not support lsetstat@openssh.com extension");
1154 return -1;
1155 }
1156
1157 id = conn->msg_id++;
1158 if ((msg = sshbuf_new()) == NULL)
1159 fatal("%s: sshbuf_new failed", __func__);
1160 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1161 (r = sshbuf_put_u32(msg, id)) != 0 ||
1162 (r = sshbuf_put_cstring(msg, "lsetstat@openssh.com")) != 0 ||
1163 (r = sshbuf_put_cstring(msg, path)) != 0 ||
1164 (r = encode_attrib(msg, a)) != 0)
1165 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1166 send_msg(conn, msg);
1167 sshbuf_free(msg);
1168
1169 status = get_status(conn, id);
1170 if (status != SSH2_FX_OK)
1171 error("Couldn't setstat on \"%s\": %s", path,
1172 fx2txt(status));
1173
1174 return status == SSH2_FX_OK ? 0 : -1;
1175}
1176
Damien Miller16a13332002-02-13 14:03:56 +11001177static void
Damien Miller65e42f82010-09-24 22:15:11 +10001178send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001179 u_int len, const u_char *handle, u_int handle_len)
Damien Miller16a13332002-02-13 14:03:56 +11001180{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001181 struct sshbuf *msg;
1182 int r;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001183
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001184 if ((msg = sshbuf_new()) == NULL)
1185 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001186 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READ)) != 0 ||
1187 (r = sshbuf_put_u32(msg, id)) != 0 ||
1188 (r = sshbuf_put_string(msg, handle, handle_len)) != 0 ||
1189 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1190 (r = sshbuf_put_u32(msg, len)) != 0)
1191 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1192 send_msg(conn, msg);
1193 sshbuf_free(msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001194}
Damien Miller16a13332002-02-13 14:03:56 +11001195
Damien Miller058316f2001-03-08 10:08:49 +11001196int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001197do_download(struct sftp_conn *conn, const char *remote_path,
1198 const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
1199 int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001200{
Darren Tucker1b0dd172009-10-07 08:37:48 +11001201 Attrib junk;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001202 struct sshbuf *msg;
1203 u_char *handle;
1204 int local_fd = -1, write_error;
djm@openbsd.orga0876bd2019-07-12 03:56:21 +00001205 int read_error, write_errno, lmodified = 0, reordered = 0, r;
Damien Miller0d032412013-07-25 11:56:52 +10001206 u_int64_t offset = 0, size, highwater;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001207 u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
Damien Miller62d57f62003-01-10 21:43:24 +11001208 off_t progress_counter;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001209 size_t handle_len;
Damien Miller0d032412013-07-25 11:56:52 +10001210 struct stat st;
Damien Miller16a13332002-02-13 14:03:56 +11001211 struct request {
1212 u_int id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001213 size_t len;
Damien Miller16a13332002-02-13 14:03:56 +11001214 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001215 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +11001216 };
1217 TAILQ_HEAD(reqhead, request) requests;
1218 struct request *req;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001219 u_char type;
Damien Miller16a13332002-02-13 14:03:56 +11001220
1221 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +11001222
Darren Tucker1b0dd172009-10-07 08:37:48 +11001223 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1224 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001225
Damien Miller9e720282008-06-29 22:46:35 +10001226 /* Do not preserve set[ug]id here, as we do not preserve ownership */
Damien Miller33804262001-02-04 23:20:18 +11001227 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +11001228 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +11001229 else
1230 mode = 0666;
1231
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001232 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +11001233 (!S_ISREG(a->perm))) {
1234 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001235 return(-1);
1236 }
1237
Damien Miller16a13332002-02-13 14:03:56 +11001238 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1239 size = a->size;
1240 else
1241 size = 0;
1242
Damien Miller3db5f532002-02-13 14:10:32 +11001243 buflen = conn->transfer_buflen;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001244 if ((msg = sshbuf_new()) == NULL)
1245 fatal("%s: sshbuf_new failed", __func__);
1246
1247 attrib_clear(&junk); /* Send empty attributes */
Damien Miller33804262001-02-04 23:20:18 +11001248
1249 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001250 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001251 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1252 (r = sshbuf_put_u32(msg, id)) != 0 ||
1253 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1254 (r = sshbuf_put_u32(msg, SSH2_FXF_READ)) != 0 ||
1255 (r = encode_attrib(msg, &junk)) != 0)
1256 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1257 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001258 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001259
Damien Miller65e42f82010-09-24 22:15:11 +10001260 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001261 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001262 if (handle == NULL) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001263 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +11001264 return(-1);
1265 }
1266
Damien Millerbda5c842013-10-15 12:05:58 +11001267 local_fd = open(local_path,
1268 O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
Damien Miller3db5f532002-02-13 14:10:32 +11001269 if (local_fd == -1) {
1270 error("Couldn't open local file \"%s\" for writing: %s",
1271 local_path, strerror(errno));
Damien Miller0d032412013-07-25 11:56:52 +10001272 goto fail;
1273 }
1274 offset = highwater = 0;
Damien Millerbda5c842013-10-15 12:05:58 +11001275 if (resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001276 if (fstat(local_fd, &st) == -1) {
1277 error("Unable to stat local file \"%s\": %s",
1278 local_path, strerror(errno));
1279 goto fail;
1280 }
Damien Millerc3d483f2014-01-17 11:20:26 +11001281 if (st.st_size < 0) {
1282 error("\"%s\" has negative size", local_path);
1283 goto fail;
1284 }
1285 if ((u_int64_t)st.st_size > size) {
Damien Miller0d032412013-07-25 11:56:52 +10001286 error("Unable to resume download of \"%s\": "
1287 "local file is larger than remote", local_path);
1288 fail:
1289 do_close(conn, handle, handle_len);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001290 sshbuf_free(msg);
Damien Miller0d032412013-07-25 11:56:52 +10001291 free(handle);
Damien Miller9275df32013-12-05 10:26:32 +11001292 if (local_fd != -1)
1293 close(local_fd);
Damien Miller0d032412013-07-25 11:56:52 +10001294 return -1;
1295 }
1296 offset = highwater = st.st_size;
Damien Miller3db5f532002-02-13 14:10:32 +11001297 }
1298
Damien Miller33804262001-02-04 23:20:18 +11001299 /* Read from remote and write to local */
Damien Miller0d032412013-07-25 11:56:52 +10001300 write_error = read_error = write_errno = num_req = 0;
Damien Miller16a13332002-02-13 14:03:56 +11001301 max_req = 1;
Damien Miller0d032412013-07-25 11:56:52 +10001302 progress_counter = offset;
Damien Miller62d57f62003-01-10 21:43:24 +11001303
Damien Miller9ba30692004-03-08 23:12:02 +11001304 if (showprogress && size != 0)
1305 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001306
Damien Miller16a13332002-02-13 14:03:56 +11001307 while (num_req > 0 || max_req > 0) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001308 u_char *data;
1309 size_t len;
Damien Miller33804262001-02-04 23:20:18 +11001310
Darren Tuckercdf547a2004-05-24 10:12:19 +10001311 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001312 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001313 * allow outstanding requests to drain gracefully
1314 */
1315 if (interrupted) {
1316 if (num_req == 0) /* If we haven't started yet... */
1317 break;
1318 max_req = 0;
1319 }
1320
Damien Miller16a13332002-02-13 14:03:56 +11001321 /* Send some more requests */
1322 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001323 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +00001324 (unsigned long long)offset,
1325 (unsigned long long)offset + buflen - 1,
1326 num_req, max_req);
Damien Miller6c81fee2013-11-08 12:19:55 +11001327 req = xcalloc(1, sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +11001328 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001329 req->len = buflen;
1330 req->offset = offset;
1331 offset += buflen;
1332 num_req++;
1333 TAILQ_INSERT_TAIL(&requests, req, tq);
Damien Miller65e42f82010-09-24 22:15:11 +10001334 send_read_request(conn, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001335 req->len, handle, handle_len);
1336 }
Damien Miller33804262001-02-04 23:20:18 +11001337
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001338 sshbuf_reset(msg);
1339 get_msg(conn, msg);
1340 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1341 (r = sshbuf_get_u32(msg, &id)) != 0)
1342 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001343 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001344
Damien Miller16a13332002-02-13 14:03:56 +11001345 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001346 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001347 req != NULL && req->id != id;
1348 req = TAILQ_NEXT(req, tq))
1349 ;
1350 if (req == NULL)
1351 fatal("Unexpected reply %u", id);
1352
1353 switch (type) {
1354 case SSH2_FXP_STATUS:
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001355 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1356 fatal("%s: buffer error: %s",
1357 __func__, ssh_err(r));
Damien Miller16a13332002-02-13 14:03:56 +11001358 if (status != SSH2_FX_EOF)
1359 read_error = 1;
1360 max_req = 0;
1361 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001362 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001363 num_req--;
1364 break;
1365 case SSH2_FXP_DATA:
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001366 if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1367 fatal("%s: buffer error: %s",
1368 __func__, ssh_err(r));
Ben Lindstromeb505452002-03-22 01:03:15 +00001369 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001370 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001371 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001372 if (len > req->len)
1373 fatal("Received more data than asked for "
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001374 "%zu > %zu", len, req->len);
djm@openbsd.orga0876bd2019-07-12 03:56:21 +00001375 lmodified = 1;
Damien Miller16a13332002-02-13 14:03:56 +11001376 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001377 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001378 !write_error) {
1379 write_errno = errno;
1380 write_error = 1;
1381 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001382 }
Damien Miller0d032412013-07-25 11:56:52 +10001383 else if (!reordered && req->offset <= highwater)
1384 highwater = req->offset + len;
1385 else if (!reordered && req->offset > highwater)
1386 reordered = 1;
Damien Miller62d57f62003-01-10 21:43:24 +11001387 progress_counter += len;
Darren Tuckera627d422013-06-02 07:31:17 +10001388 free(data);
Damien Miller16a13332002-02-13 14:03:56 +11001389
1390 if (len == req->len) {
1391 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001392 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001393 num_req--;
1394 } else {
1395 /* Resend the request for the missing data */
1396 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001397 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001398 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001399 (unsigned long long)req->offset +
1400 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001401 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001402 req->len -= len;
1403 req->offset += len;
Damien Miller65e42f82010-09-24 22:15:11 +10001404 send_read_request(conn, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001405 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001406 /* Reduce the request size */
1407 if (len < buflen)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001408 buflen = MAXIMUM(MIN_READ_SIZE, len);
Damien Miller16a13332002-02-13 14:03:56 +11001409 }
1410 if (max_req > 0) { /* max_req = 0 iff EOF received */
1411 if (size > 0 && offset > size) {
1412 /* Only one request at a time
1413 * after the expected EOF */
1414 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001415 (unsigned long long)offset,
1416 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001417 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001418 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001419 ++max_req;
1420 }
1421 }
1422 break;
1423 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001424 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001425 SSH2_FXP_DATA, type);
1426 }
Damien Miller33804262001-02-04 23:20:18 +11001427 }
Damien Miller33804262001-02-04 23:20:18 +11001428
Damien Miller62d57f62003-01-10 21:43:24 +11001429 if (showprogress && size)
1430 stop_progress_meter();
1431
Damien Miller16a13332002-02-13 14:03:56 +11001432 /* Sanity check */
1433 if (TAILQ_FIRST(&requests) != NULL)
1434 fatal("Transfer complete, but requests still in queue");
Damien Miller0d032412013-07-25 11:56:52 +10001435 /* Truncate at highest contiguous point to avoid holes on interrupt */
1436 if (read_error || write_error || interrupted) {
Damien Millerbda5c842013-10-15 12:05:58 +11001437 if (reordered && resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001438 error("Unable to resume download of \"%s\": "
1439 "server reordered requests", local_path);
1440 }
1441 debug("truncating at %llu", (unsigned long long)highwater);
djm@openbsd.org9cc68422015-05-28 04:50:53 +00001442 if (ftruncate(local_fd, highwater) == -1)
1443 error("ftruncate \"%s\": %s", local_path,
1444 strerror(errno));
Damien Miller0d032412013-07-25 11:56:52 +10001445 }
Damien Miller16a13332002-02-13 14:03:56 +11001446 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001447 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001448 remote_path, fx2txt(status));
Damien Millerfec029f2013-08-21 02:42:12 +10001449 status = -1;
Damien Miller3db5f532002-02-13 14:10:32 +11001450 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001451 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001452 error("Couldn't write to \"%s\": %s", local_path,
1453 strerror(write_errno));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001454 status = SSH2_FX_FAILURE;
Damien Miller3db5f532002-02-13 14:10:32 +11001455 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001456 } else {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001457 if (do_close(conn, handle, handle_len) != 0 || interrupted)
1458 status = SSH2_FX_FAILURE;
1459 else
1460 status = SSH2_FX_OK;
Damien Miller16a13332002-02-13 14:03:56 +11001461 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001462#ifdef HAVE_FCHMOD
Damien Millerbda5c842013-10-15 12:05:58 +11001463 if (preserve_flag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001464#else
Damien Millerbda5c842013-10-15 12:05:58 +11001465 if (preserve_flag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001466#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001467 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001468 strerror(errno));
Damien Millerbda5c842013-10-15 12:05:58 +11001469 if (preserve_flag &&
1470 (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
Damien Miller16a13332002-02-13 14:03:56 +11001471 struct timeval tv[2];
1472 tv[0].tv_sec = a->atime;
1473 tv[1].tv_sec = a->mtime;
1474 tv[0].tv_usec = tv[1].tv_usec = 0;
1475 if (utimes(local_path, tv) == -1)
1476 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001477 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001478 }
djm@openbsd.orga0876bd2019-07-12 03:56:21 +00001479 if (resume_flag && !lmodified)
1480 logit("File \"%s\" was not modified", local_path);
1481 else if (fsync_flag) {
Damien Millerf29238e2013-10-17 11:48:52 +11001482 debug("syncing \"%s\"", local_path);
1483 if (fsync(local_fd) == -1)
1484 error("Couldn't sync file \"%s\": %s",
1485 local_path, strerror(errno));
1486 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001487 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001488 close(local_fd);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001489 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001490 free(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001491
djm@openbsd.orgf6a59a22018-05-25 04:25:46 +00001492 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +11001493}
1494
Darren Tucker1b0dd172009-10-07 08:37:48 +11001495static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001496download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1497 int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
1498 int resume_flag, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001499{
1500 int i, ret = 0;
1501 SFTP_DIRENT **dir_entries;
djm@openbsd.org1a660792018-07-31 03:07:24 +00001502 char *filename, *new_src = NULL, *new_dst = NULL;
Darren Tucker1b0dd172009-10-07 08:37:48 +11001503 mode_t mode = 0777;
1504
1505 if (depth >= MAX_DIR_DEPTH) {
1506 error("Maximum directory depth exceeded: %d levels", depth);
1507 return -1;
1508 }
1509
1510 if (dirattrib == NULL &&
1511 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1512 error("Unable to stat remote directory \"%s\"", src);
1513 return -1;
1514 }
1515 if (!S_ISDIR(dirattrib->perm)) {
1516 error("\"%s\" is not a directory", src);
1517 return -1;
1518 }
Damien Millerbda5c842013-10-15 12:05:58 +11001519 if (print_flag)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001520 mprintf("Retrieving %s\n", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001521
1522 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1523 mode = dirattrib->perm & 01777;
1524 else {
1525 debug("Server did not send permissions for "
1526 "directory \"%s\"", dst);
1527 }
1528
1529 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1530 error("mkdir %s: %s", dst, strerror(errno));
1531 return -1;
1532 }
1533
1534 if (do_readdir(conn, src, &dir_entries) == -1) {
1535 error("%s: Failed to get directory contents", src);
1536 return -1;
1537 }
1538
1539 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
djm@openbsd.org1a660792018-07-31 03:07:24 +00001540 free(new_dst);
1541 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001542
djm@openbsd.org1a660792018-07-31 03:07:24 +00001543 filename = dir_entries[i]->filename;
Darren Tucker1b0dd172009-10-07 08:37:48 +11001544 new_dst = path_append(dst, filename);
1545 new_src = path_append(src, filename);
1546
1547 if (S_ISDIR(dir_entries[i]->a.perm)) {
1548 if (strcmp(filename, ".") == 0 ||
1549 strcmp(filename, "..") == 0)
1550 continue;
1551 if (download_dir_internal(conn, new_src, new_dst,
Damien Millerbda5c842013-10-15 12:05:58 +11001552 depth + 1, &(dir_entries[i]->a), preserve_flag,
Damien Millerf29238e2013-10-17 11:48:52 +11001553 print_flag, resume_flag, fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001554 ret = -1;
1555 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1556 if (do_download(conn, new_src, new_dst,
Damien Millerf29238e2013-10-17 11:48:52 +11001557 &(dir_entries[i]->a), preserve_flag,
1558 resume_flag, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001559 error("Download of file %s to %s failed",
1560 new_src, new_dst);
1561 ret = -1;
1562 }
1563 } else
1564 logit("%s: not a regular file\n", new_src);
1565
Darren Tucker1b0dd172009-10-07 08:37:48 +11001566 }
djm@openbsd.org1a660792018-07-31 03:07:24 +00001567 free(new_dst);
1568 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001569
Damien Millerbda5c842013-10-15 12:05:58 +11001570 if (preserve_flag) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001571 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1572 struct timeval tv[2];
1573 tv[0].tv_sec = dirattrib->atime;
1574 tv[1].tv_sec = dirattrib->mtime;
1575 tv[0].tv_usec = tv[1].tv_usec = 0;
1576 if (utimes(dst, tv) == -1)
1577 error("Can't set times on \"%s\": %s",
1578 dst, strerror(errno));
1579 } else
1580 debug("Server did not send times for directory "
1581 "\"%s\"", dst);
1582 }
1583
1584 free_sftp_dirents(dir_entries);
1585
1586 return ret;
1587}
1588
1589int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001590download_dir(struct sftp_conn *conn, const char *src, const char *dst,
1591 Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1592 int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001593{
1594 char *src_canon;
1595 int ret;
1596
1597 if ((src_canon = do_realpath(conn, src)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001598 error("Unable to canonicalize path \"%s\"", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001599 return -1;
1600 }
1601
Damien Millerbda5c842013-10-15 12:05:58 +11001602 ret = download_dir_internal(conn, src_canon, dst, 0,
Damien Millerf29238e2013-10-17 11:48:52 +11001603 dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag);
Darren Tuckera627d422013-06-02 07:31:17 +10001604 free(src_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001605 return ret;
1606}
1607
Damien Miller33804262001-02-04 23:20:18 +11001608int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001609do_upload(struct sftp_conn *conn, const char *local_path,
1610 const char *remote_path, int preserve_flag, int resume, int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001611{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001612 int r, local_fd;
1613 u_int status = SSH2_FX_OK;
1614 u_int id;
1615 u_char type;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001616 off_t offset, progress_counter;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001617 u_char *handle, *data;
1618 struct sshbuf *msg;
Damien Miller33804262001-02-04 23:20:18 +11001619 struct stat sb;
Damien Millerd8accc02014-05-15 13:46:25 +10001620 Attrib a, *c = NULL;
Damien Miller16a13332002-02-13 14:03:56 +11001621 u_int32_t startid;
1622 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001623 struct outstanding_ack {
1624 u_int id;
1625 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001626 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001627 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001628 };
1629 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001630 struct outstanding_ack *ack = NULL;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001631 size_t handle_len;
Damien Miller5873dfd2002-02-13 14:04:37 +11001632
1633 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001634
1635 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1636 error("Couldn't open local file \"%s\" for reading: %s",
1637 local_path, strerror(errno));
1638 return(-1);
1639 }
1640 if (fstat(local_fd, &sb) == -1) {
1641 error("Couldn't fstat local file \"%s\": %s",
1642 local_path, strerror(errno));
1643 close(local_fd);
1644 return(-1);
1645 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001646 if (!S_ISREG(sb.st_mode)) {
1647 error("%s is not a regular file", local_path);
1648 close(local_fd);
1649 return(-1);
1650 }
Damien Miller33804262001-02-04 23:20:18 +11001651 stat_to_attrib(&sb, &a);
1652
1653 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1654 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1655 a.perm &= 0777;
Damien Millerbda5c842013-10-15 12:05:58 +11001656 if (!preserve_flag)
Damien Miller33804262001-02-04 23:20:18 +11001657 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1658
Damien Millerd8accc02014-05-15 13:46:25 +10001659 if (resume) {
1660 /* Get remote file size if it exists */
1661 if ((c = do_stat(conn, remote_path, 0)) == NULL) {
djm@openbsd.org34a01b22016-04-08 08:19:17 +00001662 close(local_fd);
Damien Millerd8accc02014-05-15 13:46:25 +10001663 return -1;
1664 }
1665
1666 if ((off_t)c->size >= sb.st_size) {
1667 error("destination file bigger or same size as "
1668 "source file");
1669 close(local_fd);
1670 return -1;
1671 }
1672
1673 if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
1674 close(local_fd);
1675 return -1;
1676 }
1677 }
1678
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001679 if ((msg = sshbuf_new()) == NULL)
1680 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001681
1682 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001683 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001684 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1685 (r = sshbuf_put_u32(msg, id)) != 0 ||
1686 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1687 (r = sshbuf_put_u32(msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|
1688 (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC))) != 0 ||
1689 (r = encode_attrib(msg, &a)) != 0)
1690 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1691 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001692 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001693
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001694 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +11001695
Damien Miller65e42f82010-09-24 22:15:11 +10001696 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001697 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001698 if (handle == NULL) {
1699 close(local_fd);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001700 sshbuf_free(msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001701 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001702 }
1703
Damien Miller16a13332002-02-13 14:03:56 +11001704 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001705 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001706
Damien Miller33804262001-02-04 23:20:18 +11001707 /* Read from local and write to remote */
Damien Millerd8accc02014-05-15 13:46:25 +10001708 offset = progress_counter = (resume ? c->size : 0);
Damien Miller62d57f62003-01-10 21:43:24 +11001709 if (showprogress)
Darren Tuckerc9a19912013-06-02 08:37:05 +10001710 start_progress_meter(local_path, sb.st_size,
1711 &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001712
Damien Miller9f0f5c62001-12-21 14:45:46 +11001713 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001714 int len;
Damien Miller33804262001-02-04 23:20:18 +11001715
1716 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001717 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001718 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001719 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001720 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001721 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001722 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001723 len = 0;
1724 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001725 len = read(local_fd, data, conn->transfer_buflen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001726 while ((len == -1) &&
1727 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
Damien Miller33804262001-02-04 23:20:18 +11001728
1729 if (len == -1)
1730 fatal("Couldn't read from \"%s\": %s", local_path,
1731 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001732
1733 if (len != 0) {
Damien Miller6c81fee2013-11-08 12:19:55 +11001734 ack = xcalloc(1, sizeof(*ack));
Damien Miller5873dfd2002-02-13 14:04:37 +11001735 ack->id = ++id;
1736 ack->offset = offset;
1737 ack->len = len;
1738 TAILQ_INSERT_TAIL(&acks, ack, tq);
1739
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001740 sshbuf_reset(msg);
1741 if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
1742 (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
1743 (r = sshbuf_put_string(msg, handle,
1744 handle_len)) != 0 ||
1745 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1746 (r = sshbuf_put_string(msg, data, len)) != 0)
1747 fatal("%s: buffer error: %s",
1748 __func__, ssh_err(r));
1749 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001750 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001751 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001752 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001753 break;
1754
Damien Miller5873dfd2002-02-13 14:04:37 +11001755 if (ack == NULL)
1756 fatal("Unexpected ACK %u", id);
1757
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001758 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001759 id - ackid >= conn->num_requests) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001760 u_int rid;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001761
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001762 sshbuf_reset(msg);
1763 get_msg(conn, msg);
1764 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1765 (r = sshbuf_get_u32(msg, &rid)) != 0)
1766 fatal("%s: buffer error: %s",
1767 __func__, ssh_err(r));
Damien Miller5873dfd2002-02-13 14:04:37 +11001768
1769 if (type != SSH2_FXP_STATUS)
1770 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1771 "got %d", SSH2_FXP_STATUS, type);
1772
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001773 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1774 fatal("%s: buffer error: %s",
1775 __func__, ssh_err(r));
1776 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller5873dfd2002-02-13 14:04:37 +11001777
1778 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001779 for (ack = TAILQ_FIRST(&acks);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001780 ack != NULL && ack->id != rid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001781 ack = TAILQ_NEXT(ack, tq))
1782 ;
1783 if (ack == NULL)
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001784 fatal("Can't find request for ID %u", rid);
Damien Miller5873dfd2002-02-13 14:04:37 +11001785 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001786 debug3("In write loop, ack for %u %u bytes at %lld",
1787 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001788 ++ackid;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001789 progress_counter += ack->len;
Darren Tuckera627d422013-06-02 07:31:17 +10001790 free(ack);
Damien Miller33804262001-02-04 23:20:18 +11001791 }
Damien Miller33804262001-02-04 23:20:18 +11001792 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001793 if (offset < 0)
1794 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001795 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001796 sshbuf_free(msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001797
Damien Miller62d57f62003-01-10 21:43:24 +11001798 if (showprogress)
1799 stop_progress_meter();
Darren Tuckera627d422013-06-02 07:31:17 +10001800 free(data);
Damien Miller33804262001-02-04 23:20:18 +11001801
Damien Milleracdf25b2008-02-10 22:27:24 +11001802 if (status != SSH2_FX_OK) {
1803 error("Couldn't write to remote file \"%s\": %s",
1804 remote_path, fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001805 status = SSH2_FX_FAILURE;
Damien Milleracdf25b2008-02-10 22:27:24 +11001806 }
1807
Damien Miller33804262001-02-04 23:20:18 +11001808 if (close(local_fd) == -1) {
1809 error("Couldn't close local file \"%s\": %s", local_path,
1810 strerror(errno));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001811 status = SSH2_FX_FAILURE;
Damien Miller33804262001-02-04 23:20:18 +11001812 }
1813
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001814 /* Override umask and utimes if asked */
Damien Millerbda5c842013-10-15 12:05:58 +11001815 if (preserve_flag)
Damien Miller3db5f532002-02-13 14:10:32 +11001816 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001817
Damien Millerf29238e2013-10-17 11:48:52 +11001818 if (fsync_flag)
1819 (void)do_fsync(conn, handle, handle_len);
1820
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001821 if (do_close(conn, handle, handle_len) != 0)
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001822 status = SSH2_FX_FAILURE;
1823
Darren Tuckera627d422013-06-02 07:31:17 +10001824 free(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001825
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001826 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +11001827}
Darren Tucker1b0dd172009-10-07 08:37:48 +11001828
1829static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001830upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1831 int depth, int preserve_flag, int print_flag, int resume, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001832{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001833 int ret = 0;
Darren Tucker1b0dd172009-10-07 08:37:48 +11001834 DIR *dirp;
1835 struct dirent *dp;
djm@openbsd.org1a660792018-07-31 03:07:24 +00001836 char *filename, *new_src = NULL, *new_dst = NULL;
Darren Tucker1b0dd172009-10-07 08:37:48 +11001837 struct stat sb;
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001838 Attrib a, *dirattrib;
Darren Tucker1b0dd172009-10-07 08:37:48 +11001839
1840 if (depth >= MAX_DIR_DEPTH) {
1841 error("Maximum directory depth exceeded: %d levels", depth);
1842 return -1;
1843 }
1844
1845 if (stat(src, &sb) == -1) {
1846 error("Couldn't stat directory \"%s\": %s",
1847 src, strerror(errno));
1848 return -1;
1849 }
1850 if (!S_ISDIR(sb.st_mode)) {
1851 error("\"%s\" is not a directory", src);
1852 return -1;
1853 }
Damien Millerbda5c842013-10-15 12:05:58 +11001854 if (print_flag)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001855 mprintf("Entering %s\n", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001856
1857 attrib_clear(&a);
1858 stat_to_attrib(&sb, &a);
1859 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1860 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1861 a.perm &= 01777;
Damien Millerbda5c842013-10-15 12:05:58 +11001862 if (!preserve_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001863 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
Damien Miller0d032412013-07-25 11:56:52 +10001864
Darren Tucker1b0dd172009-10-07 08:37:48 +11001865 /*
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001866 * sftp lacks a portable status value to match errno EEXIST,
1867 * so if we get a failure back then we must check whether
1868 * the path already existed and is a directory.
Darren Tucker1b0dd172009-10-07 08:37:48 +11001869 */
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001870 if (do_mkdir(conn, dst, &a, 0) != 0) {
1871 if ((dirattrib = do_stat(conn, dst, 0)) == NULL)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001872 return -1;
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001873 if (!S_ISDIR(dirattrib->perm)) {
1874 error("\"%s\" exists but is not a directory", dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001875 return -1;
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001876 }
Darren Tucker1b0dd172009-10-07 08:37:48 +11001877 }
1878
1879 if ((dirp = opendir(src)) == NULL) {
1880 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1881 return -1;
1882 }
Damien Miller0d032412013-07-25 11:56:52 +10001883
Darren Tucker1b0dd172009-10-07 08:37:48 +11001884 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1885 if (dp->d_ino == 0)
1886 continue;
djm@openbsd.org1a660792018-07-31 03:07:24 +00001887 free(new_dst);
1888 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001889 filename = dp->d_name;
1890 new_dst = path_append(dst, filename);
1891 new_src = path_append(src, filename);
1892
Darren Tucker438b4732009-10-11 21:52:10 +11001893 if (lstat(new_src, &sb) == -1) {
1894 logit("%s: lstat failed: %s", filename,
1895 strerror(errno));
1896 ret = -1;
1897 } else if (S_ISDIR(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001898 if (strcmp(filename, ".") == 0 ||
1899 strcmp(filename, "..") == 0)
1900 continue;
1901
1902 if (upload_dir_internal(conn, new_src, new_dst,
Damien Millerd8accc02014-05-15 13:46:25 +10001903 depth + 1, preserve_flag, print_flag, resume,
Damien Millerf29238e2013-10-17 11:48:52 +11001904 fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001905 ret = -1;
Darren Tucker438b4732009-10-11 21:52:10 +11001906 } else if (S_ISREG(sb.st_mode)) {
Damien Millerbda5c842013-10-15 12:05:58 +11001907 if (do_upload(conn, new_src, new_dst,
Damien Millerd8accc02014-05-15 13:46:25 +10001908 preserve_flag, resume, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001909 error("Uploading of file %s to %s failed!",
1910 new_src, new_dst);
1911 ret = -1;
1912 }
1913 } else
1914 logit("%s: not a regular file\n", filename);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001915 }
djm@openbsd.org1a660792018-07-31 03:07:24 +00001916 free(new_dst);
1917 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001918
1919 do_setstat(conn, dst, &a);
1920
1921 (void) closedir(dirp);
1922 return ret;
1923}
1924
1925int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001926upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
1927 int preserve_flag, int print_flag, int resume, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001928{
1929 char *dst_canon;
1930 int ret;
1931
1932 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001933 error("Unable to canonicalize path \"%s\"", dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001934 return -1;
1935 }
1936
Damien Miller2f93d052013-10-15 12:06:27 +11001937 ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
Damien Millerd8accc02014-05-15 13:46:25 +10001938 print_flag, resume, fsync_flag);
Damien Millerf29238e2013-10-17 11:48:52 +11001939
Darren Tuckera627d422013-06-02 07:31:17 +10001940 free(dst_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001941 return ret;
1942}
1943
1944char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001945path_append(const char *p1, const char *p2)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001946{
1947 char *ret;
1948 size_t len = strlen(p1) + strlen(p2) + 2;
1949
1950 ret = xmalloc(len);
1951 strlcpy(ret, p1, len);
1952 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1953 strlcat(ret, "/", len);
1954 strlcat(ret, p2, len);
1955
1956 return(ret);
1957}
1958