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