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