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