]> andersk Git - openssh.git/blame - sftp-client.c
- (djm) OpenBSD CVS Sync
[openssh.git] / sftp-client.c
CommitLineData
61e96248 1/*
22e6c827 2 * Copyright (c) 2001,2002 Damien Miller. All rights reserved.
61e96248 3 *
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 */
22e6c827 27/* XXX: remove all logging, only return status codes */
61e96248 28/* XXX: copy between two remote sites */
29
30#include "includes.h"
bfd934b9 31RCSID("$OpenBSD: sftp-client.c,v 1.38 2003/01/06 23:51:22 djm Exp $");
c25d3df7 32
9fd2a215 33#include "openbsd-compat/sys-queue.h"
61e96248 34
61e96248 35#include "buffer.h"
36#include "bufaux.h"
37#include "getput.h"
38#include "xmalloc.h"
39#include "log.h"
40#include "atomicio.h"
61e96248 41
42#include "sftp.h"
43#include "sftp-common.h"
44#include "sftp-client.h"
45
c25d3df7 46/* Minimum amount of data to read at at time */
47#define MIN_READ_SIZE 512
48
22e6c827 49struct sftp_conn {
50 int fd_in;
51 int fd_out;
52 u_int transfer_buflen;
53 u_int num_requests;
54 u_int version;
55 u_int msg_id;
56};
9c5a8165 57
396c147e 58static void
61e96248 59send_msg(int fd, Buffer *m)
60{
61 int mlen = buffer_len(m);
62 int len;
63 Buffer oqueue;
64
65 buffer_init(&oqueue);
66 buffer_put_int(&oqueue, mlen);
67 buffer_append(&oqueue, buffer_ptr(m), mlen);
68 buffer_consume(m, mlen);
69
70 len = atomicio(write, fd, buffer_ptr(&oqueue), buffer_len(&oqueue));
71 if (len <= 0)
72 fatal("Couldn't send packet: %s", strerror(errno));
73
74 buffer_free(&oqueue);
75}
76
396c147e 77static void
61e96248 78get_msg(int fd, Buffer *m)
79{
80 u_int len, msg_len;
81 unsigned char buf[4096];
82
83 len = atomicio(read, fd, buf, 4);
bb6da70f 84 if (len == 0)
85 fatal("Connection closed");
86 else if (len == -1)
61e96248 87 fatal("Couldn't read packet: %s", strerror(errno));
88
89 msg_len = GET_32BIT(buf);
90 if (msg_len > 256 * 1024)
9906a836 91 fatal("Received message too long %u", msg_len);
61e96248 92
93 while (msg_len) {
94 len = atomicio(read, fd, buf, MIN(msg_len, sizeof(buf)));
bb6da70f 95 if (len == 0)
96 fatal("Connection closed");
97 else if (len == -1)
61e96248 98 fatal("Couldn't read packet: %s", strerror(errno));
99
100 msg_len -= len;
101 buffer_append(m, buf, len);
102 }
103}
104
396c147e 105static void
61e96248 106send_string_request(int fd, u_int id, u_int code, char *s,
107 u_int len)
108{
109 Buffer msg;
110
111 buffer_init(&msg);
112 buffer_put_char(&msg, code);
113 buffer_put_int(&msg, id);
114 buffer_put_string(&msg, s, len);
115 send_msg(fd, &msg);
9906a836 116 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
61e96248 117 buffer_free(&msg);
118}
119
396c147e 120static void
61e96248 121send_string_attrs_request(int fd, u_int id, u_int code, char *s,
122 u_int len, Attrib *a)
123{
124 Buffer msg;
125
126 buffer_init(&msg);
127 buffer_put_char(&msg, code);
128 buffer_put_int(&msg, id);
129 buffer_put_string(&msg, s, len);
130 encode_attrib(&msg, a);
131 send_msg(fd, &msg);
9906a836 132 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
61e96248 133 buffer_free(&msg);
134}
135
396c147e 136static u_int
9906a836 137get_status(int fd, u_int expected_id)
61e96248 138{
139 Buffer msg;
140 u_int type, id, status;
141
142 buffer_init(&msg);
143 get_msg(fd, &msg);
144 type = buffer_get_char(&msg);
145 id = buffer_get_int(&msg);
146
147 if (id != expected_id)
9906a836 148 fatal("ID mismatch (%u != %u)", id, expected_id);
61e96248 149 if (type != SSH2_FXP_STATUS)
9906a836 150 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
61e96248 151 SSH2_FXP_STATUS, type);
152
153 status = buffer_get_int(&msg);
154 buffer_free(&msg);
155
9906a836 156 debug3("SSH2_FXP_STATUS %u", status);
61e96248 157
158 return(status);
159}
160
396c147e 161static char *
61e96248 162get_handle(int fd, u_int expected_id, u_int *len)
163{
164 Buffer msg;
165 u_int type, id;
166 char *handle;
167
168 buffer_init(&msg);
169 get_msg(fd, &msg);
170 type = buffer_get_char(&msg);
171 id = buffer_get_int(&msg);
172
173 if (id != expected_id)
9906a836 174 fatal("ID mismatch (%u != %u)", id, expected_id);
61e96248 175 if (type == SSH2_FXP_STATUS) {
176 int status = buffer_get_int(&msg);
177
178 error("Couldn't get handle: %s", fx2txt(status));
179 return(NULL);
180 } else if (type != SSH2_FXP_HANDLE)
9906a836 181 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
61e96248 182 SSH2_FXP_HANDLE, type);
183
184 handle = buffer_get_string(&msg, len);
185 buffer_free(&msg);
186
187 return(handle);
188}
189
396c147e 190static Attrib *
d50d9b63 191get_decode_stat(int fd, u_int expected_id, int quiet)
61e96248 192{
193 Buffer msg;
194 u_int type, id;
195 Attrib *a;
196
197 buffer_init(&msg);
198 get_msg(fd, &msg);
199
200 type = buffer_get_char(&msg);
201 id = buffer_get_int(&msg);
202
9906a836 203 debug3("Received stat reply T:%u I:%u", type, id);
61e96248 204 if (id != expected_id)
9906a836 205 fatal("ID mismatch (%u != %u)", id, expected_id);
61e96248 206 if (type == SSH2_FXP_STATUS) {
207 int status = buffer_get_int(&msg);
208
d50d9b63 209 if (quiet)
210 debug("Couldn't stat remote file: %s", fx2txt(status));
211 else
212 error("Couldn't stat remote file: %s", fx2txt(status));
61e96248 213 return(NULL);
214 } else if (type != SSH2_FXP_ATTRS) {
9906a836 215 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
61e96248 216 SSH2_FXP_ATTRS, type);
217 }
218 a = decode_attrib(&msg);
219 buffer_free(&msg);
220
221 return(a);
222}
223
22e6c827 224struct sftp_conn *
225do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
61e96248 226{
9906a836 227 u_int type;
228 int version;
61e96248 229 Buffer msg;
22e6c827 230 struct sftp_conn *ret;
61e96248 231
232 buffer_init(&msg);
233 buffer_put_char(&msg, SSH2_FXP_INIT);
234 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
235 send_msg(fd_out, &msg);
236
237 buffer_clear(&msg);
238
239 get_msg(fd_in, &msg);
240
2b87da3b 241 /* Expecting a VERSION reply */
61e96248 242 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
9906a836 243 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
61e96248 244 type);
245 buffer_free(&msg);
22e6c827 246 return(NULL);
61e96248 247 }
248 version = buffer_get_int(&msg);
249
250 debug2("Remote version: %d", version);
251
252 /* Check for extensions */
253 while (buffer_len(&msg) > 0) {
254 char *name = buffer_get_string(&msg, NULL);
255 char *value = buffer_get_string(&msg, NULL);
256
257 debug2("Init extension: \"%s\"", name);
258 xfree(name);
259 xfree(value);
260 }
261
262 buffer_free(&msg);
3a7fe5ba 263
22e6c827 264 ret = xmalloc(sizeof(*ret));
265 ret->fd_in = fd_in;
266 ret->fd_out = fd_out;
267 ret->transfer_buflen = transfer_buflen;
268 ret->num_requests = num_requests;
269 ret->version = version;
270 ret->msg_id = 1;
271
272 /* Some filexfer v.0 servers don't support large packets */
273 if (version == 0)
92053302 274 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
22e6c827 275
276 return(ret);
277}
278
279u_int
280sftp_proto_version(struct sftp_conn *conn)
281{
282 return(conn->version);
61e96248 283}
284
285int
22e6c827 286do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
61e96248 287{
288 u_int id, status;
289 Buffer msg;
290
291 buffer_init(&msg);
292
22e6c827 293 id = conn->msg_id++;
61e96248 294 buffer_put_char(&msg, SSH2_FXP_CLOSE);
295 buffer_put_int(&msg, id);
296 buffer_put_string(&msg, handle, handle_len);
22e6c827 297 send_msg(conn->fd_out, &msg);
9906a836 298 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
61e96248 299
22e6c827 300 status = get_status(conn->fd_in, id);
61e96248 301 if (status != SSH2_FX_OK)
302 error("Couldn't close file: %s", fx2txt(status));
303
304 buffer_free(&msg);
305
306 return(status);
307}
308
2e4fb373 309
396c147e 310static int
22e6c827 311do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
2e4fb373 312 SFTP_DIRENT ***dir)
61e96248 313{
314 Buffer msg;
4cb5d598 315 u_int type, id, handle_len, i, expected_id, ents = 0;
61e96248 316 char *handle;
317
22e6c827 318 id = conn->msg_id++;
61e96248 319
320 buffer_init(&msg);
321 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
322 buffer_put_int(&msg, id);
323 buffer_put_cstring(&msg, path);
22e6c827 324 send_msg(conn->fd_out, &msg);
61e96248 325
326 buffer_clear(&msg);
327
22e6c827 328 handle = get_handle(conn->fd_in, id, &handle_len);
61e96248 329 if (handle == NULL)
330 return(-1);
331
2e4fb373 332 if (dir) {
333 ents = 0;
334 *dir = xmalloc(sizeof(**dir));
335 (*dir)[0] = NULL;
336 }
2e4fb373 337
184eed6a 338 for (;;) {
61e96248 339 int count;
340
22e6c827 341 id = expected_id = conn->msg_id++;
61e96248 342
9906a836 343 debug3("Sending SSH2_FXP_READDIR I:%u", id);
61e96248 344
345 buffer_clear(&msg);
346 buffer_put_char(&msg, SSH2_FXP_READDIR);
347 buffer_put_int(&msg, id);
348 buffer_put_string(&msg, handle, handle_len);
22e6c827 349 send_msg(conn->fd_out, &msg);
61e96248 350
351 buffer_clear(&msg);
352
22e6c827 353 get_msg(conn->fd_in, &msg);
61e96248 354
355 type = buffer_get_char(&msg);
356 id = buffer_get_int(&msg);
357
9906a836 358 debug3("Received reply T:%u I:%u", type, id);
61e96248 359
360 if (id != expected_id)
9906a836 361 fatal("ID mismatch (%u != %u)", id, expected_id);
61e96248 362
363 if (type == SSH2_FXP_STATUS) {
364 int status = buffer_get_int(&msg);
365
366 debug3("Received SSH2_FXP_STATUS %d", status);
367
368 if (status == SSH2_FX_EOF) {
369 break;
370 } else {
371 error("Couldn't read directory: %s",
372 fx2txt(status));
22e6c827 373 do_close(conn, handle, handle_len);
b655a207 374 return(status);
61e96248 375 }
376 } else if (type != SSH2_FXP_NAME)
9906a836 377 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
61e96248 378 SSH2_FXP_NAME, type);
379
380 count = buffer_get_int(&msg);
0426a3b4 381 if (count == 0)
382 break;
383 debug3("Received %d SSH2_FXP_NAME responses", count);
184eed6a 384 for (i = 0; i < count; i++) {
61e96248 385 char *filename, *longname;
386 Attrib *a;
387
388 filename = buffer_get_string(&msg, NULL);
389 longname = buffer_get_string(&msg, NULL);
390 a = decode_attrib(&msg);
391
2e4fb373 392 if (printflag)
393 printf("%s\n", longname);
394
395 if (dir) {
cd332296 396 *dir = xrealloc(*dir, sizeof(**dir) *
2e4fb373 397 (ents + 2));
398 (*dir)[ents] = xmalloc(sizeof(***dir));
399 (*dir)[ents]->filename = xstrdup(filename);
400 (*dir)[ents]->longname = xstrdup(longname);
401 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
402 (*dir)[++ents] = NULL;
403 }
61e96248 404
405 xfree(filename);
406 xfree(longname);
407 }
408 }
409
410 buffer_free(&msg);
22e6c827 411 do_close(conn, handle, handle_len);
61e96248 412 xfree(handle);
413
414 return(0);
415}
416
2e4fb373 417int
22e6c827 418do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
2e4fb373 419{
22e6c827 420 return(do_lsreaddir(conn, path, 0, dir));
2e4fb373 421}
422
423void free_sftp_dirents(SFTP_DIRENT **s)
424{
425 int i;
184eed6a 426
427 for (i = 0; s[i]; i++) {
2e4fb373 428 xfree(s[i]->filename);
429 xfree(s[i]->longname);
430 xfree(s[i]);
431 }
432 xfree(s);
433}
434
61e96248 435int
22e6c827 436do_rm(struct sftp_conn *conn, char *path)
61e96248 437{
438 u_int status, id;
439
440 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
441
22e6c827 442 id = conn->msg_id++;
762715ce 443 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
22e6c827 444 strlen(path));
445 status = get_status(conn->fd_in, id);
61e96248 446 if (status != SSH2_FX_OK)
447 error("Couldn't delete file: %s", fx2txt(status));
448 return(status);
449}
450
451int
22e6c827 452do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
61e96248 453{
454 u_int status, id;
455
22e6c827 456 id = conn->msg_id++;
457 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
61e96248 458 strlen(path), a);
459
22e6c827 460 status = get_status(conn->fd_in, id);
61e96248 461 if (status != SSH2_FX_OK)
462 error("Couldn't create directory: %s", fx2txt(status));
463
464 return(status);
465}
466
467int
22e6c827 468do_rmdir(struct sftp_conn *conn, char *path)
61e96248 469{
470 u_int status, id;
471
22e6c827 472 id = conn->msg_id++;
473 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
474 strlen(path));
61e96248 475
22e6c827 476 status = get_status(conn->fd_in, id);
61e96248 477 if (status != SSH2_FX_OK)
478 error("Couldn't remove directory: %s", fx2txt(status));
479
480 return(status);
481}
482
483Attrib *
22e6c827 484do_stat(struct sftp_conn *conn, char *path, int quiet)
61e96248 485{
486 u_int id;
487
22e6c827 488 id = conn->msg_id++;
489
762715ce 490 send_string_request(conn->fd_out, id,
491 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
22e6c827 492 path, strlen(path));
493
494 return(get_decode_stat(conn->fd_in, id, quiet));
61e96248 495}
496
497Attrib *
22e6c827 498do_lstat(struct sftp_conn *conn, char *path, int quiet)
61e96248 499{
500 u_int id;
501
22e6c827 502 if (conn->version == 0) {
503 if (quiet)
504 debug("Server version does not support lstat operation");
505 else
9c74a24d 506 log("Server version does not support lstat operation");
507 return(do_stat(conn, path, quiet));
22e6c827 508 }
509
510 id = conn->msg_id++;
511 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
512 strlen(path));
513
514 return(get_decode_stat(conn->fd_in, id, quiet));
61e96248 515}
516
517Attrib *
22e6c827 518do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
61e96248 519{
520 u_int id;
521
22e6c827 522 id = conn->msg_id++;
523 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
524 handle_len);
525
526 return(get_decode_stat(conn->fd_in, id, quiet));
61e96248 527}
528
529int
22e6c827 530do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
61e96248 531{
532 u_int status, id;
533
22e6c827 534 id = conn->msg_id++;
535 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
61e96248 536 strlen(path), a);
537
22e6c827 538 status = get_status(conn->fd_in, id);
61e96248 539 if (status != SSH2_FX_OK)
540 error("Couldn't setstat on \"%s\": %s", path,
541 fx2txt(status));
542
543 return(status);
544}
545
546int
22e6c827 547do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
61e96248 548 Attrib *a)
549{
550 u_int status, id;
551
22e6c827 552 id = conn->msg_id++;
553 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
61e96248 554 handle_len, a);
555
22e6c827 556 status = get_status(conn->fd_in, id);
61e96248 557 if (status != SSH2_FX_OK)
558 error("Couldn't fsetstat: %s", fx2txt(status));
559
560 return(status);
561}
562
563char *
22e6c827 564do_realpath(struct sftp_conn *conn, char *path)
61e96248 565{
566 Buffer msg;
567 u_int type, expected_id, count, id;
568 char *filename, *longname;
569 Attrib *a;
570
22e6c827 571 expected_id = id = conn->msg_id++;
572 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
573 strlen(path));
61e96248 574
575 buffer_init(&msg);
576
22e6c827 577 get_msg(conn->fd_in, &msg);
61e96248 578 type = buffer_get_char(&msg);
579 id = buffer_get_int(&msg);
580
581 if (id != expected_id)
9906a836 582 fatal("ID mismatch (%u != %u)", id, expected_id);
61e96248 583
584 if (type == SSH2_FXP_STATUS) {
585 u_int status = buffer_get_int(&msg);
586
587 error("Couldn't canonicalise: %s", fx2txt(status));
588 return(NULL);
589 } else if (type != SSH2_FXP_NAME)
9906a836 590 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
61e96248 591 SSH2_FXP_NAME, type);
592
593 count = buffer_get_int(&msg);
594 if (count != 1)
595 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
596
597 filename = buffer_get_string(&msg, NULL);
598 longname = buffer_get_string(&msg, NULL);
599 a = decode_attrib(&msg);
600
601 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
602
603 xfree(longname);
604
605 buffer_free(&msg);
606
607 return(filename);
608}
609
610int
22e6c827 611do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
61e96248 612{
613 Buffer msg;
614 u_int status, id;
615
616 buffer_init(&msg);
617
618 /* Send rename request */
22e6c827 619 id = conn->msg_id++;
61e96248 620 buffer_put_char(&msg, SSH2_FXP_RENAME);
621 buffer_put_int(&msg, id);
622 buffer_put_cstring(&msg, oldpath);
623 buffer_put_cstring(&msg, newpath);
22e6c827 624 send_msg(conn->fd_out, &msg);
61e96248 625 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
626 newpath);
627 buffer_free(&msg);
628
22e6c827 629 status = get_status(conn->fd_in, id);
61e96248 630 if (status != SSH2_FX_OK)
22e6c827 631 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
632 newpath, fx2txt(status));
61e96248 633
634 return(status);
635}
636
3a7fe5ba 637int
22e6c827 638do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
3a7fe5ba 639{
640 Buffer msg;
641 u_int status, id;
642
22e6c827 643 if (conn->version < 3) {
644 error("This server does not support the symlink operation");
645 return(SSH2_FX_OP_UNSUPPORTED);
646 }
647
3a7fe5ba 648 buffer_init(&msg);
649
650 /* Send rename request */
22e6c827 651 id = conn->msg_id++;
3a7fe5ba 652 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
653 buffer_put_int(&msg, id);
654 buffer_put_cstring(&msg, oldpath);
655 buffer_put_cstring(&msg, newpath);
22e6c827 656 send_msg(conn->fd_out, &msg);
3a7fe5ba 657 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
658 newpath);
659 buffer_free(&msg);
660
22e6c827 661 status = get_status(conn->fd_in, id);
3a7fe5ba 662 if (status != SSH2_FX_OK)
9db1a8e9 663 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
22e6c827 664 newpath, fx2txt(status));
3a7fe5ba 665
666 return(status);
667}
668
669char *
22e6c827 670do_readlink(struct sftp_conn *conn, char *path)
3a7fe5ba 671{
672 Buffer msg;
673 u_int type, expected_id, count, id;
674 char *filename, *longname;
675 Attrib *a;
676
22e6c827 677 expected_id = id = conn->msg_id++;
678 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
679 strlen(path));
3a7fe5ba 680
681 buffer_init(&msg);
682
22e6c827 683 get_msg(conn->fd_in, &msg);
3a7fe5ba 684 type = buffer_get_char(&msg);
685 id = buffer_get_int(&msg);
686
687 if (id != expected_id)
9906a836 688 fatal("ID mismatch (%u != %u)", id, expected_id);
3a7fe5ba 689
690 if (type == SSH2_FXP_STATUS) {
691 u_int status = buffer_get_int(&msg);
692
693 error("Couldn't readlink: %s", fx2txt(status));
694 return(NULL);
695 } else if (type != SSH2_FXP_NAME)
9906a836 696 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
3a7fe5ba 697 SSH2_FXP_NAME, type);
698
699 count = buffer_get_int(&msg);
700 if (count != 1)
701 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
702
703 filename = buffer_get_string(&msg, NULL);
704 longname = buffer_get_string(&msg, NULL);
705 a = decode_attrib(&msg);
706
707 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
708
709 xfree(longname);
710
711 buffer_free(&msg);
712
713 return(filename);
714}
715
c25d3df7 716static void
717send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
718 char *handle, u_int handle_len)
719{
720 Buffer msg;
762715ce 721
c25d3df7 722 buffer_init(&msg);
723 buffer_clear(&msg);
724 buffer_put_char(&msg, SSH2_FXP_READ);
725 buffer_put_int(&msg, id);
726 buffer_put_string(&msg, handle, handle_len);
727 buffer_put_int64(&msg, offset);
728 buffer_put_int(&msg, len);
729 send_msg(fd_out, &msg);
730 buffer_free(&msg);
762715ce 731}
c25d3df7 732
61e96248 733int
22e6c827 734do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
735 int pflag)
61e96248 736{
61e96248 737 Attrib junk, *a;
c25d3df7 738 Buffer msg;
739 char *handle;
740 int local_fd, status, num_req, max_req, write_error;
741 int read_error, write_errno;
742 u_int64_t offset, size;
22e6c827 743 u_int handle_len, mode, type, id, buflen;
c25d3df7 744 struct request {
745 u_int id;
746 u_int len;
747 u_int64_t offset;
762715ce 748 TAILQ_ENTRY(request) tq;
c25d3df7 749 };
750 TAILQ_HEAD(reqhead, request) requests;
751 struct request *req;
752
753 TAILQ_INIT(&requests);
61e96248 754
22e6c827 755 a = do_stat(conn, remote_path, 0);
61e96248 756 if (a == NULL)
757 return(-1);
758
759 /* XXX: should we preserve set[ug]id? */
760 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
bfd934b9 761 mode = a->perm & 0777;
61e96248 762 else
763 mode = 0666;
764
d50d9b63 765 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
766 (a->perm & S_IFDIR)) {
767 error("Cannot download a directory: %s", remote_path);
768 return(-1);
769 }
770
c25d3df7 771 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
772 size = a->size;
773 else
774 size = 0;
775
22e6c827 776 buflen = conn->transfer_buflen;
61e96248 777 buffer_init(&msg);
778
779 /* Send open request */
22e6c827 780 id = conn->msg_id++;
61e96248 781 buffer_put_char(&msg, SSH2_FXP_OPEN);
782 buffer_put_int(&msg, id);
783 buffer_put_cstring(&msg, remote_path);
784 buffer_put_int(&msg, SSH2_FXF_READ);
785 attrib_clear(&junk); /* Send empty attributes */
786 encode_attrib(&msg, &junk);
22e6c827 787 send_msg(conn->fd_out, &msg);
9906a836 788 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
61e96248 789
22e6c827 790 handle = get_handle(conn->fd_in, id, &handle_len);
61e96248 791 if (handle == NULL) {
792 buffer_free(&msg);
61e96248 793 return(-1);
794 }
795
bfd934b9 796 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
797 mode | S_IWRITE);
22e6c827 798 if (local_fd == -1) {
799 error("Couldn't open local file \"%s\" for writing: %s",
800 local_path, strerror(errno));
f60ace9f 801 buffer_free(&msg);
802 xfree(handle);
22e6c827 803 return(-1);
804 }
805
61e96248 806 /* Read from remote and write to local */
c25d3df7 807 write_error = read_error = write_errno = num_req = offset = 0;
808 max_req = 1;
809 while (num_req > 0 || max_req > 0) {
61e96248 810 char *data;
c25d3df7 811 u_int len;
61e96248 812
c25d3df7 813 /* Send some more requests */
814 while (num_req < max_req) {
762715ce 815 debug3("Request range %llu -> %llu (%d/%d)",
8627f3e0 816 (unsigned long long)offset,
817 (unsigned long long)offset + buflen - 1,
818 num_req, max_req);
c25d3df7 819 req = xmalloc(sizeof(*req));
22e6c827 820 req->id = conn->msg_id++;
c25d3df7 821 req->len = buflen;
822 req->offset = offset;
823 offset += buflen;
824 num_req++;
825 TAILQ_INSERT_TAIL(&requests, req, tq);
762715ce 826 send_read_request(conn->fd_out, req->id, req->offset,
c25d3df7 827 req->len, handle, handle_len);
828 }
61e96248 829
830 buffer_clear(&msg);
22e6c827 831 get_msg(conn->fd_in, &msg);
61e96248 832 type = buffer_get_char(&msg);
833 id = buffer_get_int(&msg);
9906a836 834 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
c25d3df7 835
836 /* Find the request in our queue */
837 for(req = TAILQ_FIRST(&requests);
838 req != NULL && req->id != id;
839 req = TAILQ_NEXT(req, tq))
840 ;
841 if (req == NULL)
842 fatal("Unexpected reply %u", id);
843
844 switch (type) {
845 case SSH2_FXP_STATUS:
0426a3b4 846 status = buffer_get_int(&msg);
c25d3df7 847 if (status != SSH2_FX_EOF)
848 read_error = 1;
849 max_req = 0;
850 TAILQ_REMOVE(&requests, req, tq);
851 xfree(req);
852 num_req--;
853 break;
854 case SSH2_FXP_DATA:
855 data = buffer_get_string(&msg, &len);
bfa7f960 856 debug3("Received data %llu -> %llu",
762715ce 857 (unsigned long long)req->offset,
bfa7f960 858 (unsigned long long)req->offset + len - 1);
c25d3df7 859 if (len > req->len)
860 fatal("Received more data than asked for "
b77a87e5 861 "%u > %u", len, req->len);
c25d3df7 862 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
b77a87e5 863 atomicio(write, local_fd, data, len) != len) &&
c25d3df7 864 !write_error) {
865 write_errno = errno;
866 write_error = 1;
867 max_req = 0;
868 }
869 xfree(data);
61e96248 870
c25d3df7 871 if (len == req->len) {
872 TAILQ_REMOVE(&requests, req, tq);
873 xfree(req);
874 num_req--;
875 } else {
876 /* Resend the request for the missing data */
877 debug3("Short data block, re-requesting "
bfa7f960 878 "%llu -> %llu (%2d)",
762715ce 879 (unsigned long long)req->offset + len,
5fc7dbc9 880 (unsigned long long)req->offset +
881 req->len - 1, num_req);
22e6c827 882 req->id = conn->msg_id++;
c25d3df7 883 req->len -= len;
884 req->offset += len;
762715ce 885 send_read_request(conn->fd_out, req->id,
22e6c827 886 req->offset, req->len, handle, handle_len);
c25d3df7 887 /* Reduce the request size */
888 if (len < buflen)
889 buflen = MAX(MIN_READ_SIZE, len);
890 }
891 if (max_req > 0) { /* max_req = 0 iff EOF received */
892 if (size > 0 && offset > size) {
893 /* Only one request at a time
894 * after the expected EOF */
895 debug3("Finish at %llu (%2d)",
bfa7f960 896 (unsigned long long)offset,
897 num_req);
c25d3df7 898 max_req = 1;
899 }
22e6c827 900 else if (max_req < conn->num_requests + 1) {
c25d3df7 901 ++max_req;
902 }
61e96248 903 }
c25d3df7 904 break;
905 default:
9906a836 906 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
61e96248 907 SSH2_FXP_DATA, type);
908 }
61e96248 909 }
61e96248 910
c25d3df7 911 /* Sanity check */
912 if (TAILQ_FIRST(&requests) != NULL)
913 fatal("Transfer complete, but requests still in queue");
914
915 if (read_error) {
762715ce 916 error("Couldn't read from remote file \"%s\" : %s",
22e6c827 917 remote_path, fx2txt(status));
918 do_close(conn, handle, handle_len);
c25d3df7 919 } else if (write_error) {
22e6c827 920 error("Couldn't write to \"%s\": %s", local_path,
921 strerror(write_errno));
922 status = -1;
923 do_close(conn, handle, handle_len);
c25d3df7 924 } else {
22e6c827 925 status = do_close(conn, handle, handle_len);
c25d3df7 926
927 /* Override umask and utimes if asked */
663fd560 928#ifdef HAVE_FCHMOD
c25d3df7 929 if (pflag && fchmod(local_fd, mode) == -1)
663fd560 930#else
c25d3df7 931 if (pflag && chmod(local_path, mode) == -1)
663fd560 932#endif /* HAVE_FCHMOD */
c25d3df7 933 error("Couldn't set mode on \"%s\": %s", local_path,
b77a87e5 934 strerror(errno));
c25d3df7 935 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
936 struct timeval tv[2];
937 tv[0].tv_sec = a->atime;
938 tv[1].tv_sec = a->mtime;
939 tv[0].tv_usec = tv[1].tv_usec = 0;
940 if (utimes(local_path, tv) == -1)
941 error("Can't set times on \"%s\": %s",
b77a87e5 942 local_path, strerror(errno));
c25d3df7 943 }
58c54a79 944 }
0426a3b4 945 close(local_fd);
946 buffer_free(&msg);
947 xfree(handle);
22e6c827 948
949 return(status);
61e96248 950}
951
952int
22e6c827 953do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
954 int pflag)
61e96248 955{
375f867e 956 int local_fd, status;
b2bab059 957 u_int handle_len, id, type;
61e96248 958 u_int64_t offset;
375f867e 959 char *handle, *data;
61e96248 960 Buffer msg;
961 struct stat sb;
962 Attrib a;
c25d3df7 963 u_int32_t startid;
964 u_int32_t ackid;
b2bab059 965 struct outstanding_ack {
966 u_int id;
967 u_int len;
968 u_int64_t offset;
762715ce 969 TAILQ_ENTRY(outstanding_ack) tq;
b2bab059 970 };
971 TAILQ_HEAD(ackhead, outstanding_ack) acks;
972 struct outstanding_ack *ack;
973
974 TAILQ_INIT(&acks);
61e96248 975
976 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
977 error("Couldn't open local file \"%s\" for reading: %s",
978 local_path, strerror(errno));
979 return(-1);
980 }
981 if (fstat(local_fd, &sb) == -1) {
982 error("Couldn't fstat local file \"%s\": %s",
983 local_path, strerror(errno));
984 close(local_fd);
985 return(-1);
986 }
987 stat_to_attrib(&sb, &a);
988
989 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
990 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
991 a.perm &= 0777;
992 if (!pflag)
993 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
994
995 buffer_init(&msg);
996
997 /* Send open request */
22e6c827 998 id = conn->msg_id++;
61e96248 999 buffer_put_char(&msg, SSH2_FXP_OPEN);
1000 buffer_put_int(&msg, id);
1001 buffer_put_cstring(&msg, remote_path);
1002 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1003 encode_attrib(&msg, &a);
22e6c827 1004 send_msg(conn->fd_out, &msg);
9906a836 1005 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
61e96248 1006
1007 buffer_clear(&msg);
1008
22e6c827 1009 handle = get_handle(conn->fd_in, id, &handle_len);
61e96248 1010 if (handle == NULL) {
1011 close(local_fd);
1012 buffer_free(&msg);
1013 return(-1);
1014 }
1015
c25d3df7 1016 startid = ackid = id + 1;
22e6c827 1017 data = xmalloc(conn->transfer_buflen);
375f867e 1018
61e96248 1019 /* Read from local and write to remote */
1020 offset = 0;
184eed6a 1021 for (;;) {
61e96248 1022 int len;
61e96248 1023
1024 /*
1025 * Can't use atomicio here because it returns 0 on EOF, thus losing
1026 * the last block of the file
1027 */
1028 do
22e6c827 1029 len = read(local_fd, data, conn->transfer_buflen);
61e96248 1030 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1031
1032 if (len == -1)
1033 fatal("Couldn't read from \"%s\": %s", local_path,
1034 strerror(errno));
c25d3df7 1035
1036 if (len != 0) {
b2bab059 1037 ack = xmalloc(sizeof(*ack));
1038 ack->id = ++id;
1039 ack->offset = offset;
1040 ack->len = len;
1041 TAILQ_INSERT_TAIL(&acks, ack, tq);
1042
c25d3df7 1043 buffer_clear(&msg);
1044 buffer_put_char(&msg, SSH2_FXP_WRITE);
b2bab059 1045 buffer_put_int(&msg, ack->id);
c25d3df7 1046 buffer_put_string(&msg, handle, handle_len);
1047 buffer_put_int64(&msg, offset);
1048 buffer_put_string(&msg, data, len);
22e6c827 1049 send_msg(conn->fd_out, &msg);
9906a836 1050 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
b77a87e5 1051 id, (unsigned long long)offset, len);
b2bab059 1052 } else if (TAILQ_FIRST(&acks) == NULL)
61e96248 1053 break;
1054
b2bab059 1055 if (ack == NULL)
1056 fatal("Unexpected ACK %u", id);
1057
762715ce 1058 if (id == startid || len == 0 ||
22e6c827 1059 id - ackid >= conn->num_requests) {
875ec275 1060 u_int r_id;
af2b3cd9 1061
b2bab059 1062 buffer_clear(&msg);
22e6c827 1063 get_msg(conn->fd_in, &msg);
b2bab059 1064 type = buffer_get_char(&msg);
af2b3cd9 1065 r_id = buffer_get_int(&msg);
b2bab059 1066
1067 if (type != SSH2_FXP_STATUS)
1068 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1069 "got %d", SSH2_FXP_STATUS, type);
1070
1071 status = buffer_get_int(&msg);
1072 debug3("SSH2_FXP_STATUS %d", status);
1073
1074 /* Find the request in our queue */
1075 for(ack = TAILQ_FIRST(&acks);
af2b3cd9 1076 ack != NULL && ack->id != r_id;
b2bab059 1077 ack = TAILQ_NEXT(ack, tq))
1078 ;
1079 if (ack == NULL)
9906a836 1080 fatal("Can't find request for ID %u", r_id);
b2bab059 1081 TAILQ_REMOVE(&acks, ack, tq);
1082
c25d3df7 1083 if (status != SSH2_FX_OK) {
1084 error("Couldn't write to remote file \"%s\": %s",
b77a87e5 1085 remote_path, fx2txt(status));
22e6c827 1086 do_close(conn, handle, handle_len);
c25d3df7 1087 close(local_fd);
1088 goto done;
1089 }
9906a836 1090 debug3("In write loop, ack for %u %u bytes at %llu",
bfa7f960 1091 ack->id, ack->len, (unsigned long long)ack->offset);
c25d3df7 1092 ++ackid;
30e37ee6 1093 xfree(ack);
61e96248 1094 }
61e96248 1095 offset += len;
1096 }
375f867e 1097 xfree(data);
61e96248 1098
1099 if (close(local_fd) == -1) {
1100 error("Couldn't close local file \"%s\": %s", local_path,
1101 strerror(errno));
22e6c827 1102 do_close(conn, handle, handle_len);
0426a3b4 1103 status = -1;
1104 goto done;
61e96248 1105 }
1106
58c54a79 1107 /* Override umask and utimes if asked */
1108 if (pflag)
22e6c827 1109 do_fsetstat(conn, handle, handle_len, &a);
58c54a79 1110
22e6c827 1111 status = do_close(conn, handle, handle_len);
0426a3b4 1112
1113done:
1114 xfree(handle);
1115 buffer_free(&msg);
22e6c827 1116 return(status);
61e96248 1117}
This page took 0.403801 seconds and 5 git commands to generate.