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

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