blob: 8b4d67b58b46c9eca8bfd2b39f412fbee2af10e1 [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 Millerf17883e2006-03-15 11:45:54 +110023
24#include <sys/types.h>
25#ifdef HAVE_SYS_STAT_H
26# include <sys/stat.h>
27#endif
Damien Miller6645e7a2006-03-15 14:42:54 +110028# include <signal.h>
Damien Miller16a13332002-02-13 14:03:56 +110029
Damien Miller9b481512002-09-12 10:43:29 +100030#include "openbsd-compat/sys-queue.h"
Damien Miller33804262001-02-04 23:20:18 +110031
Damien Miller33804262001-02-04 23:20:18 +110032#include "buffer.h"
33#include "bufaux.h"
34#include "getput.h"
35#include "xmalloc.h"
36#include "log.h"
37#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110038#include "progressmeter.h"
Damien Miller33804262001-02-04 23:20:18 +110039
40#include "sftp.h"
41#include "sftp-common.h"
42#include "sftp-client.h"
43
Darren Tuckercdf547a2004-05-24 10:12:19 +100044extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110045extern int showprogress;
46
Damien Miller0c8d8f62006-03-15 11:34:25 +110047/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110048#define MIN_READ_SIZE 512
49
Damien Miller3db5f532002-02-13 14:10:32 +110050struct sftp_conn {
51 int fd_in;
52 int fd_out;
53 u_int transfer_buflen;
54 u_int num_requests;
55 u_int version;
56 u_int msg_id;
57};
Ben Lindstrom288cc392001-02-09 02:58:04 +000058
Ben Lindstrombba81212001-06-25 05:01:22 +000059static void
Damien Miller33804262001-02-04 23:20:18 +110060send_msg(int fd, Buffer *m)
61{
Damien Millera7f3aaa2003-01-10 21:43:58 +110062 u_char mlen[4];
Damien Miller33804262001-02-04 23:20:18 +110063
Damien Miller54446182006-01-02 23:40:50 +110064 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +110065 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +110066
Damien Millera7f3aaa2003-01-10 21:43:58 +110067 /* Send length first */
68 PUT_32BIT(mlen, buffer_len(m));
Damien Millerb253cc42005-05-26 12:23:44 +100069 if (atomicio(vwrite, fd, mlen, sizeof(mlen)) != sizeof(mlen))
Damien Miller33804262001-02-04 23:20:18 +110070 fatal("Couldn't send packet: %s", strerror(errno));
71
Damien Millerb253cc42005-05-26 12:23:44 +100072 if (atomicio(vwrite, fd, buffer_ptr(m), buffer_len(m)) != buffer_len(m))
Damien Millera7f3aaa2003-01-10 21:43:58 +110073 fatal("Couldn't send packet: %s", strerror(errno));
74
75 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +110076}
77
Ben Lindstrombba81212001-06-25 05:01:22 +000078static void
Damien Miller33804262001-02-04 23:20:18 +110079get_msg(int fd, Buffer *m)
80{
Damien Millera7f3aaa2003-01-10 21:43:58 +110081 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +110082
Damien Millera7f3aaa2003-01-10 21:43:58 +110083 buffer_append_space(m, 4);
Damien Millerb253cc42005-05-26 12:23:44 +100084 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
85 if (errno == EPIPE)
86 fatal("Connection closed");
87 else
88 fatal("Couldn't read packet: %s", strerror(errno));
89 }
Damien Miller33804262001-02-04 23:20:18 +110090
Damien Millera7f3aaa2003-01-10 21:43:58 +110091 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +110092 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +000093 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +110094
Damien Millera7f3aaa2003-01-10 21:43:58 +110095 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +100096 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
97 if (errno == EPIPE)
98 fatal("Connection closed");
99 else
100 fatal("Read packet: %s", strerror(errno));
101 }
Damien Miller33804262001-02-04 23:20:18 +1100102}
103
Ben Lindstrombba81212001-06-25 05:01:22 +0000104static void
Damien Miller33804262001-02-04 23:20:18 +1100105send_string_request(int fd, u_int id, u_int code, char *s,
106 u_int len)
107{
108 Buffer msg;
109
110 buffer_init(&msg);
111 buffer_put_char(&msg, code);
112 buffer_put_int(&msg, id);
113 buffer_put_string(&msg, s, len);
114 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000115 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100116 buffer_free(&msg);
117}
118
Ben Lindstrombba81212001-06-25 05:01:22 +0000119static void
Damien Miller33804262001-02-04 23:20:18 +1100120send_string_attrs_request(int fd, u_int id, u_int code, char *s,
121 u_int len, Attrib *a)
122{
123 Buffer msg;
124
125 buffer_init(&msg);
126 buffer_put_char(&msg, code);
127 buffer_put_int(&msg, id);
128 buffer_put_string(&msg, s, len);
129 encode_attrib(&msg, a);
130 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000131 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100132 buffer_free(&msg);
133}
134
Ben Lindstrombba81212001-06-25 05:01:22 +0000135static u_int
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000136get_status(int fd, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100137{
138 Buffer msg;
139 u_int type, id, status;
140
141 buffer_init(&msg);
142 get_msg(fd, &msg);
143 type = buffer_get_char(&msg);
144 id = buffer_get_int(&msg);
145
146 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000147 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100148 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000149 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100150 SSH2_FXP_STATUS, type);
151
152 status = buffer_get_int(&msg);
153 buffer_free(&msg);
154
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000155 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100156
157 return(status);
158}
159
Ben Lindstrombba81212001-06-25 05:01:22 +0000160static char *
Damien Miller33804262001-02-04 23:20:18 +1100161get_handle(int fd, u_int expected_id, u_int *len)
162{
163 Buffer msg;
164 u_int type, id;
165 char *handle;
166
167 buffer_init(&msg);
168 get_msg(fd, &msg);
169 type = buffer_get_char(&msg);
170 id = buffer_get_int(&msg);
171
172 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000173 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100174 if (type == SSH2_FXP_STATUS) {
175 int status = buffer_get_int(&msg);
176
177 error("Couldn't get handle: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100178 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100179 return(NULL);
180 } else if (type != SSH2_FXP_HANDLE)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000181 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100182 SSH2_FXP_HANDLE, type);
183
184 handle = buffer_get_string(&msg, len);
185 buffer_free(&msg);
186
187 return(handle);
188}
189
Ben Lindstrombba81212001-06-25 05:01:22 +0000190static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000191get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100192{
193 Buffer msg;
194 u_int type, id;
195 Attrib *a;
196
197 buffer_init(&msg);
198 get_msg(fd, &msg);
199
200 type = buffer_get_char(&msg);
201 id = buffer_get_int(&msg);
202
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000203 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100204 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000205 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100206 if (type == SSH2_FXP_STATUS) {
207 int status = buffer_get_int(&msg);
208
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000209 if (quiet)
210 debug("Couldn't stat remote file: %s", fx2txt(status));
211 else
212 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100213 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100214 return(NULL);
215 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000216 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100217 SSH2_FXP_ATTRS, type);
218 }
219 a = decode_attrib(&msg);
220 buffer_free(&msg);
221
222 return(a);
223}
224
Damien Miller3db5f532002-02-13 14:10:32 +1100225struct sftp_conn *
226do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100227{
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000228 u_int type;
229 int version;
Damien Miller33804262001-02-04 23:20:18 +1100230 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100231 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100232
233 buffer_init(&msg);
234 buffer_put_char(&msg, SSH2_FXP_INIT);
235 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
236 send_msg(fd_out, &msg);
237
238 buffer_clear(&msg);
239
240 get_msg(fd_in, &msg);
241
Kevin Stevesef4eea92001-02-05 12:42:17 +0000242 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100243 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000244 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100245 type);
246 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100247 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100248 }
249 version = buffer_get_int(&msg);
250
251 debug2("Remote version: %d", version);
252
253 /* Check for extensions */
254 while (buffer_len(&msg) > 0) {
255 char *name = buffer_get_string(&msg, NULL);
256 char *value = buffer_get_string(&msg, NULL);
257
258 debug2("Init extension: \"%s\"", name);
259 xfree(name);
260 xfree(value);
261 }
262
263 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100264
Damien Miller3db5f532002-02-13 14:10:32 +1100265 ret = xmalloc(sizeof(*ret));
266 ret->fd_in = fd_in;
267 ret->fd_out = fd_out;
268 ret->transfer_buflen = transfer_buflen;
269 ret->num_requests = num_requests;
270 ret->version = version;
271 ret->msg_id = 1;
272
273 /* Some filexfer v.0 servers don't support large packets */
274 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000275 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100276
277 return(ret);
278}
279
280u_int
281sftp_proto_version(struct sftp_conn *conn)
282{
283 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100284}
285
286int
Damien Miller3db5f532002-02-13 14:10:32 +1100287do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100288{
289 u_int id, status;
290 Buffer msg;
291
292 buffer_init(&msg);
293
Damien Miller3db5f532002-02-13 14:10:32 +1100294 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100295 buffer_put_char(&msg, SSH2_FXP_CLOSE);
296 buffer_put_int(&msg, id);
297 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100298 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000299 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100300
Damien Miller3db5f532002-02-13 14:10:32 +1100301 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100302 if (status != SSH2_FX_OK)
303 error("Couldn't close file: %s", fx2txt(status));
304
305 buffer_free(&msg);
306
307 return(status);
308}
309
Damien Miller4870afd2001-03-14 10:27:09 +1100310
Ben Lindstrombba81212001-06-25 05:01:22 +0000311static int
Damien Miller3db5f532002-02-13 14:10:32 +1100312do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100313 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100314{
315 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000316 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100317 char *handle;
318
Damien Miller3db5f532002-02-13 14:10:32 +1100319 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100320
321 buffer_init(&msg);
322 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
323 buffer_put_int(&msg, id);
324 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100325 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100326
327 buffer_clear(&msg);
328
Damien Miller3db5f532002-02-13 14:10:32 +1100329 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100330 if (handle == NULL)
331 return(-1);
332
Damien Miller4870afd2001-03-14 10:27:09 +1100333 if (dir) {
334 ents = 0;
335 *dir = xmalloc(sizeof(**dir));
336 (*dir)[0] = NULL;
337 }
Damien Miller4870afd2001-03-14 10:27:09 +1100338
Darren Tuckercdf547a2004-05-24 10:12:19 +1000339 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100340 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100341
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000342 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100343
344 buffer_clear(&msg);
345 buffer_put_char(&msg, SSH2_FXP_READDIR);
346 buffer_put_int(&msg, id);
347 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100348 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100349
350 buffer_clear(&msg);
351
Damien Miller3db5f532002-02-13 14:10:32 +1100352 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100353
354 type = buffer_get_char(&msg);
355 id = buffer_get_int(&msg);
356
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000357 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100358
359 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000360 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100361
362 if (type == SSH2_FXP_STATUS) {
363 int status = buffer_get_int(&msg);
364
365 debug3("Received SSH2_FXP_STATUS %d", status);
366
367 if (status == SSH2_FX_EOF) {
368 break;
369 } else {
370 error("Couldn't read directory: %s",
371 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100372 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100373 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000374 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100375 }
376 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000377 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100378 SSH2_FXP_NAME, type);
379
380 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100381 if (count == 0)
382 break;
383 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100384 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100385 char *filename, *longname;
386 Attrib *a;
387
388 filename = buffer_get_string(&msg, NULL);
389 longname = buffer_get_string(&msg, NULL);
390 a = decode_attrib(&msg);
391
Damien Miller4870afd2001-03-14 10:27:09 +1100392 if (printflag)
393 printf("%s\n", longname);
394
395 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100396 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100397 (*dir)[ents] = xmalloc(sizeof(***dir));
398 (*dir)[ents]->filename = xstrdup(filename);
399 (*dir)[ents]->longname = xstrdup(longname);
400 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
401 (*dir)[++ents] = NULL;
402 }
Damien Miller33804262001-02-04 23:20:18 +1100403
404 xfree(filename);
405 xfree(longname);
406 }
407 }
408
409 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100410 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100411 xfree(handle);
412
Darren Tuckercdf547a2004-05-24 10:12:19 +1000413 /* Don't return partial matches on interrupt */
414 if (interrupted && dir != NULL && *dir != NULL) {
415 free_sftp_dirents(*dir);
416 *dir = xmalloc(sizeof(**dir));
417 **dir = NULL;
418 }
419
Damien Miller33804262001-02-04 23:20:18 +1100420 return(0);
421}
422
423int
Damien Miller3db5f532002-02-13 14:10:32 +1100424do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100425{
Damien Miller3db5f532002-02-13 14:10:32 +1100426 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100427}
428
429void free_sftp_dirents(SFTP_DIRENT **s)
430{
431 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100432
433 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100434 xfree(s[i]->filename);
435 xfree(s[i]->longname);
436 xfree(s[i]);
437 }
438 xfree(s);
439}
440
441int
Damien Miller3db5f532002-02-13 14:10:32 +1100442do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100443{
444 u_int status, id;
445
446 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
447
Damien Miller3db5f532002-02-13 14:10:32 +1100448 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000449 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100450 strlen(path));
451 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100452 if (status != SSH2_FX_OK)
453 error("Couldn't delete file: %s", fx2txt(status));
454 return(status);
455}
456
457int
Damien Miller3db5f532002-02-13 14:10:32 +1100458do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100459{
460 u_int status, id;
461
Damien Miller3db5f532002-02-13 14:10:32 +1100462 id = conn->msg_id++;
463 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100464 strlen(path), a);
465
Damien Miller3db5f532002-02-13 14:10:32 +1100466 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100467 if (status != SSH2_FX_OK)
468 error("Couldn't create directory: %s", fx2txt(status));
469
470 return(status);
471}
472
473int
Damien Miller3db5f532002-02-13 14:10:32 +1100474do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100475{
476 u_int status, id;
477
Damien Miller3db5f532002-02-13 14:10:32 +1100478 id = conn->msg_id++;
479 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
480 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100481
Damien Miller3db5f532002-02-13 14:10:32 +1100482 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100483 if (status != SSH2_FX_OK)
484 error("Couldn't remove directory: %s", fx2txt(status));
485
486 return(status);
487}
488
489Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100490do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100491{
492 u_int id;
493
Damien Miller3db5f532002-02-13 14:10:32 +1100494 id = conn->msg_id++;
495
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000496 send_string_request(conn->fd_out, id,
497 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100498 path, strlen(path));
499
500 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100501}
502
503Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100504do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100505{
506 u_int id;
507
Damien Miller3db5f532002-02-13 14:10:32 +1100508 if (conn->version == 0) {
509 if (quiet)
510 debug("Server version does not support lstat operation");
511 else
Damien Miller996acd22003-04-09 20:59:48 +1000512 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000513 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100514 }
515
516 id = conn->msg_id++;
517 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
518 strlen(path));
519
520 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100521}
522
523Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100524do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100525{
526 u_int id;
527
Damien Miller3db5f532002-02-13 14:10:32 +1100528 id = conn->msg_id++;
529 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
530 handle_len);
531
532 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100533}
534
535int
Damien Miller3db5f532002-02-13 14:10:32 +1100536do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100537{
538 u_int status, id;
539
Damien Miller3db5f532002-02-13 14:10:32 +1100540 id = conn->msg_id++;
541 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100542 strlen(path), a);
543
Damien Miller3db5f532002-02-13 14:10:32 +1100544 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100545 if (status != SSH2_FX_OK)
546 error("Couldn't setstat on \"%s\": %s", path,
547 fx2txt(status));
548
549 return(status);
550}
551
552int
Damien Miller3db5f532002-02-13 14:10:32 +1100553do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100554 Attrib *a)
555{
556 u_int status, id;
557
Damien Miller3db5f532002-02-13 14:10:32 +1100558 id = conn->msg_id++;
559 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100560 handle_len, a);
561
Damien Miller3db5f532002-02-13 14:10:32 +1100562 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100563 if (status != SSH2_FX_OK)
564 error("Couldn't fsetstat: %s", fx2txt(status));
565
566 return(status);
567}
568
569char *
Damien Miller3db5f532002-02-13 14:10:32 +1100570do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100571{
572 Buffer msg;
573 u_int type, expected_id, count, id;
574 char *filename, *longname;
575 Attrib *a;
576
Damien Miller3db5f532002-02-13 14:10:32 +1100577 expected_id = id = conn->msg_id++;
578 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
579 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100580
581 buffer_init(&msg);
582
Damien Miller3db5f532002-02-13 14:10:32 +1100583 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100584 type = buffer_get_char(&msg);
585 id = buffer_get_int(&msg);
586
587 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000588 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100589
590 if (type == SSH2_FXP_STATUS) {
591 u_int status = buffer_get_int(&msg);
592
593 error("Couldn't canonicalise: %s", fx2txt(status));
594 return(NULL);
595 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000596 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100597 SSH2_FXP_NAME, type);
598
599 count = buffer_get_int(&msg);
600 if (count != 1)
601 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
602
603 filename = buffer_get_string(&msg, NULL);
604 longname = buffer_get_string(&msg, NULL);
605 a = decode_attrib(&msg);
606
607 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
608
609 xfree(longname);
610
611 buffer_free(&msg);
612
613 return(filename);
614}
615
616int
Damien Miller3db5f532002-02-13 14:10:32 +1100617do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100618{
619 Buffer msg;
620 u_int status, id;
621
622 buffer_init(&msg);
623
624 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100625 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100626 buffer_put_char(&msg, SSH2_FXP_RENAME);
627 buffer_put_int(&msg, id);
628 buffer_put_cstring(&msg, oldpath);
629 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100630 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100631 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
632 newpath);
633 buffer_free(&msg);
634
Damien Miller3db5f532002-02-13 14:10:32 +1100635 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100636 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100637 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
638 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100639
640 return(status);
641}
642
643int
Damien Miller3db5f532002-02-13 14:10:32 +1100644do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100645{
646 Buffer msg;
647 u_int status, id;
648
Damien Miller3db5f532002-02-13 14:10:32 +1100649 if (conn->version < 3) {
650 error("This server does not support the symlink operation");
651 return(SSH2_FX_OP_UNSUPPORTED);
652 }
653
Damien Miller058316f2001-03-08 10:08:49 +1100654 buffer_init(&msg);
655
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000656 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100657 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100658 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
659 buffer_put_int(&msg, id);
660 buffer_put_cstring(&msg, oldpath);
661 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100662 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100663 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
664 newpath);
665 buffer_free(&msg);
666
Damien Miller3db5f532002-02-13 14:10:32 +1100667 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100668 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000669 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100670 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100671
672 return(status);
673}
674
675char *
Damien Miller3db5f532002-02-13 14:10:32 +1100676do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100677{
678 Buffer msg;
679 u_int type, expected_id, count, id;
680 char *filename, *longname;
681 Attrib *a;
682
Damien Miller3db5f532002-02-13 14:10:32 +1100683 expected_id = id = conn->msg_id++;
684 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
685 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100686
687 buffer_init(&msg);
688
Damien Miller3db5f532002-02-13 14:10:32 +1100689 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100690 type = buffer_get_char(&msg);
691 id = buffer_get_int(&msg);
692
693 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000694 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100695
696 if (type == SSH2_FXP_STATUS) {
697 u_int status = buffer_get_int(&msg);
698
699 error("Couldn't readlink: %s", fx2txt(status));
700 return(NULL);
701 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000702 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100703 SSH2_FXP_NAME, type);
704
705 count = buffer_get_int(&msg);
706 if (count != 1)
707 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
708
709 filename = buffer_get_string(&msg, NULL);
710 longname = buffer_get_string(&msg, NULL);
711 a = decode_attrib(&msg);
712
713 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
714
715 xfree(longname);
716
717 buffer_free(&msg);
718
719 return(filename);
720}
721
Damien Miller16a13332002-02-13 14:03:56 +1100722static void
723send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
724 char *handle, u_int handle_len)
725{
726 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000727
Damien Miller16a13332002-02-13 14:03:56 +1100728 buffer_init(&msg);
729 buffer_clear(&msg);
730 buffer_put_char(&msg, SSH2_FXP_READ);
731 buffer_put_int(&msg, id);
732 buffer_put_string(&msg, handle, handle_len);
733 buffer_put_int64(&msg, offset);
734 buffer_put_int(&msg, len);
735 send_msg(fd_out, &msg);
736 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000737}
Damien Miller16a13332002-02-13 14:03:56 +1100738
Damien Miller058316f2001-03-08 10:08:49 +1100739int
Damien Miller3db5f532002-02-13 14:10:32 +1100740do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
741 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100742{
Damien Miller33804262001-02-04 23:20:18 +1100743 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100744 Buffer msg;
745 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000746 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100747 int read_error, write_errno;
748 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000749 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100750 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100751 struct request {
752 u_int id;
753 u_int len;
754 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000755 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100756 };
757 TAILQ_HEAD(reqhead, request) requests;
758 struct request *req;
759
760 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100761
Damien Miller3db5f532002-02-13 14:10:32 +1100762 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100763 if (a == NULL)
764 return(-1);
765
766 /* XXX: should we preserve set[ug]id? */
767 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100768 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100769 else
770 mode = 0666;
771
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000772 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100773 (!S_ISREG(a->perm))) {
774 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000775 return(-1);
776 }
777
Damien Miller16a13332002-02-13 14:03:56 +1100778 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
779 size = a->size;
780 else
781 size = 0;
782
Damien Miller3db5f532002-02-13 14:10:32 +1100783 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100784 buffer_init(&msg);
785
786 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100787 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100788 buffer_put_char(&msg, SSH2_FXP_OPEN);
789 buffer_put_int(&msg, id);
790 buffer_put_cstring(&msg, remote_path);
791 buffer_put_int(&msg, SSH2_FXF_READ);
792 attrib_clear(&junk); /* Send empty attributes */
793 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100794 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000795 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100796
Damien Miller3db5f532002-02-13 14:10:32 +1100797 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100798 if (handle == NULL) {
799 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100800 return(-1);
801 }
802
Damien Millera8e06ce2003-11-21 23:48:55 +1100803 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +1100804 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100805 if (local_fd == -1) {
806 error("Couldn't open local file \"%s\" for writing: %s",
807 local_path, strerror(errno));
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000808 buffer_free(&msg);
809 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100810 return(-1);
811 }
812
Damien Miller33804262001-02-04 23:20:18 +1100813 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100814 write_error = read_error = write_errno = num_req = offset = 0;
815 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +1100816 progress_counter = 0;
817
Damien Miller9ba30692004-03-08 23:12:02 +1100818 if (showprogress && size != 0)
819 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +1100820
Damien Miller16a13332002-02-13 14:03:56 +1100821 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100822 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100823 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100824
Darren Tuckercdf547a2004-05-24 10:12:19 +1000825 /*
Darren Tuckerfc959702004-07-17 16:12:08 +1000826 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +1000827 * allow outstanding requests to drain gracefully
828 */
829 if (interrupted) {
830 if (num_req == 0) /* If we haven't started yet... */
831 break;
832 max_req = 0;
833 }
834
Damien Miller16a13332002-02-13 14:03:56 +1100835 /* Send some more requests */
836 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000837 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000838 (unsigned long long)offset,
839 (unsigned long long)offset + buflen - 1,
840 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100841 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100842 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100843 req->len = buflen;
844 req->offset = offset;
845 offset += buflen;
846 num_req++;
847 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000848 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +1100849 req->len, handle, handle_len);
850 }
Damien Miller33804262001-02-04 23:20:18 +1100851
852 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100853 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100854 type = buffer_get_char(&msg);
855 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000856 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +1100857
Damien Miller16a13332002-02-13 14:03:56 +1100858 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +1100859 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +1100860 req != NULL && req->id != id;
861 req = TAILQ_NEXT(req, tq))
862 ;
863 if (req == NULL)
864 fatal("Unexpected reply %u", id);
865
866 switch (type) {
867 case SSH2_FXP_STATUS:
868 status = buffer_get_int(&msg);
869 if (status != SSH2_FX_EOF)
870 read_error = 1;
871 max_req = 0;
872 TAILQ_REMOVE(&requests, req, tq);
873 xfree(req);
874 num_req--;
875 break;
876 case SSH2_FXP_DATA:
877 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +0000878 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000879 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +0000880 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +1100881 if (len > req->len)
882 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +0000883 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +1100884 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +1000885 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +1100886 !write_error) {
887 write_errno = errno;
888 write_error = 1;
889 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +1100890 }
Damien Miller62d57f62003-01-10 21:43:24 +1100891 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +1100892 xfree(data);
893
894 if (len == req->len) {
895 TAILQ_REMOVE(&requests, req, tq);
896 xfree(req);
897 num_req--;
898 } else {
899 /* Resend the request for the missing data */
900 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +0000901 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000902 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +0000903 (unsigned long long)req->offset +
904 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +1100905 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100906 req->len -= len;
907 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000908 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +1100909 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100910 /* Reduce the request size */
911 if (len < buflen)
912 buflen = MAX(MIN_READ_SIZE, len);
913 }
914 if (max_req > 0) { /* max_req = 0 iff EOF received */
915 if (size > 0 && offset > size) {
916 /* Only one request at a time
917 * after the expected EOF */
918 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +0000919 (unsigned long long)offset,
920 num_req);
Damien Miller16a13332002-02-13 14:03:56 +1100921 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000922 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +1100923 ++max_req;
924 }
925 }
926 break;
927 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000928 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100929 SSH2_FXP_DATA, type);
930 }
Damien Miller33804262001-02-04 23:20:18 +1100931 }
Damien Miller33804262001-02-04 23:20:18 +1100932
Damien Miller62d57f62003-01-10 21:43:24 +1100933 if (showprogress && size)
934 stop_progress_meter();
935
Damien Miller16a13332002-02-13 14:03:56 +1100936 /* Sanity check */
937 if (TAILQ_FIRST(&requests) != NULL)
938 fatal("Transfer complete, but requests still in queue");
939
940 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000941 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +1100942 remote_path, fx2txt(status));
943 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100944 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +1100945 error("Couldn't write to \"%s\": %s", local_path,
946 strerror(write_errno));
947 status = -1;
948 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100949 } else {
Damien Miller3db5f532002-02-13 14:10:32 +1100950 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100951
952 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000953#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +1100954 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +1100955#else
Damien Miller16a13332002-02-13 14:03:56 +1100956 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000957#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +1100958 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +0000959 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100960 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
961 struct timeval tv[2];
962 tv[0].tv_sec = a->atime;
963 tv[1].tv_sec = a->mtime;
964 tv[0].tv_usec = tv[1].tv_usec = 0;
965 if (utimes(local_path, tv) == -1)
966 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +0000967 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100968 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000969 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100970 close(local_fd);
971 buffer_free(&msg);
972 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100973
974 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100975}
976
977int
Damien Miller3db5f532002-02-13 14:10:32 +1100978do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
979 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100980{
Damien Miller8829d362002-02-08 22:04:05 +1100981 int local_fd, status;
Damien Miller5873dfd2002-02-13 14:04:37 +1100982 u_int handle_len, id, type;
Damien Miller33804262001-02-04 23:20:18 +1100983 u_int64_t offset;
Damien Miller8829d362002-02-08 22:04:05 +1100984 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +1100985 Buffer msg;
986 struct stat sb;
987 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +1100988 u_int32_t startid;
989 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +1100990 struct outstanding_ack {
991 u_int id;
992 u_int len;
993 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000994 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +1100995 };
996 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +1000997 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +1100998
999 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001000
1001 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1002 error("Couldn't open local file \"%s\" for reading: %s",
1003 local_path, strerror(errno));
1004 return(-1);
1005 }
1006 if (fstat(local_fd, &sb) == -1) {
1007 error("Couldn't fstat local file \"%s\": %s",
1008 local_path, strerror(errno));
1009 close(local_fd);
1010 return(-1);
1011 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001012 if (!S_ISREG(sb.st_mode)) {
1013 error("%s is not a regular file", local_path);
1014 close(local_fd);
1015 return(-1);
1016 }
Damien Miller33804262001-02-04 23:20:18 +11001017 stat_to_attrib(&sb, &a);
1018
1019 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1020 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1021 a.perm &= 0777;
1022 if (!pflag)
1023 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1024
1025 buffer_init(&msg);
1026
1027 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001028 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001029 buffer_put_char(&msg, SSH2_FXP_OPEN);
1030 buffer_put_int(&msg, id);
1031 buffer_put_cstring(&msg, remote_path);
1032 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1033 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001034 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001035 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001036
1037 buffer_clear(&msg);
1038
Damien Miller3db5f532002-02-13 14:10:32 +11001039 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001040 if (handle == NULL) {
1041 close(local_fd);
1042 buffer_free(&msg);
1043 return(-1);
1044 }
1045
Damien Miller16a13332002-02-13 14:03:56 +11001046 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001047 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001048
Damien Miller33804262001-02-04 23:20:18 +11001049 /* Read from local and write to remote */
1050 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001051 if (showprogress)
1052 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001053
Damien Miller9f0f5c62001-12-21 14:45:46 +11001054 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001055 int len;
Damien Miller33804262001-02-04 23:20:18 +11001056
1057 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001058 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001059 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001060 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001061 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001062 */
Darren Tuckercdf547a2004-05-24 10:12:19 +10001063 if (interrupted)
1064 len = 0;
1065 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001066 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001067 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1068
1069 if (len == -1)
1070 fatal("Couldn't read from \"%s\": %s", local_path,
1071 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001072
1073 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001074 ack = xmalloc(sizeof(*ack));
1075 ack->id = ++id;
1076 ack->offset = offset;
1077 ack->len = len;
1078 TAILQ_INSERT_TAIL(&acks, ack, tq);
1079
Damien Miller16a13332002-02-13 14:03:56 +11001080 buffer_clear(&msg);
1081 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001082 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001083 buffer_put_string(&msg, handle, handle_len);
1084 buffer_put_int64(&msg, offset);
1085 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001086 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001087 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001088 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001089 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001090 break;
1091
Damien Miller5873dfd2002-02-13 14:04:37 +11001092 if (ack == NULL)
1093 fatal("Unexpected ACK %u", id);
1094
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001095 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001096 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001097 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001098
Damien Miller5873dfd2002-02-13 14:04:37 +11001099 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001100 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001101 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001102 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001103
1104 if (type != SSH2_FXP_STATUS)
1105 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1106 "got %d", SSH2_FXP_STATUS, type);
1107
1108 status = buffer_get_int(&msg);
1109 debug3("SSH2_FXP_STATUS %d", status);
1110
1111 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001112 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001113 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001114 ack = TAILQ_NEXT(ack, tq))
1115 ;
1116 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001117 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001118 TAILQ_REMOVE(&acks, ack, tq);
1119
Damien Miller16a13332002-02-13 14:03:56 +11001120 if (status != SSH2_FX_OK) {
1121 error("Couldn't write to remote file \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001122 remote_path, fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +11001123 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001124 close(local_fd);
Damien Miller00111382003-03-10 11:21:17 +11001125 xfree(data);
1126 xfree(ack);
Damien Miller16a13332002-02-13 14:03:56 +11001127 goto done;
1128 }
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001129 debug3("In write loop, ack for %u %u bytes at %llu",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001130 ack->id, ack->len, (unsigned long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001131 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001132 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001133 }
Damien Miller33804262001-02-04 23:20:18 +11001134 offset += len;
1135 }
Damien Miller62d57f62003-01-10 21:43:24 +11001136 if (showprogress)
1137 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001138 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001139
1140 if (close(local_fd) == -1) {
1141 error("Couldn't close local file \"%s\": %s", local_path,
1142 strerror(errno));
Damien Miller3db5f532002-02-13 14:10:32 +11001143 do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001144 status = -1;
1145 goto done;
Damien Miller33804262001-02-04 23:20:18 +11001146 }
1147
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001148 /* Override umask and utimes if asked */
1149 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001150 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001151
Damien Miller3db5f532002-02-13 14:10:32 +11001152 status = do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001153
1154done:
1155 xfree(handle);
1156 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001157 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001158}