blob: 5ba4f0a9f44f8d978bcb4625fd8aeae7780f5f88 [file] [log] [blame]
Damien Millere6b3b612006-07-24 14:01:23 +10001/* $OpenBSD: sftp-client.c,v 1.68 2006/07/17 01:31:09 stevesk Exp $ */
Damien Miller33804262001-02-04 23:20:18 +11002/*
Damien Miller4e60ed72004-02-17 17:07:59 +11003 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
Damien Miller33804262001-02-04 23:20:18 +11004 *
Damien Miller4e60ed72004-02-17 17:07:59 +11005 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
Damien Miller33804262001-02-04 23:20:18 +11008 *
Damien Miller4e60ed72004-02-17 17:07:59 +11009 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller33804262001-02-04 23:20:18 +110016 */
17
18/* XXX: memleaks */
19/* XXX: signed vs unsigned */
Damien Miller3db5f532002-02-13 14:10:32 +110020/* XXX: remove all logging, only return status codes */
Damien Miller33804262001-02-04 23:20:18 +110021/* XXX: copy between two remote sites */
22
23#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110024
25#include <sys/types.h>
26#ifdef HAVE_SYS_STAT_H
27# include <sys/stat.h>
28#endif
Damien Miller57cf6382006-07-10 21:13:46 +100029
Darren Tucker39972492006-07-12 22:22:46 +100030#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100031#include <fcntl.h>
32#include <signal.h>
Damien Millere6b3b612006-07-24 14:01:23 +100033#include <unistd.h>
Damien Miller16a13332002-02-13 14:03:56 +110034
Damien Miller9b481512002-09-12 10:43:29 +100035#include "openbsd-compat/sys-queue.h"
Damien Miller33804262001-02-04 23:20:18 +110036
Damien Miller33804262001-02-04 23:20:18 +110037#include "buffer.h"
38#include "bufaux.h"
Damien Miller33804262001-02-04 23:20:18 +110039#include "xmalloc.h"
40#include "log.h"
41#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110042#include "progressmeter.h"
Damien Miller3f941882006-03-31 23:13:02 +110043#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110044
45#include "sftp.h"
46#include "sftp-common.h"
47#include "sftp-client.h"
48
Darren Tuckercdf547a2004-05-24 10:12:19 +100049extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110050extern int showprogress;
51
Damien Miller0c8d8f62006-03-15 11:34:25 +110052/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110053#define MIN_READ_SIZE 512
54
Damien Miller3db5f532002-02-13 14:10:32 +110055struct sftp_conn {
56 int fd_in;
57 int fd_out;
58 u_int transfer_buflen;
59 u_int num_requests;
60 u_int version;
61 u_int msg_id;
62};
Ben Lindstrom288cc392001-02-09 02:58:04 +000063
Ben Lindstrombba81212001-06-25 05:01:22 +000064static void
Damien Miller33804262001-02-04 23:20:18 +110065send_msg(int fd, Buffer *m)
66{
Damien Millera7f3aaa2003-01-10 21:43:58 +110067 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +100068 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +110069
Damien Miller54446182006-01-02 23:40:50 +110070 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +110071 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +110072
Damien Millera7f3aaa2003-01-10 21:43:58 +110073 /* Send length first */
Damien Miller3f941882006-03-31 23:13:02 +110074 put_u32(mlen, buffer_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +100075 iov[0].iov_base = mlen;
76 iov[0].iov_len = sizeof(mlen);
77 iov[1].iov_base = buffer_ptr(m);
78 iov[1].iov_len = buffer_len(m);
79
80 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +110081 fatal("Couldn't send packet: %s", strerror(errno));
82
83 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +110084}
85
Ben Lindstrombba81212001-06-25 05:01:22 +000086static void
Damien Miller33804262001-02-04 23:20:18 +110087get_msg(int fd, Buffer *m)
88{
Damien Millera7f3aaa2003-01-10 21:43:58 +110089 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +110090
Damien Millera7f3aaa2003-01-10 21:43:58 +110091 buffer_append_space(m, 4);
Damien Millerb253cc42005-05-26 12:23:44 +100092 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
93 if (errno == EPIPE)
94 fatal("Connection closed");
95 else
96 fatal("Couldn't read packet: %s", strerror(errno));
97 }
Damien Miller33804262001-02-04 23:20:18 +110098
Damien Millera7f3aaa2003-01-10 21:43:58 +110099 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +1100100 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000101 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100102
Damien Millera7f3aaa2003-01-10 21:43:58 +1100103 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000104 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
105 if (errno == EPIPE)
106 fatal("Connection closed");
107 else
108 fatal("Read packet: %s", strerror(errno));
109 }
Damien Miller33804262001-02-04 23:20:18 +1100110}
111
Ben Lindstrombba81212001-06-25 05:01:22 +0000112static void
Damien Miller33804262001-02-04 23:20:18 +1100113send_string_request(int fd, u_int id, u_int code, char *s,
114 u_int len)
115{
116 Buffer msg;
117
118 buffer_init(&msg);
119 buffer_put_char(&msg, code);
120 buffer_put_int(&msg, id);
121 buffer_put_string(&msg, s, len);
122 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000123 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100124 buffer_free(&msg);
125}
126
Ben Lindstrombba81212001-06-25 05:01:22 +0000127static void
Damien Miller33804262001-02-04 23:20:18 +1100128send_string_attrs_request(int fd, u_int id, u_int code, char *s,
129 u_int len, Attrib *a)
130{
131 Buffer msg;
132
133 buffer_init(&msg);
134 buffer_put_char(&msg, code);
135 buffer_put_int(&msg, id);
136 buffer_put_string(&msg, s, len);
137 encode_attrib(&msg, a);
138 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000139 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100140 buffer_free(&msg);
141}
142
Ben Lindstrombba81212001-06-25 05:01:22 +0000143static u_int
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000144get_status(int fd, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100145{
146 Buffer msg;
147 u_int type, id, status;
148
149 buffer_init(&msg);
150 get_msg(fd, &msg);
151 type = buffer_get_char(&msg);
152 id = buffer_get_int(&msg);
153
154 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000155 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100156 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000157 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100158 SSH2_FXP_STATUS, type);
159
160 status = buffer_get_int(&msg);
161 buffer_free(&msg);
162
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000163 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100164
165 return(status);
166}
167
Ben Lindstrombba81212001-06-25 05:01:22 +0000168static char *
Damien Miller33804262001-02-04 23:20:18 +1100169get_handle(int fd, u_int expected_id, u_int *len)
170{
171 Buffer msg;
172 u_int type, id;
173 char *handle;
174
175 buffer_init(&msg);
176 get_msg(fd, &msg);
177 type = buffer_get_char(&msg);
178 id = buffer_get_int(&msg);
179
180 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000181 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100182 if (type == SSH2_FXP_STATUS) {
183 int status = buffer_get_int(&msg);
184
185 error("Couldn't get handle: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100186 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100187 return(NULL);
188 } else if (type != SSH2_FXP_HANDLE)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000189 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100190 SSH2_FXP_HANDLE, type);
191
192 handle = buffer_get_string(&msg, len);
193 buffer_free(&msg);
194
195 return(handle);
196}
197
Ben Lindstrombba81212001-06-25 05:01:22 +0000198static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000199get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100200{
201 Buffer msg;
202 u_int type, id;
203 Attrib *a;
204
205 buffer_init(&msg);
206 get_msg(fd, &msg);
207
208 type = buffer_get_char(&msg);
209 id = buffer_get_int(&msg);
210
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000211 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100212 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000213 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100214 if (type == SSH2_FXP_STATUS) {
215 int status = buffer_get_int(&msg);
216
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000217 if (quiet)
218 debug("Couldn't stat remote file: %s", fx2txt(status));
219 else
220 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100221 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100222 return(NULL);
223 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000224 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100225 SSH2_FXP_ATTRS, type);
226 }
227 a = decode_attrib(&msg);
228 buffer_free(&msg);
229
230 return(a);
231}
232
Damien Miller3db5f532002-02-13 14:10:32 +1100233struct sftp_conn *
234do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100235{
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000236 u_int type;
237 int version;
Damien Miller33804262001-02-04 23:20:18 +1100238 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100239 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100240
241 buffer_init(&msg);
242 buffer_put_char(&msg, SSH2_FXP_INIT);
243 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
244 send_msg(fd_out, &msg);
245
246 buffer_clear(&msg);
247
248 get_msg(fd_in, &msg);
249
Kevin Stevesef4eea92001-02-05 12:42:17 +0000250 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100251 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000252 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100253 type);
254 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100255 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100256 }
257 version = buffer_get_int(&msg);
258
259 debug2("Remote version: %d", version);
260
261 /* Check for extensions */
262 while (buffer_len(&msg) > 0) {
263 char *name = buffer_get_string(&msg, NULL);
264 char *value = buffer_get_string(&msg, NULL);
265
266 debug2("Init extension: \"%s\"", name);
267 xfree(name);
268 xfree(value);
269 }
270
271 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100272
Damien Miller3db5f532002-02-13 14:10:32 +1100273 ret = xmalloc(sizeof(*ret));
274 ret->fd_in = fd_in;
275 ret->fd_out = fd_out;
276 ret->transfer_buflen = transfer_buflen;
277 ret->num_requests = num_requests;
278 ret->version = version;
279 ret->msg_id = 1;
280
281 /* Some filexfer v.0 servers don't support large packets */
282 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000283 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100284
285 return(ret);
286}
287
288u_int
289sftp_proto_version(struct sftp_conn *conn)
290{
291 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100292}
293
294int
Damien Miller3db5f532002-02-13 14:10:32 +1100295do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100296{
297 u_int id, status;
298 Buffer msg;
299
300 buffer_init(&msg);
301
Damien Miller3db5f532002-02-13 14:10:32 +1100302 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100303 buffer_put_char(&msg, SSH2_FXP_CLOSE);
304 buffer_put_int(&msg, id);
305 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100306 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000307 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100308
Damien Miller3db5f532002-02-13 14:10:32 +1100309 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100310 if (status != SSH2_FX_OK)
311 error("Couldn't close file: %s", fx2txt(status));
312
313 buffer_free(&msg);
314
315 return(status);
316}
317
Damien Miller4870afd2001-03-14 10:27:09 +1100318
Ben Lindstrombba81212001-06-25 05:01:22 +0000319static int
Damien Miller3db5f532002-02-13 14:10:32 +1100320do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100321 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100322{
323 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000324 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100325 char *handle;
326
Damien Miller3db5f532002-02-13 14:10:32 +1100327 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100328
329 buffer_init(&msg);
330 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
331 buffer_put_int(&msg, id);
332 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100333 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100334
335 buffer_clear(&msg);
336
Damien Miller3db5f532002-02-13 14:10:32 +1100337 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100338 if (handle == NULL)
339 return(-1);
340
Damien Miller4870afd2001-03-14 10:27:09 +1100341 if (dir) {
342 ents = 0;
343 *dir = xmalloc(sizeof(**dir));
344 (*dir)[0] = NULL;
345 }
Damien Miller4870afd2001-03-14 10:27:09 +1100346
Darren Tuckercdf547a2004-05-24 10:12:19 +1000347 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100348 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100349
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000350 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100351
352 buffer_clear(&msg);
353 buffer_put_char(&msg, SSH2_FXP_READDIR);
354 buffer_put_int(&msg, id);
355 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100356 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100357
358 buffer_clear(&msg);
359
Damien Miller3db5f532002-02-13 14:10:32 +1100360 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100361
362 type = buffer_get_char(&msg);
363 id = buffer_get_int(&msg);
364
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000365 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100366
367 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000368 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100369
370 if (type == SSH2_FXP_STATUS) {
371 int status = buffer_get_int(&msg);
372
373 debug3("Received SSH2_FXP_STATUS %d", status);
374
375 if (status == SSH2_FX_EOF) {
376 break;
377 } else {
378 error("Couldn't read directory: %s",
379 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100380 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100381 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000382 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100383 }
384 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000385 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100386 SSH2_FXP_NAME, type);
387
388 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100389 if (count == 0)
390 break;
391 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100392 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100393 char *filename, *longname;
394 Attrib *a;
395
396 filename = buffer_get_string(&msg, NULL);
397 longname = buffer_get_string(&msg, NULL);
398 a = decode_attrib(&msg);
399
Damien Miller4870afd2001-03-14 10:27:09 +1100400 if (printflag)
401 printf("%s\n", longname);
402
403 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100404 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100405 (*dir)[ents] = xmalloc(sizeof(***dir));
406 (*dir)[ents]->filename = xstrdup(filename);
407 (*dir)[ents]->longname = xstrdup(longname);
408 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
409 (*dir)[++ents] = NULL;
410 }
Damien Miller33804262001-02-04 23:20:18 +1100411
412 xfree(filename);
413 xfree(longname);
414 }
415 }
416
417 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100418 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100419 xfree(handle);
420
Darren Tuckercdf547a2004-05-24 10:12:19 +1000421 /* Don't return partial matches on interrupt */
422 if (interrupted && dir != NULL && *dir != NULL) {
423 free_sftp_dirents(*dir);
424 *dir = xmalloc(sizeof(**dir));
425 **dir = NULL;
426 }
427
Damien Miller33804262001-02-04 23:20:18 +1100428 return(0);
429}
430
431int
Damien Miller3db5f532002-02-13 14:10:32 +1100432do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100433{
Damien Miller3db5f532002-02-13 14:10:32 +1100434 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100435}
436
437void free_sftp_dirents(SFTP_DIRENT **s)
438{
439 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100440
441 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100442 xfree(s[i]->filename);
443 xfree(s[i]->longname);
444 xfree(s[i]);
445 }
446 xfree(s);
447}
448
449int
Damien Miller3db5f532002-02-13 14:10:32 +1100450do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100451{
452 u_int status, id;
453
454 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
455
Damien Miller3db5f532002-02-13 14:10:32 +1100456 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000457 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100458 strlen(path));
459 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100460 if (status != SSH2_FX_OK)
461 error("Couldn't delete file: %s", fx2txt(status));
462 return(status);
463}
464
465int
Damien Miller3db5f532002-02-13 14:10:32 +1100466do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100467{
468 u_int status, id;
469
Damien Miller3db5f532002-02-13 14:10:32 +1100470 id = conn->msg_id++;
471 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100472 strlen(path), a);
473
Damien Miller3db5f532002-02-13 14:10:32 +1100474 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100475 if (status != SSH2_FX_OK)
476 error("Couldn't create directory: %s", fx2txt(status));
477
478 return(status);
479}
480
481int
Damien Miller3db5f532002-02-13 14:10:32 +1100482do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100483{
484 u_int status, id;
485
Damien Miller3db5f532002-02-13 14:10:32 +1100486 id = conn->msg_id++;
487 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
488 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100489
Damien Miller3db5f532002-02-13 14:10:32 +1100490 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100491 if (status != SSH2_FX_OK)
492 error("Couldn't remove directory: %s", fx2txt(status));
493
494 return(status);
495}
496
497Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100498do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100499{
500 u_int id;
501
Damien Miller3db5f532002-02-13 14:10:32 +1100502 id = conn->msg_id++;
503
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000504 send_string_request(conn->fd_out, id,
505 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100506 path, strlen(path));
507
508 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100509}
510
511Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100512do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100513{
514 u_int id;
515
Damien Miller3db5f532002-02-13 14:10:32 +1100516 if (conn->version == 0) {
517 if (quiet)
518 debug("Server version does not support lstat operation");
519 else
Damien Miller996acd22003-04-09 20:59:48 +1000520 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000521 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100522 }
523
524 id = conn->msg_id++;
525 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
526 strlen(path));
527
528 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100529}
530
531Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100532do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100533{
534 u_int id;
535
Damien Miller3db5f532002-02-13 14:10:32 +1100536 id = conn->msg_id++;
537 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
538 handle_len);
539
540 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100541}
542
543int
Damien Miller3db5f532002-02-13 14:10:32 +1100544do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100545{
546 u_int status, id;
547
Damien Miller3db5f532002-02-13 14:10:32 +1100548 id = conn->msg_id++;
549 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100550 strlen(path), a);
551
Damien Miller3db5f532002-02-13 14:10:32 +1100552 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100553 if (status != SSH2_FX_OK)
554 error("Couldn't setstat on \"%s\": %s", path,
555 fx2txt(status));
556
557 return(status);
558}
559
560int
Damien Miller3db5f532002-02-13 14:10:32 +1100561do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100562 Attrib *a)
563{
564 u_int status, id;
565
Damien Miller3db5f532002-02-13 14:10:32 +1100566 id = conn->msg_id++;
567 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100568 handle_len, a);
569
Damien Miller3db5f532002-02-13 14:10:32 +1100570 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100571 if (status != SSH2_FX_OK)
572 error("Couldn't fsetstat: %s", fx2txt(status));
573
574 return(status);
575}
576
577char *
Damien Miller3db5f532002-02-13 14:10:32 +1100578do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100579{
580 Buffer msg;
581 u_int type, expected_id, count, id;
582 char *filename, *longname;
583 Attrib *a;
584
Damien Miller3db5f532002-02-13 14:10:32 +1100585 expected_id = id = conn->msg_id++;
586 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
587 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100588
589 buffer_init(&msg);
590
Damien Miller3db5f532002-02-13 14:10:32 +1100591 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100592 type = buffer_get_char(&msg);
593 id = buffer_get_int(&msg);
594
595 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000596 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100597
598 if (type == SSH2_FXP_STATUS) {
599 u_int status = buffer_get_int(&msg);
600
601 error("Couldn't canonicalise: %s", fx2txt(status));
602 return(NULL);
603 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000604 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100605 SSH2_FXP_NAME, type);
606
607 count = buffer_get_int(&msg);
608 if (count != 1)
609 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
610
611 filename = buffer_get_string(&msg, NULL);
612 longname = buffer_get_string(&msg, NULL);
613 a = decode_attrib(&msg);
614
615 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
616
617 xfree(longname);
618
619 buffer_free(&msg);
620
621 return(filename);
622}
623
624int
Damien Miller3db5f532002-02-13 14:10:32 +1100625do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100626{
627 Buffer msg;
628 u_int status, id;
629
630 buffer_init(&msg);
631
632 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100633 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100634 buffer_put_char(&msg, SSH2_FXP_RENAME);
635 buffer_put_int(&msg, id);
636 buffer_put_cstring(&msg, oldpath);
637 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100638 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100639 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
640 newpath);
641 buffer_free(&msg);
642
Damien Miller3db5f532002-02-13 14:10:32 +1100643 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100644 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100645 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
646 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100647
648 return(status);
649}
650
651int
Damien Miller3db5f532002-02-13 14:10:32 +1100652do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100653{
654 Buffer msg;
655 u_int status, id;
656
Damien Miller3db5f532002-02-13 14:10:32 +1100657 if (conn->version < 3) {
658 error("This server does not support the symlink operation");
659 return(SSH2_FX_OP_UNSUPPORTED);
660 }
661
Damien Miller058316f2001-03-08 10:08:49 +1100662 buffer_init(&msg);
663
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000664 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100665 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100666 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
667 buffer_put_int(&msg, id);
668 buffer_put_cstring(&msg, oldpath);
669 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100670 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100671 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
672 newpath);
673 buffer_free(&msg);
674
Damien Miller3db5f532002-02-13 14:10:32 +1100675 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100676 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000677 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100678 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100679
680 return(status);
681}
682
683char *
Damien Miller3db5f532002-02-13 14:10:32 +1100684do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100685{
686 Buffer msg;
687 u_int type, expected_id, count, id;
688 char *filename, *longname;
689 Attrib *a;
690
Damien Miller3db5f532002-02-13 14:10:32 +1100691 expected_id = id = conn->msg_id++;
692 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
693 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100694
695 buffer_init(&msg);
696
Damien Miller3db5f532002-02-13 14:10:32 +1100697 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100698 type = buffer_get_char(&msg);
699 id = buffer_get_int(&msg);
700
701 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000702 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100703
704 if (type == SSH2_FXP_STATUS) {
705 u_int status = buffer_get_int(&msg);
706
707 error("Couldn't readlink: %s", fx2txt(status));
708 return(NULL);
709 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000710 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100711 SSH2_FXP_NAME, type);
712
713 count = buffer_get_int(&msg);
714 if (count != 1)
715 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
716
717 filename = buffer_get_string(&msg, NULL);
718 longname = buffer_get_string(&msg, NULL);
719 a = decode_attrib(&msg);
720
721 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
722
723 xfree(longname);
724
725 buffer_free(&msg);
726
727 return(filename);
728}
729
Damien Miller16a13332002-02-13 14:03:56 +1100730static void
731send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
732 char *handle, u_int handle_len)
733{
734 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000735
Damien Miller16a13332002-02-13 14:03:56 +1100736 buffer_init(&msg);
737 buffer_clear(&msg);
738 buffer_put_char(&msg, SSH2_FXP_READ);
739 buffer_put_int(&msg, id);
740 buffer_put_string(&msg, handle, handle_len);
741 buffer_put_int64(&msg, offset);
742 buffer_put_int(&msg, len);
743 send_msg(fd_out, &msg);
744 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000745}
Damien Miller16a13332002-02-13 14:03:56 +1100746
Damien Miller058316f2001-03-08 10:08:49 +1100747int
Damien Miller3db5f532002-02-13 14:10:32 +1100748do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
749 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100750{
Damien Miller33804262001-02-04 23:20:18 +1100751 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100752 Buffer msg;
753 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000754 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100755 int read_error, write_errno;
756 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000757 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100758 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100759 struct request {
760 u_int id;
761 u_int len;
762 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000763 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100764 };
765 TAILQ_HEAD(reqhead, request) requests;
766 struct request *req;
767
768 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100769
Damien Miller3db5f532002-02-13 14:10:32 +1100770 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100771 if (a == NULL)
772 return(-1);
773
774 /* XXX: should we preserve set[ug]id? */
775 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100776 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100777 else
778 mode = 0666;
779
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000780 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100781 (!S_ISREG(a->perm))) {
782 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000783 return(-1);
784 }
785
Damien Miller16a13332002-02-13 14:03:56 +1100786 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
787 size = a->size;
788 else
789 size = 0;
790
Damien Miller3db5f532002-02-13 14:10:32 +1100791 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100792 buffer_init(&msg);
793
794 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100795 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100796 buffer_put_char(&msg, SSH2_FXP_OPEN);
797 buffer_put_int(&msg, id);
798 buffer_put_cstring(&msg, remote_path);
799 buffer_put_int(&msg, SSH2_FXF_READ);
800 attrib_clear(&junk); /* Send empty attributes */
801 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100802 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000803 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100804
Damien Miller3db5f532002-02-13 14:10:32 +1100805 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100806 if (handle == NULL) {
807 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100808 return(-1);
809 }
810
Damien Millera8e06ce2003-11-21 23:48:55 +1100811 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +1100812 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100813 if (local_fd == -1) {
814 error("Couldn't open local file \"%s\" for writing: %s",
815 local_path, strerror(errno));
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000816 buffer_free(&msg);
817 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100818 return(-1);
819 }
820
Damien Miller33804262001-02-04 23:20:18 +1100821 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100822 write_error = read_error = write_errno = num_req = offset = 0;
823 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +1100824 progress_counter = 0;
825
Damien Miller9ba30692004-03-08 23:12:02 +1100826 if (showprogress && size != 0)
827 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +1100828
Damien Miller16a13332002-02-13 14:03:56 +1100829 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100830 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100831 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100832
Darren Tuckercdf547a2004-05-24 10:12:19 +1000833 /*
Darren Tuckerfc959702004-07-17 16:12:08 +1000834 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +1000835 * allow outstanding requests to drain gracefully
836 */
837 if (interrupted) {
838 if (num_req == 0) /* If we haven't started yet... */
839 break;
840 max_req = 0;
841 }
842
Damien Miller16a13332002-02-13 14:03:56 +1100843 /* Send some more requests */
844 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000845 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000846 (unsigned long long)offset,
847 (unsigned long long)offset + buflen - 1,
848 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100849 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100850 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100851 req->len = buflen;
852 req->offset = offset;
853 offset += buflen;
854 num_req++;
855 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000856 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +1100857 req->len, handle, handle_len);
858 }
Damien Miller33804262001-02-04 23:20:18 +1100859
860 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100861 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100862 type = buffer_get_char(&msg);
863 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000864 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +1100865
Damien Miller16a13332002-02-13 14:03:56 +1100866 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +1100867 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +1100868 req != NULL && req->id != id;
869 req = TAILQ_NEXT(req, tq))
870 ;
871 if (req == NULL)
872 fatal("Unexpected reply %u", id);
873
874 switch (type) {
875 case SSH2_FXP_STATUS:
876 status = buffer_get_int(&msg);
877 if (status != SSH2_FX_EOF)
878 read_error = 1;
879 max_req = 0;
880 TAILQ_REMOVE(&requests, req, tq);
881 xfree(req);
882 num_req--;
883 break;
884 case SSH2_FXP_DATA:
885 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +0000886 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000887 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +0000888 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +1100889 if (len > req->len)
890 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +0000891 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +1100892 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +1000893 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +1100894 !write_error) {
895 write_errno = errno;
896 write_error = 1;
897 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +1100898 }
Damien Miller62d57f62003-01-10 21:43:24 +1100899 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +1100900 xfree(data);
901
902 if (len == req->len) {
903 TAILQ_REMOVE(&requests, req, tq);
904 xfree(req);
905 num_req--;
906 } else {
907 /* Resend the request for the missing data */
908 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +0000909 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000910 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +0000911 (unsigned long long)req->offset +
912 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +1100913 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100914 req->len -= len;
915 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000916 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +1100917 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100918 /* Reduce the request size */
919 if (len < buflen)
920 buflen = MAX(MIN_READ_SIZE, len);
921 }
922 if (max_req > 0) { /* max_req = 0 iff EOF received */
923 if (size > 0 && offset > size) {
924 /* Only one request at a time
925 * after the expected EOF */
926 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +0000927 (unsigned long long)offset,
928 num_req);
Damien Miller16a13332002-02-13 14:03:56 +1100929 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000930 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +1100931 ++max_req;
932 }
933 }
934 break;
935 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000936 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100937 SSH2_FXP_DATA, type);
938 }
Damien Miller33804262001-02-04 23:20:18 +1100939 }
Damien Miller33804262001-02-04 23:20:18 +1100940
Damien Miller62d57f62003-01-10 21:43:24 +1100941 if (showprogress && size)
942 stop_progress_meter();
943
Damien Miller16a13332002-02-13 14:03:56 +1100944 /* Sanity check */
945 if (TAILQ_FIRST(&requests) != NULL)
946 fatal("Transfer complete, but requests still in queue");
947
948 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000949 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +1100950 remote_path, fx2txt(status));
951 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100952 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +1100953 error("Couldn't write to \"%s\": %s", local_path,
954 strerror(write_errno));
955 status = -1;
956 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100957 } else {
Damien Miller3db5f532002-02-13 14:10:32 +1100958 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100959
960 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000961#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +1100962 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +1100963#else
Damien Miller16a13332002-02-13 14:03:56 +1100964 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000965#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +1100966 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +0000967 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100968 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
969 struct timeval tv[2];
970 tv[0].tv_sec = a->atime;
971 tv[1].tv_sec = a->mtime;
972 tv[0].tv_usec = tv[1].tv_usec = 0;
973 if (utimes(local_path, tv) == -1)
974 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +0000975 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100976 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000977 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100978 close(local_fd);
979 buffer_free(&msg);
980 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100981
982 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100983}
984
985int
Damien Miller3db5f532002-02-13 14:10:32 +1100986do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
987 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100988{
Damien Miller8829d362002-02-08 22:04:05 +1100989 int local_fd, status;
Damien Miller5873dfd2002-02-13 14:04:37 +1100990 u_int handle_len, id, type;
Damien Miller33804262001-02-04 23:20:18 +1100991 u_int64_t offset;
Damien Miller8829d362002-02-08 22:04:05 +1100992 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +1100993 Buffer msg;
994 struct stat sb;
995 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +1100996 u_int32_t startid;
997 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +1100998 struct outstanding_ack {
999 u_int id;
1000 u_int len;
1001 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001002 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001003 };
1004 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001005 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001006
1007 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001008
1009 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1010 error("Couldn't open local file \"%s\" for reading: %s",
1011 local_path, strerror(errno));
1012 return(-1);
1013 }
1014 if (fstat(local_fd, &sb) == -1) {
1015 error("Couldn't fstat local file \"%s\": %s",
1016 local_path, strerror(errno));
1017 close(local_fd);
1018 return(-1);
1019 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001020 if (!S_ISREG(sb.st_mode)) {
1021 error("%s is not a regular file", local_path);
1022 close(local_fd);
1023 return(-1);
1024 }
Damien Miller33804262001-02-04 23:20:18 +11001025 stat_to_attrib(&sb, &a);
1026
1027 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1028 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1029 a.perm &= 0777;
1030 if (!pflag)
1031 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1032
1033 buffer_init(&msg);
1034
1035 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001036 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001037 buffer_put_char(&msg, SSH2_FXP_OPEN);
1038 buffer_put_int(&msg, id);
1039 buffer_put_cstring(&msg, remote_path);
1040 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1041 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001042 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001043 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001044
1045 buffer_clear(&msg);
1046
Damien Miller3db5f532002-02-13 14:10:32 +11001047 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001048 if (handle == NULL) {
1049 close(local_fd);
1050 buffer_free(&msg);
1051 return(-1);
1052 }
1053
Damien Miller16a13332002-02-13 14:03:56 +11001054 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001055 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001056
Damien Miller33804262001-02-04 23:20:18 +11001057 /* Read from local and write to remote */
1058 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001059 if (showprogress)
1060 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001061
Damien Miller9f0f5c62001-12-21 14:45:46 +11001062 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001063 int len;
Damien Miller33804262001-02-04 23:20:18 +11001064
1065 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001066 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001067 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001068 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001069 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001070 */
Darren Tuckercdf547a2004-05-24 10:12:19 +10001071 if (interrupted)
1072 len = 0;
1073 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001074 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001075 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1076
1077 if (len == -1)
1078 fatal("Couldn't read from \"%s\": %s", local_path,
1079 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001080
1081 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001082 ack = xmalloc(sizeof(*ack));
1083 ack->id = ++id;
1084 ack->offset = offset;
1085 ack->len = len;
1086 TAILQ_INSERT_TAIL(&acks, ack, tq);
1087
Damien Miller16a13332002-02-13 14:03:56 +11001088 buffer_clear(&msg);
1089 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001090 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001091 buffer_put_string(&msg, handle, handle_len);
1092 buffer_put_int64(&msg, offset);
1093 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001094 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001095 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001096 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001097 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001098 break;
1099
Damien Miller5873dfd2002-02-13 14:04:37 +11001100 if (ack == NULL)
1101 fatal("Unexpected ACK %u", id);
1102
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001103 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001104 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001105 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001106
Damien Miller5873dfd2002-02-13 14:04:37 +11001107 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001108 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001109 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001110 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001111
1112 if (type != SSH2_FXP_STATUS)
1113 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1114 "got %d", SSH2_FXP_STATUS, type);
1115
1116 status = buffer_get_int(&msg);
1117 debug3("SSH2_FXP_STATUS %d", status);
1118
1119 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001120 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001121 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001122 ack = TAILQ_NEXT(ack, tq))
1123 ;
1124 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001125 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001126 TAILQ_REMOVE(&acks, ack, tq);
1127
Damien Miller16a13332002-02-13 14:03:56 +11001128 if (status != SSH2_FX_OK) {
1129 error("Couldn't write to remote file \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001130 remote_path, fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +11001131 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001132 close(local_fd);
Damien Miller00111382003-03-10 11:21:17 +11001133 xfree(data);
1134 xfree(ack);
Damien Miller16a13332002-02-13 14:03:56 +11001135 goto done;
1136 }
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001137 debug3("In write loop, ack for %u %u bytes at %llu",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001138 ack->id, ack->len, (unsigned long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001139 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001140 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001141 }
Damien Miller33804262001-02-04 23:20:18 +11001142 offset += len;
1143 }
Damien Miller62d57f62003-01-10 21:43:24 +11001144 if (showprogress)
1145 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001146 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001147
1148 if (close(local_fd) == -1) {
1149 error("Couldn't close local file \"%s\": %s", local_path,
1150 strerror(errno));
Damien Miller3db5f532002-02-13 14:10:32 +11001151 do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001152 status = -1;
1153 goto done;
Damien Miller33804262001-02-04 23:20:18 +11001154 }
1155
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001156 /* Override umask and utimes if asked */
1157 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001158 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001159
Damien Miller3db5f532002-02-13 14:10:32 +11001160 status = do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001161
1162done:
1163 xfree(handle);
1164 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001165 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001166}