blob: b6d402143051942d246468f04632d2389f6f8012 [file] [log] [blame]
Damien Miller3decdba2011-09-22 21:41:05 +10001/* $OpenBSD: sftp-client.c,v 1.95 2011/09/11 16:07:26 markus 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,
115 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_out) !=
116 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 Miller65e42f82010-09-24 22:15:11 +1000340 ret = xmalloc(sizeof(*ret));
341 ret->fd_in = fd_in;
342 ret->fd_out = fd_out;
343 ret->transfer_buflen = transfer_buflen;
344 ret->num_requests = num_requests;
345 ret->exts = 0;
346 ret->limit_kbps = 0;
347
Damien Miller33804262001-02-04 23:20:18 +1100348 buffer_init(&msg);
349 buffer_put_char(&msg, SSH2_FXP_INIT);
350 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller65e42f82010-09-24 22:15:11 +1000351 send_msg(ret, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100352
353 buffer_clear(&msg);
354
Damien Miller65e42f82010-09-24 22:15:11 +1000355 get_msg(ret, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100356
Kevin Stevesef4eea92001-02-05 12:42:17 +0000357 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100358 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000359 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100360 type);
361 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100362 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100363 }
Damien Miller65e42f82010-09-24 22:15:11 +1000364 ret->version = buffer_get_int(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100365
Damien Miller65e42f82010-09-24 22:15:11 +1000366 debug2("Remote version: %u", ret->version);
Damien Miller33804262001-02-04 23:20:18 +1100367
368 /* Check for extensions */
369 while (buffer_len(&msg) > 0) {
370 char *name = buffer_get_string(&msg, NULL);
371 char *value = buffer_get_string(&msg, NULL);
Darren Tuckera64ab332008-06-13 07:01:29 +1000372 int known = 0;
Damien Miller33804262001-02-04 23:20:18 +1100373
Damien Millerd671e5a2008-05-19 14:53:33 +1000374 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
Darren Tuckera64ab332008-06-13 07:01:29 +1000375 strcmp(value, "1") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000376 ret->exts |= SFTP_EXT_POSIX_RENAME;
Darren Tuckera64ab332008-06-13 07:01:29 +1000377 known = 1;
378 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
379 strcmp(value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000380 ret->exts |= SFTP_EXT_STATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000381 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100382 } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
Darren Tuckera64ab332008-06-13 07:01:29 +1000383 strcmp(value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000384 ret->exts |= SFTP_EXT_FSTATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000385 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100386 } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
387 strcmp(value, "1") == 0) {
388 ret->exts |= SFTP_EXT_HARDLINK;
389 known = 1;
Darren Tuckera64ab332008-06-13 07:01:29 +1000390 }
391 if (known) {
392 debug2("Server supports extension \"%s\" revision %s",
393 name, value);
394 } else {
395 debug2("Unrecognised server extension \"%s\"", name);
396 }
Damien Miller33804262001-02-04 23:20:18 +1100397 xfree(name);
398 xfree(value);
399 }
400
401 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100402
Damien Miller3db5f532002-02-13 14:10:32 +1100403 /* Some filexfer v.0 servers don't support large packets */
Damien Miller65e42f82010-09-24 22:15:11 +1000404 if (ret->version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000405 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100406
Damien Miller65e42f82010-09-24 22:15:11 +1000407 ret->limit_kbps = limit_kbps;
408 if (ret->limit_kbps > 0) {
409 bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
410 ret->transfer_buflen);
411 bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
412 ret->transfer_buflen);
413 }
414
415 return ret;
Damien Miller3db5f532002-02-13 14:10:32 +1100416}
417
418u_int
419sftp_proto_version(struct sftp_conn *conn)
420{
Damien Miller65e42f82010-09-24 22:15:11 +1000421 return conn->version;
Damien Miller33804262001-02-04 23:20:18 +1100422}
423
424int
Damien Miller3db5f532002-02-13 14:10:32 +1100425do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100426{
427 u_int id, status;
428 Buffer msg;
429
430 buffer_init(&msg);
431
Damien Miller3db5f532002-02-13 14:10:32 +1100432 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100433 buffer_put_char(&msg, SSH2_FXP_CLOSE);
434 buffer_put_int(&msg, id);
435 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000436 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000437 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100438
Damien Miller65e42f82010-09-24 22:15:11 +1000439 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100440 if (status != SSH2_FX_OK)
441 error("Couldn't close file: %s", fx2txt(status));
442
443 buffer_free(&msg);
444
Damien Miller65e42f82010-09-24 22:15:11 +1000445 return status;
Damien Miller33804262001-02-04 23:20:18 +1100446}
447
Damien Miller4870afd2001-03-14 10:27:09 +1100448
Ben Lindstrombba81212001-06-25 05:01:22 +0000449static int
Damien Miller3db5f532002-02-13 14:10:32 +1100450do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100451 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100452{
453 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000454 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100455 char *handle;
456
Damien Miller3db5f532002-02-13 14:10:32 +1100457 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100458
459 buffer_init(&msg);
460 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
461 buffer_put_int(&msg, id);
462 buffer_put_cstring(&msg, path);
Damien Miller65e42f82010-09-24 22:15:11 +1000463 send_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100464
465 buffer_clear(&msg);
466
Damien Miller65e42f82010-09-24 22:15:11 +1000467 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +1100468 "remote readdir(\"%s\")", path);
Damien Miller33804262001-02-04 23:20:18 +1100469 if (handle == NULL)
Damien Miller65e42f82010-09-24 22:15:11 +1000470 return -1;
Damien Miller33804262001-02-04 23:20:18 +1100471
Damien Miller4870afd2001-03-14 10:27:09 +1100472 if (dir) {
473 ents = 0;
474 *dir = xmalloc(sizeof(**dir));
475 (*dir)[0] = NULL;
476 }
Damien Miller4870afd2001-03-14 10:27:09 +1100477
Darren Tuckercdf547a2004-05-24 10:12:19 +1000478 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100479 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100480
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000481 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100482
483 buffer_clear(&msg);
484 buffer_put_char(&msg, SSH2_FXP_READDIR);
485 buffer_put_int(&msg, id);
486 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000487 send_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100488
489 buffer_clear(&msg);
490
Damien Miller65e42f82010-09-24 22:15:11 +1000491 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100492
493 type = buffer_get_char(&msg);
494 id = buffer_get_int(&msg);
495
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000496 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100497
498 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000499 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100500
501 if (type == SSH2_FXP_STATUS) {
502 int status = buffer_get_int(&msg);
503
504 debug3("Received SSH2_FXP_STATUS %d", status);
505
506 if (status == SSH2_FX_EOF) {
507 break;
508 } else {
509 error("Couldn't read directory: %s",
510 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100511 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100512 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000513 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100514 }
515 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000516 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100517 SSH2_FXP_NAME, type);
518
519 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100520 if (count == 0)
521 break;
522 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100523 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100524 char *filename, *longname;
525 Attrib *a;
526
527 filename = buffer_get_string(&msg, NULL);
528 longname = buffer_get_string(&msg, NULL);
529 a = decode_attrib(&msg);
530
Damien Miller4870afd2001-03-14 10:27:09 +1100531 if (printflag)
532 printf("%s\n", longname);
533
Darren Tucker1b0dd172009-10-07 08:37:48 +1100534 /*
535 * Directory entries should never contain '/'
536 * These can be used to attack recursive ops
537 * (e.g. send '../../../../etc/passwd')
538 */
539 if (strchr(filename, '/') != NULL) {
540 error("Server sent suspect path \"%s\" "
541 "during readdir of \"%s\"", filename, path);
542 goto next;
543 }
544
Damien Miller4870afd2001-03-14 10:27:09 +1100545 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100546 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100547 (*dir)[ents] = xmalloc(sizeof(***dir));
548 (*dir)[ents]->filename = xstrdup(filename);
549 (*dir)[ents]->longname = xstrdup(longname);
550 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
551 (*dir)[++ents] = NULL;
552 }
Darren Tucker1b0dd172009-10-07 08:37:48 +1100553 next:
Damien Miller33804262001-02-04 23:20:18 +1100554 xfree(filename);
555 xfree(longname);
556 }
557 }
558
559 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100560 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100561 xfree(handle);
562
Darren Tuckercdf547a2004-05-24 10:12:19 +1000563 /* Don't return partial matches on interrupt */
564 if (interrupted && dir != NULL && *dir != NULL) {
565 free_sftp_dirents(*dir);
566 *dir = xmalloc(sizeof(**dir));
567 **dir = NULL;
568 }
569
Damien Miller65e42f82010-09-24 22:15:11 +1000570 return 0;
Damien Miller33804262001-02-04 23:20:18 +1100571}
572
573int
Damien Miller3db5f532002-02-13 14:10:32 +1100574do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100575{
Damien Miller3db5f532002-02-13 14:10:32 +1100576 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100577}
578
579void free_sftp_dirents(SFTP_DIRENT **s)
580{
581 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100582
583 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100584 xfree(s[i]->filename);
585 xfree(s[i]->longname);
586 xfree(s[i]);
587 }
588 xfree(s);
589}
590
591int
Damien Miller3db5f532002-02-13 14:10:32 +1100592do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100593{
594 u_int status, id;
595
596 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
597
Damien Miller3db5f532002-02-13 14:10:32 +1100598 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000599 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
600 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100601 if (status != SSH2_FX_OK)
602 error("Couldn't delete file: %s", fx2txt(status));
603 return(status);
604}
605
606int
Darren Tucker1b0dd172009-10-07 08:37:48 +1100607do_mkdir(struct sftp_conn *conn, char *path, Attrib *a, int printflag)
Damien Miller33804262001-02-04 23:20:18 +1100608{
609 u_int status, id;
610
Damien Miller3db5f532002-02-13 14:10:32 +1100611 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000612 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100613 strlen(path), a);
614
Damien Miller65e42f82010-09-24 22:15:11 +1000615 status = get_status(conn, id);
Darren Tucker1b0dd172009-10-07 08:37:48 +1100616 if (status != SSH2_FX_OK && printflag)
Damien Miller33804262001-02-04 23:20:18 +1100617 error("Couldn't create directory: %s", fx2txt(status));
618
619 return(status);
620}
621
622int
Damien Miller3db5f532002-02-13 14:10:32 +1100623do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100624{
625 u_int status, id;
626
Damien Miller3db5f532002-02-13 14:10:32 +1100627 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000628 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100629 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100630
Damien Miller65e42f82010-09-24 22:15:11 +1000631 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100632 if (status != SSH2_FX_OK)
633 error("Couldn't remove directory: %s", fx2txt(status));
634
635 return(status);
636}
637
638Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100639do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100640{
641 u_int id;
642
Damien Miller3db5f532002-02-13 14:10:32 +1100643 id = conn->msg_id++;
644
Damien Miller65e42f82010-09-24 22:15:11 +1000645 send_string_request(conn, id,
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000646 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100647 path, strlen(path));
648
Damien Miller65e42f82010-09-24 22:15:11 +1000649 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100650}
651
652Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100653do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100654{
655 u_int id;
656
Damien Miller3db5f532002-02-13 14:10:32 +1100657 if (conn->version == 0) {
658 if (quiet)
659 debug("Server version does not support lstat operation");
660 else
Damien Miller996acd22003-04-09 20:59:48 +1000661 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000662 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100663 }
664
665 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000666 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100667 strlen(path));
668
Damien Miller65e42f82010-09-24 22:15:11 +1000669 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100670}
671
Damien Millercfe23d32008-02-10 22:20:44 +1100672#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100673Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100674do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100675{
676 u_int id;
677
Damien Miller3db5f532002-02-13 14:10:32 +1100678 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000679 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
Damien Miller3db5f532002-02-13 14:10:32 +1100680 handle_len);
681
Damien Miller65e42f82010-09-24 22:15:11 +1000682 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100683}
Damien Millercfe23d32008-02-10 22:20:44 +1100684#endif
Damien Miller33804262001-02-04 23:20:18 +1100685
686int
Damien Miller3db5f532002-02-13 14:10:32 +1100687do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100688{
689 u_int status, id;
690
Damien Miller3db5f532002-02-13 14:10:32 +1100691 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000692 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100693 strlen(path), a);
694
Damien Miller65e42f82010-09-24 22:15:11 +1000695 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100696 if (status != SSH2_FX_OK)
697 error("Couldn't setstat on \"%s\": %s", path,
698 fx2txt(status));
699
700 return(status);
701}
702
703int
Damien Miller3db5f532002-02-13 14:10:32 +1100704do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100705 Attrib *a)
706{
707 u_int status, id;
708
Damien Miller3db5f532002-02-13 14:10:32 +1100709 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000710 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100711 handle_len, a);
712
Damien Miller65e42f82010-09-24 22:15:11 +1000713 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100714 if (status != SSH2_FX_OK)
715 error("Couldn't fsetstat: %s", fx2txt(status));
716
717 return(status);
718}
719
720char *
Damien Miller3db5f532002-02-13 14:10:32 +1100721do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100722{
723 Buffer msg;
724 u_int type, expected_id, count, id;
725 char *filename, *longname;
726 Attrib *a;
727
Damien Miller3db5f532002-02-13 14:10:32 +1100728 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000729 send_string_request(conn, id, SSH2_FXP_REALPATH, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100730 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100731
732 buffer_init(&msg);
733
Damien Miller65e42f82010-09-24 22:15:11 +1000734 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100735 type = buffer_get_char(&msg);
736 id = buffer_get_int(&msg);
737
738 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000739 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100740
741 if (type == SSH2_FXP_STATUS) {
742 u_int status = buffer_get_int(&msg);
743
744 error("Couldn't canonicalise: %s", fx2txt(status));
Damien Miller49566312010-06-26 09:38:23 +1000745 buffer_free(&msg);
746 return NULL;
Damien Miller33804262001-02-04 23:20:18 +1100747 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000748 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100749 SSH2_FXP_NAME, type);
750
751 count = buffer_get_int(&msg);
752 if (count != 1)
753 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
754
755 filename = buffer_get_string(&msg, NULL);
756 longname = buffer_get_string(&msg, NULL);
757 a = decode_attrib(&msg);
758
759 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
760
761 xfree(longname);
762
763 buffer_free(&msg);
764
765 return(filename);
766}
767
768int
Damien Miller3db5f532002-02-13 14:10:32 +1100769do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100770{
771 Buffer msg;
772 u_int status, id;
773
774 buffer_init(&msg);
775
776 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100777 id = conn->msg_id++;
Damien Miller7a3e1d02008-03-27 10:59:57 +1100778 if ((conn->exts & SFTP_EXT_POSIX_RENAME)) {
779 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
780 buffer_put_int(&msg, id);
781 buffer_put_cstring(&msg, "posix-rename@openssh.com");
782 } else {
783 buffer_put_char(&msg, SSH2_FXP_RENAME);
784 buffer_put_int(&msg, id);
785 }
Damien Miller33804262001-02-04 23:20:18 +1100786 buffer_put_cstring(&msg, oldpath);
787 buffer_put_cstring(&msg, newpath);
Damien Miller65e42f82010-09-24 22:15:11 +1000788 send_msg(conn, &msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100789 debug3("Sent message %s \"%s\" -> \"%s\"",
790 (conn->exts & SFTP_EXT_POSIX_RENAME) ? "posix-rename@openssh.com" :
791 "SSH2_FXP_RENAME", oldpath, newpath);
Damien Miller33804262001-02-04 23:20:18 +1100792 buffer_free(&msg);
793
Damien Miller65e42f82010-09-24 22:15:11 +1000794 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100795 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100796 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
797 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100798
799 return(status);
800}
801
802int
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100803do_hardlink(struct sftp_conn *conn, char *oldpath, char *newpath)
804{
805 Buffer msg;
806 u_int status, id;
807
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100808 if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
809 error("Server does not support hardlink@openssh.com extension");
810 return -1;
811 }
812
Damien Miller3decdba2011-09-22 21:41:05 +1000813 buffer_init(&msg);
814
815 /* Send link request */
816 id = conn->msg_id++;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100817 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
818 buffer_put_int(&msg, id);
819 buffer_put_cstring(&msg, "hardlink@openssh.com");
820 buffer_put_cstring(&msg, oldpath);
821 buffer_put_cstring(&msg, newpath);
822 send_msg(conn, &msg);
823 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
824 oldpath, newpath);
825 buffer_free(&msg);
826
827 status = get_status(conn, id);
828 if (status != SSH2_FX_OK)
829 error("Couldn't link file \"%s\" to \"%s\": %s", oldpath,
830 newpath, fx2txt(status));
831
832 return(status);
833}
834
835int
Damien Miller3db5f532002-02-13 14:10:32 +1100836do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100837{
838 Buffer msg;
839 u_int status, id;
840
Damien Miller3db5f532002-02-13 14:10:32 +1100841 if (conn->version < 3) {
842 error("This server does not support the symlink operation");
843 return(SSH2_FX_OP_UNSUPPORTED);
844 }
845
Damien Miller058316f2001-03-08 10:08:49 +1100846 buffer_init(&msg);
847
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000848 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100849 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100850 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
851 buffer_put_int(&msg, id);
852 buffer_put_cstring(&msg, oldpath);
853 buffer_put_cstring(&msg, newpath);
Damien Miller65e42f82010-09-24 22:15:11 +1000854 send_msg(conn, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100855 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
856 newpath);
857 buffer_free(&msg);
858
Damien Miller65e42f82010-09-24 22:15:11 +1000859 status = get_status(conn, id);
Damien Miller058316f2001-03-08 10:08:49 +1100860 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000861 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100862 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100863
864 return(status);
865}
866
Damien Millercfe23d32008-02-10 22:20:44 +1100867#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +1100868char *
Damien Miller3db5f532002-02-13 14:10:32 +1100869do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100870{
871 Buffer msg;
872 u_int type, expected_id, count, id;
873 char *filename, *longname;
874 Attrib *a;
875
Damien Miller3db5f532002-02-13 14:10:32 +1100876 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000877 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100878
879 buffer_init(&msg);
880
Damien Miller65e42f82010-09-24 22:15:11 +1000881 get_msg(conn, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100882 type = buffer_get_char(&msg);
883 id = buffer_get_int(&msg);
884
885 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000886 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100887
888 if (type == SSH2_FXP_STATUS) {
889 u_int status = buffer_get_int(&msg);
890
891 error("Couldn't readlink: %s", fx2txt(status));
Damien Miller3decdba2011-09-22 21:41:05 +1000892 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100893 return(NULL);
894 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000895 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100896 SSH2_FXP_NAME, type);
897
898 count = buffer_get_int(&msg);
899 if (count != 1)
900 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
901
902 filename = buffer_get_string(&msg, NULL);
903 longname = buffer_get_string(&msg, NULL);
904 a = decode_attrib(&msg);
905
906 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
907
908 xfree(longname);
909
910 buffer_free(&msg);
911
912 return(filename);
913}
Damien Millercfe23d32008-02-10 22:20:44 +1100914#endif
Damien Miller058316f2001-03-08 10:08:49 +1100915
Damien Millerd671e5a2008-05-19 14:53:33 +1000916int
Darren Tucker7b598892008-06-09 22:49:36 +1000917do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
Damien Millerd671e5a2008-05-19 14:53:33 +1000918 int quiet)
919{
920 Buffer msg;
921 u_int id;
922
923 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
924 error("Server does not support statvfs@openssh.com extension");
925 return -1;
926 }
927
928 id = conn->msg_id++;
929
930 buffer_init(&msg);
931 buffer_clear(&msg);
932 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
933 buffer_put_int(&msg, id);
934 buffer_put_cstring(&msg, "statvfs@openssh.com");
935 buffer_put_cstring(&msg, path);
Damien Miller65e42f82010-09-24 22:15:11 +1000936 send_msg(conn, &msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000937 buffer_free(&msg);
938
Damien Miller65e42f82010-09-24 22:15:11 +1000939 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +1000940}
941
942#ifdef notyet
943int
944do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
Darren Tucker7b598892008-06-09 22:49:36 +1000945 struct sftp_statvfs *st, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000946{
947 Buffer msg;
948 u_int id;
949
950 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
951 error("Server does not support fstatvfs@openssh.com extension");
952 return -1;
953 }
954
955 id = conn->msg_id++;
956
957 buffer_init(&msg);
958 buffer_clear(&msg);
959 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
960 buffer_put_int(&msg, id);
961 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
962 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000963 send_msg(conn, &msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000964 buffer_free(&msg);
965
Damien Miller65e42f82010-09-24 22:15:11 +1000966 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +1000967}
968#endif
969
Damien Miller16a13332002-02-13 14:03:56 +1100970static void
Damien Miller65e42f82010-09-24 22:15:11 +1000971send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
972 u_int len, char *handle, u_int handle_len)
Damien Miller16a13332002-02-13 14:03:56 +1100973{
974 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000975
Damien Miller16a13332002-02-13 14:03:56 +1100976 buffer_init(&msg);
977 buffer_clear(&msg);
978 buffer_put_char(&msg, SSH2_FXP_READ);
979 buffer_put_int(&msg, id);
980 buffer_put_string(&msg, handle, handle_len);
981 buffer_put_int64(&msg, offset);
982 buffer_put_int(&msg, len);
Damien Miller65e42f82010-09-24 22:15:11 +1000983 send_msg(conn, &msg);
Damien Miller16a13332002-02-13 14:03:56 +1100984 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000985}
Damien Miller16a13332002-02-13 14:03:56 +1100986
Damien Miller058316f2001-03-08 10:08:49 +1100987int
Damien Miller3db5f532002-02-13 14:10:32 +1100988do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
Darren Tucker1b0dd172009-10-07 08:37:48 +1100989 Attrib *a, int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100990{
Darren Tucker1b0dd172009-10-07 08:37:48 +1100991 Attrib junk;
Damien Miller16a13332002-02-13 14:03:56 +1100992 Buffer msg;
993 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000994 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100995 int read_error, write_errno;
996 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000997 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100998 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100999 struct request {
1000 u_int id;
1001 u_int len;
1002 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001003 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +11001004 };
1005 TAILQ_HEAD(reqhead, request) requests;
1006 struct request *req;
1007
1008 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +11001009
Darren Tucker1b0dd172009-10-07 08:37:48 +11001010 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1011 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001012
Damien Miller9e720282008-06-29 22:46:35 +10001013 /* Do not preserve set[ug]id here, as we do not preserve ownership */
Damien Miller33804262001-02-04 23:20:18 +11001014 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +11001015 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +11001016 else
1017 mode = 0666;
1018
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001019 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +11001020 (!S_ISREG(a->perm))) {
1021 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001022 return(-1);
1023 }
1024
Damien Miller16a13332002-02-13 14:03:56 +11001025 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1026 size = a->size;
1027 else
1028 size = 0;
1029
Damien Miller3db5f532002-02-13 14:10:32 +11001030 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +11001031 buffer_init(&msg);
1032
1033 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001034 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001035 buffer_put_char(&msg, SSH2_FXP_OPEN);
1036 buffer_put_int(&msg, id);
1037 buffer_put_cstring(&msg, remote_path);
1038 buffer_put_int(&msg, SSH2_FXF_READ);
1039 attrib_clear(&junk); /* Send empty attributes */
1040 encode_attrib(&msg, &junk);
Damien Miller65e42f82010-09-24 22:15:11 +10001041 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001042 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001043
Damien Miller65e42f82010-09-24 22:15:11 +10001044 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001045 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001046 if (handle == NULL) {
1047 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +11001048 return(-1);
1049 }
1050
Damien Millera8e06ce2003-11-21 23:48:55 +11001051 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +11001052 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +11001053 if (local_fd == -1) {
1054 error("Couldn't open local file \"%s\" for writing: %s",
1055 local_path, strerror(errno));
Damien Miller6b0c8182008-02-10 22:23:41 +11001056 do_close(conn, handle, handle_len);
Ben Lindstrom021fcd32002-02-26 18:02:43 +00001057 buffer_free(&msg);
1058 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001059 return(-1);
1060 }
1061
Damien Miller33804262001-02-04 23:20:18 +11001062 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +11001063 write_error = read_error = write_errno = num_req = offset = 0;
1064 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +11001065 progress_counter = 0;
1066
Damien Miller9ba30692004-03-08 23:12:02 +11001067 if (showprogress && size != 0)
1068 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001069
Damien Miller16a13332002-02-13 14:03:56 +11001070 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +11001071 char *data;
Damien Miller16a13332002-02-13 14:03:56 +11001072 u_int len;
Damien Miller33804262001-02-04 23:20:18 +11001073
Darren Tuckercdf547a2004-05-24 10:12:19 +10001074 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001075 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001076 * allow outstanding requests to drain gracefully
1077 */
1078 if (interrupted) {
1079 if (num_req == 0) /* If we haven't started yet... */
1080 break;
1081 max_req = 0;
1082 }
1083
Damien Miller16a13332002-02-13 14:03:56 +11001084 /* Send some more requests */
1085 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001086 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +00001087 (unsigned long long)offset,
1088 (unsigned long long)offset + buflen - 1,
1089 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +11001090 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +11001091 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001092 req->len = buflen;
1093 req->offset = offset;
1094 offset += buflen;
1095 num_req++;
1096 TAILQ_INSERT_TAIL(&requests, req, tq);
Damien Miller65e42f82010-09-24 22:15:11 +10001097 send_read_request(conn, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001098 req->len, handle, handle_len);
1099 }
Damien Miller33804262001-02-04 23:20:18 +11001100
1101 buffer_clear(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +10001102 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +11001103 type = buffer_get_char(&msg);
1104 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001105 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001106
Damien Miller16a13332002-02-13 14:03:56 +11001107 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001108 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001109 req != NULL && req->id != id;
1110 req = TAILQ_NEXT(req, tq))
1111 ;
1112 if (req == NULL)
1113 fatal("Unexpected reply %u", id);
1114
1115 switch (type) {
1116 case SSH2_FXP_STATUS:
1117 status = buffer_get_int(&msg);
1118 if (status != SSH2_FX_EOF)
1119 read_error = 1;
1120 max_req = 0;
1121 TAILQ_REMOVE(&requests, req, tq);
1122 xfree(req);
1123 num_req--;
1124 break;
1125 case SSH2_FXP_DATA:
1126 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +00001127 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001128 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001129 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001130 if (len > req->len)
1131 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +00001132 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001133 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001134 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001135 !write_error) {
1136 write_errno = errno;
1137 write_error = 1;
1138 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001139 }
Damien Miller62d57f62003-01-10 21:43:24 +11001140 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +11001141 xfree(data);
1142
1143 if (len == req->len) {
1144 TAILQ_REMOVE(&requests, req, tq);
1145 xfree(req);
1146 num_req--;
1147 } else {
1148 /* Resend the request for the missing data */
1149 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001150 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001151 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001152 (unsigned long long)req->offset +
1153 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001154 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001155 req->len -= len;
1156 req->offset += len;
Damien Miller65e42f82010-09-24 22:15:11 +10001157 send_read_request(conn, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001158 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001159 /* Reduce the request size */
1160 if (len < buflen)
1161 buflen = MAX(MIN_READ_SIZE, len);
1162 }
1163 if (max_req > 0) { /* max_req = 0 iff EOF received */
1164 if (size > 0 && offset > size) {
1165 /* Only one request at a time
1166 * after the expected EOF */
1167 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001168 (unsigned long long)offset,
1169 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001170 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001171 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001172 ++max_req;
1173 }
1174 }
1175 break;
1176 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001177 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001178 SSH2_FXP_DATA, type);
1179 }
Damien Miller33804262001-02-04 23:20:18 +11001180 }
Damien Miller33804262001-02-04 23:20:18 +11001181
Damien Miller62d57f62003-01-10 21:43:24 +11001182 if (showprogress && size)
1183 stop_progress_meter();
1184
Damien Miller16a13332002-02-13 14:03:56 +11001185 /* Sanity check */
1186 if (TAILQ_FIRST(&requests) != NULL)
1187 fatal("Transfer complete, but requests still in queue");
1188
1189 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001190 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001191 remote_path, fx2txt(status));
1192 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001193 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001194 error("Couldn't write to \"%s\": %s", local_path,
1195 strerror(write_errno));
1196 status = -1;
1197 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001198 } else {
Damien Miller3db5f532002-02-13 14:10:32 +11001199 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001200
1201 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001202#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +11001203 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001204#else
Damien Miller16a13332002-02-13 14:03:56 +11001205 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001206#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001207 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001208 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001209 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1210 struct timeval tv[2];
1211 tv[0].tv_sec = a->atime;
1212 tv[1].tv_sec = a->mtime;
1213 tv[0].tv_usec = tv[1].tv_usec = 0;
1214 if (utimes(local_path, tv) == -1)
1215 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001216 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001217 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001218 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001219 close(local_fd);
1220 buffer_free(&msg);
1221 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001222
1223 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001224}
1225
Darren Tucker1b0dd172009-10-07 08:37:48 +11001226static int
1227download_dir_internal(struct sftp_conn *conn, char *src, char *dst,
1228 Attrib *dirattrib, int pflag, int printflag, int depth)
1229{
1230 int i, ret = 0;
1231 SFTP_DIRENT **dir_entries;
1232 char *filename, *new_src, *new_dst;
1233 mode_t mode = 0777;
1234
1235 if (depth >= MAX_DIR_DEPTH) {
1236 error("Maximum directory depth exceeded: %d levels", depth);
1237 return -1;
1238 }
1239
1240 if (dirattrib == NULL &&
1241 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1242 error("Unable to stat remote directory \"%s\"", src);
1243 return -1;
1244 }
1245 if (!S_ISDIR(dirattrib->perm)) {
1246 error("\"%s\" is not a directory", src);
1247 return -1;
1248 }
1249 if (printflag)
1250 printf("Retrieving %s\n", src);
1251
1252 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1253 mode = dirattrib->perm & 01777;
1254 else {
1255 debug("Server did not send permissions for "
1256 "directory \"%s\"", dst);
1257 }
1258
1259 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1260 error("mkdir %s: %s", dst, strerror(errno));
1261 return -1;
1262 }
1263
1264 if (do_readdir(conn, src, &dir_entries) == -1) {
1265 error("%s: Failed to get directory contents", src);
1266 return -1;
1267 }
1268
1269 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1270 filename = dir_entries[i]->filename;
1271
1272 new_dst = path_append(dst, filename);
1273 new_src = path_append(src, filename);
1274
1275 if (S_ISDIR(dir_entries[i]->a.perm)) {
1276 if (strcmp(filename, ".") == 0 ||
1277 strcmp(filename, "..") == 0)
1278 continue;
1279 if (download_dir_internal(conn, new_src, new_dst,
1280 &(dir_entries[i]->a), pflag, printflag,
1281 depth + 1) == -1)
1282 ret = -1;
1283 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1284 if (do_download(conn, new_src, new_dst,
1285 &(dir_entries[i]->a), pflag) == -1) {
1286 error("Download of file %s to %s failed",
1287 new_src, new_dst);
1288 ret = -1;
1289 }
1290 } else
1291 logit("%s: not a regular file\n", new_src);
1292
1293 xfree(new_dst);
1294 xfree(new_src);
1295 }
1296
1297 if (pflag) {
1298 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1299 struct timeval tv[2];
1300 tv[0].tv_sec = dirattrib->atime;
1301 tv[1].tv_sec = dirattrib->mtime;
1302 tv[0].tv_usec = tv[1].tv_usec = 0;
1303 if (utimes(dst, tv) == -1)
1304 error("Can't set times on \"%s\": %s",
1305 dst, strerror(errno));
1306 } else
1307 debug("Server did not send times for directory "
1308 "\"%s\"", dst);
1309 }
1310
1311 free_sftp_dirents(dir_entries);
1312
1313 return ret;
1314}
1315
1316int
1317download_dir(struct sftp_conn *conn, char *src, char *dst,
1318 Attrib *dirattrib, int pflag, int printflag)
1319{
1320 char *src_canon;
1321 int ret;
1322
1323 if ((src_canon = do_realpath(conn, src)) == NULL) {
1324 error("Unable to canonicalise path \"%s\"", src);
1325 return -1;
1326 }
1327
1328 ret = download_dir_internal(conn, src_canon, dst,
1329 dirattrib, pflag, printflag, 0);
1330 xfree(src_canon);
1331 return ret;
1332}
1333
Damien Miller33804262001-02-04 23:20:18 +11001334int
Damien Miller3db5f532002-02-13 14:10:32 +11001335do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
1336 int pflag)
Damien Miller33804262001-02-04 23:20:18 +11001337{
Damien Milleracdf25b2008-02-10 22:27:24 +11001338 int local_fd;
1339 int status = SSH2_FX_OK;
Damien Miller5873dfd2002-02-13 14:04:37 +11001340 u_int handle_len, id, type;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001341 off_t offset;
Damien Miller8829d362002-02-08 22:04:05 +11001342 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +11001343 Buffer msg;
1344 struct stat sb;
1345 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +11001346 u_int32_t startid;
1347 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001348 struct outstanding_ack {
1349 u_int id;
1350 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001351 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001352 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001353 };
1354 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001355 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001356
1357 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001358
1359 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1360 error("Couldn't open local file \"%s\" for reading: %s",
1361 local_path, strerror(errno));
1362 return(-1);
1363 }
1364 if (fstat(local_fd, &sb) == -1) {
1365 error("Couldn't fstat local file \"%s\": %s",
1366 local_path, strerror(errno));
1367 close(local_fd);
1368 return(-1);
1369 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001370 if (!S_ISREG(sb.st_mode)) {
1371 error("%s is not a regular file", local_path);
1372 close(local_fd);
1373 return(-1);
1374 }
Damien Miller33804262001-02-04 23:20:18 +11001375 stat_to_attrib(&sb, &a);
1376
1377 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1378 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1379 a.perm &= 0777;
1380 if (!pflag)
1381 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1382
1383 buffer_init(&msg);
1384
1385 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001386 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001387 buffer_put_char(&msg, SSH2_FXP_OPEN);
1388 buffer_put_int(&msg, id);
1389 buffer_put_cstring(&msg, remote_path);
1390 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1391 encode_attrib(&msg, &a);
Damien Miller65e42f82010-09-24 22:15:11 +10001392 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001393 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001394
1395 buffer_clear(&msg);
1396
Damien Miller65e42f82010-09-24 22:15:11 +10001397 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001398 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001399 if (handle == NULL) {
1400 close(local_fd);
1401 buffer_free(&msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001402 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001403 }
1404
Damien Miller16a13332002-02-13 14:03:56 +11001405 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001406 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001407
Damien Miller33804262001-02-04 23:20:18 +11001408 /* Read from local and write to remote */
1409 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001410 if (showprogress)
1411 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001412
Damien Miller9f0f5c62001-12-21 14:45:46 +11001413 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001414 int len;
Damien Miller33804262001-02-04 23:20:18 +11001415
1416 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001417 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001418 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001419 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001420 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001421 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001422 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001423 len = 0;
1424 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001425 len = read(local_fd, data, conn->transfer_buflen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001426 while ((len == -1) &&
1427 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
Damien Miller33804262001-02-04 23:20:18 +11001428
1429 if (len == -1)
1430 fatal("Couldn't read from \"%s\": %s", local_path,
1431 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001432
1433 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001434 ack = xmalloc(sizeof(*ack));
1435 ack->id = ++id;
1436 ack->offset = offset;
1437 ack->len = len;
1438 TAILQ_INSERT_TAIL(&acks, ack, tq);
1439
Damien Miller16a13332002-02-13 14:03:56 +11001440 buffer_clear(&msg);
1441 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001442 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001443 buffer_put_string(&msg, handle, handle_len);
1444 buffer_put_int64(&msg, offset);
1445 buffer_put_string(&msg, data, len);
Damien Miller65e42f82010-09-24 22:15:11 +10001446 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001447 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001448 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001449 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001450 break;
1451
Damien Miller5873dfd2002-02-13 14:04:37 +11001452 if (ack == NULL)
1453 fatal("Unexpected ACK %u", id);
1454
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001455 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001456 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001457 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001458
Damien Miller5873dfd2002-02-13 14:04:37 +11001459 buffer_clear(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +10001460 get_msg(conn, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001461 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001462 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001463
1464 if (type != SSH2_FXP_STATUS)
1465 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1466 "got %d", SSH2_FXP_STATUS, type);
1467
1468 status = buffer_get_int(&msg);
1469 debug3("SSH2_FXP_STATUS %d", status);
1470
1471 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001472 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001473 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001474 ack = TAILQ_NEXT(ack, tq))
1475 ;
1476 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001477 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001478 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001479 debug3("In write loop, ack for %u %u bytes at %lld",
1480 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001481 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001482 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001483 }
Damien Miller33804262001-02-04 23:20:18 +11001484 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001485 if (offset < 0)
1486 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001487 }
Damien Milleracdf25b2008-02-10 22:27:24 +11001488 buffer_free(&msg);
1489
Damien Miller62d57f62003-01-10 21:43:24 +11001490 if (showprogress)
1491 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001492 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001493
Damien Milleracdf25b2008-02-10 22:27:24 +11001494 if (status != SSH2_FX_OK) {
1495 error("Couldn't write to remote file \"%s\": %s",
1496 remote_path, fx2txt(status));
1497 status = -1;
1498 }
1499
Damien Miller33804262001-02-04 23:20:18 +11001500 if (close(local_fd) == -1) {
1501 error("Couldn't close local file \"%s\": %s", local_path,
1502 strerror(errno));
Damien Millerd7686fd2001-02-10 00:40:03 +11001503 status = -1;
Damien Miller33804262001-02-04 23:20:18 +11001504 }
1505
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001506 /* Override umask and utimes if asked */
1507 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001508 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001509
Damien Milleracdf25b2008-02-10 22:27:24 +11001510 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
1511 status = -1;
Damien Millerd7686fd2001-02-10 00:40:03 +11001512 xfree(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001513
1514 return status;
Damien Miller33804262001-02-04 23:20:18 +11001515}
Darren Tucker1b0dd172009-10-07 08:37:48 +11001516
1517static int
1518upload_dir_internal(struct sftp_conn *conn, char *src, char *dst,
1519 int pflag, int printflag, int depth)
1520{
1521 int ret = 0, status;
1522 DIR *dirp;
1523 struct dirent *dp;
1524 char *filename, *new_src, *new_dst;
1525 struct stat sb;
1526 Attrib a;
1527
1528 if (depth >= MAX_DIR_DEPTH) {
1529 error("Maximum directory depth exceeded: %d levels", depth);
1530 return -1;
1531 }
1532
1533 if (stat(src, &sb) == -1) {
1534 error("Couldn't stat directory \"%s\": %s",
1535 src, strerror(errno));
1536 return -1;
1537 }
1538 if (!S_ISDIR(sb.st_mode)) {
1539 error("\"%s\" is not a directory", src);
1540 return -1;
1541 }
1542 if (printflag)
1543 printf("Entering %s\n", src);
1544
1545 attrib_clear(&a);
1546 stat_to_attrib(&sb, &a);
1547 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1548 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1549 a.perm &= 01777;
1550 if (!pflag)
1551 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1552
1553 status = do_mkdir(conn, dst, &a, 0);
1554 /*
1555 * we lack a portable status for errno EEXIST,
1556 * so if we get a SSH2_FX_FAILURE back we must check
1557 * if it was created successfully.
1558 */
1559 if (status != SSH2_FX_OK) {
1560 if (status != SSH2_FX_FAILURE)
1561 return -1;
1562 if (do_stat(conn, dst, 0) == NULL)
1563 return -1;
1564 }
1565
1566 if ((dirp = opendir(src)) == NULL) {
1567 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1568 return -1;
1569 }
1570
1571 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1572 if (dp->d_ino == 0)
1573 continue;
1574 filename = dp->d_name;
1575 new_dst = path_append(dst, filename);
1576 new_src = path_append(src, filename);
1577
Darren Tucker438b4732009-10-11 21:52:10 +11001578 if (lstat(new_src, &sb) == -1) {
1579 logit("%s: lstat failed: %s", filename,
1580 strerror(errno));
1581 ret = -1;
1582 } else if (S_ISDIR(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001583 if (strcmp(filename, ".") == 0 ||
1584 strcmp(filename, "..") == 0)
1585 continue;
1586
1587 if (upload_dir_internal(conn, new_src, new_dst,
Damien Millerc4bb91c2010-08-03 16:04:22 +10001588 pflag, printflag, depth + 1) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001589 ret = -1;
Darren Tucker438b4732009-10-11 21:52:10 +11001590 } else if (S_ISREG(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001591 if (do_upload(conn, new_src, new_dst, pflag) == -1) {
1592 error("Uploading of file %s to %s failed!",
1593 new_src, new_dst);
1594 ret = -1;
1595 }
1596 } else
1597 logit("%s: not a regular file\n", filename);
1598 xfree(new_dst);
1599 xfree(new_src);
1600 }
1601
1602 do_setstat(conn, dst, &a);
1603
1604 (void) closedir(dirp);
1605 return ret;
1606}
1607
1608int
1609upload_dir(struct sftp_conn *conn, char *src, char *dst, int printflag,
1610 int pflag)
1611{
1612 char *dst_canon;
1613 int ret;
1614
1615 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1616 error("Unable to canonicalise path \"%s\"", dst);
1617 return -1;
1618 }
1619
1620 ret = upload_dir_internal(conn, src, dst_canon, pflag, printflag, 0);
1621 xfree(dst_canon);
1622 return ret;
1623}
1624
1625char *
1626path_append(char *p1, char *p2)
1627{
1628 char *ret;
1629 size_t len = strlen(p1) + strlen(p2) + 2;
1630
1631 ret = xmalloc(len);
1632 strlcpy(ret, p1, len);
1633 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1634 strlcat(ret, "/", len);
1635 strlcat(ret, p2, len);
1636
1637 return(ret);
1638}
1639