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