Annotation of src/usr.bin/ssh/sftp-client.c, Revision 1.152
1.152 ! djm 1: /* $OpenBSD: sftp-client.c,v 1.151 2021/08/07 00:14:17 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.1 djm 1353: int
1.116 djm 1354: do_download(struct sftp_conn *conn, const char *remote_path,
1355: const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
1356: int fsync_flag)
1.1 djm 1357: {
1.116 djm 1358: struct sshbuf *msg;
1359: u_char *handle;
1360: int local_fd = -1, write_error;
1.134 djm 1361: int read_error, write_errno, lmodified = 0, reordered = 0, r;
1.101 djm 1362: u_int64_t offset = 0, size, highwater;
1.116 djm 1363: u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
1.39 fgsch 1364: off_t progress_counter;
1.116 djm 1365: size_t handle_len;
1.101 djm 1366: struct stat st;
1.146 djm 1367: struct requests requests;
1.21 djm 1368: struct request *req;
1.116 djm 1369: u_char type;
1.21 djm 1370:
1371: TAILQ_INIT(&requests);
1.1 djm 1372:
1.89 djm 1373: if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1374: return -1;
1.1 djm 1375:
1.86 djm 1376: /* Do not preserve set[ug]id here, as we do not preserve ownership */
1.1 djm 1377: if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1.38 djm 1378: mode = a->perm & 0777;
1.1 djm 1379: else
1380: mode = 0666;
1381:
1.14 djm 1382: if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1.41 djm 1383: (!S_ISREG(a->perm))) {
1384: error("Cannot download non-regular file: %s", remote_path);
1.14 djm 1385: return(-1);
1386: }
1387:
1.21 djm 1388: if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1389: size = a->size;
1390: else
1391: size = 0;
1392:
1.141 djm 1393: buflen = conn->download_buflen;
1.1 djm 1394:
1395: /* Send open request */
1.144 djm 1396: if (send_open(conn, remote_path, "remote", SSH2_FXF_READ, NULL,
1397: &handle, &handle_len) != 0)
1398: return -1;
1.1 djm 1399:
1.105 djm 1400: local_fd = open(local_path,
1401: O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
1.23 djm 1402: if (local_fd == -1) {
1403: error("Couldn't open local file \"%s\" for writing: %s",
1404: local_path, strerror(errno));
1.101 djm 1405: goto fail;
1406: }
1407: offset = highwater = 0;
1.105 djm 1408: if (resume_flag) {
1.101 djm 1409: if (fstat(local_fd, &st) == -1) {
1410: error("Unable to stat local file \"%s\": %s",
1411: local_path, strerror(errno));
1412: goto fail;
1413: }
1.113 djm 1414: if (st.st_size < 0) {
1415: error("\"%s\" has negative size", local_path);
1416: goto fail;
1417: }
1418: if ((u_int64_t)st.st_size > size) {
1.101 djm 1419: error("Unable to resume download of \"%s\": "
1420: "local file is larger than remote", local_path);
1421: fail:
1422: do_close(conn, handle, handle_len);
1423: free(handle);
1.110 djm 1424: if (local_fd != -1)
1425: close(local_fd);
1.101 djm 1426: return -1;
1427: }
1428: offset = highwater = st.st_size;
1.23 djm 1429: }
1430:
1.1 djm 1431: /* Read from remote and write to local */
1.101 djm 1432: write_error = read_error = write_errno = num_req = 0;
1.21 djm 1433: max_req = 1;
1.101 djm 1434: progress_counter = offset;
1.39 fgsch 1435:
1.47 djm 1436: if (showprogress && size != 0)
1437: start_progress_meter(remote_path, size, &progress_counter);
1.39 fgsch 1438:
1.144 djm 1439: if ((msg = sshbuf_new()) == NULL)
1440: fatal_f("sshbuf_new failed");
1441:
1.21 djm 1442: while (num_req > 0 || max_req > 0) {
1.116 djm 1443: u_char *data;
1444: size_t len;
1.1 djm 1445:
1.49 djm 1446: /*
1.51 deraadt 1447: * Simulate EOF on interrupt: stop sending new requests and
1.49 djm 1448: * allow outstanding requests to drain gracefully
1449: */
1450: if (interrupted) {
1451: if (num_req == 0) /* If we haven't started yet... */
1452: break;
1453: max_req = 0;
1454: }
1455:
1.21 djm 1456: /* Send some more requests */
1457: while (num_req < max_req) {
1.28 markus 1458: debug3("Request range %llu -> %llu (%d/%d)",
1.25 itojun 1459: (unsigned long long)offset,
1460: (unsigned long long)offset + buflen - 1,
1461: num_req, max_req);
1.151 djm 1462: req = request_enqueue(&requests, conn->msg_id++,
1463: buflen, offset);
1.21 djm 1464: offset += buflen;
1465: num_req++;
1.93 djm 1466: send_read_request(conn, req->id, req->offset,
1.21 djm 1467: req->len, handle, handle_len);
1468: }
1.1 djm 1469:
1.116 djm 1470: sshbuf_reset(msg);
1471: get_msg(conn, msg);
1472: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1473: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 djm 1474: fatal_fr(r, "parse");
1.33 deraadt 1475: debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1.21 djm 1476:
1477: /* Find the request in our queue */
1.151 djm 1478: if ((req = request_find(&requests, id)) == NULL)
1.21 djm 1479: fatal("Unexpected reply %u", id);
1480:
1481: switch (type) {
1482: case SSH2_FXP_STATUS:
1.116 djm 1483: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 djm 1484: fatal_fr(r, "parse status");
1.21 djm 1485: if (status != SSH2_FX_EOF)
1486: read_error = 1;
1487: max_req = 0;
1488: TAILQ_REMOVE(&requests, req, tq);
1.98 djm 1489: free(req);
1.21 djm 1490: num_req--;
1491: break;
1492: case SSH2_FXP_DATA:
1.116 djm 1493: if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1.137 djm 1494: fatal_fr(r, "parse data");
1.26 itojun 1495: debug3("Received data %llu -> %llu",
1.28 markus 1496: (unsigned long long)req->offset,
1.26 itojun 1497: (unsigned long long)req->offset + len - 1);
1.21 djm 1498: if (len > req->len)
1499: fatal("Received more data than asked for "
1.116 djm 1500: "%zu > %zu", len, req->len);
1.134 djm 1501: lmodified = 1;
1.21 djm 1502: if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1.44 deraadt 1503: atomicio(vwrite, local_fd, data, len) != len) &&
1.21 djm 1504: !write_error) {
1505: write_errno = errno;
1506: write_error = 1;
1507: max_req = 0;
1508: }
1.101 djm 1509: else if (!reordered && req->offset <= highwater)
1510: highwater = req->offset + len;
1511: else if (!reordered && req->offset > highwater)
1512: reordered = 1;
1.39 fgsch 1513: progress_counter += len;
1.98 djm 1514: free(data);
1.1 djm 1515:
1.21 djm 1516: if (len == req->len) {
1517: TAILQ_REMOVE(&requests, req, tq);
1.98 djm 1518: free(req);
1.21 djm 1519: num_req--;
1520: } else {
1521: /* Resend the request for the missing data */
1522: debug3("Short data block, re-requesting "
1.26 itojun 1523: "%llu -> %llu (%2d)",
1.28 markus 1524: (unsigned long long)req->offset + len,
1.27 itojun 1525: (unsigned long long)req->offset +
1526: req->len - 1, num_req);
1.23 djm 1527: req->id = conn->msg_id++;
1.21 djm 1528: req->len -= len;
1529: req->offset += len;
1.93 djm 1530: send_read_request(conn, req->id,
1.23 djm 1531: req->offset, req->len, handle, handle_len);
1.21 djm 1532: /* Reduce the request size */
1533: if (len < buflen)
1.125 deraadt 1534: buflen = MAXIMUM(MIN_READ_SIZE, len);
1.21 djm 1535: }
1536: if (max_req > 0) { /* max_req = 0 iff EOF received */
1537: if (size > 0 && offset > size) {
1538: /* Only one request at a time
1539: * after the expected EOF */
1540: debug3("Finish at %llu (%2d)",
1.26 itojun 1541: (unsigned long long)offset,
1542: num_req);
1.21 djm 1543: max_req = 1;
1.136 djm 1544: } else if (max_req < conn->num_requests) {
1.21 djm 1545: ++max_req;
1546: }
1.1 djm 1547: }
1.21 djm 1548: break;
1549: default:
1.33 deraadt 1550: fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1.1 djm 1551: SSH2_FXP_DATA, type);
1552: }
1.21 djm 1553: }
1.1 djm 1554:
1.39 fgsch 1555: if (showprogress && size)
1556: stop_progress_meter();
1557:
1.21 djm 1558: /* Sanity check */
1559: if (TAILQ_FIRST(&requests) != NULL)
1560: fatal("Transfer complete, but requests still in queue");
1.101 djm 1561: /* Truncate at highest contiguous point to avoid holes on interrupt */
1562: if (read_error || write_error || interrupted) {
1.105 djm 1563: if (reordered && resume_flag) {
1.101 djm 1564: error("Unable to resume download of \"%s\": "
1565: "server reordered requests", local_path);
1566: }
1567: debug("truncating at %llu", (unsigned long long)highwater);
1.120 djm 1568: if (ftruncate(local_fd, highwater) == -1)
1569: error("ftruncate \"%s\": %s", local_path,
1570: strerror(errno));
1.101 djm 1571: }
1.21 djm 1572: if (read_error) {
1.28 markus 1573: error("Couldn't read from remote file \"%s\" : %s",
1.21 djm 1574: remote_path, fx2txt(status));
1.103 djm 1575: status = -1;
1.23 djm 1576: do_close(conn, handle, handle_len);
1.21 djm 1577: } else if (write_error) {
1578: error("Couldn't write to \"%s\": %s", local_path,
1579: strerror(write_errno));
1.116 djm 1580: status = SSH2_FX_FAILURE;
1.23 djm 1581: do_close(conn, handle, handle_len);
1.21 djm 1582: } else {
1.116 djm 1583: if (do_close(conn, handle, handle_len) != 0 || interrupted)
1584: status = SSH2_FX_FAILURE;
1585: else
1586: status = SSH2_FX_OK;
1.21 djm 1587: /* Override umask and utimes if asked */
1.105 djm 1588: if (preserve_flag && fchmod(local_fd, mode) == -1)
1.21 djm 1589: error("Couldn't set mode on \"%s\": %s", local_path,
1.37 deraadt 1590: strerror(errno));
1.105 djm 1591: if (preserve_flag &&
1592: (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1.21 djm 1593: struct timeval tv[2];
1594: tv[0].tv_sec = a->atime;
1595: tv[1].tv_sec = a->mtime;
1596: tv[0].tv_usec = tv[1].tv_usec = 0;
1597: if (utimes(local_path, tv) == -1)
1598: error("Can't set times on \"%s\": %s",
1.37 deraadt 1599: local_path, strerror(errno));
1.1 djm 1600: }
1.134 djm 1601: if (resume_flag && !lmodified)
1602: logit("File \"%s\" was not modified", local_path);
1603: else if (fsync_flag) {
1.107 djm 1604: debug("syncing \"%s\"", local_path);
1605: if (fsync(local_fd) == -1)
1606: error("Couldn't sync file \"%s\": %s",
1607: local_path, strerror(errno));
1608: }
1.10 djm 1609: }
1.5 djm 1610: close(local_fd);
1.116 djm 1611: sshbuf_free(msg);
1.98 djm 1612: free(handle);
1.23 djm 1613:
1.129 djm 1614: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 1615: }
1616:
1.89 djm 1617: static int
1.116 djm 1618: download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1619: int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
1.150 djm 1620: int resume_flag, int fsync_flag, int follow_link_flag)
1.89 djm 1621: {
1622: int i, ret = 0;
1623: SFTP_DIRENT **dir_entries;
1.130 djm 1624: char *filename, *new_src = NULL, *new_dst = NULL;
1.138 dtucker 1625: mode_t mode = 0777, tmpmode = mode;
1.89 djm 1626:
1627: if (depth >= MAX_DIR_DEPTH) {
1628: error("Maximum directory depth exceeded: %d levels", depth);
1629: return -1;
1630: }
1631:
1632: if (dirattrib == NULL &&
1633: (dirattrib = do_stat(conn, src, 1)) == NULL) {
1634: error("Unable to stat remote directory \"%s\"", src);
1635: return -1;
1636: }
1637: if (!S_ISDIR(dirattrib->perm)) {
1638: error("\"%s\" is not a directory", src);
1639: return -1;
1640: }
1.147 djm 1641: if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
1.124 schwarze 1642: mprintf("Retrieving %s\n", src);
1.89 djm 1643:
1.138 dtucker 1644: if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
1.89 djm 1645: mode = dirattrib->perm & 01777;
1.138 dtucker 1646: tmpmode = mode | (S_IWUSR|S_IXUSR);
1647: } else {
1.89 djm 1648: debug("Server did not send permissions for "
1649: "directory \"%s\"", dst);
1650: }
1651:
1.138 dtucker 1652: if (mkdir(dst, tmpmode) == -1 && errno != EEXIST) {
1.89 djm 1653: error("mkdir %s: %s", dst, strerror(errno));
1654: return -1;
1655: }
1656:
1657: if (do_readdir(conn, src, &dir_entries) == -1) {
1658: error("%s: Failed to get directory contents", src);
1659: return -1;
1660: }
1661:
1662: for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1.130 djm 1663: free(new_dst);
1664: free(new_src);
1665:
1.89 djm 1666: filename = dir_entries[i]->filename;
1667: new_dst = path_append(dst, filename);
1668: new_src = path_append(src, filename);
1669:
1670: if (S_ISDIR(dir_entries[i]->a.perm)) {
1671: if (strcmp(filename, ".") == 0 ||
1672: strcmp(filename, "..") == 0)
1673: continue;
1674: if (download_dir_internal(conn, new_src, new_dst,
1.105 djm 1675: depth + 1, &(dir_entries[i]->a), preserve_flag,
1.150 djm 1676: print_flag, resume_flag,
1677: fsync_flag, follow_link_flag) == -1)
1.89 djm 1678: ret = -1;
1.150 djm 1679: } else if (S_ISREG(dir_entries[i]->a.perm) ||
1680: (follow_link_flag && S_ISLNK(dir_entries[i]->a.perm))) {
1681: /*
1682: * If this is a symlink then don't send the link's
1683: * Attrib. do_download() will do a FXP_STAT operation
1684: * and get the link target's attributes.
1685: */
1.89 djm 1686: if (do_download(conn, new_src, new_dst,
1.150 djm 1687: S_ISLNK(dir_entries[i]->a.perm) ? NULL :
1688: &(dir_entries[i]->a),
1689: preserve_flag, resume_flag, fsync_flag) == -1) {
1.89 djm 1690: error("Download of file %s to %s failed",
1691: new_src, new_dst);
1692: ret = -1;
1693: }
1694: } else
1695: logit("%s: not a regular file\n", new_src);
1696:
1697: }
1.130 djm 1698: free(new_dst);
1699: free(new_src);
1.89 djm 1700:
1.105 djm 1701: if (preserve_flag) {
1.89 djm 1702: if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1703: struct timeval tv[2];
1704: tv[0].tv_sec = dirattrib->atime;
1705: tv[1].tv_sec = dirattrib->mtime;
1706: tv[0].tv_usec = tv[1].tv_usec = 0;
1707: if (utimes(dst, tv) == -1)
1708: error("Can't set times on \"%s\": %s",
1709: dst, strerror(errno));
1710: } else
1711: debug("Server did not send times for directory "
1712: "\"%s\"", dst);
1713: }
1714:
1.138 dtucker 1715: if (mode != tmpmode && chmod(dst, mode) == -1)
1716: error("Can't set final mode on \"%s\": %s", dst,
1717: strerror(errno));
1718:
1.89 djm 1719: free_sftp_dirents(dir_entries);
1720:
1721: return ret;
1722: }
1723:
1724: int
1.116 djm 1725: download_dir(struct sftp_conn *conn, const char *src, const char *dst,
1726: Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1.150 djm 1727: int fsync_flag, int follow_link_flag)
1.89 djm 1728: {
1729: char *src_canon;
1730: int ret;
1731:
1732: if ((src_canon = do_realpath(conn, src)) == NULL) {
1.107 djm 1733: error("Unable to canonicalize path \"%s\"", src);
1.89 djm 1734: return -1;
1735: }
1736:
1.105 djm 1737: ret = download_dir_internal(conn, src_canon, dst, 0,
1.150 djm 1738: dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag,
1739: follow_link_flag);
1.98 djm 1740: free(src_canon);
1.89 djm 1741: return ret;
1742: }
1743:
1.1 djm 1744: int
1.116 djm 1745: do_upload(struct sftp_conn *conn, const char *local_path,
1746: const char *remote_path, int preserve_flag, int resume, int fsync_flag)
1.1 djm 1747: {
1.116 djm 1748: int r, local_fd;
1749: u_int status = SSH2_FX_OK;
1750: u_int id;
1751: u_char type;
1.100 dtucker 1752: off_t offset, progress_counter;
1.116 djm 1753: u_char *handle, *data;
1754: struct sshbuf *msg;
1.1 djm 1755: struct stat sb;
1.115 logan 1756: Attrib a, *c = NULL;
1.21 djm 1757: u_int32_t startid;
1758: u_int32_t ackid;
1.151 djm 1759: struct request *ack = NULL;
1760: struct requests acks;
1.116 djm 1761: size_t handle_len;
1.22 djm 1762:
1763: TAILQ_INIT(&acks);
1.1 djm 1764:
1765: if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1766: error("Couldn't open local file \"%s\" for reading: %s",
1767: local_path, strerror(errno));
1768: return(-1);
1769: }
1770: if (fstat(local_fd, &sb) == -1) {
1771: error("Couldn't fstat local file \"%s\": %s",
1772: local_path, strerror(errno));
1.41 djm 1773: close(local_fd);
1774: return(-1);
1775: }
1776: if (!S_ISREG(sb.st_mode)) {
1777: error("%s is not a regular file", local_path);
1.1 djm 1778: close(local_fd);
1779: return(-1);
1780: }
1781: stat_to_attrib(&sb, &a);
1782:
1783: a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1784: a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1785: a.perm &= 0777;
1.105 djm 1786: if (!preserve_flag)
1.1 djm 1787: a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1788:
1.115 logan 1789: if (resume) {
1790: /* Get remote file size if it exists */
1791: if ((c = do_stat(conn, remote_path, 0)) == NULL) {
1.122 djm 1792: close(local_fd);
1.115 logan 1793: return -1;
1794: }
1795:
1796: if ((off_t)c->size >= sb.st_size) {
1797: error("destination file bigger or same size as "
1.142 djm 1798: "source file");
1.115 logan 1799: close(local_fd);
1800: return -1;
1801: }
1802:
1803: if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
1804: close(local_fd);
1805: return -1;
1806: }
1807: }
1808:
1.1 djm 1809: /* Send open request */
1.144 djm 1810: if (send_open(conn, remote_path, "dest", SSH2_FXF_WRITE|SSH2_FXF_CREAT|
1811: (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC),
1812: &a, &handle, &handle_len) != 0) {
1.1 djm 1813: close(local_fd);
1.80 djm 1814: return -1;
1.1 djm 1815: }
1816:
1.144 djm 1817: id = conn->msg_id;
1.21 djm 1818: startid = ackid = id + 1;
1.141 djm 1819: data = xmalloc(conn->upload_buflen);
1.20 djm 1820:
1.1 djm 1821: /* Read from local and write to remote */
1.115 logan 1822: offset = progress_counter = (resume ? c->size : 0);
1.39 fgsch 1823: if (showprogress)
1.100 dtucker 1824: start_progress_meter(local_path, sb.st_size,
1825: &progress_counter);
1.39 fgsch 1826:
1.144 djm 1827: if ((msg = sshbuf_new()) == NULL)
1828: fatal_f("sshbuf_new failed");
1.19 deraadt 1829: for (;;) {
1.1 djm 1830: int len;
1831:
1832: /*
1.51 deraadt 1833: * Can't use atomicio here because it returns 0 on EOF,
1.49 djm 1834: * thus losing the last block of the file.
1.51 deraadt 1835: * Simulate an EOF on interrupt, allowing ACKs from the
1.49 djm 1836: * server to drain.
1.1 djm 1837: */
1.80 djm 1838: if (interrupted || status != SSH2_FX_OK)
1.49 djm 1839: len = 0;
1840: else do
1.141 djm 1841: len = read(local_fd, data, conn->upload_buflen);
1.1 djm 1842: while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1843:
1844: if (len == -1)
1845: fatal("Couldn't read from \"%s\": %s", local_path,
1846: strerror(errno));
1.21 djm 1847:
1848: if (len != 0) {
1.151 djm 1849: ack = request_enqueue(&acks, ++id, len, offset);
1.116 djm 1850: sshbuf_reset(msg);
1851: if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
1852: (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
1853: (r = sshbuf_put_string(msg, handle,
1854: handle_len)) != 0 ||
1855: (r = sshbuf_put_u64(msg, offset)) != 0 ||
1856: (r = sshbuf_put_string(msg, data, len)) != 0)
1.137 djm 1857: fatal_fr(r, "compose");
1.116 djm 1858: send_msg(conn, msg);
1.33 deraadt 1859: debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1.37 deraadt 1860: id, (unsigned long long)offset, len);
1.22 djm 1861: } else if (TAILQ_FIRST(&acks) == NULL)
1.1 djm 1862: break;
1863:
1.22 djm 1864: if (ack == NULL)
1865: fatal("Unexpected ACK %u", id);
1866:
1.28 markus 1867: if (id == startid || len == 0 ||
1.23 djm 1868: id - ackid >= conn->num_requests) {
1.116 djm 1869: u_int rid;
1.31 djm 1870:
1.116 djm 1871: sshbuf_reset(msg);
1872: get_msg(conn, msg);
1873: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1874: (r = sshbuf_get_u32(msg, &rid)) != 0)
1.137 djm 1875: fatal_fr(r, "parse");
1.22 djm 1876:
1877: if (type != SSH2_FXP_STATUS)
1878: fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1879: "got %d", SSH2_FXP_STATUS, type);
1880:
1.116 djm 1881: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 djm 1882: fatal_fr(r, "parse status");
1.116 djm 1883: debug3("SSH2_FXP_STATUS %u", status);
1.22 djm 1884:
1885: /* Find the request in our queue */
1.151 djm 1886: if ((ack = request_find(&acks, rid)) == NULL)
1.116 djm 1887: fatal("Can't find request for ID %u", rid);
1.22 djm 1888: TAILQ_REMOVE(&acks, ack, tq);
1.151 djm 1889: debug3("In write loop, ack for %u %zu bytes at %lld",
1890: ack->id, ack->len, (unsigned long long)ack->offset);
1.21 djm 1891: ++ackid;
1.100 dtucker 1892: progress_counter += ack->len;
1.98 djm 1893: free(ack);
1.1 djm 1894: }
1895: offset += len;
1.77 djm 1896: if (offset < 0)
1.137 djm 1897: fatal_f("offset < 0");
1.1 djm 1898: }
1.116 djm 1899: sshbuf_free(msg);
1.80 djm 1900:
1.39 fgsch 1901: if (showprogress)
1902: stop_progress_meter();
1.98 djm 1903: free(data);
1.1 djm 1904:
1.80 djm 1905: if (status != SSH2_FX_OK) {
1906: error("Couldn't write to remote file \"%s\": %s",
1907: remote_path, fx2txt(status));
1.116 djm 1908: status = SSH2_FX_FAILURE;
1.80 djm 1909: }
1910:
1.1 djm 1911: if (close(local_fd) == -1) {
1912: error("Couldn't close local file \"%s\": %s", local_path,
1913: strerror(errno));
1.116 djm 1914: status = SSH2_FX_FAILURE;
1.1 djm 1915: }
1916:
1.10 djm 1917: /* Override umask and utimes if asked */
1.105 djm 1918: if (preserve_flag)
1.23 djm 1919: do_fsetstat(conn, handle, handle_len, &a);
1.10 djm 1920:
1.107 djm 1921: if (fsync_flag)
1922: (void)do_fsync(conn, handle, handle_len);
1923:
1.121 djm 1924: if (do_close(conn, handle, handle_len) != 0)
1.116 djm 1925: status = SSH2_FX_FAILURE;
1926:
1.98 djm 1927: free(handle);
1.5 djm 1928:
1.116 djm 1929: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 1930: }
1.89 djm 1931:
1932: static int
1.116 djm 1933: upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1.150 djm 1934: int depth, int preserve_flag, int print_flag, int resume, int fsync_flag,
1935: int follow_link_flag)
1.89 djm 1936: {
1.116 djm 1937: int ret = 0;
1.89 djm 1938: DIR *dirp;
1939: struct dirent *dp;
1.130 djm 1940: char *filename, *new_src = NULL, *new_dst = NULL;
1.89 djm 1941: struct stat sb;
1.121 djm 1942: Attrib a, *dirattrib;
1.138 dtucker 1943: u_int32_t saved_perm;
1.89 djm 1944:
1945: if (depth >= MAX_DIR_DEPTH) {
1946: error("Maximum directory depth exceeded: %d levels", depth);
1947: return -1;
1948: }
1949:
1950: if (stat(src, &sb) == -1) {
1951: error("Couldn't stat directory \"%s\": %s",
1952: src, strerror(errno));
1953: return -1;
1954: }
1955: if (!S_ISDIR(sb.st_mode)) {
1956: error("\"%s\" is not a directory", src);
1957: return -1;
1958: }
1.147 djm 1959: if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
1.124 schwarze 1960: mprintf("Entering %s\n", src);
1.89 djm 1961:
1962: attrib_clear(&a);
1963: stat_to_attrib(&sb, &a);
1964: a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1965: a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1966: a.perm &= 01777;
1.105 djm 1967: if (!preserve_flag)
1.89 djm 1968: a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1.101 djm 1969:
1.89 djm 1970: /*
1.121 djm 1971: * sftp lacks a portable status value to match errno EEXIST,
1972: * so if we get a failure back then we must check whether
1.138 dtucker 1973: * the path already existed and is a directory. Ensure we can
1974: * write to the directory we create for the duration of the transfer.
1.89 djm 1975: */
1.138 dtucker 1976: saved_perm = a.perm;
1977: a.perm |= (S_IWUSR|S_IXUSR);
1.121 djm 1978: if (do_mkdir(conn, dst, &a, 0) != 0) {
1979: if ((dirattrib = do_stat(conn, dst, 0)) == NULL)
1.89 djm 1980: return -1;
1.121 djm 1981: if (!S_ISDIR(dirattrib->perm)) {
1982: error("\"%s\" exists but is not a directory", dst);
1.89 djm 1983: return -1;
1.121 djm 1984: }
1.89 djm 1985: }
1.138 dtucker 1986: a.perm = saved_perm;
1.89 djm 1987:
1988: if ((dirp = opendir(src)) == NULL) {
1989: error("Failed to open dir \"%s\": %s", src, strerror(errno));
1990: return -1;
1991: }
1.101 djm 1992:
1.89 djm 1993: while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1994: if (dp->d_ino == 0)
1995: continue;
1.130 djm 1996: free(new_dst);
1997: free(new_src);
1.89 djm 1998: filename = dp->d_name;
1999: new_dst = path_append(dst, filename);
2000: new_src = path_append(src, filename);
2001:
1.90 dtucker 2002: if (lstat(new_src, &sb) == -1) {
2003: logit("%s: lstat failed: %s", filename,
2004: strerror(errno));
2005: ret = -1;
2006: } else if (S_ISDIR(sb.st_mode)) {
1.89 djm 2007: if (strcmp(filename, ".") == 0 ||
2008: strcmp(filename, "..") == 0)
2009: continue;
2010:
2011: if (upload_dir_internal(conn, new_src, new_dst,
1.115 logan 2012: depth + 1, preserve_flag, print_flag, resume,
1.150 djm 2013: fsync_flag, follow_link_flag) == -1)
1.89 djm 2014: ret = -1;
1.150 djm 2015: } else if (S_ISREG(sb.st_mode) ||
2016: (follow_link_flag && S_ISLNK(sb.st_mode))) {
1.105 djm 2017: if (do_upload(conn, new_src, new_dst,
1.115 logan 2018: preserve_flag, resume, fsync_flag) == -1) {
1.89 djm 2019: error("Uploading of file %s to %s failed!",
2020: new_src, new_dst);
2021: ret = -1;
2022: }
2023: } else
2024: logit("%s: not a regular file\n", filename);
2025: }
1.130 djm 2026: free(new_dst);
2027: free(new_src);
1.89 djm 2028:
2029: do_setstat(conn, dst, &a);
2030:
2031: (void) closedir(dirp);
2032: return ret;
2033: }
2034:
2035: int
1.116 djm 2036: upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
1.150 djm 2037: int preserve_flag, int print_flag, int resume, int fsync_flag,
2038: int follow_link_flag)
1.89 djm 2039: {
2040: char *dst_canon;
2041: int ret;
2042:
2043: if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1.107 djm 2044: error("Unable to canonicalize path \"%s\"", dst);
1.89 djm 2045: return -1;
2046: }
2047:
1.106 djm 2048: ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
1.150 djm 2049: print_flag, resume, fsync_flag, follow_link_flag);
1.107 djm 2050:
1.98 djm 2051: free(dst_canon);
1.145 djm 2052: return ret;
2053: }
2054:
2055: static void
2056: handle_dest_replies(struct sftp_conn *to, const char *to_path, int synchronous,
2057: u_int *nreqsp, u_int *write_errorp)
2058: {
2059: struct sshbuf *msg;
2060: u_char type;
2061: u_int id, status;
2062: int r;
2063: struct pollfd pfd;
2064:
2065: if ((msg = sshbuf_new()) == NULL)
2066: fatal_f("sshbuf_new failed");
2067:
2068: /* Try to eat replies from the upload side */
2069: while (*nreqsp > 0) {
2070: debug3_f("%u outstanding replies", *nreqsp);
2071: if (!synchronous) {
2072: /* Bail out if no data is ready to be read */
2073: pfd.fd = to->fd_in;
2074: pfd.events = POLLIN;
2075: if ((r = poll(&pfd, 1, 0)) == -1) {
2076: if (errno == EINTR)
2077: break;
2078: fatal_f("poll: %s", strerror(errno));
2079: } else if (r == 0)
2080: break; /* fd not ready */
2081: }
2082: sshbuf_reset(msg);
2083: get_msg(to, msg);
2084:
2085: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
2086: (r = sshbuf_get_u32(msg, &id)) != 0)
2087: fatal_fr(r, "dest parse");
2088: debug3("Received dest reply T:%u I:%u R:%u", type, id, *nreqsp);
2089: if (type != SSH2_FXP_STATUS) {
2090: fatal_f("Expected SSH2_FXP_STATUS(%d) packet, got %d",
2091: SSH2_FXP_STATUS, type);
2092: }
2093: if ((r = sshbuf_get_u32(msg, &status)) != 0)
2094: fatal_fr(r, "parse dest status");
2095: debug3("dest SSH2_FXP_STATUS %u", status);
2096: if (status != SSH2_FX_OK) {
2097: /* record first error */
2098: if (*write_errorp == 0)
2099: *write_errorp = status;
2100: }
2101: /*
2102: * XXX this doesn't do full reply matching like do_upload and
2103: * so cannot gracefully truncate terminated uploads at a
2104: * high-water mark. ATM the only caller of this function (scp)
2105: * doesn't support transfer resumption, so this doesn't matter
2106: * a whole lot.
2107: *
2108: * To be safe, do_crossload truncates the destination file to
2109: * zero length on upload failure, since we can't trust the
2110: * server not to have reordered replies that could have
2111: * inserted holes where none existed in the source file.
2112: *
2113: * XXX we could get a more accutate progress bar if we updated
2114: * the counter based on the reply from the destination...
2115: */
2116: (*nreqsp)--;
2117: }
2118: debug3_f("done: %u outstanding replies", *nreqsp);
2119: }
2120:
2121: int
2122: do_crossload(struct sftp_conn *from, struct sftp_conn *to,
2123: const char *from_path, const char *to_path,
2124: Attrib *a, int preserve_flag)
2125: {
2126: struct sshbuf *msg;
1.152 ! djm 2127: int write_error, read_error, r;
1.145 djm 2128: u_int64_t offset = 0, size;
2129: u_int id, buflen, num_req, max_req, status = SSH2_FX_OK;
2130: u_int num_upload_req;
2131: off_t progress_counter;
2132: u_char *from_handle, *to_handle;
2133: size_t from_handle_len, to_handle_len;
1.146 djm 2134: struct requests requests;
1.145 djm 2135: struct request *req;
2136: u_char type;
2137:
2138: TAILQ_INIT(&requests);
2139:
2140: if (a == NULL && (a = do_stat(from, from_path, 0)) == NULL)
2141: return -1;
2142:
2143: if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
2144: (!S_ISREG(a->perm))) {
2145: error("Cannot download non-regular file: %s", from_path);
2146: return(-1);
2147: }
2148: if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
2149: size = a->size;
2150: else
2151: size = 0;
2152:
2153: buflen = from->download_buflen;
2154: if (buflen > to->upload_buflen)
2155: buflen = to->upload_buflen;
2156:
2157: /* Send open request to read side */
2158: if (send_open(from, from_path, "origin", SSH2_FXF_READ, NULL,
2159: &from_handle, &from_handle_len) != 0)
2160: return -1;
2161:
2162: /* Send open request to write side */
2163: a->flags &= ~SSH2_FILEXFER_ATTR_SIZE;
2164: a->flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
2165: a->perm &= 0777;
2166: if (!preserve_flag)
2167: a->flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
2168: if (send_open(to, to_path, "dest",
2169: SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC, a,
2170: &to_handle, &to_handle_len) != 0) {
2171: do_close(from, from_handle, from_handle_len);
2172: return -1;
2173: }
2174:
2175: /* Read from remote "from" and write to remote "to" */
2176: offset = 0;
2177: write_error = read_error = num_req = num_upload_req = 0;
2178: max_req = 1;
2179: progress_counter = 0;
2180:
2181: if (showprogress && size != 0)
2182: start_progress_meter(from_path, size, &progress_counter);
2183: if ((msg = sshbuf_new()) == NULL)
2184: fatal_f("sshbuf_new failed");
2185: while (num_req > 0 || max_req > 0) {
2186: u_char *data;
2187: size_t len;
2188:
2189: /*
2190: * Simulate EOF on interrupt: stop sending new requests and
2191: * allow outstanding requests to drain gracefully
2192: */
2193: if (interrupted) {
2194: if (num_req == 0) /* If we haven't started yet... */
2195: break;
2196: max_req = 0;
2197: }
2198:
2199: /* Send some more requests */
2200: while (num_req < max_req) {
2201: debug3("Request range %llu -> %llu (%d/%d)",
2202: (unsigned long long)offset,
2203: (unsigned long long)offset + buflen - 1,
2204: num_req, max_req);
1.151 djm 2205: req = request_enqueue(&requests, from->msg_id++,
2206: buflen, offset);
1.145 djm 2207: offset += buflen;
2208: num_req++;
2209: send_read_request(from, req->id, req->offset,
2210: req->len, from_handle, from_handle_len);
2211: }
2212:
2213: /* Try to eat replies from the upload side (nonblocking) */
2214: handle_dest_replies(to, to_path, 0,
2215: &num_upload_req, &write_error);
2216:
2217: sshbuf_reset(msg);
2218: get_msg(from, msg);
2219: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
2220: (r = sshbuf_get_u32(msg, &id)) != 0)
2221: fatal_fr(r, "parse");
2222: debug3("Received origin reply T:%u I:%u R:%d",
2223: type, id, max_req);
2224:
2225: /* Find the request in our queue */
1.151 djm 2226: if ((req = request_find(&requests, id)) == NULL)
1.145 djm 2227: fatal("Unexpected reply %u", id);
2228:
2229: switch (type) {
2230: case SSH2_FXP_STATUS:
2231: if ((r = sshbuf_get_u32(msg, &status)) != 0)
2232: fatal_fr(r, "parse status");
2233: if (status != SSH2_FX_EOF)
2234: read_error = 1;
2235: max_req = 0;
2236: TAILQ_REMOVE(&requests, req, tq);
2237: free(req);
2238: num_req--;
2239: break;
2240: case SSH2_FXP_DATA:
2241: if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
2242: fatal_fr(r, "parse data");
2243: debug3("Received data %llu -> %llu",
2244: (unsigned long long)req->offset,
2245: (unsigned long long)req->offset + len - 1);
2246: if (len > req->len)
2247: fatal("Received more data than asked for "
2248: "%zu > %zu", len, req->len);
2249:
2250: /* Write this chunk out to the destination */
2251: sshbuf_reset(msg);
2252: if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
2253: (r = sshbuf_put_u32(msg, to->msg_id++)) != 0 ||
2254: (r = sshbuf_put_string(msg, to_handle,
2255: to_handle_len)) != 0 ||
2256: (r = sshbuf_put_u64(msg, req->offset)) != 0 ||
2257: (r = sshbuf_put_string(msg, data, len)) != 0)
2258: fatal_fr(r, "compose write");
2259: send_msg(to, msg);
2260: debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%zu",
2261: id, (unsigned long long)offset, len);
2262: num_upload_req++;
2263: progress_counter += len;
2264: free(data);
2265:
2266: if (len == req->len) {
2267: TAILQ_REMOVE(&requests, req, tq);
2268: free(req);
2269: num_req--;
2270: } else {
2271: /* Resend the request for the missing data */
2272: debug3("Short data block, re-requesting "
2273: "%llu -> %llu (%2d)",
2274: (unsigned long long)req->offset + len,
2275: (unsigned long long)req->offset +
2276: req->len - 1, num_req);
2277: req->id = from->msg_id++;
2278: req->len -= len;
2279: req->offset += len;
2280: send_read_request(from, req->id,
2281: req->offset, req->len,
2282: from_handle, from_handle_len);
2283: /* Reduce the request size */
2284: if (len < buflen)
2285: buflen = MAXIMUM(MIN_READ_SIZE, len);
2286: }
2287: if (max_req > 0) { /* max_req = 0 iff EOF received */
2288: if (size > 0 && offset > size) {
2289: /* Only one request at a time
2290: * after the expected EOF */
2291: debug3("Finish at %llu (%2d)",
2292: (unsigned long long)offset,
2293: num_req);
2294: max_req = 1;
2295: } else if (max_req < from->num_requests) {
2296: ++max_req;
2297: }
2298: }
2299: break;
2300: default:
2301: fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
2302: SSH2_FXP_DATA, type);
2303: }
2304: }
2305:
2306: if (showprogress && size)
2307: stop_progress_meter();
2308:
2309: /* Drain replies from the server (blocking) */
2310: debug3_f("waiting for %u replies from destination", num_upload_req);
2311: handle_dest_replies(to, to_path, 1, &num_upload_req, &write_error);
2312:
2313: /* Sanity check */
2314: if (TAILQ_FIRST(&requests) != NULL)
2315: fatal("Transfer complete, but requests still in queue");
2316: /* Truncate at 0 length on interrupt or error to avoid holes at dest */
2317: if (read_error || write_error || interrupted) {
2318: debug("truncating \"%s\" at 0", to_path);
2319: do_close(to, to_handle, to_handle_len);
2320: free(to_handle);
2321: if (send_open(to, to_path, "dest",
2322: SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC, a,
2323: &to_handle, &to_handle_len) != 0) {
2324: error("truncation failed for \"%s\"", to_path);
2325: to_handle = NULL;
2326: }
2327: }
2328: if (read_error) {
2329: error("Couldn't read from origin file \"%s\" : %s",
2330: from_path, fx2txt(status));
2331: status = -1;
2332: do_close(from, from_handle, from_handle_len);
2333: if (to_handle != NULL)
2334: do_close(to, to_handle, to_handle_len);
2335: } else if (write_error) {
2336: error("Couldn't write to \"%s\": %s",
2337: to_path, fx2txt(write_error));
2338: status = SSH2_FX_FAILURE;
2339: do_close(from, from_handle, from_handle_len);
2340: if (to_handle != NULL)
2341: do_close(to, to_handle, to_handle_len);
2342: } else {
2343: if (do_close(from, from_handle, from_handle_len) != 0 ||
2344: interrupted)
2345: status = -1;
2346: else
2347: status = SSH2_FX_OK;
2348: if (to_handle != NULL) {
2349: /* Need to resend utimes after write */
2350: if (preserve_flag)
2351: do_fsetstat(to, to_handle, to_handle_len, a);
2352: do_close(to, to_handle, to_handle_len);
2353: }
2354: }
2355: sshbuf_free(msg);
2356: free(from_handle);
2357: free(to_handle);
2358:
2359: return status == SSH2_FX_OK ? 0 : -1;
2360: }
2361:
2362: static int
2363: crossload_dir_internal(struct sftp_conn *from, struct sftp_conn *to,
2364: const char *from_path, const char *to_path,
1.150 djm 2365: int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
2366: int follow_link_flag)
1.145 djm 2367: {
2368: int i, ret = 0;
2369: SFTP_DIRENT **dir_entries;
2370: char *filename, *new_from_path = NULL, *new_to_path = NULL;
2371: mode_t mode = 0777;
1.149 djm 2372: Attrib curdir;
1.145 djm 2373:
2374: if (depth >= MAX_DIR_DEPTH) {
2375: error("Maximum directory depth exceeded: %d levels", depth);
2376: return -1;
2377: }
2378:
2379: if (dirattrib == NULL &&
2380: (dirattrib = do_stat(from, from_path, 1)) == NULL) {
2381: error("Unable to stat remote directory \"%s\"", from_path);
2382: return -1;
2383: }
2384: if (!S_ISDIR(dirattrib->perm)) {
2385: error("\"%s\" is not a directory", from_path);
2386: return -1;
2387: }
1.150 djm 2388: if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
1.145 djm 2389: mprintf("Retrieving %s\n", from_path);
2390:
1.149 djm 2391: curdir = *dirattrib; /* dirattrib will be clobbered */
2392: curdir.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
2393: curdir.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
2394: if ((curdir.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) == 0) {
2395: debug("Origin did not send permissions for "
1.145 djm 2396: "directory \"%s\"", to_path);
1.149 djm 2397: curdir.perm = S_IWUSR|S_IXUSR;
2398: curdir.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1.145 djm 2399: }
1.149 djm 2400: /* We need to be able to write to the directory while we transfer it */
2401: mode = curdir.perm & 01777;
2402: curdir.perm = mode | (S_IWUSR|S_IXUSR);
2403:
2404: /*
2405: * sftp lacks a portable status value to match errno EEXIST,
2406: * so if we get a failure back then we must check whether
2407: * the path already existed and is a directory. Ensure we can
2408: * write to the directory we create for the duration of the transfer.
2409: */
2410: if (do_mkdir(to, to_path, &curdir, 0) != 0) {
2411: if ((dirattrib = do_stat(to, to_path, 0)) == NULL)
2412: return -1;
2413: if (!S_ISDIR(dirattrib->perm)) {
2414: error("\"%s\" exists but is not a directory", to_path);
2415: return -1;
2416: }
2417: }
2418: curdir.perm = mode;
1.145 djm 2419:
2420: if (do_readdir(from, from_path, &dir_entries) == -1) {
2421: error("%s: Failed to get directory contents", from_path);
2422: return -1;
2423: }
2424:
2425: for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
2426: free(new_from_path);
2427: free(new_to_path);
2428:
2429: filename = dir_entries[i]->filename;
2430: new_from_path = path_append(from_path, filename);
2431: new_to_path = path_append(to_path, filename);
2432:
2433: if (S_ISDIR(dir_entries[i]->a.perm)) {
2434: if (strcmp(filename, ".") == 0 ||
2435: strcmp(filename, "..") == 0)
2436: continue;
2437: if (crossload_dir_internal(from, to,
2438: new_from_path, new_to_path,
2439: depth + 1, &(dir_entries[i]->a), preserve_flag,
1.150 djm 2440: print_flag, follow_link_flag) == -1)
1.145 djm 2441: ret = -1;
1.150 djm 2442: } else if (S_ISREG(dir_entries[i]->a.perm) ||
2443: (follow_link_flag && S_ISLNK(dir_entries[i]->a.perm))) {
2444: /*
2445: * If this is a symlink then don't send the link's
2446: * Attrib. do_download() will do a FXP_STAT operation
2447: * and get the link target's attributes.
2448: */
1.145 djm 2449: if (do_crossload(from, to, new_from_path, new_to_path,
1.150 djm 2450: S_ISLNK(dir_entries[i]->a.perm) ? NULL :
1.145 djm 2451: &(dir_entries[i]->a), preserve_flag) == -1) {
2452: error("Transfer of file %s to %s failed",
2453: new_from_path, new_to_path);
2454: ret = -1;
2455: }
2456: } else
2457: logit("%s: not a regular file\n", new_from_path);
2458:
2459: }
2460: free(new_to_path);
2461: free(new_from_path);
2462:
1.149 djm 2463: do_setstat(to, to_path, &curdir);
1.145 djm 2464:
2465: free_sftp_dirents(dir_entries);
2466:
2467: return ret;
2468: }
2469:
2470: int
2471: crossload_dir(struct sftp_conn *from, struct sftp_conn *to,
2472: const char *from_path, const char *to_path,
1.150 djm 2473: Attrib *dirattrib, int preserve_flag, int print_flag, int follow_link_flag)
1.145 djm 2474: {
2475: char *from_path_canon;
2476: int ret;
2477:
2478: if ((from_path_canon = do_realpath(from, from_path)) == NULL) {
2479: error("Unable to canonicalize path \"%s\"", from_path);
2480: return -1;
2481: }
2482:
2483: ret = crossload_dir_internal(from, to, from_path_canon, to_path, 0,
1.150 djm 2484: dirattrib, preserve_flag, print_flag, follow_link_flag);
1.145 djm 2485: free(from_path_canon);
1.89 djm 2486: return ret;
2487: }
2488:
2489: char *
1.116 djm 2490: path_append(const char *p1, const char *p2)
1.89 djm 2491: {
2492: char *ret;
2493: size_t len = strlen(p1) + strlen(p2) + 2;
2494:
2495: ret = xmalloc(len);
2496: strlcpy(ret, p1, len);
2497: if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
2498: strlcat(ret, "/", len);
2499: strlcat(ret, p2, len);
2500:
2501: return(ret);
1.139 djm 2502: }
2503:
2504: char *
2505: make_absolute(char *p, const char *pwd)
2506: {
2507: char *abs_str;
2508:
2509: /* Derelativise */
2510: if (p && !path_absolute(p)) {
2511: abs_str = path_append(pwd, p);
2512: free(p);
2513: return(abs_str);
2514: } else
2515: return(p);
2516: }
2517:
2518: int
2519: remote_is_dir(struct sftp_conn *conn, const char *path)
2520: {
2521: Attrib *a;
2522:
2523: /* XXX: report errors? */
2524: if ((a = do_stat(conn, path, 1)) == NULL)
2525: return(0);
2526: if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
2527: return(0);
2528: return(S_ISDIR(a->perm));
2529: }
2530:
2531:
2532: int
2533: local_is_dir(const char *path)
2534: {
2535: struct stat sb;
2536:
2537: /* XXX: report errors? */
2538: if (stat(path, &sb) == -1)
2539: return(0);
2540:
2541: return(S_ISDIR(sb.st_mode));
2542: }
2543:
2544: /* Check whether path returned from glob(..., GLOB_MARK, ...) is a directory */
2545: int
2546: globpath_is_dir(const char *pathname)
2547: {
2548: size_t l = strlen(pathname);
2549:
2550: return l > 0 && pathname[l - 1] == '/';
1.89 djm 2551: }
2552: