blob: 8d0666b852712b3188d55356ab00a2b2cfb2ea2e [file] [log] [blame]
Darren Tucker7b598892008-06-09 22:49:36 +10001/* $OpenBSD: sftp-client.c,v 1.84 2008/06/08 20:15:29 dtucker 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);
329
330 debug2("Init extension: \"%s\"", name);
Damien Millerd671e5a2008-05-19 14:53:33 +1000331 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
332 strcmp(value, "1") == 0)
Damien Miller7a3e1d02008-03-27 10:59:57 +1100333 exts |= SFTP_EXT_POSIX_RENAME;
Damien Millerd671e5a2008-05-19 14:53:33 +1000334 if (strcmp(name, "statvfs@openssh.com") == 0 &&
Darren Tucker294b8412008-06-08 12:57:08 +1000335 strcmp(value, "2") == 0)
Damien Millerd671e5a2008-05-19 14:53:33 +1000336 exts |= SFTP_EXT_STATVFS;
337 if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
Darren Tucker294b8412008-06-08 12:57:08 +1000338 strcmp(value, "2") == 0)
Damien Millerd671e5a2008-05-19 14:53:33 +1000339 exts |= SFTP_EXT_FSTATVFS;
Damien Miller33804262001-02-04 23:20:18 +1100340 xfree(name);
341 xfree(value);
342 }
343
344 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100345
Damien Miller3db5f532002-02-13 14:10:32 +1100346 ret = xmalloc(sizeof(*ret));
347 ret->fd_in = fd_in;
348 ret->fd_out = fd_out;
349 ret->transfer_buflen = transfer_buflen;
350 ret->num_requests = num_requests;
351 ret->version = version;
352 ret->msg_id = 1;
Damien Miller7a3e1d02008-03-27 10:59:57 +1100353 ret->exts = exts;
Damien Miller3db5f532002-02-13 14:10:32 +1100354
355 /* Some filexfer v.0 servers don't support large packets */
356 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000357 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100358
359 return(ret);
360}
361
362u_int
363sftp_proto_version(struct sftp_conn *conn)
364{
365 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100366}
367
368int
Damien Miller3db5f532002-02-13 14:10:32 +1100369do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100370{
371 u_int id, status;
372 Buffer msg;
373
374 buffer_init(&msg);
375
Damien Miller3db5f532002-02-13 14:10:32 +1100376 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100377 buffer_put_char(&msg, SSH2_FXP_CLOSE);
378 buffer_put_int(&msg, id);
379 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100380 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000381 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100382
Damien Miller3db5f532002-02-13 14:10:32 +1100383 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100384 if (status != SSH2_FX_OK)
385 error("Couldn't close file: %s", fx2txt(status));
386
387 buffer_free(&msg);
388
389 return(status);
390}
391
Damien Miller4870afd2001-03-14 10:27:09 +1100392
Ben Lindstrombba81212001-06-25 05:01:22 +0000393static int
Damien Miller3db5f532002-02-13 14:10:32 +1100394do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100395 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100396{
397 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000398 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100399 char *handle;
400
Damien Miller3db5f532002-02-13 14:10:32 +1100401 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100402
403 buffer_init(&msg);
404 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
405 buffer_put_int(&msg, id);
406 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100407 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100408
409 buffer_clear(&msg);
410
Damien Miller3db5f532002-02-13 14:10:32 +1100411 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100412 if (handle == NULL)
413 return(-1);
414
Damien Miller4870afd2001-03-14 10:27:09 +1100415 if (dir) {
416 ents = 0;
417 *dir = xmalloc(sizeof(**dir));
418 (*dir)[0] = NULL;
419 }
Damien Miller4870afd2001-03-14 10:27:09 +1100420
Darren Tuckercdf547a2004-05-24 10:12:19 +1000421 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100422 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100423
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000424 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100425
426 buffer_clear(&msg);
427 buffer_put_char(&msg, SSH2_FXP_READDIR);
428 buffer_put_int(&msg, id);
429 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100430 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100431
432 buffer_clear(&msg);
433
Damien Miller3db5f532002-02-13 14:10:32 +1100434 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100435
436 type = buffer_get_char(&msg);
437 id = buffer_get_int(&msg);
438
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000439 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100440
441 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000442 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100443
444 if (type == SSH2_FXP_STATUS) {
445 int status = buffer_get_int(&msg);
446
447 debug3("Received SSH2_FXP_STATUS %d", status);
448
449 if (status == SSH2_FX_EOF) {
450 break;
451 } else {
452 error("Couldn't read directory: %s",
453 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100454 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100455 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000456 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100457 }
458 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000459 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100460 SSH2_FXP_NAME, type);
461
462 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100463 if (count == 0)
464 break;
465 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100466 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100467 char *filename, *longname;
468 Attrib *a;
469
470 filename = buffer_get_string(&msg, NULL);
471 longname = buffer_get_string(&msg, NULL);
472 a = decode_attrib(&msg);
473
Damien Miller4870afd2001-03-14 10:27:09 +1100474 if (printflag)
475 printf("%s\n", longname);
476
477 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100478 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100479 (*dir)[ents] = xmalloc(sizeof(***dir));
480 (*dir)[ents]->filename = xstrdup(filename);
481 (*dir)[ents]->longname = xstrdup(longname);
482 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
483 (*dir)[++ents] = NULL;
484 }
Damien Miller33804262001-02-04 23:20:18 +1100485
486 xfree(filename);
487 xfree(longname);
488 }
489 }
490
491 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100492 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100493 xfree(handle);
494
Darren Tuckercdf547a2004-05-24 10:12:19 +1000495 /* Don't return partial matches on interrupt */
496 if (interrupted && dir != NULL && *dir != NULL) {
497 free_sftp_dirents(*dir);
498 *dir = xmalloc(sizeof(**dir));
499 **dir = NULL;
500 }
501
Damien Miller33804262001-02-04 23:20:18 +1100502 return(0);
503}
504
505int
Damien Miller3db5f532002-02-13 14:10:32 +1100506do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100507{
Damien Miller3db5f532002-02-13 14:10:32 +1100508 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100509}
510
511void free_sftp_dirents(SFTP_DIRENT **s)
512{
513 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100514
515 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100516 xfree(s[i]->filename);
517 xfree(s[i]->longname);
518 xfree(s[i]);
519 }
520 xfree(s);
521}
522
523int
Damien Miller3db5f532002-02-13 14:10:32 +1100524do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100525{
526 u_int status, id;
527
528 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
529
Damien Miller3db5f532002-02-13 14:10:32 +1100530 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000531 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100532 strlen(path));
533 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100534 if (status != SSH2_FX_OK)
535 error("Couldn't delete file: %s", fx2txt(status));
536 return(status);
537}
538
539int
Damien Miller3db5f532002-02-13 14:10:32 +1100540do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100541{
542 u_int status, id;
543
Damien Miller3db5f532002-02-13 14:10:32 +1100544 id = conn->msg_id++;
545 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100546 strlen(path), a);
547
Damien Miller3db5f532002-02-13 14:10:32 +1100548 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100549 if (status != SSH2_FX_OK)
550 error("Couldn't create directory: %s", fx2txt(status));
551
552 return(status);
553}
554
555int
Damien Miller3db5f532002-02-13 14:10:32 +1100556do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100557{
558 u_int status, id;
559
Damien Miller3db5f532002-02-13 14:10:32 +1100560 id = conn->msg_id++;
561 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
562 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100563
Damien Miller3db5f532002-02-13 14:10:32 +1100564 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100565 if (status != SSH2_FX_OK)
566 error("Couldn't remove directory: %s", fx2txt(status));
567
568 return(status);
569}
570
571Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100572do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100573{
574 u_int id;
575
Damien Miller3db5f532002-02-13 14:10:32 +1100576 id = conn->msg_id++;
577
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000578 send_string_request(conn->fd_out, id,
579 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100580 path, strlen(path));
581
582 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100583}
584
585Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100586do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100587{
588 u_int id;
589
Damien Miller3db5f532002-02-13 14:10:32 +1100590 if (conn->version == 0) {
591 if (quiet)
592 debug("Server version does not support lstat operation");
593 else
Damien Miller996acd22003-04-09 20:59:48 +1000594 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000595 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100596 }
597
598 id = conn->msg_id++;
599 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
600 strlen(path));
601
602 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100603}
604
Damien Millercfe23d32008-02-10 22:20:44 +1100605#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100606Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100607do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100608{
609 u_int id;
610
Damien Miller3db5f532002-02-13 14:10:32 +1100611 id = conn->msg_id++;
612 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
613 handle_len);
614
615 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100616}
Damien Millercfe23d32008-02-10 22:20:44 +1100617#endif
Damien Miller33804262001-02-04 23:20:18 +1100618
619int
Damien Miller3db5f532002-02-13 14:10:32 +1100620do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100621{
622 u_int status, id;
623
Damien Miller3db5f532002-02-13 14:10:32 +1100624 id = conn->msg_id++;
625 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100626 strlen(path), a);
627
Damien Miller3db5f532002-02-13 14:10:32 +1100628 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100629 if (status != SSH2_FX_OK)
630 error("Couldn't setstat on \"%s\": %s", path,
631 fx2txt(status));
632
633 return(status);
634}
635
636int
Damien Miller3db5f532002-02-13 14:10:32 +1100637do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100638 Attrib *a)
639{
640 u_int status, id;
641
Damien Miller3db5f532002-02-13 14:10:32 +1100642 id = conn->msg_id++;
643 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100644 handle_len, a);
645
Damien Miller3db5f532002-02-13 14:10:32 +1100646 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100647 if (status != SSH2_FX_OK)
648 error("Couldn't fsetstat: %s", fx2txt(status));
649
650 return(status);
651}
652
653char *
Damien Miller3db5f532002-02-13 14:10:32 +1100654do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100655{
656 Buffer msg;
657 u_int type, expected_id, count, id;
658 char *filename, *longname;
659 Attrib *a;
660
Damien Miller3db5f532002-02-13 14:10:32 +1100661 expected_id = id = conn->msg_id++;
662 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
663 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100664
665 buffer_init(&msg);
666
Damien Miller3db5f532002-02-13 14:10:32 +1100667 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100668 type = buffer_get_char(&msg);
669 id = buffer_get_int(&msg);
670
671 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000672 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100673
674 if (type == SSH2_FXP_STATUS) {
675 u_int status = buffer_get_int(&msg);
676
677 error("Couldn't canonicalise: %s", fx2txt(status));
678 return(NULL);
679 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000680 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100681 SSH2_FXP_NAME, type);
682
683 count = buffer_get_int(&msg);
684 if (count != 1)
685 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
686
687 filename = buffer_get_string(&msg, NULL);
688 longname = buffer_get_string(&msg, NULL);
689 a = decode_attrib(&msg);
690
691 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
692
693 xfree(longname);
694
695 buffer_free(&msg);
696
697 return(filename);
698}
699
700int
Damien Miller3db5f532002-02-13 14:10:32 +1100701do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100702{
703 Buffer msg;
704 u_int status, id;
705
706 buffer_init(&msg);
707
708 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100709 id = conn->msg_id++;
Damien Miller7a3e1d02008-03-27 10:59:57 +1100710 if ((conn->exts & SFTP_EXT_POSIX_RENAME)) {
711 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
712 buffer_put_int(&msg, id);
713 buffer_put_cstring(&msg, "posix-rename@openssh.com");
714 } else {
715 buffer_put_char(&msg, SSH2_FXP_RENAME);
716 buffer_put_int(&msg, id);
717 }
Damien Miller33804262001-02-04 23:20:18 +1100718 buffer_put_cstring(&msg, oldpath);
719 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100720 send_msg(conn->fd_out, &msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100721 debug3("Sent message %s \"%s\" -> \"%s\"",
722 (conn->exts & SFTP_EXT_POSIX_RENAME) ? "posix-rename@openssh.com" :
723 "SSH2_FXP_RENAME", oldpath, newpath);
Damien Miller33804262001-02-04 23:20:18 +1100724 buffer_free(&msg);
725
Damien Miller3db5f532002-02-13 14:10:32 +1100726 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100727 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100728 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
729 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100730
731 return(status);
732}
733
734int
Damien Miller3db5f532002-02-13 14:10:32 +1100735do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100736{
737 Buffer msg;
738 u_int status, id;
739
Damien Miller3db5f532002-02-13 14:10:32 +1100740 if (conn->version < 3) {
741 error("This server does not support the symlink operation");
742 return(SSH2_FX_OP_UNSUPPORTED);
743 }
744
Damien Miller058316f2001-03-08 10:08:49 +1100745 buffer_init(&msg);
746
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000747 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100748 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100749 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
750 buffer_put_int(&msg, id);
751 buffer_put_cstring(&msg, oldpath);
752 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100753 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100754 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
755 newpath);
756 buffer_free(&msg);
757
Damien Miller3db5f532002-02-13 14:10:32 +1100758 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100759 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000760 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100761 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100762
763 return(status);
764}
765
Damien Millercfe23d32008-02-10 22:20:44 +1100766#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +1100767char *
Damien Miller3db5f532002-02-13 14:10:32 +1100768do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100769{
770 Buffer msg;
771 u_int type, expected_id, count, id;
772 char *filename, *longname;
773 Attrib *a;
774
Damien Miller3db5f532002-02-13 14:10:32 +1100775 expected_id = id = conn->msg_id++;
776 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
777 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100778
779 buffer_init(&msg);
780
Damien Miller3db5f532002-02-13 14:10:32 +1100781 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100782 type = buffer_get_char(&msg);
783 id = buffer_get_int(&msg);
784
785 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000786 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100787
788 if (type == SSH2_FXP_STATUS) {
789 u_int status = buffer_get_int(&msg);
790
791 error("Couldn't readlink: %s", fx2txt(status));
792 return(NULL);
793 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000794 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100795 SSH2_FXP_NAME, type);
796
797 count = buffer_get_int(&msg);
798 if (count != 1)
799 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
800
801 filename = buffer_get_string(&msg, NULL);
802 longname = buffer_get_string(&msg, NULL);
803 a = decode_attrib(&msg);
804
805 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
806
807 xfree(longname);
808
809 buffer_free(&msg);
810
811 return(filename);
812}
Damien Millercfe23d32008-02-10 22:20:44 +1100813#endif
Damien Miller058316f2001-03-08 10:08:49 +1100814
Damien Millerd671e5a2008-05-19 14:53:33 +1000815int
Darren Tucker7b598892008-06-09 22:49:36 +1000816do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
Damien Millerd671e5a2008-05-19 14:53:33 +1000817 int quiet)
818{
819 Buffer msg;
820 u_int id;
821
822 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
823 error("Server does not support statvfs@openssh.com extension");
824 return -1;
825 }
826
827 id = conn->msg_id++;
828
829 buffer_init(&msg);
830 buffer_clear(&msg);
831 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
832 buffer_put_int(&msg, id);
833 buffer_put_cstring(&msg, "statvfs@openssh.com");
834 buffer_put_cstring(&msg, path);
835 send_msg(conn->fd_out, &msg);
836 buffer_free(&msg);
837
838 return get_decode_statvfs(conn->fd_in, st, id, quiet);
839}
840
841#ifdef notyet
842int
843do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
Darren Tucker7b598892008-06-09 22:49:36 +1000844 struct sftp_statvfs *st, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000845{
846 Buffer msg;
847 u_int id;
848
849 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
850 error("Server does not support fstatvfs@openssh.com extension");
851 return -1;
852 }
853
854 id = conn->msg_id++;
855
856 buffer_init(&msg);
857 buffer_clear(&msg);
858 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
859 buffer_put_int(&msg, id);
860 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
861 buffer_put_string(&msg, handle, handle_len);
862 send_msg(conn->fd_out, &msg);
863 buffer_free(&msg);
864
865 return get_decode_statvfs(conn->fd_in, st, id, quiet);
866}
867#endif
868
Damien Miller16a13332002-02-13 14:03:56 +1100869static void
870send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
871 char *handle, u_int handle_len)
872{
873 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000874
Damien Miller16a13332002-02-13 14:03:56 +1100875 buffer_init(&msg);
876 buffer_clear(&msg);
877 buffer_put_char(&msg, SSH2_FXP_READ);
878 buffer_put_int(&msg, id);
879 buffer_put_string(&msg, handle, handle_len);
880 buffer_put_int64(&msg, offset);
881 buffer_put_int(&msg, len);
882 send_msg(fd_out, &msg);
883 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000884}
Damien Miller16a13332002-02-13 14:03:56 +1100885
Damien Miller058316f2001-03-08 10:08:49 +1100886int
Damien Miller3db5f532002-02-13 14:10:32 +1100887do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
888 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100889{
Damien Miller33804262001-02-04 23:20:18 +1100890 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100891 Buffer msg;
892 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000893 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100894 int read_error, write_errno;
895 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000896 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100897 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100898 struct request {
899 u_int id;
900 u_int len;
901 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000902 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100903 };
904 TAILQ_HEAD(reqhead, request) requests;
905 struct request *req;
906
907 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100908
Damien Miller3db5f532002-02-13 14:10:32 +1100909 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100910 if (a == NULL)
911 return(-1);
912
913 /* XXX: should we preserve set[ug]id? */
914 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100915 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100916 else
917 mode = 0666;
918
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000919 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100920 (!S_ISREG(a->perm))) {
921 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000922 return(-1);
923 }
924
Damien Miller16a13332002-02-13 14:03:56 +1100925 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
926 size = a->size;
927 else
928 size = 0;
929
Damien Miller3db5f532002-02-13 14:10:32 +1100930 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100931 buffer_init(&msg);
932
933 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100934 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100935 buffer_put_char(&msg, SSH2_FXP_OPEN);
936 buffer_put_int(&msg, id);
937 buffer_put_cstring(&msg, remote_path);
938 buffer_put_int(&msg, SSH2_FXF_READ);
939 attrib_clear(&junk); /* Send empty attributes */
940 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100941 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000942 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100943
Damien Miller3db5f532002-02-13 14:10:32 +1100944 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100945 if (handle == NULL) {
946 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100947 return(-1);
948 }
949
Damien Millera8e06ce2003-11-21 23:48:55 +1100950 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +1100951 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100952 if (local_fd == -1) {
953 error("Couldn't open local file \"%s\" for writing: %s",
954 local_path, strerror(errno));
Damien Miller6b0c8182008-02-10 22:23:41 +1100955 do_close(conn, handle, handle_len);
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000956 buffer_free(&msg);
957 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100958 return(-1);
959 }
960
Damien Miller33804262001-02-04 23:20:18 +1100961 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100962 write_error = read_error = write_errno = num_req = offset = 0;
963 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +1100964 progress_counter = 0;
965
Damien Miller9ba30692004-03-08 23:12:02 +1100966 if (showprogress && size != 0)
967 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +1100968
Damien Miller16a13332002-02-13 14:03:56 +1100969 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100970 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100971 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100972
Darren Tuckercdf547a2004-05-24 10:12:19 +1000973 /*
Darren Tuckerfc959702004-07-17 16:12:08 +1000974 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +1000975 * allow outstanding requests to drain gracefully
976 */
977 if (interrupted) {
978 if (num_req == 0) /* If we haven't started yet... */
979 break;
980 max_req = 0;
981 }
982
Damien Miller16a13332002-02-13 14:03:56 +1100983 /* Send some more requests */
984 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000985 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000986 (unsigned long long)offset,
987 (unsigned long long)offset + buflen - 1,
988 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100989 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100990 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100991 req->len = buflen;
992 req->offset = offset;
993 offset += buflen;
994 num_req++;
995 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000996 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +1100997 req->len, handle, handle_len);
998 }
Damien Miller33804262001-02-04 23:20:18 +1100999
1000 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001001 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +11001002 type = buffer_get_char(&msg);
1003 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001004 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001005
Damien Miller16a13332002-02-13 14:03:56 +11001006 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001007 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001008 req != NULL && req->id != id;
1009 req = TAILQ_NEXT(req, tq))
1010 ;
1011 if (req == NULL)
1012 fatal("Unexpected reply %u", id);
1013
1014 switch (type) {
1015 case SSH2_FXP_STATUS:
1016 status = buffer_get_int(&msg);
1017 if (status != SSH2_FX_EOF)
1018 read_error = 1;
1019 max_req = 0;
1020 TAILQ_REMOVE(&requests, req, tq);
1021 xfree(req);
1022 num_req--;
1023 break;
1024 case SSH2_FXP_DATA:
1025 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +00001026 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001027 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001028 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001029 if (len > req->len)
1030 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +00001031 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001032 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001033 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001034 !write_error) {
1035 write_errno = errno;
1036 write_error = 1;
1037 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001038 }
Damien Miller62d57f62003-01-10 21:43:24 +11001039 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +11001040 xfree(data);
1041
1042 if (len == req->len) {
1043 TAILQ_REMOVE(&requests, req, tq);
1044 xfree(req);
1045 num_req--;
1046 } else {
1047 /* Resend the request for the missing data */
1048 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001049 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001050 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001051 (unsigned long long)req->offset +
1052 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001053 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001054 req->len -= len;
1055 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001056 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001057 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001058 /* Reduce the request size */
1059 if (len < buflen)
1060 buflen = MAX(MIN_READ_SIZE, len);
1061 }
1062 if (max_req > 0) { /* max_req = 0 iff EOF received */
1063 if (size > 0 && offset > size) {
1064 /* Only one request at a time
1065 * after the expected EOF */
1066 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001067 (unsigned long long)offset,
1068 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001069 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001070 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001071 ++max_req;
1072 }
1073 }
1074 break;
1075 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001076 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001077 SSH2_FXP_DATA, type);
1078 }
Damien Miller33804262001-02-04 23:20:18 +11001079 }
Damien Miller33804262001-02-04 23:20:18 +11001080
Damien Miller62d57f62003-01-10 21:43:24 +11001081 if (showprogress && size)
1082 stop_progress_meter();
1083
Damien Miller16a13332002-02-13 14:03:56 +11001084 /* Sanity check */
1085 if (TAILQ_FIRST(&requests) != NULL)
1086 fatal("Transfer complete, but requests still in queue");
1087
1088 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001089 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001090 remote_path, fx2txt(status));
1091 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001092 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001093 error("Couldn't write to \"%s\": %s", local_path,
1094 strerror(write_errno));
1095 status = -1;
1096 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001097 } else {
Damien Miller3db5f532002-02-13 14:10:32 +11001098 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001099
1100 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001101#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +11001102 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001103#else
Damien Miller16a13332002-02-13 14:03:56 +11001104 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001105#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001106 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001107 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001108 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1109 struct timeval tv[2];
1110 tv[0].tv_sec = a->atime;
1111 tv[1].tv_sec = a->mtime;
1112 tv[0].tv_usec = tv[1].tv_usec = 0;
1113 if (utimes(local_path, tv) == -1)
1114 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001115 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001116 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001117 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001118 close(local_fd);
1119 buffer_free(&msg);
1120 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001121
1122 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001123}
1124
1125int
Damien Miller3db5f532002-02-13 14:10:32 +11001126do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
1127 int pflag)
Damien Miller33804262001-02-04 23:20:18 +11001128{
Damien Milleracdf25b2008-02-10 22:27:24 +11001129 int local_fd;
1130 int status = SSH2_FX_OK;
Damien Miller5873dfd2002-02-13 14:04:37 +11001131 u_int handle_len, id, type;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001132 off_t offset;
Damien Miller8829d362002-02-08 22:04:05 +11001133 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +11001134 Buffer msg;
1135 struct stat sb;
1136 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +11001137 u_int32_t startid;
1138 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001139 struct outstanding_ack {
1140 u_int id;
1141 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001142 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001143 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001144 };
1145 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001146 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001147
1148 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001149
1150 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1151 error("Couldn't open local file \"%s\" for reading: %s",
1152 local_path, strerror(errno));
1153 return(-1);
1154 }
1155 if (fstat(local_fd, &sb) == -1) {
1156 error("Couldn't fstat local file \"%s\": %s",
1157 local_path, strerror(errno));
1158 close(local_fd);
1159 return(-1);
1160 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001161 if (!S_ISREG(sb.st_mode)) {
1162 error("%s is not a regular file", local_path);
1163 close(local_fd);
1164 return(-1);
1165 }
Damien Miller33804262001-02-04 23:20:18 +11001166 stat_to_attrib(&sb, &a);
1167
1168 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1169 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1170 a.perm &= 0777;
1171 if (!pflag)
1172 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1173
1174 buffer_init(&msg);
1175
1176 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001177 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001178 buffer_put_char(&msg, SSH2_FXP_OPEN);
1179 buffer_put_int(&msg, id);
1180 buffer_put_cstring(&msg, remote_path);
1181 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1182 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001183 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001184 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001185
1186 buffer_clear(&msg);
1187
Damien Miller3db5f532002-02-13 14:10:32 +11001188 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001189 if (handle == NULL) {
1190 close(local_fd);
1191 buffer_free(&msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001192 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001193 }
1194
Damien Miller16a13332002-02-13 14:03:56 +11001195 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001196 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001197
Damien Miller33804262001-02-04 23:20:18 +11001198 /* Read from local and write to remote */
1199 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001200 if (showprogress)
1201 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001202
Damien Miller9f0f5c62001-12-21 14:45:46 +11001203 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001204 int len;
Damien Miller33804262001-02-04 23:20:18 +11001205
1206 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001207 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001208 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001209 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001210 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001211 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001212 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001213 len = 0;
1214 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001215 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001216 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1217
1218 if (len == -1)
1219 fatal("Couldn't read from \"%s\": %s", local_path,
1220 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001221
1222 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001223 ack = xmalloc(sizeof(*ack));
1224 ack->id = ++id;
1225 ack->offset = offset;
1226 ack->len = len;
1227 TAILQ_INSERT_TAIL(&acks, ack, tq);
1228
Damien Miller16a13332002-02-13 14:03:56 +11001229 buffer_clear(&msg);
1230 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001231 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001232 buffer_put_string(&msg, handle, handle_len);
1233 buffer_put_int64(&msg, offset);
1234 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001235 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001236 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001237 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001238 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001239 break;
1240
Damien Miller5873dfd2002-02-13 14:04:37 +11001241 if (ack == NULL)
1242 fatal("Unexpected ACK %u", id);
1243
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001244 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001245 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001246 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001247
Damien Miller5873dfd2002-02-13 14:04:37 +11001248 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001249 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001250 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001251 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001252
1253 if (type != SSH2_FXP_STATUS)
1254 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1255 "got %d", SSH2_FXP_STATUS, type);
1256
1257 status = buffer_get_int(&msg);
1258 debug3("SSH2_FXP_STATUS %d", status);
1259
1260 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001261 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001262 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001263 ack = TAILQ_NEXT(ack, tq))
1264 ;
1265 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001266 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001267 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001268 debug3("In write loop, ack for %u %u bytes at %lld",
1269 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001270 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001271 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001272 }
Damien Miller33804262001-02-04 23:20:18 +11001273 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001274 if (offset < 0)
1275 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001276 }
Damien Milleracdf25b2008-02-10 22:27:24 +11001277 buffer_free(&msg);
1278
Damien Miller62d57f62003-01-10 21:43:24 +11001279 if (showprogress)
1280 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001281 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001282
Damien Milleracdf25b2008-02-10 22:27:24 +11001283 if (status != SSH2_FX_OK) {
1284 error("Couldn't write to remote file \"%s\": %s",
1285 remote_path, fx2txt(status));
1286 status = -1;
1287 }
1288
Damien Miller33804262001-02-04 23:20:18 +11001289 if (close(local_fd) == -1) {
1290 error("Couldn't close local file \"%s\": %s", local_path,
1291 strerror(errno));
Damien Millerd7686fd2001-02-10 00:40:03 +11001292 status = -1;
Damien Miller33804262001-02-04 23:20:18 +11001293 }
1294
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001295 /* Override umask and utimes if asked */
1296 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001297 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001298
Damien Milleracdf25b2008-02-10 22:27:24 +11001299 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
1300 status = -1;
Damien Millerd7686fd2001-02-10 00:40:03 +11001301 xfree(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001302
1303 return status;
Damien Miller33804262001-02-04 23:20:18 +11001304}