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

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