blob: e815499fe09477b7b63213d4661e2edfeb7a0463 [file] [log] [blame]
Damien Millerbda5c842013-10-15 12:05:58 +11001/* $OpenBSD: sftp-client.c,v 1.105 2013/10/11 02:45:36 djm Exp $ */
Damien Miller33804262001-02-04 23:20:18 +11002/*
Damien Miller4e60ed72004-02-17 17:07:59 +11003 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
Damien Miller33804262001-02-04 23:20:18 +11004 *
Damien Miller4e60ed72004-02-17 17:07:59 +11005 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
Damien Miller33804262001-02-04 23:20:18 +11008 *
Damien Miller4e60ed72004-02-17 17:07:59 +11009 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller33804262001-02-04 23:20:18 +110016 */
17
18/* XXX: memleaks */
19/* XXX: signed vs unsigned */
Damien Miller3db5f532002-02-13 14:10:32 +110020/* XXX: remove all logging, only return status codes */
Damien Miller33804262001-02-04 23:20:18 +110021/* XXX: copy between two remote sites */
22
23#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110024
25#include <sys/types.h>
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 Millere3476ed2006-07-24 14:13:33 +100045#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100046#include <unistd.h>
Damien Miller16a13332002-02-13 14:03:56 +110047
Damien Miller33804262001-02-04 23:20:18 +110048#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100049#include "buffer.h"
Damien Miller33804262001-02-04 23:20:18 +110050#include "log.h"
51#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110052#include "progressmeter.h"
Damien Miller3f941882006-03-31 23:13:02 +110053#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110054
55#include "sftp.h"
56#include "sftp-common.h"
57#include "sftp-client.h"
58
Darren Tuckercdf547a2004-05-24 10:12:19 +100059extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110060extern int showprogress;
61
Damien Miller0c8d8f62006-03-15 11:34:25 +110062/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110063#define MIN_READ_SIZE 512
64
Darren Tucker1b0dd172009-10-07 08:37:48 +110065/* Maximum depth to descend in directory trees */
66#define MAX_DIR_DEPTH 64
67
Damien Miller3db5f532002-02-13 14:10:32 +110068struct sftp_conn {
69 int fd_in;
70 int fd_out;
71 u_int transfer_buflen;
72 u_int num_requests;
73 u_int version;
74 u_int msg_id;
Damien Millerd671e5a2008-05-19 14:53:33 +100075#define SFTP_EXT_POSIX_RENAME 0x00000001
76#define SFTP_EXT_STATVFS 0x00000002
77#define SFTP_EXT_FSTATVFS 0x00000004
Darren Tuckeraf1f9092010-12-05 09:02:47 +110078#define SFTP_EXT_HARDLINK 0x00000008
Damien Miller7a3e1d02008-03-27 10:59:57 +110079 u_int exts;
Damien Miller65e42f82010-09-24 22:15:11 +100080 u_int64_t limit_kbps;
81 struct bwlimit bwlimit_in, bwlimit_out;
Damien Miller3db5f532002-02-13 14:10:32 +110082};
Ben Lindstrom288cc392001-02-09 02:58:04 +000083
Darren Tuckerc22f0902009-10-07 08:24:19 +110084static char *
Damien Miller65e42f82010-09-24 22:15:11 +100085get_handle(struct sftp_conn *conn, u_int expected_id, u_int *len,
86 const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
87
88/* ARGSUSED */
89static int
90sftpio(void *_bwlimit, size_t amount)
91{
92 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
93
94 bandwidth_limit(bwlimit, amount);
95 return 0;
96}
Darren Tuckerc22f0902009-10-07 08:24:19 +110097
Ben Lindstrombba81212001-06-25 05:01:22 +000098static void
Damien Miller65e42f82010-09-24 22:15:11 +100099send_msg(struct sftp_conn *conn, Buffer *m)
Damien Miller33804262001-02-04 23:20:18 +1100100{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100101 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +1000102 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +1100103
Damien Miller54446182006-01-02 23:40:50 +1100104 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +1100105 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +1100106
Damien Millera7f3aaa2003-01-10 21:43:58 +1100107 /* Send length first */
Damien Miller3f941882006-03-31 23:13:02 +1100108 put_u32(mlen, buffer_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +1000109 iov[0].iov_base = mlen;
110 iov[0].iov_len = sizeof(mlen);
111 iov[1].iov_base = buffer_ptr(m);
112 iov[1].iov_len = buffer_len(m);
Damien Millerd7834352006-08-05 12:39:39 +1000113
Damien Miller65e42f82010-09-24 22:15:11 +1000114 if (atomiciov6(writev, conn->fd_out, iov, 2,
Damien Miller0d032412013-07-25 11:56:52 +1000115 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_out) !=
Damien Miller65e42f82010-09-24 22:15:11 +1000116 buffer_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +1100117 fatal("Couldn't send packet: %s", strerror(errno));
118
119 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +1100120}
121
Ben Lindstrombba81212001-06-25 05:01:22 +0000122static void
Damien Miller65e42f82010-09-24 22:15:11 +1000123get_msg(struct sftp_conn *conn, Buffer *m)
Damien Miller33804262001-02-04 23:20:18 +1100124{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100125 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +1100126
Damien Millera7f3aaa2003-01-10 21:43:58 +1100127 buffer_append_space(m, 4);
Damien Miller65e42f82010-09-24 22:15:11 +1000128 if (atomicio6(read, conn->fd_in, buffer_ptr(m), 4,
129 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in) != 4) {
Damien Millerb253cc42005-05-26 12:23:44 +1000130 if (errno == EPIPE)
131 fatal("Connection closed");
132 else
133 fatal("Couldn't read packet: %s", strerror(errno));
134 }
Damien Miller33804262001-02-04 23:20:18 +1100135
Damien Millera7f3aaa2003-01-10 21:43:58 +1100136 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +1100137 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000138 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100139
Damien Millera7f3aaa2003-01-10 21:43:58 +1100140 buffer_append_space(m, msg_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000141 if (atomicio6(read, conn->fd_in, buffer_ptr(m), msg_len,
142 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in)
143 != msg_len) {
Damien Millerb253cc42005-05-26 12:23:44 +1000144 if (errno == EPIPE)
145 fatal("Connection closed");
146 else
147 fatal("Read packet: %s", strerror(errno));
148 }
Damien Miller33804262001-02-04 23:20:18 +1100149}
150
Ben Lindstrombba81212001-06-25 05:01:22 +0000151static void
Damien Miller65e42f82010-09-24 22:15:11 +1000152send_string_request(struct sftp_conn *conn, u_int id, u_int code, char *s,
Damien Miller33804262001-02-04 23:20:18 +1100153 u_int len)
154{
155 Buffer msg;
156
157 buffer_init(&msg);
158 buffer_put_char(&msg, code);
159 buffer_put_int(&msg, id);
160 buffer_put_string(&msg, s, len);
Damien Miller65e42f82010-09-24 22:15:11 +1000161 send_msg(conn, &msg);
162 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100163 buffer_free(&msg);
164}
165
Ben Lindstrombba81212001-06-25 05:01:22 +0000166static void
Damien Miller65e42f82010-09-24 22:15:11 +1000167send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
168 char *s, u_int len, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100169{
170 Buffer msg;
171
172 buffer_init(&msg);
173 buffer_put_char(&msg, code);
174 buffer_put_int(&msg, id);
175 buffer_put_string(&msg, s, len);
176 encode_attrib(&msg, a);
Damien Miller65e42f82010-09-24 22:15:11 +1000177 send_msg(conn, &msg);
178 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100179 buffer_free(&msg);
180}
181
Ben Lindstrombba81212001-06-25 05:01:22 +0000182static u_int
Damien Miller65e42f82010-09-24 22:15:11 +1000183get_status(struct sftp_conn *conn, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100184{
185 Buffer msg;
186 u_int type, id, status;
187
188 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000189 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100190 type = buffer_get_char(&msg);
191 id = buffer_get_int(&msg);
192
193 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000194 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100195 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000196 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100197 SSH2_FXP_STATUS, type);
198
199 status = buffer_get_int(&msg);
200 buffer_free(&msg);
201
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000202 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100203
Damien Miller65e42f82010-09-24 22:15:11 +1000204 return status;
Damien Miller33804262001-02-04 23:20:18 +1100205}
206
Ben Lindstrombba81212001-06-25 05:01:22 +0000207static char *
Damien Miller65e42f82010-09-24 22:15:11 +1000208get_handle(struct sftp_conn *conn, u_int expected_id, u_int *len,
209 const char *errfmt, ...)
Damien Miller33804262001-02-04 23:20:18 +1100210{
211 Buffer msg;
212 u_int type, id;
Darren Tuckerc22f0902009-10-07 08:24:19 +1100213 char *handle, errmsg[256];
214 va_list args;
215 int status;
216
217 va_start(args, errfmt);
218 if (errfmt != NULL)
219 vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
220 va_end(args);
Damien Miller33804262001-02-04 23:20:18 +1100221
222 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000223 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100224 type = buffer_get_char(&msg);
225 id = buffer_get_int(&msg);
226
227 if (id != expected_id)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100228 fatal("%s: ID mismatch (%u != %u)",
229 errfmt == NULL ? __func__ : errmsg, id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100230 if (type == SSH2_FXP_STATUS) {
Darren Tuckerc22f0902009-10-07 08:24:19 +1100231 status = buffer_get_int(&msg);
232 if (errfmt != NULL)
233 error("%s: %s", errmsg, fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100234 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100235 return(NULL);
236 } else if (type != SSH2_FXP_HANDLE)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100237 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
238 errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
Damien Miller33804262001-02-04 23:20:18 +1100239
240 handle = buffer_get_string(&msg, len);
241 buffer_free(&msg);
242
243 return(handle);
244}
245
Ben Lindstrombba81212001-06-25 05:01:22 +0000246static Attrib *
Damien Miller65e42f82010-09-24 22:15:11 +1000247get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100248{
249 Buffer msg;
250 u_int type, id;
251 Attrib *a;
252
253 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000254 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100255
256 type = buffer_get_char(&msg);
257 id = buffer_get_int(&msg);
258
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000259 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100260 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000261 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100262 if (type == SSH2_FXP_STATUS) {
263 int status = buffer_get_int(&msg);
264
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000265 if (quiet)
266 debug("Couldn't stat remote file: %s", fx2txt(status));
267 else
268 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100269 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100270 return(NULL);
271 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000272 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100273 SSH2_FXP_ATTRS, type);
274 }
275 a = decode_attrib(&msg);
276 buffer_free(&msg);
277
278 return(a);
279}
280
Damien Millerd671e5a2008-05-19 14:53:33 +1000281static int
Damien Miller65e42f82010-09-24 22:15:11 +1000282get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
283 u_int expected_id, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000284{
285 Buffer msg;
286 u_int type, id, flag;
287
288 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000289 get_msg(conn, &msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000290
291 type = buffer_get_char(&msg);
292 id = buffer_get_int(&msg);
293
294 debug3("Received statvfs reply T:%u I:%u", type, id);
295 if (id != expected_id)
296 fatal("ID mismatch (%u != %u)", id, expected_id);
297 if (type == SSH2_FXP_STATUS) {
298 int status = buffer_get_int(&msg);
299
300 if (quiet)
301 debug("Couldn't statvfs: %s", fx2txt(status));
302 else
303 error("Couldn't statvfs: %s", fx2txt(status));
304 buffer_free(&msg);
305 return -1;
306 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
307 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
308 SSH2_FXP_EXTENDED_REPLY, type);
309 }
310
311 bzero(st, sizeof(*st));
Darren Tucker7b598892008-06-09 22:49:36 +1000312 st->f_bsize = buffer_get_int64(&msg);
313 st->f_frsize = buffer_get_int64(&msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000314 st->f_blocks = buffer_get_int64(&msg);
315 st->f_bfree = buffer_get_int64(&msg);
316 st->f_bavail = buffer_get_int64(&msg);
317 st->f_files = buffer_get_int64(&msg);
318 st->f_ffree = buffer_get_int64(&msg);
319 st->f_favail = buffer_get_int64(&msg);
Darren Tucker294b8412008-06-08 12:57:08 +1000320 st->f_fsid = buffer_get_int64(&msg);
Darren Tucker7b598892008-06-09 22:49:36 +1000321 flag = buffer_get_int64(&msg);
322 st->f_namemax = buffer_get_int64(&msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000323
324 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
325 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
326
327 buffer_free(&msg);
328
329 return 0;
330}
331
Damien Miller3db5f532002-02-13 14:10:32 +1100332struct sftp_conn *
Damien Miller65e42f82010-09-24 22:15:11 +1000333do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
334 u_int64_t limit_kbps)
Damien Miller33804262001-02-04 23:20:18 +1100335{
Damien Miller65e42f82010-09-24 22:15:11 +1000336 u_int type;
Damien Miller33804262001-02-04 23:20:18 +1100337 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100338 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100339
Damien Millerfec029f2013-08-21 02:42:12 +1000340 ret = xcalloc(1, sizeof(*ret));
341 ret->msg_id = 1;
Damien Miller65e42f82010-09-24 22:15:11 +1000342 ret->fd_in = fd_in;
343 ret->fd_out = fd_out;
344 ret->transfer_buflen = transfer_buflen;
345 ret->num_requests = num_requests;
346 ret->exts = 0;
347 ret->limit_kbps = 0;
348
Damien Miller33804262001-02-04 23:20:18 +1100349 buffer_init(&msg);
350 buffer_put_char(&msg, SSH2_FXP_INIT);
351 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller65e42f82010-09-24 22:15:11 +1000352 send_msg(ret, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100353
354 buffer_clear(&msg);
355
Damien Miller65e42f82010-09-24 22:15:11 +1000356 get_msg(ret, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100357
Kevin Stevesef4eea92001-02-05 12:42:17 +0000358 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100359 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000360 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100361 type);
362 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100363 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100364 }
Damien Miller65e42f82010-09-24 22:15:11 +1000365 ret->version = buffer_get_int(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100366
Damien Miller65e42f82010-09-24 22:15:11 +1000367 debug2("Remote version: %u", ret->version);
Damien Miller33804262001-02-04 23:20:18 +1100368
369 /* Check for extensions */
370 while (buffer_len(&msg) > 0) {
371 char *name = buffer_get_string(&msg, NULL);
372 char *value = buffer_get_string(&msg, NULL);
Darren Tuckera64ab332008-06-13 07:01:29 +1000373 int known = 0;
Damien Miller33804262001-02-04 23:20:18 +1100374
Damien Millerd671e5a2008-05-19 14:53:33 +1000375 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
Darren Tuckera64ab332008-06-13 07:01:29 +1000376 strcmp(value, "1") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000377 ret->exts |= SFTP_EXT_POSIX_RENAME;
Darren Tuckera64ab332008-06-13 07:01:29 +1000378 known = 1;
379 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
380 strcmp(value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000381 ret->exts |= SFTP_EXT_STATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000382 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100383 } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
Darren Tuckera64ab332008-06-13 07:01:29 +1000384 strcmp(value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000385 ret->exts |= SFTP_EXT_FSTATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000386 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100387 } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
388 strcmp(value, "1") == 0) {
389 ret->exts |= SFTP_EXT_HARDLINK;
390 known = 1;
Darren Tuckera64ab332008-06-13 07:01:29 +1000391 }
392 if (known) {
393 debug2("Server supports extension \"%s\" revision %s",
394 name, value);
395 } else {
396 debug2("Unrecognised server extension \"%s\"", name);
397 }
Darren Tuckera627d422013-06-02 07:31:17 +1000398 free(name);
399 free(value);
Damien Miller33804262001-02-04 23:20:18 +1100400 }
401
402 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100403
Damien Miller3db5f532002-02-13 14:10:32 +1100404 /* Some filexfer v.0 servers don't support large packets */
Damien Miller65e42f82010-09-24 22:15:11 +1000405 if (ret->version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000406 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100407
Damien Miller65e42f82010-09-24 22:15:11 +1000408 ret->limit_kbps = limit_kbps;
409 if (ret->limit_kbps > 0) {
410 bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
411 ret->transfer_buflen);
412 bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
413 ret->transfer_buflen);
414 }
415
416 return ret;
Damien Miller3db5f532002-02-13 14:10:32 +1100417}
418
419u_int
420sftp_proto_version(struct sftp_conn *conn)
421{
Damien Miller65e42f82010-09-24 22:15:11 +1000422 return conn->version;
Damien Miller33804262001-02-04 23:20:18 +1100423}
424
425int
Damien Miller3db5f532002-02-13 14:10:32 +1100426do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100427{
428 u_int id, status;
429 Buffer msg;
430
431 buffer_init(&msg);
432
Damien Miller3db5f532002-02-13 14:10:32 +1100433 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100434 buffer_put_char(&msg, SSH2_FXP_CLOSE);
435 buffer_put_int(&msg, id);
436 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000437 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000438 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100439
Damien Miller65e42f82010-09-24 22:15:11 +1000440 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100441 if (status != SSH2_FX_OK)
442 error("Couldn't close file: %s", fx2txt(status));
443
444 buffer_free(&msg);
445
Damien Miller65e42f82010-09-24 22:15:11 +1000446 return status;
Damien Miller33804262001-02-04 23:20:18 +1100447}
448
Damien Miller4870afd2001-03-14 10:27:09 +1100449
Ben Lindstrombba81212001-06-25 05:01:22 +0000450static int
Damien Millerbda5c842013-10-15 12:05:58 +1100451do_lsreaddir(struct sftp_conn *conn, char *path, int print_flag,
Damien Miller4870afd2001-03-14 10:27:09 +1100452 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100453{
454 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000455 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100456 char *handle;
457
Damien Miller3db5f532002-02-13 14:10:32 +1100458 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100459
460 buffer_init(&msg);
461 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
462 buffer_put_int(&msg, id);
463 buffer_put_cstring(&msg, path);
Damien Miller65e42f82010-09-24 22:15:11 +1000464 send_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100465
Damien Miller65e42f82010-09-24 22:15:11 +1000466 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +1100467 "remote readdir(\"%s\")", path);
Damien Miller57c38ac2011-09-22 21:42:45 +1000468 if (handle == NULL) {
469 buffer_free(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000470 return -1;
Damien Miller57c38ac2011-09-22 21:42:45 +1000471 }
Damien Miller33804262001-02-04 23:20:18 +1100472
Damien Miller4870afd2001-03-14 10:27:09 +1100473 if (dir) {
474 ents = 0;
475 *dir = xmalloc(sizeof(**dir));
476 (*dir)[0] = NULL;
477 }
Damien Miller4870afd2001-03-14 10:27:09 +1100478
Darren Tuckercdf547a2004-05-24 10:12:19 +1000479 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100480 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100481
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000482 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100483
484 buffer_clear(&msg);
485 buffer_put_char(&msg, SSH2_FXP_READDIR);
486 buffer_put_int(&msg, id);
487 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000488 send_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100489
490 buffer_clear(&msg);
491
Damien Miller65e42f82010-09-24 22:15:11 +1000492 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100493
494 type = buffer_get_char(&msg);
495 id = buffer_get_int(&msg);
496
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000497 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100498
499 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000500 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100501
502 if (type == SSH2_FXP_STATUS) {
503 int status = buffer_get_int(&msg);
504
505 debug3("Received SSH2_FXP_STATUS %d", status);
506
507 if (status == SSH2_FX_EOF) {
508 break;
509 } else {
510 error("Couldn't read directory: %s",
511 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100512 do_close(conn, handle, handle_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000513 free(handle);
Damien Miller57c38ac2011-09-22 21:42:45 +1000514 buffer_free(&msg);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000515 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100516 }
517 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000518 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100519 SSH2_FXP_NAME, type);
520
521 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100522 if (count == 0)
523 break;
524 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100525 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100526 char *filename, *longname;
527 Attrib *a;
528
529 filename = buffer_get_string(&msg, NULL);
530 longname = buffer_get_string(&msg, NULL);
531 a = decode_attrib(&msg);
532
Damien Millerbda5c842013-10-15 12:05:58 +1100533 if (print_flag)
Damien Miller4870afd2001-03-14 10:27:09 +1100534 printf("%s\n", longname);
535
Darren Tucker1b0dd172009-10-07 08:37:48 +1100536 /*
537 * Directory entries should never contain '/'
538 * These can be used to attack recursive ops
539 * (e.g. send '../../../../etc/passwd')
540 */
541 if (strchr(filename, '/') != NULL) {
542 error("Server sent suspect path \"%s\" "
543 "during readdir of \"%s\"", filename, path);
544 goto next;
545 }
546
Damien Miller4870afd2001-03-14 10:27:09 +1100547 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100548 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100549 (*dir)[ents] = xmalloc(sizeof(***dir));
550 (*dir)[ents]->filename = xstrdup(filename);
551 (*dir)[ents]->longname = xstrdup(longname);
552 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
553 (*dir)[++ents] = NULL;
554 }
Darren Tucker1b0dd172009-10-07 08:37:48 +1100555 next:
Darren Tuckera627d422013-06-02 07:31:17 +1000556 free(filename);
557 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100558 }
559 }
560
561 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
Darren Tuckercdf547a2004-05-24 10:12:19 +1000565 /* Don't return partial matches on interrupt */
566 if (interrupted && dir != NULL && *dir != NULL) {
567 free_sftp_dirents(*dir);
568 *dir = xmalloc(sizeof(**dir));
569 **dir = NULL;
570 }
571
Damien Miller65e42f82010-09-24 22:15:11 +1000572 return 0;
Damien Miller33804262001-02-04 23:20:18 +1100573}
574
575int
Damien Miller3db5f532002-02-13 14:10:32 +1100576do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100577{
Damien Miller3db5f532002-02-13 14:10:32 +1100578 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100579}
580
581void free_sftp_dirents(SFTP_DIRENT **s)
582{
583 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100584
585 for (i = 0; s[i]; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000586 free(s[i]->filename);
587 free(s[i]->longname);
588 free(s[i]);
Damien Miller4870afd2001-03-14 10:27:09 +1100589 }
Darren Tuckera627d422013-06-02 07:31:17 +1000590 free(s);
Damien Miller4870afd2001-03-14 10:27:09 +1100591}
592
593int
Damien Miller3db5f532002-02-13 14:10:32 +1100594do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100595{
596 u_int status, id;
597
598 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
599
Damien Miller3db5f532002-02-13 14:10:32 +1100600 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000601 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
602 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100603 if (status != SSH2_FX_OK)
604 error("Couldn't delete file: %s", fx2txt(status));
605 return(status);
606}
607
608int
Damien Millerbda5c842013-10-15 12:05:58 +1100609do_mkdir(struct sftp_conn *conn, char *path, Attrib *a, int print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100610{
611 u_int status, id;
612
Damien Miller3db5f532002-02-13 14:10:32 +1100613 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000614 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100615 strlen(path), a);
616
Damien Miller65e42f82010-09-24 22:15:11 +1000617 status = get_status(conn, id);
Damien Millerbda5c842013-10-15 12:05:58 +1100618 if (status != SSH2_FX_OK && print_flag)
Damien Miller33804262001-02-04 23:20:18 +1100619 error("Couldn't create directory: %s", fx2txt(status));
620
621 return(status);
622}
623
624int
Damien Miller3db5f532002-02-13 14:10:32 +1100625do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100626{
627 u_int status, id;
628
Damien Miller3db5f532002-02-13 14:10:32 +1100629 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000630 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100631 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100632
Damien Miller65e42f82010-09-24 22:15:11 +1000633 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100634 if (status != SSH2_FX_OK)
635 error("Couldn't remove directory: %s", fx2txt(status));
636
637 return(status);
638}
639
640Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100641do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100642{
643 u_int id;
644
Damien Miller3db5f532002-02-13 14:10:32 +1100645 id = conn->msg_id++;
646
Damien Miller65e42f82010-09-24 22:15:11 +1000647 send_string_request(conn, id,
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000648 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100649 path, strlen(path));
650
Damien Miller65e42f82010-09-24 22:15:11 +1000651 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100652}
653
654Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100655do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100656{
657 u_int id;
658
Damien Miller3db5f532002-02-13 14:10:32 +1100659 if (conn->version == 0) {
660 if (quiet)
661 debug("Server version does not support lstat operation");
662 else
Damien Miller996acd22003-04-09 20:59:48 +1000663 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000664 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100665 }
666
667 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000668 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100669 strlen(path));
670
Damien Miller65e42f82010-09-24 22:15:11 +1000671 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100672}
673
Damien Millercfe23d32008-02-10 22:20:44 +1100674#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100675Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100676do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100677{
678 u_int id;
679
Damien Miller3db5f532002-02-13 14:10:32 +1100680 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000681 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
Damien Miller3db5f532002-02-13 14:10:32 +1100682 handle_len);
683
Damien Miller65e42f82010-09-24 22:15:11 +1000684 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100685}
Damien Millercfe23d32008-02-10 22:20:44 +1100686#endif
Damien Miller33804262001-02-04 23:20:18 +1100687
688int
Damien Miller3db5f532002-02-13 14:10:32 +1100689do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100690{
691 u_int status, id;
692
Damien Miller3db5f532002-02-13 14:10:32 +1100693 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000694 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100695 strlen(path), a);
696
Damien Miller65e42f82010-09-24 22:15:11 +1000697 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100698 if (status != SSH2_FX_OK)
699 error("Couldn't setstat on \"%s\": %s", path,
700 fx2txt(status));
701
702 return(status);
703}
704
705int
Damien Miller3db5f532002-02-13 14:10:32 +1100706do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100707 Attrib *a)
708{
709 u_int status, id;
710
Damien Miller3db5f532002-02-13 14:10:32 +1100711 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000712 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100713 handle_len, a);
714
Damien Miller65e42f82010-09-24 22:15:11 +1000715 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100716 if (status != SSH2_FX_OK)
717 error("Couldn't fsetstat: %s", fx2txt(status));
718
719 return(status);
720}
721
722char *
Damien Miller3db5f532002-02-13 14:10:32 +1100723do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100724{
725 Buffer msg;
726 u_int type, expected_id, count, id;
727 char *filename, *longname;
728 Attrib *a;
729
Damien Miller3db5f532002-02-13 14:10:32 +1100730 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000731 send_string_request(conn, id, SSH2_FXP_REALPATH, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100732 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100733
734 buffer_init(&msg);
735
Damien Miller65e42f82010-09-24 22:15:11 +1000736 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100737 type = buffer_get_char(&msg);
738 id = buffer_get_int(&msg);
739
740 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000741 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100742
743 if (type == SSH2_FXP_STATUS) {
744 u_int status = buffer_get_int(&msg);
745
746 error("Couldn't canonicalise: %s", fx2txt(status));
Damien Miller49566312010-06-26 09:38:23 +1000747 buffer_free(&msg);
748 return NULL;
Damien Miller33804262001-02-04 23:20:18 +1100749 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000750 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100751 SSH2_FXP_NAME, type);
752
753 count = buffer_get_int(&msg);
754 if (count != 1)
755 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
756
757 filename = buffer_get_string(&msg, NULL);
758 longname = buffer_get_string(&msg, NULL);
759 a = decode_attrib(&msg);
760
Darren Tucker4908d442012-07-02 22:15:38 +1000761 debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename,
762 (unsigned long)a->size);
Damien Miller33804262001-02-04 23:20:18 +1100763
Darren Tuckera627d422013-06-02 07:31:17 +1000764 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100765
766 buffer_free(&msg);
767
768 return(filename);
769}
770
771int
Damien Millerc7dba122013-08-21 02:41:15 +1000772do_rename(struct sftp_conn *conn, char *oldpath, char *newpath,
773 int force_legacy)
Damien Miller33804262001-02-04 23:20:18 +1100774{
775 Buffer msg;
776 u_int status, id;
Damien Millerc7dba122013-08-21 02:41:15 +1000777 int use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
Damien Miller33804262001-02-04 23:20:18 +1100778
779 buffer_init(&msg);
780
781 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100782 id = conn->msg_id++;
Damien Millerc7dba122013-08-21 02:41:15 +1000783 if (use_ext) {
Damien Miller7a3e1d02008-03-27 10:59:57 +1100784 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
785 buffer_put_int(&msg, id);
786 buffer_put_cstring(&msg, "posix-rename@openssh.com");
787 } else {
788 buffer_put_char(&msg, SSH2_FXP_RENAME);
789 buffer_put_int(&msg, id);
790 }
Damien Miller33804262001-02-04 23:20:18 +1100791 buffer_put_cstring(&msg, oldpath);
792 buffer_put_cstring(&msg, newpath);
Damien Miller65e42f82010-09-24 22:15:11 +1000793 send_msg(conn, &msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100794 debug3("Sent message %s \"%s\" -> \"%s\"",
Damien Millerc7dba122013-08-21 02:41:15 +1000795 use_ext ? "posix-rename@openssh.com" : "SSH2_FXP_RENAME",
796 oldpath, newpath);
Damien Miller33804262001-02-04 23:20:18 +1100797 buffer_free(&msg);
798
Damien Miller65e42f82010-09-24 22:15:11 +1000799 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100800 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100801 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
802 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100803
804 return(status);
805}
806
807int
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100808do_hardlink(struct sftp_conn *conn, char *oldpath, char *newpath)
809{
810 Buffer msg;
811 u_int status, id;
812
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100813 if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
814 error("Server does not support hardlink@openssh.com extension");
815 return -1;
816 }
817
Damien Miller3decdba2011-09-22 21:41:05 +1000818 buffer_init(&msg);
819
820 /* Send link request */
821 id = conn->msg_id++;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100822 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
823 buffer_put_int(&msg, id);
824 buffer_put_cstring(&msg, "hardlink@openssh.com");
825 buffer_put_cstring(&msg, oldpath);
826 buffer_put_cstring(&msg, newpath);
827 send_msg(conn, &msg);
828 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
829 oldpath, newpath);
830 buffer_free(&msg);
831
832 status = get_status(conn, id);
833 if (status != SSH2_FX_OK)
834 error("Couldn't link file \"%s\" to \"%s\": %s", oldpath,
835 newpath, fx2txt(status));
836
837 return(status);
838}
839
840int
Damien Miller3db5f532002-02-13 14:10:32 +1100841do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100842{
843 Buffer msg;
844 u_int status, id;
845
Damien Miller3db5f532002-02-13 14:10:32 +1100846 if (conn->version < 3) {
847 error("This server does not support the symlink operation");
848 return(SSH2_FX_OP_UNSUPPORTED);
849 }
850
Damien Miller058316f2001-03-08 10:08:49 +1100851 buffer_init(&msg);
852
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000853 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100854 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100855 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
856 buffer_put_int(&msg, id);
857 buffer_put_cstring(&msg, oldpath);
858 buffer_put_cstring(&msg, newpath);
Damien Miller65e42f82010-09-24 22:15:11 +1000859 send_msg(conn, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100860 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
861 newpath);
862 buffer_free(&msg);
863
Damien Miller65e42f82010-09-24 22:15:11 +1000864 status = get_status(conn, id);
Damien Miller058316f2001-03-08 10:08:49 +1100865 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000866 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100867 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100868
869 return(status);
870}
871
Damien Millercfe23d32008-02-10 22:20:44 +1100872#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +1100873char *
Damien Miller3db5f532002-02-13 14:10:32 +1100874do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100875{
876 Buffer msg;
877 u_int type, expected_id, count, id;
878 char *filename, *longname;
879 Attrib *a;
880
Damien Miller3db5f532002-02-13 14:10:32 +1100881 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000882 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100883
884 buffer_init(&msg);
885
Damien Miller65e42f82010-09-24 22:15:11 +1000886 get_msg(conn, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100887 type = buffer_get_char(&msg);
888 id = buffer_get_int(&msg);
889
890 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000891 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100892
893 if (type == SSH2_FXP_STATUS) {
894 u_int status = buffer_get_int(&msg);
895
896 error("Couldn't readlink: %s", fx2txt(status));
Damien Miller3decdba2011-09-22 21:41:05 +1000897 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100898 return(NULL);
899 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000900 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100901 SSH2_FXP_NAME, type);
902
903 count = buffer_get_int(&msg);
904 if (count != 1)
905 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
906
907 filename = buffer_get_string(&msg, NULL);
908 longname = buffer_get_string(&msg, NULL);
909 a = decode_attrib(&msg);
910
911 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
912
Darren Tuckera627d422013-06-02 07:31:17 +1000913 free(longname);
Damien Miller058316f2001-03-08 10:08:49 +1100914
915 buffer_free(&msg);
916
917 return(filename);
918}
Damien Millercfe23d32008-02-10 22:20:44 +1100919#endif
Damien Miller058316f2001-03-08 10:08:49 +1100920
Damien Millerd671e5a2008-05-19 14:53:33 +1000921int
Darren Tucker7b598892008-06-09 22:49:36 +1000922do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
Damien Millerd671e5a2008-05-19 14:53:33 +1000923 int quiet)
924{
925 Buffer msg;
926 u_int id;
927
928 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
929 error("Server does not support statvfs@openssh.com extension");
930 return -1;
931 }
932
933 id = conn->msg_id++;
934
935 buffer_init(&msg);
936 buffer_clear(&msg);
937 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
938 buffer_put_int(&msg, id);
939 buffer_put_cstring(&msg, "statvfs@openssh.com");
940 buffer_put_cstring(&msg, path);
Damien Miller65e42f82010-09-24 22:15:11 +1000941 send_msg(conn, &msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000942 buffer_free(&msg);
943
Damien Miller65e42f82010-09-24 22:15:11 +1000944 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +1000945}
946
947#ifdef notyet
948int
949do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
Darren Tucker7b598892008-06-09 22:49:36 +1000950 struct sftp_statvfs *st, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000951{
952 Buffer msg;
953 u_int id;
954
955 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
956 error("Server does not support fstatvfs@openssh.com extension");
957 return -1;
958 }
959
960 id = conn->msg_id++;
961
962 buffer_init(&msg);
963 buffer_clear(&msg);
964 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
965 buffer_put_int(&msg, id);
966 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
967 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000968 send_msg(conn, &msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000969 buffer_free(&msg);
970
Damien Miller65e42f82010-09-24 22:15:11 +1000971 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +1000972}
973#endif
974
Damien Miller16a13332002-02-13 14:03:56 +1100975static void
Damien Miller65e42f82010-09-24 22:15:11 +1000976send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
977 u_int len, char *handle, u_int handle_len)
Damien Miller16a13332002-02-13 14:03:56 +1100978{
979 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000980
Damien Miller16a13332002-02-13 14:03:56 +1100981 buffer_init(&msg);
982 buffer_clear(&msg);
983 buffer_put_char(&msg, SSH2_FXP_READ);
984 buffer_put_int(&msg, id);
985 buffer_put_string(&msg, handle, handle_len);
986 buffer_put_int64(&msg, offset);
987 buffer_put_int(&msg, len);
Damien Miller65e42f82010-09-24 22:15:11 +1000988 send_msg(conn, &msg);
Damien Miller16a13332002-02-13 14:03:56 +1100989 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000990}
Damien Miller16a13332002-02-13 14:03:56 +1100991
Damien Miller058316f2001-03-08 10:08:49 +1100992int
Damien Miller3db5f532002-02-13 14:10:32 +1100993do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
Damien Millerbda5c842013-10-15 12:05:58 +1100994 Attrib *a, int preserve_flag, int resume_flag)
Damien Miller33804262001-02-04 23:20:18 +1100995{
Darren Tucker1b0dd172009-10-07 08:37:48 +1100996 Attrib junk;
Damien Miller16a13332002-02-13 14:03:56 +1100997 Buffer msg;
998 char *handle;
Damien Miller0d032412013-07-25 11:56:52 +1000999 int local_fd = -1, status = 0, write_error;
1000 int read_error, write_errno, reordered = 0;
1001 u_int64_t offset = 0, size, highwater;
Damien Millereccb9de2005-06-17 12:59:34 +10001002 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +11001003 off_t progress_counter;
Damien Miller0d032412013-07-25 11:56:52 +10001004 struct stat st;
Damien Miller16a13332002-02-13 14:03:56 +11001005 struct request {
1006 u_int id;
1007 u_int len;
1008 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001009 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +11001010 };
1011 TAILQ_HEAD(reqhead, request) requests;
1012 struct request *req;
1013
1014 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +11001015
Darren Tucker1b0dd172009-10-07 08:37:48 +11001016 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1017 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001018
Damien Miller9e720282008-06-29 22:46:35 +10001019 /* Do not preserve set[ug]id here, as we do not preserve ownership */
Damien Miller33804262001-02-04 23:20:18 +11001020 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +11001021 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +11001022 else
1023 mode = 0666;
1024
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001025 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +11001026 (!S_ISREG(a->perm))) {
1027 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001028 return(-1);
1029 }
1030
Damien Miller16a13332002-02-13 14:03:56 +11001031 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1032 size = a->size;
1033 else
1034 size = 0;
1035
Damien Miller3db5f532002-02-13 14:10:32 +11001036 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +11001037 buffer_init(&msg);
1038
1039 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001040 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001041 buffer_put_char(&msg, SSH2_FXP_OPEN);
1042 buffer_put_int(&msg, id);
1043 buffer_put_cstring(&msg, remote_path);
1044 buffer_put_int(&msg, SSH2_FXF_READ);
1045 attrib_clear(&junk); /* Send empty attributes */
1046 encode_attrib(&msg, &junk);
Damien Miller65e42f82010-09-24 22:15:11 +10001047 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001048 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001049
Damien Miller65e42f82010-09-24 22:15:11 +10001050 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001051 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001052 if (handle == NULL) {
1053 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +11001054 return(-1);
1055 }
1056
Damien Millerbda5c842013-10-15 12:05:58 +11001057 local_fd = open(local_path,
1058 O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
Damien Miller3db5f532002-02-13 14:10:32 +11001059 if (local_fd == -1) {
1060 error("Couldn't open local file \"%s\" for writing: %s",
1061 local_path, strerror(errno));
Damien Miller0d032412013-07-25 11:56:52 +10001062 goto fail;
1063 }
1064 offset = highwater = 0;
Damien Millerbda5c842013-10-15 12:05:58 +11001065 if (resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001066 if (fstat(local_fd, &st) == -1) {
1067 error("Unable to stat local file \"%s\": %s",
1068 local_path, strerror(errno));
1069 goto fail;
1070 }
1071 if ((size_t)st.st_size > size) {
1072 error("Unable to resume download of \"%s\": "
1073 "local file is larger than remote", local_path);
1074 fail:
1075 do_close(conn, handle, handle_len);
1076 buffer_free(&msg);
1077 free(handle);
1078 return -1;
1079 }
1080 offset = highwater = st.st_size;
Damien Miller3db5f532002-02-13 14:10:32 +11001081 }
1082
Damien Miller33804262001-02-04 23:20:18 +11001083 /* Read from remote and write to local */
Damien Miller0d032412013-07-25 11:56:52 +10001084 write_error = read_error = write_errno = num_req = 0;
Damien Miller16a13332002-02-13 14:03:56 +11001085 max_req = 1;
Damien Miller0d032412013-07-25 11:56:52 +10001086 progress_counter = offset;
Damien Miller62d57f62003-01-10 21:43:24 +11001087
Damien Miller9ba30692004-03-08 23:12:02 +11001088 if (showprogress && size != 0)
1089 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001090
Damien Miller16a13332002-02-13 14:03:56 +11001091 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +11001092 char *data;
Damien Miller16a13332002-02-13 14:03:56 +11001093 u_int len;
Damien Miller33804262001-02-04 23:20:18 +11001094
Darren Tuckercdf547a2004-05-24 10:12:19 +10001095 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001096 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001097 * allow outstanding requests to drain gracefully
1098 */
1099 if (interrupted) {
1100 if (num_req == 0) /* If we haven't started yet... */
1101 break;
1102 max_req = 0;
1103 }
1104
Damien Miller16a13332002-02-13 14:03:56 +11001105 /* Send some more requests */
1106 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001107 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +00001108 (unsigned long long)offset,
1109 (unsigned long long)offset + buflen - 1,
1110 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +11001111 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +11001112 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001113 req->len = buflen;
1114 req->offset = offset;
1115 offset += buflen;
1116 num_req++;
1117 TAILQ_INSERT_TAIL(&requests, req, tq);
Damien Miller65e42f82010-09-24 22:15:11 +10001118 send_read_request(conn, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001119 req->len, handle, handle_len);
1120 }
Damien Miller33804262001-02-04 23:20:18 +11001121
1122 buffer_clear(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +10001123 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +11001124 type = buffer_get_char(&msg);
1125 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001126 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001127
Damien Miller16a13332002-02-13 14:03:56 +11001128 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001129 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001130 req != NULL && req->id != id;
1131 req = TAILQ_NEXT(req, tq))
1132 ;
1133 if (req == NULL)
1134 fatal("Unexpected reply %u", id);
1135
1136 switch (type) {
1137 case SSH2_FXP_STATUS:
1138 status = buffer_get_int(&msg);
1139 if (status != SSH2_FX_EOF)
1140 read_error = 1;
1141 max_req = 0;
1142 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001143 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001144 num_req--;
1145 break;
1146 case SSH2_FXP_DATA:
1147 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +00001148 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001149 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001150 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001151 if (len > req->len)
1152 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +00001153 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001154 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001155 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001156 !write_error) {
1157 write_errno = errno;
1158 write_error = 1;
1159 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001160 }
Damien Miller0d032412013-07-25 11:56:52 +10001161 else if (!reordered && req->offset <= highwater)
1162 highwater = req->offset + len;
1163 else if (!reordered && req->offset > highwater)
1164 reordered = 1;
Damien Miller62d57f62003-01-10 21:43:24 +11001165 progress_counter += len;
Darren Tuckera627d422013-06-02 07:31:17 +10001166 free(data);
Damien Miller16a13332002-02-13 14:03:56 +11001167
1168 if (len == req->len) {
1169 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001170 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001171 num_req--;
1172 } else {
1173 /* Resend the request for the missing data */
1174 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001175 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001176 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001177 (unsigned long long)req->offset +
1178 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001179 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001180 req->len -= len;
1181 req->offset += len;
Damien Miller65e42f82010-09-24 22:15:11 +10001182 send_read_request(conn, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001183 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001184 /* Reduce the request size */
1185 if (len < buflen)
1186 buflen = MAX(MIN_READ_SIZE, len);
1187 }
1188 if (max_req > 0) { /* max_req = 0 iff EOF received */
1189 if (size > 0 && offset > size) {
1190 /* Only one request at a time
1191 * after the expected EOF */
1192 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001193 (unsigned long long)offset,
1194 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001195 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001196 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001197 ++max_req;
1198 }
1199 }
1200 break;
1201 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001202 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001203 SSH2_FXP_DATA, type);
1204 }
Damien Miller33804262001-02-04 23:20:18 +11001205 }
Damien Miller33804262001-02-04 23:20:18 +11001206
Damien Miller62d57f62003-01-10 21:43:24 +11001207 if (showprogress && size)
1208 stop_progress_meter();
1209
Damien Miller16a13332002-02-13 14:03:56 +11001210 /* Sanity check */
1211 if (TAILQ_FIRST(&requests) != NULL)
1212 fatal("Transfer complete, but requests still in queue");
Damien Miller0d032412013-07-25 11:56:52 +10001213 /* Truncate at highest contiguous point to avoid holes on interrupt */
1214 if (read_error || write_error || interrupted) {
Damien Millerbda5c842013-10-15 12:05:58 +11001215 if (reordered && resume_flag) {
Damien Miller0d032412013-07-25 11:56:52 +10001216 error("Unable to resume download of \"%s\": "
1217 "server reordered requests", local_path);
1218 }
1219 debug("truncating at %llu", (unsigned long long)highwater);
1220 ftruncate(local_fd, highwater);
1221 }
Damien Miller16a13332002-02-13 14:03:56 +11001222 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001223 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001224 remote_path, fx2txt(status));
Damien Millerfec029f2013-08-21 02:42:12 +10001225 status = -1;
Damien Miller3db5f532002-02-13 14:10:32 +11001226 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001227 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001228 error("Couldn't write to \"%s\": %s", local_path,
1229 strerror(write_errno));
1230 status = -1;
1231 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001232 } else {
Damien Miller3db5f532002-02-13 14:10:32 +11001233 status = do_close(conn, handle, handle_len);
Damien Millerfec029f2013-08-21 02:42:12 +10001234 if (interrupted || status != SSH2_FX_OK)
Damien Miller0d032412013-07-25 11:56:52 +10001235 status = -1;
Damien Miller16a13332002-02-13 14:03:56 +11001236 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001237#ifdef HAVE_FCHMOD
Damien Millerbda5c842013-10-15 12:05:58 +11001238 if (preserve_flag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001239#else
Damien Millerbda5c842013-10-15 12:05:58 +11001240 if (preserve_flag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001241#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001242 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001243 strerror(errno));
Damien Millerbda5c842013-10-15 12:05:58 +11001244 if (preserve_flag &&
1245 (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
Damien Miller16a13332002-02-13 14:03:56 +11001246 struct timeval tv[2];
1247 tv[0].tv_sec = a->atime;
1248 tv[1].tv_sec = a->mtime;
1249 tv[0].tv_usec = tv[1].tv_usec = 0;
1250 if (utimes(local_path, tv) == -1)
1251 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001252 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001253 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001254 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001255 close(local_fd);
1256 buffer_free(&msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001257 free(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001258
1259 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001260}
1261
Darren Tucker1b0dd172009-10-07 08:37:48 +11001262static int
Damien Millerbda5c842013-10-15 12:05:58 +11001263download_dir_internal(struct sftp_conn *conn, char *src, char *dst, int depth,
1264 Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001265{
1266 int i, ret = 0;
1267 SFTP_DIRENT **dir_entries;
1268 char *filename, *new_src, *new_dst;
1269 mode_t mode = 0777;
1270
1271 if (depth >= MAX_DIR_DEPTH) {
1272 error("Maximum directory depth exceeded: %d levels", depth);
1273 return -1;
1274 }
1275
1276 if (dirattrib == NULL &&
1277 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1278 error("Unable to stat remote directory \"%s\"", src);
1279 return -1;
1280 }
1281 if (!S_ISDIR(dirattrib->perm)) {
1282 error("\"%s\" is not a directory", src);
1283 return -1;
1284 }
Damien Millerbda5c842013-10-15 12:05:58 +11001285 if (print_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001286 printf("Retrieving %s\n", src);
1287
1288 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1289 mode = dirattrib->perm & 01777;
1290 else {
1291 debug("Server did not send permissions for "
1292 "directory \"%s\"", dst);
1293 }
1294
1295 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1296 error("mkdir %s: %s", dst, strerror(errno));
1297 return -1;
1298 }
1299
1300 if (do_readdir(conn, src, &dir_entries) == -1) {
1301 error("%s: Failed to get directory contents", src);
1302 return -1;
1303 }
1304
1305 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1306 filename = dir_entries[i]->filename;
1307
1308 new_dst = path_append(dst, filename);
1309 new_src = path_append(src, filename);
1310
1311 if (S_ISDIR(dir_entries[i]->a.perm)) {
1312 if (strcmp(filename, ".") == 0 ||
1313 strcmp(filename, "..") == 0)
1314 continue;
1315 if (download_dir_internal(conn, new_src, new_dst,
Damien Millerbda5c842013-10-15 12:05:58 +11001316 depth + 1, &(dir_entries[i]->a), preserve_flag,
1317 print_flag, resume_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001318 ret = -1;
1319 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1320 if (do_download(conn, new_src, new_dst,
Damien Millerbda5c842013-10-15 12:05:58 +11001321 &(dir_entries[i]->a), preserve_flag, resume_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001322 error("Download of file %s to %s failed",
1323 new_src, new_dst);
1324 ret = -1;
1325 }
1326 } else
1327 logit("%s: not a regular file\n", new_src);
1328
Darren Tuckera627d422013-06-02 07:31:17 +10001329 free(new_dst);
1330 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001331 }
1332
Damien Millerbda5c842013-10-15 12:05:58 +11001333 if (preserve_flag) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001334 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1335 struct timeval tv[2];
1336 tv[0].tv_sec = dirattrib->atime;
1337 tv[1].tv_sec = dirattrib->mtime;
1338 tv[0].tv_usec = tv[1].tv_usec = 0;
1339 if (utimes(dst, tv) == -1)
1340 error("Can't set times on \"%s\": %s",
1341 dst, strerror(errno));
1342 } else
1343 debug("Server did not send times for directory "
1344 "\"%s\"", dst);
1345 }
1346
1347 free_sftp_dirents(dir_entries);
1348
1349 return ret;
1350}
1351
1352int
1353download_dir(struct sftp_conn *conn, char *src, char *dst,
Damien Millerbda5c842013-10-15 12:05:58 +11001354 Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001355{
1356 char *src_canon;
1357 int ret;
1358
1359 if ((src_canon = do_realpath(conn, src)) == NULL) {
1360 error("Unable to canonicalise path \"%s\"", src);
1361 return -1;
1362 }
1363
Damien Millerbda5c842013-10-15 12:05:58 +11001364 ret = download_dir_internal(conn, src_canon, dst, 0,
1365 dirattrib, preserve_flag, print_flag, resume_flag);
Darren Tuckera627d422013-06-02 07:31:17 +10001366 free(src_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001367 return ret;
1368}
1369
Damien Miller33804262001-02-04 23:20:18 +11001370int
Damien Miller3db5f532002-02-13 14:10:32 +11001371do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
Damien Millerbda5c842013-10-15 12:05:58 +11001372 int preserve_flag)
Damien Miller33804262001-02-04 23:20:18 +11001373{
Damien Milleracdf25b2008-02-10 22:27:24 +11001374 int local_fd;
1375 int status = SSH2_FX_OK;
Damien Miller5873dfd2002-02-13 14:04:37 +11001376 u_int handle_len, id, type;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001377 off_t offset, progress_counter;
Damien Miller8829d362002-02-08 22:04:05 +11001378 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +11001379 Buffer msg;
1380 struct stat sb;
1381 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +11001382 u_int32_t startid;
1383 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001384 struct outstanding_ack {
1385 u_int id;
1386 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001387 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001388 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001389 };
1390 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001391 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001392
1393 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001394
1395 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1396 error("Couldn't open local file \"%s\" for reading: %s",
1397 local_path, strerror(errno));
1398 return(-1);
1399 }
1400 if (fstat(local_fd, &sb) == -1) {
1401 error("Couldn't fstat local file \"%s\": %s",
1402 local_path, strerror(errno));
1403 close(local_fd);
1404 return(-1);
1405 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001406 if (!S_ISREG(sb.st_mode)) {
1407 error("%s is not a regular file", local_path);
1408 close(local_fd);
1409 return(-1);
1410 }
Damien Miller33804262001-02-04 23:20:18 +11001411 stat_to_attrib(&sb, &a);
1412
1413 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1414 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1415 a.perm &= 0777;
Damien Millerbda5c842013-10-15 12:05:58 +11001416 if (!preserve_flag)
Damien Miller33804262001-02-04 23:20:18 +11001417 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1418
1419 buffer_init(&msg);
1420
1421 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001422 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001423 buffer_put_char(&msg, SSH2_FXP_OPEN);
1424 buffer_put_int(&msg, id);
1425 buffer_put_cstring(&msg, remote_path);
1426 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1427 encode_attrib(&msg, &a);
Damien Miller65e42f82010-09-24 22:15:11 +10001428 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001429 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001430
1431 buffer_clear(&msg);
1432
Damien Miller65e42f82010-09-24 22:15:11 +10001433 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001434 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001435 if (handle == NULL) {
1436 close(local_fd);
1437 buffer_free(&msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001438 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001439 }
1440
Damien Miller16a13332002-02-13 14:03:56 +11001441 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001442 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001443
Damien Miller33804262001-02-04 23:20:18 +11001444 /* Read from local and write to remote */
Darren Tuckerc9a19912013-06-02 08:37:05 +10001445 offset = progress_counter = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001446 if (showprogress)
Darren Tuckerc9a19912013-06-02 08:37:05 +10001447 start_progress_meter(local_path, sb.st_size,
1448 &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001449
Damien Miller9f0f5c62001-12-21 14:45:46 +11001450 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001451 int len;
Damien Miller33804262001-02-04 23:20:18 +11001452
1453 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001454 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001455 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001456 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001457 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001458 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001459 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001460 len = 0;
1461 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001462 len = read(local_fd, data, conn->transfer_buflen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001463 while ((len == -1) &&
1464 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
Damien Miller33804262001-02-04 23:20:18 +11001465
1466 if (len == -1)
1467 fatal("Couldn't read from \"%s\": %s", local_path,
1468 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001469
1470 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001471 ack = xmalloc(sizeof(*ack));
1472 ack->id = ++id;
1473 ack->offset = offset;
1474 ack->len = len;
1475 TAILQ_INSERT_TAIL(&acks, ack, tq);
1476
Damien Miller16a13332002-02-13 14:03:56 +11001477 buffer_clear(&msg);
1478 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001479 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001480 buffer_put_string(&msg, handle, handle_len);
1481 buffer_put_int64(&msg, offset);
1482 buffer_put_string(&msg, data, len);
Damien Miller65e42f82010-09-24 22:15:11 +10001483 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001484 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001485 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001486 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001487 break;
1488
Damien Miller5873dfd2002-02-13 14:04:37 +11001489 if (ack == NULL)
1490 fatal("Unexpected ACK %u", id);
1491
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001492 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001493 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001494 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001495
Damien Miller5873dfd2002-02-13 14:04:37 +11001496 buffer_clear(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +10001497 get_msg(conn, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001498 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001499 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001500
1501 if (type != SSH2_FXP_STATUS)
1502 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1503 "got %d", SSH2_FXP_STATUS, type);
1504
1505 status = buffer_get_int(&msg);
1506 debug3("SSH2_FXP_STATUS %d", status);
1507
1508 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001509 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001510 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001511 ack = TAILQ_NEXT(ack, tq))
1512 ;
1513 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001514 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001515 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001516 debug3("In write loop, ack for %u %u bytes at %lld",
1517 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001518 ++ackid;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001519 progress_counter += ack->len;
Darren Tuckera627d422013-06-02 07:31:17 +10001520 free(ack);
Damien Miller33804262001-02-04 23:20:18 +11001521 }
Damien Miller33804262001-02-04 23:20:18 +11001522 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001523 if (offset < 0)
1524 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001525 }
Damien Milleracdf25b2008-02-10 22:27:24 +11001526 buffer_free(&msg);
1527
Damien Miller62d57f62003-01-10 21:43:24 +11001528 if (showprogress)
1529 stop_progress_meter();
Darren Tuckera627d422013-06-02 07:31:17 +10001530 free(data);
Damien Miller33804262001-02-04 23:20:18 +11001531
Damien Milleracdf25b2008-02-10 22:27:24 +11001532 if (status != SSH2_FX_OK) {
1533 error("Couldn't write to remote file \"%s\": %s",
1534 remote_path, fx2txt(status));
1535 status = -1;
1536 }
1537
Damien Miller33804262001-02-04 23:20:18 +11001538 if (close(local_fd) == -1) {
1539 error("Couldn't close local file \"%s\": %s", local_path,
1540 strerror(errno));
Damien Millerd7686fd2001-02-10 00:40:03 +11001541 status = -1;
Damien Miller33804262001-02-04 23:20:18 +11001542 }
1543
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001544 /* Override umask and utimes if asked */
Damien Millerbda5c842013-10-15 12:05:58 +11001545 if (preserve_flag)
Damien Miller3db5f532002-02-13 14:10:32 +11001546 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001547
Damien Milleracdf25b2008-02-10 22:27:24 +11001548 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
1549 status = -1;
Darren Tuckera627d422013-06-02 07:31:17 +10001550 free(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001551
1552 return status;
Damien Miller33804262001-02-04 23:20:18 +11001553}
Darren Tucker1b0dd172009-10-07 08:37:48 +11001554
1555static int
Damien Millerbda5c842013-10-15 12:05:58 +11001556upload_dir_internal(struct sftp_conn *conn, char *src, char *dst, int depth,
1557 int preserve_flag, int print_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001558{
1559 int ret = 0, status;
1560 DIR *dirp;
1561 struct dirent *dp;
1562 char *filename, *new_src, *new_dst;
1563 struct stat sb;
1564 Attrib a;
1565
1566 if (depth >= MAX_DIR_DEPTH) {
1567 error("Maximum directory depth exceeded: %d levels", depth);
1568 return -1;
1569 }
1570
1571 if (stat(src, &sb) == -1) {
1572 error("Couldn't stat directory \"%s\": %s",
1573 src, strerror(errno));
1574 return -1;
1575 }
1576 if (!S_ISDIR(sb.st_mode)) {
1577 error("\"%s\" is not a directory", src);
1578 return -1;
1579 }
Damien Millerbda5c842013-10-15 12:05:58 +11001580 if (print_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001581 printf("Entering %s\n", src);
1582
1583 attrib_clear(&a);
1584 stat_to_attrib(&sb, &a);
1585 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1586 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1587 a.perm &= 01777;
Damien Millerbda5c842013-10-15 12:05:58 +11001588 if (!preserve_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001589 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
Damien Miller0d032412013-07-25 11:56:52 +10001590
Darren Tucker1b0dd172009-10-07 08:37:48 +11001591 status = do_mkdir(conn, dst, &a, 0);
1592 /*
1593 * we lack a portable status for errno EEXIST,
1594 * so if we get a SSH2_FX_FAILURE back we must check
1595 * if it was created successfully.
1596 */
1597 if (status != SSH2_FX_OK) {
1598 if (status != SSH2_FX_FAILURE)
1599 return -1;
Damien Miller0d032412013-07-25 11:56:52 +10001600 if (do_stat(conn, dst, 0) == NULL)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001601 return -1;
1602 }
1603
1604 if ((dirp = opendir(src)) == NULL) {
1605 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1606 return -1;
1607 }
Damien Miller0d032412013-07-25 11:56:52 +10001608
Darren Tucker1b0dd172009-10-07 08:37:48 +11001609 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1610 if (dp->d_ino == 0)
1611 continue;
1612 filename = dp->d_name;
1613 new_dst = path_append(dst, filename);
1614 new_src = path_append(src, filename);
1615
Darren Tucker438b4732009-10-11 21:52:10 +11001616 if (lstat(new_src, &sb) == -1) {
1617 logit("%s: lstat failed: %s", filename,
1618 strerror(errno));
1619 ret = -1;
1620 } else if (S_ISDIR(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001621 if (strcmp(filename, ".") == 0 ||
1622 strcmp(filename, "..") == 0)
1623 continue;
1624
1625 if (upload_dir_internal(conn, new_src, new_dst,
Damien Millerbda5c842013-10-15 12:05:58 +11001626 depth + 1, preserve_flag, print_flag) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001627 ret = -1;
Darren Tucker438b4732009-10-11 21:52:10 +11001628 } else if (S_ISREG(sb.st_mode)) {
Damien Millerbda5c842013-10-15 12:05:58 +11001629 if (do_upload(conn, new_src, new_dst,
1630 preserve_flag) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001631 error("Uploading of file %s to %s failed!",
1632 new_src, new_dst);
1633 ret = -1;
1634 }
1635 } else
1636 logit("%s: not a regular file\n", filename);
Darren Tuckera627d422013-06-02 07:31:17 +10001637 free(new_dst);
1638 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001639 }
1640
1641 do_setstat(conn, dst, &a);
1642
1643 (void) closedir(dirp);
1644 return ret;
1645}
1646
1647int
Damien Millerbda5c842013-10-15 12:05:58 +11001648upload_dir(struct sftp_conn *conn, char *src, char *dst, int preserve_flag,
1649 int print_flag)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001650{
1651 char *dst_canon;
1652 int ret;
1653
1654 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1655 error("Unable to canonicalise path \"%s\"", dst);
1656 return -1;
1657 }
1658
Damien Millerbda5c842013-10-15 12:05:58 +11001659 ret = upload_dir_internal(conn, src, dst_canon, preserve_flag,
1660 print_flag, 0);
Darren Tuckera627d422013-06-02 07:31:17 +10001661 free(dst_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001662 return ret;
1663}
1664
1665char *
1666path_append(char *p1, char *p2)
1667{
1668 char *ret;
1669 size_t len = strlen(p1) + strlen(p2) + 2;
1670
1671 ret = xmalloc(len);
1672 strlcpy(ret, p1, len);
1673 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1674 strlcat(ret, "/", len);
1675 strlcat(ret, p2, len);
1676
1677 return(ret);
1678}
1679