xref: /onnv-gate/usr/src/cmd/ssh/sftp/sftp-client.c (revision 8304:5f6b266d020d)
10Sstevel@tonic-gate /*
25087Sjp161948  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
35087Sjp161948  *
45087Sjp161948  * Permission to use, copy, modify, and distribute this software for any
55087Sjp161948  * purpose with or without fee is hereby granted, provided that the above
65087Sjp161948  * copyright notice and this permission notice appear in all copies.
70Sstevel@tonic-gate  *
85087Sjp161948  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
95087Sjp161948  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
105087Sjp161948  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
115087Sjp161948  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
125087Sjp161948  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
135087Sjp161948  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
145087Sjp161948  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
150Sstevel@tonic-gate  */
160Sstevel@tonic-gate 
175087Sjp161948 /* $OpenBSD: sftp-client.c,v 1.76 2007/01/22 11:32:50 djm Exp $ */
185087Sjp161948 
190Sstevel@tonic-gate /* XXX: memleaks */
200Sstevel@tonic-gate /* XXX: signed vs unsigned */
210Sstevel@tonic-gate /* XXX: remove all logging, only return status codes */
220Sstevel@tonic-gate /* XXX: copy between two remote sites */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include "includes.h"
250Sstevel@tonic-gate 
265087Sjp161948 #include <sys/types.h>
275087Sjp161948 #include <sys/param.h>
285087Sjp161948 #include "sys-queue.h"
295087Sjp161948 #ifdef HAVE_SYS_STAT_H
305087Sjp161948 # include <sys/stat.h>
315087Sjp161948 #endif
325087Sjp161948 #ifdef HAVE_SYS_TIME_H
335087Sjp161948 # include <sys/time.h>
345087Sjp161948 #endif
355087Sjp161948 #include <sys/uio.h>
360Sstevel@tonic-gate 
375087Sjp161948 #include <errno.h>
385087Sjp161948 #include <fcntl.h>
395087Sjp161948 #include <signal.h>
405087Sjp161948 #include <stdarg.h>
415087Sjp161948 #include <stdio.h>
425087Sjp161948 #include <string.h>
435087Sjp161948 #include <unistd.h>
440Sstevel@tonic-gate 
455087Sjp161948 #include "xmalloc.h"
460Sstevel@tonic-gate #include "buffer.h"
470Sstevel@tonic-gate #include "bufaux.h"
480Sstevel@tonic-gate #include "log.h"
490Sstevel@tonic-gate #include "atomicio.h"
505087Sjp161948 #include "progressmeter.h"
515087Sjp161948 #include "misc.h"
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include "sftp.h"
540Sstevel@tonic-gate #include "sftp-common.h"
550Sstevel@tonic-gate #include "sftp-client.h"
560Sstevel@tonic-gate 
575087Sjp161948 extern volatile sig_atomic_t interrupted;
585087Sjp161948 extern int showprogress;
595087Sjp161948 
605087Sjp161948 /* Minimum amount of data to read at a time */
610Sstevel@tonic-gate #define MIN_READ_SIZE	512
620Sstevel@tonic-gate 
630Sstevel@tonic-gate struct sftp_conn {
640Sstevel@tonic-gate 	int fd_in;
650Sstevel@tonic-gate 	int fd_out;
660Sstevel@tonic-gate 	u_int transfer_buflen;
670Sstevel@tonic-gate 	u_int num_requests;
680Sstevel@tonic-gate 	u_int version;
690Sstevel@tonic-gate 	u_int msg_id;
700Sstevel@tonic-gate };
710Sstevel@tonic-gate 
720Sstevel@tonic-gate static void
send_msg(int fd,Buffer * m)730Sstevel@tonic-gate send_msg(int fd, Buffer *m)
740Sstevel@tonic-gate {
755087Sjp161948 	char mlen[4];
765087Sjp161948 	struct iovec iov[2];
775087Sjp161948 
785087Sjp161948 	if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
795087Sjp161948 		fatal("Outbound message too long %u", buffer_len(m));
800Sstevel@tonic-gate 
815087Sjp161948 	/* Send length first */
825087Sjp161948 	put_u32(mlen, buffer_len(m));
835087Sjp161948 	iov[0].iov_base = mlen;
845087Sjp161948 	iov[0].iov_len = sizeof(mlen);
855087Sjp161948 	iov[1].iov_base = buffer_ptr(m);
865087Sjp161948 	iov[1].iov_len = buffer_len(m);
870Sstevel@tonic-gate 
885087Sjp161948 	if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
890Sstevel@tonic-gate 		fatal("Couldn't send packet: %s", strerror(errno));
900Sstevel@tonic-gate 
915087Sjp161948 	buffer_clear(m);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate static void
get_msg(int fd,Buffer * m)950Sstevel@tonic-gate get_msg(int fd, Buffer *m)
960Sstevel@tonic-gate {
975087Sjp161948 	u_int msg_len;
980Sstevel@tonic-gate 
995087Sjp161948 	buffer_append_space(m, 4);
1005087Sjp161948 	if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
1015087Sjp161948 		if (errno == EPIPE)
1025087Sjp161948 			fatal("Connection closed");
1035087Sjp161948 		else
1045087Sjp161948 			fatal("Couldn't read packet: %s", strerror(errno));
1055087Sjp161948 	}
1060Sstevel@tonic-gate 
1075087Sjp161948 	msg_len = buffer_get_int(m);
1085087Sjp161948 	if (msg_len > SFTP_MAX_MSG_LENGTH)
1090Sstevel@tonic-gate 		fatal("Received message too long %u", msg_len);
1100Sstevel@tonic-gate 
1115087Sjp161948 	buffer_append_space(m, msg_len);
1125087Sjp161948 	if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
1135087Sjp161948 		if (errno == EPIPE)
1140Sstevel@tonic-gate 			fatal("Connection closed");
1155087Sjp161948 		else
1165087Sjp161948 			fatal("Read packet: %s", strerror(errno));
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate static void
send_string_request(int fd,u_int id,u_int code,char * s,u_int len)1210Sstevel@tonic-gate send_string_request(int fd, u_int id, u_int code, char *s,
1220Sstevel@tonic-gate     u_int len)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	Buffer msg;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	buffer_init(&msg);
1270Sstevel@tonic-gate 	buffer_put_char(&msg, code);
1280Sstevel@tonic-gate 	buffer_put_int(&msg, id);
1290Sstevel@tonic-gate 	buffer_put_string(&msg, s, len);
1300Sstevel@tonic-gate 	send_msg(fd, &msg);
1310Sstevel@tonic-gate 	debug3("Sent message fd %d T:%u I:%u", fd, code, id);
1320Sstevel@tonic-gate 	buffer_free(&msg);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate static void
send_string_attrs_request(int fd,u_int id,u_int code,char * s,u_int len,Attrib * a)1360Sstevel@tonic-gate send_string_attrs_request(int fd, u_int id, u_int code, char *s,
1370Sstevel@tonic-gate     u_int len, Attrib *a)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	Buffer msg;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	buffer_init(&msg);
1420Sstevel@tonic-gate 	buffer_put_char(&msg, code);
1430Sstevel@tonic-gate 	buffer_put_int(&msg, id);
1440Sstevel@tonic-gate 	buffer_put_string(&msg, s, len);
1450Sstevel@tonic-gate 	encode_attrib(&msg, a);
1460Sstevel@tonic-gate 	send_msg(fd, &msg);
1470Sstevel@tonic-gate 	debug3("Sent message fd %d T:%u I:%u", fd, code, id);
1480Sstevel@tonic-gate 	buffer_free(&msg);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static u_int
get_status(int fd,u_int expected_id)1520Sstevel@tonic-gate get_status(int fd, u_int expected_id)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	Buffer msg;
1550Sstevel@tonic-gate 	u_int type, id, status;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	buffer_init(&msg);
1580Sstevel@tonic-gate 	get_msg(fd, &msg);
1590Sstevel@tonic-gate 	type = buffer_get_char(&msg);
1600Sstevel@tonic-gate 	id = buffer_get_int(&msg);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	if (id != expected_id)
1630Sstevel@tonic-gate 		fatal("ID mismatch (%u != %u)", id, expected_id);
1640Sstevel@tonic-gate 	if (type != SSH2_FXP_STATUS)
1650Sstevel@tonic-gate 		fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
1660Sstevel@tonic-gate 		    SSH2_FXP_STATUS, type);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	status = buffer_get_int(&msg);
1690Sstevel@tonic-gate 	buffer_free(&msg);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	debug3("SSH2_FXP_STATUS %u", status);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	return(status);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate static char *
get_handle(int fd,u_int expected_id,u_int * len)1770Sstevel@tonic-gate get_handle(int fd, u_int expected_id, u_int *len)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	Buffer msg;
1800Sstevel@tonic-gate 	u_int type, id;
1810Sstevel@tonic-gate 	char *handle;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	buffer_init(&msg);
1840Sstevel@tonic-gate 	get_msg(fd, &msg);
1850Sstevel@tonic-gate 	type = buffer_get_char(&msg);
1860Sstevel@tonic-gate 	id = buffer_get_int(&msg);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (id != expected_id)
1890Sstevel@tonic-gate 		fatal("ID mismatch (%u != %u)", id, expected_id);
1900Sstevel@tonic-gate 	if (type == SSH2_FXP_STATUS) {
1910Sstevel@tonic-gate 		int status = buffer_get_int(&msg);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 		error("Couldn't get handle: %s", fx2txt(status));
1945087Sjp161948 		buffer_free(&msg);
1950Sstevel@tonic-gate 		return(NULL);
1960Sstevel@tonic-gate 	} else if (type != SSH2_FXP_HANDLE)
1970Sstevel@tonic-gate 		fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
1980Sstevel@tonic-gate 		    SSH2_FXP_HANDLE, type);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	handle = buffer_get_string(&msg, len);
2010Sstevel@tonic-gate 	buffer_free(&msg);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	return(handle);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate static Attrib *
get_decode_stat(int fd,u_int expected_id,int quiet)2070Sstevel@tonic-gate get_decode_stat(int fd, u_int expected_id, int quiet)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	Buffer msg;
2100Sstevel@tonic-gate 	u_int type, id;
2110Sstevel@tonic-gate 	Attrib *a;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	buffer_init(&msg);
2140Sstevel@tonic-gate 	get_msg(fd, &msg);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	type = buffer_get_char(&msg);
2170Sstevel@tonic-gate 	id = buffer_get_int(&msg);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	debug3("Received stat reply T:%u I:%u", type, id);
2200Sstevel@tonic-gate 	if (id != expected_id)
2210Sstevel@tonic-gate 		fatal("ID mismatch (%u != %u)", id, expected_id);
2220Sstevel@tonic-gate 	if (type == SSH2_FXP_STATUS) {
2230Sstevel@tonic-gate 		int status = buffer_get_int(&msg);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 		if (quiet)
2260Sstevel@tonic-gate 			debug("Couldn't stat remote file: %s", fx2txt(status));
2270Sstevel@tonic-gate 		else
2280Sstevel@tonic-gate 			error("Couldn't stat remote file: %s", fx2txt(status));
2295087Sjp161948 		buffer_free(&msg);
2300Sstevel@tonic-gate 		return(NULL);
2310Sstevel@tonic-gate 	} else if (type != SSH2_FXP_ATTRS) {
2320Sstevel@tonic-gate 		fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
2330Sstevel@tonic-gate 		    SSH2_FXP_ATTRS, type);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 	a = decode_attrib(&msg);
2360Sstevel@tonic-gate 	buffer_free(&msg);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	return(a);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate struct sftp_conn *
do_init(int fd_in,int fd_out,u_int transfer_buflen,u_int num_requests)2420Sstevel@tonic-gate do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	u_int type;
2450Sstevel@tonic-gate 	int version;
2460Sstevel@tonic-gate 	Buffer msg;
2470Sstevel@tonic-gate 	struct sftp_conn *ret;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	buffer_init(&msg);
2500Sstevel@tonic-gate 	buffer_put_char(&msg, SSH2_FXP_INIT);
2510Sstevel@tonic-gate 	buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
2520Sstevel@tonic-gate 	send_msg(fd_out, &msg);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	buffer_clear(&msg);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	get_msg(fd_in, &msg);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	/* Expecting a VERSION reply */
2590Sstevel@tonic-gate 	if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
2600Sstevel@tonic-gate 		error("Invalid packet back from SSH2_FXP_INIT (type %u)",
2610Sstevel@tonic-gate 		    type);
2620Sstevel@tonic-gate 		buffer_free(&msg);
2630Sstevel@tonic-gate 		return(NULL);
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 	version = buffer_get_int(&msg);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	debug2("Remote version: %d", version);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	/* Check for extensions */
2700Sstevel@tonic-gate 	while (buffer_len(&msg) > 0) {
2710Sstevel@tonic-gate 		char *name = buffer_get_string(&msg, NULL);
2720Sstevel@tonic-gate 		char *value = buffer_get_string(&msg, NULL);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 		debug2("Init extension: \"%s\"", name);
2750Sstevel@tonic-gate 		xfree(name);
2760Sstevel@tonic-gate 		xfree(value);
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	buffer_free(&msg);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	ret = xmalloc(sizeof(*ret));
2820Sstevel@tonic-gate 	ret->fd_in = fd_in;
2830Sstevel@tonic-gate 	ret->fd_out = fd_out;
2840Sstevel@tonic-gate 	ret->transfer_buflen = transfer_buflen;
2850Sstevel@tonic-gate 	ret->num_requests = num_requests;
2860Sstevel@tonic-gate 	ret->version = version;
2870Sstevel@tonic-gate 	ret->msg_id = 1;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	/* Some filexfer v.0 servers don't support large packets */
2900Sstevel@tonic-gate 	if (version == 0)
2910Sstevel@tonic-gate 		ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	return(ret);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate u_int
sftp_proto_version(struct sftp_conn * conn)2970Sstevel@tonic-gate sftp_proto_version(struct sftp_conn *conn)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	return(conn->version);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate int
do_close(struct sftp_conn * conn,char * handle,u_int handle_len)3030Sstevel@tonic-gate do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 	u_int id, status;
3060Sstevel@tonic-gate 	Buffer msg;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	buffer_init(&msg);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	id = conn->msg_id++;
3110Sstevel@tonic-gate 	buffer_put_char(&msg, SSH2_FXP_CLOSE);
3120Sstevel@tonic-gate 	buffer_put_int(&msg, id);
3130Sstevel@tonic-gate 	buffer_put_string(&msg, handle, handle_len);
3140Sstevel@tonic-gate 	send_msg(conn->fd_out, &msg);
3150Sstevel@tonic-gate 	debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	status = get_status(conn->fd_in, id);
3180Sstevel@tonic-gate 	if (status != SSH2_FX_OK)
3190Sstevel@tonic-gate 		error("Couldn't close file: %s", fx2txt(status));
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	buffer_free(&msg);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	return(status);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate static int
do_lsreaddir(struct sftp_conn * conn,char * path,int printflag,SFTP_DIRENT *** dir)3280Sstevel@tonic-gate do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
3290Sstevel@tonic-gate     SFTP_DIRENT ***dir)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	Buffer msg;
3325087Sjp161948 	u_int count, type, id, handle_len, i, expected_id, ents = 0;
3330Sstevel@tonic-gate 	char *handle;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	id = conn->msg_id++;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	buffer_init(&msg);
3380Sstevel@tonic-gate 	buffer_put_char(&msg, SSH2_FXP_OPENDIR);
3390Sstevel@tonic-gate 	buffer_put_int(&msg, id);
3400Sstevel@tonic-gate 	buffer_put_cstring(&msg, path);
3410Sstevel@tonic-gate 	send_msg(conn->fd_out, &msg);
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	buffer_clear(&msg);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	handle = get_handle(conn->fd_in, id, &handle_len);
3460Sstevel@tonic-gate 	if (handle == NULL)
3470Sstevel@tonic-gate 		return(-1);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	if (dir) {
3500Sstevel@tonic-gate 		ents = 0;
3510Sstevel@tonic-gate 		*dir = xmalloc(sizeof(**dir));
3520Sstevel@tonic-gate 		(*dir)[0] = NULL;
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 
3555087Sjp161948 	for (; !interrupted;) {
3560Sstevel@tonic-gate 		id = expected_id = conn->msg_id++;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 		debug3("Sending SSH2_FXP_READDIR I:%u", id);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 		buffer_clear(&msg);
3610Sstevel@tonic-gate 		buffer_put_char(&msg, SSH2_FXP_READDIR);
3620Sstevel@tonic-gate 		buffer_put_int(&msg, id);
3630Sstevel@tonic-gate 		buffer_put_string(&msg, handle, handle_len);
3640Sstevel@tonic-gate 		send_msg(conn->fd_out, &msg);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 		buffer_clear(&msg);
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 		get_msg(conn->fd_in, &msg);
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 		type = buffer_get_char(&msg);
3710Sstevel@tonic-gate 		id = buffer_get_int(&msg);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 		debug3("Received reply T:%u I:%u", type, id);
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 		if (id != expected_id)
3760Sstevel@tonic-gate 			fatal("ID mismatch (%u != %u)", id, expected_id);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 		if (type == SSH2_FXP_STATUS) {
3790Sstevel@tonic-gate 			int status = buffer_get_int(&msg);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 			debug3("Received SSH2_FXP_STATUS %d", status);
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 			if (status == SSH2_FX_EOF) {
3840Sstevel@tonic-gate 				break;
3850Sstevel@tonic-gate 			} else {
3860Sstevel@tonic-gate 				error("Couldn't read directory: %s",
3870Sstevel@tonic-gate 				    fx2txt(status));
3880Sstevel@tonic-gate 				do_close(conn, handle, handle_len);
3895087Sjp161948 				xfree(handle);
3900Sstevel@tonic-gate 				return(status);
3910Sstevel@tonic-gate 			}
3920Sstevel@tonic-gate 		} else if (type != SSH2_FXP_NAME)
3930Sstevel@tonic-gate 			fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
3940Sstevel@tonic-gate 			    SSH2_FXP_NAME, type);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 		count = buffer_get_int(&msg);
3970Sstevel@tonic-gate 		if (count == 0)
3980Sstevel@tonic-gate 			break;
3990Sstevel@tonic-gate 		debug3("Received %d SSH2_FXP_NAME responses", count);
4000Sstevel@tonic-gate 		for (i = 0; i < count; i++) {
4010Sstevel@tonic-gate 			char *filename, *longname;
4020Sstevel@tonic-gate 			Attrib *a;
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 			filename = buffer_get_string(&msg, NULL);
4050Sstevel@tonic-gate 			longname = buffer_get_string(&msg, NULL);
4060Sstevel@tonic-gate 			a = decode_attrib(&msg);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 			if (printflag)
4090Sstevel@tonic-gate 				printf("%s\n", longname);
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 			if (dir) {
4125087Sjp161948 				*dir = xrealloc(*dir, (ents + 2) * sizeof(**dir));
4130Sstevel@tonic-gate 				(*dir)[ents] = xmalloc(sizeof(***dir));
4140Sstevel@tonic-gate 				(*dir)[ents]->filename = xstrdup(filename);
4150Sstevel@tonic-gate 				(*dir)[ents]->longname = xstrdup(longname);
4160Sstevel@tonic-gate 				memcpy(&(*dir)[ents]->a, a, sizeof(*a));
4170Sstevel@tonic-gate 				(*dir)[++ents] = NULL;
4180Sstevel@tonic-gate 			}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 			xfree(filename);
4210Sstevel@tonic-gate 			xfree(longname);
4220Sstevel@tonic-gate 		}
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	buffer_free(&msg);
4260Sstevel@tonic-gate 	do_close(conn, handle, handle_len);
4270Sstevel@tonic-gate 	xfree(handle);
4280Sstevel@tonic-gate 
4295087Sjp161948 	/* Don't return partial matches on interrupt */
4305087Sjp161948 	if (interrupted && dir != NULL && *dir != NULL) {
4315087Sjp161948 		free_sftp_dirents(*dir);
4325087Sjp161948 		*dir = xmalloc(sizeof(**dir));
4335087Sjp161948 		**dir = NULL;
4345087Sjp161948 	}
4355087Sjp161948 
4360Sstevel@tonic-gate 	return(0);
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate int
do_readdir(struct sftp_conn * conn,char * path,SFTP_DIRENT *** dir)4400Sstevel@tonic-gate do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
4410Sstevel@tonic-gate {
4420Sstevel@tonic-gate 	return(do_lsreaddir(conn, path, 0, dir));
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate 
free_sftp_dirents(SFTP_DIRENT ** s)4450Sstevel@tonic-gate void free_sftp_dirents(SFTP_DIRENT **s)
4460Sstevel@tonic-gate {
4470Sstevel@tonic-gate 	int i;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	for (i = 0; s[i]; i++) {
4500Sstevel@tonic-gate 		xfree(s[i]->filename);
4510Sstevel@tonic-gate 		xfree(s[i]->longname);
4520Sstevel@tonic-gate 		xfree(s[i]);
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 	xfree(s);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate int
do_rm(struct sftp_conn * conn,char * path)4580Sstevel@tonic-gate do_rm(struct sftp_conn *conn, char *path)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	u_int status, id;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	id = conn->msg_id++;
4650Sstevel@tonic-gate 	send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
4660Sstevel@tonic-gate 	    strlen(path));
4670Sstevel@tonic-gate 	status = get_status(conn->fd_in, id);
4680Sstevel@tonic-gate 	if (status != SSH2_FX_OK)
4690Sstevel@tonic-gate 		error("Couldn't delete file: %s", fx2txt(status));
4700Sstevel@tonic-gate 	return(status);
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate int
do_mkdir(struct sftp_conn * conn,char * path,Attrib * a)4740Sstevel@tonic-gate do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
4750Sstevel@tonic-gate {
4760Sstevel@tonic-gate 	u_int status, id;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	id = conn->msg_id++;
4790Sstevel@tonic-gate 	send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
4800Sstevel@tonic-gate 	    strlen(path), a);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	status = get_status(conn->fd_in, id);
4830Sstevel@tonic-gate 	if (status != SSH2_FX_OK)
4840Sstevel@tonic-gate 		error("Couldn't create directory: %s", fx2txt(status));
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	return(status);
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate int
do_rmdir(struct sftp_conn * conn,char * path)4900Sstevel@tonic-gate do_rmdir(struct sftp_conn *conn, char *path)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate 	u_int status, id;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	id = conn->msg_id++;
4950Sstevel@tonic-gate 	send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
4960Sstevel@tonic-gate 	    strlen(path));
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	status = get_status(conn->fd_in, id);
4990Sstevel@tonic-gate 	if (status != SSH2_FX_OK)
5000Sstevel@tonic-gate 		error("Couldn't remove directory: %s", fx2txt(status));
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	return(status);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate Attrib *
do_stat(struct sftp_conn * conn,char * path,int quiet)5060Sstevel@tonic-gate do_stat(struct sftp_conn *conn, char *path, int quiet)
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	u_int id;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	id = conn->msg_id++;
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	send_string_request(conn->fd_out, id,
5130Sstevel@tonic-gate 	    conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
5140Sstevel@tonic-gate 	    path, strlen(path));
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	return(get_decode_stat(conn->fd_in, id, quiet));
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate Attrib *
do_lstat(struct sftp_conn * conn,char * path,int quiet)5200Sstevel@tonic-gate do_lstat(struct sftp_conn *conn, char *path, int quiet)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate 	u_int id;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	if (conn->version == 0) {
5250Sstevel@tonic-gate 		if (quiet)
5260Sstevel@tonic-gate 			debug("Server version does not support lstat operation");
5270Sstevel@tonic-gate 		else
5280Sstevel@tonic-gate 			log("Server version does not support lstat operation");
5290Sstevel@tonic-gate 		return(do_stat(conn, path, quiet));
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	id = conn->msg_id++;
5330Sstevel@tonic-gate 	send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
5340Sstevel@tonic-gate 	    strlen(path));
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	return(get_decode_stat(conn->fd_in, id, quiet));
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate 
5395087Sjp161948 /* this is never used so hush the lint */
5400Sstevel@tonic-gate #if 0
5410Sstevel@tonic-gate Attrib *
5420Sstevel@tonic-gate do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
5430Sstevel@tonic-gate {
5440Sstevel@tonic-gate 	u_int id;
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	id = conn->msg_id++;
5470Sstevel@tonic-gate 	send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
5480Sstevel@tonic-gate 	    handle_len);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	return(get_decode_stat(conn->fd_in, id, quiet));
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate #endif
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate int
do_setstat(struct sftp_conn * conn,char * path,Attrib * a)5550Sstevel@tonic-gate do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
5560Sstevel@tonic-gate {
5570Sstevel@tonic-gate 	u_int status, id;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	id = conn->msg_id++;
5600Sstevel@tonic-gate 	send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
5610Sstevel@tonic-gate 	    strlen(path), a);
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	status = get_status(conn->fd_in, id);
5640Sstevel@tonic-gate 	if (status != SSH2_FX_OK)
5650Sstevel@tonic-gate 		error("Couldn't setstat on \"%s\": %s", path,
5660Sstevel@tonic-gate 		    fx2txt(status));
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	return(status);
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate int
do_fsetstat(struct sftp_conn * conn,char * handle,u_int handle_len,Attrib * a)5720Sstevel@tonic-gate do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
5730Sstevel@tonic-gate     Attrib *a)
5740Sstevel@tonic-gate {
5750Sstevel@tonic-gate 	u_int status, id;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	id = conn->msg_id++;
5780Sstevel@tonic-gate 	send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
5790Sstevel@tonic-gate 	    handle_len, a);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	status = get_status(conn->fd_in, id);
5820Sstevel@tonic-gate 	if (status != SSH2_FX_OK)
5830Sstevel@tonic-gate 		error("Couldn't fsetstat: %s", fx2txt(status));
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	return(status);
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate char *
do_realpath(struct sftp_conn * conn,char * path)5890Sstevel@tonic-gate do_realpath(struct sftp_conn *conn, char *path)
5900Sstevel@tonic-gate {
5910Sstevel@tonic-gate 	Buffer msg;
5920Sstevel@tonic-gate 	u_int type, expected_id, count, id;
5930Sstevel@tonic-gate 	char *filename, *longname;
5945087Sjp161948 	/* LINTED */
5955087Sjp161948 	Attrib *a;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	expected_id = id = conn->msg_id++;
5980Sstevel@tonic-gate 	send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
5990Sstevel@tonic-gate 	    strlen(path));
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	buffer_init(&msg);
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	get_msg(conn->fd_in, &msg);
6040Sstevel@tonic-gate 	type = buffer_get_char(&msg);
6050Sstevel@tonic-gate 	id = buffer_get_int(&msg);
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	if (id != expected_id)
6080Sstevel@tonic-gate 		fatal("ID mismatch (%u != %u)", id, expected_id);
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	if (type == SSH2_FXP_STATUS) {
6110Sstevel@tonic-gate 		u_int status = buffer_get_int(&msg);
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 		error("Couldn't canonicalise: %s", fx2txt(status));
6140Sstevel@tonic-gate 		return(NULL);
6150Sstevel@tonic-gate 	} else if (type != SSH2_FXP_NAME)
6160Sstevel@tonic-gate 		fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
6170Sstevel@tonic-gate 		    SSH2_FXP_NAME, type);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	count = buffer_get_int(&msg);
6200Sstevel@tonic-gate 	if (count != 1)
6210Sstevel@tonic-gate 		fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	filename = buffer_get_string(&msg, NULL);
6240Sstevel@tonic-gate 	longname = buffer_get_string(&msg, NULL);
6255087Sjp161948 	a = decode_attrib(&msg);
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	xfree(longname);
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	buffer_free(&msg);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	return(filename);
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate int
do_rename(struct sftp_conn * conn,char * oldpath,char * newpath)6370Sstevel@tonic-gate do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
6380Sstevel@tonic-gate {
6390Sstevel@tonic-gate 	Buffer msg;
6400Sstevel@tonic-gate 	u_int status, id;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	buffer_init(&msg);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	/* Send rename request */
6450Sstevel@tonic-gate 	id = conn->msg_id++;
6460Sstevel@tonic-gate 	buffer_put_char(&msg, SSH2_FXP_RENAME);
6470Sstevel@tonic-gate 	buffer_put_int(&msg, id);
6480Sstevel@tonic-gate 	buffer_put_cstring(&msg, oldpath);
6490Sstevel@tonic-gate 	buffer_put_cstring(&msg, newpath);
6500Sstevel@tonic-gate 	send_msg(conn->fd_out, &msg);
6510Sstevel@tonic-gate 	debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
6520Sstevel@tonic-gate 	    newpath);
6530Sstevel@tonic-gate 	buffer_free(&msg);
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	status = get_status(conn->fd_in, id);
6560Sstevel@tonic-gate 	if (status != SSH2_FX_OK)
6570Sstevel@tonic-gate 		error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
6580Sstevel@tonic-gate 		    newpath, fx2txt(status));
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	return(status);
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate int
do_symlink(struct sftp_conn * conn,char * oldpath,char * newpath)6640Sstevel@tonic-gate do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate 	Buffer msg;
6670Sstevel@tonic-gate 	u_int status, id;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	if (conn->version < 3) {
6700Sstevel@tonic-gate 		error("This server does not support the symlink operation");
6710Sstevel@tonic-gate 		return(SSH2_FX_OP_UNSUPPORTED);
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	buffer_init(&msg);
6750Sstevel@tonic-gate 
6765087Sjp161948 	/* Send symlink request */
6770Sstevel@tonic-gate 	id = conn->msg_id++;
6780Sstevel@tonic-gate 	buffer_put_char(&msg, SSH2_FXP_SYMLINK);
6790Sstevel@tonic-gate 	buffer_put_int(&msg, id);
6800Sstevel@tonic-gate 	buffer_put_cstring(&msg, oldpath);
6810Sstevel@tonic-gate 	buffer_put_cstring(&msg, newpath);
6820Sstevel@tonic-gate 	send_msg(conn->fd_out, &msg);
6830Sstevel@tonic-gate 	debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
6840Sstevel@tonic-gate 	    newpath);
6850Sstevel@tonic-gate 	buffer_free(&msg);
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	status = get_status(conn->fd_in, id);
6880Sstevel@tonic-gate 	if (status != SSH2_FX_OK)
6895087Sjp161948 		error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
6900Sstevel@tonic-gate 		    newpath, fx2txt(status));
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	return(status);
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate 
6955087Sjp161948 /* this is never used so hush the lint */
6960Sstevel@tonic-gate #if 0
6970Sstevel@tonic-gate char *
6980Sstevel@tonic-gate do_readlink(struct sftp_conn *conn, char *path)
6990Sstevel@tonic-gate {
7000Sstevel@tonic-gate 	Buffer msg;
7010Sstevel@tonic-gate 	u_int type, expected_id, count, id;
7020Sstevel@tonic-gate 	char *filename, *longname;
7035087Sjp161948 	Attrib *a;
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	expected_id = id = conn->msg_id++;
7060Sstevel@tonic-gate 	send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
7070Sstevel@tonic-gate 	    strlen(path));
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	buffer_init(&msg);
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	get_msg(conn->fd_in, &msg);
7120Sstevel@tonic-gate 	type = buffer_get_char(&msg);
7130Sstevel@tonic-gate 	id = buffer_get_int(&msg);
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	if (id != expected_id)
7160Sstevel@tonic-gate 		fatal("ID mismatch (%u != %u)", id, expected_id);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	if (type == SSH2_FXP_STATUS) {
7190Sstevel@tonic-gate 		u_int status = buffer_get_int(&msg);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		error("Couldn't readlink: %s", fx2txt(status));
7220Sstevel@tonic-gate 		return(NULL);
7230Sstevel@tonic-gate 	} else if (type != SSH2_FXP_NAME)
7240Sstevel@tonic-gate 		fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
7250Sstevel@tonic-gate 		    SSH2_FXP_NAME, type);
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	count = buffer_get_int(&msg);
7280Sstevel@tonic-gate 	if (count != 1)
7290Sstevel@tonic-gate 		fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	filename = buffer_get_string(&msg, NULL);
7320Sstevel@tonic-gate 	longname = buffer_get_string(&msg, NULL);
7335087Sjp161948 	a = decode_attrib(&msg);
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	debug3("SSH_FXP_READLINK %s -> %s", path, filename);
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	xfree(longname);
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	buffer_free(&msg);
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	return(filename);
7420Sstevel@tonic-gate }
7430Sstevel@tonic-gate #endif
7440Sstevel@tonic-gate 
7450Sstevel@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)7460Sstevel@tonic-gate send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
7470Sstevel@tonic-gate     char *handle, u_int handle_len)
7480Sstevel@tonic-gate {
7490Sstevel@tonic-gate 	Buffer msg;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	buffer_init(&msg);
7520Sstevel@tonic-gate 	buffer_clear(&msg);
7530Sstevel@tonic-gate 	buffer_put_char(&msg, SSH2_FXP_READ);
7540Sstevel@tonic-gate 	buffer_put_int(&msg, id);
7550Sstevel@tonic-gate 	buffer_put_string(&msg, handle, handle_len);
7560Sstevel@tonic-gate 	buffer_put_int64(&msg, offset);
7570Sstevel@tonic-gate 	buffer_put_int(&msg, len);
7580Sstevel@tonic-gate 	send_msg(fd_out, &msg);
7590Sstevel@tonic-gate 	buffer_free(&msg);
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate int
do_download(struct sftp_conn * conn,char * remote_path,char * local_path,int pflag)7630Sstevel@tonic-gate do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
7640Sstevel@tonic-gate     int pflag)
7650Sstevel@tonic-gate {
7660Sstevel@tonic-gate 	Attrib junk, *a;
7670Sstevel@tonic-gate 	Buffer msg;
7680Sstevel@tonic-gate 	char *handle;
7695087Sjp161948 	int local_fd, status = 0, write_error;
7700Sstevel@tonic-gate 	int read_error, write_errno;
7710Sstevel@tonic-gate 	u_int64_t offset, size;
7725087Sjp161948 	u_int handle_len, mode, type, id, buflen, num_req, max_req;
7735087Sjp161948 	off_t progress_counter;
7740Sstevel@tonic-gate 	struct request {
7750Sstevel@tonic-gate 		u_int id;
7760Sstevel@tonic-gate 		u_int len;
7770Sstevel@tonic-gate 		u_int64_t offset;
7780Sstevel@tonic-gate 		TAILQ_ENTRY(request) tq;
7790Sstevel@tonic-gate 	};
7800Sstevel@tonic-gate 	TAILQ_HEAD(reqhead, request) requests;
7810Sstevel@tonic-gate 	struct request *req;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	TAILQ_INIT(&requests);
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	a = do_stat(conn, remote_path, 0);
7860Sstevel@tonic-gate 	if (a == NULL)
7870Sstevel@tonic-gate 		return(-1);
7880Sstevel@tonic-gate 
789*8304SVladimir.Kotal@Sun.COM 	/* Do not preserve set[ug]id here, as we do not preserve ownership */
7900Sstevel@tonic-gate 	if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
7915087Sjp161948 		mode = a->perm & 0777;
7920Sstevel@tonic-gate 	else
7930Sstevel@tonic-gate 		mode = 0666;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
7965087Sjp161948 	    (!S_ISREG(a->perm))) {
7975087Sjp161948 		error("Cannot download non-regular file: %s", remote_path);
7980Sstevel@tonic-gate 		return(-1);
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
8020Sstevel@tonic-gate 		size = a->size;
8030Sstevel@tonic-gate 	else
8040Sstevel@tonic-gate 		size = 0;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	buflen = conn->transfer_buflen;
8070Sstevel@tonic-gate 	buffer_init(&msg);
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	/* Send open request */
8100Sstevel@tonic-gate 	id = conn->msg_id++;
8110Sstevel@tonic-gate 	buffer_put_char(&msg, SSH2_FXP_OPEN);
8120Sstevel@tonic-gate 	buffer_put_int(&msg, id);
8130Sstevel@tonic-gate 	buffer_put_cstring(&msg, remote_path);
8140Sstevel@tonic-gate 	buffer_put_int(&msg, SSH2_FXF_READ);
8150Sstevel@tonic-gate 	attrib_clear(&junk); /* Send empty attributes */
8160Sstevel@tonic-gate 	encode_attrib(&msg, &junk);
8170Sstevel@tonic-gate 	send_msg(conn->fd_out, &msg);
8180Sstevel@tonic-gate 	debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	handle = get_handle(conn->fd_in, id, &handle_len);
8210Sstevel@tonic-gate 	if (handle == NULL) {
8220Sstevel@tonic-gate 		buffer_free(&msg);
8230Sstevel@tonic-gate 		return(-1);
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 
8265087Sjp161948 	local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
8275087Sjp161948 	    mode | S_IWRITE);
8280Sstevel@tonic-gate 	if (local_fd == -1) {
8290Sstevel@tonic-gate 		error("Couldn't open local file \"%s\" for writing: %s",
8300Sstevel@tonic-gate 		    local_path, strerror(errno));
8310Sstevel@tonic-gate 		buffer_free(&msg);
8320Sstevel@tonic-gate 		xfree(handle);
8330Sstevel@tonic-gate 		return(-1);
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	/* Read from remote and write to local */
8370Sstevel@tonic-gate 	write_error = read_error = write_errno = num_req = offset = 0;
8380Sstevel@tonic-gate 	max_req = 1;
8395087Sjp161948 	progress_counter = 0;
8405087Sjp161948 
8415087Sjp161948 	if (showprogress && size != 0)
8425087Sjp161948 		start_progress_meter(remote_path, size, &progress_counter);
8435087Sjp161948 
8440Sstevel@tonic-gate 	while (num_req > 0 || max_req > 0) {
8450Sstevel@tonic-gate 		char *data;
8460Sstevel@tonic-gate 		u_int len;
8470Sstevel@tonic-gate 
8485087Sjp161948 		/*
8495087Sjp161948 		 * Simulate EOF on interrupt: stop sending new requests and
8505087Sjp161948 		 * allow outstanding requests to drain gracefully
8515087Sjp161948 		 */
8525087Sjp161948 		if (interrupted) {
8535087Sjp161948 			if (num_req == 0) /* If we haven't started yet... */
8545087Sjp161948 				break;
8555087Sjp161948 			max_req = 0;
8565087Sjp161948 		}
8575087Sjp161948 
8580Sstevel@tonic-gate 		/* Send some more requests */
8590Sstevel@tonic-gate 		while (num_req < max_req) {
8600Sstevel@tonic-gate 			debug3("Request range %llu -> %llu (%d/%d)",
8610Sstevel@tonic-gate 			    (unsigned long long)offset,
8620Sstevel@tonic-gate 			    (unsigned long long)offset + buflen - 1,
8630Sstevel@tonic-gate 			    num_req, max_req);
8640Sstevel@tonic-gate 			req = xmalloc(sizeof(*req));
8650Sstevel@tonic-gate 			req->id = conn->msg_id++;
8660Sstevel@tonic-gate 			req->len = buflen;
8670Sstevel@tonic-gate 			req->offset = offset;
8680Sstevel@tonic-gate 			offset += buflen;
8690Sstevel@tonic-gate 			num_req++;
8700Sstevel@tonic-gate 			TAILQ_INSERT_TAIL(&requests, req, tq);
8710Sstevel@tonic-gate 			send_read_request(conn->fd_out, req->id, req->offset,
8720Sstevel@tonic-gate 			    req->len, handle, handle_len);
8730Sstevel@tonic-gate 		}
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 		buffer_clear(&msg);
8760Sstevel@tonic-gate 		get_msg(conn->fd_in, &msg);
8770Sstevel@tonic-gate 		type = buffer_get_char(&msg);
8780Sstevel@tonic-gate 		id = buffer_get_int(&msg);
8790Sstevel@tonic-gate 		debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 		/* Find the request in our queue */
8825087Sjp161948 		for (req = TAILQ_FIRST(&requests);
8830Sstevel@tonic-gate 		    req != NULL && req->id != id;
8840Sstevel@tonic-gate 		    req = TAILQ_NEXT(req, tq))
8850Sstevel@tonic-gate 			;
8860Sstevel@tonic-gate 		if (req == NULL)
8870Sstevel@tonic-gate 			fatal("Unexpected reply %u", id);
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 		switch (type) {
8900Sstevel@tonic-gate 		case SSH2_FXP_STATUS:
8910Sstevel@tonic-gate 			status = buffer_get_int(&msg);
8920Sstevel@tonic-gate 			if (status != SSH2_FX_EOF)
8930Sstevel@tonic-gate 				read_error = 1;
8940Sstevel@tonic-gate 			max_req = 0;
8950Sstevel@tonic-gate 			TAILQ_REMOVE(&requests, req, tq);
8960Sstevel@tonic-gate 			xfree(req);
8970Sstevel@tonic-gate 			num_req--;
8980Sstevel@tonic-gate 			break;
8990Sstevel@tonic-gate 		case SSH2_FXP_DATA:
9000Sstevel@tonic-gate 			data = buffer_get_string(&msg, &len);
9010Sstevel@tonic-gate 			debug3("Received data %llu -> %llu",
9020Sstevel@tonic-gate 			    (unsigned long long)req->offset,
9030Sstevel@tonic-gate 			    (unsigned long long)req->offset + len - 1);
9040Sstevel@tonic-gate 			if (len > req->len)
9050Sstevel@tonic-gate 				fatal("Received more data than asked for "
9065087Sjp161948 				    "%u > %u", len, req->len);
9070Sstevel@tonic-gate 			if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
9085087Sjp161948 			    atomicio(vwrite, local_fd, data, len) != len) &&
9090Sstevel@tonic-gate 			    !write_error) {
9100Sstevel@tonic-gate 				write_errno = errno;
9110Sstevel@tonic-gate 				write_error = 1;
9120Sstevel@tonic-gate 				max_req = 0;
9130Sstevel@tonic-gate 			}
9145087Sjp161948 			progress_counter += len;
9150Sstevel@tonic-gate 			xfree(data);
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 			if (len == req->len) {
9180Sstevel@tonic-gate 				TAILQ_REMOVE(&requests, req, tq);
9190Sstevel@tonic-gate 				xfree(req);
9200Sstevel@tonic-gate 				num_req--;
9210Sstevel@tonic-gate 			} else {
9220Sstevel@tonic-gate 				/* Resend the request for the missing data */
9230Sstevel@tonic-gate 				debug3("Short data block, re-requesting "
9240Sstevel@tonic-gate 				    "%llu -> %llu (%2d)",
9250Sstevel@tonic-gate 				    (unsigned long long)req->offset + len,
9260Sstevel@tonic-gate 				    (unsigned long long)req->offset +
9270Sstevel@tonic-gate 				    req->len - 1, num_req);
9280Sstevel@tonic-gate 				req->id = conn->msg_id++;
9290Sstevel@tonic-gate 				req->len -= len;
9300Sstevel@tonic-gate 				req->offset += len;
9310Sstevel@tonic-gate 				send_read_request(conn->fd_out, req->id,
9320Sstevel@tonic-gate 				    req->offset, req->len, handle, handle_len);
9330Sstevel@tonic-gate 				/* Reduce the request size */
9340Sstevel@tonic-gate 				if (len < buflen)
9350Sstevel@tonic-gate 					buflen = MAX(MIN_READ_SIZE, len);
9360Sstevel@tonic-gate 			}
9370Sstevel@tonic-gate 			if (max_req > 0) { /* max_req = 0 iff EOF received */
9380Sstevel@tonic-gate 				if (size > 0 && offset > size) {
9390Sstevel@tonic-gate 					/* Only one request at a time
9400Sstevel@tonic-gate 					 * after the expected EOF */
9410Sstevel@tonic-gate 					debug3("Finish at %llu (%2d)",
9420Sstevel@tonic-gate 					    (unsigned long long)offset,
9430Sstevel@tonic-gate 					    num_req);
9440Sstevel@tonic-gate 					max_req = 1;
9455087Sjp161948 				} else if (max_req <= conn->num_requests) {
9460Sstevel@tonic-gate 					++max_req;
9470Sstevel@tonic-gate 				}
9480Sstevel@tonic-gate 			}
9490Sstevel@tonic-gate 			break;
9500Sstevel@tonic-gate 		default:
9510Sstevel@tonic-gate 			fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
9520Sstevel@tonic-gate 			    SSH2_FXP_DATA, type);
9530Sstevel@tonic-gate 		}
9540Sstevel@tonic-gate 	}
9550Sstevel@tonic-gate 
9565087Sjp161948 	if (showprogress && size)
9575087Sjp161948 		stop_progress_meter();
9585087Sjp161948 
9590Sstevel@tonic-gate 	/* Sanity check */
9600Sstevel@tonic-gate 	if (TAILQ_FIRST(&requests) != NULL)
9610Sstevel@tonic-gate 		fatal("Transfer complete, but requests still in queue");
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	if (read_error) {
9640Sstevel@tonic-gate 		error("Couldn't read from remote file \"%s\" : %s",
9650Sstevel@tonic-gate 		    remote_path, fx2txt(status));
9660Sstevel@tonic-gate 		do_close(conn, handle, handle_len);
9670Sstevel@tonic-gate 	} else if (write_error) {
9680Sstevel@tonic-gate 		error("Couldn't write to \"%s\": %s", local_path,
9690Sstevel@tonic-gate 		    strerror(write_errno));
9700Sstevel@tonic-gate 		status = -1;
9710Sstevel@tonic-gate 		do_close(conn, handle, handle_len);
9720Sstevel@tonic-gate 	} else {
9730Sstevel@tonic-gate 		status = do_close(conn, handle, handle_len);
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 		/* Override umask and utimes if asked */
9760Sstevel@tonic-gate #ifdef HAVE_FCHMOD
9770Sstevel@tonic-gate 		if (pflag && fchmod(local_fd, mode) == -1)
9785087Sjp161948 #else
9790Sstevel@tonic-gate 		if (pflag && chmod(local_path, mode) == -1)
9800Sstevel@tonic-gate #endif /* HAVE_FCHMOD */
9810Sstevel@tonic-gate 			error("Couldn't set mode on \"%s\": %s", local_path,
9825087Sjp161948 			    strerror(errno));
9830Sstevel@tonic-gate 		if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
9840Sstevel@tonic-gate 			struct timeval tv[2];
9850Sstevel@tonic-gate 			tv[0].tv_sec = a->atime;
9860Sstevel@tonic-gate 			tv[1].tv_sec = a->mtime;
9870Sstevel@tonic-gate 			tv[0].tv_usec = tv[1].tv_usec = 0;
9880Sstevel@tonic-gate 			if (utimes(local_path, tv) == -1)
9890Sstevel@tonic-gate 				error("Can't set times on \"%s\": %s",
9905087Sjp161948 				    local_path, strerror(errno));
9910Sstevel@tonic-gate 		}
9920Sstevel@tonic-gate 	}
9930Sstevel@tonic-gate 	close(local_fd);
9940Sstevel@tonic-gate 	buffer_free(&msg);
9950Sstevel@tonic-gate 	xfree(handle);
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	return(status);
9980Sstevel@tonic-gate }
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate int
do_upload(struct sftp_conn * conn,char * local_path,char * remote_path,int pflag)10010Sstevel@tonic-gate do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
10020Sstevel@tonic-gate     int pflag)
10030Sstevel@tonic-gate {
10040Sstevel@tonic-gate 	int local_fd, status;
10050Sstevel@tonic-gate 	u_int handle_len, id, type;
10060Sstevel@tonic-gate 	u_int64_t offset;
10070Sstevel@tonic-gate 	char *handle, *data;
10080Sstevel@tonic-gate 	Buffer msg;
10090Sstevel@tonic-gate 	struct stat sb;
10100Sstevel@tonic-gate 	Attrib a;
10110Sstevel@tonic-gate 	u_int32_t startid;
10120Sstevel@tonic-gate 	u_int32_t ackid;
10130Sstevel@tonic-gate 	struct outstanding_ack {
10140Sstevel@tonic-gate 		u_int id;
10150Sstevel@tonic-gate 		u_int len;
10160Sstevel@tonic-gate 		u_int64_t offset;
10170Sstevel@tonic-gate 		TAILQ_ENTRY(outstanding_ack) tq;
10180Sstevel@tonic-gate 	};
10190Sstevel@tonic-gate 	TAILQ_HEAD(ackhead, outstanding_ack) acks;
10205087Sjp161948 	struct outstanding_ack *ack = NULL;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	TAILQ_INIT(&acks);
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
10250Sstevel@tonic-gate 		error("Couldn't open local file \"%s\" for reading: %s",
10260Sstevel@tonic-gate 		    local_path, strerror(errno));
10270Sstevel@tonic-gate 		return(-1);
10280Sstevel@tonic-gate 	}
10290Sstevel@tonic-gate 	if (fstat(local_fd, &sb) == -1) {
10300Sstevel@tonic-gate 		error("Couldn't fstat local file \"%s\": %s",
10310Sstevel@tonic-gate 		    local_path, strerror(errno));
10320Sstevel@tonic-gate 		close(local_fd);
10330Sstevel@tonic-gate 		return(-1);
10340Sstevel@tonic-gate 	}
10355087Sjp161948 	if (!S_ISREG(sb.st_mode)) {
10365087Sjp161948 		error("%s is not a regular file", local_path);
10375087Sjp161948 		close(local_fd);
10385087Sjp161948 		return(-1);
10395087Sjp161948 	}
10400Sstevel@tonic-gate 	stat_to_attrib(&sb, &a);
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
10430Sstevel@tonic-gate 	a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
10440Sstevel@tonic-gate 	a.perm &= 0777;
10450Sstevel@tonic-gate 	if (!pflag)
10460Sstevel@tonic-gate 		a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	buffer_init(&msg);
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	/* Send open request */
10510Sstevel@tonic-gate 	id = conn->msg_id++;
10520Sstevel@tonic-gate 	buffer_put_char(&msg, SSH2_FXP_OPEN);
10530Sstevel@tonic-gate 	buffer_put_int(&msg, id);
10540Sstevel@tonic-gate 	buffer_put_cstring(&msg, remote_path);
10550Sstevel@tonic-gate 	buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
10560Sstevel@tonic-gate 	encode_attrib(&msg, &a);
10570Sstevel@tonic-gate 	send_msg(conn->fd_out, &msg);
10580Sstevel@tonic-gate 	debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	buffer_clear(&msg);
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	handle = get_handle(conn->fd_in, id, &handle_len);
10630Sstevel@tonic-gate 	if (handle == NULL) {
10640Sstevel@tonic-gate 		close(local_fd);
10650Sstevel@tonic-gate 		buffer_free(&msg);
10660Sstevel@tonic-gate 		return(-1);
10670Sstevel@tonic-gate 	}
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	startid = ackid = id + 1;
10700Sstevel@tonic-gate 	data = xmalloc(conn->transfer_buflen);
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	/* Read from local and write to remote */
10730Sstevel@tonic-gate 	offset = 0;
10745087Sjp161948 	if (showprogress)
10755087Sjp161948 		start_progress_meter(local_path, sb.st_size, (off_t *)&offset);
10765087Sjp161948 
10770Sstevel@tonic-gate 	for (;;) {
10780Sstevel@tonic-gate 		int len;
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 		/*
10815087Sjp161948 		 * Can't use atomicio here because it returns 0 on EOF,
10825087Sjp161948 		 * thus losing the last block of the file.
10835087Sjp161948 		 * Simulate an EOF on interrupt, allowing ACKs from the
10845087Sjp161948 		 * server to drain.
10850Sstevel@tonic-gate 		 */
10865087Sjp161948 		if (interrupted)
10875087Sjp161948 			len = 0;
10885087Sjp161948 		else do
10890Sstevel@tonic-gate 			len = read(local_fd, data, conn->transfer_buflen);
10900Sstevel@tonic-gate 		while ((len == -1) && (errno == EINTR || errno == EAGAIN));
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 		if (len == -1)
10930Sstevel@tonic-gate 			fatal("Couldn't read from \"%s\": %s", local_path,
10940Sstevel@tonic-gate 			    strerror(errno));
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		if (len != 0) {
10970Sstevel@tonic-gate 			ack = xmalloc(sizeof(*ack));
10980Sstevel@tonic-gate 			ack->id = ++id;
10990Sstevel@tonic-gate 			ack->offset = offset;
11000Sstevel@tonic-gate 			ack->len = len;
11010Sstevel@tonic-gate 			TAILQ_INSERT_TAIL(&acks, ack, tq);
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 			buffer_clear(&msg);
11040Sstevel@tonic-gate 			buffer_put_char(&msg, SSH2_FXP_WRITE);
11050Sstevel@tonic-gate 			buffer_put_int(&msg, ack->id);
11060Sstevel@tonic-gate 			buffer_put_string(&msg, handle, handle_len);
11070Sstevel@tonic-gate 			buffer_put_int64(&msg, offset);
11080Sstevel@tonic-gate 			buffer_put_string(&msg, data, len);
11090Sstevel@tonic-gate 			send_msg(conn->fd_out, &msg);
11100Sstevel@tonic-gate 			debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
11115087Sjp161948 			    id, (unsigned long long)offset, len);
11120Sstevel@tonic-gate 		} else if (TAILQ_FIRST(&acks) == NULL)
11130Sstevel@tonic-gate 			break;
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 		if (ack == NULL)
11160Sstevel@tonic-gate 			fatal("Unexpected ACK %u", id);
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 		if (id == startid || len == 0 ||
11190Sstevel@tonic-gate 		    id - ackid >= conn->num_requests) {
11200Sstevel@tonic-gate 			u_int r_id;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 			buffer_clear(&msg);
11230Sstevel@tonic-gate 			get_msg(conn->fd_in, &msg);
11240Sstevel@tonic-gate 			type = buffer_get_char(&msg);
11250Sstevel@tonic-gate 			r_id = buffer_get_int(&msg);
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 			if (type != SSH2_FXP_STATUS)
11280Sstevel@tonic-gate 				fatal("Expected SSH2_FXP_STATUS(%d) packet, "
11290Sstevel@tonic-gate 				    "got %d", SSH2_FXP_STATUS, type);
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 			status = buffer_get_int(&msg);
11320Sstevel@tonic-gate 			debug3("SSH2_FXP_STATUS %d", status);
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 			/* Find the request in our queue */
11355087Sjp161948 			for (ack = TAILQ_FIRST(&acks);
11360Sstevel@tonic-gate 			    ack != NULL && ack->id != r_id;
11370Sstevel@tonic-gate 			    ack = TAILQ_NEXT(ack, tq))
11380Sstevel@tonic-gate 				;
11390Sstevel@tonic-gate 			if (ack == NULL)
11400Sstevel@tonic-gate 				fatal("Can't find request for ID %u", r_id);
11410Sstevel@tonic-gate 			TAILQ_REMOVE(&acks, ack, tq);
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 			if (status != SSH2_FX_OK) {
11440Sstevel@tonic-gate 				error("Couldn't write to remote file \"%s\": %s",
11455087Sjp161948 				    remote_path, fx2txt(status));
11465087Sjp161948 				if (showprogress)
11475087Sjp161948 					stop_progress_meter();
11480Sstevel@tonic-gate 				do_close(conn, handle, handle_len);
11490Sstevel@tonic-gate 				close(local_fd);
11505087Sjp161948 				xfree(data);
11515087Sjp161948 				xfree(ack);
11525087Sjp161948 				status = -1;
11530Sstevel@tonic-gate 				goto done;
11540Sstevel@tonic-gate 			}
11550Sstevel@tonic-gate 			debug3("In write loop, ack for %u %u bytes at %llu",
11565087Sjp161948 			    ack->id, ack->len, (unsigned long long)ack->offset);
11570Sstevel@tonic-gate 			++ackid;
11580Sstevel@tonic-gate 			xfree(ack);
11590Sstevel@tonic-gate 		}
11600Sstevel@tonic-gate 		offset += len;
11610Sstevel@tonic-gate 	}
11625087Sjp161948 	if (showprogress)
11635087Sjp161948 		stop_progress_meter();
11640Sstevel@tonic-gate 	xfree(data);
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	if (close(local_fd) == -1) {
11670Sstevel@tonic-gate 		error("Couldn't close local file \"%s\": %s", local_path,
11680Sstevel@tonic-gate 		    strerror(errno));
11690Sstevel@tonic-gate 		do_close(conn, handle, handle_len);
11700Sstevel@tonic-gate 		status = -1;
11710Sstevel@tonic-gate 		goto done;
11720Sstevel@tonic-gate 	}
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	/* Override umask and utimes if asked */
11750Sstevel@tonic-gate 	if (pflag)
11760Sstevel@tonic-gate 		do_fsetstat(conn, handle, handle_len, &a);
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	status = do_close(conn, handle, handle_len);
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate done:
11810Sstevel@tonic-gate 	xfree(handle);
11820Sstevel@tonic-gate 	buffer_free(&msg);
11830Sstevel@tonic-gate 	return(status);
11840Sstevel@tonic-gate }
1185