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

Annotation of src/usr.bin/ssh/clientloop.c, Revision 1.43

1.1       deraadt     1: /*
1.12      deraadt     2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
1.33      deraadt     5:  * The main loop for the interactive session (client side).
1.20      markus      6:  *
1.33      deraadt     7:  * As far as I am concerned, the code I have written for this software
                      8:  * can be used freely for any purpose.  Any derived versions of this
                      9:  * software must be clearly marked as such, and if the derived work is
                     10:  * incompatible with the protocol description in the RFC file, it must be
                     11:  * called by a name other than "ssh" or "Secure Shell".
                     12:  *
                     13:  *
                     14:  * Copyright (c) 1999 Theo de Raadt.  All rights reserved.
                     15:  *
                     16:  * Redistribution and use in source and binary forms, with or without
                     17:  * modification, are permitted provided that the following conditions
                     18:  * are met:
                     19:  * 1. Redistributions of source code must retain the above copyright
                     20:  *    notice, this list of conditions and the following disclaimer.
                     21:  * 2. Redistributions in binary form must reproduce the above copyright
                     22:  *    notice, this list of conditions and the following disclaimer in the
                     23:  *    documentation and/or other materials provided with the distribution.
                     24:  *
                     25:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     26:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     27:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     28:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     29:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     30:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     31:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     32:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     33:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     34:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.20      markus     35:  *
                     36:  *
1.33      deraadt    37:  * SSH2 support added by Markus Friedl.
                     38:  * Copyright (c) 1999,2000 Markus Friedl.  All rights reserved.
1.20      markus     39:  *
1.33      deraadt    40:  * Redistribution and use in source and binary forms, with or without
                     41:  * modification, are permitted provided that the following conditions
                     42:  * are met:
                     43:  * 1. Redistributions of source code must retain the above copyright
                     44:  *    notice, this list of conditions and the following disclaimer.
                     45:  * 2. Redistributions in binary form must reproduce the above copyright
                     46:  *    notice, this list of conditions and the following disclaimer in the
                     47:  *    documentation and/or other materials provided with the distribution.
                     48:  *
                     49:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     50:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     51:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     52:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     53:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     54:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     55:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     56:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     57:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     58:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.12      deraadt    59:  */
1.1       deraadt    60:
                     61: #include "includes.h"
1.43    ! markus     62: RCSID("$OpenBSD: clientloop.c,v 1.42 2000/12/19 23:17:56 markus Exp $");
1.1       deraadt    63:
                     64: #include "xmalloc.h"
                     65: #include "ssh.h"
                     66: #include "packet.h"
                     67: #include "buffer.h"
1.8       markus     68: #include "readconf.h"
1.1       deraadt    69:
1.16      markus     70: #include "ssh2.h"
1.15      markus     71: #include "compat.h"
                     72: #include "channels.h"
                     73: #include "dispatch.h"
                     74:
1.30      markus     75: #include "buffer.h"
                     76: #include "bufaux.h"
                     77:
1.40      markus     78: #include <openssl/dsa.h>
                     79: #include <openssl/rsa.h>
                     80: #include "key.h"
                     81: #include "authfd.h"
1.43    ! markus     82: #include "clientloop.h"
1.39      markus     83:
                     84: /* import options */
                     85: extern Options options;
                     86:
1.1       deraadt    87: /* Flag indicating that stdin should be redirected from /dev/null. */
                     88: extern int stdin_null_flag;
                     89:
1.13      markus     90: /*
                     91:  * Name of the host we are connecting to.  This is the name given on the
                     92:  * command line, or the HostName specified for the user-supplied name in a
                     93:  * configuration file.
                     94:  */
1.1       deraadt    95: extern char *host;
                     96:
1.13      markus     97: /*
                     98:  * Flag to indicate that we have received a window change signal which has
                     99:  * not yet been processed.  This will cause a message indicating the new
                    100:  * window size to be sent to the server a little later.  This is volatile
                    101:  * because this is updated in a signal handler.
                    102:  */
1.1       deraadt   103: static volatile int received_window_change_signal = 0;
                    104:
                    105: /* Terminal modes, as saved by enter_raw_mode. */
                    106: static struct termios saved_tio;
                    107:
1.13      markus    108: /*
                    109:  * Flag indicating whether we are in raw mode.  This is used by
                    110:  * enter_raw_mode and leave_raw_mode.
                    111:  */
1.1       deraadt   112: static int in_raw_mode = 0;
                    113:
                    114: /* Flag indicating whether the user\'s terminal is in non-blocking mode. */
                    115: static int in_non_blocking_mode = 0;
                    116:
                    117: /* Common data for the client loop code. */
1.31      markus    118: static int quit_pending;       /* Set to non-zero to quit the client loop. */
                    119: static int escape_char;                /* Escape character. */
1.11      markus    120: static int escape_pending;     /* Last character was the escape character */
                    121: static int last_was_cr;                /* Last character was a newline. */
                    122: static int exit_status;                /* Used to store the exit status of the command. */
                    123: static int stdin_eof;          /* EOF has been encountered on standard error. */
                    124: static Buffer stdin_buffer;    /* Buffer for stdin data. */
                    125: static Buffer stdout_buffer;   /* Buffer for stdout data. */
                    126: static Buffer stderr_buffer;   /* Buffer for stderr data. */
1.42      markus    127: static u_long stdin_bytes, stdout_bytes, stderr_bytes;
                    128: static u_int buffer_high;/* Soft max buffer size. */
1.11      markus    129: static int max_fd;             /* Maximum file descriptor number in select(). */
                    130: static int connection_in;      /* Connection to server (input). */
                    131: static int connection_out;     /* Connection to server (output). */
1.1       deraadt   132:
1.16      markus    133:
                    134: void   client_init_dispatch(void);
                    135: int    session_ident = -1;
                    136:
1.13      markus    137: /* Returns the user\'s terminal to normal mode if it had been put in raw mode. */
1.1       deraadt   138:
1.20      markus    139: void
1.11      markus    140: leave_raw_mode()
1.1       deraadt   141: {
1.11      markus    142:        if (!in_raw_mode)
                    143:                return;
                    144:        in_raw_mode = 0;
                    145:        if (tcsetattr(fileno(stdin), TCSADRAIN, &saved_tio) < 0)
                    146:                perror("tcsetattr");
1.1       deraadt   147:
1.11      markus    148:        fatal_remove_cleanup((void (*) (void *)) leave_raw_mode, NULL);
1.1       deraadt   149: }
                    150:
                    151: /* Puts the user\'s terminal in raw mode. */
                    152:
1.20      markus    153: void
1.11      markus    154: enter_raw_mode()
1.1       deraadt   155: {
1.11      markus    156:        struct termios tio;
1.1       deraadt   157:
1.11      markus    158:        if (tcgetattr(fileno(stdin), &tio) < 0)
                    159:                perror("tcgetattr");
                    160:        saved_tio = tio;
                    161:        tio.c_iflag |= IGNPAR;
                    162:        tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
                    163:        tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
1.1       deraadt   164: #ifdef IEXTEN
1.11      markus    165:        tio.c_lflag &= ~IEXTEN;
                    166: #endif                         /* IEXTEN */
                    167:        tio.c_oflag &= ~OPOST;
                    168:        tio.c_cc[VMIN] = 1;
                    169:        tio.c_cc[VTIME] = 0;
                    170:        if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) < 0)
                    171:                perror("tcsetattr");
                    172:        in_raw_mode = 1;
1.1       deraadt   173:
1.11      markus    174:        fatal_add_cleanup((void (*) (void *)) leave_raw_mode, NULL);
                    175: }
1.1       deraadt   176:
                    177: /* Restores stdin to blocking mode. */
                    178:
1.20      markus    179: void
1.11      markus    180: leave_non_blocking()
1.1       deraadt   181: {
1.11      markus    182:        if (in_non_blocking_mode) {
                    183:                (void) fcntl(fileno(stdin), F_SETFL, 0);
                    184:                in_non_blocking_mode = 0;
                    185:                fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL);
                    186:        }
1.1       deraadt   187: }
                    188:
1.11      markus    189: /* Puts stdin terminal in non-blocking mode. */
                    190:
1.20      markus    191: void
1.11      markus    192: enter_non_blocking()
1.1       deraadt   193: {
1.11      markus    194:        in_non_blocking_mode = 1;
                    195:        (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
                    196:        fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL);
1.1       deraadt   197: }
                    198:
1.13      markus    199: /*
                    200:  * Signal handler for the window change signal (SIGWINCH).  This just sets a
                    201:  * flag indicating that the window has changed.
                    202:  */
1.1       deraadt   203:
1.20      markus    204: void
1.11      markus    205: window_change_handler(int sig)
1.1       deraadt   206: {
1.11      markus    207:        received_window_change_signal = 1;
                    208:        signal(SIGWINCH, window_change_handler);
1.1       deraadt   209: }
                    210:
1.13      markus    211: /*
                    212:  * Signal handler for signals that cause the program to terminate.  These
                    213:  * signals must be trapped to restore terminal modes.
                    214:  */
1.1       deraadt   215:
1.20      markus    216: void
1.11      markus    217: signal_handler(int sig)
1.1       deraadt   218: {
1.11      markus    219:        if (in_raw_mode)
                    220:                leave_raw_mode();
                    221:        if (in_non_blocking_mode)
                    222:                leave_non_blocking();
                    223:        channel_stop_listening();
                    224:        packet_close();
                    225:        fatal("Killed by signal %d.", sig);
1.1       deraadt   226: }
                    227:
1.13      markus    228: /*
                    229:  * Returns current time in seconds from Jan 1, 1970 with the maximum
                    230:  * available resolution.
                    231:  */
1.1       deraadt   232:
1.20      markus    233: double
1.11      markus    234: get_current_time()
1.1       deraadt   235: {
1.11      markus    236:        struct timeval tv;
                    237:        gettimeofday(&tv, NULL);
                    238:        return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
1.1       deraadt   239: }
                    240:
1.13      markus    241: /*
                    242:  * This is called when the interactive is entered.  This checks if there is
                    243:  * an EOF coming on stdin.  We must check this explicitly, as select() does
                    244:  * not appear to wake up when redirecting from /dev/null.
                    245:  */
1.1       deraadt   246:
1.20      markus    247: void
1.11      markus    248: client_check_initial_eof_on_stdin()
1.1       deraadt   249: {
1.11      markus    250:        int len;
                    251:        char buf[1];
1.1       deraadt   252:
1.13      markus    253:        /*
                    254:         * If standard input is to be "redirected from /dev/null", we simply
                    255:         * mark that we have seen an EOF and send an EOF message to the
                    256:         * server. Otherwise, we try to read a single character; it appears
                    257:         * that for some files, such /dev/null, select() never wakes up for
                    258:         * read for this descriptor, which means that we never get EOF.  This
                    259:         * way we will get the EOF if stdin comes from /dev/null or similar.
                    260:         */
1.11      markus    261:        if (stdin_null_flag) {
                    262:                /* Fake EOF on stdin. */
                    263:                debug("Sending eof.");
                    264:                stdin_eof = 1;
                    265:                packet_start(SSH_CMSG_EOF);
                    266:                packet_send();
                    267:        } else {
                    268:                enter_non_blocking();
                    269:
                    270:                /* Check for immediate EOF on stdin. */
                    271:                len = read(fileno(stdin), buf, 1);
                    272:                if (len == 0) {
1.13      markus    273:                        /* EOF.  Record that we have seen it and send EOF to server. */
1.11      markus    274:                        debug("Sending eof.");
                    275:                        stdin_eof = 1;
                    276:                        packet_start(SSH_CMSG_EOF);
                    277:                        packet_send();
                    278:                } else if (len > 0) {
1.13      markus    279:                        /*
                    280:                         * Got data.  We must store the data in the buffer,
                    281:                         * and also process it as an escape character if
                    282:                         * appropriate.
                    283:                         */
1.42      markus    284:                        if ((u_char) buf[0] == escape_char)
1.11      markus    285:                                escape_pending = 1;
                    286:                        else {
                    287:                                buffer_append(&stdin_buffer, buf, 1);
                    288:                                stdin_bytes += 1;
                    289:                        }
                    290:                }
                    291:                leave_non_blocking();
                    292:        }
1.1       deraadt   293: }
                    294:
                    295:
1.13      markus    296: /*
                    297:  * Make packets from buffered stdin data, and buffer them for sending to the
                    298:  * connection.
                    299:  */
1.1       deraadt   300:
1.20      markus    301: void
1.11      markus    302: client_make_packets_from_stdin_data()
1.1       deraadt   303: {
1.42      markus    304:        u_int len;
1.1       deraadt   305:
1.11      markus    306:        /* Send buffered stdin data to the server. */
                    307:        while (buffer_len(&stdin_buffer) > 0 &&
                    308:               packet_not_very_much_data_to_write()) {
                    309:                len = buffer_len(&stdin_buffer);
                    310:                /* Keep the packets at reasonable size. */
                    311:                if (len > packet_get_maxsize())
                    312:                        len = packet_get_maxsize();
                    313:                packet_start(SSH_CMSG_STDIN_DATA);
                    314:                packet_put_string(buffer_ptr(&stdin_buffer), len);
                    315:                packet_send();
                    316:                buffer_consume(&stdin_buffer, len);
                    317:                /* If we have a pending EOF, send it now. */
                    318:                if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
                    319:                        packet_start(SSH_CMSG_EOF);
                    320:                        packet_send();
                    321:                }
1.1       deraadt   322:        }
                    323: }
                    324:
1.13      markus    325: /*
                    326:  * Checks if the client window has changed, and sends a packet about it to
                    327:  * the server if so.  The actual change is detected elsewhere (by a software
                    328:  * interrupt on Unix); this just checks the flag and sends a message if
                    329:  * appropriate.
                    330:  */
1.1       deraadt   331:
1.20      markus    332: void
1.11      markus    333: client_check_window_change()
1.1       deraadt   334: {
1.16      markus    335:        struct winsize ws;
                    336:
                    337:        if (! received_window_change_signal)
                    338:                return;
                    339:        /** XXX race */
                    340:        received_window_change_signal = 0;
                    341:
                    342:        if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
                    343:                return;
                    344:
1.37      markus    345:        debug2("client_check_window_change: changed");
1.16      markus    346:
                    347:        if (compat20) {
                    348:                channel_request_start(session_ident, "window-change", 0);
                    349:                packet_put_int(ws.ws_col);
                    350:                packet_put_int(ws.ws_row);
                    351:                packet_put_int(ws.ws_xpixel);
                    352:                packet_put_int(ws.ws_ypixel);
                    353:                packet_send();
                    354:        } else {
                    355:                packet_start(SSH_CMSG_WINDOW_SIZE);
                    356:                packet_put_int(ws.ws_row);
                    357:                packet_put_int(ws.ws_col);
                    358:                packet_put_int(ws.ws_xpixel);
                    359:                packet_put_int(ws.ws_ypixel);
                    360:                packet_send();
1.1       deraadt   361:        }
                    362: }
                    363:
1.13      markus    364: /*
                    365:  * Waits until the client can do something (some data becomes available on
                    366:  * one of the file descriptors).
                    367:  */
1.1       deraadt   368:
1.20      markus    369: void
1.11      markus    370: client_wait_until_can_do_something(fd_set * readset, fd_set * writeset)
                    371: {
                    372:        /* Initialize select masks. */
                    373:        FD_ZERO(readset);
1.17      markus    374:        FD_ZERO(writeset);
1.11      markus    375:
1.16      markus    376:        if (!compat20) {
1.17      markus    377:                /* Read from the connection, unless our buffers are full. */
1.16      markus    378:                if (buffer_len(&stdout_buffer) < buffer_high &&
                    379:                    buffer_len(&stderr_buffer) < buffer_high &&
                    380:                    channel_not_very_much_buffered_data())
                    381:                        FD_SET(connection_in, readset);
1.17      markus    382:                /*
                    383:                 * Read from stdin, unless we have seen EOF or have very much
                    384:                 * buffered data to send to the server.
                    385:                 */
                    386:                if (!stdin_eof && packet_not_very_much_data_to_write())
                    387:                        FD_SET(fileno(stdin), readset);
                    388:
                    389:                /* Select stdout/stderr if have data in buffer. */
                    390:                if (buffer_len(&stdout_buffer) > 0)
                    391:                        FD_SET(fileno(stdout), writeset);
                    392:                if (buffer_len(&stderr_buffer) > 0)
                    393:                        FD_SET(fileno(stderr), writeset);
1.16      markus    394:        } else {
1.11      markus    395:                FD_SET(connection_in, readset);
1.16      markus    396:        }
1.11      markus    397:
                    398:        /* Add any selections by the channel mechanism. */
                    399:        channel_prepare_select(readset, writeset);
                    400:
                    401:        /* Select server connection if have data to write to the server. */
                    402:        if (packet_have_data_to_write())
                    403:                FD_SET(connection_out, writeset);
                    404:
1.16      markus    405: /* move UP XXX */
1.11      markus    406:        /* Update maximum file descriptor number, if appropriate. */
                    407:        if (channel_max_fd() > max_fd)
                    408:                max_fd = channel_max_fd();
                    409:
1.13      markus    410:        /*
                    411:         * Wait for something to happen.  This will suspend the process until
                    412:         * some selected descriptor can be read, written, or has some other
                    413:         * event pending. Note: if you want to implement SSH_MSG_IGNORE
                    414:         * messages to fool traffic analysis, this might be the place to do
                    415:         * it: just have a random timeout for the select, and send a random
                    416:         * SSH_MSG_IGNORE packet when the timeout expires.
                    417:         */
1.11      markus    418:
                    419:        if (select(max_fd + 1, readset, writeset, NULL, NULL) < 0) {
                    420:                char buf[100];
                    421:                /* Some systems fail to clear these automatically. */
                    422:                FD_ZERO(readset);
                    423:                FD_ZERO(writeset);
                    424:                if (errno == EINTR)
                    425:                        return;
                    426:                /* Note: we might still have data in the buffers. */
                    427:                snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
                    428:                buffer_append(&stderr_buffer, buf, strlen(buf));
                    429:                stderr_bytes += strlen(buf);
                    430:                quit_pending = 1;
                    431:        }
                    432: }
                    433:
1.20      markus    434: void
1.31      markus    435: client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
1.1       deraadt   436: {
1.11      markus    437:        struct winsize oldws, newws;
                    438:
                    439:        /* Flush stdout and stderr buffers. */
1.31      markus    440:        if (buffer_len(bout) > 0)
                    441:                atomicio(write, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
                    442:        if (buffer_len(berr) > 0)
                    443:                atomicio(write, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
1.11      markus    444:
                    445:        leave_raw_mode();
                    446:
1.13      markus    447:        /*
                    448:         * Free (and clear) the buffer to reduce the amount of data that gets
                    449:         * written to swap.
                    450:         */
1.31      markus    451:        buffer_free(bin);
                    452:        buffer_free(bout);
                    453:        buffer_free(berr);
1.11      markus    454:
                    455:        /* Save old window size. */
                    456:        ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
                    457:
                    458:        /* Send the suspend signal to the program itself. */
                    459:        kill(getpid(), SIGTSTP);
                    460:
                    461:        /* Check if the window size has changed. */
                    462:        if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
                    463:            (oldws.ws_row != newws.ws_row ||
                    464:             oldws.ws_col != newws.ws_col ||
                    465:             oldws.ws_xpixel != newws.ws_xpixel ||
                    466:             oldws.ws_ypixel != newws.ws_ypixel))
                    467:                received_window_change_signal = 1;
                    468:
                    469:        /* OK, we have been continued by the user. Reinitialize buffers. */
1.31      markus    470:        buffer_init(bin);
                    471:        buffer_init(bout);
                    472:        buffer_init(berr);
1.11      markus    473:
                    474:        enter_raw_mode();
                    475: }
                    476:
1.20      markus    477: void
1.17      markus    478: client_process_net_input(fd_set * readset)
1.11      markus    479: {
1.17      markus    480:        int len;
                    481:        char buf[8192];
1.11      markus    482:
1.13      markus    483:        /*
                    484:         * Read input from the server, and add any such data to the buffer of
                    485:         * the packet subsystem.
                    486:         */
1.11      markus    487:        if (FD_ISSET(connection_in, readset)) {
                    488:                /* Read as much as possible. */
                    489:                len = read(connection_in, buf, sizeof(buf));
                    490:                if (len == 0) {
                    491:                        /* Received EOF.  The remote host has closed the connection. */
                    492:                        snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
                    493:                                 host);
1.1       deraadt   494:                        buffer_append(&stderr_buffer, buf, strlen(buf));
                    495:                        stderr_bytes += strlen(buf);
                    496:                        quit_pending = 1;
                    497:                        return;
1.11      markus    498:                }
1.13      markus    499:                /*
                    500:                 * There is a kernel bug on Solaris that causes select to
                    501:                 * sometimes wake up even though there is no data available.
                    502:                 */
1.11      markus    503:                if (len < 0 && errno == EAGAIN)
                    504:                        len = 0;
                    505:
                    506:                if (len < 0) {
                    507:                        /* An error has encountered.  Perhaps there is a network problem. */
                    508:                        snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
                    509:                                 host, strerror(errno));
                    510:                        buffer_append(&stderr_buffer, buf, strlen(buf));
                    511:                        stderr_bytes += strlen(buf);
                    512:                        quit_pending = 1;
                    513:                        return;
                    514:                }
                    515:                packet_process_incoming(buf, len);
                    516:        }
1.17      markus    517: }
1.16      markus    518:
1.31      markus    519: /* process the characters one by one */
                    520: int
                    521: process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
                    522: {
1.32      markus    523:        char string[1024];
1.31      markus    524:        pid_t pid;
                    525:        int bytes = 0;
1.42      markus    526:        u_int i;
                    527:        u_char ch;
1.31      markus    528:        char *s;
                    529:
                    530:        for (i = 0; i < len; i++) {
                    531:                /* Get one character at a time. */
                    532:                ch = buf[i];
                    533:
                    534:                if (escape_pending) {
                    535:                        /* We have previously seen an escape character. */
                    536:                        /* Clear the flag now. */
                    537:                        escape_pending = 0;
                    538:
                    539:                        /* Process the escaped character. */
                    540:                        switch (ch) {
                    541:                        case '.':
                    542:                                /* Terminate the connection. */
1.32      markus    543:                                snprintf(string, sizeof string, "%c.\r\n", escape_char);
                    544:                                buffer_append(berr, string, strlen(string));
                    545:                                /*stderr_bytes += strlen(string); XXX*/
1.31      markus    546:
                    547:                                quit_pending = 1;
                    548:                                return -1;
                    549:
                    550:                        case 'Z' - 64:
                    551:                                /* Suspend the program. */
                    552:                                /* Print a message to that effect to the user. */
1.32      markus    553:                                snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
                    554:                                buffer_append(berr, string, strlen(string));
                    555:                                /*stderr_bytes += strlen(string); XXX*/
1.31      markus    556:
                    557:                                /* Restore terminal modes and suspend. */
                    558:                                client_suspend_self(bin, bout, berr);
                    559:
                    560:                                /* We have been continued. */
                    561:                                continue;
                    562:
                    563:                        case '&':
                    564:                                /* XXX does not work yet with proto 2 */
                    565:                                if (compat20)
                    566:                                        continue;
                    567:                                /*
                    568:                                 * Detach the program (continue to serve connections,
                    569:                                 * but put in background and no more new connections).
                    570:                                 */
                    571:                                if (!stdin_eof) {
                    572:                                        /*
                    573:                                         * Sending SSH_CMSG_EOF alone does not always appear
                    574:                                         * to be enough.  So we try to send an EOF character
                    575:                                         * first.
                    576:                                         */
                    577:                                        packet_start(SSH_CMSG_STDIN_DATA);
                    578:                                        packet_put_string("\004", 1);
                    579:                                        packet_send();
                    580:                                        /* Close stdin. */
                    581:                                        stdin_eof = 1;
                    582:                                        if (buffer_len(bin) == 0) {
                    583:                                                packet_start(SSH_CMSG_EOF);
                    584:                                                packet_send();
                    585:                                        }
                    586:                                }
                    587:                                /* Restore tty modes. */
                    588:                                leave_raw_mode();
                    589:
                    590:                                /* Stop listening for new connections. */
                    591:                                channel_stop_listening();
                    592:
                    593:                                printf("%c& [backgrounded]\n", escape_char);
                    594:
                    595:                                /* Fork into background. */
                    596:                                pid = fork();
                    597:                                if (pid < 0) {
                    598:                                        error("fork: %.100s", strerror(errno));
                    599:                                        continue;
                    600:                                }
                    601:                                if (pid != 0) { /* This is the parent. */
                    602:                                        /* The parent just exits. */
                    603:                                        exit(0);
                    604:                                }
                    605:                                /* The child continues serving connections. */
                    606:                                continue; /*XXX ? */
                    607:
                    608:                        case '?':
1.32      markus    609:                                snprintf(string, sizeof string,
1.31      markus    610: "%c?\r\n\
                    611: Supported escape sequences:\r\n\
                    612: ~.  - terminate connection\r\n\
                    613: ~^Z - suspend ssh\r\n\
                    614: ~#  - list forwarded connections\r\n\
                    615: ~&  - background ssh (when waiting for connections to terminate)\r\n\
                    616: ~?  - this message\r\n\
                    617: ~~  - send the escape character by typing it twice\r\n\
                    618: (Note that escapes are only recognized immediately after newline.)\r\n",
                    619:                                         escape_char);
1.32      markus    620:                                buffer_append(berr, string, strlen(string));
1.31      markus    621:                                continue;
                    622:
                    623:                        case '#':
1.32      markus    624:                                snprintf(string, sizeof string, "%c#\r\n", escape_char);
                    625:                                buffer_append(berr, string, strlen(string));
1.31      markus    626:                                s = channel_open_message();
                    627:                                buffer_append(berr, s, strlen(s));
                    628:                                xfree(s);
                    629:                                continue;
                    630:
                    631:                        default:
                    632:                                if (ch != escape_char) {
                    633:                                        buffer_put_char(bin, escape_char);
                    634:                                        bytes++;
                    635:                                }
                    636:                                /* Escaped characters fall through here */
                    637:                                break;
                    638:                        }
                    639:                } else {
                    640:                        /*
                    641:                         * The previous character was not an escape char. Check if this
                    642:                         * is an escape.
                    643:                         */
                    644:                        if (last_was_cr && ch == escape_char) {
                    645:                                /* It is. Set the flag and continue to next character. */
                    646:                                escape_pending = 1;
                    647:                                continue;
                    648:                        }
                    649:                }
                    650:
                    651:                /*
                    652:                 * Normal character.  Record whether it was a newline,
                    653:                 * and append it to the buffer.
                    654:                 */
                    655:                last_was_cr = (ch == '\r' || ch == '\n');
                    656:                buffer_put_char(bin, ch);
                    657:                bytes++;
                    658:        }
                    659:        return bytes;
                    660: }
                    661:
1.20      markus    662: void
1.17      markus    663: client_process_input(fd_set * readset)
                    664: {
1.31      markus    665:        int ret;
1.21      deraadt   666:        int len;
1.31      markus    667:        char buf[8192];
1.16      markus    668:
1.11      markus    669:        /* Read input from stdin. */
                    670:        if (FD_ISSET(fileno(stdin), readset)) {
                    671:                /* Read as much as possible. */
                    672:                len = read(fileno(stdin), buf, sizeof(buf));
                    673:                if (len <= 0) {
1.13      markus    674:                        /*
                    675:                         * Received EOF or error.  They are treated
                    676:                         * similarly, except that an error message is printed
                    677:                         * if it was an error condition.
                    678:                         */
1.11      markus    679:                        if (len < 0) {
                    680:                                snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
                    681:                                buffer_append(&stderr_buffer, buf, strlen(buf));
                    682:                                stderr_bytes += strlen(buf);
                    683:                        }
                    684:                        /* Mark that we have seen EOF. */
                    685:                        stdin_eof = 1;
1.13      markus    686:                        /*
                    687:                         * Send an EOF message to the server unless there is
                    688:                         * data in the buffer.  If there is data in the
                    689:                         * buffer, no message will be sent now.  Code
                    690:                         * elsewhere will send the EOF when the buffer
                    691:                         * becomes empty if stdin_eof is set.
                    692:                         */
1.11      markus    693:                        if (buffer_len(&stdin_buffer) == 0) {
1.1       deraadt   694:                                packet_start(SSH_CMSG_EOF);
                    695:                                packet_send();
1.11      markus    696:                        }
                    697:                } else if (escape_char == -1) {
1.13      markus    698:                        /*
                    699:                         * Normal successful read, and no escape character.
                    700:                         * Just append the data to buffer.
                    701:                         */
1.11      markus    702:                        buffer_append(&stdin_buffer, buf, len);
                    703:                        stdin_bytes += len;
                    704:                } else {
1.13      markus    705:                        /*
                    706:                         * Normal, successful read.  But we have an escape character
                    707:                         * and have to process the characters one by one.
                    708:                         */
1.31      markus    709:                        ret = process_escapes(&stdin_buffer, &stdout_buffer, &stderr_buffer, buf, len);
                    710:                        if (ret == -1)
                    711:                                return;
                    712:                        stdout_bytes += ret;
1.11      markus    713:                }
                    714:        }
                    715: }
                    716:
1.20      markus    717: void
1.11      markus    718: client_process_output(fd_set * writeset)
                    719: {
                    720:        int len;
                    721:        char buf[100];
1.1       deraadt   722:
1.11      markus    723:        /* Write buffered output to stdout. */
                    724:        if (FD_ISSET(fileno(stdout), writeset)) {
                    725:                /* Write as much data as possible. */
                    726:                len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
1.14      deraadt   727:                    buffer_len(&stdout_buffer));
1.11      markus    728:                if (len <= 0) {
                    729:                        if (errno == EAGAIN)
                    730:                                len = 0;
                    731:                        else {
1.13      markus    732:                                /*
                    733:                                 * An error or EOF was encountered.  Put an
                    734:                                 * error message to stderr buffer.
                    735:                                 */
1.11      markus    736:                                snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
                    737:                                buffer_append(&stderr_buffer, buf, strlen(buf));
                    738:                                stderr_bytes += strlen(buf);
                    739:                                quit_pending = 1;
                    740:                                return;
                    741:                        }
                    742:                }
                    743:                /* Consume printed data from the buffer. */
                    744:                buffer_consume(&stdout_buffer, len);
                    745:        }
                    746:        /* Write buffered output to stderr. */
                    747:        if (FD_ISSET(fileno(stderr), writeset)) {
                    748:                /* Write as much data as possible. */
                    749:                len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
1.14      deraadt   750:                    buffer_len(&stderr_buffer));
1.11      markus    751:                if (len <= 0) {
                    752:                        if (errno == EAGAIN)
                    753:                                len = 0;
                    754:                        else {
1.13      markus    755:                                /* EOF or error, but can't even print error message. */
1.11      markus    756:                                quit_pending = 1;
                    757:                                return;
                    758:                        }
                    759:                }
                    760:                /* Consume printed characters from the buffer. */
                    761:                buffer_consume(&stderr_buffer, len);
                    762:        }
1.1       deraadt   763: }
                    764:
1.13      markus    765: /*
1.15      markus    766:  * Get packets from the connection input buffer, and process them as long as
                    767:  * there are packets available.
                    768:  *
                    769:  * Any unknown packets received during the actual
                    770:  * session cause the session to terminate.  This is
                    771:  * intended to make debugging easier since no
                    772:  * confirmations are sent.  Any compatible protocol
                    773:  * extensions must be negotiated during the
                    774:  * preparatory phase.
                    775:  */
                    776:
1.20      markus    777: void
1.15      markus    778: client_process_buffered_input_packets()
                    779: {
1.36      markus    780:        dispatch_run(DISPATCH_NONBLOCK, &quit_pending, NULL);
1.15      markus    781: }
                    782:
1.31      markus    783: /* scan buf[] for '~' before sending data to the peer */
1.30      markus    784:
                    785: int
1.31      markus    786: simple_escape_filter(Channel *c, char *buf, int len)
1.30      markus    787: {
1.31      markus    788:        /* XXX we assume c->extended is writeable */
                    789:        return process_escapes(&c->input, &c->output, &c->extended, buf, len);
1.30      markus    790: }
                    791:
1.15      markus    792: /*
1.13      markus    793:  * Implements the interactive session with the server.  This is called after
                    794:  * the user has been authenticated, and a command has been started on the
                    795:  * remote host.  If escape_char != -1, it is the character used as an escape
                    796:  * character for terminating or suspending the session.
                    797:  */
1.1       deraadt   798:
1.20      markus    799: int
1.30      markus    800: client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
1.1       deraadt   801: {
1.11      markus    802:        double start_time, total_time;
                    803:        int len;
                    804:        char buf[100];
                    805:
                    806:        debug("Entering interactive session.");
                    807:
                    808:        start_time = get_current_time();
                    809:
                    810:        /* Initialize variables. */
                    811:        escape_pending = 0;
                    812:        last_was_cr = 1;
                    813:        exit_status = -1;
                    814:        stdin_eof = 0;
                    815:        buffer_high = 64 * 1024;
                    816:        connection_in = packet_get_connection_in();
                    817:        connection_out = packet_get_connection_out();
                    818:        max_fd = connection_in;
                    819:        if (connection_out > max_fd)
                    820:                max_fd = connection_out;
                    821:        stdin_bytes = 0;
                    822:        stdout_bytes = 0;
                    823:        stderr_bytes = 0;
                    824:        quit_pending = 0;
                    825:        escape_char = escape_char_arg;
                    826:
                    827:        /* Initialize buffers. */
                    828:        buffer_init(&stdin_buffer);
                    829:        buffer_init(&stdout_buffer);
                    830:        buffer_init(&stderr_buffer);
                    831:
1.15      markus    832:        client_init_dispatch();
                    833:
1.11      markus    834:        /* Set signal handlers to restore non-blocking mode.  */
                    835:        signal(SIGINT, signal_handler);
                    836:        signal(SIGQUIT, signal_handler);
                    837:        signal(SIGTERM, signal_handler);
                    838:        signal(SIGPIPE, SIG_IGN);
                    839:        if (have_pty)
                    840:                signal(SIGWINCH, window_change_handler);
                    841:
                    842:        if (have_pty)
                    843:                enter_raw_mode();
                    844:
1.28      provos    845:        /* Check if we should immediately send eof on stdin. */
1.17      markus    846:        if (!compat20)
1.16      markus    847:                client_check_initial_eof_on_stdin();
1.11      markus    848:
1.30      markus    849:        if (compat20 && escape_char != -1)
                    850:                channel_register_filter(ssh2_chan_id, simple_escape_filter);
                    851:
1.11      markus    852:        /* Main loop of the client for the interactive session mode. */
                    853:        while (!quit_pending) {
                    854:                fd_set readset, writeset;
                    855:
1.13      markus    856:                /* Process buffered packets sent by the server. */
1.11      markus    857:                client_process_buffered_input_packets();
                    858:
1.16      markus    859:                if (compat20 && !channel_still_open()) {
1.37      markus    860:                        debug2("!channel_still_open.");
1.16      markus    861:                        break;
                    862:                }
                    863:
1.13      markus    864:                /*
                    865:                 * Make packets of buffered stdin data, and buffer them for
                    866:                 * sending to the server.
                    867:                 */
1.17      markus    868:                if (!compat20)
1.16      markus    869:                        client_make_packets_from_stdin_data();
1.11      markus    870:
1.13      markus    871:                /*
                    872:                 * Make packets from buffered channel data, and buffer them
                    873:                 * for sending to the server.
                    874:                 */
1.11      markus    875:                if (packet_not_very_much_data_to_write())
                    876:                        channel_output_poll();
                    877:
1.13      markus    878:                /*
                    879:                 * Check if the window size has changed, and buffer a message
                    880:                 * about it to the server if so.
                    881:                 */
1.11      markus    882:                client_check_window_change();
                    883:
                    884:                if (quit_pending)
                    885:                        break;
                    886:
1.13      markus    887:                /*
                    888:                 * Wait until we have something to do (something becomes
                    889:                 * available on one of the descriptors).
                    890:                 */
1.11      markus    891:                client_wait_until_can_do_something(&readset, &writeset);
                    892:
                    893:                if (quit_pending)
                    894:                        break;
                    895:
                    896:                /* Do channel operations. */
                    897:                channel_after_select(&readset, &writeset);
                    898:
1.17      markus    899:                /* Buffer input from the connection.  */
                    900:                client_process_net_input(&readset);
                    901:
                    902:                if (quit_pending)
                    903:                        break;
1.11      markus    904:
1.17      markus    905:                if (!compat20) {
                    906:                        /* Buffer data from stdin */
                    907:                        client_process_input(&readset);
                    908:                        /*
                    909:                         * Process output to stdout and stderr.  Output to
                    910:                         * the connection is processed elsewhere (above).
                    911:                         */
1.16      markus    912:                        client_process_output(&writeset);
1.17      markus    913:                }
1.11      markus    914:
1.13      markus    915:                /* Send as much buffered packet data as possible to the sender. */
1.11      markus    916:                if (FD_ISSET(connection_out, &writeset))
                    917:                        packet_write_poll();
                    918:        }
                    919:
                    920:        /* Terminate the session. */
                    921:
                    922:        /* Stop watching for window change. */
                    923:        if (have_pty)
                    924:                signal(SIGWINCH, SIG_DFL);
                    925:
                    926:        /* Stop listening for connections. */
                    927:        channel_stop_listening();
                    928:
1.13      markus    929:        /*
                    930:         * In interactive mode (with pseudo tty) display a message indicating
                    931:         * that the connection has been closed.
                    932:         */
1.11      markus    933:        if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
                    934:                snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
                    935:                buffer_append(&stderr_buffer, buf, strlen(buf));
                    936:                stderr_bytes += strlen(buf);
                    937:        }
                    938:        /* Output any buffered data for stdout. */
                    939:        while (buffer_len(&stdout_buffer) > 0) {
                    940:                len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
1.14      deraadt   941:                    buffer_len(&stdout_buffer));
1.11      markus    942:                if (len <= 0) {
                    943:                        error("Write failed flushing stdout buffer.");
                    944:                        break;
                    945:                }
                    946:                buffer_consume(&stdout_buffer, len);
                    947:        }
                    948:
                    949:        /* Output any buffered data for stderr. */
                    950:        while (buffer_len(&stderr_buffer) > 0) {
                    951:                len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
1.14      deraadt   952:                    buffer_len(&stderr_buffer));
1.11      markus    953:                if (len <= 0) {
                    954:                        error("Write failed flushing stderr buffer.");
                    955:                        break;
                    956:                }
                    957:                buffer_consume(&stderr_buffer, len);
                    958:        }
                    959:
                    960:        if (have_pty)
                    961:                leave_raw_mode();
                    962:
                    963:        /* Clear and free any buffers. */
                    964:        memset(buf, 0, sizeof(buf));
                    965:        buffer_free(&stdin_buffer);
                    966:        buffer_free(&stdout_buffer);
                    967:        buffer_free(&stderr_buffer);
                    968:
                    969:        /* Report bytes transferred, and transfer rates. */
                    970:        total_time = get_current_time() - start_time;
                    971:        debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
                    972:              stdin_bytes, stdout_bytes, stderr_bytes, total_time);
                    973:        if (total_time > 0)
                    974:                debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
                    975:                      stdin_bytes / total_time, stdout_bytes / total_time,
                    976:                      stderr_bytes / total_time);
                    977:
                    978:        /* Return the exit status of the program. */
                    979:        debug("Exit status %d", exit_status);
                    980:        return exit_status;
1.15      markus    981: }
                    982:
                    983: /*********/
                    984:
                    985: void
1.36      markus    986: client_input_stdout_data(int type, int plen, void *ctxt)
1.15      markus    987: {
1.42      markus    988:        u_int data_len;
1.15      markus    989:        char *data = packet_get_string(&data_len);
                    990:        packet_integrity_check(plen, 4 + data_len, type);
                    991:        buffer_append(&stdout_buffer, data, data_len);
                    992:        stdout_bytes += data_len;
                    993:        memset(data, 0, data_len);
                    994:        xfree(data);
                    995: }
                    996: void
1.36      markus    997: client_input_stderr_data(int type, int plen, void *ctxt)
1.15      markus    998: {
1.42      markus    999:        u_int data_len;
1.15      markus   1000:        char *data = packet_get_string(&data_len);
                   1001:        packet_integrity_check(plen, 4 + data_len, type);
                   1002:        buffer_append(&stderr_buffer, data, data_len);
                   1003:        stdout_bytes += data_len;
                   1004:        memset(data, 0, data_len);
                   1005:        xfree(data);
                   1006: }
                   1007: void
1.36      markus   1008: client_input_exit_status(int type, int plen, void *ctxt)
1.15      markus   1009: {
                   1010:        packet_integrity_check(plen, 4, type);
                   1011:        exit_status = packet_get_int();
                   1012:        /* Acknowledge the exit. */
                   1013:        packet_start(SSH_CMSG_EXIT_CONFIRMATION);
                   1014:        packet_send();
                   1015:        /*
                   1016:         * Must wait for packet to be sent since we are
                   1017:         * exiting the loop.
                   1018:         */
                   1019:        packet_write_wait();
                   1020:        /* Flag that we want to exit. */
                   1021:        quit_pending = 1;
                   1022: }
                   1023:
1.40      markus   1024: Channel *
                   1025: client_request_forwarded_tcpip(const char *request_type, int rchan)
                   1026: {
                   1027:        Channel* c = NULL;
                   1028:        char *listen_address, *originator_address;
                   1029:        int listen_port, originator_port;
                   1030:        int sock, newch;
                   1031:
                   1032:        /* Get rest of the packet */
                   1033:        listen_address = packet_get_string(NULL);
                   1034:        listen_port = packet_get_int();
                   1035:        originator_address = packet_get_string(NULL);
                   1036:        originator_port = packet_get_int();
                   1037:        packet_done();
                   1038:
                   1039:        debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
                   1040:            listen_address, listen_port, originator_address, originator_port);
                   1041:
                   1042:        sock = channel_connect_by_listen_adress(listen_port);
                   1043:        if (sock >= 0) {
                   1044:                newch = channel_new("forwarded-tcpip",
1.41      markus   1045:                    SSH_CHANNEL_CONNECTING, sock, sock, -1,
1.40      markus   1046:                    CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
                   1047:                    xstrdup(originator_address), 1);
                   1048:                c = channel_lookup(newch);
                   1049:        }
                   1050:        xfree(originator_address);
                   1051:        xfree(listen_address);
                   1052:        return c;
                   1053: }
                   1054:
                   1055: Channel*
                   1056: client_request_x11(const char *request_type, int rchan)
                   1057: {
                   1058:        Channel *c = NULL;
                   1059:        char *originator;
                   1060:        int originator_port;
                   1061:        int sock, newch;
                   1062:
                   1063:        if (!options.forward_x11) {
                   1064:                error("Warning: ssh server tried X11 forwarding.");
                   1065:                error("Warning: this is probably a break in attempt by a malicious server.");
                   1066:                return NULL;
                   1067:        }
                   1068:        originator = packet_get_string(NULL);
                   1069:        if (datafellows & SSH_BUG_X11FWD) {
                   1070:                debug2("buggy server: x11 request w/o originator_port");
                   1071:                originator_port = 0;
                   1072:        } else {
                   1073:                originator_port = packet_get_int();
                   1074:        }
                   1075:        packet_done();
                   1076:        /* XXX check permission */
                   1077:        sock = x11_connect_display();
                   1078:        if (sock >= 0) {
                   1079:                newch = channel_new("x11",
                   1080:                    SSH_CHANNEL_X11_OPEN, sock, sock, -1,
                   1081:                    CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0,
                   1082:                    xstrdup("x11"), 1);
                   1083:                c = channel_lookup(newch);
                   1084:        }
                   1085:        xfree(originator);
                   1086:        return c;
                   1087: }
                   1088:
                   1089: Channel*
                   1090: client_request_agent(const char *request_type, int rchan)
                   1091: {
                   1092:        Channel *c = NULL;
                   1093:        int sock, newch;
                   1094:
                   1095:        if (!options.forward_agent) {
                   1096:                error("Warning: ssh server tried agent forwarding.");
                   1097:                error("Warning: this is probably a break in attempt by a malicious server.");
                   1098:                return NULL;
                   1099:        }
                   1100:        sock =  ssh_get_authentication_socket();
                   1101:        if (sock >= 0) {
                   1102:                newch = channel_new("authentication agent connection",
                   1103:                    SSH_CHANNEL_OPEN, sock, sock, -1,
                   1104:                    CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
                   1105:                    xstrdup("authentication agent connection"), 1);
                   1106:                c = channel_lookup(newch);
                   1107:        }
                   1108:        return c;
                   1109: }
                   1110:
1.22      markus   1111: /* XXXX move to generic input handler */
                   1112: void
1.36      markus   1113: client_input_channel_open(int type, int plen, void *ctxt)
1.22      markus   1114: {
                   1115:        Channel *c = NULL;
                   1116:        char *ctype;
1.42      markus   1117:        u_int len;
1.22      markus   1118:        int rchan;
                   1119:        int rmaxpack;
                   1120:        int rwindow;
                   1121:
                   1122:        ctype = packet_get_string(&len);
                   1123:        rchan = packet_get_int();
                   1124:        rwindow = packet_get_int();
                   1125:        rmaxpack = packet_get_int();
                   1126:
1.24      markus   1127:        debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
1.22      markus   1128:            ctype, rchan, rwindow, rmaxpack);
                   1129:
1.40      markus   1130:        if (strcmp(ctype, "forwarded-tcpip") == 0) {
                   1131:                c = client_request_forwarded_tcpip(ctype, rchan);
                   1132:        } else if (strcmp(ctype, "x11") == 0) {
                   1133:                c = client_request_x11(ctype, rchan);
                   1134:        } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
                   1135:                c = client_request_agent(ctype, rchan);
1.22      markus   1136:        }
                   1137: /* XXX duplicate : */
                   1138:        if (c != NULL) {
                   1139:                debug("confirm %s", ctype);
                   1140:                c->remote_id = rchan;
                   1141:                c->remote_window = rwindow;
                   1142:                c->remote_maxpacket = rmaxpack;
                   1143:
                   1144:                packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
                   1145:                packet_put_int(c->remote_id);
                   1146:                packet_put_int(c->self);
                   1147:                packet_put_int(c->local_window);
                   1148:                packet_put_int(c->local_maxpacket);
                   1149:                packet_send();
                   1150:        } else {
                   1151:                debug("failure %s", ctype);
                   1152:                packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
                   1153:                packet_put_int(rchan);
                   1154:                packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
                   1155:                packet_put_cstring("bla bla");
                   1156:                packet_put_cstring("");
                   1157:                packet_send();
                   1158:        }
                   1159:        xfree(ctype);
                   1160: }
                   1161:
1.20      markus   1162: void
1.16      markus   1163: client_init_dispatch_20()
                   1164: {
                   1165:        dispatch_init(&dispatch_protocol_error);
                   1166:        dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
                   1167:        dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
                   1168:        dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
                   1169:        dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
1.22      markus   1170:        dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
1.16      markus   1171:        dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
                   1172:        dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
                   1173:        dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
                   1174:        dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
                   1175: }
1.20      markus   1176: void
1.15      markus   1177: client_init_dispatch_13()
                   1178: {
                   1179:        dispatch_init(NULL);
                   1180:        dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
                   1181:        dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
                   1182:        dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
                   1183:        dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
                   1184:        dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
                   1185:        dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
                   1186:        dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
                   1187:        dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
                   1188:        dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
1.39      markus   1189:
                   1190:        dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
                   1191:            &auth_input_open_request : &deny_input_open);
                   1192:        dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
                   1193:            &x11_input_open : &deny_input_open);
1.15      markus   1194: }
1.20      markus   1195: void
1.15      markus   1196: client_init_dispatch_15()
                   1197: {
                   1198:        client_init_dispatch_13();
                   1199:        dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
                   1200:        dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
                   1201: }
1.20      markus   1202: void
1.15      markus   1203: client_init_dispatch()
                   1204: {
1.16      markus   1205:        if (compat20)
                   1206:                client_init_dispatch_20();
                   1207:        else if (compat13)
1.15      markus   1208:                client_init_dispatch_13();
                   1209:        else
                   1210:                client_init_dispatch_15();
1.16      markus   1211: }
                   1212:
                   1213: void
                   1214: client_input_channel_req(int id, void *arg)
                   1215: {
                   1216:        Channel *c = NULL;
1.42      markus   1217:        u_int len;
1.16      markus   1218:        int success = 0;
                   1219:        int reply;
                   1220:        char *rtype;
                   1221:
                   1222:        rtype = packet_get_string(&len);
                   1223:        reply = packet_get_char();
                   1224:
1.23      markus   1225:        debug("client_input_channel_req: rtype %s reply %d", rtype, reply);
1.16      markus   1226:
                   1227:        c = channel_lookup(id);
                   1228:        if (c == NULL)
1.35      markus   1229:                fatal("client_input_channel_req: channel %d: bad channel", id);
1.16      markus   1230:
                   1231:        if (session_ident == -1) {
                   1232:                error("client_input_channel_req: no channel %d", id);
                   1233:        } else if (id != session_ident) {
                   1234:                error("client_input_channel_req: bad channel %d != %d",
                   1235:                    id, session_ident);
                   1236:        } else if (strcmp(rtype, "exit-status") == 0) {
                   1237:                success = 1;
                   1238:                exit_status = packet_get_int();
1.19      markus   1239:                packet_done();
1.16      markus   1240:        }
                   1241:        if (reply) {
                   1242:                packet_start(success ?
                   1243:                    SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
                   1244:                packet_put_int(c->remote_id);
                   1245:                packet_send();
                   1246:        }
                   1247:        xfree(rtype);
                   1248: }
                   1249:
                   1250: void
1.43    ! markus   1251: clientloop_set_session_ident(int id)
1.16      markus   1252: {
1.43    ! markus   1253:        debug2("clientloop_set_session_ident: id %d", id);
1.16      markus   1254:        session_ident = id;
                   1255:        channel_register_callback(id, SSH2_MSG_CHANNEL_REQUEST,
                   1256:            client_input_channel_req, (void *)0);
1.1       deraadt  1257: }