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

1.1       djm         1: /*
1.18.2.1  jason       2:  * Copyright (c) 2001,2002 Damien Miller.  All rights reserved.
1.1       djm         3:  *
                      4:  * Redistribution and use in source and binary forms, with or without
                      5:  * modification, are permitted provided that the following conditions
                      6:  * are met:
                      7:  * 1. Redistributions of source code must retain the above copyright
                      8:  *    notice, this list of conditions and the following disclaimer.
                      9:  * 2. Redistributions in binary form must reproduce the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer in the
                     11:  *    documentation and/or other materials provided with the distribution.
                     12:  *
                     13:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     14:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     15:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     16:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     17:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     18:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     19:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     20:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     21:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     22:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     23:  */
                     24:
                     25: /* XXX: memleaks */
                     26: /* XXX: signed vs unsigned */
1.18.2.1  jason      27: /* XXX: remove all logging, only return status codes */
1.1       djm        28: /* XXX: copy between two remote sites */
                     29:
                     30: #include "includes.h"
1.18.2.5! miod       31: RCSID("$OpenBSD: sftp-client.c,v 1.35 2002/09/11 22:41:49 djm Exp $");
1.18.2.1  jason      32:
                     33: #include <sys/queue.h>
1.1       djm        34:
                     35: #include "buffer.h"
                     36: #include "bufaux.h"
                     37: #include "getput.h"
                     38: #include "xmalloc.h"
                     39: #include "log.h"
                     40: #include "atomicio.h"
                     41:
                     42: #include "sftp.h"
                     43: #include "sftp-common.h"
                     44: #include "sftp-client.h"
                     45:
1.18.2.1  jason      46: /* Minimum amount of data to read at at time */
                     47: #define MIN_READ_SIZE  512
1.1       djm        48:
1.18.2.1  jason      49: struct sftp_conn {
                     50:        int fd_in;
                     51:        int fd_out;
                     52:        u_int transfer_buflen;
                     53:        u_int num_requests;
                     54:        u_int version;
                     55:        u_int msg_id;
                     56: };
1.4       djm        57:
1.17      itojun     58: static void
1.1       djm        59: send_msg(int fd, Buffer *m)
                     60: {
                     61:        int mlen = buffer_len(m);
                     62:        int len;
                     63:        Buffer oqueue;
                     64:
                     65:        buffer_init(&oqueue);
                     66:        buffer_put_int(&oqueue, mlen);
                     67:        buffer_append(&oqueue, buffer_ptr(m), mlen);
                     68:        buffer_consume(m, mlen);
                     69:
                     70:        len = atomicio(write, fd, buffer_ptr(&oqueue), buffer_len(&oqueue));
                     71:        if (len <= 0)
                     72:                fatal("Couldn't send packet: %s", strerror(errno));
                     73:
                     74:        buffer_free(&oqueue);
                     75: }
                     76:
1.17      itojun     77: static void
1.1       djm        78: get_msg(int fd, Buffer *m)
                     79: {
                     80:        u_int len, msg_len;
                     81:        unsigned char buf[4096];
                     82:
                     83:        len = atomicio(read, fd, buf, 4);
1.15      djm        84:        if (len == 0)
                     85:                fatal("Connection closed");
                     86:        else if (len == -1)
1.1       djm        87:                fatal("Couldn't read packet: %s", strerror(errno));
                     88:
                     89:        msg_len = GET_32BIT(buf);
                     90:        if (msg_len > 256 * 1024)
1.18.2.4  miod       91:                fatal("Received message too long %u", msg_len);
1.1       djm        92:
                     93:        while (msg_len) {
                     94:                len = atomicio(read, fd, buf, MIN(msg_len, sizeof(buf)));
1.15      djm        95:                if (len == 0)
                     96:                        fatal("Connection closed");
                     97:                else if (len == -1)
1.1       djm        98:                        fatal("Couldn't read packet: %s", strerror(errno));
                     99:
                    100:                msg_len -= len;
                    101:                buffer_append(m, buf, len);
                    102:        }
                    103: }
                    104:
1.17      itojun    105: static void
1.1       djm       106: send_string_request(int fd, u_int id, u_int code, char *s,
                    107:     u_int len)
                    108: {
                    109:        Buffer msg;
                    110:
                    111:        buffer_init(&msg);
                    112:        buffer_put_char(&msg, code);
                    113:        buffer_put_int(&msg, id);
                    114:        buffer_put_string(&msg, s, len);
                    115:        send_msg(fd, &msg);
1.18.2.4  miod      116:        debug3("Sent message fd %d T:%u I:%u", fd, code, id);
1.1       djm       117:        buffer_free(&msg);
                    118: }
                    119:
1.17      itojun    120: static void
1.1       djm       121: send_string_attrs_request(int fd, u_int id, u_int code, char *s,
                    122:     u_int len, Attrib *a)
                    123: {
                    124:        Buffer msg;
                    125:
                    126:        buffer_init(&msg);
                    127:        buffer_put_char(&msg, code);
                    128:        buffer_put_int(&msg, id);
                    129:        buffer_put_string(&msg, s, len);
                    130:        encode_attrib(&msg, a);
                    131:        send_msg(fd, &msg);
1.18.2.4  miod      132:        debug3("Sent message fd %d T:%u I:%u", fd, code, id);
1.1       djm       133:        buffer_free(&msg);
                    134: }
                    135:
1.17      itojun    136: static u_int
1.18.2.4  miod      137: get_status(int fd, u_int expected_id)
1.1       djm       138: {
                    139:        Buffer msg;
                    140:        u_int type, id, status;
                    141:
                    142:        buffer_init(&msg);
                    143:        get_msg(fd, &msg);
                    144:        type = buffer_get_char(&msg);
                    145:        id = buffer_get_int(&msg);
                    146:
                    147:        if (id != expected_id)
1.18.2.4  miod      148:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.1       djm       149:        if (type != SSH2_FXP_STATUS)
1.18.2.4  miod      150:                fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
1.1       djm       151:                    SSH2_FXP_STATUS, type);
                    152:
                    153:        status = buffer_get_int(&msg);
                    154:        buffer_free(&msg);
                    155:
1.18.2.4  miod      156:        debug3("SSH2_FXP_STATUS %u", status);
1.1       djm       157:
                    158:        return(status);
                    159: }
                    160:
1.17      itojun    161: static char *
1.1       djm       162: get_handle(int fd, u_int expected_id, u_int *len)
                    163: {
                    164:        Buffer msg;
                    165:        u_int type, id;
                    166:        char *handle;
                    167:
                    168:        buffer_init(&msg);
                    169:        get_msg(fd, &msg);
                    170:        type = buffer_get_char(&msg);
                    171:        id = buffer_get_int(&msg);
                    172:
                    173:        if (id != expected_id)
1.18.2.4  miod      174:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.1       djm       175:        if (type == SSH2_FXP_STATUS) {
                    176:                int status = buffer_get_int(&msg);
                    177:
                    178:                error("Couldn't get handle: %s", fx2txt(status));
                    179:                return(NULL);
                    180:        } else if (type != SSH2_FXP_HANDLE)
1.18.2.4  miod      181:                fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
1.1       djm       182:                    SSH2_FXP_HANDLE, type);
                    183:
                    184:        handle = buffer_get_string(&msg, len);
                    185:        buffer_free(&msg);
                    186:
                    187:        return(handle);
                    188: }
                    189:
1.17      itojun    190: static Attrib *
1.14      djm       191: get_decode_stat(int fd, u_int expected_id, int quiet)
1.1       djm       192: {
                    193:        Buffer msg;
                    194:        u_int type, id;
                    195:        Attrib *a;
                    196:
                    197:        buffer_init(&msg);
                    198:        get_msg(fd, &msg);
                    199:
                    200:        type = buffer_get_char(&msg);
                    201:        id = buffer_get_int(&msg);
                    202:
1.18.2.4  miod      203:        debug3("Received stat reply T:%u I:%u", type, id);
1.1       djm       204:        if (id != expected_id)
1.18.2.4  miod      205:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.1       djm       206:        if (type == SSH2_FXP_STATUS) {
                    207:                int status = buffer_get_int(&msg);
                    208:
1.14      djm       209:                if (quiet)
                    210:                        debug("Couldn't stat remote file: %s", fx2txt(status));
                    211:                else
                    212:                        error("Couldn't stat remote file: %s", fx2txt(status));
1.1       djm       213:                return(NULL);
                    214:        } else if (type != SSH2_FXP_ATTRS) {
1.18.2.4  miod      215:                fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
1.1       djm       216:                    SSH2_FXP_ATTRS, type);
                    217:        }
                    218:        a = decode_attrib(&msg);
                    219:        buffer_free(&msg);
                    220:
                    221:        return(a);
                    222: }
                    223:
1.18.2.1  jason     224: struct sftp_conn *
                    225: do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
1.1       djm       226: {
1.18.2.4  miod      227:        u_int type;
                    228:        int version;
1.1       djm       229:        Buffer msg;
1.18.2.1  jason     230:        struct sftp_conn *ret;
1.1       djm       231:
                    232:        buffer_init(&msg);
                    233:        buffer_put_char(&msg, SSH2_FXP_INIT);
                    234:        buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
                    235:        send_msg(fd_out, &msg);
                    236:
                    237:        buffer_clear(&msg);
                    238:
                    239:        get_msg(fd_in, &msg);
                    240:
1.3       stevesk   241:        /* Expecting a VERSION reply */
1.1       djm       242:        if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
1.18.2.4  miod      243:                error("Invalid packet back from SSH2_FXP_INIT (type %u)",
1.1       djm       244:                    type);
                    245:                buffer_free(&msg);
1.18.2.1  jason     246:                return(NULL);
1.1       djm       247:        }
                    248:        version = buffer_get_int(&msg);
                    249:
                    250:        debug2("Remote version: %d", version);
                    251:
                    252:        /* Check for extensions */
                    253:        while (buffer_len(&msg) > 0) {
                    254:                char *name = buffer_get_string(&msg, NULL);
                    255:                char *value = buffer_get_string(&msg, NULL);
                    256:
                    257:                debug2("Init extension: \"%s\"", name);
                    258:                xfree(name);
                    259:                xfree(value);
                    260:        }
                    261:
                    262:        buffer_free(&msg);
1.11      djm       263:
1.18.2.1  jason     264:        ret = xmalloc(sizeof(*ret));
                    265:        ret->fd_in = fd_in;
                    266:        ret->fd_out = fd_out;
                    267:        ret->transfer_buflen = transfer_buflen;
                    268:        ret->num_requests = num_requests;
                    269:        ret->version = version;
                    270:        ret->msg_id = 1;
                    271:
                    272:        /* Some filexfer v.0 servers don't support large packets */
                    273:        if (version == 0)
1.18.2.2  miod      274:                ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
1.18.2.1  jason     275:
                    276:        return(ret);
                    277: }
                    278:
                    279: u_int
                    280: sftp_proto_version(struct sftp_conn *conn)
                    281: {
                    282:        return(conn->version);
1.1       djm       283: }
                    284:
                    285: int
1.18.2.1  jason     286: do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
1.1       djm       287: {
                    288:        u_int id, status;
                    289:        Buffer msg;
                    290:
                    291:        buffer_init(&msg);
                    292:
1.18.2.1  jason     293:        id = conn->msg_id++;
1.1       djm       294:        buffer_put_char(&msg, SSH2_FXP_CLOSE);
                    295:        buffer_put_int(&msg, id);
                    296:        buffer_put_string(&msg, handle, handle_len);
1.18.2.1  jason     297:        send_msg(conn->fd_out, &msg);
1.18.2.4  miod      298:        debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
1.1       djm       299:
1.18.2.1  jason     300:        status = get_status(conn->fd_in, id);
1.1       djm       301:        if (status != SSH2_FX_OK)
                    302:                error("Couldn't close file: %s", fx2txt(status));
                    303:
                    304:        buffer_free(&msg);
                    305:
                    306:        return(status);
                    307: }
                    308:
1.12      djm       309:
1.17      itojun    310: static int
1.18.2.1  jason     311: do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
1.12      djm       312:     SFTP_DIRENT ***dir)
1.1       djm       313: {
                    314:        Buffer msg;
1.13      markus    315:        u_int type, id, handle_len, i, expected_id, ents = 0;
1.1       djm       316:        char *handle;
                    317:
1.18.2.1  jason     318:        id = conn->msg_id++;
1.1       djm       319:
                    320:        buffer_init(&msg);
                    321:        buffer_put_char(&msg, SSH2_FXP_OPENDIR);
                    322:        buffer_put_int(&msg, id);
                    323:        buffer_put_cstring(&msg, path);
1.18.2.1  jason     324:        send_msg(conn->fd_out, &msg);
1.1       djm       325:
                    326:        buffer_clear(&msg);
                    327:
1.18.2.1  jason     328:        handle = get_handle(conn->fd_in, id, &handle_len);
1.1       djm       329:        if (handle == NULL)
                    330:                return(-1);
                    331:
1.12      djm       332:        if (dir) {
                    333:                ents = 0;
                    334:                *dir = xmalloc(sizeof(**dir));
                    335:                (*dir)[0] = NULL;
                    336:        }
                    337:
1.18.2.1  jason     338:        for (;;) {
1.1       djm       339:                int count;
                    340:
1.18.2.1  jason     341:                id = expected_id = conn->msg_id++;
1.1       djm       342:
1.18.2.4  miod      343:                debug3("Sending SSH2_FXP_READDIR I:%u", id);
1.1       djm       344:
                    345:                buffer_clear(&msg);
                    346:                buffer_put_char(&msg, SSH2_FXP_READDIR);
                    347:                buffer_put_int(&msg, id);
                    348:                buffer_put_string(&msg, handle, handle_len);
1.18.2.1  jason     349:                send_msg(conn->fd_out, &msg);
1.1       djm       350:
                    351:                buffer_clear(&msg);
                    352:
1.18.2.1  jason     353:                get_msg(conn->fd_in, &msg);
1.1       djm       354:
                    355:                type = buffer_get_char(&msg);
                    356:                id = buffer_get_int(&msg);
                    357:
1.18.2.4  miod      358:                debug3("Received reply T:%u I:%u", type, id);
1.1       djm       359:
                    360:                if (id != expected_id)
1.18.2.4  miod      361:                        fatal("ID mismatch (%u != %u)", id, expected_id);
1.1       djm       362:
                    363:                if (type == SSH2_FXP_STATUS) {
                    364:                        int status = buffer_get_int(&msg);
                    365:
                    366:                        debug3("Received SSH2_FXP_STATUS %d", status);
                    367:
                    368:                        if (status == SSH2_FX_EOF) {
                    369:                                break;
                    370:                        } else {
                    371:                                error("Couldn't read directory: %s",
                    372:                                    fx2txt(status));
1.18.2.1  jason     373:                                do_close(conn, handle, handle_len);
1.9       djm       374:                                return(status);
1.1       djm       375:                        }
                    376:                } else if (type != SSH2_FXP_NAME)
1.18.2.4  miod      377:                        fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.1       djm       378:                            SSH2_FXP_NAME, type);
                    379:
                    380:                count = buffer_get_int(&msg);
1.7       markus    381:                if (count == 0)
                    382:                        break;
1.8       stevesk   383:                debug3("Received %d SSH2_FXP_NAME responses", count);
1.18.2.1  jason     384:                for (i = 0; i < count; i++) {
1.1       djm       385:                        char *filename, *longname;
                    386:                        Attrib *a;
                    387:
                    388:                        filename = buffer_get_string(&msg, NULL);
                    389:                        longname = buffer_get_string(&msg, NULL);
                    390:                        a = decode_attrib(&msg);
                    391:
1.12      djm       392:                        if (printflag)
                    393:                                printf("%s\n", longname);
                    394:
                    395:                        if (dir) {
1.16      markus    396:                                *dir = xrealloc(*dir, sizeof(**dir) *
1.12      djm       397:                                    (ents + 2));
                    398:                                (*dir)[ents] = xmalloc(sizeof(***dir));
                    399:                                (*dir)[ents]->filename = xstrdup(filename);
                    400:                                (*dir)[ents]->longname = xstrdup(longname);
                    401:                                memcpy(&(*dir)[ents]->a, a, sizeof(*a));
                    402:                                (*dir)[++ents] = NULL;
                    403:                        }
1.1       djm       404:
                    405:                        xfree(filename);
                    406:                        xfree(longname);
                    407:                }
                    408:        }
                    409:
                    410:        buffer_free(&msg);
1.18.2.1  jason     411:        do_close(conn, handle, handle_len);
1.1       djm       412:        xfree(handle);
                    413:
                    414:        return(0);
                    415: }
                    416:
                    417: int
1.18.2.1  jason     418: do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
1.12      djm       419: {
1.18.2.1  jason     420:        return(do_lsreaddir(conn, path, 0, dir));
1.12      djm       421: }
                    422:
                    423: void free_sftp_dirents(SFTP_DIRENT **s)
                    424: {
                    425:        int i;
1.18.2.1  jason     426:
                    427:        for (i = 0; s[i]; i++) {
1.12      djm       428:                xfree(s[i]->filename);
                    429:                xfree(s[i]->longname);
                    430:                xfree(s[i]);
                    431:        }
                    432:        xfree(s);
                    433: }
                    434:
                    435: int
1.18.2.1  jason     436: do_rm(struct sftp_conn *conn, char *path)
1.1       djm       437: {
                    438:        u_int status, id;
                    439:
                    440:        debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
                    441:
1.18.2.1  jason     442:        id = conn->msg_id++;
1.18.2.2  miod      443:        send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
1.18.2.1  jason     444:            strlen(path));
                    445:        status = get_status(conn->fd_in, id);
1.1       djm       446:        if (status != SSH2_FX_OK)
                    447:                error("Couldn't delete file: %s", fx2txt(status));
                    448:        return(status);
                    449: }
                    450:
                    451: int
1.18.2.1  jason     452: do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
1.1       djm       453: {
                    454:        u_int status, id;
                    455:
1.18.2.1  jason     456:        id = conn->msg_id++;
                    457:        send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
1.1       djm       458:            strlen(path), a);
                    459:
1.18.2.1  jason     460:        status = get_status(conn->fd_in, id);
1.1       djm       461:        if (status != SSH2_FX_OK)
                    462:                error("Couldn't create directory: %s", fx2txt(status));
                    463:
                    464:        return(status);
                    465: }
                    466:
                    467: int
1.18.2.1  jason     468: do_rmdir(struct sftp_conn *conn, char *path)
1.1       djm       469: {
                    470:        u_int status, id;
                    471:
1.18.2.1  jason     472:        id = conn->msg_id++;
                    473:        send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
                    474:            strlen(path));
1.1       djm       475:
1.18.2.1  jason     476:        status = get_status(conn->fd_in, id);
1.1       djm       477:        if (status != SSH2_FX_OK)
                    478:                error("Couldn't remove directory: %s", fx2txt(status));
                    479:
                    480:        return(status);
                    481: }
                    482:
                    483: Attrib *
1.18.2.1  jason     484: do_stat(struct sftp_conn *conn, char *path, int quiet)
1.1       djm       485: {
                    486:        u_int id;
                    487:
1.18.2.1  jason     488:        id = conn->msg_id++;
                    489:
1.18.2.2  miod      490:        send_string_request(conn->fd_out, id,
                    491:            conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
1.18.2.1  jason     492:            path, strlen(path));
                    493:
                    494:        return(get_decode_stat(conn->fd_in, id, quiet));
1.1       djm       495: }
                    496:
                    497: Attrib *
1.18.2.1  jason     498: do_lstat(struct sftp_conn *conn, char *path, int quiet)
1.1       djm       499: {
                    500:        u_int id;
                    501:
1.18.2.1  jason     502:        if (conn->version == 0) {
                    503:                if (quiet)
                    504:                        debug("Server version does not support lstat operation");
                    505:                else
1.18.2.2  miod      506:                        log("Server version does not support lstat operation");
                    507:                return(do_stat(conn, path, quiet));
1.18.2.1  jason     508:        }
                    509:
                    510:        id = conn->msg_id++;
                    511:        send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
                    512:            strlen(path));
                    513:
                    514:        return(get_decode_stat(conn->fd_in, id, quiet));
1.1       djm       515: }
                    516:
                    517: Attrib *
1.18.2.1  jason     518: do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
1.1       djm       519: {
                    520:        u_int id;
                    521:
1.18.2.1  jason     522:        id = conn->msg_id++;
                    523:        send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
                    524:            handle_len);
                    525:
                    526:        return(get_decode_stat(conn->fd_in, id, quiet));
1.1       djm       527: }
                    528:
                    529: int
1.18.2.1  jason     530: do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
1.1       djm       531: {
                    532:        u_int status, id;
                    533:
1.18.2.1  jason     534:        id = conn->msg_id++;
                    535:        send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
1.1       djm       536:            strlen(path), a);
                    537:
1.18.2.1  jason     538:        status = get_status(conn->fd_in, id);
1.1       djm       539:        if (status != SSH2_FX_OK)
                    540:                error("Couldn't setstat on \"%s\": %s", path,
                    541:                    fx2txt(status));
                    542:
                    543:        return(status);
                    544: }
                    545:
                    546: int
1.18.2.1  jason     547: do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
1.1       djm       548:     Attrib *a)
                    549: {
                    550:        u_int status, id;
                    551:
1.18.2.1  jason     552:        id = conn->msg_id++;
                    553:        send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
1.1       djm       554:            handle_len, a);
                    555:
1.18.2.1  jason     556:        status = get_status(conn->fd_in, id);
1.1       djm       557:        if (status != SSH2_FX_OK)
                    558:                error("Couldn't fsetstat: %s", fx2txt(status));
                    559:
                    560:        return(status);
                    561: }
                    562:
                    563: char *
1.18.2.1  jason     564: do_realpath(struct sftp_conn *conn, char *path)
1.1       djm       565: {
                    566:        Buffer msg;
                    567:        u_int type, expected_id, count, id;
                    568:        char *filename, *longname;
                    569:        Attrib *a;
                    570:
1.18.2.1  jason     571:        expected_id = id = conn->msg_id++;
                    572:        send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
                    573:            strlen(path));
1.1       djm       574:
                    575:        buffer_init(&msg);
                    576:
1.18.2.1  jason     577:        get_msg(conn->fd_in, &msg);
1.1       djm       578:        type = buffer_get_char(&msg);
                    579:        id = buffer_get_int(&msg);
                    580:
                    581:        if (id != expected_id)
1.18.2.4  miod      582:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.1       djm       583:
                    584:        if (type == SSH2_FXP_STATUS) {
                    585:                u_int status = buffer_get_int(&msg);
                    586:
                    587:                error("Couldn't canonicalise: %s", fx2txt(status));
                    588:                return(NULL);
                    589:        } else if (type != SSH2_FXP_NAME)
1.18.2.4  miod      590:                fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.1       djm       591:                    SSH2_FXP_NAME, type);
                    592:
                    593:        count = buffer_get_int(&msg);
                    594:        if (count != 1)
                    595:                fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
                    596:
                    597:        filename = buffer_get_string(&msg, NULL);
                    598:        longname = buffer_get_string(&msg, NULL);
                    599:        a = decode_attrib(&msg);
                    600:
                    601:        debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
                    602:
                    603:        xfree(longname);
                    604:
                    605:        buffer_free(&msg);
                    606:
                    607:        return(filename);
                    608: }
                    609:
                    610: int
1.18.2.1  jason     611: do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
1.1       djm       612: {
                    613:        Buffer msg;
                    614:        u_int status, id;
                    615:
                    616:        buffer_init(&msg);
                    617:
                    618:        /* Send rename request */
1.18.2.1  jason     619:        id = conn->msg_id++;
1.1       djm       620:        buffer_put_char(&msg, SSH2_FXP_RENAME);
                    621:        buffer_put_int(&msg, id);
                    622:        buffer_put_cstring(&msg, oldpath);
                    623:        buffer_put_cstring(&msg, newpath);
1.18.2.1  jason     624:        send_msg(conn->fd_out, &msg);
1.1       djm       625:        debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
                    626:            newpath);
                    627:        buffer_free(&msg);
                    628:
1.18.2.1  jason     629:        status = get_status(conn->fd_in, id);
1.1       djm       630:        if (status != SSH2_FX_OK)
1.18.2.1  jason     631:                error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
                    632:                    newpath, fx2txt(status));
1.1       djm       633:
                    634:        return(status);
1.11      djm       635: }
                    636:
                    637: int
1.18.2.1  jason     638: do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
1.11      djm       639: {
                    640:        Buffer msg;
                    641:        u_int status, id;
                    642:
1.18.2.1  jason     643:        if (conn->version < 3) {
                    644:                error("This server does not support the symlink operation");
                    645:                return(SSH2_FX_OP_UNSUPPORTED);
                    646:        }
                    647:
1.11      djm       648:        buffer_init(&msg);
                    649:
                    650:        /* Send rename request */
1.18.2.1  jason     651:        id = conn->msg_id++;
1.11      djm       652:        buffer_put_char(&msg, SSH2_FXP_SYMLINK);
                    653:        buffer_put_int(&msg, id);
                    654:        buffer_put_cstring(&msg, oldpath);
                    655:        buffer_put_cstring(&msg, newpath);
1.18.2.1  jason     656:        send_msg(conn->fd_out, &msg);
1.11      djm       657:        debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
                    658:            newpath);
                    659:        buffer_free(&msg);
                    660:
1.18.2.1  jason     661:        status = get_status(conn->fd_in, id);
1.11      djm       662:        if (status != SSH2_FX_OK)
1.18.2.1  jason     663:                error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
                    664:                    newpath, fx2txt(status));
1.11      djm       665:
                    666:        return(status);
                    667: }
                    668:
                    669: char *
1.18.2.1  jason     670: do_readlink(struct sftp_conn *conn, char *path)
1.11      djm       671: {
                    672:        Buffer msg;
                    673:        u_int type, expected_id, count, id;
                    674:        char *filename, *longname;
                    675:        Attrib *a;
                    676:
1.18.2.1  jason     677:        expected_id = id = conn->msg_id++;
                    678:        send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
                    679:            strlen(path));
1.11      djm       680:
                    681:        buffer_init(&msg);
                    682:
1.18.2.1  jason     683:        get_msg(conn->fd_in, &msg);
1.11      djm       684:        type = buffer_get_char(&msg);
                    685:        id = buffer_get_int(&msg);
                    686:
                    687:        if (id != expected_id)
1.18.2.4  miod      688:                fatal("ID mismatch (%u != %u)", id, expected_id);
1.11      djm       689:
                    690:        if (type == SSH2_FXP_STATUS) {
                    691:                u_int status = buffer_get_int(&msg);
                    692:
                    693:                error("Couldn't readlink: %s", fx2txt(status));
                    694:                return(NULL);
                    695:        } else if (type != SSH2_FXP_NAME)
1.18.2.4  miod      696:                fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
1.11      djm       697:                    SSH2_FXP_NAME, type);
                    698:
                    699:        count = buffer_get_int(&msg);
                    700:        if (count != 1)
                    701:                fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
                    702:
                    703:        filename = buffer_get_string(&msg, NULL);
                    704:        longname = buffer_get_string(&msg, NULL);
                    705:        a = decode_attrib(&msg);
                    706:
                    707:        debug3("SSH_FXP_READLINK %s -> %s", path, filename);
                    708:
                    709:        xfree(longname);
                    710:
                    711:        buffer_free(&msg);
                    712:
                    713:        return(filename);
1.1       djm       714: }
                    715:
1.18.2.1  jason     716: static void
                    717: send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
                    718:     char *handle, u_int handle_len)
                    719: {
                    720:        Buffer msg;
1.18.2.2  miod      721:
1.18.2.1  jason     722:        buffer_init(&msg);
                    723:        buffer_clear(&msg);
                    724:        buffer_put_char(&msg, SSH2_FXP_READ);
                    725:        buffer_put_int(&msg, id);
                    726:        buffer_put_string(&msg, handle, handle_len);
                    727:        buffer_put_int64(&msg, offset);
                    728:        buffer_put_int(&msg, len);
                    729:        send_msg(fd_out, &msg);
                    730:        buffer_free(&msg);
1.18.2.2  miod      731: }
1.18.2.1  jason     732:
1.1       djm       733: int
1.18.2.1  jason     734: do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
1.1       djm       735:     int pflag)
                    736: {
                    737:        Attrib junk, *a;
1.18.2.1  jason     738:        Buffer msg;
                    739:        char *handle;
                    740:        int local_fd, status, num_req, max_req, write_error;
                    741:        int read_error, write_errno;
                    742:        u_int64_t offset, size;
                    743:        u_int handle_len, mode, type, id, buflen;
                    744:        struct request {
                    745:                u_int id;
                    746:                u_int len;
                    747:                u_int64_t offset;
1.18.2.2  miod      748:                TAILQ_ENTRY(request) tq;
1.18.2.1  jason     749:        };
                    750:        TAILQ_HEAD(reqhead, request) requests;
                    751:        struct request *req;
1.1       djm       752:
1.18.2.1  jason     753:        TAILQ_INIT(&requests);
                    754:
                    755:        a = do_stat(conn, remote_path, 0);
1.1       djm       756:        if (a == NULL)
                    757:                return(-1);
                    758:
                    759:        /* XXX: should we preserve set[ug]id? */
                    760:        if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
                    761:                mode = S_IWRITE | (a->perm & 0777);
                    762:        else
                    763:                mode = 0666;
                    764:
1.14      djm       765:        if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
                    766:            (a->perm & S_IFDIR)) {
                    767:                error("Cannot download a directory: %s", remote_path);
                    768:                return(-1);
                    769:        }
                    770:
1.18.2.1  jason     771:        if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
                    772:                size = a->size;
                    773:        else
                    774:                size = 0;
1.1       djm       775:
1.18.2.1  jason     776:        buflen = conn->transfer_buflen;
1.1       djm       777:        buffer_init(&msg);
                    778:
                    779:        /* Send open request */
1.18.2.1  jason     780:        id = conn->msg_id++;
1.1       djm       781:        buffer_put_char(&msg, SSH2_FXP_OPEN);
                    782:        buffer_put_int(&msg, id);
                    783:        buffer_put_cstring(&msg, remote_path);
                    784:        buffer_put_int(&msg, SSH2_FXF_READ);
                    785:        attrib_clear(&junk); /* Send empty attributes */
                    786:        encode_attrib(&msg, &junk);
1.18.2.1  jason     787:        send_msg(conn->fd_out, &msg);
1.18.2.4  miod      788:        debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1.1       djm       789:
1.18.2.1  jason     790:        handle = get_handle(conn->fd_in, id, &handle_len);
1.1       djm       791:        if (handle == NULL) {
                    792:                buffer_free(&msg);
1.18.2.1  jason     793:                return(-1);
                    794:        }
                    795:
                    796:        local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
                    797:        if (local_fd == -1) {
                    798:                error("Couldn't open local file \"%s\" for writing: %s",
                    799:                    local_path, strerror(errno));
                    800:                buffer_free(&msg);
                    801:                xfree(handle);
1.1       djm       802:                return(-1);
                    803:        }
                    804:
                    805:        /* Read from remote and write to local */
1.18.2.1  jason     806:        write_error = read_error = write_errno = num_req = offset = 0;
                    807:        max_req = 1;
                    808:        while (num_req > 0 || max_req > 0) {
1.1       djm       809:                char *data;
1.18.2.1  jason     810:                u_int len;
1.1       djm       811:
1.18.2.1  jason     812:                /* Send some more requests */
                    813:                while (num_req < max_req) {
1.18.2.2  miod      814:                        debug3("Request range %llu -> %llu (%d/%d)",
                    815:                            (unsigned long long)offset,
                    816:                            (unsigned long long)offset + buflen - 1,
                    817:                            num_req, max_req);
1.18.2.1  jason     818:                        req = xmalloc(sizeof(*req));
                    819:                        req->id = conn->msg_id++;
                    820:                        req->len = buflen;
                    821:                        req->offset = offset;
                    822:                        offset += buflen;
                    823:                        num_req++;
                    824:                        TAILQ_INSERT_TAIL(&requests, req, tq);
1.18.2.2  miod      825:                        send_read_request(conn->fd_out, req->id, req->offset,
1.18.2.1  jason     826:                            req->len, handle, handle_len);
                    827:                }
1.1       djm       828:
                    829:                buffer_clear(&msg);
1.18.2.1  jason     830:                get_msg(conn->fd_in, &msg);
1.1       djm       831:                type = buffer_get_char(&msg);
                    832:                id = buffer_get_int(&msg);
1.18.2.4  miod      833:                debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
1.18.2.1  jason     834:
                    835:                /* Find the request in our queue */
                    836:                for(req = TAILQ_FIRST(&requests);
                    837:                    req != NULL && req->id != id;
                    838:                    req = TAILQ_NEXT(req, tq))
                    839:                        ;
                    840:                if (req == NULL)
                    841:                        fatal("Unexpected reply %u", id);
                    842:
                    843:                switch (type) {
                    844:                case SSH2_FXP_STATUS:
1.5       djm       845:                        status = buffer_get_int(&msg);
1.18.2.1  jason     846:                        if (status != SSH2_FX_EOF)
                    847:                                read_error = 1;
                    848:                        max_req = 0;
                    849:                        TAILQ_REMOVE(&requests, req, tq);
                    850:                        xfree(req);
                    851:                        num_req--;
                    852:                        break;
                    853:                case SSH2_FXP_DATA:
                    854:                        data = buffer_get_string(&msg, &len);
1.18.2.2  miod      855:                        debug3("Received data %llu -> %llu",
                    856:                            (unsigned long long)req->offset,
                    857:                            (unsigned long long)req->offset + len - 1);
1.18.2.1  jason     858:                        if (len > req->len)
                    859:                                fatal("Received more data than asked for "
1.18.2.4  miod      860:                                      "%u > %u", len, req->len);
1.18.2.1  jason     861:                        if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
                    862:                             atomicio(write, local_fd, data, len) != len) &&
                    863:                            !write_error) {
                    864:                                write_errno = errno;
                    865:                                write_error = 1;
                    866:                                max_req = 0;
                    867:                        }
                    868:                        xfree(data);
1.1       djm       869:
1.18.2.1  jason     870:                        if (len == req->len) {
                    871:                                TAILQ_REMOVE(&requests, req, tq);
                    872:                                xfree(req);
                    873:                                num_req--;
                    874:                        } else {
                    875:                                /* Resend the request for the missing data */
                    876:                                debug3("Short data block, re-requesting "
1.18.2.2  miod      877:                                    "%llu -> %llu (%2d)",
                    878:                                    (unsigned long long)req->offset + len,
                    879:                                    (unsigned long long)req->offset +
                    880:                                    req->len - 1, num_req);
1.18.2.1  jason     881:                                req->id = conn->msg_id++;
                    882:                                req->len -= len;
                    883:                                req->offset += len;
1.18.2.2  miod      884:                                send_read_request(conn->fd_out, req->id,
1.18.2.1  jason     885:                                    req->offset, req->len, handle, handle_len);
                    886:                                /* Reduce the request size */
                    887:                                if (len < buflen)
                    888:                                        buflen = MAX(MIN_READ_SIZE, len);
1.1       djm       889:                        }
1.18.2.1  jason     890:                        if (max_req > 0) { /* max_req = 0 iff EOF received */
                    891:                                if (size > 0 && offset > size) {
                    892:                                        /* Only one request at a time
                    893:                                         * after the expected EOF */
                    894:                                        debug3("Finish at %llu (%2d)",
1.18.2.2  miod      895:                                            (unsigned long long)offset,
                    896:                                            num_req);
1.18.2.1  jason     897:                                        max_req = 1;
                    898:                                }
                    899:                                else if (max_req < conn->num_requests + 1) {
                    900:                                        ++max_req;
                    901:                                }
                    902:                        }
                    903:                        break;
                    904:                default:
1.18.2.4  miod      905:                        fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
1.1       djm       906:                            SSH2_FXP_DATA, type);
                    907:                }
                    908:        }
1.5       djm       909:
1.18.2.1  jason     910:        /* Sanity check */
                    911:        if (TAILQ_FIRST(&requests) != NULL)
                    912:                fatal("Transfer complete, but requests still in queue");
                    913:
                    914:        if (read_error) {
1.18.2.2  miod      915:                error("Couldn't read from remote file \"%s\" : %s",
1.18.2.1  jason     916:                    remote_path, fx2txt(status));
                    917:                do_close(conn, handle, handle_len);
                    918:        } else if (write_error) {
                    919:                error("Couldn't write to \"%s\": %s", local_path,
                    920:                    strerror(write_errno));
                    921:                status = -1;
                    922:                do_close(conn, handle, handle_len);
                    923:        } else {
                    924:                status = do_close(conn, handle, handle_len);
                    925:
                    926:                /* Override umask and utimes if asked */
                    927:                if (pflag && fchmod(local_fd, mode) == -1)
                    928:                        error("Couldn't set mode on \"%s\": %s", local_path,
                    929:                              strerror(errno));
                    930:                if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
                    931:                        struct timeval tv[2];
                    932:                        tv[0].tv_sec = a->atime;
                    933:                        tv[1].tv_sec = a->mtime;
                    934:                        tv[0].tv_usec = tv[1].tv_usec = 0;
                    935:                        if (utimes(local_path, tv) == -1)
                    936:                                error("Can't set times on \"%s\": %s",
                    937:                                      local_path, strerror(errno));
                    938:                }
1.10      djm       939:        }
1.5       djm       940:        close(local_fd);
                    941:        buffer_free(&msg);
1.1       djm       942:        xfree(handle);
1.18.2.1  jason     943:
                    944:        return(status);
1.1       djm       945: }
                    946:
                    947: int
1.18.2.1  jason     948: do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
1.1       djm       949:     int pflag)
                    950: {
1.18.2.1  jason     951:        int local_fd, status;
                    952:        u_int handle_len, id, type;
1.1       djm       953:        u_int64_t offset;
1.18.2.1  jason     954:        char *handle, *data;
1.1       djm       955:        Buffer msg;
                    956:        struct stat sb;
                    957:        Attrib a;
1.18.2.1  jason     958:        u_int32_t startid;
                    959:        u_int32_t ackid;
                    960:        struct outstanding_ack {
                    961:                u_int id;
                    962:                u_int len;
                    963:                u_int64_t offset;
1.18.2.2  miod      964:                TAILQ_ENTRY(outstanding_ack) tq;
1.18.2.1  jason     965:        };
                    966:        TAILQ_HEAD(ackhead, outstanding_ack) acks;
                    967:        struct outstanding_ack *ack;
                    968:
                    969:        TAILQ_INIT(&acks);
1.1       djm       970:
                    971:        if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
                    972:                error("Couldn't open local file \"%s\" for reading: %s",
                    973:                    local_path, strerror(errno));
                    974:                return(-1);
                    975:        }
                    976:        if (fstat(local_fd, &sb) == -1) {
                    977:                error("Couldn't fstat local file \"%s\": %s",
                    978:                    local_path, strerror(errno));
                    979:                close(local_fd);
                    980:                return(-1);
                    981:        }
                    982:        stat_to_attrib(&sb, &a);
                    983:
                    984:        a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
                    985:        a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
                    986:        a.perm &= 0777;
                    987:        if (!pflag)
                    988:                a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
                    989:
                    990:        buffer_init(&msg);
                    991:
                    992:        /* Send open request */
1.18.2.1  jason     993:        id = conn->msg_id++;
1.1       djm       994:        buffer_put_char(&msg, SSH2_FXP_OPEN);
                    995:        buffer_put_int(&msg, id);
                    996:        buffer_put_cstring(&msg, remote_path);
                    997:        buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
                    998:        encode_attrib(&msg, &a);
1.18.2.1  jason     999:        send_msg(conn->fd_out, &msg);
1.18.2.4  miod     1000:        debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1.1       djm      1001:
                   1002:        buffer_clear(&msg);
                   1003:
1.18.2.1  jason    1004:        handle = get_handle(conn->fd_in, id, &handle_len);
1.1       djm      1005:        if (handle == NULL) {
                   1006:                close(local_fd);
                   1007:                buffer_free(&msg);
                   1008:                return(-1);
                   1009:        }
                   1010:
1.18.2.1  jason    1011:        startid = ackid = id + 1;
                   1012:        data = xmalloc(conn->transfer_buflen);
                   1013:
1.1       djm      1014:        /* Read from local and write to remote */
                   1015:        offset = 0;
1.18.2.1  jason    1016:        for (;;) {
1.1       djm      1017:                int len;
                   1018:
                   1019:                /*
                   1020:                 * Can't use atomicio here because it returns 0 on EOF, thus losing
                   1021:                 * the last block of the file
                   1022:                 */
                   1023:                do
1.18.2.1  jason    1024:                        len = read(local_fd, data, conn->transfer_buflen);
1.1       djm      1025:                while ((len == -1) && (errno == EINTR || errno == EAGAIN));
                   1026:
                   1027:                if (len == -1)
                   1028:                        fatal("Couldn't read from \"%s\": %s", local_path,
                   1029:                            strerror(errno));
1.18.2.1  jason    1030:
                   1031:                if (len != 0) {
                   1032:                        ack = xmalloc(sizeof(*ack));
                   1033:                        ack->id = ++id;
                   1034:                        ack->offset = offset;
                   1035:                        ack->len = len;
                   1036:                        TAILQ_INSERT_TAIL(&acks, ack, tq);
                   1037:
                   1038:                        buffer_clear(&msg);
                   1039:                        buffer_put_char(&msg, SSH2_FXP_WRITE);
                   1040:                        buffer_put_int(&msg, ack->id);
                   1041:                        buffer_put_string(&msg, handle, handle_len);
                   1042:                        buffer_put_int64(&msg, offset);
                   1043:                        buffer_put_string(&msg, data, len);
                   1044:                        send_msg(conn->fd_out, &msg);
1.18.2.4  miod     1045:                        debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1.18.2.2  miod     1046:                               id, (unsigned long long)offset, len);
1.18.2.1  jason    1047:                } else if (TAILQ_FIRST(&acks) == NULL)
1.1       djm      1048:                        break;
                   1049:
1.18.2.1  jason    1050:                if (ack == NULL)
                   1051:                        fatal("Unexpected ACK %u", id);
                   1052:
1.18.2.2  miod     1053:                if (id == startid || len == 0 ||
1.18.2.1  jason    1054:                    id - ackid >= conn->num_requests) {
1.18.2.3  miod     1055:                        u_int r_id;
1.18.2.2  miod     1056:
1.18.2.1  jason    1057:                        buffer_clear(&msg);
                   1058:                        get_msg(conn->fd_in, &msg);
                   1059:                        type = buffer_get_char(&msg);
1.18.2.2  miod     1060:                        r_id = buffer_get_int(&msg);
1.18.2.1  jason    1061:
                   1062:                        if (type != SSH2_FXP_STATUS)
                   1063:                                fatal("Expected SSH2_FXP_STATUS(%d) packet, "
                   1064:                                    "got %d", SSH2_FXP_STATUS, type);
1.1       djm      1065:
1.18.2.1  jason    1066:                        status = buffer_get_int(&msg);
                   1067:                        debug3("SSH2_FXP_STATUS %d", status);
                   1068:
                   1069:                        /* Find the request in our queue */
                   1070:                        for(ack = TAILQ_FIRST(&acks);
1.18.2.2  miod     1071:                            ack != NULL && ack->id != r_id;
1.18.2.1  jason    1072:                            ack = TAILQ_NEXT(ack, tq))
                   1073:                                ;
                   1074:                        if (ack == NULL)
1.18.2.4  miod     1075:                                fatal("Can't find request for ID %u", r_id);
1.18.2.1  jason    1076:                        TAILQ_REMOVE(&acks, ack, tq);
                   1077:
                   1078:                        if (status != SSH2_FX_OK) {
                   1079:                                error("Couldn't write to remote file \"%s\": %s",
                   1080:                                      remote_path, fx2txt(status));
                   1081:                                do_close(conn, handle, handle_len);
                   1082:                                close(local_fd);
                   1083:                                goto done;
                   1084:                        }
1.18.2.4  miod     1085:                        debug3("In write loop, ack for %u %u bytes at %llu",
1.18.2.2  miod     1086:                           ack->id, ack->len, (unsigned long long)ack->offset);
1.18.2.1  jason    1087:                        ++ackid;
1.18.2.5! miod     1088:                        xfree(ack);
1.18.2.1  jason    1089:                }
1.1       djm      1090:                offset += len;
                   1091:        }
1.18.2.1  jason    1092:        xfree(data);
1.1       djm      1093:
                   1094:        if (close(local_fd) == -1) {
                   1095:                error("Couldn't close local file \"%s\": %s", local_path,
                   1096:                    strerror(errno));
1.18.2.1  jason    1097:                do_close(conn, handle, handle_len);
1.5       djm      1098:                status = -1;
                   1099:                goto done;
1.1       djm      1100:        }
                   1101:
1.10      djm      1102:        /* Override umask and utimes if asked */
                   1103:        if (pflag)
1.18.2.1  jason    1104:                do_fsetstat(conn, handle, handle_len, &a);
1.10      djm      1105:
1.18.2.1  jason    1106:        status = do_close(conn, handle, handle_len);
1.5       djm      1107:
                   1108: done:
                   1109:        xfree(handle);
                   1110:        buffer_free(&msg);
1.18.2.1  jason    1111:        return(status);
1.1       djm      1112: }