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.19 2001/12/19 07:18:56 deraadt Exp $");
42 #include "sftp-common.h"
43 #include "sftp-client.h"
45 /* How much data to read/write at at time during copies */
46 /* XXX: what should this be? */
47 #define COPY_SIZE 8192
50 static u_int msg_id = 1;
53 send_msg(int fd, Buffer *m)
55 int mlen = buffer_len(m);
60 buffer_put_int(&oqueue, mlen);
61 buffer_append(&oqueue, buffer_ptr(m), mlen);
62 buffer_consume(m, mlen);
64 len = atomicio(write, fd, buffer_ptr(&oqueue), buffer_len(&oqueue));
66 fatal("Couldn't send packet: %s", strerror(errno));
72 get_msg(int fd, Buffer *m)
75 unsigned char buf[4096];
77 len = atomicio(read, fd, buf, 4);
79 fatal("Connection closed");
81 fatal("Couldn't read packet: %s", strerror(errno));
83 msg_len = GET_32BIT(buf);
84 if (msg_len > 256 * 1024)
85 fatal("Received message too long %d", msg_len);
88 len = atomicio(read, fd, buf, MIN(msg_len, sizeof(buf)));
90 fatal("Connection closed");
92 fatal("Couldn't read packet: %s", strerror(errno));
95 buffer_append(m, buf, len);
100 send_string_request(int fd, u_int id, u_int code, char *s,
106 buffer_put_char(&msg, code);
107 buffer_put_int(&msg, id);
108 buffer_put_string(&msg, s, len);
110 debug3("Sent message fd %d T:%d I:%d", fd, code, id);
115 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
116 u_int len, Attrib *a)
121 buffer_put_char(&msg, code);
122 buffer_put_int(&msg, id);
123 buffer_put_string(&msg, s, len);
124 encode_attrib(&msg, a);
126 debug3("Sent message fd %d T:%d I:%d", fd, code, id);
131 get_status(int fd, int expected_id)
134 u_int type, id, status;
138 type = buffer_get_char(&msg);
139 id = buffer_get_int(&msg);
141 if (id != expected_id)
142 fatal("ID mismatch (%d != %d)", id, expected_id);
143 if (type != SSH2_FXP_STATUS)
144 fatal("Expected SSH2_FXP_STATUS(%d) packet, got %d",
145 SSH2_FXP_STATUS, type);
147 status = buffer_get_int(&msg);
150 debug3("SSH2_FXP_STATUS %d", status);
156 get_handle(int fd, u_int expected_id, u_int *len)
164 type = buffer_get_char(&msg);
165 id = buffer_get_int(&msg);
167 if (id != expected_id)
168 fatal("ID mismatch (%d != %d)", id, expected_id);
169 if (type == SSH2_FXP_STATUS) {
170 int status = buffer_get_int(&msg);
172 error("Couldn't get handle: %s", fx2txt(status));
174 } else if (type != SSH2_FXP_HANDLE)
175 fatal("Expected SSH2_FXP_HANDLE(%d) packet, got %d",
176 SSH2_FXP_HANDLE, type);
178 handle = buffer_get_string(&msg, len);
185 get_decode_stat(int fd, u_int expected_id, int quiet)
194 type = buffer_get_char(&msg);
195 id = buffer_get_int(&msg);
197 debug3("Received stat reply T:%d I:%d", type, id);
198 if (id != expected_id)
199 fatal("ID mismatch (%d != %d)", id, expected_id);
200 if (type == SSH2_FXP_STATUS) {
201 int status = buffer_get_int(&msg);
204 debug("Couldn't stat remote file: %s", fx2txt(status));
206 error("Couldn't stat remote file: %s", fx2txt(status));
208 } else if (type != SSH2_FXP_ATTRS) {
209 fatal("Expected SSH2_FXP_ATTRS(%d) packet, got %d",
210 SSH2_FXP_ATTRS, type);
212 a = decode_attrib(&msg);
219 do_init(int fd_in, int fd_out)
225 buffer_put_char(&msg, SSH2_FXP_INIT);
226 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
227 send_msg(fd_out, &msg);
231 get_msg(fd_in, &msg);
233 /* Expecting a VERSION reply */
234 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
235 error("Invalid packet back from SSH2_FXP_INIT (type %d)",
240 version = buffer_get_int(&msg);
242 debug2("Remote version: %d", version);
244 /* Check for extensions */
245 while (buffer_len(&msg) > 0) {
246 char *name = buffer_get_string(&msg, NULL);
247 char *value = buffer_get_string(&msg, NULL);
249 debug2("Init extension: \"%s\"", name);
260 do_close(int fd_in, int fd_out, char *handle, u_int handle_len)
268 buffer_put_char(&msg, SSH2_FXP_CLOSE);
269 buffer_put_int(&msg, id);
270 buffer_put_string(&msg, handle, handle_len);
271 send_msg(fd_out, &msg);
272 debug3("Sent message SSH2_FXP_CLOSE I:%d", id);
274 status = get_status(fd_in, id);
275 if (status != SSH2_FX_OK)
276 error("Couldn't close file: %s", fx2txt(status));
285 do_lsreaddir(int fd_in, int fd_out, char *path, int printflag,
289 u_int type, id, handle_len, i, expected_id, ents = 0;
295 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
296 buffer_put_int(&msg, id);
297 buffer_put_cstring(&msg, path);
298 send_msg(fd_out, &msg);
302 handle = get_handle(fd_in, id, &handle_len);
308 *dir = xmalloc(sizeof(**dir));
315 id = expected_id = msg_id++;
317 debug3("Sending SSH2_FXP_READDIR I:%d", id);
320 buffer_put_char(&msg, SSH2_FXP_READDIR);
321 buffer_put_int(&msg, id);
322 buffer_put_string(&msg, handle, handle_len);
323 send_msg(fd_out, &msg);
327 get_msg(fd_in, &msg);
329 type = buffer_get_char(&msg);
330 id = buffer_get_int(&msg);
332 debug3("Received reply T:%d I:%d", type, id);
334 if (id != expected_id)
335 fatal("ID mismatch (%d != %d)", id, expected_id);
337 if (type == SSH2_FXP_STATUS) {
338 int status = buffer_get_int(&msg);
340 debug3("Received SSH2_FXP_STATUS %d", status);
342 if (status == SSH2_FX_EOF) {
345 error("Couldn't read directory: %s",
347 do_close(fd_in, fd_out, handle, handle_len);
350 } else if (type != SSH2_FXP_NAME)
351 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
352 SSH2_FXP_NAME, type);
354 count = buffer_get_int(&msg);
357 debug3("Received %d SSH2_FXP_NAME responses", count);
358 for (i = 0; i < count; i++) {
359 char *filename, *longname;
362 filename = buffer_get_string(&msg, NULL);
363 longname = buffer_get_string(&msg, NULL);
364 a = decode_attrib(&msg);
367 printf("%s\n", longname);
370 *dir = xrealloc(*dir, sizeof(**dir) *
372 (*dir)[ents] = xmalloc(sizeof(***dir));
373 (*dir)[ents]->filename = xstrdup(filename);
374 (*dir)[ents]->longname = xstrdup(longname);
375 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
376 (*dir)[++ents] = NULL;
385 do_close(fd_in, fd_out, handle, handle_len);
392 do_ls(int fd_in, int fd_out, char *path)
394 return(do_lsreaddir(fd_in, fd_out, path, 1, NULL));
398 do_readdir(int fd_in, int fd_out, char *path, SFTP_DIRENT ***dir)
400 return(do_lsreaddir(fd_in, fd_out, path, 0, dir));
403 void free_sftp_dirents(SFTP_DIRENT **s)
407 for (i = 0; s[i]; i++) {
408 xfree(s[i]->filename);
409 xfree(s[i]->longname);
416 do_rm(int fd_in, int fd_out, char *path)
420 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
423 send_string_request(fd_out, id, SSH2_FXP_REMOVE, path, strlen(path));
424 status = get_status(fd_in, id);
425 if (status != SSH2_FX_OK)
426 error("Couldn't delete file: %s", fx2txt(status));
431 do_mkdir(int fd_in, int fd_out, char *path, Attrib *a)
436 send_string_attrs_request(fd_out, id, SSH2_FXP_MKDIR, path,
439 status = get_status(fd_in, id);
440 if (status != SSH2_FX_OK)
441 error("Couldn't create directory: %s", fx2txt(status));
447 do_rmdir(int fd_in, int fd_out, char *path)
452 send_string_request(fd_out, id, SSH2_FXP_RMDIR, path, strlen(path));
454 status = get_status(fd_in, id);
455 if (status != SSH2_FX_OK)
456 error("Couldn't remove directory: %s", fx2txt(status));
462 do_stat(int fd_in, int fd_out, char *path, int quiet)
467 send_string_request(fd_out, id, SSH2_FXP_STAT, path, strlen(path));
468 return(get_decode_stat(fd_in, id, quiet));
472 do_lstat(int fd_in, int fd_out, char *path, int quiet)
477 send_string_request(fd_out, id, SSH2_FXP_LSTAT, path, strlen(path));
478 return(get_decode_stat(fd_in, id, quiet));
482 do_fstat(int fd_in, int fd_out, char *handle, u_int handle_len, int quiet)
487 send_string_request(fd_out, id, SSH2_FXP_FSTAT, handle, handle_len);
488 return(get_decode_stat(fd_in, id, quiet));
492 do_setstat(int fd_in, int fd_out, char *path, Attrib *a)
497 send_string_attrs_request(fd_out, id, SSH2_FXP_SETSTAT, path,
500 status = get_status(fd_in, id);
501 if (status != SSH2_FX_OK)
502 error("Couldn't setstat on \"%s\": %s", path,
509 do_fsetstat(int fd_in, int fd_out, char *handle, u_int handle_len,
515 send_string_attrs_request(fd_out, id, SSH2_FXP_FSETSTAT, handle,
518 status = get_status(fd_in, id);
519 if (status != SSH2_FX_OK)
520 error("Couldn't fsetstat: %s", fx2txt(status));
526 do_realpath(int fd_in, int fd_out, char *path)
529 u_int type, expected_id, count, id;
530 char *filename, *longname;
533 expected_id = id = msg_id++;
534 send_string_request(fd_out, id, SSH2_FXP_REALPATH, path, strlen(path));
538 get_msg(fd_in, &msg);
539 type = buffer_get_char(&msg);
540 id = buffer_get_int(&msg);
542 if (id != expected_id)
543 fatal("ID mismatch (%d != %d)", id, expected_id);
545 if (type == SSH2_FXP_STATUS) {
546 u_int status = buffer_get_int(&msg);
548 error("Couldn't canonicalise: %s", fx2txt(status));
550 } else if (type != SSH2_FXP_NAME)
551 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
552 SSH2_FXP_NAME, type);
554 count = buffer_get_int(&msg);
556 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
558 filename = buffer_get_string(&msg, NULL);
559 longname = buffer_get_string(&msg, NULL);
560 a = decode_attrib(&msg);
562 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
572 do_rename(int fd_in, int fd_out, char *oldpath, char *newpath)
579 /* Send rename request */
581 buffer_put_char(&msg, SSH2_FXP_RENAME);
582 buffer_put_int(&msg, id);
583 buffer_put_cstring(&msg, oldpath);
584 buffer_put_cstring(&msg, newpath);
585 send_msg(fd_out, &msg);
586 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
590 status = get_status(fd_in, id);
591 if (status != SSH2_FX_OK)
592 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, newpath,
599 do_symlink(int fd_in, int fd_out, char *oldpath, char *newpath)
606 /* Send rename request */
608 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
609 buffer_put_int(&msg, id);
610 buffer_put_cstring(&msg, oldpath);
611 buffer_put_cstring(&msg, newpath);
612 send_msg(fd_out, &msg);
613 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
617 status = get_status(fd_in, id);
618 if (status != SSH2_FX_OK)
619 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, newpath,
626 do_readlink(int fd_in, int fd_out, char *path)
629 u_int type, expected_id, count, id;
630 char *filename, *longname;
633 expected_id = id = msg_id++;
634 send_string_request(fd_out, id, SSH2_FXP_READLINK, path, strlen(path));
638 get_msg(fd_in, &msg);
639 type = buffer_get_char(&msg);
640 id = buffer_get_int(&msg);
642 if (id != expected_id)
643 fatal("ID mismatch (%d != %d)", id, expected_id);
645 if (type == SSH2_FXP_STATUS) {
646 u_int status = buffer_get_int(&msg);
648 error("Couldn't readlink: %s", fx2txt(status));
650 } else if (type != SSH2_FXP_NAME)
651 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
652 SSH2_FXP_NAME, type);
654 count = buffer_get_int(&msg);
656 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
658 filename = buffer_get_string(&msg, NULL);
659 longname = buffer_get_string(&msg, NULL);
660 a = decode_attrib(&msg);
662 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
672 do_download(int fd_in, int fd_out, char *remote_path, char *local_path,
676 u_int expected_id, handle_len, mode, type, id;
683 a = do_stat(fd_in, fd_out, remote_path, 0);
687 /* XXX: should we preserve set[ug]id? */
688 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
689 mode = S_IWRITE | (a->perm & 0777);
693 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
694 (a->perm & S_IFDIR)) {
695 error("Cannot download a directory: %s", remote_path);
699 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
700 if (local_fd == -1) {
701 error("Couldn't open local file \"%s\" for writing: %s",
702 local_path, strerror(errno));
708 /* Send open request */
710 buffer_put_char(&msg, SSH2_FXP_OPEN);
711 buffer_put_int(&msg, id);
712 buffer_put_cstring(&msg, remote_path);
713 buffer_put_int(&msg, SSH2_FXF_READ);
714 attrib_clear(&junk); /* Send empty attributes */
715 encode_attrib(&msg, &junk);
716 send_msg(fd_out, &msg);
717 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
719 handle = get_handle(fd_in, id, &handle_len);
720 if (handle == NULL) {
726 /* Read from remote and write to local */
732 id = expected_id = msg_id++;
735 buffer_put_char(&msg, SSH2_FXP_READ);
736 buffer_put_int(&msg, id);
737 buffer_put_string(&msg, handle, handle_len);
738 buffer_put_int64(&msg, offset);
739 buffer_put_int(&msg, COPY_SIZE);
740 send_msg(fd_out, &msg);
741 debug3("Sent message SSH2_FXP_READ I:%d O:%llu S:%u",
742 id, (u_int64_t)offset, COPY_SIZE);
746 get_msg(fd_in, &msg);
747 type = buffer_get_char(&msg);
748 id = buffer_get_int(&msg);
749 debug3("Received reply T:%d I:%d", type, id);
750 if (id != expected_id)
751 fatal("ID mismatch (%d != %d)", id, expected_id);
752 if (type == SSH2_FXP_STATUS) {
753 status = buffer_get_int(&msg);
755 if (status == SSH2_FX_EOF)
758 error("Couldn't read from remote "
759 "file \"%s\" : %s", remote_path,
761 do_close(fd_in, fd_out, handle, handle_len);
764 } else if (type != SSH2_FXP_DATA) {
765 fatal("Expected SSH2_FXP_DATA(%d) packet, got %d",
766 SSH2_FXP_DATA, type);
769 data = buffer_get_string(&msg, &len);
771 fatal("Received more data than asked for %d > %d",
774 debug3("In read loop, got %d offset %llu", len,
776 if (atomicio(write, local_fd, data, len) != len) {
777 error("Couldn't write to \"%s\": %s", local_path,
779 do_close(fd_in, fd_out, handle, handle_len);
788 status = do_close(fd_in, fd_out, handle, handle_len);
790 /* Override umask and utimes if asked */
792 if (pflag && fchmod(local_fd, mode) == -1)
794 if (pflag && chmod(local_path, mode) == -1)
795 #endif /* HAVE_FCHMOD */
796 error("Couldn't set mode on \"%s\": %s", local_path,
798 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
799 struct timeval tv[2];
800 tv[0].tv_sec = a->atime;
801 tv[1].tv_sec = a->mtime;
802 tv[0].tv_usec = tv[1].tv_usec = 0;
803 if (utimes(local_path, tv) == -1)
804 error("Can't set times on \"%s\": %s", local_path,
816 do_upload(int fd_in, int fd_out, char *local_path, char *remote_path,
820 u_int handle_len, id;
828 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
829 error("Couldn't open local file \"%s\" for reading: %s",
830 local_path, strerror(errno));
833 if (fstat(local_fd, &sb) == -1) {
834 error("Couldn't fstat local file \"%s\": %s",
835 local_path, strerror(errno));
839 stat_to_attrib(&sb, &a);
841 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
842 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
845 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
849 /* Send open request */
851 buffer_put_char(&msg, SSH2_FXP_OPEN);
852 buffer_put_int(&msg, id);
853 buffer_put_cstring(&msg, remote_path);
854 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
855 encode_attrib(&msg, &a);
856 send_msg(fd_out, &msg);
857 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
861 handle = get_handle(fd_in, id, &handle_len);
862 if (handle == NULL) {
868 /* Read from local and write to remote */
872 char data[COPY_SIZE];
875 * Can't use atomicio here because it returns 0 on EOF, thus losing
876 * the last block of the file
879 len = read(local_fd, data, COPY_SIZE);
880 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
883 fatal("Couldn't read from \"%s\": %s", local_path,
889 buffer_put_char(&msg, SSH2_FXP_WRITE);
890 buffer_put_int(&msg, ++id);
891 buffer_put_string(&msg, handle, handle_len);
892 buffer_put_int64(&msg, offset);
893 buffer_put_string(&msg, data, len);
894 send_msg(fd_out, &msg);
895 debug3("Sent message SSH2_FXP_WRITE I:%d O:%llu S:%u",
896 id, (u_int64_t)offset, len);
898 status = get_status(fd_in, id);
899 if (status != SSH2_FX_OK) {
900 error("Couldn't write to remote file \"%s\": %s",
901 remote_path, fx2txt(status));
902 do_close(fd_in, fd_out, handle, handle_len);
906 debug3("In write loop, got %d offset %llu", len,
912 if (close(local_fd) == -1) {
913 error("Couldn't close local file \"%s\": %s", local_path,
915 do_close(fd_in, fd_out, handle, handle_len);
920 /* Override umask and utimes if asked */
922 do_fsetstat(fd_in, fd_out, handle, handle_len, &a);
924 status = do_close(fd_in, fd_out, handle, handle_len);