[BACK]Return to nchan.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/nchan.c, Revision 1.73

1.73    ! djm         1: /* $OpenBSD: nchan.c,v 1.72 2021/01/27 09:26:54 djm Exp $ */
1.7       markus      2: /*
1.43      markus      3:  * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl.  All rights reserved.
1.7       markus      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     15:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     16:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     17:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     18:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     19:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     20:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     21:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     22:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     23:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     24:  */
                     25:
1.54      stevesk    26: #include <sys/types.h>
                     27: #include <sys/socket.h>
1.58      djm        28: #include <sys/queue.h>
1.55      stevesk    29:
                     30: #include <errno.h>
1.56      stevesk    31: #include <string.h>
1.57      deraadt    32: #include <stdarg.h>
1.1       markus     33:
1.22      markus     34: #include "ssh2.h"
1.66      djm        35: #include "sshbuf.h"
                     36: #include "ssherr.h"
1.3       markus     37: #include "packet.h"
1.1       markus     38: #include "channels.h"
1.13      markus     39: #include "compat.h"
1.22      markus     40: #include "log.h"
1.3       markus     41:
1.28      markus     42: /*
                     43:  * SSH Protocol 1.5 aka New Channel Protocol
                     44:  * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
                     45:  * Written by Markus Friedl in October 1999
                     46:  *
                     47:  * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
                     48:  * tear down of channels:
                     49:  *
                     50:  * 1.3:        strict request-ack-protocol:
1.51      deraadt    51:  *     CLOSE   ->
                     52:  *             <-  CLOSE_CONFIRM
1.28      markus     53:  *
                     54:  * 1.5:        uses variations of:
1.51      deraadt    55:  *     IEOF    ->
                     56:  *             <-  OCLOSE
                     57:  *             <-  IEOF
                     58:  *     OCLOSE  ->
                     59:  *     i.e. both sides have to close the channel
1.28      markus     60:  *
                     61:  * 2.0: the EOF messages are optional
                     62:  *
                     63:  * See the debugging output from 'ssh -v' and 'sshd -d' of
                     64:  * ssh-1.2.27 as an example.
                     65:  *
                     66:  */
                     67:
1.13      markus     68: /* functions manipulating channel states */
1.3       markus     69: /*
1.6       markus     70:  * EVENTS update channel input/output states execute ACTIONS
1.3       markus     71:  */
1.13      markus     72: /*
                     73:  * ACTIONS: should never update the channel states
                     74:  */
1.66      djm        75: static void    chan_send_eof2(struct ssh *, Channel *);
                     76: static void    chan_send_eow2(struct ssh *, Channel *);
1.13      markus     77:
                     78: /* helper */
1.66      djm        79: static void    chan_shutdown_write(struct ssh *, Channel *);
                     80: static void    chan_shutdown_read(struct ssh *, Channel *);
1.69      djm        81: static void    chan_shutdown_extended_read(struct ssh *, Channel *);
1.13      markus     82:
1.66      djm        83: static const char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
                     84: static const char *istates[] = { "open", "drain", "wait_oclose", "closed" };
1.37      markus     85:
                     86: static void
                     87: chan_set_istate(Channel *c, u_int next)
                     88: {
                     89:        if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
                     90:                fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
1.49      markus     91:        debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
1.37      markus     92:            istates[next]);
                     93:        c->istate = next;
                     94: }
1.64      djm        95:
1.37      markus     96: static void
                     97: chan_set_ostate(Channel *c, u_int next)
                     98: {
                     99:        if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
                    100:                fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
1.49      markus    101:        debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
1.37      markus    102:            ostates[next]);
                    103:        c->ostate = next;
                    104: }
                    105:
1.42      markus    106: void
1.66      djm       107: chan_read_failed(struct ssh *ssh, Channel *c)
1.6       markus    108: {
1.49      markus    109:        debug2("channel %d: read failed", c->self);
1.6       markus    110:        switch (c->istate) {
1.3       markus    111:        case CHAN_INPUT_OPEN:
1.66      djm       112:                chan_shutdown_read(ssh, c);
1.37      markus    113:                chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
1.3       markus    114:                break;
                    115:        default:
1.28      markus    116:                error("channel %d: chan_read_failed for istate %d",
1.13      markus    117:                    c->self, c->istate);
1.3       markus    118:                break;
1.1       markus    119:        }
                    120: }
1.64      djm       121:
1.42      markus    122: void
1.66      djm       123: chan_ibuf_empty(struct ssh *ssh, Channel *c)
1.6       markus    124: {
1.49      markus    125:        debug2("channel %d: ibuf empty", c->self);
1.66      djm       126:        if (sshbuf_len(c->input)) {
1.28      markus    127:                error("channel %d: chan_ibuf_empty for non empty buffer",
1.13      markus    128:                    c->self);
1.3       markus    129:                return;
                    130:        }
1.6       markus    131:        switch (c->istate) {
1.3       markus    132:        case CHAN_INPUT_WAIT_DRAIN:
1.64      djm       133:                if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_LOCAL)))
1.66      djm       134:                        chan_send_eof2(ssh, c);
1.64      djm       135:                chan_set_istate(c, CHAN_INPUT_CLOSED);
1.3       markus    136:                break;
                    137:        default:
1.28      markus    138:                error("channel %d: chan_ibuf_empty for istate %d",
1.13      markus    139:                    c->self, c->istate);
1.3       markus    140:                break;
1.1       markus    141:        }
                    142: }
1.64      djm       143:
1.42      markus    144: void
1.66      djm       145: chan_obuf_empty(struct ssh *ssh, Channel *c)
1.6       markus    146: {
1.49      markus    147:        debug2("channel %d: obuf empty", c->self);
1.66      djm       148:        if (sshbuf_len(c->output)) {
1.28      markus    149:                error("channel %d: chan_obuf_empty for non empty buffer",
1.13      markus    150:                    c->self);
1.3       markus    151:                return;
                    152:        }
1.6       markus    153:        switch (c->ostate) {
1.3       markus    154:        case CHAN_OUTPUT_WAIT_DRAIN:
1.66      djm       155:                chan_shutdown_write(ssh, c);
1.37      markus    156:                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
1.3       markus    157:                break;
                    158:        default:
1.28      markus    159:                error("channel %d: internal error: obuf_empty for ostate %d",
1.13      markus    160:                    c->self, c->ostate);
1.3       markus    161:                break;
                    162:        }
                    163: }
1.63      djm       164:
1.59      markus    165: void
1.66      djm       166: chan_rcvd_eow(struct ssh *ssh, Channel *c)
1.59      markus    167: {
                    168:        debug2("channel %d: rcvd eow", c->self);
                    169:        switch (c->istate) {
                    170:        case CHAN_INPUT_OPEN:
1.66      djm       171:                chan_shutdown_read(ssh, c);
1.59      markus    172:                chan_set_istate(c, CHAN_INPUT_CLOSED);
                    173:                break;
                    174:        }
                    175: }
1.64      djm       176:
1.13      markus    177: static void
1.66      djm       178: chan_send_eof2(struct ssh *ssh, Channel *c)
1.6       markus    179: {
1.66      djm       180:        int r;
                    181:
1.49      markus    182:        debug2("channel %d: send eof", c->self);
1.13      markus    183:        switch (c->istate) {
                    184:        case CHAN_INPUT_WAIT_DRAIN:
1.67      djm       185:                if (!c->have_remote_id)
1.71      djm       186:                        fatal_f("channel %d: no remote_id", c->self);
1.66      djm       187:                if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EOF)) != 0 ||
                    188:                    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
                    189:                    (r = sshpkt_send(ssh)) != 0)
1.71      djm       190:                        fatal_fr(r, "send CHANNEL_EOF");
1.45      markus    191:                c->flags |= CHAN_EOF_SENT;
1.13      markus    192:                break;
                    193:        default:
1.28      markus    194:                error("channel %d: cannot send eof for istate %d",
1.13      markus    195:                    c->self, c->istate);
                    196:                break;
                    197:        }
1.1       markus    198: }
1.64      djm       199:
1.3       markus    200: static void
1.66      djm       201: chan_send_close2(struct ssh *ssh, Channel *c)
1.6       markus    202: {
1.66      djm       203:        int r;
                    204:
1.49      markus    205:        debug2("channel %d: send close", c->self);
1.13      markus    206:        if (c->ostate != CHAN_OUTPUT_CLOSED ||
                    207:            c->istate != CHAN_INPUT_CLOSED) {
1.28      markus    208:                error("channel %d: cannot send close for istate/ostate %d/%d",
1.13      markus    209:                    c->self, c->istate, c->ostate);
                    210:        } else if (c->flags & CHAN_CLOSE_SENT) {
1.28      markus    211:                error("channel %d: already sent close", c->self);
1.13      markus    212:        } else {
1.67      djm       213:                if (!c->have_remote_id)
1.71      djm       214:                        fatal_f("channel %d: no remote_id", c->self);
1.66      djm       215:                if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_CLOSE)) != 0 ||
                    216:                    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
                    217:                    (r = sshpkt_send(ssh)) != 0)
1.71      djm       218:                        fatal_fr(r, "send CHANNEL_EOF");
1.13      markus    219:                c->flags |= CHAN_CLOSE_SENT;
                    220:        }
1.59      markus    221: }
1.64      djm       222:
1.59      markus    223: static void
1.66      djm       224: chan_send_eow2(struct ssh *ssh, Channel *c)
1.59      markus    225: {
1.66      djm       226:        int r;
                    227:
1.59      markus    228:        debug2("channel %d: send eow", c->self);
                    229:        if (c->ostate == CHAN_OUTPUT_CLOSED) {
                    230:                error("channel %d: must not sent eow on closed output",
                    231:                    c->self);
                    232:                return;
                    233:        }
1.72      djm       234:        if (!(ssh->compat & SSH_NEW_OPENSSH))
1.61      markus    235:                return;
1.67      djm       236:        if (!c->have_remote_id)
1.71      djm       237:                fatal_f("channel %d: no remote_id", c->self);
1.66      djm       238:        if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
                    239:            (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
                    240:            (r = sshpkt_put_cstring(ssh, "eow@openssh.com")) != 0 ||
                    241:            (r = sshpkt_put_u8(ssh, 0)) != 0 ||
                    242:            (r = sshpkt_send(ssh)) != 0)
1.71      djm       243:                fatal_fr(r, "send CHANNEL_EOF");
1.1       markus    244: }
1.23      markus    245:
                    246: /* shared */
                    247:
1.24      markus    248: void
1.66      djm       249: chan_rcvd_ieof(struct ssh *ssh, Channel *c)
1.42      markus    250: {
1.64      djm       251:        debug2("channel %d: rcvd eof", c->self);
                    252:        c->flags |= CHAN_EOF_RCVD;
                    253:        if (c->ostate == CHAN_OUTPUT_OPEN)
                    254:                chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
1.44      markus    255:        if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
1.66      djm       256:            sshbuf_len(c->output) == 0 &&
1.45      markus    257:            !CHANNEL_EFD_OUTPUT_ACTIVE(c))
1.66      djm       258:                chan_obuf_empty(ssh, c);
1.42      markus    259: }
1.64      djm       260:
1.42      markus    261: void
1.66      djm       262: chan_rcvd_oclose(struct ssh *ssh, Channel *c)
1.42      markus    263: {
1.64      djm       264:        debug2("channel %d: rcvd close", c->self);
                    265:        if (!(c->flags & CHAN_LOCAL)) {
                    266:                if (c->flags & CHAN_CLOSE_RCVD)
                    267:                        error("channel %d: protocol error: close rcvd twice",
                    268:                            c->self);
                    269:                c->flags |= CHAN_CLOSE_RCVD;
                    270:        }
                    271:        if (c->type == SSH_CHANNEL_LARVAL) {
                    272:                /* tear down larval channels immediately */
                    273:                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
                    274:                chan_set_istate(c, CHAN_INPUT_CLOSED);
                    275:                return;
                    276:        }
                    277:        switch (c->ostate) {
                    278:        case CHAN_OUTPUT_OPEN:
                    279:                /*
                    280:                 * wait until a data from the channel is consumed if a CLOSE
                    281:                 * is received
                    282:                 */
                    283:                chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
                    284:                break;
                    285:        }
                    286:        switch (c->istate) {
                    287:        case CHAN_INPUT_OPEN:
1.66      djm       288:                chan_shutdown_read(ssh, c);
1.69      djm       289:                chan_shutdown_extended_read(ssh, c);
1.64      djm       290:                chan_set_istate(c, CHAN_INPUT_CLOSED);
                    291:                break;
                    292:        case CHAN_INPUT_WAIT_DRAIN:
                    293:                if (!(c->flags & CHAN_LOCAL))
1.66      djm       294:                        chan_send_eof2(ssh, c);
1.69      djm       295:                chan_shutdown_extended_read(ssh, c);
1.64      djm       296:                chan_set_istate(c, CHAN_INPUT_CLOSED);
                    297:                break;
                    298:        }
1.42      markus    299: }
1.64      djm       300:
1.42      markus    301: void
1.66      djm       302: chan_write_failed(struct ssh *ssh, Channel *c)
1.42      markus    303: {
1.64      djm       304:        debug2("channel %d: write failed", c->self);
                    305:        switch (c->ostate) {
                    306:        case CHAN_OUTPUT_OPEN:
                    307:        case CHAN_OUTPUT_WAIT_DRAIN:
1.66      djm       308:                chan_shutdown_write(ssh, c);
1.64      djm       309:                if (strcmp(c->ctype, "session") == 0)
1.66      djm       310:                        chan_send_eow2(ssh, c);
1.64      djm       311:                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
                    312:                break;
                    313:        default:
                    314:                error("channel %d: chan_write_failed for ostate %d",
                    315:                    c->self, c->ostate);
                    316:                break;
                    317:        }
1.42      markus    318: }
                    319:
                    320: void
1.66      djm       321: chan_mark_dead(struct ssh *ssh, Channel *c)
1.24      markus    322: {
1.26      markus    323:        c->type = SSH_CHANNEL_ZOMBIE;
1.24      markus    324: }
                    325:
1.23      markus    326: int
1.66      djm       327: chan_is_dead(struct ssh *ssh, Channel *c, int do_send)
1.6       markus    328: {
1.26      markus    329:        if (c->type == SSH_CHANNEL_ZOMBIE) {
1.49      markus    330:                debug2("channel %d: zombie", c->self);
1.24      markus    331:                return 1;
1.26      markus    332:        }
1.23      markus    333:        if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
                    334:                return 0;
1.72      djm       335:        if ((ssh->compat & SSH_BUG_EXTEOF) &&
1.45      markus    336:            c->extended_usage == CHAN_EXTENDED_WRITE &&
                    337:            c->efd != -1 &&
1.66      djm       338:            sshbuf_len(c->extended) > 0) {
                    339:                debug2("channel %d: active efd: %d len %zu",
                    340:                    c->self, c->efd, sshbuf_len(c->extended));
1.45      markus    341:                return 0;
                    342:        }
1.63      djm       343:        if (c->flags & CHAN_LOCAL) {
                    344:                debug2("channel %d: is dead (local)", c->self);
                    345:                return 1;
                    346:        }
1.45      markus    347:        if (!(c->flags & CHAN_CLOSE_SENT)) {
1.50      avsm      348:                if (do_send) {
1.66      djm       349:                        chan_send_close2(ssh, c);
1.45      markus    350:                } else {
                    351:                        /* channel would be dead if we sent a close */
                    352:                        if (c->flags & CHAN_CLOSE_RCVD) {
1.49      markus    353:                                debug2("channel %d: almost dead",
1.45      markus    354:                                    c->self);
                    355:                                return 1;
1.32      markus    356:                        }
1.13      markus    357:                }
1.45      markus    358:        }
                    359:        if ((c->flags & CHAN_CLOSE_SENT) &&
                    360:            (c->flags & CHAN_CLOSE_RCVD)) {
1.49      markus    361:                debug2("channel %d: is dead", c->self);
1.45      markus    362:                return 1;
1.1       markus    363:        }
1.23      markus    364:        return 0;
1.13      markus    365: }
                    366:
                    367: /* helper */
                    368: static void
1.66      djm       369: chan_shutdown_write(struct ssh *ssh, Channel *c)
1.13      markus    370: {
1.66      djm       371:        sshbuf_reset(c->output);
1.64      djm       372:        if (c->type == SSH_CHANNEL_LARVAL)
1.13      markus    373:                return;
                    374:        /* shutdown failure is allowed if write failed already */
1.71      djm       375:        debug2_f("channel %d: (i%d o%d sock %d wfd %d efd %d [%s])",
                    376:            c->self, c->istate, c->ostate, c->sock, c->wfd, c->efd,
1.68      djm       377:            channel_format_extended_usage(c));
1.13      markus    378:        if (c->sock != -1) {
1.70      deraadt   379:                if (shutdown(c->sock, SHUT_WR) == -1) {
1.71      djm       380:                        debug2_f("channel %d: shutdown() failed for "
                    381:                            "fd %d [i%d o%d]: %.100s", c->self, c->sock,
                    382:                            c->istate, c->ostate, strerror(errno));
1.68      djm       383:                }
1.13      markus    384:        } else {
1.73    ! djm       385:                if (channel_close_fd(ssh, c, &c->wfd) < 0) {
1.71      djm       386:                        logit_f("channel %d: close() failed for "
                    387:                            "fd %d [i%d o%d]: %.100s", c->self, c->wfd,
                    388:                            c->istate, c->ostate, strerror(errno));
1.68      djm       389:                }
1.13      markus    390:        }
                    391: }
1.64      djm       392:
1.13      markus    393: static void
1.66      djm       394: chan_shutdown_read(struct ssh *ssh, Channel *c)
1.13      markus    395: {
1.64      djm       396:        if (c->type == SSH_CHANNEL_LARVAL)
1.13      markus    397:                return;
1.71      djm       398:        debug2_f("channel %d: (i%d o%d sock %d wfd %d efd %d [%s])",
                    399:            c->self, c->istate, c->ostate, c->sock, c->rfd, c->efd,
1.68      djm       400:            channel_format_extended_usage(c));
1.13      markus    401:        if (c->sock != -1) {
1.70      deraadt   402:                if (shutdown(c->sock, SHUT_RD) == -1) {
1.71      djm       403:                        error_f("channel %d: shutdown() failed for "
                    404:                            "fd %d [i%d o%d]: %.100s", c->self, c->sock,
                    405:                            c->istate, c->ostate, strerror(errno));
1.68      djm       406:                }
1.13      markus    407:        } else {
1.73    ! djm       408:                if (channel_close_fd(ssh, c, &c->rfd) < 0) {
1.71      djm       409:                        logit_f("channel %d: close() failed for "
                    410:                            "fd %d [i%d o%d]: %.100s", c->self, c->rfd,
                    411:                            c->istate, c->ostate, strerror(errno));
1.68      djm       412:                }
1.69      djm       413:        }
                    414: }
                    415:
                    416: static void
                    417: chan_shutdown_extended_read(struct ssh *ssh, Channel *c)
                    418: {
                    419:        if (c->type == SSH_CHANNEL_LARVAL || c->efd == -1)
                    420:                return;
                    421:        if (c->extended_usage != CHAN_EXTENDED_READ &&
                    422:            c->extended_usage != CHAN_EXTENDED_IGNORE)
                    423:                return;
1.71      djm       424:        debug_f("channel %d: (i%d o%d sock %d wfd %d efd %d [%s])",
                    425:            c->self, c->istate, c->ostate, c->sock, c->rfd, c->efd,
1.69      djm       426:            channel_format_extended_usage(c));
1.73    ! djm       427:        if (channel_close_fd(ssh, c, &c->efd) < 0) {
1.71      djm       428:                logit_f("channel %d: close() failed for "
                    429:                    "extended fd %d [i%d o%d]: %.100s", c->self, c->efd,
                    430:                    c->istate, c->ostate, strerror(errno));
1.13      markus    431:        }
1.1       markus    432: }