blob: 5dbeb47c06246c9a6d5cb01080da35ef6d764ecc [file] [log] [blame]
djm@openbsd.org9cc68422015-05-28 04:50:53 +00001/* $OpenBSD: sftp-client.c,v 1.120 2015/05/28 04:50:53 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
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000025#include <sys/param.h> /* MIN MAX */
Damien Millerf17883e2006-03-15 11:45:54 +110026#include <sys/types.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100027#ifdef HAVE_SYS_STATVFS_H
Damien Millerd671e5a2008-05-19 14:53:33 +100028#include <sys/statvfs.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100029#endif
Damien Millerd7834352006-08-05 12:39:39 +100030#include "openbsd-compat/sys-queue.h"
Damien Millerf17883e2006-03-15 11:45:54 +110031#ifdef HAVE_SYS_STAT_H
32# include <sys/stat.h>
33#endif
Damien Miller9aec9192006-08-05 10:57:45 +100034#ifdef HAVE_SYS_TIME_H
35# include <sys/time.h>
36#endif
Damien Millerd7834352006-08-05 12:39:39 +100037#include <sys/uio.h>
Damien Miller57cf6382006-07-10 21:13:46 +100038
Darren Tucker1b0dd172009-10-07 08:37:48 +110039#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100040#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100041#include <fcntl.h>
42#include <signal.h>
Damien Millerd7834352006-08-05 12:39:39 +100043#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100044#include <stdio.h>
Damien Miller0600c702013-11-21 13:55:43 +110045#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100046#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100047#include <unistd.h>
Damien Miller16a13332002-02-13 14:03:56 +110048
Damien Miller33804262001-02-04 23:20:18 +110049#include "xmalloc.h"
djm@openbsd.org7d845f42015-01-14 13:54:13 +000050#include "ssherr.h"
51#include "sshbuf.h"
Damien Miller33804262001-02-04 23:20:18 +110052#include "log.h"
53#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110054#include "progressmeter.h"
Damien Miller3f941882006-03-31 23:13:02 +110055#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110056
57#include "sftp.h"
58#include "sftp-common.h"
59#include "sftp-client.h"
60
Darren Tuckercdf547a2004-05-24 10:12:19 +100061extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110062extern int showprogress;
63
Damien Miller0c8d8f62006-03-15 11:34:25 +110064/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110065#define MIN_READ_SIZE 512
66
Darren Tucker1b0dd172009-10-07 08:37:48 +110067/* Maximum depth to descend in directory trees */
68#define MAX_DIR_DEPTH 64
69
Damien Miller3db5f532002-02-13 14:10:32 +110070struct sftp_conn {
71 int fd_in;
72 int fd_out;
73 u_int transfer_buflen;
74 u_int num_requests;
75 u_int version;
76 u_int msg_id;
Damien Millerd671e5a2008-05-19 14:53:33 +100077#define SFTP_EXT_POSIX_RENAME 0x00000001
78#define SFTP_EXT_STATVFS 0x00000002
79#define SFTP_EXT_FSTATVFS 0x00000004
Darren Tuckeraf1f9092010-12-05 09:02:47 +110080#define SFTP_EXT_HARDLINK 0x00000008
Damien Millerf29238e2013-10-17 11:48:52 +110081#define SFTP_EXT_FSYNC 0x00000010
Damien Miller7a3e1d02008-03-27 10:59:57 +110082 u_int exts;
Damien Miller65e42f82010-09-24 22:15:11 +100083 u_int64_t limit_kbps;
84 struct bwlimit bwlimit_in, bwlimit_out;
Damien Miller3db5f532002-02-13 14:10:32 +110085};
Ben Lindstrom288cc392001-02-09 02:58:04 +000086
djm@openbsd.org7d845f42015-01-14 13:54:13 +000087static u_char *
88get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
Damien Miller65e42f82010-09-24 22:15:11 +100089 const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
90
91/* ARGSUSED */
92static int
93sftpio(void *_bwlimit, size_t amount)
94{
95 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
96
97 bandwidth_limit(bwlimit, amount);
98 return 0;
99}
Darren Tuckerc22f0902009-10-07 08:24:19 +1100100
Ben Lindstrombba81212001-06-25 05:01:22 +0000101static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000102send_msg(struct sftp_conn *conn, struct sshbuf *m)
Damien Miller33804262001-02-04 23:20:18 +1100103{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100104 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +1000105 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +1100106
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000107 if (sshbuf_len(m) > SFTP_MAX_MSG_LENGTH)
108 fatal("Outbound message too long %zu", sshbuf_len(m));
Damien Miller33804262001-02-04 23:20:18 +1100109
Damien Millera7f3aaa2003-01-10 21:43:58 +1100110 /* Send length first */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000111 put_u32(mlen, sshbuf_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +1000112 iov[0].iov_base = mlen;
113 iov[0].iov_len = sizeof(mlen);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000114 iov[1].iov_base = (u_char *)sshbuf_ptr(m);
115 iov[1].iov_len = sshbuf_len(m);
Damien Millerd7834352006-08-05 12:39:39 +1000116
Damien Miller65e42f82010-09-24 22:15:11 +1000117 if (atomiciov6(writev, conn->fd_out, iov, 2,
Damien Miller0d032412013-07-25 11:56:52 +1000118 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_out) !=
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000119 sshbuf_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +1100120 fatal("Couldn't send packet: %s", strerror(errno));
121
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000122 sshbuf_reset(m);
Damien Miller33804262001-02-04 23:20:18 +1100123}
124
Ben Lindstrombba81212001-06-25 05:01:22 +0000125static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000126get_msg(struct sftp_conn *conn, struct sshbuf *m)
Damien Miller33804262001-02-04 23:20:18 +1100127{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100128 u_int msg_len;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000129 u_char *p;
130 int r;
Damien Miller33804262001-02-04 23:20:18 +1100131
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000132 if ((r = sshbuf_reserve(m, 4, &p)) != 0)
133 fatal("%s: buffer error: %s", __func__, ssh_err(r));
134 if (atomicio6(read, conn->fd_in, p, 4,
Damien Miller65e42f82010-09-24 22:15:11 +1000135 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in) != 4) {
Damien Millerb253cc42005-05-26 12:23:44 +1000136 if (errno == EPIPE)
137 fatal("Connection closed");
138 else
139 fatal("Couldn't read packet: %s", strerror(errno));
140 }
Damien Miller33804262001-02-04 23:20:18 +1100141
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000142 if ((r = sshbuf_get_u32(m, &msg_len)) != 0)
143 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller54446182006-01-02 23:40:50 +1100144 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000145 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100146
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000147 if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
148 fatal("%s: buffer error: %s", __func__, ssh_err(r));
149 if (atomicio6(read, conn->fd_in, p, msg_len,
Damien Miller65e42f82010-09-24 22:15:11 +1000150 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in)
151 != msg_len) {
Damien Millerb253cc42005-05-26 12:23:44 +1000152 if (errno == EPIPE)
153 fatal("Connection closed");
154 else
155 fatal("Read packet: %s", strerror(errno));
156 }
Damien Miller33804262001-02-04 23:20:18 +1100157}
158
Ben Lindstrombba81212001-06-25 05:01:22 +0000159static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000160send_string_request(struct sftp_conn *conn, u_int id, u_int code, const char *s,
Damien Miller33804262001-02-04 23:20:18 +1100161 u_int len)
162{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000163 struct sshbuf *msg;
164 int r;
Damien Miller33804262001-02-04 23:20:18 +1100165
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000166 if ((msg = sshbuf_new()) == NULL)
167 fatal("%s: sshbuf_new failed", __func__);
168 if ((r = sshbuf_put_u8(msg, code)) != 0 ||
169 (r = sshbuf_put_u32(msg, id)) != 0 ||
170 (r = sshbuf_put_string(msg, s, len)) != 0)
171 fatal("%s: buffer error: %s", __func__, ssh_err(r));
172 send_msg(conn, msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000173 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000174 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100175}
176
Ben Lindstrombba81212001-06-25 05:01:22 +0000177static void
Damien Miller65e42f82010-09-24 22:15:11 +1000178send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000179 const void *s, u_int len, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100180{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000181 struct sshbuf *msg;
182 int r;
Damien Miller33804262001-02-04 23:20:18 +1100183
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000184 if ((msg = sshbuf_new()) == NULL)
185 fatal("%s: sshbuf_new failed", __func__);
186 if ((r = sshbuf_put_u8(msg, code)) != 0 ||
187 (r = sshbuf_put_u32(msg, id)) != 0 ||
188 (r = sshbuf_put_string(msg, s, len)) != 0 ||
189 (r = encode_attrib(msg, a)) != 0)
190 fatal("%s: buffer error: %s", __func__, ssh_err(r));
191 send_msg(conn, msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000192 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000193 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100194}
195
Ben Lindstrombba81212001-06-25 05:01:22 +0000196static u_int
Damien Miller65e42f82010-09-24 22:15:11 +1000197get_status(struct sftp_conn *conn, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100198{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000199 struct sshbuf *msg;
200 u_char type;
201 u_int id, status;
202 int r;
Damien Miller33804262001-02-04 23:20:18 +1100203
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000204 if ((msg = sshbuf_new()) == NULL)
205 fatal("%s: sshbuf_new failed", __func__);
206 get_msg(conn, msg);
207 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
208 (r = sshbuf_get_u32(msg, &id)) != 0)
209 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100210
211 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000212 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100213 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000214 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100215 SSH2_FXP_STATUS, type);
216
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000217 if ((r = sshbuf_get_u32(msg, &status)) != 0)
218 fatal("%s: buffer error: %s", __func__, ssh_err(r));
219 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100220
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000221 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100222
Damien Miller65e42f82010-09-24 22:15:11 +1000223 return status;
Damien Miller33804262001-02-04 23:20:18 +1100224}
225
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000226static u_char *
227get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
Damien Miller65e42f82010-09-24 22:15:11 +1000228 const char *errfmt, ...)
Damien Miller33804262001-02-04 23:20:18 +1100229{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000230 struct sshbuf *msg;
231 u_int id, status;
232 u_char type;
233 u_char *handle;
234 char errmsg[256];
Darren Tuckerc22f0902009-10-07 08:24:19 +1100235 va_list args;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000236 int r;
Darren Tuckerc22f0902009-10-07 08:24:19 +1100237
238 va_start(args, errfmt);
239 if (errfmt != NULL)
240 vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
241 va_end(args);
Damien Miller33804262001-02-04 23:20:18 +1100242
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000243 if ((msg = sshbuf_new()) == NULL)
244 fatal("%s: sshbuf_new failed", __func__);
245 get_msg(conn, msg);
246 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
247 (r = sshbuf_get_u32(msg, &id)) != 0)
248 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100249
250 if (id != expected_id)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100251 fatal("%s: ID mismatch (%u != %u)",
252 errfmt == NULL ? __func__ : errmsg, id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100253 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000254 if ((r = sshbuf_get_u32(msg, &status)) != 0)
255 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerc22f0902009-10-07 08:24:19 +1100256 if (errfmt != NULL)
257 error("%s: %s", errmsg, fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000258 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100259 return(NULL);
260 } else if (type != SSH2_FXP_HANDLE)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100261 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
262 errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
Damien Miller33804262001-02-04 23:20:18 +1100263
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000264 if ((r = sshbuf_get_string(msg, &handle, len)) != 0)
265 fatal("%s: buffer error: %s", __func__, ssh_err(r));
266 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100267
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000268 return handle;
Damien Miller33804262001-02-04 23:20:18 +1100269}
270
Ben Lindstrombba81212001-06-25 05:01:22 +0000271static Attrib *
Damien Miller65e42f82010-09-24 22:15:11 +1000272get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100273{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000274 struct sshbuf *msg;
275 u_int id;
276 u_char type;
277 int r;
278 static Attrib a;
Damien Miller33804262001-02-04 23:20:18 +1100279
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000280 if ((msg = sshbuf_new()) == NULL)
281 fatal("%s: sshbuf_new failed", __func__);
282 get_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100283
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000284 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
285 (r = sshbuf_get_u32(msg, &id)) != 0)
286 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100287
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000288 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100289 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000290 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100291 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000292 u_int status;
Damien Miller33804262001-02-04 23:20:18 +1100293
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000294 if ((r = sshbuf_get_u32(msg, &status)) != 0)
295 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000296 if (quiet)
297 debug("Couldn't stat remote file: %s", fx2txt(status));
298 else
299 error("Couldn't stat remote file: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000300 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100301 return(NULL);
302 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000303 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100304 SSH2_FXP_ATTRS, type);
305 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000306 if ((r = decode_attrib(msg, &a)) != 0) {
307 error("%s: couldn't decode attrib: %s", __func__, ssh_err(r));
308 sshbuf_free(msg);
309 return NULL;
310 }
311 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100312
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000313 return &a;
Damien Miller33804262001-02-04 23:20:18 +1100314}
315
Damien Millerd671e5a2008-05-19 14:53:33 +1000316static int
Damien Miller65e42f82010-09-24 22:15:11 +1000317get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
318 u_int expected_id, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000319{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000320 struct sshbuf *msg;
321 u_char type;
322 u_int id;
323 u_int64_t flag;
324 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +1000325
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000326 if ((msg = sshbuf_new()) == NULL)
327 fatal("%s: sshbuf_new failed", __func__);
328 get_msg(conn, msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000329
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000330 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
331 (r = sshbuf_get_u32(msg, &id)) != 0)
332 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000333
334 debug3("Received statvfs reply T:%u I:%u", type, id);
335 if (id != expected_id)
336 fatal("ID mismatch (%u != %u)", id, expected_id);
337 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000338 u_int status;
Damien Millerd671e5a2008-05-19 14:53:33 +1000339
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000340 if ((r = sshbuf_get_u32(msg, &status)) != 0)
341 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000342 if (quiet)
343 debug("Couldn't statvfs: %s", fx2txt(status));
344 else
345 error("Couldn't statvfs: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000346 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000347 return -1;
348 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
349 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
350 SSH2_FXP_EXTENDED_REPLY, type);
351 }
352
Damien Miller1d2c4562014-02-04 11:18:20 +1100353 memset(st, 0, sizeof(*st));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000354 if ((r = sshbuf_get_u64(msg, &st->f_bsize)) != 0 ||
355 (r = sshbuf_get_u64(msg, &st->f_frsize)) != 0 ||
356 (r = sshbuf_get_u64(msg, &st->f_blocks)) != 0 ||
357 (r = sshbuf_get_u64(msg, &st->f_bfree)) != 0 ||
358 (r = sshbuf_get_u64(msg, &st->f_bavail)) != 0 ||
359 (r = sshbuf_get_u64(msg, &st->f_files)) != 0 ||
360 (r = sshbuf_get_u64(msg, &st->f_ffree)) != 0 ||
361 (r = sshbuf_get_u64(msg, &st->f_favail)) != 0 ||
362 (r = sshbuf_get_u64(msg, &st->f_fsid)) != 0 ||
363 (r = sshbuf_get_u64(msg, &flag)) != 0 ||
364 (r = sshbuf_get_u64(msg, &st->f_namemax)) != 0)
365 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000366
367 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
368 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
369
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000370 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000371
372 return 0;
373}
374
Damien Miller3db5f532002-02-13 14:10:32 +1100375struct sftp_conn *
Damien Miller65e42f82010-09-24 22:15:11 +1000376do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
377 u_int64_t limit_kbps)
Damien Miller33804262001-02-04 23:20:18 +1100378{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000379 u_char type;
380 struct sshbuf *msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100381 struct sftp_conn *ret;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000382 int r;
Damien Miller33804262001-02-04 23:20:18 +1100383
Damien Millerfec029f2013-08-21 02:42:12 +1000384 ret = xcalloc(1, sizeof(*ret));
385 ret->msg_id = 1;
Damien Miller65e42f82010-09-24 22:15:11 +1000386 ret->fd_in = fd_in;
387 ret->fd_out = fd_out;
388 ret->transfer_buflen = transfer_buflen;
389 ret->num_requests = num_requests;
390 ret->exts = 0;
391 ret->limit_kbps = 0;
392
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000393 if ((msg = sshbuf_new()) == NULL)
394 fatal("%s: sshbuf_new failed", __func__);
395 if ((r = sshbuf_put_u8(msg, SSH2_FXP_INIT)) != 0 ||
396 (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0)
397 fatal("%s: buffer error: %s", __func__, ssh_err(r));
398 send_msg(ret, msg);
Damien Miller33804262001-02-04 23:20:18 +1100399
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000400 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +1100401
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000402 get_msg(ret, msg);
Damien Miller33804262001-02-04 23:20:18 +1100403
Kevin Stevesef4eea92001-02-05 12:42:17 +0000404 /* Expecting a VERSION reply */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000405 if ((r = sshbuf_get_u8(msg, &type)) != 0)
406 fatal("%s: buffer error: %s", __func__, ssh_err(r));
407 if (type != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000408 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100409 type);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000410 sshbuf_free(msg);
jsg@openbsd.org64a89ec2015-05-23 14:28:37 +0000411 free(ret);
Damien Miller3db5f532002-02-13 14:10:32 +1100412 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100413 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000414 if ((r = sshbuf_get_u32(msg, &ret->version)) != 0)
415 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100416
Damien Miller65e42f82010-09-24 22:15:11 +1000417 debug2("Remote version: %u", ret->version);
Damien Miller33804262001-02-04 23:20:18 +1100418
419 /* Check for extensions */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000420 while (sshbuf_len(msg) > 0) {
421 char *name;
422 u_char *value;
423 size_t vlen;
Darren Tuckera64ab332008-06-13 07:01:29 +1000424 int known = 0;
Damien Miller33804262001-02-04 23:20:18 +1100425
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000426 if ((r = sshbuf_get_cstring(msg, &name, NULL)) != 0 ||
427 (r = sshbuf_get_string(msg, &value, &vlen)) != 0)
428 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000429 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000430 strcmp((char *)value, "1") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000431 ret->exts |= SFTP_EXT_POSIX_RENAME;
Darren Tuckera64ab332008-06-13 07:01:29 +1000432 known = 1;
433 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000434 strcmp((char *)value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000435 ret->exts |= SFTP_EXT_STATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000436 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100437 } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000438 strcmp((char *)value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000439 ret->exts |= SFTP_EXT_FSTATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000440 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100441 } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000442 strcmp((char *)value, "1") == 0) {
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100443 ret->exts |= SFTP_EXT_HARDLINK;
444 known = 1;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000445 } else if (strcmp(name, "fsync@openssh.com") == 0 &&
446 strcmp((char *)value, "1") == 0) {
447 ret->exts |= SFTP_EXT_FSYNC;
448 known = 1;
Darren Tuckera64ab332008-06-13 07:01:29 +1000449 }
450 if (known) {
451 debug2("Server supports extension \"%s\" revision %s",
452 name, value);
453 } else {
454 debug2("Unrecognised server extension \"%s\"", name);
455 }
Darren Tuckera627d422013-06-02 07:31:17 +1000456 free(name);
457 free(value);
Damien Miller33804262001-02-04 23:20:18 +1100458 }
459
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000460 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +1100461
Damien Miller3db5f532002-02-13 14:10:32 +1100462 /* Some filexfer v.0 servers don't support large packets */
Damien Miller65e42f82010-09-24 22:15:11 +1000463 if (ret->version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000464 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100465
Damien Miller65e42f82010-09-24 22:15:11 +1000466 ret->limit_kbps = limit_kbps;
467 if (ret->limit_kbps > 0) {
468 bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
469 ret->transfer_buflen);
470 bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
471 ret->transfer_buflen);
472 }
473
474 return ret;
Damien Miller3db5f532002-02-13 14:10:32 +1100475}
476
477u_int
478sftp_proto_version(struct sftp_conn *conn)
479{
Damien Miller65e42f82010-09-24 22:15:11 +1000480 return conn->version;
Damien Miller33804262001-02-04 23:20:18 +1100481}
482
483int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000484do_close(struct sftp_conn *conn, const u_char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100485{
486 u_int id, status;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000487 struct sshbuf *msg;
488 int r;
Damien Miller33804262001-02-04 23:20:18 +1100489
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000490 if ((msg = sshbuf_new()) == NULL)
491 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100492
Damien Miller3db5f532002-02-13 14:10:32 +1100493 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000494 if ((r = sshbuf_put_u8(msg, SSH2_FXP_CLOSE)) != 0 ||
495 (r = sshbuf_put_u32(msg, id)) != 0 ||
496 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
497 fatal("%s: buffer error: %s", __func__, ssh_err(r));
498 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000499 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100500
Damien Miller65e42f82010-09-24 22:15:11 +1000501 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100502 if (status != SSH2_FX_OK)
503 error("Couldn't close file: %s", fx2txt(status));
504
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000505 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100506
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000507 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100508}
509
Damien Miller4870afd2001-03-14 10:27:09 +1100510
Ben Lindstrombba81212001-06-25 05:01:22 +0000511static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000512do_lsreaddir(struct sftp_conn *conn, const char *path, int print_flag,
Damien Miller4870afd2001-03-14 10:27:09 +1100513 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100514{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000515 struct sshbuf *msg;
516 u_int count, id, i, expected_id, ents = 0;
517 size_t handle_len;
518 u_char type;
Damien Miller33804262001-02-04 23:20:18 +1100519 char *handle;
Damien Miller39392072013-12-07 10:31:08 +1100520 int status = SSH2_FX_FAILURE;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000521 int r;
Damien Miller39392072013-12-07 10:31:08 +1100522
523 if (dir)
524 *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +1100525
Damien Miller3db5f532002-02-13 14:10:32 +1100526 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100527
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000528 if ((msg = sshbuf_new()) == NULL)
529 fatal("%s: sshbuf_new failed", __func__);
530 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPENDIR)) != 0 ||
531 (r = sshbuf_put_u32(msg, id)) != 0 ||
532 (r = sshbuf_put_cstring(msg, path)) != 0)
533 fatal("%s: buffer error: %s", __func__, ssh_err(r));
534 send_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100535
Damien Miller65e42f82010-09-24 22:15:11 +1000536 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +1100537 "remote readdir(\"%s\")", path);
Damien Miller57c38ac2011-09-22 21:42:45 +1000538 if (handle == NULL) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000539 sshbuf_free(msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000540 return -1;
Damien Miller57c38ac2011-09-22 21:42:45 +1000541 }
Damien Miller33804262001-02-04 23:20:18 +1100542
Damien Miller4870afd2001-03-14 10:27:09 +1100543 if (dir) {
544 ents = 0;
Damien Miller6c81fee2013-11-08 12:19:55 +1100545 *dir = xcalloc(1, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100546 (*dir)[0] = NULL;
547 }
Damien Miller4870afd2001-03-14 10:27:09 +1100548
Darren Tuckercdf547a2004-05-24 10:12:19 +1000549 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100550 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100551
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000552 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100553
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000554 sshbuf_reset(msg);
555 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READDIR)) != 0 ||
556 (r = sshbuf_put_u32(msg, id)) != 0 ||
557 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
558 fatal("%s: buffer error: %s", __func__, ssh_err(r));
559 send_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100560
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000561 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +1100562
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000563 get_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100564
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000565 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
566 (r = sshbuf_get_u32(msg, &id)) != 0)
567 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100568
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000569 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100570
571 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000572 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100573
574 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000575 u_int rstatus;
576
577 if ((r = sshbuf_get_u32(msg, &rstatus)) != 0)
578 fatal("%s: buffer error: %s",
579 __func__, ssh_err(r));
580 debug3("Received SSH2_FXP_STATUS %d", rstatus);
581 if (rstatus == SSH2_FX_EOF)
Damien Miller33804262001-02-04 23:20:18 +1100582 break;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000583 error("Couldn't read directory: %s", fx2txt(rstatus));
Damien Miller39392072013-12-07 10:31:08 +1100584 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100585 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000586 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100587 SSH2_FXP_NAME, type);
588
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000589 if ((r = sshbuf_get_u32(msg, &count)) != 0)
590 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd7686fd2001-02-10 00:40:03 +1100591 if (count == 0)
592 break;
593 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100594 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100595 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000596 Attrib a;
Damien Miller33804262001-02-04 23:20:18 +1100597
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000598 if ((r = sshbuf_get_cstring(msg, &filename,
599 NULL)) != 0 ||
600 (r = sshbuf_get_cstring(msg, &longname,
601 NULL)) != 0)
602 fatal("%s: buffer error: %s",
603 __func__, ssh_err(r));
604 if ((r = decode_attrib(msg, &a)) != 0) {
605 error("%s: couldn't decode attrib: %s",
606 __func__, ssh_err(r));
607 free(filename);
608 free(longname);
609 sshbuf_free(msg);
610 return -1;
611 }
Damien Miller33804262001-02-04 23:20:18 +1100612
Damien Millerbda5c842013-10-15 12:05:58 +1100613 if (print_flag)
Damien Miller4870afd2001-03-14 10:27:09 +1100614 printf("%s\n", longname);
615
Darren Tucker1b0dd172009-10-07 08:37:48 +1100616 /*
617 * Directory entries should never contain '/'
618 * These can be used to attack recursive ops
619 * (e.g. send '../../../../etc/passwd')
620 */
621 if (strchr(filename, '/') != NULL) {
622 error("Server sent suspect path \"%s\" "
623 "during readdir of \"%s\"", filename, path);
Damien Miller39392072013-12-07 10:31:08 +1100624 } else if (dir) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000625 *dir = xreallocarray(*dir, ents + 2, sizeof(**dir));
Damien Miller6c81fee2013-11-08 12:19:55 +1100626 (*dir)[ents] = xcalloc(1, sizeof(***dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100627 (*dir)[ents]->filename = xstrdup(filename);
628 (*dir)[ents]->longname = xstrdup(longname);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000629 memcpy(&(*dir)[ents]->a, &a, sizeof(a));
Damien Miller4870afd2001-03-14 10:27:09 +1100630 (*dir)[++ents] = NULL;
631 }
Darren Tuckera627d422013-06-02 07:31:17 +1000632 free(filename);
633 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100634 }
635 }
Damien Miller39392072013-12-07 10:31:08 +1100636 status = 0;
Damien Miller33804262001-02-04 23:20:18 +1100637
Damien Miller39392072013-12-07 10:31:08 +1100638 out:
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000639 sshbuf_free(msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100640 do_close(conn, handle, handle_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000641 free(handle);
Damien Miller33804262001-02-04 23:20:18 +1100642
Damien Miller39392072013-12-07 10:31:08 +1100643 if (status != 0 && dir != NULL) {
644 /* Don't return results on error */
645 free_sftp_dirents(*dir);
646 *dir = NULL;
647 } else if (interrupted && dir != NULL && *dir != NULL) {
648 /* Don't return partial matches on interrupt */
Darren Tuckercdf547a2004-05-24 10:12:19 +1000649 free_sftp_dirents(*dir);
Damien Miller6c81fee2013-11-08 12:19:55 +1100650 *dir = xcalloc(1, sizeof(**dir));
Darren Tuckercdf547a2004-05-24 10:12:19 +1000651 **dir = NULL;
652 }
653
Damien Miller39392072013-12-07 10:31:08 +1100654 return status;
Damien Miller33804262001-02-04 23:20:18 +1100655}
656
657int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000658do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100659{
Damien Miller3db5f532002-02-13 14:10:32 +1100660 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100661}
662
663void free_sftp_dirents(SFTP_DIRENT **s)
664{
665 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100666
Damien Miller39392072013-12-07 10:31:08 +1100667 if (s == NULL)
668 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100669 for (i = 0; s[i]; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000670 free(s[i]->filename);
671 free(s[i]->longname);
672 free(s[i]);
Damien Miller4870afd2001-03-14 10:27:09 +1100673 }
Darren Tuckera627d422013-06-02 07:31:17 +1000674 free(s);
Damien Miller4870afd2001-03-14 10:27:09 +1100675}
676
677int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000678do_rm(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100679{
680 u_int status, id;
681
682 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
683
Damien Miller3db5f532002-02-13 14:10:32 +1100684 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000685 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
686 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100687 if (status != SSH2_FX_OK)
688 error("Couldn't delete file: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000689 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100690}
691
692int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000693do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100694{
695 u_int status, id;
696
Damien Miller3db5f532002-02-13 14:10:32 +1100697 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000698 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100699 strlen(path), a);
700
Damien Miller65e42f82010-09-24 22:15:11 +1000701 status = get_status(conn, id);
Damien Millerbda5c842013-10-15 12:05:58 +1100702 if (status != SSH2_FX_OK && print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100703 error("Couldn't create directory: %s", fx2txt(status));
704
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000705 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100706}
707
708int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000709do_rmdir(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100710{
711 u_int status, id;
712
Damien Miller3db5f532002-02-13 14:10:32 +1100713 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000714 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100715 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100716
Damien Miller65e42f82010-09-24 22:15:11 +1000717 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100718 if (status != SSH2_FX_OK)
719 error("Couldn't remove directory: %s", fx2txt(status));
720
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000721 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100722}
723
724Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000725do_stat(struct sftp_conn *conn, const char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100726{
727 u_int id;
728
Damien Miller3db5f532002-02-13 14:10:32 +1100729 id = conn->msg_id++;
730
Damien Miller65e42f82010-09-24 22:15:11 +1000731 send_string_request(conn, id,
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000732 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100733 path, strlen(path));
734
Damien Miller65e42f82010-09-24 22:15:11 +1000735 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100736}
737
738Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000739do_lstat(struct sftp_conn *conn, const char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100740{
741 u_int id;
742
Damien Miller3db5f532002-02-13 14:10:32 +1100743 if (conn->version == 0) {
744 if (quiet)
745 debug("Server version does not support lstat operation");
746 else
Damien Miller996acd22003-04-09 20:59:48 +1000747 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000748 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100749 }
750
751 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000752 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100753 strlen(path));
754
Damien Miller65e42f82010-09-24 22:15:11 +1000755 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100756}
757
Damien Millercfe23d32008-02-10 22:20:44 +1100758#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100759Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000760do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
761 int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100762{
763 u_int id;
764
Damien Miller3db5f532002-02-13 14:10:32 +1100765 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000766 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
Damien Miller3db5f532002-02-13 14:10:32 +1100767 handle_len);
768
Damien Miller65e42f82010-09-24 22:15:11 +1000769 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100770}
Damien Millercfe23d32008-02-10 22:20:44 +1100771#endif
Damien Miller33804262001-02-04 23:20:18 +1100772
773int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000774do_setstat(struct sftp_conn *conn, const char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100775{
776 u_int status, id;
777
Damien Miller3db5f532002-02-13 14:10:32 +1100778 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000779 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100780 strlen(path), a);
781
Damien Miller65e42f82010-09-24 22:15:11 +1000782 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100783 if (status != SSH2_FX_OK)
784 error("Couldn't setstat on \"%s\": %s", path,
785 fx2txt(status));
786
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000787 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100788}
789
790int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000791do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100792 Attrib *a)
793{
794 u_int status, id;
795
Damien Miller3db5f532002-02-13 14:10:32 +1100796 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000797 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100798 handle_len, a);
799
Damien Miller65e42f82010-09-24 22:15:11 +1000800 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100801 if (status != SSH2_FX_OK)
802 error("Couldn't fsetstat: %s", fx2txt(status));
803
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000804 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100805}
806
807char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000808do_realpath(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100809{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000810 struct sshbuf *msg;
811 u_int expected_id, count, id;
Damien Miller33804262001-02-04 23:20:18 +1100812 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000813 Attrib a;
814 u_char type;
815 int r;
Damien Miller33804262001-02-04 23:20:18 +1100816
Damien Miller3db5f532002-02-13 14:10:32 +1100817 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000818 send_string_request(conn, id, SSH2_FXP_REALPATH, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100819 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100820
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000821 if ((msg = sshbuf_new()) == NULL)
822 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100823
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000824 get_msg(conn, msg);
825 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
826 (r = sshbuf_get_u32(msg, &id)) != 0)
827 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100828
829 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000830 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100831
832 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000833 u_int status;
Damien Miller33804262001-02-04 23:20:18 +1100834
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000835 if ((r = sshbuf_get_u32(msg, &status)) != 0)
836 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf29238e2013-10-17 11:48:52 +1100837 error("Couldn't canonicalize: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000838 sshbuf_free(msg);
Damien Miller49566312010-06-26 09:38:23 +1000839 return NULL;
Damien Miller33804262001-02-04 23:20:18 +1100840 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000841 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100842 SSH2_FXP_NAME, type);
843
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000844 if ((r = sshbuf_get_u32(msg, &count)) != 0)
845 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100846 if (count != 1)
847 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
848
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000849 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
850 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
851 (r = decode_attrib(msg, &a)) != 0)
852 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100853
Darren Tucker4908d442012-07-02 22:15:38 +1000854 debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000855 (unsigned long)a.size);
Damien Miller33804262001-02-04 23:20:18 +1100856
Darren Tuckera627d422013-06-02 07:31:17 +1000857 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100858
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000859 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100860
861 return(filename);
862}
863
864int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000865do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath,
Damien Millerc7dba122013-08-21 02:41:15 +1000866 int force_legacy)
Damien Miller33804262001-02-04 23:20:18 +1100867{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000868 struct sshbuf *msg;
Damien Miller33804262001-02-04 23:20:18 +1100869 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000870 int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
Damien Miller33804262001-02-04 23:20:18 +1100871
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000872 if ((msg = sshbuf_new()) == NULL)
873 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100874
875 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100876 id = conn->msg_id++;
Damien Millerc7dba122013-08-21 02:41:15 +1000877 if (use_ext) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000878 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
879 (r = sshbuf_put_u32(msg, id)) != 0 ||
880 (r = sshbuf_put_cstring(msg,
881 "posix-rename@openssh.com")) != 0)
882 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7a3e1d02008-03-27 10:59:57 +1100883 } else {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000884 if ((r = sshbuf_put_u8(msg, SSH2_FXP_RENAME)) != 0 ||
885 (r = sshbuf_put_u32(msg, id)) != 0)
886 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7a3e1d02008-03-27 10:59:57 +1100887 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000888 if ((r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
889 (r = sshbuf_put_cstring(msg, newpath)) != 0)
890 fatal("%s: buffer error: %s", __func__, ssh_err(r));
891 send_msg(conn, msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100892 debug3("Sent message %s \"%s\" -> \"%s\"",
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000893 use_ext ? "posix-rename@openssh.com" :
894 "SSH2_FXP_RENAME", oldpath, newpath);
895 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100896
Damien Miller65e42f82010-09-24 22:15:11 +1000897 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100898 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100899 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
900 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100901
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000902 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100903}
904
905int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000906do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100907{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000908 struct sshbuf *msg;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100909 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000910 int r;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100911
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100912 if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
913 error("Server does not support hardlink@openssh.com extension");
914 return -1;
915 }
916
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000917 if ((msg = sshbuf_new()) == NULL)
918 fatal("%s: sshbuf_new failed", __func__);
Damien Miller3decdba2011-09-22 21:41:05 +1000919
920 /* Send link request */
921 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000922 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
923 (r = sshbuf_put_u32(msg, id)) != 0 ||
924 (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
925 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
926 (r = sshbuf_put_cstring(msg, newpath)) != 0)
927 fatal("%s: buffer error: %s", __func__, ssh_err(r));
928 send_msg(conn, msg);
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100929 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
930 oldpath, newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000931 sshbuf_free(msg);
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100932
933 status = get_status(conn, id);
934 if (status != SSH2_FX_OK)
935 error("Couldn't link file \"%s\" to \"%s\": %s", oldpath,
936 newpath, fx2txt(status));
937
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000938 return status == SSH2_FX_OK ? 0 : -1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100939}
940
941int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000942do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100943{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000944 struct sshbuf *msg;
Damien Miller058316f2001-03-08 10:08:49 +1100945 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000946 int r;
Damien Miller058316f2001-03-08 10:08:49 +1100947
Damien Miller3db5f532002-02-13 14:10:32 +1100948 if (conn->version < 3) {
949 error("This server does not support the symlink operation");
950 return(SSH2_FX_OP_UNSUPPORTED);
951 }
952
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000953 if ((msg = sshbuf_new()) == NULL)
954 fatal("%s: sshbuf_new failed", __func__);
Damien Miller058316f2001-03-08 10:08:49 +1100955
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000956 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100957 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000958 if ((r = sshbuf_put_u8(msg, SSH2_FXP_SYMLINK)) != 0 ||
959 (r = sshbuf_put_u32(msg, id)) != 0 ||
960 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
961 (r = sshbuf_put_cstring(msg, newpath)) != 0)
962 fatal("%s: buffer error: %s", __func__, ssh_err(r));
963 send_msg(conn, msg);
Damien Miller058316f2001-03-08 10:08:49 +1100964 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
965 newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000966 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +1100967
Damien Miller65e42f82010-09-24 22:15:11 +1000968 status = get_status(conn, id);
Damien Miller058316f2001-03-08 10:08:49 +1100969 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000970 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100971 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100972
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000973 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller058316f2001-03-08 10:08:49 +1100974}
975
Damien Millerf29238e2013-10-17 11:48:52 +1100976int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000977do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len)
Damien Millerf29238e2013-10-17 11:48:52 +1100978{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000979 struct sshbuf *msg;
Damien Millerf29238e2013-10-17 11:48:52 +1100980 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000981 int r;
Damien Millerf29238e2013-10-17 11:48:52 +1100982
983 /* Silently return if the extension is not supported */
984 if ((conn->exts & SFTP_EXT_FSYNC) == 0)
985 return -1;
986
Damien Millerf29238e2013-10-17 11:48:52 +1100987 /* Send fsync request */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000988 if ((msg = sshbuf_new()) == NULL)
989 fatal("%s: sshbuf_new failed", __func__);
Damien Millerf29238e2013-10-17 11:48:52 +1100990 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000991 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
992 (r = sshbuf_put_u32(msg, id)) != 0 ||
993 (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
994 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
995 fatal("%s: buffer error: %s", __func__, ssh_err(r));
996 send_msg(conn, msg);
Damien Millerf29238e2013-10-17 11:48:52 +1100997 debug3("Sent message fsync@openssh.com I:%u", id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000998 sshbuf_free(msg);
Damien Millerf29238e2013-10-17 11:48:52 +1100999
1000 status = get_status(conn, id);
1001 if (status != SSH2_FX_OK)
1002 error("Couldn't sync file: %s", fx2txt(status));
1003
1004 return status;
1005}
1006
Damien Millercfe23d32008-02-10 22:20:44 +11001007#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +11001008char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001009do_readlink(struct sftp_conn *conn, const char *path)
Damien Miller058316f2001-03-08 10:08:49 +11001010{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001011 struct sshbuf *msg;
1012 u_int expected_id, count, id;
Damien Miller058316f2001-03-08 10:08:49 +11001013 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001014 Attrib a;
1015 u_char type;
1016 int r;
Damien Miller058316f2001-03-08 10:08:49 +11001017
Damien Miller3db5f532002-02-13 14:10:32 +11001018 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +10001019 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +11001020
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001021 if ((msg = sshbuf_new()) == NULL)
1022 fatal("%s: sshbuf_new failed", __func__);
Damien Miller058316f2001-03-08 10:08:49 +11001023
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001024 get_msg(conn, msg);
1025 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1026 (r = sshbuf_get_u32(msg, &id)) != 0)
1027 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001028
1029 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001030 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +11001031
1032 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001033 u_int status;
Damien Miller058316f2001-03-08 10:08:49 +11001034
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001035 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1036 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001037 error("Couldn't readlink: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001038 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +11001039 return(NULL);
1040 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001041 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +11001042 SSH2_FXP_NAME, type);
1043
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001044 if ((r = sshbuf_get_u32(msg, &count)) != 0)
1045 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001046 if (count != 1)
1047 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
1048
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001049 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
1050 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
1051 (r = decode_attrib(msg, &a)) != 0)
1052 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001053
1054 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
1055
Darren Tuckera627d422013-06-02 07:31:17 +10001056 free(longname);
Damien Miller058316f2001-03-08 10:08:49 +11001057
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001058 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +11001059
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001060 return filename;
Damien Miller058316f2001-03-08 10:08:49 +11001061}
Damien Millercfe23d32008-02-10 22:20:44 +11001062#endif
Damien Miller058316f2001-03-08 10:08:49 +11001063
Damien Millerd671e5a2008-05-19 14:53:33 +10001064int
Darren Tucker7b598892008-06-09 22:49:36 +10001065do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
Damien Millerd671e5a2008-05-19 14:53:33 +10001066 int quiet)
1067{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001068 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +10001069 u_int id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001070 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +10001071
1072 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
1073 error("Server does not support statvfs@openssh.com extension");
1074 return -1;
1075 }
1076
1077 id = conn->msg_id++;
1078
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001079 if ((msg = sshbuf_new()) == NULL)
1080 fatal("%s: sshbuf_new failed", __func__);
1081 sshbuf_reset(msg);
1082 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1083 (r = sshbuf_put_u32(msg, id)) != 0 ||
1084 (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
1085 (r = sshbuf_put_cstring(msg, path)) != 0)
1086 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1087 send_msg(conn, msg);
1088 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +10001089
Damien Miller65e42f82010-09-24 22:15:11 +10001090 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +10001091}
1092
1093#ifdef notyet
1094int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001095do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
Darren Tucker7b598892008-06-09 22:49:36 +10001096 struct sftp_statvfs *st, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +10001097{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001098 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +10001099 u_int id;
1100
1101 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
1102 error("Server does not support fstatvfs@openssh.com extension");
1103 return -1;
1104 }
1105
1106 id = conn->msg_id++;
1107
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001108 if ((msg = sshbuf_new()) == NULL)
1109 fatal("%s: sshbuf_new failed", __func__);
1110 sshbuf_reset(msg);
1111 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1112 (r = sshbuf_put_u32(msg, id)) != 0 ||
1113 (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
1114 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1115 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1116 send_msg(conn, msg);
1117 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +10001118
Damien Miller65e42f82010-09-24 22:15:11 +10001119 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +10001120}
1121#endif
1122
Damien Miller16a13332002-02-13 14:03:56 +11001123static void
Damien Miller65e42f82010-09-24 22:15:11 +10001124send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001125 u_int len, const u_char *handle, u_int handle_len)
Damien Miller16a13332002-02-13 14:03:56 +11001126{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001127 struct sshbuf *msg;
1128 int r;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001129
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001130 if ((msg = sshbuf_new()) == NULL)
1131 fatal("%s: sshbuf_new failed", __func__);
1132 sshbuf_reset(msg);
1133 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READ)) != 0 ||
1134 (r = sshbuf_put_u32(msg, id)) != 0 ||
1135 (r = sshbuf_put_string(msg, handle, handle_len)) != 0 ||
1136 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1137 (r = sshbuf_put_u32(msg, len)) != 0)
1138 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1139 send_msg(conn, msg);
1140 sshbuf_free(msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001141}
Damien Miller16a13332002-02-13 14:03:56 +11001142
Damien Miller058316f2001-03-08 10:08:49 +11001143int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001144do_download(struct sftp_conn *conn, const char *remote_path,
1145 const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
1146 int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001147{
Darren Tucker1b0dd172009-10-07 08:37:48 +11001148 Attrib junk;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001149 struct sshbuf *msg;
1150 u_char *handle;
1151 int local_fd = -1, write_error;
1152 int read_error, write_errno, reordered = 0, r;
Damien Miller0d032412013-07-25 11:56:52 +10001153 u_int64_t offset = 0, size, highwater;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001154 u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
Damien Miller62d57f62003-01-10 21:43:24 +11001155 off_t progress_counter;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001156 size_t handle_len;
Damien Miller0d032412013-07-25 11:56:52 +10001157 struct stat st;
Damien Miller16a13332002-02-13 14:03:56 +11001158 struct request {
1159 u_int id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001160 size_t len;
Damien Miller16a13332002-02-13 14:03:56 +11001161 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001162 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +11001163 };
1164 TAILQ_HEAD(reqhead, request) requests;
1165 struct request *req;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001166 u_char type;
Damien Miller16a13332002-02-13 14:03:56 +11001167
1168 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +11001169
Darren Tucker1b0dd172009-10-07 08:37:48 +11001170 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1171 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001172
Damien Miller9e720282008-06-29 22:46:35 +10001173 /* Do not preserve set[ug]id here, as we do not preserve ownership */
Damien Miller33804262001-02-04 23:20:18 +11001174 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +11001175 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +11001176 else
1177 mode = 0666;
1178
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001179 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +11001180 (!S_ISREG(a->perm))) {
1181 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001182 return(-1);
1183 }
1184
Damien Miller16a13332002-02-13 14:03:56 +11001185 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1186 size = a->size;
1187 else
1188 size = 0;
1189
Damien Miller3db5f532002-02-13 14:10:32 +11001190 buflen = conn->transfer_buflen;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001191 if ((msg = sshbuf_new()) == NULL)
1192 fatal("%s: sshbuf_new failed", __func__);
1193
1194 attrib_clear(&junk); /* Send empty attributes */
Damien Miller33804262001-02-04 23:20:18 +11001195
1196 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001197 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001198 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1199 (r = sshbuf_put_u32(msg, id)) != 0 ||
1200 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1201 (r = sshbuf_put_u32(msg, SSH2_FXF_READ)) != 0 ||
1202 (r = encode_attrib(msg, &junk)) != 0)
1203 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1204 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001205 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001206
Damien Miller65e42f82010-09-24 22:15:11 +10001207 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001208 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001209 if (handle == NULL) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001210 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +11001211 return(-1);
1212 }
1213
Damien Millerbda5c842013-10-15 12:05:58 +11001214 local_fd = open(local_path,
1215 O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
Damien Miller3db5f532002-02-13 14:10:32 +11001216 if (local_fd == -1) {
1217 error("Couldn't open local file \"%s\" for writing: %s",
1218 local_path, strerror(errno));
Damien Miller0d032412013-07-25 11:56:52 +10001219 goto fail;
1220 }
1221 offset = highwater = 0;
Damien Millerbda5c842013-10-15 12:05:58 +11001222 if (resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001223 if (fstat(local_fd, &st) == -1) {
1224 error("Unable to stat local file \"%s\": %s",
1225 local_path, strerror(errno));
1226 goto fail;
1227 }
Damien Millerc3d483f2014-01-17 11:20:26 +11001228 if (st.st_size < 0) {
1229 error("\"%s\" has negative size", local_path);
1230 goto fail;
1231 }
1232 if ((u_int64_t)st.st_size > size) {
Damien Miller0d032412013-07-25 11:56:52 +10001233 error("Unable to resume download of \"%s\": "
1234 "local file is larger than remote", local_path);
1235 fail:
1236 do_close(conn, handle, handle_len);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001237 sshbuf_free(msg);
Damien Miller0d032412013-07-25 11:56:52 +10001238 free(handle);
Damien Miller9275df32013-12-05 10:26:32 +11001239 if (local_fd != -1)
1240 close(local_fd);
Damien Miller0d032412013-07-25 11:56:52 +10001241 return -1;
1242 }
1243 offset = highwater = st.st_size;
Damien Miller3db5f532002-02-13 14:10:32 +11001244 }
1245
Damien Miller33804262001-02-04 23:20:18 +11001246 /* Read from remote and write to local */
Damien Miller0d032412013-07-25 11:56:52 +10001247 write_error = read_error = write_errno = num_req = 0;
Damien Miller16a13332002-02-13 14:03:56 +11001248 max_req = 1;
Damien Miller0d032412013-07-25 11:56:52 +10001249 progress_counter = offset;
Damien Miller62d57f62003-01-10 21:43:24 +11001250
Damien Miller9ba30692004-03-08 23:12:02 +11001251 if (showprogress && size != 0)
1252 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001253
Damien Miller16a13332002-02-13 14:03:56 +11001254 while (num_req > 0 || max_req > 0) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001255 u_char *data;
1256 size_t len;
Damien Miller33804262001-02-04 23:20:18 +11001257
Darren Tuckercdf547a2004-05-24 10:12:19 +10001258 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001259 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001260 * allow outstanding requests to drain gracefully
1261 */
1262 if (interrupted) {
1263 if (num_req == 0) /* If we haven't started yet... */
1264 break;
1265 max_req = 0;
1266 }
1267
Damien Miller16a13332002-02-13 14:03:56 +11001268 /* Send some more requests */
1269 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001270 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +00001271 (unsigned long long)offset,
1272 (unsigned long long)offset + buflen - 1,
1273 num_req, max_req);
Damien Miller6c81fee2013-11-08 12:19:55 +11001274 req = xcalloc(1, sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +11001275 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001276 req->len = buflen;
1277 req->offset = offset;
1278 offset += buflen;
1279 num_req++;
1280 TAILQ_INSERT_TAIL(&requests, req, tq);
Damien Miller65e42f82010-09-24 22:15:11 +10001281 send_read_request(conn, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001282 req->len, handle, handle_len);
1283 }
Damien Miller33804262001-02-04 23:20:18 +11001284
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001285 sshbuf_reset(msg);
1286 get_msg(conn, msg);
1287 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1288 (r = sshbuf_get_u32(msg, &id)) != 0)
1289 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001290 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001291
Damien Miller16a13332002-02-13 14:03:56 +11001292 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001293 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001294 req != NULL && req->id != id;
1295 req = TAILQ_NEXT(req, tq))
1296 ;
1297 if (req == NULL)
1298 fatal("Unexpected reply %u", id);
1299
1300 switch (type) {
1301 case SSH2_FXP_STATUS:
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001302 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1303 fatal("%s: buffer error: %s",
1304 __func__, ssh_err(r));
Damien Miller16a13332002-02-13 14:03:56 +11001305 if (status != SSH2_FX_EOF)
1306 read_error = 1;
1307 max_req = 0;
1308 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001309 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001310 num_req--;
1311 break;
1312 case SSH2_FXP_DATA:
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001313 if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1314 fatal("%s: buffer error: %s",
1315 __func__, ssh_err(r));
Ben Lindstromeb505452002-03-22 01:03:15 +00001316 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001317 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001318 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001319 if (len > req->len)
1320 fatal("Received more data than asked for "
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001321 "%zu > %zu", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001322 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001323 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001324 !write_error) {
1325 write_errno = errno;
1326 write_error = 1;
1327 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001328 }
Damien Miller0d032412013-07-25 11:56:52 +10001329 else if (!reordered && req->offset <= highwater)
1330 highwater = req->offset + len;
1331 else if (!reordered && req->offset > highwater)
1332 reordered = 1;
Damien Miller62d57f62003-01-10 21:43:24 +11001333 progress_counter += len;
Darren Tuckera627d422013-06-02 07:31:17 +10001334 free(data);
Damien Miller16a13332002-02-13 14:03:56 +11001335
1336 if (len == req->len) {
1337 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001338 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001339 num_req--;
1340 } else {
1341 /* Resend the request for the missing data */
1342 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001343 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001344 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001345 (unsigned long long)req->offset +
1346 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001347 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001348 req->len -= len;
1349 req->offset += len;
Damien Miller65e42f82010-09-24 22:15:11 +10001350 send_read_request(conn, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001351 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001352 /* Reduce the request size */
1353 if (len < buflen)
1354 buflen = MAX(MIN_READ_SIZE, len);
1355 }
1356 if (max_req > 0) { /* max_req = 0 iff EOF received */
1357 if (size > 0 && offset > size) {
1358 /* Only one request at a time
1359 * after the expected EOF */
1360 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001361 (unsigned long long)offset,
1362 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001363 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001364 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001365 ++max_req;
1366 }
1367 }
1368 break;
1369 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001370 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001371 SSH2_FXP_DATA, type);
1372 }
Damien Miller33804262001-02-04 23:20:18 +11001373 }
Damien Miller33804262001-02-04 23:20:18 +11001374
Damien Miller62d57f62003-01-10 21:43:24 +11001375 if (showprogress && size)
1376 stop_progress_meter();
1377
Damien Miller16a13332002-02-13 14:03:56 +11001378 /* Sanity check */
1379 if (TAILQ_FIRST(&requests) != NULL)
1380 fatal("Transfer complete, but requests still in queue");
Damien Miller0d032412013-07-25 11:56:52 +10001381 /* Truncate at highest contiguous point to avoid holes on interrupt */
1382 if (read_error || write_error || interrupted) {
Damien Millerbda5c842013-10-15 12:05:58 +11001383 if (reordered && resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001384 error("Unable to resume download of \"%s\": "
1385 "server reordered requests", local_path);
1386 }
1387 debug("truncating at %llu", (unsigned long long)highwater);
djm@openbsd.org9cc68422015-05-28 04:50:53 +00001388 if (ftruncate(local_fd, highwater) == -1)
1389 error("ftruncate \"%s\": %s", local_path,
1390 strerror(errno));
Damien Miller0d032412013-07-25 11:56:52 +10001391 }
Damien Miller16a13332002-02-13 14:03:56 +11001392 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001393 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001394 remote_path, fx2txt(status));
Damien Millerfec029f2013-08-21 02:42:12 +10001395 status = -1;
Damien Miller3db5f532002-02-13 14:10:32 +11001396 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001397 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001398 error("Couldn't write to \"%s\": %s", local_path,
1399 strerror(write_errno));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001400 status = SSH2_FX_FAILURE;
Damien Miller3db5f532002-02-13 14:10:32 +11001401 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001402 } else {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001403 if (do_close(conn, handle, handle_len) != 0 || interrupted)
1404 status = SSH2_FX_FAILURE;
1405 else
1406 status = SSH2_FX_OK;
Damien Miller16a13332002-02-13 14:03:56 +11001407 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001408#ifdef HAVE_FCHMOD
Damien Millerbda5c842013-10-15 12:05:58 +11001409 if (preserve_flag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001410#else
Damien Millerbda5c842013-10-15 12:05:58 +11001411 if (preserve_flag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001412#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001413 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001414 strerror(errno));
Damien Millerbda5c842013-10-15 12:05:58 +11001415 if (preserve_flag &&
1416 (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
Damien Miller16a13332002-02-13 14:03:56 +11001417 struct timeval tv[2];
1418 tv[0].tv_sec = a->atime;
1419 tv[1].tv_sec = a->mtime;
1420 tv[0].tv_usec = tv[1].tv_usec = 0;
1421 if (utimes(local_path, tv) == -1)
1422 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001423 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001424 }
Damien Millerf29238e2013-10-17 11:48:52 +11001425 if (fsync_flag) {
1426 debug("syncing \"%s\"", local_path);
1427 if (fsync(local_fd) == -1)
1428 error("Couldn't sync file \"%s\": %s",
1429 local_path, strerror(errno));
1430 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001431 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001432 close(local_fd);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001433 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001434 free(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001435
1436 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001437}
1438
Darren Tucker1b0dd172009-10-07 08:37:48 +11001439static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001440download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1441 int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
1442 int resume_flag, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001443{
1444 int i, ret = 0;
1445 SFTP_DIRENT **dir_entries;
1446 char *filename, *new_src, *new_dst;
1447 mode_t mode = 0777;
1448
1449 if (depth >= MAX_DIR_DEPTH) {
1450 error("Maximum directory depth exceeded: %d levels", depth);
1451 return -1;
1452 }
1453
1454 if (dirattrib == NULL &&
1455 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1456 error("Unable to stat remote directory \"%s\"", src);
1457 return -1;
1458 }
1459 if (!S_ISDIR(dirattrib->perm)) {
1460 error("\"%s\" is not a directory", src);
1461 return -1;
1462 }
Damien Millerbda5c842013-10-15 12:05:58 +11001463 if (print_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001464 printf("Retrieving %s\n", src);
1465
1466 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1467 mode = dirattrib->perm & 01777;
1468 else {
1469 debug("Server did not send permissions for "
1470 "directory \"%s\"", dst);
1471 }
1472
1473 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1474 error("mkdir %s: %s", dst, strerror(errno));
1475 return -1;
1476 }
1477
1478 if (do_readdir(conn, src, &dir_entries) == -1) {
1479 error("%s: Failed to get directory contents", src);
1480 return -1;
1481 }
1482
1483 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1484 filename = dir_entries[i]->filename;
1485
1486 new_dst = path_append(dst, filename);
1487 new_src = path_append(src, filename);
1488
1489 if (S_ISDIR(dir_entries[i]->a.perm)) {
1490 if (strcmp(filename, ".") == 0 ||
1491 strcmp(filename, "..") == 0)
1492 continue;
1493 if (download_dir_internal(conn, new_src, new_dst,
Damien Millerbda5c842013-10-15 12:05:58 +11001494 depth + 1, &(dir_entries[i]->a), preserve_flag,
Damien Millerf29238e2013-10-17 11:48:52 +11001495 print_flag, resume_flag, fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001496 ret = -1;
1497 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1498 if (do_download(conn, new_src, new_dst,
Damien Millerf29238e2013-10-17 11:48:52 +11001499 &(dir_entries[i]->a), preserve_flag,
1500 resume_flag, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001501 error("Download of file %s to %s failed",
1502 new_src, new_dst);
1503 ret = -1;
1504 }
1505 } else
1506 logit("%s: not a regular file\n", new_src);
1507
Darren Tuckera627d422013-06-02 07:31:17 +10001508 free(new_dst);
1509 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001510 }
1511
Damien Millerbda5c842013-10-15 12:05:58 +11001512 if (preserve_flag) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001513 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1514 struct timeval tv[2];
1515 tv[0].tv_sec = dirattrib->atime;
1516 tv[1].tv_sec = dirattrib->mtime;
1517 tv[0].tv_usec = tv[1].tv_usec = 0;
1518 if (utimes(dst, tv) == -1)
1519 error("Can't set times on \"%s\": %s",
1520 dst, strerror(errno));
1521 } else
1522 debug("Server did not send times for directory "
1523 "\"%s\"", dst);
1524 }
1525
1526 free_sftp_dirents(dir_entries);
1527
1528 return ret;
1529}
1530
1531int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001532download_dir(struct sftp_conn *conn, const char *src, const char *dst,
1533 Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1534 int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001535{
1536 char *src_canon;
1537 int ret;
1538
1539 if ((src_canon = do_realpath(conn, src)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001540 error("Unable to canonicalize path \"%s\"", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001541 return -1;
1542 }
1543
Damien Millerbda5c842013-10-15 12:05:58 +11001544 ret = download_dir_internal(conn, src_canon, dst, 0,
Damien Millerf29238e2013-10-17 11:48:52 +11001545 dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag);
Darren Tuckera627d422013-06-02 07:31:17 +10001546 free(src_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001547 return ret;
1548}
1549
Damien Miller33804262001-02-04 23:20:18 +11001550int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001551do_upload(struct sftp_conn *conn, const char *local_path,
1552 const char *remote_path, int preserve_flag, int resume, int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001553{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001554 int r, local_fd;
1555 u_int status = SSH2_FX_OK;
1556 u_int id;
1557 u_char type;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001558 off_t offset, progress_counter;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001559 u_char *handle, *data;
1560 struct sshbuf *msg;
Damien Miller33804262001-02-04 23:20:18 +11001561 struct stat sb;
Damien Millerd8accc02014-05-15 13:46:25 +10001562 Attrib a, *c = NULL;
Damien Miller16a13332002-02-13 14:03:56 +11001563 u_int32_t startid;
1564 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001565 struct outstanding_ack {
1566 u_int id;
1567 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001568 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001569 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001570 };
1571 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001572 struct outstanding_ack *ack = NULL;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001573 size_t handle_len;
Damien Miller5873dfd2002-02-13 14:04:37 +11001574
1575 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001576
1577 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1578 error("Couldn't open local file \"%s\" for reading: %s",
1579 local_path, strerror(errno));
1580 return(-1);
1581 }
1582 if (fstat(local_fd, &sb) == -1) {
1583 error("Couldn't fstat local file \"%s\": %s",
1584 local_path, strerror(errno));
1585 close(local_fd);
1586 return(-1);
1587 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001588 if (!S_ISREG(sb.st_mode)) {
1589 error("%s is not a regular file", local_path);
1590 close(local_fd);
1591 return(-1);
1592 }
Damien Miller33804262001-02-04 23:20:18 +11001593 stat_to_attrib(&sb, &a);
1594
1595 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1596 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1597 a.perm &= 0777;
Damien Millerbda5c842013-10-15 12:05:58 +11001598 if (!preserve_flag)
Damien Miller33804262001-02-04 23:20:18 +11001599 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1600
Damien Millerd8accc02014-05-15 13:46:25 +10001601 if (resume) {
1602 /* Get remote file size if it exists */
1603 if ((c = do_stat(conn, remote_path, 0)) == NULL) {
1604 close(local_fd);
1605 return -1;
1606 }
1607
1608 if ((off_t)c->size >= sb.st_size) {
1609 error("destination file bigger or same size as "
1610 "source file");
1611 close(local_fd);
1612 return -1;
1613 }
1614
1615 if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
1616 close(local_fd);
1617 return -1;
1618 }
1619 }
1620
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001621 if ((msg = sshbuf_new()) == NULL)
1622 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001623
1624 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001625 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001626 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1627 (r = sshbuf_put_u32(msg, id)) != 0 ||
1628 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1629 (r = sshbuf_put_u32(msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|
1630 (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC))) != 0 ||
1631 (r = encode_attrib(msg, &a)) != 0)
1632 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1633 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001634 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001635
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001636 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +11001637
Damien Miller65e42f82010-09-24 22:15:11 +10001638 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001639 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001640 if (handle == NULL) {
1641 close(local_fd);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001642 sshbuf_free(msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001643 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001644 }
1645
Damien Miller16a13332002-02-13 14:03:56 +11001646 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001647 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001648
Damien Miller33804262001-02-04 23:20:18 +11001649 /* Read from local and write to remote */
Damien Millerd8accc02014-05-15 13:46:25 +10001650 offset = progress_counter = (resume ? c->size : 0);
Damien Miller62d57f62003-01-10 21:43:24 +11001651 if (showprogress)
Darren Tuckerc9a19912013-06-02 08:37:05 +10001652 start_progress_meter(local_path, sb.st_size,
1653 &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001654
Damien Miller9f0f5c62001-12-21 14:45:46 +11001655 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001656 int len;
Damien Miller33804262001-02-04 23:20:18 +11001657
1658 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001659 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001660 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001661 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001662 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001663 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001664 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001665 len = 0;
1666 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001667 len = read(local_fd, data, conn->transfer_buflen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001668 while ((len == -1) &&
1669 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
Damien Miller33804262001-02-04 23:20:18 +11001670
1671 if (len == -1)
1672 fatal("Couldn't read from \"%s\": %s", local_path,
1673 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001674
1675 if (len != 0) {
Damien Miller6c81fee2013-11-08 12:19:55 +11001676 ack = xcalloc(1, sizeof(*ack));
Damien Miller5873dfd2002-02-13 14:04:37 +11001677 ack->id = ++id;
1678 ack->offset = offset;
1679 ack->len = len;
1680 TAILQ_INSERT_TAIL(&acks, ack, tq);
1681
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001682 sshbuf_reset(msg);
1683 if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
1684 (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
1685 (r = sshbuf_put_string(msg, handle,
1686 handle_len)) != 0 ||
1687 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1688 (r = sshbuf_put_string(msg, data, len)) != 0)
1689 fatal("%s: buffer error: %s",
1690 __func__, ssh_err(r));
1691 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001692 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001693 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001694 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001695 break;
1696
Damien Miller5873dfd2002-02-13 14:04:37 +11001697 if (ack == NULL)
1698 fatal("Unexpected ACK %u", id);
1699
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001700 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001701 id - ackid >= conn->num_requests) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001702 u_int rid;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001703
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001704 sshbuf_reset(msg);
1705 get_msg(conn, msg);
1706 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1707 (r = sshbuf_get_u32(msg, &rid)) != 0)
1708 fatal("%s: buffer error: %s",
1709 __func__, ssh_err(r));
Damien Miller5873dfd2002-02-13 14:04:37 +11001710
1711 if (type != SSH2_FXP_STATUS)
1712 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1713 "got %d", SSH2_FXP_STATUS, type);
1714
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001715 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1716 fatal("%s: buffer error: %s",
1717 __func__, ssh_err(r));
1718 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller5873dfd2002-02-13 14:04:37 +11001719
1720 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001721 for (ack = TAILQ_FIRST(&acks);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001722 ack != NULL && ack->id != rid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001723 ack = TAILQ_NEXT(ack, tq))
1724 ;
1725 if (ack == NULL)
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001726 fatal("Can't find request for ID %u", rid);
Damien Miller5873dfd2002-02-13 14:04:37 +11001727 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001728 debug3("In write loop, ack for %u %u bytes at %lld",
1729 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001730 ++ackid;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001731 progress_counter += ack->len;
Darren Tuckera627d422013-06-02 07:31:17 +10001732 free(ack);
Damien Miller33804262001-02-04 23:20:18 +11001733 }
Damien Miller33804262001-02-04 23:20:18 +11001734 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001735 if (offset < 0)
1736 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001737 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001738 sshbuf_free(msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001739
Damien Miller62d57f62003-01-10 21:43:24 +11001740 if (showprogress)
1741 stop_progress_meter();
Darren Tuckera627d422013-06-02 07:31:17 +10001742 free(data);
Damien Miller33804262001-02-04 23:20:18 +11001743
Damien Milleracdf25b2008-02-10 22:27:24 +11001744 if (status != SSH2_FX_OK) {
1745 error("Couldn't write to remote file \"%s\": %s",
1746 remote_path, fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001747 status = SSH2_FX_FAILURE;
Damien Milleracdf25b2008-02-10 22:27:24 +11001748 }
1749
Damien Miller33804262001-02-04 23:20:18 +11001750 if (close(local_fd) == -1) {
1751 error("Couldn't close local file \"%s\": %s", local_path,
1752 strerror(errno));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001753 status = SSH2_FX_FAILURE;
Damien Miller33804262001-02-04 23:20:18 +11001754 }
1755
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001756 /* Override umask and utimes if asked */
Damien Millerbda5c842013-10-15 12:05:58 +11001757 if (preserve_flag)
Damien Miller3db5f532002-02-13 14:10:32 +11001758 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001759
Damien Millerf29238e2013-10-17 11:48:52 +11001760 if (fsync_flag)
1761 (void)do_fsync(conn, handle, handle_len);
1762
Damien Milleracdf25b2008-02-10 22:27:24 +11001763 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001764 status = SSH2_FX_FAILURE;
1765
Darren Tuckera627d422013-06-02 07:31:17 +10001766 free(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001767
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001768 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +11001769}
Darren Tucker1b0dd172009-10-07 08:37:48 +11001770
1771static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001772upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1773 int depth, int preserve_flag, int print_flag, int resume, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001774{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001775 int ret = 0;
1776 u_int status;
Darren Tucker1b0dd172009-10-07 08:37:48 +11001777 DIR *dirp;
1778 struct dirent *dp;
1779 char *filename, *new_src, *new_dst;
1780 struct stat sb;
1781 Attrib a;
1782
1783 if (depth >= MAX_DIR_DEPTH) {
1784 error("Maximum directory depth exceeded: %d levels", depth);
1785 return -1;
1786 }
1787
1788 if (stat(src, &sb) == -1) {
1789 error("Couldn't stat directory \"%s\": %s",
1790 src, strerror(errno));
1791 return -1;
1792 }
1793 if (!S_ISDIR(sb.st_mode)) {
1794 error("\"%s\" is not a directory", src);
1795 return -1;
1796 }
Damien Millerbda5c842013-10-15 12:05:58 +11001797 if (print_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001798 printf("Entering %s\n", src);
1799
1800 attrib_clear(&a);
1801 stat_to_attrib(&sb, &a);
1802 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1803 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1804 a.perm &= 01777;
Damien Millerbda5c842013-10-15 12:05:58 +11001805 if (!preserve_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001806 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
Damien Miller0d032412013-07-25 11:56:52 +10001807
Darren Tucker1b0dd172009-10-07 08:37:48 +11001808 status = do_mkdir(conn, dst, &a, 0);
1809 /*
1810 * we lack a portable status for errno EEXIST,
1811 * so if we get a SSH2_FX_FAILURE back we must check
1812 * if it was created successfully.
1813 */
1814 if (status != SSH2_FX_OK) {
1815 if (status != SSH2_FX_FAILURE)
1816 return -1;
Damien Miller0d032412013-07-25 11:56:52 +10001817 if (do_stat(conn, dst, 0) == NULL)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001818 return -1;
1819 }
1820
1821 if ((dirp = opendir(src)) == NULL) {
1822 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1823 return -1;
1824 }
Damien Miller0d032412013-07-25 11:56:52 +10001825
Darren Tucker1b0dd172009-10-07 08:37:48 +11001826 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1827 if (dp->d_ino == 0)
1828 continue;
1829 filename = dp->d_name;
1830 new_dst = path_append(dst, filename);
1831 new_src = path_append(src, filename);
1832
Darren Tucker438b4732009-10-11 21:52:10 +11001833 if (lstat(new_src, &sb) == -1) {
1834 logit("%s: lstat failed: %s", filename,
1835 strerror(errno));
1836 ret = -1;
1837 } else if (S_ISDIR(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001838 if (strcmp(filename, ".") == 0 ||
1839 strcmp(filename, "..") == 0)
1840 continue;
1841
1842 if (upload_dir_internal(conn, new_src, new_dst,
Damien Millerd8accc02014-05-15 13:46:25 +10001843 depth + 1, preserve_flag, print_flag, resume,
Damien Millerf29238e2013-10-17 11:48:52 +11001844 fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001845 ret = -1;
Darren Tucker438b4732009-10-11 21:52:10 +11001846 } else if (S_ISREG(sb.st_mode)) {
Damien Millerbda5c842013-10-15 12:05:58 +11001847 if (do_upload(conn, new_src, new_dst,
Damien Millerd8accc02014-05-15 13:46:25 +10001848 preserve_flag, resume, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001849 error("Uploading of file %s to %s failed!",
1850 new_src, new_dst);
1851 ret = -1;
1852 }
1853 } else
1854 logit("%s: not a regular file\n", filename);
Darren Tuckera627d422013-06-02 07:31:17 +10001855 free(new_dst);
1856 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001857 }
1858
1859 do_setstat(conn, dst, &a);
1860
1861 (void) closedir(dirp);
1862 return ret;
1863}
1864
1865int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001866upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
1867 int preserve_flag, int print_flag, int resume, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001868{
1869 char *dst_canon;
1870 int ret;
1871
1872 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001873 error("Unable to canonicalize path \"%s\"", dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001874 return -1;
1875 }
1876
Damien Miller2f93d052013-10-15 12:06:27 +11001877 ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
Damien Millerd8accc02014-05-15 13:46:25 +10001878 print_flag, resume, fsync_flag);
Damien Millerf29238e2013-10-17 11:48:52 +11001879
Darren Tuckera627d422013-06-02 07:31:17 +10001880 free(dst_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001881 return ret;
1882}
1883
1884char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001885path_append(const char *p1, const char *p2)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001886{
1887 char *ret;
1888 size_t len = strlen(p1) + strlen(p2) + 2;
1889
1890 ret = xmalloc(len);
1891 strlcpy(ret, p1, len);
1892 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1893 strlcat(ret, "/", len);
1894 strlcat(ret, p2, len);
1895
1896 return(ret);
1897}
1898