1 /* $OpenBSD: sftp-client.c,v 1.81 2008/03/23 12:54:01 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 #include <sys/param.h>
27 #include "openbsd-compat/sys-queue.h"
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
31 #ifdef HAVE_SYS_TIME_H
32 # include <sys/time.h>
48 #include "progressmeter.h"
52 #include "sftp-common.h"
53 #include "sftp-client.h"
55 extern volatile sig_atomic_t interrupted
;
56 extern int showprogress
;
58 /* Minimum amount of data to read at a time */
59 #define MIN_READ_SIZE 512
64 u_int transfer_buflen
;
68 #define SFTP_EXT_POSIX_RENAME 1
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 do_init(int fd_in
, int fd_out
, u_int transfer_buflen
, u_int num_requests
)
244 u_int type
, exts
= 0;
247 struct sftp_conn
*ret
;
250 buffer_put_char(&msg
, SSH2_FXP_INIT
);
251 buffer_put_int(&msg
, SSH2_FILEXFER_VERSION
);
252 send_msg(fd_out
, &msg
);
256 get_msg(fd_in
, &msg
);
258 /* Expecting a VERSION reply */
259 if ((type
= buffer_get_char(&msg
)) != SSH2_FXP_VERSION
) {
260 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
265 version
= buffer_get_int(&msg
);
267 debug2("Remote version: %d", version
);
269 /* Check for extensions */
270 while (buffer_len(&msg
) > 0) {
271 char *name
= buffer_get_string(&msg
, NULL
);
272 char *value
= buffer_get_string(&msg
, NULL
);
274 debug2("Init extension: \"%s\"", name
);
275 if (strcmp(name
, "posix-rename@openssh.com") == 0)
276 exts
|= SFTP_EXT_POSIX_RENAME
;
283 ret
= xmalloc(sizeof(*ret
));
285 ret
->fd_out
= fd_out
;
286 ret
->transfer_buflen
= transfer_buflen
;
287 ret
->num_requests
= num_requests
;
288 ret
->version
= version
;
292 /* Some filexfer v.0 servers don't support large packets */
294 ret
->transfer_buflen
= MIN(ret
->transfer_buflen
, 20480);
300 sftp_proto_version(struct sftp_conn
*conn
)
302 return(conn
->version
);
306 do_close(struct sftp_conn
*conn
, char *handle
, u_int handle_len
)
314 buffer_put_char(&msg
, SSH2_FXP_CLOSE
);
315 buffer_put_int(&msg
, id
);
316 buffer_put_string(&msg
, handle
, handle_len
);
317 send_msg(conn
->fd_out
, &msg
);
318 debug3("Sent message SSH2_FXP_CLOSE I:%u", id
);
320 status
= get_status(conn
->fd_in
, id
);
321 if (status
!= SSH2_FX_OK
)
322 error("Couldn't close file: %s", fx2txt(status
));
331 do_lsreaddir(struct sftp_conn
*conn
, char *path
, int printflag
,
335 u_int count
, type
, id
, handle_len
, i
, expected_id
, ents
= 0;
341 buffer_put_char(&msg
, SSH2_FXP_OPENDIR
);
342 buffer_put_int(&msg
, id
);
343 buffer_put_cstring(&msg
, path
);
344 send_msg(conn
->fd_out
, &msg
);
348 handle
= get_handle(conn
->fd_in
, id
, &handle_len
);
354 *dir
= xmalloc(sizeof(**dir
));
358 for (; !interrupted
;) {
359 id
= expected_id
= conn
->msg_id
++;
361 debug3("Sending SSH2_FXP_READDIR I:%u", id
);
364 buffer_put_char(&msg
, SSH2_FXP_READDIR
);
365 buffer_put_int(&msg
, id
);
366 buffer_put_string(&msg
, handle
, handle_len
);
367 send_msg(conn
->fd_out
, &msg
);
371 get_msg(conn
->fd_in
, &msg
);
373 type
= buffer_get_char(&msg
);
374 id
= buffer_get_int(&msg
);
376 debug3("Received reply T:%u I:%u", type
, id
);
378 if (id
!= expected_id
)
379 fatal("ID mismatch (%u != %u)", id
, expected_id
);
381 if (type
== SSH2_FXP_STATUS
) {
382 int status
= buffer_get_int(&msg
);
384 debug3("Received SSH2_FXP_STATUS %d", status
);
386 if (status
== SSH2_FX_EOF
) {
389 error("Couldn't read directory: %s",
391 do_close(conn
, handle
, handle_len
);
395 } else if (type
!= SSH2_FXP_NAME
)
396 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
397 SSH2_FXP_NAME
, type
);
399 count
= buffer_get_int(&msg
);
402 debug3("Received %d SSH2_FXP_NAME responses", count
);
403 for (i
= 0; i
< count
; i
++) {
404 char *filename
, *longname
;
407 filename
= buffer_get_string(&msg
, NULL
);
408 longname
= buffer_get_string(&msg
, NULL
);
409 a
= decode_attrib(&msg
);
412 printf("%s\n", longname
);
415 *dir
= xrealloc(*dir
, ents
+ 2, sizeof(**dir
));
416 (*dir
)[ents
] = xmalloc(sizeof(***dir
));
417 (*dir
)[ents
]->filename
= xstrdup(filename
);
418 (*dir
)[ents
]->longname
= xstrdup(longname
);
419 memcpy(&(*dir
)[ents
]->a
, a
, sizeof(*a
));
420 (*dir
)[++ents
] = NULL
;
429 do_close(conn
, handle
, handle_len
);
432 /* Don't return partial matches on interrupt */
433 if (interrupted
&& dir
!= NULL
&& *dir
!= NULL
) {
434 free_sftp_dirents(*dir
);
435 *dir
= xmalloc(sizeof(**dir
));
443 do_readdir(struct sftp_conn
*conn
, char *path
, SFTP_DIRENT
***dir
)
445 return(do_lsreaddir(conn
, path
, 0, dir
));
448 void free_sftp_dirents(SFTP_DIRENT
**s
)
452 for (i
= 0; s
[i
]; i
++) {
453 xfree(s
[i
]->filename
);
454 xfree(s
[i
]->longname
);
461 do_rm(struct sftp_conn
*conn
, char *path
)
465 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path
);
468 send_string_request(conn
->fd_out
, id
, SSH2_FXP_REMOVE
, path
,
470 status
= get_status(conn
->fd_in
, id
);
471 if (status
!= SSH2_FX_OK
)
472 error("Couldn't delete file: %s", fx2txt(status
));
477 do_mkdir(struct sftp_conn
*conn
, char *path
, Attrib
*a
)
482 send_string_attrs_request(conn
->fd_out
, id
, SSH2_FXP_MKDIR
, path
,
485 status
= get_status(conn
->fd_in
, id
);
486 if (status
!= SSH2_FX_OK
)
487 error("Couldn't create directory: %s", fx2txt(status
));
493 do_rmdir(struct sftp_conn
*conn
, char *path
)
498 send_string_request(conn
->fd_out
, id
, SSH2_FXP_RMDIR
, path
,
501 status
= get_status(conn
->fd_in
, id
);
502 if (status
!= SSH2_FX_OK
)
503 error("Couldn't remove directory: %s", fx2txt(status
));
509 do_stat(struct sftp_conn
*conn
, char *path
, int quiet
)
515 send_string_request(conn
->fd_out
, id
,
516 conn
->version
== 0 ? SSH2_FXP_STAT_VERSION_0
: SSH2_FXP_STAT
,
519 return(get_decode_stat(conn
->fd_in
, id
, quiet
));
523 do_lstat(struct sftp_conn
*conn
, char *path
, int quiet
)
527 if (conn
->version
== 0) {
529 debug("Server version does not support lstat operation");
531 logit("Server version does not support lstat operation");
532 return(do_stat(conn
, path
, quiet
));
536 send_string_request(conn
->fd_out
, id
, SSH2_FXP_LSTAT
, path
,
539 return(get_decode_stat(conn
->fd_in
, id
, quiet
));
544 do_fstat(struct sftp_conn
*conn
, char *handle
, u_int handle_len
, int quiet
)
549 send_string_request(conn
->fd_out
, id
, SSH2_FXP_FSTAT
, handle
,
552 return(get_decode_stat(conn
->fd_in
, id
, quiet
));
557 do_setstat(struct sftp_conn
*conn
, char *path
, Attrib
*a
)
562 send_string_attrs_request(conn
->fd_out
, id
, SSH2_FXP_SETSTAT
, path
,
565 status
= get_status(conn
->fd_in
, id
);
566 if (status
!= SSH2_FX_OK
)
567 error("Couldn't setstat on \"%s\": %s", path
,
574 do_fsetstat(struct sftp_conn
*conn
, char *handle
, u_int handle_len
,
580 send_string_attrs_request(conn
->fd_out
, id
, SSH2_FXP_FSETSTAT
, handle
,
583 status
= get_status(conn
->fd_in
, id
);
584 if (status
!= SSH2_FX_OK
)
585 error("Couldn't fsetstat: %s", fx2txt(status
));
591 do_realpath(struct sftp_conn
*conn
, char *path
)
594 u_int type
, expected_id
, count
, id
;
595 char *filename
, *longname
;
598 expected_id
= id
= conn
->msg_id
++;
599 send_string_request(conn
->fd_out
, id
, SSH2_FXP_REALPATH
, path
,
604 get_msg(conn
->fd_in
, &msg
);
605 type
= buffer_get_char(&msg
);
606 id
= buffer_get_int(&msg
);
608 if (id
!= expected_id
)
609 fatal("ID mismatch (%u != %u)", id
, expected_id
);
611 if (type
== SSH2_FXP_STATUS
) {
612 u_int status
= buffer_get_int(&msg
);
614 error("Couldn't canonicalise: %s", fx2txt(status
));
616 } else if (type
!= SSH2_FXP_NAME
)
617 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
618 SSH2_FXP_NAME
, type
);
620 count
= buffer_get_int(&msg
);
622 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count
);
624 filename
= buffer_get_string(&msg
, NULL
);
625 longname
= buffer_get_string(&msg
, NULL
);
626 a
= decode_attrib(&msg
);
628 debug3("SSH_FXP_REALPATH %s -> %s", path
, filename
);
638 do_rename(struct sftp_conn
*conn
, char *oldpath
, char *newpath
)
645 /* Send rename request */
647 if ((conn
->exts
& SFTP_EXT_POSIX_RENAME
)) {
648 buffer_put_char(&msg
, SSH2_FXP_EXTENDED
);
649 buffer_put_int(&msg
, id
);
650 buffer_put_cstring(&msg
, "posix-rename@openssh.com");
652 buffer_put_char(&msg
, SSH2_FXP_RENAME
);
653 buffer_put_int(&msg
, id
);
655 buffer_put_cstring(&msg
, oldpath
);
656 buffer_put_cstring(&msg
, newpath
);
657 send_msg(conn
->fd_out
, &msg
);
658 debug3("Sent message %s \"%s\" -> \"%s\"",
659 (conn
->exts
& SFTP_EXT_POSIX_RENAME
) ? "posix-rename@openssh.com" :
660 "SSH2_FXP_RENAME", oldpath
, newpath
);
663 status
= get_status(conn
->fd_in
, id
);
664 if (status
!= SSH2_FX_OK
)
665 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath
,
666 newpath
, fx2txt(status
));
672 do_symlink(struct sftp_conn
*conn
, char *oldpath
, char *newpath
)
677 if (conn
->version
< 3) {
678 error("This server does not support the symlink operation");
679 return(SSH2_FX_OP_UNSUPPORTED
);
684 /* Send symlink request */
686 buffer_put_char(&msg
, SSH2_FXP_SYMLINK
);
687 buffer_put_int(&msg
, id
);
688 buffer_put_cstring(&msg
, oldpath
);
689 buffer_put_cstring(&msg
, newpath
);
690 send_msg(conn
->fd_out
, &msg
);
691 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath
,
695 status
= get_status(conn
->fd_in
, id
);
696 if (status
!= SSH2_FX_OK
)
697 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath
,
698 newpath
, fx2txt(status
));
705 do_readlink(struct sftp_conn
*conn
, char *path
)
708 u_int type
, expected_id
, count
, id
;
709 char *filename
, *longname
;
712 expected_id
= id
= conn
->msg_id
++;
713 send_string_request(conn
->fd_out
, id
, SSH2_FXP_READLINK
, path
,
718 get_msg(conn
->fd_in
, &msg
);
719 type
= buffer_get_char(&msg
);
720 id
= buffer_get_int(&msg
);
722 if (id
!= expected_id
)
723 fatal("ID mismatch (%u != %u)", id
, expected_id
);
725 if (type
== SSH2_FXP_STATUS
) {
726 u_int status
= buffer_get_int(&msg
);
728 error("Couldn't readlink: %s", fx2txt(status
));
730 } else if (type
!= SSH2_FXP_NAME
)
731 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
732 SSH2_FXP_NAME
, type
);
734 count
= buffer_get_int(&msg
);
736 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count
);
738 filename
= buffer_get_string(&msg
, NULL
);
739 longname
= buffer_get_string(&msg
, NULL
);
740 a
= decode_attrib(&msg
);
742 debug3("SSH_FXP_READLINK %s -> %s", path
, filename
);
753 send_read_request(int fd_out
, u_int id
, u_int64_t offset
, u_int len
,
754 char *handle
, u_int handle_len
)
760 buffer_put_char(&msg
, SSH2_FXP_READ
);
761 buffer_put_int(&msg
, id
);
762 buffer_put_string(&msg
, handle
, handle_len
);
763 buffer_put_int64(&msg
, offset
);
764 buffer_put_int(&msg
, len
);
765 send_msg(fd_out
, &msg
);
770 do_download(struct sftp_conn
*conn
, char *remote_path
, char *local_path
,
776 int local_fd
, status
= 0, write_error
;
777 int read_error
, write_errno
;
778 u_int64_t offset
, size
;
779 u_int handle_len
, mode
, type
, id
, buflen
, num_req
, max_req
;
780 off_t progress_counter
;
785 TAILQ_ENTRY(request
) tq
;
787 TAILQ_HEAD(reqhead
, request
) requests
;
790 TAILQ_INIT(&requests
);
792 a
= do_stat(conn
, remote_path
, 0);
796 /* XXX: should we preserve set[ug]id? */
797 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
)
798 mode
= a
->perm
& 0777;
802 if ((a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) &&
803 (!S_ISREG(a
->perm
))) {
804 error("Cannot download non-regular file: %s", remote_path
);
808 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
)
813 buflen
= conn
->transfer_buflen
;
816 /* Send open request */
818 buffer_put_char(&msg
, SSH2_FXP_OPEN
);
819 buffer_put_int(&msg
, id
);
820 buffer_put_cstring(&msg
, remote_path
);
821 buffer_put_int(&msg
, SSH2_FXF_READ
);
822 attrib_clear(&junk
); /* Send empty attributes */
823 encode_attrib(&msg
, &junk
);
824 send_msg(conn
->fd_out
, &msg
);
825 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id
, remote_path
);
827 handle
= get_handle(conn
->fd_in
, id
, &handle_len
);
828 if (handle
== NULL
) {
833 local_fd
= open(local_path
, O_WRONLY
| O_CREAT
| O_TRUNC
,
835 if (local_fd
== -1) {
836 error("Couldn't open local file \"%s\" for writing: %s",
837 local_path
, strerror(errno
));
838 do_close(conn
, handle
, handle_len
);
844 /* Read from remote and write to local */
845 write_error
= read_error
= write_errno
= num_req
= offset
= 0;
847 progress_counter
= 0;
849 if (showprogress
&& size
!= 0)
850 start_progress_meter(remote_path
, size
, &progress_counter
);
852 while (num_req
> 0 || max_req
> 0) {
857 * Simulate EOF on interrupt: stop sending new requests and
858 * allow outstanding requests to drain gracefully
861 if (num_req
== 0) /* If we haven't started yet... */
866 /* Send some more requests */
867 while (num_req
< max_req
) {
868 debug3("Request range %llu -> %llu (%d/%d)",
869 (unsigned long long)offset
,
870 (unsigned long long)offset
+ buflen
- 1,
872 req
= xmalloc(sizeof(*req
));
873 req
->id
= conn
->msg_id
++;
875 req
->offset
= offset
;
878 TAILQ_INSERT_TAIL(&requests
, req
, tq
);
879 send_read_request(conn
->fd_out
, req
->id
, req
->offset
,
880 req
->len
, handle
, handle_len
);
884 get_msg(conn
->fd_in
, &msg
);
885 type
= buffer_get_char(&msg
);
886 id
= buffer_get_int(&msg
);
887 debug3("Received reply T:%u I:%u R:%d", type
, id
, max_req
);
889 /* Find the request in our queue */
890 for (req
= TAILQ_FIRST(&requests
);
891 req
!= NULL
&& req
->id
!= id
;
892 req
= TAILQ_NEXT(req
, tq
))
895 fatal("Unexpected reply %u", id
);
898 case SSH2_FXP_STATUS
:
899 status
= buffer_get_int(&msg
);
900 if (status
!= SSH2_FX_EOF
)
903 TAILQ_REMOVE(&requests
, req
, tq
);
908 data
= buffer_get_string(&msg
, &len
);
909 debug3("Received data %llu -> %llu",
910 (unsigned long long)req
->offset
,
911 (unsigned long long)req
->offset
+ len
- 1);
913 fatal("Received more data than asked for "
914 "%u > %u", len
, req
->len
);
915 if ((lseek(local_fd
, req
->offset
, SEEK_SET
) == -1 ||
916 atomicio(vwrite
, local_fd
, data
, len
) != len
) &&
922 progress_counter
+= len
;
925 if (len
== req
->len
) {
926 TAILQ_REMOVE(&requests
, req
, tq
);
930 /* Resend the request for the missing data */
931 debug3("Short data block, re-requesting "
932 "%llu -> %llu (%2d)",
933 (unsigned long long)req
->offset
+ len
,
934 (unsigned long long)req
->offset
+
935 req
->len
- 1, num_req
);
936 req
->id
= conn
->msg_id
++;
939 send_read_request(conn
->fd_out
, req
->id
,
940 req
->offset
, req
->len
, handle
, handle_len
);
941 /* Reduce the request size */
943 buflen
= MAX(MIN_READ_SIZE
, len
);
945 if (max_req
> 0) { /* max_req = 0 iff EOF received */
946 if (size
> 0 && offset
> size
) {
947 /* Only one request at a time
948 * after the expected EOF */
949 debug3("Finish at %llu (%2d)",
950 (unsigned long long)offset
,
953 } else if (max_req
<= conn
->num_requests
) {
959 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
960 SSH2_FXP_DATA
, type
);
964 if (showprogress
&& size
)
965 stop_progress_meter();
968 if (TAILQ_FIRST(&requests
) != NULL
)
969 fatal("Transfer complete, but requests still in queue");
972 error("Couldn't read from remote file \"%s\" : %s",
973 remote_path
, fx2txt(status
));
974 do_close(conn
, handle
, handle_len
);
975 } else if (write_error
) {
976 error("Couldn't write to \"%s\": %s", local_path
,
977 strerror(write_errno
));
979 do_close(conn
, handle
, handle_len
);
981 status
= do_close(conn
, handle
, handle_len
);
983 /* Override umask and utimes if asked */
985 if (pflag
&& fchmod(local_fd
, mode
) == -1)
987 if (pflag
&& chmod(local_path
, mode
) == -1)
988 #endif /* HAVE_FCHMOD */
989 error("Couldn't set mode on \"%s\": %s", local_path
,
991 if (pflag
&& (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
)) {
992 struct timeval tv
[2];
993 tv
[0].tv_sec
= a
->atime
;
994 tv
[1].tv_sec
= a
->mtime
;
995 tv
[0].tv_usec
= tv
[1].tv_usec
= 0;
996 if (utimes(local_path
, tv
) == -1)
997 error("Can't set times on \"%s\": %s",
998 local_path
, strerror(errno
));
1009 do_upload(struct sftp_conn
*conn
, char *local_path
, char *remote_path
,
1013 int status
= SSH2_FX_OK
;
1014 u_int handle_len
, id
, type
;
1016 char *handle
, *data
;
1022 struct outstanding_ack
{
1026 TAILQ_ENTRY(outstanding_ack
) tq
;
1028 TAILQ_HEAD(ackhead
, outstanding_ack
) acks
;
1029 struct outstanding_ack
*ack
= NULL
;
1033 if ((local_fd
= open(local_path
, O_RDONLY
, 0)) == -1) {
1034 error("Couldn't open local file \"%s\" for reading: %s",
1035 local_path
, strerror(errno
));
1038 if (fstat(local_fd
, &sb
) == -1) {
1039 error("Couldn't fstat local file \"%s\": %s",
1040 local_path
, strerror(errno
));
1044 if (!S_ISREG(sb
.st_mode
)) {
1045 error("%s is not a regular file", local_path
);
1049 stat_to_attrib(&sb
, &a
);
1051 a
.flags
&= ~SSH2_FILEXFER_ATTR_SIZE
;
1052 a
.flags
&= ~SSH2_FILEXFER_ATTR_UIDGID
;
1055 a
.flags
&= ~SSH2_FILEXFER_ATTR_ACMODTIME
;
1059 /* Send open request */
1060 id
= conn
->msg_id
++;
1061 buffer_put_char(&msg
, SSH2_FXP_OPEN
);
1062 buffer_put_int(&msg
, id
);
1063 buffer_put_cstring(&msg
, remote_path
);
1064 buffer_put_int(&msg
, SSH2_FXF_WRITE
|SSH2_FXF_CREAT
|SSH2_FXF_TRUNC
);
1065 encode_attrib(&msg
, &a
);
1066 send_msg(conn
->fd_out
, &msg
);
1067 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id
, remote_path
);
1071 handle
= get_handle(conn
->fd_in
, id
, &handle_len
);
1072 if (handle
== NULL
) {
1078 startid
= ackid
= id
+ 1;
1079 data
= xmalloc(conn
->transfer_buflen
);
1081 /* Read from local and write to remote */
1084 start_progress_meter(local_path
, sb
.st_size
, &offset
);
1090 * Can't use atomicio here because it returns 0 on EOF,
1091 * thus losing the last block of the file.
1092 * Simulate an EOF on interrupt, allowing ACKs from the
1095 if (interrupted
|| status
!= SSH2_FX_OK
)
1098 len
= read(local_fd
, data
, conn
->transfer_buflen
);
1099 while ((len
== -1) && (errno
== EINTR
|| errno
== EAGAIN
));
1102 fatal("Couldn't read from \"%s\": %s", local_path
,
1106 ack
= xmalloc(sizeof(*ack
));
1108 ack
->offset
= offset
;
1110 TAILQ_INSERT_TAIL(&acks
, ack
, tq
);
1113 buffer_put_char(&msg
, SSH2_FXP_WRITE
);
1114 buffer_put_int(&msg
, ack
->id
);
1115 buffer_put_string(&msg
, handle
, handle_len
);
1116 buffer_put_int64(&msg
, offset
);
1117 buffer_put_string(&msg
, data
, len
);
1118 send_msg(conn
->fd_out
, &msg
);
1119 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1120 id
, (unsigned long long)offset
, len
);
1121 } else if (TAILQ_FIRST(&acks
) == NULL
)
1125 fatal("Unexpected ACK %u", id
);
1127 if (id
== startid
|| len
== 0 ||
1128 id
- ackid
>= conn
->num_requests
) {
1132 get_msg(conn
->fd_in
, &msg
);
1133 type
= buffer_get_char(&msg
);
1134 r_id
= buffer_get_int(&msg
);
1136 if (type
!= SSH2_FXP_STATUS
)
1137 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1138 "got %d", SSH2_FXP_STATUS
, type
);
1140 status
= buffer_get_int(&msg
);
1141 debug3("SSH2_FXP_STATUS %d", status
);
1143 /* Find the request in our queue */
1144 for (ack
= TAILQ_FIRST(&acks
);
1145 ack
!= NULL
&& ack
->id
!= r_id
;
1146 ack
= TAILQ_NEXT(ack
, tq
))
1149 fatal("Can't find request for ID %u", r_id
);
1150 TAILQ_REMOVE(&acks
, ack
, tq
);
1151 debug3("In write loop, ack for %u %u bytes at %lld",
1152 ack
->id
, ack
->len
, (long long)ack
->offset
);
1158 fatal("%s: offset < 0", __func__
);
1163 stop_progress_meter();
1166 if (status
!= SSH2_FX_OK
) {
1167 error("Couldn't write to remote file \"%s\": %s",
1168 remote_path
, fx2txt(status
));
1172 if (close(local_fd
) == -1) {
1173 error("Couldn't close local file \"%s\": %s", local_path
,
1178 /* Override umask and utimes if asked */
1180 do_fsetstat(conn
, handle
, handle_len
, &a
);
1182 if (do_close(conn
, handle
, handle_len
) != SSH2_FX_OK
)