blob: 7df46379c06d593946b9b2968e4905ddb195083c [file] [log] [blame]
Damien Millercfe23d32008-02-10 22:20:44 +11001/* $OpenBSD: sftp-client.c,v 1.78 2008/01/11 07:22:27 chl 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 Millerd7834352006-08-05 12:39:39 +100027#include "openbsd-compat/sys-queue.h"
Damien Millerf17883e2006-03-15 11:45:54 +110028#ifdef HAVE_SYS_STAT_H
29# include <sys/stat.h>
30#endif
Damien Miller9aec9192006-08-05 10:57:45 +100031#ifdef HAVE_SYS_TIME_H
32# include <sys/time.h>
33#endif
Damien Millerd7834352006-08-05 12:39:39 +100034#include <sys/uio.h>
Damien Miller57cf6382006-07-10 21:13:46 +100035
Darren Tucker39972492006-07-12 22:22:46 +100036#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100037#include <fcntl.h>
38#include <signal.h>
Damien Millerd7834352006-08-05 12:39:39 +100039#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100040#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100041#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100042#include <unistd.h>
Damien Miller16a13332002-02-13 14:03:56 +110043
Damien Miller33804262001-02-04 23:20:18 +110044#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100045#include "buffer.h"
Damien Miller33804262001-02-04 23:20:18 +110046#include "log.h"
47#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110048#include "progressmeter.h"
Damien Miller3f941882006-03-31 23:13:02 +110049#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110050
51#include "sftp.h"
52#include "sftp-common.h"
53#include "sftp-client.h"
54
Darren Tuckercdf547a2004-05-24 10:12:19 +100055extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110056extern int showprogress;
57
Damien Miller0c8d8f62006-03-15 11:34:25 +110058/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110059#define MIN_READ_SIZE 512
60
Damien Miller3db5f532002-02-13 14:10:32 +110061struct sftp_conn {
62 int fd_in;
63 int fd_out;
64 u_int transfer_buflen;
65 u_int num_requests;
66 u_int version;
67 u_int msg_id;
68};
Ben Lindstrom288cc392001-02-09 02:58:04 +000069
Ben Lindstrombba81212001-06-25 05:01:22 +000070static void
Damien Miller33804262001-02-04 23:20:18 +110071send_msg(int fd, Buffer *m)
72{
Damien Millera7f3aaa2003-01-10 21:43:58 +110073 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +100074 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +110075
Damien Miller54446182006-01-02 23:40:50 +110076 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +110077 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +110078
Damien Millera7f3aaa2003-01-10 21:43:58 +110079 /* Send length first */
Damien Miller3f941882006-03-31 23:13:02 +110080 put_u32(mlen, buffer_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +100081 iov[0].iov_base = mlen;
82 iov[0].iov_len = sizeof(mlen);
83 iov[1].iov_base = buffer_ptr(m);
84 iov[1].iov_len = buffer_len(m);
Damien Millerd7834352006-08-05 12:39:39 +100085
Damien Miller58ca98b2006-04-23 12:06:35 +100086 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +110087 fatal("Couldn't send packet: %s", strerror(errno));
88
89 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +110090}
91
Ben Lindstrombba81212001-06-25 05:01:22 +000092static void
Damien Miller33804262001-02-04 23:20:18 +110093get_msg(int fd, Buffer *m)
94{
Damien Millera7f3aaa2003-01-10 21:43:58 +110095 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +110096
Damien Millera7f3aaa2003-01-10 21:43:58 +110097 buffer_append_space(m, 4);
Damien Millerb253cc42005-05-26 12:23:44 +100098 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
99 if (errno == EPIPE)
100 fatal("Connection closed");
101 else
102 fatal("Couldn't read packet: %s", strerror(errno));
103 }
Damien Miller33804262001-02-04 23:20:18 +1100104
Damien Millera7f3aaa2003-01-10 21:43:58 +1100105 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +1100106 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000107 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100108
Damien Millera7f3aaa2003-01-10 21:43:58 +1100109 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000110 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
111 if (errno == EPIPE)
112 fatal("Connection closed");
113 else
114 fatal("Read packet: %s", strerror(errno));
115 }
Damien Miller33804262001-02-04 23:20:18 +1100116}
117
Ben Lindstrombba81212001-06-25 05:01:22 +0000118static void
Damien Miller33804262001-02-04 23:20:18 +1100119send_string_request(int fd, u_int id, u_int code, char *s,
120 u_int len)
121{
122 Buffer msg;
123
124 buffer_init(&msg);
125 buffer_put_char(&msg, code);
126 buffer_put_int(&msg, id);
127 buffer_put_string(&msg, s, len);
128 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000129 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100130 buffer_free(&msg);
131}
132
Ben Lindstrombba81212001-06-25 05:01:22 +0000133static void
Damien Miller33804262001-02-04 23:20:18 +1100134send_string_attrs_request(int fd, u_int id, u_int code, char *s,
135 u_int len, Attrib *a)
136{
137 Buffer msg;
138
139 buffer_init(&msg);
140 buffer_put_char(&msg, code);
141 buffer_put_int(&msg, id);
142 buffer_put_string(&msg, s, len);
143 encode_attrib(&msg, a);
144 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000145 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100146 buffer_free(&msg);
147}
148
Ben Lindstrombba81212001-06-25 05:01:22 +0000149static u_int
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000150get_status(int fd, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100151{
152 Buffer msg;
153 u_int type, id, status;
154
155 buffer_init(&msg);
156 get_msg(fd, &msg);
157 type = buffer_get_char(&msg);
158 id = buffer_get_int(&msg);
159
160 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000161 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100162 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000163 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100164 SSH2_FXP_STATUS, type);
165
166 status = buffer_get_int(&msg);
167 buffer_free(&msg);
168
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000169 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100170
171 return(status);
172}
173
Ben Lindstrombba81212001-06-25 05:01:22 +0000174static char *
Damien Miller33804262001-02-04 23:20:18 +1100175get_handle(int fd, u_int expected_id, u_int *len)
176{
177 Buffer msg;
178 u_int type, id;
179 char *handle;
180
181 buffer_init(&msg);
182 get_msg(fd, &msg);
183 type = buffer_get_char(&msg);
184 id = buffer_get_int(&msg);
185
186 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000187 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100188 if (type == SSH2_FXP_STATUS) {
189 int status = buffer_get_int(&msg);
190
191 error("Couldn't get handle: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100192 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100193 return(NULL);
194 } else if (type != SSH2_FXP_HANDLE)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000195 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100196 SSH2_FXP_HANDLE, type);
197
198 handle = buffer_get_string(&msg, len);
199 buffer_free(&msg);
200
201 return(handle);
202}
203
Ben Lindstrombba81212001-06-25 05:01:22 +0000204static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000205get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100206{
207 Buffer msg;
208 u_int type, id;
209 Attrib *a;
210
211 buffer_init(&msg);
212 get_msg(fd, &msg);
213
214 type = buffer_get_char(&msg);
215 id = buffer_get_int(&msg);
216
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000217 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100218 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000219 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100220 if (type == SSH2_FXP_STATUS) {
221 int status = buffer_get_int(&msg);
222
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000223 if (quiet)
224 debug("Couldn't stat remote file: %s", fx2txt(status));
225 else
226 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100227 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100228 return(NULL);
229 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000230 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100231 SSH2_FXP_ATTRS, type);
232 }
233 a = decode_attrib(&msg);
234 buffer_free(&msg);
235
236 return(a);
237}
238
Damien Miller3db5f532002-02-13 14:10:32 +1100239struct sftp_conn *
240do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100241{
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000242 u_int type;
243 int version;
Damien Miller33804262001-02-04 23:20:18 +1100244 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100245 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100246
247 buffer_init(&msg);
248 buffer_put_char(&msg, SSH2_FXP_INIT);
249 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
250 send_msg(fd_out, &msg);
251
252 buffer_clear(&msg);
253
254 get_msg(fd_in, &msg);
255
Kevin Stevesef4eea92001-02-05 12:42:17 +0000256 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100257 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000258 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100259 type);
260 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100261 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100262 }
263 version = buffer_get_int(&msg);
264
265 debug2("Remote version: %d", version);
266
267 /* Check for extensions */
268 while (buffer_len(&msg) > 0) {
269 char *name = buffer_get_string(&msg, NULL);
270 char *value = buffer_get_string(&msg, NULL);
271
272 debug2("Init extension: \"%s\"", name);
273 xfree(name);
274 xfree(value);
275 }
276
277 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100278
Damien Miller3db5f532002-02-13 14:10:32 +1100279 ret = xmalloc(sizeof(*ret));
280 ret->fd_in = fd_in;
281 ret->fd_out = fd_out;
282 ret->transfer_buflen = transfer_buflen;
283 ret->num_requests = num_requests;
284 ret->version = version;
285 ret->msg_id = 1;
286
287 /* Some filexfer v.0 servers don't support large packets */
288 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000289 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100290
291 return(ret);
292}
293
294u_int
295sftp_proto_version(struct sftp_conn *conn)
296{
297 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100298}
299
300int
Damien Miller3db5f532002-02-13 14:10:32 +1100301do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100302{
303 u_int id, status;
304 Buffer msg;
305
306 buffer_init(&msg);
307
Damien Miller3db5f532002-02-13 14:10:32 +1100308 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100309 buffer_put_char(&msg, SSH2_FXP_CLOSE);
310 buffer_put_int(&msg, id);
311 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100312 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000313 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100314
Damien Miller3db5f532002-02-13 14:10:32 +1100315 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100316 if (status != SSH2_FX_OK)
317 error("Couldn't close file: %s", fx2txt(status));
318
319 buffer_free(&msg);
320
321 return(status);
322}
323
Damien Miller4870afd2001-03-14 10:27:09 +1100324
Ben Lindstrombba81212001-06-25 05:01:22 +0000325static int
Damien Miller3db5f532002-02-13 14:10:32 +1100326do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100327 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100328{
329 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000330 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100331 char *handle;
332
Damien Miller3db5f532002-02-13 14:10:32 +1100333 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100334
335 buffer_init(&msg);
336 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
337 buffer_put_int(&msg, id);
338 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100339 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100340
341 buffer_clear(&msg);
342
Damien Miller3db5f532002-02-13 14:10:32 +1100343 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100344 if (handle == NULL)
345 return(-1);
346
Damien Miller4870afd2001-03-14 10:27:09 +1100347 if (dir) {
348 ents = 0;
349 *dir = xmalloc(sizeof(**dir));
350 (*dir)[0] = NULL;
351 }
Damien Miller4870afd2001-03-14 10:27:09 +1100352
Darren Tuckercdf547a2004-05-24 10:12:19 +1000353 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100354 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100355
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000356 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100357
358 buffer_clear(&msg);
359 buffer_put_char(&msg, SSH2_FXP_READDIR);
360 buffer_put_int(&msg, id);
361 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100362 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100363
364 buffer_clear(&msg);
365
Damien Miller3db5f532002-02-13 14:10:32 +1100366 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100367
368 type = buffer_get_char(&msg);
369 id = buffer_get_int(&msg);
370
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000371 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100372
373 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000374 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100375
376 if (type == SSH2_FXP_STATUS) {
377 int status = buffer_get_int(&msg);
378
379 debug3("Received SSH2_FXP_STATUS %d", status);
380
381 if (status == SSH2_FX_EOF) {
382 break;
383 } else {
384 error("Couldn't read directory: %s",
385 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100386 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100387 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000388 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100389 }
390 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000391 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100392 SSH2_FXP_NAME, type);
393
394 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100395 if (count == 0)
396 break;
397 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100398 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100399 char *filename, *longname;
400 Attrib *a;
401
402 filename = buffer_get_string(&msg, NULL);
403 longname = buffer_get_string(&msg, NULL);
404 a = decode_attrib(&msg);
405
Damien Miller4870afd2001-03-14 10:27:09 +1100406 if (printflag)
407 printf("%s\n", longname);
408
409 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100410 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100411 (*dir)[ents] = xmalloc(sizeof(***dir));
412 (*dir)[ents]->filename = xstrdup(filename);
413 (*dir)[ents]->longname = xstrdup(longname);
414 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
415 (*dir)[++ents] = NULL;
416 }
Damien Miller33804262001-02-04 23:20:18 +1100417
418 xfree(filename);
419 xfree(longname);
420 }
421 }
422
423 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100424 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100425 xfree(handle);
426
Darren Tuckercdf547a2004-05-24 10:12:19 +1000427 /* Don't return partial matches on interrupt */
428 if (interrupted && dir != NULL && *dir != NULL) {
429 free_sftp_dirents(*dir);
430 *dir = xmalloc(sizeof(**dir));
431 **dir = NULL;
432 }
433
Damien Miller33804262001-02-04 23:20:18 +1100434 return(0);
435}
436
437int
Damien Miller3db5f532002-02-13 14:10:32 +1100438do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100439{
Damien Miller3db5f532002-02-13 14:10:32 +1100440 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100441}
442
443void free_sftp_dirents(SFTP_DIRENT **s)
444{
445 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100446
447 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100448 xfree(s[i]->filename);
449 xfree(s[i]->longname);
450 xfree(s[i]);
451 }
452 xfree(s);
453}
454
455int
Damien Miller3db5f532002-02-13 14:10:32 +1100456do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100457{
458 u_int status, id;
459
460 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
461
Damien Miller3db5f532002-02-13 14:10:32 +1100462 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000463 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100464 strlen(path));
465 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100466 if (status != SSH2_FX_OK)
467 error("Couldn't delete file: %s", fx2txt(status));
468 return(status);
469}
470
471int
Damien Miller3db5f532002-02-13 14:10:32 +1100472do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100473{
474 u_int status, id;
475
Damien Miller3db5f532002-02-13 14:10:32 +1100476 id = conn->msg_id++;
477 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100478 strlen(path), a);
479
Damien Miller3db5f532002-02-13 14:10:32 +1100480 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100481 if (status != SSH2_FX_OK)
482 error("Couldn't create directory: %s", fx2txt(status));
483
484 return(status);
485}
486
487int
Damien Miller3db5f532002-02-13 14:10:32 +1100488do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100489{
490 u_int status, id;
491
Damien Miller3db5f532002-02-13 14:10:32 +1100492 id = conn->msg_id++;
493 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
494 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100495
Damien Miller3db5f532002-02-13 14:10:32 +1100496 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100497 if (status != SSH2_FX_OK)
498 error("Couldn't remove directory: %s", fx2txt(status));
499
500 return(status);
501}
502
503Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100504do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100505{
506 u_int id;
507
Damien Miller3db5f532002-02-13 14:10:32 +1100508 id = conn->msg_id++;
509
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000510 send_string_request(conn->fd_out, id,
511 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100512 path, strlen(path));
513
514 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100515}
516
517Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100518do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100519{
520 u_int id;
521
Damien Miller3db5f532002-02-13 14:10:32 +1100522 if (conn->version == 0) {
523 if (quiet)
524 debug("Server version does not support lstat operation");
525 else
Damien Miller996acd22003-04-09 20:59:48 +1000526 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000527 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100528 }
529
530 id = conn->msg_id++;
531 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
532 strlen(path));
533
534 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100535}
536
Damien Millercfe23d32008-02-10 22:20:44 +1100537#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100538Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100539do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100540{
541 u_int id;
542
Damien Miller3db5f532002-02-13 14:10:32 +1100543 id = conn->msg_id++;
544 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
545 handle_len);
546
547 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100548}
Damien Millercfe23d32008-02-10 22:20:44 +1100549#endif
Damien Miller33804262001-02-04 23:20:18 +1100550
551int
Damien Miller3db5f532002-02-13 14:10:32 +1100552do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100553{
554 u_int status, id;
555
Damien Miller3db5f532002-02-13 14:10:32 +1100556 id = conn->msg_id++;
557 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100558 strlen(path), a);
559
Damien Miller3db5f532002-02-13 14:10:32 +1100560 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100561 if (status != SSH2_FX_OK)
562 error("Couldn't setstat on \"%s\": %s", path,
563 fx2txt(status));
564
565 return(status);
566}
567
568int
Damien Miller3db5f532002-02-13 14:10:32 +1100569do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100570 Attrib *a)
571{
572 u_int status, id;
573
Damien Miller3db5f532002-02-13 14:10:32 +1100574 id = conn->msg_id++;
575 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100576 handle_len, a);
577
Damien Miller3db5f532002-02-13 14:10:32 +1100578 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100579 if (status != SSH2_FX_OK)
580 error("Couldn't fsetstat: %s", fx2txt(status));
581
582 return(status);
583}
584
585char *
Damien Miller3db5f532002-02-13 14:10:32 +1100586do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100587{
588 Buffer msg;
589 u_int type, expected_id, count, id;
590 char *filename, *longname;
591 Attrib *a;
592
Damien Miller3db5f532002-02-13 14:10:32 +1100593 expected_id = id = conn->msg_id++;
594 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
595 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100596
597 buffer_init(&msg);
598
Damien Miller3db5f532002-02-13 14:10:32 +1100599 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100600 type = buffer_get_char(&msg);
601 id = buffer_get_int(&msg);
602
603 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000604 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100605
606 if (type == SSH2_FXP_STATUS) {
607 u_int status = buffer_get_int(&msg);
608
609 error("Couldn't canonicalise: %s", fx2txt(status));
610 return(NULL);
611 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000612 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100613 SSH2_FXP_NAME, type);
614
615 count = buffer_get_int(&msg);
616 if (count != 1)
617 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
618
619 filename = buffer_get_string(&msg, NULL);
620 longname = buffer_get_string(&msg, NULL);
621 a = decode_attrib(&msg);
622
623 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
624
625 xfree(longname);
626
627 buffer_free(&msg);
628
629 return(filename);
630}
631
632int
Damien Miller3db5f532002-02-13 14:10:32 +1100633do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100634{
635 Buffer msg;
636 u_int status, id;
637
638 buffer_init(&msg);
639
640 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100641 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100642 buffer_put_char(&msg, SSH2_FXP_RENAME);
643 buffer_put_int(&msg, id);
644 buffer_put_cstring(&msg, oldpath);
645 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100646 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100647 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
648 newpath);
649 buffer_free(&msg);
650
Damien Miller3db5f532002-02-13 14:10:32 +1100651 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100652 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100653 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
654 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100655
656 return(status);
657}
658
659int
Damien Miller3db5f532002-02-13 14:10:32 +1100660do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100661{
662 Buffer msg;
663 u_int status, id;
664
Damien Miller3db5f532002-02-13 14:10:32 +1100665 if (conn->version < 3) {
666 error("This server does not support the symlink operation");
667 return(SSH2_FX_OP_UNSUPPORTED);
668 }
669
Damien Miller058316f2001-03-08 10:08:49 +1100670 buffer_init(&msg);
671
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000672 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100673 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100674 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
675 buffer_put_int(&msg, id);
676 buffer_put_cstring(&msg, oldpath);
677 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100678 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100679 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
680 newpath);
681 buffer_free(&msg);
682
Damien Miller3db5f532002-02-13 14:10:32 +1100683 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100684 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000685 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100686 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100687
688 return(status);
689}
690
Damien Millercfe23d32008-02-10 22:20:44 +1100691#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +1100692char *
Damien Miller3db5f532002-02-13 14:10:32 +1100693do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100694{
695 Buffer msg;
696 u_int type, expected_id, count, id;
697 char *filename, *longname;
698 Attrib *a;
699
Damien Miller3db5f532002-02-13 14:10:32 +1100700 expected_id = id = conn->msg_id++;
701 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
702 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100703
704 buffer_init(&msg);
705
Damien Miller3db5f532002-02-13 14:10:32 +1100706 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100707 type = buffer_get_char(&msg);
708 id = buffer_get_int(&msg);
709
710 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000711 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100712
713 if (type == SSH2_FXP_STATUS) {
714 u_int status = buffer_get_int(&msg);
715
716 error("Couldn't readlink: %s", fx2txt(status));
717 return(NULL);
718 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000719 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100720 SSH2_FXP_NAME, type);
721
722 count = buffer_get_int(&msg);
723 if (count != 1)
724 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
725
726 filename = buffer_get_string(&msg, NULL);
727 longname = buffer_get_string(&msg, NULL);
728 a = decode_attrib(&msg);
729
730 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
731
732 xfree(longname);
733
734 buffer_free(&msg);
735
736 return(filename);
737}
Damien Millercfe23d32008-02-10 22:20:44 +1100738#endif
Damien Miller058316f2001-03-08 10:08:49 +1100739
Damien Miller16a13332002-02-13 14:03:56 +1100740static void
741send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
742 char *handle, u_int handle_len)
743{
744 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000745
Damien Miller16a13332002-02-13 14:03:56 +1100746 buffer_init(&msg);
747 buffer_clear(&msg);
748 buffer_put_char(&msg, SSH2_FXP_READ);
749 buffer_put_int(&msg, id);
750 buffer_put_string(&msg, handle, handle_len);
751 buffer_put_int64(&msg, offset);
752 buffer_put_int(&msg, len);
753 send_msg(fd_out, &msg);
754 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000755}
Damien Miller16a13332002-02-13 14:03:56 +1100756
Damien Miller058316f2001-03-08 10:08:49 +1100757int
Damien Miller3db5f532002-02-13 14:10:32 +1100758do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
759 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100760{
Damien Miller33804262001-02-04 23:20:18 +1100761 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100762 Buffer msg;
763 char *handle;
Darren Tucker40858532005-08-02 17:07:07 +1000764 int local_fd, status = 0, write_error;
Damien Miller16a13332002-02-13 14:03:56 +1100765 int read_error, write_errno;
766 u_int64_t offset, size;
Damien Millereccb9de2005-06-17 12:59:34 +1000767 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +1100768 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100769 struct request {
770 u_int id;
771 u_int len;
772 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000773 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100774 };
775 TAILQ_HEAD(reqhead, request) requests;
776 struct request *req;
777
778 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100779
Damien Miller3db5f532002-02-13 14:10:32 +1100780 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100781 if (a == NULL)
782 return(-1);
783
784 /* XXX: should we preserve set[ug]id? */
785 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100786 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100787 else
788 mode = 0666;
789
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000790 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100791 (!S_ISREG(a->perm))) {
792 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000793 return(-1);
794 }
795
Damien Miller16a13332002-02-13 14:03:56 +1100796 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
797 size = a->size;
798 else
799 size = 0;
800
Damien Miller3db5f532002-02-13 14:10:32 +1100801 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100802 buffer_init(&msg);
803
804 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100805 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100806 buffer_put_char(&msg, SSH2_FXP_OPEN);
807 buffer_put_int(&msg, id);
808 buffer_put_cstring(&msg, remote_path);
809 buffer_put_int(&msg, SSH2_FXF_READ);
810 attrib_clear(&junk); /* Send empty attributes */
811 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100812 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000813 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100814
Damien Miller3db5f532002-02-13 14:10:32 +1100815 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100816 if (handle == NULL) {
817 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100818 return(-1);
819 }
820
Damien Millera8e06ce2003-11-21 23:48:55 +1100821 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +1100822 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100823 if (local_fd == -1) {
824 error("Couldn't open local file \"%s\" for writing: %s",
825 local_path, strerror(errno));
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000826 buffer_free(&msg);
827 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100828 return(-1);
829 }
830
Damien Miller33804262001-02-04 23:20:18 +1100831 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100832 write_error = read_error = write_errno = num_req = offset = 0;
833 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +1100834 progress_counter = 0;
835
Damien Miller9ba30692004-03-08 23:12:02 +1100836 if (showprogress && size != 0)
837 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +1100838
Damien Miller16a13332002-02-13 14:03:56 +1100839 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100840 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100841 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100842
Darren Tuckercdf547a2004-05-24 10:12:19 +1000843 /*
Darren Tuckerfc959702004-07-17 16:12:08 +1000844 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +1000845 * allow outstanding requests to drain gracefully
846 */
847 if (interrupted) {
848 if (num_req == 0) /* If we haven't started yet... */
849 break;
850 max_req = 0;
851 }
852
Damien Miller16a13332002-02-13 14:03:56 +1100853 /* Send some more requests */
854 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000855 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000856 (unsigned long long)offset,
857 (unsigned long long)offset + buflen - 1,
858 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100859 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100860 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100861 req->len = buflen;
862 req->offset = offset;
863 offset += buflen;
864 num_req++;
865 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000866 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +1100867 req->len, handle, handle_len);
868 }
Damien Miller33804262001-02-04 23:20:18 +1100869
870 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100871 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100872 type = buffer_get_char(&msg);
873 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000874 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +1100875
Damien Miller16a13332002-02-13 14:03:56 +1100876 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +1100877 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +1100878 req != NULL && req->id != id;
879 req = TAILQ_NEXT(req, tq))
880 ;
881 if (req == NULL)
882 fatal("Unexpected reply %u", id);
883
884 switch (type) {
885 case SSH2_FXP_STATUS:
886 status = buffer_get_int(&msg);
887 if (status != SSH2_FX_EOF)
888 read_error = 1;
889 max_req = 0;
890 TAILQ_REMOVE(&requests, req, tq);
891 xfree(req);
892 num_req--;
893 break;
894 case SSH2_FXP_DATA:
895 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +0000896 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000897 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +0000898 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +1100899 if (len > req->len)
900 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +0000901 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +1100902 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +1000903 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +1100904 !write_error) {
905 write_errno = errno;
906 write_error = 1;
907 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +1100908 }
Damien Miller62d57f62003-01-10 21:43:24 +1100909 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +1100910 xfree(data);
911
912 if (len == req->len) {
913 TAILQ_REMOVE(&requests, req, tq);
914 xfree(req);
915 num_req--;
916 } else {
917 /* Resend the request for the missing data */
918 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +0000919 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000920 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +0000921 (unsigned long long)req->offset +
922 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +1100923 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100924 req->len -= len;
925 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000926 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +1100927 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100928 /* Reduce the request size */
929 if (len < buflen)
930 buflen = MAX(MIN_READ_SIZE, len);
931 }
932 if (max_req > 0) { /* max_req = 0 iff EOF received */
933 if (size > 0 && offset > size) {
934 /* Only one request at a time
935 * after the expected EOF */
936 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +0000937 (unsigned long long)offset,
938 num_req);
Damien Miller16a13332002-02-13 14:03:56 +1100939 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000940 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +1100941 ++max_req;
942 }
943 }
944 break;
945 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000946 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100947 SSH2_FXP_DATA, type);
948 }
Damien Miller33804262001-02-04 23:20:18 +1100949 }
Damien Miller33804262001-02-04 23:20:18 +1100950
Damien Miller62d57f62003-01-10 21:43:24 +1100951 if (showprogress && size)
952 stop_progress_meter();
953
Damien Miller16a13332002-02-13 14:03:56 +1100954 /* Sanity check */
955 if (TAILQ_FIRST(&requests) != NULL)
956 fatal("Transfer complete, but requests still in queue");
957
958 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000959 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +1100960 remote_path, fx2txt(status));
961 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100962 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +1100963 error("Couldn't write to \"%s\": %s", local_path,
964 strerror(write_errno));
965 status = -1;
966 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100967 } else {
Damien Miller3db5f532002-02-13 14:10:32 +1100968 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100969
970 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000971#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +1100972 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +1100973#else
Damien Miller16a13332002-02-13 14:03:56 +1100974 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000975#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +1100976 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +0000977 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100978 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
979 struct timeval tv[2];
980 tv[0].tv_sec = a->atime;
981 tv[1].tv_sec = a->mtime;
982 tv[0].tv_usec = tv[1].tv_usec = 0;
983 if (utimes(local_path, tv) == -1)
984 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +0000985 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100986 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000987 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100988 close(local_fd);
989 buffer_free(&msg);
990 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100991
992 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100993}
994
995int
Damien Miller3db5f532002-02-13 14:10:32 +1100996do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
997 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100998{
Damien Miller8829d362002-02-08 22:04:05 +1100999 int local_fd, status;
Damien Miller5873dfd2002-02-13 14:04:37 +11001000 u_int handle_len, id, type;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001001 off_t offset;
Damien Miller8829d362002-02-08 22:04:05 +11001002 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +11001003 Buffer msg;
1004 struct stat sb;
1005 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +11001006 u_int32_t startid;
1007 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001008 struct outstanding_ack {
1009 u_int id;
1010 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001011 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001012 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001013 };
1014 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001015 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001016
1017 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001018
1019 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1020 error("Couldn't open local file \"%s\" for reading: %s",
1021 local_path, strerror(errno));
1022 return(-1);
1023 }
1024 if (fstat(local_fd, &sb) == -1) {
1025 error("Couldn't fstat local file \"%s\": %s",
1026 local_path, strerror(errno));
1027 close(local_fd);
1028 return(-1);
1029 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001030 if (!S_ISREG(sb.st_mode)) {
1031 error("%s is not a regular file", local_path);
1032 close(local_fd);
1033 return(-1);
1034 }
Damien Miller33804262001-02-04 23:20:18 +11001035 stat_to_attrib(&sb, &a);
1036
1037 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1038 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1039 a.perm &= 0777;
1040 if (!pflag)
1041 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1042
1043 buffer_init(&msg);
1044
1045 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001046 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001047 buffer_put_char(&msg, SSH2_FXP_OPEN);
1048 buffer_put_int(&msg, id);
1049 buffer_put_cstring(&msg, remote_path);
1050 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1051 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001052 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001053 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001054
1055 buffer_clear(&msg);
1056
Damien Miller3db5f532002-02-13 14:10:32 +11001057 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001058 if (handle == NULL) {
1059 close(local_fd);
1060 buffer_free(&msg);
1061 return(-1);
1062 }
1063
Damien Miller16a13332002-02-13 14:03:56 +11001064 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001065 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001066
Damien Miller33804262001-02-04 23:20:18 +11001067 /* Read from local and write to remote */
1068 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001069 if (showprogress)
1070 start_progress_meter(local_path, sb.st_size, &offset);
Damien Miller62d57f62003-01-10 21:43:24 +11001071
Damien Miller9f0f5c62001-12-21 14:45:46 +11001072 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001073 int len;
Damien Miller33804262001-02-04 23:20:18 +11001074
1075 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001076 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001077 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001078 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001079 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001080 */
Darren Tuckercdf547a2004-05-24 10:12:19 +10001081 if (interrupted)
1082 len = 0;
1083 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001084 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001085 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1086
1087 if (len == -1)
1088 fatal("Couldn't read from \"%s\": %s", local_path,
1089 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001090
1091 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001092 ack = xmalloc(sizeof(*ack));
1093 ack->id = ++id;
1094 ack->offset = offset;
1095 ack->len = len;
1096 TAILQ_INSERT_TAIL(&acks, ack, tq);
1097
Damien Miller16a13332002-02-13 14:03:56 +11001098 buffer_clear(&msg);
1099 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001100 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001101 buffer_put_string(&msg, handle, handle_len);
1102 buffer_put_int64(&msg, offset);
1103 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001104 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001105 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001106 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001107 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001108 break;
1109
Damien Miller5873dfd2002-02-13 14:04:37 +11001110 if (ack == NULL)
1111 fatal("Unexpected ACK %u", id);
1112
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001113 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001114 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001115 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001116
Damien Miller5873dfd2002-02-13 14:04:37 +11001117 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001118 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001119 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001120 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001121
1122 if (type != SSH2_FXP_STATUS)
1123 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1124 "got %d", SSH2_FXP_STATUS, type);
1125
1126 status = buffer_get_int(&msg);
1127 debug3("SSH2_FXP_STATUS %d", status);
1128
1129 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001130 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001131 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001132 ack = TAILQ_NEXT(ack, tq))
1133 ;
1134 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001135 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001136 TAILQ_REMOVE(&acks, ack, tq);
1137
Damien Miller16a13332002-02-13 14:03:56 +11001138 if (status != SSH2_FX_OK) {
1139 error("Couldn't write to remote file \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001140 remote_path, fx2txt(status));
Damien Miller50455892006-10-24 03:03:02 +10001141 if (showprogress)
1142 stop_progress_meter();
Damien Miller3db5f532002-02-13 14:10:32 +11001143 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001144 close(local_fd);
Damien Miller00111382003-03-10 11:21:17 +11001145 xfree(data);
1146 xfree(ack);
Darren Tucker0aa3dbb2007-02-19 22:13:39 +11001147 status = -1;
Damien Miller16a13332002-02-13 14:03:56 +11001148 goto done;
1149 }
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001150 debug3("In write loop, ack for %u %u bytes at %lld",
1151 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001152 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001153 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001154 }
Damien Miller33804262001-02-04 23:20:18 +11001155 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001156 if (offset < 0)
1157 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001158 }
Damien Miller62d57f62003-01-10 21:43:24 +11001159 if (showprogress)
1160 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001161 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001162
1163 if (close(local_fd) == -1) {
1164 error("Couldn't close local file \"%s\": %s", local_path,
1165 strerror(errno));
Damien Miller3db5f532002-02-13 14:10:32 +11001166 do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001167 status = -1;
1168 goto done;
Damien Miller33804262001-02-04 23:20:18 +11001169 }
1170
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001171 /* Override umask and utimes if asked */
1172 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001173 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001174
Damien Miller3db5f532002-02-13 14:10:32 +11001175 status = do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001176
1177done:
1178 xfree(handle);
1179 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001180 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001181}