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

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