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