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