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