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

1.1       deraadt     1: /*
1.26      deraadt     2:  *
                      3:  * channels.c
                      4:  *
                      5:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      6:  *
                      7:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      8:  *                    All rights reserved
                      9:  *
                     10:  * Created: Fri Mar 24 16:35:24 1995 ylo
                     11:  *
                     12:  * This file contains functions for generic socket connection forwarding.
                     13:  * There is also code for initiating connection forwarding for X11 connections,
                     14:  * arbitrary tcp/ip connections, and the authentication agent connection.
                     15:  *
1.44      markus     16:  * SSH2 support added by Markus Friedl.
1.26      deraadt    17:  */
1.1       deraadt    18:
                     19: #include "includes.h"
1.46    ! markus     20: RCSID("$Id: channels.c,v 1.45 2000/04/04 06:18:01 markus Exp $");
1.1       deraadt    21:
                     22: #include "ssh.h"
                     23: #include "packet.h"
                     24: #include "xmalloc.h"
                     25: #include "buffer.h"
                     26: #include "authfd.h"
                     27: #include "uidswap.h"
1.20      markus     28: #include "readconf.h"
1.3       deraadt    29: #include "servconf.h"
1.1       deraadt    30:
1.14      markus     31: #include "channels.h"
                     32: #include "nchan.h"
                     33: #include "compat.h"
                     34:
1.44      markus     35: #include "ssh2.h"
                     36:
1.1       deraadt    37: /* Maximum number of fake X11 displays to try. */
                     38: #define MAX_DISPLAYS  1000
                     39:
1.12      markus     40: /* Max len of agent socket */
                     41: #define MAX_SOCKET_NAME 100
                     42:
1.41      markus     43: /* default buffer for tcp-fwd-channel */
                     44: #define CHAN_WINDOW_DEFAULT      (8*1024)
                     45: #define CHAN_PACKET_DEFAULT     (CHAN_WINDOW_DEFAULT/2)
                     46:
1.27      markus     47: /*
                     48:  * Pointer to an array containing all allocated channels.  The array is
                     49:  * dynamically extended as needed.
                     50:  */
1.1       deraadt    51: static Channel *channels = NULL;
                     52:
1.27      markus     53: /*
                     54:  * Size of the channel array.  All slots of the array must always be
                     55:  * initialized (at least the type field); unused slots are marked with type
                     56:  * SSH_CHANNEL_FREE.
                     57:  */
1.1       deraadt    58: static int channels_alloc = 0;
                     59:
1.27      markus     60: /*
                     61:  * Maximum file descriptor value used in any of the channels.  This is
                     62:  * updated in channel_allocate.
                     63:  */
1.1       deraadt    64: static int channel_max_fd_value = 0;
                     65:
1.12      markus     66: /* Name and directory of socket for authentication agent forwarding. */
1.1       deraadt    67: static char *channel_forwarded_auth_socket_name = NULL;
1.25      markus     68: static char *channel_forwarded_auth_socket_dir = NULL;
1.1       deraadt    69:
                     70: /* Saved X11 authentication protocol name. */
                     71: char *x11_saved_proto = NULL;
                     72:
                     73: /* Saved X11 authentication data.  This is the real data. */
                     74: char *x11_saved_data = NULL;
                     75: unsigned int x11_saved_data_len = 0;
                     76:
1.27      markus     77: /*
                     78:  * Fake X11 authentication data.  This is what the server will be sending us;
                     79:  * we should replace any occurrences of this by the real data.
                     80:  */
1.1       deraadt    81: char *x11_fake_data = NULL;
                     82: unsigned int x11_fake_data_len;
                     83:
1.27      markus     84: /*
                     85:  * Data structure for storing which hosts are permitted for forward requests.
                     86:  * The local sides of any remote forwards are stored in this array to prevent
                     87:  * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
                     88:  * network (which might be behind a firewall).
                     89:  */
1.25      markus     90: typedef struct {
1.41      markus     91:        char *host_to_connect;          /* Connect to 'host'. */
                     92:        u_short port_to_connect;        /* Connect to 'port'. */
                     93:        u_short listen_port;            /* Remote side should listen port number. */
1.1       deraadt    94: } ForwardPermission;
                     95:
                     96: /* List of all permitted host/port pairs to connect. */
                     97: static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
                     98: /* Number of permitted host/port pairs in the array. */
                     99: static int num_permitted_opens = 0;
1.27      markus    100: /*
                    101:  * If this is true, all opens are permitted.  This is the case on the server
                    102:  * on which we have to trust the client anyway, and the user could do
                    103:  * anything after logging in anyway.
                    104:  */
1.1       deraadt   105: static int all_opens_permitted = 0;
                    106:
                    107: /* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
                    108: static int have_hostname_in_open = 0;
                    109:
                    110: /* Sets specific protocol options. */
                    111:
1.25      markus    112: void
                    113: channel_set_options(int hostname_in_open)
1.1       deraadt   114: {
1.25      markus    115:        have_hostname_in_open = hostname_in_open;
1.1       deraadt   116: }
                    117:
1.27      markus    118: /*
                    119:  * Permits opening to any host/port in SSH_MSG_PORT_OPEN.  This is usually
                    120:  * called by the server, because the user could connect to any port anyway,
                    121:  * and the server has no way to know but to trust the client anyway.
                    122:  */
1.1       deraadt   123:
1.25      markus    124: void
                    125: channel_permit_all_opens()
1.1       deraadt   126: {
1.25      markus    127:        all_opens_permitted = 1;
1.1       deraadt   128: }
                    129:
1.41      markus    130: /* lookup channel by id */
                    131:
                    132: Channel *
                    133: channel_lookup(int id)
                    134: {
                    135:        Channel *c;
                    136:        if (id < 0 && id > channels_alloc) {
                    137:                log("channel_lookup: %d: bad id", id);
                    138:                return NULL;
                    139:        }
                    140:        c = &channels[id];
                    141:        if (c->type == SSH_CHANNEL_FREE) {
                    142:                log("channel_lookup: %d: bad id: channel free", id);
                    143:                return NULL;
                    144:        }
                    145:        return c;
                    146: }
                    147:
1.27      markus    148: /*
                    149:  * Allocate a new channel object and set its type and socket. This will cause
                    150:  * remote_name to be freed.
                    151:  */
1.1       deraadt   152:
1.25      markus    153: int
1.41      markus    154: channel_new(char *ctype, int type, int rfd, int wfd, int efd,
                    155:     int window, int maxpack, int extended_usage, char *remote_name)
1.1       deraadt   156: {
1.25      markus    157:        int i, found;
                    158:        Channel *c;
1.1       deraadt   159:
1.25      markus    160:        /* Update the maximum file descriptor value. */
1.41      markus    161:        if (rfd > channel_max_fd_value)
                    162:                channel_max_fd_value = rfd;
                    163:        if (wfd > channel_max_fd_value)
                    164:                channel_max_fd_value = wfd;
                    165:        if (efd > channel_max_fd_value)
                    166:                channel_max_fd_value = efd;
1.27      markus    167:        /* XXX set close-on-exec -markus */
1.25      markus    168:
                    169:        /* Do initial allocation if this is the first call. */
                    170:        if (channels_alloc == 0) {
1.44      markus    171:                chan_init();
1.25      markus    172:                channels_alloc = 10;
                    173:                channels = xmalloc(channels_alloc * sizeof(Channel));
                    174:                for (i = 0; i < channels_alloc; i++)
                    175:                        channels[i].type = SSH_CHANNEL_FREE;
1.27      markus    176:                /*
                    177:                 * Kludge: arrange a call to channel_stop_listening if we
                    178:                 * terminate with fatal().
                    179:                 */
1.25      markus    180:                fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
                    181:        }
                    182:        /* Try to find a free slot where to put the new channel. */
                    183:        for (found = -1, i = 0; i < channels_alloc; i++)
                    184:                if (channels[i].type == SSH_CHANNEL_FREE) {
                    185:                        /* Found a free slot. */
                    186:                        found = i;
                    187:                        break;
                    188:                }
                    189:        if (found == -1) {
1.27      markus    190:                /* There are no free slots.  Take last+1 slot and expand the array.  */
1.25      markus    191:                found = channels_alloc;
                    192:                channels_alloc += 10;
                    193:                debug("channel: expanding %d", channels_alloc);
                    194:                channels = xrealloc(channels, channels_alloc * sizeof(Channel));
                    195:                for (i = found; i < channels_alloc; i++)
                    196:                        channels[i].type = SSH_CHANNEL_FREE;
                    197:        }
                    198:        /* Initialize and return new channel number. */
                    199:        c = &channels[found];
                    200:        buffer_init(&c->input);
                    201:        buffer_init(&c->output);
1.41      markus    202:        buffer_init(&c->extended);
1.25      markus    203:        chan_init_iostates(c);
                    204:        c->self = found;
                    205:        c->type = type;
1.41      markus    206:        c->ctype = ctype;
                    207:        c->local_window = window;
                    208:        c->local_window_max = window;
                    209:        c->local_consumed = 0;
                    210:        c->local_maxpacket = maxpack;
                    211:        c->remote_window = 0;
                    212:        c->remote_maxpacket = 0;
                    213:        c->rfd = rfd;
                    214:        c->wfd = wfd;
                    215:        c->sock = (rfd == wfd) ? rfd : -1;
                    216:        c->efd = efd;
                    217:        c->extended_usage = extended_usage;
1.25      markus    218:        c->remote_id = -1;
                    219:        c->remote_name = remote_name;
1.41      markus    220:        c->remote_window = 0;
                    221:        c->remote_maxpacket = 0;
                    222:        c->cb_fn = NULL;
                    223:        c->cb_arg = NULL;
                    224:        c->cb_event = 0;
                    225:        c->dettach_user = NULL;
1.25      markus    226:        debug("channel %d: new [%s]", found, remote_name);
                    227:        return found;
1.1       deraadt   228: }
1.41      markus    229: int
                    230: channel_allocate(int type, int sock, char *remote_name)
                    231: {
                    232:        return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name);
                    233: }
1.1       deraadt   234:
                    235: /* Free the channel and close its socket. */
                    236:
1.25      markus    237: void
1.41      markus    238: channel_free(int id)
1.1       deraadt   239: {
1.41      markus    240:        Channel *c = channel_lookup(id);
                    241:        if (c == NULL)
                    242:                packet_disconnect("channel free: bad local channel %d", id);
                    243:        debug("channel_free: channel %d: status: %s", id, channel_open_message());
1.44      markus    244:        if (c->dettach_user != NULL) {
                    245:                debug("channel_free: channel %d: dettaching channel user", id);
                    246:                c->dettach_user(c->self, NULL);
                    247:        }
1.43      markus    248:        if (c->sock != -1) {
1.41      markus    249:                shutdown(c->sock, SHUT_RDWR);
1.42      markus    250:                close(c->sock);
1.44      markus    251:                c->sock = -1;
                    252:        }
                    253:        if (compat20) {
                    254:                if (c->rfd != -1) {
                    255:                        close(c->rfd);
                    256:                        c->rfd = -1;
                    257:                }
                    258:                if (c->wfd != -1) {
                    259:                        close(c->wfd);
                    260:                        c->wfd = -1;
                    261:                }
                    262:                if (c->efd != -1) {
                    263:                        close(c->efd);
                    264:                        c->efd = -1;
                    265:                }
1.42      markus    266:        }
1.41      markus    267:        buffer_free(&c->input);
                    268:        buffer_free(&c->output);
                    269:        buffer_free(&c->extended);
                    270:        c->type = SSH_CHANNEL_FREE;
                    271:        if (c->remote_name) {
                    272:                xfree(c->remote_name);
                    273:                c->remote_name = NULL;
1.25      markus    274:        }
1.1       deraadt   275: }
                    276:
1.27      markus    277: /*
1.41      markus    278:  * 'channel_pre*' are called just before select() to add any bits relevant to
                    279:  * channels in the select bitmasks.
                    280:  */
                    281: /*
                    282:  * 'channel_post*': perform any appropriate operations for channels which
                    283:  * have events pending.
1.27      markus    284:  */
1.41      markus    285: typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
                    286: chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
                    287: chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
                    288:
                    289: void
                    290: channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
                    291: {
                    292:        FD_SET(c->sock, readset);
                    293: }
                    294:
                    295: void
                    296: channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
                    297: {
                    298:        if (buffer_len(&c->input) < packet_get_maxsize())
                    299:                FD_SET(c->sock, readset);
                    300:        if (buffer_len(&c->output) > 0)
                    301:                FD_SET(c->sock, writeset);
                    302: }
1.1       deraadt   303:
1.41      markus    304: void
                    305: channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
                    306: {
                    307:        /* test whether sockets are 'alive' for read/write */
                    308:        if (c->istate == CHAN_INPUT_OPEN)
                    309:                if (buffer_len(&c->input) < packet_get_maxsize())
                    310:                        FD_SET(c->sock, readset);
                    311:        if (c->ostate == CHAN_OUTPUT_OPEN ||
                    312:            c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
                    313:                if (buffer_len(&c->output) > 0) {
                    314:                        FD_SET(c->sock, writeset);
                    315:                } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
                    316:                        chan_obuf_empty(c);
                    317:                }
                    318:        }
                    319: }
                    320:
                    321: void
1.44      markus    322: channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
                    323: {
                    324:        if (c->istate == CHAN_INPUT_OPEN &&
                    325:            c->remote_window > 0 &&
                    326:            buffer_len(&c->input) < c->remote_window)
                    327:                FD_SET(c->rfd, readset);
                    328:        if (c->ostate == CHAN_OUTPUT_OPEN ||
                    329:            c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
                    330:                if (buffer_len(&c->output) > 0) {
                    331:                        FD_SET(c->wfd, writeset);
                    332:                } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
                    333:                        chan_obuf_empty(c);
                    334:                }
                    335:        }
                    336:        /** XXX check close conditions, too */
                    337:        if (c->efd != -1) {
                    338:                if (c->extended_usage == CHAN_EXTENDED_WRITE &&
                    339:                    buffer_len(&c->extended) > 0)
                    340:                        FD_SET(c->efd, writeset);
                    341:                else if (c->extended_usage == CHAN_EXTENDED_READ &&
                    342:                    buffer_len(&c->extended) < c->remote_window)
                    343:                        FD_SET(c->efd, readset);
                    344:        }
                    345: }
                    346:
                    347: void
1.41      markus    348: channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
                    349: {
                    350:        if (buffer_len(&c->input) == 0) {
                    351:                packet_start(SSH_MSG_CHANNEL_CLOSE);
                    352:                packet_put_int(c->remote_id);
                    353:                packet_send();
                    354:                c->type = SSH_CHANNEL_CLOSED;
                    355:                debug("Closing channel %d after input drain.", c->self);
                    356:        }
                    357: }
                    358:
                    359: void
                    360: channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
                    361: {
                    362:        if (buffer_len(&c->output) == 0)
                    363:                channel_free(c->self);
                    364:        else
                    365:                FD_SET(c->sock, writeset);
                    366: }
                    367:
                    368: /*
                    369:  * This is a special state for X11 authentication spoofing.  An opened X11
                    370:  * connection (when authentication spoofing is being done) remains in this
                    371:  * state until the first packet has been completely read.  The authentication
                    372:  * data in that packet is then substituted by the real data if it matches the
                    373:  * fake data, and the channel is put into normal mode.
                    374:  */
                    375: int
                    376: x11_open_helper(Channel *c)
1.1       deraadt   377: {
1.25      markus    378:        unsigned char *ucp;
                    379:        unsigned int proto_len, data_len;
                    380:
1.41      markus    381:        /* Check if the fixed size part of the packet is in buffer. */
                    382:        if (buffer_len(&c->output) < 12)
                    383:                return 0;
                    384:
                    385:        /* Parse the lengths of variable-length fields. */
                    386:        ucp = (unsigned char *) buffer_ptr(&c->output);
                    387:        if (ucp[0] == 0x42) {   /* Byte order MSB first. */
                    388:                proto_len = 256 * ucp[6] + ucp[7];
                    389:                data_len = 256 * ucp[8] + ucp[9];
                    390:        } else if (ucp[0] == 0x6c) {    /* Byte order LSB first. */
                    391:                proto_len = ucp[6] + 256 * ucp[7];
                    392:                data_len = ucp[8] + 256 * ucp[9];
                    393:        } else {
                    394:                debug("Initial X11 packet contains bad byte order byte: 0x%x",
                    395:                      ucp[0]);
                    396:                return -1;
                    397:        }
                    398:
                    399:        /* Check if the whole packet is in buffer. */
                    400:        if (buffer_len(&c->output) <
                    401:            12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
                    402:                return 0;
                    403:
                    404:        /* Check if authentication protocol matches. */
                    405:        if (proto_len != strlen(x11_saved_proto) ||
                    406:            memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
                    407:                debug("X11 connection uses different authentication protocol.");
                    408:                return -1;
                    409:        }
                    410:        /* Check if authentication data matches our fake data. */
                    411:        if (data_len != x11_fake_data_len ||
                    412:            memcmp(ucp + 12 + ((proto_len + 3) & ~3),
                    413:                x11_fake_data, x11_fake_data_len) != 0) {
                    414:                debug("X11 auth data does not match fake data.");
                    415:                return -1;
                    416:        }
                    417:        /* Check fake data length */
                    418:        if (x11_fake_data_len != x11_saved_data_len) {
                    419:                error("X11 fake_data_len %d != saved_data_len %d",
                    420:                    x11_fake_data_len, x11_saved_data_len);
                    421:                return -1;
                    422:        }
                    423:        /*
                    424:         * Received authentication protocol and data match
                    425:         * our fake data. Substitute the fake data with real
                    426:         * data.
                    427:         */
                    428:        memcpy(ucp + 12 + ((proto_len + 3) & ~3),
                    429:            x11_saved_data, x11_saved_data_len);
                    430:        return 1;
                    431: }
                    432:
                    433: void
                    434: channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
                    435: {
                    436:        int ret = x11_open_helper(c);
                    437:        if (ret == 1) {
                    438:                /* Start normal processing for the channel. */
                    439:                c->type = SSH_CHANNEL_OPEN;
                    440:        } else if (ret == -1) {
                    441:                /*
                    442:                 * We have received an X11 connection that has bad
                    443:                 * authentication information.
                    444:                 */
                    445:                log("X11 connection rejected because of wrong authentication.\r\n");
                    446:                buffer_clear(&c->input);
                    447:                buffer_clear(&c->output);
                    448:                close(c->sock);
                    449:                c->sock = -1;
                    450:                c->type = SSH_CHANNEL_CLOSED;
                    451:                packet_start(SSH_MSG_CHANNEL_CLOSE);
                    452:                packet_put_int(c->remote_id);
                    453:                packet_send();
                    454:        }
                    455: }
1.25      markus    456:
1.41      markus    457: void
                    458: channel_pre_x11_open_15(Channel *c, fd_set * readset, fd_set * writeset)
                    459: {
                    460:        int ret = x11_open_helper(c);
                    461:        if (ret == 1) {
                    462:                c->type = SSH_CHANNEL_OPEN;
                    463:        } else if (ret == -1) {
                    464:                debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
                    465:                chan_read_failed(c);
                    466:                chan_write_failed(c);
                    467:                debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
                    468:        }
                    469: }
1.25      markus    470:
1.41      markus    471: /* This is our fake X11 server socket. */
                    472: void
                    473: channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
                    474: {
                    475:        struct sockaddr addr;
                    476:        int newsock, newch;
                    477:        socklen_t addrlen;
                    478:        char buf[16384], *remote_hostname;
1.25      markus    479:
1.41      markus    480:        if (FD_ISSET(c->sock, readset)) {
                    481:                debug("X11 connection requested.");
                    482:                addrlen = sizeof(addr);
                    483:                newsock = accept(c->sock, &addr, &addrlen);
                    484:                if (newsock < 0) {
                    485:                        error("accept: %.100s", strerror(errno));
                    486:                        return;
                    487:                }
                    488:                remote_hostname = get_remote_hostname(newsock);
                    489:                snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
                    490:                remote_hostname, get_peer_port(newsock));
                    491:                xfree(remote_hostname);
                    492:                newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
                    493:                                         xstrdup(buf));
                    494:                packet_start(SSH_SMSG_X11_OPEN);
                    495:                packet_put_int(newch);
                    496:                if (have_hostname_in_open)
                    497:                        packet_put_string(buf, strlen(buf));
                    498:                packet_send();
                    499:        }
                    500: }
1.25      markus    501:
1.41      markus    502: /*
                    503:  * This socket is listening for connections to a forwarded TCP/IP port.
                    504:  */
                    505: void
                    506: channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
                    507: {
                    508:        struct sockaddr addr;
                    509:        int newsock, newch;
                    510:        socklen_t addrlen;
                    511:        char buf[1024], *remote_hostname;
                    512:        int remote_port;
1.25      markus    513:
1.41      markus    514:        if (FD_ISSET(c->sock, readset)) {
                    515:                debug("Connection to port %d forwarding "
                    516:                    "to %.100s port %d requested.",
                    517:                    c->listening_port, c->path, c->host_port);
                    518:                addrlen = sizeof(addr);
                    519:                newsock = accept(c->sock, &addr, &addrlen);
                    520:                if (newsock < 0) {
                    521:                        error("accept: %.100s", strerror(errno));
                    522:                        return;
                    523:                }
                    524:                remote_hostname = get_remote_hostname(newsock);
                    525:                remote_port = get_peer_port(newsock);
                    526:                snprintf(buf, sizeof buf,
                    527:                    "listen port %d for %.100s port %d, "
                    528:                    "connect from %.200s port %d",
                    529:                    c->listening_port, c->path, c->host_port,
                    530:                    remote_hostname, remote_port);
                    531:                newch = channel_new("direct-tcpip",
                    532:                    SSH_CHANNEL_OPENING, newsock, newsock, -1,
                    533:                    c->local_window_max, c->local_maxpacket,
                    534:                    0, xstrdup(buf));
1.44      markus    535:                if (compat20) {
                    536:                        packet_start(SSH2_MSG_CHANNEL_OPEN);
                    537:                        packet_put_cstring("direct-tcpip");
                    538:                        packet_put_int(newch);
                    539:                        packet_put_int(c->local_window_max);
                    540:                        packet_put_int(c->local_maxpacket);
                    541:                        packet_put_string(c->path, strlen(c->path));
                    542:                        packet_put_int(c->host_port);
                    543:                        packet_put_cstring(remote_hostname);
                    544:                        packet_put_int(remote_port);
                    545:                        packet_send();
                    546:                } else {
                    547:                        packet_start(SSH_MSG_PORT_OPEN);
                    548:                        packet_put_int(newch);
                    549:                        packet_put_string(c->path, strlen(c->path));
                    550:                        packet_put_int(c->host_port);
                    551:                        if (have_hostname_in_open) {
                    552:                                packet_put_string(buf, strlen(buf));
                    553:                        }
                    554:                        packet_send();
1.41      markus    555:                }
                    556:                xfree(remote_hostname);
                    557:        }
                    558: }
1.25      markus    559:
1.41      markus    560: /*
                    561:  * This is the authentication agent socket listening for connections from
                    562:  * clients.
                    563:  */
                    564: void
                    565: channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
                    566: {
                    567:        struct sockaddr addr;
                    568:        int newsock, newch;
                    569:        socklen_t addrlen;
1.25      markus    570:
1.41      markus    571:        if (FD_ISSET(c->sock, readset)) {
                    572:                addrlen = sizeof(addr);
                    573:                newsock = accept(c->sock, &addr, &addrlen);
                    574:                if (newsock < 0) {
                    575:                        error("accept from auth socket: %.100s", strerror(errno));
                    576:                        return;
                    577:                }
                    578:                newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
                    579:                    xstrdup("accepted auth socket"));
                    580:                packet_start(SSH_SMSG_AGENT_OPEN);
                    581:                packet_put_int(newch);
                    582:                packet_send();
                    583:        }
                    584: }
1.25      markus    585:
1.41      markus    586: int
                    587: channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
                    588: {
                    589:        char buf[16*1024];
                    590:        int len;
1.25      markus    591:
1.41      markus    592:        if (c->rfd != -1 &&
                    593:            FD_ISSET(c->rfd, readset)) {
                    594:                len = read(c->rfd, buf, sizeof(buf));
                    595:                if (len <= 0) {
                    596:                        debug("channel %d: read<0 rfd %d len %d",
                    597:                            c->self, c->rfd, len);
1.25      markus    598:                        if (compat13) {
1.41      markus    599:                                buffer_consume(&c->output, buffer_len(&c->output));
                    600:                                c->type = SSH_CHANNEL_INPUT_DRAINING;
                    601:                                debug("Channel %d status set to input draining.", c->self);
1.25      markus    602:                        } else {
1.41      markus    603:                                chan_read_failed(c);
1.25      markus    604:                        }
1.41      markus    605:                        return -1;
                    606:                }
                    607:                buffer_append(&c->input, buf, len);
                    608:        }
                    609:        return 1;
                    610: }
                    611: int
                    612: channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
                    613: {
                    614:        int len;
1.25      markus    615:
1.41      markus    616:        /* Send buffered output data to the socket. */
                    617:        if (c->wfd != -1 &&
                    618:            FD_ISSET(c->wfd, writeset) &&
                    619:            buffer_len(&c->output) > 0) {
                    620:                len = write(c->wfd, buffer_ptr(&c->output),
                    621:                            buffer_len(&c->output));
                    622:                if (len <= 0) {
                    623:                        if (compat13) {
                    624:                                buffer_consume(&c->output, buffer_len(&c->output));
                    625:                                debug("Channel %d status set to input draining.", c->self);
                    626:                                c->type = SSH_CHANNEL_INPUT_DRAINING;
                    627:                        } else {
                    628:                                chan_write_failed(c);
                    629:                        }
                    630:                        return -1;
1.25      markus    631:                }
1.41      markus    632:                buffer_consume(&c->output, len);
1.44      markus    633:                if (compat20 && len > 0) {
                    634:                        c->local_consumed += len;
                    635:                }
                    636:        }
                    637:        return 1;
                    638: }
                    639: int
                    640: channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
                    641: {
                    642:        char buf[16*1024];
                    643:        int len;
                    644:
1.45      markus    645: /** XXX handle drain efd, too */
1.44      markus    646:        if (c->efd != -1) {
                    647:                if (c->extended_usage == CHAN_EXTENDED_WRITE &&
                    648:                    FD_ISSET(c->efd, writeset) &&
                    649:                    buffer_len(&c->extended) > 0) {
                    650:                        len = write(c->efd, buffer_ptr(&c->extended),
                    651:                            buffer_len(&c->extended));
                    652:                        debug("channel %d: written %d to efd %d",
                    653:                            c->self, len, c->efd);
                    654:                        if (len > 0) {
                    655:                                buffer_consume(&c->extended, len);
                    656:                                c->local_consumed += len;
                    657:                        }
                    658:                } else if (c->extended_usage == CHAN_EXTENDED_READ &&
                    659:                    FD_ISSET(c->efd, readset)) {
                    660:                        len = read(c->efd, buf, sizeof(buf));
                    661:                        debug("channel %d: read %d from efd %d",
                    662:                             c->self, len, c->efd);
1.45      markus    663:                        if (len == 0) {
                    664:                                debug("channel %d: closing efd %d",
                    665:                                    c->self, c->efd);
                    666:                                close(c->efd);
                    667:                                c->efd = -1;
                    668:                        } else if (len > 0)
1.44      markus    669:                                buffer_append(&c->extended, buf, len);
                    670:                }
                    671:        }
                    672:        return 1;
                    673: }
                    674: int
                    675: channel_check_window(Channel *c, fd_set * readset, fd_set * writeset)
                    676: {
1.46    ! markus    677:        if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
1.44      markus    678:            c->local_window < c->local_window_max/2 &&
                    679:            c->local_consumed > 0) {
                    680:                packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
                    681:                packet_put_int(c->remote_id);
                    682:                packet_put_int(c->local_consumed);
                    683:                packet_send();
                    684:                debug("channel %d: window %d sent adjust %d",
                    685:                    c->self, c->local_window,
                    686:                    c->local_consumed);
                    687:                c->local_window += c->local_consumed;
                    688:                c->local_consumed = 0;
1.1       deraadt   689:        }
1.41      markus    690:        return 1;
1.1       deraadt   691: }
                    692:
1.41      markus    693: void
                    694: channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
                    695: {
                    696:        channel_handle_rfd(c, readset, writeset);
                    697:        channel_handle_wfd(c, readset, writeset);
                    698: }
1.1       deraadt   699:
1.41      markus    700: void
1.44      markus    701: channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
                    702: {
                    703:        channel_handle_rfd(c, readset, writeset);
                    704:        channel_handle_wfd(c, readset, writeset);
                    705:        channel_handle_efd(c, readset, writeset);
                    706:        channel_check_window(c, readset, writeset);
                    707: }
                    708:
                    709: void
1.41      markus    710: channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1.1       deraadt   711: {
1.41      markus    712:        int len;
                    713:        /* Send buffered output data to the socket. */
                    714:        if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
                    715:                len = write(c->sock, buffer_ptr(&c->output),
                    716:                            buffer_len(&c->output));
                    717:                if (len <= 0)
                    718:                        buffer_consume(&c->output, buffer_len(&c->output));
                    719:                else
                    720:                        buffer_consume(&c->output, len);
                    721:        }
                    722: }
1.25      markus    723:
1.41      markus    724: void
1.44      markus    725: channel_handler_init_20(void)
                    726: {
                    727:        channel_pre[SSH_CHANNEL_OPEN] =                 &channel_pre_open_20;
                    728:        channel_pre[SSH_CHANNEL_PORT_LISTENER] =        &channel_pre_listener;
                    729:
                    730:        channel_post[SSH_CHANNEL_OPEN] =                &channel_post_open_2;
                    731:        channel_post[SSH_CHANNEL_PORT_LISTENER] =       &channel_post_port_listener;
                    732: }
                    733:
                    734: void
1.41      markus    735: channel_handler_init_13(void)
                    736: {
                    737:        channel_pre[SSH_CHANNEL_OPEN] =                 &channel_pre_open_13;
                    738:        channel_pre[SSH_CHANNEL_X11_OPEN] =             &channel_pre_x11_open_13;
                    739:        channel_pre[SSH_CHANNEL_X11_LISTENER] =         &channel_pre_listener;
                    740:        channel_pre[SSH_CHANNEL_PORT_LISTENER] =        &channel_pre_listener;
                    741:        channel_pre[SSH_CHANNEL_AUTH_SOCKET] =          &channel_pre_listener;
                    742:        channel_pre[SSH_CHANNEL_INPUT_DRAINING] =       &channel_pre_input_draining;
                    743:        channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] =      &channel_pre_output_draining;
1.25      markus    744:
1.41      markus    745:        channel_post[SSH_CHANNEL_OPEN] =                &channel_post_open_1;
                    746:        channel_post[SSH_CHANNEL_X11_LISTENER] =        &channel_post_x11_listener;
                    747:        channel_post[SSH_CHANNEL_PORT_LISTENER] =       &channel_post_port_listener;
                    748:        channel_post[SSH_CHANNEL_AUTH_SOCKET] =         &channel_post_auth_listener;
                    749:        channel_post[SSH_CHANNEL_OUTPUT_DRAINING] =     &channel_post_output_drain_13;
                    750: }
1.25      markus    751:
1.41      markus    752: void
                    753: channel_handler_init_15(void)
                    754: {
                    755:        channel_pre[SSH_CHANNEL_OPEN] =                 &channel_pre_open_15;
                    756:        channel_pre[SSH_CHANNEL_X11_OPEN] =             &channel_pre_x11_open_15;
                    757:        channel_pre[SSH_CHANNEL_X11_LISTENER] =         &channel_pre_listener;
                    758:        channel_pre[SSH_CHANNEL_PORT_LISTENER] =        &channel_pre_listener;
                    759:        channel_pre[SSH_CHANNEL_AUTH_SOCKET] =          &channel_pre_listener;
1.25      markus    760:
1.41      markus    761:        channel_post[SSH_CHANNEL_X11_LISTENER] =        &channel_post_x11_listener;
                    762:        channel_post[SSH_CHANNEL_PORT_LISTENER] =       &channel_post_port_listener;
                    763:        channel_post[SSH_CHANNEL_AUTH_SOCKET] =         &channel_post_auth_listener;
                    764:        channel_post[SSH_CHANNEL_OPEN] =                &channel_post_open_1;
                    765: }
1.27      markus    766:
1.41      markus    767: void
                    768: channel_handler_init(void)
                    769: {
                    770:        int i;
                    771:        for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
                    772:                channel_pre[i] = NULL;
                    773:                channel_post[i] = NULL;
                    774:        }
1.44      markus    775:        if (compat20)
                    776:                channel_handler_init_20();
                    777:        else if (compat13)
1.41      markus    778:                channel_handler_init_13();
                    779:        else
                    780:                channel_handler_init_15();
                    781: }
1.25      markus    782:
1.41      markus    783: void
                    784: channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
                    785: {
                    786:        static int did_init = 0;
                    787:        int i;
                    788:        Channel *c;
1.25      markus    789:
1.41      markus    790:        if (!did_init) {
                    791:                channel_handler_init();
                    792:                did_init = 1;
                    793:        }
                    794:        for (i = 0; i < channels_alloc; i++) {
                    795:                c = &channels[i];
                    796:                if (c->type == SSH_CHANNEL_FREE)
                    797:                        continue;
                    798:                if (ftab[c->type] == NULL)
1.25      markus    799:                        continue;
1.41      markus    800:                (*ftab[c->type])(c, readset, writeset);
1.44      markus    801:                chan_delete_if_full_closed(c);
1.1       deraadt   802:        }
                    803: }
                    804:
1.41      markus    805: void
                    806: channel_prepare_select(fd_set * readset, fd_set * writeset)
                    807: {
                    808:        channel_handler(channel_pre, readset, writeset);
                    809: }
                    810:
                    811: void
                    812: channel_after_select(fd_set * readset, fd_set * writeset)
                    813: {
                    814:        channel_handler(channel_post, readset, writeset);
                    815: }
                    816:
1.1       deraadt   817: /* If there is data to send to the connection, send some of it now. */
                    818:
1.25      markus    819: void
                    820: channel_output_poll()
1.1       deraadt   821: {
1.25      markus    822:        int len, i;
1.41      markus    823:        Channel *c;
1.1       deraadt   824:
1.25      markus    825:        for (i = 0; i < channels_alloc; i++) {
1.41      markus    826:                c = &channels[i];
1.37      markus    827:
1.27      markus    828:                /* We are only interested in channels that can have buffered incoming data. */
1.37      markus    829:                if (compat13) {
1.41      markus    830:                        if (c->type != SSH_CHANNEL_OPEN &&
                    831:                            c->type != SSH_CHANNEL_INPUT_DRAINING)
1.37      markus    832:                                continue;
                    833:                } else {
1.41      markus    834:                        if (c->type != SSH_CHANNEL_OPEN)
1.37      markus    835:                                continue;
1.41      markus    836:                        if (c->istate != CHAN_INPUT_OPEN &&
                    837:                            c->istate != CHAN_INPUT_WAIT_DRAIN)
1.37      markus    838:                                continue;
                    839:                }
1.46    ! markus    840:                if (compat20 &&
        !           841:                    (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
1.44      markus    842:                        debug("channel: %d: no data after CLOSE", c->self);
                    843:                        continue;
                    844:                }
1.25      markus    845:
                    846:                /* Get the amount of buffered data for this channel. */
1.41      markus    847:                len = buffer_len(&c->input);
1.25      markus    848:                if (len > 0) {
1.27      markus    849:                        /* Send some data for the other side over the secure connection. */
1.44      markus    850:                        if (compat20) {
                    851:                                if (len > c->remote_window)
                    852:                                        len = c->remote_window;
                    853:                                if (len > c->remote_maxpacket)
                    854:                                        len = c->remote_maxpacket;
1.25      markus    855:                        } else {
1.44      markus    856:                                if (packet_is_interactive()) {
                    857:                                        if (len > 1024)
                    858:                                                len = 512;
                    859:                                } else {
                    860:                                        /* Keep the packets at reasonable size. */
                    861:                                        if (len > packet_get_maxsize()/2)
                    862:                                                len = packet_get_maxsize()/2;
                    863:                                }
1.25      markus    864:                        }
1.41      markus    865:                        if (len > 0) {
1.44      markus    866:                                packet_start(compat20 ?
                    867:                                    SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
1.41      markus    868:                                packet_put_int(c->remote_id);
                    869:                                packet_put_string(buffer_ptr(&c->input), len);
                    870:                                packet_send();
                    871:                                buffer_consume(&c->input, len);
                    872:                                c->remote_window -= len;
1.44      markus    873:                                debug("channel %d: send data len %d", c->self, len);
1.41      markus    874:                        }
                    875:                } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1.25      markus    876:                        if (compat13)
                    877:                                fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
1.27      markus    878:                        /*
                    879:                         * input-buffer is empty and read-socket shutdown:
                    880:                         * tell peer, that we will not send more data: send IEOF
                    881:                         */
1.41      markus    882:                        chan_ibuf_empty(c);
1.25      markus    883:                }
1.44      markus    884:                /* Send extended data, i.e. stderr */
                    885:                if (compat20 &&
                    886:                    c->remote_window > 0 &&
                    887:                    (len = buffer_len(&c->extended)) > 0 &&
                    888:                    c->extended_usage == CHAN_EXTENDED_READ) {
                    889:                        if (len > c->remote_window)
                    890:                                len = c->remote_window;
                    891:                        if (len > c->remote_maxpacket)
                    892:                                len = c->remote_maxpacket;
                    893:                        packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
                    894:                        packet_put_int(c->remote_id);
                    895:                        packet_put_int(SSH2_EXTENDED_DATA_STDERR);
                    896:                        packet_put_string(buffer_ptr(&c->extended), len);
                    897:                        packet_send();
                    898:                        buffer_consume(&c->extended, len);
                    899:                        c->remote_window -= len;
                    900:                }
1.25      markus    901:        }
1.1       deraadt   902: }
                    903:
1.27      markus    904: /*
                    905:  * This is called when a packet of type CHANNEL_DATA has just been received.
                    906:  * The message type has already been consumed, but channel number and data is
                    907:  * still there.
                    908:  */
1.1       deraadt   909:
1.25      markus    910: void
1.41      markus    911: channel_input_data(int type, int plen)
1.1       deraadt   912: {
1.37      markus    913:        int id;
1.25      markus    914:        char *data;
                    915:        unsigned int data_len;
1.41      markus    916:        Channel *c;
1.25      markus    917:
                    918:        /* Get the channel number and verify it. */
1.37      markus    919:        id = packet_get_int();
1.41      markus    920:        c = channel_lookup(id);
                    921:        if (c == NULL)
1.37      markus    922:                packet_disconnect("Received data for nonexistent channel %d.", id);
1.25      markus    923:
                    924:        /* Ignore any data for non-open channels (might happen on close) */
1.41      markus    925:        if (c->type != SSH_CHANNEL_OPEN &&
                    926:            c->type != SSH_CHANNEL_X11_OPEN)
1.37      markus    927:                return;
                    928:
                    929:        /* same for protocol 1.5 if output end is no longer open */
1.41      markus    930:        if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
1.25      markus    931:                return;
                    932:
                    933:        /* Get the data. */
                    934:        data = packet_get_string(&data_len);
1.41      markus    935:
1.44      markus    936:        if (compat20){
                    937:                if (data_len > c->local_maxpacket) {
                    938:                        log("channel %d: rcvd big packet %d, maxpack %d",
                    939:                            c->self, data_len, c->local_maxpacket);
                    940:                }
                    941:                if (data_len > c->local_window) {
                    942:                        log("channel %d: rcvd too much data %d, win %d",
                    943:                            c->self, data_len, c->local_window);
                    944:                        xfree(data);
                    945:                        return;
                    946:                }
                    947:                c->local_window -= data_len;
                    948:        }else{
                    949:                packet_integrity_check(plen, 4 + 4 + data_len, type);
                    950:        }
1.41      markus    951:        buffer_append(&c->output, data, data_len);
1.25      markus    952:        xfree(data);
1.1       deraadt   953: }
1.44      markus    954: void
                    955: channel_input_extended_data(int type, int plen)
                    956: {
                    957:        int id;
                    958:        int tcode;
                    959:        char *data;
                    960:        unsigned int data_len;
                    961:        Channel *c;
                    962:
                    963:        /* Get the channel number and verify it. */
                    964:        id = packet_get_int();
                    965:        c = channel_lookup(id);
                    966:
                    967:        if (c == NULL)
                    968:                packet_disconnect("Received extended_data for bad channel %d.", id);
                    969:        if (c->type != SSH_CHANNEL_OPEN) {
                    970:                log("channel %d: ext data for non open", id);
                    971:                return;
                    972:        }
                    973:        tcode = packet_get_int();
                    974:        if (c->efd == -1 ||
                    975:            c->extended_usage != CHAN_EXTENDED_WRITE ||
                    976:            tcode != SSH2_EXTENDED_DATA_STDERR) {
                    977:                log("channel %d: bad ext data", c->self);
                    978:                return;
                    979:        }
                    980:        data = packet_get_string(&data_len);
                    981:        if (data_len > c->local_window) {
                    982:                log("channel %d: rcvd too much extended_data %d, win %d",
                    983:                    c->self, data_len, c->local_window);
                    984:                xfree(data);
                    985:                return;
                    986:        }
                    987:        debug("channel %d: rcvd ext data %d", c->self, data_len);
                    988:        c->local_window -= data_len;
                    989:        buffer_append(&c->extended, data, data_len);
                    990:        xfree(data);
                    991: }
                    992:
1.1       deraadt   993:
1.27      markus    994: /*
                    995:  * Returns true if no channel has too much buffered data, and false if one or
                    996:  * more channel is overfull.
                    997:  */
1.1       deraadt   998:
1.25      markus    999: int
                   1000: channel_not_very_much_buffered_data()
1.1       deraadt  1001: {
1.25      markus   1002:        unsigned int i;
1.41      markus   1003:        Channel *c;
1.25      markus   1004:
                   1005:        for (i = 0; i < channels_alloc; i++) {
1.41      markus   1006:                c = &channels[i];
                   1007:                if (c->type == SSH_CHANNEL_OPEN) {
1.44      markus   1008:                        if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
1.41      markus   1009:                                debug("channel %d: big input buffer %d",
                   1010:                                    c->self, buffer_len(&c->input));
1.25      markus   1011:                                return 0;
1.41      markus   1012:                        }
                   1013:                        if (buffer_len(&c->output) > packet_get_maxsize()) {
                   1014:                                debug("channel %d: big output buffer %d",
                   1015:                                    c->self, buffer_len(&c->output));
1.25      markus   1016:                                return 0;
1.41      markus   1017:                        }
1.25      markus   1018:                }
1.1       deraadt  1019:        }
1.25      markus   1020:        return 1;
1.1       deraadt  1021: }
                   1022:
1.41      markus   1023: void
                   1024: channel_input_ieof(int type, int plen)
                   1025: {
                   1026:        int id;
                   1027:        Channel *c;
                   1028:
                   1029:        packet_integrity_check(plen, 4, type);
                   1030:
                   1031:        id = packet_get_int();
                   1032:        c = channel_lookup(id);
                   1033:        if (c == NULL)
                   1034:                packet_disconnect("Received ieof for nonexistent channel %d.", id);
                   1035:        chan_rcvd_ieof(c);
                   1036: }
1.1       deraadt  1037:
1.25      markus   1038: void
1.41      markus   1039: channel_input_close(int type, int plen)
1.1       deraadt  1040: {
1.41      markus   1041:        int id;
                   1042:        Channel *c;
1.1       deraadt  1043:
1.41      markus   1044:        packet_integrity_check(plen, 4, type);
                   1045:
                   1046:        id = packet_get_int();
                   1047:        c = channel_lookup(id);
                   1048:        if (c == NULL)
                   1049:                packet_disconnect("Received close for nonexistent channel %d.", id);
1.27      markus   1050:
                   1051:        /*
                   1052:         * Send a confirmation that we have closed the channel and no more
                   1053:         * data is coming for it.
                   1054:         */
1.25      markus   1055:        packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
1.41      markus   1056:        packet_put_int(c->remote_id);
1.25      markus   1057:        packet_send();
                   1058:
1.27      markus   1059:        /*
                   1060:         * If the channel is in closed state, we have sent a close request,
                   1061:         * and the other side will eventually respond with a confirmation.
                   1062:         * Thus, we cannot free the channel here, because then there would be
                   1063:         * no-one to receive the confirmation.  The channel gets freed when
                   1064:         * the confirmation arrives.
                   1065:         */
1.41      markus   1066:        if (c->type != SSH_CHANNEL_CLOSED) {
1.27      markus   1067:                /*
                   1068:                 * Not a closed channel - mark it as draining, which will
                   1069:                 * cause it to be freed later.
                   1070:                 */
1.41      markus   1071:                buffer_consume(&c->input, buffer_len(&c->input));
                   1072:                c->type = SSH_CHANNEL_OUTPUT_DRAINING;
1.25      markus   1073:        }
1.1       deraadt  1074: }
                   1075:
1.41      markus   1076: /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
                   1077: void
                   1078: channel_input_oclose(int type, int plen)
                   1079: {
                   1080:        int id = packet_get_int();
                   1081:        Channel *c = channel_lookup(id);
                   1082:        packet_integrity_check(plen, 4, type);
                   1083:        if (c == NULL)
                   1084:                packet_disconnect("Received oclose for nonexistent channel %d.", id);
                   1085:        chan_rcvd_oclose(c);
                   1086: }
1.1       deraadt  1087:
1.25      markus   1088: void
1.41      markus   1089: channel_input_close_confirmation(int type, int plen)
1.1       deraadt  1090: {
1.41      markus   1091:        int id = packet_get_int();
                   1092:        Channel *c = channel_lookup(id);
1.1       deraadt  1093:
1.41      markus   1094:        if (c == NULL)
                   1095:                packet_disconnect("Received close confirmation for "
                   1096:                    "out-of-range channel %d.", id);
                   1097:        if (c->type != SSH_CHANNEL_CLOSED)
                   1098:                packet_disconnect("Received close confirmation for "
                   1099:                    "non-closed channel %d (type %d).", id, c->type);
                   1100:        channel_free(c->self);
1.1       deraadt  1101: }
                   1102:
1.25      markus   1103: void
1.41      markus   1104: channel_input_open_confirmation(int type, int plen)
1.1       deraadt  1105: {
1.41      markus   1106:        int id, remote_id;
                   1107:        Channel *c;
1.1       deraadt  1108:
1.44      markus   1109:        if (!compat20)
                   1110:                packet_integrity_check(plen, 4 + 4, type);
1.25      markus   1111:
1.41      markus   1112:        id = packet_get_int();
                   1113:        c = channel_lookup(id);
1.25      markus   1114:
1.41      markus   1115:        if (c==NULL || c->type != SSH_CHANNEL_OPENING)
                   1116:                packet_disconnect("Received open confirmation for "
                   1117:                    "non-opening channel %d.", id);
                   1118:        remote_id = packet_get_int();
1.27      markus   1119:        /* Record the remote channel number and mark that the channel is now open. */
1.41      markus   1120:        c->remote_id = remote_id;
                   1121:        c->type = SSH_CHANNEL_OPEN;
1.44      markus   1122:
                   1123:        if (compat20) {
                   1124:                c->remote_window = packet_get_int();
                   1125:                c->remote_maxpacket = packet_get_int();
                   1126:                if (c->cb_fn != NULL && c->cb_event == type) {
                   1127:                        debug("callback start");
                   1128:                        c->cb_fn(c->self, c->cb_arg);
                   1129:                        debug("callback done");
                   1130:                }
                   1131:                debug("channel %d: open confirm rwindow %d rmax %d", c->self,
                   1132:                    c->remote_window, c->remote_maxpacket);
                   1133:        }
1.1       deraadt  1134: }
                   1135:
1.25      markus   1136: void
1.41      markus   1137: channel_input_open_failure(int type, int plen)
1.1       deraadt  1138: {
1.41      markus   1139:        int id;
                   1140:        Channel *c;
                   1141:
1.44      markus   1142:        if (!compat20)
                   1143:                packet_integrity_check(plen, 4, type);
1.41      markus   1144:
                   1145:        id = packet_get_int();
                   1146:        c = channel_lookup(id);
1.25      markus   1147:
1.41      markus   1148:        if (c==NULL || c->type != SSH_CHANNEL_OPENING)
                   1149:                packet_disconnect("Received open failure for "
                   1150:                    "non-opening channel %d.", id);
1.44      markus   1151:        if (compat20) {
                   1152:                int reason = packet_get_int();
                   1153:                char *msg  = packet_get_string(NULL);
                   1154:                log("channel_open_failure: %d: reason %d: %s", id, reason, msg);
                   1155:                xfree(msg);
                   1156:        }
1.25      markus   1157:        /* Free the channel.  This will also close the socket. */
1.41      markus   1158:        channel_free(id);
1.1       deraadt  1159: }
                   1160:
1.44      markus   1161: void
                   1162: channel_input_channel_request(int type, int plen)
                   1163: {
                   1164:        int id;
                   1165:        Channel *c;
                   1166:
                   1167:        id = packet_get_int();
                   1168:        c = channel_lookup(id);
                   1169:
                   1170:        if (c == NULL ||
                   1171:            (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
                   1172:                packet_disconnect("Received request for "
                   1173:                    "non-open channel %d.", id);
                   1174:        if (c->cb_fn != NULL && c->cb_event == type) {
                   1175:                debug("callback start");
                   1176:                c->cb_fn(c->self, c->cb_arg);
                   1177:                debug("callback done");
                   1178:        } else {
                   1179:                char *service = packet_get_string(NULL);
                   1180:                debug("channel: %d rcvd request for %s", c->self, service);
                   1181: debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
                   1182:                xfree(service);
                   1183:        }
                   1184: }
                   1185:
                   1186: void
                   1187: channel_input_window_adjust(int type, int plen)
                   1188: {
                   1189:        Channel *c;
                   1190:        int id, adjust;
                   1191:
                   1192:        if (!compat20)
                   1193:                return;
                   1194:
                   1195:        /* Get the channel number and verify it. */
                   1196:        id = packet_get_int();
                   1197:        c = channel_lookup(id);
                   1198:
                   1199:        if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
                   1200:                log("Received window adjust for "
                   1201:                    "non-open channel %d.", id);
                   1202:                return;
                   1203:        }
                   1204:        adjust = packet_get_int();
                   1205:        debug("channel %d: rcvd adjust %d", id, adjust);
                   1206:        c->remote_window += adjust;
                   1207: }
                   1208:
1.27      markus   1209: /*
                   1210:  * Stops listening for channels, and removes any unix domain sockets that we
                   1211:  * might have.
                   1212:  */
1.1       deraadt  1213:
1.25      markus   1214: void
                   1215: channel_stop_listening()
1.1       deraadt  1216: {
1.25      markus   1217:        int i;
                   1218:        for (i = 0; i < channels_alloc; i++) {
                   1219:                switch (channels[i].type) {
                   1220:                case SSH_CHANNEL_AUTH_SOCKET:
                   1221:                        close(channels[i].sock);
                   1222:                        remove(channels[i].path);
                   1223:                        channel_free(i);
                   1224:                        break;
                   1225:                case SSH_CHANNEL_PORT_LISTENER:
                   1226:                case SSH_CHANNEL_X11_LISTENER:
                   1227:                        close(channels[i].sock);
                   1228:                        channel_free(i);
                   1229:                        break;
                   1230:                default:
                   1231:                        break;
                   1232:                }
1.1       deraadt  1233:        }
                   1234: }
                   1235:
1.27      markus   1236: /*
                   1237:  * Closes the sockets of all channels.  This is used to close extra file
                   1238:  * descriptors after a fork.
                   1239:  */
1.1       deraadt  1240:
1.25      markus   1241: void
                   1242: channel_close_all()
1.1       deraadt  1243: {
1.25      markus   1244:        int i;
                   1245:        for (i = 0; i < channels_alloc; i++) {
                   1246:                if (channels[i].type != SSH_CHANNEL_FREE)
                   1247:                        close(channels[i].sock);
                   1248:        }
1.1       deraadt  1249: }
                   1250:
                   1251: /* Returns the maximum file descriptor number used by the channels. */
                   1252:
1.25      markus   1253: int
                   1254: channel_max_fd()
1.1       deraadt  1255: {
1.25      markus   1256:        return channel_max_fd_value;
1.1       deraadt  1257: }
                   1258:
                   1259: /* Returns true if any channel is still open. */
                   1260:
1.25      markus   1261: int
                   1262: channel_still_open()
1.1       deraadt  1263: {
1.25      markus   1264:        unsigned int i;
                   1265:        for (i = 0; i < channels_alloc; i++)
                   1266:                switch (channels[i].type) {
                   1267:                case SSH_CHANNEL_FREE:
                   1268:                case SSH_CHANNEL_X11_LISTENER:
                   1269:                case SSH_CHANNEL_PORT_LISTENER:
                   1270:                case SSH_CHANNEL_CLOSED:
                   1271:                case SSH_CHANNEL_AUTH_SOCKET:
                   1272:                        continue;
1.44      markus   1273:                case SSH_CHANNEL_LARVAL:
                   1274:                        if (!compat20)
                   1275:                                fatal("cannot happen: SSH_CHANNEL_LARVAL");
                   1276:                        continue;
1.25      markus   1277:                case SSH_CHANNEL_OPENING:
                   1278:                case SSH_CHANNEL_OPEN:
                   1279:                case SSH_CHANNEL_X11_OPEN:
                   1280:                        return 1;
                   1281:                case SSH_CHANNEL_INPUT_DRAINING:
                   1282:                case SSH_CHANNEL_OUTPUT_DRAINING:
                   1283:                        if (!compat13)
                   1284:                                fatal("cannot happen: OUT_DRAIN");
                   1285:                        return 1;
                   1286:                default:
                   1287:                        fatal("channel_still_open: bad channel type %d", channels[i].type);
                   1288:                        /* NOTREACHED */
                   1289:                }
                   1290:        return 0;
1.1       deraadt  1291: }
                   1292:
1.27      markus   1293: /*
                   1294:  * Returns a message describing the currently open forwarded connections,
                   1295:  * suitable for sending to the client.  The message contains crlf pairs for
                   1296:  * newlines.
                   1297:  */
1.1       deraadt  1298:
1.25      markus   1299: char *
                   1300: channel_open_message()
1.1       deraadt  1301: {
1.25      markus   1302:        Buffer buffer;
                   1303:        int i;
                   1304:        char buf[512], *cp;
                   1305:
                   1306:        buffer_init(&buffer);
                   1307:        snprintf(buf, sizeof buf, "The following connections are open:\r\n");
1.1       deraadt  1308:        buffer_append(&buffer, buf, strlen(buf));
1.25      markus   1309:        for (i = 0; i < channels_alloc; i++) {
                   1310:                Channel *c = &channels[i];
                   1311:                switch (c->type) {
                   1312:                case SSH_CHANNEL_FREE:
                   1313:                case SSH_CHANNEL_X11_LISTENER:
                   1314:                case SSH_CHANNEL_PORT_LISTENER:
                   1315:                case SSH_CHANNEL_CLOSED:
                   1316:                case SSH_CHANNEL_AUTH_SOCKET:
                   1317:                        continue;
1.44      markus   1318:                case SSH_CHANNEL_LARVAL:
1.25      markus   1319:                case SSH_CHANNEL_OPENING:
                   1320:                case SSH_CHANNEL_OPEN:
                   1321:                case SSH_CHANNEL_X11_OPEN:
                   1322:                case SSH_CHANNEL_INPUT_DRAINING:
                   1323:                case SSH_CHANNEL_OUTPUT_DRAINING:
1.41      markus   1324:                        snprintf(buf, sizeof buf, "  #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
1.37      markus   1325:                            c->self, c->remote_name,
                   1326:                            c->type, c->remote_id,
                   1327:                            c->istate, buffer_len(&c->input),
1.41      markus   1328:                            c->ostate, buffer_len(&c->output),
                   1329:                            c->rfd, c->wfd);
1.25      markus   1330:                        buffer_append(&buffer, buf, strlen(buf));
                   1331:                        continue;
                   1332:                default:
1.41      markus   1333:                        fatal("channel_open_message: bad channel type %d", c->type);
1.25      markus   1334:                        /* NOTREACHED */
                   1335:                }
                   1336:        }
                   1337:        buffer_append(&buffer, "\0", 1);
                   1338:        cp = xstrdup(buffer_ptr(&buffer));
                   1339:        buffer_free(&buffer);
                   1340:        return cp;
1.1       deraadt  1341: }
                   1342:
1.27      markus   1343: /*
                   1344:  * Initiate forwarding of connections to local port "port" through the secure
                   1345:  * channel to host:port from remote side.
                   1346:  */
1.1       deraadt  1347:
1.25      markus   1348: void
1.31      markus   1349: channel_request_local_forwarding(u_short port, const char *host,
1.33      markus   1350:                                 u_short host_port, int gateway_ports)
1.25      markus   1351: {
1.35      markus   1352:        int success, ch, sock, on = 1;
                   1353:        struct addrinfo hints, *ai, *aitop;
                   1354:        char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.28      markus   1355:        struct linger linger;
1.25      markus   1356:
                   1357:        if (strlen(host) > sizeof(channels[0].path) - 1)
                   1358:                packet_disconnect("Forward host name too long.");
                   1359:
1.28      markus   1360:        /*
1.35      markus   1361:         * getaddrinfo returns a loopback address if the hostname is
                   1362:         * set to NULL and hints.ai_flags is not AI_PASSIVE
1.28      markus   1363:         */
1.35      markus   1364:        memset(&hints, 0, sizeof(hints));
                   1365:        hints.ai_family = IPv4or6;
                   1366:        hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
                   1367:        hints.ai_socktype = SOCK_STREAM;
                   1368:        snprintf(strport, sizeof strport, "%d", port);
                   1369:        if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
                   1370:                packet_disconnect("getaddrinfo: fatal error");
                   1371:
                   1372:        success = 0;
                   1373:        for (ai = aitop; ai; ai = ai->ai_next) {
                   1374:                if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
                   1375:                        continue;
                   1376:                if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
                   1377:                    strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
                   1378:                        error("channel_request_local_forwarding: getnameinfo failed");
                   1379:                        continue;
                   1380:                }
                   1381:                /* Create a port to listen for the host. */
                   1382:                sock = socket(ai->ai_family, SOCK_STREAM, 0);
                   1383:                if (sock < 0) {
                   1384:                        /* this is no error since kernel may not support ipv6 */
                   1385:                        verbose("socket: %.100s", strerror(errno));
                   1386:                        continue;
                   1387:                }
                   1388:                /*
                   1389:                 * Set socket options.  We would like the socket to disappear
                   1390:                 * as soon as it has been closed for whatever reason.
                   1391:                 */
                   1392:                setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
                   1393:                linger.l_onoff = 1;
                   1394:                linger.l_linger = 5;
                   1395:                setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
                   1396:                debug("Local forwarding listening on %s port %s.", ntop, strport);
                   1397:
                   1398:                /* Bind the socket to the address. */
                   1399:                if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
                   1400:                        /* address can be in use ipv6 address is already bound */
                   1401:                        verbose("bind: %.100s", strerror(errno));
                   1402:                        close(sock);
                   1403:                        continue;
                   1404:                }
                   1405:                /* Start listening for connections on the socket. */
                   1406:                if (listen(sock, 5) < 0) {
                   1407:                        error("listen: %.100s", strerror(errno));
                   1408:                        close(sock);
                   1409:                        continue;
                   1410:                }
                   1411:                /* Allocate a channel number for the socket. */
1.41      markus   1412:                ch = channel_new(
                   1413:                    "port listener", SSH_CHANNEL_PORT_LISTENER,
                   1414:                    sock, sock, -1,
                   1415:                    CHAN_WINDOW_DEFAULT, CHAN_PACKET_DEFAULT,
                   1416:                    0, xstrdup("port listener"));
1.35      markus   1417:                strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
                   1418:                channels[ch].host_port = host_port;
                   1419:                channels[ch].listening_port = port;
                   1420:                success = 1;
                   1421:        }
                   1422:        if (success == 0)
                   1423:                packet_disconnect("cannot listen port: %d", port);
                   1424:        freeaddrinfo(aitop);
1.25      markus   1425: }
1.1       deraadt  1426:
1.27      markus   1427: /*
                   1428:  * Initiate forwarding of connections to port "port" on remote host through
                   1429:  * the secure channel to host:port from local side.
                   1430:  */
1.1       deraadt  1431:
1.25      markus   1432: void
1.41      markus   1433: channel_request_remote_forwarding(u_short listen_port, const char *host_to_connect,
                   1434:                                  u_short port_to_connect)
1.25      markus   1435: {
                   1436:        int payload_len;
                   1437:        /* Record locally that connection to this host/port is permitted. */
                   1438:        if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
                   1439:                fatal("channel_request_remote_forwarding: too many forwards");
                   1440:
1.41      markus   1441:        permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
                   1442:        permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
                   1443:        permitted_opens[num_permitted_opens].listen_port = listen_port;
1.25      markus   1444:        num_permitted_opens++;
                   1445:
                   1446:        /* Send the forward request to the remote side. */
1.44      markus   1447:        if (compat20) {
                   1448:                const char *address_to_bind = "0.0.0.0";
                   1449:                packet_start(SSH2_MSG_GLOBAL_REQUEST);
                   1450:                packet_put_cstring("tcpip-forward");
                   1451:                packet_put_char(0);                     /* boolean: want reply */
                   1452:                packet_put_cstring(address_to_bind);
                   1453:                packet_put_int(listen_port);
                   1454:        } else {
                   1455:                packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
                   1456:                packet_put_int(port_to_connect);
                   1457:                packet_put_cstring(host_to_connect);
                   1458:                packet_put_int(listen_port);
                   1459:                packet_send();
                   1460:                packet_write_wait();
                   1461:                /*
                   1462:                 * Wait for response from the remote side.  It will send a disconnect
                   1463:                 * message on failure, and we will never see it here.
                   1464:                 */
                   1465:                packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
                   1466:        }
1.1       deraadt  1467: }
                   1468:
1.27      markus   1469: /*
                   1470:  * This is called after receiving CHANNEL_FORWARDING_REQUEST.  This initates
                   1471:  * listening for the port, and sends back a success reply (or disconnect
                   1472:  * message if there was an error).  This never returns if there was an error.
                   1473:  */
1.1       deraadt  1474:
1.25      markus   1475: void
                   1476: channel_input_port_forward_request(int is_root)
1.1       deraadt  1477: {
1.31      markus   1478:        u_short port, host_port;
1.25      markus   1479:        char *hostname;
1.1       deraadt  1480:
1.25      markus   1481:        /* Get arguments from the packet. */
                   1482:        port = packet_get_int();
                   1483:        hostname = packet_get_string(NULL);
                   1484:        host_port = packet_get_int();
                   1485:
1.27      markus   1486:        /*
                   1487:         * Check that an unprivileged user is not trying to forward a
                   1488:         * privileged port.
                   1489:         */
1.25      markus   1490:        if (port < IPPORT_RESERVED && !is_root)
                   1491:                packet_disconnect("Requested forwarding of port %d but user is not root.",
                   1492:                                  port);
1.33      markus   1493:        /*
                   1494:         * Initiate forwarding,
                   1495:         * bind port to localhost only (gateway ports == 0).
                   1496:         */
                   1497:        channel_request_local_forwarding(port, hostname, host_port, 0);
1.25      markus   1498:
                   1499:        /* Free the argument string. */
                   1500:        xfree(hostname);
1.1       deraadt  1501: }
                   1502:
1.41      markus   1503: /* XXX move to aux.c */
                   1504: int
                   1505: channel_connect_to(const char *host, u_short host_port)
                   1506: {
                   1507:        struct addrinfo hints, *ai, *aitop;
                   1508:        char ntop[NI_MAXHOST], strport[NI_MAXSERV];
                   1509:        int gaierr;
                   1510:        int sock = -1;
                   1511:
                   1512:        memset(&hints, 0, sizeof(hints));
                   1513:        hints.ai_family = IPv4or6;
                   1514:        hints.ai_socktype = SOCK_STREAM;
                   1515:        snprintf(strport, sizeof strport, "%d", host_port);
                   1516:        if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
                   1517:                error("%.100s: unknown host (%s)", host, gai_strerror(gaierr));
                   1518:                return -1;
                   1519:        }
                   1520:        for (ai = aitop; ai; ai = ai->ai_next) {
                   1521:                if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
                   1522:                        continue;
                   1523:                if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
                   1524:                    strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
                   1525:                        error("channel_connect_to: getnameinfo failed");
                   1526:                        continue;
                   1527:                }
                   1528:                /* Create the socket. */
                   1529:                sock = socket(ai->ai_family, SOCK_STREAM, 0);
                   1530:                if (sock < 0) {
                   1531:                        error("socket: %.100s", strerror(errno));
                   1532:                        continue;
                   1533:                }
                   1534:                /* Connect to the host/port. */
                   1535:                if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
                   1536:                        error("connect %.100s port %s: %.100s", ntop, strport,
                   1537:                            strerror(errno));
                   1538:                        close(sock);
                   1539:                        continue;       /* fail -- try next */
                   1540:                }
                   1541:                break; /* success */
                   1542:
                   1543:        }
                   1544:        freeaddrinfo(aitop);
                   1545:        if (!ai) {
                   1546:                error("connect %.100s port %d: failed.", host, host_port);
                   1547:                return -1;
                   1548:        }
                   1549:        /* success */
                   1550:        return sock;
                   1551: }
1.27      markus   1552: /*
                   1553:  * This is called after receiving PORT_OPEN message.  This attempts to
                   1554:  * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
                   1555:  * or CHANNEL_OPEN_FAILURE.
                   1556:  */
1.1       deraadt  1557:
1.25      markus   1558: void
1.41      markus   1559: channel_input_port_open(int type, int plen)
1.1       deraadt  1560: {
1.31      markus   1561:        u_short host_port;
1.25      markus   1562:        char *host, *originator_string;
1.41      markus   1563:        int remote_channel, sock = -1, newch, i, denied;
1.39      markus   1564:        unsigned int host_len, originator_len;
1.25      markus   1565:
                   1566:        /* Get remote channel number. */
                   1567:        remote_channel = packet_get_int();
                   1568:
                   1569:        /* Get host name to connect to. */
                   1570:        host = packet_get_string(&host_len);
                   1571:
                   1572:        /* Get port to connect to. */
                   1573:        host_port = packet_get_int();
                   1574:
                   1575:        /* Get remote originator name. */
1.29      markus   1576:        if (have_hostname_in_open) {
1.25      markus   1577:                originator_string = packet_get_string(&originator_len);
1.29      markus   1578:                originator_len += 4;    /* size of packet_int */
                   1579:        } else {
1.25      markus   1580:                originator_string = xstrdup("unknown (remote did not supply name)");
1.29      markus   1581:                originator_len = 0;     /* no originator supplied */
                   1582:        }
1.25      markus   1583:
1.41      markus   1584:        packet_integrity_check(plen,
                   1585:            4 + 4 + host_len + 4 + originator_len, SSH_MSG_PORT_OPEN);
1.25      markus   1586:
                   1587:        /* Check if opening that port is permitted. */
1.41      markus   1588:        denied = 0;
1.25      markus   1589:        if (!all_opens_permitted) {
                   1590:                /* Go trough all permitted ports. */
                   1591:                for (i = 0; i < num_permitted_opens; i++)
1.41      markus   1592:                        if (permitted_opens[i].port_to_connect == host_port &&
                   1593:                            strcmp(permitted_opens[i].host_to_connect, host) == 0)
1.25      markus   1594:                                break;
                   1595:
                   1596:                /* Check if we found the requested port among those permitted. */
                   1597:                if (i >= num_permitted_opens) {
                   1598:                        /* The port is not permitted. */
                   1599:                        log("Received request to connect to %.100s:%d, but the request was denied.",
                   1600:                            host, host_port);
1.41      markus   1601:                        denied = 1;
1.25      markus   1602:                }
                   1603:        }
1.41      markus   1604:        sock = denied ? -1 : channel_connect_to(host, host_port);
                   1605:        if (sock > 0) {
                   1606:                /* Allocate a channel for this connection. */
                   1607:                newch = channel_allocate(SSH_CHANNEL_OPEN, sock, originator_string);
                   1608:                channels[newch].remote_id = remote_channel;
                   1609:
                   1610:                packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
                   1611:                packet_put_int(remote_channel);
                   1612:                packet_put_int(newch);
                   1613:                packet_send();
                   1614:        } else {
                   1615:                packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
                   1616:                packet_put_int(remote_channel);
                   1617:                packet_send();
1.25      markus   1618:        }
                   1619:        xfree(host);
1.1       deraadt  1620: }
                   1621:
1.27      markus   1622: /*
                   1623:  * Creates an internet domain socket for listening for X11 connections.
                   1624:  * Returns a suitable value for the DISPLAY variable, or NULL if an error
                   1625:  * occurs.
                   1626:  */
1.1       deraadt  1627:
1.35      markus   1628: #define        NUM_SOCKS       10
                   1629:
1.25      markus   1630: char *
1.33      markus   1631: x11_create_display_inet(int screen_number, int x11_display_offset)
1.1       deraadt  1632: {
1.31      markus   1633:        int display_number, sock;
                   1634:        u_short port;
1.35      markus   1635:        struct addrinfo hints, *ai, *aitop;
                   1636:        char strport[NI_MAXSERV];
                   1637:        int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
                   1638:        char display[512];
1.25      markus   1639:        char hostname[MAXHOSTNAMELEN];
                   1640:
1.33      markus   1641:        for (display_number = x11_display_offset;
1.25      markus   1642:             display_number < MAX_DISPLAYS;
                   1643:             display_number++) {
                   1644:                port = 6000 + display_number;
1.35      markus   1645:                memset(&hints, 0, sizeof(hints));
                   1646:                hints.ai_family = IPv4or6;
1.36      markus   1647:                hints.ai_flags = AI_PASSIVE;            /* XXX loopback only ? */
1.35      markus   1648:                hints.ai_socktype = SOCK_STREAM;
                   1649:                snprintf(strport, sizeof strport, "%d", port);
                   1650:                if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
                   1651:                        error("getaddrinfo: %.100s", gai_strerror(gaierr));
1.25      markus   1652:                        return NULL;
                   1653:                }
1.35      markus   1654:                for (ai = aitop; ai; ai = ai->ai_next) {
                   1655:                        if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
                   1656:                                continue;
                   1657:                        sock = socket(ai->ai_family, SOCK_STREAM, 0);
                   1658:                        if (sock < 0) {
                   1659:                                error("socket: %.100s", strerror(errno));
                   1660:                                return NULL;
                   1661:                        }
                   1662:                        if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
                   1663:                                debug("bind port %d: %.100s", port, strerror(errno));
                   1664:                                shutdown(sock, SHUT_RDWR);
                   1665:                                close(sock);
                   1666:                                for (n = 0; n < num_socks; n++) {
                   1667:                                        shutdown(socks[n], SHUT_RDWR);
                   1668:                                        close(socks[n]);
                   1669:                                }
                   1670:                                num_socks = 0;
                   1671:                                break;
                   1672:                        }
                   1673:                        socks[num_socks++] = sock;
                   1674:                        if (num_socks == NUM_SOCKS)
                   1675:                                break;
1.25      markus   1676:                }
1.35      markus   1677:                if (num_socks > 0)
                   1678:                        break;
1.25      markus   1679:        }
                   1680:        if (display_number >= MAX_DISPLAYS) {
                   1681:                error("Failed to allocate internet-domain X11 display socket.");
                   1682:                return NULL;
                   1683:        }
                   1684:        /* Start listening for connections on the socket. */
1.35      markus   1685:        for (n = 0; n < num_socks; n++) {
                   1686:                sock = socks[n];
                   1687:                if (listen(sock, 5) < 0) {
                   1688:                        error("listen: %.100s", strerror(errno));
                   1689:                        shutdown(sock, SHUT_RDWR);
                   1690:                        close(sock);
                   1691:                        return NULL;
                   1692:                }
1.25      markus   1693:        }
1.35      markus   1694:
1.25      markus   1695:        /* Set up a suitable value for the DISPLAY variable. */
                   1696:        if (gethostname(hostname, sizeof(hostname)) < 0)
                   1697:                fatal("gethostname: %.100s", strerror(errno));
1.35      markus   1698:        snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
1.25      markus   1699:                 display_number, screen_number);
                   1700:
1.35      markus   1701:        /* Allocate a channel for each socket. */
                   1702:        for (n = 0; n < num_socks; n++) {
                   1703:                sock = socks[n];
                   1704:                (void) channel_allocate(SSH_CHANNEL_X11_LISTENER, sock,
                   1705:                                        xstrdup("X11 inet listener"));
                   1706:        }
1.1       deraadt  1707:
1.25      markus   1708:        /* Return a suitable value for the DISPLAY environment variable. */
1.35      markus   1709:        return xstrdup(display);
1.1       deraadt  1710: }
                   1711:
                   1712: #ifndef X_UNIX_PATH
                   1713: #define X_UNIX_PATH "/tmp/.X11-unix/X"
                   1714: #endif
                   1715:
                   1716: static
                   1717: int
1.30      deraadt  1718: connect_local_xsocket(unsigned int dnr)
1.1       deraadt  1719: {
1.25      markus   1720:        static const char *const x_sockets[] = {
                   1721:                X_UNIX_PATH "%u",
                   1722:                "/var/X/.X11-unix/X" "%u",
                   1723:                "/usr/spool/sockets/X11/" "%u",
                   1724:                NULL
                   1725:        };
                   1726:        int sock;
                   1727:        struct sockaddr_un addr;
                   1728:        const char *const * path;
                   1729:
                   1730:        for (path = x_sockets; *path; ++path) {
                   1731:                sock = socket(AF_UNIX, SOCK_STREAM, 0);
                   1732:                if (sock < 0)
                   1733:                        error("socket: %.100s", strerror(errno));
                   1734:                memset(&addr, 0, sizeof(addr));
                   1735:                addr.sun_family = AF_UNIX;
                   1736:                snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
                   1737:                if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
                   1738:                        return sock;
                   1739:                close(sock);
                   1740:        }
                   1741:        error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
                   1742:        return -1;
1.1       deraadt  1743: }
                   1744:
                   1745:
1.27      markus   1746: /*
                   1747:  * This is called when SSH_SMSG_X11_OPEN is received.  The packet contains
                   1748:  * the remote channel number.  We should do whatever we want, and respond
                   1749:  * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
                   1750:  */
1.1       deraadt  1751:
1.25      markus   1752: void
1.41      markus   1753: x11_input_open(int type, int plen)
1.1       deraadt  1754: {
1.35      markus   1755:        int remote_channel, display_number, sock = 0, newch;
1.25      markus   1756:        const char *display;
                   1757:        char buf[1024], *cp, *remote_host;
1.39      markus   1758:        unsigned int remote_len;
1.35      markus   1759:        struct addrinfo hints, *ai, *aitop;
                   1760:        char strport[NI_MAXSERV];
                   1761:        int gaierr;
1.25      markus   1762:
                   1763:        /* Get remote channel number. */
                   1764:        remote_channel = packet_get_int();
                   1765:
                   1766:        /* Get remote originator name. */
1.29      markus   1767:        if (have_hostname_in_open) {
1.25      markus   1768:                remote_host = packet_get_string(&remote_len);
1.29      markus   1769:                remote_len += 4;
                   1770:        } else {
1.25      markus   1771:                remote_host = xstrdup("unknown (remote did not supply name)");
1.29      markus   1772:                remote_len = 0;
                   1773:        }
1.25      markus   1774:
                   1775:        debug("Received X11 open request.");
1.41      markus   1776:        packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
1.25      markus   1777:
                   1778:        /* Try to open a socket for the local X server. */
                   1779:        display = getenv("DISPLAY");
                   1780:        if (!display) {
                   1781:                error("DISPLAY not set.");
                   1782:                goto fail;
                   1783:        }
1.27      markus   1784:        /*
                   1785:         * Now we decode the value of the DISPLAY variable and make a
                   1786:         * connection to the real X server.
                   1787:         */
                   1788:
                   1789:        /*
                   1790:         * Check if it is a unix domain socket.  Unix domain displays are in
                   1791:         * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
                   1792:         */
1.25      markus   1793:        if (strncmp(display, "unix:", 5) == 0 ||
                   1794:            display[0] == ':') {
                   1795:                /* Connect to the unix domain socket. */
                   1796:                if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
                   1797:                        error("Could not parse display number from DISPLAY: %.100s",
                   1798:                              display);
                   1799:                        goto fail;
                   1800:                }
                   1801:                /* Create a socket. */
                   1802:                sock = connect_local_xsocket(display_number);
                   1803:                if (sock < 0)
                   1804:                        goto fail;
                   1805:
                   1806:                /* OK, we now have a connection to the display. */
                   1807:                goto success;
                   1808:        }
1.27      markus   1809:        /*
                   1810:         * Connect to an inet socket.  The DISPLAY value is supposedly
                   1811:         * hostname:d[.s], where hostname may also be numeric IP address.
                   1812:         */
1.25      markus   1813:        strncpy(buf, display, sizeof(buf));
                   1814:        buf[sizeof(buf) - 1] = 0;
                   1815:        cp = strchr(buf, ':');
                   1816:        if (!cp) {
                   1817:                error("Could not find ':' in DISPLAY: %.100s", display);
                   1818:                goto fail;
                   1819:        }
                   1820:        *cp = 0;
1.27      markus   1821:        /* buf now contains the host name.  But first we parse the display number. */
1.25      markus   1822:        if (sscanf(cp + 1, "%d", &display_number) != 1) {
                   1823:                error("Could not parse display number from DISPLAY: %.100s",
                   1824:                      display);
                   1825:                goto fail;
                   1826:        }
1.35      markus   1827:
                   1828:        /* Look up the host address */
                   1829:        memset(&hints, 0, sizeof(hints));
                   1830:        hints.ai_family = IPv4or6;
                   1831:        hints.ai_socktype = SOCK_STREAM;
                   1832:        snprintf(strport, sizeof strport, "%d", 6000 + display_number);
                   1833:        if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
                   1834:                error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
1.25      markus   1835:                goto fail;
                   1836:        }
1.35      markus   1837:        for (ai = aitop; ai; ai = ai->ai_next) {
                   1838:                /* Create a socket. */
                   1839:                sock = socket(ai->ai_family, SOCK_STREAM, 0);
                   1840:                if (sock < 0) {
                   1841:                        debug("socket: %.100s", strerror(errno));
1.41      markus   1842:                        continue;
                   1843:                }
                   1844:                /* Connect it to the display. */
                   1845:                if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
                   1846:                        debug("connect %.100s port %d: %.100s", buf,
                   1847:                            6000 + display_number, strerror(errno));
                   1848:                        close(sock);
                   1849:                        continue;
                   1850:                }
                   1851:                /* Success */
                   1852:                break;
1.35      markus   1853:        }
                   1854:        freeaddrinfo(aitop);
                   1855:        if (!ai) {
                   1856:                error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
                   1857:                    strerror(errno));
1.25      markus   1858:                goto fail;
                   1859:        }
                   1860: success:
                   1861:        /* We have successfully obtained a connection to the real X display. */
                   1862:
                   1863:        /* Allocate a channel for this connection. */
                   1864:        if (x11_saved_proto == NULL)
                   1865:                newch = channel_allocate(SSH_CHANNEL_OPEN, sock, remote_host);
                   1866:        else
                   1867:                newch = channel_allocate(SSH_CHANNEL_X11_OPEN, sock, remote_host);
                   1868:        channels[newch].remote_id = remote_channel;
                   1869:
                   1870:        /* Send a confirmation to the remote host. */
                   1871:        packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
                   1872:        packet_put_int(remote_channel);
                   1873:        packet_put_int(newch);
                   1874:        packet_send();
                   1875:
                   1876:        return;
                   1877:
                   1878: fail:
                   1879:        /* Send refusal to the remote host. */
                   1880:        packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
                   1881:        packet_put_int(remote_channel);
                   1882:        packet_send();
1.1       deraadt  1883: }
                   1884:
1.27      markus   1885: /*
                   1886:  * Requests forwarding of X11 connections, generates fake authentication
                   1887:  * data, and enables authentication spoofing.
                   1888:  */
1.1       deraadt  1889:
1.25      markus   1890: void
                   1891: x11_request_forwarding_with_spoofing(const char *proto, const char *data)
1.1       deraadt  1892: {
1.25      markus   1893:        unsigned int data_len = (unsigned int) strlen(data) / 2;
                   1894:        unsigned int i, value;
                   1895:        char *new_data;
                   1896:        int screen_number;
                   1897:        const char *cp;
                   1898:        u_int32_t rand = 0;
                   1899:
                   1900:        cp = getenv("DISPLAY");
                   1901:        if (cp)
                   1902:                cp = strchr(cp, ':');
                   1903:        if (cp)
                   1904:                cp = strchr(cp, '.');
                   1905:        if (cp)
                   1906:                screen_number = atoi(cp + 1);
                   1907:        else
                   1908:                screen_number = 0;
                   1909:
                   1910:        /* Save protocol name. */
                   1911:        x11_saved_proto = xstrdup(proto);
                   1912:
1.27      markus   1913:        /*
                   1914:         * Extract real authentication data and generate fake data of the
                   1915:         * same length.
                   1916:         */
1.25      markus   1917:        x11_saved_data = xmalloc(data_len);
                   1918:        x11_fake_data = xmalloc(data_len);
                   1919:        for (i = 0; i < data_len; i++) {
                   1920:                if (sscanf(data + 2 * i, "%2x", &value) != 1)
                   1921:                        fatal("x11_request_forwarding: bad authentication data: %.100s", data);
                   1922:                if (i % 4 == 0)
                   1923:                        rand = arc4random();
                   1924:                x11_saved_data[i] = value;
                   1925:                x11_fake_data[i] = rand & 0xff;
                   1926:                rand >>= 8;
                   1927:        }
                   1928:        x11_saved_data_len = data_len;
                   1929:        x11_fake_data_len = data_len;
                   1930:
                   1931:        /* Convert the fake data into hex. */
                   1932:        new_data = xmalloc(2 * data_len + 1);
                   1933:        for (i = 0; i < data_len; i++)
                   1934:                sprintf(new_data + 2 * i, "%02x", (unsigned char) x11_fake_data[i]);
                   1935:
                   1936:        /* Send the request packet. */
                   1937:        packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
                   1938:        packet_put_string(proto, strlen(proto));
                   1939:        packet_put_string(new_data, strlen(new_data));
                   1940:        packet_put_int(screen_number);
                   1941:        packet_send();
                   1942:        packet_write_wait();
                   1943:        xfree(new_data);
1.1       deraadt  1944: }
                   1945:
                   1946: /* Sends a message to the server to request authentication fd forwarding. */
                   1947:
1.25      markus   1948: void
                   1949: auth_request_forwarding()
1.1       deraadt  1950: {
1.25      markus   1951:        packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
                   1952:        packet_send();
                   1953:        packet_write_wait();
1.1       deraadt  1954: }
                   1955:
1.27      markus   1956: /*
                   1957:  * Returns the name of the forwarded authentication socket.  Returns NULL if
                   1958:  * there is no forwarded authentication socket.  The returned value points to
                   1959:  * a static buffer.
                   1960:  */
1.1       deraadt  1961:
1.25      markus   1962: char *
                   1963: auth_get_socket_name()
1.1       deraadt  1964: {
1.25      markus   1965:        return channel_forwarded_auth_socket_name;
1.1       deraadt  1966: }
                   1967:
1.12      markus   1968: /* removes the agent forwarding socket */
                   1969:
1.25      markus   1970: void
                   1971: cleanup_socket(void)
                   1972: {
                   1973:        remove(channel_forwarded_auth_socket_name);
                   1974:        rmdir(channel_forwarded_auth_socket_dir);
1.12      markus   1975: }
                   1976:
1.27      markus   1977: /*
                   1978:  * This if called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
                   1979:  * This starts forwarding authentication requests.
                   1980:  */
1.1       deraadt  1981:
1.25      markus   1982: void
                   1983: auth_input_request_forwarding(struct passwd * pw)
1.1       deraadt  1984: {
1.25      markus   1985:        int sock, newch;
                   1986:        struct sockaddr_un sunaddr;
                   1987:
                   1988:        if (auth_get_socket_name() != NULL)
                   1989:                fatal("Protocol error: authentication forwarding requested twice.");
                   1990:
                   1991:        /* Temporarily drop privileged uid for mkdir/bind. */
                   1992:        temporarily_use_uid(pw->pw_uid);
                   1993:
                   1994:        /* Allocate a buffer for the socket name, and format the name. */
                   1995:        channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
                   1996:        channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
                   1997:        strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
                   1998:
                   1999:        /* Create private directory for socket */
                   2000:        if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL)
                   2001:                packet_disconnect("mkdtemp: %.100s", strerror(errno));
                   2002:        snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
                   2003:                 channel_forwarded_auth_socket_dir, (int) getpid());
                   2004:
                   2005:        if (atexit(cleanup_socket) < 0) {
                   2006:                int saved = errno;
                   2007:                cleanup_socket();
                   2008:                packet_disconnect("socket: %.100s", strerror(saved));
                   2009:        }
                   2010:        /* Create the socket. */
                   2011:        sock = socket(AF_UNIX, SOCK_STREAM, 0);
                   2012:        if (sock < 0)
                   2013:                packet_disconnect("socket: %.100s", strerror(errno));
                   2014:
                   2015:        /* Bind it to the name. */
                   2016:        memset(&sunaddr, 0, sizeof(sunaddr));
                   2017:        sunaddr.sun_family = AF_UNIX;
                   2018:        strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
                   2019:                sizeof(sunaddr.sun_path));
                   2020:
                   2021:        if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
                   2022:                packet_disconnect("bind: %.100s", strerror(errno));
                   2023:
                   2024:        /* Restore the privileged uid. */
                   2025:        restore_uid();
                   2026:
                   2027:        /* Start listening on the socket. */
                   2028:        if (listen(sock, 5) < 0)
                   2029:                packet_disconnect("listen: %.100s", strerror(errno));
                   2030:
                   2031:        /* Allocate a channel for the authentication agent socket. */
                   2032:        newch = channel_allocate(SSH_CHANNEL_AUTH_SOCKET, sock,
                   2033:                                 xstrdup("auth socket"));
1.32      deraadt  2034:        strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
                   2035:            sizeof(channels[newch].path));
1.1       deraadt  2036: }
                   2037:
                   2038: /* This is called to process an SSH_SMSG_AGENT_OPEN message. */
                   2039:
1.25      markus   2040: void
1.41      markus   2041: auth_input_open_request(int type, int plen)
1.1       deraadt  2042: {
1.25      markus   2043:        int remch, sock, newch;
                   2044:        char *dummyname;
1.41      markus   2045:
                   2046:        packet_integrity_check(plen, 4, type);
1.25      markus   2047:
                   2048:        /* Read the remote channel number from the message. */
                   2049:        remch = packet_get_int();
                   2050:
1.27      markus   2051:        /*
                   2052:         * Get a connection to the local authentication agent (this may again
                   2053:         * get forwarded).
                   2054:         */
1.25      markus   2055:        sock = ssh_get_authentication_socket();
                   2056:
1.27      markus   2057:        /*
                   2058:         * If we could not connect the agent, send an error message back to
                   2059:         * the server. This should never happen unless the agent dies,
                   2060:         * because authentication forwarding is only enabled if we have an
                   2061:         * agent.
                   2062:         */
1.25      markus   2063:        if (sock < 0) {
                   2064:                packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
                   2065:                packet_put_int(remch);
                   2066:                packet_send();
                   2067:                return;
                   2068:        }
                   2069:        debug("Forwarding authentication connection.");
1.1       deraadt  2070:
1.27      markus   2071:        /*
                   2072:         * Dummy host name.  This will be freed when the channel is freed; it
                   2073:         * will still be valid in the packet_put_string below since the
                   2074:         * channel cannot yet be freed at that point.
                   2075:         */
1.25      markus   2076:        dummyname = xstrdup("authentication agent connection");
                   2077:
                   2078:        newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
                   2079:        channels[newch].remote_id = remch;
                   2080:
                   2081:        /* Send a confirmation to the remote host. */
                   2082:        packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
                   2083:        packet_put_int(remch);
                   2084:        packet_put_int(newch);
1.44      markus   2085:        packet_send();
                   2086: }
                   2087:
                   2088: void
                   2089: channel_open(int id)
                   2090: {
                   2091:        Channel *c = channel_lookup(id);
                   2092:        if (c == NULL) {
                   2093:                log("channel_open: %d: bad id", id);
                   2094:                return;
                   2095:        }
                   2096:        packet_start(SSH2_MSG_CHANNEL_OPEN);
                   2097:        packet_put_cstring(c->ctype);
                   2098:        packet_put_int(c->self);
                   2099:        packet_put_int(c->local_window);
                   2100:        packet_put_int(c->local_maxpacket);
                   2101:        packet_send();
                   2102:        debug("channel open %d", id);
                   2103: }
                   2104: void
                   2105: channel_request(int id, char *service, int wantconfirm)
                   2106: {
                   2107:        channel_request_start(id, service, wantconfirm);
                   2108:        packet_send();
                   2109:        debug("channel request %d: %s", id, service) ;
                   2110: }
                   2111: void
                   2112: channel_request_start(int id, char *service, int wantconfirm)
                   2113: {
                   2114:        Channel *c = channel_lookup(id);
                   2115:        if (c == NULL) {
                   2116:                log("channel_request: %d: bad id", id);
                   2117:                return;
                   2118:        }
                   2119:        packet_start(SSH2_MSG_CHANNEL_REQUEST);
                   2120:        packet_put_int(c->remote_id);
                   2121:        packet_put_cstring(service);
                   2122:        packet_put_char(wantconfirm);
                   2123: }
                   2124: void
                   2125: channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
                   2126: {
                   2127:        Channel *c = channel_lookup(id);
                   2128:        if (c == NULL) {
                   2129:                log("channel_register_callback: %d: bad id", id);
                   2130:                return;
                   2131:        }
                   2132:        c->cb_event = mtype;
                   2133:        c->cb_fn = fn;
                   2134:        c->cb_arg = arg;
                   2135: }
                   2136: void
                   2137: channel_register_cleanup(int id, channel_callback_fn *fn)
                   2138: {
                   2139:        Channel *c = channel_lookup(id);
                   2140:        if (c == NULL) {
                   2141:                log("channel_register_cleanup: %d: bad id", id);
                   2142:                return;
                   2143:        }
                   2144:        c->dettach_user = fn;
                   2145: }
                   2146: void
                   2147: channel_cancel_cleanup(int id)
                   2148: {
                   2149:        Channel *c = channel_lookup(id);
                   2150:        if (c == NULL) {
                   2151:                log("channel_cancel_cleanup: %d: bad id", id);
                   2152:                return;
                   2153:        }
                   2154:        c->dettach_user = NULL;
                   2155: }
                   2156:
                   2157: void
                   2158: channel_set_fds(int id, int rfd, int wfd, int efd, int extusage)
                   2159: {
                   2160:        Channel *c = channel_lookup(id);
                   2161:        if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
                   2162:                fatal("channel_activate for non-larval channel %d.", id);
                   2163:        if (rfd > channel_max_fd_value)
                   2164:                channel_max_fd_value = rfd;
                   2165:        if (wfd > channel_max_fd_value)
                   2166:                channel_max_fd_value = wfd;
                   2167:        if (efd > channel_max_fd_value)
                   2168:                channel_max_fd_value = efd;
                   2169:        c->type = SSH_CHANNEL_OPEN;
                   2170:        c->rfd = rfd;
                   2171:        c->wfd = wfd;
                   2172:        c->efd = efd;
                   2173:        c->extended_usage = extusage;
                   2174:        /* XXX window size? */
                   2175:        c->local_window = c->local_window_max = c->local_maxpacket/2;
                   2176:        packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
                   2177:        packet_put_int(c->remote_id);
                   2178:        packet_put_int(c->local_window);
1.25      markus   2179:        packet_send();
1.1       deraadt  2180: }