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.20 2002/02/05 00:00:46 djm Exp $");
42 #include "sftp-common.h"
43 #include "sftp-client.h"
46 static u_int msg_id = 1;
49 send_msg(int fd, Buffer *m)
51 int mlen = buffer_len(m);
56 buffer_put_int(&oqueue, mlen);
57 buffer_append(&oqueue, buffer_ptr(m), mlen);
58 buffer_consume(m, mlen);
60 len = atomicio(write, fd, buffer_ptr(&oqueue), buffer_len(&oqueue));
62 fatal("Couldn't send packet: %s", strerror(errno));
68 get_msg(int fd, Buffer *m)
71 unsigned char buf[4096];
73 len = atomicio(read, fd, buf, 4);
75 fatal("Connection closed");
77 fatal("Couldn't read packet: %s", strerror(errno));
79 msg_len = GET_32BIT(buf);
80 if (msg_len > 256 * 1024)
81 fatal("Received message too long %d", msg_len);
84 len = atomicio(read, fd, buf, MIN(msg_len, sizeof(buf)));
86 fatal("Connection closed");
88 fatal("Couldn't read packet: %s", strerror(errno));
91 buffer_append(m, buf, len);
96 send_string_request(int fd, u_int id, u_int code, char *s,
102 buffer_put_char(&msg, code);
103 buffer_put_int(&msg, id);
104 buffer_put_string(&msg, s, len);
106 debug3("Sent message fd %d T:%d I:%d", fd, code, id);
111 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
112 u_int len, Attrib *a)
117 buffer_put_char(&msg, code);
118 buffer_put_int(&msg, id);
119 buffer_put_string(&msg, s, len);
120 encode_attrib(&msg, a);
122 debug3("Sent message fd %d T:%d I:%d", fd, code, id);
127 get_status(int fd, int expected_id)
130 u_int type, id, status;
134 type = buffer_get_char(&msg);
135 id = buffer_get_int(&msg);
137 if (id != expected_id)
138 fatal("ID mismatch (%d != %d)", id, expected_id);
139 if (type != SSH2_FXP_STATUS)
140 fatal("Expected SSH2_FXP_STATUS(%d) packet, got %d",
141 SSH2_FXP_STATUS, type);
143 status = buffer_get_int(&msg);
146 debug3("SSH2_FXP_STATUS %d", status);
152 get_handle(int fd, u_int expected_id, u_int *len)
160 type = buffer_get_char(&msg);
161 id = buffer_get_int(&msg);
163 if (id != expected_id)
164 fatal("ID mismatch (%d != %d)", id, expected_id);
165 if (type == SSH2_FXP_STATUS) {
166 int status = buffer_get_int(&msg);
168 error("Couldn't get handle: %s", fx2txt(status));
170 } else if (type != SSH2_FXP_HANDLE)
171 fatal("Expected SSH2_FXP_HANDLE(%d) packet, got %d",
172 SSH2_FXP_HANDLE, type);
174 handle = buffer_get_string(&msg, len);
181 get_decode_stat(int fd, u_int expected_id, int quiet)
190 type = buffer_get_char(&msg);
191 id = buffer_get_int(&msg);
193 debug3("Received stat reply T:%d I:%d", type, id);
194 if (id != expected_id)
195 fatal("ID mismatch (%d != %d)", id, expected_id);
196 if (type == SSH2_FXP_STATUS) {
197 int status = buffer_get_int(&msg);
200 debug("Couldn't stat remote file: %s", fx2txt(status));
202 error("Couldn't stat remote file: %s", fx2txt(status));
204 } else if (type != SSH2_FXP_ATTRS) {
205 fatal("Expected SSH2_FXP_ATTRS(%d) packet, got %d",
206 SSH2_FXP_ATTRS, type);
208 a = decode_attrib(&msg);
215 do_init(int fd_in, int fd_out)
221 buffer_put_char(&msg, SSH2_FXP_INIT);
222 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
223 send_msg(fd_out, &msg);
227 get_msg(fd_in, &msg);
229 /* Expecting a VERSION reply */
230 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
231 error("Invalid packet back from SSH2_FXP_INIT (type %d)",
236 version = buffer_get_int(&msg);
238 debug2("Remote version: %d", version);
240 /* Check for extensions */
241 while (buffer_len(&msg) > 0) {
242 char *name = buffer_get_string(&msg, NULL);
243 char *value = buffer_get_string(&msg, NULL);
245 debug2("Init extension: \"%s\"", name);
256 do_close(int fd_in, int fd_out, char *handle, u_int handle_len)
264 buffer_put_char(&msg, SSH2_FXP_CLOSE);
265 buffer_put_int(&msg, id);
266 buffer_put_string(&msg, handle, handle_len);
267 send_msg(fd_out, &msg);
268 debug3("Sent message SSH2_FXP_CLOSE I:%d", id);
270 status = get_status(fd_in, id);
271 if (status != SSH2_FX_OK)
272 error("Couldn't close file: %s", fx2txt(status));
281 do_lsreaddir(int fd_in, int fd_out, char *path, int printflag,
285 u_int type, id, handle_len, i, expected_id, ents = 0;
291 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
292 buffer_put_int(&msg, id);
293 buffer_put_cstring(&msg, path);
294 send_msg(fd_out, &msg);
298 handle = get_handle(fd_in, id, &handle_len);
304 *dir = xmalloc(sizeof(**dir));
311 id = expected_id = msg_id++;
313 debug3("Sending SSH2_FXP_READDIR I:%d", id);
316 buffer_put_char(&msg, SSH2_FXP_READDIR);
317 buffer_put_int(&msg, id);
318 buffer_put_string(&msg, handle, handle_len);
319 send_msg(fd_out, &msg);
323 get_msg(fd_in, &msg);
325 type = buffer_get_char(&msg);
326 id = buffer_get_int(&msg);
328 debug3("Received reply T:%d I:%d", type, id);
330 if (id != expected_id)
331 fatal("ID mismatch (%d != %d)", id, expected_id);
333 if (type == SSH2_FXP_STATUS) {
334 int status = buffer_get_int(&msg);
336 debug3("Received SSH2_FXP_STATUS %d", status);
338 if (status == SSH2_FX_EOF) {
341 error("Couldn't read directory: %s",
343 do_close(fd_in, fd_out, handle, handle_len);
346 } else if (type != SSH2_FXP_NAME)
347 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
348 SSH2_FXP_NAME, type);
350 count = buffer_get_int(&msg);
353 debug3("Received %d SSH2_FXP_NAME responses", count);
354 for (i = 0; i < count; i++) {
355 char *filename, *longname;
358 filename = buffer_get_string(&msg, NULL);
359 longname = buffer_get_string(&msg, NULL);
360 a = decode_attrib(&msg);
363 printf("%s\n", longname);
366 *dir = xrealloc(*dir, sizeof(**dir) *
368 (*dir)[ents] = xmalloc(sizeof(***dir));
369 (*dir)[ents]->filename = xstrdup(filename);
370 (*dir)[ents]->longname = xstrdup(longname);
371 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
372 (*dir)[++ents] = NULL;
381 do_close(fd_in, fd_out, handle, handle_len);
388 do_ls(int fd_in, int fd_out, char *path)
390 return(do_lsreaddir(fd_in, fd_out, path, 1, NULL));
394 do_readdir(int fd_in, int fd_out, char *path, SFTP_DIRENT ***dir)
396 return(do_lsreaddir(fd_in, fd_out, path, 0, dir));
399 void free_sftp_dirents(SFTP_DIRENT **s)
403 for (i = 0; s[i]; i++) {
404 xfree(s[i]->filename);
405 xfree(s[i]->longname);
412 do_rm(int fd_in, int fd_out, char *path)
416 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
419 send_string_request(fd_out, id, SSH2_FXP_REMOVE, path, strlen(path));
420 status = get_status(fd_in, id);
421 if (status != SSH2_FX_OK)
422 error("Couldn't delete file: %s", fx2txt(status));
427 do_mkdir(int fd_in, int fd_out, char *path, Attrib *a)
432 send_string_attrs_request(fd_out, id, SSH2_FXP_MKDIR, path,
435 status = get_status(fd_in, id);
436 if (status != SSH2_FX_OK)
437 error("Couldn't create directory: %s", fx2txt(status));
443 do_rmdir(int fd_in, int fd_out, char *path)
448 send_string_request(fd_out, id, SSH2_FXP_RMDIR, path, strlen(path));
450 status = get_status(fd_in, id);
451 if (status != SSH2_FX_OK)
452 error("Couldn't remove directory: %s", fx2txt(status));
458 do_stat(int fd_in, int fd_out, char *path, int quiet)
463 send_string_request(fd_out, id, SSH2_FXP_STAT, path, strlen(path));
464 return(get_decode_stat(fd_in, id, quiet));
468 do_lstat(int fd_in, int fd_out, char *path, int quiet)
473 send_string_request(fd_out, id, SSH2_FXP_LSTAT, path, strlen(path));
474 return(get_decode_stat(fd_in, id, quiet));
478 do_fstat(int fd_in, int fd_out, char *handle, u_int handle_len, int quiet)
483 send_string_request(fd_out, id, SSH2_FXP_FSTAT, handle, handle_len);
484 return(get_decode_stat(fd_in, id, quiet));
488 do_setstat(int fd_in, int fd_out, char *path, Attrib *a)
493 send_string_attrs_request(fd_out, id, SSH2_FXP_SETSTAT, path,
496 status = get_status(fd_in, id);
497 if (status != SSH2_FX_OK)
498 error("Couldn't setstat on \"%s\": %s", path,
505 do_fsetstat(int fd_in, int fd_out, char *handle, u_int handle_len,
511 send_string_attrs_request(fd_out, id, SSH2_FXP_FSETSTAT, handle,
514 status = get_status(fd_in, id);
515 if (status != SSH2_FX_OK)
516 error("Couldn't fsetstat: %s", fx2txt(status));
522 do_realpath(int fd_in, int fd_out, char *path)
525 u_int type, expected_id, count, id;
526 char *filename, *longname;
529 expected_id = id = msg_id++;
530 send_string_request(fd_out, id, SSH2_FXP_REALPATH, path, strlen(path));
534 get_msg(fd_in, &msg);
535 type = buffer_get_char(&msg);
536 id = buffer_get_int(&msg);
538 if (id != expected_id)
539 fatal("ID mismatch (%d != %d)", id, expected_id);
541 if (type == SSH2_FXP_STATUS) {
542 u_int status = buffer_get_int(&msg);
544 error("Couldn't canonicalise: %s", fx2txt(status));
546 } else if (type != SSH2_FXP_NAME)
547 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
548 SSH2_FXP_NAME, type);
550 count = buffer_get_int(&msg);
552 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
554 filename = buffer_get_string(&msg, NULL);
555 longname = buffer_get_string(&msg, NULL);
556 a = decode_attrib(&msg);
558 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
568 do_rename(int fd_in, int fd_out, char *oldpath, char *newpath)
575 /* Send rename request */
577 buffer_put_char(&msg, SSH2_FXP_RENAME);
578 buffer_put_int(&msg, id);
579 buffer_put_cstring(&msg, oldpath);
580 buffer_put_cstring(&msg, newpath);
581 send_msg(fd_out, &msg);
582 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
586 status = get_status(fd_in, id);
587 if (status != SSH2_FX_OK)
588 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, newpath,
595 do_symlink(int fd_in, int fd_out, char *oldpath, char *newpath)
602 /* Send rename request */
604 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
605 buffer_put_int(&msg, id);
606 buffer_put_cstring(&msg, oldpath);
607 buffer_put_cstring(&msg, newpath);
608 send_msg(fd_out, &msg);
609 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
613 status = get_status(fd_in, id);
614 if (status != SSH2_FX_OK)
615 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, newpath,
622 do_readlink(int fd_in, int fd_out, char *path)
625 u_int type, expected_id, count, id;
626 char *filename, *longname;
629 expected_id = id = msg_id++;
630 send_string_request(fd_out, id, SSH2_FXP_READLINK, path, strlen(path));
634 get_msg(fd_in, &msg);
635 type = buffer_get_char(&msg);
636 id = buffer_get_int(&msg);
638 if (id != expected_id)
639 fatal("ID mismatch (%d != %d)", id, expected_id);
641 if (type == SSH2_FXP_STATUS) {
642 u_int status = buffer_get_int(&msg);
644 error("Couldn't readlink: %s", fx2txt(status));
646 } else if (type != SSH2_FXP_NAME)
647 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
648 SSH2_FXP_NAME, type);
650 count = buffer_get_int(&msg);
652 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
654 filename = buffer_get_string(&msg, NULL);
655 longname = buffer_get_string(&msg, NULL);
656 a = decode_attrib(&msg);
658 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
668 do_download(int fd_in, int fd_out, char *remote_path, char *local_path,
669 int pflag, size_t buflen)
671 int local_fd, status;
672 u_int expected_id, handle_len, mode, type, id;
678 a = do_stat(fd_in, fd_out, remote_path, 0);
682 /* XXX: should we preserve set[ug]id? */
683 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
684 mode = S_IWRITE | (a->perm & 0777);
688 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
689 (a->perm & S_IFDIR)) {
690 error("Cannot download a directory: %s", remote_path);
694 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
695 if (local_fd == -1) {
696 error("Couldn't open local file \"%s\" for writing: %s",
697 local_path, strerror(errno));
703 /* Send open request */
705 buffer_put_char(&msg, SSH2_FXP_OPEN);
706 buffer_put_int(&msg, id);
707 buffer_put_cstring(&msg, remote_path);
708 buffer_put_int(&msg, SSH2_FXF_READ);
709 attrib_clear(&junk); /* Send empty attributes */
710 encode_attrib(&msg, &junk);
711 send_msg(fd_out, &msg);
712 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
714 handle = get_handle(fd_in, id, &handle_len);
715 if (handle == NULL) {
721 /* Read from remote and write to local */
727 id = expected_id = msg_id++;
730 buffer_put_char(&msg, SSH2_FXP_READ);
731 buffer_put_int(&msg, id);
732 buffer_put_string(&msg, handle, handle_len);
733 buffer_put_int64(&msg, offset);
734 buffer_put_int(&msg, buflen);
735 send_msg(fd_out, &msg);
736 debug3("Sent message SSH2_FXP_READ I:%d O:%llu S:%u",
737 id, (u_int64_t)offset, buflen);
741 get_msg(fd_in, &msg);
742 type = buffer_get_char(&msg);
743 id = buffer_get_int(&msg);
744 debug3("Received reply T:%d I:%d", type, id);
745 if (id != expected_id)
746 fatal("ID mismatch (%d != %d)", id, expected_id);
747 if (type == SSH2_FXP_STATUS) {
748 status = buffer_get_int(&msg);
750 if (status == SSH2_FX_EOF)
753 error("Couldn't read from remote "
754 "file \"%s\" : %s", remote_path,
756 do_close(fd_in, fd_out, handle, handle_len);
759 } else if (type != SSH2_FXP_DATA) {
760 fatal("Expected SSH2_FXP_DATA(%d) packet, got %d",
761 SSH2_FXP_DATA, type);
764 data = buffer_get_string(&msg, &len);
766 fatal("Received more data than asked for %d > %d",
769 debug3("In read loop, got %d offset %llu", len,
771 if (atomicio(write, local_fd, data, len) != len) {
772 error("Couldn't write to \"%s\": %s", local_path,
774 do_close(fd_in, fd_out, handle, handle_len);
783 status = do_close(fd_in, fd_out, handle, handle_len);
785 /* Override umask and utimes if asked */
787 if (pflag && fchmod(local_fd, mode) == -1)
789 if (pflag && chmod(local_path, mode) == -1)
790 #endif /* HAVE_FCHMOD */
791 error("Couldn't set mode on \"%s\": %s", local_path,
793 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
794 struct timeval tv[2];
795 tv[0].tv_sec = a->atime;
796 tv[1].tv_sec = a->mtime;
797 tv[0].tv_usec = tv[1].tv_usec = 0;
798 if (utimes(local_path, tv) == -1)
799 error("Can't set times on \"%s\": %s", local_path,
811 do_upload(int fd_in, int fd_out, char *local_path, char *remote_path,
812 int pflag, size_t buflen)
814 int local_fd, status;
815 u_int handle_len, id;
822 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
823 error("Couldn't open local file \"%s\" for reading: %s",
824 local_path, strerror(errno));
827 if (fstat(local_fd, &sb) == -1) {
828 error("Couldn't fstat local file \"%s\": %s",
829 local_path, strerror(errno));
833 stat_to_attrib(&sb, &a);
835 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
836 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
839 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
843 /* Send open request */
845 buffer_put_char(&msg, SSH2_FXP_OPEN);
846 buffer_put_int(&msg, id);
847 buffer_put_cstring(&msg, remote_path);
848 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
849 encode_attrib(&msg, &a);
850 send_msg(fd_out, &msg);
851 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
855 handle = get_handle(fd_in, id, &handle_len);
856 if (handle == NULL) {
862 data = xmalloc(buflen);
864 /* Read from local and write to remote */
870 * Can't use atomicio here because it returns 0 on EOF, thus losing
871 * the last block of the file
874 len = read(local_fd, data, buflen);
875 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
878 fatal("Couldn't read from \"%s\": %s", local_path,
884 buffer_put_char(&msg, SSH2_FXP_WRITE);
885 buffer_put_int(&msg, ++id);
886 buffer_put_string(&msg, handle, handle_len);
887 buffer_put_int64(&msg, offset);
888 buffer_put_string(&msg, data, len);
889 send_msg(fd_out, &msg);
890 debug3("Sent message SSH2_FXP_WRITE I:%d O:%llu S:%u",
891 id, (u_int64_t)offset, len);
893 status = get_status(fd_in, id);
894 if (status != SSH2_FX_OK) {
895 error("Couldn't write to remote file \"%s\": %s",
896 remote_path, fx2txt(status));
897 do_close(fd_in, fd_out, handle, handle_len);
901 debug3("In write loop, got %d offset %llu", len,
908 if (close(local_fd) == -1) {
909 error("Couldn't close local file \"%s\": %s", local_path,
911 do_close(fd_in, fd_out, handle, handle_len);
916 /* Override umask and utimes if asked */
918 do_fsetstat(fd_in, fd_out, handle, handle_len, &a);
920 status = do_close(fd_in, fd_out, handle, handle_len);