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