1 /* $OpenBSD: sftp-client.c,v 1.127 2017/08/11 04:41:08 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>
53 #include "progressmeter.h"
58 #include "sftp-common.h"
59 #include "sftp-client.h"
61 extern volatile sig_atomic_t interrupted
;
62 extern int showprogress
;
64 /* Minimum amount of data to read at a time */
65 #define MIN_READ_SIZE 512
67 /* Maximum depth to descend in directory trees */
68 #define MAX_DIR_DEPTH 64
70 /* Directory separator characters */
72 # define SFTP_DIRECTORY_CHARS "/\\"
73 #else /* HAVE_CYGWIN */
74 # define SFTP_DIRECTORY_CHARS "/"
75 #endif /* HAVE_CYGWIN */
80 u_int transfer_buflen
;
84 #define SFTP_EXT_POSIX_RENAME 0x00000001
85 #define SFTP_EXT_STATVFS 0x00000002
86 #define SFTP_EXT_FSTATVFS 0x00000004
87 #define SFTP_EXT_HARDLINK 0x00000008
88 #define SFTP_EXT_FSYNC 0x00000010
91 struct bwlimit bwlimit_in
, bwlimit_out
;
95 get_handle(struct sftp_conn
*conn
, u_int expected_id
, size_t *len
,
96 const char *errfmt
, ...) __attribute__((format(printf
, 4, 5)));
100 sftpio(void *_bwlimit
, size_t amount
)
102 struct bwlimit
*bwlimit
= (struct bwlimit
*)_bwlimit
;
104 bandwidth_limit(bwlimit
, amount
);
109 send_msg(struct sftp_conn
*conn
, struct sshbuf
*m
)
114 if (sshbuf_len(m
) > SFTP_MAX_MSG_LENGTH
)
115 fatal("Outbound message too long %zu", sshbuf_len(m
));
117 /* Send length first */
118 put_u32(mlen
, sshbuf_len(m
));
119 iov
[0].iov_base
= mlen
;
120 iov
[0].iov_len
= sizeof(mlen
);
121 iov
[1].iov_base
= (u_char
*)sshbuf_ptr(m
);
122 iov
[1].iov_len
= sshbuf_len(m
);
124 if (atomiciov6(writev
, conn
->fd_out
, iov
, 2,
125 conn
->limit_kbps
> 0 ? sftpio
: NULL
, &conn
->bwlimit_out
) !=
126 sshbuf_len(m
) + sizeof(mlen
))
127 fatal("Couldn't send packet: %s", strerror(errno
));
133 get_msg(struct sftp_conn
*conn
, struct sshbuf
*m
)
139 if ((r
= sshbuf_reserve(m
, 4, &p
)) != 0)
140 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
141 if (atomicio6(read
, conn
->fd_in
, p
, 4,
142 conn
->limit_kbps
> 0 ? sftpio
: NULL
, &conn
->bwlimit_in
) != 4) {
143 if (errno
== EPIPE
|| errno
== ECONNRESET
)
144 fatal("Connection closed");
146 fatal("Couldn't read packet: %s", strerror(errno
));
149 if ((r
= sshbuf_get_u32(m
, &msg_len
)) != 0)
150 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
151 if (msg_len
> SFTP_MAX_MSG_LENGTH
)
152 fatal("Received message too long %u", msg_len
);
154 if ((r
= sshbuf_reserve(m
, msg_len
, &p
)) != 0)
155 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
156 if (atomicio6(read
, conn
->fd_in
, p
, msg_len
,
157 conn
->limit_kbps
> 0 ? sftpio
: NULL
, &conn
->bwlimit_in
)
160 fatal("Connection closed");
162 fatal("Read packet: %s", strerror(errno
));
167 send_string_request(struct sftp_conn
*conn
, u_int id
, u_int code
, const char *s
,
173 if ((msg
= sshbuf_new()) == NULL
)
174 fatal("%s: sshbuf_new failed", __func__
);
175 if ((r
= sshbuf_put_u8(msg
, code
)) != 0 ||
176 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
177 (r
= sshbuf_put_string(msg
, s
, len
)) != 0)
178 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
180 debug3("Sent message fd %d T:%u I:%u", conn
->fd_out
, code
, id
);
185 send_string_attrs_request(struct sftp_conn
*conn
, u_int id
, u_int code
,
186 const void *s
, u_int len
, Attrib
*a
)
191 if ((msg
= sshbuf_new()) == NULL
)
192 fatal("%s: sshbuf_new failed", __func__
);
193 if ((r
= sshbuf_put_u8(msg
, code
)) != 0 ||
194 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
195 (r
= sshbuf_put_string(msg
, s
, len
)) != 0 ||
196 (r
= encode_attrib(msg
, a
)) != 0)
197 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
199 debug3("Sent message fd %d T:%u I:%u", conn
->fd_out
, code
, id
);
204 get_status(struct sftp_conn
*conn
, u_int expected_id
)
211 if ((msg
= sshbuf_new()) == NULL
)
212 fatal("%s: sshbuf_new failed", __func__
);
214 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
215 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
216 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
218 if (id
!= expected_id
)
219 fatal("ID mismatch (%u != %u)", id
, expected_id
);
220 if (type
!= SSH2_FXP_STATUS
)
221 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
222 SSH2_FXP_STATUS
, type
);
224 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
225 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
228 debug3("SSH2_FXP_STATUS %u", status
);
234 get_handle(struct sftp_conn
*conn
, u_int expected_id
, size_t *len
,
235 const char *errfmt
, ...)
245 va_start(args
, errfmt
);
247 vsnprintf(errmsg
, sizeof(errmsg
), errfmt
, args
);
250 if ((msg
= sshbuf_new()) == NULL
)
251 fatal("%s: sshbuf_new failed", __func__
);
253 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
254 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
255 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
257 if (id
!= expected_id
)
258 fatal("%s: ID mismatch (%u != %u)",
259 errfmt
== NULL
? __func__
: errmsg
, id
, expected_id
);
260 if (type
== SSH2_FXP_STATUS
) {
261 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
262 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
264 error("%s: %s", errmsg
, fx2txt(status
));
267 } else if (type
!= SSH2_FXP_HANDLE
)
268 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
269 errfmt
== NULL
? __func__
: errmsg
, SSH2_FXP_HANDLE
, type
);
271 if ((r
= sshbuf_get_string(msg
, &handle
, len
)) != 0)
272 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
279 get_decode_stat(struct sftp_conn
*conn
, u_int expected_id
, int quiet
)
287 if ((msg
= sshbuf_new()) == NULL
)
288 fatal("%s: sshbuf_new failed", __func__
);
291 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
292 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
293 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
295 debug3("Received stat reply T:%u I:%u", type
, id
);
296 if (id
!= expected_id
)
297 fatal("ID mismatch (%u != %u)", id
, expected_id
);
298 if (type
== SSH2_FXP_STATUS
) {
301 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
302 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
304 debug("Couldn't stat remote file: %s", fx2txt(status
));
306 error("Couldn't stat remote file: %s", fx2txt(status
));
309 } else if (type
!= SSH2_FXP_ATTRS
) {
310 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
311 SSH2_FXP_ATTRS
, type
);
313 if ((r
= decode_attrib(msg
, &a
)) != 0) {
314 error("%s: couldn't decode attrib: %s", __func__
, ssh_err(r
));
324 get_decode_statvfs(struct sftp_conn
*conn
, struct sftp_statvfs
*st
,
325 u_int expected_id
, int quiet
)
333 if ((msg
= sshbuf_new()) == NULL
)
334 fatal("%s: sshbuf_new failed", __func__
);
337 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
338 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
339 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
341 debug3("Received statvfs reply T:%u I:%u", type
, id
);
342 if (id
!= expected_id
)
343 fatal("ID mismatch (%u != %u)", id
, expected_id
);
344 if (type
== SSH2_FXP_STATUS
) {
347 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
348 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
350 debug("Couldn't statvfs: %s", fx2txt(status
));
352 error("Couldn't statvfs: %s", fx2txt(status
));
355 } else if (type
!= SSH2_FXP_EXTENDED_REPLY
) {
356 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
357 SSH2_FXP_EXTENDED_REPLY
, type
);
360 memset(st
, 0, sizeof(*st
));
361 if ((r
= sshbuf_get_u64(msg
, &st
->f_bsize
)) != 0 ||
362 (r
= sshbuf_get_u64(msg
, &st
->f_frsize
)) != 0 ||
363 (r
= sshbuf_get_u64(msg
, &st
->f_blocks
)) != 0 ||
364 (r
= sshbuf_get_u64(msg
, &st
->f_bfree
)) != 0 ||
365 (r
= sshbuf_get_u64(msg
, &st
->f_bavail
)) != 0 ||
366 (r
= sshbuf_get_u64(msg
, &st
->f_files
)) != 0 ||
367 (r
= sshbuf_get_u64(msg
, &st
->f_ffree
)) != 0 ||
368 (r
= sshbuf_get_u64(msg
, &st
->f_favail
)) != 0 ||
369 (r
= sshbuf_get_u64(msg
, &st
->f_fsid
)) != 0 ||
370 (r
= sshbuf_get_u64(msg
, &flag
)) != 0 ||
371 (r
= sshbuf_get_u64(msg
, &st
->f_namemax
)) != 0)
372 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
374 st
->f_flag
= (flag
& SSH2_FXE_STATVFS_ST_RDONLY
) ? ST_RDONLY
: 0;
375 st
->f_flag
|= (flag
& SSH2_FXE_STATVFS_ST_NOSUID
) ? ST_NOSUID
: 0;
383 do_init(int fd_in
, int fd_out
, u_int transfer_buflen
, u_int num_requests
,
384 u_int64_t limit_kbps
)
388 struct sftp_conn
*ret
;
391 ret
= xcalloc(1, sizeof(*ret
));
394 ret
->fd_out
= fd_out
;
395 ret
->transfer_buflen
= transfer_buflen
;
396 ret
->num_requests
= num_requests
;
400 if ((msg
= sshbuf_new()) == NULL
)
401 fatal("%s: sshbuf_new failed", __func__
);
402 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_INIT
)) != 0 ||
403 (r
= sshbuf_put_u32(msg
, SSH2_FILEXFER_VERSION
)) != 0)
404 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
411 /* Expecting a VERSION reply */
412 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0)
413 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
414 if (type
!= SSH2_FXP_VERSION
) {
415 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
421 if ((r
= sshbuf_get_u32(msg
, &ret
->version
)) != 0)
422 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
424 debug2("Remote version: %u", ret
->version
);
426 /* Check for extensions */
427 while (sshbuf_len(msg
) > 0) {
433 if ((r
= sshbuf_get_cstring(msg
, &name
, NULL
)) != 0 ||
434 (r
= sshbuf_get_string(msg
, &value
, &vlen
)) != 0)
435 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
436 if (strcmp(name
, "posix-rename@openssh.com") == 0 &&
437 strcmp((char *)value
, "1") == 0) {
438 ret
->exts
|= SFTP_EXT_POSIX_RENAME
;
440 } else if (strcmp(name
, "statvfs@openssh.com") == 0 &&
441 strcmp((char *)value
, "2") == 0) {
442 ret
->exts
|= SFTP_EXT_STATVFS
;
444 } else if (strcmp(name
, "fstatvfs@openssh.com") == 0 &&
445 strcmp((char *)value
, "2") == 0) {
446 ret
->exts
|= SFTP_EXT_FSTATVFS
;
448 } else if (strcmp(name
, "hardlink@openssh.com") == 0 &&
449 strcmp((char *)value
, "1") == 0) {
450 ret
->exts
|= SFTP_EXT_HARDLINK
;
452 } else if (strcmp(name
, "fsync@openssh.com") == 0 &&
453 strcmp((char *)value
, "1") == 0) {
454 ret
->exts
|= SFTP_EXT_FSYNC
;
458 debug2("Server supports extension \"%s\" revision %s",
461 debug2("Unrecognised server extension \"%s\"", name
);
469 /* Some filexfer v.0 servers don't support large packets */
470 if (ret
->version
== 0)
471 ret
->transfer_buflen
= MINIMUM(ret
->transfer_buflen
, 20480);
473 ret
->limit_kbps
= limit_kbps
;
474 if (ret
->limit_kbps
> 0) {
475 bandwidth_limit_init(&ret
->bwlimit_in
, ret
->limit_kbps
,
476 ret
->transfer_buflen
);
477 bandwidth_limit_init(&ret
->bwlimit_out
, ret
->limit_kbps
,
478 ret
->transfer_buflen
);
485 sftp_proto_version(struct sftp_conn
*conn
)
487 return conn
->version
;
491 do_close(struct sftp_conn
*conn
, const u_char
*handle
, u_int handle_len
)
497 if ((msg
= sshbuf_new()) == NULL
)
498 fatal("%s: sshbuf_new failed", __func__
);
501 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_CLOSE
)) != 0 ||
502 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
503 (r
= sshbuf_put_string(msg
, handle
, handle_len
)) != 0)
504 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
506 debug3("Sent message SSH2_FXP_CLOSE I:%u", id
);
508 status
= get_status(conn
, id
);
509 if (status
!= SSH2_FX_OK
)
510 error("Couldn't close file: %s", fx2txt(status
));
514 return status
== SSH2_FX_OK
? 0 : -1;
519 do_lsreaddir(struct sftp_conn
*conn
, const char *path
, int print_flag
,
523 u_int count
, id
, i
, expected_id
, ents
= 0;
525 u_char type
, *handle
;
526 int status
= SSH2_FX_FAILURE
;
534 if ((msg
= sshbuf_new()) == NULL
)
535 fatal("%s: sshbuf_new failed", __func__
);
536 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_OPENDIR
)) != 0 ||
537 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
538 (r
= sshbuf_put_cstring(msg
, path
)) != 0)
539 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
542 handle
= get_handle(conn
, id
, &handle_len
,
543 "remote readdir(\"%s\")", path
);
544 if (handle
== NULL
) {
551 *dir
= xcalloc(1, sizeof(**dir
));
555 for (; !interrupted
;) {
556 id
= expected_id
= conn
->msg_id
++;
558 debug3("Sending SSH2_FXP_READDIR I:%u", id
);
561 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_READDIR
)) != 0 ||
562 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
563 (r
= sshbuf_put_string(msg
, handle
, handle_len
)) != 0)
564 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
571 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
572 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
573 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
575 debug3("Received reply T:%u I:%u", type
, id
);
577 if (id
!= expected_id
)
578 fatal("ID mismatch (%u != %u)", id
, expected_id
);
580 if (type
== SSH2_FXP_STATUS
) {
583 if ((r
= sshbuf_get_u32(msg
, &rstatus
)) != 0)
584 fatal("%s: buffer error: %s",
585 __func__
, ssh_err(r
));
586 debug3("Received SSH2_FXP_STATUS %d", rstatus
);
587 if (rstatus
== SSH2_FX_EOF
)
589 error("Couldn't read directory: %s", fx2txt(rstatus
));
591 } else if (type
!= SSH2_FXP_NAME
)
592 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
593 SSH2_FXP_NAME
, type
);
595 if ((r
= sshbuf_get_u32(msg
, &count
)) != 0)
596 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
597 if (count
> SSHBUF_SIZE_MAX
)
598 fatal("%s: nonsensical number of entries", __func__
);
601 debug3("Received %d SSH2_FXP_NAME responses", count
);
602 for (i
= 0; i
< count
; i
++) {
603 char *filename
, *longname
;
606 if ((r
= sshbuf_get_cstring(msg
, &filename
,
608 (r
= sshbuf_get_cstring(msg
, &longname
,
610 fatal("%s: buffer error: %s",
611 __func__
, ssh_err(r
));
612 if ((r
= decode_attrib(msg
, &a
)) != 0) {
613 error("%s: couldn't decode attrib: %s",
614 __func__
, ssh_err(r
));
622 mprintf("%s\n", longname
);
625 * Directory entries should never contain '/'
626 * These can be used to attack recursive ops
627 * (e.g. send '../../../../etc/passwd')
629 if (strpbrk(filename
, SFTP_DIRECTORY_CHARS
) != NULL
) {
630 error("Server sent suspect path \"%s\" "
631 "during readdir of \"%s\"", filename
, path
);
633 *dir
= xreallocarray(*dir
, ents
+ 2, sizeof(**dir
));
634 (*dir
)[ents
] = xcalloc(1, sizeof(***dir
));
635 (*dir
)[ents
]->filename
= xstrdup(filename
);
636 (*dir
)[ents
]->longname
= xstrdup(longname
);
637 memcpy(&(*dir
)[ents
]->a
, &a
, sizeof(a
));
638 (*dir
)[++ents
] = NULL
;
648 do_close(conn
, handle
, handle_len
);
651 if (status
!= 0 && dir
!= NULL
) {
652 /* Don't return results on error */
653 free_sftp_dirents(*dir
);
655 } else if (interrupted
&& dir
!= NULL
&& *dir
!= NULL
) {
656 /* Don't return partial matches on interrupt */
657 free_sftp_dirents(*dir
);
658 *dir
= xcalloc(1, sizeof(**dir
));
666 do_readdir(struct sftp_conn
*conn
, const char *path
, SFTP_DIRENT
***dir
)
668 return(do_lsreaddir(conn
, path
, 0, dir
));
671 void free_sftp_dirents(SFTP_DIRENT
**s
)
677 for (i
= 0; s
[i
]; i
++) {
678 free(s
[i
]->filename
);
679 free(s
[i
]->longname
);
686 do_rm(struct sftp_conn
*conn
, const char *path
)
690 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path
);
693 send_string_request(conn
, id
, SSH2_FXP_REMOVE
, path
, strlen(path
));
694 status
= get_status(conn
, id
);
695 if (status
!= SSH2_FX_OK
)
696 error("Couldn't delete file: %s", fx2txt(status
));
697 return status
== SSH2_FX_OK
? 0 : -1;
701 do_mkdir(struct sftp_conn
*conn
, const char *path
, Attrib
*a
, int print_flag
)
706 send_string_attrs_request(conn
, id
, SSH2_FXP_MKDIR
, path
,
709 status
= get_status(conn
, id
);
710 if (status
!= SSH2_FX_OK
&& print_flag
)
711 error("Couldn't create directory: %s", fx2txt(status
));
713 return status
== SSH2_FX_OK
? 0 : -1;
717 do_rmdir(struct sftp_conn
*conn
, const char *path
)
722 send_string_request(conn
, id
, SSH2_FXP_RMDIR
, path
,
725 status
= get_status(conn
, id
);
726 if (status
!= SSH2_FX_OK
)
727 error("Couldn't remove directory: %s", fx2txt(status
));
729 return status
== SSH2_FX_OK
? 0 : -1;
733 do_stat(struct sftp_conn
*conn
, const char *path
, int quiet
)
739 send_string_request(conn
, id
,
740 conn
->version
== 0 ? SSH2_FXP_STAT_VERSION_0
: SSH2_FXP_STAT
,
743 return(get_decode_stat(conn
, id
, quiet
));
747 do_lstat(struct sftp_conn
*conn
, const char *path
, int quiet
)
751 if (conn
->version
== 0) {
753 debug("Server version does not support lstat operation");
755 logit("Server version does not support lstat operation");
756 return(do_stat(conn
, path
, quiet
));
760 send_string_request(conn
, id
, SSH2_FXP_LSTAT
, path
,
763 return(get_decode_stat(conn
, id
, quiet
));
768 do_fstat(struct sftp_conn
*conn
, const u_char
*handle
, u_int handle_len
,
774 send_string_request(conn
, id
, SSH2_FXP_FSTAT
, handle
,
777 return(get_decode_stat(conn
, id
, quiet
));
782 do_setstat(struct sftp_conn
*conn
, const char *path
, Attrib
*a
)
787 send_string_attrs_request(conn
, id
, SSH2_FXP_SETSTAT
, path
,
790 status
= get_status(conn
, id
);
791 if (status
!= SSH2_FX_OK
)
792 error("Couldn't setstat on \"%s\": %s", path
,
795 return status
== SSH2_FX_OK
? 0 : -1;
799 do_fsetstat(struct sftp_conn
*conn
, const u_char
*handle
, u_int handle_len
,
805 send_string_attrs_request(conn
, id
, SSH2_FXP_FSETSTAT
, handle
,
808 status
= get_status(conn
, id
);
809 if (status
!= SSH2_FX_OK
)
810 error("Couldn't fsetstat: %s", fx2txt(status
));
812 return status
== SSH2_FX_OK
? 0 : -1;
816 do_realpath(struct sftp_conn
*conn
, const char *path
)
819 u_int expected_id
, count
, id
;
820 char *filename
, *longname
;
825 expected_id
= id
= conn
->msg_id
++;
826 send_string_request(conn
, id
, SSH2_FXP_REALPATH
, path
,
829 if ((msg
= sshbuf_new()) == NULL
)
830 fatal("%s: sshbuf_new failed", __func__
);
833 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
834 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
835 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
837 if (id
!= expected_id
)
838 fatal("ID mismatch (%u != %u)", id
, expected_id
);
840 if (type
== SSH2_FXP_STATUS
) {
843 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
844 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
845 error("Couldn't canonicalize: %s", fx2txt(status
));
848 } else if (type
!= SSH2_FXP_NAME
)
849 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
850 SSH2_FXP_NAME
, type
);
852 if ((r
= sshbuf_get_u32(msg
, &count
)) != 0)
853 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
855 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count
);
857 if ((r
= sshbuf_get_cstring(msg
, &filename
, NULL
)) != 0 ||
858 (r
= sshbuf_get_cstring(msg
, &longname
, NULL
)) != 0 ||
859 (r
= decode_attrib(msg
, &a
)) != 0)
860 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
862 debug3("SSH_FXP_REALPATH %s -> %s size %lu", path
, filename
,
863 (unsigned long)a
.size
);
873 do_rename(struct sftp_conn
*conn
, const char *oldpath
, const char *newpath
,
878 int r
, use_ext
= (conn
->exts
& SFTP_EXT_POSIX_RENAME
) && !force_legacy
;
880 if ((msg
= sshbuf_new()) == NULL
)
881 fatal("%s: sshbuf_new failed", __func__
);
883 /* Send rename request */
886 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
887 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
888 (r
= sshbuf_put_cstring(msg
,
889 "posix-rename@openssh.com")) != 0)
890 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
892 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_RENAME
)) != 0 ||
893 (r
= sshbuf_put_u32(msg
, id
)) != 0)
894 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
896 if ((r
= sshbuf_put_cstring(msg
, oldpath
)) != 0 ||
897 (r
= sshbuf_put_cstring(msg
, newpath
)) != 0)
898 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
900 debug3("Sent message %s \"%s\" -> \"%s\"",
901 use_ext
? "posix-rename@openssh.com" :
902 "SSH2_FXP_RENAME", oldpath
, newpath
);
905 status
= get_status(conn
, id
);
906 if (status
!= SSH2_FX_OK
)
907 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath
,
908 newpath
, fx2txt(status
));
910 return status
== SSH2_FX_OK
? 0 : -1;
914 do_hardlink(struct sftp_conn
*conn
, const char *oldpath
, const char *newpath
)
920 if ((conn
->exts
& SFTP_EXT_HARDLINK
) == 0) {
921 error("Server does not support hardlink@openssh.com extension");
925 if ((msg
= sshbuf_new()) == NULL
)
926 fatal("%s: sshbuf_new failed", __func__
);
928 /* Send link request */
930 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
931 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
932 (r
= sshbuf_put_cstring(msg
, "hardlink@openssh.com")) != 0 ||
933 (r
= sshbuf_put_cstring(msg
, oldpath
)) != 0 ||
934 (r
= sshbuf_put_cstring(msg
, newpath
)) != 0)
935 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
937 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
941 status
= get_status(conn
, id
);
942 if (status
!= SSH2_FX_OK
)
943 error("Couldn't link file \"%s\" to \"%s\": %s", oldpath
,
944 newpath
, fx2txt(status
));
946 return status
== SSH2_FX_OK
? 0 : -1;
950 do_symlink(struct sftp_conn
*conn
, const char *oldpath
, const char *newpath
)
956 if (conn
->version
< 3) {
957 error("This server does not support the symlink operation");
958 return(SSH2_FX_OP_UNSUPPORTED
);
961 if ((msg
= sshbuf_new()) == NULL
)
962 fatal("%s: sshbuf_new failed", __func__
);
964 /* Send symlink request */
966 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_SYMLINK
)) != 0 ||
967 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
968 (r
= sshbuf_put_cstring(msg
, oldpath
)) != 0 ||
969 (r
= sshbuf_put_cstring(msg
, newpath
)) != 0)
970 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
972 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath
,
976 status
= get_status(conn
, id
);
977 if (status
!= SSH2_FX_OK
)
978 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath
,
979 newpath
, fx2txt(status
));
981 return status
== SSH2_FX_OK
? 0 : -1;
985 do_fsync(struct sftp_conn
*conn
, u_char
*handle
, u_int handle_len
)
991 /* Silently return if the extension is not supported */
992 if ((conn
->exts
& SFTP_EXT_FSYNC
) == 0)
995 /* Send fsync request */
996 if ((msg
= sshbuf_new()) == NULL
)
997 fatal("%s: sshbuf_new failed", __func__
);
999 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
1000 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1001 (r
= sshbuf_put_cstring(msg
, "fsync@openssh.com")) != 0 ||
1002 (r
= sshbuf_put_string(msg
, handle
, handle_len
)) != 0)
1003 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1004 send_msg(conn
, msg
);
1005 debug3("Sent message fsync@openssh.com I:%u", id
);
1008 status
= get_status(conn
, id
);
1009 if (status
!= SSH2_FX_OK
)
1010 error("Couldn't sync file: %s", fx2txt(status
));
1017 do_readlink(struct sftp_conn
*conn
, const char *path
)
1020 u_int expected_id
, count
, id
;
1021 char *filename
, *longname
;
1026 expected_id
= id
= conn
->msg_id
++;
1027 send_string_request(conn
, id
, SSH2_FXP_READLINK
, path
, strlen(path
));
1029 if ((msg
= sshbuf_new()) == NULL
)
1030 fatal("%s: sshbuf_new failed", __func__
);
1033 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
1034 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
1035 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1037 if (id
!= expected_id
)
1038 fatal("ID mismatch (%u != %u)", id
, expected_id
);
1040 if (type
== SSH2_FXP_STATUS
) {
1043 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
1044 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1045 error("Couldn't readlink: %s", fx2txt(status
));
1048 } else if (type
!= SSH2_FXP_NAME
)
1049 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1050 SSH2_FXP_NAME
, type
);
1052 if ((r
= sshbuf_get_u32(msg
, &count
)) != 0)
1053 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1055 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count
);
1057 if ((r
= sshbuf_get_cstring(msg
, &filename
, NULL
)) != 0 ||
1058 (r
= sshbuf_get_cstring(msg
, &longname
, NULL
)) != 0 ||
1059 (r
= decode_attrib(msg
, &a
)) != 0)
1060 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1062 debug3("SSH_FXP_READLINK %s -> %s", path
, filename
);
1073 do_statvfs(struct sftp_conn
*conn
, const char *path
, struct sftp_statvfs
*st
,
1080 if ((conn
->exts
& SFTP_EXT_STATVFS
) == 0) {
1081 error("Server does not support statvfs@openssh.com extension");
1085 id
= conn
->msg_id
++;
1087 if ((msg
= sshbuf_new()) == NULL
)
1088 fatal("%s: sshbuf_new failed", __func__
);
1090 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
1091 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1092 (r
= sshbuf_put_cstring(msg
, "statvfs@openssh.com")) != 0 ||
1093 (r
= sshbuf_put_cstring(msg
, path
)) != 0)
1094 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1095 send_msg(conn
, msg
);
1098 return get_decode_statvfs(conn
, st
, id
, quiet
);
1103 do_fstatvfs(struct sftp_conn
*conn
, const u_char
*handle
, u_int handle_len
,
1104 struct sftp_statvfs
*st
, int quiet
)
1109 if ((conn
->exts
& SFTP_EXT_FSTATVFS
) == 0) {
1110 error("Server does not support fstatvfs@openssh.com extension");
1114 id
= conn
->msg_id
++;
1116 if ((msg
= sshbuf_new()) == NULL
)
1117 fatal("%s: sshbuf_new failed", __func__
);
1119 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_EXTENDED
)) != 0 ||
1120 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1121 (r
= sshbuf_put_cstring(msg
, "fstatvfs@openssh.com")) != 0 ||
1122 (r
= sshbuf_put_string(msg
, handle
, handle_len
)) != 0)
1123 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1124 send_msg(conn
, msg
);
1127 return get_decode_statvfs(conn
, st
, id
, quiet
);
1132 send_read_request(struct sftp_conn
*conn
, u_int id
, u_int64_t offset
,
1133 u_int len
, const u_char
*handle
, u_int handle_len
)
1138 if ((msg
= sshbuf_new()) == NULL
)
1139 fatal("%s: sshbuf_new failed", __func__
);
1141 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_READ
)) != 0 ||
1142 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1143 (r
= sshbuf_put_string(msg
, handle
, handle_len
)) != 0 ||
1144 (r
= sshbuf_put_u64(msg
, offset
)) != 0 ||
1145 (r
= sshbuf_put_u32(msg
, len
)) != 0)
1146 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1147 send_msg(conn
, msg
);
1152 do_download(struct sftp_conn
*conn
, const char *remote_path
,
1153 const char *local_path
, Attrib
*a
, int preserve_flag
, int resume_flag
,
1159 int local_fd
= -1, write_error
;
1160 int read_error
, write_errno
, reordered
= 0, r
;
1161 u_int64_t offset
= 0, size
, highwater
;
1162 u_int mode
, id
, buflen
, num_req
, max_req
, status
= SSH2_FX_OK
;
1163 off_t progress_counter
;
1170 TAILQ_ENTRY(request
) tq
;
1172 TAILQ_HEAD(reqhead
, request
) requests
;
1173 struct request
*req
;
1176 TAILQ_INIT(&requests
);
1178 if (a
== NULL
&& (a
= do_stat(conn
, remote_path
, 0)) == NULL
)
1181 /* Do not preserve set[ug]id here, as we do not preserve ownership */
1182 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
)
1183 mode
= a
->perm
& 0777;
1187 if ((a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) &&
1188 (!S_ISREG(a
->perm
))) {
1189 error("Cannot download non-regular file: %s", remote_path
);
1193 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
)
1198 buflen
= conn
->transfer_buflen
;
1199 if ((msg
= sshbuf_new()) == NULL
)
1200 fatal("%s: sshbuf_new failed", __func__
);
1202 attrib_clear(&junk
); /* Send empty attributes */
1204 /* Send open request */
1205 id
= conn
->msg_id
++;
1206 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_OPEN
)) != 0 ||
1207 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1208 (r
= sshbuf_put_cstring(msg
, remote_path
)) != 0 ||
1209 (r
= sshbuf_put_u32(msg
, SSH2_FXF_READ
)) != 0 ||
1210 (r
= encode_attrib(msg
, &junk
)) != 0)
1211 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1212 send_msg(conn
, msg
);
1213 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id
, remote_path
);
1215 handle
= get_handle(conn
, id
, &handle_len
,
1216 "remote open(\"%s\")", remote_path
);
1217 if (handle
== NULL
) {
1222 local_fd
= open(local_path
,
1223 O_WRONLY
| O_CREAT
| (resume_flag
? 0 : O_TRUNC
), mode
| S_IWUSR
);
1224 if (local_fd
== -1) {
1225 error("Couldn't open local file \"%s\" for writing: %s",
1226 local_path
, strerror(errno
));
1229 offset
= highwater
= 0;
1231 if (fstat(local_fd
, &st
) == -1) {
1232 error("Unable to stat local file \"%s\": %s",
1233 local_path
, strerror(errno
));
1236 if (st
.st_size
< 0) {
1237 error("\"%s\" has negative size", local_path
);
1240 if ((u_int64_t
)st
.st_size
> size
) {
1241 error("Unable to resume download of \"%s\": "
1242 "local file is larger than remote", local_path
);
1244 do_close(conn
, handle
, handle_len
);
1251 offset
= highwater
= st
.st_size
;
1254 /* Read from remote and write to local */
1255 write_error
= read_error
= write_errno
= num_req
= 0;
1257 progress_counter
= offset
;
1259 if (showprogress
&& size
!= 0)
1260 start_progress_meter(remote_path
, size
, &progress_counter
);
1262 while (num_req
> 0 || max_req
> 0) {
1267 * Simulate EOF on interrupt: stop sending new requests and
1268 * allow outstanding requests to drain gracefully
1271 if (num_req
== 0) /* If we haven't started yet... */
1276 /* Send some more requests */
1277 while (num_req
< max_req
) {
1278 debug3("Request range %llu -> %llu (%d/%d)",
1279 (unsigned long long)offset
,
1280 (unsigned long long)offset
+ buflen
- 1,
1282 req
= xcalloc(1, sizeof(*req
));
1283 req
->id
= conn
->msg_id
++;
1285 req
->offset
= offset
;
1288 TAILQ_INSERT_TAIL(&requests
, req
, tq
);
1289 send_read_request(conn
, req
->id
, req
->offset
,
1290 req
->len
, handle
, handle_len
);
1295 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
1296 (r
= sshbuf_get_u32(msg
, &id
)) != 0)
1297 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1298 debug3("Received reply T:%u I:%u R:%d", type
, id
, max_req
);
1300 /* Find the request in our queue */
1301 for (req
= TAILQ_FIRST(&requests
);
1302 req
!= NULL
&& req
->id
!= id
;
1303 req
= TAILQ_NEXT(req
, tq
))
1306 fatal("Unexpected reply %u", id
);
1309 case SSH2_FXP_STATUS
:
1310 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
1311 fatal("%s: buffer error: %s",
1312 __func__
, ssh_err(r
));
1313 if (status
!= SSH2_FX_EOF
)
1316 TAILQ_REMOVE(&requests
, req
, tq
);
1321 if ((r
= sshbuf_get_string(msg
, &data
, &len
)) != 0)
1322 fatal("%s: buffer error: %s",
1323 __func__
, ssh_err(r
));
1324 debug3("Received data %llu -> %llu",
1325 (unsigned long long)req
->offset
,
1326 (unsigned long long)req
->offset
+ len
- 1);
1328 fatal("Received more data than asked for "
1329 "%zu > %zu", len
, req
->len
);
1330 if ((lseek(local_fd
, req
->offset
, SEEK_SET
) == -1 ||
1331 atomicio(vwrite
, local_fd
, data
, len
) != len
) &&
1333 write_errno
= errno
;
1337 else if (!reordered
&& req
->offset
<= highwater
)
1338 highwater
= req
->offset
+ len
;
1339 else if (!reordered
&& req
->offset
> highwater
)
1341 progress_counter
+= len
;
1344 if (len
== req
->len
) {
1345 TAILQ_REMOVE(&requests
, req
, tq
);
1349 /* Resend the request for the missing data */
1350 debug3("Short data block, re-requesting "
1351 "%llu -> %llu (%2d)",
1352 (unsigned long long)req
->offset
+ len
,
1353 (unsigned long long)req
->offset
+
1354 req
->len
- 1, num_req
);
1355 req
->id
= conn
->msg_id
++;
1358 send_read_request(conn
, req
->id
,
1359 req
->offset
, req
->len
, handle
, handle_len
);
1360 /* Reduce the request size */
1362 buflen
= MAXIMUM(MIN_READ_SIZE
, len
);
1364 if (max_req
> 0) { /* max_req = 0 iff EOF received */
1365 if (size
> 0 && offset
> size
) {
1366 /* Only one request at a time
1367 * after the expected EOF */
1368 debug3("Finish at %llu (%2d)",
1369 (unsigned long long)offset
,
1372 } else if (max_req
<= conn
->num_requests
) {
1378 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1379 SSH2_FXP_DATA
, type
);
1383 if (showprogress
&& size
)
1384 stop_progress_meter();
1387 if (TAILQ_FIRST(&requests
) != NULL
)
1388 fatal("Transfer complete, but requests still in queue");
1389 /* Truncate at highest contiguous point to avoid holes on interrupt */
1390 if (read_error
|| write_error
|| interrupted
) {
1391 if (reordered
&& resume_flag
) {
1392 error("Unable to resume download of \"%s\": "
1393 "server reordered requests", local_path
);
1395 debug("truncating at %llu", (unsigned long long)highwater
);
1396 if (ftruncate(local_fd
, highwater
) == -1)
1397 error("ftruncate \"%s\": %s", local_path
,
1401 error("Couldn't read from remote file \"%s\" : %s",
1402 remote_path
, fx2txt(status
));
1404 do_close(conn
, handle
, handle_len
);
1405 } else if (write_error
) {
1406 error("Couldn't write to \"%s\": %s", local_path
,
1407 strerror(write_errno
));
1408 status
= SSH2_FX_FAILURE
;
1409 do_close(conn
, handle
, handle_len
);
1411 if (do_close(conn
, handle
, handle_len
) != 0 || interrupted
)
1412 status
= SSH2_FX_FAILURE
;
1414 status
= SSH2_FX_OK
;
1415 /* Override umask and utimes if asked */
1417 if (preserve_flag
&& fchmod(local_fd
, mode
) == -1)
1419 if (preserve_flag
&& chmod(local_path
, mode
) == -1)
1420 #endif /* HAVE_FCHMOD */
1421 error("Couldn't set mode on \"%s\": %s", local_path
,
1423 if (preserve_flag
&&
1424 (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
)) {
1425 struct timeval tv
[2];
1426 tv
[0].tv_sec
= a
->atime
;
1427 tv
[1].tv_sec
= a
->mtime
;
1428 tv
[0].tv_usec
= tv
[1].tv_usec
= 0;
1429 if (utimes(local_path
, tv
) == -1)
1430 error("Can't set times on \"%s\": %s",
1431 local_path
, strerror(errno
));
1434 debug("syncing \"%s\"", local_path
);
1435 if (fsync(local_fd
) == -1)
1436 error("Couldn't sync file \"%s\": %s",
1437 local_path
, strerror(errno
));
1448 download_dir_internal(struct sftp_conn
*conn
, const char *src
, const char *dst
,
1449 int depth
, Attrib
*dirattrib
, int preserve_flag
, int print_flag
,
1450 int resume_flag
, int fsync_flag
)
1453 SFTP_DIRENT
**dir_entries
;
1454 char *filename
, *new_src
, *new_dst
;
1457 if (depth
>= MAX_DIR_DEPTH
) {
1458 error("Maximum directory depth exceeded: %d levels", depth
);
1462 if (dirattrib
== NULL
&&
1463 (dirattrib
= do_stat(conn
, src
, 1)) == NULL
) {
1464 error("Unable to stat remote directory \"%s\"", src
);
1467 if (!S_ISDIR(dirattrib
->perm
)) {
1468 error("\"%s\" is not a directory", src
);
1472 mprintf("Retrieving %s\n", src
);
1474 if (dirattrib
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
)
1475 mode
= dirattrib
->perm
& 01777;
1477 debug("Server did not send permissions for "
1478 "directory \"%s\"", dst
);
1481 if (mkdir(dst
, mode
) == -1 && errno
!= EEXIST
) {
1482 error("mkdir %s: %s", dst
, strerror(errno
));
1486 if (do_readdir(conn
, src
, &dir_entries
) == -1) {
1487 error("%s: Failed to get directory contents", src
);
1491 for (i
= 0; dir_entries
[i
] != NULL
&& !interrupted
; i
++) {
1492 filename
= dir_entries
[i
]->filename
;
1494 new_dst
= path_append(dst
, filename
);
1495 new_src
= path_append(src
, filename
);
1497 if (S_ISDIR(dir_entries
[i
]->a
.perm
)) {
1498 if (strcmp(filename
, ".") == 0 ||
1499 strcmp(filename
, "..") == 0)
1501 if (download_dir_internal(conn
, new_src
, new_dst
,
1502 depth
+ 1, &(dir_entries
[i
]->a
), preserve_flag
,
1503 print_flag
, resume_flag
, fsync_flag
) == -1)
1505 } else if (S_ISREG(dir_entries
[i
]->a
.perm
) ) {
1506 if (do_download(conn
, new_src
, new_dst
,
1507 &(dir_entries
[i
]->a
), preserve_flag
,
1508 resume_flag
, fsync_flag
) == -1) {
1509 error("Download of file %s to %s failed",
1514 logit("%s: not a regular file\n", new_src
);
1520 if (preserve_flag
) {
1521 if (dirattrib
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
) {
1522 struct timeval tv
[2];
1523 tv
[0].tv_sec
= dirattrib
->atime
;
1524 tv
[1].tv_sec
= dirattrib
->mtime
;
1525 tv
[0].tv_usec
= tv
[1].tv_usec
= 0;
1526 if (utimes(dst
, tv
) == -1)
1527 error("Can't set times on \"%s\": %s",
1528 dst
, strerror(errno
));
1530 debug("Server did not send times for directory "
1534 free_sftp_dirents(dir_entries
);
1540 download_dir(struct sftp_conn
*conn
, const char *src
, const char *dst
,
1541 Attrib
*dirattrib
, int preserve_flag
, int print_flag
, int resume_flag
,
1547 if ((src_canon
= do_realpath(conn
, src
)) == NULL
) {
1548 error("Unable to canonicalize path \"%s\"", src
);
1552 ret
= download_dir_internal(conn
, src_canon
, dst
, 0,
1553 dirattrib
, preserve_flag
, print_flag
, resume_flag
, fsync_flag
);
1559 do_upload(struct sftp_conn
*conn
, const char *local_path
,
1560 const char *remote_path
, int preserve_flag
, int resume
, int fsync_flag
)
1563 u_int status
= SSH2_FX_OK
;
1566 off_t offset
, progress_counter
;
1567 u_char
*handle
, *data
;
1570 Attrib a
, *c
= NULL
;
1573 struct outstanding_ack
{
1577 TAILQ_ENTRY(outstanding_ack
) tq
;
1579 TAILQ_HEAD(ackhead
, outstanding_ack
) acks
;
1580 struct outstanding_ack
*ack
= NULL
;
1585 if ((local_fd
= open(local_path
, O_RDONLY
, 0)) == -1) {
1586 error("Couldn't open local file \"%s\" for reading: %s",
1587 local_path
, strerror(errno
));
1590 if (fstat(local_fd
, &sb
) == -1) {
1591 error("Couldn't fstat local file \"%s\": %s",
1592 local_path
, strerror(errno
));
1596 if (!S_ISREG(sb
.st_mode
)) {
1597 error("%s is not a regular file", local_path
);
1601 stat_to_attrib(&sb
, &a
);
1603 a
.flags
&= ~SSH2_FILEXFER_ATTR_SIZE
;
1604 a
.flags
&= ~SSH2_FILEXFER_ATTR_UIDGID
;
1607 a
.flags
&= ~SSH2_FILEXFER_ATTR_ACMODTIME
;
1610 /* Get remote file size if it exists */
1611 if ((c
= do_stat(conn
, remote_path
, 0)) == NULL
) {
1616 if ((off_t
)c
->size
>= sb
.st_size
) {
1617 error("destination file bigger or same size as "
1623 if (lseek(local_fd
, (off_t
)c
->size
, SEEK_SET
) == -1) {
1629 if ((msg
= sshbuf_new()) == NULL
)
1630 fatal("%s: sshbuf_new failed", __func__
);
1632 /* Send open request */
1633 id
= conn
->msg_id
++;
1634 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_OPEN
)) != 0 ||
1635 (r
= sshbuf_put_u32(msg
, id
)) != 0 ||
1636 (r
= sshbuf_put_cstring(msg
, remote_path
)) != 0 ||
1637 (r
= sshbuf_put_u32(msg
, SSH2_FXF_WRITE
|SSH2_FXF_CREAT
|
1638 (resume
? SSH2_FXF_APPEND
: SSH2_FXF_TRUNC
))) != 0 ||
1639 (r
= encode_attrib(msg
, &a
)) != 0)
1640 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1641 send_msg(conn
, msg
);
1642 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id
, remote_path
);
1646 handle
= get_handle(conn
, id
, &handle_len
,
1647 "remote open(\"%s\")", remote_path
);
1648 if (handle
== NULL
) {
1654 startid
= ackid
= id
+ 1;
1655 data
= xmalloc(conn
->transfer_buflen
);
1657 /* Read from local and write to remote */
1658 offset
= progress_counter
= (resume
? c
->size
: 0);
1660 start_progress_meter(local_path
, sb
.st_size
,
1667 * Can't use atomicio here because it returns 0 on EOF,
1668 * thus losing the last block of the file.
1669 * Simulate an EOF on interrupt, allowing ACKs from the
1672 if (interrupted
|| status
!= SSH2_FX_OK
)
1675 len
= read(local_fd
, data
, conn
->transfer_buflen
);
1676 while ((len
== -1) &&
1677 (errno
== EINTR
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
));
1680 fatal("Couldn't read from \"%s\": %s", local_path
,
1684 ack
= xcalloc(1, sizeof(*ack
));
1686 ack
->offset
= offset
;
1688 TAILQ_INSERT_TAIL(&acks
, ack
, tq
);
1691 if ((r
= sshbuf_put_u8(msg
, SSH2_FXP_WRITE
)) != 0 ||
1692 (r
= sshbuf_put_u32(msg
, ack
->id
)) != 0 ||
1693 (r
= sshbuf_put_string(msg
, handle
,
1694 handle_len
)) != 0 ||
1695 (r
= sshbuf_put_u64(msg
, offset
)) != 0 ||
1696 (r
= sshbuf_put_string(msg
, data
, len
)) != 0)
1697 fatal("%s: buffer error: %s",
1698 __func__
, ssh_err(r
));
1699 send_msg(conn
, msg
);
1700 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1701 id
, (unsigned long long)offset
, len
);
1702 } else if (TAILQ_FIRST(&acks
) == NULL
)
1706 fatal("Unexpected ACK %u", id
);
1708 if (id
== startid
|| len
== 0 ||
1709 id
- ackid
>= conn
->num_requests
) {
1714 if ((r
= sshbuf_get_u8(msg
, &type
)) != 0 ||
1715 (r
= sshbuf_get_u32(msg
, &rid
)) != 0)
1716 fatal("%s: buffer error: %s",
1717 __func__
, ssh_err(r
));
1719 if (type
!= SSH2_FXP_STATUS
)
1720 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1721 "got %d", SSH2_FXP_STATUS
, type
);
1723 if ((r
= sshbuf_get_u32(msg
, &status
)) != 0)
1724 fatal("%s: buffer error: %s",
1725 __func__
, ssh_err(r
));
1726 debug3("SSH2_FXP_STATUS %u", status
);
1728 /* Find the request in our queue */
1729 for (ack
= TAILQ_FIRST(&acks
);
1730 ack
!= NULL
&& ack
->id
!= rid
;
1731 ack
= TAILQ_NEXT(ack
, tq
))
1734 fatal("Can't find request for ID %u", rid
);
1735 TAILQ_REMOVE(&acks
, ack
, tq
);
1736 debug3("In write loop, ack for %u %u bytes at %lld",
1737 ack
->id
, ack
->len
, (long long)ack
->offset
);
1739 progress_counter
+= ack
->len
;
1744 fatal("%s: offset < 0", __func__
);
1749 stop_progress_meter();
1752 if (status
!= SSH2_FX_OK
) {
1753 error("Couldn't write to remote file \"%s\": %s",
1754 remote_path
, fx2txt(status
));
1755 status
= SSH2_FX_FAILURE
;
1758 if (close(local_fd
) == -1) {
1759 error("Couldn't close local file \"%s\": %s", local_path
,
1761 status
= SSH2_FX_FAILURE
;
1764 /* Override umask and utimes if asked */
1766 do_fsetstat(conn
, handle
, handle_len
, &a
);
1769 (void)do_fsync(conn
, handle
, handle_len
);
1771 if (do_close(conn
, handle
, handle_len
) != 0)
1772 status
= SSH2_FX_FAILURE
;
1776 return status
== SSH2_FX_OK
? 0 : -1;
1780 upload_dir_internal(struct sftp_conn
*conn
, const char *src
, const char *dst
,
1781 int depth
, int preserve_flag
, int print_flag
, int resume
, int fsync_flag
)
1786 char *filename
, *new_src
, *new_dst
;
1788 Attrib a
, *dirattrib
;
1790 if (depth
>= MAX_DIR_DEPTH
) {
1791 error("Maximum directory depth exceeded: %d levels", depth
);
1795 if (stat(src
, &sb
) == -1) {
1796 error("Couldn't stat directory \"%s\": %s",
1797 src
, strerror(errno
));
1800 if (!S_ISDIR(sb
.st_mode
)) {
1801 error("\"%s\" is not a directory", src
);
1805 mprintf("Entering %s\n", src
);
1808 stat_to_attrib(&sb
, &a
);
1809 a
.flags
&= ~SSH2_FILEXFER_ATTR_SIZE
;
1810 a
.flags
&= ~SSH2_FILEXFER_ATTR_UIDGID
;
1813 a
.flags
&= ~SSH2_FILEXFER_ATTR_ACMODTIME
;
1816 * sftp lacks a portable status value to match errno EEXIST,
1817 * so if we get a failure back then we must check whether
1818 * the path already existed and is a directory.
1820 if (do_mkdir(conn
, dst
, &a
, 0) != 0) {
1821 if ((dirattrib
= do_stat(conn
, dst
, 0)) == NULL
)
1823 if (!S_ISDIR(dirattrib
->perm
)) {
1824 error("\"%s\" exists but is not a directory", dst
);
1829 if ((dirp
= opendir(src
)) == NULL
) {
1830 error("Failed to open dir \"%s\": %s", src
, strerror(errno
));
1834 while (((dp
= readdir(dirp
)) != NULL
) && !interrupted
) {
1837 filename
= dp
->d_name
;
1838 new_dst
= path_append(dst
, filename
);
1839 new_src
= path_append(src
, filename
);
1841 if (lstat(new_src
, &sb
) == -1) {
1842 logit("%s: lstat failed: %s", filename
,
1845 } else if (S_ISDIR(sb
.st_mode
)) {
1846 if (strcmp(filename
, ".") == 0 ||
1847 strcmp(filename
, "..") == 0)
1850 if (upload_dir_internal(conn
, new_src
, new_dst
,
1851 depth
+ 1, preserve_flag
, print_flag
, resume
,
1854 } else if (S_ISREG(sb
.st_mode
)) {
1855 if (do_upload(conn
, new_src
, new_dst
,
1856 preserve_flag
, resume
, fsync_flag
) == -1) {
1857 error("Uploading of file %s to %s failed!",
1862 logit("%s: not a regular file\n", filename
);
1867 do_setstat(conn
, dst
, &a
);
1869 (void) closedir(dirp
);
1874 upload_dir(struct sftp_conn
*conn
, const char *src
, const char *dst
,
1875 int preserve_flag
, int print_flag
, int resume
, int fsync_flag
)
1880 if ((dst_canon
= do_realpath(conn
, dst
)) == NULL
) {
1881 error("Unable to canonicalize path \"%s\"", dst
);
1885 ret
= upload_dir_internal(conn
, src
, dst_canon
, 0, preserve_flag
,
1886 print_flag
, resume
, fsync_flag
);
1893 path_append(const char *p1
, const char *p2
)
1896 size_t len
= strlen(p1
) + strlen(p2
) + 2;
1899 strlcpy(ret
, p1
, len
);
1900 if (p1
[0] != '\0' && p1
[strlen(p1
) - 1] != '/')
1901 strlcat(ret
, "/", len
);
1902 strlcat(ret
, p2
, len
);