blob: 626330262264a1b8adfef0cff301d4d8e447976e [file] [log] [blame]
djm@openbsd.org4b3ecbb2017-08-11 04:41:08 +00001/* $OpenBSD: sftp-client.c,v 1.127 2017/08/11 04:41:08 djm Exp $ */
Damien Miller33804262001-02-04 23:20:18 +11002/*
Damien Miller4e60ed72004-02-17 17:07:59 +11003 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
Damien Miller33804262001-02-04 23:20:18 +11004 *
Damien Miller4e60ed72004-02-17 17:07:59 +11005 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
Damien Miller33804262001-02-04 23:20:18 +11008 *
Damien Miller4e60ed72004-02-17 17:07:59 +11009 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller33804262001-02-04 23:20:18 +110016 */
17
18/* XXX: memleaks */
19/* XXX: signed vs unsigned */
Damien Miller3db5f532002-02-13 14:10:32 +110020/* XXX: remove all logging, only return status codes */
Damien Miller33804262001-02-04 23:20:18 +110021/* XXX: copy between two remote sites */
22
23#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110024
25#include <sys/types.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100026#ifdef HAVE_SYS_STATVFS_H
Damien Millerd671e5a2008-05-19 14:53:33 +100027#include <sys/statvfs.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100028#endif
Damien Millerd7834352006-08-05 12:39:39 +100029#include "openbsd-compat/sys-queue.h"
Damien Millerf17883e2006-03-15 11:45:54 +110030#ifdef HAVE_SYS_STAT_H
31# include <sys/stat.h>
32#endif
Damien Miller9aec9192006-08-05 10:57:45 +100033#ifdef HAVE_SYS_TIME_H
34# include <sys/time.h>
35#endif
Damien Millerd7834352006-08-05 12:39:39 +100036#include <sys/uio.h>
Damien Miller57cf6382006-07-10 21:13:46 +100037
Darren Tucker1b0dd172009-10-07 08:37:48 +110038#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100039#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100040#include <fcntl.h>
41#include <signal.h>
Damien Millerd7834352006-08-05 12:39:39 +100042#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100043#include <stdio.h>
Damien Miller0600c702013-11-21 13:55:43 +110044#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100045#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100046#include <unistd.h>
Damien Miller16a13332002-02-13 14:03:56 +110047
Damien Miller33804262001-02-04 23:20:18 +110048#include "xmalloc.h"
djm@openbsd.org7d845f42015-01-14 13:54:13 +000049#include "ssherr.h"
50#include "sshbuf.h"
Damien Miller33804262001-02-04 23:20:18 +110051#include "log.h"
52#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110053#include "progressmeter.h"
Damien Miller3f941882006-03-31 23:13:02 +110054#include "misc.h"
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +000055#include "utf8.h"
Damien Miller33804262001-02-04 23:20:18 +110056
57#include "sftp.h"
58#include "sftp-common.h"
59#include "sftp-client.h"
60
Darren Tuckercdf547a2004-05-24 10:12:19 +100061extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110062extern int showprogress;
63
Damien Miller0c8d8f62006-03-15 11:34:25 +110064/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110065#define MIN_READ_SIZE 512
66
Darren Tucker1b0dd172009-10-07 08:37:48 +110067/* Maximum depth to descend in directory trees */
68#define MAX_DIR_DEPTH 64
69
Damien Miller89f04852017-03-20 11:53:34 +110070/* Directory separator characters */
71#ifdef HAVE_CYGWIN
72# define SFTP_DIRECTORY_CHARS "/\\"
73#else /* HAVE_CYGWIN */
74# define SFTP_DIRECTORY_CHARS "/"
75#endif /* HAVE_CYGWIN */
76
Damien Miller3db5f532002-02-13 14:10:32 +110077struct sftp_conn {
78 int fd_in;
79 int fd_out;
80 u_int transfer_buflen;
81 u_int num_requests;
82 u_int version;
83 u_int msg_id;
Damien Millerd671e5a2008-05-19 14:53:33 +100084#define SFTP_EXT_POSIX_RENAME 0x00000001
85#define SFTP_EXT_STATVFS 0x00000002
86#define SFTP_EXT_FSTATVFS 0x00000004
Darren Tuckeraf1f9092010-12-05 09:02:47 +110087#define SFTP_EXT_HARDLINK 0x00000008
Damien Millerf29238e2013-10-17 11:48:52 +110088#define SFTP_EXT_FSYNC 0x00000010
Damien Miller7a3e1d02008-03-27 10:59:57 +110089 u_int exts;
Damien Miller65e42f82010-09-24 22:15:11 +100090 u_int64_t limit_kbps;
91 struct bwlimit bwlimit_in, bwlimit_out;
Damien Miller3db5f532002-02-13 14:10:32 +110092};
Ben Lindstrom288cc392001-02-09 02:58:04 +000093
djm@openbsd.org7d845f42015-01-14 13:54:13 +000094static u_char *
95get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
Damien Miller65e42f82010-09-24 22:15:11 +100096 const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
97
98/* ARGSUSED */
99static int
100sftpio(void *_bwlimit, size_t amount)
101{
102 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
103
104 bandwidth_limit(bwlimit, amount);
105 return 0;
106}
Darren Tuckerc22f0902009-10-07 08:24:19 +1100107
Ben Lindstrombba81212001-06-25 05:01:22 +0000108static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000109send_msg(struct sftp_conn *conn, struct sshbuf *m)
Damien Miller33804262001-02-04 23:20:18 +1100110{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100111 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +1000112 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +1100113
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000114 if (sshbuf_len(m) > SFTP_MAX_MSG_LENGTH)
115 fatal("Outbound message too long %zu", sshbuf_len(m));
Damien Miller33804262001-02-04 23:20:18 +1100116
Damien Millera7f3aaa2003-01-10 21:43:58 +1100117 /* Send length first */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000118 put_u32(mlen, sshbuf_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +1000119 iov[0].iov_base = mlen;
120 iov[0].iov_len = sizeof(mlen);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000121 iov[1].iov_base = (u_char *)sshbuf_ptr(m);
122 iov[1].iov_len = sshbuf_len(m);
Damien Millerd7834352006-08-05 12:39:39 +1000123
Damien Miller65e42f82010-09-24 22:15:11 +1000124 if (atomiciov6(writev, conn->fd_out, iov, 2,
Damien Miller0d032412013-07-25 11:56:52 +1000125 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_out) !=
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000126 sshbuf_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +1100127 fatal("Couldn't send packet: %s", strerror(errno));
128
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000129 sshbuf_reset(m);
Damien Miller33804262001-02-04 23:20:18 +1100130}
131
Ben Lindstrombba81212001-06-25 05:01:22 +0000132static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000133get_msg(struct sftp_conn *conn, struct sshbuf *m)
Damien Miller33804262001-02-04 23:20:18 +1100134{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100135 u_int msg_len;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000136 u_char *p;
137 int r;
Damien Miller33804262001-02-04 23:20:18 +1100138
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000139 if ((r = sshbuf_reserve(m, 4, &p)) != 0)
140 fatal("%s: buffer error: %s", __func__, ssh_err(r));
141 if (atomicio6(read, conn->fd_in, p, 4,
Damien Miller65e42f82010-09-24 22:15:11 +1000142 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in) != 4) {
djm@openbsd.org4b3ecbb2017-08-11 04:41:08 +0000143 if (errno == EPIPE || errno == ECONNRESET)
Damien Millerb253cc42005-05-26 12:23:44 +1000144 fatal("Connection closed");
145 else
146 fatal("Couldn't read packet: %s", strerror(errno));
147 }
Damien Miller33804262001-02-04 23:20:18 +1100148
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000149 if ((r = sshbuf_get_u32(m, &msg_len)) != 0)
150 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller54446182006-01-02 23:40:50 +1100151 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000152 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100153
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000154 if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
155 fatal("%s: buffer error: %s", __func__, ssh_err(r));
156 if (atomicio6(read, conn->fd_in, p, msg_len,
Damien Miller65e42f82010-09-24 22:15:11 +1000157 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in)
158 != msg_len) {
Damien Millerb253cc42005-05-26 12:23:44 +1000159 if (errno == EPIPE)
160 fatal("Connection closed");
161 else
162 fatal("Read packet: %s", strerror(errno));
163 }
Damien Miller33804262001-02-04 23:20:18 +1100164}
165
Ben Lindstrombba81212001-06-25 05:01:22 +0000166static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000167send_string_request(struct sftp_conn *conn, u_int id, u_int code, const char *s,
Damien Miller33804262001-02-04 23:20:18 +1100168 u_int len)
169{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000170 struct sshbuf *msg;
171 int r;
Damien Miller33804262001-02-04 23:20:18 +1100172
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000173 if ((msg = sshbuf_new()) == NULL)
174 fatal("%s: sshbuf_new failed", __func__);
175 if ((r = sshbuf_put_u8(msg, code)) != 0 ||
176 (r = sshbuf_put_u32(msg, id)) != 0 ||
177 (r = sshbuf_put_string(msg, s, len)) != 0)
178 fatal("%s: buffer error: %s", __func__, ssh_err(r));
179 send_msg(conn, msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000180 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000181 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100182}
183
Ben Lindstrombba81212001-06-25 05:01:22 +0000184static void
Damien Miller65e42f82010-09-24 22:15:11 +1000185send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000186 const void *s, u_int len, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100187{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000188 struct sshbuf *msg;
189 int r;
Damien Miller33804262001-02-04 23:20:18 +1100190
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000191 if ((msg = sshbuf_new()) == NULL)
192 fatal("%s: sshbuf_new failed", __func__);
193 if ((r = sshbuf_put_u8(msg, code)) != 0 ||
194 (r = sshbuf_put_u32(msg, id)) != 0 ||
195 (r = sshbuf_put_string(msg, s, len)) != 0 ||
196 (r = encode_attrib(msg, a)) != 0)
197 fatal("%s: buffer error: %s", __func__, ssh_err(r));
198 send_msg(conn, msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000199 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000200 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100201}
202
Ben Lindstrombba81212001-06-25 05:01:22 +0000203static u_int
Damien Miller65e42f82010-09-24 22:15:11 +1000204get_status(struct sftp_conn *conn, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100205{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000206 struct sshbuf *msg;
207 u_char type;
208 u_int id, status;
209 int r;
Damien Miller33804262001-02-04 23:20:18 +1100210
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000211 if ((msg = sshbuf_new()) == NULL)
212 fatal("%s: sshbuf_new failed", __func__);
213 get_msg(conn, msg);
214 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
215 (r = sshbuf_get_u32(msg, &id)) != 0)
216 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100217
218 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000219 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100220 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000221 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100222 SSH2_FXP_STATUS, type);
223
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000224 if ((r = sshbuf_get_u32(msg, &status)) != 0)
225 fatal("%s: buffer error: %s", __func__, ssh_err(r));
226 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100227
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000228 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100229
Damien Miller65e42f82010-09-24 22:15:11 +1000230 return status;
Damien Miller33804262001-02-04 23:20:18 +1100231}
232
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000233static u_char *
234get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
Damien Miller65e42f82010-09-24 22:15:11 +1000235 const char *errfmt, ...)
Damien Miller33804262001-02-04 23:20:18 +1100236{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000237 struct sshbuf *msg;
238 u_int id, status;
239 u_char type;
240 u_char *handle;
241 char errmsg[256];
Darren Tuckerc22f0902009-10-07 08:24:19 +1100242 va_list args;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000243 int r;
Darren Tuckerc22f0902009-10-07 08:24:19 +1100244
245 va_start(args, errfmt);
246 if (errfmt != NULL)
247 vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
248 va_end(args);
Damien Miller33804262001-02-04 23:20:18 +1100249
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000250 if ((msg = sshbuf_new()) == NULL)
251 fatal("%s: sshbuf_new failed", __func__);
252 get_msg(conn, msg);
253 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
254 (r = sshbuf_get_u32(msg, &id)) != 0)
255 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100256
257 if (id != expected_id)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100258 fatal("%s: ID mismatch (%u != %u)",
259 errfmt == NULL ? __func__ : errmsg, id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100260 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000261 if ((r = sshbuf_get_u32(msg, &status)) != 0)
262 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerc22f0902009-10-07 08:24:19 +1100263 if (errfmt != NULL)
264 error("%s: %s", errmsg, fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000265 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100266 return(NULL);
267 } else if (type != SSH2_FXP_HANDLE)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100268 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
269 errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
Damien Miller33804262001-02-04 23:20:18 +1100270
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000271 if ((r = sshbuf_get_string(msg, &handle, len)) != 0)
272 fatal("%s: buffer error: %s", __func__, ssh_err(r));
273 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100274
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000275 return handle;
Damien Miller33804262001-02-04 23:20:18 +1100276}
277
Ben Lindstrombba81212001-06-25 05:01:22 +0000278static Attrib *
Damien Miller65e42f82010-09-24 22:15:11 +1000279get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100280{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000281 struct sshbuf *msg;
282 u_int id;
283 u_char type;
284 int r;
285 static Attrib a;
Damien Miller33804262001-02-04 23:20:18 +1100286
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000287 if ((msg = sshbuf_new()) == NULL)
288 fatal("%s: sshbuf_new failed", __func__);
289 get_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100290
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000291 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
292 (r = sshbuf_get_u32(msg, &id)) != 0)
293 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100294
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000295 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100296 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000297 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100298 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000299 u_int status;
Damien Miller33804262001-02-04 23:20:18 +1100300
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000301 if ((r = sshbuf_get_u32(msg, &status)) != 0)
302 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000303 if (quiet)
304 debug("Couldn't stat remote file: %s", fx2txt(status));
305 else
306 error("Couldn't stat remote file: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000307 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100308 return(NULL);
309 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000310 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100311 SSH2_FXP_ATTRS, type);
312 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000313 if ((r = decode_attrib(msg, &a)) != 0) {
314 error("%s: couldn't decode attrib: %s", __func__, ssh_err(r));
315 sshbuf_free(msg);
316 return NULL;
317 }
318 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100319
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000320 return &a;
Damien Miller33804262001-02-04 23:20:18 +1100321}
322
Damien Millerd671e5a2008-05-19 14:53:33 +1000323static int
Damien Miller65e42f82010-09-24 22:15:11 +1000324get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
325 u_int expected_id, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000326{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000327 struct sshbuf *msg;
328 u_char type;
329 u_int id;
330 u_int64_t flag;
331 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +1000332
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000333 if ((msg = sshbuf_new()) == NULL)
334 fatal("%s: sshbuf_new failed", __func__);
335 get_msg(conn, msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000336
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000337 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
338 (r = sshbuf_get_u32(msg, &id)) != 0)
339 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000340
341 debug3("Received statvfs reply T:%u I:%u", type, id);
342 if (id != expected_id)
343 fatal("ID mismatch (%u != %u)", id, expected_id);
344 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000345 u_int status;
Damien Millerd671e5a2008-05-19 14:53:33 +1000346
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000347 if ((r = sshbuf_get_u32(msg, &status)) != 0)
348 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000349 if (quiet)
350 debug("Couldn't statvfs: %s", fx2txt(status));
351 else
352 error("Couldn't statvfs: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000353 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000354 return -1;
355 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
356 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
357 SSH2_FXP_EXTENDED_REPLY, type);
358 }
359
Damien Miller1d2c4562014-02-04 11:18:20 +1100360 memset(st, 0, sizeof(*st));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000361 if ((r = sshbuf_get_u64(msg, &st->f_bsize)) != 0 ||
362 (r = sshbuf_get_u64(msg, &st->f_frsize)) != 0 ||
363 (r = sshbuf_get_u64(msg, &st->f_blocks)) != 0 ||
364 (r = sshbuf_get_u64(msg, &st->f_bfree)) != 0 ||
365 (r = sshbuf_get_u64(msg, &st->f_bavail)) != 0 ||
366 (r = sshbuf_get_u64(msg, &st->f_files)) != 0 ||
367 (r = sshbuf_get_u64(msg, &st->f_ffree)) != 0 ||
368 (r = sshbuf_get_u64(msg, &st->f_favail)) != 0 ||
369 (r = sshbuf_get_u64(msg, &st->f_fsid)) != 0 ||
370 (r = sshbuf_get_u64(msg, &flag)) != 0 ||
371 (r = sshbuf_get_u64(msg, &st->f_namemax)) != 0)
372 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000373
374 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
375 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
376
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000377 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000378
379 return 0;
380}
381
Damien Miller3db5f532002-02-13 14:10:32 +1100382struct sftp_conn *
Damien Miller65e42f82010-09-24 22:15:11 +1000383do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
384 u_int64_t limit_kbps)
Damien Miller33804262001-02-04 23:20:18 +1100385{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000386 u_char type;
387 struct sshbuf *msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100388 struct sftp_conn *ret;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000389 int r;
Damien Miller33804262001-02-04 23:20:18 +1100390
Damien Millerfec029f2013-08-21 02:42:12 +1000391 ret = xcalloc(1, sizeof(*ret));
392 ret->msg_id = 1;
Damien Miller65e42f82010-09-24 22:15:11 +1000393 ret->fd_in = fd_in;
394 ret->fd_out = fd_out;
395 ret->transfer_buflen = transfer_buflen;
396 ret->num_requests = num_requests;
397 ret->exts = 0;
398 ret->limit_kbps = 0;
399
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000400 if ((msg = sshbuf_new()) == NULL)
401 fatal("%s: sshbuf_new failed", __func__);
402 if ((r = sshbuf_put_u8(msg, SSH2_FXP_INIT)) != 0 ||
403 (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0)
404 fatal("%s: buffer error: %s", __func__, ssh_err(r));
405 send_msg(ret, msg);
Damien Miller33804262001-02-04 23:20:18 +1100406
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000407 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +1100408
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000409 get_msg(ret, msg);
Damien Miller33804262001-02-04 23:20:18 +1100410
Kevin Stevesef4eea92001-02-05 12:42:17 +0000411 /* Expecting a VERSION reply */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000412 if ((r = sshbuf_get_u8(msg, &type)) != 0)
413 fatal("%s: buffer error: %s", __func__, ssh_err(r));
414 if (type != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000415 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100416 type);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000417 sshbuf_free(msg);
jsg@openbsd.org64a89ec2015-05-23 14:28:37 +0000418 free(ret);
Damien Miller3db5f532002-02-13 14:10:32 +1100419 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100420 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000421 if ((r = sshbuf_get_u32(msg, &ret->version)) != 0)
422 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100423
Damien Miller65e42f82010-09-24 22:15:11 +1000424 debug2("Remote version: %u", ret->version);
Damien Miller33804262001-02-04 23:20:18 +1100425
426 /* Check for extensions */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000427 while (sshbuf_len(msg) > 0) {
428 char *name;
429 u_char *value;
430 size_t vlen;
Darren Tuckera64ab332008-06-13 07:01:29 +1000431 int known = 0;
Damien Miller33804262001-02-04 23:20:18 +1100432
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000433 if ((r = sshbuf_get_cstring(msg, &name, NULL)) != 0 ||
434 (r = sshbuf_get_string(msg, &value, &vlen)) != 0)
435 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000436 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000437 strcmp((char *)value, "1") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000438 ret->exts |= SFTP_EXT_POSIX_RENAME;
Darren Tuckera64ab332008-06-13 07:01:29 +1000439 known = 1;
440 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000441 strcmp((char *)value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000442 ret->exts |= SFTP_EXT_STATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000443 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100444 } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000445 strcmp((char *)value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000446 ret->exts |= SFTP_EXT_FSTATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000447 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100448 } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000449 strcmp((char *)value, "1") == 0) {
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100450 ret->exts |= SFTP_EXT_HARDLINK;
451 known = 1;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000452 } else if (strcmp(name, "fsync@openssh.com") == 0 &&
453 strcmp((char *)value, "1") == 0) {
454 ret->exts |= SFTP_EXT_FSYNC;
455 known = 1;
Darren Tuckera64ab332008-06-13 07:01:29 +1000456 }
457 if (known) {
458 debug2("Server supports extension \"%s\" revision %s",
459 name, value);
460 } else {
461 debug2("Unrecognised server extension \"%s\"", name);
462 }
Darren Tuckera627d422013-06-02 07:31:17 +1000463 free(name);
464 free(value);
Damien Miller33804262001-02-04 23:20:18 +1100465 }
466
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000467 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +1100468
Damien Miller3db5f532002-02-13 14:10:32 +1100469 /* Some filexfer v.0 servers don't support large packets */
Damien Miller65e42f82010-09-24 22:15:11 +1000470 if (ret->version == 0)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000471 ret->transfer_buflen = MINIMUM(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100472
Damien Miller65e42f82010-09-24 22:15:11 +1000473 ret->limit_kbps = limit_kbps;
474 if (ret->limit_kbps > 0) {
475 bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
476 ret->transfer_buflen);
477 bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
478 ret->transfer_buflen);
479 }
480
481 return ret;
Damien Miller3db5f532002-02-13 14:10:32 +1100482}
483
484u_int
485sftp_proto_version(struct sftp_conn *conn)
486{
Damien Miller65e42f82010-09-24 22:15:11 +1000487 return conn->version;
Damien Miller33804262001-02-04 23:20:18 +1100488}
489
490int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000491do_close(struct sftp_conn *conn, const u_char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100492{
493 u_int id, status;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000494 struct sshbuf *msg;
495 int r;
Damien Miller33804262001-02-04 23:20:18 +1100496
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000497 if ((msg = sshbuf_new()) == NULL)
498 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100499
Damien Miller3db5f532002-02-13 14:10:32 +1100500 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000501 if ((r = sshbuf_put_u8(msg, SSH2_FXP_CLOSE)) != 0 ||
502 (r = sshbuf_put_u32(msg, id)) != 0 ||
503 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
504 fatal("%s: buffer error: %s", __func__, ssh_err(r));
505 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000506 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100507
Damien Miller65e42f82010-09-24 22:15:11 +1000508 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100509 if (status != SSH2_FX_OK)
510 error("Couldn't close file: %s", fx2txt(status));
511
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000512 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100513
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000514 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100515}
516
Damien Miller4870afd2001-03-14 10:27:09 +1100517
Ben Lindstrombba81212001-06-25 05:01:22 +0000518static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000519do_lsreaddir(struct sftp_conn *conn, const char *path, int print_flag,
Damien Miller4870afd2001-03-14 10:27:09 +1100520 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100521{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000522 struct sshbuf *msg;
523 u_int count, id, i, expected_id, ents = 0;
524 size_t handle_len;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000525 u_char type, *handle;
Damien Miller39392072013-12-07 10:31:08 +1100526 int status = SSH2_FX_FAILURE;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000527 int r;
Damien Miller39392072013-12-07 10:31:08 +1100528
529 if (dir)
530 *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +1100531
Damien Miller3db5f532002-02-13 14:10:32 +1100532 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100533
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000534 if ((msg = sshbuf_new()) == NULL)
535 fatal("%s: sshbuf_new failed", __func__);
536 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPENDIR)) != 0 ||
537 (r = sshbuf_put_u32(msg, id)) != 0 ||
538 (r = sshbuf_put_cstring(msg, path)) != 0)
539 fatal("%s: buffer error: %s", __func__, ssh_err(r));
540 send_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100541
Damien Miller65e42f82010-09-24 22:15:11 +1000542 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +1100543 "remote readdir(\"%s\")", path);
Damien Miller57c38ac2011-09-22 21:42:45 +1000544 if (handle == NULL) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000545 sshbuf_free(msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000546 return -1;
Damien Miller57c38ac2011-09-22 21:42:45 +1000547 }
Damien Miller33804262001-02-04 23:20:18 +1100548
Damien Miller4870afd2001-03-14 10:27:09 +1100549 if (dir) {
550 ents = 0;
Damien Miller6c81fee2013-11-08 12:19:55 +1100551 *dir = xcalloc(1, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100552 (*dir)[0] = NULL;
553 }
Damien Miller4870afd2001-03-14 10:27:09 +1100554
Darren Tuckercdf547a2004-05-24 10:12:19 +1000555 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100556 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100557
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000558 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100559
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000560 sshbuf_reset(msg);
561 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READDIR)) != 0 ||
562 (r = sshbuf_put_u32(msg, id)) != 0 ||
563 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
564 fatal("%s: buffer error: %s", __func__, ssh_err(r));
565 send_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100566
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000567 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +1100568
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000569 get_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100570
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000571 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
572 (r = sshbuf_get_u32(msg, &id)) != 0)
573 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100574
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000575 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100576
577 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000578 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100579
580 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000581 u_int rstatus;
582
583 if ((r = sshbuf_get_u32(msg, &rstatus)) != 0)
584 fatal("%s: buffer error: %s",
585 __func__, ssh_err(r));
586 debug3("Received SSH2_FXP_STATUS %d", rstatus);
587 if (rstatus == SSH2_FX_EOF)
Damien Miller33804262001-02-04 23:20:18 +1100588 break;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000589 error("Couldn't read directory: %s", fx2txt(rstatus));
Damien Miller39392072013-12-07 10:31:08 +1100590 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100591 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000592 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100593 SSH2_FXP_NAME, type);
594
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000595 if ((r = sshbuf_get_u32(msg, &count)) != 0)
596 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgc7995f22017-01-03 05:46:51 +0000597 if (count > SSHBUF_SIZE_MAX)
598 fatal("%s: nonsensical number of entries", __func__);
Damien Millerd7686fd2001-02-10 00:40:03 +1100599 if (count == 0)
600 break;
601 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100602 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100603 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000604 Attrib a;
Damien Miller33804262001-02-04 23:20:18 +1100605
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000606 if ((r = sshbuf_get_cstring(msg, &filename,
607 NULL)) != 0 ||
608 (r = sshbuf_get_cstring(msg, &longname,
609 NULL)) != 0)
610 fatal("%s: buffer error: %s",
611 __func__, ssh_err(r));
612 if ((r = decode_attrib(msg, &a)) != 0) {
613 error("%s: couldn't decode attrib: %s",
614 __func__, ssh_err(r));
615 free(filename);
616 free(longname);
617 sshbuf_free(msg);
618 return -1;
619 }
Damien Miller33804262001-02-04 23:20:18 +1100620
Damien Millerbda5c842013-10-15 12:05:58 +1100621 if (print_flag)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000622 mprintf("%s\n", longname);
Damien Miller4870afd2001-03-14 10:27:09 +1100623
Darren Tucker1b0dd172009-10-07 08:37:48 +1100624 /*
625 * Directory entries should never contain '/'
626 * These can be used to attack recursive ops
627 * (e.g. send '../../../../etc/passwd')
628 */
Damien Miller89f04852017-03-20 11:53:34 +1100629 if (strpbrk(filename, SFTP_DIRECTORY_CHARS) != NULL) {
Darren Tucker1b0dd172009-10-07 08:37:48 +1100630 error("Server sent suspect path \"%s\" "
631 "during readdir of \"%s\"", filename, path);
Damien Miller39392072013-12-07 10:31:08 +1100632 } else if (dir) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000633 *dir = xreallocarray(*dir, ents + 2, sizeof(**dir));
Damien Miller6c81fee2013-11-08 12:19:55 +1100634 (*dir)[ents] = xcalloc(1, sizeof(***dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100635 (*dir)[ents]->filename = xstrdup(filename);
636 (*dir)[ents]->longname = xstrdup(longname);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000637 memcpy(&(*dir)[ents]->a, &a, sizeof(a));
Damien Miller4870afd2001-03-14 10:27:09 +1100638 (*dir)[++ents] = NULL;
639 }
Darren Tuckera627d422013-06-02 07:31:17 +1000640 free(filename);
641 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100642 }
643 }
Damien Miller39392072013-12-07 10:31:08 +1100644 status = 0;
Damien Miller33804262001-02-04 23:20:18 +1100645
Damien Miller39392072013-12-07 10:31:08 +1100646 out:
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000647 sshbuf_free(msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100648 do_close(conn, handle, handle_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000649 free(handle);
Damien Miller33804262001-02-04 23:20:18 +1100650
Damien Miller39392072013-12-07 10:31:08 +1100651 if (status != 0 && dir != NULL) {
652 /* Don't return results on error */
653 free_sftp_dirents(*dir);
654 *dir = NULL;
655 } else if (interrupted && dir != NULL && *dir != NULL) {
656 /* Don't return partial matches on interrupt */
Darren Tuckercdf547a2004-05-24 10:12:19 +1000657 free_sftp_dirents(*dir);
Damien Miller6c81fee2013-11-08 12:19:55 +1100658 *dir = xcalloc(1, sizeof(**dir));
Darren Tuckercdf547a2004-05-24 10:12:19 +1000659 **dir = NULL;
660 }
661
Damien Miller39392072013-12-07 10:31:08 +1100662 return status;
Damien Miller33804262001-02-04 23:20:18 +1100663}
664
665int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000666do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100667{
Damien Miller3db5f532002-02-13 14:10:32 +1100668 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100669}
670
671void free_sftp_dirents(SFTP_DIRENT **s)
672{
673 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100674
Damien Miller39392072013-12-07 10:31:08 +1100675 if (s == NULL)
676 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100677 for (i = 0; s[i]; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000678 free(s[i]->filename);
679 free(s[i]->longname);
680 free(s[i]);
Damien Miller4870afd2001-03-14 10:27:09 +1100681 }
Darren Tuckera627d422013-06-02 07:31:17 +1000682 free(s);
Damien Miller4870afd2001-03-14 10:27:09 +1100683}
684
685int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000686do_rm(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100687{
688 u_int status, id;
689
690 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
691
Damien Miller3db5f532002-02-13 14:10:32 +1100692 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000693 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
694 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100695 if (status != SSH2_FX_OK)
696 error("Couldn't delete file: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000697 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100698}
699
700int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000701do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100702{
703 u_int status, id;
704
Damien Miller3db5f532002-02-13 14:10:32 +1100705 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000706 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100707 strlen(path), a);
708
Damien Miller65e42f82010-09-24 22:15:11 +1000709 status = get_status(conn, id);
Damien Millerbda5c842013-10-15 12:05:58 +1100710 if (status != SSH2_FX_OK && print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100711 error("Couldn't create directory: %s", fx2txt(status));
712
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000713 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100714}
715
716int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000717do_rmdir(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100718{
719 u_int status, id;
720
Damien Miller3db5f532002-02-13 14:10:32 +1100721 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000722 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100723 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100724
Damien Miller65e42f82010-09-24 22:15:11 +1000725 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100726 if (status != SSH2_FX_OK)
727 error("Couldn't remove directory: %s", fx2txt(status));
728
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000729 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100730}
731
732Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000733do_stat(struct sftp_conn *conn, const char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100734{
735 u_int id;
736
Damien Miller3db5f532002-02-13 14:10:32 +1100737 id = conn->msg_id++;
738
Damien Miller65e42f82010-09-24 22:15:11 +1000739 send_string_request(conn, id,
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000740 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100741 path, strlen(path));
742
Damien Miller65e42f82010-09-24 22:15:11 +1000743 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100744}
745
746Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000747do_lstat(struct sftp_conn *conn, const char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100748{
749 u_int id;
750
Damien Miller3db5f532002-02-13 14:10:32 +1100751 if (conn->version == 0) {
752 if (quiet)
753 debug("Server version does not support lstat operation");
754 else
Damien Miller996acd22003-04-09 20:59:48 +1000755 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000756 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100757 }
758
759 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000760 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100761 strlen(path));
762
Damien Miller65e42f82010-09-24 22:15:11 +1000763 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100764}
765
Damien Millercfe23d32008-02-10 22:20:44 +1100766#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100767Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000768do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
769 int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100770{
771 u_int id;
772
Damien Miller3db5f532002-02-13 14:10:32 +1100773 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000774 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
Damien Miller3db5f532002-02-13 14:10:32 +1100775 handle_len);
776
Damien Miller65e42f82010-09-24 22:15:11 +1000777 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100778}
Damien Millercfe23d32008-02-10 22:20:44 +1100779#endif
Damien Miller33804262001-02-04 23:20:18 +1100780
781int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000782do_setstat(struct sftp_conn *conn, const char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100783{
784 u_int status, id;
785
Damien Miller3db5f532002-02-13 14:10:32 +1100786 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000787 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100788 strlen(path), a);
789
Damien Miller65e42f82010-09-24 22:15:11 +1000790 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100791 if (status != SSH2_FX_OK)
792 error("Couldn't setstat on \"%s\": %s", path,
793 fx2txt(status));
794
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000795 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100796}
797
798int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000799do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100800 Attrib *a)
801{
802 u_int status, id;
803
Damien Miller3db5f532002-02-13 14:10:32 +1100804 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000805 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100806 handle_len, a);
807
Damien Miller65e42f82010-09-24 22:15:11 +1000808 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100809 if (status != SSH2_FX_OK)
810 error("Couldn't fsetstat: %s", fx2txt(status));
811
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000812 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100813}
814
815char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000816do_realpath(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100817{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000818 struct sshbuf *msg;
819 u_int expected_id, count, id;
Damien Miller33804262001-02-04 23:20:18 +1100820 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000821 Attrib a;
822 u_char type;
823 int r;
Damien Miller33804262001-02-04 23:20:18 +1100824
Damien Miller3db5f532002-02-13 14:10:32 +1100825 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000826 send_string_request(conn, id, SSH2_FXP_REALPATH, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100827 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100828
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000829 if ((msg = sshbuf_new()) == NULL)
830 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100831
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000832 get_msg(conn, msg);
833 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
834 (r = sshbuf_get_u32(msg, &id)) != 0)
835 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100836
837 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000838 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100839
840 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000841 u_int status;
Damien Miller33804262001-02-04 23:20:18 +1100842
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000843 if ((r = sshbuf_get_u32(msg, &status)) != 0)
844 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf29238e2013-10-17 11:48:52 +1100845 error("Couldn't canonicalize: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000846 sshbuf_free(msg);
Damien Miller49566312010-06-26 09:38:23 +1000847 return NULL;
Damien Miller33804262001-02-04 23:20:18 +1100848 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000849 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100850 SSH2_FXP_NAME, type);
851
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000852 if ((r = sshbuf_get_u32(msg, &count)) != 0)
853 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100854 if (count != 1)
855 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
856
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000857 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
858 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
859 (r = decode_attrib(msg, &a)) != 0)
860 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100861
Darren Tucker4908d442012-07-02 22:15:38 +1000862 debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000863 (unsigned long)a.size);
Damien Miller33804262001-02-04 23:20:18 +1100864
Darren Tuckera627d422013-06-02 07:31:17 +1000865 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100866
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000867 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100868
869 return(filename);
870}
871
872int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000873do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath,
Damien Millerc7dba122013-08-21 02:41:15 +1000874 int force_legacy)
Damien Miller33804262001-02-04 23:20:18 +1100875{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000876 struct sshbuf *msg;
Damien Miller33804262001-02-04 23:20:18 +1100877 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000878 int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
Damien Miller33804262001-02-04 23:20:18 +1100879
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000880 if ((msg = sshbuf_new()) == NULL)
881 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100882
883 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100884 id = conn->msg_id++;
Damien Millerc7dba122013-08-21 02:41:15 +1000885 if (use_ext) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000886 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
887 (r = sshbuf_put_u32(msg, id)) != 0 ||
888 (r = sshbuf_put_cstring(msg,
889 "posix-rename@openssh.com")) != 0)
890 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7a3e1d02008-03-27 10:59:57 +1100891 } else {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000892 if ((r = sshbuf_put_u8(msg, SSH2_FXP_RENAME)) != 0 ||
893 (r = sshbuf_put_u32(msg, id)) != 0)
894 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7a3e1d02008-03-27 10:59:57 +1100895 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000896 if ((r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
897 (r = sshbuf_put_cstring(msg, newpath)) != 0)
898 fatal("%s: buffer error: %s", __func__, ssh_err(r));
899 send_msg(conn, msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100900 debug3("Sent message %s \"%s\" -> \"%s\"",
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000901 use_ext ? "posix-rename@openssh.com" :
902 "SSH2_FXP_RENAME", oldpath, newpath);
903 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100904
Damien Miller65e42f82010-09-24 22:15:11 +1000905 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100906 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100907 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
908 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100909
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000910 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100911}
912
913int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000914do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100915{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000916 struct sshbuf *msg;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100917 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000918 int r;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100919
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100920 if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
921 error("Server does not support hardlink@openssh.com extension");
922 return -1;
923 }
924
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000925 if ((msg = sshbuf_new()) == NULL)
926 fatal("%s: sshbuf_new failed", __func__);
Damien Miller3decdba2011-09-22 21:41:05 +1000927
928 /* Send link request */
929 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000930 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
931 (r = sshbuf_put_u32(msg, id)) != 0 ||
932 (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
933 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
934 (r = sshbuf_put_cstring(msg, newpath)) != 0)
935 fatal("%s: buffer error: %s", __func__, ssh_err(r));
936 send_msg(conn, msg);
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100937 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
938 oldpath, newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000939 sshbuf_free(msg);
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100940
941 status = get_status(conn, id);
942 if (status != SSH2_FX_OK)
943 error("Couldn't link file \"%s\" to \"%s\": %s", oldpath,
944 newpath, fx2txt(status));
945
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000946 return status == SSH2_FX_OK ? 0 : -1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100947}
948
949int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000950do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100951{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000952 struct sshbuf *msg;
Damien Miller058316f2001-03-08 10:08:49 +1100953 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000954 int r;
Damien Miller058316f2001-03-08 10:08:49 +1100955
Damien Miller3db5f532002-02-13 14:10:32 +1100956 if (conn->version < 3) {
957 error("This server does not support the symlink operation");
958 return(SSH2_FX_OP_UNSUPPORTED);
959 }
960
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000961 if ((msg = sshbuf_new()) == NULL)
962 fatal("%s: sshbuf_new failed", __func__);
Damien Miller058316f2001-03-08 10:08:49 +1100963
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000964 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100965 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000966 if ((r = sshbuf_put_u8(msg, SSH2_FXP_SYMLINK)) != 0 ||
967 (r = sshbuf_put_u32(msg, id)) != 0 ||
968 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
969 (r = sshbuf_put_cstring(msg, newpath)) != 0)
970 fatal("%s: buffer error: %s", __func__, ssh_err(r));
971 send_msg(conn, msg);
Damien Miller058316f2001-03-08 10:08:49 +1100972 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
973 newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000974 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +1100975
Damien Miller65e42f82010-09-24 22:15:11 +1000976 status = get_status(conn, id);
Damien Miller058316f2001-03-08 10:08:49 +1100977 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000978 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100979 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100980
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000981 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller058316f2001-03-08 10:08:49 +1100982}
983
Damien Millerf29238e2013-10-17 11:48:52 +1100984int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000985do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len)
Damien Millerf29238e2013-10-17 11:48:52 +1100986{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000987 struct sshbuf *msg;
Damien Millerf29238e2013-10-17 11:48:52 +1100988 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000989 int r;
Damien Millerf29238e2013-10-17 11:48:52 +1100990
991 /* Silently return if the extension is not supported */
992 if ((conn->exts & SFTP_EXT_FSYNC) == 0)
993 return -1;
994
Damien Millerf29238e2013-10-17 11:48:52 +1100995 /* Send fsync request */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000996 if ((msg = sshbuf_new()) == NULL)
997 fatal("%s: sshbuf_new failed", __func__);
Damien Millerf29238e2013-10-17 11:48:52 +1100998 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000999 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1000 (r = sshbuf_put_u32(msg, id)) != 0 ||
1001 (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
1002 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1003 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1004 send_msg(conn, msg);
Damien Millerf29238e2013-10-17 11:48:52 +11001005 debug3("Sent message fsync@openssh.com I:%u", id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001006 sshbuf_free(msg);
Damien Millerf29238e2013-10-17 11:48:52 +11001007
1008 status = get_status(conn, id);
1009 if (status != SSH2_FX_OK)
1010 error("Couldn't sync file: %s", fx2txt(status));
1011
1012 return status;
1013}
1014
Damien Millercfe23d32008-02-10 22:20:44 +11001015#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +11001016char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001017do_readlink(struct sftp_conn *conn, const char *path)
Damien Miller058316f2001-03-08 10:08:49 +11001018{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001019 struct sshbuf *msg;
1020 u_int expected_id, count, id;
Damien Miller058316f2001-03-08 10:08:49 +11001021 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001022 Attrib a;
1023 u_char type;
1024 int r;
Damien Miller058316f2001-03-08 10:08:49 +11001025
Damien Miller3db5f532002-02-13 14:10:32 +11001026 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +10001027 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +11001028
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001029 if ((msg = sshbuf_new()) == NULL)
1030 fatal("%s: sshbuf_new failed", __func__);
Damien Miller058316f2001-03-08 10:08:49 +11001031
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001032 get_msg(conn, msg);
1033 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1034 (r = sshbuf_get_u32(msg, &id)) != 0)
1035 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001036
1037 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001038 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +11001039
1040 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001041 u_int status;
Damien Miller058316f2001-03-08 10:08:49 +11001042
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001043 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1044 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001045 error("Couldn't readlink: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001046 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +11001047 return(NULL);
1048 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001049 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +11001050 SSH2_FXP_NAME, type);
1051
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001052 if ((r = sshbuf_get_u32(msg, &count)) != 0)
1053 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001054 if (count != 1)
1055 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
1056
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001057 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
1058 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
1059 (r = decode_attrib(msg, &a)) != 0)
1060 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001061
1062 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
1063
Darren Tuckera627d422013-06-02 07:31:17 +10001064 free(longname);
Damien Miller058316f2001-03-08 10:08:49 +11001065
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001066 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +11001067
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001068 return filename;
Damien Miller058316f2001-03-08 10:08:49 +11001069}
Damien Millercfe23d32008-02-10 22:20:44 +11001070#endif
Damien Miller058316f2001-03-08 10:08:49 +11001071
Damien Millerd671e5a2008-05-19 14:53:33 +10001072int
Darren Tucker7b598892008-06-09 22:49:36 +10001073do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
Damien Millerd671e5a2008-05-19 14:53:33 +10001074 int quiet)
1075{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001076 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +10001077 u_int id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001078 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +10001079
1080 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
1081 error("Server does not support statvfs@openssh.com extension");
1082 return -1;
1083 }
1084
1085 id = conn->msg_id++;
1086
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001087 if ((msg = sshbuf_new()) == NULL)
1088 fatal("%s: sshbuf_new failed", __func__);
1089 sshbuf_reset(msg);
1090 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1091 (r = sshbuf_put_u32(msg, id)) != 0 ||
1092 (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
1093 (r = sshbuf_put_cstring(msg, path)) != 0)
1094 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1095 send_msg(conn, msg);
1096 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +10001097
Damien Miller65e42f82010-09-24 22:15:11 +10001098 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +10001099}
1100
1101#ifdef notyet
1102int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001103do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
Darren Tucker7b598892008-06-09 22:49:36 +10001104 struct sftp_statvfs *st, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +10001105{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001106 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +10001107 u_int id;
1108
1109 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
1110 error("Server does not support fstatvfs@openssh.com extension");
1111 return -1;
1112 }
1113
1114 id = conn->msg_id++;
1115
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001116 if ((msg = sshbuf_new()) == NULL)
1117 fatal("%s: sshbuf_new failed", __func__);
1118 sshbuf_reset(msg);
1119 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1120 (r = sshbuf_put_u32(msg, id)) != 0 ||
1121 (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
1122 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1123 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1124 send_msg(conn, msg);
1125 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +10001126
Damien Miller65e42f82010-09-24 22:15:11 +10001127 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +10001128}
1129#endif
1130
Damien Miller16a13332002-02-13 14:03:56 +11001131static void
Damien Miller65e42f82010-09-24 22:15:11 +10001132send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001133 u_int len, const u_char *handle, u_int handle_len)
Damien Miller16a13332002-02-13 14:03:56 +11001134{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001135 struct sshbuf *msg;
1136 int r;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001137
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001138 if ((msg = sshbuf_new()) == NULL)
1139 fatal("%s: sshbuf_new failed", __func__);
1140 sshbuf_reset(msg);
1141 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READ)) != 0 ||
1142 (r = sshbuf_put_u32(msg, id)) != 0 ||
1143 (r = sshbuf_put_string(msg, handle, handle_len)) != 0 ||
1144 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1145 (r = sshbuf_put_u32(msg, len)) != 0)
1146 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1147 send_msg(conn, msg);
1148 sshbuf_free(msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001149}
Damien Miller16a13332002-02-13 14:03:56 +11001150
Damien Miller058316f2001-03-08 10:08:49 +11001151int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001152do_download(struct sftp_conn *conn, const char *remote_path,
1153 const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
1154 int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001155{
Darren Tucker1b0dd172009-10-07 08:37:48 +11001156 Attrib junk;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001157 struct sshbuf *msg;
1158 u_char *handle;
1159 int local_fd = -1, write_error;
1160 int read_error, write_errno, reordered = 0, r;
Damien Miller0d032412013-07-25 11:56:52 +10001161 u_int64_t offset = 0, size, highwater;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001162 u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
Damien Miller62d57f62003-01-10 21:43:24 +11001163 off_t progress_counter;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001164 size_t handle_len;
Damien Miller0d032412013-07-25 11:56:52 +10001165 struct stat st;
Damien Miller16a13332002-02-13 14:03:56 +11001166 struct request {
1167 u_int id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001168 size_t len;
Damien Miller16a13332002-02-13 14:03:56 +11001169 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001170 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +11001171 };
1172 TAILQ_HEAD(reqhead, request) requests;
1173 struct request *req;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001174 u_char type;
Damien Miller16a13332002-02-13 14:03:56 +11001175
1176 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +11001177
Darren Tucker1b0dd172009-10-07 08:37:48 +11001178 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1179 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001180
Damien Miller9e720282008-06-29 22:46:35 +10001181 /* Do not preserve set[ug]id here, as we do not preserve ownership */
Damien Miller33804262001-02-04 23:20:18 +11001182 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +11001183 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +11001184 else
1185 mode = 0666;
1186
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001187 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +11001188 (!S_ISREG(a->perm))) {
1189 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001190 return(-1);
1191 }
1192
Damien Miller16a13332002-02-13 14:03:56 +11001193 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1194 size = a->size;
1195 else
1196 size = 0;
1197
Damien Miller3db5f532002-02-13 14:10:32 +11001198 buflen = conn->transfer_buflen;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001199 if ((msg = sshbuf_new()) == NULL)
1200 fatal("%s: sshbuf_new failed", __func__);
1201
1202 attrib_clear(&junk); /* Send empty attributes */
Damien Miller33804262001-02-04 23:20:18 +11001203
1204 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001205 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001206 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1207 (r = sshbuf_put_u32(msg, id)) != 0 ||
1208 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1209 (r = sshbuf_put_u32(msg, SSH2_FXF_READ)) != 0 ||
1210 (r = encode_attrib(msg, &junk)) != 0)
1211 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1212 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001213 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001214
Damien Miller65e42f82010-09-24 22:15:11 +10001215 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001216 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001217 if (handle == NULL) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001218 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +11001219 return(-1);
1220 }
1221
Damien Millerbda5c842013-10-15 12:05:58 +11001222 local_fd = open(local_path,
1223 O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
Damien Miller3db5f532002-02-13 14:10:32 +11001224 if (local_fd == -1) {
1225 error("Couldn't open local file \"%s\" for writing: %s",
1226 local_path, strerror(errno));
Damien Miller0d032412013-07-25 11:56:52 +10001227 goto fail;
1228 }
1229 offset = highwater = 0;
Damien Millerbda5c842013-10-15 12:05:58 +11001230 if (resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001231 if (fstat(local_fd, &st) == -1) {
1232 error("Unable to stat local file \"%s\": %s",
1233 local_path, strerror(errno));
1234 goto fail;
1235 }
Damien Millerc3d483f2014-01-17 11:20:26 +11001236 if (st.st_size < 0) {
1237 error("\"%s\" has negative size", local_path);
1238 goto fail;
1239 }
1240 if ((u_int64_t)st.st_size > size) {
Damien Miller0d032412013-07-25 11:56:52 +10001241 error("Unable to resume download of \"%s\": "
1242 "local file is larger than remote", local_path);
1243 fail:
1244 do_close(conn, handle, handle_len);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001245 sshbuf_free(msg);
Damien Miller0d032412013-07-25 11:56:52 +10001246 free(handle);
Damien Miller9275df32013-12-05 10:26:32 +11001247 if (local_fd != -1)
1248 close(local_fd);
Damien Miller0d032412013-07-25 11:56:52 +10001249 return -1;
1250 }
1251 offset = highwater = st.st_size;
Damien Miller3db5f532002-02-13 14:10:32 +11001252 }
1253
Damien Miller33804262001-02-04 23:20:18 +11001254 /* Read from remote and write to local */
Damien Miller0d032412013-07-25 11:56:52 +10001255 write_error = read_error = write_errno = num_req = 0;
Damien Miller16a13332002-02-13 14:03:56 +11001256 max_req = 1;
Damien Miller0d032412013-07-25 11:56:52 +10001257 progress_counter = offset;
Damien Miller62d57f62003-01-10 21:43:24 +11001258
Damien Miller9ba30692004-03-08 23:12:02 +11001259 if (showprogress && size != 0)
1260 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001261
Damien Miller16a13332002-02-13 14:03:56 +11001262 while (num_req > 0 || max_req > 0) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001263 u_char *data;
1264 size_t len;
Damien Miller33804262001-02-04 23:20:18 +11001265
Darren Tuckercdf547a2004-05-24 10:12:19 +10001266 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001267 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001268 * allow outstanding requests to drain gracefully
1269 */
1270 if (interrupted) {
1271 if (num_req == 0) /* If we haven't started yet... */
1272 break;
1273 max_req = 0;
1274 }
1275
Damien Miller16a13332002-02-13 14:03:56 +11001276 /* Send some more requests */
1277 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001278 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +00001279 (unsigned long long)offset,
1280 (unsigned long long)offset + buflen - 1,
1281 num_req, max_req);
Damien Miller6c81fee2013-11-08 12:19:55 +11001282 req = xcalloc(1, sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +11001283 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001284 req->len = buflen;
1285 req->offset = offset;
1286 offset += buflen;
1287 num_req++;
1288 TAILQ_INSERT_TAIL(&requests, req, tq);
Damien Miller65e42f82010-09-24 22:15:11 +10001289 send_read_request(conn, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001290 req->len, handle, handle_len);
1291 }
Damien Miller33804262001-02-04 23:20:18 +11001292
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001293 sshbuf_reset(msg);
1294 get_msg(conn, msg);
1295 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1296 (r = sshbuf_get_u32(msg, &id)) != 0)
1297 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001298 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001299
Damien Miller16a13332002-02-13 14:03:56 +11001300 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001301 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001302 req != NULL && req->id != id;
1303 req = TAILQ_NEXT(req, tq))
1304 ;
1305 if (req == NULL)
1306 fatal("Unexpected reply %u", id);
1307
1308 switch (type) {
1309 case SSH2_FXP_STATUS:
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001310 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1311 fatal("%s: buffer error: %s",
1312 __func__, ssh_err(r));
Damien Miller16a13332002-02-13 14:03:56 +11001313 if (status != SSH2_FX_EOF)
1314 read_error = 1;
1315 max_req = 0;
1316 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001317 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001318 num_req--;
1319 break;
1320 case SSH2_FXP_DATA:
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001321 if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1322 fatal("%s: buffer error: %s",
1323 __func__, ssh_err(r));
Ben Lindstromeb505452002-03-22 01:03:15 +00001324 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001325 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001326 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001327 if (len > req->len)
1328 fatal("Received more data than asked for "
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001329 "%zu > %zu", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001330 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001331 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001332 !write_error) {
1333 write_errno = errno;
1334 write_error = 1;
1335 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001336 }
Damien Miller0d032412013-07-25 11:56:52 +10001337 else if (!reordered && req->offset <= highwater)
1338 highwater = req->offset + len;
1339 else if (!reordered && req->offset > highwater)
1340 reordered = 1;
Damien Miller62d57f62003-01-10 21:43:24 +11001341 progress_counter += len;
Darren Tuckera627d422013-06-02 07:31:17 +10001342 free(data);
Damien Miller16a13332002-02-13 14:03:56 +11001343
1344 if (len == req->len) {
1345 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001346 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001347 num_req--;
1348 } else {
1349 /* Resend the request for the missing data */
1350 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001351 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001352 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001353 (unsigned long long)req->offset +
1354 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001355 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001356 req->len -= len;
1357 req->offset += len;
Damien Miller65e42f82010-09-24 22:15:11 +10001358 send_read_request(conn, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001359 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001360 /* Reduce the request size */
1361 if (len < buflen)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001362 buflen = MAXIMUM(MIN_READ_SIZE, len);
Damien Miller16a13332002-02-13 14:03:56 +11001363 }
1364 if (max_req > 0) { /* max_req = 0 iff EOF received */
1365 if (size > 0 && offset > size) {
1366 /* Only one request at a time
1367 * after the expected EOF */
1368 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001369 (unsigned long long)offset,
1370 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001371 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001372 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001373 ++max_req;
1374 }
1375 }
1376 break;
1377 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001378 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001379 SSH2_FXP_DATA, type);
1380 }
Damien Miller33804262001-02-04 23:20:18 +11001381 }
Damien Miller33804262001-02-04 23:20:18 +11001382
Damien Miller62d57f62003-01-10 21:43:24 +11001383 if (showprogress && size)
1384 stop_progress_meter();
1385
Damien Miller16a13332002-02-13 14:03:56 +11001386 /* Sanity check */
1387 if (TAILQ_FIRST(&requests) != NULL)
1388 fatal("Transfer complete, but requests still in queue");
Damien Miller0d032412013-07-25 11:56:52 +10001389 /* Truncate at highest contiguous point to avoid holes on interrupt */
1390 if (read_error || write_error || interrupted) {
Damien Millerbda5c842013-10-15 12:05:58 +11001391 if (reordered && resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001392 error("Unable to resume download of \"%s\": "
1393 "server reordered requests", local_path);
1394 }
1395 debug("truncating at %llu", (unsigned long long)highwater);
djm@openbsd.org9cc68422015-05-28 04:50:53 +00001396 if (ftruncate(local_fd, highwater) == -1)
1397 error("ftruncate \"%s\": %s", local_path,
1398 strerror(errno));
Damien Miller0d032412013-07-25 11:56:52 +10001399 }
Damien Miller16a13332002-02-13 14:03:56 +11001400 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001401 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001402 remote_path, fx2txt(status));
Damien Millerfec029f2013-08-21 02:42:12 +10001403 status = -1;
Damien Miller3db5f532002-02-13 14:10:32 +11001404 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001405 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001406 error("Couldn't write to \"%s\": %s", local_path,
1407 strerror(write_errno));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001408 status = SSH2_FX_FAILURE;
Damien Miller3db5f532002-02-13 14:10:32 +11001409 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001410 } else {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001411 if (do_close(conn, handle, handle_len) != 0 || interrupted)
1412 status = SSH2_FX_FAILURE;
1413 else
1414 status = SSH2_FX_OK;
Damien Miller16a13332002-02-13 14:03:56 +11001415 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001416#ifdef HAVE_FCHMOD
Damien Millerbda5c842013-10-15 12:05:58 +11001417 if (preserve_flag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001418#else
Damien Millerbda5c842013-10-15 12:05:58 +11001419 if (preserve_flag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001420#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001421 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001422 strerror(errno));
Damien Millerbda5c842013-10-15 12:05:58 +11001423 if (preserve_flag &&
1424 (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
Damien Miller16a13332002-02-13 14:03:56 +11001425 struct timeval tv[2];
1426 tv[0].tv_sec = a->atime;
1427 tv[1].tv_sec = a->mtime;
1428 tv[0].tv_usec = tv[1].tv_usec = 0;
1429 if (utimes(local_path, tv) == -1)
1430 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001431 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001432 }
Damien Millerf29238e2013-10-17 11:48:52 +11001433 if (fsync_flag) {
1434 debug("syncing \"%s\"", local_path);
1435 if (fsync(local_fd) == -1)
1436 error("Couldn't sync file \"%s\": %s",
1437 local_path, strerror(errno));
1438 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001439 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001440 close(local_fd);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001441 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001442 free(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001443
1444 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001445}
1446
Darren Tucker1b0dd172009-10-07 08:37:48 +11001447static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001448download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1449 int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
1450 int resume_flag, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001451{
1452 int i, ret = 0;
1453 SFTP_DIRENT **dir_entries;
1454 char *filename, *new_src, *new_dst;
1455 mode_t mode = 0777;
1456
1457 if (depth >= MAX_DIR_DEPTH) {
1458 error("Maximum directory depth exceeded: %d levels", depth);
1459 return -1;
1460 }
1461
1462 if (dirattrib == NULL &&
1463 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1464 error("Unable to stat remote directory \"%s\"", src);
1465 return -1;
1466 }
1467 if (!S_ISDIR(dirattrib->perm)) {
1468 error("\"%s\" is not a directory", src);
1469 return -1;
1470 }
Damien Millerbda5c842013-10-15 12:05:58 +11001471 if (print_flag)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001472 mprintf("Retrieving %s\n", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001473
1474 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1475 mode = dirattrib->perm & 01777;
1476 else {
1477 debug("Server did not send permissions for "
1478 "directory \"%s\"", dst);
1479 }
1480
1481 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1482 error("mkdir %s: %s", dst, strerror(errno));
1483 return -1;
1484 }
1485
1486 if (do_readdir(conn, src, &dir_entries) == -1) {
1487 error("%s: Failed to get directory contents", src);
1488 return -1;
1489 }
1490
1491 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1492 filename = dir_entries[i]->filename;
1493
1494 new_dst = path_append(dst, filename);
1495 new_src = path_append(src, filename);
1496
1497 if (S_ISDIR(dir_entries[i]->a.perm)) {
1498 if (strcmp(filename, ".") == 0 ||
1499 strcmp(filename, "..") == 0)
1500 continue;
1501 if (download_dir_internal(conn, new_src, new_dst,
Damien Millerbda5c842013-10-15 12:05:58 +11001502 depth + 1, &(dir_entries[i]->a), preserve_flag,
Damien Millerf29238e2013-10-17 11:48:52 +11001503 print_flag, resume_flag, fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001504 ret = -1;
1505 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1506 if (do_download(conn, new_src, new_dst,
Damien Millerf29238e2013-10-17 11:48:52 +11001507 &(dir_entries[i]->a), preserve_flag,
1508 resume_flag, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001509 error("Download of file %s to %s failed",
1510 new_src, new_dst);
1511 ret = -1;
1512 }
1513 } else
1514 logit("%s: not a regular file\n", new_src);
1515
Darren Tuckera627d422013-06-02 07:31:17 +10001516 free(new_dst);
1517 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001518 }
1519
Damien Millerbda5c842013-10-15 12:05:58 +11001520 if (preserve_flag) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001521 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1522 struct timeval tv[2];
1523 tv[0].tv_sec = dirattrib->atime;
1524 tv[1].tv_sec = dirattrib->mtime;
1525 tv[0].tv_usec = tv[1].tv_usec = 0;
1526 if (utimes(dst, tv) == -1)
1527 error("Can't set times on \"%s\": %s",
1528 dst, strerror(errno));
1529 } else
1530 debug("Server did not send times for directory "
1531 "\"%s\"", dst);
1532 }
1533
1534 free_sftp_dirents(dir_entries);
1535
1536 return ret;
1537}
1538
1539int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001540download_dir(struct sftp_conn *conn, const char *src, const char *dst,
1541 Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1542 int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001543{
1544 char *src_canon;
1545 int ret;
1546
1547 if ((src_canon = do_realpath(conn, src)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001548 error("Unable to canonicalize path \"%s\"", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001549 return -1;
1550 }
1551
Damien Millerbda5c842013-10-15 12:05:58 +11001552 ret = download_dir_internal(conn, src_canon, dst, 0,
Damien Millerf29238e2013-10-17 11:48:52 +11001553 dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag);
Darren Tuckera627d422013-06-02 07:31:17 +10001554 free(src_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001555 return ret;
1556}
1557
Damien Miller33804262001-02-04 23:20:18 +11001558int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001559do_upload(struct sftp_conn *conn, const char *local_path,
1560 const char *remote_path, int preserve_flag, int resume, int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001561{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001562 int r, local_fd;
1563 u_int status = SSH2_FX_OK;
1564 u_int id;
1565 u_char type;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001566 off_t offset, progress_counter;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001567 u_char *handle, *data;
1568 struct sshbuf *msg;
Damien Miller33804262001-02-04 23:20:18 +11001569 struct stat sb;
Damien Millerd8accc02014-05-15 13:46:25 +10001570 Attrib a, *c = NULL;
Damien Miller16a13332002-02-13 14:03:56 +11001571 u_int32_t startid;
1572 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001573 struct outstanding_ack {
1574 u_int id;
1575 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001576 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001577 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001578 };
1579 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001580 struct outstanding_ack *ack = NULL;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001581 size_t handle_len;
Damien Miller5873dfd2002-02-13 14:04:37 +11001582
1583 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001584
1585 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1586 error("Couldn't open local file \"%s\" for reading: %s",
1587 local_path, strerror(errno));
1588 return(-1);
1589 }
1590 if (fstat(local_fd, &sb) == -1) {
1591 error("Couldn't fstat local file \"%s\": %s",
1592 local_path, strerror(errno));
1593 close(local_fd);
1594 return(-1);
1595 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001596 if (!S_ISREG(sb.st_mode)) {
1597 error("%s is not a regular file", local_path);
1598 close(local_fd);
1599 return(-1);
1600 }
Damien Miller33804262001-02-04 23:20:18 +11001601 stat_to_attrib(&sb, &a);
1602
1603 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1604 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1605 a.perm &= 0777;
Damien Millerbda5c842013-10-15 12:05:58 +11001606 if (!preserve_flag)
Damien Miller33804262001-02-04 23:20:18 +11001607 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1608
Damien Millerd8accc02014-05-15 13:46:25 +10001609 if (resume) {
1610 /* Get remote file size if it exists */
1611 if ((c = do_stat(conn, remote_path, 0)) == NULL) {
djm@openbsd.org34a01b22016-04-08 08:19:17 +00001612 close(local_fd);
Damien Millerd8accc02014-05-15 13:46:25 +10001613 return -1;
1614 }
1615
1616 if ((off_t)c->size >= sb.st_size) {
1617 error("destination file bigger or same size as "
1618 "source file");
1619 close(local_fd);
1620 return -1;
1621 }
1622
1623 if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
1624 close(local_fd);
1625 return -1;
1626 }
1627 }
1628
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001629 if ((msg = sshbuf_new()) == NULL)
1630 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001631
1632 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001633 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001634 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1635 (r = sshbuf_put_u32(msg, id)) != 0 ||
1636 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1637 (r = sshbuf_put_u32(msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|
1638 (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC))) != 0 ||
1639 (r = encode_attrib(msg, &a)) != 0)
1640 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1641 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001642 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001643
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001644 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +11001645
Damien Miller65e42f82010-09-24 22:15:11 +10001646 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001647 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001648 if (handle == NULL) {
1649 close(local_fd);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001650 sshbuf_free(msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001651 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001652 }
1653
Damien Miller16a13332002-02-13 14:03:56 +11001654 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001655 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001656
Damien Miller33804262001-02-04 23:20:18 +11001657 /* Read from local and write to remote */
Damien Millerd8accc02014-05-15 13:46:25 +10001658 offset = progress_counter = (resume ? c->size : 0);
Damien Miller62d57f62003-01-10 21:43:24 +11001659 if (showprogress)
Darren Tuckerc9a19912013-06-02 08:37:05 +10001660 start_progress_meter(local_path, sb.st_size,
1661 &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001662
Damien Miller9f0f5c62001-12-21 14:45:46 +11001663 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001664 int len;
Damien Miller33804262001-02-04 23:20:18 +11001665
1666 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001667 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001668 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001669 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001670 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001671 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001672 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001673 len = 0;
1674 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001675 len = read(local_fd, data, conn->transfer_buflen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001676 while ((len == -1) &&
1677 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
Damien Miller33804262001-02-04 23:20:18 +11001678
1679 if (len == -1)
1680 fatal("Couldn't read from \"%s\": %s", local_path,
1681 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001682
1683 if (len != 0) {
Damien Miller6c81fee2013-11-08 12:19:55 +11001684 ack = xcalloc(1, sizeof(*ack));
Damien Miller5873dfd2002-02-13 14:04:37 +11001685 ack->id = ++id;
1686 ack->offset = offset;
1687 ack->len = len;
1688 TAILQ_INSERT_TAIL(&acks, ack, tq);
1689
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001690 sshbuf_reset(msg);
1691 if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
1692 (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
1693 (r = sshbuf_put_string(msg, handle,
1694 handle_len)) != 0 ||
1695 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1696 (r = sshbuf_put_string(msg, data, len)) != 0)
1697 fatal("%s: buffer error: %s",
1698 __func__, ssh_err(r));
1699 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001700 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001701 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001702 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001703 break;
1704
Damien Miller5873dfd2002-02-13 14:04:37 +11001705 if (ack == NULL)
1706 fatal("Unexpected ACK %u", id);
1707
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001708 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001709 id - ackid >= conn->num_requests) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001710 u_int rid;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001711
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001712 sshbuf_reset(msg);
1713 get_msg(conn, msg);
1714 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1715 (r = sshbuf_get_u32(msg, &rid)) != 0)
1716 fatal("%s: buffer error: %s",
1717 __func__, ssh_err(r));
Damien Miller5873dfd2002-02-13 14:04:37 +11001718
1719 if (type != SSH2_FXP_STATUS)
1720 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1721 "got %d", SSH2_FXP_STATUS, type);
1722
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001723 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1724 fatal("%s: buffer error: %s",
1725 __func__, ssh_err(r));
1726 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller5873dfd2002-02-13 14:04:37 +11001727
1728 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001729 for (ack = TAILQ_FIRST(&acks);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001730 ack != NULL && ack->id != rid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001731 ack = TAILQ_NEXT(ack, tq))
1732 ;
1733 if (ack == NULL)
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001734 fatal("Can't find request for ID %u", rid);
Damien Miller5873dfd2002-02-13 14:04:37 +11001735 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001736 debug3("In write loop, ack for %u %u bytes at %lld",
1737 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001738 ++ackid;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001739 progress_counter += ack->len;
Darren Tuckera627d422013-06-02 07:31:17 +10001740 free(ack);
Damien Miller33804262001-02-04 23:20:18 +11001741 }
Damien Miller33804262001-02-04 23:20:18 +11001742 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001743 if (offset < 0)
1744 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001745 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001746 sshbuf_free(msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001747
Damien Miller62d57f62003-01-10 21:43:24 +11001748 if (showprogress)
1749 stop_progress_meter();
Darren Tuckera627d422013-06-02 07:31:17 +10001750 free(data);
Damien Miller33804262001-02-04 23:20:18 +11001751
Damien Milleracdf25b2008-02-10 22:27:24 +11001752 if (status != SSH2_FX_OK) {
1753 error("Couldn't write to remote file \"%s\": %s",
1754 remote_path, fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001755 status = SSH2_FX_FAILURE;
Damien Milleracdf25b2008-02-10 22:27:24 +11001756 }
1757
Damien Miller33804262001-02-04 23:20:18 +11001758 if (close(local_fd) == -1) {
1759 error("Couldn't close local file \"%s\": %s", local_path,
1760 strerror(errno));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001761 status = SSH2_FX_FAILURE;
Damien Miller33804262001-02-04 23:20:18 +11001762 }
1763
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001764 /* Override umask and utimes if asked */
Damien Millerbda5c842013-10-15 12:05:58 +11001765 if (preserve_flag)
Damien Miller3db5f532002-02-13 14:10:32 +11001766 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001767
Damien Millerf29238e2013-10-17 11:48:52 +11001768 if (fsync_flag)
1769 (void)do_fsync(conn, handle, handle_len);
1770
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001771 if (do_close(conn, handle, handle_len) != 0)
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001772 status = SSH2_FX_FAILURE;
1773
Darren Tuckera627d422013-06-02 07:31:17 +10001774 free(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001775
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001776 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +11001777}
Darren Tucker1b0dd172009-10-07 08:37:48 +11001778
1779static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001780upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1781 int depth, int preserve_flag, int print_flag, int resume, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001782{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001783 int ret = 0;
Darren Tucker1b0dd172009-10-07 08:37:48 +11001784 DIR *dirp;
1785 struct dirent *dp;
1786 char *filename, *new_src, *new_dst;
1787 struct stat sb;
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001788 Attrib a, *dirattrib;
Darren Tucker1b0dd172009-10-07 08:37:48 +11001789
1790 if (depth >= MAX_DIR_DEPTH) {
1791 error("Maximum directory depth exceeded: %d levels", depth);
1792 return -1;
1793 }
1794
1795 if (stat(src, &sb) == -1) {
1796 error("Couldn't stat directory \"%s\": %s",
1797 src, strerror(errno));
1798 return -1;
1799 }
1800 if (!S_ISDIR(sb.st_mode)) {
1801 error("\"%s\" is not a directory", src);
1802 return -1;
1803 }
Damien Millerbda5c842013-10-15 12:05:58 +11001804 if (print_flag)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001805 mprintf("Entering %s\n", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001806
1807 attrib_clear(&a);
1808 stat_to_attrib(&sb, &a);
1809 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1810 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1811 a.perm &= 01777;
Damien Millerbda5c842013-10-15 12:05:58 +11001812 if (!preserve_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001813 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
Damien Miller0d032412013-07-25 11:56:52 +10001814
Darren Tucker1b0dd172009-10-07 08:37:48 +11001815 /*
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001816 * sftp lacks a portable status value to match errno EEXIST,
1817 * so if we get a failure back then we must check whether
1818 * the path already existed and is a directory.
Darren Tucker1b0dd172009-10-07 08:37:48 +11001819 */
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001820 if (do_mkdir(conn, dst, &a, 0) != 0) {
1821 if ((dirattrib = do_stat(conn, dst, 0)) == NULL)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001822 return -1;
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001823 if (!S_ISDIR(dirattrib->perm)) {
1824 error("\"%s\" exists but is not a directory", dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001825 return -1;
djm@openbsd.orge30cabf2016-02-11 02:21:34 +00001826 }
Darren Tucker1b0dd172009-10-07 08:37:48 +11001827 }
1828
1829 if ((dirp = opendir(src)) == NULL) {
1830 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1831 return -1;
1832 }
Damien Miller0d032412013-07-25 11:56:52 +10001833
Darren Tucker1b0dd172009-10-07 08:37:48 +11001834 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1835 if (dp->d_ino == 0)
1836 continue;
1837 filename = dp->d_name;
1838 new_dst = path_append(dst, filename);
1839 new_src = path_append(src, filename);
1840
Darren Tucker438b4732009-10-11 21:52:10 +11001841 if (lstat(new_src, &sb) == -1) {
1842 logit("%s: lstat failed: %s", filename,
1843 strerror(errno));
1844 ret = -1;
1845 } else if (S_ISDIR(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001846 if (strcmp(filename, ".") == 0 ||
1847 strcmp(filename, "..") == 0)
1848 continue;
1849
1850 if (upload_dir_internal(conn, new_src, new_dst,
Damien Millerd8accc02014-05-15 13:46:25 +10001851 depth + 1, preserve_flag, print_flag, resume,
Damien Millerf29238e2013-10-17 11:48:52 +11001852 fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001853 ret = -1;
Darren Tucker438b4732009-10-11 21:52:10 +11001854 } else if (S_ISREG(sb.st_mode)) {
Damien Millerbda5c842013-10-15 12:05:58 +11001855 if (do_upload(conn, new_src, new_dst,
Damien Millerd8accc02014-05-15 13:46:25 +10001856 preserve_flag, resume, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001857 error("Uploading of file %s to %s failed!",
1858 new_src, new_dst);
1859 ret = -1;
1860 }
1861 } else
1862 logit("%s: not a regular file\n", filename);
Darren Tuckera627d422013-06-02 07:31:17 +10001863 free(new_dst);
1864 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001865 }
1866
1867 do_setstat(conn, dst, &a);
1868
1869 (void) closedir(dirp);
1870 return ret;
1871}
1872
1873int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001874upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
1875 int preserve_flag, int print_flag, int resume, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001876{
1877 char *dst_canon;
1878 int ret;
1879
1880 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001881 error("Unable to canonicalize path \"%s\"", dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001882 return -1;
1883 }
1884
Damien Miller2f93d052013-10-15 12:06:27 +11001885 ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
Damien Millerd8accc02014-05-15 13:46:25 +10001886 print_flag, resume, fsync_flag);
Damien Millerf29238e2013-10-17 11:48:52 +11001887
Darren Tuckera627d422013-06-02 07:31:17 +10001888 free(dst_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001889 return ret;
1890}
1891
1892char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001893path_append(const char *p1, const char *p2)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001894{
1895 char *ret;
1896 size_t len = strlen(p1) + strlen(p2) + 2;
1897
1898 ret = xmalloc(len);
1899 strlcpy(ret, p1, len);
1900 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1901 strlcat(ret, "/", len);
1902 strlcat(ret, p2, len);
1903
1904 return(ret);
1905}
1906