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

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