djm@openbsd.org | 07f2c7f | 2019-10-04 04:31:59 +0000 | [diff] [blame] | 1 | /* $OpenBSD: sftp-client.c,v 1.135 2019/10/04 04:31:59 djm Exp $ */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 2 | /* |
Damien Miller | 4e60ed7 | 2004-02-17 17:07:59 +1100 | [diff] [blame] | 3 | * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org> |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 4 | * |
Damien Miller | 4e60ed7 | 2004-02-17 17:07:59 +1100 | [diff] [blame] | 5 | * Permission to use, copy, modify, and distribute this software for any |
| 6 | * purpose with or without fee is hereby granted, provided that the above |
| 7 | * copyright notice and this permission notice appear in all copies. |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 8 | * |
Damien Miller | 4e60ed7 | 2004-02-17 17:07:59 +1100 | [diff] [blame] | 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | /* XXX: memleaks */ |
| 19 | /* XXX: signed vs unsigned */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 20 | /* XXX: remove all logging, only return status codes */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 21 | /* XXX: copy between two remote sites */ |
| 22 | |
| 23 | #include "includes.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 24 | |
| 25 | #include <sys/types.h> |
Darren Tucker | 5b2e2ba | 2008-06-08 09:25:28 +1000 | [diff] [blame] | 26 | #ifdef HAVE_SYS_STATVFS_H |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 27 | #include <sys/statvfs.h> |
Darren Tucker | 5b2e2ba | 2008-06-08 09:25:28 +1000 | [diff] [blame] | 28 | #endif |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 29 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 30 | #ifdef HAVE_SYS_STAT_H |
| 31 | # include <sys/stat.h> |
| 32 | #endif |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 33 | #ifdef HAVE_SYS_TIME_H |
| 34 | # include <sys/time.h> |
| 35 | #endif |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 36 | #include <sys/uio.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 37 | |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 38 | #include <dirent.h> |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 39 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 40 | #include <fcntl.h> |
| 41 | #include <signal.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 42 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 43 | #include <stdio.h> |
Damien Miller | 0600c70 | 2013-11-21 13:55:43 +1100 | [diff] [blame] | 44 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 45 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 46 | #include <unistd.h> |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 47 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 48 | #include "xmalloc.h" |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 49 | #include "ssherr.h" |
| 50 | #include "sshbuf.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 51 | #include "log.h" |
| 52 | #include "atomicio.h" |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 53 | #include "progressmeter.h" |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 54 | #include "misc.h" |
schwarze@openbsd.org | 0e059cd | 2016-05-25 23:48:45 +0000 | [diff] [blame] | 55 | #include "utf8.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 56 | |
| 57 | #include "sftp.h" |
| 58 | #include "sftp-common.h" |
| 59 | #include "sftp-client.h" |
| 60 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 61 | extern volatile sig_atomic_t interrupted; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 62 | extern int showprogress; |
| 63 | |
Damien Miller | 0c8d8f6 | 2006-03-15 11:34:25 +1100 | [diff] [blame] | 64 | /* Minimum amount of data to read at a time */ |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 65 | #define MIN_READ_SIZE 512 |
| 66 | |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 67 | /* Maximum depth to descend in directory trees */ |
| 68 | #define MAX_DIR_DEPTH 64 |
| 69 | |
Damien Miller | 89f0485 | 2017-03-20 11:53:34 +1100 | [diff] [blame] | 70 | /* Directory separator characters */ |
| 71 | #ifdef HAVE_CYGWIN |
| 72 | # define SFTP_DIRECTORY_CHARS "/\\" |
| 73 | #else /* HAVE_CYGWIN */ |
| 74 | # define SFTP_DIRECTORY_CHARS "/" |
| 75 | #endif /* HAVE_CYGWIN */ |
| 76 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 77 | struct sftp_conn { |
| 78 | int fd_in; |
| 79 | int fd_out; |
| 80 | u_int transfer_buflen; |
| 81 | u_int num_requests; |
| 82 | u_int version; |
| 83 | u_int msg_id; |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 84 | #define SFTP_EXT_POSIX_RENAME 0x00000001 |
| 85 | #define SFTP_EXT_STATVFS 0x00000002 |
| 86 | #define SFTP_EXT_FSTATVFS 0x00000004 |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 87 | #define SFTP_EXT_HARDLINK 0x00000008 |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 88 | #define SFTP_EXT_FSYNC 0x00000010 |
djm@openbsd.org | 60d8c84 | 2019-01-16 23:23:45 +0000 | [diff] [blame] | 89 | #define SFTP_EXT_LSETSTAT 0x00000020 |
Damien Miller | 7a3e1d0 | 2008-03-27 10:59:57 +1100 | [diff] [blame] | 90 | u_int exts; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 91 | u_int64_t limit_kbps; |
| 92 | struct bwlimit bwlimit_in, bwlimit_out; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 93 | }; |
Ben Lindstrom | 288cc39 | 2001-02-09 02:58:04 +0000 | [diff] [blame] | 94 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 95 | static u_char * |
| 96 | get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len, |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 97 | const char *errfmt, ...) __attribute__((format(printf, 4, 5))); |
| 98 | |
| 99 | /* ARGSUSED */ |
| 100 | static int |
| 101 | sftpio(void *_bwlimit, size_t amount) |
| 102 | { |
| 103 | struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit; |
| 104 | |
dtucker@openbsd.org | bdc6c63 | 2019-01-24 16:52:17 +0000 | [diff] [blame] | 105 | refresh_progress_meter(0); |
dtucker@openbsd.org | 8976f1c | 2019-01-23 08:01:46 +0000 | [diff] [blame] | 106 | if (bwlimit != NULL) |
| 107 | bandwidth_limit(bwlimit, amount); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 108 | return 0; |
| 109 | } |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 110 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 111 | static void |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 112 | send_msg(struct sftp_conn *conn, struct sshbuf *m) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 113 | { |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 114 | u_char mlen[4]; |
Damien Miller | 58ca98b | 2006-04-23 12:06:35 +1000 | [diff] [blame] | 115 | struct iovec iov[2]; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 116 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 117 | if (sshbuf_len(m) > SFTP_MAX_MSG_LENGTH) |
| 118 | fatal("Outbound message too long %zu", sshbuf_len(m)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 119 | |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 120 | /* Send length first */ |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 121 | put_u32(mlen, sshbuf_len(m)); |
Damien Miller | 58ca98b | 2006-04-23 12:06:35 +1000 | [diff] [blame] | 122 | iov[0].iov_base = mlen; |
| 123 | iov[0].iov_len = sizeof(mlen); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 124 | iov[1].iov_base = (u_char *)sshbuf_ptr(m); |
| 125 | iov[1].iov_len = sshbuf_len(m); |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 126 | |
dtucker@openbsd.org | 8976f1c | 2019-01-23 08:01:46 +0000 | [diff] [blame] | 127 | if (atomiciov6(writev, conn->fd_out, iov, 2, sftpio, |
| 128 | conn->limit_kbps > 0 ? &conn->bwlimit_out : NULL) != |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 129 | sshbuf_len(m) + sizeof(mlen)) |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 130 | fatal("Couldn't send packet: %s", strerror(errno)); |
| 131 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 132 | sshbuf_reset(m); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 133 | } |
| 134 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 135 | static void |
dtucker@openbsd.org | 3dde09a | 2017-11-28 21:10:22 +0000 | [diff] [blame] | 136 | get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 137 | { |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 138 | u_int msg_len; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 139 | u_char *p; |
| 140 | int r; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 141 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 142 | if ((r = sshbuf_reserve(m, 4, &p)) != 0) |
| 143 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
dtucker@openbsd.org | 8976f1c | 2019-01-23 08:01:46 +0000 | [diff] [blame] | 144 | if (atomicio6(read, conn->fd_in, p, 4, sftpio, |
| 145 | conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL) != 4) { |
djm@openbsd.org | 4b3ecbb | 2017-08-11 04:41:08 +0000 | [diff] [blame] | 146 | if (errno == EPIPE || errno == ECONNRESET) |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 147 | fatal("Connection closed"); |
| 148 | else |
| 149 | fatal("Couldn't read packet: %s", strerror(errno)); |
| 150 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 151 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 152 | if ((r = sshbuf_get_u32(m, &msg_len)) != 0) |
| 153 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
dtucker@openbsd.org | 3dde09a | 2017-11-28 21:10:22 +0000 | [diff] [blame] | 154 | if (msg_len > SFTP_MAX_MSG_LENGTH) { |
| 155 | do_log2(initial ? SYSLOG_LEVEL_ERROR : SYSLOG_LEVEL_FATAL, |
| 156 | "Received message too long %u", msg_len); |
| 157 | fatal("Ensure the remote shell produces no output " |
| 158 | "for non-interactive sessions."); |
| 159 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 160 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 161 | if ((r = sshbuf_reserve(m, msg_len, &p)) != 0) |
| 162 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
dtucker@openbsd.org | 8976f1c | 2019-01-23 08:01:46 +0000 | [diff] [blame] | 163 | if (atomicio6(read, conn->fd_in, p, msg_len, sftpio, |
| 164 | conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL) |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 165 | != msg_len) { |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 166 | if (errno == EPIPE) |
| 167 | fatal("Connection closed"); |
| 168 | else |
| 169 | fatal("Read packet: %s", strerror(errno)); |
| 170 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 171 | } |
| 172 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 173 | static void |
dtucker@openbsd.org | 3dde09a | 2017-11-28 21:10:22 +0000 | [diff] [blame] | 174 | get_msg(struct sftp_conn *conn, struct sshbuf *m) |
| 175 | { |
| 176 | get_msg_extended(conn, m, 0); |
| 177 | } |
| 178 | |
| 179 | static void |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 180 | send_string_request(struct sftp_conn *conn, u_int id, u_int code, const char *s, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 181 | u_int len) |
| 182 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 183 | struct sshbuf *msg; |
| 184 | int r; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 185 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 186 | if ((msg = sshbuf_new()) == NULL) |
| 187 | fatal("%s: sshbuf_new failed", __func__); |
| 188 | if ((r = sshbuf_put_u8(msg, code)) != 0 || |
| 189 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 190 | (r = sshbuf_put_string(msg, s, len)) != 0) |
| 191 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 192 | send_msg(conn, msg); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 193 | debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 194 | sshbuf_free(msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 195 | } |
| 196 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 197 | static void |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 198 | send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code, |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 199 | const void *s, u_int len, Attrib *a) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 200 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 201 | struct sshbuf *msg; |
| 202 | int r; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 203 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 204 | if ((msg = sshbuf_new()) == NULL) |
| 205 | fatal("%s: sshbuf_new failed", __func__); |
| 206 | if ((r = sshbuf_put_u8(msg, code)) != 0 || |
| 207 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 208 | (r = sshbuf_put_string(msg, s, len)) != 0 || |
| 209 | (r = encode_attrib(msg, a)) != 0) |
| 210 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 211 | send_msg(conn, msg); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 212 | debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 213 | sshbuf_free(msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 214 | } |
| 215 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 216 | static u_int |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 217 | get_status(struct sftp_conn *conn, u_int expected_id) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 218 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 219 | struct sshbuf *msg; |
| 220 | u_char type; |
| 221 | u_int id, status; |
| 222 | int r; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 223 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 224 | if ((msg = sshbuf_new()) == NULL) |
| 225 | fatal("%s: sshbuf_new failed", __func__); |
| 226 | get_msg(conn, msg); |
| 227 | if ((r = sshbuf_get_u8(msg, &type)) != 0 || |
| 228 | (r = sshbuf_get_u32(msg, &id)) != 0) |
| 229 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 230 | |
| 231 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 232 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 233 | if (type != SSH2_FXP_STATUS) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 234 | fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 235 | SSH2_FXP_STATUS, type); |
| 236 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 237 | if ((r = sshbuf_get_u32(msg, &status)) != 0) |
| 238 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 239 | sshbuf_free(msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 240 | |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 241 | debug3("SSH2_FXP_STATUS %u", status); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 242 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 243 | return status; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 244 | } |
| 245 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 246 | static u_char * |
| 247 | get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len, |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 248 | const char *errfmt, ...) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 249 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 250 | struct sshbuf *msg; |
| 251 | u_int id, status; |
| 252 | u_char type; |
| 253 | u_char *handle; |
| 254 | char errmsg[256]; |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 255 | va_list args; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 256 | int r; |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 257 | |
| 258 | va_start(args, errfmt); |
| 259 | if (errfmt != NULL) |
| 260 | vsnprintf(errmsg, sizeof(errmsg), errfmt, args); |
| 261 | va_end(args); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 262 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 263 | if ((msg = sshbuf_new()) == NULL) |
| 264 | fatal("%s: sshbuf_new failed", __func__); |
| 265 | get_msg(conn, msg); |
| 266 | if ((r = sshbuf_get_u8(msg, &type)) != 0 || |
| 267 | (r = sshbuf_get_u32(msg, &id)) != 0) |
| 268 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 269 | |
| 270 | if (id != expected_id) |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 271 | fatal("%s: ID mismatch (%u != %u)", |
| 272 | errfmt == NULL ? __func__ : errmsg, id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 273 | if (type == SSH2_FXP_STATUS) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 274 | if ((r = sshbuf_get_u32(msg, &status)) != 0) |
| 275 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 276 | if (errfmt != NULL) |
| 277 | error("%s: %s", errmsg, fx2txt(status)); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 278 | sshbuf_free(msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 279 | return(NULL); |
| 280 | } else if (type != SSH2_FXP_HANDLE) |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 281 | fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u", |
| 282 | errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 283 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 284 | if ((r = sshbuf_get_string(msg, &handle, len)) != 0) |
| 285 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 286 | sshbuf_free(msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 287 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 288 | return handle; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 289 | } |
| 290 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 291 | static Attrib * |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 292 | get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 293 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 294 | struct sshbuf *msg; |
| 295 | u_int id; |
| 296 | u_char type; |
| 297 | int r; |
| 298 | static Attrib a; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 299 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 300 | if ((msg = sshbuf_new()) == NULL) |
| 301 | fatal("%s: sshbuf_new failed", __func__); |
| 302 | get_msg(conn, msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 303 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 304 | if ((r = sshbuf_get_u8(msg, &type)) != 0 || |
| 305 | (r = sshbuf_get_u32(msg, &id)) != 0) |
| 306 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 307 | |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 308 | debug3("Received stat reply T:%u I:%u", type, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 309 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 310 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 311 | if (type == SSH2_FXP_STATUS) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 312 | u_int status; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 313 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 314 | if ((r = sshbuf_get_u32(msg, &status)) != 0) |
| 315 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 316 | if (quiet) |
| 317 | debug("Couldn't stat remote file: %s", fx2txt(status)); |
| 318 | else |
| 319 | error("Couldn't stat remote file: %s", fx2txt(status)); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 320 | sshbuf_free(msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 321 | return(NULL); |
| 322 | } else if (type != SSH2_FXP_ATTRS) { |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 323 | fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 324 | SSH2_FXP_ATTRS, type); |
| 325 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 326 | if ((r = decode_attrib(msg, &a)) != 0) { |
| 327 | error("%s: couldn't decode attrib: %s", __func__, ssh_err(r)); |
| 328 | sshbuf_free(msg); |
| 329 | return NULL; |
| 330 | } |
| 331 | sshbuf_free(msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 332 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 333 | return &a; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 334 | } |
| 335 | |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 336 | static int |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 337 | get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st, |
| 338 | u_int expected_id, int quiet) |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 339 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 340 | struct sshbuf *msg; |
| 341 | u_char type; |
| 342 | u_int id; |
| 343 | u_int64_t flag; |
| 344 | int r; |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 345 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 346 | if ((msg = sshbuf_new()) == NULL) |
| 347 | fatal("%s: sshbuf_new failed", __func__); |
| 348 | get_msg(conn, msg); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 349 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 350 | if ((r = sshbuf_get_u8(msg, &type)) != 0 || |
| 351 | (r = sshbuf_get_u32(msg, &id)) != 0) |
| 352 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 353 | |
| 354 | debug3("Received statvfs reply T:%u I:%u", type, id); |
| 355 | if (id != expected_id) |
| 356 | fatal("ID mismatch (%u != %u)", id, expected_id); |
| 357 | if (type == SSH2_FXP_STATUS) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 358 | u_int status; |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 359 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 360 | if ((r = sshbuf_get_u32(msg, &status)) != 0) |
| 361 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 362 | if (quiet) |
| 363 | debug("Couldn't statvfs: %s", fx2txt(status)); |
| 364 | else |
| 365 | error("Couldn't statvfs: %s", fx2txt(status)); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 366 | sshbuf_free(msg); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 367 | return -1; |
| 368 | } else if (type != SSH2_FXP_EXTENDED_REPLY) { |
| 369 | fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u", |
| 370 | SSH2_FXP_EXTENDED_REPLY, type); |
| 371 | } |
| 372 | |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 373 | memset(st, 0, sizeof(*st)); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 374 | if ((r = sshbuf_get_u64(msg, &st->f_bsize)) != 0 || |
| 375 | (r = sshbuf_get_u64(msg, &st->f_frsize)) != 0 || |
| 376 | (r = sshbuf_get_u64(msg, &st->f_blocks)) != 0 || |
| 377 | (r = sshbuf_get_u64(msg, &st->f_bfree)) != 0 || |
| 378 | (r = sshbuf_get_u64(msg, &st->f_bavail)) != 0 || |
| 379 | (r = sshbuf_get_u64(msg, &st->f_files)) != 0 || |
| 380 | (r = sshbuf_get_u64(msg, &st->f_ffree)) != 0 || |
| 381 | (r = sshbuf_get_u64(msg, &st->f_favail)) != 0 || |
| 382 | (r = sshbuf_get_u64(msg, &st->f_fsid)) != 0 || |
| 383 | (r = sshbuf_get_u64(msg, &flag)) != 0 || |
| 384 | (r = sshbuf_get_u64(msg, &st->f_namemax)) != 0) |
| 385 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 386 | |
| 387 | st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0; |
| 388 | st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0; |
| 389 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 390 | sshbuf_free(msg); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 395 | struct sftp_conn * |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 396 | do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests, |
| 397 | u_int64_t limit_kbps) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 398 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 399 | u_char type; |
| 400 | struct sshbuf *msg; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 401 | struct sftp_conn *ret; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 402 | int r; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 403 | |
Damien Miller | fec029f | 2013-08-21 02:42:12 +1000 | [diff] [blame] | 404 | ret = xcalloc(1, sizeof(*ret)); |
| 405 | ret->msg_id = 1; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 406 | ret->fd_in = fd_in; |
| 407 | ret->fd_out = fd_out; |
| 408 | ret->transfer_buflen = transfer_buflen; |
| 409 | ret->num_requests = num_requests; |
| 410 | ret->exts = 0; |
| 411 | ret->limit_kbps = 0; |
| 412 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 413 | if ((msg = sshbuf_new()) == NULL) |
| 414 | fatal("%s: sshbuf_new failed", __func__); |
| 415 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_INIT)) != 0 || |
| 416 | (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0) |
| 417 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 418 | send_msg(ret, msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 419 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 420 | sshbuf_reset(msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 421 | |
dtucker@openbsd.org | 3dde09a | 2017-11-28 21:10:22 +0000 | [diff] [blame] | 422 | get_msg_extended(ret, msg, 1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 423 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 424 | /* Expecting a VERSION reply */ |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 425 | if ((r = sshbuf_get_u8(msg, &type)) != 0) |
| 426 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 427 | if (type != SSH2_FXP_VERSION) { |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 428 | error("Invalid packet back from SSH2_FXP_INIT (type %u)", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 429 | type); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 430 | sshbuf_free(msg); |
jsg@openbsd.org | 64a89ec | 2015-05-23 14:28:37 +0000 | [diff] [blame] | 431 | free(ret); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 432 | return(NULL); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 433 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 434 | if ((r = sshbuf_get_u32(msg, &ret->version)) != 0) |
| 435 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 436 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 437 | debug2("Remote version: %u", ret->version); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 438 | |
| 439 | /* Check for extensions */ |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 440 | while (sshbuf_len(msg) > 0) { |
| 441 | char *name; |
| 442 | u_char *value; |
| 443 | size_t vlen; |
Darren Tucker | a64ab33 | 2008-06-13 07:01:29 +1000 | [diff] [blame] | 444 | int known = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 445 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 446 | if ((r = sshbuf_get_cstring(msg, &name, NULL)) != 0 || |
| 447 | (r = sshbuf_get_string(msg, &value, &vlen)) != 0) |
| 448 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 449 | if (strcmp(name, "posix-rename@openssh.com") == 0 && |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 450 | strcmp((char *)value, "1") == 0) { |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 451 | ret->exts |= SFTP_EXT_POSIX_RENAME; |
Darren Tucker | a64ab33 | 2008-06-13 07:01:29 +1000 | [diff] [blame] | 452 | known = 1; |
| 453 | } else if (strcmp(name, "statvfs@openssh.com") == 0 && |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 454 | strcmp((char *)value, "2") == 0) { |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 455 | ret->exts |= SFTP_EXT_STATVFS; |
Darren Tucker | a64ab33 | 2008-06-13 07:01:29 +1000 | [diff] [blame] | 456 | known = 1; |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 457 | } else if (strcmp(name, "fstatvfs@openssh.com") == 0 && |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 458 | strcmp((char *)value, "2") == 0) { |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 459 | ret->exts |= SFTP_EXT_FSTATVFS; |
Darren Tucker | a64ab33 | 2008-06-13 07:01:29 +1000 | [diff] [blame] | 460 | known = 1; |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 461 | } else if (strcmp(name, "hardlink@openssh.com") == 0 && |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 462 | strcmp((char *)value, "1") == 0) { |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 463 | ret->exts |= SFTP_EXT_HARDLINK; |
| 464 | known = 1; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 465 | } else if (strcmp(name, "fsync@openssh.com") == 0 && |
| 466 | strcmp((char *)value, "1") == 0) { |
| 467 | ret->exts |= SFTP_EXT_FSYNC; |
| 468 | known = 1; |
djm@openbsd.org | 60d8c84 | 2019-01-16 23:23:45 +0000 | [diff] [blame] | 469 | } else if (strcmp(name, "lsetstat@openssh.com") == 0 && |
| 470 | strcmp((char *)value, "1") == 0) { |
| 471 | ret->exts |= SFTP_EXT_LSETSTAT; |
| 472 | known = 1; |
Darren Tucker | a64ab33 | 2008-06-13 07:01:29 +1000 | [diff] [blame] | 473 | } |
| 474 | if (known) { |
| 475 | debug2("Server supports extension \"%s\" revision %s", |
| 476 | name, value); |
| 477 | } else { |
| 478 | debug2("Unrecognised server extension \"%s\"", name); |
| 479 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 480 | free(name); |
| 481 | free(value); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 482 | } |
| 483 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 484 | sshbuf_free(msg); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 485 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 486 | /* Some filexfer v.0 servers don't support large packets */ |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 487 | if (ret->version == 0) |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 488 | ret->transfer_buflen = MINIMUM(ret->transfer_buflen, 20480); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 489 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 490 | ret->limit_kbps = limit_kbps; |
| 491 | if (ret->limit_kbps > 0) { |
| 492 | bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps, |
| 493 | ret->transfer_buflen); |
| 494 | bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps, |
| 495 | ret->transfer_buflen); |
| 496 | } |
| 497 | |
| 498 | return ret; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | u_int |
| 502 | sftp_proto_version(struct sftp_conn *conn) |
| 503 | { |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 504 | return conn->version; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 508 | do_close(struct sftp_conn *conn, const u_char *handle, u_int handle_len) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 509 | { |
| 510 | u_int id, status; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 511 | struct sshbuf *msg; |
| 512 | int r; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 513 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 514 | if ((msg = sshbuf_new()) == NULL) |
| 515 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 516 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 517 | id = conn->msg_id++; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 518 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_CLOSE)) != 0 || |
| 519 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 520 | (r = sshbuf_put_string(msg, handle, handle_len)) != 0) |
| 521 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 522 | send_msg(conn, msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 523 | debug3("Sent message SSH2_FXP_CLOSE I:%u", id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 524 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 525 | status = get_status(conn, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 526 | if (status != SSH2_FX_OK) |
| 527 | error("Couldn't close file: %s", fx2txt(status)); |
| 528 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 529 | sshbuf_free(msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 530 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 531 | return status == SSH2_FX_OK ? 0 : -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 532 | } |
| 533 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 534 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 535 | static int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 536 | do_lsreaddir(struct sftp_conn *conn, const char *path, int print_flag, |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 537 | SFTP_DIRENT ***dir) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 538 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 539 | struct sshbuf *msg; |
| 540 | u_int count, id, i, expected_id, ents = 0; |
| 541 | size_t handle_len; |
djm@openbsd.org | 1a31d02 | 2016-05-02 08:49:03 +0000 | [diff] [blame] | 542 | u_char type, *handle; |
Damien Miller | 3939207 | 2013-12-07 10:31:08 +1100 | [diff] [blame] | 543 | int status = SSH2_FX_FAILURE; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 544 | int r; |
Damien Miller | 3939207 | 2013-12-07 10:31:08 +1100 | [diff] [blame] | 545 | |
| 546 | if (dir) |
| 547 | *dir = NULL; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 548 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 549 | id = conn->msg_id++; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 550 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 551 | if ((msg = sshbuf_new()) == NULL) |
| 552 | fatal("%s: sshbuf_new failed", __func__); |
| 553 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPENDIR)) != 0 || |
| 554 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 555 | (r = sshbuf_put_cstring(msg, path)) != 0) |
| 556 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 557 | send_msg(conn, msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 558 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 559 | handle = get_handle(conn, id, &handle_len, |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 560 | "remote readdir(\"%s\")", path); |
Damien Miller | 57c38ac | 2011-09-22 21:42:45 +1000 | [diff] [blame] | 561 | if (handle == NULL) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 562 | sshbuf_free(msg); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 563 | return -1; |
Damien Miller | 57c38ac | 2011-09-22 21:42:45 +1000 | [diff] [blame] | 564 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 565 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 566 | if (dir) { |
| 567 | ents = 0; |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 568 | *dir = xcalloc(1, sizeof(**dir)); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 569 | (*dir)[0] = NULL; |
| 570 | } |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 571 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 572 | for (; !interrupted;) { |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 573 | id = expected_id = conn->msg_id++; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 574 | |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 575 | debug3("Sending SSH2_FXP_READDIR I:%u", id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 576 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 577 | sshbuf_reset(msg); |
| 578 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_READDIR)) != 0 || |
| 579 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 580 | (r = sshbuf_put_string(msg, handle, handle_len)) != 0) |
| 581 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 582 | send_msg(conn, msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 583 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 584 | sshbuf_reset(msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 585 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 586 | get_msg(conn, msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 587 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 588 | if ((r = sshbuf_get_u8(msg, &type)) != 0 || |
| 589 | (r = sshbuf_get_u32(msg, &id)) != 0) |
| 590 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 591 | |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 592 | debug3("Received reply T:%u I:%u", type, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 593 | |
| 594 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 595 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 596 | |
| 597 | if (type == SSH2_FXP_STATUS) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 598 | u_int rstatus; |
| 599 | |
| 600 | if ((r = sshbuf_get_u32(msg, &rstatus)) != 0) |
| 601 | fatal("%s: buffer error: %s", |
| 602 | __func__, ssh_err(r)); |
| 603 | debug3("Received SSH2_FXP_STATUS %d", rstatus); |
| 604 | if (rstatus == SSH2_FX_EOF) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 605 | break; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 606 | error("Couldn't read directory: %s", fx2txt(rstatus)); |
Damien Miller | 3939207 | 2013-12-07 10:31:08 +1100 | [diff] [blame] | 607 | goto out; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 608 | } else if (type != SSH2_FXP_NAME) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 609 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 610 | SSH2_FXP_NAME, type); |
| 611 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 612 | if ((r = sshbuf_get_u32(msg, &count)) != 0) |
| 613 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
djm@openbsd.org | c7995f2 | 2017-01-03 05:46:51 +0000 | [diff] [blame] | 614 | if (count > SSHBUF_SIZE_MAX) |
| 615 | fatal("%s: nonsensical number of entries", __func__); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 616 | if (count == 0) |
| 617 | break; |
| 618 | debug3("Received %d SSH2_FXP_NAME responses", count); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 619 | for (i = 0; i < count; i++) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 620 | char *filename, *longname; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 621 | Attrib a; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 622 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 623 | if ((r = sshbuf_get_cstring(msg, &filename, |
| 624 | NULL)) != 0 || |
| 625 | (r = sshbuf_get_cstring(msg, &longname, |
| 626 | NULL)) != 0) |
| 627 | fatal("%s: buffer error: %s", |
| 628 | __func__, ssh_err(r)); |
| 629 | if ((r = decode_attrib(msg, &a)) != 0) { |
| 630 | error("%s: couldn't decode attrib: %s", |
| 631 | __func__, ssh_err(r)); |
| 632 | free(filename); |
| 633 | free(longname); |
djm@openbsd.org | 07f2c7f | 2019-10-04 04:31:59 +0000 | [diff] [blame] | 634 | goto out; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 635 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 636 | |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 637 | if (print_flag) |
schwarze@openbsd.org | 0e059cd | 2016-05-25 23:48:45 +0000 | [diff] [blame] | 638 | mprintf("%s\n", longname); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 639 | |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 640 | /* |
| 641 | * Directory entries should never contain '/' |
| 642 | * These can be used to attack recursive ops |
| 643 | * (e.g. send '../../../../etc/passwd') |
| 644 | */ |
Damien Miller | 89f0485 | 2017-03-20 11:53:34 +1100 | [diff] [blame] | 645 | if (strpbrk(filename, SFTP_DIRECTORY_CHARS) != NULL) { |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 646 | error("Server sent suspect path \"%s\" " |
| 647 | "during readdir of \"%s\"", filename, path); |
Damien Miller | 3939207 | 2013-12-07 10:31:08 +1100 | [diff] [blame] | 648 | } else if (dir) { |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 649 | *dir = xreallocarray(*dir, ents + 2, sizeof(**dir)); |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 650 | (*dir)[ents] = xcalloc(1, sizeof(***dir)); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 651 | (*dir)[ents]->filename = xstrdup(filename); |
| 652 | (*dir)[ents]->longname = xstrdup(longname); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 653 | memcpy(&(*dir)[ents]->a, &a, sizeof(a)); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 654 | (*dir)[++ents] = NULL; |
| 655 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 656 | free(filename); |
| 657 | free(longname); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 658 | } |
| 659 | } |
Damien Miller | 3939207 | 2013-12-07 10:31:08 +1100 | [diff] [blame] | 660 | status = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 661 | |
Damien Miller | 3939207 | 2013-12-07 10:31:08 +1100 | [diff] [blame] | 662 | out: |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 663 | sshbuf_free(msg); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 664 | do_close(conn, handle, handle_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 665 | free(handle); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 666 | |
Damien Miller | 3939207 | 2013-12-07 10:31:08 +1100 | [diff] [blame] | 667 | if (status != 0 && dir != NULL) { |
| 668 | /* Don't return results on error */ |
| 669 | free_sftp_dirents(*dir); |
| 670 | *dir = NULL; |
| 671 | } else if (interrupted && dir != NULL && *dir != NULL) { |
| 672 | /* Don't return partial matches on interrupt */ |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 673 | free_sftp_dirents(*dir); |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 674 | *dir = xcalloc(1, sizeof(**dir)); |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 675 | **dir = NULL; |
| 676 | } |
| 677 | |
djm@openbsd.org | f6a59a2 | 2018-05-25 04:25:46 +0000 | [diff] [blame] | 678 | return status == SSH2_FX_OK ? 0 : -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 682 | do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 683 | { |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 684 | return(do_lsreaddir(conn, path, 0, dir)); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | void free_sftp_dirents(SFTP_DIRENT **s) |
| 688 | { |
| 689 | int i; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 690 | |
Damien Miller | 3939207 | 2013-12-07 10:31:08 +1100 | [diff] [blame] | 691 | if (s == NULL) |
| 692 | return; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 693 | for (i = 0; s[i]; i++) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 694 | free(s[i]->filename); |
| 695 | free(s[i]->longname); |
| 696 | free(s[i]); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 697 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 698 | free(s); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 702 | do_rm(struct sftp_conn *conn, const char *path) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 703 | { |
| 704 | u_int status, id; |
| 705 | |
| 706 | debug2("Sending SSH2_FXP_REMOVE \"%s\"", path); |
| 707 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 708 | id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 709 | send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path)); |
| 710 | status = get_status(conn, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 711 | if (status != SSH2_FX_OK) |
| 712 | error("Couldn't delete file: %s", fx2txt(status)); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 713 | return status == SSH2_FX_OK ? 0 : -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 717 | do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 718 | { |
| 719 | u_int status, id; |
| 720 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 721 | id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 722 | send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 723 | strlen(path), a); |
| 724 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 725 | status = get_status(conn, id); |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 726 | if (status != SSH2_FX_OK && print_flag) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 727 | error("Couldn't create directory: %s", fx2txt(status)); |
| 728 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 729 | return status == SSH2_FX_OK ? 0 : -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 733 | do_rmdir(struct sftp_conn *conn, const char *path) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 734 | { |
| 735 | u_int status, id; |
| 736 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 737 | id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 738 | send_string_request(conn, id, SSH2_FXP_RMDIR, path, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 739 | strlen(path)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 740 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 741 | status = get_status(conn, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 742 | if (status != SSH2_FX_OK) |
| 743 | error("Couldn't remove directory: %s", fx2txt(status)); |
| 744 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 745 | return status == SSH2_FX_OK ? 0 : -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | Attrib * |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 749 | do_stat(struct sftp_conn *conn, const char *path, int quiet) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 750 | { |
| 751 | u_int id; |
| 752 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 753 | id = conn->msg_id++; |
| 754 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 755 | send_string_request(conn, id, |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 756 | conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 757 | path, strlen(path)); |
| 758 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 759 | return(get_decode_stat(conn, id, quiet)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | Attrib * |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 763 | do_lstat(struct sftp_conn *conn, const char *path, int quiet) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 764 | { |
| 765 | u_int id; |
| 766 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 767 | if (conn->version == 0) { |
| 768 | if (quiet) |
| 769 | debug("Server version does not support lstat operation"); |
| 770 | else |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 771 | logit("Server version does not support lstat operation"); |
Ben Lindstrom | f26ff5b | 2002-04-02 21:00:31 +0000 | [diff] [blame] | 772 | return(do_stat(conn, path, quiet)); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 776 | send_string_request(conn, id, SSH2_FXP_LSTAT, path, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 777 | strlen(path)); |
| 778 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 779 | return(get_decode_stat(conn, id, quiet)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 780 | } |
| 781 | |
Damien Miller | cfe23d3 | 2008-02-10 22:20:44 +1100 | [diff] [blame] | 782 | #ifdef notyet |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 783 | Attrib * |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 784 | do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len, |
| 785 | int quiet) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 786 | { |
| 787 | u_int id; |
| 788 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 789 | id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 790 | send_string_request(conn, id, SSH2_FXP_FSTAT, handle, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 791 | handle_len); |
| 792 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 793 | return(get_decode_stat(conn, id, quiet)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 794 | } |
Damien Miller | cfe23d3 | 2008-02-10 22:20:44 +1100 | [diff] [blame] | 795 | #endif |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 796 | |
| 797 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 798 | do_setstat(struct sftp_conn *conn, const char *path, Attrib *a) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 799 | { |
| 800 | u_int status, id; |
| 801 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 802 | id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 803 | send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 804 | strlen(path), a); |
| 805 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 806 | status = get_status(conn, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 807 | if (status != SSH2_FX_OK) |
| 808 | error("Couldn't setstat on \"%s\": %s", path, |
| 809 | fx2txt(status)); |
| 810 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 811 | return status == SSH2_FX_OK ? 0 : -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 815 | do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 816 | Attrib *a) |
| 817 | { |
| 818 | u_int status, id; |
| 819 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 820 | id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 821 | send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 822 | handle_len, a); |
| 823 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 824 | status = get_status(conn, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 825 | if (status != SSH2_FX_OK) |
| 826 | error("Couldn't fsetstat: %s", fx2txt(status)); |
| 827 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 828 | return status == SSH2_FX_OK ? 0 : -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | char * |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 832 | do_realpath(struct sftp_conn *conn, const char *path) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 833 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 834 | struct sshbuf *msg; |
| 835 | u_int expected_id, count, id; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 836 | char *filename, *longname; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 837 | Attrib a; |
| 838 | u_char type; |
| 839 | int r; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 840 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 841 | expected_id = id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 842 | send_string_request(conn, id, SSH2_FXP_REALPATH, path, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 843 | strlen(path)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 844 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 845 | if ((msg = sshbuf_new()) == NULL) |
| 846 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 847 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 848 | get_msg(conn, msg); |
| 849 | if ((r = sshbuf_get_u8(msg, &type)) != 0 || |
| 850 | (r = sshbuf_get_u32(msg, &id)) != 0) |
| 851 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 852 | |
| 853 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 854 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 855 | |
| 856 | if (type == SSH2_FXP_STATUS) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 857 | u_int status; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 858 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 859 | if ((r = sshbuf_get_u32(msg, &status)) != 0) |
| 860 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 861 | error("Couldn't canonicalize: %s", fx2txt(status)); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 862 | sshbuf_free(msg); |
Damien Miller | 4956631 | 2010-06-26 09:38:23 +1000 | [diff] [blame] | 863 | return NULL; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 864 | } else if (type != SSH2_FXP_NAME) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 865 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 866 | SSH2_FXP_NAME, type); |
| 867 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 868 | if ((r = sshbuf_get_u32(msg, &count)) != 0) |
| 869 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 870 | if (count != 1) |
| 871 | fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count); |
| 872 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 873 | if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 || |
| 874 | (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 || |
| 875 | (r = decode_attrib(msg, &a)) != 0) |
| 876 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 877 | |
Darren Tucker | 4908d44 | 2012-07-02 22:15:38 +1000 | [diff] [blame] | 878 | debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename, |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 879 | (unsigned long)a.size); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 880 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 881 | free(longname); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 882 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 883 | sshbuf_free(msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 884 | |
| 885 | return(filename); |
| 886 | } |
| 887 | |
| 888 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 889 | do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath, |
Damien Miller | c7dba12 | 2013-08-21 02:41:15 +1000 | [diff] [blame] | 890 | int force_legacy) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 891 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 892 | struct sshbuf *msg; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 893 | u_int status, id; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 894 | int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 895 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 896 | if ((msg = sshbuf_new()) == NULL) |
| 897 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 898 | |
| 899 | /* Send rename request */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 900 | id = conn->msg_id++; |
Damien Miller | c7dba12 | 2013-08-21 02:41:15 +1000 | [diff] [blame] | 901 | if (use_ext) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 902 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 || |
| 903 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 904 | (r = sshbuf_put_cstring(msg, |
| 905 | "posix-rename@openssh.com")) != 0) |
| 906 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 7a3e1d0 | 2008-03-27 10:59:57 +1100 | [diff] [blame] | 907 | } else { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 908 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_RENAME)) != 0 || |
| 909 | (r = sshbuf_put_u32(msg, id)) != 0) |
| 910 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 7a3e1d0 | 2008-03-27 10:59:57 +1100 | [diff] [blame] | 911 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 912 | if ((r = sshbuf_put_cstring(msg, oldpath)) != 0 || |
| 913 | (r = sshbuf_put_cstring(msg, newpath)) != 0) |
| 914 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 915 | send_msg(conn, msg); |
Damien Miller | 7a3e1d0 | 2008-03-27 10:59:57 +1100 | [diff] [blame] | 916 | debug3("Sent message %s \"%s\" -> \"%s\"", |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 917 | use_ext ? "posix-rename@openssh.com" : |
| 918 | "SSH2_FXP_RENAME", oldpath, newpath); |
| 919 | sshbuf_free(msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 920 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 921 | status = get_status(conn, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 922 | if (status != SSH2_FX_OK) |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 923 | error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, |
| 924 | newpath, fx2txt(status)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 925 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 926 | return status == SSH2_FX_OK ? 0 : -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 930 | do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath) |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 931 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 932 | struct sshbuf *msg; |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 933 | u_int status, id; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 934 | int r; |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 935 | |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 936 | if ((conn->exts & SFTP_EXT_HARDLINK) == 0) { |
| 937 | error("Server does not support hardlink@openssh.com extension"); |
| 938 | return -1; |
| 939 | } |
| 940 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 941 | if ((msg = sshbuf_new()) == NULL) |
| 942 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | 3decdba | 2011-09-22 21:41:05 +1000 | [diff] [blame] | 943 | |
| 944 | /* Send link request */ |
| 945 | id = conn->msg_id++; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 946 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 || |
| 947 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 948 | (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 || |
| 949 | (r = sshbuf_put_cstring(msg, oldpath)) != 0 || |
| 950 | (r = sshbuf_put_cstring(msg, newpath)) != 0) |
| 951 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 952 | send_msg(conn, msg); |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 953 | debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"", |
| 954 | oldpath, newpath); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 955 | sshbuf_free(msg); |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 956 | |
| 957 | status = get_status(conn, id); |
| 958 | if (status != SSH2_FX_OK) |
| 959 | error("Couldn't link file \"%s\" to \"%s\": %s", oldpath, |
| 960 | newpath, fx2txt(status)); |
| 961 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 962 | return status == SSH2_FX_OK ? 0 : -1; |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 966 | do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath) |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 967 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 968 | struct sshbuf *msg; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 969 | u_int status, id; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 970 | int r; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 971 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 972 | if (conn->version < 3) { |
| 973 | error("This server does not support the symlink operation"); |
| 974 | return(SSH2_FX_OP_UNSUPPORTED); |
| 975 | } |
| 976 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 977 | if ((msg = sshbuf_new()) == NULL) |
| 978 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 979 | |
Darren Tucker | dca6a4d | 2004-04-19 22:10:52 +1000 | [diff] [blame] | 980 | /* Send symlink request */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 981 | id = conn->msg_id++; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 982 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_SYMLINK)) != 0 || |
| 983 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 984 | (r = sshbuf_put_cstring(msg, oldpath)) != 0 || |
| 985 | (r = sshbuf_put_cstring(msg, newpath)) != 0) |
| 986 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 987 | send_msg(conn, msg); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 988 | debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath, |
| 989 | newpath); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 990 | sshbuf_free(msg); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 991 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 992 | status = get_status(conn, id); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 993 | if (status != SSH2_FX_OK) |
Ben Lindstrom | 8e879cf | 2002-11-09 15:48:49 +0000 | [diff] [blame] | 994 | error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 995 | newpath, fx2txt(status)); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 996 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 997 | return status == SSH2_FX_OK ? 0 : -1; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 998 | } |
| 999 | |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1000 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1001 | do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len) |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1002 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1003 | struct sshbuf *msg; |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1004 | u_int status, id; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1005 | int r; |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1006 | |
| 1007 | /* Silently return if the extension is not supported */ |
| 1008 | if ((conn->exts & SFTP_EXT_FSYNC) == 0) |
| 1009 | return -1; |
| 1010 | |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1011 | /* Send fsync request */ |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1012 | if ((msg = sshbuf_new()) == NULL) |
| 1013 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1014 | id = conn->msg_id++; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1015 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 || |
| 1016 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 1017 | (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 || |
| 1018 | (r = sshbuf_put_string(msg, handle, handle_len)) != 0) |
| 1019 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1020 | send_msg(conn, msg); |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1021 | debug3("Sent message fsync@openssh.com I:%u", id); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1022 | sshbuf_free(msg); |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1023 | |
| 1024 | status = get_status(conn, id); |
| 1025 | if (status != SSH2_FX_OK) |
| 1026 | error("Couldn't sync file: %s", fx2txt(status)); |
| 1027 | |
djm@openbsd.org | f6a59a2 | 2018-05-25 04:25:46 +0000 | [diff] [blame] | 1028 | return status == SSH2_FX_OK ? 0 : -1; |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1029 | } |
| 1030 | |
Damien Miller | cfe23d3 | 2008-02-10 22:20:44 +1100 | [diff] [blame] | 1031 | #ifdef notyet |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1032 | char * |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1033 | do_readlink(struct sftp_conn *conn, const char *path) |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1034 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1035 | struct sshbuf *msg; |
| 1036 | u_int expected_id, count, id; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1037 | char *filename, *longname; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1038 | Attrib a; |
| 1039 | u_char type; |
| 1040 | int r; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1041 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1042 | expected_id = id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1043 | send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path)); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1044 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1045 | if ((msg = sshbuf_new()) == NULL) |
| 1046 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1047 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1048 | get_msg(conn, msg); |
| 1049 | if ((r = sshbuf_get_u8(msg, &type)) != 0 || |
| 1050 | (r = sshbuf_get_u32(msg, &id)) != 0) |
| 1051 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1052 | |
| 1053 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1054 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1055 | |
| 1056 | if (type == SSH2_FXP_STATUS) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1057 | u_int status; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1058 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1059 | if ((r = sshbuf_get_u32(msg, &status)) != 0) |
| 1060 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1061 | error("Couldn't readlink: %s", fx2txt(status)); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1062 | sshbuf_free(msg); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1063 | return(NULL); |
| 1064 | } else if (type != SSH2_FXP_NAME) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1065 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1066 | SSH2_FXP_NAME, type); |
| 1067 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1068 | if ((r = sshbuf_get_u32(msg, &count)) != 0) |
| 1069 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1070 | if (count != 1) |
| 1071 | fatal("Got multiple names (%d) from SSH_FXP_READLINK", count); |
| 1072 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1073 | if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 || |
| 1074 | (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 || |
| 1075 | (r = decode_attrib(msg, &a)) != 0) |
| 1076 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1077 | |
| 1078 | debug3("SSH_FXP_READLINK %s -> %s", path, filename); |
| 1079 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1080 | free(longname); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1081 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1082 | sshbuf_free(msg); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1083 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1084 | return filename; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1085 | } |
Damien Miller | cfe23d3 | 2008-02-10 22:20:44 +1100 | [diff] [blame] | 1086 | #endif |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1087 | |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1088 | int |
Darren Tucker | 7b59889 | 2008-06-09 22:49:36 +1000 | [diff] [blame] | 1089 | do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st, |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1090 | int quiet) |
| 1091 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1092 | struct sshbuf *msg; |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1093 | u_int id; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1094 | int r; |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1095 | |
| 1096 | if ((conn->exts & SFTP_EXT_STATVFS) == 0) { |
| 1097 | error("Server does not support statvfs@openssh.com extension"); |
| 1098 | return -1; |
| 1099 | } |
| 1100 | |
| 1101 | id = conn->msg_id++; |
| 1102 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1103 | if ((msg = sshbuf_new()) == NULL) |
| 1104 | fatal("%s: sshbuf_new failed", __func__); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1105 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 || |
| 1106 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 1107 | (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 || |
| 1108 | (r = sshbuf_put_cstring(msg, path)) != 0) |
| 1109 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1110 | send_msg(conn, msg); |
| 1111 | sshbuf_free(msg); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1112 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1113 | return get_decode_statvfs(conn, st, id, quiet); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | #ifdef notyet |
| 1117 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1118 | do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len, |
Darren Tucker | 7b59889 | 2008-06-09 22:49:36 +1000 | [diff] [blame] | 1119 | struct sftp_statvfs *st, int quiet) |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1120 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1121 | struct sshbuf *msg; |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1122 | u_int id; |
| 1123 | |
| 1124 | if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) { |
| 1125 | error("Server does not support fstatvfs@openssh.com extension"); |
| 1126 | return -1; |
| 1127 | } |
| 1128 | |
| 1129 | id = conn->msg_id++; |
| 1130 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1131 | if ((msg = sshbuf_new()) == NULL) |
| 1132 | fatal("%s: sshbuf_new failed", __func__); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1133 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 || |
| 1134 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 1135 | (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 || |
| 1136 | (r = sshbuf_put_string(msg, handle, handle_len)) != 0) |
| 1137 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1138 | send_msg(conn, msg); |
| 1139 | sshbuf_free(msg); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1140 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1141 | return get_decode_statvfs(conn, st, id, quiet); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1142 | } |
| 1143 | #endif |
| 1144 | |
djm@openbsd.org | 60d8c84 | 2019-01-16 23:23:45 +0000 | [diff] [blame] | 1145 | int |
| 1146 | do_lsetstat(struct sftp_conn *conn, const char *path, Attrib *a) |
| 1147 | { |
| 1148 | struct sshbuf *msg; |
| 1149 | u_int status, id; |
| 1150 | int r; |
| 1151 | |
| 1152 | if ((conn->exts & SFTP_EXT_LSETSTAT) == 0) { |
| 1153 | error("Server does not support lsetstat@openssh.com extension"); |
| 1154 | return -1; |
| 1155 | } |
| 1156 | |
| 1157 | id = conn->msg_id++; |
| 1158 | if ((msg = sshbuf_new()) == NULL) |
| 1159 | fatal("%s: sshbuf_new failed", __func__); |
| 1160 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 || |
| 1161 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 1162 | (r = sshbuf_put_cstring(msg, "lsetstat@openssh.com")) != 0 || |
| 1163 | (r = sshbuf_put_cstring(msg, path)) != 0 || |
| 1164 | (r = encode_attrib(msg, a)) != 0) |
| 1165 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1166 | send_msg(conn, msg); |
| 1167 | sshbuf_free(msg); |
| 1168 | |
| 1169 | status = get_status(conn, id); |
| 1170 | if (status != SSH2_FX_OK) |
| 1171 | error("Couldn't setstat on \"%s\": %s", path, |
| 1172 | fx2txt(status)); |
| 1173 | |
| 1174 | return status == SSH2_FX_OK ? 0 : -1; |
| 1175 | } |
| 1176 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1177 | static void |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1178 | send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset, |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1179 | u_int len, const u_char *handle, u_int handle_len) |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1180 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1181 | struct sshbuf *msg; |
| 1182 | int r; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1183 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1184 | if ((msg = sshbuf_new()) == NULL) |
| 1185 | fatal("%s: sshbuf_new failed", __func__); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1186 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_READ)) != 0 || |
| 1187 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 1188 | (r = sshbuf_put_string(msg, handle, handle_len)) != 0 || |
| 1189 | (r = sshbuf_put_u64(msg, offset)) != 0 || |
| 1190 | (r = sshbuf_put_u32(msg, len)) != 0) |
| 1191 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1192 | send_msg(conn, msg); |
| 1193 | sshbuf_free(msg); |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1194 | } |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1195 | |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1196 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1197 | do_download(struct sftp_conn *conn, const char *remote_path, |
| 1198 | const char *local_path, Attrib *a, int preserve_flag, int resume_flag, |
| 1199 | int fsync_flag) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1200 | { |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1201 | Attrib junk; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1202 | struct sshbuf *msg; |
| 1203 | u_char *handle; |
| 1204 | int local_fd = -1, write_error; |
djm@openbsd.org | a0876bd | 2019-07-12 03:56:21 +0000 | [diff] [blame] | 1205 | int read_error, write_errno, lmodified = 0, reordered = 0, r; |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1206 | u_int64_t offset = 0, size, highwater; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1207 | u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1208 | off_t progress_counter; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1209 | size_t handle_len; |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1210 | struct stat st; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1211 | struct request { |
| 1212 | u_int id; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1213 | size_t len; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1214 | u_int64_t offset; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1215 | TAILQ_ENTRY(request) tq; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1216 | }; |
| 1217 | TAILQ_HEAD(reqhead, request) requests; |
| 1218 | struct request *req; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1219 | u_char type; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1220 | |
| 1221 | TAILQ_INIT(&requests); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1222 | |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1223 | if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL) |
| 1224 | return -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1225 | |
Damien Miller | 9e72028 | 2008-06-29 22:46:35 +1000 | [diff] [blame] | 1226 | /* Do not preserve set[ug]id here, as we do not preserve ownership */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1227 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) |
Damien Miller | 770b374 | 2003-01-08 14:04:53 +1100 | [diff] [blame] | 1228 | mode = a->perm & 0777; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1229 | else |
| 1230 | mode = 0666; |
| 1231 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 1232 | if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && |
Damien Miller | 5fa01fd | 2003-01-14 22:24:47 +1100 | [diff] [blame] | 1233 | (!S_ISREG(a->perm))) { |
| 1234 | error("Cannot download non-regular file: %s", remote_path); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 1235 | return(-1); |
| 1236 | } |
| 1237 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1238 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) |
| 1239 | size = a->size; |
| 1240 | else |
| 1241 | size = 0; |
| 1242 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1243 | buflen = conn->transfer_buflen; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1244 | if ((msg = sshbuf_new()) == NULL) |
| 1245 | fatal("%s: sshbuf_new failed", __func__); |
| 1246 | |
| 1247 | attrib_clear(&junk); /* Send empty attributes */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1248 | |
| 1249 | /* Send open request */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1250 | id = conn->msg_id++; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1251 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 || |
| 1252 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 1253 | (r = sshbuf_put_cstring(msg, remote_path)) != 0 || |
| 1254 | (r = sshbuf_put_u32(msg, SSH2_FXF_READ)) != 0 || |
| 1255 | (r = encode_attrib(msg, &junk)) != 0) |
| 1256 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1257 | send_msg(conn, msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1258 | debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1259 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1260 | handle = get_handle(conn, id, &handle_len, |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 1261 | "remote open(\"%s\")", remote_path); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1262 | if (handle == NULL) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1263 | sshbuf_free(msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1264 | return(-1); |
| 1265 | } |
| 1266 | |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1267 | local_fd = open(local_path, |
| 1268 | O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1269 | if (local_fd == -1) { |
| 1270 | error("Couldn't open local file \"%s\" for writing: %s", |
| 1271 | local_path, strerror(errno)); |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1272 | goto fail; |
| 1273 | } |
| 1274 | offset = highwater = 0; |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1275 | if (resume_flag) { |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1276 | if (fstat(local_fd, &st) == -1) { |
| 1277 | error("Unable to stat local file \"%s\": %s", |
| 1278 | local_path, strerror(errno)); |
| 1279 | goto fail; |
| 1280 | } |
Damien Miller | c3d483f | 2014-01-17 11:20:26 +1100 | [diff] [blame] | 1281 | if (st.st_size < 0) { |
| 1282 | error("\"%s\" has negative size", local_path); |
| 1283 | goto fail; |
| 1284 | } |
| 1285 | if ((u_int64_t)st.st_size > size) { |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1286 | error("Unable to resume download of \"%s\": " |
| 1287 | "local file is larger than remote", local_path); |
| 1288 | fail: |
| 1289 | do_close(conn, handle, handle_len); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1290 | sshbuf_free(msg); |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1291 | free(handle); |
Damien Miller | 9275df3 | 2013-12-05 10:26:32 +1100 | [diff] [blame] | 1292 | if (local_fd != -1) |
| 1293 | close(local_fd); |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1294 | return -1; |
| 1295 | } |
| 1296 | offset = highwater = st.st_size; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1297 | } |
| 1298 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1299 | /* Read from remote and write to local */ |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1300 | write_error = read_error = write_errno = num_req = 0; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1301 | max_req = 1; |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1302 | progress_counter = offset; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1303 | |
Damien Miller | 9ba3069 | 2004-03-08 23:12:02 +1100 | [diff] [blame] | 1304 | if (showprogress && size != 0) |
| 1305 | start_progress_meter(remote_path, size, &progress_counter); |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1306 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1307 | while (num_req > 0 || max_req > 0) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1308 | u_char *data; |
| 1309 | size_t len; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1310 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1311 | /* |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1312 | * Simulate EOF on interrupt: stop sending new requests and |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1313 | * allow outstanding requests to drain gracefully |
| 1314 | */ |
| 1315 | if (interrupted) { |
| 1316 | if (num_req == 0) /* If we haven't started yet... */ |
| 1317 | break; |
| 1318 | max_req = 0; |
| 1319 | } |
| 1320 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1321 | /* Send some more requests */ |
| 1322 | while (num_req < max_req) { |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1323 | debug3("Request range %llu -> %llu (%d/%d)", |
Ben Lindstrom | d45f28c | 2002-03-22 01:00:57 +0000 | [diff] [blame] | 1324 | (unsigned long long)offset, |
| 1325 | (unsigned long long)offset + buflen - 1, |
| 1326 | num_req, max_req); |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 1327 | req = xcalloc(1, sizeof(*req)); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1328 | req->id = conn->msg_id++; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1329 | req->len = buflen; |
| 1330 | req->offset = offset; |
| 1331 | offset += buflen; |
| 1332 | num_req++; |
| 1333 | TAILQ_INSERT_TAIL(&requests, req, tq); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1334 | send_read_request(conn, req->id, req->offset, |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1335 | req->len, handle, handle_len); |
| 1336 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1337 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1338 | sshbuf_reset(msg); |
| 1339 | get_msg(conn, msg); |
| 1340 | if ((r = sshbuf_get_u8(msg, &type)) != 0 || |
| 1341 | (r = sshbuf_get_u32(msg, &id)) != 0) |
| 1342 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1343 | debug3("Received reply T:%u I:%u R:%d", type, id, max_req); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1344 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1345 | /* Find the request in our queue */ |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1346 | for (req = TAILQ_FIRST(&requests); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1347 | req != NULL && req->id != id; |
| 1348 | req = TAILQ_NEXT(req, tq)) |
| 1349 | ; |
| 1350 | if (req == NULL) |
| 1351 | fatal("Unexpected reply %u", id); |
| 1352 | |
| 1353 | switch (type) { |
| 1354 | case SSH2_FXP_STATUS: |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1355 | if ((r = sshbuf_get_u32(msg, &status)) != 0) |
| 1356 | fatal("%s: buffer error: %s", |
| 1357 | __func__, ssh_err(r)); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1358 | if (status != SSH2_FX_EOF) |
| 1359 | read_error = 1; |
| 1360 | max_req = 0; |
| 1361 | TAILQ_REMOVE(&requests, req, tq); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1362 | free(req); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1363 | num_req--; |
| 1364 | break; |
| 1365 | case SSH2_FXP_DATA: |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1366 | if ((r = sshbuf_get_string(msg, &data, &len)) != 0) |
| 1367 | fatal("%s: buffer error: %s", |
| 1368 | __func__, ssh_err(r)); |
Ben Lindstrom | eb50545 | 2002-03-22 01:03:15 +0000 | [diff] [blame] | 1369 | debug3("Received data %llu -> %llu", |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1370 | (unsigned long long)req->offset, |
Ben Lindstrom | eb50545 | 2002-03-22 01:03:15 +0000 | [diff] [blame] | 1371 | (unsigned long long)req->offset + len - 1); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1372 | if (len > req->len) |
| 1373 | fatal("Received more data than asked for " |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1374 | "%zu > %zu", len, req->len); |
djm@openbsd.org | a0876bd | 2019-07-12 03:56:21 +0000 | [diff] [blame] | 1375 | lmodified = 1; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1376 | if ((lseek(local_fd, req->offset, SEEK_SET) == -1 || |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 1377 | atomicio(vwrite, local_fd, data, len) != len) && |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1378 | !write_error) { |
| 1379 | write_errno = errno; |
| 1380 | write_error = 1; |
| 1381 | max_req = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1382 | } |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1383 | else if (!reordered && req->offset <= highwater) |
| 1384 | highwater = req->offset + len; |
| 1385 | else if (!reordered && req->offset > highwater) |
| 1386 | reordered = 1; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1387 | progress_counter += len; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1388 | free(data); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1389 | |
| 1390 | if (len == req->len) { |
| 1391 | TAILQ_REMOVE(&requests, req, tq); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1392 | free(req); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1393 | num_req--; |
| 1394 | } else { |
| 1395 | /* Resend the request for the missing data */ |
| 1396 | debug3("Short data block, re-requesting " |
Ben Lindstrom | eb50545 | 2002-03-22 01:03:15 +0000 | [diff] [blame] | 1397 | "%llu -> %llu (%2d)", |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1398 | (unsigned long long)req->offset + len, |
Ben Lindstrom | 83b79e4 | 2002-03-22 01:05:27 +0000 | [diff] [blame] | 1399 | (unsigned long long)req->offset + |
| 1400 | req->len - 1, num_req); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1401 | req->id = conn->msg_id++; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1402 | req->len -= len; |
| 1403 | req->offset += len; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1404 | send_read_request(conn, req->id, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1405 | req->offset, req->len, handle, handle_len); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1406 | /* Reduce the request size */ |
| 1407 | if (len < buflen) |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 1408 | buflen = MAXIMUM(MIN_READ_SIZE, len); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1409 | } |
| 1410 | if (max_req > 0) { /* max_req = 0 iff EOF received */ |
| 1411 | if (size > 0 && offset > size) { |
| 1412 | /* Only one request at a time |
| 1413 | * after the expected EOF */ |
| 1414 | debug3("Finish at %llu (%2d)", |
Ben Lindstrom | eb50545 | 2002-03-22 01:03:15 +0000 | [diff] [blame] | 1415 | (unsigned long long)offset, |
| 1416 | num_req); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1417 | max_req = 1; |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1418 | } else if (max_req <= conn->num_requests) { |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1419 | ++max_req; |
| 1420 | } |
| 1421 | } |
| 1422 | break; |
| 1423 | default: |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1424 | fatal("Expected SSH2_FXP_DATA(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1425 | SSH2_FXP_DATA, type); |
| 1426 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1427 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1428 | |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1429 | if (showprogress && size) |
| 1430 | stop_progress_meter(); |
| 1431 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1432 | /* Sanity check */ |
| 1433 | if (TAILQ_FIRST(&requests) != NULL) |
| 1434 | fatal("Transfer complete, but requests still in queue"); |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1435 | /* Truncate at highest contiguous point to avoid holes on interrupt */ |
| 1436 | if (read_error || write_error || interrupted) { |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1437 | if (reordered && resume_flag) { |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1438 | error("Unable to resume download of \"%s\": " |
| 1439 | "server reordered requests", local_path); |
| 1440 | } |
| 1441 | debug("truncating at %llu", (unsigned long long)highwater); |
djm@openbsd.org | 9cc6842 | 2015-05-28 04:50:53 +0000 | [diff] [blame] | 1442 | if (ftruncate(local_fd, highwater) == -1) |
| 1443 | error("ftruncate \"%s\": %s", local_path, |
| 1444 | strerror(errno)); |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1445 | } |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1446 | if (read_error) { |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1447 | error("Couldn't read from remote file \"%s\" : %s", |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1448 | remote_path, fx2txt(status)); |
Damien Miller | fec029f | 2013-08-21 02:42:12 +1000 | [diff] [blame] | 1449 | status = -1; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1450 | do_close(conn, handle, handle_len); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1451 | } else if (write_error) { |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1452 | error("Couldn't write to \"%s\": %s", local_path, |
| 1453 | strerror(write_errno)); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1454 | status = SSH2_FX_FAILURE; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1455 | do_close(conn, handle, handle_len); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1456 | } else { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1457 | if (do_close(conn, handle, handle_len) != 0 || interrupted) |
| 1458 | status = SSH2_FX_FAILURE; |
| 1459 | else |
| 1460 | status = SSH2_FX_OK; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1461 | /* Override umask and utimes if asked */ |
Ben Lindstrom | 6dc75f5 | 2001-02-17 16:47:47 +0000 | [diff] [blame] | 1462 | #ifdef HAVE_FCHMOD |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1463 | if (preserve_flag && fchmod(local_fd, mode) == -1) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1464 | #else |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1465 | if (preserve_flag && chmod(local_path, mode) == -1) |
Ben Lindstrom | 6dc75f5 | 2001-02-17 16:47:47 +0000 | [diff] [blame] | 1466 | #endif /* HAVE_FCHMOD */ |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1467 | error("Couldn't set mode on \"%s\": %s", local_path, |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 1468 | strerror(errno)); |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1469 | if (preserve_flag && |
| 1470 | (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) { |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1471 | struct timeval tv[2]; |
| 1472 | tv[0].tv_sec = a->atime; |
| 1473 | tv[1].tv_sec = a->mtime; |
| 1474 | tv[0].tv_usec = tv[1].tv_usec = 0; |
| 1475 | if (utimes(local_path, tv) == -1) |
| 1476 | error("Can't set times on \"%s\": %s", |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 1477 | local_path, strerror(errno)); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1478 | } |
djm@openbsd.org | a0876bd | 2019-07-12 03:56:21 +0000 | [diff] [blame] | 1479 | if (resume_flag && !lmodified) |
| 1480 | logit("File \"%s\" was not modified", local_path); |
| 1481 | else if (fsync_flag) { |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1482 | debug("syncing \"%s\"", local_path); |
| 1483 | if (fsync(local_fd) == -1) |
| 1484 | error("Couldn't sync file \"%s\": %s", |
| 1485 | local_path, strerror(errno)); |
| 1486 | } |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 1487 | } |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1488 | close(local_fd); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1489 | sshbuf_free(msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1490 | free(handle); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1491 | |
djm@openbsd.org | f6a59a2 | 2018-05-25 04:25:46 +0000 | [diff] [blame] | 1492 | return status == SSH2_FX_OK ? 0 : -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1493 | } |
| 1494 | |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1495 | static int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1496 | download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst, |
| 1497 | int depth, Attrib *dirattrib, int preserve_flag, int print_flag, |
| 1498 | int resume_flag, int fsync_flag) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1499 | { |
| 1500 | int i, ret = 0; |
| 1501 | SFTP_DIRENT **dir_entries; |
djm@openbsd.org | 1a66079 | 2018-07-31 03:07:24 +0000 | [diff] [blame] | 1502 | char *filename, *new_src = NULL, *new_dst = NULL; |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1503 | mode_t mode = 0777; |
| 1504 | |
| 1505 | if (depth >= MAX_DIR_DEPTH) { |
| 1506 | error("Maximum directory depth exceeded: %d levels", depth); |
| 1507 | return -1; |
| 1508 | } |
| 1509 | |
| 1510 | if (dirattrib == NULL && |
| 1511 | (dirattrib = do_stat(conn, src, 1)) == NULL) { |
| 1512 | error("Unable to stat remote directory \"%s\"", src); |
| 1513 | return -1; |
| 1514 | } |
| 1515 | if (!S_ISDIR(dirattrib->perm)) { |
| 1516 | error("\"%s\" is not a directory", src); |
| 1517 | return -1; |
| 1518 | } |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1519 | if (print_flag) |
schwarze@openbsd.org | 0e059cd | 2016-05-25 23:48:45 +0000 | [diff] [blame] | 1520 | mprintf("Retrieving %s\n", src); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1521 | |
| 1522 | if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) |
| 1523 | mode = dirattrib->perm & 01777; |
| 1524 | else { |
| 1525 | debug("Server did not send permissions for " |
| 1526 | "directory \"%s\"", dst); |
| 1527 | } |
| 1528 | |
| 1529 | if (mkdir(dst, mode) == -1 && errno != EEXIST) { |
| 1530 | error("mkdir %s: %s", dst, strerror(errno)); |
| 1531 | return -1; |
| 1532 | } |
| 1533 | |
| 1534 | if (do_readdir(conn, src, &dir_entries) == -1) { |
| 1535 | error("%s: Failed to get directory contents", src); |
| 1536 | return -1; |
| 1537 | } |
| 1538 | |
| 1539 | for (i = 0; dir_entries[i] != NULL && !interrupted; i++) { |
djm@openbsd.org | 1a66079 | 2018-07-31 03:07:24 +0000 | [diff] [blame] | 1540 | free(new_dst); |
| 1541 | free(new_src); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1542 | |
djm@openbsd.org | 1a66079 | 2018-07-31 03:07:24 +0000 | [diff] [blame] | 1543 | filename = dir_entries[i]->filename; |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1544 | new_dst = path_append(dst, filename); |
| 1545 | new_src = path_append(src, filename); |
| 1546 | |
| 1547 | if (S_ISDIR(dir_entries[i]->a.perm)) { |
| 1548 | if (strcmp(filename, ".") == 0 || |
| 1549 | strcmp(filename, "..") == 0) |
| 1550 | continue; |
| 1551 | if (download_dir_internal(conn, new_src, new_dst, |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1552 | depth + 1, &(dir_entries[i]->a), preserve_flag, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1553 | print_flag, resume_flag, fsync_flag) == -1) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1554 | ret = -1; |
| 1555 | } else if (S_ISREG(dir_entries[i]->a.perm) ) { |
| 1556 | if (do_download(conn, new_src, new_dst, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1557 | &(dir_entries[i]->a), preserve_flag, |
| 1558 | resume_flag, fsync_flag) == -1) { |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1559 | error("Download of file %s to %s failed", |
| 1560 | new_src, new_dst); |
| 1561 | ret = -1; |
| 1562 | } |
| 1563 | } else |
| 1564 | logit("%s: not a regular file\n", new_src); |
| 1565 | |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1566 | } |
djm@openbsd.org | 1a66079 | 2018-07-31 03:07:24 +0000 | [diff] [blame] | 1567 | free(new_dst); |
| 1568 | free(new_src); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1569 | |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1570 | if (preserve_flag) { |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1571 | if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
| 1572 | struct timeval tv[2]; |
| 1573 | tv[0].tv_sec = dirattrib->atime; |
| 1574 | tv[1].tv_sec = dirattrib->mtime; |
| 1575 | tv[0].tv_usec = tv[1].tv_usec = 0; |
| 1576 | if (utimes(dst, tv) == -1) |
| 1577 | error("Can't set times on \"%s\": %s", |
| 1578 | dst, strerror(errno)); |
| 1579 | } else |
| 1580 | debug("Server did not send times for directory " |
| 1581 | "\"%s\"", dst); |
| 1582 | } |
| 1583 | |
| 1584 | free_sftp_dirents(dir_entries); |
| 1585 | |
| 1586 | return ret; |
| 1587 | } |
| 1588 | |
| 1589 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1590 | download_dir(struct sftp_conn *conn, const char *src, const char *dst, |
| 1591 | Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag, |
| 1592 | int fsync_flag) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1593 | { |
| 1594 | char *src_canon; |
| 1595 | int ret; |
| 1596 | |
| 1597 | if ((src_canon = do_realpath(conn, src)) == NULL) { |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1598 | error("Unable to canonicalize path \"%s\"", src); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1599 | return -1; |
| 1600 | } |
| 1601 | |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1602 | ret = download_dir_internal(conn, src_canon, dst, 0, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1603 | dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1604 | free(src_canon); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1605 | return ret; |
| 1606 | } |
| 1607 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1608 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1609 | do_upload(struct sftp_conn *conn, const char *local_path, |
| 1610 | const char *remote_path, int preserve_flag, int resume, int fsync_flag) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1611 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1612 | int r, local_fd; |
| 1613 | u_int status = SSH2_FX_OK; |
| 1614 | u_int id; |
| 1615 | u_char type; |
Darren Tucker | c9a1991 | 2013-06-02 08:37:05 +1000 | [diff] [blame] | 1616 | off_t offset, progress_counter; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1617 | u_char *handle, *data; |
| 1618 | struct sshbuf *msg; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1619 | struct stat sb; |
Damien Miller | d8accc0 | 2014-05-15 13:46:25 +1000 | [diff] [blame] | 1620 | Attrib a, *c = NULL; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1621 | u_int32_t startid; |
| 1622 | u_int32_t ackid; |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1623 | struct outstanding_ack { |
| 1624 | u_int id; |
| 1625 | u_int len; |
Damien Miller | 8b3fdfb | 2007-09-17 16:12:03 +1000 | [diff] [blame] | 1626 | off_t offset; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1627 | TAILQ_ENTRY(outstanding_ack) tq; |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1628 | }; |
| 1629 | TAILQ_HEAD(ackhead, outstanding_ack) acks; |
Damien Miller | 7cf17eb | 2004-06-15 10:28:56 +1000 | [diff] [blame] | 1630 | struct outstanding_ack *ack = NULL; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1631 | size_t handle_len; |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1632 | |
| 1633 | TAILQ_INIT(&acks); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1634 | |
| 1635 | if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) { |
| 1636 | error("Couldn't open local file \"%s\" for reading: %s", |
| 1637 | local_path, strerror(errno)); |
| 1638 | return(-1); |
| 1639 | } |
| 1640 | if (fstat(local_fd, &sb) == -1) { |
| 1641 | error("Couldn't fstat local file \"%s\": %s", |
| 1642 | local_path, strerror(errno)); |
| 1643 | close(local_fd); |
| 1644 | return(-1); |
| 1645 | } |
Damien Miller | 5fa01fd | 2003-01-14 22:24:47 +1100 | [diff] [blame] | 1646 | if (!S_ISREG(sb.st_mode)) { |
| 1647 | error("%s is not a regular file", local_path); |
| 1648 | close(local_fd); |
| 1649 | return(-1); |
| 1650 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1651 | stat_to_attrib(&sb, &a); |
| 1652 | |
| 1653 | a.flags &= ~SSH2_FILEXFER_ATTR_SIZE; |
| 1654 | a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID; |
| 1655 | a.perm &= 0777; |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1656 | if (!preserve_flag) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1657 | a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME; |
| 1658 | |
Damien Miller | d8accc0 | 2014-05-15 13:46:25 +1000 | [diff] [blame] | 1659 | if (resume) { |
| 1660 | /* Get remote file size if it exists */ |
| 1661 | if ((c = do_stat(conn, remote_path, 0)) == NULL) { |
djm@openbsd.org | 34a01b2 | 2016-04-08 08:19:17 +0000 | [diff] [blame] | 1662 | close(local_fd); |
Damien Miller | d8accc0 | 2014-05-15 13:46:25 +1000 | [diff] [blame] | 1663 | return -1; |
| 1664 | } |
| 1665 | |
| 1666 | if ((off_t)c->size >= sb.st_size) { |
| 1667 | error("destination file bigger or same size as " |
| 1668 | "source file"); |
| 1669 | close(local_fd); |
| 1670 | return -1; |
| 1671 | } |
| 1672 | |
| 1673 | if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) { |
| 1674 | close(local_fd); |
| 1675 | return -1; |
| 1676 | } |
| 1677 | } |
| 1678 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1679 | if ((msg = sshbuf_new()) == NULL) |
| 1680 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1681 | |
| 1682 | /* Send open request */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1683 | id = conn->msg_id++; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1684 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 || |
| 1685 | (r = sshbuf_put_u32(msg, id)) != 0 || |
| 1686 | (r = sshbuf_put_cstring(msg, remote_path)) != 0 || |
| 1687 | (r = sshbuf_put_u32(msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT| |
| 1688 | (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC))) != 0 || |
| 1689 | (r = encode_attrib(msg, &a)) != 0) |
| 1690 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1691 | send_msg(conn, msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1692 | debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1693 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1694 | sshbuf_reset(msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1695 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1696 | handle = get_handle(conn, id, &handle_len, |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 1697 | "remote open(\"%s\")", remote_path); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1698 | if (handle == NULL) { |
| 1699 | close(local_fd); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1700 | sshbuf_free(msg); |
Damien Miller | acdf25b | 2008-02-10 22:27:24 +1100 | [diff] [blame] | 1701 | return -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1702 | } |
| 1703 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1704 | startid = ackid = id + 1; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1705 | data = xmalloc(conn->transfer_buflen); |
Damien Miller | 8829d36 | 2002-02-08 22:04:05 +1100 | [diff] [blame] | 1706 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1707 | /* Read from local and write to remote */ |
Damien Miller | d8accc0 | 2014-05-15 13:46:25 +1000 | [diff] [blame] | 1708 | offset = progress_counter = (resume ? c->size : 0); |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1709 | if (showprogress) |
Darren Tucker | c9a1991 | 2013-06-02 08:37:05 +1000 | [diff] [blame] | 1710 | start_progress_meter(local_path, sb.st_size, |
| 1711 | &progress_counter); |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1712 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1713 | for (;;) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1714 | int len; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1715 | |
| 1716 | /* |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1717 | * Can't use atomicio here because it returns 0 on EOF, |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1718 | * thus losing the last block of the file. |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1719 | * Simulate an EOF on interrupt, allowing ACKs from the |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1720 | * server to drain. |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1721 | */ |
Damien Miller | acdf25b | 2008-02-10 22:27:24 +1100 | [diff] [blame] | 1722 | if (interrupted || status != SSH2_FX_OK) |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1723 | len = 0; |
| 1724 | else do |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1725 | len = read(local_fd, data, conn->transfer_buflen); |
Damien Miller | d8968ad | 2008-07-04 23:10:49 +1000 | [diff] [blame] | 1726 | while ((len == -1) && |
| 1727 | (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1728 | |
| 1729 | if (len == -1) |
| 1730 | fatal("Couldn't read from \"%s\": %s", local_path, |
| 1731 | strerror(errno)); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1732 | |
| 1733 | if (len != 0) { |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 1734 | ack = xcalloc(1, sizeof(*ack)); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1735 | ack->id = ++id; |
| 1736 | ack->offset = offset; |
| 1737 | ack->len = len; |
| 1738 | TAILQ_INSERT_TAIL(&acks, ack, tq); |
| 1739 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1740 | sshbuf_reset(msg); |
| 1741 | if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 || |
| 1742 | (r = sshbuf_put_u32(msg, ack->id)) != 0 || |
| 1743 | (r = sshbuf_put_string(msg, handle, |
| 1744 | handle_len)) != 0 || |
| 1745 | (r = sshbuf_put_u64(msg, offset)) != 0 || |
| 1746 | (r = sshbuf_put_string(msg, data, len)) != 0) |
| 1747 | fatal("%s: buffer error: %s", |
| 1748 | __func__, ssh_err(r)); |
| 1749 | send_msg(conn, msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1750 | debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u", |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 1751 | id, (unsigned long long)offset, len); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1752 | } else if (TAILQ_FIRST(&acks) == NULL) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1753 | break; |
| 1754 | |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1755 | if (ack == NULL) |
| 1756 | fatal("Unexpected ACK %u", id); |
| 1757 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1758 | if (id == startid || len == 0 || |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1759 | id - ackid >= conn->num_requests) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1760 | u_int rid; |
Ben Lindstrom | 06e9515 | 2002-04-06 04:16:45 +0000 | [diff] [blame] | 1761 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1762 | sshbuf_reset(msg); |
| 1763 | get_msg(conn, msg); |
| 1764 | if ((r = sshbuf_get_u8(msg, &type)) != 0 || |
| 1765 | (r = sshbuf_get_u32(msg, &rid)) != 0) |
| 1766 | fatal("%s: buffer error: %s", |
| 1767 | __func__, ssh_err(r)); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1768 | |
| 1769 | if (type != SSH2_FXP_STATUS) |
| 1770 | fatal("Expected SSH2_FXP_STATUS(%d) packet, " |
| 1771 | "got %d", SSH2_FXP_STATUS, type); |
| 1772 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1773 | if ((r = sshbuf_get_u32(msg, &status)) != 0) |
| 1774 | fatal("%s: buffer error: %s", |
| 1775 | __func__, ssh_err(r)); |
| 1776 | debug3("SSH2_FXP_STATUS %u", status); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1777 | |
| 1778 | /* Find the request in our queue */ |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1779 | for (ack = TAILQ_FIRST(&acks); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1780 | ack != NULL && ack->id != rid; |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1781 | ack = TAILQ_NEXT(ack, tq)) |
| 1782 | ; |
| 1783 | if (ack == NULL) |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1784 | fatal("Can't find request for ID %u", rid); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1785 | TAILQ_REMOVE(&acks, ack, tq); |
Damien Miller | 8b3fdfb | 2007-09-17 16:12:03 +1000 | [diff] [blame] | 1786 | debug3("In write loop, ack for %u %u bytes at %lld", |
| 1787 | ack->id, ack->len, (long long)ack->offset); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1788 | ++ackid; |
Darren Tucker | c9a1991 | 2013-06-02 08:37:05 +1000 | [diff] [blame] | 1789 | progress_counter += ack->len; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1790 | free(ack); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1791 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1792 | offset += len; |
Damien Miller | 8b3fdfb | 2007-09-17 16:12:03 +1000 | [diff] [blame] | 1793 | if (offset < 0) |
| 1794 | fatal("%s: offset < 0", __func__); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1795 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1796 | sshbuf_free(msg); |
Damien Miller | acdf25b | 2008-02-10 22:27:24 +1100 | [diff] [blame] | 1797 | |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1798 | if (showprogress) |
| 1799 | stop_progress_meter(); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1800 | free(data); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1801 | |
Damien Miller | acdf25b | 2008-02-10 22:27:24 +1100 | [diff] [blame] | 1802 | if (status != SSH2_FX_OK) { |
| 1803 | error("Couldn't write to remote file \"%s\": %s", |
| 1804 | remote_path, fx2txt(status)); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1805 | status = SSH2_FX_FAILURE; |
Damien Miller | acdf25b | 2008-02-10 22:27:24 +1100 | [diff] [blame] | 1806 | } |
| 1807 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1808 | if (close(local_fd) == -1) { |
| 1809 | error("Couldn't close local file \"%s\": %s", local_path, |
| 1810 | strerror(errno)); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1811 | status = SSH2_FX_FAILURE; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1812 | } |
| 1813 | |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 1814 | /* Override umask and utimes if asked */ |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1815 | if (preserve_flag) |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1816 | do_fsetstat(conn, handle, handle_len, &a); |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 1817 | |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1818 | if (fsync_flag) |
| 1819 | (void)do_fsync(conn, handle, handle_len); |
| 1820 | |
djm@openbsd.org | e30cabf | 2016-02-11 02:21:34 +0000 | [diff] [blame] | 1821 | if (do_close(conn, handle, handle_len) != 0) |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1822 | status = SSH2_FX_FAILURE; |
| 1823 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1824 | free(handle); |
Damien Miller | acdf25b | 2008-02-10 22:27:24 +1100 | [diff] [blame] | 1825 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1826 | return status == SSH2_FX_OK ? 0 : -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1827 | } |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1828 | |
| 1829 | static int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1830 | upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst, |
| 1831 | int depth, int preserve_flag, int print_flag, int resume, int fsync_flag) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1832 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1833 | int ret = 0; |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1834 | DIR *dirp; |
| 1835 | struct dirent *dp; |
djm@openbsd.org | 1a66079 | 2018-07-31 03:07:24 +0000 | [diff] [blame] | 1836 | char *filename, *new_src = NULL, *new_dst = NULL; |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1837 | struct stat sb; |
djm@openbsd.org | e30cabf | 2016-02-11 02:21:34 +0000 | [diff] [blame] | 1838 | Attrib a, *dirattrib; |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1839 | |
| 1840 | if (depth >= MAX_DIR_DEPTH) { |
| 1841 | error("Maximum directory depth exceeded: %d levels", depth); |
| 1842 | return -1; |
| 1843 | } |
| 1844 | |
| 1845 | if (stat(src, &sb) == -1) { |
| 1846 | error("Couldn't stat directory \"%s\": %s", |
| 1847 | src, strerror(errno)); |
| 1848 | return -1; |
| 1849 | } |
| 1850 | if (!S_ISDIR(sb.st_mode)) { |
| 1851 | error("\"%s\" is not a directory", src); |
| 1852 | return -1; |
| 1853 | } |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1854 | if (print_flag) |
schwarze@openbsd.org | 0e059cd | 2016-05-25 23:48:45 +0000 | [diff] [blame] | 1855 | mprintf("Entering %s\n", src); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1856 | |
| 1857 | attrib_clear(&a); |
| 1858 | stat_to_attrib(&sb, &a); |
| 1859 | a.flags &= ~SSH2_FILEXFER_ATTR_SIZE; |
| 1860 | a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID; |
| 1861 | a.perm &= 01777; |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1862 | if (!preserve_flag) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1863 | a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME; |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1864 | |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1865 | /* |
djm@openbsd.org | e30cabf | 2016-02-11 02:21:34 +0000 | [diff] [blame] | 1866 | * sftp lacks a portable status value to match errno EEXIST, |
| 1867 | * so if we get a failure back then we must check whether |
| 1868 | * the path already existed and is a directory. |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1869 | */ |
djm@openbsd.org | e30cabf | 2016-02-11 02:21:34 +0000 | [diff] [blame] | 1870 | if (do_mkdir(conn, dst, &a, 0) != 0) { |
| 1871 | if ((dirattrib = do_stat(conn, dst, 0)) == NULL) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1872 | return -1; |
djm@openbsd.org | e30cabf | 2016-02-11 02:21:34 +0000 | [diff] [blame] | 1873 | if (!S_ISDIR(dirattrib->perm)) { |
| 1874 | error("\"%s\" exists but is not a directory", dst); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1875 | return -1; |
djm@openbsd.org | e30cabf | 2016-02-11 02:21:34 +0000 | [diff] [blame] | 1876 | } |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | if ((dirp = opendir(src)) == NULL) { |
| 1880 | error("Failed to open dir \"%s\": %s", src, strerror(errno)); |
| 1881 | return -1; |
| 1882 | } |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1883 | |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1884 | while (((dp = readdir(dirp)) != NULL) && !interrupted) { |
| 1885 | if (dp->d_ino == 0) |
| 1886 | continue; |
djm@openbsd.org | 1a66079 | 2018-07-31 03:07:24 +0000 | [diff] [blame] | 1887 | free(new_dst); |
| 1888 | free(new_src); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1889 | filename = dp->d_name; |
| 1890 | new_dst = path_append(dst, filename); |
| 1891 | new_src = path_append(src, filename); |
| 1892 | |
Darren Tucker | 438b473 | 2009-10-11 21:52:10 +1100 | [diff] [blame] | 1893 | if (lstat(new_src, &sb) == -1) { |
| 1894 | logit("%s: lstat failed: %s", filename, |
| 1895 | strerror(errno)); |
| 1896 | ret = -1; |
| 1897 | } else if (S_ISDIR(sb.st_mode)) { |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1898 | if (strcmp(filename, ".") == 0 || |
| 1899 | strcmp(filename, "..") == 0) |
| 1900 | continue; |
| 1901 | |
| 1902 | if (upload_dir_internal(conn, new_src, new_dst, |
Damien Miller | d8accc0 | 2014-05-15 13:46:25 +1000 | [diff] [blame] | 1903 | depth + 1, preserve_flag, print_flag, resume, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1904 | fsync_flag) == -1) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1905 | ret = -1; |
Darren Tucker | 438b473 | 2009-10-11 21:52:10 +1100 | [diff] [blame] | 1906 | } else if (S_ISREG(sb.st_mode)) { |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1907 | if (do_upload(conn, new_src, new_dst, |
Damien Miller | d8accc0 | 2014-05-15 13:46:25 +1000 | [diff] [blame] | 1908 | preserve_flag, resume, fsync_flag) == -1) { |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1909 | error("Uploading of file %s to %s failed!", |
| 1910 | new_src, new_dst); |
| 1911 | ret = -1; |
| 1912 | } |
| 1913 | } else |
| 1914 | logit("%s: not a regular file\n", filename); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1915 | } |
djm@openbsd.org | 1a66079 | 2018-07-31 03:07:24 +0000 | [diff] [blame] | 1916 | free(new_dst); |
| 1917 | free(new_src); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1918 | |
| 1919 | do_setstat(conn, dst, &a); |
| 1920 | |
| 1921 | (void) closedir(dirp); |
| 1922 | return ret; |
| 1923 | } |
| 1924 | |
| 1925 | int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1926 | upload_dir(struct sftp_conn *conn, const char *src, const char *dst, |
| 1927 | int preserve_flag, int print_flag, int resume, int fsync_flag) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1928 | { |
| 1929 | char *dst_canon; |
| 1930 | int ret; |
| 1931 | |
| 1932 | if ((dst_canon = do_realpath(conn, dst)) == NULL) { |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1933 | error("Unable to canonicalize path \"%s\"", dst); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1934 | return -1; |
| 1935 | } |
| 1936 | |
Damien Miller | 2f93d05 | 2013-10-15 12:06:27 +1100 | [diff] [blame] | 1937 | ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag, |
Damien Miller | d8accc0 | 2014-05-15 13:46:25 +1000 | [diff] [blame] | 1938 | print_flag, resume, fsync_flag); |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1939 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1940 | free(dst_canon); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1941 | return ret; |
| 1942 | } |
| 1943 | |
| 1944 | char * |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1945 | path_append(const char *p1, const char *p2) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1946 | { |
| 1947 | char *ret; |
| 1948 | size_t len = strlen(p1) + strlen(p2) + 2; |
| 1949 | |
| 1950 | ret = xmalloc(len); |
| 1951 | strlcpy(ret, p1, len); |
| 1952 | if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/') |
| 1953 | strlcat(ret, "/", len); |
| 1954 | strlcat(ret, p2, len); |
| 1955 | |
| 1956 | return(ret); |
| 1957 | } |
| 1958 | |