blob: 80f4805cb125152ad0a8ce27a143c7a62e76bff9 [file] [log] [blame]
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001/* $OpenBSD: sftp-client.c,v 1.117 2015/01/20 23:14:00 deraadt 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);
Damien Miller3db5f532002-02-13 14:10:32 +1100411 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100412 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000413 if ((r = sshbuf_get_u32(msg, &ret->version)) != 0)
414 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100415
Damien Miller65e42f82010-09-24 22:15:11 +1000416 debug2("Remote version: %u", ret->version);
Damien Miller33804262001-02-04 23:20:18 +1100417
418 /* Check for extensions */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000419 while (sshbuf_len(msg) > 0) {
420 char *name;
421 u_char *value;
422 size_t vlen;
Darren Tuckera64ab332008-06-13 07:01:29 +1000423 int known = 0;
Damien Miller33804262001-02-04 23:20:18 +1100424
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000425 if ((r = sshbuf_get_cstring(msg, &name, NULL)) != 0 ||
426 (r = sshbuf_get_string(msg, &value, &vlen)) != 0)
427 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +1000428 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000429 strcmp((char *)value, "1") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000430 ret->exts |= SFTP_EXT_POSIX_RENAME;
Darren Tuckera64ab332008-06-13 07:01:29 +1000431 known = 1;
432 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000433 strcmp((char *)value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000434 ret->exts |= SFTP_EXT_STATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000435 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100436 } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000437 strcmp((char *)value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000438 ret->exts |= SFTP_EXT_FSTATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000439 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100440 } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000441 strcmp((char *)value, "1") == 0) {
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100442 ret->exts |= SFTP_EXT_HARDLINK;
443 known = 1;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000444 } else if (strcmp(name, "fsync@openssh.com") == 0 &&
445 strcmp((char *)value, "1") == 0) {
446 ret->exts |= SFTP_EXT_FSYNC;
447 known = 1;
Darren Tuckera64ab332008-06-13 07:01:29 +1000448 }
449 if (known) {
450 debug2("Server supports extension \"%s\" revision %s",
451 name, value);
452 } else {
453 debug2("Unrecognised server extension \"%s\"", name);
454 }
Darren Tuckera627d422013-06-02 07:31:17 +1000455 free(name);
456 free(value);
Damien Miller33804262001-02-04 23:20:18 +1100457 }
458
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000459 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +1100460
Damien Miller3db5f532002-02-13 14:10:32 +1100461 /* Some filexfer v.0 servers don't support large packets */
Damien Miller65e42f82010-09-24 22:15:11 +1000462 if (ret->version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000463 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100464
Damien Miller65e42f82010-09-24 22:15:11 +1000465 ret->limit_kbps = limit_kbps;
466 if (ret->limit_kbps > 0) {
467 bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
468 ret->transfer_buflen);
469 bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
470 ret->transfer_buflen);
471 }
472
473 return ret;
Damien Miller3db5f532002-02-13 14:10:32 +1100474}
475
476u_int
477sftp_proto_version(struct sftp_conn *conn)
478{
Damien Miller65e42f82010-09-24 22:15:11 +1000479 return conn->version;
Damien Miller33804262001-02-04 23:20:18 +1100480}
481
482int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000483do_close(struct sftp_conn *conn, const u_char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100484{
485 u_int id, status;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000486 struct sshbuf *msg;
487 int r;
Damien Miller33804262001-02-04 23:20:18 +1100488
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000489 if ((msg = sshbuf_new()) == NULL)
490 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100491
Damien Miller3db5f532002-02-13 14:10:32 +1100492 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000493 if ((r = sshbuf_put_u8(msg, SSH2_FXP_CLOSE)) != 0 ||
494 (r = sshbuf_put_u32(msg, id)) != 0 ||
495 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
496 fatal("%s: buffer error: %s", __func__, ssh_err(r));
497 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000498 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100499
Damien Miller65e42f82010-09-24 22:15:11 +1000500 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100501 if (status != SSH2_FX_OK)
502 error("Couldn't close file: %s", fx2txt(status));
503
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000504 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100505
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000506 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100507}
508
Damien Miller4870afd2001-03-14 10:27:09 +1100509
Ben Lindstrombba81212001-06-25 05:01:22 +0000510static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000511do_lsreaddir(struct sftp_conn *conn, const char *path, int print_flag,
Damien Miller4870afd2001-03-14 10:27:09 +1100512 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100513{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000514 struct sshbuf *msg;
515 u_int count, id, i, expected_id, ents = 0;
516 size_t handle_len;
517 u_char type;
Damien Miller33804262001-02-04 23:20:18 +1100518 char *handle;
Damien Miller39392072013-12-07 10:31:08 +1100519 int status = SSH2_FX_FAILURE;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000520 int r;
Damien Miller39392072013-12-07 10:31:08 +1100521
522 if (dir)
523 *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +1100524
Damien Miller3db5f532002-02-13 14:10:32 +1100525 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100526
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000527 if ((msg = sshbuf_new()) == NULL)
528 fatal("%s: sshbuf_new failed", __func__);
529 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPENDIR)) != 0 ||
530 (r = sshbuf_put_u32(msg, id)) != 0 ||
531 (r = sshbuf_put_cstring(msg, path)) != 0)
532 fatal("%s: buffer error: %s", __func__, ssh_err(r));
533 send_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100534
Damien Miller65e42f82010-09-24 22:15:11 +1000535 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +1100536 "remote readdir(\"%s\")", path);
Damien Miller57c38ac2011-09-22 21:42:45 +1000537 if (handle == NULL) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000538 sshbuf_free(msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000539 return -1;
Damien Miller57c38ac2011-09-22 21:42:45 +1000540 }
Damien Miller33804262001-02-04 23:20:18 +1100541
Damien Miller4870afd2001-03-14 10:27:09 +1100542 if (dir) {
543 ents = 0;
Damien Miller6c81fee2013-11-08 12:19:55 +1100544 *dir = xcalloc(1, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100545 (*dir)[0] = NULL;
546 }
Damien Miller4870afd2001-03-14 10:27:09 +1100547
Darren Tuckercdf547a2004-05-24 10:12:19 +1000548 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100549 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100550
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000551 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100552
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000553 sshbuf_reset(msg);
554 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READDIR)) != 0 ||
555 (r = sshbuf_put_u32(msg, id)) != 0 ||
556 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
557 fatal("%s: buffer error: %s", __func__, ssh_err(r));
558 send_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100559
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000560 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +1100561
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000562 get_msg(conn, msg);
Damien Miller33804262001-02-04 23:20:18 +1100563
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000564 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
565 (r = sshbuf_get_u32(msg, &id)) != 0)
566 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100567
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000568 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100569
570 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000571 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100572
573 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000574 u_int rstatus;
575
576 if ((r = sshbuf_get_u32(msg, &rstatus)) != 0)
577 fatal("%s: buffer error: %s",
578 __func__, ssh_err(r));
579 debug3("Received SSH2_FXP_STATUS %d", rstatus);
580 if (rstatus == SSH2_FX_EOF)
Damien Miller33804262001-02-04 23:20:18 +1100581 break;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000582 error("Couldn't read directory: %s", fx2txt(rstatus));
Damien Miller39392072013-12-07 10:31:08 +1100583 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100584 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000585 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100586 SSH2_FXP_NAME, type);
587
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000588 if ((r = sshbuf_get_u32(msg, &count)) != 0)
589 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd7686fd2001-02-10 00:40:03 +1100590 if (count == 0)
591 break;
592 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100593 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100594 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000595 Attrib a;
Damien Miller33804262001-02-04 23:20:18 +1100596
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000597 if ((r = sshbuf_get_cstring(msg, &filename,
598 NULL)) != 0 ||
599 (r = sshbuf_get_cstring(msg, &longname,
600 NULL)) != 0)
601 fatal("%s: buffer error: %s",
602 __func__, ssh_err(r));
603 if ((r = decode_attrib(msg, &a)) != 0) {
604 error("%s: couldn't decode attrib: %s",
605 __func__, ssh_err(r));
606 free(filename);
607 free(longname);
608 sshbuf_free(msg);
609 return -1;
610 }
Damien Miller33804262001-02-04 23:20:18 +1100611
Damien Millerbda5c842013-10-15 12:05:58 +1100612 if (print_flag)
Damien Miller4870afd2001-03-14 10:27:09 +1100613 printf("%s\n", longname);
614
Darren Tucker1b0dd172009-10-07 08:37:48 +1100615 /*
616 * Directory entries should never contain '/'
617 * These can be used to attack recursive ops
618 * (e.g. send '../../../../etc/passwd')
619 */
620 if (strchr(filename, '/') != NULL) {
621 error("Server sent suspect path \"%s\" "
622 "during readdir of \"%s\"", filename, path);
Damien Miller39392072013-12-07 10:31:08 +1100623 } else if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100624 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller6c81fee2013-11-08 12:19:55 +1100625 (*dir)[ents] = xcalloc(1, sizeof(***dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100626 (*dir)[ents]->filename = xstrdup(filename);
627 (*dir)[ents]->longname = xstrdup(longname);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000628 memcpy(&(*dir)[ents]->a, &a, sizeof(a));
Damien Miller4870afd2001-03-14 10:27:09 +1100629 (*dir)[++ents] = NULL;
630 }
Darren Tuckera627d422013-06-02 07:31:17 +1000631 free(filename);
632 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100633 }
634 }
Damien Miller39392072013-12-07 10:31:08 +1100635 status = 0;
Damien Miller33804262001-02-04 23:20:18 +1100636
Damien Miller39392072013-12-07 10:31:08 +1100637 out:
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000638 sshbuf_free(msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100639 do_close(conn, handle, handle_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000640 free(handle);
Damien Miller33804262001-02-04 23:20:18 +1100641
Damien Miller39392072013-12-07 10:31:08 +1100642 if (status != 0 && dir != NULL) {
643 /* Don't return results on error */
644 free_sftp_dirents(*dir);
645 *dir = NULL;
646 } else if (interrupted && dir != NULL && *dir != NULL) {
647 /* Don't return partial matches on interrupt */
Darren Tuckercdf547a2004-05-24 10:12:19 +1000648 free_sftp_dirents(*dir);
Damien Miller6c81fee2013-11-08 12:19:55 +1100649 *dir = xcalloc(1, sizeof(**dir));
Darren Tuckercdf547a2004-05-24 10:12:19 +1000650 **dir = NULL;
651 }
652
Damien Miller39392072013-12-07 10:31:08 +1100653 return status;
Damien Miller33804262001-02-04 23:20:18 +1100654}
655
656int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000657do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100658{
Damien Miller3db5f532002-02-13 14:10:32 +1100659 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100660}
661
662void free_sftp_dirents(SFTP_DIRENT **s)
663{
664 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100665
Damien Miller39392072013-12-07 10:31:08 +1100666 if (s == NULL)
667 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100668 for (i = 0; s[i]; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000669 free(s[i]->filename);
670 free(s[i]->longname);
671 free(s[i]);
Damien Miller4870afd2001-03-14 10:27:09 +1100672 }
Darren Tuckera627d422013-06-02 07:31:17 +1000673 free(s);
Damien Miller4870afd2001-03-14 10:27:09 +1100674}
675
676int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000677do_rm(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100678{
679 u_int status, id;
680
681 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
682
Damien Miller3db5f532002-02-13 14:10:32 +1100683 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000684 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
685 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100686 if (status != SSH2_FX_OK)
687 error("Couldn't delete file: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000688 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100689}
690
691int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000692do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100693{
694 u_int status, id;
695
Damien Miller3db5f532002-02-13 14:10:32 +1100696 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000697 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100698 strlen(path), a);
699
Damien Miller65e42f82010-09-24 22:15:11 +1000700 status = get_status(conn, id);
Damien Millerbda5c842013-10-15 12:05:58 +1100701 if (status != SSH2_FX_OK && print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100702 error("Couldn't create directory: %s", fx2txt(status));
703
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000704 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100705}
706
707int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000708do_rmdir(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100709{
710 u_int status, id;
711
Damien Miller3db5f532002-02-13 14:10:32 +1100712 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000713 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100714 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100715
Damien Miller65e42f82010-09-24 22:15:11 +1000716 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100717 if (status != SSH2_FX_OK)
718 error("Couldn't remove directory: %s", fx2txt(status));
719
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000720 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100721}
722
723Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000724do_stat(struct sftp_conn *conn, const char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100725{
726 u_int id;
727
Damien Miller3db5f532002-02-13 14:10:32 +1100728 id = conn->msg_id++;
729
Damien Miller65e42f82010-09-24 22:15:11 +1000730 send_string_request(conn, id,
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000731 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100732 path, strlen(path));
733
Damien Miller65e42f82010-09-24 22:15:11 +1000734 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100735}
736
737Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000738do_lstat(struct sftp_conn *conn, const char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100739{
740 u_int id;
741
Damien Miller3db5f532002-02-13 14:10:32 +1100742 if (conn->version == 0) {
743 if (quiet)
744 debug("Server version does not support lstat operation");
745 else
Damien Miller996acd22003-04-09 20:59:48 +1000746 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000747 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100748 }
749
750 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000751 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100752 strlen(path));
753
Damien Miller65e42f82010-09-24 22:15:11 +1000754 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100755}
756
Damien Millercfe23d32008-02-10 22:20:44 +1100757#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100758Attrib *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000759do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
760 int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100761{
762 u_int id;
763
Damien Miller3db5f532002-02-13 14:10:32 +1100764 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000765 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
Damien Miller3db5f532002-02-13 14:10:32 +1100766 handle_len);
767
Damien Miller65e42f82010-09-24 22:15:11 +1000768 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100769}
Damien Millercfe23d32008-02-10 22:20:44 +1100770#endif
Damien Miller33804262001-02-04 23:20:18 +1100771
772int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000773do_setstat(struct sftp_conn *conn, const char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100774{
775 u_int status, id;
776
Damien Miller3db5f532002-02-13 14:10:32 +1100777 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000778 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100779 strlen(path), a);
780
Damien Miller65e42f82010-09-24 22:15:11 +1000781 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100782 if (status != SSH2_FX_OK)
783 error("Couldn't setstat on \"%s\": %s", path,
784 fx2txt(status));
785
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000786 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100787}
788
789int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000790do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100791 Attrib *a)
792{
793 u_int status, id;
794
Damien Miller3db5f532002-02-13 14:10:32 +1100795 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000796 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100797 handle_len, a);
798
Damien Miller65e42f82010-09-24 22:15:11 +1000799 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100800 if (status != SSH2_FX_OK)
801 error("Couldn't fsetstat: %s", fx2txt(status));
802
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000803 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100804}
805
806char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000807do_realpath(struct sftp_conn *conn, const char *path)
Damien Miller33804262001-02-04 23:20:18 +1100808{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000809 struct sshbuf *msg;
810 u_int expected_id, count, id;
Damien Miller33804262001-02-04 23:20:18 +1100811 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000812 Attrib a;
813 u_char type;
814 int r;
Damien Miller33804262001-02-04 23:20:18 +1100815
Damien Miller3db5f532002-02-13 14:10:32 +1100816 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000817 send_string_request(conn, id, SSH2_FXP_REALPATH, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100818 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100819
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000820 if ((msg = sshbuf_new()) == NULL)
821 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100822
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000823 get_msg(conn, msg);
824 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
825 (r = sshbuf_get_u32(msg, &id)) != 0)
826 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100827
828 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000829 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100830
831 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000832 u_int status;
Damien Miller33804262001-02-04 23:20:18 +1100833
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000834 if ((r = sshbuf_get_u32(msg, &status)) != 0)
835 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf29238e2013-10-17 11:48:52 +1100836 error("Couldn't canonicalize: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000837 sshbuf_free(msg);
Damien Miller49566312010-06-26 09:38:23 +1000838 return NULL;
Damien Miller33804262001-02-04 23:20:18 +1100839 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000840 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100841 SSH2_FXP_NAME, type);
842
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000843 if ((r = sshbuf_get_u32(msg, &count)) != 0)
844 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100845 if (count != 1)
846 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
847
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000848 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
849 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
850 (r = decode_attrib(msg, &a)) != 0)
851 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100852
Darren Tucker4908d442012-07-02 22:15:38 +1000853 debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000854 (unsigned long)a.size);
Damien Miller33804262001-02-04 23:20:18 +1100855
Darren Tuckera627d422013-06-02 07:31:17 +1000856 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100857
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000858 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100859
860 return(filename);
861}
862
863int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000864do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath,
Damien Millerc7dba122013-08-21 02:41:15 +1000865 int force_legacy)
Damien Miller33804262001-02-04 23:20:18 +1100866{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000867 struct sshbuf *msg;
Damien Miller33804262001-02-04 23:20:18 +1100868 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000869 int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
Damien Miller33804262001-02-04 23:20:18 +1100870
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000871 if ((msg = sshbuf_new()) == NULL)
872 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +1100873
874 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100875 id = conn->msg_id++;
Damien Millerc7dba122013-08-21 02:41:15 +1000876 if (use_ext) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000877 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
878 (r = sshbuf_put_u32(msg, id)) != 0 ||
879 (r = sshbuf_put_cstring(msg,
880 "posix-rename@openssh.com")) != 0)
881 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7a3e1d02008-03-27 10:59:57 +1100882 } else {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000883 if ((r = sshbuf_put_u8(msg, SSH2_FXP_RENAME)) != 0 ||
884 (r = sshbuf_put_u32(msg, id)) != 0)
885 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7a3e1d02008-03-27 10:59:57 +1100886 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000887 if ((r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
888 (r = sshbuf_put_cstring(msg, newpath)) != 0)
889 fatal("%s: buffer error: %s", __func__, ssh_err(r));
890 send_msg(conn, msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100891 debug3("Sent message %s \"%s\" -> \"%s\"",
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000892 use_ext ? "posix-rename@openssh.com" :
893 "SSH2_FXP_RENAME", oldpath, newpath);
894 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +1100895
Damien Miller65e42f82010-09-24 22:15:11 +1000896 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100897 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100898 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
899 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100900
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000901 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +1100902}
903
904int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000905do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100906{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000907 struct sshbuf *msg;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100908 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000909 int r;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100910
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100911 if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
912 error("Server does not support hardlink@openssh.com extension");
913 return -1;
914 }
915
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000916 if ((msg = sshbuf_new()) == NULL)
917 fatal("%s: sshbuf_new failed", __func__);
Damien Miller3decdba2011-09-22 21:41:05 +1000918
919 /* Send link request */
920 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000921 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
922 (r = sshbuf_put_u32(msg, id)) != 0 ||
923 (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
924 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
925 (r = sshbuf_put_cstring(msg, newpath)) != 0)
926 fatal("%s: buffer error: %s", __func__, ssh_err(r));
927 send_msg(conn, msg);
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100928 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
929 oldpath, newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000930 sshbuf_free(msg);
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100931
932 status = get_status(conn, id);
933 if (status != SSH2_FX_OK)
934 error("Couldn't link file \"%s\" to \"%s\": %s", oldpath,
935 newpath, fx2txt(status));
936
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000937 return status == SSH2_FX_OK ? 0 : -1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100938}
939
940int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000941do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100942{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000943 struct sshbuf *msg;
Damien Miller058316f2001-03-08 10:08:49 +1100944 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000945 int r;
Damien Miller058316f2001-03-08 10:08:49 +1100946
Damien Miller3db5f532002-02-13 14:10:32 +1100947 if (conn->version < 3) {
948 error("This server does not support the symlink operation");
949 return(SSH2_FX_OP_UNSUPPORTED);
950 }
951
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000952 if ((msg = sshbuf_new()) == NULL)
953 fatal("%s: sshbuf_new failed", __func__);
Damien Miller058316f2001-03-08 10:08:49 +1100954
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000955 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100956 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000957 if ((r = sshbuf_put_u8(msg, SSH2_FXP_SYMLINK)) != 0 ||
958 (r = sshbuf_put_u32(msg, id)) != 0 ||
959 (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
960 (r = sshbuf_put_cstring(msg, newpath)) != 0)
961 fatal("%s: buffer error: %s", __func__, ssh_err(r));
962 send_msg(conn, msg);
Damien Miller058316f2001-03-08 10:08:49 +1100963 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
964 newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000965 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +1100966
Damien Miller65e42f82010-09-24 22:15:11 +1000967 status = get_status(conn, id);
Damien Miller058316f2001-03-08 10:08:49 +1100968 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000969 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100970 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100971
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000972 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller058316f2001-03-08 10:08:49 +1100973}
974
Damien Millerf29238e2013-10-17 11:48:52 +1100975int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000976do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len)
Damien Millerf29238e2013-10-17 11:48:52 +1100977{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000978 struct sshbuf *msg;
Damien Millerf29238e2013-10-17 11:48:52 +1100979 u_int status, id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000980 int r;
Damien Millerf29238e2013-10-17 11:48:52 +1100981
982 /* Silently return if the extension is not supported */
983 if ((conn->exts & SFTP_EXT_FSYNC) == 0)
984 return -1;
985
Damien Millerf29238e2013-10-17 11:48:52 +1100986 /* Send fsync request */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000987 if ((msg = sshbuf_new()) == NULL)
988 fatal("%s: sshbuf_new failed", __func__);
Damien Millerf29238e2013-10-17 11:48:52 +1100989 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000990 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
991 (r = sshbuf_put_u32(msg, id)) != 0 ||
992 (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
993 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
994 fatal("%s: buffer error: %s", __func__, ssh_err(r));
995 send_msg(conn, msg);
Damien Millerf29238e2013-10-17 11:48:52 +1100996 debug3("Sent message fsync@openssh.com I:%u", id);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000997 sshbuf_free(msg);
Damien Millerf29238e2013-10-17 11:48:52 +1100998
999 status = get_status(conn, id);
1000 if (status != SSH2_FX_OK)
1001 error("Couldn't sync file: %s", fx2txt(status));
1002
1003 return status;
1004}
1005
Damien Millercfe23d32008-02-10 22:20:44 +11001006#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +11001007char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001008do_readlink(struct sftp_conn *conn, const char *path)
Damien Miller058316f2001-03-08 10:08:49 +11001009{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001010 struct sshbuf *msg;
1011 u_int expected_id, count, id;
Damien Miller058316f2001-03-08 10:08:49 +11001012 char *filename, *longname;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001013 Attrib a;
1014 u_char type;
1015 int r;
Damien Miller058316f2001-03-08 10:08:49 +11001016
Damien Miller3db5f532002-02-13 14:10:32 +11001017 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +10001018 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +11001019
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001020 if ((msg = sshbuf_new()) == NULL)
1021 fatal("%s: sshbuf_new failed", __func__);
Damien Miller058316f2001-03-08 10:08:49 +11001022
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001023 get_msg(conn, msg);
1024 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1025 (r = sshbuf_get_u32(msg, &id)) != 0)
1026 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001027
1028 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001029 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +11001030
1031 if (type == SSH2_FXP_STATUS) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001032 u_int status;
Damien Miller058316f2001-03-08 10:08:49 +11001033
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001034 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1035 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001036 error("Couldn't readlink: %s", fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001037 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +11001038 return(NULL);
1039 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001040 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +11001041 SSH2_FXP_NAME, type);
1042
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001043 if ((r = sshbuf_get_u32(msg, &count)) != 0)
1044 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001045 if (count != 1)
1046 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
1047
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001048 if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
1049 (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
1050 (r = decode_attrib(msg, &a)) != 0)
1051 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +11001052
1053 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
1054
Darren Tuckera627d422013-06-02 07:31:17 +10001055 free(longname);
Damien Miller058316f2001-03-08 10:08:49 +11001056
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001057 sshbuf_free(msg);
Damien Miller058316f2001-03-08 10:08:49 +11001058
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001059 return filename;
Damien Miller058316f2001-03-08 10:08:49 +11001060}
Damien Millercfe23d32008-02-10 22:20:44 +11001061#endif
Damien Miller058316f2001-03-08 10:08:49 +11001062
Damien Millerd671e5a2008-05-19 14:53:33 +10001063int
Darren Tucker7b598892008-06-09 22:49:36 +10001064do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
Damien Millerd671e5a2008-05-19 14:53:33 +10001065 int quiet)
1066{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001067 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +10001068 u_int id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001069 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +10001070
1071 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
1072 error("Server does not support statvfs@openssh.com extension");
1073 return -1;
1074 }
1075
1076 id = conn->msg_id++;
1077
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001078 if ((msg = sshbuf_new()) == NULL)
1079 fatal("%s: sshbuf_new failed", __func__);
1080 sshbuf_reset(msg);
1081 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1082 (r = sshbuf_put_u32(msg, id)) != 0 ||
1083 (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
1084 (r = sshbuf_put_cstring(msg, path)) != 0)
1085 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1086 send_msg(conn, msg);
1087 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +10001088
Damien Miller65e42f82010-09-24 22:15:11 +10001089 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +10001090}
1091
1092#ifdef notyet
1093int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001094do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
Darren Tucker7b598892008-06-09 22:49:36 +10001095 struct sftp_statvfs *st, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +10001096{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001097 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +10001098 u_int id;
1099
1100 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
1101 error("Server does not support fstatvfs@openssh.com extension");
1102 return -1;
1103 }
1104
1105 id = conn->msg_id++;
1106
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001107 if ((msg = sshbuf_new()) == NULL)
1108 fatal("%s: sshbuf_new failed", __func__);
1109 sshbuf_reset(msg);
1110 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1111 (r = sshbuf_put_u32(msg, id)) != 0 ||
1112 (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
1113 (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1114 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1115 send_msg(conn, msg);
1116 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +10001117
Damien Miller65e42f82010-09-24 22:15:11 +10001118 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +10001119}
1120#endif
1121
Damien Miller16a13332002-02-13 14:03:56 +11001122static void
Damien Miller65e42f82010-09-24 22:15:11 +10001123send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001124 u_int len, const u_char *handle, u_int handle_len)
Damien Miller16a13332002-02-13 14:03:56 +11001125{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001126 struct sshbuf *msg;
1127 int r;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001128
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001129 if ((msg = sshbuf_new()) == NULL)
1130 fatal("%s: sshbuf_new failed", __func__);
1131 sshbuf_reset(msg);
1132 if ((r = sshbuf_put_u8(msg, SSH2_FXP_READ)) != 0 ||
1133 (r = sshbuf_put_u32(msg, id)) != 0 ||
1134 (r = sshbuf_put_string(msg, handle, handle_len)) != 0 ||
1135 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1136 (r = sshbuf_put_u32(msg, len)) != 0)
1137 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1138 send_msg(conn, msg);
1139 sshbuf_free(msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001140}
Damien Miller16a13332002-02-13 14:03:56 +11001141
Damien Miller058316f2001-03-08 10:08:49 +11001142int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001143do_download(struct sftp_conn *conn, const char *remote_path,
1144 const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
1145 int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001146{
Darren Tucker1b0dd172009-10-07 08:37:48 +11001147 Attrib junk;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001148 struct sshbuf *msg;
1149 u_char *handle;
1150 int local_fd = -1, write_error;
1151 int read_error, write_errno, reordered = 0, r;
Damien Miller0d032412013-07-25 11:56:52 +10001152 u_int64_t offset = 0, size, highwater;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001153 u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
Damien Miller62d57f62003-01-10 21:43:24 +11001154 off_t progress_counter;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001155 size_t handle_len;
Damien Miller0d032412013-07-25 11:56:52 +10001156 struct stat st;
Damien Miller16a13332002-02-13 14:03:56 +11001157 struct request {
1158 u_int id;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001159 size_t len;
Damien Miller16a13332002-02-13 14:03:56 +11001160 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001161 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +11001162 };
1163 TAILQ_HEAD(reqhead, request) requests;
1164 struct request *req;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001165 u_char type;
Damien Miller16a13332002-02-13 14:03:56 +11001166
1167 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +11001168
Darren Tucker1b0dd172009-10-07 08:37:48 +11001169 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1170 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001171
Damien Miller9e720282008-06-29 22:46:35 +10001172 /* Do not preserve set[ug]id here, as we do not preserve ownership */
Damien Miller33804262001-02-04 23:20:18 +11001173 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +11001174 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +11001175 else
1176 mode = 0666;
1177
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001178 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +11001179 (!S_ISREG(a->perm))) {
1180 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001181 return(-1);
1182 }
1183
Damien Miller16a13332002-02-13 14:03:56 +11001184 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1185 size = a->size;
1186 else
1187 size = 0;
1188
Damien Miller3db5f532002-02-13 14:10:32 +11001189 buflen = conn->transfer_buflen;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001190 if ((msg = sshbuf_new()) == NULL)
1191 fatal("%s: sshbuf_new failed", __func__);
1192
1193 attrib_clear(&junk); /* Send empty attributes */
Damien Miller33804262001-02-04 23:20:18 +11001194
1195 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001196 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001197 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1198 (r = sshbuf_put_u32(msg, id)) != 0 ||
1199 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1200 (r = sshbuf_put_u32(msg, SSH2_FXF_READ)) != 0 ||
1201 (r = encode_attrib(msg, &junk)) != 0)
1202 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1203 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001204 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001205
Damien Miller65e42f82010-09-24 22:15:11 +10001206 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001207 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001208 if (handle == NULL) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001209 sshbuf_free(msg);
Damien Miller33804262001-02-04 23:20:18 +11001210 return(-1);
1211 }
1212
Damien Millerbda5c842013-10-15 12:05:58 +11001213 local_fd = open(local_path,
1214 O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
Damien Miller3db5f532002-02-13 14:10:32 +11001215 if (local_fd == -1) {
1216 error("Couldn't open local file \"%s\" for writing: %s",
1217 local_path, strerror(errno));
Damien Miller0d032412013-07-25 11:56:52 +10001218 goto fail;
1219 }
1220 offset = highwater = 0;
Damien Millerbda5c842013-10-15 12:05:58 +11001221 if (resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001222 if (fstat(local_fd, &st) == -1) {
1223 error("Unable to stat local file \"%s\": %s",
1224 local_path, strerror(errno));
1225 goto fail;
1226 }
Damien Millerc3d483f2014-01-17 11:20:26 +11001227 if (st.st_size < 0) {
1228 error("\"%s\" has negative size", local_path);
1229 goto fail;
1230 }
1231 if ((u_int64_t)st.st_size > size) {
Damien Miller0d032412013-07-25 11:56:52 +10001232 error("Unable to resume download of \"%s\": "
1233 "local file is larger than remote", local_path);
1234 fail:
1235 do_close(conn, handle, handle_len);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001236 sshbuf_free(msg);
Damien Miller0d032412013-07-25 11:56:52 +10001237 free(handle);
Damien Miller9275df32013-12-05 10:26:32 +11001238 if (local_fd != -1)
1239 close(local_fd);
Damien Miller0d032412013-07-25 11:56:52 +10001240 return -1;
1241 }
1242 offset = highwater = st.st_size;
Damien Miller3db5f532002-02-13 14:10:32 +11001243 }
1244
Damien Miller33804262001-02-04 23:20:18 +11001245 /* Read from remote and write to local */
Damien Miller0d032412013-07-25 11:56:52 +10001246 write_error = read_error = write_errno = num_req = 0;
Damien Miller16a13332002-02-13 14:03:56 +11001247 max_req = 1;
Damien Miller0d032412013-07-25 11:56:52 +10001248 progress_counter = offset;
Damien Miller62d57f62003-01-10 21:43:24 +11001249
Damien Miller9ba30692004-03-08 23:12:02 +11001250 if (showprogress && size != 0)
1251 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001252
Damien Miller16a13332002-02-13 14:03:56 +11001253 while (num_req > 0 || max_req > 0) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001254 u_char *data;
1255 size_t len;
Damien Miller33804262001-02-04 23:20:18 +11001256
Darren Tuckercdf547a2004-05-24 10:12:19 +10001257 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001258 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001259 * allow outstanding requests to drain gracefully
1260 */
1261 if (interrupted) {
1262 if (num_req == 0) /* If we haven't started yet... */
1263 break;
1264 max_req = 0;
1265 }
1266
Damien Miller16a13332002-02-13 14:03:56 +11001267 /* Send some more requests */
1268 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001269 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +00001270 (unsigned long long)offset,
1271 (unsigned long long)offset + buflen - 1,
1272 num_req, max_req);
Damien Miller6c81fee2013-11-08 12:19:55 +11001273 req = xcalloc(1, sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +11001274 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001275 req->len = buflen;
1276 req->offset = offset;
1277 offset += buflen;
1278 num_req++;
1279 TAILQ_INSERT_TAIL(&requests, req, tq);
Damien Miller65e42f82010-09-24 22:15:11 +10001280 send_read_request(conn, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001281 req->len, handle, handle_len);
1282 }
Damien Miller33804262001-02-04 23:20:18 +11001283
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001284 sshbuf_reset(msg);
1285 get_msg(conn, msg);
1286 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1287 (r = sshbuf_get_u32(msg, &id)) != 0)
1288 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001289 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001290
Damien Miller16a13332002-02-13 14:03:56 +11001291 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001292 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001293 req != NULL && req->id != id;
1294 req = TAILQ_NEXT(req, tq))
1295 ;
1296 if (req == NULL)
1297 fatal("Unexpected reply %u", id);
1298
1299 switch (type) {
1300 case SSH2_FXP_STATUS:
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001301 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1302 fatal("%s: buffer error: %s",
1303 __func__, ssh_err(r));
Damien Miller16a13332002-02-13 14:03:56 +11001304 if (status != SSH2_FX_EOF)
1305 read_error = 1;
1306 max_req = 0;
1307 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001308 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001309 num_req--;
1310 break;
1311 case SSH2_FXP_DATA:
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001312 if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1313 fatal("%s: buffer error: %s",
1314 __func__, ssh_err(r));
Ben Lindstromeb505452002-03-22 01:03:15 +00001315 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001316 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001317 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001318 if (len > req->len)
1319 fatal("Received more data than asked for "
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001320 "%zu > %zu", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001321 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001322 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001323 !write_error) {
1324 write_errno = errno;
1325 write_error = 1;
1326 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001327 }
Damien Miller0d032412013-07-25 11:56:52 +10001328 else if (!reordered && req->offset <= highwater)
1329 highwater = req->offset + len;
1330 else if (!reordered && req->offset > highwater)
1331 reordered = 1;
Damien Miller62d57f62003-01-10 21:43:24 +11001332 progress_counter += len;
Darren Tuckera627d422013-06-02 07:31:17 +10001333 free(data);
Damien Miller16a13332002-02-13 14:03:56 +11001334
1335 if (len == req->len) {
1336 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001337 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001338 num_req--;
1339 } else {
1340 /* Resend the request for the missing data */
1341 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001342 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001343 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001344 (unsigned long long)req->offset +
1345 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001346 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001347 req->len -= len;
1348 req->offset += len;
Damien Miller65e42f82010-09-24 22:15:11 +10001349 send_read_request(conn, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001350 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001351 /* Reduce the request size */
1352 if (len < buflen)
1353 buflen = MAX(MIN_READ_SIZE, len);
1354 }
1355 if (max_req > 0) { /* max_req = 0 iff EOF received */
1356 if (size > 0 && offset > size) {
1357 /* Only one request at a time
1358 * after the expected EOF */
1359 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001360 (unsigned long long)offset,
1361 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001362 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001363 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001364 ++max_req;
1365 }
1366 }
1367 break;
1368 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001369 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001370 SSH2_FXP_DATA, type);
1371 }
Damien Miller33804262001-02-04 23:20:18 +11001372 }
Damien Miller33804262001-02-04 23:20:18 +11001373
Damien Miller62d57f62003-01-10 21:43:24 +11001374 if (showprogress && size)
1375 stop_progress_meter();
1376
Damien Miller16a13332002-02-13 14:03:56 +11001377 /* Sanity check */
1378 if (TAILQ_FIRST(&requests) != NULL)
1379 fatal("Transfer complete, but requests still in queue");
Damien Miller0d032412013-07-25 11:56:52 +10001380 /* Truncate at highest contiguous point to avoid holes on interrupt */
1381 if (read_error || write_error || interrupted) {
Damien Millerbda5c842013-10-15 12:05:58 +11001382 if (reordered && resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001383 error("Unable to resume download of \"%s\": "
1384 "server reordered requests", local_path);
1385 }
1386 debug("truncating at %llu", (unsigned long long)highwater);
1387 ftruncate(local_fd, highwater);
1388 }
Damien Miller16a13332002-02-13 14:03:56 +11001389 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001390 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001391 remote_path, fx2txt(status));
Damien Millerfec029f2013-08-21 02:42:12 +10001392 status = -1;
Damien Miller3db5f532002-02-13 14:10:32 +11001393 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001394 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001395 error("Couldn't write to \"%s\": %s", local_path,
1396 strerror(write_errno));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001397 status = SSH2_FX_FAILURE;
Damien Miller3db5f532002-02-13 14:10:32 +11001398 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001399 } else {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001400 if (do_close(conn, handle, handle_len) != 0 || interrupted)
1401 status = SSH2_FX_FAILURE;
1402 else
1403 status = SSH2_FX_OK;
Damien Miller16a13332002-02-13 14:03:56 +11001404 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001405#ifdef HAVE_FCHMOD
Damien Millerbda5c842013-10-15 12:05:58 +11001406 if (preserve_flag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001407#else
Damien Millerbda5c842013-10-15 12:05:58 +11001408 if (preserve_flag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001409#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001410 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001411 strerror(errno));
Damien Millerbda5c842013-10-15 12:05:58 +11001412 if (preserve_flag &&
1413 (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
Damien Miller16a13332002-02-13 14:03:56 +11001414 struct timeval tv[2];
1415 tv[0].tv_sec = a->atime;
1416 tv[1].tv_sec = a->mtime;
1417 tv[0].tv_usec = tv[1].tv_usec = 0;
1418 if (utimes(local_path, tv) == -1)
1419 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001420 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001421 }
Damien Millerf29238e2013-10-17 11:48:52 +11001422 if (fsync_flag) {
1423 debug("syncing \"%s\"", local_path);
1424 if (fsync(local_fd) == -1)
1425 error("Couldn't sync file \"%s\": %s",
1426 local_path, strerror(errno));
1427 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001428 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001429 close(local_fd);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001430 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001431 free(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001432
1433 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001434}
1435
Darren Tucker1b0dd172009-10-07 08:37:48 +11001436static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001437download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1438 int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
1439 int resume_flag, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001440{
1441 int i, ret = 0;
1442 SFTP_DIRENT **dir_entries;
1443 char *filename, *new_src, *new_dst;
1444 mode_t mode = 0777;
1445
1446 if (depth >= MAX_DIR_DEPTH) {
1447 error("Maximum directory depth exceeded: %d levels", depth);
1448 return -1;
1449 }
1450
1451 if (dirattrib == NULL &&
1452 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1453 error("Unable to stat remote directory \"%s\"", src);
1454 return -1;
1455 }
1456 if (!S_ISDIR(dirattrib->perm)) {
1457 error("\"%s\" is not a directory", src);
1458 return -1;
1459 }
Damien Millerbda5c842013-10-15 12:05:58 +11001460 if (print_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001461 printf("Retrieving %s\n", src);
1462
1463 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1464 mode = dirattrib->perm & 01777;
1465 else {
1466 debug("Server did not send permissions for "
1467 "directory \"%s\"", dst);
1468 }
1469
1470 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1471 error("mkdir %s: %s", dst, strerror(errno));
1472 return -1;
1473 }
1474
1475 if (do_readdir(conn, src, &dir_entries) == -1) {
1476 error("%s: Failed to get directory contents", src);
1477 return -1;
1478 }
1479
1480 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1481 filename = dir_entries[i]->filename;
1482
1483 new_dst = path_append(dst, filename);
1484 new_src = path_append(src, filename);
1485
1486 if (S_ISDIR(dir_entries[i]->a.perm)) {
1487 if (strcmp(filename, ".") == 0 ||
1488 strcmp(filename, "..") == 0)
1489 continue;
1490 if (download_dir_internal(conn, new_src, new_dst,
Damien Millerbda5c842013-10-15 12:05:58 +11001491 depth + 1, &(dir_entries[i]->a), preserve_flag,
Damien Millerf29238e2013-10-17 11:48:52 +11001492 print_flag, resume_flag, fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001493 ret = -1;
1494 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1495 if (do_download(conn, new_src, new_dst,
Damien Millerf29238e2013-10-17 11:48:52 +11001496 &(dir_entries[i]->a), preserve_flag,
1497 resume_flag, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001498 error("Download of file %s to %s failed",
1499 new_src, new_dst);
1500 ret = -1;
1501 }
1502 } else
1503 logit("%s: not a regular file\n", new_src);
1504
Darren Tuckera627d422013-06-02 07:31:17 +10001505 free(new_dst);
1506 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001507 }
1508
Damien Millerbda5c842013-10-15 12:05:58 +11001509 if (preserve_flag) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001510 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1511 struct timeval tv[2];
1512 tv[0].tv_sec = dirattrib->atime;
1513 tv[1].tv_sec = dirattrib->mtime;
1514 tv[0].tv_usec = tv[1].tv_usec = 0;
1515 if (utimes(dst, tv) == -1)
1516 error("Can't set times on \"%s\": %s",
1517 dst, strerror(errno));
1518 } else
1519 debug("Server did not send times for directory "
1520 "\"%s\"", dst);
1521 }
1522
1523 free_sftp_dirents(dir_entries);
1524
1525 return ret;
1526}
1527
1528int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001529download_dir(struct sftp_conn *conn, const char *src, const char *dst,
1530 Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1531 int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001532{
1533 char *src_canon;
1534 int ret;
1535
1536 if ((src_canon = do_realpath(conn, src)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001537 error("Unable to canonicalize path \"%s\"", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001538 return -1;
1539 }
1540
Damien Millerbda5c842013-10-15 12:05:58 +11001541 ret = download_dir_internal(conn, src_canon, dst, 0,
Damien Millerf29238e2013-10-17 11:48:52 +11001542 dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag);
Darren Tuckera627d422013-06-02 07:31:17 +10001543 free(src_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001544 return ret;
1545}
1546
Damien Miller33804262001-02-04 23:20:18 +11001547int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001548do_upload(struct sftp_conn *conn, const char *local_path,
1549 const char *remote_path, int preserve_flag, int resume, int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001550{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001551 int r, local_fd;
1552 u_int status = SSH2_FX_OK;
1553 u_int id;
1554 u_char type;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001555 off_t offset, progress_counter;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001556 u_char *handle, *data;
1557 struct sshbuf *msg;
Damien Miller33804262001-02-04 23:20:18 +11001558 struct stat sb;
Damien Millerd8accc02014-05-15 13:46:25 +10001559 Attrib a, *c = NULL;
Damien Miller16a13332002-02-13 14:03:56 +11001560 u_int32_t startid;
1561 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001562 struct outstanding_ack {
1563 u_int id;
1564 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001565 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001566 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001567 };
1568 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001569 struct outstanding_ack *ack = NULL;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001570 size_t handle_len;
Damien Miller5873dfd2002-02-13 14:04:37 +11001571
1572 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001573
1574 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1575 error("Couldn't open local file \"%s\" for reading: %s",
1576 local_path, strerror(errno));
1577 return(-1);
1578 }
1579 if (fstat(local_fd, &sb) == -1) {
1580 error("Couldn't fstat local file \"%s\": %s",
1581 local_path, strerror(errno));
1582 close(local_fd);
1583 return(-1);
1584 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001585 if (!S_ISREG(sb.st_mode)) {
1586 error("%s is not a regular file", local_path);
1587 close(local_fd);
1588 return(-1);
1589 }
Damien Miller33804262001-02-04 23:20:18 +11001590 stat_to_attrib(&sb, &a);
1591
1592 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1593 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1594 a.perm &= 0777;
Damien Millerbda5c842013-10-15 12:05:58 +11001595 if (!preserve_flag)
Damien Miller33804262001-02-04 23:20:18 +11001596 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1597
Damien Millerd8accc02014-05-15 13:46:25 +10001598 if (resume) {
1599 /* Get remote file size if it exists */
1600 if ((c = do_stat(conn, remote_path, 0)) == NULL) {
1601 close(local_fd);
1602 return -1;
1603 }
1604
1605 if ((off_t)c->size >= sb.st_size) {
1606 error("destination file bigger or same size as "
1607 "source file");
1608 close(local_fd);
1609 return -1;
1610 }
1611
1612 if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
1613 close(local_fd);
1614 return -1;
1615 }
1616 }
1617
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001618 if ((msg = sshbuf_new()) == NULL)
1619 fatal("%s: sshbuf_new failed", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001620
1621 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001622 id = conn->msg_id++;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001623 if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1624 (r = sshbuf_put_u32(msg, id)) != 0 ||
1625 (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1626 (r = sshbuf_put_u32(msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|
1627 (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC))) != 0 ||
1628 (r = encode_attrib(msg, &a)) != 0)
1629 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1630 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001631 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001632
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001633 sshbuf_reset(msg);
Damien Miller33804262001-02-04 23:20:18 +11001634
Damien Miller65e42f82010-09-24 22:15:11 +10001635 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001636 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001637 if (handle == NULL) {
1638 close(local_fd);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001639 sshbuf_free(msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001640 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001641 }
1642
Damien Miller16a13332002-02-13 14:03:56 +11001643 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001644 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001645
Damien Miller33804262001-02-04 23:20:18 +11001646 /* Read from local and write to remote */
Damien Millerd8accc02014-05-15 13:46:25 +10001647 offset = progress_counter = (resume ? c->size : 0);
Damien Miller62d57f62003-01-10 21:43:24 +11001648 if (showprogress)
Darren Tuckerc9a19912013-06-02 08:37:05 +10001649 start_progress_meter(local_path, sb.st_size,
1650 &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001651
Damien Miller9f0f5c62001-12-21 14:45:46 +11001652 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001653 int len;
Damien Miller33804262001-02-04 23:20:18 +11001654
1655 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001656 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001657 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001658 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001659 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001660 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001661 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001662 len = 0;
1663 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001664 len = read(local_fd, data, conn->transfer_buflen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001665 while ((len == -1) &&
1666 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
Damien Miller33804262001-02-04 23:20:18 +11001667
1668 if (len == -1)
1669 fatal("Couldn't read from \"%s\": %s", local_path,
1670 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001671
1672 if (len != 0) {
Damien Miller6c81fee2013-11-08 12:19:55 +11001673 ack = xcalloc(1, sizeof(*ack));
Damien Miller5873dfd2002-02-13 14:04:37 +11001674 ack->id = ++id;
1675 ack->offset = offset;
1676 ack->len = len;
1677 TAILQ_INSERT_TAIL(&acks, ack, tq);
1678
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001679 sshbuf_reset(msg);
1680 if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
1681 (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
1682 (r = sshbuf_put_string(msg, handle,
1683 handle_len)) != 0 ||
1684 (r = sshbuf_put_u64(msg, offset)) != 0 ||
1685 (r = sshbuf_put_string(msg, data, len)) != 0)
1686 fatal("%s: buffer error: %s",
1687 __func__, ssh_err(r));
1688 send_msg(conn, msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001689 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001690 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001691 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001692 break;
1693
Damien Miller5873dfd2002-02-13 14:04:37 +11001694 if (ack == NULL)
1695 fatal("Unexpected ACK %u", id);
1696
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001697 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001698 id - ackid >= conn->num_requests) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001699 u_int rid;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001700
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001701 sshbuf_reset(msg);
1702 get_msg(conn, msg);
1703 if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1704 (r = sshbuf_get_u32(msg, &rid)) != 0)
1705 fatal("%s: buffer error: %s",
1706 __func__, ssh_err(r));
Damien Miller5873dfd2002-02-13 14:04:37 +11001707
1708 if (type != SSH2_FXP_STATUS)
1709 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1710 "got %d", SSH2_FXP_STATUS, type);
1711
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001712 if ((r = sshbuf_get_u32(msg, &status)) != 0)
1713 fatal("%s: buffer error: %s",
1714 __func__, ssh_err(r));
1715 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller5873dfd2002-02-13 14:04:37 +11001716
1717 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001718 for (ack = TAILQ_FIRST(&acks);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001719 ack != NULL && ack->id != rid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001720 ack = TAILQ_NEXT(ack, tq))
1721 ;
1722 if (ack == NULL)
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001723 fatal("Can't find request for ID %u", rid);
Damien Miller5873dfd2002-02-13 14:04:37 +11001724 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001725 debug3("In write loop, ack for %u %u bytes at %lld",
1726 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001727 ++ackid;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001728 progress_counter += ack->len;
Darren Tuckera627d422013-06-02 07:31:17 +10001729 free(ack);
Damien Miller33804262001-02-04 23:20:18 +11001730 }
Damien Miller33804262001-02-04 23:20:18 +11001731 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001732 if (offset < 0)
1733 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001734 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001735 sshbuf_free(msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001736
Damien Miller62d57f62003-01-10 21:43:24 +11001737 if (showprogress)
1738 stop_progress_meter();
Darren Tuckera627d422013-06-02 07:31:17 +10001739 free(data);
Damien Miller33804262001-02-04 23:20:18 +11001740
Damien Milleracdf25b2008-02-10 22:27:24 +11001741 if (status != SSH2_FX_OK) {
1742 error("Couldn't write to remote file \"%s\": %s",
1743 remote_path, fx2txt(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001744 status = SSH2_FX_FAILURE;
Damien Milleracdf25b2008-02-10 22:27:24 +11001745 }
1746
Damien Miller33804262001-02-04 23:20:18 +11001747 if (close(local_fd) == -1) {
1748 error("Couldn't close local file \"%s\": %s", local_path,
1749 strerror(errno));
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001750 status = SSH2_FX_FAILURE;
Damien Miller33804262001-02-04 23:20:18 +11001751 }
1752
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001753 /* Override umask and utimes if asked */
Damien Millerbda5c842013-10-15 12:05:58 +11001754 if (preserve_flag)
Damien Miller3db5f532002-02-13 14:10:32 +11001755 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001756
Damien Millerf29238e2013-10-17 11:48:52 +11001757 if (fsync_flag)
1758 (void)do_fsync(conn, handle, handle_len);
1759
Damien Milleracdf25b2008-02-10 22:27:24 +11001760 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001761 status = SSH2_FX_FAILURE;
1762
Darren Tuckera627d422013-06-02 07:31:17 +10001763 free(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001764
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001765 return status == SSH2_FX_OK ? 0 : -1;
Damien Miller33804262001-02-04 23:20:18 +11001766}
Darren Tucker1b0dd172009-10-07 08:37:48 +11001767
1768static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001769upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1770 int depth, int preserve_flag, int print_flag, int resume, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001771{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001772 int ret = 0;
1773 u_int status;
Darren Tucker1b0dd172009-10-07 08:37:48 +11001774 DIR *dirp;
1775 struct dirent *dp;
1776 char *filename, *new_src, *new_dst;
1777 struct stat sb;
1778 Attrib a;
1779
1780 if (depth >= MAX_DIR_DEPTH) {
1781 error("Maximum directory depth exceeded: %d levels", depth);
1782 return -1;
1783 }
1784
1785 if (stat(src, &sb) == -1) {
1786 error("Couldn't stat directory \"%s\": %s",
1787 src, strerror(errno));
1788 return -1;
1789 }
1790 if (!S_ISDIR(sb.st_mode)) {
1791 error("\"%s\" is not a directory", src);
1792 return -1;
1793 }
Damien Millerbda5c842013-10-15 12:05:58 +11001794 if (print_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001795 printf("Entering %s\n", src);
1796
1797 attrib_clear(&a);
1798 stat_to_attrib(&sb, &a);
1799 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1800 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1801 a.perm &= 01777;
Damien Millerbda5c842013-10-15 12:05:58 +11001802 if (!preserve_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001803 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
Damien Miller0d032412013-07-25 11:56:52 +10001804
Darren Tucker1b0dd172009-10-07 08:37:48 +11001805 status = do_mkdir(conn, dst, &a, 0);
1806 /*
1807 * we lack a portable status for errno EEXIST,
1808 * so if we get a SSH2_FX_FAILURE back we must check
1809 * if it was created successfully.
1810 */
1811 if (status != SSH2_FX_OK) {
1812 if (status != SSH2_FX_FAILURE)
1813 return -1;
Damien Miller0d032412013-07-25 11:56:52 +10001814 if (do_stat(conn, dst, 0) == NULL)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001815 return -1;
1816 }
1817
1818 if ((dirp = opendir(src)) == NULL) {
1819 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1820 return -1;
1821 }
Damien Miller0d032412013-07-25 11:56:52 +10001822
Darren Tucker1b0dd172009-10-07 08:37:48 +11001823 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1824 if (dp->d_ino == 0)
1825 continue;
1826 filename = dp->d_name;
1827 new_dst = path_append(dst, filename);
1828 new_src = path_append(src, filename);
1829
Darren Tucker438b4732009-10-11 21:52:10 +11001830 if (lstat(new_src, &sb) == -1) {
1831 logit("%s: lstat failed: %s", filename,
1832 strerror(errno));
1833 ret = -1;
1834 } else if (S_ISDIR(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001835 if (strcmp(filename, ".") == 0 ||
1836 strcmp(filename, "..") == 0)
1837 continue;
1838
1839 if (upload_dir_internal(conn, new_src, new_dst,
Damien Millerd8accc02014-05-15 13:46:25 +10001840 depth + 1, preserve_flag, print_flag, resume,
Damien Millerf29238e2013-10-17 11:48:52 +11001841 fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001842 ret = -1;
Darren Tucker438b4732009-10-11 21:52:10 +11001843 } else if (S_ISREG(sb.st_mode)) {
Damien Millerbda5c842013-10-15 12:05:58 +11001844 if (do_upload(conn, new_src, new_dst,
Damien Millerd8accc02014-05-15 13:46:25 +10001845 preserve_flag, resume, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001846 error("Uploading of file %s to %s failed!",
1847 new_src, new_dst);
1848 ret = -1;
1849 }
1850 } else
1851 logit("%s: not a regular file\n", filename);
Darren Tuckera627d422013-06-02 07:31:17 +10001852 free(new_dst);
1853 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001854 }
1855
1856 do_setstat(conn, dst, &a);
1857
1858 (void) closedir(dirp);
1859 return ret;
1860}
1861
1862int
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001863upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
1864 int preserve_flag, int print_flag, int resume, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001865{
1866 char *dst_canon;
1867 int ret;
1868
1869 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001870 error("Unable to canonicalize path \"%s\"", dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001871 return -1;
1872 }
1873
Damien Miller2f93d052013-10-15 12:06:27 +11001874 ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
Damien Millerd8accc02014-05-15 13:46:25 +10001875 print_flag, resume, fsync_flag);
Damien Millerf29238e2013-10-17 11:48:52 +11001876
Darren Tuckera627d422013-06-02 07:31:17 +10001877 free(dst_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001878 return ret;
1879}
1880
1881char *
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001882path_append(const char *p1, const char *p2)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001883{
1884 char *ret;
1885 size_t len = strlen(p1) + strlen(p2) + 2;
1886
1887 ret = xmalloc(len);
1888 strlcpy(ret, p1, len);
1889 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1890 strlcat(ret, "/", len);
1891 strlcat(ret, p2, len);
1892
1893 return(ret);
1894}
1895