blob: 81c5dd49732fb7f7a09c5f79c6369e0d0898398f [file] [log] [blame]
Damien Miller33804262001-02-04 23:20:18 +11001/*
Damien Miller4e60ed72004-02-17 17:07:59 +11002 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
Damien Miller33804262001-02-04 23:20:18 +11003 *
Damien Miller4e60ed72004-02-17 17:07:59 +11004 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
Damien Miller33804262001-02-04 23:20:18 +11007 *
Damien Miller4e60ed72004-02-17 17:07:59 +11008 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller33804262001-02-04 23:20:18 +110015 */
16
17/* XXX: memleaks */
18/* XXX: signed vs unsigned */
Damien Miller3db5f532002-02-13 14:10:32 +110019/* XXX: remove all logging, only return status codes */
Damien Miller33804262001-02-04 23:20:18 +110020/* XXX: copy between two remote sites */
21
22#include "includes.h"
Damien Miller4e60ed72004-02-17 17:07:59 +110023RCSID("$OpenBSD: sftp-client.c,v 1.46 2004/02/17 05:39:51 djm Exp $");
Damien Miller16a13332002-02-13 14:03:56 +110024
Damien Miller9b481512002-09-12 10:43:29 +100025#include "openbsd-compat/sys-queue.h"
Damien Miller33804262001-02-04 23:20:18 +110026
Damien Miller33804262001-02-04 23:20:18 +110027#include "buffer.h"
28#include "bufaux.h"
29#include "getput.h"
30#include "xmalloc.h"
31#include "log.h"
32#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110033#include "progressmeter.h"
Damien Miller33804262001-02-04 23:20:18 +110034
35#include "sftp.h"
36#include "sftp-common.h"
37#include "sftp-client.h"
38
Damien Miller62d57f62003-01-10 21:43:24 +110039extern int showprogress;
40
Damien Miller16a13332002-02-13 14:03:56 +110041/* Minimum amount of data to read at at time */
42#define MIN_READ_SIZE 512
43
Damien Millera7f3aaa2003-01-10 21:43:58 +110044/* Maximum packet size */
45#define MAX_MSG_LENGTH (256 * 1024)
46
Damien Miller3db5f532002-02-13 14:10:32 +110047struct sftp_conn {
48 int fd_in;
49 int fd_out;
50 u_int transfer_buflen;
51 u_int num_requests;
52 u_int version;
53 u_int msg_id;
54};
Ben Lindstrom288cc392001-02-09 02:58:04 +000055
Ben Lindstrombba81212001-06-25 05:01:22 +000056static void
Damien Miller33804262001-02-04 23:20:18 +110057send_msg(int fd, Buffer *m)
58{
Damien Millera7f3aaa2003-01-10 21:43:58 +110059 u_char mlen[4];
Damien Miller33804262001-02-04 23:20:18 +110060
Damien Millera7f3aaa2003-01-10 21:43:58 +110061 if (buffer_len(m) > MAX_MSG_LENGTH)
62 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +110063
Damien Millera7f3aaa2003-01-10 21:43:58 +110064 /* Send length first */
65 PUT_32BIT(mlen, buffer_len(m));
Darren Tucker9f63f222003-07-03 13:46:56 +100066 if (atomicio(vwrite, fd, mlen, sizeof(mlen)) <= 0)
Damien Miller33804262001-02-04 23:20:18 +110067 fatal("Couldn't send packet: %s", strerror(errno));
68
Darren Tucker9f63f222003-07-03 13:46:56 +100069 if (atomicio(vwrite, fd, buffer_ptr(m), buffer_len(m)) <= 0)
Damien Millera7f3aaa2003-01-10 21:43:58 +110070 fatal("Couldn't send packet: %s", strerror(errno));
71
72 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +110073}
74
Ben Lindstrombba81212001-06-25 05:01:22 +000075static void
Damien Miller33804262001-02-04 23:20:18 +110076get_msg(int fd, Buffer *m)
77{
Damien Millera7f3aaa2003-01-10 21:43:58 +110078 ssize_t len;
79 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +110080
Damien Millera7f3aaa2003-01-10 21:43:58 +110081 buffer_append_space(m, 4);
82 len = atomicio(read, fd, buffer_ptr(m), 4);
Damien Millercafff192001-03-19 22:29:46 +110083 if (len == 0)
84 fatal("Connection closed");
85 else if (len == -1)
Damien Miller33804262001-02-04 23:20:18 +110086 fatal("Couldn't read packet: %s", strerror(errno));
87
Damien Millera7f3aaa2003-01-10 21:43:58 +110088 msg_len = buffer_get_int(m);
89 if (msg_len > MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +000090 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +110091
Damien Millera7f3aaa2003-01-10 21:43:58 +110092 buffer_append_space(m, msg_len);
93 len = atomicio(read, fd, buffer_ptr(m), msg_len);
94 if (len == 0)
95 fatal("Connection closed");
96 else if (len == -1)
97 fatal("Read packet: %s", strerror(errno));
Damien Miller33804262001-02-04 23:20:18 +110098}
99
Ben Lindstrombba81212001-06-25 05:01:22 +0000100static void
Damien Miller33804262001-02-04 23:20:18 +1100101send_string_request(int fd, u_int id, u_int code, char *s,
102 u_int len)
103{
104 Buffer msg;
105
106 buffer_init(&msg);
107 buffer_put_char(&msg, code);
108 buffer_put_int(&msg, id);
109 buffer_put_string(&msg, s, len);
110 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000111 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100112 buffer_free(&msg);
113}
114
Ben Lindstrombba81212001-06-25 05:01:22 +0000115static void
Damien Miller33804262001-02-04 23:20:18 +1100116send_string_attrs_request(int fd, u_int id, u_int code, char *s,
117 u_int len, Attrib *a)
118{
119 Buffer msg;
120
121 buffer_init(&msg);
122 buffer_put_char(&msg, code);
123 buffer_put_int(&msg, id);
124 buffer_put_string(&msg, s, len);
125 encode_attrib(&msg, a);
126 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000127 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100128 buffer_free(&msg);
129}
130
Ben Lindstrombba81212001-06-25 05:01:22 +0000131static u_int
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000132get_status(int fd, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100133{
134 Buffer msg;
135 u_int type, id, status;
136
137 buffer_init(&msg);
138 get_msg(fd, &msg);
139 type = buffer_get_char(&msg);
140 id = buffer_get_int(&msg);
141
142 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000143 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100144 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000145 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100146 SSH2_FXP_STATUS, type);
147
148 status = buffer_get_int(&msg);
149 buffer_free(&msg);
150
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000151 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100152
153 return(status);
154}
155
Ben Lindstrombba81212001-06-25 05:01:22 +0000156static char *
Damien Miller33804262001-02-04 23:20:18 +1100157get_handle(int fd, u_int expected_id, u_int *len)
158{
159 Buffer msg;
160 u_int type, id;
161 char *handle;
162
163 buffer_init(&msg);
164 get_msg(fd, &msg);
165 type = buffer_get_char(&msg);
166 id = buffer_get_int(&msg);
167
168 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000169 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100170 if (type == SSH2_FXP_STATUS) {
171 int status = buffer_get_int(&msg);
172
173 error("Couldn't get handle: %s", fx2txt(status));
174 return(NULL);
175 } else if (type != SSH2_FXP_HANDLE)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000176 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100177 SSH2_FXP_HANDLE, type);
178
179 handle = buffer_get_string(&msg, len);
180 buffer_free(&msg);
181
182 return(handle);
183}
184
Ben Lindstrombba81212001-06-25 05:01:22 +0000185static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000186get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100187{
188 Buffer msg;
189 u_int type, id;
190 Attrib *a;
191
192 buffer_init(&msg);
193 get_msg(fd, &msg);
194
195 type = buffer_get_char(&msg);
196 id = buffer_get_int(&msg);
197
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000198 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100199 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000200 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100201 if (type == SSH2_FXP_STATUS) {
202 int status = buffer_get_int(&msg);
203
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000204 if (quiet)
205 debug("Couldn't stat remote file: %s", fx2txt(status));
206 else
207 error("Couldn't stat remote file: %s", fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100208 return(NULL);
209 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000210 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100211 SSH2_FXP_ATTRS, type);
212 }
213 a = decode_attrib(&msg);
214 buffer_free(&msg);
215
216 return(a);
217}
218
Damien Miller3db5f532002-02-13 14:10:32 +1100219struct sftp_conn *
220do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100221{
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000222 u_int type;
223 int version;
Damien Miller33804262001-02-04 23:20:18 +1100224 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100225 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100226
227 buffer_init(&msg);
228 buffer_put_char(&msg, SSH2_FXP_INIT);
229 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
230 send_msg(fd_out, &msg);
231
232 buffer_clear(&msg);
233
234 get_msg(fd_in, &msg);
235
Kevin Stevesef4eea92001-02-05 12:42:17 +0000236 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100237 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000238 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100239 type);
240 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100241 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100242 }
243 version = buffer_get_int(&msg);
244
245 debug2("Remote version: %d", version);
246
247 /* Check for extensions */
248 while (buffer_len(&msg) > 0) {
249 char *name = buffer_get_string(&msg, NULL);
250 char *value = buffer_get_string(&msg, NULL);
251
252 debug2("Init extension: \"%s\"", name);
253 xfree(name);
254 xfree(value);
255 }
256
257 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100258
Damien Miller3db5f532002-02-13 14:10:32 +1100259 ret = xmalloc(sizeof(*ret));
260 ret->fd_in = fd_in;
261 ret->fd_out = fd_out;
262 ret->transfer_buflen = transfer_buflen;
263 ret->num_requests = num_requests;
264 ret->version = version;
265 ret->msg_id = 1;
266
267 /* Some filexfer v.0 servers don't support large packets */
268 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000269 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100270
271 return(ret);
272}
273
274u_int
275sftp_proto_version(struct sftp_conn *conn)
276{
277 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100278}
279
280int
Damien Miller3db5f532002-02-13 14:10:32 +1100281do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100282{
283 u_int id, status;
284 Buffer msg;
285
286 buffer_init(&msg);
287
Damien Miller3db5f532002-02-13 14:10:32 +1100288 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100289 buffer_put_char(&msg, SSH2_FXP_CLOSE);
290 buffer_put_int(&msg, id);
291 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100292 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000293 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100294
Damien Miller3db5f532002-02-13 14:10:32 +1100295 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100296 if (status != SSH2_FX_OK)
297 error("Couldn't close file: %s", fx2txt(status));
298
299 buffer_free(&msg);
300
301 return(status);
302}
303
Damien Miller4870afd2001-03-14 10:27:09 +1100304
Ben Lindstrombba81212001-06-25 05:01:22 +0000305static int
Damien Miller3db5f532002-02-13 14:10:32 +1100306do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100307 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100308{
309 Buffer msg;
Ben Lindstrom025df4a2001-03-14 15:16:34 +0000310 u_int type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100311 char *handle;
312
Damien Miller3db5f532002-02-13 14:10:32 +1100313 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100314
315 buffer_init(&msg);
316 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
317 buffer_put_int(&msg, id);
318 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100319 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100320
321 buffer_clear(&msg);
322
Damien Miller3db5f532002-02-13 14:10:32 +1100323 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100324 if (handle == NULL)
325 return(-1);
326
Damien Miller4870afd2001-03-14 10:27:09 +1100327 if (dir) {
328 ents = 0;
329 *dir = xmalloc(sizeof(**dir));
330 (*dir)[0] = NULL;
331 }
Damien Miller4870afd2001-03-14 10:27:09 +1100332
Damien Miller9f0f5c62001-12-21 14:45:46 +1100333 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +1100334 int count;
335
Damien Miller3db5f532002-02-13 14:10:32 +1100336 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100337
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000338 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100339
340 buffer_clear(&msg);
341 buffer_put_char(&msg, SSH2_FXP_READDIR);
342 buffer_put_int(&msg, id);
343 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100344 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100345
346 buffer_clear(&msg);
347
Damien Miller3db5f532002-02-13 14:10:32 +1100348 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100349
350 type = buffer_get_char(&msg);
351 id = buffer_get_int(&msg);
352
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000353 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100354
355 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000356 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100357
358 if (type == SSH2_FXP_STATUS) {
359 int status = buffer_get_int(&msg);
360
361 debug3("Received SSH2_FXP_STATUS %d", status);
362
363 if (status == SSH2_FX_EOF) {
364 break;
365 } else {
366 error("Couldn't read directory: %s",
367 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100368 do_close(conn, handle, handle_len);
Damien Miller00111382003-03-10 11:21:17 +1100369 xfree(handle);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000370 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100371 }
372 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000373 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100374 SSH2_FXP_NAME, type);
375
376 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100377 if (count == 0)
378 break;
379 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100380 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100381 char *filename, *longname;
382 Attrib *a;
383
384 filename = buffer_get_string(&msg, NULL);
385 longname = buffer_get_string(&msg, NULL);
386 a = decode_attrib(&msg);
387
Damien Miller4870afd2001-03-14 10:27:09 +1100388 if (printflag)
389 printf("%s\n", longname);
390
391 if (dir) {
Ben Lindstroma3700052001-04-05 23:26:32 +0000392 *dir = xrealloc(*dir, sizeof(**dir) *
Damien Miller4870afd2001-03-14 10:27:09 +1100393 (ents + 2));
394 (*dir)[ents] = xmalloc(sizeof(***dir));
395 (*dir)[ents]->filename = xstrdup(filename);
396 (*dir)[ents]->longname = xstrdup(longname);
397 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
398 (*dir)[++ents] = NULL;
399 }
Damien Miller33804262001-02-04 23:20:18 +1100400
401 xfree(filename);
402 xfree(longname);
403 }
404 }
405
406 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100407 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100408 xfree(handle);
409
410 return(0);
411}
412
413int
Damien Miller3db5f532002-02-13 14:10:32 +1100414do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100415{
Damien Miller3db5f532002-02-13 14:10:32 +1100416 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100417}
418
419void free_sftp_dirents(SFTP_DIRENT **s)
420{
421 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100422
423 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100424 xfree(s[i]->filename);
425 xfree(s[i]->longname);
426 xfree(s[i]);
427 }
428 xfree(s);
429}
430
431int
Damien Miller3db5f532002-02-13 14:10:32 +1100432do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100433{
434 u_int status, id;
435
436 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
437
Damien Miller3db5f532002-02-13 14:10:32 +1100438 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000439 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100440 strlen(path));
441 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100442 if (status != SSH2_FX_OK)
443 error("Couldn't delete file: %s", fx2txt(status));
444 return(status);
445}
446
447int
Damien Miller3db5f532002-02-13 14:10:32 +1100448do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100449{
450 u_int status, id;
451
Damien Miller3db5f532002-02-13 14:10:32 +1100452 id = conn->msg_id++;
453 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100454 strlen(path), a);
455
Damien Miller3db5f532002-02-13 14:10:32 +1100456 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100457 if (status != SSH2_FX_OK)
458 error("Couldn't create directory: %s", fx2txt(status));
459
460 return(status);
461}
462
463int
Damien Miller3db5f532002-02-13 14:10:32 +1100464do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100465{
466 u_int status, id;
467
Damien Miller3db5f532002-02-13 14:10:32 +1100468 id = conn->msg_id++;
469 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
470 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100471
Damien Miller3db5f532002-02-13 14:10:32 +1100472 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100473 if (status != SSH2_FX_OK)
474 error("Couldn't remove directory: %s", fx2txt(status));
475
476 return(status);
477}
478
479Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100480do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100481{
482 u_int id;
483
Damien Miller3db5f532002-02-13 14:10:32 +1100484 id = conn->msg_id++;
485
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000486 send_string_request(conn->fd_out, id,
487 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100488 path, strlen(path));
489
490 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100491}
492
493Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100494do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100495{
496 u_int id;
497
Damien Miller3db5f532002-02-13 14:10:32 +1100498 if (conn->version == 0) {
499 if (quiet)
500 debug("Server version does not support lstat operation");
501 else
Damien Miller996acd22003-04-09 20:59:48 +1000502 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000503 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100504 }
505
506 id = conn->msg_id++;
507 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
508 strlen(path));
509
510 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100511}
512
513Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100514do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100515{
516 u_int id;
517
Damien Miller3db5f532002-02-13 14:10:32 +1100518 id = conn->msg_id++;
519 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
520 handle_len);
521
522 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100523}
524
525int
Damien Miller3db5f532002-02-13 14:10:32 +1100526do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100527{
528 u_int status, id;
529
Damien Miller3db5f532002-02-13 14:10:32 +1100530 id = conn->msg_id++;
531 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100532 strlen(path), a);
533
Damien Miller3db5f532002-02-13 14:10:32 +1100534 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100535 if (status != SSH2_FX_OK)
536 error("Couldn't setstat on \"%s\": %s", path,
537 fx2txt(status));
538
539 return(status);
540}
541
542int
Damien Miller3db5f532002-02-13 14:10:32 +1100543do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100544 Attrib *a)
545{
546 u_int status, id;
547
Damien Miller3db5f532002-02-13 14:10:32 +1100548 id = conn->msg_id++;
549 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100550 handle_len, a);
551
Damien Miller3db5f532002-02-13 14:10:32 +1100552 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100553 if (status != SSH2_FX_OK)
554 error("Couldn't fsetstat: %s", fx2txt(status));
555
556 return(status);
557}
558
559char *
Damien Miller3db5f532002-02-13 14:10:32 +1100560do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100561{
562 Buffer msg;
563 u_int type, expected_id, count, id;
564 char *filename, *longname;
565 Attrib *a;
566
Damien Miller3db5f532002-02-13 14:10:32 +1100567 expected_id = id = conn->msg_id++;
568 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
569 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100570
571 buffer_init(&msg);
572
Damien Miller3db5f532002-02-13 14:10:32 +1100573 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100574 type = buffer_get_char(&msg);
575 id = buffer_get_int(&msg);
576
577 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000578 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100579
580 if (type == SSH2_FXP_STATUS) {
581 u_int status = buffer_get_int(&msg);
582
583 error("Couldn't canonicalise: %s", fx2txt(status));
584 return(NULL);
585 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000586 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100587 SSH2_FXP_NAME, type);
588
589 count = buffer_get_int(&msg);
590 if (count != 1)
591 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
592
593 filename = buffer_get_string(&msg, NULL);
594 longname = buffer_get_string(&msg, NULL);
595 a = decode_attrib(&msg);
596
597 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
598
599 xfree(longname);
600
601 buffer_free(&msg);
602
603 return(filename);
604}
605
606int
Damien Miller3db5f532002-02-13 14:10:32 +1100607do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100608{
609 Buffer msg;
610 u_int status, id;
611
612 buffer_init(&msg);
613
614 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100615 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100616 buffer_put_char(&msg, SSH2_FXP_RENAME);
617 buffer_put_int(&msg, id);
618 buffer_put_cstring(&msg, oldpath);
619 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100620 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100621 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
622 newpath);
623 buffer_free(&msg);
624
Damien Miller3db5f532002-02-13 14:10:32 +1100625 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100626 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100627 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
628 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100629
630 return(status);
631}
632
633int
Damien Miller3db5f532002-02-13 14:10:32 +1100634do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100635{
636 Buffer msg;
637 u_int status, id;
638
Damien Miller3db5f532002-02-13 14:10:32 +1100639 if (conn->version < 3) {
640 error("This server does not support the symlink operation");
641 return(SSH2_FX_OP_UNSUPPORTED);
642 }
643
Damien Miller058316f2001-03-08 10:08:49 +1100644 buffer_init(&msg);
645
646 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100647 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100648 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
649 buffer_put_int(&msg, id);
650 buffer_put_cstring(&msg, oldpath);
651 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100652 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100653 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
654 newpath);
655 buffer_free(&msg);
656
Damien Miller3db5f532002-02-13 14:10:32 +1100657 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100658 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000659 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100660 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100661
662 return(status);
663}
664
665char *
Damien Miller3db5f532002-02-13 14:10:32 +1100666do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100667{
668 Buffer msg;
669 u_int type, expected_id, count, id;
670 char *filename, *longname;
671 Attrib *a;
672
Damien Miller3db5f532002-02-13 14:10:32 +1100673 expected_id = id = conn->msg_id++;
674 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
675 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100676
677 buffer_init(&msg);
678
Damien Miller3db5f532002-02-13 14:10:32 +1100679 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100680 type = buffer_get_char(&msg);
681 id = buffer_get_int(&msg);
682
683 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000684 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100685
686 if (type == SSH2_FXP_STATUS) {
687 u_int status = buffer_get_int(&msg);
688
689 error("Couldn't readlink: %s", fx2txt(status));
690 return(NULL);
691 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000692 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100693 SSH2_FXP_NAME, type);
694
695 count = buffer_get_int(&msg);
696 if (count != 1)
697 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
698
699 filename = buffer_get_string(&msg, NULL);
700 longname = buffer_get_string(&msg, NULL);
701 a = decode_attrib(&msg);
702
703 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
704
705 xfree(longname);
706
707 buffer_free(&msg);
708
709 return(filename);
710}
711
Damien Miller16a13332002-02-13 14:03:56 +1100712static void
713send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
714 char *handle, u_int handle_len)
715{
716 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000717
Damien Miller16a13332002-02-13 14:03:56 +1100718 buffer_init(&msg);
719 buffer_clear(&msg);
720 buffer_put_char(&msg, SSH2_FXP_READ);
721 buffer_put_int(&msg, id);
722 buffer_put_string(&msg, handle, handle_len);
723 buffer_put_int64(&msg, offset);
724 buffer_put_int(&msg, len);
725 send_msg(fd_out, &msg);
726 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000727}
Damien Miller16a13332002-02-13 14:03:56 +1100728
Damien Miller058316f2001-03-08 10:08:49 +1100729int
Damien Miller3db5f532002-02-13 14:10:32 +1100730do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
731 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100732{
Damien Miller33804262001-02-04 23:20:18 +1100733 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100734 Buffer msg;
735 char *handle;
736 int local_fd, status, num_req, max_req, write_error;
737 int read_error, write_errno;
738 u_int64_t offset, size;
Damien Miller3db5f532002-02-13 14:10:32 +1100739 u_int handle_len, mode, type, id, buflen;
Damien Miller62d57f62003-01-10 21:43:24 +1100740 off_t progress_counter;
Damien Miller16a13332002-02-13 14:03:56 +1100741 struct request {
742 u_int id;
743 u_int len;
744 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000745 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100746 };
747 TAILQ_HEAD(reqhead, request) requests;
748 struct request *req;
749
750 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100751
Damien Miller3db5f532002-02-13 14:10:32 +1100752 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100753 if (a == NULL)
754 return(-1);
755
756 /* XXX: should we preserve set[ug]id? */
757 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +1100758 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +1100759 else
760 mode = 0666;
761
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000762 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +1100763 (!S_ISREG(a->perm))) {
764 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000765 return(-1);
766 }
767
Damien Miller16a13332002-02-13 14:03:56 +1100768 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
769 size = a->size;
770 else
771 size = 0;
772
Damien Miller3db5f532002-02-13 14:10:32 +1100773 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100774 buffer_init(&msg);
775
776 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100777 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100778 buffer_put_char(&msg, SSH2_FXP_OPEN);
779 buffer_put_int(&msg, id);
780 buffer_put_cstring(&msg, remote_path);
781 buffer_put_int(&msg, SSH2_FXF_READ);
782 attrib_clear(&junk); /* Send empty attributes */
783 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100784 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000785 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100786
Damien Miller3db5f532002-02-13 14:10:32 +1100787 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100788 if (handle == NULL) {
789 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100790 return(-1);
791 }
792
Damien Millera8e06ce2003-11-21 23:48:55 +1100793 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
Damien Miller770b3742003-01-08 14:04:53 +1100794 mode | S_IWRITE);
Damien Miller3db5f532002-02-13 14:10:32 +1100795 if (local_fd == -1) {
796 error("Couldn't open local file \"%s\" for writing: %s",
797 local_path, strerror(errno));
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000798 buffer_free(&msg);
799 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100800 return(-1);
801 }
802
Damien Miller33804262001-02-04 23:20:18 +1100803 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100804 write_error = read_error = write_errno = num_req = offset = 0;
805 max_req = 1;
Damien Miller62d57f62003-01-10 21:43:24 +1100806 progress_counter = 0;
807
808 if (showprogress) {
809 if (size)
810 start_progress_meter(remote_path, size,
811 &progress_counter);
812 else
813 printf("Fetching %s to %s\n", remote_path, local_path);
814 }
815
Damien Miller16a13332002-02-13 14:03:56 +1100816 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100817 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100818 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100819
Damien Miller16a13332002-02-13 14:03:56 +1100820 /* Send some more requests */
821 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000822 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000823 (unsigned long long)offset,
824 (unsigned long long)offset + buflen - 1,
825 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100826 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100827 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100828 req->len = buflen;
829 req->offset = offset;
830 offset += buflen;
831 num_req++;
832 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000833 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +1100834 req->len, handle, handle_len);
835 }
Damien Miller33804262001-02-04 23:20:18 +1100836
837 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100838 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100839 type = buffer_get_char(&msg);
840 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000841 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +1100842
Damien Miller16a13332002-02-13 14:03:56 +1100843 /* Find the request in our queue */
844 for(req = TAILQ_FIRST(&requests);
845 req != NULL && req->id != id;
846 req = TAILQ_NEXT(req, tq))
847 ;
848 if (req == NULL)
849 fatal("Unexpected reply %u", id);
850
851 switch (type) {
852 case SSH2_FXP_STATUS:
853 status = buffer_get_int(&msg);
854 if (status != SSH2_FX_EOF)
855 read_error = 1;
856 max_req = 0;
857 TAILQ_REMOVE(&requests, req, tq);
858 xfree(req);
859 num_req--;
860 break;
861 case SSH2_FXP_DATA:
862 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +0000863 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000864 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +0000865 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +1100866 if (len > req->len)
867 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +0000868 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +1100869 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +1000870 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +1100871 !write_error) {
872 write_errno = errno;
873 write_error = 1;
874 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +1100875 }
Damien Miller62d57f62003-01-10 21:43:24 +1100876 progress_counter += len;
Damien Miller16a13332002-02-13 14:03:56 +1100877 xfree(data);
878
879 if (len == req->len) {
880 TAILQ_REMOVE(&requests, req, tq);
881 xfree(req);
882 num_req--;
883 } else {
884 /* Resend the request for the missing data */
885 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +0000886 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000887 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +0000888 (unsigned long long)req->offset +
889 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +1100890 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100891 req->len -= len;
892 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000893 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +1100894 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100895 /* Reduce the request size */
896 if (len < buflen)
897 buflen = MAX(MIN_READ_SIZE, len);
898 }
899 if (max_req > 0) { /* max_req = 0 iff EOF received */
900 if (size > 0 && offset > size) {
901 /* Only one request at a time
902 * after the expected EOF */
903 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +0000904 (unsigned long long)offset,
905 num_req);
Damien Miller16a13332002-02-13 14:03:56 +1100906 max_req = 1;
907 }
Damien Miller3db5f532002-02-13 14:10:32 +1100908 else if (max_req < conn->num_requests + 1) {
Damien Miller16a13332002-02-13 14:03:56 +1100909 ++max_req;
910 }
911 }
912 break;
913 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000914 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100915 SSH2_FXP_DATA, type);
916 }
Damien Miller33804262001-02-04 23:20:18 +1100917 }
Damien Miller33804262001-02-04 23:20:18 +1100918
Damien Miller62d57f62003-01-10 21:43:24 +1100919 if (showprogress && size)
920 stop_progress_meter();
921
Damien Miller16a13332002-02-13 14:03:56 +1100922 /* Sanity check */
923 if (TAILQ_FIRST(&requests) != NULL)
924 fatal("Transfer complete, but requests still in queue");
925
926 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000927 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +1100928 remote_path, fx2txt(status));
929 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100930 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +1100931 error("Couldn't write to \"%s\": %s", local_path,
932 strerror(write_errno));
933 status = -1;
934 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100935 } else {
Damien Miller3db5f532002-02-13 14:10:32 +1100936 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100937
938 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000939#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +1100940 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +1100941#else
Damien Miller16a13332002-02-13 14:03:56 +1100942 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000943#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +1100944 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +0000945 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100946 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
947 struct timeval tv[2];
948 tv[0].tv_sec = a->atime;
949 tv[1].tv_sec = a->mtime;
950 tv[0].tv_usec = tv[1].tv_usec = 0;
951 if (utimes(local_path, tv) == -1)
952 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +0000953 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +1100954 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000955 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100956 close(local_fd);
957 buffer_free(&msg);
958 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100959
960 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100961}
962
963int
Damien Miller3db5f532002-02-13 14:10:32 +1100964do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
965 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100966{
Damien Miller8829d362002-02-08 22:04:05 +1100967 int local_fd, status;
Damien Miller5873dfd2002-02-13 14:04:37 +1100968 u_int handle_len, id, type;
Damien Miller33804262001-02-04 23:20:18 +1100969 u_int64_t offset;
Damien Miller8829d362002-02-08 22:04:05 +1100970 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +1100971 Buffer msg;
972 struct stat sb;
973 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +1100974 u_int32_t startid;
975 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +1100976 struct outstanding_ack {
977 u_int id;
978 u_int len;
979 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000980 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +1100981 };
982 TAILQ_HEAD(ackhead, outstanding_ack) acks;
983 struct outstanding_ack *ack;
984
985 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +1100986
987 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
988 error("Couldn't open local file \"%s\" for reading: %s",
989 local_path, strerror(errno));
990 return(-1);
991 }
992 if (fstat(local_fd, &sb) == -1) {
993 error("Couldn't fstat local file \"%s\": %s",
994 local_path, strerror(errno));
995 close(local_fd);
996 return(-1);
997 }
Damien Miller5fa01fd2003-01-14 22:24:47 +1100998 if (!S_ISREG(sb.st_mode)) {
999 error("%s is not a regular file", local_path);
1000 close(local_fd);
1001 return(-1);
1002 }
Damien Miller33804262001-02-04 23:20:18 +11001003 stat_to_attrib(&sb, &a);
1004
1005 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1006 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1007 a.perm &= 0777;
1008 if (!pflag)
1009 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1010
1011 buffer_init(&msg);
1012
1013 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001014 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001015 buffer_put_char(&msg, SSH2_FXP_OPEN);
1016 buffer_put_int(&msg, id);
1017 buffer_put_cstring(&msg, remote_path);
1018 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1019 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001020 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001021 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001022
1023 buffer_clear(&msg);
1024
Damien Miller3db5f532002-02-13 14:10:32 +11001025 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001026 if (handle == NULL) {
1027 close(local_fd);
1028 buffer_free(&msg);
1029 return(-1);
1030 }
1031
Damien Miller16a13332002-02-13 14:03:56 +11001032 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001033 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001034
Damien Miller33804262001-02-04 23:20:18 +11001035 /* Read from local and write to remote */
1036 offset = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001037 if (showprogress)
1038 start_progress_meter(local_path, sb.st_size, &offset);
1039 else
1040 printf("Uploading %s to %s\n", local_path, remote_path);
1041
Damien Miller9f0f5c62001-12-21 14:45:46 +11001042 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001043 int len;
Damien Miller33804262001-02-04 23:20:18 +11001044
1045 /*
1046 * Can't use atomicio here because it returns 0 on EOF, thus losing
1047 * the last block of the file
1048 */
1049 do
Damien Miller3db5f532002-02-13 14:10:32 +11001050 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001051 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1052
1053 if (len == -1)
1054 fatal("Couldn't read from \"%s\": %s", local_path,
1055 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001056
1057 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001058 ack = xmalloc(sizeof(*ack));
1059 ack->id = ++id;
1060 ack->offset = offset;
1061 ack->len = len;
1062 TAILQ_INSERT_TAIL(&acks, ack, tq);
1063
Damien Miller16a13332002-02-13 14:03:56 +11001064 buffer_clear(&msg);
1065 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001066 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001067 buffer_put_string(&msg, handle, handle_len);
1068 buffer_put_int64(&msg, offset);
1069 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001070 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001071 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001072 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001073 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001074 break;
1075
Damien Miller5873dfd2002-02-13 14:04:37 +11001076 if (ack == NULL)
1077 fatal("Unexpected ACK %u", id);
1078
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001079 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001080 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001081 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001082
Damien Miller5873dfd2002-02-13 14:04:37 +11001083 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001084 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001085 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001086 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001087
1088 if (type != SSH2_FXP_STATUS)
1089 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1090 "got %d", SSH2_FXP_STATUS, type);
1091
1092 status = buffer_get_int(&msg);
1093 debug3("SSH2_FXP_STATUS %d", status);
1094
1095 /* Find the request in our queue */
1096 for(ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001097 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001098 ack = TAILQ_NEXT(ack, tq))
1099 ;
1100 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001101 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001102 TAILQ_REMOVE(&acks, ack, tq);
1103
Damien Miller16a13332002-02-13 14:03:56 +11001104 if (status != SSH2_FX_OK) {
1105 error("Couldn't write to remote file \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001106 remote_path, fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +11001107 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001108 close(local_fd);
Damien Miller00111382003-03-10 11:21:17 +11001109 xfree(data);
1110 xfree(ack);
Damien Miller16a13332002-02-13 14:03:56 +11001111 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}