blob: 990b58d14f9f7b61409e3a1e04995ded6f4cf66a [file] [log] [blame]
Damien Millerd8accc02014-05-15 13:46:25 +10001/* $OpenBSD: sftp-client.c,v 1.115 2014/04/21 14:36:16 logan Exp $ */
Damien Miller33804262001-02-04 23:20:18 +11002/*
Damien Miller4e60ed72004-02-17 17:07:59 +11003 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
Damien Miller33804262001-02-04 23:20:18 +11004 *
Damien Miller4e60ed72004-02-17 17:07:59 +11005 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
Damien Miller33804262001-02-04 23:20:18 +11008 *
Damien Miller4e60ed72004-02-17 17:07:59 +11009 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller33804262001-02-04 23:20:18 +110016 */
17
18/* XXX: memleaks */
19/* XXX: signed vs unsigned */
Damien Miller3db5f532002-02-13 14:10:32 +110020/* XXX: remove all logging, only return status codes */
Damien Miller33804262001-02-04 23:20:18 +110021/* XXX: copy between two remote sites */
22
23#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110024
25#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100026#include <sys/param.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"
Damien Millerd7834352006-08-05 12:39:39 +100050#include "buffer.h"
Damien Miller33804262001-02-04 23:20:18 +110051#include "log.h"
52#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110053#include "progressmeter.h"
Damien Miller3f941882006-03-31 23:13:02 +110054#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110055
56#include "sftp.h"
57#include "sftp-common.h"
58#include "sftp-client.h"
59
Darren Tuckercdf547a2004-05-24 10:12:19 +100060extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110061extern int showprogress;
62
Damien Miller0c8d8f62006-03-15 11:34:25 +110063/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110064#define MIN_READ_SIZE 512
65
Darren Tucker1b0dd172009-10-07 08:37:48 +110066/* Maximum depth to descend in directory trees */
67#define MAX_DIR_DEPTH 64
68
Damien Miller3db5f532002-02-13 14:10:32 +110069struct sftp_conn {
70 int fd_in;
71 int fd_out;
72 u_int transfer_buflen;
73 u_int num_requests;
74 u_int version;
75 u_int msg_id;
Damien Millerd671e5a2008-05-19 14:53:33 +100076#define SFTP_EXT_POSIX_RENAME 0x00000001
77#define SFTP_EXT_STATVFS 0x00000002
78#define SFTP_EXT_FSTATVFS 0x00000004
Darren Tuckeraf1f9092010-12-05 09:02:47 +110079#define SFTP_EXT_HARDLINK 0x00000008
Damien Millerf29238e2013-10-17 11:48:52 +110080#define SFTP_EXT_FSYNC 0x00000010
Damien Miller7a3e1d02008-03-27 10:59:57 +110081 u_int exts;
Damien Miller65e42f82010-09-24 22:15:11 +100082 u_int64_t limit_kbps;
83 struct bwlimit bwlimit_in, bwlimit_out;
Damien Miller3db5f532002-02-13 14:10:32 +110084};
Ben Lindstrom288cc392001-02-09 02:58:04 +000085
Darren Tuckerc22f0902009-10-07 08:24:19 +110086static char *
Damien Miller65e42f82010-09-24 22:15:11 +100087get_handle(struct sftp_conn *conn, u_int expected_id, u_int *len,
88 const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
89
90/* ARGSUSED */
91static int
92sftpio(void *_bwlimit, size_t amount)
93{
94 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
95
96 bandwidth_limit(bwlimit, amount);
97 return 0;
98}
Darren Tuckerc22f0902009-10-07 08:24:19 +110099
Ben Lindstrombba81212001-06-25 05:01:22 +0000100static void
Damien Miller65e42f82010-09-24 22:15:11 +1000101send_msg(struct sftp_conn *conn, Buffer *m)
Damien Miller33804262001-02-04 23:20:18 +1100102{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100103 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +1000104 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +1100105
Damien Miller54446182006-01-02 23:40:50 +1100106 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +1100107 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +1100108
Damien Millera7f3aaa2003-01-10 21:43:58 +1100109 /* Send length first */
Damien Miller3f941882006-03-31 23:13:02 +1100110 put_u32(mlen, buffer_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +1000111 iov[0].iov_base = mlen;
112 iov[0].iov_len = sizeof(mlen);
113 iov[1].iov_base = buffer_ptr(m);
114 iov[1].iov_len = buffer_len(m);
Damien Millerd7834352006-08-05 12:39:39 +1000115
Damien Miller65e42f82010-09-24 22:15:11 +1000116 if (atomiciov6(writev, conn->fd_out, iov, 2,
Damien Miller0d032412013-07-25 11:56:52 +1000117 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_out) !=
Damien Miller65e42f82010-09-24 22:15:11 +1000118 buffer_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +1100119 fatal("Couldn't send packet: %s", strerror(errno));
120
121 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +1100122}
123
Ben Lindstrombba81212001-06-25 05:01:22 +0000124static void
Damien Miller65e42f82010-09-24 22:15:11 +1000125get_msg(struct sftp_conn *conn, Buffer *m)
Damien Miller33804262001-02-04 23:20:18 +1100126{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100127 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +1100128
Damien Millera7f3aaa2003-01-10 21:43:58 +1100129 buffer_append_space(m, 4);
Damien Miller65e42f82010-09-24 22:15:11 +1000130 if (atomicio6(read, conn->fd_in, buffer_ptr(m), 4,
131 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in) != 4) {
Damien Millerb253cc42005-05-26 12:23:44 +1000132 if (errno == EPIPE)
133 fatal("Connection closed");
134 else
135 fatal("Couldn't read packet: %s", strerror(errno));
136 }
Damien Miller33804262001-02-04 23:20:18 +1100137
Damien Millera7f3aaa2003-01-10 21:43:58 +1100138 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +1100139 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000140 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100141
Damien Millera7f3aaa2003-01-10 21:43:58 +1100142 buffer_append_space(m, msg_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000143 if (atomicio6(read, conn->fd_in, buffer_ptr(m), msg_len,
144 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in)
145 != msg_len) {
Damien Millerb253cc42005-05-26 12:23:44 +1000146 if (errno == EPIPE)
147 fatal("Connection closed");
148 else
149 fatal("Read packet: %s", strerror(errno));
150 }
Damien Miller33804262001-02-04 23:20:18 +1100151}
152
Ben Lindstrombba81212001-06-25 05:01:22 +0000153static void
Damien Miller65e42f82010-09-24 22:15:11 +1000154send_string_request(struct sftp_conn *conn, u_int id, u_int code, char *s,
Damien Miller33804262001-02-04 23:20:18 +1100155 u_int len)
156{
157 Buffer msg;
158
159 buffer_init(&msg);
160 buffer_put_char(&msg, code);
161 buffer_put_int(&msg, id);
162 buffer_put_string(&msg, s, len);
Damien Miller65e42f82010-09-24 22:15:11 +1000163 send_msg(conn, &msg);
164 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100165 buffer_free(&msg);
166}
167
Ben Lindstrombba81212001-06-25 05:01:22 +0000168static void
Damien Miller65e42f82010-09-24 22:15:11 +1000169send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
170 char *s, u_int len, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100171{
172 Buffer msg;
173
174 buffer_init(&msg);
175 buffer_put_char(&msg, code);
176 buffer_put_int(&msg, id);
177 buffer_put_string(&msg, s, len);
178 encode_attrib(&msg, a);
Damien Miller65e42f82010-09-24 22:15:11 +1000179 send_msg(conn, &msg);
180 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100181 buffer_free(&msg);
182}
183
Ben Lindstrombba81212001-06-25 05:01:22 +0000184static u_int
Damien Miller65e42f82010-09-24 22:15:11 +1000185get_status(struct sftp_conn *conn, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100186{
187 Buffer msg;
188 u_int type, id, status;
189
190 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000191 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100192 type = buffer_get_char(&msg);
193 id = buffer_get_int(&msg);
194
195 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000196 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100197 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000198 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100199 SSH2_FXP_STATUS, type);
200
201 status = buffer_get_int(&msg);
202 buffer_free(&msg);
203
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000204 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100205
Damien Miller65e42f82010-09-24 22:15:11 +1000206 return status;
Damien Miller33804262001-02-04 23:20:18 +1100207}
208
Ben Lindstrombba81212001-06-25 05:01:22 +0000209static char *
Damien Miller65e42f82010-09-24 22:15:11 +1000210get_handle(struct sftp_conn *conn, u_int expected_id, u_int *len,
211 const char *errfmt, ...)
Damien Miller33804262001-02-04 23:20:18 +1100212{
213 Buffer msg;
214 u_int type, id;
Darren Tuckerc22f0902009-10-07 08:24:19 +1100215 char *handle, errmsg[256];
216 va_list args;
217 int status;
218
219 va_start(args, errfmt);
220 if (errfmt != NULL)
221 vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
222 va_end(args);
Damien Miller33804262001-02-04 23:20:18 +1100223
224 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000225 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100226 type = buffer_get_char(&msg);
227 id = buffer_get_int(&msg);
228
229 if (id != expected_id)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100230 fatal("%s: ID mismatch (%u != %u)",
231 errfmt == NULL ? __func__ : errmsg, id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100232 if (type == SSH2_FXP_STATUS) {
Darren Tuckerc22f0902009-10-07 08:24:19 +1100233 status = buffer_get_int(&msg);
234 if (errfmt != NULL)
235 error("%s: %s", errmsg, fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100236 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100237 return(NULL);
238 } else if (type != SSH2_FXP_HANDLE)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100239 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
240 errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
Damien Miller33804262001-02-04 23:20:18 +1100241
242 handle = buffer_get_string(&msg, len);
243 buffer_free(&msg);
244
245 return(handle);
246}
247
Ben Lindstrombba81212001-06-25 05:01:22 +0000248static Attrib *
Damien Miller65e42f82010-09-24 22:15:11 +1000249get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100250{
251 Buffer msg;
252 u_int type, id;
253 Attrib *a;
254
255 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000256 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100257
258 type = buffer_get_char(&msg);
259 id = buffer_get_int(&msg);
260
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000261 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100262 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000263 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100264 if (type == SSH2_FXP_STATUS) {
265 int status = buffer_get_int(&msg);
266
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000267 if (quiet)
268 debug("Couldn't stat remote file: %s", fx2txt(status));
269 else
270 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100271 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100272 return(NULL);
273 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000274 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100275 SSH2_FXP_ATTRS, type);
276 }
277 a = decode_attrib(&msg);
278 buffer_free(&msg);
279
280 return(a);
281}
282
Damien Millerd671e5a2008-05-19 14:53:33 +1000283static int
Damien Miller65e42f82010-09-24 22:15:11 +1000284get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
285 u_int expected_id, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000286{
287 Buffer msg;
288 u_int type, id, flag;
289
290 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000291 get_msg(conn, &msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000292
293 type = buffer_get_char(&msg);
294 id = buffer_get_int(&msg);
295
296 debug3("Received statvfs reply T:%u I:%u", type, id);
297 if (id != expected_id)
298 fatal("ID mismatch (%u != %u)", id, expected_id);
299 if (type == SSH2_FXP_STATUS) {
300 int status = buffer_get_int(&msg);
301
302 if (quiet)
303 debug("Couldn't statvfs: %s", fx2txt(status));
304 else
305 error("Couldn't statvfs: %s", fx2txt(status));
306 buffer_free(&msg);
307 return -1;
308 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
309 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
310 SSH2_FXP_EXTENDED_REPLY, type);
311 }
312
Damien Miller1d2c4562014-02-04 11:18:20 +1100313 memset(st, 0, sizeof(*st));
Darren Tucker7b598892008-06-09 22:49:36 +1000314 st->f_bsize = buffer_get_int64(&msg);
315 st->f_frsize = buffer_get_int64(&msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000316 st->f_blocks = buffer_get_int64(&msg);
317 st->f_bfree = buffer_get_int64(&msg);
318 st->f_bavail = buffer_get_int64(&msg);
319 st->f_files = buffer_get_int64(&msg);
320 st->f_ffree = buffer_get_int64(&msg);
321 st->f_favail = buffer_get_int64(&msg);
Darren Tucker294b8412008-06-08 12:57:08 +1000322 st->f_fsid = buffer_get_int64(&msg);
Darren Tucker7b598892008-06-09 22:49:36 +1000323 flag = buffer_get_int64(&msg);
324 st->f_namemax = buffer_get_int64(&msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000325
326 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
327 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
328
329 buffer_free(&msg);
330
331 return 0;
332}
333
Damien Miller3db5f532002-02-13 14:10:32 +1100334struct sftp_conn *
Damien Miller65e42f82010-09-24 22:15:11 +1000335do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
336 u_int64_t limit_kbps)
Damien Miller33804262001-02-04 23:20:18 +1100337{
Damien Miller65e42f82010-09-24 22:15:11 +1000338 u_int type;
Damien Miller33804262001-02-04 23:20:18 +1100339 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100340 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100341
Damien Millerfec029f2013-08-21 02:42:12 +1000342 ret = xcalloc(1, sizeof(*ret));
343 ret->msg_id = 1;
Damien Miller65e42f82010-09-24 22:15:11 +1000344 ret->fd_in = fd_in;
345 ret->fd_out = fd_out;
346 ret->transfer_buflen = transfer_buflen;
347 ret->num_requests = num_requests;
348 ret->exts = 0;
349 ret->limit_kbps = 0;
350
Damien Miller33804262001-02-04 23:20:18 +1100351 buffer_init(&msg);
352 buffer_put_char(&msg, SSH2_FXP_INIT);
353 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller65e42f82010-09-24 22:15:11 +1000354 send_msg(ret, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100355
356 buffer_clear(&msg);
357
Damien Miller65e42f82010-09-24 22:15:11 +1000358 get_msg(ret, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100359
Kevin Stevesef4eea92001-02-05 12:42:17 +0000360 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100361 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000362 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100363 type);
364 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100365 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100366 }
Damien Miller65e42f82010-09-24 22:15:11 +1000367 ret->version = buffer_get_int(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100368
Damien Miller65e42f82010-09-24 22:15:11 +1000369 debug2("Remote version: %u", ret->version);
Damien Miller33804262001-02-04 23:20:18 +1100370
371 /* Check for extensions */
372 while (buffer_len(&msg) > 0) {
373 char *name = buffer_get_string(&msg, NULL);
374 char *value = buffer_get_string(&msg, NULL);
Darren Tuckera64ab332008-06-13 07:01:29 +1000375 int known = 0;
Damien Miller33804262001-02-04 23:20:18 +1100376
Damien Millerd671e5a2008-05-19 14:53:33 +1000377 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
Darren Tuckera64ab332008-06-13 07:01:29 +1000378 strcmp(value, "1") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000379 ret->exts |= SFTP_EXT_POSIX_RENAME;
Darren Tuckera64ab332008-06-13 07:01:29 +1000380 known = 1;
381 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
382 strcmp(value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000383 ret->exts |= SFTP_EXT_STATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000384 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100385 } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
Darren Tuckera64ab332008-06-13 07:01:29 +1000386 strcmp(value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000387 ret->exts |= SFTP_EXT_FSTATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000388 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100389 } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
390 strcmp(value, "1") == 0) {
391 ret->exts |= SFTP_EXT_HARDLINK;
392 known = 1;
Damien Millerf29238e2013-10-17 11:48:52 +1100393 } else if (strcmp(name, "fsync@openssh.com") == 0 &&
394 strcmp(value, "1") == 0) {
395 ret->exts |= SFTP_EXT_FSYNC;
396 known = 1;
Darren Tuckera64ab332008-06-13 07:01:29 +1000397 }
398 if (known) {
399 debug2("Server supports extension \"%s\" revision %s",
400 name, value);
401 } else {
402 debug2("Unrecognised server extension \"%s\"", name);
403 }
Darren Tuckera627d422013-06-02 07:31:17 +1000404 free(name);
405 free(value);
Damien Miller33804262001-02-04 23:20:18 +1100406 }
407
408 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100409
Damien Miller3db5f532002-02-13 14:10:32 +1100410 /* Some filexfer v.0 servers don't support large packets */
Damien Miller65e42f82010-09-24 22:15:11 +1000411 if (ret->version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000412 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100413
Damien Miller65e42f82010-09-24 22:15:11 +1000414 ret->limit_kbps = limit_kbps;
415 if (ret->limit_kbps > 0) {
416 bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
417 ret->transfer_buflen);
418 bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
419 ret->transfer_buflen);
420 }
421
422 return ret;
Damien Miller3db5f532002-02-13 14:10:32 +1100423}
424
425u_int
426sftp_proto_version(struct sftp_conn *conn)
427{
Damien Miller65e42f82010-09-24 22:15:11 +1000428 return conn->version;
Damien Miller33804262001-02-04 23:20:18 +1100429}
430
431int
Damien Miller3db5f532002-02-13 14:10:32 +1100432do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100433{
434 u_int id, status;
435 Buffer msg;
436
437 buffer_init(&msg);
438
Damien Miller3db5f532002-02-13 14:10:32 +1100439 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100440 buffer_put_char(&msg, SSH2_FXP_CLOSE);
441 buffer_put_int(&msg, id);
442 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000443 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000444 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100445
Damien Miller65e42f82010-09-24 22:15:11 +1000446 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100447 if (status != SSH2_FX_OK)
448 error("Couldn't close file: %s", fx2txt(status));
449
450 buffer_free(&msg);
451
Damien Miller65e42f82010-09-24 22:15:11 +1000452 return status;
Damien Miller33804262001-02-04 23:20:18 +1100453}
454
Damien Miller4870afd2001-03-14 10:27:09 +1100455
Ben Lindstrombba81212001-06-25 05:01:22 +0000456static int
Damien Millerbda5c842013-10-15 12:05:58 +1100457do_lsreaddir(struct sftp_conn *conn, char *path, int print_flag,
Damien Miller4870afd2001-03-14 10:27:09 +1100458 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100459{
460 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000461 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100462 char *handle;
Damien Miller39392072013-12-07 10:31:08 +1100463 int status = SSH2_FX_FAILURE;
464
465 if (dir)
466 *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +1100467
Damien Miller3db5f532002-02-13 14:10:32 +1100468 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100469
470 buffer_init(&msg);
471 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
472 buffer_put_int(&msg, id);
473 buffer_put_cstring(&msg, path);
Damien Miller65e42f82010-09-24 22:15:11 +1000474 send_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100475
Damien Miller65e42f82010-09-24 22:15:11 +1000476 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +1100477 "remote readdir(\"%s\")", path);
Damien Miller57c38ac2011-09-22 21:42:45 +1000478 if (handle == NULL) {
479 buffer_free(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000480 return -1;
Damien Miller57c38ac2011-09-22 21:42:45 +1000481 }
Damien Miller33804262001-02-04 23:20:18 +1100482
Damien Miller4870afd2001-03-14 10:27:09 +1100483 if (dir) {
484 ents = 0;
Damien Miller6c81fee2013-11-08 12:19:55 +1100485 *dir = xcalloc(1, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100486 (*dir)[0] = NULL;
487 }
Damien Miller4870afd2001-03-14 10:27:09 +1100488
Darren Tuckercdf547a2004-05-24 10:12:19 +1000489 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100490 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100491
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000492 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100493
494 buffer_clear(&msg);
495 buffer_put_char(&msg, SSH2_FXP_READDIR);
496 buffer_put_int(&msg, id);
497 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000498 send_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100499
500 buffer_clear(&msg);
501
Damien Miller65e42f82010-09-24 22:15:11 +1000502 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100503
504 type = buffer_get_char(&msg);
505 id = buffer_get_int(&msg);
506
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000507 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100508
509 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000510 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100511
512 if (type == SSH2_FXP_STATUS) {
Damien Miller39392072013-12-07 10:31:08 +1100513 status = buffer_get_int(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100514 debug3("Received SSH2_FXP_STATUS %d", status);
Damien Miller39392072013-12-07 10:31:08 +1100515 if (status == SSH2_FX_EOF)
Damien Miller33804262001-02-04 23:20:18 +1100516 break;
Damien Miller39392072013-12-07 10:31:08 +1100517 error("Couldn't read directory: %s", fx2txt(status));
518 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100519 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000520 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100521 SSH2_FXP_NAME, type);
522
523 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100524 if (count == 0)
525 break;
526 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100527 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100528 char *filename, *longname;
529 Attrib *a;
530
531 filename = buffer_get_string(&msg, NULL);
532 longname = buffer_get_string(&msg, NULL);
533 a = decode_attrib(&msg);
534
Damien Millerbda5c842013-10-15 12:05:58 +1100535 if (print_flag)
Damien Miller4870afd2001-03-14 10:27:09 +1100536 printf("%s\n", longname);
537
Darren Tucker1b0dd172009-10-07 08:37:48 +1100538 /*
539 * Directory entries should never contain '/'
540 * These can be used to attack recursive ops
541 * (e.g. send '../../../../etc/passwd')
542 */
543 if (strchr(filename, '/') != NULL) {
544 error("Server sent suspect path \"%s\" "
545 "during readdir of \"%s\"", filename, path);
Damien Miller39392072013-12-07 10:31:08 +1100546 } else if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100547 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller6c81fee2013-11-08 12:19:55 +1100548 (*dir)[ents] = xcalloc(1, sizeof(***dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100549 (*dir)[ents]->filename = xstrdup(filename);
550 (*dir)[ents]->longname = xstrdup(longname);
551 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
552 (*dir)[++ents] = NULL;
553 }
Darren Tuckera627d422013-06-02 07:31:17 +1000554 free(filename);
555 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100556 }
557 }
Damien Miller39392072013-12-07 10:31:08 +1100558 status = 0;
Damien Miller33804262001-02-04 23:20:18 +1100559
Damien Miller39392072013-12-07 10:31:08 +1100560 out:
Damien Miller33804262001-02-04 23:20:18 +1100561 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100562 do_close(conn, handle, handle_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000563 free(handle);
Damien Miller33804262001-02-04 23:20:18 +1100564
Damien Miller39392072013-12-07 10:31:08 +1100565 if (status != 0 && dir != NULL) {
566 /* Don't return results on error */
567 free_sftp_dirents(*dir);
568 *dir = NULL;
569 } else if (interrupted && dir != NULL && *dir != NULL) {
570 /* Don't return partial matches on interrupt */
Darren Tuckercdf547a2004-05-24 10:12:19 +1000571 free_sftp_dirents(*dir);
Damien Miller6c81fee2013-11-08 12:19:55 +1100572 *dir = xcalloc(1, sizeof(**dir));
Darren Tuckercdf547a2004-05-24 10:12:19 +1000573 **dir = NULL;
574 }
575
Damien Miller39392072013-12-07 10:31:08 +1100576 return status;
Damien Miller33804262001-02-04 23:20:18 +1100577}
578
579int
Damien Miller3db5f532002-02-13 14:10:32 +1100580do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100581{
Damien Miller3db5f532002-02-13 14:10:32 +1100582 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100583}
584
585void free_sftp_dirents(SFTP_DIRENT **s)
586{
587 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100588
Damien Miller39392072013-12-07 10:31:08 +1100589 if (s == NULL)
590 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100591 for (i = 0; s[i]; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000592 free(s[i]->filename);
593 free(s[i]->longname);
594 free(s[i]);
Damien Miller4870afd2001-03-14 10:27:09 +1100595 }
Darren Tuckera627d422013-06-02 07:31:17 +1000596 free(s);
Damien Miller4870afd2001-03-14 10:27:09 +1100597}
598
599int
Damien Miller3db5f532002-02-13 14:10:32 +1100600do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100601{
602 u_int status, id;
603
604 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
605
Damien Miller3db5f532002-02-13 14:10:32 +1100606 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000607 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
608 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100609 if (status != SSH2_FX_OK)
610 error("Couldn't delete file: %s", fx2txt(status));
611 return(status);
612}
613
614int
Damien Millerbda5c842013-10-15 12:05:58 +1100615do_mkdir(struct sftp_conn *conn, char *path, Attrib *a, int print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100616{
617 u_int status, id;
618
Damien Miller3db5f532002-02-13 14:10:32 +1100619 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000620 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100621 strlen(path), a);
622
Damien Miller65e42f82010-09-24 22:15:11 +1000623 status = get_status(conn, id);
Damien Millerbda5c842013-10-15 12:05:58 +1100624 if (status != SSH2_FX_OK && print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100625 error("Couldn't create directory: %s", fx2txt(status));
626
627 return(status);
628}
629
630int
Damien Miller3db5f532002-02-13 14:10:32 +1100631do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100632{
633 u_int status, id;
634
Damien Miller3db5f532002-02-13 14:10:32 +1100635 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000636 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100637 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100638
Damien Miller65e42f82010-09-24 22:15:11 +1000639 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100640 if (status != SSH2_FX_OK)
641 error("Couldn't remove directory: %s", fx2txt(status));
642
643 return(status);
644}
645
646Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100647do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100648{
649 u_int id;
650
Damien Miller3db5f532002-02-13 14:10:32 +1100651 id = conn->msg_id++;
652
Damien Miller65e42f82010-09-24 22:15:11 +1000653 send_string_request(conn, id,
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000654 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100655 path, strlen(path));
656
Damien Miller65e42f82010-09-24 22:15:11 +1000657 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100658}
659
660Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100661do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100662{
663 u_int id;
664
Damien Miller3db5f532002-02-13 14:10:32 +1100665 if (conn->version == 0) {
666 if (quiet)
667 debug("Server version does not support lstat operation");
668 else
Damien Miller996acd22003-04-09 20:59:48 +1000669 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000670 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100671 }
672
673 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000674 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100675 strlen(path));
676
Damien Miller65e42f82010-09-24 22:15:11 +1000677 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100678}
679
Damien Millercfe23d32008-02-10 22:20:44 +1100680#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100681Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100682do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100683{
684 u_int id;
685
Damien Miller3db5f532002-02-13 14:10:32 +1100686 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000687 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
Damien Miller3db5f532002-02-13 14:10:32 +1100688 handle_len);
689
Damien Miller65e42f82010-09-24 22:15:11 +1000690 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100691}
Damien Millercfe23d32008-02-10 22:20:44 +1100692#endif
Damien Miller33804262001-02-04 23:20:18 +1100693
694int
Damien Miller3db5f532002-02-13 14:10:32 +1100695do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100696{
697 u_int status, id;
698
Damien Miller3db5f532002-02-13 14:10:32 +1100699 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000700 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100701 strlen(path), a);
702
Damien Miller65e42f82010-09-24 22:15:11 +1000703 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100704 if (status != SSH2_FX_OK)
705 error("Couldn't setstat on \"%s\": %s", path,
706 fx2txt(status));
707
708 return(status);
709}
710
711int
Damien Miller3db5f532002-02-13 14:10:32 +1100712do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100713 Attrib *a)
714{
715 u_int status, id;
716
Damien Miller3db5f532002-02-13 14:10:32 +1100717 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000718 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100719 handle_len, a);
720
Damien Miller65e42f82010-09-24 22:15:11 +1000721 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100722 if (status != SSH2_FX_OK)
723 error("Couldn't fsetstat: %s", fx2txt(status));
724
725 return(status);
726}
727
728char *
Damien Miller3db5f532002-02-13 14:10:32 +1100729do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100730{
731 Buffer msg;
732 u_int type, expected_id, count, id;
733 char *filename, *longname;
734 Attrib *a;
735
Damien Miller3db5f532002-02-13 14:10:32 +1100736 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000737 send_string_request(conn, id, SSH2_FXP_REALPATH, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100738 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100739
740 buffer_init(&msg);
741
Damien Miller65e42f82010-09-24 22:15:11 +1000742 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100743 type = buffer_get_char(&msg);
744 id = buffer_get_int(&msg);
745
746 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000747 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100748
749 if (type == SSH2_FXP_STATUS) {
750 u_int status = buffer_get_int(&msg);
751
Damien Millerf29238e2013-10-17 11:48:52 +1100752 error("Couldn't canonicalize: %s", fx2txt(status));
Damien Miller49566312010-06-26 09:38:23 +1000753 buffer_free(&msg);
754 return NULL;
Damien Miller33804262001-02-04 23:20:18 +1100755 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000756 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100757 SSH2_FXP_NAME, type);
758
759 count = buffer_get_int(&msg);
760 if (count != 1)
761 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
762
763 filename = buffer_get_string(&msg, NULL);
764 longname = buffer_get_string(&msg, NULL);
765 a = decode_attrib(&msg);
766
Darren Tucker4908d442012-07-02 22:15:38 +1000767 debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename,
768 (unsigned long)a->size);
Damien Miller33804262001-02-04 23:20:18 +1100769
Darren Tuckera627d422013-06-02 07:31:17 +1000770 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100771
772 buffer_free(&msg);
773
774 return(filename);
775}
776
777int
Damien Millerc7dba122013-08-21 02:41:15 +1000778do_rename(struct sftp_conn *conn, char *oldpath, char *newpath,
779 int force_legacy)
Damien Miller33804262001-02-04 23:20:18 +1100780{
781 Buffer msg;
782 u_int status, id;
Damien Millerc7dba122013-08-21 02:41:15 +1000783 int use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
Damien Miller33804262001-02-04 23:20:18 +1100784
785 buffer_init(&msg);
786
787 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100788 id = conn->msg_id++;
Damien Millerc7dba122013-08-21 02:41:15 +1000789 if (use_ext) {
Damien Miller7a3e1d02008-03-27 10:59:57 +1100790 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
791 buffer_put_int(&msg, id);
792 buffer_put_cstring(&msg, "posix-rename@openssh.com");
793 } else {
794 buffer_put_char(&msg, SSH2_FXP_RENAME);
795 buffer_put_int(&msg, id);
796 }
Damien Miller33804262001-02-04 23:20:18 +1100797 buffer_put_cstring(&msg, oldpath);
798 buffer_put_cstring(&msg, newpath);
Damien Miller65e42f82010-09-24 22:15:11 +1000799 send_msg(conn, &msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100800 debug3("Sent message %s \"%s\" -> \"%s\"",
Damien Millerc7dba122013-08-21 02:41:15 +1000801 use_ext ? "posix-rename@openssh.com" : "SSH2_FXP_RENAME",
802 oldpath, newpath);
Damien Miller33804262001-02-04 23:20:18 +1100803 buffer_free(&msg);
804
Damien Miller65e42f82010-09-24 22:15:11 +1000805 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100806 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100807 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
808 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100809
810 return(status);
811}
812
813int
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100814do_hardlink(struct sftp_conn *conn, char *oldpath, char *newpath)
815{
816 Buffer msg;
817 u_int status, id;
818
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100819 if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
820 error("Server does not support hardlink@openssh.com extension");
821 return -1;
822 }
823
Damien Miller3decdba2011-09-22 21:41:05 +1000824 buffer_init(&msg);
825
826 /* Send link request */
827 id = conn->msg_id++;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100828 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
829 buffer_put_int(&msg, id);
830 buffer_put_cstring(&msg, "hardlink@openssh.com");
831 buffer_put_cstring(&msg, oldpath);
832 buffer_put_cstring(&msg, newpath);
833 send_msg(conn, &msg);
834 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
835 oldpath, newpath);
836 buffer_free(&msg);
837
838 status = get_status(conn, id);
839 if (status != SSH2_FX_OK)
840 error("Couldn't link file \"%s\" to \"%s\": %s", oldpath,
841 newpath, fx2txt(status));
842
843 return(status);
844}
845
846int
Damien Miller3db5f532002-02-13 14:10:32 +1100847do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100848{
849 Buffer msg;
850 u_int status, id;
851
Damien Miller3db5f532002-02-13 14:10:32 +1100852 if (conn->version < 3) {
853 error("This server does not support the symlink operation");
854 return(SSH2_FX_OP_UNSUPPORTED);
855 }
856
Damien Miller058316f2001-03-08 10:08:49 +1100857 buffer_init(&msg);
858
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000859 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100860 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100861 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
862 buffer_put_int(&msg, id);
863 buffer_put_cstring(&msg, oldpath);
864 buffer_put_cstring(&msg, newpath);
Damien Miller65e42f82010-09-24 22:15:11 +1000865 send_msg(conn, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100866 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
867 newpath);
868 buffer_free(&msg);
869
Damien Miller65e42f82010-09-24 22:15:11 +1000870 status = get_status(conn, id);
Damien Miller058316f2001-03-08 10:08:49 +1100871 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000872 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100873 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100874
875 return(status);
876}
877
Damien Millerf29238e2013-10-17 11:48:52 +1100878int
879do_fsync(struct sftp_conn *conn, char *handle, u_int handle_len)
880{
881 Buffer msg;
882 u_int status, id;
883
884 /* Silently return if the extension is not supported */
885 if ((conn->exts & SFTP_EXT_FSYNC) == 0)
886 return -1;
887
888 buffer_init(&msg);
889
890 /* Send fsync request */
891 id = conn->msg_id++;
892
893 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
894 buffer_put_int(&msg, id);
895 buffer_put_cstring(&msg, "fsync@openssh.com");
896 buffer_put_string(&msg, handle, handle_len);
897 send_msg(conn, &msg);
898 debug3("Sent message fsync@openssh.com I:%u", id);
899 buffer_free(&msg);
900
901 status = get_status(conn, id);
902 if (status != SSH2_FX_OK)
903 error("Couldn't sync file: %s", fx2txt(status));
904
905 return status;
906}
907
Damien Millercfe23d32008-02-10 22:20:44 +1100908#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +1100909char *
Damien Miller3db5f532002-02-13 14:10:32 +1100910do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100911{
912 Buffer msg;
913 u_int type, expected_id, count, id;
914 char *filename, *longname;
915 Attrib *a;
916
Damien Miller3db5f532002-02-13 14:10:32 +1100917 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000918 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100919
920 buffer_init(&msg);
921
Damien Miller65e42f82010-09-24 22:15:11 +1000922 get_msg(conn, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100923 type = buffer_get_char(&msg);
924 id = buffer_get_int(&msg);
925
926 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000927 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100928
929 if (type == SSH2_FXP_STATUS) {
930 u_int status = buffer_get_int(&msg);
931
932 error("Couldn't readlink: %s", fx2txt(status));
Damien Miller3decdba2011-09-22 21:41:05 +1000933 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100934 return(NULL);
935 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000936 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100937 SSH2_FXP_NAME, type);
938
939 count = buffer_get_int(&msg);
940 if (count != 1)
941 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
942
943 filename = buffer_get_string(&msg, NULL);
944 longname = buffer_get_string(&msg, NULL);
945 a = decode_attrib(&msg);
946
947 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
948
Darren Tuckera627d422013-06-02 07:31:17 +1000949 free(longname);
Damien Miller058316f2001-03-08 10:08:49 +1100950
951 buffer_free(&msg);
952
953 return(filename);
954}
Damien Millercfe23d32008-02-10 22:20:44 +1100955#endif
Damien Miller058316f2001-03-08 10:08:49 +1100956
Damien Millerd671e5a2008-05-19 14:53:33 +1000957int
Darren Tucker7b598892008-06-09 22:49:36 +1000958do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
Damien Millerd671e5a2008-05-19 14:53:33 +1000959 int quiet)
960{
961 Buffer msg;
962 u_int id;
963
964 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
965 error("Server does not support statvfs@openssh.com extension");
966 return -1;
967 }
968
969 id = conn->msg_id++;
970
971 buffer_init(&msg);
972 buffer_clear(&msg);
973 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
974 buffer_put_int(&msg, id);
975 buffer_put_cstring(&msg, "statvfs@openssh.com");
976 buffer_put_cstring(&msg, path);
Damien Miller65e42f82010-09-24 22:15:11 +1000977 send_msg(conn, &msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000978 buffer_free(&msg);
979
Damien Miller65e42f82010-09-24 22:15:11 +1000980 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +1000981}
982
983#ifdef notyet
984int
985do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
Darren Tucker7b598892008-06-09 22:49:36 +1000986 struct sftp_statvfs *st, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000987{
988 Buffer msg;
989 u_int id;
990
991 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
992 error("Server does not support fstatvfs@openssh.com extension");
993 return -1;
994 }
995
996 id = conn->msg_id++;
997
998 buffer_init(&msg);
999 buffer_clear(&msg);
1000 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
1001 buffer_put_int(&msg, id);
1002 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
1003 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +10001004 send_msg(conn, &msg);
Damien Millerd671e5a2008-05-19 14:53:33 +10001005 buffer_free(&msg);
1006
Damien Miller65e42f82010-09-24 22:15:11 +10001007 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +10001008}
1009#endif
1010
Damien Miller16a13332002-02-13 14:03:56 +11001011static void
Damien Miller65e42f82010-09-24 22:15:11 +10001012send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
1013 u_int len, char *handle, u_int handle_len)
Damien Miller16a13332002-02-13 14:03:56 +11001014{
1015 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001016
Damien Miller16a13332002-02-13 14:03:56 +11001017 buffer_init(&msg);
1018 buffer_clear(&msg);
1019 buffer_put_char(&msg, SSH2_FXP_READ);
1020 buffer_put_int(&msg, id);
1021 buffer_put_string(&msg, handle, handle_len);
1022 buffer_put_int64(&msg, offset);
1023 buffer_put_int(&msg, len);
Damien Miller65e42f82010-09-24 22:15:11 +10001024 send_msg(conn, &msg);
Damien Miller16a13332002-02-13 14:03:56 +11001025 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001026}
Damien Miller16a13332002-02-13 14:03:56 +11001027
Damien Miller058316f2001-03-08 10:08:49 +11001028int
Damien Miller3db5f532002-02-13 14:10:32 +11001029do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
Damien Millerf29238e2013-10-17 11:48:52 +11001030 Attrib *a, int preserve_flag, int resume_flag, int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001031{
Darren Tucker1b0dd172009-10-07 08:37:48 +11001032 Attrib junk;
Damien Miller16a13332002-02-13 14:03:56 +11001033 Buffer msg;
1034 char *handle;
Damien Miller0d032412013-07-25 11:56:52 +10001035 int local_fd = -1, status = 0, write_error;
1036 int read_error, write_errno, reordered = 0;
1037 u_int64_t offset = 0, size, highwater;
Damien Millereccb9de2005-06-17 12:59:34 +10001038 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +11001039 off_t progress_counter;
Damien Miller0d032412013-07-25 11:56:52 +10001040 struct stat st;
Damien Miller16a13332002-02-13 14:03:56 +11001041 struct request {
1042 u_int id;
1043 u_int len;
1044 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001045 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +11001046 };
1047 TAILQ_HEAD(reqhead, request) requests;
1048 struct request *req;
1049
1050 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +11001051
Darren Tucker1b0dd172009-10-07 08:37:48 +11001052 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1053 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001054
Damien Miller9e720282008-06-29 22:46:35 +10001055 /* Do not preserve set[ug]id here, as we do not preserve ownership */
Damien Miller33804262001-02-04 23:20:18 +11001056 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +11001057 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +11001058 else
1059 mode = 0666;
1060
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001061 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +11001062 (!S_ISREG(a->perm))) {
1063 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001064 return(-1);
1065 }
1066
Damien Miller16a13332002-02-13 14:03:56 +11001067 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1068 size = a->size;
1069 else
1070 size = 0;
1071
Damien Miller3db5f532002-02-13 14:10:32 +11001072 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +11001073 buffer_init(&msg);
1074
1075 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001076 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001077 buffer_put_char(&msg, SSH2_FXP_OPEN);
1078 buffer_put_int(&msg, id);
1079 buffer_put_cstring(&msg, remote_path);
1080 buffer_put_int(&msg, SSH2_FXF_READ);
1081 attrib_clear(&junk); /* Send empty attributes */
1082 encode_attrib(&msg, &junk);
Damien Miller65e42f82010-09-24 22:15:11 +10001083 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001084 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001085
Damien Miller65e42f82010-09-24 22:15:11 +10001086 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001087 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001088 if (handle == NULL) {
1089 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +11001090 return(-1);
1091 }
1092
Damien Millerbda5c842013-10-15 12:05:58 +11001093 local_fd = open(local_path,
1094 O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
Damien Miller3db5f532002-02-13 14:10:32 +11001095 if (local_fd == -1) {
1096 error("Couldn't open local file \"%s\" for writing: %s",
1097 local_path, strerror(errno));
Damien Miller0d032412013-07-25 11:56:52 +10001098 goto fail;
1099 }
1100 offset = highwater = 0;
Damien Millerbda5c842013-10-15 12:05:58 +11001101 if (resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001102 if (fstat(local_fd, &st) == -1) {
1103 error("Unable to stat local file \"%s\": %s",
1104 local_path, strerror(errno));
1105 goto fail;
1106 }
Damien Millerc3d483f2014-01-17 11:20:26 +11001107 if (st.st_size < 0) {
1108 error("\"%s\" has negative size", local_path);
1109 goto fail;
1110 }
1111 if ((u_int64_t)st.st_size > size) {
Damien Miller0d032412013-07-25 11:56:52 +10001112 error("Unable to resume download of \"%s\": "
1113 "local file is larger than remote", local_path);
1114 fail:
1115 do_close(conn, handle, handle_len);
1116 buffer_free(&msg);
1117 free(handle);
Damien Miller9275df32013-12-05 10:26:32 +11001118 if (local_fd != -1)
1119 close(local_fd);
Damien Miller0d032412013-07-25 11:56:52 +10001120 return -1;
1121 }
1122 offset = highwater = st.st_size;
Damien Miller3db5f532002-02-13 14:10:32 +11001123 }
1124
Damien Miller33804262001-02-04 23:20:18 +11001125 /* Read from remote and write to local */
Damien Miller0d032412013-07-25 11:56:52 +10001126 write_error = read_error = write_errno = num_req = 0;
Damien Miller16a13332002-02-13 14:03:56 +11001127 max_req = 1;
Damien Miller0d032412013-07-25 11:56:52 +10001128 progress_counter = offset;
Damien Miller62d57f62003-01-10 21:43:24 +11001129
Damien Miller9ba30692004-03-08 23:12:02 +11001130 if (showprogress && size != 0)
1131 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001132
Damien Miller16a13332002-02-13 14:03:56 +11001133 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +11001134 char *data;
Damien Miller16a13332002-02-13 14:03:56 +11001135 u_int len;
Damien Miller33804262001-02-04 23:20:18 +11001136
Darren Tuckercdf547a2004-05-24 10:12:19 +10001137 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001138 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001139 * allow outstanding requests to drain gracefully
1140 */
1141 if (interrupted) {
1142 if (num_req == 0) /* If we haven't started yet... */
1143 break;
1144 max_req = 0;
1145 }
1146
Damien Miller16a13332002-02-13 14:03:56 +11001147 /* Send some more requests */
1148 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001149 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +00001150 (unsigned long long)offset,
1151 (unsigned long long)offset + buflen - 1,
1152 num_req, max_req);
Damien Miller6c81fee2013-11-08 12:19:55 +11001153 req = xcalloc(1, sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +11001154 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001155 req->len = buflen;
1156 req->offset = offset;
1157 offset += buflen;
1158 num_req++;
1159 TAILQ_INSERT_TAIL(&requests, req, tq);
Damien Miller65e42f82010-09-24 22:15:11 +10001160 send_read_request(conn, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001161 req->len, handle, handle_len);
1162 }
Damien Miller33804262001-02-04 23:20:18 +11001163
1164 buffer_clear(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +10001165 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +11001166 type = buffer_get_char(&msg);
1167 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001168 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001169
Damien Miller16a13332002-02-13 14:03:56 +11001170 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001171 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001172 req != NULL && req->id != id;
1173 req = TAILQ_NEXT(req, tq))
1174 ;
1175 if (req == NULL)
1176 fatal("Unexpected reply %u", id);
1177
1178 switch (type) {
1179 case SSH2_FXP_STATUS:
1180 status = buffer_get_int(&msg);
1181 if (status != SSH2_FX_EOF)
1182 read_error = 1;
1183 max_req = 0;
1184 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001185 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001186 num_req--;
1187 break;
1188 case SSH2_FXP_DATA:
1189 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +00001190 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001191 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001192 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001193 if (len > req->len)
1194 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +00001195 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001196 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001197 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001198 !write_error) {
1199 write_errno = errno;
1200 write_error = 1;
1201 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001202 }
Damien Miller0d032412013-07-25 11:56:52 +10001203 else if (!reordered && req->offset <= highwater)
1204 highwater = req->offset + len;
1205 else if (!reordered && req->offset > highwater)
1206 reordered = 1;
Damien Miller62d57f62003-01-10 21:43:24 +11001207 progress_counter += len;
Darren Tuckera627d422013-06-02 07:31:17 +10001208 free(data);
Damien Miller16a13332002-02-13 14:03:56 +11001209
1210 if (len == req->len) {
1211 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001212 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001213 num_req--;
1214 } else {
1215 /* Resend the request for the missing data */
1216 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001217 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001218 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001219 (unsigned long long)req->offset +
1220 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001221 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001222 req->len -= len;
1223 req->offset += len;
Damien Miller65e42f82010-09-24 22:15:11 +10001224 send_read_request(conn, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001225 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001226 /* Reduce the request size */
1227 if (len < buflen)
1228 buflen = MAX(MIN_READ_SIZE, len);
1229 }
1230 if (max_req > 0) { /* max_req = 0 iff EOF received */
1231 if (size > 0 && offset > size) {
1232 /* Only one request at a time
1233 * after the expected EOF */
1234 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001235 (unsigned long long)offset,
1236 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001237 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001238 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001239 ++max_req;
1240 }
1241 }
1242 break;
1243 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001244 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001245 SSH2_FXP_DATA, type);
1246 }
Damien Miller33804262001-02-04 23:20:18 +11001247 }
Damien Miller33804262001-02-04 23:20:18 +11001248
Damien Miller62d57f62003-01-10 21:43:24 +11001249 if (showprogress && size)
1250 stop_progress_meter();
1251
Damien Miller16a13332002-02-13 14:03:56 +11001252 /* Sanity check */
1253 if (TAILQ_FIRST(&requests) != NULL)
1254 fatal("Transfer complete, but requests still in queue");
Damien Miller0d032412013-07-25 11:56:52 +10001255 /* Truncate at highest contiguous point to avoid holes on interrupt */
1256 if (read_error || write_error || interrupted) {
Damien Millerbda5c842013-10-15 12:05:58 +11001257 if (reordered && resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001258 error("Unable to resume download of \"%s\": "
1259 "server reordered requests", local_path);
1260 }
1261 debug("truncating at %llu", (unsigned long long)highwater);
1262 ftruncate(local_fd, highwater);
1263 }
Damien Miller16a13332002-02-13 14:03:56 +11001264 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001265 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001266 remote_path, fx2txt(status));
Damien Millerfec029f2013-08-21 02:42:12 +10001267 status = -1;
Damien Miller3db5f532002-02-13 14:10:32 +11001268 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001269 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001270 error("Couldn't write to \"%s\": %s", local_path,
1271 strerror(write_errno));
1272 status = -1;
1273 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001274 } else {
Damien Miller3db5f532002-02-13 14:10:32 +11001275 status = do_close(conn, handle, handle_len);
Damien Millerfec029f2013-08-21 02:42:12 +10001276 if (interrupted || status != SSH2_FX_OK)
Damien Miller0d032412013-07-25 11:56:52 +10001277 status = -1;
Damien Miller16a13332002-02-13 14:03:56 +11001278 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001279#ifdef HAVE_FCHMOD
Damien Millerbda5c842013-10-15 12:05:58 +11001280 if (preserve_flag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001281#else
Damien Millerbda5c842013-10-15 12:05:58 +11001282 if (preserve_flag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001283#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001284 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001285 strerror(errno));
Damien Millerbda5c842013-10-15 12:05:58 +11001286 if (preserve_flag &&
1287 (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
Damien Miller16a13332002-02-13 14:03:56 +11001288 struct timeval tv[2];
1289 tv[0].tv_sec = a->atime;
1290 tv[1].tv_sec = a->mtime;
1291 tv[0].tv_usec = tv[1].tv_usec = 0;
1292 if (utimes(local_path, tv) == -1)
1293 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001294 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001295 }
Damien Millerf29238e2013-10-17 11:48:52 +11001296 if (fsync_flag) {
1297 debug("syncing \"%s\"", local_path);
1298 if (fsync(local_fd) == -1)
1299 error("Couldn't sync file \"%s\": %s",
1300 local_path, strerror(errno));
1301 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001302 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001303 close(local_fd);
1304 buffer_free(&msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001305 free(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001306
1307 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001308}
1309
Darren Tucker1b0dd172009-10-07 08:37:48 +11001310static int
Damien Millerbda5c842013-10-15 12:05:58 +11001311download_dir_internal(struct sftp_conn *conn, char *src, char *dst, int depth,
Damien Millerf29238e2013-10-17 11:48:52 +11001312 Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1313 int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001314{
1315 int i, ret = 0;
1316 SFTP_DIRENT **dir_entries;
1317 char *filename, *new_src, *new_dst;
1318 mode_t mode = 0777;
1319
1320 if (depth >= MAX_DIR_DEPTH) {
1321 error("Maximum directory depth exceeded: %d levels", depth);
1322 return -1;
1323 }
1324
1325 if (dirattrib == NULL &&
1326 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1327 error("Unable to stat remote directory \"%s\"", src);
1328 return -1;
1329 }
1330 if (!S_ISDIR(dirattrib->perm)) {
1331 error("\"%s\" is not a directory", src);
1332 return -1;
1333 }
Damien Millerbda5c842013-10-15 12:05:58 +11001334 if (print_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001335 printf("Retrieving %s\n", src);
1336
1337 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1338 mode = dirattrib->perm & 01777;
1339 else {
1340 debug("Server did not send permissions for "
1341 "directory \"%s\"", dst);
1342 }
1343
1344 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1345 error("mkdir %s: %s", dst, strerror(errno));
1346 return -1;
1347 }
1348
1349 if (do_readdir(conn, src, &dir_entries) == -1) {
1350 error("%s: Failed to get directory contents", src);
1351 return -1;
1352 }
1353
1354 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1355 filename = dir_entries[i]->filename;
1356
1357 new_dst = path_append(dst, filename);
1358 new_src = path_append(src, filename);
1359
1360 if (S_ISDIR(dir_entries[i]->a.perm)) {
1361 if (strcmp(filename, ".") == 0 ||
1362 strcmp(filename, "..") == 0)
1363 continue;
1364 if (download_dir_internal(conn, new_src, new_dst,
Damien Millerbda5c842013-10-15 12:05:58 +11001365 depth + 1, &(dir_entries[i]->a), preserve_flag,
Damien Millerf29238e2013-10-17 11:48:52 +11001366 print_flag, resume_flag, fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001367 ret = -1;
1368 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1369 if (do_download(conn, new_src, new_dst,
Damien Millerf29238e2013-10-17 11:48:52 +11001370 &(dir_entries[i]->a), preserve_flag,
1371 resume_flag, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001372 error("Download of file %s to %s failed",
1373 new_src, new_dst);
1374 ret = -1;
1375 }
1376 } else
1377 logit("%s: not a regular file\n", new_src);
1378
Darren Tuckera627d422013-06-02 07:31:17 +10001379 free(new_dst);
1380 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001381 }
1382
Damien Millerbda5c842013-10-15 12:05:58 +11001383 if (preserve_flag) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001384 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1385 struct timeval tv[2];
1386 tv[0].tv_sec = dirattrib->atime;
1387 tv[1].tv_sec = dirattrib->mtime;
1388 tv[0].tv_usec = tv[1].tv_usec = 0;
1389 if (utimes(dst, tv) == -1)
1390 error("Can't set times on \"%s\": %s",
1391 dst, strerror(errno));
1392 } else
1393 debug("Server did not send times for directory "
1394 "\"%s\"", dst);
1395 }
1396
1397 free_sftp_dirents(dir_entries);
1398
1399 return ret;
1400}
1401
1402int
1403download_dir(struct sftp_conn *conn, char *src, char *dst,
Damien Millerf29238e2013-10-17 11:48:52 +11001404 Attrib *dirattrib, int preserve_flag, int print_flag,
1405 int resume_flag, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001406{
1407 char *src_canon;
1408 int ret;
1409
1410 if ((src_canon = do_realpath(conn, src)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001411 error("Unable to canonicalize path \"%s\"", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001412 return -1;
1413 }
1414
Damien Millerbda5c842013-10-15 12:05:58 +11001415 ret = download_dir_internal(conn, src_canon, dst, 0,
Damien Millerf29238e2013-10-17 11:48:52 +11001416 dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag);
Darren Tuckera627d422013-06-02 07:31:17 +10001417 free(src_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001418 return ret;
1419}
1420
Damien Miller33804262001-02-04 23:20:18 +11001421int
Damien Miller3db5f532002-02-13 14:10:32 +11001422do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
Damien Millerd8accc02014-05-15 13:46:25 +10001423 int preserve_flag, int resume, int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001424{
Damien Milleracdf25b2008-02-10 22:27:24 +11001425 int local_fd;
1426 int status = SSH2_FX_OK;
Damien Miller5873dfd2002-02-13 14:04:37 +11001427 u_int handle_len, id, type;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001428 off_t offset, progress_counter;
Damien Miller8829d362002-02-08 22:04:05 +11001429 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +11001430 Buffer msg;
1431 struct stat sb;
Damien Millerd8accc02014-05-15 13:46:25 +10001432 Attrib a, *c = NULL;
Damien Miller16a13332002-02-13 14:03:56 +11001433 u_int32_t startid;
1434 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001435 struct outstanding_ack {
1436 u_int id;
1437 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001438 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001439 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001440 };
1441 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001442 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001443
1444 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001445
1446 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1447 error("Couldn't open local file \"%s\" for reading: %s",
1448 local_path, strerror(errno));
1449 return(-1);
1450 }
1451 if (fstat(local_fd, &sb) == -1) {
1452 error("Couldn't fstat local file \"%s\": %s",
1453 local_path, strerror(errno));
1454 close(local_fd);
1455 return(-1);
1456 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001457 if (!S_ISREG(sb.st_mode)) {
1458 error("%s is not a regular file", local_path);
1459 close(local_fd);
1460 return(-1);
1461 }
Damien Miller33804262001-02-04 23:20:18 +11001462 stat_to_attrib(&sb, &a);
1463
1464 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1465 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1466 a.perm &= 0777;
Damien Millerbda5c842013-10-15 12:05:58 +11001467 if (!preserve_flag)
Damien Miller33804262001-02-04 23:20:18 +11001468 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1469
Damien Millerd8accc02014-05-15 13:46:25 +10001470 if (resume) {
1471 /* Get remote file size if it exists */
1472 if ((c = do_stat(conn, remote_path, 0)) == NULL) {
1473 close(local_fd);
1474 return -1;
1475 }
1476
1477 if ((off_t)c->size >= sb.st_size) {
1478 error("destination file bigger or same size as "
1479 "source file");
1480 close(local_fd);
1481 return -1;
1482 }
1483
1484 if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
1485 close(local_fd);
1486 return -1;
1487 }
1488 }
1489
Damien Miller33804262001-02-04 23:20:18 +11001490 buffer_init(&msg);
1491
1492 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001493 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001494 buffer_put_char(&msg, SSH2_FXP_OPEN);
1495 buffer_put_int(&msg, id);
1496 buffer_put_cstring(&msg, remote_path);
Damien Millerd8accc02014-05-15 13:46:25 +10001497 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|
1498 (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC));
Damien Miller33804262001-02-04 23:20:18 +11001499 encode_attrib(&msg, &a);
Damien Miller65e42f82010-09-24 22:15:11 +10001500 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001501 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001502
1503 buffer_clear(&msg);
1504
Damien Miller65e42f82010-09-24 22:15:11 +10001505 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001506 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001507 if (handle == NULL) {
1508 close(local_fd);
1509 buffer_free(&msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001510 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001511 }
1512
Damien Miller16a13332002-02-13 14:03:56 +11001513 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001514 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001515
Damien Miller33804262001-02-04 23:20:18 +11001516 /* Read from local and write to remote */
Damien Millerd8accc02014-05-15 13:46:25 +10001517 offset = progress_counter = (resume ? c->size : 0);
Damien Miller62d57f62003-01-10 21:43:24 +11001518 if (showprogress)
Darren Tuckerc9a19912013-06-02 08:37:05 +10001519 start_progress_meter(local_path, sb.st_size,
1520 &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001521
Damien Miller9f0f5c62001-12-21 14:45:46 +11001522 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001523 int len;
Damien Miller33804262001-02-04 23:20:18 +11001524
1525 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001526 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001527 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001528 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001529 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001530 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001531 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001532 len = 0;
1533 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001534 len = read(local_fd, data, conn->transfer_buflen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001535 while ((len == -1) &&
1536 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
Damien Miller33804262001-02-04 23:20:18 +11001537
1538 if (len == -1)
1539 fatal("Couldn't read from \"%s\": %s", local_path,
1540 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001541
1542 if (len != 0) {
Damien Miller6c81fee2013-11-08 12:19:55 +11001543 ack = xcalloc(1, sizeof(*ack));
Damien Miller5873dfd2002-02-13 14:04:37 +11001544 ack->id = ++id;
1545 ack->offset = offset;
1546 ack->len = len;
1547 TAILQ_INSERT_TAIL(&acks, ack, tq);
1548
Damien Miller16a13332002-02-13 14:03:56 +11001549 buffer_clear(&msg);
1550 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001551 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001552 buffer_put_string(&msg, handle, handle_len);
1553 buffer_put_int64(&msg, offset);
1554 buffer_put_string(&msg, data, len);
Damien Miller65e42f82010-09-24 22:15:11 +10001555 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001556 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001557 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001558 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001559 break;
1560
Damien Miller5873dfd2002-02-13 14:04:37 +11001561 if (ack == NULL)
1562 fatal("Unexpected ACK %u", id);
1563
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001564 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001565 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001566 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001567
Damien Miller5873dfd2002-02-13 14:04:37 +11001568 buffer_clear(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +10001569 get_msg(conn, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001570 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001571 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001572
1573 if (type != SSH2_FXP_STATUS)
1574 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1575 "got %d", SSH2_FXP_STATUS, type);
1576
1577 status = buffer_get_int(&msg);
1578 debug3("SSH2_FXP_STATUS %d", status);
1579
1580 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001581 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001582 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001583 ack = TAILQ_NEXT(ack, tq))
1584 ;
1585 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001586 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001587 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001588 debug3("In write loop, ack for %u %u bytes at %lld",
1589 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001590 ++ackid;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001591 progress_counter += ack->len;
Darren Tuckera627d422013-06-02 07:31:17 +10001592 free(ack);
Damien Miller33804262001-02-04 23:20:18 +11001593 }
Damien Miller33804262001-02-04 23:20:18 +11001594 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001595 if (offset < 0)
1596 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001597 }
Damien Milleracdf25b2008-02-10 22:27:24 +11001598 buffer_free(&msg);
1599
Damien Miller62d57f62003-01-10 21:43:24 +11001600 if (showprogress)
1601 stop_progress_meter();
Darren Tuckera627d422013-06-02 07:31:17 +10001602 free(data);
Damien Miller33804262001-02-04 23:20:18 +11001603
Damien Milleracdf25b2008-02-10 22:27:24 +11001604 if (status != SSH2_FX_OK) {
1605 error("Couldn't write to remote file \"%s\": %s",
1606 remote_path, fx2txt(status));
1607 status = -1;
1608 }
1609
Damien Miller33804262001-02-04 23:20:18 +11001610 if (close(local_fd) == -1) {
1611 error("Couldn't close local file \"%s\": %s", local_path,
1612 strerror(errno));
Damien Millerd7686fd2001-02-10 00:40:03 +11001613 status = -1;
Damien Miller33804262001-02-04 23:20:18 +11001614 }
1615
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001616 /* Override umask and utimes if asked */
Damien Millerbda5c842013-10-15 12:05:58 +11001617 if (preserve_flag)
Damien Miller3db5f532002-02-13 14:10:32 +11001618 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001619
Damien Millerf29238e2013-10-17 11:48:52 +11001620 if (fsync_flag)
1621 (void)do_fsync(conn, handle, handle_len);
1622
Damien Milleracdf25b2008-02-10 22:27:24 +11001623 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
1624 status = -1;
Darren Tuckera627d422013-06-02 07:31:17 +10001625 free(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001626
1627 return status;
Damien Miller33804262001-02-04 23:20:18 +11001628}
Darren Tucker1b0dd172009-10-07 08:37:48 +11001629
1630static int
Damien Millerbda5c842013-10-15 12:05:58 +11001631upload_dir_internal(struct sftp_conn *conn, char *src, char *dst, int depth,
Damien Millerd8accc02014-05-15 13:46:25 +10001632 int preserve_flag, int print_flag, int resume, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001633{
1634 int ret = 0, status;
1635 DIR *dirp;
1636 struct dirent *dp;
1637 char *filename, *new_src, *new_dst;
1638 struct stat sb;
1639 Attrib a;
1640
1641 if (depth >= MAX_DIR_DEPTH) {
1642 error("Maximum directory depth exceeded: %d levels", depth);
1643 return -1;
1644 }
1645
1646 if (stat(src, &sb) == -1) {
1647 error("Couldn't stat directory \"%s\": %s",
1648 src, strerror(errno));
1649 return -1;
1650 }
1651 if (!S_ISDIR(sb.st_mode)) {
1652 error("\"%s\" is not a directory", src);
1653 return -1;
1654 }
Damien Millerbda5c842013-10-15 12:05:58 +11001655 if (print_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001656 printf("Entering %s\n", src);
1657
1658 attrib_clear(&a);
1659 stat_to_attrib(&sb, &a);
1660 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1661 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1662 a.perm &= 01777;
Damien Millerbda5c842013-10-15 12:05:58 +11001663 if (!preserve_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001664 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
Damien Miller0d032412013-07-25 11:56:52 +10001665
Darren Tucker1b0dd172009-10-07 08:37:48 +11001666 status = do_mkdir(conn, dst, &a, 0);
1667 /*
1668 * we lack a portable status for errno EEXIST,
1669 * so if we get a SSH2_FX_FAILURE back we must check
1670 * if it was created successfully.
1671 */
1672 if (status != SSH2_FX_OK) {
1673 if (status != SSH2_FX_FAILURE)
1674 return -1;
Damien Miller0d032412013-07-25 11:56:52 +10001675 if (do_stat(conn, dst, 0) == NULL)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001676 return -1;
1677 }
1678
1679 if ((dirp = opendir(src)) == NULL) {
1680 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1681 return -1;
1682 }
Damien Miller0d032412013-07-25 11:56:52 +10001683
Darren Tucker1b0dd172009-10-07 08:37:48 +11001684 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1685 if (dp->d_ino == 0)
1686 continue;
1687 filename = dp->d_name;
1688 new_dst = path_append(dst, filename);
1689 new_src = path_append(src, filename);
1690
Darren Tucker438b4732009-10-11 21:52:10 +11001691 if (lstat(new_src, &sb) == -1) {
1692 logit("%s: lstat failed: %s", filename,
1693 strerror(errno));
1694 ret = -1;
1695 } else if (S_ISDIR(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001696 if (strcmp(filename, ".") == 0 ||
1697 strcmp(filename, "..") == 0)
1698 continue;
1699
1700 if (upload_dir_internal(conn, new_src, new_dst,
Damien Millerd8accc02014-05-15 13:46:25 +10001701 depth + 1, preserve_flag, print_flag, resume,
Damien Millerf29238e2013-10-17 11:48:52 +11001702 fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001703 ret = -1;
Darren Tucker438b4732009-10-11 21:52:10 +11001704 } else if (S_ISREG(sb.st_mode)) {
Damien Millerbda5c842013-10-15 12:05:58 +11001705 if (do_upload(conn, new_src, new_dst,
Damien Millerd8accc02014-05-15 13:46:25 +10001706 preserve_flag, resume, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001707 error("Uploading of file %s to %s failed!",
1708 new_src, new_dst);
1709 ret = -1;
1710 }
1711 } else
1712 logit("%s: not a regular file\n", filename);
Darren Tuckera627d422013-06-02 07:31:17 +10001713 free(new_dst);
1714 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001715 }
1716
1717 do_setstat(conn, dst, &a);
1718
1719 (void) closedir(dirp);
1720 return ret;
1721}
1722
1723int
Damien Millerbda5c842013-10-15 12:05:58 +11001724upload_dir(struct sftp_conn *conn, char *src, char *dst, int preserve_flag,
Damien Millerd8accc02014-05-15 13:46:25 +10001725 int print_flag, int resume, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001726{
1727 char *dst_canon;
1728 int ret;
1729
1730 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001731 error("Unable to canonicalize path \"%s\"", dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001732 return -1;
1733 }
1734
Damien Miller2f93d052013-10-15 12:06:27 +11001735 ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
Damien Millerd8accc02014-05-15 13:46:25 +10001736 print_flag, resume, fsync_flag);
Damien Millerf29238e2013-10-17 11:48:52 +11001737
Darren Tuckera627d422013-06-02 07:31:17 +10001738 free(dst_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001739 return ret;
1740}
1741
1742char *
1743path_append(char *p1, char *p2)
1744{
1745 char *ret;
1746 size_t len = strlen(p1) + strlen(p2) + 2;
1747
1748 ret = xmalloc(len);
1749 strlcpy(ret, p1, len);
1750 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1751 strlcat(ret, "/", len);
1752 strlcat(ret, p2, len);
1753
1754 return(ret);
1755}
1756