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

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