1 /* $OpenBSD: sftp-client.c,v 1.84 2008/06/08 20:15:29 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);
330 debug2("Init extension: \"%s\"", name);
331 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
332 strcmp(value, "1") == 0)
333 exts |= SFTP_EXT_POSIX_RENAME;
334 if (strcmp(name, "statvfs@openssh.com") == 0 &&
335 strcmp(value, "2") == 0)
336 exts |= SFTP_EXT_STATVFS;
337 if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
338 strcmp(value, "2") == 0)
339 exts |= SFTP_EXT_FSTATVFS;
346 ret = xmalloc(sizeof(*ret));
348 ret->fd_out = fd_out;
349 ret->transfer_buflen = transfer_buflen;
350 ret->num_requests = num_requests;
351 ret->version = version;
355 /* Some filexfer v.0 servers don't support large packets */
357 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
363 sftp_proto_version(struct sftp_conn *conn)
365 return(conn->version);
369 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
377 buffer_put_char(&msg, SSH2_FXP_CLOSE);
378 buffer_put_int(&msg, id);
379 buffer_put_string(&msg, handle, handle_len);
380 send_msg(conn->fd_out, &msg);
381 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
383 status = get_status(conn->fd_in, id);
384 if (status != SSH2_FX_OK)
385 error("Couldn't close file: %s", fx2txt(status));
394 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
398 u_int count, type, id, handle_len, i, expected_id, ents = 0;
404 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
405 buffer_put_int(&msg, id);
406 buffer_put_cstring(&msg, path);
407 send_msg(conn->fd_out, &msg);
411 handle = get_handle(conn->fd_in, id, &handle_len);
417 *dir = xmalloc(sizeof(**dir));
421 for (; !interrupted;) {
422 id = expected_id = conn->msg_id++;
424 debug3("Sending SSH2_FXP_READDIR I:%u", id);
427 buffer_put_char(&msg, SSH2_FXP_READDIR);
428 buffer_put_int(&msg, id);
429 buffer_put_string(&msg, handle, handle_len);
430 send_msg(conn->fd_out, &msg);
434 get_msg(conn->fd_in, &msg);
436 type = buffer_get_char(&msg);
437 id = buffer_get_int(&msg);
439 debug3("Received reply T:%u I:%u", type, id);
441 if (id != expected_id)
442 fatal("ID mismatch (%u != %u)", id, expected_id);
444 if (type == SSH2_FXP_STATUS) {
445 int status = buffer_get_int(&msg);
447 debug3("Received SSH2_FXP_STATUS %d", status);
449 if (status == SSH2_FX_EOF) {
452 error("Couldn't read directory: %s",
454 do_close(conn, handle, handle_len);
458 } else if (type != SSH2_FXP_NAME)
459 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
460 SSH2_FXP_NAME, type);
462 count = buffer_get_int(&msg);
465 debug3("Received %d SSH2_FXP_NAME responses", count);
466 for (i = 0; i < count; i++) {
467 char *filename, *longname;
470 filename = buffer_get_string(&msg, NULL);
471 longname = buffer_get_string(&msg, NULL);
472 a = decode_attrib(&msg);
475 printf("%s\n", longname);
478 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
479 (*dir)[ents] = xmalloc(sizeof(***dir));
480 (*dir)[ents]->filename = xstrdup(filename);
481 (*dir)[ents]->longname = xstrdup(longname);
482 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
483 (*dir)[++ents] = NULL;
492 do_close(conn, handle, handle_len);
495 /* Don't return partial matches on interrupt */
496 if (interrupted && dir != NULL && *dir != NULL) {
497 free_sftp_dirents(*dir);
498 *dir = xmalloc(sizeof(**dir));
506 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
508 return(do_lsreaddir(conn, path, 0, dir));
511 void free_sftp_dirents(SFTP_DIRENT **s)
515 for (i = 0; s[i]; i++) {
516 xfree(s[i]->filename);
517 xfree(s[i]->longname);
524 do_rm(struct sftp_conn *conn, char *path)
528 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
531 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
533 status = get_status(conn->fd_in, id);
534 if (status != SSH2_FX_OK)
535 error("Couldn't delete file: %s", fx2txt(status));
540 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
545 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
548 status = get_status(conn->fd_in, id);
549 if (status != SSH2_FX_OK)
550 error("Couldn't create directory: %s", fx2txt(status));
556 do_rmdir(struct sftp_conn *conn, char *path)
561 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
564 status = get_status(conn->fd_in, id);
565 if (status != SSH2_FX_OK)
566 error("Couldn't remove directory: %s", fx2txt(status));
572 do_stat(struct sftp_conn *conn, char *path, int quiet)
578 send_string_request(conn->fd_out, id,
579 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
582 return(get_decode_stat(conn->fd_in, id, quiet));
586 do_lstat(struct sftp_conn *conn, char *path, int quiet)
590 if (conn->version == 0) {
592 debug("Server version does not support lstat operation");
594 logit("Server version does not support lstat operation");
595 return(do_stat(conn, path, quiet));
599 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
602 return(get_decode_stat(conn->fd_in, id, quiet));
607 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
612 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
615 return(get_decode_stat(conn->fd_in, id, quiet));
620 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
625 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
628 status = get_status(conn->fd_in, id);
629 if (status != SSH2_FX_OK)
630 error("Couldn't setstat on \"%s\": %s", path,
637 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
643 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
646 status = get_status(conn->fd_in, id);
647 if (status != SSH2_FX_OK)
648 error("Couldn't fsetstat: %s", fx2txt(status));
654 do_realpath(struct sftp_conn *conn, char *path)
657 u_int type, expected_id, count, id;
658 char *filename, *longname;
661 expected_id = id = conn->msg_id++;
662 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
667 get_msg(conn->fd_in, &msg);
668 type = buffer_get_char(&msg);
669 id = buffer_get_int(&msg);
671 if (id != expected_id)
672 fatal("ID mismatch (%u != %u)", id, expected_id);
674 if (type == SSH2_FXP_STATUS) {
675 u_int status = buffer_get_int(&msg);
677 error("Couldn't canonicalise: %s", fx2txt(status));
679 } else if (type != SSH2_FXP_NAME)
680 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
681 SSH2_FXP_NAME, type);
683 count = buffer_get_int(&msg);
685 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
687 filename = buffer_get_string(&msg, NULL);
688 longname = buffer_get_string(&msg, NULL);
689 a = decode_attrib(&msg);
691 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
701 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
708 /* Send rename request */
710 if ((conn->exts & SFTP_EXT_POSIX_RENAME)) {
711 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
712 buffer_put_int(&msg, id);
713 buffer_put_cstring(&msg, "posix-rename@openssh.com");
715 buffer_put_char(&msg, SSH2_FXP_RENAME);
716 buffer_put_int(&msg, id);
718 buffer_put_cstring(&msg, oldpath);
719 buffer_put_cstring(&msg, newpath);
720 send_msg(conn->fd_out, &msg);
721 debug3("Sent message %s \"%s\" -> \"%s\"",
722 (conn->exts & SFTP_EXT_POSIX_RENAME) ? "posix-rename@openssh.com" :
723 "SSH2_FXP_RENAME", oldpath, newpath);
726 status = get_status(conn->fd_in, id);
727 if (status != SSH2_FX_OK)
728 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
729 newpath, fx2txt(status));
735 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
740 if (conn->version < 3) {
741 error("This server does not support the symlink operation");
742 return(SSH2_FX_OP_UNSUPPORTED);
747 /* Send symlink request */
749 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
750 buffer_put_int(&msg, id);
751 buffer_put_cstring(&msg, oldpath);
752 buffer_put_cstring(&msg, newpath);
753 send_msg(conn->fd_out, &msg);
754 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
758 status = get_status(conn->fd_in, id);
759 if (status != SSH2_FX_OK)
760 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
761 newpath, fx2txt(status));
768 do_readlink(struct sftp_conn *conn, char *path)
771 u_int type, expected_id, count, id;
772 char *filename, *longname;
775 expected_id = id = conn->msg_id++;
776 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
781 get_msg(conn->fd_in, &msg);
782 type = buffer_get_char(&msg);
783 id = buffer_get_int(&msg);
785 if (id != expected_id)
786 fatal("ID mismatch (%u != %u)", id, expected_id);
788 if (type == SSH2_FXP_STATUS) {
789 u_int status = buffer_get_int(&msg);
791 error("Couldn't readlink: %s", fx2txt(status));
793 } else if (type != SSH2_FXP_NAME)
794 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
795 SSH2_FXP_NAME, type);
797 count = buffer_get_int(&msg);
799 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
801 filename = buffer_get_string(&msg, NULL);
802 longname = buffer_get_string(&msg, NULL);
803 a = decode_attrib(&msg);
805 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
816 do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
822 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
823 error("Server does not support statvfs@openssh.com extension");
831 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
832 buffer_put_int(&msg, id);
833 buffer_put_cstring(&msg, "statvfs@openssh.com");
834 buffer_put_cstring(&msg, path);
835 send_msg(conn->fd_out, &msg);
838 return get_decode_statvfs(conn->fd_in, st, id, quiet);
843 do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
844 struct sftp_statvfs *st, int quiet)
849 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
850 error("Server does not support fstatvfs@openssh.com extension");
858 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
859 buffer_put_int(&msg, id);
860 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
861 buffer_put_string(&msg, handle, handle_len);
862 send_msg(conn->fd_out, &msg);
865 return get_decode_statvfs(conn->fd_in, st, id, quiet);
870 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
871 char *handle, u_int handle_len)
877 buffer_put_char(&msg, SSH2_FXP_READ);
878 buffer_put_int(&msg, id);
879 buffer_put_string(&msg, handle, handle_len);
880 buffer_put_int64(&msg, offset);
881 buffer_put_int(&msg, len);
882 send_msg(fd_out, &msg);
887 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
893 int local_fd, status = 0, write_error;
894 int read_error, write_errno;
895 u_int64_t offset, size;
896 u_int handle_len, mode, type, id, buflen, num_req, max_req;
897 off_t progress_counter;
902 TAILQ_ENTRY(request) tq;
904 TAILQ_HEAD(reqhead, request) requests;
907 TAILQ_INIT(&requests);
909 a = do_stat(conn, remote_path, 0);
913 /* XXX: should we preserve set[ug]id? */
914 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
915 mode = a->perm & 0777;
919 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
920 (!S_ISREG(a->perm))) {
921 error("Cannot download non-regular file: %s", remote_path);
925 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
930 buflen = conn->transfer_buflen;
933 /* Send open request */
935 buffer_put_char(&msg, SSH2_FXP_OPEN);
936 buffer_put_int(&msg, id);
937 buffer_put_cstring(&msg, remote_path);
938 buffer_put_int(&msg, SSH2_FXF_READ);
939 attrib_clear(&junk); /* Send empty attributes */
940 encode_attrib(&msg, &junk);
941 send_msg(conn->fd_out, &msg);
942 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
944 handle = get_handle(conn->fd_in, id, &handle_len);
945 if (handle == NULL) {
950 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
952 if (local_fd == -1) {
953 error("Couldn't open local file \"%s\" for writing: %s",
954 local_path, strerror(errno));
955 do_close(conn, handle, handle_len);
961 /* Read from remote and write to local */
962 write_error = read_error = write_errno = num_req = offset = 0;
964 progress_counter = 0;
966 if (showprogress && size != 0)
967 start_progress_meter(remote_path, size, &progress_counter);
969 while (num_req > 0 || max_req > 0) {
974 * Simulate EOF on interrupt: stop sending new requests and
975 * allow outstanding requests to drain gracefully
978 if (num_req == 0) /* If we haven't started yet... */
983 /* Send some more requests */
984 while (num_req < max_req) {
985 debug3("Request range %llu -> %llu (%d/%d)",
986 (unsigned long long)offset,
987 (unsigned long long)offset + buflen - 1,
989 req = xmalloc(sizeof(*req));
990 req->id = conn->msg_id++;
992 req->offset = offset;
995 TAILQ_INSERT_TAIL(&requests, req, tq);
996 send_read_request(conn->fd_out, req->id, req->offset,
997 req->len, handle, handle_len);
1001 get_msg(conn->fd_in, &msg);
1002 type = buffer_get_char(&msg);
1003 id = buffer_get_int(&msg);
1004 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1006 /* Find the request in our queue */
1007 for (req = TAILQ_FIRST(&requests);
1008 req != NULL && req->id != id;
1009 req = TAILQ_NEXT(req, tq))
1012 fatal("Unexpected reply %u", id);
1015 case SSH2_FXP_STATUS:
1016 status = buffer_get_int(&msg);
1017 if (status != SSH2_FX_EOF)
1020 TAILQ_REMOVE(&requests, req, tq);
1025 data = buffer_get_string(&msg, &len);
1026 debug3("Received data %llu -> %llu",
1027 (unsigned long long)req->offset,
1028 (unsigned long long)req->offset + len - 1);
1030 fatal("Received more data than asked for "
1031 "%u > %u", len, req->len);
1032 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1033 atomicio(vwrite, local_fd, data, len) != len) &&
1035 write_errno = errno;
1039 progress_counter += len;
1042 if (len == req->len) {
1043 TAILQ_REMOVE(&requests, req, tq);
1047 /* Resend the request for the missing data */
1048 debug3("Short data block, re-requesting "
1049 "%llu -> %llu (%2d)",
1050 (unsigned long long)req->offset + len,
1051 (unsigned long long)req->offset +
1052 req->len - 1, num_req);
1053 req->id = conn->msg_id++;
1056 send_read_request(conn->fd_out, req->id,
1057 req->offset, req->len, handle, handle_len);
1058 /* Reduce the request size */
1060 buflen = MAX(MIN_READ_SIZE, len);
1062 if (max_req > 0) { /* max_req = 0 iff EOF received */
1063 if (size > 0 && offset > size) {
1064 /* Only one request at a time
1065 * after the expected EOF */
1066 debug3("Finish at %llu (%2d)",
1067 (unsigned long long)offset,
1070 } else if (max_req <= conn->num_requests) {
1076 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1077 SSH2_FXP_DATA, type);
1081 if (showprogress && size)
1082 stop_progress_meter();
1085 if (TAILQ_FIRST(&requests) != NULL)
1086 fatal("Transfer complete, but requests still in queue");
1089 error("Couldn't read from remote file \"%s\" : %s",
1090 remote_path, fx2txt(status));
1091 do_close(conn, handle, handle_len);
1092 } else if (write_error) {
1093 error("Couldn't write to \"%s\": %s", local_path,
1094 strerror(write_errno));
1096 do_close(conn, handle, handle_len);
1098 status = do_close(conn, handle, handle_len);
1100 /* Override umask and utimes if asked */
1102 if (pflag && fchmod(local_fd, mode) == -1)
1104 if (pflag && chmod(local_path, mode) == -1)
1105 #endif /* HAVE_FCHMOD */
1106 error("Couldn't set mode on \"%s\": %s", local_path,
1108 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1109 struct timeval tv[2];
1110 tv[0].tv_sec = a->atime;
1111 tv[1].tv_sec = a->mtime;
1112 tv[0].tv_usec = tv[1].tv_usec = 0;
1113 if (utimes(local_path, tv) == -1)
1114 error("Can't set times on \"%s\": %s",
1115 local_path, strerror(errno));
1126 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
1130 int status = SSH2_FX_OK;
1131 u_int handle_len, id, type;
1133 char *handle, *data;
1139 struct outstanding_ack {
1143 TAILQ_ENTRY(outstanding_ack) tq;
1145 TAILQ_HEAD(ackhead, outstanding_ack) acks;
1146 struct outstanding_ack *ack = NULL;
1150 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1151 error("Couldn't open local file \"%s\" for reading: %s",
1152 local_path, strerror(errno));
1155 if (fstat(local_fd, &sb) == -1) {
1156 error("Couldn't fstat local file \"%s\": %s",
1157 local_path, strerror(errno));
1161 if (!S_ISREG(sb.st_mode)) {
1162 error("%s is not a regular file", local_path);
1166 stat_to_attrib(&sb, &a);
1168 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1169 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1172 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1176 /* Send open request */
1177 id = conn->msg_id++;
1178 buffer_put_char(&msg, SSH2_FXP_OPEN);
1179 buffer_put_int(&msg, id);
1180 buffer_put_cstring(&msg, remote_path);
1181 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1182 encode_attrib(&msg, &a);
1183 send_msg(conn->fd_out, &msg);
1184 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1188 handle = get_handle(conn->fd_in, id, &handle_len);
1189 if (handle == NULL) {
1195 startid = ackid = id + 1;
1196 data = xmalloc(conn->transfer_buflen);
1198 /* Read from local and write to remote */
1201 start_progress_meter(local_path, sb.st_size, &offset);
1207 * Can't use atomicio here because it returns 0 on EOF,
1208 * thus losing the last block of the file.
1209 * Simulate an EOF on interrupt, allowing ACKs from the
1212 if (interrupted || status != SSH2_FX_OK)
1215 len = read(local_fd, data, conn->transfer_buflen);
1216 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1219 fatal("Couldn't read from \"%s\": %s", local_path,
1223 ack = xmalloc(sizeof(*ack));
1225 ack->offset = offset;
1227 TAILQ_INSERT_TAIL(&acks, ack, tq);
1230 buffer_put_char(&msg, SSH2_FXP_WRITE);
1231 buffer_put_int(&msg, ack->id);
1232 buffer_put_string(&msg, handle, handle_len);
1233 buffer_put_int64(&msg, offset);
1234 buffer_put_string(&msg, data, len);
1235 send_msg(conn->fd_out, &msg);
1236 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1237 id, (unsigned long long)offset, len);
1238 } else if (TAILQ_FIRST(&acks) == NULL)
1242 fatal("Unexpected ACK %u", id);
1244 if (id == startid || len == 0 ||
1245 id - ackid >= conn->num_requests) {
1249 get_msg(conn->fd_in, &msg);
1250 type = buffer_get_char(&msg);
1251 r_id = buffer_get_int(&msg);
1253 if (type != SSH2_FXP_STATUS)
1254 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1255 "got %d", SSH2_FXP_STATUS, type);
1257 status = buffer_get_int(&msg);
1258 debug3("SSH2_FXP_STATUS %d", status);
1260 /* Find the request in our queue */
1261 for (ack = TAILQ_FIRST(&acks);
1262 ack != NULL && ack->id != r_id;
1263 ack = TAILQ_NEXT(ack, tq))
1266 fatal("Can't find request for ID %u", r_id);
1267 TAILQ_REMOVE(&acks, ack, tq);
1268 debug3("In write loop, ack for %u %u bytes at %lld",
1269 ack->id, ack->len, (long long)ack->offset);
1275 fatal("%s: offset < 0", __func__);
1280 stop_progress_meter();
1283 if (status != SSH2_FX_OK) {
1284 error("Couldn't write to remote file \"%s\": %s",
1285 remote_path, fx2txt(status));
1289 if (close(local_fd) == -1) {
1290 error("Couldn't close local file \"%s\": %s", local_path,
1295 /* Override umask and utimes if asked */
1297 do_fsetstat(conn, handle, handle_len, &a);
1299 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)