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