1 /* $NetBSD: sftp-client.c,v 1.1.1.2 2009/12/27 01:07:03 christos Exp $ */
2 /* $OpenBSD: sftp-client.c,v 1.87 2009/06/22 05:39:28 dtucker Exp $ */
4 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* XXX: signed vs unsigned */
21 /* XXX: remove all logging, only return status codes */
22 /* XXX: copy between two remote sites */
25 __RCSID("$NetBSD: sftp-client.c,v 1.2 2009/06/07 22:38:47 christos Exp $");
26 #include <sys/types.h>
27 #include <sys/queue.h>
30 #include <sys/param.h>
31 #include <sys/statvfs.h>
46 #include "progressmeter.h"
50 #include "sftp-common.h"
51 #include "sftp-client.h"
53 extern volatile sig_atomic_t interrupted
;
54 extern int showprogress
;
56 /* Minimum amount of data to read at a time */
57 #define MIN_READ_SIZE 512
62 u_int transfer_buflen
;
66 #define SFTP_EXT_POSIX_RENAME 0x00000001
67 #define SFTP_EXT_STATVFS 0x00000002
68 #define SFTP_EXT_FSTATVFS 0x00000004
73 send_msg(int fd
, Buffer
*m
)
78 if (buffer_len(m
) > SFTP_MAX_MSG_LENGTH
)
79 fatal("Outbound message too long %u", buffer_len(m
));
81 /* Send length first */
82 put_u32(mlen
, buffer_len(m
));
83 iov
[0].iov_base
= mlen
;
84 iov
[0].iov_len
= sizeof(mlen
);
85 iov
[1].iov_base
= buffer_ptr(m
);
86 iov
[1].iov_len
= buffer_len(m
);
88 if (atomiciov(writev
, fd
, iov
, 2) != buffer_len(m
) + sizeof(mlen
))
89 fatal("Couldn't send packet: %s", strerror(errno
));
95 get_msg(int fd
, Buffer
*m
)
99 buffer_append_space(m
, 4);
100 if (atomicio(read
, fd
, buffer_ptr(m
), 4) != 4) {
102 fatal("Connection closed");
104 fatal("Couldn't read packet: %s", strerror(errno
));
107 msg_len
= buffer_get_int(m
);
108 if (msg_len
> SFTP_MAX_MSG_LENGTH
)
109 fatal("Received message too long %u", msg_len
);
111 buffer_append_space(m
, msg_len
);
112 if (atomicio(read
, fd
, buffer_ptr(m
), msg_len
) != msg_len
) {
114 fatal("Connection closed");
116 fatal("Read packet: %s", strerror(errno
));
121 send_string_request(int fd
, u_int id
, u_int code
, char *s
,
127 buffer_put_char(&msg
, code
);
128 buffer_put_int(&msg
, id
);
129 buffer_put_string(&msg
, s
, len
);
131 debug3("Sent message fd %d T:%u I:%u", fd
, code
, id
);
136 send_string_attrs_request(int fd
, u_int id
, u_int code
, char *s
,
137 u_int len
, Attrib
*a
)
142 buffer_put_char(&msg
, code
);
143 buffer_put_int(&msg
, id
);
144 buffer_put_string(&msg
, s
, len
);
145 encode_attrib(&msg
, a
);
147 debug3("Sent message fd %d T:%u I:%u", fd
, code
, id
);
152 get_status(int fd
, u_int expected_id
)
155 u_int type
, id
, status
;
159 type
= buffer_get_char(&msg
);
160 id
= buffer_get_int(&msg
);
162 if (id
!= expected_id
)
163 fatal("ID mismatch (%u != %u)", id
, expected_id
);
164 if (type
!= SSH2_FXP_STATUS
)
165 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
166 SSH2_FXP_STATUS
, type
);
168 status
= buffer_get_int(&msg
);
171 debug3("SSH2_FXP_STATUS %u", status
);
177 get_handle(int fd
, u_int expected_id
, u_int
*len
)
185 type
= buffer_get_char(&msg
);
186 id
= buffer_get_int(&msg
);
188 if (id
!= expected_id
)
189 fatal("ID mismatch (%u != %u)", id
, expected_id
);
190 if (type
== SSH2_FXP_STATUS
) {
191 int status
= buffer_get_int(&msg
);
193 error("Couldn't get handle: %s", fx2txt(status
));
196 } else if (type
!= SSH2_FXP_HANDLE
)
197 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
198 SSH2_FXP_HANDLE
, type
);
200 handle
= buffer_get_string(&msg
, len
);
207 get_decode_stat(int fd
, u_int expected_id
, int quiet
)
216 type
= buffer_get_char(&msg
);
217 id
= buffer_get_int(&msg
);
219 debug3("Received stat reply T:%u I:%u", type
, id
);
220 if (id
!= expected_id
)
221 fatal("ID mismatch (%u != %u)", id
, expected_id
);
222 if (type
== SSH2_FXP_STATUS
) {
223 int status
= buffer_get_int(&msg
);
226 debug("Couldn't stat remote file: %s", fx2txt(status
));
228 error("Couldn't stat remote file: %s", fx2txt(status
));
231 } else if (type
!= SSH2_FXP_ATTRS
) {
232 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
233 SSH2_FXP_ATTRS
, type
);
235 a
= decode_attrib(&msg
);
242 get_decode_statvfs(int fd
, struct sftp_statvfs
*st
, u_int expected_id
,
246 u_int type
, id
, flag
;
251 type
= buffer_get_char(&msg
);
252 id
= buffer_get_int(&msg
);
254 debug3("Received statvfs reply T:%u I:%u", type
, id
);
255 if (id
!= expected_id
)
256 fatal("ID mismatch (%u != %u)", id
, expected_id
);
257 if (type
== SSH2_FXP_STATUS
) {
258 int status
= buffer_get_int(&msg
);
261 debug("Couldn't statvfs: %s", fx2txt(status
));
263 error("Couldn't statvfs: %s", fx2txt(status
));
266 } else if (type
!= SSH2_FXP_EXTENDED_REPLY
) {
267 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
268 SSH2_FXP_EXTENDED_REPLY
, type
);
271 bzero(st
, sizeof(*st
));
272 st
->f_bsize
= buffer_get_int64(&msg
);
273 st
->f_frsize
= buffer_get_int64(&msg
);
274 st
->f_blocks
= buffer_get_int64(&msg
);
275 st
->f_bfree
= buffer_get_int64(&msg
);
276 st
->f_bavail
= buffer_get_int64(&msg
);
277 st
->f_files
= buffer_get_int64(&msg
);
278 st
->f_ffree
= buffer_get_int64(&msg
);
279 st
->f_favail
= buffer_get_int64(&msg
);
280 st
->f_fsid
= buffer_get_int64(&msg
);
281 flag
= buffer_get_int64(&msg
);
282 st
->f_namemax
= buffer_get_int64(&msg
);
284 st
->f_flag
= (flag
& SSH2_FXE_STATVFS_ST_RDONLY
) ? ST_RDONLY
: 0;
285 st
->f_flag
|= (flag
& SSH2_FXE_STATVFS_ST_NOSUID
) ? ST_NOSUID
: 0;
293 do_init(int fd_in
, int fd_out
, u_int transfer_buflen
, u_int num_requests
)
295 u_int type
, exts
= 0;
298 struct sftp_conn
*ret
;
301 buffer_put_char(&msg
, SSH2_FXP_INIT
);
302 buffer_put_int(&msg
, SSH2_FILEXFER_VERSION
);
303 send_msg(fd_out
, &msg
);
307 get_msg(fd_in
, &msg
);
309 /* Expecting a VERSION reply */
310 if ((type
= buffer_get_char(&msg
)) != SSH2_FXP_VERSION
) {
311 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
316 version
= buffer_get_int(&msg
);
318 debug2("Remote version: %d", version
);
320 /* Check for extensions */
321 while (buffer_len(&msg
) > 0) {
322 char *name
= buffer_get_string(&msg
, NULL
);
323 char *value
= buffer_get_string(&msg
, NULL
);
326 if (strcmp(name
, "posix-rename@openssh.com") == 0 &&
327 strcmp(value
, "1") == 0) {
328 exts
|= SFTP_EXT_POSIX_RENAME
;
330 } else if (strcmp(name
, "statvfs@openssh.com") == 0 &&
331 strcmp(value
, "2") == 0) {
332 exts
|= SFTP_EXT_STATVFS
;
334 } if (strcmp(name
, "fstatvfs@openssh.com") == 0 &&
335 strcmp(value
, "2") == 0) {
336 exts
|= SFTP_EXT_FSTATVFS
;
340 debug2("Server supports extension \"%s\" revision %s",
343 debug2("Unrecognised server extension \"%s\"", name
);
351 ret
= xmalloc(sizeof(*ret
));
353 ret
->fd_out
= fd_out
;
354 ret
->transfer_buflen
= transfer_buflen
;
355 ret
->num_requests
= num_requests
;
356 ret
->version
= version
;
360 /* Some filexfer v.0 servers don't support large packets */
362 ret
->transfer_buflen
= MIN(ret
->transfer_buflen
, 20480);
368 sftp_proto_version(struct sftp_conn
*conn
)
370 return(conn
->version
);
374 do_close(struct sftp_conn
*conn
, char *handle
, u_int handle_len
)
382 buffer_put_char(&msg
, SSH2_FXP_CLOSE
);
383 buffer_put_int(&msg
, id
);
384 buffer_put_string(&msg
, handle
, handle_len
);
385 send_msg(conn
->fd_out
, &msg
);
386 debug3("Sent message SSH2_FXP_CLOSE I:%u", id
);
388 status
= get_status(conn
->fd_in
, id
);
389 if (status
!= SSH2_FX_OK
)
390 error("Couldn't close file: %s", fx2txt(status
));
399 do_lsreaddir(struct sftp_conn
*conn
, char *path
, int printflag
,
403 u_int count
, type
, id
, handle_len
, i
, expected_id
, ents
= 0;
409 buffer_put_char(&msg
, SSH2_FXP_OPENDIR
);
410 buffer_put_int(&msg
, id
);
411 buffer_put_cstring(&msg
, path
);
412 send_msg(conn
->fd_out
, &msg
);
416 handle
= get_handle(conn
->fd_in
, id
, &handle_len
);
422 *dir
= xmalloc(sizeof(**dir
));
426 for (; !interrupted
;) {
427 id
= expected_id
= conn
->msg_id
++;
429 debug3("Sending SSH2_FXP_READDIR I:%u", id
);
432 buffer_put_char(&msg
, SSH2_FXP_READDIR
);
433 buffer_put_int(&msg
, id
);
434 buffer_put_string(&msg
, handle
, handle_len
);
435 send_msg(conn
->fd_out
, &msg
);
439 get_msg(conn
->fd_in
, &msg
);
441 type
= buffer_get_char(&msg
);
442 id
= buffer_get_int(&msg
);
444 debug3("Received reply T:%u I:%u", type
, id
);
446 if (id
!= expected_id
)
447 fatal("ID mismatch (%u != %u)", id
, expected_id
);
449 if (type
== SSH2_FXP_STATUS
) {
450 int status
= buffer_get_int(&msg
);
452 debug3("Received SSH2_FXP_STATUS %d", status
);
454 if (status
== SSH2_FX_EOF
) {
457 error("Couldn't read directory: %s",
459 do_close(conn
, handle
, handle_len
);
463 } else if (type
!= SSH2_FXP_NAME
)
464 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
465 SSH2_FXP_NAME
, type
);
467 count
= buffer_get_int(&msg
);
470 debug3("Received %d SSH2_FXP_NAME responses", count
);
471 for (i
= 0; i
< count
; i
++) {
472 char *filename
, *longname
;
475 filename
= buffer_get_string(&msg
, NULL
);
476 longname
= buffer_get_string(&msg
, NULL
);
477 a
= decode_attrib(&msg
);
480 printf("%s\n", longname
);
483 *dir
= xrealloc(*dir
, ents
+ 2, sizeof(**dir
));
484 (*dir
)[ents
] = xmalloc(sizeof(***dir
));
485 (*dir
)[ents
]->filename
= xstrdup(filename
);
486 (*dir
)[ents
]->longname
= xstrdup(longname
);
487 memcpy(&(*dir
)[ents
]->a
, a
, sizeof(*a
));
488 (*dir
)[++ents
] = NULL
;
497 do_close(conn
, handle
, handle_len
);
500 /* Don't return partial matches on interrupt */
501 if (interrupted
&& dir
!= NULL
&& *dir
!= NULL
) {
502 free_sftp_dirents(*dir
);
503 *dir
= xmalloc(sizeof(**dir
));
511 do_readdir(struct sftp_conn
*conn
, char *path
, SFTP_DIRENT
***dir
)
513 return(do_lsreaddir(conn
, path
, 0, dir
));
516 void free_sftp_dirents(SFTP_DIRENT
**s
)
520 for (i
= 0; s
[i
]; i
++) {
521 xfree(s
[i
]->filename
);
522 xfree(s
[i
]->longname
);
529 do_rm(struct sftp_conn
*conn
, char *path
)
533 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path
);
536 send_string_request(conn
->fd_out
, id
, SSH2_FXP_REMOVE
, path
,
538 status
= get_status(conn
->fd_in
, id
);
539 if (status
!= SSH2_FX_OK
)
540 error("Couldn't delete file: %s", fx2txt(status
));
545 do_mkdir(struct sftp_conn
*conn
, char *path
, Attrib
*a
)
550 send_string_attrs_request(conn
->fd_out
, id
, SSH2_FXP_MKDIR
, path
,
553 status
= get_status(conn
->fd_in
, id
);
554 if (status
!= SSH2_FX_OK
)
555 error("Couldn't create directory: %s", fx2txt(status
));
561 do_rmdir(struct sftp_conn
*conn
, char *path
)
566 send_string_request(conn
->fd_out
, id
, SSH2_FXP_RMDIR
, path
,
569 status
= get_status(conn
->fd_in
, id
);
570 if (status
!= SSH2_FX_OK
)
571 error("Couldn't remove directory: %s", fx2txt(status
));
577 do_stat(struct sftp_conn
*conn
, char *path
, int quiet
)
583 send_string_request(conn
->fd_out
, id
,
584 conn
->version
== 0 ? SSH2_FXP_STAT_VERSION_0
: SSH2_FXP_STAT
,
587 return(get_decode_stat(conn
->fd_in
, id
, quiet
));
591 do_lstat(struct sftp_conn
*conn
, char *path
, int quiet
)
595 if (conn
->version
== 0) {
597 debug("Server version does not support lstat operation");
599 logit("Server version does not support lstat operation");
600 return(do_stat(conn
, path
, quiet
));
604 send_string_request(conn
->fd_out
, id
, SSH2_FXP_LSTAT
, path
,
607 return(get_decode_stat(conn
->fd_in
, id
, quiet
));
612 do_fstat(struct sftp_conn
*conn
, char *handle
, u_int handle_len
, int quiet
)
617 send_string_request(conn
->fd_out
, id
, SSH2_FXP_FSTAT
, handle
,
620 return(get_decode_stat(conn
->fd_in
, id
, quiet
));
625 do_setstat(struct sftp_conn
*conn
, char *path
, Attrib
*a
)
630 send_string_attrs_request(conn
->fd_out
, id
, SSH2_FXP_SETSTAT
, path
,
633 status
= get_status(conn
->fd_in
, id
);
634 if (status
!= SSH2_FX_OK
)
635 error("Couldn't setstat on \"%s\": %s", path
,
642 do_fsetstat(struct sftp_conn
*conn
, char *handle
, u_int handle_len
,
648 send_string_attrs_request(conn
->fd_out
, id
, SSH2_FXP_FSETSTAT
, handle
,
651 status
= get_status(conn
->fd_in
, id
);
652 if (status
!= SSH2_FX_OK
)
653 error("Couldn't fsetstat: %s", fx2txt(status
));
659 do_realpath(struct sftp_conn
*conn
, char *path
)
662 u_int type
, expected_id
, count
, id
;
663 char *filename
, *longname
;
666 expected_id
= id
= conn
->msg_id
++;
667 send_string_request(conn
->fd_out
, id
, SSH2_FXP_REALPATH
, path
,
672 get_msg(conn
->fd_in
, &msg
);
673 type
= buffer_get_char(&msg
);
674 id
= buffer_get_int(&msg
);
676 if (id
!= expected_id
)
677 fatal("ID mismatch (%u != %u)", id
, expected_id
);
679 if (type
== SSH2_FXP_STATUS
) {
680 u_int status
= buffer_get_int(&msg
);
682 error("Couldn't canonicalise: %s", fx2txt(status
));
684 } else if (type
!= SSH2_FXP_NAME
)
685 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
686 SSH2_FXP_NAME
, type
);
688 count
= buffer_get_int(&msg
);
690 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count
);
692 filename
= buffer_get_string(&msg
, NULL
);
693 longname
= buffer_get_string(&msg
, NULL
);
694 a
= decode_attrib(&msg
);
696 debug3("SSH_FXP_REALPATH %s -> %s", path
, filename
);
706 do_rename(struct sftp_conn
*conn
, char *oldpath
, char *newpath
)
713 /* Send rename request */
715 if ((conn
->exts
& SFTP_EXT_POSIX_RENAME
)) {
716 buffer_put_char(&msg
, SSH2_FXP_EXTENDED
);
717 buffer_put_int(&msg
, id
);
718 buffer_put_cstring(&msg
, "posix-rename@openssh.com");
720 buffer_put_char(&msg
, SSH2_FXP_RENAME
);
721 buffer_put_int(&msg
, id
);
723 buffer_put_cstring(&msg
, oldpath
);
724 buffer_put_cstring(&msg
, newpath
);
725 send_msg(conn
->fd_out
, &msg
);
726 debug3("Sent message %s \"%s\" -> \"%s\"",
727 (conn
->exts
& SFTP_EXT_POSIX_RENAME
) ? "posix-rename@openssh.com" :
728 "SSH2_FXP_RENAME", oldpath
, newpath
);
731 status
= get_status(conn
->fd_in
, id
);
732 if (status
!= SSH2_FX_OK
)
733 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath
,
734 newpath
, fx2txt(status
));
740 do_symlink(struct sftp_conn
*conn
, char *oldpath
, char *newpath
)
745 if (conn
->version
< 3) {
746 error("This server does not support the symlink operation");
747 return(SSH2_FX_OP_UNSUPPORTED
);
752 /* Send symlink request */
754 buffer_put_char(&msg
, SSH2_FXP_SYMLINK
);
755 buffer_put_int(&msg
, id
);
756 buffer_put_cstring(&msg
, oldpath
);
757 buffer_put_cstring(&msg
, newpath
);
758 send_msg(conn
->fd_out
, &msg
);
759 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath
,
763 status
= get_status(conn
->fd_in
, id
);
764 if (status
!= SSH2_FX_OK
)
765 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath
,
766 newpath
, fx2txt(status
));
773 do_readlink(struct sftp_conn
*conn
, char *path
)
776 u_int type
, expected_id
, count
, id
;
777 char *filename
, *longname
;
780 expected_id
= id
= conn
->msg_id
++;
781 send_string_request(conn
->fd_out
, id
, SSH2_FXP_READLINK
, path
,
786 get_msg(conn
->fd_in
, &msg
);
787 type
= buffer_get_char(&msg
);
788 id
= buffer_get_int(&msg
);
790 if (id
!= expected_id
)
791 fatal("ID mismatch (%u != %u)", id
, expected_id
);
793 if (type
== SSH2_FXP_STATUS
) {
794 u_int status
= buffer_get_int(&msg
);
796 error("Couldn't readlink: %s", fx2txt(status
));
798 } else if (type
!= SSH2_FXP_NAME
)
799 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
800 SSH2_FXP_NAME
, type
);
802 count
= buffer_get_int(&msg
);
804 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count
);
806 filename
= buffer_get_string(&msg
, NULL
);
807 longname
= buffer_get_string(&msg
, NULL
);
808 a
= decode_attrib(&msg
);
810 debug3("SSH_FXP_READLINK %s -> %s", path
, filename
);
821 do_statvfs(struct sftp_conn
*conn
, const char *path
, struct sftp_statvfs
*st
,
827 if ((conn
->exts
& SFTP_EXT_STATVFS
) == 0) {
828 error("Server does not support statvfs@openssh.com extension");
836 buffer_put_char(&msg
, SSH2_FXP_EXTENDED
);
837 buffer_put_int(&msg
, id
);
838 buffer_put_cstring(&msg
, "statvfs@openssh.com");
839 buffer_put_cstring(&msg
, path
);
840 send_msg(conn
->fd_out
, &msg
);
843 return get_decode_statvfs(conn
->fd_in
, st
, id
, quiet
);
848 do_fstatvfs(struct sftp_conn
*conn
, const char *handle
, u_int handle_len
,
849 struct sftp_statvfs
*st
, int quiet
)
854 if ((conn
->exts
& SFTP_EXT_FSTATVFS
) == 0) {
855 error("Server does not support fstatvfs@openssh.com extension");
863 buffer_put_char(&msg
, SSH2_FXP_EXTENDED
);
864 buffer_put_int(&msg
, id
);
865 buffer_put_cstring(&msg
, "fstatvfs@openssh.com");
866 buffer_put_string(&msg
, handle
, handle_len
);
867 send_msg(conn
->fd_out
, &msg
);
870 return get_decode_statvfs(conn
->fd_in
, st
, id
, quiet
);
875 send_read_request(int fd_out
, u_int id
, u_int64_t offset
, u_int len
,
876 char *handle
, u_int handle_len
)
882 buffer_put_char(&msg
, SSH2_FXP_READ
);
883 buffer_put_int(&msg
, id
);
884 buffer_put_string(&msg
, handle
, handle_len
);
885 buffer_put_int64(&msg
, offset
);
886 buffer_put_int(&msg
, len
);
887 send_msg(fd_out
, &msg
);
892 do_download(struct sftp_conn
*conn
, char *remote_path
, char *local_path
,
898 int local_fd
, status
= 0, write_error
;
899 int read_error
, write_errno
;
900 u_int64_t offset
, size
;
901 u_int handle_len
, mode
, type
, id
, buflen
, num_req
, max_req
;
902 off_t progress_counter
;
907 TAILQ_ENTRY(request
) tq
;
909 TAILQ_HEAD(reqhead
, request
) requests
;
913 TAILQ_INIT(&requests
);
915 a
= do_stat(conn
, remote_path
, 0);
919 /* Do not preserve set[ug]id here, as we do not preserve ownership */
920 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
)
921 mode
= a
->perm
& 0777;
925 if ((a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) &&
926 (!S_ISREG(a
->perm
))) {
927 error("Cannot download non-regular file: %s", remote_path
);
931 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
)
936 buflen
= conn
->transfer_buflen
;
939 /* Send open request */
941 buffer_put_char(&msg
, SSH2_FXP_OPEN
);
942 buffer_put_int(&msg
, id
);
943 buffer_put_cstring(&msg
, remote_path
);
944 buffer_put_int(&msg
, SSH2_FXF_READ
);
945 attrib_clear(&junk
); /* Send empty attributes */
946 encode_attrib(&msg
, &junk
);
947 send_msg(conn
->fd_out
, &msg
);
948 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id
, remote_path
);
950 handle
= get_handle(conn
->fd_in
, id
, &handle_len
);
951 if (handle
== NULL
) {
956 local_fd
= open(local_path
, O_WRONLY
| O_CREAT
| O_TRUNC
,
958 if (local_fd
== -1) {
959 error("Couldn't open local file \"%s\" for writing: %s",
960 local_path
, strerror(errno
));
961 do_close(conn
, handle
, handle_len
);
967 /* Read from remote and write to local */
968 write_error
= read_error
= write_errno
= num_req
= offset
= 0;
970 progress_counter
= 0;
972 if (showprogress
&& size
!= 0)
973 start_progress_meter(remote_path
, size
, &progress_counter
);
975 while (num_req
> 0 || max_req
> 0) {
980 * Simulate EOF on interrupt: stop sending new requests and
981 * allow outstanding requests to drain gracefully
984 if (num_req
== 0) /* If we haven't started yet... */
989 /* Send some more requests */
990 while (num_req
< max_req
) {
991 debug3("Request range %llu -> %llu (%d/%d)",
992 (unsigned long long)offset
,
993 (unsigned long long)offset
+ buflen
- 1,
995 req
= xmalloc(sizeof(*req
));
996 req
->id
= conn
->msg_id
++;
998 req
->offset
= offset
;
1001 TAILQ_INSERT_TAIL(&requests
, req
, tq
);
1002 send_read_request(conn
->fd_out
, req
->id
, req
->offset
,
1003 req
->len
, handle
, handle_len
);
1007 get_msg(conn
->fd_in
, &msg
);
1008 type
= buffer_get_char(&msg
);
1009 id
= buffer_get_int(&msg
);
1010 debug3("Received reply T:%u I:%u R:%d", type
, id
, max_req
);
1012 /* Find the request in our queue */
1013 for (req
= TAILQ_FIRST(&requests
);
1014 req
!= NULL
&& req
->id
!= id
;
1015 req
= TAILQ_NEXT(req
, tq
))
1018 fatal("Unexpected reply %u", id
);
1021 case SSH2_FXP_STATUS
:
1022 status
= buffer_get_int(&msg
);
1023 if (status
!= SSH2_FX_EOF
)
1026 TAILQ_REMOVE(&requests
, req
, tq
);
1031 data
= buffer_get_string(&msg
, &len
);
1032 debug3("Received data %llu -> %llu",
1033 (unsigned long long)req
->offset
,
1034 (unsigned long long)req
->offset
+ len
- 1);
1036 fatal("Received more data than asked for "
1037 "%u > %u", len
, req
->len
);
1038 if ((lseek(local_fd
, req
->offset
, SEEK_SET
) == -1 ||
1039 atomicio(vwrite
, local_fd
, data
, len
) != len
) &&
1041 write_errno
= errno
;
1045 progress_counter
+= len
;
1048 if (len
== req
->len
) {
1049 TAILQ_REMOVE(&requests
, req
, tq
);
1053 /* Resend the request for the missing data */
1054 debug3("Short data block, re-requesting "
1055 "%llu -> %llu (%2d)",
1056 (unsigned long long)req
->offset
+ len
,
1057 (unsigned long long)req
->offset
+
1058 req
->len
- 1, num_req
);
1059 req
->id
= conn
->msg_id
++;
1062 send_read_request(conn
->fd_out
, req
->id
,
1063 req
->offset
, req
->len
, handle
, handle_len
);
1064 /* Reduce the request size */
1066 buflen
= MAX(MIN_READ_SIZE
, len
);
1068 if (max_req
> 0) { /* max_req = 0 iff EOF received */
1069 if (size
> 0 && offset
> size
) {
1070 /* Only one request at a time
1071 * after the expected EOF */
1072 debug3("Finish at %llu (%2d)",
1073 (unsigned long long)offset
,
1076 } else if (max_req
<= conn
->num_requests
) {
1082 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1083 SSH2_FXP_DATA
, type
);
1087 if (showprogress
&& size
)
1088 stop_progress_meter();
1091 if (TAILQ_FIRST(&requests
) != NULL
)
1092 fatal("Transfer complete, but requests still in queue");
1095 error("Couldn't read from remote file \"%s\" : %s",
1096 remote_path
, fx2txt(status
));
1097 do_close(conn
, handle
, handle_len
);
1098 } else if (write_error
) {
1099 error("Couldn't write to \"%s\": %s", local_path
,
1100 strerror(write_errno
));
1102 do_close(conn
, handle
, handle_len
);
1104 status
= do_close(conn
, handle
, handle_len
);
1106 /* Override umask and utimes if asked */
1107 if (pflag
&& fchmod(local_fd
, mode
) == -1)
1108 error("Couldn't set mode on \"%s\": %s", local_path
,
1110 if (pflag
&& (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
)) {
1111 struct timeval tv
[2];
1112 tv
[0].tv_sec
= a
->atime
;
1113 tv
[1].tv_sec
= a
->mtime
;
1114 tv
[0].tv_usec
= tv
[1].tv_usec
= 0;
1115 if (utimes(local_path
, tv
) == -1)
1116 error("Can't set times on \"%s\": %s",
1117 local_path
, strerror(errno
));
1128 do_upload(struct sftp_conn
*conn
, char *local_path
, char *remote_path
,
1132 int status
= SSH2_FX_OK
;
1133 u_int handle_len
, id
, type
;
1135 char *handle
, *data
;
1141 struct outstanding_ack
{
1145 TAILQ_ENTRY(outstanding_ack
) tq
;
1147 TAILQ_HEAD(ackhead
, outstanding_ack
) acks
;
1148 struct outstanding_ack
*ack
= NULL
;
1152 if ((local_fd
= open(local_path
, O_RDONLY
, 0)) == -1) {
1153 error("Couldn't open local file \"%s\" for reading: %s",
1154 local_path
, strerror(errno
));
1157 if (fstat(local_fd
, &sb
) == -1) {
1158 error("Couldn't fstat local file \"%s\": %s",
1159 local_path
, strerror(errno
));
1163 if (!S_ISREG(sb
.st_mode
)) {
1164 error("%s is not a regular file", local_path
);
1168 stat_to_attrib(&sb
, &a
);
1170 a
.flags
&= ~SSH2_FILEXFER_ATTR_SIZE
;
1171 a
.flags
&= ~SSH2_FILEXFER_ATTR_UIDGID
;
1174 a
.flags
&= ~SSH2_FILEXFER_ATTR_ACMODTIME
;
1178 /* Send open request */
1179 id
= conn
->msg_id
++;
1180 buffer_put_char(&msg
, SSH2_FXP_OPEN
);
1181 buffer_put_int(&msg
, id
);
1182 buffer_put_cstring(&msg
, remote_path
);
1183 buffer_put_int(&msg
, SSH2_FXF_WRITE
|SSH2_FXF_CREAT
|SSH2_FXF_TRUNC
);
1184 encode_attrib(&msg
, &a
);
1185 send_msg(conn
->fd_out
, &msg
);
1186 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id
, remote_path
);
1190 handle
= get_handle(conn
->fd_in
, id
, &handle_len
);
1191 if (handle
== NULL
) {
1197 startid
= ackid
= id
+ 1;
1198 data
= xmalloc(conn
->transfer_buflen
);
1200 /* Read from local and write to remote */
1203 start_progress_meter(local_path
, sb
.st_size
, &offset
);
1209 * Can't use atomicio here because it returns 0 on EOF,
1210 * thus losing the last block of the file.
1211 * Simulate an EOF on interrupt, allowing ACKs from the
1214 if (interrupted
|| status
!= SSH2_FX_OK
)
1217 len
= read(local_fd
, data
, conn
->transfer_buflen
);
1218 while ((len
== -1) && (errno
== EINTR
|| errno
== EAGAIN
));
1221 fatal("Couldn't read from \"%s\": %s", local_path
,
1225 ack
= xmalloc(sizeof(*ack
));
1227 ack
->offset
= offset
;
1229 TAILQ_INSERT_TAIL(&acks
, ack
, tq
);
1232 buffer_put_char(&msg
, SSH2_FXP_WRITE
);
1233 buffer_put_int(&msg
, ack
->id
);
1234 buffer_put_string(&msg
, handle
, handle_len
);
1235 buffer_put_int64(&msg
, offset
);
1236 buffer_put_string(&msg
, data
, len
);
1237 send_msg(conn
->fd_out
, &msg
);
1238 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1239 id
, (unsigned long long)offset
, len
);
1240 } else if (TAILQ_FIRST(&acks
) == NULL
)
1244 fatal("Unexpected ACK %u", id
);
1246 if (id
== startid
|| len
== 0 ||
1247 id
- ackid
>= conn
->num_requests
) {
1251 get_msg(conn
->fd_in
, &msg
);
1252 type
= buffer_get_char(&msg
);
1253 r_id
= buffer_get_int(&msg
);
1255 if (type
!= SSH2_FXP_STATUS
)
1256 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1257 "got %d", SSH2_FXP_STATUS
, type
);
1259 status
= buffer_get_int(&msg
);
1260 debug3("SSH2_FXP_STATUS %d", status
);
1262 /* Find the request in our queue */
1263 for (ack
= TAILQ_FIRST(&acks
);
1264 ack
!= NULL
&& ack
->id
!= r_id
;
1265 ack
= TAILQ_NEXT(ack
, tq
))
1268 fatal("Can't find request for ID %u", r_id
);
1269 TAILQ_REMOVE(&acks
, ack
, tq
);
1270 debug3("In write loop, ack for %u %u bytes at %lld",
1271 ack
->id
, ack
->len
, (long long)ack
->offset
);
1277 fatal("%s: offset < 0", __func__
);
1282 stop_progress_meter();
1285 if (status
!= SSH2_FX_OK
) {
1286 error("Couldn't write to remote file \"%s\": %s",
1287 remote_path
, fx2txt(status
));
1291 if (close(local_fd
) == -1) {
1292 error("Couldn't close local file \"%s\": %s", local_path
,
1297 /* Override umask and utimes if asked */
1299 do_fsetstat(conn
, handle
, handle_len
, &a
);
1301 if (do_close(conn
, handle
, handle_len
) != SSH2_FX_OK
)