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

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