blob: 84dae58d2d5741d7acf9280252899d4aef0e9e4d [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: sftp-client.c,v 1.69 2006/07/22 20:48:23 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 Millere3476ed2006-07-24 14:13:33 +100033#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100034#include <unistd.h>
Damien Miller16a13332002-02-13 14:03:56 +110035
Damien Miller9b481512002-09-12 10:43:29 +100036#include "openbsd-compat/sys-queue.h"
Damien Miller33804262001-02-04 23:20:18 +110037
Damien Miller33804262001-02-04 23:20:18 +110038#include "buffer.h"
39#include "bufaux.h"
Damien Miller33804262001-02-04 23:20:18 +110040#include "xmalloc.h"
41#include "log.h"
42#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110043#include "progressmeter.h"
Damien Miller3f941882006-03-31 23:13:02 +110044#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110045
46#include "sftp.h"
47#include "sftp-common.h"
48#include "sftp-client.h"
49
Darren Tuckercdf547a2004-05-24 10:12:19 +100050extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110051extern int showprogress;
52
Damien Miller0c8d8f62006-03-15 11:34:25 +110053/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110054#define MIN_READ_SIZE 512
55
Damien Miller3db5f532002-02-13 14:10:32 +110056struct sftp_conn {
57 int fd_in;
58 int fd_out;
59 u_int transfer_buflen;
60 u_int num_requests;
61 u_int version;
62 u_int msg_id;
63};
Ben Lindstrom288cc392001-02-09 02:58:04 +000064
Ben Lindstrombba81212001-06-25 05:01:22 +000065static void
Damien Miller33804262001-02-04 23:20:18 +110066send_msg(int fd, Buffer *m)
67{
Damien Millera7f3aaa2003-01-10 21:43:58 +110068 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +100069 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +110070
Damien Miller54446182006-01-02 23:40:50 +110071 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +110072 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +110073
Damien Millera7f3aaa2003-01-10 21:43:58 +110074 /* Send length first */
Damien Miller3f941882006-03-31 23:13:02 +110075 put_u32(mlen, buffer_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +100076 iov[0].iov_base = mlen;
77 iov[0].iov_len = sizeof(mlen);
78 iov[1].iov_base = buffer_ptr(m);
79 iov[1].iov_len = buffer_len(m);
80
81 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +110082 fatal("Couldn't send packet: %s", strerror(errno));
83
84 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +110085}
86
Ben Lindstrombba81212001-06-25 05:01:22 +000087static void
Damien Miller33804262001-02-04 23:20:18 +110088get_msg(int fd, Buffer *m)
89{
Damien Millera7f3aaa2003-01-10 21:43:58 +110090 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +110091
Damien Millera7f3aaa2003-01-10 21:43:58 +110092 buffer_append_space(m, 4);
Damien Millerb253cc42005-05-26 12:23:44 +100093 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
94 if (errno == EPIPE)
95 fatal("Connection closed");
96 else
97 fatal("Couldn't read packet: %s", strerror(errno));
98 }
Damien Miller33804262001-02-04 23:20:18 +110099
Damien Millera7f3aaa2003-01-10 21:43:58 +1100100 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +1100101 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000102 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100103
Damien Millera7f3aaa2003-01-10 21:43:58 +1100104 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000105 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
106 if (errno == EPIPE)
107 fatal("Connection closed");
108 else
109 fatal("Read packet: %s", strerror(errno));
110 }
Damien Miller33804262001-02-04 23:20:18 +1100111}
112
Ben Lindstrombba81212001-06-25 05:01:22 +0000113static void
Damien Miller33804262001-02-04 23:20:18 +1100114send_string_request(int fd, u_int id, u_int code, char *s,
115 u_int len)
116{
117 Buffer msg;
118
119 buffer_init(&msg);
120 buffer_put_char(&msg, code);
121 buffer_put_int(&msg, id);
122 buffer_put_string(&msg, s, len);
123 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000124 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100125 buffer_free(&msg);
126}
127
Ben Lindstrombba81212001-06-25 05:01:22 +0000128static void
Damien Miller33804262001-02-04 23:20:18 +1100129send_string_attrs_request(int fd, u_int id, u_int code, char *s,
130 u_int len, Attrib *a)
131{
132 Buffer msg;
133
134 buffer_init(&msg);
135 buffer_put_char(&msg, code);
136 buffer_put_int(&msg, id);
137 buffer_put_string(&msg, s, len);
138 encode_attrib(&msg, a);
139 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000140 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100141 buffer_free(&msg);
142}
143
Ben Lindstrombba81212001-06-25 05:01:22 +0000144static u_int
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000145get_status(int fd, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100146{
147 Buffer msg;
148 u_int type, id, status;
149
150 buffer_init(&msg);
151 get_msg(fd, &msg);
152 type = buffer_get_char(&msg);
153 id = buffer_get_int(&msg);
154
155 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000156 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100157 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000158 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100159 SSH2_FXP_STATUS, type);
160
161 status = buffer_get_int(&msg);
162 buffer_free(&msg);
163
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000164 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100165
166 return(status);
167}
168
Ben Lindstrombba81212001-06-25 05:01:22 +0000169static char *
Damien Miller33804262001-02-04 23:20:18 +1100170get_handle(int fd, u_int expected_id, u_int *len)
171{
172 Buffer msg;
173 u_int type, id;
174 char *handle;
175
176 buffer_init(&msg);
177 get_msg(fd, &msg);
178 type = buffer_get_char(&msg);
179 id = buffer_get_int(&msg);
180
181 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000182 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100183 if (type == SSH2_FXP_STATUS) {
184 int status = buffer_get_int(&msg);
185
186 error("Couldn't get handle: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100187 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100188 return(NULL);
189 } else if (type != SSH2_FXP_HANDLE)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000190 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100191 SSH2_FXP_HANDLE, type);
192
193 handle = buffer_get_string(&msg, len);
194 buffer_free(&msg);
195
196 return(handle);
197}
198
Ben Lindstrombba81212001-06-25 05:01:22 +0000199static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000200get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100201{
202 Buffer msg;
203 u_int type, id;
204 Attrib *a;
205
206 buffer_init(&msg);
207 get_msg(fd, &msg);
208
209 type = buffer_get_char(&msg);
210 id = buffer_get_int(&msg);
211
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000212 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100213 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000214 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100215 if (type == SSH2_FXP_STATUS) {
216 int status = buffer_get_int(&msg);
217
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000218 if (quiet)
219 debug("Couldn't stat remote file: %s", fx2txt(status));
220 else
221 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100222 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100223 return(NULL);
224 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000225 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100226 SSH2_FXP_ATTRS, type);
227 }
228 a = decode_attrib(&msg);
229 buffer_free(&msg);
230
231 return(a);
232}
233
Damien Miller3db5f532002-02-13 14:10:32 +1100234struct sftp_conn *
235do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100236{
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000237 u_int type;
238 int version;
Damien Miller33804262001-02-04 23:20:18 +1100239 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100240 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100241
242 buffer_init(&msg);
243 buffer_put_char(&msg, SSH2_FXP_INIT);
244 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
245 send_msg(fd_out, &msg);
246
247 buffer_clear(&msg);
248
249 get_msg(fd_in, &msg);
250
Kevin Stevesef4eea92001-02-05 12:42:17 +0000251 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100252 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000253 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100254 type);
255 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100256 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100257 }
258 version = buffer_get_int(&msg);
259
260 debug2("Remote version: %d", version);
261
262 /* Check for extensions */
263 while (buffer_len(&msg) > 0) {
264 char *name = buffer_get_string(&msg, NULL);
265 char *value = buffer_get_string(&msg, NULL);
266
267 debug2("Init extension: \"%s\"", name);
268 xfree(name);
269 xfree(value);
270 }
271
272 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100273
Damien Miller3db5f532002-02-13 14:10:32 +1100274 ret = xmalloc(sizeof(*ret));
275 ret->fd_in = fd_in;
276 ret->fd_out = fd_out;
277 ret->transfer_buflen = transfer_buflen;
278 ret->num_requests = num_requests;
279 ret->version = version;
280 ret->msg_id = 1;
281
282 /* Some filexfer v.0 servers don't support large packets */
283 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000284 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100285
286 return(ret);
287}
288
289u_int
290sftp_proto_version(struct sftp_conn *conn)
291{
292 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100293}
294
295int
Damien Miller3db5f532002-02-13 14:10:32 +1100296do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100297{
298 u_int id, status;
299 Buffer msg;
300
301 buffer_init(&msg);
302
Damien Miller3db5f532002-02-13 14:10:32 +1100303 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100304 buffer_put_char(&msg, SSH2_FXP_CLOSE);
305 buffer_put_int(&msg, id);
306 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100307 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000308 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100309
Damien Miller3db5f532002-02-13 14:10:32 +1100310 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100311 if (status != SSH2_FX_OK)
312 error("Couldn't close file: %s", fx2txt(status));
313
314 buffer_free(&msg);
315
316 return(status);
317}
318
Damien Miller4870afd2001-03-14 10:27:09 +1100319
Ben Lindstrombba81212001-06-25 05:01:22 +0000320static int
Damien Miller3db5f532002-02-13 14:10:32 +1100321do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100322 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100323{
324 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000325 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100326 char *handle;
327
Damien Miller3db5f532002-02-13 14:10:32 +1100328 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100329
330 buffer_init(&msg);
331 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
332 buffer_put_int(&msg, id);
333 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100334 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100335
336 buffer_clear(&msg);
337
Damien Miller3db5f532002-02-13 14:10:32 +1100338 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100339 if (handle == NULL)
340 return(-1);
341
Damien Miller4870afd2001-03-14 10:27:09 +1100342 if (dir) {
343 ents = 0;
344 *dir = xmalloc(sizeof(**dir));
345 (*dir)[0] = NULL;
346 }
Damien Miller4870afd2001-03-14 10:27:09 +1100347
Darren Tuckercdf547a2004-05-24 10:12:19 +1000348 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100349 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100350
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000351 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100352
353 buffer_clear(&msg);
354 buffer_put_char(&msg, SSH2_FXP_READDIR);
355 buffer_put_int(&msg, id);
356 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100357 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100358
359 buffer_clear(&msg);
360
Damien Miller3db5f532002-02-13 14:10:32 +1100361 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100362
363 type = buffer_get_char(&msg);
364 id = buffer_get_int(&msg);
365
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000366 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100367
368 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000369 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100370
371 if (type == SSH2_FXP_STATUS) {
372 int status = buffer_get_int(&msg);
373
374 debug3("Received SSH2_FXP_STATUS %d", status);
375
376 if (status == SSH2_FX_EOF) {
377 break;
378 } else {
379 error("Couldn't read directory: %s",
380 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100381 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100382 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000383 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100384 }
385 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000386 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100387 SSH2_FXP_NAME, type);
388
389 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100390 if (count == 0)
391 break;
392 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100393 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100394 char *filename, *longname;
395 Attrib *a;
396
397 filename = buffer_get_string(&msg, NULL);
398 longname = buffer_get_string(&msg, NULL);
399 a = decode_attrib(&msg);
400
Damien Miller4870afd2001-03-14 10:27:09 +1100401 if (printflag)
402 printf("%s\n", longname);
403
404 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100405 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100406 (*dir)[ents] = xmalloc(sizeof(***dir));
407 (*dir)[ents]->filename = xstrdup(filename);
408 (*dir)[ents]->longname = xstrdup(longname);
409 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
410 (*dir)[++ents] = NULL;
411 }
Damien Miller33804262001-02-04 23:20:18 +1100412
413 xfree(filename);
414 xfree(longname);
415 }
416 }
417
418 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100419 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100420 xfree(handle);
421
Darren Tuckercdf547a2004-05-24 10:12:19 +1000422 /* Don't return partial matches on interrupt */
423 if (interrupted && dir != NULL && *dir != NULL) {
424 free_sftp_dirents(*dir);
425 *dir = xmalloc(sizeof(**dir));
426 **dir = NULL;
427 }
428
Damien Miller33804262001-02-04 23:20:18 +1100429 return(0);
430}
431
432int
Damien Miller3db5f532002-02-13 14:10:32 +1100433do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100434{
Damien Miller3db5f532002-02-13 14:10:32 +1100435 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100436}
437
438void free_sftp_dirents(SFTP_DIRENT **s)
439{
440 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100441
442 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100443 xfree(s[i]->filename);
444 xfree(s[i]->longname);
445 xfree(s[i]);
446 }
447 xfree(s);
448}
449
450int
Damien Miller3db5f532002-02-13 14:10:32 +1100451do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100452{
453 u_int status, id;
454
455 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
456
Damien Miller3db5f532002-02-13 14:10:32 +1100457 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000458 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100459 strlen(path));
460 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100461 if (status != SSH2_FX_OK)
462 error("Couldn't delete file: %s", fx2txt(status));
463 return(status);
464}
465
466int
Damien Miller3db5f532002-02-13 14:10:32 +1100467do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100468{
469 u_int status, id;
470
Damien Miller3db5f532002-02-13 14:10:32 +1100471 id = conn->msg_id++;
472 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100473 strlen(path), a);
474
Damien Miller3db5f532002-02-13 14:10:32 +1100475 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100476 if (status != SSH2_FX_OK)
477 error("Couldn't create directory: %s", fx2txt(status));
478
479 return(status);
480}
481
482int
Damien Miller3db5f532002-02-13 14:10:32 +1100483do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100484{
485 u_int status, id;
486
Damien Miller3db5f532002-02-13 14:10:32 +1100487 id = conn->msg_id++;
488 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
489 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100490
Damien Miller3db5f532002-02-13 14:10:32 +1100491 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100492 if (status != SSH2_FX_OK)
493 error("Couldn't remove directory: %s", fx2txt(status));
494
495 return(status);
496}
497
498Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100499do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100500{
501 u_int id;
502
Damien Miller3db5f532002-02-13 14:10:32 +1100503 id = conn->msg_id++;
504
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000505 send_string_request(conn->fd_out, id,
506 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100507 path, strlen(path));
508
509 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100510}
511
512Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100513do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100514{
515 u_int id;
516
Damien Miller3db5f532002-02-13 14:10:32 +1100517 if (conn->version == 0) {
518 if (quiet)
519 debug("Server version does not support lstat operation");
520 else
Damien Miller996acd22003-04-09 20:59:48 +1000521 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000522 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100523 }
524
525 id = conn->msg_id++;
526 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
527 strlen(path));
528
529 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100530}
531
532Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100533do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100534{
535 u_int id;
536
Damien Miller3db5f532002-02-13 14:10:32 +1100537 id = conn->msg_id++;
538 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
539 handle_len);
540
541 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100542}
543
544int
Damien Miller3db5f532002-02-13 14:10:32 +1100545do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100546{
547 u_int status, id;
548
Damien Miller3db5f532002-02-13 14:10:32 +1100549 id = conn->msg_id++;
550 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100551 strlen(path), a);
552
Damien Miller3db5f532002-02-13 14:10:32 +1100553 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100554 if (status != SSH2_FX_OK)
555 error("Couldn't setstat on \"%s\": %s", path,
556 fx2txt(status));
557
558 return(status);
559}
560
561int
Damien Miller3db5f532002-02-13 14:10:32 +1100562do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100563 Attrib *a)
564{
565 u_int status, id;
566
Damien Miller3db5f532002-02-13 14:10:32 +1100567 id = conn->msg_id++;
568 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100569 handle_len, a);
570
Damien Miller3db5f532002-02-13 14:10:32 +1100571 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100572 if (status != SSH2_FX_OK)
573 error("Couldn't fsetstat: %s", fx2txt(status));
574
575 return(status);
576}
577
578char *
Damien Miller3db5f532002-02-13 14:10:32 +1100579do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100580{
581 Buffer msg;
582 u_int type, expected_id, count, id;
583 char *filename, *longname;
584 Attrib *a;
585
Damien Miller3db5f532002-02-13 14:10:32 +1100586 expected_id = id = conn->msg_id++;
587 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
588 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100589
590 buffer_init(&msg);
591
Damien Miller3db5f532002-02-13 14:10:32 +1100592 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100593 type = buffer_get_char(&msg);
594 id = buffer_get_int(&msg);
595
596 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000597 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100598
599 if (type == SSH2_FXP_STATUS) {
600 u_int status = buffer_get_int(&msg);
601
602 error("Couldn't canonicalise: %s", fx2txt(status));
603 return(NULL);
604 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000605 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100606 SSH2_FXP_NAME, type);
607
608 count = buffer_get_int(&msg);
609 if (count != 1)
610 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
611
612 filename = buffer_get_string(&msg, NULL);
613 longname = buffer_get_string(&msg, NULL);
614 a = decode_attrib(&msg);
615
616 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
617
618 xfree(longname);
619
620 buffer_free(&msg);
621
622 return(filename);
623}
624
625int
Damien Miller3db5f532002-02-13 14:10:32 +1100626do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100627{
628 Buffer msg;
629 u_int status, id;
630
631 buffer_init(&msg);
632
633 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100634 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100635 buffer_put_char(&msg, SSH2_FXP_RENAME);
636 buffer_put_int(&msg, id);
637 buffer_put_cstring(&msg, oldpath);
638 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100639 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100640 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
641 newpath);
642 buffer_free(&msg);
643
Damien Miller3db5f532002-02-13 14:10:32 +1100644 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100645 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100646 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
647 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100648
649 return(status);
650}
651
652int
Damien Miller3db5f532002-02-13 14:10:32 +1100653do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100654{
655 Buffer msg;
656 u_int status, id;
657
Damien Miller3db5f532002-02-13 14:10:32 +1100658 if (conn->version < 3) {
659 error("This server does not support the symlink operation");
660 return(SSH2_FX_OP_UNSUPPORTED);
661 }
662
Damien Miller058316f2001-03-08 10:08:49 +1100663 buffer_init(&msg);
664
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000665 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100666 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100667 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
668 buffer_put_int(&msg, id);
669 buffer_put_cstring(&msg, oldpath);
670 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100671 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100672 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
673 newpath);
674 buffer_free(&msg);
675
Damien Miller3db5f532002-02-13 14:10:32 +1100676 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100677 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000678 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100679 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100680
681 return(status);
682}
683
684char *
Damien Miller3db5f532002-02-13 14:10:32 +1100685do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100686{
687 Buffer msg;
688 u_int type, expected_id, count, id;
689 char *filename, *longname;
690 Attrib *a;
691
Damien Miller3db5f532002-02-13 14:10:32 +1100692 expected_id = id = conn->msg_id++;
693 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
694 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100695
696 buffer_init(&msg);
697
Damien Miller3db5f532002-02-13 14:10:32 +1100698 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100699 type = buffer_get_char(&msg);
700 id = buffer_get_int(&msg);
701
702 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000703 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100704
705 if (type == SSH2_FXP_STATUS) {
706 u_int status = buffer_get_int(&msg);
707
708 error("Couldn't readlink: %s", fx2txt(status));
709 return(NULL);
710 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000711 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100712 SSH2_FXP_NAME, type);
713
714 count = buffer_get_int(&msg);
715 if (count != 1)
716 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
717
718 filename = buffer_get_string(&msg, NULL);
719 longname = buffer_get_string(&msg, NULL);
720 a = decode_attrib(&msg);
721
722 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
723
724 xfree(longname);
725
726 buffer_free(&msg);
727
728 return(filename);
729}
730
Damien Miller16a13332002-02-13 14:03:56 +1100731static void
732send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
733 char *handle, u_int handle_len)
734{
735 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000736
Damien Miller16a13332002-02-13 14:03:56 +1100737 buffer_init(&msg);
738 buffer_clear(&msg);
739 buffer_put_char(&msg, SSH2_FXP_READ);
740 buffer_put_int(&msg, id);
741 buffer_put_string(&msg, handle, handle_len);
742 buffer_put_int64(&msg, offset);
743 buffer_put_int(&msg, len);
744 send_msg(fd_out, &msg);
745 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000746}
Damien Miller16a13332002-02-13 14:03:56 +1100747
Damien Miller058316f2001-03-08 10:08:49 +1100748int
Damien Miller3db5f532002-02-13 14:10:32 +1100749do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
750 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100751{
Damien Miller33804262001-02-04 23:20:18 +1100752 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100753 Buffer msg;
754 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000755 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100756 int read_error, write_errno;
757 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000758 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100759 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100760 struct request {
761 u_int id;
762 u_int len;
763 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000764 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100765 };
766 TAILQ_HEAD(reqhead, request) requests;
767 struct request *req;
768
769 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100770
Damien Miller3db5f532002-02-13 14:10:32 +1100771 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100772 if (a == NULL)
773 return(-1);
774
775 /* XXX: should we preserve set[ug]id? */
776 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100777 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100778 else
779 mode = 0666;
780
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000781 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100782 (!S_ISREG(a->perm))) {
783 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000784 return(-1);
785 }
786
Damien Miller16a13332002-02-13 14:03:56 +1100787 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
788 size = a->size;
789 else
790 size = 0;
791
Damien Miller3db5f532002-02-13 14:10:32 +1100792 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100793 buffer_init(&msg);
794
795 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100796 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100797 buffer_put_char(&msg, SSH2_FXP_OPEN);
798 buffer_put_int(&msg, id);
799 buffer_put_cstring(&msg, remote_path);
800 buffer_put_int(&msg, SSH2_FXF_READ);
801 attrib_clear(&junk); /* Send empty attributes */
802 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100803 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000804 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100805
Damien Miller3db5f532002-02-13 14:10:32 +1100806 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100807 if (handle == NULL) {
808 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100809 return(-1);
810 }
811
Damien Millera8e06ce2003-11-21 23:48:55 +1100812 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +1100813 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100814 if (local_fd == -1) {
815 error("Couldn't open local file \"%s\" for writing: %s",
816 local_path, strerror(errno));
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000817 buffer_free(&msg);
818 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100819 return(-1);
820 }
821
Damien Miller33804262001-02-04 23:20:18 +1100822 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100823 write_error = read_error = write_errno = num_req = offset = 0;
824 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +1100825 progress_counter = 0;
826
Damien Miller9ba30692004-03-08 23:12:02 +1100827 if (showprogress && size != 0)
828 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +1100829
Damien Miller16a13332002-02-13 14:03:56 +1100830 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100831 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100832 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100833
Darren Tuckercdf547a2004-05-24 10:12:19 +1000834 /*
Darren Tuckerfc959702004-07-17 16:12:08 +1000835 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +1000836 * allow outstanding requests to drain gracefully
837 */
838 if (interrupted) {
839 if (num_req == 0) /* If we haven't started yet... */
840 break;
841 max_req = 0;
842 }
843
Damien Miller16a13332002-02-13 14:03:56 +1100844 /* Send some more requests */
845 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000846 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000847 (unsigned long long)offset,
848 (unsigned long long)offset + buflen - 1,
849 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100850 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100851 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100852 req->len = buflen;
853 req->offset = offset;
854 offset += buflen;
855 num_req++;
856 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000857 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +1100858 req->len, handle, handle_len);
859 }
Damien Miller33804262001-02-04 23:20:18 +1100860
861 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100862 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100863 type = buffer_get_char(&msg);
864 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000865 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +1100866
Damien Miller16a13332002-02-13 14:03:56 +1100867 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +1100868 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +1100869 req != NULL && req->id != id;
870 req = TAILQ_NEXT(req, tq))
871 ;
872 if (req == NULL)
873 fatal("Unexpected reply %u", id);
874
875 switch (type) {
876 case SSH2_FXP_STATUS:
877 status = buffer_get_int(&msg);
878 if (status != SSH2_FX_EOF)
879 read_error = 1;
880 max_req = 0;
881 TAILQ_REMOVE(&requests, req, tq);
882 xfree(req);
883 num_req--;
884 break;
885 case SSH2_FXP_DATA:
886 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +0000887 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000888 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +0000889 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +1100890 if (len > req->len)
891 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +0000892 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +1100893 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +1000894 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +1100895 !write_error) {
896 write_errno = errno;
897 write_error = 1;
898 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +1100899 }
Damien Miller62d57f62003-01-10 21:43:24 +1100900 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +1100901 xfree(data);
902
903 if (len == req->len) {
904 TAILQ_REMOVE(&requests, req, tq);
905 xfree(req);
906 num_req--;
907 } else {
908 /* Resend the request for the missing data */
909 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +0000910 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000911 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +0000912 (unsigned long long)req->offset +
913 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +1100914 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100915 req->len -= len;
916 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000917 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +1100918 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100919 /* Reduce the request size */
920 if (len < buflen)
921 buflen = MAX(MIN_READ_SIZE, len);
922 }
923 if (max_req > 0) { /* max_req = 0 iff EOF received */
924 if (size > 0 && offset > size) {
925 /* Only one request at a time
926 * after the expected EOF */
927 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +0000928 (unsigned long long)offset,
929 num_req);
Damien Miller16a13332002-02-13 14:03:56 +1100930 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000931 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +1100932 ++max_req;
933 }
934 }
935 break;
936 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000937 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100938 SSH2_FXP_DATA, type);
939 }
Damien Miller33804262001-02-04 23:20:18 +1100940 }
Damien Miller33804262001-02-04 23:20:18 +1100941
Damien Miller62d57f62003-01-10 21:43:24 +1100942 if (showprogress && size)
943 stop_progress_meter();
944
Damien Miller16a13332002-02-13 14:03:56 +1100945 /* Sanity check */
946 if (TAILQ_FIRST(&requests) != NULL)
947 fatal("Transfer complete, but requests still in queue");
948
949 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000950 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +1100951 remote_path, fx2txt(status));
952 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100953 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +1100954 error("Couldn't write to \"%s\": %s", local_path,
955 strerror(write_errno));
956 status = -1;
957 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100958 } else {
Damien Miller3db5f532002-02-13 14:10:32 +1100959 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100960
961 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000962#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +1100963 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +1100964#else
Damien Miller16a13332002-02-13 14:03:56 +1100965 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000966#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +1100967 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +0000968 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100969 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
970 struct timeval tv[2];
971 tv[0].tv_sec = a->atime;
972 tv[1].tv_sec = a->mtime;
973 tv[0].tv_usec = tv[1].tv_usec = 0;
974 if (utimes(local_path, tv) == -1)
975 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +0000976 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100977 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000978 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100979 close(local_fd);
980 buffer_free(&msg);
981 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100982
983 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100984}
985
986int
Damien Miller3db5f532002-02-13 14:10:32 +1100987do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
988 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100989{
Damien Miller8829d362002-02-08 22:04:05 +1100990 int local_fd, status;
Damien Miller5873dfd2002-02-13 14:04:37 +1100991 u_int handle_len, id, type;
Damien Miller33804262001-02-04 23:20:18 +1100992 u_int64_t offset;
Damien Miller8829d362002-02-08 22:04:05 +1100993 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +1100994 Buffer msg;
995 struct stat sb;
996 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +1100997 u_int32_t startid;
998 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +1100999 struct outstanding_ack {
1000 u_int id;
1001 u_int len;
1002 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001003 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001004 };
1005 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001006 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001007
1008 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001009
1010 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1011 error("Couldn't open local file \"%s\" for reading: %s",
1012 local_path, strerror(errno));
1013 return(-1);
1014 }
1015 if (fstat(local_fd, &sb) == -1) {
1016 error("Couldn't fstat local file \"%s\": %s",
1017 local_path, strerror(errno));
1018 close(local_fd);
1019 return(-1);
1020 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001021 if (!S_ISREG(sb.st_mode)) {
1022 error("%s is not a regular file", local_path);
1023 close(local_fd);
1024 return(-1);
1025 }
Damien Miller33804262001-02-04 23:20:18 +11001026 stat_to_attrib(&sb, &a);
1027
1028 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1029 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1030 a.perm &= 0777;
1031 if (!pflag)
1032 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1033
1034 buffer_init(&msg);
1035
1036 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001037 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001038 buffer_put_char(&msg, SSH2_FXP_OPEN);
1039 buffer_put_int(&msg, id);
1040 buffer_put_cstring(&msg, remote_path);
1041 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1042 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001043 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001044 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001045
1046 buffer_clear(&msg);
1047
Damien Miller3db5f532002-02-13 14:10:32 +11001048 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001049 if (handle == NULL) {
1050 close(local_fd);
1051 buffer_free(&msg);
1052 return(-1);
1053 }
1054
Damien Miller16a13332002-02-13 14:03:56 +11001055 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001056 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001057
Damien Miller33804262001-02-04 23:20:18 +11001058 /* Read from local and write to remote */
1059 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001060 if (showprogress)
1061 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001062
Damien Miller9f0f5c62001-12-21 14:45:46 +11001063 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001064 int len;
Damien Miller33804262001-02-04 23:20:18 +11001065
1066 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001067 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001068 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001069 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001070 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001071 */
Darren Tuckercdf547a2004-05-24 10:12:19 +10001072 if (interrupted)
1073 len = 0;
1074 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001075 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001076 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1077
1078 if (len == -1)
1079 fatal("Couldn't read from \"%s\": %s", local_path,
1080 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001081
1082 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001083 ack = xmalloc(sizeof(*ack));
1084 ack->id = ++id;
1085 ack->offset = offset;
1086 ack->len = len;
1087 TAILQ_INSERT_TAIL(&acks, ack, tq);
1088
Damien Miller16a13332002-02-13 14:03:56 +11001089 buffer_clear(&msg);
1090 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001091 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001092 buffer_put_string(&msg, handle, handle_len);
1093 buffer_put_int64(&msg, offset);
1094 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001095 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001096 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001097 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001098 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001099 break;
1100
Damien Miller5873dfd2002-02-13 14:04:37 +11001101 if (ack == NULL)
1102 fatal("Unexpected ACK %u", id);
1103
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001104 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001105 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001106 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001107
Damien Miller5873dfd2002-02-13 14:04:37 +11001108 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001109 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001110 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001111 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001112
1113 if (type != SSH2_FXP_STATUS)
1114 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1115 "got %d", SSH2_FXP_STATUS, type);
1116
1117 status = buffer_get_int(&msg);
1118 debug3("SSH2_FXP_STATUS %d", status);
1119
1120 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001121 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001122 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001123 ack = TAILQ_NEXT(ack, tq))
1124 ;
1125 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001126 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001127 TAILQ_REMOVE(&acks, ack, tq);
1128
Damien Miller16a13332002-02-13 14:03:56 +11001129 if (status != SSH2_FX_OK) {
1130 error("Couldn't write to remote file \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001131 remote_path, fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +11001132 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001133 close(local_fd);
Damien Miller00111382003-03-10 11:21:17 +11001134 xfree(data);
1135 xfree(ack);
Damien Miller16a13332002-02-13 14:03:56 +11001136 goto done;
1137 }
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001138 debug3("In write loop, ack for %u %u bytes at %llu",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001139 ack->id, ack->len, (unsigned long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001140 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001141 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001142 }
Damien Miller33804262001-02-04 23:20:18 +11001143 offset += len;
1144 }
Damien Miller62d57f62003-01-10 21:43:24 +11001145 if (showprogress)
1146 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001147 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001148
1149 if (close(local_fd) == -1) {
1150 error("Couldn't close local file \"%s\": %s", local_path,
1151 strerror(errno));
Damien Miller3db5f532002-02-13 14:10:32 +11001152 do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001153 status = -1;
1154 goto done;
Damien Miller33804262001-02-04 23:20:18 +11001155 }
1156
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001157 /* Override umask and utimes if asked */
1158 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001159 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001160
Damien Miller3db5f532002-02-13 14:10:32 +11001161 status = do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001162
1163done:
1164 xfree(handle);
1165 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001166 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001167}