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