Annotation of src/usr.bin/ssh/clientloop.c, Revision 1.294
1.294 ! djm 1: /* $OpenBSD: clientloop.c,v 1.293 2017/04/30 23:18:44 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.189 djm 68: #include <sys/queue.h>
1.150 stevesk 69:
1.155 stevesk 70: #include <ctype.h>
1.168 stevesk 71: #include <errno.h>
1.151 stevesk 72: #include <paths.h>
1.153 stevesk 73: #include <signal.h>
1.174 stevesk 74: #include <stdio.h>
1.173 stevesk 75: #include <stdlib.h>
1.170 stevesk 76: #include <string.h>
1.150 stevesk 77: #include <termios.h>
1.175 deraadt 78: #include <pwd.h>
1.169 stevesk 79: #include <unistd.h>
1.266 deraadt 80: #include <limits.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.275 markus 94: #include "myproposal.h"
1.45 markus 95: #include "log.h"
1.261 millert 96: #include "misc.h"
1.45 markus 97: #include "readconf.h"
98: #include "clientloop.h"
1.147 djm 99: #include "sshconnect.h"
1.40 markus 100: #include "authfd.h"
1.45 markus 101: #include "atomicio.h"
1.118 deraadt 102: #include "sshpty.h"
1.123 djm 103: #include "match.h"
104: #include "msg.h"
1.262 djm 105: #include "ssherr.h"
1.267 djm 106: #include "hostfile.h"
1.39 markus 107:
108: /* import options */
109: extern Options options;
110:
1.1 deraadt 111: /* Flag indicating that stdin should be redirected from /dev/null. */
112: extern int stdin_null_flag;
113:
1.116 dtucker 114: /* Flag indicating that no shell has been requested */
115: extern int no_shell_flag;
116:
1.286 djm 117: /* Flag indicating that ssh should daemonise after authentication is complete */
118: extern int fork_after_authentication_flag;
119:
1.123 djm 120: /* Control socket */
1.217 djm 121: extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
1.123 djm 122:
1.13 markus 123: /*
124: * Name of the host we are connecting to. This is the name given on the
125: * command line, or the HostName specified for the user-supplied name in a
126: * configuration file.
127: */
1.1 deraadt 128: extern char *host;
129:
1.13 markus 130: /*
131: * Flag to indicate that we have received a window change signal which has
132: * not yet been processed. This will cause a message indicating the new
133: * window size to be sent to the server a little later. This is volatile
134: * because this is updated in a signal handler.
135: */
1.88 markus 136: static volatile sig_atomic_t received_window_change_signal = 0;
137: static volatile sig_atomic_t received_signal = 0;
1.1 deraadt 138:
1.145 djm 139: /* Flag indicating whether the user's terminal is in non-blocking mode. */
1.1 deraadt 140: static int in_non_blocking_mode = 0;
141:
1.222 djm 142: /* Time when backgrounded control master using ControlPersist should exit */
143: static time_t control_persist_exit_time = 0;
144:
1.1 deraadt 145: /* Common data for the client loop code. */
1.217 djm 146: volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
1.11 markus 147: static int last_was_cr; /* Last character was a newline. */
1.197 djm 148: static int exit_status; /* Used to store the command exit status. */
1.294 ! djm 149: static Buffer stderr_buffer; /* Used for final exit message. */
1.11 markus 150: static int connection_in; /* Connection to server (input). */
151: static int connection_out; /* Connection to server (output). */
1.56 markus 152: static int need_rekeying; /* Set to non-zero if rekeying is requested. */
1.221 djm 153: static int session_closed; /* In SSH2: login session closed. */
1.274 djm 154: static u_int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
1.1 deraadt 155:
1.77 itojun 156: static void client_init_dispatch(void);
1.16 markus 157: int session_ident = -1;
158:
1.195 djm 159: /* Track escape per proto2 channel */
160: struct escape_filter_ctx {
161: int escape_pending;
162: int escape_char;
163: };
164:
165: /* Context for channel confirmation replies */
1.190 djm 166: struct channel_reply_ctx {
167: const char *request_type;
1.234 djm 168: int id;
169: enum confirm_action action;
1.190 djm 170: };
171:
1.196 djm 172: /* Global request success/failure callbacks */
173: struct global_confirm {
174: TAILQ_ENTRY(global_confirm) entry;
175: global_confirm_cb *cb;
176: void *ctx;
177: int ref_count;
178: };
179: TAILQ_HEAD(global_confirms, global_confirm);
180: static struct global_confirms global_confirms =
181: TAILQ_HEAD_INITIALIZER(global_confirms);
182:
1.123 djm 183: void ssh_process_session2_setup(int, int, int, Buffer *);
184:
1.1 deraadt 185: /* Restores stdin to blocking mode. */
186:
1.77 itojun 187: static void
1.49 itojun 188: leave_non_blocking(void)
1.1 deraadt 189: {
1.11 markus 190: if (in_non_blocking_mode) {
1.125 djm 191: unset_nonblock(fileno(stdin));
1.11 markus 192: in_non_blocking_mode = 0;
193: }
1.1 deraadt 194: }
195:
1.13 markus 196: /*
197: * Signal handler for the window change signal (SIGWINCH). This just sets a
198: * flag indicating that the window has changed.
199: */
1.157 deraadt 200: /*ARGSUSED */
1.77 itojun 201: static void
1.11 markus 202: window_change_handler(int sig)
1.1 deraadt 203: {
1.11 markus 204: received_window_change_signal = 1;
205: signal(SIGWINCH, window_change_handler);
1.1 deraadt 206: }
207:
1.13 markus 208: /*
209: * Signal handler for signals that cause the program to terminate. These
210: * signals must be trapped to restore terminal modes.
211: */
1.157 deraadt 212: /*ARGSUSED */
1.77 itojun 213: static void
1.11 markus 214: signal_handler(int sig)
1.1 deraadt 215: {
1.75 markus 216: received_signal = sig;
217: quit_pending = 1;
1.1 deraadt 218: }
219:
1.13 markus 220: /*
221: * Returns current time in seconds from Jan 1, 1970 with the maximum
222: * available resolution.
223: */
1.1 deraadt 224:
1.77 itojun 225: static double
1.49 itojun 226: get_current_time(void)
1.1 deraadt 227: {
1.11 markus 228: struct timeval tv;
229: gettimeofday(&tv, NULL);
230: return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
1.138 djm 231: }
232:
1.222 djm 233: /*
234: * Sets control_persist_exit_time to the absolute time when the
235: * backgrounded control master should exit due to expiry of the
236: * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
237: * control master process, or if there is no ControlPersist timeout.
238: */
239: static void
240: set_control_persist_exit_time(void)
241: {
242: if (muxserver_sock == -1 || !options.control_persist
1.232 djm 243: || options.control_persist_timeout == 0) {
1.222 djm 244: /* not using a ControlPersist timeout */
245: control_persist_exit_time = 0;
1.232 djm 246: } else if (channel_still_open()) {
1.222 djm 247: /* some client connections are still open */
248: if (control_persist_exit_time > 0)
249: debug2("%s: cancel scheduled exit", __func__);
250: control_persist_exit_time = 0;
251: } else if (control_persist_exit_time <= 0) {
252: /* a client connection has recently closed */
1.251 dtucker 253: control_persist_exit_time = monotime() +
1.222 djm 254: (time_t)options.control_persist_timeout;
255: debug2("%s: schedule exit in %d seconds", __func__,
256: options.control_persist_timeout);
257: }
258: /* else we are already counting down to the timeout */
259: }
260:
1.238 dtucker 261: #define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
262: static int
263: client_x11_display_valid(const char *display)
264: {
265: size_t i, dlen;
266:
1.279 djm 267: if (display == NULL)
268: return 0;
269:
1.238 dtucker 270: dlen = strlen(display);
271: for (i = 0; i < dlen; i++) {
1.256 deraadt 272: if (!isalnum((u_char)display[i]) &&
1.238 dtucker 273: strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
274: debug("Invalid character '%c' in DISPLAY", display[i]);
275: return 0;
276: }
277: }
278: return 1;
279: }
280:
1.274 djm 281: #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
282: #define X11_TIMEOUT_SLACK 60
1.279 djm 283: int
1.138 djm 284: client_x11_get_proto(const char *display, const char *xauth_path,
1.221 djm 285: u_int trusted, u_int timeout, char **_proto, char **_data)
1.138 djm 286: {
1.279 djm 287: char cmd[1024], line[512], xdisplay[512];
288: char xauthfile[PATH_MAX], xauthdir[PATH_MAX];
1.138 djm 289: static char proto[512], data[512];
290: FILE *f;
1.288 tedu 291: int got_data = 0, generated = 0, do_unlink = 0, r;
1.138 djm 292: struct stat st;
1.274 djm 293: u_int now, x11_timeout_real;
1.138 djm 294:
295: *_proto = proto;
296: *_data = data;
1.279 djm 297: proto[0] = data[0] = xauthfile[0] = xauthdir[0] = '\0';
1.138 djm 298:
1.279 djm 299: if (!client_x11_display_valid(display)) {
1.283 millert 300: if (display != NULL)
301: logit("DISPLAY \"%s\" invalid; disabling X11 forwarding",
302: display);
1.279 djm 303: return -1;
304: }
305: if (xauth_path != NULL && stat(xauth_path, &st) == -1) {
1.138 djm 306: debug("No xauth program.");
1.279 djm 307: xauth_path = NULL;
308: }
309:
310: if (xauth_path != NULL) {
1.138 djm 311: /*
312: * Handle FamilyLocal case where $DISPLAY does
313: * not match an authorization entry. For this we
314: * just try "xauth list unix:displaynum.screennum".
315: * XXX: "localhost" match to determine FamilyLocal
316: * is not perfect.
317: */
318: if (strncmp(display, "localhost:", 10) == 0) {
1.279 djm 319: if ((r = snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
320: display + 10)) < 0 ||
321: (size_t)r >= sizeof(xdisplay)) {
322: error("%s: display name too long", __func__);
323: return -1;
324: }
1.138 djm 325: display = xdisplay;
326: }
327: if (trusted == 0) {
1.274 djm 328: /*
1.279 djm 329: * Generate an untrusted X11 auth cookie.
330: *
1.274 djm 331: * The authentication cookie should briefly outlive
332: * ssh's willingness to forward X11 connections to
333: * avoid nasty fail-open behaviour in the X server.
334: */
1.279 djm 335: mktemp_proto(xauthdir, sizeof(xauthdir));
336: if (mkdtemp(xauthdir) == NULL) {
337: error("%s: mkdtemp: %s",
338: __func__, strerror(errno));
339: return -1;
340: }
341: do_unlink = 1;
342: if ((r = snprintf(xauthfile, sizeof(xauthfile),
343: "%s/xauthfile", xauthdir)) < 0 ||
344: (size_t)r >= sizeof(xauthfile)) {
345: error("%s: xauthfile path too long", __func__);
346: unlink(xauthfile);
347: rmdir(xauthdir);
348: return -1;
349: }
350:
1.274 djm 351: if (timeout >= UINT_MAX - X11_TIMEOUT_SLACK)
352: x11_timeout_real = UINT_MAX;
353: else
354: x11_timeout_real = timeout + X11_TIMEOUT_SLACK;
1.279 djm 355: if ((r = snprintf(cmd, sizeof(cmd),
356: "%s -f %s generate %s " SSH_X11_PROTO
357: " untrusted timeout %u 2>" _PATH_DEVNULL,
358: xauth_path, xauthfile, display,
359: x11_timeout_real)) < 0 ||
360: (size_t)r >= sizeof(cmd))
361: fatal("%s: cmd too long", __func__);
362: debug2("%s: %s", __func__, cmd);
363: if (x11_refuse_time == 0) {
364: now = monotime() + 1;
365: if (UINT_MAX - timeout < now)
366: x11_refuse_time = UINT_MAX;
367: else
368: x11_refuse_time = now + timeout;
369: channel_set_x11_refuse_time(x11_refuse_time);
1.138 djm 370: }
1.279 djm 371: if (system(cmd) == 0)
372: generated = 1;
1.138 djm 373: }
1.181 markus 374:
375: /*
376: * When in untrusted mode, we read the cookie only if it was
377: * successfully generated as an untrusted one in the step
378: * above.
379: */
380: if (trusted || generated) {
381: snprintf(cmd, sizeof(cmd),
382: "%s %s%s list %s 2>" _PATH_DEVNULL,
383: xauth_path,
384: generated ? "-f " : "" ,
385: generated ? xauthfile : "",
386: display);
387: debug2("x11_get_proto: %s", cmd);
388: f = popen(cmd, "r");
389: if (f && fgets(line, sizeof(line), f) &&
390: sscanf(line, "%*s %511s %511s", proto, data) == 2)
391: got_data = 1;
392: if (f)
393: pclose(f);
1.279 djm 394: }
1.138 djm 395: }
396:
397: if (do_unlink) {
398: unlink(xauthfile);
399: rmdir(xauthdir);
400: }
401:
1.279 djm 402: /* Don't fall back to fake X11 data for untrusted forwarding */
403: if (!trusted && !got_data) {
404: error("Warning: untrusted X11 forwarding setup failed: "
405: "xauth key data not generated");
406: return -1;
407: }
408:
1.138 djm 409: /*
410: * If we didn't get authentication data, just make up some
411: * data. The forwarding code will check the validity of the
412: * response anyway, and substitute this data. The X11
413: * server, however, will ignore this fake data and use
414: * whatever authentication mechanisms it was using otherwise
415: * for the local connection.
416: */
417: if (!got_data) {
1.288 tedu 418: u_int8_t rnd[16];
419: u_int i;
1.138 djm 420:
421: logit("Warning: No xauth data; "
422: "using fake authentication data for X11 forwarding.");
423: strlcpy(proto, SSH_X11_PROTO, sizeof proto);
1.288 tedu 424: arc4random_buf(rnd, sizeof(rnd));
425: for (i = 0; i < sizeof(rnd); i++) {
1.138 djm 426: snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
1.288 tedu 427: rnd[i]);
1.138 djm 428: }
429: }
1.279 djm 430:
431: return 0;
1.1 deraadt 432: }
433:
1.13 markus 434: /*
435: * Checks if the client window has changed, and sends a packet about it to
436: * the server if so. The actual change is detected elsewhere (by a software
437: * interrupt on Unix); this just checks the flag and sends a message if
438: * appropriate.
439: */
1.1 deraadt 440:
1.77 itojun 441: static void
1.49 itojun 442: client_check_window_change(void)
1.1 deraadt 443: {
1.292 djm 444: if (!received_window_change_signal)
1.16 markus 445: return;
446: /** XXX race */
447: received_window_change_signal = 0;
448:
1.292 djm 449: debug2("%s: changed", __func__);
1.16 markus 450:
1.292 djm 451: channel_send_window_changes();
1.1 deraadt 452: }
453:
1.264 markus 454: static int
1.117 markus 455: client_global_request_reply(int type, u_int32_t seq, void *ctxt)
456: {
1.196 djm 457: struct global_confirm *gc;
458:
459: if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
1.264 markus 460: return 0;
1.196 djm 461: if (gc->cb != NULL)
462: gc->cb(type, seq, gc->ctx);
463: if (--gc->ref_count <= 0) {
464: TAILQ_REMOVE(&global_confirms, gc, entry);
1.257 tedu 465: explicit_bzero(gc, sizeof(*gc));
1.250 djm 466: free(gc);
1.196 djm 467: }
468:
1.210 andreas 469: packet_set_alive_timeouts(0);
1.264 markus 470: return 0;
1.117 markus 471: }
472:
473: static void
474: server_alive_check(void)
475: {
1.210 andreas 476: if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
1.228 djm 477: logit("Timeout, server %s not responding.", host);
1.176 markus 478: cleanup_exit(255);
479: }
1.117 markus 480: packet_start(SSH2_MSG_GLOBAL_REQUEST);
481: packet_put_cstring("keepalive@openssh.com");
482: packet_put_char(1); /* boolean: want reply */
483: packet_send();
1.196 djm 484: /* Insert an empty placeholder to maintain ordering */
485: client_register_global_confirm(NULL, NULL);
1.117 markus 486: }
487:
1.13 markus 488: /*
489: * Waits until the client can do something (some data becomes available on
490: * one of the file descriptors).
491: */
1.77 itojun 492: static void
1.46 markus 493: client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
1.130 avsm 494: int *maxfdp, u_int *nallocp, int rekeying)
1.11 markus 495: {
1.117 markus 496: struct timeval tv, *tvp;
1.222 djm 497: int timeout_secs;
1.251 dtucker 498: time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
1.117 markus 499: int ret;
500:
1.46 markus 501: /* Add any selections by the channel mechanism. */
1.239 djm 502: channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
503: &minwait_secs, rekeying);
1.11 markus 504:
1.292 djm 505: /* channel_prepare_select could have closed the last channel */
506: if (session_closed && !channel_still_open() &&
507: !packet_have_data_to_write()) {
508: /* clear mask since we did not call select() */
509: memset(*readsetp, 0, *nallocp);
510: memset(*writesetp, 0, *nallocp);
511: return;
1.16 markus 512: }
1.11 markus 513:
1.294 ! djm 514: FD_SET(connection_in, *readsetp);
! 515:
1.11 markus 516: /* Select server connection if have data to write to the server. */
517: if (packet_have_data_to_write())
1.46 markus 518: FD_SET(connection_out, *writesetp);
1.11 markus 519:
1.13 markus 520: /*
521: * Wait for something to happen. This will suspend the process until
522: * some selected descriptor can be read, written, or has some other
1.222 djm 523: * event pending, or a timeout expires.
1.13 markus 524: */
1.11 markus 525:
1.222 djm 526: timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
1.292 djm 527: if (options.server_alive_interval > 0) {
1.222 djm 528: timeout_secs = options.server_alive_interval;
1.249 dtucker 529: server_alive_time = now + options.server_alive_interval;
530: }
1.292 djm 531: if (options.rekey_interval > 0 && !rekeying)
1.287 deraadt 532: timeout_secs = MINIMUM(timeout_secs, packet_get_rekey_timeout());
1.222 djm 533: set_control_persist_exit_time();
534: if (control_persist_exit_time > 0) {
1.287 deraadt 535: timeout_secs = MINIMUM(timeout_secs,
1.249 dtucker 536: control_persist_exit_time - now);
1.222 djm 537: if (timeout_secs < 0)
538: timeout_secs = 0;
539: }
1.239 djm 540: if (minwait_secs != 0)
1.287 deraadt 541: timeout_secs = MINIMUM(timeout_secs, (int)minwait_secs);
1.222 djm 542: if (timeout_secs == INT_MAX)
1.117 markus 543: tvp = NULL;
1.129 deraadt 544: else {
1.222 djm 545: tv.tv_sec = timeout_secs;
1.117 markus 546: tv.tv_usec = 0;
547: tvp = &tv;
548: }
1.222 djm 549:
1.117 markus 550: ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
551: if (ret < 0) {
1.11 markus 552: char buf[100];
1.51 markus 553:
554: /*
555: * We have to clear the select masks, because we return.
556: * We have to return, because the mainloop checks for the flags
557: * set by the signal handlers.
558: */
1.87 markus 559: memset(*readsetp, 0, *nallocp);
560: memset(*writesetp, 0, *nallocp);
1.50 deraadt 561:
1.11 markus 562: if (errno == EINTR)
563: return;
564: /* Note: we might still have data in the buffers. */
565: snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
566: buffer_append(&stderr_buffer, buf, strlen(buf));
567: quit_pending = 1;
1.249 dtucker 568: } else if (ret == 0) {
569: /*
570: * Timeout. Could have been either keepalive or rekeying.
571: * Keepalive we check here, rekeying is checked in clientloop.
572: */
1.251 dtucker 573: if (server_alive_time != 0 && server_alive_time <= monotime())
1.249 dtucker 574: server_alive_check();
575: }
576:
1.11 markus 577: }
578:
1.77 itojun 579: static void
1.31 markus 580: client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
1.1 deraadt 581: {
1.11 markus 582: /* Flush stdout and stderr buffers. */
1.31 markus 583: if (buffer_len(bout) > 0)
1.197 djm 584: atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
585: buffer_len(bout));
1.31 markus 586: if (buffer_len(berr) > 0)
1.197 djm 587: atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
588: buffer_len(berr));
1.11 markus 589:
1.233 djm 590: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.11 markus 591:
1.13 markus 592: /*
593: * Free (and clear) the buffer to reduce the amount of data that gets
594: * written to swap.
595: */
1.31 markus 596: buffer_free(bin);
597: buffer_free(bout);
598: buffer_free(berr);
1.11 markus 599:
600: /* Send the suspend signal to the program itself. */
601: kill(getpid(), SIGTSTP);
602:
1.132 djm 603: /* Reset window sizes in case they have changed */
604: received_window_change_signal = 1;
1.11 markus 605:
606: /* OK, we have been continued by the user. Reinitialize buffers. */
1.31 markus 607: buffer_init(bin);
608: buffer_init(bout);
609: buffer_init(berr);
1.11 markus 610:
1.233 djm 611: enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.11 markus 612: }
613:
1.77 itojun 614: static void
1.161 deraadt 615: client_process_net_input(fd_set *readset)
1.11 markus 616: {
1.280 markus 617: int len;
1.17 markus 618: char buf[8192];
1.11 markus 619:
1.13 markus 620: /*
621: * Read input from the server, and add any such data to the buffer of
622: * the packet subsystem.
623: */
1.11 markus 624: if (FD_ISSET(connection_in, readset)) {
625: /* Read as much as possible. */
1.280 markus 626: len = read(connection_in, buf, sizeof(buf));
627: if (len == 0) {
1.197 djm 628: /*
629: * Received EOF. The remote host has closed the
630: * connection.
631: */
632: snprintf(buf, sizeof buf,
633: "Connection to %.300s closed by remote host.\r\n",
634: host);
1.1 deraadt 635: buffer_append(&stderr_buffer, buf, strlen(buf));
636: quit_pending = 1;
637: return;
1.11 markus 638: }
1.13 markus 639: /*
640: * There is a kernel bug on Solaris that causes select to
641: * sometimes wake up even though there is no data available.
642: */
1.53 millert 643: if (len < 0 && (errno == EAGAIN || errno == EINTR))
1.11 markus 644: len = 0;
645:
646: if (len < 0) {
1.197 djm 647: /*
648: * An error has encountered. Perhaps there is a
649: * network problem.
650: */
651: snprintf(buf, sizeof buf,
652: "Read from remote host %.300s: %.100s\r\n",
653: host, strerror(errno));
1.11 markus 654: buffer_append(&stderr_buffer, buf, strlen(buf));
655: quit_pending = 1;
656: return;
657: }
658: packet_process_incoming(buf, len);
659: }
1.17 markus 660: }
1.16 markus 661:
1.97 jakob 662: static void
1.190 djm 663: client_status_confirm(int type, Channel *c, void *ctx)
1.123 djm 664: {
1.190 djm 665: struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
666: char errmsg[256];
667: int tochan;
668:
1.234 djm 669: /*
670: * If a TTY was explicitly requested, then a failure to allocate
671: * one is fatal.
672: */
673: if (cr->action == CONFIRM_TTY &&
674: (options.request_tty == REQUEST_TTY_FORCE ||
675: options.request_tty == REQUEST_TTY_YES))
676: cr->action = CONFIRM_CLOSE;
677:
1.190 djm 678: /* XXX supress on mux _client_ quietmode */
679: tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
1.217 djm 680: c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
1.190 djm 681:
682: if (type == SSH2_MSG_CHANNEL_SUCCESS) {
683: debug2("%s request accepted on channel %d",
684: cr->request_type, c->self);
685: } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
686: if (tochan) {
687: snprintf(errmsg, sizeof(errmsg),
688: "%s request failed\r\n", cr->request_type);
689: } else {
690: snprintf(errmsg, sizeof(errmsg),
691: "%s request failed on channel %d",
692: cr->request_type, c->self);
693: }
694: /* If error occurred on primary session channel, then exit */
1.234 djm 695: if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
1.190 djm 696: fatal("%s", errmsg);
1.234 djm 697: /*
698: * If error occurred on mux client, append to
699: * their stderr.
700: */
701: if (tochan) {
702: buffer_append(&c->extended, errmsg,
703: strlen(errmsg));
704: } else
1.190 djm 705: error("%s", errmsg);
1.234 djm 706: if (cr->action == CONFIRM_TTY) {
707: /*
708: * If a TTY allocation error occurred, then arrange
709: * for the correct TTY to leave raw mode.
710: */
711: if (c->self == session_ident)
712: leave_raw_mode(0);
713: else
714: mux_tty_alloc_failed(c);
715: } else if (cr->action == CONFIRM_CLOSE) {
1.190 djm 716: chan_read_failed(c);
717: chan_write_failed(c);
718: }
719: }
1.250 djm 720: free(cr);
1.190 djm 721: }
722:
723: static void
724: client_abandon_status_confirm(Channel *c, void *ctx)
725: {
1.250 djm 726: free(ctx);
1.190 djm 727: }
1.129 deraadt 728:
1.236 djm 729: void
1.234 djm 730: client_expect_confirm(int id, const char *request,
731: enum confirm_action action)
1.190 djm 732: {
1.255 djm 733: struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
1.123 djm 734:
1.190 djm 735: cr->request_type = request;
1.234 djm 736: cr->action = action;
1.123 djm 737:
1.190 djm 738: channel_register_status_confirm(id, client_status_confirm,
739: client_abandon_status_confirm, cr);
1.196 djm 740: }
741:
742: void
743: client_register_global_confirm(global_confirm_cb *cb, void *ctx)
744: {
1.201 djm 745: struct global_confirm *gc, *last_gc;
1.196 djm 746:
747: /* Coalesce identical callbacks */
1.201 djm 748: last_gc = TAILQ_LAST(&global_confirms, global_confirms);
749: if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
750: if (++last_gc->ref_count >= INT_MAX)
751: fatal("%s: last_gc->ref_count = %d",
752: __func__, last_gc->ref_count);
1.196 djm 753: return;
754: }
755:
1.255 djm 756: gc = xcalloc(1, sizeof(*gc));
1.196 djm 757: gc->cb = cb;
758: gc->ctx = ctx;
759: gc->ref_count = 1;
760: TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
1.123 djm 761: }
762:
763: static void
1.99 markus 764: process_cmdline(void)
1.97 jakob 765: {
766: void (*handler)(int);
1.261 millert 767: char *s, *cmd;
768: int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
769: struct Forward fwd;
1.97 jakob 770:
1.257 tedu 771: memset(&fwd, 0, sizeof(fwd));
1.183 djm 772:
1.233 djm 773: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.101 markus 774: handler = signal(SIGINT, SIG_IGN);
1.99 markus 775: cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
1.97 jakob 776: if (s == NULL)
777: goto out;
1.256 deraadt 778: while (isspace((u_char)*s))
1.97 jakob 779: s++;
1.121 djm 780: if (*s == '-')
781: s++; /* Skip cmdline '-', if any */
1.120 dtucker 782: if (*s == '\0')
1.97 jakob 783: goto out;
1.121 djm 784:
1.122 djm 785: if (*s == 'h' || *s == 'H' || *s == '?') {
1.121 djm 786: logit("Commands:");
1.164 djm 787: logit(" -L[bind_address:]port:host:hostport "
788: "Request local forward");
789: logit(" -R[bind_address:]port:host:hostport "
790: "Request remote forward");
1.204 stevesk 791: logit(" -D[bind_address:]port "
792: "Request dynamic forward");
1.237 markus 793: logit(" -KL[bind_address:]port "
794: "Cancel local forward");
1.165 stevesk 795: logit(" -KR[bind_address:]port "
1.164 djm 796: "Cancel remote forward");
1.237 markus 797: logit(" -KD[bind_address:]port "
798: "Cancel dynamic forward");
1.146 reyk 799: if (!options.permit_local_command)
800: goto out;
1.164 djm 801: logit(" !args "
802: "Execute local command");
1.146 reyk 803: goto out;
804: }
805:
806: if (*s == '!' && options.permit_local_command) {
807: s++;
808: ssh_local_cmd(s);
1.121 djm 809: goto out;
810: }
811:
812: if (*s == 'K') {
813: delete = 1;
814: s++;
815: }
1.204 stevesk 816: if (*s == 'L')
817: local = 1;
818: else if (*s == 'R')
819: remote = 1;
820: else if (*s == 'D')
821: dynamic = 1;
822: else {
1.109 itojun 823: logit("Invalid command.");
1.97 jakob 824: goto out;
825: }
1.204 stevesk 826:
1.256 deraadt 827: while (isspace((u_char)*++s))
1.179 tedu 828: ;
1.97 jakob 829:
1.217 djm 830: /* XXX update list of forwards in options */
1.121 djm 831: if (delete) {
1.261 millert 832: /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
833: if (!parse_forward(&fwd, s, 1, 0)) {
834: logit("Bad forwarding close specification.");
1.121 djm 835: goto out;
836: }
1.237 markus 837: if (remote)
1.261 millert 838: ok = channel_request_rforward_cancel(&fwd) == 0;
1.237 markus 839: else if (dynamic)
1.261 millert 840: ok = channel_cancel_lport_listener(&fwd,
841: 0, &options.fwd_opts) > 0;
1.237 markus 842: else
1.261 millert 843: ok = channel_cancel_lport_listener(&fwd,
844: CHANNEL_CANCEL_PORT_STATIC,
845: &options.fwd_opts) > 0;
1.237 markus 846: if (!ok) {
1.290 dtucker 847: logit("Unknown port forwarding.");
1.237 markus 848: goto out;
849: }
850: logit("Canceled forwarding.");
1.121 djm 851: } else {
1.209 djm 852: if (!parse_forward(&fwd, s, dynamic, remote)) {
1.121 djm 853: logit("Bad forwarding specification.");
854: goto out;
855: }
1.204 stevesk 856: if (local || dynamic) {
1.261 millert 857: if (!channel_setup_local_fwd_listener(&fwd,
858: &options.fwd_opts)) {
1.121 djm 859: logit("Port forwarding failed.");
860: goto out;
861: }
1.135 djm 862: } else {
1.261 millert 863: if (channel_request_remote_forwarding(&fwd) < 0) {
1.167 markus 864: logit("Port forwarding failed.");
865: goto out;
866: }
1.135 djm 867: }
1.121 djm 868: logit("Forwarding port.");
869: }
870:
1.97 jakob 871: out:
872: signal(SIGINT, handler);
1.233 djm 873: enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.250 djm 874: free(cmd);
875: free(fwd.listen_host);
1.261 millert 876: free(fwd.listen_path);
1.250 djm 877: free(fwd.connect_host);
1.261 millert 878: free(fwd.connect_path);
1.97 jakob 879: }
880:
1.244 dtucker 881: /* reasons to suppress output of an escape command in help output */
882: #define SUPPRESS_NEVER 0 /* never suppress, always show */
1.292 djm 883: #define SUPPRESS_MUXCLIENT 1 /* don't show in mux client sessions */
884: #define SUPPRESS_MUXMASTER 2 /* don't show in mux master sessions */
885: #define SUPPRESS_SYSLOG 4 /* don't show when logging to syslog */
1.244 dtucker 886: struct escape_help_text {
887: const char *cmd;
888: const char *text;
889: unsigned int flags;
890: };
891: static struct escape_help_text esc_txt[] = {
892: {".", "terminate session", SUPPRESS_MUXMASTER},
893: {".", "terminate connection (and any multiplexed sessions)",
894: SUPPRESS_MUXCLIENT},
1.292 djm 895: {"B", "send a BREAK to the remote system", SUPPRESS_NEVER},
1.244 dtucker 896: {"C", "open a command line", SUPPRESS_MUXCLIENT},
1.292 djm 897: {"R", "request rekey", SUPPRESS_NEVER},
1.246 dtucker 898: {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
1.244 dtucker 899: {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
900: {"#", "list forwarded connections", SUPPRESS_NEVER},
901: {"&", "background ssh (when waiting for connections to terminate)",
902: SUPPRESS_MUXCLIENT},
903: {"?", "this message", SUPPRESS_NEVER},
904: };
905:
906: static void
1.292 djm 907: print_escape_help(Buffer *b, int escape_char, int mux_client, int using_stderr)
1.244 dtucker 908: {
909: unsigned int i, suppress_flags;
910: char string[1024];
911:
912: snprintf(string, sizeof string, "%c?\r\n"
913: "Supported escape sequences:\r\n", escape_char);
914: buffer_append(b, string, strlen(string));
915:
1.292 djm 916: suppress_flags =
1.244 dtucker 917: (mux_client ? SUPPRESS_MUXCLIENT : 0) |
918: (mux_client ? 0 : SUPPRESS_MUXMASTER) |
919: (using_stderr ? 0 : SUPPRESS_SYSLOG);
920:
921: for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
922: if (esc_txt[i].flags & suppress_flags)
923: continue;
1.246 dtucker 924: snprintf(string, sizeof string, " %c%-3s - %s\r\n",
1.244 dtucker 925: escape_char, esc_txt[i].cmd, esc_txt[i].text);
926: buffer_append(b, string, strlen(string));
927: }
928:
929: snprintf(string, sizeof string,
1.246 dtucker 930: " %c%c - send the escape character by typing it twice\r\n"
1.244 dtucker 931: "(Note that escapes are only recognized immediately after "
932: "newline.)\r\n", escape_char, escape_char);
933: buffer_append(b, string, strlen(string));
934: }
935:
1.195 djm 936: /*
937: * Process the characters one by one, call with c==NULL for proto1 case.
938: */
1.77 itojun 939: static int
1.195 djm 940: process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
941: char *buf, int len)
1.31 markus 942: {
1.32 markus 943: char string[1024];
1.31 markus 944: pid_t pid;
945: int bytes = 0;
1.42 markus 946: u_int i;
947: u_char ch;
1.31 markus 948: char *s;
1.294 ! djm 949: struct escape_filter_ctx *efc = c->filter_ctx == NULL ?
! 950: NULL : (struct escape_filter_ctx *)c->filter_ctx;
1.31 markus 951:
1.294 ! djm 952: if (c->filter_ctx == NULL)
! 953: return 0;
1.195 djm 954:
1.139 djm 955: if (len <= 0)
956: return (0);
957:
958: for (i = 0; i < (u_int)len; i++) {
1.31 markus 959: /* Get one character at a time. */
960: ch = buf[i];
961:
1.294 ! djm 962: if (efc->escape_pending) {
1.31 markus 963: /* We have previously seen an escape character. */
964: /* Clear the flag now. */
1.294 ! djm 965: efc->escape_pending = 0;
1.31 markus 966:
967: /* Process the escaped character. */
968: switch (ch) {
969: case '.':
970: /* Terminate the connection. */
1.195 djm 971: snprintf(string, sizeof string, "%c.\r\n",
1.294 ! djm 972: efc->escape_char);
1.32 markus 973: buffer_append(berr, string, strlen(string));
1.31 markus 974:
1.217 djm 975: if (c && c->ctl_chan != -1) {
1.195 djm 976: chan_read_failed(c);
977: chan_write_failed(c);
1.252 dtucker 978: if (c->detach_user)
979: c->detach_user(c->self, NULL);
1.253 dtucker 980: c->type = SSH_CHANNEL_ABANDONED;
981: buffer_clear(&c->input);
982: chan_ibuf_empty(c);
1.195 djm 983: return 0;
984: } else
985: quit_pending = 1;
1.31 markus 986: return -1;
987:
988: case 'Z' - 64:
1.195 djm 989: /* XXX support this for mux clients */
1.217 djm 990: if (c && c->ctl_chan != -1) {
1.245 dtucker 991: char b[16];
1.195 djm 992: noescape:
1.245 dtucker 993: if (ch == 'Z' - 64)
994: snprintf(b, sizeof b, "^Z");
995: else
996: snprintf(b, sizeof b, "%c", ch);
1.195 djm 997: snprintf(string, sizeof string,
1.245 dtucker 998: "%c%s escape not available to "
1.195 djm 999: "multiplexed sessions\r\n",
1.294 ! djm 1000: efc->escape_char, b);
1.195 djm 1001: buffer_append(berr, string,
1002: strlen(string));
1003: continue;
1004: }
1.197 djm 1005: /* Suspend the program. Inform the user */
1.195 djm 1006: snprintf(string, sizeof string,
1.294 ! djm 1007: "%c^Z [suspend ssh]\r\n", efc->escape_char);
1.32 markus 1008: buffer_append(berr, string, strlen(string));
1.31 markus 1009:
1010: /* Restore terminal modes and suspend. */
1011: client_suspend_self(bin, bout, berr);
1012:
1013: /* We have been continued. */
1014: continue;
1015:
1.111 markus 1016: case 'B':
1.292 djm 1017: snprintf(string, sizeof string,
1.294 ! djm 1018: "%cB\r\n", efc->escape_char);
1.292 djm 1019: buffer_append(berr, string, strlen(string));
1020: channel_request_start(c->self, "break", 0);
1021: packet_put_int(1000);
1022: packet_send();
1.111 markus 1023: continue;
1024:
1.54 markus 1025: case 'R':
1.292 djm 1026: if (datafellows & SSH_BUG_NOREKEY)
1027: logit("Server does not "
1028: "support re-keying");
1029: else
1030: need_rekeying = 1;
1.54 markus 1031: continue;
1032:
1.242 dtucker 1033: case 'V':
1034: /* FALLTHROUGH */
1035: case 'v':
1036: if (c && c->ctl_chan != -1)
1037: goto noescape;
1038: if (!log_is_on_stderr()) {
1039: snprintf(string, sizeof string,
1040: "%c%c [Logging to syslog]\r\n",
1.294 ! djm 1041: efc->escape_char, ch);
1.242 dtucker 1042: buffer_append(berr, string,
1043: strlen(string));
1044: continue;
1045: }
1046: if (ch == 'V' && options.log_level >
1047: SYSLOG_LEVEL_QUIET)
1048: log_change_level(--options.log_level);
1049: if (ch == 'v' && options.log_level <
1050: SYSLOG_LEVEL_DEBUG3)
1051: log_change_level(++options.log_level);
1052: snprintf(string, sizeof string,
1.294 ! djm 1053: "%c%c [LogLevel %s]\r\n",
! 1054: efc->escape_char, ch,
1.242 dtucker 1055: log_level_name(options.log_level));
1056: buffer_append(berr, string, strlen(string));
1057: continue;
1058:
1.31 markus 1059: case '&':
1.217 djm 1060: if (c && c->ctl_chan != -1)
1.195 djm 1061: goto noescape;
1.31 markus 1062: /*
1.197 djm 1063: * Detach the program (continue to serve
1064: * connections, but put in background and no
1065: * more new connections).
1.31 markus 1066: */
1067: /* Restore tty modes. */
1.233 djm 1068: leave_raw_mode(
1069: options.request_tty == REQUEST_TTY_FORCE);
1.31 markus 1070:
1071: /* Stop listening for new connections. */
1.86 markus 1072: channel_stop_listening();
1.31 markus 1073:
1.86 markus 1074: snprintf(string, sizeof string,
1.294 ! djm 1075: "%c& [backgrounded]\n", efc->escape_char);
1.86 markus 1076: buffer_append(berr, string, strlen(string));
1.31 markus 1077:
1078: /* Fork into background. */
1079: pid = fork();
1080: if (pid < 0) {
1081: error("fork: %.100s", strerror(errno));
1082: continue;
1083: }
1084: if (pid != 0) { /* This is the parent. */
1085: /* The parent just exits. */
1086: exit(0);
1087: }
1088: /* The child continues serving connections. */
1.292 djm 1089: buffer_append(bin, "\004", 1);
1090: /* fake EOF on stdin */
1091: return -1;
1.31 markus 1092: case '?':
1.294 ! djm 1093: print_escape_help(berr, efc->escape_char,
1.244 dtucker 1094: (c && c->ctl_chan != -1),
1095: log_is_on_stderr());
1.31 markus 1096: continue;
1097:
1098: case '#':
1.195 djm 1099: snprintf(string, sizeof string, "%c#\r\n",
1.294 ! djm 1100: efc->escape_char);
1.32 markus 1101: buffer_append(berr, string, strlen(string));
1.31 markus 1102: s = channel_open_message();
1103: buffer_append(berr, s, strlen(s));
1.250 djm 1104: free(s);
1.97 jakob 1105: continue;
1106:
1107: case 'C':
1.217 djm 1108: if (c && c->ctl_chan != -1)
1.206 djm 1109: goto noescape;
1.99 markus 1110: process_cmdline();
1.31 markus 1111: continue;
1112:
1113: default:
1.294 ! djm 1114: if (ch != efc->escape_char) {
! 1115: buffer_put_char(bin, efc->escape_char);
1.31 markus 1116: bytes++;
1117: }
1118: /* Escaped characters fall through here */
1119: break;
1120: }
1121: } else {
1122: /*
1.197 djm 1123: * The previous character was not an escape char.
1124: * Check if this is an escape.
1.31 markus 1125: */
1.294 ! djm 1126: if (last_was_cr && ch == efc->escape_char) {
1.197 djm 1127: /*
1128: * It is. Set the flag and continue to
1129: * next character.
1130: */
1.294 ! djm 1131: efc->escape_pending = 1;
1.31 markus 1132: continue;
1133: }
1134: }
1135:
1136: /*
1137: * Normal character. Record whether it was a newline,
1138: * and append it to the buffer.
1139: */
1140: last_was_cr = (ch == '\r' || ch == '\n');
1141: buffer_put_char(bin, ch);
1142: bytes++;
1143: }
1144: return bytes;
1145: }
1146:
1.13 markus 1147: /*
1.15 markus 1148: * Get packets from the connection input buffer, and process them as long as
1149: * there are packets available.
1150: *
1151: * Any unknown packets received during the actual
1152: * session cause the session to terminate. This is
1153: * intended to make debugging easier since no
1154: * confirmations are sent. Any compatible protocol
1155: * extensions must be negotiated during the
1156: * preparatory phase.
1157: */
1158:
1.77 itojun 1159: static void
1.49 itojun 1160: client_process_buffered_input_packets(void)
1.15 markus 1161: {
1.265 markus 1162: dispatch_run(DISPATCH_NONBLOCK, &quit_pending, active_state);
1.15 markus 1163: }
1164:
1.31 markus 1165: /* scan buf[] for '~' before sending data to the peer */
1.30 markus 1166:
1.195 djm 1167: /* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1168: void *
1169: client_new_escape_filter_ctx(int escape_char)
1170: {
1171: struct escape_filter_ctx *ret;
1172:
1.255 djm 1173: ret = xcalloc(1, sizeof(*ret));
1.195 djm 1174: ret->escape_pending = 0;
1175: ret->escape_char = escape_char;
1176: return (void *)ret;
1177: }
1178:
1.198 djm 1179: /* Free the escape filter context on channel free */
1180: void
1181: client_filter_cleanup(int cid, void *ctx)
1182: {
1.250 djm 1183: free(ctx);
1.198 djm 1184: }
1185:
1.195 djm 1186: int
1187: client_simple_escape_filter(Channel *c, char *buf, int len)
1.30 markus 1188: {
1.190 djm 1189: if (c->extended_usage != CHAN_EXTENDED_WRITE)
1190: return 0;
1191:
1.195 djm 1192: return process_escapes(c, &c->input, &c->output, &c->extended,
1193: buf, len);
1.30 markus 1194: }
1195:
1.77 itojun 1196: static void
1.60 markus 1197: client_channel_closed(int id, void *arg)
1198: {
1.83 markus 1199: channel_cancel_cleanup(id);
1.60 markus 1200: session_closed = 1;
1.233 djm 1201: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.60 markus 1202: }
1203:
1.15 markus 1204: /*
1.13 markus 1205: * Implements the interactive session with the server. This is called after
1206: * the user has been authenticated, and a command has been started on the
1.72 stevesk 1207: * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1208: * used as an escape character for terminating or suspending the session.
1.13 markus 1209: */
1.1 deraadt 1210:
1.20 markus 1211: int
1.30 markus 1212: client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
1.1 deraadt 1213: {
1.46 markus 1214: fd_set *readset = NULL, *writeset = NULL;
1.11 markus 1215: double start_time, total_time;
1.284 djm 1216: int r, max_fd = 0, max_fd2 = 0, len;
1.200 markus 1217: u_int64_t ibytes, obytes;
1.130 avsm 1218: u_int nalloc = 0;
1.11 markus 1219: char buf[100];
1220:
1221: debug("Entering interactive session.");
1.277 semarie 1222:
1.278 semarie 1223: if (options.control_master &&
1.286 djm 1224: !option_clear_or_none(options.control_path)) {
1.278 semarie 1225: debug("pledge: id");
1.285 tb 1226: if (pledge("stdio rpath wpath cpath unix inet dns recvfd proc exec id tty",
1.278 semarie 1227: NULL) == -1)
1228: fatal("%s pledge(): %s", __func__, strerror(errno));
1229:
1230: } else if (options.forward_x11 || options.permit_local_command) {
1.277 semarie 1231: debug("pledge: exec");
1232: if (pledge("stdio rpath wpath cpath unix inet dns proc exec tty",
1233: NULL) == -1)
1234: fatal("%s pledge(): %s", __func__, strerror(errno));
1235:
1236: } else if (options.update_hostkeys) {
1237: debug("pledge: filesystem full");
1238: if (pledge("stdio rpath wpath cpath unix inet dns proc tty",
1239: NULL) == -1)
1240: fatal("%s pledge(): %s", __func__, strerror(errno));
1241:
1.286 djm 1242: } else if (!option_clear_or_none(options.proxy_command) ||
1243: fork_after_authentication_flag) {
1.277 semarie 1244: debug("pledge: proc");
1245: if (pledge("stdio cpath unix inet dns proc tty", NULL) == -1)
1246: fatal("%s pledge(): %s", __func__, strerror(errno));
1247:
1248: } else {
1249: debug("pledge: network");
1250: if (pledge("stdio unix inet dns tty", NULL) == -1)
1251: fatal("%s pledge(): %s", __func__, strerror(errno));
1252: }
1.11 markus 1253:
1254: start_time = get_current_time();
1255:
1256: /* Initialize variables. */
1257: last_was_cr = 1;
1258: exit_status = -1;
1259: connection_in = packet_get_connection_in();
1260: connection_out = packet_get_connection_out();
1.287 deraadt 1261: max_fd = MAXIMUM(connection_in, connection_out);
1.46 markus 1262:
1.11 markus 1263: quit_pending = 0;
1264:
1265: /* Initialize buffers. */
1266: buffer_init(&stderr_buffer);
1267:
1.15 markus 1268: client_init_dispatch();
1269:
1.105 markus 1270: /*
1271: * Set signal handlers, (e.g. to restore non-blocking mode)
1272: * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1273: */
1.131 djm 1274: if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1275: signal(SIGHUP, signal_handler);
1.105 markus 1276: if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1277: signal(SIGINT, signal_handler);
1278: if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1279: signal(SIGQUIT, signal_handler);
1280: if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1281: signal(SIGTERM, signal_handler);
1.132 djm 1282: signal(SIGWINCH, window_change_handler);
1.11 markus 1283:
1284: if (have_pty)
1.233 djm 1285: enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.11 markus 1286:
1.292 djm 1287: session_ident = ssh2_chan_id;
1288: if (session_ident != -1) {
1289: if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1290: channel_register_filter(session_ident,
1291: client_simple_escape_filter, NULL,
1292: client_filter_cleanup,
1293: client_new_escape_filter_ctx(
1294: escape_char_arg));
1.232 djm 1295: }
1.292 djm 1296: channel_register_cleanup(session_ident,
1297: client_channel_closed, 0);
1.48 markus 1298: }
1.30 markus 1299:
1.11 markus 1300: /* Main loop of the client for the interactive session mode. */
1301: while (!quit_pending) {
1302:
1.13 markus 1303: /* Process buffered packets sent by the server. */
1.11 markus 1304: client_process_buffered_input_packets();
1305:
1.292 djm 1306: if (session_closed && !channel_still_open())
1.60 markus 1307: break;
1308:
1.284 djm 1309: if (ssh_packet_is_rekeying(active_state)) {
1.56 markus 1310: debug("rekeying in progress");
1.284 djm 1311: } else if (need_rekeying) {
1312: /* manual rekey request */
1313: debug("need rekeying");
1314: if ((r = kex_start_rekex(active_state)) != 0)
1315: fatal("%s: kex_start_rekex: %s", __func__,
1316: ssh_err(r));
1317: need_rekeying = 0;
1.56 markus 1318: } else {
1319: /*
1320: * Make packets from buffered channel data, and
1321: * enqueue them for sending to the server.
1322: */
1323: if (packet_not_very_much_data_to_write())
1324: channel_output_poll();
1.11 markus 1325:
1.56 markus 1326: /*
1327: * Check if the window size has changed, and buffer a
1328: * message about it to the server if so.
1329: */
1330: client_check_window_change();
1.11 markus 1331:
1.56 markus 1332: if (quit_pending)
1333: break;
1334: }
1.13 markus 1335: /*
1336: * Wait until we have something to do (something becomes
1337: * available on one of the descriptors).
1338: */
1.81 markus 1339: max_fd2 = max_fd;
1.56 markus 1340: client_wait_until_can_do_something(&readset, &writeset,
1.284 djm 1341: &max_fd2, &nalloc, ssh_packet_is_rekeying(active_state));
1.11 markus 1342:
1343: if (quit_pending)
1344: break;
1345:
1.56 markus 1346: /* Do channel operations unless rekeying in progress. */
1.284 djm 1347: if (!ssh_packet_is_rekeying(active_state))
1.56 markus 1348: channel_after_select(readset, writeset);
1.11 markus 1349:
1.17 markus 1350: /* Buffer input from the connection. */
1.46 markus 1351: client_process_net_input(readset);
1.17 markus 1352:
1353: if (quit_pending)
1354: break;
1.11 markus 1355:
1.197 djm 1356: /*
1357: * Send as much buffered packet data as possible to the
1358: * sender.
1359: */
1.46 markus 1360: if (FD_ISSET(connection_out, writeset))
1.11 markus 1361: packet_write_poll();
1.222 djm 1362:
1363: /*
1364: * If we are a backgrounded control master, and the
1365: * timeout has expired without any active client
1366: * connections, then quit.
1367: */
1368: if (control_persist_exit_time > 0) {
1.251 dtucker 1369: if (monotime() >= control_persist_exit_time) {
1.222 djm 1370: debug("ControlPersist timeout expired");
1371: break;
1372: }
1373: }
1.11 markus 1374: }
1.250 djm 1375: free(readset);
1376: free(writeset);
1.11 markus 1377:
1378: /* Terminate the session. */
1379:
1380: /* Stop watching for window change. */
1.132 djm 1381: signal(SIGWINCH, SIG_DFL);
1.211 andreas 1382:
1.292 djm 1383: packet_start(SSH2_MSG_DISCONNECT);
1384: packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1385: packet_put_cstring("disconnected by user");
1386: packet_put_cstring(""); /* language tag */
1387: packet_send();
1388: packet_write_wait();
1.11 markus 1389:
1.76 markus 1390: channel_free_all();
1.11 markus 1391:
1.75 markus 1392: if (have_pty)
1.233 djm 1393: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.75 markus 1394:
1395: /* restore blocking io */
1396: if (!isatty(fileno(stdin)))
1397: unset_nonblock(fileno(stdin));
1398: if (!isatty(fileno(stdout)))
1399: unset_nonblock(fileno(stdout));
1400: if (!isatty(fileno(stderr)))
1401: unset_nonblock(fileno(stderr));
1.116 dtucker 1402:
1403: /*
1404: * If there was no shell or command requested, there will be no remote
1405: * exit status to be returned. In that case, clear error code if the
1406: * connection was deliberately terminated at this end.
1407: */
1408: if (no_shell_flag && received_signal == SIGTERM) {
1409: received_signal = 0;
1410: exit_status = 0;
1411: }
1.75 markus 1412:
1.113 markus 1413: if (received_signal)
1.89 itojun 1414: fatal("Killed by signal %d.", (int) received_signal);
1.75 markus 1415:
1.13 markus 1416: /*
1417: * In interactive mode (with pseudo tty) display a message indicating
1418: * that the connection has been closed.
1419: */
1.11 markus 1420: if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1.197 djm 1421: snprintf(buf, sizeof buf,
1422: "Connection to %.64s closed.\r\n", host);
1.11 markus 1423: buffer_append(&stderr_buffer, buf, strlen(buf));
1424: }
1.70 markus 1425:
1.11 markus 1426: /* Output any buffered data for stderr. */
1.231 djm 1427: if (buffer_len(&stderr_buffer) > 0) {
1.230 djm 1428: len = atomicio(vwrite, fileno(stderr),
1429: buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
1.231 djm 1430: if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
1.11 markus 1431: error("Write failed flushing stderr buffer.");
1.231 djm 1432: else
1433: buffer_consume(&stderr_buffer, len);
1.11 markus 1434: }
1435:
1436: /* Clear and free any buffers. */
1.281 jsg 1437: explicit_bzero(buf, sizeof(buf));
1.11 markus 1438: buffer_free(&stderr_buffer);
1439:
1440: /* Report bytes transferred, and transfer rates. */
1441: total_time = get_current_time() - start_time;
1.263 markus 1442: packet_get_bytes(&ibytes, &obytes);
1.200 markus 1443: verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
1.229 djm 1444: (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
1.11 markus 1445: if (total_time > 0)
1.200 markus 1446: verbose("Bytes per second: sent %.1f, received %.1f",
1447: obytes / total_time, ibytes / total_time);
1.11 markus 1448: /* Return the exit status of the program. */
1449: debug("Exit status %d", exit_status);
1450: return exit_status;
1.15 markus 1451: }
1452:
1453: /*********/
1454:
1.77 itojun 1455: static Channel *
1.289 markus 1456: client_request_forwarded_tcpip(const char *request_type, int rchan,
1457: u_int rwindow, u_int rmaxpack)
1.40 markus 1458: {
1.103 deraadt 1459: Channel *c = NULL;
1.289 markus 1460: struct sshbuf *b = NULL;
1.40 markus 1461: char *listen_address, *originator_address;
1.208 djm 1462: u_short listen_port, originator_port;
1.289 markus 1463: int r;
1.40 markus 1464:
1465: /* Get rest of the packet */
1466: listen_address = packet_get_string(NULL);
1467: listen_port = packet_get_int();
1468: originator_address = packet_get_string(NULL);
1469: originator_port = packet_get_int();
1.93 markus 1470: packet_check_eom();
1.40 markus 1471:
1.261 millert 1472: debug("%s: listen %s port %d, originator %s port %d", __func__,
1473: listen_address, listen_port, originator_address, originator_port);
1.191 djm 1474:
1.260 markus 1475: c = channel_connect_by_listen_address(listen_address, listen_port,
1.191 djm 1476: "forwarded-tcpip", originator_address);
1.40 markus 1477:
1.289 markus 1478: if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1479: if ((b = sshbuf_new()) == NULL) {
1480: error("%s: alloc reply", __func__);
1481: goto out;
1482: }
1483: /* reconstruct and send to muxclient */
1484: if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
1485: (r = sshbuf_put_u8(b, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1486: (r = sshbuf_put_cstring(b, request_type)) != 0 ||
1487: (r = sshbuf_put_u32(b, rchan)) != 0 ||
1488: (r = sshbuf_put_u32(b, rwindow)) != 0 ||
1489: (r = sshbuf_put_u32(b, rmaxpack)) != 0 ||
1490: (r = sshbuf_put_cstring(b, listen_address)) != 0 ||
1491: (r = sshbuf_put_u32(b, listen_port)) != 0 ||
1492: (r = sshbuf_put_cstring(b, originator_address)) != 0 ||
1493: (r = sshbuf_put_u32(b, originator_port)) != 0 ||
1494: (r = sshbuf_put_stringb(&c->output, b)) != 0) {
1495: error("%s: compose for muxclient %s", __func__,
1496: ssh_err(r));
1497: goto out;
1498: }
1499: }
1500:
1501: out:
1502: sshbuf_free(b);
1.250 djm 1503: free(originator_address);
1504: free(listen_address);
1.40 markus 1505: return c;
1506: }
1507:
1.103 deraadt 1508: static Channel *
1.261 millert 1509: client_request_forwarded_streamlocal(const char *request_type, int rchan)
1510: {
1511: Channel *c = NULL;
1512: char *listen_path;
1513:
1514: /* Get the remote path. */
1515: listen_path = packet_get_string(NULL);
1516: /* XXX: Skip reserved field for now. */
1517: if (packet_get_string_ptr(NULL) == NULL)
1518: fatal("%s: packet_get_string_ptr failed", __func__);
1519: packet_check_eom();
1520:
1521: debug("%s: %s", __func__, listen_path);
1522:
1523: c = channel_connect_by_listen_path(listen_path,
1524: "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1525: free(listen_path);
1526: return c;
1527: }
1528:
1529: static Channel *
1.40 markus 1530: client_request_x11(const char *request_type, int rchan)
1531: {
1532: Channel *c = NULL;
1533: char *originator;
1.208 djm 1534: u_short originator_port;
1.67 markus 1535: int sock;
1.40 markus 1536:
1537: if (!options.forward_x11) {
1538: error("Warning: ssh server tried X11 forwarding.");
1.197 djm 1539: error("Warning: this is probably a break-in attempt by a "
1540: "malicious server.");
1.221 djm 1541: return NULL;
1542: }
1.274 djm 1543: if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
1.221 djm 1544: verbose("Rejected X11 connection after ForwardX11Timeout "
1545: "expired");
1.40 markus 1546: return NULL;
1547: }
1548: originator = packet_get_string(NULL);
1549: if (datafellows & SSH_BUG_X11FWD) {
1550: debug2("buggy server: x11 request w/o originator_port");
1551: originator_port = 0;
1552: } else {
1553: originator_port = packet_get_int();
1554: }
1.93 markus 1555: packet_check_eom();
1.40 markus 1556: /* XXX check permission */
1.47 markus 1557: debug("client_request_x11: request from %s %d", originator,
1558: originator_port);
1.250 djm 1559: free(originator);
1.40 markus 1560: sock = x11_connect_display();
1.67 markus 1561: if (sock < 0)
1562: return NULL;
1563: c = channel_new("x11",
1564: SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1.110 markus 1565: CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
1.82 markus 1566: c->force_drain = 1;
1.40 markus 1567: return c;
1568: }
1569:
1.103 deraadt 1570: static Channel *
1.40 markus 1571: client_request_agent(const char *request_type, int rchan)
1572: {
1573: Channel *c = NULL;
1.262 djm 1574: int r, sock;
1.40 markus 1575:
1576: if (!options.forward_agent) {
1577: error("Warning: ssh server tried agent forwarding.");
1.197 djm 1578: error("Warning: this is probably a break-in attempt by a "
1579: "malicious server.");
1.40 markus 1580: return NULL;
1581: }
1.262 djm 1582: if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1583: if (r != SSH_ERR_AGENT_NOT_PRESENT)
1584: debug("%s: ssh_get_authentication_socket: %s",
1585: __func__, ssh_err(r));
1.67 markus 1586: return NULL;
1.262 djm 1587: }
1.67 markus 1588: c = channel_new("authentication agent connection",
1589: SSH_CHANNEL_OPEN, sock, sock, -1,
1.185 dtucker 1590: CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
1.110 markus 1591: "authentication agent connection", 1);
1.82 markus 1592: c->force_drain = 1;
1.40 markus 1593: return c;
1.180 djm 1594: }
1595:
1596: int
1597: client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1598: {
1599: Channel *c;
1600: int fd;
1601:
1602: if (tun_mode == SSH_TUNMODE_NO)
1603: return 0;
1604:
1605: debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1606:
1607: /* Open local tunnel device */
1608: if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1609: error("Tunnel device open failed.");
1610: return -1;
1611: }
1612:
1613: c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1614: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1615: c->datagram = 1;
1616:
1617: packet_start(SSH2_MSG_CHANNEL_OPEN);
1618: packet_put_cstring("tun@openssh.com");
1619: packet_put_int(c->self);
1620: packet_put_int(c->local_window_max);
1621: packet_put_int(c->local_maxpacket);
1622: packet_put_int(tun_mode);
1623: packet_put_int(remote_tun);
1624: packet_send();
1625:
1626: return 0;
1.40 markus 1627: }
1628:
1.22 markus 1629: /* XXXX move to generic input handler */
1.264 markus 1630: static int
1.94 markus 1631: client_input_channel_open(int type, u_int32_t seq, void *ctxt)
1.22 markus 1632: {
1633: Channel *c = NULL;
1634: char *ctype;
1635: int rchan;
1.102 markus 1636: u_int rmaxpack, rwindow, len;
1.22 markus 1637:
1638: ctype = packet_get_string(&len);
1639: rchan = packet_get_int();
1640: rwindow = packet_get_int();
1641: rmaxpack = packet_get_int();
1642:
1.24 markus 1643: debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
1.22 markus 1644: ctype, rchan, rwindow, rmaxpack);
1645:
1.40 markus 1646: if (strcmp(ctype, "forwarded-tcpip") == 0) {
1.289 markus 1647: c = client_request_forwarded_tcpip(ctype, rchan, rwindow,
1648: rmaxpack);
1.261 millert 1649: } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
1650: c = client_request_forwarded_streamlocal(ctype, rchan);
1.40 markus 1651: } else if (strcmp(ctype, "x11") == 0) {
1652: c = client_request_x11(ctype, rchan);
1653: } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1654: c = client_request_agent(ctype, rchan);
1.22 markus 1655: }
1.289 markus 1656: if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1657: debug3("proxied to downstream: %s", ctype);
1658: } else if (c != NULL) {
1.22 markus 1659: debug("confirm %s", ctype);
1660: c->remote_id = rchan;
1661: c->remote_window = rwindow;
1662: c->remote_maxpacket = rmaxpack;
1.69 markus 1663: if (c->type != SSH_CHANNEL_CONNECTING) {
1664: packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1665: packet_put_int(c->remote_id);
1666: packet_put_int(c->self);
1667: packet_put_int(c->local_window);
1668: packet_put_int(c->local_maxpacket);
1669: packet_send();
1670: }
1.22 markus 1671: } else {
1672: debug("failure %s", ctype);
1673: packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1674: packet_put_int(rchan);
1675: packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
1.66 markus 1676: if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1.69 markus 1677: packet_put_cstring("open failed");
1.66 markus 1678: packet_put_cstring("");
1679: }
1.22 markus 1680: packet_send();
1681: }
1.250 djm 1682: free(ctype);
1.264 markus 1683: return 0;
1.22 markus 1684: }
1.267 djm 1685:
1.264 markus 1686: static int
1.94 markus 1687: client_input_channel_req(int type, u_int32_t seq, void *ctxt)
1.48 markus 1688: {
1689: Channel *c = NULL;
1.123 djm 1690: int exitval, id, reply, success = 0;
1.48 markus 1691: char *rtype;
1692:
1693: id = packet_get_int();
1.289 markus 1694: c = channel_lookup(id);
1695: if (channel_proxy_upstream(c, type, seq, ctxt))
1696: return 0;
1.48 markus 1697: rtype = packet_get_string(NULL);
1698: reply = packet_get_char();
1699:
1700: debug("client_input_channel_req: channel %d rtype %s reply %d",
1701: id, rtype, reply);
1702:
1.128 djm 1703: if (id == -1) {
1704: error("client_input_channel_req: request for channel -1");
1.289 markus 1705: } else if (c == NULL) {
1.197 djm 1706: error("client_input_channel_req: channel %d: "
1707: "unknown channel", id);
1.193 markus 1708: } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1709: packet_check_eom();
1710: chan_rcvd_eow(c);
1.48 markus 1711: } else if (strcmp(rtype, "exit-status") == 0) {
1.123 djm 1712: exitval = packet_get_int();
1.217 djm 1713: if (c->ctl_chan != -1) {
1714: mux_exit_message(c, exitval);
1.215 djm 1715: success = 1;
1716: } else if (id == session_ident) {
1717: /* Record exit value of local session */
1.123 djm 1718: success = 1;
1719: exit_status = exitval;
1.215 djm 1720: } else {
1.218 djm 1721: /* Probably for a mux channel that has already closed */
1722: debug("%s: no sink for exit-status on channel %d",
1723: __func__, id);
1.123 djm 1724: }
1.93 markus 1725: packet_check_eom();
1.48 markus 1726: }
1.259 djm 1727: if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
1.48 markus 1728: packet_start(success ?
1729: SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1.205 markus 1730: packet_put_int(c->remote_id);
1.48 markus 1731: packet_send();
1732: }
1.250 djm 1733: free(rtype);
1.264 markus 1734: return 0;
1.48 markus 1735: }
1.267 djm 1736:
1.269 djm 1737: struct hostkeys_update_ctx {
1738: /* The hostname and (optionally) IP address string for the server */
1739: char *host_str, *ip_str;
1740:
1741: /*
1742: * Keys received from the server and a flag for each indicating
1743: * whether they already exist in known_hosts.
1744: * keys_seen is filled in by hostkeys_find() and later (for new
1745: * keys) by client_global_hostkeys_private_confirm().
1746: */
1747: struct sshkey **keys;
1748: int *keys_seen;
1.294 ! djm 1749: size_t nkeys, nnew;
1.269 djm 1750:
1751: /*
1752: * Keys that are in known_hosts, but were not present in the update
1753: * from the server (i.e. scheduled to be deleted).
1754: * Filled in by hostkeys_find().
1755: */
1756: struct sshkey **old_keys;
1757: size_t nold;
1758: };
1759:
1760: static void
1761: hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
1762: {
1763: size_t i;
1764:
1765: if (ctx == NULL)
1766: return;
1767: for (i = 0; i < ctx->nkeys; i++)
1768: sshkey_free(ctx->keys[i]);
1769: free(ctx->keys);
1770: free(ctx->keys_seen);
1771: for (i = 0; i < ctx->nold; i++)
1772: sshkey_free(ctx->old_keys[i]);
1773: free(ctx->old_keys);
1774: free(ctx->host_str);
1775: free(ctx->ip_str);
1776: free(ctx);
1777: }
1778:
1779: static int
1780: hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
1781: {
1782: struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1783: size_t i;
1784: struct sshkey **tmp;
1785:
1.293 djm 1786: if (l->status != HKF_STATUS_MATCHED || l->key == NULL)
1.269 djm 1787: return 0;
1788:
1789: /* Mark off keys we've already seen for this host */
1790: for (i = 0; i < ctx->nkeys; i++) {
1791: if (sshkey_equal(l->key, ctx->keys[i])) {
1792: debug3("%s: found %s key at %s:%ld", __func__,
1793: sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
1794: ctx->keys_seen[i] = 1;
1795: return 0;
1796: }
1797: }
1798: /* This line contained a key that not offered by the server */
1799: debug3("%s: deprecated %s key at %s:%ld", __func__,
1800: sshkey_ssh_name(l->key), l->path, l->linenum);
1801: if ((tmp = reallocarray(ctx->old_keys, ctx->nold + 1,
1802: sizeof(*ctx->old_keys))) == NULL)
1803: fatal("%s: reallocarray failed nold = %zu",
1804: __func__, ctx->nold);
1805: ctx->old_keys = tmp;
1806: ctx->old_keys[ctx->nold++] = l->key;
1807: l->key = NULL;
1808:
1809: return 0;
1810: }
1811:
1812: static void
1813: update_known_hosts(struct hostkeys_update_ctx *ctx)
1814: {
1.271 djm 1815: int r, was_raw = 0;
1816: int loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ?
1.269 djm 1817: SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
1818: char *fp, *response;
1819: size_t i;
1820:
1821: for (i = 0; i < ctx->nkeys; i++) {
1822: if (ctx->keys_seen[i] != 2)
1823: continue;
1824: if ((fp = sshkey_fingerprint(ctx->keys[i],
1825: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1826: fatal("%s: sshkey_fingerprint failed", __func__);
1827: do_log2(loglevel, "Learned new hostkey: %s %s",
1828: sshkey_type(ctx->keys[i]), fp);
1829: free(fp);
1830: }
1831: for (i = 0; i < ctx->nold; i++) {
1832: if ((fp = sshkey_fingerprint(ctx->old_keys[i],
1833: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1834: fatal("%s: sshkey_fingerprint failed", __func__);
1835: do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
1836: sshkey_type(ctx->old_keys[i]), fp);
1837: free(fp);
1838: }
1839: if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
1.271 djm 1840: if (get_saved_tio() != NULL) {
1841: leave_raw_mode(1);
1842: was_raw = 1;
1843: }
1.269 djm 1844: response = NULL;
1845: for (i = 0; !quit_pending && i < 3; i++) {
1846: free(response);
1847: response = read_passphrase("Accept updated hostkeys? "
1848: "(yes/no): ", RP_ECHO);
1849: if (strcasecmp(response, "yes") == 0)
1850: break;
1851: else if (quit_pending || response == NULL ||
1852: strcasecmp(response, "no") == 0) {
1853: options.update_hostkeys = 0;
1854: break;
1855: } else {
1856: do_log2(loglevel, "Please enter "
1857: "\"yes\" or \"no\"");
1858: }
1859: }
1860: if (quit_pending || i >= 3 || response == NULL)
1861: options.update_hostkeys = 0;
1862: free(response);
1.271 djm 1863: if (was_raw)
1864: enter_raw_mode(1);
1.269 djm 1865: }
1866:
1867: /*
1868: * Now that all the keys are verified, we can go ahead and replace
1869: * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
1870: * cancel the operation).
1871: */
1872: if (options.update_hostkeys != 0 &&
1873: (r = hostfile_replace_entries(options.user_hostfiles[0],
1874: ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys,
1875: options.hash_known_hosts, 0,
1876: options.fingerprint_hash)) != 0)
1877: error("%s: hostfile_replace_entries failed: %s",
1878: __func__, ssh_err(r));
1879: }
1880:
1881: static void
1882: client_global_hostkeys_private_confirm(int type, u_int32_t seq, void *_ctx)
1883: {
1884: struct ssh *ssh = active_state; /* XXX */
1885: struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1886: size_t i, ndone;
1887: struct sshbuf *signdata;
1888: int r;
1889: const u_char *sig;
1890: size_t siglen;
1891:
1892: if (ctx->nnew == 0)
1893: fatal("%s: ctx->nnew == 0", __func__); /* sanity */
1894: if (type != SSH2_MSG_REQUEST_SUCCESS) {
1895: error("Server failed to confirm ownership of "
1896: "private host keys");
1897: hostkeys_update_ctx_free(ctx);
1898: return;
1899: }
1900: if ((signdata = sshbuf_new()) == NULL)
1901: fatal("%s: sshbuf_new failed", __func__);
1902: /* Don't want to accidentally accept an unbound signature */
1903: if (ssh->kex->session_id_len == 0)
1904: fatal("%s: ssh->kex->session_id_len == 0", __func__);
1905: /*
1906: * Expect a signature for each of the ctx->nnew private keys we
1907: * haven't seen before. They will be in the same order as the
1908: * ctx->keys where the corresponding ctx->keys_seen[i] == 0.
1909: */
1910: for (ndone = i = 0; i < ctx->nkeys; i++) {
1911: if (ctx->keys_seen[i])
1912: continue;
1913: /* Prepare data to be signed: session ID, unique string, key */
1914: sshbuf_reset(signdata);
1.270 djm 1915: if ( (r = sshbuf_put_cstring(signdata,
1916: "hostkeys-prove-00@openssh.com")) != 0 ||
1917: (r = sshbuf_put_string(signdata, ssh->kex->session_id,
1.269 djm 1918: ssh->kex->session_id_len)) != 0 ||
1919: (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
1920: fatal("%s: failed to prepare signature: %s",
1921: __func__, ssh_err(r));
1922: /* Extract and verify signature */
1923: if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
1924: error("%s: couldn't parse message: %s",
1925: __func__, ssh_err(r));
1926: goto out;
1927: }
1928: if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
1929: sshbuf_ptr(signdata), sshbuf_len(signdata), 0)) != 0) {
1930: error("%s: server gave bad signature for %s key %zu",
1931: __func__, sshkey_type(ctx->keys[i]), i);
1932: goto out;
1933: }
1934: /* Key is good. Mark it as 'seen' */
1935: ctx->keys_seen[i] = 2;
1936: ndone++;
1937: }
1938: if (ndone != ctx->nnew)
1939: fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__,
1940: ndone, ctx->nnew); /* Shouldn't happen */
1941: ssh_packet_check_eom(ssh);
1942:
1943: /* Make the edits to known_hosts */
1944: update_known_hosts(ctx);
1945: out:
1946: hostkeys_update_ctx_free(ctx);
1947: }
1948:
1.267 djm 1949: /*
1.291 djm 1950: * Returns non-zero if the key is accepted by HostkeyAlgorithms.
1951: * Made slightly less trivial by the multiple RSA signature algorithm names.
1952: */
1953: static int
1954: key_accepted_by_hostkeyalgs(const struct sshkey *key)
1955: {
1956: const char *ktype = sshkey_ssh_name(key);
1957: const char *hostkeyalgs = options.hostkeyalgorithms != NULL ?
1958: options.hostkeyalgorithms : KEX_DEFAULT_PK_ALG;
1959:
1960: if (key == NULL || key->type == KEY_UNSPEC)
1961: return 0;
1962: if (key->type == KEY_RSA &&
1963: (match_pattern_list("rsa-sha2-256", hostkeyalgs, 0) == 1 ||
1964: match_pattern_list("rsa-sha2-512", hostkeyalgs, 0) == 1))
1965: return 1;
1966: return match_pattern_list(ktype, hostkeyalgs, 0) == 1;
1967: }
1968:
1969: /*
1.270 djm 1970: * Handle hostkeys-00@openssh.com global request to inform the client of all
1.267 djm 1971: * the server's hostkeys. The keys are checked against the user's
1972: * HostkeyAlgorithms preference before they are accepted.
1973: */
1974: static int
1975: client_input_hostkeys(void)
1976: {
1.269 djm 1977: struct ssh *ssh = active_state; /* XXX */
1.267 djm 1978: const u_char *blob = NULL;
1.269 djm 1979: size_t i, len = 0;
1.267 djm 1980: struct sshbuf *buf = NULL;
1.269 djm 1981: struct sshkey *key = NULL, **tmp;
1982: int r;
1983: char *fp;
1.267 djm 1984: static int hostkeys_seen = 0; /* XXX use struct ssh */
1.268 djm 1985: extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
1.272 djm 1986: struct hostkeys_update_ctx *ctx = NULL;
1.267 djm 1987:
1988: if (hostkeys_seen)
1989: fatal("%s: server already sent hostkeys", __func__);
1.269 djm 1990: if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
1991: options.batch_mode)
1992: return 1; /* won't ask in batchmode, so don't even try */
1.267 djm 1993: if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
1994: return 1;
1.272 djm 1995:
1996: ctx = xcalloc(1, sizeof(*ctx));
1.269 djm 1997: while (ssh_packet_remaining(ssh) > 0) {
1.267 djm 1998: sshkey_free(key);
1999: key = NULL;
1.269 djm 2000: if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
2001: error("%s: couldn't parse message: %s",
2002: __func__, ssh_err(r));
2003: goto out;
2004: }
1.270 djm 2005: if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
2006: error("%s: parse key: %s", __func__, ssh_err(r));
2007: goto out;
2008: }
1.267 djm 2009: fp = sshkey_fingerprint(key, options.fingerprint_hash,
2010: SSH_FP_DEFAULT);
2011: debug3("%s: received %s key %s", __func__,
2012: sshkey_type(key), fp);
2013: free(fp);
1.275 markus 2014:
1.291 djm 2015: if (!key_accepted_by_hostkeyalgs(key)) {
1.267 djm 2016: debug3("%s: %s key not permitted by HostkeyAlgorithms",
2017: __func__, sshkey_ssh_name(key));
2018: continue;
2019: }
1.269 djm 2020: /* Skip certs */
2021: if (sshkey_is_cert(key)) {
2022: debug3("%s: %s key is a certificate; skipping",
2023: __func__, sshkey_ssh_name(key));
2024: continue;
2025: }
2026: /* Ensure keys are unique */
2027: for (i = 0; i < ctx->nkeys; i++) {
2028: if (sshkey_equal(key, ctx->keys[i])) {
2029: error("%s: received duplicated %s host key",
2030: __func__, sshkey_ssh_name(key));
2031: goto out;
2032: }
2033: }
2034: /* Key is good, record it */
2035: if ((tmp = reallocarray(ctx->keys, ctx->nkeys + 1,
2036: sizeof(*ctx->keys))) == NULL)
2037: fatal("%s: reallocarray failed nkeys = %zu",
2038: __func__, ctx->nkeys);
2039: ctx->keys = tmp;
2040: ctx->keys[ctx->nkeys++] = key;
1.267 djm 2041: key = NULL;
2042: }
2043:
1.269 djm 2044: if (ctx->nkeys == 0) {
1.270 djm 2045: debug("%s: server sent no hostkeys", __func__);
1.267 djm 2046: goto out;
2047: }
1.270 djm 2048:
1.269 djm 2049: if ((ctx->keys_seen = calloc(ctx->nkeys,
2050: sizeof(*ctx->keys_seen))) == NULL)
2051: fatal("%s: calloc failed", __func__);
1.267 djm 2052:
1.268 djm 2053: get_hostfile_hostname_ipaddr(host,
2054: options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
1.269 djm 2055: options.port, &ctx->host_str,
2056: options.check_host_ip ? &ctx->ip_str : NULL);
1.268 djm 2057:
1.269 djm 2058: /* Find which keys we already know about. */
2059: if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find,
2060: ctx, ctx->host_str, ctx->ip_str,
2061: HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) {
2062: error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
1.267 djm 2063: goto out;
2064: }
2065:
1.269 djm 2066: /* Figure out if we have any new keys to add */
2067: ctx->nnew = 0;
2068: for (i = 0; i < ctx->nkeys; i++) {
2069: if (!ctx->keys_seen[i])
2070: ctx->nnew++;
2071: }
2072:
2073: debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove",
2074: __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold);
2075:
2076: if (ctx->nnew == 0 && ctx->nold != 0) {
2077: /* We have some keys to remove. Just do it. */
2078: update_known_hosts(ctx);
2079: } else if (ctx->nnew != 0) {
2080: /*
2081: * We have received hitherto-unseen keys from the server.
2082: * Ask the server to confirm ownership of the private halves.
2083: */
2084: debug3("%s: asking server to prove ownership for %zu keys",
2085: __func__, ctx->nnew);
2086: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2087: (r = sshpkt_put_cstring(ssh,
1.270 djm 2088: "hostkeys-prove-00@openssh.com")) != 0 ||
1.269 djm 2089: (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
2090: fatal("%s: cannot prepare packet: %s",
2091: __func__, ssh_err(r));
2092: if ((buf = sshbuf_new()) == NULL)
2093: fatal("%s: sshbuf_new", __func__);
2094: for (i = 0; i < ctx->nkeys; i++) {
2095: if (ctx->keys_seen[i])
2096: continue;
2097: sshbuf_reset(buf);
2098: if ((r = sshkey_putb(ctx->keys[i], buf)) != 0)
2099: fatal("%s: sshkey_putb: %s",
2100: __func__, ssh_err(r));
2101: if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
2102: fatal("%s: sshpkt_put_string: %s",
2103: __func__, ssh_err(r));
2104: }
2105: if ((r = sshpkt_send(ssh)) != 0)
2106: fatal("%s: sshpkt_send: %s", __func__, ssh_err(r));
2107: client_register_global_confirm(
2108: client_global_hostkeys_private_confirm, ctx);
2109: ctx = NULL; /* will be freed in callback */
2110: }
2111:
1.267 djm 2112: /* Success */
2113: out:
1.269 djm 2114: hostkeys_update_ctx_free(ctx);
1.267 djm 2115: sshkey_free(key);
2116: sshbuf_free(buf);
1.269 djm 2117: /*
2118: * NB. Return success for all cases. The server doesn't need to know
2119: * what the client does with its hosts file.
2120: */
2121: return 1;
1.267 djm 2122: }
2123:
1.264 markus 2124: static int
1.95 markus 2125: client_input_global_request(int type, u_int32_t seq, void *ctxt)
2126: {
2127: char *rtype;
2128: int want_reply;
2129: int success = 0;
2130:
1.267 djm 2131: rtype = packet_get_cstring(NULL);
1.95 markus 2132: want_reply = packet_get_char();
1.117 markus 2133: debug("client_input_global_request: rtype %s want_reply %d",
2134: rtype, want_reply);
1.270 djm 2135: if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
1.267 djm 2136: success = client_input_hostkeys();
1.95 markus 2137: if (want_reply) {
2138: packet_start(success ?
2139: SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2140: packet_send();
2141: packet_write_wait();
2142: }
1.250 djm 2143: free(rtype);
1.264 markus 2144: return 0;
1.95 markus 2145: }
1.22 markus 2146:
1.123 djm 2147: void
1.129 deraadt 2148: client_session2_setup(int id, int want_tty, int want_subsystem,
1.190 djm 2149: const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
1.123 djm 2150: {
2151: int len;
1.132 djm 2152: Channel *c = NULL;
1.123 djm 2153:
2154: debug2("%s: id %d", __func__, id);
2155:
1.132 djm 2156: if ((c = channel_lookup(id)) == NULL)
2157: fatal("client_session2_setup: channel %d: unknown channel", id);
1.224 djm 2158:
2159: packet_set_interactive(want_tty,
2160: options.ip_qos_interactive, options.ip_qos_bulk);
1.132 djm 2161:
1.123 djm 2162: if (want_tty) {
2163: struct winsize ws;
2164:
2165: /* Store window size in the packet. */
2166: if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2167: memset(&ws, 0, sizeof(ws));
2168:
1.190 djm 2169: channel_request_start(id, "pty-req", 1);
1.234 djm 2170: client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
1.123 djm 2171: packet_put_cstring(term != NULL ? term : "");
1.158 deraadt 2172: packet_put_int((u_int)ws.ws_col);
2173: packet_put_int((u_int)ws.ws_row);
2174: packet_put_int((u_int)ws.ws_xpixel);
2175: packet_put_int((u_int)ws.ws_ypixel);
1.194 djm 2176: if (tiop == NULL)
2177: tiop = get_saved_tio();
2178: tty_make_modes(-1, tiop);
1.123 djm 2179: packet_send();
2180: /* XXX wait for reply */
1.132 djm 2181: c->client_tty = 1;
1.123 djm 2182: }
2183:
2184: /* Transfer any environment variables from client to server */
1.126 djm 2185: if (options.num_send_env != 0 && env != NULL) {
1.123 djm 2186: int i, j, matched;
2187: char *name, *val;
2188:
2189: debug("Sending environment.");
1.126 djm 2190: for (i = 0; env[i] != NULL; i++) {
1.123 djm 2191: /* Split */
1.126 djm 2192: name = xstrdup(env[i]);
1.123 djm 2193: if ((val = strchr(name, '=')) == NULL) {
1.250 djm 2194: free(name);
1.123 djm 2195: continue;
2196: }
2197: *val++ = '\0';
2198:
2199: matched = 0;
2200: for (j = 0; j < options.num_send_env; j++) {
2201: if (match_pattern(name, options.send_env[j])) {
2202: matched = 1;
2203: break;
2204: }
2205: }
2206: if (!matched) {
2207: debug3("Ignored env %s", name);
1.250 djm 2208: free(name);
1.123 djm 2209: continue;
2210: }
2211:
2212: debug("Sending env %s = %s", name, val);
2213: channel_request_start(id, "env", 0);
2214: packet_put_cstring(name);
2215: packet_put_cstring(val);
2216: packet_send();
1.250 djm 2217: free(name);
1.123 djm 2218: }
2219: }
2220:
2221: len = buffer_len(cmd);
2222: if (len > 0) {
2223: if (len > 900)
2224: len = 900;
2225: if (want_subsystem) {
1.190 djm 2226: debug("Sending subsystem: %.*s",
2227: len, (u_char*)buffer_ptr(cmd));
2228: channel_request_start(id, "subsystem", 1);
1.234 djm 2229: client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
1.123 djm 2230: } else {
1.190 djm 2231: debug("Sending command: %.*s",
2232: len, (u_char*)buffer_ptr(cmd));
2233: channel_request_start(id, "exec", 1);
1.234 djm 2234: client_expect_confirm(id, "exec", CONFIRM_CLOSE);
1.123 djm 2235: }
2236: packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2237: packet_send();
2238: } else {
1.190 djm 2239: channel_request_start(id, "shell", 1);
1.234 djm 2240: client_expect_confirm(id, "shell", CONFIRM_CLOSE);
1.123 djm 2241: packet_send();
2242: }
2243: }
2244:
1.77 itojun 2245: static void
1.292 djm 2246: client_init_dispatch(void)
1.16 markus 2247: {
1.55 markus 2248: dispatch_init(&dispatch_protocol_error);
1.100 markus 2249:
1.16 markus 2250: dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2251: dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2252: dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2253: dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
1.22 markus 2254: dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
1.16 markus 2255: dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2256: dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1.48 markus 2257: dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
1.16 markus 2258: dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
1.189 djm 2259: dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2260: dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
1.95 markus 2261: dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
1.55 markus 2262:
2263: /* rekeying */
2264: dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
1.100 markus 2265:
2266: /* global request reply messages */
2267: dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2268: dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
1.232 djm 2269: }
2270:
2271: void
2272: client_stop_mux(void)
2273: {
2274: if (options.control_path != NULL && muxserver_sock != -1)
2275: unlink(options.control_path);
2276: /*
1.247 dtucker 2277: * If we are in persist mode, or don't have a shell, signal that we
2278: * should close when all active channels are closed.
1.232 djm 2279: */
1.247 dtucker 2280: if (options.control_persist || no_shell_flag) {
1.232 djm 2281: session_closed = 1;
1.235 djm 2282: setproctitle("[stopped mux]");
2283: }
1.113 markus 2284: }
2285:
2286: /* client specific fatal cleanup */
2287: void
1.114 markus 2288: cleanup_exit(int i)
1.113 markus 2289: {
1.233 djm 2290: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.113 markus 2291: leave_non_blocking();
1.192 djm 2292: if (options.control_path != NULL && muxserver_sock != -1)
1.123 djm 2293: unlink(options.control_path);
1.223 djm 2294: ssh_kill_proxy_command();
1.114 markus 2295: _exit(i);
1.1 deraadt 2296: }