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