1 /* $OpenBSD: sftp-client.c,v 1.87 2009/06/22 05:39:28 dtucker 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 #include <sys/param.h>
27 #ifdef HAVE_SYS_STATVFS_H
28 #include <sys/statvfs.h>
30 #include "openbsd-compat/sys-queue.h"
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
34 #ifdef HAVE_SYS_TIME_H
35 # include <sys/time.h>
51 #include "progressmeter.h"
55 #include "sftp-common.h"
56 #include "sftp-client.h"
58 extern volatile sig_atomic_t interrupted
;
59 extern int showprogress
;
61 /* Minimum amount of data to read at a time */
62 #define MIN_READ_SIZE 512
67 u_int transfer_buflen
;
71 #define SFTP_EXT_POSIX_RENAME 0x00000001
72 #define SFTP_EXT_STATVFS 0x00000002
73 #define SFTP_EXT_FSTATVFS 0x00000004
78 send_msg(int fd
, Buffer
*m
)
83 if (buffer_len(m
) > SFTP_MAX_MSG_LENGTH
)
84 fatal("Outbound message too long %u", buffer_len(m
));
86 /* Send length first */
87 put_u32(mlen
, buffer_len(m
));
88 iov
[0].iov_base
= mlen
;
89 iov
[0].iov_len
= sizeof(mlen
);
90 iov
[1].iov_base
= buffer_ptr(m
);
91 iov
[1].iov_len
= buffer_len(m
);
93 if (atomiciov(writev
, fd
, iov
, 2) != buffer_len(m
) + sizeof(mlen
))
94 fatal("Couldn't send packet: %s", strerror(errno
));
100 get_msg(int fd
, Buffer
*m
)
104 buffer_append_space(m
, 4);
105 if (atomicio(read
, fd
, buffer_ptr(m
), 4) != 4) {
107 fatal("Connection closed");
109 fatal("Couldn't read packet: %s", strerror(errno
));
112 msg_len
= buffer_get_int(m
);
113 if (msg_len
> SFTP_MAX_MSG_LENGTH
)
114 fatal("Received message too long %u", msg_len
);
116 buffer_append_space(m
, msg_len
);
117 if (atomicio(read
, fd
, buffer_ptr(m
), msg_len
) != msg_len
) {
119 fatal("Connection closed");
121 fatal("Read packet: %s", strerror(errno
));
126 send_string_request(int fd
, u_int id
, u_int code
, char *s
,
132 buffer_put_char(&msg
, code
);
133 buffer_put_int(&msg
, id
);
134 buffer_put_string(&msg
, s
, len
);
136 debug3("Sent message fd %d T:%u I:%u", fd
, code
, id
);
141 send_string_attrs_request(int fd
, u_int id
, u_int code
, char *s
,
142 u_int len
, Attrib
*a
)
147 buffer_put_char(&msg
, code
);
148 buffer_put_int(&msg
, id
);
149 buffer_put_string(&msg
, s
, len
);
150 encode_attrib(&msg
, a
);
152 debug3("Sent message fd %d T:%u I:%u", fd
, code
, id
);
157 get_status(int fd
, u_int expected_id
)
160 u_int type
, id
, status
;
164 type
= buffer_get_char(&msg
);
165 id
= buffer_get_int(&msg
);
167 if (id
!= expected_id
)
168 fatal("ID mismatch (%u != %u)", id
, expected_id
);
169 if (type
!= SSH2_FXP_STATUS
)
170 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
171 SSH2_FXP_STATUS
, type
);
173 status
= buffer_get_int(&msg
);
176 debug3("SSH2_FXP_STATUS %u", status
);
182 get_handle(int fd
, u_int expected_id
, u_int
*len
)
190 type
= buffer_get_char(&msg
);
191 id
= buffer_get_int(&msg
);
193 if (id
!= expected_id
)
194 fatal("ID mismatch (%u != %u)", id
, expected_id
);
195 if (type
== SSH2_FXP_STATUS
) {
196 int status
= buffer_get_int(&msg
);
198 error("Couldn't get handle: %s", fx2txt(status
));
201 } else if (type
!= SSH2_FXP_HANDLE
)
202 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
203 SSH2_FXP_HANDLE
, type
);
205 handle
= buffer_get_string(&msg
, len
);
212 get_decode_stat(int fd
, u_int expected_id
, int quiet
)
221 type
= buffer_get_char(&msg
);
222 id
= buffer_get_int(&msg
);
224 debug3("Received stat reply T:%u I:%u", type
, id
);
225 if (id
!= expected_id
)
226 fatal("ID mismatch (%u != %u)", id
, expected_id
);
227 if (type
== SSH2_FXP_STATUS
) {
228 int status
= buffer_get_int(&msg
);
231 debug("Couldn't stat remote file: %s", fx2txt(status
));
233 error("Couldn't stat remote file: %s", fx2txt(status
));
236 } else if (type
!= SSH2_FXP_ATTRS
) {
237 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
238 SSH2_FXP_ATTRS
, type
);
240 a
= decode_attrib(&msg
);
247 get_decode_statvfs(int fd
, struct sftp_statvfs
*st
, u_int expected_id
,
251 u_int type
, id
, flag
;
256 type
= buffer_get_char(&msg
);
257 id
= buffer_get_int(&msg
);
259 debug3("Received statvfs reply T:%u I:%u", type
, id
);
260 if (id
!= expected_id
)
261 fatal("ID mismatch (%u != %u)", id
, expected_id
);
262 if (type
== SSH2_FXP_STATUS
) {
263 int status
= buffer_get_int(&msg
);
266 debug("Couldn't statvfs: %s", fx2txt(status
));
268 error("Couldn't statvfs: %s", fx2txt(status
));
271 } else if (type
!= SSH2_FXP_EXTENDED_REPLY
) {
272 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
273 SSH2_FXP_EXTENDED_REPLY
, type
);
276 bzero(st
, sizeof(*st
));
277 st
->f_bsize
= buffer_get_int64(&msg
);
278 st
->f_frsize
= buffer_get_int64(&msg
);
279 st
->f_blocks
= buffer_get_int64(&msg
);
280 st
->f_bfree
= buffer_get_int64(&msg
);
281 st
->f_bavail
= buffer_get_int64(&msg
);
282 st
->f_files
= buffer_get_int64(&msg
);
283 st
->f_ffree
= buffer_get_int64(&msg
);
284 st
->f_favail
= buffer_get_int64(&msg
);
285 st
->f_fsid
= buffer_get_int64(&msg
);
286 flag
= buffer_get_int64(&msg
);
287 st
->f_namemax
= buffer_get_int64(&msg
);
289 st
->f_flag
= (flag
& SSH2_FXE_STATVFS_ST_RDONLY
) ? ST_RDONLY
: 0;
290 st
->f_flag
|= (flag
& SSH2_FXE_STATVFS_ST_NOSUID
) ? ST_NOSUID
: 0;
298 do_init(int fd_in
, int fd_out
, u_int transfer_buflen
, u_int num_requests
)
300 u_int type
, exts
= 0;
303 struct sftp_conn
*ret
;
306 buffer_put_char(&msg
, SSH2_FXP_INIT
);
307 buffer_put_int(&msg
, SSH2_FILEXFER_VERSION
);
308 send_msg(fd_out
, &msg
);
312 get_msg(fd_in
, &msg
);
314 /* Expecting a VERSION reply */
315 if ((type
= buffer_get_char(&msg
)) != SSH2_FXP_VERSION
) {
316 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
321 version
= buffer_get_int(&msg
);
323 debug2("Remote version: %d", version
);
325 /* Check for extensions */
326 while (buffer_len(&msg
) > 0) {
327 char *name
= buffer_get_string(&msg
, NULL
);
328 char *value
= buffer_get_string(&msg
, NULL
);
331 if (strcmp(name
, "posix-rename@openssh.com") == 0 &&
332 strcmp(value
, "1") == 0) {
333 exts
|= SFTP_EXT_POSIX_RENAME
;
335 } else if (strcmp(name
, "statvfs@openssh.com") == 0 &&
336 strcmp(value
, "2") == 0) {
337 exts
|= SFTP_EXT_STATVFS
;
339 } if (strcmp(name
, "fstatvfs@openssh.com") == 0 &&
340 strcmp(value
, "2") == 0) {
341 exts
|= SFTP_EXT_FSTATVFS
;
345 debug2("Server supports extension \"%s\" revision %s",
348 debug2("Unrecognised server extension \"%s\"", name
);
356 ret
= xmalloc(sizeof(*ret
));
358 ret
->fd_out
= fd_out
;
359 ret
->transfer_buflen
= transfer_buflen
;
360 ret
->num_requests
= num_requests
;
361 ret
->version
= version
;
365 /* Some filexfer v.0 servers don't support large packets */
367 ret
->transfer_buflen
= MIN(ret
->transfer_buflen
, 20480);
373 sftp_proto_version(struct sftp_conn
*conn
)
375 return(conn
->version
);
379 do_close(struct sftp_conn
*conn
, char *handle
, u_int handle_len
)
387 buffer_put_char(&msg
, SSH2_FXP_CLOSE
);
388 buffer_put_int(&msg
, id
);
389 buffer_put_string(&msg
, handle
, handle_len
);
390 send_msg(conn
->fd_out
, &msg
);
391 debug3("Sent message SSH2_FXP_CLOSE I:%u", id
);
393 status
= get_status(conn
->fd_in
, id
);
394 if (status
!= SSH2_FX_OK
)
395 error("Couldn't close file: %s", fx2txt(status
));
404 do_lsreaddir(struct sftp_conn
*conn
, char *path
, int printflag
,
408 u_int count
, type
, id
, handle_len
, i
, expected_id
, ents
= 0;
414 buffer_put_char(&msg
, SSH2_FXP_OPENDIR
);
415 buffer_put_int(&msg
, id
);
416 buffer_put_cstring(&msg
, path
);
417 send_msg(conn
->fd_out
, &msg
);
421 handle
= get_handle(conn
->fd_in
, id
, &handle_len
);
427 *dir
= xmalloc(sizeof(**dir
));
431 for (; !interrupted
;) {
432 id
= expected_id
= conn
->msg_id
++;
434 debug3("Sending SSH2_FXP_READDIR I:%u", id
);
437 buffer_put_char(&msg
, SSH2_FXP_READDIR
);
438 buffer_put_int(&msg
, id
);
439 buffer_put_string(&msg
, handle
, handle_len
);
440 send_msg(conn
->fd_out
, &msg
);
444 get_msg(conn
->fd_in
, &msg
);
446 type
= buffer_get_char(&msg
);
447 id
= buffer_get_int(&msg
);
449 debug3("Received reply T:%u I:%u", type
, id
);
451 if (id
!= expected_id
)
452 fatal("ID mismatch (%u != %u)", id
, expected_id
);
454 if (type
== SSH2_FXP_STATUS
) {
455 int status
= buffer_get_int(&msg
);
457 debug3("Received SSH2_FXP_STATUS %d", status
);
459 if (status
== SSH2_FX_EOF
) {
462 error("Couldn't read directory: %s",
464 do_close(conn
, handle
, handle_len
);
468 } else if (type
!= SSH2_FXP_NAME
)
469 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
470 SSH2_FXP_NAME
, type
);
472 count
= buffer_get_int(&msg
);
475 debug3("Received %d SSH2_FXP_NAME responses", count
);
476 for (i
= 0; i
< count
; i
++) {
477 char *filename
, *longname
;
480 filename
= buffer_get_string(&msg
, NULL
);
481 longname
= buffer_get_string(&msg
, NULL
);
482 a
= decode_attrib(&msg
);
485 printf("%s\n", longname
);
488 *dir
= xrealloc(*dir
, ents
+ 2, sizeof(**dir
));
489 (*dir
)[ents
] = xmalloc(sizeof(***dir
));
490 (*dir
)[ents
]->filename
= xstrdup(filename
);
491 (*dir
)[ents
]->longname
= xstrdup(longname
);
492 memcpy(&(*dir
)[ents
]->a
, a
, sizeof(*a
));
493 (*dir
)[++ents
] = NULL
;
502 do_close(conn
, handle
, handle_len
);
505 /* Don't return partial matches on interrupt */
506 if (interrupted
&& dir
!= NULL
&& *dir
!= NULL
) {
507 free_sftp_dirents(*dir
);
508 *dir
= xmalloc(sizeof(**dir
));
516 do_readdir(struct sftp_conn
*conn
, char *path
, SFTP_DIRENT
***dir
)
518 return(do_lsreaddir(conn
, path
, 0, dir
));
521 void free_sftp_dirents(SFTP_DIRENT
**s
)
525 for (i
= 0; s
[i
]; i
++) {
526 xfree(s
[i
]->filename
);
527 xfree(s
[i
]->longname
);
534 do_rm(struct sftp_conn
*conn
, char *path
)
538 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path
);
541 send_string_request(conn
->fd_out
, id
, SSH2_FXP_REMOVE
, path
,
543 status
= get_status(conn
->fd_in
, id
);
544 if (status
!= SSH2_FX_OK
)
545 error("Couldn't delete file: %s", fx2txt(status
));
550 do_mkdir(struct sftp_conn
*conn
, char *path
, Attrib
*a
)
555 send_string_attrs_request(conn
->fd_out
, id
, SSH2_FXP_MKDIR
, path
,
558 status
= get_status(conn
->fd_in
, id
);
559 if (status
!= SSH2_FX_OK
)
560 error("Couldn't create directory: %s", fx2txt(status
));
566 do_rmdir(struct sftp_conn
*conn
, char *path
)
571 send_string_request(conn
->fd_out
, id
, SSH2_FXP_RMDIR
, path
,
574 status
= get_status(conn
->fd_in
, id
);
575 if (status
!= SSH2_FX_OK
)
576 error("Couldn't remove directory: %s", fx2txt(status
));
582 do_stat(struct sftp_conn
*conn
, char *path
, int quiet
)
588 send_string_request(conn
->fd_out
, id
,
589 conn
->version
== 0 ? SSH2_FXP_STAT_VERSION_0
: SSH2_FXP_STAT
,
592 return(get_decode_stat(conn
->fd_in
, id
, quiet
));
596 do_lstat(struct sftp_conn
*conn
, char *path
, int quiet
)
600 if (conn
->version
== 0) {
602 debug("Server version does not support lstat operation");
604 logit("Server version does not support lstat operation");
605 return(do_stat(conn
, path
, quiet
));
609 send_string_request(conn
->fd_out
, id
, SSH2_FXP_LSTAT
, path
,
612 return(get_decode_stat(conn
->fd_in
, id
, quiet
));
617 do_fstat(struct sftp_conn
*conn
, char *handle
, u_int handle_len
, int quiet
)
622 send_string_request(conn
->fd_out
, id
, SSH2_FXP_FSTAT
, handle
,
625 return(get_decode_stat(conn
->fd_in
, id
, quiet
));
630 do_setstat(struct sftp_conn
*conn
, char *path
, Attrib
*a
)
635 send_string_attrs_request(conn
->fd_out
, id
, SSH2_FXP_SETSTAT
, path
,
638 status
= get_status(conn
->fd_in
, id
);
639 if (status
!= SSH2_FX_OK
)
640 error("Couldn't setstat on \"%s\": %s", path
,
647 do_fsetstat(struct sftp_conn
*conn
, char *handle
, u_int handle_len
,
653 send_string_attrs_request(conn
->fd_out
, id
, SSH2_FXP_FSETSTAT
, handle
,
656 status
= get_status(conn
->fd_in
, id
);
657 if (status
!= SSH2_FX_OK
)
658 error("Couldn't fsetstat: %s", fx2txt(status
));
664 do_realpath(struct sftp_conn
*conn
, char *path
)
667 u_int type
, expected_id
, count
, id
;
668 char *filename
, *longname
;
671 expected_id
= id
= conn
->msg_id
++;
672 send_string_request(conn
->fd_out
, id
, SSH2_FXP_REALPATH
, path
,
677 get_msg(conn
->fd_in
, &msg
);
678 type
= buffer_get_char(&msg
);
679 id
= buffer_get_int(&msg
);
681 if (id
!= expected_id
)
682 fatal("ID mismatch (%u != %u)", id
, expected_id
);
684 if (type
== SSH2_FXP_STATUS
) {
685 u_int status
= buffer_get_int(&msg
);
687 error("Couldn't canonicalise: %s", fx2txt(status
));
689 } else if (type
!= SSH2_FXP_NAME
)
690 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
691 SSH2_FXP_NAME
, type
);
693 count
= buffer_get_int(&msg
);
695 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count
);
697 filename
= buffer_get_string(&msg
, NULL
);
698 longname
= buffer_get_string(&msg
, NULL
);
699 a
= decode_attrib(&msg
);
701 debug3("SSH_FXP_REALPATH %s -> %s", path
, filename
);
711 do_rename(struct sftp_conn
*conn
, char *oldpath
, char *newpath
)
718 /* Send rename request */
720 if ((conn
->exts
& SFTP_EXT_POSIX_RENAME
)) {
721 buffer_put_char(&msg
, SSH2_FXP_EXTENDED
);
722 buffer_put_int(&msg
, id
);
723 buffer_put_cstring(&msg
, "posix-rename@openssh.com");
725 buffer_put_char(&msg
, SSH2_FXP_RENAME
);
726 buffer_put_int(&msg
, id
);
728 buffer_put_cstring(&msg
, oldpath
);
729 buffer_put_cstring(&msg
, newpath
);
730 send_msg(conn
->fd_out
, &msg
);
731 debug3("Sent message %s \"%s\" -> \"%s\"",
732 (conn
->exts
& SFTP_EXT_POSIX_RENAME
) ? "posix-rename@openssh.com" :
733 "SSH2_FXP_RENAME", oldpath
, newpath
);
736 status
= get_status(conn
->fd_in
, id
);
737 if (status
!= SSH2_FX_OK
)
738 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath
,
739 newpath
, fx2txt(status
));
745 do_symlink(struct sftp_conn
*conn
, char *oldpath
, char *newpath
)
750 if (conn
->version
< 3) {
751 error("This server does not support the symlink operation");
752 return(SSH2_FX_OP_UNSUPPORTED
);
757 /* Send symlink request */
759 buffer_put_char(&msg
, SSH2_FXP_SYMLINK
);
760 buffer_put_int(&msg
, id
);
761 buffer_put_cstring(&msg
, oldpath
);
762 buffer_put_cstring(&msg
, newpath
);
763 send_msg(conn
->fd_out
, &msg
);
764 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath
,
768 status
= get_status(conn
->fd_in
, id
);
769 if (status
!= SSH2_FX_OK
)
770 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath
,
771 newpath
, fx2txt(status
));
778 do_readlink(struct sftp_conn
*conn
, char *path
)
781 u_int type
, expected_id
, count
, id
;
782 char *filename
, *longname
;
785 expected_id
= id
= conn
->msg_id
++;
786 send_string_request(conn
->fd_out
, id
, SSH2_FXP_READLINK
, path
,
791 get_msg(conn
->fd_in
, &msg
);
792 type
= buffer_get_char(&msg
);
793 id
= buffer_get_int(&msg
);
795 if (id
!= expected_id
)
796 fatal("ID mismatch (%u != %u)", id
, expected_id
);
798 if (type
== SSH2_FXP_STATUS
) {
799 u_int status
= buffer_get_int(&msg
);
801 error("Couldn't readlink: %s", fx2txt(status
));
803 } else if (type
!= SSH2_FXP_NAME
)
804 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
805 SSH2_FXP_NAME
, type
);
807 count
= buffer_get_int(&msg
);
809 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count
);
811 filename
= buffer_get_string(&msg
, NULL
);
812 longname
= buffer_get_string(&msg
, NULL
);
813 a
= decode_attrib(&msg
);
815 debug3("SSH_FXP_READLINK %s -> %s", path
, filename
);
826 do_statvfs(struct sftp_conn
*conn
, const char *path
, struct sftp_statvfs
*st
,
832 if ((conn
->exts
& SFTP_EXT_STATVFS
) == 0) {
833 error("Server does not support statvfs@openssh.com extension");
841 buffer_put_char(&msg
, SSH2_FXP_EXTENDED
);
842 buffer_put_int(&msg
, id
);
843 buffer_put_cstring(&msg
, "statvfs@openssh.com");
844 buffer_put_cstring(&msg
, path
);
845 send_msg(conn
->fd_out
, &msg
);
848 return get_decode_statvfs(conn
->fd_in
, st
, id
, quiet
);
853 do_fstatvfs(struct sftp_conn
*conn
, const char *handle
, u_int handle_len
,
854 struct sftp_statvfs
*st
, int quiet
)
859 if ((conn
->exts
& SFTP_EXT_FSTATVFS
) == 0) {
860 error("Server does not support fstatvfs@openssh.com extension");
868 buffer_put_char(&msg
, SSH2_FXP_EXTENDED
);
869 buffer_put_int(&msg
, id
);
870 buffer_put_cstring(&msg
, "fstatvfs@openssh.com");
871 buffer_put_string(&msg
, handle
, handle_len
);
872 send_msg(conn
->fd_out
, &msg
);
875 return get_decode_statvfs(conn
->fd_in
, st
, id
, quiet
);
880 send_read_request(int fd_out
, u_int id
, u_int64_t offset
, u_int len
,
881 char *handle
, u_int handle_len
)
887 buffer_put_char(&msg
, SSH2_FXP_READ
);
888 buffer_put_int(&msg
, id
);
889 buffer_put_string(&msg
, handle
, handle_len
);
890 buffer_put_int64(&msg
, offset
);
891 buffer_put_int(&msg
, len
);
892 send_msg(fd_out
, &msg
);
897 do_download(struct sftp_conn
*conn
, char *remote_path
, char *local_path
,
903 int local_fd
, status
= 0, write_error
;
904 int read_error
, write_errno
;
905 u_int64_t offset
, size
;
906 u_int handle_len
, mode
, type
, id
, buflen
, num_req
, max_req
;
907 off_t progress_counter
;
912 TAILQ_ENTRY(request
) tq
;
914 TAILQ_HEAD(reqhead
, request
) requests
;
917 TAILQ_INIT(&requests
);
919 a
= do_stat(conn
, remote_path
, 0);
923 /* Do not preserve set[ug]id here, as we do not preserve ownership */
924 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
)
925 mode
= a
->perm
& 0777;
929 if ((a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) &&
930 (!S_ISREG(a
->perm
))) {
931 error("Cannot download non-regular file: %s", remote_path
);
935 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
)
940 buflen
= conn
->transfer_buflen
;
943 /* Send open request */
945 buffer_put_char(&msg
, SSH2_FXP_OPEN
);
946 buffer_put_int(&msg
, id
);
947 buffer_put_cstring(&msg
, remote_path
);
948 buffer_put_int(&msg
, SSH2_FXF_READ
);
949 attrib_clear(&junk
); /* Send empty attributes */
950 encode_attrib(&msg
, &junk
);
951 send_msg(conn
->fd_out
, &msg
);
952 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id
, remote_path
);
954 handle
= get_handle(conn
->fd_in
, id
, &handle_len
);
955 if (handle
== NULL
) {
960 local_fd
= open(local_path
, O_WRONLY
| O_CREAT
| O_TRUNC
,
962 if (local_fd
== -1) {
963 error("Couldn't open local file \"%s\" for writing: %s",
964 local_path
, strerror(errno
));
965 do_close(conn
, handle
, handle_len
);
971 /* Read from remote and write to local */
972 write_error
= read_error
= write_errno
= num_req
= offset
= 0;
974 progress_counter
= 0;
976 if (showprogress
&& size
!= 0)
977 start_progress_meter(remote_path
, size
, &progress_counter
);
979 while (num_req
> 0 || max_req
> 0) {
984 * Simulate EOF on interrupt: stop sending new requests and
985 * allow outstanding requests to drain gracefully
988 if (num_req
== 0) /* If we haven't started yet... */
993 /* Send some more requests */
994 while (num_req
< max_req
) {
995 debug3("Request range %llu -> %llu (%d/%d)",
996 (unsigned long long)offset
,
997 (unsigned long long)offset
+ buflen
- 1,
999 req
= xmalloc(sizeof(*req
));
1000 req
->id
= conn
->msg_id
++;
1002 req
->offset
= offset
;
1005 TAILQ_INSERT_TAIL(&requests
, req
, tq
);
1006 send_read_request(conn
->fd_out
, req
->id
, req
->offset
,
1007 req
->len
, handle
, handle_len
);
1011 get_msg(conn
->fd_in
, &msg
);
1012 type
= buffer_get_char(&msg
);
1013 id
= buffer_get_int(&msg
);
1014 debug3("Received reply T:%u I:%u R:%d", type
, id
, max_req
);
1016 /* Find the request in our queue */
1017 for (req
= TAILQ_FIRST(&requests
);
1018 req
!= NULL
&& req
->id
!= id
;
1019 req
= TAILQ_NEXT(req
, tq
))
1022 fatal("Unexpected reply %u", id
);
1025 case SSH2_FXP_STATUS
:
1026 status
= buffer_get_int(&msg
);
1027 if (status
!= SSH2_FX_EOF
)
1030 TAILQ_REMOVE(&requests
, req
, tq
);
1035 data
= buffer_get_string(&msg
, &len
);
1036 debug3("Received data %llu -> %llu",
1037 (unsigned long long)req
->offset
,
1038 (unsigned long long)req
->offset
+ len
- 1);
1040 fatal("Received more data than asked for "
1041 "%u > %u", len
, req
->len
);
1042 if ((lseek(local_fd
, req
->offset
, SEEK_SET
) == -1 ||
1043 atomicio(vwrite
, local_fd
, data
, len
) != len
) &&
1045 write_errno
= errno
;
1049 progress_counter
+= len
;
1052 if (len
== req
->len
) {
1053 TAILQ_REMOVE(&requests
, req
, tq
);
1057 /* Resend the request for the missing data */
1058 debug3("Short data block, re-requesting "
1059 "%llu -> %llu (%2d)",
1060 (unsigned long long)req
->offset
+ len
,
1061 (unsigned long long)req
->offset
+
1062 req
->len
- 1, num_req
);
1063 req
->id
= conn
->msg_id
++;
1066 send_read_request(conn
->fd_out
, req
->id
,
1067 req
->offset
, req
->len
, handle
, handle_len
);
1068 /* Reduce the request size */
1070 buflen
= MAX(MIN_READ_SIZE
, len
);
1072 if (max_req
> 0) { /* max_req = 0 iff EOF received */
1073 if (size
> 0 && offset
> size
) {
1074 /* Only one request at a time
1075 * after the expected EOF */
1076 debug3("Finish at %llu (%2d)",
1077 (unsigned long long)offset
,
1080 } else if (max_req
<= conn
->num_requests
) {
1086 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1087 SSH2_FXP_DATA
, type
);
1091 if (showprogress
&& size
)
1092 stop_progress_meter();
1095 if (TAILQ_FIRST(&requests
) != NULL
)
1096 fatal("Transfer complete, but requests still in queue");
1099 error("Couldn't read from remote file \"%s\" : %s",
1100 remote_path
, fx2txt(status
));
1101 do_close(conn
, handle
, handle_len
);
1102 } else if (write_error
) {
1103 error("Couldn't write to \"%s\": %s", local_path
,
1104 strerror(write_errno
));
1106 do_close(conn
, handle
, handle_len
);
1108 status
= do_close(conn
, handle
, handle_len
);
1110 /* Override umask and utimes if asked */
1112 if (pflag
&& fchmod(local_fd
, mode
) == -1)
1114 if (pflag
&& chmod(local_path
, mode
) == -1)
1115 #endif /* HAVE_FCHMOD */
1116 error("Couldn't set mode on \"%s\": %s", local_path
,
1118 if (pflag
&& (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
)) {
1119 struct timeval tv
[2];
1120 tv
[0].tv_sec
= a
->atime
;
1121 tv
[1].tv_sec
= a
->mtime
;
1122 tv
[0].tv_usec
= tv
[1].tv_usec
= 0;
1123 if (utimes(local_path
, tv
) == -1)
1124 error("Can't set times on \"%s\": %s",
1125 local_path
, strerror(errno
));
1136 do_upload(struct sftp_conn
*conn
, char *local_path
, char *remote_path
,
1140 int status
= SSH2_FX_OK
;
1141 u_int handle_len
, id
, type
;
1143 char *handle
, *data
;
1149 struct outstanding_ack
{
1153 TAILQ_ENTRY(outstanding_ack
) tq
;
1155 TAILQ_HEAD(ackhead
, outstanding_ack
) acks
;
1156 struct outstanding_ack
*ack
= NULL
;
1160 if ((local_fd
= open(local_path
, O_RDONLY
, 0)) == -1) {
1161 error("Couldn't open local file \"%s\" for reading: %s",
1162 local_path
, strerror(errno
));
1165 if (fstat(local_fd
, &sb
) == -1) {
1166 error("Couldn't fstat local file \"%s\": %s",
1167 local_path
, strerror(errno
));
1171 if (!S_ISREG(sb
.st_mode
)) {
1172 error("%s is not a regular file", local_path
);
1176 stat_to_attrib(&sb
, &a
);
1178 a
.flags
&= ~SSH2_FILEXFER_ATTR_SIZE
;
1179 a
.flags
&= ~SSH2_FILEXFER_ATTR_UIDGID
;
1182 a
.flags
&= ~SSH2_FILEXFER_ATTR_ACMODTIME
;
1186 /* Send open request */
1187 id
= conn
->msg_id
++;
1188 buffer_put_char(&msg
, SSH2_FXP_OPEN
);
1189 buffer_put_int(&msg
, id
);
1190 buffer_put_cstring(&msg
, remote_path
);
1191 buffer_put_int(&msg
, SSH2_FXF_WRITE
|SSH2_FXF_CREAT
|SSH2_FXF_TRUNC
);
1192 encode_attrib(&msg
, &a
);
1193 send_msg(conn
->fd_out
, &msg
);
1194 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id
, remote_path
);
1198 handle
= get_handle(conn
->fd_in
, id
, &handle_len
);
1199 if (handle
== NULL
) {
1205 startid
= ackid
= id
+ 1;
1206 data
= xmalloc(conn
->transfer_buflen
);
1208 /* Read from local and write to remote */
1211 start_progress_meter(local_path
, sb
.st_size
, &offset
);
1217 * Can't use atomicio here because it returns 0 on EOF,
1218 * thus losing the last block of the file.
1219 * Simulate an EOF on interrupt, allowing ACKs from the
1222 if (interrupted
|| status
!= SSH2_FX_OK
)
1225 len
= read(local_fd
, data
, conn
->transfer_buflen
);
1226 while ((len
== -1) &&
1227 (errno
== EINTR
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
));
1230 fatal("Couldn't read from \"%s\": %s", local_path
,
1234 ack
= xmalloc(sizeof(*ack
));
1236 ack
->offset
= offset
;
1238 TAILQ_INSERT_TAIL(&acks
, ack
, tq
);
1241 buffer_put_char(&msg
, SSH2_FXP_WRITE
);
1242 buffer_put_int(&msg
, ack
->id
);
1243 buffer_put_string(&msg
, handle
, handle_len
);
1244 buffer_put_int64(&msg
, offset
);
1245 buffer_put_string(&msg
, data
, len
);
1246 send_msg(conn
->fd_out
, &msg
);
1247 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1248 id
, (unsigned long long)offset
, len
);
1249 } else if (TAILQ_FIRST(&acks
) == NULL
)
1253 fatal("Unexpected ACK %u", id
);
1255 if (id
== startid
|| len
== 0 ||
1256 id
- ackid
>= conn
->num_requests
) {
1260 get_msg(conn
->fd_in
, &msg
);
1261 type
= buffer_get_char(&msg
);
1262 r_id
= buffer_get_int(&msg
);
1264 if (type
!= SSH2_FXP_STATUS
)
1265 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1266 "got %d", SSH2_FXP_STATUS
, type
);
1268 status
= buffer_get_int(&msg
);
1269 debug3("SSH2_FXP_STATUS %d", status
);
1271 /* Find the request in our queue */
1272 for (ack
= TAILQ_FIRST(&acks
);
1273 ack
!= NULL
&& ack
->id
!= r_id
;
1274 ack
= TAILQ_NEXT(ack
, tq
))
1277 fatal("Can't find request for ID %u", r_id
);
1278 TAILQ_REMOVE(&acks
, ack
, tq
);
1279 debug3("In write loop, ack for %u %u bytes at %lld",
1280 ack
->id
, ack
->len
, (long long)ack
->offset
);
1286 fatal("%s: offset < 0", __func__
);
1291 stop_progress_meter();
1294 if (status
!= SSH2_FX_OK
) {
1295 error("Couldn't write to remote file \"%s\": %s",
1296 remote_path
, fx2txt(status
));
1300 if (close(local_fd
) == -1) {
1301 error("Couldn't close local file \"%s\": %s", local_path
,
1306 /* Override umask and utimes if asked */
1308 do_fsetstat(conn
, handle
, handle_len
, &a
);
1310 if (do_close(conn
, handle
, handle_len
) != SSH2_FX_OK
)