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