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

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