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

Annotation of src/usr.bin/ssh/channels.c, Revision 1.210.2.2

1.1       deraadt     1: /*
1.26      deraadt     2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
                      5:  * This file contains functions for generic socket connection forwarding.
                      6:  * There is also code for initiating connection forwarding for X11 connections,
                      7:  * arbitrary tcp/ip connections, and the authentication agent connection.
1.49      markus      8:  *
1.67      deraadt     9:  * As far as I am concerned, the code I have written for this software
                     10:  * can be used freely for any purpose.  Any derived versions of this
                     11:  * software must be clearly marked as such, and if the derived work is
                     12:  * incompatible with the protocol description in the RFC file, it must be
                     13:  * called by a name other than "ssh" or "Secure Shell".
                     14:  *
1.44      markus     15:  * SSH2 support added by Markus Friedl.
1.156     markus     16:  * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl.  All rights reserved.
1.67      deraadt    17:  * Copyright (c) 1999 Dug Song.  All rights reserved.
                     18:  * Copyright (c) 1999 Theo de Raadt.  All rights reserved.
                     19:  *
                     20:  * Redistribution and use in source and binary forms, with or without
                     21:  * modification, are permitted provided that the following conditions
                     22:  * are met:
                     23:  * 1. Redistributions of source code must retain the above copyright
                     24:  *    notice, this list of conditions and the following disclaimer.
                     25:  * 2. Redistributions in binary form must reproduce the above copyright
                     26:  *    notice, this list of conditions and the following disclaimer in the
                     27:  *    documentation and/or other materials provided with the distribution.
                     28:  *
                     29:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     30:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     31:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     32:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     33:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     34:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     35:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     36:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     37:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     38:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.26      deraadt    39:  */
1.1       deraadt    40:
                     41: #include "includes.h"
1.210.2.2! brad       42: RCSID("$OpenBSD: channels.c,v 1.214 2005/03/14 11:46:56 markus Exp $");
1.1       deraadt    43:
                     44: #include "ssh.h"
1.82      markus     45: #include "ssh1.h"
                     46: #include "ssh2.h"
1.1       deraadt    47: #include "packet.h"
                     48: #include "xmalloc.h"
1.82      markus     49: #include "log.h"
                     50: #include "misc.h"
1.14      markus     51: #include "channels.h"
                     52: #include "compat.h"
1.82      markus     53: #include "canohost.h"
1.64      markus     54: #include "key.h"
                     55: #include "authfd.h"
1.147     stevesk    56: #include "pathnames.h"
1.193     markus     57: #include "bufaux.h"
1.1       deraadt    58:
1.121     markus     59: /* -- channel core */
1.12      markus     60:
1.210.2.2! brad       61: #define CHAN_RBUF      16*1024
        !            62:
1.27      markus     63: /*
                     64:  * Pointer to an array containing all allocated channels.  The array is
                     65:  * dynamically extended as needed.
                     66:  */
1.113     markus     67: static Channel **channels = NULL;
1.1       deraadt    68:
1.27      markus     69: /*
                     70:  * Size of the channel array.  All slots of the array must always be
1.113     markus     71:  * initialized (at least the type field); unused slots set to NULL
1.27      markus     72:  */
1.209     avsm       73: static u_int channels_alloc = 0;
1.1       deraadt    74:
1.27      markus     75: /*
                     76:  * Maximum file descriptor value used in any of the channels.  This is
1.113     markus     77:  * updated in channel_new.
1.27      markus     78:  */
1.84      markus     79: static int channel_max_fd = 0;
1.1       deraadt    80:
                     81:
1.121     markus     82: /* -- tcp forwarding */
1.1       deraadt    83:
1.27      markus     84: /*
                     85:  * Data structure for storing which hosts are permitted for forward requests.
                     86:  * The local sides of any remote forwards are stored in this array to prevent
                     87:  * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
                     88:  * network (which might be behind a firewall).
                     89:  */
1.25      markus     90: typedef struct {
1.41      markus     91:        char *host_to_connect;          /* Connect to 'host'. */
                     92:        u_short port_to_connect;        /* Connect to 'port'. */
                     93:        u_short listen_port;            /* Remote side should listen port number. */
1.1       deraadt    94: } ForwardPermission;
                     95:
                     96: /* List of all permitted host/port pairs to connect. */
                     97: static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
1.121     markus     98:
1.1       deraadt    99: /* Number of permitted host/port pairs in the array. */
                    100: static int num_permitted_opens = 0;
1.27      markus    101: /*
                    102:  * If this is true, all opens are permitted.  This is the case on the server
                    103:  * on which we have to trust the client anyway, and the user could do
                    104:  * anything after logging in anyway.
                    105:  */
1.1       deraadt   106: static int all_opens_permitted = 0;
                    107:
1.121     markus    108:
                    109: /* -- X11 forwarding */
                    110:
                    111: /* Maximum number of fake X11 displays to try. */
                    112: #define MAX_DISPLAYS  1000
                    113:
                    114: /* Saved X11 authentication protocol name. */
                    115: static char *x11_saved_proto = NULL;
                    116:
                    117: /* Saved X11 authentication data.  This is the real data. */
                    118: static char *x11_saved_data = NULL;
                    119: static u_int x11_saved_data_len = 0;
                    120:
                    121: /*
                    122:  * Fake X11 authentication data.  This is what the server will be sending us;
                    123:  * we should replace any occurrences of this by the real data.
                    124:  */
                    125: static char *x11_fake_data = NULL;
                    126: static u_int x11_fake_data_len;
                    127:
                    128:
                    129: /* -- agent forwarding */
                    130:
                    131: #define        NUM_SOCKS       10
                    132:
1.82      markus    133: /* AF_UNSPEC or AF_INET or AF_INET6 */
1.135     markus    134: static int IPv4or6 = AF_UNSPEC;
1.1       deraadt   135:
1.121     markus    136: /* helper */
1.127     itojun    137: static void port_open_helper(Channel *c, char *rtype);
1.103     markus    138:
1.121     markus    139: /* -- channel core */
1.41      markus    140:
                    141: Channel *
                    142: channel_lookup(int id)
                    143: {
                    144:        Channel *c;
1.113     markus    145:
1.209     avsm      146:        if (id < 0 || (u_int)id >= channels_alloc) {
1.188     itojun    147:                logit("channel_lookup: %d: bad id", id);
1.41      markus    148:                return NULL;
                    149:        }
1.113     markus    150:        c = channels[id];
                    151:        if (c == NULL) {
1.188     itojun    152:                logit("channel_lookup: %d: bad id: channel free", id);
1.41      markus    153:                return NULL;
                    154:        }
                    155:        return c;
1.55      markus    156: }
                    157:
1.27      markus    158: /*
1.55      markus    159:  * Register filedescriptors for a channel, used when allocating a channel or
1.52      markus    160:  * when the channel consumer/producer is ready, e.g. shell exec'd
1.27      markus    161:  */
1.1       deraadt   162:
1.127     itojun    163: static void
1.71      markus    164: channel_register_fds(Channel *c, int rfd, int wfd, int efd,
                    165:     int extusage, int nonblock)
1.1       deraadt   166: {
1.25      markus    167:        /* Update the maximum file descriptor value. */
1.84      markus    168:        channel_max_fd = MAX(channel_max_fd, rfd);
                    169:        channel_max_fd = MAX(channel_max_fd, wfd);
                    170:        channel_max_fd = MAX(channel_max_fd, efd);
                    171:
1.27      markus    172:        /* XXX set close-on-exec -markus */
1.55      markus    173:
1.52      markus    174:        c->rfd = rfd;
                    175:        c->wfd = wfd;
                    176:        c->sock = (rfd == wfd) ? rfd : -1;
1.204     djm       177:        c->ctl_fd = -1; /* XXX: set elsewhere */
1.52      markus    178:        c->efd = efd;
                    179:        c->extended_usage = extusage;
1.71      markus    180:
1.91      markus    181:        /* XXX ugly hack: nonblock is only set by the server */
                    182:        if (nonblock && isatty(c->rfd)) {
1.194     markus    183:                debug2("channel %d: rfd %d isatty", c->self, c->rfd);
1.91      markus    184:                c->isatty = 1;
                    185:                if (!isatty(c->wfd)) {
1.94      markus    186:                        error("channel %d: wfd %d is not a tty?",
1.91      markus    187:                            c->self, c->wfd);
                    188:                }
                    189:        } else {
                    190:                c->isatty = 0;
                    191:        }
                    192:
1.71      markus    193:        /* enable nonblocking mode */
                    194:        if (nonblock) {
                    195:                if (rfd != -1)
                    196:                        set_nonblock(rfd);
                    197:                if (wfd != -1)
                    198:                        set_nonblock(wfd);
                    199:                if (efd != -1)
                    200:                        set_nonblock(efd);
                    201:        }
1.52      markus    202: }
                    203:
                    204: /*
                    205:  * Allocate a new channel object and set its type and socket. This will cause
                    206:  * remote_name to be freed.
                    207:  */
                    208:
1.113     markus    209: Channel *
1.52      markus    210: channel_new(char *ctype, int type, int rfd, int wfd, int efd,
1.178     markus    211:     u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
1.52      markus    212: {
1.209     avsm      213:        int found;
                    214:        u_int i;
1.52      markus    215:        Channel *c;
1.25      markus    216:
                    217:        /* Do initial allocation if this is the first call. */
                    218:        if (channels_alloc == 0) {
                    219:                channels_alloc = 10;
1.113     markus    220:                channels = xmalloc(channels_alloc * sizeof(Channel *));
1.25      markus    221:                for (i = 0; i < channels_alloc; i++)
1.113     markus    222:                        channels[i] = NULL;
1.25      markus    223:        }
                    224:        /* Try to find a free slot where to put the new channel. */
                    225:        for (found = -1, i = 0; i < channels_alloc; i++)
1.113     markus    226:                if (channels[i] == NULL) {
1.25      markus    227:                        /* Found a free slot. */
1.209     avsm      228:                        found = (int)i;
1.25      markus    229:                        break;
                    230:                }
1.209     avsm      231:        if (found < 0) {
1.27      markus    232:                /* There are no free slots.  Take last+1 slot and expand the array.  */
1.25      markus    233:                found = channels_alloc;
1.179     markus    234:                if (channels_alloc > 10000)
                    235:                        fatal("channel_new: internal error: channels_alloc %d "
                    236:                            "too big.", channels_alloc);
1.195     markus    237:                channels = xrealloc(channels,
                    238:                    (channels_alloc + 10) * sizeof(Channel *));
                    239:                channels_alloc += 10;
1.70      markus    240:                debug2("channel: expanding %d", channels_alloc);
1.25      markus    241:                for (i = found; i < channels_alloc; i++)
1.113     markus    242:                        channels[i] = NULL;
1.25      markus    243:        }
1.113     markus    244:        /* Initialize and return new channel. */
                    245:        c = channels[found] = xmalloc(sizeof(Channel));
1.137     markus    246:        memset(c, 0, sizeof(Channel));
1.25      markus    247:        buffer_init(&c->input);
                    248:        buffer_init(&c->output);
1.41      markus    249:        buffer_init(&c->extended);
1.159     markus    250:        c->ostate = CHAN_OUTPUT_OPEN;
                    251:        c->istate = CHAN_INPUT_OPEN;
                    252:        c->flags = 0;
1.71      markus    253:        channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
1.25      markus    254:        c->self = found;
                    255:        c->type = type;
1.41      markus    256:        c->ctype = ctype;
1.51      markus    257:        c->local_window = window;
                    258:        c->local_window_max = window;
                    259:        c->local_consumed = 0;
                    260:        c->local_maxpacket = maxpack;
1.25      markus    261:        c->remote_id = -1;
1.190     markus    262:        c->remote_name = xstrdup(remote_name);
1.41      markus    263:        c->remote_window = 0;
                    264:        c->remote_maxpacket = 0;
1.133     markus    265:        c->force_drain = 0;
1.149     markus    266:        c->single_connection = 0;
1.131     markus    267:        c->detach_user = NULL;
1.165     markus    268:        c->confirm = NULL;
1.204     djm       269:        c->confirm_ctx = NULL;
1.65      markus    270:        c->input_filter = NULL;
1.25      markus    271:        debug("channel %d: new [%s]", found, remote_name);
1.113     markus    272:        return c;
1.1       deraadt   273: }
1.52      markus    274:
1.132     markus    275: static int
                    276: channel_find_maxfd(void)
                    277: {
1.209     avsm      278:        u_int i;
                    279:        int max = 0;
1.132     markus    280:        Channel *c;
                    281:
                    282:        for (i = 0; i < channels_alloc; i++) {
                    283:                c = channels[i];
                    284:                if (c != NULL) {
                    285:                        max = MAX(max, c->rfd);
                    286:                        max = MAX(max, c->wfd);
                    287:                        max = MAX(max, c->efd);
                    288:                }
                    289:        }
                    290:        return max;
                    291: }
                    292:
                    293: int
                    294: channel_close_fd(int *fdp)
                    295: {
                    296:        int ret = 0, fd = *fdp;
                    297:
                    298:        if (fd != -1) {
                    299:                ret = close(fd);
                    300:                *fdp = -1;
                    301:                if (fd == channel_max_fd)
                    302:                        channel_max_fd = channel_find_maxfd();
                    303:        }
                    304:        return ret;
                    305: }
                    306:
1.52      markus    307: /* Close all channel fd/socket. */
                    308:
1.127     itojun    309: static void
1.52      markus    310: channel_close_fds(Channel *c)
                    311: {
1.204     djm       312:        debug3("channel %d: close_fds r %d w %d e %d c %d",
                    313:            c->self, c->rfd, c->wfd, c->efd, c->ctl_fd);
1.118     markus    314:
1.132     markus    315:        channel_close_fd(&c->sock);
1.204     djm       316:        channel_close_fd(&c->ctl_fd);
1.132     markus    317:        channel_close_fd(&c->rfd);
                    318:        channel_close_fd(&c->wfd);
                    319:        channel_close_fd(&c->efd);
1.121     markus    320: }
                    321:
                    322: /* Free the channel and close its fd/socket. */
                    323:
                    324: void
                    325: channel_free(Channel *c)
                    326: {
                    327:        char *s;
1.209     avsm      328:        u_int i, n;
1.121     markus    329:
                    330:        for (n = 0, i = 0; i < channels_alloc; i++)
                    331:                if (channels[i])
                    332:                        n++;
1.209     avsm      333:        debug("channel %d: free: %s, nchannels %u", c->self,
1.121     markus    334:            c->remote_name ? c->remote_name : "???", n);
                    335:
                    336:        s = channel_open_message();
1.194     markus    337:        debug3("channel %d: status: %s", c->self, s);
1.121     markus    338:        xfree(s);
                    339:
                    340:        if (c->sock != -1)
                    341:                shutdown(c->sock, SHUT_RDWR);
1.204     djm       342:        if (c->ctl_fd != -1)
                    343:                shutdown(c->ctl_fd, SHUT_RDWR);
1.121     markus    344:        channel_close_fds(c);
                    345:        buffer_free(&c->input);
                    346:        buffer_free(&c->output);
                    347:        buffer_free(&c->extended);
                    348:        if (c->remote_name) {
                    349:                xfree(c->remote_name);
                    350:                c->remote_name = NULL;
                    351:        }
                    352:        channels[c->self] = NULL;
                    353:        xfree(c);
                    354: }
                    355:
                    356: void
1.126     markus    357: channel_free_all(void)
1.121     markus    358: {
1.209     avsm      359:        u_int i;
1.121     markus    360:
1.126     markus    361:        for (i = 0; i < channels_alloc; i++)
                    362:                if (channels[i] != NULL)
                    363:                        channel_free(channels[i]);
1.131     markus    364: }
                    365:
1.121     markus    366: /*
                    367:  * Closes the sockets/fds of all channels.  This is used to close extra file
                    368:  * descriptors after a fork.
                    369:  */
                    370:
                    371: void
1.142     itojun    372: channel_close_all(void)
1.121     markus    373: {
1.209     avsm      374:        u_int i;
1.121     markus    375:
                    376:        for (i = 0; i < channels_alloc; i++)
                    377:                if (channels[i] != NULL)
                    378:                        channel_close_fds(channels[i]);
                    379: }
                    380:
                    381: /*
1.131     markus    382:  * Stop listening to channels.
                    383:  */
                    384:
                    385: void
                    386: channel_stop_listening(void)
                    387: {
1.209     avsm      388:        u_int i;
1.131     markus    389:        Channel *c;
                    390:
                    391:        for (i = 0; i < channels_alloc; i++) {
                    392:                c = channels[i];
                    393:                if (c != NULL) {
                    394:                        switch (c->type) {
                    395:                        case SSH_CHANNEL_AUTH_SOCKET:
                    396:                        case SSH_CHANNEL_PORT_LISTENER:
                    397:                        case SSH_CHANNEL_RPORT_LISTENER:
                    398:                        case SSH_CHANNEL_X11_LISTENER:
1.132     markus    399:                                channel_close_fd(&c->sock);
1.131     markus    400:                                channel_free(c);
                    401:                                break;
                    402:                        }
                    403:                }
                    404:        }
                    405: }
                    406:
                    407: /*
1.121     markus    408:  * Returns true if no channel has too much buffered data, and false if one or
                    409:  * more channel is overfull.
                    410:  */
                    411:
                    412: int
1.142     itojun    413: channel_not_very_much_buffered_data(void)
1.121     markus    414: {
                    415:        u_int i;
                    416:        Channel *c;
                    417:
                    418:        for (i = 0; i < channels_alloc; i++) {
                    419:                c = channels[i];
                    420:                if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
1.136     markus    421: #if 0
                    422:                        if (!compat20 &&
                    423:                            buffer_len(&c->input) > packet_get_maxsize()) {
1.185     markus    424:                                debug2("channel %d: big input buffer %d",
1.121     markus    425:                                    c->self, buffer_len(&c->input));
                    426:                                return 0;
                    427:                        }
1.136     markus    428: #endif
1.121     markus    429:                        if (buffer_len(&c->output) > packet_get_maxsize()) {
1.191     markus    430:                                debug2("channel %d: big output buffer %u > %u",
1.136     markus    431:                                    c->self, buffer_len(&c->output),
                    432:                                    packet_get_maxsize());
1.121     markus    433:                                return 0;
                    434:                        }
                    435:                }
                    436:        }
                    437:        return 1;
                    438: }
                    439:
                    440: /* Returns true if any channel is still open. */
                    441:
                    442: int
1.142     itojun    443: channel_still_open(void)
1.121     markus    444: {
1.209     avsm      445:        u_int i;
1.121     markus    446:        Channel *c;
                    447:
                    448:        for (i = 0; i < channels_alloc; i++) {
                    449:                c = channels[i];
                    450:                if (c == NULL)
                    451:                        continue;
                    452:                switch (c->type) {
                    453:                case SSH_CHANNEL_X11_LISTENER:
                    454:                case SSH_CHANNEL_PORT_LISTENER:
                    455:                case SSH_CHANNEL_RPORT_LISTENER:
                    456:                case SSH_CHANNEL_CLOSED:
                    457:                case SSH_CHANNEL_AUTH_SOCKET:
                    458:                case SSH_CHANNEL_DYNAMIC:
                    459:                case SSH_CHANNEL_CONNECTING:
                    460:                case SSH_CHANNEL_ZOMBIE:
                    461:                        continue;
                    462:                case SSH_CHANNEL_LARVAL:
                    463:                        if (!compat20)
                    464:                                fatal("cannot happen: SSH_CHANNEL_LARVAL");
                    465:                        continue;
                    466:                case SSH_CHANNEL_OPENING:
                    467:                case SSH_CHANNEL_OPEN:
                    468:                case SSH_CHANNEL_X11_OPEN:
                    469:                        return 1;
                    470:                case SSH_CHANNEL_INPUT_DRAINING:
                    471:                case SSH_CHANNEL_OUTPUT_DRAINING:
                    472:                        if (!compat13)
                    473:                                fatal("cannot happen: OUT_DRAIN");
                    474:                        return 1;
                    475:                default:
                    476:                        fatal("channel_still_open: bad channel type %d", c->type);
                    477:                        /* NOTREACHED */
                    478:                }
                    479:        }
                    480:        return 0;
                    481: }
                    482:
                    483: /* Returns the id of an open channel suitable for keepaliving */
                    484:
                    485: int
1.142     itojun    486: channel_find_open(void)
1.121     markus    487: {
1.209     avsm      488:        u_int i;
1.121     markus    489:        Channel *c;
                    490:
                    491:        for (i = 0; i < channels_alloc; i++) {
                    492:                c = channels[i];
1.206     djm       493:                if (c == NULL || c->remote_id < 0)
1.121     markus    494:                        continue;
                    495:                switch (c->type) {
                    496:                case SSH_CHANNEL_CLOSED:
                    497:                case SSH_CHANNEL_DYNAMIC:
                    498:                case SSH_CHANNEL_X11_LISTENER:
                    499:                case SSH_CHANNEL_PORT_LISTENER:
                    500:                case SSH_CHANNEL_RPORT_LISTENER:
                    501:                case SSH_CHANNEL_OPENING:
                    502:                case SSH_CHANNEL_CONNECTING:
                    503:                case SSH_CHANNEL_ZOMBIE:
                    504:                        continue;
                    505:                case SSH_CHANNEL_LARVAL:
                    506:                case SSH_CHANNEL_AUTH_SOCKET:
                    507:                case SSH_CHANNEL_OPEN:
                    508:                case SSH_CHANNEL_X11_OPEN:
                    509:                        return i;
                    510:                case SSH_CHANNEL_INPUT_DRAINING:
                    511:                case SSH_CHANNEL_OUTPUT_DRAINING:
                    512:                        if (!compat13)
                    513:                                fatal("cannot happen: OUT_DRAIN");
                    514:                        return i;
                    515:                default:
                    516:                        fatal("channel_find_open: bad channel type %d", c->type);
                    517:                        /* NOTREACHED */
                    518:                }
                    519:        }
                    520:        return -1;
                    521: }
                    522:
                    523:
                    524: /*
                    525:  * Returns a message describing the currently open forwarded connections,
                    526:  * suitable for sending to the client.  The message contains crlf pairs for
                    527:  * newlines.
                    528:  */
                    529:
                    530: char *
1.142     itojun    531: channel_open_message(void)
1.121     markus    532: {
                    533:        Buffer buffer;
                    534:        Channel *c;
                    535:        char buf[1024], *cp;
1.209     avsm      536:        u_int i;
1.121     markus    537:
                    538:        buffer_init(&buffer);
                    539:        snprintf(buf, sizeof buf, "The following connections are open:\r\n");
                    540:        buffer_append(&buffer, buf, strlen(buf));
                    541:        for (i = 0; i < channels_alloc; i++) {
                    542:                c = channels[i];
                    543:                if (c == NULL)
                    544:                        continue;
                    545:                switch (c->type) {
                    546:                case SSH_CHANNEL_X11_LISTENER:
                    547:                case SSH_CHANNEL_PORT_LISTENER:
                    548:                case SSH_CHANNEL_RPORT_LISTENER:
                    549:                case SSH_CHANNEL_CLOSED:
                    550:                case SSH_CHANNEL_AUTH_SOCKET:
                    551:                case SSH_CHANNEL_ZOMBIE:
                    552:                        continue;
                    553:                case SSH_CHANNEL_LARVAL:
                    554:                case SSH_CHANNEL_OPENING:
                    555:                case SSH_CHANNEL_CONNECTING:
                    556:                case SSH_CHANNEL_DYNAMIC:
                    557:                case SSH_CHANNEL_OPEN:
                    558:                case SSH_CHANNEL_X11_OPEN:
                    559:                case SSH_CHANNEL_INPUT_DRAINING:
                    560:                case SSH_CHANNEL_OUTPUT_DRAINING:
1.204     djm       561:                        snprintf(buf, sizeof buf,
                    562:                            "  #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d cfd %d)\r\n",
1.121     markus    563:                            c->self, c->remote_name,
                    564:                            c->type, c->remote_id,
                    565:                            c->istate, buffer_len(&c->input),
                    566:                            c->ostate, buffer_len(&c->output),
1.204     djm       567:                            c->rfd, c->wfd, c->ctl_fd);
1.121     markus    568:                        buffer_append(&buffer, buf, strlen(buf));
                    569:                        continue;
                    570:                default:
                    571:                        fatal("channel_open_message: bad channel type %d", c->type);
                    572:                        /* NOTREACHED */
                    573:                }
                    574:        }
                    575:        buffer_append(&buffer, "\0", 1);
                    576:        cp = xstrdup(buffer_ptr(&buffer));
                    577:        buffer_free(&buffer);
                    578:        return cp;
                    579: }
                    580:
                    581: void
                    582: channel_send_open(int id)
                    583: {
                    584:        Channel *c = channel_lookup(id);
1.180     deraadt   585:
1.121     markus    586:        if (c == NULL) {
1.188     itojun    587:                logit("channel_send_open: %d: bad id", id);
1.121     markus    588:                return;
                    589:        }
1.184     markus    590:        debug2("channel %d: send open", id);
1.121     markus    591:        packet_start(SSH2_MSG_CHANNEL_OPEN);
                    592:        packet_put_cstring(c->ctype);
                    593:        packet_put_int(c->self);
                    594:        packet_put_int(c->local_window);
                    595:        packet_put_int(c->local_maxpacket);
                    596:        packet_send();
                    597: }
                    598:
                    599: void
1.184     markus    600: channel_request_start(int id, char *service, int wantconfirm)
1.121     markus    601: {
1.184     markus    602:        Channel *c = channel_lookup(id);
1.180     deraadt   603:
1.121     markus    604:        if (c == NULL) {
1.188     itojun    605:                logit("channel_request_start: %d: unknown channel id", id);
1.121     markus    606:                return;
                    607:        }
1.204     djm       608:        debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
1.121     markus    609:        packet_start(SSH2_MSG_CHANNEL_REQUEST);
                    610:        packet_put_int(c->remote_id);
                    611:        packet_put_cstring(service);
                    612:        packet_put_char(wantconfirm);
                    613: }
                    614: void
1.204     djm       615: channel_register_confirm(int id, channel_callback_fn *fn, void *ctx)
1.121     markus    616: {
                    617:        Channel *c = channel_lookup(id);
1.180     deraadt   618:
1.121     markus    619:        if (c == NULL) {
1.188     itojun    620:                logit("channel_register_comfirm: %d: bad id", id);
1.121     markus    621:                return;
                    622:        }
1.165     markus    623:        c->confirm = fn;
1.204     djm       624:        c->confirm_ctx = ctx;
1.121     markus    625: }
                    626: void
                    627: channel_register_cleanup(int id, channel_callback_fn *fn)
                    628: {
                    629:        Channel *c = channel_lookup(id);
1.180     deraadt   630:
1.121     markus    631:        if (c == NULL) {
1.188     itojun    632:                logit("channel_register_cleanup: %d: bad id", id);
1.121     markus    633:                return;
                    634:        }
1.131     markus    635:        c->detach_user = fn;
1.121     markus    636: }
                    637: void
                    638: channel_cancel_cleanup(int id)
                    639: {
                    640:        Channel *c = channel_lookup(id);
1.180     deraadt   641:
1.121     markus    642:        if (c == NULL) {
1.188     itojun    643:                logit("channel_cancel_cleanup: %d: bad id", id);
1.121     markus    644:                return;
1.52      markus    645:        }
1.131     markus    646:        c->detach_user = NULL;
1.121     markus    647: }
                    648: void
                    649: channel_register_filter(int id, channel_filter_fn *fn)
                    650: {
                    651:        Channel *c = channel_lookup(id);
1.180     deraadt   652:
1.121     markus    653:        if (c == NULL) {
1.188     itojun    654:                logit("channel_register_filter: %d: bad id", id);
1.121     markus    655:                return;
1.52      markus    656:        }
1.121     markus    657:        c->input_filter = fn;
1.52      markus    658: }
                    659:
1.49      markus    660: void
1.121     markus    661: channel_set_fds(int id, int rfd, int wfd, int efd,
1.168     markus    662:     int extusage, int nonblock, u_int window_max)
1.1       deraadt   663: {
1.121     markus    664:        Channel *c = channel_lookup(id);
1.180     deraadt   665:
1.121     markus    666:        if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
                    667:                fatal("channel_activate for non-larval channel %d.", id);
                    668:        channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
                    669:        c->type = SSH_CHANNEL_OPEN;
1.168     markus    670:        c->local_window = c->local_window_max = window_max;
1.121     markus    671:        packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
                    672:        packet_put_int(c->remote_id);
                    673:        packet_put_int(c->local_window);
                    674:        packet_send();
1.1       deraadt   675: }
                    676:
1.27      markus    677: /*
1.41      markus    678:  * 'channel_pre*' are called just before select() to add any bits relevant to
                    679:  * channels in the select bitmasks.
                    680:  */
                    681: /*
                    682:  * 'channel_post*': perform any appropriate operations for channels which
                    683:  * have events pending.
1.27      markus    684:  */
1.41      markus    685: typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
                    686: chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
                    687: chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
                    688:
1.127     itojun    689: static void
1.41      markus    690: channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
                    691: {
                    692:        FD_SET(c->sock, readset);
                    693: }
                    694:
1.127     itojun    695: static void
1.75      markus    696: channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
                    697: {
                    698:        debug3("channel %d: waiting for connection", c->self);
                    699:        FD_SET(c->sock, writeset);
                    700: }
                    701:
1.127     itojun    702: static void
1.41      markus    703: channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
                    704: {
                    705:        if (buffer_len(&c->input) < packet_get_maxsize())
                    706:                FD_SET(c->sock, readset);
                    707:        if (buffer_len(&c->output) > 0)
                    708:                FD_SET(c->sock, writeset);
                    709: }
1.1       deraadt   710:
1.127     itojun    711: static void
1.157     markus    712: channel_pre_open(Channel *c, fd_set * readset, fd_set * writeset)
1.41      markus    713: {
1.157     markus    714:        u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
1.41      markus    715:
1.210.2.2! brad      716:        /* check buffer limits */
        !           717:        limit = MIN(limit, (BUFFER_MAX_LEN - BUFFER_MAX_CHUNK - CHAN_RBUF));
        !           718:
1.44      markus    719:        if (c->istate == CHAN_INPUT_OPEN &&
1.157     markus    720:            limit > 0 &&
                    721:            buffer_len(&c->input) < limit)
1.44      markus    722:                FD_SET(c->rfd, readset);
                    723:        if (c->ostate == CHAN_OUTPUT_OPEN ||
                    724:            c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
                    725:                if (buffer_len(&c->output) > 0) {
                    726:                        FD_SET(c->wfd, writeset);
                    727:                } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1.172     markus    728:                        if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
1.174     markus    729:                               debug2("channel %d: obuf_empty delayed efd %d/(%d)",
                    730:                                   c->self, c->efd, buffer_len(&c->extended));
1.172     markus    731:                        else
                    732:                                chan_obuf_empty(c);
1.44      markus    733:                }
                    734:        }
                    735:        /** XXX check close conditions, too */
1.157     markus    736:        if (compat20 && c->efd != -1) {
1.44      markus    737:                if (c->extended_usage == CHAN_EXTENDED_WRITE &&
                    738:                    buffer_len(&c->extended) > 0)
                    739:                        FD_SET(c->efd, writeset);
1.172     markus    740:                else if (!(c->flags & CHAN_EOF_SENT) &&
                    741:                    c->extended_usage == CHAN_EXTENDED_READ &&
1.44      markus    742:                    buffer_len(&c->extended) < c->remote_window)
                    743:                        FD_SET(c->efd, readset);
                    744:        }
1.204     djm       745:        /* XXX: What about efd? races? */
1.208     deraadt   746:        if (compat20 && c->ctl_fd != -1 &&
1.204     djm       747:            c->istate == CHAN_INPUT_OPEN && c->ostate == CHAN_OUTPUT_OPEN)
                    748:                FD_SET(c->ctl_fd, readset);
1.44      markus    749: }
                    750:
1.127     itojun    751: static void
1.41      markus    752: channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
                    753: {
                    754:        if (buffer_len(&c->input) == 0) {
                    755:                packet_start(SSH_MSG_CHANNEL_CLOSE);
                    756:                packet_put_int(c->remote_id);
                    757:                packet_send();
                    758:                c->type = SSH_CHANNEL_CLOSED;
1.194     markus    759:                debug2("channel %d: closing after input drain.", c->self);
1.41      markus    760:        }
                    761: }
                    762:
1.127     itojun    763: static void
1.41      markus    764: channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
                    765: {
                    766:        if (buffer_len(&c->output) == 0)
1.113     markus    767:                chan_mark_dead(c);
1.49      markus    768:        else
1.41      markus    769:                FD_SET(c->sock, writeset);
                    770: }
                    771:
                    772: /*
                    773:  * This is a special state for X11 authentication spoofing.  An opened X11
                    774:  * connection (when authentication spoofing is being done) remains in this
                    775:  * state until the first packet has been completely read.  The authentication
                    776:  * data in that packet is then substituted by the real data if it matches the
                    777:  * fake data, and the channel is put into normal mode.
1.51      markus    778:  * XXX All this happens at the client side.
1.121     markus    779:  * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
1.41      markus    780:  */
1.127     itojun    781: static int
1.121     markus    782: x11_open_helper(Buffer *b)
1.1       deraadt   783: {
1.77      markus    784:        u_char *ucp;
                    785:        u_int proto_len, data_len;
1.25      markus    786:
1.41      markus    787:        /* Check if the fixed size part of the packet is in buffer. */
1.121     markus    788:        if (buffer_len(b) < 12)
1.41      markus    789:                return 0;
                    790:
                    791:        /* Parse the lengths of variable-length fields. */
1.155     stevesk   792:        ucp = buffer_ptr(b);
1.41      markus    793:        if (ucp[0] == 0x42) {   /* Byte order MSB first. */
                    794:                proto_len = 256 * ucp[6] + ucp[7];
                    795:                data_len = 256 * ucp[8] + ucp[9];
                    796:        } else if (ucp[0] == 0x6c) {    /* Byte order LSB first. */
                    797:                proto_len = ucp[6] + 256 * ucp[7];
                    798:                data_len = ucp[8] + 256 * ucp[9];
                    799:        } else {
1.194     markus    800:                debug2("Initial X11 packet contains bad byte order byte: 0x%x",
1.148     deraadt   801:                    ucp[0]);
1.41      markus    802:                return -1;
                    803:        }
                    804:
                    805:        /* Check if the whole packet is in buffer. */
1.121     markus    806:        if (buffer_len(b) <
1.41      markus    807:            12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
                    808:                return 0;
                    809:
                    810:        /* Check if authentication protocol matches. */
                    811:        if (proto_len != strlen(x11_saved_proto) ||
                    812:            memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
1.194     markus    813:                debug2("X11 connection uses different authentication protocol.");
1.41      markus    814:                return -1;
                    815:        }
                    816:        /* Check if authentication data matches our fake data. */
                    817:        if (data_len != x11_fake_data_len ||
                    818:            memcmp(ucp + 12 + ((proto_len + 3) & ~3),
                    819:                x11_fake_data, x11_fake_data_len) != 0) {
1.194     markus    820:                debug2("X11 auth data does not match fake data.");
1.41      markus    821:                return -1;
                    822:        }
                    823:        /* Check fake data length */
                    824:        if (x11_fake_data_len != x11_saved_data_len) {
                    825:                error("X11 fake_data_len %d != saved_data_len %d",
                    826:                    x11_fake_data_len, x11_saved_data_len);
                    827:                return -1;
                    828:        }
                    829:        /*
                    830:         * Received authentication protocol and data match
                    831:         * our fake data. Substitute the fake data with real
                    832:         * data.
                    833:         */
                    834:        memcpy(ucp + 12 + ((proto_len + 3) & ~3),
                    835:            x11_saved_data, x11_saved_data_len);
                    836:        return 1;
                    837: }
                    838:
1.127     itojun    839: static void
1.41      markus    840: channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
                    841: {
1.121     markus    842:        int ret = x11_open_helper(&c->output);
1.180     deraadt   843:
1.41      markus    844:        if (ret == 1) {
                    845:                /* Start normal processing for the channel. */
                    846:                c->type = SSH_CHANNEL_OPEN;
1.47      markus    847:                channel_pre_open_13(c, readset, writeset);
1.41      markus    848:        } else if (ret == -1) {
                    849:                /*
                    850:                 * We have received an X11 connection that has bad
                    851:                 * authentication information.
                    852:                 */
1.188     itojun    853:                logit("X11 connection rejected because of wrong authentication.");
1.41      markus    854:                buffer_clear(&c->input);
                    855:                buffer_clear(&c->output);
1.132     markus    856:                channel_close_fd(&c->sock);
1.41      markus    857:                c->sock = -1;
                    858:                c->type = SSH_CHANNEL_CLOSED;
                    859:                packet_start(SSH_MSG_CHANNEL_CLOSE);
                    860:                packet_put_int(c->remote_id);
                    861:                packet_send();
                    862:        }
                    863: }
1.25      markus    864:
1.127     itojun    865: static void
1.51      markus    866: channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
1.41      markus    867: {
1.121     markus    868:        int ret = x11_open_helper(&c->output);
1.133     markus    869:
                    870:        /* c->force_drain = 1; */
                    871:
1.41      markus    872:        if (ret == 1) {
                    873:                c->type = SSH_CHANNEL_OPEN;
1.157     markus    874:                channel_pre_open(c, readset, writeset);
1.41      markus    875:        } else if (ret == -1) {
1.188     itojun    876:                logit("X11 connection rejected because of wrong authentication.");
1.194     markus    877:                debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
1.156     markus    878:                chan_read_failed(c);
                    879:                buffer_clear(&c->input);
                    880:                chan_ibuf_empty(c);
                    881:                buffer_clear(&c->output);
                    882:                /* for proto v1, the peer will send an IEOF */
                    883:                if (compat20)
                    884:                        chan_write_failed(c);
                    885:                else
                    886:                        c->type = SSH_CHANNEL_OPEN;
1.194     markus    887:                debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
1.41      markus    888:        }
                    889: }
1.25      markus    890:
1.104     markus    891: /* try to decode a socks4 header */
1.127     itojun    892: static int
1.104     markus    893: channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
1.103     markus    894: {
1.181     markus    895:        char *p, *host;
1.109     markus    896:        int len, have, i, found;
1.148     deraadt   897:        char username[256];
1.103     markus    898:        struct {
                    899:                u_int8_t version;
                    900:                u_int8_t command;
                    901:                u_int16_t dest_port;
1.104     markus    902:                struct in_addr dest_addr;
1.103     markus    903:        } s4_req, s4_rsp;
                    904:
1.104     markus    905:        debug2("channel %d: decode socks4", c->self);
1.109     markus    906:
                    907:        have = buffer_len(&c->input);
                    908:        len = sizeof(s4_req);
                    909:        if (have < len)
                    910:                return 0;
                    911:        p = buffer_ptr(&c->input);
                    912:        for (found = 0, i = len; i < have; i++) {
                    913:                if (p[i] == '\0') {
                    914:                        found = 1;
                    915:                        break;
                    916:                }
                    917:                if (i > 1024) {
                    918:                        /* the peer is probably sending garbage */
                    919:                        debug("channel %d: decode socks4: too long",
                    920:                            c->self);
                    921:                        return -1;
                    922:                }
                    923:        }
                    924:        if (!found)
                    925:                return 0;
1.103     markus    926:        buffer_get(&c->input, (char *)&s4_req.version, 1);
                    927:        buffer_get(&c->input, (char *)&s4_req.command, 1);
                    928:        buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
1.104     markus    929:        buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
1.109     markus    930:        have = buffer_len(&c->input);
1.103     markus    931:        p = buffer_ptr(&c->input);
                    932:        len = strlen(p);
1.106     markus    933:        debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
1.103     markus    934:        if (len > have)
1.104     markus    935:                fatal("channel %d: decode socks4: len %d > have %d",
1.103     markus    936:                    c->self, len, have);
                    937:        strlcpy(username, p, sizeof(username));
                    938:        buffer_consume(&c->input, len);
                    939:        buffer_consume(&c->input, 1);           /* trailing '\0' */
                    940:
1.104     markus    941:        host = inet_ntoa(s4_req.dest_addr);
1.103     markus    942:        strlcpy(c->path, host, sizeof(c->path));
                    943:        c->host_port = ntohs(s4_req.dest_port);
1.148     deraadt   944:
1.194     markus    945:        debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
1.106     markus    946:            c->self, host, c->host_port, s4_req.command);
1.103     markus    947:
1.104     markus    948:        if (s4_req.command != 1) {
                    949:                debug("channel %d: cannot handle: socks4 cn %d",
                    950:                    c->self, s4_req.command);
                    951:                return -1;
1.103     markus    952:        }
1.104     markus    953:        s4_rsp.version = 0;                     /* vn: 0 for reply */
                    954:        s4_rsp.command = 90;                    /* cd: req granted */
1.103     markus    955:        s4_rsp.dest_port = 0;                   /* ignored */
1.104     markus    956:        s4_rsp.dest_addr.s_addr = INADDR_ANY;   /* ignored */
1.103     markus    957:        buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
1.104     markus    958:        return 1;
                    959: }
                    960:
1.193     markus    961: /* try to decode a socks5 header */
                    962: #define SSH_SOCKS5_AUTHDONE    0x1000
                    963: #define SSH_SOCKS5_NOAUTH      0x00
                    964: #define SSH_SOCKS5_IPV4                0x01
                    965: #define SSH_SOCKS5_DOMAIN      0x03
                    966: #define SSH_SOCKS5_IPV6                0x04
                    967: #define SSH_SOCKS5_CONNECT     0x01
                    968: #define SSH_SOCKS5_SUCCESS     0x00
                    969:
                    970: static int
                    971: channel_decode_socks5(Channel *c, fd_set * readset, fd_set * writeset)
                    972: {
                    973:        struct {
                    974:                u_int8_t version;
                    975:                u_int8_t command;
                    976:                u_int8_t reserved;
                    977:                u_int8_t atyp;
                    978:        } s5_req, s5_rsp;
                    979:        u_int16_t dest_port;
                    980:        u_char *p, dest_addr[255+1];
                    981:        int i, have, found, nmethods, addrlen, af;
                    982:
                    983:        debug2("channel %d: decode socks5", c->self);
                    984:        p = buffer_ptr(&c->input);
                    985:        if (p[0] != 0x05)
                    986:                return -1;
                    987:        have = buffer_len(&c->input);
                    988:        if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
                    989:                /* format: ver | nmethods | methods */
1.198     djm       990:                if (have < 2)
1.193     markus    991:                        return 0;
                    992:                nmethods = p[1];
                    993:                if (have < nmethods + 2)
                    994:                        return 0;
                    995:                /* look for method: "NO AUTHENTICATION REQUIRED" */
                    996:                for (found = 0, i = 2 ; i < nmethods + 2; i++) {
                    997:                        if (p[i] == SSH_SOCKS5_NOAUTH ) {
                    998:                                found = 1;
                    999:                                break;
                   1000:                        }
                   1001:                }
                   1002:                if (!found) {
                   1003:                        debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
                   1004:                            c->self);
                   1005:                        return -1;
                   1006:                }
                   1007:                buffer_consume(&c->input, nmethods + 2);
                   1008:                buffer_put_char(&c->output, 0x05);              /* version */
                   1009:                buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
                   1010:                FD_SET(c->sock, writeset);
                   1011:                c->flags |= SSH_SOCKS5_AUTHDONE;
                   1012:                debug2("channel %d: socks5 auth done", c->self);
                   1013:                return 0;                               /* need more */
                   1014:        }
                   1015:        debug2("channel %d: socks5 post auth", c->self);
                   1016:        if (have < sizeof(s5_req)+1)
                   1017:                return 0;                       /* need more */
                   1018:        memcpy((char *)&s5_req, p, sizeof(s5_req));
                   1019:        if (s5_req.version != 0x05 ||
                   1020:            s5_req.command != SSH_SOCKS5_CONNECT ||
                   1021:            s5_req.reserved != 0x00) {
1.194     markus   1022:                debug2("channel %d: only socks5 connect supported", c->self);
1.193     markus   1023:                return -1;
                   1024:        }
1.210.2.2! brad     1025:        switch (s5_req.atyp){
1.193     markus   1026:        case SSH_SOCKS5_IPV4:
                   1027:                addrlen = 4;
                   1028:                af = AF_INET;
                   1029:                break;
                   1030:        case SSH_SOCKS5_DOMAIN:
                   1031:                addrlen = p[sizeof(s5_req)];
                   1032:                af = -1;
                   1033:                break;
                   1034:        case SSH_SOCKS5_IPV6:
                   1035:                addrlen = 16;
                   1036:                af = AF_INET6;
                   1037:                break;
                   1038:        default:
1.194     markus   1039:                debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
1.193     markus   1040:                return -1;
                   1041:        }
                   1042:        if (have < 4 + addrlen + 2)
                   1043:                return 0;
                   1044:        buffer_consume(&c->input, sizeof(s5_req));
                   1045:        if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
                   1046:                buffer_consume(&c->input, 1);    /* host string length */
                   1047:        buffer_get(&c->input, (char *)&dest_addr, addrlen);
                   1048:        buffer_get(&c->input, (char *)&dest_port, 2);
                   1049:        dest_addr[addrlen] = '\0';
                   1050:        if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1.201     deraadt  1051:                strlcpy(c->path, (char *)dest_addr, sizeof(c->path));
1.193     markus   1052:        else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
                   1053:                return -1;
                   1054:        c->host_port = ntohs(dest_port);
1.198     djm      1055:
1.194     markus   1056:        debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
1.193     markus   1057:            c->self, c->path, c->host_port, s5_req.command);
                   1058:
                   1059:        s5_rsp.version = 0x05;
                   1060:        s5_rsp.command = SSH_SOCKS5_SUCCESS;
                   1061:        s5_rsp.reserved = 0;                    /* ignored */
                   1062:        s5_rsp.atyp = SSH_SOCKS5_IPV4;
                   1063:        ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
                   1064:        dest_port = 0;                          /* ignored */
                   1065:
                   1066:        buffer_append(&c->output, (char *)&s5_rsp, sizeof(s5_rsp));
                   1067:        buffer_append(&c->output, (char *)&dest_addr, sizeof(struct in_addr));
                   1068:        buffer_append(&c->output, (char *)&dest_port, sizeof(dest_port));
                   1069:        return 1;
                   1070: }
                   1071:
1.104     markus   1072: /* dynamic port forwarding */
1.127     itojun   1073: static void
1.104     markus   1074: channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
                   1075: {
                   1076:        u_char *p;
                   1077:        int have, ret;
1.103     markus   1078:
1.104     markus   1079:        have = buffer_len(&c->input);
1.137     markus   1080:        c->delayed = 0;
1.104     markus   1081:        debug2("channel %d: pre_dynamic: have %d", c->self, have);
1.105     markus   1082:        /* buffer_dump(&c->input); */
1.104     markus   1083:        /* check if the fixed size part of the packet is in buffer. */
1.193     markus   1084:        if (have < 3) {
1.104     markus   1085:                /* need more */
                   1086:                FD_SET(c->sock, readset);
                   1087:                return;
                   1088:        }
                   1089:        /* try to guess the protocol */
                   1090:        p = buffer_ptr(&c->input);
                   1091:        switch (p[0]) {
                   1092:        case 0x04:
                   1093:                ret = channel_decode_socks4(c, readset, writeset);
1.193     markus   1094:                break;
                   1095:        case 0x05:
                   1096:                ret = channel_decode_socks5(c, readset, writeset);
1.104     markus   1097:                break;
                   1098:        default:
                   1099:                ret = -1;
                   1100:                break;
                   1101:        }
                   1102:        if (ret < 0) {
1.113     markus   1103:                chan_mark_dead(c);
1.104     markus   1104:        } else if (ret == 0) {
                   1105:                debug2("channel %d: pre_dynamic: need more", c->self);
                   1106:                /* need more */
                   1107:                FD_SET(c->sock, readset);
                   1108:        } else {
                   1109:                /* switch to the next state */
                   1110:                c->type = SSH_CHANNEL_OPENING;
                   1111:                port_open_helper(c, "direct-tcpip");
                   1112:        }
1.103     markus   1113: }
                   1114:
1.41      markus   1115: /* This is our fake X11 server socket. */
1.127     itojun   1116: static void
1.41      markus   1117: channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
                   1118: {
1.113     markus   1119:        Channel *nc;
1.41      markus   1120:        struct sockaddr addr;
1.162     stevesk  1121:        int newsock;
1.41      markus   1122:        socklen_t addrlen;
1.85      markus   1123:        char buf[16384], *remote_ipaddr;
1.51      markus   1124:        int remote_port;
1.25      markus   1125:
1.41      markus   1126:        if (FD_ISSET(c->sock, readset)) {
                   1127:                debug("X11 connection requested.");
                   1128:                addrlen = sizeof(addr);
                   1129:                newsock = accept(c->sock, &addr, &addrlen);
1.149     markus   1130:                if (c->single_connection) {
1.194     markus   1131:                        debug2("single_connection: closing X11 listener.");
1.149     markus   1132:                        channel_close_fd(&c->sock);
                   1133:                        chan_mark_dead(c);
                   1134:                }
1.41      markus   1135:                if (newsock < 0) {
                   1136:                        error("accept: %.100s", strerror(errno));
                   1137:                        return;
                   1138:                }
1.162     stevesk  1139:                set_nodelay(newsock);
1.85      markus   1140:                remote_ipaddr = get_peer_ipaddr(newsock);
1.51      markus   1141:                remote_port = get_peer_port(newsock);
1.41      markus   1142:                snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
1.85      markus   1143:                    remote_ipaddr, remote_port);
1.51      markus   1144:
1.113     markus   1145:                nc = channel_new("accepted x11 socket",
1.51      markus   1146:                    SSH_CHANNEL_OPENING, newsock, newsock, -1,
1.190     markus   1147:                    c->local_window_max, c->local_maxpacket, 0, buf, 1);
1.51      markus   1148:                if (compat20) {
                   1149:                        packet_start(SSH2_MSG_CHANNEL_OPEN);
                   1150:                        packet_put_cstring("x11");
1.113     markus   1151:                        packet_put_int(nc->self);
1.149     markus   1152:                        packet_put_int(nc->local_window_max);
                   1153:                        packet_put_int(nc->local_maxpacket);
1.85      markus   1154:                        /* originator ipaddr and port */
                   1155:                        packet_put_cstring(remote_ipaddr);
1.57      markus   1156:                        if (datafellows & SSH_BUG_X11FWD) {
1.194     markus   1157:                                debug2("ssh2 x11 bug compat mode");
1.57      markus   1158:                        } else {
                   1159:                                packet_put_int(remote_port);
                   1160:                        }
1.51      markus   1161:                        packet_send();
                   1162:                } else {
                   1163:                        packet_start(SSH_SMSG_X11_OPEN);
1.113     markus   1164:                        packet_put_int(nc->self);
1.121     markus   1165:                        if (packet_get_protocol_flags() &
                   1166:                            SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
1.125     markus   1167:                                packet_put_cstring(buf);
1.51      markus   1168:                        packet_send();
                   1169:                }
1.85      markus   1170:                xfree(remote_ipaddr);
1.41      markus   1171:        }
                   1172: }
1.25      markus   1173:
1.127     itojun   1174: static void
1.103     markus   1175: port_open_helper(Channel *c, char *rtype)
                   1176: {
                   1177:        int direct;
                   1178:        char buf[1024];
                   1179:        char *remote_ipaddr = get_peer_ipaddr(c->sock);
                   1180:        u_short remote_port = get_peer_port(c->sock);
                   1181:
                   1182:        direct = (strcmp(rtype, "direct-tcpip") == 0);
                   1183:
                   1184:        snprintf(buf, sizeof buf,
                   1185:            "%s: listening port %d for %.100s port %d, "
                   1186:            "connect from %.200s port %d",
                   1187:            rtype, c->listening_port, c->path, c->host_port,
                   1188:            remote_ipaddr, remote_port);
                   1189:
                   1190:        xfree(c->remote_name);
                   1191:        c->remote_name = xstrdup(buf);
                   1192:
                   1193:        if (compat20) {
                   1194:                packet_start(SSH2_MSG_CHANNEL_OPEN);
                   1195:                packet_put_cstring(rtype);
                   1196:                packet_put_int(c->self);
                   1197:                packet_put_int(c->local_window_max);
                   1198:                packet_put_int(c->local_maxpacket);
                   1199:                if (direct) {
                   1200:                        /* target host, port */
                   1201:                        packet_put_cstring(c->path);
                   1202:                        packet_put_int(c->host_port);
                   1203:                } else {
                   1204:                        /* listen address, port */
                   1205:                        packet_put_cstring(c->path);
                   1206:                        packet_put_int(c->listening_port);
                   1207:                }
                   1208:                /* originator host and port */
                   1209:                packet_put_cstring(remote_ipaddr);
                   1210:                packet_put_int(remote_port);
                   1211:                packet_send();
                   1212:        } else {
                   1213:                packet_start(SSH_MSG_PORT_OPEN);
                   1214:                packet_put_int(c->self);
                   1215:                packet_put_cstring(c->path);
                   1216:                packet_put_int(c->host_port);
1.121     markus   1217:                if (packet_get_protocol_flags() &
                   1218:                    SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
1.103     markus   1219:                        packet_put_cstring(c->remote_name);
                   1220:                packet_send();
                   1221:        }
                   1222:        xfree(remote_ipaddr);
                   1223: }
                   1224:
1.41      markus   1225: /*
                   1226:  * This socket is listening for connections to a forwarded TCP/IP port.
                   1227:  */
1.127     itojun   1228: static void
1.41      markus   1229: channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
                   1230: {
1.103     markus   1231:        Channel *nc;
1.41      markus   1232:        struct sockaddr addr;
1.113     markus   1233:        int newsock, nextstate;
1.41      markus   1234:        socklen_t addrlen;
1.103     markus   1235:        char *rtype;
1.73      markus   1236:
1.41      markus   1237:        if (FD_ISSET(c->sock, readset)) {
                   1238:                debug("Connection to port %d forwarding "
                   1239:                    "to %.100s port %d requested.",
                   1240:                    c->listening_port, c->path, c->host_port);
1.103     markus   1241:
1.137     markus   1242:                if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
                   1243:                        nextstate = SSH_CHANNEL_OPENING;
                   1244:                        rtype = "forwarded-tcpip";
                   1245:                } else {
                   1246:                        if (c->host_port == 0) {
                   1247:                                nextstate = SSH_CHANNEL_DYNAMIC;
1.138     markus   1248:                                rtype = "dynamic-tcpip";
1.137     markus   1249:                        } else {
                   1250:                                nextstate = SSH_CHANNEL_OPENING;
                   1251:                                rtype = "direct-tcpip";
                   1252:                        }
                   1253:                }
1.103     markus   1254:
1.41      markus   1255:                addrlen = sizeof(addr);
                   1256:                newsock = accept(c->sock, &addr, &addrlen);
                   1257:                if (newsock < 0) {
                   1258:                        error("accept: %.100s", strerror(errno));
                   1259:                        return;
                   1260:                }
1.169     stevesk  1261:                set_nodelay(newsock);
1.190     markus   1262:                nc = channel_new(rtype, nextstate, newsock, newsock, -1,
                   1263:                    c->local_window_max, c->local_maxpacket, 0, rtype, 1);
1.103     markus   1264:                nc->listening_port = c->listening_port;
                   1265:                nc->host_port = c->host_port;
                   1266:                strlcpy(nc->path, c->path, sizeof(nc->path));
                   1267:
1.137     markus   1268:                if (nextstate == SSH_CHANNEL_DYNAMIC) {
                   1269:                        /*
                   1270:                         * do not call the channel_post handler until
                   1271:                         * this flag has been reset by a pre-handler.
                   1272:                         * otherwise the FD_ISSET calls might overflow
                   1273:                         */
                   1274:                        nc->delayed = 1;
                   1275:                } else {
1.103     markus   1276:                        port_open_helper(nc, rtype);
1.137     markus   1277:                }
1.41      markus   1278:        }
                   1279: }
1.25      markus   1280:
1.41      markus   1281: /*
                   1282:  * This is the authentication agent socket listening for connections from
                   1283:  * clients.
                   1284:  */
1.127     itojun   1285: static void
1.41      markus   1286: channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
                   1287: {
1.113     markus   1288:        Channel *nc;
                   1289:        int newsock;
1.41      markus   1290:        struct sockaddr addr;
                   1291:        socklen_t addrlen;
1.25      markus   1292:
1.41      markus   1293:        if (FD_ISSET(c->sock, readset)) {
                   1294:                addrlen = sizeof(addr);
                   1295:                newsock = accept(c->sock, &addr, &addrlen);
                   1296:                if (newsock < 0) {
                   1297:                        error("accept from auth socket: %.100s", strerror(errno));
                   1298:                        return;
                   1299:                }
1.113     markus   1300:                nc = channel_new("accepted auth socket",
1.73      markus   1301:                    SSH_CHANNEL_OPENING, newsock, newsock, -1,
                   1302:                    c->local_window_max, c->local_maxpacket,
1.190     markus   1303:                    0, "accepted auth socket", 1);
1.73      markus   1304:                if (compat20) {
                   1305:                        packet_start(SSH2_MSG_CHANNEL_OPEN);
                   1306:                        packet_put_cstring("auth-agent@openssh.com");
1.113     markus   1307:                        packet_put_int(nc->self);
1.73      markus   1308:                        packet_put_int(c->local_window_max);
                   1309:                        packet_put_int(c->local_maxpacket);
                   1310:                } else {
                   1311:                        packet_start(SSH_SMSG_AGENT_OPEN);
1.113     markus   1312:                        packet_put_int(nc->self);
1.73      markus   1313:                }
1.41      markus   1314:                packet_send();
                   1315:        }
                   1316: }
1.25      markus   1317:
1.127     itojun   1318: static void
1.75      markus   1319: channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
                   1320: {
1.114     markus   1321:        int err = 0;
1.129     stevesk  1322:        socklen_t sz = sizeof(err);
1.114     markus   1323:
1.75      markus   1324:        if (FD_ISSET(c->sock, writeset)) {
1.170     stevesk  1325:                if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
1.114     markus   1326:                        err = errno;
                   1327:                        error("getsockopt SO_ERROR failed");
                   1328:                }
                   1329:                if (err == 0) {
                   1330:                        debug("channel %d: connected", c->self);
                   1331:                        c->type = SSH_CHANNEL_OPEN;
                   1332:                        if (compat20) {
                   1333:                                packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
                   1334:                                packet_put_int(c->remote_id);
                   1335:                                packet_put_int(c->self);
                   1336:                                packet_put_int(c->local_window);
                   1337:                                packet_put_int(c->local_maxpacket);
                   1338:                        } else {
                   1339:                                packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
                   1340:                                packet_put_int(c->remote_id);
                   1341:                                packet_put_int(c->self);
                   1342:                        }
1.75      markus   1343:                } else {
1.114     markus   1344:                        debug("channel %d: not connected: %s",
                   1345:                            c->self, strerror(err));
                   1346:                        if (compat20) {
                   1347:                                packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
                   1348:                                packet_put_int(c->remote_id);
                   1349:                                packet_put_int(SSH2_OPEN_CONNECT_FAILED);
                   1350:                                if (!(datafellows & SSH_BUG_OPENFAILURE)) {
                   1351:                                        packet_put_cstring(strerror(err));
                   1352:                                        packet_put_cstring("");
                   1353:                                }
1.75      markus   1354:                        } else {
1.114     markus   1355:                                packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
                   1356:                                packet_put_int(c->remote_id);
1.75      markus   1357:                        }
1.114     markus   1358:                        chan_mark_dead(c);
1.75      markus   1359:                }
1.114     markus   1360:                packet_send();
1.75      markus   1361:        }
                   1362: }
                   1363:
1.127     itojun   1364: static int
1.41      markus   1365: channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
                   1366: {
1.210.2.2! brad     1367:        char buf[CHAN_RBUF];
1.41      markus   1368:        int len;
1.25      markus   1369:
1.118     markus   1370:        if (c->rfd != -1 &&
1.41      markus   1371:            FD_ISSET(c->rfd, readset)) {
                   1372:                len = read(c->rfd, buf, sizeof(buf));
1.53      markus   1373:                if (len < 0 && (errno == EINTR || errno == EAGAIN))
                   1374:                        return 1;
1.41      markus   1375:                if (len <= 0) {
1.194     markus   1376:                        debug2("channel %d: read<=0 rfd %d len %d",
1.41      markus   1377:                            c->self, c->rfd, len);
1.104     markus   1378:                        if (c->type != SSH_CHANNEL_OPEN) {
1.194     markus   1379:                                debug2("channel %d: not open", c->self);
1.113     markus   1380:                                chan_mark_dead(c);
1.103     markus   1381:                                return -1;
1.104     markus   1382:                        } else if (compat13) {
1.158     markus   1383:                                buffer_clear(&c->output);
1.41      markus   1384:                                c->type = SSH_CHANNEL_INPUT_DRAINING;
1.194     markus   1385:                                debug2("channel %d: input draining.", c->self);
1.25      markus   1386:                        } else {
1.41      markus   1387:                                chan_read_failed(c);
1.25      markus   1388:                        }
1.41      markus   1389:                        return -1;
                   1390:                }
1.143     deraadt  1391:                if (c->input_filter != NULL) {
1.66      markus   1392:                        if (c->input_filter(c, buf, len) == -1) {
1.194     markus   1393:                                debug2("channel %d: filter stops", c->self);
1.65      markus   1394:                                chan_read_failed(c);
                   1395:                        }
                   1396:                } else {
                   1397:                        buffer_append(&c->input, buf, len);
                   1398:                }
1.41      markus   1399:        }
                   1400:        return 1;
                   1401: }
1.127     itojun   1402: static int
1.41      markus   1403: channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
                   1404: {
1.95      markus   1405:        struct termios tio;
1.134     markus   1406:        u_char *data;
                   1407:        u_int dlen;
1.41      markus   1408:        int len;
1.25      markus   1409:
1.41      markus   1410:        /* Send buffered output data to the socket. */
1.118     markus   1411:        if (c->wfd != -1 &&
1.41      markus   1412:            FD_ISSET(c->wfd, writeset) &&
                   1413:            buffer_len(&c->output) > 0) {
1.134     markus   1414:                data = buffer_ptr(&c->output);
                   1415:                dlen = buffer_len(&c->output);
                   1416:                len = write(c->wfd, data, dlen);
1.53      markus   1417:                if (len < 0 && (errno == EINTR || errno == EAGAIN))
                   1418:                        return 1;
1.41      markus   1419:                if (len <= 0) {
1.104     markus   1420:                        if (c->type != SSH_CHANNEL_OPEN) {
1.194     markus   1421:                                debug2("channel %d: not open", c->self);
1.113     markus   1422:                                chan_mark_dead(c);
1.104     markus   1423:                                return -1;
                   1424:                        } else if (compat13) {
1.158     markus   1425:                                buffer_clear(&c->output);
1.194     markus   1426:                                debug2("channel %d: input draining.", c->self);
1.41      markus   1427:                                c->type = SSH_CHANNEL_INPUT_DRAINING;
                   1428:                        } else {
                   1429:                                chan_write_failed(c);
                   1430:                        }
                   1431:                        return -1;
1.91      markus   1432:                }
1.134     markus   1433:                if (compat20 && c->isatty && dlen >= 1 && data[0] != '\r') {
1.91      markus   1434:                        if (tcgetattr(c->wfd, &tio) == 0 &&
                   1435:                            !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
                   1436:                                /*
                   1437:                                 * Simulate echo to reduce the impact of
1.96      markus   1438:                                 * traffic analysis. We need to match the
1.95      markus   1439:                                 * size of a SSH2_MSG_CHANNEL_DATA message
                   1440:                                 * (4 byte channel id + data)
1.91      markus   1441:                                 */
1.95      markus   1442:                                packet_send_ignore(4 + len);
1.91      markus   1443:                                packet_send();
                   1444:                        }
1.25      markus   1445:                }
1.41      markus   1446:                buffer_consume(&c->output, len);
1.44      markus   1447:                if (compat20 && len > 0) {
                   1448:                        c->local_consumed += len;
                   1449:                }
                   1450:        }
                   1451:        return 1;
                   1452: }
1.127     itojun   1453: static int
1.44      markus   1454: channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
                   1455: {
1.210.2.2! brad     1456:        char buf[CHAN_RBUF];
1.44      markus   1457:        int len;
                   1458:
1.45      markus   1459: /** XXX handle drain efd, too */
1.44      markus   1460:        if (c->efd != -1) {
                   1461:                if (c->extended_usage == CHAN_EXTENDED_WRITE &&
                   1462:                    FD_ISSET(c->efd, writeset) &&
                   1463:                    buffer_len(&c->extended) > 0) {
                   1464:                        len = write(c->efd, buffer_ptr(&c->extended),
                   1465:                            buffer_len(&c->extended));
1.70      markus   1466:                        debug2("channel %d: written %d to efd %d",
1.44      markus   1467:                            c->self, len, c->efd);
1.93      markus   1468:                        if (len < 0 && (errno == EINTR || errno == EAGAIN))
                   1469:                                return 1;
                   1470:                        if (len <= 0) {
                   1471:                                debug2("channel %d: closing write-efd %d",
                   1472:                                    c->self, c->efd);
1.132     markus   1473:                                channel_close_fd(&c->efd);
1.93      markus   1474:                        } else {
1.44      markus   1475:                                buffer_consume(&c->extended, len);
                   1476:                                c->local_consumed += len;
                   1477:                        }
                   1478:                } else if (c->extended_usage == CHAN_EXTENDED_READ &&
                   1479:                    FD_ISSET(c->efd, readset)) {
                   1480:                        len = read(c->efd, buf, sizeof(buf));
1.70      markus   1481:                        debug2("channel %d: read %d from efd %d",
1.148     deraadt  1482:                            c->self, len, c->efd);
1.93      markus   1483:                        if (len < 0 && (errno == EINTR || errno == EAGAIN))
                   1484:                                return 1;
                   1485:                        if (len <= 0) {
                   1486:                                debug2("channel %d: closing read-efd %d",
1.45      markus   1487:                                    c->self, c->efd);
1.132     markus   1488:                                channel_close_fd(&c->efd);
1.93      markus   1489:                        } else {
1.44      markus   1490:                                buffer_append(&c->extended, buf, len);
1.93      markus   1491:                        }
1.44      markus   1492:                }
                   1493:        }
                   1494:        return 1;
                   1495: }
1.127     itojun   1496: static int
1.204     djm      1497: channel_handle_ctl(Channel *c, fd_set * readset, fd_set * writeset)
                   1498: {
                   1499:        char buf[16];
                   1500:        int len;
                   1501:
                   1502:        /* Monitor control fd to detect if the slave client exits */
                   1503:        if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
                   1504:                len = read(c->ctl_fd, buf, sizeof(buf));
                   1505:                if (len < 0 && (errno == EINTR || errno == EAGAIN))
                   1506:                        return 1;
                   1507:                if (len <= 0) {
                   1508:                        debug2("channel %d: ctl read<=0", c->self);
                   1509:                        if (c->type != SSH_CHANNEL_OPEN) {
                   1510:                                debug2("channel %d: not open", c->self);
                   1511:                                chan_mark_dead(c);
                   1512:                                return -1;
                   1513:                        } else {
                   1514:                                chan_read_failed(c);
                   1515:                                chan_write_failed(c);
                   1516:                        }
                   1517:                        return -1;
                   1518:                } else
                   1519:                        fatal("%s: unexpected data on ctl fd", __func__);
                   1520:        }
                   1521:        return 1;
                   1522: }
                   1523: static int
1.93      markus   1524: channel_check_window(Channel *c)
1.44      markus   1525: {
1.104     markus   1526:        if (c->type == SSH_CHANNEL_OPEN &&
                   1527:            !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
1.44      markus   1528:            c->local_window < c->local_window_max/2 &&
                   1529:            c->local_consumed > 0) {
                   1530:                packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
                   1531:                packet_put_int(c->remote_id);
                   1532:                packet_put_int(c->local_consumed);
                   1533:                packet_send();
1.70      markus   1534:                debug2("channel %d: window %d sent adjust %d",
1.44      markus   1535:                    c->self, c->local_window,
                   1536:                    c->local_consumed);
                   1537:                c->local_window += c->local_consumed;
                   1538:                c->local_consumed = 0;
1.1       deraadt  1539:        }
1.41      markus   1540:        return 1;
1.1       deraadt  1541: }
                   1542:
1.127     itojun   1543: static void
1.157     markus   1544: channel_post_open(Channel *c, fd_set * readset, fd_set * writeset)
1.41      markus   1545: {
1.137     markus   1546:        if (c->delayed)
                   1547:                return;
1.41      markus   1548:        channel_handle_rfd(c, readset, writeset);
                   1549:        channel_handle_wfd(c, readset, writeset);
1.157     markus   1550:        if (!compat20)
1.137     markus   1551:                return;
1.44      markus   1552:        channel_handle_efd(c, readset, writeset);
1.204     djm      1553:        channel_handle_ctl(c, readset, writeset);
1.93      markus   1554:        channel_check_window(c);
1.44      markus   1555: }
                   1556:
1.127     itojun   1557: static void
1.41      markus   1558: channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1.1       deraadt  1559: {
1.41      markus   1560:        int len;
1.180     deraadt  1561:
1.41      markus   1562:        /* Send buffered output data to the socket. */
                   1563:        if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
                   1564:                len = write(c->sock, buffer_ptr(&c->output),
                   1565:                            buffer_len(&c->output));
                   1566:                if (len <= 0)
1.158     markus   1567:                        buffer_clear(&c->output);
1.41      markus   1568:                else
                   1569:                        buffer_consume(&c->output, len);
                   1570:        }
                   1571: }
1.25      markus   1572:
1.127     itojun   1573: static void
1.44      markus   1574: channel_handler_init_20(void)
                   1575: {
1.157     markus   1576:        channel_pre[SSH_CHANNEL_OPEN] =                 &channel_pre_open;
1.51      markus   1577:        channel_pre[SSH_CHANNEL_X11_OPEN] =             &channel_pre_x11_open;
1.44      markus   1578:        channel_pre[SSH_CHANNEL_PORT_LISTENER] =        &channel_pre_listener;
1.73      markus   1579:        channel_pre[SSH_CHANNEL_RPORT_LISTENER] =       &channel_pre_listener;
1.51      markus   1580:        channel_pre[SSH_CHANNEL_X11_LISTENER] =         &channel_pre_listener;
1.73      markus   1581:        channel_pre[SSH_CHANNEL_AUTH_SOCKET] =          &channel_pre_listener;
1.75      markus   1582:        channel_pre[SSH_CHANNEL_CONNECTING] =           &channel_pre_connecting;
1.103     markus   1583:        channel_pre[SSH_CHANNEL_DYNAMIC] =              &channel_pre_dynamic;
1.44      markus   1584:
1.157     markus   1585:        channel_post[SSH_CHANNEL_OPEN] =                &channel_post_open;
1.44      markus   1586:        channel_post[SSH_CHANNEL_PORT_LISTENER] =       &channel_post_port_listener;
1.73      markus   1587:        channel_post[SSH_CHANNEL_RPORT_LISTENER] =      &channel_post_port_listener;
1.51      markus   1588:        channel_post[SSH_CHANNEL_X11_LISTENER] =        &channel_post_x11_listener;
1.73      markus   1589:        channel_post[SSH_CHANNEL_AUTH_SOCKET] =         &channel_post_auth_listener;
1.75      markus   1590:        channel_post[SSH_CHANNEL_CONNECTING] =          &channel_post_connecting;
1.157     markus   1591:        channel_post[SSH_CHANNEL_DYNAMIC] =             &channel_post_open;
1.44      markus   1592: }
                   1593:
1.127     itojun   1594: static void
1.41      markus   1595: channel_handler_init_13(void)
                   1596: {
                   1597:        channel_pre[SSH_CHANNEL_OPEN] =                 &channel_pre_open_13;
                   1598:        channel_pre[SSH_CHANNEL_X11_OPEN] =             &channel_pre_x11_open_13;
                   1599:        channel_pre[SSH_CHANNEL_X11_LISTENER] =         &channel_pre_listener;
                   1600:        channel_pre[SSH_CHANNEL_PORT_LISTENER] =        &channel_pre_listener;
                   1601:        channel_pre[SSH_CHANNEL_AUTH_SOCKET] =          &channel_pre_listener;
                   1602:        channel_pre[SSH_CHANNEL_INPUT_DRAINING] =       &channel_pre_input_draining;
                   1603:        channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] =      &channel_pre_output_draining;
1.75      markus   1604:        channel_pre[SSH_CHANNEL_CONNECTING] =           &channel_pre_connecting;
1.103     markus   1605:        channel_pre[SSH_CHANNEL_DYNAMIC] =              &channel_pre_dynamic;
1.25      markus   1606:
1.157     markus   1607:        channel_post[SSH_CHANNEL_OPEN] =                &channel_post_open;
1.41      markus   1608:        channel_post[SSH_CHANNEL_X11_LISTENER] =        &channel_post_x11_listener;
                   1609:        channel_post[SSH_CHANNEL_PORT_LISTENER] =       &channel_post_port_listener;
                   1610:        channel_post[SSH_CHANNEL_AUTH_SOCKET] =         &channel_post_auth_listener;
                   1611:        channel_post[SSH_CHANNEL_OUTPUT_DRAINING] =     &channel_post_output_drain_13;
1.75      markus   1612:        channel_post[SSH_CHANNEL_CONNECTING] =          &channel_post_connecting;
1.157     markus   1613:        channel_post[SSH_CHANNEL_DYNAMIC] =             &channel_post_open;
1.41      markus   1614: }
1.25      markus   1615:
1.127     itojun   1616: static void
1.41      markus   1617: channel_handler_init_15(void)
                   1618: {
1.157     markus   1619:        channel_pre[SSH_CHANNEL_OPEN] =                 &channel_pre_open;
1.51      markus   1620:        channel_pre[SSH_CHANNEL_X11_OPEN] =             &channel_pre_x11_open;
1.41      markus   1621:        channel_pre[SSH_CHANNEL_X11_LISTENER] =         &channel_pre_listener;
                   1622:        channel_pre[SSH_CHANNEL_PORT_LISTENER] =        &channel_pre_listener;
                   1623:        channel_pre[SSH_CHANNEL_AUTH_SOCKET] =          &channel_pre_listener;
1.75      markus   1624:        channel_pre[SSH_CHANNEL_CONNECTING] =           &channel_pre_connecting;
1.103     markus   1625:        channel_pre[SSH_CHANNEL_DYNAMIC] =              &channel_pre_dynamic;
1.25      markus   1626:
1.41      markus   1627:        channel_post[SSH_CHANNEL_X11_LISTENER] =        &channel_post_x11_listener;
                   1628:        channel_post[SSH_CHANNEL_PORT_LISTENER] =       &channel_post_port_listener;
                   1629:        channel_post[SSH_CHANNEL_AUTH_SOCKET] =         &channel_post_auth_listener;
1.157     markus   1630:        channel_post[SSH_CHANNEL_OPEN] =                &channel_post_open;
1.75      markus   1631:        channel_post[SSH_CHANNEL_CONNECTING] =          &channel_post_connecting;
1.157     markus   1632:        channel_post[SSH_CHANNEL_DYNAMIC] =             &channel_post_open;
1.41      markus   1633: }
1.27      markus   1634:
1.127     itojun   1635: static void
1.41      markus   1636: channel_handler_init(void)
                   1637: {
                   1638:        int i;
1.180     deraadt  1639:
1.148     deraadt  1640:        for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1.41      markus   1641:                channel_pre[i] = NULL;
                   1642:                channel_post[i] = NULL;
                   1643:        }
1.44      markus   1644:        if (compat20)
                   1645:                channel_handler_init_20();
                   1646:        else if (compat13)
1.41      markus   1647:                channel_handler_init_13();
                   1648:        else
                   1649:                channel_handler_init_15();
                   1650: }
1.25      markus   1651:
1.140     markus   1652: /* gc dead channels */
                   1653: static void
                   1654: channel_garbage_collect(Channel *c)
                   1655: {
                   1656:        if (c == NULL)
                   1657:                return;
                   1658:        if (c->detach_user != NULL) {
                   1659:                if (!chan_is_dead(c, 0))
                   1660:                        return;
1.194     markus   1661:                debug2("channel %d: gc: notify user", c->self);
1.140     markus   1662:                c->detach_user(c->self, NULL);
                   1663:                /* if we still have a callback */
                   1664:                if (c->detach_user != NULL)
                   1665:                        return;
1.194     markus   1666:                debug2("channel %d: gc: user detached", c->self);
1.140     markus   1667:        }
                   1668:        if (!chan_is_dead(c, 1))
                   1669:                return;
1.194     markus   1670:        debug2("channel %d: garbage collecting", c->self);
1.140     markus   1671:        channel_free(c);
                   1672: }
                   1673:
1.127     itojun   1674: static void
1.41      markus   1675: channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
                   1676: {
                   1677:        static int did_init = 0;
1.209     avsm     1678:        u_int i;
1.41      markus   1679:        Channel *c;
1.25      markus   1680:
1.41      markus   1681:        if (!did_init) {
                   1682:                channel_handler_init();
                   1683:                did_init = 1;
                   1684:        }
                   1685:        for (i = 0; i < channels_alloc; i++) {
1.113     markus   1686:                c = channels[i];
                   1687:                if (c == NULL)
1.41      markus   1688:                        continue;
1.118     markus   1689:                if (ftab[c->type] != NULL)
                   1690:                        (*ftab[c->type])(c, readset, writeset);
1.140     markus   1691:                channel_garbage_collect(c);
1.1       deraadt  1692:        }
                   1693: }
                   1694:
1.121     markus   1695: /*
                   1696:  * Allocate/update select bitmasks and add any bits relevant to channels in
                   1697:  * select bitmasks.
                   1698:  */
1.49      markus   1699: void
1.100     markus   1700: channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1.209     avsm     1701:     u_int *nallocp, int rekeying)
1.41      markus   1702: {
1.209     avsm     1703:        u_int n, sz;
1.84      markus   1704:
                   1705:        n = MAX(*maxfdp, channel_max_fd);
                   1706:
                   1707:        sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1.132     markus   1708:        /* perhaps check sz < nalloc/2 and shrink? */
                   1709:        if (*readsetp == NULL || sz > *nallocp) {
                   1710:                *readsetp = xrealloc(*readsetp, sz);
                   1711:                *writesetp = xrealloc(*writesetp, sz);
                   1712:                *nallocp = sz;
1.84      markus   1713:        }
1.132     markus   1714:        *maxfdp = n;
1.84      markus   1715:        memset(*readsetp, 0, sz);
                   1716:        memset(*writesetp, 0, sz);
                   1717:
1.100     markus   1718:        if (!rekeying)
                   1719:                channel_handler(channel_pre, *readsetp, *writesetp);
1.41      markus   1720: }
                   1721:
1.121     markus   1722: /*
                   1723:  * After select, perform any appropriate operations for channels which have
                   1724:  * events pending.
                   1725:  */
1.49      markus   1726: void
1.41      markus   1727: channel_after_select(fd_set * readset, fd_set * writeset)
                   1728: {
                   1729:        channel_handler(channel_post, readset, writeset);
                   1730: }
                   1731:
1.121     markus   1732:
1.84      markus   1733: /* If there is data to send to the connection, enqueue some of it now. */
1.1       deraadt  1734:
1.49      markus   1735: void
1.142     itojun   1736: channel_output_poll(void)
1.1       deraadt  1737: {
1.41      markus   1738:        Channel *c;
1.209     avsm     1739:        u_int i, len;
1.1       deraadt  1740:
1.25      markus   1741:        for (i = 0; i < channels_alloc; i++) {
1.113     markus   1742:                c = channels[i];
                   1743:                if (c == NULL)
                   1744:                        continue;
1.37      markus   1745:
1.121     markus   1746:                /*
                   1747:                 * We are only interested in channels that can have buffered
                   1748:                 * incoming data.
                   1749:                 */
1.37      markus   1750:                if (compat13) {
1.41      markus   1751:                        if (c->type != SSH_CHANNEL_OPEN &&
                   1752:                            c->type != SSH_CHANNEL_INPUT_DRAINING)
1.37      markus   1753:                                continue;
                   1754:                } else {
1.41      markus   1755:                        if (c->type != SSH_CHANNEL_OPEN)
1.37      markus   1756:                                continue;
                   1757:                }
1.46      markus   1758:                if (compat20 &&
                   1759:                    (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
1.93      markus   1760:                        /* XXX is this true? */
1.140     markus   1761:                        debug3("channel %d: will not send data after close", c->self);
1.44      markus   1762:                        continue;
                   1763:                }
1.25      markus   1764:
                   1765:                /* Get the amount of buffered data for this channel. */
1.93      markus   1766:                if ((c->istate == CHAN_INPUT_OPEN ||
                   1767:                    c->istate == CHAN_INPUT_WAIT_DRAIN) &&
                   1768:                    (len = buffer_len(&c->input)) > 0) {
1.121     markus   1769:                        /*
                   1770:                         * Send some data for the other side over the secure
                   1771:                         * connection.
                   1772:                         */
1.44      markus   1773:                        if (compat20) {
                   1774:                                if (len > c->remote_window)
                   1775:                                        len = c->remote_window;
                   1776:                                if (len > c->remote_maxpacket)
                   1777:                                        len = c->remote_maxpacket;
1.25      markus   1778:                        } else {
1.44      markus   1779:                                if (packet_is_interactive()) {
                   1780:                                        if (len > 1024)
                   1781:                                                len = 512;
                   1782:                                } else {
                   1783:                                        /* Keep the packets at reasonable size. */
                   1784:                                        if (len > packet_get_maxsize()/2)
                   1785:                                                len = packet_get_maxsize()/2;
                   1786:                                }
1.25      markus   1787:                        }
1.41      markus   1788:                        if (len > 0) {
1.44      markus   1789:                                packet_start(compat20 ?
                   1790:                                    SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
1.41      markus   1791:                                packet_put_int(c->remote_id);
                   1792:                                packet_put_string(buffer_ptr(&c->input), len);
                   1793:                                packet_send();
                   1794:                                buffer_consume(&c->input, len);
                   1795:                                c->remote_window -= len;
                   1796:                        }
                   1797:                } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1.25      markus   1798:                        if (compat13)
                   1799:                                fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
1.27      markus   1800:                        /*
                   1801:                         * input-buffer is empty and read-socket shutdown:
1.172     markus   1802:                         * tell peer, that we will not send more data: send IEOF.
                   1803:                         * hack for extended data: delay EOF if EFD still in use.
1.27      markus   1804:                         */
1.172     markus   1805:                        if (CHANNEL_EFD_INPUT_ACTIVE(c))
1.174     markus   1806:                               debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
                   1807:                                   c->self, c->efd, buffer_len(&c->extended));
1.172     markus   1808:                        else
                   1809:                                chan_ibuf_empty(c);
1.25      markus   1810:                }
1.44      markus   1811:                /* Send extended data, i.e. stderr */
                   1812:                if (compat20 &&
1.172     markus   1813:                    !(c->flags & CHAN_EOF_SENT) &&
1.44      markus   1814:                    c->remote_window > 0 &&
                   1815:                    (len = buffer_len(&c->extended)) > 0 &&
                   1816:                    c->extended_usage == CHAN_EXTENDED_READ) {
1.178     markus   1817:                        debug2("channel %d: rwin %u elen %u euse %d",
1.93      markus   1818:                            c->self, c->remote_window, buffer_len(&c->extended),
                   1819:                            c->extended_usage);
1.44      markus   1820:                        if (len > c->remote_window)
                   1821:                                len = c->remote_window;
                   1822:                        if (len > c->remote_maxpacket)
                   1823:                                len = c->remote_maxpacket;
                   1824:                        packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
                   1825:                        packet_put_int(c->remote_id);
                   1826:                        packet_put_int(SSH2_EXTENDED_DATA_STDERR);
                   1827:                        packet_put_string(buffer_ptr(&c->extended), len);
                   1828:                        packet_send();
                   1829:                        buffer_consume(&c->extended, len);
                   1830:                        c->remote_window -= len;
1.93      markus   1831:                        debug2("channel %d: sent ext data %d", c->self, len);
1.44      markus   1832:                }
1.25      markus   1833:        }
1.1       deraadt  1834: }
                   1835:
1.121     markus   1836:
                   1837: /* -- protocol input */
1.1       deraadt  1838:
1.49      markus   1839: void
1.154     markus   1840: channel_input_data(int type, u_int32_t seq, void *ctxt)
1.1       deraadt  1841: {
1.37      markus   1842:        int id;
1.25      markus   1843:        char *data;
1.77      markus   1844:        u_int data_len;
1.41      markus   1845:        Channel *c;
1.25      markus   1846:
                   1847:        /* Get the channel number and verify it. */
1.37      markus   1848:        id = packet_get_int();
1.41      markus   1849:        c = channel_lookup(id);
                   1850:        if (c == NULL)
1.37      markus   1851:                packet_disconnect("Received data for nonexistent channel %d.", id);
1.25      markus   1852:
                   1853:        /* Ignore any data for non-open channels (might happen on close) */
1.41      markus   1854:        if (c->type != SSH_CHANNEL_OPEN &&
                   1855:            c->type != SSH_CHANNEL_X11_OPEN)
1.37      markus   1856:                return;
                   1857:
1.25      markus   1858:        /* Get the data. */
                   1859:        data = packet_get_string(&data_len);
1.200     markus   1860:
                   1861:        /*
                   1862:         * Ignore data for protocol > 1.3 if output end is no longer open.
                   1863:         * For protocol 2 the sending side is reducing its window as it sends
                   1864:         * data, so we must 'fake' consumption of the data in order to ensure
                   1865:         * that window updates are sent back.  Otherwise the connection might
                   1866:         * deadlock.
                   1867:         */
                   1868:        if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
                   1869:                if (compat20) {
                   1870:                        c->local_window -= data_len;
                   1871:                        c->local_consumed += data_len;
                   1872:                }
                   1873:                xfree(data);
                   1874:                return;
                   1875:        }
1.41      markus   1876:
1.143     deraadt  1877:        if (compat20) {
1.44      markus   1878:                if (data_len > c->local_maxpacket) {
1.188     itojun   1879:                        logit("channel %d: rcvd big packet %d, maxpack %d",
1.44      markus   1880:                            c->self, data_len, c->local_maxpacket);
                   1881:                }
                   1882:                if (data_len > c->local_window) {
1.188     itojun   1883:                        logit("channel %d: rcvd too much data %d, win %d",
1.44      markus   1884:                            c->self, data_len, c->local_window);
                   1885:                        xfree(data);
                   1886:                        return;
                   1887:                }
                   1888:                c->local_window -= data_len;
                   1889:        }
1.152     markus   1890:        packet_check_eom();
1.41      markus   1891:        buffer_append(&c->output, data, data_len);
1.25      markus   1892:        xfree(data);
1.1       deraadt  1893: }
1.121     markus   1894:
1.49      markus   1895: void
1.154     markus   1896: channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
1.44      markus   1897: {
                   1898:        int id;
                   1899:        char *data;
1.177     markus   1900:        u_int data_len, tcode;
1.44      markus   1901:        Channel *c;
                   1902:
                   1903:        /* Get the channel number and verify it. */
                   1904:        id = packet_get_int();
                   1905:        c = channel_lookup(id);
                   1906:
                   1907:        if (c == NULL)
                   1908:                packet_disconnect("Received extended_data for bad channel %d.", id);
                   1909:        if (c->type != SSH_CHANNEL_OPEN) {
1.188     itojun   1910:                logit("channel %d: ext data for non open", id);
1.44      markus   1911:                return;
1.172     markus   1912:        }
                   1913:        if (c->flags & CHAN_EOF_RCVD) {
                   1914:                if (datafellows & SSH_BUG_EXTEOF)
                   1915:                        debug("channel %d: accepting ext data after eof", id);
                   1916:                else
                   1917:                        packet_disconnect("Received extended_data after EOF "
                   1918:                            "on channel %d.", id);
1.44      markus   1919:        }
                   1920:        tcode = packet_get_int();
                   1921:        if (c->efd == -1 ||
                   1922:            c->extended_usage != CHAN_EXTENDED_WRITE ||
                   1923:            tcode != SSH2_EXTENDED_DATA_STDERR) {
1.188     itojun   1924:                logit("channel %d: bad ext data", c->self);
1.44      markus   1925:                return;
                   1926:        }
                   1927:        data = packet_get_string(&data_len);
1.152     markus   1928:        packet_check_eom();
1.44      markus   1929:        if (data_len > c->local_window) {
1.188     itojun   1930:                logit("channel %d: rcvd too much extended_data %d, win %d",
1.44      markus   1931:                    c->self, data_len, c->local_window);
                   1932:                xfree(data);
                   1933:                return;
                   1934:        }
1.70      markus   1935:        debug2("channel %d: rcvd ext data %d", c->self, data_len);
1.44      markus   1936:        c->local_window -= data_len;
                   1937:        buffer_append(&c->extended, data, data_len);
                   1938:        xfree(data);
                   1939: }
                   1940:
1.49      markus   1941: void
1.154     markus   1942: channel_input_ieof(int type, u_int32_t seq, void *ctxt)
1.41      markus   1943: {
                   1944:        int id;
                   1945:        Channel *c;
                   1946:
                   1947:        id = packet_get_int();
1.152     markus   1948:        packet_check_eom();
1.41      markus   1949:        c = channel_lookup(id);
                   1950:        if (c == NULL)
                   1951:                packet_disconnect("Received ieof for nonexistent channel %d.", id);
                   1952:        chan_rcvd_ieof(c);
1.133     markus   1953:
                   1954:        /* XXX force input close */
1.156     markus   1955:        if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
1.133     markus   1956:                debug("channel %d: FORCE input drain", c->self);
                   1957:                c->istate = CHAN_INPUT_WAIT_DRAIN;
1.161     markus   1958:                if (buffer_len(&c->input) == 0)
                   1959:                        chan_ibuf_empty(c);
1.133     markus   1960:        }
                   1961:
1.41      markus   1962: }
1.1       deraadt  1963:
1.49      markus   1964: void
1.154     markus   1965: channel_input_close(int type, u_int32_t seq, void *ctxt)
1.1       deraadt  1966: {
1.41      markus   1967:        int id;
                   1968:        Channel *c;
1.1       deraadt  1969:
1.41      markus   1970:        id = packet_get_int();
1.152     markus   1971:        packet_check_eom();
1.41      markus   1972:        c = channel_lookup(id);
                   1973:        if (c == NULL)
                   1974:                packet_disconnect("Received close for nonexistent channel %d.", id);
1.27      markus   1975:
                   1976:        /*
                   1977:         * Send a confirmation that we have closed the channel and no more
                   1978:         * data is coming for it.
                   1979:         */
1.25      markus   1980:        packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
1.41      markus   1981:        packet_put_int(c->remote_id);
1.25      markus   1982:        packet_send();
                   1983:
1.27      markus   1984:        /*
                   1985:         * If the channel is in closed state, we have sent a close request,
                   1986:         * and the other side will eventually respond with a confirmation.
                   1987:         * Thus, we cannot free the channel here, because then there would be
                   1988:         * no-one to receive the confirmation.  The channel gets freed when
                   1989:         * the confirmation arrives.
                   1990:         */
1.41      markus   1991:        if (c->type != SSH_CHANNEL_CLOSED) {
1.27      markus   1992:                /*
                   1993:                 * Not a closed channel - mark it as draining, which will
                   1994:                 * cause it to be freed later.
                   1995:                 */
1.158     markus   1996:                buffer_clear(&c->input);
1.41      markus   1997:                c->type = SSH_CHANNEL_OUTPUT_DRAINING;
1.25      markus   1998:        }
1.1       deraadt  1999: }
                   2000:
1.41      markus   2001: /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
1.49      markus   2002: void
1.154     markus   2003: channel_input_oclose(int type, u_int32_t seq, void *ctxt)
1.41      markus   2004: {
                   2005:        int id = packet_get_int();
                   2006:        Channel *c = channel_lookup(id);
1.151     markus   2007:
1.152     markus   2008:        packet_check_eom();
1.41      markus   2009:        if (c == NULL)
                   2010:                packet_disconnect("Received oclose for nonexistent channel %d.", id);
                   2011:        chan_rcvd_oclose(c);
                   2012: }
1.1       deraadt  2013:
1.49      markus   2014: void
1.154     markus   2015: channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
1.1       deraadt  2016: {
1.41      markus   2017:        int id = packet_get_int();
                   2018:        Channel *c = channel_lookup(id);
1.1       deraadt  2019:
1.152     markus   2020:        packet_check_eom();
1.41      markus   2021:        if (c == NULL)
                   2022:                packet_disconnect("Received close confirmation for "
                   2023:                    "out-of-range channel %d.", id);
                   2024:        if (c->type != SSH_CHANNEL_CLOSED)
                   2025:                packet_disconnect("Received close confirmation for "
                   2026:                    "non-closed channel %d (type %d).", id, c->type);
1.113     markus   2027:        channel_free(c);
1.1       deraadt  2028: }
                   2029:
1.49      markus   2030: void
1.154     markus   2031: channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
1.1       deraadt  2032: {
1.41      markus   2033:        int id, remote_id;
                   2034:        Channel *c;
1.1       deraadt  2035:
1.41      markus   2036:        id = packet_get_int();
                   2037:        c = channel_lookup(id);
1.25      markus   2038:
1.41      markus   2039:        if (c==NULL || c->type != SSH_CHANNEL_OPENING)
                   2040:                packet_disconnect("Received open confirmation for "
                   2041:                    "non-opening channel %d.", id);
                   2042:        remote_id = packet_get_int();
1.27      markus   2043:        /* Record the remote channel number and mark that the channel is now open. */
1.41      markus   2044:        c->remote_id = remote_id;
                   2045:        c->type = SSH_CHANNEL_OPEN;
1.44      markus   2046:
                   2047:        if (compat20) {
                   2048:                c->remote_window = packet_get_int();
                   2049:                c->remote_maxpacket = packet_get_int();
1.165     markus   2050:                if (c->confirm) {
1.70      markus   2051:                        debug2("callback start");
1.204     djm      2052:                        c->confirm(c->self, c->confirm_ctx);
1.70      markus   2053:                        debug2("callback done");
1.44      markus   2054:                }
1.194     markus   2055:                debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
1.44      markus   2056:                    c->remote_window, c->remote_maxpacket);
                   2057:        }
1.152     markus   2058:        packet_check_eom();
1.1       deraadt  2059: }
                   2060:
1.127     itojun   2061: static char *
1.114     markus   2062: reason2txt(int reason)
                   2063: {
1.143     deraadt  2064:        switch (reason) {
1.114     markus   2065:        case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
                   2066:                return "administratively prohibited";
                   2067:        case SSH2_OPEN_CONNECT_FAILED:
                   2068:                return "connect failed";
                   2069:        case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
                   2070:                return "unknown channel type";
                   2071:        case SSH2_OPEN_RESOURCE_SHORTAGE:
                   2072:                return "resource shortage";
                   2073:        }
1.117     stevesk  2074:        return "unknown reason";
1.114     markus   2075: }
                   2076:
1.49      markus   2077: void
1.154     markus   2078: channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
1.1       deraadt  2079: {
1.86      markus   2080:        int id, reason;
                   2081:        char *msg = NULL, *lang = NULL;
1.41      markus   2082:        Channel *c;
                   2083:
                   2084:        id = packet_get_int();
                   2085:        c = channel_lookup(id);
1.25      markus   2086:
1.41      markus   2087:        if (c==NULL || c->type != SSH_CHANNEL_OPENING)
                   2088:                packet_disconnect("Received open failure for "
                   2089:                    "non-opening channel %d.", id);
1.44      markus   2090:        if (compat20) {
1.86      markus   2091:                reason = packet_get_int();
1.110     markus   2092:                if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1.86      markus   2093:                        msg  = packet_get_string(NULL);
                   2094:                        lang = packet_get_string(NULL);
                   2095:                }
1.188     itojun   2096:                logit("channel %d: open failed: %s%s%s", id,
1.114     markus   2097:                    reason2txt(reason), msg ? ": ": "", msg ? msg : "");
1.86      markus   2098:                if (msg != NULL)
                   2099:                        xfree(msg);
                   2100:                if (lang != NULL)
                   2101:                        xfree(lang);
1.44      markus   2102:        }
1.152     markus   2103:        packet_check_eom();
1.25      markus   2104:        /* Free the channel.  This will also close the socket. */
1.113     markus   2105:        channel_free(c);
1.44      markus   2106: }
                   2107:
1.121     markus   2108: void
1.154     markus   2109: channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
1.121     markus   2110: {
                   2111:        Channel *c;
1.178     markus   2112:        int id;
                   2113:        u_int adjust;
1.121     markus   2114:
                   2115:        if (!compat20)
                   2116:                return;
                   2117:
                   2118:        /* Get the channel number and verify it. */
                   2119:        id = packet_get_int();
                   2120:        c = channel_lookup(id);
1.1       deraadt  2121:
1.121     markus   2122:        if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1.188     itojun   2123:                logit("Received window adjust for "
1.121     markus   2124:                    "non-open channel %d.", id);
                   2125:                return;
                   2126:        }
                   2127:        adjust = packet_get_int();
1.152     markus   2128:        packet_check_eom();
1.178     markus   2129:        debug2("channel %d: rcvd adjust %u", id, adjust);
1.121     markus   2130:        c->remote_window += adjust;
                   2131: }
1.1       deraadt  2132:
1.121     markus   2133: void
1.154     markus   2134: channel_input_port_open(int type, u_int32_t seq, void *ctxt)
1.1       deraadt  2135: {
1.121     markus   2136:        Channel *c = NULL;
                   2137:        u_short host_port;
                   2138:        char *host, *originator_string;
                   2139:        int remote_id, sock = -1;
                   2140:
                   2141:        remote_id = packet_get_int();
                   2142:        host = packet_get_string(NULL);
                   2143:        host_port = packet_get_int();
1.25      markus   2144:
1.121     markus   2145:        if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
                   2146:                originator_string = packet_get_string(NULL);
                   2147:        } else {
                   2148:                originator_string = xstrdup("unknown (remote did not supply name)");
                   2149:        }
1.152     markus   2150:        packet_check_eom();
1.121     markus   2151:        sock = channel_connect_to(host, host_port);
                   2152:        if (sock != -1) {
                   2153:                c = channel_new("connected socket",
                   2154:                    SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
                   2155:                    originator_string, 1);
1.167     markus   2156:                c->remote_id = remote_id;
1.25      markus   2157:        }
1.190     markus   2158:        xfree(originator_string);
1.121     markus   2159:        if (c == NULL) {
                   2160:                packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
                   2161:                packet_put_int(remote_id);
                   2162:                packet_send();
                   2163:        }
                   2164:        xfree(host);
1.1       deraadt  2165: }
                   2166:
1.121     markus   2167:
                   2168: /* -- tcp forwarding */
1.135     markus   2169:
                   2170: void
                   2171: channel_set_af(int af)
                   2172: {
                   2173:        IPv4or6 = af;
                   2174: }
1.121     markus   2175:
1.160     markus   2176: static int
                   2177: channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
                   2178:     const char *host_to_connect, u_short port_to_connect, int gateway_ports)
1.25      markus   2179: {
1.113     markus   2180:        Channel *c;
1.210.2.1  brad     2181:        int sock, r, success = 0, on = 1, wildcard = 0, is_client;
1.35      markus   2182:        struct addrinfo hints, *ai, *aitop;
1.210.2.1  brad     2183:        const char *host, *addr;
1.35      markus   2184:        char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.25      markus   2185:
1.160     markus   2186:        host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
                   2187:            listen_addr : host_to_connect;
1.210.2.1  brad     2188:        is_client = (type == SSH_CHANNEL_PORT_LISTENER);
1.87      markus   2189:
1.160     markus   2190:        if (host == NULL) {
                   2191:                error("No forward host name.");
                   2192:                return success;
1.73      markus   2193:        }
1.113     markus   2194:        if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
1.87      markus   2195:                error("Forward host name too long.");
                   2196:                return success;
                   2197:        }
1.25      markus   2198:
1.28      markus   2199:        /*
1.210.2.1  brad     2200:         * Determine whether or not a port forward listens to loopback,
1.210.2.2! brad     2201:         * specified address or wildcard. On the client, a specified bind
        !          2202:         * address will always override gateway_ports. On the server, a
        !          2203:         * gateway_ports of 1 (``yes'') will override the client's
        !          2204:         * specification and force a wildcard bind, whereas a value of 2
        !          2205:         * (``clientspecified'') will bind to whatever address the client
1.210.2.1  brad     2206:         * asked for.
                   2207:         *
                   2208:         * Special-case listen_addrs are:
                   2209:         *
                   2210:         * "0.0.0.0"               -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
                   2211:         * "" (empty string), "*"  -> wildcard v4/v6
                   2212:         * "localhost"             -> loopback v4/v6
                   2213:         */
                   2214:        addr = NULL;
                   2215:        if (listen_addr == NULL) {
                   2216:                /* No address specified: default to gateway_ports setting */
                   2217:                if (gateway_ports)
                   2218:                        wildcard = 1;
                   2219:        } else if (gateway_ports || is_client) {
                   2220:                if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
                   2221:                    strcmp(listen_addr, "0.0.0.0") == 0) ||
                   2222:                    *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
                   2223:                    (!is_client && gateway_ports == 1))
                   2224:                        wildcard = 1;
                   2225:                else if (strcmp(listen_addr, "localhost") != 0)
                   2226:                        addr = listen_addr;
                   2227:        }
                   2228:
                   2229:        debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
                   2230:            type, wildcard, (addr == NULL) ? "NULL" : addr);
                   2231:
                   2232:        /*
1.35      markus   2233:         * getaddrinfo returns a loopback address if the hostname is
                   2234:         * set to NULL and hints.ai_flags is not AI_PASSIVE
1.28      markus   2235:         */
1.35      markus   2236:        memset(&hints, 0, sizeof(hints));
                   2237:        hints.ai_family = IPv4or6;
1.210.2.1  brad     2238:        hints.ai_flags = wildcard ? AI_PASSIVE : 0;
1.35      markus   2239:        hints.ai_socktype = SOCK_STREAM;
1.73      markus   2240:        snprintf(strport, sizeof strport, "%d", listen_port);
1.210.2.1  brad     2241:        if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
                   2242:                if (addr == NULL) {
                   2243:                        /* This really shouldn't happen */
                   2244:                        packet_disconnect("getaddrinfo: fatal error: %s",
                   2245:                            gai_strerror(r));
                   2246:                } else {
                   2247:                        verbose("channel_setup_fwd_listener: "
                   2248:                            "getaddrinfo(%.64s): %s", addr, gai_strerror(r));
                   2249:                        packet_send_debug("channel_setup_fwd_listener: "
                   2250:                            "getaddrinfo(%.64s): %s", addr, gai_strerror(r));
                   2251:                }
                   2252:                aitop = NULL;
                   2253:        }
1.35      markus   2254:
                   2255:        for (ai = aitop; ai; ai = ai->ai_next) {
                   2256:                if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
                   2257:                        continue;
                   2258:                if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
                   2259:                    strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.160     markus   2260:                        error("channel_setup_fwd_listener: getnameinfo failed");
1.35      markus   2261:                        continue;
                   2262:                }
                   2263:                /* Create a port to listen for the host. */
1.189     markus   2264:                sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.35      markus   2265:                if (sock < 0) {
                   2266:                        /* this is no error since kernel may not support ipv6 */
                   2267:                        verbose("socket: %.100s", strerror(errno));
                   2268:                        continue;
                   2269:                }
                   2270:                /*
1.182     stevesk  2271:                 * Set socket options.
                   2272:                 * Allow local port reuse in TIME_WAIT.
1.35      markus   2273:                 */
1.182     stevesk  2274:                if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on,
                   2275:                    sizeof(on)) == -1)
                   2276:                        error("setsockopt SO_REUSEADDR: %s", strerror(errno));
                   2277:
1.35      markus   2278:                debug("Local forwarding listening on %s port %s.", ntop, strport);
                   2279:
                   2280:                /* Bind the socket to the address. */
                   2281:                if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
                   2282:                        /* address can be in use ipv6 address is already bound */
                   2283:                        verbose("bind: %.100s", strerror(errno));
                   2284:                        close(sock);
                   2285:                        continue;
                   2286:                }
                   2287:                /* Start listening for connections on the socket. */
1.199     markus   2288:                if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1.35      markus   2289:                        error("listen: %.100s", strerror(errno));
                   2290:                        close(sock);
                   2291:                        continue;
                   2292:                }
                   2293:                /* Allocate a channel number for the socket. */
1.121     markus   2294:                c = channel_new("port listener", type, sock, sock, -1,
1.51      markus   2295:                    CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1.190     markus   2296:                    0, "port listener", 1);
1.113     markus   2297:                strlcpy(c->path, host, sizeof(c->path));
                   2298:                c->host_port = port_to_connect;
                   2299:                c->listening_port = listen_port;
1.35      markus   2300:                success = 1;
                   2301:        }
                   2302:        if (success == 0)
1.160     markus   2303:                error("channel_setup_fwd_listener: cannot listen to port: %d",
1.87      markus   2304:                    listen_port);
1.35      markus   2305:        freeaddrinfo(aitop);
1.87      markus   2306:        return success;
1.25      markus   2307: }
1.1       deraadt  2308:
1.202     djm      2309: int
                   2310: channel_cancel_rport_listener(const char *host, u_short port)
                   2311: {
1.209     avsm     2312:        u_int i;
                   2313:        int found = 0;
1.202     djm      2314:
1.210.2.2! brad     2315:        for (i = 0; i < channels_alloc; i++) {
1.202     djm      2316:                Channel *c = channels[i];
                   2317:
                   2318:                if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
                   2319:                    strncmp(c->path, host, sizeof(c->path)) == 0 &&
1.208     deraadt  2320:                    c->listening_port == port) {
1.210     djm      2321:                        debug2("%s: close channel %d", __func__, i);
1.202     djm      2322:                        channel_free(c);
                   2323:                        found = 1;
                   2324:                }
                   2325:        }
                   2326:
                   2327:        return (found);
                   2328: }
                   2329:
1.160     markus   2330: /* protocol local port fwd, used by ssh (and sshd in v1) */
                   2331: int
1.210.2.1  brad     2332: channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
1.160     markus   2333:     const char *host_to_connect, u_short port_to_connect, int gateway_ports)
                   2334: {
                   2335:        return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
1.210.2.1  brad     2336:            listen_host, listen_port, host_to_connect, port_to_connect,
                   2337:            gateway_ports);
1.160     markus   2338: }
                   2339:
                   2340: /* protocol v2 remote port fwd, used by sshd */
                   2341: int
                   2342: channel_setup_remote_fwd_listener(const char *listen_address,
                   2343:     u_short listen_port, int gateway_ports)
                   2344: {
                   2345:        return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
                   2346:            listen_address, listen_port, NULL, 0, gateway_ports);
                   2347: }
                   2348:
1.27      markus   2349: /*
                   2350:  * Initiate forwarding of connections to port "port" on remote host through
                   2351:  * the secure channel to host:port from local side.
                   2352:  */
1.1       deraadt  2353:
1.49      markus   2354: void
1.210.2.1  brad     2355: channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
1.73      markus   2356:     const char *host_to_connect, u_short port_to_connect)
1.25      markus   2357: {
1.153     markus   2358:        int type, success = 0;
1.73      markus   2359:
1.25      markus   2360:        /* Record locally that connection to this host/port is permitted. */
                   2361:        if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
                   2362:                fatal("channel_request_remote_forwarding: too many forwards");
                   2363:
                   2364:        /* Send the forward request to the remote side. */
1.44      markus   2365:        if (compat20) {
1.210.2.1  brad     2366:                const char *address_to_bind;
                   2367:                if (listen_host == NULL)
                   2368:                        address_to_bind = "localhost";
                   2369:                else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0)
                   2370:                        address_to_bind = "";
                   2371:                else
                   2372:                        address_to_bind = listen_host;
                   2373:
1.44      markus   2374:                packet_start(SSH2_MSG_GLOBAL_REQUEST);
                   2375:                packet_put_cstring("tcpip-forward");
1.173     markus   2376:                packet_put_char(1);                     /* boolean: want reply */
1.44      markus   2377:                packet_put_cstring(address_to_bind);
                   2378:                packet_put_int(listen_port);
1.73      markus   2379:                packet_send();
                   2380:                packet_write_wait();
                   2381:                /* Assume that server accepts the request */
                   2382:                success = 1;
1.44      markus   2383:        } else {
                   2384:                packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
1.50      markus   2385:                packet_put_int(listen_port);
                   2386:                packet_put_cstring(host_to_connect);
1.44      markus   2387:                packet_put_int(port_to_connect);
                   2388:                packet_send();
                   2389:                packet_write_wait();
1.73      markus   2390:
                   2391:                /* Wait for response from the remote side. */
1.153     markus   2392:                type = packet_read();
1.73      markus   2393:                switch (type) {
                   2394:                case SSH_SMSG_SUCCESS:
                   2395:                        success = 1;
                   2396:                        break;
                   2397:                case SSH_SMSG_FAILURE:
1.188     itojun   2398:                        logit("Warning: Server denied remote port forwarding.");
1.73      markus   2399:                        break;
                   2400:                default:
                   2401:                        /* Unknown packet */
                   2402:                        packet_disconnect("Protocol error for port forward request:"
                   2403:                            "received packet type %d.", type);
                   2404:                }
                   2405:        }
                   2406:        if (success) {
                   2407:                permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
                   2408:                permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
                   2409:                permitted_opens[num_permitted_opens].listen_port = listen_port;
                   2410:                num_permitted_opens++;
1.44      markus   2411:        }
1.1       deraadt  2412: }
                   2413:
1.27      markus   2414: /*
1.208     deraadt  2415:  * Request cancellation of remote forwarding of connection host:port from
1.202     djm      2416:  * local side.
                   2417:  */
                   2418: void
1.210.2.1  brad     2419: channel_request_rforward_cancel(const char *host, u_short port)
1.202     djm      2420: {
                   2421:        int i;
                   2422:
                   2423:        if (!compat20)
                   2424:                return;
                   2425:
                   2426:        for (i = 0; i < num_permitted_opens; i++) {
1.208     deraadt  2427:                if (permitted_opens[i].host_to_connect != NULL &&
1.202     djm      2428:                    permitted_opens[i].listen_port == port)
                   2429:                        break;
                   2430:        }
                   2431:        if (i >= num_permitted_opens) {
                   2432:                debug("%s: requested forward not found", __func__);
                   2433:                return;
                   2434:        }
                   2435:        packet_start(SSH2_MSG_GLOBAL_REQUEST);
                   2436:        packet_put_cstring("cancel-tcpip-forward");
                   2437:        packet_put_char(0);
1.210.2.1  brad     2438:        packet_put_cstring(host == NULL ? "" : host);
1.202     djm      2439:        packet_put_int(port);
                   2440:        packet_send();
                   2441:
                   2442:        permitted_opens[i].listen_port = 0;
                   2443:        permitted_opens[i].port_to_connect = 0;
                   2444:        free(permitted_opens[i].host_to_connect);
                   2445:        permitted_opens[i].host_to_connect = NULL;
                   2446: }
                   2447:
                   2448: /*
1.27      markus   2449:  * This is called after receiving CHANNEL_FORWARDING_REQUEST.  This initates
                   2450:  * listening for the port, and sends back a success reply (or disconnect
                   2451:  * message if there was an error).  This never returns if there was an error.
                   2452:  */
1.1       deraadt  2453:
1.49      markus   2454: void
1.56      markus   2455: channel_input_port_forward_request(int is_root, int gateway_ports)
1.1       deraadt  2456: {
1.31      markus   2457:        u_short port, host_port;
1.25      markus   2458:        char *hostname;
1.1       deraadt  2459:
1.25      markus   2460:        /* Get arguments from the packet. */
                   2461:        port = packet_get_int();
                   2462:        hostname = packet_get_string(NULL);
                   2463:        host_port = packet_get_int();
                   2464:
1.27      markus   2465:        /*
                   2466:         * Check that an unprivileged user is not trying to forward a
                   2467:         * privileged port.
                   2468:         */
1.25      markus   2469:        if (port < IPPORT_RESERVED && !is_root)
1.192     markus   2470:                packet_disconnect(
                   2471:                    "Requested forwarding of port %d but user is not root.",
                   2472:                    port);
                   2473:        if (host_port == 0)
                   2474:                packet_disconnect("Dynamic forwarding denied.");
                   2475:
1.73      markus   2476:        /* Initiate forwarding */
1.210.2.1  brad     2477:        channel_setup_local_fwd_listener(NULL, port, hostname,
                   2478:            host_port, gateway_ports);
1.25      markus   2479:
                   2480:        /* Free the argument string. */
                   2481:        xfree(hostname);
1.1       deraadt  2482: }
                   2483:
1.99      markus   2484: /*
                   2485:  * Permits opening to any host/port if permitted_opens[] is empty.  This is
                   2486:  * usually called by the server, because the user could connect to any port
                   2487:  * anyway, and the server has no way to know but to trust the client anyway.
                   2488:  */
                   2489: void
1.142     itojun   2490: channel_permit_all_opens(void)
1.99      markus   2491: {
                   2492:        if (num_permitted_opens == 0)
                   2493:                all_opens_permitted = 1;
                   2494: }
                   2495:
1.101     markus   2496: void
1.99      markus   2497: channel_add_permitted_opens(char *host, int port)
                   2498: {
                   2499:        if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
                   2500:                fatal("channel_request_remote_forwarding: too many forwards");
                   2501:        debug("allow port forwarding to host %s port %d", host, port);
                   2502:
                   2503:        permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
                   2504:        permitted_opens[num_permitted_opens].port_to_connect = port;
                   2505:        num_permitted_opens++;
                   2506:
                   2507:        all_opens_permitted = 0;
                   2508: }
                   2509:
1.101     markus   2510: void
1.99      markus   2511: channel_clear_permitted_opens(void)
                   2512: {
                   2513:        int i;
                   2514:
                   2515:        for (i = 0; i < num_permitted_opens; i++)
1.202     djm      2516:                if (permitted_opens[i].host_to_connect != NULL)
                   2517:                        xfree(permitted_opens[i].host_to_connect);
1.99      markus   2518:        num_permitted_opens = 0;
                   2519:
                   2520: }
                   2521:
                   2522:
                   2523: /* return socket to remote host, port */
1.127     itojun   2524: static int
1.99      markus   2525: connect_to(const char *host, u_short port)
1.41      markus   2526: {
                   2527:        struct addrinfo hints, *ai, *aitop;
                   2528:        char ntop[NI_MAXHOST], strport[NI_MAXSERV];
                   2529:        int gaierr;
                   2530:        int sock = -1;
                   2531:
                   2532:        memset(&hints, 0, sizeof(hints));
                   2533:        hints.ai_family = IPv4or6;
                   2534:        hints.ai_socktype = SOCK_STREAM;
1.99      markus   2535:        snprintf(strport, sizeof strport, "%d", port);
1.41      markus   2536:        if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
1.99      markus   2537:                error("connect_to %.100s: unknown host (%s)", host,
                   2538:                    gai_strerror(gaierr));
1.41      markus   2539:                return -1;
                   2540:        }
                   2541:        for (ai = aitop; ai; ai = ai->ai_next) {
                   2542:                if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
                   2543:                        continue;
                   2544:                if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
                   2545:                    strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.99      markus   2546:                        error("connect_to: getnameinfo failed");
1.41      markus   2547:                        continue;
                   2548:                }
1.189     markus   2549:                sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.41      markus   2550:                if (sock < 0) {
1.186     djm      2551:                        if (ai->ai_next == NULL)
                   2552:                                error("socket: %.100s", strerror(errno));
                   2553:                        else
                   2554:                                verbose("socket: %.100s", strerror(errno));
1.41      markus   2555:                        continue;
                   2556:                }
1.205     djm      2557:                if (set_nonblock(sock) == -1)
                   2558:                        fatal("%s: set_nonblock(%d)", __func__, sock);
1.75      markus   2559:                if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
                   2560:                    errno != EINPROGRESS) {
1.99      markus   2561:                        error("connect_to %.100s port %s: %.100s", ntop, strport,
1.41      markus   2562:                            strerror(errno));
                   2563:                        close(sock);
1.89      stevesk  2564:                        continue;       /* fail -- try next */
1.41      markus   2565:                }
                   2566:                break; /* success */
                   2567:
                   2568:        }
                   2569:        freeaddrinfo(aitop);
                   2570:        if (!ai) {
1.99      markus   2571:                error("connect_to %.100s port %d: failed.", host, port);
1.41      markus   2572:                return -1;
                   2573:        }
                   2574:        /* success */
1.169     stevesk  2575:        set_nodelay(sock);
1.41      markus   2576:        return sock;
                   2577: }
1.99      markus   2578:
1.73      markus   2579: int
1.130     stevesk  2580: channel_connect_by_listen_address(u_short listen_port)
1.73      markus   2581: {
                   2582:        int i;
1.99      markus   2583:
1.73      markus   2584:        for (i = 0; i < num_permitted_opens; i++)
1.202     djm      2585:                if (permitted_opens[i].host_to_connect != NULL &&
                   2586:                    permitted_opens[i].listen_port == listen_port)
1.99      markus   2587:                        return connect_to(
1.73      markus   2588:                            permitted_opens[i].host_to_connect,
                   2589:                            permitted_opens[i].port_to_connect);
1.74      markus   2590:        error("WARNING: Server requests forwarding for unknown listen_port %d",
                   2591:            listen_port);
1.73      markus   2592:        return -1;
                   2593: }
                   2594:
1.99      markus   2595: /* Check if connecting to that port is permitted and connect. */
                   2596: int
                   2597: channel_connect_to(const char *host, u_short port)
                   2598: {
                   2599:        int i, permit;
                   2600:
                   2601:        permit = all_opens_permitted;
                   2602:        if (!permit) {
                   2603:                for (i = 0; i < num_permitted_opens; i++)
1.202     djm      2604:                        if (permitted_opens[i].host_to_connect != NULL &&
                   2605:                            permitted_opens[i].port_to_connect == port &&
1.99      markus   2606:                            strcmp(permitted_opens[i].host_to_connect, host) == 0)
                   2607:                                permit = 1;
                   2608:
                   2609:        }
                   2610:        if (!permit) {
1.188     itojun   2611:                logit("Received request to connect to host %.100s port %d, "
1.99      markus   2612:                    "but the request was denied.", host, port);
                   2613:                return -1;
                   2614:        }
                   2615:        return connect_to(host, port);
1.204     djm      2616: }
                   2617:
                   2618: void
                   2619: channel_send_window_changes(void)
                   2620: {
1.209     avsm     2621:        u_int i;
1.204     djm      2622:        struct winsize ws;
                   2623:
                   2624:        for (i = 0; i < channels_alloc; i++) {
1.210.2.2! brad     2625:                if (channels[i] == NULL || !channels[i]->client_tty ||
1.204     djm      2626:                    channels[i]->type != SSH_CHANNEL_OPEN)
                   2627:                        continue;
                   2628:                if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
                   2629:                        continue;
                   2630:                channel_request_start(i, "window-change", 0);
                   2631:                packet_put_int(ws.ws_col);
                   2632:                packet_put_int(ws.ws_row);
                   2633:                packet_put_int(ws.ws_xpixel);
                   2634:                packet_put_int(ws.ws_ypixel);
                   2635:                packet_send();
                   2636:        }
1.99      markus   2637: }
                   2638:
1.121     markus   2639: /* -- X11 forwarding */
1.1       deraadt  2640:
1.27      markus   2641: /*
                   2642:  * Creates an internet domain socket for listening for X11 connections.
1.176     deraadt  2643:  * Returns 0 and a suitable display number for the DISPLAY variable
                   2644:  * stored in display_numberp , or -1 if an error occurs.
1.27      markus   2645:  */
1.141     stevesk  2646: int
1.163     stevesk  2647: x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
1.176     deraadt  2648:     int single_connection, u_int *display_numberp)
1.1       deraadt  2649: {
1.149     markus   2650:        Channel *nc = NULL;
1.31      markus   2651:        int display_number, sock;
                   2652:        u_short port;
1.35      markus   2653:        struct addrinfo hints, *ai, *aitop;
                   2654:        char strport[NI_MAXSERV];
                   2655:        int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1.25      markus   2656:
1.33      markus   2657:        for (display_number = x11_display_offset;
1.148     deraadt  2658:            display_number < MAX_DISPLAYS;
                   2659:            display_number++) {
1.25      markus   2660:                port = 6000 + display_number;
1.35      markus   2661:                memset(&hints, 0, sizeof(hints));
                   2662:                hints.ai_family = IPv4or6;
1.163     stevesk  2663:                hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
1.35      markus   2664:                hints.ai_socktype = SOCK_STREAM;
                   2665:                snprintf(strport, sizeof strport, "%d", port);
                   2666:                if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
                   2667:                        error("getaddrinfo: %.100s", gai_strerror(gaierr));
1.141     stevesk  2668:                        return -1;
1.25      markus   2669:                }
1.35      markus   2670:                for (ai = aitop; ai; ai = ai->ai_next) {
                   2671:                        if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
                   2672:                                continue;
1.189     markus   2673:                        sock = socket(ai->ai_family, ai->ai_socktype,
                   2674:                            ai->ai_protocol);
1.35      markus   2675:                        if (sock < 0) {
                   2676:                                error("socket: %.100s", strerror(errno));
1.203     markus   2677:                                freeaddrinfo(aitop);
1.141     stevesk  2678:                                return -1;
1.35      markus   2679:                        }
                   2680:                        if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1.194     markus   2681:                                debug2("bind port %d: %.100s", port, strerror(errno));
1.35      markus   2682:                                close(sock);
1.183     itojun   2683:
                   2684:                                if (ai->ai_next)
                   2685:                                        continue;
                   2686:
1.35      markus   2687:                                for (n = 0; n < num_socks; n++) {
                   2688:                                        close(socks[n]);
                   2689:                                }
                   2690:                                num_socks = 0;
                   2691:                                break;
                   2692:                        }
                   2693:                        socks[num_socks++] = sock;
                   2694:                        if (num_socks == NUM_SOCKS)
                   2695:                                break;
1.25      markus   2696:                }
1.83      stevesk  2697:                freeaddrinfo(aitop);
1.35      markus   2698:                if (num_socks > 0)
                   2699:                        break;
1.25      markus   2700:        }
                   2701:        if (display_number >= MAX_DISPLAYS) {
                   2702:                error("Failed to allocate internet-domain X11 display socket.");
1.141     stevesk  2703:                return -1;
1.25      markus   2704:        }
                   2705:        /* Start listening for connections on the socket. */
1.35      markus   2706:        for (n = 0; n < num_socks; n++) {
                   2707:                sock = socks[n];
1.199     markus   2708:                if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1.35      markus   2709:                        error("listen: %.100s", strerror(errno));
                   2710:                        close(sock);
1.141     stevesk  2711:                        return -1;
1.35      markus   2712:                }
1.25      markus   2713:        }
1.35      markus   2714:
                   2715:        /* Allocate a channel for each socket. */
                   2716:        for (n = 0; n < num_socks; n++) {
                   2717:                sock = socks[n];
1.149     markus   2718:                nc = channel_new("x11 listener",
1.51      markus   2719:                    SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
                   2720:                    CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
1.190     markus   2721:                    0, "X11 inet listener", 1);
1.167     markus   2722:                nc->single_connection = single_connection;
1.35      markus   2723:        }
1.1       deraadt  2724:
1.141     stevesk  2725:        /* Return the display number for the DISPLAY environment variable. */
1.176     deraadt  2726:        *display_numberp = display_number;
                   2727:        return (0);
1.1       deraadt  2728: }
                   2729:
1.127     itojun   2730: static int
1.77      markus   2731: connect_local_xsocket(u_int dnr)
1.1       deraadt  2732: {
1.25      markus   2733:        int sock;
                   2734:        struct sockaddr_un addr;
                   2735:
1.147     stevesk  2736:        sock = socket(AF_UNIX, SOCK_STREAM, 0);
                   2737:        if (sock < 0)
                   2738:                error("socket: %.100s", strerror(errno));
                   2739:        memset(&addr, 0, sizeof(addr));
                   2740:        addr.sun_family = AF_UNIX;
                   2741:        snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
                   2742:        if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
                   2743:                return sock;
                   2744:        close(sock);
1.25      markus   2745:        error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
                   2746:        return -1;
1.1       deraadt  2747: }
                   2748:
1.51      markus   2749: int
                   2750: x11_connect_display(void)
1.1       deraadt  2751: {
1.162     stevesk  2752:        int display_number, sock = 0;
1.25      markus   2753:        const char *display;
1.51      markus   2754:        char buf[1024], *cp;
1.35      markus   2755:        struct addrinfo hints, *ai, *aitop;
                   2756:        char strport[NI_MAXSERV];
                   2757:        int gaierr;
1.25      markus   2758:
                   2759:        /* Try to open a socket for the local X server. */
                   2760:        display = getenv("DISPLAY");
                   2761:        if (!display) {
                   2762:                error("DISPLAY not set.");
1.51      markus   2763:                return -1;
1.25      markus   2764:        }
1.27      markus   2765:        /*
                   2766:         * Now we decode the value of the DISPLAY variable and make a
                   2767:         * connection to the real X server.
                   2768:         */
                   2769:
                   2770:        /*
                   2771:         * Check if it is a unix domain socket.  Unix domain displays are in
                   2772:         * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
                   2773:         */
1.25      markus   2774:        if (strncmp(display, "unix:", 5) == 0 ||
                   2775:            display[0] == ':') {
                   2776:                /* Connect to the unix domain socket. */
                   2777:                if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
                   2778:                        error("Could not parse display number from DISPLAY: %.100s",
1.148     deraadt  2779:                            display);
1.51      markus   2780:                        return -1;
1.25      markus   2781:                }
                   2782:                /* Create a socket. */
                   2783:                sock = connect_local_xsocket(display_number);
                   2784:                if (sock < 0)
1.51      markus   2785:                        return -1;
1.25      markus   2786:
                   2787:                /* OK, we now have a connection to the display. */
1.51      markus   2788:                return sock;
1.25      markus   2789:        }
1.27      markus   2790:        /*
                   2791:         * Connect to an inet socket.  The DISPLAY value is supposedly
                   2792:         * hostname:d[.s], where hostname may also be numeric IP address.
                   2793:         */
1.145     stevesk  2794:        strlcpy(buf, display, sizeof(buf));
1.25      markus   2795:        cp = strchr(buf, ':');
                   2796:        if (!cp) {
                   2797:                error("Could not find ':' in DISPLAY: %.100s", display);
1.51      markus   2798:                return -1;
1.25      markus   2799:        }
                   2800:        *cp = 0;
1.27      markus   2801:        /* buf now contains the host name.  But first we parse the display number. */
1.25      markus   2802:        if (sscanf(cp + 1, "%d", &display_number) != 1) {
                   2803:                error("Could not parse display number from DISPLAY: %.100s",
1.148     deraadt  2804:                    display);
1.51      markus   2805:                return -1;
1.25      markus   2806:        }
1.35      markus   2807:
                   2808:        /* Look up the host address */
                   2809:        memset(&hints, 0, sizeof(hints));
                   2810:        hints.ai_family = IPv4or6;
                   2811:        hints.ai_socktype = SOCK_STREAM;
                   2812:        snprintf(strport, sizeof strport, "%d", 6000 + display_number);
                   2813:        if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
                   2814:                error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
1.51      markus   2815:                return -1;
1.25      markus   2816:        }
1.35      markus   2817:        for (ai = aitop; ai; ai = ai->ai_next) {
                   2818:                /* Create a socket. */
1.189     markus   2819:                sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.35      markus   2820:                if (sock < 0) {
1.194     markus   2821:                        debug2("socket: %.100s", strerror(errno));
1.41      markus   2822:                        continue;
                   2823:                }
                   2824:                /* Connect it to the display. */
                   2825:                if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1.194     markus   2826:                        debug2("connect %.100s port %d: %.100s", buf,
1.41      markus   2827:                            6000 + display_number, strerror(errno));
                   2828:                        close(sock);
                   2829:                        continue;
                   2830:                }
                   2831:                /* Success */
                   2832:                break;
1.35      markus   2833:        }
                   2834:        freeaddrinfo(aitop);
                   2835:        if (!ai) {
1.49      markus   2836:                error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
1.35      markus   2837:                    strerror(errno));
1.51      markus   2838:                return -1;
1.25      markus   2839:        }
1.162     stevesk  2840:        set_nodelay(sock);
1.51      markus   2841:        return sock;
                   2842: }
                   2843:
                   2844: /*
                   2845:  * This is called when SSH_SMSG_X11_OPEN is received.  The packet contains
                   2846:  * the remote channel number.  We should do whatever we want, and respond
                   2847:  * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
                   2848:  */
                   2849:
                   2850: void
1.154     markus   2851: x11_input_open(int type, u_int32_t seq, void *ctxt)
1.51      markus   2852: {
1.113     markus   2853:        Channel *c = NULL;
                   2854:        int remote_id, sock = 0;
1.51      markus   2855:        char *remote_host;
1.25      markus   2856:
1.113     markus   2857:        debug("Received X11 open request.");
1.51      markus   2858:
1.113     markus   2859:        remote_id = packet_get_int();
1.121     markus   2860:
                   2861:        if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
1.113     markus   2862:                remote_host = packet_get_string(NULL);
1.51      markus   2863:        } else {
                   2864:                remote_host = xstrdup("unknown (remote did not supply name)");
                   2865:        }
1.152     markus   2866:        packet_check_eom();
1.25      markus   2867:
1.51      markus   2868:        /* Obtain a connection to the real X display. */
                   2869:        sock = x11_connect_display();
1.113     markus   2870:        if (sock != -1) {
                   2871:                /* Allocate a channel for this connection. */
                   2872:                c = channel_new("connected x11 socket",
                   2873:                    SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
                   2874:                    remote_host, 1);
1.167     markus   2875:                c->remote_id = remote_id;
                   2876:                c->force_drain = 1;
1.113     markus   2877:        }
1.190     markus   2878:        xfree(remote_host);
1.113     markus   2879:        if (c == NULL) {
1.51      markus   2880:                /* Send refusal to the remote host. */
                   2881:                packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1.113     markus   2882:                packet_put_int(remote_id);
1.51      markus   2883:        } else {
                   2884:                /* Send a confirmation to the remote host. */
                   2885:                packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1.113     markus   2886:                packet_put_int(remote_id);
                   2887:                packet_put_int(c->self);
1.51      markus   2888:        }
1.113     markus   2889:        packet_send();
1.72      markus   2890: }
                   2891:
                   2892: /* dummy protocol handler that denies SSH-1 requests (agent/x11) */
                   2893: void
1.154     markus   2894: deny_input_open(int type, u_int32_t seq, void *ctxt)
1.72      markus   2895: {
                   2896:        int rchan = packet_get_int();
1.180     deraadt  2897:
1.143     deraadt  2898:        switch (type) {
1.72      markus   2899:        case SSH_SMSG_AGENT_OPEN:
                   2900:                error("Warning: ssh server tried agent forwarding.");
                   2901:                break;
                   2902:        case SSH_SMSG_X11_OPEN:
                   2903:                error("Warning: ssh server tried X11 forwarding.");
                   2904:                break;
                   2905:        default:
1.154     markus   2906:                error("deny_input_open: type %d", type);
1.72      markus   2907:                break;
                   2908:        }
                   2909:        error("Warning: this is probably a break in attempt by a malicious server.");
                   2910:        packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
                   2911:        packet_put_int(rchan);
                   2912:        packet_send();
1.1       deraadt  2913: }
                   2914:
1.27      markus   2915: /*
                   2916:  * Requests forwarding of X11 connections, generates fake authentication
                   2917:  * data, and enables authentication spoofing.
1.121     markus   2918:  * This should be called in the client only.
1.27      markus   2919:  */
1.49      markus   2920: void
1.51      markus   2921: x11_request_forwarding_with_spoofing(int client_session_id,
                   2922:     const char *proto, const char *data)
1.1       deraadt  2923: {
1.77      markus   2924:        u_int data_len = (u_int) strlen(data) / 2;
1.90      markus   2925:        u_int i, value, len;
1.25      markus   2926:        char *new_data;
                   2927:        int screen_number;
                   2928:        const char *cp;
1.207     avsm     2929:        u_int32_t rnd = 0;
1.25      markus   2930:
                   2931:        cp = getenv("DISPLAY");
                   2932:        if (cp)
                   2933:                cp = strchr(cp, ':');
                   2934:        if (cp)
                   2935:                cp = strchr(cp, '.');
                   2936:        if (cp)
                   2937:                screen_number = atoi(cp + 1);
                   2938:        else
                   2939:                screen_number = 0;
                   2940:
                   2941:        /* Save protocol name. */
                   2942:        x11_saved_proto = xstrdup(proto);
                   2943:
1.27      markus   2944:        /*
                   2945:         * Extract real authentication data and generate fake data of the
                   2946:         * same length.
                   2947:         */
1.25      markus   2948:        x11_saved_data = xmalloc(data_len);
                   2949:        x11_fake_data = xmalloc(data_len);
                   2950:        for (i = 0; i < data_len; i++) {
                   2951:                if (sscanf(data + 2 * i, "%2x", &value) != 1)
                   2952:                        fatal("x11_request_forwarding: bad authentication data: %.100s", data);
                   2953:                if (i % 4 == 0)
1.207     avsm     2954:                        rnd = arc4random();
1.25      markus   2955:                x11_saved_data[i] = value;
1.207     avsm     2956:                x11_fake_data[i] = rnd & 0xff;
                   2957:                rnd >>= 8;
1.25      markus   2958:        }
                   2959:        x11_saved_data_len = data_len;
                   2960:        x11_fake_data_len = data_len;
                   2961:
                   2962:        /* Convert the fake data into hex. */
1.90      markus   2963:        len = 2 * data_len + 1;
                   2964:        new_data = xmalloc(len);
1.25      markus   2965:        for (i = 0; i < data_len; i++)
1.90      markus   2966:                snprintf(new_data + 2 * i, len - 2 * i,
                   2967:                    "%02x", (u_char) x11_fake_data[i]);
1.25      markus   2968:
                   2969:        /* Send the request packet. */
1.51      markus   2970:        if (compat20) {
                   2971:                channel_request_start(client_session_id, "x11-req", 0);
                   2972:                packet_put_char(0);     /* XXX bool single connection */
                   2973:        } else {
                   2974:                packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
                   2975:        }
                   2976:        packet_put_cstring(proto);
                   2977:        packet_put_cstring(new_data);
1.25      markus   2978:        packet_put_int(screen_number);
                   2979:        packet_send();
                   2980:        packet_write_wait();
                   2981:        xfree(new_data);
1.1       deraadt  2982: }
                   2983:
1.121     markus   2984:
                   2985: /* -- agent forwarding */
                   2986:
1.1       deraadt  2987: /* Sends a message to the server to request authentication fd forwarding. */
                   2988:
1.49      markus   2989: void
1.142     itojun   2990: auth_request_forwarding(void)
1.1       deraadt  2991: {
1.25      markus   2992:        packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
                   2993:        packet_send();
                   2994:        packet_write_wait();
1.1       deraadt  2995: }