blob: 3fac22beeb26943da6278a0e2872e95f69246eb2 [file] [log] [blame]
Damien Miller33804262001-02-04 23:20:18 +11001/*
Damien Millera7f3aaa2003-01-10 21:43:58 +11002 * Copyright (c) 2001-2003 Damien Miller. All rights reserved.
Damien Miller33804262001-02-04 23:20:18 +11003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25/* XXX: memleaks */
26/* XXX: signed vs unsigned */
Damien Miller3db5f532002-02-13 14:10:32 +110027/* XXX: remove all logging, only return status codes */
Damien Miller33804262001-02-04 23:20:18 +110028/* XXX: copy between two remote sites */
29
30#include "includes.h"
Damien Millera7f3aaa2003-01-10 21:43:58 +110031RCSID("$OpenBSD: sftp-client.c,v 1.40 2003/01/10 08:48:15 djm Exp $");
Damien Miller16a13332002-02-13 14:03:56 +110032
Damien Miller9b481512002-09-12 10:43:29 +100033#include "openbsd-compat/sys-queue.h"
Damien Miller33804262001-02-04 23:20:18 +110034
Damien Miller33804262001-02-04 23:20:18 +110035#include "buffer.h"
36#include "bufaux.h"
37#include "getput.h"
38#include "xmalloc.h"
39#include "log.h"
40#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110041#include "progressmeter.h"
Damien Miller33804262001-02-04 23:20:18 +110042
43#include "sftp.h"
44#include "sftp-common.h"
45#include "sftp-client.h"
46
Damien Miller62d57f62003-01-10 21:43:24 +110047extern int showprogress;
48
Damien Miller16a13332002-02-13 14:03:56 +110049/* Minimum amount of data to read at at time */
50#define MIN_READ_SIZE 512
51
Damien Millera7f3aaa2003-01-10 21:43:58 +110052/* Maximum packet size */
53#define MAX_MSG_LENGTH (256 * 1024)
54
Damien Miller3db5f532002-02-13 14:10:32 +110055struct sftp_conn {
56 int fd_in;
57 int fd_out;
58 u_int transfer_buflen;
59 u_int num_requests;
60 u_int version;
61 u_int msg_id;
62};
Ben Lindstrom288cc392001-02-09 02:58:04 +000063
Ben Lindstrombba81212001-06-25 05:01:22 +000064static void
Damien Miller33804262001-02-04 23:20:18 +110065send_msg(int fd, Buffer *m)
66{
Damien Millera7f3aaa2003-01-10 21:43:58 +110067 u_char mlen[4];
Damien Miller33804262001-02-04 23:20:18 +110068
Damien Millera7f3aaa2003-01-10 21:43:58 +110069 if (buffer_len(m) > MAX_MSG_LENGTH)
70 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +110071
Damien Millera7f3aaa2003-01-10 21:43:58 +110072 /* Send length first */
73 PUT_32BIT(mlen, buffer_len(m));
74 if (atomicio(write, fd, mlen, sizeof(mlen)) <= 0)
Damien Miller33804262001-02-04 23:20:18 +110075 fatal("Couldn't send packet: %s", strerror(errno));
76
Damien Millera7f3aaa2003-01-10 21:43:58 +110077 if (atomicio(write, fd, buffer_ptr(m), buffer_len(m)) <= 0)
78 fatal("Couldn't send packet: %s", strerror(errno));
79
80 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +110081}
82
Ben Lindstrombba81212001-06-25 05:01:22 +000083static void
Damien Miller33804262001-02-04 23:20:18 +110084get_msg(int fd, Buffer *m)
85{
Damien Millera7f3aaa2003-01-10 21:43:58 +110086 ssize_t len;
87 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +110088
Damien Millera7f3aaa2003-01-10 21:43:58 +110089 buffer_append_space(m, 4);
90 len = atomicio(read, fd, buffer_ptr(m), 4);
Damien Millercafff192001-03-19 22:29:46 +110091 if (len == 0)
92 fatal("Connection closed");
93 else if (len == -1)
Damien Miller33804262001-02-04 23:20:18 +110094 fatal("Couldn't read packet: %s", strerror(errno));
95
Damien Millera7f3aaa2003-01-10 21:43:58 +110096 msg_len = buffer_get_int(m);
97 if (msg_len > MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +000098 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +110099
Damien Millera7f3aaa2003-01-10 21:43:58 +1100100 buffer_append_space(m, msg_len);
101 len = atomicio(read, fd, buffer_ptr(m), msg_len);
102 if (len == 0)
103 fatal("Connection closed");
104 else if (len == -1)
105 fatal("Read packet: %s", strerror(errno));
Damien Miller33804262001-02-04 23:20:18 +1100106}
107
Ben Lindstrombba81212001-06-25 05:01:22 +0000108static void
Damien Miller33804262001-02-04 23:20:18 +1100109send_string_request(int fd, u_int id, u_int code, char *s,
110 u_int len)
111{
112 Buffer msg;
113
114 buffer_init(&msg);
115 buffer_put_char(&msg, code);
116 buffer_put_int(&msg, id);
117 buffer_put_string(&msg, s, len);
118 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000119 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100120 buffer_free(&msg);
121}
122
Ben Lindstrombba81212001-06-25 05:01:22 +0000123static void
Damien Miller33804262001-02-04 23:20:18 +1100124send_string_attrs_request(int fd, u_int id, u_int code, char *s,
125 u_int len, Attrib *a)
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 encode_attrib(&msg, a);
134 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000135 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100136 buffer_free(&msg);
137}
138
Ben Lindstrombba81212001-06-25 05:01:22 +0000139static u_int
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000140get_status(int fd, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100141{
142 Buffer msg;
143 u_int type, id, status;
144
145 buffer_init(&msg);
146 get_msg(fd, &msg);
147 type = buffer_get_char(&msg);
148 id = buffer_get_int(&msg);
149
150 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000151 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100152 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000153 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100154 SSH2_FXP_STATUS, type);
155
156 status = buffer_get_int(&msg);
157 buffer_free(&msg);
158
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000159 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100160
161 return(status);
162}
163
Ben Lindstrombba81212001-06-25 05:01:22 +0000164static char *
Damien Miller33804262001-02-04 23:20:18 +1100165get_handle(int fd, u_int expected_id, u_int *len)
166{
167 Buffer msg;
168 u_int type, id;
169 char *handle;
170
171 buffer_init(&msg);
172 get_msg(fd, &msg);
173 type = buffer_get_char(&msg);
174 id = buffer_get_int(&msg);
175
176 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000177 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100178 if (type == SSH2_FXP_STATUS) {
179 int status = buffer_get_int(&msg);
180
181 error("Couldn't get handle: %s", fx2txt(status));
182 return(NULL);
183 } else if (type != SSH2_FXP_HANDLE)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000184 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100185 SSH2_FXP_HANDLE, type);
186
187 handle = buffer_get_string(&msg, len);
188 buffer_free(&msg);
189
190 return(handle);
191}
192
Ben Lindstrombba81212001-06-25 05:01:22 +0000193static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000194get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100195{
196 Buffer msg;
197 u_int type, id;
198 Attrib *a;
199
200 buffer_init(&msg);
201 get_msg(fd, &msg);
202
203 type = buffer_get_char(&msg);
204 id = buffer_get_int(&msg);
205
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000206 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100207 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000208 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100209 if (type == SSH2_FXP_STATUS) {
210 int status = buffer_get_int(&msg);
211
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000212 if (quiet)
213 debug("Couldn't stat remote file: %s", fx2txt(status));
214 else
215 error("Couldn't stat remote file: %s", fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100216 return(NULL);
217 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000218 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100219 SSH2_FXP_ATTRS, type);
220 }
221 a = decode_attrib(&msg);
222 buffer_free(&msg);
223
224 return(a);
225}
226
Damien Miller3db5f532002-02-13 14:10:32 +1100227struct sftp_conn *
228do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100229{
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000230 u_int type;
231 int version;
Damien Miller33804262001-02-04 23:20:18 +1100232 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100233 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100234
235 buffer_init(&msg);
236 buffer_put_char(&msg, SSH2_FXP_INIT);
237 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
238 send_msg(fd_out, &msg);
239
240 buffer_clear(&msg);
241
242 get_msg(fd_in, &msg);
243
Kevin Stevesef4eea92001-02-05 12:42:17 +0000244 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100245 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000246 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100247 type);
248 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100249 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100250 }
251 version = buffer_get_int(&msg);
252
253 debug2("Remote version: %d", version);
254
255 /* Check for extensions */
256 while (buffer_len(&msg) > 0) {
257 char *name = buffer_get_string(&msg, NULL);
258 char *value = buffer_get_string(&msg, NULL);
259
260 debug2("Init extension: \"%s\"", name);
261 xfree(name);
262 xfree(value);
263 }
264
265 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100266
Damien Miller3db5f532002-02-13 14:10:32 +1100267 ret = xmalloc(sizeof(*ret));
268 ret->fd_in = fd_in;
269 ret->fd_out = fd_out;
270 ret->transfer_buflen = transfer_buflen;
271 ret->num_requests = num_requests;
272 ret->version = version;
273 ret->msg_id = 1;
274
275 /* Some filexfer v.0 servers don't support large packets */
276 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000277 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100278
279 return(ret);
280}
281
282u_int
283sftp_proto_version(struct sftp_conn *conn)
284{
285 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100286}
287
288int
Damien Miller3db5f532002-02-13 14:10:32 +1100289do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100290{
291 u_int id, status;
292 Buffer msg;
293
294 buffer_init(&msg);
295
Damien Miller3db5f532002-02-13 14:10:32 +1100296 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100297 buffer_put_char(&msg, SSH2_FXP_CLOSE);
298 buffer_put_int(&msg, id);
299 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100300 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000301 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100302
Damien Miller3db5f532002-02-13 14:10:32 +1100303 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100304 if (status != SSH2_FX_OK)
305 error("Couldn't close file: %s", fx2txt(status));
306
307 buffer_free(&msg);
308
309 return(status);
310}
311
Damien Miller4870afd2001-03-14 10:27:09 +1100312
Ben Lindstrombba81212001-06-25 05:01:22 +0000313static int
Damien Miller3db5f532002-02-13 14:10:32 +1100314do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100315 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100316{
317 Buffer msg;
Ben Lindstrom025df4a2001-03-14 15:16:34 +0000318 u_int type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100319 char *handle;
320
Damien Miller3db5f532002-02-13 14:10:32 +1100321 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100322
323 buffer_init(&msg);
324 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
325 buffer_put_int(&msg, id);
326 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100327 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100328
329 buffer_clear(&msg);
330
Damien Miller3db5f532002-02-13 14:10:32 +1100331 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100332 if (handle == NULL)
333 return(-1);
334
Damien Miller4870afd2001-03-14 10:27:09 +1100335 if (dir) {
336 ents = 0;
337 *dir = xmalloc(sizeof(**dir));
338 (*dir)[0] = NULL;
339 }
Damien Miller4870afd2001-03-14 10:27:09 +1100340
Damien Miller9f0f5c62001-12-21 14:45:46 +1100341 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +1100342 int count;
343
Damien Miller3db5f532002-02-13 14:10:32 +1100344 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100345
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000346 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100347
348 buffer_clear(&msg);
349 buffer_put_char(&msg, SSH2_FXP_READDIR);
350 buffer_put_int(&msg, id);
351 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100352 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100353
354 buffer_clear(&msg);
355
Damien Miller3db5f532002-02-13 14:10:32 +1100356 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100357
358 type = buffer_get_char(&msg);
359 id = buffer_get_int(&msg);
360
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000361 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100362
363 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000364 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100365
366 if (type == SSH2_FXP_STATUS) {
367 int status = buffer_get_int(&msg);
368
369 debug3("Received SSH2_FXP_STATUS %d", status);
370
371 if (status == SSH2_FX_EOF) {
372 break;
373 } else {
374 error("Couldn't read directory: %s",
375 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100376 do_close(conn, handle, handle_len);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000377 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100378 }
379 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000380 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100381 SSH2_FXP_NAME, type);
382
383 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100384 if (count == 0)
385 break;
386 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100387 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100388 char *filename, *longname;
389 Attrib *a;
390
391 filename = buffer_get_string(&msg, NULL);
392 longname = buffer_get_string(&msg, NULL);
393 a = decode_attrib(&msg);
394
Damien Miller4870afd2001-03-14 10:27:09 +1100395 if (printflag)
396 printf("%s\n", longname);
397
398 if (dir) {
Ben Lindstroma3700052001-04-05 23:26:32 +0000399 *dir = xrealloc(*dir, sizeof(**dir) *
Damien Miller4870afd2001-03-14 10:27:09 +1100400 (ents + 2));
401 (*dir)[ents] = xmalloc(sizeof(***dir));
402 (*dir)[ents]->filename = xstrdup(filename);
403 (*dir)[ents]->longname = xstrdup(longname);
404 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
405 (*dir)[++ents] = NULL;
406 }
Damien Miller33804262001-02-04 23:20:18 +1100407
408 xfree(filename);
409 xfree(longname);
410 }
411 }
412
413 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100414 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100415 xfree(handle);
416
417 return(0);
418}
419
420int
Damien Miller3db5f532002-02-13 14:10:32 +1100421do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100422{
Damien Miller3db5f532002-02-13 14:10:32 +1100423 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100424}
425
426void free_sftp_dirents(SFTP_DIRENT **s)
427{
428 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100429
430 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100431 xfree(s[i]->filename);
432 xfree(s[i]->longname);
433 xfree(s[i]);
434 }
435 xfree(s);
436}
437
438int
Damien Miller3db5f532002-02-13 14:10:32 +1100439do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100440{
441 u_int status, id;
442
443 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
444
Damien Miller3db5f532002-02-13 14:10:32 +1100445 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000446 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100447 strlen(path));
448 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100449 if (status != SSH2_FX_OK)
450 error("Couldn't delete file: %s", fx2txt(status));
451 return(status);
452}
453
454int
Damien Miller3db5f532002-02-13 14:10:32 +1100455do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100456{
457 u_int status, id;
458
Damien Miller3db5f532002-02-13 14:10:32 +1100459 id = conn->msg_id++;
460 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100461 strlen(path), a);
462
Damien Miller3db5f532002-02-13 14:10:32 +1100463 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100464 if (status != SSH2_FX_OK)
465 error("Couldn't create directory: %s", fx2txt(status));
466
467 return(status);
468}
469
470int
Damien Miller3db5f532002-02-13 14:10:32 +1100471do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100472{
473 u_int status, id;
474
Damien Miller3db5f532002-02-13 14:10:32 +1100475 id = conn->msg_id++;
476 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
477 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100478
Damien Miller3db5f532002-02-13 14:10:32 +1100479 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100480 if (status != SSH2_FX_OK)
481 error("Couldn't remove directory: %s", fx2txt(status));
482
483 return(status);
484}
485
486Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100487do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100488{
489 u_int id;
490
Damien Miller3db5f532002-02-13 14:10:32 +1100491 id = conn->msg_id++;
492
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000493 send_string_request(conn->fd_out, id,
494 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100495 path, strlen(path));
496
497 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100498}
499
500Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100501do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100502{
503 u_int id;
504
Damien Miller3db5f532002-02-13 14:10:32 +1100505 if (conn->version == 0) {
506 if (quiet)
507 debug("Server version does not support lstat operation");
508 else
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000509 log("Server version does not support lstat operation");
510 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100511 }
512
513 id = conn->msg_id++;
514 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
515 strlen(path));
516
517 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100518}
519
520Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100521do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100522{
523 u_int id;
524
Damien Miller3db5f532002-02-13 14:10:32 +1100525 id = conn->msg_id++;
526 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
527 handle_len);
528
529 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100530}
531
532int
Damien Miller3db5f532002-02-13 14:10:32 +1100533do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100534{
535 u_int status, id;
536
Damien Miller3db5f532002-02-13 14:10:32 +1100537 id = conn->msg_id++;
538 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100539 strlen(path), a);
540
Damien Miller3db5f532002-02-13 14:10:32 +1100541 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100542 if (status != SSH2_FX_OK)
543 error("Couldn't setstat on \"%s\": %s", path,
544 fx2txt(status));
545
546 return(status);
547}
548
549int
Damien Miller3db5f532002-02-13 14:10:32 +1100550do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100551 Attrib *a)
552{
553 u_int status, id;
554
Damien Miller3db5f532002-02-13 14:10:32 +1100555 id = conn->msg_id++;
556 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100557 handle_len, a);
558
Damien Miller3db5f532002-02-13 14:10:32 +1100559 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100560 if (status != SSH2_FX_OK)
561 error("Couldn't fsetstat: %s", fx2txt(status));
562
563 return(status);
564}
565
566char *
Damien Miller3db5f532002-02-13 14:10:32 +1100567do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100568{
569 Buffer msg;
570 u_int type, expected_id, count, id;
571 char *filename, *longname;
572 Attrib *a;
573
Damien Miller3db5f532002-02-13 14:10:32 +1100574 expected_id = id = conn->msg_id++;
575 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
576 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100577
578 buffer_init(&msg);
579
Damien Miller3db5f532002-02-13 14:10:32 +1100580 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100581 type = buffer_get_char(&msg);
582 id = buffer_get_int(&msg);
583
584 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000585 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100586
587 if (type == SSH2_FXP_STATUS) {
588 u_int status = buffer_get_int(&msg);
589
590 error("Couldn't canonicalise: %s", fx2txt(status));
591 return(NULL);
592 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000593 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100594 SSH2_FXP_NAME, type);
595
596 count = buffer_get_int(&msg);
597 if (count != 1)
598 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
599
600 filename = buffer_get_string(&msg, NULL);
601 longname = buffer_get_string(&msg, NULL);
602 a = decode_attrib(&msg);
603
604 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
605
606 xfree(longname);
607
608 buffer_free(&msg);
609
610 return(filename);
611}
612
613int
Damien Miller3db5f532002-02-13 14:10:32 +1100614do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100615{
616 Buffer msg;
617 u_int status, id;
618
619 buffer_init(&msg);
620
621 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100622 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100623 buffer_put_char(&msg, SSH2_FXP_RENAME);
624 buffer_put_int(&msg, id);
625 buffer_put_cstring(&msg, oldpath);
626 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100627 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100628 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
629 newpath);
630 buffer_free(&msg);
631
Damien Miller3db5f532002-02-13 14:10:32 +1100632 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100633 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100634 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
635 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100636
637 return(status);
638}
639
640int
Damien Miller3db5f532002-02-13 14:10:32 +1100641do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100642{
643 Buffer msg;
644 u_int status, id;
645
Damien Miller3db5f532002-02-13 14:10:32 +1100646 if (conn->version < 3) {
647 error("This server does not support the symlink operation");
648 return(SSH2_FX_OP_UNSUPPORTED);
649 }
650
Damien Miller058316f2001-03-08 10:08:49 +1100651 buffer_init(&msg);
652
653 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100654 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100655 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
656 buffer_put_int(&msg, id);
657 buffer_put_cstring(&msg, oldpath);
658 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100659 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100660 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
661 newpath);
662 buffer_free(&msg);
663
Damien Miller3db5f532002-02-13 14:10:32 +1100664 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100665 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000666 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100667 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100668
669 return(status);
670}
671
672char *
Damien Miller3db5f532002-02-13 14:10:32 +1100673do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100674{
675 Buffer msg;
676 u_int type, expected_id, count, id;
677 char *filename, *longname;
678 Attrib *a;
679
Damien Miller3db5f532002-02-13 14:10:32 +1100680 expected_id = id = conn->msg_id++;
681 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
682 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100683
684 buffer_init(&msg);
685
Damien Miller3db5f532002-02-13 14:10:32 +1100686 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100687 type = buffer_get_char(&msg);
688 id = buffer_get_int(&msg);
689
690 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000691 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100692
693 if (type == SSH2_FXP_STATUS) {
694 u_int status = buffer_get_int(&msg);
695
696 error("Couldn't readlink: %s", fx2txt(status));
697 return(NULL);
698 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000699 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100700 SSH2_FXP_NAME, type);
701
702 count = buffer_get_int(&msg);
703 if (count != 1)
704 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
705
706 filename = buffer_get_string(&msg, NULL);
707 longname = buffer_get_string(&msg, NULL);
708 a = decode_attrib(&msg);
709
710 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
711
712 xfree(longname);
713
714 buffer_free(&msg);
715
716 return(filename);
717}
718
Damien Miller16a13332002-02-13 14:03:56 +1100719static void
720send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
721 char *handle, u_int handle_len)
722{
723 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000724
Damien Miller16a13332002-02-13 14:03:56 +1100725 buffer_init(&msg);
726 buffer_clear(&msg);
727 buffer_put_char(&msg, SSH2_FXP_READ);
728 buffer_put_int(&msg, id);
729 buffer_put_string(&msg, handle, handle_len);
730 buffer_put_int64(&msg, offset);
731 buffer_put_int(&msg, len);
732 send_msg(fd_out, &msg);
733 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000734}
Damien Miller16a13332002-02-13 14:03:56 +1100735
Damien Miller058316f2001-03-08 10:08:49 +1100736int
Damien Miller3db5f532002-02-13 14:10:32 +1100737do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
738 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100739{
Damien Miller33804262001-02-04 23:20:18 +1100740 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100741 Buffer msg;
742 char *handle;
743 int local_fd, status, num_req, max_req, write_error;
744 int read_error, write_errno;
745 u_int64_t offset, size;
Damien Miller3db5f532002-02-13 14:10:32 +1100746 u_int handle_len, mode, type, id, buflen;
Damien Miller62d57f62003-01-10 21:43:24 +1100747 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100748 struct request {
749 u_int id;
750 u_int len;
751 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000752 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100753 };
754 TAILQ_HEAD(reqhead, request) requests;
755 struct request *req;
756
757 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100758
Damien Miller3db5f532002-02-13 14:10:32 +1100759 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100760 if (a == NULL)
761 return(-1);
762
763 /* XXX: should we preserve set[ug]id? */
764 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100765 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100766 else
767 mode = 0666;
768
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000769 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
770 (a->perm & S_IFDIR)) {
771 error("Cannot download a directory: %s", remote_path);
772 return(-1);
773 }
774
Damien Miller16a13332002-02-13 14:03:56 +1100775 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
776 size = a->size;
777 else
778 size = 0;
779
Damien Miller3db5f532002-02-13 14:10:32 +1100780 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100781 buffer_init(&msg);
782
783 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100784 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100785 buffer_put_char(&msg, SSH2_FXP_OPEN);
786 buffer_put_int(&msg, id);
787 buffer_put_cstring(&msg, remote_path);
788 buffer_put_int(&msg, SSH2_FXF_READ);
789 attrib_clear(&junk); /* Send empty attributes */
790 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100791 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000792 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100793
Damien Miller3db5f532002-02-13 14:10:32 +1100794 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100795 if (handle == NULL) {
796 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100797 return(-1);
798 }
799
Damien Miller770b3742003-01-08 14:04:53 +1100800 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
801 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100802 if (local_fd == -1) {
803 error("Couldn't open local file \"%s\" for writing: %s",
804 local_path, strerror(errno));
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000805 buffer_free(&msg);
806 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100807 return(-1);
808 }
809
Damien Miller33804262001-02-04 23:20:18 +1100810 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100811 write_error = read_error = write_errno = num_req = offset = 0;
812 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +1100813 progress_counter = 0;
814
815 if (showprogress) {
816 if (size)
817 start_progress_meter(remote_path, size,
818 &progress_counter);
819 else
820 printf("Fetching %s to %s\n", remote_path, local_path);
821 }
822
Damien Miller16a13332002-02-13 14:03:56 +1100823 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100824 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100825 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100826
Damien Miller16a13332002-02-13 14:03:56 +1100827 /* Send some more requests */
828 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000829 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000830 (unsigned long long)offset,
831 (unsigned long long)offset + buflen - 1,
832 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100833 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100834 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100835 req->len = buflen;
836 req->offset = offset;
837 offset += buflen;
838 num_req++;
839 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000840 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +1100841 req->len, handle, handle_len);
842 }
Damien Miller33804262001-02-04 23:20:18 +1100843
844 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100845 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100846 type = buffer_get_char(&msg);
847 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000848 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +1100849
Damien Miller16a13332002-02-13 14:03:56 +1100850 /* Find the request in our queue */
851 for(req = TAILQ_FIRST(&requests);
852 req != NULL && req->id != id;
853 req = TAILQ_NEXT(req, tq))
854 ;
855 if (req == NULL)
856 fatal("Unexpected reply %u", id);
857
858 switch (type) {
859 case SSH2_FXP_STATUS:
860 status = buffer_get_int(&msg);
861 if (status != SSH2_FX_EOF)
862 read_error = 1;
863 max_req = 0;
864 TAILQ_REMOVE(&requests, req, tq);
865 xfree(req);
866 num_req--;
867 break;
868 case SSH2_FXP_DATA:
869 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +0000870 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000871 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +0000872 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +1100873 if (len > req->len)
874 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +0000875 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +1100876 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Ben Lindstrom93576d92002-12-23 02:06:19 +0000877 atomicio(write, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +1100878 !write_error) {
879 write_errno = errno;
880 write_error = 1;
881 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +1100882 }
Damien Miller62d57f62003-01-10 21:43:24 +1100883 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +1100884 xfree(data);
885
886 if (len == req->len) {
887 TAILQ_REMOVE(&requests, req, tq);
888 xfree(req);
889 num_req--;
890 } else {
891 /* Resend the request for the missing data */
892 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +0000893 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000894 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +0000895 (unsigned long long)req->offset +
896 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +1100897 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100898 req->len -= len;
899 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000900 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +1100901 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100902 /* Reduce the request size */
903 if (len < buflen)
904 buflen = MAX(MIN_READ_SIZE, len);
905 }
906 if (max_req > 0) { /* max_req = 0 iff EOF received */
907 if (size > 0 && offset > size) {
908 /* Only one request at a time
909 * after the expected EOF */
910 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +0000911 (unsigned long long)offset,
912 num_req);
Damien Miller16a13332002-02-13 14:03:56 +1100913 max_req = 1;
914 }
Damien Miller3db5f532002-02-13 14:10:32 +1100915 else if (max_req < conn->num_requests + 1) {
Damien Miller16a13332002-02-13 14:03:56 +1100916 ++max_req;
917 }
918 }
919 break;
920 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000921 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100922 SSH2_FXP_DATA, type);
923 }
Damien Miller33804262001-02-04 23:20:18 +1100924 }
Damien Miller33804262001-02-04 23:20:18 +1100925
Damien Miller62d57f62003-01-10 21:43:24 +1100926 if (showprogress && size)
927 stop_progress_meter();
928
Damien Miller16a13332002-02-13 14:03:56 +1100929 /* Sanity check */
930 if (TAILQ_FIRST(&requests) != NULL)
931 fatal("Transfer complete, but requests still in queue");
932
933 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000934 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +1100935 remote_path, fx2txt(status));
936 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100937 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +1100938 error("Couldn't write to \"%s\": %s", local_path,
939 strerror(write_errno));
940 status = -1;
941 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100942 } else {
Damien Miller3db5f532002-02-13 14:10:32 +1100943 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100944
945 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000946#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +1100947 if (pflag && fchmod(local_fd, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000948#else
Damien Miller16a13332002-02-13 14:03:56 +1100949 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000950#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +1100951 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +0000952 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100953 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
954 struct timeval tv[2];
955 tv[0].tv_sec = a->atime;
956 tv[1].tv_sec = a->mtime;
957 tv[0].tv_usec = tv[1].tv_usec = 0;
958 if (utimes(local_path, tv) == -1)
959 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +0000960 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100961 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000962 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100963 close(local_fd);
964 buffer_free(&msg);
965 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100966
967 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100968}
969
970int
Damien Miller3db5f532002-02-13 14:10:32 +1100971do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
972 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100973{
Damien Miller8829d362002-02-08 22:04:05 +1100974 int local_fd, status;
Damien Miller5873dfd2002-02-13 14:04:37 +1100975 u_int handle_len, id, type;
Damien Miller33804262001-02-04 23:20:18 +1100976 u_int64_t offset;
Damien Miller8829d362002-02-08 22:04:05 +1100977 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +1100978 Buffer msg;
979 struct stat sb;
980 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +1100981 u_int32_t startid;
982 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +1100983 struct outstanding_ack {
984 u_int id;
985 u_int len;
986 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000987 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +1100988 };
989 TAILQ_HEAD(ackhead, outstanding_ack) acks;
990 struct outstanding_ack *ack;
991
992 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +1100993
994 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
995 error("Couldn't open local file \"%s\" for reading: %s",
996 local_path, strerror(errno));
997 return(-1);
998 }
999 if (fstat(local_fd, &sb) == -1) {
1000 error("Couldn't fstat local file \"%s\": %s",
1001 local_path, strerror(errno));
1002 close(local_fd);
1003 return(-1);
1004 }
1005 stat_to_attrib(&sb, &a);
1006
1007 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1008 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1009 a.perm &= 0777;
1010 if (!pflag)
1011 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1012
1013 buffer_init(&msg);
1014
1015 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001016 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001017 buffer_put_char(&msg, SSH2_FXP_OPEN);
1018 buffer_put_int(&msg, id);
1019 buffer_put_cstring(&msg, remote_path);
1020 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1021 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001022 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001023 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001024
1025 buffer_clear(&msg);
1026
Damien Miller3db5f532002-02-13 14:10:32 +11001027 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001028 if (handle == NULL) {
1029 close(local_fd);
1030 buffer_free(&msg);
1031 return(-1);
1032 }
1033
Damien Miller16a13332002-02-13 14:03:56 +11001034 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001035 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001036
Damien Miller33804262001-02-04 23:20:18 +11001037 /* Read from local and write to remote */
1038 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001039 if (showprogress)
1040 start_progress_meter(local_path, sb.st_size, &offset);
1041 else
1042 printf("Uploading %s to %s\n", local_path, remote_path);
1043
Damien Miller9f0f5c62001-12-21 14:45:46 +11001044 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001045 int len;
Damien Miller33804262001-02-04 23:20:18 +11001046
1047 /*
1048 * Can't use atomicio here because it returns 0 on EOF, thus losing
1049 * the last block of the file
1050 */
1051 do
Damien Miller3db5f532002-02-13 14:10:32 +11001052 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001053 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1054
1055 if (len == -1)
1056 fatal("Couldn't read from \"%s\": %s", local_path,
1057 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001058
1059 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001060 ack = xmalloc(sizeof(*ack));
1061 ack->id = ++id;
1062 ack->offset = offset;
1063 ack->len = len;
1064 TAILQ_INSERT_TAIL(&acks, ack, tq);
1065
Damien Miller16a13332002-02-13 14:03:56 +11001066 buffer_clear(&msg);
1067 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001068 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001069 buffer_put_string(&msg, handle, handle_len);
1070 buffer_put_int64(&msg, offset);
1071 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001072 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001073 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001074 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001075 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001076 break;
1077
Damien Miller5873dfd2002-02-13 14:04:37 +11001078 if (ack == NULL)
1079 fatal("Unexpected ACK %u", id);
1080
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001081 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001082 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001083 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001084
Damien Miller5873dfd2002-02-13 14:04:37 +11001085 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001086 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001087 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001088 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001089
1090 if (type != SSH2_FXP_STATUS)
1091 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1092 "got %d", SSH2_FXP_STATUS, type);
1093
1094 status = buffer_get_int(&msg);
1095 debug3("SSH2_FXP_STATUS %d", status);
1096
1097 /* Find the request in our queue */
1098 for(ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001099 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001100 ack = TAILQ_NEXT(ack, tq))
1101 ;
1102 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001103 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001104 TAILQ_REMOVE(&acks, ack, tq);
1105
Damien Miller16a13332002-02-13 14:03:56 +11001106 if (status != SSH2_FX_OK) {
1107 error("Couldn't write to remote file \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001108 remote_path, fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +11001109 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001110 close(local_fd);
1111 goto done;
1112 }
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001113 debug3("In write loop, ack for %u %u bytes at %llu",
Ben Lindstromeb505452002-03-22 01:03:15 +00001114 ack->id, ack->len, (unsigned long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001115 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001116 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001117 }
Damien Miller33804262001-02-04 23:20:18 +11001118 offset += len;
1119 }
Damien Miller62d57f62003-01-10 21:43:24 +11001120 if (showprogress)
1121 stop_progress_meter();
Damien Miller8829d362002-02-08 22:04:05 +11001122 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001123
1124 if (close(local_fd) == -1) {
1125 error("Couldn't close local file \"%s\": %s", local_path,
1126 strerror(errno));
Damien Miller3db5f532002-02-13 14:10:32 +11001127 do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001128 status = -1;
1129 goto done;
Damien Miller33804262001-02-04 23:20:18 +11001130 }
1131
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001132 /* Override umask and utimes if asked */
1133 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001134 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001135
Damien Miller3db5f532002-02-13 14:10:32 +11001136 status = do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001137
1138done:
1139 xfree(handle);
1140 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001141 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001142}