2 * Copyright (c) 2001 Damien Miller. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 /* XXX: signed vs unsigned */
27 /* XXX: redesign to allow concurrent overlapped operations */
28 /* XXX: we use fatal too much, error may be more appropriate in places */
29 /* XXX: copy between two remote sites */
32 RCSID("$OpenBSD: sftp-client.c,v 1.17 2001/06/23 15:12:20 itojun Exp $");
41 #include "pathnames.h"
44 #include "sftp-common.h"
45 #include "sftp-client.h"
47 /* How much data to read/write at at time during copies */
48 /* XXX: what should this be? */
49 #define COPY_SIZE 8192
52 static u_int msg_id = 1;
55 send_msg(int fd, Buffer *m)
57 int mlen = buffer_len(m);
62 buffer_put_int(&oqueue, mlen);
63 buffer_append(&oqueue, buffer_ptr(m), mlen);
64 buffer_consume(m, mlen);
66 len = atomicio(write, fd, buffer_ptr(&oqueue), buffer_len(&oqueue));
68 fatal("Couldn't send packet: %s", strerror(errno));
74 get_msg(int fd, Buffer *m)
77 unsigned char buf[4096];
79 len = atomicio(read, fd, buf, 4);
81 fatal("Connection closed");
83 fatal("Couldn't read packet: %s", strerror(errno));
85 msg_len = GET_32BIT(buf);
86 if (msg_len > 256 * 1024)
87 fatal("Received message too long %d", msg_len);
90 len = atomicio(read, fd, buf, MIN(msg_len, sizeof(buf)));
92 fatal("Connection closed");
94 fatal("Couldn't read packet: %s", strerror(errno));
97 buffer_append(m, buf, len);
102 send_string_request(int fd, u_int id, u_int code, char *s,
108 buffer_put_char(&msg, code);
109 buffer_put_int(&msg, id);
110 buffer_put_string(&msg, s, len);
112 debug3("Sent message fd %d T:%d I:%d", fd, code, id);
117 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
118 u_int len, Attrib *a)
123 buffer_put_char(&msg, code);
124 buffer_put_int(&msg, id);
125 buffer_put_string(&msg, s, len);
126 encode_attrib(&msg, a);
128 debug3("Sent message fd %d T:%d I:%d", fd, code, id);
133 get_status(int fd, int expected_id)
136 u_int type, id, status;
140 type = buffer_get_char(&msg);
141 id = buffer_get_int(&msg);
143 if (id != expected_id)
144 fatal("ID mismatch (%d != %d)", id, expected_id);
145 if (type != SSH2_FXP_STATUS)
146 fatal("Expected SSH2_FXP_STATUS(%d) packet, got %d",
147 SSH2_FXP_STATUS, type);
149 status = buffer_get_int(&msg);
152 debug3("SSH2_FXP_STATUS %d", status);
158 get_handle(int fd, u_int expected_id, u_int *len)
166 type = buffer_get_char(&msg);
167 id = buffer_get_int(&msg);
169 if (id != expected_id)
170 fatal("ID mismatch (%d != %d)", id, expected_id);
171 if (type == SSH2_FXP_STATUS) {
172 int status = buffer_get_int(&msg);
174 error("Couldn't get handle: %s", fx2txt(status));
176 } else if (type != SSH2_FXP_HANDLE)
177 fatal("Expected SSH2_FXP_HANDLE(%d) packet, got %d",
178 SSH2_FXP_HANDLE, type);
180 handle = buffer_get_string(&msg, len);
187 get_decode_stat(int fd, u_int expected_id, int quiet)
196 type = buffer_get_char(&msg);
197 id = buffer_get_int(&msg);
199 debug3("Received stat reply T:%d I:%d", type, id);
200 if (id != expected_id)
201 fatal("ID mismatch (%d != %d)", id, expected_id);
202 if (type == SSH2_FXP_STATUS) {
203 int status = buffer_get_int(&msg);
206 debug("Couldn't stat remote file: %s", fx2txt(status));
208 error("Couldn't stat remote file: %s", fx2txt(status));
210 } else if (type != SSH2_FXP_ATTRS) {
211 fatal("Expected SSH2_FXP_ATTRS(%d) packet, got %d",
212 SSH2_FXP_ATTRS, type);
214 a = decode_attrib(&msg);
221 do_init(int fd_in, int fd_out)
227 buffer_put_char(&msg, SSH2_FXP_INIT);
228 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
229 send_msg(fd_out, &msg);
233 get_msg(fd_in, &msg);
235 /* Expecting a VERSION reply */
236 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
237 error("Invalid packet back from SSH2_FXP_INIT (type %d)",
242 version = buffer_get_int(&msg);
244 debug2("Remote version: %d", version);
246 /* Check for extensions */
247 while (buffer_len(&msg) > 0) {
248 char *name = buffer_get_string(&msg, NULL);
249 char *value = buffer_get_string(&msg, NULL);
251 debug2("Init extension: \"%s\"", name);
262 do_close(int fd_in, int fd_out, char *handle, u_int handle_len)
270 buffer_put_char(&msg, SSH2_FXP_CLOSE);
271 buffer_put_int(&msg, id);
272 buffer_put_string(&msg, handle, handle_len);
273 send_msg(fd_out, &msg);
274 debug3("Sent message SSH2_FXP_CLOSE I:%d", id);
276 status = get_status(fd_in, id);
277 if (status != SSH2_FX_OK)
278 error("Couldn't close file: %s", fx2txt(status));
287 do_lsreaddir(int fd_in, int fd_out, char *path, int printflag,
291 u_int type, id, handle_len, i, expected_id, ents = 0;
297 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
298 buffer_put_int(&msg, id);
299 buffer_put_cstring(&msg, path);
300 send_msg(fd_out, &msg);
304 handle = get_handle(fd_in, id, &handle_len);
310 *dir = xmalloc(sizeof(**dir));
318 id = expected_id = msg_id++;
320 debug3("Sending SSH2_FXP_READDIR I:%d", id);
323 buffer_put_char(&msg, SSH2_FXP_READDIR);
324 buffer_put_int(&msg, id);
325 buffer_put_string(&msg, handle, handle_len);
326 send_msg(fd_out, &msg);
330 get_msg(fd_in, &msg);
332 type = buffer_get_char(&msg);
333 id = buffer_get_int(&msg);
335 debug3("Received reply T:%d I:%d", type, id);
337 if (id != expected_id)
338 fatal("ID mismatch (%d != %d)", id, expected_id);
340 if (type == SSH2_FXP_STATUS) {
341 int status = buffer_get_int(&msg);
343 debug3("Received SSH2_FXP_STATUS %d", status);
345 if (status == SSH2_FX_EOF) {
348 error("Couldn't read directory: %s",
350 do_close(fd_in, fd_out, handle, handle_len);
353 } else if (type != SSH2_FXP_NAME)
354 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
355 SSH2_FXP_NAME, type);
357 count = buffer_get_int(&msg);
360 debug3("Received %d SSH2_FXP_NAME responses", count);
361 for(i = 0; i < count; i++) {
362 char *filename, *longname;
365 filename = buffer_get_string(&msg, NULL);
366 longname = buffer_get_string(&msg, NULL);
367 a = decode_attrib(&msg);
370 printf("%s\n", longname);
373 *dir = xrealloc(*dir, sizeof(**dir) *
375 (*dir)[ents] = xmalloc(sizeof(***dir));
376 (*dir)[ents]->filename = xstrdup(filename);
377 (*dir)[ents]->longname = xstrdup(longname);
378 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
379 (*dir)[++ents] = NULL;
388 do_close(fd_in, fd_out, handle, handle_len);
395 do_ls(int fd_in, int fd_out, char *path)
397 return(do_lsreaddir(fd_in, fd_out, path, 1, NULL));
401 do_readdir(int fd_in, int fd_out, char *path, SFTP_DIRENT ***dir)
403 return(do_lsreaddir(fd_in, fd_out, path, 0, dir));
406 void free_sftp_dirents(SFTP_DIRENT **s)
410 for(i = 0; s[i]; i++) {
411 xfree(s[i]->filename);
412 xfree(s[i]->longname);
419 do_rm(int fd_in, int fd_out, char *path)
423 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
426 send_string_request(fd_out, id, SSH2_FXP_REMOVE, path, strlen(path));
427 status = get_status(fd_in, id);
428 if (status != SSH2_FX_OK)
429 error("Couldn't delete file: %s", fx2txt(status));
434 do_mkdir(int fd_in, int fd_out, char *path, Attrib *a)
439 send_string_attrs_request(fd_out, id, SSH2_FXP_MKDIR, path,
442 status = get_status(fd_in, id);
443 if (status != SSH2_FX_OK)
444 error("Couldn't create directory: %s", fx2txt(status));
450 do_rmdir(int fd_in, int fd_out, char *path)
455 send_string_request(fd_out, id, SSH2_FXP_RMDIR, path, strlen(path));
457 status = get_status(fd_in, id);
458 if (status != SSH2_FX_OK)
459 error("Couldn't remove directory: %s", fx2txt(status));
465 do_stat(int fd_in, int fd_out, char *path, int quiet)
470 send_string_request(fd_out, id, SSH2_FXP_STAT, path, strlen(path));
471 return(get_decode_stat(fd_in, id, quiet));
475 do_lstat(int fd_in, int fd_out, char *path, int quiet)
480 send_string_request(fd_out, id, SSH2_FXP_LSTAT, path, strlen(path));
481 return(get_decode_stat(fd_in, id, quiet));
485 do_fstat(int fd_in, int fd_out, char *handle, u_int handle_len, int quiet)
490 send_string_request(fd_out, id, SSH2_FXP_FSTAT, handle, handle_len);
491 return(get_decode_stat(fd_in, id, quiet));
495 do_setstat(int fd_in, int fd_out, char *path, Attrib *a)
500 send_string_attrs_request(fd_out, id, SSH2_FXP_SETSTAT, path,
503 status = get_status(fd_in, id);
504 if (status != SSH2_FX_OK)
505 error("Couldn't setstat on \"%s\": %s", path,
512 do_fsetstat(int fd_in, int fd_out, char *handle, u_int handle_len,
518 send_string_attrs_request(fd_out, id, SSH2_FXP_FSETSTAT, handle,
521 status = get_status(fd_in, id);
522 if (status != SSH2_FX_OK)
523 error("Couldn't fsetstat: %s", fx2txt(status));
529 do_realpath(int fd_in, int fd_out, char *path)
532 u_int type, expected_id, count, id;
533 char *filename, *longname;
536 expected_id = id = msg_id++;
537 send_string_request(fd_out, id, SSH2_FXP_REALPATH, path, strlen(path));
541 get_msg(fd_in, &msg);
542 type = buffer_get_char(&msg);
543 id = buffer_get_int(&msg);
545 if (id != expected_id)
546 fatal("ID mismatch (%d != %d)", id, expected_id);
548 if (type == SSH2_FXP_STATUS) {
549 u_int status = buffer_get_int(&msg);
551 error("Couldn't canonicalise: %s", fx2txt(status));
553 } else if (type != SSH2_FXP_NAME)
554 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
555 SSH2_FXP_NAME, type);
557 count = buffer_get_int(&msg);
559 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
561 filename = buffer_get_string(&msg, NULL);
562 longname = buffer_get_string(&msg, NULL);
563 a = decode_attrib(&msg);
565 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
575 do_rename(int fd_in, int fd_out, char *oldpath, char *newpath)
582 /* Send rename request */
584 buffer_put_char(&msg, SSH2_FXP_RENAME);
585 buffer_put_int(&msg, id);
586 buffer_put_cstring(&msg, oldpath);
587 buffer_put_cstring(&msg, newpath);
588 send_msg(fd_out, &msg);
589 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
593 status = get_status(fd_in, id);
594 if (status != SSH2_FX_OK)
595 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, newpath,
602 do_symlink(int fd_in, int fd_out, char *oldpath, char *newpath)
609 /* Send rename request */
611 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
612 buffer_put_int(&msg, id);
613 buffer_put_cstring(&msg, oldpath);
614 buffer_put_cstring(&msg, newpath);
615 send_msg(fd_out, &msg);
616 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
620 status = get_status(fd_in, id);
621 if (status != SSH2_FX_OK)
622 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, newpath,
629 do_readlink(int fd_in, int fd_out, char *path)
632 u_int type, expected_id, count, id;
633 char *filename, *longname;
636 expected_id = id = msg_id++;
637 send_string_request(fd_out, id, SSH2_FXP_READLINK, path, strlen(path));
641 get_msg(fd_in, &msg);
642 type = buffer_get_char(&msg);
643 id = buffer_get_int(&msg);
645 if (id != expected_id)
646 fatal("ID mismatch (%d != %d)", id, expected_id);
648 if (type == SSH2_FXP_STATUS) {
649 u_int status = buffer_get_int(&msg);
651 error("Couldn't readlink: %s", fx2txt(status));
653 } else if (type != SSH2_FXP_NAME)
654 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
655 SSH2_FXP_NAME, type);
657 count = buffer_get_int(&msg);
659 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
661 filename = buffer_get_string(&msg, NULL);
662 longname = buffer_get_string(&msg, NULL);
663 a = decode_attrib(&msg);
665 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
675 do_download(int fd_in, int fd_out, char *remote_path, char *local_path,
679 u_int expected_id, handle_len, mode, type, id;
686 a = do_stat(fd_in, fd_out, remote_path, 0);
690 /* XXX: should we preserve set[ug]id? */
691 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
692 mode = S_IWRITE | (a->perm & 0777);
696 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
697 (a->perm & S_IFDIR)) {
698 error("Cannot download a directory: %s", remote_path);
702 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
703 if (local_fd == -1) {
704 error("Couldn't open local file \"%s\" for writing: %s",
705 local_path, strerror(errno));
711 /* Send open request */
713 buffer_put_char(&msg, SSH2_FXP_OPEN);
714 buffer_put_int(&msg, id);
715 buffer_put_cstring(&msg, remote_path);
716 buffer_put_int(&msg, SSH2_FXF_READ);
717 attrib_clear(&junk); /* Send empty attributes */
718 encode_attrib(&msg, &junk);
719 send_msg(fd_out, &msg);
720 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
722 handle = get_handle(fd_in, id, &handle_len);
723 if (handle == NULL) {
729 /* Read from remote and write to local */
735 id = expected_id = msg_id++;
738 buffer_put_char(&msg, SSH2_FXP_READ);
739 buffer_put_int(&msg, id);
740 buffer_put_string(&msg, handle, handle_len);
741 buffer_put_int64(&msg, offset);
742 buffer_put_int(&msg, COPY_SIZE);
743 send_msg(fd_out, &msg);
744 debug3("Sent message SSH2_FXP_READ I:%d O:%llu S:%u",
745 id, (u_int64_t)offset, COPY_SIZE);
749 get_msg(fd_in, &msg);
750 type = buffer_get_char(&msg);
751 id = buffer_get_int(&msg);
752 debug3("Received reply T:%d I:%d", type, id);
753 if (id != expected_id)
754 fatal("ID mismatch (%d != %d)", id, expected_id);
755 if (type == SSH2_FXP_STATUS) {
756 status = buffer_get_int(&msg);
758 if (status == SSH2_FX_EOF)
761 error("Couldn't read from remote "
762 "file \"%s\" : %s", remote_path,
764 do_close(fd_in, fd_out, handle, handle_len);
767 } else if (type != SSH2_FXP_DATA) {
768 fatal("Expected SSH2_FXP_DATA(%d) packet, got %d",
769 SSH2_FXP_DATA, type);
772 data = buffer_get_string(&msg, &len);
774 fatal("Received more data than asked for %d > %d",
777 debug3("In read loop, got %d offset %llu", len,
779 if (atomicio(write, local_fd, data, len) != len) {
780 error("Couldn't write to \"%s\": %s", local_path,
782 do_close(fd_in, fd_out, handle, handle_len);
791 status = do_close(fd_in, fd_out, handle, handle_len);
793 /* Override umask and utimes if asked */
795 if (pflag && fchmod(local_fd, mode) == -1)
797 if (pflag && chmod(local_path, mode) == -1)
798 #endif /* HAVE_FCHMOD */
799 error("Couldn't set mode on \"%s\": %s", local_path,
801 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
802 struct timeval tv[2];
803 tv[0].tv_sec = a->atime;
804 tv[1].tv_sec = a->mtime;
805 tv[0].tv_usec = tv[1].tv_usec = 0;
806 if (utimes(local_path, tv) == -1)
807 error("Can't set times on \"%s\": %s", local_path,
819 do_upload(int fd_in, int fd_out, char *local_path, char *remote_path,
823 u_int handle_len, id;
831 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
832 error("Couldn't open local file \"%s\" for reading: %s",
833 local_path, strerror(errno));
836 if (fstat(local_fd, &sb) == -1) {
837 error("Couldn't fstat local file \"%s\": %s",
838 local_path, strerror(errno));
842 stat_to_attrib(&sb, &a);
844 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
845 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
848 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
852 /* Send open request */
854 buffer_put_char(&msg, SSH2_FXP_OPEN);
855 buffer_put_int(&msg, id);
856 buffer_put_cstring(&msg, remote_path);
857 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
858 encode_attrib(&msg, &a);
859 send_msg(fd_out, &msg);
860 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
864 handle = get_handle(fd_in, id, &handle_len);
865 if (handle == NULL) {
871 /* Read from local and write to remote */
875 char data[COPY_SIZE];
878 * Can't use atomicio here because it returns 0 on EOF, thus losing
879 * the last block of the file
882 len = read(local_fd, data, COPY_SIZE);
883 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
886 fatal("Couldn't read from \"%s\": %s", local_path,
892 buffer_put_char(&msg, SSH2_FXP_WRITE);
893 buffer_put_int(&msg, ++id);
894 buffer_put_string(&msg, handle, handle_len);
895 buffer_put_int64(&msg, offset);
896 buffer_put_string(&msg, data, len);
897 send_msg(fd_out, &msg);
898 debug3("Sent message SSH2_FXP_WRITE I:%d O:%llu S:%u",
899 id, (u_int64_t)offset, len);
901 status = get_status(fd_in, id);
902 if (status != SSH2_FX_OK) {
903 error("Couldn't write to remote file \"%s\": %s",
904 remote_path, fx2txt(status));
905 do_close(fd_in, fd_out, handle, handle_len);
909 debug3("In write loop, got %d offset %llu", len,
915 if (close(local_fd) == -1) {
916 error("Couldn't close local file \"%s\": %s", local_path,
918 do_close(fd_in, fd_out, handle, handle_len);
923 /* Override umask and utimes if asked */
925 do_fsetstat(fd_in, fd_out, handle, handle_len, &a);
927 status = do_close(fd_in, fd_out, handle, handle_len);