blob: 4e009ef25539e1b489289d038e3159f65763ce76 [file] [log] [blame]
Damien Miller65e42f82010-09-24 22:15:11 +10001/* $OpenBSD: sftp-client.c,v 1.93 2010/09/22 22:58:51 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
Damien Miller7a3e1d02008-03-27 10:59:57 +110078 u_int exts;
Damien Miller65e42f82010-09-24 22:15:11 +100079 u_int64_t limit_kbps;
80 struct bwlimit bwlimit_in, bwlimit_out;
Damien Miller3db5f532002-02-13 14:10:32 +110081};
Ben Lindstrom288cc392001-02-09 02:58:04 +000082
Darren Tuckerc22f0902009-10-07 08:24:19 +110083static char *
Damien Miller65e42f82010-09-24 22:15:11 +100084get_handle(struct sftp_conn *conn, u_int expected_id, u_int *len,
85 const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
86
87/* ARGSUSED */
88static int
89sftpio(void *_bwlimit, size_t amount)
90{
91 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
92
93 bandwidth_limit(bwlimit, amount);
94 return 0;
95}
Darren Tuckerc22f0902009-10-07 08:24:19 +110096
Ben Lindstrombba81212001-06-25 05:01:22 +000097static void
Damien Miller65e42f82010-09-24 22:15:11 +100098send_msg(struct sftp_conn *conn, Buffer *m)
Damien Miller33804262001-02-04 23:20:18 +110099{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100100 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +1000101 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +1100102
Damien Miller54446182006-01-02 23:40:50 +1100103 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +1100104 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +1100105
Damien Millera7f3aaa2003-01-10 21:43:58 +1100106 /* Send length first */
Damien Miller3f941882006-03-31 23:13:02 +1100107 put_u32(mlen, buffer_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +1000108 iov[0].iov_base = mlen;
109 iov[0].iov_len = sizeof(mlen);
110 iov[1].iov_base = buffer_ptr(m);
111 iov[1].iov_len = buffer_len(m);
Damien Millerd7834352006-08-05 12:39:39 +1000112
Damien Miller65e42f82010-09-24 22:15:11 +1000113 if (atomiciov6(writev, conn->fd_out, iov, 2,
114 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_out) !=
115 buffer_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +1100116 fatal("Couldn't send packet: %s", strerror(errno));
117
118 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +1100119}
120
Ben Lindstrombba81212001-06-25 05:01:22 +0000121static void
Damien Miller65e42f82010-09-24 22:15:11 +1000122get_msg(struct sftp_conn *conn, Buffer *m)
Damien Miller33804262001-02-04 23:20:18 +1100123{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100124 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +1100125
Damien Millera7f3aaa2003-01-10 21:43:58 +1100126 buffer_append_space(m, 4);
Damien Miller65e42f82010-09-24 22:15:11 +1000127 if (atomicio6(read, conn->fd_in, buffer_ptr(m), 4,
128 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in) != 4) {
Damien Millerb253cc42005-05-26 12:23:44 +1000129 if (errno == EPIPE)
130 fatal("Connection closed");
131 else
132 fatal("Couldn't read packet: %s", strerror(errno));
133 }
Damien Miller33804262001-02-04 23:20:18 +1100134
Damien Millera7f3aaa2003-01-10 21:43:58 +1100135 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +1100136 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000137 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100138
Damien Millera7f3aaa2003-01-10 21:43:58 +1100139 buffer_append_space(m, msg_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000140 if (atomicio6(read, conn->fd_in, buffer_ptr(m), msg_len,
141 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in)
142 != msg_len) {
Damien Millerb253cc42005-05-26 12:23:44 +1000143 if (errno == EPIPE)
144 fatal("Connection closed");
145 else
146 fatal("Read packet: %s", strerror(errno));
147 }
Damien Miller33804262001-02-04 23:20:18 +1100148}
149
Ben Lindstrombba81212001-06-25 05:01:22 +0000150static void
Damien Miller65e42f82010-09-24 22:15:11 +1000151send_string_request(struct sftp_conn *conn, u_int id, u_int code, char *s,
Damien Miller33804262001-02-04 23:20:18 +1100152 u_int len)
153{
154 Buffer msg;
155
156 buffer_init(&msg);
157 buffer_put_char(&msg, code);
158 buffer_put_int(&msg, id);
159 buffer_put_string(&msg, s, len);
Damien Miller65e42f82010-09-24 22:15:11 +1000160 send_msg(conn, &msg);
161 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100162 buffer_free(&msg);
163}
164
Ben Lindstrombba81212001-06-25 05:01:22 +0000165static void
Damien Miller65e42f82010-09-24 22:15:11 +1000166send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
167 char *s, u_int len, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100168{
169 Buffer msg;
170
171 buffer_init(&msg);
172 buffer_put_char(&msg, code);
173 buffer_put_int(&msg, id);
174 buffer_put_string(&msg, s, len);
175 encode_attrib(&msg, a);
Damien Miller65e42f82010-09-24 22:15:11 +1000176 send_msg(conn, &msg);
177 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100178 buffer_free(&msg);
179}
180
Ben Lindstrombba81212001-06-25 05:01:22 +0000181static u_int
Damien Miller65e42f82010-09-24 22:15:11 +1000182get_status(struct sftp_conn *conn, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100183{
184 Buffer msg;
185 u_int type, id, status;
186
187 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000188 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100189 type = buffer_get_char(&msg);
190 id = buffer_get_int(&msg);
191
192 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000193 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100194 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000195 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100196 SSH2_FXP_STATUS, type);
197
198 status = buffer_get_int(&msg);
199 buffer_free(&msg);
200
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000201 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100202
Damien Miller65e42f82010-09-24 22:15:11 +1000203 return status;
Damien Miller33804262001-02-04 23:20:18 +1100204}
205
Ben Lindstrombba81212001-06-25 05:01:22 +0000206static char *
Damien Miller65e42f82010-09-24 22:15:11 +1000207get_handle(struct sftp_conn *conn, u_int expected_id, u_int *len,
208 const char *errfmt, ...)
Damien Miller33804262001-02-04 23:20:18 +1100209{
210 Buffer msg;
211 u_int type, id;
Darren Tuckerc22f0902009-10-07 08:24:19 +1100212 char *handle, errmsg[256];
213 va_list args;
214 int status;
215
216 va_start(args, errfmt);
217 if (errfmt != NULL)
218 vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
219 va_end(args);
Damien Miller33804262001-02-04 23:20:18 +1100220
221 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000222 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100223 type = buffer_get_char(&msg);
224 id = buffer_get_int(&msg);
225
226 if (id != expected_id)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100227 fatal("%s: ID mismatch (%u != %u)",
228 errfmt == NULL ? __func__ : errmsg, id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100229 if (type == SSH2_FXP_STATUS) {
Darren Tuckerc22f0902009-10-07 08:24:19 +1100230 status = buffer_get_int(&msg);
231 if (errfmt != NULL)
232 error("%s: %s", errmsg, fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100233 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100234 return(NULL);
235 } else if (type != SSH2_FXP_HANDLE)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100236 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
237 errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
Damien Miller33804262001-02-04 23:20:18 +1100238
239 handle = buffer_get_string(&msg, len);
240 buffer_free(&msg);
241
242 return(handle);
243}
244
Ben Lindstrombba81212001-06-25 05:01:22 +0000245static Attrib *
Damien Miller65e42f82010-09-24 22:15:11 +1000246get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100247{
248 Buffer msg;
249 u_int type, id;
250 Attrib *a;
251
252 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000253 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100254
255 type = buffer_get_char(&msg);
256 id = buffer_get_int(&msg);
257
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000258 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100259 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000260 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100261 if (type == SSH2_FXP_STATUS) {
262 int status = buffer_get_int(&msg);
263
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000264 if (quiet)
265 debug("Couldn't stat remote file: %s", fx2txt(status));
266 else
267 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100268 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100269 return(NULL);
270 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000271 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100272 SSH2_FXP_ATTRS, type);
273 }
274 a = decode_attrib(&msg);
275 buffer_free(&msg);
276
277 return(a);
278}
279
Damien Millerd671e5a2008-05-19 14:53:33 +1000280static int
Damien Miller65e42f82010-09-24 22:15:11 +1000281get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
282 u_int expected_id, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000283{
284 Buffer msg;
285 u_int type, id, flag;
286
287 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000288 get_msg(conn, &msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000289
290 type = buffer_get_char(&msg);
291 id = buffer_get_int(&msg);
292
293 debug3("Received statvfs reply T:%u I:%u", type, id);
294 if (id != expected_id)
295 fatal("ID mismatch (%u != %u)", id, expected_id);
296 if (type == SSH2_FXP_STATUS) {
297 int status = buffer_get_int(&msg);
298
299 if (quiet)
300 debug("Couldn't statvfs: %s", fx2txt(status));
301 else
302 error("Couldn't statvfs: %s", fx2txt(status));
303 buffer_free(&msg);
304 return -1;
305 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
306 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
307 SSH2_FXP_EXTENDED_REPLY, type);
308 }
309
310 bzero(st, sizeof(*st));
Darren Tucker7b598892008-06-09 22:49:36 +1000311 st->f_bsize = buffer_get_int64(&msg);
312 st->f_frsize = buffer_get_int64(&msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000313 st->f_blocks = buffer_get_int64(&msg);
314 st->f_bfree = buffer_get_int64(&msg);
315 st->f_bavail = buffer_get_int64(&msg);
316 st->f_files = buffer_get_int64(&msg);
317 st->f_ffree = buffer_get_int64(&msg);
318 st->f_favail = buffer_get_int64(&msg);
Darren Tucker294b8412008-06-08 12:57:08 +1000319 st->f_fsid = buffer_get_int64(&msg);
Darren Tucker7b598892008-06-09 22:49:36 +1000320 flag = buffer_get_int64(&msg);
321 st->f_namemax = buffer_get_int64(&msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000322
323 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
324 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
325
326 buffer_free(&msg);
327
328 return 0;
329}
330
Damien Miller3db5f532002-02-13 14:10:32 +1100331struct sftp_conn *
Damien Miller65e42f82010-09-24 22:15:11 +1000332do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
333 u_int64_t limit_kbps)
Damien Miller33804262001-02-04 23:20:18 +1100334{
Damien Miller65e42f82010-09-24 22:15:11 +1000335 u_int type;
Damien Miller33804262001-02-04 23:20:18 +1100336 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100337 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100338
Damien Miller65e42f82010-09-24 22:15:11 +1000339 ret = xmalloc(sizeof(*ret));
340 ret->fd_in = fd_in;
341 ret->fd_out = fd_out;
342 ret->transfer_buflen = transfer_buflen;
343 ret->num_requests = num_requests;
344 ret->exts = 0;
345 ret->limit_kbps = 0;
346
Damien Miller33804262001-02-04 23:20:18 +1100347 buffer_init(&msg);
348 buffer_put_char(&msg, SSH2_FXP_INIT);
349 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller65e42f82010-09-24 22:15:11 +1000350 send_msg(ret, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100351
352 buffer_clear(&msg);
353
Damien Miller65e42f82010-09-24 22:15:11 +1000354 get_msg(ret, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100355
Kevin Stevesef4eea92001-02-05 12:42:17 +0000356 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100357 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000358 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100359 type);
360 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100361 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100362 }
Damien Miller65e42f82010-09-24 22:15:11 +1000363 ret->version = buffer_get_int(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100364
Damien Miller65e42f82010-09-24 22:15:11 +1000365 debug2("Remote version: %u", ret->version);
Damien Miller33804262001-02-04 23:20:18 +1100366
367 /* Check for extensions */
368 while (buffer_len(&msg) > 0) {
369 char *name = buffer_get_string(&msg, NULL);
370 char *value = buffer_get_string(&msg, NULL);
Darren Tuckera64ab332008-06-13 07:01:29 +1000371 int known = 0;
Damien Miller33804262001-02-04 23:20:18 +1100372
Damien Millerd671e5a2008-05-19 14:53:33 +1000373 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
Darren Tuckera64ab332008-06-13 07:01:29 +1000374 strcmp(value, "1") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000375 ret->exts |= SFTP_EXT_POSIX_RENAME;
Darren Tuckera64ab332008-06-13 07:01:29 +1000376 known = 1;
377 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
378 strcmp(value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000379 ret->exts |= SFTP_EXT_STATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000380 known = 1;
381 } if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
382 strcmp(value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000383 ret->exts |= SFTP_EXT_FSTATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000384 known = 1;
385 }
386 if (known) {
387 debug2("Server supports extension \"%s\" revision %s",
388 name, value);
389 } else {
390 debug2("Unrecognised server extension \"%s\"", name);
391 }
Damien Miller33804262001-02-04 23:20:18 +1100392 xfree(name);
393 xfree(value);
394 }
395
396 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100397
Damien Miller3db5f532002-02-13 14:10:32 +1100398 /* Some filexfer v.0 servers don't support large packets */
Damien Miller65e42f82010-09-24 22:15:11 +1000399 if (ret->version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000400 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100401
Damien Miller65e42f82010-09-24 22:15:11 +1000402 ret->limit_kbps = limit_kbps;
403 if (ret->limit_kbps > 0) {
404 bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
405 ret->transfer_buflen);
406 bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
407 ret->transfer_buflen);
408 }
409
410 return ret;
Damien Miller3db5f532002-02-13 14:10:32 +1100411}
412
413u_int
414sftp_proto_version(struct sftp_conn *conn)
415{
Damien Miller65e42f82010-09-24 22:15:11 +1000416 return conn->version;
Damien Miller33804262001-02-04 23:20:18 +1100417}
418
419int
Damien Miller3db5f532002-02-13 14:10:32 +1100420do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100421{
422 u_int id, status;
423 Buffer msg;
424
425 buffer_init(&msg);
426
Damien Miller3db5f532002-02-13 14:10:32 +1100427 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100428 buffer_put_char(&msg, SSH2_FXP_CLOSE);
429 buffer_put_int(&msg, id);
430 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000431 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000432 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100433
Damien Miller65e42f82010-09-24 22:15:11 +1000434 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100435 if (status != SSH2_FX_OK)
436 error("Couldn't close file: %s", fx2txt(status));
437
438 buffer_free(&msg);
439
Damien Miller65e42f82010-09-24 22:15:11 +1000440 return status;
Damien Miller33804262001-02-04 23:20:18 +1100441}
442
Damien Miller4870afd2001-03-14 10:27:09 +1100443
Ben Lindstrombba81212001-06-25 05:01:22 +0000444static int
Damien Miller3db5f532002-02-13 14:10:32 +1100445do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100446 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100447{
448 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000449 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100450 char *handle;
451
Damien Miller3db5f532002-02-13 14:10:32 +1100452 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100453
454 buffer_init(&msg);
455 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
456 buffer_put_int(&msg, id);
457 buffer_put_cstring(&msg, path);
Damien Miller65e42f82010-09-24 22:15:11 +1000458 send_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100459
460 buffer_clear(&msg);
461
Damien Miller65e42f82010-09-24 22:15:11 +1000462 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +1100463 "remote readdir(\"%s\")", path);
Damien Miller33804262001-02-04 23:20:18 +1100464 if (handle == NULL)
Damien Miller65e42f82010-09-24 22:15:11 +1000465 return -1;
Damien Miller33804262001-02-04 23:20:18 +1100466
Damien Miller4870afd2001-03-14 10:27:09 +1100467 if (dir) {
468 ents = 0;
469 *dir = xmalloc(sizeof(**dir));
470 (*dir)[0] = NULL;
471 }
Damien Miller4870afd2001-03-14 10:27:09 +1100472
Darren Tuckercdf547a2004-05-24 10:12:19 +1000473 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100474 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100475
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000476 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100477
478 buffer_clear(&msg);
479 buffer_put_char(&msg, SSH2_FXP_READDIR);
480 buffer_put_int(&msg, id);
481 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000482 send_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100483
484 buffer_clear(&msg);
485
Damien Miller65e42f82010-09-24 22:15:11 +1000486 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100487
488 type = buffer_get_char(&msg);
489 id = buffer_get_int(&msg);
490
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000491 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100492
493 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000494 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100495
496 if (type == SSH2_FXP_STATUS) {
497 int status = buffer_get_int(&msg);
498
499 debug3("Received SSH2_FXP_STATUS %d", status);
500
501 if (status == SSH2_FX_EOF) {
502 break;
503 } else {
504 error("Couldn't read directory: %s",
505 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100506 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100507 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000508 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100509 }
510 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000511 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100512 SSH2_FXP_NAME, type);
513
514 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100515 if (count == 0)
516 break;
517 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100518 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100519 char *filename, *longname;
520 Attrib *a;
521
522 filename = buffer_get_string(&msg, NULL);
523 longname = buffer_get_string(&msg, NULL);
524 a = decode_attrib(&msg);
525
Damien Miller4870afd2001-03-14 10:27:09 +1100526 if (printflag)
527 printf("%s\n", longname);
528
Darren Tucker1b0dd172009-10-07 08:37:48 +1100529 /*
530 * Directory entries should never contain '/'
531 * These can be used to attack recursive ops
532 * (e.g. send '../../../../etc/passwd')
533 */
534 if (strchr(filename, '/') != NULL) {
535 error("Server sent suspect path \"%s\" "
536 "during readdir of \"%s\"", filename, path);
537 goto next;
538 }
539
Damien Miller4870afd2001-03-14 10:27:09 +1100540 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100541 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100542 (*dir)[ents] = xmalloc(sizeof(***dir));
543 (*dir)[ents]->filename = xstrdup(filename);
544 (*dir)[ents]->longname = xstrdup(longname);
545 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
546 (*dir)[++ents] = NULL;
547 }
Darren Tucker1b0dd172009-10-07 08:37:48 +1100548 next:
Damien Miller33804262001-02-04 23:20:18 +1100549 xfree(filename);
550 xfree(longname);
551 }
552 }
553
554 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100555 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100556 xfree(handle);
557
Darren Tuckercdf547a2004-05-24 10:12:19 +1000558 /* Don't return partial matches on interrupt */
559 if (interrupted && dir != NULL && *dir != NULL) {
560 free_sftp_dirents(*dir);
561 *dir = xmalloc(sizeof(**dir));
562 **dir = NULL;
563 }
564
Damien Miller65e42f82010-09-24 22:15:11 +1000565 return 0;
Damien Miller33804262001-02-04 23:20:18 +1100566}
567
568int
Damien Miller3db5f532002-02-13 14:10:32 +1100569do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100570{
Damien Miller3db5f532002-02-13 14:10:32 +1100571 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100572}
573
574void free_sftp_dirents(SFTP_DIRENT **s)
575{
576 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100577
578 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100579 xfree(s[i]->filename);
580 xfree(s[i]->longname);
581 xfree(s[i]);
582 }
583 xfree(s);
584}
585
586int
Damien Miller3db5f532002-02-13 14:10:32 +1100587do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100588{
589 u_int status, id;
590
591 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
592
Damien Miller3db5f532002-02-13 14:10:32 +1100593 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000594 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
595 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100596 if (status != SSH2_FX_OK)
597 error("Couldn't delete file: %s", fx2txt(status));
598 return(status);
599}
600
601int
Darren Tucker1b0dd172009-10-07 08:37:48 +1100602do_mkdir(struct sftp_conn *conn, char *path, Attrib *a, int printflag)
Damien Miller33804262001-02-04 23:20:18 +1100603{
604 u_int status, id;
605
Damien Miller3db5f532002-02-13 14:10:32 +1100606 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000607 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100608 strlen(path), a);
609
Damien Miller65e42f82010-09-24 22:15:11 +1000610 status = get_status(conn, id);
Darren Tucker1b0dd172009-10-07 08:37:48 +1100611 if (status != SSH2_FX_OK && printflag)
Damien Miller33804262001-02-04 23:20:18 +1100612 error("Couldn't create directory: %s", fx2txt(status));
613
614 return(status);
615}
616
617int
Damien Miller3db5f532002-02-13 14:10:32 +1100618do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100619{
620 u_int status, id;
621
Damien Miller3db5f532002-02-13 14:10:32 +1100622 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000623 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100624 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100625
Damien Miller65e42f82010-09-24 22:15:11 +1000626 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100627 if (status != SSH2_FX_OK)
628 error("Couldn't remove directory: %s", fx2txt(status));
629
630 return(status);
631}
632
633Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100634do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100635{
636 u_int id;
637
Damien Miller3db5f532002-02-13 14:10:32 +1100638 id = conn->msg_id++;
639
Damien Miller65e42f82010-09-24 22:15:11 +1000640 send_string_request(conn, id,
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000641 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100642 path, strlen(path));
643
Damien Miller65e42f82010-09-24 22:15:11 +1000644 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100645}
646
647Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100648do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100649{
650 u_int id;
651
Damien Miller3db5f532002-02-13 14:10:32 +1100652 if (conn->version == 0) {
653 if (quiet)
654 debug("Server version does not support lstat operation");
655 else
Damien Miller996acd22003-04-09 20:59:48 +1000656 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000657 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100658 }
659
660 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000661 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100662 strlen(path));
663
Damien Miller65e42f82010-09-24 22:15:11 +1000664 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100665}
666
Damien Millercfe23d32008-02-10 22:20:44 +1100667#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100668Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100669do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100670{
671 u_int id;
672
Damien Miller3db5f532002-02-13 14:10:32 +1100673 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000674 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
Damien Miller3db5f532002-02-13 14:10:32 +1100675 handle_len);
676
Damien Miller65e42f82010-09-24 22:15:11 +1000677 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100678}
Damien Millercfe23d32008-02-10 22:20:44 +1100679#endif
Damien Miller33804262001-02-04 23:20:18 +1100680
681int
Damien Miller3db5f532002-02-13 14:10:32 +1100682do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100683{
684 u_int status, id;
685
Damien Miller3db5f532002-02-13 14:10:32 +1100686 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000687 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100688 strlen(path), a);
689
Damien Miller65e42f82010-09-24 22:15:11 +1000690 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100691 if (status != SSH2_FX_OK)
692 error("Couldn't setstat on \"%s\": %s", path,
693 fx2txt(status));
694
695 return(status);
696}
697
698int
Damien Miller3db5f532002-02-13 14:10:32 +1100699do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100700 Attrib *a)
701{
702 u_int status, id;
703
Damien Miller3db5f532002-02-13 14:10:32 +1100704 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000705 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100706 handle_len, a);
707
Damien Miller65e42f82010-09-24 22:15:11 +1000708 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100709 if (status != SSH2_FX_OK)
710 error("Couldn't fsetstat: %s", fx2txt(status));
711
712 return(status);
713}
714
715char *
Damien Miller3db5f532002-02-13 14:10:32 +1100716do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100717{
718 Buffer msg;
719 u_int type, expected_id, count, id;
720 char *filename, *longname;
721 Attrib *a;
722
Damien Miller3db5f532002-02-13 14:10:32 +1100723 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000724 send_string_request(conn, id, SSH2_FXP_REALPATH, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100725 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100726
727 buffer_init(&msg);
728
Damien Miller65e42f82010-09-24 22:15:11 +1000729 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100730 type = buffer_get_char(&msg);
731 id = buffer_get_int(&msg);
732
733 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000734 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100735
736 if (type == SSH2_FXP_STATUS) {
737 u_int status = buffer_get_int(&msg);
738
739 error("Couldn't canonicalise: %s", fx2txt(status));
Damien Miller49566312010-06-26 09:38:23 +1000740 buffer_free(&msg);
741 return NULL;
Damien Miller33804262001-02-04 23:20:18 +1100742 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000743 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100744 SSH2_FXP_NAME, type);
745
746 count = buffer_get_int(&msg);
747 if (count != 1)
748 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
749
750 filename = buffer_get_string(&msg, NULL);
751 longname = buffer_get_string(&msg, NULL);
752 a = decode_attrib(&msg);
753
754 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
755
756 xfree(longname);
757
758 buffer_free(&msg);
759
760 return(filename);
761}
762
763int
Damien Miller3db5f532002-02-13 14:10:32 +1100764do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100765{
766 Buffer msg;
767 u_int status, id;
768
769 buffer_init(&msg);
770
771 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100772 id = conn->msg_id++;
Damien Miller7a3e1d02008-03-27 10:59:57 +1100773 if ((conn->exts & SFTP_EXT_POSIX_RENAME)) {
774 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
775 buffer_put_int(&msg, id);
776 buffer_put_cstring(&msg, "posix-rename@openssh.com");
777 } else {
778 buffer_put_char(&msg, SSH2_FXP_RENAME);
779 buffer_put_int(&msg, id);
780 }
Damien Miller33804262001-02-04 23:20:18 +1100781 buffer_put_cstring(&msg, oldpath);
782 buffer_put_cstring(&msg, newpath);
Damien Miller65e42f82010-09-24 22:15:11 +1000783 send_msg(conn, &msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100784 debug3("Sent message %s \"%s\" -> \"%s\"",
785 (conn->exts & SFTP_EXT_POSIX_RENAME) ? "posix-rename@openssh.com" :
786 "SSH2_FXP_RENAME", oldpath, newpath);
Damien Miller33804262001-02-04 23:20:18 +1100787 buffer_free(&msg);
788
Damien Miller65e42f82010-09-24 22:15:11 +1000789 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100790 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100791 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
792 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100793
794 return(status);
795}
796
797int
Damien Miller3db5f532002-02-13 14:10:32 +1100798do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100799{
800 Buffer msg;
801 u_int status, id;
802
Damien Miller3db5f532002-02-13 14:10:32 +1100803 if (conn->version < 3) {
804 error("This server does not support the symlink operation");
805 return(SSH2_FX_OP_UNSUPPORTED);
806 }
807
Damien Miller058316f2001-03-08 10:08:49 +1100808 buffer_init(&msg);
809
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000810 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100811 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100812 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
813 buffer_put_int(&msg, id);
814 buffer_put_cstring(&msg, oldpath);
815 buffer_put_cstring(&msg, newpath);
Damien Miller65e42f82010-09-24 22:15:11 +1000816 send_msg(conn, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100817 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
818 newpath);
819 buffer_free(&msg);
820
Damien Miller65e42f82010-09-24 22:15:11 +1000821 status = get_status(conn, id);
Damien Miller058316f2001-03-08 10:08:49 +1100822 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000823 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100824 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100825
826 return(status);
827}
828
Damien Millercfe23d32008-02-10 22:20:44 +1100829#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +1100830char *
Damien Miller3db5f532002-02-13 14:10:32 +1100831do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100832{
833 Buffer msg;
834 u_int type, expected_id, count, id;
835 char *filename, *longname;
836 Attrib *a;
837
Damien Miller3db5f532002-02-13 14:10:32 +1100838 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000839 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100840
841 buffer_init(&msg);
842
Damien Miller65e42f82010-09-24 22:15:11 +1000843 get_msg(conn, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100844 type = buffer_get_char(&msg);
845 id = buffer_get_int(&msg);
846
847 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000848 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100849
850 if (type == SSH2_FXP_STATUS) {
851 u_int status = buffer_get_int(&msg);
852
853 error("Couldn't readlink: %s", fx2txt(status));
854 return(NULL);
855 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000856 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100857 SSH2_FXP_NAME, type);
858
859 count = buffer_get_int(&msg);
860 if (count != 1)
861 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
862
863 filename = buffer_get_string(&msg, NULL);
864 longname = buffer_get_string(&msg, NULL);
865 a = decode_attrib(&msg);
866
867 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
868
869 xfree(longname);
870
871 buffer_free(&msg);
872
873 return(filename);
874}
Damien Millercfe23d32008-02-10 22:20:44 +1100875#endif
Damien Miller058316f2001-03-08 10:08:49 +1100876
Damien Millerd671e5a2008-05-19 14:53:33 +1000877int
Darren Tucker7b598892008-06-09 22:49:36 +1000878do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
Damien Millerd671e5a2008-05-19 14:53:33 +1000879 int quiet)
880{
881 Buffer msg;
882 u_int id;
883
884 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
885 error("Server does not support statvfs@openssh.com extension");
886 return -1;
887 }
888
889 id = conn->msg_id++;
890
891 buffer_init(&msg);
892 buffer_clear(&msg);
893 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
894 buffer_put_int(&msg, id);
895 buffer_put_cstring(&msg, "statvfs@openssh.com");
896 buffer_put_cstring(&msg, path);
Damien Miller65e42f82010-09-24 22:15:11 +1000897 send_msg(conn, &msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000898 buffer_free(&msg);
899
Damien Miller65e42f82010-09-24 22:15:11 +1000900 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +1000901}
902
903#ifdef notyet
904int
905do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
Darren Tucker7b598892008-06-09 22:49:36 +1000906 struct sftp_statvfs *st, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000907{
908 Buffer msg;
909 u_int id;
910
911 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
912 error("Server does not support fstatvfs@openssh.com extension");
913 return -1;
914 }
915
916 id = conn->msg_id++;
917
918 buffer_init(&msg);
919 buffer_clear(&msg);
920 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
921 buffer_put_int(&msg, id);
922 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
923 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000924 send_msg(conn, &msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000925 buffer_free(&msg);
926
Damien Miller65e42f82010-09-24 22:15:11 +1000927 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +1000928}
929#endif
930
Damien Miller16a13332002-02-13 14:03:56 +1100931static void
Damien Miller65e42f82010-09-24 22:15:11 +1000932send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
933 u_int len, char *handle, u_int handle_len)
Damien Miller16a13332002-02-13 14:03:56 +1100934{
935 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000936
Damien Miller16a13332002-02-13 14:03:56 +1100937 buffer_init(&msg);
938 buffer_clear(&msg);
939 buffer_put_char(&msg, SSH2_FXP_READ);
940 buffer_put_int(&msg, id);
941 buffer_put_string(&msg, handle, handle_len);
942 buffer_put_int64(&msg, offset);
943 buffer_put_int(&msg, len);
Damien Miller65e42f82010-09-24 22:15:11 +1000944 send_msg(conn, &msg);
Damien Miller16a13332002-02-13 14:03:56 +1100945 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000946}
Damien Miller16a13332002-02-13 14:03:56 +1100947
Damien Miller058316f2001-03-08 10:08:49 +1100948int
Damien Miller3db5f532002-02-13 14:10:32 +1100949do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
Darren Tucker1b0dd172009-10-07 08:37:48 +1100950 Attrib *a, int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100951{
Darren Tucker1b0dd172009-10-07 08:37:48 +1100952 Attrib junk;
Damien Miller16a13332002-02-13 14:03:56 +1100953 Buffer msg;
954 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000955 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100956 int read_error, write_errno;
957 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000958 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100959 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100960 struct request {
961 u_int id;
962 u_int len;
963 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000964 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100965 };
966 TAILQ_HEAD(reqhead, request) requests;
967 struct request *req;
968
969 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100970
Darren Tucker1b0dd172009-10-07 08:37:48 +1100971 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
972 return -1;
Damien Miller33804262001-02-04 23:20:18 +1100973
Damien Miller9e720282008-06-29 22:46:35 +1000974 /* Do not preserve set[ug]id here, as we do not preserve ownership */
Damien Miller33804262001-02-04 23:20:18 +1100975 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100976 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100977 else
978 mode = 0666;
979
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000980 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100981 (!S_ISREG(a->perm))) {
982 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000983 return(-1);
984 }
985
Damien Miller16a13332002-02-13 14:03:56 +1100986 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
987 size = a->size;
988 else
989 size = 0;
990
Damien Miller3db5f532002-02-13 14:10:32 +1100991 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100992 buffer_init(&msg);
993
994 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100995 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100996 buffer_put_char(&msg, SSH2_FXP_OPEN);
997 buffer_put_int(&msg, id);
998 buffer_put_cstring(&msg, remote_path);
999 buffer_put_int(&msg, SSH2_FXF_READ);
1000 attrib_clear(&junk); /* Send empty attributes */
1001 encode_attrib(&msg, &junk);
Damien Miller65e42f82010-09-24 22:15:11 +10001002 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001003 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001004
Damien Miller65e42f82010-09-24 22:15:11 +10001005 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001006 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001007 if (handle == NULL) {
1008 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +11001009 return(-1);
1010 }
1011
Damien Millera8e06ce2003-11-21 23:48:55 +11001012 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +11001013 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +11001014 if (local_fd == -1) {
1015 error("Couldn't open local file \"%s\" for writing: %s",
1016 local_path, strerror(errno));
Damien Miller6b0c8182008-02-10 22:23:41 +11001017 do_close(conn, handle, handle_len);
Ben Lindstrom021fcd32002-02-26 18:02:43 +00001018 buffer_free(&msg);
1019 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001020 return(-1);
1021 }
1022
Damien Miller33804262001-02-04 23:20:18 +11001023 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +11001024 write_error = read_error = write_errno = num_req = offset = 0;
1025 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +11001026 progress_counter = 0;
1027
Damien Miller9ba30692004-03-08 23:12:02 +11001028 if (showprogress && size != 0)
1029 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001030
Damien Miller16a13332002-02-13 14:03:56 +11001031 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +11001032 char *data;
Damien Miller16a13332002-02-13 14:03:56 +11001033 u_int len;
Damien Miller33804262001-02-04 23:20:18 +11001034
Darren Tuckercdf547a2004-05-24 10:12:19 +10001035 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001036 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001037 * allow outstanding requests to drain gracefully
1038 */
1039 if (interrupted) {
1040 if (num_req == 0) /* If we haven't started yet... */
1041 break;
1042 max_req = 0;
1043 }
1044
Damien Miller16a13332002-02-13 14:03:56 +11001045 /* Send some more requests */
1046 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001047 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +00001048 (unsigned long long)offset,
1049 (unsigned long long)offset + buflen - 1,
1050 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +11001051 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +11001052 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001053 req->len = buflen;
1054 req->offset = offset;
1055 offset += buflen;
1056 num_req++;
1057 TAILQ_INSERT_TAIL(&requests, req, tq);
Damien Miller65e42f82010-09-24 22:15:11 +10001058 send_read_request(conn, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001059 req->len, handle, handle_len);
1060 }
Damien Miller33804262001-02-04 23:20:18 +11001061
1062 buffer_clear(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +10001063 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +11001064 type = buffer_get_char(&msg);
1065 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001066 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001067
Damien Miller16a13332002-02-13 14:03:56 +11001068 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001069 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001070 req != NULL && req->id != id;
1071 req = TAILQ_NEXT(req, tq))
1072 ;
1073 if (req == NULL)
1074 fatal("Unexpected reply %u", id);
1075
1076 switch (type) {
1077 case SSH2_FXP_STATUS:
1078 status = buffer_get_int(&msg);
1079 if (status != SSH2_FX_EOF)
1080 read_error = 1;
1081 max_req = 0;
1082 TAILQ_REMOVE(&requests, req, tq);
1083 xfree(req);
1084 num_req--;
1085 break;
1086 case SSH2_FXP_DATA:
1087 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +00001088 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001089 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001090 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001091 if (len > req->len)
1092 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +00001093 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001094 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001095 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001096 !write_error) {
1097 write_errno = errno;
1098 write_error = 1;
1099 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001100 }
Damien Miller62d57f62003-01-10 21:43:24 +11001101 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +11001102 xfree(data);
1103
1104 if (len == req->len) {
1105 TAILQ_REMOVE(&requests, req, tq);
1106 xfree(req);
1107 num_req--;
1108 } else {
1109 /* Resend the request for the missing data */
1110 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001111 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001112 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001113 (unsigned long long)req->offset +
1114 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001115 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001116 req->len -= len;
1117 req->offset += len;
Damien Miller65e42f82010-09-24 22:15:11 +10001118 send_read_request(conn, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001119 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001120 /* Reduce the request size */
1121 if (len < buflen)
1122 buflen = MAX(MIN_READ_SIZE, len);
1123 }
1124 if (max_req > 0) { /* max_req = 0 iff EOF received */
1125 if (size > 0 && offset > size) {
1126 /* Only one request at a time
1127 * after the expected EOF */
1128 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001129 (unsigned long long)offset,
1130 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001131 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001132 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001133 ++max_req;
1134 }
1135 }
1136 break;
1137 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001138 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001139 SSH2_FXP_DATA, type);
1140 }
Damien Miller33804262001-02-04 23:20:18 +11001141 }
Damien Miller33804262001-02-04 23:20:18 +11001142
Damien Miller62d57f62003-01-10 21:43:24 +11001143 if (showprogress && size)
1144 stop_progress_meter();
1145
Damien Miller16a13332002-02-13 14:03:56 +11001146 /* Sanity check */
1147 if (TAILQ_FIRST(&requests) != NULL)
1148 fatal("Transfer complete, but requests still in queue");
1149
1150 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001151 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001152 remote_path, fx2txt(status));
1153 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001154 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001155 error("Couldn't write to \"%s\": %s", local_path,
1156 strerror(write_errno));
1157 status = -1;
1158 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001159 } else {
Damien Miller3db5f532002-02-13 14:10:32 +11001160 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001161
1162 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001163#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +11001164 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001165#else
Damien Miller16a13332002-02-13 14:03:56 +11001166 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001167#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001168 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001169 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001170 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1171 struct timeval tv[2];
1172 tv[0].tv_sec = a->atime;
1173 tv[1].tv_sec = a->mtime;
1174 tv[0].tv_usec = tv[1].tv_usec = 0;
1175 if (utimes(local_path, tv) == -1)
1176 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001177 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001178 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001179 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001180 close(local_fd);
1181 buffer_free(&msg);
1182 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001183
1184 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001185}
1186
Darren Tucker1b0dd172009-10-07 08:37:48 +11001187static int
1188download_dir_internal(struct sftp_conn *conn, char *src, char *dst,
1189 Attrib *dirattrib, int pflag, int printflag, int depth)
1190{
1191 int i, ret = 0;
1192 SFTP_DIRENT **dir_entries;
1193 char *filename, *new_src, *new_dst;
1194 mode_t mode = 0777;
1195
1196 if (depth >= MAX_DIR_DEPTH) {
1197 error("Maximum directory depth exceeded: %d levels", depth);
1198 return -1;
1199 }
1200
1201 if (dirattrib == NULL &&
1202 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1203 error("Unable to stat remote directory \"%s\"", src);
1204 return -1;
1205 }
1206 if (!S_ISDIR(dirattrib->perm)) {
1207 error("\"%s\" is not a directory", src);
1208 return -1;
1209 }
1210 if (printflag)
1211 printf("Retrieving %s\n", src);
1212
1213 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1214 mode = dirattrib->perm & 01777;
1215 else {
1216 debug("Server did not send permissions for "
1217 "directory \"%s\"", dst);
1218 }
1219
1220 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1221 error("mkdir %s: %s", dst, strerror(errno));
1222 return -1;
1223 }
1224
1225 if (do_readdir(conn, src, &dir_entries) == -1) {
1226 error("%s: Failed to get directory contents", src);
1227 return -1;
1228 }
1229
1230 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1231 filename = dir_entries[i]->filename;
1232
1233 new_dst = path_append(dst, filename);
1234 new_src = path_append(src, filename);
1235
1236 if (S_ISDIR(dir_entries[i]->a.perm)) {
1237 if (strcmp(filename, ".") == 0 ||
1238 strcmp(filename, "..") == 0)
1239 continue;
1240 if (download_dir_internal(conn, new_src, new_dst,
1241 &(dir_entries[i]->a), pflag, printflag,
1242 depth + 1) == -1)
1243 ret = -1;
1244 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1245 if (do_download(conn, new_src, new_dst,
1246 &(dir_entries[i]->a), pflag) == -1) {
1247 error("Download of file %s to %s failed",
1248 new_src, new_dst);
1249 ret = -1;
1250 }
1251 } else
1252 logit("%s: not a regular file\n", new_src);
1253
1254 xfree(new_dst);
1255 xfree(new_src);
1256 }
1257
1258 if (pflag) {
1259 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1260 struct timeval tv[2];
1261 tv[0].tv_sec = dirattrib->atime;
1262 tv[1].tv_sec = dirattrib->mtime;
1263 tv[0].tv_usec = tv[1].tv_usec = 0;
1264 if (utimes(dst, tv) == -1)
1265 error("Can't set times on \"%s\": %s",
1266 dst, strerror(errno));
1267 } else
1268 debug("Server did not send times for directory "
1269 "\"%s\"", dst);
1270 }
1271
1272 free_sftp_dirents(dir_entries);
1273
1274 return ret;
1275}
1276
1277int
1278download_dir(struct sftp_conn *conn, char *src, char *dst,
1279 Attrib *dirattrib, int pflag, int printflag)
1280{
1281 char *src_canon;
1282 int ret;
1283
1284 if ((src_canon = do_realpath(conn, src)) == NULL) {
1285 error("Unable to canonicalise path \"%s\"", src);
1286 return -1;
1287 }
1288
1289 ret = download_dir_internal(conn, src_canon, dst,
1290 dirattrib, pflag, printflag, 0);
1291 xfree(src_canon);
1292 return ret;
1293}
1294
Damien Miller33804262001-02-04 23:20:18 +11001295int
Damien Miller3db5f532002-02-13 14:10:32 +11001296do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
1297 int pflag)
Damien Miller33804262001-02-04 23:20:18 +11001298{
Damien Milleracdf25b2008-02-10 22:27:24 +11001299 int local_fd;
1300 int status = SSH2_FX_OK;
Damien Miller5873dfd2002-02-13 14:04:37 +11001301 u_int handle_len, id, type;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001302 off_t offset;
Damien Miller8829d362002-02-08 22:04:05 +11001303 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +11001304 Buffer msg;
1305 struct stat sb;
1306 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +11001307 u_int32_t startid;
1308 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001309 struct outstanding_ack {
1310 u_int id;
1311 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001312 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001313 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001314 };
1315 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001316 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001317
1318 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001319
1320 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1321 error("Couldn't open local file \"%s\" for reading: %s",
1322 local_path, strerror(errno));
1323 return(-1);
1324 }
1325 if (fstat(local_fd, &sb) == -1) {
1326 error("Couldn't fstat local file \"%s\": %s",
1327 local_path, strerror(errno));
1328 close(local_fd);
1329 return(-1);
1330 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001331 if (!S_ISREG(sb.st_mode)) {
1332 error("%s is not a regular file", local_path);
1333 close(local_fd);
1334 return(-1);
1335 }
Damien Miller33804262001-02-04 23:20:18 +11001336 stat_to_attrib(&sb, &a);
1337
1338 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1339 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1340 a.perm &= 0777;
1341 if (!pflag)
1342 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1343
1344 buffer_init(&msg);
1345
1346 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001347 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001348 buffer_put_char(&msg, SSH2_FXP_OPEN);
1349 buffer_put_int(&msg, id);
1350 buffer_put_cstring(&msg, remote_path);
1351 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1352 encode_attrib(&msg, &a);
Damien Miller65e42f82010-09-24 22:15:11 +10001353 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001354 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001355
1356 buffer_clear(&msg);
1357
Damien Miller65e42f82010-09-24 22:15:11 +10001358 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001359 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001360 if (handle == NULL) {
1361 close(local_fd);
1362 buffer_free(&msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001363 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001364 }
1365
Damien Miller16a13332002-02-13 14:03:56 +11001366 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001367 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001368
Damien Miller33804262001-02-04 23:20:18 +11001369 /* Read from local and write to remote */
1370 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001371 if (showprogress)
1372 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001373
Damien Miller9f0f5c62001-12-21 14:45:46 +11001374 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001375 int len;
Damien Miller33804262001-02-04 23:20:18 +11001376
1377 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001378 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001379 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001380 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001381 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001382 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001383 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001384 len = 0;
1385 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001386 len = read(local_fd, data, conn->transfer_buflen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001387 while ((len == -1) &&
1388 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
Damien Miller33804262001-02-04 23:20:18 +11001389
1390 if (len == -1)
1391 fatal("Couldn't read from \"%s\": %s", local_path,
1392 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001393
1394 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001395 ack = xmalloc(sizeof(*ack));
1396 ack->id = ++id;
1397 ack->offset = offset;
1398 ack->len = len;
1399 TAILQ_INSERT_TAIL(&acks, ack, tq);
1400
Damien Miller16a13332002-02-13 14:03:56 +11001401 buffer_clear(&msg);
1402 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001403 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001404 buffer_put_string(&msg, handle, handle_len);
1405 buffer_put_int64(&msg, offset);
1406 buffer_put_string(&msg, data, len);
Damien Miller65e42f82010-09-24 22:15:11 +10001407 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001408 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001409 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001410 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001411 break;
1412
Damien Miller5873dfd2002-02-13 14:04:37 +11001413 if (ack == NULL)
1414 fatal("Unexpected ACK %u", id);
1415
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001416 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001417 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001418 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001419
Damien Miller5873dfd2002-02-13 14:04:37 +11001420 buffer_clear(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +10001421 get_msg(conn, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001422 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001423 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001424
1425 if (type != SSH2_FXP_STATUS)
1426 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1427 "got %d", SSH2_FXP_STATUS, type);
1428
1429 status = buffer_get_int(&msg);
1430 debug3("SSH2_FXP_STATUS %d", status);
1431
1432 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001433 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001434 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001435 ack = TAILQ_NEXT(ack, tq))
1436 ;
1437 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001438 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001439 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001440 debug3("In write loop, ack for %u %u bytes at %lld",
1441 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001442 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001443 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001444 }
Damien Miller33804262001-02-04 23:20:18 +11001445 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001446 if (offset < 0)
1447 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001448 }
Damien Milleracdf25b2008-02-10 22:27:24 +11001449 buffer_free(&msg);
1450
Damien Miller62d57f62003-01-10 21:43:24 +11001451 if (showprogress)
1452 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001453 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001454
Damien Milleracdf25b2008-02-10 22:27:24 +11001455 if (status != SSH2_FX_OK) {
1456 error("Couldn't write to remote file \"%s\": %s",
1457 remote_path, fx2txt(status));
1458 status = -1;
1459 }
1460
Damien Miller33804262001-02-04 23:20:18 +11001461 if (close(local_fd) == -1) {
1462 error("Couldn't close local file \"%s\": %s", local_path,
1463 strerror(errno));
Damien Millerd7686fd2001-02-10 00:40:03 +11001464 status = -1;
Damien Miller33804262001-02-04 23:20:18 +11001465 }
1466
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001467 /* Override umask and utimes if asked */
1468 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001469 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001470
Damien Milleracdf25b2008-02-10 22:27:24 +11001471 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
1472 status = -1;
Damien Millerd7686fd2001-02-10 00:40:03 +11001473 xfree(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001474
1475 return status;
Damien Miller33804262001-02-04 23:20:18 +11001476}
Darren Tucker1b0dd172009-10-07 08:37:48 +11001477
1478static int
1479upload_dir_internal(struct sftp_conn *conn, char *src, char *dst,
1480 int pflag, int printflag, int depth)
1481{
1482 int ret = 0, status;
1483 DIR *dirp;
1484 struct dirent *dp;
1485 char *filename, *new_src, *new_dst;
1486 struct stat sb;
1487 Attrib a;
1488
1489 if (depth >= MAX_DIR_DEPTH) {
1490 error("Maximum directory depth exceeded: %d levels", depth);
1491 return -1;
1492 }
1493
1494 if (stat(src, &sb) == -1) {
1495 error("Couldn't stat directory \"%s\": %s",
1496 src, strerror(errno));
1497 return -1;
1498 }
1499 if (!S_ISDIR(sb.st_mode)) {
1500 error("\"%s\" is not a directory", src);
1501 return -1;
1502 }
1503 if (printflag)
1504 printf("Entering %s\n", src);
1505
1506 attrib_clear(&a);
1507 stat_to_attrib(&sb, &a);
1508 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1509 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1510 a.perm &= 01777;
1511 if (!pflag)
1512 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1513
1514 status = do_mkdir(conn, dst, &a, 0);
1515 /*
1516 * we lack a portable status for errno EEXIST,
1517 * so if we get a SSH2_FX_FAILURE back we must check
1518 * if it was created successfully.
1519 */
1520 if (status != SSH2_FX_OK) {
1521 if (status != SSH2_FX_FAILURE)
1522 return -1;
1523 if (do_stat(conn, dst, 0) == NULL)
1524 return -1;
1525 }
1526
1527 if ((dirp = opendir(src)) == NULL) {
1528 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1529 return -1;
1530 }
1531
1532 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1533 if (dp->d_ino == 0)
1534 continue;
1535 filename = dp->d_name;
1536 new_dst = path_append(dst, filename);
1537 new_src = path_append(src, filename);
1538
Darren Tucker438b4732009-10-11 21:52:10 +11001539 if (lstat(new_src, &sb) == -1) {
1540 logit("%s: lstat failed: %s", filename,
1541 strerror(errno));
1542 ret = -1;
1543 } else if (S_ISDIR(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001544 if (strcmp(filename, ".") == 0 ||
1545 strcmp(filename, "..") == 0)
1546 continue;
1547
1548 if (upload_dir_internal(conn, new_src, new_dst,
Damien Millerc4bb91c2010-08-03 16:04:22 +10001549 pflag, printflag, depth + 1) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001550 ret = -1;
Darren Tucker438b4732009-10-11 21:52:10 +11001551 } else if (S_ISREG(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001552 if (do_upload(conn, new_src, new_dst, pflag) == -1) {
1553 error("Uploading of file %s to %s failed!",
1554 new_src, new_dst);
1555 ret = -1;
1556 }
1557 } else
1558 logit("%s: not a regular file\n", filename);
1559 xfree(new_dst);
1560 xfree(new_src);
1561 }
1562
1563 do_setstat(conn, dst, &a);
1564
1565 (void) closedir(dirp);
1566 return ret;
1567}
1568
1569int
1570upload_dir(struct sftp_conn *conn, char *src, char *dst, int printflag,
1571 int pflag)
1572{
1573 char *dst_canon;
1574 int ret;
1575
1576 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1577 error("Unable to canonicalise path \"%s\"", dst);
1578 return -1;
1579 }
1580
1581 ret = upload_dir_internal(conn, src, dst_canon, pflag, printflag, 0);
1582 xfree(dst_canon);
1583 return ret;
1584}
1585
1586char *
1587path_append(char *p1, char *p2)
1588{
1589 char *ret;
1590 size_t len = strlen(p1) + strlen(p2) + 2;
1591
1592 ret = xmalloc(len);
1593 strlcpy(ret, p1, len);
1594 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1595 strlcat(ret, "/", len);
1596 strlcat(ret, p2, len);
1597
1598 return(ret);
1599}
1600