19792a032SEd Maste /* $OpenBSD: clientloop.c,v 1.410 2024/12/03 22:30:03 jsg Exp $ */
2511b41d2SMark Murray /*
3511b41d2SMark Murray * Author: Tatu Ylonen <ylo@cs.hut.fi>
4511b41d2SMark Murray * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5511b41d2SMark Murray * All rights reserved
6511b41d2SMark Murray * The main loop for the interactive session (client side).
7511b41d2SMark Murray *
8b66f2d16SKris Kennaway * As far as I am concerned, the code I have written for this software
9b66f2d16SKris Kennaway * can be used freely for any purpose. Any derived versions of this
10b66f2d16SKris Kennaway * software must be clearly marked as such, and if the derived work is
11b66f2d16SKris Kennaway * incompatible with the protocol description in the RFC file, it must be
12b66f2d16SKris Kennaway * called by a name other than "ssh" or "Secure Shell".
13b66f2d16SKris Kennaway *
14b66f2d16SKris Kennaway *
15b66f2d16SKris Kennaway * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16b66f2d16SKris Kennaway *
17b66f2d16SKris Kennaway * Redistribution and use in source and binary forms, with or without
18b66f2d16SKris Kennaway * modification, are permitted provided that the following conditions
19b66f2d16SKris Kennaway * are met:
20b66f2d16SKris Kennaway * 1. Redistributions of source code must retain the above copyright
21b66f2d16SKris Kennaway * notice, this list of conditions and the following disclaimer.
22b66f2d16SKris Kennaway * 2. Redistributions in binary form must reproduce the above copyright
23b66f2d16SKris Kennaway * notice, this list of conditions and the following disclaimer in the
24b66f2d16SKris Kennaway * documentation and/or other materials provided with the distribution.
25b66f2d16SKris Kennaway *
26b66f2d16SKris Kennaway * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27b66f2d16SKris Kennaway * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28b66f2d16SKris Kennaway * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29b66f2d16SKris Kennaway * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30b66f2d16SKris Kennaway * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31b66f2d16SKris Kennaway * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32b66f2d16SKris Kennaway * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33b66f2d16SKris Kennaway * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34b66f2d16SKris Kennaway * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35b66f2d16SKris Kennaway * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36b66f2d16SKris Kennaway *
37b66f2d16SKris Kennaway *
38a04a10f8SKris Kennaway * SSH2 support added by Markus Friedl.
39ae1f160dSDag-Erling Smørgrav * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
40b66f2d16SKris Kennaway *
41b66f2d16SKris Kennaway * Redistribution and use in source and binary forms, with or without
42b66f2d16SKris Kennaway * modification, are permitted provided that the following conditions
43b66f2d16SKris Kennaway * are met:
44b66f2d16SKris Kennaway * 1. Redistributions of source code must retain the above copyright
45b66f2d16SKris Kennaway * notice, this list of conditions and the following disclaimer.
46b66f2d16SKris Kennaway * 2. Redistributions in binary form must reproduce the above copyright
47b66f2d16SKris Kennaway * notice, this list of conditions and the following disclaimer in the
48b66f2d16SKris Kennaway * documentation and/or other materials provided with the distribution.
49b66f2d16SKris Kennaway *
50b66f2d16SKris Kennaway * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51b66f2d16SKris Kennaway * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52b66f2d16SKris Kennaway * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53b66f2d16SKris Kennaway * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54b66f2d16SKris Kennaway * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55b66f2d16SKris Kennaway * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56b66f2d16SKris Kennaway * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57b66f2d16SKris Kennaway * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58b66f2d16SKris Kennaway * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59b66f2d16SKris Kennaway * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60511b41d2SMark Murray */
61511b41d2SMark Murray
62511b41d2SMark Murray #include "includes.h"
63511b41d2SMark Murray
64761efaa7SDag-Erling Smørgrav #include <sys/types.h>
65761efaa7SDag-Erling Smørgrav #include <sys/ioctl.h>
66761efaa7SDag-Erling Smørgrav #ifdef HAVE_SYS_STAT_H
67761efaa7SDag-Erling Smørgrav # include <sys/stat.h>
68761efaa7SDag-Erling Smørgrav #endif
69761efaa7SDag-Erling Smørgrav #ifdef HAVE_SYS_TIME_H
70761efaa7SDag-Erling Smørgrav # include <sys/time.h>
71761efaa7SDag-Erling Smørgrav #endif
72761efaa7SDag-Erling Smørgrav #include <sys/socket.h>
73761efaa7SDag-Erling Smørgrav
74761efaa7SDag-Erling Smørgrav #include <ctype.h>
75761efaa7SDag-Erling Smørgrav #include <errno.h>
76761efaa7SDag-Erling Smørgrav #ifdef HAVE_PATHS_H
77761efaa7SDag-Erling Smørgrav #include <paths.h>
78761efaa7SDag-Erling Smørgrav #endif
7985d1f2d4SEd Maste #ifdef HAVE_POLL_H
8085d1f2d4SEd Maste #include <poll.h>
8185d1f2d4SEd Maste #endif
82761efaa7SDag-Erling Smørgrav #include <signal.h>
83761efaa7SDag-Erling Smørgrav #include <stdio.h>
84761efaa7SDag-Erling Smørgrav #include <stdlib.h>
85761efaa7SDag-Erling Smørgrav #include <string.h>
8682e5fdc5SEd Maste #include <stdarg.h>
87761efaa7SDag-Erling Smørgrav #include <termios.h>
88761efaa7SDag-Erling Smørgrav #include <pwd.h>
89761efaa7SDag-Erling Smørgrav #include <unistd.h>
90c1e08615SDag-Erling Smørgrav #include <limits.h>
91761efaa7SDag-Erling Smørgrav
9255215393SDag-Erling Smørgrav #include "openbsd-compat/sys-queue.h"
93761efaa7SDag-Erling Smørgrav #include "xmalloc.h"
94511b41d2SMark Murray #include "ssh.h"
951e8db6e2SBrian Feldman #include "ssh2.h"
96511b41d2SMark Murray #include "packet.h"
97d46065dfSDag-Erling Smørgrav #include "sshbuf.h"
98a04a10f8SKris Kennaway #include "compat.h"
99a04a10f8SKris Kennaway #include "channels.h"
100a04a10f8SKris Kennaway #include "dispatch.h"
101d46065dfSDag-Erling Smørgrav #include "sshkey.h"
102761efaa7SDag-Erling Smørgrav #include "cipher.h"
1031e8db6e2SBrian Feldman #include "kex.h"
104d994eeedSDag-Erling Smørgrav #include "myproposal.h"
1051e8db6e2SBrian Feldman #include "log.h"
106c0bbca73SDag-Erling Smørgrav #include "misc.h"
1071e8db6e2SBrian Feldman #include "readconf.h"
1081e8db6e2SBrian Feldman #include "clientloop.h"
109021d409fSDag-Erling Smørgrav #include "sshconnect.h"
1101e8db6e2SBrian Feldman #include "authfd.h"
1111e8db6e2SBrian Feldman #include "atomicio.h"
112d74d50a8SDag-Erling Smørgrav #include "sshpty.h"
113d74d50a8SDag-Erling Smørgrav #include "match.h"
114d74d50a8SDag-Erling Smørgrav #include "msg.h"
115c1e08615SDag-Erling Smørgrav #include "ssherr.h"
116c1e08615SDag-Erling Smørgrav #include "hostfile.h"
1175b9b2fafSBrian Feldman
11885d1f2d4SEd Maste /* Permitted RSA signature algorithms for UpdateHostkeys proofs */
11985d1f2d4SEd Maste #define HOSTKEY_PROOF_RSA_ALGS "rsa-sha2-512,rsa-sha2-256"
12085d1f2d4SEd Maste
121b3ced7f2SEd Maste /* Uncertainty (in percent) of keystroke timing intervals */
122b3ced7f2SEd Maste #define SSH_KEYSTROKE_TIMING_FUZZ 10
123b3ced7f2SEd Maste
1245b9b2fafSBrian Feldman /* import options */
1254899dde7SBrian Feldman extern Options options;
1264899dde7SBrian Feldman
127d74d50a8SDag-Erling Smørgrav /* Control socket */
1286d4f2dd1SDag-Erling Smørgrav extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
129d74d50a8SDag-Erling Smørgrav
130511b41d2SMark Murray /*
131511b41d2SMark Murray * Name of the host we are connecting to. This is the name given on the
1320194e6d0SEd Maste * command line, or the Hostname specified for the user-supplied name in a
133511b41d2SMark Murray * configuration file.
134511b41d2SMark Murray */
135511b41d2SMark Murray extern char *host;
136511b41d2SMark Murray
137511b41d2SMark Murray /*
13882e5fdc5SEd Maste * If this field is not NULL, the ForwardAgent socket is this path and different
13982e5fdc5SEd Maste * instead of SSH_AUTH_SOCK.
14082e5fdc5SEd Maste */
14182e5fdc5SEd Maste extern char *forward_agent_sock_path;
14282e5fdc5SEd Maste
14382e5fdc5SEd Maste /*
144511b41d2SMark Murray * Flag to indicate that we have received a window change signal which has
145511b41d2SMark Murray * not yet been processed. This will cause a message indicating the new
146511b41d2SMark Murray * window size to be sent to the server a little later. This is volatile
147511b41d2SMark Murray * because this is updated in a signal handler.
148511b41d2SMark Murray */
149ae1f160dSDag-Erling Smørgrav static volatile sig_atomic_t received_window_change_signal = 0;
150ae1f160dSDag-Erling Smørgrav static volatile sig_atomic_t received_signal = 0;
151511b41d2SMark Murray
1523a927e69SDag-Erling Smørgrav /* Time when backgrounded control master using ControlPersist should exit */
1533a927e69SDag-Erling Smørgrav static time_t control_persist_exit_time = 0;
1543a927e69SDag-Erling Smørgrav
155511b41d2SMark Murray /* Common data for the client loop code. */
1566d4f2dd1SDag-Erling Smørgrav volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
157511b41d2SMark Murray static int last_was_cr; /* Last character was a newline. */
15855215393SDag-Erling Smørgrav static int exit_status; /* Used to store the command exit status. */
159511b41d2SMark Murray static int connection_in; /* Connection to server (input). */
160511b41d2SMark Murray static int connection_out; /* Connection to server (output). */
1611e8db6e2SBrian Feldman static int need_rekeying; /* Set to non-zero if rekeying is requested. */
1623a927e69SDag-Erling Smørgrav static int session_closed; /* In SSH2: login session closed. */
16336b354d7SEd Maste static time_t x11_refuse_time; /* If >0, refuse x11 opens after this time. */
1643bbd8dc9SEd Maste static time_t server_alive_time; /* Time to do server_alive_check */
1657ee81174SEd Maste static int hostkeys_update_complete;
1667ee81174SEd Maste static int session_setup_complete;
167a04a10f8SKris Kennaway
168f02e3998SEd Maste static void client_init_dispatch(struct ssh *ssh);
169a04a10f8SKris Kennaway int session_ident = -1;
170a04a10f8SKris Kennaway
17155215393SDag-Erling Smørgrav /* Track escape per proto2 channel */
17255215393SDag-Erling Smørgrav struct escape_filter_ctx {
17355215393SDag-Erling Smørgrav int escape_pending;
17455215393SDag-Erling Smørgrav int escape_char;
175d74d50a8SDag-Erling Smørgrav };
176d74d50a8SDag-Erling Smørgrav
17755215393SDag-Erling Smørgrav /* Context for channel confirmation replies */
17855215393SDag-Erling Smørgrav struct channel_reply_ctx {
17955215393SDag-Erling Smørgrav const char *request_type;
1801e26c29bSDag-Erling Smørgrav int id;
1811e26c29bSDag-Erling Smørgrav enum confirm_action action;
18255215393SDag-Erling Smørgrav };
18355215393SDag-Erling Smørgrav
18455215393SDag-Erling Smørgrav /* Global request success/failure callbacks */
18520adc8f2SDag-Erling Smørgrav /* XXX move to struct ssh? */
18655215393SDag-Erling Smørgrav struct global_confirm {
18755215393SDag-Erling Smørgrav TAILQ_ENTRY(global_confirm) entry;
18855215393SDag-Erling Smørgrav global_confirm_cb *cb;
18955215393SDag-Erling Smørgrav void *ctx;
19055215393SDag-Erling Smørgrav int ref_count;
19155215393SDag-Erling Smørgrav };
19255215393SDag-Erling Smørgrav TAILQ_HEAD(global_confirms, global_confirm);
19355215393SDag-Erling Smørgrav static struct global_confirms global_confirms =
19455215393SDag-Erling Smørgrav TAILQ_HEAD_INITIALIZER(global_confirms);
19555215393SDag-Erling Smørgrav
19685d1f2d4SEd Maste static void quit_message(const char *fmt, ...)
19785d1f2d4SEd Maste __attribute__((__format__ (printf, 1, 2)));
19885d1f2d4SEd Maste
19985d1f2d4SEd Maste static void
quit_message(const char * fmt,...)20085d1f2d4SEd Maste quit_message(const char *fmt, ...)
20185d1f2d4SEd Maste {
202d565364dSEd Maste char *msg, *fmt2;
20385d1f2d4SEd Maste va_list args;
204d565364dSEd Maste xasprintf(&fmt2, "%s\r\n", fmt);
20585d1f2d4SEd Maste
20685d1f2d4SEd Maste va_start(args, fmt);
207d565364dSEd Maste xvasprintf(&msg, fmt2, args);
20885d1f2d4SEd Maste va_end(args);
20985d1f2d4SEd Maste
210d565364dSEd Maste (void)atomicio(vwrite, STDERR_FILENO, msg, strlen(msg));
21138f55691SGordon Tetlow free(msg);
212d565364dSEd Maste free(fmt2);
213d565364dSEd Maste
21485d1f2d4SEd Maste quit_pending = 1;
21585d1f2d4SEd Maste }
216d74d50a8SDag-Erling Smørgrav
217511b41d2SMark Murray /*
218511b41d2SMark Murray * Signal handler for the window change signal (SIGWINCH). This just sets a
219511b41d2SMark Murray * flag indicating that the window has changed.
220511b41d2SMark Murray */
221ae1f160dSDag-Erling Smørgrav static void
window_change_handler(int sig)222511b41d2SMark Murray window_change_handler(int sig)
223511b41d2SMark Murray {
224511b41d2SMark Murray received_window_change_signal = 1;
225511b41d2SMark Murray }
226511b41d2SMark Murray
227511b41d2SMark Murray /*
228511b41d2SMark Murray * Signal handler for signals that cause the program to terminate. These
229511b41d2SMark Murray * signals must be trapped to restore terminal modes.
230511b41d2SMark Murray */
231ae1f160dSDag-Erling Smørgrav static void
signal_handler(int sig)232511b41d2SMark Murray signal_handler(int sig)
233511b41d2SMark Murray {
234ae1f160dSDag-Erling Smørgrav received_signal = sig;
235ae1f160dSDag-Erling Smørgrav quit_pending = 1;
236511b41d2SMark Murray }
237511b41d2SMark Murray
238511b41d2SMark Murray /*
2393a927e69SDag-Erling Smørgrav * Sets control_persist_exit_time to the absolute time when the
2403a927e69SDag-Erling Smørgrav * backgrounded control master should exit due to expiry of the
2413a927e69SDag-Erling Smørgrav * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
2423a927e69SDag-Erling Smørgrav * control master process, or if there is no ControlPersist timeout.
2433a927e69SDag-Erling Smørgrav */
2443a927e69SDag-Erling Smørgrav static void
set_control_persist_exit_time(struct ssh * ssh)24520adc8f2SDag-Erling Smørgrav set_control_persist_exit_time(struct ssh *ssh)
2463a927e69SDag-Erling Smørgrav {
2473a927e69SDag-Erling Smørgrav if (muxserver_sock == -1 || !options.control_persist
2481e26c29bSDag-Erling Smørgrav || options.control_persist_timeout == 0) {
2493a927e69SDag-Erling Smørgrav /* not using a ControlPersist timeout */
2503a927e69SDag-Erling Smørgrav control_persist_exit_time = 0;
25120adc8f2SDag-Erling Smørgrav } else if (channel_still_open(ssh)) {
2523a927e69SDag-Erling Smørgrav /* some client connections are still open */
2533a927e69SDag-Erling Smørgrav if (control_persist_exit_time > 0)
254206be79aSEd Maste debug2_f("cancel scheduled exit");
2553a927e69SDag-Erling Smørgrav control_persist_exit_time = 0;
2563a927e69SDag-Erling Smørgrav } else if (control_persist_exit_time <= 0) {
2573a927e69SDag-Erling Smørgrav /* a client connection has recently closed */
2580dddc34cSDag-Erling Smørgrav control_persist_exit_time = monotime() +
2593a927e69SDag-Erling Smørgrav (time_t)options.control_persist_timeout;
260206be79aSEd Maste debug2_f("schedule exit in %d seconds",
2613a927e69SDag-Erling Smørgrav options.control_persist_timeout);
2623a927e69SDag-Erling Smørgrav }
2633a927e69SDag-Erling Smørgrav /* else we are already counting down to the timeout */
2643a927e69SDag-Erling Smørgrav }
2653a927e69SDag-Erling Smørgrav
2662e97a369SDag-Erling Smørgrav #define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
2672e97a369SDag-Erling Smørgrav static int
client_x11_display_valid(const char * display)2682e97a369SDag-Erling Smørgrav client_x11_display_valid(const char *display)
2692e97a369SDag-Erling Smørgrav {
2702e97a369SDag-Erling Smørgrav size_t i, dlen;
2712e97a369SDag-Erling Smørgrav
272ff4b04e0SDag-Erling Smørgrav if (display == NULL)
273ff4b04e0SDag-Erling Smørgrav return 0;
274ff4b04e0SDag-Erling Smørgrav
2752e97a369SDag-Erling Smørgrav dlen = strlen(display);
2762e97a369SDag-Erling Smørgrav for (i = 0; i < dlen; i++) {
27702d4c2acSDag-Erling Smørgrav if (!isalnum((u_char)display[i]) &&
2782e97a369SDag-Erling Smørgrav strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
2792e97a369SDag-Erling Smørgrav debug("Invalid character '%c' in DISPLAY", display[i]);
2802e97a369SDag-Erling Smørgrav return 0;
2812e97a369SDag-Erling Smørgrav }
2822e97a369SDag-Erling Smørgrav }
2832e97a369SDag-Erling Smørgrav return 1;
2842e97a369SDag-Erling Smørgrav }
2852e97a369SDag-Erling Smørgrav
286043840dfSDag-Erling Smørgrav #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
287b5a1b3a8SDag-Erling Smørgrav #define X11_TIMEOUT_SLACK 60
288ff4b04e0SDag-Erling Smørgrav int
client_x11_get_proto(struct ssh * ssh,const char * display,const char * xauth_path,u_int trusted,u_int timeout,char ** _proto,char ** _data)28920adc8f2SDag-Erling Smørgrav client_x11_get_proto(struct ssh *ssh, const char *display,
29020adc8f2SDag-Erling Smørgrav const char *xauth_path, u_int trusted, u_int timeout,
29120adc8f2SDag-Erling Smørgrav char **_proto, char **_data)
292043840dfSDag-Erling Smørgrav {
293d18f6dc9SDag-Erling Smørgrav char *cmd, line[512], xdisplay[512];
294ff4b04e0SDag-Erling Smørgrav char xauthfile[PATH_MAX], xauthdir[PATH_MAX];
295043840dfSDag-Erling Smørgrav static char proto[512], data[512];
296043840dfSDag-Erling Smørgrav FILE *f;
29719ca8551SDag-Erling Smørgrav int got_data = 0, generated = 0, do_unlink = 0, r;
298043840dfSDag-Erling Smørgrav struct stat st;
299b5a1b3a8SDag-Erling Smørgrav u_int now, x11_timeout_real;
300043840dfSDag-Erling Smørgrav
301043840dfSDag-Erling Smørgrav *_proto = proto;
302043840dfSDag-Erling Smørgrav *_data = data;
303ff4b04e0SDag-Erling Smørgrav proto[0] = data[0] = xauthfile[0] = xauthdir[0] = '\0';
304043840dfSDag-Erling Smørgrav
305ff4b04e0SDag-Erling Smørgrav if (!client_x11_display_valid(display)) {
306ff4b04e0SDag-Erling Smørgrav if (display != NULL)
307ff4b04e0SDag-Erling Smørgrav logit("DISPLAY \"%s\" invalid; disabling X11 forwarding",
3082e97a369SDag-Erling Smørgrav display);
309ff4b04e0SDag-Erling Smørgrav return -1;
310043840dfSDag-Erling Smørgrav }
311ff4b04e0SDag-Erling Smørgrav if (xauth_path != NULL && stat(xauth_path, &st) == -1) {
312ff4b04e0SDag-Erling Smørgrav debug("No xauth program.");
313ff4b04e0SDag-Erling Smørgrav xauth_path = NULL;
314ff4b04e0SDag-Erling Smørgrav }
315ff4b04e0SDag-Erling Smørgrav
316ff4b04e0SDag-Erling Smørgrav if (xauth_path != NULL) {
317043840dfSDag-Erling Smørgrav /*
318043840dfSDag-Erling Smørgrav * Handle FamilyLocal case where $DISPLAY does
319043840dfSDag-Erling Smørgrav * not match an authorization entry. For this we
320043840dfSDag-Erling Smørgrav * just try "xauth list unix:displaynum.screennum".
321043840dfSDag-Erling Smørgrav * XXX: "localhost" match to determine FamilyLocal
322043840dfSDag-Erling Smørgrav * is not perfect.
323043840dfSDag-Erling Smørgrav */
324043840dfSDag-Erling Smørgrav if (strncmp(display, "localhost:", 10) == 0) {
325ff4b04e0SDag-Erling Smørgrav if ((r = snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
326ff4b04e0SDag-Erling Smørgrav display + 10)) < 0 ||
327ff4b04e0SDag-Erling Smørgrav (size_t)r >= sizeof(xdisplay)) {
328206be79aSEd Maste error_f("display name too long");
329ff4b04e0SDag-Erling Smørgrav return -1;
330ff4b04e0SDag-Erling Smørgrav }
331043840dfSDag-Erling Smørgrav display = xdisplay;
332043840dfSDag-Erling Smørgrav }
333043840dfSDag-Erling Smørgrav if (trusted == 0) {
334b5a1b3a8SDag-Erling Smørgrav /*
335ff4b04e0SDag-Erling Smørgrav * Generate an untrusted X11 auth cookie.
336ff4b04e0SDag-Erling Smørgrav *
337b5a1b3a8SDag-Erling Smørgrav * The authentication cookie should briefly outlive
338b5a1b3a8SDag-Erling Smørgrav * ssh's willingness to forward X11 connections to
339b5a1b3a8SDag-Erling Smørgrav * avoid nasty fail-open behaviour in the X server.
340b5a1b3a8SDag-Erling Smørgrav */
341ff4b04e0SDag-Erling Smørgrav mktemp_proto(xauthdir, sizeof(xauthdir));
342ff4b04e0SDag-Erling Smørgrav if (mkdtemp(xauthdir) == NULL) {
343206be79aSEd Maste error_f("mkdtemp: %s", strerror(errno));
344ff4b04e0SDag-Erling Smørgrav return -1;
345ff4b04e0SDag-Erling Smørgrav }
346ff4b04e0SDag-Erling Smørgrav do_unlink = 1;
347ff4b04e0SDag-Erling Smørgrav if ((r = snprintf(xauthfile, sizeof(xauthfile),
348ff4b04e0SDag-Erling Smørgrav "%s/xauthfile", xauthdir)) < 0 ||
349ff4b04e0SDag-Erling Smørgrav (size_t)r >= sizeof(xauthfile)) {
350206be79aSEd Maste error_f("xauthfile path too long");
351ff4b04e0SDag-Erling Smørgrav rmdir(xauthdir);
352ff4b04e0SDag-Erling Smørgrav return -1;
353ff4b04e0SDag-Erling Smørgrav }
354ff4b04e0SDag-Erling Smørgrav
355d18f6dc9SDag-Erling Smørgrav if (timeout == 0) {
356d18f6dc9SDag-Erling Smørgrav /* auth doesn't time out */
357d18f6dc9SDag-Erling Smørgrav xasprintf(&cmd, "%s -f %s generate %s %s "
358d18f6dc9SDag-Erling Smørgrav "untrusted 2>%s",
359b5a1b3a8SDag-Erling Smørgrav xauth_path, xauthfile, display,
360d18f6dc9SDag-Erling Smørgrav SSH_X11_PROTO, _PATH_DEVNULL);
361d18f6dc9SDag-Erling Smørgrav } else {
362d18f6dc9SDag-Erling Smørgrav /* Add some slack to requested expiry */
363d18f6dc9SDag-Erling Smørgrav if (timeout < UINT_MAX - X11_TIMEOUT_SLACK)
364d18f6dc9SDag-Erling Smørgrav x11_timeout_real = timeout +
365d18f6dc9SDag-Erling Smørgrav X11_TIMEOUT_SLACK;
366d18f6dc9SDag-Erling Smørgrav else {
367d18f6dc9SDag-Erling Smørgrav /* Don't overflow on long timeouts */
368d18f6dc9SDag-Erling Smørgrav x11_timeout_real = UINT_MAX;
369d18f6dc9SDag-Erling Smørgrav }
370d18f6dc9SDag-Erling Smørgrav xasprintf(&cmd, "%s -f %s generate %s %s "
371d18f6dc9SDag-Erling Smørgrav "untrusted timeout %u 2>%s",
372d18f6dc9SDag-Erling Smørgrav xauth_path, xauthfile, display,
373d18f6dc9SDag-Erling Smørgrav SSH_X11_PROTO, x11_timeout_real,
374d18f6dc9SDag-Erling Smørgrav _PATH_DEVNULL);
375d18f6dc9SDag-Erling Smørgrav }
376206be79aSEd Maste debug2_f("xauth command: %s", cmd);
377d18f6dc9SDag-Erling Smørgrav
378d18f6dc9SDag-Erling Smørgrav if (timeout != 0 && x11_refuse_time == 0) {
3790dddc34cSDag-Erling Smørgrav now = monotime() + 1;
38036b354d7SEd Maste if (SSH_TIME_T_MAX - timeout < now)
38136b354d7SEd Maste x11_refuse_time = SSH_TIME_T_MAX;
3823a927e69SDag-Erling Smørgrav else
3833a927e69SDag-Erling Smørgrav x11_refuse_time = now + timeout;
38420adc8f2SDag-Erling Smørgrav channel_set_x11_refuse_time(ssh,
38520adc8f2SDag-Erling Smørgrav x11_refuse_time);
3863a927e69SDag-Erling Smørgrav }
387b5a1b3a8SDag-Erling Smørgrav if (system(cmd) == 0)
388b5a1b3a8SDag-Erling Smørgrav generated = 1;
389d18f6dc9SDag-Erling Smørgrav free(cmd);
390043840dfSDag-Erling Smørgrav }
391490bfaadSDag-Erling Smørgrav
392490bfaadSDag-Erling Smørgrav /*
393490bfaadSDag-Erling Smørgrav * When in untrusted mode, we read the cookie only if it was
394490bfaadSDag-Erling Smørgrav * successfully generated as an untrusted one in the step
395490bfaadSDag-Erling Smørgrav * above.
396490bfaadSDag-Erling Smørgrav */
397490bfaadSDag-Erling Smørgrav if (trusted || generated) {
398d18f6dc9SDag-Erling Smørgrav xasprintf(&cmd,
399021d409fSDag-Erling Smørgrav "%s %s%s list %s 2>" _PATH_DEVNULL,
400043840dfSDag-Erling Smørgrav xauth_path,
401043840dfSDag-Erling Smørgrav generated ? "-f " : "" ,
402043840dfSDag-Erling Smørgrav generated ? xauthfile : "",
403043840dfSDag-Erling Smørgrav display);
404043840dfSDag-Erling Smørgrav debug2("x11_get_proto: %s", cmd);
405043840dfSDag-Erling Smørgrav f = popen(cmd, "r");
406043840dfSDag-Erling Smørgrav if (f && fgets(line, sizeof(line), f) &&
407043840dfSDag-Erling Smørgrav sscanf(line, "%*s %511s %511s", proto, data) == 2)
408043840dfSDag-Erling Smørgrav got_data = 1;
409043840dfSDag-Erling Smørgrav if (f)
410043840dfSDag-Erling Smørgrav pclose(f);
411d18f6dc9SDag-Erling Smørgrav free(cmd);
412ff4b04e0SDag-Erling Smørgrav }
413043840dfSDag-Erling Smørgrav }
414043840dfSDag-Erling Smørgrav
415043840dfSDag-Erling Smørgrav if (do_unlink) {
416043840dfSDag-Erling Smørgrav unlink(xauthfile);
417043840dfSDag-Erling Smørgrav rmdir(xauthdir);
418043840dfSDag-Erling Smørgrav }
419ff4b04e0SDag-Erling Smørgrav
420ff4b04e0SDag-Erling Smørgrav /* Don't fall back to fake X11 data for untrusted forwarding */
421ff4b04e0SDag-Erling Smørgrav if (!trusted && !got_data) {
422ff4b04e0SDag-Erling Smørgrav error("Warning: untrusted X11 forwarding setup failed: "
423ff4b04e0SDag-Erling Smørgrav "xauth key data not generated");
424ff4b04e0SDag-Erling Smørgrav return -1;
425ff4b04e0SDag-Erling Smørgrav }
426043840dfSDag-Erling Smørgrav
427043840dfSDag-Erling Smørgrav /*
428043840dfSDag-Erling Smørgrav * If we didn't get authentication data, just make up some
429043840dfSDag-Erling Smørgrav * data. The forwarding code will check the validity of the
430043840dfSDag-Erling Smørgrav * response anyway, and substitute this data. The X11
431043840dfSDag-Erling Smørgrav * server, however, will ignore this fake data and use
432043840dfSDag-Erling Smørgrav * whatever authentication mechanisms it was using otherwise
433043840dfSDag-Erling Smørgrav * for the local connection.
434043840dfSDag-Erling Smørgrav */
435043840dfSDag-Erling Smørgrav if (!got_data) {
43619ca8551SDag-Erling Smørgrav u_int8_t rnd[16];
43719ca8551SDag-Erling Smørgrav u_int i;
438043840dfSDag-Erling Smørgrav
439043840dfSDag-Erling Smørgrav logit("Warning: No xauth data; "
440043840dfSDag-Erling Smørgrav "using fake authentication data for X11 forwarding.");
441043840dfSDag-Erling Smørgrav strlcpy(proto, SSH_X11_PROTO, sizeof proto);
44219ca8551SDag-Erling Smørgrav arc4random_buf(rnd, sizeof(rnd));
44319ca8551SDag-Erling Smørgrav for (i = 0; i < sizeof(rnd); i++) {
444043840dfSDag-Erling Smørgrav snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
44519ca8551SDag-Erling Smørgrav rnd[i]);
446043840dfSDag-Erling Smørgrav }
447043840dfSDag-Erling Smørgrav }
448ff4b04e0SDag-Erling Smørgrav
449ff4b04e0SDag-Erling Smørgrav return 0;
450043840dfSDag-Erling Smørgrav }
451043840dfSDag-Erling Smørgrav
452511b41d2SMark Murray /*
453511b41d2SMark Murray * Checks if the client window has changed, and sends a packet about it to
454511b41d2SMark Murray * the server if so. The actual change is detected elsewhere (by a software
455511b41d2SMark Murray * interrupt on Unix); this just checks the flag and sends a message if
456511b41d2SMark Murray * appropriate.
457511b41d2SMark Murray */
458511b41d2SMark Murray
459ae1f160dSDag-Erling Smørgrav static void
client_check_window_change(struct ssh * ssh)46020adc8f2SDag-Erling Smørgrav client_check_window_change(struct ssh *ssh)
461511b41d2SMark Murray {
462a04a10f8SKris Kennaway if (!received_window_change_signal)
463a04a10f8SKris Kennaway return;
464511b41d2SMark Murray received_window_change_signal = 0;
465206be79aSEd Maste debug2_f("changed");
46620adc8f2SDag-Erling Smørgrav channel_send_window_changes(ssh);
467511b41d2SMark Murray }
468511b41d2SMark Murray
469c1e08615SDag-Erling Smørgrav static int
client_global_request_reply(int type,u_int32_t seq,struct ssh * ssh)47020adc8f2SDag-Erling Smørgrav client_global_request_reply(int type, u_int32_t seq, struct ssh *ssh)
471efcad6b7SDag-Erling Smørgrav {
47255215393SDag-Erling Smørgrav struct global_confirm *gc;
47355215393SDag-Erling Smørgrav
47455215393SDag-Erling Smørgrav if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
475c1e08615SDag-Erling Smørgrav return 0;
47655215393SDag-Erling Smørgrav if (gc->cb != NULL)
47720adc8f2SDag-Erling Smørgrav gc->cb(ssh, type, seq, gc->ctx);
47855215393SDag-Erling Smørgrav if (--gc->ref_count <= 0) {
47955215393SDag-Erling Smørgrav TAILQ_REMOVE(&global_confirms, gc, entry);
480db903103SEd Maste freezero(gc, sizeof(*gc));
48155215393SDag-Erling Smørgrav }
48255215393SDag-Erling Smørgrav
483f02e3998SEd Maste ssh_packet_set_alive_timeouts(ssh, 0);
484c1e08615SDag-Erling Smørgrav return 0;
485efcad6b7SDag-Erling Smørgrav }
486efcad6b7SDag-Erling Smørgrav
487efcad6b7SDag-Erling Smørgrav static void
schedule_server_alive_check(void)4883bbd8dc9SEd Maste schedule_server_alive_check(void)
4893bbd8dc9SEd Maste {
4903bbd8dc9SEd Maste if (options.server_alive_interval > 0)
4913bbd8dc9SEd Maste server_alive_time = monotime() + options.server_alive_interval;
4923bbd8dc9SEd Maste }
4933bbd8dc9SEd Maste
4943bbd8dc9SEd Maste static void
server_alive_check(struct ssh * ssh)495f02e3998SEd Maste server_alive_check(struct ssh *ssh)
496efcad6b7SDag-Erling Smørgrav {
497f02e3998SEd Maste int r;
498f02e3998SEd Maste
499f02e3998SEd Maste if (ssh_packet_inc_alive_timeouts(ssh) > options.server_alive_count_max) {
5009f6de2d7SDag-Erling Smørgrav logit("Timeout, server %s not responding.", host);
50192eb0aa1SDag-Erling Smørgrav cleanup_exit(255);
50292eb0aa1SDag-Erling Smørgrav }
503f02e3998SEd Maste if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
504f02e3998SEd Maste (r = sshpkt_put_cstring(ssh, "keepalive@openssh.com")) != 0 ||
505f02e3998SEd Maste (r = sshpkt_put_u8(ssh, 1)) != 0 || /* boolean: want reply */
506f02e3998SEd Maste (r = sshpkt_send(ssh)) != 0)
507206be79aSEd Maste fatal_fr(r, "send packet");
50855215393SDag-Erling Smørgrav /* Insert an empty placeholder to maintain ordering */
50955215393SDag-Erling Smørgrav client_register_global_confirm(NULL, NULL);
5103bbd8dc9SEd Maste schedule_server_alive_check();
511efcad6b7SDag-Erling Smørgrav }
512efcad6b7SDag-Erling Smørgrav
513b3ced7f2SEd Maste /* Try to send a dummy keystroke */
514b3ced7f2SEd Maste static int
send_chaff(struct ssh * ssh)515b3ced7f2SEd Maste send_chaff(struct ssh *ssh)
516b3ced7f2SEd Maste {
517b3ced7f2SEd Maste int r;
518b3ced7f2SEd Maste
5199200ce32SEd Maste if (ssh->kex == NULL || (ssh->kex->flags & KEX_HAS_PING) == 0)
520b3ced7f2SEd Maste return 0;
521b3ced7f2SEd Maste /* XXX probabilistically send chaff? */
522b3ced7f2SEd Maste /*
523b3ced7f2SEd Maste * a SSH2_MSG_CHANNEL_DATA payload is 9 bytes:
524b3ced7f2SEd Maste * 4 bytes channel ID + 4 bytes string length + 1 byte string data
525b3ced7f2SEd Maste * simulate that here.
526b3ced7f2SEd Maste */
527b3ced7f2SEd Maste if ((r = sshpkt_start(ssh, SSH2_MSG_PING)) != 0 ||
528b3ced7f2SEd Maste (r = sshpkt_put_cstring(ssh, "PING!")) != 0 ||
529b3ced7f2SEd Maste (r = sshpkt_send(ssh)) != 0)
530b3ced7f2SEd Maste fatal_fr(r, "send packet");
531b3ced7f2SEd Maste return 1;
532b3ced7f2SEd Maste }
533b3ced7f2SEd Maste
534b3ced7f2SEd Maste /* Sets the next interval to send a keystroke or chaff packet */
535b3ced7f2SEd Maste static void
set_next_interval(const struct timespec * now,struct timespec * next_interval,u_int interval_ms,int starting)536b3ced7f2SEd Maste set_next_interval(const struct timespec *now, struct timespec *next_interval,
537b3ced7f2SEd Maste u_int interval_ms, int starting)
538b3ced7f2SEd Maste {
539b3ced7f2SEd Maste struct timespec tmp;
540b3ced7f2SEd Maste long long interval_ns, fuzz_ns;
541b3ced7f2SEd Maste static long long rate_fuzz;
542b3ced7f2SEd Maste
543b3ced7f2SEd Maste interval_ns = interval_ms * (1000LL * 1000);
544b3ced7f2SEd Maste fuzz_ns = (interval_ns * SSH_KEYSTROKE_TIMING_FUZZ) / 100;
545b3ced7f2SEd Maste /* Center fuzz around requested interval */
546b3ced7f2SEd Maste if (fuzz_ns > INT_MAX)
547b3ced7f2SEd Maste fuzz_ns = INT_MAX;
548b3ced7f2SEd Maste if (fuzz_ns > interval_ns) {
549b3ced7f2SEd Maste /* Shouldn't happen */
550b3ced7f2SEd Maste fatal_f("internal error: fuzz %u%% %lldns > interval %lldns",
551b3ced7f2SEd Maste SSH_KEYSTROKE_TIMING_FUZZ, fuzz_ns, interval_ns);
552b3ced7f2SEd Maste }
553b3ced7f2SEd Maste /*
554b3ced7f2SEd Maste * Randomise the keystroke/chaff intervals in two ways:
555b3ced7f2SEd Maste * 1. Each interval has some random jitter applied to make the
556b3ced7f2SEd Maste * interval-to-interval time unpredictable.
557b3ced7f2SEd Maste * 2. The overall interval rate is also randomly perturbed for each
558b3ced7f2SEd Maste * chaffing session to make the average rate unpredictable.
559b3ced7f2SEd Maste */
560b3ced7f2SEd Maste if (starting)
561b3ced7f2SEd Maste rate_fuzz = arc4random_uniform(fuzz_ns);
562b3ced7f2SEd Maste interval_ns -= fuzz_ns;
563b3ced7f2SEd Maste interval_ns += arc4random_uniform(fuzz_ns) + rate_fuzz;
564b3ced7f2SEd Maste
565b3ced7f2SEd Maste tmp.tv_sec = interval_ns / (1000 * 1000 * 1000);
566b3ced7f2SEd Maste tmp.tv_nsec = interval_ns % (1000 * 1000 * 1000);
567b3ced7f2SEd Maste
568b3ced7f2SEd Maste timespecadd(now, &tmp, next_interval);
569b3ced7f2SEd Maste }
570b3ced7f2SEd Maste
571b3ced7f2SEd Maste /*
572b3ced7f2SEd Maste * Performs keystroke timing obfuscation. Returns non-zero if the
573b3ced7f2SEd Maste * output fd should be polled.
574b3ced7f2SEd Maste */
575b3ced7f2SEd Maste static int
obfuscate_keystroke_timing(struct ssh * ssh,struct timespec * timeout,int channel_did_enqueue)576b3ced7f2SEd Maste obfuscate_keystroke_timing(struct ssh *ssh, struct timespec *timeout,
577b3ced7f2SEd Maste int channel_did_enqueue)
578b3ced7f2SEd Maste {
579b3ced7f2SEd Maste static int active;
580b3ced7f2SEd Maste static struct timespec next_interval, chaff_until;
581b3ced7f2SEd Maste struct timespec now, tmp;
582b3ced7f2SEd Maste int just_started = 0, had_keystroke = 0;
583b3ced7f2SEd Maste static unsigned long long nchaff;
584b3ced7f2SEd Maste char *stop_reason = NULL;
585b3ced7f2SEd Maste long long n;
586b3ced7f2SEd Maste
587b3ced7f2SEd Maste monotime_ts(&now);
588b3ced7f2SEd Maste
589b3ced7f2SEd Maste if (options.obscure_keystroke_timing_interval <= 0)
590b3ced7f2SEd Maste return 1; /* disabled in config */
591b3ced7f2SEd Maste
59238f55691SGordon Tetlow if (!channel_tty_open(ssh) || quit_pending) {
593b3ced7f2SEd Maste /* Stop if no channels left of we're waiting for one to close */
594b3ced7f2SEd Maste stop_reason = "no active channels";
595b3ced7f2SEd Maste } else if (ssh_packet_is_rekeying(ssh)) {
596b3ced7f2SEd Maste /* Stop if we're rekeying */
597b3ced7f2SEd Maste stop_reason = "rekeying started";
598b3ced7f2SEd Maste } else if (!ssh_packet_interactive_data_to_write(ssh) &&
599b3ced7f2SEd Maste ssh_packet_have_data_to_write(ssh)) {
600b3ced7f2SEd Maste /* Stop if the output buffer has more than a few keystrokes */
601b3ced7f2SEd Maste stop_reason = "output buffer filling";
602b3ced7f2SEd Maste } else if (active && channel_did_enqueue &&
603b3ced7f2SEd Maste ssh_packet_have_data_to_write(ssh)) {
604b3ced7f2SEd Maste /* Still in active mode and have a keystroke queued. */
605b3ced7f2SEd Maste had_keystroke = 1;
606b3ced7f2SEd Maste } else if (active) {
607b3ced7f2SEd Maste if (timespeccmp(&now, &chaff_until, >=)) {
608b3ced7f2SEd Maste /* Stop if there have been no keystrokes for a while */
609b3ced7f2SEd Maste stop_reason = "chaff time expired";
610d565364dSEd Maste } else if (timespeccmp(&now, &next_interval, >=) &&
611d565364dSEd Maste !ssh_packet_have_data_to_write(ssh)) {
612d565364dSEd Maste /* If due to send but have no data, then send chaff */
613b3ced7f2SEd Maste if (send_chaff(ssh))
614b3ced7f2SEd Maste nchaff++;
615b3ced7f2SEd Maste }
616b3ced7f2SEd Maste }
617b3ced7f2SEd Maste
618b3ced7f2SEd Maste if (stop_reason != NULL) {
619b3ced7f2SEd Maste if (active) {
620b3ced7f2SEd Maste debug3_f("stopping: %s (%llu chaff packets sent)",
621b3ced7f2SEd Maste stop_reason, nchaff);
622b3ced7f2SEd Maste active = 0;
623b3ced7f2SEd Maste }
624b3ced7f2SEd Maste return 1;
625b3ced7f2SEd Maste }
626b3ced7f2SEd Maste
627b3ced7f2SEd Maste /*
628b3ced7f2SEd Maste * If we're in interactive mode, and only have a small amount
629b3ced7f2SEd Maste * of outbound data, then we assume that the user is typing
630b3ced7f2SEd Maste * interactively. In this case, start quantising outbound packets to
631b3ced7f2SEd Maste * fixed time intervals to hide inter-keystroke timing.
632b3ced7f2SEd Maste */
633b3ced7f2SEd Maste if (!active && ssh_packet_interactive_data_to_write(ssh) &&
634b3ced7f2SEd Maste channel_did_enqueue && ssh_packet_have_data_to_write(ssh)) {
635b3ced7f2SEd Maste debug3_f("starting: interval ~%dms",
636b3ced7f2SEd Maste options.obscure_keystroke_timing_interval);
637b3ced7f2SEd Maste just_started = had_keystroke = active = 1;
638b3ced7f2SEd Maste nchaff = 0;
639b3ced7f2SEd Maste set_next_interval(&now, &next_interval,
640b3ced7f2SEd Maste options.obscure_keystroke_timing_interval, 1);
641b3ced7f2SEd Maste }
642b3ced7f2SEd Maste
643b3ced7f2SEd Maste /* Don't hold off if obfuscation inactive */
644b3ced7f2SEd Maste if (!active)
645b3ced7f2SEd Maste return 1;
646b3ced7f2SEd Maste
647b3ced7f2SEd Maste if (had_keystroke) {
648b3ced7f2SEd Maste /*
649b3ced7f2SEd Maste * Arrange to send chaff packets for a random interval after
650b3ced7f2SEd Maste * the last keystroke was sent.
651b3ced7f2SEd Maste */
652b3ced7f2SEd Maste ms_to_timespec(&tmp, SSH_KEYSTROKE_CHAFF_MIN_MS +
653b3ced7f2SEd Maste arc4random_uniform(SSH_KEYSTROKE_CHAFF_RNG_MS));
654b3ced7f2SEd Maste timespecadd(&now, &tmp, &chaff_until);
655b3ced7f2SEd Maste }
656b3ced7f2SEd Maste
657b3ced7f2SEd Maste ptimeout_deadline_monotime_tsp(timeout, &next_interval);
658b3ced7f2SEd Maste
659b3ced7f2SEd Maste if (just_started)
660b3ced7f2SEd Maste return 1;
661b3ced7f2SEd Maste
6629792a032SEd Maste /* Don't arm output fd for poll until the timing interval has elapsed... */
663b3ced7f2SEd Maste if (timespeccmp(&now, &next_interval, <))
6649792a032SEd Maste /* ...unless there's x11 communication happening */
6659792a032SEd Maste return x11_channel_used_recently(ssh);
666b3ced7f2SEd Maste
667b3ced7f2SEd Maste /* Calculate number of intervals missed since the last check */
668b3ced7f2SEd Maste n = (now.tv_sec - next_interval.tv_sec) * 1000LL * 1000 * 1000;
669b3ced7f2SEd Maste n += now.tv_nsec - next_interval.tv_nsec;
670b3ced7f2SEd Maste n /= options.obscure_keystroke_timing_interval * 1000LL * 1000;
671b3ced7f2SEd Maste n = (n < 0) ? 1 : n + 1;
672b3ced7f2SEd Maste
673b3ced7f2SEd Maste /* Advance to the next interval */
674b3ced7f2SEd Maste set_next_interval(&now, &next_interval,
675b3ced7f2SEd Maste options.obscure_keystroke_timing_interval * n, 0);
676b3ced7f2SEd Maste return 1;
677b3ced7f2SEd Maste }
678b3ced7f2SEd Maste
679511b41d2SMark Murray /*
680511b41d2SMark Murray * Waits until the client can do something (some data becomes available on
681511b41d2SMark Murray * one of the file descriptors).
682511b41d2SMark Murray */
683ae1f160dSDag-Erling Smørgrav static void
client_wait_until_can_do_something(struct ssh * ssh,struct pollfd ** pfdp,u_int * npfd_allocp,u_int * npfd_activep,int channel_did_enqueue,sigset_t * sigsetp,int * conn_in_readyp,int * conn_out_readyp)68485d1f2d4SEd Maste client_wait_until_can_do_something(struct ssh *ssh, struct pollfd **pfdp,
685b3ced7f2SEd Maste u_int *npfd_allocp, u_int *npfd_activep, int channel_did_enqueue,
68638f55691SGordon Tetlow sigset_t *sigsetp, int *conn_in_readyp, int *conn_out_readyp)
687511b41d2SMark Murray {
6887ee81174SEd Maste struct timespec timeout;
689b3ced7f2SEd Maste int ret, oready;
69085d1f2d4SEd Maste u_int p;
691efcad6b7SDag-Erling Smørgrav
69285d1f2d4SEd Maste *conn_in_readyp = *conn_out_readyp = 0;
693511b41d2SMark Murray
69485d1f2d4SEd Maste /* Prepare channel poll. First two pollfd entries are reserved */
6957ee81174SEd Maste ptimeout_init(&timeout);
6967ee81174SEd Maste channel_prepare_poll(ssh, pfdp, npfd_allocp, npfd_activep, 2, &timeout);
69785d1f2d4SEd Maste if (*npfd_activep < 2)
69885d1f2d4SEd Maste fatal_f("bad npfd %u", *npfd_activep); /* shouldn't happen */
69985d1f2d4SEd Maste
70085d1f2d4SEd Maste /* channel_prepare_poll could have closed the last channel */
70120adc8f2SDag-Erling Smørgrav if (session_closed && !channel_still_open(ssh) &&
702f02e3998SEd Maste !ssh_packet_have_data_to_write(ssh)) {
70385d1f2d4SEd Maste /* clear events since we did not call poll() */
70485d1f2d4SEd Maste for (p = 0; p < *npfd_activep; p++)
70585d1f2d4SEd Maste (*pfdp)[p].revents = 0;
706ae1f160dSDag-Erling Smørgrav return;
70720adc8f2SDag-Erling Smørgrav }
70820adc8f2SDag-Erling Smørgrav
709b3ced7f2SEd Maste oready = obfuscate_keystroke_timing(ssh, &timeout, channel_did_enqueue);
710b3ced7f2SEd Maste
71185d1f2d4SEd Maste /* Monitor server connection on reserved pollfd entries */
71285d1f2d4SEd Maste (*pfdp)[0].fd = connection_in;
71385d1f2d4SEd Maste (*pfdp)[0].events = POLLIN;
71485d1f2d4SEd Maste (*pfdp)[1].fd = connection_out;
715b3ced7f2SEd Maste (*pfdp)[1].events = (oready && ssh_packet_have_data_to_write(ssh)) ?
716b3ced7f2SEd Maste POLLOUT : 0;
717511b41d2SMark Murray
718511b41d2SMark Murray /*
719511b41d2SMark Murray * Wait for something to happen. This will suspend the process until
72085d1f2d4SEd Maste * some polled descriptor can be read, written, or has some other
7213a927e69SDag-Erling Smørgrav * event pending, or a timeout expires.
722511b41d2SMark Murray */
72320adc8f2SDag-Erling Smørgrav set_control_persist_exit_time(ssh);
7247ee81174SEd Maste if (control_persist_exit_time > 0)
7257ee81174SEd Maste ptimeout_deadline_monotime(&timeout, control_persist_exit_time);
7267ee81174SEd Maste if (options.server_alive_interval > 0)
7277ee81174SEd Maste ptimeout_deadline_monotime(&timeout, server_alive_time);
728b3ced7f2SEd Maste if (options.rekey_interval > 0 && !ssh_packet_is_rekeying(ssh)) {
7297ee81174SEd Maste ptimeout_deadline_sec(&timeout,
7307ee81174SEd Maste ssh_packet_get_rekey_timeout(ssh));
7313a927e69SDag-Erling Smørgrav }
7323a927e69SDag-Erling Smørgrav
73338f55691SGordon Tetlow ret = ppoll(*pfdp, *npfd_activep, ptimeout_get_tsp(&timeout), sigsetp);
73485d1f2d4SEd Maste
7350194e6d0SEd Maste if (ret == -1) {
7361e8db6e2SBrian Feldman /*
73785d1f2d4SEd Maste * We have to clear the events because we return.
7381e8db6e2SBrian Feldman * We have to return, because the mainloop checks for the flags
7391e8db6e2SBrian Feldman * set by the signal handlers.
7401e8db6e2SBrian Feldman */
74185d1f2d4SEd Maste for (p = 0; p < *npfd_activep; p++)
74285d1f2d4SEd Maste (*pfdp)[p].revents = 0;
743511b41d2SMark Murray if (errno == EINTR)
744511b41d2SMark Murray return;
745511b41d2SMark Murray /* Note: we might still have data in the buffers. */
74685d1f2d4SEd Maste quit_message("poll: %s", strerror(errno));
74785d1f2d4SEd Maste return;
74885d1f2d4SEd Maste }
74985d1f2d4SEd Maste
75085d1f2d4SEd Maste *conn_in_readyp = (*pfdp)[0].revents != 0;
75185d1f2d4SEd Maste *conn_out_readyp = (*pfdp)[1].revents != 0;
75285d1f2d4SEd Maste
75385d1f2d4SEd Maste if (options.server_alive_interval > 0 && !*conn_in_readyp &&
75485d1f2d4SEd Maste monotime() >= server_alive_time) {
7550dddc34cSDag-Erling Smørgrav /*
75685d1f2d4SEd Maste * ServerAlive check is needed. We can't rely on the poll
7573bbd8dc9SEd Maste * timing out since traffic on the client side such as port
7583bbd8dc9SEd Maste * forwards can keep waking it up.
7590dddc34cSDag-Erling Smørgrav */
760f02e3998SEd Maste server_alive_check(ssh);
761511b41d2SMark Murray }
76285d1f2d4SEd Maste }
763511b41d2SMark Murray
764ae1f160dSDag-Erling Smørgrav static void
client_suspend_self(struct sshbuf * bin,struct sshbuf * bout,struct sshbuf * berr)765d46065dfSDag-Erling Smørgrav client_suspend_self(struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr)
766511b41d2SMark Murray {
767511b41d2SMark Murray /* Flush stdout and stderr buffers. */
768d46065dfSDag-Erling Smørgrav if (sshbuf_len(bout) > 0)
769d46065dfSDag-Erling Smørgrav atomicio(vwrite, fileno(stdout), sshbuf_mutable_ptr(bout),
770d46065dfSDag-Erling Smørgrav sshbuf_len(bout));
771d46065dfSDag-Erling Smørgrav if (sshbuf_len(berr) > 0)
772d46065dfSDag-Erling Smørgrav atomicio(vwrite, fileno(stderr), sshbuf_mutable_ptr(berr),
773d46065dfSDag-Erling Smørgrav sshbuf_len(berr));
774511b41d2SMark Murray
7751e26c29bSDag-Erling Smørgrav leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
776511b41d2SMark Murray
77720adc8f2SDag-Erling Smørgrav sshbuf_reset(bin);
77820adc8f2SDag-Erling Smørgrav sshbuf_reset(bout);
77920adc8f2SDag-Erling Smørgrav sshbuf_reset(berr);
780511b41d2SMark Murray
781511b41d2SMark Murray /* Send the suspend signal to the program itself. */
782511b41d2SMark Murray kill(getpid(), SIGTSTP);
783511b41d2SMark Murray
7845e8dbd04SDag-Erling Smørgrav /* Reset window sizes in case they have changed */
785511b41d2SMark Murray received_window_change_signal = 1;
786511b41d2SMark Murray
7871e26c29bSDag-Erling Smørgrav enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
788511b41d2SMark Murray }
789511b41d2SMark Murray
790ae1f160dSDag-Erling Smørgrav static void
client_process_net_input(struct ssh * ssh)79185d1f2d4SEd Maste client_process_net_input(struct ssh *ssh)
792511b41d2SMark Murray {
79385d1f2d4SEd Maste int r;
794511b41d2SMark Murray
795511b41d2SMark Murray /*
796511b41d2SMark Murray * Read input from the server, and add any such data to the buffer of
797511b41d2SMark Murray * the packet subsystem.
798511b41d2SMark Murray */
7993bbd8dc9SEd Maste schedule_server_alive_check();
80085d1f2d4SEd Maste if ((r = ssh_packet_process_read(ssh, connection_in)) == 0)
80185d1f2d4SEd Maste return; /* success */
80285d1f2d4SEd Maste if (r == SSH_ERR_SYSTEM_ERROR) {
80385d1f2d4SEd Maste if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)
80485d1f2d4SEd Maste return;
80585d1f2d4SEd Maste if (errno == EPIPE) {
80685d1f2d4SEd Maste quit_message("Connection to %s closed by remote host.",
80785d1f2d4SEd Maste host);
808511b41d2SMark Murray return;
809511b41d2SMark Murray }
810511b41d2SMark Murray }
81185d1f2d4SEd Maste quit_message("Read from remote host %s: %s", host, ssh_err(r));
812a04a10f8SKris Kennaway }
813a04a10f8SKris Kennaway
814545d5ecaSDag-Erling Smørgrav static void
client_status_confirm(struct ssh * ssh,int type,Channel * c,void * ctx)81520adc8f2SDag-Erling Smørgrav client_status_confirm(struct ssh *ssh, int type, Channel *c, void *ctx)
816d74d50a8SDag-Erling Smørgrav {
81755215393SDag-Erling Smørgrav struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
81855215393SDag-Erling Smørgrav char errmsg[256];
819d46065dfSDag-Erling Smørgrav int r, tochan;
820d74d50a8SDag-Erling Smørgrav
8211e26c29bSDag-Erling Smørgrav /*
8221e26c29bSDag-Erling Smørgrav * If a TTY was explicitly requested, then a failure to allocate
8231e26c29bSDag-Erling Smørgrav * one is fatal.
8241e26c29bSDag-Erling Smørgrav */
8251e26c29bSDag-Erling Smørgrav if (cr->action == CONFIRM_TTY &&
8261e26c29bSDag-Erling Smørgrav (options.request_tty == REQUEST_TTY_FORCE ||
8271e26c29bSDag-Erling Smørgrav options.request_tty == REQUEST_TTY_YES))
8281e26c29bSDag-Erling Smørgrav cr->action = CONFIRM_CLOSE;
8291e26c29bSDag-Erling Smørgrav
830d46065dfSDag-Erling Smørgrav /* XXX suppress on mux _client_ quietmode */
83155215393SDag-Erling Smørgrav tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
8326d4f2dd1SDag-Erling Smørgrav c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
833d74d50a8SDag-Erling Smørgrav
83455215393SDag-Erling Smørgrav if (type == SSH2_MSG_CHANNEL_SUCCESS) {
83555215393SDag-Erling Smørgrav debug2("%s request accepted on channel %d",
83655215393SDag-Erling Smørgrav cr->request_type, c->self);
83755215393SDag-Erling Smørgrav } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
83855215393SDag-Erling Smørgrav if (tochan) {
83955215393SDag-Erling Smørgrav snprintf(errmsg, sizeof(errmsg),
84055215393SDag-Erling Smørgrav "%s request failed\r\n", cr->request_type);
84155215393SDag-Erling Smørgrav } else {
84255215393SDag-Erling Smørgrav snprintf(errmsg, sizeof(errmsg),
84355215393SDag-Erling Smørgrav "%s request failed on channel %d",
84455215393SDag-Erling Smørgrav cr->request_type, c->self);
845d74d50a8SDag-Erling Smørgrav }
84655215393SDag-Erling Smørgrav /* If error occurred on primary session channel, then exit */
8471e26c29bSDag-Erling Smørgrav if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
84855215393SDag-Erling Smørgrav fatal("%s", errmsg);
8491e26c29bSDag-Erling Smørgrav /*
8501e26c29bSDag-Erling Smørgrav * If error occurred on mux client, append to
8511e26c29bSDag-Erling Smørgrav * their stderr.
8521e26c29bSDag-Erling Smørgrav */
8531e26c29bSDag-Erling Smørgrav if (tochan) {
85466719ee5SEd Maste debug3_f("channel %d: mux request: %s", c->self,
85566719ee5SEd Maste cr->request_type);
856d46065dfSDag-Erling Smørgrav if ((r = sshbuf_put(c->extended, errmsg,
857d46065dfSDag-Erling Smørgrav strlen(errmsg))) != 0)
858206be79aSEd Maste fatal_fr(r, "sshbuf_put");
8591e26c29bSDag-Erling Smørgrav } else
86055215393SDag-Erling Smørgrav error("%s", errmsg);
8611e26c29bSDag-Erling Smørgrav if (cr->action == CONFIRM_TTY) {
8621e26c29bSDag-Erling Smørgrav /*
8631e26c29bSDag-Erling Smørgrav * If a TTY allocation error occurred, then arrange
8641e26c29bSDag-Erling Smørgrav * for the correct TTY to leave raw mode.
8651e26c29bSDag-Erling Smørgrav */
8661e26c29bSDag-Erling Smørgrav if (c->self == session_ident)
8671e26c29bSDag-Erling Smørgrav leave_raw_mode(0);
8681e26c29bSDag-Erling Smørgrav else
86920adc8f2SDag-Erling Smørgrav mux_tty_alloc_failed(ssh, c);
8701e26c29bSDag-Erling Smørgrav } else if (cr->action == CONFIRM_CLOSE) {
87120adc8f2SDag-Erling Smørgrav chan_read_failed(ssh, c);
87220adc8f2SDag-Erling Smørgrav chan_write_failed(ssh, c);
873d74d50a8SDag-Erling Smørgrav }
874d74d50a8SDag-Erling Smørgrav }
8750dddc34cSDag-Erling Smørgrav free(cr);
87655215393SDag-Erling Smørgrav }
877d74d50a8SDag-Erling Smørgrav
878d74d50a8SDag-Erling Smørgrav static void
client_abandon_status_confirm(struct ssh * ssh,Channel * c,void * ctx)87920adc8f2SDag-Erling Smørgrav client_abandon_status_confirm(struct ssh *ssh, Channel *c, void *ctx)
880d74d50a8SDag-Erling Smørgrav {
8810dddc34cSDag-Erling Smørgrav free(ctx);
882d74d50a8SDag-Erling Smørgrav }
883d74d50a8SDag-Erling Smørgrav
8841e26c29bSDag-Erling Smørgrav void
client_expect_confirm(struct ssh * ssh,int id,const char * request,enum confirm_action action)88520adc8f2SDag-Erling Smørgrav client_expect_confirm(struct ssh *ssh, int id, const char *request,
8861e26c29bSDag-Erling Smørgrav enum confirm_action action)
887d74d50a8SDag-Erling Smørgrav {
888ff96c0c8SXin LI struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
889d74d50a8SDag-Erling Smørgrav
89055215393SDag-Erling Smørgrav cr->request_type = request;
8911e26c29bSDag-Erling Smørgrav cr->action = action;
892d74d50a8SDag-Erling Smørgrav
89320adc8f2SDag-Erling Smørgrav channel_register_status_confirm(ssh, id, client_status_confirm,
89455215393SDag-Erling Smørgrav client_abandon_status_confirm, cr);
89555215393SDag-Erling Smørgrav }
89655215393SDag-Erling Smørgrav
89755215393SDag-Erling Smørgrav void
client_register_global_confirm(global_confirm_cb * cb,void * ctx)89855215393SDag-Erling Smørgrav client_register_global_confirm(global_confirm_cb *cb, void *ctx)
89955215393SDag-Erling Smørgrav {
90055215393SDag-Erling Smørgrav struct global_confirm *gc, *last_gc;
90155215393SDag-Erling Smørgrav
90255215393SDag-Erling Smørgrav /* Coalesce identical callbacks */
90355215393SDag-Erling Smørgrav last_gc = TAILQ_LAST(&global_confirms, global_confirms);
90455215393SDag-Erling Smørgrav if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
90555215393SDag-Erling Smørgrav if (++last_gc->ref_count >= INT_MAX)
906206be79aSEd Maste fatal_f("last_gc->ref_count = %d",
907206be79aSEd Maste last_gc->ref_count);
908d74d50a8SDag-Erling Smørgrav return;
909d74d50a8SDag-Erling Smørgrav }
910d74d50a8SDag-Erling Smørgrav
911ff96c0c8SXin LI gc = xcalloc(1, sizeof(*gc));
91255215393SDag-Erling Smørgrav gc->cb = cb;
91355215393SDag-Erling Smørgrav gc->ctx = ctx;
91455215393SDag-Erling Smørgrav gc->ref_count = 1;
91555215393SDag-Erling Smørgrav TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
916d74d50a8SDag-Erling Smørgrav }
917d74d50a8SDag-Erling Smørgrav
9187ee81174SEd Maste /*
9197ee81174SEd Maste * Returns non-zero if the client is able to handle a hostkeys-00@openssh.com
9207ee81174SEd Maste * hostkey update request.
9217ee81174SEd Maste */
9227ee81174SEd Maste static int
can_update_hostkeys(void)9237ee81174SEd Maste can_update_hostkeys(void)
9247ee81174SEd Maste {
9257ee81174SEd Maste if (hostkeys_update_complete)
9267ee81174SEd Maste return 0;
9277ee81174SEd Maste if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
9287ee81174SEd Maste options.batch_mode)
9297ee81174SEd Maste return 0; /* won't ask in batchmode, so don't even try */
9307ee81174SEd Maste if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
9317ee81174SEd Maste return 0;
9327ee81174SEd Maste return 1;
9337ee81174SEd Maste }
9347ee81174SEd Maste
9357ee81174SEd Maste static void
client_repledge(void)9367ee81174SEd Maste client_repledge(void)
9377ee81174SEd Maste {
9387ee81174SEd Maste debug3_f("enter");
9397ee81174SEd Maste
9407ee81174SEd Maste /* Might be able to tighten pledge now that session is established */
9417ee81174SEd Maste if (options.control_master || options.control_path != NULL ||
9427ee81174SEd Maste options.forward_x11 || options.fork_after_authentication ||
9437ee81174SEd Maste can_update_hostkeys() ||
9447ee81174SEd Maste (session_ident != -1 && !session_setup_complete)) {
9457ee81174SEd Maste /* Can't tighten */
9467ee81174SEd Maste return;
9477ee81174SEd Maste }
9487ee81174SEd Maste /*
9497ee81174SEd Maste * LocalCommand and UpdateHostkeys have finished, so can get rid of
9507ee81174SEd Maste * filesystem.
9517ee81174SEd Maste *
9527ee81174SEd Maste * XXX protocol allows a server can to change hostkeys during the
9537ee81174SEd Maste * connection at rekey time that could trigger a hostkeys update
9547ee81174SEd Maste * but AFAIK no implementations support this. Could improve by
9557ee81174SEd Maste * forcing known_hosts to be read-only or via unveil(2).
9567ee81174SEd Maste */
9577ee81174SEd Maste if (options.num_local_forwards != 0 ||
9587ee81174SEd Maste options.num_remote_forwards != 0 ||
9597ee81174SEd Maste options.num_permitted_remote_opens != 0 ||
9607ee81174SEd Maste options.enable_escape_commandline != 0) {
9617ee81174SEd Maste /* rfwd needs inet */
9627ee81174SEd Maste debug("pledge: network");
9637ee81174SEd Maste if (pledge("stdio unix inet dns proc tty", NULL) == -1)
9647ee81174SEd Maste fatal_f("pledge(): %s", strerror(errno));
9657ee81174SEd Maste } else if (options.forward_agent != 0) {
9667ee81174SEd Maste /* agent forwarding needs to open $SSH_AUTH_SOCK at will */
9677ee81174SEd Maste debug("pledge: agent");
9687ee81174SEd Maste if (pledge("stdio unix proc tty", NULL) == -1)
9697ee81174SEd Maste fatal_f("pledge(): %s", strerror(errno));
9707ee81174SEd Maste } else {
9717ee81174SEd Maste debug("pledge: fork");
9727ee81174SEd Maste if (pledge("stdio proc tty", NULL) == -1)
9737ee81174SEd Maste fatal_f("pledge(): %s", strerror(errno));
9747ee81174SEd Maste }
9757ee81174SEd Maste /* XXX further things to do:
9767ee81174SEd Maste *
9777ee81174SEd Maste * - might be able to get rid of proc if we kill ~^Z
9787ee81174SEd Maste * - ssh -N (no session)
9797ee81174SEd Maste * - stdio forwarding
9807ee81174SEd Maste * - sessions without tty
9817ee81174SEd Maste */
9827ee81174SEd Maste }
9837ee81174SEd Maste
984d74d50a8SDag-Erling Smørgrav static void
process_cmdline(struct ssh * ssh)98520adc8f2SDag-Erling Smørgrav process_cmdline(struct ssh *ssh)
986545d5ecaSDag-Erling Smørgrav {
987545d5ecaSDag-Erling Smørgrav void (*handler)(int);
988c0bbca73SDag-Erling Smørgrav char *s, *cmd;
989c0bbca73SDag-Erling Smørgrav int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
990c0bbca73SDag-Erling Smørgrav struct Forward fwd;
991545d5ecaSDag-Erling Smørgrav
9920c79daccSDag-Erling Smørgrav memset(&fwd, 0, sizeof(fwd));
993ad22e48fSDag-Erling Smørgrav
9941e26c29bSDag-Erling Smørgrav leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
99582e5fdc5SEd Maste handler = ssh_signal(SIGINT, SIG_IGN);
996545d5ecaSDag-Erling Smørgrav cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
997545d5ecaSDag-Erling Smørgrav if (s == NULL)
998545d5ecaSDag-Erling Smørgrav goto out;
99902d4c2acSDag-Erling Smørgrav while (isspace((u_char)*s))
1000545d5ecaSDag-Erling Smørgrav s++;
1001d74d50a8SDag-Erling Smørgrav if (*s == '-')
1002d74d50a8SDag-Erling Smørgrav s++; /* Skip cmdline '-', if any */
1003d74d50a8SDag-Erling Smørgrav if (*s == '\0')
1004545d5ecaSDag-Erling Smørgrav goto out;
1005d74d50a8SDag-Erling Smørgrav
1006d74d50a8SDag-Erling Smørgrav if (*s == 'h' || *s == 'H' || *s == '?') {
1007d74d50a8SDag-Erling Smørgrav logit("Commands:");
1008761efaa7SDag-Erling Smørgrav logit(" -L[bind_address:]port:host:hostport "
1009761efaa7SDag-Erling Smørgrav "Request local forward");
1010761efaa7SDag-Erling Smørgrav logit(" -R[bind_address:]port:host:hostport "
1011761efaa7SDag-Erling Smørgrav "Request remote forward");
10129ab1052dSDag-Erling Smørgrav logit(" -D[bind_address:]port "
10139ab1052dSDag-Erling Smørgrav "Request dynamic forward");
10142e97a369SDag-Erling Smørgrav logit(" -KL[bind_address:]port "
10152e97a369SDag-Erling Smørgrav "Cancel local forward");
1016761efaa7SDag-Erling Smørgrav logit(" -KR[bind_address:]port "
1017761efaa7SDag-Erling Smørgrav "Cancel remote forward");
10182e97a369SDag-Erling Smørgrav logit(" -KD[bind_address:]port "
10192e97a369SDag-Erling Smørgrav "Cancel dynamic forward");
1020021d409fSDag-Erling Smørgrav if (!options.permit_local_command)
1021021d409fSDag-Erling Smørgrav goto out;
1022761efaa7SDag-Erling Smørgrav logit(" !args "
1023761efaa7SDag-Erling Smørgrav "Execute local command");
1024021d409fSDag-Erling Smørgrav goto out;
1025021d409fSDag-Erling Smørgrav }
1026021d409fSDag-Erling Smørgrav
1027021d409fSDag-Erling Smørgrav if (*s == '!' && options.permit_local_command) {
1028021d409fSDag-Erling Smørgrav s++;
1029021d409fSDag-Erling Smørgrav ssh_local_cmd(s);
1030d74d50a8SDag-Erling Smørgrav goto out;
1031d74d50a8SDag-Erling Smørgrav }
1032d74d50a8SDag-Erling Smørgrav
1033d74d50a8SDag-Erling Smørgrav if (*s == 'K') {
1034d74d50a8SDag-Erling Smørgrav delete = 1;
1035d74d50a8SDag-Erling Smørgrav s++;
1036d74d50a8SDag-Erling Smørgrav }
10379ab1052dSDag-Erling Smørgrav if (*s == 'L')
10389ab1052dSDag-Erling Smørgrav local = 1;
10399ab1052dSDag-Erling Smørgrav else if (*s == 'R')
10409ab1052dSDag-Erling Smørgrav remote = 1;
10419ab1052dSDag-Erling Smørgrav else if (*s == 'D')
10429ab1052dSDag-Erling Smørgrav dynamic = 1;
10439ab1052dSDag-Erling Smørgrav else {
1044d95e11bfSDag-Erling Smørgrav logit("Invalid command.");
1045545d5ecaSDag-Erling Smørgrav goto out;
1046545d5ecaSDag-Erling Smørgrav }
10479ab1052dSDag-Erling Smørgrav
104802d4c2acSDag-Erling Smørgrav while (isspace((u_char)*++s))
1049490bfaadSDag-Erling Smørgrav ;
1050545d5ecaSDag-Erling Smørgrav
10516d4f2dd1SDag-Erling Smørgrav /* XXX update list of forwards in options */
1052d74d50a8SDag-Erling Smørgrav if (delete) {
1053c0bbca73SDag-Erling Smørgrav /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
1054c0bbca73SDag-Erling Smørgrav if (!parse_forward(&fwd, s, 1, 0)) {
1055c0bbca73SDag-Erling Smørgrav logit("Bad forwarding close specification.");
1056545d5ecaSDag-Erling Smørgrav goto out;
1057545d5ecaSDag-Erling Smørgrav }
10582e97a369SDag-Erling Smørgrav if (remote)
105920adc8f2SDag-Erling Smørgrav ok = channel_request_rforward_cancel(ssh, &fwd) == 0;
10602e97a369SDag-Erling Smørgrav else if (dynamic)
106120adc8f2SDag-Erling Smørgrav ok = channel_cancel_lport_listener(ssh, &fwd,
1062c0bbca73SDag-Erling Smørgrav 0, &options.fwd_opts) > 0;
10632e97a369SDag-Erling Smørgrav else
106420adc8f2SDag-Erling Smørgrav ok = channel_cancel_lport_listener(ssh, &fwd,
1065c0bbca73SDag-Erling Smørgrav CHANNEL_CANCEL_PORT_STATIC,
1066c0bbca73SDag-Erling Smørgrav &options.fwd_opts) > 0;
10672e97a369SDag-Erling Smørgrav if (!ok) {
1068343d5771SDag-Erling Smørgrav logit("Unknown port forwarding.");
10692e97a369SDag-Erling Smørgrav goto out;
10702e97a369SDag-Erling Smørgrav }
10712e97a369SDag-Erling Smørgrav logit("Canceled forwarding.");
10725e8dbd04SDag-Erling Smørgrav } else {
10737ee81174SEd Maste /* -R specs can be both dynamic or not, so check both. */
10747ee81174SEd Maste if (remote) {
10757ee81174SEd Maste if (!parse_forward(&fwd, s, 0, remote) &&
10767ee81174SEd Maste !parse_forward(&fwd, s, 1, remote)) {
10777ee81174SEd Maste logit("Bad remote forwarding specification.");
10787ee81174SEd Maste goto out;
10797ee81174SEd Maste }
10807ee81174SEd Maste } else if (!parse_forward(&fwd, s, dynamic, remote)) {
10817ee81174SEd Maste logit("Bad local forwarding specification.");
1082545d5ecaSDag-Erling Smørgrav goto out;
1083545d5ecaSDag-Erling Smørgrav }
10849ab1052dSDag-Erling Smørgrav if (local || dynamic) {
108520adc8f2SDag-Erling Smørgrav if (!channel_setup_local_fwd_listener(ssh, &fwd,
1086c0bbca73SDag-Erling Smørgrav &options.fwd_opts)) {
1087d95e11bfSDag-Erling Smørgrav logit("Port forwarding failed.");
1088545d5ecaSDag-Erling Smørgrav goto out;
1089545d5ecaSDag-Erling Smørgrav }
10905e8dbd04SDag-Erling Smørgrav } else {
109120adc8f2SDag-Erling Smørgrav if (channel_request_remote_forwarding(ssh, &fwd) < 0) {
1092761efaa7SDag-Erling Smørgrav logit("Port forwarding failed.");
1093761efaa7SDag-Erling Smørgrav goto out;
1094761efaa7SDag-Erling Smørgrav }
10955e8dbd04SDag-Erling Smørgrav }
1096d95e11bfSDag-Erling Smørgrav logit("Forwarding port.");
1097d74d50a8SDag-Erling Smørgrav }
1098d74d50a8SDag-Erling Smørgrav
1099545d5ecaSDag-Erling Smørgrav out:
110082e5fdc5SEd Maste ssh_signal(SIGINT, handler);
11011e26c29bSDag-Erling Smørgrav enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
11020dddc34cSDag-Erling Smørgrav free(cmd);
11030dddc34cSDag-Erling Smørgrav free(fwd.listen_host);
1104c0bbca73SDag-Erling Smørgrav free(fwd.listen_path);
11050dddc34cSDag-Erling Smørgrav free(fwd.connect_host);
1106c0bbca73SDag-Erling Smørgrav free(fwd.connect_path);
1107545d5ecaSDag-Erling Smørgrav }
1108545d5ecaSDag-Erling Smørgrav
11099b81c128SDag-Erling Smørgrav /* reasons to suppress output of an escape command in help output */
11109b81c128SDag-Erling Smørgrav #define SUPPRESS_NEVER 0 /* never suppress, always show */
111120adc8f2SDag-Erling Smørgrav #define SUPPRESS_MUXCLIENT 1 /* don't show in mux client sessions */
111220adc8f2SDag-Erling Smørgrav #define SUPPRESS_MUXMASTER 2 /* don't show in mux master sessions */
111320adc8f2SDag-Erling Smørgrav #define SUPPRESS_SYSLOG 4 /* don't show when logging to syslog */
11147ee81174SEd Maste #define SUPPRESS_NOCMDLINE 8 /* don't show when cmdline disabled*/
11159b81c128SDag-Erling Smørgrav struct escape_help_text {
11169b81c128SDag-Erling Smørgrav const char *cmd;
11179b81c128SDag-Erling Smørgrav const char *text;
11189b81c128SDag-Erling Smørgrav unsigned int flags;
11199b81c128SDag-Erling Smørgrav };
11209b81c128SDag-Erling Smørgrav static struct escape_help_text esc_txt[] = {
11219b81c128SDag-Erling Smørgrav {".", "terminate session", SUPPRESS_MUXMASTER},
11229b81c128SDag-Erling Smørgrav {".", "terminate connection (and any multiplexed sessions)",
11239b81c128SDag-Erling Smørgrav SUPPRESS_MUXCLIENT},
112420adc8f2SDag-Erling Smørgrav {"B", "send a BREAK to the remote system", SUPPRESS_NEVER},
11257ee81174SEd Maste {"C", "open a command line", SUPPRESS_MUXCLIENT|SUPPRESS_NOCMDLINE},
112620adc8f2SDag-Erling Smørgrav {"R", "request rekey", SUPPRESS_NEVER},
11279b81c128SDag-Erling Smørgrav {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
11289b81c128SDag-Erling Smørgrav {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
11299b81c128SDag-Erling Smørgrav {"#", "list forwarded connections", SUPPRESS_NEVER},
11309b81c128SDag-Erling Smørgrav {"&", "background ssh (when waiting for connections to terminate)",
11319b81c128SDag-Erling Smørgrav SUPPRESS_MUXCLIENT},
11329b81c128SDag-Erling Smørgrav {"?", "this message", SUPPRESS_NEVER},
11339b81c128SDag-Erling Smørgrav };
11349b81c128SDag-Erling Smørgrav
11359b81c128SDag-Erling Smørgrav static void
print_escape_help(struct sshbuf * b,int escape_char,int mux_client,int using_stderr)1136d46065dfSDag-Erling Smørgrav print_escape_help(struct sshbuf *b, int escape_char, int mux_client,
1137d46065dfSDag-Erling Smørgrav int using_stderr)
11389b81c128SDag-Erling Smørgrav {
11399b81c128SDag-Erling Smørgrav unsigned int i, suppress_flags;
1140d46065dfSDag-Erling Smørgrav int r;
11419b81c128SDag-Erling Smørgrav
1142d46065dfSDag-Erling Smørgrav if ((r = sshbuf_putf(b,
1143d46065dfSDag-Erling Smørgrav "%c?\r\nSupported escape sequences:\r\n", escape_char)) != 0)
1144206be79aSEd Maste fatal_fr(r, "sshbuf_putf");
11459b81c128SDag-Erling Smørgrav
114620adc8f2SDag-Erling Smørgrav suppress_flags =
11479b81c128SDag-Erling Smørgrav (mux_client ? SUPPRESS_MUXCLIENT : 0) |
11489b81c128SDag-Erling Smørgrav (mux_client ? 0 : SUPPRESS_MUXMASTER) |
11497ee81174SEd Maste (using_stderr ? 0 : SUPPRESS_SYSLOG) |
11507ee81174SEd Maste (options.enable_escape_commandline == 0 ? SUPPRESS_NOCMDLINE : 0);
11519b81c128SDag-Erling Smørgrav
11529b81c128SDag-Erling Smørgrav for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
11539b81c128SDag-Erling Smørgrav if (esc_txt[i].flags & suppress_flags)
11549b81c128SDag-Erling Smørgrav continue;
1155d46065dfSDag-Erling Smørgrav if ((r = sshbuf_putf(b, " %c%-3s - %s\r\n",
1156d46065dfSDag-Erling Smørgrav escape_char, esc_txt[i].cmd, esc_txt[i].text)) != 0)
1157206be79aSEd Maste fatal_fr(r, "sshbuf_putf");
11589b81c128SDag-Erling Smørgrav }
11599b81c128SDag-Erling Smørgrav
1160d46065dfSDag-Erling Smørgrav if ((r = sshbuf_putf(b,
11619b81c128SDag-Erling Smørgrav " %c%c - send the escape character by typing it twice\r\n"
11629b81c128SDag-Erling Smørgrav "(Note that escapes are only recognized immediately after "
1163d46065dfSDag-Erling Smørgrav "newline.)\r\n", escape_char, escape_char)) != 0)
1164206be79aSEd Maste fatal_fr(r, "sshbuf_putf");
11659b81c128SDag-Erling Smørgrav }
11669b81c128SDag-Erling Smørgrav
116755215393SDag-Erling Smørgrav /*
116820adc8f2SDag-Erling Smørgrav * Process the characters one by one.
116955215393SDag-Erling Smørgrav */
1170ae1f160dSDag-Erling Smørgrav static int
process_escapes(struct ssh * ssh,Channel * c,struct sshbuf * bin,struct sshbuf * bout,struct sshbuf * berr,char * buf,int len)117120adc8f2SDag-Erling Smørgrav process_escapes(struct ssh *ssh, Channel *c,
1172d46065dfSDag-Erling Smørgrav struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr,
117355215393SDag-Erling Smørgrav char *buf, int len)
1174b66f2d16SKris Kennaway {
1175b66f2d16SKris Kennaway pid_t pid;
1176d46065dfSDag-Erling Smørgrav int r, bytes = 0;
11771e8db6e2SBrian Feldman u_int i;
11781e8db6e2SBrian Feldman u_char ch;
1179b66f2d16SKris Kennaway char *s;
118078f30535SEd Maste struct escape_filter_ctx *efc;
118155215393SDag-Erling Smørgrav
118278f30535SEd Maste if (c == NULL || c->filter_ctx == NULL || len <= 0)
118355215393SDag-Erling Smørgrav return 0;
1184b66f2d16SKris Kennaway
118578f30535SEd Maste efc = (struct escape_filter_ctx *)c->filter_ctx;
1186043840dfSDag-Erling Smørgrav
1187043840dfSDag-Erling Smørgrav for (i = 0; i < (u_int)len; i++) {
1188b66f2d16SKris Kennaway /* Get one character at a time. */
1189b66f2d16SKris Kennaway ch = buf[i];
1190b66f2d16SKris Kennaway
119120adc8f2SDag-Erling Smørgrav if (efc->escape_pending) {
1192b66f2d16SKris Kennaway /* We have previously seen an escape character. */
1193b66f2d16SKris Kennaway /* Clear the flag now. */
119420adc8f2SDag-Erling Smørgrav efc->escape_pending = 0;
1195b66f2d16SKris Kennaway
1196b66f2d16SKris Kennaway /* Process the escaped character. */
1197b66f2d16SKris Kennaway switch (ch) {
1198b66f2d16SKris Kennaway case '.':
1199b66f2d16SKris Kennaway /* Terminate the connection. */
1200d46065dfSDag-Erling Smørgrav if ((r = sshbuf_putf(berr, "%c.\r\n",
1201d46065dfSDag-Erling Smørgrav efc->escape_char)) != 0)
1202206be79aSEd Maste fatal_fr(r, "sshbuf_putf");
12036d4f2dd1SDag-Erling Smørgrav if (c && c->ctl_chan != -1) {
12047ee81174SEd Maste channel_force_close(ssh, c, 1);
120555215393SDag-Erling Smørgrav return 0;
120655215393SDag-Erling Smørgrav } else
1207b66f2d16SKris Kennaway quit_pending = 1;
1208b66f2d16SKris Kennaway return -1;
1209b66f2d16SKris Kennaway
1210b66f2d16SKris Kennaway case 'Z' - 64:
121155215393SDag-Erling Smørgrav /* XXX support this for mux clients */
12126d4f2dd1SDag-Erling Smørgrav if (c && c->ctl_chan != -1) {
12139b81c128SDag-Erling Smørgrav char b[16];
121455215393SDag-Erling Smørgrav noescape:
12159b81c128SDag-Erling Smørgrav if (ch == 'Z' - 64)
12169b81c128SDag-Erling Smørgrav snprintf(b, sizeof b, "^Z");
12179b81c128SDag-Erling Smørgrav else
12189b81c128SDag-Erling Smørgrav snprintf(b, sizeof b, "%c", ch);
1219d46065dfSDag-Erling Smørgrav if ((r = sshbuf_putf(berr,
12209b81c128SDag-Erling Smørgrav "%c%s escape not available to "
122155215393SDag-Erling Smørgrav "multiplexed sessions\r\n",
1222d46065dfSDag-Erling Smørgrav efc->escape_char, b)) != 0)
1223206be79aSEd Maste fatal_fr(r, "sshbuf_putf");
122455215393SDag-Erling Smørgrav continue;
122555215393SDag-Erling Smørgrav }
122655215393SDag-Erling Smørgrav /* Suspend the program. Inform the user */
1227d46065dfSDag-Erling Smørgrav if ((r = sshbuf_putf(berr,
1228d46065dfSDag-Erling Smørgrav "%c^Z [suspend ssh]\r\n",
1229d46065dfSDag-Erling Smørgrav efc->escape_char)) != 0)
1230206be79aSEd Maste fatal_fr(r, "sshbuf_putf");
1231b66f2d16SKris Kennaway
1232b66f2d16SKris Kennaway /* Restore terminal modes and suspend. */
1233b66f2d16SKris Kennaway client_suspend_self(bin, bout, berr);
1234b66f2d16SKris Kennaway
1235b66f2d16SKris Kennaway /* We have been continued. */
1236b66f2d16SKris Kennaway continue;
1237b66f2d16SKris Kennaway
1238d95e11bfSDag-Erling Smørgrav case 'B':
1239d46065dfSDag-Erling Smørgrav if ((r = sshbuf_putf(berr,
1240d46065dfSDag-Erling Smørgrav "%cB\r\n", efc->escape_char)) != 0)
1241206be79aSEd Maste fatal_fr(r, "sshbuf_putf");
124220adc8f2SDag-Erling Smørgrav channel_request_start(ssh, c->self, "break", 0);
1243d46065dfSDag-Erling Smørgrav if ((r = sshpkt_put_u32(ssh, 1000)) != 0 ||
1244d46065dfSDag-Erling Smørgrav (r = sshpkt_send(ssh)) != 0)
1245206be79aSEd Maste fatal_fr(r, "send packet");
1246d95e11bfSDag-Erling Smørgrav continue;
1247d95e11bfSDag-Erling Smørgrav
12481e8db6e2SBrian Feldman case 'R':
1249206be79aSEd Maste if (ssh->compat & SSH_BUG_NOREKEY)
125055215393SDag-Erling Smørgrav logit("Server does not "
125155215393SDag-Erling Smørgrav "support re-keying");
12521e8db6e2SBrian Feldman else
12531e8db6e2SBrian Feldman need_rekeying = 1;
12541e8db6e2SBrian Feldman continue;
12551e8db6e2SBrian Feldman
12569b81c128SDag-Erling Smørgrav case 'V':
12579b81c128SDag-Erling Smørgrav /* FALLTHROUGH */
12589b81c128SDag-Erling Smørgrav case 'v':
12599b81c128SDag-Erling Smørgrav if (c && c->ctl_chan != -1)
12609b81c128SDag-Erling Smørgrav goto noescape;
12619b81c128SDag-Erling Smørgrav if (!log_is_on_stderr()) {
1262d46065dfSDag-Erling Smørgrav if ((r = sshbuf_putf(berr,
12639b81c128SDag-Erling Smørgrav "%c%c [Logging to syslog]\r\n",
1264d46065dfSDag-Erling Smørgrav efc->escape_char, ch)) != 0)
1265206be79aSEd Maste fatal_fr(r, "sshbuf_putf");
12669b81c128SDag-Erling Smørgrav continue;
12679b81c128SDag-Erling Smørgrav }
12689b81c128SDag-Erling Smørgrav if (ch == 'V' && options.log_level >
12699b81c128SDag-Erling Smørgrav SYSLOG_LEVEL_QUIET)
12709b81c128SDag-Erling Smørgrav log_change_level(--options.log_level);
12719b81c128SDag-Erling Smørgrav if (ch == 'v' && options.log_level <
12729b81c128SDag-Erling Smørgrav SYSLOG_LEVEL_DEBUG3)
12739b81c128SDag-Erling Smørgrav log_change_level(++options.log_level);
1274d46065dfSDag-Erling Smørgrav if ((r = sshbuf_putf(berr,
127520adc8f2SDag-Erling Smørgrav "%c%c [LogLevel %s]\r\n",
127620adc8f2SDag-Erling Smørgrav efc->escape_char, ch,
1277d46065dfSDag-Erling Smørgrav log_level_name(options.log_level))) != 0)
1278206be79aSEd Maste fatal_fr(r, "sshbuf_putf");
12799b81c128SDag-Erling Smørgrav continue;
12809b81c128SDag-Erling Smørgrav
1281b66f2d16SKris Kennaway case '&':
128278f30535SEd Maste if (c->ctl_chan != -1)
128355215393SDag-Erling Smørgrav goto noescape;
1284b66f2d16SKris Kennaway /*
128555215393SDag-Erling Smørgrav * Detach the program (continue to serve
128655215393SDag-Erling Smørgrav * connections, but put in background and no
128755215393SDag-Erling Smørgrav * more new connections).
1288b66f2d16SKris Kennaway */
1289ae1f160dSDag-Erling Smørgrav /* Restore tty modes. */
12901e26c29bSDag-Erling Smørgrav leave_raw_mode(
12911e26c29bSDag-Erling Smørgrav options.request_tty == REQUEST_TTY_FORCE);
1292ae1f160dSDag-Erling Smørgrav
1293ae1f160dSDag-Erling Smørgrav /* Stop listening for new connections. */
129420adc8f2SDag-Erling Smørgrav channel_stop_listening(ssh);
1295ae1f160dSDag-Erling Smørgrav
1296206be79aSEd Maste if ((r = sshbuf_putf(berr, "%c& "
1297206be79aSEd Maste "[backgrounded]\n", efc->escape_char)) != 0)
1298206be79aSEd Maste fatal_fr(r, "sshbuf_putf");
1299ae1f160dSDag-Erling Smørgrav
1300ae1f160dSDag-Erling Smørgrav /* Fork into background. */
1301ae1f160dSDag-Erling Smørgrav pid = fork();
13020194e6d0SEd Maste if (pid == -1) {
1303ae1f160dSDag-Erling Smørgrav error("fork: %.100s", strerror(errno));
1304ae1f160dSDag-Erling Smørgrav continue;
1305ae1f160dSDag-Erling Smørgrav }
1306ae1f160dSDag-Erling Smørgrav if (pid != 0) { /* This is the parent. */
1307ae1f160dSDag-Erling Smørgrav /* The parent just exits. */
1308ae1f160dSDag-Erling Smørgrav exit(0);
1309ae1f160dSDag-Erling Smørgrav }
1310ae1f160dSDag-Erling Smørgrav /* The child continues serving connections. */
1311ae1f160dSDag-Erling Smørgrav /* fake EOF on stdin */
1312d46065dfSDag-Erling Smørgrav if ((r = sshbuf_put_u8(bin, 4)) != 0)
1313206be79aSEd Maste fatal_fr(r, "sshbuf_put_u8");
1314ae1f160dSDag-Erling Smørgrav return -1;
1315b66f2d16SKris Kennaway case '?':
131620adc8f2SDag-Erling Smørgrav print_escape_help(berr, efc->escape_char,
13179b81c128SDag-Erling Smørgrav (c && c->ctl_chan != -1),
13189b81c128SDag-Erling Smørgrav log_is_on_stderr());
1319b66f2d16SKris Kennaway continue;
1320b66f2d16SKris Kennaway
1321b66f2d16SKris Kennaway case '#':
1322d46065dfSDag-Erling Smørgrav if ((r = sshbuf_putf(berr, "%c#\r\n",
1323d46065dfSDag-Erling Smørgrav efc->escape_char)) != 0)
1324206be79aSEd Maste fatal_fr(r, "sshbuf_putf");
132520adc8f2SDag-Erling Smørgrav s = channel_open_message(ssh);
1326d46065dfSDag-Erling Smørgrav if ((r = sshbuf_put(berr, s, strlen(s))) != 0)
1327206be79aSEd Maste fatal_fr(r, "sshbuf_put");
13280dddc34cSDag-Erling Smørgrav free(s);
1329b66f2d16SKris Kennaway continue;
1330b66f2d16SKris Kennaway
1331545d5ecaSDag-Erling Smørgrav case 'C':
13326d4f2dd1SDag-Erling Smørgrav if (c && c->ctl_chan != -1)
13339ab1052dSDag-Erling Smørgrav goto noescape;
13347ee81174SEd Maste if (options.enable_escape_commandline == 0) {
13357ee81174SEd Maste if ((r = sshbuf_putf(berr,
13367ee81174SEd Maste "commandline disabled\r\n")) != 0)
13377ee81174SEd Maste fatal_fr(r, "sshbuf_putf");
13387ee81174SEd Maste continue;
13397ee81174SEd Maste }
134020adc8f2SDag-Erling Smørgrav process_cmdline(ssh);
1341545d5ecaSDag-Erling Smørgrav continue;
1342545d5ecaSDag-Erling Smørgrav
1343b66f2d16SKris Kennaway default:
134420adc8f2SDag-Erling Smørgrav if (ch != efc->escape_char) {
1345d46065dfSDag-Erling Smørgrav if ((r = sshbuf_put_u8(bin,
1346d46065dfSDag-Erling Smørgrav efc->escape_char)) != 0)
1347206be79aSEd Maste fatal_fr(r, "sshbuf_put_u8");
1348b66f2d16SKris Kennaway bytes++;
1349b66f2d16SKris Kennaway }
1350b66f2d16SKris Kennaway /* Escaped characters fall through here */
1351b66f2d16SKris Kennaway break;
1352b66f2d16SKris Kennaway }
1353b66f2d16SKris Kennaway } else {
1354b66f2d16SKris Kennaway /*
135555215393SDag-Erling Smørgrav * The previous character was not an escape char.
135655215393SDag-Erling Smørgrav * Check if this is an escape.
1357b66f2d16SKris Kennaway */
135820adc8f2SDag-Erling Smørgrav if (last_was_cr && ch == efc->escape_char) {
135955215393SDag-Erling Smørgrav /*
136055215393SDag-Erling Smørgrav * It is. Set the flag and continue to
136155215393SDag-Erling Smørgrav * next character.
136255215393SDag-Erling Smørgrav */
136320adc8f2SDag-Erling Smørgrav efc->escape_pending = 1;
1364b66f2d16SKris Kennaway continue;
1365b66f2d16SKris Kennaway }
1366b66f2d16SKris Kennaway }
1367b66f2d16SKris Kennaway
1368b66f2d16SKris Kennaway /*
1369b66f2d16SKris Kennaway * Normal character. Record whether it was a newline,
1370b66f2d16SKris Kennaway * and append it to the buffer.
1371b66f2d16SKris Kennaway */
1372b66f2d16SKris Kennaway last_was_cr = (ch == '\r' || ch == '\n');
1373d46065dfSDag-Erling Smørgrav if ((r = sshbuf_put_u8(bin, ch)) != 0)
1374206be79aSEd Maste fatal_fr(r, "sshbuf_put_u8");
1375b66f2d16SKris Kennaway bytes++;
1376b66f2d16SKris Kennaway }
1377b66f2d16SKris Kennaway return bytes;
1378b66f2d16SKris Kennaway }
1379b66f2d16SKris Kennaway
1380511b41d2SMark Murray /*
1381a04a10f8SKris Kennaway * Get packets from the connection input buffer, and process them as long as
1382a04a10f8SKris Kennaway * there are packets available.
1383a04a10f8SKris Kennaway *
1384a04a10f8SKris Kennaway * Any unknown packets received during the actual
1385a04a10f8SKris Kennaway * session cause the session to terminate. This is
1386a04a10f8SKris Kennaway * intended to make debugging easier since no
1387a04a10f8SKris Kennaway * confirmations are sent. Any compatible protocol
1388a04a10f8SKris Kennaway * extensions must be negotiated during the
1389a04a10f8SKris Kennaway * preparatory phase.
1390a04a10f8SKris Kennaway */
1391a04a10f8SKris Kennaway
1392ae1f160dSDag-Erling Smørgrav static void
client_process_buffered_input_packets(struct ssh * ssh)1393f02e3998SEd Maste client_process_buffered_input_packets(struct ssh *ssh)
1394a04a10f8SKris Kennaway {
1395f02e3998SEd Maste ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, &quit_pending);
1396a04a10f8SKris Kennaway }
1397a04a10f8SKris Kennaway
1398b66f2d16SKris Kennaway /* scan buf[] for '~' before sending data to the peer */
1399b66f2d16SKris Kennaway
140055215393SDag-Erling Smørgrav /* Helper: allocate a new escape_filter_ctx and fill in its escape char */
140155215393SDag-Erling Smørgrav void *
client_new_escape_filter_ctx(int escape_char)140255215393SDag-Erling Smørgrav client_new_escape_filter_ctx(int escape_char)
1403b66f2d16SKris Kennaway {
140455215393SDag-Erling Smørgrav struct escape_filter_ctx *ret;
140555215393SDag-Erling Smørgrav
1406ff96c0c8SXin LI ret = xcalloc(1, sizeof(*ret));
140755215393SDag-Erling Smørgrav ret->escape_pending = 0;
140855215393SDag-Erling Smørgrav ret->escape_char = escape_char;
140955215393SDag-Erling Smørgrav return (void *)ret;
141055215393SDag-Erling Smørgrav }
141155215393SDag-Erling Smørgrav
141255215393SDag-Erling Smørgrav /* Free the escape filter context on channel free */
141355215393SDag-Erling Smørgrav void
client_filter_cleanup(struct ssh * ssh,int cid,void * ctx)141420adc8f2SDag-Erling Smørgrav client_filter_cleanup(struct ssh *ssh, int cid, void *ctx)
141555215393SDag-Erling Smørgrav {
14160dddc34cSDag-Erling Smørgrav free(ctx);
141755215393SDag-Erling Smørgrav }
141855215393SDag-Erling Smørgrav
141955215393SDag-Erling Smørgrav int
client_simple_escape_filter(struct ssh * ssh,Channel * c,char * buf,int len)142020adc8f2SDag-Erling Smørgrav client_simple_escape_filter(struct ssh *ssh, Channel *c, char *buf, int len)
142155215393SDag-Erling Smørgrav {
142255215393SDag-Erling Smørgrav if (c->extended_usage != CHAN_EXTENDED_WRITE)
142355215393SDag-Erling Smørgrav return 0;
142455215393SDag-Erling Smørgrav
142520adc8f2SDag-Erling Smørgrav return process_escapes(ssh, c, c->input, c->output, c->extended,
142655215393SDag-Erling Smørgrav buf, len);
1427b66f2d16SKris Kennaway }
1428b66f2d16SKris Kennaway
1429ae1f160dSDag-Erling Smørgrav static void
client_channel_closed(struct ssh * ssh,int id,int force,void * arg)14307ee81174SEd Maste client_channel_closed(struct ssh *ssh, int id, int force, void *arg)
14311e8db6e2SBrian Feldman {
143220adc8f2SDag-Erling Smørgrav channel_cancel_cleanup(ssh, id);
14331e8db6e2SBrian Feldman session_closed = 1;
14341e26c29bSDag-Erling Smørgrav leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
14351e8db6e2SBrian Feldman }
14361e8db6e2SBrian Feldman
1437a04a10f8SKris Kennaway /*
1438511b41d2SMark Murray * Implements the interactive session with the server. This is called after
1439511b41d2SMark Murray * the user has been authenticated, and a command has been started on the
1440ae1f160dSDag-Erling Smørgrav * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1441ae1f160dSDag-Erling Smørgrav * used as an escape character for terminating or suspending the session.
1442511b41d2SMark Murray */
1443511b41d2SMark Murray int
client_loop(struct ssh * ssh,int have_pty,int escape_char_arg,int ssh2_chan_id)144420adc8f2SDag-Erling Smørgrav client_loop(struct ssh *ssh, int have_pty, int escape_char_arg,
144520adc8f2SDag-Erling Smørgrav int ssh2_chan_id)
1446511b41d2SMark Murray {
144785d1f2d4SEd Maste struct pollfd *pfd = NULL;
144885d1f2d4SEd Maste u_int npfd_alloc = 0, npfd_active = 0;
1449511b41d2SMark Murray double start_time, total_time;
1450d565364dSEd Maste int channel_did_enqueue = 0, r;
145155215393SDag-Erling Smørgrav u_int64_t ibytes, obytes;
145285d1f2d4SEd Maste int conn_in_ready, conn_out_ready;
145338f55691SGordon Tetlow sigset_t bsigset, osigset;
1454511b41d2SMark Murray
1455511b41d2SMark Murray debug("Entering interactive session.");
14567ee81174SEd Maste session_ident = ssh2_chan_id;
1457511b41d2SMark Murray
1458ff4b04e0SDag-Erling Smørgrav if (options.control_master &&
1459ff4b04e0SDag-Erling Smørgrav !option_clear_or_none(options.control_path)) {
1460ff4b04e0SDag-Erling Smørgrav debug("pledge: id");
14610194e6d0SEd Maste if (pledge("stdio rpath wpath cpath unix inet dns recvfd sendfd proc exec id tty",
1462ff4b04e0SDag-Erling Smørgrav NULL) == -1)
1463206be79aSEd Maste fatal_f("pledge(): %s", strerror(errno));
1464ff4b04e0SDag-Erling Smørgrav
1465ff4b04e0SDag-Erling Smørgrav } else if (options.forward_x11 || options.permit_local_command) {
1466ff4b04e0SDag-Erling Smørgrav debug("pledge: exec");
1467ff4b04e0SDag-Erling Smørgrav if (pledge("stdio rpath wpath cpath unix inet dns proc exec tty",
1468ff4b04e0SDag-Erling Smørgrav NULL) == -1)
1469206be79aSEd Maste fatal_f("pledge(): %s", strerror(errno));
1470ff4b04e0SDag-Erling Smørgrav
1471ff4b04e0SDag-Erling Smørgrav } else if (options.update_hostkeys) {
147285d1f2d4SEd Maste debug("pledge: filesystem");
1473ff4b04e0SDag-Erling Smørgrav if (pledge("stdio rpath wpath cpath unix inet dns proc tty",
1474ff4b04e0SDag-Erling Smørgrav NULL) == -1)
1475206be79aSEd Maste fatal_f("pledge(): %s", strerror(errno));
1476ff4b04e0SDag-Erling Smørgrav
1477ab4ec008SDag-Erling Smørgrav } else if (!option_clear_or_none(options.proxy_command) ||
147866719ee5SEd Maste options.fork_after_authentication) {
1479ff4b04e0SDag-Erling Smørgrav debug("pledge: proc");
1480ff4b04e0SDag-Erling Smørgrav if (pledge("stdio cpath unix inet dns proc tty", NULL) == -1)
1481206be79aSEd Maste fatal_f("pledge(): %s", strerror(errno));
1482ff4b04e0SDag-Erling Smørgrav
1483ff4b04e0SDag-Erling Smørgrav } else {
1484ff4b04e0SDag-Erling Smørgrav debug("pledge: network");
148520adc8f2SDag-Erling Smørgrav if (pledge("stdio unix inet dns proc tty", NULL) == -1)
1486206be79aSEd Maste fatal_f("pledge(): %s", strerror(errno));
1487ff4b04e0SDag-Erling Smørgrav }
1488ff4b04e0SDag-Erling Smørgrav
14897ee81174SEd Maste /* might be able to tighten now */
14907ee81174SEd Maste client_repledge();
14917ee81174SEd Maste
1492c8a2bf14SDag-Erling Smørgrav start_time = monotime_double();
1493511b41d2SMark Murray
1494511b41d2SMark Murray /* Initialize variables. */
1495511b41d2SMark Murray last_was_cr = 1;
1496511b41d2SMark Murray exit_status = -1;
1497f02e3998SEd Maste connection_in = ssh_packet_get_connection_in(ssh);
1498f02e3998SEd Maste connection_out = ssh_packet_get_connection_out(ssh);
14991e8db6e2SBrian Feldman
1500511b41d2SMark Murray quit_pending = 0;
1501511b41d2SMark Murray
1502f02e3998SEd Maste client_init_dispatch(ssh);
1503a04a10f8SKris Kennaway
1504d0c8c0bcSDag-Erling Smørgrav /*
1505d0c8c0bcSDag-Erling Smørgrav * Set signal handlers, (e.g. to restore non-blocking mode)
1506d0c8c0bcSDag-Erling Smørgrav * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1507d0c8c0bcSDag-Erling Smørgrav */
150882e5fdc5SEd Maste if (ssh_signal(SIGHUP, SIG_IGN) != SIG_IGN)
150982e5fdc5SEd Maste ssh_signal(SIGHUP, signal_handler);
151082e5fdc5SEd Maste if (ssh_signal(SIGINT, SIG_IGN) != SIG_IGN)
151182e5fdc5SEd Maste ssh_signal(SIGINT, signal_handler);
151282e5fdc5SEd Maste if (ssh_signal(SIGQUIT, SIG_IGN) != SIG_IGN)
151382e5fdc5SEd Maste ssh_signal(SIGQUIT, signal_handler);
151482e5fdc5SEd Maste if (ssh_signal(SIGTERM, SIG_IGN) != SIG_IGN)
151582e5fdc5SEd Maste ssh_signal(SIGTERM, signal_handler);
151682e5fdc5SEd Maste ssh_signal(SIGWINCH, window_change_handler);
1517511b41d2SMark Murray
1518511b41d2SMark Murray if (have_pty)
15191e26c29bSDag-Erling Smørgrav enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1520511b41d2SMark Murray
15211e26c29bSDag-Erling Smørgrav if (session_ident != -1) {
15221e26c29bSDag-Erling Smørgrav if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
152320adc8f2SDag-Erling Smørgrav channel_register_filter(ssh, session_ident,
152455215393SDag-Erling Smørgrav client_simple_escape_filter, NULL,
152555215393SDag-Erling Smørgrav client_filter_cleanup,
15261e26c29bSDag-Erling Smørgrav client_new_escape_filter_ctx(
15271e26c29bSDag-Erling Smørgrav escape_char_arg));
15281e26c29bSDag-Erling Smørgrav }
152920adc8f2SDag-Erling Smørgrav channel_register_cleanup(ssh, session_ident,
1530021d409fSDag-Erling Smørgrav client_channel_closed, 0);
15311e26c29bSDag-Erling Smørgrav }
1532b66f2d16SKris Kennaway
15333bbd8dc9SEd Maste schedule_server_alive_check();
15343bbd8dc9SEd Maste
153538f55691SGordon Tetlow if (sigemptyset(&bsigset) == -1 ||
153638f55691SGordon Tetlow sigaddset(&bsigset, SIGHUP) == -1 ||
153738f55691SGordon Tetlow sigaddset(&bsigset, SIGINT) == -1 ||
153838f55691SGordon Tetlow sigaddset(&bsigset, SIGQUIT) == -1 ||
153938f55691SGordon Tetlow sigaddset(&bsigset, SIGTERM) == -1)
154038f55691SGordon Tetlow error_f("bsigset setup: %s", strerror(errno));
154138f55691SGordon Tetlow
1542511b41d2SMark Murray /* Main loop of the client for the interactive session mode. */
1543511b41d2SMark Murray while (!quit_pending) {
1544b3ced7f2SEd Maste channel_did_enqueue = 0;
1545511b41d2SMark Murray
1546511b41d2SMark Murray /* Process buffered packets sent by the server. */
1547f02e3998SEd Maste client_process_buffered_input_packets(ssh);
1548511b41d2SMark Murray
154920adc8f2SDag-Erling Smørgrav if (session_closed && !channel_still_open(ssh))
1550a04a10f8SKris Kennaway break;
1551a04a10f8SKris Kennaway
155220adc8f2SDag-Erling Smørgrav if (ssh_packet_is_rekeying(ssh)) {
15531e8db6e2SBrian Feldman debug("rekeying in progress");
1554ff4b04e0SDag-Erling Smørgrav } else if (need_rekeying) {
1555ff4b04e0SDag-Erling Smørgrav /* manual rekey request */
1556ff4b04e0SDag-Erling Smørgrav debug("need rekeying");
155720adc8f2SDag-Erling Smørgrav if ((r = kex_start_rekex(ssh)) != 0)
1558206be79aSEd Maste fatal_fr(r, "kex_start_rekex");
1559ff4b04e0SDag-Erling Smørgrav need_rekeying = 0;
15601e8db6e2SBrian Feldman } else {
1561511b41d2SMark Murray /*
15621e8db6e2SBrian Feldman * Make packets from buffered channel data, and
15631e8db6e2SBrian Feldman * enqueue them for sending to the server.
1564511b41d2SMark Murray */
1565f02e3998SEd Maste if (ssh_packet_not_very_much_data_to_write(ssh))
1566b3ced7f2SEd Maste channel_did_enqueue = channel_output_poll(ssh);
1567511b41d2SMark Murray
1568511b41d2SMark Murray /*
15691e8db6e2SBrian Feldman * Check if the window size has changed, and buffer a
15701e8db6e2SBrian Feldman * message about it to the server if so.
1571511b41d2SMark Murray */
157220adc8f2SDag-Erling Smørgrav client_check_window_change(ssh);
15731e8db6e2SBrian Feldman }
1574511b41d2SMark Murray /*
1575511b41d2SMark Murray * Wait until we have something to do (something becomes
1576511b41d2SMark Murray * available on one of the descriptors).
1577511b41d2SMark Murray */
157838f55691SGordon Tetlow if (sigprocmask(SIG_BLOCK, &bsigset, &osigset) == -1)
157938f55691SGordon Tetlow error_f("bsigset sigprocmask: %s", strerror(errno));
158038f55691SGordon Tetlow if (quit_pending)
158138f55691SGordon Tetlow break;
158285d1f2d4SEd Maste client_wait_until_can_do_something(ssh, &pfd, &npfd_alloc,
158338f55691SGordon Tetlow &npfd_active, channel_did_enqueue, &osigset,
158485d1f2d4SEd Maste &conn_in_ready, &conn_out_ready);
1585d565364dSEd Maste if (sigprocmask(SIG_SETMASK, &osigset, NULL) == -1)
158638f55691SGordon Tetlow error_f("osigset sigprocmask: %s", strerror(errno));
1587511b41d2SMark Murray
1588511b41d2SMark Murray if (quit_pending)
1589511b41d2SMark Murray break;
1590511b41d2SMark Murray
159156fae065SEd Maste /* Do channel operations. */
159285d1f2d4SEd Maste channel_after_poll(ssh, pfd, npfd_active);
1593511b41d2SMark Murray
1594a04a10f8SKris Kennaway /* Buffer input from the connection. */
159585d1f2d4SEd Maste if (conn_in_ready)
159685d1f2d4SEd Maste client_process_net_input(ssh);
1597511b41d2SMark Murray
1598a04a10f8SKris Kennaway if (quit_pending)
1599a04a10f8SKris Kennaway break;
1600a04a10f8SKris Kennaway
160166719ee5SEd Maste /* A timeout may have triggered rekeying */
160266719ee5SEd Maste if ((r = ssh_packet_check_rekey(ssh)) != 0)
160366719ee5SEd Maste fatal_fr(r, "cannot start rekeying");
160466719ee5SEd Maste
160555215393SDag-Erling Smørgrav /*
160655215393SDag-Erling Smørgrav * Send as much buffered packet data as possible to the
160755215393SDag-Erling Smørgrav * sender.
160855215393SDag-Erling Smørgrav */
160985d1f2d4SEd Maste if (conn_out_ready) {
161082e5fdc5SEd Maste if ((r = ssh_packet_write_poll(ssh)) != 0) {
161182e5fdc5SEd Maste sshpkt_fatal(ssh, r,
161282e5fdc5SEd Maste "%s: ssh_packet_write_poll", __func__);
161382e5fdc5SEd Maste }
161482e5fdc5SEd Maste }
16153a927e69SDag-Erling Smørgrav
16163a927e69SDag-Erling Smørgrav /*
16173a927e69SDag-Erling Smørgrav * If we are a backgrounded control master, and the
16183a927e69SDag-Erling Smørgrav * timeout has expired without any active client
16193a927e69SDag-Erling Smørgrav * connections, then quit.
16203a927e69SDag-Erling Smørgrav */
16213a927e69SDag-Erling Smørgrav if (control_persist_exit_time > 0) {
16220dddc34cSDag-Erling Smørgrav if (monotime() >= control_persist_exit_time) {
16233a927e69SDag-Erling Smørgrav debug("ControlPersist timeout expired");
16243a927e69SDag-Erling Smørgrav break;
16253a927e69SDag-Erling Smørgrav }
16263a927e69SDag-Erling Smørgrav }
1627511b41d2SMark Murray }
162885d1f2d4SEd Maste free(pfd);
1629511b41d2SMark Murray
1630511b41d2SMark Murray /* Terminate the session. */
1631511b41d2SMark Murray
1632d565364dSEd Maste /*
1633d565364dSEd Maste * In interactive mode (with pseudo tty) display a message indicating
1634d565364dSEd Maste * that the connection has been closed.
1635d565364dSEd Maste */
1636d565364dSEd Maste if (have_pty && options.log_level >= SYSLOG_LEVEL_INFO)
1637d565364dSEd Maste quit_message("Connection to %s closed.", host);
1638d565364dSEd Maste
1639d565364dSEd Maste
1640511b41d2SMark Murray /* Stop watching for window change. */
164182e5fdc5SEd Maste ssh_signal(SIGWINCH, SIG_DFL);
1642511b41d2SMark Murray
1643f02e3998SEd Maste if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
1644f02e3998SEd Maste (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_BY_APPLICATION)) != 0 ||
1645f02e3998SEd Maste (r = sshpkt_put_cstring(ssh, "disconnected by user")) != 0 ||
1646f02e3998SEd Maste (r = sshpkt_put_cstring(ssh, "")) != 0 || /* language tag */
1647f02e3998SEd Maste (r = sshpkt_send(ssh)) != 0 ||
1648f02e3998SEd Maste (r = ssh_packet_write_wait(ssh)) != 0)
1649206be79aSEd Maste fatal_fr(r, "send disconnect");
1650e5e752b5SDag-Erling Smørgrav
165120adc8f2SDag-Erling Smørgrav channel_free_all(ssh);
1652ae1f160dSDag-Erling Smørgrav
1653ae1f160dSDag-Erling Smørgrav if (have_pty)
16541e26c29bSDag-Erling Smørgrav leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1655ae1f160dSDag-Erling Smørgrav
1656efcad6b7SDag-Erling Smørgrav /*
1657efcad6b7SDag-Erling Smørgrav * If there was no shell or command requested, there will be no remote
1658efcad6b7SDag-Erling Smørgrav * exit status to be returned. In that case, clear error code if the
1659efcad6b7SDag-Erling Smørgrav * connection was deliberately terminated at this end.
1660efcad6b7SDag-Erling Smørgrav */
16614f199003SEd Maste if (options.session_type == SESSION_TYPE_NONE &&
16624f199003SEd Maste received_signal == SIGTERM) {
1663efcad6b7SDag-Erling Smørgrav received_signal = 0;
1664efcad6b7SDag-Erling Smørgrav exit_status = 0;
1665ae1f160dSDag-Erling Smørgrav }
1666511b41d2SMark Murray
166720adc8f2SDag-Erling Smørgrav if (received_signal) {
166820adc8f2SDag-Erling Smørgrav verbose("Killed by signal %d.", (int) received_signal);
1669cbaad7c7SEd Maste cleanup_exit(255);
167020adc8f2SDag-Erling Smørgrav }
1671efcad6b7SDag-Erling Smørgrav
1672511b41d2SMark Murray /* Report bytes transferred, and transfer rates. */
1673c8a2bf14SDag-Erling Smørgrav total_time = monotime_double() - start_time;
1674f02e3998SEd Maste ssh_packet_get_bytes(ssh, &ibytes, &obytes);
167555215393SDag-Erling Smørgrav verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
16769f6de2d7SDag-Erling Smørgrav (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
1677511b41d2SMark Murray if (total_time > 0)
167855215393SDag-Erling Smørgrav verbose("Bytes per second: sent %.1f, received %.1f",
167955215393SDag-Erling Smørgrav obytes / total_time, ibytes / total_time);
1680511b41d2SMark Murray /* Return the exit status of the program. */
1681511b41d2SMark Murray debug("Exit status %d", exit_status);
1682511b41d2SMark Murray return exit_status;
1683511b41d2SMark Murray }
1684a04a10f8SKris Kennaway
1685a04a10f8SKris Kennaway /*********/
1686a04a10f8SKris Kennaway
1687ae1f160dSDag-Erling Smørgrav static Channel *
client_request_forwarded_tcpip(struct ssh * ssh,const char * request_type,int rchan,u_int rwindow,u_int rmaxpack)168820adc8f2SDag-Erling Smørgrav client_request_forwarded_tcpip(struct ssh *ssh, const char *request_type,
168920adc8f2SDag-Erling Smørgrav int rchan, u_int rwindow, u_int rmaxpack)
16901e8db6e2SBrian Feldman {
16911e8db6e2SBrian Feldman Channel *c = NULL;
169219ca8551SDag-Erling Smørgrav struct sshbuf *b = NULL;
16931e8db6e2SBrian Feldman char *listen_address, *originator_address;
1694f02e3998SEd Maste u_int listen_port, originator_port;
169519ca8551SDag-Erling Smørgrav int r;
16961e8db6e2SBrian Feldman
16971e8db6e2SBrian Feldman /* Get rest of the packet */
1698f02e3998SEd Maste if ((r = sshpkt_get_cstring(ssh, &listen_address, NULL)) != 0 ||
1699f02e3998SEd Maste (r = sshpkt_get_u32(ssh, &listen_port)) != 0 ||
1700f02e3998SEd Maste (r = sshpkt_get_cstring(ssh, &originator_address, NULL)) != 0 ||
1701f02e3998SEd Maste (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
1702f02e3998SEd Maste (r = sshpkt_get_end(ssh)) != 0)
1703206be79aSEd Maste fatal_fr(r, "parse packet");
17041e8db6e2SBrian Feldman
1705206be79aSEd Maste debug_f("listen %s port %d, originator %s port %d",
1706c0bbca73SDag-Erling Smørgrav listen_address, listen_port, originator_address, originator_port);
17071e8db6e2SBrian Feldman
1708f02e3998SEd Maste if (listen_port > 0xffff)
1709206be79aSEd Maste error_f("invalid listen port");
1710f02e3998SEd Maste else if (originator_port > 0xffff)
1711206be79aSEd Maste error_f("invalid originator port");
1712f02e3998SEd Maste else {
1713f02e3998SEd Maste c = channel_connect_by_listen_address(ssh,
1714f02e3998SEd Maste listen_address, listen_port, "forwarded-tcpip",
1715f02e3998SEd Maste originator_address);
1716f02e3998SEd Maste }
171755215393SDag-Erling Smørgrav
171819ca8551SDag-Erling Smørgrav if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
171919ca8551SDag-Erling Smørgrav if ((b = sshbuf_new()) == NULL) {
1720206be79aSEd Maste error_f("alloc reply");
172119ca8551SDag-Erling Smørgrav goto out;
172219ca8551SDag-Erling Smørgrav }
172319ca8551SDag-Erling Smørgrav /* reconstruct and send to muxclient */
172419ca8551SDag-Erling Smørgrav if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
172519ca8551SDag-Erling Smørgrav (r = sshbuf_put_u8(b, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
172619ca8551SDag-Erling Smørgrav (r = sshbuf_put_cstring(b, request_type)) != 0 ||
172719ca8551SDag-Erling Smørgrav (r = sshbuf_put_u32(b, rchan)) != 0 ||
172819ca8551SDag-Erling Smørgrav (r = sshbuf_put_u32(b, rwindow)) != 0 ||
172919ca8551SDag-Erling Smørgrav (r = sshbuf_put_u32(b, rmaxpack)) != 0 ||
173019ca8551SDag-Erling Smørgrav (r = sshbuf_put_cstring(b, listen_address)) != 0 ||
173119ca8551SDag-Erling Smørgrav (r = sshbuf_put_u32(b, listen_port)) != 0 ||
173219ca8551SDag-Erling Smørgrav (r = sshbuf_put_cstring(b, originator_address)) != 0 ||
173319ca8551SDag-Erling Smørgrav (r = sshbuf_put_u32(b, originator_port)) != 0 ||
173420adc8f2SDag-Erling Smørgrav (r = sshbuf_put_stringb(c->output, b)) != 0) {
1735206be79aSEd Maste error_fr(r, "compose for muxclient");
173619ca8551SDag-Erling Smørgrav goto out;
173719ca8551SDag-Erling Smørgrav }
173819ca8551SDag-Erling Smørgrav }
173919ca8551SDag-Erling Smørgrav
174019ca8551SDag-Erling Smørgrav out:
174119ca8551SDag-Erling Smørgrav sshbuf_free(b);
17420dddc34cSDag-Erling Smørgrav free(originator_address);
17430dddc34cSDag-Erling Smørgrav free(listen_address);
17441e8db6e2SBrian Feldman return c;
17451e8db6e2SBrian Feldman }
17461e8db6e2SBrian Feldman
1747ae1f160dSDag-Erling Smørgrav static Channel *
client_request_forwarded_streamlocal(struct ssh * ssh,const char * request_type,int rchan)174820adc8f2SDag-Erling Smørgrav client_request_forwarded_streamlocal(struct ssh *ssh,
174920adc8f2SDag-Erling Smørgrav const char *request_type, int rchan)
1750c0bbca73SDag-Erling Smørgrav {
1751c0bbca73SDag-Erling Smørgrav Channel *c = NULL;
1752c0bbca73SDag-Erling Smørgrav char *listen_path;
1753f02e3998SEd Maste int r;
1754c0bbca73SDag-Erling Smørgrav
1755c0bbca73SDag-Erling Smørgrav /* Get the remote path. */
1756f02e3998SEd Maste if ((r = sshpkt_get_cstring(ssh, &listen_path, NULL)) != 0 ||
1757f02e3998SEd Maste (r = sshpkt_get_string(ssh, NULL, NULL)) != 0 || /* reserved */
1758f02e3998SEd Maste (r = sshpkt_get_end(ssh)) != 0)
1759206be79aSEd Maste fatal_fr(r, "parse packet");
1760c0bbca73SDag-Erling Smørgrav
1761206be79aSEd Maste debug_f("request: %s", listen_path);
1762c0bbca73SDag-Erling Smørgrav
176320adc8f2SDag-Erling Smørgrav c = channel_connect_by_listen_path(ssh, listen_path,
1764c0bbca73SDag-Erling Smørgrav "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1765c0bbca73SDag-Erling Smørgrav free(listen_path);
1766c0bbca73SDag-Erling Smørgrav return c;
1767c0bbca73SDag-Erling Smørgrav }
1768c0bbca73SDag-Erling Smørgrav
1769c0bbca73SDag-Erling Smørgrav static Channel *
client_request_x11(struct ssh * ssh,const char * request_type,int rchan)177020adc8f2SDag-Erling Smørgrav client_request_x11(struct ssh *ssh, const char *request_type, int rchan)
17711e8db6e2SBrian Feldman {
17721e8db6e2SBrian Feldman Channel *c = NULL;
17731e8db6e2SBrian Feldman char *originator;
1774f02e3998SEd Maste u_int originator_port;
1775f02e3998SEd Maste int r, sock;
17761e8db6e2SBrian Feldman
17771e8db6e2SBrian Feldman if (!options.forward_x11) {
17781e8db6e2SBrian Feldman error("Warning: ssh server tried X11 forwarding.");
177955215393SDag-Erling Smørgrav error("Warning: this is probably a break-in attempt by a "
178055215393SDag-Erling Smørgrav "malicious server.");
17811e8db6e2SBrian Feldman return NULL;
17821e8db6e2SBrian Feldman }
178336b354d7SEd Maste if (x11_refuse_time != 0 && monotime() >= x11_refuse_time) {
17843a927e69SDag-Erling Smørgrav verbose("Rejected X11 connection after ForwardX11Timeout "
17853a927e69SDag-Erling Smørgrav "expired");
17863a927e69SDag-Erling Smørgrav return NULL;
17873a927e69SDag-Erling Smørgrav }
1788f02e3998SEd Maste if ((r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 ||
1789f02e3998SEd Maste (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
1790f02e3998SEd Maste (r = sshpkt_get_end(ssh)) != 0)
1791206be79aSEd Maste fatal_fr(r, "parse packet");
17921e8db6e2SBrian Feldman /* XXX check permission */
1793f02e3998SEd Maste /* XXX range check originator port? */
1794f02e3998SEd Maste debug("client_request_x11: request from %s %u", originator,
17951e8db6e2SBrian Feldman originator_port);
17960dddc34cSDag-Erling Smørgrav free(originator);
179720adc8f2SDag-Erling Smørgrav sock = x11_connect_display(ssh);
1798ae1f160dSDag-Erling Smørgrav if (sock < 0)
1799ae1f160dSDag-Erling Smørgrav return NULL;
180038f55691SGordon Tetlow c = channel_new(ssh, "x11-connection",
18011e8db6e2SBrian Feldman SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1802d95e11bfSDag-Erling Smørgrav CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
1803ae1f160dSDag-Erling Smørgrav c->force_drain = 1;
18041e8db6e2SBrian Feldman return c;
18051e8db6e2SBrian Feldman }
18061e8db6e2SBrian Feldman
1807ae1f160dSDag-Erling Smørgrav static Channel *
client_request_agent(struct ssh * ssh,const char * request_type,int rchan)180820adc8f2SDag-Erling Smørgrav client_request_agent(struct ssh *ssh, const char *request_type, int rchan)
18091e8db6e2SBrian Feldman {
18101e8db6e2SBrian Feldman Channel *c = NULL;
1811c1e08615SDag-Erling Smørgrav int r, sock;
18121e8db6e2SBrian Feldman
18131e8db6e2SBrian Feldman if (!options.forward_agent) {
18141e8db6e2SBrian Feldman error("Warning: ssh server tried agent forwarding.");
181555215393SDag-Erling Smørgrav error("Warning: this is probably a break-in attempt by a "
181655215393SDag-Erling Smørgrav "malicious server.");
18171e8db6e2SBrian Feldman return NULL;
18181e8db6e2SBrian Feldman }
181982e5fdc5SEd Maste if (forward_agent_sock_path == NULL) {
182082e5fdc5SEd Maste r = ssh_get_authentication_socket(&sock);
182182e5fdc5SEd Maste } else {
182282e5fdc5SEd Maste r = ssh_get_authentication_socket_path(forward_agent_sock_path, &sock);
182382e5fdc5SEd Maste }
182482e5fdc5SEd Maste if (r != 0) {
1825c1e08615SDag-Erling Smørgrav if (r != SSH_ERR_AGENT_NOT_PRESENT)
1826206be79aSEd Maste debug_fr(r, "ssh_get_authentication_socket");
1827ae1f160dSDag-Erling Smørgrav return NULL;
1828c1e08615SDag-Erling Smørgrav }
182985d1f2d4SEd Maste if ((r = ssh_agent_bind_hostkey(sock, ssh->kex->initial_hostkey,
183085d1f2d4SEd Maste ssh->kex->session_id, ssh->kex->initial_sig, 1)) == 0)
183185d1f2d4SEd Maste debug_f("bound agent to hostkey");
183285d1f2d4SEd Maste else
183385d1f2d4SEd Maste debug2_fr(r, "ssh_agent_bind_hostkey");
183485d1f2d4SEd Maste
183538f55691SGordon Tetlow c = channel_new(ssh, "agent-connection",
18361e8db6e2SBrian Feldman SSH_CHANNEL_OPEN, sock, sock, -1,
1837ad22e48fSDag-Erling Smørgrav CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
1838d95e11bfSDag-Erling Smørgrav "authentication agent connection", 1);
1839ae1f160dSDag-Erling Smørgrav c->force_drain = 1;
18401e8db6e2SBrian Feldman return c;
18411e8db6e2SBrian Feldman }
18421e8db6e2SBrian Feldman
1843c8a2bf14SDag-Erling Smørgrav char *
client_request_tun_fwd(struct ssh * ssh,int tun_mode,int local_tun,int remote_tun,channel_open_fn * cb,void * cbctx)184420adc8f2SDag-Erling Smørgrav client_request_tun_fwd(struct ssh *ssh, int tun_mode,
1845db903103SEd Maste int local_tun, int remote_tun, channel_open_fn *cb, void *cbctx)
1846490bfaadSDag-Erling Smørgrav {
1847490bfaadSDag-Erling Smørgrav Channel *c;
1848f02e3998SEd Maste int r, fd;
1849c8a2bf14SDag-Erling Smørgrav char *ifname = NULL;
1850490bfaadSDag-Erling Smørgrav
1851490bfaadSDag-Erling Smørgrav if (tun_mode == SSH_TUNMODE_NO)
1852490bfaadSDag-Erling Smørgrav return 0;
1853490bfaadSDag-Erling Smørgrav
1854490bfaadSDag-Erling Smørgrav debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1855490bfaadSDag-Erling Smørgrav
1856490bfaadSDag-Erling Smørgrav /* Open local tunnel device */
1857c8a2bf14SDag-Erling Smørgrav if ((fd = tun_open(local_tun, tun_mode, &ifname)) == -1) {
1858490bfaadSDag-Erling Smørgrav error("Tunnel device open failed.");
1859c8a2bf14SDag-Erling Smørgrav return NULL;
1860490bfaadSDag-Erling Smørgrav }
1861c8a2bf14SDag-Erling Smørgrav debug("Tunnel forwarding using interface %s", ifname);
1862490bfaadSDag-Erling Smørgrav
186338f55691SGordon Tetlow c = channel_new(ssh, "tun-connection", SSH_CHANNEL_OPENING, fd, fd, -1,
1864490bfaadSDag-Erling Smørgrav CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1865490bfaadSDag-Erling Smørgrav c->datagram = 1;
1866490bfaadSDag-Erling Smørgrav
1867490bfaadSDag-Erling Smørgrav #if defined(SSH_TUN_FILTER)
1868490bfaadSDag-Erling Smørgrav if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
186920adc8f2SDag-Erling Smørgrav channel_register_filter(ssh, c->self, sys_tun_infilter,
187055215393SDag-Erling Smørgrav sys_tun_outfilter, NULL, NULL);
1871490bfaadSDag-Erling Smørgrav #endif
1872490bfaadSDag-Erling Smørgrav
1873db903103SEd Maste if (cb != NULL)
1874db903103SEd Maste channel_register_open_confirm(ssh, c->self, cb, cbctx);
1875db903103SEd Maste
1876f02e3998SEd Maste if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1877f02e3998SEd Maste (r = sshpkt_put_cstring(ssh, "tun@openssh.com")) != 0 ||
1878f02e3998SEd Maste (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1879f02e3998SEd Maste (r = sshpkt_put_u32(ssh, c->local_window_max)) != 0 ||
1880f02e3998SEd Maste (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
1881f02e3998SEd Maste (r = sshpkt_put_u32(ssh, tun_mode)) != 0 ||
1882f02e3998SEd Maste (r = sshpkt_put_u32(ssh, remote_tun)) != 0 ||
1883f02e3998SEd Maste (r = sshpkt_send(ssh)) != 0)
1884f02e3998SEd Maste sshpkt_fatal(ssh, r, "%s: send reply", __func__);
1885490bfaadSDag-Erling Smørgrav
1886c8a2bf14SDag-Erling Smørgrav return ifname;
1887490bfaadSDag-Erling Smørgrav }
1888490bfaadSDag-Erling Smørgrav
1889a04a10f8SKris Kennaway /* XXXX move to generic input handler */
1890c1e08615SDag-Erling Smørgrav static int
client_input_channel_open(int type,u_int32_t seq,struct ssh * ssh)189120adc8f2SDag-Erling Smørgrav client_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
1892a04a10f8SKris Kennaway {
1893a04a10f8SKris Kennaway Channel *c = NULL;
1894f02e3998SEd Maste char *ctype = NULL;
1895f02e3998SEd Maste int r;
1896f02e3998SEd Maste u_int rchan;
1897f02e3998SEd Maste size_t len;
1898f02e3998SEd Maste u_int rmaxpack, rwindow;
1899a04a10f8SKris Kennaway
1900f02e3998SEd Maste if ((r = sshpkt_get_cstring(ssh, &ctype, &len)) != 0 ||
1901f02e3998SEd Maste (r = sshpkt_get_u32(ssh, &rchan)) != 0 ||
1902f02e3998SEd Maste (r = sshpkt_get_u32(ssh, &rwindow)) != 0 ||
1903f02e3998SEd Maste (r = sshpkt_get_u32(ssh, &rmaxpack)) != 0)
1904f02e3998SEd Maste goto out;
1905a04a10f8SKris Kennaway
1906a04a10f8SKris Kennaway debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
1907a04a10f8SKris Kennaway ctype, rchan, rwindow, rmaxpack);
1908a04a10f8SKris Kennaway
19091e8db6e2SBrian Feldman if (strcmp(ctype, "forwarded-tcpip") == 0) {
191020adc8f2SDag-Erling Smørgrav c = client_request_forwarded_tcpip(ssh, ctype, rchan, rwindow,
191119ca8551SDag-Erling Smørgrav rmaxpack);
1912c0bbca73SDag-Erling Smørgrav } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
191320adc8f2SDag-Erling Smørgrav c = client_request_forwarded_streamlocal(ssh, ctype, rchan);
19141e8db6e2SBrian Feldman } else if (strcmp(ctype, "x11") == 0) {
191520adc8f2SDag-Erling Smørgrav c = client_request_x11(ssh, ctype, rchan);
19161e8db6e2SBrian Feldman } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
191720adc8f2SDag-Erling Smørgrav c = client_request_agent(ssh, ctype, rchan);
1918a04a10f8SKris Kennaway }
191919ca8551SDag-Erling Smørgrav if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
192019ca8551SDag-Erling Smørgrav debug3("proxied to downstream: %s", ctype);
192119ca8551SDag-Erling Smørgrav } else if (c != NULL) {
1922a04a10f8SKris Kennaway debug("confirm %s", ctype);
1923a04a10f8SKris Kennaway c->remote_id = rchan;
192420adc8f2SDag-Erling Smørgrav c->have_remote_id = 1;
1925a04a10f8SKris Kennaway c->remote_window = rwindow;
1926a04a10f8SKris Kennaway c->remote_maxpacket = rmaxpack;
1927ae1f160dSDag-Erling Smørgrav if (c->type != SSH_CHANNEL_CONNECTING) {
1928f02e3998SEd Maste if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
1929f02e3998SEd Maste (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1930f02e3998SEd Maste (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1931f02e3998SEd Maste (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1932f02e3998SEd Maste (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
1933f02e3998SEd Maste (r = sshpkt_send(ssh)) != 0)
1934f02e3998SEd Maste sshpkt_fatal(ssh, r, "%s: send reply", __func__);
1935ae1f160dSDag-Erling Smørgrav }
1936a04a10f8SKris Kennaway } else {
1937a04a10f8SKris Kennaway debug("failure %s", ctype);
1938f02e3998SEd Maste if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
1939f02e3998SEd Maste (r = sshpkt_put_u32(ssh, rchan)) != 0 ||
1940f02e3998SEd Maste (r = sshpkt_put_u32(ssh, SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED)) != 0 ||
1941f02e3998SEd Maste (r = sshpkt_put_cstring(ssh, "open failed")) != 0 ||
1942f02e3998SEd Maste (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1943f02e3998SEd Maste (r = sshpkt_send(ssh)) != 0)
1944f02e3998SEd Maste sshpkt_fatal(ssh, r, "%s: send failure", __func__);
1945a04a10f8SKris Kennaway }
1946f02e3998SEd Maste r = 0;
1947f02e3998SEd Maste out:
19480dddc34cSDag-Erling Smørgrav free(ctype);
1949f02e3998SEd Maste return r;
1950a04a10f8SKris Kennaway }
1951c1e08615SDag-Erling Smørgrav
1952c1e08615SDag-Erling Smørgrav static int
client_input_channel_req(int type,u_int32_t seq,struct ssh * ssh)195320adc8f2SDag-Erling Smørgrav client_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
19541e8db6e2SBrian Feldman {
19551e8db6e2SBrian Feldman Channel *c = NULL;
1956f02e3998SEd Maste char *rtype = NULL;
1957f02e3998SEd Maste u_char reply;
1958f02e3998SEd Maste u_int id, exitval;
1959f02e3998SEd Maste int r, success = 0;
19601e8db6e2SBrian Feldman
1961f02e3998SEd Maste if ((r = sshpkt_get_u32(ssh, &id)) != 0)
1962f02e3998SEd Maste return r;
1963f02e3998SEd Maste if (id <= INT_MAX)
196420adc8f2SDag-Erling Smørgrav c = channel_lookup(ssh, id);
196520adc8f2SDag-Erling Smørgrav if (channel_proxy_upstream(c, type, seq, ssh))
196619ca8551SDag-Erling Smørgrav return 0;
1967f02e3998SEd Maste if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
1968f02e3998SEd Maste (r = sshpkt_get_u8(ssh, &reply)) != 0)
1969f02e3998SEd Maste goto out;
19701e8db6e2SBrian Feldman
1971f02e3998SEd Maste debug("client_input_channel_req: channel %u rtype %s reply %d",
19721e8db6e2SBrian Feldman id, rtype, reply);
19731e8db6e2SBrian Feldman
1974f02e3998SEd Maste if (c == NULL) {
197555215393SDag-Erling Smørgrav error("client_input_channel_req: channel %d: "
197655215393SDag-Erling Smørgrav "unknown channel", id);
197755215393SDag-Erling Smørgrav } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1978f02e3998SEd Maste if ((r = sshpkt_get_end(ssh)) != 0)
1979f02e3998SEd Maste goto out;
198020adc8f2SDag-Erling Smørgrav chan_rcvd_eow(ssh, c);
19811e8db6e2SBrian Feldman } else if (strcmp(rtype, "exit-status") == 0) {
1982f02e3998SEd Maste if ((r = sshpkt_get_u32(ssh, &exitval)) != 0)
1983f02e3998SEd Maste goto out;
19846d4f2dd1SDag-Erling Smørgrav if (c->ctl_chan != -1) {
198520adc8f2SDag-Erling Smørgrav mux_exit_message(ssh, c, exitval);
19866d4f2dd1SDag-Erling Smørgrav success = 1;
1987f02e3998SEd Maste } else if ((int)id == session_ident) {
19886d4f2dd1SDag-Erling Smørgrav /* Record exit value of local session */
19891e8db6e2SBrian Feldman success = 1;
1990d74d50a8SDag-Erling Smørgrav exit_status = exitval;
1991d74d50a8SDag-Erling Smørgrav } else {
19926d4f2dd1SDag-Erling Smørgrav /* Probably for a mux channel that has already closed */
1993206be79aSEd Maste debug_f("no sink for exit-status on channel %d",
1994206be79aSEd Maste id);
1995d74d50a8SDag-Erling Smørgrav }
1996f02e3998SEd Maste if ((r = sshpkt_get_end(ssh)) != 0)
1997f02e3998SEd Maste goto out;
19981e8db6e2SBrian Feldman }
1999c0bbca73SDag-Erling Smørgrav if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
200020adc8f2SDag-Erling Smørgrav if (!c->have_remote_id)
2001206be79aSEd Maste fatal_f("channel %d: no remote_id", c->self);
2002f02e3998SEd Maste if ((r = sshpkt_start(ssh, success ?
2003f02e3998SEd Maste SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE)) != 0 ||
2004f02e3998SEd Maste (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2005f02e3998SEd Maste (r = sshpkt_send(ssh)) != 0)
2006f02e3998SEd Maste sshpkt_fatal(ssh, r, "%s: send failure", __func__);
20071e8db6e2SBrian Feldman }
2008f02e3998SEd Maste r = 0;
2009f02e3998SEd Maste out:
20100dddc34cSDag-Erling Smørgrav free(rtype);
2011f02e3998SEd Maste return r;
20121e8db6e2SBrian Feldman }
2013c1e08615SDag-Erling Smørgrav
2014c1e08615SDag-Erling Smørgrav struct hostkeys_update_ctx {
2015c1e08615SDag-Erling Smørgrav /* The hostname and (optionally) IP address string for the server */
2016c1e08615SDag-Erling Smørgrav char *host_str, *ip_str;
2017c1e08615SDag-Erling Smørgrav
2018c1e08615SDag-Erling Smørgrav /*
2019c1e08615SDag-Erling Smørgrav * Keys received from the server and a flag for each indicating
2020c1e08615SDag-Erling Smørgrav * whether they already exist in known_hosts.
2021206be79aSEd Maste * keys_match is filled in by hostkeys_find() and later (for new
20227ee81174SEd Maste * keys) by client_global_hostkeys_prove_confirm().
2023c1e08615SDag-Erling Smørgrav */
2024c1e08615SDag-Erling Smørgrav struct sshkey **keys;
2025206be79aSEd Maste u_int *keys_match; /* mask of HKF_MATCH_* from hostfile.h */
2026206be79aSEd Maste int *keys_verified; /* flag for new keys verified by server */
2027206be79aSEd Maste size_t nkeys, nnew, nincomplete; /* total, new keys, incomplete match */
2028c1e08615SDag-Erling Smørgrav
2029c1e08615SDag-Erling Smørgrav /*
2030c1e08615SDag-Erling Smørgrav * Keys that are in known_hosts, but were not present in the update
2031c1e08615SDag-Erling Smørgrav * from the server (i.e. scheduled to be deleted).
2032c1e08615SDag-Erling Smørgrav * Filled in by hostkeys_find().
2033c1e08615SDag-Erling Smørgrav */
2034c1e08615SDag-Erling Smørgrav struct sshkey **old_keys;
2035c1e08615SDag-Erling Smørgrav size_t nold;
2036206be79aSEd Maste
2037206be79aSEd Maste /* Various special cases. */
2038206be79aSEd Maste int complex_hostspec; /* wildcard or manual pattern-list host name */
2039206be79aSEd Maste int ca_available; /* saw CA key for this host */
2040206be79aSEd Maste int old_key_seen; /* saw old key with other name/addr */
2041206be79aSEd Maste int other_name_seen; /* saw key with other name/addr */
2042c1e08615SDag-Erling Smørgrav };
2043c1e08615SDag-Erling Smørgrav
2044ae1f160dSDag-Erling Smørgrav static void
hostkeys_update_ctx_free(struct hostkeys_update_ctx * ctx)2045c1e08615SDag-Erling Smørgrav hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
2046c1e08615SDag-Erling Smørgrav {
2047c1e08615SDag-Erling Smørgrav size_t i;
2048c1e08615SDag-Erling Smørgrav
2049c1e08615SDag-Erling Smørgrav if (ctx == NULL)
2050c1e08615SDag-Erling Smørgrav return;
2051c1e08615SDag-Erling Smørgrav for (i = 0; i < ctx->nkeys; i++)
2052c1e08615SDag-Erling Smørgrav sshkey_free(ctx->keys[i]);
2053c1e08615SDag-Erling Smørgrav free(ctx->keys);
2054206be79aSEd Maste free(ctx->keys_match);
2055206be79aSEd Maste free(ctx->keys_verified);
2056c1e08615SDag-Erling Smørgrav for (i = 0; i < ctx->nold; i++)
2057c1e08615SDag-Erling Smørgrav sshkey_free(ctx->old_keys[i]);
2058c1e08615SDag-Erling Smørgrav free(ctx->old_keys);
2059c1e08615SDag-Erling Smørgrav free(ctx->host_str);
2060c1e08615SDag-Erling Smørgrav free(ctx->ip_str);
2061c1e08615SDag-Erling Smørgrav free(ctx);
2062c1e08615SDag-Erling Smørgrav }
2063c1e08615SDag-Erling Smørgrav
2064206be79aSEd Maste /*
2065206be79aSEd Maste * Returns non-zero if a known_hosts hostname list is not of a form that
2066206be79aSEd Maste * can be handled by UpdateHostkeys. These include wildcard hostnames and
2067206be79aSEd Maste * hostnames lists that do not follow the form host[,ip].
2068206be79aSEd Maste */
2069206be79aSEd Maste static int
hostspec_is_complex(const char * hosts)2070206be79aSEd Maste hostspec_is_complex(const char *hosts)
2071206be79aSEd Maste {
2072206be79aSEd Maste char *cp;
2073206be79aSEd Maste
2074206be79aSEd Maste /* wildcard */
2075206be79aSEd Maste if (strchr(hosts, '*') != NULL || strchr(hosts, '?') != NULL)
2076206be79aSEd Maste return 1;
2077206be79aSEd Maste /* single host/ip = ok */
2078206be79aSEd Maste if ((cp = strchr(hosts, ',')) == NULL)
2079206be79aSEd Maste return 0;
2080206be79aSEd Maste /* more than two entries on the line */
2081206be79aSEd Maste if (strchr(cp + 1, ',') != NULL)
2082206be79aSEd Maste return 1;
2083206be79aSEd Maste /* XXX maybe parse cp+1 and ensure it is an IP? */
2084206be79aSEd Maste return 0;
2085206be79aSEd Maste }
2086206be79aSEd Maste
2087206be79aSEd Maste /* callback to search for ctx->keys in known_hosts */
2088c1e08615SDag-Erling Smørgrav static int
hostkeys_find(struct hostkey_foreach_line * l,void * _ctx)2089c1e08615SDag-Erling Smørgrav hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
2090c1e08615SDag-Erling Smørgrav {
2091c1e08615SDag-Erling Smørgrav struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
2092c1e08615SDag-Erling Smørgrav size_t i;
2093c1e08615SDag-Erling Smørgrav struct sshkey **tmp;
2094c1e08615SDag-Erling Smørgrav
2095206be79aSEd Maste if (l->key == NULL)
2096c1e08615SDag-Erling Smørgrav return 0;
2097206be79aSEd Maste if (l->status != HKF_STATUS_MATCHED) {
2098206be79aSEd Maste /* Record if one of the keys appears on a non-matching line */
2099206be79aSEd Maste for (i = 0; i < ctx->nkeys; i++) {
2100206be79aSEd Maste if (sshkey_equal(l->key, ctx->keys[i])) {
2101206be79aSEd Maste ctx->other_name_seen = 1;
2102206be79aSEd Maste debug3_f("found %s key under different "
2103206be79aSEd Maste "name/addr at %s:%ld",
2104206be79aSEd Maste sshkey_ssh_name(ctx->keys[i]),
2105206be79aSEd Maste l->path, l->linenum);
2106206be79aSEd Maste return 0;
2107206be79aSEd Maste }
2108206be79aSEd Maste }
2109206be79aSEd Maste return 0;
2110206be79aSEd Maste }
2111206be79aSEd Maste /* Don't proceed if revocation or CA markers are present */
2112206be79aSEd Maste /* XXX relax this */
2113206be79aSEd Maste if (l->marker != MRK_NONE) {
2114206be79aSEd Maste debug3_f("hostkeys file %s:%ld has CA/revocation marker",
2115206be79aSEd Maste l->path, l->linenum);
2116206be79aSEd Maste ctx->complex_hostspec = 1;
2117206be79aSEd Maste return 0;
2118206be79aSEd Maste }
2119206be79aSEd Maste
2120206be79aSEd Maste /* If CheckHostIP is enabled, then check for mismatched hostname/addr */
2121206be79aSEd Maste if (ctx->ip_str != NULL && strchr(l->hosts, ',') != NULL) {
2122206be79aSEd Maste if ((l->match & HKF_MATCH_HOST) == 0) {
2123206be79aSEd Maste /* Record if address matched a different hostname. */
2124206be79aSEd Maste ctx->other_name_seen = 1;
2125206be79aSEd Maste debug3_f("found address %s against different hostname "
2126206be79aSEd Maste "at %s:%ld", ctx->ip_str, l->path, l->linenum);
2127206be79aSEd Maste return 0;
2128206be79aSEd Maste } else if ((l->match & HKF_MATCH_IP) == 0) {
2129206be79aSEd Maste /* Record if hostname matched a different address. */
2130206be79aSEd Maste ctx->other_name_seen = 1;
2131206be79aSEd Maste debug3_f("found hostname %s against different address "
2132206be79aSEd Maste "at %s:%ld", ctx->host_str, l->path, l->linenum);
2133206be79aSEd Maste }
2134206be79aSEd Maste }
2135206be79aSEd Maste
2136206be79aSEd Maste /*
2137206be79aSEd Maste * UpdateHostkeys is skipped for wildcard host names and hostnames
2138206be79aSEd Maste * that contain more than two entries (ssh never writes these).
2139206be79aSEd Maste */
2140206be79aSEd Maste if (hostspec_is_complex(l->hosts)) {
2141206be79aSEd Maste debug3_f("hostkeys file %s:%ld complex host specification",
2142206be79aSEd Maste l->path, l->linenum);
2143206be79aSEd Maste ctx->complex_hostspec = 1;
2144206be79aSEd Maste return 0;
2145206be79aSEd Maste }
2146c1e08615SDag-Erling Smørgrav
2147c1e08615SDag-Erling Smørgrav /* Mark off keys we've already seen for this host */
2148c1e08615SDag-Erling Smørgrav for (i = 0; i < ctx->nkeys; i++) {
2149206be79aSEd Maste if (!sshkey_equal(l->key, ctx->keys[i]))
2150206be79aSEd Maste continue;
2151206be79aSEd Maste debug3_f("found %s key at %s:%ld",
2152c1e08615SDag-Erling Smørgrav sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
2153206be79aSEd Maste ctx->keys_match[i] |= l->match;
2154c1e08615SDag-Erling Smørgrav return 0;
2155c1e08615SDag-Erling Smørgrav }
2156c1e08615SDag-Erling Smørgrav /* This line contained a key that not offered by the server */
2157206be79aSEd Maste debug3_f("deprecated %s key at %s:%ld", sshkey_ssh_name(l->key),
2158206be79aSEd Maste l->path, l->linenum);
215920adc8f2SDag-Erling Smørgrav if ((tmp = recallocarray(ctx->old_keys, ctx->nold, ctx->nold + 1,
2160c1e08615SDag-Erling Smørgrav sizeof(*ctx->old_keys))) == NULL)
2161206be79aSEd Maste fatal_f("recallocarray failed nold = %zu", ctx->nold);
2162c1e08615SDag-Erling Smørgrav ctx->old_keys = tmp;
2163c1e08615SDag-Erling Smørgrav ctx->old_keys[ctx->nold++] = l->key;
2164c1e08615SDag-Erling Smørgrav l->key = NULL;
2165c1e08615SDag-Erling Smørgrav
2166c1e08615SDag-Erling Smørgrav return 0;
2167c1e08615SDag-Erling Smørgrav }
2168c1e08615SDag-Erling Smørgrav
2169206be79aSEd Maste /* callback to search for ctx->old_keys in known_hosts under other names */
2170206be79aSEd Maste static int
hostkeys_check_old(struct hostkey_foreach_line * l,void * _ctx)2171206be79aSEd Maste hostkeys_check_old(struct hostkey_foreach_line *l, void *_ctx)
2172206be79aSEd Maste {
2173206be79aSEd Maste struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
2174206be79aSEd Maste size_t i;
2175206be79aSEd Maste int hashed;
2176206be79aSEd Maste
2177206be79aSEd Maste /* only care about lines that *don't* match the active host spec */
2178206be79aSEd Maste if (l->status == HKF_STATUS_MATCHED || l->key == NULL)
2179206be79aSEd Maste return 0;
2180206be79aSEd Maste
2181206be79aSEd Maste hashed = l->match & (HKF_MATCH_HOST_HASHED|HKF_MATCH_IP_HASHED);
2182206be79aSEd Maste for (i = 0; i < ctx->nold; i++) {
2183206be79aSEd Maste if (!sshkey_equal(l->key, ctx->old_keys[i]))
2184206be79aSEd Maste continue;
2185206be79aSEd Maste debug3_f("found deprecated %s key at %s:%ld as %s",
218666719ee5SEd Maste sshkey_ssh_name(ctx->old_keys[i]), l->path, l->linenum,
2187206be79aSEd Maste hashed ? "[HASHED]" : l->hosts);
2188206be79aSEd Maste ctx->old_key_seen = 1;
2189206be79aSEd Maste break;
2190206be79aSEd Maste }
2191206be79aSEd Maste return 0;
2192206be79aSEd Maste }
2193206be79aSEd Maste
2194206be79aSEd Maste /*
2195206be79aSEd Maste * Check known_hosts files for deprecated keys under other names. Returns 0
2196206be79aSEd Maste * on success or -1 on failure. Updates ctx->old_key_seen if deprecated keys
2197206be79aSEd Maste * exist under names other than the active hostname/IP.
2198206be79aSEd Maste */
2199206be79aSEd Maste static int
check_old_keys_othernames(struct hostkeys_update_ctx * ctx)2200206be79aSEd Maste check_old_keys_othernames(struct hostkeys_update_ctx *ctx)
2201206be79aSEd Maste {
2202206be79aSEd Maste size_t i;
2203206be79aSEd Maste int r;
2204206be79aSEd Maste
2205206be79aSEd Maste debug2_f("checking for %zu deprecated keys", ctx->nold);
2206206be79aSEd Maste for (i = 0; i < options.num_user_hostfiles; i++) {
2207206be79aSEd Maste debug3_f("searching %s for %s / %s",
2208206be79aSEd Maste options.user_hostfiles[i], ctx->host_str,
2209206be79aSEd Maste ctx->ip_str ? ctx->ip_str : "(none)");
2210206be79aSEd Maste if ((r = hostkeys_foreach(options.user_hostfiles[i],
2211206be79aSEd Maste hostkeys_check_old, ctx, ctx->host_str, ctx->ip_str,
2212206be79aSEd Maste HKF_WANT_PARSE_KEY, 0)) != 0) {
2213206be79aSEd Maste if (r == SSH_ERR_SYSTEM_ERROR && errno == ENOENT) {
2214206be79aSEd Maste debug_f("hostkeys file %s does not exist",
2215206be79aSEd Maste options.user_hostfiles[i]);
2216206be79aSEd Maste continue;
2217206be79aSEd Maste }
2218206be79aSEd Maste error_fr(r, "hostkeys_foreach failed for %s",
2219206be79aSEd Maste options.user_hostfiles[i]);
2220206be79aSEd Maste return -1;
2221206be79aSEd Maste }
2222206be79aSEd Maste }
2223206be79aSEd Maste return 0;
2224206be79aSEd Maste }
2225206be79aSEd Maste
2226c1e08615SDag-Erling Smørgrav static void
hostkey_change_preamble(LogLevel loglevel)222782e5fdc5SEd Maste hostkey_change_preamble(LogLevel loglevel)
222882e5fdc5SEd Maste {
222982e5fdc5SEd Maste do_log2(loglevel, "The server has updated its host keys.");
223082e5fdc5SEd Maste do_log2(loglevel, "These changes were verified by the server's "
223182e5fdc5SEd Maste "existing trusted key.");
223282e5fdc5SEd Maste }
223382e5fdc5SEd Maste
223482e5fdc5SEd Maste static void
update_known_hosts(struct hostkeys_update_ctx * ctx)2235c1e08615SDag-Erling Smørgrav update_known_hosts(struct hostkeys_update_ctx *ctx)
2236c1e08615SDag-Erling Smørgrav {
223782e5fdc5SEd Maste int r, was_raw = 0, first = 1;
223882e5fdc5SEd Maste int asking = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK;
223982e5fdc5SEd Maste LogLevel loglevel = asking ? SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
2240c1e08615SDag-Erling Smørgrav char *fp, *response;
2241c1e08615SDag-Erling Smørgrav size_t i;
224282e5fdc5SEd Maste struct stat sb;
2243c1e08615SDag-Erling Smørgrav
2244c1e08615SDag-Erling Smørgrav for (i = 0; i < ctx->nkeys; i++) {
2245206be79aSEd Maste if (!ctx->keys_verified[i])
2246c1e08615SDag-Erling Smørgrav continue;
2247c1e08615SDag-Erling Smørgrav if ((fp = sshkey_fingerprint(ctx->keys[i],
2248c1e08615SDag-Erling Smørgrav options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
2249206be79aSEd Maste fatal_f("sshkey_fingerprint failed");
225082e5fdc5SEd Maste if (first && asking)
225182e5fdc5SEd Maste hostkey_change_preamble(loglevel);
2252c1e08615SDag-Erling Smørgrav do_log2(loglevel, "Learned new hostkey: %s %s",
2253c1e08615SDag-Erling Smørgrav sshkey_type(ctx->keys[i]), fp);
225482e5fdc5SEd Maste first = 0;
2255c1e08615SDag-Erling Smørgrav free(fp);
2256c1e08615SDag-Erling Smørgrav }
2257c1e08615SDag-Erling Smørgrav for (i = 0; i < ctx->nold; i++) {
2258c1e08615SDag-Erling Smørgrav if ((fp = sshkey_fingerprint(ctx->old_keys[i],
2259c1e08615SDag-Erling Smørgrav options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
2260206be79aSEd Maste fatal_f("sshkey_fingerprint failed");
226182e5fdc5SEd Maste if (first && asking)
226282e5fdc5SEd Maste hostkey_change_preamble(loglevel);
2263c1e08615SDag-Erling Smørgrav do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
2264c1e08615SDag-Erling Smørgrav sshkey_type(ctx->old_keys[i]), fp);
226582e5fdc5SEd Maste first = 0;
2266c1e08615SDag-Erling Smørgrav free(fp);
2267c1e08615SDag-Erling Smørgrav }
2268c1e08615SDag-Erling Smørgrav if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
2269c1e08615SDag-Erling Smørgrav if (get_saved_tio() != NULL) {
2270c1e08615SDag-Erling Smørgrav leave_raw_mode(1);
2271c1e08615SDag-Erling Smørgrav was_raw = 1;
2272c1e08615SDag-Erling Smørgrav }
2273c1e08615SDag-Erling Smørgrav response = NULL;
2274c1e08615SDag-Erling Smørgrav for (i = 0; !quit_pending && i < 3; i++) {
2275c1e08615SDag-Erling Smørgrav free(response);
2276c1e08615SDag-Erling Smørgrav response = read_passphrase("Accept updated hostkeys? "
2277c1e08615SDag-Erling Smørgrav "(yes/no): ", RP_ECHO);
227836b354d7SEd Maste if (response != NULL && strcasecmp(response, "yes") == 0)
2279c1e08615SDag-Erling Smørgrav break;
2280c1e08615SDag-Erling Smørgrav else if (quit_pending || response == NULL ||
2281c1e08615SDag-Erling Smørgrav strcasecmp(response, "no") == 0) {
2282c1e08615SDag-Erling Smørgrav options.update_hostkeys = 0;
2283c1e08615SDag-Erling Smørgrav break;
2284c1e08615SDag-Erling Smørgrav } else {
2285c1e08615SDag-Erling Smørgrav do_log2(loglevel, "Please enter "
2286c1e08615SDag-Erling Smørgrav "\"yes\" or \"no\"");
2287c1e08615SDag-Erling Smørgrav }
2288c1e08615SDag-Erling Smørgrav }
2289c1e08615SDag-Erling Smørgrav if (quit_pending || i >= 3 || response == NULL)
2290c1e08615SDag-Erling Smørgrav options.update_hostkeys = 0;
2291c1e08615SDag-Erling Smørgrav free(response);
2292c1e08615SDag-Erling Smørgrav if (was_raw)
2293c1e08615SDag-Erling Smørgrav enter_raw_mode(1);
2294c1e08615SDag-Erling Smørgrav }
229582e5fdc5SEd Maste if (options.update_hostkeys == 0)
229682e5fdc5SEd Maste return;
2297c1e08615SDag-Erling Smørgrav /*
2298c1e08615SDag-Erling Smørgrav * Now that all the keys are verified, we can go ahead and replace
2299c1e08615SDag-Erling Smørgrav * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
2300c1e08615SDag-Erling Smørgrav * cancel the operation).
2301c1e08615SDag-Erling Smørgrav */
230282e5fdc5SEd Maste for (i = 0; i < options.num_user_hostfiles; i++) {
230382e5fdc5SEd Maste /*
230482e5fdc5SEd Maste * NB. keys are only added to hostfiles[0], for the rest we
230582e5fdc5SEd Maste * just delete the hostname entries.
230682e5fdc5SEd Maste */
230782e5fdc5SEd Maste if (stat(options.user_hostfiles[i], &sb) != 0) {
230882e5fdc5SEd Maste if (errno == ENOENT) {
2309206be79aSEd Maste debug_f("known hosts file %s does not "
2310206be79aSEd Maste "exist", options.user_hostfiles[i]);
231182e5fdc5SEd Maste } else {
2312206be79aSEd Maste error_f("known hosts file %s "
2313206be79aSEd Maste "inaccessible: %s",
2314206be79aSEd Maste options.user_hostfiles[i], strerror(errno));
231582e5fdc5SEd Maste }
231682e5fdc5SEd Maste continue;
231782e5fdc5SEd Maste }
231882e5fdc5SEd Maste if ((r = hostfile_replace_entries(options.user_hostfiles[i],
231982e5fdc5SEd Maste ctx->host_str, ctx->ip_str,
232082e5fdc5SEd Maste i == 0 ? ctx->keys : NULL, i == 0 ? ctx->nkeys : 0,
2321c1e08615SDag-Erling Smørgrav options.hash_known_hosts, 0,
232282e5fdc5SEd Maste options.fingerprint_hash)) != 0) {
2323206be79aSEd Maste error_fr(r, "hostfile_replace_entries failed for %s",
2324206be79aSEd Maste options.user_hostfiles[i]);
232582e5fdc5SEd Maste }
232682e5fdc5SEd Maste }
2327c1e08615SDag-Erling Smørgrav }
2328c1e08615SDag-Erling Smørgrav
2329c1e08615SDag-Erling Smørgrav static void
client_global_hostkeys_prove_confirm(struct ssh * ssh,int type,u_int32_t seq,void * _ctx)23307ee81174SEd Maste client_global_hostkeys_prove_confirm(struct ssh *ssh, int type,
233120adc8f2SDag-Erling Smørgrav u_int32_t seq, void *_ctx)
2332c1e08615SDag-Erling Smørgrav {
2333c1e08615SDag-Erling Smørgrav struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
2334c1e08615SDag-Erling Smørgrav size_t i, ndone;
2335c1e08615SDag-Erling Smørgrav struct sshbuf *signdata;
233685d1f2d4SEd Maste int r, plaintype;
2337c1e08615SDag-Erling Smørgrav const u_char *sig;
233885d1f2d4SEd Maste const char *rsa_kexalg = NULL;
233985d1f2d4SEd Maste char *alg = NULL;
2340c1e08615SDag-Erling Smørgrav size_t siglen;
2341c1e08615SDag-Erling Smørgrav
2342c1e08615SDag-Erling Smørgrav if (ctx->nnew == 0)
2343206be79aSEd Maste fatal_f("ctx->nnew == 0"); /* sanity */
2344c1e08615SDag-Erling Smørgrav if (type != SSH2_MSG_REQUEST_SUCCESS) {
2345c1e08615SDag-Erling Smørgrav error("Server failed to confirm ownership of "
2346c1e08615SDag-Erling Smørgrav "private host keys");
2347c1e08615SDag-Erling Smørgrav hostkeys_update_ctx_free(ctx);
2348c1e08615SDag-Erling Smørgrav return;
2349c1e08615SDag-Erling Smørgrav }
235085d1f2d4SEd Maste if (sshkey_type_plain(sshkey_type_from_name(
235185d1f2d4SEd Maste ssh->kex->hostkey_alg)) == KEY_RSA)
235285d1f2d4SEd Maste rsa_kexalg = ssh->kex->hostkey_alg;
2353c1e08615SDag-Erling Smørgrav if ((signdata = sshbuf_new()) == NULL)
2354206be79aSEd Maste fatal_f("sshbuf_new failed");
2355c1e08615SDag-Erling Smørgrav /*
2356c1e08615SDag-Erling Smørgrav * Expect a signature for each of the ctx->nnew private keys we
2357c1e08615SDag-Erling Smørgrav * haven't seen before. They will be in the same order as the
2358206be79aSEd Maste * ctx->keys where the corresponding ctx->keys_match[i] == 0.
2359c1e08615SDag-Erling Smørgrav */
2360c1e08615SDag-Erling Smørgrav for (ndone = i = 0; i < ctx->nkeys; i++) {
2361206be79aSEd Maste if (ctx->keys_match[i])
2362c1e08615SDag-Erling Smørgrav continue;
236385d1f2d4SEd Maste plaintype = sshkey_type_plain(ctx->keys[i]->type);
2364c1e08615SDag-Erling Smørgrav /* Prepare data to be signed: session ID, unique string, key */
2365c1e08615SDag-Erling Smørgrav sshbuf_reset(signdata);
2366c1e08615SDag-Erling Smørgrav if ( (r = sshbuf_put_cstring(signdata,
2367c1e08615SDag-Erling Smørgrav "hostkeys-prove-00@openssh.com")) != 0 ||
2368206be79aSEd Maste (r = sshbuf_put_stringb(signdata,
2369206be79aSEd Maste ssh->kex->session_id)) != 0 ||
2370c1e08615SDag-Erling Smørgrav (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
2371206be79aSEd Maste fatal_fr(r, "compose signdata");
2372c1e08615SDag-Erling Smørgrav /* Extract and verify signature */
2373c1e08615SDag-Erling Smørgrav if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
2374206be79aSEd Maste error_fr(r, "parse sig");
2375c1e08615SDag-Erling Smørgrav goto out;
2376c1e08615SDag-Erling Smørgrav }
237785d1f2d4SEd Maste if ((r = sshkey_get_sigtype(sig, siglen, &alg)) != 0) {
237885d1f2d4SEd Maste error_fr(r, "server gave unintelligible signature "
237985d1f2d4SEd Maste "for %s key %zu", sshkey_type(ctx->keys[i]), i);
238085d1f2d4SEd Maste goto out;
238185d1f2d4SEd Maste }
2382c8a2bf14SDag-Erling Smørgrav /*
238385d1f2d4SEd Maste * Special case for RSA keys: if a RSA hostkey was negotiated,
238485d1f2d4SEd Maste * then use its signature type for verification of RSA hostkey
238585d1f2d4SEd Maste * proofs. Otherwise, accept only RSA-SHA256/512 signatures.
2386c8a2bf14SDag-Erling Smørgrav */
238785d1f2d4SEd Maste if (plaintype == KEY_RSA && rsa_kexalg == NULL &&
238885d1f2d4SEd Maste match_pattern_list(alg, HOSTKEY_PROOF_RSA_ALGS, 0) != 1) {
238985d1f2d4SEd Maste debug_f("server used untrusted RSA signature algorithm "
239085d1f2d4SEd Maste "%s for key %zu, disregarding", alg, i);
239185d1f2d4SEd Maste free(alg);
239285d1f2d4SEd Maste /* zap the key from the list */
239385d1f2d4SEd Maste sshkey_free(ctx->keys[i]);
239485d1f2d4SEd Maste ctx->keys[i] = NULL;
239585d1f2d4SEd Maste ndone++;
239685d1f2d4SEd Maste continue;
239785d1f2d4SEd Maste }
239885d1f2d4SEd Maste debug3_f("verify %s key %zu using sigalg %s",
239985d1f2d4SEd Maste sshkey_type(ctx->keys[i]), i, alg);
240085d1f2d4SEd Maste free(alg);
2401c1e08615SDag-Erling Smørgrav if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
2402c8a2bf14SDag-Erling Smørgrav sshbuf_ptr(signdata), sshbuf_len(signdata),
240385d1f2d4SEd Maste plaintype == KEY_RSA ? rsa_kexalg : NULL, 0, NULL)) != 0) {
240466719ee5SEd Maste error_fr(r, "server gave bad signature for %s key %zu",
2405206be79aSEd Maste sshkey_type(ctx->keys[i]), i);
2406c1e08615SDag-Erling Smørgrav goto out;
2407c1e08615SDag-Erling Smørgrav }
2408c1e08615SDag-Erling Smørgrav /* Key is good. Mark it as 'seen' */
2409206be79aSEd Maste ctx->keys_verified[i] = 1;
2410c1e08615SDag-Erling Smørgrav ndone++;
2411c1e08615SDag-Erling Smørgrav }
2412206be79aSEd Maste /* Shouldn't happen */
2413c1e08615SDag-Erling Smørgrav if (ndone != ctx->nnew)
2414206be79aSEd Maste fatal_f("ndone != ctx->nnew (%zu / %zu)", ndone, ctx->nnew);
2415f02e3998SEd Maste if ((r = sshpkt_get_end(ssh)) != 0) {
2416206be79aSEd Maste error_f("protocol error");
2417f02e3998SEd Maste goto out;
2418f02e3998SEd Maste }
2419c1e08615SDag-Erling Smørgrav
2420c1e08615SDag-Erling Smørgrav /* Make the edits to known_hosts */
2421c1e08615SDag-Erling Smørgrav update_known_hosts(ctx);
2422c1e08615SDag-Erling Smørgrav out:
2423c1e08615SDag-Erling Smørgrav hostkeys_update_ctx_free(ctx);
24247ee81174SEd Maste hostkeys_update_complete = 1;
24257ee81174SEd Maste client_repledge();
2426c1e08615SDag-Erling Smørgrav }
2427c1e08615SDag-Erling Smørgrav
2428c1e08615SDag-Erling Smørgrav /*
2429c1e08615SDag-Erling Smørgrav * Handle hostkeys-00@openssh.com global request to inform the client of all
2430c1e08615SDag-Erling Smørgrav * the server's hostkeys. The keys are checked against the user's
2431c1e08615SDag-Erling Smørgrav * HostkeyAlgorithms preference before they are accepted.
2432c1e08615SDag-Erling Smørgrav */
2433c1e08615SDag-Erling Smørgrav static int
client_input_hostkeys(struct ssh * ssh)2434f02e3998SEd Maste client_input_hostkeys(struct ssh *ssh)
2435c1e08615SDag-Erling Smørgrav {
2436c1e08615SDag-Erling Smørgrav const u_char *blob = NULL;
2437c1e08615SDag-Erling Smørgrav size_t i, len = 0;
2438c1e08615SDag-Erling Smørgrav struct sshbuf *buf = NULL;
2439c1e08615SDag-Erling Smørgrav struct sshkey *key = NULL, **tmp;
24407ee81174SEd Maste int r, prove_sent = 0;
2441c1e08615SDag-Erling Smørgrav char *fp;
2442c1e08615SDag-Erling Smørgrav static int hostkeys_seen = 0; /* XXX use struct ssh */
2443c1e08615SDag-Erling Smørgrav extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
2444c1e08615SDag-Erling Smørgrav struct hostkeys_update_ctx *ctx = NULL;
2445206be79aSEd Maste u_int want;
2446c1e08615SDag-Erling Smørgrav
2447c1e08615SDag-Erling Smørgrav if (hostkeys_seen)
2448206be79aSEd Maste fatal_f("server already sent hostkeys");
24497ee81174SEd Maste if (!can_update_hostkeys())
2450c1e08615SDag-Erling Smørgrav return 1;
24517ee81174SEd Maste hostkeys_seen = 1;
2452c1e08615SDag-Erling Smørgrav
2453c1e08615SDag-Erling Smørgrav ctx = xcalloc(1, sizeof(*ctx));
2454c1e08615SDag-Erling Smørgrav while (ssh_packet_remaining(ssh) > 0) {
2455c1e08615SDag-Erling Smørgrav sshkey_free(key);
2456c1e08615SDag-Erling Smørgrav key = NULL;
2457c1e08615SDag-Erling Smørgrav if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
2458206be79aSEd Maste error_fr(r, "parse key");
2459c1e08615SDag-Erling Smørgrav goto out;
2460c1e08615SDag-Erling Smørgrav }
2461c1e08615SDag-Erling Smørgrav if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
2462206be79aSEd Maste do_log2_fr(r, r == SSH_ERR_KEY_TYPE_UNKNOWN ?
246382e5fdc5SEd Maste SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_ERROR,
2464206be79aSEd Maste "convert key");
246582e5fdc5SEd Maste continue;
2466c1e08615SDag-Erling Smørgrav }
2467c1e08615SDag-Erling Smørgrav fp = sshkey_fingerprint(key, options.fingerprint_hash,
2468c1e08615SDag-Erling Smørgrav SSH_FP_DEFAULT);
2469206be79aSEd Maste debug3_f("received %s key %s", sshkey_type(key), fp);
2470c1e08615SDag-Erling Smørgrav free(fp);
2471d994eeedSDag-Erling Smørgrav
2472d565364dSEd Maste if (!hostkey_accepted_by_hostkeyalgs(key)) {
2473206be79aSEd Maste debug3_f("%s key not permitted by "
2474206be79aSEd Maste "HostkeyAlgorithms", sshkey_ssh_name(key));
2475c1e08615SDag-Erling Smørgrav continue;
2476c1e08615SDag-Erling Smørgrav }
2477c1e08615SDag-Erling Smørgrav /* Skip certs */
2478c1e08615SDag-Erling Smørgrav if (sshkey_is_cert(key)) {
2479206be79aSEd Maste debug3_f("%s key is a certificate; skipping",
2480206be79aSEd Maste sshkey_ssh_name(key));
2481c1e08615SDag-Erling Smørgrav continue;
2482c1e08615SDag-Erling Smørgrav }
2483c1e08615SDag-Erling Smørgrav /* Ensure keys are unique */
2484c1e08615SDag-Erling Smørgrav for (i = 0; i < ctx->nkeys; i++) {
2485c1e08615SDag-Erling Smørgrav if (sshkey_equal(key, ctx->keys[i])) {
2486206be79aSEd Maste error_f("received duplicated %s host key",
2487206be79aSEd Maste sshkey_ssh_name(key));
2488c1e08615SDag-Erling Smørgrav goto out;
2489c1e08615SDag-Erling Smørgrav }
2490c1e08615SDag-Erling Smørgrav }
2491c1e08615SDag-Erling Smørgrav /* Key is good, record it */
249220adc8f2SDag-Erling Smørgrav if ((tmp = recallocarray(ctx->keys, ctx->nkeys, ctx->nkeys + 1,
2493c1e08615SDag-Erling Smørgrav sizeof(*ctx->keys))) == NULL)
2494206be79aSEd Maste fatal_f("recallocarray failed nkeys = %zu",
2495206be79aSEd Maste ctx->nkeys);
2496c1e08615SDag-Erling Smørgrav ctx->keys = tmp;
2497c1e08615SDag-Erling Smørgrav ctx->keys[ctx->nkeys++] = key;
2498c1e08615SDag-Erling Smørgrav key = NULL;
2499c1e08615SDag-Erling Smørgrav }
2500c1e08615SDag-Erling Smørgrav
2501c1e08615SDag-Erling Smørgrav if (ctx->nkeys == 0) {
2502206be79aSEd Maste debug_f("server sent no hostkeys");
2503c1e08615SDag-Erling Smørgrav goto out;
2504c1e08615SDag-Erling Smørgrav }
2505c1e08615SDag-Erling Smørgrav
2506206be79aSEd Maste if ((ctx->keys_match = calloc(ctx->nkeys,
2507206be79aSEd Maste sizeof(*ctx->keys_match))) == NULL ||
2508206be79aSEd Maste (ctx->keys_verified = calloc(ctx->nkeys,
2509206be79aSEd Maste sizeof(*ctx->keys_verified))) == NULL)
2510206be79aSEd Maste fatal_f("calloc failed");
2511c1e08615SDag-Erling Smørgrav
2512c1e08615SDag-Erling Smørgrav get_hostfile_hostname_ipaddr(host,
2513c1e08615SDag-Erling Smørgrav options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
2514c1e08615SDag-Erling Smørgrav options.port, &ctx->host_str,
2515c1e08615SDag-Erling Smørgrav options.check_host_ip ? &ctx->ip_str : NULL);
2516c1e08615SDag-Erling Smørgrav
2517c1e08615SDag-Erling Smørgrav /* Find which keys we already know about. */
251882e5fdc5SEd Maste for (i = 0; i < options.num_user_hostfiles; i++) {
2519206be79aSEd Maste debug_f("searching %s for %s / %s",
252082e5fdc5SEd Maste options.user_hostfiles[i], ctx->host_str,
252182e5fdc5SEd Maste ctx->ip_str ? ctx->ip_str : "(none)");
252282e5fdc5SEd Maste if ((r = hostkeys_foreach(options.user_hostfiles[i],
252382e5fdc5SEd Maste hostkeys_find, ctx, ctx->host_str, ctx->ip_str,
2524206be79aSEd Maste HKF_WANT_PARSE_KEY, 0)) != 0) {
252582e5fdc5SEd Maste if (r == SSH_ERR_SYSTEM_ERROR && errno == ENOENT) {
2526206be79aSEd Maste debug_f("hostkeys file %s does not exist",
2527206be79aSEd Maste options.user_hostfiles[i]);
252882e5fdc5SEd Maste continue;
252982e5fdc5SEd Maste }
2530206be79aSEd Maste error_fr(r, "hostkeys_foreach failed for %s",
2531206be79aSEd Maste options.user_hostfiles[i]);
2532c1e08615SDag-Erling Smørgrav goto out;
2533c1e08615SDag-Erling Smørgrav }
253482e5fdc5SEd Maste }
2535c1e08615SDag-Erling Smørgrav
2536c1e08615SDag-Erling Smørgrav /* Figure out if we have any new keys to add */
2537206be79aSEd Maste ctx->nnew = ctx->nincomplete = 0;
2538206be79aSEd Maste want = HKF_MATCH_HOST | ( options.check_host_ip ? HKF_MATCH_IP : 0);
2539c1e08615SDag-Erling Smørgrav for (i = 0; i < ctx->nkeys; i++) {
2540206be79aSEd Maste if (ctx->keys_match[i] == 0)
2541c1e08615SDag-Erling Smørgrav ctx->nnew++;
2542206be79aSEd Maste if ((ctx->keys_match[i] & want) != want)
2543206be79aSEd Maste ctx->nincomplete++;
2544c1e08615SDag-Erling Smørgrav }
2545c1e08615SDag-Erling Smørgrav
2546206be79aSEd Maste debug3_f("%zu server keys: %zu new, %zu retained, "
2547206be79aSEd Maste "%zu incomplete match. %zu to remove", ctx->nkeys, ctx->nnew,
2548206be79aSEd Maste ctx->nkeys - ctx->nnew - ctx->nincomplete,
2549206be79aSEd Maste ctx->nincomplete, ctx->nold);
2550c1e08615SDag-Erling Smørgrav
2551206be79aSEd Maste if (ctx->nnew == 0 && ctx->nold == 0) {
2552206be79aSEd Maste debug_f("no new or deprecated keys from server");
2553206be79aSEd Maste goto out;
2554206be79aSEd Maste }
2555206be79aSEd Maste
2556206be79aSEd Maste /* Various reasons why we cannot proceed with the update */
2557206be79aSEd Maste if (ctx->complex_hostspec) {
2558206be79aSEd Maste debug_f("CA/revocation marker, manual host list or wildcard "
2559206be79aSEd Maste "host pattern found, skipping UserKnownHostsFile update");
2560206be79aSEd Maste goto out;
2561206be79aSEd Maste }
2562206be79aSEd Maste if (ctx->other_name_seen) {
2563206be79aSEd Maste debug_f("host key found matching a different name/address, "
2564206be79aSEd Maste "skipping UserKnownHostsFile update");
2565206be79aSEd Maste goto out;
2566206be79aSEd Maste }
2567c1e08615SDag-Erling Smørgrav /*
2568206be79aSEd Maste * If removing keys, check whether they appear under different
2569206be79aSEd Maste * names/addresses and refuse to proceed if they do. This avoids
2570206be79aSEd Maste * cases such as hosts with multiple names becoming inconsistent
2571206be79aSEd Maste * with regards to CheckHostIP entries.
2572206be79aSEd Maste * XXX UpdateHostkeys=force to override this (and other) checks?
2573206be79aSEd Maste */
2574206be79aSEd Maste if (ctx->nold != 0) {
2575206be79aSEd Maste if (check_old_keys_othernames(ctx) != 0)
2576206be79aSEd Maste goto out; /* error already logged */
2577206be79aSEd Maste if (ctx->old_key_seen) {
2578206be79aSEd Maste debug_f("key(s) for %s%s%s exist under other names; "
2579206be79aSEd Maste "skipping UserKnownHostsFile update",
2580206be79aSEd Maste ctx->host_str, ctx->ip_str == NULL ? "" : ",",
2581206be79aSEd Maste ctx->ip_str == NULL ? "" : ctx->ip_str);
2582206be79aSEd Maste goto out;
2583206be79aSEd Maste }
2584206be79aSEd Maste }
2585206be79aSEd Maste
2586206be79aSEd Maste if (ctx->nnew == 0) {
2587206be79aSEd Maste /*
2588206be79aSEd Maste * We have some keys to remove or fix matching for.
2589206be79aSEd Maste * We can proceed to do this without requiring a fresh proof
2590206be79aSEd Maste * from the server.
2591206be79aSEd Maste */
2592206be79aSEd Maste update_known_hosts(ctx);
2593206be79aSEd Maste goto out;
2594206be79aSEd Maste }
2595206be79aSEd Maste /*
2596206be79aSEd Maste * We have received previously-unseen keys from the server.
2597c1e08615SDag-Erling Smørgrav * Ask the server to confirm ownership of the private halves.
2598c1e08615SDag-Erling Smørgrav */
2599206be79aSEd Maste debug3_f("asking server to prove ownership for %zu keys", ctx->nnew);
2600c1e08615SDag-Erling Smørgrav if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2601c1e08615SDag-Erling Smørgrav (r = sshpkt_put_cstring(ssh,
2602c1e08615SDag-Erling Smørgrav "hostkeys-prove-00@openssh.com")) != 0 ||
2603c1e08615SDag-Erling Smørgrav (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
2604206be79aSEd Maste fatal_fr(r, "prepare hostkeys-prove");
2605c1e08615SDag-Erling Smørgrav if ((buf = sshbuf_new()) == NULL)
2606206be79aSEd Maste fatal_f("sshbuf_new");
2607c1e08615SDag-Erling Smørgrav for (i = 0; i < ctx->nkeys; i++) {
2608206be79aSEd Maste if (ctx->keys_match[i])
2609c1e08615SDag-Erling Smørgrav continue;
2610c1e08615SDag-Erling Smørgrav sshbuf_reset(buf);
2611206be79aSEd Maste if ((r = sshkey_putb(ctx->keys[i], buf)) != 0 ||
2612206be79aSEd Maste (r = sshpkt_put_stringb(ssh, buf)) != 0)
2613206be79aSEd Maste fatal_fr(r, "assemble hostkeys-prove");
2614c1e08615SDag-Erling Smørgrav }
2615c1e08615SDag-Erling Smørgrav if ((r = sshpkt_send(ssh)) != 0)
2616206be79aSEd Maste fatal_fr(r, "send hostkeys-prove");
2617c1e08615SDag-Erling Smørgrav client_register_global_confirm(
26187ee81174SEd Maste client_global_hostkeys_prove_confirm, ctx);
2619c1e08615SDag-Erling Smørgrav ctx = NULL; /* will be freed in callback */
26207ee81174SEd Maste prove_sent = 1;
2621c1e08615SDag-Erling Smørgrav
2622c1e08615SDag-Erling Smørgrav /* Success */
2623c1e08615SDag-Erling Smørgrav out:
2624c1e08615SDag-Erling Smørgrav hostkeys_update_ctx_free(ctx);
2625c1e08615SDag-Erling Smørgrav sshkey_free(key);
2626c1e08615SDag-Erling Smørgrav sshbuf_free(buf);
26277ee81174SEd Maste if (!prove_sent) {
26287ee81174SEd Maste /* UpdateHostkeys handling completed */
26297ee81174SEd Maste hostkeys_update_complete = 1;
26307ee81174SEd Maste client_repledge();
26317ee81174SEd Maste }
2632c1e08615SDag-Erling Smørgrav /*
2633c1e08615SDag-Erling Smørgrav * NB. Return success for all cases. The server doesn't need to know
2634c1e08615SDag-Erling Smørgrav * what the client does with its hosts file.
2635c1e08615SDag-Erling Smørgrav */
2636c1e08615SDag-Erling Smørgrav return 1;
2637c1e08615SDag-Erling Smørgrav }
2638c1e08615SDag-Erling Smørgrav
2639c1e08615SDag-Erling Smørgrav static int
client_input_global_request(int type,u_int32_t seq,struct ssh * ssh)264020adc8f2SDag-Erling Smørgrav client_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
2641ae1f160dSDag-Erling Smørgrav {
2642ae1f160dSDag-Erling Smørgrav char *rtype;
2643f02e3998SEd Maste u_char want_reply;
2644f02e3998SEd Maste int r, success = 0;
2645a04a10f8SKris Kennaway
2646f02e3998SEd Maste if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
2647f02e3998SEd Maste (r = sshpkt_get_u8(ssh, &want_reply)) != 0)
2648f02e3998SEd Maste goto out;
2649efcad6b7SDag-Erling Smørgrav debug("client_input_global_request: rtype %s want_reply %d",
2650efcad6b7SDag-Erling Smørgrav rtype, want_reply);
2651c1e08615SDag-Erling Smørgrav if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
2652f02e3998SEd Maste success = client_input_hostkeys(ssh);
2653ae1f160dSDag-Erling Smørgrav if (want_reply) {
2654f02e3998SEd Maste if ((r = sshpkt_start(ssh, success ? SSH2_MSG_REQUEST_SUCCESS :
2655f02e3998SEd Maste SSH2_MSG_REQUEST_FAILURE)) != 0 ||
2656f02e3998SEd Maste (r = sshpkt_send(ssh)) != 0 ||
2657f02e3998SEd Maste (r = ssh_packet_write_wait(ssh)) != 0)
2658f02e3998SEd Maste goto out;
2659ae1f160dSDag-Erling Smørgrav }
2660f02e3998SEd Maste r = 0;
2661f02e3998SEd Maste out:
26620dddc34cSDag-Erling Smørgrav free(rtype);
2663f02e3998SEd Maste return r;
2664ae1f160dSDag-Erling Smørgrav }
2665ae1f160dSDag-Erling Smørgrav
2666206be79aSEd Maste static void
client_send_env(struct ssh * ssh,int id,const char * name,const char * val)2667206be79aSEd Maste client_send_env(struct ssh *ssh, int id, const char *name, const char *val)
2668206be79aSEd Maste {
2669206be79aSEd Maste int r;
2670206be79aSEd Maste
2671206be79aSEd Maste debug("channel %d: setting env %s = \"%s\"", id, name, val);
2672206be79aSEd Maste channel_request_start(ssh, id, "env", 0);
2673206be79aSEd Maste if ((r = sshpkt_put_cstring(ssh, name)) != 0 ||
2674206be79aSEd Maste (r = sshpkt_put_cstring(ssh, val)) != 0 ||
2675206be79aSEd Maste (r = sshpkt_send(ssh)) != 0)
2676206be79aSEd Maste fatal_fr(r, "send setenv");
2677206be79aSEd Maste }
2678206be79aSEd Maste
2679d74d50a8SDag-Erling Smørgrav void
client_session2_setup(struct ssh * ssh,int id,int want_tty,int want_subsystem,const char * term,struct termios * tiop,int in_fd,struct sshbuf * cmd,char ** env)268020adc8f2SDag-Erling Smørgrav client_session2_setup(struct ssh *ssh, int id, int want_tty, int want_subsystem,
2681d46065dfSDag-Erling Smørgrav const char *term, struct termios *tiop, int in_fd, struct sshbuf *cmd,
2682d46065dfSDag-Erling Smørgrav char **env)
2683d74d50a8SDag-Erling Smørgrav {
268456fae065SEd Maste size_t i, j, len;
268556fae065SEd Maste int matched, r;
2686d46065dfSDag-Erling Smørgrav char *name, *val;
26875e8dbd04SDag-Erling Smørgrav Channel *c = NULL;
2688d74d50a8SDag-Erling Smørgrav
2689206be79aSEd Maste debug2_f("id %d", id);
2690d74d50a8SDag-Erling Smørgrav
269120adc8f2SDag-Erling Smørgrav if ((c = channel_lookup(ssh, id)) == NULL)
2692206be79aSEd Maste fatal_f("channel %d: unknown channel", id);
26935e8dbd04SDag-Erling Smørgrav
2694f02e3998SEd Maste ssh_packet_set_interactive(ssh, want_tty,
26959f6de2d7SDag-Erling Smørgrav options.ip_qos_interactive, options.ip_qos_bulk);
26969f6de2d7SDag-Erling Smørgrav
2697d74d50a8SDag-Erling Smørgrav if (want_tty) {
2698d74d50a8SDag-Erling Smørgrav struct winsize ws;
2699d74d50a8SDag-Erling Smørgrav
2700d74d50a8SDag-Erling Smørgrav /* Store window size in the packet. */
27010194e6d0SEd Maste if (ioctl(in_fd, TIOCGWINSZ, &ws) == -1)
2702d74d50a8SDag-Erling Smørgrav memset(&ws, 0, sizeof(ws));
2703d74d50a8SDag-Erling Smørgrav
270420adc8f2SDag-Erling Smørgrav channel_request_start(ssh, id, "pty-req", 1);
270520adc8f2SDag-Erling Smørgrav client_expect_confirm(ssh, id, "PTY allocation", CONFIRM_TTY);
2706f02e3998SEd Maste if ((r = sshpkt_put_cstring(ssh, term != NULL ? term : ""))
2707f02e3998SEd Maste != 0 ||
2708f02e3998SEd Maste (r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
2709f02e3998SEd Maste (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
2710f02e3998SEd Maste (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
2711f02e3998SEd Maste (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0)
2712206be79aSEd Maste fatal_fr(r, "build pty-req");
271355215393SDag-Erling Smørgrav if (tiop == NULL)
271455215393SDag-Erling Smørgrav tiop = get_saved_tio();
2715d46065dfSDag-Erling Smørgrav ssh_tty_make_modes(ssh, -1, tiop);
2716f02e3998SEd Maste if ((r = sshpkt_send(ssh)) != 0)
2717206be79aSEd Maste fatal_fr(r, "send pty-req");
2718d74d50a8SDag-Erling Smørgrav /* XXX wait for reply */
27195e8dbd04SDag-Erling Smørgrav c->client_tty = 1;
2720d74d50a8SDag-Erling Smørgrav }
2721d74d50a8SDag-Erling Smørgrav
2722d74d50a8SDag-Erling Smørgrav /* Transfer any environment variables from client to server */
2723d74d50a8SDag-Erling Smørgrav if (options.num_send_env != 0 && env != NULL) {
2724d74d50a8SDag-Erling Smørgrav debug("Sending environment.");
2725d74d50a8SDag-Erling Smørgrav for (i = 0; env[i] != NULL; i++) {
2726d74d50a8SDag-Erling Smørgrav /* Split */
2727d74d50a8SDag-Erling Smørgrav name = xstrdup(env[i]);
2728d74d50a8SDag-Erling Smørgrav if ((val = strchr(name, '=')) == NULL) {
27290dddc34cSDag-Erling Smørgrav free(name);
2730d74d50a8SDag-Erling Smørgrav continue;
2731d74d50a8SDag-Erling Smørgrav }
2732d74d50a8SDag-Erling Smørgrav *val++ = '\0';
2733d74d50a8SDag-Erling Smørgrav
2734d74d50a8SDag-Erling Smørgrav matched = 0;
2735d74d50a8SDag-Erling Smørgrav for (j = 0; j < options.num_send_env; j++) {
2736d74d50a8SDag-Erling Smørgrav if (match_pattern(name, options.send_env[j])) {
2737d74d50a8SDag-Erling Smørgrav matched = 1;
2738d74d50a8SDag-Erling Smørgrav break;
2739d74d50a8SDag-Erling Smørgrav }
2740d74d50a8SDag-Erling Smørgrav }
2741d74d50a8SDag-Erling Smørgrav if (!matched) {
2742d74d50a8SDag-Erling Smørgrav debug3("Ignored env %s", name);
27430dddc34cSDag-Erling Smørgrav free(name);
2744d74d50a8SDag-Erling Smørgrav continue;
2745d74d50a8SDag-Erling Smørgrav }
2746206be79aSEd Maste client_send_env(ssh, id, name, val);
27470dddc34cSDag-Erling Smørgrav free(name);
2748d74d50a8SDag-Erling Smørgrav }
2749d74d50a8SDag-Erling Smørgrav }
2750d46065dfSDag-Erling Smørgrav for (i = 0; i < options.num_setenv; i++) {
2751d46065dfSDag-Erling Smørgrav /* Split */
2752d46065dfSDag-Erling Smørgrav name = xstrdup(options.setenv[i]);
2753d46065dfSDag-Erling Smørgrav if ((val = strchr(name, '=')) == NULL) {
2754d46065dfSDag-Erling Smørgrav free(name);
2755d46065dfSDag-Erling Smørgrav continue;
2756d46065dfSDag-Erling Smørgrav }
2757d46065dfSDag-Erling Smørgrav *val++ = '\0';
2758206be79aSEd Maste client_send_env(ssh, id, name, val);
2759d46065dfSDag-Erling Smørgrav free(name);
2760d46065dfSDag-Erling Smørgrav }
2761d46065dfSDag-Erling Smørgrav
2762d46065dfSDag-Erling Smørgrav len = sshbuf_len(cmd);
2763d74d50a8SDag-Erling Smørgrav if (len > 0) {
2764d74d50a8SDag-Erling Smørgrav if (len > 900)
2765d74d50a8SDag-Erling Smørgrav len = 900;
2766d74d50a8SDag-Erling Smørgrav if (want_subsystem) {
276755215393SDag-Erling Smørgrav debug("Sending subsystem: %.*s",
276856fae065SEd Maste (int)len, (const u_char*)sshbuf_ptr(cmd));
276920adc8f2SDag-Erling Smørgrav channel_request_start(ssh, id, "subsystem", 1);
277020adc8f2SDag-Erling Smørgrav client_expect_confirm(ssh, id, "subsystem",
277120adc8f2SDag-Erling Smørgrav CONFIRM_CLOSE);
2772d74d50a8SDag-Erling Smørgrav } else {
277355215393SDag-Erling Smørgrav debug("Sending command: %.*s",
277456fae065SEd Maste (int)len, (const u_char*)sshbuf_ptr(cmd));
277520adc8f2SDag-Erling Smørgrav channel_request_start(ssh, id, "exec", 1);
277620adc8f2SDag-Erling Smørgrav client_expect_confirm(ssh, id, "exec", CONFIRM_CLOSE);
2777d74d50a8SDag-Erling Smørgrav }
2778f02e3998SEd Maste if ((r = sshpkt_put_stringb(ssh, cmd)) != 0 ||
2779f02e3998SEd Maste (r = sshpkt_send(ssh)) != 0)
2780206be79aSEd Maste fatal_fr(r, "send command");
2781d74d50a8SDag-Erling Smørgrav } else {
278220adc8f2SDag-Erling Smørgrav channel_request_start(ssh, id, "shell", 1);
278320adc8f2SDag-Erling Smørgrav client_expect_confirm(ssh, id, "shell", CONFIRM_CLOSE);
2784206be79aSEd Maste if ((r = sshpkt_send(ssh)) != 0)
2785206be79aSEd Maste fatal_fr(r, "send shell");
2786d74d50a8SDag-Erling Smørgrav }
27877ee81174SEd Maste
27887ee81174SEd Maste session_setup_complete = 1;
27897ee81174SEd Maste client_repledge();
2790d74d50a8SDag-Erling Smørgrav }
2791d74d50a8SDag-Erling Smørgrav
2792ae1f160dSDag-Erling Smørgrav static void
client_init_dispatch(struct ssh * ssh)2793f02e3998SEd Maste client_init_dispatch(struct ssh *ssh)
2794a04a10f8SKris Kennaway {
2795f02e3998SEd Maste ssh_dispatch_init(ssh, &dispatch_protocol_error);
2796545d5ecaSDag-Erling Smørgrav
2797f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2798f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2799f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2800f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
2801f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
2802f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2803f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2804f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
2805f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
2806f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2807f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
2808f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
28091e8db6e2SBrian Feldman
28101e8db6e2SBrian Feldman /* rekeying */
2811f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
2812545d5ecaSDag-Erling Smørgrav
2813545d5ecaSDag-Erling Smørgrav /* global request reply messages */
2814f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2815f02e3998SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
2816a04a10f8SKris Kennaway }
281755215393SDag-Erling Smørgrav
28181e26c29bSDag-Erling Smørgrav void
client_stop_mux(void)28191e26c29bSDag-Erling Smørgrav client_stop_mux(void)
28201e26c29bSDag-Erling Smørgrav {
28211e26c29bSDag-Erling Smørgrav if (options.control_path != NULL && muxserver_sock != -1)
28221e26c29bSDag-Erling Smørgrav unlink(options.control_path);
28231e26c29bSDag-Erling Smørgrav /*
28249b81c128SDag-Erling Smørgrav * If we are in persist mode, or don't have a shell, signal that we
28259b81c128SDag-Erling Smørgrav * should close when all active channels are closed.
28261e26c29bSDag-Erling Smørgrav */
282766719ee5SEd Maste if (options.control_persist || options.session_type == SESSION_TYPE_NONE) {
28281e26c29bSDag-Erling Smørgrav session_closed = 1;
28291e26c29bSDag-Erling Smørgrav setproctitle("[stopped mux]");
28301e26c29bSDag-Erling Smørgrav }
28311e26c29bSDag-Erling Smørgrav }
28321e26c29bSDag-Erling Smørgrav
2833efcad6b7SDag-Erling Smørgrav /* client specific fatal cleanup */
2834efcad6b7SDag-Erling Smørgrav void
cleanup_exit(int i)2835efcad6b7SDag-Erling Smørgrav cleanup_exit(int i)
2836efcad6b7SDag-Erling Smørgrav {
28371e26c29bSDag-Erling Smørgrav leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
283855215393SDag-Erling Smørgrav if (options.control_path != NULL && muxserver_sock != -1)
2839d74d50a8SDag-Erling Smørgrav unlink(options.control_path);
28409f6de2d7SDag-Erling Smørgrav ssh_kill_proxy_command();
2841efcad6b7SDag-Erling Smørgrav _exit(i);
2842efcad6b7SDag-Erling Smørgrav }
2843