xref: /netbsd-src/crypto/external/bsd/openssh/dist/sftp-client.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: sftp-client.c,v 1.3 2009/12/27 01:40:47 christos Exp $	*/
2 /* $OpenBSD: sftp-client.c,v 1.87 2009/06/22 05:39:28 dtucker Exp $ */
3 /*
4  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /* XXX: memleaks */
20 /* XXX: signed vs unsigned */
21 /* XXX: remove all logging, only return status codes */
22 /* XXX: copy between two remote sites */
23 
24 #include "includes.h"
25 __RCSID("$NetBSD: sftp-client.c,v 1.3 2009/12/27 01:40:47 christos Exp $");
26 #include <sys/types.h>
27 #include <sys/queue.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30 #include <sys/param.h>
31 #include <sys/statvfs.h>
32 #include <sys/uio.h>
33 
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <signal.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include "xmalloc.h"
43 #include "buffer.h"
44 #include "log.h"
45 #include "atomicio.h"
46 #include "progressmeter.h"
47 #include "misc.h"
48 
49 #include "sftp.h"
50 #include "sftp-common.h"
51 #include "sftp-client.h"
52 
53 extern volatile sig_atomic_t interrupted;
54 extern int showprogress;
55 
56 /* Minimum amount of data to read at a time */
57 #define MIN_READ_SIZE	512
58 
59 struct sftp_conn {
60 	int fd_in;
61 	int fd_out;
62 	u_int transfer_buflen;
63 	u_int num_requests;
64 	u_int version;
65 	u_int msg_id;
66 #define SFTP_EXT_POSIX_RENAME	0x00000001
67 #define SFTP_EXT_STATVFS	0x00000002
68 #define SFTP_EXT_FSTATVFS	0x00000004
69 	u_int exts;
70 };
71 
72 static void
73 send_msg(int fd, Buffer *m)
74 {
75 	u_char mlen[4];
76 	struct iovec iov[2];
77 
78 	if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
79 		fatal("Outbound message too long %u", buffer_len(m));
80 
81 	/* Send length first */
82 	put_u32(mlen, buffer_len(m));
83 	iov[0].iov_base = mlen;
84 	iov[0].iov_len = sizeof(mlen);
85 	iov[1].iov_base = buffer_ptr(m);
86 	iov[1].iov_len = buffer_len(m);
87 
88 	if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
89 		fatal("Couldn't send packet: %s", strerror(errno));
90 
91 	buffer_clear(m);
92 }
93 
94 static void
95 get_msg(int fd, Buffer *m)
96 {
97 	u_int msg_len;
98 
99 	buffer_append_space(m, 4);
100 	if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
101 		if (errno == EPIPE)
102 			fatal("Connection closed");
103 		else
104 			fatal("Couldn't read packet: %s", strerror(errno));
105 	}
106 
107 	msg_len = buffer_get_int(m);
108 	if (msg_len > SFTP_MAX_MSG_LENGTH)
109 		fatal("Received message too long %u", msg_len);
110 
111 	buffer_append_space(m, msg_len);
112 	if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
113 		if (errno == EPIPE)
114 			fatal("Connection closed");
115 		else
116 			fatal("Read packet: %s", strerror(errno));
117 	}
118 }
119 
120 static void
121 send_string_request(int fd, u_int id, u_int code, char *s,
122     u_int len)
123 {
124 	Buffer msg;
125 
126 	buffer_init(&msg);
127 	buffer_put_char(&msg, code);
128 	buffer_put_int(&msg, id);
129 	buffer_put_string(&msg, s, len);
130 	send_msg(fd, &msg);
131 	debug3("Sent message fd %d T:%u I:%u", fd, code, id);
132 	buffer_free(&msg);
133 }
134 
135 static void
136 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
137     u_int len, Attrib *a)
138 {
139 	Buffer msg;
140 
141 	buffer_init(&msg);
142 	buffer_put_char(&msg, code);
143 	buffer_put_int(&msg, id);
144 	buffer_put_string(&msg, s, len);
145 	encode_attrib(&msg, a);
146 	send_msg(fd, &msg);
147 	debug3("Sent message fd %d T:%u I:%u", fd, code, id);
148 	buffer_free(&msg);
149 }
150 
151 static u_int
152 get_status(int fd, u_int expected_id)
153 {
154 	Buffer msg;
155 	u_int type, id, status;
156 
157 	buffer_init(&msg);
158 	get_msg(fd, &msg);
159 	type = buffer_get_char(&msg);
160 	id = buffer_get_int(&msg);
161 
162 	if (id != expected_id)
163 		fatal("ID mismatch (%u != %u)", id, expected_id);
164 	if (type != SSH2_FXP_STATUS)
165 		fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
166 		    SSH2_FXP_STATUS, type);
167 
168 	status = buffer_get_int(&msg);
169 	buffer_free(&msg);
170 
171 	debug3("SSH2_FXP_STATUS %u", status);
172 
173 	return(status);
174 }
175 
176 static char *
177 get_handle(int fd, u_int expected_id, u_int *len)
178 {
179 	Buffer msg;
180 	u_int type, id;
181 	char *handle;
182 
183 	buffer_init(&msg);
184 	get_msg(fd, &msg);
185 	type = buffer_get_char(&msg);
186 	id = buffer_get_int(&msg);
187 
188 	if (id != expected_id)
189 		fatal("ID mismatch (%u != %u)", id, expected_id);
190 	if (type == SSH2_FXP_STATUS) {
191 		int status = buffer_get_int(&msg);
192 
193 		error("Couldn't get handle: %s", fx2txt(status));
194 		buffer_free(&msg);
195 		return(NULL);
196 	} else if (type != SSH2_FXP_HANDLE)
197 		fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
198 		    SSH2_FXP_HANDLE, type);
199 
200 	handle = buffer_get_string(&msg, len);
201 	buffer_free(&msg);
202 
203 	return(handle);
204 }
205 
206 static Attrib *
207 get_decode_stat(int fd, u_int expected_id, int quiet)
208 {
209 	Buffer msg;
210 	u_int type, id;
211 	Attrib *a;
212 
213 	buffer_init(&msg);
214 	get_msg(fd, &msg);
215 
216 	type = buffer_get_char(&msg);
217 	id = buffer_get_int(&msg);
218 
219 	debug3("Received stat reply T:%u I:%u", type, id);
220 	if (id != expected_id)
221 		fatal("ID mismatch (%u != %u)", id, expected_id);
222 	if (type == SSH2_FXP_STATUS) {
223 		int status = buffer_get_int(&msg);
224 
225 		if (quiet)
226 			debug("Couldn't stat remote file: %s", fx2txt(status));
227 		else
228 			error("Couldn't stat remote file: %s", fx2txt(status));
229 		buffer_free(&msg);
230 		return(NULL);
231 	} else if (type != SSH2_FXP_ATTRS) {
232 		fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
233 		    SSH2_FXP_ATTRS, type);
234 	}
235 	a = decode_attrib(&msg);
236 	buffer_free(&msg);
237 
238 	return(a);
239 }
240 
241 static int
242 get_decode_statvfs(int fd, struct sftp_statvfs *st, u_int expected_id,
243     int quiet)
244 {
245 	Buffer msg;
246 	u_int type, id, flag;
247 
248 	buffer_init(&msg);
249 	get_msg(fd, &msg);
250 
251 	type = buffer_get_char(&msg);
252 	id = buffer_get_int(&msg);
253 
254 	debug3("Received statvfs reply T:%u I:%u", type, id);
255 	if (id != expected_id)
256 		fatal("ID mismatch (%u != %u)", id, expected_id);
257 	if (type == SSH2_FXP_STATUS) {
258 		int status = buffer_get_int(&msg);
259 
260 		if (quiet)
261 			debug("Couldn't statvfs: %s", fx2txt(status));
262 		else
263 			error("Couldn't statvfs: %s", fx2txt(status));
264 		buffer_free(&msg);
265 		return -1;
266 	} else if (type != SSH2_FXP_EXTENDED_REPLY) {
267 		fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
268 		    SSH2_FXP_EXTENDED_REPLY, type);
269 	}
270 
271 	bzero(st, sizeof(*st));
272 	st->f_bsize = buffer_get_int64(&msg);
273 	st->f_frsize = buffer_get_int64(&msg);
274 	st->f_blocks = buffer_get_int64(&msg);
275 	st->f_bfree = buffer_get_int64(&msg);
276 	st->f_bavail = buffer_get_int64(&msg);
277 	st->f_files = buffer_get_int64(&msg);
278 	st->f_ffree = buffer_get_int64(&msg);
279 	st->f_favail = buffer_get_int64(&msg);
280 	st->f_fsid = buffer_get_int64(&msg);
281 	flag = buffer_get_int64(&msg);
282 	st->f_namemax = buffer_get_int64(&msg);
283 
284 	st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
285 	st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
286 
287 	buffer_free(&msg);
288 
289 	return 0;
290 }
291 
292 struct sftp_conn *
293 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
294 {
295 	u_int type, exts = 0;
296 	int version;
297 	Buffer msg;
298 	struct sftp_conn *ret;
299 
300 	buffer_init(&msg);
301 	buffer_put_char(&msg, SSH2_FXP_INIT);
302 	buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
303 	send_msg(fd_out, &msg);
304 
305 	buffer_clear(&msg);
306 
307 	get_msg(fd_in, &msg);
308 
309 	/* Expecting a VERSION reply */
310 	if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
311 		error("Invalid packet back from SSH2_FXP_INIT (type %u)",
312 		    type);
313 		buffer_free(&msg);
314 		return(NULL);
315 	}
316 	version = buffer_get_int(&msg);
317 
318 	debug2("Remote version: %d", version);
319 
320 	/* Check for extensions */
321 	while (buffer_len(&msg) > 0) {
322 		char *name = buffer_get_string(&msg, NULL);
323 		char *value = buffer_get_string(&msg, NULL);
324 		int known = 0;
325 
326 		if (strcmp(name, "posix-rename@openssh.com") == 0 &&
327 		    strcmp(value, "1") == 0) {
328 			exts |= SFTP_EXT_POSIX_RENAME;
329 			known = 1;
330 		} else if (strcmp(name, "statvfs@openssh.com") == 0 &&
331 		    strcmp(value, "2") == 0) {
332 			exts |= SFTP_EXT_STATVFS;
333 			known = 1;
334 		} if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
335 		    strcmp(value, "2") == 0) {
336 			exts |= SFTP_EXT_FSTATVFS;
337 			known = 1;
338 		}
339 		if (known) {
340 			debug2("Server supports extension \"%s\" revision %s",
341 			    name, value);
342 		} else {
343 			debug2("Unrecognised server extension \"%s\"", name);
344 		}
345 		xfree(name);
346 		xfree(value);
347 	}
348 
349 	buffer_free(&msg);
350 
351 	ret = xmalloc(sizeof(*ret));
352 	ret->fd_in = fd_in;
353 	ret->fd_out = fd_out;
354 	ret->transfer_buflen = transfer_buflen;
355 	ret->num_requests = num_requests;
356 	ret->version = version;
357 	ret->msg_id = 1;
358 	ret->exts = exts;
359 
360 	/* Some filexfer v.0 servers don't support large packets */
361 	if (version == 0)
362 		ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
363 
364 	return(ret);
365 }
366 
367 u_int
368 sftp_proto_version(struct sftp_conn *conn)
369 {
370 	return(conn->version);
371 }
372 
373 int
374 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
375 {
376 	u_int id, status;
377 	Buffer msg;
378 
379 	buffer_init(&msg);
380 
381 	id = conn->msg_id++;
382 	buffer_put_char(&msg, SSH2_FXP_CLOSE);
383 	buffer_put_int(&msg, id);
384 	buffer_put_string(&msg, handle, handle_len);
385 	send_msg(conn->fd_out, &msg);
386 	debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
387 
388 	status = get_status(conn->fd_in, id);
389 	if (status != SSH2_FX_OK)
390 		error("Couldn't close file: %s", fx2txt(status));
391 
392 	buffer_free(&msg);
393 
394 	return(status);
395 }
396 
397 
398 static int
399 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
400     SFTP_DIRENT ***dir)
401 {
402 	Buffer msg;
403 	u_int count, type, id, handle_len, i, expected_id, ents = 0;
404 	char *handle;
405 
406 	id = conn->msg_id++;
407 
408 	buffer_init(&msg);
409 	buffer_put_char(&msg, SSH2_FXP_OPENDIR);
410 	buffer_put_int(&msg, id);
411 	buffer_put_cstring(&msg, path);
412 	send_msg(conn->fd_out, &msg);
413 
414 	buffer_clear(&msg);
415 
416 	handle = get_handle(conn->fd_in, id, &handle_len);
417 	if (handle == NULL)
418 		return(-1);
419 
420 	if (dir) {
421 		ents = 0;
422 		*dir = xmalloc(sizeof(**dir));
423 		(*dir)[0] = NULL;
424 	}
425 
426 	for (; !interrupted;) {
427 		id = expected_id = conn->msg_id++;
428 
429 		debug3("Sending SSH2_FXP_READDIR I:%u", id);
430 
431 		buffer_clear(&msg);
432 		buffer_put_char(&msg, SSH2_FXP_READDIR);
433 		buffer_put_int(&msg, id);
434 		buffer_put_string(&msg, handle, handle_len);
435 		send_msg(conn->fd_out, &msg);
436 
437 		buffer_clear(&msg);
438 
439 		get_msg(conn->fd_in, &msg);
440 
441 		type = buffer_get_char(&msg);
442 		id = buffer_get_int(&msg);
443 
444 		debug3("Received reply T:%u I:%u", type, id);
445 
446 		if (id != expected_id)
447 			fatal("ID mismatch (%u != %u)", id, expected_id);
448 
449 		if (type == SSH2_FXP_STATUS) {
450 			int status = buffer_get_int(&msg);
451 
452 			debug3("Received SSH2_FXP_STATUS %d", status);
453 
454 			if (status == SSH2_FX_EOF) {
455 				break;
456 			} else {
457 				error("Couldn't read directory: %s",
458 				    fx2txt(status));
459 				do_close(conn, handle, handle_len);
460 				xfree(handle);
461 				return(status);
462 			}
463 		} else if (type != SSH2_FXP_NAME)
464 			fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
465 			    SSH2_FXP_NAME, type);
466 
467 		count = buffer_get_int(&msg);
468 		if (count == 0)
469 			break;
470 		debug3("Received %d SSH2_FXP_NAME responses", count);
471 		for (i = 0; i < count; i++) {
472 			char *filename, *longname;
473 			Attrib *a;
474 
475 			filename = buffer_get_string(&msg, NULL);
476 			longname = buffer_get_string(&msg, NULL);
477 			a = decode_attrib(&msg);
478 
479 			if (printflag)
480 				printf("%s\n", longname);
481 
482 			if (dir) {
483 				*dir = xrealloc(*dir, ents + 2, sizeof(**dir));
484 				(*dir)[ents] = xmalloc(sizeof(***dir));
485 				(*dir)[ents]->filename = xstrdup(filename);
486 				(*dir)[ents]->longname = xstrdup(longname);
487 				memcpy(&(*dir)[ents]->a, a, sizeof(*a));
488 				(*dir)[++ents] = NULL;
489 			}
490 
491 			xfree(filename);
492 			xfree(longname);
493 		}
494 	}
495 
496 	buffer_free(&msg);
497 	do_close(conn, handle, handle_len);
498 	xfree(handle);
499 
500 	/* Don't return partial matches on interrupt */
501 	if (interrupted && dir != NULL && *dir != NULL) {
502 		free_sftp_dirents(*dir);
503 		*dir = xmalloc(sizeof(**dir));
504 		**dir = NULL;
505 	}
506 
507 	return(0);
508 }
509 
510 int
511 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
512 {
513 	return(do_lsreaddir(conn, path, 0, dir));
514 }
515 
516 void free_sftp_dirents(SFTP_DIRENT **s)
517 {
518 	int i;
519 
520 	for (i = 0; s[i]; i++) {
521 		xfree(s[i]->filename);
522 		xfree(s[i]->longname);
523 		xfree(s[i]);
524 	}
525 	xfree(s);
526 }
527 
528 int
529 do_rm(struct sftp_conn *conn, char *path)
530 {
531 	u_int status, id;
532 
533 	debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
534 
535 	id = conn->msg_id++;
536 	send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
537 	    strlen(path));
538 	status = get_status(conn->fd_in, id);
539 	if (status != SSH2_FX_OK)
540 		error("Couldn't delete file: %s", fx2txt(status));
541 	return(status);
542 }
543 
544 int
545 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
546 {
547 	u_int status, id;
548 
549 	id = conn->msg_id++;
550 	send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
551 	    strlen(path), a);
552 
553 	status = get_status(conn->fd_in, id);
554 	if (status != SSH2_FX_OK)
555 		error("Couldn't create directory: %s", fx2txt(status));
556 
557 	return(status);
558 }
559 
560 int
561 do_rmdir(struct sftp_conn *conn, char *path)
562 {
563 	u_int status, id;
564 
565 	id = conn->msg_id++;
566 	send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
567 	    strlen(path));
568 
569 	status = get_status(conn->fd_in, id);
570 	if (status != SSH2_FX_OK)
571 		error("Couldn't remove directory: %s", fx2txt(status));
572 
573 	return(status);
574 }
575 
576 Attrib *
577 do_stat(struct sftp_conn *conn, char *path, int quiet)
578 {
579 	u_int id;
580 
581 	id = conn->msg_id++;
582 
583 	send_string_request(conn->fd_out, id,
584 	    conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
585 	    path, strlen(path));
586 
587 	return(get_decode_stat(conn->fd_in, id, quiet));
588 }
589 
590 Attrib *
591 do_lstat(struct sftp_conn *conn, char *path, int quiet)
592 {
593 	u_int id;
594 
595 	if (conn->version == 0) {
596 		if (quiet)
597 			debug("Server version does not support lstat operation");
598 		else
599 			logit("Server version does not support lstat operation");
600 		return(do_stat(conn, path, quiet));
601 	}
602 
603 	id = conn->msg_id++;
604 	send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
605 	    strlen(path));
606 
607 	return(get_decode_stat(conn->fd_in, id, quiet));
608 }
609 
610 #ifdef notyet
611 Attrib *
612 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
613 {
614 	u_int id;
615 
616 	id = conn->msg_id++;
617 	send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
618 	    handle_len);
619 
620 	return(get_decode_stat(conn->fd_in, id, quiet));
621 }
622 #endif
623 
624 int
625 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
626 {
627 	u_int status, id;
628 
629 	id = conn->msg_id++;
630 	send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
631 	    strlen(path), a);
632 
633 	status = get_status(conn->fd_in, id);
634 	if (status != SSH2_FX_OK)
635 		error("Couldn't setstat on \"%s\": %s", path,
636 		    fx2txt(status));
637 
638 	return(status);
639 }
640 
641 int
642 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
643     Attrib *a)
644 {
645 	u_int status, id;
646 
647 	id = conn->msg_id++;
648 	send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
649 	    handle_len, a);
650 
651 	status = get_status(conn->fd_in, id);
652 	if (status != SSH2_FX_OK)
653 		error("Couldn't fsetstat: %s", fx2txt(status));
654 
655 	return(status);
656 }
657 
658 char *
659 do_realpath(struct sftp_conn *conn, char *path)
660 {
661 	Buffer msg;
662 	u_int type, expected_id, count, id;
663 	char *filename, *longname;
664 	Attrib *a;
665 
666 	expected_id = id = conn->msg_id++;
667 	send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
668 	    strlen(path));
669 
670 	buffer_init(&msg);
671 
672 	get_msg(conn->fd_in, &msg);
673 	type = buffer_get_char(&msg);
674 	id = buffer_get_int(&msg);
675 
676 	if (id != expected_id)
677 		fatal("ID mismatch (%u != %u)", id, expected_id);
678 
679 	if (type == SSH2_FXP_STATUS) {
680 		u_int status = buffer_get_int(&msg);
681 
682 		error("Couldn't canonicalise: %s", fx2txt(status));
683 		return(NULL);
684 	} else if (type != SSH2_FXP_NAME)
685 		fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
686 		    SSH2_FXP_NAME, type);
687 
688 	count = buffer_get_int(&msg);
689 	if (count != 1)
690 		fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
691 
692 	filename = buffer_get_string(&msg, NULL);
693 	longname = buffer_get_string(&msg, NULL);
694 	a = decode_attrib(&msg);
695 
696 	debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
697 
698 	xfree(longname);
699 
700 	buffer_free(&msg);
701 
702 	return(filename);
703 }
704 
705 int
706 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
707 {
708 	Buffer msg;
709 	u_int status, id;
710 
711 	buffer_init(&msg);
712 
713 	/* Send rename request */
714 	id = conn->msg_id++;
715 	if ((conn->exts & SFTP_EXT_POSIX_RENAME)) {
716 		buffer_put_char(&msg, SSH2_FXP_EXTENDED);
717 		buffer_put_int(&msg, id);
718 		buffer_put_cstring(&msg, "posix-rename@openssh.com");
719 	} else {
720 		buffer_put_char(&msg, SSH2_FXP_RENAME);
721 		buffer_put_int(&msg, id);
722 	}
723 	buffer_put_cstring(&msg, oldpath);
724 	buffer_put_cstring(&msg, newpath);
725 	send_msg(conn->fd_out, &msg);
726 	debug3("Sent message %s \"%s\" -> \"%s\"",
727 	    (conn->exts & SFTP_EXT_POSIX_RENAME) ? "posix-rename@openssh.com" :
728 	    "SSH2_FXP_RENAME", oldpath, newpath);
729 	buffer_free(&msg);
730 
731 	status = get_status(conn->fd_in, id);
732 	if (status != SSH2_FX_OK)
733 		error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
734 		    newpath, fx2txt(status));
735 
736 	return(status);
737 }
738 
739 int
740 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
741 {
742 	Buffer msg;
743 	u_int status, id;
744 
745 	if (conn->version < 3) {
746 		error("This server does not support the symlink operation");
747 		return(SSH2_FX_OP_UNSUPPORTED);
748 	}
749 
750 	buffer_init(&msg);
751 
752 	/* Send symlink request */
753 	id = conn->msg_id++;
754 	buffer_put_char(&msg, SSH2_FXP_SYMLINK);
755 	buffer_put_int(&msg, id);
756 	buffer_put_cstring(&msg, oldpath);
757 	buffer_put_cstring(&msg, newpath);
758 	send_msg(conn->fd_out, &msg);
759 	debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
760 	    newpath);
761 	buffer_free(&msg);
762 
763 	status = get_status(conn->fd_in, id);
764 	if (status != SSH2_FX_OK)
765 		error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
766 		    newpath, fx2txt(status));
767 
768 	return(status);
769 }
770 
771 #ifdef notyet
772 char *
773 do_readlink(struct sftp_conn *conn, char *path)
774 {
775 	Buffer msg;
776 	u_int type, expected_id, count, id;
777 	char *filename, *longname;
778 	Attrib *a;
779 
780 	expected_id = id = conn->msg_id++;
781 	send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
782 	    strlen(path));
783 
784 	buffer_init(&msg);
785 
786 	get_msg(conn->fd_in, &msg);
787 	type = buffer_get_char(&msg);
788 	id = buffer_get_int(&msg);
789 
790 	if (id != expected_id)
791 		fatal("ID mismatch (%u != %u)", id, expected_id);
792 
793 	if (type == SSH2_FXP_STATUS) {
794 		u_int status = buffer_get_int(&msg);
795 
796 		error("Couldn't readlink: %s", fx2txt(status));
797 		return(NULL);
798 	} else if (type != SSH2_FXP_NAME)
799 		fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
800 		    SSH2_FXP_NAME, type);
801 
802 	count = buffer_get_int(&msg);
803 	if (count != 1)
804 		fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
805 
806 	filename = buffer_get_string(&msg, NULL);
807 	longname = buffer_get_string(&msg, NULL);
808 	a = decode_attrib(&msg);
809 
810 	debug3("SSH_FXP_READLINK %s -> %s", path, filename);
811 
812 	xfree(longname);
813 
814 	buffer_free(&msg);
815 
816 	return(filename);
817 }
818 #endif
819 
820 int
821 do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
822     int quiet)
823 {
824 	Buffer msg;
825 	u_int id;
826 
827 	if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
828 		error("Server does not support statvfs@openssh.com extension");
829 		return -1;
830 	}
831 
832 	id = conn->msg_id++;
833 
834 	buffer_init(&msg);
835 	buffer_clear(&msg);
836 	buffer_put_char(&msg, SSH2_FXP_EXTENDED);
837 	buffer_put_int(&msg, id);
838 	buffer_put_cstring(&msg, "statvfs@openssh.com");
839 	buffer_put_cstring(&msg, path);
840 	send_msg(conn->fd_out, &msg);
841 	buffer_free(&msg);
842 
843 	return get_decode_statvfs(conn->fd_in, st, id, quiet);
844 }
845 
846 #ifdef notyet
847 int
848 do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
849     struct sftp_statvfs *st, int quiet)
850 {
851 	Buffer msg;
852 	u_int id;
853 
854 	if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
855 		error("Server does not support fstatvfs@openssh.com extension");
856 		return -1;
857 	}
858 
859 	id = conn->msg_id++;
860 
861 	buffer_init(&msg);
862 	buffer_clear(&msg);
863 	buffer_put_char(&msg, SSH2_FXP_EXTENDED);
864 	buffer_put_int(&msg, id);
865 	buffer_put_cstring(&msg, "fstatvfs@openssh.com");
866 	buffer_put_string(&msg, handle, handle_len);
867 	send_msg(conn->fd_out, &msg);
868 	buffer_free(&msg);
869 
870 	return get_decode_statvfs(conn->fd_in, st, id, quiet);
871 }
872 #endif
873 
874 static void
875 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
876     char *handle, u_int handle_len)
877 {
878 	Buffer msg;
879 
880 	buffer_init(&msg);
881 	buffer_clear(&msg);
882 	buffer_put_char(&msg, SSH2_FXP_READ);
883 	buffer_put_int(&msg, id);
884 	buffer_put_string(&msg, handle, handle_len);
885 	buffer_put_int64(&msg, offset);
886 	buffer_put_int(&msg, len);
887 	send_msg(fd_out, &msg);
888 	buffer_free(&msg);
889 }
890 
891 int
892 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
893     int pflag)
894 {
895 	Attrib junk, *a;
896 	Buffer msg;
897 	char *handle;
898 	int local_fd, status = 0, write_error;
899 	int read_error, write_errno;
900 	u_int64_t offset, size;
901 	u_int handle_len, mode, type, id, buflen, num_req, max_req;
902 	off_t progress_counter;
903 	struct request {
904 		u_int id;
905 		u_int len;
906 		u_int64_t offset;
907 		TAILQ_ENTRY(request) tq;
908 	};
909 	TAILQ_HEAD(reqhead, request) requests;
910 	struct request *req;
911 
912 	status = -1;
913 	TAILQ_INIT(&requests);
914 
915 	a = do_stat(conn, remote_path, 0);
916 	if (a == NULL)
917 		return(-1);
918 
919 	/* Do not preserve set[ug]id here, as we do not preserve ownership */
920 	if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
921 		mode = a->perm & 0777;
922 	else
923 		mode = 0666;
924 
925 	if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
926 	    (!S_ISREG(a->perm))) {
927 		error("Cannot download non-regular file: %s", remote_path);
928 		return(-1);
929 	}
930 
931 	if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
932 		size = a->size;
933 	else
934 		size = 0;
935 
936 	buflen = conn->transfer_buflen;
937 	buffer_init(&msg);
938 
939 	/* Send open request */
940 	id = conn->msg_id++;
941 	buffer_put_char(&msg, SSH2_FXP_OPEN);
942 	buffer_put_int(&msg, id);
943 	buffer_put_cstring(&msg, remote_path);
944 	buffer_put_int(&msg, SSH2_FXF_READ);
945 	attrib_clear(&junk); /* Send empty attributes */
946 	encode_attrib(&msg, &junk);
947 	send_msg(conn->fd_out, &msg);
948 	debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
949 
950 	handle = get_handle(conn->fd_in, id, &handle_len);
951 	if (handle == NULL) {
952 		buffer_free(&msg);
953 		return(-1);
954 	}
955 
956 	local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
957 	    mode | S_IWRITE);
958 	if (local_fd == -1) {
959 		error("Couldn't open local file \"%s\" for writing: %s",
960 		    local_path, strerror(errno));
961 		do_close(conn, handle, handle_len);
962 		buffer_free(&msg);
963 		xfree(handle);
964 		return(-1);
965 	}
966 
967 	/* Read from remote and write to local */
968 	write_error = read_error = write_errno = num_req = offset = 0;
969 	max_req = 1;
970 	progress_counter = 0;
971 
972 	if (showprogress && size != 0)
973 		start_progress_meter(remote_path, size, &progress_counter);
974 
975 	while (num_req > 0 || max_req > 0) {
976 		char *data;
977 		u_int len;
978 
979 		/*
980 		 * Simulate EOF on interrupt: stop sending new requests and
981 		 * allow outstanding requests to drain gracefully
982 		 */
983 		if (interrupted) {
984 			if (num_req == 0) /* If we haven't started yet... */
985 				break;
986 			max_req = 0;
987 		}
988 
989 		/* Send some more requests */
990 		while (num_req < max_req) {
991 			debug3("Request range %llu -> %llu (%d/%d)",
992 			    (unsigned long long)offset,
993 			    (unsigned long long)offset + buflen - 1,
994 			    num_req, max_req);
995 			req = xmalloc(sizeof(*req));
996 			req->id = conn->msg_id++;
997 			req->len = buflen;
998 			req->offset = offset;
999 			offset += buflen;
1000 			num_req++;
1001 			TAILQ_INSERT_TAIL(&requests, req, tq);
1002 			send_read_request(conn->fd_out, req->id, req->offset,
1003 			    req->len, handle, handle_len);
1004 		}
1005 
1006 		buffer_clear(&msg);
1007 		get_msg(conn->fd_in, &msg);
1008 		type = buffer_get_char(&msg);
1009 		id = buffer_get_int(&msg);
1010 		debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1011 
1012 		/* Find the request in our queue */
1013 		for (req = TAILQ_FIRST(&requests);
1014 		    req != NULL && req->id != id;
1015 		    req = TAILQ_NEXT(req, tq))
1016 			;
1017 		if (req == NULL)
1018 			fatal("Unexpected reply %u", id);
1019 
1020 		switch (type) {
1021 		case SSH2_FXP_STATUS:
1022 			status = buffer_get_int(&msg);
1023 			if (status != SSH2_FX_EOF)
1024 				read_error = 1;
1025 			max_req = 0;
1026 			TAILQ_REMOVE(&requests, req, tq);
1027 			xfree(req);
1028 			num_req--;
1029 			break;
1030 		case SSH2_FXP_DATA:
1031 			data = buffer_get_string(&msg, &len);
1032 			debug3("Received data %llu -> %llu",
1033 			    (unsigned long long)req->offset,
1034 			    (unsigned long long)req->offset + len - 1);
1035 			if (len > req->len)
1036 				fatal("Received more data than asked for "
1037 				    "%u > %u", len, req->len);
1038 			if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1039 			    atomicio(vwrite, local_fd, data, len) != len) &&
1040 			    !write_error) {
1041 				write_errno = errno;
1042 				write_error = 1;
1043 				max_req = 0;
1044 			}
1045 			progress_counter += len;
1046 			xfree(data);
1047 
1048 			if (len == req->len) {
1049 				TAILQ_REMOVE(&requests, req, tq);
1050 				xfree(req);
1051 				num_req--;
1052 			} else {
1053 				/* Resend the request for the missing data */
1054 				debug3("Short data block, re-requesting "
1055 				    "%llu -> %llu (%2d)",
1056 				    (unsigned long long)req->offset + len,
1057 				    (unsigned long long)req->offset +
1058 				    req->len - 1, num_req);
1059 				req->id = conn->msg_id++;
1060 				req->len -= len;
1061 				req->offset += len;
1062 				send_read_request(conn->fd_out, req->id,
1063 				    req->offset, req->len, handle, handle_len);
1064 				/* Reduce the request size */
1065 				if (len < buflen)
1066 					buflen = MAX(MIN_READ_SIZE, len);
1067 			}
1068 			if (max_req > 0) { /* max_req = 0 iff EOF received */
1069 				if (size > 0 && offset > size) {
1070 					/* Only one request at a time
1071 					 * after the expected EOF */
1072 					debug3("Finish at %llu (%2d)",
1073 					    (unsigned long long)offset,
1074 					    num_req);
1075 					max_req = 1;
1076 				} else if (max_req <= conn->num_requests) {
1077 					++max_req;
1078 				}
1079 			}
1080 			break;
1081 		default:
1082 			fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1083 			    SSH2_FXP_DATA, type);
1084 		}
1085 	}
1086 
1087 	if (showprogress && size)
1088 		stop_progress_meter();
1089 
1090 	/* Sanity check */
1091 	if (TAILQ_FIRST(&requests) != NULL)
1092 		fatal("Transfer complete, but requests still in queue");
1093 
1094 	if (read_error) {
1095 		error("Couldn't read from remote file \"%s\" : %s",
1096 		    remote_path, fx2txt(status));
1097 		do_close(conn, handle, handle_len);
1098 	} else if (write_error) {
1099 		error("Couldn't write to \"%s\": %s", local_path,
1100 		    strerror(write_errno));
1101 		status = -1;
1102 		do_close(conn, handle, handle_len);
1103 	} else {
1104 		status = do_close(conn, handle, handle_len);
1105 
1106 		/* Override umask and utimes if asked */
1107 		if (pflag && fchmod(local_fd, mode) == -1)
1108 			error("Couldn't set mode on \"%s\": %s", local_path,
1109 			    strerror(errno));
1110 		if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1111 			struct timeval tv[2];
1112 			tv[0].tv_sec = a->atime;
1113 			tv[1].tv_sec = a->mtime;
1114 			tv[0].tv_usec = tv[1].tv_usec = 0;
1115 			if (utimes(local_path, tv) == -1)
1116 				error("Can't set times on \"%s\": %s",
1117 				    local_path, strerror(errno));
1118 		}
1119 	}
1120 	close(local_fd);
1121 	buffer_free(&msg);
1122 	xfree(handle);
1123 
1124 	return(status);
1125 }
1126 
1127 int
1128 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
1129     int pflag)
1130 {
1131 	int local_fd;
1132 	int status = SSH2_FX_OK;
1133 	u_int handle_len, id, type;
1134 	off_t offset;
1135 	char *handle, *data;
1136 	Buffer msg;
1137 	struct stat sb;
1138 	Attrib a;
1139 	u_int32_t startid;
1140 	u_int32_t ackid;
1141 	struct outstanding_ack {
1142 		u_int id;
1143 		u_int len;
1144 		off_t offset;
1145 		TAILQ_ENTRY(outstanding_ack) tq;
1146 	};
1147 	TAILQ_HEAD(ackhead, outstanding_ack) acks;
1148 	struct outstanding_ack *ack = NULL;
1149 
1150 	TAILQ_INIT(&acks);
1151 
1152 	if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1153 		error("Couldn't open local file \"%s\" for reading: %s",
1154 		    local_path, strerror(errno));
1155 		return(-1);
1156 	}
1157 	if (fstat(local_fd, &sb) == -1) {
1158 		error("Couldn't fstat local file \"%s\": %s",
1159 		    local_path, strerror(errno));
1160 		close(local_fd);
1161 		return(-1);
1162 	}
1163 	if (!S_ISREG(sb.st_mode)) {
1164 		error("%s is not a regular file", local_path);
1165 		close(local_fd);
1166 		return(-1);
1167 	}
1168 	stat_to_attrib(&sb, &a);
1169 
1170 	a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1171 	a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1172 	a.perm &= 0777;
1173 	if (!pflag)
1174 		a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1175 
1176 	buffer_init(&msg);
1177 
1178 	/* Send open request */
1179 	id = conn->msg_id++;
1180 	buffer_put_char(&msg, SSH2_FXP_OPEN);
1181 	buffer_put_int(&msg, id);
1182 	buffer_put_cstring(&msg, remote_path);
1183 	buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1184 	encode_attrib(&msg, &a);
1185 	send_msg(conn->fd_out, &msg);
1186 	debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1187 
1188 	buffer_clear(&msg);
1189 
1190 	handle = get_handle(conn->fd_in, id, &handle_len);
1191 	if (handle == NULL) {
1192 		close(local_fd);
1193 		buffer_free(&msg);
1194 		return -1;
1195 	}
1196 
1197 	startid = ackid = id + 1;
1198 	data = xmalloc(conn->transfer_buflen);
1199 
1200 	/* Read from local and write to remote */
1201 	offset = 0;
1202 	if (showprogress)
1203 		start_progress_meter(local_path, sb.st_size, &offset);
1204 
1205 	for (;;) {
1206 		int len;
1207 
1208 		/*
1209 		 * Can't use atomicio here because it returns 0 on EOF,
1210 		 * thus losing the last block of the file.
1211 		 * Simulate an EOF on interrupt, allowing ACKs from the
1212 		 * server to drain.
1213 		 */
1214 		if (interrupted || status != SSH2_FX_OK)
1215 			len = 0;
1216 		else do
1217 			len = read(local_fd, data, conn->transfer_buflen);
1218 		while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1219 
1220 		if (len == -1)
1221 			fatal("Couldn't read from \"%s\": %s", local_path,
1222 			    strerror(errno));
1223 
1224 		if (len != 0) {
1225 			ack = xmalloc(sizeof(*ack));
1226 			ack->id = ++id;
1227 			ack->offset = offset;
1228 			ack->len = len;
1229 			TAILQ_INSERT_TAIL(&acks, ack, tq);
1230 
1231 			buffer_clear(&msg);
1232 			buffer_put_char(&msg, SSH2_FXP_WRITE);
1233 			buffer_put_int(&msg, ack->id);
1234 			buffer_put_string(&msg, handle, handle_len);
1235 			buffer_put_int64(&msg, offset);
1236 			buffer_put_string(&msg, data, len);
1237 			send_msg(conn->fd_out, &msg);
1238 			debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1239 			    id, (unsigned long long)offset, len);
1240 		} else if (TAILQ_FIRST(&acks) == NULL)
1241 			break;
1242 
1243 		if (ack == NULL)
1244 			fatal("Unexpected ACK %u", id);
1245 
1246 		if (id == startid || len == 0 ||
1247 		    id - ackid >= conn->num_requests) {
1248 			u_int r_id;
1249 
1250 			buffer_clear(&msg);
1251 			get_msg(conn->fd_in, &msg);
1252 			type = buffer_get_char(&msg);
1253 			r_id = buffer_get_int(&msg);
1254 
1255 			if (type != SSH2_FXP_STATUS)
1256 				fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1257 				    "got %d", SSH2_FXP_STATUS, type);
1258 
1259 			status = buffer_get_int(&msg);
1260 			debug3("SSH2_FXP_STATUS %d", status);
1261 
1262 			/* Find the request in our queue */
1263 			for (ack = TAILQ_FIRST(&acks);
1264 			    ack != NULL && ack->id != r_id;
1265 			    ack = TAILQ_NEXT(ack, tq))
1266 				;
1267 			if (ack == NULL)
1268 				fatal("Can't find request for ID %u", r_id);
1269 			TAILQ_REMOVE(&acks, ack, tq);
1270 			debug3("In write loop, ack for %u %u bytes at %lld",
1271 			    ack->id, ack->len, (long long)ack->offset);
1272 			++ackid;
1273 			xfree(ack);
1274 		}
1275 		offset += len;
1276 		if (offset < 0)
1277 			fatal("%s: offset < 0", __func__);
1278 	}
1279 	buffer_free(&msg);
1280 
1281 	if (showprogress)
1282 		stop_progress_meter();
1283 	xfree(data);
1284 
1285 	if (status != SSH2_FX_OK) {
1286 		error("Couldn't write to remote file \"%s\": %s",
1287 		    remote_path, fx2txt(status));
1288 		status = -1;
1289 	}
1290 
1291 	if (close(local_fd) == -1) {
1292 		error("Couldn't close local file \"%s\": %s", local_path,
1293 		    strerror(errno));
1294 		status = -1;
1295 	}
1296 
1297 	/* Override umask and utimes if asked */
1298 	if (pflag)
1299 		do_fsetstat(conn, handle, handle_len, &a);
1300 
1301 	if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
1302 		status = -1;
1303 	xfree(handle);
1304 
1305 	return status;
1306 }
1307