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