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