Annotation of src/usr.bin/ssh/clientloop.c, Revision 1.83
1.1 deraadt 1: /*
1.12 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
1.33 deraadt 5: * The main loop for the interactive session (client side).
1.20 markus 6: *
1.33 deraadt 7: * As far as I am concerned, the code I have written for this software
8: * can be used freely for any purpose. Any derived versions of this
9: * software must be clearly marked as such, and if the derived work is
10: * incompatible with the protocol description in the RFC file, it must be
11: * called by a name other than "ssh" or "Secure Shell".
12: *
13: *
14: * Copyright (c) 1999 Theo de Raadt. All rights reserved.
15: *
16: * Redistribution and use in source and binary forms, with or without
17: * modification, are permitted provided that the following conditions
18: * are met:
19: * 1. Redistributions of source code must retain the above copyright
20: * notice, this list of conditions and the following disclaimer.
21: * 2. Redistributions in binary form must reproduce the above copyright
22: * notice, this list of conditions and the following disclaimer in the
23: * documentation and/or other materials provided with the distribution.
24: *
25: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.20 markus 35: *
36: *
1.33 deraadt 37: * SSH2 support added by Markus Friedl.
1.78 markus 38: * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
1.20 markus 39: *
1.33 deraadt 40: * Redistribution and use in source and binary forms, with or without
41: * modification, are permitted provided that the following conditions
42: * are met:
43: * 1. Redistributions of source code must retain the above copyright
44: * notice, this list of conditions and the following disclaimer.
45: * 2. Redistributions in binary form must reproduce the above copyright
46: * notice, this list of conditions and the following disclaimer in the
47: * documentation and/or other materials provided with the distribution.
48: *
49: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.12 deraadt 59: */
1.1 deraadt 60:
61: #include "includes.h"
1.83 ! markus 62: RCSID("$OpenBSD: clientloop.c,v 1.82 2001/09/17 20:52:47 markus Exp $");
1.1 deraadt 63:
1.45 markus 64: #include "ssh.h"
65: #include "ssh1.h"
66: #include "ssh2.h"
1.1 deraadt 67: #include "xmalloc.h"
68: #include "packet.h"
69: #include "buffer.h"
1.15 markus 70: #include "compat.h"
1.74 markus 71: #include "channels.h"
1.15 markus 72: #include "dispatch.h"
1.30 markus 73: #include "buffer.h"
74: #include "bufaux.h"
1.40 markus 75: #include "key.h"
1.54 markus 76: #include "kex.h"
1.45 markus 77: #include "log.h"
78: #include "readconf.h"
79: #include "clientloop.h"
1.40 markus 80: #include "authfd.h"
1.45 markus 81: #include "atomicio.h"
1.62 stevesk 82: #include "sshtty.h"
1.63 markus 83: #include "misc.h"
1.39 markus 84:
85: /* import options */
86: extern Options options;
87:
1.1 deraadt 88: /* Flag indicating that stdin should be redirected from /dev/null. */
89: extern int stdin_null_flag;
90:
1.13 markus 91: /*
92: * Name of the host we are connecting to. This is the name given on the
93: * command line, or the HostName specified for the user-supplied name in a
94: * configuration file.
95: */
1.1 deraadt 96: extern char *host;
97:
1.13 markus 98: /*
99: * Flag to indicate that we have received a window change signal which has
100: * not yet been processed. This will cause a message indicating the new
101: * window size to be sent to the server a little later. This is volatile
102: * because this is updated in a signal handler.
103: */
1.1 deraadt 104: static volatile int received_window_change_signal = 0;
1.75 markus 105: static volatile int received_signal = 0;
1.1 deraadt 106:
107: /* Flag indicating whether the user\'s terminal is in non-blocking mode. */
108: static int in_non_blocking_mode = 0;
109:
110: /* Common data for the client loop code. */
1.31 markus 111: static int quit_pending; /* Set to non-zero to quit the client loop. */
112: static int escape_char; /* Escape character. */
1.11 markus 113: static int escape_pending; /* Last character was the escape character */
114: static int last_was_cr; /* Last character was a newline. */
115: static int exit_status; /* Used to store the exit status of the command. */
116: static int stdin_eof; /* EOF has been encountered on standard error. */
117: static Buffer stdin_buffer; /* Buffer for stdin data. */
118: static Buffer stdout_buffer; /* Buffer for stdout data. */
119: static Buffer stderr_buffer; /* Buffer for stderr data. */
1.42 markus 120: static u_long stdin_bytes, stdout_bytes, stderr_bytes;
121: static u_int buffer_high;/* Soft max buffer size. */
1.11 markus 122: static int connection_in; /* Connection to server (input). */
123: static int connection_out; /* Connection to server (output). */
1.56 markus 124: static int need_rekeying; /* Set to non-zero if rekeying is requested. */
1.60 markus 125: static int session_closed = 0; /* In SSH2: login session closed. */
1.1 deraadt 126:
1.77 itojun 127: static void client_init_dispatch(void);
1.16 markus 128: int session_ident = -1;
129:
1.54 markus 130: /*XXX*/
131: extern Kex *xxx_kex;
132:
1.1 deraadt 133: /* Restores stdin to blocking mode. */
134:
1.77 itojun 135: static void
1.49 itojun 136: leave_non_blocking(void)
1.1 deraadt 137: {
1.11 markus 138: if (in_non_blocking_mode) {
139: (void) fcntl(fileno(stdin), F_SETFL, 0);
140: in_non_blocking_mode = 0;
141: fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL);
142: }
1.1 deraadt 143: }
144:
1.11 markus 145: /* Puts stdin terminal in non-blocking mode. */
146:
1.77 itojun 147: static void
1.49 itojun 148: enter_non_blocking(void)
1.1 deraadt 149: {
1.11 markus 150: in_non_blocking_mode = 1;
151: (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
152: fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL);
1.1 deraadt 153: }
154:
1.13 markus 155: /*
156: * Signal handler for the window change signal (SIGWINCH). This just sets a
157: * flag indicating that the window has changed.
158: */
1.1 deraadt 159:
1.77 itojun 160: static void
1.11 markus 161: window_change_handler(int sig)
1.1 deraadt 162: {
1.11 markus 163: received_window_change_signal = 1;
164: signal(SIGWINCH, window_change_handler);
1.1 deraadt 165: }
166:
1.13 markus 167: /*
168: * Signal handler for signals that cause the program to terminate. These
169: * signals must be trapped to restore terminal modes.
170: */
1.1 deraadt 171:
1.77 itojun 172: static void
1.11 markus 173: signal_handler(int sig)
1.1 deraadt 174: {
1.75 markus 175: received_signal = sig;
176: quit_pending = 1;
1.1 deraadt 177: }
178:
1.13 markus 179: /*
180: * Returns current time in seconds from Jan 1, 1970 with the maximum
181: * available resolution.
182: */
1.1 deraadt 183:
1.77 itojun 184: static double
1.49 itojun 185: get_current_time(void)
1.1 deraadt 186: {
1.11 markus 187: struct timeval tv;
188: gettimeofday(&tv, NULL);
189: return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
1.1 deraadt 190: }
191:
1.13 markus 192: /*
193: * This is called when the interactive is entered. This checks if there is
194: * an EOF coming on stdin. We must check this explicitly, as select() does
195: * not appear to wake up when redirecting from /dev/null.
196: */
1.1 deraadt 197:
1.77 itojun 198: static void
1.49 itojun 199: client_check_initial_eof_on_stdin(void)
1.1 deraadt 200: {
1.11 markus 201: int len;
202: char buf[1];
1.1 deraadt 203:
1.13 markus 204: /*
205: * If standard input is to be "redirected from /dev/null", we simply
206: * mark that we have seen an EOF and send an EOF message to the
207: * server. Otherwise, we try to read a single character; it appears
208: * that for some files, such /dev/null, select() never wakes up for
209: * read for this descriptor, which means that we never get EOF. This
210: * way we will get the EOF if stdin comes from /dev/null or similar.
211: */
1.11 markus 212: if (stdin_null_flag) {
213: /* Fake EOF on stdin. */
214: debug("Sending eof.");
215: stdin_eof = 1;
216: packet_start(SSH_CMSG_EOF);
217: packet_send();
218: } else {
219: enter_non_blocking();
220:
221: /* Check for immediate EOF on stdin. */
222: len = read(fileno(stdin), buf, 1);
223: if (len == 0) {
1.13 markus 224: /* EOF. Record that we have seen it and send EOF to server. */
1.11 markus 225: debug("Sending eof.");
226: stdin_eof = 1;
227: packet_start(SSH_CMSG_EOF);
228: packet_send();
229: } else if (len > 0) {
1.13 markus 230: /*
231: * Got data. We must store the data in the buffer,
232: * and also process it as an escape character if
233: * appropriate.
234: */
1.42 markus 235: if ((u_char) buf[0] == escape_char)
1.11 markus 236: escape_pending = 1;
1.52 markus 237: else
1.11 markus 238: buffer_append(&stdin_buffer, buf, 1);
239: }
240: leave_non_blocking();
241: }
1.1 deraadt 242: }
243:
244:
1.13 markus 245: /*
246: * Make packets from buffered stdin data, and buffer them for sending to the
247: * connection.
248: */
1.1 deraadt 249:
1.77 itojun 250: static void
1.49 itojun 251: client_make_packets_from_stdin_data(void)
1.1 deraadt 252: {
1.42 markus 253: u_int len;
1.1 deraadt 254:
1.11 markus 255: /* Send buffered stdin data to the server. */
256: while (buffer_len(&stdin_buffer) > 0 &&
257: packet_not_very_much_data_to_write()) {
258: len = buffer_len(&stdin_buffer);
259: /* Keep the packets at reasonable size. */
260: if (len > packet_get_maxsize())
261: len = packet_get_maxsize();
262: packet_start(SSH_CMSG_STDIN_DATA);
263: packet_put_string(buffer_ptr(&stdin_buffer), len);
264: packet_send();
265: buffer_consume(&stdin_buffer, len);
1.52 markus 266: stdin_bytes += len;
1.11 markus 267: /* If we have a pending EOF, send it now. */
268: if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
269: packet_start(SSH_CMSG_EOF);
270: packet_send();
271: }
1.1 deraadt 272: }
273: }
274:
1.13 markus 275: /*
276: * Checks if the client window has changed, and sends a packet about it to
277: * the server if so. The actual change is detected elsewhere (by a software
278: * interrupt on Unix); this just checks the flag and sends a message if
279: * appropriate.
280: */
1.1 deraadt 281:
1.77 itojun 282: static void
1.49 itojun 283: client_check_window_change(void)
1.1 deraadt 284: {
1.16 markus 285: struct winsize ws;
286:
287: if (! received_window_change_signal)
288: return;
289: /** XXX race */
290: received_window_change_signal = 0;
291:
292: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
293: return;
294:
1.37 markus 295: debug2("client_check_window_change: changed");
1.16 markus 296:
297: if (compat20) {
298: channel_request_start(session_ident, "window-change", 0);
299: packet_put_int(ws.ws_col);
300: packet_put_int(ws.ws_row);
301: packet_put_int(ws.ws_xpixel);
302: packet_put_int(ws.ws_ypixel);
303: packet_send();
304: } else {
305: packet_start(SSH_CMSG_WINDOW_SIZE);
306: packet_put_int(ws.ws_row);
307: packet_put_int(ws.ws_col);
308: packet_put_int(ws.ws_xpixel);
309: packet_put_int(ws.ws_ypixel);
310: packet_send();
1.1 deraadt 311: }
312: }
313:
1.13 markus 314: /*
315: * Waits until the client can do something (some data becomes available on
316: * one of the file descriptors).
317: */
1.1 deraadt 318:
1.77 itojun 319: static void
1.46 markus 320: client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
1.81 markus 321: int *maxfdp, int *nallocp, int rekeying)
1.11 markus 322: {
1.46 markus 323: /* Add any selections by the channel mechanism. */
1.81 markus 324: channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
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())
1.46 markus 331: FD_SET(connection_in, *readsetp);
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())
1.46 markus 337: FD_SET(fileno(stdin), *readsetp);
1.17 markus 338:
339: /* Select stdout/stderr if have data in buffer. */
340: if (buffer_len(&stdout_buffer) > 0)
1.46 markus 341: FD_SET(fileno(stdout), *writesetp);
1.17 markus 342: if (buffer_len(&stderr_buffer) > 0)
1.46 markus 343: FD_SET(fileno(stderr), *writesetp);
1.16 markus 344: } else {
1.71 markus 345: /* channel_prepare_select could have closed the last channel */
346: if (session_closed && !channel_still_open()) {
347: if (!packet_have_data_to_write())
348: return;
349: } else {
350: FD_SET(connection_in, *readsetp);
351: }
1.16 markus 352: }
1.11 markus 353:
354: /* Select server connection if have data to write to the server. */
355: if (packet_have_data_to_write())
1.46 markus 356: FD_SET(connection_out, *writesetp);
1.11 markus 357:
1.13 markus 358: /*
359: * Wait for something to happen. This will suspend the process until
360: * some selected descriptor can be read, written, or has some other
361: * event pending. Note: if you want to implement SSH_MSG_IGNORE
362: * messages to fool traffic analysis, this might be the place to do
363: * it: just have a random timeout for the select, and send a random
364: * SSH_MSG_IGNORE packet when the timeout expires.
365: */
1.11 markus 366:
1.46 markus 367: if (select((*maxfdp)+1, *readsetp, *writesetp, NULL, NULL) < 0) {
1.11 markus 368: char buf[100];
1.51 markus 369:
370: /*
371: * We have to clear the select masks, because we return.
372: * We have to return, because the mainloop checks for the flags
373: * set by the signal handlers.
374: */
375: memset(*readsetp, 0, *maxfdp);
376: memset(*writesetp, 0, *maxfdp);
1.50 deraadt 377:
1.11 markus 378: if (errno == EINTR)
379: return;
380: /* Note: we might still have data in the buffers. */
381: snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
382: buffer_append(&stderr_buffer, buf, strlen(buf));
383: quit_pending = 1;
384: }
385: }
386:
1.77 itojun 387: static void
1.31 markus 388: client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
1.1 deraadt 389: {
1.11 markus 390: struct winsize oldws, newws;
391:
392: /* Flush stdout and stderr buffers. */
1.31 markus 393: if (buffer_len(bout) > 0)
394: atomicio(write, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
395: if (buffer_len(berr) > 0)
396: atomicio(write, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
1.11 markus 397:
398: leave_raw_mode();
399:
1.13 markus 400: /*
401: * Free (and clear) the buffer to reduce the amount of data that gets
402: * written to swap.
403: */
1.31 markus 404: buffer_free(bin);
405: buffer_free(bout);
406: buffer_free(berr);
1.11 markus 407:
408: /* Save old window size. */
409: ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
410:
411: /* Send the suspend signal to the program itself. */
412: kill(getpid(), SIGTSTP);
413:
414: /* Check if the window size has changed. */
415: if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
416: (oldws.ws_row != newws.ws_row ||
417: oldws.ws_col != newws.ws_col ||
418: oldws.ws_xpixel != newws.ws_xpixel ||
419: oldws.ws_ypixel != newws.ws_ypixel))
420: received_window_change_signal = 1;
421:
422: /* OK, we have been continued by the user. Reinitialize buffers. */
1.31 markus 423: buffer_init(bin);
424: buffer_init(bout);
425: buffer_init(berr);
1.11 markus 426:
427: enter_raw_mode();
428: }
429:
1.77 itojun 430: static void
1.17 markus 431: client_process_net_input(fd_set * readset)
1.11 markus 432: {
1.17 markus 433: int len;
434: char buf[8192];
1.11 markus 435:
1.13 markus 436: /*
437: * Read input from the server, and add any such data to the buffer of
438: * the packet subsystem.
439: */
1.11 markus 440: if (FD_ISSET(connection_in, readset)) {
441: /* Read as much as possible. */
442: len = read(connection_in, buf, sizeof(buf));
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: quit_pending = 1;
449: return;
1.11 markus 450: }
1.13 markus 451: /*
452: * There is a kernel bug on Solaris that causes select to
453: * sometimes wake up even though there is no data available.
454: */
1.53 millert 455: if (len < 0 && (errno == EAGAIN || errno == EINTR))
1.11 markus 456: len = 0;
457:
458: if (len < 0) {
459: /* An error has encountered. Perhaps there is a network problem. */
460: snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
461: host, strerror(errno));
462: buffer_append(&stderr_buffer, buf, strlen(buf));
463: quit_pending = 1;
464: return;
465: }
466: packet_process_incoming(buf, len);
467: }
1.17 markus 468: }
1.16 markus 469:
1.31 markus 470: /* process the characters one by one */
1.77 itojun 471: static int
1.31 markus 472: process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
473: {
1.32 markus 474: char string[1024];
1.31 markus 475: pid_t pid;
476: int bytes = 0;
1.42 markus 477: u_int i;
478: u_char ch;
1.31 markus 479: char *s;
480:
481: for (i = 0; i < len; i++) {
482: /* Get one character at a time. */
483: ch = buf[i];
484:
485: if (escape_pending) {
486: /* We have previously seen an escape character. */
487: /* Clear the flag now. */
488: escape_pending = 0;
489:
490: /* Process the escaped character. */
491: switch (ch) {
492: case '.':
493: /* Terminate the connection. */
1.32 markus 494: snprintf(string, sizeof string, "%c.\r\n", escape_char);
495: buffer_append(berr, string, strlen(string));
1.31 markus 496:
497: quit_pending = 1;
498: return -1;
499:
500: case 'Z' - 64:
501: /* Suspend the program. */
502: /* Print a message to that effect to the user. */
1.32 markus 503: snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
504: buffer_append(berr, string, strlen(string));
1.31 markus 505:
506: /* Restore terminal modes and suspend. */
507: client_suspend_self(bin, bout, berr);
508:
509: /* We have been continued. */
510: continue;
511:
1.54 markus 512: case 'R':
1.59 markus 513: if (compat20) {
514: if (datafellows & SSH_BUG_NOREKEY)
515: log("Server does not support re-keying");
516: else
517: need_rekeying = 1;
518: }
1.54 markus 519: continue;
520:
1.31 markus 521: case '&':
522: /* XXX does not work yet with proto 2 */
523: if (compat20)
524: continue;
525: /*
526: * Detach the program (continue to serve connections,
527: * but put in background and no more new connections).
528: */
529: if (!stdin_eof) {
530: /*
531: * Sending SSH_CMSG_EOF alone does not always appear
532: * to be enough. So we try to send an EOF character
533: * first.
534: */
535: packet_start(SSH_CMSG_STDIN_DATA);
536: packet_put_string("\004", 1);
537: packet_send();
538: /* Close stdin. */
539: stdin_eof = 1;
540: if (buffer_len(bin) == 0) {
541: packet_start(SSH_CMSG_EOF);
542: packet_send();
543: }
544: }
545: /* Restore tty modes. */
546: leave_raw_mode();
547:
548: /* Stop listening for new connections. */
1.76 markus 549: channel_close_all(); /* proto1 only XXXX */
1.31 markus 550:
551: printf("%c& [backgrounded]\n", escape_char);
552:
553: /* Fork into background. */
554: pid = fork();
555: if (pid < 0) {
556: error("fork: %.100s", strerror(errno));
557: continue;
558: }
559: if (pid != 0) { /* This is the parent. */
560: /* The parent just exits. */
561: exit(0);
562: }
563: /* The child continues serving connections. */
564: continue; /*XXX ? */
565:
566: case '?':
1.32 markus 567: snprintf(string, sizeof string,
1.31 markus 568: "%c?\r\n\
569: Supported escape sequences:\r\n\
570: ~. - terminate connection\r\n\
1.72 stevesk 571: ~R - Request rekey (SSH protocol 2 only)\r\n\
1.31 markus 572: ~^Z - suspend ssh\r\n\
573: ~# - list forwarded connections\r\n\
574: ~& - background ssh (when waiting for connections to terminate)\r\n\
575: ~? - this message\r\n\
576: ~~ - send the escape character by typing it twice\r\n\
577: (Note that escapes are only recognized immediately after newline.)\r\n",
578: escape_char);
1.32 markus 579: buffer_append(berr, string, strlen(string));
1.31 markus 580: continue;
581:
582: case '#':
1.32 markus 583: snprintf(string, sizeof string, "%c#\r\n", escape_char);
584: buffer_append(berr, string, strlen(string));
1.31 markus 585: s = channel_open_message();
586: buffer_append(berr, s, strlen(s));
587: xfree(s);
588: continue;
589:
590: default:
591: if (ch != escape_char) {
592: buffer_put_char(bin, escape_char);
593: bytes++;
594: }
595: /* Escaped characters fall through here */
596: break;
597: }
598: } else {
599: /*
600: * The previous character was not an escape char. Check if this
601: * is an escape.
602: */
603: if (last_was_cr && ch == escape_char) {
604: /* It is. Set the flag and continue to next character. */
605: escape_pending = 1;
606: continue;
607: }
608: }
609:
610: /*
611: * Normal character. Record whether it was a newline,
612: * and append it to the buffer.
613: */
614: last_was_cr = (ch == '\r' || ch == '\n');
615: buffer_put_char(bin, ch);
616: bytes++;
617: }
618: return bytes;
619: }
620:
1.77 itojun 621: static void
1.17 markus 622: client_process_input(fd_set * readset)
623: {
1.21 deraadt 624: int len;
1.31 markus 625: char buf[8192];
1.16 markus 626:
1.11 markus 627: /* Read input from stdin. */
628: if (FD_ISSET(fileno(stdin), readset)) {
629: /* Read as much as possible. */
630: len = read(fileno(stdin), buf, sizeof(buf));
1.64 markus 631: if (len < 0 && (errno == EAGAIN || errno == EINTR))
632: return; /* we'll try again later */
1.11 markus 633: if (len <= 0) {
1.13 markus 634: /*
635: * Received EOF or error. They are treated
636: * similarly, except that an error message is printed
637: * if it was an error condition.
638: */
1.11 markus 639: if (len < 0) {
640: snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
641: buffer_append(&stderr_buffer, buf, strlen(buf));
642: }
643: /* Mark that we have seen EOF. */
644: stdin_eof = 1;
1.13 markus 645: /*
646: * Send an EOF message to the server unless there is
647: * data in the buffer. If there is data in the
648: * buffer, no message will be sent now. Code
649: * elsewhere will send the EOF when the buffer
650: * becomes empty if stdin_eof is set.
651: */
1.11 markus 652: if (buffer_len(&stdin_buffer) == 0) {
1.1 deraadt 653: packet_start(SSH_CMSG_EOF);
654: packet_send();
1.11 markus 655: }
1.72 stevesk 656: } else if (escape_char == SSH_ESCAPECHAR_NONE) {
1.13 markus 657: /*
658: * Normal successful read, and no escape character.
659: * Just append the data to buffer.
660: */
1.11 markus 661: buffer_append(&stdin_buffer, buf, len);
662: } else {
1.13 markus 663: /*
664: * Normal, successful read. But we have an escape character
665: * and have to process the characters one by one.
666: */
1.52 markus 667: if (process_escapes(&stdin_buffer, &stdout_buffer,
668: &stderr_buffer, buf, len) == -1)
1.31 markus 669: return;
1.11 markus 670: }
671: }
672: }
673:
1.77 itojun 674: static void
1.11 markus 675: client_process_output(fd_set * writeset)
676: {
677: int len;
678: char buf[100];
1.1 deraadt 679:
1.11 markus 680: /* Write buffered output to stdout. */
681: if (FD_ISSET(fileno(stdout), writeset)) {
682: /* Write as much data as possible. */
683: len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
1.14 deraadt 684: buffer_len(&stdout_buffer));
1.11 markus 685: if (len <= 0) {
1.64 markus 686: if (errno == EINTR || errno == EAGAIN)
1.11 markus 687: len = 0;
688: else {
1.13 markus 689: /*
690: * An error or EOF was encountered. Put an
691: * error message to stderr buffer.
692: */
1.11 markus 693: snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
694: buffer_append(&stderr_buffer, buf, strlen(buf));
695: quit_pending = 1;
696: return;
697: }
698: }
699: /* Consume printed data from the buffer. */
700: buffer_consume(&stdout_buffer, len);
1.57 markus 701: stdout_bytes += len;
1.11 markus 702: }
703: /* Write buffered output to stderr. */
704: if (FD_ISSET(fileno(stderr), writeset)) {
705: /* Write as much data as possible. */
706: len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
1.14 deraadt 707: buffer_len(&stderr_buffer));
1.11 markus 708: if (len <= 0) {
1.64 markus 709: if (errno == EINTR || errno == EAGAIN)
1.11 markus 710: len = 0;
711: else {
1.13 markus 712: /* EOF or error, but can't even print error message. */
1.11 markus 713: quit_pending = 1;
714: return;
715: }
716: }
717: /* Consume printed characters from the buffer. */
718: buffer_consume(&stderr_buffer, len);
1.57 markus 719: stderr_bytes += len;
1.11 markus 720: }
1.1 deraadt 721: }
722:
1.13 markus 723: /*
1.15 markus 724: * Get packets from the connection input buffer, and process them as long as
725: * there are packets available.
726: *
727: * Any unknown packets received during the actual
728: * session cause the session to terminate. This is
729: * intended to make debugging easier since no
730: * confirmations are sent. Any compatible protocol
731: * extensions must be negotiated during the
732: * preparatory phase.
733: */
734:
1.77 itojun 735: static void
1.49 itojun 736: client_process_buffered_input_packets(void)
1.15 markus 737: {
1.54 markus 738: dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
1.15 markus 739: }
740:
1.31 markus 741: /* scan buf[] for '~' before sending data to the peer */
1.30 markus 742:
1.77 itojun 743: static int
1.31 markus 744: simple_escape_filter(Channel *c, char *buf, int len)
1.30 markus 745: {
1.31 markus 746: /* XXX we assume c->extended is writeable */
747: return process_escapes(&c->input, &c->output, &c->extended, buf, len);
1.30 markus 748: }
749:
1.77 itojun 750: static void
1.60 markus 751: client_channel_closed(int id, void *arg)
752: {
753: if (id != session_ident)
754: error("client_channel_closed: id %d != session_ident %d",
755: id, session_ident);
1.83 ! markus 756: channel_cancel_cleanup(id);
1.60 markus 757: session_closed = 1;
1.62 stevesk 758: if (in_raw_mode())
1.61 markus 759: leave_raw_mode();
1.60 markus 760: }
761:
1.15 markus 762: /*
1.13 markus 763: * Implements the interactive session with the server. This is called after
764: * the user has been authenticated, and a command has been started on the
1.72 stevesk 765: * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
766: * used as an escape character for terminating or suspending the session.
1.13 markus 767: */
1.1 deraadt 768:
1.20 markus 769: int
1.30 markus 770: client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
1.1 deraadt 771: {
1.46 markus 772: fd_set *readset = NULL, *writeset = NULL;
1.11 markus 773: double start_time, total_time;
1.81 markus 774: int max_fd = 0, max_fd2 = 0, len, rekeying = 0, nalloc = 0;
1.11 markus 775: char buf[100];
776:
777: debug("Entering interactive session.");
778:
779: start_time = get_current_time();
780:
781: /* Initialize variables. */
782: escape_pending = 0;
783: last_was_cr = 1;
784: exit_status = -1;
785: stdin_eof = 0;
786: buffer_high = 64 * 1024;
787: connection_in = packet_get_connection_in();
788: connection_out = packet_get_connection_out();
1.46 markus 789: max_fd = MAX(connection_in, connection_out);
790:
791: if (!compat20) {
1.63 markus 792: /* enable nonblocking unless tty */
793: if (!isatty(fileno(stdin)))
794: set_nonblock(fileno(stdin));
795: if (!isatty(fileno(stdout)))
796: set_nonblock(fileno(stdout));
797: if (!isatty(fileno(stderr)))
798: set_nonblock(fileno(stderr));
1.46 markus 799: max_fd = MAX(max_fd, fileno(stdin));
800: max_fd = MAX(max_fd, fileno(stdout));
801: max_fd = MAX(max_fd, fileno(stderr));
802: }
1.11 markus 803: stdin_bytes = 0;
804: stdout_bytes = 0;
805: stderr_bytes = 0;
806: quit_pending = 0;
807: escape_char = escape_char_arg;
808:
809: /* Initialize buffers. */
810: buffer_init(&stdin_buffer);
811: buffer_init(&stdout_buffer);
812: buffer_init(&stderr_buffer);
813:
1.15 markus 814: client_init_dispatch();
815:
1.11 markus 816: /* Set signal handlers to restore non-blocking mode. */
817: signal(SIGINT, signal_handler);
818: signal(SIGQUIT, signal_handler);
819: signal(SIGTERM, signal_handler);
820: signal(SIGPIPE, SIG_IGN);
821: if (have_pty)
822: signal(SIGWINCH, window_change_handler);
823:
824: if (have_pty)
825: enter_raw_mode();
826:
1.48 markus 827: if (compat20) {
828: session_ident = ssh2_chan_id;
1.72 stevesk 829: if (escape_char != SSH_ESCAPECHAR_NONE)
1.48 markus 830: channel_register_filter(session_ident,
831: simple_escape_filter);
1.60 markus 832: if (session_ident != -1)
833: channel_register_cleanup(session_ident,
834: client_channel_closed);
1.48 markus 835: } else {
836: /* Check if we should immediately send eof on stdin. */
1.16 markus 837: client_check_initial_eof_on_stdin();
1.48 markus 838: }
1.30 markus 839:
1.11 markus 840: /* Main loop of the client for the interactive session mode. */
841: while (!quit_pending) {
842:
1.13 markus 843: /* Process buffered packets sent by the server. */
1.11 markus 844: client_process_buffered_input_packets();
845:
1.60 markus 846: if (compat20 && session_closed && !channel_still_open())
847: break;
848:
1.56 markus 849: rekeying = (xxx_kex != NULL && !xxx_kex->done);
1.16 markus 850:
1.56 markus 851: if (rekeying) {
852: debug("rekeying in progress");
853: } else {
854: /*
855: * Make packets of buffered stdin data, and buffer
856: * them for sending to the server.
857: */
858: if (!compat20)
859: client_make_packets_from_stdin_data();
1.11 markus 860:
1.56 markus 861: /*
862: * Make packets from buffered channel data, and
863: * enqueue them for sending to the server.
864: */
865: if (packet_not_very_much_data_to_write())
866: channel_output_poll();
1.11 markus 867:
1.56 markus 868: /*
869: * Check if the window size has changed, and buffer a
870: * message about it to the server if so.
871: */
872: client_check_window_change();
1.11 markus 873:
1.56 markus 874: if (quit_pending)
875: break;
876: }
1.13 markus 877: /*
878: * Wait until we have something to do (something becomes
879: * available on one of the descriptors).
880: */
1.81 markus 881: max_fd2 = max_fd;
1.56 markus 882: client_wait_until_can_do_something(&readset, &writeset,
1.81 markus 883: &max_fd2, &nalloc, rekeying);
1.11 markus 884:
885: if (quit_pending)
886: break;
887:
1.56 markus 888: /* Do channel operations unless rekeying in progress. */
889: if (!rekeying) {
890: channel_after_select(readset, writeset);
891:
892: if (need_rekeying) {
893: debug("user requests rekeying");
894: xxx_kex->done = 0;
895: kex_send_kexinit(xxx_kex);
896: need_rekeying = 0;
897: }
898: }
1.11 markus 899:
1.17 markus 900: /* Buffer input from the connection. */
1.46 markus 901: client_process_net_input(readset);
1.17 markus 902:
903: if (quit_pending)
904: break;
1.11 markus 905:
1.17 markus 906: if (!compat20) {
907: /* Buffer data from stdin */
1.46 markus 908: client_process_input(readset);
1.17 markus 909: /*
910: * Process output to stdout and stderr. Output to
911: * the connection is processed elsewhere (above).
912: */
1.46 markus 913: client_process_output(writeset);
1.17 markus 914: }
1.11 markus 915:
1.13 markus 916: /* Send as much buffered packet data as possible to the sender. */
1.46 markus 917: if (FD_ISSET(connection_out, writeset))
1.11 markus 918: packet_write_poll();
919: }
1.46 markus 920: if (readset)
921: xfree(readset);
922: if (writeset)
923: xfree(writeset);
1.11 markus 924:
925: /* Terminate the session. */
926:
927: /* Stop watching for window change. */
928: if (have_pty)
929: signal(SIGWINCH, SIG_DFL);
930:
1.76 markus 931: channel_free_all();
1.11 markus 932:
1.75 markus 933: if (have_pty)
934: leave_raw_mode();
935:
936: /* restore blocking io */
937: if (!isatty(fileno(stdin)))
938: unset_nonblock(fileno(stdin));
939: if (!isatty(fileno(stdout)))
940: unset_nonblock(fileno(stdout));
941: if (!isatty(fileno(stderr)))
942: unset_nonblock(fileno(stderr));
943:
944: if (received_signal) {
945: if (in_non_blocking_mode) /* XXX */
946: leave_non_blocking();
947: fatal("Killed by signal %d.", received_signal);
948: }
949:
1.13 markus 950: /*
951: * In interactive mode (with pseudo tty) display a message indicating
952: * that the connection has been closed.
953: */
1.11 markus 954: if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
955: snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
956: buffer_append(&stderr_buffer, buf, strlen(buf));
957: }
1.70 markus 958:
1.11 markus 959: /* Output any buffered data for stdout. */
1.70 markus 960: while (buffer_len(&stdout_buffer) > 0) {
961: len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
1.14 deraadt 962: buffer_len(&stdout_buffer));
1.70 markus 963: if (len <= 0) {
1.11 markus 964: error("Write failed flushing stdout buffer.");
1.70 markus 965: break;
966: }
1.11 markus 967: buffer_consume(&stdout_buffer, len);
1.57 markus 968: stdout_bytes += len;
1.11 markus 969: }
970:
971: /* Output any buffered data for stderr. */
1.70 markus 972: while (buffer_len(&stderr_buffer) > 0) {
973: len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
1.14 deraadt 974: buffer_len(&stderr_buffer));
1.70 markus 975: if (len <= 0) {
1.11 markus 976: error("Write failed flushing stderr buffer.");
1.70 markus 977: break;
978: }
1.11 markus 979: buffer_consume(&stderr_buffer, len);
1.57 markus 980: stderr_bytes += len;
1.11 markus 981: }
982:
983: /* Clear and free any buffers. */
984: memset(buf, 0, sizeof(buf));
985: buffer_free(&stdin_buffer);
986: buffer_free(&stdout_buffer);
987: buffer_free(&stderr_buffer);
988:
989: /* Report bytes transferred, and transfer rates. */
990: total_time = get_current_time() - start_time;
991: debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
992: stdin_bytes, stdout_bytes, stderr_bytes, total_time);
993: if (total_time > 0)
994: debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
995: stdin_bytes / total_time, stdout_bytes / total_time,
996: stderr_bytes / total_time);
997:
998: /* Return the exit status of the program. */
999: debug("Exit status %d", exit_status);
1000: return exit_status;
1.15 markus 1001: }
1002:
1003: /*********/
1004:
1.77 itojun 1005: static void
1.36 markus 1006: client_input_stdout_data(int type, int plen, void *ctxt)
1.15 markus 1007: {
1.42 markus 1008: u_int data_len;
1.15 markus 1009: char *data = packet_get_string(&data_len);
1010: packet_integrity_check(plen, 4 + data_len, type);
1011: buffer_append(&stdout_buffer, data, data_len);
1012: memset(data, 0, data_len);
1013: xfree(data);
1014: }
1.77 itojun 1015: static void
1.36 markus 1016: client_input_stderr_data(int type, int plen, void *ctxt)
1.15 markus 1017: {
1.42 markus 1018: u_int data_len;
1.15 markus 1019: char *data = packet_get_string(&data_len);
1020: packet_integrity_check(plen, 4 + data_len, type);
1021: buffer_append(&stderr_buffer, data, data_len);
1022: memset(data, 0, data_len);
1023: xfree(data);
1024: }
1.77 itojun 1025: static void
1.36 markus 1026: client_input_exit_status(int type, int plen, void *ctxt)
1.15 markus 1027: {
1028: packet_integrity_check(plen, 4, type);
1029: exit_status = packet_get_int();
1030: /* Acknowledge the exit. */
1031: packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1032: packet_send();
1033: /*
1034: * Must wait for packet to be sent since we are
1035: * exiting the loop.
1036: */
1037: packet_write_wait();
1038: /* Flag that we want to exit. */
1039: quit_pending = 1;
1040: }
1041:
1.77 itojun 1042: static Channel *
1.40 markus 1043: client_request_forwarded_tcpip(const char *request_type, int rchan)
1044: {
1045: Channel* c = NULL;
1046: char *listen_address, *originator_address;
1047: int listen_port, originator_port;
1.67 markus 1048: int sock;
1.40 markus 1049:
1050: /* Get rest of the packet */
1051: listen_address = packet_get_string(NULL);
1052: listen_port = packet_get_int();
1053: originator_address = packet_get_string(NULL);
1054: originator_port = packet_get_int();
1055: packet_done();
1056:
1057: debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
1058: listen_address, listen_port, originator_address, originator_port);
1059:
1.80 stevesk 1060: sock = channel_connect_by_listen_address(listen_port);
1.67 markus 1061: if (sock < 0) {
1062: xfree(originator_address);
1063: xfree(listen_address);
1064: return NULL;
1065: }
1066: c = channel_new("forwarded-tcpip",
1067: SSH_CHANNEL_CONNECTING, sock, sock, -1,
1068: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
1069: xstrdup(originator_address), 1);
1070: if (c == NULL) {
1071: error("client_request_forwarded_tcpip: channel_new failed");
1072: close(sock);
1.40 markus 1073: }
1074: xfree(originator_address);
1075: xfree(listen_address);
1076: return c;
1077: }
1078:
1.77 itojun 1079: static Channel*
1.40 markus 1080: client_request_x11(const char *request_type, int rchan)
1081: {
1082: Channel *c = NULL;
1083: char *originator;
1084: int originator_port;
1.67 markus 1085: int sock;
1.40 markus 1086:
1087: if (!options.forward_x11) {
1088: error("Warning: ssh server tried X11 forwarding.");
1089: error("Warning: this is probably a break in attempt by a malicious server.");
1090: return NULL;
1091: }
1092: originator = packet_get_string(NULL);
1093: if (datafellows & SSH_BUG_X11FWD) {
1094: debug2("buggy server: x11 request w/o originator_port");
1095: originator_port = 0;
1096: } else {
1097: originator_port = packet_get_int();
1098: }
1099: packet_done();
1100: /* XXX check permission */
1.47 markus 1101: debug("client_request_x11: request from %s %d", originator,
1102: originator_port);
1.67 markus 1103: xfree(originator);
1.40 markus 1104: sock = x11_connect_display();
1.67 markus 1105: if (sock < 0)
1106: return NULL;
1107: c = channel_new("x11",
1108: SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1109: CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0,
1110: xstrdup("x11"), 1);
1111: if (c == NULL) {
1112: error("client_request_x11: channel_new failed");
1113: close(sock);
1.40 markus 1114: }
1.82 markus 1115: c->force_drain = 1;
1.40 markus 1116: return c;
1117: }
1118:
1.77 itojun 1119: static Channel*
1.40 markus 1120: client_request_agent(const char *request_type, int rchan)
1121: {
1122: Channel *c = NULL;
1.67 markus 1123: int sock;
1.40 markus 1124:
1125: if (!options.forward_agent) {
1126: error("Warning: ssh server tried agent forwarding.");
1127: error("Warning: this is probably a break in attempt by a malicious server.");
1128: return NULL;
1129: }
1130: sock = ssh_get_authentication_socket();
1.67 markus 1131: if (sock < 0)
1132: return NULL;
1133: c = channel_new("authentication agent connection",
1134: SSH_CHANNEL_OPEN, sock, sock, -1,
1135: CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
1136: xstrdup("authentication agent connection"), 1);
1137: if (c == NULL) {
1138: error("client_request_agent: channel_new failed");
1139: close(sock);
1.40 markus 1140: }
1.82 markus 1141: c->force_drain = 1;
1.40 markus 1142: return c;
1143: }
1144:
1.22 markus 1145: /* XXXX move to generic input handler */
1.77 itojun 1146: static void
1.36 markus 1147: client_input_channel_open(int type, int plen, void *ctxt)
1.22 markus 1148: {
1149: Channel *c = NULL;
1150: char *ctype;
1.42 markus 1151: u_int len;
1.22 markus 1152: int rchan;
1153: int rmaxpack;
1154: int rwindow;
1155:
1156: ctype = packet_get_string(&len);
1157: rchan = packet_get_int();
1158: rwindow = packet_get_int();
1159: rmaxpack = packet_get_int();
1160:
1.24 markus 1161: debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
1.22 markus 1162: ctype, rchan, rwindow, rmaxpack);
1163:
1.40 markus 1164: if (strcmp(ctype, "forwarded-tcpip") == 0) {
1165: c = client_request_forwarded_tcpip(ctype, rchan);
1166: } else if (strcmp(ctype, "x11") == 0) {
1167: c = client_request_x11(ctype, rchan);
1168: } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1169: c = client_request_agent(ctype, rchan);
1.22 markus 1170: }
1171: /* XXX duplicate : */
1172: if (c != NULL) {
1173: debug("confirm %s", ctype);
1174: c->remote_id = rchan;
1175: c->remote_window = rwindow;
1176: c->remote_maxpacket = rmaxpack;
1.69 markus 1177: if (c->type != SSH_CHANNEL_CONNECTING) {
1178: packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1179: packet_put_int(c->remote_id);
1180: packet_put_int(c->self);
1181: packet_put_int(c->local_window);
1182: packet_put_int(c->local_maxpacket);
1183: packet_send();
1184: }
1.22 markus 1185: } else {
1186: debug("failure %s", ctype);
1187: packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1188: packet_put_int(rchan);
1189: packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
1.66 markus 1190: if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1.69 markus 1191: packet_put_cstring("open failed");
1.66 markus 1192: packet_put_cstring("");
1193: }
1.22 markus 1194: packet_send();
1195: }
1196: xfree(ctype);
1197: }
1.77 itojun 1198: static void
1.48 markus 1199: client_input_channel_req(int type, int plen, void *ctxt)
1200: {
1201: Channel *c = NULL;
1202: int id, reply, success = 0;
1203: char *rtype;
1204:
1205: id = packet_get_int();
1206: rtype = packet_get_string(NULL);
1207: reply = packet_get_char();
1208:
1209: debug("client_input_channel_req: channel %d rtype %s reply %d",
1210: id, rtype, reply);
1211:
1212: if (session_ident == -1) {
1213: error("client_input_channel_req: no channel %d", session_ident);
1214: } else if (id != session_ident) {
1215: error("client_input_channel_req: channel %d: wrong channel: %d",
1216: session_ident, id);
1217: }
1218: c = channel_lookup(id);
1219: if (c == NULL) {
1220: error("client_input_channel_req: channel %d: unknown channel", id);
1221: } else if (strcmp(rtype, "exit-status") == 0) {
1222: success = 1;
1223: exit_status = packet_get_int();
1224: packet_done();
1225: }
1226: if (reply) {
1227: packet_start(success ?
1228: SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1229: packet_put_int(c->remote_id);
1230: packet_send();
1231: }
1232: xfree(rtype);
1233: }
1.22 markus 1234:
1.77 itojun 1235: static void
1.49 itojun 1236: client_init_dispatch_20(void)
1.16 markus 1237: {
1.55 markus 1238: dispatch_init(&dispatch_protocol_error);
1.16 markus 1239: dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1240: dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1241: dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1242: dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
1.22 markus 1243: dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
1.16 markus 1244: dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1245: dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1.48 markus 1246: dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
1.16 markus 1247: dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
1.55 markus 1248:
1249: /* rekeying */
1250: dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
1.16 markus 1251: }
1.77 itojun 1252: static void
1.49 itojun 1253: client_init_dispatch_13(void)
1.15 markus 1254: {
1255: dispatch_init(NULL);
1256: dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1257: dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1258: dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
1259: dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1260: dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1261: dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
1262: dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
1263: dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
1264: dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
1.39 markus 1265:
1266: dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
1267: &auth_input_open_request : &deny_input_open);
1268: dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
1269: &x11_input_open : &deny_input_open);
1.15 markus 1270: }
1.77 itojun 1271: static void
1.49 itojun 1272: client_init_dispatch_15(void)
1.15 markus 1273: {
1274: client_init_dispatch_13();
1275: dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1276: dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
1277: }
1.79 stevesk 1278: static void
1.49 itojun 1279: client_init_dispatch(void)
1.15 markus 1280: {
1.16 markus 1281: if (compat20)
1282: client_init_dispatch_20();
1283: else if (compat13)
1.15 markus 1284: client_init_dispatch_13();
1285: else
1286: client_init_dispatch_15();
1.1 deraadt 1287: }