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