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.11 2001/03/07 10:11:22 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)
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);
201 error("Couldn't stat remote file: %s", fx2txt(status));
203 } else if (type != SSH2_FXP_ATTRS) {
204 fatal("Expected SSH2_FXP_ATTRS(%d) packet, got %d",
205 SSH2_FXP_ATTRS, type);
207 a = decode_attrib(&msg);
214 do_init(int fd_in, int fd_out)
220 buffer_put_char(&msg, SSH2_FXP_INIT);
221 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
222 send_msg(fd_out, &msg);
226 get_msg(fd_in, &msg);
228 /* Expecting a VERSION reply */
229 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
230 error("Invalid packet back from SSH2_FXP_INIT (type %d)",
235 version = buffer_get_int(&msg);
237 debug2("Remote version: %d", version);
239 /* Check for extensions */
240 while (buffer_len(&msg) > 0) {
241 char *name = buffer_get_string(&msg, NULL);
242 char *value = buffer_get_string(&msg, NULL);
244 debug2("Init extension: \"%s\"", name);
255 do_close(int fd_in, int fd_out, char *handle, u_int handle_len)
263 buffer_put_char(&msg, SSH2_FXP_CLOSE);
264 buffer_put_int(&msg, id);
265 buffer_put_string(&msg, handle, handle_len);
266 send_msg(fd_out, &msg);
267 debug3("Sent message SSH2_FXP_CLOSE I:%d", id);
269 status = get_status(fd_in, id);
270 if (status != SSH2_FX_OK)
271 error("Couldn't close file: %s", fx2txt(status));
279 do_ls(int fd_in, int fd_out, char *path)
282 u_int type, id, handle_len, i, expected_id;
288 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
289 buffer_put_int(&msg, id);
290 buffer_put_cstring(&msg, path);
291 send_msg(fd_out, &msg);
295 handle = get_handle(fd_in, id, &handle_len);
302 id = expected_id = msg_id++;
304 debug3("Sending SSH2_FXP_READDIR I:%d", id);
307 buffer_put_char(&msg, SSH2_FXP_READDIR);
308 buffer_put_int(&msg, id);
309 buffer_put_string(&msg, handle, handle_len);
310 send_msg(fd_out, &msg);
314 get_msg(fd_in, &msg);
316 type = buffer_get_char(&msg);
317 id = buffer_get_int(&msg);
319 debug3("Received reply T:%d I:%d", type, id);
321 if (id != expected_id)
322 fatal("ID mismatch (%d != %d)", id, expected_id);
324 if (type == SSH2_FXP_STATUS) {
325 int status = buffer_get_int(&msg);
327 debug3("Received SSH2_FXP_STATUS %d", status);
329 if (status == SSH2_FX_EOF) {
332 error("Couldn't read directory: %s",
334 do_close(fd_in, fd_out, handle, handle_len);
337 } else if (type != SSH2_FXP_NAME)
338 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
339 SSH2_FXP_NAME, type);
341 count = buffer_get_int(&msg);
344 debug3("Received %d SSH2_FXP_NAME responses", count);
345 for(i = 0; i < count; i++) {
346 char *filename, *longname;
349 filename = buffer_get_string(&msg, NULL);
350 longname = buffer_get_string(&msg, NULL);
351 a = decode_attrib(&msg);
353 printf("%s\n", longname);
361 do_close(fd_in, fd_out, handle, handle_len);
368 do_rm(int fd_in, int fd_out, char *path)
372 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
375 send_string_request(fd_out, id, SSH2_FXP_REMOVE, path, strlen(path));
376 status = get_status(fd_in, id);
377 if (status != SSH2_FX_OK)
378 error("Couldn't delete file: %s", fx2txt(status));
383 do_mkdir(int fd_in, int fd_out, char *path, Attrib *a)
388 send_string_attrs_request(fd_out, id, SSH2_FXP_MKDIR, path,
391 status = get_status(fd_in, id);
392 if (status != SSH2_FX_OK)
393 error("Couldn't create directory: %s", fx2txt(status));
399 do_rmdir(int fd_in, int fd_out, char *path)
404 send_string_request(fd_out, id, SSH2_FXP_RMDIR, path, strlen(path));
406 status = get_status(fd_in, id);
407 if (status != SSH2_FX_OK)
408 error("Couldn't remove directory: %s", fx2txt(status));
414 do_stat(int fd_in, int fd_out, char *path)
419 send_string_request(fd_out, id, SSH2_FXP_STAT, path, strlen(path));
420 return(get_decode_stat(fd_in, id));
424 do_lstat(int fd_in, int fd_out, char *path)
429 send_string_request(fd_out, id, SSH2_FXP_LSTAT, path, strlen(path));
430 return(get_decode_stat(fd_in, id));
434 do_fstat(int fd_in, int fd_out, char *handle,
440 send_string_request(fd_out, id, SSH2_FXP_FSTAT, handle, handle_len);
441 return(get_decode_stat(fd_in, id));
445 do_setstat(int fd_in, int fd_out, char *path, Attrib *a)
450 send_string_attrs_request(fd_out, id, SSH2_FXP_SETSTAT, path,
453 status = get_status(fd_in, id);
454 if (status != SSH2_FX_OK)
455 error("Couldn't setstat on \"%s\": %s", path,
462 do_fsetstat(int fd_in, int fd_out, char *handle, u_int handle_len,
468 send_string_attrs_request(fd_out, id, SSH2_FXP_FSETSTAT, handle,
471 status = get_status(fd_in, id);
472 if (status != SSH2_FX_OK)
473 error("Couldn't fsetstat: %s", fx2txt(status));
479 do_realpath(int fd_in, int fd_out, char *path)
482 u_int type, expected_id, count, id;
483 char *filename, *longname;
486 expected_id = id = msg_id++;
487 send_string_request(fd_out, id, SSH2_FXP_REALPATH, path, strlen(path));
491 get_msg(fd_in, &msg);
492 type = buffer_get_char(&msg);
493 id = buffer_get_int(&msg);
495 if (id != expected_id)
496 fatal("ID mismatch (%d != %d)", id, expected_id);
498 if (type == SSH2_FXP_STATUS) {
499 u_int status = buffer_get_int(&msg);
501 error("Couldn't canonicalise: %s", fx2txt(status));
503 } else if (type != SSH2_FXP_NAME)
504 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
505 SSH2_FXP_NAME, type);
507 count = buffer_get_int(&msg);
509 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
511 filename = buffer_get_string(&msg, NULL);
512 longname = buffer_get_string(&msg, NULL);
513 a = decode_attrib(&msg);
515 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
525 do_rename(int fd_in, int fd_out, char *oldpath, char *newpath)
532 /* Send rename request */
534 buffer_put_char(&msg, SSH2_FXP_RENAME);
535 buffer_put_int(&msg, id);
536 buffer_put_cstring(&msg, oldpath);
537 buffer_put_cstring(&msg, newpath);
538 send_msg(fd_out, &msg);
539 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
543 status = get_status(fd_in, id);
544 if (status != SSH2_FX_OK)
545 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, newpath,
552 do_symlink(int fd_in, int fd_out, char *oldpath, char *newpath)
559 /* Send rename request */
561 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
562 buffer_put_int(&msg, id);
563 buffer_put_cstring(&msg, oldpath);
564 buffer_put_cstring(&msg, newpath);
565 send_msg(fd_out, &msg);
566 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
570 status = get_status(fd_in, id);
571 if (status != SSH2_FX_OK)
572 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, newpath,
579 do_readlink(int fd_in, int fd_out, char *path)
582 u_int type, expected_id, count, id;
583 char *filename, *longname;
586 expected_id = id = msg_id++;
587 send_string_request(fd_out, id, SSH2_FXP_READLINK, path, strlen(path));
591 get_msg(fd_in, &msg);
592 type = buffer_get_char(&msg);
593 id = buffer_get_int(&msg);
595 if (id != expected_id)
596 fatal("ID mismatch (%d != %d)", id, expected_id);
598 if (type == SSH2_FXP_STATUS) {
599 u_int status = buffer_get_int(&msg);
601 error("Couldn't readlink: %s", fx2txt(status));
603 } else if (type != SSH2_FXP_NAME)
604 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
605 SSH2_FXP_NAME, type);
607 count = buffer_get_int(&msg);
609 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
611 filename = buffer_get_string(&msg, NULL);
612 longname = buffer_get_string(&msg, NULL);
613 a = decode_attrib(&msg);
615 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
625 do_download(int fd_in, int fd_out, char *remote_path, char *local_path,
629 u_int expected_id, handle_len, mode, type, id;
636 a = do_stat(fd_in, fd_out, remote_path);
640 /* XXX: should we preserve set[ug]id? */
641 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
642 mode = S_IWRITE | (a->perm & 0777);
646 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
647 if (local_fd == -1) {
648 error("Couldn't open local file \"%s\" for writing: %s",
649 local_path, strerror(errno));
655 /* Send open request */
657 buffer_put_char(&msg, SSH2_FXP_OPEN);
658 buffer_put_int(&msg, id);
659 buffer_put_cstring(&msg, remote_path);
660 buffer_put_int(&msg, SSH2_FXF_READ);
661 attrib_clear(&junk); /* Send empty attributes */
662 encode_attrib(&msg, &junk);
663 send_msg(fd_out, &msg);
664 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
666 handle = get_handle(fd_in, id, &handle_len);
667 if (handle == NULL) {
673 /* Read from remote and write to local */
679 id = expected_id = msg_id++;
682 buffer_put_char(&msg, SSH2_FXP_READ);
683 buffer_put_int(&msg, id);
684 buffer_put_string(&msg, handle, handle_len);
685 buffer_put_int64(&msg, offset);
686 buffer_put_int(&msg, COPY_SIZE);
687 send_msg(fd_out, &msg);
688 debug3("Sent message SSH2_FXP_READ I:%d O:%llu S:%u",
689 id, (u_int64_t)offset, COPY_SIZE);
693 get_msg(fd_in, &msg);
694 type = buffer_get_char(&msg);
695 id = buffer_get_int(&msg);
696 debug3("Received reply T:%d I:%d", type, id);
697 if (id != expected_id)
698 fatal("ID mismatch (%d != %d)", id, expected_id);
699 if (type == SSH2_FXP_STATUS) {
700 status = buffer_get_int(&msg);
702 if (status == SSH2_FX_EOF)
705 error("Couldn't read from remote "
706 "file \"%s\" : %s", remote_path,
708 do_close(fd_in, fd_out, handle, handle_len);
711 } else if (type != SSH2_FXP_DATA) {
712 fatal("Expected SSH2_FXP_DATA(%d) packet, got %d",
713 SSH2_FXP_DATA, type);
716 data = buffer_get_string(&msg, &len);
718 fatal("Received more data than asked for %d > %d",
721 debug3("In read loop, got %d offset %llu", len,
723 if (atomicio(write, local_fd, data, len) != len) {
724 error("Couldn't write to \"%s\": %s", local_path,
726 do_close(fd_in, fd_out, handle, handle_len);
735 status = do_close(fd_in, fd_out, handle, handle_len);
737 /* Override umask and utimes if asked */
739 if (pflag && fchmod(local_fd, mode) == -1)
741 if (pflag && chmod(local_path, mode) == -1)
742 #endif /* HAVE_FCHMOD */
743 error("Couldn't set mode on \"%s\": %s", local_path,
745 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
746 struct timeval tv[2];
747 tv[0].tv_sec = a->atime;
748 tv[1].tv_sec = a->mtime;
749 tv[0].tv_usec = tv[1].tv_usec = 0;
750 if (utimes(local_path, tv) == -1)
751 error("Can't set times on \"%s\": %s", local_path,
763 do_upload(int fd_in, int fd_out, char *local_path, char *remote_path,
767 u_int handle_len, id;
775 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
776 error("Couldn't open local file \"%s\" for reading: %s",
777 local_path, strerror(errno));
780 if (fstat(local_fd, &sb) == -1) {
781 error("Couldn't fstat local file \"%s\": %s",
782 local_path, strerror(errno));
786 stat_to_attrib(&sb, &a);
788 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
789 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
792 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
796 /* Send open request */
798 buffer_put_char(&msg, SSH2_FXP_OPEN);
799 buffer_put_int(&msg, id);
800 buffer_put_cstring(&msg, remote_path);
801 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
802 encode_attrib(&msg, &a);
803 send_msg(fd_out, &msg);
804 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
808 handle = get_handle(fd_in, id, &handle_len);
809 if (handle == NULL) {
815 /* Read from local and write to remote */
819 char data[COPY_SIZE];
822 * Can't use atomicio here because it returns 0 on EOF, thus losing
823 * the last block of the file
826 len = read(local_fd, data, COPY_SIZE);
827 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
830 fatal("Couldn't read from \"%s\": %s", local_path,
836 buffer_put_char(&msg, SSH2_FXP_WRITE);
837 buffer_put_int(&msg, ++id);
838 buffer_put_string(&msg, handle, handle_len);
839 buffer_put_int64(&msg, offset);
840 buffer_put_string(&msg, data, len);
841 send_msg(fd_out, &msg);
842 debug3("Sent message SSH2_FXP_WRITE I:%d O:%llu S:%u",
843 id, (u_int64_t)offset, len);
845 status = get_status(fd_in, id);
846 if (status != SSH2_FX_OK) {
847 error("Couldn't write to remote file \"%s\": %s",
848 remote_path, fx2txt(status));
849 do_close(fd_in, fd_out, handle, handle_len);
853 debug3("In write loop, got %d offset %llu", len,
859 if (close(local_fd) == -1) {
860 error("Couldn't close local file \"%s\": %s", local_path,
862 do_close(fd_in, fd_out, handle, handle_len);
867 /* Override umask and utimes if asked */
869 do_fsetstat(fd_in, fd_out, handle, handle_len, &a);
871 status = do_close(fd_in, fd_out, handle, handle_len);