blob: 8fe0c0fe5646a8adefd1a1da56f6026e5ba1b30c [file] [log] [blame]
Darren Tucker39972492006-07-12 22:22:46 +10001/* $OpenBSD: sftp-client.c,v 1.67 2006/07/11 20:07:25 stevesk 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>
26#ifdef HAVE_SYS_STAT_H
27# include <sys/stat.h>
28#endif
Damien Miller57cf6382006-07-10 21:13:46 +100029
Darren Tucker39972492006-07-12 22:22:46 +100030#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100031#include <fcntl.h>
32#include <signal.h>
Damien Miller16a13332002-02-13 14:03:56 +110033
Damien Miller9b481512002-09-12 10:43:29 +100034#include "openbsd-compat/sys-queue.h"
Damien Miller33804262001-02-04 23:20:18 +110035
Damien Miller33804262001-02-04 23:20:18 +110036#include "buffer.h"
37#include "bufaux.h"
Damien Miller33804262001-02-04 23:20:18 +110038#include "xmalloc.h"
39#include "log.h"
40#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110041#include "progressmeter.h"
Damien Miller3f941882006-03-31 23:13:02 +110042#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110043
44#include "sftp.h"
45#include "sftp-common.h"
46#include "sftp-client.h"
47
Darren Tuckercdf547a2004-05-24 10:12:19 +100048extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110049extern int showprogress;
50
Damien Miller0c8d8f62006-03-15 11:34:25 +110051/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110052#define MIN_READ_SIZE 512
53
Damien Miller3db5f532002-02-13 14:10:32 +110054struct sftp_conn {
55 int fd_in;
56 int fd_out;
57 u_int transfer_buflen;
58 u_int num_requests;
59 u_int version;
60 u_int msg_id;
61};
Ben Lindstrom288cc392001-02-09 02:58:04 +000062
Ben Lindstrombba81212001-06-25 05:01:22 +000063static void
Damien Miller33804262001-02-04 23:20:18 +110064send_msg(int fd, Buffer *m)
65{
Damien Millera7f3aaa2003-01-10 21:43:58 +110066 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +100067 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +110068
Damien Miller54446182006-01-02 23:40:50 +110069 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +110070 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +110071
Damien Millera7f3aaa2003-01-10 21:43:58 +110072 /* Send length first */
Damien Miller3f941882006-03-31 23:13:02 +110073 put_u32(mlen, buffer_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +100074 iov[0].iov_base = mlen;
75 iov[0].iov_len = sizeof(mlen);
76 iov[1].iov_base = buffer_ptr(m);
77 iov[1].iov_len = buffer_len(m);
78
79 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +110080 fatal("Couldn't send packet: %s", strerror(errno));
81
82 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +110083}
84
Ben Lindstrombba81212001-06-25 05:01:22 +000085static void
Damien Miller33804262001-02-04 23:20:18 +110086get_msg(int fd, Buffer *m)
87{
Damien Millera7f3aaa2003-01-10 21:43:58 +110088 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +110089
Damien Millera7f3aaa2003-01-10 21:43:58 +110090 buffer_append_space(m, 4);
Damien Millerb253cc42005-05-26 12:23:44 +100091 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
92 if (errno == EPIPE)
93 fatal("Connection closed");
94 else
95 fatal("Couldn't read packet: %s", strerror(errno));
96 }
Damien Miller33804262001-02-04 23:20:18 +110097
Damien Millera7f3aaa2003-01-10 21:43:58 +110098 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +110099 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000100 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100101
Damien Millera7f3aaa2003-01-10 21:43:58 +1100102 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000103 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
104 if (errno == EPIPE)
105 fatal("Connection closed");
106 else
107 fatal("Read packet: %s", strerror(errno));
108 }
Damien Miller33804262001-02-04 23:20:18 +1100109}
110
Ben Lindstrombba81212001-06-25 05:01:22 +0000111static void
Damien Miller33804262001-02-04 23:20:18 +1100112send_string_request(int fd, u_int id, u_int code, char *s,
113 u_int len)
114{
115 Buffer msg;
116
117 buffer_init(&msg);
118 buffer_put_char(&msg, code);
119 buffer_put_int(&msg, id);
120 buffer_put_string(&msg, s, len);
121 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000122 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100123 buffer_free(&msg);
124}
125
Ben Lindstrombba81212001-06-25 05:01:22 +0000126static void
Damien Miller33804262001-02-04 23:20:18 +1100127send_string_attrs_request(int fd, u_int id, u_int code, char *s,
128 u_int len, Attrib *a)
129{
130 Buffer msg;
131
132 buffer_init(&msg);
133 buffer_put_char(&msg, code);
134 buffer_put_int(&msg, id);
135 buffer_put_string(&msg, s, len);
136 encode_attrib(&msg, a);
137 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000138 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100139 buffer_free(&msg);
140}
141
Ben Lindstrombba81212001-06-25 05:01:22 +0000142static u_int
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000143get_status(int fd, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100144{
145 Buffer msg;
146 u_int type, id, status;
147
148 buffer_init(&msg);
149 get_msg(fd, &msg);
150 type = buffer_get_char(&msg);
151 id = buffer_get_int(&msg);
152
153 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000154 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100155 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000156 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100157 SSH2_FXP_STATUS, type);
158
159 status = buffer_get_int(&msg);
160 buffer_free(&msg);
161
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000162 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100163
164 return(status);
165}
166
Ben Lindstrombba81212001-06-25 05:01:22 +0000167static char *
Damien Miller33804262001-02-04 23:20:18 +1100168get_handle(int fd, u_int expected_id, u_int *len)
169{
170 Buffer msg;
171 u_int type, id;
172 char *handle;
173
174 buffer_init(&msg);
175 get_msg(fd, &msg);
176 type = buffer_get_char(&msg);
177 id = buffer_get_int(&msg);
178
179 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000180 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100181 if (type == SSH2_FXP_STATUS) {
182 int status = buffer_get_int(&msg);
183
184 error("Couldn't get handle: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100185 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100186 return(NULL);
187 } else if (type != SSH2_FXP_HANDLE)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000188 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100189 SSH2_FXP_HANDLE, type);
190
191 handle = buffer_get_string(&msg, len);
192 buffer_free(&msg);
193
194 return(handle);
195}
196
Ben Lindstrombba81212001-06-25 05:01:22 +0000197static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000198get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100199{
200 Buffer msg;
201 u_int type, id;
202 Attrib *a;
203
204 buffer_init(&msg);
205 get_msg(fd, &msg);
206
207 type = buffer_get_char(&msg);
208 id = buffer_get_int(&msg);
209
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000210 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100211 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000212 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100213 if (type == SSH2_FXP_STATUS) {
214 int status = buffer_get_int(&msg);
215
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000216 if (quiet)
217 debug("Couldn't stat remote file: %s", fx2txt(status));
218 else
219 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100220 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100221 return(NULL);
222 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000223 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100224 SSH2_FXP_ATTRS, type);
225 }
226 a = decode_attrib(&msg);
227 buffer_free(&msg);
228
229 return(a);
230}
231
Damien Miller3db5f532002-02-13 14:10:32 +1100232struct sftp_conn *
233do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100234{
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000235 u_int type;
236 int version;
Damien Miller33804262001-02-04 23:20:18 +1100237 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100238 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100239
240 buffer_init(&msg);
241 buffer_put_char(&msg, SSH2_FXP_INIT);
242 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
243 send_msg(fd_out, &msg);
244
245 buffer_clear(&msg);
246
247 get_msg(fd_in, &msg);
248
Kevin Stevesef4eea92001-02-05 12:42:17 +0000249 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100250 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000251 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100252 type);
253 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100254 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100255 }
256 version = buffer_get_int(&msg);
257
258 debug2("Remote version: %d", version);
259
260 /* Check for extensions */
261 while (buffer_len(&msg) > 0) {
262 char *name = buffer_get_string(&msg, NULL);
263 char *value = buffer_get_string(&msg, NULL);
264
265 debug2("Init extension: \"%s\"", name);
266 xfree(name);
267 xfree(value);
268 }
269
270 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100271
Damien Miller3db5f532002-02-13 14:10:32 +1100272 ret = xmalloc(sizeof(*ret));
273 ret->fd_in = fd_in;
274 ret->fd_out = fd_out;
275 ret->transfer_buflen = transfer_buflen;
276 ret->num_requests = num_requests;
277 ret->version = version;
278 ret->msg_id = 1;
279
280 /* Some filexfer v.0 servers don't support large packets */
281 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000282 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100283
284 return(ret);
285}
286
287u_int
288sftp_proto_version(struct sftp_conn *conn)
289{
290 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100291}
292
293int
Damien Miller3db5f532002-02-13 14:10:32 +1100294do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100295{
296 u_int id, status;
297 Buffer msg;
298
299 buffer_init(&msg);
300
Damien Miller3db5f532002-02-13 14:10:32 +1100301 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100302 buffer_put_char(&msg, SSH2_FXP_CLOSE);
303 buffer_put_int(&msg, id);
304 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100305 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000306 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100307
Damien Miller3db5f532002-02-13 14:10:32 +1100308 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100309 if (status != SSH2_FX_OK)
310 error("Couldn't close file: %s", fx2txt(status));
311
312 buffer_free(&msg);
313
314 return(status);
315}
316
Damien Miller4870afd2001-03-14 10:27:09 +1100317
Ben Lindstrombba81212001-06-25 05:01:22 +0000318static int
Damien Miller3db5f532002-02-13 14:10:32 +1100319do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100320 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100321{
322 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000323 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100324 char *handle;
325
Damien Miller3db5f532002-02-13 14:10:32 +1100326 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100327
328 buffer_init(&msg);
329 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
330 buffer_put_int(&msg, id);
331 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100332 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100333
334 buffer_clear(&msg);
335
Damien Miller3db5f532002-02-13 14:10:32 +1100336 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100337 if (handle == NULL)
338 return(-1);
339
Damien Miller4870afd2001-03-14 10:27:09 +1100340 if (dir) {
341 ents = 0;
342 *dir = xmalloc(sizeof(**dir));
343 (*dir)[0] = NULL;
344 }
Damien Miller4870afd2001-03-14 10:27:09 +1100345
Darren Tuckercdf547a2004-05-24 10:12:19 +1000346 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100347 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100348
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000349 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100350
351 buffer_clear(&msg);
352 buffer_put_char(&msg, SSH2_FXP_READDIR);
353 buffer_put_int(&msg, id);
354 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100355 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100356
357 buffer_clear(&msg);
358
Damien Miller3db5f532002-02-13 14:10:32 +1100359 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100360
361 type = buffer_get_char(&msg);
362 id = buffer_get_int(&msg);
363
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000364 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100365
366 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000367 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100368
369 if (type == SSH2_FXP_STATUS) {
370 int status = buffer_get_int(&msg);
371
372 debug3("Received SSH2_FXP_STATUS %d", status);
373
374 if (status == SSH2_FX_EOF) {
375 break;
376 } else {
377 error("Couldn't read directory: %s",
378 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100379 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100380 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000381 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100382 }
383 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000384 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100385 SSH2_FXP_NAME, type);
386
387 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100388 if (count == 0)
389 break;
390 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100391 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100392 char *filename, *longname;
393 Attrib *a;
394
395 filename = buffer_get_string(&msg, NULL);
396 longname = buffer_get_string(&msg, NULL);
397 a = decode_attrib(&msg);
398
Damien Miller4870afd2001-03-14 10:27:09 +1100399 if (printflag)
400 printf("%s\n", longname);
401
402 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100403 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100404 (*dir)[ents] = xmalloc(sizeof(***dir));
405 (*dir)[ents]->filename = xstrdup(filename);
406 (*dir)[ents]->longname = xstrdup(longname);
407 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
408 (*dir)[++ents] = NULL;
409 }
Damien Miller33804262001-02-04 23:20:18 +1100410
411 xfree(filename);
412 xfree(longname);
413 }
414 }
415
416 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100417 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100418 xfree(handle);
419
Darren Tuckercdf547a2004-05-24 10:12:19 +1000420 /* Don't return partial matches on interrupt */
421 if (interrupted && dir != NULL && *dir != NULL) {
422 free_sftp_dirents(*dir);
423 *dir = xmalloc(sizeof(**dir));
424 **dir = NULL;
425 }
426
Damien Miller33804262001-02-04 23:20:18 +1100427 return(0);
428}
429
430int
Damien Miller3db5f532002-02-13 14:10:32 +1100431do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100432{
Damien Miller3db5f532002-02-13 14:10:32 +1100433 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100434}
435
436void free_sftp_dirents(SFTP_DIRENT **s)
437{
438 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100439
440 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100441 xfree(s[i]->filename);
442 xfree(s[i]->longname);
443 xfree(s[i]);
444 }
445 xfree(s);
446}
447
448int
Damien Miller3db5f532002-02-13 14:10:32 +1100449do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100450{
451 u_int status, id;
452
453 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
454
Damien Miller3db5f532002-02-13 14:10:32 +1100455 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000456 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100457 strlen(path));
458 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100459 if (status != SSH2_FX_OK)
460 error("Couldn't delete file: %s", fx2txt(status));
461 return(status);
462}
463
464int
Damien Miller3db5f532002-02-13 14:10:32 +1100465do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100466{
467 u_int status, id;
468
Damien Miller3db5f532002-02-13 14:10:32 +1100469 id = conn->msg_id++;
470 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100471 strlen(path), a);
472
Damien Miller3db5f532002-02-13 14:10:32 +1100473 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100474 if (status != SSH2_FX_OK)
475 error("Couldn't create directory: %s", fx2txt(status));
476
477 return(status);
478}
479
480int
Damien Miller3db5f532002-02-13 14:10:32 +1100481do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100482{
483 u_int status, id;
484
Damien Miller3db5f532002-02-13 14:10:32 +1100485 id = conn->msg_id++;
486 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
487 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100488
Damien Miller3db5f532002-02-13 14:10:32 +1100489 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100490 if (status != SSH2_FX_OK)
491 error("Couldn't remove directory: %s", fx2txt(status));
492
493 return(status);
494}
495
496Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100497do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100498{
499 u_int id;
500
Damien Miller3db5f532002-02-13 14:10:32 +1100501 id = conn->msg_id++;
502
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000503 send_string_request(conn->fd_out, id,
504 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100505 path, strlen(path));
506
507 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100508}
509
510Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100511do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100512{
513 u_int id;
514
Damien Miller3db5f532002-02-13 14:10:32 +1100515 if (conn->version == 0) {
516 if (quiet)
517 debug("Server version does not support lstat operation");
518 else
Damien Miller996acd22003-04-09 20:59:48 +1000519 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000520 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100521 }
522
523 id = conn->msg_id++;
524 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
525 strlen(path));
526
527 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100528}
529
530Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100531do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100532{
533 u_int id;
534
Damien Miller3db5f532002-02-13 14:10:32 +1100535 id = conn->msg_id++;
536 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
537 handle_len);
538
539 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100540}
541
542int
Damien Miller3db5f532002-02-13 14:10:32 +1100543do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100544{
545 u_int status, id;
546
Damien Miller3db5f532002-02-13 14:10:32 +1100547 id = conn->msg_id++;
548 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100549 strlen(path), a);
550
Damien Miller3db5f532002-02-13 14:10:32 +1100551 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100552 if (status != SSH2_FX_OK)
553 error("Couldn't setstat on \"%s\": %s", path,
554 fx2txt(status));
555
556 return(status);
557}
558
559int
Damien Miller3db5f532002-02-13 14:10:32 +1100560do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100561 Attrib *a)
562{
563 u_int status, id;
564
Damien Miller3db5f532002-02-13 14:10:32 +1100565 id = conn->msg_id++;
566 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100567 handle_len, a);
568
Damien Miller3db5f532002-02-13 14:10:32 +1100569 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100570 if (status != SSH2_FX_OK)
571 error("Couldn't fsetstat: %s", fx2txt(status));
572
573 return(status);
574}
575
576char *
Damien Miller3db5f532002-02-13 14:10:32 +1100577do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100578{
579 Buffer msg;
580 u_int type, expected_id, count, id;
581 char *filename, *longname;
582 Attrib *a;
583
Damien Miller3db5f532002-02-13 14:10:32 +1100584 expected_id = id = conn->msg_id++;
585 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
586 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100587
588 buffer_init(&msg);
589
Damien Miller3db5f532002-02-13 14:10:32 +1100590 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100591 type = buffer_get_char(&msg);
592 id = buffer_get_int(&msg);
593
594 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000595 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100596
597 if (type == SSH2_FXP_STATUS) {
598 u_int status = buffer_get_int(&msg);
599
600 error("Couldn't canonicalise: %s", fx2txt(status));
601 return(NULL);
602 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000603 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100604 SSH2_FXP_NAME, type);
605
606 count = buffer_get_int(&msg);
607 if (count != 1)
608 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
609
610 filename = buffer_get_string(&msg, NULL);
611 longname = buffer_get_string(&msg, NULL);
612 a = decode_attrib(&msg);
613
614 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
615
616 xfree(longname);
617
618 buffer_free(&msg);
619
620 return(filename);
621}
622
623int
Damien Miller3db5f532002-02-13 14:10:32 +1100624do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100625{
626 Buffer msg;
627 u_int status, id;
628
629 buffer_init(&msg);
630
631 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100632 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100633 buffer_put_char(&msg, SSH2_FXP_RENAME);
634 buffer_put_int(&msg, id);
635 buffer_put_cstring(&msg, oldpath);
636 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100637 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100638 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
639 newpath);
640 buffer_free(&msg);
641
Damien Miller3db5f532002-02-13 14:10:32 +1100642 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100643 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100644 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
645 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100646
647 return(status);
648}
649
650int
Damien Miller3db5f532002-02-13 14:10:32 +1100651do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100652{
653 Buffer msg;
654 u_int status, id;
655
Damien Miller3db5f532002-02-13 14:10:32 +1100656 if (conn->version < 3) {
657 error("This server does not support the symlink operation");
658 return(SSH2_FX_OP_UNSUPPORTED);
659 }
660
Damien Miller058316f2001-03-08 10:08:49 +1100661 buffer_init(&msg);
662
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000663 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100664 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100665 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
666 buffer_put_int(&msg, id);
667 buffer_put_cstring(&msg, oldpath);
668 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100669 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100670 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
671 newpath);
672 buffer_free(&msg);
673
Damien Miller3db5f532002-02-13 14:10:32 +1100674 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100675 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000676 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100677 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100678
679 return(status);
680}
681
682char *
Damien Miller3db5f532002-02-13 14:10:32 +1100683do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100684{
685 Buffer msg;
686 u_int type, expected_id, count, id;
687 char *filename, *longname;
688 Attrib *a;
689
Damien Miller3db5f532002-02-13 14:10:32 +1100690 expected_id = id = conn->msg_id++;
691 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
692 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100693
694 buffer_init(&msg);
695
Damien Miller3db5f532002-02-13 14:10:32 +1100696 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100697 type = buffer_get_char(&msg);
698 id = buffer_get_int(&msg);
699
700 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000701 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100702
703 if (type == SSH2_FXP_STATUS) {
704 u_int status = buffer_get_int(&msg);
705
706 error("Couldn't readlink: %s", fx2txt(status));
707 return(NULL);
708 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000709 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100710 SSH2_FXP_NAME, type);
711
712 count = buffer_get_int(&msg);
713 if (count != 1)
714 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
715
716 filename = buffer_get_string(&msg, NULL);
717 longname = buffer_get_string(&msg, NULL);
718 a = decode_attrib(&msg);
719
720 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
721
722 xfree(longname);
723
724 buffer_free(&msg);
725
726 return(filename);
727}
728
Damien Miller16a13332002-02-13 14:03:56 +1100729static void
730send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
731 char *handle, u_int handle_len)
732{
733 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000734
Damien Miller16a13332002-02-13 14:03:56 +1100735 buffer_init(&msg);
736 buffer_clear(&msg);
737 buffer_put_char(&msg, SSH2_FXP_READ);
738 buffer_put_int(&msg, id);
739 buffer_put_string(&msg, handle, handle_len);
740 buffer_put_int64(&msg, offset);
741 buffer_put_int(&msg, len);
742 send_msg(fd_out, &msg);
743 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000744}
Damien Miller16a13332002-02-13 14:03:56 +1100745
Damien Miller058316f2001-03-08 10:08:49 +1100746int
Damien Miller3db5f532002-02-13 14:10:32 +1100747do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
748 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100749{
Damien Miller33804262001-02-04 23:20:18 +1100750 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100751 Buffer msg;
752 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000753 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100754 int read_error, write_errno;
755 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000756 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100757 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100758 struct request {
759 u_int id;
760 u_int len;
761 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000762 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100763 };
764 TAILQ_HEAD(reqhead, request) requests;
765 struct request *req;
766
767 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100768
Damien Miller3db5f532002-02-13 14:10:32 +1100769 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100770 if (a == NULL)
771 return(-1);
772
773 /* XXX: should we preserve set[ug]id? */
774 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100775 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100776 else
777 mode = 0666;
778
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000779 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100780 (!S_ISREG(a->perm))) {
781 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000782 return(-1);
783 }
784
Damien Miller16a13332002-02-13 14:03:56 +1100785 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
786 size = a->size;
787 else
788 size = 0;
789
Damien Miller3db5f532002-02-13 14:10:32 +1100790 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100791 buffer_init(&msg);
792
793 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100794 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100795 buffer_put_char(&msg, SSH2_FXP_OPEN);
796 buffer_put_int(&msg, id);
797 buffer_put_cstring(&msg, remote_path);
798 buffer_put_int(&msg, SSH2_FXF_READ);
799 attrib_clear(&junk); /* Send empty attributes */
800 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100801 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000802 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100803
Damien Miller3db5f532002-02-13 14:10:32 +1100804 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100805 if (handle == NULL) {
806 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100807 return(-1);
808 }
809
Damien Millera8e06ce2003-11-21 23:48:55 +1100810 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +1100811 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100812 if (local_fd == -1) {
813 error("Couldn't open local file \"%s\" for writing: %s",
814 local_path, strerror(errno));
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000815 buffer_free(&msg);
816 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100817 return(-1);
818 }
819
Damien Miller33804262001-02-04 23:20:18 +1100820 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100821 write_error = read_error = write_errno = num_req = offset = 0;
822 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +1100823 progress_counter = 0;
824
Damien Miller9ba30692004-03-08 23:12:02 +1100825 if (showprogress && size != 0)
826 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +1100827
Damien Miller16a13332002-02-13 14:03:56 +1100828 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100829 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100830 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100831
Darren Tuckercdf547a2004-05-24 10:12:19 +1000832 /*
Darren Tuckerfc959702004-07-17 16:12:08 +1000833 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +1000834 * allow outstanding requests to drain gracefully
835 */
836 if (interrupted) {
837 if (num_req == 0) /* If we haven't started yet... */
838 break;
839 max_req = 0;
840 }
841
Damien Miller16a13332002-02-13 14:03:56 +1100842 /* Send some more requests */
843 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000844 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000845 (unsigned long long)offset,
846 (unsigned long long)offset + buflen - 1,
847 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100848 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100849 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100850 req->len = buflen;
851 req->offset = offset;
852 offset += buflen;
853 num_req++;
854 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000855 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +1100856 req->len, handle, handle_len);
857 }
Damien Miller33804262001-02-04 23:20:18 +1100858
859 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100860 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100861 type = buffer_get_char(&msg);
862 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000863 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +1100864
Damien Miller16a13332002-02-13 14:03:56 +1100865 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +1100866 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +1100867 req != NULL && req->id != id;
868 req = TAILQ_NEXT(req, tq))
869 ;
870 if (req == NULL)
871 fatal("Unexpected reply %u", id);
872
873 switch (type) {
874 case SSH2_FXP_STATUS:
875 status = buffer_get_int(&msg);
876 if (status != SSH2_FX_EOF)
877 read_error = 1;
878 max_req = 0;
879 TAILQ_REMOVE(&requests, req, tq);
880 xfree(req);
881 num_req--;
882 break;
883 case SSH2_FXP_DATA:
884 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +0000885 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000886 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +0000887 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +1100888 if (len > req->len)
889 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +0000890 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +1100891 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +1000892 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +1100893 !write_error) {
894 write_errno = errno;
895 write_error = 1;
896 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +1100897 }
Damien Miller62d57f62003-01-10 21:43:24 +1100898 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +1100899 xfree(data);
900
901 if (len == req->len) {
902 TAILQ_REMOVE(&requests, req, tq);
903 xfree(req);
904 num_req--;
905 } else {
906 /* Resend the request for the missing data */
907 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +0000908 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000909 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +0000910 (unsigned long long)req->offset +
911 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +1100912 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100913 req->len -= len;
914 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000915 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +1100916 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100917 /* Reduce the request size */
918 if (len < buflen)
919 buflen = MAX(MIN_READ_SIZE, len);
920 }
921 if (max_req > 0) { /* max_req = 0 iff EOF received */
922 if (size > 0 && offset > size) {
923 /* Only one request at a time
924 * after the expected EOF */
925 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +0000926 (unsigned long long)offset,
927 num_req);
Damien Miller16a13332002-02-13 14:03:56 +1100928 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000929 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +1100930 ++max_req;
931 }
932 }
933 break;
934 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000935 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100936 SSH2_FXP_DATA, type);
937 }
Damien Miller33804262001-02-04 23:20:18 +1100938 }
Damien Miller33804262001-02-04 23:20:18 +1100939
Damien Miller62d57f62003-01-10 21:43:24 +1100940 if (showprogress && size)
941 stop_progress_meter();
942
Damien Miller16a13332002-02-13 14:03:56 +1100943 /* Sanity check */
944 if (TAILQ_FIRST(&requests) != NULL)
945 fatal("Transfer complete, but requests still in queue");
946
947 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000948 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +1100949 remote_path, fx2txt(status));
950 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100951 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +1100952 error("Couldn't write to \"%s\": %s", local_path,
953 strerror(write_errno));
954 status = -1;
955 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100956 } else {
Damien Miller3db5f532002-02-13 14:10:32 +1100957 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100958
959 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000960#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +1100961 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +1100962#else
Damien Miller16a13332002-02-13 14:03:56 +1100963 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000964#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +1100965 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +0000966 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100967 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
968 struct timeval tv[2];
969 tv[0].tv_sec = a->atime;
970 tv[1].tv_sec = a->mtime;
971 tv[0].tv_usec = tv[1].tv_usec = 0;
972 if (utimes(local_path, tv) == -1)
973 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +0000974 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100975 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000976 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100977 close(local_fd);
978 buffer_free(&msg);
979 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100980
981 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100982}
983
984int
Damien Miller3db5f532002-02-13 14:10:32 +1100985do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
986 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100987{
Damien Miller8829d362002-02-08 22:04:05 +1100988 int local_fd, status;
Damien Miller5873dfd2002-02-13 14:04:37 +1100989 u_int handle_len, id, type;
Damien Miller33804262001-02-04 23:20:18 +1100990 u_int64_t offset;
Damien Miller8829d362002-02-08 22:04:05 +1100991 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +1100992 Buffer msg;
993 struct stat sb;
994 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +1100995 u_int32_t startid;
996 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +1100997 struct outstanding_ack {
998 u_int id;
999 u_int len;
1000 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001001 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001002 };
1003 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001004 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001005
1006 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001007
1008 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1009 error("Couldn't open local file \"%s\" for reading: %s",
1010 local_path, strerror(errno));
1011 return(-1);
1012 }
1013 if (fstat(local_fd, &sb) == -1) {
1014 error("Couldn't fstat local file \"%s\": %s",
1015 local_path, strerror(errno));
1016 close(local_fd);
1017 return(-1);
1018 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001019 if (!S_ISREG(sb.st_mode)) {
1020 error("%s is not a regular file", local_path);
1021 close(local_fd);
1022 return(-1);
1023 }
Damien Miller33804262001-02-04 23:20:18 +11001024 stat_to_attrib(&sb, &a);
1025
1026 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1027 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1028 a.perm &= 0777;
1029 if (!pflag)
1030 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1031
1032 buffer_init(&msg);
1033
1034 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001035 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001036 buffer_put_char(&msg, SSH2_FXP_OPEN);
1037 buffer_put_int(&msg, id);
1038 buffer_put_cstring(&msg, remote_path);
1039 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1040 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001041 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001042 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001043
1044 buffer_clear(&msg);
1045
Damien Miller3db5f532002-02-13 14:10:32 +11001046 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001047 if (handle == NULL) {
1048 close(local_fd);
1049 buffer_free(&msg);
1050 return(-1);
1051 }
1052
Damien Miller16a13332002-02-13 14:03:56 +11001053 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001054 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001055
Damien Miller33804262001-02-04 23:20:18 +11001056 /* Read from local and write to remote */
1057 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001058 if (showprogress)
1059 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001060
Damien Miller9f0f5c62001-12-21 14:45:46 +11001061 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001062 int len;
Damien Miller33804262001-02-04 23:20:18 +11001063
1064 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001065 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001066 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001067 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001068 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001069 */
Darren Tuckercdf547a2004-05-24 10:12:19 +10001070 if (interrupted)
1071 len = 0;
1072 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001073 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001074 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1075
1076 if (len == -1)
1077 fatal("Couldn't read from \"%s\": %s", local_path,
1078 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001079
1080 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001081 ack = xmalloc(sizeof(*ack));
1082 ack->id = ++id;
1083 ack->offset = offset;
1084 ack->len = len;
1085 TAILQ_INSERT_TAIL(&acks, ack, tq);
1086
Damien Miller16a13332002-02-13 14:03:56 +11001087 buffer_clear(&msg);
1088 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001089 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001090 buffer_put_string(&msg, handle, handle_len);
1091 buffer_put_int64(&msg, offset);
1092 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001093 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001094 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001095 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001096 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001097 break;
1098
Damien Miller5873dfd2002-02-13 14:04:37 +11001099 if (ack == NULL)
1100 fatal("Unexpected ACK %u", id);
1101
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001102 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001103 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001104 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001105
Damien Miller5873dfd2002-02-13 14:04:37 +11001106 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001107 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001108 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001109 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001110
1111 if (type != SSH2_FXP_STATUS)
1112 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1113 "got %d", SSH2_FXP_STATUS, type);
1114
1115 status = buffer_get_int(&msg);
1116 debug3("SSH2_FXP_STATUS %d", status);
1117
1118 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001119 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001120 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001121 ack = TAILQ_NEXT(ack, tq))
1122 ;
1123 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001124 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001125 TAILQ_REMOVE(&acks, ack, tq);
1126
Damien Miller16a13332002-02-13 14:03:56 +11001127 if (status != SSH2_FX_OK) {
1128 error("Couldn't write to remote file \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001129 remote_path, fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +11001130 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001131 close(local_fd);
Damien Miller00111382003-03-10 11:21:17 +11001132 xfree(data);
1133 xfree(ack);
Damien Miller16a13332002-02-13 14:03:56 +11001134 goto done;
1135 }
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001136 debug3("In write loop, ack for %u %u bytes at %llu",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001137 ack->id, ack->len, (unsigned long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001138 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001139 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001140 }
Damien Miller33804262001-02-04 23:20:18 +11001141 offset += len;
1142 }
Damien Miller62d57f62003-01-10 21:43:24 +11001143 if (showprogress)
1144 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001145 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001146
1147 if (close(local_fd) == -1) {
1148 error("Couldn't close local file \"%s\": %s", local_path,
1149 strerror(errno));
Damien Miller3db5f532002-02-13 14:10:32 +11001150 do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001151 status = -1;
1152 goto done;
Damien Miller33804262001-02-04 23:20:18 +11001153 }
1154
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001155 /* Override umask and utimes if asked */
1156 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001157 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001158
Damien Miller3db5f532002-02-13 14:10:32 +11001159 status = do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001160
1161done:
1162 xfree(handle);
1163 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001164 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001165}