Annotation of src/usr.bin/ssh/sftp-client.c, Revision 1.137
1.137 ! djm 1: /* $OpenBSD: sftp-client.c,v 1.136 2020/05/15 03:57:33 djm Exp $ */
1.1 djm 2: /*
1.46 djm 3: * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
1.1 djm 4: *
1.46 djm 5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 djm 8: *
1.46 djm 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 djm 16: */
17:
18: /* XXX: memleaks */
19: /* XXX: signed vs unsigned */
1.23 djm 20: /* XXX: remove all logging, only return status codes */
1.1 djm 21: /* XXX: copy between two remote sites */
22:
1.74 deraadt 23: #include <sys/types.h>
1.93 djm 24: #include <sys/poll.h>
1.21 djm 25: #include <sys/queue.h>
1.60 stevesk 26: #include <sys/stat.h>
1.71 stevesk 27: #include <sys/time.h>
1.82 djm 28: #include <sys/statvfs.h>
1.74 deraadt 29: #include <sys/uio.h>
1.66 stevesk 30:
1.89 djm 31: #include <dirent.h>
1.67 stevesk 32: #include <errno.h>
1.66 stevesk 33: #include <fcntl.h>
1.70 stevesk 34: #include <signal.h>
1.87 dtucker 35: #include <stdarg.h>
1.73 stevesk 36: #include <stdio.h>
1.109 dtucker 37: #include <stdlib.h>
1.69 stevesk 38: #include <string.h>
1.68 stevesk 39: #include <unistd.h>
1.1 djm 40:
1.74 deraadt 41: #include "xmalloc.h"
1.116 djm 42: #include "ssherr.h"
43: #include "sshbuf.h"
1.1 djm 44: #include "log.h"
45: #include "atomicio.h"
1.39 fgsch 46: #include "progressmeter.h"
1.64 djm 47: #include "misc.h"
1.124 schwarze 48: #include "utf8.h"
1.1 djm 49:
50: #include "sftp.h"
51: #include "sftp-common.h"
52: #include "sftp-client.h"
53:
1.49 djm 54: extern volatile sig_atomic_t interrupted;
1.39 fgsch 55: extern int showprogress;
56:
1.59 david 57: /* Minimum amount of data to read at a time */
1.21 djm 58: #define MIN_READ_SIZE 512
59:
1.89 djm 60: /* Maximum depth to descend in directory trees */
61: #define MAX_DIR_DEPTH 64
62:
1.23 djm 63: struct sftp_conn {
64: int fd_in;
65: int fd_out;
66: u_int transfer_buflen;
67: u_int num_requests;
68: u_int version;
69: u_int msg_id;
1.82 djm 70: #define SFTP_EXT_POSIX_RENAME 0x00000001
71: #define SFTP_EXT_STATVFS 0x00000002
72: #define SFTP_EXT_FSTATVFS 0x00000004
1.94 djm 73: #define SFTP_EXT_HARDLINK 0x00000008
1.107 djm 74: #define SFTP_EXT_FSYNC 0x00000010
1.131 djm 75: #define SFTP_EXT_LSETSTAT 0x00000020
1.81 djm 76: u_int exts;
1.93 djm 77: u_int64_t limit_kbps;
78: struct bwlimit bwlimit_in, bwlimit_out;
1.23 djm 79: };
1.4 djm 80:
1.116 djm 81: static u_char *
82: get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
1.93 djm 83: const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
84:
85: /* ARGSUSED */
86: static int
87: sftpio(void *_bwlimit, size_t amount)
88: {
89: struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
90:
1.133 dtucker 91: refresh_progress_meter(0);
1.132 dtucker 92: if (bwlimit != NULL)
93: bandwidth_limit(bwlimit, amount);
1.93 djm 94: return 0;
95: }
1.88 djm 96:
1.17 itojun 97: static void
1.116 djm 98: send_msg(struct sftp_conn *conn, struct sshbuf *m)
1.1 djm 99: {
1.40 djm 100: u_char mlen[4];
1.65 djm 101: struct iovec iov[2];
1.40 djm 102:
1.116 djm 103: if (sshbuf_len(m) > SFTP_MAX_MSG_LENGTH)
104: fatal("Outbound message too long %zu", sshbuf_len(m));
1.40 djm 105:
106: /* Send length first */
1.116 djm 107: put_u32(mlen, sshbuf_len(m));
1.65 djm 108: iov[0].iov_base = mlen;
109: iov[0].iov_len = sizeof(mlen);
1.116 djm 110: iov[1].iov_base = (u_char *)sshbuf_ptr(m);
111: iov[1].iov_len = sshbuf_len(m);
1.74 deraadt 112:
1.132 dtucker 113: if (atomiciov6(writev, conn->fd_out, iov, 2, sftpio,
114: conn->limit_kbps > 0 ? &conn->bwlimit_out : NULL) !=
1.116 djm 115: sshbuf_len(m) + sizeof(mlen))
1.1 djm 116: fatal("Couldn't send packet: %s", strerror(errno));
117:
1.116 djm 118: sshbuf_reset(m);
1.1 djm 119: }
120:
1.17 itojun 121: static void
1.128 dtucker 122: get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
1.1 djm 123: {
1.40 djm 124: u_int msg_len;
1.116 djm 125: u_char *p;
126: int r;
1.1 djm 127:
1.116 djm 128: if ((r = sshbuf_reserve(m, 4, &p)) != 0)
1.137 ! djm 129: fatal_fr(r, "reserve");
1.132 dtucker 130: if (atomicio6(read, conn->fd_in, p, 4, sftpio,
131: conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL) != 4) {
1.127 djm 132: if (errno == EPIPE || errno == ECONNRESET)
1.54 avsm 133: fatal("Connection closed");
134: else
135: fatal("Couldn't read packet: %s", strerror(errno));
136: }
1.1 djm 137:
1.116 djm 138: if ((r = sshbuf_get_u32(m, &msg_len)) != 0)
1.137 ! djm 139: fatal_fr(r, "sshbuf_get_u32");
1.128 dtucker 140: if (msg_len > SFTP_MAX_MSG_LENGTH) {
141: do_log2(initial ? SYSLOG_LEVEL_ERROR : SYSLOG_LEVEL_FATAL,
142: "Received message too long %u", msg_len);
143: fatal("Ensure the remote shell produces no output "
144: "for non-interactive sessions.");
145: }
1.1 djm 146:
1.116 djm 147: if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
1.137 ! djm 148: fatal_fr(r, "reserve");
1.132 dtucker 149: if (atomicio6(read, conn->fd_in, p, msg_len, sftpio,
150: conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL)
1.93 djm 151: != msg_len) {
1.54 avsm 152: if (errno == EPIPE)
153: fatal("Connection closed");
154: else
155: fatal("Read packet: %s", strerror(errno));
156: }
1.1 djm 157: }
158:
1.17 itojun 159: static void
1.128 dtucker 160: get_msg(struct sftp_conn *conn, struct sshbuf *m)
161: {
162: get_msg_extended(conn, m, 0);
163: }
164:
165: static void
1.116 djm 166: send_string_request(struct sftp_conn *conn, u_int id, u_int code, const char *s,
1.1 djm 167: u_int len)
168: {
1.116 djm 169: struct sshbuf *msg;
170: int r;
1.1 djm 171:
1.116 djm 172: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 173: fatal_f("sshbuf_new failed");
1.116 djm 174: if ((r = sshbuf_put_u8(msg, code)) != 0 ||
175: (r = sshbuf_put_u32(msg, id)) != 0 ||
176: (r = sshbuf_put_string(msg, s, len)) != 0)
1.137 ! djm 177: fatal_fr(r, "compose");
1.116 djm 178: send_msg(conn, msg);
1.93 djm 179: debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
1.116 djm 180: sshbuf_free(msg);
1.1 djm 181: }
182:
1.17 itojun 183: static void
1.93 djm 184: send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
1.116 djm 185: const void *s, u_int len, Attrib *a)
1.1 djm 186: {
1.116 djm 187: struct sshbuf *msg;
188: int r;
1.1 djm 189:
1.116 djm 190: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 191: fatal_f("sshbuf_new failed");
1.116 djm 192: if ((r = sshbuf_put_u8(msg, code)) != 0 ||
193: (r = sshbuf_put_u32(msg, id)) != 0 ||
194: (r = sshbuf_put_string(msg, s, len)) != 0 ||
195: (r = encode_attrib(msg, a)) != 0)
1.137 ! djm 196: fatal_fr(r, "compose");
1.116 djm 197: send_msg(conn, msg);
1.93 djm 198: debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
1.116 djm 199: sshbuf_free(msg);
1.1 djm 200: }
201:
1.17 itojun 202: static u_int
1.93 djm 203: get_status(struct sftp_conn *conn, u_int expected_id)
1.1 djm 204: {
1.116 djm 205: struct sshbuf *msg;
206: u_char type;
207: u_int id, status;
208: int r;
1.1 djm 209:
1.116 djm 210: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 211: fatal_f("sshbuf_new failed");
1.116 djm 212: get_msg(conn, msg);
213: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
214: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 ! djm 215: fatal_fr(r, "compose");
1.1 djm 216:
217: if (id != expected_id)
1.33 deraadt 218: fatal("ID mismatch (%u != %u)", id, expected_id);
1.1 djm 219: if (type != SSH2_FXP_STATUS)
1.33 deraadt 220: fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
1.1 djm 221: SSH2_FXP_STATUS, type);
222:
1.116 djm 223: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 ! djm 224: fatal_fr(r, "parse");
1.116 djm 225: sshbuf_free(msg);
1.1 djm 226:
1.33 deraadt 227: debug3("SSH2_FXP_STATUS %u", status);
1.1 djm 228:
1.93 djm 229: return status;
1.1 djm 230: }
231:
1.116 djm 232: static u_char *
233: get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
1.93 djm 234: const char *errfmt, ...)
1.1 djm 235: {
1.116 djm 236: struct sshbuf *msg;
237: u_int id, status;
238: u_char type;
239: u_char *handle;
240: char errmsg[256];
1.88 djm 241: va_list args;
1.116 djm 242: int r;
1.88 djm 243:
244: va_start(args, errfmt);
245: if (errfmt != NULL)
246: vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
247: va_end(args);
1.1 djm 248:
1.116 djm 249: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 250: fatal_f("sshbuf_new failed");
1.116 djm 251: get_msg(conn, msg);
252: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
253: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 ! djm 254: fatal_fr(r, "parse");
1.1 djm 255:
256: if (id != expected_id)
1.88 djm 257: fatal("%s: ID mismatch (%u != %u)",
258: errfmt == NULL ? __func__ : errmsg, id, expected_id);
1.1 djm 259: if (type == SSH2_FXP_STATUS) {
1.116 djm 260: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 ! djm 261: fatal_fr(r, "parse status");
1.88 djm 262: if (errfmt != NULL)
263: error("%s: %s", errmsg, fx2txt(status));
1.116 djm 264: sshbuf_free(msg);
1.1 djm 265: return(NULL);
266: } else if (type != SSH2_FXP_HANDLE)
1.88 djm 267: fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
268: errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
1.1 djm 269:
1.116 djm 270: if ((r = sshbuf_get_string(msg, &handle, len)) != 0)
1.137 ! djm 271: fatal_fr(r, "parse handle");
1.116 djm 272: sshbuf_free(msg);
1.1 djm 273:
1.116 djm 274: return handle;
1.1 djm 275: }
276:
1.17 itojun 277: static Attrib *
1.93 djm 278: get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
1.1 djm 279: {
1.116 djm 280: struct sshbuf *msg;
281: u_int id;
282: u_char type;
283: int r;
284: static Attrib a;
285:
286: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 287: fatal_f("sshbuf_new failed");
1.116 djm 288: get_msg(conn, msg);
289:
290: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
291: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 ! djm 292: fatal_fr(r, "parse");
1.1 djm 293:
1.33 deraadt 294: debug3("Received stat reply T:%u I:%u", type, id);
1.1 djm 295: if (id != expected_id)
1.33 deraadt 296: fatal("ID mismatch (%u != %u)", id, expected_id);
1.1 djm 297: if (type == SSH2_FXP_STATUS) {
1.116 djm 298: u_int status;
1.1 djm 299:
1.116 djm 300: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 ! djm 301: fatal_fr(r, "parse status");
1.14 djm 302: if (quiet)
303: debug("Couldn't stat remote file: %s", fx2txt(status));
304: else
305: error("Couldn't stat remote file: %s", fx2txt(status));
1.116 djm 306: sshbuf_free(msg);
1.1 djm 307: return(NULL);
308: } else if (type != SSH2_FXP_ATTRS) {
1.33 deraadt 309: fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
1.1 djm 310: SSH2_FXP_ATTRS, type);
311: }
1.116 djm 312: if ((r = decode_attrib(msg, &a)) != 0) {
1.137 ! djm 313: error_fr(r, "decode_attrib");
1.116 djm 314: sshbuf_free(msg);
315: return NULL;
316: }
317: sshbuf_free(msg);
1.1 djm 318:
1.116 djm 319: return &a;
1.1 djm 320: }
321:
1.82 djm 322: static int
1.93 djm 323: get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
324: u_int expected_id, int quiet)
1.82 djm 325: {
1.116 djm 326: struct sshbuf *msg;
327: u_char type;
328: u_int id;
329: u_int64_t flag;
330: int r;
1.82 djm 331:
1.116 djm 332: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 333: fatal_f("sshbuf_new failed");
1.116 djm 334: get_msg(conn, msg);
335:
336: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
337: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 ! djm 338: fatal_fr(r, "parse");
1.82 djm 339:
340: debug3("Received statvfs reply T:%u I:%u", type, id);
341: if (id != expected_id)
342: fatal("ID mismatch (%u != %u)", id, expected_id);
343: if (type == SSH2_FXP_STATUS) {
1.116 djm 344: u_int status;
1.82 djm 345:
1.116 djm 346: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 ! djm 347: fatal_fr(r, "parse status");
1.82 djm 348: if (quiet)
349: debug("Couldn't statvfs: %s", fx2txt(status));
350: else
351: error("Couldn't statvfs: %s", fx2txt(status));
1.116 djm 352: sshbuf_free(msg);
1.82 djm 353: return -1;
354: } else if (type != SSH2_FXP_EXTENDED_REPLY) {
355: fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
356: SSH2_FXP_EXTENDED_REPLY, type);
357: }
358:
1.114 tedu 359: memset(st, 0, sizeof(*st));
1.116 djm 360: if ((r = sshbuf_get_u64(msg, &st->f_bsize)) != 0 ||
361: (r = sshbuf_get_u64(msg, &st->f_frsize)) != 0 ||
362: (r = sshbuf_get_u64(msg, &st->f_blocks)) != 0 ||
363: (r = sshbuf_get_u64(msg, &st->f_bfree)) != 0 ||
364: (r = sshbuf_get_u64(msg, &st->f_bavail)) != 0 ||
365: (r = sshbuf_get_u64(msg, &st->f_files)) != 0 ||
366: (r = sshbuf_get_u64(msg, &st->f_ffree)) != 0 ||
367: (r = sshbuf_get_u64(msg, &st->f_favail)) != 0 ||
368: (r = sshbuf_get_u64(msg, &st->f_fsid)) != 0 ||
369: (r = sshbuf_get_u64(msg, &flag)) != 0 ||
370: (r = sshbuf_get_u64(msg, &st->f_namemax)) != 0)
1.137 ! djm 371: fatal_fr(r, "parse statvfs");
1.82 djm 372:
373: st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
374: st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
375:
1.116 djm 376: sshbuf_free(msg);
1.82 djm 377:
378: return 0;
379: }
380:
1.23 djm 381: struct sftp_conn *
1.93 djm 382: do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
383: u_int64_t limit_kbps)
1.1 djm 384: {
1.116 djm 385: u_char type;
386: struct sshbuf *msg;
1.23 djm 387: struct sftp_conn *ret;
1.116 djm 388: int r;
1.1 djm 389:
1.103 djm 390: ret = xcalloc(1, sizeof(*ret));
391: ret->msg_id = 1;
1.93 djm 392: ret->fd_in = fd_in;
393: ret->fd_out = fd_out;
394: ret->transfer_buflen = transfer_buflen;
395: ret->num_requests = num_requests;
396: ret->exts = 0;
397: ret->limit_kbps = 0;
398:
1.116 djm 399: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 400: fatal_f("sshbuf_new failed");
1.116 djm 401: if ((r = sshbuf_put_u8(msg, SSH2_FXP_INIT)) != 0 ||
402: (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0)
1.137 ! djm 403: fatal_fr(r, "parse");
! 404:
1.116 djm 405: send_msg(ret, msg);
1.1 djm 406:
1.116 djm 407: sshbuf_reset(msg);
1.1 djm 408:
1.128 dtucker 409: get_msg_extended(ret, msg, 1);
1.1 djm 410:
1.3 stevesk 411: /* Expecting a VERSION reply */
1.116 djm 412: if ((r = sshbuf_get_u8(msg, &type)) != 0)
1.137 ! djm 413: fatal_fr(r, "parse type");
1.116 djm 414: if (type != SSH2_FXP_VERSION) {
1.33 deraadt 415: error("Invalid packet back from SSH2_FXP_INIT (type %u)",
1.1 djm 416: type);
1.116 djm 417: sshbuf_free(msg);
1.119 jsg 418: free(ret);
1.23 djm 419: return(NULL);
1.1 djm 420: }
1.116 djm 421: if ((r = sshbuf_get_u32(msg, &ret->version)) != 0)
1.137 ! djm 422: fatal_fr(r, "parse version");
1.1 djm 423:
1.93 djm 424: debug2("Remote version: %u", ret->version);
1.1 djm 425:
426: /* Check for extensions */
1.116 djm 427: while (sshbuf_len(msg) > 0) {
428: char *name;
429: u_char *value;
430: size_t vlen;
1.85 djm 431: int known = 0;
1.1 djm 432:
1.116 djm 433: if ((r = sshbuf_get_cstring(msg, &name, NULL)) != 0 ||
434: (r = sshbuf_get_string(msg, &value, &vlen)) != 0)
1.137 ! djm 435: fatal_fr(r, "parse extension");
1.82 djm 436: if (strcmp(name, "posix-rename@openssh.com") == 0 &&
1.116 djm 437: strcmp((char *)value, "1") == 0) {
1.93 djm 438: ret->exts |= SFTP_EXT_POSIX_RENAME;
1.85 djm 439: known = 1;
440: } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
1.116 djm 441: strcmp((char *)value, "2") == 0) {
1.93 djm 442: ret->exts |= SFTP_EXT_STATVFS;
1.85 djm 443: known = 1;
1.94 djm 444: } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
1.116 djm 445: strcmp((char *)value, "2") == 0) {
1.93 djm 446: ret->exts |= SFTP_EXT_FSTATVFS;
1.85 djm 447: known = 1;
1.94 djm 448: } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
1.116 djm 449: strcmp((char *)value, "1") == 0) {
1.94 djm 450: ret->exts |= SFTP_EXT_HARDLINK;
451: known = 1;
1.116 djm 452: } else if (strcmp(name, "fsync@openssh.com") == 0 &&
453: strcmp((char *)value, "1") == 0) {
454: ret->exts |= SFTP_EXT_FSYNC;
455: known = 1;
1.131 djm 456: } else if (strcmp(name, "lsetstat@openssh.com") == 0 &&
457: strcmp((char *)value, "1") == 0) {
458: ret->exts |= SFTP_EXT_LSETSTAT;
459: known = 1;
1.85 djm 460: }
461: if (known) {
462: debug2("Server supports extension \"%s\" revision %s",
463: name, value);
464: } else {
465: debug2("Unrecognised server extension \"%s\"", name);
466: }
1.98 djm 467: free(name);
468: free(value);
1.1 djm 469: }
470:
1.116 djm 471: sshbuf_free(msg);
1.11 djm 472:
1.23 djm 473: /* Some filexfer v.0 servers don't support large packets */
1.93 djm 474: if (ret->version == 0)
1.125 deraadt 475: ret->transfer_buflen = MINIMUM(ret->transfer_buflen, 20480);
1.23 djm 476:
1.93 djm 477: ret->limit_kbps = limit_kbps;
478: if (ret->limit_kbps > 0) {
479: bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
480: ret->transfer_buflen);
481: bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
482: ret->transfer_buflen);
483: }
484:
485: return ret;
1.23 djm 486: }
487:
488: u_int
489: sftp_proto_version(struct sftp_conn *conn)
490: {
1.93 djm 491: return conn->version;
1.1 djm 492: }
493:
494: int
1.116 djm 495: do_close(struct sftp_conn *conn, const u_char *handle, u_int handle_len)
1.1 djm 496: {
497: u_int id, status;
1.116 djm 498: struct sshbuf *msg;
499: int r;
1.1 djm 500:
1.116 djm 501: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 502: fatal_f("sshbuf_new failed");
1.1 djm 503:
1.23 djm 504: id = conn->msg_id++;
1.116 djm 505: if ((r = sshbuf_put_u8(msg, SSH2_FXP_CLOSE)) != 0 ||
506: (r = sshbuf_put_u32(msg, id)) != 0 ||
507: (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137 ! djm 508: fatal_fr(r, "parse");
1.116 djm 509: send_msg(conn, msg);
1.33 deraadt 510: debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
1.1 djm 511:
1.93 djm 512: status = get_status(conn, id);
1.1 djm 513: if (status != SSH2_FX_OK)
514: error("Couldn't close file: %s", fx2txt(status));
515:
1.116 djm 516: sshbuf_free(msg);
1.1 djm 517:
1.116 djm 518: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 519: }
520:
1.12 djm 521:
1.17 itojun 522: static int
1.116 djm 523: do_lsreaddir(struct sftp_conn *conn, const char *path, int print_flag,
1.12 djm 524: SFTP_DIRENT ***dir)
1.1 djm 525: {
1.116 djm 526: struct sshbuf *msg;
527: u_int count, id, i, expected_id, ents = 0;
528: size_t handle_len;
1.123 djm 529: u_char type, *handle;
1.111 djm 530: int status = SSH2_FX_FAILURE;
1.116 djm 531: int r;
1.111 djm 532:
533: if (dir)
534: *dir = NULL;
1.1 djm 535:
1.23 djm 536: id = conn->msg_id++;
1.1 djm 537:
1.116 djm 538: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 539: fatal_f("sshbuf_new failed");
1.116 djm 540: if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPENDIR)) != 0 ||
541: (r = sshbuf_put_u32(msg, id)) != 0 ||
542: (r = sshbuf_put_cstring(msg, path)) != 0)
1.137 ! djm 543: fatal_fr(r, "compose OPENDIR");
1.116 djm 544: send_msg(conn, msg);
1.1 djm 545:
1.93 djm 546: handle = get_handle(conn, id, &handle_len,
1.88 djm 547: "remote readdir(\"%s\")", path);
1.96 markus 548: if (handle == NULL) {
1.116 djm 549: sshbuf_free(msg);
1.93 djm 550: return -1;
1.96 markus 551: }
1.1 djm 552:
1.12 djm 553: if (dir) {
554: ents = 0;
1.108 djm 555: *dir = xcalloc(1, sizeof(**dir));
1.12 djm 556: (*dir)[0] = NULL;
557: }
558:
1.49 djm 559: for (; !interrupted;) {
1.23 djm 560: id = expected_id = conn->msg_id++;
1.1 djm 561:
1.33 deraadt 562: debug3("Sending SSH2_FXP_READDIR I:%u", id);
1.1 djm 563:
1.116 djm 564: sshbuf_reset(msg);
565: if ((r = sshbuf_put_u8(msg, SSH2_FXP_READDIR)) != 0 ||
566: (r = sshbuf_put_u32(msg, id)) != 0 ||
567: (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137 ! djm 568: fatal_fr(r, "compose READDIR");
1.116 djm 569: send_msg(conn, msg);
570:
571: sshbuf_reset(msg);
572:
573: get_msg(conn, msg);
574:
575: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
576: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 ! djm 577: fatal_fr(r, "parse");
1.1 djm 578:
1.33 deraadt 579: debug3("Received reply T:%u I:%u", type, id);
1.1 djm 580:
581: if (id != expected_id)
1.33 deraadt 582: fatal("ID mismatch (%u != %u)", id, expected_id);
1.1 djm 583:
584: if (type == SSH2_FXP_STATUS) {
1.116 djm 585: u_int rstatus;
586:
587: if ((r = sshbuf_get_u32(msg, &rstatus)) != 0)
1.137 ! djm 588: fatal_fr(r, "parse status");
1.116 djm 589: debug3("Received SSH2_FXP_STATUS %d", rstatus);
590: if (rstatus == SSH2_FX_EOF)
1.1 djm 591: break;
1.116 djm 592: error("Couldn't read directory: %s", fx2txt(rstatus));
1.111 djm 593: goto out;
1.1 djm 594: } else if (type != SSH2_FXP_NAME)
1.33 deraadt 595: fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.1 djm 596: SSH2_FXP_NAME, type);
597:
1.116 djm 598: if ((r = sshbuf_get_u32(msg, &count)) != 0)
1.137 ! djm 599: fatal_fr(r, "parse count");
1.126 djm 600: if (count > SSHBUF_SIZE_MAX)
1.137 ! djm 601: fatal_f("nonsensical number of entries");
1.7 markus 602: if (count == 0)
603: break;
1.8 stevesk 604: debug3("Received %d SSH2_FXP_NAME responses", count);
1.19 deraadt 605: for (i = 0; i < count; i++) {
1.1 djm 606: char *filename, *longname;
1.116 djm 607: Attrib a;
1.1 djm 608:
1.116 djm 609: if ((r = sshbuf_get_cstring(msg, &filename,
610: NULL)) != 0 ||
611: (r = sshbuf_get_cstring(msg, &longname,
612: NULL)) != 0)
1.137 ! djm 613: fatal_fr(r, "parse filenames");
1.116 djm 614: if ((r = decode_attrib(msg, &a)) != 0) {
1.137 ! djm 615: error_fr(r, "couldn't decode attrib");
1.116 djm 616: free(filename);
617: free(longname);
1.135 djm 618: goto out;
1.116 djm 619: }
1.1 djm 620:
1.105 djm 621: if (print_flag)
1.124 schwarze 622: mprintf("%s\n", longname);
1.12 djm 623:
1.89 djm 624: /*
625: * Directory entries should never contain '/'
626: * These can be used to attack recursive ops
627: * (e.g. send '../../../../etc/passwd')
628: */
629: if (strchr(filename, '/') != NULL) {
630: error("Server sent suspect path \"%s\" "
631: "during readdir of \"%s\"", filename, path);
1.111 djm 632: } else if (dir) {
1.118 deraadt 633: *dir = xreallocarray(*dir, ents + 2, sizeof(**dir));
1.108 djm 634: (*dir)[ents] = xcalloc(1, sizeof(***dir));
1.12 djm 635: (*dir)[ents]->filename = xstrdup(filename);
636: (*dir)[ents]->longname = xstrdup(longname);
1.116 djm 637: memcpy(&(*dir)[ents]->a, &a, sizeof(a));
1.12 djm 638: (*dir)[++ents] = NULL;
639: }
1.98 djm 640: free(filename);
641: free(longname);
1.1 djm 642: }
643: }
1.111 djm 644: status = 0;
1.1 djm 645:
1.111 djm 646: out:
1.116 djm 647: sshbuf_free(msg);
1.23 djm 648: do_close(conn, handle, handle_len);
1.98 djm 649: free(handle);
1.1 djm 650:
1.111 djm 651: if (status != 0 && dir != NULL) {
652: /* Don't return results on error */
653: free_sftp_dirents(*dir);
654: *dir = NULL;
655: } else if (interrupted && dir != NULL && *dir != NULL) {
656: /* Don't return partial matches on interrupt */
1.49 djm 657: free_sftp_dirents(*dir);
1.108 djm 658: *dir = xcalloc(1, sizeof(**dir));
1.49 djm 659: **dir = NULL;
660: }
661:
1.129 djm 662: return status == SSH2_FX_OK ? 0 : -1;
1.12 djm 663: }
664:
665: int
1.116 djm 666: do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir)
1.12 djm 667: {
1.23 djm 668: return(do_lsreaddir(conn, path, 0, dir));
1.12 djm 669: }
670:
671: void free_sftp_dirents(SFTP_DIRENT **s)
672: {
673: int i;
1.19 deraadt 674:
1.111 djm 675: if (s == NULL)
676: return;
1.19 deraadt 677: for (i = 0; s[i]; i++) {
1.98 djm 678: free(s[i]->filename);
679: free(s[i]->longname);
680: free(s[i]);
1.12 djm 681: }
1.98 djm 682: free(s);
1.12 djm 683: }
684:
685: int
1.116 djm 686: do_rm(struct sftp_conn *conn, const char *path)
1.1 djm 687: {
688: u_int status, id;
689:
690: debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
691:
1.23 djm 692: id = conn->msg_id++;
1.93 djm 693: send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
694: status = get_status(conn, id);
1.1 djm 695: if (status != SSH2_FX_OK)
696: error("Couldn't delete file: %s", fx2txt(status));
1.116 djm 697: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 698: }
699:
700: int
1.116 djm 701: do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag)
1.1 djm 702: {
703: u_int status, id;
704:
1.23 djm 705: id = conn->msg_id++;
1.93 djm 706: send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
1.1 djm 707: strlen(path), a);
708:
1.93 djm 709: status = get_status(conn, id);
1.105 djm 710: if (status != SSH2_FX_OK && print_flag)
1.1 djm 711: error("Couldn't create directory: %s", fx2txt(status));
712:
1.116 djm 713: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 714: }
715:
716: int
1.116 djm 717: do_rmdir(struct sftp_conn *conn, const char *path)
1.1 djm 718: {
719: u_int status, id;
720:
1.23 djm 721: id = conn->msg_id++;
1.93 djm 722: send_string_request(conn, id, SSH2_FXP_RMDIR, path,
1.23 djm 723: strlen(path));
1.1 djm 724:
1.93 djm 725: status = get_status(conn, id);
1.1 djm 726: if (status != SSH2_FX_OK)
727: error("Couldn't remove directory: %s", fx2txt(status));
728:
1.116 djm 729: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 730: }
731:
732: Attrib *
1.116 djm 733: do_stat(struct sftp_conn *conn, const char *path, int quiet)
1.1 djm 734: {
735: u_int id;
736:
1.23 djm 737: id = conn->msg_id++;
738:
1.93 djm 739: send_string_request(conn, id,
1.28 markus 740: conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
1.23 djm 741: path, strlen(path));
742:
1.93 djm 743: return(get_decode_stat(conn, id, quiet));
1.1 djm 744: }
745:
746: Attrib *
1.116 djm 747: do_lstat(struct sftp_conn *conn, const char *path, int quiet)
1.1 djm 748: {
749: u_int id;
750:
1.23 djm 751: if (conn->version == 0) {
752: if (quiet)
753: debug("Server version does not support lstat operation");
754: else
1.43 itojun 755: logit("Server version does not support lstat operation");
1.30 markus 756: return(do_stat(conn, path, quiet));
1.23 djm 757: }
758:
759: id = conn->msg_id++;
1.93 djm 760: send_string_request(conn, id, SSH2_FXP_LSTAT, path,
1.23 djm 761: strlen(path));
762:
1.93 djm 763: return(get_decode_stat(conn, id, quiet));
1.1 djm 764: }
765:
1.78 chl 766: #ifdef notyet
1.1 djm 767: Attrib *
1.116 djm 768: do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
769: int quiet)
1.1 djm 770: {
771: u_int id;
772:
1.23 djm 773: id = conn->msg_id++;
1.93 djm 774: send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
1.23 djm 775: handle_len);
776:
1.93 djm 777: return(get_decode_stat(conn, id, quiet));
1.1 djm 778: }
1.78 chl 779: #endif
1.1 djm 780:
781: int
1.116 djm 782: do_setstat(struct sftp_conn *conn, const char *path, Attrib *a)
1.1 djm 783: {
784: u_int status, id;
785:
1.23 djm 786: id = conn->msg_id++;
1.93 djm 787: send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
1.1 djm 788: strlen(path), a);
789:
1.93 djm 790: status = get_status(conn, id);
1.1 djm 791: if (status != SSH2_FX_OK)
792: error("Couldn't setstat on \"%s\": %s", path,
793: fx2txt(status));
794:
1.116 djm 795: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 796: }
797:
798: int
1.116 djm 799: do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
1.1 djm 800: Attrib *a)
801: {
802: u_int status, id;
803:
1.23 djm 804: id = conn->msg_id++;
1.93 djm 805: send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
1.1 djm 806: handle_len, a);
807:
1.93 djm 808: status = get_status(conn, id);
1.1 djm 809: if (status != SSH2_FX_OK)
810: error("Couldn't fsetstat: %s", fx2txt(status));
811:
1.116 djm 812: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 813: }
814:
815: char *
1.116 djm 816: do_realpath(struct sftp_conn *conn, const char *path)
1.1 djm 817: {
1.116 djm 818: struct sshbuf *msg;
819: u_int expected_id, count, id;
1.1 djm 820: char *filename, *longname;
1.116 djm 821: Attrib a;
822: u_char type;
823: int r;
1.1 djm 824:
1.23 djm 825: expected_id = id = conn->msg_id++;
1.93 djm 826: send_string_request(conn, id, SSH2_FXP_REALPATH, path,
1.23 djm 827: strlen(path));
1.1 djm 828:
1.116 djm 829: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 830: fatal_f("sshbuf_new failed");
1.1 djm 831:
1.116 djm 832: get_msg(conn, msg);
833: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
834: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 ! djm 835: fatal_fr(r, "parse");
1.1 djm 836:
837: if (id != expected_id)
1.33 deraadt 838: fatal("ID mismatch (%u != %u)", id, expected_id);
1.1 djm 839:
840: if (type == SSH2_FXP_STATUS) {
1.116 djm 841: u_int status;
1.1 djm 842:
1.116 djm 843: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 ! djm 844: fatal_fr(r, "parse status");
1.107 djm 845: error("Couldn't canonicalize: %s", fx2txt(status));
1.116 djm 846: sshbuf_free(msg);
1.91 djm 847: return NULL;
1.1 djm 848: } else if (type != SSH2_FXP_NAME)
1.33 deraadt 849: fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.1 djm 850: SSH2_FXP_NAME, type);
851:
1.116 djm 852: if ((r = sshbuf_get_u32(msg, &count)) != 0)
1.137 ! djm 853: fatal_fr(r, "parse count");
1.1 djm 854: if (count != 1)
855: fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
856:
1.116 djm 857: if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
858: (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
859: (r = decode_attrib(msg, &a)) != 0)
1.137 ! djm 860: fatal_fr(r, "parse filename/attrib");
1.1 djm 861:
1.97 dtucker 862: debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename,
1.116 djm 863: (unsigned long)a.size);
1.1 djm 864:
1.98 djm 865: free(longname);
1.1 djm 866:
1.116 djm 867: sshbuf_free(msg);
1.1 djm 868:
869: return(filename);
870: }
871:
872: int
1.116 djm 873: do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath,
1.102 djm 874: int force_legacy)
1.1 djm 875: {
1.116 djm 876: struct sshbuf *msg;
1.1 djm 877: u_int status, id;
1.116 djm 878: int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
1.1 djm 879:
1.116 djm 880: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 881: fatal_f("sshbuf_new failed");
1.1 djm 882:
883: /* Send rename request */
1.23 djm 884: id = conn->msg_id++;
1.102 djm 885: if (use_ext) {
1.116 djm 886: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
887: (r = sshbuf_put_u32(msg, id)) != 0 ||
888: (r = sshbuf_put_cstring(msg,
889: "posix-rename@openssh.com")) != 0)
1.137 ! djm 890: fatal_fr(r, "compose posix-rename");
1.81 djm 891: } else {
1.116 djm 892: if ((r = sshbuf_put_u8(msg, SSH2_FXP_RENAME)) != 0 ||
893: (r = sshbuf_put_u32(msg, id)) != 0)
1.137 ! djm 894: fatal_fr(r, "compose rename");
1.116 djm 895: }
896: if ((r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
897: (r = sshbuf_put_cstring(msg, newpath)) != 0)
1.137 ! djm 898: fatal_fr(r, "compose paths");
1.116 djm 899: send_msg(conn, msg);
1.81 djm 900: debug3("Sent message %s \"%s\" -> \"%s\"",
1.116 djm 901: use_ext ? "posix-rename@openssh.com" :
902: "SSH2_FXP_RENAME", oldpath, newpath);
903: sshbuf_free(msg);
1.1 djm 904:
1.93 djm 905: status = get_status(conn, id);
1.1 djm 906: if (status != SSH2_FX_OK)
1.23 djm 907: error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
1.94 djm 908: newpath, fx2txt(status));
909:
1.116 djm 910: return status == SSH2_FX_OK ? 0 : -1;
1.94 djm 911: }
912:
913: int
1.116 djm 914: do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
1.94 djm 915: {
1.116 djm 916: struct sshbuf *msg;
1.94 djm 917: u_int status, id;
1.116 djm 918: int r;
1.94 djm 919:
920: if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
921: error("Server does not support hardlink@openssh.com extension");
922: return -1;
923: }
924:
1.116 djm 925: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 926: fatal_f("sshbuf_new failed");
1.95 markus 927:
928: /* Send link request */
929: id = conn->msg_id++;
1.116 djm 930: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
931: (r = sshbuf_put_u32(msg, id)) != 0 ||
932: (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
933: (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
934: (r = sshbuf_put_cstring(msg, newpath)) != 0)
1.137 ! djm 935: fatal_fr(r, "compose");
1.116 djm 936: send_msg(conn, msg);
1.94 djm 937: debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
938: oldpath, newpath);
1.116 djm 939: sshbuf_free(msg);
1.94 djm 940:
941: status = get_status(conn, id);
942: if (status != SSH2_FX_OK)
943: error("Couldn't link file \"%s\" to \"%s\": %s", oldpath,
1.23 djm 944: newpath, fx2txt(status));
1.1 djm 945:
1.116 djm 946: return status == SSH2_FX_OK ? 0 : -1;
1.11 djm 947: }
948:
949: int
1.116 djm 950: do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
1.11 djm 951: {
1.116 djm 952: struct sshbuf *msg;
1.11 djm 953: u_int status, id;
1.116 djm 954: int r;
1.11 djm 955:
1.23 djm 956: if (conn->version < 3) {
957: error("This server does not support the symlink operation");
958: return(SSH2_FX_OP_UNSUPPORTED);
959: }
960:
1.116 djm 961: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 962: fatal_f("sshbuf_new failed");
1.11 djm 963:
1.48 djm 964: /* Send symlink request */
1.23 djm 965: id = conn->msg_id++;
1.116 djm 966: if ((r = sshbuf_put_u8(msg, SSH2_FXP_SYMLINK)) != 0 ||
967: (r = sshbuf_put_u32(msg, id)) != 0 ||
968: (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
969: (r = sshbuf_put_cstring(msg, newpath)) != 0)
1.137 ! djm 970: fatal_fr(r, "compose");
1.116 djm 971: send_msg(conn, msg);
1.11 djm 972: debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
973: newpath);
1.116 djm 974: sshbuf_free(msg);
1.11 djm 975:
1.93 djm 976: status = get_status(conn, id);
1.11 djm 977: if (status != SSH2_FX_OK)
1.36 markus 978: error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
1.23 djm 979: newpath, fx2txt(status));
1.11 djm 980:
1.116 djm 981: return status == SSH2_FX_OK ? 0 : -1;
1.11 djm 982: }
983:
1.107 djm 984: int
1.116 djm 985: do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len)
1.107 djm 986: {
1.116 djm 987: struct sshbuf *msg;
1.107 djm 988: u_int status, id;
1.116 djm 989: int r;
1.107 djm 990:
991: /* Silently return if the extension is not supported */
992: if ((conn->exts & SFTP_EXT_FSYNC) == 0)
993: return -1;
994:
995: /* Send fsync request */
1.116 djm 996: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 997: fatal_f("sshbuf_new failed");
1.107 djm 998: id = conn->msg_id++;
1.116 djm 999: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1000: (r = sshbuf_put_u32(msg, id)) != 0 ||
1001: (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
1002: (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137 ! djm 1003: fatal_fr(r, "compose");
1.116 djm 1004: send_msg(conn, msg);
1.107 djm 1005: debug3("Sent message fsync@openssh.com I:%u", id);
1.116 djm 1006: sshbuf_free(msg);
1.107 djm 1007:
1008: status = get_status(conn, id);
1009: if (status != SSH2_FX_OK)
1010: error("Couldn't sync file: %s", fx2txt(status));
1011:
1.129 djm 1012: return status == SSH2_FX_OK ? 0 : -1;
1.107 djm 1013: }
1014:
1.78 chl 1015: #ifdef notyet
1.11 djm 1016: char *
1.116 djm 1017: do_readlink(struct sftp_conn *conn, const char *path)
1.11 djm 1018: {
1.116 djm 1019: struct sshbuf *msg;
1020: u_int expected_id, count, id;
1.11 djm 1021: char *filename, *longname;
1.116 djm 1022: Attrib a;
1023: u_char type;
1024: int r;
1.11 djm 1025:
1.23 djm 1026: expected_id = id = conn->msg_id++;
1.93 djm 1027: send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
1.11 djm 1028:
1.116 djm 1029: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 1030: fatal_f("sshbuf_new failed");
1.11 djm 1031:
1.116 djm 1032: get_msg(conn, msg);
1033: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1034: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 ! djm 1035: fatal_fr(r, "parse");
1.11 djm 1036:
1037: if (id != expected_id)
1.33 deraadt 1038: fatal("ID mismatch (%u != %u)", id, expected_id);
1.11 djm 1039:
1040: if (type == SSH2_FXP_STATUS) {
1.116 djm 1041: u_int status;
1.11 djm 1042:
1.116 djm 1043: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 ! djm 1044: fatal_fr(r, "parse status");
1.11 djm 1045: error("Couldn't readlink: %s", fx2txt(status));
1.116 djm 1046: sshbuf_free(msg);
1.11 djm 1047: return(NULL);
1048: } else if (type != SSH2_FXP_NAME)
1.33 deraadt 1049: fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.11 djm 1050: SSH2_FXP_NAME, type);
1051:
1.116 djm 1052: if ((r = sshbuf_get_u32(msg, &count)) != 0)
1.137 ! djm 1053: fatal_fr(r, "parse count");
1.11 djm 1054: if (count != 1)
1055: fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
1056:
1.116 djm 1057: if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
1058: (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
1059: (r = decode_attrib(msg, &a)) != 0)
1.137 ! djm 1060: fatal_fr(r, "parse filenames/attrib");
1.11 djm 1061:
1062: debug3("SSH_FXP_READLINK %s -> %s", path, filename);
1063:
1.98 djm 1064: free(longname);
1.11 djm 1065:
1.116 djm 1066: sshbuf_free(msg);
1.11 djm 1067:
1.116 djm 1068: return filename;
1.82 djm 1069: }
1070: #endif
1071:
1072: int
1.84 dtucker 1073: do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
1.82 djm 1074: int quiet)
1075: {
1.116 djm 1076: struct sshbuf *msg;
1.82 djm 1077: u_int id;
1.116 djm 1078: int r;
1.82 djm 1079:
1080: if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
1081: error("Server does not support statvfs@openssh.com extension");
1082: return -1;
1083: }
1084:
1085: id = conn->msg_id++;
1086:
1.116 djm 1087: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 1088: fatal_f("sshbuf_new failed");
1.116 djm 1089: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1090: (r = sshbuf_put_u32(msg, id)) != 0 ||
1091: (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
1092: (r = sshbuf_put_cstring(msg, path)) != 0)
1.137 ! djm 1093: fatal_fr(r, "compose");
1.116 djm 1094: send_msg(conn, msg);
1095: sshbuf_free(msg);
1.82 djm 1096:
1.93 djm 1097: return get_decode_statvfs(conn, st, id, quiet);
1.82 djm 1098: }
1099:
1100: #ifdef notyet
1101: int
1.116 djm 1102: do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
1.84 dtucker 1103: struct sftp_statvfs *st, int quiet)
1.82 djm 1104: {
1.116 djm 1105: struct sshbuf *msg;
1.82 djm 1106: u_int id;
1107:
1108: if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
1109: error("Server does not support fstatvfs@openssh.com extension");
1110: return -1;
1111: }
1112:
1113: id = conn->msg_id++;
1114:
1.116 djm 1115: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 1116: fatal_f("sshbuf_new failed");
1.116 djm 1117: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1118: (r = sshbuf_put_u32(msg, id)) != 0 ||
1119: (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
1120: (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137 ! djm 1121: fatal_fr(r, "compose");
1.116 djm 1122: send_msg(conn, msg);
1123: sshbuf_free(msg);
1.82 djm 1124:
1.93 djm 1125: return get_decode_statvfs(conn, st, id, quiet);
1.1 djm 1126: }
1.78 chl 1127: #endif
1.1 djm 1128:
1.131 djm 1129: int
1130: do_lsetstat(struct sftp_conn *conn, const char *path, Attrib *a)
1131: {
1132: struct sshbuf *msg;
1133: u_int status, id;
1134: int r;
1135:
1136: if ((conn->exts & SFTP_EXT_LSETSTAT) == 0) {
1137: error("Server does not support lsetstat@openssh.com extension");
1138: return -1;
1139: }
1140:
1141: id = conn->msg_id++;
1142: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 1143: fatal_f("sshbuf_new failed");
1.131 djm 1144: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
1145: (r = sshbuf_put_u32(msg, id)) != 0 ||
1146: (r = sshbuf_put_cstring(msg, "lsetstat@openssh.com")) != 0 ||
1147: (r = sshbuf_put_cstring(msg, path)) != 0 ||
1148: (r = encode_attrib(msg, a)) != 0)
1.137 ! djm 1149: fatal_fr(r, "compose");
1.131 djm 1150: send_msg(conn, msg);
1151: sshbuf_free(msg);
1152:
1153: status = get_status(conn, id);
1154: if (status != SSH2_FX_OK)
1155: error("Couldn't setstat on \"%s\": %s", path,
1156: fx2txt(status));
1157:
1158: return status == SSH2_FX_OK ? 0 : -1;
1159: }
1160:
1.21 djm 1161: static void
1.93 djm 1162: send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
1.116 djm 1163: u_int len, const u_char *handle, u_int handle_len)
1.21 djm 1164: {
1.116 djm 1165: struct sshbuf *msg;
1166: int r;
1.28 markus 1167:
1.116 djm 1168: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 1169: fatal_f("sshbuf_new failed");
1.116 djm 1170: if ((r = sshbuf_put_u8(msg, SSH2_FXP_READ)) != 0 ||
1171: (r = sshbuf_put_u32(msg, id)) != 0 ||
1172: (r = sshbuf_put_string(msg, handle, handle_len)) != 0 ||
1173: (r = sshbuf_put_u64(msg, offset)) != 0 ||
1174: (r = sshbuf_put_u32(msg, len)) != 0)
1.137 ! djm 1175: fatal_fr(r, "compose");
1.116 djm 1176: send_msg(conn, msg);
1177: sshbuf_free(msg);
1.28 markus 1178: }
1.21 djm 1179:
1.1 djm 1180: int
1.116 djm 1181: do_download(struct sftp_conn *conn, const char *remote_path,
1182: const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
1183: int fsync_flag)
1.1 djm 1184: {
1.89 djm 1185: Attrib junk;
1.116 djm 1186: struct sshbuf *msg;
1187: u_char *handle;
1188: int local_fd = -1, write_error;
1.134 djm 1189: int read_error, write_errno, lmodified = 0, reordered = 0, r;
1.101 djm 1190: u_int64_t offset = 0, size, highwater;
1.116 djm 1191: u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
1.39 fgsch 1192: off_t progress_counter;
1.116 djm 1193: size_t handle_len;
1.101 djm 1194: struct stat st;
1.21 djm 1195: struct request {
1196: u_int id;
1.116 djm 1197: size_t len;
1.21 djm 1198: u_int64_t offset;
1.28 markus 1199: TAILQ_ENTRY(request) tq;
1.21 djm 1200: };
1201: TAILQ_HEAD(reqhead, request) requests;
1202: struct request *req;
1.116 djm 1203: u_char type;
1.21 djm 1204:
1205: TAILQ_INIT(&requests);
1.1 djm 1206:
1.89 djm 1207: if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1208: return -1;
1.1 djm 1209:
1.86 djm 1210: /* Do not preserve set[ug]id here, as we do not preserve ownership */
1.1 djm 1211: if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1.38 djm 1212: mode = a->perm & 0777;
1.1 djm 1213: else
1214: mode = 0666;
1215:
1.14 djm 1216: if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1.41 djm 1217: (!S_ISREG(a->perm))) {
1218: error("Cannot download non-regular file: %s", remote_path);
1.14 djm 1219: return(-1);
1220: }
1221:
1.21 djm 1222: if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1223: size = a->size;
1224: else
1225: size = 0;
1226:
1.23 djm 1227: buflen = conn->transfer_buflen;
1.116 djm 1228: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 1229: fatal_f("sshbuf_new failed");
1.116 djm 1230:
1231: attrib_clear(&junk); /* Send empty attributes */
1.1 djm 1232:
1233: /* Send open request */
1.23 djm 1234: id = conn->msg_id++;
1.116 djm 1235: if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1236: (r = sshbuf_put_u32(msg, id)) != 0 ||
1237: (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1238: (r = sshbuf_put_u32(msg, SSH2_FXF_READ)) != 0 ||
1239: (r = encode_attrib(msg, &junk)) != 0)
1.137 ! djm 1240: fatal_fr(r, "compose");
1.116 djm 1241: send_msg(conn, msg);
1.33 deraadt 1242: debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1.1 djm 1243:
1.93 djm 1244: handle = get_handle(conn, id, &handle_len,
1.88 djm 1245: "remote open(\"%s\")", remote_path);
1.1 djm 1246: if (handle == NULL) {
1.116 djm 1247: sshbuf_free(msg);
1.1 djm 1248: return(-1);
1249: }
1250:
1.105 djm 1251: local_fd = open(local_path,
1252: O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
1.23 djm 1253: if (local_fd == -1) {
1254: error("Couldn't open local file \"%s\" for writing: %s",
1255: local_path, strerror(errno));
1.101 djm 1256: goto fail;
1257: }
1258: offset = highwater = 0;
1.105 djm 1259: if (resume_flag) {
1.101 djm 1260: if (fstat(local_fd, &st) == -1) {
1261: error("Unable to stat local file \"%s\": %s",
1262: local_path, strerror(errno));
1263: goto fail;
1264: }
1.113 djm 1265: if (st.st_size < 0) {
1266: error("\"%s\" has negative size", local_path);
1267: goto fail;
1268: }
1269: if ((u_int64_t)st.st_size > size) {
1.101 djm 1270: error("Unable to resume download of \"%s\": "
1271: "local file is larger than remote", local_path);
1272: fail:
1273: do_close(conn, handle, handle_len);
1.116 djm 1274: sshbuf_free(msg);
1.101 djm 1275: free(handle);
1.110 djm 1276: if (local_fd != -1)
1277: close(local_fd);
1.101 djm 1278: return -1;
1279: }
1280: offset = highwater = st.st_size;
1.23 djm 1281: }
1282:
1.1 djm 1283: /* Read from remote and write to local */
1.101 djm 1284: write_error = read_error = write_errno = num_req = 0;
1.21 djm 1285: max_req = 1;
1.101 djm 1286: progress_counter = offset;
1.39 fgsch 1287:
1.47 djm 1288: if (showprogress && size != 0)
1289: start_progress_meter(remote_path, size, &progress_counter);
1.39 fgsch 1290:
1.21 djm 1291: while (num_req > 0 || max_req > 0) {
1.116 djm 1292: u_char *data;
1293: size_t len;
1.1 djm 1294:
1.49 djm 1295: /*
1.51 deraadt 1296: * Simulate EOF on interrupt: stop sending new requests and
1.49 djm 1297: * allow outstanding requests to drain gracefully
1298: */
1299: if (interrupted) {
1300: if (num_req == 0) /* If we haven't started yet... */
1301: break;
1302: max_req = 0;
1303: }
1304:
1.21 djm 1305: /* Send some more requests */
1306: while (num_req < max_req) {
1.28 markus 1307: debug3("Request range %llu -> %llu (%d/%d)",
1.25 itojun 1308: (unsigned long long)offset,
1309: (unsigned long long)offset + buflen - 1,
1310: num_req, max_req);
1.108 djm 1311: req = xcalloc(1, sizeof(*req));
1.23 djm 1312: req->id = conn->msg_id++;
1.21 djm 1313: req->len = buflen;
1314: req->offset = offset;
1315: offset += buflen;
1316: num_req++;
1317: TAILQ_INSERT_TAIL(&requests, req, tq);
1.93 djm 1318: send_read_request(conn, req->id, req->offset,
1.21 djm 1319: req->len, handle, handle_len);
1320: }
1.1 djm 1321:
1.116 djm 1322: sshbuf_reset(msg);
1323: get_msg(conn, msg);
1324: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1325: (r = sshbuf_get_u32(msg, &id)) != 0)
1.137 ! djm 1326: fatal_fr(r, "parse");
1.33 deraadt 1327: debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1.21 djm 1328:
1329: /* Find the request in our queue */
1.53 deraadt 1330: for (req = TAILQ_FIRST(&requests);
1.21 djm 1331: req != NULL && req->id != id;
1332: req = TAILQ_NEXT(req, tq))
1333: ;
1334: if (req == NULL)
1335: fatal("Unexpected reply %u", id);
1336:
1337: switch (type) {
1338: case SSH2_FXP_STATUS:
1.116 djm 1339: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 ! djm 1340: fatal_fr(r, "parse status");
1.21 djm 1341: if (status != SSH2_FX_EOF)
1342: read_error = 1;
1343: max_req = 0;
1344: TAILQ_REMOVE(&requests, req, tq);
1.98 djm 1345: free(req);
1.21 djm 1346: num_req--;
1347: break;
1348: case SSH2_FXP_DATA:
1.116 djm 1349: if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1.137 ! djm 1350: fatal_fr(r, "parse data");
1.26 itojun 1351: debug3("Received data %llu -> %llu",
1.28 markus 1352: (unsigned long long)req->offset,
1.26 itojun 1353: (unsigned long long)req->offset + len - 1);
1.21 djm 1354: if (len > req->len)
1355: fatal("Received more data than asked for "
1.116 djm 1356: "%zu > %zu", len, req->len);
1.134 djm 1357: lmodified = 1;
1.21 djm 1358: if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1.44 deraadt 1359: atomicio(vwrite, local_fd, data, len) != len) &&
1.21 djm 1360: !write_error) {
1361: write_errno = errno;
1362: write_error = 1;
1363: max_req = 0;
1364: }
1.101 djm 1365: else if (!reordered && req->offset <= highwater)
1366: highwater = req->offset + len;
1367: else if (!reordered && req->offset > highwater)
1368: reordered = 1;
1.39 fgsch 1369: progress_counter += len;
1.98 djm 1370: free(data);
1.1 djm 1371:
1.21 djm 1372: if (len == req->len) {
1373: TAILQ_REMOVE(&requests, req, tq);
1.98 djm 1374: free(req);
1.21 djm 1375: num_req--;
1376: } else {
1377: /* Resend the request for the missing data */
1378: debug3("Short data block, re-requesting "
1.26 itojun 1379: "%llu -> %llu (%2d)",
1.28 markus 1380: (unsigned long long)req->offset + len,
1.27 itojun 1381: (unsigned long long)req->offset +
1382: req->len - 1, num_req);
1.23 djm 1383: req->id = conn->msg_id++;
1.21 djm 1384: req->len -= len;
1385: req->offset += len;
1.93 djm 1386: send_read_request(conn, req->id,
1.23 djm 1387: req->offset, req->len, handle, handle_len);
1.21 djm 1388: /* Reduce the request size */
1389: if (len < buflen)
1.125 deraadt 1390: buflen = MAXIMUM(MIN_READ_SIZE, len);
1.21 djm 1391: }
1392: if (max_req > 0) { /* max_req = 0 iff EOF received */
1393: if (size > 0 && offset > size) {
1394: /* Only one request at a time
1395: * after the expected EOF */
1396: debug3("Finish at %llu (%2d)",
1.26 itojun 1397: (unsigned long long)offset,
1398: num_req);
1.21 djm 1399: max_req = 1;
1.136 djm 1400: } else if (max_req < conn->num_requests) {
1.21 djm 1401: ++max_req;
1402: }
1.1 djm 1403: }
1.21 djm 1404: break;
1405: default:
1.33 deraadt 1406: fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1.1 djm 1407: SSH2_FXP_DATA, type);
1408: }
1.21 djm 1409: }
1.1 djm 1410:
1.39 fgsch 1411: if (showprogress && size)
1412: stop_progress_meter();
1413:
1.21 djm 1414: /* Sanity check */
1415: if (TAILQ_FIRST(&requests) != NULL)
1416: fatal("Transfer complete, but requests still in queue");
1.101 djm 1417: /* Truncate at highest contiguous point to avoid holes on interrupt */
1418: if (read_error || write_error || interrupted) {
1.105 djm 1419: if (reordered && resume_flag) {
1.101 djm 1420: error("Unable to resume download of \"%s\": "
1421: "server reordered requests", local_path);
1422: }
1423: debug("truncating at %llu", (unsigned long long)highwater);
1.120 djm 1424: if (ftruncate(local_fd, highwater) == -1)
1425: error("ftruncate \"%s\": %s", local_path,
1426: strerror(errno));
1.101 djm 1427: }
1.21 djm 1428: if (read_error) {
1.28 markus 1429: error("Couldn't read from remote file \"%s\" : %s",
1.21 djm 1430: remote_path, fx2txt(status));
1.103 djm 1431: status = -1;
1.23 djm 1432: do_close(conn, handle, handle_len);
1.21 djm 1433: } else if (write_error) {
1434: error("Couldn't write to \"%s\": %s", local_path,
1435: strerror(write_errno));
1.116 djm 1436: status = SSH2_FX_FAILURE;
1.23 djm 1437: do_close(conn, handle, handle_len);
1.21 djm 1438: } else {
1.116 djm 1439: if (do_close(conn, handle, handle_len) != 0 || interrupted)
1440: status = SSH2_FX_FAILURE;
1441: else
1442: status = SSH2_FX_OK;
1.21 djm 1443: /* Override umask and utimes if asked */
1.105 djm 1444: if (preserve_flag && fchmod(local_fd, mode) == -1)
1.21 djm 1445: error("Couldn't set mode on \"%s\": %s", local_path,
1.37 deraadt 1446: strerror(errno));
1.105 djm 1447: if (preserve_flag &&
1448: (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1.21 djm 1449: struct timeval tv[2];
1450: tv[0].tv_sec = a->atime;
1451: tv[1].tv_sec = a->mtime;
1452: tv[0].tv_usec = tv[1].tv_usec = 0;
1453: if (utimes(local_path, tv) == -1)
1454: error("Can't set times on \"%s\": %s",
1.37 deraadt 1455: local_path, strerror(errno));
1.1 djm 1456: }
1.134 djm 1457: if (resume_flag && !lmodified)
1458: logit("File \"%s\" was not modified", local_path);
1459: else if (fsync_flag) {
1.107 djm 1460: debug("syncing \"%s\"", local_path);
1461: if (fsync(local_fd) == -1)
1462: error("Couldn't sync file \"%s\": %s",
1463: local_path, strerror(errno));
1464: }
1.10 djm 1465: }
1.5 djm 1466: close(local_fd);
1.116 djm 1467: sshbuf_free(msg);
1.98 djm 1468: free(handle);
1.23 djm 1469:
1.129 djm 1470: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 1471: }
1472:
1.89 djm 1473: static int
1.116 djm 1474: download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1475: int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
1476: int resume_flag, int fsync_flag)
1.89 djm 1477: {
1478: int i, ret = 0;
1479: SFTP_DIRENT **dir_entries;
1.130 djm 1480: char *filename, *new_src = NULL, *new_dst = NULL;
1.89 djm 1481: mode_t mode = 0777;
1482:
1483: if (depth >= MAX_DIR_DEPTH) {
1484: error("Maximum directory depth exceeded: %d levels", depth);
1485: return -1;
1486: }
1487:
1488: if (dirattrib == NULL &&
1489: (dirattrib = do_stat(conn, src, 1)) == NULL) {
1490: error("Unable to stat remote directory \"%s\"", src);
1491: return -1;
1492: }
1493: if (!S_ISDIR(dirattrib->perm)) {
1494: error("\"%s\" is not a directory", src);
1495: return -1;
1496: }
1.105 djm 1497: if (print_flag)
1.124 schwarze 1498: mprintf("Retrieving %s\n", src);
1.89 djm 1499:
1500: if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1501: mode = dirattrib->perm & 01777;
1502: else {
1503: debug("Server did not send permissions for "
1504: "directory \"%s\"", dst);
1505: }
1506:
1507: if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1508: error("mkdir %s: %s", dst, strerror(errno));
1509: return -1;
1510: }
1511:
1512: if (do_readdir(conn, src, &dir_entries) == -1) {
1513: error("%s: Failed to get directory contents", src);
1514: return -1;
1515: }
1516:
1517: for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1.130 djm 1518: free(new_dst);
1519: free(new_src);
1520:
1.89 djm 1521: filename = dir_entries[i]->filename;
1522: new_dst = path_append(dst, filename);
1523: new_src = path_append(src, filename);
1524:
1525: if (S_ISDIR(dir_entries[i]->a.perm)) {
1526: if (strcmp(filename, ".") == 0 ||
1527: strcmp(filename, "..") == 0)
1528: continue;
1529: if (download_dir_internal(conn, new_src, new_dst,
1.105 djm 1530: depth + 1, &(dir_entries[i]->a), preserve_flag,
1.107 djm 1531: print_flag, resume_flag, fsync_flag) == -1)
1.89 djm 1532: ret = -1;
1533: } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1534: if (do_download(conn, new_src, new_dst,
1.107 djm 1535: &(dir_entries[i]->a), preserve_flag,
1536: resume_flag, fsync_flag) == -1) {
1.89 djm 1537: error("Download of file %s to %s failed",
1538: new_src, new_dst);
1539: ret = -1;
1540: }
1541: } else
1542: logit("%s: not a regular file\n", new_src);
1543:
1544: }
1.130 djm 1545: free(new_dst);
1546: free(new_src);
1.89 djm 1547:
1.105 djm 1548: if (preserve_flag) {
1.89 djm 1549: if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1550: struct timeval tv[2];
1551: tv[0].tv_sec = dirattrib->atime;
1552: tv[1].tv_sec = dirattrib->mtime;
1553: tv[0].tv_usec = tv[1].tv_usec = 0;
1554: if (utimes(dst, tv) == -1)
1555: error("Can't set times on \"%s\": %s",
1556: dst, strerror(errno));
1557: } else
1558: debug("Server did not send times for directory "
1559: "\"%s\"", dst);
1560: }
1561:
1562: free_sftp_dirents(dir_entries);
1563:
1564: return ret;
1565: }
1566:
1567: int
1.116 djm 1568: download_dir(struct sftp_conn *conn, const char *src, const char *dst,
1569: Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1570: int fsync_flag)
1.89 djm 1571: {
1572: char *src_canon;
1573: int ret;
1574:
1575: if ((src_canon = do_realpath(conn, src)) == NULL) {
1.107 djm 1576: error("Unable to canonicalize path \"%s\"", src);
1.89 djm 1577: return -1;
1578: }
1579:
1.105 djm 1580: ret = download_dir_internal(conn, src_canon, dst, 0,
1.107 djm 1581: dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag);
1.98 djm 1582: free(src_canon);
1.89 djm 1583: return ret;
1584: }
1585:
1.1 djm 1586: int
1.116 djm 1587: do_upload(struct sftp_conn *conn, const char *local_path,
1588: const char *remote_path, int preserve_flag, int resume, int fsync_flag)
1.1 djm 1589: {
1.116 djm 1590: int r, local_fd;
1591: u_int status = SSH2_FX_OK;
1592: u_int id;
1593: u_char type;
1.100 dtucker 1594: off_t offset, progress_counter;
1.116 djm 1595: u_char *handle, *data;
1596: struct sshbuf *msg;
1.1 djm 1597: struct stat sb;
1.115 logan 1598: Attrib a, *c = NULL;
1.21 djm 1599: u_int32_t startid;
1600: u_int32_t ackid;
1.22 djm 1601: struct outstanding_ack {
1602: u_int id;
1603: u_int len;
1.77 djm 1604: off_t offset;
1.28 markus 1605: TAILQ_ENTRY(outstanding_ack) tq;
1.22 djm 1606: };
1607: TAILQ_HEAD(ackhead, outstanding_ack) acks;
1.50 pedro 1608: struct outstanding_ack *ack = NULL;
1.116 djm 1609: size_t handle_len;
1.22 djm 1610:
1611: TAILQ_INIT(&acks);
1.1 djm 1612:
1613: if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1614: error("Couldn't open local file \"%s\" for reading: %s",
1615: local_path, strerror(errno));
1616: return(-1);
1617: }
1618: if (fstat(local_fd, &sb) == -1) {
1619: error("Couldn't fstat local file \"%s\": %s",
1620: local_path, strerror(errno));
1.41 djm 1621: close(local_fd);
1622: return(-1);
1623: }
1624: if (!S_ISREG(sb.st_mode)) {
1625: error("%s is not a regular file", local_path);
1.1 djm 1626: close(local_fd);
1627: return(-1);
1628: }
1629: stat_to_attrib(&sb, &a);
1630:
1631: a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1632: a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1633: a.perm &= 0777;
1.105 djm 1634: if (!preserve_flag)
1.1 djm 1635: a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1636:
1.115 logan 1637: if (resume) {
1638: /* Get remote file size if it exists */
1639: if ((c = do_stat(conn, remote_path, 0)) == NULL) {
1.122 djm 1640: close(local_fd);
1.115 logan 1641: return -1;
1642: }
1643:
1644: if ((off_t)c->size >= sb.st_size) {
1645: error("destination file bigger or same size as "
1646: "source file");
1647: close(local_fd);
1648: return -1;
1649: }
1650:
1651: if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
1652: close(local_fd);
1653: return -1;
1654: }
1655: }
1656:
1.116 djm 1657: if ((msg = sshbuf_new()) == NULL)
1.137 ! djm 1658: fatal_f("sshbuf_new failed");
1.1 djm 1659:
1660: /* Send open request */
1.23 djm 1661: id = conn->msg_id++;
1.116 djm 1662: if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
1663: (r = sshbuf_put_u32(msg, id)) != 0 ||
1664: (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
1665: (r = sshbuf_put_u32(msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|
1666: (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC))) != 0 ||
1667: (r = encode_attrib(msg, &a)) != 0)
1.137 ! djm 1668: fatal_fr(r, "compose");
1.116 djm 1669: send_msg(conn, msg);
1.33 deraadt 1670: debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1.1 djm 1671:
1.116 djm 1672: sshbuf_reset(msg);
1.1 djm 1673:
1.93 djm 1674: handle = get_handle(conn, id, &handle_len,
1.88 djm 1675: "remote open(\"%s\")", remote_path);
1.1 djm 1676: if (handle == NULL) {
1677: close(local_fd);
1.116 djm 1678: sshbuf_free(msg);
1.80 djm 1679: return -1;
1.1 djm 1680: }
1681:
1.21 djm 1682: startid = ackid = id + 1;
1.23 djm 1683: data = xmalloc(conn->transfer_buflen);
1.20 djm 1684:
1.1 djm 1685: /* Read from local and write to remote */
1.115 logan 1686: offset = progress_counter = (resume ? c->size : 0);
1.39 fgsch 1687: if (showprogress)
1.100 dtucker 1688: start_progress_meter(local_path, sb.st_size,
1689: &progress_counter);
1.39 fgsch 1690:
1.19 deraadt 1691: for (;;) {
1.1 djm 1692: int len;
1693:
1694: /*
1.51 deraadt 1695: * Can't use atomicio here because it returns 0 on EOF,
1.49 djm 1696: * thus losing the last block of the file.
1.51 deraadt 1697: * Simulate an EOF on interrupt, allowing ACKs from the
1.49 djm 1698: * server to drain.
1.1 djm 1699: */
1.80 djm 1700: if (interrupted || status != SSH2_FX_OK)
1.49 djm 1701: len = 0;
1702: else do
1.23 djm 1703: len = read(local_fd, data, conn->transfer_buflen);
1.1 djm 1704: while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1705:
1706: if (len == -1)
1707: fatal("Couldn't read from \"%s\": %s", local_path,
1708: strerror(errno));
1.21 djm 1709:
1710: if (len != 0) {
1.108 djm 1711: ack = xcalloc(1, sizeof(*ack));
1.22 djm 1712: ack->id = ++id;
1713: ack->offset = offset;
1714: ack->len = len;
1715: TAILQ_INSERT_TAIL(&acks, ack, tq);
1716:
1.116 djm 1717: sshbuf_reset(msg);
1718: if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
1719: (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
1720: (r = sshbuf_put_string(msg, handle,
1721: handle_len)) != 0 ||
1722: (r = sshbuf_put_u64(msg, offset)) != 0 ||
1723: (r = sshbuf_put_string(msg, data, len)) != 0)
1.137 ! djm 1724: fatal_fr(r, "compose");
1.116 djm 1725: send_msg(conn, msg);
1.33 deraadt 1726: debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1.37 deraadt 1727: id, (unsigned long long)offset, len);
1.22 djm 1728: } else if (TAILQ_FIRST(&acks) == NULL)
1.1 djm 1729: break;
1730:
1.22 djm 1731: if (ack == NULL)
1732: fatal("Unexpected ACK %u", id);
1733:
1.28 markus 1734: if (id == startid || len == 0 ||
1.23 djm 1735: id - ackid >= conn->num_requests) {
1.116 djm 1736: u_int rid;
1.31 djm 1737:
1.116 djm 1738: sshbuf_reset(msg);
1739: get_msg(conn, msg);
1740: if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
1741: (r = sshbuf_get_u32(msg, &rid)) != 0)
1.137 ! djm 1742: fatal_fr(r, "parse");
1.22 djm 1743:
1744: if (type != SSH2_FXP_STATUS)
1745: fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1746: "got %d", SSH2_FXP_STATUS, type);
1747:
1.116 djm 1748: if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137 ! djm 1749: fatal_fr(r, "parse status");
1.116 djm 1750: debug3("SSH2_FXP_STATUS %u", status);
1.22 djm 1751:
1752: /* Find the request in our queue */
1.53 deraadt 1753: for (ack = TAILQ_FIRST(&acks);
1.116 djm 1754: ack != NULL && ack->id != rid;
1.22 djm 1755: ack = TAILQ_NEXT(ack, tq))
1756: ;
1757: if (ack == NULL)
1.116 djm 1758: fatal("Can't find request for ID %u", rid);
1.22 djm 1759: TAILQ_REMOVE(&acks, ack, tq);
1.77 djm 1760: debug3("In write loop, ack for %u %u bytes at %lld",
1761: ack->id, ack->len, (long long)ack->offset);
1.21 djm 1762: ++ackid;
1.100 dtucker 1763: progress_counter += ack->len;
1.98 djm 1764: free(ack);
1.1 djm 1765: }
1766: offset += len;
1.77 djm 1767: if (offset < 0)
1.137 ! djm 1768: fatal_f("offset < 0");
1.1 djm 1769: }
1.116 djm 1770: sshbuf_free(msg);
1.80 djm 1771:
1.39 fgsch 1772: if (showprogress)
1773: stop_progress_meter();
1.98 djm 1774: free(data);
1.1 djm 1775:
1.80 djm 1776: if (status != SSH2_FX_OK) {
1777: error("Couldn't write to remote file \"%s\": %s",
1778: remote_path, fx2txt(status));
1.116 djm 1779: status = SSH2_FX_FAILURE;
1.80 djm 1780: }
1781:
1.1 djm 1782: if (close(local_fd) == -1) {
1783: error("Couldn't close local file \"%s\": %s", local_path,
1784: strerror(errno));
1.116 djm 1785: status = SSH2_FX_FAILURE;
1.1 djm 1786: }
1787:
1.10 djm 1788: /* Override umask and utimes if asked */
1.105 djm 1789: if (preserve_flag)
1.23 djm 1790: do_fsetstat(conn, handle, handle_len, &a);
1.10 djm 1791:
1.107 djm 1792: if (fsync_flag)
1793: (void)do_fsync(conn, handle, handle_len);
1794:
1.121 djm 1795: if (do_close(conn, handle, handle_len) != 0)
1.116 djm 1796: status = SSH2_FX_FAILURE;
1797:
1.98 djm 1798: free(handle);
1.5 djm 1799:
1.116 djm 1800: return status == SSH2_FX_OK ? 0 : -1;
1.1 djm 1801: }
1.89 djm 1802:
1803: static int
1.116 djm 1804: upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1805: int depth, int preserve_flag, int print_flag, int resume, int fsync_flag)
1.89 djm 1806: {
1.116 djm 1807: int ret = 0;
1.89 djm 1808: DIR *dirp;
1809: struct dirent *dp;
1.130 djm 1810: char *filename, *new_src = NULL, *new_dst = NULL;
1.89 djm 1811: struct stat sb;
1.121 djm 1812: Attrib a, *dirattrib;
1.89 djm 1813:
1814: if (depth >= MAX_DIR_DEPTH) {
1815: error("Maximum directory depth exceeded: %d levels", depth);
1816: return -1;
1817: }
1818:
1819: if (stat(src, &sb) == -1) {
1820: error("Couldn't stat directory \"%s\": %s",
1821: src, strerror(errno));
1822: return -1;
1823: }
1824: if (!S_ISDIR(sb.st_mode)) {
1825: error("\"%s\" is not a directory", src);
1826: return -1;
1827: }
1.105 djm 1828: if (print_flag)
1.124 schwarze 1829: mprintf("Entering %s\n", src);
1.89 djm 1830:
1831: attrib_clear(&a);
1832: stat_to_attrib(&sb, &a);
1833: a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1834: a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1835: a.perm &= 01777;
1.105 djm 1836: if (!preserve_flag)
1.89 djm 1837: a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1.101 djm 1838:
1.89 djm 1839: /*
1.121 djm 1840: * sftp lacks a portable status value to match errno EEXIST,
1841: * so if we get a failure back then we must check whether
1842: * the path already existed and is a directory.
1.89 djm 1843: */
1.121 djm 1844: if (do_mkdir(conn, dst, &a, 0) != 0) {
1845: if ((dirattrib = do_stat(conn, dst, 0)) == NULL)
1.89 djm 1846: return -1;
1.121 djm 1847: if (!S_ISDIR(dirattrib->perm)) {
1848: error("\"%s\" exists but is not a directory", dst);
1.89 djm 1849: return -1;
1.121 djm 1850: }
1.89 djm 1851: }
1852:
1853: if ((dirp = opendir(src)) == NULL) {
1854: error("Failed to open dir \"%s\": %s", src, strerror(errno));
1855: return -1;
1856: }
1.101 djm 1857:
1.89 djm 1858: while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1859: if (dp->d_ino == 0)
1860: continue;
1.130 djm 1861: free(new_dst);
1862: free(new_src);
1.89 djm 1863: filename = dp->d_name;
1864: new_dst = path_append(dst, filename);
1865: new_src = path_append(src, filename);
1866:
1.90 dtucker 1867: if (lstat(new_src, &sb) == -1) {
1868: logit("%s: lstat failed: %s", filename,
1869: strerror(errno));
1870: ret = -1;
1871: } else if (S_ISDIR(sb.st_mode)) {
1.89 djm 1872: if (strcmp(filename, ".") == 0 ||
1873: strcmp(filename, "..") == 0)
1874: continue;
1875:
1876: if (upload_dir_internal(conn, new_src, new_dst,
1.115 logan 1877: depth + 1, preserve_flag, print_flag, resume,
1.107 djm 1878: fsync_flag) == -1)
1.89 djm 1879: ret = -1;
1.90 dtucker 1880: } else if (S_ISREG(sb.st_mode)) {
1.105 djm 1881: if (do_upload(conn, new_src, new_dst,
1.115 logan 1882: preserve_flag, resume, fsync_flag) == -1) {
1.89 djm 1883: error("Uploading of file %s to %s failed!",
1884: new_src, new_dst);
1885: ret = -1;
1886: }
1887: } else
1888: logit("%s: not a regular file\n", filename);
1889: }
1.130 djm 1890: free(new_dst);
1891: free(new_src);
1.89 djm 1892:
1893: do_setstat(conn, dst, &a);
1894:
1895: (void) closedir(dirp);
1896: return ret;
1897: }
1898:
1899: int
1.116 djm 1900: upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
1901: int preserve_flag, int print_flag, int resume, int fsync_flag)
1.89 djm 1902: {
1903: char *dst_canon;
1904: int ret;
1905:
1906: if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1.107 djm 1907: error("Unable to canonicalize path \"%s\"", dst);
1.89 djm 1908: return -1;
1909: }
1910:
1.106 djm 1911: ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
1.115 logan 1912: print_flag, resume, fsync_flag);
1.107 djm 1913:
1.98 djm 1914: free(dst_canon);
1.89 djm 1915: return ret;
1916: }
1917:
1918: char *
1.116 djm 1919: path_append(const char *p1, const char *p2)
1.89 djm 1920: {
1921: char *ret;
1922: size_t len = strlen(p1) + strlen(p2) + 2;
1923:
1924: ret = xmalloc(len);
1925: strlcpy(ret, p1, len);
1926: if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1927: strlcat(ret, "/", len);
1928: strlcat(ret, p2, len);
1929:
1930: return(ret);
1931: }
1932: