Annotation of src/usr.bin/ssh/sftp-client.c, Revision 1.163
1.163 ! djm 1: /* $OpenBSD: sftp-client.c,v 1.162 2022/03/31 03:07:03 djm Exp $ */
1.1 djm 2: /*
1.46 djm 3: * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
1.1 djm 4: *
1.46 djm 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.
1.1 djm 8: *
1.46 djm 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.
1.1 djm 16: */
17:
18: /* XXX: memleaks */
19: /* XXX: signed vs unsigned */
1.23 djm 20: /* XXX: remove all logging, only return status codes */
1.1 djm 21: /* XXX: copy between two remote sites */
22:
1.74 deraadt 23: #include <sys/types.h>
1.93 djm 24: #include <sys/poll.h>
1.21 djm 25: #include <sys/queue.h>
1.60 stevesk 26: #include <sys/stat.h>
1.71 stevesk 27: #include <sys/time.h>
1.82 djm 28: #include <sys/statvfs.h>
1.74 deraadt 29: #include <sys/uio.h>
1.66 stevesk 30:
1.89 djm 31: #include <dirent.h>
1.67 stevesk 32: #include <errno.h>
1.66 stevesk 33: #include <fcntl.h>
1.152 djm 34: #include <poll.h>
1.70 stevesk 35: #include <signal.h>
1.87 dtucker 36: #include <stdarg.h>
1.73 stevesk 37: #include <stdio.h>
1.109 dtucker 38: #include <stdlib.h>
1.69 stevesk 39: #include <string.h>
1.68 stevesk 40: #include <unistd.h>
1.1 djm 41:
1.74 deraadt 42: #include "xmalloc.h"
1.116 djm 43: #include "ssherr.h"
44: #include "sshbuf.h"
1.1 djm 45: #include "log.h"
46: #include "atomicio.h"
1.39 fgsch 47: #include "progressmeter.h"
1.64 djm 48: #include "misc.h"
1.124 schwarze 49: #include "utf8.h"
1.1 djm 50:
51: #include "sftp.h"
52: #include "sftp-common.h"
53: #include "sftp-client.h"
54:
1.49 djm 55: extern volatile sig_atomic_t interrupted;
1.39 fgsch 56: extern int showprogress;
57:
1.141 djm 58: /* Default size of buffer for up/download */
59: #define DEFAULT_COPY_BUFLEN 32768
60:
61: /* Default number of concurrent outstanding requests */
62: #define DEFAULT_NUM_REQUESTS 64
63:
1.59 david 64: /* Minimum amount of data to read at a time */
1.21 djm 65: #define MIN_READ_SIZE 512
66:
1.89 djm 67: /* Maximum depth to descend in directory trees */
68: #define MAX_DIR_DEPTH 64
69:
1.23 djm 70: struct sftp_conn {
71: int fd_in;
72: int fd_out;
1.141 djm 73: u_int download_buflen;
74: u_int upload_buflen;
1.23 djm 75: u_int num_requests;
76: u_int version;
77: u_int msg_id;
1.82 djm 78: #define SFTP_EXT_POSIX_RENAME 0x00000001
79: #define SFTP_EXT_STATVFS 0x00000002
80: #define SFTP_EXT_FSTATVFS 0x00000004
1.94 djm 81: #define SFTP_EXT_HARDLINK 0x00000008
1.107 djm 82: #define SFTP_EXT_FSYNC 0x00000010
1.131 djm 83: #define SFTP_EXT_LSETSTAT 0x00000020
1.141 djm 84: #define SFTP_EXT_LIMITS 0x00000040
1.154 djm 85: #define SFTP_EXT_PATH_EXPAND 0x00000080
1.162 djm 86: #define SFTP_EXT_COPY_DATA 0x00000100
1.81 djm 87: u_int exts;
1.93 djm 88: u_int64_t limit_kbps;
89: struct bwlimit bwlimit_in, bwlimit_out;
1.23 djm 90: };
1.4 djm 91:
1.146 djm 92: /* Tracks in-progress requests during file transfers */
93: struct request {
94: u_int id;
95: size_t len;
96: u_int64_t offset;
97: TAILQ_ENTRY(request) tq;
98: };
99: TAILQ_HEAD(requests, request);
100:
1.116 djm 101: static u_char *
102: get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
1.93 djm 103: const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
104:
1.151 djm 105: static struct request *
106: request_enqueue(struct requests *requests, u_int id, size_t len,
107: uint64_t offset)
108: {
109: struct request *req;
110:
111: req = xcalloc(1, sizeof(*req));
112: req->id = id;
113: req->len = len;
114: req->offset = offset;
115: TAILQ_INSERT_TAIL(requests, req, tq);
116: return req;
117: }
118:
119: static struct request *
120: request_find(struct requests *requests, u_int id)
121: {
122: struct request *req;
123:
124: for (req = TAILQ_FIRST(requests);
125: req != NULL && req->id != id;
126: req = TAILQ_NEXT(req, tq))
127: ;
128: return req;
129: }
130:
1.93 djm 131: /* ARGSUSED */
132: static int
133: sftpio(void *_bwlimit, size_t amount)
134: {
135: struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
136:
1.133 dtucker 137: refresh_progress_meter(0);
1.132 dtucker 138: if (bwlimit != NULL)
139: bandwidth_limit(bwlimit, amount);
1.93 djm 140: return 0;
141: }
1.88 djm 142:
1.17 itojun 143: static void
1.116 djm 144: send_msg(struct sftp_conn *conn, struct sshbuf *m)
1.1 djm 145: {
1.40 djm 146: u_char mlen[4];
1.65 djm 147: struct iovec iov[2];
1.40 djm 148:
1.116 djm 149: if (sshbuf_len(m) > SFTP_MAX_MSG_LENGTH)
150: fatal("Outbound message too long %zu", sshbuf_len(m));
1.40 djm 151:
152: /* Send length first */
1.116 djm 153: put_u32(mlen, sshbuf_len(m));
1.65 djm 154: iov[0].iov_base = mlen;
155: iov[0].iov_len = sizeof(mlen);
1.116 djm 156: iov[1].iov_base = (u_char *)sshbuf_ptr(m);
157: iov[1].iov_len = sshbuf_len(m);
1.74 deraadt 158:
1.132 dtucker 159: if (atomiciov6(writev, conn->fd_out, iov, 2, sftpio,
160: conn->limit_kbps > 0 ? &conn->bwlimit_out : NULL) !=
1.116 djm 161: sshbuf_len(m) + sizeof(mlen))
1.1 djm 162: fatal("Couldn't send packet: %s", strerror(errno));
163:
1.116 djm 164: sshbuf_reset(m);
1.1 djm 165: }
166:
1.17 itojun 167: static void
1.128 dtucker 168: get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
1.1 djm 169: {
1.40 djm 170: u_int msg_len;
1.116 djm 171: u_char *p;
172: int r;
1.1 djm 173:
1.144 djm 174: sshbuf_reset(m);
1.116 djm 175: if ((r = sshbuf_reserve(m, 4, &p)) != 0)
1.137 djm 176: fatal_fr(r, "reserve");
1.132 dtucker 177: if (atomicio6(read, conn->fd_in, p, 4, sftpio,
178: conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL) != 4) {
1.127 djm 179: if (errno == EPIPE || errno == ECONNRESET)
1.54 avsm 180: fatal("Connection closed");
181: else
182: fatal("Couldn't read packet: %s", strerror(errno));
183: }
1.1 djm 184:
1.116 djm 185: if ((r = sshbuf_get_u32(m, &msg_len)) != 0)
1.137 djm 186: fatal_fr(r, "sshbuf_get_u32");
1.128 dtucker 187: if (msg_len > SFTP_MAX_MSG_LENGTH) {
188: do_log2(initial ? SYSLOG_LEVEL_ERROR : SYSLOG_LEVEL_FATAL,
189: "Received message too long %u", msg_len);
190: fatal("Ensure the remote shell produces no output "
191: "for non-interactive sessions.");
192: }
1.1 djm 193:
1.116 djm 194: if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
1.137 djm 195: fatal_fr(r, "reserve");
1.132 dtucker 196: if (atomicio6(read, conn->fd_in, p, msg_len, sftpio,
197: conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL)
1.93 djm 198: != msg_len) {
1.54 avsm 199: if (errno == EPIPE)
200: fatal("Connection closed");
201: else
202: fatal("Read packet: %s", strerror(errno));
203: }
1.1 djm 204: }
205:
1.17 itojun 206: static void
1.128 dtucker 207: get_msg(struct sftp_conn *conn, struct sshbuf *m)
208: {
209: get_msg_extended(conn, m, 0);
210: }
211:
212: static void
1.116 djm 213: send_string_request(struct sftp_conn *conn, u_int id, u_int code, const char *s,
1.1 djm 214: u_int len)
215: {
1.116 djm 216: struct sshbuf *msg;
217: int r;
1.1 djm 218:
1.116 djm 219: if ((msg = sshbuf_new()) == NULL)
1.137 djm 220: fatal_f("sshbuf_new failed");
1.116 djm 221: if ((r = sshbuf_put_u8(msg, code)) != 0 ||
222: (r = sshbuf_put_u32(msg, id)) != 0 ||
223: (r = sshbuf_put_string(msg, s, len)) != 0)
1.137 djm 224: fatal_fr(r, "compose");
1.116 djm 225: send_msg(conn, msg);
1.93 djm 226: debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
1.116 djm 227: sshbuf_free(msg);
1.1 djm 228: }
229:
1.17 itojun 230: static void
1.93 djm 231: send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
1.116 djm 232: const void *s, u_int len, Attrib *a)
1.1 djm 233: {
1.116 djm 234: struct sshbuf *msg;
235: int r;
1.1 djm 236:
1.116 djm 237: if ((msg = sshbuf_new()) == NULL)
1.137 djm 238: fatal_f("sshbuf_new failed");
1.116 djm 239: if ((r = sshbuf_put_u8(msg, code)) != 0 ||
240: (r = sshbuf_put_u32(msg, id)) != 0 ||
241: (r = sshbuf_put_string(msg, s, len)) != 0 ||
242: (r = encode_attrib(msg, a)) != 0)
1.137 djm 243: fatal_fr(r, "compose");
1.116 djm 244: send_msg(conn, msg);
1.148 djm 245: debug3("Sent message fd %d T:%u I:%u F:0x%04x M:%05o",
246: conn->fd_out, code, id, a->flags, a->perm);
1.116 djm 247: sshbuf_free(msg);
1.1 djm 248: }
249:
1.17 itojun 250: static u_int
1.93 djm 251: get_status(struct sftp_conn *conn, u_int expected_id)
1.1 djm 252: {
1.116 djm 253: struct sshbuf *msg;
254: u_char type;
255: u_int id, status;
256: int r;
1.1 djm 257:
1.116 djm 258: if ((msg = sshbuf_new()) == NULL)
1.137 djm 259: fatal_f("sshbuf_new failed");
1.116 djm 260: get_msg(conn, msg);
261: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
262: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 djm 263: fatal_fr(r, "compose");
1.1 djm 264:
265: if (id != expected_id)
1.33 deraadt 266: fatal("ID mismatch (%u != %u)", id, expected_id);
1.1 djm 267: if (type != SSH2_FXP_STATUS)
1.33 deraadt 268: fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
1.1 djm 269: SSH2_FXP_STATUS, type);
270:
1.116 djm 271: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 djm 272: fatal_fr(r, "parse");
1.116 djm 273: sshbuf_free(msg);
1.1 djm 274:
1.33 deraadt 275: debug3("SSH2_FXP_STATUS %u", status);
1.1 djm 276:
1.93 djm 277: return status;
1.1 djm 278: }
279:
1.116 djm 280: static u_char *
281: get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
1.93 djm 282: const char *errfmt, ...)
1.1 djm 283: {
1.116 djm 284: struct sshbuf *msg;
285: u_int id, status;
286: u_char type;
287: u_char *handle;
288: char errmsg[256];
1.88 djm 289: va_list args;
1.116 djm 290: int r;
1.88 djm 291:
292: va_start(args, errfmt);
293: if (errfmt != NULL)
294: vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
295: va_end(args);
1.1 djm 296:
1.116 djm 297: if ((msg = sshbuf_new()) == NULL)
1.137 djm 298: fatal_f("sshbuf_new failed");
1.116 djm 299: get_msg(conn, msg);
300: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
301: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 djm 302: fatal_fr(r, "parse");
1.1 djm 303:
304: if (id != expected_id)
1.88 djm 305: fatal("%s: ID mismatch (%u != %u)",
306: errfmt == NULL ? __func__ : errmsg, id, expected_id);
1.1 djm 307: if (type == SSH2_FXP_STATUS) {
1.116 djm 308: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 djm 309: fatal_fr(r, "parse status");
1.88 djm 310: if (errfmt != NULL)
311: error("%s: %s", errmsg, fx2txt(status));
1.116 djm 312: sshbuf_free(msg);
1.1 djm 313: return(NULL);
314: } else if (type != SSH2_FXP_HANDLE)
1.88 djm 315: fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
316: errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
1.1 djm 317:
1.116 djm 318: if ((r = sshbuf_get_string(msg, &handle, len)) != 0)
1.137 djm 319: fatal_fr(r, "parse handle");
1.116 djm 320: sshbuf_free(msg);
1.1 djm 321:
1.116 djm 322: return handle;
1.1 djm 323: }
324:
1.158 jsg 325: /* XXX returning &static is error-prone. Refactor to fill *Attrib argument */
1.17 itojun 326: static Attrib *
1.93 djm 327: get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
1.1 djm 328: {
1.116 djm 329: struct sshbuf *msg;
330: u_int id;
331: u_char type;
332: int r;
333: static Attrib a;
334:
335: if ((msg = sshbuf_new()) == NULL)
1.137 djm 336: fatal_f("sshbuf_new failed");
1.116 djm 337: get_msg(conn, msg);
338:
339: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
340: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 djm 341: fatal_fr(r, "parse");
1.1 djm 342:
343: if (id != expected_id)
1.33 deraadt 344: fatal("ID mismatch (%u != %u)", id, expected_id);
1.1 djm 345: if (type == SSH2_FXP_STATUS) {
1.116 djm 346: u_int status;
1.1 djm 347:
1.116 djm 348: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 djm 349: fatal_fr(r, "parse status");
1.14 djm 350: if (quiet)
1.161 djm 351: debug("stat remote: %s", fx2txt(status));
1.14 djm 352: else
1.161 djm 353: error("stat remote: %s", fx2txt(status));
1.116 djm 354: sshbuf_free(msg);
1.1 djm 355: return(NULL);
356: } else if (type != SSH2_FXP_ATTRS) {
1.33 deraadt 357: fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
1.1 djm 358: SSH2_FXP_ATTRS, type);
359: }
1.116 djm 360: if ((r = decode_attrib(msg, &a)) != 0) {
1.137 djm 361: error_fr(r, "decode_attrib");
1.116 djm 362: sshbuf_free(msg);
363: return NULL;
364: }
1.158 jsg 365: debug3("Received stat reply T:%u I:%u F:0x%04x M:%05o",
1.148 djm 366: type, id, a.flags, a.perm);
1.116 djm 367: sshbuf_free(msg);
1.1 djm 368:
1.116 djm 369: return &a;
1.1 djm 370: }
371:
1.82 djm 372: static int
1.93 djm 373: get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
374: u_int expected_id, int quiet)
1.82 djm 375: {
1.116 djm 376: struct sshbuf *msg;
377: u_char type;
378: u_int id;
379: u_int64_t flag;
380: int r;
1.82 djm 381:
1.116 djm 382: if ((msg = sshbuf_new()) == NULL)
1.137 djm 383: fatal_f("sshbuf_new failed");
1.116 djm 384: get_msg(conn, msg);
385:
386: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
387: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 djm 388: fatal_fr(r, "parse");
1.82 djm 389:
390: debug3("Received statvfs reply T:%u I:%u", type, id);
391: if (id != expected_id)
392: fatal("ID mismatch (%u != %u)", id, expected_id);
393: if (type == SSH2_FXP_STATUS) {
1.116 djm 394: u_int status;
1.82 djm 395:
1.116 djm 396: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 djm 397: fatal_fr(r, "parse status");
1.82 djm 398: if (quiet)
1.161 djm 399: debug("remote statvfs: %s", fx2txt(status));
1.82 djm 400: else
1.161 djm 401: error("remote statvfs: %s", fx2txt(status));
1.116 djm 402: sshbuf_free(msg);
1.82 djm 403: return -1;
404: } else if (type != SSH2_FXP_EXTENDED_REPLY) {
405: fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
406: SSH2_FXP_EXTENDED_REPLY, type);
407: }
408:
1.114 tedu 409: memset(st, 0, sizeof(*st));
1.116 djm 410: if ((r = sshbuf_get_u64(msg, &st->f_bsize)) != 0 ||
411: (r = sshbuf_get_u64(msg, &st->f_frsize)) != 0 ||
412: (r = sshbuf_get_u64(msg, &st->f_blocks)) != 0 ||
413: (r = sshbuf_get_u64(msg, &st->f_bfree)) != 0 ||
414: (r = sshbuf_get_u64(msg, &st->f_bavail)) != 0 ||
415: (r = sshbuf_get_u64(msg, &st->f_files)) != 0 ||
416: (r = sshbuf_get_u64(msg, &st->f_ffree)) != 0 ||
417: (r = sshbuf_get_u64(msg, &st->f_favail)) != 0 ||
418: (r = sshbuf_get_u64(msg, &st->f_fsid)) != 0 ||
419: (r = sshbuf_get_u64(msg, &flag)) != 0 ||
420: (r = sshbuf_get_u64(msg, &st->f_namemax)) != 0)
1.137 djm 421: fatal_fr(r, "parse statvfs");
1.82 djm 422:
423: st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
424: st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
425:
1.116 djm 426: sshbuf_free(msg);
1.82 djm 427:
428: return 0;
429: }
430:
1.23 djm 431: struct sftp_conn *
1.93 djm 432: do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
433: u_int64_t limit_kbps)
1.1 djm 434: {
1.116 djm 435: u_char type;
436: struct sshbuf *msg;
1.23 djm 437: struct sftp_conn *ret;
1.116 djm 438: int r;
1.1 djm 439:
1.103 djm 440: ret = xcalloc(1, sizeof(*ret));
441: ret->msg_id = 1;
1.93 djm 442: ret->fd_in = fd_in;
443: ret->fd_out = fd_out;
1.141 djm 444: ret->download_buflen = ret->upload_buflen =
445: transfer_buflen ? transfer_buflen : DEFAULT_COPY_BUFLEN;
446: ret->num_requests =
447: num_requests ? num_requests : DEFAULT_NUM_REQUESTS;
1.93 djm 448: ret->exts = 0;
449: ret->limit_kbps = 0;
450:
1.116 djm 451: if ((msg = sshbuf_new()) == NULL)
1.137 djm 452: fatal_f("sshbuf_new failed");
1.116 djm 453: if ((r = sshbuf_put_u8(msg, SSH2_FXP_INIT)) != 0 ||
454: (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0)
1.137 djm 455: fatal_fr(r, "parse");
456:
1.116 djm 457: send_msg(ret, msg);
1.1 djm 458:
1.128 dtucker 459: get_msg_extended(ret, msg, 1);
1.1 djm 460:
1.3 stevesk 461: /* Expecting a VERSION reply */
1.116 djm 462: if ((r = sshbuf_get_u8(msg, &type)) != 0)
1.137 djm 463: fatal_fr(r, "parse type");
1.116 djm 464: if (type != SSH2_FXP_VERSION) {
1.33 deraadt 465: error("Invalid packet back from SSH2_FXP_INIT (type %u)",
1.1 djm 466: type);
1.116 djm 467: sshbuf_free(msg);
1.119 jsg 468: free(ret);
1.23 djm 469: return(NULL);
1.1 djm 470: }
1.116 djm 471: if ((r = sshbuf_get_u32(msg, &ret->version)) != 0)
1.137 djm 472: fatal_fr(r, "parse version");
1.1 djm 473:
1.93 djm 474: debug2("Remote version: %u", ret->version);
1.1 djm 475:
476: /* Check for extensions */
1.116 djm 477: while (sshbuf_len(msg) > 0) {
478: char *name;
479: u_char *value;
480: size_t vlen;
1.85 djm 481: int known = 0;
1.1 djm 482:
1.116 djm 483: if ((r = sshbuf_get_cstring(msg, &name, NULL)) != 0 ||
484: (r = sshbuf_get_string(msg, &value, &vlen)) != 0)
1.137 djm 485: fatal_fr(r, "parse extension");
1.82 djm 486: if (strcmp(name, "posix-rename@openssh.com") == 0 &&
1.116 djm 487: strcmp((char *)value, "1") == 0) {
1.93 djm 488: ret->exts |= SFTP_EXT_POSIX_RENAME;
1.85 djm 489: known = 1;
490: } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
1.116 djm 491: strcmp((char *)value, "2") == 0) {
1.93 djm 492: ret->exts |= SFTP_EXT_STATVFS;
1.85 djm 493: known = 1;
1.94 djm 494: } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
1.116 djm 495: strcmp((char *)value, "2") == 0) {
1.93 djm 496: ret->exts |= SFTP_EXT_FSTATVFS;
1.85 djm 497: known = 1;
1.94 djm 498: } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
1.116 djm 499: strcmp((char *)value, "1") == 0) {
1.94 djm 500: ret->exts |= SFTP_EXT_HARDLINK;
501: known = 1;
1.116 djm 502: } else if (strcmp(name, "fsync@openssh.com") == 0 &&
503: strcmp((char *)value, "1") == 0) {
504: ret->exts |= SFTP_EXT_FSYNC;
505: known = 1;
1.131 djm 506: } else if (strcmp(name, "lsetstat@openssh.com") == 0 &&
507: strcmp((char *)value, "1") == 0) {
508: ret->exts |= SFTP_EXT_LSETSTAT;
509: known = 1;
1.141 djm 510: } else if (strcmp(name, "limits@openssh.com") == 0 &&
511: strcmp((char *)value, "1") == 0) {
512: ret->exts |= SFTP_EXT_LIMITS;
513: known = 1;
1.154 djm 514: } else if (strcmp(name, "expand-path@openssh.com") == 0 &&
515: strcmp((char *)value, "1") == 0) {
516: ret->exts |= SFTP_EXT_PATH_EXPAND;
517: known = 1;
1.162 djm 518: } else if (strcmp(name, "copy-data") == 0 &&
519: strcmp((char *)value, "1") == 0) {
520: ret->exts |= SFTP_EXT_COPY_DATA;
521: known = 1;
1.85 djm 522: }
523: if (known) {
524: debug2("Server supports extension \"%s\" revision %s",
525: name, value);
526: } else {
527: debug2("Unrecognised server extension \"%s\"", name);
528: }
1.98 djm 529: free(name);
530: free(value);
1.1 djm 531: }
532:
1.116 djm 533: sshbuf_free(msg);
1.11 djm 534:
1.141 djm 535: /* Query the server for its limits */
536: if (ret->exts & SFTP_EXT_LIMITS) {
537: struct sftp_limits limits;
538: if (do_limits(ret, &limits) != 0)
539: fatal_f("limits failed");
540:
541: /* If the caller did not specify, find a good value */
542: if (transfer_buflen == 0) {
543: ret->download_buflen = limits.read_length;
544: ret->upload_buflen = limits.write_length;
545: debug("Using server download size %u", ret->download_buflen);
546: debug("Using server upload size %u", ret->upload_buflen);
547: }
548:
549: /* Use the server limit to scale down our value only */
550: if (num_requests == 0 && limits.open_handles) {
551: ret->num_requests =
552: MINIMUM(DEFAULT_NUM_REQUESTS, limits.open_handles);
553: debug("Server handle limit %llu; using %u",
1.142 djm 554: (unsigned long long)limits.open_handles,
555: ret->num_requests);
1.141 djm 556: }
557: }
558:
1.23 djm 559: /* Some filexfer v.0 servers don't support large packets */
1.141 djm 560: if (ret->version == 0) {
561: ret->download_buflen = MINIMUM(ret->download_buflen, 20480);
562: ret->upload_buflen = MINIMUM(ret->upload_buflen, 20480);
563: }
1.23 djm 564:
1.93 djm 565: ret->limit_kbps = limit_kbps;
566: if (ret->limit_kbps > 0) {
567: bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
1.141 djm 568: ret->download_buflen);
1.93 djm 569: bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
1.141 djm 570: ret->upload_buflen);
1.93 djm 571: }
572:
573: return ret;
1.23 djm 574: }
575:
576: u_int
577: sftp_proto_version(struct sftp_conn *conn)
578: {
1.93 djm 579: return conn->version;
1.1 djm 580: }
581:
582: int
1.141 djm 583: do_limits(struct sftp_conn *conn, struct sftp_limits *limits)
584: {
585: u_int id, msg_id;
586: u_char type;
587: struct sshbuf *msg;
588: int r;
589:
590: if ((conn->exts & SFTP_EXT_LIMITS) == 0) {
591: error("Server does not support limits@openssh.com extension");
592: return -1;
593: }
594:
595: if ((msg = sshbuf_new()) == NULL)
596: fatal_f("sshbuf_new failed");
597:
598: id = conn->msg_id++;
599: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
600: (r = sshbuf_put_u32(msg, id)) != 0 ||
601: (r = sshbuf_put_cstring(msg, "limits@openssh.com")) != 0)
602: fatal_fr(r, "compose");
603: send_msg(conn, msg);
604: debug3("Sent message limits@openssh.com I:%u", id);
605:
606: get_msg(conn, msg);
607:
608: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
609: (r = sshbuf_get_u32(msg, &msg_id)) != 0)
610: fatal_fr(r, "parse");
611:
612: debug3("Received limits reply T:%u I:%u", type, msg_id);
613: if (id != msg_id)
614: fatal("ID mismatch (%u != %u)", msg_id, id);
615: if (type != SSH2_FXP_EXTENDED_REPLY) {
1.143 djm 616: debug_f("expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
1.142 djm 617: SSH2_FXP_EXTENDED_REPLY, type);
1.143 djm 618: /* Disable the limits extension */
619: conn->exts &= ~SFTP_EXT_LIMITS;
620: sshbuf_free(msg);
621: return 0;
1.141 djm 622: }
623:
624: memset(limits, 0, sizeof(*limits));
625: if ((r = sshbuf_get_u64(msg, &limits->packet_length)) != 0 ||
626: (r = sshbuf_get_u64(msg, &limits->read_length)) != 0 ||
627: (r = sshbuf_get_u64(msg, &limits->write_length)) != 0 ||
628: (r = sshbuf_get_u64(msg, &limits->open_handles)) != 0)
629: fatal_fr(r, "parse limits");
630:
631: sshbuf_free(msg);
632:
633: return 0;
634: }
635:
636: int
1.116 djm 637: do_close(struct sftp_conn *conn, const u_char *handle, u_int handle_len)
1.1 djm 638: {
639: u_int id, status;
1.116 djm 640: struct sshbuf *msg;
641: int r;
1.1 djm 642:
1.116 djm 643: if ((msg = sshbuf_new()) == NULL)
1.137 djm 644: fatal_f("sshbuf_new failed");
1.1 djm 645:
1.23 djm 646: id = conn->msg_id++;
1.116 djm 647: if ((r = sshbuf_put_u8(msg, SSH2_FXP_CLOSE)) != 0 ||
648: (r = sshbuf_put_u32(msg, id)) != 0 ||
649: (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137 djm 650: fatal_fr(r, "parse");
1.116 djm 651: send_msg(conn, msg);
1.33 deraadt 652: debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
1.1 djm 653:
1.93 djm 654: status = get_status(conn, id);
1.1 djm 655: if (status != SSH2_FX_OK)
1.161 djm 656: error("close remote: %s", fx2txt(status));
1.1 djm 657:
1.116 djm 658: sshbuf_free(msg);
1.1 djm 659:
1.116 djm 660: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 661: }
662:
1.12 djm 663:
1.17 itojun 664: static int
1.116 djm 665: do_lsreaddir(struct sftp_conn *conn, const char *path, int print_flag,
1.12 djm 666: SFTP_DIRENT ***dir)
1.1 djm 667: {
1.116 djm 668: struct sshbuf *msg;
669: u_int count, id, i, expected_id, ents = 0;
670: size_t handle_len;
1.123 djm 671: u_char type, *handle;
1.111 djm 672: int status = SSH2_FX_FAILURE;
1.116 djm 673: int r;
1.111 djm 674:
675: if (dir)
676: *dir = NULL;
1.1 djm 677:
1.23 djm 678: id = conn->msg_id++;
1.1 djm 679:
1.116 djm 680: if ((msg = sshbuf_new()) == NULL)
1.137 djm 681: fatal_f("sshbuf_new failed");
1.116 djm 682: if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPENDIR)) != 0 ||
683: (r = sshbuf_put_u32(msg, id)) != 0 ||
684: (r = sshbuf_put_cstring(msg, path)) != 0)
1.137 djm 685: fatal_fr(r, "compose OPENDIR");
1.116 djm 686: send_msg(conn, msg);
1.1 djm 687:
1.93 djm 688: handle = get_handle(conn, id, &handle_len,
1.88 djm 689: "remote readdir(\"%s\")", path);
1.96 markus 690: if (handle == NULL) {
1.116 djm 691: sshbuf_free(msg);
1.93 djm 692: return -1;
1.96 markus 693: }
1.1 djm 694:
1.12 djm 695: if (dir) {
696: ents = 0;
1.108 djm 697: *dir = xcalloc(1, sizeof(**dir));
1.12 djm 698: (*dir)[0] = NULL;
699: }
700:
1.49 djm 701: for (; !interrupted;) {
1.23 djm 702: id = expected_id = conn->msg_id++;
1.1 djm 703:
1.33 deraadt 704: debug3("Sending SSH2_FXP_READDIR I:%u", id);
1.1 djm 705:
1.116 djm 706: sshbuf_reset(msg);
707: if ((r = sshbuf_put_u8(msg, SSH2_FXP_READDIR)) != 0 ||
708: (r = sshbuf_put_u32(msg, id)) != 0 ||
709: (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137 djm 710: fatal_fr(r, "compose READDIR");
1.116 djm 711: send_msg(conn, msg);
712:
713: sshbuf_reset(msg);
714:
715: get_msg(conn, msg);
716:
717: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
718: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 djm 719: fatal_fr(r, "parse");
1.1 djm 720:
1.33 deraadt 721: debug3("Received reply T:%u I:%u", type, id);
1.1 djm 722:
723: if (id != expected_id)
1.33 deraadt 724: fatal("ID mismatch (%u != %u)", id, expected_id);
1.1 djm 725:
726: if (type == SSH2_FXP_STATUS) {
1.116 djm 727: u_int rstatus;
728:
729: if ((r = sshbuf_get_u32(msg, &rstatus)) != 0)
1.137 djm 730: fatal_fr(r, "parse status");
1.116 djm 731: debug3("Received SSH2_FXP_STATUS %d", rstatus);
732: if (rstatus == SSH2_FX_EOF)
1.1 djm 733: break;
1.116 djm 734: error("Couldn't read directory: %s", fx2txt(rstatus));
1.111 djm 735: goto out;
1.1 djm 736: } else if (type != SSH2_FXP_NAME)
1.33 deraadt 737: fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.1 djm 738: SSH2_FXP_NAME, type);
739:
1.116 djm 740: if ((r = sshbuf_get_u32(msg, &count)) != 0)
1.137 djm 741: fatal_fr(r, "parse count");
1.126 djm 742: if (count > SSHBUF_SIZE_MAX)
1.137 djm 743: fatal_f("nonsensical number of entries");
1.7 markus 744: if (count == 0)
745: break;
1.8 stevesk 746: debug3("Received %d SSH2_FXP_NAME responses", count);
1.19 deraadt 747: for (i = 0; i < count; i++) {
1.1 djm 748: char *filename, *longname;
1.116 djm 749: Attrib a;
1.1 djm 750:
1.116 djm 751: if ((r = sshbuf_get_cstring(msg, &filename,
752: NULL)) != 0 ||
753: (r = sshbuf_get_cstring(msg, &longname,
754: NULL)) != 0)
1.137 djm 755: fatal_fr(r, "parse filenames");
1.116 djm 756: if ((r = decode_attrib(msg, &a)) != 0) {
1.137 djm 757: error_fr(r, "couldn't decode attrib");
1.116 djm 758: free(filename);
759: free(longname);
1.135 djm 760: goto out;
1.116 djm 761: }
1.1 djm 762:
1.105 djm 763: if (print_flag)
1.124 schwarze 764: mprintf("%s\n", longname);
1.12 djm 765:
1.89 djm 766: /*
767: * Directory entries should never contain '/'
768: * These can be used to attack recursive ops
769: * (e.g. send '../../../../etc/passwd')
770: */
771: if (strchr(filename, '/') != NULL) {
772: error("Server sent suspect path \"%s\" "
773: "during readdir of \"%s\"", filename, path);
1.111 djm 774: } else if (dir) {
1.118 deraadt 775: *dir = xreallocarray(*dir, ents + 2, sizeof(**dir));
1.108 djm 776: (*dir)[ents] = xcalloc(1, sizeof(***dir));
1.12 djm 777: (*dir)[ents]->filename = xstrdup(filename);
778: (*dir)[ents]->longname = xstrdup(longname);
1.116 djm 779: memcpy(&(*dir)[ents]->a, &a, sizeof(a));
1.12 djm 780: (*dir)[++ents] = NULL;
781: }
1.98 djm 782: free(filename);
783: free(longname);
1.1 djm 784: }
785: }
1.111 djm 786: status = 0;
1.1 djm 787:
1.111 djm 788: out:
1.116 djm 789: sshbuf_free(msg);
1.23 djm 790: do_close(conn, handle, handle_len);
1.98 djm 791: free(handle);
1.1 djm 792:
1.111 djm 793: if (status != 0 && dir != NULL) {
794: /* Don't return results on error */
795: free_sftp_dirents(*dir);
796: *dir = NULL;
797: } else if (interrupted && dir != NULL && *dir != NULL) {
798: /* Don't return partial matches on interrupt */
1.49 djm 799: free_sftp_dirents(*dir);
1.108 djm 800: *dir = xcalloc(1, sizeof(**dir));
1.49 djm 801: **dir = NULL;
802: }
803:
1.129 djm 804: return status == SSH2_FX_OK ? 0 : -1;
1.12 djm 805: }
806:
807: int
1.116 djm 808: do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir)
1.12 djm 809: {
1.23 djm 810: return(do_lsreaddir(conn, path, 0, dir));
1.12 djm 811: }
812:
813: void free_sftp_dirents(SFTP_DIRENT **s)
814: {
815: int i;
1.19 deraadt 816:
1.111 djm 817: if (s == NULL)
818: return;
1.19 deraadt 819: for (i = 0; s[i]; i++) {
1.98 djm 820: free(s[i]->filename);
821: free(s[i]->longname);
822: free(s[i]);
1.12 djm 823: }
1.98 djm 824: free(s);
1.12 djm 825: }
826:
827: int
1.116 djm 828: do_rm(struct sftp_conn *conn, const char *path)
1.1 djm 829: {
830: u_int status, id;
831:
832: debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
833:
1.23 djm 834: id = conn->msg_id++;
1.93 djm 835: send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
836: status = get_status(conn, id);
1.1 djm 837: if (status != SSH2_FX_OK)
1.161 djm 838: error("remote delete %s: %s", path, fx2txt(status));
1.116 djm 839: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 840: }
841:
842: int
1.116 djm 843: do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag)
1.1 djm 844: {
845: u_int status, id;
846:
1.161 djm 847: debug2("Sending SSH2_FXP_MKDIR \"%s\"", path);
848:
1.23 djm 849: id = conn->msg_id++;
1.93 djm 850: send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
1.1 djm 851: strlen(path), a);
852:
1.93 djm 853: status = get_status(conn, id);
1.105 djm 854: if (status != SSH2_FX_OK && print_flag)
1.161 djm 855: error("remote mkdir \"%s\": %s", path, fx2txt(status));
1.1 djm 856:
1.116 djm 857: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 858: }
859:
860: int
1.116 djm 861: do_rmdir(struct sftp_conn *conn, const char *path)
1.1 djm 862: {
863: u_int status, id;
864:
1.161 djm 865: debug2("Sending SSH2_FXP_RMDIR \"%s\"", path);
866:
1.23 djm 867: id = conn->msg_id++;
1.93 djm 868: send_string_request(conn, id, SSH2_FXP_RMDIR, path,
1.23 djm 869: strlen(path));
1.1 djm 870:
1.93 djm 871: status = get_status(conn, id);
1.1 djm 872: if (status != SSH2_FX_OK)
1.161 djm 873: error("remote rmdir \"%s\": %s", path, fx2txt(status));
1.1 djm 874:
1.116 djm 875: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 876: }
877:
878: Attrib *
1.116 djm 879: do_stat(struct sftp_conn *conn, const char *path, int quiet)
1.1 djm 880: {
881: u_int id;
882:
1.161 djm 883: debug2("Sending SSH2_FXP_STAT \"%s\"", path);
884:
1.23 djm 885: id = conn->msg_id++;
886:
1.93 djm 887: send_string_request(conn, id,
1.28 markus 888: conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
1.23 djm 889: path, strlen(path));
890:
1.93 djm 891: return(get_decode_stat(conn, id, quiet));
1.1 djm 892: }
893:
894: Attrib *
1.116 djm 895: do_lstat(struct sftp_conn *conn, const char *path, int quiet)
1.1 djm 896: {
897: u_int id;
898:
1.23 djm 899: if (conn->version == 0) {
900: if (quiet)
901: debug("Server version does not support lstat operation");
902: else
1.43 itojun 903: logit("Server version does not support lstat operation");
1.30 markus 904: return(do_stat(conn, path, quiet));
1.23 djm 905: }
906:
907: id = conn->msg_id++;
1.93 djm 908: send_string_request(conn, id, SSH2_FXP_LSTAT, path,
1.23 djm 909: strlen(path));
910:
1.93 djm 911: return(get_decode_stat(conn, id, quiet));
1.1 djm 912: }
913:
1.78 chl 914: #ifdef notyet
1.1 djm 915: Attrib *
1.116 djm 916: do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
917: int quiet)
1.1 djm 918: {
919: u_int id;
920:
1.161 djm 921: debug2("Sending SSH2_FXP_FSTAT \"%s\"");
922:
1.23 djm 923: id = conn->msg_id++;
1.93 djm 924: send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
1.23 djm 925: handle_len);
926:
1.93 djm 927: return(get_decode_stat(conn, id, quiet));
1.1 djm 928: }
1.78 chl 929: #endif
1.1 djm 930:
931: int
1.116 djm 932: do_setstat(struct sftp_conn *conn, const char *path, Attrib *a)
1.1 djm 933: {
934: u_int status, id;
935:
1.161 djm 936: debug2("Sending SSH2_FXP_SETSTAT \"%s\"", path);
937:
1.23 djm 938: id = conn->msg_id++;
1.93 djm 939: send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
1.1 djm 940: strlen(path), a);
941:
1.93 djm 942: status = get_status(conn, id);
1.1 djm 943: if (status != SSH2_FX_OK)
1.161 djm 944: error("remote setstat \"%s\": %s", path, fx2txt(status));
1.1 djm 945:
1.116 djm 946: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 947: }
948:
949: int
1.116 djm 950: do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
1.1 djm 951: Attrib *a)
952: {
953: u_int status, id;
954:
1.161 djm 955: debug2("Sending SSH2_FXP_FSETSTAT");
956:
1.23 djm 957: id = conn->msg_id++;
1.93 djm 958: send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
1.1 djm 959: handle_len, a);
960:
1.93 djm 961: status = get_status(conn, id);
1.1 djm 962: if (status != SSH2_FX_OK)
1.161 djm 963: error("remote fsetstat: %s", fx2txt(status));
1.1 djm 964:
1.116 djm 965: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 966: }
967:
1.154 djm 968: /* Implements both the realpath and expand-path operations */
969: static char *
970: do_realpath_expand(struct sftp_conn *conn, const char *path, int expand)
1.1 djm 971: {
1.116 djm 972: struct sshbuf *msg;
973: u_int expected_id, count, id;
1.1 djm 974: char *filename, *longname;
1.116 djm 975: Attrib a;
976: u_char type;
977: int r;
1.154 djm 978: const char *what = "SSH2_FXP_REALPATH";
1.1 djm 979:
1.154 djm 980: if (expand)
981: what = "expand-path@openssh.com";
1.116 djm 982: if ((msg = sshbuf_new()) == NULL)
1.137 djm 983: fatal_f("sshbuf_new failed");
1.1 djm 984:
1.154 djm 985: expected_id = id = conn->msg_id++;
986: if (expand) {
1.161 djm 987: debug2("Sending SSH2_FXP_EXTENDED(expand-path@openssh.com) "
988: "\"%s\"", path);
1.154 djm 989: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
990: (r = sshbuf_put_u32(msg, id)) != 0 ||
991: (r = sshbuf_put_cstring(msg,
992: "expand-path@openssh.com")) != 0 ||
993: (r = sshbuf_put_cstring(msg, path)) != 0)
994: fatal_fr(r, "compose %s", what);
995: send_msg(conn, msg);
996: } else {
1.161 djm 997: debug2("Sending SSH2_FXP_REALPATH \"%s\"", path);
1.154 djm 998: send_string_request(conn, id, SSH2_FXP_REALPATH,
999: path, strlen(path));
1000: }
1.116 djm 1001: get_msg(conn, msg);
1002: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1003: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 djm 1004: fatal_fr(r, "parse");
1.1 djm 1005:
1006: if (id != expected_id)
1.33 deraadt 1007: fatal("ID mismatch (%u != %u)", id, expected_id);
1.1 djm 1008:
1009: if (type == SSH2_FXP_STATUS) {
1.116 djm 1010: u_int status;
1.160 djm 1011: char *errmsg;
1.1 djm 1012:
1.160 djm 1013: if ((r = sshbuf_get_u32(msg, &status)) != 0 ||
1014: (r = sshbuf_get_cstring(msg, &errmsg, NULL)) != 0)
1.137 djm 1015: fatal_fr(r, "parse status");
1.160 djm 1016: error("%s %s: %s", expand ? "expand" : "realpath",
1017: path, *errmsg == '\0' ? fx2txt(status) : errmsg);
1018: free(errmsg);
1.116 djm 1019: sshbuf_free(msg);
1.91 djm 1020: return NULL;
1.1 djm 1021: } else if (type != SSH2_FXP_NAME)
1.33 deraadt 1022: fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.1 djm 1023: SSH2_FXP_NAME, type);
1024:
1.116 djm 1025: if ((r = sshbuf_get_u32(msg, &count)) != 0)
1.137 djm 1026: fatal_fr(r, "parse count");
1.1 djm 1027: if (count != 1)
1.154 djm 1028: fatal("Got multiple names (%d) from %s", count, what);
1.1 djm 1029:
1.116 djm 1030: if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
1031: (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
1032: (r = decode_attrib(msg, &a)) != 0)
1.137 djm 1033: fatal_fr(r, "parse filename/attrib");
1.1 djm 1034:
1.154 djm 1035: debug3("%s %s -> %s", what, path, filename);
1.1 djm 1036:
1.98 djm 1037: free(longname);
1.1 djm 1038:
1.116 djm 1039: sshbuf_free(msg);
1.1 djm 1040:
1041: return(filename);
1.154 djm 1042: }
1043:
1044: char *
1045: do_realpath(struct sftp_conn *conn, const char *path)
1046: {
1047: return do_realpath_expand(conn, path, 0);
1048: }
1049:
1050: int
1051: can_expand_path(struct sftp_conn *conn)
1052: {
1053: return (conn->exts & SFTP_EXT_PATH_EXPAND) != 0;
1054: }
1055:
1056: char *
1057: do_expand_path(struct sftp_conn *conn, const char *path)
1058: {
1059: if (!can_expand_path(conn)) {
1060: debug3_f("no server support, fallback to realpath");
1061: return do_realpath_expand(conn, path, 0);
1062: }
1063: return do_realpath_expand(conn, path, 1);
1.162 djm 1064: }
1065:
1066: int
1067: do_copy(struct sftp_conn *conn, const char *oldpath, const char *newpath)
1068: {
1069: Attrib junk, *a;
1070: struct sshbuf *msg;
1071: u_char *old_handle, *new_handle;
1072: u_int mode, status, id;
1073: size_t old_handle_len, new_handle_len;
1074: int r;
1075:
1076: /* Return if the extension is not supported */
1077: if ((conn->exts & SFTP_EXT_COPY_DATA) == 0) {
1078: error("Server does not support copy-data extension");
1079: return -1;
1080: }
1081:
1082: /* Make sure the file exists, and we can copy its perms */
1083: if ((a = do_stat(conn, oldpath, 0)) == NULL)
1084: return -1;
1085:
1086: /* Do not preserve set[ug]id here, as we do not preserve ownership */
1087: if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
1088: mode = a->perm & 0777;
1089:
1090: if (!S_ISREG(a->perm)) {
1091: error("Cannot copy non-regular file: %s", oldpath);
1092: return -1;
1093: }
1094: } else {
1095: /* NB: The user's umask will apply to this */
1096: mode = 0666;
1097: }
1098:
1099: /* Set up the new perms for the new file */
1100: attrib_clear(a);
1101: a->perm = mode;
1102: a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1103:
1104: if ((msg = sshbuf_new()) == NULL)
1105: fatal("%s: sshbuf_new failed", __func__);
1106:
1107: attrib_clear(&junk); /* Send empty attributes */
1108:
1109: /* Open the old file for reading */
1110: id = conn->msg_id++;
1111: if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1112: (r = sshbuf_put_u32(msg, id)) != 0 ||
1113: (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
1114: (r = sshbuf_put_u32(msg, SSH2_FXF_READ)) != 0 ||
1115: (r = encode_attrib(msg, &junk)) != 0)
1116: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1117: send_msg(conn, msg);
1118: debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, oldpath);
1119:
1120: sshbuf_reset(msg);
1121:
1122: old_handle = get_handle(conn, id, &old_handle_len,
1123: "remote open(\"%s\")", oldpath);
1124: if (old_handle == NULL) {
1125: sshbuf_free(msg);
1126: return -1;
1127: }
1128:
1129: /* Open the new file for writing */
1130: id = conn->msg_id++;
1131: if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1132: (r = sshbuf_put_u32(msg, id)) != 0 ||
1133: (r = sshbuf_put_cstring(msg, newpath)) != 0 ||
1134: (r = sshbuf_put_u32(msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|
1135: SSH2_FXF_TRUNC)) != 0 ||
1136: (r = encode_attrib(msg, a)) != 0)
1137: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1138: send_msg(conn, msg);
1139: debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, newpath);
1140:
1141: sshbuf_reset(msg);
1142:
1143: new_handle = get_handle(conn, id, &new_handle_len,
1144: "remote open(\"%s\")", newpath);
1145: if (new_handle == NULL) {
1146: sshbuf_free(msg);
1147: free(old_handle);
1148: return -1;
1149: }
1150:
1151: /* Copy the file data */
1152: id = conn->msg_id++;
1153: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1154: (r = sshbuf_put_u32(msg, id)) != 0 ||
1155: (r = sshbuf_put_cstring(msg, "copy-data")) != 0 ||
1156: (r = sshbuf_put_string(msg, old_handle, old_handle_len)) != 0 ||
1157: (r = sshbuf_put_u64(msg, 0)) != 0 ||
1158: (r = sshbuf_put_u64(msg, 0)) != 0 ||
1159: (r = sshbuf_put_string(msg, new_handle, new_handle_len)) != 0 ||
1160: (r = sshbuf_put_u64(msg, 0)) != 0)
1161: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1162: send_msg(conn, msg);
1163: debug3("Sent message copy-data \"%s\" 0 0 -> \"%s\" 0",
1164: oldpath, newpath);
1165:
1166: status = get_status(conn, id);
1167: if (status != SSH2_FX_OK)
1168: error("Couldn't copy file \"%s\" to \"%s\": %s", oldpath,
1169: newpath, fx2txt(status));
1170:
1171: /* Clean up everything */
1172: sshbuf_free(msg);
1173: do_close(conn, old_handle, old_handle_len);
1174: do_close(conn, new_handle, new_handle_len);
1175: free(old_handle);
1176: free(new_handle);
1177:
1178: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 1179: }
1180:
1181: int
1.116 djm 1182: do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath,
1.102 djm 1183: int force_legacy)
1.1 djm 1184: {
1.116 djm 1185: struct sshbuf *msg;
1.1 djm 1186: u_int status, id;
1.116 djm 1187: int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
1.1 djm 1188:
1.116 djm 1189: if ((msg = sshbuf_new()) == NULL)
1.137 djm 1190: fatal_f("sshbuf_new failed");
1.1 djm 1191:
1192: /* Send rename request */
1.23 djm 1193: id = conn->msg_id++;
1.102 djm 1194: if (use_ext) {
1.161 djm 1195: debug2("Sending SSH2_FXP_EXTENDED(posix-rename@openssh.com) "
1196: "\"%s\" to \"%s\"", oldpath, newpath);
1.116 djm 1197: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1198: (r = sshbuf_put_u32(msg, id)) != 0 ||
1199: (r = sshbuf_put_cstring(msg,
1200: "posix-rename@openssh.com")) != 0)
1.137 djm 1201: fatal_fr(r, "compose posix-rename");
1.81 djm 1202: } else {
1.161 djm 1203: debug2("Sending SSH2_FXP_RENAME \"%s\" to \"%s\"",
1204: oldpath, newpath);
1.116 djm 1205: if ((r = sshbuf_put_u8(msg, SSH2_FXP_RENAME)) != 0 ||
1206: (r = sshbuf_put_u32(msg, id)) != 0)
1.137 djm 1207: fatal_fr(r, "compose rename");
1.116 djm 1208: }
1209: if ((r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
1210: (r = sshbuf_put_cstring(msg, newpath)) != 0)
1.137 djm 1211: fatal_fr(r, "compose paths");
1.116 djm 1212: send_msg(conn, msg);
1.81 djm 1213: debug3("Sent message %s \"%s\" -> \"%s\"",
1.116 djm 1214: use_ext ? "posix-rename@openssh.com" :
1215: "SSH2_FXP_RENAME", oldpath, newpath);
1216: sshbuf_free(msg);
1.1 djm 1217:
1.93 djm 1218: status = get_status(conn, id);
1.1 djm 1219: if (status != SSH2_FX_OK)
1.161 djm 1220: error("remote rename \"%s\" to \"%s\": %s", oldpath,
1.94 djm 1221: newpath, fx2txt(status));
1222:
1.116 djm 1223: return status == SSH2_FX_OK ? 0 : -1;
1.94 djm 1224: }
1225:
1226: int
1.116 djm 1227: do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
1.94 djm 1228: {
1.116 djm 1229: struct sshbuf *msg;
1.94 djm 1230: u_int status, id;
1.116 djm 1231: int r;
1.94 djm 1232:
1233: if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
1234: error("Server does not support hardlink@openssh.com extension");
1235: return -1;
1236: }
1.161 djm 1237: debug2("Sending SSH2_FXP_EXTENDED(hardlink@openssh.com) "
1238: "\"%s\" to \"%s\"", oldpath, newpath);
1.94 djm 1239:
1.116 djm 1240: if ((msg = sshbuf_new()) == NULL)
1.137 djm 1241: fatal_f("sshbuf_new failed");
1.95 markus 1242:
1243: /* Send link request */
1244: id = conn->msg_id++;
1.116 djm 1245: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1246: (r = sshbuf_put_u32(msg, id)) != 0 ||
1247: (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
1248: (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
1249: (r = sshbuf_put_cstring(msg, newpath)) != 0)
1.137 djm 1250: fatal_fr(r, "compose");
1.116 djm 1251: send_msg(conn, msg);
1.94 djm 1252: debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
1.142 djm 1253: oldpath, newpath);
1.116 djm 1254: sshbuf_free(msg);
1.94 djm 1255:
1256: status = get_status(conn, id);
1257: if (status != SSH2_FX_OK)
1.161 djm 1258: error("remote link \"%s\" to \"%s\": %s", oldpath,
1.23 djm 1259: newpath, fx2txt(status));
1.1 djm 1260:
1.116 djm 1261: return status == SSH2_FX_OK ? 0 : -1;
1.11 djm 1262: }
1263:
1264: int
1.116 djm 1265: do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
1.11 djm 1266: {
1.116 djm 1267: struct sshbuf *msg;
1.11 djm 1268: u_int status, id;
1.116 djm 1269: int r;
1.11 djm 1270:
1.23 djm 1271: if (conn->version < 3) {
1272: error("This server does not support the symlink operation");
1273: return(SSH2_FX_OP_UNSUPPORTED);
1274: }
1.161 djm 1275: debug2("Sending SSH2_FXP_SYMLINK \"%s\" to \"%s\"", oldpath, newpath);
1.23 djm 1276:
1.116 djm 1277: if ((msg = sshbuf_new()) == NULL)
1.137 djm 1278: fatal_f("sshbuf_new failed");
1.11 djm 1279:
1.48 djm 1280: /* Send symlink request */
1.23 djm 1281: id = conn->msg_id++;
1.116 djm 1282: if ((r = sshbuf_put_u8(msg, SSH2_FXP_SYMLINK)) != 0 ||
1283: (r = sshbuf_put_u32(msg, id)) != 0 ||
1284: (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
1285: (r = sshbuf_put_cstring(msg, newpath)) != 0)
1.137 djm 1286: fatal_fr(r, "compose");
1.116 djm 1287: send_msg(conn, msg);
1.11 djm 1288: debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
1289: newpath);
1.116 djm 1290: sshbuf_free(msg);
1.11 djm 1291:
1.93 djm 1292: status = get_status(conn, id);
1.11 djm 1293: if (status != SSH2_FX_OK)
1.161 djm 1294: error("remote symlink file \"%s\" to \"%s\": %s", oldpath,
1.23 djm 1295: newpath, fx2txt(status));
1.11 djm 1296:
1.116 djm 1297: return status == SSH2_FX_OK ? 0 : -1;
1.11 djm 1298: }
1299:
1.107 djm 1300: int
1.116 djm 1301: do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len)
1.107 djm 1302: {
1.116 djm 1303: struct sshbuf *msg;
1.107 djm 1304: u_int status, id;
1.116 djm 1305: int r;
1.107 djm 1306:
1307: /* Silently return if the extension is not supported */
1308: if ((conn->exts & SFTP_EXT_FSYNC) == 0)
1309: return -1;
1.161 djm 1310: debug2("Sending SSH2_FXP_EXTENDED(fsync@openssh.com)");
1.107 djm 1311:
1312: /* Send fsync request */
1.116 djm 1313: if ((msg = sshbuf_new()) == NULL)
1.137 djm 1314: fatal_f("sshbuf_new failed");
1.107 djm 1315: id = conn->msg_id++;
1.116 djm 1316: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1317: (r = sshbuf_put_u32(msg, id)) != 0 ||
1318: (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
1319: (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137 djm 1320: fatal_fr(r, "compose");
1.116 djm 1321: send_msg(conn, msg);
1.107 djm 1322: debug3("Sent message fsync@openssh.com I:%u", id);
1.116 djm 1323: sshbuf_free(msg);
1.107 djm 1324:
1325: status = get_status(conn, id);
1326: if (status != SSH2_FX_OK)
1.161 djm 1327: error("remote fsync: %s", fx2txt(status));
1.107 djm 1328:
1.129 djm 1329: return status == SSH2_FX_OK ? 0 : -1;
1.107 djm 1330: }
1331:
1.78 chl 1332: #ifdef notyet
1.11 djm 1333: char *
1.116 djm 1334: do_readlink(struct sftp_conn *conn, const char *path)
1.11 djm 1335: {
1.116 djm 1336: struct sshbuf *msg;
1337: u_int expected_id, count, id;
1.11 djm 1338: char *filename, *longname;
1.116 djm 1339: Attrib a;
1340: u_char type;
1341: int r;
1.11 djm 1342:
1.161 djm 1343: debug2("Sending SSH2_FXP_READLINK \"%s\"", path);
1344:
1.23 djm 1345: expected_id = id = conn->msg_id++;
1.93 djm 1346: send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
1.11 djm 1347:
1.116 djm 1348: if ((msg = sshbuf_new()) == NULL)
1.137 djm 1349: fatal_f("sshbuf_new failed");
1.11 djm 1350:
1.116 djm 1351: get_msg(conn, msg);
1352: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1353: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 djm 1354: fatal_fr(r, "parse");
1.11 djm 1355:
1356: if (id != expected_id)
1.33 deraadt 1357: fatal("ID mismatch (%u != %u)", id, expected_id);
1.11 djm 1358:
1359: if (type == SSH2_FXP_STATUS) {
1.116 djm 1360: u_int status;
1.11 djm 1361:
1.116 djm 1362: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 djm 1363: fatal_fr(r, "parse status");
1.11 djm 1364: error("Couldn't readlink: %s", fx2txt(status));
1.116 djm 1365: sshbuf_free(msg);
1.11 djm 1366: return(NULL);
1367: } else if (type != SSH2_FXP_NAME)
1.33 deraadt 1368: fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.11 djm 1369: SSH2_FXP_NAME, type);
1370:
1.116 djm 1371: if ((r = sshbuf_get_u32(msg, &count)) != 0)
1.137 djm 1372: fatal_fr(r, "parse count");
1.11 djm 1373: if (count != 1)
1374: fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
1375:
1.116 djm 1376: if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
1377: (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
1378: (r = decode_attrib(msg, &a)) != 0)
1.137 djm 1379: fatal_fr(r, "parse filenames/attrib");
1.11 djm 1380:
1381: debug3("SSH_FXP_READLINK %s -> %s", path, filename);
1382:
1.98 djm 1383: free(longname);
1.11 djm 1384:
1.116 djm 1385: sshbuf_free(msg);
1.11 djm 1386:
1.116 djm 1387: return filename;
1.82 djm 1388: }
1389: #endif
1390:
1391: int
1.84 dtucker 1392: do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
1.82 djm 1393: int quiet)
1394: {
1.116 djm 1395: struct sshbuf *msg;
1.82 djm 1396: u_int id;
1.116 djm 1397: int r;
1.82 djm 1398:
1399: if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
1400: error("Server does not support statvfs@openssh.com extension");
1401: return -1;
1402: }
1403:
1.161 djm 1404: debug2("Sending SSH2_FXP_EXTENDED(statvfs@openssh.com) \"%s\"", path);
1405:
1.82 djm 1406: id = conn->msg_id++;
1407:
1.116 djm 1408: if ((msg = sshbuf_new()) == NULL)
1.137 djm 1409: fatal_f("sshbuf_new failed");
1.116 djm 1410: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1411: (r = sshbuf_put_u32(msg, id)) != 0 ||
1412: (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
1413: (r = sshbuf_put_cstring(msg, path)) != 0)
1.137 djm 1414: fatal_fr(r, "compose");
1.116 djm 1415: send_msg(conn, msg);
1416: sshbuf_free(msg);
1.82 djm 1417:
1.93 djm 1418: return get_decode_statvfs(conn, st, id, quiet);
1.82 djm 1419: }
1420:
1421: #ifdef notyet
1422: int
1.116 djm 1423: do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
1.84 dtucker 1424: struct sftp_statvfs *st, int quiet)
1.82 djm 1425: {
1.116 djm 1426: struct sshbuf *msg;
1.82 djm 1427: u_int id;
1428:
1429: if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
1430: error("Server does not support fstatvfs@openssh.com extension");
1431: return -1;
1432: }
1433:
1.161 djm 1434: debug2("Sending SSH2_FXP_EXTENDED(fstatvfs@openssh.com)");
1435:
1.82 djm 1436: id = conn->msg_id++;
1437:
1.116 djm 1438: if ((msg = sshbuf_new()) == NULL)
1.137 djm 1439: fatal_f("sshbuf_new failed");
1.116 djm 1440: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1441: (r = sshbuf_put_u32(msg, id)) != 0 ||
1442: (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
1443: (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137 djm 1444: fatal_fr(r, "compose");
1.116 djm 1445: send_msg(conn, msg);
1446: sshbuf_free(msg);
1.82 djm 1447:
1.93 djm 1448: return get_decode_statvfs(conn, st, id, quiet);
1.1 djm 1449: }
1.78 chl 1450: #endif
1.1 djm 1451:
1.131 djm 1452: int
1453: do_lsetstat(struct sftp_conn *conn, const char *path, Attrib *a)
1454: {
1455: struct sshbuf *msg;
1456: u_int status, id;
1457: int r;
1458:
1459: if ((conn->exts & SFTP_EXT_LSETSTAT) == 0) {
1460: error("Server does not support lsetstat@openssh.com extension");
1461: return -1;
1462: }
1463:
1.161 djm 1464: debug2("Sending SSH2_FXP_EXTENDED(lsetstat@openssh.com) \"%s\"", path);
1465:
1.131 djm 1466: id = conn->msg_id++;
1467: if ((msg = sshbuf_new()) == NULL)
1.137 djm 1468: fatal_f("sshbuf_new failed");
1.131 djm 1469: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1470: (r = sshbuf_put_u32(msg, id)) != 0 ||
1471: (r = sshbuf_put_cstring(msg, "lsetstat@openssh.com")) != 0 ||
1472: (r = sshbuf_put_cstring(msg, path)) != 0 ||
1473: (r = encode_attrib(msg, a)) != 0)
1.137 djm 1474: fatal_fr(r, "compose");
1.131 djm 1475: send_msg(conn, msg);
1476: sshbuf_free(msg);
1477:
1478: status = get_status(conn, id);
1479: if (status != SSH2_FX_OK)
1.161 djm 1480: error("remote lsetstat \"%s\": %s", path, fx2txt(status));
1.131 djm 1481:
1482: return status == SSH2_FX_OK ? 0 : -1;
1483: }
1484:
1.21 djm 1485: static void
1.93 djm 1486: send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
1.116 djm 1487: u_int len, const u_char *handle, u_int handle_len)
1.21 djm 1488: {
1.116 djm 1489: struct sshbuf *msg;
1490: int r;
1.28 markus 1491:
1.116 djm 1492: if ((msg = sshbuf_new()) == NULL)
1.137 djm 1493: fatal_f("sshbuf_new failed");
1.116 djm 1494: if ((r = sshbuf_put_u8(msg, SSH2_FXP_READ)) != 0 ||
1495: (r = sshbuf_put_u32(msg, id)) != 0 ||
1496: (r = sshbuf_put_string(msg, handle, handle_len)) != 0 ||
1497: (r = sshbuf_put_u64(msg, offset)) != 0 ||
1498: (r = sshbuf_put_u32(msg, len)) != 0)
1.137 djm 1499: fatal_fr(r, "compose");
1.116 djm 1500: send_msg(conn, msg);
1501: sshbuf_free(msg);
1.28 markus 1502: }
1.21 djm 1503:
1.144 djm 1504: static int
1505: send_open(struct sftp_conn *conn, const char *path, const char *tag,
1506: u_int openmode, Attrib *a, u_char **handlep, size_t *handle_lenp)
1507: {
1508: Attrib junk;
1509: u_char *handle;
1510: size_t handle_len;
1511: struct sshbuf *msg;
1512: int r;
1513: u_int id;
1514:
1.161 djm 1515: debug2("Sending SSH2_FXP_OPEN \"%s\"", path);
1516:
1.144 djm 1517: *handlep = NULL;
1518: *handle_lenp = 0;
1519:
1520: if (a == NULL) {
1521: attrib_clear(&junk); /* Send empty attributes */
1522: a = &junk;
1523: }
1524: /* Send open request */
1525: if ((msg = sshbuf_new()) == NULL)
1526: fatal_f("sshbuf_new failed");
1527: id = conn->msg_id++;
1528: if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1529: (r = sshbuf_put_u32(msg, id)) != 0 ||
1530: (r = sshbuf_put_cstring(msg, path)) != 0 ||
1531: (r = sshbuf_put_u32(msg, openmode)) != 0 ||
1532: (r = encode_attrib(msg, a)) != 0)
1533: fatal_fr(r, "compose %s open", tag);
1534: send_msg(conn, msg);
1535: sshbuf_free(msg);
1536: debug3("Sent %s message SSH2_FXP_OPEN I:%u P:%s M:0x%04x",
1537: tag, id, path, openmode);
1538: if ((handle = get_handle(conn, id, &handle_len,
1.159 djm 1539: "%s open \"%s\"", tag, path)) == NULL)
1.144 djm 1540: return -1;
1541: /* success */
1542: *handlep = handle;
1543: *handle_lenp = handle_len;
1544: return 0;
1545: }
1546:
1.153 djm 1547: static const char *
1548: progress_meter_path(const char *path)
1549: {
1550: const char *progresspath;
1551:
1552: if ((progresspath = strrchr(path, '/')) == NULL)
1553: return path;
1554: progresspath++;
1555: if (*progresspath == '\0')
1556: return path;
1557: return progresspath;
1558: }
1559:
1.1 djm 1560: int
1.116 djm 1561: do_download(struct sftp_conn *conn, const char *remote_path,
1562: const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
1.163 ! djm 1563: int fsync_flag, int inplace_flag)
1.1 djm 1564: {
1.116 djm 1565: struct sshbuf *msg;
1566: u_char *handle;
1567: int local_fd = -1, write_error;
1.134 djm 1568: int read_error, write_errno, lmodified = 0, reordered = 0, r;
1.101 djm 1569: u_int64_t offset = 0, size, highwater;
1.116 djm 1570: u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
1.39 fgsch 1571: off_t progress_counter;
1.116 djm 1572: size_t handle_len;
1.101 djm 1573: struct stat st;
1.146 djm 1574: struct requests requests;
1.21 djm 1575: struct request *req;
1.116 djm 1576: u_char type;
1.21 djm 1577:
1.161 djm 1578: debug2_f("download remote \"%s\" to local \"%s\"",
1579: remote_path, local_path);
1580:
1.21 djm 1581: TAILQ_INIT(&requests);
1.1 djm 1582:
1.89 djm 1583: if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1584: return -1;
1.1 djm 1585:
1.86 djm 1586: /* Do not preserve set[ug]id here, as we do not preserve ownership */
1.1 djm 1587: if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1.38 djm 1588: mode = a->perm & 0777;
1.1 djm 1589: else
1590: mode = 0666;
1591:
1.14 djm 1592: if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1.41 djm 1593: (!S_ISREG(a->perm))) {
1.161 djm 1594: error("download %s: not a regular file", remote_path);
1.14 djm 1595: return(-1);
1596: }
1597:
1.21 djm 1598: if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1599: size = a->size;
1600: else
1601: size = 0;
1602:
1.141 djm 1603: buflen = conn->download_buflen;
1.1 djm 1604:
1605: /* Send open request */
1.144 djm 1606: if (send_open(conn, remote_path, "remote", SSH2_FXF_READ, NULL,
1607: &handle, &handle_len) != 0)
1608: return -1;
1.1 djm 1609:
1.163 ! djm 1610: local_fd = open(local_path, O_WRONLY | O_CREAT |
! 1611: ((resume_flag || inplace_flag) ? 0 : O_TRUNC), mode | S_IWUSR);
1.23 djm 1612: if (local_fd == -1) {
1.161 djm 1613: error("open local \"%s\": %s", local_path, strerror(errno));
1.101 djm 1614: goto fail;
1615: }
1616: offset = highwater = 0;
1.105 djm 1617: if (resume_flag) {
1.101 djm 1618: if (fstat(local_fd, &st) == -1) {
1.161 djm 1619: error("stat local \"%s\": %s",
1.101 djm 1620: local_path, strerror(errno));
1621: goto fail;
1622: }
1.113 djm 1623: if (st.st_size < 0) {
1624: error("\"%s\" has negative size", local_path);
1625: goto fail;
1626: }
1627: if ((u_int64_t)st.st_size > size) {
1.101 djm 1628: error("Unable to resume download of \"%s\": "
1629: "local file is larger than remote", local_path);
1630: fail:
1631: do_close(conn, handle, handle_len);
1632: free(handle);
1.110 djm 1633: if (local_fd != -1)
1634: close(local_fd);
1.101 djm 1635: return -1;
1636: }
1637: offset = highwater = st.st_size;
1.23 djm 1638: }
1639:
1.1 djm 1640: /* Read from remote and write to local */
1.101 djm 1641: write_error = read_error = write_errno = num_req = 0;
1.21 djm 1642: max_req = 1;
1.101 djm 1643: progress_counter = offset;
1.39 fgsch 1644:
1.153 djm 1645: if (showprogress && size != 0) {
1646: start_progress_meter(progress_meter_path(remote_path),
1647: size, &progress_counter);
1648: }
1.39 fgsch 1649:
1.144 djm 1650: if ((msg = sshbuf_new()) == NULL)
1651: fatal_f("sshbuf_new failed");
1652:
1.21 djm 1653: while (num_req > 0 || max_req > 0) {
1.116 djm 1654: u_char *data;
1655: size_t len;
1.1 djm 1656:
1.49 djm 1657: /*
1.51 deraadt 1658: * Simulate EOF on interrupt: stop sending new requests and
1.49 djm 1659: * allow outstanding requests to drain gracefully
1660: */
1661: if (interrupted) {
1662: if (num_req == 0) /* If we haven't started yet... */
1663: break;
1664: max_req = 0;
1665: }
1666:
1.21 djm 1667: /* Send some more requests */
1668: while (num_req < max_req) {
1.28 markus 1669: debug3("Request range %llu -> %llu (%d/%d)",
1.25 itojun 1670: (unsigned long long)offset,
1671: (unsigned long long)offset + buflen - 1,
1672: num_req, max_req);
1.151 djm 1673: req = request_enqueue(&requests, conn->msg_id++,
1674: buflen, offset);
1.21 djm 1675: offset += buflen;
1676: num_req++;
1.93 djm 1677: send_read_request(conn, req->id, req->offset,
1.21 djm 1678: req->len, handle, handle_len);
1679: }
1.1 djm 1680:
1.116 djm 1681: sshbuf_reset(msg);
1682: get_msg(conn, msg);
1683: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1684: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 djm 1685: fatal_fr(r, "parse");
1.33 deraadt 1686: debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1.21 djm 1687:
1688: /* Find the request in our queue */
1.151 djm 1689: if ((req = request_find(&requests, id)) == NULL)
1.21 djm 1690: fatal("Unexpected reply %u", id);
1691:
1692: switch (type) {
1693: case SSH2_FXP_STATUS:
1.116 djm 1694: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 djm 1695: fatal_fr(r, "parse status");
1.21 djm 1696: if (status != SSH2_FX_EOF)
1697: read_error = 1;
1698: max_req = 0;
1699: TAILQ_REMOVE(&requests, req, tq);
1.98 djm 1700: free(req);
1.21 djm 1701: num_req--;
1702: break;
1703: case SSH2_FXP_DATA:
1.116 djm 1704: if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1.137 djm 1705: fatal_fr(r, "parse data");
1.26 itojun 1706: debug3("Received data %llu -> %llu",
1.28 markus 1707: (unsigned long long)req->offset,
1.26 itojun 1708: (unsigned long long)req->offset + len - 1);
1.21 djm 1709: if (len > req->len)
1710: fatal("Received more data than asked for "
1.116 djm 1711: "%zu > %zu", len, req->len);
1.134 djm 1712: lmodified = 1;
1.21 djm 1713: if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1.44 deraadt 1714: atomicio(vwrite, local_fd, data, len) != len) &&
1.21 djm 1715: !write_error) {
1716: write_errno = errno;
1717: write_error = 1;
1718: max_req = 0;
1719: }
1.101 djm 1720: else if (!reordered && req->offset <= highwater)
1721: highwater = req->offset + len;
1722: else if (!reordered && req->offset > highwater)
1723: reordered = 1;
1.39 fgsch 1724: progress_counter += len;
1.98 djm 1725: free(data);
1.1 djm 1726:
1.21 djm 1727: if (len == req->len) {
1728: TAILQ_REMOVE(&requests, req, tq);
1.98 djm 1729: free(req);
1.21 djm 1730: num_req--;
1731: } else {
1732: /* Resend the request for the missing data */
1733: debug3("Short data block, re-requesting "
1.26 itojun 1734: "%llu -> %llu (%2d)",
1.28 markus 1735: (unsigned long long)req->offset + len,
1.27 itojun 1736: (unsigned long long)req->offset +
1737: req->len - 1, num_req);
1.23 djm 1738: req->id = conn->msg_id++;
1.21 djm 1739: req->len -= len;
1740: req->offset += len;
1.93 djm 1741: send_read_request(conn, req->id,
1.23 djm 1742: req->offset, req->len, handle, handle_len);
1.21 djm 1743: /* Reduce the request size */
1744: if (len < buflen)
1.125 deraadt 1745: buflen = MAXIMUM(MIN_READ_SIZE, len);
1.21 djm 1746: }
1747: if (max_req > 0) { /* max_req = 0 iff EOF received */
1748: if (size > 0 && offset > size) {
1749: /* Only one request at a time
1750: * after the expected EOF */
1751: debug3("Finish at %llu (%2d)",
1.26 itojun 1752: (unsigned long long)offset,
1753: num_req);
1.21 djm 1754: max_req = 1;
1.136 djm 1755: } else if (max_req < conn->num_requests) {
1.21 djm 1756: ++max_req;
1757: }
1.1 djm 1758: }
1.21 djm 1759: break;
1760: default:
1.33 deraadt 1761: fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1.1 djm 1762: SSH2_FXP_DATA, type);
1763: }
1.21 djm 1764: }
1.1 djm 1765:
1.39 fgsch 1766: if (showprogress && size)
1767: stop_progress_meter();
1768:
1.21 djm 1769: /* Sanity check */
1770: if (TAILQ_FIRST(&requests) != NULL)
1771: fatal("Transfer complete, but requests still in queue");
1.101 djm 1772: /* Truncate at highest contiguous point to avoid holes on interrupt */
1773: if (read_error || write_error || interrupted) {
1.105 djm 1774: if (reordered && resume_flag) {
1.101 djm 1775: error("Unable to resume download of \"%s\": "
1776: "server reordered requests", local_path);
1777: }
1778: debug("truncating at %llu", (unsigned long long)highwater);
1.120 djm 1779: if (ftruncate(local_fd, highwater) == -1)
1.161 djm 1780: error("local ftruncate \"%s\": %s", local_path,
1.120 djm 1781: strerror(errno));
1.101 djm 1782: }
1.21 djm 1783: if (read_error) {
1.161 djm 1784: error("read remote \"%s\" : %s", remote_path, fx2txt(status));
1.103 djm 1785: status = -1;
1.23 djm 1786: do_close(conn, handle, handle_len);
1.21 djm 1787: } else if (write_error) {
1.161 djm 1788: error("write local \"%s\": %s", local_path,
1.21 djm 1789: strerror(write_errno));
1.116 djm 1790: status = SSH2_FX_FAILURE;
1.23 djm 1791: do_close(conn, handle, handle_len);
1.21 djm 1792: } else {
1.116 djm 1793: if (do_close(conn, handle, handle_len) != 0 || interrupted)
1794: status = SSH2_FX_FAILURE;
1795: else
1796: status = SSH2_FX_OK;
1.21 djm 1797: /* Override umask and utimes if asked */
1.105 djm 1798: if (preserve_flag && fchmod(local_fd, mode) == -1)
1.161 djm 1799: error("local chmod \"%s\": %s", local_path,
1.37 deraadt 1800: strerror(errno));
1.105 djm 1801: if (preserve_flag &&
1802: (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1.21 djm 1803: struct timeval tv[2];
1804: tv[0].tv_sec = a->atime;
1805: tv[1].tv_sec = a->mtime;
1806: tv[0].tv_usec = tv[1].tv_usec = 0;
1807: if (utimes(local_path, tv) == -1)
1.161 djm 1808: error("local set times \"%s\": %s",
1.37 deraadt 1809: local_path, strerror(errno));
1.1 djm 1810: }
1.134 djm 1811: if (resume_flag && !lmodified)
1812: logit("File \"%s\" was not modified", local_path);
1813: else if (fsync_flag) {
1.107 djm 1814: debug("syncing \"%s\"", local_path);
1815: if (fsync(local_fd) == -1)
1.161 djm 1816: error("local sync \"%s\": %s",
1.107 djm 1817: local_path, strerror(errno));
1818: }
1.10 djm 1819: }
1.5 djm 1820: close(local_fd);
1.116 djm 1821: sshbuf_free(msg);
1.98 djm 1822: free(handle);
1.23 djm 1823:
1.129 djm 1824: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 1825: }
1826:
1.89 djm 1827: static int
1.116 djm 1828: download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1829: int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
1.163 ! djm 1830: int resume_flag, int fsync_flag, int follow_link_flag, int inplace_flag)
1.89 djm 1831: {
1832: int i, ret = 0;
1833: SFTP_DIRENT **dir_entries;
1.130 djm 1834: char *filename, *new_src = NULL, *new_dst = NULL;
1.138 dtucker 1835: mode_t mode = 0777, tmpmode = mode;
1.89 djm 1836:
1837: if (depth >= MAX_DIR_DEPTH) {
1838: error("Maximum directory depth exceeded: %d levels", depth);
1839: return -1;
1840: }
1841:
1.161 djm 1842: debug2_f("download dir remote \"%s\" to local \"%s\"", src, dst);
1843:
1.89 djm 1844: if (dirattrib == NULL &&
1845: (dirattrib = do_stat(conn, src, 1)) == NULL) {
1.161 djm 1846: error("stat remote \"%s\" directory failed", src);
1.89 djm 1847: return -1;
1848: }
1849: if (!S_ISDIR(dirattrib->perm)) {
1850: error("\"%s\" is not a directory", src);
1851: return -1;
1852: }
1.147 djm 1853: if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
1.124 schwarze 1854: mprintf("Retrieving %s\n", src);
1.89 djm 1855:
1.138 dtucker 1856: if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
1.89 djm 1857: mode = dirattrib->perm & 01777;
1.138 dtucker 1858: tmpmode = mode | (S_IWUSR|S_IXUSR);
1859: } else {
1.161 djm 1860: debug("download remote \"%s\": server "
1861: "did not send permissions", dst);
1.89 djm 1862: }
1863:
1.138 dtucker 1864: if (mkdir(dst, tmpmode) == -1 && errno != EEXIST) {
1.89 djm 1865: error("mkdir %s: %s", dst, strerror(errno));
1866: return -1;
1867: }
1868:
1869: if (do_readdir(conn, src, &dir_entries) == -1) {
1.161 djm 1870: error("remote readdir \"%s\" failed", src);
1.89 djm 1871: return -1;
1872: }
1873:
1874: for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1.130 djm 1875: free(new_dst);
1876: free(new_src);
1877:
1.89 djm 1878: filename = dir_entries[i]->filename;
1879: new_dst = path_append(dst, filename);
1880: new_src = path_append(src, filename);
1881:
1882: if (S_ISDIR(dir_entries[i]->a.perm)) {
1883: if (strcmp(filename, ".") == 0 ||
1884: strcmp(filename, "..") == 0)
1885: continue;
1886: if (download_dir_internal(conn, new_src, new_dst,
1.105 djm 1887: depth + 1, &(dir_entries[i]->a), preserve_flag,
1.150 djm 1888: print_flag, resume_flag,
1.163 ! djm 1889: fsync_flag, follow_link_flag, inplace_flag) == -1)
1.89 djm 1890: ret = -1;
1.150 djm 1891: } else if (S_ISREG(dir_entries[i]->a.perm) ||
1892: (follow_link_flag && S_ISLNK(dir_entries[i]->a.perm))) {
1893: /*
1894: * If this is a symlink then don't send the link's
1895: * Attrib. do_download() will do a FXP_STAT operation
1896: * and get the link target's attributes.
1897: */
1.89 djm 1898: if (do_download(conn, new_src, new_dst,
1.150 djm 1899: S_ISLNK(dir_entries[i]->a.perm) ? NULL :
1900: &(dir_entries[i]->a),
1.163 ! djm 1901: preserve_flag, resume_flag, fsync_flag,
! 1902: inplace_flag) == -1) {
1.89 djm 1903: error("Download of file %s to %s failed",
1904: new_src, new_dst);
1905: ret = -1;
1906: }
1907: } else
1.161 djm 1908: logit("download \"%s\": not a regular file", new_src);
1.89 djm 1909:
1910: }
1.130 djm 1911: free(new_dst);
1912: free(new_src);
1.89 djm 1913:
1.105 djm 1914: if (preserve_flag) {
1.89 djm 1915: if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1916: struct timeval tv[2];
1917: tv[0].tv_sec = dirattrib->atime;
1918: tv[1].tv_sec = dirattrib->mtime;
1919: tv[0].tv_usec = tv[1].tv_usec = 0;
1920: if (utimes(dst, tv) == -1)
1.161 djm 1921: error("local set times on \"%s\": %s",
1.89 djm 1922: dst, strerror(errno));
1923: } else
1924: debug("Server did not send times for directory "
1925: "\"%s\"", dst);
1926: }
1927:
1.138 dtucker 1928: if (mode != tmpmode && chmod(dst, mode) == -1)
1.161 djm 1929: error("local chmod directory \"%s\": %s", dst,
1.138 dtucker 1930: strerror(errno));
1931:
1.89 djm 1932: free_sftp_dirents(dir_entries);
1933:
1934: return ret;
1935: }
1936:
1937: int
1.116 djm 1938: download_dir(struct sftp_conn *conn, const char *src, const char *dst,
1939: Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1.163 ! djm 1940: int fsync_flag, int follow_link_flag, int inplace_flag)
1.89 djm 1941: {
1942: char *src_canon;
1943: int ret;
1944:
1945: if ((src_canon = do_realpath(conn, src)) == NULL) {
1.161 djm 1946: error("download \"%s\": path canonicalization failed", src);
1.89 djm 1947: return -1;
1948: }
1949:
1.105 djm 1950: ret = download_dir_internal(conn, src_canon, dst, 0,
1.150 djm 1951: dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag,
1.163 ! djm 1952: follow_link_flag, inplace_flag);
1.98 djm 1953: free(src_canon);
1.89 djm 1954: return ret;
1955: }
1956:
1.1 djm 1957: int
1.116 djm 1958: do_upload(struct sftp_conn *conn, const char *local_path,
1.163 ! djm 1959: const char *remote_path, int preserve_flag, int resume,
! 1960: int fsync_flag, int inplace_flag)
1.1 djm 1961: {
1.116 djm 1962: int r, local_fd;
1.163 ! djm 1963: u_int openmode, id, status = SSH2_FX_OK, reordered = 0;
1.100 dtucker 1964: off_t offset, progress_counter;
1.163 ! djm 1965: u_char type, *handle, *data;
1.116 djm 1966: struct sshbuf *msg;
1.1 djm 1967: struct stat sb;
1.163 ! djm 1968: Attrib a, t, *c = NULL;
! 1969: u_int32_t startid, ackid;
! 1970: u_int64_t highwater = 0;
1.151 djm 1971: struct request *ack = NULL;
1972: struct requests acks;
1.116 djm 1973: size_t handle_len;
1.22 djm 1974:
1.161 djm 1975: debug2_f("upload local \"%s\" to remote \"%s\"",
1976: local_path, remote_path);
1977:
1.22 djm 1978: TAILQ_INIT(&acks);
1.1 djm 1979:
1.156 deraadt 1980: if ((local_fd = open(local_path, O_RDONLY)) == -1) {
1.161 djm 1981: error("open local \"%s\": %s", local_path, strerror(errno));
1.1 djm 1982: return(-1);
1983: }
1984: if (fstat(local_fd, &sb) == -1) {
1.161 djm 1985: error("fstat local \"%s\": %s", local_path, strerror(errno));
1.41 djm 1986: close(local_fd);
1987: return(-1);
1988: }
1989: if (!S_ISREG(sb.st_mode)) {
1.161 djm 1990: error("local \"%s\" is not a regular file", local_path);
1.1 djm 1991: close(local_fd);
1992: return(-1);
1993: }
1994: stat_to_attrib(&sb, &a);
1995:
1996: a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1997: a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1998: a.perm &= 0777;
1.105 djm 1999: if (!preserve_flag)
1.1 djm 2000: a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
2001:
1.115 logan 2002: if (resume) {
2003: /* Get remote file size if it exists */
2004: if ((c = do_stat(conn, remote_path, 0)) == NULL) {
1.122 djm 2005: close(local_fd);
1.115 logan 2006: return -1;
2007: }
2008:
2009: if ((off_t)c->size >= sb.st_size) {
1.161 djm 2010: error("resume \"%s\": destination file "
2011: "same size or larger", local_path);
1.115 logan 2012: close(local_fd);
2013: return -1;
2014: }
2015:
2016: if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
2017: close(local_fd);
2018: return -1;
2019: }
2020: }
2021:
1.163 ! djm 2022: openmode = SSH2_FXF_WRITE|SSH2_FXF_CREAT;
! 2023: if (resume)
! 2024: openmode |= SSH2_FXF_APPEND;
! 2025: else if (!inplace_flag)
! 2026: openmode |= SSH2_FXF_TRUNC;
! 2027:
1.1 djm 2028: /* Send open request */
1.163 ! djm 2029: if (send_open(conn, remote_path, "dest", openmode, &a,
! 2030: &handle, &handle_len) != 0) {
1.1 djm 2031: close(local_fd);
1.80 djm 2032: return -1;
1.1 djm 2033: }
2034:
1.144 djm 2035: id = conn->msg_id;
1.21 djm 2036: startid = ackid = id + 1;
1.141 djm 2037: data = xmalloc(conn->upload_buflen);
1.20 djm 2038:
1.1 djm 2039: /* Read from local and write to remote */
1.115 logan 2040: offset = progress_counter = (resume ? c->size : 0);
1.153 djm 2041: if (showprogress) {
2042: start_progress_meter(progress_meter_path(local_path),
2043: sb.st_size, &progress_counter);
2044: }
1.39 fgsch 2045:
1.144 djm 2046: if ((msg = sshbuf_new()) == NULL)
2047: fatal_f("sshbuf_new failed");
1.19 deraadt 2048: for (;;) {
1.1 djm 2049: int len;
2050:
2051: /*
1.51 deraadt 2052: * Can't use atomicio here because it returns 0 on EOF,
1.49 djm 2053: * thus losing the last block of the file.
1.51 deraadt 2054: * Simulate an EOF on interrupt, allowing ACKs from the
1.49 djm 2055: * server to drain.
1.1 djm 2056: */
1.80 djm 2057: if (interrupted || status != SSH2_FX_OK)
1.49 djm 2058: len = 0;
2059: else do
1.141 djm 2060: len = read(local_fd, data, conn->upload_buflen);
1.1 djm 2061: while ((len == -1) && (errno == EINTR || errno == EAGAIN));
2062:
1.161 djm 2063: if (len == -1) {
2064: fatal("read local \"%s\": %s",
2065: local_path, strerror(errno));
2066: } else if (len != 0) {
1.151 djm 2067: ack = request_enqueue(&acks, ++id, len, offset);
1.116 djm 2068: sshbuf_reset(msg);
2069: if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
2070: (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
2071: (r = sshbuf_put_string(msg, handle,
2072: handle_len)) != 0 ||
2073: (r = sshbuf_put_u64(msg, offset)) != 0 ||
2074: (r = sshbuf_put_string(msg, data, len)) != 0)
1.137 djm 2075: fatal_fr(r, "compose");
1.116 djm 2076: send_msg(conn, msg);
1.33 deraadt 2077: debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1.37 deraadt 2078: id, (unsigned long long)offset, len);
1.22 djm 2079: } else if (TAILQ_FIRST(&acks) == NULL)
1.1 djm 2080: break;
2081:
1.22 djm 2082: if (ack == NULL)
2083: fatal("Unexpected ACK %u", id);
2084:
1.28 markus 2085: if (id == startid || len == 0 ||
1.23 djm 2086: id - ackid >= conn->num_requests) {
1.116 djm 2087: u_int rid;
1.31 djm 2088:
1.116 djm 2089: sshbuf_reset(msg);
2090: get_msg(conn, msg);
2091: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
2092: (r = sshbuf_get_u32(msg, &rid)) != 0)
1.137 djm 2093: fatal_fr(r, "parse");
1.22 djm 2094:
2095: if (type != SSH2_FXP_STATUS)
2096: fatal("Expected SSH2_FXP_STATUS(%d) packet, "
2097: "got %d", SSH2_FXP_STATUS, type);
2098:
1.116 djm 2099: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 djm 2100: fatal_fr(r, "parse status");
1.116 djm 2101: debug3("SSH2_FXP_STATUS %u", status);
1.22 djm 2102:
2103: /* Find the request in our queue */
1.151 djm 2104: if ((ack = request_find(&acks, rid)) == NULL)
1.116 djm 2105: fatal("Can't find request for ID %u", rid);
1.22 djm 2106: TAILQ_REMOVE(&acks, ack, tq);
1.151 djm 2107: debug3("In write loop, ack for %u %zu bytes at %lld",
2108: ack->id, ack->len, (unsigned long long)ack->offset);
1.21 djm 2109: ++ackid;
1.100 dtucker 2110: progress_counter += ack->len;
1.163 ! djm 2111: if (!reordered && ack->offset <= highwater)
! 2112: highwater = ack->offset + ack->len;
! 2113: else if (!reordered && ack->offset > highwater) {
! 2114: debug3_f("server reordered ACKs");
! 2115: reordered = 1;
! 2116: }
1.98 djm 2117: free(ack);
1.1 djm 2118: }
2119: offset += len;
1.77 djm 2120: if (offset < 0)
1.137 djm 2121: fatal_f("offset < 0");
1.1 djm 2122: }
1.116 djm 2123: sshbuf_free(msg);
1.80 djm 2124:
1.39 fgsch 2125: if (showprogress)
2126: stop_progress_meter();
1.98 djm 2127: free(data);
1.1 djm 2128:
1.80 djm 2129: if (status != SSH2_FX_OK) {
1.161 djm 2130: error("write remote \"%s\": %s", remote_path, fx2txt(status));
1.116 djm 2131: status = SSH2_FX_FAILURE;
1.80 djm 2132: }
2133:
1.163 ! djm 2134: if ((resume || inplace_flag) && (status != SSH2_FX_OK || interrupted)) {
! 2135: debug("truncating at %llu", (unsigned long long)highwater);
! 2136: attrib_clear(&t);
! 2137: t.flags = SSH2_FILEXFER_ATTR_SIZE;
! 2138: t.size = highwater;
! 2139: do_fsetstat(conn, handle, handle_len, &a);
! 2140: }
! 2141:
1.1 djm 2142: if (close(local_fd) == -1) {
1.161 djm 2143: error("close local \"%s\": %s", local_path, strerror(errno));
1.116 djm 2144: status = SSH2_FX_FAILURE;
1.1 djm 2145: }
2146:
1.10 djm 2147: /* Override umask and utimes if asked */
1.105 djm 2148: if (preserve_flag)
1.23 djm 2149: do_fsetstat(conn, handle, handle_len, &a);
1.10 djm 2150:
1.107 djm 2151: if (fsync_flag)
2152: (void)do_fsync(conn, handle, handle_len);
2153:
1.121 djm 2154: if (do_close(conn, handle, handle_len) != 0)
1.116 djm 2155: status = SSH2_FX_FAILURE;
2156:
1.98 djm 2157: free(handle);
1.5 djm 2158:
1.116 djm 2159: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 2160: }
1.89 djm 2161:
2162: static int
1.116 djm 2163: upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1.150 djm 2164: int depth, int preserve_flag, int print_flag, int resume, int fsync_flag,
1.163 ! djm 2165: int follow_link_flag, int inplace_flag)
1.89 djm 2166: {
1.116 djm 2167: int ret = 0;
1.89 djm 2168: DIR *dirp;
2169: struct dirent *dp;
1.130 djm 2170: char *filename, *new_src = NULL, *new_dst = NULL;
1.89 djm 2171: struct stat sb;
1.121 djm 2172: Attrib a, *dirattrib;
1.138 dtucker 2173: u_int32_t saved_perm;
1.89 djm 2174:
1.161 djm 2175: debug2_f("upload local dir \"%s\" to remote \"%s\"", src, dst);
2176:
1.89 djm 2177: if (depth >= MAX_DIR_DEPTH) {
2178: error("Maximum directory depth exceeded: %d levels", depth);
2179: return -1;
2180: }
2181:
2182: if (stat(src, &sb) == -1) {
1.161 djm 2183: error("stat local \"%s\": %s", src, strerror(errno));
1.89 djm 2184: return -1;
2185: }
2186: if (!S_ISDIR(sb.st_mode)) {
2187: error("\"%s\" is not a directory", src);
2188: return -1;
2189: }
1.147 djm 2190: if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
1.124 schwarze 2191: mprintf("Entering %s\n", src);
1.89 djm 2192:
2193: stat_to_attrib(&sb, &a);
2194: a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
2195: a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
2196: a.perm &= 01777;
1.105 djm 2197: if (!preserve_flag)
1.89 djm 2198: a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1.101 djm 2199:
1.89 djm 2200: /*
1.121 djm 2201: * sftp lacks a portable status value to match errno EEXIST,
2202: * so if we get a failure back then we must check whether
1.138 dtucker 2203: * the path already existed and is a directory. Ensure we can
2204: * write to the directory we create for the duration of the transfer.
1.89 djm 2205: */
1.138 dtucker 2206: saved_perm = a.perm;
2207: a.perm |= (S_IWUSR|S_IXUSR);
1.121 djm 2208: if (do_mkdir(conn, dst, &a, 0) != 0) {
2209: if ((dirattrib = do_stat(conn, dst, 0)) == NULL)
1.89 djm 2210: return -1;
1.121 djm 2211: if (!S_ISDIR(dirattrib->perm)) {
2212: error("\"%s\" exists but is not a directory", dst);
1.89 djm 2213: return -1;
1.121 djm 2214: }
1.89 djm 2215: }
1.138 dtucker 2216: a.perm = saved_perm;
1.89 djm 2217:
2218: if ((dirp = opendir(src)) == NULL) {
1.161 djm 2219: error("local opendir \"%s\": %s", src, strerror(errno));
1.89 djm 2220: return -1;
2221: }
1.101 djm 2222:
1.89 djm 2223: while (((dp = readdir(dirp)) != NULL) && !interrupted) {
2224: if (dp->d_ino == 0)
2225: continue;
1.130 djm 2226: free(new_dst);
2227: free(new_src);
1.89 djm 2228: filename = dp->d_name;
2229: new_dst = path_append(dst, filename);
2230: new_src = path_append(src, filename);
2231:
1.90 dtucker 2232: if (lstat(new_src, &sb) == -1) {
1.161 djm 2233: logit("local lstat \"%s\": %s", filename,
1.90 dtucker 2234: strerror(errno));
2235: ret = -1;
2236: } else if (S_ISDIR(sb.st_mode)) {
1.89 djm 2237: if (strcmp(filename, ".") == 0 ||
2238: strcmp(filename, "..") == 0)
2239: continue;
2240:
2241: if (upload_dir_internal(conn, new_src, new_dst,
1.115 logan 2242: depth + 1, preserve_flag, print_flag, resume,
1.163 ! djm 2243: fsync_flag, follow_link_flag, inplace_flag) == -1)
1.89 djm 2244: ret = -1;
1.150 djm 2245: } else if (S_ISREG(sb.st_mode) ||
2246: (follow_link_flag && S_ISLNK(sb.st_mode))) {
1.105 djm 2247: if (do_upload(conn, new_src, new_dst,
1.163 ! djm 2248: preserve_flag, resume, fsync_flag,
! 2249: inplace_flag) == -1) {
1.161 djm 2250: error("upload \"%s\" to \"%s\" failed",
1.89 djm 2251: new_src, new_dst);
2252: ret = -1;
2253: }
2254: } else
1.161 djm 2255: logit("%s: not a regular file", filename);
1.89 djm 2256: }
1.130 djm 2257: free(new_dst);
2258: free(new_src);
1.89 djm 2259:
2260: do_setstat(conn, dst, &a);
2261:
2262: (void) closedir(dirp);
2263: return ret;
2264: }
2265:
2266: int
1.116 djm 2267: upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
1.150 djm 2268: int preserve_flag, int print_flag, int resume, int fsync_flag,
1.163 ! djm 2269: int follow_link_flag, int inplace_flag)
1.89 djm 2270: {
2271: char *dst_canon;
2272: int ret;
2273:
2274: if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1.161 djm 2275: error("upload \"%s\": path canonicalization failed", dst);
1.89 djm 2276: return -1;
2277: }
2278:
1.106 djm 2279: ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
1.163 ! djm 2280: print_flag, resume, fsync_flag, follow_link_flag, inplace_flag);
1.107 djm 2281:
1.98 djm 2282: free(dst_canon);
1.145 djm 2283: return ret;
2284: }
2285:
2286: static void
2287: handle_dest_replies(struct sftp_conn *to, const char *to_path, int synchronous,
2288: u_int *nreqsp, u_int *write_errorp)
2289: {
2290: struct sshbuf *msg;
2291: u_char type;
2292: u_int id, status;
2293: int r;
2294: struct pollfd pfd;
2295:
2296: if ((msg = sshbuf_new()) == NULL)
2297: fatal_f("sshbuf_new failed");
2298:
2299: /* Try to eat replies from the upload side */
2300: while (*nreqsp > 0) {
2301: debug3_f("%u outstanding replies", *nreqsp);
2302: if (!synchronous) {
2303: /* Bail out if no data is ready to be read */
2304: pfd.fd = to->fd_in;
2305: pfd.events = POLLIN;
2306: if ((r = poll(&pfd, 1, 0)) == -1) {
2307: if (errno == EINTR)
2308: break;
2309: fatal_f("poll: %s", strerror(errno));
2310: } else if (r == 0)
2311: break; /* fd not ready */
2312: }
2313: sshbuf_reset(msg);
2314: get_msg(to, msg);
2315:
2316: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
2317: (r = sshbuf_get_u32(msg, &id)) != 0)
2318: fatal_fr(r, "dest parse");
2319: debug3("Received dest reply T:%u I:%u R:%u", type, id, *nreqsp);
2320: if (type != SSH2_FXP_STATUS) {
2321: fatal_f("Expected SSH2_FXP_STATUS(%d) packet, got %d",
2322: SSH2_FXP_STATUS, type);
2323: }
2324: if ((r = sshbuf_get_u32(msg, &status)) != 0)
2325: fatal_fr(r, "parse dest status");
2326: debug3("dest SSH2_FXP_STATUS %u", status);
2327: if (status != SSH2_FX_OK) {
2328: /* record first error */
2329: if (*write_errorp == 0)
2330: *write_errorp = status;
2331: }
2332: /*
2333: * XXX this doesn't do full reply matching like do_upload and
2334: * so cannot gracefully truncate terminated uploads at a
2335: * high-water mark. ATM the only caller of this function (scp)
2336: * doesn't support transfer resumption, so this doesn't matter
2337: * a whole lot.
2338: *
2339: * To be safe, do_crossload truncates the destination file to
2340: * zero length on upload failure, since we can't trust the
2341: * server not to have reordered replies that could have
2342: * inserted holes where none existed in the source file.
2343: *
2344: * XXX we could get a more accutate progress bar if we updated
2345: * the counter based on the reply from the destination...
2346: */
2347: (*nreqsp)--;
2348: }
2349: debug3_f("done: %u outstanding replies", *nreqsp);
1.157 dtucker 2350: sshbuf_free(msg);
1.145 djm 2351: }
2352:
2353: int
2354: do_crossload(struct sftp_conn *from, struct sftp_conn *to,
2355: const char *from_path, const char *to_path,
2356: Attrib *a, int preserve_flag)
2357: {
2358: struct sshbuf *msg;
1.152 djm 2359: int write_error, read_error, r;
1.145 djm 2360: u_int64_t offset = 0, size;
2361: u_int id, buflen, num_req, max_req, status = SSH2_FX_OK;
2362: u_int num_upload_req;
2363: off_t progress_counter;
2364: u_char *from_handle, *to_handle;
2365: size_t from_handle_len, to_handle_len;
1.146 djm 2366: struct requests requests;
1.145 djm 2367: struct request *req;
2368: u_char type;
2369:
1.161 djm 2370: debug2_f("crossload src \"%s\" to dst \"%s\"", from_path, to_path);
2371:
1.145 djm 2372: TAILQ_INIT(&requests);
2373:
2374: if (a == NULL && (a = do_stat(from, from_path, 0)) == NULL)
2375: return -1;
2376:
2377: if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
2378: (!S_ISREG(a->perm))) {
1.161 djm 2379: error("download \"%s\": not a regular file", from_path);
1.145 djm 2380: return(-1);
2381: }
2382: if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
2383: size = a->size;
2384: else
2385: size = 0;
2386:
2387: buflen = from->download_buflen;
2388: if (buflen > to->upload_buflen)
2389: buflen = to->upload_buflen;
2390:
2391: /* Send open request to read side */
2392: if (send_open(from, from_path, "origin", SSH2_FXF_READ, NULL,
2393: &from_handle, &from_handle_len) != 0)
2394: return -1;
2395:
2396: /* Send open request to write side */
2397: a->flags &= ~SSH2_FILEXFER_ATTR_SIZE;
2398: a->flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
2399: a->perm &= 0777;
2400: if (!preserve_flag)
2401: a->flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
2402: if (send_open(to, to_path, "dest",
2403: SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC, a,
2404: &to_handle, &to_handle_len) != 0) {
2405: do_close(from, from_handle, from_handle_len);
2406: return -1;
2407: }
2408:
2409: /* Read from remote "from" and write to remote "to" */
2410: offset = 0;
2411: write_error = read_error = num_req = num_upload_req = 0;
2412: max_req = 1;
2413: progress_counter = 0;
2414:
1.153 djm 2415: if (showprogress && size != 0) {
2416: start_progress_meter(progress_meter_path(from_path),
2417: size, &progress_counter);
2418: }
1.145 djm 2419: if ((msg = sshbuf_new()) == NULL)
2420: fatal_f("sshbuf_new failed");
2421: while (num_req > 0 || max_req > 0) {
2422: u_char *data;
2423: size_t len;
2424:
2425: /*
2426: * Simulate EOF on interrupt: stop sending new requests and
2427: * allow outstanding requests to drain gracefully
2428: */
2429: if (interrupted) {
2430: if (num_req == 0) /* If we haven't started yet... */
2431: break;
2432: max_req = 0;
2433: }
2434:
2435: /* Send some more requests */
2436: while (num_req < max_req) {
2437: debug3("Request range %llu -> %llu (%d/%d)",
2438: (unsigned long long)offset,
2439: (unsigned long long)offset + buflen - 1,
2440: num_req, max_req);
1.151 djm 2441: req = request_enqueue(&requests, from->msg_id++,
2442: buflen, offset);
1.145 djm 2443: offset += buflen;
2444: num_req++;
2445: send_read_request(from, req->id, req->offset,
2446: req->len, from_handle, from_handle_len);
2447: }
2448:
2449: /* Try to eat replies from the upload side (nonblocking) */
2450: handle_dest_replies(to, to_path, 0,
2451: &num_upload_req, &write_error);
2452:
2453: sshbuf_reset(msg);
2454: get_msg(from, msg);
2455: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
2456: (r = sshbuf_get_u32(msg, &id)) != 0)
2457: fatal_fr(r, "parse");
2458: debug3("Received origin reply T:%u I:%u R:%d",
2459: type, id, max_req);
2460:
2461: /* Find the request in our queue */
1.151 djm 2462: if ((req = request_find(&requests, id)) == NULL)
1.145 djm 2463: fatal("Unexpected reply %u", id);
2464:
2465: switch (type) {
2466: case SSH2_FXP_STATUS:
2467: if ((r = sshbuf_get_u32(msg, &status)) != 0)
2468: fatal_fr(r, "parse status");
2469: if (status != SSH2_FX_EOF)
2470: read_error = 1;
2471: max_req = 0;
2472: TAILQ_REMOVE(&requests, req, tq);
2473: free(req);
2474: num_req--;
2475: break;
2476: case SSH2_FXP_DATA:
2477: if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
2478: fatal_fr(r, "parse data");
2479: debug3("Received data %llu -> %llu",
2480: (unsigned long long)req->offset,
2481: (unsigned long long)req->offset + len - 1);
2482: if (len > req->len)
2483: fatal("Received more data than asked for "
2484: "%zu > %zu", len, req->len);
2485:
2486: /* Write this chunk out to the destination */
2487: sshbuf_reset(msg);
2488: if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
2489: (r = sshbuf_put_u32(msg, to->msg_id++)) != 0 ||
2490: (r = sshbuf_put_string(msg, to_handle,
2491: to_handle_len)) != 0 ||
2492: (r = sshbuf_put_u64(msg, req->offset)) != 0 ||
2493: (r = sshbuf_put_string(msg, data, len)) != 0)
2494: fatal_fr(r, "compose write");
2495: send_msg(to, msg);
2496: debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%zu",
2497: id, (unsigned long long)offset, len);
2498: num_upload_req++;
2499: progress_counter += len;
2500: free(data);
2501:
2502: if (len == req->len) {
2503: TAILQ_REMOVE(&requests, req, tq);
2504: free(req);
2505: num_req--;
2506: } else {
2507: /* Resend the request for the missing data */
2508: debug3("Short data block, re-requesting "
2509: "%llu -> %llu (%2d)",
2510: (unsigned long long)req->offset + len,
2511: (unsigned long long)req->offset +
2512: req->len - 1, num_req);
2513: req->id = from->msg_id++;
2514: req->len -= len;
2515: req->offset += len;
2516: send_read_request(from, req->id,
2517: req->offset, req->len,
2518: from_handle, from_handle_len);
2519: /* Reduce the request size */
2520: if (len < buflen)
2521: buflen = MAXIMUM(MIN_READ_SIZE, len);
2522: }
2523: if (max_req > 0) { /* max_req = 0 iff EOF received */
2524: if (size > 0 && offset > size) {
2525: /* Only one request at a time
2526: * after the expected EOF */
2527: debug3("Finish at %llu (%2d)",
2528: (unsigned long long)offset,
2529: num_req);
2530: max_req = 1;
2531: } else if (max_req < from->num_requests) {
2532: ++max_req;
2533: }
2534: }
2535: break;
2536: default:
2537: fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
2538: SSH2_FXP_DATA, type);
2539: }
2540: }
2541:
2542: if (showprogress && size)
2543: stop_progress_meter();
2544:
2545: /* Drain replies from the server (blocking) */
2546: debug3_f("waiting for %u replies from destination", num_upload_req);
2547: handle_dest_replies(to, to_path, 1, &num_upload_req, &write_error);
2548:
2549: /* Sanity check */
2550: if (TAILQ_FIRST(&requests) != NULL)
2551: fatal("Transfer complete, but requests still in queue");
2552: /* Truncate at 0 length on interrupt or error to avoid holes at dest */
2553: if (read_error || write_error || interrupted) {
2554: debug("truncating \"%s\" at 0", to_path);
2555: do_close(to, to_handle, to_handle_len);
2556: free(to_handle);
2557: if (send_open(to, to_path, "dest",
2558: SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC, a,
2559: &to_handle, &to_handle_len) != 0) {
1.161 djm 2560: error("dest truncate \"%s\" failed", to_path);
1.145 djm 2561: to_handle = NULL;
2562: }
2563: }
2564: if (read_error) {
1.161 djm 2565: error("read origin \"%s\": %s", from_path, fx2txt(status));
1.145 djm 2566: status = -1;
2567: do_close(from, from_handle, from_handle_len);
2568: if (to_handle != NULL)
2569: do_close(to, to_handle, to_handle_len);
2570: } else if (write_error) {
1.161 djm 2571: error("write dest \"%s\": %s", to_path, fx2txt(write_error));
1.145 djm 2572: status = SSH2_FX_FAILURE;
2573: do_close(from, from_handle, from_handle_len);
2574: if (to_handle != NULL)
2575: do_close(to, to_handle, to_handle_len);
2576: } else {
2577: if (do_close(from, from_handle, from_handle_len) != 0 ||
2578: interrupted)
2579: status = -1;
2580: else
2581: status = SSH2_FX_OK;
2582: if (to_handle != NULL) {
2583: /* Need to resend utimes after write */
2584: if (preserve_flag)
2585: do_fsetstat(to, to_handle, to_handle_len, a);
2586: do_close(to, to_handle, to_handle_len);
2587: }
2588: }
2589: sshbuf_free(msg);
2590: free(from_handle);
2591: free(to_handle);
2592:
2593: return status == SSH2_FX_OK ? 0 : -1;
2594: }
2595:
2596: static int
2597: crossload_dir_internal(struct sftp_conn *from, struct sftp_conn *to,
2598: const char *from_path, const char *to_path,
1.150 djm 2599: int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
2600: int follow_link_flag)
1.145 djm 2601: {
2602: int i, ret = 0;
2603: SFTP_DIRENT **dir_entries;
2604: char *filename, *new_from_path = NULL, *new_to_path = NULL;
2605: mode_t mode = 0777;
1.149 djm 2606: Attrib curdir;
1.145 djm 2607:
1.161 djm 2608: debug2_f("crossload dir src \"%s\" to dst \"%s\"", from_path, to_path);
2609:
1.145 djm 2610: if (depth >= MAX_DIR_DEPTH) {
2611: error("Maximum directory depth exceeded: %d levels", depth);
2612: return -1;
2613: }
2614:
2615: if (dirattrib == NULL &&
2616: (dirattrib = do_stat(from, from_path, 1)) == NULL) {
1.161 djm 2617: error("stat remote \"%s\" failed", from_path);
1.145 djm 2618: return -1;
2619: }
2620: if (!S_ISDIR(dirattrib->perm)) {
2621: error("\"%s\" is not a directory", from_path);
2622: return -1;
2623: }
1.150 djm 2624: if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
1.145 djm 2625: mprintf("Retrieving %s\n", from_path);
2626:
1.149 djm 2627: curdir = *dirattrib; /* dirattrib will be clobbered */
2628: curdir.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
2629: curdir.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
2630: if ((curdir.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) == 0) {
2631: debug("Origin did not send permissions for "
1.145 djm 2632: "directory \"%s\"", to_path);
1.149 djm 2633: curdir.perm = S_IWUSR|S_IXUSR;
2634: curdir.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1.145 djm 2635: }
1.149 djm 2636: /* We need to be able to write to the directory while we transfer it */
2637: mode = curdir.perm & 01777;
2638: curdir.perm = mode | (S_IWUSR|S_IXUSR);
2639:
2640: /*
2641: * sftp lacks a portable status value to match errno EEXIST,
2642: * so if we get a failure back then we must check whether
2643: * the path already existed and is a directory. Ensure we can
2644: * write to the directory we create for the duration of the transfer.
2645: */
2646: if (do_mkdir(to, to_path, &curdir, 0) != 0) {
2647: if ((dirattrib = do_stat(to, to_path, 0)) == NULL)
2648: return -1;
2649: if (!S_ISDIR(dirattrib->perm)) {
2650: error("\"%s\" exists but is not a directory", to_path);
2651: return -1;
2652: }
2653: }
2654: curdir.perm = mode;
1.145 djm 2655:
2656: if (do_readdir(from, from_path, &dir_entries) == -1) {
1.161 djm 2657: error("origin readdir \"%s\" failed", from_path);
1.145 djm 2658: return -1;
2659: }
2660:
2661: for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
2662: free(new_from_path);
2663: free(new_to_path);
2664:
2665: filename = dir_entries[i]->filename;
2666: new_from_path = path_append(from_path, filename);
2667: new_to_path = path_append(to_path, filename);
2668:
2669: if (S_ISDIR(dir_entries[i]->a.perm)) {
2670: if (strcmp(filename, ".") == 0 ||
2671: strcmp(filename, "..") == 0)
2672: continue;
2673: if (crossload_dir_internal(from, to,
2674: new_from_path, new_to_path,
2675: depth + 1, &(dir_entries[i]->a), preserve_flag,
1.150 djm 2676: print_flag, follow_link_flag) == -1)
1.145 djm 2677: ret = -1;
1.150 djm 2678: } else if (S_ISREG(dir_entries[i]->a.perm) ||
2679: (follow_link_flag && S_ISLNK(dir_entries[i]->a.perm))) {
2680: /*
2681: * If this is a symlink then don't send the link's
2682: * Attrib. do_download() will do a FXP_STAT operation
2683: * and get the link target's attributes.
2684: */
1.145 djm 2685: if (do_crossload(from, to, new_from_path, new_to_path,
1.150 djm 2686: S_ISLNK(dir_entries[i]->a.perm) ? NULL :
1.145 djm 2687: &(dir_entries[i]->a), preserve_flag) == -1) {
1.161 djm 2688: error("crossload \"%s\" to \"%s\" failed",
1.145 djm 2689: new_from_path, new_to_path);
2690: ret = -1;
2691: }
1.161 djm 2692: } else {
2693: logit("origin \"%s\": not a regular file",
2694: new_from_path);
2695: }
1.145 djm 2696: }
2697: free(new_to_path);
2698: free(new_from_path);
2699:
1.149 djm 2700: do_setstat(to, to_path, &curdir);
1.145 djm 2701:
2702: free_sftp_dirents(dir_entries);
2703:
2704: return ret;
2705: }
2706:
2707: int
2708: crossload_dir(struct sftp_conn *from, struct sftp_conn *to,
2709: const char *from_path, const char *to_path,
1.150 djm 2710: Attrib *dirattrib, int preserve_flag, int print_flag, int follow_link_flag)
1.145 djm 2711: {
2712: char *from_path_canon;
2713: int ret;
2714:
2715: if ((from_path_canon = do_realpath(from, from_path)) == NULL) {
1.161 djm 2716: error("crossload \"%s\": path canonicalization failed",
2717: from_path);
1.145 djm 2718: return -1;
2719: }
2720:
2721: ret = crossload_dir_internal(from, to, from_path_canon, to_path, 0,
1.150 djm 2722: dirattrib, preserve_flag, print_flag, follow_link_flag);
1.145 djm 2723: free(from_path_canon);
1.89 djm 2724: return ret;
2725: }
2726:
2727: char *
1.116 djm 2728: path_append(const char *p1, const char *p2)
1.89 djm 2729: {
2730: char *ret;
2731: size_t len = strlen(p1) + strlen(p2) + 2;
2732:
2733: ret = xmalloc(len);
2734: strlcpy(ret, p1, len);
2735: if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
2736: strlcat(ret, "/", len);
2737: strlcat(ret, p2, len);
2738:
2739: return(ret);
1.139 djm 2740: }
2741:
2742: char *
2743: make_absolute(char *p, const char *pwd)
2744: {
2745: char *abs_str;
2746:
2747: /* Derelativise */
2748: if (p && !path_absolute(p)) {
2749: abs_str = path_append(pwd, p);
2750: free(p);
2751: return(abs_str);
2752: } else
2753: return(p);
2754: }
2755:
2756: int
2757: remote_is_dir(struct sftp_conn *conn, const char *path)
2758: {
2759: Attrib *a;
2760:
2761: /* XXX: report errors? */
2762: if ((a = do_stat(conn, path, 1)) == NULL)
2763: return(0);
2764: if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
2765: return(0);
2766: return(S_ISDIR(a->perm));
2767: }
2768:
2769:
2770: int
2771: local_is_dir(const char *path)
2772: {
2773: struct stat sb;
2774:
2775: /* XXX: report errors? */
2776: if (stat(path, &sb) == -1)
2777: return(0);
2778:
2779: return(S_ISDIR(sb.st_mode));
2780: }
2781:
2782: /* Check whether path returned from glob(..., GLOB_MARK, ...) is a directory */
2783: int
2784: globpath_is_dir(const char *pathname)
2785: {
2786: size_t l = strlen(pathname);
2787:
2788: return l > 0 && pathname[l - 1] == '/';
1.89 djm 2789: }
2790: