Annotation of src/usr.bin/ssh/clientloop.c, Revision 1.237
1.237 ! markus 1: /* $OpenBSD: clientloop.c,v 1.236 2011/06/22 22:08:42 djm Exp $ */
1.1 deraadt 2: /*
1.12 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
1.33 deraadt 6: * The main loop for the interactive session (client side).
1.20 markus 7: *
1.33 deraadt 8: * As far as I am concerned, the code I have written for this software
9: * can be used freely for any purpose. Any derived versions of this
10: * software must be clearly marked as such, and if the derived work is
11: * incompatible with the protocol description in the RFC file, it must be
12: * called by a name other than "ssh" or "Secure Shell".
13: *
14: *
15: * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16: *
17: * Redistribution and use in source and binary forms, with or without
18: * modification, are permitted provided that the following conditions
19: * are met:
20: * 1. Redistributions of source code must retain the above copyright
21: * notice, this list of conditions and the following disclaimer.
22: * 2. Redistributions in binary form must reproduce the above copyright
23: * notice, this list of conditions and the following disclaimer in the
24: * documentation and/or other materials provided with the distribution.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.20 markus 36: *
37: *
1.33 deraadt 38: * SSH2 support added by Markus Friedl.
1.78 markus 39: * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
1.20 markus 40: *
1.33 deraadt 41: * Redistribution and use in source and binary forms, with or without
42: * modification, are permitted provided that the following conditions
43: * are met:
44: * 1. Redistributions of source code must retain the above copyright
45: * notice, this list of conditions and the following disclaimer.
46: * 2. Redistributions in binary form must reproduce the above copyright
47: * notice, this list of conditions and the following disclaimer in the
48: * documentation and/or other materials provided with the distribution.
49: *
50: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.12 deraadt 60: */
1.1 deraadt 61:
1.152 stevesk 62:
1.175 deraadt 63: #include <sys/types.h>
1.152 stevesk 64: #include <sys/ioctl.h>
1.154 stevesk 65: #include <sys/stat.h>
1.166 stevesk 66: #include <sys/socket.h>
1.171 stevesk 67: #include <sys/time.h>
1.172 stevesk 68: #include <sys/param.h>
1.189 djm 69: #include <sys/queue.h>
1.150 stevesk 70:
1.155 stevesk 71: #include <ctype.h>
1.168 stevesk 72: #include <errno.h>
1.151 stevesk 73: #include <paths.h>
1.153 stevesk 74: #include <signal.h>
1.174 stevesk 75: #include <stdio.h>
1.173 stevesk 76: #include <stdlib.h>
1.170 stevesk 77: #include <string.h>
1.150 stevesk 78: #include <termios.h>
1.175 deraadt 79: #include <pwd.h>
1.169 stevesk 80: #include <unistd.h>
1.1 deraadt 81:
1.175 deraadt 82: #include "xmalloc.h"
1.45 markus 83: #include "ssh.h"
84: #include "ssh1.h"
85: #include "ssh2.h"
1.1 deraadt 86: #include "packet.h"
87: #include "buffer.h"
1.15 markus 88: #include "compat.h"
1.74 markus 89: #include "channels.h"
1.15 markus 90: #include "dispatch.h"
1.40 markus 91: #include "key.h"
1.175 deraadt 92: #include "cipher.h"
1.54 markus 93: #include "kex.h"
1.45 markus 94: #include "log.h"
95: #include "readconf.h"
96: #include "clientloop.h"
1.147 djm 97: #include "sshconnect.h"
1.40 markus 98: #include "authfd.h"
1.45 markus 99: #include "atomicio.h"
1.118 deraadt 100: #include "sshpty.h"
1.63 markus 101: #include "misc.h"
1.123 djm 102: #include "match.h"
103: #include "msg.h"
1.212 andreas 104: #include "roaming.h"
1.39 markus 105:
106: /* import options */
107: extern Options options;
108:
1.1 deraadt 109: /* Flag indicating that stdin should be redirected from /dev/null. */
110: extern int stdin_null_flag;
111:
1.116 dtucker 112: /* Flag indicating that no shell has been requested */
113: extern int no_shell_flag;
114:
1.123 djm 115: /* Control socket */
1.217 djm 116: extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
1.123 djm 117:
1.13 markus 118: /*
119: * Name of the host we are connecting to. This is the name given on the
120: * command line, or the HostName specified for the user-supplied name in a
121: * configuration file.
122: */
1.1 deraadt 123: extern char *host;
124:
1.13 markus 125: /*
126: * Flag to indicate that we have received a window change signal which has
127: * not yet been processed. This will cause a message indicating the new
128: * window size to be sent to the server a little later. This is volatile
129: * because this is updated in a signal handler.
130: */
1.88 markus 131: static volatile sig_atomic_t received_window_change_signal = 0;
132: static volatile sig_atomic_t received_signal = 0;
1.1 deraadt 133:
1.145 djm 134: /* Flag indicating whether the user's terminal is in non-blocking mode. */
1.1 deraadt 135: static int in_non_blocking_mode = 0;
136:
1.222 djm 137: /* Time when backgrounded control master using ControlPersist should exit */
138: static time_t control_persist_exit_time = 0;
139:
1.1 deraadt 140: /* Common data for the client loop code. */
1.217 djm 141: volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
1.195 djm 142: static int escape_char1; /* Escape character. (proto1 only) */
143: static int escape_pending1; /* Last character was an escape (proto1 only) */
1.11 markus 144: static int last_was_cr; /* Last character was a newline. */
1.197 djm 145: static int exit_status; /* Used to store the command exit status. */
146: static int stdin_eof; /* EOF has been encountered on stderr. */
1.11 markus 147: static Buffer stdin_buffer; /* Buffer for stdin data. */
148: static Buffer stdout_buffer; /* Buffer for stdout data. */
149: static Buffer stderr_buffer; /* Buffer for stderr data. */
1.221 djm 150: static u_int buffer_high; /* Soft max buffer size. */
1.11 markus 151: static int connection_in; /* Connection to server (input). */
152: static int connection_out; /* Connection to server (output). */
1.56 markus 153: static int need_rekeying; /* Set to non-zero if rekeying is requested. */
1.221 djm 154: static int session_closed; /* In SSH2: login session closed. */
155: static int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
1.1 deraadt 156:
1.77 itojun 157: static void client_init_dispatch(void);
1.16 markus 158: int session_ident = -1;
159:
1.214 andreas 160: int session_resumed = 0;
161:
1.195 djm 162: /* Track escape per proto2 channel */
163: struct escape_filter_ctx {
164: int escape_pending;
165: int escape_char;
166: };
167:
168: /* Context for channel confirmation replies */
1.190 djm 169: struct channel_reply_ctx {
170: const char *request_type;
1.234 djm 171: int id;
172: enum confirm_action action;
1.190 djm 173: };
174:
1.196 djm 175: /* Global request success/failure callbacks */
176: struct global_confirm {
177: TAILQ_ENTRY(global_confirm) entry;
178: global_confirm_cb *cb;
179: void *ctx;
180: int ref_count;
181: };
182: TAILQ_HEAD(global_confirms, global_confirm);
183: static struct global_confirms global_confirms =
184: TAILQ_HEAD_INITIALIZER(global_confirms);
185:
1.54 markus 186: /*XXX*/
187: extern Kex *xxx_kex;
188:
1.123 djm 189: void ssh_process_session2_setup(int, int, int, Buffer *);
190:
1.1 deraadt 191: /* Restores stdin to blocking mode. */
192:
1.77 itojun 193: static void
1.49 itojun 194: leave_non_blocking(void)
1.1 deraadt 195: {
1.11 markus 196: if (in_non_blocking_mode) {
1.125 djm 197: unset_nonblock(fileno(stdin));
1.11 markus 198: in_non_blocking_mode = 0;
199: }
1.1 deraadt 200: }
201:
1.11 markus 202: /* Puts stdin terminal in non-blocking mode. */
203:
1.77 itojun 204: static void
1.49 itojun 205: enter_non_blocking(void)
1.1 deraadt 206: {
1.11 markus 207: in_non_blocking_mode = 1;
1.124 djm 208: set_nonblock(fileno(stdin));
1.1 deraadt 209: }
210:
1.13 markus 211: /*
212: * Signal handler for the window change signal (SIGWINCH). This just sets a
213: * flag indicating that the window has changed.
214: */
1.157 deraadt 215: /*ARGSUSED */
1.77 itojun 216: static void
1.11 markus 217: window_change_handler(int sig)
1.1 deraadt 218: {
1.11 markus 219: received_window_change_signal = 1;
220: signal(SIGWINCH, window_change_handler);
1.1 deraadt 221: }
222:
1.13 markus 223: /*
224: * Signal handler for signals that cause the program to terminate. These
225: * signals must be trapped to restore terminal modes.
226: */
1.157 deraadt 227: /*ARGSUSED */
1.77 itojun 228: static void
1.11 markus 229: signal_handler(int sig)
1.1 deraadt 230: {
1.75 markus 231: received_signal = sig;
232: quit_pending = 1;
1.1 deraadt 233: }
234:
1.13 markus 235: /*
236: * Returns current time in seconds from Jan 1, 1970 with the maximum
237: * available resolution.
238: */
1.1 deraadt 239:
1.77 itojun 240: static double
1.49 itojun 241: get_current_time(void)
1.1 deraadt 242: {
1.11 markus 243: struct timeval tv;
244: gettimeofday(&tv, NULL);
245: return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
1.138 djm 246: }
247:
1.222 djm 248: /*
249: * Sets control_persist_exit_time to the absolute time when the
250: * backgrounded control master should exit due to expiry of the
251: * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
252: * control master process, or if there is no ControlPersist timeout.
253: */
254: static void
255: set_control_persist_exit_time(void)
256: {
257: if (muxserver_sock == -1 || !options.control_persist
1.232 djm 258: || options.control_persist_timeout == 0) {
1.222 djm 259: /* not using a ControlPersist timeout */
260: control_persist_exit_time = 0;
1.232 djm 261: } else if (channel_still_open()) {
1.222 djm 262: /* some client connections are still open */
263: if (control_persist_exit_time > 0)
264: debug2("%s: cancel scheduled exit", __func__);
265: control_persist_exit_time = 0;
266: } else if (control_persist_exit_time <= 0) {
267: /* a client connection has recently closed */
268: control_persist_exit_time = time(NULL) +
269: (time_t)options.control_persist_timeout;
270: debug2("%s: schedule exit in %d seconds", __func__,
271: options.control_persist_timeout);
272: }
273: /* else we are already counting down to the timeout */
274: }
275:
1.138 djm 276: #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
277: void
278: client_x11_get_proto(const char *display, const char *xauth_path,
1.221 djm 279: u_int trusted, u_int timeout, char **_proto, char **_data)
1.138 djm 280: {
281: char cmd[1024];
282: char line[512];
283: char xdisplay[512];
284: static char proto[512], data[512];
285: FILE *f;
286: int got_data = 0, generated = 0, do_unlink = 0, i;
287: char *xauthdir, *xauthfile;
288: struct stat st;
1.221 djm 289: u_int now;
1.138 djm 290:
291: xauthdir = xauthfile = NULL;
292: *_proto = proto;
293: *_data = data;
294: proto[0] = data[0] = '\0';
295:
296: if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
297: debug("No xauth program.");
298: } else {
299: if (display == NULL) {
300: debug("x11_get_proto: DISPLAY not set");
301: return;
302: }
303: /*
304: * Handle FamilyLocal case where $DISPLAY does
305: * not match an authorization entry. For this we
306: * just try "xauth list unix:displaynum.screennum".
307: * XXX: "localhost" match to determine FamilyLocal
308: * is not perfect.
309: */
310: if (strncmp(display, "localhost:", 10) == 0) {
311: snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
312: display + 10);
313: display = xdisplay;
314: }
315: if (trusted == 0) {
316: xauthdir = xmalloc(MAXPATHLEN);
317: xauthfile = xmalloc(MAXPATHLEN);
1.225 djm 318: mktemp_proto(xauthdir, MAXPATHLEN);
1.138 djm 319: if (mkdtemp(xauthdir) != NULL) {
320: do_unlink = 1;
321: snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
322: xauthdir);
323: snprintf(cmd, sizeof(cmd),
324: "%s -f %s generate %s " SSH_X11_PROTO
1.221 djm 325: " untrusted timeout %u 2>" _PATH_DEVNULL,
326: xauth_path, xauthfile, display, timeout);
1.138 djm 327: debug2("x11_get_proto: %s", cmd);
328: if (system(cmd) == 0)
329: generated = 1;
1.221 djm 330: if (x11_refuse_time == 0) {
331: now = time(NULL) + 1;
332: if (UINT_MAX - timeout < now)
333: x11_refuse_time = UINT_MAX;
334: else
335: x11_refuse_time = now + timeout;
336: }
1.138 djm 337: }
338: }
1.181 markus 339:
340: /*
341: * When in untrusted mode, we read the cookie only if it was
342: * successfully generated as an untrusted one in the step
343: * above.
344: */
345: if (trusted || generated) {
346: snprintf(cmd, sizeof(cmd),
347: "%s %s%s list %s 2>" _PATH_DEVNULL,
348: xauth_path,
349: generated ? "-f " : "" ,
350: generated ? xauthfile : "",
351: display);
352: debug2("x11_get_proto: %s", cmd);
353: f = popen(cmd, "r");
354: if (f && fgets(line, sizeof(line), f) &&
355: sscanf(line, "%*s %511s %511s", proto, data) == 2)
356: got_data = 1;
357: if (f)
358: pclose(f);
359: } else
360: error("Warning: untrusted X11 forwarding setup failed: "
361: "xauth key data not generated");
1.138 djm 362: }
363:
364: if (do_unlink) {
365: unlink(xauthfile);
366: rmdir(xauthdir);
367: }
368: if (xauthdir)
369: xfree(xauthdir);
370: if (xauthfile)
371: xfree(xauthfile);
372:
373: /*
374: * If we didn't get authentication data, just make up some
375: * data. The forwarding code will check the validity of the
376: * response anyway, and substitute this data. The X11
377: * server, however, will ignore this fake data and use
378: * whatever authentication mechanisms it was using otherwise
379: * for the local connection.
380: */
381: if (!got_data) {
382: u_int32_t rnd = 0;
383:
384: logit("Warning: No xauth data; "
385: "using fake authentication data for X11 forwarding.");
386: strlcpy(proto, SSH_X11_PROTO, sizeof proto);
387: for (i = 0; i < 16; i++) {
388: if (i % 4 == 0)
389: rnd = arc4random();
390: snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
391: rnd & 0xff);
392: rnd >>= 8;
393: }
394: }
1.1 deraadt 395: }
396:
1.13 markus 397: /*
398: * This is called when the interactive is entered. This checks if there is
399: * an EOF coming on stdin. We must check this explicitly, as select() does
400: * not appear to wake up when redirecting from /dev/null.
401: */
1.1 deraadt 402:
1.77 itojun 403: static void
1.49 itojun 404: client_check_initial_eof_on_stdin(void)
1.1 deraadt 405: {
1.11 markus 406: int len;
407: char buf[1];
1.1 deraadt 408:
1.13 markus 409: /*
410: * If standard input is to be "redirected from /dev/null", we simply
411: * mark that we have seen an EOF and send an EOF message to the
412: * server. Otherwise, we try to read a single character; it appears
413: * that for some files, such /dev/null, select() never wakes up for
414: * read for this descriptor, which means that we never get EOF. This
415: * way we will get the EOF if stdin comes from /dev/null or similar.
416: */
1.11 markus 417: if (stdin_null_flag) {
418: /* Fake EOF on stdin. */
419: debug("Sending eof.");
420: stdin_eof = 1;
421: packet_start(SSH_CMSG_EOF);
422: packet_send();
423: } else {
424: enter_non_blocking();
425:
426: /* Check for immediate EOF on stdin. */
427: len = read(fileno(stdin), buf, 1);
428: if (len == 0) {
1.197 djm 429: /*
430: * EOF. Record that we have seen it and send
431: * EOF to server.
432: */
1.11 markus 433: debug("Sending eof.");
434: stdin_eof = 1;
435: packet_start(SSH_CMSG_EOF);
436: packet_send();
437: } else if (len > 0) {
1.13 markus 438: /*
439: * Got data. We must store the data in the buffer,
440: * and also process it as an escape character if
441: * appropriate.
442: */
1.195 djm 443: if ((u_char) buf[0] == escape_char1)
444: escape_pending1 = 1;
1.52 markus 445: else
1.11 markus 446: buffer_append(&stdin_buffer, buf, 1);
447: }
448: leave_non_blocking();
449: }
1.1 deraadt 450: }
451:
452:
1.13 markus 453: /*
454: * Make packets from buffered stdin data, and buffer them for sending to the
455: * connection.
456: */
1.1 deraadt 457:
1.77 itojun 458: static void
1.49 itojun 459: client_make_packets_from_stdin_data(void)
1.1 deraadt 460: {
1.42 markus 461: u_int len;
1.1 deraadt 462:
1.11 markus 463: /* Send buffered stdin data to the server. */
464: while (buffer_len(&stdin_buffer) > 0 &&
1.90 deraadt 465: packet_not_very_much_data_to_write()) {
1.11 markus 466: len = buffer_len(&stdin_buffer);
467: /* Keep the packets at reasonable size. */
468: if (len > packet_get_maxsize())
469: len = packet_get_maxsize();
470: packet_start(SSH_CMSG_STDIN_DATA);
471: packet_put_string(buffer_ptr(&stdin_buffer), len);
472: packet_send();
473: buffer_consume(&stdin_buffer, len);
474: /* If we have a pending EOF, send it now. */
475: if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
476: packet_start(SSH_CMSG_EOF);
477: packet_send();
478: }
1.1 deraadt 479: }
480: }
481:
1.13 markus 482: /*
483: * Checks if the client window has changed, and sends a packet about it to
484: * the server if so. The actual change is detected elsewhere (by a software
485: * interrupt on Unix); this just checks the flag and sends a message if
486: * appropriate.
487: */
1.1 deraadt 488:
1.77 itojun 489: static void
1.49 itojun 490: client_check_window_change(void)
1.1 deraadt 491: {
1.16 markus 492: struct winsize ws;
493:
494: if (! received_window_change_signal)
495: return;
496: /** XXX race */
497: received_window_change_signal = 0;
498:
1.37 markus 499: debug2("client_check_window_change: changed");
1.16 markus 500:
501: if (compat20) {
1.123 djm 502: channel_send_window_changes();
1.16 markus 503: } else {
1.123 djm 504: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
505: return;
1.16 markus 506: packet_start(SSH_CMSG_WINDOW_SIZE);
1.158 deraadt 507: packet_put_int((u_int)ws.ws_row);
508: packet_put_int((u_int)ws.ws_col);
509: packet_put_int((u_int)ws.ws_xpixel);
510: packet_put_int((u_int)ws.ws_ypixel);
1.16 markus 511: packet_send();
1.1 deraadt 512: }
513: }
514:
1.117 markus 515: static void
516: client_global_request_reply(int type, u_int32_t seq, void *ctxt)
517: {
1.196 djm 518: struct global_confirm *gc;
519:
520: if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
521: return;
522: if (gc->cb != NULL)
523: gc->cb(type, seq, gc->ctx);
524: if (--gc->ref_count <= 0) {
525: TAILQ_REMOVE(&global_confirms, gc, entry);
526: bzero(gc, sizeof(*gc));
527: xfree(gc);
528: }
529:
1.210 andreas 530: packet_set_alive_timeouts(0);
1.117 markus 531: }
532:
533: static void
534: server_alive_check(void)
535: {
1.210 andreas 536: if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
1.228 djm 537: logit("Timeout, server %s not responding.", host);
1.176 markus 538: cleanup_exit(255);
539: }
1.117 markus 540: packet_start(SSH2_MSG_GLOBAL_REQUEST);
541: packet_put_cstring("keepalive@openssh.com");
542: packet_put_char(1); /* boolean: want reply */
543: packet_send();
1.196 djm 544: /* Insert an empty placeholder to maintain ordering */
545: client_register_global_confirm(NULL, NULL);
1.117 markus 546: }
547:
1.13 markus 548: /*
549: * Waits until the client can do something (some data becomes available on
550: * one of the file descriptors).
551: */
1.77 itojun 552: static void
1.46 markus 553: client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
1.130 avsm 554: int *maxfdp, u_int *nallocp, int rekeying)
1.11 markus 555: {
1.117 markus 556: struct timeval tv, *tvp;
1.222 djm 557: int timeout_secs;
1.117 markus 558: int ret;
559:
1.46 markus 560: /* Add any selections by the channel mechanism. */
1.81 markus 561: channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
1.11 markus 562:
1.16 markus 563: if (!compat20) {
1.17 markus 564: /* Read from the connection, unless our buffers are full. */
1.16 markus 565: if (buffer_len(&stdout_buffer) < buffer_high &&
566: buffer_len(&stderr_buffer) < buffer_high &&
567: channel_not_very_much_buffered_data())
1.46 markus 568: FD_SET(connection_in, *readsetp);
1.17 markus 569: /*
570: * Read from stdin, unless we have seen EOF or have very much
571: * buffered data to send to the server.
572: */
573: if (!stdin_eof && packet_not_very_much_data_to_write())
1.46 markus 574: FD_SET(fileno(stdin), *readsetp);
1.17 markus 575:
576: /* Select stdout/stderr if have data in buffer. */
577: if (buffer_len(&stdout_buffer) > 0)
1.46 markus 578: FD_SET(fileno(stdout), *writesetp);
1.17 markus 579: if (buffer_len(&stderr_buffer) > 0)
1.46 markus 580: FD_SET(fileno(stderr), *writesetp);
1.16 markus 581: } else {
1.71 markus 582: /* channel_prepare_select could have closed the last channel */
1.84 markus 583: if (session_closed && !channel_still_open() &&
584: !packet_have_data_to_write()) {
585: /* clear mask since we did not call select() */
1.87 markus 586: memset(*readsetp, 0, *nallocp);
587: memset(*writesetp, 0, *nallocp);
1.84 markus 588: return;
1.71 markus 589: } else {
590: FD_SET(connection_in, *readsetp);
591: }
1.16 markus 592: }
1.11 markus 593:
594: /* Select server connection if have data to write to the server. */
595: if (packet_have_data_to_write())
1.46 markus 596: FD_SET(connection_out, *writesetp);
1.11 markus 597:
1.13 markus 598: /*
599: * Wait for something to happen. This will suspend the process until
600: * some selected descriptor can be read, written, or has some other
1.222 djm 601: * event pending, or a timeout expires.
1.13 markus 602: */
1.11 markus 603:
1.222 djm 604: timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
605: if (options.server_alive_interval > 0 && compat20)
606: timeout_secs = options.server_alive_interval;
607: set_control_persist_exit_time();
608: if (control_persist_exit_time > 0) {
609: timeout_secs = MIN(timeout_secs,
610: control_persist_exit_time - time(NULL));
611: if (timeout_secs < 0)
612: timeout_secs = 0;
613: }
614: if (timeout_secs == INT_MAX)
1.117 markus 615: tvp = NULL;
1.129 deraadt 616: else {
1.222 djm 617: tv.tv_sec = timeout_secs;
1.117 markus 618: tv.tv_usec = 0;
619: tvp = &tv;
620: }
1.222 djm 621:
1.117 markus 622: ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
623: if (ret < 0) {
1.11 markus 624: char buf[100];
1.51 markus 625:
626: /*
627: * We have to clear the select masks, because we return.
628: * We have to return, because the mainloop checks for the flags
629: * set by the signal handlers.
630: */
1.87 markus 631: memset(*readsetp, 0, *nallocp);
632: memset(*writesetp, 0, *nallocp);
1.50 deraadt 633:
1.11 markus 634: if (errno == EINTR)
635: return;
636: /* Note: we might still have data in the buffers. */
637: snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
638: buffer_append(&stderr_buffer, buf, strlen(buf));
639: quit_pending = 1;
1.117 markus 640: } else if (ret == 0)
641: server_alive_check();
1.11 markus 642: }
643:
1.77 itojun 644: static void
1.31 markus 645: client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
1.1 deraadt 646: {
1.11 markus 647: /* Flush stdout and stderr buffers. */
1.31 markus 648: if (buffer_len(bout) > 0)
1.197 djm 649: atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
650: buffer_len(bout));
1.31 markus 651: if (buffer_len(berr) > 0)
1.197 djm 652: atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
653: buffer_len(berr));
1.11 markus 654:
1.233 djm 655: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.11 markus 656:
1.13 markus 657: /*
658: * Free (and clear) the buffer to reduce the amount of data that gets
659: * written to swap.
660: */
1.31 markus 661: buffer_free(bin);
662: buffer_free(bout);
663: buffer_free(berr);
1.11 markus 664:
665: /* Send the suspend signal to the program itself. */
666: kill(getpid(), SIGTSTP);
667:
1.132 djm 668: /* Reset window sizes in case they have changed */
669: received_window_change_signal = 1;
1.11 markus 670:
671: /* OK, we have been continued by the user. Reinitialize buffers. */
1.31 markus 672: buffer_init(bin);
673: buffer_init(bout);
674: buffer_init(berr);
1.11 markus 675:
1.233 djm 676: enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.11 markus 677: }
678:
1.77 itojun 679: static void
1.161 deraadt 680: client_process_net_input(fd_set *readset)
1.11 markus 681: {
1.212 andreas 682: int len, cont = 0;
1.17 markus 683: char buf[8192];
1.11 markus 684:
1.13 markus 685: /*
686: * Read input from the server, and add any such data to the buffer of
687: * the packet subsystem.
688: */
1.11 markus 689: if (FD_ISSET(connection_in, readset)) {
690: /* Read as much as possible. */
1.212 andreas 691: len = roaming_read(connection_in, buf, sizeof(buf), &cont);
692: if (len == 0 && cont == 0) {
1.197 djm 693: /*
694: * Received EOF. The remote host has closed the
695: * connection.
696: */
697: snprintf(buf, sizeof buf,
698: "Connection to %.300s closed by remote host.\r\n",
699: host);
1.1 deraadt 700: buffer_append(&stderr_buffer, buf, strlen(buf));
701: quit_pending = 1;
702: return;
1.11 markus 703: }
1.13 markus 704: /*
705: * There is a kernel bug on Solaris that causes select to
706: * sometimes wake up even though there is no data available.
707: */
1.53 millert 708: if (len < 0 && (errno == EAGAIN || errno == EINTR))
1.11 markus 709: len = 0;
710:
711: if (len < 0) {
1.197 djm 712: /*
713: * An error has encountered. Perhaps there is a
714: * network problem.
715: */
716: snprintf(buf, sizeof buf,
717: "Read from remote host %.300s: %.100s\r\n",
718: host, strerror(errno));
1.11 markus 719: buffer_append(&stderr_buffer, buf, strlen(buf));
720: quit_pending = 1;
721: return;
722: }
723: packet_process_incoming(buf, len);
724: }
1.17 markus 725: }
1.16 markus 726:
1.97 jakob 727: static void
1.190 djm 728: client_status_confirm(int type, Channel *c, void *ctx)
1.123 djm 729: {
1.190 djm 730: struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
731: char errmsg[256];
732: int tochan;
733:
1.234 djm 734: /*
735: * If a TTY was explicitly requested, then a failure to allocate
736: * one is fatal.
737: */
738: if (cr->action == CONFIRM_TTY &&
739: (options.request_tty == REQUEST_TTY_FORCE ||
740: options.request_tty == REQUEST_TTY_YES))
741: cr->action = CONFIRM_CLOSE;
742:
1.190 djm 743: /* XXX supress on mux _client_ quietmode */
744: tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
1.217 djm 745: c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
1.190 djm 746:
747: if (type == SSH2_MSG_CHANNEL_SUCCESS) {
748: debug2("%s request accepted on channel %d",
749: cr->request_type, c->self);
750: } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
751: if (tochan) {
752: snprintf(errmsg, sizeof(errmsg),
753: "%s request failed\r\n", cr->request_type);
754: } else {
755: snprintf(errmsg, sizeof(errmsg),
756: "%s request failed on channel %d",
757: cr->request_type, c->self);
758: }
759: /* If error occurred on primary session channel, then exit */
1.234 djm 760: if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
1.190 djm 761: fatal("%s", errmsg);
1.234 djm 762: /*
763: * If error occurred on mux client, append to
764: * their stderr.
765: */
766: if (tochan) {
767: buffer_append(&c->extended, errmsg,
768: strlen(errmsg));
769: } else
1.190 djm 770: error("%s", errmsg);
1.234 djm 771: if (cr->action == CONFIRM_TTY) {
772: /*
773: * If a TTY allocation error occurred, then arrange
774: * for the correct TTY to leave raw mode.
775: */
776: if (c->self == session_ident)
777: leave_raw_mode(0);
778: else
779: mux_tty_alloc_failed(c);
780: } else if (cr->action == CONFIRM_CLOSE) {
1.190 djm 781: chan_read_failed(c);
782: chan_write_failed(c);
783: }
784: }
785: xfree(cr);
786: }
787:
788: static void
789: client_abandon_status_confirm(Channel *c, void *ctx)
790: {
791: xfree(ctx);
792: }
1.129 deraadt 793:
1.236 djm 794: void
1.234 djm 795: client_expect_confirm(int id, const char *request,
796: enum confirm_action action)
1.190 djm 797: {
798: struct channel_reply_ctx *cr = xmalloc(sizeof(*cr));
1.123 djm 799:
1.190 djm 800: cr->request_type = request;
1.234 djm 801: cr->action = action;
1.123 djm 802:
1.190 djm 803: channel_register_status_confirm(id, client_status_confirm,
804: client_abandon_status_confirm, cr);
1.196 djm 805: }
806:
807: void
808: client_register_global_confirm(global_confirm_cb *cb, void *ctx)
809: {
1.201 djm 810: struct global_confirm *gc, *last_gc;
1.196 djm 811:
812: /* Coalesce identical callbacks */
1.201 djm 813: last_gc = TAILQ_LAST(&global_confirms, global_confirms);
814: if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
815: if (++last_gc->ref_count >= INT_MAX)
816: fatal("%s: last_gc->ref_count = %d",
817: __func__, last_gc->ref_count);
1.196 djm 818: return;
819: }
820:
821: gc = xmalloc(sizeof(*gc));
822: gc->cb = cb;
823: gc->ctx = ctx;
824: gc->ref_count = 1;
825: TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
1.123 djm 826: }
827:
828: static void
1.99 markus 829: process_cmdline(void)
1.97 jakob 830: {
831: void (*handler)(int);
1.135 djm 832: char *s, *cmd, *cancel_host;
1.237 ! markus 833: int delete = 0, local = 0, remote = 0, dynamic = 0;
! 834: int cancel_port, ok;
1.135 djm 835: Forward fwd;
1.97 jakob 836:
1.183 djm 837: bzero(&fwd, sizeof(fwd));
838: fwd.listen_host = fwd.connect_host = NULL;
839:
1.233 djm 840: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.101 markus 841: handler = signal(SIGINT, SIG_IGN);
1.99 markus 842: cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
1.97 jakob 843: if (s == NULL)
844: goto out;
1.179 tedu 845: while (isspace(*s))
1.97 jakob 846: s++;
1.121 djm 847: if (*s == '-')
848: s++; /* Skip cmdline '-', if any */
1.120 dtucker 849: if (*s == '\0')
1.97 jakob 850: goto out;
1.121 djm 851:
1.122 djm 852: if (*s == 'h' || *s == 'H' || *s == '?') {
1.121 djm 853: logit("Commands:");
1.164 djm 854: logit(" -L[bind_address:]port:host:hostport "
855: "Request local forward");
856: logit(" -R[bind_address:]port:host:hostport "
857: "Request remote forward");
1.204 stevesk 858: logit(" -D[bind_address:]port "
859: "Request dynamic forward");
1.237 ! markus 860: logit(" -KL[bind_address:]port "
! 861: "Cancel local forward");
1.165 stevesk 862: logit(" -KR[bind_address:]port "
1.164 djm 863: "Cancel remote forward");
1.237 ! markus 864: logit(" -KD[bind_address:]port "
! 865: "Cancel dynamic forward");
1.146 reyk 866: if (!options.permit_local_command)
867: goto out;
1.164 djm 868: logit(" !args "
869: "Execute local command");
1.146 reyk 870: goto out;
871: }
872:
873: if (*s == '!' && options.permit_local_command) {
874: s++;
875: ssh_local_cmd(s);
1.121 djm 876: goto out;
877: }
878:
879: if (*s == 'K') {
880: delete = 1;
881: s++;
882: }
1.204 stevesk 883: if (*s == 'L')
884: local = 1;
885: else if (*s == 'R')
886: remote = 1;
887: else if (*s == 'D')
888: dynamic = 1;
889: else {
1.109 itojun 890: logit("Invalid command.");
1.97 jakob 891: goto out;
892: }
1.204 stevesk 893:
1.237 ! markus 894: if (delete && !compat20) {
1.109 itojun 895: logit("Not supported for SSH protocol version 1.");
1.97 jakob 896: goto out;
897: }
1.121 djm 898:
1.179 tedu 899: while (isspace(*++s))
900: ;
1.97 jakob 901:
1.217 djm 902: /* XXX update list of forwards in options */
1.121 djm 903: if (delete) {
1.135 djm 904: cancel_port = 0;
905: cancel_host = hpdelim(&s); /* may be NULL */
906: if (s != NULL) {
907: cancel_port = a2port(s);
908: cancel_host = cleanhostname(cancel_host);
909: } else {
910: cancel_port = a2port(cancel_host);
911: cancel_host = NULL;
1.121 djm 912: }
1.208 djm 913: if (cancel_port <= 0) {
1.135 djm 914: logit("Bad forwarding close port");
1.121 djm 915: goto out;
916: }
1.237 ! markus 917: if (remote)
! 918: ok = channel_request_rforward_cancel(cancel_host,
! 919: cancel_port) == 0;
! 920: else if (dynamic)
! 921: ok = channel_cancel_lport_listener(cancel_host,
! 922: cancel_port, 0, options.gateway_ports) > 0;
! 923: else
! 924: ok = channel_cancel_lport_listener(cancel_host,
! 925: cancel_port, CHANNEL_CANCEL_PORT_STATIC,
! 926: options.gateway_ports) > 0;
! 927: if (!ok) {
! 928: logit("Unkown port forwarding.");
! 929: goto out;
! 930: }
! 931: logit("Canceled forwarding.");
1.121 djm 932: } else {
1.209 djm 933: if (!parse_forward(&fwd, s, dynamic, remote)) {
1.121 djm 934: logit("Bad forwarding specification.");
935: goto out;
936: }
1.204 stevesk 937: if (local || dynamic) {
1.135 djm 938: if (channel_setup_local_fwd_listener(fwd.listen_host,
939: fwd.listen_port, fwd.connect_host,
940: fwd.connect_port, options.gateway_ports) < 0) {
1.121 djm 941: logit("Port forwarding failed.");
942: goto out;
943: }
1.135 djm 944: } else {
1.167 markus 945: if (channel_request_remote_forwarding(fwd.listen_host,
1.135 djm 946: fwd.listen_port, fwd.connect_host,
1.167 markus 947: fwd.connect_port) < 0) {
948: logit("Port forwarding failed.");
949: goto out;
950: }
1.135 djm 951: }
1.121 djm 952: logit("Forwarding port.");
953: }
954:
1.97 jakob 955: out:
956: signal(SIGINT, handler);
1.233 djm 957: enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.97 jakob 958: if (cmd)
959: xfree(cmd);
1.183 djm 960: if (fwd.listen_host != NULL)
961: xfree(fwd.listen_host);
962: if (fwd.connect_host != NULL)
963: xfree(fwd.connect_host);
1.97 jakob 964: }
965:
1.195 djm 966: /*
967: * Process the characters one by one, call with c==NULL for proto1 case.
968: */
1.77 itojun 969: static int
1.195 djm 970: process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
971: char *buf, int len)
1.31 markus 972: {
1.32 markus 973: char string[1024];
1.31 markus 974: pid_t pid;
975: int bytes = 0;
1.42 markus 976: u_int i;
977: u_char ch;
1.31 markus 978: char *s;
1.195 djm 979: int *escape_pendingp, escape_char;
980: struct escape_filter_ctx *efc;
1.31 markus 981:
1.195 djm 982: if (c == NULL) {
983: escape_pendingp = &escape_pending1;
984: escape_char = escape_char1;
985: } else {
986: if (c->filter_ctx == NULL)
987: return 0;
988: efc = (struct escape_filter_ctx *)c->filter_ctx;
989: escape_pendingp = &efc->escape_pending;
990: escape_char = efc->escape_char;
991: }
992:
1.139 djm 993: if (len <= 0)
994: return (0);
995:
996: for (i = 0; i < (u_int)len; i++) {
1.31 markus 997: /* Get one character at a time. */
998: ch = buf[i];
999:
1.195 djm 1000: if (*escape_pendingp) {
1.31 markus 1001: /* We have previously seen an escape character. */
1002: /* Clear the flag now. */
1.195 djm 1003: *escape_pendingp = 0;
1.31 markus 1004:
1005: /* Process the escaped character. */
1006: switch (ch) {
1007: case '.':
1008: /* Terminate the connection. */
1.195 djm 1009: snprintf(string, sizeof string, "%c.\r\n",
1010: escape_char);
1.32 markus 1011: buffer_append(berr, string, strlen(string));
1.31 markus 1012:
1.217 djm 1013: if (c && c->ctl_chan != -1) {
1.195 djm 1014: chan_read_failed(c);
1015: chan_write_failed(c);
1016: return 0;
1017: } else
1018: quit_pending = 1;
1.31 markus 1019: return -1;
1020:
1021: case 'Z' - 64:
1.195 djm 1022: /* XXX support this for mux clients */
1.217 djm 1023: if (c && c->ctl_chan != -1) {
1.195 djm 1024: noescape:
1025: snprintf(string, sizeof string,
1026: "%c%c escape not available to "
1027: "multiplexed sessions\r\n",
1028: escape_char, ch);
1029: buffer_append(berr, string,
1030: strlen(string));
1031: continue;
1032: }
1.197 djm 1033: /* Suspend the program. Inform the user */
1.195 djm 1034: snprintf(string, sizeof string,
1035: "%c^Z [suspend ssh]\r\n", escape_char);
1.32 markus 1036: buffer_append(berr, string, strlen(string));
1.31 markus 1037:
1038: /* Restore terminal modes and suspend. */
1039: client_suspend_self(bin, bout, berr);
1040:
1041: /* We have been continued. */
1042: continue;
1043:
1.111 markus 1044: case 'B':
1045: if (compat20) {
1046: snprintf(string, sizeof string,
1047: "%cB\r\n", escape_char);
1048: buffer_append(berr, string,
1049: strlen(string));
1050: channel_request_start(session_ident,
1051: "break", 0);
1052: packet_put_int(1000);
1053: packet_send();
1054: }
1055: continue;
1056:
1.54 markus 1057: case 'R':
1.59 markus 1058: if (compat20) {
1059: if (datafellows & SSH_BUG_NOREKEY)
1.197 djm 1060: logit("Server does not "
1061: "support re-keying");
1.59 markus 1062: else
1063: need_rekeying = 1;
1064: }
1.54 markus 1065: continue;
1066:
1.31 markus 1067: case '&':
1.217 djm 1068: if (c && c->ctl_chan != -1)
1.195 djm 1069: goto noescape;
1.31 markus 1070: /*
1.197 djm 1071: * Detach the program (continue to serve
1072: * connections, but put in background and no
1073: * more new connections).
1.31 markus 1074: */
1075: /* Restore tty modes. */
1.233 djm 1076: leave_raw_mode(
1077: options.request_tty == REQUEST_TTY_FORCE);
1.31 markus 1078:
1079: /* Stop listening for new connections. */
1.86 markus 1080: channel_stop_listening();
1.31 markus 1081:
1.86 markus 1082: snprintf(string, sizeof string,
1083: "%c& [backgrounded]\n", escape_char);
1084: buffer_append(berr, string, strlen(string));
1.31 markus 1085:
1086: /* Fork into background. */
1087: pid = fork();
1088: if (pid < 0) {
1089: error("fork: %.100s", strerror(errno));
1090: continue;
1091: }
1092: if (pid != 0) { /* This is the parent. */
1093: /* The parent just exits. */
1094: exit(0);
1095: }
1096: /* The child continues serving connections. */
1.86 markus 1097: if (compat20) {
1098: buffer_append(bin, "\004", 1);
1099: /* fake EOF on stdin */
1100: return -1;
1101: } else if (!stdin_eof) {
1102: /*
1.197 djm 1103: * Sending SSH_CMSG_EOF alone does not
1104: * always appear to be enough. So we
1105: * try to send an EOF character first.
1.86 markus 1106: */
1107: packet_start(SSH_CMSG_STDIN_DATA);
1108: packet_put_string("\004", 1);
1109: packet_send();
1110: /* Close stdin. */
1111: stdin_eof = 1;
1112: if (buffer_len(bin) == 0) {
1113: packet_start(SSH_CMSG_EOF);
1114: packet_send();
1115: }
1116: }
1117: continue;
1.31 markus 1118:
1119: case '?':
1.217 djm 1120: if (c && c->ctl_chan != -1) {
1.195 djm 1121: snprintf(string, sizeof string,
1122: "%c?\r\n\
1123: Supported escape sequences:\r\n\
1124: %c. - terminate session\r\n\
1125: %cB - send a BREAK to the remote system\r\n\
1126: %cR - Request rekey (SSH protocol 2 only)\r\n\
1127: %c# - list forwarded connections\r\n\
1128: %c? - this message\r\n\
1129: %c%c - send the escape character by typing it twice\r\n\
1130: (Note that escapes are only recognized immediately after newline.)\r\n",
1131: escape_char, escape_char,
1132: escape_char, escape_char,
1133: escape_char, escape_char,
1.206 djm 1134: escape_char, escape_char);
1.195 djm 1135: } else {
1136: snprintf(string, sizeof string,
1.31 markus 1137: "%c?\r\n\
1138: Supported escape sequences:\r\n\
1.195 djm 1139: %c. - terminate connection (and any multiplexed sessions)\r\n\
1140: %cB - send a BREAK to the remote system\r\n\
1141: %cC - open a command line\r\n\
1142: %cR - Request rekey (SSH protocol 2 only)\r\n\
1143: %c^Z - suspend ssh\r\n\
1144: %c# - list forwarded connections\r\n\
1145: %c& - background ssh (when waiting for connections to terminate)\r\n\
1146: %c? - this message\r\n\
1147: %c%c - send the escape character by typing it twice\r\n\
1.31 markus 1148: (Note that escapes are only recognized immediately after newline.)\r\n",
1.195 djm 1149: escape_char, escape_char,
1150: escape_char, escape_char,
1151: escape_char, escape_char,
1152: escape_char, escape_char,
1153: escape_char, escape_char,
1154: escape_char);
1155: }
1.32 markus 1156: buffer_append(berr, string, strlen(string));
1.31 markus 1157: continue;
1158:
1159: case '#':
1.195 djm 1160: snprintf(string, sizeof string, "%c#\r\n",
1161: escape_char);
1.32 markus 1162: buffer_append(berr, string, strlen(string));
1.31 markus 1163: s = channel_open_message();
1164: buffer_append(berr, s, strlen(s));
1165: xfree(s);
1.97 jakob 1166: continue;
1167:
1168: case 'C':
1.217 djm 1169: if (c && c->ctl_chan != -1)
1.206 djm 1170: goto noescape;
1.99 markus 1171: process_cmdline();
1.31 markus 1172: continue;
1173:
1174: default:
1175: if (ch != escape_char) {
1176: buffer_put_char(bin, escape_char);
1177: bytes++;
1178: }
1179: /* Escaped characters fall through here */
1180: break;
1181: }
1182: } else {
1183: /*
1.197 djm 1184: * The previous character was not an escape char.
1185: * Check if this is an escape.
1.31 markus 1186: */
1187: if (last_was_cr && ch == escape_char) {
1.197 djm 1188: /*
1189: * It is. Set the flag and continue to
1190: * next character.
1191: */
1.195 djm 1192: *escape_pendingp = 1;
1.31 markus 1193: continue;
1194: }
1195: }
1196:
1197: /*
1198: * Normal character. Record whether it was a newline,
1199: * and append it to the buffer.
1200: */
1201: last_was_cr = (ch == '\r' || ch == '\n');
1202: buffer_put_char(bin, ch);
1203: bytes++;
1204: }
1205: return bytes;
1206: }
1207:
1.77 itojun 1208: static void
1.161 deraadt 1209: client_process_input(fd_set *readset)
1.17 markus 1210: {
1.21 deraadt 1211: int len;
1.31 markus 1212: char buf[8192];
1.16 markus 1213:
1.11 markus 1214: /* Read input from stdin. */
1215: if (FD_ISSET(fileno(stdin), readset)) {
1216: /* Read as much as possible. */
1217: len = read(fileno(stdin), buf, sizeof(buf));
1.64 markus 1218: if (len < 0 && (errno == EAGAIN || errno == EINTR))
1219: return; /* we'll try again later */
1.11 markus 1220: if (len <= 0) {
1.13 markus 1221: /*
1222: * Received EOF or error. They are treated
1223: * similarly, except that an error message is printed
1224: * if it was an error condition.
1225: */
1.11 markus 1226: if (len < 0) {
1.197 djm 1227: snprintf(buf, sizeof buf, "read: %.100s\r\n",
1228: strerror(errno));
1.11 markus 1229: buffer_append(&stderr_buffer, buf, strlen(buf));
1230: }
1231: /* Mark that we have seen EOF. */
1232: stdin_eof = 1;
1.13 markus 1233: /*
1234: * Send an EOF message to the server unless there is
1235: * data in the buffer. If there is data in the
1236: * buffer, no message will be sent now. Code
1237: * elsewhere will send the EOF when the buffer
1238: * becomes empty if stdin_eof is set.
1239: */
1.11 markus 1240: if (buffer_len(&stdin_buffer) == 0) {
1.1 deraadt 1241: packet_start(SSH_CMSG_EOF);
1242: packet_send();
1.11 markus 1243: }
1.195 djm 1244: } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
1.13 markus 1245: /*
1246: * Normal successful read, and no escape character.
1247: * Just append the data to buffer.
1248: */
1.11 markus 1249: buffer_append(&stdin_buffer, buf, len);
1250: } else {
1.13 markus 1251: /*
1.197 djm 1252: * Normal, successful read. But we have an escape
1253: * character and have to process the characters one
1254: * by one.
1.13 markus 1255: */
1.195 djm 1256: if (process_escapes(NULL, &stdin_buffer,
1257: &stdout_buffer, &stderr_buffer, buf, len) == -1)
1.31 markus 1258: return;
1.11 markus 1259: }
1260: }
1261: }
1262:
1.77 itojun 1263: static void
1.161 deraadt 1264: client_process_output(fd_set *writeset)
1.11 markus 1265: {
1266: int len;
1267: char buf[100];
1.1 deraadt 1268:
1.11 markus 1269: /* Write buffered output to stdout. */
1270: if (FD_ISSET(fileno(stdout), writeset)) {
1271: /* Write as much data as possible. */
1272: len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
1.14 deraadt 1273: buffer_len(&stdout_buffer));
1.11 markus 1274: if (len <= 0) {
1.64 markus 1275: if (errno == EINTR || errno == EAGAIN)
1.11 markus 1276: len = 0;
1277: else {
1.13 markus 1278: /*
1279: * An error or EOF was encountered. Put an
1280: * error message to stderr buffer.
1281: */
1.197 djm 1282: snprintf(buf, sizeof buf,
1283: "write stdout: %.50s\r\n", strerror(errno));
1.11 markus 1284: buffer_append(&stderr_buffer, buf, strlen(buf));
1285: quit_pending = 1;
1286: return;
1287: }
1288: }
1289: /* Consume printed data from the buffer. */
1290: buffer_consume(&stdout_buffer, len);
1291: }
1292: /* Write buffered output to stderr. */
1293: if (FD_ISSET(fileno(stderr), writeset)) {
1294: /* Write as much data as possible. */
1295: len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
1.14 deraadt 1296: buffer_len(&stderr_buffer));
1.11 markus 1297: if (len <= 0) {
1.64 markus 1298: if (errno == EINTR || errno == EAGAIN)
1.11 markus 1299: len = 0;
1300: else {
1.197 djm 1301: /*
1302: * EOF or error, but can't even print
1303: * error message.
1304: */
1.11 markus 1305: quit_pending = 1;
1306: return;
1307: }
1308: }
1309: /* Consume printed characters from the buffer. */
1310: buffer_consume(&stderr_buffer, len);
1311: }
1.1 deraadt 1312: }
1313:
1.13 markus 1314: /*
1.15 markus 1315: * Get packets from the connection input buffer, and process them as long as
1316: * there are packets available.
1317: *
1318: * Any unknown packets received during the actual
1319: * session cause the session to terminate. This is
1320: * intended to make debugging easier since no
1321: * confirmations are sent. Any compatible protocol
1322: * extensions must be negotiated during the
1323: * preparatory phase.
1324: */
1325:
1.77 itojun 1326: static void
1.49 itojun 1327: client_process_buffered_input_packets(void)
1.15 markus 1328: {
1.197 djm 1329: dispatch_run(DISPATCH_NONBLOCK, &quit_pending,
1330: compat20 ? xxx_kex : NULL);
1.15 markus 1331: }
1332:
1.31 markus 1333: /* scan buf[] for '~' before sending data to the peer */
1.30 markus 1334:
1.195 djm 1335: /* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1336: void *
1337: client_new_escape_filter_ctx(int escape_char)
1338: {
1339: struct escape_filter_ctx *ret;
1340:
1341: ret = xmalloc(sizeof(*ret));
1342: ret->escape_pending = 0;
1343: ret->escape_char = escape_char;
1344: return (void *)ret;
1345: }
1346:
1.198 djm 1347: /* Free the escape filter context on channel free */
1348: void
1349: client_filter_cleanup(int cid, void *ctx)
1350: {
1351: xfree(ctx);
1352: }
1353:
1.195 djm 1354: int
1355: client_simple_escape_filter(Channel *c, char *buf, int len)
1.30 markus 1356: {
1.190 djm 1357: if (c->extended_usage != CHAN_EXTENDED_WRITE)
1358: return 0;
1359:
1.195 djm 1360: return process_escapes(c, &c->input, &c->output, &c->extended,
1361: buf, len);
1.30 markus 1362: }
1363:
1.77 itojun 1364: static void
1.60 markus 1365: client_channel_closed(int id, void *arg)
1366: {
1.83 markus 1367: channel_cancel_cleanup(id);
1.60 markus 1368: session_closed = 1;
1.233 djm 1369: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.60 markus 1370: }
1371:
1.15 markus 1372: /*
1.13 markus 1373: * Implements the interactive session with the server. This is called after
1374: * the user has been authenticated, and a command has been started on the
1.72 stevesk 1375: * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1376: * used as an escape character for terminating or suspending the session.
1.13 markus 1377: */
1.1 deraadt 1378:
1.20 markus 1379: int
1.30 markus 1380: client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
1.1 deraadt 1381: {
1.46 markus 1382: fd_set *readset = NULL, *writeset = NULL;
1.11 markus 1383: double start_time, total_time;
1.130 avsm 1384: int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
1.200 markus 1385: u_int64_t ibytes, obytes;
1.130 avsm 1386: u_int nalloc = 0;
1.11 markus 1387: char buf[100];
1388:
1389: debug("Entering interactive session.");
1390:
1391: start_time = get_current_time();
1392:
1393: /* Initialize variables. */
1.195 djm 1394: escape_pending1 = 0;
1.11 markus 1395: last_was_cr = 1;
1396: exit_status = -1;
1397: stdin_eof = 0;
1398: buffer_high = 64 * 1024;
1399: connection_in = packet_get_connection_in();
1400: connection_out = packet_get_connection_out();
1.46 markus 1401: max_fd = MAX(connection_in, connection_out);
1402:
1403: if (!compat20) {
1.63 markus 1404: /* enable nonblocking unless tty */
1405: if (!isatty(fileno(stdin)))
1406: set_nonblock(fileno(stdin));
1407: if (!isatty(fileno(stdout)))
1408: set_nonblock(fileno(stdout));
1409: if (!isatty(fileno(stderr)))
1410: set_nonblock(fileno(stderr));
1.46 markus 1411: max_fd = MAX(max_fd, fileno(stdin));
1412: max_fd = MAX(max_fd, fileno(stdout));
1413: max_fd = MAX(max_fd, fileno(stderr));
1414: }
1.11 markus 1415: quit_pending = 0;
1.195 djm 1416: escape_char1 = escape_char_arg;
1.11 markus 1417:
1418: /* Initialize buffers. */
1419: buffer_init(&stdin_buffer);
1420: buffer_init(&stdout_buffer);
1421: buffer_init(&stderr_buffer);
1422:
1.15 markus 1423: client_init_dispatch();
1424:
1.105 markus 1425: /*
1426: * Set signal handlers, (e.g. to restore non-blocking mode)
1427: * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1428: */
1.131 djm 1429: if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1430: signal(SIGHUP, signal_handler);
1.105 markus 1431: if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1432: signal(SIGINT, signal_handler);
1433: if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1434: signal(SIGQUIT, signal_handler);
1435: if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1436: signal(SIGTERM, signal_handler);
1.132 djm 1437: signal(SIGWINCH, window_change_handler);
1.11 markus 1438:
1439: if (have_pty)
1.233 djm 1440: enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.11 markus 1441:
1.48 markus 1442: if (compat20) {
1443: session_ident = ssh2_chan_id;
1.232 djm 1444: if (session_ident != -1) {
1445: if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1446: channel_register_filter(session_ident,
1447: client_simple_escape_filter, NULL,
1448: client_filter_cleanup,
1449: client_new_escape_filter_ctx(
1450: escape_char_arg));
1451: }
1.60 markus 1452: channel_register_cleanup(session_ident,
1.143 djm 1453: client_channel_closed, 0);
1.232 djm 1454: }
1.48 markus 1455: } else {
1456: /* Check if we should immediately send eof on stdin. */
1.16 markus 1457: client_check_initial_eof_on_stdin();
1.48 markus 1458: }
1.30 markus 1459:
1.11 markus 1460: /* Main loop of the client for the interactive session mode. */
1461: while (!quit_pending) {
1462:
1.13 markus 1463: /* Process buffered packets sent by the server. */
1.11 markus 1464: client_process_buffered_input_packets();
1465:
1.60 markus 1466: if (compat20 && session_closed && !channel_still_open())
1467: break;
1468:
1.56 markus 1469: rekeying = (xxx_kex != NULL && !xxx_kex->done);
1.16 markus 1470:
1.56 markus 1471: if (rekeying) {
1472: debug("rekeying in progress");
1473: } else {
1474: /*
1475: * Make packets of buffered stdin data, and buffer
1476: * them for sending to the server.
1477: */
1478: if (!compat20)
1479: client_make_packets_from_stdin_data();
1.11 markus 1480:
1.56 markus 1481: /*
1482: * Make packets from buffered channel data, and
1483: * enqueue them for sending to the server.
1484: */
1485: if (packet_not_very_much_data_to_write())
1486: channel_output_poll();
1.11 markus 1487:
1.56 markus 1488: /*
1489: * Check if the window size has changed, and buffer a
1490: * message about it to the server if so.
1491: */
1492: client_check_window_change();
1.11 markus 1493:
1.56 markus 1494: if (quit_pending)
1495: break;
1496: }
1.13 markus 1497: /*
1498: * Wait until we have something to do (something becomes
1499: * available on one of the descriptors).
1500: */
1.81 markus 1501: max_fd2 = max_fd;
1.56 markus 1502: client_wait_until_can_do_something(&readset, &writeset,
1.81 markus 1503: &max_fd2, &nalloc, rekeying);
1.11 markus 1504:
1505: if (quit_pending)
1506: break;
1507:
1.56 markus 1508: /* Do channel operations unless rekeying in progress. */
1509: if (!rekeying) {
1510: channel_after_select(readset, writeset);
1.108 markus 1511: if (need_rekeying || packet_need_rekeying()) {
1512: debug("need rekeying");
1.56 markus 1513: xxx_kex->done = 0;
1514: kex_send_kexinit(xxx_kex);
1515: need_rekeying = 0;
1516: }
1517: }
1.11 markus 1518:
1.17 markus 1519: /* Buffer input from the connection. */
1.46 markus 1520: client_process_net_input(readset);
1.17 markus 1521:
1522: if (quit_pending)
1523: break;
1.11 markus 1524:
1.17 markus 1525: if (!compat20) {
1526: /* Buffer data from stdin */
1.46 markus 1527: client_process_input(readset);
1.17 markus 1528: /*
1529: * Process output to stdout and stderr. Output to
1530: * the connection is processed elsewhere (above).
1531: */
1.46 markus 1532: client_process_output(writeset);
1.214 andreas 1533: }
1534:
1535: if (session_resumed) {
1536: connection_in = packet_get_connection_in();
1537: connection_out = packet_get_connection_out();
1538: max_fd = MAX(max_fd, connection_out);
1539: max_fd = MAX(max_fd, connection_in);
1540: session_resumed = 0;
1.17 markus 1541: }
1.11 markus 1542:
1.197 djm 1543: /*
1544: * Send as much buffered packet data as possible to the
1545: * sender.
1546: */
1.46 markus 1547: if (FD_ISSET(connection_out, writeset))
1.11 markus 1548: packet_write_poll();
1.222 djm 1549:
1550: /*
1551: * If we are a backgrounded control master, and the
1552: * timeout has expired without any active client
1553: * connections, then quit.
1554: */
1555: if (control_persist_exit_time > 0) {
1556: if (time(NULL) >= control_persist_exit_time) {
1557: debug("ControlPersist timeout expired");
1558: break;
1559: }
1560: }
1.11 markus 1561: }
1.46 markus 1562: if (readset)
1563: xfree(readset);
1564: if (writeset)
1565: xfree(writeset);
1.11 markus 1566:
1567: /* Terminate the session. */
1568:
1569: /* Stop watching for window change. */
1.132 djm 1570: signal(SIGWINCH, SIG_DFL);
1.211 andreas 1571:
1.213 stevesk 1572: if (compat20) {
1573: packet_start(SSH2_MSG_DISCONNECT);
1574: packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1575: packet_put_cstring("disconnected by user");
1.219 djm 1576: packet_put_cstring(""); /* language tag */
1.213 stevesk 1577: packet_send();
1578: packet_write_wait();
1579: }
1.11 markus 1580:
1.76 markus 1581: channel_free_all();
1.11 markus 1582:
1.75 markus 1583: if (have_pty)
1.233 djm 1584: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.75 markus 1585:
1586: /* restore blocking io */
1587: if (!isatty(fileno(stdin)))
1588: unset_nonblock(fileno(stdin));
1589: if (!isatty(fileno(stdout)))
1590: unset_nonblock(fileno(stdout));
1591: if (!isatty(fileno(stderr)))
1592: unset_nonblock(fileno(stderr));
1.116 dtucker 1593:
1594: /*
1595: * If there was no shell or command requested, there will be no remote
1596: * exit status to be returned. In that case, clear error code if the
1597: * connection was deliberately terminated at this end.
1598: */
1599: if (no_shell_flag && received_signal == SIGTERM) {
1600: received_signal = 0;
1601: exit_status = 0;
1602: }
1.75 markus 1603:
1.113 markus 1604: if (received_signal)
1.89 itojun 1605: fatal("Killed by signal %d.", (int) received_signal);
1.75 markus 1606:
1.13 markus 1607: /*
1608: * In interactive mode (with pseudo tty) display a message indicating
1609: * that the connection has been closed.
1610: */
1.11 markus 1611: if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1.197 djm 1612: snprintf(buf, sizeof buf,
1613: "Connection to %.64s closed.\r\n", host);
1.11 markus 1614: buffer_append(&stderr_buffer, buf, strlen(buf));
1615: }
1.70 markus 1616:
1.11 markus 1617: /* Output any buffered data for stdout. */
1.231 djm 1618: if (buffer_len(&stdout_buffer) > 0) {
1.230 djm 1619: len = atomicio(vwrite, fileno(stdout),
1620: buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer));
1.231 djm 1621: if (len < 0 || (u_int)len != buffer_len(&stdout_buffer))
1.11 markus 1622: error("Write failed flushing stdout buffer.");
1.231 djm 1623: else
1624: buffer_consume(&stdout_buffer, len);
1.11 markus 1625: }
1626:
1627: /* Output any buffered data for stderr. */
1.231 djm 1628: if (buffer_len(&stderr_buffer) > 0) {
1.230 djm 1629: len = atomicio(vwrite, fileno(stderr),
1630: buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
1.231 djm 1631: if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
1.11 markus 1632: error("Write failed flushing stderr buffer.");
1.231 djm 1633: else
1634: buffer_consume(&stderr_buffer, len);
1.11 markus 1635: }
1636:
1637: /* Clear and free any buffers. */
1638: memset(buf, 0, sizeof(buf));
1639: buffer_free(&stdin_buffer);
1640: buffer_free(&stdout_buffer);
1641: buffer_free(&stderr_buffer);
1642:
1643: /* Report bytes transferred, and transfer rates. */
1644: total_time = get_current_time() - start_time;
1.200 markus 1645: packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
1646: packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
1647: verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
1.229 djm 1648: (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
1.11 markus 1649: if (total_time > 0)
1.200 markus 1650: verbose("Bytes per second: sent %.1f, received %.1f",
1651: obytes / total_time, ibytes / total_time);
1.11 markus 1652: /* Return the exit status of the program. */
1653: debug("Exit status %d", exit_status);
1654: return exit_status;
1.15 markus 1655: }
1656:
1657: /*********/
1658:
1.77 itojun 1659: static void
1.94 markus 1660: client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
1.15 markus 1661: {
1.42 markus 1662: u_int data_len;
1.15 markus 1663: char *data = packet_get_string(&data_len);
1.93 markus 1664: packet_check_eom();
1.15 markus 1665: buffer_append(&stdout_buffer, data, data_len);
1666: memset(data, 0, data_len);
1667: xfree(data);
1668: }
1.77 itojun 1669: static void
1.94 markus 1670: client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
1.15 markus 1671: {
1.42 markus 1672: u_int data_len;
1.15 markus 1673: char *data = packet_get_string(&data_len);
1.93 markus 1674: packet_check_eom();
1.15 markus 1675: buffer_append(&stderr_buffer, data, data_len);
1676: memset(data, 0, data_len);
1677: xfree(data);
1678: }
1.77 itojun 1679: static void
1.94 markus 1680: client_input_exit_status(int type, u_int32_t seq, void *ctxt)
1.15 markus 1681: {
1682: exit_status = packet_get_int();
1.93 markus 1683: packet_check_eom();
1.15 markus 1684: /* Acknowledge the exit. */
1685: packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1686: packet_send();
1687: /*
1688: * Must wait for packet to be sent since we are
1689: * exiting the loop.
1690: */
1691: packet_write_wait();
1692: /* Flag that we want to exit. */
1693: quit_pending = 1;
1694: }
1.115 markus 1695: static void
1696: client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1697: {
1698: Channel *c = NULL;
1699: int remote_id, sock;
1700:
1701: /* Read the remote channel number from the message. */
1702: remote_id = packet_get_int();
1703: packet_check_eom();
1704:
1705: /*
1706: * Get a connection to the local authentication agent (this may again
1707: * get forwarded).
1708: */
1709: sock = ssh_get_authentication_socket();
1710:
1711: /*
1712: * If we could not connect the agent, send an error message back to
1713: * the server. This should never happen unless the agent dies,
1714: * because authentication forwarding is only enabled if we have an
1715: * agent.
1716: */
1717: if (sock >= 0) {
1718: c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1719: -1, 0, 0, 0, "authentication agent connection", 1);
1720: c->remote_id = remote_id;
1721: c->force_drain = 1;
1722: }
1723: if (c == NULL) {
1724: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1725: packet_put_int(remote_id);
1726: } else {
1727: /* Send a confirmation to the remote host. */
1728: debug("Forwarding authentication connection.");
1729: packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1730: packet_put_int(remote_id);
1731: packet_put_int(c->self);
1732: }
1733: packet_send();
1734: }
1.15 markus 1735:
1.77 itojun 1736: static Channel *
1.40 markus 1737: client_request_forwarded_tcpip(const char *request_type, int rchan)
1738: {
1.103 deraadt 1739: Channel *c = NULL;
1.40 markus 1740: char *listen_address, *originator_address;
1.208 djm 1741: u_short listen_port, originator_port;
1.40 markus 1742:
1743: /* Get rest of the packet */
1744: listen_address = packet_get_string(NULL);
1745: listen_port = packet_get_int();
1746: originator_address = packet_get_string(NULL);
1747: originator_port = packet_get_int();
1.93 markus 1748: packet_check_eom();
1.40 markus 1749:
1.191 djm 1750: debug("client_request_forwarded_tcpip: listen %s port %d, "
1751: "originator %s port %d", listen_address, listen_port,
1752: originator_address, originator_port);
1753:
1754: c = channel_connect_by_listen_address(listen_port,
1755: "forwarded-tcpip", originator_address);
1.40 markus 1756:
1757: xfree(originator_address);
1758: xfree(listen_address);
1759: return c;
1760: }
1761:
1.103 deraadt 1762: static Channel *
1.40 markus 1763: client_request_x11(const char *request_type, int rchan)
1764: {
1765: Channel *c = NULL;
1766: char *originator;
1.208 djm 1767: u_short originator_port;
1.67 markus 1768: int sock;
1.40 markus 1769:
1770: if (!options.forward_x11) {
1771: error("Warning: ssh server tried X11 forwarding.");
1.197 djm 1772: error("Warning: this is probably a break-in attempt by a "
1773: "malicious server.");
1.221 djm 1774: return NULL;
1775: }
1776: if (x11_refuse_time != 0 && time(NULL) >= x11_refuse_time) {
1777: verbose("Rejected X11 connection after ForwardX11Timeout "
1778: "expired");
1.40 markus 1779: return NULL;
1780: }
1781: originator = packet_get_string(NULL);
1782: if (datafellows & SSH_BUG_X11FWD) {
1783: debug2("buggy server: x11 request w/o originator_port");
1784: originator_port = 0;
1785: } else {
1786: originator_port = packet_get_int();
1787: }
1.93 markus 1788: packet_check_eom();
1.40 markus 1789: /* XXX check permission */
1.47 markus 1790: debug("client_request_x11: request from %s %d", originator,
1791: originator_port);
1.67 markus 1792: xfree(originator);
1.40 markus 1793: sock = x11_connect_display();
1.67 markus 1794: if (sock < 0)
1795: return NULL;
1796: c = channel_new("x11",
1797: SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1.110 markus 1798: CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
1.82 markus 1799: c->force_drain = 1;
1.40 markus 1800: return c;
1801: }
1802:
1.103 deraadt 1803: static Channel *
1.40 markus 1804: client_request_agent(const char *request_type, int rchan)
1805: {
1806: Channel *c = NULL;
1.67 markus 1807: int sock;
1.40 markus 1808:
1809: if (!options.forward_agent) {
1810: error("Warning: ssh server tried agent forwarding.");
1.197 djm 1811: error("Warning: this is probably a break-in attempt by a "
1812: "malicious server.");
1.40 markus 1813: return NULL;
1814: }
1.177 stevesk 1815: sock = ssh_get_authentication_socket();
1.67 markus 1816: if (sock < 0)
1817: return NULL;
1818: c = channel_new("authentication agent connection",
1819: SSH_CHANNEL_OPEN, sock, sock, -1,
1.185 dtucker 1820: CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
1.110 markus 1821: "authentication agent connection", 1);
1.82 markus 1822: c->force_drain = 1;
1.40 markus 1823: return c;
1.180 djm 1824: }
1825:
1826: int
1827: client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1828: {
1829: Channel *c;
1830: int fd;
1831:
1832: if (tun_mode == SSH_TUNMODE_NO)
1833: return 0;
1834:
1835: if (!compat20) {
1.207 stevesk 1836: error("Tunnel forwarding is not supported for protocol 1");
1.180 djm 1837: return -1;
1838: }
1839:
1840: debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1841:
1842: /* Open local tunnel device */
1843: if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1844: error("Tunnel device open failed.");
1845: return -1;
1846: }
1847:
1848: c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1849: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1850: c->datagram = 1;
1851:
1852: packet_start(SSH2_MSG_CHANNEL_OPEN);
1853: packet_put_cstring("tun@openssh.com");
1854: packet_put_int(c->self);
1855: packet_put_int(c->local_window_max);
1856: packet_put_int(c->local_maxpacket);
1857: packet_put_int(tun_mode);
1858: packet_put_int(remote_tun);
1859: packet_send();
1860:
1861: return 0;
1.40 markus 1862: }
1863:
1.22 markus 1864: /* XXXX move to generic input handler */
1.77 itojun 1865: static void
1.94 markus 1866: client_input_channel_open(int type, u_int32_t seq, void *ctxt)
1.22 markus 1867: {
1868: Channel *c = NULL;
1869: char *ctype;
1870: int rchan;
1.102 markus 1871: u_int rmaxpack, rwindow, len;
1.22 markus 1872:
1873: ctype = packet_get_string(&len);
1874: rchan = packet_get_int();
1875: rwindow = packet_get_int();
1876: rmaxpack = packet_get_int();
1877:
1.24 markus 1878: debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
1.22 markus 1879: ctype, rchan, rwindow, rmaxpack);
1880:
1.40 markus 1881: if (strcmp(ctype, "forwarded-tcpip") == 0) {
1882: c = client_request_forwarded_tcpip(ctype, rchan);
1883: } else if (strcmp(ctype, "x11") == 0) {
1884: c = client_request_x11(ctype, rchan);
1885: } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1886: c = client_request_agent(ctype, rchan);
1.22 markus 1887: }
1888: /* XXX duplicate : */
1889: if (c != NULL) {
1890: debug("confirm %s", ctype);
1891: c->remote_id = rchan;
1892: c->remote_window = rwindow;
1893: c->remote_maxpacket = rmaxpack;
1.69 markus 1894: if (c->type != SSH_CHANNEL_CONNECTING) {
1895: packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1896: packet_put_int(c->remote_id);
1897: packet_put_int(c->self);
1898: packet_put_int(c->local_window);
1899: packet_put_int(c->local_maxpacket);
1900: packet_send();
1901: }
1.22 markus 1902: } else {
1903: debug("failure %s", ctype);
1904: packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1905: packet_put_int(rchan);
1906: packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
1.66 markus 1907: if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1.69 markus 1908: packet_put_cstring("open failed");
1.66 markus 1909: packet_put_cstring("");
1910: }
1.22 markus 1911: packet_send();
1912: }
1913: xfree(ctype);
1914: }
1.77 itojun 1915: static void
1.94 markus 1916: client_input_channel_req(int type, u_int32_t seq, void *ctxt)
1.48 markus 1917: {
1918: Channel *c = NULL;
1.123 djm 1919: int exitval, id, reply, success = 0;
1.48 markus 1920: char *rtype;
1921:
1922: id = packet_get_int();
1923: rtype = packet_get_string(NULL);
1924: reply = packet_get_char();
1925:
1926: debug("client_input_channel_req: channel %d rtype %s reply %d",
1927: id, rtype, reply);
1928:
1.128 djm 1929: if (id == -1) {
1930: error("client_input_channel_req: request for channel -1");
1931: } else if ((c = channel_lookup(id)) == NULL) {
1.197 djm 1932: error("client_input_channel_req: channel %d: "
1933: "unknown channel", id);
1.193 markus 1934: } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1935: packet_check_eom();
1936: chan_rcvd_eow(c);
1.48 markus 1937: } else if (strcmp(rtype, "exit-status") == 0) {
1.123 djm 1938: exitval = packet_get_int();
1.217 djm 1939: if (c->ctl_chan != -1) {
1940: mux_exit_message(c, exitval);
1.215 djm 1941: success = 1;
1942: } else if (id == session_ident) {
1943: /* Record exit value of local session */
1.123 djm 1944: success = 1;
1945: exit_status = exitval;
1.215 djm 1946: } else {
1.218 djm 1947: /* Probably for a mux channel that has already closed */
1948: debug("%s: no sink for exit-status on channel %d",
1949: __func__, id);
1.123 djm 1950: }
1.93 markus 1951: packet_check_eom();
1.48 markus 1952: }
1.226 djm 1953: if (reply && c != NULL) {
1.48 markus 1954: packet_start(success ?
1955: SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1.205 markus 1956: packet_put_int(c->remote_id);
1.48 markus 1957: packet_send();
1958: }
1959: xfree(rtype);
1960: }
1.95 markus 1961: static void
1962: client_input_global_request(int type, u_int32_t seq, void *ctxt)
1963: {
1964: char *rtype;
1965: int want_reply;
1966: int success = 0;
1967:
1968: rtype = packet_get_string(NULL);
1969: want_reply = packet_get_char();
1.117 markus 1970: debug("client_input_global_request: rtype %s want_reply %d",
1971: rtype, want_reply);
1.95 markus 1972: if (want_reply) {
1973: packet_start(success ?
1974: SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1975: packet_send();
1976: packet_write_wait();
1977: }
1978: xfree(rtype);
1979: }
1.22 markus 1980:
1.123 djm 1981: void
1.129 deraadt 1982: client_session2_setup(int id, int want_tty, int want_subsystem,
1.190 djm 1983: const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
1.123 djm 1984: {
1985: int len;
1.132 djm 1986: Channel *c = NULL;
1.123 djm 1987:
1988: debug2("%s: id %d", __func__, id);
1989:
1.132 djm 1990: if ((c = channel_lookup(id)) == NULL)
1991: fatal("client_session2_setup: channel %d: unknown channel", id);
1.224 djm 1992:
1993: packet_set_interactive(want_tty,
1994: options.ip_qos_interactive, options.ip_qos_bulk);
1.132 djm 1995:
1.123 djm 1996: if (want_tty) {
1997: struct winsize ws;
1998:
1999: /* Store window size in the packet. */
2000: if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2001: memset(&ws, 0, sizeof(ws));
2002:
1.190 djm 2003: channel_request_start(id, "pty-req", 1);
1.234 djm 2004: client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
1.123 djm 2005: packet_put_cstring(term != NULL ? term : "");
1.158 deraadt 2006: packet_put_int((u_int)ws.ws_col);
2007: packet_put_int((u_int)ws.ws_row);
2008: packet_put_int((u_int)ws.ws_xpixel);
2009: packet_put_int((u_int)ws.ws_ypixel);
1.194 djm 2010: if (tiop == NULL)
2011: tiop = get_saved_tio();
2012: tty_make_modes(-1, tiop);
1.123 djm 2013: packet_send();
2014: /* XXX wait for reply */
1.132 djm 2015: c->client_tty = 1;
1.123 djm 2016: }
2017:
2018: /* Transfer any environment variables from client to server */
1.126 djm 2019: if (options.num_send_env != 0 && env != NULL) {
1.123 djm 2020: int i, j, matched;
2021: char *name, *val;
2022:
2023: debug("Sending environment.");
1.126 djm 2024: for (i = 0; env[i] != NULL; i++) {
1.123 djm 2025: /* Split */
1.126 djm 2026: name = xstrdup(env[i]);
1.123 djm 2027: if ((val = strchr(name, '=')) == NULL) {
1.144 stevesk 2028: xfree(name);
1.123 djm 2029: continue;
2030: }
2031: *val++ = '\0';
2032:
2033: matched = 0;
2034: for (j = 0; j < options.num_send_env; j++) {
2035: if (match_pattern(name, options.send_env[j])) {
2036: matched = 1;
2037: break;
2038: }
2039: }
2040: if (!matched) {
2041: debug3("Ignored env %s", name);
1.144 stevesk 2042: xfree(name);
1.123 djm 2043: continue;
2044: }
2045:
2046: debug("Sending env %s = %s", name, val);
2047: channel_request_start(id, "env", 0);
2048: packet_put_cstring(name);
2049: packet_put_cstring(val);
2050: packet_send();
1.144 stevesk 2051: xfree(name);
1.123 djm 2052: }
2053: }
2054:
2055: len = buffer_len(cmd);
2056: if (len > 0) {
2057: if (len > 900)
2058: len = 900;
2059: if (want_subsystem) {
1.190 djm 2060: debug("Sending subsystem: %.*s",
2061: len, (u_char*)buffer_ptr(cmd));
2062: channel_request_start(id, "subsystem", 1);
1.234 djm 2063: client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
1.123 djm 2064: } else {
1.190 djm 2065: debug("Sending command: %.*s",
2066: len, (u_char*)buffer_ptr(cmd));
2067: channel_request_start(id, "exec", 1);
1.234 djm 2068: client_expect_confirm(id, "exec", CONFIRM_CLOSE);
1.123 djm 2069: }
2070: packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2071: packet_send();
2072: } else {
1.190 djm 2073: channel_request_start(id, "shell", 1);
1.234 djm 2074: client_expect_confirm(id, "shell", CONFIRM_CLOSE);
1.123 djm 2075: packet_send();
2076: }
2077: }
2078:
1.77 itojun 2079: static void
1.49 itojun 2080: client_init_dispatch_20(void)
1.16 markus 2081: {
1.55 markus 2082: dispatch_init(&dispatch_protocol_error);
1.100 markus 2083:
1.16 markus 2084: dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2085: dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2086: dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2087: dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
1.22 markus 2088: dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
1.16 markus 2089: dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2090: dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1.48 markus 2091: dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
1.16 markus 2092: dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
1.189 djm 2093: dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2094: dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
1.95 markus 2095: dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
1.55 markus 2096:
2097: /* rekeying */
2098: dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
1.100 markus 2099:
2100: /* global request reply messages */
2101: dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2102: dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
1.16 markus 2103: }
1.197 djm 2104:
1.77 itojun 2105: static void
1.49 itojun 2106: client_init_dispatch_13(void)
1.15 markus 2107: {
2108: dispatch_init(NULL);
2109: dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2110: dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2111: dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
2112: dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2113: dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2114: dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
2115: dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2116: dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2117: dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
1.39 markus 2118:
2119: dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
1.115 markus 2120: &client_input_agent_open : &deny_input_open);
1.39 markus 2121: dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2122: &x11_input_open : &deny_input_open);
1.15 markus 2123: }
1.197 djm 2124:
1.77 itojun 2125: static void
1.49 itojun 2126: client_init_dispatch_15(void)
1.15 markus 2127: {
2128: client_init_dispatch_13();
2129: dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2130: dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2131: }
1.197 djm 2132:
1.79 stevesk 2133: static void
1.49 itojun 2134: client_init_dispatch(void)
1.15 markus 2135: {
1.16 markus 2136: if (compat20)
2137: client_init_dispatch_20();
2138: else if (compat13)
1.15 markus 2139: client_init_dispatch_13();
2140: else
2141: client_init_dispatch_15();
1.232 djm 2142: }
2143:
2144: void
2145: client_stop_mux(void)
2146: {
2147: if (options.control_path != NULL && muxserver_sock != -1)
2148: unlink(options.control_path);
2149: /*
2150: * If we are in persist mode, signal that we should close when all
2151: * active channels are closed.
2152: */
1.235 djm 2153: if (options.control_persist) {
1.232 djm 2154: session_closed = 1;
1.235 djm 2155: setproctitle("[stopped mux]");
2156: }
1.113 markus 2157: }
2158:
2159: /* client specific fatal cleanup */
2160: void
1.114 markus 2161: cleanup_exit(int i)
1.113 markus 2162: {
1.233 djm 2163: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.113 markus 2164: leave_non_blocking();
1.192 djm 2165: if (options.control_path != NULL && muxserver_sock != -1)
1.123 djm 2166: unlink(options.control_path);
1.223 djm 2167: ssh_kill_proxy_command();
1.114 markus 2168: _exit(i);
1.1 deraadt 2169: }