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

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