1 /* $OpenBSD: sftp-client.c,v 1.165 2022/09/19 10:43:12 djm Exp $ */
3 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
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.
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.
19 /* XXX: signed vs unsigned */
20 /* XXX: remove all logging, only return status codes */
21 /* XXX: copy between two remote sites */
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_STATVFS_H
27 #include <sys/statvfs.h>
29 #include "openbsd-compat/sys-queue.h"
30 #ifdef HAVE_SYS_STAT_H
31 # include <sys/stat.h>
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
43 # ifdef HAVE_SYS_POLL_H
44 # include <sys/poll.h>
60 #include "progressmeter.h"
65 #include "sftp-common.h"
66 #include "sftp-client.h"
68 extern volatile sig_atomic_t interrupted
;
69 extern int showprogress
;
71 /* Default size of buffer for up/download */
72 #define DEFAULT_COPY_BUFLEN 32768
74 /* Default number of concurrent outstanding requests */
75 #define DEFAULT_NUM_REQUESTS 64
77 /* Minimum amount of data to read at a time */
78 #define MIN_READ_SIZE 512
80 /* Maximum depth to descend in directory trees */
81 #define MAX_DIR_DEPTH 64
83 /* Directory separator characters */
85 # define SFTP_DIRECTORY_CHARS "/\\"
86 #else /* HAVE_CYGWIN */
87 # define SFTP_DIRECTORY_CHARS "/"
88 #endif /* HAVE_CYGWIN */
93 u_int download_buflen
;
98 #define SFTP_EXT_POSIX_RENAME 0x00000001
99 #define SFTP_EXT_STATVFS 0x00000002
100 #define SFTP_EXT_FSTATVFS 0x00000004
101 #define SFTP_EXT_HARDLINK 0x00000008
102 #define SFTP_EXT_FSYNC 0x00000010
103 #define SFTP_EXT_LSETSTAT 0x00000020
104 #define SFTP_EXT_LIMITS 0x00000040
105 #define SFTP_EXT_PATH_EXPAND 0x00000080
106 #define SFTP_EXT_COPY_DATA 0x00000100
107 #define SFTP_EXT_GETUSERSGROUPS_BY_ID 0x00000200
109 u_int64_t limit_kbps
;
110 struct bwlimit bwlimit_in
, bwlimit_out
;
113 /* Tracks in-progress requests during file transfers */
118 TAILQ_ENTRY(request
) tq
;
120 TAILQ_HEAD(requests
, request
);
123 get_handle(struct sftp_conn
*conn
, u_int expected_id
, size_t *len
,
124 const char *errfmt
, ...) __attribute__((format(printf
, 4, 5)));
126 static struct request
*
127 request_enqueue(struct requests
*requests
, u_int id
, size_t len
,
132 req
= xcalloc(1, sizeof(*req
));
135 req
->offset
= offset
;
136 TAILQ_INSERT_TAIL(requests
, req
, tq
);
140 static struct request
*
141 request_find(struct requests
*requests
, u_int id
)
145 for (req
= TAILQ_FIRST(requests
);
146 req
!= NULL
&& req
->id
!= id
;
147 req
= TAILQ_NEXT(req
, tq
))
154 sftpio(void *_bwlimit
, size_t amount
)
156 struct bwlimit
*bwlimit
= (struct bwlimit
*)_bwlimit
;
158 refresh_progress_meter(0);
160 bandwidth_limit(bwlimit
, amount
);
165 send_msg(struct sftp_conn
*conn
, struct sshbuf
*m
)
170 if (sshbuf_len(m
) > SFTP_MAX_MSG_LENGTH
)
171 fatal("Outbound message too long %zu", sshbuf_len(m
));
173 /* Send length first */
174 put_u32(mlen
, sshbuf_len(m
));
175 iov
[0].iov_base
= mlen
;
176 iov
[0].iov_len
= sizeof(mlen
);
177 iov
[1].iov_base
= (u_char
*)sshbuf_ptr(m
);
178 iov
[1].iov_len
= sshbuf_len(m
);
180 if (atomiciov6(writev
, conn
->fd_out
, iov
, 2, sftpio
,
181 conn
->limit_kbps
> 0 ? &conn
->bwlimit_out
: NULL
) !=
182 sshbuf_len(m
) + sizeof(mlen
))
183 fatal("Couldn't send packet: %s", strerror(errno
));
189 get_msg_extended(struct sftp_conn
*conn
, struct sshbuf
*m
, int initial
)
196 if ((r
= sshbuf_reserve(m
, 4, &p
)) != 0)
197 fatal_fr(r
, "reserve");
198 if (atomicio6(read
, conn
->fd_in
, p
, 4, sftpio
,
199 conn
->limit_kbps
> 0 ? &conn
->bwlimit_in
: NULL
) != 4) {
200 if (errno
== EPIPE
|| errno
== ECONNRESET
)
201 fatal("Connection closed");
203 fatal("Couldn't read packet: %s", strerror(errno
));
206 if ((r
= sshbuf_get_u32(m
, &msg_len
)) != 0)
207 fatal_fr(r
, "sshbuf_get_u32");
208 if (msg_len
> SFTP_MAX_MSG_LENGTH
) {
209 do_log2(initial
? SYSLOG_LEVEL_ERROR
: SYSLOG_LEVEL_FATAL
,
210 "Received message too long %u", msg_len
);
211 fatal("Ensure the remote shell produces no output "
212 "for non-interactive sessions.");
215 if ((r
= sshbuf_reserve(m
, msg_len
, &p
)) != 0)
216 fatal_fr(r
, "reserve");
217 if (atomicio6(read
, conn
->fd_in
, p
, msg_len
, sftpio
,
218 conn
->limit_kbps
> 0 ? &conn
->bwlimit_in
: NULL
)
221 fatal("Connection closed");
223 fatal("Read packet: %s", strerror(errno
));
228 get_msg(struct sftp_conn
*conn
, struct sshbuf
*m
)
230 get_msg_extended(conn
, m
, 0);
234 send_string_request(struct sftp_conn
*conn
, u_int id
, u_int code
, const char *s
,
240 if ((msg
= sshbuf_new()) == NULL
)
241 fatal_f("sshbuf_new failed");
242 if ((r
= sshbuf_put_u8(msg
, code
)) != 0 ||
243 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
244 (r
= sshbuf_put_string(msg
, s
, len
)) != 0)
245 fatal_fr(r
, "compose");
247 debug3("Sent message fd %d T:%u I:%u", conn
->fd_out
, code
, id
);
252 send_string_attrs_request(struct sftp_conn
*conn
, u_int id
, u_int code
,
253 const void *s
, u_int len
, Attrib
*a
)
258 if ((msg
= sshbuf_new()) == NULL
)
259 fatal_f("sshbuf_new failed");
260 if ((r
= sshbuf_put_u8(msg
, code
)) != 0 ||
261 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
262 (r
= sshbuf_put_string(msg
, s
, len
)) != 0 ||
263 (r
= encode_attrib(msg
, a
)) != 0)
264 fatal_fr(r
, "compose");
266 debug3("Sent message fd %d T:%u I:%u F:0x%04x M:%05o",
267 conn
->fd_out
, code
, id
, a
->flags
, a
->perm
);
272 get_status(struct sftp_conn
*conn
, u_int expected_id
)
279 if ((msg
= sshbuf_new()) == NULL
)
280 fatal_f("sshbuf_new failed");
282 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
283 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
284 fatal_fr(r
, "compose");
286 if (id
!= expected_id
)
287 fatal("ID mismatch (%u != %u)", id
, expected_id
);
288 if (type
!= SSH2_FXP_STATUS
)
289 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
290 SSH2_FXP_STATUS
, type
);
292 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
293 fatal_fr(r
, "parse");
296 debug3("SSH2_FXP_STATUS %u", status
);
302 get_handle(struct sftp_conn
*conn
, u_int expected_id
, size_t *len
,
303 const char *errfmt
, ...)
313 va_start(args
, errfmt
);
315 vsnprintf(errmsg
, sizeof(errmsg
), errfmt
, args
);
318 if ((msg
= sshbuf_new()) == NULL
)
319 fatal_f("sshbuf_new failed");
321 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
322 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
323 fatal_fr(r
, "parse");
325 if (id
!= expected_id
)
326 fatal("%s: ID mismatch (%u != %u)",
327 errfmt
== NULL
? __func__
: errmsg
, id
, expected_id
);
328 if (type
== SSH2_FXP_STATUS
) {
329 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
330 fatal_fr(r
, "parse status");
332 error("%s: %s", errmsg
, fx2txt(status
));
335 } else if (type
!= SSH2_FXP_HANDLE
)
336 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
337 errfmt
== NULL
? __func__
: errmsg
, SSH2_FXP_HANDLE
, type
);
339 if ((r
= sshbuf_get_string(msg
, &handle
, len
)) != 0)
340 fatal_fr(r
, "parse handle");
346 /* XXX returning &static is error-prone. Refactor to fill *Attrib argument */
348 get_decode_stat(struct sftp_conn
*conn
, u_int expected_id
, int quiet
)
356 if ((msg
= sshbuf_new()) == NULL
)
357 fatal_f("sshbuf_new failed");
360 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
361 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
362 fatal_fr(r
, "parse");
364 if (id
!= expected_id
)
365 fatal("ID mismatch (%u != %u)", id
, expected_id
);
366 if (type
== SSH2_FXP_STATUS
) {
369 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
370 fatal_fr(r
, "parse status");
372 debug("stat remote: %s", fx2txt(status
));
374 error("stat remote: %s", fx2txt(status
));
377 } else if (type
!= SSH2_FXP_ATTRS
) {
378 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
379 SSH2_FXP_ATTRS
, type
);
381 if ((r
= decode_attrib(msg
, &a
)) != 0) {
382 error_fr(r
, "decode_attrib");
386 debug3("Received stat reply T:%u I:%u F:0x%04x M:%05o",
387 type
, id
, a
.flags
, a
.perm
);
394 get_decode_statvfs(struct sftp_conn
*conn
, struct sftp_statvfs
*st
,
395 u_int expected_id
, int quiet
)
403 if ((msg
= sshbuf_new()) == NULL
)
404 fatal_f("sshbuf_new failed");
407 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
408 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
409 fatal_fr(r
, "parse");
411 debug3("Received statvfs reply T:%u I:%u", type
, id
);
412 if (id
!= expected_id
)
413 fatal("ID mismatch (%u != %u)", id
, expected_id
);
414 if (type
== SSH2_FXP_STATUS
) {
417 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
418 fatal_fr(r
, "parse status");
420 debug("remote statvfs: %s", fx2txt(status
));
422 error("remote statvfs: %s", fx2txt(status
));
425 } else if (type
!= SSH2_FXP_EXTENDED_REPLY
) {
426 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
427 SSH2_FXP_EXTENDED_REPLY
, type
);
430 memset(st
, 0, sizeof(*st
));
431 if ((r
= sshbuf_get_u64(msg
, &st
->f_bsize
)) != 0 ||
432 (r
= sshbuf_get_u64(msg
, &st
->f_frsize
)) != 0 ||
433 (r
= sshbuf_get_u64(msg
, &st
->f_blocks
)) != 0 ||
434 (r
= sshbuf_get_u64(msg
, &st
->f_bfree
)) != 0 ||
435 (r
= sshbuf_get_u64(msg
, &st
->f_bavail
)) != 0 ||
436 (r
= sshbuf_get_u64(msg
, &st
->f_files
)) != 0 ||
437 (r
= sshbuf_get_u64(msg
, &st
->f_ffree
)) != 0 ||
438 (r
= sshbuf_get_u64(msg
, &st
->f_favail
)) != 0 ||
439 (r
= sshbuf_get_u64(msg
, &st
->f_fsid
)) != 0 ||
440 (r
= sshbuf_get_u64(msg
, &flag
)) != 0 ||
441 (r
= sshbuf_get_u64(msg
, &st
->f_namemax
)) != 0)
442 fatal_fr(r
, "parse statvfs");
444 st
->f_flag
= (flag
& SSH2_FXE_STATVFS_ST_RDONLY
) ? ST_RDONLY
: 0;
445 st
->f_flag
|= (flag
& SSH2_FXE_STATVFS_ST_NOSUID
) ? ST_NOSUID
: 0;
453 do_init(int fd_in
, int fd_out
, u_int transfer_buflen
, u_int num_requests
,
454 u_int64_t limit_kbps
)
458 struct sftp_conn
*ret
;
461 ret
= xcalloc(1, sizeof(*ret
));
464 ret
->fd_out
= fd_out
;
465 ret
->download_buflen
= ret
->upload_buflen
=
466 transfer_buflen
? transfer_buflen
: DEFAULT_COPY_BUFLEN
;
468 num_requests
? num_requests
: DEFAULT_NUM_REQUESTS
;
472 if ((msg
= sshbuf_new()) == NULL
)
473 fatal_f("sshbuf_new failed");
474 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_INIT
)) != 0 ||
475 (r
= sshbuf_put_u32(msg
, SSH2_FILEXFER_VERSION
)) != 0)
476 fatal_fr(r
, "parse");
480 get_msg_extended(ret
, msg
, 1);
482 /* Expecting a VERSION reply */
483 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0)
484 fatal_fr(r
, "parse type");
485 if (type
!= SSH2_FXP_VERSION
) {
486 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
492 if ((r
= sshbuf_get_u32(msg
, &ret
->version
)) != 0)
493 fatal_fr(r
, "parse version");
495 debug2("Remote version: %u", ret
->version
);
497 /* Check for extensions */
498 while (sshbuf_len(msg
) > 0) {
504 if ((r
= sshbuf_get_cstring(msg
, &name
, NULL
)) != 0 ||
505 (r
= sshbuf_get_string(msg
, &value
, &vlen
)) != 0)
506 fatal_fr(r
, "parse extension");
507 if (strcmp(name
, "posix-rename@openssh.com") == 0 &&
508 strcmp((char *)value
, "1") == 0) {
509 ret
->exts
|= SFTP_EXT_POSIX_RENAME
;
511 } else if (strcmp(name
, "statvfs@openssh.com") == 0 &&
512 strcmp((char *)value
, "2") == 0) {
513 ret
->exts
|= SFTP_EXT_STATVFS
;
515 } else if (strcmp(name
, "fstatvfs@openssh.com") == 0 &&
516 strcmp((char *)value
, "2") == 0) {
517 ret
->exts
|= SFTP_EXT_FSTATVFS
;
519 } else if (strcmp(name
, "hardlink@openssh.com") == 0 &&
520 strcmp((char *)value
, "1") == 0) {
521 ret
->exts
|= SFTP_EXT_HARDLINK
;
523 } else if (strcmp(name
, "fsync@openssh.com") == 0 &&
524 strcmp((char *)value
, "1") == 0) {
525 ret
->exts
|= SFTP_EXT_FSYNC
;
527 } else if (strcmp(name
, "lsetstat@openssh.com") == 0 &&
528 strcmp((char *)value
, "1") == 0) {
529 ret
->exts
|= SFTP_EXT_LSETSTAT
;
531 } else if (strcmp(name
, "limits@openssh.com") == 0 &&
532 strcmp((char *)value
, "1") == 0) {
533 ret
->exts
|= SFTP_EXT_LIMITS
;
535 } else if (strcmp(name
, "expand-path@openssh.com") == 0 &&
536 strcmp((char *)value
, "1") == 0) {
537 ret
->exts
|= SFTP_EXT_PATH_EXPAND
;
539 } else if (strcmp(name
, "copy-data") == 0 &&
540 strcmp((char *)value
, "1") == 0) {
541 ret
->exts
|= SFTP_EXT_COPY_DATA
;
543 } else if (strcmp(name
,
544 "users-groups-by-id@openssh.com") == 0 &&
545 strcmp((char *)value
, "1") == 0) {
546 ret
->exts
|= SFTP_EXT_GETUSERSGROUPS_BY_ID
;
550 debug2("Server supports extension \"%s\" revision %s",
553 debug2("Unrecognised server extension \"%s\"", name
);
561 /* Query the server for its limits */
562 if (ret
->exts
& SFTP_EXT_LIMITS
) {
563 struct sftp_limits limits
;
564 if (do_limits(ret
, &limits
) != 0)
565 fatal_f("limits failed");
567 /* If the caller did not specify, find a good value */
568 if (transfer_buflen
== 0) {
569 ret
->download_buflen
= limits
.read_length
;
570 ret
->upload_buflen
= limits
.write_length
;
571 debug("Using server download size %u", ret
->download_buflen
);
572 debug("Using server upload size %u", ret
->upload_buflen
);
575 /* Use the server limit to scale down our value only */
576 if (num_requests
== 0 && limits
.open_handles
) {
578 MINIMUM(DEFAULT_NUM_REQUESTS
, limits
.open_handles
);
579 debug("Server handle limit %llu; using %u",
580 (unsigned long long)limits
.open_handles
,
585 /* Some filexfer v.0 servers don't support large packets */
586 if (ret
->version
== 0) {
587 ret
->download_buflen
= MINIMUM(ret
->download_buflen
, 20480);
588 ret
->upload_buflen
= MINIMUM(ret
->upload_buflen
, 20480);
591 ret
->limit_kbps
= limit_kbps
;
592 if (ret
->limit_kbps
> 0) {
593 bandwidth_limit_init(&ret
->bwlimit_in
, ret
->limit_kbps
,
594 ret
->download_buflen
);
595 bandwidth_limit_init(&ret
->bwlimit_out
, ret
->limit_kbps
,
603 sftp_proto_version(struct sftp_conn
*conn
)
605 return conn
->version
;
609 do_limits(struct sftp_conn
*conn
, struct sftp_limits
*limits
)
616 if ((conn
->exts
& SFTP_EXT_LIMITS
) == 0) {
617 error("Server does not support limits@openssh.com extension");
621 if ((msg
= sshbuf_new()) == NULL
)
622 fatal_f("sshbuf_new failed");
625 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
626 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
627 (r
= sshbuf_put_cstring(msg
, "limits@openssh.com")) != 0)
628 fatal_fr(r
, "compose");
630 debug3("Sent message limits@openssh.com I:%u", id
);
634 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
635 (r
= sshbuf_get_u32(msg
, &msg_id
)) != 0)
636 fatal_fr(r
, "parse");
638 debug3("Received limits reply T:%u I:%u", type
, msg_id
);
640 fatal("ID mismatch (%u != %u)", msg_id
, id
);
641 if (type
!= SSH2_FXP_EXTENDED_REPLY
) {
642 debug_f("expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
643 SSH2_FXP_EXTENDED_REPLY
, type
);
644 /* Disable the limits extension */
645 conn
->exts
&= ~SFTP_EXT_LIMITS
;
650 memset(limits
, 0, sizeof(*limits
));
651 if ((r
= sshbuf_get_u64(msg
, &limits
->packet_length
)) != 0 ||
652 (r
= sshbuf_get_u64(msg
, &limits
->read_length
)) != 0 ||
653 (r
= sshbuf_get_u64(msg
, &limits
->write_length
)) != 0 ||
654 (r
= sshbuf_get_u64(msg
, &limits
->open_handles
)) != 0)
655 fatal_fr(r
, "parse limits");
663 do_close(struct sftp_conn
*conn
, const u_char
*handle
, u_int handle_len
)
669 if ((msg
= sshbuf_new()) == NULL
)
670 fatal_f("sshbuf_new failed");
673 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_CLOSE
)) != 0 ||
674 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
675 (r
= sshbuf_put_string(msg
, handle
, handle_len
)) != 0)
676 fatal_fr(r
, "parse");
678 debug3("Sent message SSH2_FXP_CLOSE I:%u", id
);
680 status
= get_status(conn
, id
);
681 if (status
!= SSH2_FX_OK
)
682 error("close remote: %s", fx2txt(status
));
686 return status
== SSH2_FX_OK
? 0 : -1;
691 do_lsreaddir(struct sftp_conn
*conn
, const char *path
, int print_flag
,
695 u_int count
, id
, i
, expected_id
, ents
= 0;
697 u_char type
, *handle
;
698 int status
= SSH2_FX_FAILURE
;
706 if ((msg
= sshbuf_new()) == NULL
)
707 fatal_f("sshbuf_new failed");
708 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_OPENDIR
)) != 0 ||
709 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
710 (r
= sshbuf_put_cstring(msg
, path
)) != 0)
711 fatal_fr(r
, "compose OPENDIR");
714 handle
= get_handle(conn
, id
, &handle_len
,
715 "remote readdir(\"%s\")", path
);
716 if (handle
== NULL
) {
723 *dir
= xcalloc(1, sizeof(**dir
));
727 for (; !interrupted
;) {
728 id
= expected_id
= conn
->msg_id
++;
730 debug3("Sending SSH2_FXP_READDIR I:%u", id
);
733 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_READDIR
)) != 0 ||
734 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
735 (r
= sshbuf_put_string(msg
, handle
, handle_len
)) != 0)
736 fatal_fr(r
, "compose READDIR");
743 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
744 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
745 fatal_fr(r
, "parse");
747 debug3("Received reply T:%u I:%u", type
, id
);
749 if (id
!= expected_id
)
750 fatal("ID mismatch (%u != %u)", id
, expected_id
);
752 if (type
== SSH2_FXP_STATUS
) {
755 if ((r
= sshbuf_get_u32(msg
, &rstatus
)) != 0)
756 fatal_fr(r
, "parse status");
757 debug3("Received SSH2_FXP_STATUS %d", rstatus
);
758 if (rstatus
== SSH2_FX_EOF
)
760 error("Couldn't read directory: %s", fx2txt(rstatus
));
762 } else if (type
!= SSH2_FXP_NAME
)
763 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
764 SSH2_FXP_NAME
, type
);
766 if ((r
= sshbuf_get_u32(msg
, &count
)) != 0)
767 fatal_fr(r
, "parse count");
768 if (count
> SSHBUF_SIZE_MAX
)
769 fatal_f("nonsensical number of entries");
772 debug3("Received %d SSH2_FXP_NAME responses", count
);
773 for (i
= 0; i
< count
; i
++) {
774 char *filename
, *longname
;
777 if ((r
= sshbuf_get_cstring(msg
, &filename
,
779 (r
= sshbuf_get_cstring(msg
, &longname
,
781 fatal_fr(r
, "parse filenames");
782 if ((r
= decode_attrib(msg
, &a
)) != 0) {
783 error_fr(r
, "couldn't decode attrib");
790 mprintf("%s\n", longname
);
793 * Directory entries should never contain '/'
794 * These can be used to attack recursive ops
795 * (e.g. send '../../../../etc/passwd')
797 if (strpbrk(filename
, SFTP_DIRECTORY_CHARS
) != NULL
) {
798 error("Server sent suspect path \"%s\" "
799 "during readdir of \"%s\"", filename
, path
);
801 *dir
= xreallocarray(*dir
, ents
+ 2, sizeof(**dir
));
802 (*dir
)[ents
] = xcalloc(1, sizeof(***dir
));
803 (*dir
)[ents
]->filename
= xstrdup(filename
);
804 (*dir
)[ents
]->longname
= xstrdup(longname
);
805 memcpy(&(*dir
)[ents
]->a
, &a
, sizeof(a
));
806 (*dir
)[++ents
] = NULL
;
816 do_close(conn
, handle
, handle_len
);
819 if (status
!= 0 && dir
!= NULL
) {
820 /* Don't return results on error */
821 free_sftp_dirents(*dir
);
823 } else if (interrupted
&& dir
!= NULL
&& *dir
!= NULL
) {
824 /* Don't return partial matches on interrupt */
825 free_sftp_dirents(*dir
);
826 *dir
= xcalloc(1, sizeof(**dir
));
830 return status
== SSH2_FX_OK
? 0 : -1;
834 do_readdir(struct sftp_conn
*conn
, const char *path
, SFTP_DIRENT
***dir
)
836 return(do_lsreaddir(conn
, path
, 0, dir
));
839 void free_sftp_dirents(SFTP_DIRENT
**s
)
845 for (i
= 0; s
[i
]; i
++) {
846 free(s
[i
]->filename
);
847 free(s
[i
]->longname
);
854 do_rm(struct sftp_conn
*conn
, const char *path
)
858 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path
);
861 send_string_request(conn
, id
, SSH2_FXP_REMOVE
, path
, strlen(path
));
862 status
= get_status(conn
, id
);
863 if (status
!= SSH2_FX_OK
)
864 error("remote delete %s: %s", path
, fx2txt(status
));
865 return status
== SSH2_FX_OK
? 0 : -1;
869 do_mkdir(struct sftp_conn
*conn
, const char *path
, Attrib
*a
, int print_flag
)
873 debug2("Sending SSH2_FXP_MKDIR \"%s\"", path
);
876 send_string_attrs_request(conn
, id
, SSH2_FXP_MKDIR
, path
,
879 status
= get_status(conn
, id
);
880 if (status
!= SSH2_FX_OK
&& print_flag
)
881 error("remote mkdir \"%s\": %s", path
, fx2txt(status
));
883 return status
== SSH2_FX_OK
? 0 : -1;
887 do_rmdir(struct sftp_conn
*conn
, const char *path
)
891 debug2("Sending SSH2_FXP_RMDIR \"%s\"", path
);
894 send_string_request(conn
, id
, SSH2_FXP_RMDIR
, path
,
897 status
= get_status(conn
, id
);
898 if (status
!= SSH2_FX_OK
)
899 error("remote rmdir \"%s\": %s", path
, fx2txt(status
));
901 return status
== SSH2_FX_OK
? 0 : -1;
905 do_stat(struct sftp_conn
*conn
, const char *path
, int quiet
)
909 debug2("Sending SSH2_FXP_STAT \"%s\"", path
);
913 send_string_request(conn
, id
,
914 conn
->version
== 0 ? SSH2_FXP_STAT_VERSION_0
: SSH2_FXP_STAT
,
917 return(get_decode_stat(conn
, id
, quiet
));
921 do_lstat(struct sftp_conn
*conn
, const char *path
, int quiet
)
925 if (conn
->version
== 0) {
927 debug("Server version does not support lstat operation");
929 logit("Server version does not support lstat operation");
930 return(do_stat(conn
, path
, quiet
));
934 send_string_request(conn
, id
, SSH2_FXP_LSTAT
, path
,
937 return(get_decode_stat(conn
, id
, quiet
));
942 do_fstat(struct sftp_conn
*conn
, const u_char
*handle
, u_int handle_len
,
947 debug2("Sending SSH2_FXP_FSTAT \"%s\"");
950 send_string_request(conn
, id
, SSH2_FXP_FSTAT
, handle
,
953 return(get_decode_stat(conn
, id
, quiet
));
958 do_setstat(struct sftp_conn
*conn
, const char *path
, Attrib
*a
)
962 debug2("Sending SSH2_FXP_SETSTAT \"%s\"", path
);
965 send_string_attrs_request(conn
, id
, SSH2_FXP_SETSTAT
, path
,
968 status
= get_status(conn
, id
);
969 if (status
!= SSH2_FX_OK
)
970 error("remote setstat \"%s\": %s", path
, fx2txt(status
));
972 return status
== SSH2_FX_OK
? 0 : -1;
976 do_fsetstat(struct sftp_conn
*conn
, const u_char
*handle
, u_int handle_len
,
981 debug2("Sending SSH2_FXP_FSETSTAT");
984 send_string_attrs_request(conn
, id
, SSH2_FXP_FSETSTAT
, handle
,
987 status
= get_status(conn
, id
);
988 if (status
!= SSH2_FX_OK
)
989 error("remote fsetstat: %s", fx2txt(status
));
991 return status
== SSH2_FX_OK
? 0 : -1;
994 /* Implements both the realpath and expand-path operations */
996 do_realpath_expand(struct sftp_conn
*conn
, const char *path
, int expand
)
999 u_int expected_id
, count
, id
;
1000 char *filename
, *longname
;
1004 const char *what
= "SSH2_FXP_REALPATH";
1007 what
= "expand-path@openssh.com";
1008 if ((msg
= sshbuf_new()) == NULL
)
1009 fatal_f("sshbuf_new failed");
1011 expected_id
= id
= conn
->msg_id
++;
1013 debug2("Sending SSH2_FXP_EXTENDED(expand-path@openssh.com) "
1015 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
1016 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1017 (r
= sshbuf_put_cstring(msg
,
1018 "expand-path@openssh.com")) != 0 ||
1019 (r
= sshbuf_put_cstring(msg
, path
)) != 0)
1020 fatal_fr(r
, "compose %s", what
);
1021 send_msg(conn
, msg
);
1023 debug2("Sending SSH2_FXP_REALPATH \"%s\"", path
);
1024 send_string_request(conn
, id
, SSH2_FXP_REALPATH
,
1025 path
, strlen(path
));
1028 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
1029 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
1030 fatal_fr(r
, "parse");
1032 if (id
!= expected_id
)
1033 fatal("ID mismatch (%u != %u)", id
, expected_id
);
1035 if (type
== SSH2_FXP_STATUS
) {
1039 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0 ||
1040 (r
= sshbuf_get_cstring(msg
, &errmsg
, NULL
)) != 0)
1041 fatal_fr(r
, "parse status");
1042 error("%s %s: %s", expand
? "expand" : "realpath",
1043 path
, *errmsg
== '\0' ? fx2txt(status
) : errmsg
);
1047 } else if (type
!= SSH2_FXP_NAME
)
1048 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1049 SSH2_FXP_NAME
, type
);
1051 if ((r
= sshbuf_get_u32(msg
, &count
)) != 0)
1052 fatal_fr(r
, "parse count");
1054 fatal("Got multiple names (%d) from %s", count
, what
);
1056 if ((r
= sshbuf_get_cstring(msg
, &filename
, NULL
)) != 0 ||
1057 (r
= sshbuf_get_cstring(msg
, &longname
, NULL
)) != 0 ||
1058 (r
= decode_attrib(msg
, &a
)) != 0)
1059 fatal_fr(r
, "parse filename/attrib");
1061 debug3("%s %s -> %s", what
, path
, filename
);
1071 do_realpath(struct sftp_conn
*conn
, const char *path
)
1073 return do_realpath_expand(conn
, path
, 0);
1077 can_expand_path(struct sftp_conn
*conn
)
1079 return (conn
->exts
& SFTP_EXT_PATH_EXPAND
) != 0;
1083 do_expand_path(struct sftp_conn
*conn
, const char *path
)
1085 if (!can_expand_path(conn
)) {
1086 debug3_f("no server support, fallback to realpath");
1087 return do_realpath_expand(conn
, path
, 0);
1089 return do_realpath_expand(conn
, path
, 1);
1093 do_copy(struct sftp_conn
*conn
, const char *oldpath
, const char *newpath
)
1097 u_char
*old_handle
, *new_handle
;
1098 u_int mode
, status
, id
;
1099 size_t old_handle_len
, new_handle_len
;
1102 /* Return if the extension is not supported */
1103 if ((conn
->exts
& SFTP_EXT_COPY_DATA
) == 0) {
1104 error("Server does not support copy-data extension");
1108 /* Make sure the file exists, and we can copy its perms */
1109 if ((a
= do_stat(conn
, oldpath
, 0)) == NULL
)
1112 /* Do not preserve set[ug]id here, as we do not preserve ownership */
1113 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) {
1114 mode
= a
->perm
& 0777;
1116 if (!S_ISREG(a
->perm
)) {
1117 error("Cannot copy non-regular file: %s", oldpath
);
1121 /* NB: The user's umask will apply to this */
1125 /* Set up the new perms for the new file */
1128 a
->flags
|= SSH2_FILEXFER_ATTR_PERMISSIONS
;
1130 if ((msg
= sshbuf_new()) == NULL
)
1131 fatal("%s: sshbuf_new failed", __func__
);
1133 attrib_clear(&junk
); /* Send empty attributes */
1135 /* Open the old file for reading */
1136 id
= conn
->msg_id
++;
1137 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_OPEN
)) != 0 ||
1138 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1139 (r
= sshbuf_put_cstring(msg
, oldpath
)) != 0 ||
1140 (r
= sshbuf_put_u32(msg
, SSH2_FXF_READ
)) != 0 ||
1141 (r
= encode_attrib(msg
, &junk
)) != 0)
1142 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1143 send_msg(conn
, msg
);
1144 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id
, oldpath
);
1148 old_handle
= get_handle(conn
, id
, &old_handle_len
,
1149 "remote open(\"%s\")", oldpath
);
1150 if (old_handle
== NULL
) {
1155 /* Open the new file for writing */
1156 id
= conn
->msg_id
++;
1157 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_OPEN
)) != 0 ||
1158 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1159 (r
= sshbuf_put_cstring(msg
, newpath
)) != 0 ||
1160 (r
= sshbuf_put_u32(msg
, SSH2_FXF_WRITE
|SSH2_FXF_CREAT
|
1161 SSH2_FXF_TRUNC
)) != 0 ||
1162 (r
= encode_attrib(msg
, a
)) != 0)
1163 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1164 send_msg(conn
, msg
);
1165 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id
, newpath
);
1169 new_handle
= get_handle(conn
, id
, &new_handle_len
,
1170 "remote open(\"%s\")", newpath
);
1171 if (new_handle
== NULL
) {
1177 /* Copy the file data */
1178 id
= conn
->msg_id
++;
1179 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
1180 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1181 (r
= sshbuf_put_cstring(msg
, "copy-data")) != 0 ||
1182 (r
= sshbuf_put_string(msg
, old_handle
, old_handle_len
)) != 0 ||
1183 (r
= sshbuf_put_u64(msg
, 0)) != 0 ||
1184 (r
= sshbuf_put_u64(msg
, 0)) != 0 ||
1185 (r
= sshbuf_put_string(msg
, new_handle
, new_handle_len
)) != 0 ||
1186 (r
= sshbuf_put_u64(msg
, 0)) != 0)
1187 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1188 send_msg(conn
, msg
);
1189 debug3("Sent message copy-data \"%s\" 0 0 -> \"%s\" 0",
1192 status
= get_status(conn
, id
);
1193 if (status
!= SSH2_FX_OK
)
1194 error("Couldn't copy file \"%s\" to \"%s\": %s", oldpath
,
1195 newpath
, fx2txt(status
));
1197 /* Clean up everything */
1199 do_close(conn
, old_handle
, old_handle_len
);
1200 do_close(conn
, new_handle
, new_handle_len
);
1204 return status
== SSH2_FX_OK
? 0 : -1;
1208 do_rename(struct sftp_conn
*conn
, const char *oldpath
, const char *newpath
,
1213 int r
, use_ext
= (conn
->exts
& SFTP_EXT_POSIX_RENAME
) && !force_legacy
;
1215 if ((msg
= sshbuf_new()) == NULL
)
1216 fatal_f("sshbuf_new failed");
1218 /* Send rename request */
1219 id
= conn
->msg_id
++;
1221 debug2("Sending SSH2_FXP_EXTENDED(posix-rename@openssh.com) "
1222 "\"%s\" to \"%s\"", oldpath
, newpath
);
1223 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
1224 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1225 (r
= sshbuf_put_cstring(msg
,
1226 "posix-rename@openssh.com")) != 0)
1227 fatal_fr(r
, "compose posix-rename");
1229 debug2("Sending SSH2_FXP_RENAME \"%s\" to \"%s\"",
1231 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_RENAME
)) != 0 ||
1232 (r
= sshbuf_put_u32(msg
, id
)) != 0)
1233 fatal_fr(r
, "compose rename");
1235 if ((r
= sshbuf_put_cstring(msg
, oldpath
)) != 0 ||
1236 (r
= sshbuf_put_cstring(msg
, newpath
)) != 0)
1237 fatal_fr(r
, "compose paths");
1238 send_msg(conn
, msg
);
1239 debug3("Sent message %s \"%s\" -> \"%s\"",
1240 use_ext
? "posix-rename@openssh.com" :
1241 "SSH2_FXP_RENAME", oldpath
, newpath
);
1244 status
= get_status(conn
, id
);
1245 if (status
!= SSH2_FX_OK
)
1246 error("remote rename \"%s\" to \"%s\": %s", oldpath
,
1247 newpath
, fx2txt(status
));
1249 return status
== SSH2_FX_OK
? 0 : -1;
1253 do_hardlink(struct sftp_conn
*conn
, const char *oldpath
, const char *newpath
)
1259 if ((conn
->exts
& SFTP_EXT_HARDLINK
) == 0) {
1260 error("Server does not support hardlink@openssh.com extension");
1263 debug2("Sending SSH2_FXP_EXTENDED(hardlink@openssh.com) "
1264 "\"%s\" to \"%s\"", oldpath
, newpath
);
1266 if ((msg
= sshbuf_new()) == NULL
)
1267 fatal_f("sshbuf_new failed");
1269 /* Send link request */
1270 id
= conn
->msg_id
++;
1271 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
1272 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1273 (r
= sshbuf_put_cstring(msg
, "hardlink@openssh.com")) != 0 ||
1274 (r
= sshbuf_put_cstring(msg
, oldpath
)) != 0 ||
1275 (r
= sshbuf_put_cstring(msg
, newpath
)) != 0)
1276 fatal_fr(r
, "compose");
1277 send_msg(conn
, msg
);
1278 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
1282 status
= get_status(conn
, id
);
1283 if (status
!= SSH2_FX_OK
)
1284 error("remote link \"%s\" to \"%s\": %s", oldpath
,
1285 newpath
, fx2txt(status
));
1287 return status
== SSH2_FX_OK
? 0 : -1;
1291 do_symlink(struct sftp_conn
*conn
, const char *oldpath
, const char *newpath
)
1297 if (conn
->version
< 3) {
1298 error("This server does not support the symlink operation");
1299 return(SSH2_FX_OP_UNSUPPORTED
);
1301 debug2("Sending SSH2_FXP_SYMLINK \"%s\" to \"%s\"", oldpath
, newpath
);
1303 if ((msg
= sshbuf_new()) == NULL
)
1304 fatal_f("sshbuf_new failed");
1306 /* Send symlink request */
1307 id
= conn
->msg_id
++;
1308 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_SYMLINK
)) != 0 ||
1309 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1310 (r
= sshbuf_put_cstring(msg
, oldpath
)) != 0 ||
1311 (r
= sshbuf_put_cstring(msg
, newpath
)) != 0)
1312 fatal_fr(r
, "compose");
1313 send_msg(conn
, msg
);
1314 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath
,
1318 status
= get_status(conn
, id
);
1319 if (status
!= SSH2_FX_OK
)
1320 error("remote symlink file \"%s\" to \"%s\": %s", oldpath
,
1321 newpath
, fx2txt(status
));
1323 return status
== SSH2_FX_OK
? 0 : -1;
1327 do_fsync(struct sftp_conn
*conn
, u_char
*handle
, u_int handle_len
)
1333 /* Silently return if the extension is not supported */
1334 if ((conn
->exts
& SFTP_EXT_FSYNC
) == 0)
1336 debug2("Sending SSH2_FXP_EXTENDED(fsync@openssh.com)");
1338 /* Send fsync request */
1339 if ((msg
= sshbuf_new()) == NULL
)
1340 fatal_f("sshbuf_new failed");
1341 id
= conn
->msg_id
++;
1342 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
1343 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1344 (r
= sshbuf_put_cstring(msg
, "fsync@openssh.com")) != 0 ||
1345 (r
= sshbuf_put_string(msg
, handle
, handle_len
)) != 0)
1346 fatal_fr(r
, "compose");
1347 send_msg(conn
, msg
);
1348 debug3("Sent message fsync@openssh.com I:%u", id
);
1351 status
= get_status(conn
, id
);
1352 if (status
!= SSH2_FX_OK
)
1353 error("remote fsync: %s", fx2txt(status
));
1355 return status
== SSH2_FX_OK
? 0 : -1;
1360 do_readlink(struct sftp_conn
*conn
, const char *path
)
1363 u_int expected_id
, count
, id
;
1364 char *filename
, *longname
;
1369 debug2("Sending SSH2_FXP_READLINK \"%s\"", path
);
1371 expected_id
= id
= conn
->msg_id
++;
1372 send_string_request(conn
, id
, SSH2_FXP_READLINK
, path
, strlen(path
));
1374 if ((msg
= sshbuf_new()) == NULL
)
1375 fatal_f("sshbuf_new failed");
1378 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
1379 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
1380 fatal_fr(r
, "parse");
1382 if (id
!= expected_id
)
1383 fatal("ID mismatch (%u != %u)", id
, expected_id
);
1385 if (type
== SSH2_FXP_STATUS
) {
1388 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
1389 fatal_fr(r
, "parse status");
1390 error("Couldn't readlink: %s", fx2txt(status
));
1393 } else if (type
!= SSH2_FXP_NAME
)
1394 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1395 SSH2_FXP_NAME
, type
);
1397 if ((r
= sshbuf_get_u32(msg
, &count
)) != 0)
1398 fatal_fr(r
, "parse count");
1400 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count
);
1402 if ((r
= sshbuf_get_cstring(msg
, &filename
, NULL
)) != 0 ||
1403 (r
= sshbuf_get_cstring(msg
, &longname
, NULL
)) != 0 ||
1404 (r
= decode_attrib(msg
, &a
)) != 0)
1405 fatal_fr(r
, "parse filenames/attrib");
1407 debug3("SSH_FXP_READLINK %s -> %s", path
, filename
);
1418 do_statvfs(struct sftp_conn
*conn
, const char *path
, struct sftp_statvfs
*st
,
1425 if ((conn
->exts
& SFTP_EXT_STATVFS
) == 0) {
1426 error("Server does not support statvfs@openssh.com extension");
1430 debug2("Sending SSH2_FXP_EXTENDED(statvfs@openssh.com) \"%s\"", path
);
1432 id
= conn
->msg_id
++;
1434 if ((msg
= sshbuf_new()) == NULL
)
1435 fatal_f("sshbuf_new failed");
1436 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
1437 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1438 (r
= sshbuf_put_cstring(msg
, "statvfs@openssh.com")) != 0 ||
1439 (r
= sshbuf_put_cstring(msg
, path
)) != 0)
1440 fatal_fr(r
, "compose");
1441 send_msg(conn
, msg
);
1444 return get_decode_statvfs(conn
, st
, id
, quiet
);
1449 do_fstatvfs(struct sftp_conn
*conn
, const u_char
*handle
, u_int handle_len
,
1450 struct sftp_statvfs
*st
, int quiet
)
1455 if ((conn
->exts
& SFTP_EXT_FSTATVFS
) == 0) {
1456 error("Server does not support fstatvfs@openssh.com extension");
1460 debug2("Sending SSH2_FXP_EXTENDED(fstatvfs@openssh.com)");
1462 id
= conn
->msg_id
++;
1464 if ((msg
= sshbuf_new()) == NULL
)
1465 fatal_f("sshbuf_new failed");
1466 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
1467 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1468 (r
= sshbuf_put_cstring(msg
, "fstatvfs@openssh.com")) != 0 ||
1469 (r
= sshbuf_put_string(msg
, handle
, handle_len
)) != 0)
1470 fatal_fr(r
, "compose");
1471 send_msg(conn
, msg
);
1474 return get_decode_statvfs(conn
, st
, id
, quiet
);
1479 do_lsetstat(struct sftp_conn
*conn
, const char *path
, Attrib
*a
)
1485 if ((conn
->exts
& SFTP_EXT_LSETSTAT
) == 0) {
1486 error("Server does not support lsetstat@openssh.com extension");
1490 debug2("Sending SSH2_FXP_EXTENDED(lsetstat@openssh.com) \"%s\"", path
);
1492 id
= conn
->msg_id
++;
1493 if ((msg
= sshbuf_new()) == NULL
)
1494 fatal_f("sshbuf_new failed");
1495 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
1496 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1497 (r
= sshbuf_put_cstring(msg
, "lsetstat@openssh.com")) != 0 ||
1498 (r
= sshbuf_put_cstring(msg
, path
)) != 0 ||
1499 (r
= encode_attrib(msg
, a
)) != 0)
1500 fatal_fr(r
, "compose");
1501 send_msg(conn
, msg
);
1504 status
= get_status(conn
, id
);
1505 if (status
!= SSH2_FX_OK
)
1506 error("remote lsetstat \"%s\": %s", path
, fx2txt(status
));
1508 return status
== SSH2_FX_OK
? 0 : -1;
1512 send_read_request(struct sftp_conn
*conn
, u_int id
, u_int64_t offset
,
1513 u_int len
, const u_char
*handle
, u_int handle_len
)
1518 if ((msg
= sshbuf_new()) == NULL
)
1519 fatal_f("sshbuf_new failed");
1520 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_READ
)) != 0 ||
1521 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1522 (r
= sshbuf_put_string(msg
, handle
, handle_len
)) != 0 ||
1523 (r
= sshbuf_put_u64(msg
, offset
)) != 0 ||
1524 (r
= sshbuf_put_u32(msg
, len
)) != 0)
1525 fatal_fr(r
, "compose");
1526 send_msg(conn
, msg
);
1531 send_open(struct sftp_conn
*conn
, const char *path
, const char *tag
,
1532 u_int openmode
, Attrib
*a
, u_char
**handlep
, size_t *handle_lenp
)
1541 debug2("Sending SSH2_FXP_OPEN \"%s\"", path
);
1547 attrib_clear(&junk
); /* Send empty attributes */
1550 /* Send open request */
1551 if ((msg
= sshbuf_new()) == NULL
)
1552 fatal_f("sshbuf_new failed");
1553 id
= conn
->msg_id
++;
1554 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_OPEN
)) != 0 ||
1555 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1556 (r
= sshbuf_put_cstring(msg
, path
)) != 0 ||
1557 (r
= sshbuf_put_u32(msg
, openmode
)) != 0 ||
1558 (r
= encode_attrib(msg
, a
)) != 0)
1559 fatal_fr(r
, "compose %s open", tag
);
1560 send_msg(conn
, msg
);
1562 debug3("Sent %s message SSH2_FXP_OPEN I:%u P:%s M:0x%04x",
1563 tag
, id
, path
, openmode
);
1564 if ((handle
= get_handle(conn
, id
, &handle_len
,
1565 "%s open \"%s\"", tag
, path
)) == NULL
)
1569 *handle_lenp
= handle_len
;
1574 progress_meter_path(const char *path
)
1576 const char *progresspath
;
1578 if ((progresspath
= strrchr(path
, '/')) == NULL
)
1581 if (*progresspath
== '\0')
1583 return progresspath
;
1587 do_download(struct sftp_conn
*conn
, const char *remote_path
,
1588 const char *local_path
, Attrib
*a
, int preserve_flag
, int resume_flag
,
1589 int fsync_flag
, int inplace_flag
)
1593 int local_fd
= -1, write_error
;
1594 int read_error
, write_errno
, lmodified
= 0, reordered
= 0, r
;
1595 u_int64_t offset
= 0, size
, highwater
;
1596 u_int mode
, id
, buflen
, num_req
, max_req
, status
= SSH2_FX_OK
;
1597 off_t progress_counter
;
1600 struct requests requests
;
1601 struct request
*req
;
1604 debug2_f("download remote \"%s\" to local \"%s\"",
1605 remote_path
, local_path
);
1607 TAILQ_INIT(&requests
);
1609 if (a
== NULL
&& (a
= do_stat(conn
, remote_path
, 0)) == NULL
)
1612 /* Do not preserve set[ug]id here, as we do not preserve ownership */
1613 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
)
1614 mode
= a
->perm
& 0777;
1618 if ((a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) &&
1619 (!S_ISREG(a
->perm
))) {
1620 error("download %s: not a regular file", remote_path
);
1624 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
)
1629 buflen
= conn
->download_buflen
;
1631 /* Send open request */
1632 if (send_open(conn
, remote_path
, "remote", SSH2_FXF_READ
, NULL
,
1633 &handle
, &handle_len
) != 0)
1636 local_fd
= open(local_path
, O_WRONLY
| O_CREAT
|
1637 ((resume_flag
|| inplace_flag
) ? 0 : O_TRUNC
), mode
| S_IWUSR
);
1638 if (local_fd
== -1) {
1639 error("open local \"%s\": %s", local_path
, strerror(errno
));
1642 offset
= highwater
= 0;
1644 if (fstat(local_fd
, &st
) == -1) {
1645 error("stat local \"%s\": %s",
1646 local_path
, strerror(errno
));
1649 if (st
.st_size
< 0) {
1650 error("\"%s\" has negative size", local_path
);
1653 if ((u_int64_t
)st
.st_size
> size
) {
1654 error("Unable to resume download of \"%s\": "
1655 "local file is larger than remote", local_path
);
1657 do_close(conn
, handle
, handle_len
);
1663 offset
= highwater
= st
.st_size
;
1666 /* Read from remote and write to local */
1667 write_error
= read_error
= write_errno
= num_req
= 0;
1669 progress_counter
= offset
;
1671 if (showprogress
&& size
!= 0) {
1672 start_progress_meter(progress_meter_path(remote_path
),
1673 size
, &progress_counter
);
1676 if ((msg
= sshbuf_new()) == NULL
)
1677 fatal_f("sshbuf_new failed");
1679 while (num_req
> 0 || max_req
> 0) {
1684 * Simulate EOF on interrupt: stop sending new requests and
1685 * allow outstanding requests to drain gracefully
1688 if (num_req
== 0) /* If we haven't started yet... */
1693 /* Send some more requests */
1694 while (num_req
< max_req
) {
1695 debug3("Request range %llu -> %llu (%d/%d)",
1696 (unsigned long long)offset
,
1697 (unsigned long long)offset
+ buflen
- 1,
1699 req
= request_enqueue(&requests
, conn
->msg_id
++,
1703 send_read_request(conn
, req
->id
, req
->offset
,
1704 req
->len
, handle
, handle_len
);
1709 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
1710 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
1711 fatal_fr(r
, "parse");
1712 debug3("Received reply T:%u I:%u R:%d", type
, id
, max_req
);
1714 /* Find the request in our queue */
1715 if ((req
= request_find(&requests
, id
)) == NULL
)
1716 fatal("Unexpected reply %u", id
);
1719 case SSH2_FXP_STATUS
:
1720 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
1721 fatal_fr(r
, "parse status");
1722 if (status
!= SSH2_FX_EOF
)
1725 TAILQ_REMOVE(&requests
, req
, tq
);
1730 if ((r
= sshbuf_get_string(msg
, &data
, &len
)) != 0)
1731 fatal_fr(r
, "parse data");
1732 debug3("Received data %llu -> %llu",
1733 (unsigned long long)req
->offset
,
1734 (unsigned long long)req
->offset
+ len
- 1);
1736 fatal("Received more data than asked for "
1737 "%zu > %zu", len
, req
->len
);
1739 if ((lseek(local_fd
, req
->offset
, SEEK_SET
) == -1 ||
1740 atomicio(vwrite
, local_fd
, data
, len
) != len
) &&
1742 write_errno
= errno
;
1746 else if (!reordered
&& req
->offset
<= highwater
)
1747 highwater
= req
->offset
+ len
;
1748 else if (!reordered
&& req
->offset
> highwater
)
1750 progress_counter
+= len
;
1753 if (len
== req
->len
) {
1754 TAILQ_REMOVE(&requests
, req
, tq
);
1758 /* Resend the request for the missing data */
1759 debug3("Short data block, re-requesting "
1760 "%llu -> %llu (%2d)",
1761 (unsigned long long)req
->offset
+ len
,
1762 (unsigned long long)req
->offset
+
1763 req
->len
- 1, num_req
);
1764 req
->id
= conn
->msg_id
++;
1767 send_read_request(conn
, req
->id
,
1768 req
->offset
, req
->len
, handle
, handle_len
);
1769 /* Reduce the request size */
1771 buflen
= MAXIMUM(MIN_READ_SIZE
, len
);
1773 if (max_req
> 0) { /* max_req = 0 iff EOF received */
1774 if (size
> 0 && offset
> size
) {
1775 /* Only one request at a time
1776 * after the expected EOF */
1777 debug3("Finish at %llu (%2d)",
1778 (unsigned long long)offset
,
1781 } else if (max_req
< conn
->num_requests
) {
1787 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1788 SSH2_FXP_DATA
, type
);
1792 if (showprogress
&& size
)
1793 stop_progress_meter();
1796 if (TAILQ_FIRST(&requests
) != NULL
)
1797 fatal("Transfer complete, but requests still in queue");
1799 * Truncate at highest contiguous point to avoid holes on interrupt,
1800 * or unconditionally if writing in place.
1802 if (inplace_flag
|| read_error
|| write_error
|| interrupted
) {
1803 if (reordered
&& resume_flag
) {
1804 error("Unable to resume download of \"%s\": "
1805 "server reordered requests", local_path
);
1807 debug("truncating at %llu", (unsigned long long)highwater
);
1808 if (ftruncate(local_fd
, highwater
) == -1)
1809 error("local ftruncate \"%s\": %s", local_path
,
1813 error("read remote \"%s\" : %s", remote_path
, fx2txt(status
));
1815 do_close(conn
, handle
, handle_len
);
1816 } else if (write_error
) {
1817 error("write local \"%s\": %s", local_path
,
1818 strerror(write_errno
));
1819 status
= SSH2_FX_FAILURE
;
1820 do_close(conn
, handle
, handle_len
);
1822 if (do_close(conn
, handle
, handle_len
) != 0 || interrupted
)
1823 status
= SSH2_FX_FAILURE
;
1825 status
= SSH2_FX_OK
;
1826 /* Override umask and utimes if asked */
1828 if (preserve_flag
&& fchmod(local_fd
, mode
) == -1)
1830 if (preserve_flag
&& chmod(local_path
, mode
) == -1)
1831 #endif /* HAVE_FCHMOD */
1832 error("local chmod \"%s\": %s", local_path
,
1834 if (preserve_flag
&&
1835 (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
)) {
1836 struct timeval tv
[2];
1837 tv
[0].tv_sec
= a
->atime
;
1838 tv
[1].tv_sec
= a
->mtime
;
1839 tv
[0].tv_usec
= tv
[1].tv_usec
= 0;
1840 if (utimes(local_path
, tv
) == -1)
1841 error("local set times \"%s\": %s",
1842 local_path
, strerror(errno
));
1844 if (resume_flag
&& !lmodified
)
1845 logit("File \"%s\" was not modified", local_path
);
1846 else if (fsync_flag
) {
1847 debug("syncing \"%s\"", local_path
);
1848 if (fsync(local_fd
) == -1)
1849 error("local sync \"%s\": %s",
1850 local_path
, strerror(errno
));
1857 return status
== SSH2_FX_OK
? 0 : -1;
1861 download_dir_internal(struct sftp_conn
*conn
, const char *src
, const char *dst
,
1862 int depth
, Attrib
*dirattrib
, int preserve_flag
, int print_flag
,
1863 int resume_flag
, int fsync_flag
, int follow_link_flag
, int inplace_flag
)
1866 SFTP_DIRENT
**dir_entries
;
1867 char *filename
, *new_src
= NULL
, *new_dst
= NULL
;
1868 mode_t mode
= 0777, tmpmode
= mode
;
1870 if (depth
>= MAX_DIR_DEPTH
) {
1871 error("Maximum directory depth exceeded: %d levels", depth
);
1875 debug2_f("download dir remote \"%s\" to local \"%s\"", src
, dst
);
1877 if (dirattrib
== NULL
&&
1878 (dirattrib
= do_stat(conn
, src
, 1)) == NULL
) {
1879 error("stat remote \"%s\" directory failed", src
);
1882 if (!S_ISDIR(dirattrib
->perm
)) {
1883 error("\"%s\" is not a directory", src
);
1886 if (print_flag
&& print_flag
!= SFTP_PROGRESS_ONLY
)
1887 mprintf("Retrieving %s\n", src
);
1889 if (dirattrib
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) {
1890 mode
= dirattrib
->perm
& 01777;
1891 tmpmode
= mode
| (S_IWUSR
|S_IXUSR
);
1893 debug("download remote \"%s\": server "
1894 "did not send permissions", dst
);
1897 if (mkdir(dst
, tmpmode
) == -1 && errno
!= EEXIST
) {
1898 error("mkdir %s: %s", dst
, strerror(errno
));
1902 if (do_readdir(conn
, src
, &dir_entries
) == -1) {
1903 error("remote readdir \"%s\" failed", src
);
1907 for (i
= 0; dir_entries
[i
] != NULL
&& !interrupted
; i
++) {
1911 filename
= dir_entries
[i
]->filename
;
1912 new_dst
= path_append(dst
, filename
);
1913 new_src
= path_append(src
, filename
);
1915 if (S_ISDIR(dir_entries
[i
]->a
.perm
)) {
1916 if (strcmp(filename
, ".") == 0 ||
1917 strcmp(filename
, "..") == 0)
1919 if (download_dir_internal(conn
, new_src
, new_dst
,
1920 depth
+ 1, &(dir_entries
[i
]->a
), preserve_flag
,
1921 print_flag
, resume_flag
,
1922 fsync_flag
, follow_link_flag
, inplace_flag
) == -1)
1924 } else if (S_ISREG(dir_entries
[i
]->a
.perm
) ||
1925 (follow_link_flag
&& S_ISLNK(dir_entries
[i
]->a
.perm
))) {
1927 * If this is a symlink then don't send the link's
1928 * Attrib. do_download() will do a FXP_STAT operation
1929 * and get the link target's attributes.
1931 if (do_download(conn
, new_src
, new_dst
,
1932 S_ISLNK(dir_entries
[i
]->a
.perm
) ? NULL
:
1933 &(dir_entries
[i
]->a
),
1934 preserve_flag
, resume_flag
, fsync_flag
,
1935 inplace_flag
) == -1) {
1936 error("Download of file %s to %s failed",
1941 logit("download \"%s\": not a regular file", new_src
);
1947 if (preserve_flag
) {
1948 if (dirattrib
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
) {
1949 struct timeval tv
[2];
1950 tv
[0].tv_sec
= dirattrib
->atime
;
1951 tv
[1].tv_sec
= dirattrib
->mtime
;
1952 tv
[0].tv_usec
= tv
[1].tv_usec
= 0;
1953 if (utimes(dst
, tv
) == -1)
1954 error("local set times on \"%s\": %s",
1955 dst
, strerror(errno
));
1957 debug("Server did not send times for directory "
1961 if (mode
!= tmpmode
&& chmod(dst
, mode
) == -1)
1962 error("local chmod directory \"%s\": %s", dst
,
1965 free_sftp_dirents(dir_entries
);
1971 download_dir(struct sftp_conn
*conn
, const char *src
, const char *dst
,
1972 Attrib
*dirattrib
, int preserve_flag
, int print_flag
, int resume_flag
,
1973 int fsync_flag
, int follow_link_flag
, int inplace_flag
)
1978 if ((src_canon
= do_realpath(conn
, src
)) == NULL
) {
1979 error("download \"%s\": path canonicalization failed", src
);
1983 ret
= download_dir_internal(conn
, src_canon
, dst
, 0,
1984 dirattrib
, preserve_flag
, print_flag
, resume_flag
, fsync_flag
,
1985 follow_link_flag
, inplace_flag
);
1991 do_upload(struct sftp_conn
*conn
, const char *local_path
,
1992 const char *remote_path
, int preserve_flag
, int resume
,
1993 int fsync_flag
, int inplace_flag
)
1996 u_int openmode
, id
, status
= SSH2_FX_OK
, reordered
= 0;
1997 off_t offset
, progress_counter
;
1998 u_char type
, *handle
, *data
;
2001 Attrib a
, t
, *c
= NULL
;
2002 u_int32_t startid
, ackid
;
2003 u_int64_t highwater
= 0;
2004 struct request
*ack
= NULL
;
2005 struct requests acks
;
2008 debug2_f("upload local \"%s\" to remote \"%s\"",
2009 local_path
, remote_path
);
2013 if ((local_fd
= open(local_path
, O_RDONLY
)) == -1) {
2014 error("open local \"%s\": %s", local_path
, strerror(errno
));
2017 if (fstat(local_fd
, &sb
) == -1) {
2018 error("fstat local \"%s\": %s", local_path
, strerror(errno
));
2022 if (!S_ISREG(sb
.st_mode
)) {
2023 error("local \"%s\" is not a regular file", local_path
);
2027 stat_to_attrib(&sb
, &a
);
2029 a
.flags
&= ~SSH2_FILEXFER_ATTR_SIZE
;
2030 a
.flags
&= ~SSH2_FILEXFER_ATTR_UIDGID
;
2033 a
.flags
&= ~SSH2_FILEXFER_ATTR_ACMODTIME
;
2036 /* Get remote file size if it exists */
2037 if ((c
= do_stat(conn
, remote_path
, 0)) == NULL
) {
2042 if ((off_t
)c
->size
>= sb
.st_size
) {
2043 error("resume \"%s\": destination file "
2044 "same size or larger", local_path
);
2049 if (lseek(local_fd
, (off_t
)c
->size
, SEEK_SET
) == -1) {
2055 openmode
= SSH2_FXF_WRITE
|SSH2_FXF_CREAT
;
2057 openmode
|= SSH2_FXF_APPEND
;
2058 else if (!inplace_flag
)
2059 openmode
|= SSH2_FXF_TRUNC
;
2061 /* Send open request */
2062 if (send_open(conn
, remote_path
, "dest", openmode
, &a
,
2063 &handle
, &handle_len
) != 0) {
2069 startid
= ackid
= id
+ 1;
2070 data
= xmalloc(conn
->upload_buflen
);
2072 /* Read from local and write to remote */
2073 offset
= progress_counter
= (resume
? c
->size
: 0);
2075 start_progress_meter(progress_meter_path(local_path
),
2076 sb
.st_size
, &progress_counter
);
2079 if ((msg
= sshbuf_new()) == NULL
)
2080 fatal_f("sshbuf_new failed");
2085 * Can't use atomicio here because it returns 0 on EOF,
2086 * thus losing the last block of the file.
2087 * Simulate an EOF on interrupt, allowing ACKs from the
2090 if (interrupted
|| status
!= SSH2_FX_OK
)
2093 len
= read(local_fd
, data
, conn
->upload_buflen
);
2094 while ((len
== -1) &&
2095 (errno
== EINTR
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
));
2098 fatal("read local \"%s\": %s",
2099 local_path
, strerror(errno
));
2100 } else if (len
!= 0) {
2101 ack
= request_enqueue(&acks
, ++id
, len
, offset
);
2103 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_WRITE
)) != 0 ||
2104 (r
= sshbuf_put_u32(msg
, ack
->id
)) != 0 ||
2105 (r
= sshbuf_put_string(msg
, handle
,
2106 handle_len
)) != 0 ||
2107 (r
= sshbuf_put_u64(msg
, offset
)) != 0 ||
2108 (r
= sshbuf_put_string(msg
, data
, len
)) != 0)
2109 fatal_fr(r
, "compose");
2110 send_msg(conn
, msg
);
2111 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
2112 id
, (unsigned long long)offset
, len
);
2113 } else if (TAILQ_FIRST(&acks
) == NULL
)
2117 fatal("Unexpected ACK %u", id
);
2119 if (id
== startid
|| len
== 0 ||
2120 id
- ackid
>= conn
->num_requests
) {
2125 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
2126 (r
= sshbuf_get_u32(msg
, &rid
)) != 0)
2127 fatal_fr(r
, "parse");
2129 if (type
!= SSH2_FXP_STATUS
)
2130 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
2131 "got %d", SSH2_FXP_STATUS
, type
);
2133 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
2134 fatal_fr(r
, "parse status");
2135 debug3("SSH2_FXP_STATUS %u", status
);
2137 /* Find the request in our queue */
2138 if ((ack
= request_find(&acks
, rid
)) == NULL
)
2139 fatal("Can't find request for ID %u", rid
);
2140 TAILQ_REMOVE(&acks
, ack
, tq
);
2141 debug3("In write loop, ack for %u %zu bytes at %lld",
2142 ack
->id
, ack
->len
, (unsigned long long)ack
->offset
);
2144 progress_counter
+= ack
->len
;
2145 if (!reordered
&& ack
->offset
<= highwater
)
2146 highwater
= ack
->offset
+ ack
->len
;
2147 else if (!reordered
&& ack
->offset
> highwater
) {
2148 debug3_f("server reordered ACKs");
2155 fatal_f("offset < 0");
2160 stop_progress_meter();
2163 if (status
!= SSH2_FX_OK
) {
2164 error("write remote \"%s\": %s", remote_path
, fx2txt(status
));
2165 status
= SSH2_FX_FAILURE
;
2168 if (inplace_flag
|| (resume
&& (status
!= SSH2_FX_OK
|| interrupted
))) {
2169 debug("truncating at %llu", (unsigned long long)highwater
);
2171 t
.flags
= SSH2_FILEXFER_ATTR_SIZE
;
2173 do_fsetstat(conn
, handle
, handle_len
, &t
);
2176 if (close(local_fd
) == -1) {
2177 error("close local \"%s\": %s", local_path
, strerror(errno
));
2178 status
= SSH2_FX_FAILURE
;
2181 /* Override umask and utimes if asked */
2183 do_fsetstat(conn
, handle
, handle_len
, &a
);
2186 (void)do_fsync(conn
, handle
, handle_len
);
2188 if (do_close(conn
, handle
, handle_len
) != 0)
2189 status
= SSH2_FX_FAILURE
;
2193 return status
== SSH2_FX_OK
? 0 : -1;
2197 upload_dir_internal(struct sftp_conn
*conn
, const char *src
, const char *dst
,
2198 int depth
, int preserve_flag
, int print_flag
, int resume
, int fsync_flag
,
2199 int follow_link_flag
, int inplace_flag
)
2204 char *filename
, *new_src
= NULL
, *new_dst
= NULL
;
2206 Attrib a
, *dirattrib
;
2207 u_int32_t saved_perm
;
2209 debug2_f("upload local dir \"%s\" to remote \"%s\"", src
, dst
);
2211 if (depth
>= MAX_DIR_DEPTH
) {
2212 error("Maximum directory depth exceeded: %d levels", depth
);
2216 if (stat(src
, &sb
) == -1) {
2217 error("stat local \"%s\": %s", src
, strerror(errno
));
2220 if (!S_ISDIR(sb
.st_mode
)) {
2221 error("\"%s\" is not a directory", src
);
2224 if (print_flag
&& print_flag
!= SFTP_PROGRESS_ONLY
)
2225 mprintf("Entering %s\n", src
);
2227 stat_to_attrib(&sb
, &a
);
2228 a
.flags
&= ~SSH2_FILEXFER_ATTR_SIZE
;
2229 a
.flags
&= ~SSH2_FILEXFER_ATTR_UIDGID
;
2232 a
.flags
&= ~SSH2_FILEXFER_ATTR_ACMODTIME
;
2235 * sftp lacks a portable status value to match errno EEXIST,
2236 * so if we get a failure back then we must check whether
2237 * the path already existed and is a directory. Ensure we can
2238 * write to the directory we create for the duration of the transfer.
2240 saved_perm
= a
.perm
;
2241 a
.perm
|= (S_IWUSR
|S_IXUSR
);
2242 if (do_mkdir(conn
, dst
, &a
, 0) != 0) {
2243 if ((dirattrib
= do_stat(conn
, dst
, 0)) == NULL
)
2245 if (!S_ISDIR(dirattrib
->perm
)) {
2246 error("\"%s\" exists but is not a directory", dst
);
2250 a
.perm
= saved_perm
;
2252 if ((dirp
= opendir(src
)) == NULL
) {
2253 error("local opendir \"%s\": %s", src
, strerror(errno
));
2257 while (((dp
= readdir(dirp
)) != NULL
) && !interrupted
) {
2262 filename
= dp
->d_name
;
2263 new_dst
= path_append(dst
, filename
);
2264 new_src
= path_append(src
, filename
);
2266 if (lstat(new_src
, &sb
) == -1) {
2267 logit("local lstat \"%s\": %s", filename
,
2270 } else if (S_ISDIR(sb
.st_mode
)) {
2271 if (strcmp(filename
, ".") == 0 ||
2272 strcmp(filename
, "..") == 0)
2275 if (upload_dir_internal(conn
, new_src
, new_dst
,
2276 depth
+ 1, preserve_flag
, print_flag
, resume
,
2277 fsync_flag
, follow_link_flag
, inplace_flag
) == -1)
2279 } else if (S_ISREG(sb
.st_mode
) ||
2280 (follow_link_flag
&& S_ISLNK(sb
.st_mode
))) {
2281 if (do_upload(conn
, new_src
, new_dst
,
2282 preserve_flag
, resume
, fsync_flag
,
2283 inplace_flag
) == -1) {
2284 error("upload \"%s\" to \"%s\" failed",
2289 logit("%s: not a regular file", filename
);
2294 do_setstat(conn
, dst
, &a
);
2296 (void) closedir(dirp
);
2301 upload_dir(struct sftp_conn
*conn
, const char *src
, const char *dst
,
2302 int preserve_flag
, int print_flag
, int resume
, int fsync_flag
,
2303 int follow_link_flag
, int inplace_flag
)
2308 if ((dst_canon
= do_realpath(conn
, dst
)) == NULL
) {
2309 error("upload \"%s\": path canonicalization failed", dst
);
2313 ret
= upload_dir_internal(conn
, src
, dst_canon
, 0, preserve_flag
,
2314 print_flag
, resume
, fsync_flag
, follow_link_flag
, inplace_flag
);
2321 handle_dest_replies(struct sftp_conn
*to
, const char *to_path
, int synchronous
,
2322 u_int
*nreqsp
, u_int
*write_errorp
)
2330 if ((msg
= sshbuf_new()) == NULL
)
2331 fatal_f("sshbuf_new failed");
2333 /* Try to eat replies from the upload side */
2334 while (*nreqsp
> 0) {
2335 debug3_f("%u outstanding replies", *nreqsp
);
2337 /* Bail out if no data is ready to be read */
2339 pfd
.events
= POLLIN
;
2340 if ((r
= poll(&pfd
, 1, 0)) == -1) {
2343 fatal_f("poll: %s", strerror(errno
));
2345 break; /* fd not ready */
2350 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
2351 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
2352 fatal_fr(r
, "dest parse");
2353 debug3("Received dest reply T:%u I:%u R:%u", type
, id
, *nreqsp
);
2354 if (type
!= SSH2_FXP_STATUS
) {
2355 fatal_f("Expected SSH2_FXP_STATUS(%d) packet, got %d",
2356 SSH2_FXP_STATUS
, type
);
2358 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
2359 fatal_fr(r
, "parse dest status");
2360 debug3("dest SSH2_FXP_STATUS %u", status
);
2361 if (status
!= SSH2_FX_OK
) {
2362 /* record first error */
2363 if (*write_errorp
== 0)
2364 *write_errorp
= status
;
2367 * XXX this doesn't do full reply matching like do_upload and
2368 * so cannot gracefully truncate terminated uploads at a
2369 * high-water mark. ATM the only caller of this function (scp)
2370 * doesn't support transfer resumption, so this doesn't matter
2373 * To be safe, do_crossload truncates the destination file to
2374 * zero length on upload failure, since we can't trust the
2375 * server not to have reordered replies that could have
2376 * inserted holes where none existed in the source file.
2378 * XXX we could get a more accutate progress bar if we updated
2379 * the counter based on the reply from the destination...
2383 debug3_f("done: %u outstanding replies", *nreqsp
);
2388 do_crossload(struct sftp_conn
*from
, struct sftp_conn
*to
,
2389 const char *from_path
, const char *to_path
,
2390 Attrib
*a
, int preserve_flag
)
2393 int write_error
, read_error
, r
;
2394 u_int64_t offset
= 0, size
;
2395 u_int id
, buflen
, num_req
, max_req
, status
= SSH2_FX_OK
;
2396 u_int num_upload_req
;
2397 off_t progress_counter
;
2398 u_char
*from_handle
, *to_handle
;
2399 size_t from_handle_len
, to_handle_len
;
2400 struct requests requests
;
2401 struct request
*req
;
2404 debug2_f("crossload src \"%s\" to dst \"%s\"", from_path
, to_path
);
2406 TAILQ_INIT(&requests
);
2408 if (a
== NULL
&& (a
= do_stat(from
, from_path
, 0)) == NULL
)
2411 if ((a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) &&
2412 (!S_ISREG(a
->perm
))) {
2413 error("download \"%s\": not a regular file", from_path
);
2416 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
)
2421 buflen
= from
->download_buflen
;
2422 if (buflen
> to
->upload_buflen
)
2423 buflen
= to
->upload_buflen
;
2425 /* Send open request to read side */
2426 if (send_open(from
, from_path
, "origin", SSH2_FXF_READ
, NULL
,
2427 &from_handle
, &from_handle_len
) != 0)
2430 /* Send open request to write side */
2431 a
->flags
&= ~SSH2_FILEXFER_ATTR_SIZE
;
2432 a
->flags
&= ~SSH2_FILEXFER_ATTR_UIDGID
;
2435 a
->flags
&= ~SSH2_FILEXFER_ATTR_ACMODTIME
;
2436 if (send_open(to
, to_path
, "dest",
2437 SSH2_FXF_WRITE
|SSH2_FXF_CREAT
|SSH2_FXF_TRUNC
, a
,
2438 &to_handle
, &to_handle_len
) != 0) {
2439 do_close(from
, from_handle
, from_handle_len
);
2443 /* Read from remote "from" and write to remote "to" */
2445 write_error
= read_error
= num_req
= num_upload_req
= 0;
2447 progress_counter
= 0;
2449 if (showprogress
&& size
!= 0) {
2450 start_progress_meter(progress_meter_path(from_path
),
2451 size
, &progress_counter
);
2453 if ((msg
= sshbuf_new()) == NULL
)
2454 fatal_f("sshbuf_new failed");
2455 while (num_req
> 0 || max_req
> 0) {
2460 * Simulate EOF on interrupt: stop sending new requests and
2461 * allow outstanding requests to drain gracefully
2464 if (num_req
== 0) /* If we haven't started yet... */
2469 /* Send some more requests */
2470 while (num_req
< max_req
) {
2471 debug3("Request range %llu -> %llu (%d/%d)",
2472 (unsigned long long)offset
,
2473 (unsigned long long)offset
+ buflen
- 1,
2475 req
= request_enqueue(&requests
, from
->msg_id
++,
2479 send_read_request(from
, req
->id
, req
->offset
,
2480 req
->len
, from_handle
, from_handle_len
);
2483 /* Try to eat replies from the upload side (nonblocking) */
2484 handle_dest_replies(to
, to_path
, 0,
2485 &num_upload_req
, &write_error
);
2489 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
2490 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
2491 fatal_fr(r
, "parse");
2492 debug3("Received origin reply T:%u I:%u R:%d",
2495 /* Find the request in our queue */
2496 if ((req
= request_find(&requests
, id
)) == NULL
)
2497 fatal("Unexpected reply %u", id
);
2500 case SSH2_FXP_STATUS
:
2501 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
2502 fatal_fr(r
, "parse status");
2503 if (status
!= SSH2_FX_EOF
)
2506 TAILQ_REMOVE(&requests
, req
, tq
);
2511 if ((r
= sshbuf_get_string(msg
, &data
, &len
)) != 0)
2512 fatal_fr(r
, "parse data");
2513 debug3("Received data %llu -> %llu",
2514 (unsigned long long)req
->offset
,
2515 (unsigned long long)req
->offset
+ len
- 1);
2517 fatal("Received more data than asked for "
2518 "%zu > %zu", len
, req
->len
);
2520 /* Write this chunk out to the destination */
2522 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_WRITE
)) != 0 ||
2523 (r
= sshbuf_put_u32(msg
, to
->msg_id
++)) != 0 ||
2524 (r
= sshbuf_put_string(msg
, to_handle
,
2525 to_handle_len
)) != 0 ||
2526 (r
= sshbuf_put_u64(msg
, req
->offset
)) != 0 ||
2527 (r
= sshbuf_put_string(msg
, data
, len
)) != 0)
2528 fatal_fr(r
, "compose write");
2530 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%zu",
2531 id
, (unsigned long long)offset
, len
);
2533 progress_counter
+= len
;
2536 if (len
== req
->len
) {
2537 TAILQ_REMOVE(&requests
, req
, tq
);
2541 /* Resend the request for the missing data */
2542 debug3("Short data block, re-requesting "
2543 "%llu -> %llu (%2d)",
2544 (unsigned long long)req
->offset
+ len
,
2545 (unsigned long long)req
->offset
+
2546 req
->len
- 1, num_req
);
2547 req
->id
= from
->msg_id
++;
2550 send_read_request(from
, req
->id
,
2551 req
->offset
, req
->len
,
2552 from_handle
, from_handle_len
);
2553 /* Reduce the request size */
2555 buflen
= MAXIMUM(MIN_READ_SIZE
, len
);
2557 if (max_req
> 0) { /* max_req = 0 iff EOF received */
2558 if (size
> 0 && offset
> size
) {
2559 /* Only one request at a time
2560 * after the expected EOF */
2561 debug3("Finish at %llu (%2d)",
2562 (unsigned long long)offset
,
2565 } else if (max_req
< from
->num_requests
) {
2571 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
2572 SSH2_FXP_DATA
, type
);
2576 if (showprogress
&& size
)
2577 stop_progress_meter();
2579 /* Drain replies from the server (blocking) */
2580 debug3_f("waiting for %u replies from destination", num_upload_req
);
2581 handle_dest_replies(to
, to_path
, 1, &num_upload_req
, &write_error
);
2584 if (TAILQ_FIRST(&requests
) != NULL
)
2585 fatal("Transfer complete, but requests still in queue");
2586 /* Truncate at 0 length on interrupt or error to avoid holes at dest */
2587 if (read_error
|| write_error
|| interrupted
) {
2588 debug("truncating \"%s\" at 0", to_path
);
2589 do_close(to
, to_handle
, to_handle_len
);
2591 if (send_open(to
, to_path
, "dest",
2592 SSH2_FXF_WRITE
|SSH2_FXF_CREAT
|SSH2_FXF_TRUNC
, a
,
2593 &to_handle
, &to_handle_len
) != 0) {
2594 error("dest truncate \"%s\" failed", to_path
);
2599 error("read origin \"%s\": %s", from_path
, fx2txt(status
));
2601 do_close(from
, from_handle
, from_handle_len
);
2602 if (to_handle
!= NULL
)
2603 do_close(to
, to_handle
, to_handle_len
);
2604 } else if (write_error
) {
2605 error("write dest \"%s\": %s", to_path
, fx2txt(write_error
));
2606 status
= SSH2_FX_FAILURE
;
2607 do_close(from
, from_handle
, from_handle_len
);
2608 if (to_handle
!= NULL
)
2609 do_close(to
, to_handle
, to_handle_len
);
2611 if (do_close(from
, from_handle
, from_handle_len
) != 0 ||
2615 status
= SSH2_FX_OK
;
2616 if (to_handle
!= NULL
) {
2617 /* Need to resend utimes after write */
2619 do_fsetstat(to
, to_handle
, to_handle_len
, a
);
2620 do_close(to
, to_handle
, to_handle_len
);
2627 return status
== SSH2_FX_OK
? 0 : -1;
2631 crossload_dir_internal(struct sftp_conn
*from
, struct sftp_conn
*to
,
2632 const char *from_path
, const char *to_path
,
2633 int depth
, Attrib
*dirattrib
, int preserve_flag
, int print_flag
,
2634 int follow_link_flag
)
2637 SFTP_DIRENT
**dir_entries
;
2638 char *filename
, *new_from_path
= NULL
, *new_to_path
= NULL
;
2642 debug2_f("crossload dir src \"%s\" to dst \"%s\"", from_path
, to_path
);
2644 if (depth
>= MAX_DIR_DEPTH
) {
2645 error("Maximum directory depth exceeded: %d levels", depth
);
2649 if (dirattrib
== NULL
&&
2650 (dirattrib
= do_stat(from
, from_path
, 1)) == NULL
) {
2651 error("stat remote \"%s\" failed", from_path
);
2654 if (!S_ISDIR(dirattrib
->perm
)) {
2655 error("\"%s\" is not a directory", from_path
);
2658 if (print_flag
&& print_flag
!= SFTP_PROGRESS_ONLY
)
2659 mprintf("Retrieving %s\n", from_path
);
2661 curdir
= *dirattrib
; /* dirattrib will be clobbered */
2662 curdir
.flags
&= ~SSH2_FILEXFER_ATTR_SIZE
;
2663 curdir
.flags
&= ~SSH2_FILEXFER_ATTR_UIDGID
;
2664 if ((curdir
.flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) == 0) {
2665 debug("Origin did not send permissions for "
2666 "directory \"%s\"", to_path
);
2667 curdir
.perm
= S_IWUSR
|S_IXUSR
;
2668 curdir
.flags
|= SSH2_FILEXFER_ATTR_PERMISSIONS
;
2670 /* We need to be able to write to the directory while we transfer it */
2671 mode
= curdir
.perm
& 01777;
2672 curdir
.perm
= mode
| (S_IWUSR
|S_IXUSR
);
2675 * sftp lacks a portable status value to match errno EEXIST,
2676 * so if we get a failure back then we must check whether
2677 * the path already existed and is a directory. Ensure we can
2678 * write to the directory we create for the duration of the transfer.
2680 if (do_mkdir(to
, to_path
, &curdir
, 0) != 0) {
2681 if ((dirattrib
= do_stat(to
, to_path
, 0)) == NULL
)
2683 if (!S_ISDIR(dirattrib
->perm
)) {
2684 error("\"%s\" exists but is not a directory", to_path
);
2690 if (do_readdir(from
, from_path
, &dir_entries
) == -1) {
2691 error("origin readdir \"%s\" failed", from_path
);
2695 for (i
= 0; dir_entries
[i
] != NULL
&& !interrupted
; i
++) {
2696 free(new_from_path
);
2699 filename
= dir_entries
[i
]->filename
;
2700 new_from_path
= path_append(from_path
, filename
);
2701 new_to_path
= path_append(to_path
, filename
);
2703 if (S_ISDIR(dir_entries
[i
]->a
.perm
)) {
2704 if (strcmp(filename
, ".") == 0 ||
2705 strcmp(filename
, "..") == 0)
2707 if (crossload_dir_internal(from
, to
,
2708 new_from_path
, new_to_path
,
2709 depth
+ 1, &(dir_entries
[i
]->a
), preserve_flag
,
2710 print_flag
, follow_link_flag
) == -1)
2712 } else if (S_ISREG(dir_entries
[i
]->a
.perm
) ||
2713 (follow_link_flag
&& S_ISLNK(dir_entries
[i
]->a
.perm
))) {
2715 * If this is a symlink then don't send the link's
2716 * Attrib. do_download() will do a FXP_STAT operation
2717 * and get the link target's attributes.
2719 if (do_crossload(from
, to
, new_from_path
, new_to_path
,
2720 S_ISLNK(dir_entries
[i
]->a
.perm
) ? NULL
:
2721 &(dir_entries
[i
]->a
), preserve_flag
) == -1) {
2722 error("crossload \"%s\" to \"%s\" failed",
2723 new_from_path
, new_to_path
);
2727 logit("origin \"%s\": not a regular file",
2732 free(new_from_path
);
2734 do_setstat(to
, to_path
, &curdir
);
2736 free_sftp_dirents(dir_entries
);
2742 crossload_dir(struct sftp_conn
*from
, struct sftp_conn
*to
,
2743 const char *from_path
, const char *to_path
,
2744 Attrib
*dirattrib
, int preserve_flag
, int print_flag
, int follow_link_flag
)
2746 char *from_path_canon
;
2749 if ((from_path_canon
= do_realpath(from
, from_path
)) == NULL
) {
2750 error("crossload \"%s\": path canonicalization failed",
2755 ret
= crossload_dir_internal(from
, to
, from_path_canon
, to_path
, 0,
2756 dirattrib
, preserve_flag
, print_flag
, follow_link_flag
);
2757 free(from_path_canon
);
2762 can_get_users_groups_by_id(struct sftp_conn
*conn
)
2764 return (conn
->exts
& SFTP_EXT_GETUSERSGROUPS_BY_ID
) != 0;
2768 do_get_users_groups_by_id(struct sftp_conn
*conn
,
2769 const u_int
*uids
, u_int nuids
,
2770 const u_int
*gids
, u_int ngids
,
2771 char ***usernamesp
, char ***groupnamesp
)
2773 struct sshbuf
*msg
, *uidbuf
, *gidbuf
;
2774 u_int i
, expected_id
, id
;
2775 char *name
, **usernames
= NULL
, **groupnames
= NULL
;
2779 *usernamesp
= *groupnamesp
= NULL
;
2780 if (!can_get_users_groups_by_id(conn
))
2781 return SSH_ERR_FEATURE_UNSUPPORTED
;
2783 if ((msg
= sshbuf_new()) == NULL
||
2784 (uidbuf
= sshbuf_new()) == NULL
||
2785 (gidbuf
= sshbuf_new()) == NULL
)
2786 fatal_f("sshbuf_new failed");
2787 expected_id
= id
= conn
->msg_id
++;
2788 debug2("Sending SSH2_FXP_EXTENDED(users-groups-by-id@openssh.com)");
2789 for (i
= 0; i
< nuids
; i
++) {
2790 if ((r
= sshbuf_put_u32(uidbuf
, uids
[i
])) != 0)
2791 fatal_fr(r
, "compose uids");
2793 for (i
= 0; i
< ngids
; i
++) {
2794 if ((r
= sshbuf_put_u32(gidbuf
, gids
[i
])) != 0)
2795 fatal_fr(r
, "compose gids");
2797 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
2798 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
2799 (r
= sshbuf_put_cstring(msg
,
2800 "users-groups-by-id@openssh.com")) != 0 ||
2801 (r
= sshbuf_put_stringb(msg
, uidbuf
)) != 0 ||
2802 (r
= sshbuf_put_stringb(msg
, gidbuf
)) != 0)
2803 fatal_fr(r
, "compose");
2804 send_msg(conn
, msg
);
2806 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
2807 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
2808 fatal_fr(r
, "parse");
2809 if (id
!= expected_id
)
2810 fatal("ID mismatch (%u != %u)", id
, expected_id
);
2811 if (type
== SSH2_FXP_STATUS
) {
2815 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0 ||
2816 (r
= sshbuf_get_cstring(msg
, &errmsg
, NULL
)) != 0)
2817 fatal_fr(r
, "parse status");
2818 error("users-groups-by-id %s",
2819 *errmsg
== '\0' ? fx2txt(status
) : errmsg
);
2822 sshbuf_free(uidbuf
);
2823 sshbuf_free(gidbuf
);
2825 } else if (type
!= SSH2_FXP_EXTENDED_REPLY
)
2826 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
2827 SSH2_FXP_EXTENDED_REPLY
, type
);
2830 sshbuf_free(uidbuf
);
2831 sshbuf_free(gidbuf
);
2832 uidbuf
= gidbuf
= NULL
;
2833 if ((r
= sshbuf_froms(msg
, &uidbuf
)) != 0 ||
2834 (r
= sshbuf_froms(msg
, &gidbuf
)) != 0)
2835 fatal_fr(r
, "parse response");
2837 usernames
= xcalloc(nuids
, sizeof(*usernames
));
2838 for (i
= 0; i
< nuids
; i
++) {
2839 if ((r
= sshbuf_get_cstring(uidbuf
, &name
, NULL
)) != 0)
2840 fatal_fr(r
, "parse user name");
2841 /* Handle unresolved names */
2842 if (*name
== '\0') {
2846 usernames
[i
] = name
;
2850 groupnames
= xcalloc(ngids
, sizeof(*groupnames
));
2851 for (i
= 0; i
< ngids
; i
++) {
2852 if ((r
= sshbuf_get_cstring(gidbuf
, &name
, NULL
)) != 0)
2853 fatal_fr(r
, "parse user name");
2854 /* Handle unresolved names */
2855 if (*name
== '\0') {
2859 groupnames
[i
] = name
;
2862 if (sshbuf_len(uidbuf
) != 0)
2863 fatal_f("unexpected extra username data");
2864 if (sshbuf_len(gidbuf
) != 0)
2865 fatal_f("unexpected extra groupname data");
2866 sshbuf_free(uidbuf
);
2867 sshbuf_free(gidbuf
);
2870 *usernamesp
= usernames
;
2871 *groupnamesp
= groupnames
;
2876 path_append(const char *p1
, const char *p2
)
2879 size_t len
= strlen(p1
) + strlen(p2
) + 2;
2882 strlcpy(ret
, p1
, len
);
2883 if (p1
[0] != '\0' && p1
[strlen(p1
) - 1] != '/')
2884 strlcat(ret
, "/", len
);
2885 strlcat(ret
, p2
, len
);
2891 make_absolute(char *p
, const char *pwd
)
2896 if (p
&& !path_absolute(p
)) {
2897 abs_str
= path_append(pwd
, p
);
2905 remote_is_dir(struct sftp_conn
*conn
, const char *path
)
2909 /* XXX: report errors? */
2910 if ((a
= do_stat(conn
, path
, 1)) == NULL
)
2912 if (!(a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
))
2914 return(S_ISDIR(a
->perm
));
2919 local_is_dir(const char *path
)
2923 /* XXX: report errors? */
2924 if (stat(path
, &sb
) == -1)
2927 return(S_ISDIR(sb
.st_mode
));
2930 /* Check whether path returned from glob(..., GLOB_MARK, ...) is a directory */
2932 globpath_is_dir(const char *pathname
)
2934 size_t l
= strlen(pathname
);
2936 return l
> 0 && pathname
[l
- 1] == '/';