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