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

1.1       deraadt     1: /*
1.20      markus      2:  *
1.12      deraadt     3:  * clientloop.c
1.20      markus      4:  *
1.12      deraadt     5:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
1.20      markus      6:  *
1.12      deraadt     7:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      8:  *                    All rights reserved
1.20      markus      9:  *
                     10:  *
1.12      deraadt    11:  * Created: Sat Sep 23 12:23:57 1995 ylo
1.20      markus     12:  *
1.12      deraadt    13:  * The main loop for the interactive session (client side).
1.20      markus     14:  *
1.16      markus     15:  * SSH2 support added by Markus Friedl.
1.12      deraadt    16:  */
1.1       deraadt    17:
                     18: #include "includes.h"
1.30    ! markus     19: RCSID("$OpenBSD: clientloop.c,v 1.29 2000/07/16 08:27:21 markus Exp $");
1.1       deraadt    20:
                     21: #include "xmalloc.h"
                     22: #include "ssh.h"
                     23: #include "packet.h"
                     24: #include "buffer.h"
1.8       markus     25: #include "readconf.h"
1.1       deraadt    26:
1.16      markus     27: #include "ssh2.h"
1.15      markus     28: #include "compat.h"
                     29: #include "channels.h"
                     30: #include "dispatch.h"
                     31:
1.30    ! markus     32: #include "buffer.h"
        !            33: #include "bufaux.h"
        !            34:
1.1       deraadt    35: /* Flag indicating that stdin should be redirected from /dev/null. */
                     36: extern int stdin_null_flag;
                     37:
1.13      markus     38: /*
                     39:  * Name of the host we are connecting to.  This is the name given on the
                     40:  * command line, or the HostName specified for the user-supplied name in a
                     41:  * configuration file.
                     42:  */
1.1       deraadt    43: extern char *host;
                     44:
1.13      markus     45: /*
                     46:  * Flag to indicate that we have received a window change signal which has
                     47:  * not yet been processed.  This will cause a message indicating the new
                     48:  * window size to be sent to the server a little later.  This is volatile
                     49:  * because this is updated in a signal handler.
                     50:  */
1.1       deraadt    51: static volatile int received_window_change_signal = 0;
                     52:
                     53: /* Terminal modes, as saved by enter_raw_mode. */
                     54: static struct termios saved_tio;
                     55:
1.13      markus     56: /*
                     57:  * Flag indicating whether we are in raw mode.  This is used by
                     58:  * enter_raw_mode and leave_raw_mode.
                     59:  */
1.1       deraadt    60: static int in_raw_mode = 0;
                     61:
                     62: /* Flag indicating whether the user\'s terminal is in non-blocking mode. */
                     63: static int in_non_blocking_mode = 0;
                     64:
                     65: /* Common data for the client loop code. */
1.11      markus     66: static int escape_pending;     /* Last character was the escape character */
                     67: static int last_was_cr;                /* Last character was a newline. */
                     68: static int exit_status;                /* Used to store the exit status of the command. */
                     69: static int stdin_eof;          /* EOF has been encountered on standard error. */
                     70: static Buffer stdin_buffer;    /* Buffer for stdin data. */
                     71: static Buffer stdout_buffer;   /* Buffer for stdout data. */
                     72: static Buffer stderr_buffer;   /* Buffer for stderr data. */
                     73: static unsigned int buffer_high;/* Soft max buffer size. */
                     74: static int max_fd;             /* Maximum file descriptor number in select(). */
                     75: static int connection_in;      /* Connection to server (input). */
                     76: static int connection_out;     /* Connection to server (output). */
1.1       deraadt    77: static unsigned long stdin_bytes, stdout_bytes, stderr_bytes;
1.11      markus     78: static int quit_pending;       /* Set to non-zero to quit the client loop. */
                     79: static int escape_char;                /* Escape character. */
1.1       deraadt    80:
1.16      markus     81:
                     82: void   client_init_dispatch(void);
                     83: int    session_ident = -1;
                     84:
1.13      markus     85: /* Returns the user\'s terminal to normal mode if it had been put in raw mode. */
1.1       deraadt    86:
1.20      markus     87: void
1.11      markus     88: leave_raw_mode()
1.1       deraadt    89: {
1.11      markus     90:        if (!in_raw_mode)
                     91:                return;
                     92:        in_raw_mode = 0;
                     93:        if (tcsetattr(fileno(stdin), TCSADRAIN, &saved_tio) < 0)
                     94:                perror("tcsetattr");
1.1       deraadt    95:
1.11      markus     96:        fatal_remove_cleanup((void (*) (void *)) leave_raw_mode, NULL);
1.1       deraadt    97: }
                     98:
                     99: /* Puts the user\'s terminal in raw mode. */
                    100:
1.20      markus    101: void
1.11      markus    102: enter_raw_mode()
1.1       deraadt   103: {
1.11      markus    104:        struct termios tio;
1.1       deraadt   105:
1.11      markus    106:        if (tcgetattr(fileno(stdin), &tio) < 0)
                    107:                perror("tcgetattr");
                    108:        saved_tio = tio;
                    109:        tio.c_iflag |= IGNPAR;
                    110:        tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
                    111:        tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
1.1       deraadt   112: #ifdef IEXTEN
1.11      markus    113:        tio.c_lflag &= ~IEXTEN;
                    114: #endif                         /* IEXTEN */
                    115:        tio.c_oflag &= ~OPOST;
                    116:        tio.c_cc[VMIN] = 1;
                    117:        tio.c_cc[VTIME] = 0;
                    118:        if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) < 0)
                    119:                perror("tcsetattr");
                    120:        in_raw_mode = 1;
1.1       deraadt   121:
1.11      markus    122:        fatal_add_cleanup((void (*) (void *)) leave_raw_mode, NULL);
                    123: }
1.1       deraadt   124:
                    125: /* Restores stdin to blocking mode. */
                    126:
1.20      markus    127: void
1.11      markus    128: leave_non_blocking()
1.1       deraadt   129: {
1.11      markus    130:        if (in_non_blocking_mode) {
                    131:                (void) fcntl(fileno(stdin), F_SETFL, 0);
                    132:                in_non_blocking_mode = 0;
                    133:                fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL);
                    134:        }
1.1       deraadt   135: }
                    136:
1.11      markus    137: /* Puts stdin terminal in non-blocking mode. */
                    138:
1.20      markus    139: void
1.11      markus    140: enter_non_blocking()
1.1       deraadt   141: {
1.11      markus    142:        in_non_blocking_mode = 1;
                    143:        (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
                    144:        fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL);
1.1       deraadt   145: }
                    146:
1.13      markus    147: /*
                    148:  * Signal handler for the window change signal (SIGWINCH).  This just sets a
                    149:  * flag indicating that the window has changed.
                    150:  */
1.1       deraadt   151:
1.20      markus    152: void
1.11      markus    153: window_change_handler(int sig)
1.1       deraadt   154: {
1.11      markus    155:        received_window_change_signal = 1;
                    156:        signal(SIGWINCH, window_change_handler);
1.1       deraadt   157: }
                    158:
1.13      markus    159: /*
                    160:  * Signal handler for signals that cause the program to terminate.  These
                    161:  * signals must be trapped to restore terminal modes.
                    162:  */
1.1       deraadt   163:
1.20      markus    164: void
1.11      markus    165: signal_handler(int sig)
1.1       deraadt   166: {
1.11      markus    167:        if (in_raw_mode)
                    168:                leave_raw_mode();
                    169:        if (in_non_blocking_mode)
                    170:                leave_non_blocking();
                    171:        channel_stop_listening();
                    172:        packet_close();
                    173:        fatal("Killed by signal %d.", sig);
1.1       deraadt   174: }
                    175:
1.13      markus    176: /*
                    177:  * Returns current time in seconds from Jan 1, 1970 with the maximum
                    178:  * available resolution.
                    179:  */
1.1       deraadt   180:
1.20      markus    181: double
1.11      markus    182: get_current_time()
1.1       deraadt   183: {
1.11      markus    184:        struct timeval tv;
                    185:        gettimeofday(&tv, NULL);
                    186:        return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
1.1       deraadt   187: }
                    188:
1.13      markus    189: /*
                    190:  * This is called when the interactive is entered.  This checks if there is
                    191:  * an EOF coming on stdin.  We must check this explicitly, as select() does
                    192:  * not appear to wake up when redirecting from /dev/null.
                    193:  */
1.1       deraadt   194:
1.20      markus    195: void
1.11      markus    196: client_check_initial_eof_on_stdin()
1.1       deraadt   197: {
1.11      markus    198:        int len;
                    199:        char buf[1];
1.1       deraadt   200:
1.13      markus    201:        /*
                    202:         * If standard input is to be "redirected from /dev/null", we simply
                    203:         * mark that we have seen an EOF and send an EOF message to the
                    204:         * server. Otherwise, we try to read a single character; it appears
                    205:         * that for some files, such /dev/null, select() never wakes up for
                    206:         * read for this descriptor, which means that we never get EOF.  This
                    207:         * way we will get the EOF if stdin comes from /dev/null or similar.
                    208:         */
1.11      markus    209:        if (stdin_null_flag) {
                    210:                /* Fake EOF on stdin. */
                    211:                debug("Sending eof.");
                    212:                stdin_eof = 1;
                    213:                packet_start(SSH_CMSG_EOF);
                    214:                packet_send();
                    215:        } else {
                    216:                enter_non_blocking();
                    217:
                    218:                /* Check for immediate EOF on stdin. */
                    219:                len = read(fileno(stdin), buf, 1);
                    220:                if (len == 0) {
1.13      markus    221:                        /* EOF.  Record that we have seen it and send EOF to server. */
1.11      markus    222:                        debug("Sending eof.");
                    223:                        stdin_eof = 1;
                    224:                        packet_start(SSH_CMSG_EOF);
                    225:                        packet_send();
                    226:                } else if (len > 0) {
1.13      markus    227:                        /*
                    228:                         * Got data.  We must store the data in the buffer,
                    229:                         * and also process it as an escape character if
                    230:                         * appropriate.
                    231:                         */
1.11      markus    232:                        if ((unsigned char) buf[0] == escape_char)
                    233:                                escape_pending = 1;
                    234:                        else {
                    235:                                buffer_append(&stdin_buffer, buf, 1);
                    236:                                stdin_bytes += 1;
                    237:                        }
                    238:                }
                    239:                leave_non_blocking();
                    240:        }
1.1       deraadt   241: }
                    242:
                    243:
1.13      markus    244: /*
                    245:  * Make packets from buffered stdin data, and buffer them for sending to the
                    246:  * connection.
                    247:  */
1.1       deraadt   248:
1.20      markus    249: void
1.11      markus    250: client_make_packets_from_stdin_data()
1.1       deraadt   251: {
1.11      markus    252:        unsigned int len;
1.1       deraadt   253:
1.11      markus    254:        /* Send buffered stdin data to the server. */
                    255:        while (buffer_len(&stdin_buffer) > 0 &&
                    256:               packet_not_very_much_data_to_write()) {
                    257:                len = buffer_len(&stdin_buffer);
                    258:                /* Keep the packets at reasonable size. */
                    259:                if (len > packet_get_maxsize())
                    260:                        len = packet_get_maxsize();
                    261:                packet_start(SSH_CMSG_STDIN_DATA);
                    262:                packet_put_string(buffer_ptr(&stdin_buffer), len);
                    263:                packet_send();
                    264:                buffer_consume(&stdin_buffer, len);
                    265:                /* If we have a pending EOF, send it now. */
                    266:                if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
                    267:                        packet_start(SSH_CMSG_EOF);
                    268:                        packet_send();
                    269:                }
1.1       deraadt   270:        }
                    271: }
                    272:
1.13      markus    273: /*
                    274:  * Checks if the client window has changed, and sends a packet about it to
                    275:  * the server if so.  The actual change is detected elsewhere (by a software
                    276:  * interrupt on Unix); this just checks the flag and sends a message if
                    277:  * appropriate.
                    278:  */
1.1       deraadt   279:
1.20      markus    280: void
1.11      markus    281: client_check_window_change()
1.1       deraadt   282: {
1.16      markus    283:        struct winsize ws;
                    284:
                    285:        if (! received_window_change_signal)
                    286:                return;
                    287:        /** XXX race */
                    288:        received_window_change_signal = 0;
                    289:
                    290:        if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
                    291:                return;
                    292:
                    293:        debug("client_check_window_change: changed");
                    294:
                    295:        if (compat20) {
                    296:                channel_request_start(session_ident, "window-change", 0);
                    297:                packet_put_int(ws.ws_col);
                    298:                packet_put_int(ws.ws_row);
                    299:                packet_put_int(ws.ws_xpixel);
                    300:                packet_put_int(ws.ws_ypixel);
                    301:                packet_send();
                    302:        } else {
                    303:                packet_start(SSH_CMSG_WINDOW_SIZE);
                    304:                packet_put_int(ws.ws_row);
                    305:                packet_put_int(ws.ws_col);
                    306:                packet_put_int(ws.ws_xpixel);
                    307:                packet_put_int(ws.ws_ypixel);
                    308:                packet_send();
1.1       deraadt   309:        }
                    310: }
                    311:
1.13      markus    312: /*
                    313:  * Waits until the client can do something (some data becomes available on
                    314:  * one of the file descriptors).
                    315:  */
1.1       deraadt   316:
1.20      markus    317: void
1.11      markus    318: client_wait_until_can_do_something(fd_set * readset, fd_set * writeset)
                    319: {
1.16      markus    320:        /*debug("client_wait_until_can_do_something"); */
                    321:
1.11      markus    322:        /* Initialize select masks. */
                    323:        FD_ZERO(readset);
1.17      markus    324:        FD_ZERO(writeset);
1.11      markus    325:
1.16      markus    326:        if (!compat20) {
1.17      markus    327:                /* Read from the connection, unless our buffers are full. */
1.16      markus    328:                if (buffer_len(&stdout_buffer) < buffer_high &&
                    329:                    buffer_len(&stderr_buffer) < buffer_high &&
                    330:                    channel_not_very_much_buffered_data())
                    331:                        FD_SET(connection_in, readset);
1.17      markus    332:                /*
                    333:                 * Read from stdin, unless we have seen EOF or have very much
                    334:                 * buffered data to send to the server.
                    335:                 */
                    336:                if (!stdin_eof && packet_not_very_much_data_to_write())
                    337:                        FD_SET(fileno(stdin), readset);
                    338:
                    339:                /* Select stdout/stderr if have data in buffer. */
                    340:                if (buffer_len(&stdout_buffer) > 0)
                    341:                        FD_SET(fileno(stdout), writeset);
                    342:                if (buffer_len(&stderr_buffer) > 0)
                    343:                        FD_SET(fileno(stderr), writeset);
1.16      markus    344:        } else {
1.11      markus    345:                FD_SET(connection_in, readset);
1.16      markus    346:        }
1.11      markus    347:
                    348:        /* Add any selections by the channel mechanism. */
                    349:        channel_prepare_select(readset, writeset);
                    350:
                    351:        /* Select server connection if have data to write to the server. */
                    352:        if (packet_have_data_to_write())
                    353:                FD_SET(connection_out, writeset);
                    354:
1.16      markus    355: /* move UP XXX */
1.11      markus    356:        /* Update maximum file descriptor number, if appropriate. */
                    357:        if (channel_max_fd() > max_fd)
                    358:                max_fd = channel_max_fd();
                    359:
1.13      markus    360:        /*
                    361:         * Wait for something to happen.  This will suspend the process until
                    362:         * some selected descriptor can be read, written, or has some other
                    363:         * event pending. Note: if you want to implement SSH_MSG_IGNORE
                    364:         * messages to fool traffic analysis, this might be the place to do
                    365:         * it: just have a random timeout for the select, and send a random
                    366:         * SSH_MSG_IGNORE packet when the timeout expires.
                    367:         */
1.11      markus    368:
                    369:        if (select(max_fd + 1, readset, writeset, NULL, NULL) < 0) {
                    370:                char buf[100];
                    371:                /* Some systems fail to clear these automatically. */
                    372:                FD_ZERO(readset);
                    373:                FD_ZERO(writeset);
                    374:                if (errno == EINTR)
                    375:                        return;
                    376:                /* Note: we might still have data in the buffers. */
                    377:                snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
                    378:                buffer_append(&stderr_buffer, buf, strlen(buf));
                    379:                stderr_bytes += strlen(buf);
                    380:                quit_pending = 1;
                    381:        }
                    382: }
                    383:
1.20      markus    384: void
1.11      markus    385: client_suspend_self()
1.1       deraadt   386: {
1.11      markus    387:        struct winsize oldws, newws;
                    388:
                    389:        /* Flush stdout and stderr buffers. */
                    390:        if (buffer_len(&stdout_buffer) > 0)
1.14      deraadt   391:                atomicio(write, fileno(stdout), buffer_ptr(&stdout_buffer),
                    392:                    buffer_len(&stdout_buffer));
1.11      markus    393:        if (buffer_len(&stderr_buffer) > 0)
1.14      deraadt   394:                atomicio(write, fileno(stderr), buffer_ptr(&stderr_buffer),
                    395:                    buffer_len(&stderr_buffer));
1.11      markus    396:
                    397:        leave_raw_mode();
                    398:
1.13      markus    399:        /*
                    400:         * Free (and clear) the buffer to reduce the amount of data that gets
                    401:         * written to swap.
                    402:         */
1.11      markus    403:        buffer_free(&stdin_buffer);
                    404:        buffer_free(&stdout_buffer);
                    405:        buffer_free(&stderr_buffer);
                    406:
                    407:        /* Save old window size. */
                    408:        ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
                    409:
                    410:        /* Send the suspend signal to the program itself. */
                    411:        kill(getpid(), SIGTSTP);
                    412:
                    413:        /* Check if the window size has changed. */
                    414:        if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
                    415:            (oldws.ws_row != newws.ws_row ||
                    416:             oldws.ws_col != newws.ws_col ||
                    417:             oldws.ws_xpixel != newws.ws_xpixel ||
                    418:             oldws.ws_ypixel != newws.ws_ypixel))
                    419:                received_window_change_signal = 1;
                    420:
                    421:        /* OK, we have been continued by the user. Reinitialize buffers. */
                    422:        buffer_init(&stdin_buffer);
                    423:        buffer_init(&stdout_buffer);
                    424:        buffer_init(&stderr_buffer);
                    425:
                    426:        enter_raw_mode();
                    427: }
                    428:
1.20      markus    429: void
1.17      markus    430: client_process_net_input(fd_set * readset)
1.11      markus    431: {
1.17      markus    432:        int len;
                    433:        char buf[8192];
1.11      markus    434:
1.13      markus    435:        /*
                    436:         * Read input from the server, and add any such data to the buffer of
                    437:         * the packet subsystem.
                    438:         */
1.11      markus    439:        if (FD_ISSET(connection_in, readset)) {
                    440:                /* Read as much as possible. */
                    441:                len = read(connection_in, buf, sizeof(buf));
1.16      markus    442: /*debug("read connection_in len %d", len); XXX */
1.11      markus    443:                if (len == 0) {
                    444:                        /* Received EOF.  The remote host has closed the connection. */
                    445:                        snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
                    446:                                 host);
1.1       deraadt   447:                        buffer_append(&stderr_buffer, buf, strlen(buf));
                    448:                        stderr_bytes += strlen(buf);
                    449:                        quit_pending = 1;
                    450:                        return;
1.11      markus    451:                }
1.13      markus    452:                /*
                    453:                 * There is a kernel bug on Solaris that causes select to
                    454:                 * sometimes wake up even though there is no data available.
                    455:                 */
1.11      markus    456:                if (len < 0 && errno == EAGAIN)
                    457:                        len = 0;
                    458:
                    459:                if (len < 0) {
                    460:                        /* An error has encountered.  Perhaps there is a network problem. */
                    461:                        snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
                    462:                                 host, strerror(errno));
                    463:                        buffer_append(&stderr_buffer, buf, strlen(buf));
                    464:                        stderr_bytes += strlen(buf);
                    465:                        quit_pending = 1;
                    466:                        return;
                    467:                }
                    468:                packet_process_incoming(buf, len);
                    469:        }
1.17      markus    470: }
1.16      markus    471:
1.20      markus    472: void
1.17      markus    473: client_process_input(fd_set * readset)
                    474: {
1.21      deraadt   475:        int len;
                    476:        pid_t pid;
1.17      markus    477:        char buf[8192], *s;
1.16      markus    478:
1.11      markus    479:        /* Read input from stdin. */
                    480:        if (FD_ISSET(fileno(stdin), readset)) {
                    481:                /* Read as much as possible. */
                    482:                len = read(fileno(stdin), buf, sizeof(buf));
                    483:                if (len <= 0) {
1.13      markus    484:                        /*
                    485:                         * Received EOF or error.  They are treated
                    486:                         * similarly, except that an error message is printed
                    487:                         * if it was an error condition.
                    488:                         */
1.11      markus    489:                        if (len < 0) {
                    490:                                snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
                    491:                                buffer_append(&stderr_buffer, buf, strlen(buf));
                    492:                                stderr_bytes += strlen(buf);
                    493:                        }
                    494:                        /* Mark that we have seen EOF. */
                    495:                        stdin_eof = 1;
1.13      markus    496:                        /*
                    497:                         * Send an EOF message to the server unless there is
                    498:                         * data in the buffer.  If there is data in the
                    499:                         * buffer, no message will be sent now.  Code
                    500:                         * elsewhere will send the EOF when the buffer
                    501:                         * becomes empty if stdin_eof is set.
                    502:                         */
1.11      markus    503:                        if (buffer_len(&stdin_buffer) == 0) {
1.1       deraadt   504:                                packet_start(SSH_CMSG_EOF);
                    505:                                packet_send();
1.11      markus    506:                        }
                    507:                } else if (escape_char == -1) {
1.13      markus    508:                        /*
                    509:                         * Normal successful read, and no escape character.
                    510:                         * Just append the data to buffer.
                    511:                         */
1.11      markus    512:                        buffer_append(&stdin_buffer, buf, len);
                    513:                        stdin_bytes += len;
                    514:                } else {
1.13      markus    515:                        /*
                    516:                         * Normal, successful read.  But we have an escape character
                    517:                         * and have to process the characters one by one.
                    518:                         */
1.11      markus    519:                        unsigned int i;
                    520:                        for (i = 0; i < len; i++) {
                    521:                                unsigned char ch;
                    522:                                /* Get one character at a time. */
                    523:                                ch = buf[i];
                    524:
                    525:                                if (escape_pending) {
                    526:                                        /* We have previously seen an escape character. */
                    527:                                        /* Clear the flag now. */
                    528:                                        escape_pending = 0;
                    529:                                        /* Process the escaped character. */
                    530:                                        switch (ch) {
                    531:                                        case '.':
                    532:                                                /* Terminate the connection. */
                    533:                                                snprintf(buf, sizeof buf, "%c.\r\n", escape_char);
                    534:                                                buffer_append(&stderr_buffer, buf, strlen(buf));
                    535:                                                stderr_bytes += strlen(buf);
                    536:                                                quit_pending = 1;
                    537:                                                return;
                    538:
                    539:                                        case 'Z' - 64:
                    540:                                                /* Suspend the program. */
                    541:                                                /* Print a message to that effect to the user. */
                    542:                                                snprintf(buf, sizeof buf, "%c^Z\r\n", escape_char);
                    543:                                                buffer_append(&stderr_buffer, buf, strlen(buf));
                    544:                                                stderr_bytes += strlen(buf);
                    545:
                    546:                                                /* Restore terminal modes and suspend. */
                    547:                                                client_suspend_self();
                    548:
                    549:                                                /* We have been continued. */
                    550:                                                continue;
                    551:
                    552:                                        case '&':
1.13      markus    553:                                                /*
                    554:                                                 * Detach the program (continue to serve connections,
                    555:                                                 * but put in background and no more new connections).
                    556:                                                 */
1.11      markus    557:                                                if (!stdin_eof) {
1.13      markus    558:                                                        /*
                    559:                                                         * Sending SSH_CMSG_EOF alone does not always appear
                    560:                                                         * to be enough.  So we try to send an EOF character
                    561:                                                         * first.
                    562:                                                         */
1.11      markus    563:                                                        packet_start(SSH_CMSG_STDIN_DATA);
                    564:                                                        packet_put_string("\004", 1);
                    565:                                                        packet_send();
                    566:                                                        /* Close stdin. */
                    567:                                                        stdin_eof = 1;
                    568:                                                        if (buffer_len(&stdin_buffer) == 0) {
                    569:                                                                packet_start(SSH_CMSG_EOF);
                    570:                                                                packet_send();
                    571:                                                        }
                    572:                                                }
                    573:                                                /* Restore tty modes. */
                    574:                                                leave_raw_mode();
                    575:
                    576:                                                /* Stop listening for new connections. */
                    577:                                                channel_stop_listening();
                    578:
                    579:                                                printf("%c& [backgrounded]\n", escape_char);
                    580:
                    581:                                                /* Fork into background. */
                    582:                                                pid = fork();
                    583:                                                if (pid < 0) {
                    584:                                                        error("fork: %.100s", strerror(errno));
                    585:                                                        continue;
                    586:                                                }
                    587:                                                if (pid != 0) { /* This is the parent. */
                    588:                                                        /* The parent just exits. */
                    589:                                                        exit(0);
                    590:                                                }
                    591:                                                /* The child continues serving connections. */
                    592:                                                continue;
                    593:
                    594:                                        case '?':
                    595:                                                snprintf(buf, sizeof buf,
                    596: "%c?\r\n\
1.1       deraadt   597: Supported escape sequences:\r\n\
                    598: ~.  - terminate connection\r\n\
                    599: ~^Z - suspend ssh\r\n\
                    600: ~#  - list forwarded connections\r\n\
                    601: ~&  - background ssh (when waiting for connections to terminate)\r\n\
                    602: ~?  - this message\r\n\
                    603: ~~  - send the escape character by typing it twice\r\n\
                    604: (Note that escapes are only recognized immediately after newline.)\r\n",
1.11      markus    605:                                                         escape_char);
                    606:                                                buffer_append(&stderr_buffer, buf, strlen(buf));
                    607:                                                continue;
                    608:
                    609:                                        case '#':
                    610:                                                snprintf(buf, sizeof buf, "%c#\r\n", escape_char);
                    611:                                                buffer_append(&stderr_buffer, buf, strlen(buf));
                    612:                                                s = channel_open_message();
                    613:                                                buffer_append(&stderr_buffer, s, strlen(s));
                    614:                                                xfree(s);
                    615:                                                continue;
                    616:
                    617:                                        default:
                    618:                                                if (ch != escape_char) {
1.13      markus    619:                                                        /*
                    620:                                                         * Escape character followed by non-special character.
                    621:                                                         * Append both to the input buffer.
                    622:                                                         */
1.11      markus    623:                                                        buf[0] = escape_char;
                    624:                                                        buf[1] = ch;
                    625:                                                        buffer_append(&stdin_buffer, buf, 2);
                    626:                                                        stdin_bytes += 2;
                    627:                                                        continue;
                    628:                                                }
1.13      markus    629:                                                /*
                    630:                                                 * Note that escape character typed twice
                    631:                                                 * falls through here; the latter gets processed
                    632:                                                 * as a normal character below.
                    633:                                                 */
1.11      markus    634:                                                break;
                    635:                                        }
                    636:                                } else {
1.13      markus    637:                                        /*
                    638:                                         * The previous character was not an escape char. Check if this
                    639:                                         * is an escape.
                    640:                                         */
1.11      markus    641:                                        if (last_was_cr && ch == escape_char) {
                    642:                                                /* It is. Set the flag and continue to next character. */
                    643:                                                escape_pending = 1;
                    644:                                                continue;
                    645:                                        }
                    646:                                }
                    647:
1.13      markus    648:                                /*
                    649:                                 * Normal character.  Record whether it was a newline,
                    650:                                 * and append it to the buffer.
                    651:                                 */
1.11      markus    652:                                last_was_cr = (ch == '\r' || ch == '\n');
                    653:                                buf[0] = ch;
                    654:                                buffer_append(&stdin_buffer, buf, 1);
                    655:                                stdin_bytes += 1;
                    656:                                continue;
                    657:                        }
                    658:                }
                    659:        }
                    660: }
                    661:
1.20      markus    662: void
1.11      markus    663: client_process_output(fd_set * writeset)
                    664: {
                    665:        int len;
                    666:        char buf[100];
1.1       deraadt   667:
1.11      markus    668:        /* Write buffered output to stdout. */
                    669:        if (FD_ISSET(fileno(stdout), writeset)) {
                    670:                /* Write as much data as possible. */
                    671:                len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
1.14      deraadt   672:                    buffer_len(&stdout_buffer));
1.11      markus    673:                if (len <= 0) {
                    674:                        if (errno == EAGAIN)
                    675:                                len = 0;
                    676:                        else {
1.13      markus    677:                                /*
                    678:                                 * An error or EOF was encountered.  Put an
                    679:                                 * error message to stderr buffer.
                    680:                                 */
1.11      markus    681:                                snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
                    682:                                buffer_append(&stderr_buffer, buf, strlen(buf));
                    683:                                stderr_bytes += strlen(buf);
                    684:                                quit_pending = 1;
                    685:                                return;
                    686:                        }
                    687:                }
                    688:                /* Consume printed data from the buffer. */
                    689:                buffer_consume(&stdout_buffer, len);
                    690:        }
                    691:        /* Write buffered output to stderr. */
                    692:        if (FD_ISSET(fileno(stderr), writeset)) {
                    693:                /* Write as much data as possible. */
                    694:                len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
1.14      deraadt   695:                    buffer_len(&stderr_buffer));
1.11      markus    696:                if (len <= 0) {
                    697:                        if (errno == EAGAIN)
                    698:                                len = 0;
                    699:                        else {
1.13      markus    700:                                /* EOF or error, but can't even print error message. */
1.11      markus    701:                                quit_pending = 1;
                    702:                                return;
                    703:                        }
                    704:                }
                    705:                /* Consume printed characters from the buffer. */
                    706:                buffer_consume(&stderr_buffer, len);
                    707:        }
1.1       deraadt   708: }
                    709:
1.13      markus    710: /*
1.15      markus    711:  * Get packets from the connection input buffer, and process them as long as
                    712:  * there are packets available.
                    713:  *
                    714:  * Any unknown packets received during the actual
                    715:  * session cause the session to terminate.  This is
                    716:  * intended to make debugging easier since no
                    717:  * confirmations are sent.  Any compatible protocol
                    718:  * extensions must be negotiated during the
                    719:  * preparatory phase.
                    720:  */
                    721:
1.20      markus    722: void
1.15      markus    723: client_process_buffered_input_packets()
                    724: {
                    725:        dispatch_run(DISPATCH_NONBLOCK, &quit_pending);
                    726: }
                    727:
1.30    ! markus    728: /* scan stdin buf[] for '~' before sending data to the peer */
        !           729:
        !           730: int
        !           731: simple_escape_filter(Buffer *bin, char *buf, int len)
        !           732: {
        !           733:        unsigned int i;
        !           734:        unsigned char ch;
        !           735:        for (i = 0; i < len; i++) {
        !           736:                ch = buf[i];
        !           737:                if (escape_pending) {
        !           738:                        escape_pending = 0;
        !           739:                        if (ch == '.') {
        !           740:                                quit_pending = 1;
        !           741:                                return -1;
        !           742:                        }
        !           743:                        if (ch != escape_char)
        !           744:                                buffer_put_char(bin, escape_char);
        !           745:                } else {
        !           746:                        if (last_was_cr && ch == escape_char) {
        !           747:                                escape_pending = 1;
        !           748:                                continue;
        !           749:                        }
        !           750:                }
        !           751:                last_was_cr = (ch == '\n' || ch == '\r');
        !           752:                buffer_put_char(bin, ch);
        !           753:        }
        !           754:        return 0;
        !           755: }
        !           756:
1.15      markus    757: /*
1.13      markus    758:  * Implements the interactive session with the server.  This is called after
                    759:  * the user has been authenticated, and a command has been started on the
                    760:  * remote host.  If escape_char != -1, it is the character used as an escape
                    761:  * character for terminating or suspending the session.
                    762:  */
1.1       deraadt   763:
1.20      markus    764: int
1.30    ! markus    765: client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
1.1       deraadt   766: {
1.11      markus    767:        extern Options options;
                    768:        double start_time, total_time;
                    769:        int len;
                    770:        char buf[100];
                    771:
                    772:        debug("Entering interactive session.");
                    773:
                    774:        start_time = get_current_time();
                    775:
                    776:        /* Initialize variables. */
                    777:        escape_pending = 0;
                    778:        last_was_cr = 1;
                    779:        exit_status = -1;
                    780:        stdin_eof = 0;
                    781:        buffer_high = 64 * 1024;
                    782:        connection_in = packet_get_connection_in();
                    783:        connection_out = packet_get_connection_out();
                    784:        max_fd = connection_in;
                    785:        if (connection_out > max_fd)
                    786:                max_fd = connection_out;
                    787:        stdin_bytes = 0;
                    788:        stdout_bytes = 0;
                    789:        stderr_bytes = 0;
                    790:        quit_pending = 0;
                    791:        escape_char = escape_char_arg;
                    792:
                    793:        /* Initialize buffers. */
                    794:        buffer_init(&stdin_buffer);
                    795:        buffer_init(&stdout_buffer);
                    796:        buffer_init(&stderr_buffer);
                    797:
1.15      markus    798:        client_init_dispatch();
                    799:
1.11      markus    800:        /* Set signal handlers to restore non-blocking mode.  */
                    801:        signal(SIGINT, signal_handler);
                    802:        signal(SIGQUIT, signal_handler);
                    803:        signal(SIGTERM, signal_handler);
                    804:        signal(SIGPIPE, SIG_IGN);
                    805:        if (have_pty)
                    806:                signal(SIGWINCH, window_change_handler);
                    807:
                    808:        if (have_pty)
                    809:                enter_raw_mode();
                    810:
1.28      provos    811:        /* Check if we should immediately send eof on stdin. */
1.17      markus    812:        if (!compat20)
1.16      markus    813:                client_check_initial_eof_on_stdin();
1.11      markus    814:
1.30    ! markus    815:        if (compat20 && escape_char != -1)
        !           816:                channel_register_filter(ssh2_chan_id, simple_escape_filter);
        !           817:
1.11      markus    818:        /* Main loop of the client for the interactive session mode. */
                    819:        while (!quit_pending) {
                    820:                fd_set readset, writeset;
                    821:
1.13      markus    822:                /* Process buffered packets sent by the server. */
1.11      markus    823:                client_process_buffered_input_packets();
                    824:
1.16      markus    825:                if (compat20 && !channel_still_open()) {
                    826:                        debug("!channel_still_open.");
                    827:                        break;
                    828:                }
                    829:
1.13      markus    830:                /*
                    831:                 * Make packets of buffered stdin data, and buffer them for
                    832:                 * sending to the server.
                    833:                 */
1.17      markus    834:                if (!compat20)
1.16      markus    835:                        client_make_packets_from_stdin_data();
1.11      markus    836:
1.13      markus    837:                /*
                    838:                 * Make packets from buffered channel data, and buffer them
                    839:                 * for sending to the server.
                    840:                 */
1.11      markus    841:                if (packet_not_very_much_data_to_write())
                    842:                        channel_output_poll();
                    843:
1.13      markus    844:                /*
                    845:                 * Check if the window size has changed, and buffer a message
                    846:                 * about it to the server if so.
                    847:                 */
1.11      markus    848:                client_check_window_change();
                    849:
                    850:                if (quit_pending)
                    851:                        break;
                    852:
1.13      markus    853:                /*
                    854:                 * Wait until we have something to do (something becomes
                    855:                 * available on one of the descriptors).
                    856:                 */
1.11      markus    857:                client_wait_until_can_do_something(&readset, &writeset);
                    858:
                    859:                if (quit_pending)
                    860:                        break;
                    861:
                    862:                /* Do channel operations. */
                    863:                channel_after_select(&readset, &writeset);
                    864:
1.17      markus    865:                /* Buffer input from the connection.  */
                    866:                client_process_net_input(&readset);
                    867:
                    868:                if (quit_pending)
                    869:                        break;
1.11      markus    870:
1.17      markus    871:                if (!compat20) {
                    872:                        /* Buffer data from stdin */
                    873:                        client_process_input(&readset);
                    874:                        /*
                    875:                         * Process output to stdout and stderr.  Output to
                    876:                         * the connection is processed elsewhere (above).
                    877:                         */
1.16      markus    878:                        client_process_output(&writeset);
1.17      markus    879:                }
1.11      markus    880:
1.13      markus    881:                /* Send as much buffered packet data as possible to the sender. */
1.11      markus    882:                if (FD_ISSET(connection_out, &writeset))
                    883:                        packet_write_poll();
                    884:        }
                    885:
                    886:        /* Terminate the session. */
                    887:
                    888:        /* Stop watching for window change. */
                    889:        if (have_pty)
                    890:                signal(SIGWINCH, SIG_DFL);
                    891:
                    892:        /* Stop listening for connections. */
                    893:        channel_stop_listening();
                    894:
1.13      markus    895:        /*
                    896:         * In interactive mode (with pseudo tty) display a message indicating
                    897:         * that the connection has been closed.
                    898:         */
1.11      markus    899:        if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
                    900:                snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
                    901:                buffer_append(&stderr_buffer, buf, strlen(buf));
                    902:                stderr_bytes += strlen(buf);
                    903:        }
                    904:        /* Output any buffered data for stdout. */
                    905:        while (buffer_len(&stdout_buffer) > 0) {
                    906:                len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
1.14      deraadt   907:                    buffer_len(&stdout_buffer));
1.11      markus    908:                if (len <= 0) {
                    909:                        error("Write failed flushing stdout buffer.");
                    910:                        break;
                    911:                }
                    912:                buffer_consume(&stdout_buffer, len);
                    913:        }
                    914:
                    915:        /* Output any buffered data for stderr. */
                    916:        while (buffer_len(&stderr_buffer) > 0) {
                    917:                len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
1.14      deraadt   918:                    buffer_len(&stderr_buffer));
1.11      markus    919:                if (len <= 0) {
                    920:                        error("Write failed flushing stderr buffer.");
                    921:                        break;
                    922:                }
                    923:                buffer_consume(&stderr_buffer, len);
                    924:        }
                    925:
                    926:        if (have_pty)
                    927:                leave_raw_mode();
                    928:
                    929:        /* Clear and free any buffers. */
                    930:        memset(buf, 0, sizeof(buf));
                    931:        buffer_free(&stdin_buffer);
                    932:        buffer_free(&stdout_buffer);
                    933:        buffer_free(&stderr_buffer);
                    934:
                    935:        /* Report bytes transferred, and transfer rates. */
                    936:        total_time = get_current_time() - start_time;
                    937:        debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
                    938:              stdin_bytes, stdout_bytes, stderr_bytes, total_time);
                    939:        if (total_time > 0)
                    940:                debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
                    941:                      stdin_bytes / total_time, stdout_bytes / total_time,
                    942:                      stderr_bytes / total_time);
                    943:
                    944:        /* Return the exit status of the program. */
                    945:        debug("Exit status %d", exit_status);
                    946:        return exit_status;
1.15      markus    947: }
                    948:
                    949: /*********/
                    950:
                    951: void
                    952: client_input_stdout_data(int type, int plen)
                    953: {
                    954:        unsigned int data_len;
                    955:        char *data = packet_get_string(&data_len);
                    956:        packet_integrity_check(plen, 4 + data_len, type);
                    957:        buffer_append(&stdout_buffer, data, data_len);
                    958:        stdout_bytes += data_len;
                    959:        memset(data, 0, data_len);
                    960:        xfree(data);
                    961: }
                    962: void
                    963: client_input_stderr_data(int type, int plen)
                    964: {
                    965:        unsigned int data_len;
                    966:        char *data = packet_get_string(&data_len);
                    967:        packet_integrity_check(plen, 4 + data_len, type);
                    968:        buffer_append(&stderr_buffer, data, data_len);
                    969:        stdout_bytes += data_len;
                    970:        memset(data, 0, data_len);
                    971:        xfree(data);
                    972: }
                    973: void
                    974: client_input_exit_status(int type, int plen)
                    975: {
                    976:        packet_integrity_check(plen, 4, type);
                    977:        exit_status = packet_get_int();
                    978:        /* Acknowledge the exit. */
                    979:        packet_start(SSH_CMSG_EXIT_CONFIRMATION);
                    980:        packet_send();
                    981:        /*
                    982:         * Must wait for packet to be sent since we are
                    983:         * exiting the loop.
                    984:         */
                    985:        packet_write_wait();
                    986:        /* Flag that we want to exit. */
                    987:        quit_pending = 1;
                    988: }
                    989:
1.22      markus    990: /* XXXX move to generic input handler */
                    991: void
                    992: client_input_channel_open(int type, int plen)
                    993: {
                    994:        Channel *c = NULL;
                    995:        char *ctype;
                    996:        int id;
                    997:        unsigned int len;
                    998:        int rchan;
                    999:        int rmaxpack;
                   1000:        int rwindow;
                   1001:
                   1002:        ctype = packet_get_string(&len);
                   1003:        rchan = packet_get_int();
                   1004:        rwindow = packet_get_int();
                   1005:        rmaxpack = packet_get_int();
                   1006:
1.24      markus   1007:        debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
1.22      markus   1008:            ctype, rchan, rwindow, rmaxpack);
                   1009:
                   1010:        if (strcmp(ctype, "x11") == 0) {
                   1011:                int sock;
                   1012:                char *originator;
                   1013:                int originator_port;
                   1014:                originator = packet_get_string(NULL);
1.26      markus   1015:                if (datafellows & SSH_BUG_X11FWD) {
1.25      markus   1016:                        debug("buggy server: x11 request w/o originator_port");
                   1017:                        originator_port = 0;
1.26      markus   1018:                } else {
                   1019:                        originator_port = packet_get_int();
1.25      markus   1020:                }
1.22      markus   1021:                packet_done();
                   1022:                /* XXX check permission */
                   1023:                xfree(originator);
                   1024:                /* XXX move to channels.c */
                   1025:                sock = x11_connect_display();
                   1026:                if (sock >= 0) {
1.30    ! markus   1027: /*XXX MAXPACK */
1.22      markus   1028:                        id = channel_new("x11", SSH_CHANNEL_X11_OPEN,
                   1029:                            sock, sock, -1, 4*1024, 32*1024, 0,
                   1030:                            xstrdup("x11"));
                   1031:                        c = channel_lookup(id);
                   1032:                }
                   1033:        }
                   1034: /* XXX duplicate : */
                   1035:        if (c != NULL) {
                   1036:                debug("confirm %s", ctype);
                   1037:                c->remote_id = rchan;
                   1038:                c->remote_window = rwindow;
                   1039:                c->remote_maxpacket = rmaxpack;
                   1040:
                   1041:                packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
                   1042:                packet_put_int(c->remote_id);
                   1043:                packet_put_int(c->self);
                   1044:                packet_put_int(c->local_window);
                   1045:                packet_put_int(c->local_maxpacket);
                   1046:                packet_send();
                   1047:        } else {
                   1048:                debug("failure %s", ctype);
                   1049:                packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
                   1050:                packet_put_int(rchan);
                   1051:                packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
                   1052:                packet_put_cstring("bla bla");
                   1053:                packet_put_cstring("");
                   1054:                packet_send();
                   1055:        }
                   1056:        xfree(ctype);
                   1057: }
                   1058:
1.20      markus   1059: void
1.16      markus   1060: client_init_dispatch_20()
                   1061: {
                   1062:        dispatch_init(&dispatch_protocol_error);
                   1063:        dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
                   1064:        dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
                   1065:        dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
                   1066:        dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
1.22      markus   1067:        dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
1.16      markus   1068:        dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
                   1069:        dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
                   1070:        dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
                   1071:        dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
                   1072: }
1.20      markus   1073: void
1.15      markus   1074: client_init_dispatch_13()
                   1075: {
                   1076:        dispatch_init(NULL);
                   1077:        dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
                   1078:        dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
                   1079:        dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
                   1080:        dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
                   1081:        dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
                   1082:        dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
                   1083:        dispatch_set(SSH_SMSG_AGENT_OPEN, &auth_input_open_request);
                   1084:        dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
                   1085:        dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
                   1086:        dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
                   1087:        dispatch_set(SSH_SMSG_X11_OPEN, &x11_input_open);
                   1088: }
1.20      markus   1089: void
1.15      markus   1090: client_init_dispatch_15()
                   1091: {
                   1092:        client_init_dispatch_13();
                   1093:        dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
                   1094:        dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
                   1095: }
1.20      markus   1096: void
1.15      markus   1097: client_init_dispatch()
                   1098: {
1.16      markus   1099:        if (compat20)
                   1100:                client_init_dispatch_20();
                   1101:        else if (compat13)
1.15      markus   1102:                client_init_dispatch_13();
                   1103:        else
                   1104:                client_init_dispatch_15();
1.16      markus   1105: }
                   1106:
                   1107: void
                   1108: client_input_channel_req(int id, void *arg)
                   1109: {
                   1110:        Channel *c = NULL;
                   1111:        unsigned int len;
                   1112:        int success = 0;
                   1113:        int reply;
                   1114:        char *rtype;
                   1115:
                   1116:        rtype = packet_get_string(&len);
                   1117:        reply = packet_get_char();
                   1118:
1.23      markus   1119:        debug("client_input_channel_req: rtype %s reply %d", rtype, reply);
1.16      markus   1120:
                   1121:        c = channel_lookup(id);
                   1122:        if (c == NULL)
                   1123:                fatal("session_input_channel_req: channel %d: bad channel", id);
                   1124:
                   1125:        if (session_ident == -1) {
                   1126:                error("client_input_channel_req: no channel %d", id);
                   1127:        } else if (id != session_ident) {
                   1128:                error("client_input_channel_req: bad channel %d != %d",
                   1129:                    id, session_ident);
                   1130:        } else if (strcmp(rtype, "exit-status") == 0) {
                   1131:                success = 1;
                   1132:                exit_status = packet_get_int();
1.19      markus   1133:                packet_done();
1.16      markus   1134:        }
                   1135:        if (reply) {
                   1136:                packet_start(success ?
                   1137:                    SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
                   1138:                packet_put_int(c->remote_id);
                   1139:                packet_send();
                   1140:        }
                   1141:        xfree(rtype);
                   1142: }
                   1143:
                   1144: void
                   1145: client_set_session_ident(int id)
                   1146: {
                   1147:        debug("client_set_session_ident: id %d", id);
                   1148:        session_ident = id;
                   1149:        channel_register_callback(id, SSH2_MSG_CHANNEL_REQUEST,
                   1150:            client_input_channel_req, (void *)0);
1.1       deraadt  1151: }