1 /* $OpenBSD: sftp-client.c,v 1.82 2008/04/18 12:32:11 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 <sys/statvfs.h>
28 #include "openbsd-compat/sys-queue.h"
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
32 #ifdef HAVE_SYS_TIME_H
33 # include <sys/time.h>
49 #include "progressmeter.h"
53 #include "sftp-common.h"
54 #include "sftp-client.h"
56 extern volatile sig_atomic_t interrupted;
57 extern int showprogress;
59 /* Minimum amount of data to read at a time */
60 #define MIN_READ_SIZE 512
65 u_int transfer_buflen;
69 #define SFTP_EXT_POSIX_RENAME 0x00000001
70 #define SFTP_EXT_STATVFS 0x00000002
71 #define SFTP_EXT_FSTATVFS 0x00000004
76 send_msg(int fd, Buffer *m)
81 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
82 fatal("Outbound message too long %u", buffer_len(m));
84 /* Send length first */
85 put_u32(mlen, buffer_len(m));
86 iov[0].iov_base = mlen;
87 iov[0].iov_len = sizeof(mlen);
88 iov[1].iov_base = buffer_ptr(m);
89 iov[1].iov_len = buffer_len(m);
91 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
92 fatal("Couldn't send packet: %s", strerror(errno));
98 get_msg(int fd, Buffer *m)
102 buffer_append_space(m, 4);
103 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
105 fatal("Connection closed");
107 fatal("Couldn't read packet: %s", strerror(errno));
110 msg_len = buffer_get_int(m);
111 if (msg_len > SFTP_MAX_MSG_LENGTH)
112 fatal("Received message too long %u", msg_len);
114 buffer_append_space(m, msg_len);
115 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
117 fatal("Connection closed");
119 fatal("Read packet: %s", strerror(errno));
124 send_string_request(int fd, u_int id, u_int code, char *s,
130 buffer_put_char(&msg, code);
131 buffer_put_int(&msg, id);
132 buffer_put_string(&msg, s, len);
134 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
139 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
140 u_int len, Attrib *a)
145 buffer_put_char(&msg, code);
146 buffer_put_int(&msg, id);
147 buffer_put_string(&msg, s, len);
148 encode_attrib(&msg, a);
150 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
155 get_status(int fd, u_int expected_id)
158 u_int type, id, status;
162 type = buffer_get_char(&msg);
163 id = buffer_get_int(&msg);
165 if (id != expected_id)
166 fatal("ID mismatch (%u != %u)", id, expected_id);
167 if (type != SSH2_FXP_STATUS)
168 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
169 SSH2_FXP_STATUS, type);
171 status = buffer_get_int(&msg);
174 debug3("SSH2_FXP_STATUS %u", status);
180 get_handle(int fd, u_int expected_id, u_int *len)
188 type = buffer_get_char(&msg);
189 id = buffer_get_int(&msg);
191 if (id != expected_id)
192 fatal("ID mismatch (%u != %u)", id, expected_id);
193 if (type == SSH2_FXP_STATUS) {
194 int status = buffer_get_int(&msg);
196 error("Couldn't get handle: %s", fx2txt(status));
199 } else if (type != SSH2_FXP_HANDLE)
200 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
201 SSH2_FXP_HANDLE, type);
203 handle = buffer_get_string(&msg, len);
210 get_decode_stat(int fd, u_int expected_id, int quiet)
219 type = buffer_get_char(&msg);
220 id = buffer_get_int(&msg);
222 debug3("Received stat reply T:%u I:%u", type, id);
223 if (id != expected_id)
224 fatal("ID mismatch (%u != %u)", id, expected_id);
225 if (type == SSH2_FXP_STATUS) {
226 int status = buffer_get_int(&msg);
229 debug("Couldn't stat remote file: %s", fx2txt(status));
231 error("Couldn't stat remote file: %s", fx2txt(status));
234 } else if (type != SSH2_FXP_ATTRS) {
235 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
236 SSH2_FXP_ATTRS, type);
238 a = decode_attrib(&msg);
245 get_decode_statvfs(int fd, struct statvfs *st, u_int expected_id, int quiet)
248 u_int type, id, flag;
253 type = buffer_get_char(&msg);
254 id = buffer_get_int(&msg);
256 debug3("Received statvfs reply T:%u I:%u", type, id);
257 if (id != expected_id)
258 fatal("ID mismatch (%u != %u)", id, expected_id);
259 if (type == SSH2_FXP_STATUS) {
260 int status = buffer_get_int(&msg);
263 debug("Couldn't statvfs: %s", fx2txt(status));
265 error("Couldn't statvfs: %s", fx2txt(status));
268 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
269 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
270 SSH2_FXP_EXTENDED_REPLY, type);
273 bzero(st, sizeof(*st));
274 st->f_bsize = buffer_get_int(&msg);
275 st->f_frsize = buffer_get_int(&msg);
276 st->f_blocks = buffer_get_int64(&msg);
277 st->f_bfree = buffer_get_int64(&msg);
278 st->f_bavail = buffer_get_int64(&msg);
279 st->f_files = buffer_get_int64(&msg);
280 st->f_ffree = buffer_get_int64(&msg);
281 st->f_favail = buffer_get_int64(&msg);
282 st->f_fsid = buffer_get_int(&msg);
283 flag = buffer_get_int(&msg);
284 st->f_namemax = buffer_get_int(&msg);
286 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
287 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
295 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
297 u_int type, exts = 0;
300 struct sftp_conn *ret;
303 buffer_put_char(&msg, SSH2_FXP_INIT);
304 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
305 send_msg(fd_out, &msg);
309 get_msg(fd_in, &msg);
311 /* Expecting a VERSION reply */
312 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
313 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
318 version = buffer_get_int(&msg);
320 debug2("Remote version: %d", version);
322 /* Check for extensions */
323 while (buffer_len(&msg) > 0) {
324 char *name = buffer_get_string(&msg, NULL);
325 char *value = buffer_get_string(&msg, NULL);
327 debug2("Init extension: \"%s\"", name);
328 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
329 strcmp(value, "1") == 0)
330 exts |= SFTP_EXT_POSIX_RENAME;
331 if (strcmp(name, "statvfs@openssh.com") == 0 &&
332 strcmp(value, "1") == 0)
333 exts |= SFTP_EXT_STATVFS;
334 if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
335 strcmp(value, "1") == 0)
336 exts |= SFTP_EXT_FSTATVFS;
343 ret = xmalloc(sizeof(*ret));
345 ret->fd_out = fd_out;
346 ret->transfer_buflen = transfer_buflen;
347 ret->num_requests = num_requests;
348 ret->version = version;
352 /* Some filexfer v.0 servers don't support large packets */
354 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
360 sftp_proto_version(struct sftp_conn *conn)
362 return(conn->version);
366 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
374 buffer_put_char(&msg, SSH2_FXP_CLOSE);
375 buffer_put_int(&msg, id);
376 buffer_put_string(&msg, handle, handle_len);
377 send_msg(conn->fd_out, &msg);
378 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
380 status = get_status(conn->fd_in, id);
381 if (status != SSH2_FX_OK)
382 error("Couldn't close file: %s", fx2txt(status));
391 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
395 u_int count, type, id, handle_len, i, expected_id, ents = 0;
401 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
402 buffer_put_int(&msg, id);
403 buffer_put_cstring(&msg, path);
404 send_msg(conn->fd_out, &msg);
408 handle = get_handle(conn->fd_in, id, &handle_len);
414 *dir = xmalloc(sizeof(**dir));
418 for (; !interrupted;) {
419 id = expected_id = conn->msg_id++;
421 debug3("Sending SSH2_FXP_READDIR I:%u", id);
424 buffer_put_char(&msg, SSH2_FXP_READDIR);
425 buffer_put_int(&msg, id);
426 buffer_put_string(&msg, handle, handle_len);
427 send_msg(conn->fd_out, &msg);
431 get_msg(conn->fd_in, &msg);
433 type = buffer_get_char(&msg);
434 id = buffer_get_int(&msg);
436 debug3("Received reply T:%u I:%u", type, id);
438 if (id != expected_id)
439 fatal("ID mismatch (%u != %u)", id, expected_id);
441 if (type == SSH2_FXP_STATUS) {
442 int status = buffer_get_int(&msg);
444 debug3("Received SSH2_FXP_STATUS %d", status);
446 if (status == SSH2_FX_EOF) {
449 error("Couldn't read directory: %s",
451 do_close(conn, handle, handle_len);
455 } else if (type != SSH2_FXP_NAME)
456 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
457 SSH2_FXP_NAME, type);
459 count = buffer_get_int(&msg);
462 debug3("Received %d SSH2_FXP_NAME responses", count);
463 for (i = 0; i < count; i++) {
464 char *filename, *longname;
467 filename = buffer_get_string(&msg, NULL);
468 longname = buffer_get_string(&msg, NULL);
469 a = decode_attrib(&msg);
472 printf("%s\n", longname);
475 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
476 (*dir)[ents] = xmalloc(sizeof(***dir));
477 (*dir)[ents]->filename = xstrdup(filename);
478 (*dir)[ents]->longname = xstrdup(longname);
479 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
480 (*dir)[++ents] = NULL;
489 do_close(conn, handle, handle_len);
492 /* Don't return partial matches on interrupt */
493 if (interrupted && dir != NULL && *dir != NULL) {
494 free_sftp_dirents(*dir);
495 *dir = xmalloc(sizeof(**dir));
503 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
505 return(do_lsreaddir(conn, path, 0, dir));
508 void free_sftp_dirents(SFTP_DIRENT **s)
512 for (i = 0; s[i]; i++) {
513 xfree(s[i]->filename);
514 xfree(s[i]->longname);
521 do_rm(struct sftp_conn *conn, char *path)
525 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
528 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
530 status = get_status(conn->fd_in, id);
531 if (status != SSH2_FX_OK)
532 error("Couldn't delete file: %s", fx2txt(status));
537 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
542 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
545 status = get_status(conn->fd_in, id);
546 if (status != SSH2_FX_OK)
547 error("Couldn't create directory: %s", fx2txt(status));
553 do_rmdir(struct sftp_conn *conn, char *path)
558 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
561 status = get_status(conn->fd_in, id);
562 if (status != SSH2_FX_OK)
563 error("Couldn't remove directory: %s", fx2txt(status));
569 do_stat(struct sftp_conn *conn, char *path, int quiet)
575 send_string_request(conn->fd_out, id,
576 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
579 return(get_decode_stat(conn->fd_in, id, quiet));
583 do_lstat(struct sftp_conn *conn, char *path, int quiet)
587 if (conn->version == 0) {
589 debug("Server version does not support lstat operation");
591 logit("Server version does not support lstat operation");
592 return(do_stat(conn, path, quiet));
596 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
599 return(get_decode_stat(conn->fd_in, id, quiet));
604 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
609 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
612 return(get_decode_stat(conn->fd_in, id, quiet));
617 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
622 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
625 status = get_status(conn->fd_in, id);
626 if (status != SSH2_FX_OK)
627 error("Couldn't setstat on \"%s\": %s", path,
634 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
640 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
643 status = get_status(conn->fd_in, id);
644 if (status != SSH2_FX_OK)
645 error("Couldn't fsetstat: %s", fx2txt(status));
651 do_realpath(struct sftp_conn *conn, char *path)
654 u_int type, expected_id, count, id;
655 char *filename, *longname;
658 expected_id = id = conn->msg_id++;
659 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
664 get_msg(conn->fd_in, &msg);
665 type = buffer_get_char(&msg);
666 id = buffer_get_int(&msg);
668 if (id != expected_id)
669 fatal("ID mismatch (%u != %u)", id, expected_id);
671 if (type == SSH2_FXP_STATUS) {
672 u_int status = buffer_get_int(&msg);
674 error("Couldn't canonicalise: %s", fx2txt(status));
676 } else if (type != SSH2_FXP_NAME)
677 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
678 SSH2_FXP_NAME, type);
680 count = buffer_get_int(&msg);
682 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
684 filename = buffer_get_string(&msg, NULL);
685 longname = buffer_get_string(&msg, NULL);
686 a = decode_attrib(&msg);
688 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
698 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
705 /* Send rename request */
707 if ((conn->exts & SFTP_EXT_POSIX_RENAME)) {
708 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
709 buffer_put_int(&msg, id);
710 buffer_put_cstring(&msg, "posix-rename@openssh.com");
712 buffer_put_char(&msg, SSH2_FXP_RENAME);
713 buffer_put_int(&msg, id);
715 buffer_put_cstring(&msg, oldpath);
716 buffer_put_cstring(&msg, newpath);
717 send_msg(conn->fd_out, &msg);
718 debug3("Sent message %s \"%s\" -> \"%s\"",
719 (conn->exts & SFTP_EXT_POSIX_RENAME) ? "posix-rename@openssh.com" :
720 "SSH2_FXP_RENAME", oldpath, newpath);
723 status = get_status(conn->fd_in, id);
724 if (status != SSH2_FX_OK)
725 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
726 newpath, fx2txt(status));
732 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
737 if (conn->version < 3) {
738 error("This server does not support the symlink operation");
739 return(SSH2_FX_OP_UNSUPPORTED);
744 /* Send symlink request */
746 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
747 buffer_put_int(&msg, id);
748 buffer_put_cstring(&msg, oldpath);
749 buffer_put_cstring(&msg, newpath);
750 send_msg(conn->fd_out, &msg);
751 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
755 status = get_status(conn->fd_in, id);
756 if (status != SSH2_FX_OK)
757 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
758 newpath, fx2txt(status));
765 do_readlink(struct sftp_conn *conn, char *path)
768 u_int type, expected_id, count, id;
769 char *filename, *longname;
772 expected_id = id = conn->msg_id++;
773 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
778 get_msg(conn->fd_in, &msg);
779 type = buffer_get_char(&msg);
780 id = buffer_get_int(&msg);
782 if (id != expected_id)
783 fatal("ID mismatch (%u != %u)", id, expected_id);
785 if (type == SSH2_FXP_STATUS) {
786 u_int status = buffer_get_int(&msg);
788 error("Couldn't readlink: %s", fx2txt(status));
790 } else if (type != SSH2_FXP_NAME)
791 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
792 SSH2_FXP_NAME, type);
794 count = buffer_get_int(&msg);
796 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
798 filename = buffer_get_string(&msg, NULL);
799 longname = buffer_get_string(&msg, NULL);
800 a = decode_attrib(&msg);
802 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
813 do_statvfs(struct sftp_conn *conn, const char *path, struct statvfs *st,
819 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
820 error("Server does not support statvfs@openssh.com extension");
828 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
829 buffer_put_int(&msg, id);
830 buffer_put_cstring(&msg, "statvfs@openssh.com");
831 buffer_put_cstring(&msg, path);
832 send_msg(conn->fd_out, &msg);
835 return get_decode_statvfs(conn->fd_in, st, id, quiet);
840 do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
841 struct statvfs *st, int quiet)
846 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
847 error("Server does not support fstatvfs@openssh.com extension");
855 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
856 buffer_put_int(&msg, id);
857 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
858 buffer_put_string(&msg, handle, handle_len);
859 send_msg(conn->fd_out, &msg);
862 return get_decode_statvfs(conn->fd_in, st, id, quiet);
867 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
868 char *handle, u_int handle_len)
874 buffer_put_char(&msg, SSH2_FXP_READ);
875 buffer_put_int(&msg, id);
876 buffer_put_string(&msg, handle, handle_len);
877 buffer_put_int64(&msg, offset);
878 buffer_put_int(&msg, len);
879 send_msg(fd_out, &msg);
884 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
890 int local_fd, status = 0, write_error;
891 int read_error, write_errno;
892 u_int64_t offset, size;
893 u_int handle_len, mode, type, id, buflen, num_req, max_req;
894 off_t progress_counter;
899 TAILQ_ENTRY(request) tq;
901 TAILQ_HEAD(reqhead, request) requests;
904 TAILQ_INIT(&requests);
906 a = do_stat(conn, remote_path, 0);
910 /* XXX: should we preserve set[ug]id? */
911 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
912 mode = a->perm & 0777;
916 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
917 (!S_ISREG(a->perm))) {
918 error("Cannot download non-regular file: %s", remote_path);
922 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
927 buflen = conn->transfer_buflen;
930 /* Send open request */
932 buffer_put_char(&msg, SSH2_FXP_OPEN);
933 buffer_put_int(&msg, id);
934 buffer_put_cstring(&msg, remote_path);
935 buffer_put_int(&msg, SSH2_FXF_READ);
936 attrib_clear(&junk); /* Send empty attributes */
937 encode_attrib(&msg, &junk);
938 send_msg(conn->fd_out, &msg);
939 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
941 handle = get_handle(conn->fd_in, id, &handle_len);
942 if (handle == NULL) {
947 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
949 if (local_fd == -1) {
950 error("Couldn't open local file \"%s\" for writing: %s",
951 local_path, strerror(errno));
952 do_close(conn, handle, handle_len);
958 /* Read from remote and write to local */
959 write_error = read_error = write_errno = num_req = offset = 0;
961 progress_counter = 0;
963 if (showprogress && size != 0)
964 start_progress_meter(remote_path, size, &progress_counter);
966 while (num_req > 0 || max_req > 0) {
971 * Simulate EOF on interrupt: stop sending new requests and
972 * allow outstanding requests to drain gracefully
975 if (num_req == 0) /* If we haven't started yet... */
980 /* Send some more requests */
981 while (num_req < max_req) {
982 debug3("Request range %llu -> %llu (%d/%d)",
983 (unsigned long long)offset,
984 (unsigned long long)offset + buflen - 1,
986 req = xmalloc(sizeof(*req));
987 req->id = conn->msg_id++;
989 req->offset = offset;
992 TAILQ_INSERT_TAIL(&requests, req, tq);
993 send_read_request(conn->fd_out, req->id, req->offset,
994 req->len, handle, handle_len);
998 get_msg(conn->fd_in, &msg);
999 type = buffer_get_char(&msg);
1000 id = buffer_get_int(&msg);
1001 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1003 /* Find the request in our queue */
1004 for (req = TAILQ_FIRST(&requests);
1005 req != NULL && req->id != id;
1006 req = TAILQ_NEXT(req, tq))
1009 fatal("Unexpected reply %u", id);
1012 case SSH2_FXP_STATUS:
1013 status = buffer_get_int(&msg);
1014 if (status != SSH2_FX_EOF)
1017 TAILQ_REMOVE(&requests, req, tq);
1022 data = buffer_get_string(&msg, &len);
1023 debug3("Received data %llu -> %llu",
1024 (unsigned long long)req->offset,
1025 (unsigned long long)req->offset + len - 1);
1027 fatal("Received more data than asked for "
1028 "%u > %u", len, req->len);
1029 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1030 atomicio(vwrite, local_fd, data, len) != len) &&
1032 write_errno = errno;
1036 progress_counter += len;
1039 if (len == req->len) {
1040 TAILQ_REMOVE(&requests, req, tq);
1044 /* Resend the request for the missing data */
1045 debug3("Short data block, re-requesting "
1046 "%llu -> %llu (%2d)",
1047 (unsigned long long)req->offset + len,
1048 (unsigned long long)req->offset +
1049 req->len - 1, num_req);
1050 req->id = conn->msg_id++;
1053 send_read_request(conn->fd_out, req->id,
1054 req->offset, req->len, handle, handle_len);
1055 /* Reduce the request size */
1057 buflen = MAX(MIN_READ_SIZE, len);
1059 if (max_req > 0) { /* max_req = 0 iff EOF received */
1060 if (size > 0 && offset > size) {
1061 /* Only one request at a time
1062 * after the expected EOF */
1063 debug3("Finish at %llu (%2d)",
1064 (unsigned long long)offset,
1067 } else if (max_req <= conn->num_requests) {
1073 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1074 SSH2_FXP_DATA, type);
1078 if (showprogress && size)
1079 stop_progress_meter();
1082 if (TAILQ_FIRST(&requests) != NULL)
1083 fatal("Transfer complete, but requests still in queue");
1086 error("Couldn't read from remote file \"%s\" : %s",
1087 remote_path, fx2txt(status));
1088 do_close(conn, handle, handle_len);
1089 } else if (write_error) {
1090 error("Couldn't write to \"%s\": %s", local_path,
1091 strerror(write_errno));
1093 do_close(conn, handle, handle_len);
1095 status = do_close(conn, handle, handle_len);
1097 /* Override umask and utimes if asked */
1099 if (pflag && fchmod(local_fd, mode) == -1)
1101 if (pflag && chmod(local_path, mode) == -1)
1102 #endif /* HAVE_FCHMOD */
1103 error("Couldn't set mode on \"%s\": %s", local_path,
1105 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1106 struct timeval tv[2];
1107 tv[0].tv_sec = a->atime;
1108 tv[1].tv_sec = a->mtime;
1109 tv[0].tv_usec = tv[1].tv_usec = 0;
1110 if (utimes(local_path, tv) == -1)
1111 error("Can't set times on \"%s\": %s",
1112 local_path, strerror(errno));
1123 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
1127 int status = SSH2_FX_OK;
1128 u_int handle_len, id, type;
1130 char *handle, *data;
1136 struct outstanding_ack {
1140 TAILQ_ENTRY(outstanding_ack) tq;
1142 TAILQ_HEAD(ackhead, outstanding_ack) acks;
1143 struct outstanding_ack *ack = NULL;
1147 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1148 error("Couldn't open local file \"%s\" for reading: %s",
1149 local_path, strerror(errno));
1152 if (fstat(local_fd, &sb) == -1) {
1153 error("Couldn't fstat local file \"%s\": %s",
1154 local_path, strerror(errno));
1158 if (!S_ISREG(sb.st_mode)) {
1159 error("%s is not a regular file", local_path);
1163 stat_to_attrib(&sb, &a);
1165 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1166 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1169 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1173 /* Send open request */
1174 id = conn->msg_id++;
1175 buffer_put_char(&msg, SSH2_FXP_OPEN);
1176 buffer_put_int(&msg, id);
1177 buffer_put_cstring(&msg, remote_path);
1178 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1179 encode_attrib(&msg, &a);
1180 send_msg(conn->fd_out, &msg);
1181 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1185 handle = get_handle(conn->fd_in, id, &handle_len);
1186 if (handle == NULL) {
1192 startid = ackid = id + 1;
1193 data = xmalloc(conn->transfer_buflen);
1195 /* Read from local and write to remote */
1198 start_progress_meter(local_path, sb.st_size, &offset);
1204 * Can't use atomicio here because it returns 0 on EOF,
1205 * thus losing the last block of the file.
1206 * Simulate an EOF on interrupt, allowing ACKs from the
1209 if (interrupted || status != SSH2_FX_OK)
1212 len = read(local_fd, data, conn->transfer_buflen);
1213 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1216 fatal("Couldn't read from \"%s\": %s", local_path,
1220 ack = xmalloc(sizeof(*ack));
1222 ack->offset = offset;
1224 TAILQ_INSERT_TAIL(&acks, ack, tq);
1227 buffer_put_char(&msg, SSH2_FXP_WRITE);
1228 buffer_put_int(&msg, ack->id);
1229 buffer_put_string(&msg, handle, handle_len);
1230 buffer_put_int64(&msg, offset);
1231 buffer_put_string(&msg, data, len);
1232 send_msg(conn->fd_out, &msg);
1233 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1234 id, (unsigned long long)offset, len);
1235 } else if (TAILQ_FIRST(&acks) == NULL)
1239 fatal("Unexpected ACK %u", id);
1241 if (id == startid || len == 0 ||
1242 id - ackid >= conn->num_requests) {
1246 get_msg(conn->fd_in, &msg);
1247 type = buffer_get_char(&msg);
1248 r_id = buffer_get_int(&msg);
1250 if (type != SSH2_FXP_STATUS)
1251 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1252 "got %d", SSH2_FXP_STATUS, type);
1254 status = buffer_get_int(&msg);
1255 debug3("SSH2_FXP_STATUS %d", status);
1257 /* Find the request in our queue */
1258 for (ack = TAILQ_FIRST(&acks);
1259 ack != NULL && ack->id != r_id;
1260 ack = TAILQ_NEXT(ack, tq))
1263 fatal("Can't find request for ID %u", r_id);
1264 TAILQ_REMOVE(&acks, ack, tq);
1265 debug3("In write loop, ack for %u %u bytes at %lld",
1266 ack->id, ack->len, (long long)ack->offset);
1272 fatal("%s: offset < 0", __func__);
1277 stop_progress_meter();
1280 if (status != SSH2_FX_OK) {
1281 error("Couldn't write to remote file \"%s\": %s",
1282 remote_path, fx2txt(status));
1286 if (close(local_fd) == -1) {
1287 error("Couldn't close local file \"%s\": %s", local_path,
1292 /* Override umask and utimes if asked */
1294 do_fsetstat(conn, handle, handle_len, &a);
1296 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)