Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1 | /* |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 2 | * Copyright (c) 2001-2003 Damien Miller. All rights reserved. |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
| 25 | /* XXX: memleaks */ |
| 26 | /* XXX: signed vs unsigned */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 27 | /* XXX: remove all logging, only return status codes */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 28 | /* XXX: copy between two remote sites */ |
| 29 | |
| 30 | #include "includes.h" |
Damien Miller | 5fa01fd | 2003-01-14 22:24:47 +1100 | [diff] [blame] | 31 | RCSID("$OpenBSD: sftp-client.c,v 1.41 2003/01/14 10:58:00 djm Exp $"); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 32 | |
Damien Miller | 9b48151 | 2002-09-12 10:43:29 +1000 | [diff] [blame] | 33 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 34 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 35 | #include "buffer.h" |
| 36 | #include "bufaux.h" |
| 37 | #include "getput.h" |
| 38 | #include "xmalloc.h" |
| 39 | #include "log.h" |
| 40 | #include "atomicio.h" |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 41 | #include "progressmeter.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 42 | |
| 43 | #include "sftp.h" |
| 44 | #include "sftp-common.h" |
| 45 | #include "sftp-client.h" |
| 46 | |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 47 | extern int showprogress; |
| 48 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 49 | /* Minimum amount of data to read at at time */ |
| 50 | #define MIN_READ_SIZE 512 |
| 51 | |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 52 | /* Maximum packet size */ |
| 53 | #define MAX_MSG_LENGTH (256 * 1024) |
| 54 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 55 | struct sftp_conn { |
| 56 | int fd_in; |
| 57 | int fd_out; |
| 58 | u_int transfer_buflen; |
| 59 | u_int num_requests; |
| 60 | u_int version; |
| 61 | u_int msg_id; |
| 62 | }; |
Ben Lindstrom | 288cc39 | 2001-02-09 02:58:04 +0000 | [diff] [blame] | 63 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 64 | static void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 65 | send_msg(int fd, Buffer *m) |
| 66 | { |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 67 | u_char mlen[4]; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 68 | |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 69 | if (buffer_len(m) > MAX_MSG_LENGTH) |
| 70 | fatal("Outbound message too long %u", buffer_len(m)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 71 | |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 72 | /* Send length first */ |
| 73 | PUT_32BIT(mlen, buffer_len(m)); |
| 74 | if (atomicio(write, fd, mlen, sizeof(mlen)) <= 0) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 75 | fatal("Couldn't send packet: %s", strerror(errno)); |
| 76 | |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 77 | if (atomicio(write, fd, buffer_ptr(m), buffer_len(m)) <= 0) |
| 78 | fatal("Couldn't send packet: %s", strerror(errno)); |
| 79 | |
| 80 | buffer_clear(m); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 81 | } |
| 82 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 83 | static void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 84 | get_msg(int fd, Buffer *m) |
| 85 | { |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 86 | ssize_t len; |
| 87 | u_int msg_len; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 88 | |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 89 | buffer_append_space(m, 4); |
| 90 | len = atomicio(read, fd, buffer_ptr(m), 4); |
Damien Miller | cafff19 | 2001-03-19 22:29:46 +1100 | [diff] [blame] | 91 | if (len == 0) |
| 92 | fatal("Connection closed"); |
| 93 | else if (len == -1) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 94 | fatal("Couldn't read packet: %s", strerror(errno)); |
| 95 | |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 96 | msg_len = buffer_get_int(m); |
| 97 | if (msg_len > MAX_MSG_LENGTH) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 98 | fatal("Received message too long %u", msg_len); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 99 | |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 100 | buffer_append_space(m, msg_len); |
| 101 | len = atomicio(read, fd, buffer_ptr(m), msg_len); |
| 102 | if (len == 0) |
| 103 | fatal("Connection closed"); |
| 104 | else if (len == -1) |
| 105 | fatal("Read packet: %s", strerror(errno)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 106 | } |
| 107 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 108 | static void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 109 | send_string_request(int fd, u_int id, u_int code, char *s, |
| 110 | u_int len) |
| 111 | { |
| 112 | Buffer msg; |
| 113 | |
| 114 | buffer_init(&msg); |
| 115 | buffer_put_char(&msg, code); |
| 116 | buffer_put_int(&msg, id); |
| 117 | buffer_put_string(&msg, s, len); |
| 118 | send_msg(fd, &msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 119 | debug3("Sent message fd %d T:%u I:%u", fd, code, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 120 | buffer_free(&msg); |
| 121 | } |
| 122 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 123 | static void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 124 | send_string_attrs_request(int fd, u_int id, u_int code, char *s, |
| 125 | u_int len, Attrib *a) |
| 126 | { |
| 127 | Buffer msg; |
| 128 | |
| 129 | buffer_init(&msg); |
| 130 | buffer_put_char(&msg, code); |
| 131 | buffer_put_int(&msg, id); |
| 132 | buffer_put_string(&msg, s, len); |
| 133 | encode_attrib(&msg, a); |
| 134 | send_msg(fd, &msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 135 | debug3("Sent message fd %d T:%u I:%u", fd, code, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 136 | buffer_free(&msg); |
| 137 | } |
| 138 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 139 | static u_int |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 140 | get_status(int fd, u_int expected_id) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 141 | { |
| 142 | Buffer msg; |
| 143 | u_int type, id, status; |
| 144 | |
| 145 | buffer_init(&msg); |
| 146 | get_msg(fd, &msg); |
| 147 | type = buffer_get_char(&msg); |
| 148 | id = buffer_get_int(&msg); |
| 149 | |
| 150 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 151 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 152 | if (type != SSH2_FXP_STATUS) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 153 | fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 154 | SSH2_FXP_STATUS, type); |
| 155 | |
| 156 | status = buffer_get_int(&msg); |
| 157 | buffer_free(&msg); |
| 158 | |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 159 | debug3("SSH2_FXP_STATUS %u", status); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 160 | |
| 161 | return(status); |
| 162 | } |
| 163 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 164 | static char * |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 165 | get_handle(int fd, u_int expected_id, u_int *len) |
| 166 | { |
| 167 | Buffer msg; |
| 168 | u_int type, id; |
| 169 | char *handle; |
| 170 | |
| 171 | buffer_init(&msg); |
| 172 | get_msg(fd, &msg); |
| 173 | type = buffer_get_char(&msg); |
| 174 | id = buffer_get_int(&msg); |
| 175 | |
| 176 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 177 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 178 | if (type == SSH2_FXP_STATUS) { |
| 179 | int status = buffer_get_int(&msg); |
| 180 | |
| 181 | error("Couldn't get handle: %s", fx2txt(status)); |
| 182 | return(NULL); |
| 183 | } else if (type != SSH2_FXP_HANDLE) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 184 | fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 185 | SSH2_FXP_HANDLE, type); |
| 186 | |
| 187 | handle = buffer_get_string(&msg, len); |
| 188 | buffer_free(&msg); |
| 189 | |
| 190 | return(handle); |
| 191 | } |
| 192 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 193 | static Attrib * |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 194 | get_decode_stat(int fd, u_int expected_id, int quiet) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 195 | { |
| 196 | Buffer msg; |
| 197 | u_int type, id; |
| 198 | Attrib *a; |
| 199 | |
| 200 | buffer_init(&msg); |
| 201 | get_msg(fd, &msg); |
| 202 | |
| 203 | type = buffer_get_char(&msg); |
| 204 | id = buffer_get_int(&msg); |
| 205 | |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 206 | debug3("Received stat reply T:%u I:%u", type, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 207 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 208 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 209 | if (type == SSH2_FXP_STATUS) { |
| 210 | int status = buffer_get_int(&msg); |
| 211 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 212 | if (quiet) |
| 213 | debug("Couldn't stat remote file: %s", fx2txt(status)); |
| 214 | else |
| 215 | error("Couldn't stat remote file: %s", fx2txt(status)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 216 | return(NULL); |
| 217 | } else if (type != SSH2_FXP_ATTRS) { |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 218 | fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 219 | SSH2_FXP_ATTRS, type); |
| 220 | } |
| 221 | a = decode_attrib(&msg); |
| 222 | buffer_free(&msg); |
| 223 | |
| 224 | return(a); |
| 225 | } |
| 226 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 227 | struct sftp_conn * |
| 228 | do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 229 | { |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 230 | u_int type; |
| 231 | int version; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 232 | Buffer msg; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 233 | struct sftp_conn *ret; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 234 | |
| 235 | buffer_init(&msg); |
| 236 | buffer_put_char(&msg, SSH2_FXP_INIT); |
| 237 | buffer_put_int(&msg, SSH2_FILEXFER_VERSION); |
| 238 | send_msg(fd_out, &msg); |
| 239 | |
| 240 | buffer_clear(&msg); |
| 241 | |
| 242 | get_msg(fd_in, &msg); |
| 243 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 244 | /* Expecting a VERSION reply */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 245 | if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) { |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 246 | error("Invalid packet back from SSH2_FXP_INIT (type %u)", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 247 | type); |
| 248 | buffer_free(&msg); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 249 | return(NULL); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 250 | } |
| 251 | version = buffer_get_int(&msg); |
| 252 | |
| 253 | debug2("Remote version: %d", version); |
| 254 | |
| 255 | /* Check for extensions */ |
| 256 | while (buffer_len(&msg) > 0) { |
| 257 | char *name = buffer_get_string(&msg, NULL); |
| 258 | char *value = buffer_get_string(&msg, NULL); |
| 259 | |
| 260 | debug2("Init extension: \"%s\"", name); |
| 261 | xfree(name); |
| 262 | xfree(value); |
| 263 | } |
| 264 | |
| 265 | buffer_free(&msg); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 266 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 267 | ret = xmalloc(sizeof(*ret)); |
| 268 | ret->fd_in = fd_in; |
| 269 | ret->fd_out = fd_out; |
| 270 | ret->transfer_buflen = transfer_buflen; |
| 271 | ret->num_requests = num_requests; |
| 272 | ret->version = version; |
| 273 | ret->msg_id = 1; |
| 274 | |
| 275 | /* Some filexfer v.0 servers don't support large packets */ |
| 276 | if (version == 0) |
Ben Lindstrom | a1d8114 | 2002-04-02 20:58:11 +0000 | [diff] [blame] | 277 | ret->transfer_buflen = MIN(ret->transfer_buflen, 20480); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 278 | |
| 279 | return(ret); |
| 280 | } |
| 281 | |
| 282 | u_int |
| 283 | sftp_proto_version(struct sftp_conn *conn) |
| 284 | { |
| 285 | return(conn->version); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 289 | do_close(struct sftp_conn *conn, char *handle, u_int handle_len) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 290 | { |
| 291 | u_int id, status; |
| 292 | Buffer msg; |
| 293 | |
| 294 | buffer_init(&msg); |
| 295 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 296 | id = conn->msg_id++; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 297 | buffer_put_char(&msg, SSH2_FXP_CLOSE); |
| 298 | buffer_put_int(&msg, id); |
| 299 | buffer_put_string(&msg, handle, handle_len); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 300 | send_msg(conn->fd_out, &msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 301 | debug3("Sent message SSH2_FXP_CLOSE I:%u", id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 302 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 303 | status = get_status(conn->fd_in, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 304 | if (status != SSH2_FX_OK) |
| 305 | error("Couldn't close file: %s", fx2txt(status)); |
| 306 | |
| 307 | buffer_free(&msg); |
| 308 | |
| 309 | return(status); |
| 310 | } |
| 311 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 312 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 313 | static int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 314 | do_lsreaddir(struct sftp_conn *conn, char *path, int printflag, |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 315 | SFTP_DIRENT ***dir) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 316 | { |
| 317 | Buffer msg; |
Ben Lindstrom | 025df4a | 2001-03-14 15:16:34 +0000 | [diff] [blame] | 318 | u_int type, id, handle_len, i, expected_id, ents = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 319 | char *handle; |
| 320 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 321 | id = conn->msg_id++; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 322 | |
| 323 | buffer_init(&msg); |
| 324 | buffer_put_char(&msg, SSH2_FXP_OPENDIR); |
| 325 | buffer_put_int(&msg, id); |
| 326 | buffer_put_cstring(&msg, path); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 327 | send_msg(conn->fd_out, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 328 | |
| 329 | buffer_clear(&msg); |
| 330 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 331 | handle = get_handle(conn->fd_in, id, &handle_len); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 332 | if (handle == NULL) |
| 333 | return(-1); |
| 334 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 335 | if (dir) { |
| 336 | ents = 0; |
| 337 | *dir = xmalloc(sizeof(**dir)); |
| 338 | (*dir)[0] = NULL; |
| 339 | } |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 340 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 341 | for (;;) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 342 | int count; |
| 343 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 344 | id = expected_id = conn->msg_id++; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 345 | |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 346 | debug3("Sending SSH2_FXP_READDIR I:%u", id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 347 | |
| 348 | buffer_clear(&msg); |
| 349 | buffer_put_char(&msg, SSH2_FXP_READDIR); |
| 350 | buffer_put_int(&msg, id); |
| 351 | buffer_put_string(&msg, handle, handle_len); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 352 | send_msg(conn->fd_out, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 353 | |
| 354 | buffer_clear(&msg); |
| 355 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 356 | get_msg(conn->fd_in, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 357 | |
| 358 | type = buffer_get_char(&msg); |
| 359 | id = buffer_get_int(&msg); |
| 360 | |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 361 | debug3("Received reply T:%u I:%u", type, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 362 | |
| 363 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 364 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 365 | |
| 366 | if (type == SSH2_FXP_STATUS) { |
| 367 | int status = buffer_get_int(&msg); |
| 368 | |
| 369 | debug3("Received SSH2_FXP_STATUS %d", status); |
| 370 | |
| 371 | if (status == SSH2_FX_EOF) { |
| 372 | break; |
| 373 | } else { |
| 374 | error("Couldn't read directory: %s", |
| 375 | fx2txt(status)); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 376 | do_close(conn, handle, handle_len); |
Ben Lindstrom | 10ac33f | 2001-02-10 21:53:40 +0000 | [diff] [blame] | 377 | return(status); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 378 | } |
| 379 | } else if (type != SSH2_FXP_NAME) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 380 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 381 | SSH2_FXP_NAME, type); |
| 382 | |
| 383 | count = buffer_get_int(&msg); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 384 | if (count == 0) |
| 385 | break; |
| 386 | debug3("Received %d SSH2_FXP_NAME responses", count); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 387 | for (i = 0; i < count; i++) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 388 | char *filename, *longname; |
| 389 | Attrib *a; |
| 390 | |
| 391 | filename = buffer_get_string(&msg, NULL); |
| 392 | longname = buffer_get_string(&msg, NULL); |
| 393 | a = decode_attrib(&msg); |
| 394 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 395 | if (printflag) |
| 396 | printf("%s\n", longname); |
| 397 | |
| 398 | if (dir) { |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 399 | *dir = xrealloc(*dir, sizeof(**dir) * |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 400 | (ents + 2)); |
| 401 | (*dir)[ents] = xmalloc(sizeof(***dir)); |
| 402 | (*dir)[ents]->filename = xstrdup(filename); |
| 403 | (*dir)[ents]->longname = xstrdup(longname); |
| 404 | memcpy(&(*dir)[ents]->a, a, sizeof(*a)); |
| 405 | (*dir)[++ents] = NULL; |
| 406 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 407 | |
| 408 | xfree(filename); |
| 409 | xfree(longname); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | buffer_free(&msg); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 414 | do_close(conn, handle, handle_len); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 415 | xfree(handle); |
| 416 | |
| 417 | return(0); |
| 418 | } |
| 419 | |
| 420 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 421 | do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 422 | { |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 423 | return(do_lsreaddir(conn, path, 0, dir)); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void free_sftp_dirents(SFTP_DIRENT **s) |
| 427 | { |
| 428 | int i; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 429 | |
| 430 | for (i = 0; s[i]; i++) { |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 431 | xfree(s[i]->filename); |
| 432 | xfree(s[i]->longname); |
| 433 | xfree(s[i]); |
| 434 | } |
| 435 | xfree(s); |
| 436 | } |
| 437 | |
| 438 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 439 | do_rm(struct sftp_conn *conn, char *path) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 440 | { |
| 441 | u_int status, id; |
| 442 | |
| 443 | debug2("Sending SSH2_FXP_REMOVE \"%s\"", path); |
| 444 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 445 | id = conn->msg_id++; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 446 | send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 447 | strlen(path)); |
| 448 | status = get_status(conn->fd_in, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 449 | if (status != SSH2_FX_OK) |
| 450 | error("Couldn't delete file: %s", fx2txt(status)); |
| 451 | return(status); |
| 452 | } |
| 453 | |
| 454 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 455 | do_mkdir(struct sftp_conn *conn, char *path, Attrib *a) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 456 | { |
| 457 | u_int status, id; |
| 458 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 459 | id = conn->msg_id++; |
| 460 | send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 461 | strlen(path), a); |
| 462 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 463 | status = get_status(conn->fd_in, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 464 | if (status != SSH2_FX_OK) |
| 465 | error("Couldn't create directory: %s", fx2txt(status)); |
| 466 | |
| 467 | return(status); |
| 468 | } |
| 469 | |
| 470 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 471 | do_rmdir(struct sftp_conn *conn, char *path) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 472 | { |
| 473 | u_int status, id; |
| 474 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 475 | id = conn->msg_id++; |
| 476 | send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path, |
| 477 | strlen(path)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 478 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 479 | status = get_status(conn->fd_in, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 480 | if (status != SSH2_FX_OK) |
| 481 | error("Couldn't remove directory: %s", fx2txt(status)); |
| 482 | |
| 483 | return(status); |
| 484 | } |
| 485 | |
| 486 | Attrib * |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 487 | do_stat(struct sftp_conn *conn, char *path, int quiet) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 488 | { |
| 489 | u_int id; |
| 490 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 491 | id = conn->msg_id++; |
| 492 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 493 | send_string_request(conn->fd_out, id, |
| 494 | conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 495 | path, strlen(path)); |
| 496 | |
| 497 | return(get_decode_stat(conn->fd_in, id, quiet)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | Attrib * |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 501 | do_lstat(struct sftp_conn *conn, char *path, int quiet) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 502 | { |
| 503 | u_int id; |
| 504 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 505 | if (conn->version == 0) { |
| 506 | if (quiet) |
| 507 | debug("Server version does not support lstat operation"); |
| 508 | else |
Ben Lindstrom | f26ff5b | 2002-04-02 21:00:31 +0000 | [diff] [blame] | 509 | log("Server version does not support lstat operation"); |
| 510 | return(do_stat(conn, path, quiet)); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | id = conn->msg_id++; |
| 514 | send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path, |
| 515 | strlen(path)); |
| 516 | |
| 517 | return(get_decode_stat(conn->fd_in, id, quiet)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | Attrib * |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 521 | do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 522 | { |
| 523 | u_int id; |
| 524 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 525 | id = conn->msg_id++; |
| 526 | send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle, |
| 527 | handle_len); |
| 528 | |
| 529 | return(get_decode_stat(conn->fd_in, id, quiet)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 533 | do_setstat(struct sftp_conn *conn, char *path, Attrib *a) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 534 | { |
| 535 | u_int status, id; |
| 536 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 537 | id = conn->msg_id++; |
| 538 | send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 539 | strlen(path), a); |
| 540 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 541 | status = get_status(conn->fd_in, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 542 | if (status != SSH2_FX_OK) |
| 543 | error("Couldn't setstat on \"%s\": %s", path, |
| 544 | fx2txt(status)); |
| 545 | |
| 546 | return(status); |
| 547 | } |
| 548 | |
| 549 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 550 | do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 551 | Attrib *a) |
| 552 | { |
| 553 | u_int status, id; |
| 554 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 555 | id = conn->msg_id++; |
| 556 | send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 557 | handle_len, a); |
| 558 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 559 | status = get_status(conn->fd_in, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 560 | if (status != SSH2_FX_OK) |
| 561 | error("Couldn't fsetstat: %s", fx2txt(status)); |
| 562 | |
| 563 | return(status); |
| 564 | } |
| 565 | |
| 566 | char * |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 567 | do_realpath(struct sftp_conn *conn, char *path) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 568 | { |
| 569 | Buffer msg; |
| 570 | u_int type, expected_id, count, id; |
| 571 | char *filename, *longname; |
| 572 | Attrib *a; |
| 573 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 574 | expected_id = id = conn->msg_id++; |
| 575 | send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path, |
| 576 | strlen(path)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 577 | |
| 578 | buffer_init(&msg); |
| 579 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 580 | get_msg(conn->fd_in, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 581 | type = buffer_get_char(&msg); |
| 582 | id = buffer_get_int(&msg); |
| 583 | |
| 584 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 585 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 586 | |
| 587 | if (type == SSH2_FXP_STATUS) { |
| 588 | u_int status = buffer_get_int(&msg); |
| 589 | |
| 590 | error("Couldn't canonicalise: %s", fx2txt(status)); |
| 591 | return(NULL); |
| 592 | } else if (type != SSH2_FXP_NAME) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 593 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 594 | SSH2_FXP_NAME, type); |
| 595 | |
| 596 | count = buffer_get_int(&msg); |
| 597 | if (count != 1) |
| 598 | fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count); |
| 599 | |
| 600 | filename = buffer_get_string(&msg, NULL); |
| 601 | longname = buffer_get_string(&msg, NULL); |
| 602 | a = decode_attrib(&msg); |
| 603 | |
| 604 | debug3("SSH_FXP_REALPATH %s -> %s", path, filename); |
| 605 | |
| 606 | xfree(longname); |
| 607 | |
| 608 | buffer_free(&msg); |
| 609 | |
| 610 | return(filename); |
| 611 | } |
| 612 | |
| 613 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 614 | do_rename(struct sftp_conn *conn, char *oldpath, char *newpath) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 615 | { |
| 616 | Buffer msg; |
| 617 | u_int status, id; |
| 618 | |
| 619 | buffer_init(&msg); |
| 620 | |
| 621 | /* Send rename request */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 622 | id = conn->msg_id++; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 623 | buffer_put_char(&msg, SSH2_FXP_RENAME); |
| 624 | buffer_put_int(&msg, id); |
| 625 | buffer_put_cstring(&msg, oldpath); |
| 626 | buffer_put_cstring(&msg, newpath); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 627 | send_msg(conn->fd_out, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 628 | debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath, |
| 629 | newpath); |
| 630 | buffer_free(&msg); |
| 631 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 632 | status = get_status(conn->fd_in, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 633 | if (status != SSH2_FX_OK) |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 634 | error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, |
| 635 | newpath, fx2txt(status)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 636 | |
| 637 | return(status); |
| 638 | } |
| 639 | |
| 640 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 641 | do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath) |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 642 | { |
| 643 | Buffer msg; |
| 644 | u_int status, id; |
| 645 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 646 | if (conn->version < 3) { |
| 647 | error("This server does not support the symlink operation"); |
| 648 | return(SSH2_FX_OP_UNSUPPORTED); |
| 649 | } |
| 650 | |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 651 | buffer_init(&msg); |
| 652 | |
| 653 | /* Send rename request */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 654 | id = conn->msg_id++; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 655 | buffer_put_char(&msg, SSH2_FXP_SYMLINK); |
| 656 | buffer_put_int(&msg, id); |
| 657 | buffer_put_cstring(&msg, oldpath); |
| 658 | buffer_put_cstring(&msg, newpath); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 659 | send_msg(conn->fd_out, &msg); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 660 | debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath, |
| 661 | newpath); |
| 662 | buffer_free(&msg); |
| 663 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 664 | status = get_status(conn->fd_in, id); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 665 | if (status != SSH2_FX_OK) |
Ben Lindstrom | 8e879cf | 2002-11-09 15:48:49 +0000 | [diff] [blame] | 666 | error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 667 | newpath, fx2txt(status)); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 668 | |
| 669 | return(status); |
| 670 | } |
| 671 | |
| 672 | char * |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 673 | do_readlink(struct sftp_conn *conn, char *path) |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 674 | { |
| 675 | Buffer msg; |
| 676 | u_int type, expected_id, count, id; |
| 677 | char *filename, *longname; |
| 678 | Attrib *a; |
| 679 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 680 | expected_id = id = conn->msg_id++; |
| 681 | send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path, |
| 682 | strlen(path)); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 683 | |
| 684 | buffer_init(&msg); |
| 685 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 686 | get_msg(conn->fd_in, &msg); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 687 | type = buffer_get_char(&msg); |
| 688 | id = buffer_get_int(&msg); |
| 689 | |
| 690 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 691 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 692 | |
| 693 | if (type == SSH2_FXP_STATUS) { |
| 694 | u_int status = buffer_get_int(&msg); |
| 695 | |
| 696 | error("Couldn't readlink: %s", fx2txt(status)); |
| 697 | return(NULL); |
| 698 | } else if (type != SSH2_FXP_NAME) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 699 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 700 | SSH2_FXP_NAME, type); |
| 701 | |
| 702 | count = buffer_get_int(&msg); |
| 703 | if (count != 1) |
| 704 | fatal("Got multiple names (%d) from SSH_FXP_READLINK", count); |
| 705 | |
| 706 | filename = buffer_get_string(&msg, NULL); |
| 707 | longname = buffer_get_string(&msg, NULL); |
| 708 | a = decode_attrib(&msg); |
| 709 | |
| 710 | debug3("SSH_FXP_READLINK %s -> %s", path, filename); |
| 711 | |
| 712 | xfree(longname); |
| 713 | |
| 714 | buffer_free(&msg); |
| 715 | |
| 716 | return(filename); |
| 717 | } |
| 718 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 719 | static void |
| 720 | send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len, |
| 721 | char *handle, u_int handle_len) |
| 722 | { |
| 723 | Buffer msg; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 724 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 725 | buffer_init(&msg); |
| 726 | buffer_clear(&msg); |
| 727 | buffer_put_char(&msg, SSH2_FXP_READ); |
| 728 | buffer_put_int(&msg, id); |
| 729 | buffer_put_string(&msg, handle, handle_len); |
| 730 | buffer_put_int64(&msg, offset); |
| 731 | buffer_put_int(&msg, len); |
| 732 | send_msg(fd_out, &msg); |
| 733 | buffer_free(&msg); |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 734 | } |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 735 | |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 736 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 737 | do_download(struct sftp_conn *conn, char *remote_path, char *local_path, |
| 738 | int pflag) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 739 | { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 740 | Attrib junk, *a; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 741 | Buffer msg; |
| 742 | char *handle; |
| 743 | int local_fd, status, num_req, max_req, write_error; |
| 744 | int read_error, write_errno; |
| 745 | u_int64_t offset, size; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 746 | u_int handle_len, mode, type, id, buflen; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 747 | off_t progress_counter; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 748 | struct request { |
| 749 | u_int id; |
| 750 | u_int len; |
| 751 | u_int64_t offset; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 752 | TAILQ_ENTRY(request) tq; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 753 | }; |
| 754 | TAILQ_HEAD(reqhead, request) requests; |
| 755 | struct request *req; |
| 756 | |
| 757 | TAILQ_INIT(&requests); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 758 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 759 | a = do_stat(conn, remote_path, 0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 760 | if (a == NULL) |
| 761 | return(-1); |
| 762 | |
| 763 | /* XXX: should we preserve set[ug]id? */ |
| 764 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) |
Damien Miller | 770b374 | 2003-01-08 14:04:53 +1100 | [diff] [blame] | 765 | mode = a->perm & 0777; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 766 | else |
| 767 | mode = 0666; |
| 768 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 769 | if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && |
Damien Miller | 5fa01fd | 2003-01-14 22:24:47 +1100 | [diff] [blame] | 770 | (!S_ISREG(a->perm))) { |
| 771 | error("Cannot download non-regular file: %s", remote_path); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 772 | return(-1); |
| 773 | } |
| 774 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 775 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) |
| 776 | size = a->size; |
| 777 | else |
| 778 | size = 0; |
| 779 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 780 | buflen = conn->transfer_buflen; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 781 | buffer_init(&msg); |
| 782 | |
| 783 | /* Send open request */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 784 | id = conn->msg_id++; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 785 | buffer_put_char(&msg, SSH2_FXP_OPEN); |
| 786 | buffer_put_int(&msg, id); |
| 787 | buffer_put_cstring(&msg, remote_path); |
| 788 | buffer_put_int(&msg, SSH2_FXF_READ); |
| 789 | attrib_clear(&junk); /* Send empty attributes */ |
| 790 | encode_attrib(&msg, &junk); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 791 | send_msg(conn->fd_out, &msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 792 | 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] | 793 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 794 | handle = get_handle(conn->fd_in, id, &handle_len); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 795 | if (handle == NULL) { |
| 796 | buffer_free(&msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 797 | return(-1); |
| 798 | } |
| 799 | |
Damien Miller | 770b374 | 2003-01-08 14:04:53 +1100 | [diff] [blame] | 800 | local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, |
| 801 | mode | S_IWRITE); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 802 | if (local_fd == -1) { |
| 803 | error("Couldn't open local file \"%s\" for writing: %s", |
| 804 | local_path, strerror(errno)); |
Ben Lindstrom | 021fcd3 | 2002-02-26 18:02:43 +0000 | [diff] [blame] | 805 | buffer_free(&msg); |
| 806 | xfree(handle); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 807 | return(-1); |
| 808 | } |
| 809 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 810 | /* Read from remote and write to local */ |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 811 | write_error = read_error = write_errno = num_req = offset = 0; |
| 812 | max_req = 1; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 813 | progress_counter = 0; |
| 814 | |
| 815 | if (showprogress) { |
| 816 | if (size) |
| 817 | start_progress_meter(remote_path, size, |
| 818 | &progress_counter); |
| 819 | else |
| 820 | printf("Fetching %s to %s\n", remote_path, local_path); |
| 821 | } |
| 822 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 823 | while (num_req > 0 || max_req > 0) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 824 | char *data; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 825 | u_int len; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 826 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 827 | /* Send some more requests */ |
| 828 | while (num_req < max_req) { |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 829 | debug3("Request range %llu -> %llu (%d/%d)", |
Ben Lindstrom | d45f28c | 2002-03-22 01:00:57 +0000 | [diff] [blame] | 830 | (unsigned long long)offset, |
| 831 | (unsigned long long)offset + buflen - 1, |
| 832 | num_req, max_req); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 833 | req = xmalloc(sizeof(*req)); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 834 | req->id = conn->msg_id++; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 835 | req->len = buflen; |
| 836 | req->offset = offset; |
| 837 | offset += buflen; |
| 838 | num_req++; |
| 839 | TAILQ_INSERT_TAIL(&requests, req, tq); |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 840 | send_read_request(conn->fd_out, req->id, req->offset, |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 841 | req->len, handle, handle_len); |
| 842 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 843 | |
| 844 | buffer_clear(&msg); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 845 | get_msg(conn->fd_in, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 846 | type = buffer_get_char(&msg); |
| 847 | id = buffer_get_int(&msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 848 | 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] | 849 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 850 | /* Find the request in our queue */ |
| 851 | for(req = TAILQ_FIRST(&requests); |
| 852 | req != NULL && req->id != id; |
| 853 | req = TAILQ_NEXT(req, tq)) |
| 854 | ; |
| 855 | if (req == NULL) |
| 856 | fatal("Unexpected reply %u", id); |
| 857 | |
| 858 | switch (type) { |
| 859 | case SSH2_FXP_STATUS: |
| 860 | status = buffer_get_int(&msg); |
| 861 | if (status != SSH2_FX_EOF) |
| 862 | read_error = 1; |
| 863 | max_req = 0; |
| 864 | TAILQ_REMOVE(&requests, req, tq); |
| 865 | xfree(req); |
| 866 | num_req--; |
| 867 | break; |
| 868 | case SSH2_FXP_DATA: |
| 869 | data = buffer_get_string(&msg, &len); |
Ben Lindstrom | eb50545 | 2002-03-22 01:03:15 +0000 | [diff] [blame] | 870 | debug3("Received data %llu -> %llu", |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 871 | (unsigned long long)req->offset, |
Ben Lindstrom | eb50545 | 2002-03-22 01:03:15 +0000 | [diff] [blame] | 872 | (unsigned long long)req->offset + len - 1); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 873 | if (len > req->len) |
| 874 | fatal("Received more data than asked for " |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 875 | "%u > %u", len, req->len); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 876 | if ((lseek(local_fd, req->offset, SEEK_SET) == -1 || |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 877 | atomicio(write, local_fd, data, len) != len) && |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 878 | !write_error) { |
| 879 | write_errno = errno; |
| 880 | write_error = 1; |
| 881 | max_req = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 882 | } |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 883 | progress_counter += len; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 884 | xfree(data); |
| 885 | |
| 886 | if (len == req->len) { |
| 887 | TAILQ_REMOVE(&requests, req, tq); |
| 888 | xfree(req); |
| 889 | num_req--; |
| 890 | } else { |
| 891 | /* Resend the request for the missing data */ |
| 892 | debug3("Short data block, re-requesting " |
Ben Lindstrom | eb50545 | 2002-03-22 01:03:15 +0000 | [diff] [blame] | 893 | "%llu -> %llu (%2d)", |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 894 | (unsigned long long)req->offset + len, |
Ben Lindstrom | 83b79e4 | 2002-03-22 01:05:27 +0000 | [diff] [blame] | 895 | (unsigned long long)req->offset + |
| 896 | req->len - 1, num_req); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 897 | req->id = conn->msg_id++; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 898 | req->len -= len; |
| 899 | req->offset += len; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 900 | send_read_request(conn->fd_out, req->id, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 901 | req->offset, req->len, handle, handle_len); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 902 | /* Reduce the request size */ |
| 903 | if (len < buflen) |
| 904 | buflen = MAX(MIN_READ_SIZE, len); |
| 905 | } |
| 906 | if (max_req > 0) { /* max_req = 0 iff EOF received */ |
| 907 | if (size > 0 && offset > size) { |
| 908 | /* Only one request at a time |
| 909 | * after the expected EOF */ |
| 910 | debug3("Finish at %llu (%2d)", |
Ben Lindstrom | eb50545 | 2002-03-22 01:03:15 +0000 | [diff] [blame] | 911 | (unsigned long long)offset, |
| 912 | num_req); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 913 | max_req = 1; |
| 914 | } |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 915 | else if (max_req < conn->num_requests + 1) { |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 916 | ++max_req; |
| 917 | } |
| 918 | } |
| 919 | break; |
| 920 | default: |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 921 | fatal("Expected SSH2_FXP_DATA(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 922 | SSH2_FXP_DATA, type); |
| 923 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 924 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 925 | |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 926 | if (showprogress && size) |
| 927 | stop_progress_meter(); |
| 928 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 929 | /* Sanity check */ |
| 930 | if (TAILQ_FIRST(&requests) != NULL) |
| 931 | fatal("Transfer complete, but requests still in queue"); |
| 932 | |
| 933 | if (read_error) { |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 934 | error("Couldn't read from remote file \"%s\" : %s", |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 935 | remote_path, fx2txt(status)); |
| 936 | do_close(conn, handle, handle_len); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 937 | } else if (write_error) { |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 938 | error("Couldn't write to \"%s\": %s", local_path, |
| 939 | strerror(write_errno)); |
| 940 | status = -1; |
| 941 | do_close(conn, handle, handle_len); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 942 | } else { |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 943 | status = do_close(conn, handle, handle_len); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 944 | |
| 945 | /* Override umask and utimes if asked */ |
Ben Lindstrom | 6dc75f5 | 2001-02-17 16:47:47 +0000 | [diff] [blame] | 946 | #ifdef HAVE_FCHMOD |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 947 | if (pflag && fchmod(local_fd, mode) == -1) |
Ben Lindstrom | 6dc75f5 | 2001-02-17 16:47:47 +0000 | [diff] [blame] | 948 | #else |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 949 | if (pflag && chmod(local_path, mode) == -1) |
Ben Lindstrom | 6dc75f5 | 2001-02-17 16:47:47 +0000 | [diff] [blame] | 950 | #endif /* HAVE_FCHMOD */ |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 951 | error("Couldn't set mode on \"%s\": %s", local_path, |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 952 | strerror(errno)); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 953 | if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) { |
| 954 | struct timeval tv[2]; |
| 955 | tv[0].tv_sec = a->atime; |
| 956 | tv[1].tv_sec = a->mtime; |
| 957 | tv[0].tv_usec = tv[1].tv_usec = 0; |
| 958 | if (utimes(local_path, tv) == -1) |
| 959 | error("Can't set times on \"%s\": %s", |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 960 | local_path, strerror(errno)); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 961 | } |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 962 | } |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 963 | close(local_fd); |
| 964 | buffer_free(&msg); |
| 965 | xfree(handle); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 966 | |
| 967 | return(status); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 971 | do_upload(struct sftp_conn *conn, char *local_path, char *remote_path, |
| 972 | int pflag) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 973 | { |
Damien Miller | 8829d36 | 2002-02-08 22:04:05 +1100 | [diff] [blame] | 974 | int local_fd, status; |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 975 | u_int handle_len, id, type; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 976 | u_int64_t offset; |
Damien Miller | 8829d36 | 2002-02-08 22:04:05 +1100 | [diff] [blame] | 977 | char *handle, *data; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 978 | Buffer msg; |
| 979 | struct stat sb; |
| 980 | Attrib a; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 981 | u_int32_t startid; |
| 982 | u_int32_t ackid; |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 983 | struct outstanding_ack { |
| 984 | u_int id; |
| 985 | u_int len; |
| 986 | u_int64_t offset; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 987 | TAILQ_ENTRY(outstanding_ack) tq; |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 988 | }; |
| 989 | TAILQ_HEAD(ackhead, outstanding_ack) acks; |
| 990 | struct outstanding_ack *ack; |
| 991 | |
| 992 | TAILQ_INIT(&acks); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 993 | |
| 994 | if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) { |
| 995 | error("Couldn't open local file \"%s\" for reading: %s", |
| 996 | local_path, strerror(errno)); |
| 997 | return(-1); |
| 998 | } |
| 999 | if (fstat(local_fd, &sb) == -1) { |
| 1000 | error("Couldn't fstat local file \"%s\": %s", |
| 1001 | local_path, strerror(errno)); |
| 1002 | close(local_fd); |
| 1003 | return(-1); |
| 1004 | } |
Damien Miller | 5fa01fd | 2003-01-14 22:24:47 +1100 | [diff] [blame] | 1005 | if (!S_ISREG(sb.st_mode)) { |
| 1006 | error("%s is not a regular file", local_path); |
| 1007 | close(local_fd); |
| 1008 | return(-1); |
| 1009 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1010 | stat_to_attrib(&sb, &a); |
| 1011 | |
| 1012 | a.flags &= ~SSH2_FILEXFER_ATTR_SIZE; |
| 1013 | a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID; |
| 1014 | a.perm &= 0777; |
| 1015 | if (!pflag) |
| 1016 | a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME; |
| 1017 | |
| 1018 | buffer_init(&msg); |
| 1019 | |
| 1020 | /* Send open request */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1021 | id = conn->msg_id++; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1022 | buffer_put_char(&msg, SSH2_FXP_OPEN); |
| 1023 | buffer_put_int(&msg, id); |
| 1024 | buffer_put_cstring(&msg, remote_path); |
| 1025 | buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC); |
| 1026 | encode_attrib(&msg, &a); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1027 | send_msg(conn->fd_out, &msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1028 | 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] | 1029 | |
| 1030 | buffer_clear(&msg); |
| 1031 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1032 | handle = get_handle(conn->fd_in, id, &handle_len); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1033 | if (handle == NULL) { |
| 1034 | close(local_fd); |
| 1035 | buffer_free(&msg); |
| 1036 | return(-1); |
| 1037 | } |
| 1038 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1039 | startid = ackid = id + 1; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1040 | data = xmalloc(conn->transfer_buflen); |
Damien Miller | 8829d36 | 2002-02-08 22:04:05 +1100 | [diff] [blame] | 1041 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1042 | /* Read from local and write to remote */ |
| 1043 | offset = 0; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1044 | if (showprogress) |
| 1045 | start_progress_meter(local_path, sb.st_size, &offset); |
| 1046 | else |
| 1047 | printf("Uploading %s to %s\n", local_path, remote_path); |
| 1048 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1049 | for (;;) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1050 | int len; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1051 | |
| 1052 | /* |
| 1053 | * Can't use atomicio here because it returns 0 on EOF, thus losing |
| 1054 | * the last block of the file |
| 1055 | */ |
| 1056 | do |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1057 | len = read(local_fd, data, conn->transfer_buflen); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1058 | while ((len == -1) && (errno == EINTR || errno == EAGAIN)); |
| 1059 | |
| 1060 | if (len == -1) |
| 1061 | fatal("Couldn't read from \"%s\": %s", local_path, |
| 1062 | strerror(errno)); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1063 | |
| 1064 | if (len != 0) { |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1065 | ack = xmalloc(sizeof(*ack)); |
| 1066 | ack->id = ++id; |
| 1067 | ack->offset = offset; |
| 1068 | ack->len = len; |
| 1069 | TAILQ_INSERT_TAIL(&acks, ack, tq); |
| 1070 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1071 | buffer_clear(&msg); |
| 1072 | buffer_put_char(&msg, SSH2_FXP_WRITE); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1073 | buffer_put_int(&msg, ack->id); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1074 | buffer_put_string(&msg, handle, handle_len); |
| 1075 | buffer_put_int64(&msg, offset); |
| 1076 | buffer_put_string(&msg, data, len); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1077 | send_msg(conn->fd_out, &msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1078 | debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u", |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 1079 | id, (unsigned long long)offset, len); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1080 | } else if (TAILQ_FIRST(&acks) == NULL) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1081 | break; |
| 1082 | |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1083 | if (ack == NULL) |
| 1084 | fatal("Unexpected ACK %u", id); |
| 1085 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1086 | if (id == startid || len == 0 || |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1087 | id - ackid >= conn->num_requests) { |
Ben Lindstrom | 5a6abda | 2002-06-09 19:41:48 +0000 | [diff] [blame] | 1088 | u_int r_id; |
Ben Lindstrom | 06e9515 | 2002-04-06 04:16:45 +0000 | [diff] [blame] | 1089 | |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1090 | buffer_clear(&msg); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1091 | get_msg(conn->fd_in, &msg); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1092 | type = buffer_get_char(&msg); |
Ben Lindstrom | 06e9515 | 2002-04-06 04:16:45 +0000 | [diff] [blame] | 1093 | r_id = buffer_get_int(&msg); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1094 | |
| 1095 | if (type != SSH2_FXP_STATUS) |
| 1096 | fatal("Expected SSH2_FXP_STATUS(%d) packet, " |
| 1097 | "got %d", SSH2_FXP_STATUS, type); |
| 1098 | |
| 1099 | status = buffer_get_int(&msg); |
| 1100 | debug3("SSH2_FXP_STATUS %d", status); |
| 1101 | |
| 1102 | /* Find the request in our queue */ |
| 1103 | for(ack = TAILQ_FIRST(&acks); |
Ben Lindstrom | 06e9515 | 2002-04-06 04:16:45 +0000 | [diff] [blame] | 1104 | ack != NULL && ack->id != r_id; |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1105 | ack = TAILQ_NEXT(ack, tq)) |
| 1106 | ; |
| 1107 | if (ack == NULL) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1108 | fatal("Can't find request for ID %u", r_id); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1109 | TAILQ_REMOVE(&acks, ack, tq); |
| 1110 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1111 | if (status != SSH2_FX_OK) { |
| 1112 | error("Couldn't write to remote file \"%s\": %s", |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 1113 | remote_path, fx2txt(status)); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1114 | do_close(conn, handle, handle_len); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1115 | close(local_fd); |
| 1116 | goto done; |
| 1117 | } |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1118 | debug3("In write loop, ack for %u %u bytes at %llu", |
Ben Lindstrom | eb50545 | 2002-03-22 01:03:15 +0000 | [diff] [blame] | 1119 | ack->id, ack->len, (unsigned long long)ack->offset); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1120 | ++ackid; |
Ben Lindstrom | eec16fc | 2002-07-04 00:06:15 +0000 | [diff] [blame] | 1121 | xfree(ack); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1122 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1123 | offset += len; |
| 1124 | } |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1125 | if (showprogress) |
| 1126 | stop_progress_meter(); |
Damien Miller | 8829d36 | 2002-02-08 22:04:05 +1100 | [diff] [blame] | 1127 | xfree(data); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1128 | |
| 1129 | if (close(local_fd) == -1) { |
| 1130 | error("Couldn't close local file \"%s\": %s", local_path, |
| 1131 | strerror(errno)); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1132 | do_close(conn, handle, handle_len); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1133 | status = -1; |
| 1134 | goto done; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1135 | } |
| 1136 | |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 1137 | /* Override umask and utimes if asked */ |
| 1138 | if (pflag) |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1139 | do_fsetstat(conn, handle, handle_len, &a); |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 1140 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1141 | status = do_close(conn, handle, handle_len); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1142 | |
| 1143 | done: |
| 1144 | xfree(handle); |
| 1145 | buffer_free(&msg); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1146 | return(status); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1147 | } |