[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.151

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