Annotation of src/usr.bin/ssh/sftp-client.c, Revision 1.169
1.169 ! guenther 1: /* $OpenBSD: sftp-client.c,v 1.168 2023/01/11 05:39:38 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.101 djm 1583: u_int64_t offset = 0, size, highwater;
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: }
1630: offset = highwater = 0;
1.105 djm 1631: if (resume_flag) {
1.101 djm 1632: if (fstat(local_fd, &st) == -1) {
1.161 djm 1633: error("stat local \"%s\": %s",
1.101 djm 1634: local_path, strerror(errno));
1635: goto fail;
1636: }
1.113 djm 1637: if (st.st_size < 0) {
1638: error("\"%s\" has negative size", local_path);
1639: goto fail;
1640: }
1641: if ((u_int64_t)st.st_size > size) {
1.101 djm 1642: error("Unable to resume download of \"%s\": "
1643: "local file is larger than remote", local_path);
1644: fail:
1645: do_close(conn, handle, handle_len);
1646: free(handle);
1.110 djm 1647: if (local_fd != -1)
1648: close(local_fd);
1.101 djm 1649: return -1;
1650: }
1651: offset = highwater = st.st_size;
1.23 djm 1652: }
1653:
1.1 djm 1654: /* Read from remote and write to local */
1.101 djm 1655: write_error = read_error = write_errno = num_req = 0;
1.21 djm 1656: max_req = 1;
1.101 djm 1657: progress_counter = offset;
1.39 fgsch 1658:
1.153 djm 1659: if (showprogress && size != 0) {
1660: start_progress_meter(progress_meter_path(remote_path),
1661: size, &progress_counter);
1662: }
1.39 fgsch 1663:
1.144 djm 1664: if ((msg = sshbuf_new()) == NULL)
1665: fatal_f("sshbuf_new failed");
1666:
1.21 djm 1667: while (num_req > 0 || max_req > 0) {
1.116 djm 1668: u_char *data;
1669: size_t len;
1.1 djm 1670:
1.49 djm 1671: /*
1.51 deraadt 1672: * Simulate EOF on interrupt: stop sending new requests and
1.49 djm 1673: * allow outstanding requests to drain gracefully
1674: */
1675: if (interrupted) {
1676: if (num_req == 0) /* If we haven't started yet... */
1677: break;
1678: max_req = 0;
1679: }
1680:
1.21 djm 1681: /* Send some more requests */
1682: while (num_req < max_req) {
1.28 markus 1683: debug3("Request range %llu -> %llu (%d/%d)",
1.25 itojun 1684: (unsigned long long)offset,
1685: (unsigned long long)offset + buflen - 1,
1686: num_req, max_req);
1.151 djm 1687: req = request_enqueue(&requests, conn->msg_id++,
1688: buflen, offset);
1.21 djm 1689: offset += buflen;
1690: num_req++;
1.93 djm 1691: send_read_request(conn, req->id, req->offset,
1.21 djm 1692: req->len, handle, handle_len);
1693: }
1.1 djm 1694:
1.116 djm 1695: sshbuf_reset(msg);
1696: get_msg(conn, msg);
1697: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1698: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 djm 1699: fatal_fr(r, "parse");
1.33 deraadt 1700: debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1.21 djm 1701:
1702: /* Find the request in our queue */
1.151 djm 1703: if ((req = request_find(&requests, id)) == NULL)
1.21 djm 1704: fatal("Unexpected reply %u", id);
1705:
1706: switch (type) {
1707: case SSH2_FXP_STATUS:
1.116 djm 1708: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 djm 1709: fatal_fr(r, "parse status");
1.21 djm 1710: if (status != SSH2_FX_EOF)
1711: read_error = 1;
1712: max_req = 0;
1713: TAILQ_REMOVE(&requests, req, tq);
1.98 djm 1714: free(req);
1.21 djm 1715: num_req--;
1716: break;
1717: case SSH2_FXP_DATA:
1.116 djm 1718: if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1.137 djm 1719: fatal_fr(r, "parse data");
1.26 itojun 1720: debug3("Received data %llu -> %llu",
1.28 markus 1721: (unsigned long long)req->offset,
1.26 itojun 1722: (unsigned long long)req->offset + len - 1);
1.21 djm 1723: if (len > req->len)
1724: fatal("Received more data than asked for "
1.116 djm 1725: "%zu > %zu", len, req->len);
1.134 djm 1726: lmodified = 1;
1.21 djm 1727: if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1.44 deraadt 1728: atomicio(vwrite, local_fd, data, len) != len) &&
1.21 djm 1729: !write_error) {
1730: write_errno = errno;
1731: write_error = 1;
1732: max_req = 0;
1733: }
1.101 djm 1734: else if (!reordered && req->offset <= highwater)
1735: highwater = req->offset + len;
1736: else if (!reordered && req->offset > highwater)
1737: reordered = 1;
1.39 fgsch 1738: progress_counter += len;
1.98 djm 1739: free(data);
1.1 djm 1740:
1.21 djm 1741: if (len == req->len) {
1742: TAILQ_REMOVE(&requests, req, tq);
1.98 djm 1743: free(req);
1.21 djm 1744: num_req--;
1745: } else {
1746: /* Resend the request for the missing data */
1747: debug3("Short data block, re-requesting "
1.26 itojun 1748: "%llu -> %llu (%2d)",
1.28 markus 1749: (unsigned long long)req->offset + len,
1.27 itojun 1750: (unsigned long long)req->offset +
1751: req->len - 1, num_req);
1.23 djm 1752: req->id = conn->msg_id++;
1.21 djm 1753: req->len -= len;
1754: req->offset += len;
1.93 djm 1755: send_read_request(conn, req->id,
1.23 djm 1756: req->offset, req->len, handle, handle_len);
1.21 djm 1757: /* Reduce the request size */
1758: if (len < buflen)
1.125 deraadt 1759: buflen = MAXIMUM(MIN_READ_SIZE, len);
1.21 djm 1760: }
1761: if (max_req > 0) { /* max_req = 0 iff EOF received */
1762: if (size > 0 && offset > size) {
1763: /* Only one request at a time
1764: * after the expected EOF */
1765: debug3("Finish at %llu (%2d)",
1.26 itojun 1766: (unsigned long long)offset,
1767: num_req);
1.21 djm 1768: max_req = 1;
1.136 djm 1769: } else if (max_req < conn->num_requests) {
1.21 djm 1770: ++max_req;
1771: }
1.1 djm 1772: }
1.21 djm 1773: break;
1774: default:
1.33 deraadt 1775: fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1.1 djm 1776: SSH2_FXP_DATA, type);
1777: }
1.21 djm 1778: }
1.1 djm 1779:
1.39 fgsch 1780: if (showprogress && size)
1781: stop_progress_meter();
1782:
1.21 djm 1783: /* Sanity check */
1784: if (TAILQ_FIRST(&requests) != NULL)
1785: fatal("Transfer complete, but requests still in queue");
1.164 djm 1786: /*
1787: * Truncate at highest contiguous point to avoid holes on interrupt,
1788: * or unconditionally if writing in place.
1789: */
1790: if (inplace_flag || read_error || write_error || interrupted) {
1.105 djm 1791: if (reordered && resume_flag) {
1.101 djm 1792: error("Unable to resume download of \"%s\": "
1793: "server reordered requests", local_path);
1794: }
1795: debug("truncating at %llu", (unsigned long long)highwater);
1.120 djm 1796: if (ftruncate(local_fd, highwater) == -1)
1.161 djm 1797: error("local ftruncate \"%s\": %s", local_path,
1.120 djm 1798: strerror(errno));
1.101 djm 1799: }
1.21 djm 1800: if (read_error) {
1.161 djm 1801: error("read remote \"%s\" : %s", remote_path, fx2txt(status));
1.103 djm 1802: status = -1;
1.23 djm 1803: do_close(conn, handle, handle_len);
1.21 djm 1804: } else if (write_error) {
1.161 djm 1805: error("write local \"%s\": %s", local_path,
1.21 djm 1806: strerror(write_errno));
1.116 djm 1807: status = SSH2_FX_FAILURE;
1.23 djm 1808: do_close(conn, handle, handle_len);
1.21 djm 1809: } else {
1.116 djm 1810: if (do_close(conn, handle, handle_len) != 0 || interrupted)
1811: status = SSH2_FX_FAILURE;
1812: else
1813: status = SSH2_FX_OK;
1.21 djm 1814: /* Override umask and utimes if asked */
1.105 djm 1815: if (preserve_flag && fchmod(local_fd, mode) == -1)
1.161 djm 1816: error("local chmod \"%s\": %s", local_path,
1.37 deraadt 1817: strerror(errno));
1.105 djm 1818: if (preserve_flag &&
1819: (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1.21 djm 1820: struct timeval tv[2];
1821: tv[0].tv_sec = a->atime;
1822: tv[1].tv_sec = a->mtime;
1823: tv[0].tv_usec = tv[1].tv_usec = 0;
1824: if (utimes(local_path, tv) == -1)
1.161 djm 1825: error("local set times \"%s\": %s",
1.37 deraadt 1826: local_path, strerror(errno));
1.1 djm 1827: }
1.134 djm 1828: if (resume_flag && !lmodified)
1829: logit("File \"%s\" was not modified", local_path);
1830: else if (fsync_flag) {
1.107 djm 1831: debug("syncing \"%s\"", local_path);
1832: if (fsync(local_fd) == -1)
1.161 djm 1833: error("local sync \"%s\": %s",
1.107 djm 1834: local_path, strerror(errno));
1835: }
1.10 djm 1836: }
1.5 djm 1837: close(local_fd);
1.116 djm 1838: sshbuf_free(msg);
1.98 djm 1839: free(handle);
1.23 djm 1840:
1.129 djm 1841: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 1842: }
1843:
1.89 djm 1844: static int
1.116 djm 1845: download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1846: int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
1.163 djm 1847: int resume_flag, int fsync_flag, int follow_link_flag, int inplace_flag)
1.89 djm 1848: {
1849: int i, ret = 0;
1850: SFTP_DIRENT **dir_entries;
1.130 djm 1851: char *filename, *new_src = NULL, *new_dst = NULL;
1.138 dtucker 1852: mode_t mode = 0777, tmpmode = mode;
1.89 djm 1853:
1854: if (depth >= MAX_DIR_DEPTH) {
1855: error("Maximum directory depth exceeded: %d levels", depth);
1856: return -1;
1857: }
1858:
1.161 djm 1859: debug2_f("download dir remote \"%s\" to local \"%s\"", src, dst);
1860:
1.89 djm 1861: if (dirattrib == NULL &&
1862: (dirattrib = do_stat(conn, src, 1)) == NULL) {
1.161 djm 1863: error("stat remote \"%s\" directory failed", src);
1.89 djm 1864: return -1;
1865: }
1866: if (!S_ISDIR(dirattrib->perm)) {
1867: error("\"%s\" is not a directory", src);
1868: return -1;
1869: }
1.147 djm 1870: if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
1.124 schwarze 1871: mprintf("Retrieving %s\n", src);
1.89 djm 1872:
1.138 dtucker 1873: if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
1.89 djm 1874: mode = dirattrib->perm & 01777;
1.138 dtucker 1875: tmpmode = mode | (S_IWUSR|S_IXUSR);
1876: } else {
1.161 djm 1877: debug("download remote \"%s\": server "
1878: "did not send permissions", dst);
1.89 djm 1879: }
1880:
1.138 dtucker 1881: if (mkdir(dst, tmpmode) == -1 && errno != EEXIST) {
1.89 djm 1882: error("mkdir %s: %s", dst, strerror(errno));
1883: return -1;
1884: }
1885:
1886: if (do_readdir(conn, src, &dir_entries) == -1) {
1.161 djm 1887: error("remote readdir \"%s\" failed", src);
1.89 djm 1888: return -1;
1889: }
1890:
1891: for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1.130 djm 1892: free(new_dst);
1893: free(new_src);
1894:
1.89 djm 1895: filename = dir_entries[i]->filename;
1896: new_dst = path_append(dst, filename);
1897: new_src = path_append(src, filename);
1898:
1899: if (S_ISDIR(dir_entries[i]->a.perm)) {
1900: if (strcmp(filename, ".") == 0 ||
1901: strcmp(filename, "..") == 0)
1902: continue;
1903: if (download_dir_internal(conn, new_src, new_dst,
1.105 djm 1904: depth + 1, &(dir_entries[i]->a), preserve_flag,
1.150 djm 1905: print_flag, resume_flag,
1.163 djm 1906: fsync_flag, follow_link_flag, inplace_flag) == -1)
1.89 djm 1907: ret = -1;
1.150 djm 1908: } else if (S_ISREG(dir_entries[i]->a.perm) ||
1909: (follow_link_flag && S_ISLNK(dir_entries[i]->a.perm))) {
1910: /*
1911: * If this is a symlink then don't send the link's
1912: * Attrib. do_download() will do a FXP_STAT operation
1913: * and get the link target's attributes.
1914: */
1.89 djm 1915: if (do_download(conn, new_src, new_dst,
1.150 djm 1916: S_ISLNK(dir_entries[i]->a.perm) ? NULL :
1917: &(dir_entries[i]->a),
1.163 djm 1918: preserve_flag, resume_flag, fsync_flag,
1919: inplace_flag) == -1) {
1.89 djm 1920: error("Download of file %s to %s failed",
1921: new_src, new_dst);
1922: ret = -1;
1923: }
1924: } else
1.161 djm 1925: logit("download \"%s\": not a regular file", new_src);
1.89 djm 1926:
1927: }
1.130 djm 1928: free(new_dst);
1929: free(new_src);
1.89 djm 1930:
1.105 djm 1931: if (preserve_flag) {
1.89 djm 1932: if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1933: struct timeval tv[2];
1934: tv[0].tv_sec = dirattrib->atime;
1935: tv[1].tv_sec = dirattrib->mtime;
1936: tv[0].tv_usec = tv[1].tv_usec = 0;
1937: if (utimes(dst, tv) == -1)
1.161 djm 1938: error("local set times on \"%s\": %s",
1.89 djm 1939: dst, strerror(errno));
1940: } else
1941: debug("Server did not send times for directory "
1942: "\"%s\"", dst);
1943: }
1944:
1.138 dtucker 1945: if (mode != tmpmode && chmod(dst, mode) == -1)
1.161 djm 1946: error("local chmod directory \"%s\": %s", dst,
1.138 dtucker 1947: strerror(errno));
1948:
1.89 djm 1949: free_sftp_dirents(dir_entries);
1950:
1951: return ret;
1952: }
1953:
1954: int
1.116 djm 1955: download_dir(struct sftp_conn *conn, const char *src, const char *dst,
1956: Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1.163 djm 1957: int fsync_flag, int follow_link_flag, int inplace_flag)
1.89 djm 1958: {
1959: char *src_canon;
1960: int ret;
1961:
1962: if ((src_canon = do_realpath(conn, src)) == NULL) {
1.161 djm 1963: error("download \"%s\": path canonicalization failed", src);
1.89 djm 1964: return -1;
1965: }
1966:
1.105 djm 1967: ret = download_dir_internal(conn, src_canon, dst, 0,
1.150 djm 1968: dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag,
1.163 djm 1969: follow_link_flag, inplace_flag);
1.98 djm 1970: free(src_canon);
1.89 djm 1971: return ret;
1972: }
1973:
1.1 djm 1974: int
1.116 djm 1975: do_upload(struct sftp_conn *conn, const char *local_path,
1.163 djm 1976: const char *remote_path, int preserve_flag, int resume,
1977: int fsync_flag, int inplace_flag)
1.1 djm 1978: {
1.116 djm 1979: int r, local_fd;
1.163 djm 1980: u_int openmode, id, status = SSH2_FX_OK, reordered = 0;
1.100 dtucker 1981: off_t offset, progress_counter;
1.163 djm 1982: u_char type, *handle, *data;
1.116 djm 1983: struct sshbuf *msg;
1.1 djm 1984: struct stat sb;
1.163 djm 1985: Attrib a, t, *c = NULL;
1986: u_int32_t startid, ackid;
1987: u_int64_t highwater = 0;
1.151 djm 1988: struct request *ack = NULL;
1989: struct requests acks;
1.116 djm 1990: size_t handle_len;
1.22 djm 1991:
1.161 djm 1992: debug2_f("upload local \"%s\" to remote \"%s\"",
1993: local_path, remote_path);
1994:
1.22 djm 1995: TAILQ_INIT(&acks);
1.1 djm 1996:
1.156 deraadt 1997: if ((local_fd = open(local_path, O_RDONLY)) == -1) {
1.161 djm 1998: error("open local \"%s\": %s", local_path, strerror(errno));
1.1 djm 1999: return(-1);
2000: }
2001: if (fstat(local_fd, &sb) == -1) {
1.161 djm 2002: error("fstat local \"%s\": %s", local_path, strerror(errno));
1.41 djm 2003: close(local_fd);
2004: return(-1);
2005: }
2006: if (!S_ISREG(sb.st_mode)) {
1.161 djm 2007: error("local \"%s\" is not a regular file", local_path);
1.1 djm 2008: close(local_fd);
2009: return(-1);
2010: }
2011: stat_to_attrib(&sb, &a);
2012:
2013: a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
2014: a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
2015: a.perm &= 0777;
1.105 djm 2016: if (!preserve_flag)
1.1 djm 2017: a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
2018:
1.115 logan 2019: if (resume) {
2020: /* Get remote file size if it exists */
2021: if ((c = do_stat(conn, remote_path, 0)) == NULL) {
1.122 djm 2022: close(local_fd);
1.115 logan 2023: return -1;
2024: }
2025:
2026: if ((off_t)c->size >= sb.st_size) {
1.161 djm 2027: error("resume \"%s\": destination file "
2028: "same size or larger", local_path);
1.115 logan 2029: close(local_fd);
2030: return -1;
2031: }
2032:
2033: if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
2034: close(local_fd);
2035: return -1;
2036: }
2037: }
2038:
1.163 djm 2039: openmode = SSH2_FXF_WRITE|SSH2_FXF_CREAT;
2040: if (resume)
2041: openmode |= SSH2_FXF_APPEND;
2042: else if (!inplace_flag)
2043: openmode |= SSH2_FXF_TRUNC;
2044:
1.1 djm 2045: /* Send open request */
1.163 djm 2046: if (send_open(conn, remote_path, "dest", openmode, &a,
2047: &handle, &handle_len) != 0) {
1.1 djm 2048: close(local_fd);
1.80 djm 2049: return -1;
1.1 djm 2050: }
2051:
1.144 djm 2052: id = conn->msg_id;
1.21 djm 2053: startid = ackid = id + 1;
1.141 djm 2054: data = xmalloc(conn->upload_buflen);
1.20 djm 2055:
1.1 djm 2056: /* Read from local and write to remote */
1.115 logan 2057: offset = progress_counter = (resume ? c->size : 0);
1.153 djm 2058: if (showprogress) {
2059: start_progress_meter(progress_meter_path(local_path),
2060: sb.st_size, &progress_counter);
2061: }
1.39 fgsch 2062:
1.144 djm 2063: if ((msg = sshbuf_new()) == NULL)
2064: fatal_f("sshbuf_new failed");
1.19 deraadt 2065: for (;;) {
1.1 djm 2066: int len;
2067:
2068: /*
1.51 deraadt 2069: * Can't use atomicio here because it returns 0 on EOF,
1.49 djm 2070: * thus losing the last block of the file.
1.51 deraadt 2071: * Simulate an EOF on interrupt, allowing ACKs from the
1.49 djm 2072: * server to drain.
1.1 djm 2073: */
1.80 djm 2074: if (interrupted || status != SSH2_FX_OK)
1.49 djm 2075: len = 0;
2076: else do
1.141 djm 2077: len = read(local_fd, data, conn->upload_buflen);
1.1 djm 2078: while ((len == -1) && (errno == EINTR || errno == EAGAIN));
2079:
1.161 djm 2080: if (len == -1) {
2081: fatal("read local \"%s\": %s",
2082: local_path, strerror(errno));
2083: } else if (len != 0) {
1.151 djm 2084: ack = request_enqueue(&acks, ++id, len, offset);
1.116 djm 2085: sshbuf_reset(msg);
2086: if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
2087: (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
2088: (r = sshbuf_put_string(msg, handle,
2089: handle_len)) != 0 ||
2090: (r = sshbuf_put_u64(msg, offset)) != 0 ||
2091: (r = sshbuf_put_string(msg, data, len)) != 0)
1.137 djm 2092: fatal_fr(r, "compose");
1.116 djm 2093: send_msg(conn, msg);
1.33 deraadt 2094: debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1.37 deraadt 2095: id, (unsigned long long)offset, len);
1.22 djm 2096: } else if (TAILQ_FIRST(&acks) == NULL)
1.1 djm 2097: break;
2098:
1.22 djm 2099: if (ack == NULL)
2100: fatal("Unexpected ACK %u", id);
2101:
1.28 markus 2102: if (id == startid || len == 0 ||
1.23 djm 2103: id - ackid >= conn->num_requests) {
1.116 djm 2104: u_int rid;
1.31 djm 2105:
1.116 djm 2106: sshbuf_reset(msg);
2107: get_msg(conn, msg);
2108: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
2109: (r = sshbuf_get_u32(msg, &rid)) != 0)
1.137 djm 2110: fatal_fr(r, "parse");
1.22 djm 2111:
2112: if (type != SSH2_FXP_STATUS)
2113: fatal("Expected SSH2_FXP_STATUS(%d) packet, "
2114: "got %d", SSH2_FXP_STATUS, type);
2115:
1.116 djm 2116: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 djm 2117: fatal_fr(r, "parse status");
1.116 djm 2118: debug3("SSH2_FXP_STATUS %u", status);
1.22 djm 2119:
2120: /* Find the request in our queue */
1.151 djm 2121: if ((ack = request_find(&acks, rid)) == NULL)
1.116 djm 2122: fatal("Can't find request for ID %u", rid);
1.22 djm 2123: TAILQ_REMOVE(&acks, ack, tq);
1.151 djm 2124: debug3("In write loop, ack for %u %zu bytes at %lld",
2125: ack->id, ack->len, (unsigned long long)ack->offset);
1.21 djm 2126: ++ackid;
1.100 dtucker 2127: progress_counter += ack->len;
1.163 djm 2128: if (!reordered && ack->offset <= highwater)
2129: highwater = ack->offset + ack->len;
2130: else if (!reordered && ack->offset > highwater) {
2131: debug3_f("server reordered ACKs");
2132: reordered = 1;
2133: }
1.98 djm 2134: free(ack);
1.1 djm 2135: }
2136: offset += len;
1.77 djm 2137: if (offset < 0)
1.137 djm 2138: fatal_f("offset < 0");
1.1 djm 2139: }
1.116 djm 2140: sshbuf_free(msg);
1.80 djm 2141:
1.39 fgsch 2142: if (showprogress)
2143: stop_progress_meter();
1.98 djm 2144: free(data);
1.1 djm 2145:
1.80 djm 2146: if (status != SSH2_FX_OK) {
1.161 djm 2147: error("write remote \"%s\": %s", remote_path, fx2txt(status));
1.116 djm 2148: status = SSH2_FX_FAILURE;
1.80 djm 2149: }
2150:
1.164 djm 2151: if (inplace_flag || (resume && (status != SSH2_FX_OK || interrupted))) {
1.163 djm 2152: debug("truncating at %llu", (unsigned long long)highwater);
2153: attrib_clear(&t);
2154: t.flags = SSH2_FILEXFER_ATTR_SIZE;
2155: t.size = highwater;
1.164 djm 2156: do_fsetstat(conn, handle, handle_len, &t);
1.163 djm 2157: }
2158:
1.1 djm 2159: if (close(local_fd) == -1) {
1.161 djm 2160: error("close local \"%s\": %s", local_path, strerror(errno));
1.116 djm 2161: status = SSH2_FX_FAILURE;
1.1 djm 2162: }
2163:
1.10 djm 2164: /* Override umask and utimes if asked */
1.105 djm 2165: if (preserve_flag)
1.23 djm 2166: do_fsetstat(conn, handle, handle_len, &a);
1.10 djm 2167:
1.107 djm 2168: if (fsync_flag)
2169: (void)do_fsync(conn, handle, handle_len);
2170:
1.121 djm 2171: if (do_close(conn, handle, handle_len) != 0)
1.116 djm 2172: status = SSH2_FX_FAILURE;
2173:
1.98 djm 2174: free(handle);
1.5 djm 2175:
1.116 djm 2176: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 2177: }
1.89 djm 2178:
2179: static int
1.116 djm 2180: upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1.150 djm 2181: int depth, int preserve_flag, int print_flag, int resume, int fsync_flag,
1.163 djm 2182: int follow_link_flag, int inplace_flag)
1.89 djm 2183: {
1.116 djm 2184: int ret = 0;
1.89 djm 2185: DIR *dirp;
2186: struct dirent *dp;
1.130 djm 2187: char *filename, *new_src = NULL, *new_dst = NULL;
1.89 djm 2188: struct stat sb;
1.121 djm 2189: Attrib a, *dirattrib;
1.138 dtucker 2190: u_int32_t saved_perm;
1.89 djm 2191:
1.161 djm 2192: debug2_f("upload local dir \"%s\" to remote \"%s\"", src, dst);
2193:
1.89 djm 2194: if (depth >= MAX_DIR_DEPTH) {
2195: error("Maximum directory depth exceeded: %d levels", depth);
2196: return -1;
2197: }
2198:
2199: if (stat(src, &sb) == -1) {
1.161 djm 2200: error("stat local \"%s\": %s", src, strerror(errno));
1.89 djm 2201: return -1;
2202: }
2203: if (!S_ISDIR(sb.st_mode)) {
2204: error("\"%s\" is not a directory", src);
2205: return -1;
2206: }
1.147 djm 2207: if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
1.124 schwarze 2208: mprintf("Entering %s\n", src);
1.89 djm 2209:
2210: stat_to_attrib(&sb, &a);
2211: a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
2212: a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
2213: a.perm &= 01777;
1.105 djm 2214: if (!preserve_flag)
1.89 djm 2215: a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1.101 djm 2216:
1.89 djm 2217: /*
1.121 djm 2218: * sftp lacks a portable status value to match errno EEXIST,
2219: * so if we get a failure back then we must check whether
1.138 dtucker 2220: * the path already existed and is a directory. Ensure we can
2221: * write to the directory we create for the duration of the transfer.
1.89 djm 2222: */
1.138 dtucker 2223: saved_perm = a.perm;
2224: a.perm |= (S_IWUSR|S_IXUSR);
1.121 djm 2225: if (do_mkdir(conn, dst, &a, 0) != 0) {
2226: if ((dirattrib = do_stat(conn, dst, 0)) == NULL)
1.89 djm 2227: return -1;
1.121 djm 2228: if (!S_ISDIR(dirattrib->perm)) {
2229: error("\"%s\" exists but is not a directory", dst);
1.89 djm 2230: return -1;
1.121 djm 2231: }
1.89 djm 2232: }
1.138 dtucker 2233: a.perm = saved_perm;
1.89 djm 2234:
2235: if ((dirp = opendir(src)) == NULL) {
1.161 djm 2236: error("local opendir \"%s\": %s", src, strerror(errno));
1.89 djm 2237: return -1;
2238: }
1.101 djm 2239:
1.89 djm 2240: while (((dp = readdir(dirp)) != NULL) && !interrupted) {
2241: if (dp->d_ino == 0)
2242: continue;
1.130 djm 2243: free(new_dst);
2244: free(new_src);
1.89 djm 2245: filename = dp->d_name;
2246: new_dst = path_append(dst, filename);
2247: new_src = path_append(src, filename);
2248:
1.90 dtucker 2249: if (lstat(new_src, &sb) == -1) {
1.161 djm 2250: logit("local lstat \"%s\": %s", filename,
1.90 dtucker 2251: strerror(errno));
2252: ret = -1;
2253: } else if (S_ISDIR(sb.st_mode)) {
1.89 djm 2254: if (strcmp(filename, ".") == 0 ||
2255: strcmp(filename, "..") == 0)
2256: continue;
2257:
2258: if (upload_dir_internal(conn, new_src, new_dst,
1.115 logan 2259: depth + 1, preserve_flag, print_flag, resume,
1.163 djm 2260: fsync_flag, follow_link_flag, inplace_flag) == -1)
1.89 djm 2261: ret = -1;
1.150 djm 2262: } else if (S_ISREG(sb.st_mode) ||
2263: (follow_link_flag && S_ISLNK(sb.st_mode))) {
1.105 djm 2264: if (do_upload(conn, new_src, new_dst,
1.163 djm 2265: preserve_flag, resume, fsync_flag,
2266: inplace_flag) == -1) {
1.161 djm 2267: error("upload \"%s\" to \"%s\" failed",
1.89 djm 2268: new_src, new_dst);
2269: ret = -1;
2270: }
2271: } else
1.161 djm 2272: logit("%s: not a regular file", filename);
1.89 djm 2273: }
1.130 djm 2274: free(new_dst);
2275: free(new_src);
1.89 djm 2276:
2277: do_setstat(conn, dst, &a);
2278:
2279: (void) closedir(dirp);
2280: return ret;
2281: }
2282:
2283: int
1.116 djm 2284: upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
1.150 djm 2285: int preserve_flag, int print_flag, int resume, int fsync_flag,
1.163 djm 2286: int follow_link_flag, int inplace_flag)
1.89 djm 2287: {
2288: char *dst_canon;
2289: int ret;
2290:
2291: if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1.161 djm 2292: error("upload \"%s\": path canonicalization failed", dst);
1.89 djm 2293: return -1;
2294: }
2295:
1.106 djm 2296: ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
1.163 djm 2297: print_flag, resume, fsync_flag, follow_link_flag, inplace_flag);
1.107 djm 2298:
1.98 djm 2299: free(dst_canon);
1.145 djm 2300: return ret;
2301: }
2302:
2303: static void
2304: handle_dest_replies(struct sftp_conn *to, const char *to_path, int synchronous,
2305: u_int *nreqsp, u_int *write_errorp)
2306: {
2307: struct sshbuf *msg;
2308: u_char type;
2309: u_int id, status;
2310: int r;
2311: struct pollfd pfd;
2312:
2313: if ((msg = sshbuf_new()) == NULL)
2314: fatal_f("sshbuf_new failed");
2315:
2316: /* Try to eat replies from the upload side */
2317: while (*nreqsp > 0) {
2318: debug3_f("%u outstanding replies", *nreqsp);
2319: if (!synchronous) {
2320: /* Bail out if no data is ready to be read */
2321: pfd.fd = to->fd_in;
2322: pfd.events = POLLIN;
2323: if ((r = poll(&pfd, 1, 0)) == -1) {
2324: if (errno == EINTR)
2325: break;
2326: fatal_f("poll: %s", strerror(errno));
2327: } else if (r == 0)
2328: break; /* fd not ready */
2329: }
2330: sshbuf_reset(msg);
2331: get_msg(to, msg);
2332:
2333: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
2334: (r = sshbuf_get_u32(msg, &id)) != 0)
2335: fatal_fr(r, "dest parse");
2336: debug3("Received dest reply T:%u I:%u R:%u", type, id, *nreqsp);
2337: if (type != SSH2_FXP_STATUS) {
2338: fatal_f("Expected SSH2_FXP_STATUS(%d) packet, got %d",
2339: SSH2_FXP_STATUS, type);
2340: }
2341: if ((r = sshbuf_get_u32(msg, &status)) != 0)
2342: fatal_fr(r, "parse dest status");
2343: debug3("dest SSH2_FXP_STATUS %u", status);
2344: if (status != SSH2_FX_OK) {
2345: /* record first error */
2346: if (*write_errorp == 0)
2347: *write_errorp = status;
2348: }
2349: /*
2350: * XXX this doesn't do full reply matching like do_upload and
2351: * so cannot gracefully truncate terminated uploads at a
2352: * high-water mark. ATM the only caller of this function (scp)
2353: * doesn't support transfer resumption, so this doesn't matter
2354: * a whole lot.
2355: *
2356: * To be safe, do_crossload truncates the destination file to
2357: * zero length on upload failure, since we can't trust the
2358: * server not to have reordered replies that could have
2359: * inserted holes where none existed in the source file.
2360: *
2361: * XXX we could get a more accutate progress bar if we updated
2362: * the counter based on the reply from the destination...
2363: */
2364: (*nreqsp)--;
2365: }
2366: debug3_f("done: %u outstanding replies", *nreqsp);
1.157 dtucker 2367: sshbuf_free(msg);
1.145 djm 2368: }
2369:
2370: int
2371: do_crossload(struct sftp_conn *from, struct sftp_conn *to,
2372: const char *from_path, const char *to_path,
2373: Attrib *a, int preserve_flag)
2374: {
2375: struct sshbuf *msg;
1.152 djm 2376: int write_error, read_error, r;
1.145 djm 2377: u_int64_t offset = 0, size;
2378: u_int id, buflen, num_req, max_req, status = SSH2_FX_OK;
2379: u_int num_upload_req;
2380: off_t progress_counter;
2381: u_char *from_handle, *to_handle;
2382: size_t from_handle_len, to_handle_len;
1.146 djm 2383: struct requests requests;
1.145 djm 2384: struct request *req;
2385: u_char type;
2386:
1.161 djm 2387: debug2_f("crossload src \"%s\" to dst \"%s\"", from_path, to_path);
2388:
1.145 djm 2389: TAILQ_INIT(&requests);
2390:
2391: if (a == NULL && (a = do_stat(from, from_path, 0)) == NULL)
2392: return -1;
2393:
2394: if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
2395: (!S_ISREG(a->perm))) {
1.161 djm 2396: error("download \"%s\": not a regular file", from_path);
1.145 djm 2397: return(-1);
2398: }
2399: if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
2400: size = a->size;
2401: else
2402: size = 0;
2403:
2404: buflen = from->download_buflen;
2405: if (buflen > to->upload_buflen)
2406: buflen = to->upload_buflen;
2407:
2408: /* Send open request to read side */
2409: if (send_open(from, from_path, "origin", SSH2_FXF_READ, NULL,
2410: &from_handle, &from_handle_len) != 0)
2411: return -1;
2412:
2413: /* Send open request to write side */
2414: a->flags &= ~SSH2_FILEXFER_ATTR_SIZE;
2415: a->flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
2416: a->perm &= 0777;
2417: if (!preserve_flag)
2418: a->flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
2419: if (send_open(to, to_path, "dest",
2420: SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC, a,
2421: &to_handle, &to_handle_len) != 0) {
2422: do_close(from, from_handle, from_handle_len);
2423: return -1;
2424: }
2425:
2426: /* Read from remote "from" and write to remote "to" */
2427: offset = 0;
2428: write_error = read_error = num_req = num_upload_req = 0;
2429: max_req = 1;
2430: progress_counter = 0;
2431:
1.153 djm 2432: if (showprogress && size != 0) {
2433: start_progress_meter(progress_meter_path(from_path),
2434: size, &progress_counter);
2435: }
1.145 djm 2436: if ((msg = sshbuf_new()) == NULL)
2437: fatal_f("sshbuf_new failed");
2438: while (num_req > 0 || max_req > 0) {
2439: u_char *data;
2440: size_t len;
2441:
2442: /*
2443: * Simulate EOF on interrupt: stop sending new requests and
2444: * allow outstanding requests to drain gracefully
2445: */
2446: if (interrupted) {
2447: if (num_req == 0) /* If we haven't started yet... */
2448: break;
2449: max_req = 0;
2450: }
2451:
2452: /* Send some more requests */
2453: while (num_req < max_req) {
2454: debug3("Request range %llu -> %llu (%d/%d)",
2455: (unsigned long long)offset,
2456: (unsigned long long)offset + buflen - 1,
2457: num_req, max_req);
1.151 djm 2458: req = request_enqueue(&requests, from->msg_id++,
2459: buflen, offset);
1.145 djm 2460: offset += buflen;
2461: num_req++;
2462: send_read_request(from, req->id, req->offset,
2463: req->len, from_handle, from_handle_len);
2464: }
2465:
2466: /* Try to eat replies from the upload side (nonblocking) */
2467: handle_dest_replies(to, to_path, 0,
2468: &num_upload_req, &write_error);
2469:
2470: sshbuf_reset(msg);
2471: get_msg(from, msg);
2472: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
2473: (r = sshbuf_get_u32(msg, &id)) != 0)
2474: fatal_fr(r, "parse");
2475: debug3("Received origin reply T:%u I:%u R:%d",
2476: type, id, max_req);
2477:
2478: /* Find the request in our queue */
1.151 djm 2479: if ((req = request_find(&requests, id)) == NULL)
1.145 djm 2480: fatal("Unexpected reply %u", id);
2481:
2482: switch (type) {
2483: case SSH2_FXP_STATUS:
2484: if ((r = sshbuf_get_u32(msg, &status)) != 0)
2485: fatal_fr(r, "parse status");
2486: if (status != SSH2_FX_EOF)
2487: read_error = 1;
2488: max_req = 0;
2489: TAILQ_REMOVE(&requests, req, tq);
2490: free(req);
2491: num_req--;
2492: break;
2493: case SSH2_FXP_DATA:
2494: if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
2495: fatal_fr(r, "parse data");
2496: debug3("Received data %llu -> %llu",
2497: (unsigned long long)req->offset,
2498: (unsigned long long)req->offset + len - 1);
2499: if (len > req->len)
2500: fatal("Received more data than asked for "
2501: "%zu > %zu", len, req->len);
2502:
2503: /* Write this chunk out to the destination */
2504: sshbuf_reset(msg);
2505: if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
2506: (r = sshbuf_put_u32(msg, to->msg_id++)) != 0 ||
2507: (r = sshbuf_put_string(msg, to_handle,
2508: to_handle_len)) != 0 ||
2509: (r = sshbuf_put_u64(msg, req->offset)) != 0 ||
2510: (r = sshbuf_put_string(msg, data, len)) != 0)
2511: fatal_fr(r, "compose write");
2512: send_msg(to, msg);
2513: debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%zu",
2514: id, (unsigned long long)offset, len);
2515: num_upload_req++;
2516: progress_counter += len;
2517: free(data);
2518:
2519: if (len == req->len) {
2520: TAILQ_REMOVE(&requests, req, tq);
2521: free(req);
2522: num_req--;
2523: } else {
2524: /* Resend the request for the missing data */
2525: debug3("Short data block, re-requesting "
2526: "%llu -> %llu (%2d)",
2527: (unsigned long long)req->offset + len,
2528: (unsigned long long)req->offset +
2529: req->len - 1, num_req);
2530: req->id = from->msg_id++;
2531: req->len -= len;
2532: req->offset += len;
2533: send_read_request(from, req->id,
2534: req->offset, req->len,
2535: from_handle, from_handle_len);
2536: /* Reduce the request size */
2537: if (len < buflen)
2538: buflen = MAXIMUM(MIN_READ_SIZE, len);
2539: }
2540: if (max_req > 0) { /* max_req = 0 iff EOF received */
2541: if (size > 0 && offset > size) {
2542: /* Only one request at a time
2543: * after the expected EOF */
2544: debug3("Finish at %llu (%2d)",
2545: (unsigned long long)offset,
2546: num_req);
2547: max_req = 1;
2548: } else if (max_req < from->num_requests) {
2549: ++max_req;
2550: }
2551: }
2552: break;
2553: default:
2554: fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
2555: SSH2_FXP_DATA, type);
2556: }
2557: }
2558:
2559: if (showprogress && size)
2560: stop_progress_meter();
2561:
2562: /* Drain replies from the server (blocking) */
2563: debug3_f("waiting for %u replies from destination", num_upload_req);
2564: handle_dest_replies(to, to_path, 1, &num_upload_req, &write_error);
2565:
2566: /* Sanity check */
2567: if (TAILQ_FIRST(&requests) != NULL)
2568: fatal("Transfer complete, but requests still in queue");
2569: /* Truncate at 0 length on interrupt or error to avoid holes at dest */
2570: if (read_error || write_error || interrupted) {
2571: debug("truncating \"%s\" at 0", to_path);
2572: do_close(to, to_handle, to_handle_len);
2573: free(to_handle);
2574: if (send_open(to, to_path, "dest",
2575: SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC, a,
2576: &to_handle, &to_handle_len) != 0) {
1.161 djm 2577: error("dest truncate \"%s\" failed", to_path);
1.145 djm 2578: to_handle = NULL;
2579: }
2580: }
2581: if (read_error) {
1.161 djm 2582: error("read origin \"%s\": %s", from_path, fx2txt(status));
1.145 djm 2583: status = -1;
2584: do_close(from, from_handle, from_handle_len);
2585: if (to_handle != NULL)
2586: do_close(to, to_handle, to_handle_len);
2587: } else if (write_error) {
1.161 djm 2588: error("write dest \"%s\": %s", to_path, fx2txt(write_error));
1.145 djm 2589: status = SSH2_FX_FAILURE;
2590: do_close(from, from_handle, from_handle_len);
2591: if (to_handle != NULL)
2592: do_close(to, to_handle, to_handle_len);
2593: } else {
2594: if (do_close(from, from_handle, from_handle_len) != 0 ||
2595: interrupted)
2596: status = -1;
2597: else
2598: status = SSH2_FX_OK;
2599: if (to_handle != NULL) {
2600: /* Need to resend utimes after write */
2601: if (preserve_flag)
2602: do_fsetstat(to, to_handle, to_handle_len, a);
2603: do_close(to, to_handle, to_handle_len);
2604: }
2605: }
2606: sshbuf_free(msg);
2607: free(from_handle);
2608: free(to_handle);
2609:
2610: return status == SSH2_FX_OK ? 0 : -1;
2611: }
2612:
2613: static int
2614: crossload_dir_internal(struct sftp_conn *from, struct sftp_conn *to,
2615: const char *from_path, const char *to_path,
1.150 djm 2616: int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
2617: int follow_link_flag)
1.145 djm 2618: {
2619: int i, ret = 0;
2620: SFTP_DIRENT **dir_entries;
2621: char *filename, *new_from_path = NULL, *new_to_path = NULL;
2622: mode_t mode = 0777;
1.149 djm 2623: Attrib curdir;
1.145 djm 2624:
1.161 djm 2625: debug2_f("crossload dir src \"%s\" to dst \"%s\"", from_path, to_path);
2626:
1.145 djm 2627: if (depth >= MAX_DIR_DEPTH) {
2628: error("Maximum directory depth exceeded: %d levels", depth);
2629: return -1;
2630: }
2631:
2632: if (dirattrib == NULL &&
2633: (dirattrib = do_stat(from, from_path, 1)) == NULL) {
1.161 djm 2634: error("stat remote \"%s\" failed", from_path);
1.145 djm 2635: return -1;
2636: }
2637: if (!S_ISDIR(dirattrib->perm)) {
2638: error("\"%s\" is not a directory", from_path);
2639: return -1;
2640: }
1.150 djm 2641: if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
1.145 djm 2642: mprintf("Retrieving %s\n", from_path);
2643:
1.149 djm 2644: curdir = *dirattrib; /* dirattrib will be clobbered */
2645: curdir.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
2646: curdir.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
2647: if ((curdir.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) == 0) {
2648: debug("Origin did not send permissions for "
1.145 djm 2649: "directory \"%s\"", to_path);
1.149 djm 2650: curdir.perm = S_IWUSR|S_IXUSR;
2651: curdir.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1.145 djm 2652: }
1.149 djm 2653: /* We need to be able to write to the directory while we transfer it */
2654: mode = curdir.perm & 01777;
2655: curdir.perm = mode | (S_IWUSR|S_IXUSR);
2656:
2657: /*
2658: * sftp lacks a portable status value to match errno EEXIST,
2659: * so if we get a failure back then we must check whether
2660: * the path already existed and is a directory. Ensure we can
2661: * write to the directory we create for the duration of the transfer.
2662: */
2663: if (do_mkdir(to, to_path, &curdir, 0) != 0) {
2664: if ((dirattrib = do_stat(to, to_path, 0)) == NULL)
2665: return -1;
2666: if (!S_ISDIR(dirattrib->perm)) {
2667: error("\"%s\" exists but is not a directory", to_path);
2668: return -1;
2669: }
2670: }
2671: curdir.perm = mode;
1.145 djm 2672:
2673: if (do_readdir(from, from_path, &dir_entries) == -1) {
1.161 djm 2674: error("origin readdir \"%s\" failed", from_path);
1.145 djm 2675: return -1;
2676: }
2677:
2678: for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
2679: free(new_from_path);
2680: free(new_to_path);
2681:
2682: filename = dir_entries[i]->filename;
2683: new_from_path = path_append(from_path, filename);
2684: new_to_path = path_append(to_path, filename);
2685:
2686: if (S_ISDIR(dir_entries[i]->a.perm)) {
2687: if (strcmp(filename, ".") == 0 ||
2688: strcmp(filename, "..") == 0)
2689: continue;
2690: if (crossload_dir_internal(from, to,
2691: new_from_path, new_to_path,
2692: depth + 1, &(dir_entries[i]->a), preserve_flag,
1.150 djm 2693: print_flag, follow_link_flag) == -1)
1.145 djm 2694: ret = -1;
1.150 djm 2695: } else if (S_ISREG(dir_entries[i]->a.perm) ||
2696: (follow_link_flag && S_ISLNK(dir_entries[i]->a.perm))) {
2697: /*
2698: * If this is a symlink then don't send the link's
2699: * Attrib. do_download() will do a FXP_STAT operation
2700: * and get the link target's attributes.
2701: */
1.145 djm 2702: if (do_crossload(from, to, new_from_path, new_to_path,
1.150 djm 2703: S_ISLNK(dir_entries[i]->a.perm) ? NULL :
1.145 djm 2704: &(dir_entries[i]->a), preserve_flag) == -1) {
1.161 djm 2705: error("crossload \"%s\" to \"%s\" failed",
1.145 djm 2706: new_from_path, new_to_path);
2707: ret = -1;
2708: }
1.161 djm 2709: } else {
2710: logit("origin \"%s\": not a regular file",
2711: new_from_path);
2712: }
1.145 djm 2713: }
2714: free(new_to_path);
2715: free(new_from_path);
2716:
1.149 djm 2717: do_setstat(to, to_path, &curdir);
1.145 djm 2718:
2719: free_sftp_dirents(dir_entries);
2720:
2721: return ret;
2722: }
2723:
2724: int
2725: crossload_dir(struct sftp_conn *from, struct sftp_conn *to,
2726: const char *from_path, const char *to_path,
1.150 djm 2727: Attrib *dirattrib, int preserve_flag, int print_flag, int follow_link_flag)
1.145 djm 2728: {
2729: char *from_path_canon;
2730: int ret;
2731:
2732: if ((from_path_canon = do_realpath(from, from_path)) == NULL) {
1.161 djm 2733: error("crossload \"%s\": path canonicalization failed",
2734: from_path);
1.145 djm 2735: return -1;
2736: }
2737:
2738: ret = crossload_dir_internal(from, to, from_path_canon, to_path, 0,
1.150 djm 2739: dirattrib, preserve_flag, print_flag, follow_link_flag);
1.145 djm 2740: free(from_path_canon);
1.89 djm 2741: return ret;
1.165 djm 2742: }
2743:
2744: int
2745: can_get_users_groups_by_id(struct sftp_conn *conn)
2746: {
2747: return (conn->exts & SFTP_EXT_GETUSERSGROUPS_BY_ID) != 0;
2748: }
2749:
2750: int
2751: do_get_users_groups_by_id(struct sftp_conn *conn,
2752: const u_int *uids, u_int nuids,
2753: const u_int *gids, u_int ngids,
2754: char ***usernamesp, char ***groupnamesp)
2755: {
2756: struct sshbuf *msg, *uidbuf, *gidbuf;
2757: u_int i, expected_id, id;
2758: char *name, **usernames = NULL, **groupnames = NULL;
2759: u_char type;
2760: int r;
2761:
2762: *usernamesp = *groupnamesp = NULL;
2763: if (!can_get_users_groups_by_id(conn))
2764: return SSH_ERR_FEATURE_UNSUPPORTED;
2765:
2766: if ((msg = sshbuf_new()) == NULL ||
2767: (uidbuf = sshbuf_new()) == NULL ||
2768: (gidbuf = sshbuf_new()) == NULL)
2769: fatal_f("sshbuf_new failed");
2770: expected_id = id = conn->msg_id++;
2771: debug2("Sending SSH2_FXP_EXTENDED(users-groups-by-id@openssh.com)");
2772: for (i = 0; i < nuids; i++) {
2773: if ((r = sshbuf_put_u32(uidbuf, uids[i])) != 0)
2774: fatal_fr(r, "compose uids");
2775: }
2776: for (i = 0; i < ngids; i++) {
2777: if ((r = sshbuf_put_u32(gidbuf, gids[i])) != 0)
2778: fatal_fr(r, "compose gids");
2779: }
2780: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
2781: (r = sshbuf_put_u32(msg, id)) != 0 ||
2782: (r = sshbuf_put_cstring(msg,
2783: "users-groups-by-id@openssh.com")) != 0 ||
2784: (r = sshbuf_put_stringb(msg, uidbuf)) != 0 ||
2785: (r = sshbuf_put_stringb(msg, gidbuf)) != 0)
2786: fatal_fr(r, "compose");
2787: send_msg(conn, msg);
2788: get_msg(conn, msg);
2789: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
2790: (r = sshbuf_get_u32(msg, &id)) != 0)
2791: fatal_fr(r, "parse");
2792: if (id != expected_id)
2793: fatal("ID mismatch (%u != %u)", id, expected_id);
2794: if (type == SSH2_FXP_STATUS) {
2795: u_int status;
2796: char *errmsg;
2797:
2798: if ((r = sshbuf_get_u32(msg, &status)) != 0 ||
2799: (r = sshbuf_get_cstring(msg, &errmsg, NULL)) != 0)
2800: fatal_fr(r, "parse status");
2801: error("users-groups-by-id %s",
2802: *errmsg == '\0' ? fx2txt(status) : errmsg);
2803: free(errmsg);
2804: sshbuf_free(msg);
2805: sshbuf_free(uidbuf);
2806: sshbuf_free(gidbuf);
2807: return -1;
2808: } else if (type != SSH2_FXP_EXTENDED_REPLY)
2809: fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
2810: SSH2_FXP_EXTENDED_REPLY, type);
2811:
2812: /* reuse */
2813: sshbuf_free(uidbuf);
2814: sshbuf_free(gidbuf);
2815: uidbuf = gidbuf = NULL;
2816: if ((r = sshbuf_froms(msg, &uidbuf)) != 0 ||
2817: (r = sshbuf_froms(msg, &gidbuf)) != 0)
2818: fatal_fr(r, "parse response");
2819: if (nuids > 0) {
2820: usernames = xcalloc(nuids, sizeof(*usernames));
2821: for (i = 0; i < nuids; i++) {
2822: if ((r = sshbuf_get_cstring(uidbuf, &name, NULL)) != 0)
2823: fatal_fr(r, "parse user name");
2824: /* Handle unresolved names */
2825: if (*name == '\0') {
2826: free(name);
2827: name = NULL;
2828: }
2829: usernames[i] = name;
2830: }
2831: }
2832: if (ngids > 0) {
2833: groupnames = xcalloc(ngids, sizeof(*groupnames));
2834: for (i = 0; i < ngids; i++) {
2835: if ((r = sshbuf_get_cstring(gidbuf, &name, NULL)) != 0)
2836: fatal_fr(r, "parse user name");
2837: /* Handle unresolved names */
2838: if (*name == '\0') {
2839: free(name);
2840: name = NULL;
2841: }
2842: groupnames[i] = name;
2843: }
2844: }
2845: if (sshbuf_len(uidbuf) != 0)
2846: fatal_f("unexpected extra username data");
2847: if (sshbuf_len(gidbuf) != 0)
2848: fatal_f("unexpected extra groupname data");
2849: sshbuf_free(uidbuf);
2850: sshbuf_free(gidbuf);
2851: sshbuf_free(msg);
2852: /* success */
2853: *usernamesp = usernames;
2854: *groupnamesp = groupnames;
2855: return 0;
1.89 djm 2856: }
2857:
2858: char *
1.116 djm 2859: path_append(const char *p1, const char *p2)
1.89 djm 2860: {
2861: char *ret;
2862: size_t len = strlen(p1) + strlen(p2) + 2;
2863:
2864: ret = xmalloc(len);
2865: strlcpy(ret, p1, len);
2866: if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
2867: strlcat(ret, "/", len);
2868: strlcat(ret, p2, len);
2869:
2870: return(ret);
1.139 djm 2871: }
2872:
2873: char *
2874: make_absolute(char *p, const char *pwd)
2875: {
2876: char *abs_str;
2877:
2878: /* Derelativise */
2879: if (p && !path_absolute(p)) {
2880: abs_str = path_append(pwd, p);
2881: free(p);
2882: return(abs_str);
2883: } else
2884: return(p);
2885: }
2886:
2887: int
2888: remote_is_dir(struct sftp_conn *conn, const char *path)
2889: {
2890: Attrib *a;
2891:
2892: /* XXX: report errors? */
2893: if ((a = do_stat(conn, path, 1)) == NULL)
2894: return(0);
2895: if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
2896: return(0);
2897: return(S_ISDIR(a->perm));
2898: }
2899:
2900:
2901: int
2902: local_is_dir(const char *path)
2903: {
2904: struct stat sb;
2905:
2906: /* XXX: report errors? */
2907: if (stat(path, &sb) == -1)
2908: return(0);
2909:
2910: return(S_ISDIR(sb.st_mode));
2911: }
2912:
2913: /* Check whether path returned from glob(..., GLOB_MARK, ...) is a directory */
2914: int
2915: globpath_is_dir(const char *pathname)
2916: {
2917: size_t l = strlen(pathname);
2918:
2919: return l > 0 && pathname[l - 1] == '/';
1.89 djm 2920: }
2921: