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