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