17c478bd9Sstevel@tonic-gate /*
290685d2cSjp161948 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
37c478bd9Sstevel@tonic-gate *
490685d2cSjp161948 * Permission to use, copy, modify, and distribute this software for any
590685d2cSjp161948 * purpose with or without fee is hereby granted, provided that the above
690685d2cSjp161948 * copyright notice and this permission notice appear in all copies.
77c478bd9Sstevel@tonic-gate *
890685d2cSjp161948 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
990685d2cSjp161948 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1090685d2cSjp161948 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1190685d2cSjp161948 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1290685d2cSjp161948 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1390685d2cSjp161948 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1490685d2cSjp161948 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
157c478bd9Sstevel@tonic-gate */
167c478bd9Sstevel@tonic-gate
1790685d2cSjp161948 /* $OpenBSD: sftp-client.c,v 1.76 2007/01/22 11:32:50 djm Exp $ */
1890685d2cSjp161948
197c478bd9Sstevel@tonic-gate /* XXX: memleaks */
207c478bd9Sstevel@tonic-gate /* XXX: signed vs unsigned */
217c478bd9Sstevel@tonic-gate /* XXX: remove all logging, only return status codes */
227c478bd9Sstevel@tonic-gate /* XXX: copy between two remote sites */
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate #include "includes.h"
257c478bd9Sstevel@tonic-gate
2690685d2cSjp161948 #include <sys/types.h>
2790685d2cSjp161948 #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include "sys-queue.h"
2990685d2cSjp161948 #ifdef HAVE_SYS_STAT_H
3090685d2cSjp161948 # include <sys/stat.h>
3190685d2cSjp161948 #endif
3290685d2cSjp161948 #ifdef HAVE_SYS_TIME_H
3390685d2cSjp161948 # include <sys/time.h>
3490685d2cSjp161948 #endif
3590685d2cSjp161948 #include <sys/uio.h>
367c478bd9Sstevel@tonic-gate
3790685d2cSjp161948 #include <errno.h>
3890685d2cSjp161948 #include <fcntl.h>
3990685d2cSjp161948 #include <signal.h>
4090685d2cSjp161948 #include <stdarg.h>
4190685d2cSjp161948 #include <stdio.h>
4290685d2cSjp161948 #include <string.h>
4390685d2cSjp161948 #include <unistd.h>
4490685d2cSjp161948
4590685d2cSjp161948 #include "xmalloc.h"
467c478bd9Sstevel@tonic-gate #include "buffer.h"
477c478bd9Sstevel@tonic-gate #include "bufaux.h"
487c478bd9Sstevel@tonic-gate #include "log.h"
497c478bd9Sstevel@tonic-gate #include "atomicio.h"
5090685d2cSjp161948 #include "progressmeter.h"
5190685d2cSjp161948 #include "misc.h"
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #include "sftp.h"
547c478bd9Sstevel@tonic-gate #include "sftp-common.h"
557c478bd9Sstevel@tonic-gate #include "sftp-client.h"
567c478bd9Sstevel@tonic-gate
5790685d2cSjp161948 extern volatile sig_atomic_t interrupted;
5890685d2cSjp161948 extern int showprogress;
5990685d2cSjp161948
6090685d2cSjp161948 /* Minimum amount of data to read at a time */
617c478bd9Sstevel@tonic-gate #define MIN_READ_SIZE 512
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate struct sftp_conn {
647c478bd9Sstevel@tonic-gate int fd_in;
657c478bd9Sstevel@tonic-gate int fd_out;
667c478bd9Sstevel@tonic-gate u_int transfer_buflen;
677c478bd9Sstevel@tonic-gate u_int num_requests;
687c478bd9Sstevel@tonic-gate u_int version;
697c478bd9Sstevel@tonic-gate u_int msg_id;
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate static void
send_msg(int fd,Buffer * m)737c478bd9Sstevel@tonic-gate send_msg(int fd, Buffer *m)
747c478bd9Sstevel@tonic-gate {
7590685d2cSjp161948 char mlen[4];
7690685d2cSjp161948 struct iovec iov[2];
777c478bd9Sstevel@tonic-gate
7890685d2cSjp161948 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
7990685d2cSjp161948 fatal("Outbound message too long %u", buffer_len(m));
807c478bd9Sstevel@tonic-gate
8190685d2cSjp161948 /* Send length first */
8290685d2cSjp161948 put_u32(mlen, buffer_len(m));
8390685d2cSjp161948 iov[0].iov_base = mlen;
8490685d2cSjp161948 iov[0].iov_len = sizeof(mlen);
8590685d2cSjp161948 iov[1].iov_base = buffer_ptr(m);
8690685d2cSjp161948 iov[1].iov_len = buffer_len(m);
8790685d2cSjp161948
8890685d2cSjp161948 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
897c478bd9Sstevel@tonic-gate fatal("Couldn't send packet: %s", strerror(errno));
907c478bd9Sstevel@tonic-gate
9190685d2cSjp161948 buffer_clear(m);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate static void
get_msg(int fd,Buffer * m)957c478bd9Sstevel@tonic-gate get_msg(int fd, Buffer *m)
967c478bd9Sstevel@tonic-gate {
9790685d2cSjp161948 u_int msg_len;
987c478bd9Sstevel@tonic-gate
9990685d2cSjp161948 buffer_append_space(m, 4);
10090685d2cSjp161948 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
10190685d2cSjp161948 if (errno == EPIPE)
1027c478bd9Sstevel@tonic-gate fatal("Connection closed");
10390685d2cSjp161948 else
1047c478bd9Sstevel@tonic-gate fatal("Couldn't read packet: %s", strerror(errno));
10590685d2cSjp161948 }
1067c478bd9Sstevel@tonic-gate
10790685d2cSjp161948 msg_len = buffer_get_int(m);
10890685d2cSjp161948 if (msg_len > SFTP_MAX_MSG_LENGTH)
1097c478bd9Sstevel@tonic-gate fatal("Received message too long %u", msg_len);
1107c478bd9Sstevel@tonic-gate
11190685d2cSjp161948 buffer_append_space(m, msg_len);
11290685d2cSjp161948 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
11390685d2cSjp161948 if (errno == EPIPE)
1147c478bd9Sstevel@tonic-gate fatal("Connection closed");
11590685d2cSjp161948 else
11690685d2cSjp161948 fatal("Read packet: %s", strerror(errno));
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate static void
send_string_request(int fd,u_int id,u_int code,char * s,u_int len)1217c478bd9Sstevel@tonic-gate send_string_request(int fd, u_int id, u_int code, char *s,
1227c478bd9Sstevel@tonic-gate u_int len)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate Buffer msg;
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate buffer_init(&msg);
1277c478bd9Sstevel@tonic-gate buffer_put_char(&msg, code);
1287c478bd9Sstevel@tonic-gate buffer_put_int(&msg, id);
1297c478bd9Sstevel@tonic-gate buffer_put_string(&msg, s, len);
1307c478bd9Sstevel@tonic-gate send_msg(fd, &msg);
1317c478bd9Sstevel@tonic-gate debug3("Sent message fd %d T:%u I:%u", fd, code, id);
1327c478bd9Sstevel@tonic-gate buffer_free(&msg);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate static void
send_string_attrs_request(int fd,u_int id,u_int code,char * s,u_int len,Attrib * a)1367c478bd9Sstevel@tonic-gate send_string_attrs_request(int fd, u_int id, u_int code, char *s,
1377c478bd9Sstevel@tonic-gate u_int len, Attrib *a)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate Buffer msg;
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate buffer_init(&msg);
1427c478bd9Sstevel@tonic-gate buffer_put_char(&msg, code);
1437c478bd9Sstevel@tonic-gate buffer_put_int(&msg, id);
1447c478bd9Sstevel@tonic-gate buffer_put_string(&msg, s, len);
1457c478bd9Sstevel@tonic-gate encode_attrib(&msg, a);
1467c478bd9Sstevel@tonic-gate send_msg(fd, &msg);
1477c478bd9Sstevel@tonic-gate debug3("Sent message fd %d T:%u I:%u", fd, code, id);
1487c478bd9Sstevel@tonic-gate buffer_free(&msg);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate static u_int
get_status(int fd,u_int expected_id)1527c478bd9Sstevel@tonic-gate get_status(int fd, u_int expected_id)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate Buffer msg;
1557c478bd9Sstevel@tonic-gate u_int type, id, status;
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate buffer_init(&msg);
1587c478bd9Sstevel@tonic-gate get_msg(fd, &msg);
1597c478bd9Sstevel@tonic-gate type = buffer_get_char(&msg);
1607c478bd9Sstevel@tonic-gate id = buffer_get_int(&msg);
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate if (id != expected_id)
1637c478bd9Sstevel@tonic-gate fatal("ID mismatch (%u != %u)", id, expected_id);
1647c478bd9Sstevel@tonic-gate if (type != SSH2_FXP_STATUS)
1657c478bd9Sstevel@tonic-gate fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
1667c478bd9Sstevel@tonic-gate SSH2_FXP_STATUS, type);
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate status = buffer_get_int(&msg);
1697c478bd9Sstevel@tonic-gate buffer_free(&msg);
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate debug3("SSH2_FXP_STATUS %u", status);
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate return(status);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate static char *
get_handle(int fd,u_int expected_id,u_int * len)1777c478bd9Sstevel@tonic-gate get_handle(int fd, u_int expected_id, u_int *len)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate Buffer msg;
1807c478bd9Sstevel@tonic-gate u_int type, id;
1817c478bd9Sstevel@tonic-gate char *handle;
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate buffer_init(&msg);
1847c478bd9Sstevel@tonic-gate get_msg(fd, &msg);
1857c478bd9Sstevel@tonic-gate type = buffer_get_char(&msg);
1867c478bd9Sstevel@tonic-gate id = buffer_get_int(&msg);
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate if (id != expected_id)
1897c478bd9Sstevel@tonic-gate fatal("ID mismatch (%u != %u)", id, expected_id);
1907c478bd9Sstevel@tonic-gate if (type == SSH2_FXP_STATUS) {
1917c478bd9Sstevel@tonic-gate int status = buffer_get_int(&msg);
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate error("Couldn't get handle: %s", fx2txt(status));
19490685d2cSjp161948 buffer_free(&msg);
1957c478bd9Sstevel@tonic-gate return(NULL);
1967c478bd9Sstevel@tonic-gate } else if (type != SSH2_FXP_HANDLE)
1977c478bd9Sstevel@tonic-gate fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
1987c478bd9Sstevel@tonic-gate SSH2_FXP_HANDLE, type);
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate handle = buffer_get_string(&msg, len);
2017c478bd9Sstevel@tonic-gate buffer_free(&msg);
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate return(handle);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate static Attrib *
get_decode_stat(int fd,u_int expected_id,int quiet)2077c478bd9Sstevel@tonic-gate get_decode_stat(int fd, u_int expected_id, int quiet)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate Buffer msg;
2107c478bd9Sstevel@tonic-gate u_int type, id;
2117c478bd9Sstevel@tonic-gate Attrib *a;
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate buffer_init(&msg);
2147c478bd9Sstevel@tonic-gate get_msg(fd, &msg);
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate type = buffer_get_char(&msg);
2177c478bd9Sstevel@tonic-gate id = buffer_get_int(&msg);
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate debug3("Received stat reply T:%u I:%u", type, id);
2207c478bd9Sstevel@tonic-gate if (id != expected_id)
2217c478bd9Sstevel@tonic-gate fatal("ID mismatch (%u != %u)", id, expected_id);
2227c478bd9Sstevel@tonic-gate if (type == SSH2_FXP_STATUS) {
2237c478bd9Sstevel@tonic-gate int status = buffer_get_int(&msg);
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate if (quiet)
2267c478bd9Sstevel@tonic-gate debug("Couldn't stat remote file: %s", fx2txt(status));
2277c478bd9Sstevel@tonic-gate else
2287c478bd9Sstevel@tonic-gate error("Couldn't stat remote file: %s", fx2txt(status));
22990685d2cSjp161948 buffer_free(&msg);
2307c478bd9Sstevel@tonic-gate return(NULL);
2317c478bd9Sstevel@tonic-gate } else if (type != SSH2_FXP_ATTRS) {
2327c478bd9Sstevel@tonic-gate fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
2337c478bd9Sstevel@tonic-gate SSH2_FXP_ATTRS, type);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate a = decode_attrib(&msg);
2367c478bd9Sstevel@tonic-gate buffer_free(&msg);
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate return(a);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate struct sftp_conn *
do_init(int fd_in,int fd_out,u_int transfer_buflen,u_int num_requests)2427c478bd9Sstevel@tonic-gate do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate u_int type;
2457c478bd9Sstevel@tonic-gate int version;
2467c478bd9Sstevel@tonic-gate Buffer msg;
2477c478bd9Sstevel@tonic-gate struct sftp_conn *ret;
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate buffer_init(&msg);
2507c478bd9Sstevel@tonic-gate buffer_put_char(&msg, SSH2_FXP_INIT);
2517c478bd9Sstevel@tonic-gate buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
2527c478bd9Sstevel@tonic-gate send_msg(fd_out, &msg);
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate buffer_clear(&msg);
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate get_msg(fd_in, &msg);
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate /* Expecting a VERSION reply */
2597c478bd9Sstevel@tonic-gate if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
2607c478bd9Sstevel@tonic-gate error("Invalid packet back from SSH2_FXP_INIT (type %u)",
2617c478bd9Sstevel@tonic-gate type);
2627c478bd9Sstevel@tonic-gate buffer_free(&msg);
2637c478bd9Sstevel@tonic-gate return(NULL);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate version = buffer_get_int(&msg);
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate debug2("Remote version: %d", version);
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate /* Check for extensions */
2707c478bd9Sstevel@tonic-gate while (buffer_len(&msg) > 0) {
2717c478bd9Sstevel@tonic-gate char *name = buffer_get_string(&msg, NULL);
2727c478bd9Sstevel@tonic-gate char *value = buffer_get_string(&msg, NULL);
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate debug2("Init extension: \"%s\"", name);
2757c478bd9Sstevel@tonic-gate xfree(name);
2767c478bd9Sstevel@tonic-gate xfree(value);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate buffer_free(&msg);
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate ret = xmalloc(sizeof(*ret));
2827c478bd9Sstevel@tonic-gate ret->fd_in = fd_in;
2837c478bd9Sstevel@tonic-gate ret->fd_out = fd_out;
2847c478bd9Sstevel@tonic-gate ret->transfer_buflen = transfer_buflen;
2857c478bd9Sstevel@tonic-gate ret->num_requests = num_requests;
2867c478bd9Sstevel@tonic-gate ret->version = version;
2877c478bd9Sstevel@tonic-gate ret->msg_id = 1;
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate /* Some filexfer v.0 servers don't support large packets */
2907c478bd9Sstevel@tonic-gate if (version == 0)
2917c478bd9Sstevel@tonic-gate ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate return(ret);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate u_int
sftp_proto_version(struct sftp_conn * conn)2977c478bd9Sstevel@tonic-gate sftp_proto_version(struct sftp_conn *conn)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate return(conn->version);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate int
do_close(struct sftp_conn * conn,char * handle,u_int handle_len)3037c478bd9Sstevel@tonic-gate do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
3047c478bd9Sstevel@tonic-gate {
3057c478bd9Sstevel@tonic-gate u_int id, status;
3067c478bd9Sstevel@tonic-gate Buffer msg;
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate buffer_init(&msg);
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate id = conn->msg_id++;
3117c478bd9Sstevel@tonic-gate buffer_put_char(&msg, SSH2_FXP_CLOSE);
3127c478bd9Sstevel@tonic-gate buffer_put_int(&msg, id);
3137c478bd9Sstevel@tonic-gate buffer_put_string(&msg, handle, handle_len);
3147c478bd9Sstevel@tonic-gate send_msg(conn->fd_out, &msg);
3157c478bd9Sstevel@tonic-gate debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate status = get_status(conn->fd_in, id);
3187c478bd9Sstevel@tonic-gate if (status != SSH2_FX_OK)
3197c478bd9Sstevel@tonic-gate error("Couldn't close file: %s", fx2txt(status));
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate buffer_free(&msg);
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate return(status);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate static int
do_lsreaddir(struct sftp_conn * conn,char * path,int printflag,SFTP_DIRENT *** dir)3287c478bd9Sstevel@tonic-gate do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
3297c478bd9Sstevel@tonic-gate SFTP_DIRENT ***dir)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate Buffer msg;
33290685d2cSjp161948 u_int count, type, id, handle_len, i, expected_id, ents = 0;
3337c478bd9Sstevel@tonic-gate char *handle;
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate id = conn->msg_id++;
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate buffer_init(&msg);
3387c478bd9Sstevel@tonic-gate buffer_put_char(&msg, SSH2_FXP_OPENDIR);
3397c478bd9Sstevel@tonic-gate buffer_put_int(&msg, id);
3407c478bd9Sstevel@tonic-gate buffer_put_cstring(&msg, path);
3417c478bd9Sstevel@tonic-gate send_msg(conn->fd_out, &msg);
3427c478bd9Sstevel@tonic-gate
3437c478bd9Sstevel@tonic-gate buffer_clear(&msg);
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate handle = get_handle(conn->fd_in, id, &handle_len);
3467c478bd9Sstevel@tonic-gate if (handle == NULL)
3477c478bd9Sstevel@tonic-gate return(-1);
3487c478bd9Sstevel@tonic-gate
3497c478bd9Sstevel@tonic-gate if (dir) {
3507c478bd9Sstevel@tonic-gate ents = 0;
3517c478bd9Sstevel@tonic-gate *dir = xmalloc(sizeof(**dir));
3527c478bd9Sstevel@tonic-gate (*dir)[0] = NULL;
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate
35590685d2cSjp161948 for (; !interrupted;) {
3567c478bd9Sstevel@tonic-gate id = expected_id = conn->msg_id++;
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate debug3("Sending SSH2_FXP_READDIR I:%u", id);
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate buffer_clear(&msg);
3617c478bd9Sstevel@tonic-gate buffer_put_char(&msg, SSH2_FXP_READDIR);
3627c478bd9Sstevel@tonic-gate buffer_put_int(&msg, id);
3637c478bd9Sstevel@tonic-gate buffer_put_string(&msg, handle, handle_len);
3647c478bd9Sstevel@tonic-gate send_msg(conn->fd_out, &msg);
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate buffer_clear(&msg);
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate get_msg(conn->fd_in, &msg);
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate type = buffer_get_char(&msg);
3717c478bd9Sstevel@tonic-gate id = buffer_get_int(&msg);
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate debug3("Received reply T:%u I:%u", type, id);
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate if (id != expected_id)
3767c478bd9Sstevel@tonic-gate fatal("ID mismatch (%u != %u)", id, expected_id);
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate if (type == SSH2_FXP_STATUS) {
3797c478bd9Sstevel@tonic-gate int status = buffer_get_int(&msg);
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate debug3("Received SSH2_FXP_STATUS %d", status);
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate if (status == SSH2_FX_EOF) {
3847c478bd9Sstevel@tonic-gate break;
3857c478bd9Sstevel@tonic-gate } else {
3867c478bd9Sstevel@tonic-gate error("Couldn't read directory: %s",
3877c478bd9Sstevel@tonic-gate fx2txt(status));
3887c478bd9Sstevel@tonic-gate do_close(conn, handle, handle_len);
38990685d2cSjp161948 xfree(handle);
3907c478bd9Sstevel@tonic-gate return(status);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate } else if (type != SSH2_FXP_NAME)
3937c478bd9Sstevel@tonic-gate fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
3947c478bd9Sstevel@tonic-gate SSH2_FXP_NAME, type);
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gate count = buffer_get_int(&msg);
3977c478bd9Sstevel@tonic-gate if (count == 0)
3987c478bd9Sstevel@tonic-gate break;
3997c478bd9Sstevel@tonic-gate debug3("Received %d SSH2_FXP_NAME responses", count);
4007c478bd9Sstevel@tonic-gate for (i = 0; i < count; i++) {
4017c478bd9Sstevel@tonic-gate char *filename, *longname;
4027c478bd9Sstevel@tonic-gate Attrib *a;
4037c478bd9Sstevel@tonic-gate
4047c478bd9Sstevel@tonic-gate filename = buffer_get_string(&msg, NULL);
4057c478bd9Sstevel@tonic-gate longname = buffer_get_string(&msg, NULL);
4067c478bd9Sstevel@tonic-gate a = decode_attrib(&msg);
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate if (printflag)
4097c478bd9Sstevel@tonic-gate printf("%s\n", longname);
4107c478bd9Sstevel@tonic-gate
4117c478bd9Sstevel@tonic-gate if (dir) {
41290685d2cSjp161948 *dir = xrealloc(*dir, (ents + 2) * sizeof(**dir));
4137c478bd9Sstevel@tonic-gate (*dir)[ents] = xmalloc(sizeof(***dir));
4147c478bd9Sstevel@tonic-gate (*dir)[ents]->filename = xstrdup(filename);
4157c478bd9Sstevel@tonic-gate (*dir)[ents]->longname = xstrdup(longname);
4167c478bd9Sstevel@tonic-gate memcpy(&(*dir)[ents]->a, a, sizeof(*a));
4177c478bd9Sstevel@tonic-gate (*dir)[++ents] = NULL;
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate
4207c478bd9Sstevel@tonic-gate xfree(filename);
4217c478bd9Sstevel@tonic-gate xfree(longname);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate buffer_free(&msg);
4267c478bd9Sstevel@tonic-gate do_close(conn, handle, handle_len);
4277c478bd9Sstevel@tonic-gate xfree(handle);
4287c478bd9Sstevel@tonic-gate
42990685d2cSjp161948 /* Don't return partial matches on interrupt */
43090685d2cSjp161948 if (interrupted && dir != NULL && *dir != NULL) {
43190685d2cSjp161948 free_sftp_dirents(*dir);
43290685d2cSjp161948 *dir = xmalloc(sizeof(**dir));
43390685d2cSjp161948 **dir = NULL;
43490685d2cSjp161948 }
43590685d2cSjp161948
4367c478bd9Sstevel@tonic-gate return(0);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate
4397c478bd9Sstevel@tonic-gate int
do_readdir(struct sftp_conn * conn,char * path,SFTP_DIRENT *** dir)4407c478bd9Sstevel@tonic-gate do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate return(do_lsreaddir(conn, path, 0, dir));
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate
free_sftp_dirents(SFTP_DIRENT ** s)4457c478bd9Sstevel@tonic-gate void free_sftp_dirents(SFTP_DIRENT **s)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate int i;
4487c478bd9Sstevel@tonic-gate
4497c478bd9Sstevel@tonic-gate for (i = 0; s[i]; i++) {
4507c478bd9Sstevel@tonic-gate xfree(s[i]->filename);
4517c478bd9Sstevel@tonic-gate xfree(s[i]->longname);
4527c478bd9Sstevel@tonic-gate xfree(s[i]);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate xfree(s);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate
4577c478bd9Sstevel@tonic-gate int
do_rm(struct sftp_conn * conn,char * path)4587c478bd9Sstevel@tonic-gate do_rm(struct sftp_conn *conn, char *path)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate u_int status, id;
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate id = conn->msg_id++;
4657c478bd9Sstevel@tonic-gate send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
4667c478bd9Sstevel@tonic-gate strlen(path));
4677c478bd9Sstevel@tonic-gate status = get_status(conn->fd_in, id);
4687c478bd9Sstevel@tonic-gate if (status != SSH2_FX_OK)
4697c478bd9Sstevel@tonic-gate error("Couldn't delete file: %s", fx2txt(status));
4707c478bd9Sstevel@tonic-gate return(status);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate int
do_mkdir(struct sftp_conn * conn,char * path,Attrib * a)4747c478bd9Sstevel@tonic-gate do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate u_int status, id;
4777c478bd9Sstevel@tonic-gate
4787c478bd9Sstevel@tonic-gate id = conn->msg_id++;
4797c478bd9Sstevel@tonic-gate send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
4807c478bd9Sstevel@tonic-gate strlen(path), a);
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate status = get_status(conn->fd_in, id);
4837c478bd9Sstevel@tonic-gate if (status != SSH2_FX_OK)
4847c478bd9Sstevel@tonic-gate error("Couldn't create directory: %s", fx2txt(status));
4857c478bd9Sstevel@tonic-gate
4867c478bd9Sstevel@tonic-gate return(status);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate
4897c478bd9Sstevel@tonic-gate int
do_rmdir(struct sftp_conn * conn,char * path)4907c478bd9Sstevel@tonic-gate do_rmdir(struct sftp_conn *conn, char *path)
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate u_int status, id;
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate id = conn->msg_id++;
4957c478bd9Sstevel@tonic-gate send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
4967c478bd9Sstevel@tonic-gate strlen(path));
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate status = get_status(conn->fd_in, id);
4997c478bd9Sstevel@tonic-gate if (status != SSH2_FX_OK)
5007c478bd9Sstevel@tonic-gate error("Couldn't remove directory: %s", fx2txt(status));
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate return(status);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate Attrib *
do_stat(struct sftp_conn * conn,char * path,int quiet)5067c478bd9Sstevel@tonic-gate do_stat(struct sftp_conn *conn, char *path, int quiet)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate u_int id;
5097c478bd9Sstevel@tonic-gate
5107c478bd9Sstevel@tonic-gate id = conn->msg_id++;
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate send_string_request(conn->fd_out, id,
5137c478bd9Sstevel@tonic-gate conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
5147c478bd9Sstevel@tonic-gate path, strlen(path));
5157c478bd9Sstevel@tonic-gate
5167c478bd9Sstevel@tonic-gate return(get_decode_stat(conn->fd_in, id, quiet));
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate
5197c478bd9Sstevel@tonic-gate Attrib *
do_lstat(struct sftp_conn * conn,char * path,int quiet)5207c478bd9Sstevel@tonic-gate do_lstat(struct sftp_conn *conn, char *path, int quiet)
5217c478bd9Sstevel@tonic-gate {
5227c478bd9Sstevel@tonic-gate u_int id;
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate if (conn->version == 0) {
5257c478bd9Sstevel@tonic-gate if (quiet)
5267c478bd9Sstevel@tonic-gate debug("Server version does not support lstat operation");
5277c478bd9Sstevel@tonic-gate else
5287c478bd9Sstevel@tonic-gate log("Server version does not support lstat operation");
5297c478bd9Sstevel@tonic-gate return(do_stat(conn, path, quiet));
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate id = conn->msg_id++;
5337c478bd9Sstevel@tonic-gate send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
5347c478bd9Sstevel@tonic-gate strlen(path));
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate return(get_decode_stat(conn->fd_in, id, quiet));
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate
53990685d2cSjp161948 /* this is never used so hush the lint */
5407c478bd9Sstevel@tonic-gate #if 0
5417c478bd9Sstevel@tonic-gate Attrib *
5427c478bd9Sstevel@tonic-gate do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate u_int id;
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate id = conn->msg_id++;
5477c478bd9Sstevel@tonic-gate send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
5487c478bd9Sstevel@tonic-gate handle_len);
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate return(get_decode_stat(conn->fd_in, id, quiet));
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate #endif
5537c478bd9Sstevel@tonic-gate
5547c478bd9Sstevel@tonic-gate int
do_setstat(struct sftp_conn * conn,char * path,Attrib * a)5557c478bd9Sstevel@tonic-gate do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
5567c478bd9Sstevel@tonic-gate {
5577c478bd9Sstevel@tonic-gate u_int status, id;
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate id = conn->msg_id++;
5607c478bd9Sstevel@tonic-gate send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
5617c478bd9Sstevel@tonic-gate strlen(path), a);
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate status = get_status(conn->fd_in, id);
5647c478bd9Sstevel@tonic-gate if (status != SSH2_FX_OK)
5657c478bd9Sstevel@tonic-gate error("Couldn't setstat on \"%s\": %s", path,
5667c478bd9Sstevel@tonic-gate fx2txt(status));
5677c478bd9Sstevel@tonic-gate
5687c478bd9Sstevel@tonic-gate return(status);
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate int
do_fsetstat(struct sftp_conn * conn,char * handle,u_int handle_len,Attrib * a)5727c478bd9Sstevel@tonic-gate do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
5737c478bd9Sstevel@tonic-gate Attrib *a)
5747c478bd9Sstevel@tonic-gate {
5757c478bd9Sstevel@tonic-gate u_int status, id;
5767c478bd9Sstevel@tonic-gate
5777c478bd9Sstevel@tonic-gate id = conn->msg_id++;
5787c478bd9Sstevel@tonic-gate send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
5797c478bd9Sstevel@tonic-gate handle_len, a);
5807c478bd9Sstevel@tonic-gate
5817c478bd9Sstevel@tonic-gate status = get_status(conn->fd_in, id);
5827c478bd9Sstevel@tonic-gate if (status != SSH2_FX_OK)
5837c478bd9Sstevel@tonic-gate error("Couldn't fsetstat: %s", fx2txt(status));
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate return(status);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate
5887c478bd9Sstevel@tonic-gate char *
do_realpath(struct sftp_conn * conn,char * path)5897c478bd9Sstevel@tonic-gate do_realpath(struct sftp_conn *conn, char *path)
5907c478bd9Sstevel@tonic-gate {
5917c478bd9Sstevel@tonic-gate Buffer msg;
5927c478bd9Sstevel@tonic-gate u_int type, expected_id, count, id;
5937c478bd9Sstevel@tonic-gate char *filename, *longname;
59490685d2cSjp161948 /* LINTED */
59590685d2cSjp161948 Attrib *a;
5967c478bd9Sstevel@tonic-gate
5977c478bd9Sstevel@tonic-gate expected_id = id = conn->msg_id++;
5987c478bd9Sstevel@tonic-gate send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
5997c478bd9Sstevel@tonic-gate strlen(path));
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate buffer_init(&msg);
6027c478bd9Sstevel@tonic-gate
6037c478bd9Sstevel@tonic-gate get_msg(conn->fd_in, &msg);
6047c478bd9Sstevel@tonic-gate type = buffer_get_char(&msg);
6057c478bd9Sstevel@tonic-gate id = buffer_get_int(&msg);
6067c478bd9Sstevel@tonic-gate
6077c478bd9Sstevel@tonic-gate if (id != expected_id)
6087c478bd9Sstevel@tonic-gate fatal("ID mismatch (%u != %u)", id, expected_id);
6097c478bd9Sstevel@tonic-gate
6107c478bd9Sstevel@tonic-gate if (type == SSH2_FXP_STATUS) {
6117c478bd9Sstevel@tonic-gate u_int status = buffer_get_int(&msg);
6127c478bd9Sstevel@tonic-gate
6137c478bd9Sstevel@tonic-gate error("Couldn't canonicalise: %s", fx2txt(status));
6147c478bd9Sstevel@tonic-gate return(NULL);
6157c478bd9Sstevel@tonic-gate } else if (type != SSH2_FXP_NAME)
6167c478bd9Sstevel@tonic-gate fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
6177c478bd9Sstevel@tonic-gate SSH2_FXP_NAME, type);
6187c478bd9Sstevel@tonic-gate
6197c478bd9Sstevel@tonic-gate count = buffer_get_int(&msg);
6207c478bd9Sstevel@tonic-gate if (count != 1)
6217c478bd9Sstevel@tonic-gate fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
6227c478bd9Sstevel@tonic-gate
6237c478bd9Sstevel@tonic-gate filename = buffer_get_string(&msg, NULL);
6247c478bd9Sstevel@tonic-gate longname = buffer_get_string(&msg, NULL);
62590685d2cSjp161948 a = decode_attrib(&msg);
6267c478bd9Sstevel@tonic-gate
6277c478bd9Sstevel@tonic-gate debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
6287c478bd9Sstevel@tonic-gate
6297c478bd9Sstevel@tonic-gate xfree(longname);
6307c478bd9Sstevel@tonic-gate
6317c478bd9Sstevel@tonic-gate buffer_free(&msg);
6327c478bd9Sstevel@tonic-gate
6337c478bd9Sstevel@tonic-gate return(filename);
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate
6367c478bd9Sstevel@tonic-gate int
do_rename(struct sftp_conn * conn,char * oldpath,char * newpath)6377c478bd9Sstevel@tonic-gate do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
6387c478bd9Sstevel@tonic-gate {
6397c478bd9Sstevel@tonic-gate Buffer msg;
6407c478bd9Sstevel@tonic-gate u_int status, id;
6417c478bd9Sstevel@tonic-gate
6427c478bd9Sstevel@tonic-gate buffer_init(&msg);
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate /* Send rename request */
6457c478bd9Sstevel@tonic-gate id = conn->msg_id++;
6467c478bd9Sstevel@tonic-gate buffer_put_char(&msg, SSH2_FXP_RENAME);
6477c478bd9Sstevel@tonic-gate buffer_put_int(&msg, id);
6487c478bd9Sstevel@tonic-gate buffer_put_cstring(&msg, oldpath);
6497c478bd9Sstevel@tonic-gate buffer_put_cstring(&msg, newpath);
6507c478bd9Sstevel@tonic-gate send_msg(conn->fd_out, &msg);
6517c478bd9Sstevel@tonic-gate debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
6527c478bd9Sstevel@tonic-gate newpath);
6537c478bd9Sstevel@tonic-gate buffer_free(&msg);
6547c478bd9Sstevel@tonic-gate
6557c478bd9Sstevel@tonic-gate status = get_status(conn->fd_in, id);
6567c478bd9Sstevel@tonic-gate if (status != SSH2_FX_OK)
6577c478bd9Sstevel@tonic-gate error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
6587c478bd9Sstevel@tonic-gate newpath, fx2txt(status));
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate return(status);
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate
6637c478bd9Sstevel@tonic-gate int
do_symlink(struct sftp_conn * conn,char * oldpath,char * newpath)6647c478bd9Sstevel@tonic-gate do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate Buffer msg;
6677c478bd9Sstevel@tonic-gate u_int status, id;
6687c478bd9Sstevel@tonic-gate
6697c478bd9Sstevel@tonic-gate if (conn->version < 3) {
6707c478bd9Sstevel@tonic-gate error("This server does not support the symlink operation");
6717c478bd9Sstevel@tonic-gate return(SSH2_FX_OP_UNSUPPORTED);
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate
6747c478bd9Sstevel@tonic-gate buffer_init(&msg);
6757c478bd9Sstevel@tonic-gate
67690685d2cSjp161948 /* Send symlink request */
6777c478bd9Sstevel@tonic-gate id = conn->msg_id++;
6787c478bd9Sstevel@tonic-gate buffer_put_char(&msg, SSH2_FXP_SYMLINK);
6797c478bd9Sstevel@tonic-gate buffer_put_int(&msg, id);
6807c478bd9Sstevel@tonic-gate buffer_put_cstring(&msg, oldpath);
6817c478bd9Sstevel@tonic-gate buffer_put_cstring(&msg, newpath);
6827c478bd9Sstevel@tonic-gate send_msg(conn->fd_out, &msg);
6837c478bd9Sstevel@tonic-gate debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
6847c478bd9Sstevel@tonic-gate newpath);
6857c478bd9Sstevel@tonic-gate buffer_free(&msg);
6867c478bd9Sstevel@tonic-gate
6877c478bd9Sstevel@tonic-gate status = get_status(conn->fd_in, id);
6887c478bd9Sstevel@tonic-gate if (status != SSH2_FX_OK)
68990685d2cSjp161948 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
6907c478bd9Sstevel@tonic-gate newpath, fx2txt(status));
6917c478bd9Sstevel@tonic-gate
6927c478bd9Sstevel@tonic-gate return(status);
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate
69590685d2cSjp161948 /* this is never used so hush the lint */
6967c478bd9Sstevel@tonic-gate #if 0
6977c478bd9Sstevel@tonic-gate char *
6987c478bd9Sstevel@tonic-gate do_readlink(struct sftp_conn *conn, char *path)
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate Buffer msg;
7017c478bd9Sstevel@tonic-gate u_int type, expected_id, count, id;
7027c478bd9Sstevel@tonic-gate char *filename, *longname;
70390685d2cSjp161948 Attrib *a;
7047c478bd9Sstevel@tonic-gate
7057c478bd9Sstevel@tonic-gate expected_id = id = conn->msg_id++;
7067c478bd9Sstevel@tonic-gate send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
7077c478bd9Sstevel@tonic-gate strlen(path));
7087c478bd9Sstevel@tonic-gate
7097c478bd9Sstevel@tonic-gate buffer_init(&msg);
7107c478bd9Sstevel@tonic-gate
7117c478bd9Sstevel@tonic-gate get_msg(conn->fd_in, &msg);
7127c478bd9Sstevel@tonic-gate type = buffer_get_char(&msg);
7137c478bd9Sstevel@tonic-gate id = buffer_get_int(&msg);
7147c478bd9Sstevel@tonic-gate
7157c478bd9Sstevel@tonic-gate if (id != expected_id)
7167c478bd9Sstevel@tonic-gate fatal("ID mismatch (%u != %u)", id, expected_id);
7177c478bd9Sstevel@tonic-gate
7187c478bd9Sstevel@tonic-gate if (type == SSH2_FXP_STATUS) {
7197c478bd9Sstevel@tonic-gate u_int status = buffer_get_int(&msg);
7207c478bd9Sstevel@tonic-gate
7217c478bd9Sstevel@tonic-gate error("Couldn't readlink: %s", fx2txt(status));
7227c478bd9Sstevel@tonic-gate return(NULL);
7237c478bd9Sstevel@tonic-gate } else if (type != SSH2_FXP_NAME)
7247c478bd9Sstevel@tonic-gate fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
7257c478bd9Sstevel@tonic-gate SSH2_FXP_NAME, type);
7267c478bd9Sstevel@tonic-gate
7277c478bd9Sstevel@tonic-gate count = buffer_get_int(&msg);
7287c478bd9Sstevel@tonic-gate if (count != 1)
7297c478bd9Sstevel@tonic-gate fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
7307c478bd9Sstevel@tonic-gate
7317c478bd9Sstevel@tonic-gate filename = buffer_get_string(&msg, NULL);
7327c478bd9Sstevel@tonic-gate longname = buffer_get_string(&msg, NULL);
73390685d2cSjp161948 a = decode_attrib(&msg);
7347c478bd9Sstevel@tonic-gate
7357c478bd9Sstevel@tonic-gate debug3("SSH_FXP_READLINK %s -> %s", path, filename);
7367c478bd9Sstevel@tonic-gate
7377c478bd9Sstevel@tonic-gate xfree(longname);
7387c478bd9Sstevel@tonic-gate
7397c478bd9Sstevel@tonic-gate buffer_free(&msg);
7407c478bd9Sstevel@tonic-gate
7417c478bd9Sstevel@tonic-gate return(filename);
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate #endif
7447c478bd9Sstevel@tonic-gate
7457c478bd9Sstevel@tonic-gate static void
send_read_request(int fd_out,u_int id,u_int64_t offset,u_int len,char * handle,u_int handle_len)7467c478bd9Sstevel@tonic-gate send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
7477c478bd9Sstevel@tonic-gate char *handle, u_int handle_len)
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate Buffer msg;
7507c478bd9Sstevel@tonic-gate
7517c478bd9Sstevel@tonic-gate buffer_init(&msg);
7527c478bd9Sstevel@tonic-gate buffer_clear(&msg);
7537c478bd9Sstevel@tonic-gate buffer_put_char(&msg, SSH2_FXP_READ);
7547c478bd9Sstevel@tonic-gate buffer_put_int(&msg, id);
7557c478bd9Sstevel@tonic-gate buffer_put_string(&msg, handle, handle_len);
7567c478bd9Sstevel@tonic-gate buffer_put_int64(&msg, offset);
7577c478bd9Sstevel@tonic-gate buffer_put_int(&msg, len);
7587c478bd9Sstevel@tonic-gate send_msg(fd_out, &msg);
7597c478bd9Sstevel@tonic-gate buffer_free(&msg);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate
7627c478bd9Sstevel@tonic-gate int
do_download(struct sftp_conn * conn,char * remote_path,char * local_path,int pflag)7637c478bd9Sstevel@tonic-gate do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
7647c478bd9Sstevel@tonic-gate int pflag)
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate Attrib junk, *a;
7677c478bd9Sstevel@tonic-gate Buffer msg;
7687c478bd9Sstevel@tonic-gate char *handle;
76990685d2cSjp161948 int local_fd, status = 0, write_error;
7707c478bd9Sstevel@tonic-gate int read_error, write_errno;
7717c478bd9Sstevel@tonic-gate u_int64_t offset, size;
77290685d2cSjp161948 u_int handle_len, mode, type, id, buflen, num_req, max_req;
77390685d2cSjp161948 off_t progress_counter;
7747c478bd9Sstevel@tonic-gate struct request {
7757c478bd9Sstevel@tonic-gate u_int id;
7767c478bd9Sstevel@tonic-gate u_int len;
7777c478bd9Sstevel@tonic-gate u_int64_t offset;
7787c478bd9Sstevel@tonic-gate TAILQ_ENTRY(request) tq;
7797c478bd9Sstevel@tonic-gate };
7807c478bd9Sstevel@tonic-gate TAILQ_HEAD(reqhead, request) requests;
7817c478bd9Sstevel@tonic-gate struct request *req;
7827c478bd9Sstevel@tonic-gate
7837c478bd9Sstevel@tonic-gate TAILQ_INIT(&requests);
7847c478bd9Sstevel@tonic-gate
7857c478bd9Sstevel@tonic-gate a = do_stat(conn, remote_path, 0);
7867c478bd9Sstevel@tonic-gate if (a == NULL)
7877c478bd9Sstevel@tonic-gate return(-1);
7887c478bd9Sstevel@tonic-gate
789*9c68f910SVladimir Kotal /* Do not preserve set[ug]id here, as we do not preserve ownership */
7907c478bd9Sstevel@tonic-gate if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
79190685d2cSjp161948 mode = a->perm & 0777;
7927c478bd9Sstevel@tonic-gate else
7937c478bd9Sstevel@tonic-gate mode = 0666;
7947c478bd9Sstevel@tonic-gate
7957c478bd9Sstevel@tonic-gate if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
79690685d2cSjp161948 (!S_ISREG(a->perm))) {
79790685d2cSjp161948 error("Cannot download non-regular file: %s", remote_path);
7987c478bd9Sstevel@tonic-gate return(-1);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate
8017c478bd9Sstevel@tonic-gate if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
8027c478bd9Sstevel@tonic-gate size = a->size;
8037c478bd9Sstevel@tonic-gate else
8047c478bd9Sstevel@tonic-gate size = 0;
8057c478bd9Sstevel@tonic-gate
8067c478bd9Sstevel@tonic-gate buflen = conn->transfer_buflen;
8077c478bd9Sstevel@tonic-gate buffer_init(&msg);
8087c478bd9Sstevel@tonic-gate
8097c478bd9Sstevel@tonic-gate /* Send open request */
8107c478bd9Sstevel@tonic-gate id = conn->msg_id++;
8117c478bd9Sstevel@tonic-gate buffer_put_char(&msg, SSH2_FXP_OPEN);
8127c478bd9Sstevel@tonic-gate buffer_put_int(&msg, id);
8137c478bd9Sstevel@tonic-gate buffer_put_cstring(&msg, remote_path);
8147c478bd9Sstevel@tonic-gate buffer_put_int(&msg, SSH2_FXF_READ);
8157c478bd9Sstevel@tonic-gate attrib_clear(&junk); /* Send empty attributes */
8167c478bd9Sstevel@tonic-gate encode_attrib(&msg, &junk);
8177c478bd9Sstevel@tonic-gate send_msg(conn->fd_out, &msg);
8187c478bd9Sstevel@tonic-gate debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
8197c478bd9Sstevel@tonic-gate
8207c478bd9Sstevel@tonic-gate handle = get_handle(conn->fd_in, id, &handle_len);
8217c478bd9Sstevel@tonic-gate if (handle == NULL) {
8227c478bd9Sstevel@tonic-gate buffer_free(&msg);
8237c478bd9Sstevel@tonic-gate return(-1);
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate
82690685d2cSjp161948 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
82790685d2cSjp161948 mode | S_IWRITE);
8287c478bd9Sstevel@tonic-gate if (local_fd == -1) {
8297c478bd9Sstevel@tonic-gate error("Couldn't open local file \"%s\" for writing: %s",
8307c478bd9Sstevel@tonic-gate local_path, strerror(errno));
8317c478bd9Sstevel@tonic-gate buffer_free(&msg);
8327c478bd9Sstevel@tonic-gate xfree(handle);
8337c478bd9Sstevel@tonic-gate return(-1);
8347c478bd9Sstevel@tonic-gate }
8357c478bd9Sstevel@tonic-gate
8367c478bd9Sstevel@tonic-gate /* Read from remote and write to local */
8377c478bd9Sstevel@tonic-gate write_error = read_error = write_errno = num_req = offset = 0;
8387c478bd9Sstevel@tonic-gate max_req = 1;
83990685d2cSjp161948 progress_counter = 0;
84090685d2cSjp161948
84190685d2cSjp161948 if (showprogress && size != 0)
84290685d2cSjp161948 start_progress_meter(remote_path, size, &progress_counter);
84390685d2cSjp161948
8447c478bd9Sstevel@tonic-gate while (num_req > 0 || max_req > 0) {
8457c478bd9Sstevel@tonic-gate char *data;
8467c478bd9Sstevel@tonic-gate u_int len;
8477c478bd9Sstevel@tonic-gate
84890685d2cSjp161948 /*
84990685d2cSjp161948 * Simulate EOF on interrupt: stop sending new requests and
85090685d2cSjp161948 * allow outstanding requests to drain gracefully
85190685d2cSjp161948 */
85290685d2cSjp161948 if (interrupted) {
85390685d2cSjp161948 if (num_req == 0) /* If we haven't started yet... */
85490685d2cSjp161948 break;
85590685d2cSjp161948 max_req = 0;
85690685d2cSjp161948 }
85790685d2cSjp161948
8587c478bd9Sstevel@tonic-gate /* Send some more requests */
8597c478bd9Sstevel@tonic-gate while (num_req < max_req) {
8607c478bd9Sstevel@tonic-gate debug3("Request range %llu -> %llu (%d/%d)",
8617c478bd9Sstevel@tonic-gate (unsigned long long)offset,
8627c478bd9Sstevel@tonic-gate (unsigned long long)offset + buflen - 1,
8637c478bd9Sstevel@tonic-gate num_req, max_req);
8647c478bd9Sstevel@tonic-gate req = xmalloc(sizeof(*req));
8657c478bd9Sstevel@tonic-gate req->id = conn->msg_id++;
8667c478bd9Sstevel@tonic-gate req->len = buflen;
8677c478bd9Sstevel@tonic-gate req->offset = offset;
8687c478bd9Sstevel@tonic-gate offset += buflen;
8697c478bd9Sstevel@tonic-gate num_req++;
8707c478bd9Sstevel@tonic-gate TAILQ_INSERT_TAIL(&requests, req, tq);
8717c478bd9Sstevel@tonic-gate send_read_request(conn->fd_out, req->id, req->offset,
8727c478bd9Sstevel@tonic-gate req->len, handle, handle_len);
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate
8757c478bd9Sstevel@tonic-gate buffer_clear(&msg);
8767c478bd9Sstevel@tonic-gate get_msg(conn->fd_in, &msg);
8777c478bd9Sstevel@tonic-gate type = buffer_get_char(&msg);
8787c478bd9Sstevel@tonic-gate id = buffer_get_int(&msg);
8797c478bd9Sstevel@tonic-gate debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
8807c478bd9Sstevel@tonic-gate
8817c478bd9Sstevel@tonic-gate /* Find the request in our queue */
8827c478bd9Sstevel@tonic-gate for (req = TAILQ_FIRST(&requests);
8837c478bd9Sstevel@tonic-gate req != NULL && req->id != id;
8847c478bd9Sstevel@tonic-gate req = TAILQ_NEXT(req, tq))
8857c478bd9Sstevel@tonic-gate ;
8867c478bd9Sstevel@tonic-gate if (req == NULL)
8877c478bd9Sstevel@tonic-gate fatal("Unexpected reply %u", id);
8887c478bd9Sstevel@tonic-gate
8897c478bd9Sstevel@tonic-gate switch (type) {
8907c478bd9Sstevel@tonic-gate case SSH2_FXP_STATUS:
8917c478bd9Sstevel@tonic-gate status = buffer_get_int(&msg);
8927c478bd9Sstevel@tonic-gate if (status != SSH2_FX_EOF)
8937c478bd9Sstevel@tonic-gate read_error = 1;
8947c478bd9Sstevel@tonic-gate max_req = 0;
8957c478bd9Sstevel@tonic-gate TAILQ_REMOVE(&requests, req, tq);
8967c478bd9Sstevel@tonic-gate xfree(req);
8977c478bd9Sstevel@tonic-gate num_req--;
8987c478bd9Sstevel@tonic-gate break;
8997c478bd9Sstevel@tonic-gate case SSH2_FXP_DATA:
9007c478bd9Sstevel@tonic-gate data = buffer_get_string(&msg, &len);
9017c478bd9Sstevel@tonic-gate debug3("Received data %llu -> %llu",
9027c478bd9Sstevel@tonic-gate (unsigned long long)req->offset,
9037c478bd9Sstevel@tonic-gate (unsigned long long)req->offset + len - 1);
9047c478bd9Sstevel@tonic-gate if (len > req->len)
9057c478bd9Sstevel@tonic-gate fatal("Received more data than asked for "
9067c478bd9Sstevel@tonic-gate "%u > %u", len, req->len);
9077c478bd9Sstevel@tonic-gate if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
90890685d2cSjp161948 atomicio(vwrite, local_fd, data, len) != len) &&
9097c478bd9Sstevel@tonic-gate !write_error) {
9107c478bd9Sstevel@tonic-gate write_errno = errno;
9117c478bd9Sstevel@tonic-gate write_error = 1;
9127c478bd9Sstevel@tonic-gate max_req = 0;
9137c478bd9Sstevel@tonic-gate }
91490685d2cSjp161948 progress_counter += len;
9157c478bd9Sstevel@tonic-gate xfree(data);
9167c478bd9Sstevel@tonic-gate
9177c478bd9Sstevel@tonic-gate if (len == req->len) {
9187c478bd9Sstevel@tonic-gate TAILQ_REMOVE(&requests, req, tq);
9197c478bd9Sstevel@tonic-gate xfree(req);
9207c478bd9Sstevel@tonic-gate num_req--;
9217c478bd9Sstevel@tonic-gate } else {
9227c478bd9Sstevel@tonic-gate /* Resend the request for the missing data */
9237c478bd9Sstevel@tonic-gate debug3("Short data block, re-requesting "
9247c478bd9Sstevel@tonic-gate "%llu -> %llu (%2d)",
9257c478bd9Sstevel@tonic-gate (unsigned long long)req->offset + len,
9267c478bd9Sstevel@tonic-gate (unsigned long long)req->offset +
9277c478bd9Sstevel@tonic-gate req->len - 1, num_req);
9287c478bd9Sstevel@tonic-gate req->id = conn->msg_id++;
9297c478bd9Sstevel@tonic-gate req->len -= len;
9307c478bd9Sstevel@tonic-gate req->offset += len;
9317c478bd9Sstevel@tonic-gate send_read_request(conn->fd_out, req->id,
9327c478bd9Sstevel@tonic-gate req->offset, req->len, handle, handle_len);
9337c478bd9Sstevel@tonic-gate /* Reduce the request size */
9347c478bd9Sstevel@tonic-gate if (len < buflen)
9357c478bd9Sstevel@tonic-gate buflen = MAX(MIN_READ_SIZE, len);
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate if (max_req > 0) { /* max_req = 0 iff EOF received */
9387c478bd9Sstevel@tonic-gate if (size > 0 && offset > size) {
9397c478bd9Sstevel@tonic-gate /* Only one request at a time
9407c478bd9Sstevel@tonic-gate * after the expected EOF */
9417c478bd9Sstevel@tonic-gate debug3("Finish at %llu (%2d)",
9427c478bd9Sstevel@tonic-gate (unsigned long long)offset,
9437c478bd9Sstevel@tonic-gate num_req);
9447c478bd9Sstevel@tonic-gate max_req = 1;
94590685d2cSjp161948 } else if (max_req <= conn->num_requests) {
9467c478bd9Sstevel@tonic-gate ++max_req;
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate break;
9507c478bd9Sstevel@tonic-gate default:
9517c478bd9Sstevel@tonic-gate fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
9527c478bd9Sstevel@tonic-gate SSH2_FXP_DATA, type);
9537c478bd9Sstevel@tonic-gate }
9547c478bd9Sstevel@tonic-gate }
9557c478bd9Sstevel@tonic-gate
95690685d2cSjp161948 if (showprogress && size)
95790685d2cSjp161948 stop_progress_meter();
95890685d2cSjp161948
9597c478bd9Sstevel@tonic-gate /* Sanity check */
9607c478bd9Sstevel@tonic-gate if (TAILQ_FIRST(&requests) != NULL)
9617c478bd9Sstevel@tonic-gate fatal("Transfer complete, but requests still in queue");
9627c478bd9Sstevel@tonic-gate
9637c478bd9Sstevel@tonic-gate if (read_error) {
9647c478bd9Sstevel@tonic-gate error("Couldn't read from remote file \"%s\" : %s",
9657c478bd9Sstevel@tonic-gate remote_path, fx2txt(status));
9667c478bd9Sstevel@tonic-gate do_close(conn, handle, handle_len);
9677c478bd9Sstevel@tonic-gate } else if (write_error) {
9687c478bd9Sstevel@tonic-gate error("Couldn't write to \"%s\": %s", local_path,
9697c478bd9Sstevel@tonic-gate strerror(write_errno));
9707c478bd9Sstevel@tonic-gate status = -1;
9717c478bd9Sstevel@tonic-gate do_close(conn, handle, handle_len);
9727c478bd9Sstevel@tonic-gate } else {
9737c478bd9Sstevel@tonic-gate status = do_close(conn, handle, handle_len);
9747c478bd9Sstevel@tonic-gate
9757c478bd9Sstevel@tonic-gate /* Override umask and utimes if asked */
9767c478bd9Sstevel@tonic-gate #ifdef HAVE_FCHMOD
9777c478bd9Sstevel@tonic-gate if (pflag && fchmod(local_fd, mode) == -1)
9787c478bd9Sstevel@tonic-gate #else
9797c478bd9Sstevel@tonic-gate if (pflag && chmod(local_path, mode) == -1)
9807c478bd9Sstevel@tonic-gate #endif /* HAVE_FCHMOD */
9817c478bd9Sstevel@tonic-gate error("Couldn't set mode on \"%s\": %s", local_path,
9827c478bd9Sstevel@tonic-gate strerror(errno));
9837c478bd9Sstevel@tonic-gate if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
9847c478bd9Sstevel@tonic-gate struct timeval tv[2];
9857c478bd9Sstevel@tonic-gate tv[0].tv_sec = a->atime;
9867c478bd9Sstevel@tonic-gate tv[1].tv_sec = a->mtime;
9877c478bd9Sstevel@tonic-gate tv[0].tv_usec = tv[1].tv_usec = 0;
9887c478bd9Sstevel@tonic-gate if (utimes(local_path, tv) == -1)
9897c478bd9Sstevel@tonic-gate error("Can't set times on \"%s\": %s",
9907c478bd9Sstevel@tonic-gate local_path, strerror(errno));
9917c478bd9Sstevel@tonic-gate }
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate close(local_fd);
9947c478bd9Sstevel@tonic-gate buffer_free(&msg);
9957c478bd9Sstevel@tonic-gate xfree(handle);
9967c478bd9Sstevel@tonic-gate
9977c478bd9Sstevel@tonic-gate return(status);
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate
10007c478bd9Sstevel@tonic-gate int
do_upload(struct sftp_conn * conn,char * local_path,char * remote_path,int pflag)10017c478bd9Sstevel@tonic-gate do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
10027c478bd9Sstevel@tonic-gate int pflag)
10037c478bd9Sstevel@tonic-gate {
10047c478bd9Sstevel@tonic-gate int local_fd, status;
10057c478bd9Sstevel@tonic-gate u_int handle_len, id, type;
10067c478bd9Sstevel@tonic-gate u_int64_t offset;
10077c478bd9Sstevel@tonic-gate char *handle, *data;
10087c478bd9Sstevel@tonic-gate Buffer msg;
10097c478bd9Sstevel@tonic-gate struct stat sb;
10107c478bd9Sstevel@tonic-gate Attrib a;
10117c478bd9Sstevel@tonic-gate u_int32_t startid;
10127c478bd9Sstevel@tonic-gate u_int32_t ackid;
10137c478bd9Sstevel@tonic-gate struct outstanding_ack {
10147c478bd9Sstevel@tonic-gate u_int id;
10157c478bd9Sstevel@tonic-gate u_int len;
10167c478bd9Sstevel@tonic-gate u_int64_t offset;
10177c478bd9Sstevel@tonic-gate TAILQ_ENTRY(outstanding_ack) tq;
10187c478bd9Sstevel@tonic-gate };
10197c478bd9Sstevel@tonic-gate TAILQ_HEAD(ackhead, outstanding_ack) acks;
102090685d2cSjp161948 struct outstanding_ack *ack = NULL;
10217c478bd9Sstevel@tonic-gate
10227c478bd9Sstevel@tonic-gate TAILQ_INIT(&acks);
10237c478bd9Sstevel@tonic-gate
10247c478bd9Sstevel@tonic-gate if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
10257c478bd9Sstevel@tonic-gate error("Couldn't open local file \"%s\" for reading: %s",
10267c478bd9Sstevel@tonic-gate local_path, strerror(errno));
10277c478bd9Sstevel@tonic-gate return(-1);
10287c478bd9Sstevel@tonic-gate }
10297c478bd9Sstevel@tonic-gate if (fstat(local_fd, &sb) == -1) {
10307c478bd9Sstevel@tonic-gate error("Couldn't fstat local file \"%s\": %s",
10317c478bd9Sstevel@tonic-gate local_path, strerror(errno));
10327c478bd9Sstevel@tonic-gate close(local_fd);
10337c478bd9Sstevel@tonic-gate return(-1);
10347c478bd9Sstevel@tonic-gate }
103590685d2cSjp161948 if (!S_ISREG(sb.st_mode)) {
103690685d2cSjp161948 error("%s is not a regular file", local_path);
103790685d2cSjp161948 close(local_fd);
103890685d2cSjp161948 return(-1);
103990685d2cSjp161948 }
10407c478bd9Sstevel@tonic-gate stat_to_attrib(&sb, &a);
10417c478bd9Sstevel@tonic-gate
10427c478bd9Sstevel@tonic-gate a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
10437c478bd9Sstevel@tonic-gate a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
10447c478bd9Sstevel@tonic-gate a.perm &= 0777;
10457c478bd9Sstevel@tonic-gate if (!pflag)
10467c478bd9Sstevel@tonic-gate a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
10477c478bd9Sstevel@tonic-gate
10487c478bd9Sstevel@tonic-gate buffer_init(&msg);
10497c478bd9Sstevel@tonic-gate
10507c478bd9Sstevel@tonic-gate /* Send open request */
10517c478bd9Sstevel@tonic-gate id = conn->msg_id++;
10527c478bd9Sstevel@tonic-gate buffer_put_char(&msg, SSH2_FXP_OPEN);
10537c478bd9Sstevel@tonic-gate buffer_put_int(&msg, id);
10547c478bd9Sstevel@tonic-gate buffer_put_cstring(&msg, remote_path);
10557c478bd9Sstevel@tonic-gate buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
10567c478bd9Sstevel@tonic-gate encode_attrib(&msg, &a);
10577c478bd9Sstevel@tonic-gate send_msg(conn->fd_out, &msg);
10587c478bd9Sstevel@tonic-gate debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
10597c478bd9Sstevel@tonic-gate
10607c478bd9Sstevel@tonic-gate buffer_clear(&msg);
10617c478bd9Sstevel@tonic-gate
10627c478bd9Sstevel@tonic-gate handle = get_handle(conn->fd_in, id, &handle_len);
10637c478bd9Sstevel@tonic-gate if (handle == NULL) {
10647c478bd9Sstevel@tonic-gate close(local_fd);
10657c478bd9Sstevel@tonic-gate buffer_free(&msg);
10667c478bd9Sstevel@tonic-gate return(-1);
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate
10697c478bd9Sstevel@tonic-gate startid = ackid = id + 1;
10707c478bd9Sstevel@tonic-gate data = xmalloc(conn->transfer_buflen);
10717c478bd9Sstevel@tonic-gate
10727c478bd9Sstevel@tonic-gate /* Read from local and write to remote */
10737c478bd9Sstevel@tonic-gate offset = 0;
107490685d2cSjp161948 if (showprogress)
107590685d2cSjp161948 start_progress_meter(local_path, sb.st_size, (off_t *)&offset);
107690685d2cSjp161948
10777c478bd9Sstevel@tonic-gate for (;;) {
10787c478bd9Sstevel@tonic-gate int len;
10797c478bd9Sstevel@tonic-gate
10807c478bd9Sstevel@tonic-gate /*
108190685d2cSjp161948 * Can't use atomicio here because it returns 0 on EOF,
108290685d2cSjp161948 * thus losing the last block of the file.
108390685d2cSjp161948 * Simulate an EOF on interrupt, allowing ACKs from the
108490685d2cSjp161948 * server to drain.
10857c478bd9Sstevel@tonic-gate */
108690685d2cSjp161948 if (interrupted)
108790685d2cSjp161948 len = 0;
108890685d2cSjp161948 else do
10897c478bd9Sstevel@tonic-gate len = read(local_fd, data, conn->transfer_buflen);
10907c478bd9Sstevel@tonic-gate while ((len == -1) && (errno == EINTR || errno == EAGAIN));
10917c478bd9Sstevel@tonic-gate
10927c478bd9Sstevel@tonic-gate if (len == -1)
10937c478bd9Sstevel@tonic-gate fatal("Couldn't read from \"%s\": %s", local_path,
10947c478bd9Sstevel@tonic-gate strerror(errno));
10957c478bd9Sstevel@tonic-gate
10967c478bd9Sstevel@tonic-gate if (len != 0) {
10977c478bd9Sstevel@tonic-gate ack = xmalloc(sizeof(*ack));
10987c478bd9Sstevel@tonic-gate ack->id = ++id;
10997c478bd9Sstevel@tonic-gate ack->offset = offset;
11007c478bd9Sstevel@tonic-gate ack->len = len;
11017c478bd9Sstevel@tonic-gate TAILQ_INSERT_TAIL(&acks, ack, tq);
11027c478bd9Sstevel@tonic-gate
11037c478bd9Sstevel@tonic-gate buffer_clear(&msg);
11047c478bd9Sstevel@tonic-gate buffer_put_char(&msg, SSH2_FXP_WRITE);
11057c478bd9Sstevel@tonic-gate buffer_put_int(&msg, ack->id);
11067c478bd9Sstevel@tonic-gate buffer_put_string(&msg, handle, handle_len);
11077c478bd9Sstevel@tonic-gate buffer_put_int64(&msg, offset);
11087c478bd9Sstevel@tonic-gate buffer_put_string(&msg, data, len);
11097c478bd9Sstevel@tonic-gate send_msg(conn->fd_out, &msg);
11107c478bd9Sstevel@tonic-gate debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
11117c478bd9Sstevel@tonic-gate id, (unsigned long long)offset, len);
11127c478bd9Sstevel@tonic-gate } else if (TAILQ_FIRST(&acks) == NULL)
11137c478bd9Sstevel@tonic-gate break;
11147c478bd9Sstevel@tonic-gate
11157c478bd9Sstevel@tonic-gate if (ack == NULL)
11167c478bd9Sstevel@tonic-gate fatal("Unexpected ACK %u", id);
11177c478bd9Sstevel@tonic-gate
11187c478bd9Sstevel@tonic-gate if (id == startid || len == 0 ||
11197c478bd9Sstevel@tonic-gate id - ackid >= conn->num_requests) {
11207c478bd9Sstevel@tonic-gate u_int r_id;
11217c478bd9Sstevel@tonic-gate
11227c478bd9Sstevel@tonic-gate buffer_clear(&msg);
11237c478bd9Sstevel@tonic-gate get_msg(conn->fd_in, &msg);
11247c478bd9Sstevel@tonic-gate type = buffer_get_char(&msg);
11257c478bd9Sstevel@tonic-gate r_id = buffer_get_int(&msg);
11267c478bd9Sstevel@tonic-gate
11277c478bd9Sstevel@tonic-gate if (type != SSH2_FXP_STATUS)
11287c478bd9Sstevel@tonic-gate fatal("Expected SSH2_FXP_STATUS(%d) packet, "
11297c478bd9Sstevel@tonic-gate "got %d", SSH2_FXP_STATUS, type);
11307c478bd9Sstevel@tonic-gate
11317c478bd9Sstevel@tonic-gate status = buffer_get_int(&msg);
11327c478bd9Sstevel@tonic-gate debug3("SSH2_FXP_STATUS %d", status);
11337c478bd9Sstevel@tonic-gate
11347c478bd9Sstevel@tonic-gate /* Find the request in our queue */
11357c478bd9Sstevel@tonic-gate for (ack = TAILQ_FIRST(&acks);
11367c478bd9Sstevel@tonic-gate ack != NULL && ack->id != r_id;
11377c478bd9Sstevel@tonic-gate ack = TAILQ_NEXT(ack, tq))
11387c478bd9Sstevel@tonic-gate ;
11397c478bd9Sstevel@tonic-gate if (ack == NULL)
11407c478bd9Sstevel@tonic-gate fatal("Can't find request for ID %u", r_id);
11417c478bd9Sstevel@tonic-gate TAILQ_REMOVE(&acks, ack, tq);
11427c478bd9Sstevel@tonic-gate
11437c478bd9Sstevel@tonic-gate if (status != SSH2_FX_OK) {
11447c478bd9Sstevel@tonic-gate error("Couldn't write to remote file \"%s\": %s",
11457c478bd9Sstevel@tonic-gate remote_path, fx2txt(status));
114690685d2cSjp161948 if (showprogress)
114790685d2cSjp161948 stop_progress_meter();
11487c478bd9Sstevel@tonic-gate do_close(conn, handle, handle_len);
11497c478bd9Sstevel@tonic-gate close(local_fd);
115090685d2cSjp161948 xfree(data);
115190685d2cSjp161948 xfree(ack);
115290685d2cSjp161948 status = -1;
11537c478bd9Sstevel@tonic-gate goto done;
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate debug3("In write loop, ack for %u %u bytes at %llu",
11567c478bd9Sstevel@tonic-gate ack->id, ack->len, (unsigned long long)ack->offset);
11577c478bd9Sstevel@tonic-gate ++ackid;
11587c478bd9Sstevel@tonic-gate xfree(ack);
11597c478bd9Sstevel@tonic-gate }
11607c478bd9Sstevel@tonic-gate offset += len;
11617c478bd9Sstevel@tonic-gate }
116290685d2cSjp161948 if (showprogress)
116390685d2cSjp161948 stop_progress_meter();
11647c478bd9Sstevel@tonic-gate xfree(data);
11657c478bd9Sstevel@tonic-gate
11667c478bd9Sstevel@tonic-gate if (close(local_fd) == -1) {
11677c478bd9Sstevel@tonic-gate error("Couldn't close local file \"%s\": %s", local_path,
11687c478bd9Sstevel@tonic-gate strerror(errno));
11697c478bd9Sstevel@tonic-gate do_close(conn, handle, handle_len);
11707c478bd9Sstevel@tonic-gate status = -1;
11717c478bd9Sstevel@tonic-gate goto done;
11727c478bd9Sstevel@tonic-gate }
11737c478bd9Sstevel@tonic-gate
11747c478bd9Sstevel@tonic-gate /* Override umask and utimes if asked */
11757c478bd9Sstevel@tonic-gate if (pflag)
11767c478bd9Sstevel@tonic-gate do_fsetstat(conn, handle, handle_len, &a);
11777c478bd9Sstevel@tonic-gate
11787c478bd9Sstevel@tonic-gate status = do_close(conn, handle, handle_len);
11797c478bd9Sstevel@tonic-gate
11807c478bd9Sstevel@tonic-gate done:
11817c478bd9Sstevel@tonic-gate xfree(handle);
11827c478bd9Sstevel@tonic-gate buffer_free(&msg);
11837c478bd9Sstevel@tonic-gate return(status);
11847c478bd9Sstevel@tonic-gate }
1185