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

1.134   ! djm         1: /* $OpenBSD: sftp-client.c,v 1.133 2019/01/24 16:52:17 dtucker 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);
                    620:                                sshbuf_free(msg);
                    621:                                return -1;
                    622:                        }
1.1       djm       623:
1.105     djm       624:                        if (print_flag)
1.124     schwarze  625:                                mprintf("%s\n", longname);
1.12      djm       626:
1.89      djm       627:                        /*
                    628:                         * Directory entries should never contain '/'
                    629:                         * These can be used to attack recursive ops
                    630:                         * (e.g. send '../../../../etc/passwd')
                    631:                         */
                    632:                        if (strchr(filename, '/') != NULL) {
                    633:                                error("Server sent suspect path \"%s\" "
                    634:                                    "during readdir of \"%s\"", filename, path);
1.111     djm       635:                        } else if (dir) {
1.118     deraadt   636:                                *dir = xreallocarray(*dir, ents + 2, sizeof(**dir));
1.108     djm       637:                                (*dir)[ents] = xcalloc(1, sizeof(***dir));
1.12      djm       638:                                (*dir)[ents]->filename = xstrdup(filename);
                    639:                                (*dir)[ents]->longname = xstrdup(longname);
1.116     djm       640:                                memcpy(&(*dir)[ents]->a, &a, sizeof(a));
1.12      djm       641:                                (*dir)[++ents] = NULL;
                    642:                        }
1.98      djm       643:                        free(filename);
                    644:                        free(longname);
1.1       djm       645:                }
                    646:        }
1.111     djm       647:        status = 0;
1.1       djm       648:
1.111     djm       649:  out:
1.116     djm       650:        sshbuf_free(msg);
1.23      djm       651:        do_close(conn, handle, handle_len);
1.98      djm       652:        free(handle);
1.1       djm       653:
1.111     djm       654:        if (status != 0 && dir != NULL) {
                    655:                /* Don't return results on error */
                    656:                free_sftp_dirents(*dir);
                    657:                *dir = NULL;
                    658:        } else if (interrupted && dir != NULL && *dir != NULL) {
                    659:                /* Don't return partial matches on interrupt */
1.49      djm       660:                free_sftp_dirents(*dir);
1.108     djm       661:                *dir = xcalloc(1, sizeof(**dir));
1.49      djm       662:                **dir = NULL;
                    663:        }
                    664:
1.129     djm       665:        return status == SSH2_FX_OK ? 0 : -1;
1.12      djm       666: }
                    667:
                    668: int
1.116     djm       669: do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir)
1.12      djm       670: {
1.23      djm       671:        return(do_lsreaddir(conn, path, 0, dir));
1.12      djm       672: }
                    673:
                    674: void free_sftp_dirents(SFTP_DIRENT **s)
                    675: {
                    676:        int i;
1.19      deraadt   677:
1.111     djm       678:        if (s == NULL)
                    679:                return;
1.19      deraadt   680:        for (i = 0; s[i]; i++) {
1.98      djm       681:                free(s[i]->filename);
                    682:                free(s[i]->longname);
                    683:                free(s[i]);
1.12      djm       684:        }
1.98      djm       685:        free(s);
1.12      djm       686: }
                    687:
                    688: int
1.116     djm       689: do_rm(struct sftp_conn *conn, const char *path)
1.1       djm       690: {
                    691:        u_int status, id;
                    692:
                    693:        debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
                    694:
1.23      djm       695:        id = conn->msg_id++;
1.93      djm       696:        send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
                    697:        status = get_status(conn, id);
1.1       djm       698:        if (status != SSH2_FX_OK)
                    699:                error("Couldn't delete file: %s", fx2txt(status));
1.116     djm       700:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       701: }
                    702:
                    703: int
1.116     djm       704: do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag)
1.1       djm       705: {
                    706:        u_int status, id;
                    707:
1.23      djm       708:        id = conn->msg_id++;
1.93      djm       709:        send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
1.1       djm       710:            strlen(path), a);
                    711:
1.93      djm       712:        status = get_status(conn, id);
1.105     djm       713:        if (status != SSH2_FX_OK && print_flag)
1.1       djm       714:                error("Couldn't create directory: %s", fx2txt(status));
                    715:
1.116     djm       716:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       717: }
                    718:
                    719: int
1.116     djm       720: do_rmdir(struct sftp_conn *conn, const char *path)
1.1       djm       721: {
                    722:        u_int status, id;
                    723:
1.23      djm       724:        id = conn->msg_id++;
1.93      djm       725:        send_string_request(conn, id, SSH2_FXP_RMDIR, path,
1.23      djm       726:            strlen(path));
1.1       djm       727:
1.93      djm       728:        status = get_status(conn, id);
1.1       djm       729:        if (status != SSH2_FX_OK)
                    730:                error("Couldn't remove directory: %s", fx2txt(status));
                    731:
1.116     djm       732:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       733: }
                    734:
                    735: Attrib *
1.116     djm       736: do_stat(struct sftp_conn *conn, const char *path, int quiet)
1.1       djm       737: {
                    738:        u_int id;
                    739:
1.23      djm       740:        id = conn->msg_id++;
                    741:
1.93      djm       742:        send_string_request(conn, id,
1.28      markus    743:            conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
1.23      djm       744:            path, strlen(path));
                    745:
1.93      djm       746:        return(get_decode_stat(conn, id, quiet));
1.1       djm       747: }
                    748:
                    749: Attrib *
1.116     djm       750: do_lstat(struct sftp_conn *conn, const char *path, int quiet)
1.1       djm       751: {
                    752:        u_int id;
                    753:
1.23      djm       754:        if (conn->version == 0) {
                    755:                if (quiet)
                    756:                        debug("Server version does not support lstat operation");
                    757:                else
1.43      itojun    758:                        logit("Server version does not support lstat operation");
1.30      markus    759:                return(do_stat(conn, path, quiet));
1.23      djm       760:        }
                    761:
                    762:        id = conn->msg_id++;
1.93      djm       763:        send_string_request(conn, id, SSH2_FXP_LSTAT, path,
1.23      djm       764:            strlen(path));
                    765:
1.93      djm       766:        return(get_decode_stat(conn, id, quiet));
1.1       djm       767: }
                    768:
1.78      chl       769: #ifdef notyet
1.1       djm       770: Attrib *
1.116     djm       771: do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
                    772:     int quiet)
1.1       djm       773: {
                    774:        u_int id;
                    775:
1.23      djm       776:        id = conn->msg_id++;
1.93      djm       777:        send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
1.23      djm       778:            handle_len);
                    779:
1.93      djm       780:        return(get_decode_stat(conn, id, quiet));
1.1       djm       781: }
1.78      chl       782: #endif
1.1       djm       783:
                    784: int
1.116     djm       785: do_setstat(struct sftp_conn *conn, const char *path, Attrib *a)
1.1       djm       786: {
                    787:        u_int status, id;
                    788:
1.23      djm       789:        id = conn->msg_id++;
1.93      djm       790:        send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
1.1       djm       791:            strlen(path), a);
                    792:
1.93      djm       793:        status = get_status(conn, id);
1.1       djm       794:        if (status != SSH2_FX_OK)
                    795:                error("Couldn't setstat on \"%s\": %s", path,
                    796:                    fx2txt(status));
                    797:
1.116     djm       798:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       799: }
                    800:
                    801: int
1.116     djm       802: do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
1.1       djm       803:     Attrib *a)
                    804: {
                    805:        u_int status, id;
                    806:
1.23      djm       807:        id = conn->msg_id++;
1.93      djm       808:        send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
1.1       djm       809:            handle_len, a);
                    810:
1.93      djm       811:        status = get_status(conn, id);
1.1       djm       812:        if (status != SSH2_FX_OK)
                    813:                error("Couldn't fsetstat: %s", fx2txt(status));
                    814:
1.116     djm       815:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       816: }
                    817:
                    818: char *
1.116     djm       819: do_realpath(struct sftp_conn *conn, const char *path)
1.1       djm       820: {
1.116     djm       821:        struct sshbuf *msg;
                    822:        u_int expected_id, count, id;
1.1       djm       823:        char *filename, *longname;
1.116     djm       824:        Attrib a;
                    825:        u_char type;
                    826:        int r;
1.1       djm       827:
1.23      djm       828:        expected_id = id = conn->msg_id++;
1.93      djm       829:        send_string_request(conn, id, SSH2_FXP_REALPATH, path,
1.23      djm       830:            strlen(path));
1.1       djm       831:
1.116     djm       832:        if ((msg = sshbuf_new()) == NULL)
                    833:                fatal("%s: sshbuf_new failed", __func__);
1.1       djm       834:
1.116     djm       835:        get_msg(conn, msg);
                    836:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                    837:            (r = sshbuf_get_u32(msg, &id)) != 0)
                    838:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       djm       839:
                    840:        if (id != expected_id)
1.33      deraadt   841:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.1       djm       842:
                    843:        if (type == SSH2_FXP_STATUS) {
1.116     djm       844:                u_int status;
1.1       djm       845:
1.116     djm       846:                if ((r = sshbuf_get_u32(msg, &status)) != 0)
                    847:                        fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.107     djm       848:                error("Couldn't canonicalize: %s", fx2txt(status));
1.116     djm       849:                sshbuf_free(msg);
1.91      djm       850:                return NULL;
1.1       djm       851:        } else if (type != SSH2_FXP_NAME)
1.33      deraadt   852:                fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.1       djm       853:                    SSH2_FXP_NAME, type);
                    854:
1.116     djm       855:        if ((r = sshbuf_get_u32(msg, &count)) != 0)
                    856:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       djm       857:        if (count != 1)
                    858:                fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
                    859:
1.116     djm       860:        if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
                    861:            (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
                    862:            (r = decode_attrib(msg, &a)) != 0)
                    863:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       djm       864:
1.97      dtucker   865:        debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename,
1.116     djm       866:            (unsigned long)a.size);
1.1       djm       867:
1.98      djm       868:        free(longname);
1.1       djm       869:
1.116     djm       870:        sshbuf_free(msg);
1.1       djm       871:
                    872:        return(filename);
                    873: }
                    874:
                    875: int
1.116     djm       876: do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath,
1.102     djm       877:     int force_legacy)
1.1       djm       878: {
1.116     djm       879:        struct sshbuf *msg;
1.1       djm       880:        u_int status, id;
1.116     djm       881:        int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
1.1       djm       882:
1.116     djm       883:        if ((msg = sshbuf_new()) == NULL)
                    884:                fatal("%s: sshbuf_new failed", __func__);
1.1       djm       885:
                    886:        /* Send rename request */
1.23      djm       887:        id = conn->msg_id++;
1.102     djm       888:        if (use_ext) {
1.116     djm       889:                if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                    890:                    (r = sshbuf_put_u32(msg, id)) != 0 ||
                    891:                    (r = sshbuf_put_cstring(msg,
                    892:                    "posix-rename@openssh.com")) != 0)
                    893:                        fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.81      djm       894:        } else {
1.116     djm       895:                if ((r = sshbuf_put_u8(msg, SSH2_FXP_RENAME)) != 0 ||
                    896:                    (r = sshbuf_put_u32(msg, id)) != 0)
                    897:                        fatal("%s: buffer error: %s", __func__, ssh_err(r));
                    898:        }
                    899:        if ((r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
                    900:            (r = sshbuf_put_cstring(msg, newpath)) != 0)
                    901:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                    902:        send_msg(conn, msg);
1.81      djm       903:        debug3("Sent message %s \"%s\" -> \"%s\"",
1.116     djm       904:            use_ext ? "posix-rename@openssh.com" :
                    905:            "SSH2_FXP_RENAME", oldpath, newpath);
                    906:        sshbuf_free(msg);
1.1       djm       907:
1.93      djm       908:        status = get_status(conn, id);
1.1       djm       909:        if (status != SSH2_FX_OK)
1.23      djm       910:                error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
1.94      djm       911:                    newpath, fx2txt(status));
                    912:
1.116     djm       913:        return status == SSH2_FX_OK ? 0 : -1;
1.94      djm       914: }
                    915:
                    916: int
1.116     djm       917: do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
1.94      djm       918: {
1.116     djm       919:        struct sshbuf *msg;
1.94      djm       920:        u_int status, id;
1.116     djm       921:        int r;
1.94      djm       922:
                    923:        if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
                    924:                error("Server does not support hardlink@openssh.com extension");
                    925:                return -1;
                    926:        }
                    927:
1.116     djm       928:        if ((msg = sshbuf_new()) == NULL)
                    929:                fatal("%s: sshbuf_new failed", __func__);
1.95      markus    930:
                    931:        /* Send link request */
                    932:        id = conn->msg_id++;
1.116     djm       933:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                    934:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                    935:            (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
                    936:            (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
                    937:            (r = sshbuf_put_cstring(msg, newpath)) != 0)
                    938:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                    939:        send_msg(conn, msg);
1.94      djm       940:        debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
                    941:               oldpath, newpath);
1.116     djm       942:        sshbuf_free(msg);
1.94      djm       943:
                    944:        status = get_status(conn, id);
                    945:        if (status != SSH2_FX_OK)
                    946:                error("Couldn't link file \"%s\" to \"%s\": %s", oldpath,
1.23      djm       947:                    newpath, fx2txt(status));
1.1       djm       948:
1.116     djm       949:        return status == SSH2_FX_OK ? 0 : -1;
1.11      djm       950: }
                    951:
                    952: int
1.116     djm       953: do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
1.11      djm       954: {
1.116     djm       955:        struct sshbuf *msg;
1.11      djm       956:        u_int status, id;
1.116     djm       957:        int r;
1.11      djm       958:
1.23      djm       959:        if (conn->version < 3) {
                    960:                error("This server does not support the symlink operation");
                    961:                return(SSH2_FX_OP_UNSUPPORTED);
                    962:        }
                    963:
1.116     djm       964:        if ((msg = sshbuf_new()) == NULL)
                    965:                fatal("%s: sshbuf_new failed", __func__);
1.11      djm       966:
1.48      djm       967:        /* Send symlink request */
1.23      djm       968:        id = conn->msg_id++;
1.116     djm       969:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_SYMLINK)) != 0 ||
                    970:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                    971:            (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
                    972:            (r = sshbuf_put_cstring(msg, newpath)) != 0)
                    973:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                    974:        send_msg(conn, msg);
1.11      djm       975:        debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
                    976:            newpath);
1.116     djm       977:        sshbuf_free(msg);
1.11      djm       978:
1.93      djm       979:        status = get_status(conn, id);
1.11      djm       980:        if (status != SSH2_FX_OK)
1.36      markus    981:                error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
1.23      djm       982:                    newpath, fx2txt(status));
1.11      djm       983:
1.116     djm       984:        return status == SSH2_FX_OK ? 0 : -1;
1.11      djm       985: }
                    986:
1.107     djm       987: int
1.116     djm       988: do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len)
1.107     djm       989: {
1.116     djm       990:        struct sshbuf *msg;
1.107     djm       991:        u_int status, id;
1.116     djm       992:        int r;
1.107     djm       993:
                    994:        /* Silently return if the extension is not supported */
                    995:        if ((conn->exts & SFTP_EXT_FSYNC) == 0)
                    996:                return -1;
                    997:
                    998:        /* Send fsync request */
1.116     djm       999:        if ((msg = sshbuf_new()) == NULL)
                   1000:                fatal("%s: sshbuf_new failed", __func__);
1.107     djm      1001:        id = conn->msg_id++;
1.116     djm      1002:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1003:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1004:            (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
                   1005:            (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
                   1006:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                   1007:        send_msg(conn, msg);
1.107     djm      1008:        debug3("Sent message fsync@openssh.com I:%u", id);
1.116     djm      1009:        sshbuf_free(msg);
1.107     djm      1010:
                   1011:        status = get_status(conn, id);
                   1012:        if (status != SSH2_FX_OK)
                   1013:                error("Couldn't sync file: %s", fx2txt(status));
                   1014:
1.129     djm      1015:        return status == SSH2_FX_OK ? 0 : -1;
1.107     djm      1016: }
                   1017:
1.78      chl      1018: #ifdef notyet
1.11      djm      1019: char *
1.116     djm      1020: do_readlink(struct sftp_conn *conn, const char *path)
1.11      djm      1021: {
1.116     djm      1022:        struct sshbuf *msg;
                   1023:        u_int expected_id, count, id;
1.11      djm      1024:        char *filename, *longname;
1.116     djm      1025:        Attrib a;
                   1026:        u_char type;
                   1027:        int r;
1.11      djm      1028:
1.23      djm      1029:        expected_id = id = conn->msg_id++;
1.93      djm      1030:        send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
1.11      djm      1031:
1.116     djm      1032:        if ((msg = sshbuf_new()) == NULL)
                   1033:                fatal("%s: sshbuf_new failed", __func__);
1.11      djm      1034:
1.116     djm      1035:        get_msg(conn, msg);
                   1036:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   1037:            (r = sshbuf_get_u32(msg, &id)) != 0)
                   1038:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.11      djm      1039:
                   1040:        if (id != expected_id)
1.33      deraadt  1041:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.11      djm      1042:
                   1043:        if (type == SSH2_FXP_STATUS) {
1.116     djm      1044:                u_int status;
1.11      djm      1045:
1.116     djm      1046:                if ((r = sshbuf_get_u32(msg, &status)) != 0)
                   1047:                        fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.11      djm      1048:                error("Couldn't readlink: %s", fx2txt(status));
1.116     djm      1049:                sshbuf_free(msg);
1.11      djm      1050:                return(NULL);
                   1051:        } else if (type != SSH2_FXP_NAME)
1.33      deraadt  1052:                fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.11      djm      1053:                    SSH2_FXP_NAME, type);
                   1054:
1.116     djm      1055:        if ((r = sshbuf_get_u32(msg, &count)) != 0)
                   1056:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.11      djm      1057:        if (count != 1)
                   1058:                fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
                   1059:
1.116     djm      1060:        if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
                   1061:            (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
                   1062:            (r = decode_attrib(msg, &a)) != 0)
                   1063:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.11      djm      1064:
                   1065:        debug3("SSH_FXP_READLINK %s -> %s", path, filename);
                   1066:
1.98      djm      1067:        free(longname);
1.11      djm      1068:
1.116     djm      1069:        sshbuf_free(msg);
1.11      djm      1070:
1.116     djm      1071:        return filename;
1.82      djm      1072: }
                   1073: #endif
                   1074:
                   1075: int
1.84      dtucker  1076: do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
1.82      djm      1077:     int quiet)
                   1078: {
1.116     djm      1079:        struct sshbuf *msg;
1.82      djm      1080:        u_int id;
1.116     djm      1081:        int r;
1.82      djm      1082:
                   1083:        if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
                   1084:                error("Server does not support statvfs@openssh.com extension");
                   1085:                return -1;
                   1086:        }
                   1087:
                   1088:        id = conn->msg_id++;
                   1089:
1.116     djm      1090:        if ((msg = sshbuf_new()) == NULL)
                   1091:                fatal("%s: sshbuf_new failed", __func__);
                   1092:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1093:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1094:            (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
                   1095:            (r = sshbuf_put_cstring(msg, path)) != 0)
                   1096:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                   1097:        send_msg(conn, msg);
                   1098:        sshbuf_free(msg);
1.82      djm      1099:
1.93      djm      1100:        return get_decode_statvfs(conn, st, id, quiet);
1.82      djm      1101: }
                   1102:
                   1103: #ifdef notyet
                   1104: int
1.116     djm      1105: do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
1.84      dtucker  1106:     struct sftp_statvfs *st, int quiet)
1.82      djm      1107: {
1.116     djm      1108:        struct sshbuf *msg;
1.82      djm      1109:        u_int id;
                   1110:
                   1111:        if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
                   1112:                error("Server does not support fstatvfs@openssh.com extension");
                   1113:                return -1;
                   1114:        }
                   1115:
                   1116:        id = conn->msg_id++;
                   1117:
1.116     djm      1118:        if ((msg = sshbuf_new()) == NULL)
                   1119:                fatal("%s: sshbuf_new failed", __func__);
                   1120:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1121:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1122:            (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
                   1123:            (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
                   1124:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                   1125:        send_msg(conn, msg);
                   1126:        sshbuf_free(msg);
1.82      djm      1127:
1.93      djm      1128:        return get_decode_statvfs(conn, st, id, quiet);
1.1       djm      1129: }
1.78      chl      1130: #endif
1.1       djm      1131:
1.131     djm      1132: int
                   1133: do_lsetstat(struct sftp_conn *conn, const char *path, Attrib *a)
                   1134: {
                   1135:        struct sshbuf *msg;
                   1136:        u_int status, id;
                   1137:        int r;
                   1138:
                   1139:        if ((conn->exts & SFTP_EXT_LSETSTAT) == 0) {
                   1140:                error("Server does not support lsetstat@openssh.com extension");
                   1141:                return -1;
                   1142:        }
                   1143:
                   1144:        id = conn->msg_id++;
                   1145:        if ((msg = sshbuf_new()) == NULL)
                   1146:                fatal("%s: sshbuf_new failed", __func__);
                   1147:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1148:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1149:            (r = sshbuf_put_cstring(msg, "lsetstat@openssh.com")) != 0 ||
                   1150:            (r = sshbuf_put_cstring(msg, path)) != 0 ||
                   1151:            (r = encode_attrib(msg, a)) != 0)
                   1152:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                   1153:        send_msg(conn, msg);
                   1154:        sshbuf_free(msg);
                   1155:
                   1156:        status = get_status(conn, id);
                   1157:        if (status != SSH2_FX_OK)
                   1158:                error("Couldn't setstat on \"%s\": %s", path,
                   1159:                    fx2txt(status));
                   1160:
                   1161:        return status == SSH2_FX_OK ? 0 : -1;
                   1162: }
                   1163:
1.21      djm      1164: static void
1.93      djm      1165: send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
1.116     djm      1166:     u_int len, const u_char *handle, u_int handle_len)
1.21      djm      1167: {
1.116     djm      1168:        struct sshbuf *msg;
                   1169:        int r;
1.28      markus   1170:
1.116     djm      1171:        if ((msg = sshbuf_new()) == NULL)
                   1172:                fatal("%s: sshbuf_new failed", __func__);
                   1173:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_READ)) != 0 ||
                   1174:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1175:            (r = sshbuf_put_string(msg, handle, handle_len)) != 0 ||
                   1176:            (r = sshbuf_put_u64(msg, offset)) != 0 ||
                   1177:            (r = sshbuf_put_u32(msg, len)) != 0)
                   1178:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                   1179:        send_msg(conn, msg);
                   1180:        sshbuf_free(msg);
1.28      markus   1181: }
1.21      djm      1182:
1.1       djm      1183: int
1.116     djm      1184: do_download(struct sftp_conn *conn, const char *remote_path,
                   1185:     const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
                   1186:     int fsync_flag)
1.1       djm      1187: {
1.89      djm      1188:        Attrib junk;
1.116     djm      1189:        struct sshbuf *msg;
                   1190:        u_char *handle;
                   1191:        int local_fd = -1, write_error;
1.134   ! djm      1192:        int read_error, write_errno, lmodified = 0, reordered = 0, r;
1.101     djm      1193:        u_int64_t offset = 0, size, highwater;
1.116     djm      1194:        u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
1.39      fgsch    1195:        off_t progress_counter;
1.116     djm      1196:        size_t handle_len;
1.101     djm      1197:        struct stat st;
1.21      djm      1198:        struct request {
                   1199:                u_int id;
1.116     djm      1200:                size_t len;
1.21      djm      1201:                u_int64_t offset;
1.28      markus   1202:                TAILQ_ENTRY(request) tq;
1.21      djm      1203:        };
                   1204:        TAILQ_HEAD(reqhead, request) requests;
                   1205:        struct request *req;
1.116     djm      1206:        u_char type;
1.21      djm      1207:
                   1208:        TAILQ_INIT(&requests);
1.1       djm      1209:
1.89      djm      1210:        if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
                   1211:                return -1;
1.1       djm      1212:
1.86      djm      1213:        /* Do not preserve set[ug]id here, as we do not preserve ownership */
1.1       djm      1214:        if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1.38      djm      1215:                mode = a->perm & 0777;
1.1       djm      1216:        else
                   1217:                mode = 0666;
                   1218:
1.14      djm      1219:        if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1.41      djm      1220:            (!S_ISREG(a->perm))) {
                   1221:                error("Cannot download non-regular file: %s", remote_path);
1.14      djm      1222:                return(-1);
                   1223:        }
                   1224:
1.21      djm      1225:        if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
                   1226:                size = a->size;
                   1227:        else
                   1228:                size = 0;
                   1229:
1.23      djm      1230:        buflen = conn->transfer_buflen;
1.116     djm      1231:        if ((msg = sshbuf_new()) == NULL)
                   1232:                fatal("%s: sshbuf_new failed", __func__);
                   1233:
                   1234:        attrib_clear(&junk); /* Send empty attributes */
1.1       djm      1235:
                   1236:        /* Send open request */
1.23      djm      1237:        id = conn->msg_id++;
1.116     djm      1238:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
                   1239:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1240:            (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
                   1241:            (r = sshbuf_put_u32(msg, SSH2_FXF_READ)) != 0 ||
                   1242:            (r = encode_attrib(msg, &junk)) != 0)
                   1243:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                   1244:        send_msg(conn, msg);
1.33      deraadt  1245:        debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1.1       djm      1246:
1.93      djm      1247:        handle = get_handle(conn, id, &handle_len,
1.88      djm      1248:            "remote open(\"%s\")", remote_path);
1.1       djm      1249:        if (handle == NULL) {
1.116     djm      1250:                sshbuf_free(msg);
1.1       djm      1251:                return(-1);
                   1252:        }
                   1253:
1.105     djm      1254:        local_fd = open(local_path,
                   1255:            O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
1.23      djm      1256:        if (local_fd == -1) {
                   1257:                error("Couldn't open local file \"%s\" for writing: %s",
                   1258:                    local_path, strerror(errno));
1.101     djm      1259:                goto fail;
                   1260:        }
                   1261:        offset = highwater = 0;
1.105     djm      1262:        if (resume_flag) {
1.101     djm      1263:                if (fstat(local_fd, &st) == -1) {
                   1264:                        error("Unable to stat local file \"%s\": %s",
                   1265:                            local_path, strerror(errno));
                   1266:                        goto fail;
                   1267:                }
1.113     djm      1268:                if (st.st_size < 0) {
                   1269:                        error("\"%s\" has negative size", local_path);
                   1270:                        goto fail;
                   1271:                }
                   1272:                if ((u_int64_t)st.st_size > size) {
1.101     djm      1273:                        error("Unable to resume download of \"%s\": "
                   1274:                            "local file is larger than remote", local_path);
                   1275:  fail:
                   1276:                        do_close(conn, handle, handle_len);
1.116     djm      1277:                        sshbuf_free(msg);
1.101     djm      1278:                        free(handle);
1.110     djm      1279:                        if (local_fd != -1)
                   1280:                                close(local_fd);
1.101     djm      1281:                        return -1;
                   1282:                }
                   1283:                offset = highwater = st.st_size;
1.23      djm      1284:        }
                   1285:
1.1       djm      1286:        /* Read from remote and write to local */
1.101     djm      1287:        write_error = read_error = write_errno = num_req = 0;
1.21      djm      1288:        max_req = 1;
1.101     djm      1289:        progress_counter = offset;
1.39      fgsch    1290:
1.47      djm      1291:        if (showprogress && size != 0)
                   1292:                start_progress_meter(remote_path, size, &progress_counter);
1.39      fgsch    1293:
1.21      djm      1294:        while (num_req > 0 || max_req > 0) {
1.116     djm      1295:                u_char *data;
                   1296:                size_t len;
1.1       djm      1297:
1.49      djm      1298:                /*
1.51      deraadt  1299:                 * Simulate EOF on interrupt: stop sending new requests and
1.49      djm      1300:                 * allow outstanding requests to drain gracefully
                   1301:                 */
                   1302:                if (interrupted) {
                   1303:                        if (num_req == 0) /* If we haven't started yet... */
                   1304:                                break;
                   1305:                        max_req = 0;
                   1306:                }
                   1307:
1.21      djm      1308:                /* Send some more requests */
                   1309:                while (num_req < max_req) {
1.28      markus   1310:                        debug3("Request range %llu -> %llu (%d/%d)",
1.25      itojun   1311:                            (unsigned long long)offset,
                   1312:                            (unsigned long long)offset + buflen - 1,
                   1313:                            num_req, max_req);
1.108     djm      1314:                        req = xcalloc(1, sizeof(*req));
1.23      djm      1315:                        req->id = conn->msg_id++;
1.21      djm      1316:                        req->len = buflen;
                   1317:                        req->offset = offset;
                   1318:                        offset += buflen;
                   1319:                        num_req++;
                   1320:                        TAILQ_INSERT_TAIL(&requests, req, tq);
1.93      djm      1321:                        send_read_request(conn, req->id, req->offset,
1.21      djm      1322:                            req->len, handle, handle_len);
                   1323:                }
1.1       djm      1324:
1.116     djm      1325:                sshbuf_reset(msg);
                   1326:                get_msg(conn, msg);
                   1327:                if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   1328:                    (r = sshbuf_get_u32(msg, &id)) != 0)
                   1329:                        fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.33      deraadt  1330:                debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1.21      djm      1331:
                   1332:                /* Find the request in our queue */
1.53      deraadt  1333:                for (req = TAILQ_FIRST(&requests);
1.21      djm      1334:                    req != NULL && req->id != id;
                   1335:                    req = TAILQ_NEXT(req, tq))
                   1336:                        ;
                   1337:                if (req == NULL)
                   1338:                        fatal("Unexpected reply %u", id);
                   1339:
                   1340:                switch (type) {
                   1341:                case SSH2_FXP_STATUS:
1.116     djm      1342:                        if ((r = sshbuf_get_u32(msg, &status)) != 0)
                   1343:                                fatal("%s: buffer error: %s",
                   1344:                                    __func__, ssh_err(r));
1.21      djm      1345:                        if (status != SSH2_FX_EOF)
                   1346:                                read_error = 1;
                   1347:                        max_req = 0;
                   1348:                        TAILQ_REMOVE(&requests, req, tq);
1.98      djm      1349:                        free(req);
1.21      djm      1350:                        num_req--;
                   1351:                        break;
                   1352:                case SSH2_FXP_DATA:
1.116     djm      1353:                        if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
                   1354:                                fatal("%s: buffer error: %s",
                   1355:                                    __func__, ssh_err(r));
1.26      itojun   1356:                        debug3("Received data %llu -> %llu",
1.28      markus   1357:                            (unsigned long long)req->offset,
1.26      itojun   1358:                            (unsigned long long)req->offset + len - 1);
1.21      djm      1359:                        if (len > req->len)
                   1360:                                fatal("Received more data than asked for "
1.116     djm      1361:                                    "%zu > %zu", len, req->len);
1.134   ! djm      1362:                        lmodified = 1;
1.21      djm      1363:                        if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1.44      deraadt  1364:                            atomicio(vwrite, local_fd, data, len) != len) &&
1.21      djm      1365:                            !write_error) {
                   1366:                                write_errno = errno;
                   1367:                                write_error = 1;
                   1368:                                max_req = 0;
                   1369:                        }
1.101     djm      1370:                        else if (!reordered && req->offset <= highwater)
                   1371:                                highwater = req->offset + len;
                   1372:                        else if (!reordered && req->offset > highwater)
                   1373:                                reordered = 1;
1.39      fgsch    1374:                        progress_counter += len;
1.98      djm      1375:                        free(data);
1.1       djm      1376:
1.21      djm      1377:                        if (len == req->len) {
                   1378:                                TAILQ_REMOVE(&requests, req, tq);
1.98      djm      1379:                                free(req);
1.21      djm      1380:                                num_req--;
                   1381:                        } else {
                   1382:                                /* Resend the request for the missing data */
                   1383:                                debug3("Short data block, re-requesting "
1.26      itojun   1384:                                    "%llu -> %llu (%2d)",
1.28      markus   1385:                                    (unsigned long long)req->offset + len,
1.27      itojun   1386:                                    (unsigned long long)req->offset +
                   1387:                                    req->len - 1, num_req);
1.23      djm      1388:                                req->id = conn->msg_id++;
1.21      djm      1389:                                req->len -= len;
                   1390:                                req->offset += len;
1.93      djm      1391:                                send_read_request(conn, req->id,
1.23      djm      1392:                                    req->offset, req->len, handle, handle_len);
1.21      djm      1393:                                /* Reduce the request size */
                   1394:                                if (len < buflen)
1.125     deraadt  1395:                                        buflen = MAXIMUM(MIN_READ_SIZE, len);
1.21      djm      1396:                        }
                   1397:                        if (max_req > 0) { /* max_req = 0 iff EOF received */
                   1398:                                if (size > 0 && offset > size) {
                   1399:                                        /* Only one request at a time
                   1400:                                         * after the expected EOF */
                   1401:                                        debug3("Finish at %llu (%2d)",
1.26      itojun   1402:                                            (unsigned long long)offset,
                   1403:                                            num_req);
1.21      djm      1404:                                        max_req = 1;
1.49      djm      1405:                                } else if (max_req <= conn->num_requests) {
1.21      djm      1406:                                        ++max_req;
                   1407:                                }
1.1       djm      1408:                        }
1.21      djm      1409:                        break;
                   1410:                default:
1.33      deraadt  1411:                        fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1.1       djm      1412:                            SSH2_FXP_DATA, type);
                   1413:                }
1.21      djm      1414:        }
1.1       djm      1415:
1.39      fgsch    1416:        if (showprogress && size)
                   1417:                stop_progress_meter();
                   1418:
1.21      djm      1419:        /* Sanity check */
                   1420:        if (TAILQ_FIRST(&requests) != NULL)
                   1421:                fatal("Transfer complete, but requests still in queue");
1.101     djm      1422:        /* Truncate at highest contiguous point to avoid holes on interrupt */
                   1423:        if (read_error || write_error || interrupted) {
1.105     djm      1424:                if (reordered && resume_flag) {
1.101     djm      1425:                        error("Unable to resume download of \"%s\": "
                   1426:                            "server reordered requests", local_path);
                   1427:                }
                   1428:                debug("truncating at %llu", (unsigned long long)highwater);
1.120     djm      1429:                if (ftruncate(local_fd, highwater) == -1)
                   1430:                        error("ftruncate \"%s\": %s", local_path,
                   1431:                            strerror(errno));
1.101     djm      1432:        }
1.21      djm      1433:        if (read_error) {
1.28      markus   1434:                error("Couldn't read from remote file \"%s\" : %s",
1.21      djm      1435:                    remote_path, fx2txt(status));
1.103     djm      1436:                status = -1;
1.23      djm      1437:                do_close(conn, handle, handle_len);
1.21      djm      1438:        } else if (write_error) {
                   1439:                error("Couldn't write to \"%s\": %s", local_path,
                   1440:                    strerror(write_errno));
1.116     djm      1441:                status = SSH2_FX_FAILURE;
1.23      djm      1442:                do_close(conn, handle, handle_len);
1.21      djm      1443:        } else {
1.116     djm      1444:                if (do_close(conn, handle, handle_len) != 0 || interrupted)
                   1445:                        status = SSH2_FX_FAILURE;
                   1446:                else
                   1447:                        status = SSH2_FX_OK;
1.21      djm      1448:                /* Override umask and utimes if asked */
1.105     djm      1449:                if (preserve_flag && fchmod(local_fd, mode) == -1)
1.21      djm      1450:                        error("Couldn't set mode on \"%s\": %s", local_path,
1.37      deraadt  1451:                            strerror(errno));
1.105     djm      1452:                if (preserve_flag &&
                   1453:                    (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1.21      djm      1454:                        struct timeval tv[2];
                   1455:                        tv[0].tv_sec = a->atime;
                   1456:                        tv[1].tv_sec = a->mtime;
                   1457:                        tv[0].tv_usec = tv[1].tv_usec = 0;
                   1458:                        if (utimes(local_path, tv) == -1)
                   1459:                                error("Can't set times on \"%s\": %s",
1.37      deraadt  1460:                                    local_path, strerror(errno));
1.1       djm      1461:                }
1.134   ! djm      1462:                if (resume_flag && !lmodified)
        !          1463:                        logit("File \"%s\" was not modified", local_path);
        !          1464:                else if (fsync_flag) {
1.107     djm      1465:                        debug("syncing \"%s\"", local_path);
                   1466:                        if (fsync(local_fd) == -1)
                   1467:                                error("Couldn't sync file \"%s\": %s",
                   1468:                                    local_path, strerror(errno));
                   1469:                }
1.10      djm      1470:        }
1.5       djm      1471:        close(local_fd);
1.116     djm      1472:        sshbuf_free(msg);
1.98      djm      1473:        free(handle);
1.23      djm      1474:
1.129     djm      1475:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm      1476: }
                   1477:
1.89      djm      1478: static int
1.116     djm      1479: download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
                   1480:     int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
                   1481:     int resume_flag, int fsync_flag)
1.89      djm      1482: {
                   1483:        int i, ret = 0;
                   1484:        SFTP_DIRENT **dir_entries;
1.130     djm      1485:        char *filename, *new_src = NULL, *new_dst = NULL;
1.89      djm      1486:        mode_t mode = 0777;
                   1487:
                   1488:        if (depth >= MAX_DIR_DEPTH) {
                   1489:                error("Maximum directory depth exceeded: %d levels", depth);
                   1490:                return -1;
                   1491:        }
                   1492:
                   1493:        if (dirattrib == NULL &&
                   1494:            (dirattrib = do_stat(conn, src, 1)) == NULL) {
                   1495:                error("Unable to stat remote directory \"%s\"", src);
                   1496:                return -1;
                   1497:        }
                   1498:        if (!S_ISDIR(dirattrib->perm)) {
                   1499:                error("\"%s\" is not a directory", src);
                   1500:                return -1;
                   1501:        }
1.105     djm      1502:        if (print_flag)
1.124     schwarze 1503:                mprintf("Retrieving %s\n", src);
1.89      djm      1504:
                   1505:        if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
                   1506:                mode = dirattrib->perm & 01777;
                   1507:        else {
                   1508:                debug("Server did not send permissions for "
                   1509:                    "directory \"%s\"", dst);
                   1510:        }
                   1511:
                   1512:        if (mkdir(dst, mode) == -1 && errno != EEXIST) {
                   1513:                error("mkdir %s: %s", dst, strerror(errno));
                   1514:                return -1;
                   1515:        }
                   1516:
                   1517:        if (do_readdir(conn, src, &dir_entries) == -1) {
                   1518:                error("%s: Failed to get directory contents", src);
                   1519:                return -1;
                   1520:        }
                   1521:
                   1522:        for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1.130     djm      1523:                free(new_dst);
                   1524:                free(new_src);
                   1525:
1.89      djm      1526:                filename = dir_entries[i]->filename;
                   1527:                new_dst = path_append(dst, filename);
                   1528:                new_src = path_append(src, filename);
                   1529:
                   1530:                if (S_ISDIR(dir_entries[i]->a.perm)) {
                   1531:                        if (strcmp(filename, ".") == 0 ||
                   1532:                            strcmp(filename, "..") == 0)
                   1533:                                continue;
                   1534:                        if (download_dir_internal(conn, new_src, new_dst,
1.105     djm      1535:                            depth + 1, &(dir_entries[i]->a), preserve_flag,
1.107     djm      1536:                            print_flag, resume_flag, fsync_flag) == -1)
1.89      djm      1537:                                ret = -1;
                   1538:                } else if (S_ISREG(dir_entries[i]->a.perm) ) {
                   1539:                        if (do_download(conn, new_src, new_dst,
1.107     djm      1540:                            &(dir_entries[i]->a), preserve_flag,
                   1541:                            resume_flag, fsync_flag) == -1) {
1.89      djm      1542:                                error("Download of file %s to %s failed",
                   1543:                                    new_src, new_dst);
                   1544:                                ret = -1;
                   1545:                        }
                   1546:                } else
                   1547:                        logit("%s: not a regular file\n", new_src);
                   1548:
                   1549:        }
1.130     djm      1550:        free(new_dst);
                   1551:        free(new_src);
1.89      djm      1552:
1.105     djm      1553:        if (preserve_flag) {
1.89      djm      1554:                if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
                   1555:                        struct timeval tv[2];
                   1556:                        tv[0].tv_sec = dirattrib->atime;
                   1557:                        tv[1].tv_sec = dirattrib->mtime;
                   1558:                        tv[0].tv_usec = tv[1].tv_usec = 0;
                   1559:                        if (utimes(dst, tv) == -1)
                   1560:                                error("Can't set times on \"%s\": %s",
                   1561:                                    dst, strerror(errno));
                   1562:                } else
                   1563:                        debug("Server did not send times for directory "
                   1564:                            "\"%s\"", dst);
                   1565:        }
                   1566:
                   1567:        free_sftp_dirents(dir_entries);
                   1568:
                   1569:        return ret;
                   1570: }
                   1571:
                   1572: int
1.116     djm      1573: download_dir(struct sftp_conn *conn, const char *src, const char *dst,
                   1574:     Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
                   1575:     int fsync_flag)
1.89      djm      1576: {
                   1577:        char *src_canon;
                   1578:        int ret;
                   1579:
                   1580:        if ((src_canon = do_realpath(conn, src)) == NULL) {
1.107     djm      1581:                error("Unable to canonicalize path \"%s\"", src);
1.89      djm      1582:                return -1;
                   1583:        }
                   1584:
1.105     djm      1585:        ret = download_dir_internal(conn, src_canon, dst, 0,
1.107     djm      1586:            dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag);
1.98      djm      1587:        free(src_canon);
1.89      djm      1588:        return ret;
                   1589: }
                   1590:
1.1       djm      1591: int
1.116     djm      1592: do_upload(struct sftp_conn *conn, const char *local_path,
                   1593:     const char *remote_path, int preserve_flag, int resume, int fsync_flag)
1.1       djm      1594: {
1.116     djm      1595:        int r, local_fd;
                   1596:        u_int status = SSH2_FX_OK;
                   1597:        u_int id;
                   1598:        u_char type;
1.100     dtucker  1599:        off_t offset, progress_counter;
1.116     djm      1600:        u_char *handle, *data;
                   1601:        struct sshbuf *msg;
1.1       djm      1602:        struct stat sb;
1.115     logan    1603:        Attrib a, *c = NULL;
1.21      djm      1604:        u_int32_t startid;
                   1605:        u_int32_t ackid;
1.22      djm      1606:        struct outstanding_ack {
                   1607:                u_int id;
                   1608:                u_int len;
1.77      djm      1609:                off_t offset;
1.28      markus   1610:                TAILQ_ENTRY(outstanding_ack) tq;
1.22      djm      1611:        };
                   1612:        TAILQ_HEAD(ackhead, outstanding_ack) acks;
1.50      pedro    1613:        struct outstanding_ack *ack = NULL;
1.116     djm      1614:        size_t handle_len;
1.22      djm      1615:
                   1616:        TAILQ_INIT(&acks);
1.1       djm      1617:
                   1618:        if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
                   1619:                error("Couldn't open local file \"%s\" for reading: %s",
                   1620:                    local_path, strerror(errno));
                   1621:                return(-1);
                   1622:        }
                   1623:        if (fstat(local_fd, &sb) == -1) {
                   1624:                error("Couldn't fstat local file \"%s\": %s",
                   1625:                    local_path, strerror(errno));
1.41      djm      1626:                close(local_fd);
                   1627:                return(-1);
                   1628:        }
                   1629:        if (!S_ISREG(sb.st_mode)) {
                   1630:                error("%s is not a regular file", local_path);
1.1       djm      1631:                close(local_fd);
                   1632:                return(-1);
                   1633:        }
                   1634:        stat_to_attrib(&sb, &a);
                   1635:
                   1636:        a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
                   1637:        a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
                   1638:        a.perm &= 0777;
1.105     djm      1639:        if (!preserve_flag)
1.1       djm      1640:                a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
                   1641:
1.115     logan    1642:        if (resume) {
                   1643:                /* Get remote file size if it exists */
                   1644:                if ((c = do_stat(conn, remote_path, 0)) == NULL) {
1.122     djm      1645:                        close(local_fd);
1.115     logan    1646:                        return -1;
                   1647:                }
                   1648:
                   1649:                if ((off_t)c->size >= sb.st_size) {
                   1650:                        error("destination file bigger or same size as "
                   1651:                              "source file");
                   1652:                        close(local_fd);
                   1653:                        return -1;
                   1654:                }
                   1655:
                   1656:                if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
                   1657:                        close(local_fd);
                   1658:                        return -1;
                   1659:                }
                   1660:        }
                   1661:
1.116     djm      1662:        if ((msg = sshbuf_new()) == NULL)
                   1663:                fatal("%s: sshbuf_new failed", __func__);
1.1       djm      1664:
                   1665:        /* Send open request */
1.23      djm      1666:        id = conn->msg_id++;
1.116     djm      1667:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
                   1668:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1669:            (r = sshbuf_put_cstring(msg, remote_path)) != 0 ||
                   1670:            (r = sshbuf_put_u32(msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|
                   1671:            (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC))) != 0 ||
                   1672:            (r = encode_attrib(msg, &a)) != 0)
                   1673:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                   1674:        send_msg(conn, msg);
1.33      deraadt  1675:        debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1.1       djm      1676:
1.116     djm      1677:        sshbuf_reset(msg);
1.1       djm      1678:
1.93      djm      1679:        handle = get_handle(conn, id, &handle_len,
1.88      djm      1680:            "remote open(\"%s\")", remote_path);
1.1       djm      1681:        if (handle == NULL) {
                   1682:                close(local_fd);
1.116     djm      1683:                sshbuf_free(msg);
1.80      djm      1684:                return -1;
1.1       djm      1685:        }
                   1686:
1.21      djm      1687:        startid = ackid = id + 1;
1.23      djm      1688:        data = xmalloc(conn->transfer_buflen);
1.20      djm      1689:
1.1       djm      1690:        /* Read from local and write to remote */
1.115     logan    1691:        offset = progress_counter = (resume ? c->size : 0);
1.39      fgsch    1692:        if (showprogress)
1.100     dtucker  1693:                start_progress_meter(local_path, sb.st_size,
                   1694:                    &progress_counter);
1.39      fgsch    1695:
1.19      deraadt  1696:        for (;;) {
1.1       djm      1697:                int len;
                   1698:
                   1699:                /*
1.51      deraadt  1700:                 * Can't use atomicio here because it returns 0 on EOF,
1.49      djm      1701:                 * thus losing the last block of the file.
1.51      deraadt  1702:                 * Simulate an EOF on interrupt, allowing ACKs from the
1.49      djm      1703:                 * server to drain.
1.1       djm      1704:                 */
1.80      djm      1705:                if (interrupted || status != SSH2_FX_OK)
1.49      djm      1706:                        len = 0;
                   1707:                else do
1.23      djm      1708:                        len = read(local_fd, data, conn->transfer_buflen);
1.1       djm      1709:                while ((len == -1) && (errno == EINTR || errno == EAGAIN));
                   1710:
                   1711:                if (len == -1)
                   1712:                        fatal("Couldn't read from \"%s\": %s", local_path,
                   1713:                            strerror(errno));
1.21      djm      1714:
                   1715:                if (len != 0) {
1.108     djm      1716:                        ack = xcalloc(1, sizeof(*ack));
1.22      djm      1717:                        ack->id = ++id;
                   1718:                        ack->offset = offset;
                   1719:                        ack->len = len;
                   1720:                        TAILQ_INSERT_TAIL(&acks, ack, tq);
                   1721:
1.116     djm      1722:                        sshbuf_reset(msg);
                   1723:                        if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
                   1724:                            (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
                   1725:                            (r = sshbuf_put_string(msg, handle,
                   1726:                            handle_len)) != 0 ||
                   1727:                            (r = sshbuf_put_u64(msg, offset)) != 0 ||
                   1728:                            (r = sshbuf_put_string(msg, data, len)) != 0)
                   1729:                                fatal("%s: buffer error: %s",
                   1730:                                    __func__, ssh_err(r));
                   1731:                        send_msg(conn, msg);
1.33      deraadt  1732:                        debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1.37      deraadt  1733:                            id, (unsigned long long)offset, len);
1.22      djm      1734:                } else if (TAILQ_FIRST(&acks) == NULL)
1.1       djm      1735:                        break;
                   1736:
1.22      djm      1737:                if (ack == NULL)
                   1738:                        fatal("Unexpected ACK %u", id);
                   1739:
1.28      markus   1740:                if (id == startid || len == 0 ||
1.23      djm      1741:                    id - ackid >= conn->num_requests) {
1.116     djm      1742:                        u_int rid;
1.31      djm      1743:
1.116     djm      1744:                        sshbuf_reset(msg);
                   1745:                        get_msg(conn, msg);
                   1746:                        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   1747:                            (r = sshbuf_get_u32(msg, &rid)) != 0)
                   1748:                                fatal("%s: buffer error: %s",
                   1749:                                    __func__, ssh_err(r));
1.22      djm      1750:
                   1751:                        if (type != SSH2_FXP_STATUS)
                   1752:                                fatal("Expected SSH2_FXP_STATUS(%d) packet, "
                   1753:                                    "got %d", SSH2_FXP_STATUS, type);
                   1754:
1.116     djm      1755:                        if ((r = sshbuf_get_u32(msg, &status)) != 0)
                   1756:                                fatal("%s: buffer error: %s",
                   1757:                                    __func__, ssh_err(r));
                   1758:                        debug3("SSH2_FXP_STATUS %u", status);
1.22      djm      1759:
                   1760:                        /* Find the request in our queue */
1.53      deraadt  1761:                        for (ack = TAILQ_FIRST(&acks);
1.116     djm      1762:                            ack != NULL && ack->id != rid;
1.22      djm      1763:                            ack = TAILQ_NEXT(ack, tq))
                   1764:                                ;
                   1765:                        if (ack == NULL)
1.116     djm      1766:                                fatal("Can't find request for ID %u", rid);
1.22      djm      1767:                        TAILQ_REMOVE(&acks, ack, tq);
1.77      djm      1768:                        debug3("In write loop, ack for %u %u bytes at %lld",
                   1769:                            ack->id, ack->len, (long long)ack->offset);
1.21      djm      1770:                        ++ackid;
1.100     dtucker  1771:                        progress_counter += ack->len;
1.98      djm      1772:                        free(ack);
1.1       djm      1773:                }
                   1774:                offset += len;
1.77      djm      1775:                if (offset < 0)
                   1776:                        fatal("%s: offset < 0", __func__);
1.1       djm      1777:        }
1.116     djm      1778:        sshbuf_free(msg);
1.80      djm      1779:
1.39      fgsch    1780:        if (showprogress)
                   1781:                stop_progress_meter();
1.98      djm      1782:        free(data);
1.1       djm      1783:
1.80      djm      1784:        if (status != SSH2_FX_OK) {
                   1785:                error("Couldn't write to remote file \"%s\": %s",
                   1786:                    remote_path, fx2txt(status));
1.116     djm      1787:                status = SSH2_FX_FAILURE;
1.80      djm      1788:        }
                   1789:
1.1       djm      1790:        if (close(local_fd) == -1) {
                   1791:                error("Couldn't close local file \"%s\": %s", local_path,
                   1792:                    strerror(errno));
1.116     djm      1793:                status = SSH2_FX_FAILURE;
1.1       djm      1794:        }
                   1795:
1.10      djm      1796:        /* Override umask and utimes if asked */
1.105     djm      1797:        if (preserve_flag)
1.23      djm      1798:                do_fsetstat(conn, handle, handle_len, &a);
1.10      djm      1799:
1.107     djm      1800:        if (fsync_flag)
                   1801:                (void)do_fsync(conn, handle, handle_len);
                   1802:
1.121     djm      1803:        if (do_close(conn, handle, handle_len) != 0)
1.116     djm      1804:                status = SSH2_FX_FAILURE;
                   1805:
1.98      djm      1806:        free(handle);
1.5       djm      1807:
1.116     djm      1808:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm      1809: }
1.89      djm      1810:
                   1811: static int
1.116     djm      1812: upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
                   1813:     int depth, int preserve_flag, int print_flag, int resume, int fsync_flag)
1.89      djm      1814: {
1.116     djm      1815:        int ret = 0;
1.89      djm      1816:        DIR *dirp;
                   1817:        struct dirent *dp;
1.130     djm      1818:        char *filename, *new_src = NULL, *new_dst = NULL;
1.89      djm      1819:        struct stat sb;
1.121     djm      1820:        Attrib a, *dirattrib;
1.89      djm      1821:
                   1822:        if (depth >= MAX_DIR_DEPTH) {
                   1823:                error("Maximum directory depth exceeded: %d levels", depth);
                   1824:                return -1;
                   1825:        }
                   1826:
                   1827:        if (stat(src, &sb) == -1) {
                   1828:                error("Couldn't stat directory \"%s\": %s",
                   1829:                    src, strerror(errno));
                   1830:                return -1;
                   1831:        }
                   1832:        if (!S_ISDIR(sb.st_mode)) {
                   1833:                error("\"%s\" is not a directory", src);
                   1834:                return -1;
                   1835:        }
1.105     djm      1836:        if (print_flag)
1.124     schwarze 1837:                mprintf("Entering %s\n", src);
1.89      djm      1838:
                   1839:        attrib_clear(&a);
                   1840:        stat_to_attrib(&sb, &a);
                   1841:        a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
                   1842:        a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
                   1843:        a.perm &= 01777;
1.105     djm      1844:        if (!preserve_flag)
1.89      djm      1845:                a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1.101     djm      1846:
1.89      djm      1847:        /*
1.121     djm      1848:         * sftp lacks a portable status value to match errno EEXIST,
                   1849:         * so if we get a failure back then we must check whether
                   1850:         * the path already existed and is a directory.
1.89      djm      1851:         */
1.121     djm      1852:        if (do_mkdir(conn, dst, &a, 0) != 0) {
                   1853:                if ((dirattrib = do_stat(conn, dst, 0)) == NULL)
1.89      djm      1854:                        return -1;
1.121     djm      1855:                if (!S_ISDIR(dirattrib->perm)) {
                   1856:                        error("\"%s\" exists but is not a directory", dst);
1.89      djm      1857:                        return -1;
1.121     djm      1858:                }
1.89      djm      1859:        }
                   1860:
                   1861:        if ((dirp = opendir(src)) == NULL) {
                   1862:                error("Failed to open dir \"%s\": %s", src, strerror(errno));
                   1863:                return -1;
                   1864:        }
1.101     djm      1865:
1.89      djm      1866:        while (((dp = readdir(dirp)) != NULL) && !interrupted) {
                   1867:                if (dp->d_ino == 0)
                   1868:                        continue;
1.130     djm      1869:                free(new_dst);
                   1870:                free(new_src);
1.89      djm      1871:                filename = dp->d_name;
                   1872:                new_dst = path_append(dst, filename);
                   1873:                new_src = path_append(src, filename);
                   1874:
1.90      dtucker  1875:                if (lstat(new_src, &sb) == -1) {
                   1876:                        logit("%s: lstat failed: %s", filename,
                   1877:                            strerror(errno));
                   1878:                        ret = -1;
                   1879:                } else if (S_ISDIR(sb.st_mode)) {
1.89      djm      1880:                        if (strcmp(filename, ".") == 0 ||
                   1881:                            strcmp(filename, "..") == 0)
                   1882:                                continue;
                   1883:
                   1884:                        if (upload_dir_internal(conn, new_src, new_dst,
1.115     logan    1885:                            depth + 1, preserve_flag, print_flag, resume,
1.107     djm      1886:                            fsync_flag) == -1)
1.89      djm      1887:                                ret = -1;
1.90      dtucker  1888:                } else if (S_ISREG(sb.st_mode)) {
1.105     djm      1889:                        if (do_upload(conn, new_src, new_dst,
1.115     logan    1890:                            preserve_flag, resume, fsync_flag) == -1) {
1.89      djm      1891:                                error("Uploading of file %s to %s failed!",
                   1892:                                    new_src, new_dst);
                   1893:                                ret = -1;
                   1894:                        }
                   1895:                } else
                   1896:                        logit("%s: not a regular file\n", filename);
                   1897:        }
1.130     djm      1898:        free(new_dst);
                   1899:        free(new_src);
1.89      djm      1900:
                   1901:        do_setstat(conn, dst, &a);
                   1902:
                   1903:        (void) closedir(dirp);
                   1904:        return ret;
                   1905: }
                   1906:
                   1907: int
1.116     djm      1908: upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
                   1909:     int preserve_flag, int print_flag, int resume, int fsync_flag)
1.89      djm      1910: {
                   1911:        char *dst_canon;
                   1912:        int ret;
                   1913:
                   1914:        if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1.107     djm      1915:                error("Unable to canonicalize path \"%s\"", dst);
1.89      djm      1916:                return -1;
                   1917:        }
                   1918:
1.106     djm      1919:        ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
1.115     logan    1920:            print_flag, resume, fsync_flag);
1.107     djm      1921:
1.98      djm      1922:        free(dst_canon);
1.89      djm      1923:        return ret;
                   1924: }
                   1925:
                   1926: char *
1.116     djm      1927: path_append(const char *p1, const char *p2)
1.89      djm      1928: {
                   1929:        char *ret;
                   1930:        size_t len = strlen(p1) + strlen(p2) + 2;
                   1931:
                   1932:        ret = xmalloc(len);
                   1933:        strlcpy(ret, p1, len);
                   1934:        if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
                   1935:                strlcat(ret, "/", len);
                   1936:        strlcat(ret, p2, len);
                   1937:
                   1938:        return(ret);
                   1939: }
                   1940: