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