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