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

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