- (tim) [openbsd-compat/glob.h] Remove sys/cdefs.h include that came with
[openssh-git.git] / sftp-client.c
blob4e009ef25539e1b489289d038e3159f65763ce76
1 /* $OpenBSD: sftp-client.c,v 1.93 2010/09/22 22:58:51 djm Exp $ */
2 /*
3 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 /* XXX: memleaks */
19 /* XXX: signed vs unsigned */
20 /* XXX: remove all logging, only return status codes */
21 /* XXX: copy between two remote sites */
23 #include "includes.h"
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #ifdef HAVE_SYS_STATVFS_H
28 #include <sys/statvfs.h>
29 #endif
30 #include "openbsd-compat/sys-queue.h"
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #ifdef HAVE_SYS_TIME_H
35 # include <sys/time.h>
36 #endif
37 #include <sys/uio.h>
39 #include <dirent.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <signal.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <unistd.h>
48 #include "xmalloc.h"
49 #include "buffer.h"
50 #include "log.h"
51 #include "atomicio.h"
52 #include "progressmeter.h"
53 #include "misc.h"
55 #include "sftp.h"
56 #include "sftp-common.h"
57 #include "sftp-client.h"
59 extern volatile sig_atomic_t interrupted;
60 extern int showprogress;
62 /* Minimum amount of data to read at a time */
63 #define MIN_READ_SIZE 512
65 /* Maximum depth to descend in directory trees */
66 #define MAX_DIR_DEPTH 64
68 struct sftp_conn {
69 int fd_in;
70 int fd_out;
71 u_int transfer_buflen;
72 u_int num_requests;
73 u_int version;
74 u_int msg_id;
75 #define SFTP_EXT_POSIX_RENAME 0x00000001
76 #define SFTP_EXT_STATVFS 0x00000002
77 #define SFTP_EXT_FSTATVFS 0x00000004
78 u_int exts;
79 u_int64_t limit_kbps;
80 struct bwlimit bwlimit_in, bwlimit_out;
83 static char *
84 get_handle(struct sftp_conn *conn, u_int expected_id, u_int *len,
85 const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
87 /* ARGSUSED */
88 static int
89 sftpio(void *_bwlimit, size_t amount)
91 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
93 bandwidth_limit(bwlimit, amount);
94 return 0;
97 static void
98 send_msg(struct sftp_conn *conn, Buffer *m)
100 u_char mlen[4];
101 struct iovec iov[2];
103 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
104 fatal("Outbound message too long %u", buffer_len(m));
106 /* Send length first */
107 put_u32(mlen, buffer_len(m));
108 iov[0].iov_base = mlen;
109 iov[0].iov_len = sizeof(mlen);
110 iov[1].iov_base = buffer_ptr(m);
111 iov[1].iov_len = buffer_len(m);
113 if (atomiciov6(writev, conn->fd_out, iov, 2,
114 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_out) !=
115 buffer_len(m) + sizeof(mlen))
116 fatal("Couldn't send packet: %s", strerror(errno));
118 buffer_clear(m);
121 static void
122 get_msg(struct sftp_conn *conn, Buffer *m)
124 u_int msg_len;
126 buffer_append_space(m, 4);
127 if (atomicio6(read, conn->fd_in, buffer_ptr(m), 4,
128 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in) != 4) {
129 if (errno == EPIPE)
130 fatal("Connection closed");
131 else
132 fatal("Couldn't read packet: %s", strerror(errno));
135 msg_len = buffer_get_int(m);
136 if (msg_len > SFTP_MAX_MSG_LENGTH)
137 fatal("Received message too long %u", msg_len);
139 buffer_append_space(m, msg_len);
140 if (atomicio6(read, conn->fd_in, buffer_ptr(m), msg_len,
141 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in)
142 != msg_len) {
143 if (errno == EPIPE)
144 fatal("Connection closed");
145 else
146 fatal("Read packet: %s", strerror(errno));
150 static void
151 send_string_request(struct sftp_conn *conn, u_int id, u_int code, char *s,
152 u_int len)
154 Buffer msg;
156 buffer_init(&msg);
157 buffer_put_char(&msg, code);
158 buffer_put_int(&msg, id);
159 buffer_put_string(&msg, s, len);
160 send_msg(conn, &msg);
161 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
162 buffer_free(&msg);
165 static void
166 send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
167 char *s, u_int len, Attrib *a)
169 Buffer msg;
171 buffer_init(&msg);
172 buffer_put_char(&msg, code);
173 buffer_put_int(&msg, id);
174 buffer_put_string(&msg, s, len);
175 encode_attrib(&msg, a);
176 send_msg(conn, &msg);
177 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
178 buffer_free(&msg);
181 static u_int
182 get_status(struct sftp_conn *conn, u_int expected_id)
184 Buffer msg;
185 u_int type, id, status;
187 buffer_init(&msg);
188 get_msg(conn, &msg);
189 type = buffer_get_char(&msg);
190 id = buffer_get_int(&msg);
192 if (id != expected_id)
193 fatal("ID mismatch (%u != %u)", id, expected_id);
194 if (type != SSH2_FXP_STATUS)
195 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
196 SSH2_FXP_STATUS, type);
198 status = buffer_get_int(&msg);
199 buffer_free(&msg);
201 debug3("SSH2_FXP_STATUS %u", status);
203 return status;
206 static char *
207 get_handle(struct sftp_conn *conn, u_int expected_id, u_int *len,
208 const char *errfmt, ...)
210 Buffer msg;
211 u_int type, id;
212 char *handle, errmsg[256];
213 va_list args;
214 int status;
216 va_start(args, errfmt);
217 if (errfmt != NULL)
218 vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
219 va_end(args);
221 buffer_init(&msg);
222 get_msg(conn, &msg);
223 type = buffer_get_char(&msg);
224 id = buffer_get_int(&msg);
226 if (id != expected_id)
227 fatal("%s: ID mismatch (%u != %u)",
228 errfmt == NULL ? __func__ : errmsg, id, expected_id);
229 if (type == SSH2_FXP_STATUS) {
230 status = buffer_get_int(&msg);
231 if (errfmt != NULL)
232 error("%s: %s", errmsg, fx2txt(status));
233 buffer_free(&msg);
234 return(NULL);
235 } else if (type != SSH2_FXP_HANDLE)
236 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
237 errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
239 handle = buffer_get_string(&msg, len);
240 buffer_free(&msg);
242 return(handle);
245 static Attrib *
246 get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
248 Buffer msg;
249 u_int type, id;
250 Attrib *a;
252 buffer_init(&msg);
253 get_msg(conn, &msg);
255 type = buffer_get_char(&msg);
256 id = buffer_get_int(&msg);
258 debug3("Received stat reply T:%u I:%u", type, id);
259 if (id != expected_id)
260 fatal("ID mismatch (%u != %u)", id, expected_id);
261 if (type == SSH2_FXP_STATUS) {
262 int status = buffer_get_int(&msg);
264 if (quiet)
265 debug("Couldn't stat remote file: %s", fx2txt(status));
266 else
267 error("Couldn't stat remote file: %s", fx2txt(status));
268 buffer_free(&msg);
269 return(NULL);
270 } else if (type != SSH2_FXP_ATTRS) {
271 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
272 SSH2_FXP_ATTRS, type);
274 a = decode_attrib(&msg);
275 buffer_free(&msg);
277 return(a);
280 static int
281 get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
282 u_int expected_id, int quiet)
284 Buffer msg;
285 u_int type, id, flag;
287 buffer_init(&msg);
288 get_msg(conn, &msg);
290 type = buffer_get_char(&msg);
291 id = buffer_get_int(&msg);
293 debug3("Received statvfs reply T:%u I:%u", type, id);
294 if (id != expected_id)
295 fatal("ID mismatch (%u != %u)", id, expected_id);
296 if (type == SSH2_FXP_STATUS) {
297 int status = buffer_get_int(&msg);
299 if (quiet)
300 debug("Couldn't statvfs: %s", fx2txt(status));
301 else
302 error("Couldn't statvfs: %s", fx2txt(status));
303 buffer_free(&msg);
304 return -1;
305 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
306 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
307 SSH2_FXP_EXTENDED_REPLY, type);
310 bzero(st, sizeof(*st));
311 st->f_bsize = buffer_get_int64(&msg);
312 st->f_frsize = buffer_get_int64(&msg);
313 st->f_blocks = buffer_get_int64(&msg);
314 st->f_bfree = buffer_get_int64(&msg);
315 st->f_bavail = buffer_get_int64(&msg);
316 st->f_files = buffer_get_int64(&msg);
317 st->f_ffree = buffer_get_int64(&msg);
318 st->f_favail = buffer_get_int64(&msg);
319 st->f_fsid = buffer_get_int64(&msg);
320 flag = buffer_get_int64(&msg);
321 st->f_namemax = buffer_get_int64(&msg);
323 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
324 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
326 buffer_free(&msg);
328 return 0;
331 struct sftp_conn *
332 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
333 u_int64_t limit_kbps)
335 u_int type;
336 Buffer msg;
337 struct sftp_conn *ret;
339 ret = xmalloc(sizeof(*ret));
340 ret->fd_in = fd_in;
341 ret->fd_out = fd_out;
342 ret->transfer_buflen = transfer_buflen;
343 ret->num_requests = num_requests;
344 ret->exts = 0;
345 ret->limit_kbps = 0;
347 buffer_init(&msg);
348 buffer_put_char(&msg, SSH2_FXP_INIT);
349 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
350 send_msg(ret, &msg);
352 buffer_clear(&msg);
354 get_msg(ret, &msg);
356 /* Expecting a VERSION reply */
357 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
358 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
359 type);
360 buffer_free(&msg);
361 return(NULL);
363 ret->version = buffer_get_int(&msg);
365 debug2("Remote version: %u", ret->version);
367 /* Check for extensions */
368 while (buffer_len(&msg) > 0) {
369 char *name = buffer_get_string(&msg, NULL);
370 char *value = buffer_get_string(&msg, NULL);
371 int known = 0;
373 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
374 strcmp(value, "1") == 0) {
375 ret->exts |= SFTP_EXT_POSIX_RENAME;
376 known = 1;
377 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
378 strcmp(value, "2") == 0) {
379 ret->exts |= SFTP_EXT_STATVFS;
380 known = 1;
381 } if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
382 strcmp(value, "2") == 0) {
383 ret->exts |= SFTP_EXT_FSTATVFS;
384 known = 1;
386 if (known) {
387 debug2("Server supports extension \"%s\" revision %s",
388 name, value);
389 } else {
390 debug2("Unrecognised server extension \"%s\"", name);
392 xfree(name);
393 xfree(value);
396 buffer_free(&msg);
398 /* Some filexfer v.0 servers don't support large packets */
399 if (ret->version == 0)
400 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
402 ret->limit_kbps = limit_kbps;
403 if (ret->limit_kbps > 0) {
404 bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
405 ret->transfer_buflen);
406 bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
407 ret->transfer_buflen);
410 return ret;
413 u_int
414 sftp_proto_version(struct sftp_conn *conn)
416 return conn->version;
420 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
422 u_int id, status;
423 Buffer msg;
425 buffer_init(&msg);
427 id = conn->msg_id++;
428 buffer_put_char(&msg, SSH2_FXP_CLOSE);
429 buffer_put_int(&msg, id);
430 buffer_put_string(&msg, handle, handle_len);
431 send_msg(conn, &msg);
432 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
434 status = get_status(conn, id);
435 if (status != SSH2_FX_OK)
436 error("Couldn't close file: %s", fx2txt(status));
438 buffer_free(&msg);
440 return status;
444 static int
445 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
446 SFTP_DIRENT ***dir)
448 Buffer msg;
449 u_int count, type, id, handle_len, i, expected_id, ents = 0;
450 char *handle;
452 id = conn->msg_id++;
454 buffer_init(&msg);
455 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
456 buffer_put_int(&msg, id);
457 buffer_put_cstring(&msg, path);
458 send_msg(conn, &msg);
460 buffer_clear(&msg);
462 handle = get_handle(conn, id, &handle_len,
463 "remote readdir(\"%s\")", path);
464 if (handle == NULL)
465 return -1;
467 if (dir) {
468 ents = 0;
469 *dir = xmalloc(sizeof(**dir));
470 (*dir)[0] = NULL;
473 for (; !interrupted;) {
474 id = expected_id = conn->msg_id++;
476 debug3("Sending SSH2_FXP_READDIR I:%u", id);
478 buffer_clear(&msg);
479 buffer_put_char(&msg, SSH2_FXP_READDIR);
480 buffer_put_int(&msg, id);
481 buffer_put_string(&msg, handle, handle_len);
482 send_msg(conn, &msg);
484 buffer_clear(&msg);
486 get_msg(conn, &msg);
488 type = buffer_get_char(&msg);
489 id = buffer_get_int(&msg);
491 debug3("Received reply T:%u I:%u", type, id);
493 if (id != expected_id)
494 fatal("ID mismatch (%u != %u)", id, expected_id);
496 if (type == SSH2_FXP_STATUS) {
497 int status = buffer_get_int(&msg);
499 debug3("Received SSH2_FXP_STATUS %d", status);
501 if (status == SSH2_FX_EOF) {
502 break;
503 } else {
504 error("Couldn't read directory: %s",
505 fx2txt(status));
506 do_close(conn, handle, handle_len);
507 xfree(handle);
508 return(status);
510 } else if (type != SSH2_FXP_NAME)
511 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
512 SSH2_FXP_NAME, type);
514 count = buffer_get_int(&msg);
515 if (count == 0)
516 break;
517 debug3("Received %d SSH2_FXP_NAME responses", count);
518 for (i = 0; i < count; i++) {
519 char *filename, *longname;
520 Attrib *a;
522 filename = buffer_get_string(&msg, NULL);
523 longname = buffer_get_string(&msg, NULL);
524 a = decode_attrib(&msg);
526 if (printflag)
527 printf("%s\n", longname);
530 * Directory entries should never contain '/'
531 * These can be used to attack recursive ops
532 * (e.g. send '../../../../etc/passwd')
534 if (strchr(filename, '/') != NULL) {
535 error("Server sent suspect path \"%s\" "
536 "during readdir of \"%s\"", filename, path);
537 goto next;
540 if (dir) {
541 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
542 (*dir)[ents] = xmalloc(sizeof(***dir));
543 (*dir)[ents]->filename = xstrdup(filename);
544 (*dir)[ents]->longname = xstrdup(longname);
545 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
546 (*dir)[++ents] = NULL;
548 next:
549 xfree(filename);
550 xfree(longname);
554 buffer_free(&msg);
555 do_close(conn, handle, handle_len);
556 xfree(handle);
558 /* Don't return partial matches on interrupt */
559 if (interrupted && dir != NULL && *dir != NULL) {
560 free_sftp_dirents(*dir);
561 *dir = xmalloc(sizeof(**dir));
562 **dir = NULL;
565 return 0;
569 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
571 return(do_lsreaddir(conn, path, 0, dir));
574 void free_sftp_dirents(SFTP_DIRENT **s)
576 int i;
578 for (i = 0; s[i]; i++) {
579 xfree(s[i]->filename);
580 xfree(s[i]->longname);
581 xfree(s[i]);
583 xfree(s);
587 do_rm(struct sftp_conn *conn, char *path)
589 u_int status, id;
591 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
593 id = conn->msg_id++;
594 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
595 status = get_status(conn, id);
596 if (status != SSH2_FX_OK)
597 error("Couldn't delete file: %s", fx2txt(status));
598 return(status);
602 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a, int printflag)
604 u_int status, id;
606 id = conn->msg_id++;
607 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
608 strlen(path), a);
610 status = get_status(conn, id);
611 if (status != SSH2_FX_OK && printflag)
612 error("Couldn't create directory: %s", fx2txt(status));
614 return(status);
618 do_rmdir(struct sftp_conn *conn, char *path)
620 u_int status, id;
622 id = conn->msg_id++;
623 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
624 strlen(path));
626 status = get_status(conn, id);
627 if (status != SSH2_FX_OK)
628 error("Couldn't remove directory: %s", fx2txt(status));
630 return(status);
633 Attrib *
634 do_stat(struct sftp_conn *conn, char *path, int quiet)
636 u_int id;
638 id = conn->msg_id++;
640 send_string_request(conn, id,
641 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
642 path, strlen(path));
644 return(get_decode_stat(conn, id, quiet));
647 Attrib *
648 do_lstat(struct sftp_conn *conn, char *path, int quiet)
650 u_int id;
652 if (conn->version == 0) {
653 if (quiet)
654 debug("Server version does not support lstat operation");
655 else
656 logit("Server version does not support lstat operation");
657 return(do_stat(conn, path, quiet));
660 id = conn->msg_id++;
661 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
662 strlen(path));
664 return(get_decode_stat(conn, id, quiet));
667 #ifdef notyet
668 Attrib *
669 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
671 u_int id;
673 id = conn->msg_id++;
674 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
675 handle_len);
677 return(get_decode_stat(conn, id, quiet));
679 #endif
682 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
684 u_int status, id;
686 id = conn->msg_id++;
687 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
688 strlen(path), a);
690 status = get_status(conn, id);
691 if (status != SSH2_FX_OK)
692 error("Couldn't setstat on \"%s\": %s", path,
693 fx2txt(status));
695 return(status);
699 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
700 Attrib *a)
702 u_int status, id;
704 id = conn->msg_id++;
705 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
706 handle_len, a);
708 status = get_status(conn, id);
709 if (status != SSH2_FX_OK)
710 error("Couldn't fsetstat: %s", fx2txt(status));
712 return(status);
715 char *
716 do_realpath(struct sftp_conn *conn, char *path)
718 Buffer msg;
719 u_int type, expected_id, count, id;
720 char *filename, *longname;
721 Attrib *a;
723 expected_id = id = conn->msg_id++;
724 send_string_request(conn, id, SSH2_FXP_REALPATH, path,
725 strlen(path));
727 buffer_init(&msg);
729 get_msg(conn, &msg);
730 type = buffer_get_char(&msg);
731 id = buffer_get_int(&msg);
733 if (id != expected_id)
734 fatal("ID mismatch (%u != %u)", id, expected_id);
736 if (type == SSH2_FXP_STATUS) {
737 u_int status = buffer_get_int(&msg);
739 error("Couldn't canonicalise: %s", fx2txt(status));
740 buffer_free(&msg);
741 return NULL;
742 } else if (type != SSH2_FXP_NAME)
743 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
744 SSH2_FXP_NAME, type);
746 count = buffer_get_int(&msg);
747 if (count != 1)
748 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
750 filename = buffer_get_string(&msg, NULL);
751 longname = buffer_get_string(&msg, NULL);
752 a = decode_attrib(&msg);
754 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
756 xfree(longname);
758 buffer_free(&msg);
760 return(filename);
764 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
766 Buffer msg;
767 u_int status, id;
769 buffer_init(&msg);
771 /* Send rename request */
772 id = conn->msg_id++;
773 if ((conn->exts & SFTP_EXT_POSIX_RENAME)) {
774 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
775 buffer_put_int(&msg, id);
776 buffer_put_cstring(&msg, "posix-rename@openssh.com");
777 } else {
778 buffer_put_char(&msg, SSH2_FXP_RENAME);
779 buffer_put_int(&msg, id);
781 buffer_put_cstring(&msg, oldpath);
782 buffer_put_cstring(&msg, newpath);
783 send_msg(conn, &msg);
784 debug3("Sent message %s \"%s\" -> \"%s\"",
785 (conn->exts & SFTP_EXT_POSIX_RENAME) ? "posix-rename@openssh.com" :
786 "SSH2_FXP_RENAME", oldpath, newpath);
787 buffer_free(&msg);
789 status = get_status(conn, id);
790 if (status != SSH2_FX_OK)
791 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
792 newpath, fx2txt(status));
794 return(status);
798 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
800 Buffer msg;
801 u_int status, id;
803 if (conn->version < 3) {
804 error("This server does not support the symlink operation");
805 return(SSH2_FX_OP_UNSUPPORTED);
808 buffer_init(&msg);
810 /* Send symlink request */
811 id = conn->msg_id++;
812 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
813 buffer_put_int(&msg, id);
814 buffer_put_cstring(&msg, oldpath);
815 buffer_put_cstring(&msg, newpath);
816 send_msg(conn, &msg);
817 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
818 newpath);
819 buffer_free(&msg);
821 status = get_status(conn, id);
822 if (status != SSH2_FX_OK)
823 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
824 newpath, fx2txt(status));
826 return(status);
829 #ifdef notyet
830 char *
831 do_readlink(struct sftp_conn *conn, char *path)
833 Buffer msg;
834 u_int type, expected_id, count, id;
835 char *filename, *longname;
836 Attrib *a;
838 expected_id = id = conn->msg_id++;
839 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
841 buffer_init(&msg);
843 get_msg(conn, &msg);
844 type = buffer_get_char(&msg);
845 id = buffer_get_int(&msg);
847 if (id != expected_id)
848 fatal("ID mismatch (%u != %u)", id, expected_id);
850 if (type == SSH2_FXP_STATUS) {
851 u_int status = buffer_get_int(&msg);
853 error("Couldn't readlink: %s", fx2txt(status));
854 return(NULL);
855 } else if (type != SSH2_FXP_NAME)
856 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
857 SSH2_FXP_NAME, type);
859 count = buffer_get_int(&msg);
860 if (count != 1)
861 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
863 filename = buffer_get_string(&msg, NULL);
864 longname = buffer_get_string(&msg, NULL);
865 a = decode_attrib(&msg);
867 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
869 xfree(longname);
871 buffer_free(&msg);
873 return(filename);
875 #endif
878 do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
879 int quiet)
881 Buffer msg;
882 u_int id;
884 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
885 error("Server does not support statvfs@openssh.com extension");
886 return -1;
889 id = conn->msg_id++;
891 buffer_init(&msg);
892 buffer_clear(&msg);
893 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
894 buffer_put_int(&msg, id);
895 buffer_put_cstring(&msg, "statvfs@openssh.com");
896 buffer_put_cstring(&msg, path);
897 send_msg(conn, &msg);
898 buffer_free(&msg);
900 return get_decode_statvfs(conn, st, id, quiet);
903 #ifdef notyet
905 do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
906 struct sftp_statvfs *st, int quiet)
908 Buffer msg;
909 u_int id;
911 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
912 error("Server does not support fstatvfs@openssh.com extension");
913 return -1;
916 id = conn->msg_id++;
918 buffer_init(&msg);
919 buffer_clear(&msg);
920 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
921 buffer_put_int(&msg, id);
922 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
923 buffer_put_string(&msg, handle, handle_len);
924 send_msg(conn, &msg);
925 buffer_free(&msg);
927 return get_decode_statvfs(conn, st, id, quiet);
929 #endif
931 static void
932 send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
933 u_int len, char *handle, u_int handle_len)
935 Buffer msg;
937 buffer_init(&msg);
938 buffer_clear(&msg);
939 buffer_put_char(&msg, SSH2_FXP_READ);
940 buffer_put_int(&msg, id);
941 buffer_put_string(&msg, handle, handle_len);
942 buffer_put_int64(&msg, offset);
943 buffer_put_int(&msg, len);
944 send_msg(conn, &msg);
945 buffer_free(&msg);
949 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
950 Attrib *a, int pflag)
952 Attrib junk;
953 Buffer msg;
954 char *handle;
955 int local_fd, status = 0, write_error;
956 int read_error, write_errno;
957 u_int64_t offset, size;
958 u_int handle_len, mode, type, id, buflen, num_req, max_req;
959 off_t progress_counter;
960 struct request {
961 u_int id;
962 u_int len;
963 u_int64_t offset;
964 TAILQ_ENTRY(request) tq;
966 TAILQ_HEAD(reqhead, request) requests;
967 struct request *req;
969 TAILQ_INIT(&requests);
971 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
972 return -1;
974 /* Do not preserve set[ug]id here, as we do not preserve ownership */
975 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
976 mode = a->perm & 0777;
977 else
978 mode = 0666;
980 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
981 (!S_ISREG(a->perm))) {
982 error("Cannot download non-regular file: %s", remote_path);
983 return(-1);
986 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
987 size = a->size;
988 else
989 size = 0;
991 buflen = conn->transfer_buflen;
992 buffer_init(&msg);
994 /* Send open request */
995 id = conn->msg_id++;
996 buffer_put_char(&msg, SSH2_FXP_OPEN);
997 buffer_put_int(&msg, id);
998 buffer_put_cstring(&msg, remote_path);
999 buffer_put_int(&msg, SSH2_FXF_READ);
1000 attrib_clear(&junk); /* Send empty attributes */
1001 encode_attrib(&msg, &junk);
1002 send_msg(conn, &msg);
1003 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1005 handle = get_handle(conn, id, &handle_len,
1006 "remote open(\"%s\")", remote_path);
1007 if (handle == NULL) {
1008 buffer_free(&msg);
1009 return(-1);
1012 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
1013 mode | S_IWRITE);
1014 if (local_fd == -1) {
1015 error("Couldn't open local file \"%s\" for writing: %s",
1016 local_path, strerror(errno));
1017 do_close(conn, handle, handle_len);
1018 buffer_free(&msg);
1019 xfree(handle);
1020 return(-1);
1023 /* Read from remote and write to local */
1024 write_error = read_error = write_errno = num_req = offset = 0;
1025 max_req = 1;
1026 progress_counter = 0;
1028 if (showprogress && size != 0)
1029 start_progress_meter(remote_path, size, &progress_counter);
1031 while (num_req > 0 || max_req > 0) {
1032 char *data;
1033 u_int len;
1036 * Simulate EOF on interrupt: stop sending new requests and
1037 * allow outstanding requests to drain gracefully
1039 if (interrupted) {
1040 if (num_req == 0) /* If we haven't started yet... */
1041 break;
1042 max_req = 0;
1045 /* Send some more requests */
1046 while (num_req < max_req) {
1047 debug3("Request range %llu -> %llu (%d/%d)",
1048 (unsigned long long)offset,
1049 (unsigned long long)offset + buflen - 1,
1050 num_req, max_req);
1051 req = xmalloc(sizeof(*req));
1052 req->id = conn->msg_id++;
1053 req->len = buflen;
1054 req->offset = offset;
1055 offset += buflen;
1056 num_req++;
1057 TAILQ_INSERT_TAIL(&requests, req, tq);
1058 send_read_request(conn, req->id, req->offset,
1059 req->len, handle, handle_len);
1062 buffer_clear(&msg);
1063 get_msg(conn, &msg);
1064 type = buffer_get_char(&msg);
1065 id = buffer_get_int(&msg);
1066 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1068 /* Find the request in our queue */
1069 for (req = TAILQ_FIRST(&requests);
1070 req != NULL && req->id != id;
1071 req = TAILQ_NEXT(req, tq))
1073 if (req == NULL)
1074 fatal("Unexpected reply %u", id);
1076 switch (type) {
1077 case SSH2_FXP_STATUS:
1078 status = buffer_get_int(&msg);
1079 if (status != SSH2_FX_EOF)
1080 read_error = 1;
1081 max_req = 0;
1082 TAILQ_REMOVE(&requests, req, tq);
1083 xfree(req);
1084 num_req--;
1085 break;
1086 case SSH2_FXP_DATA:
1087 data = buffer_get_string(&msg, &len);
1088 debug3("Received data %llu -> %llu",
1089 (unsigned long long)req->offset,
1090 (unsigned long long)req->offset + len - 1);
1091 if (len > req->len)
1092 fatal("Received more data than asked for "
1093 "%u > %u", len, req->len);
1094 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1095 atomicio(vwrite, local_fd, data, len) != len) &&
1096 !write_error) {
1097 write_errno = errno;
1098 write_error = 1;
1099 max_req = 0;
1101 progress_counter += len;
1102 xfree(data);
1104 if (len == req->len) {
1105 TAILQ_REMOVE(&requests, req, tq);
1106 xfree(req);
1107 num_req--;
1108 } else {
1109 /* Resend the request for the missing data */
1110 debug3("Short data block, re-requesting "
1111 "%llu -> %llu (%2d)",
1112 (unsigned long long)req->offset + len,
1113 (unsigned long long)req->offset +
1114 req->len - 1, num_req);
1115 req->id = conn->msg_id++;
1116 req->len -= len;
1117 req->offset += len;
1118 send_read_request(conn, req->id,
1119 req->offset, req->len, handle, handle_len);
1120 /* Reduce the request size */
1121 if (len < buflen)
1122 buflen = MAX(MIN_READ_SIZE, len);
1124 if (max_req > 0) { /* max_req = 0 iff EOF received */
1125 if (size > 0 && offset > size) {
1126 /* Only one request at a time
1127 * after the expected EOF */
1128 debug3("Finish at %llu (%2d)",
1129 (unsigned long long)offset,
1130 num_req);
1131 max_req = 1;
1132 } else if (max_req <= conn->num_requests) {
1133 ++max_req;
1136 break;
1137 default:
1138 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1139 SSH2_FXP_DATA, type);
1143 if (showprogress && size)
1144 stop_progress_meter();
1146 /* Sanity check */
1147 if (TAILQ_FIRST(&requests) != NULL)
1148 fatal("Transfer complete, but requests still in queue");
1150 if (read_error) {
1151 error("Couldn't read from remote file \"%s\" : %s",
1152 remote_path, fx2txt(status));
1153 do_close(conn, handle, handle_len);
1154 } else if (write_error) {
1155 error("Couldn't write to \"%s\": %s", local_path,
1156 strerror(write_errno));
1157 status = -1;
1158 do_close(conn, handle, handle_len);
1159 } else {
1160 status = do_close(conn, handle, handle_len);
1162 /* Override umask and utimes if asked */
1163 #ifdef HAVE_FCHMOD
1164 if (pflag && fchmod(local_fd, mode) == -1)
1165 #else
1166 if (pflag && chmod(local_path, mode) == -1)
1167 #endif /* HAVE_FCHMOD */
1168 error("Couldn't set mode on \"%s\": %s", local_path,
1169 strerror(errno));
1170 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1171 struct timeval tv[2];
1172 tv[0].tv_sec = a->atime;
1173 tv[1].tv_sec = a->mtime;
1174 tv[0].tv_usec = tv[1].tv_usec = 0;
1175 if (utimes(local_path, tv) == -1)
1176 error("Can't set times on \"%s\": %s",
1177 local_path, strerror(errno));
1180 close(local_fd);
1181 buffer_free(&msg);
1182 xfree(handle);
1184 return(status);
1187 static int
1188 download_dir_internal(struct sftp_conn *conn, char *src, char *dst,
1189 Attrib *dirattrib, int pflag, int printflag, int depth)
1191 int i, ret = 0;
1192 SFTP_DIRENT **dir_entries;
1193 char *filename, *new_src, *new_dst;
1194 mode_t mode = 0777;
1196 if (depth >= MAX_DIR_DEPTH) {
1197 error("Maximum directory depth exceeded: %d levels", depth);
1198 return -1;
1201 if (dirattrib == NULL &&
1202 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1203 error("Unable to stat remote directory \"%s\"", src);
1204 return -1;
1206 if (!S_ISDIR(dirattrib->perm)) {
1207 error("\"%s\" is not a directory", src);
1208 return -1;
1210 if (printflag)
1211 printf("Retrieving %s\n", src);
1213 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1214 mode = dirattrib->perm & 01777;
1215 else {
1216 debug("Server did not send permissions for "
1217 "directory \"%s\"", dst);
1220 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1221 error("mkdir %s: %s", dst, strerror(errno));
1222 return -1;
1225 if (do_readdir(conn, src, &dir_entries) == -1) {
1226 error("%s: Failed to get directory contents", src);
1227 return -1;
1230 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1231 filename = dir_entries[i]->filename;
1233 new_dst = path_append(dst, filename);
1234 new_src = path_append(src, filename);
1236 if (S_ISDIR(dir_entries[i]->a.perm)) {
1237 if (strcmp(filename, ".") == 0 ||
1238 strcmp(filename, "..") == 0)
1239 continue;
1240 if (download_dir_internal(conn, new_src, new_dst,
1241 &(dir_entries[i]->a), pflag, printflag,
1242 depth + 1) == -1)
1243 ret = -1;
1244 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1245 if (do_download(conn, new_src, new_dst,
1246 &(dir_entries[i]->a), pflag) == -1) {
1247 error("Download of file %s to %s failed",
1248 new_src, new_dst);
1249 ret = -1;
1251 } else
1252 logit("%s: not a regular file\n", new_src);
1254 xfree(new_dst);
1255 xfree(new_src);
1258 if (pflag) {
1259 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1260 struct timeval tv[2];
1261 tv[0].tv_sec = dirattrib->atime;
1262 tv[1].tv_sec = dirattrib->mtime;
1263 tv[0].tv_usec = tv[1].tv_usec = 0;
1264 if (utimes(dst, tv) == -1)
1265 error("Can't set times on \"%s\": %s",
1266 dst, strerror(errno));
1267 } else
1268 debug("Server did not send times for directory "
1269 "\"%s\"", dst);
1272 free_sftp_dirents(dir_entries);
1274 return ret;
1278 download_dir(struct sftp_conn *conn, char *src, char *dst,
1279 Attrib *dirattrib, int pflag, int printflag)
1281 char *src_canon;
1282 int ret;
1284 if ((src_canon = do_realpath(conn, src)) == NULL) {
1285 error("Unable to canonicalise path \"%s\"", src);
1286 return -1;
1289 ret = download_dir_internal(conn, src_canon, dst,
1290 dirattrib, pflag, printflag, 0);
1291 xfree(src_canon);
1292 return ret;
1296 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
1297 int pflag)
1299 int local_fd;
1300 int status = SSH2_FX_OK;
1301 u_int handle_len, id, type;
1302 off_t offset;
1303 char *handle, *data;
1304 Buffer msg;
1305 struct stat sb;
1306 Attrib a;
1307 u_int32_t startid;
1308 u_int32_t ackid;
1309 struct outstanding_ack {
1310 u_int id;
1311 u_int len;
1312 off_t offset;
1313 TAILQ_ENTRY(outstanding_ack) tq;
1315 TAILQ_HEAD(ackhead, outstanding_ack) acks;
1316 struct outstanding_ack *ack = NULL;
1318 TAILQ_INIT(&acks);
1320 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1321 error("Couldn't open local file \"%s\" for reading: %s",
1322 local_path, strerror(errno));
1323 return(-1);
1325 if (fstat(local_fd, &sb) == -1) {
1326 error("Couldn't fstat local file \"%s\": %s",
1327 local_path, strerror(errno));
1328 close(local_fd);
1329 return(-1);
1331 if (!S_ISREG(sb.st_mode)) {
1332 error("%s is not a regular file", local_path);
1333 close(local_fd);
1334 return(-1);
1336 stat_to_attrib(&sb, &a);
1338 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1339 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1340 a.perm &= 0777;
1341 if (!pflag)
1342 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1344 buffer_init(&msg);
1346 /* Send open request */
1347 id = conn->msg_id++;
1348 buffer_put_char(&msg, SSH2_FXP_OPEN);
1349 buffer_put_int(&msg, id);
1350 buffer_put_cstring(&msg, remote_path);
1351 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1352 encode_attrib(&msg, &a);
1353 send_msg(conn, &msg);
1354 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1356 buffer_clear(&msg);
1358 handle = get_handle(conn, id, &handle_len,
1359 "remote open(\"%s\")", remote_path);
1360 if (handle == NULL) {
1361 close(local_fd);
1362 buffer_free(&msg);
1363 return -1;
1366 startid = ackid = id + 1;
1367 data = xmalloc(conn->transfer_buflen);
1369 /* Read from local and write to remote */
1370 offset = 0;
1371 if (showprogress)
1372 start_progress_meter(local_path, sb.st_size, &offset);
1374 for (;;) {
1375 int len;
1378 * Can't use atomicio here because it returns 0 on EOF,
1379 * thus losing the last block of the file.
1380 * Simulate an EOF on interrupt, allowing ACKs from the
1381 * server to drain.
1383 if (interrupted || status != SSH2_FX_OK)
1384 len = 0;
1385 else do
1386 len = read(local_fd, data, conn->transfer_buflen);
1387 while ((len == -1) &&
1388 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
1390 if (len == -1)
1391 fatal("Couldn't read from \"%s\": %s", local_path,
1392 strerror(errno));
1394 if (len != 0) {
1395 ack = xmalloc(sizeof(*ack));
1396 ack->id = ++id;
1397 ack->offset = offset;
1398 ack->len = len;
1399 TAILQ_INSERT_TAIL(&acks, ack, tq);
1401 buffer_clear(&msg);
1402 buffer_put_char(&msg, SSH2_FXP_WRITE);
1403 buffer_put_int(&msg, ack->id);
1404 buffer_put_string(&msg, handle, handle_len);
1405 buffer_put_int64(&msg, offset);
1406 buffer_put_string(&msg, data, len);
1407 send_msg(conn, &msg);
1408 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1409 id, (unsigned long long)offset, len);
1410 } else if (TAILQ_FIRST(&acks) == NULL)
1411 break;
1413 if (ack == NULL)
1414 fatal("Unexpected ACK %u", id);
1416 if (id == startid || len == 0 ||
1417 id - ackid >= conn->num_requests) {
1418 u_int r_id;
1420 buffer_clear(&msg);
1421 get_msg(conn, &msg);
1422 type = buffer_get_char(&msg);
1423 r_id = buffer_get_int(&msg);
1425 if (type != SSH2_FXP_STATUS)
1426 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1427 "got %d", SSH2_FXP_STATUS, type);
1429 status = buffer_get_int(&msg);
1430 debug3("SSH2_FXP_STATUS %d", status);
1432 /* Find the request in our queue */
1433 for (ack = TAILQ_FIRST(&acks);
1434 ack != NULL && ack->id != r_id;
1435 ack = TAILQ_NEXT(ack, tq))
1437 if (ack == NULL)
1438 fatal("Can't find request for ID %u", r_id);
1439 TAILQ_REMOVE(&acks, ack, tq);
1440 debug3("In write loop, ack for %u %u bytes at %lld",
1441 ack->id, ack->len, (long long)ack->offset);
1442 ++ackid;
1443 xfree(ack);
1445 offset += len;
1446 if (offset < 0)
1447 fatal("%s: offset < 0", __func__);
1449 buffer_free(&msg);
1451 if (showprogress)
1452 stop_progress_meter();
1453 xfree(data);
1455 if (status != SSH2_FX_OK) {
1456 error("Couldn't write to remote file \"%s\": %s",
1457 remote_path, fx2txt(status));
1458 status = -1;
1461 if (close(local_fd) == -1) {
1462 error("Couldn't close local file \"%s\": %s", local_path,
1463 strerror(errno));
1464 status = -1;
1467 /* Override umask and utimes if asked */
1468 if (pflag)
1469 do_fsetstat(conn, handle, handle_len, &a);
1471 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
1472 status = -1;
1473 xfree(handle);
1475 return status;
1478 static int
1479 upload_dir_internal(struct sftp_conn *conn, char *src, char *dst,
1480 int pflag, int printflag, int depth)
1482 int ret = 0, status;
1483 DIR *dirp;
1484 struct dirent *dp;
1485 char *filename, *new_src, *new_dst;
1486 struct stat sb;
1487 Attrib a;
1489 if (depth >= MAX_DIR_DEPTH) {
1490 error("Maximum directory depth exceeded: %d levels", depth);
1491 return -1;
1494 if (stat(src, &sb) == -1) {
1495 error("Couldn't stat directory \"%s\": %s",
1496 src, strerror(errno));
1497 return -1;
1499 if (!S_ISDIR(sb.st_mode)) {
1500 error("\"%s\" is not a directory", src);
1501 return -1;
1503 if (printflag)
1504 printf("Entering %s\n", src);
1506 attrib_clear(&a);
1507 stat_to_attrib(&sb, &a);
1508 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1509 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1510 a.perm &= 01777;
1511 if (!pflag)
1512 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1514 status = do_mkdir(conn, dst, &a, 0);
1516 * we lack a portable status for errno EEXIST,
1517 * so if we get a SSH2_FX_FAILURE back we must check
1518 * if it was created successfully.
1520 if (status != SSH2_FX_OK) {
1521 if (status != SSH2_FX_FAILURE)
1522 return -1;
1523 if (do_stat(conn, dst, 0) == NULL)
1524 return -1;
1527 if ((dirp = opendir(src)) == NULL) {
1528 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1529 return -1;
1532 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1533 if (dp->d_ino == 0)
1534 continue;
1535 filename = dp->d_name;
1536 new_dst = path_append(dst, filename);
1537 new_src = path_append(src, filename);
1539 if (lstat(new_src, &sb) == -1) {
1540 logit("%s: lstat failed: %s", filename,
1541 strerror(errno));
1542 ret = -1;
1543 } else if (S_ISDIR(sb.st_mode)) {
1544 if (strcmp(filename, ".") == 0 ||
1545 strcmp(filename, "..") == 0)
1546 continue;
1548 if (upload_dir_internal(conn, new_src, new_dst,
1549 pflag, printflag, depth + 1) == -1)
1550 ret = -1;
1551 } else if (S_ISREG(sb.st_mode)) {
1552 if (do_upload(conn, new_src, new_dst, pflag) == -1) {
1553 error("Uploading of file %s to %s failed!",
1554 new_src, new_dst);
1555 ret = -1;
1557 } else
1558 logit("%s: not a regular file\n", filename);
1559 xfree(new_dst);
1560 xfree(new_src);
1563 do_setstat(conn, dst, &a);
1565 (void) closedir(dirp);
1566 return ret;
1570 upload_dir(struct sftp_conn *conn, char *src, char *dst, int printflag,
1571 int pflag)
1573 char *dst_canon;
1574 int ret;
1576 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1577 error("Unable to canonicalise path \"%s\"", dst);
1578 return -1;
1581 ret = upload_dir_internal(conn, src, dst_canon, pflag, printflag, 0);
1582 xfree(dst_canon);
1583 return ret;
1586 char *
1587 path_append(char *p1, char *p2)
1589 char *ret;
1590 size_t len = strlen(p1) + strlen(p2) + 2;
1592 ret = xmalloc(len);
1593 strlcpy(ret, p1, len);
1594 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1595 strlcat(ret, "/", len);
1596 strlcat(ret, p2, len);
1598 return(ret);