No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / external / bsd / openssh / dist / sftp-client.c
blob1877534ad11757d0f343c83dd034094a8d6deed5
1 /* $NetBSD: sftp-client.c,v 1.1.1.2 2009/12/27 01:07:03 christos Exp $ */
2 /* $OpenBSD: sftp-client.c,v 1.87 2009/06/22 05:39:28 dtucker Exp $ */
3 /*
4 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 /* XXX: memleaks */
20 /* XXX: signed vs unsigned */
21 /* XXX: remove all logging, only return status codes */
22 /* XXX: copy between two remote sites */
24 #include "includes.h"
25 __RCSID("$NetBSD: sftp-client.c,v 1.2 2009/06/07 22:38:47 christos Exp $");
26 #include <sys/types.h>
27 #include <sys/queue.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30 #include <sys/param.h>
31 #include <sys/statvfs.h>
32 #include <sys/uio.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <signal.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
42 #include "xmalloc.h"
43 #include "buffer.h"
44 #include "log.h"
45 #include "atomicio.h"
46 #include "progressmeter.h"
47 #include "misc.h"
49 #include "sftp.h"
50 #include "sftp-common.h"
51 #include "sftp-client.h"
53 extern volatile sig_atomic_t interrupted;
54 extern int showprogress;
56 /* Minimum amount of data to read at a time */
57 #define MIN_READ_SIZE 512
59 struct sftp_conn {
60 int fd_in;
61 int fd_out;
62 u_int transfer_buflen;
63 u_int num_requests;
64 u_int version;
65 u_int msg_id;
66 #define SFTP_EXT_POSIX_RENAME 0x00000001
67 #define SFTP_EXT_STATVFS 0x00000002
68 #define SFTP_EXT_FSTATVFS 0x00000004
69 u_int exts;
72 static void
73 send_msg(int fd, Buffer *m)
75 u_char mlen[4];
76 struct iovec iov[2];
78 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
79 fatal("Outbound message too long %u", buffer_len(m));
81 /* Send length first */
82 put_u32(mlen, buffer_len(m));
83 iov[0].iov_base = mlen;
84 iov[0].iov_len = sizeof(mlen);
85 iov[1].iov_base = buffer_ptr(m);
86 iov[1].iov_len = buffer_len(m);
88 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
89 fatal("Couldn't send packet: %s", strerror(errno));
91 buffer_clear(m);
94 static void
95 get_msg(int fd, Buffer *m)
97 u_int msg_len;
99 buffer_append_space(m, 4);
100 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
101 if (errno == EPIPE)
102 fatal("Connection closed");
103 else
104 fatal("Couldn't read packet: %s", strerror(errno));
107 msg_len = buffer_get_int(m);
108 if (msg_len > SFTP_MAX_MSG_LENGTH)
109 fatal("Received message too long %u", msg_len);
111 buffer_append_space(m, msg_len);
112 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
113 if (errno == EPIPE)
114 fatal("Connection closed");
115 else
116 fatal("Read packet: %s", strerror(errno));
120 static void
121 send_string_request(int fd, u_int id, u_int code, char *s,
122 u_int len)
124 Buffer msg;
126 buffer_init(&msg);
127 buffer_put_char(&msg, code);
128 buffer_put_int(&msg, id);
129 buffer_put_string(&msg, s, len);
130 send_msg(fd, &msg);
131 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
132 buffer_free(&msg);
135 static void
136 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
137 u_int len, Attrib *a)
139 Buffer msg;
141 buffer_init(&msg);
142 buffer_put_char(&msg, code);
143 buffer_put_int(&msg, id);
144 buffer_put_string(&msg, s, len);
145 encode_attrib(&msg, a);
146 send_msg(fd, &msg);
147 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
148 buffer_free(&msg);
151 static u_int
152 get_status(int fd, u_int expected_id)
154 Buffer msg;
155 u_int type, id, status;
157 buffer_init(&msg);
158 get_msg(fd, &msg);
159 type = buffer_get_char(&msg);
160 id = buffer_get_int(&msg);
162 if (id != expected_id)
163 fatal("ID mismatch (%u != %u)", id, expected_id);
164 if (type != SSH2_FXP_STATUS)
165 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
166 SSH2_FXP_STATUS, type);
168 status = buffer_get_int(&msg);
169 buffer_free(&msg);
171 debug3("SSH2_FXP_STATUS %u", status);
173 return(status);
176 static char *
177 get_handle(int fd, u_int expected_id, u_int *len)
179 Buffer msg;
180 u_int type, id;
181 char *handle;
183 buffer_init(&msg);
184 get_msg(fd, &msg);
185 type = buffer_get_char(&msg);
186 id = buffer_get_int(&msg);
188 if (id != expected_id)
189 fatal("ID mismatch (%u != %u)", id, expected_id);
190 if (type == SSH2_FXP_STATUS) {
191 int status = buffer_get_int(&msg);
193 error("Couldn't get handle: %s", fx2txt(status));
194 buffer_free(&msg);
195 return(NULL);
196 } else if (type != SSH2_FXP_HANDLE)
197 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
198 SSH2_FXP_HANDLE, type);
200 handle = buffer_get_string(&msg, len);
201 buffer_free(&msg);
203 return(handle);
206 static Attrib *
207 get_decode_stat(int fd, u_int expected_id, int quiet)
209 Buffer msg;
210 u_int type, id;
211 Attrib *a;
213 buffer_init(&msg);
214 get_msg(fd, &msg);
216 type = buffer_get_char(&msg);
217 id = buffer_get_int(&msg);
219 debug3("Received stat reply T:%u I:%u", type, id);
220 if (id != expected_id)
221 fatal("ID mismatch (%u != %u)", id, expected_id);
222 if (type == SSH2_FXP_STATUS) {
223 int status = buffer_get_int(&msg);
225 if (quiet)
226 debug("Couldn't stat remote file: %s", fx2txt(status));
227 else
228 error("Couldn't stat remote file: %s", fx2txt(status));
229 buffer_free(&msg);
230 return(NULL);
231 } else if (type != SSH2_FXP_ATTRS) {
232 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
233 SSH2_FXP_ATTRS, type);
235 a = decode_attrib(&msg);
236 buffer_free(&msg);
238 return(a);
241 static int
242 get_decode_statvfs(int fd, struct sftp_statvfs *st, u_int expected_id,
243 int quiet)
245 Buffer msg;
246 u_int type, id, flag;
248 buffer_init(&msg);
249 get_msg(fd, &msg);
251 type = buffer_get_char(&msg);
252 id = buffer_get_int(&msg);
254 debug3("Received statvfs reply T:%u I:%u", type, id);
255 if (id != expected_id)
256 fatal("ID mismatch (%u != %u)", id, expected_id);
257 if (type == SSH2_FXP_STATUS) {
258 int status = buffer_get_int(&msg);
260 if (quiet)
261 debug("Couldn't statvfs: %s", fx2txt(status));
262 else
263 error("Couldn't statvfs: %s", fx2txt(status));
264 buffer_free(&msg);
265 return -1;
266 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
267 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
268 SSH2_FXP_EXTENDED_REPLY, type);
271 bzero(st, sizeof(*st));
272 st->f_bsize = buffer_get_int64(&msg);
273 st->f_frsize = buffer_get_int64(&msg);
274 st->f_blocks = buffer_get_int64(&msg);
275 st->f_bfree = buffer_get_int64(&msg);
276 st->f_bavail = buffer_get_int64(&msg);
277 st->f_files = buffer_get_int64(&msg);
278 st->f_ffree = buffer_get_int64(&msg);
279 st->f_favail = buffer_get_int64(&msg);
280 st->f_fsid = buffer_get_int64(&msg);
281 flag = buffer_get_int64(&msg);
282 st->f_namemax = buffer_get_int64(&msg);
284 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
285 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
287 buffer_free(&msg);
289 return 0;
292 struct sftp_conn *
293 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
295 u_int type, exts = 0;
296 int version;
297 Buffer msg;
298 struct sftp_conn *ret;
300 buffer_init(&msg);
301 buffer_put_char(&msg, SSH2_FXP_INIT);
302 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
303 send_msg(fd_out, &msg);
305 buffer_clear(&msg);
307 get_msg(fd_in, &msg);
309 /* Expecting a VERSION reply */
310 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
311 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
312 type);
313 buffer_free(&msg);
314 return(NULL);
316 version = buffer_get_int(&msg);
318 debug2("Remote version: %d", version);
320 /* Check for extensions */
321 while (buffer_len(&msg) > 0) {
322 char *name = buffer_get_string(&msg, NULL);
323 char *value = buffer_get_string(&msg, NULL);
324 int known = 0;
326 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
327 strcmp(value, "1") == 0) {
328 exts |= SFTP_EXT_POSIX_RENAME;
329 known = 1;
330 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
331 strcmp(value, "2") == 0) {
332 exts |= SFTP_EXT_STATVFS;
333 known = 1;
334 } if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
335 strcmp(value, "2") == 0) {
336 exts |= SFTP_EXT_FSTATVFS;
337 known = 1;
339 if (known) {
340 debug2("Server supports extension \"%s\" revision %s",
341 name, value);
342 } else {
343 debug2("Unrecognised server extension \"%s\"", name);
345 xfree(name);
346 xfree(value);
349 buffer_free(&msg);
351 ret = xmalloc(sizeof(*ret));
352 ret->fd_in = fd_in;
353 ret->fd_out = fd_out;
354 ret->transfer_buflen = transfer_buflen;
355 ret->num_requests = num_requests;
356 ret->version = version;
357 ret->msg_id = 1;
358 ret->exts = exts;
360 /* Some filexfer v.0 servers don't support large packets */
361 if (version == 0)
362 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
364 return(ret);
367 u_int
368 sftp_proto_version(struct sftp_conn *conn)
370 return(conn->version);
374 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
376 u_int id, status;
377 Buffer msg;
379 buffer_init(&msg);
381 id = conn->msg_id++;
382 buffer_put_char(&msg, SSH2_FXP_CLOSE);
383 buffer_put_int(&msg, id);
384 buffer_put_string(&msg, handle, handle_len);
385 send_msg(conn->fd_out, &msg);
386 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
388 status = get_status(conn->fd_in, id);
389 if (status != SSH2_FX_OK)
390 error("Couldn't close file: %s", fx2txt(status));
392 buffer_free(&msg);
394 return(status);
398 static int
399 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
400 SFTP_DIRENT ***dir)
402 Buffer msg;
403 u_int count, type, id, handle_len, i, expected_id, ents = 0;
404 char *handle;
406 id = conn->msg_id++;
408 buffer_init(&msg);
409 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
410 buffer_put_int(&msg, id);
411 buffer_put_cstring(&msg, path);
412 send_msg(conn->fd_out, &msg);
414 buffer_clear(&msg);
416 handle = get_handle(conn->fd_in, id, &handle_len);
417 if (handle == NULL)
418 return(-1);
420 if (dir) {
421 ents = 0;
422 *dir = xmalloc(sizeof(**dir));
423 (*dir)[0] = NULL;
426 for (; !interrupted;) {
427 id = expected_id = conn->msg_id++;
429 debug3("Sending SSH2_FXP_READDIR I:%u", id);
431 buffer_clear(&msg);
432 buffer_put_char(&msg, SSH2_FXP_READDIR);
433 buffer_put_int(&msg, id);
434 buffer_put_string(&msg, handle, handle_len);
435 send_msg(conn->fd_out, &msg);
437 buffer_clear(&msg);
439 get_msg(conn->fd_in, &msg);
441 type = buffer_get_char(&msg);
442 id = buffer_get_int(&msg);
444 debug3("Received reply T:%u I:%u", type, id);
446 if (id != expected_id)
447 fatal("ID mismatch (%u != %u)", id, expected_id);
449 if (type == SSH2_FXP_STATUS) {
450 int status = buffer_get_int(&msg);
452 debug3("Received SSH2_FXP_STATUS %d", status);
454 if (status == SSH2_FX_EOF) {
455 break;
456 } else {
457 error("Couldn't read directory: %s",
458 fx2txt(status));
459 do_close(conn, handle, handle_len);
460 xfree(handle);
461 return(status);
463 } else if (type != SSH2_FXP_NAME)
464 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
465 SSH2_FXP_NAME, type);
467 count = buffer_get_int(&msg);
468 if (count == 0)
469 break;
470 debug3("Received %d SSH2_FXP_NAME responses", count);
471 for (i = 0; i < count; i++) {
472 char *filename, *longname;
473 Attrib *a;
475 filename = buffer_get_string(&msg, NULL);
476 longname = buffer_get_string(&msg, NULL);
477 a = decode_attrib(&msg);
479 if (printflag)
480 printf("%s\n", longname);
482 if (dir) {
483 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
484 (*dir)[ents] = xmalloc(sizeof(***dir));
485 (*dir)[ents]->filename = xstrdup(filename);
486 (*dir)[ents]->longname = xstrdup(longname);
487 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
488 (*dir)[++ents] = NULL;
491 xfree(filename);
492 xfree(longname);
496 buffer_free(&msg);
497 do_close(conn, handle, handle_len);
498 xfree(handle);
500 /* Don't return partial matches on interrupt */
501 if (interrupted && dir != NULL && *dir != NULL) {
502 free_sftp_dirents(*dir);
503 *dir = xmalloc(sizeof(**dir));
504 **dir = NULL;
507 return(0);
511 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
513 return(do_lsreaddir(conn, path, 0, dir));
516 void free_sftp_dirents(SFTP_DIRENT **s)
518 int i;
520 for (i = 0; s[i]; i++) {
521 xfree(s[i]->filename);
522 xfree(s[i]->longname);
523 xfree(s[i]);
525 xfree(s);
529 do_rm(struct sftp_conn *conn, char *path)
531 u_int status, id;
533 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
535 id = conn->msg_id++;
536 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
537 strlen(path));
538 status = get_status(conn->fd_in, id);
539 if (status != SSH2_FX_OK)
540 error("Couldn't delete file: %s", fx2txt(status));
541 return(status);
545 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
547 u_int status, id;
549 id = conn->msg_id++;
550 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
551 strlen(path), a);
553 status = get_status(conn->fd_in, id);
554 if (status != SSH2_FX_OK)
555 error("Couldn't create directory: %s", fx2txt(status));
557 return(status);
561 do_rmdir(struct sftp_conn *conn, char *path)
563 u_int status, id;
565 id = conn->msg_id++;
566 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
567 strlen(path));
569 status = get_status(conn->fd_in, id);
570 if (status != SSH2_FX_OK)
571 error("Couldn't remove directory: %s", fx2txt(status));
573 return(status);
576 Attrib *
577 do_stat(struct sftp_conn *conn, char *path, int quiet)
579 u_int id;
581 id = conn->msg_id++;
583 send_string_request(conn->fd_out, id,
584 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
585 path, strlen(path));
587 return(get_decode_stat(conn->fd_in, id, quiet));
590 Attrib *
591 do_lstat(struct sftp_conn *conn, char *path, int quiet)
593 u_int id;
595 if (conn->version == 0) {
596 if (quiet)
597 debug("Server version does not support lstat operation");
598 else
599 logit("Server version does not support lstat operation");
600 return(do_stat(conn, path, quiet));
603 id = conn->msg_id++;
604 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
605 strlen(path));
607 return(get_decode_stat(conn->fd_in, id, quiet));
610 #ifdef notyet
611 Attrib *
612 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
614 u_int id;
616 id = conn->msg_id++;
617 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
618 handle_len);
620 return(get_decode_stat(conn->fd_in, id, quiet));
622 #endif
625 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
627 u_int status, id;
629 id = conn->msg_id++;
630 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
631 strlen(path), a);
633 status = get_status(conn->fd_in, id);
634 if (status != SSH2_FX_OK)
635 error("Couldn't setstat on \"%s\": %s", path,
636 fx2txt(status));
638 return(status);
642 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
643 Attrib *a)
645 u_int status, id;
647 id = conn->msg_id++;
648 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
649 handle_len, a);
651 status = get_status(conn->fd_in, id);
652 if (status != SSH2_FX_OK)
653 error("Couldn't fsetstat: %s", fx2txt(status));
655 return(status);
658 char *
659 do_realpath(struct sftp_conn *conn, char *path)
661 Buffer msg;
662 u_int type, expected_id, count, id;
663 char *filename, *longname;
664 Attrib *a;
666 expected_id = id = conn->msg_id++;
667 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
668 strlen(path));
670 buffer_init(&msg);
672 get_msg(conn->fd_in, &msg);
673 type = buffer_get_char(&msg);
674 id = buffer_get_int(&msg);
676 if (id != expected_id)
677 fatal("ID mismatch (%u != %u)", id, expected_id);
679 if (type == SSH2_FXP_STATUS) {
680 u_int status = buffer_get_int(&msg);
682 error("Couldn't canonicalise: %s", fx2txt(status));
683 return(NULL);
684 } else if (type != SSH2_FXP_NAME)
685 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
686 SSH2_FXP_NAME, type);
688 count = buffer_get_int(&msg);
689 if (count != 1)
690 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
692 filename = buffer_get_string(&msg, NULL);
693 longname = buffer_get_string(&msg, NULL);
694 a = decode_attrib(&msg);
696 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
698 xfree(longname);
700 buffer_free(&msg);
702 return(filename);
706 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
708 Buffer msg;
709 u_int status, id;
711 buffer_init(&msg);
713 /* Send rename request */
714 id = conn->msg_id++;
715 if ((conn->exts & SFTP_EXT_POSIX_RENAME)) {
716 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
717 buffer_put_int(&msg, id);
718 buffer_put_cstring(&msg, "posix-rename@openssh.com");
719 } else {
720 buffer_put_char(&msg, SSH2_FXP_RENAME);
721 buffer_put_int(&msg, id);
723 buffer_put_cstring(&msg, oldpath);
724 buffer_put_cstring(&msg, newpath);
725 send_msg(conn->fd_out, &msg);
726 debug3("Sent message %s \"%s\" -> \"%s\"",
727 (conn->exts & SFTP_EXT_POSIX_RENAME) ? "posix-rename@openssh.com" :
728 "SSH2_FXP_RENAME", oldpath, newpath);
729 buffer_free(&msg);
731 status = get_status(conn->fd_in, id);
732 if (status != SSH2_FX_OK)
733 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
734 newpath, fx2txt(status));
736 return(status);
740 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
742 Buffer msg;
743 u_int status, id;
745 if (conn->version < 3) {
746 error("This server does not support the symlink operation");
747 return(SSH2_FX_OP_UNSUPPORTED);
750 buffer_init(&msg);
752 /* Send symlink request */
753 id = conn->msg_id++;
754 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
755 buffer_put_int(&msg, id);
756 buffer_put_cstring(&msg, oldpath);
757 buffer_put_cstring(&msg, newpath);
758 send_msg(conn->fd_out, &msg);
759 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
760 newpath);
761 buffer_free(&msg);
763 status = get_status(conn->fd_in, id);
764 if (status != SSH2_FX_OK)
765 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
766 newpath, fx2txt(status));
768 return(status);
771 #ifdef notyet
772 char *
773 do_readlink(struct sftp_conn *conn, char *path)
775 Buffer msg;
776 u_int type, expected_id, count, id;
777 char *filename, *longname;
778 Attrib *a;
780 expected_id = id = conn->msg_id++;
781 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
782 strlen(path));
784 buffer_init(&msg);
786 get_msg(conn->fd_in, &msg);
787 type = buffer_get_char(&msg);
788 id = buffer_get_int(&msg);
790 if (id != expected_id)
791 fatal("ID mismatch (%u != %u)", id, expected_id);
793 if (type == SSH2_FXP_STATUS) {
794 u_int status = buffer_get_int(&msg);
796 error("Couldn't readlink: %s", fx2txt(status));
797 return(NULL);
798 } else if (type != SSH2_FXP_NAME)
799 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
800 SSH2_FXP_NAME, type);
802 count = buffer_get_int(&msg);
803 if (count != 1)
804 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
806 filename = buffer_get_string(&msg, NULL);
807 longname = buffer_get_string(&msg, NULL);
808 a = decode_attrib(&msg);
810 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
812 xfree(longname);
814 buffer_free(&msg);
816 return(filename);
818 #endif
821 do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
822 int quiet)
824 Buffer msg;
825 u_int id;
827 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
828 error("Server does not support statvfs@openssh.com extension");
829 return -1;
832 id = conn->msg_id++;
834 buffer_init(&msg);
835 buffer_clear(&msg);
836 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
837 buffer_put_int(&msg, id);
838 buffer_put_cstring(&msg, "statvfs@openssh.com");
839 buffer_put_cstring(&msg, path);
840 send_msg(conn->fd_out, &msg);
841 buffer_free(&msg);
843 return get_decode_statvfs(conn->fd_in, st, id, quiet);
846 #ifdef notyet
848 do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
849 struct sftp_statvfs *st, int quiet)
851 Buffer msg;
852 u_int id;
854 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
855 error("Server does not support fstatvfs@openssh.com extension");
856 return -1;
859 id = conn->msg_id++;
861 buffer_init(&msg);
862 buffer_clear(&msg);
863 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
864 buffer_put_int(&msg, id);
865 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
866 buffer_put_string(&msg, handle, handle_len);
867 send_msg(conn->fd_out, &msg);
868 buffer_free(&msg);
870 return get_decode_statvfs(conn->fd_in, st, id, quiet);
872 #endif
874 static void
875 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
876 char *handle, u_int handle_len)
878 Buffer msg;
880 buffer_init(&msg);
881 buffer_clear(&msg);
882 buffer_put_char(&msg, SSH2_FXP_READ);
883 buffer_put_int(&msg, id);
884 buffer_put_string(&msg, handle, handle_len);
885 buffer_put_int64(&msg, offset);
886 buffer_put_int(&msg, len);
887 send_msg(fd_out, &msg);
888 buffer_free(&msg);
892 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
893 int pflag)
895 Attrib junk, *a;
896 Buffer msg;
897 char *handle;
898 int local_fd, status = 0, write_error;
899 int read_error, write_errno;
900 u_int64_t offset, size;
901 u_int handle_len, mode, type, id, buflen, num_req, max_req;
902 off_t progress_counter;
903 struct request {
904 u_int id;
905 u_int len;
906 u_int64_t offset;
907 TAILQ_ENTRY(request) tq;
909 TAILQ_HEAD(reqhead, request) requests;
910 struct request *req;
912 status = -1;
913 TAILQ_INIT(&requests);
915 a = do_stat(conn, remote_path, 0);
916 if (a == NULL)
917 return(-1);
919 /* Do not preserve set[ug]id here, as we do not preserve ownership */
920 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
921 mode = a->perm & 0777;
922 else
923 mode = 0666;
925 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
926 (!S_ISREG(a->perm))) {
927 error("Cannot download non-regular file: %s", remote_path);
928 return(-1);
931 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
932 size = a->size;
933 else
934 size = 0;
936 buflen = conn->transfer_buflen;
937 buffer_init(&msg);
939 /* Send open request */
940 id = conn->msg_id++;
941 buffer_put_char(&msg, SSH2_FXP_OPEN);
942 buffer_put_int(&msg, id);
943 buffer_put_cstring(&msg, remote_path);
944 buffer_put_int(&msg, SSH2_FXF_READ);
945 attrib_clear(&junk); /* Send empty attributes */
946 encode_attrib(&msg, &junk);
947 send_msg(conn->fd_out, &msg);
948 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
950 handle = get_handle(conn->fd_in, id, &handle_len);
951 if (handle == NULL) {
952 buffer_free(&msg);
953 return(-1);
956 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
957 mode | S_IWRITE);
958 if (local_fd == -1) {
959 error("Couldn't open local file \"%s\" for writing: %s",
960 local_path, strerror(errno));
961 do_close(conn, handle, handle_len);
962 buffer_free(&msg);
963 xfree(handle);
964 return(-1);
967 /* Read from remote and write to local */
968 write_error = read_error = write_errno = num_req = offset = 0;
969 max_req = 1;
970 progress_counter = 0;
972 if (showprogress && size != 0)
973 start_progress_meter(remote_path, size, &progress_counter);
975 while (num_req > 0 || max_req > 0) {
976 char *data;
977 u_int len;
980 * Simulate EOF on interrupt: stop sending new requests and
981 * allow outstanding requests to drain gracefully
983 if (interrupted) {
984 if (num_req == 0) /* If we haven't started yet... */
985 break;
986 max_req = 0;
989 /* Send some more requests */
990 while (num_req < max_req) {
991 debug3("Request range %llu -> %llu (%d/%d)",
992 (unsigned long long)offset,
993 (unsigned long long)offset + buflen - 1,
994 num_req, max_req);
995 req = xmalloc(sizeof(*req));
996 req->id = conn->msg_id++;
997 req->len = buflen;
998 req->offset = offset;
999 offset += buflen;
1000 num_req++;
1001 TAILQ_INSERT_TAIL(&requests, req, tq);
1002 send_read_request(conn->fd_out, req->id, req->offset,
1003 req->len, handle, handle_len);
1006 buffer_clear(&msg);
1007 get_msg(conn->fd_in, &msg);
1008 type = buffer_get_char(&msg);
1009 id = buffer_get_int(&msg);
1010 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1012 /* Find the request in our queue */
1013 for (req = TAILQ_FIRST(&requests);
1014 req != NULL && req->id != id;
1015 req = TAILQ_NEXT(req, tq))
1017 if (req == NULL)
1018 fatal("Unexpected reply %u", id);
1020 switch (type) {
1021 case SSH2_FXP_STATUS:
1022 status = buffer_get_int(&msg);
1023 if (status != SSH2_FX_EOF)
1024 read_error = 1;
1025 max_req = 0;
1026 TAILQ_REMOVE(&requests, req, tq);
1027 xfree(req);
1028 num_req--;
1029 break;
1030 case SSH2_FXP_DATA:
1031 data = buffer_get_string(&msg, &len);
1032 debug3("Received data %llu -> %llu",
1033 (unsigned long long)req->offset,
1034 (unsigned long long)req->offset + len - 1);
1035 if (len > req->len)
1036 fatal("Received more data than asked for "
1037 "%u > %u", len, req->len);
1038 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1039 atomicio(vwrite, local_fd, data, len) != len) &&
1040 !write_error) {
1041 write_errno = errno;
1042 write_error = 1;
1043 max_req = 0;
1045 progress_counter += len;
1046 xfree(data);
1048 if (len == req->len) {
1049 TAILQ_REMOVE(&requests, req, tq);
1050 xfree(req);
1051 num_req--;
1052 } else {
1053 /* Resend the request for the missing data */
1054 debug3("Short data block, re-requesting "
1055 "%llu -> %llu (%2d)",
1056 (unsigned long long)req->offset + len,
1057 (unsigned long long)req->offset +
1058 req->len - 1, num_req);
1059 req->id = conn->msg_id++;
1060 req->len -= len;
1061 req->offset += len;
1062 send_read_request(conn->fd_out, req->id,
1063 req->offset, req->len, handle, handle_len);
1064 /* Reduce the request size */
1065 if (len < buflen)
1066 buflen = MAX(MIN_READ_SIZE, len);
1068 if (max_req > 0) { /* max_req = 0 iff EOF received */
1069 if (size > 0 && offset > size) {
1070 /* Only one request at a time
1071 * after the expected EOF */
1072 debug3("Finish at %llu (%2d)",
1073 (unsigned long long)offset,
1074 num_req);
1075 max_req = 1;
1076 } else if (max_req <= conn->num_requests) {
1077 ++max_req;
1080 break;
1081 default:
1082 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1083 SSH2_FXP_DATA, type);
1087 if (showprogress && size)
1088 stop_progress_meter();
1090 /* Sanity check */
1091 if (TAILQ_FIRST(&requests) != NULL)
1092 fatal("Transfer complete, but requests still in queue");
1094 if (read_error) {
1095 error("Couldn't read from remote file \"%s\" : %s",
1096 remote_path, fx2txt(status));
1097 do_close(conn, handle, handle_len);
1098 } else if (write_error) {
1099 error("Couldn't write to \"%s\": %s", local_path,
1100 strerror(write_errno));
1101 status = -1;
1102 do_close(conn, handle, handle_len);
1103 } else {
1104 status = do_close(conn, handle, handle_len);
1106 /* Override umask and utimes if asked */
1107 if (pflag && fchmod(local_fd, mode) == -1)
1108 error("Couldn't set mode on \"%s\": %s", local_path,
1109 strerror(errno));
1110 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1111 struct timeval tv[2];
1112 tv[0].tv_sec = a->atime;
1113 tv[1].tv_sec = a->mtime;
1114 tv[0].tv_usec = tv[1].tv_usec = 0;
1115 if (utimes(local_path, tv) == -1)
1116 error("Can't set times on \"%s\": %s",
1117 local_path, strerror(errno));
1120 close(local_fd);
1121 buffer_free(&msg);
1122 xfree(handle);
1124 return(status);
1128 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
1129 int pflag)
1131 int local_fd;
1132 int status = SSH2_FX_OK;
1133 u_int handle_len, id, type;
1134 off_t offset;
1135 char *handle, *data;
1136 Buffer msg;
1137 struct stat sb;
1138 Attrib a;
1139 u_int32_t startid;
1140 u_int32_t ackid;
1141 struct outstanding_ack {
1142 u_int id;
1143 u_int len;
1144 off_t offset;
1145 TAILQ_ENTRY(outstanding_ack) tq;
1147 TAILQ_HEAD(ackhead, outstanding_ack) acks;
1148 struct outstanding_ack *ack = NULL;
1150 TAILQ_INIT(&acks);
1152 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1153 error("Couldn't open local file \"%s\" for reading: %s",
1154 local_path, strerror(errno));
1155 return(-1);
1157 if (fstat(local_fd, &sb) == -1) {
1158 error("Couldn't fstat local file \"%s\": %s",
1159 local_path, strerror(errno));
1160 close(local_fd);
1161 return(-1);
1163 if (!S_ISREG(sb.st_mode)) {
1164 error("%s is not a regular file", local_path);
1165 close(local_fd);
1166 return(-1);
1168 stat_to_attrib(&sb, &a);
1170 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1171 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1172 a.perm &= 0777;
1173 if (!pflag)
1174 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1176 buffer_init(&msg);
1178 /* Send open request */
1179 id = conn->msg_id++;
1180 buffer_put_char(&msg, SSH2_FXP_OPEN);
1181 buffer_put_int(&msg, id);
1182 buffer_put_cstring(&msg, remote_path);
1183 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1184 encode_attrib(&msg, &a);
1185 send_msg(conn->fd_out, &msg);
1186 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1188 buffer_clear(&msg);
1190 handle = get_handle(conn->fd_in, id, &handle_len);
1191 if (handle == NULL) {
1192 close(local_fd);
1193 buffer_free(&msg);
1194 return -1;
1197 startid = ackid = id + 1;
1198 data = xmalloc(conn->transfer_buflen);
1200 /* Read from local and write to remote */
1201 offset = 0;
1202 if (showprogress)
1203 start_progress_meter(local_path, sb.st_size, &offset);
1205 for (;;) {
1206 int len;
1209 * Can't use atomicio here because it returns 0 on EOF,
1210 * thus losing the last block of the file.
1211 * Simulate an EOF on interrupt, allowing ACKs from the
1212 * server to drain.
1214 if (interrupted || status != SSH2_FX_OK)
1215 len = 0;
1216 else do
1217 len = read(local_fd, data, conn->transfer_buflen);
1218 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1220 if (len == -1)
1221 fatal("Couldn't read from \"%s\": %s", local_path,
1222 strerror(errno));
1224 if (len != 0) {
1225 ack = xmalloc(sizeof(*ack));
1226 ack->id = ++id;
1227 ack->offset = offset;
1228 ack->len = len;
1229 TAILQ_INSERT_TAIL(&acks, ack, tq);
1231 buffer_clear(&msg);
1232 buffer_put_char(&msg, SSH2_FXP_WRITE);
1233 buffer_put_int(&msg, ack->id);
1234 buffer_put_string(&msg, handle, handle_len);
1235 buffer_put_int64(&msg, offset);
1236 buffer_put_string(&msg, data, len);
1237 send_msg(conn->fd_out, &msg);
1238 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1239 id, (unsigned long long)offset, len);
1240 } else if (TAILQ_FIRST(&acks) == NULL)
1241 break;
1243 if (ack == NULL)
1244 fatal("Unexpected ACK %u", id);
1246 if (id == startid || len == 0 ||
1247 id - ackid >= conn->num_requests) {
1248 u_int r_id;
1250 buffer_clear(&msg);
1251 get_msg(conn->fd_in, &msg);
1252 type = buffer_get_char(&msg);
1253 r_id = buffer_get_int(&msg);
1255 if (type != SSH2_FXP_STATUS)
1256 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1257 "got %d", SSH2_FXP_STATUS, type);
1259 status = buffer_get_int(&msg);
1260 debug3("SSH2_FXP_STATUS %d", status);
1262 /* Find the request in our queue */
1263 for (ack = TAILQ_FIRST(&acks);
1264 ack != NULL && ack->id != r_id;
1265 ack = TAILQ_NEXT(ack, tq))
1267 if (ack == NULL)
1268 fatal("Can't find request for ID %u", r_id);
1269 TAILQ_REMOVE(&acks, ack, tq);
1270 debug3("In write loop, ack for %u %u bytes at %lld",
1271 ack->id, ack->len, (long long)ack->offset);
1272 ++ackid;
1273 xfree(ack);
1275 offset += len;
1276 if (offset < 0)
1277 fatal("%s: offset < 0", __func__);
1279 buffer_free(&msg);
1281 if (showprogress)
1282 stop_progress_meter();
1283 xfree(data);
1285 if (status != SSH2_FX_OK) {
1286 error("Couldn't write to remote file \"%s\": %s",
1287 remote_path, fx2txt(status));
1288 status = -1;
1291 if (close(local_fd) == -1) {
1292 error("Couldn't close local file \"%s\": %s", local_path,
1293 strerror(errno));
1294 status = -1;
1297 /* Override umask and utimes if asked */
1298 if (pflag)
1299 do_fsetstat(conn, handle, handle_len, &a);
1301 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
1302 status = -1;
1303 xfree(handle);
1305 return status;