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