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

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