blob: 1e54348b7e24b1c9904abe3724acd7da79194b16 [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>
Damien Millerd671e5a2008-05-19 14:53:33 +100027#include <sys/statvfs.h>
Damien Millerd7834352006-08-05 12:39:39 +100028#include "openbsd-compat/sys-queue.h"
Damien Millerf17883e2006-03-15 11:45:54 +110029#ifdef HAVE_SYS_STAT_H
30# include <sys/stat.h>
31#endif
Damien Miller9aec9192006-08-05 10:57:45 +100032#ifdef HAVE_SYS_TIME_H
33# include <sys/time.h>
34#endif
Damien Millerd7834352006-08-05 12:39:39 +100035#include <sys/uio.h>
Damien Miller57cf6382006-07-10 21:13:46 +100036
Darren Tucker39972492006-07-12 22:22:46 +100037#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100038#include <fcntl.h>
39#include <signal.h>
Damien Millerd7834352006-08-05 12:39:39 +100040#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100041#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100042#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100043#include <unistd.h>
Damien Miller16a13332002-02-13 14:03:56 +110044
Damien Miller33804262001-02-04 23:20:18 +110045#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100046#include "buffer.h"
Damien Miller33804262001-02-04 23:20:18 +110047#include "log.h"
48#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110049#include "progressmeter.h"
Damien Miller3f941882006-03-31 23:13:02 +110050#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110051
52#include "sftp.h"
53#include "sftp-common.h"
54#include "sftp-client.h"
55
Darren Tuckercdf547a2004-05-24 10:12:19 +100056extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110057extern int showprogress;
58
Damien Miller0c8d8f62006-03-15 11:34:25 +110059/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110060#define MIN_READ_SIZE 512
61
Damien Miller3db5f532002-02-13 14:10:32 +110062struct sftp_conn {
63 int fd_in;
64 int fd_out;
65 u_int transfer_buflen;
66 u_int num_requests;
67 u_int version;
68 u_int msg_id;
Damien Millerd671e5a2008-05-19 14:53:33 +100069#define SFTP_EXT_POSIX_RENAME 0x00000001
70#define SFTP_EXT_STATVFS 0x00000002
71#define SFTP_EXT_FSTATVFS 0x00000004
Damien Miller7a3e1d02008-03-27 10:59:57 +110072 u_int exts;
Damien Miller3db5f532002-02-13 14:10:32 +110073};
Ben Lindstrom288cc392001-02-09 02:58:04 +000074
Ben Lindstrombba81212001-06-25 05:01:22 +000075static void
Damien Miller33804262001-02-04 23:20:18 +110076send_msg(int fd, Buffer *m)
77{
Damien Millera7f3aaa2003-01-10 21:43:58 +110078 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +100079 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +110080
Damien Miller54446182006-01-02 23:40:50 +110081 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +110082 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +110083
Damien Millera7f3aaa2003-01-10 21:43:58 +110084 /* Send length first */
Damien Miller3f941882006-03-31 23:13:02 +110085 put_u32(mlen, buffer_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +100086 iov[0].iov_base = mlen;
87 iov[0].iov_len = sizeof(mlen);
88 iov[1].iov_base = buffer_ptr(m);
89 iov[1].iov_len = buffer_len(m);
Damien Millerd7834352006-08-05 12:39:39 +100090
Damien Miller58ca98b2006-04-23 12:06:35 +100091 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +110092 fatal("Couldn't send packet: %s", strerror(errno));
93
94 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +110095}
96
Ben Lindstrombba81212001-06-25 05:01:22 +000097static void
Damien Miller33804262001-02-04 23:20:18 +110098get_msg(int fd, Buffer *m)
99{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100100 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +1100101
Damien Millera7f3aaa2003-01-10 21:43:58 +1100102 buffer_append_space(m, 4);
Damien Millerb253cc42005-05-26 12:23:44 +1000103 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
104 if (errno == EPIPE)
105 fatal("Connection closed");
106 else
107 fatal("Couldn't read packet: %s", strerror(errno));
108 }
Damien Miller33804262001-02-04 23:20:18 +1100109
Damien Millera7f3aaa2003-01-10 21:43:58 +1100110 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +1100111 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000112 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100113
Damien Millera7f3aaa2003-01-10 21:43:58 +1100114 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000115 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
116 if (errno == EPIPE)
117 fatal("Connection closed");
118 else
119 fatal("Read packet: %s", strerror(errno));
120 }
Damien Miller33804262001-02-04 23:20:18 +1100121}
122
Ben Lindstrombba81212001-06-25 05:01:22 +0000123static void
Damien Miller33804262001-02-04 23:20:18 +1100124send_string_request(int fd, u_int id, u_int code, char *s,
125 u_int len)
126{
127 Buffer msg;
128
129 buffer_init(&msg);
130 buffer_put_char(&msg, code);
131 buffer_put_int(&msg, id);
132 buffer_put_string(&msg, s, len);
133 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000134 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100135 buffer_free(&msg);
136}
137
Ben Lindstrombba81212001-06-25 05:01:22 +0000138static void
Damien Miller33804262001-02-04 23:20:18 +1100139send_string_attrs_request(int fd, u_int id, u_int code, char *s,
140 u_int len, Attrib *a)
141{
142 Buffer msg;
143
144 buffer_init(&msg);
145 buffer_put_char(&msg, code);
146 buffer_put_int(&msg, id);
147 buffer_put_string(&msg, s, len);
148 encode_attrib(&msg, a);
149 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000150 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100151 buffer_free(&msg);
152}
153
Ben Lindstrombba81212001-06-25 05:01:22 +0000154static u_int
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000155get_status(int fd, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100156{
157 Buffer msg;
158 u_int type, id, status;
159
160 buffer_init(&msg);
161 get_msg(fd, &msg);
162 type = buffer_get_char(&msg);
163 id = buffer_get_int(&msg);
164
165 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000166 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100167 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000168 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100169 SSH2_FXP_STATUS, type);
170
171 status = buffer_get_int(&msg);
172 buffer_free(&msg);
173
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000174 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100175
176 return(status);
177}
178
Ben Lindstrombba81212001-06-25 05:01:22 +0000179static char *
Damien Miller33804262001-02-04 23:20:18 +1100180get_handle(int fd, u_int expected_id, u_int *len)
181{
182 Buffer msg;
183 u_int type, id;
184 char *handle;
185
186 buffer_init(&msg);
187 get_msg(fd, &msg);
188 type = buffer_get_char(&msg);
189 id = buffer_get_int(&msg);
190
191 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000192 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100193 if (type == SSH2_FXP_STATUS) {
194 int status = buffer_get_int(&msg);
195
196 error("Couldn't get handle: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100197 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100198 return(NULL);
199 } else if (type != SSH2_FXP_HANDLE)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000200 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100201 SSH2_FXP_HANDLE, type);
202
203 handle = buffer_get_string(&msg, len);
204 buffer_free(&msg);
205
206 return(handle);
207}
208
Ben Lindstrombba81212001-06-25 05:01:22 +0000209static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000210get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100211{
212 Buffer msg;
213 u_int type, id;
214 Attrib *a;
215
216 buffer_init(&msg);
217 get_msg(fd, &msg);
218
219 type = buffer_get_char(&msg);
220 id = buffer_get_int(&msg);
221
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000222 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100223 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000224 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100225 if (type == SSH2_FXP_STATUS) {
226 int status = buffer_get_int(&msg);
227
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000228 if (quiet)
229 debug("Couldn't stat remote file: %s", fx2txt(status));
230 else
231 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100232 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100233 return(NULL);
234 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000235 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100236 SSH2_FXP_ATTRS, type);
237 }
238 a = decode_attrib(&msg);
239 buffer_free(&msg);
240
241 return(a);
242}
243
Damien Millerd671e5a2008-05-19 14:53:33 +1000244static int
245get_decode_statvfs(int fd, struct statvfs *st, u_int expected_id, int quiet)
246{
247 Buffer msg;
248 u_int type, id, flag;
249
250 buffer_init(&msg);
251 get_msg(fd, &msg);
252
253 type = buffer_get_char(&msg);
254 id = buffer_get_int(&msg);
255
256 debug3("Received statvfs reply T:%u I:%u", type, id);
257 if (id != expected_id)
258 fatal("ID mismatch (%u != %u)", id, expected_id);
259 if (type == SSH2_FXP_STATUS) {
260 int status = buffer_get_int(&msg);
261
262 if (quiet)
263 debug("Couldn't statvfs: %s", fx2txt(status));
264 else
265 error("Couldn't statvfs: %s", fx2txt(status));
266 buffer_free(&msg);
267 return -1;
268 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
269 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
270 SSH2_FXP_EXTENDED_REPLY, type);
271 }
272
273 bzero(st, sizeof(*st));
274 st->f_bsize = buffer_get_int(&msg);
275 st->f_frsize = buffer_get_int(&msg);
276 st->f_blocks = buffer_get_int64(&msg);
277 st->f_bfree = buffer_get_int64(&msg);
278 st->f_bavail = buffer_get_int64(&msg);
279 st->f_files = buffer_get_int64(&msg);
280 st->f_ffree = buffer_get_int64(&msg);
281 st->f_favail = buffer_get_int64(&msg);
282 st->f_fsid = buffer_get_int(&msg);
283 flag = buffer_get_int(&msg);
284 st->f_namemax = buffer_get_int(&msg);
285
286 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
287 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
288
289 buffer_free(&msg);
290
291 return 0;
292}
293
Damien Miller3db5f532002-02-13 14:10:32 +1100294struct sftp_conn *
295do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100296{
Damien Miller7a3e1d02008-03-27 10:59:57 +1100297 u_int type, exts = 0;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000298 int version;
Damien Miller33804262001-02-04 23:20:18 +1100299 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100300 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100301
302 buffer_init(&msg);
303 buffer_put_char(&msg, SSH2_FXP_INIT);
304 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
305 send_msg(fd_out, &msg);
306
307 buffer_clear(&msg);
308
309 get_msg(fd_in, &msg);
310
Kevin Stevesef4eea92001-02-05 12:42:17 +0000311 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100312 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000313 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100314 type);
315 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100316 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100317 }
318 version = buffer_get_int(&msg);
319
320 debug2("Remote version: %d", version);
321
322 /* Check for extensions */
323 while (buffer_len(&msg) > 0) {
324 char *name = buffer_get_string(&msg, NULL);
325 char *value = buffer_get_string(&msg, NULL);
326
327 debug2("Init extension: \"%s\"", name);
Damien Millerd671e5a2008-05-19 14:53:33 +1000328 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
329 strcmp(value, "1") == 0)
Damien Miller7a3e1d02008-03-27 10:59:57 +1100330 exts |= SFTP_EXT_POSIX_RENAME;
Damien Millerd671e5a2008-05-19 14:53:33 +1000331 if (strcmp(name, "statvfs@openssh.com") == 0 &&
332 strcmp(value, "1") == 0)
333 exts |= SFTP_EXT_STATVFS;
334 if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
335 strcmp(value, "1") == 0)
336 exts |= SFTP_EXT_FSTATVFS;
Damien Miller33804262001-02-04 23:20:18 +1100337 xfree(name);
338 xfree(value);
339 }
340
341 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100342
Damien Miller3db5f532002-02-13 14:10:32 +1100343 ret = xmalloc(sizeof(*ret));
344 ret->fd_in = fd_in;
345 ret->fd_out = fd_out;
346 ret->transfer_buflen = transfer_buflen;
347 ret->num_requests = num_requests;
348 ret->version = version;
349 ret->msg_id = 1;
Damien Miller7a3e1d02008-03-27 10:59:57 +1100350 ret->exts = exts;
Damien Miller3db5f532002-02-13 14:10:32 +1100351
352 /* Some filexfer v.0 servers don't support large packets */
353 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000354 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100355
356 return(ret);
357}
358
359u_int
360sftp_proto_version(struct sftp_conn *conn)
361{
362 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100363}
364
365int
Damien Miller3db5f532002-02-13 14:10:32 +1100366do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100367{
368 u_int id, status;
369 Buffer msg;
370
371 buffer_init(&msg);
372
Damien Miller3db5f532002-02-13 14:10:32 +1100373 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100374 buffer_put_char(&msg, SSH2_FXP_CLOSE);
375 buffer_put_int(&msg, id);
376 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100377 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000378 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100379
Damien Miller3db5f532002-02-13 14:10:32 +1100380 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100381 if (status != SSH2_FX_OK)
382 error("Couldn't close file: %s", fx2txt(status));
383
384 buffer_free(&msg);
385
386 return(status);
387}
388
Damien Miller4870afd2001-03-14 10:27:09 +1100389
Ben Lindstrombba81212001-06-25 05:01:22 +0000390static int
Damien Miller3db5f532002-02-13 14:10:32 +1100391do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100392 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100393{
394 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000395 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100396 char *handle;
397
Damien Miller3db5f532002-02-13 14:10:32 +1100398 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100399
400 buffer_init(&msg);
401 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
402 buffer_put_int(&msg, id);
403 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100404 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100405
406 buffer_clear(&msg);
407
Damien Miller3db5f532002-02-13 14:10:32 +1100408 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100409 if (handle == NULL)
410 return(-1);
411
Damien Miller4870afd2001-03-14 10:27:09 +1100412 if (dir) {
413 ents = 0;
414 *dir = xmalloc(sizeof(**dir));
415 (*dir)[0] = NULL;
416 }
Damien Miller4870afd2001-03-14 10:27:09 +1100417
Darren Tuckercdf547a2004-05-24 10:12:19 +1000418 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100419 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100420
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000421 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100422
423 buffer_clear(&msg);
424 buffer_put_char(&msg, SSH2_FXP_READDIR);
425 buffer_put_int(&msg, id);
426 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100427 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100428
429 buffer_clear(&msg);
430
Damien Miller3db5f532002-02-13 14:10:32 +1100431 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100432
433 type = buffer_get_char(&msg);
434 id = buffer_get_int(&msg);
435
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000436 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100437
438 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000439 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100440
441 if (type == SSH2_FXP_STATUS) {
442 int status = buffer_get_int(&msg);
443
444 debug3("Received SSH2_FXP_STATUS %d", status);
445
446 if (status == SSH2_FX_EOF) {
447 break;
448 } else {
449 error("Couldn't read directory: %s",
450 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100451 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100452 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000453 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100454 }
455 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000456 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100457 SSH2_FXP_NAME, type);
458
459 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100460 if (count == 0)
461 break;
462 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100463 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100464 char *filename, *longname;
465 Attrib *a;
466
467 filename = buffer_get_string(&msg, NULL);
468 longname = buffer_get_string(&msg, NULL);
469 a = decode_attrib(&msg);
470
Damien Miller4870afd2001-03-14 10:27:09 +1100471 if (printflag)
472 printf("%s\n", longname);
473
474 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100475 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100476 (*dir)[ents] = xmalloc(sizeof(***dir));
477 (*dir)[ents]->filename = xstrdup(filename);
478 (*dir)[ents]->longname = xstrdup(longname);
479 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
480 (*dir)[++ents] = NULL;
481 }
Damien Miller33804262001-02-04 23:20:18 +1100482
483 xfree(filename);
484 xfree(longname);
485 }
486 }
487
488 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100489 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100490 xfree(handle);
491
Darren Tuckercdf547a2004-05-24 10:12:19 +1000492 /* Don't return partial matches on interrupt */
493 if (interrupted && dir != NULL && *dir != NULL) {
494 free_sftp_dirents(*dir);
495 *dir = xmalloc(sizeof(**dir));
496 **dir = NULL;
497 }
498
Damien Miller33804262001-02-04 23:20:18 +1100499 return(0);
500}
501
502int
Damien Miller3db5f532002-02-13 14:10:32 +1100503do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100504{
Damien Miller3db5f532002-02-13 14:10:32 +1100505 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100506}
507
508void free_sftp_dirents(SFTP_DIRENT **s)
509{
510 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100511
512 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100513 xfree(s[i]->filename);
514 xfree(s[i]->longname);
515 xfree(s[i]);
516 }
517 xfree(s);
518}
519
520int
Damien Miller3db5f532002-02-13 14:10:32 +1100521do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100522{
523 u_int status, id;
524
525 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
526
Damien Miller3db5f532002-02-13 14:10:32 +1100527 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000528 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100529 strlen(path));
530 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100531 if (status != SSH2_FX_OK)
532 error("Couldn't delete file: %s", fx2txt(status));
533 return(status);
534}
535
536int
Damien Miller3db5f532002-02-13 14:10:32 +1100537do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100538{
539 u_int status, id;
540
Damien Miller3db5f532002-02-13 14:10:32 +1100541 id = conn->msg_id++;
542 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100543 strlen(path), a);
544
Damien Miller3db5f532002-02-13 14:10:32 +1100545 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100546 if (status != SSH2_FX_OK)
547 error("Couldn't create directory: %s", fx2txt(status));
548
549 return(status);
550}
551
552int
Damien Miller3db5f532002-02-13 14:10:32 +1100553do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100554{
555 u_int status, id;
556
Damien Miller3db5f532002-02-13 14:10:32 +1100557 id = conn->msg_id++;
558 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
559 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100560
Damien Miller3db5f532002-02-13 14:10:32 +1100561 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100562 if (status != SSH2_FX_OK)
563 error("Couldn't remove directory: %s", fx2txt(status));
564
565 return(status);
566}
567
568Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100569do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100570{
571 u_int id;
572
Damien Miller3db5f532002-02-13 14:10:32 +1100573 id = conn->msg_id++;
574
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000575 send_string_request(conn->fd_out, id,
576 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100577 path, strlen(path));
578
579 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100580}
581
582Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100583do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100584{
585 u_int id;
586
Damien Miller3db5f532002-02-13 14:10:32 +1100587 if (conn->version == 0) {
588 if (quiet)
589 debug("Server version does not support lstat operation");
590 else
Damien Miller996acd22003-04-09 20:59:48 +1000591 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000592 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100593 }
594
595 id = conn->msg_id++;
596 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
597 strlen(path));
598
599 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100600}
601
Damien Millercfe23d32008-02-10 22:20:44 +1100602#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100603Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100604do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100605{
606 u_int id;
607
Damien Miller3db5f532002-02-13 14:10:32 +1100608 id = conn->msg_id++;
609 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
610 handle_len);
611
612 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100613}
Damien Millercfe23d32008-02-10 22:20:44 +1100614#endif
Damien Miller33804262001-02-04 23:20:18 +1100615
616int
Damien Miller3db5f532002-02-13 14:10:32 +1100617do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100618{
619 u_int status, id;
620
Damien Miller3db5f532002-02-13 14:10:32 +1100621 id = conn->msg_id++;
622 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100623 strlen(path), a);
624
Damien Miller3db5f532002-02-13 14:10:32 +1100625 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100626 if (status != SSH2_FX_OK)
627 error("Couldn't setstat on \"%s\": %s", path,
628 fx2txt(status));
629
630 return(status);
631}
632
633int
Damien Miller3db5f532002-02-13 14:10:32 +1100634do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100635 Attrib *a)
636{
637 u_int status, id;
638
Damien Miller3db5f532002-02-13 14:10:32 +1100639 id = conn->msg_id++;
640 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100641 handle_len, a);
642
Damien Miller3db5f532002-02-13 14:10:32 +1100643 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100644 if (status != SSH2_FX_OK)
645 error("Couldn't fsetstat: %s", fx2txt(status));
646
647 return(status);
648}
649
650char *
Damien Miller3db5f532002-02-13 14:10:32 +1100651do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100652{
653 Buffer msg;
654 u_int type, expected_id, count, id;
655 char *filename, *longname;
656 Attrib *a;
657
Damien Miller3db5f532002-02-13 14:10:32 +1100658 expected_id = id = conn->msg_id++;
659 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
660 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100661
662 buffer_init(&msg);
663
Damien Miller3db5f532002-02-13 14:10:32 +1100664 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100665 type = buffer_get_char(&msg);
666 id = buffer_get_int(&msg);
667
668 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000669 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100670
671 if (type == SSH2_FXP_STATUS) {
672 u_int status = buffer_get_int(&msg);
673
674 error("Couldn't canonicalise: %s", fx2txt(status));
675 return(NULL);
676 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000677 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100678 SSH2_FXP_NAME, type);
679
680 count = buffer_get_int(&msg);
681 if (count != 1)
682 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
683
684 filename = buffer_get_string(&msg, NULL);
685 longname = buffer_get_string(&msg, NULL);
686 a = decode_attrib(&msg);
687
688 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
689
690 xfree(longname);
691
692 buffer_free(&msg);
693
694 return(filename);
695}
696
697int
Damien Miller3db5f532002-02-13 14:10:32 +1100698do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100699{
700 Buffer msg;
701 u_int status, id;
702
703 buffer_init(&msg);
704
705 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100706 id = conn->msg_id++;
Damien Miller7a3e1d02008-03-27 10:59:57 +1100707 if ((conn->exts & SFTP_EXT_POSIX_RENAME)) {
708 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
709 buffer_put_int(&msg, id);
710 buffer_put_cstring(&msg, "posix-rename@openssh.com");
711 } else {
712 buffer_put_char(&msg, SSH2_FXP_RENAME);
713 buffer_put_int(&msg, id);
714 }
Damien Miller33804262001-02-04 23:20:18 +1100715 buffer_put_cstring(&msg, oldpath);
716 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100717 send_msg(conn->fd_out, &msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100718 debug3("Sent message %s \"%s\" -> \"%s\"",
719 (conn->exts & SFTP_EXT_POSIX_RENAME) ? "posix-rename@openssh.com" :
720 "SSH2_FXP_RENAME", oldpath, newpath);
Damien Miller33804262001-02-04 23:20:18 +1100721 buffer_free(&msg);
722
Damien Miller3db5f532002-02-13 14:10:32 +1100723 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100724 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100725 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
726 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100727
728 return(status);
729}
730
731int
Damien Miller3db5f532002-02-13 14:10:32 +1100732do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100733{
734 Buffer msg;
735 u_int status, id;
736
Damien Miller3db5f532002-02-13 14:10:32 +1100737 if (conn->version < 3) {
738 error("This server does not support the symlink operation");
739 return(SSH2_FX_OP_UNSUPPORTED);
740 }
741
Damien Miller058316f2001-03-08 10:08:49 +1100742 buffer_init(&msg);
743
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000744 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100745 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100746 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
747 buffer_put_int(&msg, id);
748 buffer_put_cstring(&msg, oldpath);
749 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100750 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100751 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
752 newpath);
753 buffer_free(&msg);
754
Damien Miller3db5f532002-02-13 14:10:32 +1100755 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100756 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000757 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100758 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100759
760 return(status);
761}
762
Damien Millercfe23d32008-02-10 22:20:44 +1100763#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +1100764char *
Damien Miller3db5f532002-02-13 14:10:32 +1100765do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100766{
767 Buffer msg;
768 u_int type, expected_id, count, id;
769 char *filename, *longname;
770 Attrib *a;
771
Damien Miller3db5f532002-02-13 14:10:32 +1100772 expected_id = id = conn->msg_id++;
773 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
774 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100775
776 buffer_init(&msg);
777
Damien Miller3db5f532002-02-13 14:10:32 +1100778 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100779 type = buffer_get_char(&msg);
780 id = buffer_get_int(&msg);
781
782 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000783 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100784
785 if (type == SSH2_FXP_STATUS) {
786 u_int status = buffer_get_int(&msg);
787
788 error("Couldn't readlink: %s", fx2txt(status));
789 return(NULL);
790 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000791 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100792 SSH2_FXP_NAME, type);
793
794 count = buffer_get_int(&msg);
795 if (count != 1)
796 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
797
798 filename = buffer_get_string(&msg, NULL);
799 longname = buffer_get_string(&msg, NULL);
800 a = decode_attrib(&msg);
801
802 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
803
804 xfree(longname);
805
806 buffer_free(&msg);
807
808 return(filename);
809}
Damien Millercfe23d32008-02-10 22:20:44 +1100810#endif
Damien Miller058316f2001-03-08 10:08:49 +1100811
Damien Millerd671e5a2008-05-19 14:53:33 +1000812int
813do_statvfs(struct sftp_conn *conn, const char *path, struct statvfs *st,
814 int quiet)
815{
816 Buffer msg;
817 u_int id;
818
819 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
820 error("Server does not support statvfs@openssh.com extension");
821 return -1;
822 }
823
824 id = conn->msg_id++;
825
826 buffer_init(&msg);
827 buffer_clear(&msg);
828 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
829 buffer_put_int(&msg, id);
830 buffer_put_cstring(&msg, "statvfs@openssh.com");
831 buffer_put_cstring(&msg, path);
832 send_msg(conn->fd_out, &msg);
833 buffer_free(&msg);
834
835 return get_decode_statvfs(conn->fd_in, st, id, quiet);
836}
837
838#ifdef notyet
839int
840do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
841 struct statvfs *st, int quiet)
842{
843 Buffer msg;
844 u_int id;
845
846 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
847 error("Server does not support fstatvfs@openssh.com extension");
848 return -1;
849 }
850
851 id = conn->msg_id++;
852
853 buffer_init(&msg);
854 buffer_clear(&msg);
855 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
856 buffer_put_int(&msg, id);
857 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
858 buffer_put_string(&msg, handle, handle_len);
859 send_msg(conn->fd_out, &msg);
860 buffer_free(&msg);
861
862 return get_decode_statvfs(conn->fd_in, st, id, quiet);
863}
864#endif
865
Damien Miller16a13332002-02-13 14:03:56 +1100866static void
867send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
868 char *handle, u_int handle_len)
869{
870 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000871
Damien Miller16a13332002-02-13 14:03:56 +1100872 buffer_init(&msg);
873 buffer_clear(&msg);
874 buffer_put_char(&msg, SSH2_FXP_READ);
875 buffer_put_int(&msg, id);
876 buffer_put_string(&msg, handle, handle_len);
877 buffer_put_int64(&msg, offset);
878 buffer_put_int(&msg, len);
879 send_msg(fd_out, &msg);
880 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000881}
Damien Miller16a13332002-02-13 14:03:56 +1100882
Damien Miller058316f2001-03-08 10:08:49 +1100883int
Damien Miller3db5f532002-02-13 14:10:32 +1100884do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
885 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100886{
Damien Miller33804262001-02-04 23:20:18 +1100887 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100888 Buffer msg;
889 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000890 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100891 int read_error, write_errno;
892 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000893 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100894 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100895 struct request {
896 u_int id;
897 u_int len;
898 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000899 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100900 };
901 TAILQ_HEAD(reqhead, request) requests;
902 struct request *req;
903
904 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100905
Damien Miller3db5f532002-02-13 14:10:32 +1100906 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100907 if (a == NULL)
908 return(-1);
909
910 /* XXX: should we preserve set[ug]id? */
911 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100912 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100913 else
914 mode = 0666;
915
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000916 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100917 (!S_ISREG(a->perm))) {
918 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000919 return(-1);
920 }
921
Damien Miller16a13332002-02-13 14:03:56 +1100922 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
923 size = a->size;
924 else
925 size = 0;
926
Damien Miller3db5f532002-02-13 14:10:32 +1100927 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100928 buffer_init(&msg);
929
930 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100931 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100932 buffer_put_char(&msg, SSH2_FXP_OPEN);
933 buffer_put_int(&msg, id);
934 buffer_put_cstring(&msg, remote_path);
935 buffer_put_int(&msg, SSH2_FXF_READ);
936 attrib_clear(&junk); /* Send empty attributes */
937 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100938 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000939 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100940
Damien Miller3db5f532002-02-13 14:10:32 +1100941 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100942 if (handle == NULL) {
943 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100944 return(-1);
945 }
946
Damien Millera8e06ce2003-11-21 23:48:55 +1100947 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +1100948 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100949 if (local_fd == -1) {
950 error("Couldn't open local file \"%s\" for writing: %s",
951 local_path, strerror(errno));
Damien Miller6b0c8182008-02-10 22:23:41 +1100952 do_close(conn, handle, handle_len);
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000953 buffer_free(&msg);
954 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100955 return(-1);
956 }
957
Damien Miller33804262001-02-04 23:20:18 +1100958 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100959 write_error = read_error = write_errno = num_req = offset = 0;
960 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +1100961 progress_counter = 0;
962
Damien Miller9ba30692004-03-08 23:12:02 +1100963 if (showprogress && size != 0)
964 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +1100965
Damien Miller16a13332002-02-13 14:03:56 +1100966 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100967 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100968 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100969
Darren Tuckercdf547a2004-05-24 10:12:19 +1000970 /*
Darren Tuckerfc959702004-07-17 16:12:08 +1000971 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +1000972 * allow outstanding requests to drain gracefully
973 */
974 if (interrupted) {
975 if (num_req == 0) /* If we haven't started yet... */
976 break;
977 max_req = 0;
978 }
979
Damien Miller16a13332002-02-13 14:03:56 +1100980 /* Send some more requests */
981 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000982 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000983 (unsigned long long)offset,
984 (unsigned long long)offset + buflen - 1,
985 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100986 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100987 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100988 req->len = buflen;
989 req->offset = offset;
990 offset += buflen;
991 num_req++;
992 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000993 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +1100994 req->len, handle, handle_len);
995 }
Damien Miller33804262001-02-04 23:20:18 +1100996
997 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100998 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100999 type = buffer_get_char(&msg);
1000 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001001 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001002
Damien Miller16a13332002-02-13 14:03:56 +11001003 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001004 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001005 req != NULL && req->id != id;
1006 req = TAILQ_NEXT(req, tq))
1007 ;
1008 if (req == NULL)
1009 fatal("Unexpected reply %u", id);
1010
1011 switch (type) {
1012 case SSH2_FXP_STATUS:
1013 status = buffer_get_int(&msg);
1014 if (status != SSH2_FX_EOF)
1015 read_error = 1;
1016 max_req = 0;
1017 TAILQ_REMOVE(&requests, req, tq);
1018 xfree(req);
1019 num_req--;
1020 break;
1021 case SSH2_FXP_DATA:
1022 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +00001023 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001024 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001025 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001026 if (len > req->len)
1027 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +00001028 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001029 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001030 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001031 !write_error) {
1032 write_errno = errno;
1033 write_error = 1;
1034 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001035 }
Damien Miller62d57f62003-01-10 21:43:24 +11001036 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +11001037 xfree(data);
1038
1039 if (len == req->len) {
1040 TAILQ_REMOVE(&requests, req, tq);
1041 xfree(req);
1042 num_req--;
1043 } else {
1044 /* Resend the request for the missing data */
1045 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001046 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001047 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001048 (unsigned long long)req->offset +
1049 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001050 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001051 req->len -= len;
1052 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001053 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001054 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001055 /* Reduce the request size */
1056 if (len < buflen)
1057 buflen = MAX(MIN_READ_SIZE, len);
1058 }
1059 if (max_req > 0) { /* max_req = 0 iff EOF received */
1060 if (size > 0 && offset > size) {
1061 /* Only one request at a time
1062 * after the expected EOF */
1063 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001064 (unsigned long long)offset,
1065 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001066 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001067 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001068 ++max_req;
1069 }
1070 }
1071 break;
1072 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001073 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001074 SSH2_FXP_DATA, type);
1075 }
Damien Miller33804262001-02-04 23:20:18 +11001076 }
Damien Miller33804262001-02-04 23:20:18 +11001077
Damien Miller62d57f62003-01-10 21:43:24 +11001078 if (showprogress && size)
1079 stop_progress_meter();
1080
Damien Miller16a13332002-02-13 14:03:56 +11001081 /* Sanity check */
1082 if (TAILQ_FIRST(&requests) != NULL)
1083 fatal("Transfer complete, but requests still in queue");
1084
1085 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001086 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001087 remote_path, fx2txt(status));
1088 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001089 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001090 error("Couldn't write to \"%s\": %s", local_path,
1091 strerror(write_errno));
1092 status = -1;
1093 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001094 } else {
Damien Miller3db5f532002-02-13 14:10:32 +11001095 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001096
1097 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001098#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +11001099 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001100#else
Damien Miller16a13332002-02-13 14:03:56 +11001101 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001102#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001103 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001104 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001105 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1106 struct timeval tv[2];
1107 tv[0].tv_sec = a->atime;
1108 tv[1].tv_sec = a->mtime;
1109 tv[0].tv_usec = tv[1].tv_usec = 0;
1110 if (utimes(local_path, tv) == -1)
1111 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001112 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001113 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001114 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001115 close(local_fd);
1116 buffer_free(&msg);
1117 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001118
1119 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001120}
1121
1122int
Damien Miller3db5f532002-02-13 14:10:32 +11001123do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
1124 int pflag)
Damien Miller33804262001-02-04 23:20:18 +11001125{
Damien Milleracdf25b2008-02-10 22:27:24 +11001126 int local_fd;
1127 int status = SSH2_FX_OK;
Damien Miller5873dfd2002-02-13 14:04:37 +11001128 u_int handle_len, id, type;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001129 off_t offset;
Damien Miller8829d362002-02-08 22:04:05 +11001130 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +11001131 Buffer msg;
1132 struct stat sb;
1133 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +11001134 u_int32_t startid;
1135 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001136 struct outstanding_ack {
1137 u_int id;
1138 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001139 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001140 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001141 };
1142 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001143 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001144
1145 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001146
1147 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1148 error("Couldn't open local file \"%s\" for reading: %s",
1149 local_path, strerror(errno));
1150 return(-1);
1151 }
1152 if (fstat(local_fd, &sb) == -1) {
1153 error("Couldn't fstat local file \"%s\": %s",
1154 local_path, strerror(errno));
1155 close(local_fd);
1156 return(-1);
1157 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001158 if (!S_ISREG(sb.st_mode)) {
1159 error("%s is not a regular file", local_path);
1160 close(local_fd);
1161 return(-1);
1162 }
Damien Miller33804262001-02-04 23:20:18 +11001163 stat_to_attrib(&sb, &a);
1164
1165 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1166 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1167 a.perm &= 0777;
1168 if (!pflag)
1169 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1170
1171 buffer_init(&msg);
1172
1173 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001174 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001175 buffer_put_char(&msg, SSH2_FXP_OPEN);
1176 buffer_put_int(&msg, id);
1177 buffer_put_cstring(&msg, remote_path);
1178 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1179 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001180 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001181 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001182
1183 buffer_clear(&msg);
1184
Damien Miller3db5f532002-02-13 14:10:32 +11001185 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001186 if (handle == NULL) {
1187 close(local_fd);
1188 buffer_free(&msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001189 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001190 }
1191
Damien Miller16a13332002-02-13 14:03:56 +11001192 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001193 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001194
Damien Miller33804262001-02-04 23:20:18 +11001195 /* Read from local and write to remote */
1196 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001197 if (showprogress)
1198 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001199
Damien Miller9f0f5c62001-12-21 14:45:46 +11001200 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001201 int len;
Damien Miller33804262001-02-04 23:20:18 +11001202
1203 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001204 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001205 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001206 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001207 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001208 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001209 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001210 len = 0;
1211 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001212 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001213 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1214
1215 if (len == -1)
1216 fatal("Couldn't read from \"%s\": %s", local_path,
1217 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001218
1219 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001220 ack = xmalloc(sizeof(*ack));
1221 ack->id = ++id;
1222 ack->offset = offset;
1223 ack->len = len;
1224 TAILQ_INSERT_TAIL(&acks, ack, tq);
1225
Damien Miller16a13332002-02-13 14:03:56 +11001226 buffer_clear(&msg);
1227 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001228 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001229 buffer_put_string(&msg, handle, handle_len);
1230 buffer_put_int64(&msg, offset);
1231 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001232 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001233 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001234 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001235 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001236 break;
1237
Damien Miller5873dfd2002-02-13 14:04:37 +11001238 if (ack == NULL)
1239 fatal("Unexpected ACK %u", id);
1240
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001241 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001242 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001243 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001244
Damien Miller5873dfd2002-02-13 14:04:37 +11001245 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001246 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001247 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001248 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001249
1250 if (type != SSH2_FXP_STATUS)
1251 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1252 "got %d", SSH2_FXP_STATUS, type);
1253
1254 status = buffer_get_int(&msg);
1255 debug3("SSH2_FXP_STATUS %d", status);
1256
1257 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001258 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001259 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001260 ack = TAILQ_NEXT(ack, tq))
1261 ;
1262 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001263 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001264 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001265 debug3("In write loop, ack for %u %u bytes at %lld",
1266 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001267 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001268 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001269 }
Damien Miller33804262001-02-04 23:20:18 +11001270 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001271 if (offset < 0)
1272 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001273 }
Damien Milleracdf25b2008-02-10 22:27:24 +11001274 buffer_free(&msg);
1275
Damien Miller62d57f62003-01-10 21:43:24 +11001276 if (showprogress)
1277 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001278 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001279
Damien Milleracdf25b2008-02-10 22:27:24 +11001280 if (status != SSH2_FX_OK) {
1281 error("Couldn't write to remote file \"%s\": %s",
1282 remote_path, fx2txt(status));
1283 status = -1;
1284 }
1285
Damien Miller33804262001-02-04 23:20:18 +11001286 if (close(local_fd) == -1) {
1287 error("Couldn't close local file \"%s\": %s", local_path,
1288 strerror(errno));
Damien Millerd7686fd2001-02-10 00:40:03 +11001289 status = -1;
Damien Miller33804262001-02-04 23:20:18 +11001290 }
1291
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001292 /* Override umask and utimes if asked */
1293 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001294 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001295
Damien Milleracdf25b2008-02-10 22:27:24 +11001296 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
1297 status = -1;
Damien Millerd7686fd2001-02-10 00:40:03 +11001298 xfree(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001299
1300 return status;
Damien Miller33804262001-02-04 23:20:18 +11001301}