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

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