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