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