Annotation of src/usr.bin/ssh/clientloop.c, Revision 1.32
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.32 ! markus 19: RCSID("$OpenBSD: clientloop.c,v 1.31 2000/08/19 21:55:52 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.31 markus 66: static int quit_pending; /* Set to non-zero to quit the client loop. */
67: static int escape_char; /* Escape character. */
1.11 markus 68: static int escape_pending; /* Last character was the escape character */
69: static int last_was_cr; /* Last character was a newline. */
70: static int exit_status; /* Used to store the exit status of the command. */
71: static int stdin_eof; /* EOF has been encountered on standard error. */
72: static Buffer stdin_buffer; /* Buffer for stdin data. */
73: static Buffer stdout_buffer; /* Buffer for stdout data. */
74: static Buffer stderr_buffer; /* Buffer for stderr data. */
1.31 markus 75: static unsigned long stdin_bytes, stdout_bytes, stderr_bytes;
1.11 markus 76: static unsigned int buffer_high;/* Soft max buffer size. */
77: static int max_fd; /* Maximum file descriptor number in select(). */
78: static int connection_in; /* Connection to server (input). */
79: static int connection_out; /* Connection to server (output). */
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.31 markus 385: client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
1.1 deraadt 386: {
1.11 markus 387: struct winsize oldws, newws;
388:
389: /* Flush stdout and stderr buffers. */
1.31 markus 390: if (buffer_len(bout) > 0)
391: atomicio(write, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
392: if (buffer_len(berr) > 0)
393: atomicio(write, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
1.11 markus 394:
395: leave_raw_mode();
396:
1.13 markus 397: /*
398: * Free (and clear) the buffer to reduce the amount of data that gets
399: * written to swap.
400: */
1.31 markus 401: buffer_free(bin);
402: buffer_free(bout);
403: buffer_free(berr);
1.11 markus 404:
405: /* Save old window size. */
406: ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
407:
408: /* Send the suspend signal to the program itself. */
409: kill(getpid(), SIGTSTP);
410:
411: /* Check if the window size has changed. */
412: if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
413: (oldws.ws_row != newws.ws_row ||
414: oldws.ws_col != newws.ws_col ||
415: oldws.ws_xpixel != newws.ws_xpixel ||
416: oldws.ws_ypixel != newws.ws_ypixel))
417: received_window_change_signal = 1;
418:
419: /* OK, we have been continued by the user. Reinitialize buffers. */
1.31 markus 420: buffer_init(bin);
421: buffer_init(bout);
422: buffer_init(berr);
1.11 markus 423:
424: enter_raw_mode();
425: }
426:
1.20 markus 427: void
1.17 markus 428: client_process_net_input(fd_set * readset)
1.11 markus 429: {
1.17 markus 430: int len;
431: char buf[8192];
1.11 markus 432:
1.13 markus 433: /*
434: * Read input from the server, and add any such data to the buffer of
435: * the packet subsystem.
436: */
1.11 markus 437: if (FD_ISSET(connection_in, readset)) {
438: /* Read as much as possible. */
439: len = read(connection_in, buf, sizeof(buf));
1.16 markus 440: /*debug("read connection_in len %d", len); XXX */
1.11 markus 441: if (len == 0) {
442: /* Received EOF. The remote host has closed the connection. */
443: snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
444: host);
1.1 deraadt 445: buffer_append(&stderr_buffer, buf, strlen(buf));
446: stderr_bytes += strlen(buf);
447: quit_pending = 1;
448: return;
1.11 markus 449: }
1.13 markus 450: /*
451: * There is a kernel bug on Solaris that causes select to
452: * sometimes wake up even though there is no data available.
453: */
1.11 markus 454: if (len < 0 && errno == EAGAIN)
455: len = 0;
456:
457: if (len < 0) {
458: /* An error has encountered. Perhaps there is a network problem. */
459: snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
460: host, strerror(errno));
461: buffer_append(&stderr_buffer, buf, strlen(buf));
462: stderr_bytes += 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 */
471: int
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;
477: unsigned int i;
478: unsigned char ch;
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));
! 496: /*stderr_bytes += strlen(string); XXX*/
1.31 markus 497:
498: quit_pending = 1;
499: return -1;
500:
501: case 'Z' - 64:
502: /* Suspend the program. */
503: /* Print a message to that effect to the user. */
1.32 ! markus 504: snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
! 505: buffer_append(berr, string, strlen(string));
! 506: /*stderr_bytes += strlen(string); XXX*/
1.31 markus 507:
508: /* Restore terminal modes and suspend. */
509: client_suspend_self(bin, bout, berr);
510:
511: /* We have been continued. */
512: continue;
513:
514: case '&':
515: /* XXX does not work yet with proto 2 */
516: if (compat20)
517: continue;
518: /*
519: * Detach the program (continue to serve connections,
520: * but put in background and no more new connections).
521: */
522: if (!stdin_eof) {
523: /*
524: * Sending SSH_CMSG_EOF alone does not always appear
525: * to be enough. So we try to send an EOF character
526: * first.
527: */
528: packet_start(SSH_CMSG_STDIN_DATA);
529: packet_put_string("\004", 1);
530: packet_send();
531: /* Close stdin. */
532: stdin_eof = 1;
533: if (buffer_len(bin) == 0) {
534: packet_start(SSH_CMSG_EOF);
535: packet_send();
536: }
537: }
538: /* Restore tty modes. */
539: leave_raw_mode();
540:
541: /* Stop listening for new connections. */
542: channel_stop_listening();
543:
544: printf("%c& [backgrounded]\n", escape_char);
545:
546: /* Fork into background. */
547: pid = fork();
548: if (pid < 0) {
549: error("fork: %.100s", strerror(errno));
550: continue;
551: }
552: if (pid != 0) { /* This is the parent. */
553: /* The parent just exits. */
554: exit(0);
555: }
556: /* The child continues serving connections. */
557: continue; /*XXX ? */
558:
559: case '?':
1.32 ! markus 560: snprintf(string, sizeof string,
1.31 markus 561: "%c?\r\n\
562: Supported escape sequences:\r\n\
563: ~. - terminate connection\r\n\
564: ~^Z - suspend ssh\r\n\
565: ~# - list forwarded connections\r\n\
566: ~& - background ssh (when waiting for connections to terminate)\r\n\
567: ~? - this message\r\n\
568: ~~ - send the escape character by typing it twice\r\n\
569: (Note that escapes are only recognized immediately after newline.)\r\n",
570: escape_char);
1.32 ! markus 571: buffer_append(berr, string, strlen(string));
1.31 markus 572: continue;
573:
574: case '#':
1.32 ! markus 575: snprintf(string, sizeof string, "%c#\r\n", escape_char);
! 576: buffer_append(berr, string, strlen(string));
1.31 markus 577: s = channel_open_message();
578: buffer_append(berr, s, strlen(s));
579: xfree(s);
580: continue;
581:
582: default:
583: if (ch != escape_char) {
584: buffer_put_char(bin, escape_char);
585: bytes++;
586: }
587: /* Escaped characters fall through here */
588: break;
589: }
590: } else {
591: /*
592: * The previous character was not an escape char. Check if this
593: * is an escape.
594: */
595: if (last_was_cr && ch == escape_char) {
596: /* It is. Set the flag and continue to next character. */
597: escape_pending = 1;
598: continue;
599: }
600: }
601:
602: /*
603: * Normal character. Record whether it was a newline,
604: * and append it to the buffer.
605: */
606: last_was_cr = (ch == '\r' || ch == '\n');
607: buffer_put_char(bin, ch);
608: bytes++;
609: }
610: return bytes;
611: }
612:
1.20 markus 613: void
1.17 markus 614: client_process_input(fd_set * readset)
615: {
1.31 markus 616: int ret;
1.21 deraadt 617: int len;
1.31 markus 618: char buf[8192];
1.16 markus 619:
1.11 markus 620: /* Read input from stdin. */
621: if (FD_ISSET(fileno(stdin), readset)) {
622: /* Read as much as possible. */
623: len = read(fileno(stdin), buf, sizeof(buf));
624: if (len <= 0) {
1.13 markus 625: /*
626: * Received EOF or error. They are treated
627: * similarly, except that an error message is printed
628: * if it was an error condition.
629: */
1.11 markus 630: if (len < 0) {
631: snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
632: buffer_append(&stderr_buffer, buf, strlen(buf));
633: stderr_bytes += strlen(buf);
634: }
635: /* Mark that we have seen EOF. */
636: stdin_eof = 1;
1.13 markus 637: /*
638: * Send an EOF message to the server unless there is
639: * data in the buffer. If there is data in the
640: * buffer, no message will be sent now. Code
641: * elsewhere will send the EOF when the buffer
642: * becomes empty if stdin_eof is set.
643: */
1.11 markus 644: if (buffer_len(&stdin_buffer) == 0) {
1.1 deraadt 645: packet_start(SSH_CMSG_EOF);
646: packet_send();
1.11 markus 647: }
648: } else if (escape_char == -1) {
1.13 markus 649: /*
650: * Normal successful read, and no escape character.
651: * Just append the data to buffer.
652: */
1.11 markus 653: buffer_append(&stdin_buffer, buf, len);
654: stdin_bytes += len;
655: } else {
1.13 markus 656: /*
657: * Normal, successful read. But we have an escape character
658: * and have to process the characters one by one.
659: */
1.31 markus 660: ret = process_escapes(&stdin_buffer, &stdout_buffer, &stderr_buffer, buf, len);
661: if (ret == -1)
662: return;
663: stdout_bytes += ret;
1.11 markus 664: }
665: }
666: }
667:
1.20 markus 668: void
1.11 markus 669: client_process_output(fd_set * writeset)
670: {
671: int len;
672: char buf[100];
1.1 deraadt 673:
1.11 markus 674: /* Write buffered output to stdout. */
675: if (FD_ISSET(fileno(stdout), writeset)) {
676: /* Write as much data as possible. */
677: len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
1.14 deraadt 678: buffer_len(&stdout_buffer));
1.11 markus 679: if (len <= 0) {
680: if (errno == EAGAIN)
681: len = 0;
682: else {
1.13 markus 683: /*
684: * An error or EOF was encountered. Put an
685: * error message to stderr buffer.
686: */
1.11 markus 687: snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
688: buffer_append(&stderr_buffer, buf, strlen(buf));
689: stderr_bytes += strlen(buf);
690: quit_pending = 1;
691: return;
692: }
693: }
694: /* Consume printed data from the buffer. */
695: buffer_consume(&stdout_buffer, len);
696: }
697: /* Write buffered output to stderr. */
698: if (FD_ISSET(fileno(stderr), writeset)) {
699: /* Write as much data as possible. */
700: len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
1.14 deraadt 701: buffer_len(&stderr_buffer));
1.11 markus 702: if (len <= 0) {
703: if (errno == EAGAIN)
704: len = 0;
705: else {
1.13 markus 706: /* EOF or error, but can't even print error message. */
1.11 markus 707: quit_pending = 1;
708: return;
709: }
710: }
711: /* Consume printed characters from the buffer. */
712: buffer_consume(&stderr_buffer, len);
713: }
1.1 deraadt 714: }
715:
1.13 markus 716: /*
1.15 markus 717: * Get packets from the connection input buffer, and process them as long as
718: * there are packets available.
719: *
720: * Any unknown packets received during the actual
721: * session cause the session to terminate. This is
722: * intended to make debugging easier since no
723: * confirmations are sent. Any compatible protocol
724: * extensions must be negotiated during the
725: * preparatory phase.
726: */
727:
1.20 markus 728: void
1.15 markus 729: client_process_buffered_input_packets()
730: {
731: dispatch_run(DISPATCH_NONBLOCK, &quit_pending);
732: }
733:
1.31 markus 734: /* scan buf[] for '~' before sending data to the peer */
1.30 markus 735:
736: int
1.31 markus 737: simple_escape_filter(Channel *c, char *buf, int len)
1.30 markus 738: {
1.31 markus 739: /* XXX we assume c->extended is writeable */
740: return process_escapes(&c->input, &c->output, &c->extended, buf, len);
1.30 markus 741: }
742:
1.15 markus 743: /*
1.13 markus 744: * Implements the interactive session with the server. This is called after
745: * the user has been authenticated, and a command has been started on the
746: * remote host. If escape_char != -1, it is the character used as an escape
747: * character for terminating or suspending the session.
748: */
1.1 deraadt 749:
1.20 markus 750: int
1.30 markus 751: client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
1.1 deraadt 752: {
1.11 markus 753: extern Options options;
754: double start_time, total_time;
755: int len;
756: char buf[100];
757:
758: debug("Entering interactive session.");
759:
760: start_time = get_current_time();
761:
762: /* Initialize variables. */
763: escape_pending = 0;
764: last_was_cr = 1;
765: exit_status = -1;
766: stdin_eof = 0;
767: buffer_high = 64 * 1024;
768: connection_in = packet_get_connection_in();
769: connection_out = packet_get_connection_out();
770: max_fd = connection_in;
771: if (connection_out > max_fd)
772: max_fd = connection_out;
773: stdin_bytes = 0;
774: stdout_bytes = 0;
775: stderr_bytes = 0;
776: quit_pending = 0;
777: escape_char = escape_char_arg;
778:
779: /* Initialize buffers. */
780: buffer_init(&stdin_buffer);
781: buffer_init(&stdout_buffer);
782: buffer_init(&stderr_buffer);
783:
1.15 markus 784: client_init_dispatch();
785:
1.11 markus 786: /* Set signal handlers to restore non-blocking mode. */
787: signal(SIGINT, signal_handler);
788: signal(SIGQUIT, signal_handler);
789: signal(SIGTERM, signal_handler);
790: signal(SIGPIPE, SIG_IGN);
791: if (have_pty)
792: signal(SIGWINCH, window_change_handler);
793:
794: if (have_pty)
795: enter_raw_mode();
796:
1.28 provos 797: /* Check if we should immediately send eof on stdin. */
1.17 markus 798: if (!compat20)
1.16 markus 799: client_check_initial_eof_on_stdin();
1.11 markus 800:
1.30 markus 801: if (compat20 && escape_char != -1)
802: channel_register_filter(ssh2_chan_id, simple_escape_filter);
803:
1.11 markus 804: /* Main loop of the client for the interactive session mode. */
805: while (!quit_pending) {
806: fd_set readset, writeset;
807:
1.13 markus 808: /* Process buffered packets sent by the server. */
1.11 markus 809: client_process_buffered_input_packets();
810:
1.16 markus 811: if (compat20 && !channel_still_open()) {
812: debug("!channel_still_open.");
813: break;
814: }
815:
1.13 markus 816: /*
817: * Make packets of buffered stdin data, and buffer them for
818: * sending to the server.
819: */
1.17 markus 820: if (!compat20)
1.16 markus 821: client_make_packets_from_stdin_data();
1.11 markus 822:
1.13 markus 823: /*
824: * Make packets from buffered channel data, and buffer them
825: * for sending to the server.
826: */
1.11 markus 827: if (packet_not_very_much_data_to_write())
828: channel_output_poll();
829:
1.13 markus 830: /*
831: * Check if the window size has changed, and buffer a message
832: * about it to the server if so.
833: */
1.11 markus 834: client_check_window_change();
835:
836: if (quit_pending)
837: break;
838:
1.13 markus 839: /*
840: * Wait until we have something to do (something becomes
841: * available on one of the descriptors).
842: */
1.11 markus 843: client_wait_until_can_do_something(&readset, &writeset);
844:
845: if (quit_pending)
846: break;
847:
848: /* Do channel operations. */
849: channel_after_select(&readset, &writeset);
850:
1.17 markus 851: /* Buffer input from the connection. */
852: client_process_net_input(&readset);
853:
854: if (quit_pending)
855: break;
1.11 markus 856:
1.17 markus 857: if (!compat20) {
858: /* Buffer data from stdin */
859: client_process_input(&readset);
860: /*
861: * Process output to stdout and stderr. Output to
862: * the connection is processed elsewhere (above).
863: */
1.16 markus 864: client_process_output(&writeset);
1.17 markus 865: }
1.11 markus 866:
1.13 markus 867: /* Send as much buffered packet data as possible to the sender. */
1.11 markus 868: if (FD_ISSET(connection_out, &writeset))
869: packet_write_poll();
870: }
871:
872: /* Terminate the session. */
873:
874: /* Stop watching for window change. */
875: if (have_pty)
876: signal(SIGWINCH, SIG_DFL);
877:
878: /* Stop listening for connections. */
879: channel_stop_listening();
880:
1.13 markus 881: /*
882: * In interactive mode (with pseudo tty) display a message indicating
883: * that the connection has been closed.
884: */
1.11 markus 885: if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
886: snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
887: buffer_append(&stderr_buffer, buf, strlen(buf));
888: stderr_bytes += strlen(buf);
889: }
890: /* Output any buffered data for stdout. */
891: while (buffer_len(&stdout_buffer) > 0) {
892: len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
1.14 deraadt 893: buffer_len(&stdout_buffer));
1.11 markus 894: if (len <= 0) {
895: error("Write failed flushing stdout buffer.");
896: break;
897: }
898: buffer_consume(&stdout_buffer, len);
899: }
900:
901: /* Output any buffered data for stderr. */
902: while (buffer_len(&stderr_buffer) > 0) {
903: len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
1.14 deraadt 904: buffer_len(&stderr_buffer));
1.11 markus 905: if (len <= 0) {
906: error("Write failed flushing stderr buffer.");
907: break;
908: }
909: buffer_consume(&stderr_buffer, len);
910: }
911:
912: if (have_pty)
913: leave_raw_mode();
914:
915: /* Clear and free any buffers. */
916: memset(buf, 0, sizeof(buf));
917: buffer_free(&stdin_buffer);
918: buffer_free(&stdout_buffer);
919: buffer_free(&stderr_buffer);
920:
921: /* Report bytes transferred, and transfer rates. */
922: total_time = get_current_time() - start_time;
923: debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
924: stdin_bytes, stdout_bytes, stderr_bytes, total_time);
925: if (total_time > 0)
926: debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
927: stdin_bytes / total_time, stdout_bytes / total_time,
928: stderr_bytes / total_time);
929:
930: /* Return the exit status of the program. */
931: debug("Exit status %d", exit_status);
932: return exit_status;
1.15 markus 933: }
934:
935: /*********/
936:
937: void
938: client_input_stdout_data(int type, int plen)
939: {
940: unsigned int data_len;
941: char *data = packet_get_string(&data_len);
942: packet_integrity_check(plen, 4 + data_len, type);
943: buffer_append(&stdout_buffer, data, data_len);
944: stdout_bytes += data_len;
945: memset(data, 0, data_len);
946: xfree(data);
947: }
948: void
949: client_input_stderr_data(int type, int plen)
950: {
951: unsigned int data_len;
952: char *data = packet_get_string(&data_len);
953: packet_integrity_check(plen, 4 + data_len, type);
954: buffer_append(&stderr_buffer, data, data_len);
955: stdout_bytes += data_len;
956: memset(data, 0, data_len);
957: xfree(data);
958: }
959: void
960: client_input_exit_status(int type, int plen)
961: {
962: packet_integrity_check(plen, 4, type);
963: exit_status = packet_get_int();
964: /* Acknowledge the exit. */
965: packet_start(SSH_CMSG_EXIT_CONFIRMATION);
966: packet_send();
967: /*
968: * Must wait for packet to be sent since we are
969: * exiting the loop.
970: */
971: packet_write_wait();
972: /* Flag that we want to exit. */
973: quit_pending = 1;
974: }
975:
1.22 markus 976: /* XXXX move to generic input handler */
977: void
978: client_input_channel_open(int type, int plen)
979: {
980: Channel *c = NULL;
981: char *ctype;
982: int id;
983: unsigned int len;
984: int rchan;
985: int rmaxpack;
986: int rwindow;
987:
988: ctype = packet_get_string(&len);
989: rchan = packet_get_int();
990: rwindow = packet_get_int();
991: rmaxpack = packet_get_int();
992:
1.24 markus 993: debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
1.22 markus 994: ctype, rchan, rwindow, rmaxpack);
995:
996: if (strcmp(ctype, "x11") == 0) {
997: int sock;
998: char *originator;
999: int originator_port;
1000: originator = packet_get_string(NULL);
1.26 markus 1001: if (datafellows & SSH_BUG_X11FWD) {
1.25 markus 1002: debug("buggy server: x11 request w/o originator_port");
1003: originator_port = 0;
1.26 markus 1004: } else {
1005: originator_port = packet_get_int();
1.25 markus 1006: }
1.22 markus 1007: packet_done();
1008: /* XXX check permission */
1009: xfree(originator);
1010: /* XXX move to channels.c */
1011: sock = x11_connect_display();
1012: if (sock >= 0) {
1.30 markus 1013: /*XXX MAXPACK */
1.22 markus 1014: id = channel_new("x11", SSH_CHANNEL_X11_OPEN,
1015: sock, sock, -1, 4*1024, 32*1024, 0,
1016: xstrdup("x11"));
1017: c = channel_lookup(id);
1018: }
1019: }
1020: /* XXX duplicate : */
1021: if (c != NULL) {
1022: debug("confirm %s", ctype);
1023: c->remote_id = rchan;
1024: c->remote_window = rwindow;
1025: c->remote_maxpacket = rmaxpack;
1026:
1027: packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1028: packet_put_int(c->remote_id);
1029: packet_put_int(c->self);
1030: packet_put_int(c->local_window);
1031: packet_put_int(c->local_maxpacket);
1032: packet_send();
1033: } else {
1034: debug("failure %s", ctype);
1035: packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1036: packet_put_int(rchan);
1037: packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
1038: packet_put_cstring("bla bla");
1039: packet_put_cstring("");
1040: packet_send();
1041: }
1042: xfree(ctype);
1043: }
1044:
1.20 markus 1045: void
1.16 markus 1046: client_init_dispatch_20()
1047: {
1048: dispatch_init(&dispatch_protocol_error);
1049: dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1050: dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1051: dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1052: dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
1.22 markus 1053: dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
1.16 markus 1054: dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1055: dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1056: dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
1057: dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
1058: }
1.20 markus 1059: void
1.15 markus 1060: client_init_dispatch_13()
1061: {
1062: dispatch_init(NULL);
1063: dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1064: dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1065: dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
1066: dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1067: dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1068: dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
1069: dispatch_set(SSH_SMSG_AGENT_OPEN, &auth_input_open_request);
1070: dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
1071: dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
1072: dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
1073: dispatch_set(SSH_SMSG_X11_OPEN, &x11_input_open);
1074: }
1.20 markus 1075: void
1.15 markus 1076: client_init_dispatch_15()
1077: {
1078: client_init_dispatch_13();
1079: dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1080: dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
1081: }
1.20 markus 1082: void
1.15 markus 1083: client_init_dispatch()
1084: {
1.16 markus 1085: if (compat20)
1086: client_init_dispatch_20();
1087: else if (compat13)
1.15 markus 1088: client_init_dispatch_13();
1089: else
1090: client_init_dispatch_15();
1.16 markus 1091: }
1092:
1093: void
1094: client_input_channel_req(int id, void *arg)
1095: {
1096: Channel *c = NULL;
1097: unsigned int len;
1098: int success = 0;
1099: int reply;
1100: char *rtype;
1101:
1102: rtype = packet_get_string(&len);
1103: reply = packet_get_char();
1104:
1.23 markus 1105: debug("client_input_channel_req: rtype %s reply %d", rtype, reply);
1.16 markus 1106:
1107: c = channel_lookup(id);
1108: if (c == NULL)
1109: fatal("session_input_channel_req: channel %d: bad channel", id);
1110:
1111: if (session_ident == -1) {
1112: error("client_input_channel_req: no channel %d", id);
1113: } else if (id != session_ident) {
1114: error("client_input_channel_req: bad channel %d != %d",
1115: id, session_ident);
1116: } else if (strcmp(rtype, "exit-status") == 0) {
1117: success = 1;
1118: exit_status = packet_get_int();
1.19 markus 1119: packet_done();
1.16 markus 1120: }
1121: if (reply) {
1122: packet_start(success ?
1123: SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1124: packet_put_int(c->remote_id);
1125: packet_send();
1126: }
1127: xfree(rtype);
1128: }
1129:
1130: void
1131: client_set_session_ident(int id)
1132: {
1133: debug("client_set_session_ident: id %d", id);
1134: session_ident = id;
1135: channel_register_callback(id, SSH2_MSG_CHANNEL_REQUEST,
1136: client_input_channel_req, (void *)0);
1.1 deraadt 1137: }