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