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

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:  *
                     16:  */
1.1       deraadt    17:
                     18: #include "includes.h"
1.35    ! markus     19: RCSID("$Id: channels.c,v 1.34 1999/12/27 09:48:38 markus Exp $");
1.1       deraadt    20:
                     21: #include "ssh.h"
                     22: #include "packet.h"
                     23: #include "xmalloc.h"
                     24: #include "buffer.h"
                     25: #include "authfd.h"
                     26: #include "uidswap.h"
1.20      markus     27: #include "readconf.h"
1.3       deraadt    28: #include "servconf.h"
1.1       deraadt    29:
1.14      markus     30: #include "channels.h"
                     31: #include "nchan.h"
                     32: #include "compat.h"
                     33:
1.1       deraadt    34: /* Maximum number of fake X11 displays to try. */
                     35: #define MAX_DISPLAYS  1000
                     36:
1.12      markus     37: /* Max len of agent socket */
                     38: #define MAX_SOCKET_NAME 100
                     39:
1.27      markus     40: /*
                     41:  * Pointer to an array containing all allocated channels.  The array is
                     42:  * dynamically extended as needed.
                     43:  */
1.1       deraadt    44: static Channel *channels = NULL;
                     45:
1.27      markus     46: /*
                     47:  * Size of the channel array.  All slots of the array must always be
                     48:  * initialized (at least the type field); unused slots are marked with type
                     49:  * SSH_CHANNEL_FREE.
                     50:  */
1.1       deraadt    51: static int channels_alloc = 0;
                     52:
1.27      markus     53: /*
                     54:  * Maximum file descriptor value used in any of the channels.  This is
                     55:  * updated in channel_allocate.
                     56:  */
1.1       deraadt    57: static int channel_max_fd_value = 0;
                     58:
1.12      markus     59: /* Name and directory of socket for authentication agent forwarding. */
1.1       deraadt    60: static char *channel_forwarded_auth_socket_name = NULL;
1.25      markus     61: static char *channel_forwarded_auth_socket_dir = NULL;
1.1       deraadt    62:
                     63: /* Saved X11 authentication protocol name. */
                     64: char *x11_saved_proto = NULL;
                     65:
                     66: /* Saved X11 authentication data.  This is the real data. */
                     67: char *x11_saved_data = NULL;
                     68: unsigned int x11_saved_data_len = 0;
                     69:
1.27      markus     70: /*
                     71:  * Fake X11 authentication data.  This is what the server will be sending us;
                     72:  * we should replace any occurrences of this by the real data.
                     73:  */
1.1       deraadt    74: char *x11_fake_data = NULL;
                     75: unsigned int x11_fake_data_len;
                     76:
1.27      markus     77: /*
                     78:  * Data structure for storing which hosts are permitted for forward requests.
                     79:  * The local sides of any remote forwards are stored in this array to prevent
                     80:  * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
                     81:  * network (which might be behind a firewall).
                     82:  */
1.25      markus     83: typedef struct {
                     84:        char *host;             /* Host name. */
1.31      markus     85:        u_short port;           /* Port number. */
1.1       deraadt    86: } ForwardPermission;
                     87:
                     88: /* List of all permitted host/port pairs to connect. */
                     89: static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
                     90: /* Number of permitted host/port pairs in the array. */
                     91: static int num_permitted_opens = 0;
1.27      markus     92: /*
                     93:  * If this is true, all opens are permitted.  This is the case on the server
                     94:  * on which we have to trust the client anyway, and the user could do
                     95:  * anything after logging in anyway.
                     96:  */
1.1       deraadt    97: static int all_opens_permitted = 0;
                     98:
                     99: /* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
                    100: static int have_hostname_in_open = 0;
                    101:
                    102: /* Sets specific protocol options. */
                    103:
1.25      markus    104: void
                    105: channel_set_options(int hostname_in_open)
1.1       deraadt   106: {
1.25      markus    107:        have_hostname_in_open = hostname_in_open;
1.1       deraadt   108: }
                    109:
1.27      markus    110: /*
                    111:  * Permits opening to any host/port in SSH_MSG_PORT_OPEN.  This is usually
                    112:  * called by the server, because the user could connect to any port anyway,
                    113:  * and the server has no way to know but to trust the client anyway.
                    114:  */
1.1       deraadt   115:
1.25      markus    116: void
                    117: channel_permit_all_opens()
1.1       deraadt   118: {
1.25      markus    119:        all_opens_permitted = 1;
1.1       deraadt   120: }
                    121:
1.27      markus    122: /*
                    123:  * Allocate a new channel object and set its type and socket. This will cause
                    124:  * remote_name to be freed.
                    125:  */
1.1       deraadt   126:
1.25      markus    127: int
                    128: channel_allocate(int type, int sock, char *remote_name)
1.1       deraadt   129: {
1.25      markus    130:        int i, found;
                    131:        Channel *c;
1.1       deraadt   132:
1.25      markus    133:        /* Update the maximum file descriptor value. */
                    134:        if (sock > channel_max_fd_value)
                    135:                channel_max_fd_value = sock;
1.27      markus    136:        /* XXX set close-on-exec -markus */
1.25      markus    137:
                    138:        /* Do initial allocation if this is the first call. */
                    139:        if (channels_alloc == 0) {
                    140:                channels_alloc = 10;
                    141:                channels = xmalloc(channels_alloc * sizeof(Channel));
                    142:                for (i = 0; i < channels_alloc; i++)
                    143:                        channels[i].type = SSH_CHANNEL_FREE;
1.27      markus    144:                /*
                    145:                 * Kludge: arrange a call to channel_stop_listening if we
                    146:                 * terminate with fatal().
                    147:                 */
1.25      markus    148:                fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
                    149:        }
                    150:        /* Try to find a free slot where to put the new channel. */
                    151:        for (found = -1, i = 0; i < channels_alloc; i++)
                    152:                if (channels[i].type == SSH_CHANNEL_FREE) {
                    153:                        /* Found a free slot. */
                    154:                        found = i;
                    155:                        break;
                    156:                }
                    157:        if (found == -1) {
1.27      markus    158:                /* There are no free slots.  Take last+1 slot and expand the array.  */
1.25      markus    159:                found = channels_alloc;
                    160:                channels_alloc += 10;
                    161:                debug("channel: expanding %d", channels_alloc);
                    162:                channels = xrealloc(channels, channels_alloc * sizeof(Channel));
                    163:                for (i = found; i < channels_alloc; i++)
                    164:                        channels[i].type = SSH_CHANNEL_FREE;
                    165:        }
                    166:        /* Initialize and return new channel number. */
                    167:        c = &channels[found];
                    168:        buffer_init(&c->input);
                    169:        buffer_init(&c->output);
                    170:        chan_init_iostates(c);
                    171:        c->self = found;
                    172:        c->type = type;
                    173:        c->sock = sock;
                    174:        c->remote_id = -1;
                    175:        c->remote_name = remote_name;
                    176:        debug("channel %d: new [%s]", found, remote_name);
                    177:        return found;
1.1       deraadt   178: }
                    179:
                    180: /* Free the channel and close its socket. */
                    181:
1.25      markus    182: void
                    183: channel_free(int channel)
1.1       deraadt   184: {
1.25      markus    185:        if (channel < 0 || channel >= channels_alloc ||
                    186:            channels[channel].type == SSH_CHANNEL_FREE)
                    187:                packet_disconnect("channel free: bad local channel %d", channel);
                    188:
                    189:        if (compat13)
                    190:                shutdown(channels[channel].sock, SHUT_RDWR);
                    191:        close(channels[channel].sock);
                    192:        buffer_free(&channels[channel].input);
                    193:        buffer_free(&channels[channel].output);
                    194:        channels[channel].type = SSH_CHANNEL_FREE;
                    195:        if (channels[channel].remote_name) {
                    196:                xfree(channels[channel].remote_name);
                    197:                channels[channel].remote_name = NULL;
                    198:        }
1.1       deraadt   199: }
                    200:
1.27      markus    201: /*
                    202:  * This is called just before select() to add any bits relevant to channels
                    203:  * in the select bitmasks.
                    204:  */
1.1       deraadt   205:
1.25      markus    206: void
                    207: channel_prepare_select(fd_set * readset, fd_set * writeset)
1.1       deraadt   208: {
1.25      markus    209:        int i;
                    210:        Channel *ch;
                    211:        unsigned char *ucp;
                    212:        unsigned int proto_len, data_len;
                    213:
                    214:        for (i = 0; i < channels_alloc; i++) {
                    215:                ch = &channels[i];
                    216: redo:
                    217:                switch (ch->type) {
                    218:                case SSH_CHANNEL_X11_LISTENER:
                    219:                case SSH_CHANNEL_PORT_LISTENER:
                    220:                case SSH_CHANNEL_AUTH_SOCKET:
                    221:                        FD_SET(ch->sock, readset);
                    222:                        break;
                    223:
                    224:                case SSH_CHANNEL_OPEN:
                    225:                        if (compat13) {
                    226:                                if (buffer_len(&ch->input) < packet_get_maxsize())
                    227:                                        FD_SET(ch->sock, readset);
                    228:                                if (buffer_len(&ch->output) > 0)
                    229:                                        FD_SET(ch->sock, writeset);
                    230:                                break;
                    231:                        }
                    232:                        /* test whether sockets are 'alive' for read/write */
                    233:                        if (ch->istate == CHAN_INPUT_OPEN)
                    234:                                if (buffer_len(&ch->input) < packet_get_maxsize())
                    235:                                        FD_SET(ch->sock, readset);
                    236:                        if (ch->ostate == CHAN_OUTPUT_OPEN ||
                    237:                            ch->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
                    238:                                if (buffer_len(&ch->output) > 0) {
                    239:                                        FD_SET(ch->sock, writeset);
                    240:                                } else if (ch->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
                    241:                                        chan_obuf_empty(ch);
                    242:                                }
                    243:                        }
                    244:                        break;
                    245:
                    246:                case SSH_CHANNEL_INPUT_DRAINING:
                    247:                        if (!compat13)
                    248:                                fatal("cannot happen: IN_DRAIN");
                    249:                        if (buffer_len(&ch->input) == 0) {
                    250:                                packet_start(SSH_MSG_CHANNEL_CLOSE);
                    251:                                packet_put_int(ch->remote_id);
                    252:                                packet_send();
                    253:                                ch->type = SSH_CHANNEL_CLOSED;
1.34      markus    254:                                debug("Closing channel %d after input drain.", ch->self);
1.25      markus    255:                                break;
                    256:                        }
                    257:                        break;
                    258:
                    259:                case SSH_CHANNEL_OUTPUT_DRAINING:
                    260:                        if (!compat13)
                    261:                                fatal("cannot happen: OUT_DRAIN");
                    262:                        if (buffer_len(&ch->output) == 0) {
                    263:                                channel_free(i);
                    264:                                break;
                    265:                        }
                    266:                        FD_SET(ch->sock, writeset);
                    267:                        break;
                    268:
                    269:                case SSH_CHANNEL_X11_OPEN:
1.27      markus    270:                        /*
                    271:                         * This is a special state for X11 authentication
                    272:                         * spoofing.  An opened X11 connection (when
                    273:                         * authentication spoofing is being done) remains in
                    274:                         * this state until the first packet has been
                    275:                         * completely read.  The authentication data in that
                    276:                         * packet is then substituted by the real data if it
                    277:                         * matches the fake data, and the channel is put into
                    278:                         * normal mode.
                    279:                         */
1.25      markus    280:                        /* Check if the fixed size part of the packet is in buffer. */
                    281:                        if (buffer_len(&ch->output) < 12)
                    282:                                break;
                    283:
                    284:                        /* Parse the lengths of variable-length fields. */
                    285:                        ucp = (unsigned char *) buffer_ptr(&ch->output);
                    286:                        if (ucp[0] == 0x42) {   /* Byte order MSB first. */
                    287:                                proto_len = 256 * ucp[6] + ucp[7];
                    288:                                data_len = 256 * ucp[8] + ucp[9];
                    289:                        } else if (ucp[0] == 0x6c) {    /* Byte order LSB first. */
                    290:                                proto_len = ucp[6] + 256 * ucp[7];
                    291:                                data_len = ucp[8] + 256 * ucp[9];
                    292:                        } else {
                    293:                                debug("Initial X11 packet contains bad byte order byte: 0x%x",
                    294:                                      ucp[0]);
                    295:                                ch->type = SSH_CHANNEL_OPEN;
                    296:                                goto reject;
                    297:                        }
                    298:
                    299:                        /* Check if the whole packet is in buffer. */
                    300:                        if (buffer_len(&ch->output) <
                    301:                            12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
                    302:                                break;
                    303:
                    304:                        /* Check if authentication protocol matches. */
                    305:                        if (proto_len != strlen(x11_saved_proto) ||
                    306:                            memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
                    307:                                debug("X11 connection uses different authentication protocol.");
                    308:                                ch->type = SSH_CHANNEL_OPEN;
                    309:                                goto reject;
                    310:                        }
                    311:                        /* Check if authentication data matches our fake data. */
                    312:                        if (data_len != x11_fake_data_len ||
                    313:                            memcmp(ucp + 12 + ((proto_len + 3) & ~3),
                    314:                                x11_fake_data, x11_fake_data_len) != 0) {
                    315:                                debug("X11 auth data does not match fake data.");
                    316:                                ch->type = SSH_CHANNEL_OPEN;
                    317:                                goto reject;
                    318:                        }
                    319:                        /* Check fake data length */
                    320:                        if (x11_fake_data_len != x11_saved_data_len) {
                    321:                                error("X11 fake_data_len %d != saved_data_len %d",
                    322:                                  x11_fake_data_len, x11_saved_data_len);
                    323:                                ch->type = SSH_CHANNEL_OPEN;
                    324:                                goto reject;
                    325:                        }
1.27      markus    326:                        /*
                    327:                         * Received authentication protocol and data match
                    328:                         * our fake data. Substitute the fake data with real
                    329:                         * data.
                    330:                         */
1.25      markus    331:                        memcpy(ucp + 12 + ((proto_len + 3) & ~3),
                    332:                               x11_saved_data, x11_saved_data_len);
                    333:
                    334:                        /* Start normal processing for the channel. */
                    335:                        ch->type = SSH_CHANNEL_OPEN;
                    336:                        goto redo;
                    337:
1.1       deraadt   338:        reject:
1.27      markus    339:                        /*
                    340:                         * We have received an X11 connection that has bad
                    341:                         * authentication information.
                    342:                         */
1.25      markus    343:                        log("X11 connection rejected because of wrong authentication.\r\n");
                    344:                        buffer_clear(&ch->input);
                    345:                        buffer_clear(&ch->output);
                    346:                        if (compat13) {
                    347:                                close(ch->sock);
                    348:                                ch->sock = -1;
                    349:                                ch->type = SSH_CHANNEL_CLOSED;
                    350:                                packet_start(SSH_MSG_CHANNEL_CLOSE);
                    351:                                packet_put_int(ch->remote_id);
                    352:                                packet_send();
                    353:                        } else {
                    354:                                debug("X11 rejected %d i%d/o%d", ch->self, ch->istate, ch->ostate);
                    355:                                chan_read_failed(ch);
                    356:                                chan_write_failed(ch);
                    357:                                debug("X11 rejected %d i%d/o%d", ch->self, ch->istate, ch->ostate);
                    358:                        }
                    359:                        break;
                    360:
                    361:                case SSH_CHANNEL_FREE:
                    362:                default:
                    363:                        continue;
                    364:                }
1.1       deraadt   365:        }
                    366: }
                    367:
1.27      markus    368: /*
                    369:  * After select, perform any appropriate operations for channels which have
                    370:  * events pending.
                    371:  */
1.1       deraadt   372:
1.25      markus    373: void
                    374: channel_after_select(fd_set * readset, fd_set * writeset)
1.1       deraadt   375: {
1.25      markus    376:        struct sockaddr addr;
1.35    ! markus    377:        int newsock, i, newch, len;
        !           378:        socklen_t addrlen;
1.25      markus    379:        Channel *ch;
                    380:        char buf[16384], *remote_hostname;
                    381:
                    382:        /* Loop over all channels... */
                    383:        for (i = 0; i < channels_alloc; i++) {
                    384:                ch = &channels[i];
                    385:                switch (ch->type) {
                    386:                case SSH_CHANNEL_X11_LISTENER:
                    387:                        /* This is our fake X11 server socket. */
                    388:                        if (FD_ISSET(ch->sock, readset)) {
                    389:                                debug("X11 connection requested.");
                    390:                                addrlen = sizeof(addr);
                    391:                                newsock = accept(ch->sock, &addr, &addrlen);
                    392:                                if (newsock < 0) {
                    393:                                        error("accept: %.100s", strerror(errno));
                    394:                                        break;
                    395:                                }
                    396:                                remote_hostname = get_remote_hostname(newsock);
                    397:                                snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
                    398:                                remote_hostname, get_peer_port(newsock));
                    399:                                xfree(remote_hostname);
                    400:                                newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
                    401:                                                         xstrdup(buf));
                    402:                                packet_start(SSH_SMSG_X11_OPEN);
                    403:                                packet_put_int(newch);
                    404:                                if (have_hostname_in_open)
                    405:                                        packet_put_string(buf, strlen(buf));
                    406:                                packet_send();
                    407:                        }
                    408:                        break;
                    409:
                    410:                case SSH_CHANNEL_PORT_LISTENER:
1.27      markus    411:                        /*
                    412:                         * This socket is listening for connections to a
                    413:                         * forwarded TCP/IP port.
                    414:                         */
1.25      markus    415:                        if (FD_ISSET(ch->sock, readset)) {
1.35    ! markus    416:                                debug("Connection to port %d forwarding to %.100s port %d requested.",
1.25      markus    417:                                      ch->listening_port, ch->path, ch->host_port);
                    418:                                addrlen = sizeof(addr);
                    419:                                newsock = accept(ch->sock, &addr, &addrlen);
                    420:                                if (newsock < 0) {
                    421:                                        error("accept: %.100s", strerror(errno));
                    422:                                        break;
                    423:                                }
                    424:                                remote_hostname = get_remote_hostname(newsock);
1.35    ! markus    425:                                snprintf(buf, sizeof buf, "listen port %d for %.100s port %d, connect from %.200s port %d",
1.25      markus    426:                                         ch->listening_port, ch->path, ch->host_port,
                    427:                                remote_hostname, get_peer_port(newsock));
                    428:                                xfree(remote_hostname);
                    429:                                newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
                    430:                                                         xstrdup(buf));
                    431:                                packet_start(SSH_MSG_PORT_OPEN);
                    432:                                packet_put_int(newch);
                    433:                                packet_put_string(ch->path, strlen(ch->path));
                    434:                                packet_put_int(ch->host_port);
                    435:                                if (have_hostname_in_open)
                    436:                                        packet_put_string(buf, strlen(buf));
                    437:                                packet_send();
                    438:                        }
                    439:                        break;
                    440:
                    441:                case SSH_CHANNEL_AUTH_SOCKET:
1.27      markus    442:                        /*
                    443:                         * This is the authentication agent socket listening
                    444:                         * for connections from clients.
                    445:                         */
1.25      markus    446:                        if (FD_ISSET(ch->sock, readset)) {
1.34      markus    447:                                addrlen = sizeof(addr);
                    448:                                newsock = accept(ch->sock, &addr, &addrlen);
1.25      markus    449:                                if (newsock < 0) {
                    450:                                        error("accept from auth socket: %.100s", strerror(errno));
                    451:                                        break;
                    452:                                }
1.34      markus    453:                                newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
1.25      markus    454:                                        xstrdup("accepted auth socket"));
                    455:                                packet_start(SSH_SMSG_AGENT_OPEN);
1.34      markus    456:                                packet_put_int(newch);
1.25      markus    457:                                packet_send();
                    458:                        }
                    459:                        break;
                    460:
                    461:                case SSH_CHANNEL_OPEN:
1.27      markus    462:                        /*
                    463:                         * This is an open two-way communication channel. It
                    464:                         * is not of interest to us at this point what kind
                    465:                         * of data is being transmitted.
                    466:                         */
                    467:
                    468:                        /*
                    469:                         * Read available incoming data and append it to
                    470:                         * buffer; shutdown socket, if read or write failes
                    471:                         */
1.25      markus    472:                        if (FD_ISSET(ch->sock, readset)) {
                    473:                                len = read(ch->sock, buf, sizeof(buf));
                    474:                                if (len <= 0) {
                    475:                                        if (compat13) {
                    476:                                                buffer_consume(&ch->output, buffer_len(&ch->output));
                    477:                                                ch->type = SSH_CHANNEL_INPUT_DRAINING;
                    478:                                                debug("Channel %d status set to input draining.", i);
                    479:                                        } else {
                    480:                                                chan_read_failed(ch);
                    481:                                        }
                    482:                                        break;
                    483:                                }
                    484:                                buffer_append(&ch->input, buf, len);
                    485:                        }
                    486:                        /* Send buffered output data to the socket. */
                    487:                        if (FD_ISSET(ch->sock, writeset) && buffer_len(&ch->output) > 0) {
                    488:                                len = write(ch->sock, buffer_ptr(&ch->output),
                    489:                                            buffer_len(&ch->output));
                    490:                                if (len <= 0) {
                    491:                                        if (compat13) {
                    492:                                                buffer_consume(&ch->output, buffer_len(&ch->output));
                    493:                                                debug("Channel %d status set to input draining.", i);
                    494:                                                ch->type = SSH_CHANNEL_INPUT_DRAINING;
                    495:                                        } else {
                    496:                                                chan_write_failed(ch);
                    497:                                        }
                    498:                                        break;
                    499:                                }
                    500:                                buffer_consume(&ch->output, len);
                    501:                        }
                    502:                        break;
                    503:
                    504:                case SSH_CHANNEL_OUTPUT_DRAINING:
                    505:                        if (!compat13)
                    506:                                fatal("cannot happen: OUT_DRAIN");
                    507:                        /* Send buffered output data to the socket. */
                    508:                        if (FD_ISSET(ch->sock, writeset) && buffer_len(&ch->output) > 0) {
                    509:                                len = write(ch->sock, buffer_ptr(&ch->output),
                    510:                                            buffer_len(&ch->output));
                    511:                                if (len <= 0)
                    512:                                        buffer_consume(&ch->output, buffer_len(&ch->output));
                    513:                                else
                    514:                                        buffer_consume(&ch->output, len);
                    515:                        }
                    516:                        break;
                    517:
                    518:                case SSH_CHANNEL_X11_OPEN:
                    519:                case SSH_CHANNEL_FREE:
                    520:                default:
                    521:                        continue;
1.1       deraadt   522:                }
                    523:        }
                    524: }
                    525:
                    526: /* If there is data to send to the connection, send some of it now. */
                    527:
1.25      markus    528: void
                    529: channel_output_poll()
1.1       deraadt   530: {
1.25      markus    531:        int len, i;
                    532:        Channel *ch;
1.1       deraadt   533:
1.25      markus    534:        for (i = 0; i < channels_alloc; i++) {
                    535:                ch = &channels[i];
1.27      markus    536:                /* We are only interested in channels that can have buffered incoming data. */
1.25      markus    537:                if (ch->type != SSH_CHANNEL_OPEN &&
                    538:                    ch->type != SSH_CHANNEL_INPUT_DRAINING)
                    539:                        continue;
                    540:
                    541:                /* Get the amount of buffered data for this channel. */
                    542:                len = buffer_len(&ch->input);
                    543:                if (len > 0) {
1.27      markus    544:                        /* Send some data for the other side over the secure connection. */
1.25      markus    545:                        if (packet_is_interactive()) {
                    546:                                if (len > 1024)
                    547:                                        len = 512;
                    548:                        } else {
                    549:                                /* Keep the packets at reasonable size. */
1.34      markus    550:                                if (len > packet_get_maxsize()/2)
                    551:                                        len = packet_get_maxsize()/2;
1.25      markus    552:                        }
                    553:                        packet_start(SSH_MSG_CHANNEL_DATA);
                    554:                        packet_put_int(ch->remote_id);
                    555:                        packet_put_string(buffer_ptr(&ch->input), len);
                    556:                        packet_send();
                    557:                        buffer_consume(&ch->input, len);
                    558:                } else if (ch->istate == CHAN_INPUT_WAIT_DRAIN) {
                    559:                        if (compat13)
                    560:                                fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
1.27      markus    561:                        /*
                    562:                         * input-buffer is empty and read-socket shutdown:
                    563:                         * tell peer, that we will not send more data: send IEOF
                    564:                         */
1.25      markus    565:                        chan_ibuf_empty(ch);
                    566:                }
                    567:        }
1.1       deraadt   568: }
                    569:
1.27      markus    570: /*
                    571:  * This is called when a packet of type CHANNEL_DATA has just been received.
                    572:  * The message type has already been consumed, but channel number and data is
                    573:  * still there.
                    574:  */
1.1       deraadt   575:
1.25      markus    576: void
                    577: channel_input_data(int payload_len)
1.1       deraadt   578: {
1.25      markus    579:        int channel;
                    580:        char *data;
                    581:        unsigned int data_len;
                    582:
                    583:        /* Get the channel number and verify it. */
                    584:        channel = packet_get_int();
                    585:        if (channel < 0 || channel >= channels_alloc ||
                    586:            channels[channel].type == SSH_CHANNEL_FREE)
                    587:                packet_disconnect("Received data for nonexistent channel %d.", channel);
                    588:
                    589:        /* Ignore any data for non-open channels (might happen on close) */
                    590:        if (channels[channel].type != SSH_CHANNEL_OPEN &&
                    591:            channels[channel].type != SSH_CHANNEL_X11_OPEN)
                    592:                return;
                    593:
                    594:        /* Get the data. */
                    595:        data = packet_get_string(&data_len);
                    596:        packet_integrity_check(payload_len, 4 + 4 + data_len, SSH_MSG_CHANNEL_DATA);
                    597:        buffer_append(&channels[channel].output, data, data_len);
                    598:        xfree(data);
1.1       deraadt   599: }
                    600:
1.27      markus    601: /*
                    602:  * Returns true if no channel has too much buffered data, and false if one or
                    603:  * more channel is overfull.
                    604:  */
1.1       deraadt   605:
1.25      markus    606: int
                    607: channel_not_very_much_buffered_data()
1.1       deraadt   608: {
1.25      markus    609:        unsigned int i;
                    610:        Channel *ch;
                    611:
                    612:        for (i = 0; i < channels_alloc; i++) {
                    613:                ch = &channels[i];
                    614:                switch (ch->type) {
                    615:                case SSH_CHANNEL_X11_LISTENER:
                    616:                case SSH_CHANNEL_PORT_LISTENER:
                    617:                case SSH_CHANNEL_AUTH_SOCKET:
                    618:                        continue;
                    619:                case SSH_CHANNEL_OPEN:
                    620:                        if (buffer_len(&ch->input) > packet_get_maxsize())
                    621:                                return 0;
                    622:                        if (buffer_len(&ch->output) > packet_get_maxsize())
                    623:                                return 0;
                    624:                        continue;
                    625:                case SSH_CHANNEL_INPUT_DRAINING:
                    626:                case SSH_CHANNEL_OUTPUT_DRAINING:
                    627:                case SSH_CHANNEL_X11_OPEN:
                    628:                case SSH_CHANNEL_FREE:
                    629:                default:
                    630:                        continue;
                    631:                }
1.1       deraadt   632:        }
1.25      markus    633:        return 1;
1.1       deraadt   634: }
                    635:
1.14      markus    636: /* This is called after receiving CHANNEL_CLOSE/IEOF. */
1.1       deraadt   637:
1.25      markus    638: void
                    639: channel_input_close()
1.1       deraadt   640: {
1.25      markus    641:        int channel;
1.1       deraadt   642:
1.25      markus    643:        /* Get the channel number and verify it. */
                    644:        channel = packet_get_int();
                    645:        if (channel < 0 || channel >= channels_alloc ||
                    646:            channels[channel].type == SSH_CHANNEL_FREE)
                    647:                packet_disconnect("Received data for nonexistent channel %d.", channel);
                    648:
                    649:        if (!compat13) {
                    650:                /* proto version 1.5 overloads CLOSE with IEOF */
                    651:                chan_rcvd_ieof(&channels[channel]);
                    652:                return;
                    653:        }
1.27      markus    654:
                    655:        /*
                    656:         * Send a confirmation that we have closed the channel and no more
                    657:         * data is coming for it.
                    658:         */
1.25      markus    659:        packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
                    660:        packet_put_int(channels[channel].remote_id);
                    661:        packet_send();
                    662:
1.27      markus    663:        /*
                    664:         * If the channel is in closed state, we have sent a close request,
                    665:         * and the other side will eventually respond with a confirmation.
                    666:         * Thus, we cannot free the channel here, because then there would be
                    667:         * no-one to receive the confirmation.  The channel gets freed when
                    668:         * the confirmation arrives.
                    669:         */
1.25      markus    670:        if (channels[channel].type != SSH_CHANNEL_CLOSED) {
1.27      markus    671:                /*
                    672:                 * Not a closed channel - mark it as draining, which will
                    673:                 * cause it to be freed later.
                    674:                 */
1.25      markus    675:                buffer_consume(&channels[channel].input,
                    676:                               buffer_len(&channels[channel].input));
                    677:                channels[channel].type = SSH_CHANNEL_OUTPUT_DRAINING;
                    678:        }
1.1       deraadt   679: }
                    680:
1.14      markus    681: /* This is called after receiving CHANNEL_CLOSE_CONFIRMATION/OCLOSE. */
1.1       deraadt   682:
1.25      markus    683: void
                    684: channel_input_close_confirmation()
1.1       deraadt   685: {
1.25      markus    686:        int channel;
1.1       deraadt   687:
1.25      markus    688:        /* Get the channel number and verify it. */
                    689:        channel = packet_get_int();
                    690:        if (channel < 0 || channel >= channels_alloc)
                    691:                packet_disconnect("Received close confirmation for out-of-range channel %d.",
                    692:                                  channel);
                    693:
                    694:        if (!compat13) {
                    695:                /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
                    696:                chan_rcvd_oclose(&channels[channel]);
                    697:                return;
                    698:        }
                    699:        if (channels[channel].type != SSH_CHANNEL_CLOSED)
                    700:                packet_disconnect("Received close confirmation for non-closed channel %d (type %d).",
                    701:                                  channel, channels[channel].type);
1.1       deraadt   702:
1.25      markus    703:        /* Free the channel. */
                    704:        channel_free(channel);
1.1       deraadt   705: }
                    706:
                    707: /* This is called after receiving CHANNEL_OPEN_CONFIRMATION. */
                    708:
1.25      markus    709: void
                    710: channel_input_open_confirmation()
1.1       deraadt   711: {
1.25      markus    712:        int channel, remote_channel;
1.1       deraadt   713:
1.25      markus    714:        /* Get the channel number and verify it. */
                    715:        channel = packet_get_int();
                    716:        if (channel < 0 || channel >= channels_alloc ||
                    717:            channels[channel].type != SSH_CHANNEL_OPENING)
                    718:                packet_disconnect("Received open confirmation for non-opening channel %d.",
                    719:                                  channel);
                    720:
                    721:        /* Get remote side's id for this channel. */
                    722:        remote_channel = packet_get_int();
                    723:
1.27      markus    724:        /* Record the remote channel number and mark that the channel is now open. */
1.25      markus    725:        channels[channel].remote_id = remote_channel;
                    726:        channels[channel].type = SSH_CHANNEL_OPEN;
1.1       deraadt   727: }
                    728:
                    729: /* This is called after receiving CHANNEL_OPEN_FAILURE from the other side. */
                    730:
1.25      markus    731: void
                    732: channel_input_open_failure()
1.1       deraadt   733: {
1.25      markus    734:        int channel;
                    735:
                    736:        /* Get the channel number and verify it. */
                    737:        channel = packet_get_int();
                    738:        if (channel < 0 || channel >= channels_alloc ||
                    739:            channels[channel].type != SSH_CHANNEL_OPENING)
                    740:                packet_disconnect("Received open failure for non-opening channel %d.",
                    741:                                  channel);
1.1       deraadt   742:
1.25      markus    743:        /* Free the channel.  This will also close the socket. */
                    744:        channel_free(channel);
1.1       deraadt   745: }
                    746:
1.27      markus    747: /*
                    748:  * Stops listening for channels, and removes any unix domain sockets that we
                    749:  * might have.
                    750:  */
1.1       deraadt   751:
1.25      markus    752: void
                    753: channel_stop_listening()
1.1       deraadt   754: {
1.25      markus    755:        int i;
                    756:        for (i = 0; i < channels_alloc; i++) {
                    757:                switch (channels[i].type) {
                    758:                case SSH_CHANNEL_AUTH_SOCKET:
                    759:                        close(channels[i].sock);
                    760:                        remove(channels[i].path);
                    761:                        channel_free(i);
                    762:                        break;
                    763:                case SSH_CHANNEL_PORT_LISTENER:
                    764:                case SSH_CHANNEL_X11_LISTENER:
                    765:                        close(channels[i].sock);
                    766:                        channel_free(i);
                    767:                        break;
                    768:                default:
                    769:                        break;
                    770:                }
1.1       deraadt   771:        }
                    772: }
                    773:
1.27      markus    774: /*
                    775:  * Closes the sockets of all channels.  This is used to close extra file
                    776:  * descriptors after a fork.
                    777:  */
1.1       deraadt   778:
1.25      markus    779: void
                    780: channel_close_all()
1.1       deraadt   781: {
1.25      markus    782:        int i;
                    783:        for (i = 0; i < channels_alloc; i++) {
                    784:                if (channels[i].type != SSH_CHANNEL_FREE)
                    785:                        close(channels[i].sock);
                    786:        }
1.1       deraadt   787: }
                    788:
                    789: /* Returns the maximum file descriptor number used by the channels. */
                    790:
1.25      markus    791: int
                    792: channel_max_fd()
1.1       deraadt   793: {
1.25      markus    794:        return channel_max_fd_value;
1.1       deraadt   795: }
                    796:
                    797: /* Returns true if any channel is still open. */
                    798:
1.25      markus    799: int
                    800: channel_still_open()
1.1       deraadt   801: {
1.25      markus    802:        unsigned int i;
                    803:        for (i = 0; i < channels_alloc; i++)
                    804:                switch (channels[i].type) {
                    805:                case SSH_CHANNEL_FREE:
                    806:                case SSH_CHANNEL_X11_LISTENER:
                    807:                case SSH_CHANNEL_PORT_LISTENER:
                    808:                case SSH_CHANNEL_CLOSED:
                    809:                case SSH_CHANNEL_AUTH_SOCKET:
                    810:                        continue;
                    811:                case SSH_CHANNEL_OPENING:
                    812:                case SSH_CHANNEL_OPEN:
                    813:                case SSH_CHANNEL_X11_OPEN:
                    814:                        return 1;
                    815:                case SSH_CHANNEL_INPUT_DRAINING:
                    816:                case SSH_CHANNEL_OUTPUT_DRAINING:
                    817:                        if (!compat13)
                    818:                                fatal("cannot happen: OUT_DRAIN");
                    819:                        return 1;
                    820:                default:
                    821:                        fatal("channel_still_open: bad channel type %d", channels[i].type);
                    822:                        /* NOTREACHED */
                    823:                }
                    824:        return 0;
1.1       deraadt   825: }
                    826:
1.27      markus    827: /*
                    828:  * Returns a message describing the currently open forwarded connections,
                    829:  * suitable for sending to the client.  The message contains crlf pairs for
                    830:  * newlines.
                    831:  */
1.1       deraadt   832:
1.25      markus    833: char *
                    834: channel_open_message()
1.1       deraadt   835: {
1.25      markus    836:        Buffer buffer;
                    837:        int i;
                    838:        char buf[512], *cp;
                    839:
                    840:        buffer_init(&buffer);
                    841:        snprintf(buf, sizeof buf, "The following connections are open:\r\n");
1.1       deraadt   842:        buffer_append(&buffer, buf, strlen(buf));
1.25      markus    843:        for (i = 0; i < channels_alloc; i++) {
                    844:                Channel *c = &channels[i];
                    845:                switch (c->type) {
                    846:                case SSH_CHANNEL_FREE:
                    847:                case SSH_CHANNEL_X11_LISTENER:
                    848:                case SSH_CHANNEL_PORT_LISTENER:
                    849:                case SSH_CHANNEL_CLOSED:
                    850:                case SSH_CHANNEL_AUTH_SOCKET:
                    851:                        continue;
                    852:                case SSH_CHANNEL_OPENING:
                    853:                case SSH_CHANNEL_OPEN:
                    854:                case SSH_CHANNEL_X11_OPEN:
                    855:                case SSH_CHANNEL_INPUT_DRAINING:
                    856:                case SSH_CHANNEL_OUTPUT_DRAINING:
                    857:                        snprintf(buf, sizeof buf, "  #%d %.300s (t%d r%d i%d o%d)\r\n",
                    858:                                 c->self, c->remote_name,
                    859:                                 c->type, c->remote_id, c->istate, c->ostate);
                    860:                        buffer_append(&buffer, buf, strlen(buf));
                    861:                        continue;
                    862:                default:
                    863:                        fatal("channel_still_open: bad channel type %d", c->type);
                    864:                        /* NOTREACHED */
                    865:                }
                    866:        }
                    867:        buffer_append(&buffer, "\0", 1);
                    868:        cp = xstrdup(buffer_ptr(&buffer));
                    869:        buffer_free(&buffer);
                    870:        return cp;
1.1       deraadt   871: }
                    872:
1.27      markus    873: /*
                    874:  * Initiate forwarding of connections to local port "port" through the secure
                    875:  * channel to host:port from remote side.
                    876:  */
1.1       deraadt   877:
1.25      markus    878: void
1.31      markus    879: channel_request_local_forwarding(u_short port, const char *host,
1.33      markus    880:                                 u_short host_port, int gateway_ports)
1.25      markus    881: {
1.35    ! markus    882:        int success, ch, sock, on = 1;
        !           883:        struct addrinfo hints, *ai, *aitop;
        !           884:        char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.28      markus    885:        struct linger linger;
1.25      markus    886:
                    887:        if (strlen(host) > sizeof(channels[0].path) - 1)
                    888:                packet_disconnect("Forward host name too long.");
                    889:
1.28      markus    890:        /*
1.35    ! markus    891:         * getaddrinfo returns a loopback address if the hostname is
        !           892:         * set to NULL and hints.ai_flags is not AI_PASSIVE
1.28      markus    893:         */
1.35    ! markus    894:        memset(&hints, 0, sizeof(hints));
        !           895:        hints.ai_family = IPv4or6;
        !           896:        hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
        !           897:        hints.ai_socktype = SOCK_STREAM;
        !           898:        snprintf(strport, sizeof strport, "%d", port);
        !           899:        if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
        !           900:                packet_disconnect("getaddrinfo: fatal error");
        !           901:
        !           902:        success = 0;
        !           903:        for (ai = aitop; ai; ai = ai->ai_next) {
        !           904:                if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
        !           905:                        continue;
        !           906:                if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
        !           907:                    strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
        !           908:                        error("channel_request_local_forwarding: getnameinfo failed");
        !           909:                        continue;
        !           910:                }
        !           911:                /* Create a port to listen for the host. */
        !           912:                sock = socket(ai->ai_family, SOCK_STREAM, 0);
        !           913:                if (sock < 0) {
        !           914:                        /* this is no error since kernel may not support ipv6 */
        !           915:                        verbose("socket: %.100s", strerror(errno));
        !           916:                        continue;
        !           917:                }
        !           918:                /*
        !           919:                 * Set socket options.  We would like the socket to disappear
        !           920:                 * as soon as it has been closed for whatever reason.
        !           921:                 */
        !           922:                setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
        !           923:                linger.l_onoff = 1;
        !           924:                linger.l_linger = 5;
        !           925:                setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
        !           926:                debug("Local forwarding listening on %s port %s.", ntop, strport);
        !           927:
        !           928:                /* Bind the socket to the address. */
        !           929:                if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
        !           930:                        /* address can be in use ipv6 address is already bound */
        !           931:                        verbose("bind: %.100s", strerror(errno));
        !           932:                        close(sock);
        !           933:                        continue;
        !           934:                }
        !           935:                /* Start listening for connections on the socket. */
        !           936:                if (listen(sock, 5) < 0) {
        !           937:                        error("listen: %.100s", strerror(errno));
        !           938:                        close(sock);
        !           939:                        continue;
        !           940:                }
        !           941:                /* Allocate a channel number for the socket. */
        !           942:                ch = channel_allocate(SSH_CHANNEL_PORT_LISTENER, sock,
        !           943:                    xstrdup("port listener"));
        !           944:                strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
        !           945:                channels[ch].host_port = host_port;
        !           946:                channels[ch].listening_port = port;
        !           947:                success = 1;
        !           948:        }
        !           949:        if (success == 0)
        !           950:                packet_disconnect("cannot listen port: %d", port);
        !           951:        freeaddrinfo(aitop);
1.25      markus    952: }
1.1       deraadt   953:
1.27      markus    954: /*
                    955:  * Initiate forwarding of connections to port "port" on remote host through
                    956:  * the secure channel to host:port from local side.
                    957:  */
1.1       deraadt   958:
1.25      markus    959: void
1.31      markus    960: channel_request_remote_forwarding(u_short port, const char *host,
                    961:                                  u_short remote_port)
1.25      markus    962: {
                    963:        int payload_len;
                    964:        /* Record locally that connection to this host/port is permitted. */
                    965:        if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
                    966:                fatal("channel_request_remote_forwarding: too many forwards");
                    967:
                    968:        permitted_opens[num_permitted_opens].host = xstrdup(host);
                    969:        permitted_opens[num_permitted_opens].port = remote_port;
                    970:        num_permitted_opens++;
                    971:
                    972:        /* Send the forward request to the remote side. */
                    973:        packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
                    974:        packet_put_int(port);
                    975:        packet_put_string(host, strlen(host));
                    976:        packet_put_int(remote_port);
                    977:        packet_send();
                    978:        packet_write_wait();
                    979:
1.27      markus    980:        /*
                    981:         * Wait for response from the remote side.  It will send a disconnect
                    982:         * message on failure, and we will never see it here.
                    983:         */
1.25      markus    984:        packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
1.1       deraadt   985: }
                    986:
1.27      markus    987: /*
                    988:  * This is called after receiving CHANNEL_FORWARDING_REQUEST.  This initates
                    989:  * listening for the port, and sends back a success reply (or disconnect
                    990:  * message if there was an error).  This never returns if there was an error.
                    991:  */
1.1       deraadt   992:
1.25      markus    993: void
                    994: channel_input_port_forward_request(int is_root)
1.1       deraadt   995: {
1.31      markus    996:        u_short port, host_port;
1.25      markus    997:        char *hostname;
1.1       deraadt   998:
1.25      markus    999:        /* Get arguments from the packet. */
                   1000:        port = packet_get_int();
                   1001:        hostname = packet_get_string(NULL);
                   1002:        host_port = packet_get_int();
                   1003:
1.27      markus   1004:        /*
                   1005:         * Check that an unprivileged user is not trying to forward a
                   1006:         * privileged port.
                   1007:         */
1.25      markus   1008:        if (port < IPPORT_RESERVED && !is_root)
                   1009:                packet_disconnect("Requested forwarding of port %d but user is not root.",
                   1010:                                  port);
1.33      markus   1011:        /*
                   1012:         * Initiate forwarding,
                   1013:         * bind port to localhost only (gateway ports == 0).
                   1014:         */
                   1015:        channel_request_local_forwarding(port, hostname, host_port, 0);
1.25      markus   1016:
                   1017:        /* Free the argument string. */
                   1018:        xfree(hostname);
1.1       deraadt  1019: }
                   1020:
1.27      markus   1021: /*
                   1022:  * This is called after receiving PORT_OPEN message.  This attempts to
                   1023:  * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
                   1024:  * or CHANNEL_OPEN_FAILURE.
                   1025:  */
1.1       deraadt  1026:
1.25      markus   1027: void
                   1028: channel_input_port_open(int payload_len)
1.1       deraadt  1029: {
1.35    ! markus   1030:        int remote_channel, sock = 0, newch, i;
1.31      markus   1031:        u_short host_port;
1.25      markus   1032:        char *host, *originator_string;
                   1033:        int host_len, originator_len;
1.35    ! markus   1034:        struct addrinfo hints, *ai, *aitop;
        !          1035:        char ntop[NI_MAXHOST], strport[NI_MAXSERV];
        !          1036:        int gaierr;
1.25      markus   1037:
                   1038:        /* Get remote channel number. */
                   1039:        remote_channel = packet_get_int();
                   1040:
                   1041:        /* Get host name to connect to. */
                   1042:        host = packet_get_string(&host_len);
                   1043:
                   1044:        /* Get port to connect to. */
                   1045:        host_port = packet_get_int();
                   1046:
                   1047:        /* Get remote originator name. */
1.29      markus   1048:        if (have_hostname_in_open) {
1.25      markus   1049:                originator_string = packet_get_string(&originator_len);
1.29      markus   1050:                originator_len += 4;    /* size of packet_int */
                   1051:        } else {
1.25      markus   1052:                originator_string = xstrdup("unknown (remote did not supply name)");
1.29      markus   1053:                originator_len = 0;     /* no originator supplied */
                   1054:        }
1.25      markus   1055:
                   1056:        packet_integrity_check(payload_len,
1.29      markus   1057:                               4 + 4 + host_len + 4 + originator_len,
1.25      markus   1058:                               SSH_MSG_PORT_OPEN);
                   1059:
                   1060:        /* Check if opening that port is permitted. */
                   1061:        if (!all_opens_permitted) {
                   1062:                /* Go trough all permitted ports. */
                   1063:                for (i = 0; i < num_permitted_opens; i++)
                   1064:                        if (permitted_opens[i].port == host_port &&
                   1065:                            strcmp(permitted_opens[i].host, host) == 0)
                   1066:                                break;
                   1067:
                   1068:                /* Check if we found the requested port among those permitted. */
                   1069:                if (i >= num_permitted_opens) {
                   1070:                        /* The port is not permitted. */
                   1071:                        log("Received request to connect to %.100s:%d, but the request was denied.",
                   1072:                            host, host_port);
                   1073:                        packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
                   1074:                        packet_put_int(remote_channel);
                   1075:                        packet_send();
                   1076:                }
                   1077:        }
1.35    ! markus   1078:
        !          1079:        memset(&hints, 0, sizeof(hints));
        !          1080:        hints.ai_family = IPv4or6;
        !          1081:        hints.ai_socktype = SOCK_STREAM;
        !          1082:        snprintf(strport, sizeof strport, "%d", host_port);
        !          1083:        if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
        !          1084:                error("%.100s: unknown host (%s)", host, gai_strerror(gaierr));
        !          1085:                goto fail;
        !          1086:        }
        !          1087:
        !          1088:        for (ai = aitop; ai; ai = ai->ai_next) {
        !          1089:                if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
        !          1090:                        continue;
        !          1091:                if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
        !          1092:                    strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
        !          1093:                        error("channel_input_port_open: getnameinfo failed");
        !          1094:                        continue;
        !          1095:                }
        !          1096:                /* Create the socket. */
        !          1097:                sock = socket(ai->ai_family, SOCK_STREAM, 0);
        !          1098:                if (sock < 0) {
        !          1099:                        error("socket: %.100s", strerror(errno));
        !          1100:                        continue;
1.25      markus   1101:                }
1.35    ! markus   1102:                /* Connect to the host/port. */
        !          1103:                if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
        !          1104:                        error("connect %.100s port %s: %.100s", ntop, strport,
        !          1105:                            strerror(errno));
        !          1106:                        close(sock);
        !          1107:                        continue;       /* fail -- try next */
1.25      markus   1108:                }
1.35    ! markus   1109:                break; /* success */
        !          1110:
1.25      markus   1111:        }
1.35    ! markus   1112:        freeaddrinfo(aitop);
        !          1113:
        !          1114:        if (!ai) {
        !          1115:                error("connect %.100s port %d: failed.", host, host_port);
1.25      markus   1116:                goto fail;
                   1117:        }
1.35    ! markus   1118:
1.25      markus   1119:        /* Successful connection. */
                   1120:
                   1121:        /* Allocate a channel for this connection. */
                   1122:        newch = channel_allocate(SSH_CHANNEL_OPEN, sock, originator_string);
                   1123:        channels[newch].remote_id = remote_channel;
                   1124:
                   1125:        /* Send a confirmation to the remote host. */
                   1126:        packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
                   1127:        packet_put_int(remote_channel);
                   1128:        packet_put_int(newch);
                   1129:        packet_send();
                   1130:
                   1131:        /* Free the argument string. */
                   1132:        xfree(host);
                   1133:
                   1134:        return;
                   1135:
                   1136: fail:
                   1137:        /* Free the argument string. */
                   1138:        xfree(host);
                   1139:
                   1140:        /* Send refusal to the remote host. */
                   1141:        packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
                   1142:        packet_put_int(remote_channel);
                   1143:        packet_send();
1.1       deraadt  1144: }
                   1145:
1.27      markus   1146: /*
                   1147:  * Creates an internet domain socket for listening for X11 connections.
                   1148:  * Returns a suitable value for the DISPLAY variable, or NULL if an error
                   1149:  * occurs.
                   1150:  */
1.1       deraadt  1151:
1.35    ! markus   1152: #define        NUM_SOCKS       10
        !          1153:
1.25      markus   1154: char *
1.33      markus   1155: x11_create_display_inet(int screen_number, int x11_display_offset)
1.1       deraadt  1156: {
1.31      markus   1157:        int display_number, sock;
                   1158:        u_short port;
1.35    ! markus   1159:        struct addrinfo hints, *ai, *aitop;
        !          1160:        char strport[NI_MAXSERV];
        !          1161:        int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
        !          1162:        char display[512];
1.25      markus   1163:        char hostname[MAXHOSTNAMELEN];
                   1164:
1.33      markus   1165:        for (display_number = x11_display_offset;
1.25      markus   1166:             display_number < MAX_DISPLAYS;
                   1167:             display_number++) {
                   1168:                port = 6000 + display_number;
1.35    ! markus   1169:                memset(&hints, 0, sizeof(hints));
        !          1170:                hints.ai_family = IPv4or6;
        !          1171:                hints.ai_flags = 0 /*AI_PASSIVE*/;      /* XXX loopback only ? */
        !          1172:                hints.ai_socktype = SOCK_STREAM;
        !          1173:                snprintf(strport, sizeof strport, "%d", port);
        !          1174:                if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
        !          1175:                        error("getaddrinfo: %.100s", gai_strerror(gaierr));
1.25      markus   1176:                        return NULL;
                   1177:                }
1.35    ! markus   1178:                for (ai = aitop; ai; ai = ai->ai_next) {
        !          1179:                        if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
        !          1180:                                continue;
        !          1181:                        sock = socket(ai->ai_family, SOCK_STREAM, 0);
        !          1182:                        if (sock < 0) {
        !          1183:                                error("socket: %.100s", strerror(errno));
        !          1184:                                return NULL;
        !          1185:                        }
        !          1186:                        if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
        !          1187:                                debug("bind port %d: %.100s", port, strerror(errno));
        !          1188:                                shutdown(sock, SHUT_RDWR);
        !          1189:                                close(sock);
        !          1190:                                for (n = 0; n < num_socks; n++) {
        !          1191:                                        shutdown(socks[n], SHUT_RDWR);
        !          1192:                                        close(socks[n]);
        !          1193:                                }
        !          1194:                                num_socks = 0;
        !          1195:                                break;
        !          1196:                        }
        !          1197:                        socks[num_socks++] = sock;
        !          1198:                        if (num_socks == NUM_SOCKS)
        !          1199:                                break;
1.25      markus   1200:                }
1.35    ! markus   1201:                if (num_socks > 0)
        !          1202:                        break;
1.25      markus   1203:        }
                   1204:        if (display_number >= MAX_DISPLAYS) {
                   1205:                error("Failed to allocate internet-domain X11 display socket.");
                   1206:                return NULL;
                   1207:        }
                   1208:        /* Start listening for connections on the socket. */
1.35    ! markus   1209:        for (n = 0; n < num_socks; n++) {
        !          1210:                sock = socks[n];
        !          1211:                if (listen(sock, 5) < 0) {
        !          1212:                        error("listen: %.100s", strerror(errno));
        !          1213:                        shutdown(sock, SHUT_RDWR);
        !          1214:                        close(sock);
        !          1215:                        return NULL;
        !          1216:                }
1.25      markus   1217:        }
1.35    ! markus   1218:
1.25      markus   1219:        /* Set up a suitable value for the DISPLAY variable. */
                   1220:        if (gethostname(hostname, sizeof(hostname)) < 0)
                   1221:                fatal("gethostname: %.100s", strerror(errno));
1.35    ! markus   1222:        snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
1.25      markus   1223:                 display_number, screen_number);
                   1224:
1.35    ! markus   1225:        /* Allocate a channel for each socket. */
        !          1226:        for (n = 0; n < num_socks; n++) {
        !          1227:                sock = socks[n];
        !          1228:                (void) channel_allocate(SSH_CHANNEL_X11_LISTENER, sock,
        !          1229:                                        xstrdup("X11 inet listener"));
        !          1230:        }
1.1       deraadt  1231:
1.25      markus   1232:        /* Return a suitable value for the DISPLAY environment variable. */
1.35    ! markus   1233:        return xstrdup(display);
1.1       deraadt  1234: }
                   1235:
                   1236: #ifndef X_UNIX_PATH
                   1237: #define X_UNIX_PATH "/tmp/.X11-unix/X"
                   1238: #endif
                   1239:
                   1240: static
                   1241: int
1.30      deraadt  1242: connect_local_xsocket(unsigned int dnr)
1.1       deraadt  1243: {
1.25      markus   1244:        static const char *const x_sockets[] = {
                   1245:                X_UNIX_PATH "%u",
                   1246:                "/var/X/.X11-unix/X" "%u",
                   1247:                "/usr/spool/sockets/X11/" "%u",
                   1248:                NULL
                   1249:        };
                   1250:        int sock;
                   1251:        struct sockaddr_un addr;
                   1252:        const char *const * path;
                   1253:
                   1254:        for (path = x_sockets; *path; ++path) {
                   1255:                sock = socket(AF_UNIX, SOCK_STREAM, 0);
                   1256:                if (sock < 0)
                   1257:                        error("socket: %.100s", strerror(errno));
                   1258:                memset(&addr, 0, sizeof(addr));
                   1259:                addr.sun_family = AF_UNIX;
                   1260:                snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
                   1261:                if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
                   1262:                        return sock;
                   1263:                close(sock);
                   1264:        }
                   1265:        error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
                   1266:        return -1;
1.1       deraadt  1267: }
                   1268:
                   1269:
1.27      markus   1270: /*
                   1271:  * This is called when SSH_SMSG_X11_OPEN is received.  The packet contains
                   1272:  * the remote channel number.  We should do whatever we want, and respond
                   1273:  * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
                   1274:  */
1.1       deraadt  1275:
1.25      markus   1276: void
                   1277: x11_input_open(int payload_len)
1.1       deraadt  1278: {
1.35    ! markus   1279:        int remote_channel, display_number, sock = 0, newch;
1.25      markus   1280:        const char *display;
                   1281:        char buf[1024], *cp, *remote_host;
                   1282:        int remote_len;
1.35    ! markus   1283:        struct addrinfo hints, *ai, *aitop;
        !          1284:        char strport[NI_MAXSERV];
        !          1285:        int gaierr;
1.25      markus   1286:
                   1287:        /* Get remote channel number. */
                   1288:        remote_channel = packet_get_int();
                   1289:
                   1290:        /* Get remote originator name. */
1.29      markus   1291:        if (have_hostname_in_open) {
1.25      markus   1292:                remote_host = packet_get_string(&remote_len);
1.29      markus   1293:                remote_len += 4;
                   1294:        } else {
1.25      markus   1295:                remote_host = xstrdup("unknown (remote did not supply name)");
1.29      markus   1296:                remote_len = 0;
                   1297:        }
1.25      markus   1298:
                   1299:        debug("Received X11 open request.");
1.29      markus   1300:        packet_integrity_check(payload_len, 4 + remote_len, SSH_SMSG_X11_OPEN);
1.25      markus   1301:
                   1302:        /* Try to open a socket for the local X server. */
                   1303:        display = getenv("DISPLAY");
                   1304:        if (!display) {
                   1305:                error("DISPLAY not set.");
                   1306:                goto fail;
                   1307:        }
1.27      markus   1308:        /*
                   1309:         * Now we decode the value of the DISPLAY variable and make a
                   1310:         * connection to the real X server.
                   1311:         */
                   1312:
                   1313:        /*
                   1314:         * Check if it is a unix domain socket.  Unix domain displays are in
                   1315:         * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
                   1316:         */
1.25      markus   1317:        if (strncmp(display, "unix:", 5) == 0 ||
                   1318:            display[0] == ':') {
                   1319:                /* Connect to the unix domain socket. */
                   1320:                if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
                   1321:                        error("Could not parse display number from DISPLAY: %.100s",
                   1322:                              display);
                   1323:                        goto fail;
                   1324:                }
                   1325:                /* Create a socket. */
                   1326:                sock = connect_local_xsocket(display_number);
                   1327:                if (sock < 0)
                   1328:                        goto fail;
                   1329:
                   1330:                /* OK, we now have a connection to the display. */
                   1331:                goto success;
                   1332:        }
1.27      markus   1333:        /*
                   1334:         * Connect to an inet socket.  The DISPLAY value is supposedly
                   1335:         * hostname:d[.s], where hostname may also be numeric IP address.
                   1336:         */
1.25      markus   1337:        strncpy(buf, display, sizeof(buf));
                   1338:        buf[sizeof(buf) - 1] = 0;
                   1339:        cp = strchr(buf, ':');
                   1340:        if (!cp) {
                   1341:                error("Could not find ':' in DISPLAY: %.100s", display);
                   1342:                goto fail;
                   1343:        }
                   1344:        *cp = 0;
1.27      markus   1345:        /* buf now contains the host name.  But first we parse the display number. */
1.25      markus   1346:        if (sscanf(cp + 1, "%d", &display_number) != 1) {
                   1347:                error("Could not parse display number from DISPLAY: %.100s",
                   1348:                      display);
                   1349:                goto fail;
                   1350:        }
1.35    ! markus   1351:
        !          1352:        /* Look up the host address */
        !          1353:        memset(&hints, 0, sizeof(hints));
        !          1354:        hints.ai_family = IPv4or6;
        !          1355:        hints.ai_socktype = SOCK_STREAM;
        !          1356:        snprintf(strport, sizeof strport, "%d", 6000 + display_number);
        !          1357:        if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
        !          1358:                error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
1.25      markus   1359:                goto fail;
                   1360:        }
1.35    ! markus   1361:        for (ai = aitop; ai; ai = ai->ai_next) {
        !          1362:                /* Create a socket. */
        !          1363:                sock = socket(ai->ai_family, SOCK_STREAM, 0);
        !          1364:                if (sock < 0) {
        !          1365:                        debug("socket: %.100s", strerror(errno));
        !          1366:                continue;
        !          1367:        }
1.25      markus   1368:        /* Connect it to the display. */
1.35    ! markus   1369:        if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
        !          1370:                debug("connect %.100s port %d: %.100s", buf, 6000 + display_number,
        !          1371:                    strerror(errno));
1.25      markus   1372:                close(sock);
1.35    ! markus   1373:                continue;
        !          1374:        }
        !          1375:        /* Success */
        !          1376:        break;
        !          1377:
        !          1378:        } /* (ai = aitop, ai; ai = ai->ai_next) */
        !          1379:        freeaddrinfo(aitop);
        !          1380:        if (!ai) {
        !          1381:                error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
        !          1382:                    strerror(errno));
1.25      markus   1383:                goto fail;
                   1384:        }
                   1385: success:
                   1386:        /* We have successfully obtained a connection to the real X display. */
                   1387:
                   1388:        /* Allocate a channel for this connection. */
                   1389:        if (x11_saved_proto == NULL)
                   1390:                newch = channel_allocate(SSH_CHANNEL_OPEN, sock, remote_host);
                   1391:        else
                   1392:                newch = channel_allocate(SSH_CHANNEL_X11_OPEN, sock, remote_host);
                   1393:        channels[newch].remote_id = remote_channel;
                   1394:
                   1395:        /* Send a confirmation to the remote host. */
                   1396:        packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
                   1397:        packet_put_int(remote_channel);
                   1398:        packet_put_int(newch);
                   1399:        packet_send();
                   1400:
                   1401:        return;
                   1402:
                   1403: fail:
                   1404:        /* Send refusal to the remote host. */
                   1405:        packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
                   1406:        packet_put_int(remote_channel);
                   1407:        packet_send();
1.1       deraadt  1408: }
                   1409:
1.27      markus   1410: /*
                   1411:  * Requests forwarding of X11 connections, generates fake authentication
                   1412:  * data, and enables authentication spoofing.
                   1413:  */
1.1       deraadt  1414:
1.25      markus   1415: void
                   1416: x11_request_forwarding_with_spoofing(const char *proto, const char *data)
1.1       deraadt  1417: {
1.25      markus   1418:        unsigned int data_len = (unsigned int) strlen(data) / 2;
                   1419:        unsigned int i, value;
                   1420:        char *new_data;
                   1421:        int screen_number;
                   1422:        const char *cp;
                   1423:        u_int32_t rand = 0;
                   1424:
                   1425:        cp = getenv("DISPLAY");
                   1426:        if (cp)
                   1427:                cp = strchr(cp, ':');
                   1428:        if (cp)
                   1429:                cp = strchr(cp, '.');
                   1430:        if (cp)
                   1431:                screen_number = atoi(cp + 1);
                   1432:        else
                   1433:                screen_number = 0;
                   1434:
                   1435:        /* Save protocol name. */
                   1436:        x11_saved_proto = xstrdup(proto);
                   1437:
1.27      markus   1438:        /*
                   1439:         * Extract real authentication data and generate fake data of the
                   1440:         * same length.
                   1441:         */
1.25      markus   1442:        x11_saved_data = xmalloc(data_len);
                   1443:        x11_fake_data = xmalloc(data_len);
                   1444:        for (i = 0; i < data_len; i++) {
                   1445:                if (sscanf(data + 2 * i, "%2x", &value) != 1)
                   1446:                        fatal("x11_request_forwarding: bad authentication data: %.100s", data);
                   1447:                if (i % 4 == 0)
                   1448:                        rand = arc4random();
                   1449:                x11_saved_data[i] = value;
                   1450:                x11_fake_data[i] = rand & 0xff;
                   1451:                rand >>= 8;
                   1452:        }
                   1453:        x11_saved_data_len = data_len;
                   1454:        x11_fake_data_len = data_len;
                   1455:
                   1456:        /* Convert the fake data into hex. */
                   1457:        new_data = xmalloc(2 * data_len + 1);
                   1458:        for (i = 0; i < data_len; i++)
                   1459:                sprintf(new_data + 2 * i, "%02x", (unsigned char) x11_fake_data[i]);
                   1460:
                   1461:        /* Send the request packet. */
                   1462:        packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
                   1463:        packet_put_string(proto, strlen(proto));
                   1464:        packet_put_string(new_data, strlen(new_data));
                   1465:        packet_put_int(screen_number);
                   1466:        packet_send();
                   1467:        packet_write_wait();
                   1468:        xfree(new_data);
1.1       deraadt  1469: }
                   1470:
                   1471: /* Sends a message to the server to request authentication fd forwarding. */
                   1472:
1.25      markus   1473: void
                   1474: auth_request_forwarding()
1.1       deraadt  1475: {
1.25      markus   1476:        packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
                   1477:        packet_send();
                   1478:        packet_write_wait();
1.1       deraadt  1479: }
                   1480:
1.27      markus   1481: /*
                   1482:  * Returns the name of the forwarded authentication socket.  Returns NULL if
                   1483:  * there is no forwarded authentication socket.  The returned value points to
                   1484:  * a static buffer.
                   1485:  */
1.1       deraadt  1486:
1.25      markus   1487: char *
                   1488: auth_get_socket_name()
1.1       deraadt  1489: {
1.25      markus   1490:        return channel_forwarded_auth_socket_name;
1.1       deraadt  1491: }
                   1492:
1.12      markus   1493: /* removes the agent forwarding socket */
                   1494:
1.25      markus   1495: void
                   1496: cleanup_socket(void)
                   1497: {
                   1498:        remove(channel_forwarded_auth_socket_name);
                   1499:        rmdir(channel_forwarded_auth_socket_dir);
1.12      markus   1500: }
                   1501:
1.27      markus   1502: /*
                   1503:  * This if called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
                   1504:  * This starts forwarding authentication requests.
                   1505:  */
1.1       deraadt  1506:
1.25      markus   1507: void
                   1508: auth_input_request_forwarding(struct passwd * pw)
1.1       deraadt  1509: {
1.25      markus   1510:        int sock, newch;
                   1511:        struct sockaddr_un sunaddr;
                   1512:
                   1513:        if (auth_get_socket_name() != NULL)
                   1514:                fatal("Protocol error: authentication forwarding requested twice.");
                   1515:
                   1516:        /* Temporarily drop privileged uid for mkdir/bind. */
                   1517:        temporarily_use_uid(pw->pw_uid);
                   1518:
                   1519:        /* Allocate a buffer for the socket name, and format the name. */
                   1520:        channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
                   1521:        channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
                   1522:        strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
                   1523:
                   1524:        /* Create private directory for socket */
                   1525:        if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL)
                   1526:                packet_disconnect("mkdtemp: %.100s", strerror(errno));
                   1527:        snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
                   1528:                 channel_forwarded_auth_socket_dir, (int) getpid());
                   1529:
                   1530:        if (atexit(cleanup_socket) < 0) {
                   1531:                int saved = errno;
                   1532:                cleanup_socket();
                   1533:                packet_disconnect("socket: %.100s", strerror(saved));
                   1534:        }
                   1535:        /* Create the socket. */
                   1536:        sock = socket(AF_UNIX, SOCK_STREAM, 0);
                   1537:        if (sock < 0)
                   1538:                packet_disconnect("socket: %.100s", strerror(errno));
                   1539:
                   1540:        /* Bind it to the name. */
                   1541:        memset(&sunaddr, 0, sizeof(sunaddr));
                   1542:        sunaddr.sun_family = AF_UNIX;
                   1543:        strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
                   1544:                sizeof(sunaddr.sun_path));
                   1545:
                   1546:        if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
                   1547:                packet_disconnect("bind: %.100s", strerror(errno));
                   1548:
                   1549:        /* Restore the privileged uid. */
                   1550:        restore_uid();
                   1551:
                   1552:        /* Start listening on the socket. */
                   1553:        if (listen(sock, 5) < 0)
                   1554:                packet_disconnect("listen: %.100s", strerror(errno));
                   1555:
                   1556:        /* Allocate a channel for the authentication agent socket. */
                   1557:        newch = channel_allocate(SSH_CHANNEL_AUTH_SOCKET, sock,
                   1558:                                 xstrdup("auth socket"));
1.32      deraadt  1559:        strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
                   1560:            sizeof(channels[newch].path));
1.1       deraadt  1561: }
                   1562:
                   1563: /* This is called to process an SSH_SMSG_AGENT_OPEN message. */
                   1564:
1.25      markus   1565: void
                   1566: auth_input_open_request()
1.1       deraadt  1567: {
1.25      markus   1568:        int remch, sock, newch;
                   1569:        char *dummyname;
                   1570:
                   1571:        /* Read the remote channel number from the message. */
                   1572:        remch = packet_get_int();
                   1573:
1.27      markus   1574:        /*
                   1575:         * Get a connection to the local authentication agent (this may again
                   1576:         * get forwarded).
                   1577:         */
1.25      markus   1578:        sock = ssh_get_authentication_socket();
                   1579:
1.27      markus   1580:        /*
                   1581:         * If we could not connect the agent, send an error message back to
                   1582:         * the server. This should never happen unless the agent dies,
                   1583:         * because authentication forwarding is only enabled if we have an
                   1584:         * agent.
                   1585:         */
1.25      markus   1586:        if (sock < 0) {
                   1587:                packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
                   1588:                packet_put_int(remch);
                   1589:                packet_send();
                   1590:                return;
                   1591:        }
                   1592:        debug("Forwarding authentication connection.");
1.1       deraadt  1593:
1.27      markus   1594:        /*
                   1595:         * Dummy host name.  This will be freed when the channel is freed; it
                   1596:         * will still be valid in the packet_put_string below since the
                   1597:         * channel cannot yet be freed at that point.
                   1598:         */
1.25      markus   1599:        dummyname = xstrdup("authentication agent connection");
                   1600:
                   1601:        newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
                   1602:        channels[newch].remote_id = remch;
                   1603:
                   1604:        /* Send a confirmation to the remote host. */
                   1605:        packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
                   1606:        packet_put_int(remch);
                   1607:        packet_put_int(newch);
                   1608:        packet_send();
1.1       deraadt  1609: }