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

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