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