Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1 | /* $OpenBSD: sftp-client.c,v 1.107 2013/10/17 00:30:13 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 | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 39 | #include <dirent.h> |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 40 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 41 | #include <fcntl.h> |
| 42 | #include <signal.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 43 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 44 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 45 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 46 | #include <unistd.h> |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 47 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 48 | #include "xmalloc.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 49 | #include "buffer.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 50 | #include "log.h" |
| 51 | #include "atomicio.h" |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 52 | #include "progressmeter.h" |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 53 | #include "misc.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 54 | |
| 55 | #include "sftp.h" |
| 56 | #include "sftp-common.h" |
| 57 | #include "sftp-client.h" |
| 58 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 59 | extern volatile sig_atomic_t interrupted; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 60 | extern int showprogress; |
| 61 | |
Damien Miller | 0c8d8f6 | 2006-03-15 11:34:25 +1100 | [diff] [blame] | 62 | /* Minimum amount of data to read at a time */ |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 63 | #define MIN_READ_SIZE 512 |
| 64 | |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 65 | /* Maximum depth to descend in directory trees */ |
| 66 | #define MAX_DIR_DEPTH 64 |
| 67 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 68 | struct sftp_conn { |
| 69 | int fd_in; |
| 70 | int fd_out; |
| 71 | u_int transfer_buflen; |
| 72 | u_int num_requests; |
| 73 | u_int version; |
| 74 | u_int msg_id; |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 75 | #define SFTP_EXT_POSIX_RENAME 0x00000001 |
| 76 | #define SFTP_EXT_STATVFS 0x00000002 |
| 77 | #define SFTP_EXT_FSTATVFS 0x00000004 |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 78 | #define SFTP_EXT_HARDLINK 0x00000008 |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 79 | #define SFTP_EXT_FSYNC 0x00000010 |
Damien Miller | 7a3e1d0 | 2008-03-27 10:59:57 +1100 | [diff] [blame] | 80 | u_int exts; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 81 | u_int64_t limit_kbps; |
| 82 | struct bwlimit bwlimit_in, bwlimit_out; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 83 | }; |
Ben Lindstrom | 288cc39 | 2001-02-09 02:58:04 +0000 | [diff] [blame] | 84 | |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 85 | static char * |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 86 | get_handle(struct sftp_conn *conn, u_int expected_id, u_int *len, |
| 87 | const char *errfmt, ...) __attribute__((format(printf, 4, 5))); |
| 88 | |
| 89 | /* ARGSUSED */ |
| 90 | static int |
| 91 | sftpio(void *_bwlimit, size_t amount) |
| 92 | { |
| 93 | struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit; |
| 94 | |
| 95 | bandwidth_limit(bwlimit, amount); |
| 96 | return 0; |
| 97 | } |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 98 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 99 | static void |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 100 | send_msg(struct sftp_conn *conn, Buffer *m) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 101 | { |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 102 | u_char mlen[4]; |
Damien Miller | 58ca98b | 2006-04-23 12:06:35 +1000 | [diff] [blame] | 103 | struct iovec iov[2]; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 104 | |
Damien Miller | 5444618 | 2006-01-02 23:40:50 +1100 | [diff] [blame] | 105 | if (buffer_len(m) > SFTP_MAX_MSG_LENGTH) |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 106 | fatal("Outbound message too long %u", buffer_len(m)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 107 | |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 108 | /* Send length first */ |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 109 | put_u32(mlen, buffer_len(m)); |
Damien Miller | 58ca98b | 2006-04-23 12:06:35 +1000 | [diff] [blame] | 110 | iov[0].iov_base = mlen; |
| 111 | iov[0].iov_len = sizeof(mlen); |
| 112 | iov[1].iov_base = buffer_ptr(m); |
| 113 | iov[1].iov_len = buffer_len(m); |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 114 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 115 | if (atomiciov6(writev, conn->fd_out, iov, 2, |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 116 | conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_out) != |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 117 | buffer_len(m) + sizeof(mlen)) |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 118 | fatal("Couldn't send packet: %s", strerror(errno)); |
| 119 | |
| 120 | buffer_clear(m); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 121 | } |
| 122 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 123 | static void |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 124 | get_msg(struct sftp_conn *conn, Buffer *m) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 125 | { |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 126 | u_int msg_len; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 127 | |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 128 | buffer_append_space(m, 4); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 129 | if (atomicio6(read, conn->fd_in, buffer_ptr(m), 4, |
| 130 | conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in) != 4) { |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 131 | if (errno == EPIPE) |
| 132 | fatal("Connection closed"); |
| 133 | else |
| 134 | fatal("Couldn't read packet: %s", strerror(errno)); |
| 135 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 136 | |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 137 | msg_len = buffer_get_int(m); |
Damien Miller | 5444618 | 2006-01-02 23:40:50 +1100 | [diff] [blame] | 138 | if (msg_len > SFTP_MAX_MSG_LENGTH) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 139 | fatal("Received message too long %u", msg_len); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 140 | |
Damien Miller | a7f3aaa | 2003-01-10 21:43:58 +1100 | [diff] [blame] | 141 | buffer_append_space(m, msg_len); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 142 | if (atomicio6(read, conn->fd_in, buffer_ptr(m), msg_len, |
| 143 | conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in) |
| 144 | != msg_len) { |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 145 | if (errno == EPIPE) |
| 146 | fatal("Connection closed"); |
| 147 | else |
| 148 | fatal("Read packet: %s", strerror(errno)); |
| 149 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 150 | } |
| 151 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 152 | static void |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 153 | send_string_request(struct sftp_conn *conn, u_int id, u_int code, char *s, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 154 | u_int len) |
| 155 | { |
| 156 | Buffer msg; |
| 157 | |
| 158 | buffer_init(&msg); |
| 159 | buffer_put_char(&msg, code); |
| 160 | buffer_put_int(&msg, id); |
| 161 | buffer_put_string(&msg, s, len); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 162 | send_msg(conn, &msg); |
| 163 | debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 164 | buffer_free(&msg); |
| 165 | } |
| 166 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 167 | static void |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 168 | send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code, |
| 169 | char *s, u_int len, Attrib *a) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 170 | { |
| 171 | Buffer msg; |
| 172 | |
| 173 | buffer_init(&msg); |
| 174 | buffer_put_char(&msg, code); |
| 175 | buffer_put_int(&msg, id); |
| 176 | buffer_put_string(&msg, s, len); |
| 177 | encode_attrib(&msg, a); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 178 | send_msg(conn, &msg); |
| 179 | debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 180 | buffer_free(&msg); |
| 181 | } |
| 182 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 183 | static u_int |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 184 | get_status(struct sftp_conn *conn, u_int expected_id) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 185 | { |
| 186 | Buffer msg; |
| 187 | u_int type, id, status; |
| 188 | |
| 189 | buffer_init(&msg); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 190 | get_msg(conn, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 191 | type = buffer_get_char(&msg); |
| 192 | id = buffer_get_int(&msg); |
| 193 | |
| 194 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 195 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 196 | if (type != SSH2_FXP_STATUS) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 197 | fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 198 | SSH2_FXP_STATUS, type); |
| 199 | |
| 200 | status = buffer_get_int(&msg); |
| 201 | buffer_free(&msg); |
| 202 | |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 203 | debug3("SSH2_FXP_STATUS %u", status); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 204 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 205 | return status; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 206 | } |
| 207 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 208 | static char * |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 209 | get_handle(struct sftp_conn *conn, u_int expected_id, u_int *len, |
| 210 | const char *errfmt, ...) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 211 | { |
| 212 | Buffer msg; |
| 213 | u_int type, id; |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 214 | char *handle, errmsg[256]; |
| 215 | va_list args; |
| 216 | int status; |
| 217 | |
| 218 | va_start(args, errfmt); |
| 219 | if (errfmt != NULL) |
| 220 | vsnprintf(errmsg, sizeof(errmsg), errfmt, args); |
| 221 | va_end(args); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 222 | |
| 223 | buffer_init(&msg); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 224 | get_msg(conn, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 225 | type = buffer_get_char(&msg); |
| 226 | id = buffer_get_int(&msg); |
| 227 | |
| 228 | if (id != expected_id) |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 229 | fatal("%s: ID mismatch (%u != %u)", |
| 230 | errfmt == NULL ? __func__ : errmsg, id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 231 | if (type == SSH2_FXP_STATUS) { |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 232 | status = buffer_get_int(&msg); |
| 233 | if (errfmt != NULL) |
| 234 | error("%s: %s", errmsg, fx2txt(status)); |
Darren Tucker | cd516ef | 2004-12-06 22:43:43 +1100 | [diff] [blame] | 235 | buffer_free(&msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 236 | return(NULL); |
| 237 | } else if (type != SSH2_FXP_HANDLE) |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 238 | fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u", |
| 239 | errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 240 | |
| 241 | handle = buffer_get_string(&msg, len); |
| 242 | buffer_free(&msg); |
| 243 | |
| 244 | return(handle); |
| 245 | } |
| 246 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 247 | static Attrib * |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 248 | get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 249 | { |
| 250 | Buffer msg; |
| 251 | u_int type, id; |
| 252 | Attrib *a; |
| 253 | |
| 254 | buffer_init(&msg); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 255 | get_msg(conn, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 256 | |
| 257 | type = buffer_get_char(&msg); |
| 258 | id = buffer_get_int(&msg); |
| 259 | |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 260 | debug3("Received stat reply T:%u I:%u", type, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 261 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 262 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 263 | if (type == SSH2_FXP_STATUS) { |
| 264 | int status = buffer_get_int(&msg); |
| 265 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 266 | if (quiet) |
| 267 | debug("Couldn't stat remote file: %s", fx2txt(status)); |
| 268 | else |
| 269 | error("Couldn't stat remote file: %s", fx2txt(status)); |
Darren Tucker | cd516ef | 2004-12-06 22:43:43 +1100 | [diff] [blame] | 270 | buffer_free(&msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 271 | return(NULL); |
| 272 | } else if (type != SSH2_FXP_ATTRS) { |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 273 | fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 274 | SSH2_FXP_ATTRS, type); |
| 275 | } |
| 276 | a = decode_attrib(&msg); |
| 277 | buffer_free(&msg); |
| 278 | |
| 279 | return(a); |
| 280 | } |
| 281 | |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 282 | static int |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 283 | get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st, |
| 284 | u_int expected_id, int quiet) |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 285 | { |
| 286 | Buffer msg; |
| 287 | u_int type, id, flag; |
| 288 | |
| 289 | buffer_init(&msg); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 290 | get_msg(conn, &msg); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 291 | |
| 292 | type = buffer_get_char(&msg); |
| 293 | id = buffer_get_int(&msg); |
| 294 | |
| 295 | debug3("Received statvfs reply T:%u I:%u", type, id); |
| 296 | if (id != expected_id) |
| 297 | fatal("ID mismatch (%u != %u)", id, expected_id); |
| 298 | if (type == SSH2_FXP_STATUS) { |
| 299 | int status = buffer_get_int(&msg); |
| 300 | |
| 301 | if (quiet) |
| 302 | debug("Couldn't statvfs: %s", fx2txt(status)); |
| 303 | else |
| 304 | error("Couldn't statvfs: %s", fx2txt(status)); |
| 305 | buffer_free(&msg); |
| 306 | return -1; |
| 307 | } else if (type != SSH2_FXP_EXTENDED_REPLY) { |
| 308 | fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u", |
| 309 | SSH2_FXP_EXTENDED_REPLY, type); |
| 310 | } |
| 311 | |
| 312 | bzero(st, sizeof(*st)); |
Darren Tucker | 7b59889 | 2008-06-09 22:49:36 +1000 | [diff] [blame] | 313 | st->f_bsize = buffer_get_int64(&msg); |
| 314 | st->f_frsize = buffer_get_int64(&msg); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 315 | st->f_blocks = buffer_get_int64(&msg); |
| 316 | st->f_bfree = buffer_get_int64(&msg); |
| 317 | st->f_bavail = buffer_get_int64(&msg); |
| 318 | st->f_files = buffer_get_int64(&msg); |
| 319 | st->f_ffree = buffer_get_int64(&msg); |
| 320 | st->f_favail = buffer_get_int64(&msg); |
Darren Tucker | 294b841 | 2008-06-08 12:57:08 +1000 | [diff] [blame] | 321 | st->f_fsid = buffer_get_int64(&msg); |
Darren Tucker | 7b59889 | 2008-06-09 22:49:36 +1000 | [diff] [blame] | 322 | flag = buffer_get_int64(&msg); |
| 323 | st->f_namemax = buffer_get_int64(&msg); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 324 | |
| 325 | st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0; |
| 326 | st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0; |
| 327 | |
| 328 | buffer_free(&msg); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 333 | struct sftp_conn * |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 334 | do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests, |
| 335 | u_int64_t limit_kbps) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 336 | { |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 337 | u_int type; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 338 | Buffer msg; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 339 | struct sftp_conn *ret; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 340 | |
Damien Miller | fec029f | 2013-08-21 02:42:12 +1000 | [diff] [blame] | 341 | ret = xcalloc(1, sizeof(*ret)); |
| 342 | ret->msg_id = 1; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 343 | ret->fd_in = fd_in; |
| 344 | ret->fd_out = fd_out; |
| 345 | ret->transfer_buflen = transfer_buflen; |
| 346 | ret->num_requests = num_requests; |
| 347 | ret->exts = 0; |
| 348 | ret->limit_kbps = 0; |
| 349 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 350 | buffer_init(&msg); |
| 351 | buffer_put_char(&msg, SSH2_FXP_INIT); |
| 352 | buffer_put_int(&msg, SSH2_FILEXFER_VERSION); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 353 | send_msg(ret, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 354 | |
| 355 | buffer_clear(&msg); |
| 356 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 357 | get_msg(ret, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 358 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 359 | /* Expecting a VERSION reply */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 360 | if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) { |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 361 | error("Invalid packet back from SSH2_FXP_INIT (type %u)", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 362 | type); |
| 363 | buffer_free(&msg); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 364 | return(NULL); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 365 | } |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 366 | ret->version = buffer_get_int(&msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 367 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 368 | debug2("Remote version: %u", ret->version); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 369 | |
| 370 | /* Check for extensions */ |
| 371 | while (buffer_len(&msg) > 0) { |
| 372 | char *name = buffer_get_string(&msg, NULL); |
| 373 | char *value = buffer_get_string(&msg, NULL); |
Darren Tucker | a64ab33 | 2008-06-13 07:01:29 +1000 | [diff] [blame] | 374 | int known = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 375 | |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 376 | if (strcmp(name, "posix-rename@openssh.com") == 0 && |
Darren Tucker | a64ab33 | 2008-06-13 07:01:29 +1000 | [diff] [blame] | 377 | strcmp(value, "1") == 0) { |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 378 | ret->exts |= SFTP_EXT_POSIX_RENAME; |
Darren Tucker | a64ab33 | 2008-06-13 07:01:29 +1000 | [diff] [blame] | 379 | known = 1; |
| 380 | } else if (strcmp(name, "statvfs@openssh.com") == 0 && |
| 381 | strcmp(value, "2") == 0) { |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 382 | ret->exts |= SFTP_EXT_STATVFS; |
Darren Tucker | a64ab33 | 2008-06-13 07:01:29 +1000 | [diff] [blame] | 383 | known = 1; |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 384 | } else if (strcmp(name, "fstatvfs@openssh.com") == 0 && |
Darren Tucker | a64ab33 | 2008-06-13 07:01:29 +1000 | [diff] [blame] | 385 | strcmp(value, "2") == 0) { |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 386 | ret->exts |= SFTP_EXT_FSTATVFS; |
Darren Tucker | a64ab33 | 2008-06-13 07:01:29 +1000 | [diff] [blame] | 387 | known = 1; |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 388 | } else if (strcmp(name, "hardlink@openssh.com") == 0 && |
| 389 | strcmp(value, "1") == 0) { |
| 390 | ret->exts |= SFTP_EXT_HARDLINK; |
| 391 | known = 1; |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 392 | } else if (strcmp(name, "fsync@openssh.com") == 0 && |
| 393 | strcmp(value, "1") == 0) { |
| 394 | ret->exts |= SFTP_EXT_FSYNC; |
| 395 | known = 1; |
Darren Tucker | a64ab33 | 2008-06-13 07:01:29 +1000 | [diff] [blame] | 396 | } |
| 397 | if (known) { |
| 398 | debug2("Server supports extension \"%s\" revision %s", |
| 399 | name, value); |
| 400 | } else { |
| 401 | debug2("Unrecognised server extension \"%s\"", name); |
| 402 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 403 | free(name); |
| 404 | free(value); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | buffer_free(&msg); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 408 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 409 | /* Some filexfer v.0 servers don't support large packets */ |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 410 | if (ret->version == 0) |
Ben Lindstrom | a1d8114 | 2002-04-02 20:58:11 +0000 | [diff] [blame] | 411 | ret->transfer_buflen = MIN(ret->transfer_buflen, 20480); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 412 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 413 | ret->limit_kbps = limit_kbps; |
| 414 | if (ret->limit_kbps > 0) { |
| 415 | bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps, |
| 416 | ret->transfer_buflen); |
| 417 | bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps, |
| 418 | ret->transfer_buflen); |
| 419 | } |
| 420 | |
| 421 | return ret; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | u_int |
| 425 | sftp_proto_version(struct sftp_conn *conn) |
| 426 | { |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 427 | return conn->version; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 431 | do_close(struct sftp_conn *conn, char *handle, u_int handle_len) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 432 | { |
| 433 | u_int id, status; |
| 434 | Buffer msg; |
| 435 | |
| 436 | buffer_init(&msg); |
| 437 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 438 | id = conn->msg_id++; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 439 | buffer_put_char(&msg, SSH2_FXP_CLOSE); |
| 440 | buffer_put_int(&msg, id); |
| 441 | buffer_put_string(&msg, handle, handle_len); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 442 | send_msg(conn, &msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 443 | debug3("Sent message SSH2_FXP_CLOSE I:%u", id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 444 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 445 | status = get_status(conn, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 446 | if (status != SSH2_FX_OK) |
| 447 | error("Couldn't close file: %s", fx2txt(status)); |
| 448 | |
| 449 | buffer_free(&msg); |
| 450 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 451 | return status; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 452 | } |
| 453 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 454 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 455 | static int |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 456 | do_lsreaddir(struct sftp_conn *conn, char *path, int print_flag, |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 457 | SFTP_DIRENT ***dir) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 458 | { |
| 459 | Buffer msg; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 460 | u_int count, type, id, handle_len, i, expected_id, ents = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 461 | char *handle; |
| 462 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 463 | id = conn->msg_id++; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 464 | |
| 465 | buffer_init(&msg); |
| 466 | buffer_put_char(&msg, SSH2_FXP_OPENDIR); |
| 467 | buffer_put_int(&msg, id); |
| 468 | buffer_put_cstring(&msg, path); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 469 | send_msg(conn, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 470 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 471 | handle = get_handle(conn, id, &handle_len, |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 472 | "remote readdir(\"%s\")", path); |
Damien Miller | 57c38ac | 2011-09-22 21:42:45 +1000 | [diff] [blame] | 473 | if (handle == NULL) { |
| 474 | buffer_free(&msg); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 475 | return -1; |
Damien Miller | 57c38ac | 2011-09-22 21:42:45 +1000 | [diff] [blame] | 476 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 477 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 478 | if (dir) { |
| 479 | ents = 0; |
| 480 | *dir = xmalloc(sizeof(**dir)); |
| 481 | (*dir)[0] = NULL; |
| 482 | } |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 483 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 484 | for (; !interrupted;) { |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 485 | id = expected_id = conn->msg_id++; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 486 | |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 487 | debug3("Sending SSH2_FXP_READDIR I:%u", id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 488 | |
| 489 | buffer_clear(&msg); |
| 490 | buffer_put_char(&msg, SSH2_FXP_READDIR); |
| 491 | buffer_put_int(&msg, id); |
| 492 | buffer_put_string(&msg, handle, handle_len); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 493 | send_msg(conn, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 494 | |
| 495 | buffer_clear(&msg); |
| 496 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 497 | get_msg(conn, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 498 | |
| 499 | type = buffer_get_char(&msg); |
| 500 | id = buffer_get_int(&msg); |
| 501 | |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 502 | debug3("Received reply T:%u I:%u", type, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 503 | |
| 504 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 505 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 506 | |
| 507 | if (type == SSH2_FXP_STATUS) { |
| 508 | int status = buffer_get_int(&msg); |
| 509 | |
| 510 | debug3("Received SSH2_FXP_STATUS %d", status); |
| 511 | |
| 512 | if (status == SSH2_FX_EOF) { |
| 513 | break; |
| 514 | } else { |
| 515 | error("Couldn't read directory: %s", |
| 516 | fx2txt(status)); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 517 | do_close(conn, handle, handle_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 518 | free(handle); |
Damien Miller | 57c38ac | 2011-09-22 21:42:45 +1000 | [diff] [blame] | 519 | buffer_free(&msg); |
Ben Lindstrom | 10ac33f | 2001-02-10 21:53:40 +0000 | [diff] [blame] | 520 | return(status); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 521 | } |
| 522 | } else if (type != SSH2_FXP_NAME) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 523 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 524 | SSH2_FXP_NAME, type); |
| 525 | |
| 526 | count = buffer_get_int(&msg); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 527 | if (count == 0) |
| 528 | break; |
| 529 | debug3("Received %d SSH2_FXP_NAME responses", count); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 530 | for (i = 0; i < count; i++) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 531 | char *filename, *longname; |
| 532 | Attrib *a; |
| 533 | |
| 534 | filename = buffer_get_string(&msg, NULL); |
| 535 | longname = buffer_get_string(&msg, NULL); |
| 536 | a = decode_attrib(&msg); |
| 537 | |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 538 | if (print_flag) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 539 | printf("%s\n", longname); |
| 540 | |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 541 | /* |
| 542 | * Directory entries should never contain '/' |
| 543 | * These can be used to attack recursive ops |
| 544 | * (e.g. send '../../../../etc/passwd') |
| 545 | */ |
| 546 | if (strchr(filename, '/') != NULL) { |
| 547 | error("Server sent suspect path \"%s\" " |
| 548 | "during readdir of \"%s\"", filename, path); |
| 549 | goto next; |
| 550 | } |
| 551 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 552 | if (dir) { |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 553 | *dir = xrealloc(*dir, ents + 2, sizeof(**dir)); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 554 | (*dir)[ents] = xmalloc(sizeof(***dir)); |
| 555 | (*dir)[ents]->filename = xstrdup(filename); |
| 556 | (*dir)[ents]->longname = xstrdup(longname); |
| 557 | memcpy(&(*dir)[ents]->a, a, sizeof(*a)); |
| 558 | (*dir)[++ents] = NULL; |
| 559 | } |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 560 | next: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 561 | free(filename); |
| 562 | free(longname); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | buffer_free(&msg); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 567 | do_close(conn, handle, handle_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 568 | free(handle); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 569 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 570 | /* Don't return partial matches on interrupt */ |
| 571 | if (interrupted && dir != NULL && *dir != NULL) { |
| 572 | free_sftp_dirents(*dir); |
| 573 | *dir = xmalloc(sizeof(**dir)); |
| 574 | **dir = NULL; |
| 575 | } |
| 576 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 577 | return 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 581 | do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 582 | { |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 583 | return(do_lsreaddir(conn, path, 0, dir)); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void free_sftp_dirents(SFTP_DIRENT **s) |
| 587 | { |
| 588 | int i; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 589 | |
| 590 | for (i = 0; s[i]; i++) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 591 | free(s[i]->filename); |
| 592 | free(s[i]->longname); |
| 593 | free(s[i]); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 594 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 595 | free(s); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 599 | do_rm(struct sftp_conn *conn, char *path) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 600 | { |
| 601 | u_int status, id; |
| 602 | |
| 603 | debug2("Sending SSH2_FXP_REMOVE \"%s\"", path); |
| 604 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 605 | id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 606 | send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path)); |
| 607 | status = get_status(conn, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 608 | if (status != SSH2_FX_OK) |
| 609 | error("Couldn't delete file: %s", fx2txt(status)); |
| 610 | return(status); |
| 611 | } |
| 612 | |
| 613 | int |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 614 | do_mkdir(struct sftp_conn *conn, char *path, Attrib *a, int print_flag) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 615 | { |
| 616 | u_int status, id; |
| 617 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 618 | id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 619 | send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 620 | strlen(path), a); |
| 621 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 622 | status = get_status(conn, id); |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 623 | if (status != SSH2_FX_OK && print_flag) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 624 | error("Couldn't create directory: %s", fx2txt(status)); |
| 625 | |
| 626 | return(status); |
| 627 | } |
| 628 | |
| 629 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 630 | do_rmdir(struct sftp_conn *conn, char *path) |
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++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 635 | send_string_request(conn, id, SSH2_FXP_RMDIR, path, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 636 | strlen(path)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 637 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 638 | status = get_status(conn, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 639 | if (status != SSH2_FX_OK) |
| 640 | error("Couldn't remove directory: %s", fx2txt(status)); |
| 641 | |
| 642 | return(status); |
| 643 | } |
| 644 | |
| 645 | Attrib * |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 646 | do_stat(struct sftp_conn *conn, char *path, int quiet) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 647 | { |
| 648 | u_int id; |
| 649 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 650 | id = conn->msg_id++; |
| 651 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 652 | send_string_request(conn, id, |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 653 | conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 654 | path, strlen(path)); |
| 655 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 656 | return(get_decode_stat(conn, id, quiet)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | Attrib * |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 660 | do_lstat(struct sftp_conn *conn, char *path, int quiet) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 661 | { |
| 662 | u_int id; |
| 663 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 664 | if (conn->version == 0) { |
| 665 | if (quiet) |
| 666 | debug("Server version does not support lstat operation"); |
| 667 | else |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 668 | logit("Server version does not support lstat operation"); |
Ben Lindstrom | f26ff5b | 2002-04-02 21:00:31 +0000 | [diff] [blame] | 669 | return(do_stat(conn, path, quiet)); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 673 | send_string_request(conn, id, SSH2_FXP_LSTAT, path, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 674 | strlen(path)); |
| 675 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 676 | return(get_decode_stat(conn, id, quiet)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 677 | } |
| 678 | |
Damien Miller | cfe23d3 | 2008-02-10 22:20:44 +1100 | [diff] [blame] | 679 | #ifdef notyet |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 680 | Attrib * |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 681 | 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] | 682 | { |
| 683 | u_int id; |
| 684 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 685 | id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 686 | send_string_request(conn, id, SSH2_FXP_FSTAT, handle, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 687 | handle_len); |
| 688 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 689 | return(get_decode_stat(conn, id, quiet)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 690 | } |
Damien Miller | cfe23d3 | 2008-02-10 22:20:44 +1100 | [diff] [blame] | 691 | #endif |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 692 | |
| 693 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 694 | do_setstat(struct sftp_conn *conn, char *path, Attrib *a) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 695 | { |
| 696 | u_int status, id; |
| 697 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 698 | id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 699 | send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 700 | strlen(path), a); |
| 701 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 702 | status = get_status(conn, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 703 | if (status != SSH2_FX_OK) |
| 704 | error("Couldn't setstat on \"%s\": %s", path, |
| 705 | fx2txt(status)); |
| 706 | |
| 707 | return(status); |
| 708 | } |
| 709 | |
| 710 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 711 | do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 712 | Attrib *a) |
| 713 | { |
| 714 | u_int status, id; |
| 715 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 716 | id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 717 | send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 718 | handle_len, a); |
| 719 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 720 | status = get_status(conn, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 721 | if (status != SSH2_FX_OK) |
| 722 | error("Couldn't fsetstat: %s", fx2txt(status)); |
| 723 | |
| 724 | return(status); |
| 725 | } |
| 726 | |
| 727 | char * |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 728 | do_realpath(struct sftp_conn *conn, char *path) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 729 | { |
| 730 | Buffer msg; |
| 731 | u_int type, expected_id, count, id; |
| 732 | char *filename, *longname; |
| 733 | Attrib *a; |
| 734 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 735 | expected_id = id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 736 | send_string_request(conn, id, SSH2_FXP_REALPATH, path, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 737 | strlen(path)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 738 | |
| 739 | buffer_init(&msg); |
| 740 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 741 | get_msg(conn, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 742 | type = buffer_get_char(&msg); |
| 743 | id = buffer_get_int(&msg); |
| 744 | |
| 745 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 746 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 747 | |
| 748 | if (type == SSH2_FXP_STATUS) { |
| 749 | u_int status = buffer_get_int(&msg); |
| 750 | |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 751 | error("Couldn't canonicalize: %s", fx2txt(status)); |
Damien Miller | 4956631 | 2010-06-26 09:38:23 +1000 | [diff] [blame] | 752 | buffer_free(&msg); |
| 753 | return NULL; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 754 | } else if (type != SSH2_FXP_NAME) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 755 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 756 | SSH2_FXP_NAME, type); |
| 757 | |
| 758 | count = buffer_get_int(&msg); |
| 759 | if (count != 1) |
| 760 | fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count); |
| 761 | |
| 762 | filename = buffer_get_string(&msg, NULL); |
| 763 | longname = buffer_get_string(&msg, NULL); |
| 764 | a = decode_attrib(&msg); |
| 765 | |
Darren Tucker | 4908d44 | 2012-07-02 22:15:38 +1000 | [diff] [blame] | 766 | debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename, |
| 767 | (unsigned long)a->size); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 768 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 769 | free(longname); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 770 | |
| 771 | buffer_free(&msg); |
| 772 | |
| 773 | return(filename); |
| 774 | } |
| 775 | |
| 776 | int |
Damien Miller | c7dba12 | 2013-08-21 02:41:15 +1000 | [diff] [blame] | 777 | do_rename(struct sftp_conn *conn, char *oldpath, char *newpath, |
| 778 | int force_legacy) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 779 | { |
| 780 | Buffer msg; |
| 781 | u_int status, id; |
Damien Miller | c7dba12 | 2013-08-21 02:41:15 +1000 | [diff] [blame] | 782 | int use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 783 | |
| 784 | buffer_init(&msg); |
| 785 | |
| 786 | /* Send rename request */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 787 | id = conn->msg_id++; |
Damien Miller | c7dba12 | 2013-08-21 02:41:15 +1000 | [diff] [blame] | 788 | if (use_ext) { |
Damien Miller | 7a3e1d0 | 2008-03-27 10:59:57 +1100 | [diff] [blame] | 789 | buffer_put_char(&msg, SSH2_FXP_EXTENDED); |
| 790 | buffer_put_int(&msg, id); |
| 791 | buffer_put_cstring(&msg, "posix-rename@openssh.com"); |
| 792 | } else { |
| 793 | buffer_put_char(&msg, SSH2_FXP_RENAME); |
| 794 | buffer_put_int(&msg, id); |
| 795 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 796 | buffer_put_cstring(&msg, oldpath); |
| 797 | buffer_put_cstring(&msg, newpath); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 798 | send_msg(conn, &msg); |
Damien Miller | 7a3e1d0 | 2008-03-27 10:59:57 +1100 | [diff] [blame] | 799 | debug3("Sent message %s \"%s\" -> \"%s\"", |
Damien Miller | c7dba12 | 2013-08-21 02:41:15 +1000 | [diff] [blame] | 800 | use_ext ? "posix-rename@openssh.com" : "SSH2_FXP_RENAME", |
| 801 | oldpath, newpath); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 802 | buffer_free(&msg); |
| 803 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 804 | status = get_status(conn, id); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 805 | if (status != SSH2_FX_OK) |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 806 | error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, |
| 807 | newpath, fx2txt(status)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 808 | |
| 809 | return(status); |
| 810 | } |
| 811 | |
| 812 | int |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 813 | do_hardlink(struct sftp_conn *conn, char *oldpath, char *newpath) |
| 814 | { |
| 815 | Buffer msg; |
| 816 | u_int status, id; |
| 817 | |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 818 | if ((conn->exts & SFTP_EXT_HARDLINK) == 0) { |
| 819 | error("Server does not support hardlink@openssh.com extension"); |
| 820 | return -1; |
| 821 | } |
| 822 | |
Damien Miller | 3decdba | 2011-09-22 21:41:05 +1000 | [diff] [blame] | 823 | buffer_init(&msg); |
| 824 | |
| 825 | /* Send link request */ |
| 826 | id = conn->msg_id++; |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 827 | buffer_put_char(&msg, SSH2_FXP_EXTENDED); |
| 828 | buffer_put_int(&msg, id); |
| 829 | buffer_put_cstring(&msg, "hardlink@openssh.com"); |
| 830 | buffer_put_cstring(&msg, oldpath); |
| 831 | buffer_put_cstring(&msg, newpath); |
| 832 | send_msg(conn, &msg); |
| 833 | debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"", |
| 834 | oldpath, newpath); |
| 835 | buffer_free(&msg); |
| 836 | |
| 837 | status = get_status(conn, id); |
| 838 | if (status != SSH2_FX_OK) |
| 839 | error("Couldn't link file \"%s\" to \"%s\": %s", oldpath, |
| 840 | newpath, fx2txt(status)); |
| 841 | |
| 842 | return(status); |
| 843 | } |
| 844 | |
| 845 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 846 | do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath) |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 847 | { |
| 848 | Buffer msg; |
| 849 | u_int status, id; |
| 850 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 851 | if (conn->version < 3) { |
| 852 | error("This server does not support the symlink operation"); |
| 853 | return(SSH2_FX_OP_UNSUPPORTED); |
| 854 | } |
| 855 | |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 856 | buffer_init(&msg); |
| 857 | |
Darren Tucker | dca6a4d | 2004-04-19 22:10:52 +1000 | [diff] [blame] | 858 | /* Send symlink request */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 859 | id = conn->msg_id++; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 860 | buffer_put_char(&msg, SSH2_FXP_SYMLINK); |
| 861 | buffer_put_int(&msg, id); |
| 862 | buffer_put_cstring(&msg, oldpath); |
| 863 | buffer_put_cstring(&msg, newpath); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 864 | send_msg(conn, &msg); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 865 | debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath, |
| 866 | newpath); |
| 867 | buffer_free(&msg); |
| 868 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 869 | status = get_status(conn, id); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 870 | if (status != SSH2_FX_OK) |
Ben Lindstrom | 8e879cf | 2002-11-09 15:48:49 +0000 | [diff] [blame] | 871 | error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 872 | newpath, fx2txt(status)); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 873 | |
| 874 | return(status); |
| 875 | } |
| 876 | |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 877 | int |
| 878 | do_fsync(struct sftp_conn *conn, char *handle, u_int handle_len) |
| 879 | { |
| 880 | Buffer msg; |
| 881 | u_int status, id; |
| 882 | |
| 883 | /* Silently return if the extension is not supported */ |
| 884 | if ((conn->exts & SFTP_EXT_FSYNC) == 0) |
| 885 | return -1; |
| 886 | |
| 887 | buffer_init(&msg); |
| 888 | |
| 889 | /* Send fsync request */ |
| 890 | id = conn->msg_id++; |
| 891 | |
| 892 | buffer_put_char(&msg, SSH2_FXP_EXTENDED); |
| 893 | buffer_put_int(&msg, id); |
| 894 | buffer_put_cstring(&msg, "fsync@openssh.com"); |
| 895 | buffer_put_string(&msg, handle, handle_len); |
| 896 | send_msg(conn, &msg); |
| 897 | debug3("Sent message fsync@openssh.com I:%u", id); |
| 898 | buffer_free(&msg); |
| 899 | |
| 900 | status = get_status(conn, id); |
| 901 | if (status != SSH2_FX_OK) |
| 902 | error("Couldn't sync file: %s", fx2txt(status)); |
| 903 | |
| 904 | return status; |
| 905 | } |
| 906 | |
Damien Miller | cfe23d3 | 2008-02-10 22:20:44 +1100 | [diff] [blame] | 907 | #ifdef notyet |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 908 | char * |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 909 | do_readlink(struct sftp_conn *conn, char *path) |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 910 | { |
| 911 | Buffer msg; |
| 912 | u_int type, expected_id, count, id; |
| 913 | char *filename, *longname; |
| 914 | Attrib *a; |
| 915 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 916 | expected_id = id = conn->msg_id++; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 917 | send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path)); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 918 | |
| 919 | buffer_init(&msg); |
| 920 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 921 | get_msg(conn, &msg); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 922 | type = buffer_get_char(&msg); |
| 923 | id = buffer_get_int(&msg); |
| 924 | |
| 925 | if (id != expected_id) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 926 | fatal("ID mismatch (%u != %u)", id, expected_id); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 927 | |
| 928 | if (type == SSH2_FXP_STATUS) { |
| 929 | u_int status = buffer_get_int(&msg); |
| 930 | |
| 931 | error("Couldn't readlink: %s", fx2txt(status)); |
Damien Miller | 3decdba | 2011-09-22 21:41:05 +1000 | [diff] [blame] | 932 | buffer_free(&msg); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 933 | return(NULL); |
| 934 | } else if (type != SSH2_FXP_NAME) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 935 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 936 | SSH2_FXP_NAME, type); |
| 937 | |
| 938 | count = buffer_get_int(&msg); |
| 939 | if (count != 1) |
| 940 | fatal("Got multiple names (%d) from SSH_FXP_READLINK", count); |
| 941 | |
| 942 | filename = buffer_get_string(&msg, NULL); |
| 943 | longname = buffer_get_string(&msg, NULL); |
| 944 | a = decode_attrib(&msg); |
| 945 | |
| 946 | debug3("SSH_FXP_READLINK %s -> %s", path, filename); |
| 947 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 948 | free(longname); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 949 | |
| 950 | buffer_free(&msg); |
| 951 | |
| 952 | return(filename); |
| 953 | } |
Damien Miller | cfe23d3 | 2008-02-10 22:20:44 +1100 | [diff] [blame] | 954 | #endif |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 955 | |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 956 | int |
Darren Tucker | 7b59889 | 2008-06-09 22:49:36 +1000 | [diff] [blame] | 957 | 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] | 958 | int quiet) |
| 959 | { |
| 960 | Buffer msg; |
| 961 | u_int id; |
| 962 | |
| 963 | if ((conn->exts & SFTP_EXT_STATVFS) == 0) { |
| 964 | error("Server does not support statvfs@openssh.com extension"); |
| 965 | return -1; |
| 966 | } |
| 967 | |
| 968 | id = conn->msg_id++; |
| 969 | |
| 970 | buffer_init(&msg); |
| 971 | buffer_clear(&msg); |
| 972 | buffer_put_char(&msg, SSH2_FXP_EXTENDED); |
| 973 | buffer_put_int(&msg, id); |
| 974 | buffer_put_cstring(&msg, "statvfs@openssh.com"); |
| 975 | buffer_put_cstring(&msg, path); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 976 | send_msg(conn, &msg); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 977 | buffer_free(&msg); |
| 978 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 979 | return get_decode_statvfs(conn, st, id, quiet); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | #ifdef notyet |
| 983 | int |
| 984 | 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] | 985 | struct sftp_statvfs *st, int quiet) |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 986 | { |
| 987 | Buffer msg; |
| 988 | u_int id; |
| 989 | |
| 990 | if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) { |
| 991 | error("Server does not support fstatvfs@openssh.com extension"); |
| 992 | return -1; |
| 993 | } |
| 994 | |
| 995 | id = conn->msg_id++; |
| 996 | |
| 997 | buffer_init(&msg); |
| 998 | buffer_clear(&msg); |
| 999 | buffer_put_char(&msg, SSH2_FXP_EXTENDED); |
| 1000 | buffer_put_int(&msg, id); |
| 1001 | buffer_put_cstring(&msg, "fstatvfs@openssh.com"); |
| 1002 | buffer_put_string(&msg, handle, handle_len); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1003 | send_msg(conn, &msg); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1004 | buffer_free(&msg); |
| 1005 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1006 | return get_decode_statvfs(conn, st, id, quiet); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1007 | } |
| 1008 | #endif |
| 1009 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1010 | static void |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1011 | send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset, |
| 1012 | u_int len, char *handle, u_int handle_len) |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1013 | { |
| 1014 | Buffer msg; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1015 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1016 | buffer_init(&msg); |
| 1017 | buffer_clear(&msg); |
| 1018 | buffer_put_char(&msg, SSH2_FXP_READ); |
| 1019 | buffer_put_int(&msg, id); |
| 1020 | buffer_put_string(&msg, handle, handle_len); |
| 1021 | buffer_put_int64(&msg, offset); |
| 1022 | buffer_put_int(&msg, len); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1023 | send_msg(conn, &msg); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1024 | buffer_free(&msg); |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1025 | } |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1026 | |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1027 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1028 | do_download(struct sftp_conn *conn, char *remote_path, char *local_path, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1029 | Attrib *a, int preserve_flag, int resume_flag, int fsync_flag) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1030 | { |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1031 | Attrib junk; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1032 | Buffer msg; |
| 1033 | char *handle; |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1034 | int local_fd = -1, status = 0, write_error; |
| 1035 | int read_error, write_errno, reordered = 0; |
| 1036 | u_int64_t offset = 0, size, highwater; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1037 | u_int handle_len, mode, type, id, buflen, num_req, max_req; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1038 | off_t progress_counter; |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1039 | struct stat st; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1040 | struct request { |
| 1041 | u_int id; |
| 1042 | u_int len; |
| 1043 | u_int64_t offset; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1044 | TAILQ_ENTRY(request) tq; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1045 | }; |
| 1046 | TAILQ_HEAD(reqhead, request) requests; |
| 1047 | struct request *req; |
| 1048 | |
| 1049 | TAILQ_INIT(&requests); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1050 | |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1051 | if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL) |
| 1052 | return -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1053 | |
Damien Miller | 9e72028 | 2008-06-29 22:46:35 +1000 | [diff] [blame] | 1054 | /* 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] | 1055 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) |
Damien Miller | 770b374 | 2003-01-08 14:04:53 +1100 | [diff] [blame] | 1056 | mode = a->perm & 0777; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1057 | else |
| 1058 | mode = 0666; |
| 1059 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 1060 | if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && |
Damien Miller | 5fa01fd | 2003-01-14 22:24:47 +1100 | [diff] [blame] | 1061 | (!S_ISREG(a->perm))) { |
| 1062 | error("Cannot download non-regular file: %s", remote_path); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 1063 | return(-1); |
| 1064 | } |
| 1065 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1066 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) |
| 1067 | size = a->size; |
| 1068 | else |
| 1069 | size = 0; |
| 1070 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1071 | buflen = conn->transfer_buflen; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1072 | buffer_init(&msg); |
| 1073 | |
| 1074 | /* Send open request */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1075 | id = conn->msg_id++; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1076 | buffer_put_char(&msg, SSH2_FXP_OPEN); |
| 1077 | buffer_put_int(&msg, id); |
| 1078 | buffer_put_cstring(&msg, remote_path); |
| 1079 | buffer_put_int(&msg, SSH2_FXF_READ); |
| 1080 | attrib_clear(&junk); /* Send empty attributes */ |
| 1081 | encode_attrib(&msg, &junk); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1082 | send_msg(conn, &msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1083 | 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] | 1084 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1085 | handle = get_handle(conn, id, &handle_len, |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 1086 | "remote open(\"%s\")", remote_path); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1087 | if (handle == NULL) { |
| 1088 | buffer_free(&msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1089 | return(-1); |
| 1090 | } |
| 1091 | |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1092 | local_fd = open(local_path, |
| 1093 | O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1094 | if (local_fd == -1) { |
| 1095 | error("Couldn't open local file \"%s\" for writing: %s", |
| 1096 | local_path, strerror(errno)); |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1097 | goto fail; |
| 1098 | } |
| 1099 | offset = highwater = 0; |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1100 | if (resume_flag) { |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1101 | if (fstat(local_fd, &st) == -1) { |
| 1102 | error("Unable to stat local file \"%s\": %s", |
| 1103 | local_path, strerror(errno)); |
| 1104 | goto fail; |
| 1105 | } |
| 1106 | if ((size_t)st.st_size > size) { |
| 1107 | error("Unable to resume download of \"%s\": " |
| 1108 | "local file is larger than remote", local_path); |
| 1109 | fail: |
| 1110 | do_close(conn, handle, handle_len); |
| 1111 | buffer_free(&msg); |
| 1112 | free(handle); |
| 1113 | return -1; |
| 1114 | } |
| 1115 | offset = highwater = st.st_size; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1116 | } |
| 1117 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1118 | /* Read from remote and write to local */ |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1119 | write_error = read_error = write_errno = num_req = 0; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1120 | max_req = 1; |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1121 | progress_counter = offset; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1122 | |
Damien Miller | 9ba3069 | 2004-03-08 23:12:02 +1100 | [diff] [blame] | 1123 | if (showprogress && size != 0) |
| 1124 | start_progress_meter(remote_path, size, &progress_counter); |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1125 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1126 | while (num_req > 0 || max_req > 0) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1127 | char *data; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1128 | u_int len; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1129 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1130 | /* |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1131 | * Simulate EOF on interrupt: stop sending new requests and |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1132 | * allow outstanding requests to drain gracefully |
| 1133 | */ |
| 1134 | if (interrupted) { |
| 1135 | if (num_req == 0) /* If we haven't started yet... */ |
| 1136 | break; |
| 1137 | max_req = 0; |
| 1138 | } |
| 1139 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1140 | /* Send some more requests */ |
| 1141 | while (num_req < max_req) { |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1142 | debug3("Request range %llu -> %llu (%d/%d)", |
Ben Lindstrom | d45f28c | 2002-03-22 01:00:57 +0000 | [diff] [blame] | 1143 | (unsigned long long)offset, |
| 1144 | (unsigned long long)offset + buflen - 1, |
| 1145 | num_req, max_req); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1146 | req = xmalloc(sizeof(*req)); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1147 | req->id = conn->msg_id++; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1148 | req->len = buflen; |
| 1149 | req->offset = offset; |
| 1150 | offset += buflen; |
| 1151 | num_req++; |
| 1152 | TAILQ_INSERT_TAIL(&requests, req, tq); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1153 | send_read_request(conn, req->id, req->offset, |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1154 | req->len, handle, handle_len); |
| 1155 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1156 | |
| 1157 | buffer_clear(&msg); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1158 | get_msg(conn, &msg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1159 | type = buffer_get_char(&msg); |
| 1160 | id = buffer_get_int(&msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1161 | 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] | 1162 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1163 | /* Find the request in our queue */ |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1164 | for (req = TAILQ_FIRST(&requests); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1165 | req != NULL && req->id != id; |
| 1166 | req = TAILQ_NEXT(req, tq)) |
| 1167 | ; |
| 1168 | if (req == NULL) |
| 1169 | fatal("Unexpected reply %u", id); |
| 1170 | |
| 1171 | switch (type) { |
| 1172 | case SSH2_FXP_STATUS: |
| 1173 | status = buffer_get_int(&msg); |
| 1174 | if (status != SSH2_FX_EOF) |
| 1175 | read_error = 1; |
| 1176 | max_req = 0; |
| 1177 | TAILQ_REMOVE(&requests, req, tq); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1178 | free(req); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1179 | num_req--; |
| 1180 | break; |
| 1181 | case SSH2_FXP_DATA: |
| 1182 | data = buffer_get_string(&msg, &len); |
Ben Lindstrom | eb50545 | 2002-03-22 01:03:15 +0000 | [diff] [blame] | 1183 | debug3("Received data %llu -> %llu", |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1184 | (unsigned long long)req->offset, |
Ben Lindstrom | eb50545 | 2002-03-22 01:03:15 +0000 | [diff] [blame] | 1185 | (unsigned long long)req->offset + len - 1); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1186 | if (len > req->len) |
| 1187 | fatal("Received more data than asked for " |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 1188 | "%u > %u", len, req->len); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1189 | if ((lseek(local_fd, req->offset, SEEK_SET) == -1 || |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 1190 | atomicio(vwrite, local_fd, data, len) != len) && |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1191 | !write_error) { |
| 1192 | write_errno = errno; |
| 1193 | write_error = 1; |
| 1194 | max_req = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1195 | } |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1196 | else if (!reordered && req->offset <= highwater) |
| 1197 | highwater = req->offset + len; |
| 1198 | else if (!reordered && req->offset > highwater) |
| 1199 | reordered = 1; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1200 | progress_counter += len; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1201 | free(data); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1202 | |
| 1203 | if (len == req->len) { |
| 1204 | TAILQ_REMOVE(&requests, req, tq); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1205 | free(req); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1206 | num_req--; |
| 1207 | } else { |
| 1208 | /* Resend the request for the missing data */ |
| 1209 | debug3("Short data block, re-requesting " |
Ben Lindstrom | eb50545 | 2002-03-22 01:03:15 +0000 | [diff] [blame] | 1210 | "%llu -> %llu (%2d)", |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1211 | (unsigned long long)req->offset + len, |
Ben Lindstrom | 83b79e4 | 2002-03-22 01:05:27 +0000 | [diff] [blame] | 1212 | (unsigned long long)req->offset + |
| 1213 | req->len - 1, num_req); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1214 | req->id = conn->msg_id++; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1215 | req->len -= len; |
| 1216 | req->offset += len; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1217 | send_read_request(conn, req->id, |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1218 | req->offset, req->len, handle, handle_len); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1219 | /* Reduce the request size */ |
| 1220 | if (len < buflen) |
| 1221 | buflen = MAX(MIN_READ_SIZE, len); |
| 1222 | } |
| 1223 | if (max_req > 0) { /* max_req = 0 iff EOF received */ |
| 1224 | if (size > 0 && offset > size) { |
| 1225 | /* Only one request at a time |
| 1226 | * after the expected EOF */ |
| 1227 | debug3("Finish at %llu (%2d)", |
Ben Lindstrom | eb50545 | 2002-03-22 01:03:15 +0000 | [diff] [blame] | 1228 | (unsigned long long)offset, |
| 1229 | num_req); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1230 | max_req = 1; |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1231 | } else if (max_req <= conn->num_requests) { |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1232 | ++max_req; |
| 1233 | } |
| 1234 | } |
| 1235 | break; |
| 1236 | default: |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1237 | fatal("Expected SSH2_FXP_DATA(%u) packet, got %u", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1238 | SSH2_FXP_DATA, type); |
| 1239 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1240 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1241 | |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1242 | if (showprogress && size) |
| 1243 | stop_progress_meter(); |
| 1244 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1245 | /* Sanity check */ |
| 1246 | if (TAILQ_FIRST(&requests) != NULL) |
| 1247 | fatal("Transfer complete, but requests still in queue"); |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1248 | /* Truncate at highest contiguous point to avoid holes on interrupt */ |
| 1249 | if (read_error || write_error || interrupted) { |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1250 | if (reordered && resume_flag) { |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1251 | error("Unable to resume download of \"%s\": " |
| 1252 | "server reordered requests", local_path); |
| 1253 | } |
| 1254 | debug("truncating at %llu", (unsigned long long)highwater); |
| 1255 | ftruncate(local_fd, highwater); |
| 1256 | } |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1257 | if (read_error) { |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1258 | error("Couldn't read from remote file \"%s\" : %s", |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1259 | remote_path, fx2txt(status)); |
Damien Miller | fec029f | 2013-08-21 02:42:12 +1000 | [diff] [blame] | 1260 | status = -1; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1261 | do_close(conn, handle, handle_len); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1262 | } else if (write_error) { |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1263 | error("Couldn't write to \"%s\": %s", local_path, |
| 1264 | strerror(write_errno)); |
| 1265 | status = -1; |
| 1266 | do_close(conn, handle, handle_len); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1267 | } else { |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1268 | status = do_close(conn, handle, handle_len); |
Damien Miller | fec029f | 2013-08-21 02:42:12 +1000 | [diff] [blame] | 1269 | if (interrupted || status != SSH2_FX_OK) |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1270 | status = -1; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1271 | /* Override umask and utimes if asked */ |
Ben Lindstrom | 6dc75f5 | 2001-02-17 16:47:47 +0000 | [diff] [blame] | 1272 | #ifdef HAVE_FCHMOD |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1273 | if (preserve_flag && fchmod(local_fd, mode) == -1) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1274 | #else |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1275 | if (preserve_flag && chmod(local_path, mode) == -1) |
Ben Lindstrom | 6dc75f5 | 2001-02-17 16:47:47 +0000 | [diff] [blame] | 1276 | #endif /* HAVE_FCHMOD */ |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1277 | error("Couldn't set mode on \"%s\": %s", local_path, |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 1278 | strerror(errno)); |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1279 | if (preserve_flag && |
| 1280 | (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) { |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1281 | struct timeval tv[2]; |
| 1282 | tv[0].tv_sec = a->atime; |
| 1283 | tv[1].tv_sec = a->mtime; |
| 1284 | tv[0].tv_usec = tv[1].tv_usec = 0; |
| 1285 | if (utimes(local_path, tv) == -1) |
| 1286 | error("Can't set times on \"%s\": %s", |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 1287 | local_path, strerror(errno)); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1288 | } |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1289 | if (fsync_flag) { |
| 1290 | debug("syncing \"%s\"", local_path); |
| 1291 | if (fsync(local_fd) == -1) |
| 1292 | error("Couldn't sync file \"%s\": %s", |
| 1293 | local_path, strerror(errno)); |
| 1294 | } |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 1295 | } |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1296 | close(local_fd); |
| 1297 | buffer_free(&msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1298 | free(handle); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1299 | |
| 1300 | return(status); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1301 | } |
| 1302 | |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1303 | static int |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1304 | download_dir_internal(struct sftp_conn *conn, char *src, char *dst, int depth, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1305 | Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag, |
| 1306 | int fsync_flag) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1307 | { |
| 1308 | int i, ret = 0; |
| 1309 | SFTP_DIRENT **dir_entries; |
| 1310 | char *filename, *new_src, *new_dst; |
| 1311 | mode_t mode = 0777; |
| 1312 | |
| 1313 | if (depth >= MAX_DIR_DEPTH) { |
| 1314 | error("Maximum directory depth exceeded: %d levels", depth); |
| 1315 | return -1; |
| 1316 | } |
| 1317 | |
| 1318 | if (dirattrib == NULL && |
| 1319 | (dirattrib = do_stat(conn, src, 1)) == NULL) { |
| 1320 | error("Unable to stat remote directory \"%s\"", src); |
| 1321 | return -1; |
| 1322 | } |
| 1323 | if (!S_ISDIR(dirattrib->perm)) { |
| 1324 | error("\"%s\" is not a directory", src); |
| 1325 | return -1; |
| 1326 | } |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1327 | if (print_flag) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1328 | printf("Retrieving %s\n", src); |
| 1329 | |
| 1330 | if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) |
| 1331 | mode = dirattrib->perm & 01777; |
| 1332 | else { |
| 1333 | debug("Server did not send permissions for " |
| 1334 | "directory \"%s\"", dst); |
| 1335 | } |
| 1336 | |
| 1337 | if (mkdir(dst, mode) == -1 && errno != EEXIST) { |
| 1338 | error("mkdir %s: %s", dst, strerror(errno)); |
| 1339 | return -1; |
| 1340 | } |
| 1341 | |
| 1342 | if (do_readdir(conn, src, &dir_entries) == -1) { |
| 1343 | error("%s: Failed to get directory contents", src); |
| 1344 | return -1; |
| 1345 | } |
| 1346 | |
| 1347 | for (i = 0; dir_entries[i] != NULL && !interrupted; i++) { |
| 1348 | filename = dir_entries[i]->filename; |
| 1349 | |
| 1350 | new_dst = path_append(dst, filename); |
| 1351 | new_src = path_append(src, filename); |
| 1352 | |
| 1353 | if (S_ISDIR(dir_entries[i]->a.perm)) { |
| 1354 | if (strcmp(filename, ".") == 0 || |
| 1355 | strcmp(filename, "..") == 0) |
| 1356 | continue; |
| 1357 | if (download_dir_internal(conn, new_src, new_dst, |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1358 | depth + 1, &(dir_entries[i]->a), preserve_flag, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1359 | print_flag, resume_flag, fsync_flag) == -1) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1360 | ret = -1; |
| 1361 | } else if (S_ISREG(dir_entries[i]->a.perm) ) { |
| 1362 | if (do_download(conn, new_src, new_dst, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1363 | &(dir_entries[i]->a), preserve_flag, |
| 1364 | resume_flag, fsync_flag) == -1) { |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1365 | error("Download of file %s to %s failed", |
| 1366 | new_src, new_dst); |
| 1367 | ret = -1; |
| 1368 | } |
| 1369 | } else |
| 1370 | logit("%s: not a regular file\n", new_src); |
| 1371 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1372 | free(new_dst); |
| 1373 | free(new_src); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1374 | } |
| 1375 | |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1376 | if (preserve_flag) { |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1377 | if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
| 1378 | struct timeval tv[2]; |
| 1379 | tv[0].tv_sec = dirattrib->atime; |
| 1380 | tv[1].tv_sec = dirattrib->mtime; |
| 1381 | tv[0].tv_usec = tv[1].tv_usec = 0; |
| 1382 | if (utimes(dst, tv) == -1) |
| 1383 | error("Can't set times on \"%s\": %s", |
| 1384 | dst, strerror(errno)); |
| 1385 | } else |
| 1386 | debug("Server did not send times for directory " |
| 1387 | "\"%s\"", dst); |
| 1388 | } |
| 1389 | |
| 1390 | free_sftp_dirents(dir_entries); |
| 1391 | |
| 1392 | return ret; |
| 1393 | } |
| 1394 | |
| 1395 | int |
| 1396 | download_dir(struct sftp_conn *conn, char *src, char *dst, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1397 | Attrib *dirattrib, int preserve_flag, int print_flag, |
| 1398 | int resume_flag, int fsync_flag) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1399 | { |
| 1400 | char *src_canon; |
| 1401 | int ret; |
| 1402 | |
| 1403 | if ((src_canon = do_realpath(conn, src)) == NULL) { |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1404 | error("Unable to canonicalize path \"%s\"", src); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1405 | return -1; |
| 1406 | } |
| 1407 | |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1408 | ret = download_dir_internal(conn, src_canon, dst, 0, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1409 | dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1410 | free(src_canon); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1411 | return ret; |
| 1412 | } |
| 1413 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1414 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1415 | do_upload(struct sftp_conn *conn, char *local_path, char *remote_path, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1416 | int preserve_flag, int fsync_flag) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1417 | { |
Damien Miller | acdf25b | 2008-02-10 22:27:24 +1100 | [diff] [blame] | 1418 | int local_fd; |
| 1419 | int status = SSH2_FX_OK; |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1420 | u_int handle_len, id, type; |
Darren Tucker | c9a1991 | 2013-06-02 08:37:05 +1000 | [diff] [blame] | 1421 | off_t offset, progress_counter; |
Damien Miller | 8829d36 | 2002-02-08 22:04:05 +1100 | [diff] [blame] | 1422 | char *handle, *data; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1423 | Buffer msg; |
| 1424 | struct stat sb; |
| 1425 | Attrib a; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1426 | u_int32_t startid; |
| 1427 | u_int32_t ackid; |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1428 | struct outstanding_ack { |
| 1429 | u_int id; |
| 1430 | u_int len; |
Damien Miller | 8b3fdfb | 2007-09-17 16:12:03 +1000 | [diff] [blame] | 1431 | off_t offset; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1432 | TAILQ_ENTRY(outstanding_ack) tq; |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1433 | }; |
| 1434 | TAILQ_HEAD(ackhead, outstanding_ack) acks; |
Damien Miller | 7cf17eb | 2004-06-15 10:28:56 +1000 | [diff] [blame] | 1435 | struct outstanding_ack *ack = NULL; |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1436 | |
| 1437 | TAILQ_INIT(&acks); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1438 | |
| 1439 | if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) { |
| 1440 | error("Couldn't open local file \"%s\" for reading: %s", |
| 1441 | local_path, strerror(errno)); |
| 1442 | return(-1); |
| 1443 | } |
| 1444 | if (fstat(local_fd, &sb) == -1) { |
| 1445 | error("Couldn't fstat local file \"%s\": %s", |
| 1446 | local_path, strerror(errno)); |
| 1447 | close(local_fd); |
| 1448 | return(-1); |
| 1449 | } |
Damien Miller | 5fa01fd | 2003-01-14 22:24:47 +1100 | [diff] [blame] | 1450 | if (!S_ISREG(sb.st_mode)) { |
| 1451 | error("%s is not a regular file", local_path); |
| 1452 | close(local_fd); |
| 1453 | return(-1); |
| 1454 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1455 | stat_to_attrib(&sb, &a); |
| 1456 | |
| 1457 | a.flags &= ~SSH2_FILEXFER_ATTR_SIZE; |
| 1458 | a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID; |
| 1459 | a.perm &= 0777; |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1460 | if (!preserve_flag) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1461 | a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME; |
| 1462 | |
| 1463 | buffer_init(&msg); |
| 1464 | |
| 1465 | /* Send open request */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1466 | id = conn->msg_id++; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1467 | buffer_put_char(&msg, SSH2_FXP_OPEN); |
| 1468 | buffer_put_int(&msg, id); |
| 1469 | buffer_put_cstring(&msg, remote_path); |
| 1470 | buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC); |
| 1471 | encode_attrib(&msg, &a); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1472 | send_msg(conn, &msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1473 | 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] | 1474 | |
| 1475 | buffer_clear(&msg); |
| 1476 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1477 | handle = get_handle(conn, id, &handle_len, |
Darren Tucker | c22f090 | 2009-10-07 08:24:19 +1100 | [diff] [blame] | 1478 | "remote open(\"%s\")", remote_path); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1479 | if (handle == NULL) { |
| 1480 | close(local_fd); |
| 1481 | buffer_free(&msg); |
Damien Miller | acdf25b | 2008-02-10 22:27:24 +1100 | [diff] [blame] | 1482 | return -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1483 | } |
| 1484 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1485 | startid = ackid = id + 1; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1486 | data = xmalloc(conn->transfer_buflen); |
Damien Miller | 8829d36 | 2002-02-08 22:04:05 +1100 | [diff] [blame] | 1487 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1488 | /* Read from local and write to remote */ |
Darren Tucker | c9a1991 | 2013-06-02 08:37:05 +1000 | [diff] [blame] | 1489 | offset = progress_counter = 0; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1490 | if (showprogress) |
Darren Tucker | c9a1991 | 2013-06-02 08:37:05 +1000 | [diff] [blame] | 1491 | start_progress_meter(local_path, sb.st_size, |
| 1492 | &progress_counter); |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1493 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1494 | for (;;) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1495 | int len; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1496 | |
| 1497 | /* |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1498 | * Can't use atomicio here because it returns 0 on EOF, |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1499 | * thus losing the last block of the file. |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1500 | * Simulate an EOF on interrupt, allowing ACKs from the |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1501 | * server to drain. |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1502 | */ |
Damien Miller | acdf25b | 2008-02-10 22:27:24 +1100 | [diff] [blame] | 1503 | if (interrupted || status != SSH2_FX_OK) |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1504 | len = 0; |
| 1505 | else do |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1506 | len = read(local_fd, data, conn->transfer_buflen); |
Damien Miller | d8968ad | 2008-07-04 23:10:49 +1000 | [diff] [blame] | 1507 | while ((len == -1) && |
| 1508 | (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1509 | |
| 1510 | if (len == -1) |
| 1511 | fatal("Couldn't read from \"%s\": %s", local_path, |
| 1512 | strerror(errno)); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1513 | |
| 1514 | if (len != 0) { |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1515 | ack = xmalloc(sizeof(*ack)); |
| 1516 | ack->id = ++id; |
| 1517 | ack->offset = offset; |
| 1518 | ack->len = len; |
| 1519 | TAILQ_INSERT_TAIL(&acks, ack, tq); |
| 1520 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1521 | buffer_clear(&msg); |
| 1522 | buffer_put_char(&msg, SSH2_FXP_WRITE); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1523 | buffer_put_int(&msg, ack->id); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1524 | buffer_put_string(&msg, handle, handle_len); |
| 1525 | buffer_put_int64(&msg, offset); |
| 1526 | buffer_put_string(&msg, data, len); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1527 | send_msg(conn, &msg); |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1528 | debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u", |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 1529 | id, (unsigned long long)offset, len); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1530 | } else if (TAILQ_FIRST(&acks) == NULL) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1531 | break; |
| 1532 | |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1533 | if (ack == NULL) |
| 1534 | fatal("Unexpected ACK %u", id); |
| 1535 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1536 | if (id == startid || len == 0 || |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1537 | id - ackid >= conn->num_requests) { |
Ben Lindstrom | 5a6abda | 2002-06-09 19:41:48 +0000 | [diff] [blame] | 1538 | u_int r_id; |
Ben Lindstrom | 06e9515 | 2002-04-06 04:16:45 +0000 | [diff] [blame] | 1539 | |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1540 | buffer_clear(&msg); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1541 | get_msg(conn, &msg); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1542 | type = buffer_get_char(&msg); |
Ben Lindstrom | 06e9515 | 2002-04-06 04:16:45 +0000 | [diff] [blame] | 1543 | r_id = buffer_get_int(&msg); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1544 | |
| 1545 | if (type != SSH2_FXP_STATUS) |
| 1546 | fatal("Expected SSH2_FXP_STATUS(%d) packet, " |
| 1547 | "got %d", SSH2_FXP_STATUS, type); |
| 1548 | |
| 1549 | status = buffer_get_int(&msg); |
| 1550 | debug3("SSH2_FXP_STATUS %d", status); |
| 1551 | |
| 1552 | /* Find the request in our queue */ |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1553 | for (ack = TAILQ_FIRST(&acks); |
Ben Lindstrom | 06e9515 | 2002-04-06 04:16:45 +0000 | [diff] [blame] | 1554 | ack != NULL && ack->id != r_id; |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1555 | ack = TAILQ_NEXT(ack, tq)) |
| 1556 | ; |
| 1557 | if (ack == NULL) |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1558 | fatal("Can't find request for ID %u", r_id); |
Damien Miller | 5873dfd | 2002-02-13 14:04:37 +1100 | [diff] [blame] | 1559 | TAILQ_REMOVE(&acks, ack, tq); |
Damien Miller | 8b3fdfb | 2007-09-17 16:12:03 +1000 | [diff] [blame] | 1560 | debug3("In write loop, ack for %u %u bytes at %lld", |
| 1561 | ack->id, ack->len, (long long)ack->offset); |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1562 | ++ackid; |
Darren Tucker | c9a1991 | 2013-06-02 08:37:05 +1000 | [diff] [blame] | 1563 | progress_counter += ack->len; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1564 | free(ack); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1565 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1566 | offset += len; |
Damien Miller | 8b3fdfb | 2007-09-17 16:12:03 +1000 | [diff] [blame] | 1567 | if (offset < 0) |
| 1568 | fatal("%s: offset < 0", __func__); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1569 | } |
Damien Miller | acdf25b | 2008-02-10 22:27:24 +1100 | [diff] [blame] | 1570 | buffer_free(&msg); |
| 1571 | |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1572 | if (showprogress) |
| 1573 | stop_progress_meter(); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1574 | free(data); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1575 | |
Damien Miller | acdf25b | 2008-02-10 22:27:24 +1100 | [diff] [blame] | 1576 | if (status != SSH2_FX_OK) { |
| 1577 | error("Couldn't write to remote file \"%s\": %s", |
| 1578 | remote_path, fx2txt(status)); |
| 1579 | status = -1; |
| 1580 | } |
| 1581 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1582 | if (close(local_fd) == -1) { |
| 1583 | error("Couldn't close local file \"%s\": %s", local_path, |
| 1584 | strerror(errno)); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1585 | status = -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1586 | } |
| 1587 | |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 1588 | /* Override umask and utimes if asked */ |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1589 | if (preserve_flag) |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1590 | do_fsetstat(conn, handle, handle_len, &a); |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 1591 | |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1592 | if (fsync_flag) |
| 1593 | (void)do_fsync(conn, handle, handle_len); |
| 1594 | |
Damien Miller | acdf25b | 2008-02-10 22:27:24 +1100 | [diff] [blame] | 1595 | if (do_close(conn, handle, handle_len) != SSH2_FX_OK) |
| 1596 | status = -1; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1597 | free(handle); |
Damien Miller | acdf25b | 2008-02-10 22:27:24 +1100 | [diff] [blame] | 1598 | |
| 1599 | return status; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1600 | } |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1601 | |
| 1602 | static int |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1603 | upload_dir_internal(struct sftp_conn *conn, char *src, char *dst, int depth, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1604 | int preserve_flag, int print_flag, int fsync_flag) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1605 | { |
| 1606 | int ret = 0, status; |
| 1607 | DIR *dirp; |
| 1608 | struct dirent *dp; |
| 1609 | char *filename, *new_src, *new_dst; |
| 1610 | struct stat sb; |
| 1611 | Attrib a; |
| 1612 | |
| 1613 | if (depth >= MAX_DIR_DEPTH) { |
| 1614 | error("Maximum directory depth exceeded: %d levels", depth); |
| 1615 | return -1; |
| 1616 | } |
| 1617 | |
| 1618 | if (stat(src, &sb) == -1) { |
| 1619 | error("Couldn't stat directory \"%s\": %s", |
| 1620 | src, strerror(errno)); |
| 1621 | return -1; |
| 1622 | } |
| 1623 | if (!S_ISDIR(sb.st_mode)) { |
| 1624 | error("\"%s\" is not a directory", src); |
| 1625 | return -1; |
| 1626 | } |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1627 | if (print_flag) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1628 | printf("Entering %s\n", src); |
| 1629 | |
| 1630 | attrib_clear(&a); |
| 1631 | stat_to_attrib(&sb, &a); |
| 1632 | a.flags &= ~SSH2_FILEXFER_ATTR_SIZE; |
| 1633 | a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID; |
| 1634 | a.perm &= 01777; |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1635 | if (!preserve_flag) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1636 | a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME; |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1637 | |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1638 | status = do_mkdir(conn, dst, &a, 0); |
| 1639 | /* |
| 1640 | * we lack a portable status for errno EEXIST, |
| 1641 | * so if we get a SSH2_FX_FAILURE back we must check |
| 1642 | * if it was created successfully. |
| 1643 | */ |
| 1644 | if (status != SSH2_FX_OK) { |
| 1645 | if (status != SSH2_FX_FAILURE) |
| 1646 | return -1; |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1647 | if (do_stat(conn, dst, 0) == NULL) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1648 | return -1; |
| 1649 | } |
| 1650 | |
| 1651 | if ((dirp = opendir(src)) == NULL) { |
| 1652 | error("Failed to open dir \"%s\": %s", src, strerror(errno)); |
| 1653 | return -1; |
| 1654 | } |
Damien Miller | 0d03241 | 2013-07-25 11:56:52 +1000 | [diff] [blame] | 1655 | |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1656 | while (((dp = readdir(dirp)) != NULL) && !interrupted) { |
| 1657 | if (dp->d_ino == 0) |
| 1658 | continue; |
| 1659 | filename = dp->d_name; |
| 1660 | new_dst = path_append(dst, filename); |
| 1661 | new_src = path_append(src, filename); |
| 1662 | |
Darren Tucker | 438b473 | 2009-10-11 21:52:10 +1100 | [diff] [blame] | 1663 | if (lstat(new_src, &sb) == -1) { |
| 1664 | logit("%s: lstat failed: %s", filename, |
| 1665 | strerror(errno)); |
| 1666 | ret = -1; |
| 1667 | } else if (S_ISDIR(sb.st_mode)) { |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1668 | if (strcmp(filename, ".") == 0 || |
| 1669 | strcmp(filename, "..") == 0) |
| 1670 | continue; |
| 1671 | |
| 1672 | if (upload_dir_internal(conn, new_src, new_dst, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1673 | depth + 1, preserve_flag, print_flag, |
| 1674 | fsync_flag) == -1) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1675 | ret = -1; |
Darren Tucker | 438b473 | 2009-10-11 21:52:10 +1100 | [diff] [blame] | 1676 | } else if (S_ISREG(sb.st_mode)) { |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1677 | if (do_upload(conn, new_src, new_dst, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1678 | preserve_flag, fsync_flag) == -1) { |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1679 | error("Uploading of file %s to %s failed!", |
| 1680 | new_src, new_dst); |
| 1681 | ret = -1; |
| 1682 | } |
| 1683 | } else |
| 1684 | logit("%s: not a regular file\n", filename); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1685 | free(new_dst); |
| 1686 | free(new_src); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | do_setstat(conn, dst, &a); |
| 1690 | |
| 1691 | (void) closedir(dirp); |
| 1692 | return ret; |
| 1693 | } |
| 1694 | |
| 1695 | int |
Damien Miller | bda5c84 | 2013-10-15 12:05:58 +1100 | [diff] [blame] | 1696 | upload_dir(struct sftp_conn *conn, char *src, char *dst, int preserve_flag, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1697 | int print_flag, int fsync_flag) |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1698 | { |
| 1699 | char *dst_canon; |
| 1700 | int ret; |
| 1701 | |
| 1702 | if ((dst_canon = do_realpath(conn, dst)) == NULL) { |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1703 | error("Unable to canonicalize path \"%s\"", dst); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1704 | return -1; |
| 1705 | } |
| 1706 | |
Damien Miller | 2f93d05 | 2013-10-15 12:06:27 +1100 | [diff] [blame] | 1707 | ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1708 | print_flag, fsync_flag); |
| 1709 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1710 | free(dst_canon); |
Darren Tucker | 1b0dd17 | 2009-10-07 08:37:48 +1100 | [diff] [blame] | 1711 | return ret; |
| 1712 | } |
| 1713 | |
| 1714 | char * |
| 1715 | path_append(char *p1, char *p2) |
| 1716 | { |
| 1717 | char *ret; |
| 1718 | size_t len = strlen(p1) + strlen(p2) + 2; |
| 1719 | |
| 1720 | ret = xmalloc(len); |
| 1721 | strlcpy(ret, p1, len); |
| 1722 | if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/') |
| 1723 | strlcat(ret, "/", len); |
| 1724 | strlcat(ret, p2, len); |
| 1725 | |
| 1726 | return(ret); |
| 1727 | } |
| 1728 | |