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.14 2001/03/16 08:16:17 djm 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("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("Couldn't read packet: %s", strerror(errno));
93 buffer_append(m, buf, len);
98 send_string_request(int fd, u_int id, u_int code, char *s,
104 buffer_put_char(&msg, code);
105 buffer_put_int(&msg, id);
106 buffer_put_string(&msg, s, len);
108 debug3("Sent message fd %d T:%d I:%d", fd, code, id);
113 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
114 u_int len, Attrib *a)
119 buffer_put_char(&msg, code);
120 buffer_put_int(&msg, id);
121 buffer_put_string(&msg, s, len);
122 encode_attrib(&msg, a);
124 debug3("Sent message fd %d T:%d I:%d", fd, code, id);
129 get_status(int fd, int expected_id)
132 u_int type, id, status;
136 type = buffer_get_char(&msg);
137 id = buffer_get_int(&msg);
139 if (id != expected_id)
140 fatal("ID mismatch (%d != %d)", id, expected_id);
141 if (type != SSH2_FXP_STATUS)
142 fatal("Expected SSH2_FXP_STATUS(%d) packet, got %d",
143 SSH2_FXP_STATUS, type);
145 status = buffer_get_int(&msg);
148 debug3("SSH2_FXP_STATUS %d", status);
154 get_handle(int fd, u_int expected_id, u_int *len)
162 type = buffer_get_char(&msg);
163 id = buffer_get_int(&msg);
165 if (id != expected_id)
166 fatal("ID mismatch (%d != %d)", id, expected_id);
167 if (type == SSH2_FXP_STATUS) {
168 int status = buffer_get_int(&msg);
170 error("Couldn't get handle: %s", fx2txt(status));
172 } else if (type != SSH2_FXP_HANDLE)
173 fatal("Expected SSH2_FXP_HANDLE(%d) packet, got %d",
174 SSH2_FXP_HANDLE, type);
176 handle = buffer_get_string(&msg, len);
183 get_decode_stat(int fd, u_int expected_id, int quiet)
192 type = buffer_get_char(&msg);
193 id = buffer_get_int(&msg);
195 debug3("Received stat reply T:%d I:%d", type, id);
196 if (id != expected_id)
197 fatal("ID mismatch (%d != %d)", id, expected_id);
198 if (type == SSH2_FXP_STATUS) {
199 int status = buffer_get_int(&msg);
202 debug("Couldn't stat remote file: %s", fx2txt(status));
204 error("Couldn't stat remote file: %s", fx2txt(status));
206 } else if (type != SSH2_FXP_ATTRS) {
207 fatal("Expected SSH2_FXP_ATTRS(%d) packet, got %d",
208 SSH2_FXP_ATTRS, type);
210 a = decode_attrib(&msg);
217 do_init(int fd_in, int fd_out)
223 buffer_put_char(&msg, SSH2_FXP_INIT);
224 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
225 send_msg(fd_out, &msg);
229 get_msg(fd_in, &msg);
231 /* Expecting a VERSION reply */
232 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
233 error("Invalid packet back from SSH2_FXP_INIT (type %d)",
238 version = buffer_get_int(&msg);
240 debug2("Remote version: %d", version);
242 /* Check for extensions */
243 while (buffer_len(&msg) > 0) {
244 char *name = buffer_get_string(&msg, NULL);
245 char *value = buffer_get_string(&msg, NULL);
247 debug2("Init extension: \"%s\"", name);
258 do_close(int fd_in, int fd_out, char *handle, u_int handle_len)
266 buffer_put_char(&msg, SSH2_FXP_CLOSE);
267 buffer_put_int(&msg, id);
268 buffer_put_string(&msg, handle, handle_len);
269 send_msg(fd_out, &msg);
270 debug3("Sent message SSH2_FXP_CLOSE I:%d", id);
272 status = get_status(fd_in, id);
273 if (status != SSH2_FX_OK)
274 error("Couldn't close file: %s", fx2txt(status));
283 do_lsreaddir(int fd_in, int fd_out, char *path, int printflag,
287 u_int type, id, handle_len, i, expected_id, ents = 0;
293 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
294 buffer_put_int(&msg, id);
295 buffer_put_cstring(&msg, path);
296 send_msg(fd_out, &msg);
300 handle = get_handle(fd_in, id, &handle_len);
306 *dir = xmalloc(sizeof(**dir));
314 id = expected_id = msg_id++;
316 debug3("Sending SSH2_FXP_READDIR I:%d", id);
319 buffer_put_char(&msg, SSH2_FXP_READDIR);
320 buffer_put_int(&msg, id);
321 buffer_put_string(&msg, handle, handle_len);
322 send_msg(fd_out, &msg);
326 get_msg(fd_in, &msg);
328 type = buffer_get_char(&msg);
329 id = buffer_get_int(&msg);
331 debug3("Received reply T:%d I:%d", type, id);
333 if (id != expected_id)
334 fatal("ID mismatch (%d != %d)", id, expected_id);
336 if (type == SSH2_FXP_STATUS) {
337 int status = buffer_get_int(&msg);
339 debug3("Received SSH2_FXP_STATUS %d", status);
341 if (status == SSH2_FX_EOF) {
344 error("Couldn't read directory: %s",
346 do_close(fd_in, fd_out, handle, handle_len);
349 } else if (type != SSH2_FXP_NAME)
350 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
351 SSH2_FXP_NAME, type);
353 count = buffer_get_int(&msg);
356 debug3("Received %d SSH2_FXP_NAME responses", count);
357 for(i = 0; i < count; i++) {
358 char *filename, *longname;
361 filename = buffer_get_string(&msg, NULL);
362 longname = buffer_get_string(&msg, NULL);
363 a = decode_attrib(&msg);
366 printf("%s\n", longname);
369 *dir = xrealloc(*dir, sizeof(**dir) *
371 (*dir)[ents] = xmalloc(sizeof(***dir));
372 (*dir)[ents]->filename = xstrdup(filename);
373 (*dir)[ents]->longname = xstrdup(longname);
374 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
375 (*dir)[++ents] = NULL;
384 do_close(fd_in, fd_out, handle, handle_len);
391 do_ls(int fd_in, int fd_out, char *path)
393 return(do_lsreaddir(fd_in, fd_out, path, 1, NULL));
397 do_readdir(int fd_in, int fd_out, char *path, SFTP_DIRENT ***dir)
399 return(do_lsreaddir(fd_in, fd_out, path, 0, dir));
402 void free_sftp_dirents(SFTP_DIRENT **s)
406 for(i = 0; s[i]; i++) {
407 xfree(s[i]->filename);
408 xfree(s[i]->longname);
415 do_rm(int fd_in, int fd_out, char *path)
419 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
422 send_string_request(fd_out, id, SSH2_FXP_REMOVE, path, strlen(path));
423 status = get_status(fd_in, id);
424 if (status != SSH2_FX_OK)
425 error("Couldn't delete file: %s", fx2txt(status));
430 do_mkdir(int fd_in, int fd_out, char *path, Attrib *a)
435 send_string_attrs_request(fd_out, id, SSH2_FXP_MKDIR, path,
438 status = get_status(fd_in, id);
439 if (status != SSH2_FX_OK)
440 error("Couldn't create directory: %s", fx2txt(status));
446 do_rmdir(int fd_in, int fd_out, char *path)
451 send_string_request(fd_out, id, SSH2_FXP_RMDIR, path, strlen(path));
453 status = get_status(fd_in, id);
454 if (status != SSH2_FX_OK)
455 error("Couldn't remove directory: %s", fx2txt(status));
461 do_stat(int fd_in, int fd_out, char *path, int quiet)
466 send_string_request(fd_out, id, SSH2_FXP_STAT, path, strlen(path));
467 return(get_decode_stat(fd_in, id, quiet));
471 do_lstat(int fd_in, int fd_out, char *path, int quiet)
476 send_string_request(fd_out, id, SSH2_FXP_LSTAT, path, strlen(path));
477 return(get_decode_stat(fd_in, id, quiet));
481 do_fstat(int fd_in, int fd_out, char *handle, u_int handle_len, int quiet)
486 send_string_request(fd_out, id, SSH2_FXP_FSTAT, handle, handle_len);
487 return(get_decode_stat(fd_in, id, quiet));
491 do_setstat(int fd_in, int fd_out, char *path, Attrib *a)
496 send_string_attrs_request(fd_out, id, SSH2_FXP_SETSTAT, path,
499 status = get_status(fd_in, id);
500 if (status != SSH2_FX_OK)
501 error("Couldn't setstat on \"%s\": %s", path,
508 do_fsetstat(int fd_in, int fd_out, char *handle, u_int handle_len,
514 send_string_attrs_request(fd_out, id, SSH2_FXP_FSETSTAT, handle,
517 status = get_status(fd_in, id);
518 if (status != SSH2_FX_OK)
519 error("Couldn't fsetstat: %s", fx2txt(status));
525 do_realpath(int fd_in, int fd_out, char *path)
528 u_int type, expected_id, count, id;
529 char *filename, *longname;
532 expected_id = id = msg_id++;
533 send_string_request(fd_out, id, SSH2_FXP_REALPATH, path, strlen(path));
537 get_msg(fd_in, &msg);
538 type = buffer_get_char(&msg);
539 id = buffer_get_int(&msg);
541 if (id != expected_id)
542 fatal("ID mismatch (%d != %d)", id, expected_id);
544 if (type == SSH2_FXP_STATUS) {
545 u_int status = buffer_get_int(&msg);
547 error("Couldn't canonicalise: %s", fx2txt(status));
549 } else if (type != SSH2_FXP_NAME)
550 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
551 SSH2_FXP_NAME, type);
553 count = buffer_get_int(&msg);
555 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
557 filename = buffer_get_string(&msg, NULL);
558 longname = buffer_get_string(&msg, NULL);
559 a = decode_attrib(&msg);
561 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
571 do_rename(int fd_in, int fd_out, char *oldpath, char *newpath)
578 /* Send rename request */
580 buffer_put_char(&msg, SSH2_FXP_RENAME);
581 buffer_put_int(&msg, id);
582 buffer_put_cstring(&msg, oldpath);
583 buffer_put_cstring(&msg, newpath);
584 send_msg(fd_out, &msg);
585 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
589 status = get_status(fd_in, id);
590 if (status != SSH2_FX_OK)
591 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, newpath,
598 do_symlink(int fd_in, int fd_out, char *oldpath, char *newpath)
605 /* Send rename request */
607 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
608 buffer_put_int(&msg, id);
609 buffer_put_cstring(&msg, oldpath);
610 buffer_put_cstring(&msg, newpath);
611 send_msg(fd_out, &msg);
612 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
616 status = get_status(fd_in, id);
617 if (status != SSH2_FX_OK)
618 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, newpath,
625 do_readlink(int fd_in, int fd_out, char *path)
628 u_int type, expected_id, count, id;
629 char *filename, *longname;
632 expected_id = id = msg_id++;
633 send_string_request(fd_out, id, SSH2_FXP_READLINK, path, strlen(path));
637 get_msg(fd_in, &msg);
638 type = buffer_get_char(&msg);
639 id = buffer_get_int(&msg);
641 if (id != expected_id)
642 fatal("ID mismatch (%d != %d)", id, expected_id);
644 if (type == SSH2_FXP_STATUS) {
645 u_int status = buffer_get_int(&msg);
647 error("Couldn't readlink: %s", fx2txt(status));
649 } else if (type != SSH2_FXP_NAME)
650 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
651 SSH2_FXP_NAME, type);
653 count = buffer_get_int(&msg);
655 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
657 filename = buffer_get_string(&msg, NULL);
658 longname = buffer_get_string(&msg, NULL);
659 a = decode_attrib(&msg);
661 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
671 do_download(int fd_in, int fd_out, char *remote_path, char *local_path,
675 u_int expected_id, handle_len, mode, type, id;
682 a = do_stat(fd_in, fd_out, remote_path, 0);
686 /* XXX: should we preserve set[ug]id? */
687 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
688 mode = S_IWRITE | (a->perm & 0777);
692 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
693 (a->perm & S_IFDIR)) {
694 error("Cannot download a directory: %s", remote_path);
698 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
699 if (local_fd == -1) {
700 error("Couldn't open local file \"%s\" for writing: %s",
701 local_path, strerror(errno));
707 /* Send open request */
709 buffer_put_char(&msg, SSH2_FXP_OPEN);
710 buffer_put_int(&msg, id);
711 buffer_put_cstring(&msg, remote_path);
712 buffer_put_int(&msg, SSH2_FXF_READ);
713 attrib_clear(&junk); /* Send empty attributes */
714 encode_attrib(&msg, &junk);
715 send_msg(fd_out, &msg);
716 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
718 handle = get_handle(fd_in, id, &handle_len);
719 if (handle == NULL) {
725 /* Read from remote and write to local */
731 id = expected_id = msg_id++;
734 buffer_put_char(&msg, SSH2_FXP_READ);
735 buffer_put_int(&msg, id);
736 buffer_put_string(&msg, handle, handle_len);
737 buffer_put_int64(&msg, offset);
738 buffer_put_int(&msg, COPY_SIZE);
739 send_msg(fd_out, &msg);
740 debug3("Sent message SSH2_FXP_READ I:%d O:%llu S:%u",
741 id, (u_int64_t)offset, COPY_SIZE);
745 get_msg(fd_in, &msg);
746 type = buffer_get_char(&msg);
747 id = buffer_get_int(&msg);
748 debug3("Received reply T:%d I:%d", type, id);
749 if (id != expected_id)
750 fatal("ID mismatch (%d != %d)", id, expected_id);
751 if (type == SSH2_FXP_STATUS) {
752 status = buffer_get_int(&msg);
754 if (status == SSH2_FX_EOF)
757 error("Couldn't read from remote "
758 "file \"%s\" : %s", remote_path,
760 do_close(fd_in, fd_out, handle, handle_len);
763 } else if (type != SSH2_FXP_DATA) {
764 fatal("Expected SSH2_FXP_DATA(%d) packet, got %d",
765 SSH2_FXP_DATA, type);
768 data = buffer_get_string(&msg, &len);
770 fatal("Received more data than asked for %d > %d",
773 debug3("In read loop, got %d offset %llu", len,
775 if (atomicio(write, local_fd, data, len) != len) {
776 error("Couldn't write to \"%s\": %s", local_path,
778 do_close(fd_in, fd_out, handle, handle_len);
787 status = do_close(fd_in, fd_out, handle, handle_len);
789 /* Override umask and utimes if asked */
791 if (pflag && fchmod(local_fd, mode) == -1)
793 if (pflag && chmod(local_path, mode) == -1)
794 #endif /* HAVE_FCHMOD */
795 error("Couldn't set mode on \"%s\": %s", local_path,
797 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
798 struct timeval tv[2];
799 tv[0].tv_sec = a->atime;
800 tv[1].tv_sec = a->mtime;
801 tv[0].tv_usec = tv[1].tv_usec = 0;
802 if (utimes(local_path, tv) == -1)
803 error("Can't set times on \"%s\": %s", local_path,
815 do_upload(int fd_in, int fd_out, char *local_path, char *remote_path,
819 u_int handle_len, id;
827 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
828 error("Couldn't open local file \"%s\" for reading: %s",
829 local_path, strerror(errno));
832 if (fstat(local_fd, &sb) == -1) {
833 error("Couldn't fstat local file \"%s\": %s",
834 local_path, strerror(errno));
838 stat_to_attrib(&sb, &a);
840 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
841 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
844 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
848 /* Send open request */
850 buffer_put_char(&msg, SSH2_FXP_OPEN);
851 buffer_put_int(&msg, id);
852 buffer_put_cstring(&msg, remote_path);
853 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
854 encode_attrib(&msg, &a);
855 send_msg(fd_out, &msg);
856 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
860 handle = get_handle(fd_in, id, &handle_len);
861 if (handle == NULL) {
867 /* Read from local and write to remote */
871 char data[COPY_SIZE];
874 * Can't use atomicio here because it returns 0 on EOF, thus losing
875 * the last block of the file
878 len = read(local_fd, data, COPY_SIZE);
879 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
882 fatal("Couldn't read from \"%s\": %s", local_path,
888 buffer_put_char(&msg, SSH2_FXP_WRITE);
889 buffer_put_int(&msg, ++id);
890 buffer_put_string(&msg, handle, handle_len);
891 buffer_put_int64(&msg, offset);
892 buffer_put_string(&msg, data, len);
893 send_msg(fd_out, &msg);
894 debug3("Sent message SSH2_FXP_WRITE I:%d O:%llu S:%u",
895 id, (u_int64_t)offset, len);
897 status = get_status(fd_in, id);
898 if (status != SSH2_FX_OK) {
899 error("Couldn't write to remote file \"%s\": %s",
900 remote_path, fx2txt(status));
901 do_close(fd_in, fd_out, handle, handle_len);
905 debug3("In write loop, got %d offset %llu", len,
911 if (close(local_fd) == -1) {
912 error("Couldn't close local file \"%s\": %s", local_path,
914 do_close(fd_in, fd_out, handle, handle_len);
919 /* Override umask and utimes if asked */
921 do_fsetstat(fd_in, fd_out, handle, handle_len, &a);
923 status = do_close(fd_in, fd_out, handle, handle_len);