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