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