blob: 4416afa4c2e1dc0a6ce82f0c288f4d33e35d7b5a [file] [log] [blame]
Damien Miller8dbffe72006-08-05 11:02:17 +10001/* $OpenBSD: sftp-client.c,v 1.72 2006/07/26 02:35:17 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>
Damien Miller8dbffe72006-08-05 11:02:17 +100026#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110027#ifdef HAVE_SYS_STAT_H
28# include <sys/stat.h>
29#endif
Damien Miller9aec9192006-08-05 10:57:45 +100030#ifdef HAVE_SYS_TIME_H
31# include <sys/time.h>
32#endif
Damien Miller57cf6382006-07-10 21:13:46 +100033
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100035#include <fcntl.h>
36#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100037#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100038#include <unistd.h>
Damien Miller16a13332002-02-13 14:03:56 +110039
Damien Miller9b481512002-09-12 10:43:29 +100040#include "openbsd-compat/sys-queue.h"
Damien Miller33804262001-02-04 23:20:18 +110041
Damien Miller33804262001-02-04 23:20:18 +110042#include "buffer.h"
43#include "bufaux.h"
Damien Miller33804262001-02-04 23:20:18 +110044#include "xmalloc.h"
45#include "log.h"
46#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110047#include "progressmeter.h"
Damien Miller3f941882006-03-31 23:13:02 +110048#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110049
50#include "sftp.h"
51#include "sftp-common.h"
52#include "sftp-client.h"
53
Darren Tuckercdf547a2004-05-24 10:12:19 +100054extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110055extern int showprogress;
56
Damien Miller0c8d8f62006-03-15 11:34:25 +110057/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110058#define MIN_READ_SIZE 512
59
Damien Miller3db5f532002-02-13 14:10:32 +110060struct sftp_conn {
61 int fd_in;
62 int fd_out;
63 u_int transfer_buflen;
64 u_int num_requests;
65 u_int version;
66 u_int msg_id;
67};
Ben Lindstrom288cc392001-02-09 02:58:04 +000068
Ben Lindstrombba81212001-06-25 05:01:22 +000069static void
Damien Miller33804262001-02-04 23:20:18 +110070send_msg(int fd, Buffer *m)
71{
Damien Millera7f3aaa2003-01-10 21:43:58 +110072 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +100073 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +110074
Damien Miller54446182006-01-02 23:40:50 +110075 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +110076 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +110077
Damien Millera7f3aaa2003-01-10 21:43:58 +110078 /* Send length first */
Damien Miller3f941882006-03-31 23:13:02 +110079 put_u32(mlen, buffer_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +100080 iov[0].iov_base = mlen;
81 iov[0].iov_len = sizeof(mlen);
82 iov[1].iov_base = buffer_ptr(m);
83 iov[1].iov_len = buffer_len(m);
84
85 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +110086 fatal("Couldn't send packet: %s", strerror(errno));
87
88 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +110089}
90
Ben Lindstrombba81212001-06-25 05:01:22 +000091static void
Damien Miller33804262001-02-04 23:20:18 +110092get_msg(int fd, Buffer *m)
93{
Damien Millera7f3aaa2003-01-10 21:43:58 +110094 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +110095
Damien Millera7f3aaa2003-01-10 21:43:58 +110096 buffer_append_space(m, 4);
Damien Millerb253cc42005-05-26 12:23:44 +100097 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
98 if (errno == EPIPE)
99 fatal("Connection closed");
100 else
101 fatal("Couldn't read packet: %s", strerror(errno));
102 }
Damien Miller33804262001-02-04 23:20:18 +1100103
Damien Millera7f3aaa2003-01-10 21:43:58 +1100104 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +1100105 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000106 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100107
Damien Millera7f3aaa2003-01-10 21:43:58 +1100108 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000109 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
110 if (errno == EPIPE)
111 fatal("Connection closed");
112 else
113 fatal("Read packet: %s", strerror(errno));
114 }
Damien Miller33804262001-02-04 23:20:18 +1100115}
116
Ben Lindstrombba81212001-06-25 05:01:22 +0000117static void
Damien Miller33804262001-02-04 23:20:18 +1100118send_string_request(int fd, u_int id, u_int code, char *s,
119 u_int len)
120{
121 Buffer msg;
122
123 buffer_init(&msg);
124 buffer_put_char(&msg, code);
125 buffer_put_int(&msg, id);
126 buffer_put_string(&msg, s, len);
127 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000128 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100129 buffer_free(&msg);
130}
131
Ben Lindstrombba81212001-06-25 05:01:22 +0000132static void
Damien Miller33804262001-02-04 23:20:18 +1100133send_string_attrs_request(int fd, u_int id, u_int code, char *s,
134 u_int len, Attrib *a)
135{
136 Buffer msg;
137
138 buffer_init(&msg);
139 buffer_put_char(&msg, code);
140 buffer_put_int(&msg, id);
141 buffer_put_string(&msg, s, len);
142 encode_attrib(&msg, a);
143 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000144 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100145 buffer_free(&msg);
146}
147
Ben Lindstrombba81212001-06-25 05:01:22 +0000148static u_int
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000149get_status(int fd, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100150{
151 Buffer msg;
152 u_int type, id, status;
153
154 buffer_init(&msg);
155 get_msg(fd, &msg);
156 type = buffer_get_char(&msg);
157 id = buffer_get_int(&msg);
158
159 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000160 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100161 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000162 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100163 SSH2_FXP_STATUS, type);
164
165 status = buffer_get_int(&msg);
166 buffer_free(&msg);
167
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000168 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100169
170 return(status);
171}
172
Ben Lindstrombba81212001-06-25 05:01:22 +0000173static char *
Damien Miller33804262001-02-04 23:20:18 +1100174get_handle(int fd, u_int expected_id, u_int *len)
175{
176 Buffer msg;
177 u_int type, id;
178 char *handle;
179
180 buffer_init(&msg);
181 get_msg(fd, &msg);
182 type = buffer_get_char(&msg);
183 id = buffer_get_int(&msg);
184
185 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000186 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100187 if (type == SSH2_FXP_STATUS) {
188 int status = buffer_get_int(&msg);
189
190 error("Couldn't get handle: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100191 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100192 return(NULL);
193 } else if (type != SSH2_FXP_HANDLE)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000194 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100195 SSH2_FXP_HANDLE, type);
196
197 handle = buffer_get_string(&msg, len);
198 buffer_free(&msg);
199
200 return(handle);
201}
202
Ben Lindstrombba81212001-06-25 05:01:22 +0000203static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000204get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100205{
206 Buffer msg;
207 u_int type, id;
208 Attrib *a;
209
210 buffer_init(&msg);
211 get_msg(fd, &msg);
212
213 type = buffer_get_char(&msg);
214 id = buffer_get_int(&msg);
215
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000216 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100217 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000218 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100219 if (type == SSH2_FXP_STATUS) {
220 int status = buffer_get_int(&msg);
221
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000222 if (quiet)
223 debug("Couldn't stat remote file: %s", fx2txt(status));
224 else
225 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100226 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100227 return(NULL);
228 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000229 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100230 SSH2_FXP_ATTRS, type);
231 }
232 a = decode_attrib(&msg);
233 buffer_free(&msg);
234
235 return(a);
236}
237
Damien Miller3db5f532002-02-13 14:10:32 +1100238struct sftp_conn *
239do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100240{
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000241 u_int type;
242 int version;
Damien Miller33804262001-02-04 23:20:18 +1100243 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100244 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100245
246 buffer_init(&msg);
247 buffer_put_char(&msg, SSH2_FXP_INIT);
248 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
249 send_msg(fd_out, &msg);
250
251 buffer_clear(&msg);
252
253 get_msg(fd_in, &msg);
254
Kevin Stevesef4eea92001-02-05 12:42:17 +0000255 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100256 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000257 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100258 type);
259 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100260 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100261 }
262 version = buffer_get_int(&msg);
263
264 debug2("Remote version: %d", version);
265
266 /* Check for extensions */
267 while (buffer_len(&msg) > 0) {
268 char *name = buffer_get_string(&msg, NULL);
269 char *value = buffer_get_string(&msg, NULL);
270
271 debug2("Init extension: \"%s\"", name);
272 xfree(name);
273 xfree(value);
274 }
275
276 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100277
Damien Miller3db5f532002-02-13 14:10:32 +1100278 ret = xmalloc(sizeof(*ret));
279 ret->fd_in = fd_in;
280 ret->fd_out = fd_out;
281 ret->transfer_buflen = transfer_buflen;
282 ret->num_requests = num_requests;
283 ret->version = version;
284 ret->msg_id = 1;
285
286 /* Some filexfer v.0 servers don't support large packets */
287 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000288 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100289
290 return(ret);
291}
292
293u_int
294sftp_proto_version(struct sftp_conn *conn)
295{
296 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100297}
298
299int
Damien Miller3db5f532002-02-13 14:10:32 +1100300do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100301{
302 u_int id, status;
303 Buffer msg;
304
305 buffer_init(&msg);
306
Damien Miller3db5f532002-02-13 14:10:32 +1100307 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100308 buffer_put_char(&msg, SSH2_FXP_CLOSE);
309 buffer_put_int(&msg, id);
310 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100311 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000312 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100313
Damien Miller3db5f532002-02-13 14:10:32 +1100314 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100315 if (status != SSH2_FX_OK)
316 error("Couldn't close file: %s", fx2txt(status));
317
318 buffer_free(&msg);
319
320 return(status);
321}
322
Damien Miller4870afd2001-03-14 10:27:09 +1100323
Ben Lindstrombba81212001-06-25 05:01:22 +0000324static int
Damien Miller3db5f532002-02-13 14:10:32 +1100325do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100326 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100327{
328 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000329 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100330 char *handle;
331
Damien Miller3db5f532002-02-13 14:10:32 +1100332 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100333
334 buffer_init(&msg);
335 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
336 buffer_put_int(&msg, id);
337 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100338 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100339
340 buffer_clear(&msg);
341
Damien Miller3db5f532002-02-13 14:10:32 +1100342 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100343 if (handle == NULL)
344 return(-1);
345
Damien Miller4870afd2001-03-14 10:27:09 +1100346 if (dir) {
347 ents = 0;
348 *dir = xmalloc(sizeof(**dir));
349 (*dir)[0] = NULL;
350 }
Damien Miller4870afd2001-03-14 10:27:09 +1100351
Darren Tuckercdf547a2004-05-24 10:12:19 +1000352 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100353 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100354
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000355 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100356
357 buffer_clear(&msg);
358 buffer_put_char(&msg, SSH2_FXP_READDIR);
359 buffer_put_int(&msg, id);
360 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100361 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100362
363 buffer_clear(&msg);
364
Damien Miller3db5f532002-02-13 14:10:32 +1100365 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100366
367 type = buffer_get_char(&msg);
368 id = buffer_get_int(&msg);
369
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000370 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100371
372 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000373 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100374
375 if (type == SSH2_FXP_STATUS) {
376 int status = buffer_get_int(&msg);
377
378 debug3("Received SSH2_FXP_STATUS %d", status);
379
380 if (status == SSH2_FX_EOF) {
381 break;
382 } else {
383 error("Couldn't read directory: %s",
384 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100385 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100386 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000387 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100388 }
389 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000390 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100391 SSH2_FXP_NAME, type);
392
393 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100394 if (count == 0)
395 break;
396 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100397 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100398 char *filename, *longname;
399 Attrib *a;
400
401 filename = buffer_get_string(&msg, NULL);
402 longname = buffer_get_string(&msg, NULL);
403 a = decode_attrib(&msg);
404
Damien Miller4870afd2001-03-14 10:27:09 +1100405 if (printflag)
406 printf("%s\n", longname);
407
408 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100409 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100410 (*dir)[ents] = xmalloc(sizeof(***dir));
411 (*dir)[ents]->filename = xstrdup(filename);
412 (*dir)[ents]->longname = xstrdup(longname);
413 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
414 (*dir)[++ents] = NULL;
415 }
Damien Miller33804262001-02-04 23:20:18 +1100416
417 xfree(filename);
418 xfree(longname);
419 }
420 }
421
422 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100423 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100424 xfree(handle);
425
Darren Tuckercdf547a2004-05-24 10:12:19 +1000426 /* Don't return partial matches on interrupt */
427 if (interrupted && dir != NULL && *dir != NULL) {
428 free_sftp_dirents(*dir);
429 *dir = xmalloc(sizeof(**dir));
430 **dir = NULL;
431 }
432
Damien Miller33804262001-02-04 23:20:18 +1100433 return(0);
434}
435
436int
Damien Miller3db5f532002-02-13 14:10:32 +1100437do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100438{
Damien Miller3db5f532002-02-13 14:10:32 +1100439 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100440}
441
442void free_sftp_dirents(SFTP_DIRENT **s)
443{
444 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100445
446 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100447 xfree(s[i]->filename);
448 xfree(s[i]->longname);
449 xfree(s[i]);
450 }
451 xfree(s);
452}
453
454int
Damien Miller3db5f532002-02-13 14:10:32 +1100455do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100456{
457 u_int status, id;
458
459 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
460
Damien Miller3db5f532002-02-13 14:10:32 +1100461 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000462 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100463 strlen(path));
464 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100465 if (status != SSH2_FX_OK)
466 error("Couldn't delete file: %s", fx2txt(status));
467 return(status);
468}
469
470int
Damien Miller3db5f532002-02-13 14:10:32 +1100471do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100472{
473 u_int status, id;
474
Damien Miller3db5f532002-02-13 14:10:32 +1100475 id = conn->msg_id++;
476 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100477 strlen(path), a);
478
Damien Miller3db5f532002-02-13 14:10:32 +1100479 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100480 if (status != SSH2_FX_OK)
481 error("Couldn't create directory: %s", fx2txt(status));
482
483 return(status);
484}
485
486int
Damien Miller3db5f532002-02-13 14:10:32 +1100487do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100488{
489 u_int status, id;
490
Damien Miller3db5f532002-02-13 14:10:32 +1100491 id = conn->msg_id++;
492 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
493 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100494
Damien Miller3db5f532002-02-13 14:10:32 +1100495 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100496 if (status != SSH2_FX_OK)
497 error("Couldn't remove directory: %s", fx2txt(status));
498
499 return(status);
500}
501
502Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100503do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100504{
505 u_int id;
506
Damien Miller3db5f532002-02-13 14:10:32 +1100507 id = conn->msg_id++;
508
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000509 send_string_request(conn->fd_out, id,
510 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100511 path, strlen(path));
512
513 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100514}
515
516Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100517do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100518{
519 u_int id;
520
Damien Miller3db5f532002-02-13 14:10:32 +1100521 if (conn->version == 0) {
522 if (quiet)
523 debug("Server version does not support lstat operation");
524 else
Damien Miller996acd22003-04-09 20:59:48 +1000525 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000526 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100527 }
528
529 id = conn->msg_id++;
530 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
531 strlen(path));
532
533 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100534}
535
536Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100537do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100538{
539 u_int id;
540
Damien Miller3db5f532002-02-13 14:10:32 +1100541 id = conn->msg_id++;
542 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
543 handle_len);
544
545 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100546}
547
548int
Damien Miller3db5f532002-02-13 14:10:32 +1100549do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100550{
551 u_int status, id;
552
Damien Miller3db5f532002-02-13 14:10:32 +1100553 id = conn->msg_id++;
554 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100555 strlen(path), a);
556
Damien Miller3db5f532002-02-13 14:10:32 +1100557 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100558 if (status != SSH2_FX_OK)
559 error("Couldn't setstat on \"%s\": %s", path,
560 fx2txt(status));
561
562 return(status);
563}
564
565int
Damien Miller3db5f532002-02-13 14:10:32 +1100566do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100567 Attrib *a)
568{
569 u_int status, id;
570
Damien Miller3db5f532002-02-13 14:10:32 +1100571 id = conn->msg_id++;
572 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100573 handle_len, a);
574
Damien Miller3db5f532002-02-13 14:10:32 +1100575 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100576 if (status != SSH2_FX_OK)
577 error("Couldn't fsetstat: %s", fx2txt(status));
578
579 return(status);
580}
581
582char *
Damien Miller3db5f532002-02-13 14:10:32 +1100583do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100584{
585 Buffer msg;
586 u_int type, expected_id, count, id;
587 char *filename, *longname;
588 Attrib *a;
589
Damien Miller3db5f532002-02-13 14:10:32 +1100590 expected_id = id = conn->msg_id++;
591 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
592 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100593
594 buffer_init(&msg);
595
Damien Miller3db5f532002-02-13 14:10:32 +1100596 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100597 type = buffer_get_char(&msg);
598 id = buffer_get_int(&msg);
599
600 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000601 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100602
603 if (type == SSH2_FXP_STATUS) {
604 u_int status = buffer_get_int(&msg);
605
606 error("Couldn't canonicalise: %s", fx2txt(status));
607 return(NULL);
608 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000609 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100610 SSH2_FXP_NAME, type);
611
612 count = buffer_get_int(&msg);
613 if (count != 1)
614 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
615
616 filename = buffer_get_string(&msg, NULL);
617 longname = buffer_get_string(&msg, NULL);
618 a = decode_attrib(&msg);
619
620 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
621
622 xfree(longname);
623
624 buffer_free(&msg);
625
626 return(filename);
627}
628
629int
Damien Miller3db5f532002-02-13 14:10:32 +1100630do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100631{
632 Buffer msg;
633 u_int status, id;
634
635 buffer_init(&msg);
636
637 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100638 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100639 buffer_put_char(&msg, SSH2_FXP_RENAME);
640 buffer_put_int(&msg, id);
641 buffer_put_cstring(&msg, oldpath);
642 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100643 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100644 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
645 newpath);
646 buffer_free(&msg);
647
Damien Miller3db5f532002-02-13 14:10:32 +1100648 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100649 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100650 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
651 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100652
653 return(status);
654}
655
656int
Damien Miller3db5f532002-02-13 14:10:32 +1100657do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100658{
659 Buffer msg;
660 u_int status, id;
661
Damien Miller3db5f532002-02-13 14:10:32 +1100662 if (conn->version < 3) {
663 error("This server does not support the symlink operation");
664 return(SSH2_FX_OP_UNSUPPORTED);
665 }
666
Damien Miller058316f2001-03-08 10:08:49 +1100667 buffer_init(&msg);
668
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000669 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100670 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100671 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
672 buffer_put_int(&msg, id);
673 buffer_put_cstring(&msg, oldpath);
674 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100675 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100676 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
677 newpath);
678 buffer_free(&msg);
679
Damien Miller3db5f532002-02-13 14:10:32 +1100680 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100681 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000682 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100683 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100684
685 return(status);
686}
687
688char *
Damien Miller3db5f532002-02-13 14:10:32 +1100689do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100690{
691 Buffer msg;
692 u_int type, expected_id, count, id;
693 char *filename, *longname;
694 Attrib *a;
695
Damien Miller3db5f532002-02-13 14:10:32 +1100696 expected_id = id = conn->msg_id++;
697 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
698 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100699
700 buffer_init(&msg);
701
Damien Miller3db5f532002-02-13 14:10:32 +1100702 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100703 type = buffer_get_char(&msg);
704 id = buffer_get_int(&msg);
705
706 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000707 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100708
709 if (type == SSH2_FXP_STATUS) {
710 u_int status = buffer_get_int(&msg);
711
712 error("Couldn't readlink: %s", fx2txt(status));
713 return(NULL);
714 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000715 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100716 SSH2_FXP_NAME, type);
717
718 count = buffer_get_int(&msg);
719 if (count != 1)
720 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
721
722 filename = buffer_get_string(&msg, NULL);
723 longname = buffer_get_string(&msg, NULL);
724 a = decode_attrib(&msg);
725
726 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
727
728 xfree(longname);
729
730 buffer_free(&msg);
731
732 return(filename);
733}
734
Damien Miller16a13332002-02-13 14:03:56 +1100735static void
736send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
737 char *handle, u_int handle_len)
738{
739 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000740
Damien Miller16a13332002-02-13 14:03:56 +1100741 buffer_init(&msg);
742 buffer_clear(&msg);
743 buffer_put_char(&msg, SSH2_FXP_READ);
744 buffer_put_int(&msg, id);
745 buffer_put_string(&msg, handle, handle_len);
746 buffer_put_int64(&msg, offset);
747 buffer_put_int(&msg, len);
748 send_msg(fd_out, &msg);
749 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000750}
Damien Miller16a13332002-02-13 14:03:56 +1100751
Damien Miller058316f2001-03-08 10:08:49 +1100752int
Damien Miller3db5f532002-02-13 14:10:32 +1100753do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
754 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100755{
Damien Miller33804262001-02-04 23:20:18 +1100756 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100757 Buffer msg;
758 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000759 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100760 int read_error, write_errno;
761 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000762 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100763 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100764 struct request {
765 u_int id;
766 u_int len;
767 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000768 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100769 };
770 TAILQ_HEAD(reqhead, request) requests;
771 struct request *req;
772
773 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100774
Damien Miller3db5f532002-02-13 14:10:32 +1100775 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100776 if (a == NULL)
777 return(-1);
778
779 /* XXX: should we preserve set[ug]id? */
780 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100781 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100782 else
783 mode = 0666;
784
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000785 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100786 (!S_ISREG(a->perm))) {
787 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000788 return(-1);
789 }
790
Damien Miller16a13332002-02-13 14:03:56 +1100791 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
792 size = a->size;
793 else
794 size = 0;
795
Damien Miller3db5f532002-02-13 14:10:32 +1100796 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100797 buffer_init(&msg);
798
799 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100800 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100801 buffer_put_char(&msg, SSH2_FXP_OPEN);
802 buffer_put_int(&msg, id);
803 buffer_put_cstring(&msg, remote_path);
804 buffer_put_int(&msg, SSH2_FXF_READ);
805 attrib_clear(&junk); /* Send empty attributes */
806 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100807 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000808 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100809
Damien Miller3db5f532002-02-13 14:10:32 +1100810 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100811 if (handle == NULL) {
812 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100813 return(-1);
814 }
815
Damien Millera8e06ce2003-11-21 23:48:55 +1100816 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +1100817 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100818 if (local_fd == -1) {
819 error("Couldn't open local file \"%s\" for writing: %s",
820 local_path, strerror(errno));
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000821 buffer_free(&msg);
822 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100823 return(-1);
824 }
825
Damien Miller33804262001-02-04 23:20:18 +1100826 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100827 write_error = read_error = write_errno = num_req = offset = 0;
828 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +1100829 progress_counter = 0;
830
Damien Miller9ba30692004-03-08 23:12:02 +1100831 if (showprogress && size != 0)
832 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +1100833
Damien Miller16a13332002-02-13 14:03:56 +1100834 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100835 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100836 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100837
Darren Tuckercdf547a2004-05-24 10:12:19 +1000838 /*
Darren Tuckerfc959702004-07-17 16:12:08 +1000839 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +1000840 * allow outstanding requests to drain gracefully
841 */
842 if (interrupted) {
843 if (num_req == 0) /* If we haven't started yet... */
844 break;
845 max_req = 0;
846 }
847
Damien Miller16a13332002-02-13 14:03:56 +1100848 /* Send some more requests */
849 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000850 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000851 (unsigned long long)offset,
852 (unsigned long long)offset + buflen - 1,
853 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100854 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100855 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100856 req->len = buflen;
857 req->offset = offset;
858 offset += buflen;
859 num_req++;
860 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000861 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +1100862 req->len, handle, handle_len);
863 }
Damien Miller33804262001-02-04 23:20:18 +1100864
865 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100866 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100867 type = buffer_get_char(&msg);
868 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000869 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +1100870
Damien Miller16a13332002-02-13 14:03:56 +1100871 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +1100872 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +1100873 req != NULL && req->id != id;
874 req = TAILQ_NEXT(req, tq))
875 ;
876 if (req == NULL)
877 fatal("Unexpected reply %u", id);
878
879 switch (type) {
880 case SSH2_FXP_STATUS:
881 status = buffer_get_int(&msg);
882 if (status != SSH2_FX_EOF)
883 read_error = 1;
884 max_req = 0;
885 TAILQ_REMOVE(&requests, req, tq);
886 xfree(req);
887 num_req--;
888 break;
889 case SSH2_FXP_DATA:
890 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +0000891 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000892 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +0000893 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +1100894 if (len > req->len)
895 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +0000896 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +1100897 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +1000898 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +1100899 !write_error) {
900 write_errno = errno;
901 write_error = 1;
902 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +1100903 }
Damien Miller62d57f62003-01-10 21:43:24 +1100904 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +1100905 xfree(data);
906
907 if (len == req->len) {
908 TAILQ_REMOVE(&requests, req, tq);
909 xfree(req);
910 num_req--;
911 } else {
912 /* Resend the request for the missing data */
913 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +0000914 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000915 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +0000916 (unsigned long long)req->offset +
917 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +1100918 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100919 req->len -= len;
920 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000921 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +1100922 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100923 /* Reduce the request size */
924 if (len < buflen)
925 buflen = MAX(MIN_READ_SIZE, len);
926 }
927 if (max_req > 0) { /* max_req = 0 iff EOF received */
928 if (size > 0 && offset > size) {
929 /* Only one request at a time
930 * after the expected EOF */
931 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +0000932 (unsigned long long)offset,
933 num_req);
Damien Miller16a13332002-02-13 14:03:56 +1100934 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000935 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +1100936 ++max_req;
937 }
938 }
939 break;
940 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000941 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100942 SSH2_FXP_DATA, type);
943 }
Damien Miller33804262001-02-04 23:20:18 +1100944 }
Damien Miller33804262001-02-04 23:20:18 +1100945
Damien Miller62d57f62003-01-10 21:43:24 +1100946 if (showprogress && size)
947 stop_progress_meter();
948
Damien Miller16a13332002-02-13 14:03:56 +1100949 /* Sanity check */
950 if (TAILQ_FIRST(&requests) != NULL)
951 fatal("Transfer complete, but requests still in queue");
952
953 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000954 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +1100955 remote_path, fx2txt(status));
956 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100957 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +1100958 error("Couldn't write to \"%s\": %s", local_path,
959 strerror(write_errno));
960 status = -1;
961 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100962 } else {
Damien Miller3db5f532002-02-13 14:10:32 +1100963 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100964
965 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000966#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +1100967 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +1100968#else
Damien Miller16a13332002-02-13 14:03:56 +1100969 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000970#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +1100971 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +0000972 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100973 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
974 struct timeval tv[2];
975 tv[0].tv_sec = a->atime;
976 tv[1].tv_sec = a->mtime;
977 tv[0].tv_usec = tv[1].tv_usec = 0;
978 if (utimes(local_path, tv) == -1)
979 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +0000980 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100981 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000982 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100983 close(local_fd);
984 buffer_free(&msg);
985 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100986
987 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100988}
989
990int
Damien Miller3db5f532002-02-13 14:10:32 +1100991do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
992 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100993{
Damien Miller8829d362002-02-08 22:04:05 +1100994 int local_fd, status;
Damien Miller5873dfd2002-02-13 14:04:37 +1100995 u_int handle_len, id, type;
Damien Miller33804262001-02-04 23:20:18 +1100996 u_int64_t offset;
Damien Miller8829d362002-02-08 22:04:05 +1100997 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +1100998 Buffer msg;
999 struct stat sb;
1000 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +11001001 u_int32_t startid;
1002 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001003 struct outstanding_ack {
1004 u_int id;
1005 u_int len;
1006 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001007 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001008 };
1009 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001010 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001011
1012 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001013
1014 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1015 error("Couldn't open local file \"%s\" for reading: %s",
1016 local_path, strerror(errno));
1017 return(-1);
1018 }
1019 if (fstat(local_fd, &sb) == -1) {
1020 error("Couldn't fstat local file \"%s\": %s",
1021 local_path, strerror(errno));
1022 close(local_fd);
1023 return(-1);
1024 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001025 if (!S_ISREG(sb.st_mode)) {
1026 error("%s is not a regular file", local_path);
1027 close(local_fd);
1028 return(-1);
1029 }
Damien Miller33804262001-02-04 23:20:18 +11001030 stat_to_attrib(&sb, &a);
1031
1032 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1033 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1034 a.perm &= 0777;
1035 if (!pflag)
1036 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1037
1038 buffer_init(&msg);
1039
1040 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001041 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001042 buffer_put_char(&msg, SSH2_FXP_OPEN);
1043 buffer_put_int(&msg, id);
1044 buffer_put_cstring(&msg, remote_path);
1045 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1046 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001047 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001048 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001049
1050 buffer_clear(&msg);
1051
Damien Miller3db5f532002-02-13 14:10:32 +11001052 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001053 if (handle == NULL) {
1054 close(local_fd);
1055 buffer_free(&msg);
1056 return(-1);
1057 }
1058
Damien Miller16a13332002-02-13 14:03:56 +11001059 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001060 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001061
Damien Miller33804262001-02-04 23:20:18 +11001062 /* Read from local and write to remote */
1063 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001064 if (showprogress)
1065 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001066
Damien Miller9f0f5c62001-12-21 14:45:46 +11001067 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001068 int len;
Damien Miller33804262001-02-04 23:20:18 +11001069
1070 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001071 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001072 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001073 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001074 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001075 */
Darren Tuckercdf547a2004-05-24 10:12:19 +10001076 if (interrupted)
1077 len = 0;
1078 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001079 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001080 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1081
1082 if (len == -1)
1083 fatal("Couldn't read from \"%s\": %s", local_path,
1084 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001085
1086 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001087 ack = xmalloc(sizeof(*ack));
1088 ack->id = ++id;
1089 ack->offset = offset;
1090 ack->len = len;
1091 TAILQ_INSERT_TAIL(&acks, ack, tq);
1092
Damien Miller16a13332002-02-13 14:03:56 +11001093 buffer_clear(&msg);
1094 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001095 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001096 buffer_put_string(&msg, handle, handle_len);
1097 buffer_put_int64(&msg, offset);
1098 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001099 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001100 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001101 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001102 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001103 break;
1104
Damien Miller5873dfd2002-02-13 14:04:37 +11001105 if (ack == NULL)
1106 fatal("Unexpected ACK %u", id);
1107
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001108 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001109 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001110 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001111
Damien Miller5873dfd2002-02-13 14:04:37 +11001112 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001113 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001114 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001115 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001116
1117 if (type != SSH2_FXP_STATUS)
1118 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1119 "got %d", SSH2_FXP_STATUS, type);
1120
1121 status = buffer_get_int(&msg);
1122 debug3("SSH2_FXP_STATUS %d", status);
1123
1124 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001125 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001126 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001127 ack = TAILQ_NEXT(ack, tq))
1128 ;
1129 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001130 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001131 TAILQ_REMOVE(&acks, ack, tq);
1132
Damien Miller16a13332002-02-13 14:03:56 +11001133 if (status != SSH2_FX_OK) {
1134 error("Couldn't write to remote file \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001135 remote_path, fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +11001136 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001137 close(local_fd);
Damien Miller00111382003-03-10 11:21:17 +11001138 xfree(data);
1139 xfree(ack);
Damien Miller16a13332002-02-13 14:03:56 +11001140 goto done;
1141 }
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001142 debug3("In write loop, ack for %u %u bytes at %llu",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001143 ack->id, ack->len, (unsigned long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001144 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001145 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001146 }
Damien Miller33804262001-02-04 23:20:18 +11001147 offset += len;
1148 }
Damien Miller62d57f62003-01-10 21:43:24 +11001149 if (showprogress)
1150 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001151 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001152
1153 if (close(local_fd) == -1) {
1154 error("Couldn't close local file \"%s\": %s", local_path,
1155 strerror(errno));
Damien Miller3db5f532002-02-13 14:10:32 +11001156 do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001157 status = -1;
1158 goto done;
Damien Miller33804262001-02-04 23:20:18 +11001159 }
1160
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001161 /* Override umask and utimes if asked */
1162 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001163 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001164
Damien Miller3db5f532002-02-13 14:10:32 +11001165 status = do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001166
1167done:
1168 xfree(handle);
1169 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001170 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001171}