blob: cc4a5b15bfca99133c31ae39954ced44877e6ae9 [file] [log] [blame]
Darren Tucker1b0dd172009-10-07 08:37:48 +11001/* $OpenBSD: sftp-client.c,v 1.89 2009/08/18 18:36:20 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 Miller3db5f532002-02-13 14:10:32 +110079};
Ben Lindstrom288cc392001-02-09 02:58:04 +000080
Darren Tuckerc22f0902009-10-07 08:24:19 +110081static char *
82get_handle(int fd, u_int expected_id, u_int *len, const char *errfmt, ...)
83 __attribute__((format(printf, 4, 5)));
84
Ben Lindstrombba81212001-06-25 05:01:22 +000085static void
Damien Miller33804262001-02-04 23:20:18 +110086send_msg(int fd, Buffer *m)
87{
Damien Millera7f3aaa2003-01-10 21:43:58 +110088 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +100089 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +110090
Damien Miller54446182006-01-02 23:40:50 +110091 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +110092 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +110093
Damien Millera7f3aaa2003-01-10 21:43:58 +110094 /* Send length first */
Damien Miller3f941882006-03-31 23:13:02 +110095 put_u32(mlen, buffer_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +100096 iov[0].iov_base = mlen;
97 iov[0].iov_len = sizeof(mlen);
98 iov[1].iov_base = buffer_ptr(m);
99 iov[1].iov_len = buffer_len(m);
Damien Millerd7834352006-08-05 12:39:39 +1000100
Damien Miller58ca98b2006-04-23 12:06:35 +1000101 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +1100102 fatal("Couldn't send packet: %s", strerror(errno));
103
104 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +1100105}
106
Ben Lindstrombba81212001-06-25 05:01:22 +0000107static void
Damien Miller33804262001-02-04 23:20:18 +1100108get_msg(int fd, Buffer *m)
109{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100110 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +1100111
Damien Millera7f3aaa2003-01-10 21:43:58 +1100112 buffer_append_space(m, 4);
Damien Millerb253cc42005-05-26 12:23:44 +1000113 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
114 if (errno == EPIPE)
115 fatal("Connection closed");
116 else
117 fatal("Couldn't read packet: %s", strerror(errno));
118 }
Damien Miller33804262001-02-04 23:20:18 +1100119
Damien Millera7f3aaa2003-01-10 21:43:58 +1100120 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +1100121 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000122 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100123
Damien Millera7f3aaa2003-01-10 21:43:58 +1100124 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000125 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
126 if (errno == EPIPE)
127 fatal("Connection closed");
128 else
129 fatal("Read packet: %s", strerror(errno));
130 }
Damien Miller33804262001-02-04 23:20:18 +1100131}
132
Ben Lindstrombba81212001-06-25 05:01:22 +0000133static void
Damien Miller33804262001-02-04 23:20:18 +1100134send_string_request(int fd, u_int id, u_int code, char *s,
135 u_int len)
136{
137 Buffer msg;
138
139 buffer_init(&msg);
140 buffer_put_char(&msg, code);
141 buffer_put_int(&msg, id);
142 buffer_put_string(&msg, s, len);
143 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000144 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100145 buffer_free(&msg);
146}
147
Ben Lindstrombba81212001-06-25 05:01:22 +0000148static void
Damien Miller33804262001-02-04 23:20:18 +1100149send_string_attrs_request(int fd, u_int id, u_int code, char *s,
150 u_int len, Attrib *a)
151{
152 Buffer msg;
153
154 buffer_init(&msg);
155 buffer_put_char(&msg, code);
156 buffer_put_int(&msg, id);
157 buffer_put_string(&msg, s, len);
158 encode_attrib(&msg, a);
159 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000160 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100161 buffer_free(&msg);
162}
163
Ben Lindstrombba81212001-06-25 05:01:22 +0000164static u_int
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000165get_status(int fd, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100166{
167 Buffer msg;
168 u_int type, id, status;
169
170 buffer_init(&msg);
171 get_msg(fd, &msg);
172 type = buffer_get_char(&msg);
173 id = buffer_get_int(&msg);
174
175 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000176 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100177 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000178 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100179 SSH2_FXP_STATUS, type);
180
181 status = buffer_get_int(&msg);
182 buffer_free(&msg);
183
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000184 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100185
186 return(status);
187}
188
Ben Lindstrombba81212001-06-25 05:01:22 +0000189static char *
Darren Tuckerc22f0902009-10-07 08:24:19 +1100190get_handle(int fd, u_int expected_id, u_int *len, const char *errfmt, ...)
Damien Miller33804262001-02-04 23:20:18 +1100191{
192 Buffer msg;
193 u_int type, id;
Darren Tuckerc22f0902009-10-07 08:24:19 +1100194 char *handle, errmsg[256];
195 va_list args;
196 int status;
197
198 va_start(args, errfmt);
199 if (errfmt != NULL)
200 vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
201 va_end(args);
Damien Miller33804262001-02-04 23:20:18 +1100202
203 buffer_init(&msg);
204 get_msg(fd, &msg);
205 type = buffer_get_char(&msg);
206 id = buffer_get_int(&msg);
207
208 if (id != expected_id)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100209 fatal("%s: ID mismatch (%u != %u)",
210 errfmt == NULL ? __func__ : errmsg, id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100211 if (type == SSH2_FXP_STATUS) {
Darren Tuckerc22f0902009-10-07 08:24:19 +1100212 status = buffer_get_int(&msg);
213 if (errfmt != NULL)
214 error("%s: %s", errmsg, fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100215 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100216 return(NULL);
217 } else if (type != SSH2_FXP_HANDLE)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100218 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
219 errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
Damien Miller33804262001-02-04 23:20:18 +1100220
221 handle = buffer_get_string(&msg, len);
222 buffer_free(&msg);
223
224 return(handle);
225}
226
Ben Lindstrombba81212001-06-25 05:01:22 +0000227static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000228get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100229{
230 Buffer msg;
231 u_int type, id;
232 Attrib *a;
233
234 buffer_init(&msg);
235 get_msg(fd, &msg);
236
237 type = buffer_get_char(&msg);
238 id = buffer_get_int(&msg);
239
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000240 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100241 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000242 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100243 if (type == SSH2_FXP_STATUS) {
244 int status = buffer_get_int(&msg);
245
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000246 if (quiet)
247 debug("Couldn't stat remote file: %s", fx2txt(status));
248 else
249 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100250 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100251 return(NULL);
252 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000253 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100254 SSH2_FXP_ATTRS, type);
255 }
256 a = decode_attrib(&msg);
257 buffer_free(&msg);
258
259 return(a);
260}
261
Damien Millerd671e5a2008-05-19 14:53:33 +1000262static int
Darren Tucker7b598892008-06-09 22:49:36 +1000263get_decode_statvfs(int fd, struct sftp_statvfs *st, u_int expected_id,
264 int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000265{
266 Buffer msg;
267 u_int type, id, flag;
268
269 buffer_init(&msg);
270 get_msg(fd, &msg);
271
272 type = buffer_get_char(&msg);
273 id = buffer_get_int(&msg);
274
275 debug3("Received statvfs reply T:%u I:%u", type, id);
276 if (id != expected_id)
277 fatal("ID mismatch (%u != %u)", id, expected_id);
278 if (type == SSH2_FXP_STATUS) {
279 int status = buffer_get_int(&msg);
280
281 if (quiet)
282 debug("Couldn't statvfs: %s", fx2txt(status));
283 else
284 error("Couldn't statvfs: %s", fx2txt(status));
285 buffer_free(&msg);
286 return -1;
287 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
288 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
289 SSH2_FXP_EXTENDED_REPLY, type);
290 }
291
292 bzero(st, sizeof(*st));
Darren Tucker7b598892008-06-09 22:49:36 +1000293 st->f_bsize = buffer_get_int64(&msg);
294 st->f_frsize = buffer_get_int64(&msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000295 st->f_blocks = buffer_get_int64(&msg);
296 st->f_bfree = buffer_get_int64(&msg);
297 st->f_bavail = buffer_get_int64(&msg);
298 st->f_files = buffer_get_int64(&msg);
299 st->f_ffree = buffer_get_int64(&msg);
300 st->f_favail = buffer_get_int64(&msg);
Darren Tucker294b8412008-06-08 12:57:08 +1000301 st->f_fsid = buffer_get_int64(&msg);
Darren Tucker7b598892008-06-09 22:49:36 +1000302 flag = buffer_get_int64(&msg);
303 st->f_namemax = buffer_get_int64(&msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000304
305 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
306 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
307
308 buffer_free(&msg);
309
310 return 0;
311}
312
Damien Miller3db5f532002-02-13 14:10:32 +1100313struct sftp_conn *
314do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100315{
Damien Miller7a3e1d02008-03-27 10:59:57 +1100316 u_int type, exts = 0;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000317 int version;
Damien Miller33804262001-02-04 23:20:18 +1100318 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100319 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100320
321 buffer_init(&msg);
322 buffer_put_char(&msg, SSH2_FXP_INIT);
323 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
324 send_msg(fd_out, &msg);
325
326 buffer_clear(&msg);
327
328 get_msg(fd_in, &msg);
329
Kevin Stevesef4eea92001-02-05 12:42:17 +0000330 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100331 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000332 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100333 type);
334 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100335 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100336 }
337 version = buffer_get_int(&msg);
338
339 debug2("Remote version: %d", version);
340
341 /* Check for extensions */
342 while (buffer_len(&msg) > 0) {
343 char *name = buffer_get_string(&msg, NULL);
344 char *value = buffer_get_string(&msg, NULL);
Darren Tuckera64ab332008-06-13 07:01:29 +1000345 int known = 0;
Damien Miller33804262001-02-04 23:20:18 +1100346
Damien Millerd671e5a2008-05-19 14:53:33 +1000347 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
Darren Tuckera64ab332008-06-13 07:01:29 +1000348 strcmp(value, "1") == 0) {
Damien Miller7a3e1d02008-03-27 10:59:57 +1100349 exts |= SFTP_EXT_POSIX_RENAME;
Darren Tuckera64ab332008-06-13 07:01:29 +1000350 known = 1;
351 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
352 strcmp(value, "2") == 0) {
Damien Millerd671e5a2008-05-19 14:53:33 +1000353 exts |= SFTP_EXT_STATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000354 known = 1;
355 } if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
356 strcmp(value, "2") == 0) {
Damien Millerd671e5a2008-05-19 14:53:33 +1000357 exts |= SFTP_EXT_FSTATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000358 known = 1;
359 }
360 if (known) {
361 debug2("Server supports extension \"%s\" revision %s",
362 name, value);
363 } else {
364 debug2("Unrecognised server extension \"%s\"", name);
365 }
Damien Miller33804262001-02-04 23:20:18 +1100366 xfree(name);
367 xfree(value);
368 }
369
370 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100371
Damien Miller3db5f532002-02-13 14:10:32 +1100372 ret = xmalloc(sizeof(*ret));
373 ret->fd_in = fd_in;
374 ret->fd_out = fd_out;
375 ret->transfer_buflen = transfer_buflen;
376 ret->num_requests = num_requests;
377 ret->version = version;
378 ret->msg_id = 1;
Damien Miller7a3e1d02008-03-27 10:59:57 +1100379 ret->exts = exts;
Damien Miller3db5f532002-02-13 14:10:32 +1100380
381 /* Some filexfer v.0 servers don't support large packets */
382 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000383 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100384
385 return(ret);
386}
387
388u_int
389sftp_proto_version(struct sftp_conn *conn)
390{
391 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100392}
393
394int
Damien Miller3db5f532002-02-13 14:10:32 +1100395do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100396{
397 u_int id, status;
398 Buffer msg;
399
400 buffer_init(&msg);
401
Damien Miller3db5f532002-02-13 14:10:32 +1100402 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100403 buffer_put_char(&msg, SSH2_FXP_CLOSE);
404 buffer_put_int(&msg, id);
405 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100406 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000407 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100408
Damien Miller3db5f532002-02-13 14:10:32 +1100409 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100410 if (status != SSH2_FX_OK)
411 error("Couldn't close file: %s", fx2txt(status));
412
413 buffer_free(&msg);
414
415 return(status);
416}
417
Damien Miller4870afd2001-03-14 10:27:09 +1100418
Ben Lindstrombba81212001-06-25 05:01:22 +0000419static int
Damien Miller3db5f532002-02-13 14:10:32 +1100420do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100421 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100422{
423 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000424 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100425 char *handle;
426
Damien Miller3db5f532002-02-13 14:10:32 +1100427 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100428
429 buffer_init(&msg);
430 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
431 buffer_put_int(&msg, id);
432 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100433 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100434
435 buffer_clear(&msg);
436
Darren Tuckerc22f0902009-10-07 08:24:19 +1100437 handle = get_handle(conn->fd_in, id, &handle_len,
438 "remote readdir(\"%s\")", path);
Damien Miller33804262001-02-04 23:20:18 +1100439 if (handle == NULL)
440 return(-1);
441
Damien Miller4870afd2001-03-14 10:27:09 +1100442 if (dir) {
443 ents = 0;
444 *dir = xmalloc(sizeof(**dir));
445 (*dir)[0] = NULL;
446 }
Damien Miller4870afd2001-03-14 10:27:09 +1100447
Darren Tuckercdf547a2004-05-24 10:12:19 +1000448 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100449 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100450
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000451 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100452
453 buffer_clear(&msg);
454 buffer_put_char(&msg, SSH2_FXP_READDIR);
455 buffer_put_int(&msg, id);
456 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100457 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100458
459 buffer_clear(&msg);
460
Damien Miller3db5f532002-02-13 14:10:32 +1100461 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100462
463 type = buffer_get_char(&msg);
464 id = buffer_get_int(&msg);
465
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000466 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100467
468 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000469 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100470
471 if (type == SSH2_FXP_STATUS) {
472 int status = buffer_get_int(&msg);
473
474 debug3("Received SSH2_FXP_STATUS %d", status);
475
476 if (status == SSH2_FX_EOF) {
477 break;
478 } else {
479 error("Couldn't read directory: %s",
480 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100481 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100482 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000483 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100484 }
485 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000486 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100487 SSH2_FXP_NAME, type);
488
489 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100490 if (count == 0)
491 break;
492 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100493 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100494 char *filename, *longname;
495 Attrib *a;
496
497 filename = buffer_get_string(&msg, NULL);
498 longname = buffer_get_string(&msg, NULL);
499 a = decode_attrib(&msg);
500
Damien Miller4870afd2001-03-14 10:27:09 +1100501 if (printflag)
502 printf("%s\n", longname);
503
Darren Tucker1b0dd172009-10-07 08:37:48 +1100504 /*
505 * Directory entries should never contain '/'
506 * These can be used to attack recursive ops
507 * (e.g. send '../../../../etc/passwd')
508 */
509 if (strchr(filename, '/') != NULL) {
510 error("Server sent suspect path \"%s\" "
511 "during readdir of \"%s\"", filename, path);
512 goto next;
513 }
514
Damien Miller4870afd2001-03-14 10:27:09 +1100515 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100516 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100517 (*dir)[ents] = xmalloc(sizeof(***dir));
518 (*dir)[ents]->filename = xstrdup(filename);
519 (*dir)[ents]->longname = xstrdup(longname);
520 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
521 (*dir)[++ents] = NULL;
522 }
Darren Tucker1b0dd172009-10-07 08:37:48 +1100523 next:
Damien Miller33804262001-02-04 23:20:18 +1100524 xfree(filename);
525 xfree(longname);
526 }
527 }
528
529 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100530 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100531 xfree(handle);
532
Darren Tuckercdf547a2004-05-24 10:12:19 +1000533 /* Don't return partial matches on interrupt */
534 if (interrupted && dir != NULL && *dir != NULL) {
535 free_sftp_dirents(*dir);
536 *dir = xmalloc(sizeof(**dir));
537 **dir = NULL;
538 }
539
Damien Miller33804262001-02-04 23:20:18 +1100540 return(0);
541}
542
543int
Damien Miller3db5f532002-02-13 14:10:32 +1100544do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100545{
Damien Miller3db5f532002-02-13 14:10:32 +1100546 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100547}
548
549void free_sftp_dirents(SFTP_DIRENT **s)
550{
551 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100552
553 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100554 xfree(s[i]->filename);
555 xfree(s[i]->longname);
556 xfree(s[i]);
557 }
558 xfree(s);
559}
560
561int
Damien Miller3db5f532002-02-13 14:10:32 +1100562do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100563{
564 u_int status, id;
565
566 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
567
Damien Miller3db5f532002-02-13 14:10:32 +1100568 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000569 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100570 strlen(path));
571 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100572 if (status != SSH2_FX_OK)
573 error("Couldn't delete file: %s", fx2txt(status));
574 return(status);
575}
576
577int
Darren Tucker1b0dd172009-10-07 08:37:48 +1100578do_mkdir(struct sftp_conn *conn, char *path, Attrib *a, int printflag)
Damien Miller33804262001-02-04 23:20:18 +1100579{
580 u_int status, id;
581
Damien Miller3db5f532002-02-13 14:10:32 +1100582 id = conn->msg_id++;
583 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100584 strlen(path), a);
585
Damien Miller3db5f532002-02-13 14:10:32 +1100586 status = get_status(conn->fd_in, id);
Darren Tucker1b0dd172009-10-07 08:37:48 +1100587 if (status != SSH2_FX_OK && printflag)
Damien Miller33804262001-02-04 23:20:18 +1100588 error("Couldn't create directory: %s", fx2txt(status));
589
590 return(status);
591}
592
593int
Damien Miller3db5f532002-02-13 14:10:32 +1100594do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100595{
596 u_int status, id;
597
Damien Miller3db5f532002-02-13 14:10:32 +1100598 id = conn->msg_id++;
599 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
600 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100601
Damien Miller3db5f532002-02-13 14:10:32 +1100602 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100603 if (status != SSH2_FX_OK)
604 error("Couldn't remove directory: %s", fx2txt(status));
605
606 return(status);
607}
608
609Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100610do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100611{
612 u_int id;
613
Damien Miller3db5f532002-02-13 14:10:32 +1100614 id = conn->msg_id++;
615
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000616 send_string_request(conn->fd_out, id,
617 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100618 path, strlen(path));
619
620 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100621}
622
623Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100624do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100625{
626 u_int id;
627
Damien Miller3db5f532002-02-13 14:10:32 +1100628 if (conn->version == 0) {
629 if (quiet)
630 debug("Server version does not support lstat operation");
631 else
Damien Miller996acd22003-04-09 20:59:48 +1000632 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000633 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100634 }
635
636 id = conn->msg_id++;
637 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
638 strlen(path));
639
640 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100641}
642
Damien Millercfe23d32008-02-10 22:20:44 +1100643#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100644Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100645do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100646{
647 u_int id;
648
Damien Miller3db5f532002-02-13 14:10:32 +1100649 id = conn->msg_id++;
650 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
651 handle_len);
652
653 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100654}
Damien Millercfe23d32008-02-10 22:20:44 +1100655#endif
Damien Miller33804262001-02-04 23:20:18 +1100656
657int
Damien Miller3db5f532002-02-13 14:10:32 +1100658do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100659{
660 u_int status, id;
661
Damien Miller3db5f532002-02-13 14:10:32 +1100662 id = conn->msg_id++;
663 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100664 strlen(path), a);
665
Damien Miller3db5f532002-02-13 14:10:32 +1100666 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100667 if (status != SSH2_FX_OK)
668 error("Couldn't setstat on \"%s\": %s", path,
669 fx2txt(status));
670
671 return(status);
672}
673
674int
Damien Miller3db5f532002-02-13 14:10:32 +1100675do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100676 Attrib *a)
677{
678 u_int status, id;
679
Damien Miller3db5f532002-02-13 14:10:32 +1100680 id = conn->msg_id++;
681 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100682 handle_len, a);
683
Damien Miller3db5f532002-02-13 14:10:32 +1100684 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100685 if (status != SSH2_FX_OK)
686 error("Couldn't fsetstat: %s", fx2txt(status));
687
688 return(status);
689}
690
691char *
Damien Miller3db5f532002-02-13 14:10:32 +1100692do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100693{
694 Buffer msg;
695 u_int type, expected_id, count, id;
696 char *filename, *longname;
697 Attrib *a;
698
Damien Miller3db5f532002-02-13 14:10:32 +1100699 expected_id = id = conn->msg_id++;
700 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
701 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100702
703 buffer_init(&msg);
704
Damien Miller3db5f532002-02-13 14:10:32 +1100705 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100706 type = buffer_get_char(&msg);
707 id = buffer_get_int(&msg);
708
709 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000710 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100711
712 if (type == SSH2_FXP_STATUS) {
713 u_int status = buffer_get_int(&msg);
714
715 error("Couldn't canonicalise: %s", fx2txt(status));
716 return(NULL);
717 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000718 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100719 SSH2_FXP_NAME, type);
720
721 count = buffer_get_int(&msg);
722 if (count != 1)
723 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
724
725 filename = buffer_get_string(&msg, NULL);
726 longname = buffer_get_string(&msg, NULL);
727 a = decode_attrib(&msg);
728
729 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
730
731 xfree(longname);
732
733 buffer_free(&msg);
734
735 return(filename);
736}
737
738int
Damien Miller3db5f532002-02-13 14:10:32 +1100739do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100740{
741 Buffer msg;
742 u_int status, id;
743
744 buffer_init(&msg);
745
746 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100747 id = conn->msg_id++;
Damien Miller7a3e1d02008-03-27 10:59:57 +1100748 if ((conn->exts & SFTP_EXT_POSIX_RENAME)) {
749 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
750 buffer_put_int(&msg, id);
751 buffer_put_cstring(&msg, "posix-rename@openssh.com");
752 } else {
753 buffer_put_char(&msg, SSH2_FXP_RENAME);
754 buffer_put_int(&msg, id);
755 }
Damien Miller33804262001-02-04 23:20:18 +1100756 buffer_put_cstring(&msg, oldpath);
757 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100758 send_msg(conn->fd_out, &msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100759 debug3("Sent message %s \"%s\" -> \"%s\"",
760 (conn->exts & SFTP_EXT_POSIX_RENAME) ? "posix-rename@openssh.com" :
761 "SSH2_FXP_RENAME", oldpath, newpath);
Damien Miller33804262001-02-04 23:20:18 +1100762 buffer_free(&msg);
763
Damien Miller3db5f532002-02-13 14:10:32 +1100764 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100765 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100766 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
767 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100768
769 return(status);
770}
771
772int
Damien Miller3db5f532002-02-13 14:10:32 +1100773do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100774{
775 Buffer msg;
776 u_int status, id;
777
Damien Miller3db5f532002-02-13 14:10:32 +1100778 if (conn->version < 3) {
779 error("This server does not support the symlink operation");
780 return(SSH2_FX_OP_UNSUPPORTED);
781 }
782
Damien Miller058316f2001-03-08 10:08:49 +1100783 buffer_init(&msg);
784
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000785 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100786 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100787 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
788 buffer_put_int(&msg, id);
789 buffer_put_cstring(&msg, oldpath);
790 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100791 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100792 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
793 newpath);
794 buffer_free(&msg);
795
Damien Miller3db5f532002-02-13 14:10:32 +1100796 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100797 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000798 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100799 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100800
801 return(status);
802}
803
Damien Millercfe23d32008-02-10 22:20:44 +1100804#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +1100805char *
Damien Miller3db5f532002-02-13 14:10:32 +1100806do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100807{
808 Buffer msg;
809 u_int type, expected_id, count, id;
810 char *filename, *longname;
811 Attrib *a;
812
Damien Miller3db5f532002-02-13 14:10:32 +1100813 expected_id = id = conn->msg_id++;
814 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
815 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100816
817 buffer_init(&msg);
818
Damien Miller3db5f532002-02-13 14:10:32 +1100819 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100820 type = buffer_get_char(&msg);
821 id = buffer_get_int(&msg);
822
823 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000824 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100825
826 if (type == SSH2_FXP_STATUS) {
827 u_int status = buffer_get_int(&msg);
828
829 error("Couldn't readlink: %s", fx2txt(status));
830 return(NULL);
831 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000832 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100833 SSH2_FXP_NAME, type);
834
835 count = buffer_get_int(&msg);
836 if (count != 1)
837 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
838
839 filename = buffer_get_string(&msg, NULL);
840 longname = buffer_get_string(&msg, NULL);
841 a = decode_attrib(&msg);
842
843 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
844
845 xfree(longname);
846
847 buffer_free(&msg);
848
849 return(filename);
850}
Damien Millercfe23d32008-02-10 22:20:44 +1100851#endif
Damien Miller058316f2001-03-08 10:08:49 +1100852
Damien Millerd671e5a2008-05-19 14:53:33 +1000853int
Darren Tucker7b598892008-06-09 22:49:36 +1000854do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
Damien Millerd671e5a2008-05-19 14:53:33 +1000855 int quiet)
856{
857 Buffer msg;
858 u_int id;
859
860 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
861 error("Server does not support statvfs@openssh.com extension");
862 return -1;
863 }
864
865 id = conn->msg_id++;
866
867 buffer_init(&msg);
868 buffer_clear(&msg);
869 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
870 buffer_put_int(&msg, id);
871 buffer_put_cstring(&msg, "statvfs@openssh.com");
872 buffer_put_cstring(&msg, path);
873 send_msg(conn->fd_out, &msg);
874 buffer_free(&msg);
875
876 return get_decode_statvfs(conn->fd_in, st, id, quiet);
877}
878
879#ifdef notyet
880int
881do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
Darren Tucker7b598892008-06-09 22:49:36 +1000882 struct sftp_statvfs *st, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000883{
884 Buffer msg;
885 u_int id;
886
887 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
888 error("Server does not support fstatvfs@openssh.com extension");
889 return -1;
890 }
891
892 id = conn->msg_id++;
893
894 buffer_init(&msg);
895 buffer_clear(&msg);
896 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
897 buffer_put_int(&msg, id);
898 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
899 buffer_put_string(&msg, handle, handle_len);
900 send_msg(conn->fd_out, &msg);
901 buffer_free(&msg);
902
903 return get_decode_statvfs(conn->fd_in, st, id, quiet);
904}
905#endif
906
Damien Miller16a13332002-02-13 14:03:56 +1100907static void
908send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
909 char *handle, u_int handle_len)
910{
911 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000912
Damien Miller16a13332002-02-13 14:03:56 +1100913 buffer_init(&msg);
914 buffer_clear(&msg);
915 buffer_put_char(&msg, SSH2_FXP_READ);
916 buffer_put_int(&msg, id);
917 buffer_put_string(&msg, handle, handle_len);
918 buffer_put_int64(&msg, offset);
919 buffer_put_int(&msg, len);
920 send_msg(fd_out, &msg);
921 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000922}
Damien Miller16a13332002-02-13 14:03:56 +1100923
Damien Miller058316f2001-03-08 10:08:49 +1100924int
Damien Miller3db5f532002-02-13 14:10:32 +1100925do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
Darren Tucker1b0dd172009-10-07 08:37:48 +1100926 Attrib *a, int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100927{
Darren Tucker1b0dd172009-10-07 08:37:48 +1100928 Attrib junk;
Damien Miller16a13332002-02-13 14:03:56 +1100929 Buffer msg;
930 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000931 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100932 int read_error, write_errno;
933 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000934 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100935 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100936 struct request {
937 u_int id;
938 u_int len;
939 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000940 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100941 };
942 TAILQ_HEAD(reqhead, request) requests;
943 struct request *req;
944
945 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100946
Darren Tucker1b0dd172009-10-07 08:37:48 +1100947 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
948 return -1;
Damien Miller33804262001-02-04 23:20:18 +1100949
Damien Miller9e720282008-06-29 22:46:35 +1000950 /* Do not preserve set[ug]id here, as we do not preserve ownership */
Damien Miller33804262001-02-04 23:20:18 +1100951 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100952 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100953 else
954 mode = 0666;
955
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000956 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100957 (!S_ISREG(a->perm))) {
958 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000959 return(-1);
960 }
961
Damien Miller16a13332002-02-13 14:03:56 +1100962 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
963 size = a->size;
964 else
965 size = 0;
966
Damien Miller3db5f532002-02-13 14:10:32 +1100967 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100968 buffer_init(&msg);
969
970 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100971 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100972 buffer_put_char(&msg, SSH2_FXP_OPEN);
973 buffer_put_int(&msg, id);
974 buffer_put_cstring(&msg, remote_path);
975 buffer_put_int(&msg, SSH2_FXF_READ);
976 attrib_clear(&junk); /* Send empty attributes */
977 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100978 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000979 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100980
Darren Tuckerc22f0902009-10-07 08:24:19 +1100981 handle = get_handle(conn->fd_in, id, &handle_len,
982 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100983 if (handle == NULL) {
984 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100985 return(-1);
986 }
987
Damien Millera8e06ce2003-11-21 23:48:55 +1100988 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +1100989 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100990 if (local_fd == -1) {
991 error("Couldn't open local file \"%s\" for writing: %s",
992 local_path, strerror(errno));
Damien Miller6b0c8182008-02-10 22:23:41 +1100993 do_close(conn, handle, handle_len);
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000994 buffer_free(&msg);
995 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100996 return(-1);
997 }
998
Damien Miller33804262001-02-04 23:20:18 +1100999 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +11001000 write_error = read_error = write_errno = num_req = offset = 0;
1001 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +11001002 progress_counter = 0;
1003
Damien Miller9ba30692004-03-08 23:12:02 +11001004 if (showprogress && size != 0)
1005 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001006
Damien Miller16a13332002-02-13 14:03:56 +11001007 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +11001008 char *data;
Damien Miller16a13332002-02-13 14:03:56 +11001009 u_int len;
Damien Miller33804262001-02-04 23:20:18 +11001010
Darren Tuckercdf547a2004-05-24 10:12:19 +10001011 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001012 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001013 * allow outstanding requests to drain gracefully
1014 */
1015 if (interrupted) {
1016 if (num_req == 0) /* If we haven't started yet... */
1017 break;
1018 max_req = 0;
1019 }
1020
Damien Miller16a13332002-02-13 14:03:56 +11001021 /* Send some more requests */
1022 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001023 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +00001024 (unsigned long long)offset,
1025 (unsigned long long)offset + buflen - 1,
1026 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +11001027 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +11001028 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001029 req->len = buflen;
1030 req->offset = offset;
1031 offset += buflen;
1032 num_req++;
1033 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001034 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001035 req->len, handle, handle_len);
1036 }
Damien Miller33804262001-02-04 23:20:18 +11001037
1038 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001039 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +11001040 type = buffer_get_char(&msg);
1041 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001042 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001043
Damien Miller16a13332002-02-13 14:03:56 +11001044 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001045 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001046 req != NULL && req->id != id;
1047 req = TAILQ_NEXT(req, tq))
1048 ;
1049 if (req == NULL)
1050 fatal("Unexpected reply %u", id);
1051
1052 switch (type) {
1053 case SSH2_FXP_STATUS:
1054 status = buffer_get_int(&msg);
1055 if (status != SSH2_FX_EOF)
1056 read_error = 1;
1057 max_req = 0;
1058 TAILQ_REMOVE(&requests, req, tq);
1059 xfree(req);
1060 num_req--;
1061 break;
1062 case SSH2_FXP_DATA:
1063 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +00001064 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001065 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001066 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001067 if (len > req->len)
1068 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +00001069 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001070 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001071 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001072 !write_error) {
1073 write_errno = errno;
1074 write_error = 1;
1075 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001076 }
Damien Miller62d57f62003-01-10 21:43:24 +11001077 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +11001078 xfree(data);
1079
1080 if (len == req->len) {
1081 TAILQ_REMOVE(&requests, req, tq);
1082 xfree(req);
1083 num_req--;
1084 } else {
1085 /* Resend the request for the missing data */
1086 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001087 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001088 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001089 (unsigned long long)req->offset +
1090 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001091 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001092 req->len -= len;
1093 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001094 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001095 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001096 /* Reduce the request size */
1097 if (len < buflen)
1098 buflen = MAX(MIN_READ_SIZE, len);
1099 }
1100 if (max_req > 0) { /* max_req = 0 iff EOF received */
1101 if (size > 0 && offset > size) {
1102 /* Only one request at a time
1103 * after the expected EOF */
1104 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001105 (unsigned long long)offset,
1106 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001107 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001108 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001109 ++max_req;
1110 }
1111 }
1112 break;
1113 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001114 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001115 SSH2_FXP_DATA, type);
1116 }
Damien Miller33804262001-02-04 23:20:18 +11001117 }
Damien Miller33804262001-02-04 23:20:18 +11001118
Damien Miller62d57f62003-01-10 21:43:24 +11001119 if (showprogress && size)
1120 stop_progress_meter();
1121
Damien Miller16a13332002-02-13 14:03:56 +11001122 /* Sanity check */
1123 if (TAILQ_FIRST(&requests) != NULL)
1124 fatal("Transfer complete, but requests still in queue");
1125
1126 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001127 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001128 remote_path, fx2txt(status));
1129 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001130 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001131 error("Couldn't write to \"%s\": %s", local_path,
1132 strerror(write_errno));
1133 status = -1;
1134 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001135 } else {
Damien Miller3db5f532002-02-13 14:10:32 +11001136 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001137
1138 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001139#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +11001140 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001141#else
Damien Miller16a13332002-02-13 14:03:56 +11001142 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001143#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001144 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001145 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001146 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1147 struct timeval tv[2];
1148 tv[0].tv_sec = a->atime;
1149 tv[1].tv_sec = a->mtime;
1150 tv[0].tv_usec = tv[1].tv_usec = 0;
1151 if (utimes(local_path, tv) == -1)
1152 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001153 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001154 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001155 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001156 close(local_fd);
1157 buffer_free(&msg);
1158 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001159
1160 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001161}
1162
Darren Tucker1b0dd172009-10-07 08:37:48 +11001163static int
1164download_dir_internal(struct sftp_conn *conn, char *src, char *dst,
1165 Attrib *dirattrib, int pflag, int printflag, int depth)
1166{
1167 int i, ret = 0;
1168 SFTP_DIRENT **dir_entries;
1169 char *filename, *new_src, *new_dst;
1170 mode_t mode = 0777;
1171
1172 if (depth >= MAX_DIR_DEPTH) {
1173 error("Maximum directory depth exceeded: %d levels", depth);
1174 return -1;
1175 }
1176
1177 if (dirattrib == NULL &&
1178 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1179 error("Unable to stat remote directory \"%s\"", src);
1180 return -1;
1181 }
1182 if (!S_ISDIR(dirattrib->perm)) {
1183 error("\"%s\" is not a directory", src);
1184 return -1;
1185 }
1186 if (printflag)
1187 printf("Retrieving %s\n", src);
1188
1189 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1190 mode = dirattrib->perm & 01777;
1191 else {
1192 debug("Server did not send permissions for "
1193 "directory \"%s\"", dst);
1194 }
1195
1196 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1197 error("mkdir %s: %s", dst, strerror(errno));
1198 return -1;
1199 }
1200
1201 if (do_readdir(conn, src, &dir_entries) == -1) {
1202 error("%s: Failed to get directory contents", src);
1203 return -1;
1204 }
1205
1206 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1207 filename = dir_entries[i]->filename;
1208
1209 new_dst = path_append(dst, filename);
1210 new_src = path_append(src, filename);
1211
1212 if (S_ISDIR(dir_entries[i]->a.perm)) {
1213 if (strcmp(filename, ".") == 0 ||
1214 strcmp(filename, "..") == 0)
1215 continue;
1216 if (download_dir_internal(conn, new_src, new_dst,
1217 &(dir_entries[i]->a), pflag, printflag,
1218 depth + 1) == -1)
1219 ret = -1;
1220 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1221 if (do_download(conn, new_src, new_dst,
1222 &(dir_entries[i]->a), pflag) == -1) {
1223 error("Download of file %s to %s failed",
1224 new_src, new_dst);
1225 ret = -1;
1226 }
1227 } else
1228 logit("%s: not a regular file\n", new_src);
1229
1230 xfree(new_dst);
1231 xfree(new_src);
1232 }
1233
1234 if (pflag) {
1235 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1236 struct timeval tv[2];
1237 tv[0].tv_sec = dirattrib->atime;
1238 tv[1].tv_sec = dirattrib->mtime;
1239 tv[0].tv_usec = tv[1].tv_usec = 0;
1240 if (utimes(dst, tv) == -1)
1241 error("Can't set times on \"%s\": %s",
1242 dst, strerror(errno));
1243 } else
1244 debug("Server did not send times for directory "
1245 "\"%s\"", dst);
1246 }
1247
1248 free_sftp_dirents(dir_entries);
1249
1250 return ret;
1251}
1252
1253int
1254download_dir(struct sftp_conn *conn, char *src, char *dst,
1255 Attrib *dirattrib, int pflag, int printflag)
1256{
1257 char *src_canon;
1258 int ret;
1259
1260 if ((src_canon = do_realpath(conn, src)) == NULL) {
1261 error("Unable to canonicalise path \"%s\"", src);
1262 return -1;
1263 }
1264
1265 ret = download_dir_internal(conn, src_canon, dst,
1266 dirattrib, pflag, printflag, 0);
1267 xfree(src_canon);
1268 return ret;
1269}
1270
Damien Miller33804262001-02-04 23:20:18 +11001271int
Damien Miller3db5f532002-02-13 14:10:32 +11001272do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
1273 int pflag)
Damien Miller33804262001-02-04 23:20:18 +11001274{
Damien Milleracdf25b2008-02-10 22:27:24 +11001275 int local_fd;
1276 int status = SSH2_FX_OK;
Damien Miller5873dfd2002-02-13 14:04:37 +11001277 u_int handle_len, id, type;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001278 off_t offset;
Damien Miller8829d362002-02-08 22:04:05 +11001279 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +11001280 Buffer msg;
1281 struct stat sb;
1282 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +11001283 u_int32_t startid;
1284 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001285 struct outstanding_ack {
1286 u_int id;
1287 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001288 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001289 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001290 };
1291 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001292 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001293
1294 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001295
1296 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1297 error("Couldn't open local file \"%s\" for reading: %s",
1298 local_path, strerror(errno));
1299 return(-1);
1300 }
1301 if (fstat(local_fd, &sb) == -1) {
1302 error("Couldn't fstat local file \"%s\": %s",
1303 local_path, strerror(errno));
1304 close(local_fd);
1305 return(-1);
1306 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001307 if (!S_ISREG(sb.st_mode)) {
1308 error("%s is not a regular file", local_path);
1309 close(local_fd);
1310 return(-1);
1311 }
Damien Miller33804262001-02-04 23:20:18 +11001312 stat_to_attrib(&sb, &a);
1313
1314 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1315 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1316 a.perm &= 0777;
1317 if (!pflag)
1318 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1319
1320 buffer_init(&msg);
1321
1322 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001323 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001324 buffer_put_char(&msg, SSH2_FXP_OPEN);
1325 buffer_put_int(&msg, id);
1326 buffer_put_cstring(&msg, remote_path);
1327 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1328 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001329 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001330 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001331
1332 buffer_clear(&msg);
1333
Darren Tuckerc22f0902009-10-07 08:24:19 +11001334 handle = get_handle(conn->fd_in, id, &handle_len,
1335 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001336 if (handle == NULL) {
1337 close(local_fd);
1338 buffer_free(&msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001339 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001340 }
1341
Damien Miller16a13332002-02-13 14:03:56 +11001342 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001343 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001344
Damien Miller33804262001-02-04 23:20:18 +11001345 /* Read from local and write to remote */
1346 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001347 if (showprogress)
1348 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001349
Damien Miller9f0f5c62001-12-21 14:45:46 +11001350 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001351 int len;
Damien Miller33804262001-02-04 23:20:18 +11001352
1353 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001354 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001355 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001356 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001357 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001358 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001359 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001360 len = 0;
1361 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001362 len = read(local_fd, data, conn->transfer_buflen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001363 while ((len == -1) &&
1364 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
Damien Miller33804262001-02-04 23:20:18 +11001365
1366 if (len == -1)
1367 fatal("Couldn't read from \"%s\": %s", local_path,
1368 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001369
1370 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001371 ack = xmalloc(sizeof(*ack));
1372 ack->id = ++id;
1373 ack->offset = offset;
1374 ack->len = len;
1375 TAILQ_INSERT_TAIL(&acks, ack, tq);
1376
Damien Miller16a13332002-02-13 14:03:56 +11001377 buffer_clear(&msg);
1378 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001379 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001380 buffer_put_string(&msg, handle, handle_len);
1381 buffer_put_int64(&msg, offset);
1382 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001383 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001384 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001385 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001386 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001387 break;
1388
Damien Miller5873dfd2002-02-13 14:04:37 +11001389 if (ack == NULL)
1390 fatal("Unexpected ACK %u", id);
1391
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001392 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001393 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001394 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001395
Damien Miller5873dfd2002-02-13 14:04:37 +11001396 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001397 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001398 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001399 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001400
1401 if (type != SSH2_FXP_STATUS)
1402 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1403 "got %d", SSH2_FXP_STATUS, type);
1404
1405 status = buffer_get_int(&msg);
1406 debug3("SSH2_FXP_STATUS %d", status);
1407
1408 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001409 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001410 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001411 ack = TAILQ_NEXT(ack, tq))
1412 ;
1413 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001414 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001415 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001416 debug3("In write loop, ack for %u %u bytes at %lld",
1417 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001418 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001419 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001420 }
Damien Miller33804262001-02-04 23:20:18 +11001421 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001422 if (offset < 0)
1423 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001424 }
Damien Milleracdf25b2008-02-10 22:27:24 +11001425 buffer_free(&msg);
1426
Damien Miller62d57f62003-01-10 21:43:24 +11001427 if (showprogress)
1428 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001429 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001430
Damien Milleracdf25b2008-02-10 22:27:24 +11001431 if (status != SSH2_FX_OK) {
1432 error("Couldn't write to remote file \"%s\": %s",
1433 remote_path, fx2txt(status));
1434 status = -1;
1435 }
1436
Damien Miller33804262001-02-04 23:20:18 +11001437 if (close(local_fd) == -1) {
1438 error("Couldn't close local file \"%s\": %s", local_path,
1439 strerror(errno));
Damien Millerd7686fd2001-02-10 00:40:03 +11001440 status = -1;
Damien Miller33804262001-02-04 23:20:18 +11001441 }
1442
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001443 /* Override umask and utimes if asked */
1444 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001445 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001446
Damien Milleracdf25b2008-02-10 22:27:24 +11001447 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
1448 status = -1;
Damien Millerd7686fd2001-02-10 00:40:03 +11001449 xfree(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001450
1451 return status;
Damien Miller33804262001-02-04 23:20:18 +11001452}
Darren Tucker1b0dd172009-10-07 08:37:48 +11001453
1454static int
1455upload_dir_internal(struct sftp_conn *conn, char *src, char *dst,
1456 int pflag, int printflag, int depth)
1457{
1458 int ret = 0, status;
1459 DIR *dirp;
1460 struct dirent *dp;
1461 char *filename, *new_src, *new_dst;
1462 struct stat sb;
1463 Attrib a;
1464
1465 if (depth >= MAX_DIR_DEPTH) {
1466 error("Maximum directory depth exceeded: %d levels", depth);
1467 return -1;
1468 }
1469
1470 if (stat(src, &sb) == -1) {
1471 error("Couldn't stat directory \"%s\": %s",
1472 src, strerror(errno));
1473 return -1;
1474 }
1475 if (!S_ISDIR(sb.st_mode)) {
1476 error("\"%s\" is not a directory", src);
1477 return -1;
1478 }
1479 if (printflag)
1480 printf("Entering %s\n", src);
1481
1482 attrib_clear(&a);
1483 stat_to_attrib(&sb, &a);
1484 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1485 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1486 a.perm &= 01777;
1487 if (!pflag)
1488 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1489
1490 status = do_mkdir(conn, dst, &a, 0);
1491 /*
1492 * we lack a portable status for errno EEXIST,
1493 * so if we get a SSH2_FX_FAILURE back we must check
1494 * if it was created successfully.
1495 */
1496 if (status != SSH2_FX_OK) {
1497 if (status != SSH2_FX_FAILURE)
1498 return -1;
1499 if (do_stat(conn, dst, 0) == NULL)
1500 return -1;
1501 }
1502
1503 if ((dirp = opendir(src)) == NULL) {
1504 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1505 return -1;
1506 }
1507
1508 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1509 if (dp->d_ino == 0)
1510 continue;
1511 filename = dp->d_name;
1512 new_dst = path_append(dst, filename);
1513 new_src = path_append(src, filename);
1514
1515 if (S_ISDIR(DTTOIF(dp->d_type))) {
1516 if (strcmp(filename, ".") == 0 ||
1517 strcmp(filename, "..") == 0)
1518 continue;
1519
1520 if (upload_dir_internal(conn, new_src, new_dst,
1521 pflag, depth + 1, printflag) == -1)
1522 ret = -1;
1523 } else if (S_ISREG(DTTOIF(dp->d_type)) ) {
1524 if (do_upload(conn, new_src, new_dst, pflag) == -1) {
1525 error("Uploading of file %s to %s failed!",
1526 new_src, new_dst);
1527 ret = -1;
1528 }
1529 } else
1530 logit("%s: not a regular file\n", filename);
1531 xfree(new_dst);
1532 xfree(new_src);
1533 }
1534
1535 do_setstat(conn, dst, &a);
1536
1537 (void) closedir(dirp);
1538 return ret;
1539}
1540
1541int
1542upload_dir(struct sftp_conn *conn, char *src, char *dst, int printflag,
1543 int pflag)
1544{
1545 char *dst_canon;
1546 int ret;
1547
1548 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1549 error("Unable to canonicalise path \"%s\"", dst);
1550 return -1;
1551 }
1552
1553 ret = upload_dir_internal(conn, src, dst_canon, pflag, printflag, 0);
1554 xfree(dst_canon);
1555 return ret;
1556}
1557
1558char *
1559path_append(char *p1, char *p2)
1560{
1561 char *ret;
1562 size_t len = strlen(p1) + strlen(p2) + 2;
1563
1564 ret = xmalloc(len);
1565 strlcpy(ret, p1, len);
1566 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1567 strlcat(ret, "/", len);
1568 strlcat(ret, p2, len);
1569
1570 return(ret);
1571}
1572