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