blob: 05bce3368ea812d3ce75c1999af9ec16753e8d03 [file] [log] [blame]
Damien Miller33804262001-02-04 23:20:18 +11001/*
Damien Miller4e60ed72004-02-17 17:07:59 +11002 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
Damien Miller33804262001-02-04 23:20:18 +11003 *
Damien Miller4e60ed72004-02-17 17:07:59 +11004 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
Damien Miller33804262001-02-04 23:20:18 +11007 *
Damien Miller4e60ed72004-02-17 17:07:59 +11008 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller33804262001-02-04 23:20:18 +110015 */
16
17/* XXX: memleaks */
18/* XXX: signed vs unsigned */
Damien Miller3db5f532002-02-13 14:10:32 +110019/* XXX: remove all logging, only return status codes */
Damien Miller33804262001-02-04 23:20:18 +110020/* XXX: copy between two remote sites */
21
22#include "includes.h"
Damien Miller54446182006-01-02 23:40:50 +110023RCSID("$OpenBSD: sftp-client.c,v 1.58 2006/01/02 01:20:31 djm Exp $");
Damien Miller16a13332002-02-13 14:03:56 +110024
Damien Miller9b481512002-09-12 10:43:29 +100025#include "openbsd-compat/sys-queue.h"
Damien Miller33804262001-02-04 23:20:18 +110026
Damien Miller33804262001-02-04 23:20:18 +110027#include "buffer.h"
28#include "bufaux.h"
29#include "getput.h"
30#include "xmalloc.h"
31#include "log.h"
32#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110033#include "progressmeter.h"
Damien Miller33804262001-02-04 23:20:18 +110034
35#include "sftp.h"
36#include "sftp-common.h"
37#include "sftp-client.h"
38
Darren Tuckercdf547a2004-05-24 10:12:19 +100039extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110040extern int showprogress;
41
Damien Miller16a13332002-02-13 14:03:56 +110042/* Minimum amount of data to read at at time */
43#define MIN_READ_SIZE 512
44
Damien Miller3db5f532002-02-13 14:10:32 +110045struct sftp_conn {
46 int fd_in;
47 int fd_out;
48 u_int transfer_buflen;
49 u_int num_requests;
50 u_int version;
51 u_int msg_id;
52};
Ben Lindstrom288cc392001-02-09 02:58:04 +000053
Ben Lindstrombba81212001-06-25 05:01:22 +000054static void
Damien Miller33804262001-02-04 23:20:18 +110055send_msg(int fd, Buffer *m)
56{
Damien Millera7f3aaa2003-01-10 21:43:58 +110057 u_char mlen[4];
Damien Miller33804262001-02-04 23:20:18 +110058
Damien Miller54446182006-01-02 23:40:50 +110059 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +110060 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +110061
Damien Millera7f3aaa2003-01-10 21:43:58 +110062 /* Send length first */
63 PUT_32BIT(mlen, buffer_len(m));
Damien Millerb253cc42005-05-26 12:23:44 +100064 if (atomicio(vwrite, fd, mlen, sizeof(mlen)) != sizeof(mlen))
Damien Miller33804262001-02-04 23:20:18 +110065 fatal("Couldn't send packet: %s", strerror(errno));
66
Damien Millerb253cc42005-05-26 12:23:44 +100067 if (atomicio(vwrite, fd, buffer_ptr(m), buffer_len(m)) != buffer_len(m))
Damien Millera7f3aaa2003-01-10 21:43:58 +110068 fatal("Couldn't send packet: %s", strerror(errno));
69
70 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +110071}
72
Ben Lindstrombba81212001-06-25 05:01:22 +000073static void
Damien Miller33804262001-02-04 23:20:18 +110074get_msg(int fd, Buffer *m)
75{
Damien Millera7f3aaa2003-01-10 21:43:58 +110076 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +110077
Damien Millera7f3aaa2003-01-10 21:43:58 +110078 buffer_append_space(m, 4);
Damien Millerb253cc42005-05-26 12:23:44 +100079 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
80 if (errno == EPIPE)
81 fatal("Connection closed");
82 else
83 fatal("Couldn't read packet: %s", strerror(errno));
84 }
Damien Miller33804262001-02-04 23:20:18 +110085
Damien Millera7f3aaa2003-01-10 21:43:58 +110086 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +110087 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +000088 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +110089
Damien Millera7f3aaa2003-01-10 21:43:58 +110090 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +100091 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
92 if (errno == EPIPE)
93 fatal("Connection closed");
94 else
95 fatal("Read packet: %s", strerror(errno));
96 }
Damien Miller33804262001-02-04 23:20:18 +110097}
98
Ben Lindstrombba81212001-06-25 05:01:22 +000099static void
Damien Miller33804262001-02-04 23:20:18 +1100100send_string_request(int fd, u_int id, u_int code, char *s,
101 u_int len)
102{
103 Buffer msg;
104
105 buffer_init(&msg);
106 buffer_put_char(&msg, code);
107 buffer_put_int(&msg, id);
108 buffer_put_string(&msg, s, len);
109 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000110 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100111 buffer_free(&msg);
112}
113
Ben Lindstrombba81212001-06-25 05:01:22 +0000114static void
Damien Miller33804262001-02-04 23:20:18 +1100115send_string_attrs_request(int fd, u_int id, u_int code, char *s,
116 u_int len, Attrib *a)
117{
118 Buffer msg;
119
120 buffer_init(&msg);
121 buffer_put_char(&msg, code);
122 buffer_put_int(&msg, id);
123 buffer_put_string(&msg, s, len);
124 encode_attrib(&msg, a);
125 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000126 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100127 buffer_free(&msg);
128}
129
Ben Lindstrombba81212001-06-25 05:01:22 +0000130static u_int
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000131get_status(int fd, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100132{
133 Buffer msg;
134 u_int type, id, status;
135
136 buffer_init(&msg);
137 get_msg(fd, &msg);
138 type = buffer_get_char(&msg);
139 id = buffer_get_int(&msg);
140
141 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000142 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100143 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000144 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100145 SSH2_FXP_STATUS, type);
146
147 status = buffer_get_int(&msg);
148 buffer_free(&msg);
149
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000150 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100151
152 return(status);
153}
154
Ben Lindstrombba81212001-06-25 05:01:22 +0000155static char *
Damien Miller33804262001-02-04 23:20:18 +1100156get_handle(int fd, u_int expected_id, u_int *len)
157{
158 Buffer msg;
159 u_int type, id;
160 char *handle;
161
162 buffer_init(&msg);
163 get_msg(fd, &msg);
164 type = buffer_get_char(&msg);
165 id = buffer_get_int(&msg);
166
167 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000168 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100169 if (type == SSH2_FXP_STATUS) {
170 int status = buffer_get_int(&msg);
171
172 error("Couldn't get handle: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100173 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100174 return(NULL);
175 } else if (type != SSH2_FXP_HANDLE)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000176 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100177 SSH2_FXP_HANDLE, type);
178
179 handle = buffer_get_string(&msg, len);
180 buffer_free(&msg);
181
182 return(handle);
183}
184
Ben Lindstrombba81212001-06-25 05:01:22 +0000185static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000186get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100187{
188 Buffer msg;
189 u_int type, id;
190 Attrib *a;
191
192 buffer_init(&msg);
193 get_msg(fd, &msg);
194
195 type = buffer_get_char(&msg);
196 id = buffer_get_int(&msg);
197
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000198 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100199 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000200 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100201 if (type == SSH2_FXP_STATUS) {
202 int status = buffer_get_int(&msg);
203
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000204 if (quiet)
205 debug("Couldn't stat remote file: %s", fx2txt(status));
206 else
207 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100208 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100209 return(NULL);
210 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000211 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100212 SSH2_FXP_ATTRS, type);
213 }
214 a = decode_attrib(&msg);
215 buffer_free(&msg);
216
217 return(a);
218}
219
Damien Miller3db5f532002-02-13 14:10:32 +1100220struct sftp_conn *
221do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100222{
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000223 u_int type;
224 int version;
Damien Miller33804262001-02-04 23:20:18 +1100225 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100226 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100227
228 buffer_init(&msg);
229 buffer_put_char(&msg, SSH2_FXP_INIT);
230 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
231 send_msg(fd_out, &msg);
232
233 buffer_clear(&msg);
234
235 get_msg(fd_in, &msg);
236
Kevin Stevesef4eea92001-02-05 12:42:17 +0000237 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100238 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000239 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100240 type);
241 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100242 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100243 }
244 version = buffer_get_int(&msg);
245
246 debug2("Remote version: %d", version);
247
248 /* Check for extensions */
249 while (buffer_len(&msg) > 0) {
250 char *name = buffer_get_string(&msg, NULL);
251 char *value = buffer_get_string(&msg, NULL);
252
253 debug2("Init extension: \"%s\"", name);
254 xfree(name);
255 xfree(value);
256 }
257
258 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100259
Damien Miller3db5f532002-02-13 14:10:32 +1100260 ret = xmalloc(sizeof(*ret));
261 ret->fd_in = fd_in;
262 ret->fd_out = fd_out;
263 ret->transfer_buflen = transfer_buflen;
264 ret->num_requests = num_requests;
265 ret->version = version;
266 ret->msg_id = 1;
267
268 /* Some filexfer v.0 servers don't support large packets */
269 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000270 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100271
272 return(ret);
273}
274
275u_int
276sftp_proto_version(struct sftp_conn *conn)
277{
278 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100279}
280
281int
Damien Miller3db5f532002-02-13 14:10:32 +1100282do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100283{
284 u_int id, status;
285 Buffer msg;
286
287 buffer_init(&msg);
288
Damien Miller3db5f532002-02-13 14:10:32 +1100289 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100290 buffer_put_char(&msg, SSH2_FXP_CLOSE);
291 buffer_put_int(&msg, id);
292 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100293 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000294 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100295
Damien Miller3db5f532002-02-13 14:10:32 +1100296 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100297 if (status != SSH2_FX_OK)
298 error("Couldn't close file: %s", fx2txt(status));
299
300 buffer_free(&msg);
301
302 return(status);
303}
304
Damien Miller4870afd2001-03-14 10:27:09 +1100305
Ben Lindstrombba81212001-06-25 05:01:22 +0000306static int
Damien Miller3db5f532002-02-13 14:10:32 +1100307do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100308 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100309{
310 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000311 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100312 char *handle;
313
Damien Miller3db5f532002-02-13 14:10:32 +1100314 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100315
316 buffer_init(&msg);
317 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
318 buffer_put_int(&msg, id);
319 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100320 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100321
322 buffer_clear(&msg);
323
Damien Miller3db5f532002-02-13 14:10:32 +1100324 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100325 if (handle == NULL)
326 return(-1);
327
Damien Miller4870afd2001-03-14 10:27:09 +1100328 if (dir) {
329 ents = 0;
330 *dir = xmalloc(sizeof(**dir));
331 (*dir)[0] = NULL;
332 }
Damien Miller4870afd2001-03-14 10:27:09 +1100333
Darren Tuckercdf547a2004-05-24 10:12:19 +1000334 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100335 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100336
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000337 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100338
339 buffer_clear(&msg);
340 buffer_put_char(&msg, SSH2_FXP_READDIR);
341 buffer_put_int(&msg, id);
342 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100343 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100344
345 buffer_clear(&msg);
346
Damien Miller3db5f532002-02-13 14:10:32 +1100347 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100348
349 type = buffer_get_char(&msg);
350 id = buffer_get_int(&msg);
351
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000352 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100353
354 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000355 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100356
357 if (type == SSH2_FXP_STATUS) {
358 int status = buffer_get_int(&msg);
359
360 debug3("Received SSH2_FXP_STATUS %d", status);
361
362 if (status == SSH2_FX_EOF) {
363 break;
364 } else {
365 error("Couldn't read directory: %s",
366 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100367 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100368 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000369 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100370 }
371 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000372 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100373 SSH2_FXP_NAME, type);
374
375 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100376 if (count == 0)
377 break;
378 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100379 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100380 char *filename, *longname;
381 Attrib *a;
382
383 filename = buffer_get_string(&msg, NULL);
384 longname = buffer_get_string(&msg, NULL);
385 a = decode_attrib(&msg);
386
Damien Miller4870afd2001-03-14 10:27:09 +1100387 if (printflag)
388 printf("%s\n", longname);
389
390 if (dir) {
Ben Lindstroma3700052001-04-05 23:26:32 +0000391 *dir = xrealloc(*dir, sizeof(**dir) *
Damien Miller4870afd2001-03-14 10:27:09 +1100392 (ents + 2));
393 (*dir)[ents] = xmalloc(sizeof(***dir));
394 (*dir)[ents]->filename = xstrdup(filename);
395 (*dir)[ents]->longname = xstrdup(longname);
396 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
397 (*dir)[++ents] = NULL;
398 }
Damien Miller33804262001-02-04 23:20:18 +1100399
400 xfree(filename);
401 xfree(longname);
402 }
403 }
404
405 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100406 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100407 xfree(handle);
408
Darren Tuckercdf547a2004-05-24 10:12:19 +1000409 /* Don't return partial matches on interrupt */
410 if (interrupted && dir != NULL && *dir != NULL) {
411 free_sftp_dirents(*dir);
412 *dir = xmalloc(sizeof(**dir));
413 **dir = NULL;
414 }
415
Damien Miller33804262001-02-04 23:20:18 +1100416 return(0);
417}
418
419int
Damien Miller3db5f532002-02-13 14:10:32 +1100420do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100421{
Damien Miller3db5f532002-02-13 14:10:32 +1100422 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100423}
424
425void free_sftp_dirents(SFTP_DIRENT **s)
426{
427 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100428
429 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100430 xfree(s[i]->filename);
431 xfree(s[i]->longname);
432 xfree(s[i]);
433 }
434 xfree(s);
435}
436
437int
Damien Miller3db5f532002-02-13 14:10:32 +1100438do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100439{
440 u_int status, id;
441
442 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
443
Damien Miller3db5f532002-02-13 14:10:32 +1100444 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000445 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100446 strlen(path));
447 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100448 if (status != SSH2_FX_OK)
449 error("Couldn't delete file: %s", fx2txt(status));
450 return(status);
451}
452
453int
Damien Miller3db5f532002-02-13 14:10:32 +1100454do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100455{
456 u_int status, id;
457
Damien Miller3db5f532002-02-13 14:10:32 +1100458 id = conn->msg_id++;
459 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100460 strlen(path), a);
461
Damien Miller3db5f532002-02-13 14:10:32 +1100462 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100463 if (status != SSH2_FX_OK)
464 error("Couldn't create directory: %s", fx2txt(status));
465
466 return(status);
467}
468
469int
Damien Miller3db5f532002-02-13 14:10:32 +1100470do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100471{
472 u_int status, id;
473
Damien Miller3db5f532002-02-13 14:10:32 +1100474 id = conn->msg_id++;
475 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
476 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100477
Damien Miller3db5f532002-02-13 14:10:32 +1100478 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100479 if (status != SSH2_FX_OK)
480 error("Couldn't remove directory: %s", fx2txt(status));
481
482 return(status);
483}
484
485Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100486do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100487{
488 u_int id;
489
Damien Miller3db5f532002-02-13 14:10:32 +1100490 id = conn->msg_id++;
491
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000492 send_string_request(conn->fd_out, id,
493 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100494 path, strlen(path));
495
496 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100497}
498
499Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100500do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100501{
502 u_int id;
503
Damien Miller3db5f532002-02-13 14:10:32 +1100504 if (conn->version == 0) {
505 if (quiet)
506 debug("Server version does not support lstat operation");
507 else
Damien Miller996acd22003-04-09 20:59:48 +1000508 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000509 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100510 }
511
512 id = conn->msg_id++;
513 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
514 strlen(path));
515
516 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100517}
518
519Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100520do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100521{
522 u_int id;
523
Damien Miller3db5f532002-02-13 14:10:32 +1100524 id = conn->msg_id++;
525 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
526 handle_len);
527
528 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100529}
530
531int
Damien Miller3db5f532002-02-13 14:10:32 +1100532do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100533{
534 u_int status, id;
535
Damien Miller3db5f532002-02-13 14:10:32 +1100536 id = conn->msg_id++;
537 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100538 strlen(path), a);
539
Damien Miller3db5f532002-02-13 14:10:32 +1100540 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100541 if (status != SSH2_FX_OK)
542 error("Couldn't setstat on \"%s\": %s", path,
543 fx2txt(status));
544
545 return(status);
546}
547
548int
Damien Miller3db5f532002-02-13 14:10:32 +1100549do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100550 Attrib *a)
551{
552 u_int status, id;
553
Damien Miller3db5f532002-02-13 14:10:32 +1100554 id = conn->msg_id++;
555 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100556 handle_len, a);
557
Damien Miller3db5f532002-02-13 14:10:32 +1100558 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100559 if (status != SSH2_FX_OK)
560 error("Couldn't fsetstat: %s", fx2txt(status));
561
562 return(status);
563}
564
565char *
Damien Miller3db5f532002-02-13 14:10:32 +1100566do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100567{
568 Buffer msg;
569 u_int type, expected_id, count, id;
570 char *filename, *longname;
571 Attrib *a;
572
Damien Miller3db5f532002-02-13 14:10:32 +1100573 expected_id = id = conn->msg_id++;
574 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
575 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100576
577 buffer_init(&msg);
578
Damien Miller3db5f532002-02-13 14:10:32 +1100579 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100580 type = buffer_get_char(&msg);
581 id = buffer_get_int(&msg);
582
583 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000584 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100585
586 if (type == SSH2_FXP_STATUS) {
587 u_int status = buffer_get_int(&msg);
588
589 error("Couldn't canonicalise: %s", fx2txt(status));
590 return(NULL);
591 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000592 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100593 SSH2_FXP_NAME, type);
594
595 count = buffer_get_int(&msg);
596 if (count != 1)
597 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
598
599 filename = buffer_get_string(&msg, NULL);
600 longname = buffer_get_string(&msg, NULL);
601 a = decode_attrib(&msg);
602
603 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
604
605 xfree(longname);
606
607 buffer_free(&msg);
608
609 return(filename);
610}
611
612int
Damien Miller3db5f532002-02-13 14:10:32 +1100613do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100614{
615 Buffer msg;
616 u_int status, id;
617
618 buffer_init(&msg);
619
620 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100621 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100622 buffer_put_char(&msg, SSH2_FXP_RENAME);
623 buffer_put_int(&msg, id);
624 buffer_put_cstring(&msg, oldpath);
625 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100626 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100627 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
628 newpath);
629 buffer_free(&msg);
630
Damien Miller3db5f532002-02-13 14:10:32 +1100631 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100632 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100633 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
634 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100635
636 return(status);
637}
638
639int
Damien Miller3db5f532002-02-13 14:10:32 +1100640do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100641{
642 Buffer msg;
643 u_int status, id;
644
Damien Miller3db5f532002-02-13 14:10:32 +1100645 if (conn->version < 3) {
646 error("This server does not support the symlink operation");
647 return(SSH2_FX_OP_UNSUPPORTED);
648 }
649
Damien Miller058316f2001-03-08 10:08:49 +1100650 buffer_init(&msg);
651
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000652 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100653 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100654 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
655 buffer_put_int(&msg, id);
656 buffer_put_cstring(&msg, oldpath);
657 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100658 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100659 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
660 newpath);
661 buffer_free(&msg);
662
Damien Miller3db5f532002-02-13 14:10:32 +1100663 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100664 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000665 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100666 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100667
668 return(status);
669}
670
671char *
Damien Miller3db5f532002-02-13 14:10:32 +1100672do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100673{
674 Buffer msg;
675 u_int type, expected_id, count, id;
676 char *filename, *longname;
677 Attrib *a;
678
Damien Miller3db5f532002-02-13 14:10:32 +1100679 expected_id = id = conn->msg_id++;
680 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
681 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100682
683 buffer_init(&msg);
684
Damien Miller3db5f532002-02-13 14:10:32 +1100685 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100686 type = buffer_get_char(&msg);
687 id = buffer_get_int(&msg);
688
689 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000690 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100691
692 if (type == SSH2_FXP_STATUS) {
693 u_int status = buffer_get_int(&msg);
694
695 error("Couldn't readlink: %s", fx2txt(status));
696 return(NULL);
697 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000698 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100699 SSH2_FXP_NAME, type);
700
701 count = buffer_get_int(&msg);
702 if (count != 1)
703 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
704
705 filename = buffer_get_string(&msg, NULL);
706 longname = buffer_get_string(&msg, NULL);
707 a = decode_attrib(&msg);
708
709 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
710
711 xfree(longname);
712
713 buffer_free(&msg);
714
715 return(filename);
716}
717
Damien Miller16a13332002-02-13 14:03:56 +1100718static void
719send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
720 char *handle, u_int handle_len)
721{
722 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000723
Damien Miller16a13332002-02-13 14:03:56 +1100724 buffer_init(&msg);
725 buffer_clear(&msg);
726 buffer_put_char(&msg, SSH2_FXP_READ);
727 buffer_put_int(&msg, id);
728 buffer_put_string(&msg, handle, handle_len);
729 buffer_put_int64(&msg, offset);
730 buffer_put_int(&msg, len);
731 send_msg(fd_out, &msg);
732 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000733}
Damien Miller16a13332002-02-13 14:03:56 +1100734
Damien Miller058316f2001-03-08 10:08:49 +1100735int
Damien Miller3db5f532002-02-13 14:10:32 +1100736do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
737 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100738{
Damien Miller33804262001-02-04 23:20:18 +1100739 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100740 Buffer msg;
741 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000742 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100743 int read_error, write_errno;
744 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000745 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100746 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100747 struct request {
748 u_int id;
749 u_int len;
750 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000751 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100752 };
753 TAILQ_HEAD(reqhead, request) requests;
754 struct request *req;
755
756 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100757
Damien Miller3db5f532002-02-13 14:10:32 +1100758 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100759 if (a == NULL)
760 return(-1);
761
762 /* XXX: should we preserve set[ug]id? */
763 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100764 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100765 else
766 mode = 0666;
767
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000768 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100769 (!S_ISREG(a->perm))) {
770 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000771 return(-1);
772 }
773
Damien Miller16a13332002-02-13 14:03:56 +1100774 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
775 size = a->size;
776 else
777 size = 0;
778
Damien Miller3db5f532002-02-13 14:10:32 +1100779 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100780 buffer_init(&msg);
781
782 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100783 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100784 buffer_put_char(&msg, SSH2_FXP_OPEN);
785 buffer_put_int(&msg, id);
786 buffer_put_cstring(&msg, remote_path);
787 buffer_put_int(&msg, SSH2_FXF_READ);
788 attrib_clear(&junk); /* Send empty attributes */
789 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100790 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000791 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100792
Damien Miller3db5f532002-02-13 14:10:32 +1100793 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100794 if (handle == NULL) {
795 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100796 return(-1);
797 }
798
Damien Millera8e06ce2003-11-21 23:48:55 +1100799 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +1100800 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100801 if (local_fd == -1) {
802 error("Couldn't open local file \"%s\" for writing: %s",
803 local_path, strerror(errno));
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000804 buffer_free(&msg);
805 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100806 return(-1);
807 }
808
Damien Miller33804262001-02-04 23:20:18 +1100809 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100810 write_error = read_error = write_errno = num_req = offset = 0;
811 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +1100812 progress_counter = 0;
813
Damien Miller9ba30692004-03-08 23:12:02 +1100814 if (showprogress && size != 0)
815 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +1100816
Damien Miller16a13332002-02-13 14:03:56 +1100817 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100818 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100819 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100820
Darren Tuckercdf547a2004-05-24 10:12:19 +1000821 /*
Darren Tuckerfc959702004-07-17 16:12:08 +1000822 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +1000823 * allow outstanding requests to drain gracefully
824 */
825 if (interrupted) {
826 if (num_req == 0) /* If we haven't started yet... */
827 break;
828 max_req = 0;
829 }
830
Damien Miller16a13332002-02-13 14:03:56 +1100831 /* Send some more requests */
832 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000833 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000834 (unsigned long long)offset,
835 (unsigned long long)offset + buflen - 1,
836 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100837 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100838 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100839 req->len = buflen;
840 req->offset = offset;
841 offset += buflen;
842 num_req++;
843 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000844 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +1100845 req->len, handle, handle_len);
846 }
Damien Miller33804262001-02-04 23:20:18 +1100847
848 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100849 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100850 type = buffer_get_char(&msg);
851 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000852 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +1100853
Damien Miller16a13332002-02-13 14:03:56 +1100854 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +1100855 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +1100856 req != NULL && req->id != id;
857 req = TAILQ_NEXT(req, tq))
858 ;
859 if (req == NULL)
860 fatal("Unexpected reply %u", id);
861
862 switch (type) {
863 case SSH2_FXP_STATUS:
864 status = buffer_get_int(&msg);
865 if (status != SSH2_FX_EOF)
866 read_error = 1;
867 max_req = 0;
868 TAILQ_REMOVE(&requests, req, tq);
869 xfree(req);
870 num_req--;
871 break;
872 case SSH2_FXP_DATA:
873 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +0000874 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000875 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +0000876 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +1100877 if (len > req->len)
878 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +0000879 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +1100880 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +1000881 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +1100882 !write_error) {
883 write_errno = errno;
884 write_error = 1;
885 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +1100886 }
Damien Miller62d57f62003-01-10 21:43:24 +1100887 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +1100888 xfree(data);
889
890 if (len == req->len) {
891 TAILQ_REMOVE(&requests, req, tq);
892 xfree(req);
893 num_req--;
894 } else {
895 /* Resend the request for the missing data */
896 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +0000897 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000898 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +0000899 (unsigned long long)req->offset +
900 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +1100901 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100902 req->len -= len;
903 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000904 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +1100905 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100906 /* Reduce the request size */
907 if (len < buflen)
908 buflen = MAX(MIN_READ_SIZE, len);
909 }
910 if (max_req > 0) { /* max_req = 0 iff EOF received */
911 if (size > 0 && offset > size) {
912 /* Only one request at a time
913 * after the expected EOF */
914 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +0000915 (unsigned long long)offset,
916 num_req);
Damien Miller16a13332002-02-13 14:03:56 +1100917 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000918 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +1100919 ++max_req;
920 }
921 }
922 break;
923 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000924 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100925 SSH2_FXP_DATA, type);
926 }
Damien Miller33804262001-02-04 23:20:18 +1100927 }
Damien Miller33804262001-02-04 23:20:18 +1100928
Damien Miller62d57f62003-01-10 21:43:24 +1100929 if (showprogress && size)
930 stop_progress_meter();
931
Damien Miller16a13332002-02-13 14:03:56 +1100932 /* Sanity check */
933 if (TAILQ_FIRST(&requests) != NULL)
934 fatal("Transfer complete, but requests still in queue");
935
936 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000937 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +1100938 remote_path, fx2txt(status));
939 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100940 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +1100941 error("Couldn't write to \"%s\": %s", local_path,
942 strerror(write_errno));
943 status = -1;
944 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100945 } else {
Damien Miller3db5f532002-02-13 14:10:32 +1100946 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100947
948 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000949#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +1100950 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +1100951#else
Damien Miller16a13332002-02-13 14:03:56 +1100952 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000953#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +1100954 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +0000955 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100956 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
957 struct timeval tv[2];
958 tv[0].tv_sec = a->atime;
959 tv[1].tv_sec = a->mtime;
960 tv[0].tv_usec = tv[1].tv_usec = 0;
961 if (utimes(local_path, tv) == -1)
962 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +0000963 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100964 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000965 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100966 close(local_fd);
967 buffer_free(&msg);
968 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100969
970 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100971}
972
973int
Damien Miller3db5f532002-02-13 14:10:32 +1100974do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
975 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100976{
Damien Miller8829d362002-02-08 22:04:05 +1100977 int local_fd, status;
Damien Miller5873dfd2002-02-13 14:04:37 +1100978 u_int handle_len, id, type;
Damien Miller33804262001-02-04 23:20:18 +1100979 u_int64_t offset;
Damien Miller8829d362002-02-08 22:04:05 +1100980 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +1100981 Buffer msg;
982 struct stat sb;
983 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +1100984 u_int32_t startid;
985 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +1100986 struct outstanding_ack {
987 u_int id;
988 u_int len;
989 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000990 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +1100991 };
992 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +1000993 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +1100994
995 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +1100996
997 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
998 error("Couldn't open local file \"%s\" for reading: %s",
999 local_path, strerror(errno));
1000 return(-1);
1001 }
1002 if (fstat(local_fd, &sb) == -1) {
1003 error("Couldn't fstat local file \"%s\": %s",
1004 local_path, strerror(errno));
1005 close(local_fd);
1006 return(-1);
1007 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001008 if (!S_ISREG(sb.st_mode)) {
1009 error("%s is not a regular file", local_path);
1010 close(local_fd);
1011 return(-1);
1012 }
Damien Miller33804262001-02-04 23:20:18 +11001013 stat_to_attrib(&sb, &a);
1014
1015 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1016 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1017 a.perm &= 0777;
1018 if (!pflag)
1019 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1020
1021 buffer_init(&msg);
1022
1023 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001024 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001025 buffer_put_char(&msg, SSH2_FXP_OPEN);
1026 buffer_put_int(&msg, id);
1027 buffer_put_cstring(&msg, remote_path);
1028 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1029 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001030 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001031 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001032
1033 buffer_clear(&msg);
1034
Damien Miller3db5f532002-02-13 14:10:32 +11001035 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001036 if (handle == NULL) {
1037 close(local_fd);
1038 buffer_free(&msg);
1039 return(-1);
1040 }
1041
Damien Miller16a13332002-02-13 14:03:56 +11001042 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001043 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001044
Damien Miller33804262001-02-04 23:20:18 +11001045 /* Read from local and write to remote */
1046 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001047 if (showprogress)
1048 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001049
Damien Miller9f0f5c62001-12-21 14:45:46 +11001050 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001051 int len;
Damien Miller33804262001-02-04 23:20:18 +11001052
1053 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001054 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001055 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001056 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001057 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001058 */
Darren Tuckercdf547a2004-05-24 10:12:19 +10001059 if (interrupted)
1060 len = 0;
1061 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001062 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001063 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1064
1065 if (len == -1)
1066 fatal("Couldn't read from \"%s\": %s", local_path,
1067 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001068
1069 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001070 ack = xmalloc(sizeof(*ack));
1071 ack->id = ++id;
1072 ack->offset = offset;
1073 ack->len = len;
1074 TAILQ_INSERT_TAIL(&acks, ack, tq);
1075
Damien Miller16a13332002-02-13 14:03:56 +11001076 buffer_clear(&msg);
1077 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001078 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001079 buffer_put_string(&msg, handle, handle_len);
1080 buffer_put_int64(&msg, offset);
1081 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001082 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001083 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001084 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001085 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001086 break;
1087
Damien Miller5873dfd2002-02-13 14:04:37 +11001088 if (ack == NULL)
1089 fatal("Unexpected ACK %u", id);
1090
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001091 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001092 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001093 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001094
Damien Miller5873dfd2002-02-13 14:04:37 +11001095 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001096 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001097 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001098 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001099
1100 if (type != SSH2_FXP_STATUS)
1101 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1102 "got %d", SSH2_FXP_STATUS, type);
1103
1104 status = buffer_get_int(&msg);
1105 debug3("SSH2_FXP_STATUS %d", status);
1106
1107 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001108 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001109 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001110 ack = TAILQ_NEXT(ack, tq))
1111 ;
1112 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001113 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001114 TAILQ_REMOVE(&acks, ack, tq);
1115
Damien Miller16a13332002-02-13 14:03:56 +11001116 if (status != SSH2_FX_OK) {
1117 error("Couldn't write to remote file \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001118 remote_path, fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +11001119 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001120 close(local_fd);
Damien Miller00111382003-03-10 11:21:17 +11001121 xfree(data);
1122 xfree(ack);
Damien Miller16a13332002-02-13 14:03:56 +11001123 goto done;
1124 }
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001125 debug3("In write loop, ack for %u %u bytes at %llu",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001126 ack->id, ack->len, (unsigned long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001127 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001128 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001129 }
Damien Miller33804262001-02-04 23:20:18 +11001130 offset += len;
1131 }
Damien Miller62d57f62003-01-10 21:43:24 +11001132 if (showprogress)
1133 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001134 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001135
1136 if (close(local_fd) == -1) {
1137 error("Couldn't close local file \"%s\": %s", local_path,
1138 strerror(errno));
Damien Miller3db5f532002-02-13 14:10:32 +11001139 do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001140 status = -1;
1141 goto done;
Damien Miller33804262001-02-04 23:20:18 +11001142 }
1143
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001144 /* Override umask and utimes if asked */
1145 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001146 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001147
Damien Miller3db5f532002-02-13 14:10:32 +11001148 status = do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001149
1150done:
1151 xfree(handle);
1152 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001153 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001154}