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