[BACK]Return to sftp-client.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/sftp-client.c, Revision 1.146

1.146   ! djm         1: /* $OpenBSD: sftp-client.c,v 1.145 2021/08/07 00:02:41 djm Exp $ */
1.1       djm         2: /*
1.46      djm         3:  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
1.1       djm         4:  *
1.46      djm         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.
1.1       djm         8:  *
1.46      djm         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.
1.1       djm        16:  */
                     17:
                     18: /* XXX: memleaks */
                     19: /* XXX: signed vs unsigned */
1.23      djm        20: /* XXX: remove all logging, only return status codes */
1.1       djm        21: /* XXX: copy between two remote sites */
                     22:
1.74      deraadt    23: #include <sys/types.h>
1.93      djm        24: #include <sys/poll.h>
1.21      djm        25: #include <sys/queue.h>
1.60      stevesk    26: #include <sys/stat.h>
1.71      stevesk    27: #include <sys/time.h>
1.82      djm        28: #include <sys/statvfs.h>
1.74      deraadt    29: #include <sys/uio.h>
1.66      stevesk    30:
1.89      djm        31: #include <dirent.h>
1.67      stevesk    32: #include <errno.h>
1.66      stevesk    33: #include <fcntl.h>
1.70      stevesk    34: #include <signal.h>
1.87      dtucker    35: #include <stdarg.h>
1.73      stevesk    36: #include <stdio.h>
1.109     dtucker    37: #include <stdlib.h>
1.69      stevesk    38: #include <string.h>
1.68      stevesk    39: #include <unistd.h>
1.1       djm        40:
1.74      deraadt    41: #include "xmalloc.h"
1.116     djm        42: #include "ssherr.h"
                     43: #include "sshbuf.h"
1.1       djm        44: #include "log.h"
                     45: #include "atomicio.h"
1.39      fgsch      46: #include "progressmeter.h"
1.64      djm        47: #include "misc.h"
1.124     schwarze   48: #include "utf8.h"
1.1       djm        49:
                     50: #include "sftp.h"
                     51: #include "sftp-common.h"
                     52: #include "sftp-client.h"
                     53:
1.49      djm        54: extern volatile sig_atomic_t interrupted;
1.39      fgsch      55: extern int showprogress;
                     56:
1.141     djm        57: /* Default size of buffer for up/download */
                     58: #define DEFAULT_COPY_BUFLEN    32768
                     59:
                     60: /* Default number of concurrent outstanding requests */
                     61: #define DEFAULT_NUM_REQUESTS   64
                     62:
1.59      david      63: /* Minimum amount of data to read at a time */
1.21      djm        64: #define MIN_READ_SIZE  512
                     65:
1.89      djm        66: /* Maximum depth to descend in directory trees */
                     67: #define MAX_DIR_DEPTH 64
                     68:
1.23      djm        69: struct sftp_conn {
                     70:        int fd_in;
                     71:        int fd_out;
1.141     djm        72:        u_int download_buflen;
                     73:        u_int upload_buflen;
1.23      djm        74:        u_int num_requests;
                     75:        u_int version;
                     76:        u_int msg_id;
1.82      djm        77: #define SFTP_EXT_POSIX_RENAME  0x00000001
                     78: #define SFTP_EXT_STATVFS       0x00000002
                     79: #define SFTP_EXT_FSTATVFS      0x00000004
1.94      djm        80: #define SFTP_EXT_HARDLINK      0x00000008
1.107     djm        81: #define SFTP_EXT_FSYNC         0x00000010
1.131     djm        82: #define SFTP_EXT_LSETSTAT      0x00000020
1.141     djm        83: #define SFTP_EXT_LIMITS                0x00000040
1.81      djm        84:        u_int exts;
1.93      djm        85:        u_int64_t limit_kbps;
                     86:        struct bwlimit bwlimit_in, bwlimit_out;
1.23      djm        87: };
1.4       djm        88:
1.146   ! djm        89: /* Tracks in-progress requests during file transfers */
        !            90: struct request {
        !            91:        u_int id;
        !            92:        size_t len;
        !            93:        u_int64_t offset;
        !            94:        TAILQ_ENTRY(request) tq;
        !            95: };
        !            96: TAILQ_HEAD(requests, request);
        !            97:
1.116     djm        98: static u_char *
                     99: get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
1.93      djm       100:     const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
                    101:
                    102: /* ARGSUSED */
                    103: static int
                    104: sftpio(void *_bwlimit, size_t amount)
                    105: {
                    106:        struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
                    107:
1.133     dtucker   108:        refresh_progress_meter(0);
1.132     dtucker   109:        if (bwlimit != NULL)
                    110:                bandwidth_limit(bwlimit, amount);
1.93      djm       111:        return 0;
                    112: }
1.88      djm       113:
1.17      itojun    114: static void
1.116     djm       115: send_msg(struct sftp_conn *conn, struct sshbuf *m)
1.1       djm       116: {
1.40      djm       117:        u_char mlen[4];
1.65      djm       118:        struct iovec iov[2];
1.40      djm       119:
1.116     djm       120:        if (sshbuf_len(m) > SFTP_MAX_MSG_LENGTH)
                    121:                fatal("Outbound message too long %zu", sshbuf_len(m));
1.40      djm       122:
                    123:        /* Send length first */
1.116     djm       124:        put_u32(mlen, sshbuf_len(m));
1.65      djm       125:        iov[0].iov_base = mlen;
                    126:        iov[0].iov_len = sizeof(mlen);
1.116     djm       127:        iov[1].iov_base = (u_char *)sshbuf_ptr(m);
                    128:        iov[1].iov_len = sshbuf_len(m);
1.74      deraadt   129:
1.132     dtucker   130:        if (atomiciov6(writev, conn->fd_out, iov, 2, sftpio,
                    131:            conn->limit_kbps > 0 ? &conn->bwlimit_out : NULL) !=
1.116     djm       132:            sshbuf_len(m) + sizeof(mlen))
1.1       djm       133:                fatal("Couldn't send packet: %s", strerror(errno));
                    134:
1.116     djm       135:        sshbuf_reset(m);
1.1       djm       136: }
                    137:
1.17      itojun    138: static void
1.128     dtucker   139: get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
1.1       djm       140: {
1.40      djm       141:        u_int msg_len;
1.116     djm       142:        u_char *p;
                    143:        int r;
1.1       djm       144:
1.144     djm       145:        sshbuf_reset(m);
1.116     djm       146:        if ((r = sshbuf_reserve(m, 4, &p)) != 0)
1.137     djm       147:                fatal_fr(r, "reserve");
1.132     dtucker   148:        if (atomicio6(read, conn->fd_in, p, 4, sftpio,
                    149:            conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL) != 4) {
1.127     djm       150:                if (errno == EPIPE || errno == ECONNRESET)
1.54      avsm      151:                        fatal("Connection closed");
                    152:                else
                    153:                        fatal("Couldn't read packet: %s", strerror(errno));
                    154:        }
1.1       djm       155:
1.116     djm       156:        if ((r = sshbuf_get_u32(m, &msg_len)) != 0)
1.137     djm       157:                fatal_fr(r, "sshbuf_get_u32");
1.128     dtucker   158:        if (msg_len > SFTP_MAX_MSG_LENGTH) {
                    159:                do_log2(initial ? SYSLOG_LEVEL_ERROR : SYSLOG_LEVEL_FATAL,
                    160:                    "Received message too long %u", msg_len);
                    161:                fatal("Ensure the remote shell produces no output "
                    162:                    "for non-interactive sessions.");
                    163:        }
1.1       djm       164:
1.116     djm       165:        if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
1.137     djm       166:                fatal_fr(r, "reserve");
1.132     dtucker   167:        if (atomicio6(read, conn->fd_in, p, msg_len, sftpio,
                    168:            conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL)
1.93      djm       169:            != msg_len) {
1.54      avsm      170:                if (errno == EPIPE)
                    171:                        fatal("Connection closed");
                    172:                else
                    173:                        fatal("Read packet: %s", strerror(errno));
                    174:        }
1.1       djm       175: }
                    176:
1.17      itojun    177: static void
1.128     dtucker   178: get_msg(struct sftp_conn *conn, struct sshbuf *m)
                    179: {
                    180:        get_msg_extended(conn, m, 0);
                    181: }
                    182:
                    183: static void
1.116     djm       184: send_string_request(struct sftp_conn *conn, u_int id, u_int code, const char *s,
1.1       djm       185:     u_int len)
                    186: {
1.116     djm       187:        struct sshbuf *msg;
                    188:        int r;
1.1       djm       189:
1.116     djm       190:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       191:                fatal_f("sshbuf_new failed");
1.116     djm       192:        if ((r = sshbuf_put_u8(msg, code)) != 0 ||
                    193:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                    194:            (r = sshbuf_put_string(msg, s, len)) != 0)
1.137     djm       195:                fatal_fr(r, "compose");
1.116     djm       196:        send_msg(conn, msg);
1.93      djm       197:        debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
1.116     djm       198:        sshbuf_free(msg);
1.1       djm       199: }
                    200:
1.17      itojun    201: static void
1.93      djm       202: send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
1.116     djm       203:     const void *s, u_int len, Attrib *a)
1.1       djm       204: {
1.116     djm       205:        struct sshbuf *msg;
                    206:        int r;
1.1       djm       207:
1.116     djm       208:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       209:                fatal_f("sshbuf_new failed");
1.116     djm       210:        if ((r = sshbuf_put_u8(msg, code)) != 0 ||
                    211:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                    212:            (r = sshbuf_put_string(msg, s, len)) != 0 ||
                    213:            (r = encode_attrib(msg, a)) != 0)
1.137     djm       214:                fatal_fr(r, "compose");
1.116     djm       215:        send_msg(conn, msg);
1.93      djm       216:        debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
1.116     djm       217:        sshbuf_free(msg);
1.1       djm       218: }
                    219:
1.17      itojun    220: static u_int
1.93      djm       221: get_status(struct sftp_conn *conn, u_int expected_id)
1.1       djm       222: {
1.116     djm       223:        struct sshbuf *msg;
                    224:        u_char type;
                    225:        u_int id, status;
                    226:        int r;
1.1       djm       227:
1.116     djm       228:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       229:                fatal_f("sshbuf_new failed");
1.116     djm       230:        get_msg(conn, msg);
                    231:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                    232:            (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm       233:                fatal_fr(r, "compose");
1.1       djm       234:
                    235:        if (id != expected_id)
1.33      deraadt   236:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.1       djm       237:        if (type != SSH2_FXP_STATUS)
1.33      deraadt   238:                fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
1.1       djm       239:                    SSH2_FXP_STATUS, type);
                    240:
1.116     djm       241:        if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm       242:                fatal_fr(r, "parse");
1.116     djm       243:        sshbuf_free(msg);
1.1       djm       244:
1.33      deraadt   245:        debug3("SSH2_FXP_STATUS %u", status);
1.1       djm       246:
1.93      djm       247:        return status;
1.1       djm       248: }
                    249:
1.116     djm       250: static u_char *
                    251: get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
1.93      djm       252:     const char *errfmt, ...)
1.1       djm       253: {
1.116     djm       254:        struct sshbuf *msg;
                    255:        u_int id, status;
                    256:        u_char type;
                    257:        u_char *handle;
                    258:        char errmsg[256];
1.88      djm       259:        va_list args;
1.116     djm       260:        int r;
1.88      djm       261:
                    262:        va_start(args, errfmt);
                    263:        if (errfmt != NULL)
                    264:                vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
                    265:        va_end(args);
1.1       djm       266:
1.116     djm       267:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       268:                fatal_f("sshbuf_new failed");
1.116     djm       269:        get_msg(conn, msg);
                    270:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                    271:            (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm       272:                fatal_fr(r, "parse");
1.1       djm       273:
                    274:        if (id != expected_id)
1.88      djm       275:                fatal("%s: ID mismatch (%u != %u)",
                    276:                    errfmt == NULL ? __func__ : errmsg, id, expected_id);
1.1       djm       277:        if (type == SSH2_FXP_STATUS) {
1.116     djm       278:                if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm       279:                        fatal_fr(r, "parse status");
1.88      djm       280:                if (errfmt != NULL)
                    281:                        error("%s: %s", errmsg, fx2txt(status));
1.116     djm       282:                sshbuf_free(msg);
1.1       djm       283:                return(NULL);
                    284:        } else if (type != SSH2_FXP_HANDLE)
1.88      djm       285:                fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
                    286:                    errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
1.1       djm       287:
1.116     djm       288:        if ((r = sshbuf_get_string(msg, &handle, len)) != 0)
1.137     djm       289:                fatal_fr(r, "parse handle");
1.116     djm       290:        sshbuf_free(msg);
1.1       djm       291:
1.116     djm       292:        return handle;
1.1       djm       293: }
                    294:
1.17      itojun    295: static Attrib *
1.93      djm       296: get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
1.1       djm       297: {
1.116     djm       298:        struct sshbuf *msg;
                    299:        u_int id;
                    300:        u_char type;
                    301:        int r;
                    302:        static Attrib a;
                    303:
                    304:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       305:                fatal_f("sshbuf_new failed");
1.116     djm       306:        get_msg(conn, msg);
                    307:
                    308:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                    309:            (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm       310:                fatal_fr(r, "parse");
1.1       djm       311:
1.33      deraadt   312:        debug3("Received stat reply T:%u I:%u", type, id);
1.1       djm       313:        if (id != expected_id)
1.33      deraadt   314:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.1       djm       315:        if (type == SSH2_FXP_STATUS) {
1.116     djm       316:                u_int status;
1.1       djm       317:
1.116     djm       318:                if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm       319:                        fatal_fr(r, "parse status");
1.14      djm       320:                if (quiet)
                    321:                        debug("Couldn't stat remote file: %s", fx2txt(status));
                    322:                else
                    323:                        error("Couldn't stat remote file: %s", fx2txt(status));
1.116     djm       324:                sshbuf_free(msg);
1.1       djm       325:                return(NULL);
                    326:        } else if (type != SSH2_FXP_ATTRS) {
1.33      deraadt   327:                fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
1.1       djm       328:                    SSH2_FXP_ATTRS, type);
                    329:        }
1.116     djm       330:        if ((r = decode_attrib(msg, &a)) != 0) {
1.137     djm       331:                error_fr(r, "decode_attrib");
1.116     djm       332:                sshbuf_free(msg);
                    333:                return NULL;
                    334:        }
                    335:        sshbuf_free(msg);
1.1       djm       336:
1.116     djm       337:        return &a;
1.1       djm       338: }
                    339:
1.82      djm       340: static int
1.93      djm       341: get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
                    342:     u_int expected_id, int quiet)
1.82      djm       343: {
1.116     djm       344:        struct sshbuf *msg;
                    345:        u_char type;
                    346:        u_int id;
                    347:        u_int64_t flag;
                    348:        int r;
1.82      djm       349:
1.116     djm       350:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       351:                fatal_f("sshbuf_new failed");
1.116     djm       352:        get_msg(conn, msg);
                    353:
                    354:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                    355:            (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm       356:                fatal_fr(r, "parse");
1.82      djm       357:
                    358:        debug3("Received statvfs reply T:%u I:%u", type, id);
                    359:        if (id != expected_id)
                    360:                fatal("ID mismatch (%u != %u)", id, expected_id);
                    361:        if (type == SSH2_FXP_STATUS) {
1.116     djm       362:                u_int status;
1.82      djm       363:
1.116     djm       364:                if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm       365:                        fatal_fr(r, "parse status");
1.82      djm       366:                if (quiet)
                    367:                        debug("Couldn't statvfs: %s", fx2txt(status));
                    368:                else
                    369:                        error("Couldn't statvfs: %s", fx2txt(status));
1.116     djm       370:                sshbuf_free(msg);
1.82      djm       371:                return -1;
                    372:        } else if (type != SSH2_FXP_EXTENDED_REPLY) {
                    373:                fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
                    374:                    SSH2_FXP_EXTENDED_REPLY, type);
                    375:        }
                    376:
1.114     tedu      377:        memset(st, 0, sizeof(*st));
1.116     djm       378:        if ((r = sshbuf_get_u64(msg, &st->f_bsize)) != 0 ||
                    379:            (r = sshbuf_get_u64(msg, &st->f_frsize)) != 0 ||
                    380:            (r = sshbuf_get_u64(msg, &st->f_blocks)) != 0 ||
                    381:            (r = sshbuf_get_u64(msg, &st->f_bfree)) != 0 ||
                    382:            (r = sshbuf_get_u64(msg, &st->f_bavail)) != 0 ||
                    383:            (r = sshbuf_get_u64(msg, &st->f_files)) != 0 ||
                    384:            (r = sshbuf_get_u64(msg, &st->f_ffree)) != 0 ||
                    385:            (r = sshbuf_get_u64(msg, &st->f_favail)) != 0 ||
                    386:            (r = sshbuf_get_u64(msg, &st->f_fsid)) != 0 ||
                    387:            (r = sshbuf_get_u64(msg, &flag)) != 0 ||
                    388:            (r = sshbuf_get_u64(msg, &st->f_namemax)) != 0)
1.137     djm       389:                fatal_fr(r, "parse statvfs");
1.82      djm       390:
                    391:        st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
                    392:        st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
                    393:
1.116     djm       394:        sshbuf_free(msg);
1.82      djm       395:
                    396:        return 0;
                    397: }
                    398:
1.23      djm       399: struct sftp_conn *
1.93      djm       400: do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
                    401:     u_int64_t limit_kbps)
1.1       djm       402: {
1.116     djm       403:        u_char type;
                    404:        struct sshbuf *msg;
1.23      djm       405:        struct sftp_conn *ret;
1.116     djm       406:        int r;
1.1       djm       407:
1.103     djm       408:        ret = xcalloc(1, sizeof(*ret));
                    409:        ret->msg_id = 1;
1.93      djm       410:        ret->fd_in = fd_in;
                    411:        ret->fd_out = fd_out;
1.141     djm       412:        ret->download_buflen = ret->upload_buflen =
                    413:            transfer_buflen ? transfer_buflen : DEFAULT_COPY_BUFLEN;
                    414:        ret->num_requests =
                    415:            num_requests ? num_requests : DEFAULT_NUM_REQUESTS;
1.93      djm       416:        ret->exts = 0;
                    417:        ret->limit_kbps = 0;
                    418:
1.116     djm       419:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       420:                fatal_f("sshbuf_new failed");
1.116     djm       421:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_INIT)) != 0 ||
                    422:            (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0)
1.137     djm       423:                fatal_fr(r, "parse");
                    424:
1.116     djm       425:        send_msg(ret, msg);
1.1       djm       426:
1.128     dtucker   427:        get_msg_extended(ret, msg, 1);
1.1       djm       428:
1.3       stevesk   429:        /* Expecting a VERSION reply */
1.116     djm       430:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
1.137     djm       431:                fatal_fr(r, "parse type");
1.116     djm       432:        if (type != SSH2_FXP_VERSION) {
1.33      deraadt   433:                error("Invalid packet back from SSH2_FXP_INIT (type %u)",
1.1       djm       434:                    type);
1.116     djm       435:                sshbuf_free(msg);
1.119     jsg       436:                free(ret);
1.23      djm       437:                return(NULL);
1.1       djm       438:        }
1.116     djm       439:        if ((r = sshbuf_get_u32(msg, &ret->version)) != 0)
1.137     djm       440:                fatal_fr(r, "parse version");
1.1       djm       441:
1.93      djm       442:        debug2("Remote version: %u", ret->version);
1.1       djm       443:
                    444:        /* Check for extensions */
1.116     djm       445:        while (sshbuf_len(msg) > 0) {
                    446:                char *name;
                    447:                u_char *value;
                    448:                size_t vlen;
1.85      djm       449:                int known = 0;
1.1       djm       450:
1.116     djm       451:                if ((r = sshbuf_get_cstring(msg, &name, NULL)) != 0 ||
                    452:                    (r = sshbuf_get_string(msg, &value, &vlen)) != 0)
1.137     djm       453:                        fatal_fr(r, "parse extension");
1.82      djm       454:                if (strcmp(name, "posix-rename@openssh.com") == 0 &&
1.116     djm       455:                    strcmp((char *)value, "1") == 0) {
1.93      djm       456:                        ret->exts |= SFTP_EXT_POSIX_RENAME;
1.85      djm       457:                        known = 1;
                    458:                } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
1.116     djm       459:                    strcmp((char *)value, "2") == 0) {
1.93      djm       460:                        ret->exts |= SFTP_EXT_STATVFS;
1.85      djm       461:                        known = 1;
1.94      djm       462:                } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
1.116     djm       463:                    strcmp((char *)value, "2") == 0) {
1.93      djm       464:                        ret->exts |= SFTP_EXT_FSTATVFS;
1.85      djm       465:                        known = 1;
1.94      djm       466:                } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
1.116     djm       467:                    strcmp((char *)value, "1") == 0) {
1.94      djm       468:                        ret->exts |= SFTP_EXT_HARDLINK;
                    469:                        known = 1;
1.116     djm       470:                } else if (strcmp(name, "fsync@openssh.com") == 0 &&
                    471:                    strcmp((char *)value, "1") == 0) {
                    472:                        ret->exts |= SFTP_EXT_FSYNC;
                    473:                        known = 1;
1.131     djm       474:                } else if (strcmp(name, "lsetstat@openssh.com") == 0 &&
                    475:                    strcmp((char *)value, "1") == 0) {
                    476:                        ret->exts |= SFTP_EXT_LSETSTAT;
                    477:                        known = 1;
1.141     djm       478:                } else if (strcmp(name, "limits@openssh.com") == 0 &&
                    479:                    strcmp((char *)value, "1") == 0) {
                    480:                        ret->exts |= SFTP_EXT_LIMITS;
                    481:                        known = 1;
1.85      djm       482:                }
                    483:                if (known) {
                    484:                        debug2("Server supports extension \"%s\" revision %s",
                    485:                            name, value);
                    486:                } else {
                    487:                        debug2("Unrecognised server extension \"%s\"", name);
                    488:                }
1.98      djm       489:                free(name);
                    490:                free(value);
1.1       djm       491:        }
                    492:
1.116     djm       493:        sshbuf_free(msg);
1.11      djm       494:
1.141     djm       495:        /* Query the server for its limits */
                    496:        if (ret->exts & SFTP_EXT_LIMITS) {
                    497:                struct sftp_limits limits;
                    498:                if (do_limits(ret, &limits) != 0)
                    499:                        fatal_f("limits failed");
                    500:
                    501:                /* If the caller did not specify, find a good value */
                    502:                if (transfer_buflen == 0) {
                    503:                        ret->download_buflen = limits.read_length;
                    504:                        ret->upload_buflen = limits.write_length;
                    505:                        debug("Using server download size %u", ret->download_buflen);
                    506:                        debug("Using server upload size %u", ret->upload_buflen);
                    507:                }
                    508:
                    509:                /* Use the server limit to scale down our value only */
                    510:                if (num_requests == 0 && limits.open_handles) {
                    511:                        ret->num_requests =
                    512:                            MINIMUM(DEFAULT_NUM_REQUESTS, limits.open_handles);
                    513:                        debug("Server handle limit %llu; using %u",
1.142     djm       514:                            (unsigned long long)limits.open_handles,
                    515:                            ret->num_requests);
1.141     djm       516:                }
                    517:        }
                    518:
1.23      djm       519:        /* Some filexfer v.0 servers don't support large packets */
1.141     djm       520:        if (ret->version == 0) {
                    521:                ret->download_buflen = MINIMUM(ret->download_buflen, 20480);
                    522:                ret->upload_buflen = MINIMUM(ret->upload_buflen, 20480);
                    523:        }
1.23      djm       524:
1.93      djm       525:        ret->limit_kbps = limit_kbps;
                    526:        if (ret->limit_kbps > 0) {
                    527:                bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
1.141     djm       528:                    ret->download_buflen);
1.93      djm       529:                bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
1.141     djm       530:                    ret->upload_buflen);
1.93      djm       531:        }
                    532:
                    533:        return ret;
1.23      djm       534: }
                    535:
                    536: u_int
                    537: sftp_proto_version(struct sftp_conn *conn)
                    538: {
1.93      djm       539:        return conn->version;
1.1       djm       540: }
                    541:
                    542: int
1.141     djm       543: do_limits(struct sftp_conn *conn, struct sftp_limits *limits)
                    544: {
                    545:        u_int id, msg_id;
                    546:        u_char type;
                    547:        struct sshbuf *msg;
                    548:        int r;
                    549:
                    550:        if ((conn->exts & SFTP_EXT_LIMITS) == 0) {
                    551:                error("Server does not support limits@openssh.com extension");
                    552:                return -1;
                    553:        }
                    554:
                    555:        if ((msg = sshbuf_new()) == NULL)
                    556:                fatal_f("sshbuf_new failed");
                    557:
                    558:        id = conn->msg_id++;
                    559:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                    560:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                    561:            (r = sshbuf_put_cstring(msg, "limits@openssh.com")) != 0)
                    562:                fatal_fr(r, "compose");
                    563:        send_msg(conn, msg);
                    564:        debug3("Sent message limits@openssh.com I:%u", id);
                    565:
                    566:        get_msg(conn, msg);
                    567:
                    568:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                    569:            (r = sshbuf_get_u32(msg, &msg_id)) != 0)
                    570:                fatal_fr(r, "parse");
                    571:
                    572:        debug3("Received limits reply T:%u I:%u", type, msg_id);
                    573:        if (id != msg_id)
                    574:                fatal("ID mismatch (%u != %u)", msg_id, id);
                    575:        if (type != SSH2_FXP_EXTENDED_REPLY) {
1.143     djm       576:                debug_f("expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
1.142     djm       577:                    SSH2_FXP_EXTENDED_REPLY, type);
1.143     djm       578:                /* Disable the limits extension */
                    579:                conn->exts &= ~SFTP_EXT_LIMITS;
                    580:                sshbuf_free(msg);
                    581:                return 0;
1.141     djm       582:        }
                    583:
                    584:        memset(limits, 0, sizeof(*limits));
                    585:        if ((r = sshbuf_get_u64(msg, &limits->packet_length)) != 0 ||
                    586:            (r = sshbuf_get_u64(msg, &limits->read_length)) != 0 ||
                    587:            (r = sshbuf_get_u64(msg, &limits->write_length)) != 0 ||
                    588:            (r = sshbuf_get_u64(msg, &limits->open_handles)) != 0)
                    589:                fatal_fr(r, "parse limits");
                    590:
                    591:        sshbuf_free(msg);
                    592:
                    593:        return 0;
                    594: }
                    595:
                    596: int
1.116     djm       597: do_close(struct sftp_conn *conn, const u_char *handle, u_int handle_len)
1.1       djm       598: {
                    599:        u_int id, status;
1.116     djm       600:        struct sshbuf *msg;
                    601:        int r;
1.1       djm       602:
1.116     djm       603:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       604:                fatal_f("sshbuf_new failed");
1.1       djm       605:
1.23      djm       606:        id = conn->msg_id++;
1.116     djm       607:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_CLOSE)) != 0 ||
                    608:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                    609:            (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137     djm       610:                fatal_fr(r, "parse");
1.116     djm       611:        send_msg(conn, msg);
1.33      deraadt   612:        debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
1.1       djm       613:
1.93      djm       614:        status = get_status(conn, id);
1.1       djm       615:        if (status != SSH2_FX_OK)
                    616:                error("Couldn't close file: %s", fx2txt(status));
                    617:
1.116     djm       618:        sshbuf_free(msg);
1.1       djm       619:
1.116     djm       620:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       621: }
                    622:
1.12      djm       623:
1.17      itojun    624: static int
1.116     djm       625: do_lsreaddir(struct sftp_conn *conn, const char *path, int print_flag,
1.12      djm       626:     SFTP_DIRENT ***dir)
1.1       djm       627: {
1.116     djm       628:        struct sshbuf *msg;
                    629:        u_int count, id, i, expected_id, ents = 0;
                    630:        size_t handle_len;
1.123     djm       631:        u_char type, *handle;
1.111     djm       632:        int status = SSH2_FX_FAILURE;
1.116     djm       633:        int r;
1.111     djm       634:
                    635:        if (dir)
                    636:                *dir = NULL;
1.1       djm       637:
1.23      djm       638:        id = conn->msg_id++;
1.1       djm       639:
1.116     djm       640:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       641:                fatal_f("sshbuf_new failed");
1.116     djm       642:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPENDIR)) != 0 ||
                    643:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                    644:            (r = sshbuf_put_cstring(msg, path)) != 0)
1.137     djm       645:                fatal_fr(r, "compose OPENDIR");
1.116     djm       646:        send_msg(conn, msg);
1.1       djm       647:
1.93      djm       648:        handle = get_handle(conn, id, &handle_len,
1.88      djm       649:            "remote readdir(\"%s\")", path);
1.96      markus    650:        if (handle == NULL) {
1.116     djm       651:                sshbuf_free(msg);
1.93      djm       652:                return -1;
1.96      markus    653:        }
1.1       djm       654:
1.12      djm       655:        if (dir) {
                    656:                ents = 0;
1.108     djm       657:                *dir = xcalloc(1, sizeof(**dir));
1.12      djm       658:                (*dir)[0] = NULL;
                    659:        }
                    660:
1.49      djm       661:        for (; !interrupted;) {
1.23      djm       662:                id = expected_id = conn->msg_id++;
1.1       djm       663:
1.33      deraadt   664:                debug3("Sending SSH2_FXP_READDIR I:%u", id);
1.1       djm       665:
1.116     djm       666:                sshbuf_reset(msg);
                    667:                if ((r = sshbuf_put_u8(msg, SSH2_FXP_READDIR)) != 0 ||
                    668:                    (r = sshbuf_put_u32(msg, id)) != 0 ||
                    669:                    (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137     djm       670:                        fatal_fr(r, "compose READDIR");
1.116     djm       671:                send_msg(conn, msg);
                    672:
                    673:                sshbuf_reset(msg);
                    674:
                    675:                get_msg(conn, msg);
                    676:
                    677:                if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                    678:                    (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm       679:                        fatal_fr(r, "parse");
1.1       djm       680:
1.33      deraadt   681:                debug3("Received reply T:%u I:%u", type, id);
1.1       djm       682:
                    683:                if (id != expected_id)
1.33      deraadt   684:                        fatal("ID mismatch (%u != %u)", id, expected_id);
1.1       djm       685:
                    686:                if (type == SSH2_FXP_STATUS) {
1.116     djm       687:                        u_int rstatus;
                    688:
                    689:                        if ((r = sshbuf_get_u32(msg, &rstatus)) != 0)
1.137     djm       690:                                fatal_fr(r, "parse status");
1.116     djm       691:                        debug3("Received SSH2_FXP_STATUS %d", rstatus);
                    692:                        if (rstatus == SSH2_FX_EOF)
1.1       djm       693:                                break;
1.116     djm       694:                        error("Couldn't read directory: %s", fx2txt(rstatus));
1.111     djm       695:                        goto out;
1.1       djm       696:                } else if (type != SSH2_FXP_NAME)
1.33      deraadt   697:                        fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.1       djm       698:                            SSH2_FXP_NAME, type);
                    699:
1.116     djm       700:                if ((r = sshbuf_get_u32(msg, &count)) != 0)
1.137     djm       701:                        fatal_fr(r, "parse count");
1.126     djm       702:                if (count > SSHBUF_SIZE_MAX)
1.137     djm       703:                        fatal_f("nonsensical number of entries");
1.7       markus    704:                if (count == 0)
                    705:                        break;
1.8       stevesk   706:                debug3("Received %d SSH2_FXP_NAME responses", count);
1.19      deraadt   707:                for (i = 0; i < count; i++) {
1.1       djm       708:                        char *filename, *longname;
1.116     djm       709:                        Attrib a;
1.1       djm       710:
1.116     djm       711:                        if ((r = sshbuf_get_cstring(msg, &filename,
                    712:                            NULL)) != 0 ||
                    713:                            (r = sshbuf_get_cstring(msg, &longname,
                    714:                            NULL)) != 0)
1.137     djm       715:                                fatal_fr(r, "parse filenames");
1.116     djm       716:                        if ((r = decode_attrib(msg, &a)) != 0) {
1.137     djm       717:                                error_fr(r, "couldn't decode attrib");
1.116     djm       718:                                free(filename);
                    719:                                free(longname);
1.135     djm       720:                                goto out;
1.116     djm       721:                        }
1.1       djm       722:
1.105     djm       723:                        if (print_flag)
1.124     schwarze  724:                                mprintf("%s\n", longname);
1.12      djm       725:
1.89      djm       726:                        /*
                    727:                         * Directory entries should never contain '/'
                    728:                         * These can be used to attack recursive ops
                    729:                         * (e.g. send '../../../../etc/passwd')
                    730:                         */
                    731:                        if (strchr(filename, '/') != NULL) {
                    732:                                error("Server sent suspect path \"%s\" "
                    733:                                    "during readdir of \"%s\"", filename, path);
1.111     djm       734:                        } else if (dir) {
1.118     deraadt   735:                                *dir = xreallocarray(*dir, ents + 2, sizeof(**dir));
1.108     djm       736:                                (*dir)[ents] = xcalloc(1, sizeof(***dir));
1.12      djm       737:                                (*dir)[ents]->filename = xstrdup(filename);
                    738:                                (*dir)[ents]->longname = xstrdup(longname);
1.116     djm       739:                                memcpy(&(*dir)[ents]->a, &a, sizeof(a));
1.12      djm       740:                                (*dir)[++ents] = NULL;
                    741:                        }
1.98      djm       742:                        free(filename);
                    743:                        free(longname);
1.1       djm       744:                }
                    745:        }
1.111     djm       746:        status = 0;
1.1       djm       747:
1.111     djm       748:  out:
1.116     djm       749:        sshbuf_free(msg);
1.23      djm       750:        do_close(conn, handle, handle_len);
1.98      djm       751:        free(handle);
1.1       djm       752:
1.111     djm       753:        if (status != 0 && dir != NULL) {
                    754:                /* Don't return results on error */
                    755:                free_sftp_dirents(*dir);
                    756:                *dir = NULL;
                    757:        } else if (interrupted && dir != NULL && *dir != NULL) {
                    758:                /* Don't return partial matches on interrupt */
1.49      djm       759:                free_sftp_dirents(*dir);
1.108     djm       760:                *dir = xcalloc(1, sizeof(**dir));
1.49      djm       761:                **dir = NULL;
                    762:        }
                    763:
1.129     djm       764:        return status == SSH2_FX_OK ? 0 : -1;
1.12      djm       765: }
                    766:
                    767: int
1.116     djm       768: do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir)
1.12      djm       769: {
1.23      djm       770:        return(do_lsreaddir(conn, path, 0, dir));
1.12      djm       771: }
                    772:
                    773: void free_sftp_dirents(SFTP_DIRENT **s)
                    774: {
                    775:        int i;
1.19      deraadt   776:
1.111     djm       777:        if (s == NULL)
                    778:                return;
1.19      deraadt   779:        for (i = 0; s[i]; i++) {
1.98      djm       780:                free(s[i]->filename);
                    781:                free(s[i]->longname);
                    782:                free(s[i]);
1.12      djm       783:        }
1.98      djm       784:        free(s);
1.12      djm       785: }
                    786:
                    787: int
1.116     djm       788: do_rm(struct sftp_conn *conn, const char *path)
1.1       djm       789: {
                    790:        u_int status, id;
                    791:
                    792:        debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
                    793:
1.23      djm       794:        id = conn->msg_id++;
1.93      djm       795:        send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
                    796:        status = get_status(conn, id);
1.1       djm       797:        if (status != SSH2_FX_OK)
                    798:                error("Couldn't delete file: %s", fx2txt(status));
1.116     djm       799:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       800: }
                    801:
                    802: int
1.116     djm       803: do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag)
1.1       djm       804: {
                    805:        u_int status, id;
                    806:
1.23      djm       807:        id = conn->msg_id++;
1.93      djm       808:        send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
1.1       djm       809:            strlen(path), a);
                    810:
1.93      djm       811:        status = get_status(conn, id);
1.105     djm       812:        if (status != SSH2_FX_OK && print_flag)
1.1       djm       813:                error("Couldn't create directory: %s", fx2txt(status));
                    814:
1.116     djm       815:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       816: }
                    817:
                    818: int
1.116     djm       819: do_rmdir(struct sftp_conn *conn, const char *path)
1.1       djm       820: {
                    821:        u_int status, id;
                    822:
1.23      djm       823:        id = conn->msg_id++;
1.93      djm       824:        send_string_request(conn, id, SSH2_FXP_RMDIR, path,
1.23      djm       825:            strlen(path));
1.1       djm       826:
1.93      djm       827:        status = get_status(conn, id);
1.1       djm       828:        if (status != SSH2_FX_OK)
                    829:                error("Couldn't remove directory: %s", fx2txt(status));
                    830:
1.116     djm       831:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       832: }
                    833:
                    834: Attrib *
1.116     djm       835: do_stat(struct sftp_conn *conn, const char *path, int quiet)
1.1       djm       836: {
                    837:        u_int id;
                    838:
1.23      djm       839:        id = conn->msg_id++;
                    840:
1.93      djm       841:        send_string_request(conn, id,
1.28      markus    842:            conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
1.23      djm       843:            path, strlen(path));
                    844:
1.93      djm       845:        return(get_decode_stat(conn, id, quiet));
1.1       djm       846: }
                    847:
                    848: Attrib *
1.116     djm       849: do_lstat(struct sftp_conn *conn, const char *path, int quiet)
1.1       djm       850: {
                    851:        u_int id;
                    852:
1.23      djm       853:        if (conn->version == 0) {
                    854:                if (quiet)
                    855:                        debug("Server version does not support lstat operation");
                    856:                else
1.43      itojun    857:                        logit("Server version does not support lstat operation");
1.30      markus    858:                return(do_stat(conn, path, quiet));
1.23      djm       859:        }
                    860:
                    861:        id = conn->msg_id++;
1.93      djm       862:        send_string_request(conn, id, SSH2_FXP_LSTAT, path,
1.23      djm       863:            strlen(path));
                    864:
1.93      djm       865:        return(get_decode_stat(conn, id, quiet));
1.1       djm       866: }
                    867:
1.78      chl       868: #ifdef notyet
1.1       djm       869: Attrib *
1.116     djm       870: do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
                    871:     int quiet)
1.1       djm       872: {
                    873:        u_int id;
                    874:
1.23      djm       875:        id = conn->msg_id++;
1.93      djm       876:        send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
1.23      djm       877:            handle_len);
                    878:
1.93      djm       879:        return(get_decode_stat(conn, id, quiet));
1.1       djm       880: }
1.78      chl       881: #endif
1.1       djm       882:
                    883: int
1.116     djm       884: do_setstat(struct sftp_conn *conn, const char *path, Attrib *a)
1.1       djm       885: {
                    886:        u_int status, id;
                    887:
1.23      djm       888:        id = conn->msg_id++;
1.93      djm       889:        send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
1.1       djm       890:            strlen(path), a);
                    891:
1.93      djm       892:        status = get_status(conn, id);
1.1       djm       893:        if (status != SSH2_FX_OK)
                    894:                error("Couldn't setstat on \"%s\": %s", path,
                    895:                    fx2txt(status));
                    896:
1.116     djm       897:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       898: }
                    899:
                    900: int
1.116     djm       901: do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
1.1       djm       902:     Attrib *a)
                    903: {
                    904:        u_int status, id;
                    905:
1.23      djm       906:        id = conn->msg_id++;
1.93      djm       907:        send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
1.1       djm       908:            handle_len, a);
                    909:
1.93      djm       910:        status = get_status(conn, id);
1.1       djm       911:        if (status != SSH2_FX_OK)
                    912:                error("Couldn't fsetstat: %s", fx2txt(status));
                    913:
1.116     djm       914:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       915: }
                    916:
                    917: char *
1.116     djm       918: do_realpath(struct sftp_conn *conn, const char *path)
1.1       djm       919: {
1.116     djm       920:        struct sshbuf *msg;
                    921:        u_int expected_id, count, id;
1.1       djm       922:        char *filename, *longname;
1.116     djm       923:        Attrib a;
                    924:        u_char type;
                    925:        int r;
1.1       djm       926:
1.23      djm       927:        expected_id = id = conn->msg_id++;
1.93      djm       928:        send_string_request(conn, id, SSH2_FXP_REALPATH, path,
1.23      djm       929:            strlen(path));
1.1       djm       930:
1.116     djm       931:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       932:                fatal_f("sshbuf_new failed");
1.1       djm       933:
1.116     djm       934:        get_msg(conn, msg);
                    935:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                    936:            (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm       937:                fatal_fr(r, "parse");
1.1       djm       938:
                    939:        if (id != expected_id)
1.33      deraadt   940:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.1       djm       941:
                    942:        if (type == SSH2_FXP_STATUS) {
1.116     djm       943:                u_int status;
1.1       djm       944:
1.116     djm       945:                if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm       946:                        fatal_fr(r, "parse status");
1.107     djm       947:                error("Couldn't canonicalize: %s", fx2txt(status));
1.116     djm       948:                sshbuf_free(msg);
1.91      djm       949:                return NULL;
1.1       djm       950:        } else if (type != SSH2_FXP_NAME)
1.33      deraadt   951:                fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.1       djm       952:                    SSH2_FXP_NAME, type);
                    953:
1.116     djm       954:        if ((r = sshbuf_get_u32(msg, &count)) != 0)
1.137     djm       955:                fatal_fr(r, "parse count");
1.1       djm       956:        if (count != 1)
                    957:                fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
                    958:
1.116     djm       959:        if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
                    960:            (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
                    961:            (r = decode_attrib(msg, &a)) != 0)
1.137     djm       962:                fatal_fr(r, "parse filename/attrib");
1.1       djm       963:
1.97      dtucker   964:        debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename,
1.116     djm       965:            (unsigned long)a.size);
1.1       djm       966:
1.98      djm       967:        free(longname);
1.1       djm       968:
1.116     djm       969:        sshbuf_free(msg);
1.1       djm       970:
                    971:        return(filename);
                    972: }
                    973:
                    974: int
1.116     djm       975: do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath,
1.102     djm       976:     int force_legacy)
1.1       djm       977: {
1.116     djm       978:        struct sshbuf *msg;
1.1       djm       979:        u_int status, id;
1.116     djm       980:        int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
1.1       djm       981:
1.116     djm       982:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       983:                fatal_f("sshbuf_new failed");
1.1       djm       984:
                    985:        /* Send rename request */
1.23      djm       986:        id = conn->msg_id++;
1.102     djm       987:        if (use_ext) {
1.116     djm       988:                if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                    989:                    (r = sshbuf_put_u32(msg, id)) != 0 ||
                    990:                    (r = sshbuf_put_cstring(msg,
                    991:                    "posix-rename@openssh.com")) != 0)
1.137     djm       992:                        fatal_fr(r, "compose posix-rename");
1.81      djm       993:        } else {
1.116     djm       994:                if ((r = sshbuf_put_u8(msg, SSH2_FXP_RENAME)) != 0 ||
                    995:                    (r = sshbuf_put_u32(msg, id)) != 0)
1.137     djm       996:                        fatal_fr(r, "compose rename");
1.116     djm       997:        }
                    998:        if ((r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
                    999:            (r = sshbuf_put_cstring(msg, newpath)) != 0)
1.137     djm      1000:                fatal_fr(r, "compose paths");
1.116     djm      1001:        send_msg(conn, msg);
1.81      djm      1002:        debug3("Sent message %s \"%s\" -> \"%s\"",
1.116     djm      1003:            use_ext ? "posix-rename@openssh.com" :
                   1004:            "SSH2_FXP_RENAME", oldpath, newpath);
                   1005:        sshbuf_free(msg);
1.1       djm      1006:
1.93      djm      1007:        status = get_status(conn, id);
1.1       djm      1008:        if (status != SSH2_FX_OK)
1.23      djm      1009:                error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
1.94      djm      1010:                    newpath, fx2txt(status));
                   1011:
1.116     djm      1012:        return status == SSH2_FX_OK ? 0 : -1;
1.94      djm      1013: }
                   1014:
                   1015: int
1.116     djm      1016: do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
1.94      djm      1017: {
1.116     djm      1018:        struct sshbuf *msg;
1.94      djm      1019:        u_int status, id;
1.116     djm      1020:        int r;
1.94      djm      1021:
                   1022:        if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
                   1023:                error("Server does not support hardlink@openssh.com extension");
                   1024:                return -1;
                   1025:        }
                   1026:
1.116     djm      1027:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1028:                fatal_f("sshbuf_new failed");
1.95      markus   1029:
                   1030:        /* Send link request */
                   1031:        id = conn->msg_id++;
1.116     djm      1032:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1033:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1034:            (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
                   1035:            (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
                   1036:            (r = sshbuf_put_cstring(msg, newpath)) != 0)
1.137     djm      1037:                fatal_fr(r, "compose");
1.116     djm      1038:        send_msg(conn, msg);
1.94      djm      1039:        debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
1.142     djm      1040:            oldpath, newpath);
1.116     djm      1041:        sshbuf_free(msg);
1.94      djm      1042:
                   1043:        status = get_status(conn, id);
                   1044:        if (status != SSH2_FX_OK)
                   1045:                error("Couldn't link file \"%s\" to \"%s\": %s", oldpath,
1.23      djm      1046:                    newpath, fx2txt(status));
1.1       djm      1047:
1.116     djm      1048:        return status == SSH2_FX_OK ? 0 : -1;
1.11      djm      1049: }
                   1050:
                   1051: int
1.116     djm      1052: do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
1.11      djm      1053: {
1.116     djm      1054:        struct sshbuf *msg;
1.11      djm      1055:        u_int status, id;
1.116     djm      1056:        int r;
1.11      djm      1057:
1.23      djm      1058:        if (conn->version < 3) {
                   1059:                error("This server does not support the symlink operation");
                   1060:                return(SSH2_FX_OP_UNSUPPORTED);
                   1061:        }
                   1062:
1.116     djm      1063:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1064:                fatal_f("sshbuf_new failed");
1.11      djm      1065:
1.48      djm      1066:        /* Send symlink request */
1.23      djm      1067:        id = conn->msg_id++;
1.116     djm      1068:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_SYMLINK)) != 0 ||
                   1069:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1070:            (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
                   1071:            (r = sshbuf_put_cstring(msg, newpath)) != 0)
1.137     djm      1072:                fatal_fr(r, "compose");
1.116     djm      1073:        send_msg(conn, msg);
1.11      djm      1074:        debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
                   1075:            newpath);
1.116     djm      1076:        sshbuf_free(msg);
1.11      djm      1077:
1.93      djm      1078:        status = get_status(conn, id);
1.11      djm      1079:        if (status != SSH2_FX_OK)
1.36      markus   1080:                error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
1.23      djm      1081:                    newpath, fx2txt(status));
1.11      djm      1082:
1.116     djm      1083:        return status == SSH2_FX_OK ? 0 : -1;
1.11      djm      1084: }
                   1085:
1.107     djm      1086: int
1.116     djm      1087: do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len)
1.107     djm      1088: {
1.116     djm      1089:        struct sshbuf *msg;
1.107     djm      1090:        u_int status, id;
1.116     djm      1091:        int r;
1.107     djm      1092:
                   1093:        /* Silently return if the extension is not supported */
                   1094:        if ((conn->exts & SFTP_EXT_FSYNC) == 0)
                   1095:                return -1;
                   1096:
                   1097:        /* Send fsync request */
1.116     djm      1098:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1099:                fatal_f("sshbuf_new failed");
1.107     djm      1100:        id = conn->msg_id++;
1.116     djm      1101:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1102:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1103:            (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
                   1104:            (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137     djm      1105:                fatal_fr(r, "compose");
1.116     djm      1106:        send_msg(conn, msg);
1.107     djm      1107:        debug3("Sent message fsync@openssh.com I:%u", id);
1.116     djm      1108:        sshbuf_free(msg);
1.107     djm      1109:
                   1110:        status = get_status(conn, id);
                   1111:        if (status != SSH2_FX_OK)
                   1112:                error("Couldn't sync file: %s", fx2txt(status));
                   1113:
1.129     djm      1114:        return status == SSH2_FX_OK ? 0 : -1;
1.107     djm      1115: }
                   1116:
1.78      chl      1117: #ifdef notyet
1.11      djm      1118: char *
1.116     djm      1119: do_readlink(struct sftp_conn *conn, const char *path)
1.11      djm      1120: {
1.116     djm      1121:        struct sshbuf *msg;
                   1122:        u_int expected_id, count, id;
1.11      djm      1123:        char *filename, *longname;
1.116     djm      1124:        Attrib a;
                   1125:        u_char type;
                   1126:        int r;
1.11      djm      1127:
1.23      djm      1128:        expected_id = id = conn->msg_id++;
1.93      djm      1129:        send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
1.11      djm      1130:
1.116     djm      1131:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1132:                fatal_f("sshbuf_new failed");
1.11      djm      1133:
1.116     djm      1134:        get_msg(conn, msg);
                   1135:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   1136:            (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm      1137:                fatal_fr(r, "parse");
1.11      djm      1138:
                   1139:        if (id != expected_id)
1.33      deraadt  1140:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.11      djm      1141:
                   1142:        if (type == SSH2_FXP_STATUS) {
1.116     djm      1143:                u_int status;
1.11      djm      1144:
1.116     djm      1145:                if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm      1146:                        fatal_fr(r, "parse status");
1.11      djm      1147:                error("Couldn't readlink: %s", fx2txt(status));
1.116     djm      1148:                sshbuf_free(msg);
1.11      djm      1149:                return(NULL);
                   1150:        } else if (type != SSH2_FXP_NAME)
1.33      deraadt  1151:                fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.11      djm      1152:                    SSH2_FXP_NAME, type);
                   1153:
1.116     djm      1154:        if ((r = sshbuf_get_u32(msg, &count)) != 0)
1.137     djm      1155:                fatal_fr(r, "parse count");
1.11      djm      1156:        if (count != 1)
                   1157:                fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
                   1158:
1.116     djm      1159:        if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
                   1160:            (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
                   1161:            (r = decode_attrib(msg, &a)) != 0)
1.137     djm      1162:                fatal_fr(r, "parse filenames/attrib");
1.11      djm      1163:
                   1164:        debug3("SSH_FXP_READLINK %s -> %s", path, filename);
                   1165:
1.98      djm      1166:        free(longname);
1.11      djm      1167:
1.116     djm      1168:        sshbuf_free(msg);
1.11      djm      1169:
1.116     djm      1170:        return filename;
1.82      djm      1171: }
                   1172: #endif
                   1173:
                   1174: int
1.84      dtucker  1175: do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
1.82      djm      1176:     int quiet)
                   1177: {
1.116     djm      1178:        struct sshbuf *msg;
1.82      djm      1179:        u_int id;
1.116     djm      1180:        int r;
1.82      djm      1181:
                   1182:        if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
                   1183:                error("Server does not support statvfs@openssh.com extension");
                   1184:                return -1;
                   1185:        }
                   1186:
                   1187:        id = conn->msg_id++;
                   1188:
1.116     djm      1189:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1190:                fatal_f("sshbuf_new failed");
1.116     djm      1191:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1192:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1193:            (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
                   1194:            (r = sshbuf_put_cstring(msg, path)) != 0)
1.137     djm      1195:                fatal_fr(r, "compose");
1.116     djm      1196:        send_msg(conn, msg);
                   1197:        sshbuf_free(msg);
1.82      djm      1198:
1.93      djm      1199:        return get_decode_statvfs(conn, st, id, quiet);
1.82      djm      1200: }
                   1201:
                   1202: #ifdef notyet
                   1203: int
1.116     djm      1204: do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
1.84      dtucker  1205:     struct sftp_statvfs *st, int quiet)
1.82      djm      1206: {
1.116     djm      1207:        struct sshbuf *msg;
1.82      djm      1208:        u_int id;
                   1209:
                   1210:        if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
                   1211:                error("Server does not support fstatvfs@openssh.com extension");
                   1212:                return -1;
                   1213:        }
                   1214:
                   1215:        id = conn->msg_id++;
                   1216:
1.116     djm      1217:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1218:                fatal_f("sshbuf_new failed");
1.116     djm      1219:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1220:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1221:            (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
                   1222:            (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137     djm      1223:                fatal_fr(r, "compose");
1.116     djm      1224:        send_msg(conn, msg);
                   1225:        sshbuf_free(msg);
1.82      djm      1226:
1.93      djm      1227:        return get_decode_statvfs(conn, st, id, quiet);
1.1       djm      1228: }
1.78      chl      1229: #endif
1.1       djm      1230:
1.131     djm      1231: int
                   1232: do_lsetstat(struct sftp_conn *conn, const char *path, Attrib *a)
                   1233: {
                   1234:        struct sshbuf *msg;
                   1235:        u_int status, id;
                   1236:        int r;
                   1237:
                   1238:        if ((conn->exts & SFTP_EXT_LSETSTAT) == 0) {
                   1239:                error("Server does not support lsetstat@openssh.com extension");
                   1240:                return -1;
                   1241:        }
                   1242:
                   1243:        id = conn->msg_id++;
                   1244:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1245:                fatal_f("sshbuf_new failed");
1.131     djm      1246:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1247:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1248:            (r = sshbuf_put_cstring(msg, "lsetstat@openssh.com")) != 0 ||
                   1249:            (r = sshbuf_put_cstring(msg, path)) != 0 ||
                   1250:            (r = encode_attrib(msg, a)) != 0)
1.137     djm      1251:                fatal_fr(r, "compose");
1.131     djm      1252:        send_msg(conn, msg);
                   1253:        sshbuf_free(msg);
                   1254:
                   1255:        status = get_status(conn, id);
                   1256:        if (status != SSH2_FX_OK)
                   1257:                error("Couldn't setstat on \"%s\": %s", path,
                   1258:                    fx2txt(status));
                   1259:
                   1260:        return status == SSH2_FX_OK ? 0 : -1;
                   1261: }
                   1262:
1.21      djm      1263: static void
1.93      djm      1264: send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
1.116     djm      1265:     u_int len, const u_char *handle, u_int handle_len)
1.21      djm      1266: {
1.116     djm      1267:        struct sshbuf *msg;
                   1268:        int r;
1.28      markus   1269:
1.116     djm      1270:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1271:                fatal_f("sshbuf_new failed");
1.116     djm      1272:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_READ)) != 0 ||
                   1273:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1274:            (r = sshbuf_put_string(msg, handle, handle_len)) != 0 ||
                   1275:            (r = sshbuf_put_u64(msg, offset)) != 0 ||
                   1276:            (r = sshbuf_put_u32(msg, len)) != 0)
1.137     djm      1277:                fatal_fr(r, "compose");
1.116     djm      1278:        send_msg(conn, msg);
                   1279:        sshbuf_free(msg);
1.28      markus   1280: }
1.21      djm      1281:
1.144     djm      1282: static int
                   1283: send_open(struct sftp_conn *conn, const char *path, const char *tag,
                   1284:     u_int openmode, Attrib *a, u_char **handlep, size_t *handle_lenp)
                   1285: {
                   1286:        Attrib junk;
                   1287:        u_char *handle;
                   1288:        size_t handle_len;
                   1289:        struct sshbuf *msg;
                   1290:        int r;
                   1291:        u_int id;
                   1292:
                   1293:        *handlep = NULL;
                   1294:        *handle_lenp = 0;
                   1295:
                   1296:        if (a == NULL) {
                   1297:                attrib_clear(&junk); /* Send empty attributes */
                   1298:                a = &junk;
                   1299:        }
                   1300:        /* Send open request */
                   1301:        if ((msg = sshbuf_new()) == NULL)
                   1302:                fatal_f("sshbuf_new failed");
                   1303:        id = conn->msg_id++;
                   1304:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
                   1305:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1306:            (r = sshbuf_put_cstring(msg, path)) != 0 ||
                   1307:            (r = sshbuf_put_u32(msg, openmode)) != 0 ||
                   1308:            (r = encode_attrib(msg, a)) != 0)
                   1309:                fatal_fr(r, "compose %s open", tag);
                   1310:        send_msg(conn, msg);
                   1311:        sshbuf_free(msg);
                   1312:        debug3("Sent %s message SSH2_FXP_OPEN I:%u P:%s M:0x%04x",
                   1313:            tag, id, path, openmode);
                   1314:        if ((handle = get_handle(conn, id, &handle_len,
                   1315:            "%s open(\"%s\")", tag, path)) == NULL)
                   1316:                return -1;
                   1317:        /* success */
                   1318:        *handlep = handle;
                   1319:        *handle_lenp = handle_len;
                   1320:        return 0;
                   1321: }
                   1322:
1.1       djm      1323: int
1.116     djm      1324: do_download(struct sftp_conn *conn, const char *remote_path,
                   1325:     const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
                   1326:     int fsync_flag)
1.1       djm      1327: {
1.116     djm      1328:        struct sshbuf *msg;
                   1329:        u_char *handle;
                   1330:        int local_fd = -1, write_error;
1.134     djm      1331:        int read_error, write_errno, lmodified = 0, reordered = 0, r;
1.101     djm      1332:        u_int64_t offset = 0, size, highwater;
1.116     djm      1333:        u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
1.39      fgsch    1334:        off_t progress_counter;
1.116     djm      1335:        size_t handle_len;
1.101     djm      1336:        struct stat st;
1.146   ! djm      1337:        struct requests requests;
1.21      djm      1338:        struct request *req;
1.116     djm      1339:        u_char type;
1.21      djm      1340:
                   1341:        TAILQ_INIT(&requests);
1.1       djm      1342:
1.89      djm      1343:        if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
                   1344:                return -1;
1.1       djm      1345:
1.86      djm      1346:        /* Do not preserve set[ug]id here, as we do not preserve ownership */
1.1       djm      1347:        if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1.38      djm      1348:                mode = a->perm & 0777;
1.1       djm      1349:        else
                   1350:                mode = 0666;
                   1351:
1.14      djm      1352:        if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1.41      djm      1353:            (!S_ISREG(a->perm))) {
                   1354:                error("Cannot download non-regular file: %s", remote_path);
1.14      djm      1355:                return(-1);
                   1356:        }
                   1357:
1.21      djm      1358:        if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
                   1359:                size = a->size;
                   1360:        else
                   1361:                size = 0;
                   1362:
1.141     djm      1363:        buflen = conn->download_buflen;
1.1       djm      1364:
                   1365:        /* Send open request */
1.144     djm      1366:        if (send_open(conn, remote_path, "remote", SSH2_FXF_READ, NULL,
                   1367:            &handle, &handle_len) != 0)
                   1368:                return -1;
1.1       djm      1369:
1.105     djm      1370:        local_fd = open(local_path,
                   1371:            O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
1.23      djm      1372:        if (local_fd == -1) {
                   1373:                error("Couldn't open local file \"%s\" for writing: %s",
                   1374:                    local_path, strerror(errno));
1.101     djm      1375:                goto fail;
                   1376:        }
                   1377:        offset = highwater = 0;
1.105     djm      1378:        if (resume_flag) {
1.101     djm      1379:                if (fstat(local_fd, &st) == -1) {
                   1380:                        error("Unable to stat local file \"%s\": %s",
                   1381:                            local_path, strerror(errno));
                   1382:                        goto fail;
                   1383:                }
1.113     djm      1384:                if (st.st_size < 0) {
                   1385:                        error("\"%s\" has negative size", local_path);
                   1386:                        goto fail;
                   1387:                }
                   1388:                if ((u_int64_t)st.st_size > size) {
1.101     djm      1389:                        error("Unable to resume download of \"%s\": "
                   1390:                            "local file is larger than remote", local_path);
                   1391:  fail:
                   1392:                        do_close(conn, handle, handle_len);
                   1393:                        free(handle);
1.110     djm      1394:                        if (local_fd != -1)
                   1395:                                close(local_fd);
1.101     djm      1396:                        return -1;
                   1397:                }
                   1398:                offset = highwater = st.st_size;
1.23      djm      1399:        }
                   1400:
1.1       djm      1401:        /* Read from remote and write to local */
1.101     djm      1402:        write_error = read_error = write_errno = num_req = 0;
1.21      djm      1403:        max_req = 1;
1.101     djm      1404:        progress_counter = offset;
1.39      fgsch    1405:
1.47      djm      1406:        if (showprogress && size != 0)
                   1407:                start_progress_meter(remote_path, size, &progress_counter);
1.39      fgsch    1408:
1.144     djm      1409:        if ((msg = sshbuf_new()) == NULL)
                   1410:                fatal_f("sshbuf_new failed");
                   1411:
1.21      djm      1412:        while (num_req > 0 || max_req > 0) {
1.116     djm      1413:                u_char *data;
                   1414:                size_t len;
1.1       djm      1415:
1.49      djm      1416:                /*
1.51      deraadt  1417:                 * Simulate EOF on interrupt: stop sending new requests and
1.49      djm      1418:                 * allow outstanding requests to drain gracefully
                   1419:                 */
                   1420:                if (interrupted) {
                   1421:                        if (num_req == 0) /* If we haven't started yet... */
                   1422:                                break;
                   1423:                        max_req = 0;
                   1424:                }
                   1425:
1.21      djm      1426:                /* Send some more requests */
                   1427:                while (num_req < max_req) {
1.28      markus   1428:                        debug3("Request range %llu -> %llu (%d/%d)",
1.25      itojun   1429:                            (unsigned long long)offset,
                   1430:                            (unsigned long long)offset + buflen - 1,
                   1431:                            num_req, max_req);
1.108     djm      1432:                        req = xcalloc(1, sizeof(*req));
1.23      djm      1433:                        req->id = conn->msg_id++;
1.21      djm      1434:                        req->len = buflen;
                   1435:                        req->offset = offset;
                   1436:                        offset += buflen;
                   1437:                        num_req++;
                   1438:                        TAILQ_INSERT_TAIL(&requests, req, tq);
1.93      djm      1439:                        send_read_request(conn, req->id, req->offset,
1.21      djm      1440:                            req->len, handle, handle_len);
                   1441:                }
1.1       djm      1442:
1.116     djm      1443:                sshbuf_reset(msg);
                   1444:                get_msg(conn, msg);
                   1445:                if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   1446:                    (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm      1447:                        fatal_fr(r, "parse");
1.33      deraadt  1448:                debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1.21      djm      1449:
                   1450:                /* Find the request in our queue */
1.53      deraadt  1451:                for (req = TAILQ_FIRST(&requests);
1.21      djm      1452:                    req != NULL && req->id != id;
                   1453:                    req = TAILQ_NEXT(req, tq))
                   1454:                        ;
                   1455:                if (req == NULL)
                   1456:                        fatal("Unexpected reply %u", id);
                   1457:
                   1458:                switch (type) {
                   1459:                case SSH2_FXP_STATUS:
1.116     djm      1460:                        if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm      1461:                                fatal_fr(r, "parse status");
1.21      djm      1462:                        if (status != SSH2_FX_EOF)
                   1463:                                read_error = 1;
                   1464:                        max_req = 0;
                   1465:                        TAILQ_REMOVE(&requests, req, tq);
1.98      djm      1466:                        free(req);
1.21      djm      1467:                        num_req--;
                   1468:                        break;
                   1469:                case SSH2_FXP_DATA:
1.116     djm      1470:                        if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1.137     djm      1471:                                fatal_fr(r, "parse data");
1.26      itojun   1472:                        debug3("Received data %llu -> %llu",
1.28      markus   1473:                            (unsigned long long)req->offset,
1.26      itojun   1474:                            (unsigned long long)req->offset + len - 1);
1.21      djm      1475:                        if (len > req->len)
                   1476:                                fatal("Received more data than asked for "
1.116     djm      1477:                                    "%zu > %zu", len, req->len);
1.134     djm      1478:                        lmodified = 1;
1.21      djm      1479:                        if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1.44      deraadt  1480:                            atomicio(vwrite, local_fd, data, len) != len) &&
1.21      djm      1481:                            !write_error) {
                   1482:                                write_errno = errno;
                   1483:                                write_error = 1;
                   1484:                                max_req = 0;
                   1485:                        }
1.101     djm      1486:                        else if (!reordered && req->offset <= highwater)
                   1487:                                highwater = req->offset + len;
                   1488:                        else if (!reordered && req->offset > highwater)
                   1489:                                reordered = 1;
1.39      fgsch    1490:                        progress_counter += len;
1.98      djm      1491:                        free(data);
1.1       djm      1492:
1.21      djm      1493:                        if (len == req->len) {
                   1494:                                TAILQ_REMOVE(&requests, req, tq);
1.98      djm      1495:                                free(req);
1.21      djm      1496:                                num_req--;
                   1497:                        } else {
                   1498:                                /* Resend the request for the missing data */
                   1499:                                debug3("Short data block, re-requesting "
1.26      itojun   1500:                                    "%llu -> %llu (%2d)",
1.28      markus   1501:                                    (unsigned long long)req->offset + len,
1.27      itojun   1502:                                    (unsigned long long)req->offset +
                   1503:                                    req->len - 1, num_req);
1.23      djm      1504:                                req->id = conn->msg_id++;
1.21      djm      1505:                                req->len -= len;
                   1506:                                req->offset += len;
1.93      djm      1507:                                send_read_request(conn, req->id,
1.23      djm      1508:                                    req->offset, req->len, handle, handle_len);
1.21      djm      1509:                                /* Reduce the request size */
                   1510:                                if (len < buflen)
1.125     deraadt  1511:                                        buflen = MAXIMUM(MIN_READ_SIZE, len);
1.21      djm      1512:                        }
                   1513:                        if (max_req > 0) { /* max_req = 0 iff EOF received */
                   1514:                                if (size > 0 && offset > size) {
                   1515:                                        /* Only one request at a time
                   1516:                                         * after the expected EOF */
                   1517:                                        debug3("Finish at %llu (%2d)",
1.26      itojun   1518:                                            (unsigned long long)offset,
                   1519:                                            num_req);
1.21      djm      1520:                                        max_req = 1;
1.136     djm      1521:                                } else if (max_req < conn->num_requests) {
1.21      djm      1522:                                        ++max_req;
                   1523:                                }
1.1       djm      1524:                        }
1.21      djm      1525:                        break;
                   1526:                default:
1.33      deraadt  1527:                        fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1.1       djm      1528:                            SSH2_FXP_DATA, type);
                   1529:                }
1.21      djm      1530:        }
1.1       djm      1531:
1.39      fgsch    1532:        if (showprogress && size)
                   1533:                stop_progress_meter();
                   1534:
1.21      djm      1535:        /* Sanity check */
                   1536:        if (TAILQ_FIRST(&requests) != NULL)
                   1537:                fatal("Transfer complete, but requests still in queue");
1.101     djm      1538:        /* Truncate at highest contiguous point to avoid holes on interrupt */
                   1539:        if (read_error || write_error || interrupted) {
1.105     djm      1540:                if (reordered && resume_flag) {
1.101     djm      1541:                        error("Unable to resume download of \"%s\": "
                   1542:                            "server reordered requests", local_path);
                   1543:                }
                   1544:                debug("truncating at %llu", (unsigned long long)highwater);
1.120     djm      1545:                if (ftruncate(local_fd, highwater) == -1)
                   1546:                        error("ftruncate \"%s\": %s", local_path,
                   1547:                            strerror(errno));
1.101     djm      1548:        }
1.21      djm      1549:        if (read_error) {
1.28      markus   1550:                error("Couldn't read from remote file \"%s\" : %s",
1.21      djm      1551:                    remote_path, fx2txt(status));
1.103     djm      1552:                status = -1;
1.23      djm      1553:                do_close(conn, handle, handle_len);
1.21      djm      1554:        } else if (write_error) {
                   1555:                error("Couldn't write to \"%s\": %s", local_path,
                   1556:                    strerror(write_errno));
1.116     djm      1557:                status = SSH2_FX_FAILURE;
1.23      djm      1558:                do_close(conn, handle, handle_len);
1.21      djm      1559:        } else {
1.116     djm      1560:                if (do_close(conn, handle, handle_len) != 0 || interrupted)
                   1561:                        status = SSH2_FX_FAILURE;
                   1562:                else
                   1563:                        status = SSH2_FX_OK;
1.21      djm      1564:                /* Override umask and utimes if asked */
1.105     djm      1565:                if (preserve_flag && fchmod(local_fd, mode) == -1)
1.21      djm      1566:                        error("Couldn't set mode on \"%s\": %s", local_path,
1.37      deraadt  1567:                            strerror(errno));
1.105     djm      1568:                if (preserve_flag &&
                   1569:                    (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1.21      djm      1570:                        struct timeval tv[2];
                   1571:                        tv[0].tv_sec = a->atime;
                   1572:                        tv[1].tv_sec = a->mtime;
                   1573:                        tv[0].tv_usec = tv[1].tv_usec = 0;
                   1574:                        if (utimes(local_path, tv) == -1)
                   1575:                                error("Can't set times on \"%s\": %s",
1.37      deraadt  1576:                                    local_path, strerror(errno));
1.1       djm      1577:                }
1.134     djm      1578:                if (resume_flag && !lmodified)
                   1579:                        logit("File \"%s\" was not modified", local_path);
                   1580:                else if (fsync_flag) {
1.107     djm      1581:                        debug("syncing \"%s\"", local_path);
                   1582:                        if (fsync(local_fd) == -1)
                   1583:                                error("Couldn't sync file \"%s\": %s",
                   1584:                                    local_path, strerror(errno));
                   1585:                }
1.10      djm      1586:        }
1.5       djm      1587:        close(local_fd);
1.116     djm      1588:        sshbuf_free(msg);
1.98      djm      1589:        free(handle);
1.23      djm      1590:
1.129     djm      1591:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm      1592: }
                   1593:
1.89      djm      1594: static int
1.116     djm      1595: download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
                   1596:     int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
                   1597:     int resume_flag, int fsync_flag)
1.89      djm      1598: {
                   1599:        int i, ret = 0;
                   1600:        SFTP_DIRENT **dir_entries;
1.130     djm      1601:        char *filename, *new_src = NULL, *new_dst = NULL;
1.138     dtucker  1602:        mode_t mode = 0777, tmpmode = mode;
1.89      djm      1603:
                   1604:        if (depth >= MAX_DIR_DEPTH) {
                   1605:                error("Maximum directory depth exceeded: %d levels", depth);
                   1606:                return -1;
                   1607:        }
                   1608:
                   1609:        if (dirattrib == NULL &&
                   1610:            (dirattrib = do_stat(conn, src, 1)) == NULL) {
                   1611:                error("Unable to stat remote directory \"%s\"", src);
                   1612:                return -1;
                   1613:        }
                   1614:        if (!S_ISDIR(dirattrib->perm)) {
                   1615:                error("\"%s\" is not a directory", src);
                   1616:                return -1;
                   1617:        }
1.105     djm      1618:        if (print_flag)
1.124     schwarze 1619:                mprintf("Retrieving %s\n", src);
1.89      djm      1620:
1.138     dtucker  1621:        if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
1.89      djm      1622:                mode = dirattrib->perm & 01777;
1.138     dtucker  1623:                tmpmode = mode | (S_IWUSR|S_IXUSR);
                   1624:        } else {
1.89      djm      1625:                debug("Server did not send permissions for "
                   1626:                    "directory \"%s\"", dst);
                   1627:        }
                   1628:
1.138     dtucker  1629:        if (mkdir(dst, tmpmode) == -1 && errno != EEXIST) {
1.89      djm      1630:                error("mkdir %s: %s", dst, strerror(errno));
                   1631:                return -1;
                   1632:        }
                   1633:
                   1634:        if (do_readdir(conn, src, &dir_entries) == -1) {
                   1635:                error("%s: Failed to get directory contents", src);
                   1636:                return -1;
                   1637:        }
                   1638:
                   1639:        for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1.130     djm      1640:                free(new_dst);
                   1641:                free(new_src);
                   1642:
1.89      djm      1643:                filename = dir_entries[i]->filename;
                   1644:                new_dst = path_append(dst, filename);
                   1645:                new_src = path_append(src, filename);
                   1646:
                   1647:                if (S_ISDIR(dir_entries[i]->a.perm)) {
                   1648:                        if (strcmp(filename, ".") == 0 ||
                   1649:                            strcmp(filename, "..") == 0)
                   1650:                                continue;
                   1651:                        if (download_dir_internal(conn, new_src, new_dst,
1.105     djm      1652:                            depth + 1, &(dir_entries[i]->a), preserve_flag,
1.107     djm      1653:                            print_flag, resume_flag, fsync_flag) == -1)
1.89      djm      1654:                                ret = -1;
                   1655:                } else if (S_ISREG(dir_entries[i]->a.perm) ) {
                   1656:                        if (do_download(conn, new_src, new_dst,
1.107     djm      1657:                            &(dir_entries[i]->a), preserve_flag,
                   1658:                            resume_flag, fsync_flag) == -1) {
1.89      djm      1659:                                error("Download of file %s to %s failed",
                   1660:                                    new_src, new_dst);
                   1661:                                ret = -1;
                   1662:                        }
                   1663:                } else
                   1664:                        logit("%s: not a regular file\n", new_src);
                   1665:
                   1666:        }
1.130     djm      1667:        free(new_dst);
                   1668:        free(new_src);
1.89      djm      1669:
1.105     djm      1670:        if (preserve_flag) {
1.89      djm      1671:                if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
                   1672:                        struct timeval tv[2];
                   1673:                        tv[0].tv_sec = dirattrib->atime;
                   1674:                        tv[1].tv_sec = dirattrib->mtime;
                   1675:                        tv[0].tv_usec = tv[1].tv_usec = 0;
                   1676:                        if (utimes(dst, tv) == -1)
                   1677:                                error("Can't set times on \"%s\": %s",
                   1678:                                    dst, strerror(errno));
                   1679:                } else
                   1680:                        debug("Server did not send times for directory "
                   1681:                            "\"%s\"", dst);
                   1682:        }
                   1683:
1.138     dtucker  1684:        if (mode != tmpmode && chmod(dst, mode) == -1)
                   1685:                error("Can't set final mode on \"%s\": %s", dst,
                   1686:                    strerror(errno));
                   1687:
1.89      djm      1688:        free_sftp_dirents(dir_entries);
                   1689:
                   1690:        return ret;
                   1691: }
                   1692:
                   1693: int
1.116     djm      1694: download_dir(struct sftp_conn *conn, const char *src, const char *dst,
                   1695:     Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
                   1696:     int fsync_flag)
1.89      djm      1697: {
                   1698:        char *src_canon;
                   1699:        int ret;
                   1700:
                   1701:        if ((src_canon = do_realpath(conn, src)) == NULL) {
1.107     djm      1702:                error("Unable to canonicalize path \"%s\"", src);
1.89      djm      1703:                return -1;
                   1704:        }
                   1705:
1.105     djm      1706:        ret = download_dir_internal(conn, src_canon, dst, 0,
1.107     djm      1707:            dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag);
1.98      djm      1708:        free(src_canon);
1.89      djm      1709:        return ret;
                   1710: }
                   1711:
1.1       djm      1712: int
1.116     djm      1713: do_upload(struct sftp_conn *conn, const char *local_path,
                   1714:     const char *remote_path, int preserve_flag, int resume, int fsync_flag)
1.1       djm      1715: {
1.116     djm      1716:        int r, local_fd;
                   1717:        u_int status = SSH2_FX_OK;
                   1718:        u_int id;
                   1719:        u_char type;
1.100     dtucker  1720:        off_t offset, progress_counter;
1.116     djm      1721:        u_char *handle, *data;
                   1722:        struct sshbuf *msg;
1.1       djm      1723:        struct stat sb;
1.115     logan    1724:        Attrib a, *c = NULL;
1.21      djm      1725:        u_int32_t startid;
                   1726:        u_int32_t ackid;
1.22      djm      1727:        struct outstanding_ack {
                   1728:                u_int id;
                   1729:                u_int len;
1.77      djm      1730:                off_t offset;
1.28      markus   1731:                TAILQ_ENTRY(outstanding_ack) tq;
1.22      djm      1732:        };
                   1733:        TAILQ_HEAD(ackhead, outstanding_ack) acks;
1.50      pedro    1734:        struct outstanding_ack *ack = NULL;
1.116     djm      1735:        size_t handle_len;
1.22      djm      1736:
                   1737:        TAILQ_INIT(&acks);
1.1       djm      1738:
                   1739:        if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
                   1740:                error("Couldn't open local file \"%s\" for reading: %s",
                   1741:                    local_path, strerror(errno));
                   1742:                return(-1);
                   1743:        }
                   1744:        if (fstat(local_fd, &sb) == -1) {
                   1745:                error("Couldn't fstat local file \"%s\": %s",
                   1746:                    local_path, strerror(errno));
1.41      djm      1747:                close(local_fd);
                   1748:                return(-1);
                   1749:        }
                   1750:        if (!S_ISREG(sb.st_mode)) {
                   1751:                error("%s is not a regular file", local_path);
1.1       djm      1752:                close(local_fd);
                   1753:                return(-1);
                   1754:        }
                   1755:        stat_to_attrib(&sb, &a);
                   1756:
                   1757:        a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
                   1758:        a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
                   1759:        a.perm &= 0777;
1.105     djm      1760:        if (!preserve_flag)
1.1       djm      1761:                a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
                   1762:
1.115     logan    1763:        if (resume) {
                   1764:                /* Get remote file size if it exists */
                   1765:                if ((c = do_stat(conn, remote_path, 0)) == NULL) {
1.122     djm      1766:                        close(local_fd);
1.115     logan    1767:                        return -1;
                   1768:                }
                   1769:
                   1770:                if ((off_t)c->size >= sb.st_size) {
                   1771:                        error("destination file bigger or same size as "
1.142     djm      1772:                            "source file");
1.115     logan    1773:                        close(local_fd);
                   1774:                        return -1;
                   1775:                }
                   1776:
                   1777:                if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
                   1778:                        close(local_fd);
                   1779:                        return -1;
                   1780:                }
                   1781:        }
                   1782:
1.1       djm      1783:        /* Send open request */
1.144     djm      1784:        if (send_open(conn, remote_path, "dest", SSH2_FXF_WRITE|SSH2_FXF_CREAT|
                   1785:            (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC),
                   1786:            &a, &handle, &handle_len) != 0) {
1.1       djm      1787:                close(local_fd);
1.80      djm      1788:                return -1;
1.1       djm      1789:        }
                   1790:
1.144     djm      1791:        id = conn->msg_id;
1.21      djm      1792:        startid = ackid = id + 1;
1.141     djm      1793:        data = xmalloc(conn->upload_buflen);
1.20      djm      1794:
1.1       djm      1795:        /* Read from local and write to remote */
1.115     logan    1796:        offset = progress_counter = (resume ? c->size : 0);
1.39      fgsch    1797:        if (showprogress)
1.100     dtucker  1798:                start_progress_meter(local_path, sb.st_size,
                   1799:                    &progress_counter);
1.39      fgsch    1800:
1.144     djm      1801:        if ((msg = sshbuf_new()) == NULL)
                   1802:                fatal_f("sshbuf_new failed");
1.19      deraadt  1803:        for (;;) {
1.1       djm      1804:                int len;
                   1805:
                   1806:                /*
1.51      deraadt  1807:                 * Can't use atomicio here because it returns 0 on EOF,
1.49      djm      1808:                 * thus losing the last block of the file.
1.51      deraadt  1809:                 * Simulate an EOF on interrupt, allowing ACKs from the
1.49      djm      1810:                 * server to drain.
1.1       djm      1811:                 */
1.80      djm      1812:                if (interrupted || status != SSH2_FX_OK)
1.49      djm      1813:                        len = 0;
                   1814:                else do
1.141     djm      1815:                        len = read(local_fd, data, conn->upload_buflen);
1.1       djm      1816:                while ((len == -1) && (errno == EINTR || errno == EAGAIN));
                   1817:
                   1818:                if (len == -1)
                   1819:                        fatal("Couldn't read from \"%s\": %s", local_path,
                   1820:                            strerror(errno));
1.21      djm      1821:
                   1822:                if (len != 0) {
1.108     djm      1823:                        ack = xcalloc(1, sizeof(*ack));
1.22      djm      1824:                        ack->id = ++id;
                   1825:                        ack->offset = offset;
                   1826:                        ack->len = len;
                   1827:                        TAILQ_INSERT_TAIL(&acks, ack, tq);
                   1828:
1.116     djm      1829:                        sshbuf_reset(msg);
                   1830:                        if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
                   1831:                            (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
                   1832:                            (r = sshbuf_put_string(msg, handle,
                   1833:                            handle_len)) != 0 ||
                   1834:                            (r = sshbuf_put_u64(msg, offset)) != 0 ||
                   1835:                            (r = sshbuf_put_string(msg, data, len)) != 0)
1.137     djm      1836:                                fatal_fr(r, "compose");
1.116     djm      1837:                        send_msg(conn, msg);
1.33      deraadt  1838:                        debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1.37      deraadt  1839:                            id, (unsigned long long)offset, len);
1.22      djm      1840:                } else if (TAILQ_FIRST(&acks) == NULL)
1.1       djm      1841:                        break;
                   1842:
1.22      djm      1843:                if (ack == NULL)
                   1844:                        fatal("Unexpected ACK %u", id);
                   1845:
1.28      markus   1846:                if (id == startid || len == 0 ||
1.23      djm      1847:                    id - ackid >= conn->num_requests) {
1.116     djm      1848:                        u_int rid;
1.31      djm      1849:
1.116     djm      1850:                        sshbuf_reset(msg);
                   1851:                        get_msg(conn, msg);
                   1852:                        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   1853:                            (r = sshbuf_get_u32(msg, &rid)) != 0)
1.137     djm      1854:                                fatal_fr(r, "parse");
1.22      djm      1855:
                   1856:                        if (type != SSH2_FXP_STATUS)
                   1857:                                fatal("Expected SSH2_FXP_STATUS(%d) packet, "
                   1858:                                    "got %d", SSH2_FXP_STATUS, type);
                   1859:
1.116     djm      1860:                        if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm      1861:                                fatal_fr(r, "parse status");
1.116     djm      1862:                        debug3("SSH2_FXP_STATUS %u", status);
1.22      djm      1863:
                   1864:                        /* Find the request in our queue */
1.53      deraadt  1865:                        for (ack = TAILQ_FIRST(&acks);
1.116     djm      1866:                            ack != NULL && ack->id != rid;
1.22      djm      1867:                            ack = TAILQ_NEXT(ack, tq))
                   1868:                                ;
                   1869:                        if (ack == NULL)
1.116     djm      1870:                                fatal("Can't find request for ID %u", rid);
1.22      djm      1871:                        TAILQ_REMOVE(&acks, ack, tq);
1.77      djm      1872:                        debug3("In write loop, ack for %u %u bytes at %lld",
                   1873:                            ack->id, ack->len, (long long)ack->offset);
1.21      djm      1874:                        ++ackid;
1.100     dtucker  1875:                        progress_counter += ack->len;
1.98      djm      1876:                        free(ack);
1.1       djm      1877:                }
                   1878:                offset += len;
1.77      djm      1879:                if (offset < 0)
1.137     djm      1880:                        fatal_f("offset < 0");
1.1       djm      1881:        }
1.116     djm      1882:        sshbuf_free(msg);
1.80      djm      1883:
1.39      fgsch    1884:        if (showprogress)
                   1885:                stop_progress_meter();
1.98      djm      1886:        free(data);
1.1       djm      1887:
1.80      djm      1888:        if (status != SSH2_FX_OK) {
                   1889:                error("Couldn't write to remote file \"%s\": %s",
                   1890:                    remote_path, fx2txt(status));
1.116     djm      1891:                status = SSH2_FX_FAILURE;
1.80      djm      1892:        }
                   1893:
1.1       djm      1894:        if (close(local_fd) == -1) {
                   1895:                error("Couldn't close local file \"%s\": %s", local_path,
                   1896:                    strerror(errno));
1.116     djm      1897:                status = SSH2_FX_FAILURE;
1.1       djm      1898:        }
                   1899:
1.10      djm      1900:        /* Override umask and utimes if asked */
1.105     djm      1901:        if (preserve_flag)
1.23      djm      1902:                do_fsetstat(conn, handle, handle_len, &a);
1.10      djm      1903:
1.107     djm      1904:        if (fsync_flag)
                   1905:                (void)do_fsync(conn, handle, handle_len);
                   1906:
1.121     djm      1907:        if (do_close(conn, handle, handle_len) != 0)
1.116     djm      1908:                status = SSH2_FX_FAILURE;
                   1909:
1.98      djm      1910:        free(handle);
1.5       djm      1911:
1.116     djm      1912:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm      1913: }
1.89      djm      1914:
                   1915: static int
1.116     djm      1916: upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
                   1917:     int depth, int preserve_flag, int print_flag, int resume, int fsync_flag)
1.89      djm      1918: {
1.116     djm      1919:        int ret = 0;
1.89      djm      1920:        DIR *dirp;
                   1921:        struct dirent *dp;
1.130     djm      1922:        char *filename, *new_src = NULL, *new_dst = NULL;
1.89      djm      1923:        struct stat sb;
1.121     djm      1924:        Attrib a, *dirattrib;
1.138     dtucker  1925:        u_int32_t saved_perm;
1.89      djm      1926:
                   1927:        if (depth >= MAX_DIR_DEPTH) {
                   1928:                error("Maximum directory depth exceeded: %d levels", depth);
                   1929:                return -1;
                   1930:        }
                   1931:
                   1932:        if (stat(src, &sb) == -1) {
                   1933:                error("Couldn't stat directory \"%s\": %s",
                   1934:                    src, strerror(errno));
                   1935:                return -1;
                   1936:        }
                   1937:        if (!S_ISDIR(sb.st_mode)) {
                   1938:                error("\"%s\" is not a directory", src);
                   1939:                return -1;
                   1940:        }
1.105     djm      1941:        if (print_flag)
1.124     schwarze 1942:                mprintf("Entering %s\n", src);
1.89      djm      1943:
                   1944:        attrib_clear(&a);
                   1945:        stat_to_attrib(&sb, &a);
                   1946:        a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
                   1947:        a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
                   1948:        a.perm &= 01777;
1.105     djm      1949:        if (!preserve_flag)
1.89      djm      1950:                a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1.101     djm      1951:
1.89      djm      1952:        /*
1.121     djm      1953:         * sftp lacks a portable status value to match errno EEXIST,
                   1954:         * so if we get a failure back then we must check whether
1.138     dtucker  1955:         * the path already existed and is a directory.  Ensure we can
                   1956:         * write to the directory we create for the duration of the transfer.
1.89      djm      1957:         */
1.138     dtucker  1958:        saved_perm = a.perm;
                   1959:        a.perm |= (S_IWUSR|S_IXUSR);
1.121     djm      1960:        if (do_mkdir(conn, dst, &a, 0) != 0) {
                   1961:                if ((dirattrib = do_stat(conn, dst, 0)) == NULL)
1.89      djm      1962:                        return -1;
1.121     djm      1963:                if (!S_ISDIR(dirattrib->perm)) {
                   1964:                        error("\"%s\" exists but is not a directory", dst);
1.89      djm      1965:                        return -1;
1.121     djm      1966:                }
1.89      djm      1967:        }
1.138     dtucker  1968:        a.perm = saved_perm;
1.89      djm      1969:
                   1970:        if ((dirp = opendir(src)) == NULL) {
                   1971:                error("Failed to open dir \"%s\": %s", src, strerror(errno));
                   1972:                return -1;
                   1973:        }
1.101     djm      1974:
1.89      djm      1975:        while (((dp = readdir(dirp)) != NULL) && !interrupted) {
                   1976:                if (dp->d_ino == 0)
                   1977:                        continue;
1.130     djm      1978:                free(new_dst);
                   1979:                free(new_src);
1.89      djm      1980:                filename = dp->d_name;
                   1981:                new_dst = path_append(dst, filename);
                   1982:                new_src = path_append(src, filename);
                   1983:
1.90      dtucker  1984:                if (lstat(new_src, &sb) == -1) {
                   1985:                        logit("%s: lstat failed: %s", filename,
                   1986:                            strerror(errno));
                   1987:                        ret = -1;
                   1988:                } else if (S_ISDIR(sb.st_mode)) {
1.89      djm      1989:                        if (strcmp(filename, ".") == 0 ||
                   1990:                            strcmp(filename, "..") == 0)
                   1991:                                continue;
                   1992:
                   1993:                        if (upload_dir_internal(conn, new_src, new_dst,
1.115     logan    1994:                            depth + 1, preserve_flag, print_flag, resume,
1.107     djm      1995:                            fsync_flag) == -1)
1.89      djm      1996:                                ret = -1;
1.90      dtucker  1997:                } else if (S_ISREG(sb.st_mode)) {
1.105     djm      1998:                        if (do_upload(conn, new_src, new_dst,
1.115     logan    1999:                            preserve_flag, resume, fsync_flag) == -1) {
1.89      djm      2000:                                error("Uploading of file %s to %s failed!",
                   2001:                                    new_src, new_dst);
                   2002:                                ret = -1;
                   2003:                        }
                   2004:                } else
                   2005:                        logit("%s: not a regular file\n", filename);
                   2006:        }
1.130     djm      2007:        free(new_dst);
                   2008:        free(new_src);
1.89      djm      2009:
                   2010:        do_setstat(conn, dst, &a);
                   2011:
                   2012:        (void) closedir(dirp);
                   2013:        return ret;
                   2014: }
                   2015:
                   2016: int
1.116     djm      2017: upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
                   2018:     int preserve_flag, int print_flag, int resume, int fsync_flag)
1.89      djm      2019: {
                   2020:        char *dst_canon;
                   2021:        int ret;
                   2022:
                   2023:        if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1.107     djm      2024:                error("Unable to canonicalize path \"%s\"", dst);
1.89      djm      2025:                return -1;
                   2026:        }
                   2027:
1.106     djm      2028:        ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
1.115     logan    2029:            print_flag, resume, fsync_flag);
1.107     djm      2030:
1.98      djm      2031:        free(dst_canon);
1.145     djm      2032:        return ret;
                   2033: }
                   2034:
                   2035: static void
                   2036: handle_dest_replies(struct sftp_conn *to, const char *to_path, int synchronous,
                   2037:     u_int *nreqsp, u_int *write_errorp)
                   2038: {
                   2039:        struct sshbuf *msg;
                   2040:        u_char type;
                   2041:        u_int id, status;
                   2042:        int r;
                   2043:        struct pollfd pfd;
                   2044:
                   2045:        if ((msg = sshbuf_new()) == NULL)
                   2046:                fatal_f("sshbuf_new failed");
                   2047:
                   2048:        /* Try to eat replies from the upload side */
                   2049:        while (*nreqsp > 0) {
                   2050:                debug3_f("%u outstanding replies", *nreqsp);
                   2051:                if (!synchronous) {
                   2052:                        /* Bail out if no data is ready to be read */
                   2053:                        pfd.fd = to->fd_in;
                   2054:                        pfd.events = POLLIN;
                   2055:                        if ((r = poll(&pfd, 1, 0)) == -1) {
                   2056:                                if (errno == EINTR)
                   2057:                                        break;
                   2058:                                fatal_f("poll: %s", strerror(errno));
                   2059:                        } else if (r == 0)
                   2060:                                break; /* fd not ready */
                   2061:                }
                   2062:                sshbuf_reset(msg);
                   2063:                get_msg(to, msg);
                   2064:
                   2065:                if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   2066:                    (r = sshbuf_get_u32(msg, &id)) != 0)
                   2067:                        fatal_fr(r, "dest parse");
                   2068:                debug3("Received dest reply T:%u I:%u R:%u", type, id, *nreqsp);
                   2069:                if (type != SSH2_FXP_STATUS) {
                   2070:                        fatal_f("Expected SSH2_FXP_STATUS(%d) packet, got %d",
                   2071:                            SSH2_FXP_STATUS, type);
                   2072:                }
                   2073:                if ((r = sshbuf_get_u32(msg, &status)) != 0)
                   2074:                        fatal_fr(r, "parse dest status");
                   2075:                debug3("dest SSH2_FXP_STATUS %u", status);
                   2076:                if (status != SSH2_FX_OK) {
                   2077:                        /* record first error */
                   2078:                        if (*write_errorp == 0)
                   2079:                                *write_errorp = status;
                   2080:                }
                   2081:                /*
                   2082:                 * XXX this doesn't do full reply matching like do_upload and
                   2083:                 * so cannot gracefully truncate terminated uploads at a
                   2084:                 * high-water mark. ATM the only caller of this function (scp)
                   2085:                 * doesn't support transfer resumption, so this doesn't matter
                   2086:                 * a whole lot.
                   2087:                 *
                   2088:                 * To be safe, do_crossload truncates the destination file to
                   2089:                 * zero length on upload failure, since we can't trust the
                   2090:                 * server not to have reordered replies that could have
                   2091:                 * inserted holes where none existed in the source file.
                   2092:                 *
                   2093:                 * XXX we could get a more accutate progress bar if we updated
                   2094:                 * the counter based on the reply from the destination...
                   2095:                 */
                   2096:                (*nreqsp)--;
                   2097:        }
                   2098:        debug3_f("done: %u outstanding replies", *nreqsp);
                   2099: }
                   2100:
                   2101: int
                   2102: do_crossload(struct sftp_conn *from, struct sftp_conn *to,
                   2103:     const char *from_path, const char *to_path,
                   2104:     Attrib *a, int preserve_flag)
                   2105: {
                   2106:        struct sshbuf *msg;
                   2107:        int write_error, read_error, lmodified = 0, r;
                   2108:        u_int64_t offset = 0, size;
                   2109:        u_int id, buflen, num_req, max_req, status = SSH2_FX_OK;
                   2110:        u_int num_upload_req;
                   2111:        off_t progress_counter;
                   2112:        u_char *from_handle, *to_handle;
                   2113:        size_t from_handle_len, to_handle_len;
1.146   ! djm      2114:        struct requests requests;
1.145     djm      2115:        struct request *req;
                   2116:        u_char type;
                   2117:
                   2118:        TAILQ_INIT(&requests);
                   2119:
                   2120:        if (a == NULL && (a = do_stat(from, from_path, 0)) == NULL)
                   2121:                return -1;
                   2122:
                   2123:        if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
                   2124:            (!S_ISREG(a->perm))) {
                   2125:                error("Cannot download non-regular file: %s", from_path);
                   2126:                return(-1);
                   2127:        }
                   2128:        if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
                   2129:                size = a->size;
                   2130:        else
                   2131:                size = 0;
                   2132:
                   2133:        buflen = from->download_buflen;
                   2134:        if (buflen > to->upload_buflen)
                   2135:                buflen = to->upload_buflen;
                   2136:
                   2137:        /* Send open request to read side */
                   2138:        if (send_open(from, from_path, "origin", SSH2_FXF_READ, NULL,
                   2139:            &from_handle, &from_handle_len) != 0)
                   2140:                return -1;
                   2141:
                   2142:        /* Send open request to write side */
                   2143:        a->flags &= ~SSH2_FILEXFER_ATTR_SIZE;
                   2144:        a->flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
                   2145:        a->perm &= 0777;
                   2146:        if (!preserve_flag)
                   2147:                a->flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
                   2148:        if (send_open(to, to_path, "dest",
                   2149:            SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC, a,
                   2150:            &to_handle, &to_handle_len) != 0) {
                   2151:                do_close(from, from_handle, from_handle_len);
                   2152:                return -1;
                   2153:        }
                   2154:
                   2155:        /* Read from remote "from" and write to remote "to" */
                   2156:        offset = 0;
                   2157:        write_error = read_error = num_req = num_upload_req = 0;
                   2158:        max_req = 1;
                   2159:        progress_counter = 0;
                   2160:
                   2161:        if (showprogress && size != 0)
                   2162:                start_progress_meter(from_path, size, &progress_counter);
                   2163:        if ((msg = sshbuf_new()) == NULL)
                   2164:                fatal_f("sshbuf_new failed");
                   2165:        while (num_req > 0 || max_req > 0) {
                   2166:                u_char *data;
                   2167:                size_t len;
                   2168:
                   2169:                /*
                   2170:                 * Simulate EOF on interrupt: stop sending new requests and
                   2171:                 * allow outstanding requests to drain gracefully
                   2172:                 */
                   2173:                if (interrupted) {
                   2174:                        if (num_req == 0) /* If we haven't started yet... */
                   2175:                                break;
                   2176:                        max_req = 0;
                   2177:                }
                   2178:
                   2179:                /* Send some more requests */
                   2180:                while (num_req < max_req) {
                   2181:                        debug3("Request range %llu -> %llu (%d/%d)",
                   2182:                            (unsigned long long)offset,
                   2183:                            (unsigned long long)offset + buflen - 1,
                   2184:                            num_req, max_req);
                   2185:                        req = xcalloc(1, sizeof(*req));
                   2186:                        req->id = from->msg_id++;
                   2187:                        req->len = buflen;
                   2188:                        req->offset = offset;
                   2189:                        offset += buflen;
                   2190:                        num_req++;
                   2191:                        TAILQ_INSERT_TAIL(&requests, req, tq);
                   2192:                        send_read_request(from, req->id, req->offset,
                   2193:                            req->len, from_handle, from_handle_len);
                   2194:                }
                   2195:
                   2196:                /* Try to eat replies from the upload side (nonblocking) */
                   2197:                handle_dest_replies(to, to_path, 0,
                   2198:                    &num_upload_req, &write_error);
                   2199:
                   2200:                sshbuf_reset(msg);
                   2201:                get_msg(from, msg);
                   2202:                if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   2203:                    (r = sshbuf_get_u32(msg, &id)) != 0)
                   2204:                        fatal_fr(r, "parse");
                   2205:                debug3("Received origin reply T:%u I:%u R:%d",
                   2206:                    type, id, max_req);
                   2207:
                   2208:                /* Find the request in our queue */
                   2209:                for (req = TAILQ_FIRST(&requests);
                   2210:                    req != NULL && req->id != id;
                   2211:                    req = TAILQ_NEXT(req, tq))
                   2212:                        ;
                   2213:                if (req == NULL)
                   2214:                        fatal("Unexpected reply %u", id);
                   2215:
                   2216:                switch (type) {
                   2217:                case SSH2_FXP_STATUS:
                   2218:                        if ((r = sshbuf_get_u32(msg, &status)) != 0)
                   2219:                                fatal_fr(r, "parse status");
                   2220:                        if (status != SSH2_FX_EOF)
                   2221:                                read_error = 1;
                   2222:                        max_req = 0;
                   2223:                        TAILQ_REMOVE(&requests, req, tq);
                   2224:                        free(req);
                   2225:                        num_req--;
                   2226:                        break;
                   2227:                case SSH2_FXP_DATA:
                   2228:                        if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
                   2229:                                fatal_fr(r, "parse data");
                   2230:                        debug3("Received data %llu -> %llu",
                   2231:                            (unsigned long long)req->offset,
                   2232:                            (unsigned long long)req->offset + len - 1);
                   2233:                        if (len > req->len)
                   2234:                                fatal("Received more data than asked for "
                   2235:                                    "%zu > %zu", len, req->len);
                   2236:                        lmodified = 1;
                   2237:
                   2238:                        /* Write this chunk out to the destination */
                   2239:                        sshbuf_reset(msg);
                   2240:                        if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
                   2241:                            (r = sshbuf_put_u32(msg, to->msg_id++)) != 0 ||
                   2242:                            (r = sshbuf_put_string(msg, to_handle,
                   2243:                            to_handle_len)) != 0 ||
                   2244:                            (r = sshbuf_put_u64(msg, req->offset)) != 0 ||
                   2245:                            (r = sshbuf_put_string(msg, data, len)) != 0)
                   2246:                                fatal_fr(r, "compose write");
                   2247:                        send_msg(to, msg);
                   2248:                        debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%zu",
                   2249:                            id, (unsigned long long)offset, len);
                   2250:                        num_upload_req++;
                   2251:                        progress_counter += len;
                   2252:                        free(data);
                   2253:
                   2254:                        if (len == req->len) {
                   2255:                                TAILQ_REMOVE(&requests, req, tq);
                   2256:                                free(req);
                   2257:                                num_req--;
                   2258:                        } else {
                   2259:                                /* Resend the request for the missing data */
                   2260:                                debug3("Short data block, re-requesting "
                   2261:                                    "%llu -> %llu (%2d)",
                   2262:                                    (unsigned long long)req->offset + len,
                   2263:                                    (unsigned long long)req->offset +
                   2264:                                    req->len - 1, num_req);
                   2265:                                req->id = from->msg_id++;
                   2266:                                req->len -= len;
                   2267:                                req->offset += len;
                   2268:                                send_read_request(from, req->id,
                   2269:                                    req->offset, req->len,
                   2270:                                    from_handle, from_handle_len);
                   2271:                                /* Reduce the request size */
                   2272:                                if (len < buflen)
                   2273:                                        buflen = MAXIMUM(MIN_READ_SIZE, len);
                   2274:                        }
                   2275:                        if (max_req > 0) { /* max_req = 0 iff EOF received */
                   2276:                                if (size > 0 && offset > size) {
                   2277:                                        /* Only one request at a time
                   2278:                                         * after the expected EOF */
                   2279:                                        debug3("Finish at %llu (%2d)",
                   2280:                                            (unsigned long long)offset,
                   2281:                                            num_req);
                   2282:                                        max_req = 1;
                   2283:                                } else if (max_req < from->num_requests) {
                   2284:                                        ++max_req;
                   2285:                                }
                   2286:                        }
                   2287:                        break;
                   2288:                default:
                   2289:                        fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
                   2290:                            SSH2_FXP_DATA, type);
                   2291:                }
                   2292:        }
                   2293:
                   2294:        if (showprogress && size)
                   2295:                stop_progress_meter();
                   2296:
                   2297:        /* Drain replies from the server (blocking) */
                   2298:        debug3_f("waiting for %u replies from destination", num_upload_req);
                   2299:        handle_dest_replies(to, to_path, 1, &num_upload_req, &write_error);
                   2300:
                   2301:        /* Sanity check */
                   2302:        if (TAILQ_FIRST(&requests) != NULL)
                   2303:                fatal("Transfer complete, but requests still in queue");
                   2304:        /* Truncate at 0 length on interrupt or error to avoid holes at dest */
                   2305:        if (read_error || write_error || interrupted) {
                   2306:                debug("truncating \"%s\" at 0", to_path);
                   2307:                do_close(to, to_handle, to_handle_len);
                   2308:                free(to_handle);
                   2309:                if (send_open(to, to_path, "dest",
                   2310:                    SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC, a,
                   2311:                    &to_handle, &to_handle_len) != 0) {
                   2312:                        error("truncation failed for \"%s\"", to_path);
                   2313:                        to_handle = NULL;
                   2314:                }
                   2315:        }
                   2316:        if (read_error) {
                   2317:                error("Couldn't read from origin file \"%s\" : %s",
                   2318:                    from_path, fx2txt(status));
                   2319:                status = -1;
                   2320:                do_close(from, from_handle, from_handle_len);
                   2321:                if (to_handle != NULL)
                   2322:                        do_close(to, to_handle, to_handle_len);
                   2323:        } else if (write_error) {
                   2324:                error("Couldn't write to \"%s\": %s",
                   2325:                    to_path, fx2txt(write_error));
                   2326:                status = SSH2_FX_FAILURE;
                   2327:                do_close(from, from_handle, from_handle_len);
                   2328:                if (to_handle != NULL)
                   2329:                        do_close(to, to_handle, to_handle_len);
                   2330:        } else {
                   2331:                if (do_close(from, from_handle, from_handle_len) != 0 ||
                   2332:                    interrupted)
                   2333:                        status = -1;
                   2334:                else
                   2335:                        status = SSH2_FX_OK;
                   2336:                if (to_handle != NULL) {
                   2337:                        /* Need to resend utimes after write */
                   2338:                        if (preserve_flag)
                   2339:                                do_fsetstat(to, to_handle, to_handle_len, a);
                   2340:                        do_close(to, to_handle, to_handle_len);
                   2341:                }
                   2342:        }
                   2343:        sshbuf_free(msg);
                   2344:        free(from_handle);
                   2345:        free(to_handle);
                   2346:
                   2347:        return status == SSH2_FX_OK ? 0 : -1;
                   2348: }
                   2349:
                   2350: static int
                   2351: crossload_dir_internal(struct sftp_conn *from, struct sftp_conn *to,
                   2352:     const char *from_path, const char *to_path,
                   2353:     int depth, Attrib *dirattrib, int preserve_flag, int print_flag)
                   2354: {
                   2355:        int i, ret = 0;
                   2356:        SFTP_DIRENT **dir_entries;
                   2357:        char *filename, *new_from_path = NULL, *new_to_path = NULL;
                   2358:        mode_t mode = 0777;
                   2359:
                   2360:        if (depth >= MAX_DIR_DEPTH) {
                   2361:                error("Maximum directory depth exceeded: %d levels", depth);
                   2362:                return -1;
                   2363:        }
                   2364:
                   2365:        if (dirattrib == NULL &&
                   2366:            (dirattrib = do_stat(from, from_path, 1)) == NULL) {
                   2367:                error("Unable to stat remote directory \"%s\"", from_path);
                   2368:                return -1;
                   2369:        }
                   2370:        if (!S_ISDIR(dirattrib->perm)) {
                   2371:                error("\"%s\" is not a directory", from_path);
                   2372:                return -1;
                   2373:        }
                   2374:        if (print_flag)
                   2375:                mprintf("Retrieving %s\n", from_path);
                   2376:
                   2377:        dirattrib->flags &= ~SSH2_FILEXFER_ATTR_SIZE;
                   2378:        dirattrib->flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
                   2379:        if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
                   2380:                mode = dirattrib->perm & 01777;
                   2381:                dirattrib->perm = mode | (S_IWUSR|S_IXUSR); /* temp */
                   2382:        } else {
                   2383:                debug("Server did not send permissions for "
                   2384:                    "directory \"%s\"", to_path);
                   2385:        }
                   2386:        if (do_mkdir(to, to_path, dirattrib, print_flag) != 0)
                   2387:                return -1;
                   2388:
                   2389:        if (do_readdir(from, from_path, &dir_entries) == -1) {
                   2390:                error("%s: Failed to get directory contents", from_path);
                   2391:                return -1;
                   2392:        }
                   2393:
                   2394:        for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
                   2395:                free(new_from_path);
                   2396:                free(new_to_path);
                   2397:
                   2398:                filename = dir_entries[i]->filename;
                   2399:                new_from_path = path_append(from_path, filename);
                   2400:                new_to_path = path_append(to_path, filename);
                   2401:
                   2402:                if (S_ISDIR(dir_entries[i]->a.perm)) {
                   2403:                        if (strcmp(filename, ".") == 0 ||
                   2404:                            strcmp(filename, "..") == 0)
                   2405:                                continue;
                   2406:                        if (crossload_dir_internal(from, to,
                   2407:                            new_from_path, new_to_path,
                   2408:                            depth + 1, &(dir_entries[i]->a), preserve_flag,
                   2409:                            print_flag) == -1)
                   2410:                                ret = -1;
                   2411:                } else if (S_ISREG(dir_entries[i]->a.perm) ) {
                   2412:                        if (do_crossload(from, to, new_from_path, new_to_path,
                   2413:                            &(dir_entries[i]->a), preserve_flag) == -1) {
                   2414:                                error("Transfer of file %s to %s failed",
                   2415:                                    new_from_path, new_to_path);
                   2416:                                ret = -1;
                   2417:                        }
                   2418:                } else
                   2419:                        logit("%s: not a regular file\n", new_from_path);
                   2420:
                   2421:        }
                   2422:        free(new_to_path);
                   2423:        free(new_from_path);
                   2424:
                   2425:        dirattrib->perm = mode; /* original mode */
                   2426:        do_setstat(to, to_path, dirattrib);
                   2427:
                   2428:        free_sftp_dirents(dir_entries);
                   2429:
                   2430:        return ret;
                   2431: }
                   2432:
                   2433: int
                   2434: crossload_dir(struct sftp_conn *from, struct sftp_conn *to,
                   2435:     const char *from_path, const char *to_path,
                   2436:     Attrib *dirattrib, int preserve_flag, int print_flag)
                   2437: {
                   2438:        char *from_path_canon;
                   2439:        int ret;
                   2440:
                   2441:        if ((from_path_canon = do_realpath(from, from_path)) == NULL) {
                   2442:                error("Unable to canonicalize path \"%s\"", from_path);
                   2443:                return -1;
                   2444:        }
                   2445:
                   2446:        ret = crossload_dir_internal(from, to, from_path_canon, to_path, 0,
                   2447:            dirattrib, preserve_flag, print_flag);
                   2448:        free(from_path_canon);
1.89      djm      2449:        return ret;
                   2450: }
                   2451:
                   2452: char *
1.116     djm      2453: path_append(const char *p1, const char *p2)
1.89      djm      2454: {
                   2455:        char *ret;
                   2456:        size_t len = strlen(p1) + strlen(p2) + 2;
                   2457:
                   2458:        ret = xmalloc(len);
                   2459:        strlcpy(ret, p1, len);
                   2460:        if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
                   2461:                strlcat(ret, "/", len);
                   2462:        strlcat(ret, p2, len);
                   2463:
                   2464:        return(ret);
1.139     djm      2465: }
                   2466:
                   2467: char *
                   2468: make_absolute(char *p, const char *pwd)
                   2469: {
                   2470:        char *abs_str;
                   2471:
                   2472:        /* Derelativise */
                   2473:        if (p && !path_absolute(p)) {
                   2474:                abs_str = path_append(pwd, p);
                   2475:                free(p);
                   2476:                return(abs_str);
                   2477:        } else
                   2478:                return(p);
                   2479: }
                   2480:
                   2481: int
                   2482: remote_is_dir(struct sftp_conn *conn, const char *path)
                   2483: {
                   2484:        Attrib *a;
                   2485:
                   2486:        /* XXX: report errors? */
                   2487:        if ((a = do_stat(conn, path, 1)) == NULL)
                   2488:                return(0);
                   2489:        if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
                   2490:                return(0);
                   2491:        return(S_ISDIR(a->perm));
                   2492: }
                   2493:
                   2494:
                   2495: int
                   2496: local_is_dir(const char *path)
                   2497: {
                   2498:        struct stat sb;
                   2499:
                   2500:        /* XXX: report errors? */
                   2501:        if (stat(path, &sb) == -1)
                   2502:                return(0);
                   2503:
                   2504:        return(S_ISDIR(sb.st_mode));
                   2505: }
                   2506:
                   2507: /* Check whether path returned from glob(..., GLOB_MARK, ...) is a directory */
                   2508: int
                   2509: globpath_is_dir(const char *pathname)
                   2510: {
                   2511:        size_t l = strlen(pathname);
                   2512:
                   2513:        return l > 0 && pathname[l - 1] == '/';
1.89      djm      2514: }
                   2515: