blob: 5e39aa7d21bd6f8b309d1e774c0230ed7fc75aa9 [file] [log] [blame]
Damien Miller9e720282008-06-29 22:46:35 +10001/* $OpenBSD: sftp-client.c,v 1.86 2008/06/26 06:10:09 djm 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>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100027#ifdef HAVE_SYS_STATVFS_H
Damien Millerd671e5a2008-05-19 14:53:33 +100028#include <sys/statvfs.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100029#endif
Damien Millerd7834352006-08-05 12:39:39 +100030#include "openbsd-compat/sys-queue.h"
Damien Millerf17883e2006-03-15 11:45:54 +110031#ifdef HAVE_SYS_STAT_H
32# include <sys/stat.h>
33#endif
Damien Miller9aec9192006-08-05 10:57:45 +100034#ifdef HAVE_SYS_TIME_H
35# include <sys/time.h>
36#endif
Damien Millerd7834352006-08-05 12:39:39 +100037#include <sys/uio.h>
Damien Miller57cf6382006-07-10 21:13:46 +100038
Darren Tucker39972492006-07-12 22:22:46 +100039#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100040#include <fcntl.h>
41#include <signal.h>
Damien Millerd7834352006-08-05 12:39:39 +100042#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100043#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100044#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100045#include <unistd.h>
Damien Miller16a13332002-02-13 14:03:56 +110046
Damien Miller33804262001-02-04 23:20:18 +110047#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100048#include "buffer.h"
Damien Miller33804262001-02-04 23:20:18 +110049#include "log.h"
50#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110051#include "progressmeter.h"
Damien Miller3f941882006-03-31 23:13:02 +110052#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110053
54#include "sftp.h"
55#include "sftp-common.h"
56#include "sftp-client.h"
57
Darren Tuckercdf547a2004-05-24 10:12:19 +100058extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110059extern int showprogress;
60
Damien Miller0c8d8f62006-03-15 11:34:25 +110061/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110062#define MIN_READ_SIZE 512
63
Damien Miller3db5f532002-02-13 14:10:32 +110064struct sftp_conn {
65 int fd_in;
66 int fd_out;
67 u_int transfer_buflen;
68 u_int num_requests;
69 u_int version;
70 u_int msg_id;
Damien Millerd671e5a2008-05-19 14:53:33 +100071#define SFTP_EXT_POSIX_RENAME 0x00000001
72#define SFTP_EXT_STATVFS 0x00000002
73#define SFTP_EXT_FSTATVFS 0x00000004
Damien Miller7a3e1d02008-03-27 10:59:57 +110074 u_int exts;
Damien Miller3db5f532002-02-13 14:10:32 +110075};
Ben Lindstrom288cc392001-02-09 02:58:04 +000076
Ben Lindstrombba81212001-06-25 05:01:22 +000077static void
Damien Miller33804262001-02-04 23:20:18 +110078send_msg(int fd, Buffer *m)
79{
Damien Millera7f3aaa2003-01-10 21:43:58 +110080 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +100081 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +110082
Damien Miller54446182006-01-02 23:40:50 +110083 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +110084 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +110085
Damien Millera7f3aaa2003-01-10 21:43:58 +110086 /* Send length first */
Damien Miller3f941882006-03-31 23:13:02 +110087 put_u32(mlen, buffer_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +100088 iov[0].iov_base = mlen;
89 iov[0].iov_len = sizeof(mlen);
90 iov[1].iov_base = buffer_ptr(m);
91 iov[1].iov_len = buffer_len(m);
Damien Millerd7834352006-08-05 12:39:39 +100092
Damien Miller58ca98b2006-04-23 12:06:35 +100093 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +110094 fatal("Couldn't send packet: %s", strerror(errno));
95
96 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +110097}
98
Ben Lindstrombba81212001-06-25 05:01:22 +000099static void
Damien Miller33804262001-02-04 23:20:18 +1100100get_msg(int fd, Buffer *m)
101{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100102 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +1100103
Damien Millera7f3aaa2003-01-10 21:43:58 +1100104 buffer_append_space(m, 4);
Damien Millerb253cc42005-05-26 12:23:44 +1000105 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
106 if (errno == EPIPE)
107 fatal("Connection closed");
108 else
109 fatal("Couldn't read packet: %s", strerror(errno));
110 }
Damien Miller33804262001-02-04 23:20:18 +1100111
Damien Millera7f3aaa2003-01-10 21:43:58 +1100112 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +1100113 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000114 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100115
Damien Millera7f3aaa2003-01-10 21:43:58 +1100116 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000117 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
118 if (errno == EPIPE)
119 fatal("Connection closed");
120 else
121 fatal("Read packet: %s", strerror(errno));
122 }
Damien Miller33804262001-02-04 23:20:18 +1100123}
124
Ben Lindstrombba81212001-06-25 05:01:22 +0000125static void
Damien Miller33804262001-02-04 23:20:18 +1100126send_string_request(int fd, u_int id, u_int code, char *s,
127 u_int len)
128{
129 Buffer msg;
130
131 buffer_init(&msg);
132 buffer_put_char(&msg, code);
133 buffer_put_int(&msg, id);
134 buffer_put_string(&msg, s, len);
135 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000136 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100137 buffer_free(&msg);
138}
139
Ben Lindstrombba81212001-06-25 05:01:22 +0000140static void
Damien Miller33804262001-02-04 23:20:18 +1100141send_string_attrs_request(int fd, u_int id, u_int code, char *s,
142 u_int len, Attrib *a)
143{
144 Buffer msg;
145
146 buffer_init(&msg);
147 buffer_put_char(&msg, code);
148 buffer_put_int(&msg, id);
149 buffer_put_string(&msg, s, len);
150 encode_attrib(&msg, a);
151 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000152 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100153 buffer_free(&msg);
154}
155
Ben Lindstrombba81212001-06-25 05:01:22 +0000156static u_int
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000157get_status(int fd, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100158{
159 Buffer msg;
160 u_int type, id, status;
161
162 buffer_init(&msg);
163 get_msg(fd, &msg);
164 type = buffer_get_char(&msg);
165 id = buffer_get_int(&msg);
166
167 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000168 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100169 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000170 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100171 SSH2_FXP_STATUS, type);
172
173 status = buffer_get_int(&msg);
174 buffer_free(&msg);
175
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000176 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100177
178 return(status);
179}
180
Ben Lindstrombba81212001-06-25 05:01:22 +0000181static char *
Damien Miller33804262001-02-04 23:20:18 +1100182get_handle(int fd, u_int expected_id, u_int *len)
183{
184 Buffer msg;
185 u_int type, id;
186 char *handle;
187
188 buffer_init(&msg);
189 get_msg(fd, &msg);
190 type = buffer_get_char(&msg);
191 id = buffer_get_int(&msg);
192
193 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000194 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100195 if (type == SSH2_FXP_STATUS) {
196 int status = buffer_get_int(&msg);
197
198 error("Couldn't get handle: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100199 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100200 return(NULL);
201 } else if (type != SSH2_FXP_HANDLE)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000202 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100203 SSH2_FXP_HANDLE, type);
204
205 handle = buffer_get_string(&msg, len);
206 buffer_free(&msg);
207
208 return(handle);
209}
210
Ben Lindstrombba81212001-06-25 05:01:22 +0000211static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000212get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100213{
214 Buffer msg;
215 u_int type, id;
216 Attrib *a;
217
218 buffer_init(&msg);
219 get_msg(fd, &msg);
220
221 type = buffer_get_char(&msg);
222 id = buffer_get_int(&msg);
223
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000224 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100225 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000226 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100227 if (type == SSH2_FXP_STATUS) {
228 int status = buffer_get_int(&msg);
229
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000230 if (quiet)
231 debug("Couldn't stat remote file: %s", fx2txt(status));
232 else
233 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100234 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100235 return(NULL);
236 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000237 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100238 SSH2_FXP_ATTRS, type);
239 }
240 a = decode_attrib(&msg);
241 buffer_free(&msg);
242
243 return(a);
244}
245
Damien Millerd671e5a2008-05-19 14:53:33 +1000246static int
Darren Tucker7b598892008-06-09 22:49:36 +1000247get_decode_statvfs(int fd, struct sftp_statvfs *st, u_int expected_id,
248 int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000249{
250 Buffer msg;
251 u_int type, id, flag;
252
253 buffer_init(&msg);
254 get_msg(fd, &msg);
255
256 type = buffer_get_char(&msg);
257 id = buffer_get_int(&msg);
258
259 debug3("Received statvfs reply T:%u I:%u", type, id);
260 if (id != expected_id)
261 fatal("ID mismatch (%u != %u)", id, expected_id);
262 if (type == SSH2_FXP_STATUS) {
263 int status = buffer_get_int(&msg);
264
265 if (quiet)
266 debug("Couldn't statvfs: %s", fx2txt(status));
267 else
268 error("Couldn't statvfs: %s", fx2txt(status));
269 buffer_free(&msg);
270 return -1;
271 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
272 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
273 SSH2_FXP_EXTENDED_REPLY, type);
274 }
275
276 bzero(st, sizeof(*st));
Darren Tucker7b598892008-06-09 22:49:36 +1000277 st->f_bsize = buffer_get_int64(&msg);
278 st->f_frsize = buffer_get_int64(&msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000279 st->f_blocks = buffer_get_int64(&msg);
280 st->f_bfree = buffer_get_int64(&msg);
281 st->f_bavail = buffer_get_int64(&msg);
282 st->f_files = buffer_get_int64(&msg);
283 st->f_ffree = buffer_get_int64(&msg);
284 st->f_favail = buffer_get_int64(&msg);
Darren Tucker294b8412008-06-08 12:57:08 +1000285 st->f_fsid = buffer_get_int64(&msg);
Darren Tucker7b598892008-06-09 22:49:36 +1000286 flag = buffer_get_int64(&msg);
287 st->f_namemax = buffer_get_int64(&msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000288
289 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
290 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
291
292 buffer_free(&msg);
293
294 return 0;
295}
296
Damien Miller3db5f532002-02-13 14:10:32 +1100297struct sftp_conn *
298do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100299{
Damien Miller7a3e1d02008-03-27 10:59:57 +1100300 u_int type, exts = 0;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000301 int version;
Damien Miller33804262001-02-04 23:20:18 +1100302 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100303 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100304
305 buffer_init(&msg);
306 buffer_put_char(&msg, SSH2_FXP_INIT);
307 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
308 send_msg(fd_out, &msg);
309
310 buffer_clear(&msg);
311
312 get_msg(fd_in, &msg);
313
Kevin Stevesef4eea92001-02-05 12:42:17 +0000314 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100315 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000316 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100317 type);
318 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100319 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100320 }
321 version = buffer_get_int(&msg);
322
323 debug2("Remote version: %d", version);
324
325 /* Check for extensions */
326 while (buffer_len(&msg) > 0) {
327 char *name = buffer_get_string(&msg, NULL);
328 char *value = buffer_get_string(&msg, NULL);
Darren Tuckera64ab332008-06-13 07:01:29 +1000329 int known = 0;
Damien Miller33804262001-02-04 23:20:18 +1100330
Damien Millerd671e5a2008-05-19 14:53:33 +1000331 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
Darren Tuckera64ab332008-06-13 07:01:29 +1000332 strcmp(value, "1") == 0) {
Damien Miller7a3e1d02008-03-27 10:59:57 +1100333 exts |= SFTP_EXT_POSIX_RENAME;
Darren Tuckera64ab332008-06-13 07:01:29 +1000334 known = 1;
335 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
336 strcmp(value, "2") == 0) {
Damien Millerd671e5a2008-05-19 14:53:33 +1000337 exts |= SFTP_EXT_STATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000338 known = 1;
339 } if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
340 strcmp(value, "2") == 0) {
Damien Millerd671e5a2008-05-19 14:53:33 +1000341 exts |= SFTP_EXT_FSTATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000342 known = 1;
343 }
344 if (known) {
345 debug2("Server supports extension \"%s\" revision %s",
346 name, value);
347 } else {
348 debug2("Unrecognised server extension \"%s\"", name);
349 }
Damien Miller33804262001-02-04 23:20:18 +1100350 xfree(name);
351 xfree(value);
352 }
353
354 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100355
Damien Miller3db5f532002-02-13 14:10:32 +1100356 ret = xmalloc(sizeof(*ret));
357 ret->fd_in = fd_in;
358 ret->fd_out = fd_out;
359 ret->transfer_buflen = transfer_buflen;
360 ret->num_requests = num_requests;
361 ret->version = version;
362 ret->msg_id = 1;
Damien Miller7a3e1d02008-03-27 10:59:57 +1100363 ret->exts = exts;
Damien Miller3db5f532002-02-13 14:10:32 +1100364
365 /* Some filexfer v.0 servers don't support large packets */
366 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000367 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100368
369 return(ret);
370}
371
372u_int
373sftp_proto_version(struct sftp_conn *conn)
374{
375 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100376}
377
378int
Damien Miller3db5f532002-02-13 14:10:32 +1100379do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100380{
381 u_int id, status;
382 Buffer msg;
383
384 buffer_init(&msg);
385
Damien Miller3db5f532002-02-13 14:10:32 +1100386 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100387 buffer_put_char(&msg, SSH2_FXP_CLOSE);
388 buffer_put_int(&msg, id);
389 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100390 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000391 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100392
Damien Miller3db5f532002-02-13 14:10:32 +1100393 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100394 if (status != SSH2_FX_OK)
395 error("Couldn't close file: %s", fx2txt(status));
396
397 buffer_free(&msg);
398
399 return(status);
400}
401
Damien Miller4870afd2001-03-14 10:27:09 +1100402
Ben Lindstrombba81212001-06-25 05:01:22 +0000403static int
Damien Miller3db5f532002-02-13 14:10:32 +1100404do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100405 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100406{
407 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000408 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100409 char *handle;
410
Damien Miller3db5f532002-02-13 14:10:32 +1100411 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100412
413 buffer_init(&msg);
414 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
415 buffer_put_int(&msg, id);
416 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100417 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100418
419 buffer_clear(&msg);
420
Damien Miller3db5f532002-02-13 14:10:32 +1100421 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100422 if (handle == NULL)
423 return(-1);
424
Damien Miller4870afd2001-03-14 10:27:09 +1100425 if (dir) {
426 ents = 0;
427 *dir = xmalloc(sizeof(**dir));
428 (*dir)[0] = NULL;
429 }
Damien Miller4870afd2001-03-14 10:27:09 +1100430
Darren Tuckercdf547a2004-05-24 10:12:19 +1000431 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100432 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100433
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000434 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100435
436 buffer_clear(&msg);
437 buffer_put_char(&msg, SSH2_FXP_READDIR);
438 buffer_put_int(&msg, id);
439 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100440 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100441
442 buffer_clear(&msg);
443
Damien Miller3db5f532002-02-13 14:10:32 +1100444 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100445
446 type = buffer_get_char(&msg);
447 id = buffer_get_int(&msg);
448
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000449 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100450
451 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000452 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100453
454 if (type == SSH2_FXP_STATUS) {
455 int status = buffer_get_int(&msg);
456
457 debug3("Received SSH2_FXP_STATUS %d", status);
458
459 if (status == SSH2_FX_EOF) {
460 break;
461 } else {
462 error("Couldn't read directory: %s",
463 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100464 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100465 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000466 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100467 }
468 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000469 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100470 SSH2_FXP_NAME, type);
471
472 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100473 if (count == 0)
474 break;
475 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100476 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100477 char *filename, *longname;
478 Attrib *a;
479
480 filename = buffer_get_string(&msg, NULL);
481 longname = buffer_get_string(&msg, NULL);
482 a = decode_attrib(&msg);
483
Damien Miller4870afd2001-03-14 10:27:09 +1100484 if (printflag)
485 printf("%s\n", longname);
486
487 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100488 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100489 (*dir)[ents] = xmalloc(sizeof(***dir));
490 (*dir)[ents]->filename = xstrdup(filename);
491 (*dir)[ents]->longname = xstrdup(longname);
492 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
493 (*dir)[++ents] = NULL;
494 }
Damien Miller33804262001-02-04 23:20:18 +1100495
496 xfree(filename);
497 xfree(longname);
498 }
499 }
500
501 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100502 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100503 xfree(handle);
504
Darren Tuckercdf547a2004-05-24 10:12:19 +1000505 /* Don't return partial matches on interrupt */
506 if (interrupted && dir != NULL && *dir != NULL) {
507 free_sftp_dirents(*dir);
508 *dir = xmalloc(sizeof(**dir));
509 **dir = NULL;
510 }
511
Damien Miller33804262001-02-04 23:20:18 +1100512 return(0);
513}
514
515int
Damien Miller3db5f532002-02-13 14:10:32 +1100516do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100517{
Damien Miller3db5f532002-02-13 14:10:32 +1100518 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100519}
520
521void free_sftp_dirents(SFTP_DIRENT **s)
522{
523 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100524
525 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100526 xfree(s[i]->filename);
527 xfree(s[i]->longname);
528 xfree(s[i]);
529 }
530 xfree(s);
531}
532
533int
Damien Miller3db5f532002-02-13 14:10:32 +1100534do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100535{
536 u_int status, id;
537
538 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
539
Damien Miller3db5f532002-02-13 14:10:32 +1100540 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000541 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100542 strlen(path));
543 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100544 if (status != SSH2_FX_OK)
545 error("Couldn't delete file: %s", fx2txt(status));
546 return(status);
547}
548
549int
Damien Miller3db5f532002-02-13 14:10:32 +1100550do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100551{
552 u_int status, id;
553
Damien Miller3db5f532002-02-13 14:10:32 +1100554 id = conn->msg_id++;
555 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100556 strlen(path), a);
557
Damien Miller3db5f532002-02-13 14:10:32 +1100558 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100559 if (status != SSH2_FX_OK)
560 error("Couldn't create directory: %s", fx2txt(status));
561
562 return(status);
563}
564
565int
Damien Miller3db5f532002-02-13 14:10:32 +1100566do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100567{
568 u_int status, id;
569
Damien Miller3db5f532002-02-13 14:10:32 +1100570 id = conn->msg_id++;
571 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
572 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100573
Damien Miller3db5f532002-02-13 14:10:32 +1100574 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100575 if (status != SSH2_FX_OK)
576 error("Couldn't remove directory: %s", fx2txt(status));
577
578 return(status);
579}
580
581Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100582do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100583{
584 u_int id;
585
Damien Miller3db5f532002-02-13 14:10:32 +1100586 id = conn->msg_id++;
587
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000588 send_string_request(conn->fd_out, id,
589 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100590 path, strlen(path));
591
592 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100593}
594
595Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100596do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100597{
598 u_int id;
599
Damien Miller3db5f532002-02-13 14:10:32 +1100600 if (conn->version == 0) {
601 if (quiet)
602 debug("Server version does not support lstat operation");
603 else
Damien Miller996acd22003-04-09 20:59:48 +1000604 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000605 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100606 }
607
608 id = conn->msg_id++;
609 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
610 strlen(path));
611
612 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100613}
614
Damien Millercfe23d32008-02-10 22:20:44 +1100615#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100616Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100617do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100618{
619 u_int id;
620
Damien Miller3db5f532002-02-13 14:10:32 +1100621 id = conn->msg_id++;
622 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
623 handle_len);
624
625 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100626}
Damien Millercfe23d32008-02-10 22:20:44 +1100627#endif
Damien Miller33804262001-02-04 23:20:18 +1100628
629int
Damien Miller3db5f532002-02-13 14:10:32 +1100630do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100631{
632 u_int status, id;
633
Damien Miller3db5f532002-02-13 14:10:32 +1100634 id = conn->msg_id++;
635 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100636 strlen(path), a);
637
Damien Miller3db5f532002-02-13 14:10:32 +1100638 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100639 if (status != SSH2_FX_OK)
640 error("Couldn't setstat on \"%s\": %s", path,
641 fx2txt(status));
642
643 return(status);
644}
645
646int
Damien Miller3db5f532002-02-13 14:10:32 +1100647do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100648 Attrib *a)
649{
650 u_int status, id;
651
Damien Miller3db5f532002-02-13 14:10:32 +1100652 id = conn->msg_id++;
653 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100654 handle_len, a);
655
Damien Miller3db5f532002-02-13 14:10:32 +1100656 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100657 if (status != SSH2_FX_OK)
658 error("Couldn't fsetstat: %s", fx2txt(status));
659
660 return(status);
661}
662
663char *
Damien Miller3db5f532002-02-13 14:10:32 +1100664do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100665{
666 Buffer msg;
667 u_int type, expected_id, count, id;
668 char *filename, *longname;
669 Attrib *a;
670
Damien Miller3db5f532002-02-13 14:10:32 +1100671 expected_id = id = conn->msg_id++;
672 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
673 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100674
675 buffer_init(&msg);
676
Damien Miller3db5f532002-02-13 14:10:32 +1100677 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100678 type = buffer_get_char(&msg);
679 id = buffer_get_int(&msg);
680
681 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000682 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100683
684 if (type == SSH2_FXP_STATUS) {
685 u_int status = buffer_get_int(&msg);
686
687 error("Couldn't canonicalise: %s", fx2txt(status));
688 return(NULL);
689 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000690 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100691 SSH2_FXP_NAME, type);
692
693 count = buffer_get_int(&msg);
694 if (count != 1)
695 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
696
697 filename = buffer_get_string(&msg, NULL);
698 longname = buffer_get_string(&msg, NULL);
699 a = decode_attrib(&msg);
700
701 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
702
703 xfree(longname);
704
705 buffer_free(&msg);
706
707 return(filename);
708}
709
710int
Damien Miller3db5f532002-02-13 14:10:32 +1100711do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100712{
713 Buffer msg;
714 u_int status, id;
715
716 buffer_init(&msg);
717
718 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100719 id = conn->msg_id++;
Damien Miller7a3e1d02008-03-27 10:59:57 +1100720 if ((conn->exts & SFTP_EXT_POSIX_RENAME)) {
721 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
722 buffer_put_int(&msg, id);
723 buffer_put_cstring(&msg, "posix-rename@openssh.com");
724 } else {
725 buffer_put_char(&msg, SSH2_FXP_RENAME);
726 buffer_put_int(&msg, id);
727 }
Damien Miller33804262001-02-04 23:20:18 +1100728 buffer_put_cstring(&msg, oldpath);
729 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100730 send_msg(conn->fd_out, &msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100731 debug3("Sent message %s \"%s\" -> \"%s\"",
732 (conn->exts & SFTP_EXT_POSIX_RENAME) ? "posix-rename@openssh.com" :
733 "SSH2_FXP_RENAME", oldpath, newpath);
Damien Miller33804262001-02-04 23:20:18 +1100734 buffer_free(&msg);
735
Damien Miller3db5f532002-02-13 14:10:32 +1100736 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100737 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100738 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
739 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100740
741 return(status);
742}
743
744int
Damien Miller3db5f532002-02-13 14:10:32 +1100745do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100746{
747 Buffer msg;
748 u_int status, id;
749
Damien Miller3db5f532002-02-13 14:10:32 +1100750 if (conn->version < 3) {
751 error("This server does not support the symlink operation");
752 return(SSH2_FX_OP_UNSUPPORTED);
753 }
754
Damien Miller058316f2001-03-08 10:08:49 +1100755 buffer_init(&msg);
756
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000757 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100758 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100759 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
760 buffer_put_int(&msg, id);
761 buffer_put_cstring(&msg, oldpath);
762 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100763 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100764 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
765 newpath);
766 buffer_free(&msg);
767
Damien Miller3db5f532002-02-13 14:10:32 +1100768 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100769 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000770 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100771 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100772
773 return(status);
774}
775
Damien Millercfe23d32008-02-10 22:20:44 +1100776#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +1100777char *
Damien Miller3db5f532002-02-13 14:10:32 +1100778do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100779{
780 Buffer msg;
781 u_int type, expected_id, count, id;
782 char *filename, *longname;
783 Attrib *a;
784
Damien Miller3db5f532002-02-13 14:10:32 +1100785 expected_id = id = conn->msg_id++;
786 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
787 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100788
789 buffer_init(&msg);
790
Damien Miller3db5f532002-02-13 14:10:32 +1100791 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100792 type = buffer_get_char(&msg);
793 id = buffer_get_int(&msg);
794
795 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000796 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100797
798 if (type == SSH2_FXP_STATUS) {
799 u_int status = buffer_get_int(&msg);
800
801 error("Couldn't readlink: %s", fx2txt(status));
802 return(NULL);
803 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000804 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100805 SSH2_FXP_NAME, type);
806
807 count = buffer_get_int(&msg);
808 if (count != 1)
809 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
810
811 filename = buffer_get_string(&msg, NULL);
812 longname = buffer_get_string(&msg, NULL);
813 a = decode_attrib(&msg);
814
815 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
816
817 xfree(longname);
818
819 buffer_free(&msg);
820
821 return(filename);
822}
Damien Millercfe23d32008-02-10 22:20:44 +1100823#endif
Damien Miller058316f2001-03-08 10:08:49 +1100824
Damien Millerd671e5a2008-05-19 14:53:33 +1000825int
Darren Tucker7b598892008-06-09 22:49:36 +1000826do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
Damien Millerd671e5a2008-05-19 14:53:33 +1000827 int quiet)
828{
829 Buffer msg;
830 u_int id;
831
832 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
833 error("Server does not support statvfs@openssh.com extension");
834 return -1;
835 }
836
837 id = conn->msg_id++;
838
839 buffer_init(&msg);
840 buffer_clear(&msg);
841 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
842 buffer_put_int(&msg, id);
843 buffer_put_cstring(&msg, "statvfs@openssh.com");
844 buffer_put_cstring(&msg, path);
845 send_msg(conn->fd_out, &msg);
846 buffer_free(&msg);
847
848 return get_decode_statvfs(conn->fd_in, st, id, quiet);
849}
850
851#ifdef notyet
852int
853do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
Darren Tucker7b598892008-06-09 22:49:36 +1000854 struct sftp_statvfs *st, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000855{
856 Buffer msg;
857 u_int id;
858
859 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
860 error("Server does not support fstatvfs@openssh.com extension");
861 return -1;
862 }
863
864 id = conn->msg_id++;
865
866 buffer_init(&msg);
867 buffer_clear(&msg);
868 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
869 buffer_put_int(&msg, id);
870 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
871 buffer_put_string(&msg, handle, handle_len);
872 send_msg(conn->fd_out, &msg);
873 buffer_free(&msg);
874
875 return get_decode_statvfs(conn->fd_in, st, id, quiet);
876}
877#endif
878
Damien Miller16a13332002-02-13 14:03:56 +1100879static void
880send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
881 char *handle, u_int handle_len)
882{
883 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000884
Damien Miller16a13332002-02-13 14:03:56 +1100885 buffer_init(&msg);
886 buffer_clear(&msg);
887 buffer_put_char(&msg, SSH2_FXP_READ);
888 buffer_put_int(&msg, id);
889 buffer_put_string(&msg, handle, handle_len);
890 buffer_put_int64(&msg, offset);
891 buffer_put_int(&msg, len);
892 send_msg(fd_out, &msg);
893 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000894}
Damien Miller16a13332002-02-13 14:03:56 +1100895
Damien Miller058316f2001-03-08 10:08:49 +1100896int
Damien Miller3db5f532002-02-13 14:10:32 +1100897do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
898 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100899{
Damien Miller33804262001-02-04 23:20:18 +1100900 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100901 Buffer msg;
902 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000903 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100904 int read_error, write_errno;
905 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000906 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100907 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100908 struct request {
909 u_int id;
910 u_int len;
911 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000912 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100913 };
914 TAILQ_HEAD(reqhead, request) requests;
915 struct request *req;
916
917 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100918
Damien Miller3db5f532002-02-13 14:10:32 +1100919 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100920 if (a == NULL)
921 return(-1);
922
Damien Miller9e720282008-06-29 22:46:35 +1000923 /* Do not preserve set[ug]id here, as we do not preserve ownership */
Damien Miller33804262001-02-04 23:20:18 +1100924 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100925 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100926 else
927 mode = 0666;
928
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000929 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100930 (!S_ISREG(a->perm))) {
931 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000932 return(-1);
933 }
934
Damien Miller16a13332002-02-13 14:03:56 +1100935 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
936 size = a->size;
937 else
938 size = 0;
939
Damien Miller3db5f532002-02-13 14:10:32 +1100940 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100941 buffer_init(&msg);
942
943 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100944 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100945 buffer_put_char(&msg, SSH2_FXP_OPEN);
946 buffer_put_int(&msg, id);
947 buffer_put_cstring(&msg, remote_path);
948 buffer_put_int(&msg, SSH2_FXF_READ);
949 attrib_clear(&junk); /* Send empty attributes */
950 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100951 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000952 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100953
Damien Miller3db5f532002-02-13 14:10:32 +1100954 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100955 if (handle == NULL) {
956 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100957 return(-1);
958 }
959
Damien Millera8e06ce2003-11-21 23:48:55 +1100960 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +1100961 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100962 if (local_fd == -1) {
963 error("Couldn't open local file \"%s\" for writing: %s",
964 local_path, strerror(errno));
Damien Miller6b0c8182008-02-10 22:23:41 +1100965 do_close(conn, handle, handle_len);
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000966 buffer_free(&msg);
967 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100968 return(-1);
969 }
970
Damien Miller33804262001-02-04 23:20:18 +1100971 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100972 write_error = read_error = write_errno = num_req = offset = 0;
973 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +1100974 progress_counter = 0;
975
Damien Miller9ba30692004-03-08 23:12:02 +1100976 if (showprogress && size != 0)
977 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +1100978
Damien Miller16a13332002-02-13 14:03:56 +1100979 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100980 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100981 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100982
Darren Tuckercdf547a2004-05-24 10:12:19 +1000983 /*
Darren Tuckerfc959702004-07-17 16:12:08 +1000984 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +1000985 * allow outstanding requests to drain gracefully
986 */
987 if (interrupted) {
988 if (num_req == 0) /* If we haven't started yet... */
989 break;
990 max_req = 0;
991 }
992
Damien Miller16a13332002-02-13 14:03:56 +1100993 /* Send some more requests */
994 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000995 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000996 (unsigned long long)offset,
997 (unsigned long long)offset + buflen - 1,
998 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100999 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +11001000 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001001 req->len = buflen;
1002 req->offset = offset;
1003 offset += buflen;
1004 num_req++;
1005 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001006 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001007 req->len, handle, handle_len);
1008 }
Damien Miller33804262001-02-04 23:20:18 +11001009
1010 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001011 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +11001012 type = buffer_get_char(&msg);
1013 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001014 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001015
Damien Miller16a13332002-02-13 14:03:56 +11001016 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001017 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001018 req != NULL && req->id != id;
1019 req = TAILQ_NEXT(req, tq))
1020 ;
1021 if (req == NULL)
1022 fatal("Unexpected reply %u", id);
1023
1024 switch (type) {
1025 case SSH2_FXP_STATUS:
1026 status = buffer_get_int(&msg);
1027 if (status != SSH2_FX_EOF)
1028 read_error = 1;
1029 max_req = 0;
1030 TAILQ_REMOVE(&requests, req, tq);
1031 xfree(req);
1032 num_req--;
1033 break;
1034 case SSH2_FXP_DATA:
1035 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +00001036 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001037 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001038 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001039 if (len > req->len)
1040 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +00001041 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001042 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001043 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001044 !write_error) {
1045 write_errno = errno;
1046 write_error = 1;
1047 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001048 }
Damien Miller62d57f62003-01-10 21:43:24 +11001049 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +11001050 xfree(data);
1051
1052 if (len == req->len) {
1053 TAILQ_REMOVE(&requests, req, tq);
1054 xfree(req);
1055 num_req--;
1056 } else {
1057 /* Resend the request for the missing data */
1058 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001059 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001060 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001061 (unsigned long long)req->offset +
1062 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001063 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001064 req->len -= len;
1065 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001066 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001067 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001068 /* Reduce the request size */
1069 if (len < buflen)
1070 buflen = MAX(MIN_READ_SIZE, len);
1071 }
1072 if (max_req > 0) { /* max_req = 0 iff EOF received */
1073 if (size > 0 && offset > size) {
1074 /* Only one request at a time
1075 * after the expected EOF */
1076 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001077 (unsigned long long)offset,
1078 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001079 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001080 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001081 ++max_req;
1082 }
1083 }
1084 break;
1085 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001086 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001087 SSH2_FXP_DATA, type);
1088 }
Damien Miller33804262001-02-04 23:20:18 +11001089 }
Damien Miller33804262001-02-04 23:20:18 +11001090
Damien Miller62d57f62003-01-10 21:43:24 +11001091 if (showprogress && size)
1092 stop_progress_meter();
1093
Damien Miller16a13332002-02-13 14:03:56 +11001094 /* Sanity check */
1095 if (TAILQ_FIRST(&requests) != NULL)
1096 fatal("Transfer complete, but requests still in queue");
1097
1098 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001099 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001100 remote_path, fx2txt(status));
1101 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001102 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001103 error("Couldn't write to \"%s\": %s", local_path,
1104 strerror(write_errno));
1105 status = -1;
1106 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001107 } else {
Damien Miller3db5f532002-02-13 14:10:32 +11001108 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001109
1110 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001111#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +11001112 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001113#else
Damien Miller16a13332002-02-13 14:03:56 +11001114 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001115#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001116 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001117 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001118 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1119 struct timeval tv[2];
1120 tv[0].tv_sec = a->atime;
1121 tv[1].tv_sec = a->mtime;
1122 tv[0].tv_usec = tv[1].tv_usec = 0;
1123 if (utimes(local_path, tv) == -1)
1124 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001125 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001126 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001127 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001128 close(local_fd);
1129 buffer_free(&msg);
1130 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001131
1132 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001133}
1134
1135int
Damien Miller3db5f532002-02-13 14:10:32 +11001136do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
1137 int pflag)
Damien Miller33804262001-02-04 23:20:18 +11001138{
Damien Milleracdf25b2008-02-10 22:27:24 +11001139 int local_fd;
1140 int status = SSH2_FX_OK;
Damien Miller5873dfd2002-02-13 14:04:37 +11001141 u_int handle_len, id, type;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001142 off_t offset;
Damien Miller8829d362002-02-08 22:04:05 +11001143 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +11001144 Buffer msg;
1145 struct stat sb;
1146 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +11001147 u_int32_t startid;
1148 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001149 struct outstanding_ack {
1150 u_int id;
1151 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001152 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001153 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001154 };
1155 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001156 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001157
1158 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001159
1160 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1161 error("Couldn't open local file \"%s\" for reading: %s",
1162 local_path, strerror(errno));
1163 return(-1);
1164 }
1165 if (fstat(local_fd, &sb) == -1) {
1166 error("Couldn't fstat local file \"%s\": %s",
1167 local_path, strerror(errno));
1168 close(local_fd);
1169 return(-1);
1170 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001171 if (!S_ISREG(sb.st_mode)) {
1172 error("%s is not a regular file", local_path);
1173 close(local_fd);
1174 return(-1);
1175 }
Damien Miller33804262001-02-04 23:20:18 +11001176 stat_to_attrib(&sb, &a);
1177
1178 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1179 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1180 a.perm &= 0777;
1181 if (!pflag)
1182 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1183
1184 buffer_init(&msg);
1185
1186 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001187 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001188 buffer_put_char(&msg, SSH2_FXP_OPEN);
1189 buffer_put_int(&msg, id);
1190 buffer_put_cstring(&msg, remote_path);
1191 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1192 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001193 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001194 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001195
1196 buffer_clear(&msg);
1197
Damien Miller3db5f532002-02-13 14:10:32 +11001198 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001199 if (handle == NULL) {
1200 close(local_fd);
1201 buffer_free(&msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001202 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001203 }
1204
Damien Miller16a13332002-02-13 14:03:56 +11001205 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001206 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001207
Damien Miller33804262001-02-04 23:20:18 +11001208 /* Read from local and write to remote */
1209 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001210 if (showprogress)
1211 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001212
Damien Miller9f0f5c62001-12-21 14:45:46 +11001213 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001214 int len;
Damien Miller33804262001-02-04 23:20:18 +11001215
1216 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001217 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001218 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001219 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001220 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001221 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001222 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001223 len = 0;
1224 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001225 len = read(local_fd, data, conn->transfer_buflen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001226 while ((len == -1) &&
1227 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
Damien Miller33804262001-02-04 23:20:18 +11001228
1229 if (len == -1)
1230 fatal("Couldn't read from \"%s\": %s", local_path,
1231 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001232
1233 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001234 ack = xmalloc(sizeof(*ack));
1235 ack->id = ++id;
1236 ack->offset = offset;
1237 ack->len = len;
1238 TAILQ_INSERT_TAIL(&acks, ack, tq);
1239
Damien Miller16a13332002-02-13 14:03:56 +11001240 buffer_clear(&msg);
1241 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001242 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001243 buffer_put_string(&msg, handle, handle_len);
1244 buffer_put_int64(&msg, offset);
1245 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001246 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001247 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001248 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001249 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001250 break;
1251
Damien Miller5873dfd2002-02-13 14:04:37 +11001252 if (ack == NULL)
1253 fatal("Unexpected ACK %u", id);
1254
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001255 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001256 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001257 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001258
Damien Miller5873dfd2002-02-13 14:04:37 +11001259 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001260 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001261 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001262 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001263
1264 if (type != SSH2_FXP_STATUS)
1265 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1266 "got %d", SSH2_FXP_STATUS, type);
1267
1268 status = buffer_get_int(&msg);
1269 debug3("SSH2_FXP_STATUS %d", status);
1270
1271 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001272 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001273 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001274 ack = TAILQ_NEXT(ack, tq))
1275 ;
1276 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001277 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001278 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001279 debug3("In write loop, ack for %u %u bytes at %lld",
1280 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001281 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001282 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001283 }
Damien Miller33804262001-02-04 23:20:18 +11001284 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001285 if (offset < 0)
1286 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001287 }
Damien Milleracdf25b2008-02-10 22:27:24 +11001288 buffer_free(&msg);
1289
Damien Miller62d57f62003-01-10 21:43:24 +11001290 if (showprogress)
1291 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001292 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001293
Damien Milleracdf25b2008-02-10 22:27:24 +11001294 if (status != SSH2_FX_OK) {
1295 error("Couldn't write to remote file \"%s\": %s",
1296 remote_path, fx2txt(status));
1297 status = -1;
1298 }
1299
Damien Miller33804262001-02-04 23:20:18 +11001300 if (close(local_fd) == -1) {
1301 error("Couldn't close local file \"%s\": %s", local_path,
1302 strerror(errno));
Damien Millerd7686fd2001-02-10 00:40:03 +11001303 status = -1;
Damien Miller33804262001-02-04 23:20:18 +11001304 }
1305
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001306 /* Override umask and utimes if asked */
1307 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001308 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001309
Damien Milleracdf25b2008-02-10 22:27:24 +11001310 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
1311 status = -1;
Damien Millerd7686fd2001-02-10 00:40:03 +11001312 xfree(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001313
1314 return status;
Damien Miller33804262001-02-04 23:20:18 +11001315}