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

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