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