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

1.188   ! dtucker     1: /* $OpenBSD: clientloop.c,v 1.187 2008/01/23 01:56:54 dtucker Exp $ */
1.1       deraadt     2: /*
1.12      deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
1.33      deraadt     6:  * The main loop for the interactive session (client side).
1.20      markus      7:  *
1.33      deraadt     8:  * As far as I am concerned, the code I have written for this software
                      9:  * can be used freely for any purpose.  Any derived versions of this
                     10:  * software must be clearly marked as such, and if the derived work is
                     11:  * incompatible with the protocol description in the RFC file, it must be
                     12:  * called by a name other than "ssh" or "Secure Shell".
                     13:  *
                     14:  *
                     15:  * Copyright (c) 1999 Theo de Raadt.  All rights reserved.
                     16:  *
                     17:  * Redistribution and use in source and binary forms, with or without
                     18:  * modification, are permitted provided that the following conditions
                     19:  * are met:
                     20:  * 1. Redistributions of source code must retain the above copyright
                     21:  *    notice, this list of conditions and the following disclaimer.
                     22:  * 2. Redistributions in binary form must reproduce the above copyright
                     23:  *    notice, this list of conditions and the following disclaimer in the
                     24:  *    documentation and/or other materials provided with the distribution.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     27:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     28:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     29:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     30:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     31:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     32:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     33:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     35:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.20      markus     36:  *
                     37:  *
1.33      deraadt    38:  * SSH2 support added by Markus Friedl.
1.78      markus     39:  * Copyright (c) 1999, 2000, 2001 Markus Friedl.  All rights reserved.
1.20      markus     40:  *
1.33      deraadt    41:  * Redistribution and use in source and binary forms, with or without
                     42:  * modification, are permitted provided that the following conditions
                     43:  * are met:
                     44:  * 1. Redistributions of source code must retain the above copyright
                     45:  *    notice, this list of conditions and the following disclaimer.
                     46:  * 2. Redistributions in binary form must reproduce the above copyright
                     47:  *    notice, this list of conditions and the following disclaimer in the
                     48:  *    documentation and/or other materials provided with the distribution.
                     49:  *
                     50:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     51:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     52:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     53:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     54:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     55:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     56:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     57:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     58:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     59:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.12      deraadt    60:  */
1.1       deraadt    61:
1.152     stevesk    62:
1.175     deraadt    63: #include <sys/types.h>
1.152     stevesk    64: #include <sys/ioctl.h>
1.154     stevesk    65: #include <sys/stat.h>
1.166     stevesk    66: #include <sys/socket.h>
1.171     stevesk    67: #include <sys/time.h>
1.172     stevesk    68: #include <sys/param.h>
1.150     stevesk    69:
1.155     stevesk    70: #include <ctype.h>
1.168     stevesk    71: #include <errno.h>
1.151     stevesk    72: #include <paths.h>
1.153     stevesk    73: #include <signal.h>
1.174     stevesk    74: #include <stdio.h>
1.173     stevesk    75: #include <stdlib.h>
1.170     stevesk    76: #include <string.h>
1.150     stevesk    77: #include <termios.h>
1.175     deraadt    78: #include <pwd.h>
1.169     stevesk    79: #include <unistd.h>
1.1       deraadt    80:
1.175     deraadt    81: #include "xmalloc.h"
1.45      markus     82: #include "ssh.h"
                     83: #include "ssh1.h"
                     84: #include "ssh2.h"
1.1       deraadt    85: #include "packet.h"
                     86: #include "buffer.h"
1.15      markus     87: #include "compat.h"
1.74      markus     88: #include "channels.h"
1.15      markus     89: #include "dispatch.h"
1.40      markus     90: #include "key.h"
1.175     deraadt    91: #include "cipher.h"
1.54      markus     92: #include "kex.h"
1.45      markus     93: #include "log.h"
                     94: #include "readconf.h"
                     95: #include "clientloop.h"
1.147     djm        96: #include "sshconnect.h"
1.40      markus     97: #include "authfd.h"
1.45      markus     98: #include "atomicio.h"
1.118     deraadt    99: #include "sshpty.h"
1.63      markus    100: #include "misc.h"
1.123     djm       101: #include "monitor_fdpass.h"
                    102: #include "match.h"
                    103: #include "msg.h"
1.39      markus    104:
                    105: /* import options */
                    106: extern Options options;
                    107:
1.1       deraadt   108: /* Flag indicating that stdin should be redirected from /dev/null. */
                    109: extern int stdin_null_flag;
                    110:
1.116     dtucker   111: /* Flag indicating that no shell has been requested */
                    112: extern int no_shell_flag;
                    113:
1.123     djm       114: /* Control socket */
                    115: extern int control_fd;
                    116:
1.13      markus    117: /*
                    118:  * Name of the host we are connecting to.  This is the name given on the
                    119:  * command line, or the HostName specified for the user-supplied name in a
                    120:  * configuration file.
                    121:  */
1.1       deraadt   122: extern char *host;
                    123:
1.13      markus    124: /*
                    125:  * Flag to indicate that we have received a window change signal which has
                    126:  * not yet been processed.  This will cause a message indicating the new
                    127:  * window size to be sent to the server a little later.  This is volatile
                    128:  * because this is updated in a signal handler.
                    129:  */
1.88      markus    130: static volatile sig_atomic_t received_window_change_signal = 0;
                    131: static volatile sig_atomic_t received_signal = 0;
1.1       deraadt   132:
1.145     djm       133: /* Flag indicating whether the user's terminal is in non-blocking mode. */
1.1       deraadt   134: static int in_non_blocking_mode = 0;
                    135:
                    136: /* Common data for the client loop code. */
1.162     djm       137: static volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
1.31      markus    138: static int escape_char;                /* Escape character. */
1.11      markus    139: static int escape_pending;     /* Last character was the escape character */
                    140: static int last_was_cr;                /* Last character was a newline. */
                    141: static int exit_status;                /* Used to store the exit status of the command. */
                    142: static int stdin_eof;          /* EOF has been encountered on standard error. */
                    143: static Buffer stdin_buffer;    /* Buffer for stdin data. */
                    144: static Buffer stdout_buffer;   /* Buffer for stdout data. */
                    145: static Buffer stderr_buffer;   /* Buffer for stderr data. */
1.42      markus    146: static u_long stdin_bytes, stdout_bytes, stderr_bytes;
                    147: static u_int buffer_high;/* Soft max buffer size. */
1.11      markus    148: static int connection_in;      /* Connection to server (input). */
                    149: static int connection_out;     /* Connection to server (output). */
1.56      markus    150: static int need_rekeying;      /* Set to non-zero if rekeying is requested. */
1.60      markus    151: static int session_closed = 0; /* In SSH2: login session closed. */
1.1       deraadt   152:
1.77      itojun    153: static void client_init_dispatch(void);
1.16      markus    154: int    session_ident = -1;
                    155:
1.123     djm       156: struct confirm_ctx {
                    157:        int want_tty;
                    158:        int want_subsys;
1.140     djm       159:        int want_x_fwd;
                    160:        int want_agent_fwd;
1.123     djm       161:        Buffer cmd;
                    162:        char *term;
                    163:        struct termios tio;
1.126     djm       164:        char **env;
1.123     djm       165: };
                    166:
1.54      markus    167: /*XXX*/
                    168: extern Kex *xxx_kex;
                    169:
1.123     djm       170: void ssh_process_session2_setup(int, int, int, Buffer *);
                    171:
1.1       deraadt   172: /* Restores stdin to blocking mode. */
                    173:
1.77      itojun    174: static void
1.49      itojun    175: leave_non_blocking(void)
1.1       deraadt   176: {
1.11      markus    177:        if (in_non_blocking_mode) {
1.125     djm       178:                unset_nonblock(fileno(stdin));
1.11      markus    179:                in_non_blocking_mode = 0;
                    180:        }
1.1       deraadt   181: }
                    182:
1.11      markus    183: /* Puts stdin terminal in non-blocking mode. */
                    184:
1.77      itojun    185: static void
1.49      itojun    186: enter_non_blocking(void)
1.1       deraadt   187: {
1.11      markus    188:        in_non_blocking_mode = 1;
1.124     djm       189:        set_nonblock(fileno(stdin));
1.1       deraadt   190: }
                    191:
1.13      markus    192: /*
                    193:  * Signal handler for the window change signal (SIGWINCH).  This just sets a
                    194:  * flag indicating that the window has changed.
                    195:  */
1.157     deraadt   196: /*ARGSUSED */
1.77      itojun    197: static void
1.11      markus    198: window_change_handler(int sig)
1.1       deraadt   199: {
1.11      markus    200:        received_window_change_signal = 1;
                    201:        signal(SIGWINCH, window_change_handler);
1.1       deraadt   202: }
                    203:
1.13      markus    204: /*
                    205:  * Signal handler for signals that cause the program to terminate.  These
                    206:  * signals must be trapped to restore terminal modes.
                    207:  */
1.157     deraadt   208: /*ARGSUSED */
1.77      itojun    209: static void
1.11      markus    210: signal_handler(int sig)
1.1       deraadt   211: {
1.75      markus    212:        received_signal = sig;
                    213:        quit_pending = 1;
1.1       deraadt   214: }
                    215:
1.13      markus    216: /*
                    217:  * Returns current time in seconds from Jan 1, 1970 with the maximum
                    218:  * available resolution.
                    219:  */
1.1       deraadt   220:
1.77      itojun    221: static double
1.49      itojun    222: get_current_time(void)
1.1       deraadt   223: {
1.11      markus    224:        struct timeval tv;
                    225:        gettimeofday(&tv, NULL);
                    226:        return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
1.138     djm       227: }
                    228:
                    229: #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
                    230: void
                    231: client_x11_get_proto(const char *display, const char *xauth_path,
                    232:     u_int trusted, char **_proto, char **_data)
                    233: {
                    234:        char cmd[1024];
                    235:        char line[512];
                    236:        char xdisplay[512];
                    237:        static char proto[512], data[512];
                    238:        FILE *f;
                    239:        int got_data = 0, generated = 0, do_unlink = 0, i;
                    240:        char *xauthdir, *xauthfile;
                    241:        struct stat st;
                    242:
                    243:        xauthdir = xauthfile = NULL;
                    244:        *_proto = proto;
                    245:        *_data = data;
                    246:        proto[0] = data[0] = '\0';
                    247:
                    248:        if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
                    249:                debug("No xauth program.");
                    250:        } else {
                    251:                if (display == NULL) {
                    252:                        debug("x11_get_proto: DISPLAY not set");
                    253:                        return;
                    254:                }
                    255:                /*
                    256:                 * Handle FamilyLocal case where $DISPLAY does
                    257:                 * not match an authorization entry.  For this we
                    258:                 * just try "xauth list unix:displaynum.screennum".
                    259:                 * XXX: "localhost" match to determine FamilyLocal
                    260:                 *      is not perfect.
                    261:                 */
                    262:                if (strncmp(display, "localhost:", 10) == 0) {
                    263:                        snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
                    264:                            display + 10);
                    265:                        display = xdisplay;
                    266:                }
                    267:                if (trusted == 0) {
                    268:                        xauthdir = xmalloc(MAXPATHLEN);
                    269:                        xauthfile = xmalloc(MAXPATHLEN);
                    270:                        strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
                    271:                        if (mkdtemp(xauthdir) != NULL) {
                    272:                                do_unlink = 1;
                    273:                                snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
                    274:                                    xauthdir);
                    275:                                snprintf(cmd, sizeof(cmd),
                    276:                                    "%s -f %s generate %s " SSH_X11_PROTO
                    277:                                    " untrusted timeout 1200 2>" _PATH_DEVNULL,
                    278:                                    xauth_path, xauthfile, display);
                    279:                                debug2("x11_get_proto: %s", cmd);
                    280:                                if (system(cmd) == 0)
                    281:                                        generated = 1;
                    282:                        }
                    283:                }
1.181     markus    284:
                    285:                /*
                    286:                 * When in untrusted mode, we read the cookie only if it was
                    287:                 * successfully generated as an untrusted one in the step
                    288:                 * above.
                    289:                 */
                    290:                if (trusted || generated) {
                    291:                        snprintf(cmd, sizeof(cmd),
                    292:                            "%s %s%s list %s 2>" _PATH_DEVNULL,
                    293:                            xauth_path,
                    294:                            generated ? "-f " : "" ,
                    295:                            generated ? xauthfile : "",
                    296:                            display);
                    297:                        debug2("x11_get_proto: %s", cmd);
                    298:                        f = popen(cmd, "r");
                    299:                        if (f && fgets(line, sizeof(line), f) &&
                    300:                            sscanf(line, "%*s %511s %511s", proto, data) == 2)
                    301:                                got_data = 1;
                    302:                        if (f)
                    303:                                pclose(f);
                    304:                } else
                    305:                        error("Warning: untrusted X11 forwarding setup failed: "
                    306:                            "xauth key data not generated");
1.138     djm       307:        }
                    308:
                    309:        if (do_unlink) {
                    310:                unlink(xauthfile);
                    311:                rmdir(xauthdir);
                    312:        }
                    313:        if (xauthdir)
                    314:                xfree(xauthdir);
                    315:        if (xauthfile)
                    316:                xfree(xauthfile);
                    317:
                    318:        /*
                    319:         * If we didn't get authentication data, just make up some
                    320:         * data.  The forwarding code will check the validity of the
                    321:         * response anyway, and substitute this data.  The X11
                    322:         * server, however, will ignore this fake data and use
                    323:         * whatever authentication mechanisms it was using otherwise
                    324:         * for the local connection.
                    325:         */
                    326:        if (!got_data) {
                    327:                u_int32_t rnd = 0;
                    328:
                    329:                logit("Warning: No xauth data; "
                    330:                    "using fake authentication data for X11 forwarding.");
                    331:                strlcpy(proto, SSH_X11_PROTO, sizeof proto);
                    332:                for (i = 0; i < 16; i++) {
                    333:                        if (i % 4 == 0)
                    334:                                rnd = arc4random();
                    335:                        snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
                    336:                            rnd & 0xff);
                    337:                        rnd >>= 8;
                    338:                }
                    339:        }
1.1       deraadt   340: }
                    341:
1.13      markus    342: /*
                    343:  * This is called when the interactive is entered.  This checks if there is
                    344:  * an EOF coming on stdin.  We must check this explicitly, as select() does
                    345:  * not appear to wake up when redirecting from /dev/null.
                    346:  */
1.1       deraadt   347:
1.77      itojun    348: static void
1.49      itojun    349: client_check_initial_eof_on_stdin(void)
1.1       deraadt   350: {
1.11      markus    351:        int len;
                    352:        char buf[1];
1.1       deraadt   353:
1.13      markus    354:        /*
                    355:         * If standard input is to be "redirected from /dev/null", we simply
                    356:         * mark that we have seen an EOF and send an EOF message to the
                    357:         * server. Otherwise, we try to read a single character; it appears
                    358:         * that for some files, such /dev/null, select() never wakes up for
                    359:         * read for this descriptor, which means that we never get EOF.  This
                    360:         * way we will get the EOF if stdin comes from /dev/null or similar.
                    361:         */
1.11      markus    362:        if (stdin_null_flag) {
                    363:                /* Fake EOF on stdin. */
                    364:                debug("Sending eof.");
                    365:                stdin_eof = 1;
                    366:                packet_start(SSH_CMSG_EOF);
                    367:                packet_send();
                    368:        } else {
                    369:                enter_non_blocking();
                    370:
                    371:                /* Check for immediate EOF on stdin. */
                    372:                len = read(fileno(stdin), buf, 1);
                    373:                if (len == 0) {
1.13      markus    374:                        /* EOF.  Record that we have seen it and send EOF to server. */
1.11      markus    375:                        debug("Sending eof.");
                    376:                        stdin_eof = 1;
                    377:                        packet_start(SSH_CMSG_EOF);
                    378:                        packet_send();
                    379:                } else if (len > 0) {
1.13      markus    380:                        /*
                    381:                         * Got data.  We must store the data in the buffer,
                    382:                         * and also process it as an escape character if
                    383:                         * appropriate.
                    384:                         */
1.42      markus    385:                        if ((u_char) buf[0] == escape_char)
1.11      markus    386:                                escape_pending = 1;
1.52      markus    387:                        else
1.11      markus    388:                                buffer_append(&stdin_buffer, buf, 1);
                    389:                }
                    390:                leave_non_blocking();
                    391:        }
1.1       deraadt   392: }
                    393:
                    394:
1.13      markus    395: /*
                    396:  * Make packets from buffered stdin data, and buffer them for sending to the
                    397:  * connection.
                    398:  */
1.1       deraadt   399:
1.77      itojun    400: static void
1.49      itojun    401: client_make_packets_from_stdin_data(void)
1.1       deraadt   402: {
1.42      markus    403:        u_int len;
1.1       deraadt   404:
1.11      markus    405:        /* Send buffered stdin data to the server. */
                    406:        while (buffer_len(&stdin_buffer) > 0 &&
1.90      deraadt   407:            packet_not_very_much_data_to_write()) {
1.11      markus    408:                len = buffer_len(&stdin_buffer);
                    409:                /* Keep the packets at reasonable size. */
                    410:                if (len > packet_get_maxsize())
                    411:                        len = packet_get_maxsize();
                    412:                packet_start(SSH_CMSG_STDIN_DATA);
                    413:                packet_put_string(buffer_ptr(&stdin_buffer), len);
                    414:                packet_send();
                    415:                buffer_consume(&stdin_buffer, len);
1.52      markus    416:                stdin_bytes += len;
1.11      markus    417:                /* If we have a pending EOF, send it now. */
                    418:                if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
                    419:                        packet_start(SSH_CMSG_EOF);
                    420:                        packet_send();
                    421:                }
1.1       deraadt   422:        }
                    423: }
                    424:
1.13      markus    425: /*
                    426:  * Checks if the client window has changed, and sends a packet about it to
                    427:  * the server if so.  The actual change is detected elsewhere (by a software
                    428:  * interrupt on Unix); this just checks the flag and sends a message if
                    429:  * appropriate.
                    430:  */
1.1       deraadt   431:
1.77      itojun    432: static void
1.49      itojun    433: client_check_window_change(void)
1.1       deraadt   434: {
1.16      markus    435:        struct winsize ws;
                    436:
                    437:        if (! received_window_change_signal)
                    438:                return;
                    439:        /** XXX race */
                    440:        received_window_change_signal = 0;
                    441:
1.37      markus    442:        debug2("client_check_window_change: changed");
1.16      markus    443:
                    444:        if (compat20) {
1.123     djm       445:                channel_send_window_changes();
1.16      markus    446:        } else {
1.123     djm       447:                if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
                    448:                        return;
1.16      markus    449:                packet_start(SSH_CMSG_WINDOW_SIZE);
1.158     deraadt   450:                packet_put_int((u_int)ws.ws_row);
                    451:                packet_put_int((u_int)ws.ws_col);
                    452:                packet_put_int((u_int)ws.ws_xpixel);
                    453:                packet_put_int((u_int)ws.ws_ypixel);
1.16      markus    454:                packet_send();
1.1       deraadt   455:        }
                    456: }
                    457:
1.117     markus    458: static void
                    459: client_global_request_reply(int type, u_int32_t seq, void *ctxt)
                    460: {
1.188   ! dtucker   461:        keep_alive_timeouts = 0;
1.117     markus    462:        client_global_request_reply_fwd(type, seq, ctxt);
                    463: }
                    464:
                    465: static void
                    466: server_alive_check(void)
                    467: {
1.188   ! dtucker   468:        if (++keep_alive_timeouts > options.server_alive_count_max) {
1.176     markus    469:                logit("Timeout, server not responding.");
                    470:                cleanup_exit(255);
                    471:        }
1.117     markus    472:        packet_start(SSH2_MSG_GLOBAL_REQUEST);
                    473:        packet_put_cstring("keepalive@openssh.com");
                    474:        packet_put_char(1);     /* boolean: want reply */
                    475:        packet_send();
                    476: }
                    477:
1.13      markus    478: /*
                    479:  * Waits until the client can do something (some data becomes available on
                    480:  * one of the file descriptors).
                    481:  */
1.77      itojun    482: static void
1.46      markus    483: client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
1.130     avsm      484:     int *maxfdp, u_int *nallocp, int rekeying)
1.11      markus    485: {
1.117     markus    486:        struct timeval tv, *tvp;
                    487:        int ret;
                    488:
1.46      markus    489:        /* Add any selections by the channel mechanism. */
1.81      markus    490:        channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
1.11      markus    491:
1.16      markus    492:        if (!compat20) {
1.17      markus    493:                /* Read from the connection, unless our buffers are full. */
1.16      markus    494:                if (buffer_len(&stdout_buffer) < buffer_high &&
                    495:                    buffer_len(&stderr_buffer) < buffer_high &&
                    496:                    channel_not_very_much_buffered_data())
1.46      markus    497:                        FD_SET(connection_in, *readsetp);
1.17      markus    498:                /*
                    499:                 * Read from stdin, unless we have seen EOF or have very much
                    500:                 * buffered data to send to the server.
                    501:                 */
                    502:                if (!stdin_eof && packet_not_very_much_data_to_write())
1.46      markus    503:                        FD_SET(fileno(stdin), *readsetp);
1.17      markus    504:
                    505:                /* Select stdout/stderr if have data in buffer. */
                    506:                if (buffer_len(&stdout_buffer) > 0)
1.46      markus    507:                        FD_SET(fileno(stdout), *writesetp);
1.17      markus    508:                if (buffer_len(&stderr_buffer) > 0)
1.46      markus    509:                        FD_SET(fileno(stderr), *writesetp);
1.16      markus    510:        } else {
1.71      markus    511:                /* channel_prepare_select could have closed the last channel */
1.84      markus    512:                if (session_closed && !channel_still_open() &&
                    513:                    !packet_have_data_to_write()) {
                    514:                        /* clear mask since we did not call select() */
1.87      markus    515:                        memset(*readsetp, 0, *nallocp);
                    516:                        memset(*writesetp, 0, *nallocp);
1.84      markus    517:                        return;
1.71      markus    518:                } else {
                    519:                        FD_SET(connection_in, *readsetp);
                    520:                }
1.16      markus    521:        }
1.11      markus    522:
                    523:        /* Select server connection if have data to write to the server. */
                    524:        if (packet_have_data_to_write())
1.46      markus    525:                FD_SET(connection_out, *writesetp);
1.11      markus    526:
1.123     djm       527:        if (control_fd != -1)
                    528:                FD_SET(control_fd, *readsetp);
                    529:
1.13      markus    530:        /*
                    531:         * Wait for something to happen.  This will suspend the process until
                    532:         * some selected descriptor can be read, written, or has some other
1.117     markus    533:         * event pending.
1.13      markus    534:         */
1.11      markus    535:
1.117     markus    536:        if (options.server_alive_interval == 0 || !compat20)
                    537:                tvp = NULL;
1.129     deraadt   538:        else {
1.117     markus    539:                tv.tv_sec = options.server_alive_interval;
                    540:                tv.tv_usec = 0;
                    541:                tvp = &tv;
                    542:        }
                    543:        ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
                    544:        if (ret < 0) {
1.11      markus    545:                char buf[100];
1.51      markus    546:
                    547:                /*
                    548:                 * We have to clear the select masks, because we return.
                    549:                 * We have to return, because the mainloop checks for the flags
                    550:                 * set by the signal handlers.
                    551:                 */
1.87      markus    552:                memset(*readsetp, 0, *nallocp);
                    553:                memset(*writesetp, 0, *nallocp);
1.50      deraadt   554:
1.11      markus    555:                if (errno == EINTR)
                    556:                        return;
                    557:                /* Note: we might still have data in the buffers. */
                    558:                snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
                    559:                buffer_append(&stderr_buffer, buf, strlen(buf));
                    560:                quit_pending = 1;
1.117     markus    561:        } else if (ret == 0)
                    562:                server_alive_check();
1.11      markus    563: }
                    564:
1.77      itojun    565: static void
1.31      markus    566: client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
1.1       deraadt   567: {
1.11      markus    568:        /* Flush stdout and stderr buffers. */
1.31      markus    569:        if (buffer_len(bout) > 0)
1.112     deraadt   570:                atomicio(vwrite, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
1.31      markus    571:        if (buffer_len(berr) > 0)
1.112     deraadt   572:                atomicio(vwrite, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
1.11      markus    573:
                    574:        leave_raw_mode();
                    575:
1.13      markus    576:        /*
                    577:         * Free (and clear) the buffer to reduce the amount of data that gets
                    578:         * written to swap.
                    579:         */
1.31      markus    580:        buffer_free(bin);
                    581:        buffer_free(bout);
                    582:        buffer_free(berr);
1.11      markus    583:
                    584:        /* Send the suspend signal to the program itself. */
                    585:        kill(getpid(), SIGTSTP);
                    586:
1.132     djm       587:        /* Reset window sizes in case they have changed */
                    588:        received_window_change_signal = 1;
1.11      markus    589:
                    590:        /* OK, we have been continued by the user. Reinitialize buffers. */
1.31      markus    591:        buffer_init(bin);
                    592:        buffer_init(bout);
                    593:        buffer_init(berr);
1.11      markus    594:
                    595:        enter_raw_mode();
                    596: }
                    597:
1.77      itojun    598: static void
1.161     deraadt   599: client_process_net_input(fd_set *readset)
1.11      markus    600: {
1.17      markus    601:        int len;
                    602:        char buf[8192];
1.11      markus    603:
1.13      markus    604:        /*
                    605:         * Read input from the server, and add any such data to the buffer of
                    606:         * the packet subsystem.
                    607:         */
1.11      markus    608:        if (FD_ISSET(connection_in, readset)) {
                    609:                /* Read as much as possible. */
                    610:                len = read(connection_in, buf, sizeof(buf));
                    611:                if (len == 0) {
                    612:                        /* Received EOF.  The remote host has closed the connection. */
                    613:                        snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
                    614:                                 host);
1.1       deraadt   615:                        buffer_append(&stderr_buffer, buf, strlen(buf));
                    616:                        quit_pending = 1;
                    617:                        return;
1.11      markus    618:                }
1.13      markus    619:                /*
                    620:                 * There is a kernel bug on Solaris that causes select to
                    621:                 * sometimes wake up even though there is no data available.
                    622:                 */
1.53      millert   623:                if (len < 0 && (errno == EAGAIN || errno == EINTR))
1.11      markus    624:                        len = 0;
                    625:
                    626:                if (len < 0) {
                    627:                        /* An error has encountered.  Perhaps there is a network problem. */
                    628:                        snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
                    629:                                 host, strerror(errno));
                    630:                        buffer_append(&stderr_buffer, buf, strlen(buf));
                    631:                        quit_pending = 1;
                    632:                        return;
                    633:                }
                    634:                packet_process_incoming(buf, len);
                    635:        }
1.17      markus    636: }
1.16      markus    637:
1.97      jakob     638: static void
1.123     djm       639: client_subsystem_reply(int type, u_int32_t seq, void *ctxt)
                    640: {
                    641:        int id;
                    642:        Channel *c;
1.129     deraadt   643:
1.123     djm       644:        id = packet_get_int();
                    645:        packet_check_eom();
                    646:
                    647:        if ((c = channel_lookup(id)) == NULL) {
                    648:                error("%s: no channel for id %d", __func__, id);
                    649:                return;
                    650:        }
                    651:
                    652:        if (type == SSH2_MSG_CHANNEL_SUCCESS)
                    653:                debug2("Request suceeded on channel %d", id);
                    654:        else if (type == SSH2_MSG_CHANNEL_FAILURE) {
                    655:                error("Request failed on channel %d", id);
                    656:                channel_free(c);
                    657:        }
                    658: }
                    659:
                    660: static void
                    661: client_extra_session2_setup(int id, void *arg)
                    662: {
                    663:        struct confirm_ctx *cctx = arg;
1.140     djm       664:        const char *display;
1.123     djm       665:        Channel *c;
1.126     djm       666:        int i;
1.129     deraadt   667:
1.123     djm       668:        if (cctx == NULL)
                    669:                fatal("%s: cctx == NULL", __func__);
                    670:        if ((c = channel_lookup(id)) == NULL)
                    671:                fatal("%s: no channel for id %d", __func__, id);
                    672:
1.141     djm       673:        display = getenv("DISPLAY");
1.140     djm       674:        if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
                    675:                char *proto, *data;
                    676:                /* Get reasonable local authentication information. */
                    677:                client_x11_get_proto(display, options.xauth_location,
                    678:                    options.forward_x11_trusted, &proto, &data);
                    679:                /* Request forwarding with authentication spoofing. */
                    680:                debug("Requesting X11 forwarding with authentication spoofing.");
                    681:                x11_request_forwarding_with_spoofing(id, display, proto, data);
                    682:                /* XXX wait for reply */
                    683:        }
                    684:
                    685:        if (cctx->want_agent_fwd && options.forward_agent) {
                    686:                debug("Requesting authentication agent forwarding.");
                    687:                channel_request_start(id, "auth-agent-req@openssh.com", 0);
                    688:                packet_send();
                    689:        }
                    690:
1.129     deraadt   691:        client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
1.126     djm       692:            cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env,
1.123     djm       693:            client_subsystem_reply);
1.127     djm       694:
1.123     djm       695:        c->confirm_ctx = NULL;
                    696:        buffer_free(&cctx->cmd);
1.126     djm       697:        xfree(cctx->term);
                    698:        if (cctx->env != NULL) {
                    699:                for (i = 0; cctx->env[i] != NULL; i++)
                    700:                        xfree(cctx->env[i]);
                    701:                xfree(cctx->env);
1.129     deraadt   702:        }
1.126     djm       703:        xfree(cctx);
1.123     djm       704: }
                    705:
                    706: static void
1.161     deraadt   707: client_process_control(fd_set *readset)
1.123     djm       708: {
                    709:        Buffer m;
                    710:        Channel *c;
1.178     djm       711:        int client_fd, new_fd[3], ver, allowed, window, packetmax;
1.123     djm       712:        socklen_t addrlen;
                    713:        struct sockaddr_storage addr;
                    714:        struct confirm_ctx *cctx;
                    715:        char *cmd;
1.182     djm       716:        u_int i, j, len, env_len, command, flags;
1.123     djm       717:        uid_t euid;
                    718:        gid_t egid;
                    719:
                    720:        /*
                    721:         * Accept connection on control socket
                    722:         */
                    723:        if (control_fd == -1 || !FD_ISSET(control_fd, readset))
                    724:                return;
                    725:
                    726:        memset(&addr, 0, sizeof(addr));
                    727:        addrlen = sizeof(addr);
                    728:        if ((client_fd = accept(control_fd,
                    729:            (struct sockaddr*)&addr, &addrlen)) == -1) {
                    730:                error("%s accept: %s", __func__, strerror(errno));
                    731:                return;
                    732:        }
                    733:
                    734:        if (getpeereid(client_fd, &euid, &egid) < 0) {
                    735:                error("%s getpeereid failed: %s", __func__, strerror(errno));
                    736:                close(client_fd);
                    737:                return;
                    738:        }
                    739:        if ((euid != 0) && (getuid() != euid)) {
                    740:                error("control mode uid mismatch: peer euid %u != uid %u",
                    741:                    (u_int) euid, (u_int) getuid());
                    742:                close(client_fd);
                    743:                return;
                    744:        }
1.127     djm       745:
1.134     djm       746:        unset_nonblock(client_fd);
                    747:
                    748:        /* Read command */
                    749:        buffer_init(&m);
                    750:        if (ssh_msg_recv(client_fd, &m) == -1) {
                    751:                error("%s: client msg_recv failed", __func__);
                    752:                close(client_fd);
                    753:                buffer_free(&m);
                    754:                return;
                    755:        }
1.140     djm       756:        if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
1.134     djm       757:                error("%s: wrong client version %d", __func__, ver);
                    758:                buffer_free(&m);
                    759:                close(client_fd);
                    760:                return;
                    761:        }
                    762:
1.127     djm       763:        allowed = 1;
1.134     djm       764:        command = buffer_get_int(&m);
                    765:        flags = buffer_get_int(&m);
1.123     djm       766:
1.134     djm       767:        buffer_clear(&m);
1.123     djm       768:
1.134     djm       769:        switch (command) {
                    770:        case SSHMUX_COMMAND_OPEN:
1.137     djm       771:                if (options.control_master == SSHCTL_MASTER_ASK ||
                    772:                    options.control_master == SSHCTL_MASTER_AUTO_ASK)
1.134     djm       773:                        allowed = ask_permission("Allow shared connection "
                    774:                            "to %s? ", host);
                    775:                /* continue below */
                    776:                break;
                    777:        case SSHMUX_COMMAND_TERMINATE:
1.137     djm       778:                if (options.control_master == SSHCTL_MASTER_ASK ||
                    779:                    options.control_master == SSHCTL_MASTER_AUTO_ASK)
1.134     djm       780:                        allowed = ask_permission("Terminate shared connection "
                    781:                            "to %s? ", host);
                    782:                if (allowed)
                    783:                        quit_pending = 1;
1.136     deraadt   784:                /* FALLTHROUGH */
1.134     djm       785:        case SSHMUX_COMMAND_ALIVE_CHECK:
                    786:                /* Reply for SSHMUX_COMMAND_TERMINATE and ALIVE_CHECK */
                    787:                buffer_clear(&m);
                    788:                buffer_put_int(&m, allowed);
                    789:                buffer_put_int(&m, getpid());
1.140     djm       790:                if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
1.134     djm       791:                        error("%s: client msg_send failed", __func__);
                    792:                        close(client_fd);
                    793:                        buffer_free(&m);
                    794:                        return;
                    795:                }
                    796:                buffer_free(&m);
                    797:                close(client_fd);
                    798:                return;
                    799:        default:
                    800:                error("Unsupported command %d", command);
                    801:                buffer_free(&m);
                    802:                close(client_fd);
                    803:                return;
                    804:        }
1.123     djm       805:
1.134     djm       806:        /* Reply for SSHMUX_COMMAND_OPEN */
                    807:        buffer_clear(&m);
1.127     djm       808:        buffer_put_int(&m, allowed);
1.123     djm       809:        buffer_put_int(&m, getpid());
1.140     djm       810:        if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
1.123     djm       811:                error("%s: client msg_send failed", __func__);
                    812:                close(client_fd);
1.127     djm       813:                buffer_free(&m);
1.123     djm       814:                return;
                    815:        }
                    816:
1.127     djm       817:        if (!allowed) {
                    818:                error("Refused control connection");
                    819:                close(client_fd);
                    820:                buffer_free(&m);
                    821:                return;
                    822:        }
                    823:
1.134     djm       824:        buffer_clear(&m);
1.123     djm       825:        if (ssh_msg_recv(client_fd, &m) == -1) {
                    826:                error("%s: client msg_recv failed", __func__);
                    827:                close(client_fd);
1.127     djm       828:                buffer_free(&m);
1.123     djm       829:                return;
                    830:        }
1.140     djm       831:        if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
1.123     djm       832:                error("%s: wrong client version %d", __func__, ver);
                    833:                buffer_free(&m);
                    834:                close(client_fd);
                    835:                return;
                    836:        }
                    837:
1.159     djm       838:        cctx = xcalloc(1, sizeof(*cctx));
1.134     djm       839:        cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0;
                    840:        cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0;
1.140     djm       841:        cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0;
                    842:        cctx->want_agent_fwd = (flags & SSHMUX_FLAG_AGENT_FWD) != 0;
1.123     djm       843:        cctx->term = buffer_get_string(&m, &len);
                    844:
                    845:        cmd = buffer_get_string(&m, &len);
                    846:        buffer_init(&cctx->cmd);
                    847:        buffer_append(&cctx->cmd, cmd, strlen(cmd));
                    848:
1.126     djm       849:        env_len = buffer_get_int(&m);
                    850:        env_len = MIN(env_len, 4096);
                    851:        debug3("%s: receiving %d env vars", __func__, env_len);
                    852:        if (env_len != 0) {
1.159     djm       853:                cctx->env = xcalloc(env_len + 1, sizeof(*cctx->env));
1.126     djm       854:                for (i = 0; i < env_len; i++)
                    855:                        cctx->env[i] = buffer_get_string(&m, &len);
                    856:                cctx->env[i] = NULL;
                    857:        }
                    858:
1.123     djm       859:        debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__,
                    860:            cctx->want_tty, cctx->want_subsys, cmd);
1.163     markus    861:        xfree(cmd);
1.123     djm       862:
                    863:        /* Gather fds from client */
1.182     djm       864:        for(i = 0; i < 3; i++) {
                    865:                if ((new_fd[i] = mm_receive_fd(client_fd)) == -1) {
                    866:                        error("%s: failed to receive fd %d from slave",
                    867:                            __func__, i);
                    868:                        for (j = 0; j < i; j++)
                    869:                                close(new_fd[j]);
                    870:                        for (j = 0; j < env_len; j++)
                    871:                                xfree(cctx->env[j]);
                    872:                        if (env_len > 0)
                    873:                                xfree(cctx->env);
                    874:                        xfree(cctx->term);
                    875:                        buffer_free(&cctx->cmd);
1.186     djm       876:                        close(client_fd);
1.182     djm       877:                        xfree(cctx);
                    878:                        return;
                    879:                }
                    880:        }
1.123     djm       881:
                    882:        debug2("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
                    883:            new_fd[0], new_fd[1], new_fd[2]);
                    884:
                    885:        /* Try to pick up ttymodes from client before it goes raw */
                    886:        if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
                    887:                error("%s: tcgetattr: %s", __func__, strerror(errno));
                    888:
1.134     djm       889:        /* This roundtrip is just for synchronisation of ttymodes */
1.123     djm       890:        buffer_clear(&m);
1.140     djm       891:        if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
1.123     djm       892:                error("%s: client msg_send failed", __func__);
                    893:                close(client_fd);
                    894:                close(new_fd[0]);
                    895:                close(new_fd[1]);
                    896:                close(new_fd[2]);
1.127     djm       897:                buffer_free(&m);
1.134     djm       898:                xfree(cctx->term);
                    899:                if (env_len != 0) {
                    900:                        for (i = 0; i < env_len; i++)
                    901:                                xfree(cctx->env[i]);
                    902:                        xfree(cctx->env);
                    903:                }
1.123     djm       904:                return;
                    905:        }
                    906:        buffer_free(&m);
                    907:
                    908:        /* enable nonblocking unless tty */
                    909:        if (!isatty(new_fd[0]))
                    910:                set_nonblock(new_fd[0]);
                    911:        if (!isatty(new_fd[1]))
                    912:                set_nonblock(new_fd[1]);
                    913:        if (!isatty(new_fd[2]))
                    914:                set_nonblock(new_fd[2]);
                    915:
                    916:        set_nonblock(client_fd);
                    917:
1.178     djm       918:        window = CHAN_SES_WINDOW_DEFAULT;
                    919:        packetmax = CHAN_SES_PACKET_DEFAULT;
                    920:        if (cctx->want_tty) {
                    921:                window >>= 1;
                    922:                packetmax >>= 1;
                    923:        }
                    924:
1.129     deraadt   925:        c = channel_new("session", SSH_CHANNEL_OPENING,
1.178     djm       926:            new_fd[0], new_fd[1], new_fd[2], window, packetmax,
1.123     djm       927:            CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
                    928:
                    929:        /* XXX */
                    930:        c->ctl_fd = client_fd;
                    931:
                    932:        debug3("%s: channel_new: %d", __func__, c->self);
                    933:
                    934:        channel_send_open(c->self);
                    935:        channel_register_confirm(c->self, client_extra_session2_setup, cctx);
                    936: }
                    937:
                    938: static void
1.99      markus    939: process_cmdline(void)
1.97      jakob     940: {
                    941:        void (*handler)(int);
1.135     djm       942:        char *s, *cmd, *cancel_host;
1.121     djm       943:        int delete = 0;
1.97      jakob     944:        int local = 0;
1.135     djm       945:        u_short cancel_port;
                    946:        Forward fwd;
1.97      jakob     947:
1.183     djm       948:        bzero(&fwd, sizeof(fwd));
                    949:        fwd.listen_host = fwd.connect_host = NULL;
                    950:
1.97      jakob     951:        leave_raw_mode();
1.101     markus    952:        handler = signal(SIGINT, SIG_IGN);
1.99      markus    953:        cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
1.97      jakob     954:        if (s == NULL)
                    955:                goto out;
1.179     tedu      956:        while (isspace(*s))
1.97      jakob     957:                s++;
1.121     djm       958:        if (*s == '-')
                    959:                s++;    /* Skip cmdline '-', if any */
1.120     dtucker   960:        if (*s == '\0')
1.97      jakob     961:                goto out;
1.121     djm       962:
1.122     djm       963:        if (*s == 'h' || *s == 'H' || *s == '?') {
1.121     djm       964:                logit("Commands:");
1.164     djm       965:                logit("      -L[bind_address:]port:host:hostport    "
                    966:                    "Request local forward");
                    967:                logit("      -R[bind_address:]port:host:hostport    "
                    968:                    "Request remote forward");
1.165     stevesk   969:                logit("      -KR[bind_address:]port                 "
1.164     djm       970:                    "Cancel remote forward");
1.146     reyk      971:                if (!options.permit_local_command)
                    972:                        goto out;
1.164     djm       973:                logit("      !args                                  "
                    974:                    "Execute local command");
1.146     reyk      975:                goto out;
                    976:        }
                    977:
                    978:        if (*s == '!' && options.permit_local_command) {
                    979:                s++;
                    980:                ssh_local_cmd(s);
1.121     djm       981:                goto out;
                    982:        }
                    983:
                    984:        if (*s == 'K') {
                    985:                delete = 1;
                    986:                s++;
                    987:        }
                    988:        if (*s != 'L' && *s != 'R') {
1.109     itojun    989:                logit("Invalid command.");
1.97      jakob     990:                goto out;
                    991:        }
1.121     djm       992:        if (*s == 'L')
1.97      jakob     993:                local = 1;
1.121     djm       994:        if (local && delete) {
                    995:                logit("Not supported.");
                    996:                goto out;
                    997:        }
                    998:        if ((!local || delete) && !compat20) {
1.109     itojun    999:                logit("Not supported for SSH protocol version 1.");
1.97      jakob    1000:                goto out;
                   1001:        }
1.121     djm      1002:
1.179     tedu     1003:        while (isspace(*++s))
                   1004:                ;
1.97      jakob    1005:
1.121     djm      1006:        if (delete) {
1.135     djm      1007:                cancel_port = 0;
                   1008:                cancel_host = hpdelim(&s);      /* may be NULL */
                   1009:                if (s != NULL) {
                   1010:                        cancel_port = a2port(s);
                   1011:                        cancel_host = cleanhostname(cancel_host);
                   1012:                } else {
                   1013:                        cancel_port = a2port(cancel_host);
                   1014:                        cancel_host = NULL;
1.121     djm      1015:                }
1.135     djm      1016:                if (cancel_port == 0) {
                   1017:                        logit("Bad forwarding close port");
1.121     djm      1018:                        goto out;
                   1019:                }
1.135     djm      1020:                channel_request_rforward_cancel(cancel_host, cancel_port);
1.121     djm      1021:        } else {
1.135     djm      1022:                if (!parse_forward(&fwd, s)) {
1.121     djm      1023:                        logit("Bad forwarding specification.");
                   1024:                        goto out;
                   1025:                }
                   1026:                if (local) {
1.135     djm      1027:                        if (channel_setup_local_fwd_listener(fwd.listen_host,
                   1028:                            fwd.listen_port, fwd.connect_host,
                   1029:                            fwd.connect_port, options.gateway_ports) < 0) {
1.121     djm      1030:                                logit("Port forwarding failed.");
                   1031:                                goto out;
                   1032:                        }
1.135     djm      1033:                } else {
1.167     markus   1034:                        if (channel_request_remote_forwarding(fwd.listen_host,
1.135     djm      1035:                            fwd.listen_port, fwd.connect_host,
1.167     markus   1036:                            fwd.connect_port) < 0) {
                   1037:                                logit("Port forwarding failed.");
                   1038:                                goto out;
                   1039:                        }
1.135     djm      1040:                }
                   1041:
1.121     djm      1042:                logit("Forwarding port.");
                   1043:        }
                   1044:
1.97      jakob    1045: out:
                   1046:        signal(SIGINT, handler);
                   1047:        enter_raw_mode();
                   1048:        if (cmd)
                   1049:                xfree(cmd);
1.183     djm      1050:        if (fwd.listen_host != NULL)
                   1051:                xfree(fwd.listen_host);
                   1052:        if (fwd.connect_host != NULL)
                   1053:                xfree(fwd.connect_host);
1.97      jakob    1054: }
                   1055:
1.31      markus   1056: /* process the characters one by one */
1.77      itojun   1057: static int
1.31      markus   1058: process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
                   1059: {
1.32      markus   1060:        char string[1024];
1.31      markus   1061:        pid_t pid;
                   1062:        int bytes = 0;
1.42      markus   1063:        u_int i;
                   1064:        u_char ch;
1.31      markus   1065:        char *s;
                   1066:
1.139     djm      1067:        if (len <= 0)
                   1068:                return (0);
                   1069:
                   1070:        for (i = 0; i < (u_int)len; i++) {
1.31      markus   1071:                /* Get one character at a time. */
                   1072:                ch = buf[i];
                   1073:
                   1074:                if (escape_pending) {
                   1075:                        /* We have previously seen an escape character. */
                   1076:                        /* Clear the flag now. */
                   1077:                        escape_pending = 0;
                   1078:
                   1079:                        /* Process the escaped character. */
                   1080:                        switch (ch) {
                   1081:                        case '.':
                   1082:                                /* Terminate the connection. */
1.32      markus   1083:                                snprintf(string, sizeof string, "%c.\r\n", escape_char);
                   1084:                                buffer_append(berr, string, strlen(string));
1.31      markus   1085:
                   1086:                                quit_pending = 1;
                   1087:                                return -1;
                   1088:
                   1089:                        case 'Z' - 64:
                   1090:                                /* Suspend the program. */
                   1091:                                /* Print a message to that effect to the user. */
1.32      markus   1092:                                snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
                   1093:                                buffer_append(berr, string, strlen(string));
1.31      markus   1094:
                   1095:                                /* Restore terminal modes and suspend. */
                   1096:                                client_suspend_self(bin, bout, berr);
                   1097:
                   1098:                                /* We have been continued. */
                   1099:                                continue;
                   1100:
1.111     markus   1101:                        case 'B':
                   1102:                                if (compat20) {
                   1103:                                        snprintf(string, sizeof string,
                   1104:                                            "%cB\r\n", escape_char);
                   1105:                                        buffer_append(berr, string,
                   1106:                                            strlen(string));
                   1107:                                        channel_request_start(session_ident,
                   1108:                                            "break", 0);
                   1109:                                        packet_put_int(1000);
                   1110:                                        packet_send();
                   1111:                                }
                   1112:                                continue;
                   1113:
1.54      markus   1114:                        case 'R':
1.59      markus   1115:                                if (compat20) {
                   1116:                                        if (datafellows & SSH_BUG_NOREKEY)
1.109     itojun   1117:                                                logit("Server does not support re-keying");
1.59      markus   1118:                                        else
                   1119:                                                need_rekeying = 1;
                   1120:                                }
1.54      markus   1121:                                continue;
                   1122:
1.31      markus   1123:                        case '&':
                   1124:                                /*
                   1125:                                 * Detach the program (continue to serve connections,
                   1126:                                 * but put in background and no more new connections).
                   1127:                                 */
                   1128:                                /* Restore tty modes. */
                   1129:                                leave_raw_mode();
                   1130:
                   1131:                                /* Stop listening for new connections. */
1.86      markus   1132:                                channel_stop_listening();
1.31      markus   1133:
1.86      markus   1134:                                snprintf(string, sizeof string,
                   1135:                                    "%c& [backgrounded]\n", escape_char);
                   1136:                                buffer_append(berr, string, strlen(string));
1.31      markus   1137:
                   1138:                                /* Fork into background. */
                   1139:                                pid = fork();
                   1140:                                if (pid < 0) {
                   1141:                                        error("fork: %.100s", strerror(errno));
                   1142:                                        continue;
                   1143:                                }
                   1144:                                if (pid != 0) { /* This is the parent. */
                   1145:                                        /* The parent just exits. */
                   1146:                                        exit(0);
                   1147:                                }
                   1148:                                /* The child continues serving connections. */
1.86      markus   1149:                                if (compat20) {
                   1150:                                        buffer_append(bin, "\004", 1);
                   1151:                                        /* fake EOF on stdin */
                   1152:                                        return -1;
                   1153:                                } else if (!stdin_eof) {
                   1154:                                        /*
                   1155:                                         * Sending SSH_CMSG_EOF alone does not always appear
                   1156:                                         * to be enough.  So we try to send an EOF character
                   1157:                                         * first.
                   1158:                                         */
                   1159:                                        packet_start(SSH_CMSG_STDIN_DATA);
                   1160:                                        packet_put_string("\004", 1);
                   1161:                                        packet_send();
                   1162:                                        /* Close stdin. */
                   1163:                                        stdin_eof = 1;
                   1164:                                        if (buffer_len(bin) == 0) {
                   1165:                                                packet_start(SSH_CMSG_EOF);
                   1166:                                                packet_send();
                   1167:                                        }
                   1168:                                }
                   1169:                                continue;
1.31      markus   1170:
                   1171:                        case '?':
1.32      markus   1172:                                snprintf(string, sizeof string,
1.31      markus   1173: "%c?\r\n\
                   1174: Supported escape sequences:\r\n\
1.104     stevesk  1175: %c.  - terminate connection\r\n\
1.111     markus   1176: %cB  - send a BREAK to the remote system\r\n\
1.104     stevesk  1177: %cC  - open a command line\r\n\
                   1178: %cR  - Request rekey (SSH protocol 2 only)\r\n\
                   1179: %c^Z - suspend ssh\r\n\
                   1180: %c#  - list forwarded connections\r\n\
                   1181: %c&  - background ssh (when waiting for connections to terminate)\r\n\
                   1182: %c?  - this message\r\n\
                   1183: %c%c  - send the escape character by typing it twice\r\n\
1.31      markus   1184: (Note that escapes are only recognized immediately after newline.)\r\n",
1.104     stevesk  1185:                                    escape_char, escape_char, escape_char, escape_char,
                   1186:                                    escape_char, escape_char, escape_char, escape_char,
1.111     markus   1187:                                    escape_char, escape_char, escape_char);
1.32      markus   1188:                                buffer_append(berr, string, strlen(string));
1.31      markus   1189:                                continue;
                   1190:
                   1191:                        case '#':
1.32      markus   1192:                                snprintf(string, sizeof string, "%c#\r\n", escape_char);
                   1193:                                buffer_append(berr, string, strlen(string));
1.31      markus   1194:                                s = channel_open_message();
                   1195:                                buffer_append(berr, s, strlen(s));
                   1196:                                xfree(s);
1.97      jakob    1197:                                continue;
                   1198:
                   1199:                        case 'C':
1.99      markus   1200:                                process_cmdline();
1.31      markus   1201:                                continue;
                   1202:
                   1203:                        default:
                   1204:                                if (ch != escape_char) {
                   1205:                                        buffer_put_char(bin, escape_char);
                   1206:                                        bytes++;
                   1207:                                }
                   1208:                                /* Escaped characters fall through here */
                   1209:                                break;
                   1210:                        }
                   1211:                } else {
                   1212:                        /*
                   1213:                         * The previous character was not an escape char. Check if this
                   1214:                         * is an escape.
                   1215:                         */
                   1216:                        if (last_was_cr && ch == escape_char) {
                   1217:                                /* It is. Set the flag and continue to next character. */
                   1218:                                escape_pending = 1;
                   1219:                                continue;
                   1220:                        }
                   1221:                }
                   1222:
                   1223:                /*
                   1224:                 * Normal character.  Record whether it was a newline,
                   1225:                 * and append it to the buffer.
                   1226:                 */
                   1227:                last_was_cr = (ch == '\r' || ch == '\n');
                   1228:                buffer_put_char(bin, ch);
                   1229:                bytes++;
                   1230:        }
                   1231:        return bytes;
                   1232: }
                   1233:
1.77      itojun   1234: static void
1.161     deraadt  1235: client_process_input(fd_set *readset)
1.17      markus   1236: {
1.21      deraadt  1237:        int len;
1.31      markus   1238:        char buf[8192];
1.16      markus   1239:
1.11      markus   1240:        /* Read input from stdin. */
                   1241:        if (FD_ISSET(fileno(stdin), readset)) {
                   1242:                /* Read as much as possible. */
                   1243:                len = read(fileno(stdin), buf, sizeof(buf));
1.64      markus   1244:                if (len < 0 && (errno == EAGAIN || errno == EINTR))
                   1245:                        return;         /* we'll try again later */
1.11      markus   1246:                if (len <= 0) {
1.13      markus   1247:                        /*
                   1248:                         * Received EOF or error.  They are treated
                   1249:                         * similarly, except that an error message is printed
                   1250:                         * if it was an error condition.
                   1251:                         */
1.11      markus   1252:                        if (len < 0) {
                   1253:                                snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
                   1254:                                buffer_append(&stderr_buffer, buf, strlen(buf));
                   1255:                        }
                   1256:                        /* Mark that we have seen EOF. */
                   1257:                        stdin_eof = 1;
1.13      markus   1258:                        /*
                   1259:                         * Send an EOF message to the server unless there is
                   1260:                         * data in the buffer.  If there is data in the
                   1261:                         * buffer, no message will be sent now.  Code
                   1262:                         * elsewhere will send the EOF when the buffer
                   1263:                         * becomes empty if stdin_eof is set.
                   1264:                         */
1.11      markus   1265:                        if (buffer_len(&stdin_buffer) == 0) {
1.1       deraadt  1266:                                packet_start(SSH_CMSG_EOF);
                   1267:                                packet_send();
1.11      markus   1268:                        }
1.72      stevesk  1269:                } else if (escape_char == SSH_ESCAPECHAR_NONE) {
1.13      markus   1270:                        /*
                   1271:                         * Normal successful read, and no escape character.
                   1272:                         * Just append the data to buffer.
                   1273:                         */
1.11      markus   1274:                        buffer_append(&stdin_buffer, buf, len);
                   1275:                } else {
1.13      markus   1276:                        /*
                   1277:                         * Normal, successful read.  But we have an escape character
                   1278:                         * and have to process the characters one by one.
                   1279:                         */
1.52      markus   1280:                        if (process_escapes(&stdin_buffer, &stdout_buffer,
                   1281:                            &stderr_buffer, buf, len) == -1)
1.31      markus   1282:                                return;
1.11      markus   1283:                }
                   1284:        }
                   1285: }
                   1286:
1.77      itojun   1287: static void
1.161     deraadt  1288: client_process_output(fd_set *writeset)
1.11      markus   1289: {
                   1290:        int len;
                   1291:        char buf[100];
1.1       deraadt  1292:
1.11      markus   1293:        /* Write buffered output to stdout. */
                   1294:        if (FD_ISSET(fileno(stdout), writeset)) {
                   1295:                /* Write as much data as possible. */
                   1296:                len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
1.14      deraadt  1297:                    buffer_len(&stdout_buffer));
1.11      markus   1298:                if (len <= 0) {
1.64      markus   1299:                        if (errno == EINTR || errno == EAGAIN)
1.11      markus   1300:                                len = 0;
                   1301:                        else {
1.13      markus   1302:                                /*
                   1303:                                 * An error or EOF was encountered.  Put an
                   1304:                                 * error message to stderr buffer.
                   1305:                                 */
1.11      markus   1306:                                snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
                   1307:                                buffer_append(&stderr_buffer, buf, strlen(buf));
                   1308:                                quit_pending = 1;
                   1309:                                return;
                   1310:                        }
                   1311:                }
                   1312:                /* Consume printed data from the buffer. */
                   1313:                buffer_consume(&stdout_buffer, len);
1.57      markus   1314:                stdout_bytes += len;
1.11      markus   1315:        }
                   1316:        /* Write buffered output to stderr. */
                   1317:        if (FD_ISSET(fileno(stderr), writeset)) {
                   1318:                /* Write as much data as possible. */
                   1319:                len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
1.14      deraadt  1320:                    buffer_len(&stderr_buffer));
1.11      markus   1321:                if (len <= 0) {
1.64      markus   1322:                        if (errno == EINTR || errno == EAGAIN)
1.11      markus   1323:                                len = 0;
                   1324:                        else {
1.13      markus   1325:                                /* EOF or error, but can't even print error message. */
1.11      markus   1326:                                quit_pending = 1;
                   1327:                                return;
                   1328:                        }
                   1329:                }
                   1330:                /* Consume printed characters from the buffer. */
                   1331:                buffer_consume(&stderr_buffer, len);
1.57      markus   1332:                stderr_bytes += len;
1.11      markus   1333:        }
1.1       deraadt  1334: }
                   1335:
1.13      markus   1336: /*
1.15      markus   1337:  * Get packets from the connection input buffer, and process them as long as
                   1338:  * there are packets available.
                   1339:  *
                   1340:  * Any unknown packets received during the actual
                   1341:  * session cause the session to terminate.  This is
                   1342:  * intended to make debugging easier since no
                   1343:  * confirmations are sent.  Any compatible protocol
                   1344:  * extensions must be negotiated during the
                   1345:  * preparatory phase.
                   1346:  */
                   1347:
1.77      itojun   1348: static void
1.49      itojun   1349: client_process_buffered_input_packets(void)
1.15      markus   1350: {
1.54      markus   1351:        dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
1.15      markus   1352: }
                   1353:
1.31      markus   1354: /* scan buf[] for '~' before sending data to the peer */
1.30      markus   1355:
1.77      itojun   1356: static int
1.31      markus   1357: simple_escape_filter(Channel *c, char *buf, int len)
1.30      markus   1358: {
1.31      markus   1359:        /* XXX we assume c->extended is writeable */
                   1360:        return process_escapes(&c->input, &c->output, &c->extended, buf, len);
1.30      markus   1361: }
                   1362:
1.77      itojun   1363: static void
1.60      markus   1364: client_channel_closed(int id, void *arg)
                   1365: {
1.83      markus   1366:        channel_cancel_cleanup(id);
1.60      markus   1367:        session_closed = 1;
1.113     markus   1368:        leave_raw_mode();
1.60      markus   1369: }
                   1370:
1.15      markus   1371: /*
1.13      markus   1372:  * Implements the interactive session with the server.  This is called after
                   1373:  * the user has been authenticated, and a command has been started on the
1.72      stevesk  1374:  * remote host.  If escape_char != SSH_ESCAPECHAR_NONE, it is the character
                   1375:  * used as an escape character for terminating or suspending the session.
1.13      markus   1376:  */
1.1       deraadt  1377:
1.20      markus   1378: int
1.30      markus   1379: client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
1.1       deraadt  1380: {
1.46      markus   1381:        fd_set *readset = NULL, *writeset = NULL;
1.11      markus   1382:        double start_time, total_time;
1.130     avsm     1383:        int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
                   1384:        u_int nalloc = 0;
1.11      markus   1385:        char buf[100];
                   1386:
                   1387:        debug("Entering interactive session.");
                   1388:
                   1389:        start_time = get_current_time();
                   1390:
                   1391:        /* Initialize variables. */
                   1392:        escape_pending = 0;
                   1393:        last_was_cr = 1;
                   1394:        exit_status = -1;
                   1395:        stdin_eof = 0;
                   1396:        buffer_high = 64 * 1024;
                   1397:        connection_in = packet_get_connection_in();
                   1398:        connection_out = packet_get_connection_out();
1.46      markus   1399:        max_fd = MAX(connection_in, connection_out);
1.123     djm      1400:        if (control_fd != -1)
                   1401:                max_fd = MAX(max_fd, control_fd);
1.46      markus   1402:
                   1403:        if (!compat20) {
1.63      markus   1404:                /* enable nonblocking unless tty */
                   1405:                if (!isatty(fileno(stdin)))
                   1406:                        set_nonblock(fileno(stdin));
                   1407:                if (!isatty(fileno(stdout)))
                   1408:                        set_nonblock(fileno(stdout));
                   1409:                if (!isatty(fileno(stderr)))
                   1410:                        set_nonblock(fileno(stderr));
1.46      markus   1411:                max_fd = MAX(max_fd, fileno(stdin));
                   1412:                max_fd = MAX(max_fd, fileno(stdout));
                   1413:                max_fd = MAX(max_fd, fileno(stderr));
                   1414:        }
1.11      markus   1415:        stdin_bytes = 0;
                   1416:        stdout_bytes = 0;
                   1417:        stderr_bytes = 0;
                   1418:        quit_pending = 0;
                   1419:        escape_char = escape_char_arg;
                   1420:
                   1421:        /* Initialize buffers. */
                   1422:        buffer_init(&stdin_buffer);
                   1423:        buffer_init(&stdout_buffer);
                   1424:        buffer_init(&stderr_buffer);
                   1425:
1.15      markus   1426:        client_init_dispatch();
                   1427:
1.105     markus   1428:        /*
                   1429:         * Set signal handlers, (e.g. to restore non-blocking mode)
                   1430:         * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
                   1431:         */
1.131     djm      1432:        if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
                   1433:                signal(SIGHUP, signal_handler);
1.105     markus   1434:        if (signal(SIGINT, SIG_IGN) != SIG_IGN)
                   1435:                signal(SIGINT, signal_handler);
                   1436:        if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
                   1437:                signal(SIGQUIT, signal_handler);
                   1438:        if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
                   1439:                signal(SIGTERM, signal_handler);
1.132     djm      1440:        signal(SIGWINCH, window_change_handler);
1.11      markus   1441:
                   1442:        if (have_pty)
                   1443:                enter_raw_mode();
                   1444:
1.48      markus   1445:        if (compat20) {
                   1446:                session_ident = ssh2_chan_id;
1.72      stevesk  1447:                if (escape_char != SSH_ESCAPECHAR_NONE)
1.48      markus   1448:                        channel_register_filter(session_ident,
1.149     reyk     1449:                            simple_escape_filter, NULL);
1.60      markus   1450:                if (session_ident != -1)
                   1451:                        channel_register_cleanup(session_ident,
1.143     djm      1452:                            client_channel_closed, 0);
1.48      markus   1453:        } else {
                   1454:                /* Check if we should immediately send eof on stdin. */
1.16      markus   1455:                client_check_initial_eof_on_stdin();
1.48      markus   1456:        }
1.30      markus   1457:
1.11      markus   1458:        /* Main loop of the client for the interactive session mode. */
                   1459:        while (!quit_pending) {
                   1460:
1.13      markus   1461:                /* Process buffered packets sent by the server. */
1.11      markus   1462:                client_process_buffered_input_packets();
                   1463:
1.60      markus   1464:                if (compat20 && session_closed && !channel_still_open())
                   1465:                        break;
                   1466:
1.56      markus   1467:                rekeying = (xxx_kex != NULL && !xxx_kex->done);
1.16      markus   1468:
1.56      markus   1469:                if (rekeying) {
                   1470:                        debug("rekeying in progress");
                   1471:                } else {
                   1472:                        /*
                   1473:                         * Make packets of buffered stdin data, and buffer
                   1474:                         * them for sending to the server.
                   1475:                         */
                   1476:                        if (!compat20)
                   1477:                                client_make_packets_from_stdin_data();
1.11      markus   1478:
1.56      markus   1479:                        /*
                   1480:                         * Make packets from buffered channel data, and
                   1481:                         * enqueue them for sending to the server.
                   1482:                         */
                   1483:                        if (packet_not_very_much_data_to_write())
                   1484:                                channel_output_poll();
1.11      markus   1485:
1.56      markus   1486:                        /*
                   1487:                         * Check if the window size has changed, and buffer a
                   1488:                         * message about it to the server if so.
                   1489:                         */
                   1490:                        client_check_window_change();
1.11      markus   1491:
1.56      markus   1492:                        if (quit_pending)
                   1493:                                break;
                   1494:                }
1.13      markus   1495:                /*
                   1496:                 * Wait until we have something to do (something becomes
                   1497:                 * available on one of the descriptors).
                   1498:                 */
1.81      markus   1499:                max_fd2 = max_fd;
1.56      markus   1500:                client_wait_until_can_do_something(&readset, &writeset,
1.81      markus   1501:                    &max_fd2, &nalloc, rekeying);
1.11      markus   1502:
                   1503:                if (quit_pending)
                   1504:                        break;
                   1505:
1.56      markus   1506:                /* Do channel operations unless rekeying in progress. */
                   1507:                if (!rekeying) {
                   1508:                        channel_after_select(readset, writeset);
1.108     markus   1509:                        if (need_rekeying || packet_need_rekeying()) {
                   1510:                                debug("need rekeying");
1.56      markus   1511:                                xxx_kex->done = 0;
                   1512:                                kex_send_kexinit(xxx_kex);
                   1513:                                need_rekeying = 0;
                   1514:                        }
                   1515:                }
1.11      markus   1516:
1.17      markus   1517:                /* Buffer input from the connection.  */
1.46      markus   1518:                client_process_net_input(readset);
1.17      markus   1519:
1.123     djm      1520:                /* Accept control connections.  */
                   1521:                client_process_control(readset);
                   1522:
1.17      markus   1523:                if (quit_pending)
                   1524:                        break;
1.11      markus   1525:
1.17      markus   1526:                if (!compat20) {
                   1527:                        /* Buffer data from stdin */
1.46      markus   1528:                        client_process_input(readset);
1.17      markus   1529:                        /*
                   1530:                         * Process output to stdout and stderr.  Output to
                   1531:                         * the connection is processed elsewhere (above).
                   1532:                         */
1.46      markus   1533:                        client_process_output(writeset);
1.17      markus   1534:                }
1.11      markus   1535:
1.13      markus   1536:                /* Send as much buffered packet data as possible to the sender. */
1.46      markus   1537:                if (FD_ISSET(connection_out, writeset))
1.11      markus   1538:                        packet_write_poll();
                   1539:        }
1.46      markus   1540:        if (readset)
                   1541:                xfree(readset);
                   1542:        if (writeset)
                   1543:                xfree(writeset);
1.11      markus   1544:
                   1545:        /* Terminate the session. */
                   1546:
                   1547:        /* Stop watching for window change. */
1.132     djm      1548:        signal(SIGWINCH, SIG_DFL);
1.11      markus   1549:
1.76      markus   1550:        channel_free_all();
1.11      markus   1551:
1.75      markus   1552:        if (have_pty)
                   1553:                leave_raw_mode();
                   1554:
                   1555:        /* restore blocking io */
                   1556:        if (!isatty(fileno(stdin)))
                   1557:                unset_nonblock(fileno(stdin));
                   1558:        if (!isatty(fileno(stdout)))
                   1559:                unset_nonblock(fileno(stdout));
                   1560:        if (!isatty(fileno(stderr)))
                   1561:                unset_nonblock(fileno(stderr));
1.116     dtucker  1562:
                   1563:        /*
                   1564:         * If there was no shell or command requested, there will be no remote
                   1565:         * exit status to be returned.  In that case, clear error code if the
                   1566:         * connection was deliberately terminated at this end.
                   1567:         */
                   1568:        if (no_shell_flag && received_signal == SIGTERM) {
                   1569:                received_signal = 0;
                   1570:                exit_status = 0;
                   1571:        }
1.75      markus   1572:
1.113     markus   1573:        if (received_signal)
1.89      itojun   1574:                fatal("Killed by signal %d.", (int) received_signal);
1.75      markus   1575:
1.13      markus   1576:        /*
                   1577:         * In interactive mode (with pseudo tty) display a message indicating
                   1578:         * that the connection has been closed.
                   1579:         */
1.11      markus   1580:        if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
                   1581:                snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
                   1582:                buffer_append(&stderr_buffer, buf, strlen(buf));
                   1583:        }
1.70      markus   1584:
1.11      markus   1585:        /* Output any buffered data for stdout. */
1.70      markus   1586:        while (buffer_len(&stdout_buffer) > 0) {
                   1587:                len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
1.14      deraadt  1588:                    buffer_len(&stdout_buffer));
1.70      markus   1589:                if (len <= 0) {
1.11      markus   1590:                        error("Write failed flushing stdout buffer.");
1.70      markus   1591:                        break;
                   1592:                }
1.11      markus   1593:                buffer_consume(&stdout_buffer, len);
1.57      markus   1594:                stdout_bytes += len;
1.11      markus   1595:        }
                   1596:
                   1597:        /* Output any buffered data for stderr. */
1.70      markus   1598:        while (buffer_len(&stderr_buffer) > 0) {
                   1599:                len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
1.14      deraadt  1600:                    buffer_len(&stderr_buffer));
1.70      markus   1601:                if (len <= 0) {
1.11      markus   1602:                        error("Write failed flushing stderr buffer.");
1.70      markus   1603:                        break;
                   1604:                }
1.11      markus   1605:                buffer_consume(&stderr_buffer, len);
1.57      markus   1606:                stderr_bytes += len;
1.11      markus   1607:        }
                   1608:
                   1609:        /* Clear and free any buffers. */
                   1610:        memset(buf, 0, sizeof(buf));
                   1611:        buffer_free(&stdin_buffer);
                   1612:        buffer_free(&stdout_buffer);
                   1613:        buffer_free(&stderr_buffer);
                   1614:
                   1615:        /* Report bytes transferred, and transfer rates. */
                   1616:        total_time = get_current_time() - start_time;
                   1617:        debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
1.90      deraadt  1618:            stdin_bytes, stdout_bytes, stderr_bytes, total_time);
1.11      markus   1619:        if (total_time > 0)
                   1620:                debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
1.90      deraadt  1621:                    stdin_bytes / total_time, stdout_bytes / total_time,
                   1622:                    stderr_bytes / total_time);
1.11      markus   1623:
                   1624:        /* Return the exit status of the program. */
                   1625:        debug("Exit status %d", exit_status);
                   1626:        return exit_status;
1.15      markus   1627: }
                   1628:
                   1629: /*********/
                   1630:
1.77      itojun   1631: static void
1.94      markus   1632: client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
1.15      markus   1633: {
1.42      markus   1634:        u_int data_len;
1.15      markus   1635:        char *data = packet_get_string(&data_len);
1.93      markus   1636:        packet_check_eom();
1.15      markus   1637:        buffer_append(&stdout_buffer, data, data_len);
                   1638:        memset(data, 0, data_len);
                   1639:        xfree(data);
                   1640: }
1.77      itojun   1641: static void
1.94      markus   1642: client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
1.15      markus   1643: {
1.42      markus   1644:        u_int data_len;
1.15      markus   1645:        char *data = packet_get_string(&data_len);
1.93      markus   1646:        packet_check_eom();
1.15      markus   1647:        buffer_append(&stderr_buffer, data, data_len);
                   1648:        memset(data, 0, data_len);
                   1649:        xfree(data);
                   1650: }
1.77      itojun   1651: static void
1.94      markus   1652: client_input_exit_status(int type, u_int32_t seq, void *ctxt)
1.15      markus   1653: {
                   1654:        exit_status = packet_get_int();
1.93      markus   1655:        packet_check_eom();
1.15      markus   1656:        /* Acknowledge the exit. */
                   1657:        packet_start(SSH_CMSG_EXIT_CONFIRMATION);
                   1658:        packet_send();
                   1659:        /*
                   1660:         * Must wait for packet to be sent since we are
                   1661:         * exiting the loop.
                   1662:         */
                   1663:        packet_write_wait();
                   1664:        /* Flag that we want to exit. */
                   1665:        quit_pending = 1;
                   1666: }
1.115     markus   1667: static void
                   1668: client_input_agent_open(int type, u_int32_t seq, void *ctxt)
                   1669: {
                   1670:        Channel *c = NULL;
                   1671:        int remote_id, sock;
                   1672:
                   1673:        /* Read the remote channel number from the message. */
                   1674:        remote_id = packet_get_int();
                   1675:        packet_check_eom();
                   1676:
                   1677:        /*
                   1678:         * Get a connection to the local authentication agent (this may again
                   1679:         * get forwarded).
                   1680:         */
                   1681:        sock = ssh_get_authentication_socket();
                   1682:
                   1683:        /*
                   1684:         * If we could not connect the agent, send an error message back to
                   1685:         * the server. This should never happen unless the agent dies,
                   1686:         * because authentication forwarding is only enabled if we have an
                   1687:         * agent.
                   1688:         */
                   1689:        if (sock >= 0) {
                   1690:                c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
                   1691:                    -1, 0, 0, 0, "authentication agent connection", 1);
                   1692:                c->remote_id = remote_id;
                   1693:                c->force_drain = 1;
                   1694:        }
                   1695:        if (c == NULL) {
                   1696:                packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
                   1697:                packet_put_int(remote_id);
                   1698:        } else {
                   1699:                /* Send a confirmation to the remote host. */
                   1700:                debug("Forwarding authentication connection.");
                   1701:                packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
                   1702:                packet_put_int(remote_id);
                   1703:                packet_put_int(c->self);
                   1704:        }
                   1705:        packet_send();
                   1706: }
1.15      markus   1707:
1.77      itojun   1708: static Channel *
1.40      markus   1709: client_request_forwarded_tcpip(const char *request_type, int rchan)
                   1710: {
1.103     deraadt  1711:        Channel *c = NULL;
1.40      markus   1712:        char *listen_address, *originator_address;
                   1713:        int listen_port, originator_port;
1.67      markus   1714:        int sock;
1.40      markus   1715:
                   1716:        /* Get rest of the packet */
                   1717:        listen_address = packet_get_string(NULL);
                   1718:        listen_port = packet_get_int();
                   1719:        originator_address = packet_get_string(NULL);
                   1720:        originator_port = packet_get_int();
1.93      markus   1721:        packet_check_eom();
1.40      markus   1722:
                   1723:        debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
                   1724:            listen_address, listen_port, originator_address, originator_port);
                   1725:
1.80      stevesk  1726:        sock = channel_connect_by_listen_address(listen_port);
1.67      markus   1727:        if (sock < 0) {
                   1728:                xfree(originator_address);
                   1729:                xfree(listen_address);
                   1730:                return NULL;
                   1731:        }
                   1732:        c = channel_new("forwarded-tcpip",
                   1733:            SSH_CHANNEL_CONNECTING, sock, sock, -1,
1.185     dtucker  1734:            CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
1.110     markus   1735:            originator_address, 1);
1.40      markus   1736:        xfree(originator_address);
                   1737:        xfree(listen_address);
                   1738:        return c;
                   1739: }
                   1740:
1.103     deraadt  1741: static Channel *
1.40      markus   1742: client_request_x11(const char *request_type, int rchan)
                   1743: {
                   1744:        Channel *c = NULL;
                   1745:        char *originator;
                   1746:        int originator_port;
1.67      markus   1747:        int sock;
1.40      markus   1748:
                   1749:        if (!options.forward_x11) {
                   1750:                error("Warning: ssh server tried X11 forwarding.");
1.148     stevesk  1751:                error("Warning: this is probably a break-in attempt by a malicious server.");
1.40      markus   1752:                return NULL;
                   1753:        }
                   1754:        originator = packet_get_string(NULL);
                   1755:        if (datafellows & SSH_BUG_X11FWD) {
                   1756:                debug2("buggy server: x11 request w/o originator_port");
                   1757:                originator_port = 0;
                   1758:        } else {
                   1759:                originator_port = packet_get_int();
                   1760:        }
1.93      markus   1761:        packet_check_eom();
1.40      markus   1762:        /* XXX check permission */
1.47      markus   1763:        debug("client_request_x11: request from %s %d", originator,
                   1764:            originator_port);
1.67      markus   1765:        xfree(originator);
1.40      markus   1766:        sock = x11_connect_display();
1.67      markus   1767:        if (sock < 0)
                   1768:                return NULL;
                   1769:        c = channel_new("x11",
                   1770:            SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1.110     markus   1771:            CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
1.82      markus   1772:        c->force_drain = 1;
1.40      markus   1773:        return c;
                   1774: }
                   1775:
1.103     deraadt  1776: static Channel *
1.40      markus   1777: client_request_agent(const char *request_type, int rchan)
                   1778: {
                   1779:        Channel *c = NULL;
1.67      markus   1780:        int sock;
1.40      markus   1781:
                   1782:        if (!options.forward_agent) {
                   1783:                error("Warning: ssh server tried agent forwarding.");
1.148     stevesk  1784:                error("Warning: this is probably a break-in attempt by a malicious server.");
1.40      markus   1785:                return NULL;
                   1786:        }
1.177     stevesk  1787:        sock = ssh_get_authentication_socket();
1.67      markus   1788:        if (sock < 0)
                   1789:                return NULL;
                   1790:        c = channel_new("authentication agent connection",
                   1791:            SSH_CHANNEL_OPEN, sock, sock, -1,
1.185     dtucker  1792:            CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
1.110     markus   1793:            "authentication agent connection", 1);
1.82      markus   1794:        c->force_drain = 1;
1.40      markus   1795:        return c;
1.180     djm      1796: }
                   1797:
                   1798: int
                   1799: client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
                   1800: {
                   1801:        Channel *c;
                   1802:        int fd;
                   1803:
                   1804:        if (tun_mode == SSH_TUNMODE_NO)
                   1805:                return 0;
                   1806:
                   1807:        if (!compat20) {
                   1808:                error("Tunnel forwarding is not support for protocol 1");
                   1809:                return -1;
                   1810:        }
                   1811:
                   1812:        debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
                   1813:
                   1814:        /* Open local tunnel device */
                   1815:        if ((fd = tun_open(local_tun, tun_mode)) == -1) {
                   1816:                error("Tunnel device open failed.");
                   1817:                return -1;
                   1818:        }
                   1819:
                   1820:        c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
                   1821:            CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
                   1822:        c->datagram = 1;
                   1823:
                   1824:        packet_start(SSH2_MSG_CHANNEL_OPEN);
                   1825:        packet_put_cstring("tun@openssh.com");
                   1826:        packet_put_int(c->self);
                   1827:        packet_put_int(c->local_window_max);
                   1828:        packet_put_int(c->local_maxpacket);
                   1829:        packet_put_int(tun_mode);
                   1830:        packet_put_int(remote_tun);
                   1831:        packet_send();
                   1832:
                   1833:        return 0;
1.40      markus   1834: }
                   1835:
1.22      markus   1836: /* XXXX move to generic input handler */
1.77      itojun   1837: static void
1.94      markus   1838: client_input_channel_open(int type, u_int32_t seq, void *ctxt)
1.22      markus   1839: {
                   1840:        Channel *c = NULL;
                   1841:        char *ctype;
                   1842:        int rchan;
1.102     markus   1843:        u_int rmaxpack, rwindow, len;
1.22      markus   1844:
                   1845:        ctype = packet_get_string(&len);
                   1846:        rchan = packet_get_int();
                   1847:        rwindow = packet_get_int();
                   1848:        rmaxpack = packet_get_int();
                   1849:
1.24      markus   1850:        debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
1.22      markus   1851:            ctype, rchan, rwindow, rmaxpack);
                   1852:
1.40      markus   1853:        if (strcmp(ctype, "forwarded-tcpip") == 0) {
                   1854:                c = client_request_forwarded_tcpip(ctype, rchan);
                   1855:        } else if (strcmp(ctype, "x11") == 0) {
                   1856:                c = client_request_x11(ctype, rchan);
                   1857:        } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
                   1858:                c = client_request_agent(ctype, rchan);
1.22      markus   1859:        }
                   1860: /* XXX duplicate : */
                   1861:        if (c != NULL) {
                   1862:                debug("confirm %s", ctype);
                   1863:                c->remote_id = rchan;
                   1864:                c->remote_window = rwindow;
                   1865:                c->remote_maxpacket = rmaxpack;
1.69      markus   1866:                if (c->type != SSH_CHANNEL_CONNECTING) {
                   1867:                        packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
                   1868:                        packet_put_int(c->remote_id);
                   1869:                        packet_put_int(c->self);
                   1870:                        packet_put_int(c->local_window);
                   1871:                        packet_put_int(c->local_maxpacket);
                   1872:                        packet_send();
                   1873:                }
1.22      markus   1874:        } else {
                   1875:                debug("failure %s", ctype);
                   1876:                packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
                   1877:                packet_put_int(rchan);
                   1878:                packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
1.66      markus   1879:                if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1.69      markus   1880:                        packet_put_cstring("open failed");
1.66      markus   1881:                        packet_put_cstring("");
                   1882:                }
1.22      markus   1883:                packet_send();
                   1884:        }
                   1885:        xfree(ctype);
                   1886: }
1.77      itojun   1887: static void
1.94      markus   1888: client_input_channel_req(int type, u_int32_t seq, void *ctxt)
1.48      markus   1889: {
                   1890:        Channel *c = NULL;
1.123     djm      1891:        int exitval, id, reply, success = 0;
1.48      markus   1892:        char *rtype;
                   1893:
                   1894:        id = packet_get_int();
                   1895:        rtype = packet_get_string(NULL);
                   1896:        reply = packet_get_char();
                   1897:
                   1898:        debug("client_input_channel_req: channel %d rtype %s reply %d",
                   1899:            id, rtype, reply);
                   1900:
1.128     djm      1901:        if (id == -1) {
                   1902:                error("client_input_channel_req: request for channel -1");
                   1903:        } else if ((c = channel_lookup(id)) == NULL) {
1.48      markus   1904:                error("client_input_channel_req: channel %d: unknown channel", id);
                   1905:        } else if (strcmp(rtype, "exit-status") == 0) {
1.123     djm      1906:                exitval = packet_get_int();
                   1907:                if (id == session_ident) {
                   1908:                        success = 1;
                   1909:                        exit_status = exitval;
                   1910:                } else if (c->ctl_fd == -1) {
                   1911:                        error("client_input_channel_req: unexpected channel %d",
                   1912:                            session_ident);
                   1913:                } else {
                   1914:                        atomicio(vwrite, c->ctl_fd, &exitval, sizeof(exitval));
                   1915:                        success = 1;
                   1916:                }
1.93      markus   1917:                packet_check_eom();
1.48      markus   1918:        }
                   1919:        if (reply) {
                   1920:                packet_start(success ?
                   1921:                    SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1.128     djm      1922:                packet_put_int(id);
1.48      markus   1923:                packet_send();
                   1924:        }
                   1925:        xfree(rtype);
                   1926: }
1.95      markus   1927: static void
                   1928: client_input_global_request(int type, u_int32_t seq, void *ctxt)
                   1929: {
                   1930:        char *rtype;
                   1931:        int want_reply;
                   1932:        int success = 0;
                   1933:
                   1934:        rtype = packet_get_string(NULL);
                   1935:        want_reply = packet_get_char();
1.117     markus   1936:        debug("client_input_global_request: rtype %s want_reply %d",
                   1937:            rtype, want_reply);
1.95      markus   1938:        if (want_reply) {
                   1939:                packet_start(success ?
                   1940:                    SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
                   1941:                packet_send();
                   1942:                packet_write_wait();
                   1943:        }
                   1944:        xfree(rtype);
                   1945: }
1.22      markus   1946:
1.123     djm      1947: void
1.129     deraadt  1948: client_session2_setup(int id, int want_tty, int want_subsystem,
1.126     djm      1949:     const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env,
1.123     djm      1950:     dispatch_fn *subsys_repl)
                   1951: {
                   1952:        int len;
1.132     djm      1953:        Channel *c = NULL;
1.123     djm      1954:
                   1955:        debug2("%s: id %d", __func__, id);
                   1956:
1.132     djm      1957:        if ((c = channel_lookup(id)) == NULL)
                   1958:                fatal("client_session2_setup: channel %d: unknown channel", id);
                   1959:
1.123     djm      1960:        if (want_tty) {
                   1961:                struct winsize ws;
                   1962:                struct termios tio;
                   1963:
                   1964:                /* Store window size in the packet. */
                   1965:                if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
                   1966:                        memset(&ws, 0, sizeof(ws));
                   1967:
                   1968:                channel_request_start(id, "pty-req", 0);
                   1969:                packet_put_cstring(term != NULL ? term : "");
1.158     deraadt  1970:                packet_put_int((u_int)ws.ws_col);
                   1971:                packet_put_int((u_int)ws.ws_row);
                   1972:                packet_put_int((u_int)ws.ws_xpixel);
                   1973:                packet_put_int((u_int)ws.ws_ypixel);
1.123     djm      1974:                tio = get_saved_tio();
                   1975:                tty_make_modes(-1, tiop != NULL ? tiop : &tio);
                   1976:                packet_send();
                   1977:                /* XXX wait for reply */
1.132     djm      1978:                c->client_tty = 1;
1.123     djm      1979:        }
                   1980:
                   1981:        /* Transfer any environment variables from client to server */
1.126     djm      1982:        if (options.num_send_env != 0 && env != NULL) {
1.123     djm      1983:                int i, j, matched;
                   1984:                char *name, *val;
                   1985:
                   1986:                debug("Sending environment.");
1.126     djm      1987:                for (i = 0; env[i] != NULL; i++) {
1.123     djm      1988:                        /* Split */
1.126     djm      1989:                        name = xstrdup(env[i]);
1.123     djm      1990:                        if ((val = strchr(name, '=')) == NULL) {
1.144     stevesk  1991:                                xfree(name);
1.123     djm      1992:                                continue;
                   1993:                        }
                   1994:                        *val++ = '\0';
                   1995:
                   1996:                        matched = 0;
                   1997:                        for (j = 0; j < options.num_send_env; j++) {
                   1998:                                if (match_pattern(name, options.send_env[j])) {
                   1999:                                        matched = 1;
                   2000:                                        break;
                   2001:                                }
                   2002:                        }
                   2003:                        if (!matched) {
                   2004:                                debug3("Ignored env %s", name);
1.144     stevesk  2005:                                xfree(name);
1.123     djm      2006:                                continue;
                   2007:                        }
                   2008:
                   2009:                        debug("Sending env %s = %s", name, val);
                   2010:                        channel_request_start(id, "env", 0);
                   2011:                        packet_put_cstring(name);
                   2012:                        packet_put_cstring(val);
                   2013:                        packet_send();
1.144     stevesk  2014:                        xfree(name);
1.123     djm      2015:                }
                   2016:        }
                   2017:
                   2018:        len = buffer_len(cmd);
                   2019:        if (len > 0) {
                   2020:                if (len > 900)
                   2021:                        len = 900;
                   2022:                if (want_subsystem) {
                   2023:                        debug("Sending subsystem: %.*s", len, (u_char*)buffer_ptr(cmd));
                   2024:                        channel_request_start(id, "subsystem", subsys_repl != NULL);
                   2025:                        if (subsys_repl != NULL) {
                   2026:                                /* register callback for reply */
                   2027:                                /* XXX we assume that client_loop has already been called */
                   2028:                                dispatch_set(SSH2_MSG_CHANNEL_FAILURE, subsys_repl);
                   2029:                                dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, subsys_repl);
                   2030:                        }
                   2031:                } else {
                   2032:                        debug("Sending command: %.*s", len, (u_char*)buffer_ptr(cmd));
                   2033:                        channel_request_start(id, "exec", 0);
                   2034:                }
                   2035:                packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
                   2036:                packet_send();
                   2037:        } else {
                   2038:                channel_request_start(id, "shell", 0);
                   2039:                packet_send();
                   2040:        }
                   2041: }
                   2042:
1.77      itojun   2043: static void
1.49      itojun   2044: client_init_dispatch_20(void)
1.16      markus   2045: {
1.55      markus   2046:        dispatch_init(&dispatch_protocol_error);
1.100     markus   2047:
1.16      markus   2048:        dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
                   2049:        dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
                   2050:        dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
                   2051:        dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
1.22      markus   2052:        dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
1.16      markus   2053:        dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
                   2054:        dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1.48      markus   2055:        dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
1.16      markus   2056:        dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
1.95      markus   2057:        dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
1.55      markus   2058:
                   2059:        /* rekeying */
                   2060:        dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
1.100     markus   2061:
                   2062:        /* global request reply messages */
                   2063:        dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
                   2064:        dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
1.16      markus   2065: }
1.77      itojun   2066: static void
1.49      itojun   2067: client_init_dispatch_13(void)
1.15      markus   2068: {
                   2069:        dispatch_init(NULL);
                   2070:        dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
                   2071:        dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
                   2072:        dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
                   2073:        dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
                   2074:        dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
                   2075:        dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
                   2076:        dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
                   2077:        dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
                   2078:        dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
1.39      markus   2079:
                   2080:        dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
1.115     markus   2081:            &client_input_agent_open : &deny_input_open);
1.39      markus   2082:        dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
                   2083:            &x11_input_open : &deny_input_open);
1.15      markus   2084: }
1.77      itojun   2085: static void
1.49      itojun   2086: client_init_dispatch_15(void)
1.15      markus   2087: {
                   2088:        client_init_dispatch_13();
                   2089:        dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
                   2090:        dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
                   2091: }
1.79      stevesk  2092: static void
1.49      itojun   2093: client_init_dispatch(void)
1.15      markus   2094: {
1.16      markus   2095:        if (compat20)
                   2096:                client_init_dispatch_20();
                   2097:        else if (compat13)
1.15      markus   2098:                client_init_dispatch_13();
                   2099:        else
                   2100:                client_init_dispatch_15();
1.113     markus   2101: }
                   2102:
                   2103: /* client specific fatal cleanup */
                   2104: void
1.114     markus   2105: cleanup_exit(int i)
1.113     markus   2106: {
                   2107:        leave_raw_mode();
                   2108:        leave_non_blocking();
1.123     djm      2109:        if (options.control_path != NULL && control_fd != -1)
                   2110:                unlink(options.control_path);
1.114     markus   2111:        _exit(i);
1.1       deraadt  2112: }