blob: d47be0ea5f29ae3c75d09fa25217c81941a89f3f [file] [log] [blame]
djm@openbsd.orgc7995f22017-01-03 05:46:51 +00001/* $OpenBSD: sftp-client.c,v 1.126 2017/01/03 05:46:51 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 Miller3db5f532002-02-13 14:10:32 +110070struct sftp_conn {
71 int fd_in;
72 int fd_out;
73 u_int transfer_buflen;
74 u_int num_requests;
75 u_int version;
76 u_int msg_id;
Damien Millerd671e5a2008-05-19 14:53:33 +100077#define SFTP_EXT_POSIX_RENAME 0x00000001
78#define SFTP_EXT_STATVFS 0x00000002
79#define SFTP_EXT_FSTATVFS 0x00000004
Darren Tuckeraf1f9092010-12-05 09:02:47 +110080#define SFTP_EXT_HARDLINK 0x00000008
Damien Millerf29238e2013-10-17 11:48:52 +110081#define SFTP_EXT_FSYNC 0x00000010
Damien Miller7a3e1d02008-03-27 10:59:57 +110082 u_int exts;
Damien Miller65e42f82010-09-24 22:15:11 +100083 u_int64_t limit_kbps;
84 struct bwlimit bwlimit_in, bwlimit_out;
Damien Miller3db5f532002-02-13 14:10:32 +110085};
Ben Lindstrom288cc392001-02-09 02:58:04 +000086
djm@openbsd.org7d845f42015-01-14 13:54:13 +000087static u_char *
88get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
Damien Miller65e42f82010-09-24 22:15:11 +100089 const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
90
91/* ARGSUSED */
92static int
93sftpio(void *_bwlimit, size_t amount)
94{
95 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
96
97 bandwidth_limit(bwlimit, amount);
98 return 0;
99}
Darren Tuckerc22f0902009-10-07 08:24:19 +1100100
Ben Lindstrombba81212001-06-25 05:01:22 +0000101static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000102send_msg(struct sftp_conn *conn, struct sshbuf *m)
Damien Miller33804262001-02-04 23:20:18 +1100103{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100104 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +1000105 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +1100106
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000107 if (sshbuf_len(m) > SFTP_MAX_MSG_LENGTH)
108 fatal("Outbound message too long %zu", sshbuf_len(m));
Damien Miller33804262001-02-04 23:20:18 +1100109
Damien Millera7f3aaa2003-01-10 21:43:58 +1100110 /* Send length first */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000111 put_u32(mlen, sshbuf_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +1000112 iov[0].iov_base = mlen;
113 iov[0].iov_len = sizeof(mlen);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000114 iov[1].iov_base = (u_char *)sshbuf_ptr(m);
115 iov[1].iov_len = sshbuf_len(m);
Damien Millerd7834352006-08-05 12:39:39 +1000116
Damien Miller65e42f82010-09-24 22:15:11 +1000117 if (atomiciov6(writev, conn->fd_out, iov, 2,
Damien Miller0d032412013-07-25 11:56:52 +1000118 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_out) !=
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000119 sshbuf_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +1100120 fatal("Couldn't send packet: %s", strerror(errno));
121
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000122 sshbuf_reset(m);
Damien Miller33804262001-02-04 23:20:18 +1100123}
124
Ben Lindstrombba81212001-06-25 05:01:22 +0000125static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000126get_msg(struct sftp_conn *conn, struct sshbuf *m)
Damien Miller33804262001-02-04 23:20:18 +1100127{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100128 u_int msg_len;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000129 u_char *p;
130 int r;
Damien Miller33804262001-02-04 23:20:18 +1100131
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000132 if ((r = sshbuf_reserve(m, 4, &p)) != 0)
133 fatal("%s: buffer error: %s", __func__, ssh_err(r));
134 if (atomicio6(read, conn->fd_in, p, 4,
Damien Miller65e42f82010-09-24 22:15:11 +1000135 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in) != 4) {
Damien Millerb253cc42005-05-26 12:23:44 +1000136 if (errno == EPIPE)
137 fatal("Connection closed");
138 else
139 fatal("Couldn't read packet: %s", strerror(errno));
140 }
Damien Miller33804262001-02-04 23:20:18 +1100141
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000142 if ((r = sshbuf_get_u32(m, &msg_len)) != 0)
143 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller54446182006-01-02 23:40:50 +1100144 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000145 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100146
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000147 if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
148 fatal("%s: buffer error: %s", __func__, ssh_err(r));
149 if (atomicio6(read, conn->fd_in, p, msg_len,
Damien Miller65e42f82010-09-24 22:15:11 +1000150 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in)
151 != msg_len) {
Damien Millerb253cc42005-05-26 12:23:44 +1000152 if (errno == EPIPE)
153 fatal("Connection closed");
154 else
155 fatal("Read packet: %s", strerror(errno));
156 }
Damien Miller33804262001-02-04 23:20:18 +1100157}
158
Ben Lindstrombba81212001-06-25 05:01:22 +0000159static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000160send_string_request(struct sftp_conn *conn, u_int id, u_int code, const char *s,
Damien Miller33804262001-02-04 23:20:18 +1100161 u_int len)
162{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000163 struct sshbuf *msg;
164 int r;
Damien Miller33804262001-02-04 23:20:18 +1100165
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000166 if ((msg = sshbuf_new()) == NULL)
167 fatal("%s: sshbuf_new failed", __func__);
168 if ((r = sshbuf_put_u8(msg, code)) != 0 ||
169 (r = sshbuf_put_u32(msg, id)) != 0 ||
170 (r = sshbuf_put_string(msg, s, len)) != 0)
171 fatal("%s: buffer error: %s", __func__, ssh_err(r));
172 send_msg(conn, msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000173 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000174 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100175}
176
Ben Lindstrombba81212001-06-25 05:01:22 +0000177static void
Damien Miller65e42f82010-09-24 22:15:11 +1000178send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000179 const void *s, u_int len, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100180{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000181 struct sshbuf *msg;
182 int r;
Damien Miller33804262001-02-04 23:20:18 +1100183
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000184 if ((msg = sshbuf_new()) == NULL)
185 fatal("%s: sshbuf_new failed", __func__);
186 if ((r = sshbuf_put_u8(msg, code)) != 0 ||
187 (r = sshbuf_put_u32(msg, id)) != 0 ||
188 (r = sshbuf_put_string(msg, s, len)) != 0 ||
189 (r = encode_attrib(msg, a)) != 0)
190 fatal("%s: buffer error: %s", __func__, ssh_err(r));
191 send_msg(conn, msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000192 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000193 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100194}
195
Ben Lindstrombba81212001-06-25 05:01:22 +0000196static u_int
Damien Miller65e42f82010-09-24 22:15:11 +1000197get_status(struct sftp_conn *conn, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100198{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000199 struct sshbuf *msg;
200 u_char type;
201 u_int id, status;
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 get_msg(conn, msg);
207 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
208 (r = sshbuf_get_u32(msg, &id)) != 0)
209 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100210
211 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000212 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100213 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000214 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100215 SSH2_FXP_STATUS, type);
216
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000217 if ((r = sshbuf_get_u32(msg, &status)) != 0)
218 fatal("%s: buffer error: %s", __func__, ssh_err(r));
219 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100220
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000221 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100222
Damien Miller65e42f82010-09-24 22:15:11 +1000223 return status;
Damien Miller33804262001-02-04 23:20:18 +1100224}
225
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000226static u_char *
227get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
Damien Miller65e42f82010-09-24 22:15:11 +1000228 const char *errfmt, ...)
Damien Miller33804262001-02-04 23:20:18 +1100229{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000230 struct sshbuf *msg;
231 u_int id, status;
232 u_char type;
233 u_char *handle;
234 char errmsg[256];
Darren Tuckerc22f0902009-10-07 08:24:19 +1100235 va_list args;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000236 int r;
Darren Tuckerc22f0902009-10-07 08:24:19 +1100237
238 va_start(args, errfmt);
239 if (errfmt != NULL)
240 vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
241 va_end(args);
Damien Miller33804262001-02-04 23:20:18 +1100242
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000243 if ((msg = sshbuf_new()) == NULL)
244 fatal("%s: sshbuf_new failed", __func__);
245 get_msg(conn, msg);
246 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
247 (r = sshbuf_get_u32(msg, &id)) != 0)
248 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100249
250 if (id != expected_id)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100251 fatal("%s: ID mismatch (%u != %u)",
252 errfmt == NULL ? __func__ : errmsg, id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100253 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000254 if ((r = sshbuf_get_u32(msg, &status)) != 0)
255 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerc22f0902009-10-07 08:24:19 +1100256 if (errfmt != NULL)
257 error("%s: %s", errmsg, fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000258 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100259 return(NULL);
260 } else if (type != SSH2_FXP_HANDLE)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100261 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
262 errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
Damien Miller33804262001-02-04 23:20:18 +1100263
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000264 if ((r = sshbuf_get_string(msg, &handle, len)) != 0)
265 fatal("%s: buffer error: %s", __func__, ssh_err(r));
266 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100267
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000268 return handle;
Damien Miller33804262001-02-04 23:20:18 +1100269}
270
Ben Lindstrombba81212001-06-25 05:01:22 +0000271static Attrib *
Damien Miller65e42f82010-09-24 22:15:11 +1000272get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100273{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000274 struct sshbuf *msg;
275 u_int id;
276 u_char type;
277 int r;
278 static Attrib a;
Damien Miller33804262001-02-04 23:20:18 +1100279
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000280 if ((msg = sshbuf_new()) == NULL)
281 fatal("%s: sshbuf_new failed", __func__);
282 get_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100283
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000284 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
285 (r = sshbuf_get_u32(msg, &id)) != 0)
286 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100287
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000288 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100289 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000290 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100291 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000292 u_int status;
Damien Miller33804262001-02-04 23:20:18 +1100293
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000294 if ((r = sshbuf_get_u32(msg, &status)) != 0)
295 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000296 if (quiet)
297 debug("Couldn't stat remote file: %s", fx2txt(status));
298 else
299 error("Couldn't stat remote file: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000300 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100301 return(NULL);
302 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000303 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100304 SSH2_FXP_ATTRS, type);
305 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000306 if ((r = decode_attrib(msg, &a)) != 0) {
307 error("%s: couldn't decode attrib: %s", __func__, ssh_err(r));
308 sshbuf_free(msg);
309 return NULL;
310 }
311 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100312
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000313 return &a;
Damien Miller33804262001-02-04 23:20:18 +1100314}
315
Damien Millerd671e5a2008-05-19 14:53:33 +1000316static int
Damien Miller65e42f82010-09-24 22:15:11 +1000317get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
318 u_int expected_id, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000319{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000320 struct sshbuf *msg;
321 u_char type;
322 u_int id;
323 u_int64_t flag;
324 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +1000325
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000326 if ((msg = sshbuf_new()) == NULL)
327 fatal("%s: sshbuf_new failed", __func__);
328 get_msg(conn, msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000329
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000330 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
331 (r = sshbuf_get_u32(msg, &id)) != 0)
332 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000333
334 debug3("Received statvfs reply T:%u I:%u", type, id);
335 if (id != expected_id)
336 fatal("ID mismatch (%u != %u)", id, expected_id);
337 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000338 u_int status;
Damien Millerd671e5a2008-05-19 14:53:33 +1000339
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000340 if ((r = sshbuf_get_u32(msg, &status)) != 0)
341 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000342 if (quiet)
343 debug("Couldn't statvfs: %s", fx2txt(status));
344 else
345 error("Couldn't statvfs: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000346 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000347 return -1;
348 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
349 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
350 SSH2_FXP_EXTENDED_REPLY, type);
351 }
352
Damien Miller1d2c4562014-02-04 11:18:20 +1100353 memset(st, 0, sizeof(*st));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000354 if ((r = sshbuf_get_u64(msg, &st->f_bsize)) != 0 ||
355 (r = sshbuf_get_u64(msg, &st->f_frsize)) != 0 ||
356 (r = sshbuf_get_u64(msg, &st->f_blocks)) != 0 ||
357 (r = sshbuf_get_u64(msg, &st->f_bfree)) != 0 ||
358 (r = sshbuf_get_u64(msg, &st->f_bavail)) != 0 ||
359 (r = sshbuf_get_u64(msg, &st->f_files)) != 0 ||
360 (r = sshbuf_get_u64(msg, &st->f_ffree)) != 0 ||
361 (r = sshbuf_get_u64(msg, &st->f_favail)) != 0 ||
362 (r = sshbuf_get_u64(msg, &st->f_fsid)) != 0 ||
363 (r = sshbuf_get_u64(msg, &flag)) != 0 ||
364 (r = sshbuf_get_u64(msg, &st->f_namemax)) != 0)
365 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000366
367 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
368 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
369
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000370 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000371
372 return 0;
373}
374
Damien Miller3db5f532002-02-13 14:10:32 +1100375struct sftp_conn *
Damien Miller65e42f82010-09-24 22:15:11 +1000376do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
377 u_int64_t limit_kbps)
Damien Miller33804262001-02-04 23:20:18 +1100378{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000379 u_char type;
380 struct sshbuf *msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100381 struct sftp_conn *ret;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000382 int r;
Damien Miller33804262001-02-04 23:20:18 +1100383
Damien Millerfec029f2013-08-21 02:42:12 +1000384 ret = xcalloc(1, sizeof(*ret));
385 ret->msg_id = 1;
Damien Miller65e42f82010-09-24 22:15:11 +1000386 ret->fd_in = fd_in;
387 ret->fd_out = fd_out;
388 ret->transfer_buflen = transfer_buflen;
389 ret->num_requests = num_requests;
390 ret->exts = 0;
391 ret->limit_kbps = 0;
392
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000393 if ((msg = sshbuf_new()) == NULL)
394 fatal("%s: sshbuf_new failed", __func__);
395 if ((r = sshbuf_put_u8(msg, SSH2_FXP_INIT)) != 0 ||
396 (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0)
397 fatal("%s: buffer error: %s", __func__, ssh_err(r));
398 send_msg(ret, msg);
Damien Miller33804262001-02-04 23:20:18 +1100399
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000400 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +1100401
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000402 get_msg(ret, msg);
Damien Miller33804262001-02-04 23:20:18 +1100403
Kevin Stevesef4eea92001-02-05 12:42:17 +0000404 /* Expecting a VERSION reply */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000405 if ((r = sshbuf_get_u8(msg, &type)) != 0)
406 fatal("%s: buffer error: %s", __func__, ssh_err(r));
407 if (type != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000408 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100409 type);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000410 sshbuf_free(msg);
jsg@openbsd.org64a89ec2015-05-23 14:28:37 +0000411 free(ret);
Damien Miller3db5f532002-02-13 14:10:32 +1100412 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100413 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000414 if ((r = sshbuf_get_u32(msg, &ret->version)) != 0)
415 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100416
Damien Miller65e42f82010-09-24 22:15:11 +1000417 debug2("Remote version: %u", ret->version);
Damien Miller33804262001-02-04 23:20:18 +1100418
419 /* Check for extensions */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000420 while (sshbuf_len(msg) > 0) {
421 char *name;
422 u_char *value;
423 size_t vlen;
Darren Tuckera64ab332008-06-13 07:01:29 +1000424 int known = 0;
Damien Miller33804262001-02-04 23:20:18 +1100425
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000426 if ((r = sshbuf_get_cstring(msg, &name, NULL)) != 0 ||
427 (r = sshbuf_get_string(msg, &value, &vlen)) != 0)
428 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000429 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000430 strcmp((char *)value, "1") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000431 ret->exts |= SFTP_EXT_POSIX_RENAME;
Darren Tuckera64ab332008-06-13 07:01:29 +1000432 known = 1;
433 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000434 strcmp((char *)value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000435 ret->exts |= SFTP_EXT_STATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000436 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100437 } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000438 strcmp((char *)value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000439 ret->exts |= SFTP_EXT_FSTATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000440 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100441 } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000442 strcmp((char *)value, "1") == 0) {
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100443 ret->exts |= SFTP_EXT_HARDLINK;
444 known = 1;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000445 } else if (strcmp(name, "fsync@openssh.com") == 0 &&
446 strcmp((char *)value, "1") == 0) {
447 ret->exts |= SFTP_EXT_FSYNC;
448 known = 1;
Darren Tuckera64ab332008-06-13 07:01:29 +1000449 }
450 if (known) {
451 debug2("Server supports extension \"%s\" revision %s",
452 name, value);
453 } else {
454 debug2("Unrecognised server extension \"%s\"", name);
455 }
Darren Tuckera627d422013-06-02 07:31:17 +1000456 free(name);
457 free(value);
Damien Miller33804262001-02-04 23:20:18 +1100458 }
459
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000460 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +1100461
Damien Miller3db5f532002-02-13 14:10:32 +1100462 /* Some filexfer v.0 servers don't support large packets */
Damien Miller65e42f82010-09-24 22:15:11 +1000463 if (ret->version == 0)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000464 ret->transfer_buflen = MINIMUM(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100465
Damien Miller65e42f82010-09-24 22:15:11 +1000466 ret->limit_kbps = limit_kbps;
467 if (ret->limit_kbps > 0) {
468 bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
469 ret->transfer_buflen);
470 bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
471 ret->transfer_buflen);
472 }
473
474 return ret;
Damien Miller3db5f532002-02-13 14:10:32 +1100475}
476
477u_int
478sftp_proto_version(struct sftp_conn *conn)
479{
Damien Miller65e42f82010-09-24 22:15:11 +1000480 return conn->version;
Damien Miller33804262001-02-04 23:20:18 +1100481}
482
483int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000484do_close(struct sftp_conn *conn, const u_char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100485{
486 u_int id, status;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000487 struct sshbuf *msg;
488 int r;
Damien Miller33804262001-02-04 23:20:18 +1100489
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000490 if ((msg = sshbuf_new()) == NULL)
491 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100492
Damien Miller3db5f532002-02-13 14:10:32 +1100493 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000494 if ((r = sshbuf_put_u8(msg, SSH2_FXP_CLOSE)) != 0 ||
495 (r = sshbuf_put_u32(msg, id)) != 0 ||
496 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
497 fatal("%s: buffer error: %s", __func__, ssh_err(r));
498 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000499 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100500
Damien Miller65e42f82010-09-24 22:15:11 +1000501 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100502 if (status != SSH2_FX_OK)
503 error("Couldn't close file: %s", fx2txt(status));
504
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000505 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100506
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000507 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100508}
509
Damien Miller4870afd2001-03-14 10:27:09 +1100510
Ben Lindstrombba81212001-06-25 05:01:22 +0000511static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000512do_lsreaddir(struct sftp_conn *conn, const char *path, int print_flag,
Damien Miller4870afd2001-03-14 10:27:09 +1100513 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100514{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000515 struct sshbuf *msg;
516 u_int count, id, i, expected_id, ents = 0;
517 size_t handle_len;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000518 u_char type, *handle;
Damien Miller39392072013-12-07 10:31:08 +1100519 int status = SSH2_FX_FAILURE;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000520 int r;
Damien Miller39392072013-12-07 10:31:08 +1100521
522 if (dir)
523 *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +1100524
Damien Miller3db5f532002-02-13 14:10:32 +1100525 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100526
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000527 if ((msg = sshbuf_new()) == NULL)
528 fatal("%s: sshbuf_new failed", __func__);
529 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPENDIR)) != 0 ||
530 (r = sshbuf_put_u32(msg, id)) != 0 ||
531 (r = sshbuf_put_cstring(msg, path)) != 0)
532 fatal("%s: buffer error: %s", __func__, ssh_err(r));
533 send_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100534
Damien Miller65e42f82010-09-24 22:15:11 +1000535 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +1100536 "remote readdir(\"%s\")", path);
Damien Miller57c38ac2011-09-22 21:42:45 +1000537 if (handle == NULL) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000538 sshbuf_free(msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000539 return -1;
Damien Miller57c38ac2011-09-22 21:42:45 +1000540 }
Damien Miller33804262001-02-04 23:20:18 +1100541
Damien Miller4870afd2001-03-14 10:27:09 +1100542 if (dir) {
543 ents = 0;
Damien Miller6c81fee2013-11-08 12:19:55 +1100544 *dir = xcalloc(1, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100545 (*dir)[0] = NULL;
546 }
Damien Miller4870afd2001-03-14 10:27:09 +1100547
Darren Tuckercdf547a2004-05-24 10:12:19 +1000548 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100549 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100550
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000551 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100552
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000553 sshbuf_reset(msg);
554 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READDIR)) != 0 ||
555 (r = sshbuf_put_u32(msg, id)) != 0 ||
556 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
557 fatal("%s: buffer error: %s", __func__, ssh_err(r));
558 send_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100559
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000560 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +1100561
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000562 get_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100563
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000564 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
565 (r = sshbuf_get_u32(msg, &id)) != 0)
566 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100567
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000568 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100569
570 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000571 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100572
573 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000574 u_int rstatus;
575
576 if ((r = sshbuf_get_u32(msg, &rstatus)) != 0)
577 fatal("%s: buffer error: %s",
578 __func__, ssh_err(r));
579 debug3("Received SSH2_FXP_STATUS %d", rstatus);
580 if (rstatus == SSH2_FX_EOF)
Damien Miller33804262001-02-04 23:20:18 +1100581 break;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000582 error("Couldn't read directory: %s", fx2txt(rstatus));
Damien Miller39392072013-12-07 10:31:08 +1100583 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100584 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000585 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100586 SSH2_FXP_NAME, type);
587
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000588 if ((r = sshbuf_get_u32(msg, &count)) != 0)
589 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgc7995f22017-01-03 05:46:51 +0000590 if (count > SSHBUF_SIZE_MAX)
591 fatal("%s: nonsensical number of entries", __func__);
Damien Millerd7686fd2001-02-10 00:40:03 +1100592 if (count == 0)
593 break;
594 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100595 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100596 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000597 Attrib a;
Damien Miller33804262001-02-04 23:20:18 +1100598
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000599 if ((r = sshbuf_get_cstring(msg, &filename,
600 NULL)) != 0 ||
601 (r = sshbuf_get_cstring(msg, &longname,
602 NULL)) != 0)
603 fatal("%s: buffer error: %s",
604 __func__, ssh_err(r));
605 if ((r = decode_attrib(msg, &a)) != 0) {
606 error("%s: couldn't decode attrib: %s",
607 __func__, ssh_err(r));
608 free(filename);
609 free(longname);
610 sshbuf_free(msg);
611 return -1;
612 }
Damien Miller33804262001-02-04 23:20:18 +1100613
Damien Millerbda5c842013-10-15 12:05:58 +1100614 if (print_flag)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000615 mprintf("%s\n", longname);
Damien Miller4870afd2001-03-14 10:27:09 +1100616
Darren Tucker1b0dd172009-10-07 08:37:48 +1100617 /*
618 * Directory entries should never contain '/'
619 * These can be used to attack recursive ops
620 * (e.g. send '../../../../etc/passwd')
621 */
622 if (strchr(filename, '/') != NULL) {
623 error("Server sent suspect path \"%s\" "
624 "during readdir of \"%s\"", filename, path);
Damien Miller39392072013-12-07 10:31:08 +1100625 } else if (dir) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000626 *dir = xreallocarray(*dir, ents + 2, sizeof(**dir));
Damien Miller6c81fee2013-11-08 12:19:55 +1100627 (*dir)[ents] = xcalloc(1, sizeof(***dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100628 (*dir)[ents]->filename = xstrdup(filename);
629 (*dir)[ents]->longname = xstrdup(longname);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000630 memcpy(&(*dir)[ents]->a, &a, sizeof(a));
Damien Miller4870afd2001-03-14 10:27:09 +1100631 (*dir)[++ents] = NULL;
632 }
Darren Tuckera627d422013-06-02 07:31:17 +1000633 free(filename);
634 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100635 }
636 }
Damien Miller39392072013-12-07 10:31:08 +1100637 status = 0;
Damien Miller33804262001-02-04 23:20:18 +1100638
Damien Miller39392072013-12-07 10:31:08 +1100639 out:
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000640 sshbuf_free(msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100641 do_close(conn, handle, handle_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000642 free(handle);
Damien Miller33804262001-02-04 23:20:18 +1100643
Damien Miller39392072013-12-07 10:31:08 +1100644 if (status != 0 && dir != NULL) {
645 /* Don't return results on error */
646 free_sftp_dirents(*dir);
647 *dir = NULL;
648 } else if (interrupted && dir != NULL && *dir != NULL) {
649 /* Don't return partial matches on interrupt */
Darren Tuckercdf547a2004-05-24 10:12:19 +1000650 free_sftp_dirents(*dir);
Damien Miller6c81fee2013-11-08 12:19:55 +1100651 *dir = xcalloc(1, sizeof(**dir));
Darren Tuckercdf547a2004-05-24 10:12:19 +1000652 **dir = NULL;
653 }
654
Damien Miller39392072013-12-07 10:31:08 +1100655 return status;
Damien Miller33804262001-02-04 23:20:18 +1100656}
657
658int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000659do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100660{
Damien Miller3db5f532002-02-13 14:10:32 +1100661 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100662}
663
664void free_sftp_dirents(SFTP_DIRENT **s)
665{
666 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100667
Damien Miller39392072013-12-07 10:31:08 +1100668 if (s == NULL)
669 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100670 for (i = 0; s[i]; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000671 free(s[i]->filename);
672 free(s[i]->longname);
673 free(s[i]);
Damien Miller4870afd2001-03-14 10:27:09 +1100674 }
Darren Tuckera627d422013-06-02 07:31:17 +1000675 free(s);
Damien Miller4870afd2001-03-14 10:27:09 +1100676}
677
678int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000679do_rm(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100680{
681 u_int status, id;
682
683 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
684
Damien Miller3db5f532002-02-13 14:10:32 +1100685 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000686 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
687 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100688 if (status != SSH2_FX_OK)
689 error("Couldn't delete file: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000690 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100691}
692
693int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000694do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100695{
696 u_int status, id;
697
Damien Miller3db5f532002-02-13 14:10:32 +1100698 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000699 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100700 strlen(path), a);
701
Damien Miller65e42f82010-09-24 22:15:11 +1000702 status = get_status(conn, id);
Damien Millerbda5c842013-10-15 12:05:58 +1100703 if (status != SSH2_FX_OK && print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100704 error("Couldn't create directory: %s", fx2txt(status));
705
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000706 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100707}
708
709int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000710do_rmdir(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100711{
712 u_int status, id;
713
Damien Miller3db5f532002-02-13 14:10:32 +1100714 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000715 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100716 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100717
Damien Miller65e42f82010-09-24 22:15:11 +1000718 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100719 if (status != SSH2_FX_OK)
720 error("Couldn't remove directory: %s", fx2txt(status));
721
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000722 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100723}
724
725Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000726do_stat(struct sftp_conn *conn, const char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100727{
728 u_int id;
729
Damien Miller3db5f532002-02-13 14:10:32 +1100730 id = conn->msg_id++;
731
Damien Miller65e42f82010-09-24 22:15:11 +1000732 send_string_request(conn, id,
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000733 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100734 path, strlen(path));
735
Damien Miller65e42f82010-09-24 22:15:11 +1000736 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100737}
738
739Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000740do_lstat(struct sftp_conn *conn, const char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100741{
742 u_int id;
743
Damien Miller3db5f532002-02-13 14:10:32 +1100744 if (conn->version == 0) {
745 if (quiet)
746 debug("Server version does not support lstat operation");
747 else
Damien Miller996acd22003-04-09 20:59:48 +1000748 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000749 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100750 }
751
752 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000753 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100754 strlen(path));
755
Damien Miller65e42f82010-09-24 22:15:11 +1000756 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100757}
758
Damien Millercfe23d32008-02-10 22:20:44 +1100759#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100760Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000761do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
762 int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100763{
764 u_int id;
765
Damien Miller3db5f532002-02-13 14:10:32 +1100766 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000767 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
Damien Miller3db5f532002-02-13 14:10:32 +1100768 handle_len);
769
Damien Miller65e42f82010-09-24 22:15:11 +1000770 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100771}
Damien Millercfe23d32008-02-10 22:20:44 +1100772#endif
Damien Miller33804262001-02-04 23:20:18 +1100773
774int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000775do_setstat(struct sftp_conn *conn, const char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100776{
777 u_int status, id;
778
Damien Miller3db5f532002-02-13 14:10:32 +1100779 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000780 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100781 strlen(path), a);
782
Damien Miller65e42f82010-09-24 22:15:11 +1000783 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100784 if (status != SSH2_FX_OK)
785 error("Couldn't setstat on \"%s\": %s", path,
786 fx2txt(status));
787
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000788 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100789}
790
791int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000792do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100793 Attrib *a)
794{
795 u_int status, id;
796
Damien Miller3db5f532002-02-13 14:10:32 +1100797 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000798 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100799 handle_len, a);
800
Damien Miller65e42f82010-09-24 22:15:11 +1000801 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100802 if (status != SSH2_FX_OK)
803 error("Couldn't fsetstat: %s", fx2txt(status));
804
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000805 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100806}
807
808char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000809do_realpath(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100810{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000811 struct sshbuf *msg;
812 u_int expected_id, count, id;
Damien Miller33804262001-02-04 23:20:18 +1100813 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000814 Attrib a;
815 u_char type;
816 int r;
Damien Miller33804262001-02-04 23:20:18 +1100817
Damien Miller3db5f532002-02-13 14:10:32 +1100818 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000819 send_string_request(conn, id, SSH2_FXP_REALPATH, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100820 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100821
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000822 if ((msg = sshbuf_new()) == NULL)
823 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100824
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000825 get_msg(conn, msg);
826 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
827 (r = sshbuf_get_u32(msg, &id)) != 0)
828 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100829
830 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000831 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100832
833 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000834 u_int status;
Damien Miller33804262001-02-04 23:20:18 +1100835
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000836 if ((r = sshbuf_get_u32(msg, &status)) != 0)
837 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf29238e2013-10-17 11:48:52 +1100838 error("Couldn't canonicalize: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000839 sshbuf_free(msg);
Damien Miller49566312010-06-26 09:38:23 +1000840 return NULL;
Damien Miller33804262001-02-04 23:20:18 +1100841 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000842 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100843 SSH2_FXP_NAME, type);
844
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000845 if ((r = sshbuf_get_u32(msg, &count)) != 0)
846 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100847 if (count != 1)
848 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
849
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000850 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
851 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
852 (r = decode_attrib(msg, &a)) != 0)
853 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100854
Darren Tucker4908d442012-07-02 22:15:38 +1000855 debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000856 (unsigned long)a.size);
Damien Miller33804262001-02-04 23:20:18 +1100857
Darren Tuckera627d422013-06-02 07:31:17 +1000858 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100859
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000860 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100861
862 return(filename);
863}
864
865int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000866do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath,
Damien Millerc7dba122013-08-21 02:41:15 +1000867 int force_legacy)
Damien Miller33804262001-02-04 23:20:18 +1100868{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000869 struct sshbuf *msg;
Damien Miller33804262001-02-04 23:20:18 +1100870 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000871 int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
Damien Miller33804262001-02-04 23:20:18 +1100872
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000873 if ((msg = sshbuf_new()) == NULL)
874 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100875
876 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100877 id = conn->msg_id++;
Damien Millerc7dba122013-08-21 02:41:15 +1000878 if (use_ext) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000879 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
880 (r = sshbuf_put_u32(msg, id)) != 0 ||
881 (r = sshbuf_put_cstring(msg,
882 "posix-rename@openssh.com")) != 0)
883 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7a3e1d02008-03-27 10:59:57 +1100884 } else {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000885 if ((r = sshbuf_put_u8(msg, SSH2_FXP_RENAME)) != 0 ||
886 (r = sshbuf_put_u32(msg, id)) != 0)
887 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7a3e1d02008-03-27 10:59:57 +1100888 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000889 if ((r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
890 (r = sshbuf_put_cstring(msg, newpath)) != 0)
891 fatal("%s: buffer error: %s", __func__, ssh_err(r));
892 send_msg(conn, msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100893 debug3("Sent message %s \"%s\" -> \"%s\"",
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000894 use_ext ? "posix-rename@openssh.com" :
895 "SSH2_FXP_RENAME", oldpath, newpath);
896 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100897
Damien Miller65e42f82010-09-24 22:15:11 +1000898 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100899 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100900 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
901 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100902
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000903 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100904}
905
906int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000907do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100908{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000909 struct sshbuf *msg;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100910 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000911 int r;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100912
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100913 if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
914 error("Server does not support hardlink@openssh.com extension");
915 return -1;
916 }
917
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000918 if ((msg = sshbuf_new()) == NULL)
919 fatal("%s: sshbuf_new failed", __func__);
Damien Miller3decdba2011-09-22 21:41:05 +1000920
921 /* Send link request */
922 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000923 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
924 (r = sshbuf_put_u32(msg, id)) != 0 ||
925 (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
926 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
927 (r = sshbuf_put_cstring(msg, newpath)) != 0)
928 fatal("%s: buffer error: %s", __func__, ssh_err(r));
929 send_msg(conn, msg);
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100930 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
931 oldpath, newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000932 sshbuf_free(msg);
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100933
934 status = get_status(conn, id);
935 if (status != SSH2_FX_OK)
936 error("Couldn't link file \"%s\" to \"%s\": %s", oldpath,
937 newpath, fx2txt(status));
938
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000939 return status == SSH2_FX_OK ? 0 : -1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100940}
941
942int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000943do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100944{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000945 struct sshbuf *msg;
Damien Miller058316f2001-03-08 10:08:49 +1100946 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000947 int r;
Damien Miller058316f2001-03-08 10:08:49 +1100948
Damien Miller3db5f532002-02-13 14:10:32 +1100949 if (conn->version < 3) {
950 error("This server does not support the symlink operation");
951 return(SSH2_FX_OP_UNSUPPORTED);
952 }
953
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000954 if ((msg = sshbuf_new()) == NULL)
955 fatal("%s: sshbuf_new failed", __func__);
Damien Miller058316f2001-03-08 10:08:49 +1100956
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000957 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100958 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000959 if ((r = sshbuf_put_u8(msg, SSH2_FXP_SYMLINK)) != 0 ||
960 (r = sshbuf_put_u32(msg, id)) != 0 ||
961 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
962 (r = sshbuf_put_cstring(msg, newpath)) != 0)
963 fatal("%s: buffer error: %s", __func__, ssh_err(r));
964 send_msg(conn, msg);
Damien Miller058316f2001-03-08 10:08:49 +1100965 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
966 newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000967 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +1100968
Damien Miller65e42f82010-09-24 22:15:11 +1000969 status = get_status(conn, id);
Damien Miller058316f2001-03-08 10:08:49 +1100970 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000971 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100972 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100973
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000974 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller058316f2001-03-08 10:08:49 +1100975}
976
Damien Millerf29238e2013-10-17 11:48:52 +1100977int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000978do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len)
Damien Millerf29238e2013-10-17 11:48:52 +1100979{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000980 struct sshbuf *msg;
Damien Millerf29238e2013-10-17 11:48:52 +1100981 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000982 int r;
Damien Millerf29238e2013-10-17 11:48:52 +1100983
984 /* Silently return if the extension is not supported */
985 if ((conn->exts & SFTP_EXT_FSYNC) == 0)
986 return -1;
987
Damien Millerf29238e2013-10-17 11:48:52 +1100988 /* Send fsync request */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000989 if ((msg = sshbuf_new()) == NULL)
990 fatal("%s: sshbuf_new failed", __func__);
Damien Millerf29238e2013-10-17 11:48:52 +1100991 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000992 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
993 (r = sshbuf_put_u32(msg, id)) != 0 ||
994 (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
995 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
996 fatal("%s: buffer error: %s", __func__, ssh_err(r));
997 send_msg(conn, msg);
Damien Millerf29238e2013-10-17 11:48:52 +1100998 debug3("Sent message fsync@openssh.com I:%u", id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000999 sshbuf_free(msg);
Damien Millerf29238e2013-10-17 11:48:52 +11001000
1001 status = get_status(conn, id);
1002 if (status != SSH2_FX_OK)
1003 error("Couldn't sync file: %s", fx2txt(status));
1004
1005 return status;
1006}
1007
Damien Millercfe23d32008-02-10 22:20:44 +11001008#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +11001009char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001010do_readlink(struct sftp_conn *conn, const char *path)
Damien Miller058316f2001-03-08 10:08:49 +11001011{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001012 struct sshbuf *msg;
1013 u_int expected_id, count, id;
Damien Miller058316f2001-03-08 10:08:49 +11001014 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001015 Attrib a;
1016 u_char type;
1017 int r;
Damien Miller058316f2001-03-08 10:08:49 +11001018
Damien Miller3db5f532002-02-13 14:10:32 +11001019 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +10001020 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +11001021
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001022 if ((msg = sshbuf_new()) == NULL)
1023 fatal("%s: sshbuf_new failed", __func__);
Damien Miller058316f2001-03-08 10:08:49 +11001024
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001025 get_msg(conn, msg);
1026 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1027 (r = sshbuf_get_u32(msg, &id)) != 0)
1028 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001029
1030 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001031 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +11001032
1033 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001034 u_int status;
Damien Miller058316f2001-03-08 10:08:49 +11001035
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001036 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1037 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001038 error("Couldn't readlink: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001039 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +11001040 return(NULL);
1041 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001042 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +11001043 SSH2_FXP_NAME, type);
1044
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001045 if ((r = sshbuf_get_u32(msg, &count)) != 0)
1046 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001047 if (count != 1)
1048 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
1049
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001050 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
1051 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
1052 (r = decode_attrib(msg, &a)) != 0)
1053 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001054
1055 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
1056
Darren Tuckera627d422013-06-02 07:31:17 +10001057 free(longname);
Damien Miller058316f2001-03-08 10:08:49 +11001058
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001059 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +11001060
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001061 return filename;
Damien Miller058316f2001-03-08 10:08:49 +11001062}
Damien Millercfe23d32008-02-10 22:20:44 +11001063#endif
Damien Miller058316f2001-03-08 10:08:49 +11001064
Damien Millerd671e5a2008-05-19 14:53:33 +10001065int
Darren Tucker7b598892008-06-09 22:49:36 +10001066do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
Damien Millerd671e5a2008-05-19 14:53:33 +10001067 int quiet)
1068{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001069 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +10001070 u_int id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001071 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +10001072
1073 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
1074 error("Server does not support statvfs@openssh.com extension");
1075 return -1;
1076 }
1077
1078 id = conn->msg_id++;
1079
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001080 if ((msg = sshbuf_new()) == NULL)
1081 fatal("%s: sshbuf_new failed", __func__);
1082 sshbuf_reset(msg);
1083 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1084 (r = sshbuf_put_u32(msg, id)) != 0 ||
1085 (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
1086 (r = sshbuf_put_cstring(msg, path)) != 0)
1087 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1088 send_msg(conn, msg);
1089 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +10001090
Damien Miller65e42f82010-09-24 22:15:11 +10001091 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +10001092}
1093
1094#ifdef notyet
1095int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001096do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
Darren Tucker7b598892008-06-09 22:49:36 +10001097 struct sftp_statvfs *st, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +10001098{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001099 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +10001100 u_int id;
1101
1102 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
1103 error("Server does not support fstatvfs@openssh.com extension");
1104 return -1;
1105 }
1106
1107 id = conn->msg_id++;
1108
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001109 if ((msg = sshbuf_new()) == NULL)
1110 fatal("%s: sshbuf_new failed", __func__);
1111 sshbuf_reset(msg);
1112 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1113 (r = sshbuf_put_u32(msg, id)) != 0 ||
1114 (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
1115 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1116 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1117 send_msg(conn, msg);
1118 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +10001119
Damien Miller65e42f82010-09-24 22:15:11 +10001120 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +10001121}
1122#endif
1123
Damien Miller16a13332002-02-13 14:03:56 +11001124static void
Damien Miller65e42f82010-09-24 22:15:11 +10001125send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001126 u_int len, const u_char *handle, u_int handle_len)
Damien Miller16a13332002-02-13 14:03:56 +11001127{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001128 struct sshbuf *msg;
1129 int r;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001130
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001131 if ((msg = sshbuf_new()) == NULL)
1132 fatal("%s: sshbuf_new failed", __func__);
1133 sshbuf_reset(msg);
1134 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READ)) != 0 ||
1135 (r = sshbuf_put_u32(msg, id)) != 0 ||
1136 (r = sshbuf_put_string(msg, handle, handle_len)) != 0 ||
1137 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1138 (r = sshbuf_put_u32(msg, len)) != 0)
1139 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1140 send_msg(conn, msg);
1141 sshbuf_free(msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001142}
Damien Miller16a13332002-02-13 14:03:56 +11001143
Damien Miller058316f2001-03-08 10:08:49 +11001144int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001145do_download(struct sftp_conn *conn, const char *remote_path,
1146 const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
1147 int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001148{
Darren Tucker1b0dd172009-10-07 08:37:48 +11001149 Attrib junk;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001150 struct sshbuf *msg;
1151 u_char *handle;
1152 int local_fd = -1, write_error;
1153 int read_error, write_errno, reordered = 0, r;
Damien Miller0d032412013-07-25 11:56:52 +10001154 u_int64_t offset = 0, size, highwater;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001155 u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
Damien Miller62d57f62003-01-10 21:43:24 +11001156 off_t progress_counter;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001157 size_t handle_len;
Damien Miller0d032412013-07-25 11:56:52 +10001158 struct stat st;
Damien Miller16a13332002-02-13 14:03:56 +11001159 struct request {
1160 u_int id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001161 size_t len;
Damien Miller16a13332002-02-13 14:03:56 +11001162 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001163 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +11001164 };
1165 TAILQ_HEAD(reqhead, request) requests;
1166 struct request *req;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001167 u_char type;
Damien Miller16a13332002-02-13 14:03:56 +11001168
1169 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +11001170
Darren Tucker1b0dd172009-10-07 08:37:48 +11001171 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1172 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001173
Damien Miller9e720282008-06-29 22:46:35 +10001174 /* Do not preserve set[ug]id here, as we do not preserve ownership */
Damien Miller33804262001-02-04 23:20:18 +11001175 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +11001176 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +11001177 else
1178 mode = 0666;
1179
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001180 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +11001181 (!S_ISREG(a->perm))) {
1182 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001183 return(-1);
1184 }
1185
Damien Miller16a13332002-02-13 14:03:56 +11001186 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1187 size = a->size;
1188 else
1189 size = 0;
1190
Damien Miller3db5f532002-02-13 14:10:32 +11001191 buflen = conn->transfer_buflen;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001192 if ((msg = sshbuf_new()) == NULL)
1193 fatal("%s: sshbuf_new failed", __func__);
1194
1195 attrib_clear(&junk); /* Send empty attributes */
Damien Miller33804262001-02-04 23:20:18 +11001196
1197 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001198 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001199 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1200 (r = sshbuf_put_u32(msg, id)) != 0 ||
1201 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1202 (r = sshbuf_put_u32(msg, SSH2_FXF_READ)) != 0 ||
1203 (r = encode_attrib(msg, &junk)) != 0)
1204 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1205 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001206 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001207
Damien Miller65e42f82010-09-24 22:15:11 +10001208 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001209 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001210 if (handle == NULL) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001211 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +11001212 return(-1);
1213 }
1214
Damien Millerbda5c842013-10-15 12:05:58 +11001215 local_fd = open(local_path,
1216 O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
Damien Miller3db5f532002-02-13 14:10:32 +11001217 if (local_fd == -1) {
1218 error("Couldn't open local file \"%s\" for writing: %s",
1219 local_path, strerror(errno));
Damien Miller0d032412013-07-25 11:56:52 +10001220 goto fail;
1221 }
1222 offset = highwater = 0;
Damien Millerbda5c842013-10-15 12:05:58 +11001223 if (resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001224 if (fstat(local_fd, &st) == -1) {
1225 error("Unable to stat local file \"%s\": %s",
1226 local_path, strerror(errno));
1227 goto fail;
1228 }
Damien Millerc3d483f2014-01-17 11:20:26 +11001229 if (st.st_size < 0) {
1230 error("\"%s\" has negative size", local_path);
1231 goto fail;
1232 }
1233 if ((u_int64_t)st.st_size > size) {
Damien Miller0d032412013-07-25 11:56:52 +10001234 error("Unable to resume download of \"%s\": "
1235 "local file is larger than remote", local_path);
1236 fail:
1237 do_close(conn, handle, handle_len);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001238 sshbuf_free(msg);
Damien Miller0d032412013-07-25 11:56:52 +10001239 free(handle);
Damien Miller9275df32013-12-05 10:26:32 +11001240 if (local_fd != -1)
1241 close(local_fd);
Damien Miller0d032412013-07-25 11:56:52 +10001242 return -1;
1243 }
1244 offset = highwater = st.st_size;
Damien Miller3db5f532002-02-13 14:10:32 +11001245 }
1246
Damien Miller33804262001-02-04 23:20:18 +11001247 /* Read from remote and write to local */
Damien Miller0d032412013-07-25 11:56:52 +10001248 write_error = read_error = write_errno = num_req = 0;
Damien Miller16a13332002-02-13 14:03:56 +11001249 max_req = 1;
Damien Miller0d032412013-07-25 11:56:52 +10001250 progress_counter = offset;
Damien Miller62d57f62003-01-10 21:43:24 +11001251
Damien Miller9ba30692004-03-08 23:12:02 +11001252 if (showprogress && size != 0)
1253 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001254
Damien Miller16a13332002-02-13 14:03:56 +11001255 while (num_req > 0 || max_req > 0) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001256 u_char *data;
1257 size_t len;
Damien Miller33804262001-02-04 23:20:18 +11001258
Darren Tuckercdf547a2004-05-24 10:12:19 +10001259 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001260 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001261 * allow outstanding requests to drain gracefully
1262 */
1263 if (interrupted) {
1264 if (num_req == 0) /* If we haven't started yet... */
1265 break;
1266 max_req = 0;
1267 }
1268
Damien Miller16a13332002-02-13 14:03:56 +11001269 /* Send some more requests */
1270 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001271 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +00001272 (unsigned long long)offset,
1273 (unsigned long long)offset + buflen - 1,
1274 num_req, max_req);
Damien Miller6c81fee2013-11-08 12:19:55 +11001275 req = xcalloc(1, sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +11001276 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001277 req->len = buflen;
1278 req->offset = offset;
1279 offset += buflen;
1280 num_req++;
1281 TAILQ_INSERT_TAIL(&requests, req, tq);
Damien Miller65e42f82010-09-24 22:15:11 +10001282 send_read_request(conn, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001283 req->len, handle, handle_len);
1284 }
Damien Miller33804262001-02-04 23:20:18 +11001285
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001286 sshbuf_reset(msg);
1287 get_msg(conn, msg);
1288 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1289 (r = sshbuf_get_u32(msg, &id)) != 0)
1290 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001291 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001292
Damien Miller16a13332002-02-13 14:03:56 +11001293 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001294 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001295 req != NULL && req->id != id;
1296 req = TAILQ_NEXT(req, tq))
1297 ;
1298 if (req == NULL)
1299 fatal("Unexpected reply %u", id);
1300
1301 switch (type) {
1302 case SSH2_FXP_STATUS:
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001303 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1304 fatal("%s: buffer error: %s",
1305 __func__, ssh_err(r));
Damien Miller16a13332002-02-13 14:03:56 +11001306 if (status != SSH2_FX_EOF)
1307 read_error = 1;
1308 max_req = 0;
1309 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001310 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001311 num_req--;
1312 break;
1313 case SSH2_FXP_DATA:
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001314 if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1315 fatal("%s: buffer error: %s",
1316 __func__, ssh_err(r));
Ben Lindstromeb505452002-03-22 01:03:15 +00001317 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001318 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001319 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001320 if (len > req->len)
1321 fatal("Received more data than asked for "
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001322 "%zu > %zu", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001323 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001324 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001325 !write_error) {
1326 write_errno = errno;
1327 write_error = 1;
1328 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001329 }
Damien Miller0d032412013-07-25 11:56:52 +10001330 else if (!reordered && req->offset <= highwater)
1331 highwater = req->offset + len;
1332 else if (!reordered && req->offset > highwater)
1333 reordered = 1;
Damien Miller62d57f62003-01-10 21:43:24 +11001334 progress_counter += len;
Darren Tuckera627d422013-06-02 07:31:17 +10001335 free(data);
Damien Miller16a13332002-02-13 14:03:56 +11001336
1337 if (len == req->len) {
1338 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001339 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001340 num_req--;
1341 } else {
1342 /* Resend the request for the missing data */
1343 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001344 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001345 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001346 (unsigned long long)req->offset +
1347 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001348 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001349 req->len -= len;
1350 req->offset += len;
Damien Miller65e42f82010-09-24 22:15:11 +10001351 send_read_request(conn, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001352 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001353 /* Reduce the request size */
1354 if (len < buflen)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001355 buflen = MAXIMUM(MIN_READ_SIZE, len);
Damien Miller16a13332002-02-13 14:03:56 +11001356 }
1357 if (max_req > 0) { /* max_req = 0 iff EOF received */
1358 if (size > 0 && offset > size) {
1359 /* Only one request at a time
1360 * after the expected EOF */
1361 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001362 (unsigned long long)offset,
1363 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001364 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001365 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001366 ++max_req;
1367 }
1368 }
1369 break;
1370 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001371 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001372 SSH2_FXP_DATA, type);
1373 }
Damien Miller33804262001-02-04 23:20:18 +11001374 }
Damien Miller33804262001-02-04 23:20:18 +11001375
Damien Miller62d57f62003-01-10 21:43:24 +11001376 if (showprogress && size)
1377 stop_progress_meter();
1378
Damien Miller16a13332002-02-13 14:03:56 +11001379 /* Sanity check */
1380 if (TAILQ_FIRST(&requests) != NULL)
1381 fatal("Transfer complete, but requests still in queue");
Damien Miller0d032412013-07-25 11:56:52 +10001382 /* Truncate at highest contiguous point to avoid holes on interrupt */
1383 if (read_error || write_error || interrupted) {
Damien Millerbda5c842013-10-15 12:05:58 +11001384 if (reordered && resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001385 error("Unable to resume download of \"%s\": "
1386 "server reordered requests", local_path);
1387 }
1388 debug("truncating at %llu", (unsigned long long)highwater);
djm@openbsd.org9cc68422015-05-28 04:50:53 +00001389 if (ftruncate(local_fd, highwater) == -1)
1390 error("ftruncate \"%s\": %s", local_path,
1391 strerror(errno));
Damien Miller0d032412013-07-25 11:56:52 +10001392 }
Damien Miller16a13332002-02-13 14:03:56 +11001393 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001394 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001395 remote_path, fx2txt(status));
Damien Millerfec029f2013-08-21 02:42:12 +10001396 status = -1;
Damien Miller3db5f532002-02-13 14:10:32 +11001397 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001398 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001399 error("Couldn't write to \"%s\": %s", local_path,
1400 strerror(write_errno));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001401 status = SSH2_FX_FAILURE;
Damien Miller3db5f532002-02-13 14:10:32 +11001402 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001403 } else {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001404 if (do_close(conn, handle, handle_len) != 0 || interrupted)
1405 status = SSH2_FX_FAILURE;
1406 else
1407 status = SSH2_FX_OK;
Damien Miller16a13332002-02-13 14:03:56 +11001408 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001409#ifdef HAVE_FCHMOD
Damien Millerbda5c842013-10-15 12:05:58 +11001410 if (preserve_flag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001411#else
Damien Millerbda5c842013-10-15 12:05:58 +11001412 if (preserve_flag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001413#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001414 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001415 strerror(errno));
Damien Millerbda5c842013-10-15 12:05:58 +11001416 if (preserve_flag &&
1417 (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
Damien Miller16a13332002-02-13 14:03:56 +11001418 struct timeval tv[2];
1419 tv[0].tv_sec = a->atime;
1420 tv[1].tv_sec = a->mtime;
1421 tv[0].tv_usec = tv[1].tv_usec = 0;
1422 if (utimes(local_path, tv) == -1)
1423 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001424 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001425 }
Damien Millerf29238e2013-10-17 11:48:52 +11001426 if (fsync_flag) {
1427 debug("syncing \"%s\"", local_path);
1428 if (fsync(local_fd) == -1)
1429 error("Couldn't sync file \"%s\": %s",
1430 local_path, strerror(errno));
1431 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001432 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001433 close(local_fd);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001434 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001435 free(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001436
1437 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001438}
1439
Darren Tucker1b0dd172009-10-07 08:37:48 +11001440static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001441download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1442 int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
1443 int resume_flag, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001444{
1445 int i, ret = 0;
1446 SFTP_DIRENT **dir_entries;
1447 char *filename, *new_src, *new_dst;
1448 mode_t mode = 0777;
1449
1450 if (depth >= MAX_DIR_DEPTH) {
1451 error("Maximum directory depth exceeded: %d levels", depth);
1452 return -1;
1453 }
1454
1455 if (dirattrib == NULL &&
1456 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1457 error("Unable to stat remote directory \"%s\"", src);
1458 return -1;
1459 }
1460 if (!S_ISDIR(dirattrib->perm)) {
1461 error("\"%s\" is not a directory", src);
1462 return -1;
1463 }
Damien Millerbda5c842013-10-15 12:05:58 +11001464 if (print_flag)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001465 mprintf("Retrieving %s\n", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001466
1467 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1468 mode = dirattrib->perm & 01777;
1469 else {
1470 debug("Server did not send permissions for "
1471 "directory \"%s\"", dst);
1472 }
1473
1474 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1475 error("mkdir %s: %s", dst, strerror(errno));
1476 return -1;
1477 }
1478
1479 if (do_readdir(conn, src, &dir_entries) == -1) {
1480 error("%s: Failed to get directory contents", src);
1481 return -1;
1482 }
1483
1484 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1485 filename = dir_entries[i]->filename;
1486
1487 new_dst = path_append(dst, filename);
1488 new_src = path_append(src, filename);
1489
1490 if (S_ISDIR(dir_entries[i]->a.perm)) {
1491 if (strcmp(filename, ".") == 0 ||
1492 strcmp(filename, "..") == 0)
1493 continue;
1494 if (download_dir_internal(conn, new_src, new_dst,
Damien Millerbda5c842013-10-15 12:05:58 +11001495 depth + 1, &(dir_entries[i]->a), preserve_flag,
Damien Millerf29238e2013-10-17 11:48:52 +11001496 print_flag, resume_flag, fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001497 ret = -1;
1498 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1499 if (do_download(conn, new_src, new_dst,
Damien Millerf29238e2013-10-17 11:48:52 +11001500 &(dir_entries[i]->a), preserve_flag,
1501 resume_flag, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001502 error("Download of file %s to %s failed",
1503 new_src, new_dst);
1504 ret = -1;
1505 }
1506 } else
1507 logit("%s: not a regular file\n", new_src);
1508
Darren Tuckera627d422013-06-02 07:31:17 +10001509 free(new_dst);
1510 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001511 }
1512
Damien Millerbda5c842013-10-15 12:05:58 +11001513 if (preserve_flag) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001514 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1515 struct timeval tv[2];
1516 tv[0].tv_sec = dirattrib->atime;
1517 tv[1].tv_sec = dirattrib->mtime;
1518 tv[0].tv_usec = tv[1].tv_usec = 0;
1519 if (utimes(dst, tv) == -1)
1520 error("Can't set times on \"%s\": %s",
1521 dst, strerror(errno));
1522 } else
1523 debug("Server did not send times for directory "
1524 "\"%s\"", dst);
1525 }
1526
1527 free_sftp_dirents(dir_entries);
1528
1529 return ret;
1530}
1531
1532int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001533download_dir(struct sftp_conn *conn, const char *src, const char *dst,
1534 Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1535 int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001536{
1537 char *src_canon;
1538 int ret;
1539
1540 if ((src_canon = do_realpath(conn, src)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001541 error("Unable to canonicalize path \"%s\"", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001542 return -1;
1543 }
1544
Damien Millerbda5c842013-10-15 12:05:58 +11001545 ret = download_dir_internal(conn, src_canon, dst, 0,
Damien Millerf29238e2013-10-17 11:48:52 +11001546 dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag);
Darren Tuckera627d422013-06-02 07:31:17 +10001547 free(src_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001548 return ret;
1549}
1550
Damien Miller33804262001-02-04 23:20:18 +11001551int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001552do_upload(struct sftp_conn *conn, const char *local_path,
1553 const char *remote_path, int preserve_flag, int resume, int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001554{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001555 int r, local_fd;
1556 u_int status = SSH2_FX_OK;
1557 u_int id;
1558 u_char type;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001559 off_t offset, progress_counter;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001560 u_char *handle, *data;
1561 struct sshbuf *msg;
Damien Miller33804262001-02-04 23:20:18 +11001562 struct stat sb;
Damien Millerd8accc02014-05-15 13:46:25 +10001563 Attrib a, *c = NULL;
Damien Miller16a13332002-02-13 14:03:56 +11001564 u_int32_t startid;
1565 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001566 struct outstanding_ack {
1567 u_int id;
1568 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001569 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001570 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001571 };
1572 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001573 struct outstanding_ack *ack = NULL;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001574 size_t handle_len;
Damien Miller5873dfd2002-02-13 14:04:37 +11001575
1576 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001577
1578 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1579 error("Couldn't open local file \"%s\" for reading: %s",
1580 local_path, strerror(errno));
1581 return(-1);
1582 }
1583 if (fstat(local_fd, &sb) == -1) {
1584 error("Couldn't fstat local file \"%s\": %s",
1585 local_path, strerror(errno));
1586 close(local_fd);
1587 return(-1);
1588 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001589 if (!S_ISREG(sb.st_mode)) {
1590 error("%s is not a regular file", local_path);
1591 close(local_fd);
1592 return(-1);
1593 }
Damien Miller33804262001-02-04 23:20:18 +11001594 stat_to_attrib(&sb, &a);
1595
1596 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1597 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1598 a.perm &= 0777;
Damien Millerbda5c842013-10-15 12:05:58 +11001599 if (!preserve_flag)
Damien Miller33804262001-02-04 23:20:18 +11001600 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1601
Damien Millerd8accc02014-05-15 13:46:25 +10001602 if (resume) {
1603 /* Get remote file size if it exists */
1604 if ((c = do_stat(conn, remote_path, 0)) == NULL) {
djm@openbsd.org34a01b22016-04-08 08:19:17 +00001605 close(local_fd);
Damien Millerd8accc02014-05-15 13:46:25 +10001606 return -1;
1607 }
1608
1609 if ((off_t)c->size >= sb.st_size) {
1610 error("destination file bigger or same size as "
1611 "source file");
1612 close(local_fd);
1613 return -1;
1614 }
1615
1616 if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
1617 close(local_fd);
1618 return -1;
1619 }
1620 }
1621
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001622 if ((msg = sshbuf_new()) == NULL)
1623 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001624
1625 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001626 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001627 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1628 (r = sshbuf_put_u32(msg, id)) != 0 ||
1629 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1630 (r = sshbuf_put_u32(msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|
1631 (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC))) != 0 ||
1632 (r = encode_attrib(msg, &a)) != 0)
1633 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1634 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001635 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001636
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001637 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +11001638
Damien Miller65e42f82010-09-24 22:15:11 +10001639 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001640 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001641 if (handle == NULL) {
1642 close(local_fd);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001643 sshbuf_free(msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001644 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001645 }
1646
Damien Miller16a13332002-02-13 14:03:56 +11001647 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001648 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001649
Damien Miller33804262001-02-04 23:20:18 +11001650 /* Read from local and write to remote */
Damien Millerd8accc02014-05-15 13:46:25 +10001651 offset = progress_counter = (resume ? c->size : 0);
Damien Miller62d57f62003-01-10 21:43:24 +11001652 if (showprogress)
Darren Tuckerc9a19912013-06-02 08:37:05 +10001653 start_progress_meter(local_path, sb.st_size,
1654 &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001655
Damien Miller9f0f5c62001-12-21 14:45:46 +11001656 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001657 int len;
Damien Miller33804262001-02-04 23:20:18 +11001658
1659 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001660 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001661 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001662 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001663 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001664 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001665 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001666 len = 0;
1667 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001668 len = read(local_fd, data, conn->transfer_buflen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001669 while ((len == -1) &&
1670 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
Damien Miller33804262001-02-04 23:20:18 +11001671
1672 if (len == -1)
1673 fatal("Couldn't read from \"%s\": %s", local_path,
1674 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001675
1676 if (len != 0) {
Damien Miller6c81fee2013-11-08 12:19:55 +11001677 ack = xcalloc(1, sizeof(*ack));
Damien Miller5873dfd2002-02-13 14:04:37 +11001678 ack->id = ++id;
1679 ack->offset = offset;
1680 ack->len = len;
1681 TAILQ_INSERT_TAIL(&acks, ack, tq);
1682
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001683 sshbuf_reset(msg);
1684 if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
1685 (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
1686 (r = sshbuf_put_string(msg, handle,
1687 handle_len)) != 0 ||
1688 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1689 (r = sshbuf_put_string(msg, data, len)) != 0)
1690 fatal("%s: buffer error: %s",
1691 __func__, ssh_err(r));
1692 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001693 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001694 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001695 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001696 break;
1697
Damien Miller5873dfd2002-02-13 14:04:37 +11001698 if (ack == NULL)
1699 fatal("Unexpected ACK %u", id);
1700
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001701 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001702 id - ackid >= conn->num_requests) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001703 u_int rid;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001704
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001705 sshbuf_reset(msg);
1706 get_msg(conn, msg);
1707 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1708 (r = sshbuf_get_u32(msg, &rid)) != 0)
1709 fatal("%s: buffer error: %s",
1710 __func__, ssh_err(r));
Damien Miller5873dfd2002-02-13 14:04:37 +11001711
1712 if (type != SSH2_FXP_STATUS)
1713 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1714 "got %d", SSH2_FXP_STATUS, type);
1715
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001716 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1717 fatal("%s: buffer error: %s",
1718 __func__, ssh_err(r));
1719 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller5873dfd2002-02-13 14:04:37 +11001720
1721 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001722 for (ack = TAILQ_FIRST(&acks);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001723 ack != NULL && ack->id != rid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001724 ack = TAILQ_NEXT(ack, tq))
1725 ;
1726 if (ack == NULL)
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001727 fatal("Can't find request for ID %u", rid);
Damien Miller5873dfd2002-02-13 14:04:37 +11001728 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001729 debug3("In write loop, ack for %u %u bytes at %lld",
1730 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001731 ++ackid;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001732 progress_counter += ack->len;
Darren Tuckera627d422013-06-02 07:31:17 +10001733 free(ack);
Damien Miller33804262001-02-04 23:20:18 +11001734 }
Damien Miller33804262001-02-04 23:20:18 +11001735 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001736 if (offset < 0)
1737 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001738 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001739 sshbuf_free(msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001740
Damien Miller62d57f62003-01-10 21:43:24 +11001741 if (showprogress)
1742 stop_progress_meter();
Darren Tuckera627d422013-06-02 07:31:17 +10001743 free(data);
Damien Miller33804262001-02-04 23:20:18 +11001744
Damien Milleracdf25b2008-02-10 22:27:24 +11001745 if (status != SSH2_FX_OK) {
1746 error("Couldn't write to remote file \"%s\": %s",
1747 remote_path, fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001748 status = SSH2_FX_FAILURE;
Damien Milleracdf25b2008-02-10 22:27:24 +11001749 }
1750
Damien Miller33804262001-02-04 23:20:18 +11001751 if (close(local_fd) == -1) {
1752 error("Couldn't close local file \"%s\": %s", local_path,
1753 strerror(errno));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001754 status = SSH2_FX_FAILURE;
Damien Miller33804262001-02-04 23:20:18 +11001755 }
1756
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001757 /* Override umask and utimes if asked */
Damien Millerbda5c842013-10-15 12:05:58 +11001758 if (preserve_flag)
Damien Miller3db5f532002-02-13 14:10:32 +11001759 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001760
Damien Millerf29238e2013-10-17 11:48:52 +11001761 if (fsync_flag)
1762 (void)do_fsync(conn, handle, handle_len);
1763
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001764 if (do_close(conn, handle, handle_len) != 0)
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001765 status = SSH2_FX_FAILURE;
1766
Darren Tuckera627d422013-06-02 07:31:17 +10001767 free(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001768
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001769 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +11001770}
Darren Tucker1b0dd172009-10-07 08:37:48 +11001771
1772static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001773upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1774 int depth, int preserve_flag, int print_flag, int resume, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001775{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001776 int ret = 0;
Darren Tucker1b0dd172009-10-07 08:37:48 +11001777 DIR *dirp;
1778 struct dirent *dp;
1779 char *filename, *new_src, *new_dst;
1780 struct stat sb;
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001781 Attrib a, *dirattrib;
Darren Tucker1b0dd172009-10-07 08:37:48 +11001782
1783 if (depth >= MAX_DIR_DEPTH) {
1784 error("Maximum directory depth exceeded: %d levels", depth);
1785 return -1;
1786 }
1787
1788 if (stat(src, &sb) == -1) {
1789 error("Couldn't stat directory \"%s\": %s",
1790 src, strerror(errno));
1791 return -1;
1792 }
1793 if (!S_ISDIR(sb.st_mode)) {
1794 error("\"%s\" is not a directory", src);
1795 return -1;
1796 }
Damien Millerbda5c842013-10-15 12:05:58 +11001797 if (print_flag)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001798 mprintf("Entering %s\n", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001799
1800 attrib_clear(&a);
1801 stat_to_attrib(&sb, &a);
1802 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1803 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1804 a.perm &= 01777;
Damien Millerbda5c842013-10-15 12:05:58 +11001805 if (!preserve_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001806 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
Damien Miller0d032412013-07-25 11:56:52 +10001807
Darren Tucker1b0dd172009-10-07 08:37:48 +11001808 /*
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001809 * sftp lacks a portable status value to match errno EEXIST,
1810 * so if we get a failure back then we must check whether
1811 * the path already existed and is a directory.
Darren Tucker1b0dd172009-10-07 08:37:48 +11001812 */
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001813 if (do_mkdir(conn, dst, &a, 0) != 0) {
1814 if ((dirattrib = do_stat(conn, dst, 0)) == NULL)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001815 return -1;
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001816 if (!S_ISDIR(dirattrib->perm)) {
1817 error("\"%s\" exists but is not a directory", dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001818 return -1;
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001819 }
Darren Tucker1b0dd172009-10-07 08:37:48 +11001820 }
1821
1822 if ((dirp = opendir(src)) == NULL) {
1823 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1824 return -1;
1825 }
Damien Miller0d032412013-07-25 11:56:52 +10001826
Darren Tucker1b0dd172009-10-07 08:37:48 +11001827 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1828 if (dp->d_ino == 0)
1829 continue;
1830 filename = dp->d_name;
1831 new_dst = path_append(dst, filename);
1832 new_src = path_append(src, filename);
1833
Darren Tucker438b4732009-10-11 21:52:10 +11001834 if (lstat(new_src, &sb) == -1) {
1835 logit("%s: lstat failed: %s", filename,
1836 strerror(errno));
1837 ret = -1;
1838 } else if (S_ISDIR(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001839 if (strcmp(filename, ".") == 0 ||
1840 strcmp(filename, "..") == 0)
1841 continue;
1842
1843 if (upload_dir_internal(conn, new_src, new_dst,
Damien Millerd8accc02014-05-15 13:46:25 +10001844 depth + 1, preserve_flag, print_flag, resume,
Damien Millerf29238e2013-10-17 11:48:52 +11001845 fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001846 ret = -1;
Darren Tucker438b4732009-10-11 21:52:10 +11001847 } else if (S_ISREG(sb.st_mode)) {
Damien Millerbda5c842013-10-15 12:05:58 +11001848 if (do_upload(conn, new_src, new_dst,
Damien Millerd8accc02014-05-15 13:46:25 +10001849 preserve_flag, resume, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001850 error("Uploading of file %s to %s failed!",
1851 new_src, new_dst);
1852 ret = -1;
1853 }
1854 } else
1855 logit("%s: not a regular file\n", filename);
Darren Tuckera627d422013-06-02 07:31:17 +10001856 free(new_dst);
1857 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001858 }
1859
1860 do_setstat(conn, dst, &a);
1861
1862 (void) closedir(dirp);
1863 return ret;
1864}
1865
1866int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001867upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
1868 int preserve_flag, int print_flag, int resume, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001869{
1870 char *dst_canon;
1871 int ret;
1872
1873 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001874 error("Unable to canonicalize path \"%s\"", dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001875 return -1;
1876 }
1877
Damien Miller2f93d052013-10-15 12:06:27 +11001878 ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
Damien Millerd8accc02014-05-15 13:46:25 +10001879 print_flag, resume, fsync_flag);
Damien Millerf29238e2013-10-17 11:48:52 +11001880
Darren Tuckera627d422013-06-02 07:31:17 +10001881 free(dst_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001882 return ret;
1883}
1884
1885char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001886path_append(const char *p1, const char *p2)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001887{
1888 char *ret;
1889 size_t len = strlen(p1) + strlen(p2) + 2;
1890
1891 ret = xmalloc(len);
1892 strlcpy(ret, p1, len);
1893 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1894 strlcat(ret, "/", len);
1895 strlcat(ret, p2, len);
1896
1897 return(ret);
1898}
1899