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