blob: 8593cb8ceb81069395b5fd8c4441e6bde5862834 [file] [log] [blame]
Damien Millerd671e5a2008-05-19 14:53:33 +10001/* $OpenBSD: sftp-client.c,v 1.82 2008/04/18 12:32:11 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
Darren Tucker5b2e2ba2008-06-08 09:25:28 +1000246#ifdef USE_STATVFS
Damien Millerd671e5a2008-05-19 14:53:33 +1000247static int
248get_decode_statvfs(int fd, struct statvfs *st, u_int expected_id, int quiet)
249{
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));
277 st->f_bsize = buffer_get_int(&msg);
278 st->f_frsize = buffer_get_int(&msg);
279 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);
285 st->f_fsid = buffer_get_int(&msg);
286 flag = buffer_get_int(&msg);
287 st->f_namemax = buffer_get_int(&msg);
288
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}
Darren Tucker5b2e2ba2008-06-08 09:25:28 +1000296#endif
Damien Millerd671e5a2008-05-19 14:53:33 +1000297
Damien Miller3db5f532002-02-13 14:10:32 +1100298struct sftp_conn *
299do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100300{
Damien Miller7a3e1d02008-03-27 10:59:57 +1100301 u_int type, exts = 0;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000302 int version;
Damien Miller33804262001-02-04 23:20:18 +1100303 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100304 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100305
306 buffer_init(&msg);
307 buffer_put_char(&msg, SSH2_FXP_INIT);
308 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
309 send_msg(fd_out, &msg);
310
311 buffer_clear(&msg);
312
313 get_msg(fd_in, &msg);
314
Kevin Stevesef4eea92001-02-05 12:42:17 +0000315 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100316 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000317 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100318 type);
319 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100320 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100321 }
322 version = buffer_get_int(&msg);
323
324 debug2("Remote version: %d", version);
325
326 /* Check for extensions */
327 while (buffer_len(&msg) > 0) {
328 char *name = buffer_get_string(&msg, NULL);
329 char *value = buffer_get_string(&msg, NULL);
330
331 debug2("Init extension: \"%s\"", name);
Damien Millerd671e5a2008-05-19 14:53:33 +1000332 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
333 strcmp(value, "1") == 0)
Damien Miller7a3e1d02008-03-27 10:59:57 +1100334 exts |= SFTP_EXT_POSIX_RENAME;
Damien Millerd671e5a2008-05-19 14:53:33 +1000335 if (strcmp(name, "statvfs@openssh.com") == 0 &&
336 strcmp(value, "1") == 0)
337 exts |= SFTP_EXT_STATVFS;
338 if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
339 strcmp(value, "1") == 0)
340 exts |= SFTP_EXT_FSTATVFS;
Damien Miller33804262001-02-04 23:20:18 +1100341 xfree(name);
342 xfree(value);
343 }
344
345 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100346
Damien Miller3db5f532002-02-13 14:10:32 +1100347 ret = xmalloc(sizeof(*ret));
348 ret->fd_in = fd_in;
349 ret->fd_out = fd_out;
350 ret->transfer_buflen = transfer_buflen;
351 ret->num_requests = num_requests;
352 ret->version = version;
353 ret->msg_id = 1;
Damien Miller7a3e1d02008-03-27 10:59:57 +1100354 ret->exts = exts;
Damien Miller3db5f532002-02-13 14:10:32 +1100355
356 /* Some filexfer v.0 servers don't support large packets */
357 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000358 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100359
360 return(ret);
361}
362
363u_int
364sftp_proto_version(struct sftp_conn *conn)
365{
366 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100367}
368
369int
Damien Miller3db5f532002-02-13 14:10:32 +1100370do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100371{
372 u_int id, status;
373 Buffer msg;
374
375 buffer_init(&msg);
376
Damien Miller3db5f532002-02-13 14:10:32 +1100377 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100378 buffer_put_char(&msg, SSH2_FXP_CLOSE);
379 buffer_put_int(&msg, id);
380 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100381 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000382 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100383
Damien Miller3db5f532002-02-13 14:10:32 +1100384 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100385 if (status != SSH2_FX_OK)
386 error("Couldn't close file: %s", fx2txt(status));
387
388 buffer_free(&msg);
389
390 return(status);
391}
392
Damien Miller4870afd2001-03-14 10:27:09 +1100393
Ben Lindstrombba81212001-06-25 05:01:22 +0000394static int
Damien Miller3db5f532002-02-13 14:10:32 +1100395do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100396 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100397{
398 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000399 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100400 char *handle;
401
Damien Miller3db5f532002-02-13 14:10:32 +1100402 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100403
404 buffer_init(&msg);
405 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
406 buffer_put_int(&msg, id);
407 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100408 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100409
410 buffer_clear(&msg);
411
Damien Miller3db5f532002-02-13 14:10:32 +1100412 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100413 if (handle == NULL)
414 return(-1);
415
Damien Miller4870afd2001-03-14 10:27:09 +1100416 if (dir) {
417 ents = 0;
418 *dir = xmalloc(sizeof(**dir));
419 (*dir)[0] = NULL;
420 }
Damien Miller4870afd2001-03-14 10:27:09 +1100421
Darren Tuckercdf547a2004-05-24 10:12:19 +1000422 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100423 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100424
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000425 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100426
427 buffer_clear(&msg);
428 buffer_put_char(&msg, SSH2_FXP_READDIR);
429 buffer_put_int(&msg, id);
430 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100431 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100432
433 buffer_clear(&msg);
434
Damien Miller3db5f532002-02-13 14:10:32 +1100435 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100436
437 type = buffer_get_char(&msg);
438 id = buffer_get_int(&msg);
439
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000440 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100441
442 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000443 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100444
445 if (type == SSH2_FXP_STATUS) {
446 int status = buffer_get_int(&msg);
447
448 debug3("Received SSH2_FXP_STATUS %d", status);
449
450 if (status == SSH2_FX_EOF) {
451 break;
452 } else {
453 error("Couldn't read directory: %s",
454 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100455 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100456 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000457 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100458 }
459 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000460 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100461 SSH2_FXP_NAME, type);
462
463 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100464 if (count == 0)
465 break;
466 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100467 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100468 char *filename, *longname;
469 Attrib *a;
470
471 filename = buffer_get_string(&msg, NULL);
472 longname = buffer_get_string(&msg, NULL);
473 a = decode_attrib(&msg);
474
Damien Miller4870afd2001-03-14 10:27:09 +1100475 if (printflag)
476 printf("%s\n", longname);
477
478 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100479 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100480 (*dir)[ents] = xmalloc(sizeof(***dir));
481 (*dir)[ents]->filename = xstrdup(filename);
482 (*dir)[ents]->longname = xstrdup(longname);
483 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
484 (*dir)[++ents] = NULL;
485 }
Damien Miller33804262001-02-04 23:20:18 +1100486
487 xfree(filename);
488 xfree(longname);
489 }
490 }
491
492 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100493 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100494 xfree(handle);
495
Darren Tuckercdf547a2004-05-24 10:12:19 +1000496 /* Don't return partial matches on interrupt */
497 if (interrupted && dir != NULL && *dir != NULL) {
498 free_sftp_dirents(*dir);
499 *dir = xmalloc(sizeof(**dir));
500 **dir = NULL;
501 }
502
Damien Miller33804262001-02-04 23:20:18 +1100503 return(0);
504}
505
506int
Damien Miller3db5f532002-02-13 14:10:32 +1100507do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100508{
Damien Miller3db5f532002-02-13 14:10:32 +1100509 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100510}
511
512void free_sftp_dirents(SFTP_DIRENT **s)
513{
514 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100515
516 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100517 xfree(s[i]->filename);
518 xfree(s[i]->longname);
519 xfree(s[i]);
520 }
521 xfree(s);
522}
523
524int
Damien Miller3db5f532002-02-13 14:10:32 +1100525do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100526{
527 u_int status, id;
528
529 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
530
Damien Miller3db5f532002-02-13 14:10:32 +1100531 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000532 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100533 strlen(path));
534 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100535 if (status != SSH2_FX_OK)
536 error("Couldn't delete file: %s", fx2txt(status));
537 return(status);
538}
539
540int
Damien Miller3db5f532002-02-13 14:10:32 +1100541do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100542{
543 u_int status, id;
544
Damien Miller3db5f532002-02-13 14:10:32 +1100545 id = conn->msg_id++;
546 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100547 strlen(path), a);
548
Damien Miller3db5f532002-02-13 14:10:32 +1100549 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100550 if (status != SSH2_FX_OK)
551 error("Couldn't create directory: %s", fx2txt(status));
552
553 return(status);
554}
555
556int
Damien Miller3db5f532002-02-13 14:10:32 +1100557do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100558{
559 u_int status, id;
560
Damien Miller3db5f532002-02-13 14:10:32 +1100561 id = conn->msg_id++;
562 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
563 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100564
Damien Miller3db5f532002-02-13 14:10:32 +1100565 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100566 if (status != SSH2_FX_OK)
567 error("Couldn't remove directory: %s", fx2txt(status));
568
569 return(status);
570}
571
572Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100573do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100574{
575 u_int id;
576
Damien Miller3db5f532002-02-13 14:10:32 +1100577 id = conn->msg_id++;
578
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000579 send_string_request(conn->fd_out, id,
580 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100581 path, strlen(path));
582
583 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100584}
585
586Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100587do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100588{
589 u_int id;
590
Damien Miller3db5f532002-02-13 14:10:32 +1100591 if (conn->version == 0) {
592 if (quiet)
593 debug("Server version does not support lstat operation");
594 else
Damien Miller996acd22003-04-09 20:59:48 +1000595 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000596 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100597 }
598
599 id = conn->msg_id++;
600 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
601 strlen(path));
602
603 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100604}
605
Damien Millercfe23d32008-02-10 22:20:44 +1100606#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100607Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100608do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100609{
610 u_int id;
611
Damien Miller3db5f532002-02-13 14:10:32 +1100612 id = conn->msg_id++;
613 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
614 handle_len);
615
616 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100617}
Damien Millercfe23d32008-02-10 22:20:44 +1100618#endif
Damien Miller33804262001-02-04 23:20:18 +1100619
620int
Damien Miller3db5f532002-02-13 14:10:32 +1100621do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100622{
623 u_int status, id;
624
Damien Miller3db5f532002-02-13 14:10:32 +1100625 id = conn->msg_id++;
626 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100627 strlen(path), a);
628
Damien Miller3db5f532002-02-13 14:10:32 +1100629 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100630 if (status != SSH2_FX_OK)
631 error("Couldn't setstat on \"%s\": %s", path,
632 fx2txt(status));
633
634 return(status);
635}
636
637int
Damien Miller3db5f532002-02-13 14:10:32 +1100638do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100639 Attrib *a)
640{
641 u_int status, id;
642
Damien Miller3db5f532002-02-13 14:10:32 +1100643 id = conn->msg_id++;
644 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100645 handle_len, a);
646
Damien Miller3db5f532002-02-13 14:10:32 +1100647 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100648 if (status != SSH2_FX_OK)
649 error("Couldn't fsetstat: %s", fx2txt(status));
650
651 return(status);
652}
653
654char *
Damien Miller3db5f532002-02-13 14:10:32 +1100655do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100656{
657 Buffer msg;
658 u_int type, expected_id, count, id;
659 char *filename, *longname;
660 Attrib *a;
661
Damien Miller3db5f532002-02-13 14:10:32 +1100662 expected_id = id = conn->msg_id++;
663 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
664 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100665
666 buffer_init(&msg);
667
Damien Miller3db5f532002-02-13 14:10:32 +1100668 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100669 type = buffer_get_char(&msg);
670 id = buffer_get_int(&msg);
671
672 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000673 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100674
675 if (type == SSH2_FXP_STATUS) {
676 u_int status = buffer_get_int(&msg);
677
678 error("Couldn't canonicalise: %s", fx2txt(status));
679 return(NULL);
680 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000681 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100682 SSH2_FXP_NAME, type);
683
684 count = buffer_get_int(&msg);
685 if (count != 1)
686 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
687
688 filename = buffer_get_string(&msg, NULL);
689 longname = buffer_get_string(&msg, NULL);
690 a = decode_attrib(&msg);
691
692 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
693
694 xfree(longname);
695
696 buffer_free(&msg);
697
698 return(filename);
699}
700
701int
Damien Miller3db5f532002-02-13 14:10:32 +1100702do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100703{
704 Buffer msg;
705 u_int status, id;
706
707 buffer_init(&msg);
708
709 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100710 id = conn->msg_id++;
Damien Miller7a3e1d02008-03-27 10:59:57 +1100711 if ((conn->exts & SFTP_EXT_POSIX_RENAME)) {
712 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
713 buffer_put_int(&msg, id);
714 buffer_put_cstring(&msg, "posix-rename@openssh.com");
715 } else {
716 buffer_put_char(&msg, SSH2_FXP_RENAME);
717 buffer_put_int(&msg, id);
718 }
Damien Miller33804262001-02-04 23:20:18 +1100719 buffer_put_cstring(&msg, oldpath);
720 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100721 send_msg(conn->fd_out, &msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100722 debug3("Sent message %s \"%s\" -> \"%s\"",
723 (conn->exts & SFTP_EXT_POSIX_RENAME) ? "posix-rename@openssh.com" :
724 "SSH2_FXP_RENAME", oldpath, newpath);
Damien Miller33804262001-02-04 23:20:18 +1100725 buffer_free(&msg);
726
Damien Miller3db5f532002-02-13 14:10:32 +1100727 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100728 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100729 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
730 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100731
732 return(status);
733}
734
735int
Damien Miller3db5f532002-02-13 14:10:32 +1100736do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100737{
738 Buffer msg;
739 u_int status, id;
740
Damien Miller3db5f532002-02-13 14:10:32 +1100741 if (conn->version < 3) {
742 error("This server does not support the symlink operation");
743 return(SSH2_FX_OP_UNSUPPORTED);
744 }
745
Damien Miller058316f2001-03-08 10:08:49 +1100746 buffer_init(&msg);
747
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000748 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100749 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100750 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
751 buffer_put_int(&msg, id);
752 buffer_put_cstring(&msg, oldpath);
753 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100754 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100755 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
756 newpath);
757 buffer_free(&msg);
758
Damien Miller3db5f532002-02-13 14:10:32 +1100759 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100760 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000761 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100762 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100763
764 return(status);
765}
766
Damien Millercfe23d32008-02-10 22:20:44 +1100767#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +1100768char *
Damien Miller3db5f532002-02-13 14:10:32 +1100769do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100770{
771 Buffer msg;
772 u_int type, expected_id, count, id;
773 char *filename, *longname;
774 Attrib *a;
775
Damien Miller3db5f532002-02-13 14:10:32 +1100776 expected_id = id = conn->msg_id++;
777 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
778 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100779
780 buffer_init(&msg);
781
Damien Miller3db5f532002-02-13 14:10:32 +1100782 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100783 type = buffer_get_char(&msg);
784 id = buffer_get_int(&msg);
785
786 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000787 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100788
789 if (type == SSH2_FXP_STATUS) {
790 u_int status = buffer_get_int(&msg);
791
792 error("Couldn't readlink: %s", fx2txt(status));
793 return(NULL);
794 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000795 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100796 SSH2_FXP_NAME, type);
797
798 count = buffer_get_int(&msg);
799 if (count != 1)
800 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
801
802 filename = buffer_get_string(&msg, NULL);
803 longname = buffer_get_string(&msg, NULL);
804 a = decode_attrib(&msg);
805
806 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
807
808 xfree(longname);
809
810 buffer_free(&msg);
811
812 return(filename);
813}
Damien Millercfe23d32008-02-10 22:20:44 +1100814#endif
Damien Miller058316f2001-03-08 10:08:49 +1100815
Darren Tucker5b2e2ba2008-06-08 09:25:28 +1000816#ifdef USE_STATVFS
Damien Millerd671e5a2008-05-19 14:53:33 +1000817int
818do_statvfs(struct sftp_conn *conn, const char *path, struct statvfs *st,
819 int quiet)
820{
821 Buffer msg;
822 u_int id;
823
824 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
825 error("Server does not support statvfs@openssh.com extension");
826 return -1;
827 }
828
829 id = conn->msg_id++;
830
831 buffer_init(&msg);
832 buffer_clear(&msg);
833 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
834 buffer_put_int(&msg, id);
835 buffer_put_cstring(&msg, "statvfs@openssh.com");
836 buffer_put_cstring(&msg, path);
837 send_msg(conn->fd_out, &msg);
838 buffer_free(&msg);
839
840 return get_decode_statvfs(conn->fd_in, st, id, quiet);
841}
Darren Tucker5b2e2ba2008-06-08 09:25:28 +1000842#endif
Damien Millerd671e5a2008-05-19 14:53:33 +1000843
844#ifdef notyet
845int
846do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
847 struct statvfs *st, int quiet)
848{
849 Buffer msg;
850 u_int id;
851
852 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
853 error("Server does not support fstatvfs@openssh.com extension");
854 return -1;
855 }
856
857 id = conn->msg_id++;
858
859 buffer_init(&msg);
860 buffer_clear(&msg);
861 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
862 buffer_put_int(&msg, id);
863 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
864 buffer_put_string(&msg, handle, handle_len);
865 send_msg(conn->fd_out, &msg);
866 buffer_free(&msg);
867
868 return get_decode_statvfs(conn->fd_in, st, id, quiet);
869}
870#endif
871
Damien Miller16a13332002-02-13 14:03:56 +1100872static void
873send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
874 char *handle, u_int handle_len)
875{
876 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000877
Damien Miller16a13332002-02-13 14:03:56 +1100878 buffer_init(&msg);
879 buffer_clear(&msg);
880 buffer_put_char(&msg, SSH2_FXP_READ);
881 buffer_put_int(&msg, id);
882 buffer_put_string(&msg, handle, handle_len);
883 buffer_put_int64(&msg, offset);
884 buffer_put_int(&msg, len);
885 send_msg(fd_out, &msg);
886 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000887}
Damien Miller16a13332002-02-13 14:03:56 +1100888
Damien Miller058316f2001-03-08 10:08:49 +1100889int
Damien Miller3db5f532002-02-13 14:10:32 +1100890do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
891 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100892{
Damien Miller33804262001-02-04 23:20:18 +1100893 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100894 Buffer msg;
895 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000896 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100897 int read_error, write_errno;
898 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000899 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100900 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100901 struct request {
902 u_int id;
903 u_int len;
904 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000905 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100906 };
907 TAILQ_HEAD(reqhead, request) requests;
908 struct request *req;
909
910 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100911
Damien Miller3db5f532002-02-13 14:10:32 +1100912 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100913 if (a == NULL)
914 return(-1);
915
916 /* XXX: should we preserve set[ug]id? */
917 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100918 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100919 else
920 mode = 0666;
921
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000922 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100923 (!S_ISREG(a->perm))) {
924 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000925 return(-1);
926 }
927
Damien Miller16a13332002-02-13 14:03:56 +1100928 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
929 size = a->size;
930 else
931 size = 0;
932
Damien Miller3db5f532002-02-13 14:10:32 +1100933 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100934 buffer_init(&msg);
935
936 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100937 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100938 buffer_put_char(&msg, SSH2_FXP_OPEN);
939 buffer_put_int(&msg, id);
940 buffer_put_cstring(&msg, remote_path);
941 buffer_put_int(&msg, SSH2_FXF_READ);
942 attrib_clear(&junk); /* Send empty attributes */
943 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100944 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000945 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100946
Damien Miller3db5f532002-02-13 14:10:32 +1100947 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100948 if (handle == NULL) {
949 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100950 return(-1);
951 }
952
Damien Millera8e06ce2003-11-21 23:48:55 +1100953 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +1100954 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100955 if (local_fd == -1) {
956 error("Couldn't open local file \"%s\" for writing: %s",
957 local_path, strerror(errno));
Damien Miller6b0c8182008-02-10 22:23:41 +1100958 do_close(conn, handle, handle_len);
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000959 buffer_free(&msg);
960 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100961 return(-1);
962 }
963
Damien Miller33804262001-02-04 23:20:18 +1100964 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100965 write_error = read_error = write_errno = num_req = offset = 0;
966 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +1100967 progress_counter = 0;
968
Damien Miller9ba30692004-03-08 23:12:02 +1100969 if (showprogress && size != 0)
970 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +1100971
Damien Miller16a13332002-02-13 14:03:56 +1100972 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100973 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100974 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100975
Darren Tuckercdf547a2004-05-24 10:12:19 +1000976 /*
Darren Tuckerfc959702004-07-17 16:12:08 +1000977 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +1000978 * allow outstanding requests to drain gracefully
979 */
980 if (interrupted) {
981 if (num_req == 0) /* If we haven't started yet... */
982 break;
983 max_req = 0;
984 }
985
Damien Miller16a13332002-02-13 14:03:56 +1100986 /* Send some more requests */
987 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000988 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000989 (unsigned long long)offset,
990 (unsigned long long)offset + buflen - 1,
991 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100992 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100993 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100994 req->len = buflen;
995 req->offset = offset;
996 offset += buflen;
997 num_req++;
998 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000999 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001000 req->len, handle, handle_len);
1001 }
Damien Miller33804262001-02-04 23:20:18 +11001002
1003 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001004 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +11001005 type = buffer_get_char(&msg);
1006 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001007 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001008
Damien Miller16a13332002-02-13 14:03:56 +11001009 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001010 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001011 req != NULL && req->id != id;
1012 req = TAILQ_NEXT(req, tq))
1013 ;
1014 if (req == NULL)
1015 fatal("Unexpected reply %u", id);
1016
1017 switch (type) {
1018 case SSH2_FXP_STATUS:
1019 status = buffer_get_int(&msg);
1020 if (status != SSH2_FX_EOF)
1021 read_error = 1;
1022 max_req = 0;
1023 TAILQ_REMOVE(&requests, req, tq);
1024 xfree(req);
1025 num_req--;
1026 break;
1027 case SSH2_FXP_DATA:
1028 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +00001029 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001030 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001031 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001032 if (len > req->len)
1033 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +00001034 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001035 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001036 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001037 !write_error) {
1038 write_errno = errno;
1039 write_error = 1;
1040 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001041 }
Damien Miller62d57f62003-01-10 21:43:24 +11001042 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +11001043 xfree(data);
1044
1045 if (len == req->len) {
1046 TAILQ_REMOVE(&requests, req, tq);
1047 xfree(req);
1048 num_req--;
1049 } else {
1050 /* Resend the request for the missing data */
1051 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001052 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001053 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001054 (unsigned long long)req->offset +
1055 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001056 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001057 req->len -= len;
1058 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001059 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001060 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001061 /* Reduce the request size */
1062 if (len < buflen)
1063 buflen = MAX(MIN_READ_SIZE, len);
1064 }
1065 if (max_req > 0) { /* max_req = 0 iff EOF received */
1066 if (size > 0 && offset > size) {
1067 /* Only one request at a time
1068 * after the expected EOF */
1069 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001070 (unsigned long long)offset,
1071 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001072 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001073 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001074 ++max_req;
1075 }
1076 }
1077 break;
1078 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001079 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001080 SSH2_FXP_DATA, type);
1081 }
Damien Miller33804262001-02-04 23:20:18 +11001082 }
Damien Miller33804262001-02-04 23:20:18 +11001083
Damien Miller62d57f62003-01-10 21:43:24 +11001084 if (showprogress && size)
1085 stop_progress_meter();
1086
Damien Miller16a13332002-02-13 14:03:56 +11001087 /* Sanity check */
1088 if (TAILQ_FIRST(&requests) != NULL)
1089 fatal("Transfer complete, but requests still in queue");
1090
1091 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001092 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001093 remote_path, fx2txt(status));
1094 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001095 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001096 error("Couldn't write to \"%s\": %s", local_path,
1097 strerror(write_errno));
1098 status = -1;
1099 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001100 } else {
Damien Miller3db5f532002-02-13 14:10:32 +11001101 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001102
1103 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001104#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +11001105 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001106#else
Damien Miller16a13332002-02-13 14:03:56 +11001107 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001108#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001109 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001110 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001111 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1112 struct timeval tv[2];
1113 tv[0].tv_sec = a->atime;
1114 tv[1].tv_sec = a->mtime;
1115 tv[0].tv_usec = tv[1].tv_usec = 0;
1116 if (utimes(local_path, tv) == -1)
1117 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001118 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001119 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001120 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001121 close(local_fd);
1122 buffer_free(&msg);
1123 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001124
1125 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001126}
1127
1128int
Damien Miller3db5f532002-02-13 14:10:32 +11001129do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
1130 int pflag)
Damien Miller33804262001-02-04 23:20:18 +11001131{
Damien Milleracdf25b2008-02-10 22:27:24 +11001132 int local_fd;
1133 int status = SSH2_FX_OK;
Damien Miller5873dfd2002-02-13 14:04:37 +11001134 u_int handle_len, id, type;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001135 off_t offset;
Damien Miller8829d362002-02-08 22:04:05 +11001136 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +11001137 Buffer msg;
1138 struct stat sb;
1139 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +11001140 u_int32_t startid;
1141 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001142 struct outstanding_ack {
1143 u_int id;
1144 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001145 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001146 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001147 };
1148 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001149 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001150
1151 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001152
1153 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1154 error("Couldn't open local file \"%s\" for reading: %s",
1155 local_path, strerror(errno));
1156 return(-1);
1157 }
1158 if (fstat(local_fd, &sb) == -1) {
1159 error("Couldn't fstat local file \"%s\": %s",
1160 local_path, strerror(errno));
1161 close(local_fd);
1162 return(-1);
1163 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001164 if (!S_ISREG(sb.st_mode)) {
1165 error("%s is not a regular file", local_path);
1166 close(local_fd);
1167 return(-1);
1168 }
Damien Miller33804262001-02-04 23:20:18 +11001169 stat_to_attrib(&sb, &a);
1170
1171 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1172 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1173 a.perm &= 0777;
1174 if (!pflag)
1175 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1176
1177 buffer_init(&msg);
1178
1179 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001180 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001181 buffer_put_char(&msg, SSH2_FXP_OPEN);
1182 buffer_put_int(&msg, id);
1183 buffer_put_cstring(&msg, remote_path);
1184 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1185 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001186 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001187 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001188
1189 buffer_clear(&msg);
1190
Damien Miller3db5f532002-02-13 14:10:32 +11001191 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001192 if (handle == NULL) {
1193 close(local_fd);
1194 buffer_free(&msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001195 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001196 }
1197
Damien Miller16a13332002-02-13 14:03:56 +11001198 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001199 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001200
Damien Miller33804262001-02-04 23:20:18 +11001201 /* Read from local and write to remote */
1202 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001203 if (showprogress)
1204 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001205
Damien Miller9f0f5c62001-12-21 14:45:46 +11001206 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001207 int len;
Damien Miller33804262001-02-04 23:20:18 +11001208
1209 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001210 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001211 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001212 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001213 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001214 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001215 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001216 len = 0;
1217 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001218 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001219 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1220
1221 if (len == -1)
1222 fatal("Couldn't read from \"%s\": %s", local_path,
1223 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001224
1225 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001226 ack = xmalloc(sizeof(*ack));
1227 ack->id = ++id;
1228 ack->offset = offset;
1229 ack->len = len;
1230 TAILQ_INSERT_TAIL(&acks, ack, tq);
1231
Damien Miller16a13332002-02-13 14:03:56 +11001232 buffer_clear(&msg);
1233 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001234 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001235 buffer_put_string(&msg, handle, handle_len);
1236 buffer_put_int64(&msg, offset);
1237 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001238 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001239 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001240 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001241 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001242 break;
1243
Damien Miller5873dfd2002-02-13 14:04:37 +11001244 if (ack == NULL)
1245 fatal("Unexpected ACK %u", id);
1246
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001247 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001248 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001249 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001250
Damien Miller5873dfd2002-02-13 14:04:37 +11001251 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001252 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001253 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001254 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001255
1256 if (type != SSH2_FXP_STATUS)
1257 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1258 "got %d", SSH2_FXP_STATUS, type);
1259
1260 status = buffer_get_int(&msg);
1261 debug3("SSH2_FXP_STATUS %d", status);
1262
1263 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001264 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001265 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001266 ack = TAILQ_NEXT(ack, tq))
1267 ;
1268 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001269 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001270 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001271 debug3("In write loop, ack for %u %u bytes at %lld",
1272 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001273 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001274 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001275 }
Damien Miller33804262001-02-04 23:20:18 +11001276 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001277 if (offset < 0)
1278 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001279 }
Damien Milleracdf25b2008-02-10 22:27:24 +11001280 buffer_free(&msg);
1281
Damien Miller62d57f62003-01-10 21:43:24 +11001282 if (showprogress)
1283 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001284 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001285
Damien Milleracdf25b2008-02-10 22:27:24 +11001286 if (status != SSH2_FX_OK) {
1287 error("Couldn't write to remote file \"%s\": %s",
1288 remote_path, fx2txt(status));
1289 status = -1;
1290 }
1291
Damien Miller33804262001-02-04 23:20:18 +11001292 if (close(local_fd) == -1) {
1293 error("Couldn't close local file \"%s\": %s", local_path,
1294 strerror(errno));
Damien Millerd7686fd2001-02-10 00:40:03 +11001295 status = -1;
Damien Miller33804262001-02-04 23:20:18 +11001296 }
1297
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001298 /* Override umask and utimes if asked */
1299 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001300 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001301
Damien Milleracdf25b2008-02-10 22:27:24 +11001302 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
1303 status = -1;
Damien Millerd7686fd2001-02-10 00:40:03 +11001304 xfree(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001305
1306 return status;
Damien Miller33804262001-02-04 23:20:18 +11001307}