blob: e10361e93d4cc40cefd646a6dc5037a62b7acf76 [file] [log] [blame]
Damien Miller57cf6382006-07-10 21:13:46 +10001/* $OpenBSD: sftp-client.c,v 1.66 2006/07/09 15:15:11 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
30#include <fcntl.h>
31#include <signal.h>
Damien Miller16a13332002-02-13 14:03:56 +110032
Damien Miller9b481512002-09-12 10:43:29 +100033#include "openbsd-compat/sys-queue.h"
Damien Miller33804262001-02-04 23:20:18 +110034
Damien Miller33804262001-02-04 23:20:18 +110035#include "buffer.h"
36#include "bufaux.h"
Damien Miller33804262001-02-04 23:20:18 +110037#include "xmalloc.h"
38#include "log.h"
39#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110040#include "progressmeter.h"
Damien Miller3f941882006-03-31 23:13:02 +110041#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110042
43#include "sftp.h"
44#include "sftp-common.h"
45#include "sftp-client.h"
46
Darren Tuckercdf547a2004-05-24 10:12:19 +100047extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110048extern int showprogress;
49
Damien Miller0c8d8f62006-03-15 11:34:25 +110050/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110051#define MIN_READ_SIZE 512
52
Damien Miller3db5f532002-02-13 14:10:32 +110053struct sftp_conn {
54 int fd_in;
55 int fd_out;
56 u_int transfer_buflen;
57 u_int num_requests;
58 u_int version;
59 u_int msg_id;
60};
Ben Lindstrom288cc392001-02-09 02:58:04 +000061
Ben Lindstrombba81212001-06-25 05:01:22 +000062static void
Damien Miller33804262001-02-04 23:20:18 +110063send_msg(int fd, Buffer *m)
64{
Damien Millera7f3aaa2003-01-10 21:43:58 +110065 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +100066 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +110067
Damien Miller54446182006-01-02 23:40:50 +110068 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +110069 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +110070
Damien Millera7f3aaa2003-01-10 21:43:58 +110071 /* Send length first */
Damien Miller3f941882006-03-31 23:13:02 +110072 put_u32(mlen, buffer_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +100073 iov[0].iov_base = mlen;
74 iov[0].iov_len = sizeof(mlen);
75 iov[1].iov_base = buffer_ptr(m);
76 iov[1].iov_len = buffer_len(m);
77
78 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +110079 fatal("Couldn't send packet: %s", strerror(errno));
80
81 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +110082}
83
Ben Lindstrombba81212001-06-25 05:01:22 +000084static void
Damien Miller33804262001-02-04 23:20:18 +110085get_msg(int fd, Buffer *m)
86{
Damien Millera7f3aaa2003-01-10 21:43:58 +110087 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +110088
Damien Millera7f3aaa2003-01-10 21:43:58 +110089 buffer_append_space(m, 4);
Damien Millerb253cc42005-05-26 12:23:44 +100090 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
91 if (errno == EPIPE)
92 fatal("Connection closed");
93 else
94 fatal("Couldn't read packet: %s", strerror(errno));
95 }
Damien Miller33804262001-02-04 23:20:18 +110096
Damien Millera7f3aaa2003-01-10 21:43:58 +110097 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +110098 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +000099 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100100
Damien Millera7f3aaa2003-01-10 21:43:58 +1100101 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000102 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
103 if (errno == EPIPE)
104 fatal("Connection closed");
105 else
106 fatal("Read packet: %s", strerror(errno));
107 }
Damien Miller33804262001-02-04 23:20:18 +1100108}
109
Ben Lindstrombba81212001-06-25 05:01:22 +0000110static void
Damien Miller33804262001-02-04 23:20:18 +1100111send_string_request(int fd, u_int id, u_int code, char *s,
112 u_int len)
113{
114 Buffer msg;
115
116 buffer_init(&msg);
117 buffer_put_char(&msg, code);
118 buffer_put_int(&msg, id);
119 buffer_put_string(&msg, s, len);
120 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000121 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100122 buffer_free(&msg);
123}
124
Ben Lindstrombba81212001-06-25 05:01:22 +0000125static void
Damien Miller33804262001-02-04 23:20:18 +1100126send_string_attrs_request(int fd, u_int id, u_int code, char *s,
127 u_int len, Attrib *a)
128{
129 Buffer msg;
130
131 buffer_init(&msg);
132 buffer_put_char(&msg, code);
133 buffer_put_int(&msg, id);
134 buffer_put_string(&msg, s, len);
135 encode_attrib(&msg, a);
136 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000137 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100138 buffer_free(&msg);
139}
140
Ben Lindstrombba81212001-06-25 05:01:22 +0000141static u_int
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000142get_status(int fd, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100143{
144 Buffer msg;
145 u_int type, id, status;
146
147 buffer_init(&msg);
148 get_msg(fd, &msg);
149 type = buffer_get_char(&msg);
150 id = buffer_get_int(&msg);
151
152 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000153 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100154 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000155 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100156 SSH2_FXP_STATUS, type);
157
158 status = buffer_get_int(&msg);
159 buffer_free(&msg);
160
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000161 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100162
163 return(status);
164}
165
Ben Lindstrombba81212001-06-25 05:01:22 +0000166static char *
Damien Miller33804262001-02-04 23:20:18 +1100167get_handle(int fd, u_int expected_id, u_int *len)
168{
169 Buffer msg;
170 u_int type, id;
171 char *handle;
172
173 buffer_init(&msg);
174 get_msg(fd, &msg);
175 type = buffer_get_char(&msg);
176 id = buffer_get_int(&msg);
177
178 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000179 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100180 if (type == SSH2_FXP_STATUS) {
181 int status = buffer_get_int(&msg);
182
183 error("Couldn't get handle: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100184 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100185 return(NULL);
186 } else if (type != SSH2_FXP_HANDLE)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000187 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100188 SSH2_FXP_HANDLE, type);
189
190 handle = buffer_get_string(&msg, len);
191 buffer_free(&msg);
192
193 return(handle);
194}
195
Ben Lindstrombba81212001-06-25 05:01:22 +0000196static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000197get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100198{
199 Buffer msg;
200 u_int type, id;
201 Attrib *a;
202
203 buffer_init(&msg);
204 get_msg(fd, &msg);
205
206 type = buffer_get_char(&msg);
207 id = buffer_get_int(&msg);
208
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000209 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100210 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000211 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100212 if (type == SSH2_FXP_STATUS) {
213 int status = buffer_get_int(&msg);
214
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000215 if (quiet)
216 debug("Couldn't stat remote file: %s", fx2txt(status));
217 else
218 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100219 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100220 return(NULL);
221 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000222 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100223 SSH2_FXP_ATTRS, type);
224 }
225 a = decode_attrib(&msg);
226 buffer_free(&msg);
227
228 return(a);
229}
230
Damien Miller3db5f532002-02-13 14:10:32 +1100231struct sftp_conn *
232do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100233{
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000234 u_int type;
235 int version;
Damien Miller33804262001-02-04 23:20:18 +1100236 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100237 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100238
239 buffer_init(&msg);
240 buffer_put_char(&msg, SSH2_FXP_INIT);
241 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
242 send_msg(fd_out, &msg);
243
244 buffer_clear(&msg);
245
246 get_msg(fd_in, &msg);
247
Kevin Stevesef4eea92001-02-05 12:42:17 +0000248 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100249 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000250 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100251 type);
252 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100253 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100254 }
255 version = buffer_get_int(&msg);
256
257 debug2("Remote version: %d", version);
258
259 /* Check for extensions */
260 while (buffer_len(&msg) > 0) {
261 char *name = buffer_get_string(&msg, NULL);
262 char *value = buffer_get_string(&msg, NULL);
263
264 debug2("Init extension: \"%s\"", name);
265 xfree(name);
266 xfree(value);
267 }
268
269 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100270
Damien Miller3db5f532002-02-13 14:10:32 +1100271 ret = xmalloc(sizeof(*ret));
272 ret->fd_in = fd_in;
273 ret->fd_out = fd_out;
274 ret->transfer_buflen = transfer_buflen;
275 ret->num_requests = num_requests;
276 ret->version = version;
277 ret->msg_id = 1;
278
279 /* Some filexfer v.0 servers don't support large packets */
280 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000281 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100282
283 return(ret);
284}
285
286u_int
287sftp_proto_version(struct sftp_conn *conn)
288{
289 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100290}
291
292int
Damien Miller3db5f532002-02-13 14:10:32 +1100293do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100294{
295 u_int id, status;
296 Buffer msg;
297
298 buffer_init(&msg);
299
Damien Miller3db5f532002-02-13 14:10:32 +1100300 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100301 buffer_put_char(&msg, SSH2_FXP_CLOSE);
302 buffer_put_int(&msg, id);
303 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100304 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000305 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100306
Damien Miller3db5f532002-02-13 14:10:32 +1100307 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100308 if (status != SSH2_FX_OK)
309 error("Couldn't close file: %s", fx2txt(status));
310
311 buffer_free(&msg);
312
313 return(status);
314}
315
Damien Miller4870afd2001-03-14 10:27:09 +1100316
Ben Lindstrombba81212001-06-25 05:01:22 +0000317static int
Damien Miller3db5f532002-02-13 14:10:32 +1100318do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100319 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100320{
321 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000322 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100323 char *handle;
324
Damien Miller3db5f532002-02-13 14:10:32 +1100325 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100326
327 buffer_init(&msg);
328 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
329 buffer_put_int(&msg, id);
330 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100331 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100332
333 buffer_clear(&msg);
334
Damien Miller3db5f532002-02-13 14:10:32 +1100335 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100336 if (handle == NULL)
337 return(-1);
338
Damien Miller4870afd2001-03-14 10:27:09 +1100339 if (dir) {
340 ents = 0;
341 *dir = xmalloc(sizeof(**dir));
342 (*dir)[0] = NULL;
343 }
Damien Miller4870afd2001-03-14 10:27:09 +1100344
Darren Tuckercdf547a2004-05-24 10:12:19 +1000345 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100346 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100347
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000348 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100349
350 buffer_clear(&msg);
351 buffer_put_char(&msg, SSH2_FXP_READDIR);
352 buffer_put_int(&msg, id);
353 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100354 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100355
356 buffer_clear(&msg);
357
Damien Miller3db5f532002-02-13 14:10:32 +1100358 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100359
360 type = buffer_get_char(&msg);
361 id = buffer_get_int(&msg);
362
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000363 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100364
365 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000366 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100367
368 if (type == SSH2_FXP_STATUS) {
369 int status = buffer_get_int(&msg);
370
371 debug3("Received SSH2_FXP_STATUS %d", status);
372
373 if (status == SSH2_FX_EOF) {
374 break;
375 } else {
376 error("Couldn't read directory: %s",
377 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100378 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100379 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000380 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100381 }
382 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000383 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100384 SSH2_FXP_NAME, type);
385
386 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100387 if (count == 0)
388 break;
389 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100390 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100391 char *filename, *longname;
392 Attrib *a;
393
394 filename = buffer_get_string(&msg, NULL);
395 longname = buffer_get_string(&msg, NULL);
396 a = decode_attrib(&msg);
397
Damien Miller4870afd2001-03-14 10:27:09 +1100398 if (printflag)
399 printf("%s\n", longname);
400
401 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100402 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100403 (*dir)[ents] = xmalloc(sizeof(***dir));
404 (*dir)[ents]->filename = xstrdup(filename);
405 (*dir)[ents]->longname = xstrdup(longname);
406 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
407 (*dir)[++ents] = NULL;
408 }
Damien Miller33804262001-02-04 23:20:18 +1100409
410 xfree(filename);
411 xfree(longname);
412 }
413 }
414
415 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100416 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100417 xfree(handle);
418
Darren Tuckercdf547a2004-05-24 10:12:19 +1000419 /* Don't return partial matches on interrupt */
420 if (interrupted && dir != NULL && *dir != NULL) {
421 free_sftp_dirents(*dir);
422 *dir = xmalloc(sizeof(**dir));
423 **dir = NULL;
424 }
425
Damien Miller33804262001-02-04 23:20:18 +1100426 return(0);
427}
428
429int
Damien Miller3db5f532002-02-13 14:10:32 +1100430do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100431{
Damien Miller3db5f532002-02-13 14:10:32 +1100432 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100433}
434
435void free_sftp_dirents(SFTP_DIRENT **s)
436{
437 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100438
439 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100440 xfree(s[i]->filename);
441 xfree(s[i]->longname);
442 xfree(s[i]);
443 }
444 xfree(s);
445}
446
447int
Damien Miller3db5f532002-02-13 14:10:32 +1100448do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100449{
450 u_int status, id;
451
452 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
453
Damien Miller3db5f532002-02-13 14:10:32 +1100454 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000455 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100456 strlen(path));
457 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100458 if (status != SSH2_FX_OK)
459 error("Couldn't delete file: %s", fx2txt(status));
460 return(status);
461}
462
463int
Damien Miller3db5f532002-02-13 14:10:32 +1100464do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100465{
466 u_int status, id;
467
Damien Miller3db5f532002-02-13 14:10:32 +1100468 id = conn->msg_id++;
469 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100470 strlen(path), a);
471
Damien Miller3db5f532002-02-13 14:10:32 +1100472 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100473 if (status != SSH2_FX_OK)
474 error("Couldn't create directory: %s", fx2txt(status));
475
476 return(status);
477}
478
479int
Damien Miller3db5f532002-02-13 14:10:32 +1100480do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100481{
482 u_int status, id;
483
Damien Miller3db5f532002-02-13 14:10:32 +1100484 id = conn->msg_id++;
485 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
486 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100487
Damien Miller3db5f532002-02-13 14:10:32 +1100488 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100489 if (status != SSH2_FX_OK)
490 error("Couldn't remove directory: %s", fx2txt(status));
491
492 return(status);
493}
494
495Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100496do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100497{
498 u_int id;
499
Damien Miller3db5f532002-02-13 14:10:32 +1100500 id = conn->msg_id++;
501
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000502 send_string_request(conn->fd_out, id,
503 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100504 path, strlen(path));
505
506 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100507}
508
509Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100510do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100511{
512 u_int id;
513
Damien Miller3db5f532002-02-13 14:10:32 +1100514 if (conn->version == 0) {
515 if (quiet)
516 debug("Server version does not support lstat operation");
517 else
Damien Miller996acd22003-04-09 20:59:48 +1000518 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000519 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100520 }
521
522 id = conn->msg_id++;
523 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
524 strlen(path));
525
526 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100527}
528
529Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100530do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100531{
532 u_int id;
533
Damien Miller3db5f532002-02-13 14:10:32 +1100534 id = conn->msg_id++;
535 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
536 handle_len);
537
538 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100539}
540
541int
Damien Miller3db5f532002-02-13 14:10:32 +1100542do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100543{
544 u_int status, id;
545
Damien Miller3db5f532002-02-13 14:10:32 +1100546 id = conn->msg_id++;
547 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100548 strlen(path), a);
549
Damien Miller3db5f532002-02-13 14:10:32 +1100550 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100551 if (status != SSH2_FX_OK)
552 error("Couldn't setstat on \"%s\": %s", path,
553 fx2txt(status));
554
555 return(status);
556}
557
558int
Damien Miller3db5f532002-02-13 14:10:32 +1100559do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100560 Attrib *a)
561{
562 u_int status, id;
563
Damien Miller3db5f532002-02-13 14:10:32 +1100564 id = conn->msg_id++;
565 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100566 handle_len, a);
567
Damien Miller3db5f532002-02-13 14:10:32 +1100568 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100569 if (status != SSH2_FX_OK)
570 error("Couldn't fsetstat: %s", fx2txt(status));
571
572 return(status);
573}
574
575char *
Damien Miller3db5f532002-02-13 14:10:32 +1100576do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100577{
578 Buffer msg;
579 u_int type, expected_id, count, id;
580 char *filename, *longname;
581 Attrib *a;
582
Damien Miller3db5f532002-02-13 14:10:32 +1100583 expected_id = id = conn->msg_id++;
584 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
585 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100586
587 buffer_init(&msg);
588
Damien Miller3db5f532002-02-13 14:10:32 +1100589 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100590 type = buffer_get_char(&msg);
591 id = buffer_get_int(&msg);
592
593 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000594 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100595
596 if (type == SSH2_FXP_STATUS) {
597 u_int status = buffer_get_int(&msg);
598
599 error("Couldn't canonicalise: %s", fx2txt(status));
600 return(NULL);
601 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000602 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100603 SSH2_FXP_NAME, type);
604
605 count = buffer_get_int(&msg);
606 if (count != 1)
607 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
608
609 filename = buffer_get_string(&msg, NULL);
610 longname = buffer_get_string(&msg, NULL);
611 a = decode_attrib(&msg);
612
613 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
614
615 xfree(longname);
616
617 buffer_free(&msg);
618
619 return(filename);
620}
621
622int
Damien Miller3db5f532002-02-13 14:10:32 +1100623do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100624{
625 Buffer msg;
626 u_int status, id;
627
628 buffer_init(&msg);
629
630 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100631 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100632 buffer_put_char(&msg, SSH2_FXP_RENAME);
633 buffer_put_int(&msg, id);
634 buffer_put_cstring(&msg, oldpath);
635 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100636 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100637 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
638 newpath);
639 buffer_free(&msg);
640
Damien Miller3db5f532002-02-13 14:10:32 +1100641 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100642 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100643 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
644 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100645
646 return(status);
647}
648
649int
Damien Miller3db5f532002-02-13 14:10:32 +1100650do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100651{
652 Buffer msg;
653 u_int status, id;
654
Damien Miller3db5f532002-02-13 14:10:32 +1100655 if (conn->version < 3) {
656 error("This server does not support the symlink operation");
657 return(SSH2_FX_OP_UNSUPPORTED);
658 }
659
Damien Miller058316f2001-03-08 10:08:49 +1100660 buffer_init(&msg);
661
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000662 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100663 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100664 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
665 buffer_put_int(&msg, id);
666 buffer_put_cstring(&msg, oldpath);
667 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100668 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100669 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
670 newpath);
671 buffer_free(&msg);
672
Damien Miller3db5f532002-02-13 14:10:32 +1100673 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100674 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000675 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100676 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100677
678 return(status);
679}
680
681char *
Damien Miller3db5f532002-02-13 14:10:32 +1100682do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100683{
684 Buffer msg;
685 u_int type, expected_id, count, id;
686 char *filename, *longname;
687 Attrib *a;
688
Damien Miller3db5f532002-02-13 14:10:32 +1100689 expected_id = id = conn->msg_id++;
690 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
691 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100692
693 buffer_init(&msg);
694
Damien Miller3db5f532002-02-13 14:10:32 +1100695 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100696 type = buffer_get_char(&msg);
697 id = buffer_get_int(&msg);
698
699 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000700 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100701
702 if (type == SSH2_FXP_STATUS) {
703 u_int status = buffer_get_int(&msg);
704
705 error("Couldn't readlink: %s", fx2txt(status));
706 return(NULL);
707 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000708 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100709 SSH2_FXP_NAME, type);
710
711 count = buffer_get_int(&msg);
712 if (count != 1)
713 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
714
715 filename = buffer_get_string(&msg, NULL);
716 longname = buffer_get_string(&msg, NULL);
717 a = decode_attrib(&msg);
718
719 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
720
721 xfree(longname);
722
723 buffer_free(&msg);
724
725 return(filename);
726}
727
Damien Miller16a13332002-02-13 14:03:56 +1100728static void
729send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
730 char *handle, u_int handle_len)
731{
732 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000733
Damien Miller16a13332002-02-13 14:03:56 +1100734 buffer_init(&msg);
735 buffer_clear(&msg);
736 buffer_put_char(&msg, SSH2_FXP_READ);
737 buffer_put_int(&msg, id);
738 buffer_put_string(&msg, handle, handle_len);
739 buffer_put_int64(&msg, offset);
740 buffer_put_int(&msg, len);
741 send_msg(fd_out, &msg);
742 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000743}
Damien Miller16a13332002-02-13 14:03:56 +1100744
Damien Miller058316f2001-03-08 10:08:49 +1100745int
Damien Miller3db5f532002-02-13 14:10:32 +1100746do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
747 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100748{
Damien Miller33804262001-02-04 23:20:18 +1100749 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100750 Buffer msg;
751 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000752 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100753 int read_error, write_errno;
754 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000755 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100756 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100757 struct request {
758 u_int id;
759 u_int len;
760 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000761 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100762 };
763 TAILQ_HEAD(reqhead, request) requests;
764 struct request *req;
765
766 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100767
Damien Miller3db5f532002-02-13 14:10:32 +1100768 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100769 if (a == NULL)
770 return(-1);
771
772 /* XXX: should we preserve set[ug]id? */
773 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100774 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100775 else
776 mode = 0666;
777
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000778 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100779 (!S_ISREG(a->perm))) {
780 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000781 return(-1);
782 }
783
Damien Miller16a13332002-02-13 14:03:56 +1100784 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
785 size = a->size;
786 else
787 size = 0;
788
Damien Miller3db5f532002-02-13 14:10:32 +1100789 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100790 buffer_init(&msg);
791
792 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100793 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100794 buffer_put_char(&msg, SSH2_FXP_OPEN);
795 buffer_put_int(&msg, id);
796 buffer_put_cstring(&msg, remote_path);
797 buffer_put_int(&msg, SSH2_FXF_READ);
798 attrib_clear(&junk); /* Send empty attributes */
799 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100800 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000801 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100802
Damien Miller3db5f532002-02-13 14:10:32 +1100803 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100804 if (handle == NULL) {
805 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100806 return(-1);
807 }
808
Damien Millera8e06ce2003-11-21 23:48:55 +1100809 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +1100810 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100811 if (local_fd == -1) {
812 error("Couldn't open local file \"%s\" for writing: %s",
813 local_path, strerror(errno));
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000814 buffer_free(&msg);
815 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100816 return(-1);
817 }
818
Damien Miller33804262001-02-04 23:20:18 +1100819 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100820 write_error = read_error = write_errno = num_req = offset = 0;
821 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +1100822 progress_counter = 0;
823
Damien Miller9ba30692004-03-08 23:12:02 +1100824 if (showprogress && size != 0)
825 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +1100826
Damien Miller16a13332002-02-13 14:03:56 +1100827 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100828 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100829 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100830
Darren Tuckercdf547a2004-05-24 10:12:19 +1000831 /*
Darren Tuckerfc959702004-07-17 16:12:08 +1000832 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +1000833 * allow outstanding requests to drain gracefully
834 */
835 if (interrupted) {
836 if (num_req == 0) /* If we haven't started yet... */
837 break;
838 max_req = 0;
839 }
840
Damien Miller16a13332002-02-13 14:03:56 +1100841 /* Send some more requests */
842 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000843 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000844 (unsigned long long)offset,
845 (unsigned long long)offset + buflen - 1,
846 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100847 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100848 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100849 req->len = buflen;
850 req->offset = offset;
851 offset += buflen;
852 num_req++;
853 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000854 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +1100855 req->len, handle, handle_len);
856 }
Damien Miller33804262001-02-04 23:20:18 +1100857
858 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100859 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100860 type = buffer_get_char(&msg);
861 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000862 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +1100863
Damien Miller16a13332002-02-13 14:03:56 +1100864 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +1100865 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +1100866 req != NULL && req->id != id;
867 req = TAILQ_NEXT(req, tq))
868 ;
869 if (req == NULL)
870 fatal("Unexpected reply %u", id);
871
872 switch (type) {
873 case SSH2_FXP_STATUS:
874 status = buffer_get_int(&msg);
875 if (status != SSH2_FX_EOF)
876 read_error = 1;
877 max_req = 0;
878 TAILQ_REMOVE(&requests, req, tq);
879 xfree(req);
880 num_req--;
881 break;
882 case SSH2_FXP_DATA:
883 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +0000884 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000885 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +0000886 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +1100887 if (len > req->len)
888 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +0000889 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +1100890 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +1000891 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +1100892 !write_error) {
893 write_errno = errno;
894 write_error = 1;
895 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +1100896 }
Damien Miller62d57f62003-01-10 21:43:24 +1100897 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +1100898 xfree(data);
899
900 if (len == req->len) {
901 TAILQ_REMOVE(&requests, req, tq);
902 xfree(req);
903 num_req--;
904 } else {
905 /* Resend the request for the missing data */
906 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +0000907 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000908 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +0000909 (unsigned long long)req->offset +
910 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +1100911 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100912 req->len -= len;
913 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000914 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +1100915 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100916 /* Reduce the request size */
917 if (len < buflen)
918 buflen = MAX(MIN_READ_SIZE, len);
919 }
920 if (max_req > 0) { /* max_req = 0 iff EOF received */
921 if (size > 0 && offset > size) {
922 /* Only one request at a time
923 * after the expected EOF */
924 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +0000925 (unsigned long long)offset,
926 num_req);
Damien Miller16a13332002-02-13 14:03:56 +1100927 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000928 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +1100929 ++max_req;
930 }
931 }
932 break;
933 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000934 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100935 SSH2_FXP_DATA, type);
936 }
Damien Miller33804262001-02-04 23:20:18 +1100937 }
Damien Miller33804262001-02-04 23:20:18 +1100938
Damien Miller62d57f62003-01-10 21:43:24 +1100939 if (showprogress && size)
940 stop_progress_meter();
941
Damien Miller16a13332002-02-13 14:03:56 +1100942 /* Sanity check */
943 if (TAILQ_FIRST(&requests) != NULL)
944 fatal("Transfer complete, but requests still in queue");
945
946 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000947 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +1100948 remote_path, fx2txt(status));
949 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100950 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +1100951 error("Couldn't write to \"%s\": %s", local_path,
952 strerror(write_errno));
953 status = -1;
954 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100955 } else {
Damien Miller3db5f532002-02-13 14:10:32 +1100956 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100957
958 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000959#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +1100960 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +1100961#else
Damien Miller16a13332002-02-13 14:03:56 +1100962 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000963#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +1100964 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +0000965 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100966 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
967 struct timeval tv[2];
968 tv[0].tv_sec = a->atime;
969 tv[1].tv_sec = a->mtime;
970 tv[0].tv_usec = tv[1].tv_usec = 0;
971 if (utimes(local_path, tv) == -1)
972 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +0000973 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100974 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000975 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100976 close(local_fd);
977 buffer_free(&msg);
978 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100979
980 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100981}
982
983int
Damien Miller3db5f532002-02-13 14:10:32 +1100984do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
985 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100986{
Damien Miller8829d362002-02-08 22:04:05 +1100987 int local_fd, status;
Damien Miller5873dfd2002-02-13 14:04:37 +1100988 u_int handle_len, id, type;
Damien Miller33804262001-02-04 23:20:18 +1100989 u_int64_t offset;
Damien Miller8829d362002-02-08 22:04:05 +1100990 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +1100991 Buffer msg;
992 struct stat sb;
993 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +1100994 u_int32_t startid;
995 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +1100996 struct outstanding_ack {
997 u_int id;
998 u_int len;
999 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001000 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001001 };
1002 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001003 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001004
1005 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001006
1007 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1008 error("Couldn't open local file \"%s\" for reading: %s",
1009 local_path, strerror(errno));
1010 return(-1);
1011 }
1012 if (fstat(local_fd, &sb) == -1) {
1013 error("Couldn't fstat local file \"%s\": %s",
1014 local_path, strerror(errno));
1015 close(local_fd);
1016 return(-1);
1017 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001018 if (!S_ISREG(sb.st_mode)) {
1019 error("%s is not a regular file", local_path);
1020 close(local_fd);
1021 return(-1);
1022 }
Damien Miller33804262001-02-04 23:20:18 +11001023 stat_to_attrib(&sb, &a);
1024
1025 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1026 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1027 a.perm &= 0777;
1028 if (!pflag)
1029 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1030
1031 buffer_init(&msg);
1032
1033 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001034 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001035 buffer_put_char(&msg, SSH2_FXP_OPEN);
1036 buffer_put_int(&msg, id);
1037 buffer_put_cstring(&msg, remote_path);
1038 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1039 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001040 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001041 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001042
1043 buffer_clear(&msg);
1044
Damien Miller3db5f532002-02-13 14:10:32 +11001045 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001046 if (handle == NULL) {
1047 close(local_fd);
1048 buffer_free(&msg);
1049 return(-1);
1050 }
1051
Damien Miller16a13332002-02-13 14:03:56 +11001052 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001053 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001054
Damien Miller33804262001-02-04 23:20:18 +11001055 /* Read from local and write to remote */
1056 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001057 if (showprogress)
1058 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001059
Damien Miller9f0f5c62001-12-21 14:45:46 +11001060 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001061 int len;
Damien Miller33804262001-02-04 23:20:18 +11001062
1063 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001064 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001065 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001066 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001067 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001068 */
Darren Tuckercdf547a2004-05-24 10:12:19 +10001069 if (interrupted)
1070 len = 0;
1071 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001072 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001073 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1074
1075 if (len == -1)
1076 fatal("Couldn't read from \"%s\": %s", local_path,
1077 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001078
1079 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001080 ack = xmalloc(sizeof(*ack));
1081 ack->id = ++id;
1082 ack->offset = offset;
1083 ack->len = len;
1084 TAILQ_INSERT_TAIL(&acks, ack, tq);
1085
Damien Miller16a13332002-02-13 14:03:56 +11001086 buffer_clear(&msg);
1087 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001088 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001089 buffer_put_string(&msg, handle, handle_len);
1090 buffer_put_int64(&msg, offset);
1091 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001092 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001093 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001094 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001095 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001096 break;
1097
Damien Miller5873dfd2002-02-13 14:04:37 +11001098 if (ack == NULL)
1099 fatal("Unexpected ACK %u", id);
1100
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001101 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001102 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001103 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001104
Damien Miller5873dfd2002-02-13 14:04:37 +11001105 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001106 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001107 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001108 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001109
1110 if (type != SSH2_FXP_STATUS)
1111 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1112 "got %d", SSH2_FXP_STATUS, type);
1113
1114 status = buffer_get_int(&msg);
1115 debug3("SSH2_FXP_STATUS %d", status);
1116
1117 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001118 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001119 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001120 ack = TAILQ_NEXT(ack, tq))
1121 ;
1122 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001123 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001124 TAILQ_REMOVE(&acks, ack, tq);
1125
Damien Miller16a13332002-02-13 14:03:56 +11001126 if (status != SSH2_FX_OK) {
1127 error("Couldn't write to remote file \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001128 remote_path, fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +11001129 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001130 close(local_fd);
Damien Miller00111382003-03-10 11:21:17 +11001131 xfree(data);
1132 xfree(ack);
Damien Miller16a13332002-02-13 14:03:56 +11001133 goto done;
1134 }
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001135 debug3("In write loop, ack for %u %u bytes at %llu",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001136 ack->id, ack->len, (unsigned long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001137 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001138 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001139 }
Damien Miller33804262001-02-04 23:20:18 +11001140 offset += len;
1141 }
Damien Miller62d57f62003-01-10 21:43:24 +11001142 if (showprogress)
1143 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001144 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001145
1146 if (close(local_fd) == -1) {
1147 error("Couldn't close local file \"%s\": %s", local_path,
1148 strerror(errno));
Damien Miller3db5f532002-02-13 14:10:32 +11001149 do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001150 status = -1;
1151 goto done;
Damien Miller33804262001-02-04 23:20:18 +11001152 }
1153
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001154 /* Override umask and utimes if asked */
1155 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001156 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001157
Damien Miller3db5f532002-02-13 14:10:32 +11001158 status = do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001159
1160done:
1161 xfree(handle);
1162 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001163 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001164}