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