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

1.171   ! djm         1: /* $OpenBSD: sftp-client.c,v 1.170 2023/03/28 07:44:32 dtucker Exp $ */
1.1       djm         2: /*
1.46      djm         3:  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
1.1       djm         4:  *
1.46      djm         5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       djm         8:  *
1.46      djm         9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       djm        16:  */
                     17:
                     18: /* XXX: memleaks */
                     19: /* XXX: signed vs unsigned */
1.23      djm        20: /* XXX: remove all logging, only return status codes */
1.1       djm        21: /* XXX: copy between two remote sites */
                     22:
1.74      deraadt    23: #include <sys/types.h>
1.93      djm        24: #include <sys/poll.h>
1.21      djm        25: #include <sys/queue.h>
1.60      stevesk    26: #include <sys/stat.h>
1.71      stevesk    27: #include <sys/time.h>
1.82      djm        28: #include <sys/statvfs.h>
1.74      deraadt    29: #include <sys/uio.h>
1.66      stevesk    30:
1.89      djm        31: #include <dirent.h>
1.67      stevesk    32: #include <errno.h>
1.66      stevesk    33: #include <fcntl.h>
1.152     djm        34: #include <poll.h>
1.70      stevesk    35: #include <signal.h>
1.87      dtucker    36: #include <stdarg.h>
1.73      stevesk    37: #include <stdio.h>
1.109     dtucker    38: #include <stdlib.h>
1.69      stevesk    39: #include <string.h>
1.68      stevesk    40: #include <unistd.h>
1.1       djm        41:
1.74      deraadt    42: #include "xmalloc.h"
1.116     djm        43: #include "ssherr.h"
                     44: #include "sshbuf.h"
1.1       djm        45: #include "log.h"
                     46: #include "atomicio.h"
1.39      fgsch      47: #include "progressmeter.h"
1.64      djm        48: #include "misc.h"
1.124     schwarze   49: #include "utf8.h"
1.1       djm        50:
                     51: #include "sftp.h"
                     52: #include "sftp-common.h"
                     53: #include "sftp-client.h"
                     54:
1.49      djm        55: extern volatile sig_atomic_t interrupted;
1.39      fgsch      56: extern int showprogress;
                     57:
1.166     djm        58: /* Default size of buffer for up/download (fix sftp.1 scp.1 if changed) */
1.141     djm        59: #define DEFAULT_COPY_BUFLEN    32768
                     60:
1.166     djm        61: /* Default number of concurrent xfer requests (fix sftp.1 scp.1 if changed) */
1.141     djm        62: #define DEFAULT_NUM_REQUESTS   64
                     63:
1.59      david      64: /* Minimum amount of data to read at a time */
1.21      djm        65: #define MIN_READ_SIZE  512
                     66:
1.89      djm        67: /* Maximum depth to descend in directory trees */
                     68: #define MAX_DIR_DEPTH 64
                     69:
1.23      djm        70: struct sftp_conn {
                     71:        int fd_in;
                     72:        int fd_out;
1.141     djm        73:        u_int download_buflen;
                     74:        u_int upload_buflen;
1.23      djm        75:        u_int num_requests;
                     76:        u_int version;
                     77:        u_int msg_id;
1.165     djm        78: #define SFTP_EXT_POSIX_RENAME          0x00000001
                     79: #define SFTP_EXT_STATVFS               0x00000002
                     80: #define SFTP_EXT_FSTATVFS              0x00000004
                     81: #define SFTP_EXT_HARDLINK              0x00000008
                     82: #define SFTP_EXT_FSYNC                 0x00000010
                     83: #define SFTP_EXT_LSETSTAT              0x00000020
                     84: #define SFTP_EXT_LIMITS                        0x00000040
                     85: #define SFTP_EXT_PATH_EXPAND           0x00000080
                     86: #define SFTP_EXT_COPY_DATA             0x00000100
                     87: #define SFTP_EXT_GETUSERSGROUPS_BY_ID  0x00000200
1.81      djm        88:        u_int exts;
1.93      djm        89:        u_int64_t limit_kbps;
                     90:        struct bwlimit bwlimit_in, bwlimit_out;
1.23      djm        91: };
1.4       djm        92:
1.146     djm        93: /* Tracks in-progress requests during file transfers */
                     94: struct request {
                     95:        u_int id;
                     96:        size_t len;
                     97:        u_int64_t offset;
                     98:        TAILQ_ENTRY(request) tq;
                     99: };
                    100: TAILQ_HEAD(requests, request);
                    101:
1.116     djm       102: static u_char *
                    103: get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
1.93      djm       104:     const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
                    105:
1.151     djm       106: static struct request *
                    107: request_enqueue(struct requests *requests, u_int id, size_t len,
                    108:     uint64_t offset)
                    109: {
                    110:        struct request *req;
                    111:
                    112:        req = xcalloc(1, sizeof(*req));
                    113:        req->id = id;
                    114:        req->len = len;
                    115:        req->offset = offset;
                    116:        TAILQ_INSERT_TAIL(requests, req, tq);
                    117:        return req;
                    118: }
                    119:
                    120: static struct request *
                    121: request_find(struct requests *requests, u_int id)
                    122: {
                    123:        struct request *req;
                    124:
                    125:        for (req = TAILQ_FIRST(requests);
                    126:            req != NULL && req->id != id;
                    127:            req = TAILQ_NEXT(req, tq))
                    128:                ;
                    129:        return req;
                    130: }
                    131:
1.93      djm       132: static int
                    133: sftpio(void *_bwlimit, size_t amount)
                    134: {
                    135:        struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
                    136:
1.133     dtucker   137:        refresh_progress_meter(0);
1.132     dtucker   138:        if (bwlimit != NULL)
                    139:                bandwidth_limit(bwlimit, amount);
1.93      djm       140:        return 0;
                    141: }
1.88      djm       142:
1.17      itojun    143: static void
1.116     djm       144: send_msg(struct sftp_conn *conn, struct sshbuf *m)
1.1       djm       145: {
1.40      djm       146:        u_char mlen[4];
1.65      djm       147:        struct iovec iov[2];
1.40      djm       148:
1.116     djm       149:        if (sshbuf_len(m) > SFTP_MAX_MSG_LENGTH)
                    150:                fatal("Outbound message too long %zu", sshbuf_len(m));
1.40      djm       151:
                    152:        /* Send length first */
1.116     djm       153:        put_u32(mlen, sshbuf_len(m));
1.65      djm       154:        iov[0].iov_base = mlen;
                    155:        iov[0].iov_len = sizeof(mlen);
1.116     djm       156:        iov[1].iov_base = (u_char *)sshbuf_ptr(m);
                    157:        iov[1].iov_len = sshbuf_len(m);
1.74      deraadt   158:
1.132     dtucker   159:        if (atomiciov6(writev, conn->fd_out, iov, 2, sftpio,
                    160:            conn->limit_kbps > 0 ? &conn->bwlimit_out : NULL) !=
1.116     djm       161:            sshbuf_len(m) + sizeof(mlen))
1.1       djm       162:                fatal("Couldn't send packet: %s", strerror(errno));
                    163:
1.116     djm       164:        sshbuf_reset(m);
1.1       djm       165: }
                    166:
1.17      itojun    167: static void
1.128     dtucker   168: get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
1.1       djm       169: {
1.40      djm       170:        u_int msg_len;
1.116     djm       171:        u_char *p;
                    172:        int r;
1.1       djm       173:
1.144     djm       174:        sshbuf_reset(m);
1.116     djm       175:        if ((r = sshbuf_reserve(m, 4, &p)) != 0)
1.137     djm       176:                fatal_fr(r, "reserve");
1.132     dtucker   177:        if (atomicio6(read, conn->fd_in, p, 4, sftpio,
                    178:            conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL) != 4) {
1.127     djm       179:                if (errno == EPIPE || errno == ECONNRESET)
1.54      avsm      180:                        fatal("Connection closed");
                    181:                else
                    182:                        fatal("Couldn't read packet: %s", strerror(errno));
                    183:        }
1.1       djm       184:
1.116     djm       185:        if ((r = sshbuf_get_u32(m, &msg_len)) != 0)
1.137     djm       186:                fatal_fr(r, "sshbuf_get_u32");
1.128     dtucker   187:        if (msg_len > SFTP_MAX_MSG_LENGTH) {
                    188:                do_log2(initial ? SYSLOG_LEVEL_ERROR : SYSLOG_LEVEL_FATAL,
                    189:                    "Received message too long %u", msg_len);
                    190:                fatal("Ensure the remote shell produces no output "
                    191:                    "for non-interactive sessions.");
                    192:        }
1.1       djm       193:
1.116     djm       194:        if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
1.137     djm       195:                fatal_fr(r, "reserve");
1.132     dtucker   196:        if (atomicio6(read, conn->fd_in, p, msg_len, sftpio,
                    197:            conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL)
1.93      djm       198:            != msg_len) {
1.54      avsm      199:                if (errno == EPIPE)
                    200:                        fatal("Connection closed");
                    201:                else
                    202:                        fatal("Read packet: %s", strerror(errno));
                    203:        }
1.1       djm       204: }
                    205:
1.17      itojun    206: static void
1.128     dtucker   207: get_msg(struct sftp_conn *conn, struct sshbuf *m)
                    208: {
                    209:        get_msg_extended(conn, m, 0);
                    210: }
                    211:
                    212: static void
1.116     djm       213: send_string_request(struct sftp_conn *conn, u_int id, u_int code, const char *s,
1.1       djm       214:     u_int len)
                    215: {
1.116     djm       216:        struct sshbuf *msg;
                    217:        int r;
1.1       djm       218:
1.116     djm       219:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       220:                fatal_f("sshbuf_new failed");
1.116     djm       221:        if ((r = sshbuf_put_u8(msg, code)) != 0 ||
                    222:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                    223:            (r = sshbuf_put_string(msg, s, len)) != 0)
1.137     djm       224:                fatal_fr(r, "compose");
1.116     djm       225:        send_msg(conn, msg);
1.93      djm       226:        debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
1.116     djm       227:        sshbuf_free(msg);
1.1       djm       228: }
                    229:
1.17      itojun    230: static void
1.93      djm       231: send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
1.116     djm       232:     const void *s, u_int len, Attrib *a)
1.1       djm       233: {
1.116     djm       234:        struct sshbuf *msg;
                    235:        int r;
1.1       djm       236:
1.116     djm       237:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       238:                fatal_f("sshbuf_new failed");
1.116     djm       239:        if ((r = sshbuf_put_u8(msg, code)) != 0 ||
                    240:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                    241:            (r = sshbuf_put_string(msg, s, len)) != 0 ||
                    242:            (r = encode_attrib(msg, a)) != 0)
1.137     djm       243:                fatal_fr(r, "compose");
1.116     djm       244:        send_msg(conn, msg);
1.148     djm       245:        debug3("Sent message fd %d T:%u I:%u F:0x%04x M:%05o",
                    246:            conn->fd_out, code, id, a->flags, a->perm);
1.116     djm       247:        sshbuf_free(msg);
1.1       djm       248: }
                    249:
1.17      itojun    250: static u_int
1.93      djm       251: get_status(struct sftp_conn *conn, u_int expected_id)
1.1       djm       252: {
1.116     djm       253:        struct sshbuf *msg;
                    254:        u_char type;
                    255:        u_int id, status;
                    256:        int r;
1.1       djm       257:
1.116     djm       258:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       259:                fatal_f("sshbuf_new failed");
1.116     djm       260:        get_msg(conn, msg);
                    261:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                    262:            (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm       263:                fatal_fr(r, "compose");
1.1       djm       264:
                    265:        if (id != expected_id)
1.33      deraadt   266:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.1       djm       267:        if (type != SSH2_FXP_STATUS)
1.33      deraadt   268:                fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
1.1       djm       269:                    SSH2_FXP_STATUS, type);
                    270:
1.116     djm       271:        if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm       272:                fatal_fr(r, "parse");
1.116     djm       273:        sshbuf_free(msg);
1.1       djm       274:
1.33      deraadt   275:        debug3("SSH2_FXP_STATUS %u", status);
1.1       djm       276:
1.93      djm       277:        return status;
1.1       djm       278: }
                    279:
1.116     djm       280: static u_char *
                    281: get_handle(struct sftp_conn *conn, u_int expected_id, size_t *len,
1.93      djm       282:     const char *errfmt, ...)
1.1       djm       283: {
1.116     djm       284:        struct sshbuf *msg;
                    285:        u_int id, status;
                    286:        u_char type;
                    287:        u_char *handle;
                    288:        char errmsg[256];
1.88      djm       289:        va_list args;
1.116     djm       290:        int r;
1.88      djm       291:
                    292:        va_start(args, errfmt);
                    293:        if (errfmt != NULL)
                    294:                vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
                    295:        va_end(args);
1.1       djm       296:
1.116     djm       297:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       298:                fatal_f("sshbuf_new failed");
1.116     djm       299:        get_msg(conn, msg);
                    300:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                    301:            (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm       302:                fatal_fr(r, "parse");
1.1       djm       303:
                    304:        if (id != expected_id)
1.88      djm       305:                fatal("%s: ID mismatch (%u != %u)",
                    306:                    errfmt == NULL ? __func__ : errmsg, id, expected_id);
1.1       djm       307:        if (type == SSH2_FXP_STATUS) {
1.116     djm       308:                if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm       309:                        fatal_fr(r, "parse status");
1.88      djm       310:                if (errfmt != NULL)
                    311:                        error("%s: %s", errmsg, fx2txt(status));
1.116     djm       312:                sshbuf_free(msg);
1.1       djm       313:                return(NULL);
                    314:        } else if (type != SSH2_FXP_HANDLE)
1.88      djm       315:                fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
                    316:                    errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
1.1       djm       317:
1.116     djm       318:        if ((r = sshbuf_get_string(msg, &handle, len)) != 0)
1.137     djm       319:                fatal_fr(r, "parse handle");
1.116     djm       320:        sshbuf_free(msg);
1.1       djm       321:
1.116     djm       322:        return handle;
1.1       djm       323: }
                    324:
1.158     jsg       325: /* XXX returning &static is error-prone. Refactor to fill *Attrib argument */
1.17      itojun    326: static Attrib *
1.93      djm       327: get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
1.1       djm       328: {
1.116     djm       329:        struct sshbuf *msg;
                    330:        u_int id;
                    331:        u_char type;
                    332:        int r;
                    333:        static Attrib a;
                    334:
                    335:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       336:                fatal_f("sshbuf_new failed");
1.116     djm       337:        get_msg(conn, msg);
                    338:
                    339:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                    340:            (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm       341:                fatal_fr(r, "parse");
1.1       djm       342:
                    343:        if (id != expected_id)
1.33      deraadt   344:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.1       djm       345:        if (type == SSH2_FXP_STATUS) {
1.116     djm       346:                u_int status;
1.1       djm       347:
1.116     djm       348:                if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm       349:                        fatal_fr(r, "parse status");
1.14      djm       350:                if (quiet)
1.161     djm       351:                        debug("stat remote: %s", fx2txt(status));
1.14      djm       352:                else
1.161     djm       353:                        error("stat remote: %s", fx2txt(status));
1.116     djm       354:                sshbuf_free(msg);
1.1       djm       355:                return(NULL);
                    356:        } else if (type != SSH2_FXP_ATTRS) {
1.33      deraadt   357:                fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
1.1       djm       358:                    SSH2_FXP_ATTRS, type);
                    359:        }
1.116     djm       360:        if ((r = decode_attrib(msg, &a)) != 0) {
1.137     djm       361:                error_fr(r, "decode_attrib");
1.116     djm       362:                sshbuf_free(msg);
                    363:                return NULL;
                    364:        }
1.158     jsg       365:        debug3("Received stat reply T:%u I:%u F:0x%04x M:%05o",
1.148     djm       366:            type, id, a.flags, a.perm);
1.116     djm       367:        sshbuf_free(msg);
1.1       djm       368:
1.116     djm       369:        return &a;
1.1       djm       370: }
                    371:
1.82      djm       372: static int
1.93      djm       373: get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
                    374:     u_int expected_id, int quiet)
1.82      djm       375: {
1.116     djm       376:        struct sshbuf *msg;
                    377:        u_char type;
                    378:        u_int id;
                    379:        u_int64_t flag;
                    380:        int r;
1.82      djm       381:
1.116     djm       382:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       383:                fatal_f("sshbuf_new failed");
1.116     djm       384:        get_msg(conn, msg);
                    385:
                    386:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                    387:            (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm       388:                fatal_fr(r, "parse");
1.82      djm       389:
                    390:        debug3("Received statvfs reply T:%u I:%u", type, id);
                    391:        if (id != expected_id)
                    392:                fatal("ID mismatch (%u != %u)", id, expected_id);
                    393:        if (type == SSH2_FXP_STATUS) {
1.116     djm       394:                u_int status;
1.82      djm       395:
1.116     djm       396:                if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm       397:                        fatal_fr(r, "parse status");
1.82      djm       398:                if (quiet)
1.161     djm       399:                        debug("remote statvfs: %s", fx2txt(status));
1.82      djm       400:                else
1.161     djm       401:                        error("remote statvfs: %s", fx2txt(status));
1.116     djm       402:                sshbuf_free(msg);
1.82      djm       403:                return -1;
                    404:        } else if (type != SSH2_FXP_EXTENDED_REPLY) {
                    405:                fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
                    406:                    SSH2_FXP_EXTENDED_REPLY, type);
                    407:        }
                    408:
1.114     tedu      409:        memset(st, 0, sizeof(*st));
1.116     djm       410:        if ((r = sshbuf_get_u64(msg, &st->f_bsize)) != 0 ||
                    411:            (r = sshbuf_get_u64(msg, &st->f_frsize)) != 0 ||
                    412:            (r = sshbuf_get_u64(msg, &st->f_blocks)) != 0 ||
                    413:            (r = sshbuf_get_u64(msg, &st->f_bfree)) != 0 ||
                    414:            (r = sshbuf_get_u64(msg, &st->f_bavail)) != 0 ||
                    415:            (r = sshbuf_get_u64(msg, &st->f_files)) != 0 ||
                    416:            (r = sshbuf_get_u64(msg, &st->f_ffree)) != 0 ||
                    417:            (r = sshbuf_get_u64(msg, &st->f_favail)) != 0 ||
                    418:            (r = sshbuf_get_u64(msg, &st->f_fsid)) != 0 ||
                    419:            (r = sshbuf_get_u64(msg, &flag)) != 0 ||
                    420:            (r = sshbuf_get_u64(msg, &st->f_namemax)) != 0)
1.137     djm       421:                fatal_fr(r, "parse statvfs");
1.82      djm       422:
                    423:        st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
                    424:        st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
                    425:
1.116     djm       426:        sshbuf_free(msg);
1.82      djm       427:
                    428:        return 0;
                    429: }
                    430:
1.23      djm       431: struct sftp_conn *
1.93      djm       432: do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
                    433:     u_int64_t limit_kbps)
1.1       djm       434: {
1.116     djm       435:        u_char type;
                    436:        struct sshbuf *msg;
1.23      djm       437:        struct sftp_conn *ret;
1.116     djm       438:        int r;
1.1       djm       439:
1.103     djm       440:        ret = xcalloc(1, sizeof(*ret));
                    441:        ret->msg_id = 1;
1.93      djm       442:        ret->fd_in = fd_in;
                    443:        ret->fd_out = fd_out;
1.141     djm       444:        ret->download_buflen = ret->upload_buflen =
                    445:            transfer_buflen ? transfer_buflen : DEFAULT_COPY_BUFLEN;
                    446:        ret->num_requests =
                    447:            num_requests ? num_requests : DEFAULT_NUM_REQUESTS;
1.93      djm       448:        ret->exts = 0;
                    449:        ret->limit_kbps = 0;
                    450:
1.116     djm       451:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       452:                fatal_f("sshbuf_new failed");
1.116     djm       453:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_INIT)) != 0 ||
                    454:            (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0)
1.137     djm       455:                fatal_fr(r, "parse");
                    456:
1.116     djm       457:        send_msg(ret, msg);
1.1       djm       458:
1.128     dtucker   459:        get_msg_extended(ret, msg, 1);
1.1       djm       460:
1.3       stevesk   461:        /* Expecting a VERSION reply */
1.116     djm       462:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
1.137     djm       463:                fatal_fr(r, "parse type");
1.116     djm       464:        if (type != SSH2_FXP_VERSION) {
1.33      deraadt   465:                error("Invalid packet back from SSH2_FXP_INIT (type %u)",
1.1       djm       466:                    type);
1.116     djm       467:                sshbuf_free(msg);
1.119     jsg       468:                free(ret);
1.23      djm       469:                return(NULL);
1.1       djm       470:        }
1.116     djm       471:        if ((r = sshbuf_get_u32(msg, &ret->version)) != 0)
1.137     djm       472:                fatal_fr(r, "parse version");
1.1       djm       473:
1.93      djm       474:        debug2("Remote version: %u", ret->version);
1.1       djm       475:
                    476:        /* Check for extensions */
1.116     djm       477:        while (sshbuf_len(msg) > 0) {
                    478:                char *name;
                    479:                u_char *value;
                    480:                size_t vlen;
1.85      djm       481:                int known = 0;
1.1       djm       482:
1.116     djm       483:                if ((r = sshbuf_get_cstring(msg, &name, NULL)) != 0 ||
                    484:                    (r = sshbuf_get_string(msg, &value, &vlen)) != 0)
1.137     djm       485:                        fatal_fr(r, "parse extension");
1.82      djm       486:                if (strcmp(name, "posix-rename@openssh.com") == 0 &&
1.116     djm       487:                    strcmp((char *)value, "1") == 0) {
1.93      djm       488:                        ret->exts |= SFTP_EXT_POSIX_RENAME;
1.85      djm       489:                        known = 1;
                    490:                } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
1.116     djm       491:                    strcmp((char *)value, "2") == 0) {
1.93      djm       492:                        ret->exts |= SFTP_EXT_STATVFS;
1.85      djm       493:                        known = 1;
1.94      djm       494:                } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
1.116     djm       495:                    strcmp((char *)value, "2") == 0) {
1.93      djm       496:                        ret->exts |= SFTP_EXT_FSTATVFS;
1.85      djm       497:                        known = 1;
1.94      djm       498:                } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
1.116     djm       499:                    strcmp((char *)value, "1") == 0) {
1.94      djm       500:                        ret->exts |= SFTP_EXT_HARDLINK;
                    501:                        known = 1;
1.116     djm       502:                } else if (strcmp(name, "fsync@openssh.com") == 0 &&
                    503:                    strcmp((char *)value, "1") == 0) {
                    504:                        ret->exts |= SFTP_EXT_FSYNC;
                    505:                        known = 1;
1.131     djm       506:                } else if (strcmp(name, "lsetstat@openssh.com") == 0 &&
                    507:                    strcmp((char *)value, "1") == 0) {
                    508:                        ret->exts |= SFTP_EXT_LSETSTAT;
                    509:                        known = 1;
1.141     djm       510:                } else if (strcmp(name, "limits@openssh.com") == 0 &&
                    511:                    strcmp((char *)value, "1") == 0) {
                    512:                        ret->exts |= SFTP_EXT_LIMITS;
                    513:                        known = 1;
1.154     djm       514:                } else if (strcmp(name, "expand-path@openssh.com") == 0 &&
                    515:                    strcmp((char *)value, "1") == 0) {
                    516:                        ret->exts |= SFTP_EXT_PATH_EXPAND;
                    517:                        known = 1;
1.162     djm       518:                } else if (strcmp(name, "copy-data") == 0 &&
                    519:                    strcmp((char *)value, "1") == 0) {
                    520:                        ret->exts |= SFTP_EXT_COPY_DATA;
                    521:                        known = 1;
1.165     djm       522:                } else if (strcmp(name,
                    523:                    "users-groups-by-id@openssh.com") == 0 &&
                    524:                    strcmp((char *)value, "1") == 0) {
                    525:                        ret->exts |= SFTP_EXT_GETUSERSGROUPS_BY_ID;
                    526:                        known = 1;
1.85      djm       527:                }
                    528:                if (known) {
                    529:                        debug2("Server supports extension \"%s\" revision %s",
                    530:                            name, value);
                    531:                } else {
                    532:                        debug2("Unrecognised server extension \"%s\"", name);
                    533:                }
1.98      djm       534:                free(name);
                    535:                free(value);
1.1       djm       536:        }
                    537:
1.116     djm       538:        sshbuf_free(msg);
1.11      djm       539:
1.141     djm       540:        /* Query the server for its limits */
                    541:        if (ret->exts & SFTP_EXT_LIMITS) {
                    542:                struct sftp_limits limits;
                    543:                if (do_limits(ret, &limits) != 0)
                    544:                        fatal_f("limits failed");
                    545:
                    546:                /* If the caller did not specify, find a good value */
                    547:                if (transfer_buflen == 0) {
1.167     djm       548:                        ret->download_buflen = MINIMUM(limits.read_length,
                    549:                            SFTP_MAX_MSG_LENGTH - 1024);
                    550:                        ret->upload_buflen = MINIMUM(limits.write_length,
                    551:                            SFTP_MAX_MSG_LENGTH - 1024);
1.168     djm       552:                        ret->download_buflen = MAXIMUM(ret->download_buflen, 64);
                    553:                        ret->upload_buflen = MAXIMUM(ret->upload_buflen, 64);
1.167     djm       554:                        debug3("server upload/download buffer sizes "
                    555:                            "%llu / %llu; using %u / %u",
                    556:                            (unsigned long long)limits.write_length,
                    557:                            (unsigned long long)limits.read_length,
                    558:                            ret->upload_buflen, ret->download_buflen);
1.141     djm       559:                }
                    560:
                    561:                /* Use the server limit to scale down our value only */
                    562:                if (num_requests == 0 && limits.open_handles) {
                    563:                        ret->num_requests =
                    564:                            MINIMUM(DEFAULT_NUM_REQUESTS, limits.open_handles);
1.168     djm       565:                        if (ret->num_requests == 0)
                    566:                                ret->num_requests = 1;
1.167     djm       567:                        debug3("server handle limit %llu; using %u",
1.142     djm       568:                            (unsigned long long)limits.open_handles,
                    569:                            ret->num_requests);
1.141     djm       570:                }
                    571:        }
                    572:
1.23      djm       573:        /* Some filexfer v.0 servers don't support large packets */
1.141     djm       574:        if (ret->version == 0) {
                    575:                ret->download_buflen = MINIMUM(ret->download_buflen, 20480);
                    576:                ret->upload_buflen = MINIMUM(ret->upload_buflen, 20480);
                    577:        }
1.23      djm       578:
1.93      djm       579:        ret->limit_kbps = limit_kbps;
                    580:        if (ret->limit_kbps > 0) {
                    581:                bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
1.141     djm       582:                    ret->download_buflen);
1.93      djm       583:                bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
1.141     djm       584:                    ret->upload_buflen);
1.93      djm       585:        }
                    586:
                    587:        return ret;
1.23      djm       588: }
                    589:
                    590: u_int
                    591: sftp_proto_version(struct sftp_conn *conn)
                    592: {
1.93      djm       593:        return conn->version;
1.1       djm       594: }
                    595:
                    596: int
1.141     djm       597: do_limits(struct sftp_conn *conn, struct sftp_limits *limits)
                    598: {
                    599:        u_int id, msg_id;
                    600:        u_char type;
                    601:        struct sshbuf *msg;
                    602:        int r;
                    603:
                    604:        if ((conn->exts & SFTP_EXT_LIMITS) == 0) {
                    605:                error("Server does not support limits@openssh.com extension");
                    606:                return -1;
                    607:        }
                    608:
                    609:        if ((msg = sshbuf_new()) == NULL)
                    610:                fatal_f("sshbuf_new failed");
                    611:
                    612:        id = conn->msg_id++;
                    613:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                    614:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                    615:            (r = sshbuf_put_cstring(msg, "limits@openssh.com")) != 0)
                    616:                fatal_fr(r, "compose");
                    617:        send_msg(conn, msg);
                    618:        debug3("Sent message limits@openssh.com I:%u", id);
                    619:
                    620:        get_msg(conn, msg);
                    621:
                    622:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                    623:            (r = sshbuf_get_u32(msg, &msg_id)) != 0)
                    624:                fatal_fr(r, "parse");
                    625:
                    626:        debug3("Received limits reply T:%u I:%u", type, msg_id);
                    627:        if (id != msg_id)
                    628:                fatal("ID mismatch (%u != %u)", msg_id, id);
                    629:        if (type != SSH2_FXP_EXTENDED_REPLY) {
1.143     djm       630:                debug_f("expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
1.142     djm       631:                    SSH2_FXP_EXTENDED_REPLY, type);
1.143     djm       632:                /* Disable the limits extension */
                    633:                conn->exts &= ~SFTP_EXT_LIMITS;
                    634:                sshbuf_free(msg);
                    635:                return 0;
1.141     djm       636:        }
                    637:
                    638:        memset(limits, 0, sizeof(*limits));
                    639:        if ((r = sshbuf_get_u64(msg, &limits->packet_length)) != 0 ||
                    640:            (r = sshbuf_get_u64(msg, &limits->read_length)) != 0 ||
                    641:            (r = sshbuf_get_u64(msg, &limits->write_length)) != 0 ||
                    642:            (r = sshbuf_get_u64(msg, &limits->open_handles)) != 0)
                    643:                fatal_fr(r, "parse limits");
                    644:
                    645:        sshbuf_free(msg);
                    646:
                    647:        return 0;
                    648: }
                    649:
                    650: int
1.116     djm       651: do_close(struct sftp_conn *conn, const u_char *handle, u_int handle_len)
1.1       djm       652: {
                    653:        u_int id, status;
1.116     djm       654:        struct sshbuf *msg;
                    655:        int r;
1.1       djm       656:
1.116     djm       657:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       658:                fatal_f("sshbuf_new failed");
1.1       djm       659:
1.23      djm       660:        id = conn->msg_id++;
1.116     djm       661:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_CLOSE)) != 0 ||
                    662:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                    663:            (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137     djm       664:                fatal_fr(r, "parse");
1.116     djm       665:        send_msg(conn, msg);
1.33      deraadt   666:        debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
1.1       djm       667:
1.93      djm       668:        status = get_status(conn, id);
1.1       djm       669:        if (status != SSH2_FX_OK)
1.161     djm       670:                error("close remote: %s", fx2txt(status));
1.1       djm       671:
1.116     djm       672:        sshbuf_free(msg);
1.1       djm       673:
1.116     djm       674:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       675: }
                    676:
1.12      djm       677:
1.17      itojun    678: static int
1.116     djm       679: do_lsreaddir(struct sftp_conn *conn, const char *path, int print_flag,
1.12      djm       680:     SFTP_DIRENT ***dir)
1.1       djm       681: {
1.116     djm       682:        struct sshbuf *msg;
                    683:        u_int count, id, i, expected_id, ents = 0;
                    684:        size_t handle_len;
1.123     djm       685:        u_char type, *handle;
1.111     djm       686:        int status = SSH2_FX_FAILURE;
1.116     djm       687:        int r;
1.111     djm       688:
                    689:        if (dir)
                    690:                *dir = NULL;
1.1       djm       691:
1.23      djm       692:        id = conn->msg_id++;
1.1       djm       693:
1.116     djm       694:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       695:                fatal_f("sshbuf_new failed");
1.116     djm       696:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPENDIR)) != 0 ||
                    697:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                    698:            (r = sshbuf_put_cstring(msg, path)) != 0)
1.137     djm       699:                fatal_fr(r, "compose OPENDIR");
1.116     djm       700:        send_msg(conn, msg);
1.1       djm       701:
1.93      djm       702:        handle = get_handle(conn, id, &handle_len,
1.88      djm       703:            "remote readdir(\"%s\")", path);
1.96      markus    704:        if (handle == NULL) {
1.116     djm       705:                sshbuf_free(msg);
1.93      djm       706:                return -1;
1.96      markus    707:        }
1.1       djm       708:
1.12      djm       709:        if (dir) {
                    710:                ents = 0;
1.108     djm       711:                *dir = xcalloc(1, sizeof(**dir));
1.12      djm       712:                (*dir)[0] = NULL;
                    713:        }
                    714:
1.49      djm       715:        for (; !interrupted;) {
1.23      djm       716:                id = expected_id = conn->msg_id++;
1.1       djm       717:
1.33      deraadt   718:                debug3("Sending SSH2_FXP_READDIR I:%u", id);
1.1       djm       719:
1.116     djm       720:                sshbuf_reset(msg);
                    721:                if ((r = sshbuf_put_u8(msg, SSH2_FXP_READDIR)) != 0 ||
                    722:                    (r = sshbuf_put_u32(msg, id)) != 0 ||
                    723:                    (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137     djm       724:                        fatal_fr(r, "compose READDIR");
1.116     djm       725:                send_msg(conn, msg);
                    726:
                    727:                sshbuf_reset(msg);
                    728:
                    729:                get_msg(conn, msg);
                    730:
                    731:                if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                    732:                    (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm       733:                        fatal_fr(r, "parse");
1.1       djm       734:
1.33      deraadt   735:                debug3("Received reply T:%u I:%u", type, id);
1.1       djm       736:
                    737:                if (id != expected_id)
1.33      deraadt   738:                        fatal("ID mismatch (%u != %u)", id, expected_id);
1.1       djm       739:
                    740:                if (type == SSH2_FXP_STATUS) {
1.116     djm       741:                        u_int rstatus;
                    742:
                    743:                        if ((r = sshbuf_get_u32(msg, &rstatus)) != 0)
1.137     djm       744:                                fatal_fr(r, "parse status");
1.116     djm       745:                        debug3("Received SSH2_FXP_STATUS %d", rstatus);
                    746:                        if (rstatus == SSH2_FX_EOF)
1.1       djm       747:                                break;
1.116     djm       748:                        error("Couldn't read directory: %s", fx2txt(rstatus));
1.111     djm       749:                        goto out;
1.1       djm       750:                } else if (type != SSH2_FXP_NAME)
1.33      deraadt   751:                        fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.1       djm       752:                            SSH2_FXP_NAME, type);
                    753:
1.116     djm       754:                if ((r = sshbuf_get_u32(msg, &count)) != 0)
1.137     djm       755:                        fatal_fr(r, "parse count");
1.126     djm       756:                if (count > SSHBUF_SIZE_MAX)
1.137     djm       757:                        fatal_f("nonsensical number of entries");
1.7       markus    758:                if (count == 0)
                    759:                        break;
1.8       stevesk   760:                debug3("Received %d SSH2_FXP_NAME responses", count);
1.19      deraadt   761:                for (i = 0; i < count; i++) {
1.1       djm       762:                        char *filename, *longname;
1.116     djm       763:                        Attrib a;
1.1       djm       764:
1.116     djm       765:                        if ((r = sshbuf_get_cstring(msg, &filename,
                    766:                            NULL)) != 0 ||
                    767:                            (r = sshbuf_get_cstring(msg, &longname,
                    768:                            NULL)) != 0)
1.137     djm       769:                                fatal_fr(r, "parse filenames");
1.116     djm       770:                        if ((r = decode_attrib(msg, &a)) != 0) {
1.137     djm       771:                                error_fr(r, "couldn't decode attrib");
1.116     djm       772:                                free(filename);
                    773:                                free(longname);
1.135     djm       774:                                goto out;
1.116     djm       775:                        }
1.1       djm       776:
1.105     djm       777:                        if (print_flag)
1.124     schwarze  778:                                mprintf("%s\n", longname);
1.12      djm       779:
1.89      djm       780:                        /*
                    781:                         * Directory entries should never contain '/'
                    782:                         * These can be used to attack recursive ops
                    783:                         * (e.g. send '../../../../etc/passwd')
                    784:                         */
                    785:                        if (strchr(filename, '/') != NULL) {
                    786:                                error("Server sent suspect path \"%s\" "
                    787:                                    "during readdir of \"%s\"", filename, path);
1.111     djm       788:                        } else if (dir) {
1.118     deraadt   789:                                *dir = xreallocarray(*dir, ents + 2, sizeof(**dir));
1.108     djm       790:                                (*dir)[ents] = xcalloc(1, sizeof(***dir));
1.12      djm       791:                                (*dir)[ents]->filename = xstrdup(filename);
                    792:                                (*dir)[ents]->longname = xstrdup(longname);
1.116     djm       793:                                memcpy(&(*dir)[ents]->a, &a, sizeof(a));
1.12      djm       794:                                (*dir)[++ents] = NULL;
                    795:                        }
1.98      djm       796:                        free(filename);
                    797:                        free(longname);
1.1       djm       798:                }
                    799:        }
1.111     djm       800:        status = 0;
1.1       djm       801:
1.111     djm       802:  out:
1.116     djm       803:        sshbuf_free(msg);
1.23      djm       804:        do_close(conn, handle, handle_len);
1.98      djm       805:        free(handle);
1.1       djm       806:
1.111     djm       807:        if (status != 0 && dir != NULL) {
                    808:                /* Don't return results on error */
                    809:                free_sftp_dirents(*dir);
                    810:                *dir = NULL;
                    811:        } else if (interrupted && dir != NULL && *dir != NULL) {
                    812:                /* Don't return partial matches on interrupt */
1.49      djm       813:                free_sftp_dirents(*dir);
1.108     djm       814:                *dir = xcalloc(1, sizeof(**dir));
1.49      djm       815:                **dir = NULL;
                    816:        }
                    817:
1.129     djm       818:        return status == SSH2_FX_OK ? 0 : -1;
1.12      djm       819: }
                    820:
                    821: int
1.116     djm       822: do_readdir(struct sftp_conn *conn, const char *path, SFTP_DIRENT ***dir)
1.12      djm       823: {
1.23      djm       824:        return(do_lsreaddir(conn, path, 0, dir));
1.12      djm       825: }
                    826:
                    827: void free_sftp_dirents(SFTP_DIRENT **s)
                    828: {
                    829:        int i;
1.19      deraadt   830:
1.111     djm       831:        if (s == NULL)
                    832:                return;
1.19      deraadt   833:        for (i = 0; s[i]; i++) {
1.98      djm       834:                free(s[i]->filename);
                    835:                free(s[i]->longname);
                    836:                free(s[i]);
1.12      djm       837:        }
1.98      djm       838:        free(s);
1.12      djm       839: }
                    840:
                    841: int
1.116     djm       842: do_rm(struct sftp_conn *conn, const char *path)
1.1       djm       843: {
                    844:        u_int status, id;
                    845:
                    846:        debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
                    847:
1.23      djm       848:        id = conn->msg_id++;
1.93      djm       849:        send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
                    850:        status = get_status(conn, id);
1.1       djm       851:        if (status != SSH2_FX_OK)
1.161     djm       852:                error("remote delete %s: %s", path, fx2txt(status));
1.116     djm       853:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       854: }
                    855:
                    856: int
1.116     djm       857: do_mkdir(struct sftp_conn *conn, const char *path, Attrib *a, int print_flag)
1.1       djm       858: {
                    859:        u_int status, id;
                    860:
1.161     djm       861:        debug2("Sending SSH2_FXP_MKDIR \"%s\"", path);
                    862:
1.23      djm       863:        id = conn->msg_id++;
1.93      djm       864:        send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
1.1       djm       865:            strlen(path), a);
                    866:
1.93      djm       867:        status = get_status(conn, id);
1.105     djm       868:        if (status != SSH2_FX_OK && print_flag)
1.161     djm       869:                error("remote mkdir \"%s\": %s", path, fx2txt(status));
1.1       djm       870:
1.116     djm       871:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       872: }
                    873:
                    874: int
1.116     djm       875: do_rmdir(struct sftp_conn *conn, const char *path)
1.1       djm       876: {
                    877:        u_int status, id;
                    878:
1.161     djm       879:        debug2("Sending SSH2_FXP_RMDIR \"%s\"", path);
                    880:
1.23      djm       881:        id = conn->msg_id++;
1.93      djm       882:        send_string_request(conn, id, SSH2_FXP_RMDIR, path,
1.23      djm       883:            strlen(path));
1.1       djm       884:
1.93      djm       885:        status = get_status(conn, id);
1.1       djm       886:        if (status != SSH2_FX_OK)
1.161     djm       887:                error("remote rmdir \"%s\": %s", path, fx2txt(status));
1.1       djm       888:
1.116     djm       889:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       890: }
                    891:
                    892: Attrib *
1.116     djm       893: do_stat(struct sftp_conn *conn, const char *path, int quiet)
1.1       djm       894: {
                    895:        u_int id;
                    896:
1.161     djm       897:        debug2("Sending SSH2_FXP_STAT \"%s\"", path);
                    898:
1.23      djm       899:        id = conn->msg_id++;
                    900:
1.93      djm       901:        send_string_request(conn, id,
1.28      markus    902:            conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
1.23      djm       903:            path, strlen(path));
                    904:
1.93      djm       905:        return(get_decode_stat(conn, id, quiet));
1.1       djm       906: }
                    907:
                    908: Attrib *
1.116     djm       909: do_lstat(struct sftp_conn *conn, const char *path, int quiet)
1.1       djm       910: {
                    911:        u_int id;
                    912:
1.23      djm       913:        if (conn->version == 0) {
                    914:                if (quiet)
                    915:                        debug("Server version does not support lstat operation");
                    916:                else
1.43      itojun    917:                        logit("Server version does not support lstat operation");
1.30      markus    918:                return(do_stat(conn, path, quiet));
1.23      djm       919:        }
                    920:
                    921:        id = conn->msg_id++;
1.93      djm       922:        send_string_request(conn, id, SSH2_FXP_LSTAT, path,
1.23      djm       923:            strlen(path));
                    924:
1.93      djm       925:        return(get_decode_stat(conn, id, quiet));
1.1       djm       926: }
                    927:
1.78      chl       928: #ifdef notyet
1.1       djm       929: Attrib *
1.116     djm       930: do_fstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
                    931:     int quiet)
1.1       djm       932: {
                    933:        u_int id;
                    934:
1.161     djm       935:        debug2("Sending SSH2_FXP_FSTAT \"%s\"");
                    936:
1.23      djm       937:        id = conn->msg_id++;
1.93      djm       938:        send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
1.23      djm       939:            handle_len);
                    940:
1.93      djm       941:        return(get_decode_stat(conn, id, quiet));
1.1       djm       942: }
1.78      chl       943: #endif
1.1       djm       944:
                    945: int
1.116     djm       946: do_setstat(struct sftp_conn *conn, const char *path, Attrib *a)
1.1       djm       947: {
                    948:        u_int status, id;
                    949:
1.161     djm       950:        debug2("Sending SSH2_FXP_SETSTAT \"%s\"", path);
                    951:
1.23      djm       952:        id = conn->msg_id++;
1.93      djm       953:        send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
1.1       djm       954:            strlen(path), a);
                    955:
1.93      djm       956:        status = get_status(conn, id);
1.1       djm       957:        if (status != SSH2_FX_OK)
1.161     djm       958:                error("remote setstat \"%s\": %s", path, fx2txt(status));
1.1       djm       959:
1.116     djm       960:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       961: }
                    962:
                    963: int
1.116     djm       964: do_fsetstat(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
1.1       djm       965:     Attrib *a)
                    966: {
                    967:        u_int status, id;
                    968:
1.161     djm       969:        debug2("Sending SSH2_FXP_FSETSTAT");
                    970:
1.23      djm       971:        id = conn->msg_id++;
1.93      djm       972:        send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
1.1       djm       973:            handle_len, a);
                    974:
1.93      djm       975:        status = get_status(conn, id);
1.1       djm       976:        if (status != SSH2_FX_OK)
1.161     djm       977:                error("remote fsetstat: %s", fx2txt(status));
1.1       djm       978:
1.116     djm       979:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm       980: }
                    981:
1.154     djm       982: /* Implements both the realpath and expand-path operations */
                    983: static char *
                    984: do_realpath_expand(struct sftp_conn *conn, const char *path, int expand)
1.1       djm       985: {
1.116     djm       986:        struct sshbuf *msg;
                    987:        u_int expected_id, count, id;
1.1       djm       988:        char *filename, *longname;
1.116     djm       989:        Attrib a;
                    990:        u_char type;
                    991:        int r;
1.154     djm       992:        const char *what = "SSH2_FXP_REALPATH";
1.1       djm       993:
1.154     djm       994:        if (expand)
                    995:                what = "expand-path@openssh.com";
1.116     djm       996:        if ((msg = sshbuf_new()) == NULL)
1.137     djm       997:                fatal_f("sshbuf_new failed");
1.1       djm       998:
1.154     djm       999:        expected_id = id = conn->msg_id++;
                   1000:        if (expand) {
1.161     djm      1001:                debug2("Sending SSH2_FXP_EXTENDED(expand-path@openssh.com) "
                   1002:                    "\"%s\"", path);
1.154     djm      1003:                if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1004:                    (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1005:                    (r = sshbuf_put_cstring(msg,
                   1006:                    "expand-path@openssh.com")) != 0 ||
                   1007:                    (r = sshbuf_put_cstring(msg, path)) != 0)
                   1008:                        fatal_fr(r, "compose %s", what);
                   1009:                send_msg(conn, msg);
                   1010:        } else {
1.161     djm      1011:                debug2("Sending SSH2_FXP_REALPATH \"%s\"", path);
1.154     djm      1012:                send_string_request(conn, id, SSH2_FXP_REALPATH,
                   1013:                    path, strlen(path));
                   1014:        }
1.116     djm      1015:        get_msg(conn, msg);
                   1016:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   1017:            (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm      1018:                fatal_fr(r, "parse");
1.1       djm      1019:
                   1020:        if (id != expected_id)
1.33      deraadt  1021:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.1       djm      1022:
                   1023:        if (type == SSH2_FXP_STATUS) {
1.116     djm      1024:                u_int status;
1.160     djm      1025:                char *errmsg;
1.1       djm      1026:
1.160     djm      1027:                if ((r = sshbuf_get_u32(msg, &status)) != 0 ||
                   1028:                    (r = sshbuf_get_cstring(msg, &errmsg, NULL)) != 0)
1.137     djm      1029:                        fatal_fr(r, "parse status");
1.160     djm      1030:                error("%s %s: %s", expand ? "expand" : "realpath",
                   1031:                    path, *errmsg == '\0' ? fx2txt(status) : errmsg);
                   1032:                free(errmsg);
1.116     djm      1033:                sshbuf_free(msg);
1.91      djm      1034:                return NULL;
1.1       djm      1035:        } else if (type != SSH2_FXP_NAME)
1.33      deraadt  1036:                fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.1       djm      1037:                    SSH2_FXP_NAME, type);
                   1038:
1.116     djm      1039:        if ((r = sshbuf_get_u32(msg, &count)) != 0)
1.137     djm      1040:                fatal_fr(r, "parse count");
1.1       djm      1041:        if (count != 1)
1.154     djm      1042:                fatal("Got multiple names (%d) from %s", count, what);
1.1       djm      1043:
1.116     djm      1044:        if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
                   1045:            (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
                   1046:            (r = decode_attrib(msg, &a)) != 0)
1.137     djm      1047:                fatal_fr(r, "parse filename/attrib");
1.1       djm      1048:
1.154     djm      1049:        debug3("%s %s -> %s", what, path, filename);
1.1       djm      1050:
1.98      djm      1051:        free(longname);
1.1       djm      1052:
1.116     djm      1053:        sshbuf_free(msg);
1.1       djm      1054:
                   1055:        return(filename);
1.154     djm      1056: }
                   1057:
                   1058: char *
                   1059: do_realpath(struct sftp_conn *conn, const char *path)
                   1060: {
                   1061:        return do_realpath_expand(conn, path, 0);
                   1062: }
                   1063:
                   1064: int
                   1065: can_expand_path(struct sftp_conn *conn)
                   1066: {
                   1067:        return (conn->exts & SFTP_EXT_PATH_EXPAND) != 0;
                   1068: }
                   1069:
                   1070: char *
                   1071: do_expand_path(struct sftp_conn *conn, const char *path)
                   1072: {
                   1073:        if (!can_expand_path(conn)) {
                   1074:                debug3_f("no server support, fallback to realpath");
                   1075:                return do_realpath_expand(conn, path, 0);
                   1076:        }
                   1077:        return do_realpath_expand(conn, path, 1);
1.162     djm      1078: }
                   1079:
                   1080: int
                   1081: do_copy(struct sftp_conn *conn, const char *oldpath, const char *newpath)
                   1082: {
                   1083:        Attrib junk, *a;
                   1084:        struct sshbuf *msg;
                   1085:        u_char *old_handle, *new_handle;
                   1086:        u_int mode, status, id;
                   1087:        size_t old_handle_len, new_handle_len;
                   1088:        int r;
                   1089:
                   1090:        /* Return if the extension is not supported */
                   1091:        if ((conn->exts & SFTP_EXT_COPY_DATA) == 0) {
                   1092:                error("Server does not support copy-data extension");
                   1093:                return -1;
                   1094:        }
                   1095:
                   1096:        /* Make sure the file exists, and we can copy its perms */
                   1097:        if ((a = do_stat(conn, oldpath, 0)) == NULL)
                   1098:                return -1;
                   1099:
                   1100:        /* Do not preserve set[ug]id here, as we do not preserve ownership */
                   1101:        if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
                   1102:                mode = a->perm & 0777;
                   1103:
                   1104:                if (!S_ISREG(a->perm)) {
                   1105:                        error("Cannot copy non-regular file: %s", oldpath);
                   1106:                        return -1;
                   1107:                }
                   1108:        } else {
                   1109:                /* NB: The user's umask will apply to this */
                   1110:                mode = 0666;
                   1111:        }
                   1112:
                   1113:        /* Set up the new perms for the new file */
                   1114:        attrib_clear(a);
                   1115:        a->perm = mode;
                   1116:        a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
                   1117:
                   1118:        if ((msg = sshbuf_new()) == NULL)
                   1119:                fatal("%s: sshbuf_new failed", __func__);
                   1120:
                   1121:        attrib_clear(&junk); /* Send empty attributes */
                   1122:
                   1123:        /* Open the old file for reading */
                   1124:        id = conn->msg_id++;
                   1125:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
                   1126:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1127:            (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
                   1128:            (r = sshbuf_put_u32(msg, SSH2_FXF_READ)) != 0 ||
                   1129:            (r = encode_attrib(msg, &junk)) != 0)
                   1130:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                   1131:        send_msg(conn, msg);
                   1132:        debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, oldpath);
                   1133:
                   1134:        sshbuf_reset(msg);
                   1135:
                   1136:        old_handle = get_handle(conn, id, &old_handle_len,
                   1137:            "remote open(\"%s\")", oldpath);
                   1138:        if (old_handle == NULL) {
                   1139:                sshbuf_free(msg);
                   1140:                return -1;
                   1141:        }
                   1142:
                   1143:        /* Open the new file for writing */
                   1144:        id = conn->msg_id++;
                   1145:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
                   1146:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1147:            (r = sshbuf_put_cstring(msg, newpath)) != 0 ||
                   1148:            (r = sshbuf_put_u32(msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|
                   1149:            SSH2_FXF_TRUNC)) != 0 ||
                   1150:            (r = encode_attrib(msg, a)) != 0)
                   1151:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                   1152:        send_msg(conn, msg);
                   1153:        debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, newpath);
                   1154:
                   1155:        sshbuf_reset(msg);
                   1156:
                   1157:        new_handle = get_handle(conn, id, &new_handle_len,
                   1158:            "remote open(\"%s\")", newpath);
                   1159:        if (new_handle == NULL) {
                   1160:                sshbuf_free(msg);
                   1161:                free(old_handle);
                   1162:                return -1;
                   1163:        }
                   1164:
                   1165:        /* Copy the file data */
                   1166:        id = conn->msg_id++;
                   1167:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1168:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1169:            (r = sshbuf_put_cstring(msg, "copy-data")) != 0 ||
                   1170:            (r = sshbuf_put_string(msg, old_handle, old_handle_len)) != 0 ||
                   1171:            (r = sshbuf_put_u64(msg, 0)) != 0 ||
                   1172:            (r = sshbuf_put_u64(msg, 0)) != 0 ||
                   1173:            (r = sshbuf_put_string(msg, new_handle, new_handle_len)) != 0 ||
                   1174:            (r = sshbuf_put_u64(msg, 0)) != 0)
                   1175:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                   1176:        send_msg(conn, msg);
                   1177:        debug3("Sent message copy-data \"%s\" 0 0 -> \"%s\" 0",
                   1178:               oldpath, newpath);
                   1179:
                   1180:        status = get_status(conn, id);
                   1181:        if (status != SSH2_FX_OK)
                   1182:                error("Couldn't copy file \"%s\" to \"%s\": %s", oldpath,
                   1183:                    newpath, fx2txt(status));
                   1184:
                   1185:        /* Clean up everything */
                   1186:        sshbuf_free(msg);
                   1187:        do_close(conn, old_handle, old_handle_len);
                   1188:        do_close(conn, new_handle, new_handle_len);
                   1189:        free(old_handle);
                   1190:        free(new_handle);
                   1191:
                   1192:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm      1193: }
                   1194:
                   1195: int
1.116     djm      1196: do_rename(struct sftp_conn *conn, const char *oldpath, const char *newpath,
1.102     djm      1197:     int force_legacy)
1.1       djm      1198: {
1.116     djm      1199:        struct sshbuf *msg;
1.1       djm      1200:        u_int status, id;
1.116     djm      1201:        int r, use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
1.1       djm      1202:
1.116     djm      1203:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1204:                fatal_f("sshbuf_new failed");
1.1       djm      1205:
                   1206:        /* Send rename request */
1.23      djm      1207:        id = conn->msg_id++;
1.102     djm      1208:        if (use_ext) {
1.161     djm      1209:                debug2("Sending SSH2_FXP_EXTENDED(posix-rename@openssh.com) "
                   1210:                    "\"%s\" to \"%s\"", oldpath, newpath);
1.116     djm      1211:                if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1212:                    (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1213:                    (r = sshbuf_put_cstring(msg,
                   1214:                    "posix-rename@openssh.com")) != 0)
1.137     djm      1215:                        fatal_fr(r, "compose posix-rename");
1.81      djm      1216:        } else {
1.161     djm      1217:                debug2("Sending SSH2_FXP_RENAME \"%s\" to \"%s\"",
                   1218:                    oldpath, newpath);
1.116     djm      1219:                if ((r = sshbuf_put_u8(msg, SSH2_FXP_RENAME)) != 0 ||
                   1220:                    (r = sshbuf_put_u32(msg, id)) != 0)
1.137     djm      1221:                        fatal_fr(r, "compose rename");
1.116     djm      1222:        }
                   1223:        if ((r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
                   1224:            (r = sshbuf_put_cstring(msg, newpath)) != 0)
1.137     djm      1225:                fatal_fr(r, "compose paths");
1.116     djm      1226:        send_msg(conn, msg);
1.81      djm      1227:        debug3("Sent message %s \"%s\" -> \"%s\"",
1.116     djm      1228:            use_ext ? "posix-rename@openssh.com" :
                   1229:            "SSH2_FXP_RENAME", oldpath, newpath);
                   1230:        sshbuf_free(msg);
1.1       djm      1231:
1.93      djm      1232:        status = get_status(conn, id);
1.1       djm      1233:        if (status != SSH2_FX_OK)
1.161     djm      1234:                error("remote rename \"%s\" to \"%s\": %s", oldpath,
1.94      djm      1235:                    newpath, fx2txt(status));
                   1236:
1.116     djm      1237:        return status == SSH2_FX_OK ? 0 : -1;
1.94      djm      1238: }
                   1239:
                   1240: int
1.116     djm      1241: do_hardlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
1.94      djm      1242: {
1.116     djm      1243:        struct sshbuf *msg;
1.94      djm      1244:        u_int status, id;
1.116     djm      1245:        int r;
1.94      djm      1246:
                   1247:        if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
                   1248:                error("Server does not support hardlink@openssh.com extension");
                   1249:                return -1;
                   1250:        }
1.161     djm      1251:        debug2("Sending SSH2_FXP_EXTENDED(hardlink@openssh.com) "
                   1252:            "\"%s\" to \"%s\"", oldpath, newpath);
1.94      djm      1253:
1.116     djm      1254:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1255:                fatal_f("sshbuf_new failed");
1.95      markus   1256:
                   1257:        /* Send link request */
                   1258:        id = conn->msg_id++;
1.116     djm      1259:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1260:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1261:            (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
                   1262:            (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
                   1263:            (r = sshbuf_put_cstring(msg, newpath)) != 0)
1.137     djm      1264:                fatal_fr(r, "compose");
1.116     djm      1265:        send_msg(conn, msg);
1.94      djm      1266:        debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
1.142     djm      1267:            oldpath, newpath);
1.116     djm      1268:        sshbuf_free(msg);
1.94      djm      1269:
                   1270:        status = get_status(conn, id);
                   1271:        if (status != SSH2_FX_OK)
1.161     djm      1272:                error("remote link \"%s\" to \"%s\": %s", oldpath,
1.23      djm      1273:                    newpath, fx2txt(status));
1.1       djm      1274:
1.116     djm      1275:        return status == SSH2_FX_OK ? 0 : -1;
1.11      djm      1276: }
                   1277:
                   1278: int
1.116     djm      1279: do_symlink(struct sftp_conn *conn, const char *oldpath, const char *newpath)
1.11      djm      1280: {
1.116     djm      1281:        struct sshbuf *msg;
1.11      djm      1282:        u_int status, id;
1.116     djm      1283:        int r;
1.11      djm      1284:
1.23      djm      1285:        if (conn->version < 3) {
                   1286:                error("This server does not support the symlink operation");
                   1287:                return(SSH2_FX_OP_UNSUPPORTED);
                   1288:        }
1.161     djm      1289:        debug2("Sending SSH2_FXP_SYMLINK \"%s\" to \"%s\"", oldpath, newpath);
1.23      djm      1290:
1.116     djm      1291:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1292:                fatal_f("sshbuf_new failed");
1.11      djm      1293:
1.48      djm      1294:        /* Send symlink request */
1.23      djm      1295:        id = conn->msg_id++;
1.116     djm      1296:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_SYMLINK)) != 0 ||
                   1297:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1298:            (r = sshbuf_put_cstring(msg, oldpath)) != 0 ||
                   1299:            (r = sshbuf_put_cstring(msg, newpath)) != 0)
1.137     djm      1300:                fatal_fr(r, "compose");
1.116     djm      1301:        send_msg(conn, msg);
1.11      djm      1302:        debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
                   1303:            newpath);
1.116     djm      1304:        sshbuf_free(msg);
1.11      djm      1305:
1.93      djm      1306:        status = get_status(conn, id);
1.11      djm      1307:        if (status != SSH2_FX_OK)
1.161     djm      1308:                error("remote symlink file \"%s\" to \"%s\": %s", oldpath,
1.23      djm      1309:                    newpath, fx2txt(status));
1.11      djm      1310:
1.116     djm      1311:        return status == SSH2_FX_OK ? 0 : -1;
1.11      djm      1312: }
                   1313:
1.107     djm      1314: int
1.116     djm      1315: do_fsync(struct sftp_conn *conn, u_char *handle, u_int handle_len)
1.107     djm      1316: {
1.116     djm      1317:        struct sshbuf *msg;
1.107     djm      1318:        u_int status, id;
1.116     djm      1319:        int r;
1.107     djm      1320:
                   1321:        /* Silently return if the extension is not supported */
                   1322:        if ((conn->exts & SFTP_EXT_FSYNC) == 0)
                   1323:                return -1;
1.161     djm      1324:        debug2("Sending SSH2_FXP_EXTENDED(fsync@openssh.com)");
1.107     djm      1325:
                   1326:        /* Send fsync request */
1.116     djm      1327:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1328:                fatal_f("sshbuf_new failed");
1.107     djm      1329:        id = conn->msg_id++;
1.116     djm      1330:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1331:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1332:            (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
                   1333:            (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137     djm      1334:                fatal_fr(r, "compose");
1.116     djm      1335:        send_msg(conn, msg);
1.107     djm      1336:        debug3("Sent message fsync@openssh.com I:%u", id);
1.116     djm      1337:        sshbuf_free(msg);
1.107     djm      1338:
                   1339:        status = get_status(conn, id);
                   1340:        if (status != SSH2_FX_OK)
1.161     djm      1341:                error("remote fsync: %s", fx2txt(status));
1.107     djm      1342:
1.129     djm      1343:        return status == SSH2_FX_OK ? 0 : -1;
1.107     djm      1344: }
                   1345:
1.78      chl      1346: #ifdef notyet
1.11      djm      1347: char *
1.116     djm      1348: do_readlink(struct sftp_conn *conn, const char *path)
1.11      djm      1349: {
1.116     djm      1350:        struct sshbuf *msg;
                   1351:        u_int expected_id, count, id;
1.11      djm      1352:        char *filename, *longname;
1.116     djm      1353:        Attrib a;
                   1354:        u_char type;
                   1355:        int r;
1.11      djm      1356:
1.161     djm      1357:        debug2("Sending SSH2_FXP_READLINK \"%s\"", path);
                   1358:
1.23      djm      1359:        expected_id = id = conn->msg_id++;
1.93      djm      1360:        send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
1.11      djm      1361:
1.116     djm      1362:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1363:                fatal_f("sshbuf_new failed");
1.11      djm      1364:
1.116     djm      1365:        get_msg(conn, msg);
                   1366:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   1367:            (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm      1368:                fatal_fr(r, "parse");
1.11      djm      1369:
                   1370:        if (id != expected_id)
1.33      deraadt  1371:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.11      djm      1372:
                   1373:        if (type == SSH2_FXP_STATUS) {
1.116     djm      1374:                u_int status;
1.11      djm      1375:
1.116     djm      1376:                if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm      1377:                        fatal_fr(r, "parse status");
1.11      djm      1378:                error("Couldn't readlink: %s", fx2txt(status));
1.116     djm      1379:                sshbuf_free(msg);
1.11      djm      1380:                return(NULL);
                   1381:        } else if (type != SSH2_FXP_NAME)
1.33      deraadt  1382:                fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.11      djm      1383:                    SSH2_FXP_NAME, type);
                   1384:
1.116     djm      1385:        if ((r = sshbuf_get_u32(msg, &count)) != 0)
1.137     djm      1386:                fatal_fr(r, "parse count");
1.11      djm      1387:        if (count != 1)
                   1388:                fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
                   1389:
1.116     djm      1390:        if ((r = sshbuf_get_cstring(msg, &filename, NULL)) != 0 ||
                   1391:            (r = sshbuf_get_cstring(msg, &longname, NULL)) != 0 ||
                   1392:            (r = decode_attrib(msg, &a)) != 0)
1.137     djm      1393:                fatal_fr(r, "parse filenames/attrib");
1.11      djm      1394:
                   1395:        debug3("SSH_FXP_READLINK %s -> %s", path, filename);
                   1396:
1.98      djm      1397:        free(longname);
1.11      djm      1398:
1.116     djm      1399:        sshbuf_free(msg);
1.11      djm      1400:
1.116     djm      1401:        return filename;
1.82      djm      1402: }
                   1403: #endif
                   1404:
                   1405: int
1.84      dtucker  1406: do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
1.82      djm      1407:     int quiet)
                   1408: {
1.116     djm      1409:        struct sshbuf *msg;
1.82      djm      1410:        u_int id;
1.116     djm      1411:        int r;
1.82      djm      1412:
                   1413:        if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
                   1414:                error("Server does not support statvfs@openssh.com extension");
                   1415:                return -1;
                   1416:        }
                   1417:
1.161     djm      1418:        debug2("Sending SSH2_FXP_EXTENDED(statvfs@openssh.com) \"%s\"", path);
                   1419:
1.82      djm      1420:        id = conn->msg_id++;
                   1421:
1.116     djm      1422:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1423:                fatal_f("sshbuf_new failed");
1.116     djm      1424:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1425:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1426:            (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
                   1427:            (r = sshbuf_put_cstring(msg, path)) != 0)
1.137     djm      1428:                fatal_fr(r, "compose");
1.116     djm      1429:        send_msg(conn, msg);
                   1430:        sshbuf_free(msg);
1.82      djm      1431:
1.93      djm      1432:        return get_decode_statvfs(conn, st, id, quiet);
1.82      djm      1433: }
                   1434:
                   1435: #ifdef notyet
                   1436: int
1.116     djm      1437: do_fstatvfs(struct sftp_conn *conn, const u_char *handle, u_int handle_len,
1.84      dtucker  1438:     struct sftp_statvfs *st, int quiet)
1.82      djm      1439: {
1.116     djm      1440:        struct sshbuf *msg;
1.82      djm      1441:        u_int id;
                   1442:
                   1443:        if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
                   1444:                error("Server does not support fstatvfs@openssh.com extension");
                   1445:                return -1;
                   1446:        }
                   1447:
1.161     djm      1448:        debug2("Sending SSH2_FXP_EXTENDED(fstatvfs@openssh.com)");
                   1449:
1.82      djm      1450:        id = conn->msg_id++;
                   1451:
1.116     djm      1452:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1453:                fatal_f("sshbuf_new failed");
1.116     djm      1454:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1455:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1456:            (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
                   1457:            (r = sshbuf_put_string(msg, handle, handle_len)) != 0)
1.137     djm      1458:                fatal_fr(r, "compose");
1.116     djm      1459:        send_msg(conn, msg);
                   1460:        sshbuf_free(msg);
1.82      djm      1461:
1.93      djm      1462:        return get_decode_statvfs(conn, st, id, quiet);
1.1       djm      1463: }
1.78      chl      1464: #endif
1.1       djm      1465:
1.131     djm      1466: int
                   1467: do_lsetstat(struct sftp_conn *conn, const char *path, Attrib *a)
                   1468: {
                   1469:        struct sshbuf *msg;
                   1470:        u_int status, id;
                   1471:        int r;
                   1472:
                   1473:        if ((conn->exts & SFTP_EXT_LSETSTAT) == 0) {
                   1474:                error("Server does not support lsetstat@openssh.com extension");
                   1475:                return -1;
                   1476:        }
                   1477:
1.161     djm      1478:        debug2("Sending SSH2_FXP_EXTENDED(lsetstat@openssh.com) \"%s\"", path);
                   1479:
1.131     djm      1480:        id = conn->msg_id++;
                   1481:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1482:                fatal_f("sshbuf_new failed");
1.131     djm      1483:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   1484:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1485:            (r = sshbuf_put_cstring(msg, "lsetstat@openssh.com")) != 0 ||
                   1486:            (r = sshbuf_put_cstring(msg, path)) != 0 ||
                   1487:            (r = encode_attrib(msg, a)) != 0)
1.137     djm      1488:                fatal_fr(r, "compose");
1.131     djm      1489:        send_msg(conn, msg);
                   1490:        sshbuf_free(msg);
                   1491:
                   1492:        status = get_status(conn, id);
                   1493:        if (status != SSH2_FX_OK)
1.161     djm      1494:                error("remote lsetstat \"%s\": %s", path, fx2txt(status));
1.131     djm      1495:
                   1496:        return status == SSH2_FX_OK ? 0 : -1;
                   1497: }
                   1498:
1.21      djm      1499: static void
1.93      djm      1500: send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
1.116     djm      1501:     u_int len, const u_char *handle, u_int handle_len)
1.21      djm      1502: {
1.116     djm      1503:        struct sshbuf *msg;
                   1504:        int r;
1.28      markus   1505:
1.116     djm      1506:        if ((msg = sshbuf_new()) == NULL)
1.137     djm      1507:                fatal_f("sshbuf_new failed");
1.116     djm      1508:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_READ)) != 0 ||
                   1509:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1510:            (r = sshbuf_put_string(msg, handle, handle_len)) != 0 ||
                   1511:            (r = sshbuf_put_u64(msg, offset)) != 0 ||
                   1512:            (r = sshbuf_put_u32(msg, len)) != 0)
1.137     djm      1513:                fatal_fr(r, "compose");
1.116     djm      1514:        send_msg(conn, msg);
                   1515:        sshbuf_free(msg);
1.28      markus   1516: }
1.21      djm      1517:
1.144     djm      1518: static int
                   1519: send_open(struct sftp_conn *conn, const char *path, const char *tag,
                   1520:     u_int openmode, Attrib *a, u_char **handlep, size_t *handle_lenp)
                   1521: {
                   1522:        Attrib junk;
                   1523:        u_char *handle;
                   1524:        size_t handle_len;
                   1525:        struct sshbuf *msg;
                   1526:        int r;
                   1527:        u_int id;
                   1528:
1.161     djm      1529:        debug2("Sending SSH2_FXP_OPEN \"%s\"", path);
                   1530:
1.144     djm      1531:        *handlep = NULL;
                   1532:        *handle_lenp = 0;
                   1533:
                   1534:        if (a == NULL) {
                   1535:                attrib_clear(&junk); /* Send empty attributes */
                   1536:                a = &junk;
                   1537:        }
                   1538:        /* Send open request */
                   1539:        if ((msg = sshbuf_new()) == NULL)
                   1540:                fatal_f("sshbuf_new failed");
                   1541:        id = conn->msg_id++;
                   1542:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_OPEN)) != 0 ||
                   1543:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   1544:            (r = sshbuf_put_cstring(msg, path)) != 0 ||
                   1545:            (r = sshbuf_put_u32(msg, openmode)) != 0 ||
                   1546:            (r = encode_attrib(msg, a)) != 0)
                   1547:                fatal_fr(r, "compose %s open", tag);
                   1548:        send_msg(conn, msg);
                   1549:        sshbuf_free(msg);
                   1550:        debug3("Sent %s message SSH2_FXP_OPEN I:%u P:%s M:0x%04x",
                   1551:            tag, id, path, openmode);
                   1552:        if ((handle = get_handle(conn, id, &handle_len,
1.159     djm      1553:            "%s open \"%s\"", tag, path)) == NULL)
1.144     djm      1554:                return -1;
                   1555:        /* success */
                   1556:        *handlep = handle;
                   1557:        *handle_lenp = handle_len;
                   1558:        return 0;
                   1559: }
                   1560:
1.153     djm      1561: static const char *
                   1562: progress_meter_path(const char *path)
                   1563: {
                   1564:        const char *progresspath;
                   1565:
                   1566:        if ((progresspath = strrchr(path, '/')) == NULL)
                   1567:                return path;
                   1568:        progresspath++;
                   1569:        if (*progresspath == '\0')
                   1570:                return path;
                   1571:        return progresspath;
                   1572: }
                   1573:
1.1       djm      1574: int
1.116     djm      1575: do_download(struct sftp_conn *conn, const char *remote_path,
                   1576:     const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
1.163     djm      1577:     int fsync_flag, int inplace_flag)
1.1       djm      1578: {
1.116     djm      1579:        struct sshbuf *msg;
                   1580:        u_char *handle;
                   1581:        int local_fd = -1, write_error;
1.134     djm      1582:        int read_error, write_errno, lmodified = 0, reordered = 0, r;
1.171   ! djm      1583:        u_int64_t offset = 0, size, highwater = 0, maxack = 0;
1.116     djm      1584:        u_int mode, id, buflen, num_req, max_req, status = SSH2_FX_OK;
1.39      fgsch    1585:        off_t progress_counter;
1.116     djm      1586:        size_t handle_len;
1.101     djm      1587:        struct stat st;
1.146     djm      1588:        struct requests requests;
1.21      djm      1589:        struct request *req;
1.116     djm      1590:        u_char type;
1.21      djm      1591:
1.161     djm      1592:        debug2_f("download remote \"%s\" to local \"%s\"",
                   1593:            remote_path, local_path);
                   1594:
1.21      djm      1595:        TAILQ_INIT(&requests);
1.1       djm      1596:
1.89      djm      1597:        if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
                   1598:                return -1;
1.1       djm      1599:
1.86      djm      1600:        /* Do not preserve set[ug]id here, as we do not preserve ownership */
1.1       djm      1601:        if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1.38      djm      1602:                mode = a->perm & 0777;
1.1       djm      1603:        else
                   1604:                mode = 0666;
                   1605:
1.14      djm      1606:        if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1.41      djm      1607:            (!S_ISREG(a->perm))) {
1.161     djm      1608:                error("download %s: not a regular file", remote_path);
1.14      djm      1609:                return(-1);
                   1610:        }
                   1611:
1.21      djm      1612:        if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
                   1613:                size = a->size;
                   1614:        else
                   1615:                size = 0;
                   1616:
1.141     djm      1617:        buflen = conn->download_buflen;
1.1       djm      1618:
                   1619:        /* Send open request */
1.144     djm      1620:        if (send_open(conn, remote_path, "remote", SSH2_FXF_READ, NULL,
                   1621:            &handle, &handle_len) != 0)
                   1622:                return -1;
1.1       djm      1623:
1.163     djm      1624:        local_fd = open(local_path, O_WRONLY | O_CREAT |
                   1625:        ((resume_flag || inplace_flag) ? 0 : O_TRUNC), mode | S_IWUSR);
1.23      djm      1626:        if (local_fd == -1) {
1.161     djm      1627:                error("open local \"%s\": %s", local_path, strerror(errno));
1.101     djm      1628:                goto fail;
                   1629:        }
1.105     djm      1630:        if (resume_flag) {
1.101     djm      1631:                if (fstat(local_fd, &st) == -1) {
1.161     djm      1632:                        error("stat local \"%s\": %s",
1.101     djm      1633:                            local_path, strerror(errno));
                   1634:                        goto fail;
                   1635:                }
1.113     djm      1636:                if (st.st_size < 0) {
                   1637:                        error("\"%s\" has negative size", local_path);
                   1638:                        goto fail;
                   1639:                }
                   1640:                if ((u_int64_t)st.st_size > size) {
1.101     djm      1641:                        error("Unable to resume download of \"%s\": "
                   1642:                            "local file is larger than remote", local_path);
                   1643:  fail:
                   1644:                        do_close(conn, handle, handle_len);
                   1645:                        free(handle);
1.110     djm      1646:                        if (local_fd != -1)
                   1647:                                close(local_fd);
1.101     djm      1648:                        return -1;
                   1649:                }
1.171   ! djm      1650:                offset = highwater = maxack = st.st_size;
1.23      djm      1651:        }
                   1652:
1.1       djm      1653:        /* Read from remote and write to local */
1.101     djm      1654:        write_error = read_error = write_errno = num_req = 0;
1.21      djm      1655:        max_req = 1;
1.101     djm      1656:        progress_counter = offset;
1.39      fgsch    1657:
1.153     djm      1658:        if (showprogress && size != 0) {
                   1659:                start_progress_meter(progress_meter_path(remote_path),
                   1660:                    size, &progress_counter);
                   1661:        }
1.39      fgsch    1662:
1.144     djm      1663:        if ((msg = sshbuf_new()) == NULL)
                   1664:                fatal_f("sshbuf_new failed");
                   1665:
1.21      djm      1666:        while (num_req > 0 || max_req > 0) {
1.116     djm      1667:                u_char *data;
                   1668:                size_t len;
1.1       djm      1669:
1.49      djm      1670:                /*
1.51      deraadt  1671:                 * Simulate EOF on interrupt: stop sending new requests and
1.49      djm      1672:                 * allow outstanding requests to drain gracefully
                   1673:                 */
                   1674:                if (interrupted) {
                   1675:                        if (num_req == 0) /* If we haven't started yet... */
                   1676:                                break;
                   1677:                        max_req = 0;
                   1678:                }
                   1679:
1.21      djm      1680:                /* Send some more requests */
                   1681:                while (num_req < max_req) {
1.28      markus   1682:                        debug3("Request range %llu -> %llu (%d/%d)",
1.25      itojun   1683:                            (unsigned long long)offset,
                   1684:                            (unsigned long long)offset + buflen - 1,
                   1685:                            num_req, max_req);
1.151     djm      1686:                        req = request_enqueue(&requests, conn->msg_id++,
                   1687:                            buflen, offset);
1.21      djm      1688:                        offset += buflen;
                   1689:                        num_req++;
1.93      djm      1690:                        send_read_request(conn, req->id, req->offset,
1.21      djm      1691:                            req->len, handle, handle_len);
                   1692:                }
1.1       djm      1693:
1.116     djm      1694:                sshbuf_reset(msg);
                   1695:                get_msg(conn, msg);
                   1696:                if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   1697:                    (r = sshbuf_get_u32(msg, &id)) != 0)
1.137     djm      1698:                        fatal_fr(r, "parse");
1.33      deraadt  1699:                debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1.21      djm      1700:
                   1701:                /* Find the request in our queue */
1.151     djm      1702:                if ((req = request_find(&requests, id)) == NULL)
1.21      djm      1703:                        fatal("Unexpected reply %u", id);
                   1704:
                   1705:                switch (type) {
                   1706:                case SSH2_FXP_STATUS:
1.116     djm      1707:                        if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm      1708:                                fatal_fr(r, "parse status");
1.21      djm      1709:                        if (status != SSH2_FX_EOF)
                   1710:                                read_error = 1;
                   1711:                        max_req = 0;
                   1712:                        TAILQ_REMOVE(&requests, req, tq);
1.98      djm      1713:                        free(req);
1.21      djm      1714:                        num_req--;
                   1715:                        break;
                   1716:                case SSH2_FXP_DATA:
1.116     djm      1717:                        if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
1.137     djm      1718:                                fatal_fr(r, "parse data");
1.26      itojun   1719:                        debug3("Received data %llu -> %llu",
1.28      markus   1720:                            (unsigned long long)req->offset,
1.26      itojun   1721:                            (unsigned long long)req->offset + len - 1);
1.21      djm      1722:                        if (len > req->len)
                   1723:                                fatal("Received more data than asked for "
1.116     djm      1724:                                    "%zu > %zu", len, req->len);
1.134     djm      1725:                        lmodified = 1;
1.21      djm      1726:                        if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
1.44      deraadt  1727:                            atomicio(vwrite, local_fd, data, len) != len) &&
1.21      djm      1728:                            !write_error) {
                   1729:                                write_errno = errno;
                   1730:                                write_error = 1;
                   1731:                                max_req = 0;
1.171   ! djm      1732:                        } else {
        !          1733:                                /*
        !          1734:                                 * Track both the highest offset acknowledged
        !          1735:                                 * and the highest *contiguous* offset
        !          1736:                                 * acknowledged.
        !          1737:                                 * We'll need the latter for ftruncate()ing
        !          1738:                                 * interrupted transfers.
        !          1739:                                 */
        !          1740:                                if (maxack < req->offset + len)
        !          1741:                                        maxack = req->offset + len;
        !          1742:                                if (!reordered && req->offset <= highwater)
        !          1743:                                        highwater = maxack;
        !          1744:                                else if (!reordered && req->offset > highwater)
        !          1745:                                        reordered = 1;
1.21      djm      1746:                        }
1.39      fgsch    1747:                        progress_counter += len;
1.98      djm      1748:                        free(data);
1.1       djm      1749:
1.21      djm      1750:                        if (len == req->len) {
                   1751:                                TAILQ_REMOVE(&requests, req, tq);
1.98      djm      1752:                                free(req);
1.21      djm      1753:                                num_req--;
                   1754:                        } else {
                   1755:                                /* Resend the request for the missing data */
                   1756:                                debug3("Short data block, re-requesting "
1.26      itojun   1757:                                    "%llu -> %llu (%2d)",
1.28      markus   1758:                                    (unsigned long long)req->offset + len,
1.27      itojun   1759:                                    (unsigned long long)req->offset +
                   1760:                                    req->len - 1, num_req);
1.23      djm      1761:                                req->id = conn->msg_id++;
1.21      djm      1762:                                req->len -= len;
                   1763:                                req->offset += len;
1.93      djm      1764:                                send_read_request(conn, req->id,
1.23      djm      1765:                                    req->offset, req->len, handle, handle_len);
1.21      djm      1766:                                /* Reduce the request size */
                   1767:                                if (len < buflen)
1.125     deraadt  1768:                                        buflen = MAXIMUM(MIN_READ_SIZE, len);
1.21      djm      1769:                        }
                   1770:                        if (max_req > 0) { /* max_req = 0 iff EOF received */
                   1771:                                if (size > 0 && offset > size) {
                   1772:                                        /* Only one request at a time
                   1773:                                         * after the expected EOF */
                   1774:                                        debug3("Finish at %llu (%2d)",
1.26      itojun   1775:                                            (unsigned long long)offset,
                   1776:                                            num_req);
1.21      djm      1777:                                        max_req = 1;
1.136     djm      1778:                                } else if (max_req < conn->num_requests) {
1.21      djm      1779:                                        ++max_req;
                   1780:                                }
1.1       djm      1781:                        }
1.21      djm      1782:                        break;
                   1783:                default:
1.33      deraadt  1784:                        fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1.1       djm      1785:                            SSH2_FXP_DATA, type);
                   1786:                }
1.21      djm      1787:        }
1.1       djm      1788:
1.39      fgsch    1789:        if (showprogress && size)
                   1790:                stop_progress_meter();
                   1791:
1.21      djm      1792:        /* Sanity check */
                   1793:        if (TAILQ_FIRST(&requests) != NULL)
                   1794:                fatal("Transfer complete, but requests still in queue");
1.171   ! djm      1795:
        !          1796:        if (!read_error && !write_error && !interrupted) {
        !          1797:                /* we got everything */
        !          1798:                highwater = maxack;
        !          1799:        }
        !          1800:
1.164     djm      1801:        /*
                   1802:         * Truncate at highest contiguous point to avoid holes on interrupt,
                   1803:         * or unconditionally if writing in place.
                   1804:         */
                   1805:        if (inplace_flag || read_error || write_error || interrupted) {
1.171   ! djm      1806:                if (reordered && resume_flag &&
        !          1807:                    (read_error || write_error || interrupted)) {
1.101     djm      1808:                        error("Unable to resume download of \"%s\": "
                   1809:                            "server reordered requests", local_path);
                   1810:                }
                   1811:                debug("truncating at %llu", (unsigned long long)highwater);
1.120     djm      1812:                if (ftruncate(local_fd, highwater) == -1)
1.161     djm      1813:                        error("local ftruncate \"%s\": %s", local_path,
1.120     djm      1814:                            strerror(errno));
1.101     djm      1815:        }
1.21      djm      1816:        if (read_error) {
1.161     djm      1817:                error("read remote \"%s\" : %s", remote_path, fx2txt(status));
1.103     djm      1818:                status = -1;
1.23      djm      1819:                do_close(conn, handle, handle_len);
1.21      djm      1820:        } else if (write_error) {
1.161     djm      1821:                error("write local \"%s\": %s", local_path,
1.21      djm      1822:                    strerror(write_errno));
1.116     djm      1823:                status = SSH2_FX_FAILURE;
1.23      djm      1824:                do_close(conn, handle, handle_len);
1.21      djm      1825:        } else {
1.116     djm      1826:                if (do_close(conn, handle, handle_len) != 0 || interrupted)
                   1827:                        status = SSH2_FX_FAILURE;
                   1828:                else
                   1829:                        status = SSH2_FX_OK;
1.21      djm      1830:                /* Override umask and utimes if asked */
1.105     djm      1831:                if (preserve_flag && fchmod(local_fd, mode) == -1)
1.161     djm      1832:                        error("local chmod \"%s\": %s", local_path,
1.37      deraadt  1833:                            strerror(errno));
1.105     djm      1834:                if (preserve_flag &&
                   1835:                    (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1.21      djm      1836:                        struct timeval tv[2];
                   1837:                        tv[0].tv_sec = a->atime;
                   1838:                        tv[1].tv_sec = a->mtime;
                   1839:                        tv[0].tv_usec = tv[1].tv_usec = 0;
                   1840:                        if (utimes(local_path, tv) == -1)
1.161     djm      1841:                                error("local set times \"%s\": %s",
1.37      deraadt  1842:                                    local_path, strerror(errno));
1.1       djm      1843:                }
1.134     djm      1844:                if (resume_flag && !lmodified)
                   1845:                        logit("File \"%s\" was not modified", local_path);
                   1846:                else if (fsync_flag) {
1.107     djm      1847:                        debug("syncing \"%s\"", local_path);
                   1848:                        if (fsync(local_fd) == -1)
1.161     djm      1849:                                error("local sync \"%s\": %s",
1.107     djm      1850:                                    local_path, strerror(errno));
                   1851:                }
1.10      djm      1852:        }
1.5       djm      1853:        close(local_fd);
1.116     djm      1854:        sshbuf_free(msg);
1.98      djm      1855:        free(handle);
1.23      djm      1856:
1.129     djm      1857:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm      1858: }
                   1859:
1.89      djm      1860: static int
1.116     djm      1861: download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
                   1862:     int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
1.163     djm      1863:     int resume_flag, int fsync_flag, int follow_link_flag, int inplace_flag)
1.89      djm      1864: {
                   1865:        int i, ret = 0;
                   1866:        SFTP_DIRENT **dir_entries;
1.130     djm      1867:        char *filename, *new_src = NULL, *new_dst = NULL;
1.138     dtucker  1868:        mode_t mode = 0777, tmpmode = mode;
1.89      djm      1869:
                   1870:        if (depth >= MAX_DIR_DEPTH) {
                   1871:                error("Maximum directory depth exceeded: %d levels", depth);
                   1872:                return -1;
                   1873:        }
                   1874:
1.161     djm      1875:        debug2_f("download dir remote \"%s\" to local \"%s\"", src, dst);
                   1876:
1.89      djm      1877:        if (dirattrib == NULL &&
                   1878:            (dirattrib = do_stat(conn, src, 1)) == NULL) {
1.161     djm      1879:                error("stat remote \"%s\" directory failed", src);
1.89      djm      1880:                return -1;
                   1881:        }
                   1882:        if (!S_ISDIR(dirattrib->perm)) {
                   1883:                error("\"%s\" is not a directory", src);
                   1884:                return -1;
                   1885:        }
1.147     djm      1886:        if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
1.124     schwarze 1887:                mprintf("Retrieving %s\n", src);
1.89      djm      1888:
1.138     dtucker  1889:        if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
1.89      djm      1890:                mode = dirattrib->perm & 01777;
1.138     dtucker  1891:                tmpmode = mode | (S_IWUSR|S_IXUSR);
                   1892:        } else {
1.161     djm      1893:                debug("download remote \"%s\": server "
                   1894:                    "did not send permissions", dst);
1.89      djm      1895:        }
                   1896:
1.138     dtucker  1897:        if (mkdir(dst, tmpmode) == -1 && errno != EEXIST) {
1.89      djm      1898:                error("mkdir %s: %s", dst, strerror(errno));
                   1899:                return -1;
                   1900:        }
                   1901:
                   1902:        if (do_readdir(conn, src, &dir_entries) == -1) {
1.161     djm      1903:                error("remote readdir \"%s\" failed", src);
1.89      djm      1904:                return -1;
                   1905:        }
                   1906:
                   1907:        for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1.130     djm      1908:                free(new_dst);
                   1909:                free(new_src);
                   1910:
1.89      djm      1911:                filename = dir_entries[i]->filename;
                   1912:                new_dst = path_append(dst, filename);
                   1913:                new_src = path_append(src, filename);
                   1914:
                   1915:                if (S_ISDIR(dir_entries[i]->a.perm)) {
                   1916:                        if (strcmp(filename, ".") == 0 ||
                   1917:                            strcmp(filename, "..") == 0)
                   1918:                                continue;
                   1919:                        if (download_dir_internal(conn, new_src, new_dst,
1.105     djm      1920:                            depth + 1, &(dir_entries[i]->a), preserve_flag,
1.150     djm      1921:                            print_flag, resume_flag,
1.163     djm      1922:                            fsync_flag, follow_link_flag, inplace_flag) == -1)
1.89      djm      1923:                                ret = -1;
1.150     djm      1924:                } else if (S_ISREG(dir_entries[i]->a.perm) ||
                   1925:                    (follow_link_flag && S_ISLNK(dir_entries[i]->a.perm))) {
                   1926:                        /*
                   1927:                         * If this is a symlink then don't send the link's
                   1928:                         * Attrib. do_download() will do a FXP_STAT operation
                   1929:                         * and get the link target's attributes.
                   1930:                         */
1.89      djm      1931:                        if (do_download(conn, new_src, new_dst,
1.150     djm      1932:                            S_ISLNK(dir_entries[i]->a.perm) ? NULL :
                   1933:                            &(dir_entries[i]->a),
1.163     djm      1934:                            preserve_flag, resume_flag, fsync_flag,
                   1935:                            inplace_flag) == -1) {
1.89      djm      1936:                                error("Download of file %s to %s failed",
                   1937:                                    new_src, new_dst);
                   1938:                                ret = -1;
                   1939:                        }
                   1940:                } else
1.161     djm      1941:                        logit("download \"%s\": not a regular file", new_src);
1.89      djm      1942:
                   1943:        }
1.130     djm      1944:        free(new_dst);
                   1945:        free(new_src);
1.89      djm      1946:
1.105     djm      1947:        if (preserve_flag) {
1.89      djm      1948:                if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
                   1949:                        struct timeval tv[2];
                   1950:                        tv[0].tv_sec = dirattrib->atime;
                   1951:                        tv[1].tv_sec = dirattrib->mtime;
                   1952:                        tv[0].tv_usec = tv[1].tv_usec = 0;
                   1953:                        if (utimes(dst, tv) == -1)
1.161     djm      1954:                                error("local set times on \"%s\": %s",
1.89      djm      1955:                                    dst, strerror(errno));
                   1956:                } else
                   1957:                        debug("Server did not send times for directory "
                   1958:                            "\"%s\"", dst);
                   1959:        }
                   1960:
1.138     dtucker  1961:        if (mode != tmpmode && chmod(dst, mode) == -1)
1.161     djm      1962:                error("local chmod directory \"%s\": %s", dst,
1.138     dtucker  1963:                    strerror(errno));
                   1964:
1.89      djm      1965:        free_sftp_dirents(dir_entries);
                   1966:
                   1967:        return ret;
                   1968: }
                   1969:
                   1970: int
1.116     djm      1971: download_dir(struct sftp_conn *conn, const char *src, const char *dst,
                   1972:     Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
1.163     djm      1973:     int fsync_flag, int follow_link_flag, int inplace_flag)
1.89      djm      1974: {
                   1975:        char *src_canon;
                   1976:        int ret;
                   1977:
                   1978:        if ((src_canon = do_realpath(conn, src)) == NULL) {
1.161     djm      1979:                error("download \"%s\": path canonicalization failed", src);
1.89      djm      1980:                return -1;
                   1981:        }
                   1982:
1.105     djm      1983:        ret = download_dir_internal(conn, src_canon, dst, 0,
1.150     djm      1984:            dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag,
1.163     djm      1985:            follow_link_flag, inplace_flag);
1.98      djm      1986:        free(src_canon);
1.89      djm      1987:        return ret;
                   1988: }
                   1989:
1.1       djm      1990: int
1.116     djm      1991: do_upload(struct sftp_conn *conn, const char *local_path,
1.163     djm      1992:     const char *remote_path, int preserve_flag, int resume,
                   1993:     int fsync_flag, int inplace_flag)
1.1       djm      1994: {
1.116     djm      1995:        int r, local_fd;
1.163     djm      1996:        u_int openmode, id, status = SSH2_FX_OK, reordered = 0;
1.100     dtucker  1997:        off_t offset, progress_counter;
1.163     djm      1998:        u_char type, *handle, *data;
1.116     djm      1999:        struct sshbuf *msg;
1.1       djm      2000:        struct stat sb;
1.163     djm      2001:        Attrib a, t, *c = NULL;
                   2002:        u_int32_t startid, ackid;
1.171   ! djm      2003:        u_int64_t highwater = 0, maxack = 0;
1.151     djm      2004:        struct request *ack = NULL;
                   2005:        struct requests acks;
1.116     djm      2006:        size_t handle_len;
1.22      djm      2007:
1.161     djm      2008:        debug2_f("upload local \"%s\" to remote \"%s\"",
                   2009:            local_path, remote_path);
                   2010:
1.22      djm      2011:        TAILQ_INIT(&acks);
1.1       djm      2012:
1.156     deraadt  2013:        if ((local_fd = open(local_path, O_RDONLY)) == -1) {
1.161     djm      2014:                error("open local \"%s\": %s", local_path, strerror(errno));
1.1       djm      2015:                return(-1);
                   2016:        }
                   2017:        if (fstat(local_fd, &sb) == -1) {
1.161     djm      2018:                error("fstat local \"%s\": %s", local_path, strerror(errno));
1.41      djm      2019:                close(local_fd);
                   2020:                return(-1);
                   2021:        }
                   2022:        if (!S_ISREG(sb.st_mode)) {
1.161     djm      2023:                error("local \"%s\" is not a regular file", local_path);
1.1       djm      2024:                close(local_fd);
                   2025:                return(-1);
                   2026:        }
                   2027:        stat_to_attrib(&sb, &a);
                   2028:
                   2029:        a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
                   2030:        a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
                   2031:        a.perm &= 0777;
1.105     djm      2032:        if (!preserve_flag)
1.1       djm      2033:                a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
                   2034:
1.115     logan    2035:        if (resume) {
                   2036:                /* Get remote file size if it exists */
                   2037:                if ((c = do_stat(conn, remote_path, 0)) == NULL) {
1.122     djm      2038:                        close(local_fd);
1.115     logan    2039:                        return -1;
                   2040:                }
                   2041:
                   2042:                if ((off_t)c->size >= sb.st_size) {
1.161     djm      2043:                        error("resume \"%s\": destination file "
                   2044:                            "same size or larger", local_path);
1.115     logan    2045:                        close(local_fd);
                   2046:                        return -1;
                   2047:                }
                   2048:
                   2049:                if (lseek(local_fd, (off_t)c->size, SEEK_SET) == -1) {
                   2050:                        close(local_fd);
                   2051:                        return -1;
                   2052:                }
                   2053:        }
                   2054:
1.163     djm      2055:        openmode = SSH2_FXF_WRITE|SSH2_FXF_CREAT;
                   2056:        if (resume)
                   2057:                openmode |= SSH2_FXF_APPEND;
                   2058:        else if (!inplace_flag)
                   2059:                openmode |= SSH2_FXF_TRUNC;
                   2060:
1.1       djm      2061:        /* Send open request */
1.163     djm      2062:        if (send_open(conn, remote_path, "dest", openmode, &a,
                   2063:            &handle, &handle_len) != 0) {
1.1       djm      2064:                close(local_fd);
1.80      djm      2065:                return -1;
1.1       djm      2066:        }
                   2067:
1.144     djm      2068:        id = conn->msg_id;
1.21      djm      2069:        startid = ackid = id + 1;
1.141     djm      2070:        data = xmalloc(conn->upload_buflen);
1.20      djm      2071:
1.1       djm      2072:        /* Read from local and write to remote */
1.115     logan    2073:        offset = progress_counter = (resume ? c->size : 0);
1.153     djm      2074:        if (showprogress) {
                   2075:                start_progress_meter(progress_meter_path(local_path),
                   2076:                    sb.st_size, &progress_counter);
                   2077:        }
1.39      fgsch    2078:
1.144     djm      2079:        if ((msg = sshbuf_new()) == NULL)
                   2080:                fatal_f("sshbuf_new failed");
1.19      deraadt  2081:        for (;;) {
1.1       djm      2082:                int len;
                   2083:
                   2084:                /*
1.51      deraadt  2085:                 * Can't use atomicio here because it returns 0 on EOF,
1.49      djm      2086:                 * thus losing the last block of the file.
1.51      deraadt  2087:                 * Simulate an EOF on interrupt, allowing ACKs from the
1.49      djm      2088:                 * server to drain.
1.1       djm      2089:                 */
1.80      djm      2090:                if (interrupted || status != SSH2_FX_OK)
1.49      djm      2091:                        len = 0;
                   2092:                else do
1.141     djm      2093:                        len = read(local_fd, data, conn->upload_buflen);
1.1       djm      2094:                while ((len == -1) && (errno == EINTR || errno == EAGAIN));
                   2095:
1.161     djm      2096:                if (len == -1) {
                   2097:                        fatal("read local \"%s\": %s",
                   2098:                            local_path, strerror(errno));
                   2099:                } else if (len != 0) {
1.151     djm      2100:                        ack = request_enqueue(&acks, ++id, len, offset);
1.116     djm      2101:                        sshbuf_reset(msg);
                   2102:                        if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
                   2103:                            (r = sshbuf_put_u32(msg, ack->id)) != 0 ||
                   2104:                            (r = sshbuf_put_string(msg, handle,
                   2105:                            handle_len)) != 0 ||
                   2106:                            (r = sshbuf_put_u64(msg, offset)) != 0 ||
                   2107:                            (r = sshbuf_put_string(msg, data, len)) != 0)
1.137     djm      2108:                                fatal_fr(r, "compose");
1.116     djm      2109:                        send_msg(conn, msg);
1.33      deraadt  2110:                        debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1.37      deraadt  2111:                            id, (unsigned long long)offset, len);
1.22      djm      2112:                } else if (TAILQ_FIRST(&acks) == NULL)
1.1       djm      2113:                        break;
                   2114:
1.22      djm      2115:                if (ack == NULL)
                   2116:                        fatal("Unexpected ACK %u", id);
                   2117:
1.28      markus   2118:                if (id == startid || len == 0 ||
1.23      djm      2119:                    id - ackid >= conn->num_requests) {
1.116     djm      2120:                        u_int rid;
1.31      djm      2121:
1.116     djm      2122:                        sshbuf_reset(msg);
                   2123:                        get_msg(conn, msg);
                   2124:                        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   2125:                            (r = sshbuf_get_u32(msg, &rid)) != 0)
1.137     djm      2126:                                fatal_fr(r, "parse");
1.22      djm      2127:
                   2128:                        if (type != SSH2_FXP_STATUS)
                   2129:                                fatal("Expected SSH2_FXP_STATUS(%d) packet, "
                   2130:                                    "got %d", SSH2_FXP_STATUS, type);
                   2131:
1.116     djm      2132:                        if ((r = sshbuf_get_u32(msg, &status)) != 0)
1.137     djm      2133:                                fatal_fr(r, "parse status");
1.116     djm      2134:                        debug3("SSH2_FXP_STATUS %u", status);
1.22      djm      2135:
                   2136:                        /* Find the request in our queue */
1.151     djm      2137:                        if ((ack = request_find(&acks, rid)) == NULL)
1.116     djm      2138:                                fatal("Can't find request for ID %u", rid);
1.22      djm      2139:                        TAILQ_REMOVE(&acks, ack, tq);
1.151     djm      2140:                        debug3("In write loop, ack for %u %zu bytes at %lld",
                   2141:                            ack->id, ack->len, (unsigned long long)ack->offset);
1.21      djm      2142:                        ++ackid;
1.100     dtucker  2143:                        progress_counter += ack->len;
1.171   ! djm      2144:                        /*
        !          2145:                         * Track both the highest offset acknowledged and the
        !          2146:                         * highest *contiguous* offset acknowledged.
        !          2147:                         * We'll need the latter for ftruncate()ing
        !          2148:                         * interrupted transfers.
        !          2149:                         */
        !          2150:                        if (maxack < ack->offset + ack->len)
        !          2151:                                maxack = ack->offset + ack->len;
1.163     djm      2152:                        if (!reordered && ack->offset <= highwater)
1.171   ! djm      2153:                                highwater = maxack;
1.163     djm      2154:                        else if (!reordered && ack->offset > highwater) {
                   2155:                                debug3_f("server reordered ACKs");
                   2156:                                reordered = 1;
                   2157:                        }
1.98      djm      2158:                        free(ack);
1.1       djm      2159:                }
                   2160:                offset += len;
1.77      djm      2161:                if (offset < 0)
1.137     djm      2162:                        fatal_f("offset < 0");
1.1       djm      2163:        }
1.116     djm      2164:        sshbuf_free(msg);
1.80      djm      2165:
1.39      fgsch    2166:        if (showprogress)
                   2167:                stop_progress_meter();
1.98      djm      2168:        free(data);
1.1       djm      2169:
1.171   ! djm      2170:        if (status == SSH2_FX_OK && !interrupted) {
        !          2171:                /* we got everything */
        !          2172:                highwater = maxack;
        !          2173:        }
1.80      djm      2174:        if (status != SSH2_FX_OK) {
1.161     djm      2175:                error("write remote \"%s\": %s", remote_path, fx2txt(status));
1.116     djm      2176:                status = SSH2_FX_FAILURE;
1.80      djm      2177:        }
                   2178:
1.164     djm      2179:        if (inplace_flag || (resume && (status != SSH2_FX_OK || interrupted))) {
1.163     djm      2180:                debug("truncating at %llu", (unsigned long long)highwater);
                   2181:                attrib_clear(&t);
                   2182:                t.flags = SSH2_FILEXFER_ATTR_SIZE;
                   2183:                t.size = highwater;
1.164     djm      2184:                do_fsetstat(conn, handle, handle_len, &t);
1.163     djm      2185:        }
                   2186:
1.1       djm      2187:        if (close(local_fd) == -1) {
1.161     djm      2188:                error("close local \"%s\": %s", local_path, strerror(errno));
1.116     djm      2189:                status = SSH2_FX_FAILURE;
1.1       djm      2190:        }
                   2191:
1.10      djm      2192:        /* Override umask and utimes if asked */
1.105     djm      2193:        if (preserve_flag)
1.23      djm      2194:                do_fsetstat(conn, handle, handle_len, &a);
1.10      djm      2195:
1.107     djm      2196:        if (fsync_flag)
                   2197:                (void)do_fsync(conn, handle, handle_len);
                   2198:
1.121     djm      2199:        if (do_close(conn, handle, handle_len) != 0)
1.116     djm      2200:                status = SSH2_FX_FAILURE;
                   2201:
1.98      djm      2202:        free(handle);
1.5       djm      2203:
1.116     djm      2204:        return status == SSH2_FX_OK ? 0 : -1;
1.1       djm      2205: }
1.89      djm      2206:
                   2207: static int
1.116     djm      2208: upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
1.150     djm      2209:     int depth, int preserve_flag, int print_flag, int resume, int fsync_flag,
1.163     djm      2210:     int follow_link_flag, int inplace_flag)
1.89      djm      2211: {
1.116     djm      2212:        int ret = 0;
1.89      djm      2213:        DIR *dirp;
                   2214:        struct dirent *dp;
1.130     djm      2215:        char *filename, *new_src = NULL, *new_dst = NULL;
1.89      djm      2216:        struct stat sb;
1.121     djm      2217:        Attrib a, *dirattrib;
1.138     dtucker  2218:        u_int32_t saved_perm;
1.89      djm      2219:
1.161     djm      2220:        debug2_f("upload local dir \"%s\" to remote \"%s\"", src, dst);
                   2221:
1.89      djm      2222:        if (depth >= MAX_DIR_DEPTH) {
                   2223:                error("Maximum directory depth exceeded: %d levels", depth);
                   2224:                return -1;
                   2225:        }
                   2226:
                   2227:        if (stat(src, &sb) == -1) {
1.161     djm      2228:                error("stat local \"%s\": %s", src, strerror(errno));
1.89      djm      2229:                return -1;
                   2230:        }
                   2231:        if (!S_ISDIR(sb.st_mode)) {
                   2232:                error("\"%s\" is not a directory", src);
                   2233:                return -1;
                   2234:        }
1.147     djm      2235:        if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
1.124     schwarze 2236:                mprintf("Entering %s\n", src);
1.89      djm      2237:
                   2238:        stat_to_attrib(&sb, &a);
                   2239:        a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
                   2240:        a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
                   2241:        a.perm &= 01777;
1.105     djm      2242:        if (!preserve_flag)
1.89      djm      2243:                a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1.101     djm      2244:
1.89      djm      2245:        /*
1.121     djm      2246:         * sftp lacks a portable status value to match errno EEXIST,
                   2247:         * so if we get a failure back then we must check whether
1.138     dtucker  2248:         * the path already existed and is a directory.  Ensure we can
                   2249:         * write to the directory we create for the duration of the transfer.
1.89      djm      2250:         */
1.138     dtucker  2251:        saved_perm = a.perm;
                   2252:        a.perm |= (S_IWUSR|S_IXUSR);
1.121     djm      2253:        if (do_mkdir(conn, dst, &a, 0) != 0) {
                   2254:                if ((dirattrib = do_stat(conn, dst, 0)) == NULL)
1.89      djm      2255:                        return -1;
1.121     djm      2256:                if (!S_ISDIR(dirattrib->perm)) {
                   2257:                        error("\"%s\" exists but is not a directory", dst);
1.89      djm      2258:                        return -1;
1.121     djm      2259:                }
1.89      djm      2260:        }
1.138     dtucker  2261:        a.perm = saved_perm;
1.89      djm      2262:
                   2263:        if ((dirp = opendir(src)) == NULL) {
1.161     djm      2264:                error("local opendir \"%s\": %s", src, strerror(errno));
1.89      djm      2265:                return -1;
                   2266:        }
1.101     djm      2267:
1.89      djm      2268:        while (((dp = readdir(dirp)) != NULL) && !interrupted) {
                   2269:                if (dp->d_ino == 0)
                   2270:                        continue;
1.130     djm      2271:                free(new_dst);
                   2272:                free(new_src);
1.89      djm      2273:                filename = dp->d_name;
                   2274:                new_dst = path_append(dst, filename);
                   2275:                new_src = path_append(src, filename);
                   2276:
1.90      dtucker  2277:                if (lstat(new_src, &sb) == -1) {
1.161     djm      2278:                        logit("local lstat \"%s\": %s", filename,
1.90      dtucker  2279:                            strerror(errno));
                   2280:                        ret = -1;
                   2281:                } else if (S_ISDIR(sb.st_mode)) {
1.89      djm      2282:                        if (strcmp(filename, ".") == 0 ||
                   2283:                            strcmp(filename, "..") == 0)
                   2284:                                continue;
                   2285:
                   2286:                        if (upload_dir_internal(conn, new_src, new_dst,
1.115     logan    2287:                            depth + 1, preserve_flag, print_flag, resume,
1.163     djm      2288:                            fsync_flag, follow_link_flag, inplace_flag) == -1)
1.89      djm      2289:                                ret = -1;
1.150     djm      2290:                } else if (S_ISREG(sb.st_mode) ||
                   2291:                    (follow_link_flag && S_ISLNK(sb.st_mode))) {
1.105     djm      2292:                        if (do_upload(conn, new_src, new_dst,
1.163     djm      2293:                            preserve_flag, resume, fsync_flag,
                   2294:                            inplace_flag) == -1) {
1.161     djm      2295:                                error("upload \"%s\" to \"%s\" failed",
1.89      djm      2296:                                    new_src, new_dst);
                   2297:                                ret = -1;
                   2298:                        }
                   2299:                } else
1.161     djm      2300:                        logit("%s: not a regular file", filename);
1.89      djm      2301:        }
1.130     djm      2302:        free(new_dst);
                   2303:        free(new_src);
1.89      djm      2304:
                   2305:        do_setstat(conn, dst, &a);
                   2306:
                   2307:        (void) closedir(dirp);
                   2308:        return ret;
                   2309: }
                   2310:
                   2311: int
1.116     djm      2312: upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
1.150     djm      2313:     int preserve_flag, int print_flag, int resume, int fsync_flag,
1.163     djm      2314:     int follow_link_flag, int inplace_flag)
1.89      djm      2315: {
                   2316:        char *dst_canon;
                   2317:        int ret;
                   2318:
                   2319:        if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1.161     djm      2320:                error("upload \"%s\": path canonicalization failed", dst);
1.89      djm      2321:                return -1;
                   2322:        }
                   2323:
1.106     djm      2324:        ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
1.163     djm      2325:            print_flag, resume, fsync_flag, follow_link_flag, inplace_flag);
1.107     djm      2326:
1.98      djm      2327:        free(dst_canon);
1.145     djm      2328:        return ret;
                   2329: }
                   2330:
                   2331: static void
                   2332: handle_dest_replies(struct sftp_conn *to, const char *to_path, int synchronous,
                   2333:     u_int *nreqsp, u_int *write_errorp)
                   2334: {
                   2335:        struct sshbuf *msg;
                   2336:        u_char type;
                   2337:        u_int id, status;
                   2338:        int r;
                   2339:        struct pollfd pfd;
                   2340:
                   2341:        if ((msg = sshbuf_new()) == NULL)
                   2342:                fatal_f("sshbuf_new failed");
                   2343:
                   2344:        /* Try to eat replies from the upload side */
                   2345:        while (*nreqsp > 0) {
                   2346:                debug3_f("%u outstanding replies", *nreqsp);
                   2347:                if (!synchronous) {
                   2348:                        /* Bail out if no data is ready to be read */
                   2349:                        pfd.fd = to->fd_in;
                   2350:                        pfd.events = POLLIN;
                   2351:                        if ((r = poll(&pfd, 1, 0)) == -1) {
                   2352:                                if (errno == EINTR)
                   2353:                                        break;
                   2354:                                fatal_f("poll: %s", strerror(errno));
                   2355:                        } else if (r == 0)
                   2356:                                break; /* fd not ready */
                   2357:                }
                   2358:                sshbuf_reset(msg);
                   2359:                get_msg(to, msg);
                   2360:
                   2361:                if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   2362:                    (r = sshbuf_get_u32(msg, &id)) != 0)
                   2363:                        fatal_fr(r, "dest parse");
                   2364:                debug3("Received dest reply T:%u I:%u R:%u", type, id, *nreqsp);
                   2365:                if (type != SSH2_FXP_STATUS) {
                   2366:                        fatal_f("Expected SSH2_FXP_STATUS(%d) packet, got %d",
                   2367:                            SSH2_FXP_STATUS, type);
                   2368:                }
                   2369:                if ((r = sshbuf_get_u32(msg, &status)) != 0)
                   2370:                        fatal_fr(r, "parse dest status");
                   2371:                debug3("dest SSH2_FXP_STATUS %u", status);
                   2372:                if (status != SSH2_FX_OK) {
                   2373:                        /* record first error */
                   2374:                        if (*write_errorp == 0)
                   2375:                                *write_errorp = status;
                   2376:                }
                   2377:                /*
                   2378:                 * XXX this doesn't do full reply matching like do_upload and
                   2379:                 * so cannot gracefully truncate terminated uploads at a
                   2380:                 * high-water mark. ATM the only caller of this function (scp)
                   2381:                 * doesn't support transfer resumption, so this doesn't matter
                   2382:                 * a whole lot.
                   2383:                 *
                   2384:                 * To be safe, do_crossload truncates the destination file to
                   2385:                 * zero length on upload failure, since we can't trust the
                   2386:                 * server not to have reordered replies that could have
                   2387:                 * inserted holes where none existed in the source file.
                   2388:                 *
                   2389:                 * XXX we could get a more accutate progress bar if we updated
                   2390:                 * the counter based on the reply from the destination...
                   2391:                 */
                   2392:                (*nreqsp)--;
                   2393:        }
                   2394:        debug3_f("done: %u outstanding replies", *nreqsp);
1.157     dtucker  2395:        sshbuf_free(msg);
1.145     djm      2396: }
                   2397:
                   2398: int
                   2399: do_crossload(struct sftp_conn *from, struct sftp_conn *to,
                   2400:     const char *from_path, const char *to_path,
                   2401:     Attrib *a, int preserve_flag)
                   2402: {
                   2403:        struct sshbuf *msg;
1.152     djm      2404:        int write_error, read_error, r;
1.145     djm      2405:        u_int64_t offset = 0, size;
                   2406:        u_int id, buflen, num_req, max_req, status = SSH2_FX_OK;
                   2407:        u_int num_upload_req;
                   2408:        off_t progress_counter;
                   2409:        u_char *from_handle, *to_handle;
                   2410:        size_t from_handle_len, to_handle_len;
1.146     djm      2411:        struct requests requests;
1.145     djm      2412:        struct request *req;
                   2413:        u_char type;
                   2414:
1.161     djm      2415:        debug2_f("crossload src \"%s\" to dst \"%s\"", from_path, to_path);
                   2416:
1.145     djm      2417:        TAILQ_INIT(&requests);
                   2418:
                   2419:        if (a == NULL && (a = do_stat(from, from_path, 0)) == NULL)
                   2420:                return -1;
                   2421:
                   2422:        if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
                   2423:            (!S_ISREG(a->perm))) {
1.161     djm      2424:                error("download \"%s\": not a regular file", from_path);
1.145     djm      2425:                return(-1);
                   2426:        }
                   2427:        if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
                   2428:                size = a->size;
                   2429:        else
                   2430:                size = 0;
                   2431:
                   2432:        buflen = from->download_buflen;
                   2433:        if (buflen > to->upload_buflen)
                   2434:                buflen = to->upload_buflen;
                   2435:
                   2436:        /* Send open request to read side */
                   2437:        if (send_open(from, from_path, "origin", SSH2_FXF_READ, NULL,
                   2438:            &from_handle, &from_handle_len) != 0)
                   2439:                return -1;
                   2440:
                   2441:        /* Send open request to write side */
                   2442:        a->flags &= ~SSH2_FILEXFER_ATTR_SIZE;
                   2443:        a->flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
                   2444:        a->perm &= 0777;
                   2445:        if (!preserve_flag)
                   2446:                a->flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
                   2447:        if (send_open(to, to_path, "dest",
                   2448:            SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC, a,
                   2449:            &to_handle, &to_handle_len) != 0) {
                   2450:                do_close(from, from_handle, from_handle_len);
                   2451:                return -1;
                   2452:        }
                   2453:
                   2454:        /* Read from remote "from" and write to remote "to" */
                   2455:        offset = 0;
                   2456:        write_error = read_error = num_req = num_upload_req = 0;
                   2457:        max_req = 1;
                   2458:        progress_counter = 0;
                   2459:
1.153     djm      2460:        if (showprogress && size != 0) {
                   2461:                start_progress_meter(progress_meter_path(from_path),
                   2462:                    size, &progress_counter);
                   2463:        }
1.145     djm      2464:        if ((msg = sshbuf_new()) == NULL)
                   2465:                fatal_f("sshbuf_new failed");
                   2466:        while (num_req > 0 || max_req > 0) {
                   2467:                u_char *data;
                   2468:                size_t len;
                   2469:
                   2470:                /*
                   2471:                 * Simulate EOF on interrupt: stop sending new requests and
                   2472:                 * allow outstanding requests to drain gracefully
                   2473:                 */
                   2474:                if (interrupted) {
                   2475:                        if (num_req == 0) /* If we haven't started yet... */
                   2476:                                break;
                   2477:                        max_req = 0;
                   2478:                }
                   2479:
                   2480:                /* Send some more requests */
                   2481:                while (num_req < max_req) {
                   2482:                        debug3("Request range %llu -> %llu (%d/%d)",
                   2483:                            (unsigned long long)offset,
                   2484:                            (unsigned long long)offset + buflen - 1,
                   2485:                            num_req, max_req);
1.151     djm      2486:                        req = request_enqueue(&requests, from->msg_id++,
                   2487:                            buflen, offset);
1.145     djm      2488:                        offset += buflen;
                   2489:                        num_req++;
                   2490:                        send_read_request(from, req->id, req->offset,
                   2491:                            req->len, from_handle, from_handle_len);
                   2492:                }
                   2493:
                   2494:                /* Try to eat replies from the upload side (nonblocking) */
                   2495:                handle_dest_replies(to, to_path, 0,
                   2496:                    &num_upload_req, &write_error);
                   2497:
                   2498:                sshbuf_reset(msg);
                   2499:                get_msg(from, msg);
                   2500:                if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   2501:                    (r = sshbuf_get_u32(msg, &id)) != 0)
                   2502:                        fatal_fr(r, "parse");
                   2503:                debug3("Received origin reply T:%u I:%u R:%d",
                   2504:                    type, id, max_req);
                   2505:
                   2506:                /* Find the request in our queue */
1.151     djm      2507:                if ((req = request_find(&requests, id)) == NULL)
1.145     djm      2508:                        fatal("Unexpected reply %u", id);
                   2509:
                   2510:                switch (type) {
                   2511:                case SSH2_FXP_STATUS:
                   2512:                        if ((r = sshbuf_get_u32(msg, &status)) != 0)
                   2513:                                fatal_fr(r, "parse status");
                   2514:                        if (status != SSH2_FX_EOF)
                   2515:                                read_error = 1;
                   2516:                        max_req = 0;
                   2517:                        TAILQ_REMOVE(&requests, req, tq);
                   2518:                        free(req);
                   2519:                        num_req--;
                   2520:                        break;
                   2521:                case SSH2_FXP_DATA:
                   2522:                        if ((r = sshbuf_get_string(msg, &data, &len)) != 0)
                   2523:                                fatal_fr(r, "parse data");
                   2524:                        debug3("Received data %llu -> %llu",
                   2525:                            (unsigned long long)req->offset,
                   2526:                            (unsigned long long)req->offset + len - 1);
                   2527:                        if (len > req->len)
                   2528:                                fatal("Received more data than asked for "
                   2529:                                    "%zu > %zu", len, req->len);
                   2530:
                   2531:                        /* Write this chunk out to the destination */
                   2532:                        sshbuf_reset(msg);
                   2533:                        if ((r = sshbuf_put_u8(msg, SSH2_FXP_WRITE)) != 0 ||
                   2534:                            (r = sshbuf_put_u32(msg, to->msg_id++)) != 0 ||
                   2535:                            (r = sshbuf_put_string(msg, to_handle,
                   2536:                            to_handle_len)) != 0 ||
                   2537:                            (r = sshbuf_put_u64(msg, req->offset)) != 0 ||
                   2538:                            (r = sshbuf_put_string(msg, data, len)) != 0)
                   2539:                                fatal_fr(r, "compose write");
                   2540:                        send_msg(to, msg);
                   2541:                        debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%zu",
                   2542:                            id, (unsigned long long)offset, len);
                   2543:                        num_upload_req++;
                   2544:                        progress_counter += len;
                   2545:                        free(data);
                   2546:
                   2547:                        if (len == req->len) {
                   2548:                                TAILQ_REMOVE(&requests, req, tq);
                   2549:                                free(req);
                   2550:                                num_req--;
                   2551:                        } else {
                   2552:                                /* Resend the request for the missing data */
                   2553:                                debug3("Short data block, re-requesting "
                   2554:                                    "%llu -> %llu (%2d)",
                   2555:                                    (unsigned long long)req->offset + len,
                   2556:                                    (unsigned long long)req->offset +
                   2557:                                    req->len - 1, num_req);
                   2558:                                req->id = from->msg_id++;
                   2559:                                req->len -= len;
                   2560:                                req->offset += len;
                   2561:                                send_read_request(from, req->id,
                   2562:                                    req->offset, req->len,
                   2563:                                    from_handle, from_handle_len);
                   2564:                                /* Reduce the request size */
                   2565:                                if (len < buflen)
                   2566:                                        buflen = MAXIMUM(MIN_READ_SIZE, len);
                   2567:                        }
                   2568:                        if (max_req > 0) { /* max_req = 0 iff EOF received */
                   2569:                                if (size > 0 && offset > size) {
                   2570:                                        /* Only one request at a time
                   2571:                                         * after the expected EOF */
                   2572:                                        debug3("Finish at %llu (%2d)",
                   2573:                                            (unsigned long long)offset,
                   2574:                                            num_req);
                   2575:                                        max_req = 1;
                   2576:                                } else if (max_req < from->num_requests) {
                   2577:                                        ++max_req;
                   2578:                                }
                   2579:                        }
                   2580:                        break;
                   2581:                default:
                   2582:                        fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
                   2583:                            SSH2_FXP_DATA, type);
                   2584:                }
                   2585:        }
                   2586:
                   2587:        if (showprogress && size)
                   2588:                stop_progress_meter();
                   2589:
                   2590:        /* Drain replies from the server (blocking) */
                   2591:        debug3_f("waiting for %u replies from destination", num_upload_req);
                   2592:        handle_dest_replies(to, to_path, 1, &num_upload_req, &write_error);
                   2593:
                   2594:        /* Sanity check */
                   2595:        if (TAILQ_FIRST(&requests) != NULL)
                   2596:                fatal("Transfer complete, but requests still in queue");
                   2597:        /* Truncate at 0 length on interrupt or error to avoid holes at dest */
                   2598:        if (read_error || write_error || interrupted) {
                   2599:                debug("truncating \"%s\" at 0", to_path);
                   2600:                do_close(to, to_handle, to_handle_len);
                   2601:                free(to_handle);
                   2602:                if (send_open(to, to_path, "dest",
                   2603:                    SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC, a,
                   2604:                    &to_handle, &to_handle_len) != 0) {
1.161     djm      2605:                        error("dest truncate \"%s\" failed", to_path);
1.145     djm      2606:                        to_handle = NULL;
                   2607:                }
                   2608:        }
                   2609:        if (read_error) {
1.161     djm      2610:                error("read origin \"%s\": %s", from_path, fx2txt(status));
1.145     djm      2611:                status = -1;
                   2612:                do_close(from, from_handle, from_handle_len);
                   2613:                if (to_handle != NULL)
                   2614:                        do_close(to, to_handle, to_handle_len);
                   2615:        } else if (write_error) {
1.161     djm      2616:                error("write dest \"%s\": %s", to_path, fx2txt(write_error));
1.145     djm      2617:                status = SSH2_FX_FAILURE;
                   2618:                do_close(from, from_handle, from_handle_len);
                   2619:                if (to_handle != NULL)
                   2620:                        do_close(to, to_handle, to_handle_len);
                   2621:        } else {
                   2622:                if (do_close(from, from_handle, from_handle_len) != 0 ||
                   2623:                    interrupted)
                   2624:                        status = -1;
                   2625:                else
                   2626:                        status = SSH2_FX_OK;
                   2627:                if (to_handle != NULL) {
                   2628:                        /* Need to resend utimes after write */
                   2629:                        if (preserve_flag)
                   2630:                                do_fsetstat(to, to_handle, to_handle_len, a);
                   2631:                        do_close(to, to_handle, to_handle_len);
                   2632:                }
                   2633:        }
                   2634:        sshbuf_free(msg);
                   2635:        free(from_handle);
                   2636:        free(to_handle);
                   2637:
                   2638:        return status == SSH2_FX_OK ? 0 : -1;
                   2639: }
                   2640:
                   2641: static int
                   2642: crossload_dir_internal(struct sftp_conn *from, struct sftp_conn *to,
                   2643:     const char *from_path, const char *to_path,
1.150     djm      2644:     int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
                   2645:     int follow_link_flag)
1.145     djm      2646: {
                   2647:        int i, ret = 0;
                   2648:        SFTP_DIRENT **dir_entries;
                   2649:        char *filename, *new_from_path = NULL, *new_to_path = NULL;
                   2650:        mode_t mode = 0777;
1.149     djm      2651:        Attrib curdir;
1.145     djm      2652:
1.161     djm      2653:        debug2_f("crossload dir src \"%s\" to dst \"%s\"", from_path, to_path);
                   2654:
1.145     djm      2655:        if (depth >= MAX_DIR_DEPTH) {
                   2656:                error("Maximum directory depth exceeded: %d levels", depth);
                   2657:                return -1;
                   2658:        }
                   2659:
                   2660:        if (dirattrib == NULL &&
                   2661:            (dirattrib = do_stat(from, from_path, 1)) == NULL) {
1.161     djm      2662:                error("stat remote \"%s\" failed", from_path);
1.145     djm      2663:                return -1;
                   2664:        }
                   2665:        if (!S_ISDIR(dirattrib->perm)) {
                   2666:                error("\"%s\" is not a directory", from_path);
                   2667:                return -1;
                   2668:        }
1.150     djm      2669:        if (print_flag && print_flag != SFTP_PROGRESS_ONLY)
1.145     djm      2670:                mprintf("Retrieving %s\n", from_path);
                   2671:
1.149     djm      2672:        curdir = *dirattrib; /* dirattrib will be clobbered */
                   2673:        curdir.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
                   2674:        curdir.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
                   2675:        if ((curdir.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) == 0) {
                   2676:                debug("Origin did not send permissions for "
1.145     djm      2677:                    "directory \"%s\"", to_path);
1.149     djm      2678:                curdir.perm = S_IWUSR|S_IXUSR;
                   2679:                curdir.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1.145     djm      2680:        }
1.149     djm      2681:        /* We need to be able to write to the directory while we transfer it */
                   2682:        mode = curdir.perm & 01777;
                   2683:        curdir.perm = mode | (S_IWUSR|S_IXUSR);
                   2684:
                   2685:        /*
                   2686:         * sftp lacks a portable status value to match errno EEXIST,
                   2687:         * so if we get a failure back then we must check whether
                   2688:         * the path already existed and is a directory.  Ensure we can
                   2689:         * write to the directory we create for the duration of the transfer.
                   2690:         */
                   2691:        if (do_mkdir(to, to_path, &curdir, 0) != 0) {
                   2692:                if ((dirattrib = do_stat(to, to_path, 0)) == NULL)
                   2693:                        return -1;
                   2694:                if (!S_ISDIR(dirattrib->perm)) {
                   2695:                        error("\"%s\" exists but is not a directory", to_path);
                   2696:                        return -1;
                   2697:                }
                   2698:        }
                   2699:        curdir.perm = mode;
1.145     djm      2700:
                   2701:        if (do_readdir(from, from_path, &dir_entries) == -1) {
1.161     djm      2702:                error("origin readdir \"%s\" failed", from_path);
1.145     djm      2703:                return -1;
                   2704:        }
                   2705:
                   2706:        for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
                   2707:                free(new_from_path);
                   2708:                free(new_to_path);
                   2709:
                   2710:                filename = dir_entries[i]->filename;
                   2711:                new_from_path = path_append(from_path, filename);
                   2712:                new_to_path = path_append(to_path, filename);
                   2713:
                   2714:                if (S_ISDIR(dir_entries[i]->a.perm)) {
                   2715:                        if (strcmp(filename, ".") == 0 ||
                   2716:                            strcmp(filename, "..") == 0)
                   2717:                                continue;
                   2718:                        if (crossload_dir_internal(from, to,
                   2719:                            new_from_path, new_to_path,
                   2720:                            depth + 1, &(dir_entries[i]->a), preserve_flag,
1.150     djm      2721:                            print_flag, follow_link_flag) == -1)
1.145     djm      2722:                                ret = -1;
1.150     djm      2723:                } else if (S_ISREG(dir_entries[i]->a.perm) ||
                   2724:                    (follow_link_flag && S_ISLNK(dir_entries[i]->a.perm))) {
                   2725:                        /*
                   2726:                         * If this is a symlink then don't send the link's
                   2727:                         * Attrib. do_download() will do a FXP_STAT operation
                   2728:                         * and get the link target's attributes.
                   2729:                         */
1.145     djm      2730:                        if (do_crossload(from, to, new_from_path, new_to_path,
1.150     djm      2731:                            S_ISLNK(dir_entries[i]->a.perm) ? NULL :
1.145     djm      2732:                            &(dir_entries[i]->a), preserve_flag) == -1) {
1.161     djm      2733:                                error("crossload \"%s\" to \"%s\" failed",
1.145     djm      2734:                                    new_from_path, new_to_path);
                   2735:                                ret = -1;
                   2736:                        }
1.161     djm      2737:                } else {
                   2738:                        logit("origin \"%s\": not a regular file",
                   2739:                            new_from_path);
                   2740:                }
1.145     djm      2741:        }
                   2742:        free(new_to_path);
                   2743:        free(new_from_path);
                   2744:
1.149     djm      2745:        do_setstat(to, to_path, &curdir);
1.145     djm      2746:
                   2747:        free_sftp_dirents(dir_entries);
                   2748:
                   2749:        return ret;
                   2750: }
                   2751:
                   2752: int
                   2753: crossload_dir(struct sftp_conn *from, struct sftp_conn *to,
                   2754:     const char *from_path, const char *to_path,
1.150     djm      2755:     Attrib *dirattrib, int preserve_flag, int print_flag, int follow_link_flag)
1.145     djm      2756: {
                   2757:        char *from_path_canon;
                   2758:        int ret;
                   2759:
                   2760:        if ((from_path_canon = do_realpath(from, from_path)) == NULL) {
1.161     djm      2761:                error("crossload \"%s\": path canonicalization failed",
                   2762:                    from_path);
1.145     djm      2763:                return -1;
                   2764:        }
                   2765:
                   2766:        ret = crossload_dir_internal(from, to, from_path_canon, to_path, 0,
1.150     djm      2767:            dirattrib, preserve_flag, print_flag, follow_link_flag);
1.145     djm      2768:        free(from_path_canon);
1.89      djm      2769:        return ret;
1.165     djm      2770: }
                   2771:
                   2772: int
                   2773: can_get_users_groups_by_id(struct sftp_conn *conn)
                   2774: {
                   2775:        return (conn->exts & SFTP_EXT_GETUSERSGROUPS_BY_ID) != 0;
                   2776: }
                   2777:
                   2778: int
                   2779: do_get_users_groups_by_id(struct sftp_conn *conn,
                   2780:     const u_int *uids, u_int nuids,
                   2781:     const u_int *gids, u_int ngids,
                   2782:     char ***usernamesp, char ***groupnamesp)
                   2783: {
                   2784:        struct sshbuf *msg, *uidbuf, *gidbuf;
                   2785:        u_int i, expected_id, id;
                   2786:        char *name, **usernames = NULL, **groupnames = NULL;
                   2787:        u_char type;
                   2788:        int r;
                   2789:
                   2790:        *usernamesp = *groupnamesp = NULL;
                   2791:        if (!can_get_users_groups_by_id(conn))
                   2792:                return SSH_ERR_FEATURE_UNSUPPORTED;
                   2793:
                   2794:        if ((msg = sshbuf_new()) == NULL ||
                   2795:            (uidbuf = sshbuf_new()) == NULL ||
                   2796:            (gidbuf = sshbuf_new()) == NULL)
                   2797:                fatal_f("sshbuf_new failed");
                   2798:        expected_id = id = conn->msg_id++;
                   2799:        debug2("Sending SSH2_FXP_EXTENDED(users-groups-by-id@openssh.com)");
                   2800:        for (i = 0; i < nuids; i++) {
                   2801:                if ((r = sshbuf_put_u32(uidbuf, uids[i])) != 0)
                   2802:                        fatal_fr(r, "compose uids");
                   2803:        }
                   2804:        for (i = 0; i < ngids; i++) {
                   2805:                if ((r = sshbuf_put_u32(gidbuf, gids[i])) != 0)
                   2806:                        fatal_fr(r, "compose gids");
                   2807:        }
                   2808:        if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED)) != 0 ||
                   2809:            (r = sshbuf_put_u32(msg, id)) != 0 ||
                   2810:            (r = sshbuf_put_cstring(msg,
                   2811:            "users-groups-by-id@openssh.com")) != 0 ||
                   2812:            (r = sshbuf_put_stringb(msg, uidbuf)) != 0 ||
                   2813:            (r = sshbuf_put_stringb(msg, gidbuf)) != 0)
                   2814:                fatal_fr(r, "compose");
                   2815:        send_msg(conn, msg);
                   2816:        get_msg(conn, msg);
                   2817:        if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
                   2818:            (r = sshbuf_get_u32(msg, &id)) != 0)
                   2819:                fatal_fr(r, "parse");
                   2820:        if (id != expected_id)
                   2821:                fatal("ID mismatch (%u != %u)", id, expected_id);
                   2822:        if (type == SSH2_FXP_STATUS) {
                   2823:                u_int status;
                   2824:                char *errmsg;
                   2825:
                   2826:                if ((r = sshbuf_get_u32(msg, &status)) != 0 ||
                   2827:                    (r = sshbuf_get_cstring(msg, &errmsg, NULL)) != 0)
                   2828:                        fatal_fr(r, "parse status");
                   2829:                error("users-groups-by-id %s",
                   2830:                    *errmsg == '\0' ? fx2txt(status) : errmsg);
                   2831:                free(errmsg);
                   2832:                sshbuf_free(msg);
                   2833:                sshbuf_free(uidbuf);
                   2834:                sshbuf_free(gidbuf);
                   2835:                return -1;
                   2836:        } else if (type != SSH2_FXP_EXTENDED_REPLY)
                   2837:                fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
                   2838:                    SSH2_FXP_EXTENDED_REPLY, type);
                   2839:
                   2840:        /* reuse */
                   2841:        sshbuf_free(uidbuf);
                   2842:        sshbuf_free(gidbuf);
                   2843:        uidbuf = gidbuf = NULL;
                   2844:        if ((r = sshbuf_froms(msg, &uidbuf)) != 0 ||
                   2845:            (r = sshbuf_froms(msg, &gidbuf)) != 0)
                   2846:                fatal_fr(r, "parse response");
                   2847:        if (nuids > 0) {
                   2848:                usernames = xcalloc(nuids, sizeof(*usernames));
                   2849:                for (i = 0; i < nuids; i++) {
                   2850:                        if ((r = sshbuf_get_cstring(uidbuf, &name, NULL)) != 0)
                   2851:                                fatal_fr(r, "parse user name");
                   2852:                        /* Handle unresolved names */
                   2853:                        if (*name == '\0') {
                   2854:                                free(name);
                   2855:                                name = NULL;
                   2856:                        }
                   2857:                        usernames[i] = name;
                   2858:                }
                   2859:        }
                   2860:        if (ngids > 0) {
                   2861:                groupnames = xcalloc(ngids, sizeof(*groupnames));
                   2862:                for (i = 0; i < ngids; i++) {
                   2863:                        if ((r = sshbuf_get_cstring(gidbuf, &name, NULL)) != 0)
                   2864:                                fatal_fr(r, "parse user name");
                   2865:                        /* Handle unresolved names */
                   2866:                        if (*name == '\0') {
                   2867:                                free(name);
                   2868:                                name = NULL;
                   2869:                        }
                   2870:                        groupnames[i] = name;
                   2871:                }
                   2872:        }
                   2873:        if (sshbuf_len(uidbuf) != 0)
                   2874:                fatal_f("unexpected extra username data");
                   2875:        if (sshbuf_len(gidbuf) != 0)
                   2876:                fatal_f("unexpected extra groupname data");
                   2877:        sshbuf_free(uidbuf);
                   2878:        sshbuf_free(gidbuf);
                   2879:        sshbuf_free(msg);
                   2880:        /* success */
                   2881:        *usernamesp = usernames;
                   2882:        *groupnamesp = groupnames;
                   2883:        return 0;
1.89      djm      2884: }
                   2885:
                   2886: char *
1.116     djm      2887: path_append(const char *p1, const char *p2)
1.89      djm      2888: {
                   2889:        char *ret;
                   2890:        size_t len = strlen(p1) + strlen(p2) + 2;
                   2891:
                   2892:        ret = xmalloc(len);
                   2893:        strlcpy(ret, p1, len);
                   2894:        if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
                   2895:                strlcat(ret, "/", len);
                   2896:        strlcat(ret, p2, len);
                   2897:
                   2898:        return(ret);
1.139     djm      2899: }
                   2900:
1.170     dtucker  2901: /*
                   2902:  * Arg p must be dynamically allocated.  It will either be returned or
                   2903:  * freed and a replacement allocated.  Caller must free returned string.
                   2904:  */
1.139     djm      2905: char *
                   2906: make_absolute(char *p, const char *pwd)
                   2907: {
                   2908:        char *abs_str;
                   2909:
                   2910:        /* Derelativise */
                   2911:        if (p && !path_absolute(p)) {
                   2912:                abs_str = path_append(pwd, p);
                   2913:                free(p);
                   2914:                return(abs_str);
                   2915:        } else
                   2916:                return(p);
                   2917: }
                   2918:
                   2919: int
                   2920: remote_is_dir(struct sftp_conn *conn, const char *path)
                   2921: {
                   2922:        Attrib *a;
                   2923:
                   2924:        /* XXX: report errors? */
                   2925:        if ((a = do_stat(conn, path, 1)) == NULL)
                   2926:                return(0);
                   2927:        if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
                   2928:                return(0);
                   2929:        return(S_ISDIR(a->perm));
                   2930: }
                   2931:
                   2932:
                   2933: int
                   2934: local_is_dir(const char *path)
                   2935: {
                   2936:        struct stat sb;
                   2937:
                   2938:        /* XXX: report errors? */
                   2939:        if (stat(path, &sb) == -1)
                   2940:                return(0);
                   2941:
                   2942:        return(S_ISDIR(sb.st_mode));
                   2943: }
                   2944:
                   2945: /* Check whether path returned from glob(..., GLOB_MARK, ...) is a directory */
                   2946: int
                   2947: globpath_is_dir(const char *pathname)
                   2948: {
                   2949:        size_t l = strlen(pathname);
                   2950:
                   2951:        return l > 0 && pathname[l - 1] == '/';
1.89      djm      2952: }
                   2953: