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