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