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