blob: 9ecded04b352df248384a9d7b7ed9f5dcca56a16 [file] [log] [blame]
Damien Miller0600c702013-11-21 13:55:43 +11001/* $OpenBSD: sftp-client.c,v 1.109 2013/11/08 11:15:19 dtucker 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
313 bzero(st, 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;
463
Damien Miller3db5f532002-02-13 14:10:32 +1100464 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100465
466 buffer_init(&msg);
467 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
468 buffer_put_int(&msg, id);
469 buffer_put_cstring(&msg, path);
Damien Miller65e42f82010-09-24 22:15:11 +1000470 send_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100471
Damien Miller65e42f82010-09-24 22:15:11 +1000472 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +1100473 "remote readdir(\"%s\")", path);
Damien Miller57c38ac2011-09-22 21:42:45 +1000474 if (handle == NULL) {
475 buffer_free(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000476 return -1;
Damien Miller57c38ac2011-09-22 21:42:45 +1000477 }
Damien Miller33804262001-02-04 23:20:18 +1100478
Damien Miller4870afd2001-03-14 10:27:09 +1100479 if (dir) {
480 ents = 0;
Damien Miller6c81fee2013-11-08 12:19:55 +1100481 *dir = xcalloc(1, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100482 (*dir)[0] = NULL;
483 }
Damien Miller4870afd2001-03-14 10:27:09 +1100484
Darren Tuckercdf547a2004-05-24 10:12:19 +1000485 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100486 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100487
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000488 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100489
490 buffer_clear(&msg);
491 buffer_put_char(&msg, SSH2_FXP_READDIR);
492 buffer_put_int(&msg, id);
493 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000494 send_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100495
496 buffer_clear(&msg);
497
Damien Miller65e42f82010-09-24 22:15:11 +1000498 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100499
500 type = buffer_get_char(&msg);
501 id = buffer_get_int(&msg);
502
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000503 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100504
505 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000506 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100507
508 if (type == SSH2_FXP_STATUS) {
509 int status = buffer_get_int(&msg);
510
511 debug3("Received SSH2_FXP_STATUS %d", status);
512
513 if (status == SSH2_FX_EOF) {
514 break;
515 } else {
516 error("Couldn't read directory: %s",
517 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100518 do_close(conn, handle, handle_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000519 free(handle);
Damien Miller57c38ac2011-09-22 21:42:45 +1000520 buffer_free(&msg);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000521 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100522 }
523 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000524 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100525 SSH2_FXP_NAME, type);
526
527 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100528 if (count == 0)
529 break;
530 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100531 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100532 char *filename, *longname;
533 Attrib *a;
534
535 filename = buffer_get_string(&msg, NULL);
536 longname = buffer_get_string(&msg, NULL);
537 a = decode_attrib(&msg);
538
Damien Millerbda5c842013-10-15 12:05:58 +1100539 if (print_flag)
Damien Miller4870afd2001-03-14 10:27:09 +1100540 printf("%s\n", longname);
541
Darren Tucker1b0dd172009-10-07 08:37:48 +1100542 /*
543 * Directory entries should never contain '/'
544 * These can be used to attack recursive ops
545 * (e.g. send '../../../../etc/passwd')
546 */
547 if (strchr(filename, '/') != NULL) {
548 error("Server sent suspect path \"%s\" "
549 "during readdir of \"%s\"", filename, path);
550 goto next;
551 }
552
Damien Miller4870afd2001-03-14 10:27:09 +1100553 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100554 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller6c81fee2013-11-08 12:19:55 +1100555 (*dir)[ents] = xcalloc(1, sizeof(***dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100556 (*dir)[ents]->filename = xstrdup(filename);
557 (*dir)[ents]->longname = xstrdup(longname);
558 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
559 (*dir)[++ents] = NULL;
560 }
Darren Tucker1b0dd172009-10-07 08:37:48 +1100561 next:
Darren Tuckera627d422013-06-02 07:31:17 +1000562 free(filename);
563 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100564 }
565 }
566
567 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100568 do_close(conn, handle, handle_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000569 free(handle);
Damien Miller33804262001-02-04 23:20:18 +1100570
Darren Tuckercdf547a2004-05-24 10:12:19 +1000571 /* Don't return partial matches on interrupt */
572 if (interrupted && dir != NULL && *dir != NULL) {
573 free_sftp_dirents(*dir);
Damien Miller6c81fee2013-11-08 12:19:55 +1100574 *dir = xcalloc(1, sizeof(**dir));
Darren Tuckercdf547a2004-05-24 10:12:19 +1000575 **dir = NULL;
576 }
577
Damien Miller65e42f82010-09-24 22:15:11 +1000578 return 0;
Damien Miller33804262001-02-04 23:20:18 +1100579}
580
581int
Damien Miller3db5f532002-02-13 14:10:32 +1100582do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100583{
Damien Miller3db5f532002-02-13 14:10:32 +1100584 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100585}
586
587void free_sftp_dirents(SFTP_DIRENT **s)
588{
589 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100590
591 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 }
1107 if ((size_t)st.st_size > size) {
1108 error("Unable to resume download of \"%s\": "
1109 "local file is larger than remote", local_path);
1110 fail:
1111 do_close(conn, handle, handle_len);
1112 buffer_free(&msg);
1113 free(handle);
1114 return -1;
1115 }
1116 offset = highwater = st.st_size;
Damien Miller3db5f532002-02-13 14:10:32 +11001117 }
1118
Damien Miller33804262001-02-04 23:20:18 +11001119 /* Read from remote and write to local */
Damien Miller0d032412013-07-25 11:56:52 +10001120 write_error = read_error = write_errno = num_req = 0;
Damien Miller16a13332002-02-13 14:03:56 +11001121 max_req = 1;
Damien Miller0d032412013-07-25 11:56:52 +10001122 progress_counter = offset;
Damien Miller62d57f62003-01-10 21:43:24 +11001123
Damien Miller9ba30692004-03-08 23:12:02 +11001124 if (showprogress && size != 0)
1125 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001126
Damien Miller16a13332002-02-13 14:03:56 +11001127 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +11001128 char *data;
Damien Miller16a13332002-02-13 14:03:56 +11001129 u_int len;
Damien Miller33804262001-02-04 23:20:18 +11001130
Darren Tuckercdf547a2004-05-24 10:12:19 +10001131 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001132 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001133 * allow outstanding requests to drain gracefully
1134 */
1135 if (interrupted) {
1136 if (num_req == 0) /* If we haven't started yet... */
1137 break;
1138 max_req = 0;
1139 }
1140
Damien Miller16a13332002-02-13 14:03:56 +11001141 /* Send some more requests */
1142 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001143 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +00001144 (unsigned long long)offset,
1145 (unsigned long long)offset + buflen - 1,
1146 num_req, max_req);
Damien Miller6c81fee2013-11-08 12:19:55 +11001147 req = xcalloc(1, sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +11001148 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001149 req->len = buflen;
1150 req->offset = offset;
1151 offset += buflen;
1152 num_req++;
1153 TAILQ_INSERT_TAIL(&requests, req, tq);
Damien Miller65e42f82010-09-24 22:15:11 +10001154 send_read_request(conn, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001155 req->len, handle, handle_len);
1156 }
Damien Miller33804262001-02-04 23:20:18 +11001157
1158 buffer_clear(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +10001159 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +11001160 type = buffer_get_char(&msg);
1161 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001162 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001163
Damien Miller16a13332002-02-13 14:03:56 +11001164 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001165 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001166 req != NULL && req->id != id;
1167 req = TAILQ_NEXT(req, tq))
1168 ;
1169 if (req == NULL)
1170 fatal("Unexpected reply %u", id);
1171
1172 switch (type) {
1173 case SSH2_FXP_STATUS:
1174 status = buffer_get_int(&msg);
1175 if (status != SSH2_FX_EOF)
1176 read_error = 1;
1177 max_req = 0;
1178 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001179 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001180 num_req--;
1181 break;
1182 case SSH2_FXP_DATA:
1183 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +00001184 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001185 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001186 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001187 if (len > req->len)
1188 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +00001189 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001190 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001191 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001192 !write_error) {
1193 write_errno = errno;
1194 write_error = 1;
1195 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001196 }
Damien Miller0d032412013-07-25 11:56:52 +10001197 else if (!reordered && req->offset <= highwater)
1198 highwater = req->offset + len;
1199 else if (!reordered && req->offset > highwater)
1200 reordered = 1;
Damien Miller62d57f62003-01-10 21:43:24 +11001201 progress_counter += len;
Darren Tuckera627d422013-06-02 07:31:17 +10001202 free(data);
Damien Miller16a13332002-02-13 14:03:56 +11001203
1204 if (len == req->len) {
1205 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001206 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001207 num_req--;
1208 } else {
1209 /* Resend the request for the missing data */
1210 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001211 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001212 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001213 (unsigned long long)req->offset +
1214 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001215 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001216 req->len -= len;
1217 req->offset += len;
Damien Miller65e42f82010-09-24 22:15:11 +10001218 send_read_request(conn, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001219 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001220 /* Reduce the request size */
1221 if (len < buflen)
1222 buflen = MAX(MIN_READ_SIZE, len);
1223 }
1224 if (max_req > 0) { /* max_req = 0 iff EOF received */
1225 if (size > 0 && offset > size) {
1226 /* Only one request at a time
1227 * after the expected EOF */
1228 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001229 (unsigned long long)offset,
1230 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001231 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001232 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001233 ++max_req;
1234 }
1235 }
1236 break;
1237 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001238 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001239 SSH2_FXP_DATA, type);
1240 }
Damien Miller33804262001-02-04 23:20:18 +11001241 }
Damien Miller33804262001-02-04 23:20:18 +11001242
Damien Miller62d57f62003-01-10 21:43:24 +11001243 if (showprogress && size)
1244 stop_progress_meter();
1245
Damien Miller16a13332002-02-13 14:03:56 +11001246 /* Sanity check */
1247 if (TAILQ_FIRST(&requests) != NULL)
1248 fatal("Transfer complete, but requests still in queue");
Damien Miller0d032412013-07-25 11:56:52 +10001249 /* Truncate at highest contiguous point to avoid holes on interrupt */
1250 if (read_error || write_error || interrupted) {
Damien Millerbda5c842013-10-15 12:05:58 +11001251 if (reordered && resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001252 error("Unable to resume download of \"%s\": "
1253 "server reordered requests", local_path);
1254 }
1255 debug("truncating at %llu", (unsigned long long)highwater);
1256 ftruncate(local_fd, highwater);
1257 }
Damien Miller16a13332002-02-13 14:03:56 +11001258 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001259 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001260 remote_path, fx2txt(status));
Damien Millerfec029f2013-08-21 02:42:12 +10001261 status = -1;
Damien Miller3db5f532002-02-13 14:10:32 +11001262 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001263 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001264 error("Couldn't write to \"%s\": %s", local_path,
1265 strerror(write_errno));
1266 status = -1;
1267 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001268 } else {
Damien Miller3db5f532002-02-13 14:10:32 +11001269 status = do_close(conn, handle, handle_len);
Damien Millerfec029f2013-08-21 02:42:12 +10001270 if (interrupted || status != SSH2_FX_OK)
Damien Miller0d032412013-07-25 11:56:52 +10001271 status = -1;
Damien Miller16a13332002-02-13 14:03:56 +11001272 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001273#ifdef HAVE_FCHMOD
Damien Millerbda5c842013-10-15 12:05:58 +11001274 if (preserve_flag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001275#else
Damien Millerbda5c842013-10-15 12:05:58 +11001276 if (preserve_flag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001277#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001278 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001279 strerror(errno));
Damien Millerbda5c842013-10-15 12:05:58 +11001280 if (preserve_flag &&
1281 (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
Damien Miller16a13332002-02-13 14:03:56 +11001282 struct timeval tv[2];
1283 tv[0].tv_sec = a->atime;
1284 tv[1].tv_sec = a->mtime;
1285 tv[0].tv_usec = tv[1].tv_usec = 0;
1286 if (utimes(local_path, tv) == -1)
1287 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001288 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001289 }
Damien Millerf29238e2013-10-17 11:48:52 +11001290 if (fsync_flag) {
1291 debug("syncing \"%s\"", local_path);
1292 if (fsync(local_fd) == -1)
1293 error("Couldn't sync file \"%s\": %s",
1294 local_path, strerror(errno));
1295 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001296 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001297 close(local_fd);
1298 buffer_free(&msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001299 free(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001300
1301 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001302}
1303
Darren Tucker1b0dd172009-10-07 08:37:48 +11001304static int
Damien Millerbda5c842013-10-15 12:05:58 +11001305download_dir_internal(struct sftp_conn *conn, char *src, char *dst, int depth,
Damien Millerf29238e2013-10-17 11:48:52 +11001306 Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1307 int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001308{
1309 int i, ret = 0;
1310 SFTP_DIRENT **dir_entries;
1311 char *filename, *new_src, *new_dst;
1312 mode_t mode = 0777;
1313
1314 if (depth >= MAX_DIR_DEPTH) {
1315 error("Maximum directory depth exceeded: %d levels", depth);
1316 return -1;
1317 }
1318
1319 if (dirattrib == NULL &&
1320 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1321 error("Unable to stat remote directory \"%s\"", src);
1322 return -1;
1323 }
1324 if (!S_ISDIR(dirattrib->perm)) {
1325 error("\"%s\" is not a directory", src);
1326 return -1;
1327 }
Damien Millerbda5c842013-10-15 12:05:58 +11001328 if (print_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001329 printf("Retrieving %s\n", src);
1330
1331 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1332 mode = dirattrib->perm & 01777;
1333 else {
1334 debug("Server did not send permissions for "
1335 "directory \"%s\"", dst);
1336 }
1337
1338 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1339 error("mkdir %s: %s", dst, strerror(errno));
1340 return -1;
1341 }
1342
1343 if (do_readdir(conn, src, &dir_entries) == -1) {
1344 error("%s: Failed to get directory contents", src);
1345 return -1;
1346 }
1347
1348 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1349 filename = dir_entries[i]->filename;
1350
1351 new_dst = path_append(dst, filename);
1352 new_src = path_append(src, filename);
1353
1354 if (S_ISDIR(dir_entries[i]->a.perm)) {
1355 if (strcmp(filename, ".") == 0 ||
1356 strcmp(filename, "..") == 0)
1357 continue;
1358 if (download_dir_internal(conn, new_src, new_dst,
Damien Millerbda5c842013-10-15 12:05:58 +11001359 depth + 1, &(dir_entries[i]->a), preserve_flag,
Damien Millerf29238e2013-10-17 11:48:52 +11001360 print_flag, resume_flag, fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001361 ret = -1;
1362 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1363 if (do_download(conn, new_src, new_dst,
Damien Millerf29238e2013-10-17 11:48:52 +11001364 &(dir_entries[i]->a), preserve_flag,
1365 resume_flag, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001366 error("Download of file %s to %s failed",
1367 new_src, new_dst);
1368 ret = -1;
1369 }
1370 } else
1371 logit("%s: not a regular file\n", new_src);
1372
Darren Tuckera627d422013-06-02 07:31:17 +10001373 free(new_dst);
1374 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001375 }
1376
Damien Millerbda5c842013-10-15 12:05:58 +11001377 if (preserve_flag) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001378 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1379 struct timeval tv[2];
1380 tv[0].tv_sec = dirattrib->atime;
1381 tv[1].tv_sec = dirattrib->mtime;
1382 tv[0].tv_usec = tv[1].tv_usec = 0;
1383 if (utimes(dst, tv) == -1)
1384 error("Can't set times on \"%s\": %s",
1385 dst, strerror(errno));
1386 } else
1387 debug("Server did not send times for directory "
1388 "\"%s\"", dst);
1389 }
1390
1391 free_sftp_dirents(dir_entries);
1392
1393 return ret;
1394}
1395
1396int
1397download_dir(struct sftp_conn *conn, char *src, char *dst,
Damien Millerf29238e2013-10-17 11:48:52 +11001398 Attrib *dirattrib, int preserve_flag, int print_flag,
1399 int resume_flag, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001400{
1401 char *src_canon;
1402 int ret;
1403
1404 if ((src_canon = do_realpath(conn, src)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001405 error("Unable to canonicalize path \"%s\"", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001406 return -1;
1407 }
1408
Damien Millerbda5c842013-10-15 12:05:58 +11001409 ret = download_dir_internal(conn, src_canon, dst, 0,
Damien Millerf29238e2013-10-17 11:48:52 +11001410 dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag);
Darren Tuckera627d422013-06-02 07:31:17 +10001411 free(src_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001412 return ret;
1413}
1414
Damien Miller33804262001-02-04 23:20:18 +11001415int
Damien Miller3db5f532002-02-13 14:10:32 +11001416do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
Damien Millerf29238e2013-10-17 11:48:52 +11001417 int preserve_flag, int fsync_flag)
Damien Miller33804262001-02-04 23:20:18 +11001418{
Damien Milleracdf25b2008-02-10 22:27:24 +11001419 int local_fd;
1420 int status = SSH2_FX_OK;
Damien Miller5873dfd2002-02-13 14:04:37 +11001421 u_int handle_len, id, type;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001422 off_t offset, progress_counter;
Damien Miller8829d362002-02-08 22:04:05 +11001423 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +11001424 Buffer msg;
1425 struct stat sb;
1426 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +11001427 u_int32_t startid;
1428 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001429 struct outstanding_ack {
1430 u_int id;
1431 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001432 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001433 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001434 };
1435 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001436 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001437
1438 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001439
1440 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1441 error("Couldn't open local file \"%s\" for reading: %s",
1442 local_path, strerror(errno));
1443 return(-1);
1444 }
1445 if (fstat(local_fd, &sb) == -1) {
1446 error("Couldn't fstat local file \"%s\": %s",
1447 local_path, strerror(errno));
1448 close(local_fd);
1449 return(-1);
1450 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001451 if (!S_ISREG(sb.st_mode)) {
1452 error("%s is not a regular file", local_path);
1453 close(local_fd);
1454 return(-1);
1455 }
Damien Miller33804262001-02-04 23:20:18 +11001456 stat_to_attrib(&sb, &a);
1457
1458 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1459 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1460 a.perm &= 0777;
Damien Millerbda5c842013-10-15 12:05:58 +11001461 if (!preserve_flag)
Damien Miller33804262001-02-04 23:20:18 +11001462 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1463
1464 buffer_init(&msg);
1465
1466 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001467 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001468 buffer_put_char(&msg, SSH2_FXP_OPEN);
1469 buffer_put_int(&msg, id);
1470 buffer_put_cstring(&msg, remote_path);
1471 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1472 encode_attrib(&msg, &a);
Damien Miller65e42f82010-09-24 22:15:11 +10001473 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001474 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001475
1476 buffer_clear(&msg);
1477
Damien Miller65e42f82010-09-24 22:15:11 +10001478 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001479 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001480 if (handle == NULL) {
1481 close(local_fd);
1482 buffer_free(&msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001483 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001484 }
1485
Damien Miller16a13332002-02-13 14:03:56 +11001486 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001487 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001488
Damien Miller33804262001-02-04 23:20:18 +11001489 /* Read from local and write to remote */
Darren Tuckerc9a19912013-06-02 08:37:05 +10001490 offset = progress_counter = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001491 if (showprogress)
Darren Tuckerc9a19912013-06-02 08:37:05 +10001492 start_progress_meter(local_path, sb.st_size,
1493 &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001494
Damien Miller9f0f5c62001-12-21 14:45:46 +11001495 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001496 int len;
Damien Miller33804262001-02-04 23:20:18 +11001497
1498 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001499 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001500 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001501 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001502 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001503 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001504 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001505 len = 0;
1506 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001507 len = read(local_fd, data, conn->transfer_buflen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001508 while ((len == -1) &&
1509 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
Damien Miller33804262001-02-04 23:20:18 +11001510
1511 if (len == -1)
1512 fatal("Couldn't read from \"%s\": %s", local_path,
1513 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001514
1515 if (len != 0) {
Damien Miller6c81fee2013-11-08 12:19:55 +11001516 ack = xcalloc(1, sizeof(*ack));
Damien Miller5873dfd2002-02-13 14:04:37 +11001517 ack->id = ++id;
1518 ack->offset = offset;
1519 ack->len = len;
1520 TAILQ_INSERT_TAIL(&acks, ack, tq);
1521
Damien Miller16a13332002-02-13 14:03:56 +11001522 buffer_clear(&msg);
1523 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001524 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001525 buffer_put_string(&msg, handle, handle_len);
1526 buffer_put_int64(&msg, offset);
1527 buffer_put_string(&msg, data, len);
Damien Miller65e42f82010-09-24 22:15:11 +10001528 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001529 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001530 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001531 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001532 break;
1533
Damien Miller5873dfd2002-02-13 14:04:37 +11001534 if (ack == NULL)
1535 fatal("Unexpected ACK %u", id);
1536
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001537 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001538 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001539 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001540
Damien Miller5873dfd2002-02-13 14:04:37 +11001541 buffer_clear(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +10001542 get_msg(conn, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001543 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001544 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001545
1546 if (type != SSH2_FXP_STATUS)
1547 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1548 "got %d", SSH2_FXP_STATUS, type);
1549
1550 status = buffer_get_int(&msg);
1551 debug3("SSH2_FXP_STATUS %d", status);
1552
1553 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001554 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001555 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001556 ack = TAILQ_NEXT(ack, tq))
1557 ;
1558 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001559 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001560 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001561 debug3("In write loop, ack for %u %u bytes at %lld",
1562 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001563 ++ackid;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001564 progress_counter += ack->len;
Darren Tuckera627d422013-06-02 07:31:17 +10001565 free(ack);
Damien Miller33804262001-02-04 23:20:18 +11001566 }
Damien Miller33804262001-02-04 23:20:18 +11001567 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001568 if (offset < 0)
1569 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001570 }
Damien Milleracdf25b2008-02-10 22:27:24 +11001571 buffer_free(&msg);
1572
Damien Miller62d57f62003-01-10 21:43:24 +11001573 if (showprogress)
1574 stop_progress_meter();
Darren Tuckera627d422013-06-02 07:31:17 +10001575 free(data);
Damien Miller33804262001-02-04 23:20:18 +11001576
Damien Milleracdf25b2008-02-10 22:27:24 +11001577 if (status != SSH2_FX_OK) {
1578 error("Couldn't write to remote file \"%s\": %s",
1579 remote_path, fx2txt(status));
1580 status = -1;
1581 }
1582
Damien Miller33804262001-02-04 23:20:18 +11001583 if (close(local_fd) == -1) {
1584 error("Couldn't close local file \"%s\": %s", local_path,
1585 strerror(errno));
Damien Millerd7686fd2001-02-10 00:40:03 +11001586 status = -1;
Damien Miller33804262001-02-04 23:20:18 +11001587 }
1588
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001589 /* Override umask and utimes if asked */
Damien Millerbda5c842013-10-15 12:05:58 +11001590 if (preserve_flag)
Damien Miller3db5f532002-02-13 14:10:32 +11001591 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001592
Damien Millerf29238e2013-10-17 11:48:52 +11001593 if (fsync_flag)
1594 (void)do_fsync(conn, handle, handle_len);
1595
Damien Milleracdf25b2008-02-10 22:27:24 +11001596 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
1597 status = -1;
Darren Tuckera627d422013-06-02 07:31:17 +10001598 free(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001599
1600 return status;
Damien Miller33804262001-02-04 23:20:18 +11001601}
Darren Tucker1b0dd172009-10-07 08:37:48 +11001602
1603static int
Damien Millerbda5c842013-10-15 12:05:58 +11001604upload_dir_internal(struct sftp_conn *conn, char *src, char *dst, int depth,
Damien Millerf29238e2013-10-17 11:48:52 +11001605 int preserve_flag, int print_flag, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001606{
1607 int ret = 0, status;
1608 DIR *dirp;
1609 struct dirent *dp;
1610 char *filename, *new_src, *new_dst;
1611 struct stat sb;
1612 Attrib a;
1613
1614 if (depth >= MAX_DIR_DEPTH) {
1615 error("Maximum directory depth exceeded: %d levels", depth);
1616 return -1;
1617 }
1618
1619 if (stat(src, &sb) == -1) {
1620 error("Couldn't stat directory \"%s\": %s",
1621 src, strerror(errno));
1622 return -1;
1623 }
1624 if (!S_ISDIR(sb.st_mode)) {
1625 error("\"%s\" is not a directory", src);
1626 return -1;
1627 }
Damien Millerbda5c842013-10-15 12:05:58 +11001628 if (print_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001629 printf("Entering %s\n", src);
1630
1631 attrib_clear(&a);
1632 stat_to_attrib(&sb, &a);
1633 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1634 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1635 a.perm &= 01777;
Damien Millerbda5c842013-10-15 12:05:58 +11001636 if (!preserve_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001637 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
Damien Miller0d032412013-07-25 11:56:52 +10001638
Darren Tucker1b0dd172009-10-07 08:37:48 +11001639 status = do_mkdir(conn, dst, &a, 0);
1640 /*
1641 * we lack a portable status for errno EEXIST,
1642 * so if we get a SSH2_FX_FAILURE back we must check
1643 * if it was created successfully.
1644 */
1645 if (status != SSH2_FX_OK) {
1646 if (status != SSH2_FX_FAILURE)
1647 return -1;
Damien Miller0d032412013-07-25 11:56:52 +10001648 if (do_stat(conn, dst, 0) == NULL)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001649 return -1;
1650 }
1651
1652 if ((dirp = opendir(src)) == NULL) {
1653 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1654 return -1;
1655 }
Damien Miller0d032412013-07-25 11:56:52 +10001656
Darren Tucker1b0dd172009-10-07 08:37:48 +11001657 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1658 if (dp->d_ino == 0)
1659 continue;
1660 filename = dp->d_name;
1661 new_dst = path_append(dst, filename);
1662 new_src = path_append(src, filename);
1663
Darren Tucker438b4732009-10-11 21:52:10 +11001664 if (lstat(new_src, &sb) == -1) {
1665 logit("%s: lstat failed: %s", filename,
1666 strerror(errno));
1667 ret = -1;
1668 } else if (S_ISDIR(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001669 if (strcmp(filename, ".") == 0 ||
1670 strcmp(filename, "..") == 0)
1671 continue;
1672
1673 if (upload_dir_internal(conn, new_src, new_dst,
Damien Millerf29238e2013-10-17 11:48:52 +11001674 depth + 1, preserve_flag, print_flag,
1675 fsync_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001676 ret = -1;
Darren Tucker438b4732009-10-11 21:52:10 +11001677 } else if (S_ISREG(sb.st_mode)) {
Damien Millerbda5c842013-10-15 12:05:58 +11001678 if (do_upload(conn, new_src, new_dst,
Damien Millerf29238e2013-10-17 11:48:52 +11001679 preserve_flag, fsync_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001680 error("Uploading of file %s to %s failed!",
1681 new_src, new_dst);
1682 ret = -1;
1683 }
1684 } else
1685 logit("%s: not a regular file\n", filename);
Darren Tuckera627d422013-06-02 07:31:17 +10001686 free(new_dst);
1687 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001688 }
1689
1690 do_setstat(conn, dst, &a);
1691
1692 (void) closedir(dirp);
1693 return ret;
1694}
1695
1696int
Damien Millerbda5c842013-10-15 12:05:58 +11001697upload_dir(struct sftp_conn *conn, char *src, char *dst, int preserve_flag,
Damien Millerf29238e2013-10-17 11:48:52 +11001698 int print_flag, int fsync_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001699{
1700 char *dst_canon;
1701 int ret;
1702
1703 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
Damien Millerf29238e2013-10-17 11:48:52 +11001704 error("Unable to canonicalize path \"%s\"", dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001705 return -1;
1706 }
1707
Damien Miller2f93d052013-10-15 12:06:27 +11001708 ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
Damien Millerf29238e2013-10-17 11:48:52 +11001709 print_flag, fsync_flag);
1710
Darren Tuckera627d422013-06-02 07:31:17 +10001711 free(dst_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001712 return ret;
1713}
1714
1715char *
1716path_append(char *p1, char *p2)
1717{
1718 char *ret;
1719 size_t len = strlen(p1) + strlen(p2) + 2;
1720
1721 ret = xmalloc(len);
1722 strlcpy(ret, p1, len);
1723 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1724 strlcat(ret, "/", len);
1725 strlcat(ret, p2, len);
1726
1727 return(ret);
1728}
1729