xref: /src/crypto/openssh/serverloop.c (revision 8e28d84935f2f0ee081d44f9803f3052b960e50b)
19792a032SEd Maste /* $OpenBSD: serverloop.c,v 1.241 2024/11/26 22:01:37 djm 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  * Server main loop for handling the interactive session.
7b66f2d16SKris Kennaway  *
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  *
14a04a10f8SKris Kennaway  * SSH2 support by Markus Friedl.
15ae1f160dSDag-Erling Smørgrav  * Copyright (c) 2000, 2001 Markus Friedl.  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.
36a04a10f8SKris Kennaway  */
37511b41d2SMark Murray 
38511b41d2SMark Murray #include "includes.h"
39761efaa7SDag-Erling Smørgrav 
40761efaa7SDag-Erling Smørgrav #include <sys/types.h>
41761efaa7SDag-Erling Smørgrav #include <sys/wait.h>
42761efaa7SDag-Erling Smørgrav #include <sys/socket.h>
43761efaa7SDag-Erling Smørgrav #ifdef HAVE_SYS_TIME_H
44761efaa7SDag-Erling Smørgrav # include <sys/time.h>
45761efaa7SDag-Erling Smørgrav #endif
46761efaa7SDag-Erling Smørgrav 
47761efaa7SDag-Erling Smørgrav #include <netinet/in.h>
48761efaa7SDag-Erling Smørgrav 
49761efaa7SDag-Erling Smørgrav #include <errno.h>
50761efaa7SDag-Erling Smørgrav #include <fcntl.h>
51761efaa7SDag-Erling Smørgrav #include <pwd.h>
52f02e3998SEd Maste #include <limits.h>
5385d1f2d4SEd Maste #ifdef HAVE_POLL_H
5485d1f2d4SEd Maste #include <poll.h>
5585d1f2d4SEd Maste #endif
56761efaa7SDag-Erling Smørgrav #include <signal.h>
57761efaa7SDag-Erling Smørgrav #include <string.h>
58761efaa7SDag-Erling Smørgrav #include <termios.h>
59761efaa7SDag-Erling Smørgrav #include <unistd.h>
60761efaa7SDag-Erling Smørgrav #include <stdarg.h>
615b9b2fafSBrian Feldman 
6255215393SDag-Erling Smørgrav #include "openbsd-compat/sys-queue.h"
63511b41d2SMark Murray #include "xmalloc.h"
64511b41d2SMark Murray #include "packet.h"
65d46065dfSDag-Erling Smørgrav #include "sshbuf.h"
661e8db6e2SBrian Feldman #include "log.h"
67c0bbca73SDag-Erling Smørgrav #include "misc.h"
68511b41d2SMark Murray #include "servconf.h"
694b17dab0SDag-Erling Smørgrav #include "canohost.h"
701e8db6e2SBrian Feldman #include "sshpty.h"
71a04a10f8SKris Kennaway #include "channels.h"
72a04a10f8SKris Kennaway #include "ssh2.h"
73d46065dfSDag-Erling Smørgrav #include "sshkey.h"
74761efaa7SDag-Erling Smørgrav #include "cipher.h"
75761efaa7SDag-Erling Smørgrav #include "kex.h"
76761efaa7SDag-Erling Smørgrav #include "hostfile.h"
771e8db6e2SBrian Feldman #include "auth.h"
78a04a10f8SKris Kennaway #include "session.h"
79a04a10f8SKris Kennaway #include "dispatch.h"
80b66f2d16SKris Kennaway #include "auth-options.h"
811e8db6e2SBrian Feldman #include "serverloop.h"
82c1e08615SDag-Erling Smørgrav #include "ssherr.h"
83511b41d2SMark Murray 
845b9b2fafSBrian Feldman extern ServerOptions options;
855b9b2fafSBrian Feldman 
861e8db6e2SBrian Feldman /* XXX */
87efcad6b7SDag-Erling Smørgrav extern Authctxt *the_authctxt;
88c8a2bf14SDag-Erling Smørgrav extern struct sshauthopt *auth_opts;
891e8db6e2SBrian Feldman 
9055215393SDag-Erling Smørgrav static int no_more_sessions = 0; /* Disallow further sessions. */
91511b41d2SMark Murray 
92ae1f160dSDag-Erling Smørgrav static volatile sig_atomic_t child_terminated = 0;	/* The child has terminated. */
93511b41d2SMark Murray 
94ae1f160dSDag-Erling Smørgrav /* prototypes */
95f02e3998SEd Maste static void server_init_dispatch(struct ssh *);
96a04a10f8SKris Kennaway 
97c8a2bf14SDag-Erling Smørgrav /* requested tunnel forwarding interface(s), shared with session.c */
98c8a2bf14SDag-Erling Smørgrav char *tun_fwd_ifnames = NULL;
99c8a2bf14SDag-Erling Smørgrav 
100ae1f160dSDag-Erling Smørgrav static void
sigchld_handler(int sig)101511b41d2SMark Murray sigchld_handler(int sig)
102511b41d2SMark Murray {
103511b41d2SMark Murray 	child_terminated = 1;
104511b41d2SMark Murray }
105511b41d2SMark Murray 
106021d409fSDag-Erling Smørgrav static void
client_alive_check(struct ssh * ssh)10720adc8f2SDag-Erling Smørgrav client_alive_check(struct ssh *ssh)
108ae1f160dSDag-Erling Smørgrav {
109c8a2bf14SDag-Erling Smørgrav 	char remote_id[512];
110f02e3998SEd Maste 	int r, channel_id;
111ae1f160dSDag-Erling Smørgrav 
112ae1f160dSDag-Erling Smørgrav 	/* timeout, check to see how many we have had */
11382e5fdc5SEd Maste 	if (options.client_alive_count_max > 0 &&
11482e5fdc5SEd Maste 	    ssh_packet_inc_alive_timeouts(ssh) >
115f02e3998SEd Maste 	    options.client_alive_count_max) {
116c8a2bf14SDag-Erling Smørgrav 		sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
117c8a2bf14SDag-Erling Smørgrav 		logit("Timeout, client not responding from %s", remote_id);
11892eb0aa1SDag-Erling Smørgrav 		cleanup_exit(255);
11992eb0aa1SDag-Erling Smørgrav 	}
120ae1f160dSDag-Erling Smørgrav 
121ae1f160dSDag-Erling Smørgrav 	/*
122efcad6b7SDag-Erling Smørgrav 	 * send a bogus global/channel request with "wantreply",
123ae1f160dSDag-Erling Smørgrav 	 * we should get back a failure
124ae1f160dSDag-Erling Smørgrav 	 */
12520adc8f2SDag-Erling Smørgrav 	if ((channel_id = channel_find_open(ssh)) == -1) {
126f02e3998SEd Maste 		if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
127f02e3998SEd Maste 		    (r = sshpkt_put_cstring(ssh, "keepalive@openssh.com"))
128f02e3998SEd Maste 		    != 0 ||
129f02e3998SEd Maste 		    (r = sshpkt_put_u8(ssh, 1)) != 0) /* boolean: want reply */
130206be79aSEd Maste 			fatal_fr(r, "compose");
131efcad6b7SDag-Erling Smørgrav 	} else {
13220adc8f2SDag-Erling Smørgrav 		channel_request_start(ssh, channel_id,
13320adc8f2SDag-Erling Smørgrav 		    "keepalive@openssh.com", 1);
134efcad6b7SDag-Erling Smørgrav 	}
135f02e3998SEd Maste 	if ((r = sshpkt_send(ssh)) != 0)
136206be79aSEd Maste 		fatal_fr(r, "send");
137ae1f160dSDag-Erling Smørgrav }
138ae1f160dSDag-Erling Smørgrav 
139511b41d2SMark Murray /*
14085d1f2d4SEd Maste  * Sleep in ppoll() until we can do something.
14185d1f2d4SEd Maste  * Optionally, a maximum time can be specified for the duration of
14285d1f2d4SEd Maste  * the wait (0 = infinite).
143511b41d2SMark Murray  */
144ae1f160dSDag-Erling Smørgrav static void
wait_until_can_do_something(struct ssh * ssh,int connection_in,int connection_out,struct pollfd ** pfdp,u_int * npfd_allocp,u_int * npfd_activep,sigset_t * sigsetp,int * conn_in_readyp,int * conn_out_readyp)14520adc8f2SDag-Erling Smørgrav wait_until_can_do_something(struct ssh *ssh,
14685d1f2d4SEd Maste     int connection_in, int connection_out, struct pollfd **pfdp,
1477ee81174SEd Maste     u_int *npfd_allocp, u_int *npfd_activep, sigset_t *sigsetp,
1487ee81174SEd Maste     int *conn_in_readyp, int *conn_out_readyp)
149511b41d2SMark Murray {
1507ee81174SEd Maste 	struct timespec timeout;
1517ee81174SEd Maste 	char remote_id[512];
152511b41d2SMark Murray 	int ret;
1531e8db6e2SBrian Feldman 	int client_alive_scheduled = 0;
15485d1f2d4SEd Maste 	u_int p;
1557ee81174SEd Maste 	time_t now;
1567ee81174SEd Maste 	static time_t last_client_time, unused_connection_expiry;
1571e8db6e2SBrian Feldman 
15885d1f2d4SEd Maste 	*conn_in_readyp = *conn_out_readyp = 0;
15985d1f2d4SEd Maste 
16085d1f2d4SEd Maste 	/* Prepare channel poll. First two pollfd entries are reserved */
1617ee81174SEd Maste 	ptimeout_init(&timeout);
1627ee81174SEd Maste 	channel_prepare_poll(ssh, pfdp, npfd_allocp, npfd_activep, 2, &timeout);
1637ee81174SEd Maste 	now = monotime();
16485d1f2d4SEd Maste 	if (*npfd_activep < 2)
16585d1f2d4SEd Maste 		fatal_f("bad npfd %u", *npfd_activep); /* shouldn't happen */
1667ee81174SEd Maste 	if (options.rekey_interval > 0 && !ssh_packet_is_rekeying(ssh)) {
1677ee81174SEd Maste 		ptimeout_deadline_sec(&timeout,
1687ee81174SEd Maste 		    ssh_packet_get_rekey_timeout(ssh));
1697ee81174SEd Maste 	}
170925f1fb7SDag-Erling Smørgrav 
1717ee81174SEd Maste 	/*
1727ee81174SEd Maste 	 * If no channels are open and UnusedConnectionTimeout is set, then
1737ee81174SEd Maste 	 * start the clock to terminate the connection.
1747ee81174SEd Maste 	 */
1757ee81174SEd Maste 	if (options.unused_connection_timeout != 0) {
1767ee81174SEd Maste 		if (channel_still_open(ssh) || unused_connection_expiry == 0) {
1777ee81174SEd Maste 			unused_connection_expiry = now +
1787ee81174SEd Maste 			    options.unused_connection_timeout;
1797ee81174SEd Maste 		}
1807ee81174SEd Maste 		ptimeout_deadline_monotime(&timeout, unused_connection_expiry);
1817ee81174SEd Maste 	}
182925f1fb7SDag-Erling Smørgrav 
1831e8db6e2SBrian Feldman 	/*
1841e8db6e2SBrian Feldman 	 * if using client_alive, set the max timeout accordingly,
1851e8db6e2SBrian Feldman 	 * and indicate that this particular timeout was for client
1861e8db6e2SBrian Feldman 	 * alive by setting the client_alive_scheduled flag.
1871e8db6e2SBrian Feldman 	 *
1881e8db6e2SBrian Feldman 	 * this could be randomized somewhat to make traffic
1891e8db6e2SBrian Feldman 	 * analysis more difficult, but we're not doing it yet.
1901e8db6e2SBrian Feldman 	 */
19119ca8551SDag-Erling Smørgrav 	if (options.client_alive_interval) {
1927ee81174SEd Maste 		/* Time we last heard from the client OR sent a keepalive */
1933bbd8dc9SEd Maste 		if (last_client_time == 0)
1947ee81174SEd Maste 			last_client_time = now;
1957ee81174SEd Maste 		ptimeout_deadline_sec(&timeout, options.client_alive_interval);
1967ee81174SEd Maste 		/* XXX ? deadline_monotime(last_client_time + alive_interval) */
1977ee81174SEd Maste 		client_alive_scheduled = 1;
198ae1f160dSDag-Erling Smørgrav 	}
199511b41d2SMark Murray 
200ae1f160dSDag-Erling Smørgrav #if 0
201a04a10f8SKris Kennaway 	/* wrong: bad condition XXX */
202a04a10f8SKris Kennaway 	if (channel_not_very_much_buffered_data())
203ae1f160dSDag-Erling Smørgrav #endif
20485d1f2d4SEd Maste 	/* Monitor client connection on reserved pollfd entries */
20585d1f2d4SEd Maste 	(*pfdp)[0].fd = connection_in;
20685d1f2d4SEd Maste 	(*pfdp)[0].events = POLLIN;
20785d1f2d4SEd Maste 	(*pfdp)[1].fd = connection_out;
20885d1f2d4SEd Maste 	(*pfdp)[1].events = ssh_packet_have_data_to_write(ssh) ? POLLOUT : 0;
209511b41d2SMark Murray 
210511b41d2SMark Murray 	/*
211511b41d2SMark Murray 	 * If child has terminated and there is enough buffer space to read
212511b41d2SMark Murray 	 * from it, then read as much as is available and exit.
213511b41d2SMark Murray 	 */
214f02e3998SEd Maste 	if (child_terminated && ssh_packet_not_very_much_data_to_write(ssh))
2157ee81174SEd Maste 		ptimeout_deadline_ms(&timeout, 100);
216511b41d2SMark Murray 
217511b41d2SMark Murray 	/* Wait for something to happen, or the timeout to expire. */
2187ee81174SEd Maste 	ret = ppoll(*pfdp, *npfd_activep, ptimeout_get_tsp(&timeout), sigsetp);
219511b41d2SMark Murray 
2201e8db6e2SBrian Feldman 	if (ret == -1) {
22185d1f2d4SEd Maste 		for (p = 0; p < *npfd_activep; p++)
22285d1f2d4SEd Maste 			(*pfdp)[p].revents = 0;
223511b41d2SMark Murray 		if (errno != EINTR)
22485d1f2d4SEd Maste 			fatal_f("ppoll: %.100s", strerror(errno));
22585d1f2d4SEd Maste 		return;
22685d1f2d4SEd Maste 	}
22785d1f2d4SEd Maste 
22885d1f2d4SEd Maste 	*conn_in_readyp = (*pfdp)[0].revents != 0;
22985d1f2d4SEd Maste 	*conn_out_readyp = (*pfdp)[1].revents != 0;
23085d1f2d4SEd Maste 
2317ee81174SEd Maste 	now = monotime(); /* need to reset after ppoll() */
2327ee81174SEd Maste 	/* ClientAliveInterval probing */
23385d1f2d4SEd Maste 	if (client_alive_scheduled) {
2347ee81174SEd Maste 		if (ret == 0 &&
235b3ced7f2SEd Maste 		    now >= last_client_time + options.client_alive_interval) {
2367ee81174SEd Maste 			/* ppoll timed out and we're due to probe */
23720adc8f2SDag-Erling Smørgrav 			client_alive_check(ssh);
23820adc8f2SDag-Erling Smørgrav 			last_client_time = now;
2397ee81174SEd Maste 		} else if (ret != 0 && *conn_in_readyp) {
2407ee81174SEd Maste 			/* Data from peer; reset probe timer. */
24120adc8f2SDag-Erling Smørgrav 			last_client_time = now;
24220adc8f2SDag-Erling Smørgrav 		}
24320adc8f2SDag-Erling Smørgrav 	}
244511b41d2SMark Murray 
2457ee81174SEd Maste 	/* UnusedConnectionTimeout handling */
2467ee81174SEd Maste 	if (unused_connection_expiry != 0 &&
2477ee81174SEd Maste 	    now > unused_connection_expiry && !channel_still_open(ssh)) {
2487ee81174SEd Maste 		sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
2497ee81174SEd Maste 		logit("terminating inactive connection from %s", remote_id);
2507ee81174SEd Maste 		cleanup_exit(255);
2517ee81174SEd Maste 	}
2527ee81174SEd Maste }
2537ee81174SEd Maste 
254511b41d2SMark Murray /*
255511b41d2SMark Murray  * Processes input from the client and the program.  Input data is stored
256511b41d2SMark Murray  * in buffers and processed later.
257511b41d2SMark Murray  */
25819ca8551SDag-Erling Smørgrav static int
process_input(struct ssh * ssh,int connection_in)25985d1f2d4SEd Maste process_input(struct ssh *ssh, int connection_in)
260511b41d2SMark Murray {
26185d1f2d4SEd Maste 	int r;
262511b41d2SMark Murray 
26385d1f2d4SEd Maste 	if ((r = ssh_packet_process_read(ssh, connection_in)) == 0)
26485d1f2d4SEd Maste 		return 0; /* success */
26585d1f2d4SEd Maste 	if (r == SSH_ERR_SYSTEM_ERROR) {
26685d1f2d4SEd Maste 		if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)
26785d1f2d4SEd Maste 			return 0;
26885d1f2d4SEd Maste 		if (errno == EPIPE) {
269d565364dSEd Maste 			logit("Connection closed by %.100s port %d",
270ab4ec008SDag-Erling Smørgrav 			    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
27119ca8551SDag-Erling Smørgrav 			return -1;
27285d1f2d4SEd Maste 		}
273d565364dSEd Maste 		logit("Read error from remote host %s port %d: %s",
274206be79aSEd Maste 		    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
275206be79aSEd Maste 		    strerror(errno));
276efcad6b7SDag-Erling Smørgrav 		cleanup_exit(255);
277511b41d2SMark Murray 	}
27885d1f2d4SEd Maste 	return -1;
2792632b0c8SKris Kennaway }
280511b41d2SMark Murray 
281511b41d2SMark Murray /*
282511b41d2SMark Murray  * Sends data from internal buffers to client program stdin.
283511b41d2SMark Murray  */
284ae1f160dSDag-Erling Smørgrav static void
process_output(struct ssh * ssh,int connection_out)28585d1f2d4SEd Maste process_output(struct ssh *ssh, int connection_out)
286511b41d2SMark Murray {
287f02e3998SEd Maste 	int r;
288f02e3998SEd Maste 
289511b41d2SMark Murray 	/* Send any buffered packet data to the client. */
29082e5fdc5SEd Maste 	if ((r = ssh_packet_write_poll(ssh)) != 0) {
29182e5fdc5SEd Maste 		sshpkt_fatal(ssh, r, "%s: ssh_packet_write_poll",
29282e5fdc5SEd Maste 		    __func__);
29382e5fdc5SEd Maste 	}
294f02e3998SEd Maste }
295511b41d2SMark Murray 
296ae1f160dSDag-Erling Smørgrav static void
process_buffered_input_packets(struct ssh * ssh)29720adc8f2SDag-Erling Smørgrav process_buffered_input_packets(struct ssh *ssh)
298a04a10f8SKris Kennaway {
29920adc8f2SDag-Erling Smørgrav 	ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, NULL);
300a04a10f8SKris Kennaway }
301a04a10f8SKris Kennaway 
302ae1f160dSDag-Erling Smørgrav static void
collect_children(struct ssh * ssh)30320adc8f2SDag-Erling Smørgrav collect_children(struct ssh *ssh)
304ae1f160dSDag-Erling Smørgrav {
305ae1f160dSDag-Erling Smørgrav 	pid_t pid;
306ae1f160dSDag-Erling Smørgrav 	int status;
307ae1f160dSDag-Erling Smørgrav 
308ae1f160dSDag-Erling Smørgrav 	if (child_terminated) {
309761efaa7SDag-Erling Smørgrav 		debug("Received SIGCHLD.");
310545d5ecaSDag-Erling Smørgrav 		while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
3110194e6d0SEd Maste 		    (pid == -1 && errno == EINTR))
312545d5ecaSDag-Erling Smørgrav 			if (pid > 0)
31320adc8f2SDag-Erling Smørgrav 				session_close_by_pid(ssh, pid, status);
314ae1f160dSDag-Erling Smørgrav 		child_terminated = 0;
315ae1f160dSDag-Erling Smørgrav 	}
316ae1f160dSDag-Erling Smørgrav }
317ae1f160dSDag-Erling Smørgrav 
318a04a10f8SKris Kennaway void
server_loop2(struct ssh * ssh,Authctxt * authctxt)31920adc8f2SDag-Erling Smørgrav server_loop2(struct ssh *ssh, Authctxt *authctxt)
320a04a10f8SKris Kennaway {
32185d1f2d4SEd Maste 	struct pollfd *pfd = NULL;
32285d1f2d4SEd Maste 	u_int npfd_alloc = 0, npfd_active = 0;
32385d1f2d4SEd Maste 	int r, conn_in_ready, conn_out_ready;
32485d1f2d4SEd Maste 	u_int connection_in, connection_out;
32566719ee5SEd Maste 	sigset_t bsigset, osigset;
326a04a10f8SKris Kennaway 
327a04a10f8SKris Kennaway 	debug("Entering interactive session for SSH2.");
328a04a10f8SKris Kennaway 
32966719ee5SEd Maste 	if (sigemptyset(&bsigset) == -1 || sigaddset(&bsigset, SIGCHLD) == -1)
33066719ee5SEd Maste 		error_f("bsigset setup: %s", strerror(errno));
33182e5fdc5SEd Maste 	ssh_signal(SIGCHLD, sigchld_handler);
332a04a10f8SKris Kennaway 	child_terminated = 0;
333f02e3998SEd Maste 	connection_in = ssh_packet_get_connection_in(ssh);
334f02e3998SEd Maste 	connection_out = ssh_packet_get_connection_out(ssh);
3351e8db6e2SBrian Feldman 
336f02e3998SEd Maste 	server_init_dispatch(ssh);
337a04a10f8SKris Kennaway 
338a04a10f8SKris Kennaway 	for (;;) {
33920adc8f2SDag-Erling Smørgrav 		process_buffered_input_packets(ssh);
3401e8db6e2SBrian Feldman 
34120adc8f2SDag-Erling Smørgrav 		if (!ssh_packet_is_rekeying(ssh) &&
342f02e3998SEd Maste 		    ssh_packet_not_very_much_data_to_write(ssh))
34320adc8f2SDag-Erling Smørgrav 			channel_output_poll(ssh);
3440dddc34cSDag-Erling Smørgrav 
34566719ee5SEd Maste 		/*
34666719ee5SEd Maste 		 * Block SIGCHLD while we check for dead children, then pass
34785d1f2d4SEd Maste 		 * the old signal mask through to ppoll() so that it'll wake
34866719ee5SEd Maste 		 * up immediately if a child exits after we've called waitpid().
34966719ee5SEd Maste 		 */
35066719ee5SEd Maste 		if (sigprocmask(SIG_BLOCK, &bsigset, &osigset) == -1)
35166719ee5SEd Maste 			error_f("bsigset sigprocmask: %s", strerror(errno));
35266719ee5SEd Maste 		collect_children(ssh);
35320adc8f2SDag-Erling Smørgrav 		wait_until_can_do_something(ssh, connection_in, connection_out,
3547ee81174SEd Maste 		    &pfd, &npfd_alloc, &npfd_active, &osigset,
35585d1f2d4SEd Maste 		    &conn_in_ready, &conn_out_ready);
356d565364dSEd Maste 		if (sigprocmask(SIG_SETMASK, &osigset, NULL) == -1)
35766719ee5SEd Maste 			error_f("osigset sigprocmask: %s", strerror(errno));
358ae1f160dSDag-Erling Smørgrav 
35985d1f2d4SEd Maste 		channel_after_poll(ssh, pfd, npfd_active);
36085d1f2d4SEd Maste 		if (conn_in_ready &&
36185d1f2d4SEd Maste 		    process_input(ssh, connection_in) < 0)
3621e8db6e2SBrian Feldman 			break;
36366719ee5SEd Maste 		/* A timeout may have triggered rekeying */
36466719ee5SEd Maste 		if ((r = ssh_packet_check_rekey(ssh)) != 0)
36566719ee5SEd Maste 			fatal_fr(r, "cannot start rekeying");
36685d1f2d4SEd Maste 		if (conn_out_ready)
36785d1f2d4SEd Maste 			process_output(ssh, connection_out);
368a04a10f8SKris Kennaway 	}
36920adc8f2SDag-Erling Smørgrav 	collect_children(ssh);
37085d1f2d4SEd Maste 	free(pfd);
3711e8db6e2SBrian Feldman 
372ae1f160dSDag-Erling Smørgrav 	/* free all channels, no more reads and writes */
37320adc8f2SDag-Erling Smørgrav 	channel_free_all(ssh);
374ae1f160dSDag-Erling Smørgrav 
375ae1f160dSDag-Erling Smørgrav 	/* free remaining sessions, e.g. remove wtmp entries */
37620adc8f2SDag-Erling Smørgrav 	session_destroy_all(ssh, NULL);
377a04a10f8SKris Kennaway }
378a04a10f8SKris Kennaway 
379c1e08615SDag-Erling Smørgrav static int
server_input_keep_alive(int type,u_int32_t seq,struct ssh * ssh)38020adc8f2SDag-Erling Smørgrav server_input_keep_alive(int type, u_int32_t seq, struct ssh *ssh)
3811e8db6e2SBrian Feldman {
382efcad6b7SDag-Erling Smørgrav 	debug("Got %d/%u for keepalive", type, seq);
3831e8db6e2SBrian Feldman 	/*
3841e8db6e2SBrian Feldman 	 * reset timeout, since we got a sane answer from the client.
3851e8db6e2SBrian Feldman 	 * even if this was generated by something other than
3861e8db6e2SBrian Feldman 	 * the bogus CHANNEL_REQUEST we send for keepalives.
3871e8db6e2SBrian Feldman 	 */
388f02e3998SEd Maste 	ssh_packet_set_alive_timeouts(ssh, 0);
389c1e08615SDag-Erling Smørgrav 	return 0;
3901e8db6e2SBrian Feldman }
3911e8db6e2SBrian Feldman 
392ae1f160dSDag-Erling Smørgrav static Channel *
server_request_direct_tcpip(struct ssh * ssh,int * reason,const char ** errmsg)39320adc8f2SDag-Erling Smørgrav server_request_direct_tcpip(struct ssh *ssh, int *reason, const char **errmsg)
394a04a10f8SKris Kennaway {
3959b81c128SDag-Erling Smørgrav 	Channel *c = NULL;
396f02e3998SEd Maste 	char *target = NULL, *originator = NULL;
397f02e3998SEd Maste 	u_int target_port = 0, originator_port = 0;
398f02e3998SEd Maste 	int r;
399a04a10f8SKris Kennaway 
400f02e3998SEd Maste 	if ((r = sshpkt_get_cstring(ssh, &target, NULL)) != 0 ||
401f02e3998SEd Maste 	    (r = sshpkt_get_u32(ssh, &target_port)) != 0 ||
402f02e3998SEd Maste 	    (r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 ||
403f02e3998SEd Maste 	    (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
404f02e3998SEd Maste 	    (r = sshpkt_get_end(ssh)) != 0)
405f02e3998SEd Maste 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
406f02e3998SEd Maste 	if (target_port > 0xFFFF) {
407206be79aSEd Maste 		error_f("invalid target port");
408f02e3998SEd Maste 		*reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
409f02e3998SEd Maste 		goto out;
410f02e3998SEd Maste 	}
411f02e3998SEd Maste 	if (originator_port > 0xFFFF) {
412206be79aSEd Maste 		error_f("invalid originator port");
413f02e3998SEd Maste 		*reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
414f02e3998SEd Maste 		goto out;
415f02e3998SEd Maste 	}
4162632b0c8SKris Kennaway 
417206be79aSEd Maste 	debug_f("originator %s port %u, target %s port %u",
418c8a2bf14SDag-Erling Smørgrav 	    originator, originator_port, target, target_port);
419b66f2d16SKris Kennaway 
4209b81c128SDag-Erling Smørgrav 	/* XXX fine grained permissions */
4219b81c128SDag-Erling Smørgrav 	if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 &&
422c8a2bf14SDag-Erling Smørgrav 	    auth_opts->permit_port_forwarding_flag &&
423c8a2bf14SDag-Erling Smørgrav 	    !options.disable_forwarding) {
42420adc8f2SDag-Erling Smørgrav 		c = channel_connect_to_port(ssh, target, target_port,
425343d5771SDag-Erling Smørgrav 		    "direct-tcpip", "direct-tcpip", reason, errmsg);
4269b81c128SDag-Erling Smørgrav 	} else {
4279b81c128SDag-Erling Smørgrav 		logit("refused local port forward: "
4289b81c128SDag-Erling Smørgrav 		    "originator %s port %d, target %s port %d",
4299b81c128SDag-Erling Smørgrav 		    originator, originator_port, target, target_port);
430343d5771SDag-Erling Smørgrav 		if (reason != NULL)
431343d5771SDag-Erling Smørgrav 			*reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
4329b81c128SDag-Erling Smørgrav 	}
43355215393SDag-Erling Smørgrav 
434f02e3998SEd Maste  out:
4350dddc34cSDag-Erling Smørgrav 	free(originator);
4360dddc34cSDag-Erling Smørgrav 	free(target);
437ae1f160dSDag-Erling Smørgrav 	return c;
4381e8db6e2SBrian Feldman }
4391e8db6e2SBrian Feldman 
440ae1f160dSDag-Erling Smørgrav static Channel *
server_request_direct_streamlocal(struct ssh * ssh)44120adc8f2SDag-Erling Smørgrav server_request_direct_streamlocal(struct ssh *ssh)
442c0bbca73SDag-Erling Smørgrav {
443c0bbca73SDag-Erling Smørgrav 	Channel *c = NULL;
444f02e3998SEd Maste 	char *target = NULL, *originator = NULL;
445f02e3998SEd Maste 	u_int originator_port = 0;
446343d5771SDag-Erling Smørgrav 	struct passwd *pw = the_authctxt->pw;
447f02e3998SEd Maste 	int r;
448343d5771SDag-Erling Smørgrav 
449343d5771SDag-Erling Smørgrav 	if (pw == NULL || !the_authctxt->valid)
450206be79aSEd Maste 		fatal_f("no/invalid user");
451c0bbca73SDag-Erling Smørgrav 
452f02e3998SEd Maste 	if ((r = sshpkt_get_cstring(ssh, &target, NULL)) != 0 ||
453f02e3998SEd Maste 	    (r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 ||
454f02e3998SEd Maste 	    (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
455f02e3998SEd Maste 	    (r = sshpkt_get_end(ssh)) != 0)
456f02e3998SEd Maste 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
457f02e3998SEd Maste 	if (originator_port > 0xFFFF) {
458206be79aSEd Maste 		error_f("invalid originator port");
459f02e3998SEd Maste 		goto out;
460f02e3998SEd Maste 	}
461c0bbca73SDag-Erling Smørgrav 
462206be79aSEd Maste 	debug_f("originator %s port %d, target %s",
463c0bbca73SDag-Erling Smørgrav 	    originator, originator_port, target);
464c0bbca73SDag-Erling Smørgrav 
465c0bbca73SDag-Erling Smørgrav 	/* XXX fine grained permissions */
466c0bbca73SDag-Erling Smørgrav 	if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 &&
467c8a2bf14SDag-Erling Smørgrav 	    auth_opts->permit_port_forwarding_flag &&
468d565364dSEd Maste 	    !options.disable_forwarding) {
46920adc8f2SDag-Erling Smørgrav 		c = channel_connect_to_path(ssh, target,
470c0bbca73SDag-Erling Smørgrav 		    "direct-streamlocal@openssh.com", "direct-streamlocal");
471c0bbca73SDag-Erling Smørgrav 	} else {
472c0bbca73SDag-Erling Smørgrav 		logit("refused streamlocal port forward: "
473c0bbca73SDag-Erling Smørgrav 		    "originator %s port %d, target %s",
474c0bbca73SDag-Erling Smørgrav 		    originator, originator_port, target);
475c0bbca73SDag-Erling Smørgrav 	}
476c0bbca73SDag-Erling Smørgrav 
477f02e3998SEd Maste out:
478c0bbca73SDag-Erling Smørgrav 	free(originator);
479c0bbca73SDag-Erling Smørgrav 	free(target);
480c0bbca73SDag-Erling Smørgrav 	return c;
481c0bbca73SDag-Erling Smørgrav }
482c0bbca73SDag-Erling Smørgrav 
483c0bbca73SDag-Erling Smørgrav static Channel *
server_request_tun(struct ssh * ssh)48420adc8f2SDag-Erling Smørgrav server_request_tun(struct ssh *ssh)
485021d409fSDag-Erling Smørgrav {
486021d409fSDag-Erling Smørgrav 	Channel *c = NULL;
487f02e3998SEd Maste 	u_int mode, tun;
488f02e3998SEd Maste 	int r, sock;
489c8a2bf14SDag-Erling Smørgrav 	char *tmp, *ifname = NULL;
490021d409fSDag-Erling Smørgrav 
491f02e3998SEd Maste 	if ((r = sshpkt_get_u32(ssh, &mode)) != 0)
492f02e3998SEd Maste 		sshpkt_fatal(ssh, r, "%s: parse mode", __func__);
493021d409fSDag-Erling Smørgrav 	switch (mode) {
494021d409fSDag-Erling Smørgrav 	case SSH_TUNMODE_POINTOPOINT:
495021d409fSDag-Erling Smørgrav 	case SSH_TUNMODE_ETHERNET:
496021d409fSDag-Erling Smørgrav 		break;
497021d409fSDag-Erling Smørgrav 	default:
498f02e3998SEd Maste 		ssh_packet_send_debug(ssh, "Unsupported tunnel device mode.");
499021d409fSDag-Erling Smørgrav 		return NULL;
500021d409fSDag-Erling Smørgrav 	}
501021d409fSDag-Erling Smørgrav 	if ((options.permit_tun & mode) == 0) {
502f02e3998SEd Maste 		ssh_packet_send_debug(ssh, "Server has rejected tunnel device "
503021d409fSDag-Erling Smørgrav 		    "forwarding");
504021d409fSDag-Erling Smørgrav 		return NULL;
505021d409fSDag-Erling Smørgrav 	}
506021d409fSDag-Erling Smørgrav 
507f02e3998SEd Maste 	if ((r = sshpkt_get_u32(ssh, &tun)) != 0)
508f02e3998SEd Maste 		sshpkt_fatal(ssh, r, "%s: parse device", __func__);
509f02e3998SEd Maste 	if (tun > INT_MAX) {
510206be79aSEd Maste 		debug_f("invalid tun");
511f02e3998SEd Maste 		goto done;
512f02e3998SEd Maste 	}
513c8a2bf14SDag-Erling Smørgrav 	if (auth_opts->force_tun_device != -1) {
514f02e3998SEd Maste 		if (tun != SSH_TUNID_ANY &&
515f02e3998SEd Maste 		    auth_opts->force_tun_device != (int)tun)
516021d409fSDag-Erling Smørgrav 			goto done;
517c8a2bf14SDag-Erling Smørgrav 		tun = auth_opts->force_tun_device;
518021d409fSDag-Erling Smørgrav 	}
519c8a2bf14SDag-Erling Smørgrav 	sock = tun_open(tun, mode, &ifname);
520021d409fSDag-Erling Smørgrav 	if (sock < 0)
521021d409fSDag-Erling Smørgrav 		goto done;
522c8a2bf14SDag-Erling Smørgrav 	debug("Tunnel forwarding using interface %s", ifname);
523c8a2bf14SDag-Erling Smørgrav 
52420adc8f2SDag-Erling Smørgrav 	c = channel_new(ssh, "tun", SSH_CHANNEL_OPEN, sock, sock, -1,
525021d409fSDag-Erling Smørgrav 	    CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
526021d409fSDag-Erling Smørgrav 	c->datagram = 1;
527021d409fSDag-Erling Smørgrav #if defined(SSH_TUN_FILTER)
528021d409fSDag-Erling Smørgrav 	if (mode == SSH_TUNMODE_POINTOPOINT)
52920adc8f2SDag-Erling Smørgrav 		channel_register_filter(ssh, c->self, sys_tun_infilter,
53055215393SDag-Erling Smørgrav 		    sys_tun_outfilter, NULL, NULL);
531021d409fSDag-Erling Smørgrav #endif
532021d409fSDag-Erling Smørgrav 
533c8a2bf14SDag-Erling Smørgrav 	/*
534c8a2bf14SDag-Erling Smørgrav 	 * Update the list of names exposed to the session
535c8a2bf14SDag-Erling Smørgrav 	 * XXX remove these if the tunnels are closed (won't matter
536c8a2bf14SDag-Erling Smørgrav 	 * much if they are already in the environment though)
537c8a2bf14SDag-Erling Smørgrav 	 */
538c8a2bf14SDag-Erling Smørgrav 	tmp = tun_fwd_ifnames;
539c8a2bf14SDag-Erling Smørgrav 	xasprintf(&tun_fwd_ifnames, "%s%s%s",
540c8a2bf14SDag-Erling Smørgrav 	    tun_fwd_ifnames == NULL ? "" : tun_fwd_ifnames,
541c8a2bf14SDag-Erling Smørgrav 	    tun_fwd_ifnames == NULL ? "" : ",",
542c8a2bf14SDag-Erling Smørgrav 	    ifname);
543c8a2bf14SDag-Erling Smørgrav 	free(tmp);
544c8a2bf14SDag-Erling Smørgrav 	free(ifname);
545c8a2bf14SDag-Erling Smørgrav 
546021d409fSDag-Erling Smørgrav  done:
547021d409fSDag-Erling Smørgrav 	if (c == NULL)
548f02e3998SEd Maste 		ssh_packet_send_debug(ssh, "Failed to open the tunnel device.");
549021d409fSDag-Erling Smørgrav 	return c;
550021d409fSDag-Erling Smørgrav }
551021d409fSDag-Erling Smørgrav 
552021d409fSDag-Erling Smørgrav static Channel *
server_request_session(struct ssh * ssh)55320adc8f2SDag-Erling Smørgrav server_request_session(struct ssh *ssh)
5541e8db6e2SBrian Feldman {
555ae1f160dSDag-Erling Smørgrav 	Channel *c;
556f02e3998SEd Maste 	int r;
5571e8db6e2SBrian Feldman 
5581e8db6e2SBrian Feldman 	debug("input_session_request");
559f02e3998SEd Maste 	if ((r = sshpkt_get_end(ssh)) != 0)
560f02e3998SEd Maste 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
56155215393SDag-Erling Smørgrav 
56255215393SDag-Erling Smørgrav 	if (no_more_sessions) {
563f02e3998SEd Maste 		ssh_packet_disconnect(ssh, "Possible attack: attempt to open a "
564f02e3998SEd Maste 		    "session after additional sessions disabled");
56555215393SDag-Erling Smørgrav 	}
56655215393SDag-Erling Smørgrav 
5671e8db6e2SBrian Feldman 	/*
5681e8db6e2SBrian Feldman 	 * A server session has no fd to read or write until a
5691e8db6e2SBrian Feldman 	 * CHANNEL_REQUEST for a shell is made, so we set the type to
5701e8db6e2SBrian Feldman 	 * SSH_CHANNEL_LARVAL.  Additionally, a callback for handling all
5711e8db6e2SBrian Feldman 	 * CHANNEL_REQUEST messages is registered.
5721e8db6e2SBrian Feldman 	 */
57320adc8f2SDag-Erling Smørgrav 	c = channel_new(ssh, "session", SSH_CHANNEL_LARVAL,
574ae1f160dSDag-Erling Smørgrav 	    -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT,
575d95e11bfSDag-Erling Smørgrav 	    0, "server-session", 1);
576efcad6b7SDag-Erling Smørgrav 	if (session_open(the_authctxt, c->self) != 1) {
577ae1f160dSDag-Erling Smørgrav 		debug("session open failed, free channel %d", c->self);
57820adc8f2SDag-Erling Smørgrav 		channel_free(ssh, c);
5791e8db6e2SBrian Feldman 		return NULL;
580a04a10f8SKris Kennaway 	}
58120adc8f2SDag-Erling Smørgrav 	channel_register_cleanup(ssh, c->self, session_close_by_channel, 0);
582ae1f160dSDag-Erling Smørgrav 	return c;
583ae1f160dSDag-Erling Smørgrav }
584a04a10f8SKris Kennaway 
585c1e08615SDag-Erling Smørgrav static int
server_input_channel_open(int type,u_int32_t seq,struct ssh * ssh)58620adc8f2SDag-Erling Smørgrav server_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
587a04a10f8SKris Kennaway {
588a04a10f8SKris Kennaway 	Channel *c = NULL;
589f02e3998SEd Maste 	char *ctype = NULL;
590343d5771SDag-Erling Smørgrav 	const char *errmsg = NULL;
591f02e3998SEd Maste 	int r, reason = SSH2_OPEN_CONNECT_FAILED;
592f02e3998SEd Maste 	u_int rchan = 0, rmaxpack = 0, rwindow = 0;
593a04a10f8SKris Kennaway 
594f02e3998SEd Maste 	if ((r = sshpkt_get_cstring(ssh, &ctype, NULL)) != 0 ||
595f02e3998SEd Maste 	    (r = sshpkt_get_u32(ssh, &rchan)) != 0 ||
596f02e3998SEd Maste 	    (r = sshpkt_get_u32(ssh, &rwindow)) != 0 ||
597f02e3998SEd Maste 	    (r = sshpkt_get_u32(ssh, &rmaxpack)) != 0)
598f02e3998SEd Maste 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
599206be79aSEd Maste 	debug_f("ctype %s rchan %u win %u max %u",
600a04a10f8SKris Kennaway 	    ctype, rchan, rwindow, rmaxpack);
601a04a10f8SKris Kennaway 
60282e5fdc5SEd Maste 	if (strcmp(ctype, "session") == 0) {
60320adc8f2SDag-Erling Smørgrav 		c = server_request_session(ssh);
604a04a10f8SKris Kennaway 	} else if (strcmp(ctype, "direct-tcpip") == 0) {
60520adc8f2SDag-Erling Smørgrav 		c = server_request_direct_tcpip(ssh, &reason, &errmsg);
606c0bbca73SDag-Erling Smørgrav 	} else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) {
60720adc8f2SDag-Erling Smørgrav 		c = server_request_direct_streamlocal(ssh);
608021d409fSDag-Erling Smørgrav 	} else if (strcmp(ctype, "tun@openssh.com") == 0) {
60920adc8f2SDag-Erling Smørgrav 		c = server_request_tun(ssh);
610a04a10f8SKris Kennaway 	}
611a04a10f8SKris Kennaway 	if (c != NULL) {
612206be79aSEd Maste 		debug_f("confirm %s", ctype);
61382e5fdc5SEd Maste 		c->remote_id = rchan;
61420adc8f2SDag-Erling Smørgrav 		c->have_remote_id = 1;
615a04a10f8SKris Kennaway 		c->remote_window = rwindow;
616a04a10f8SKris Kennaway 		c->remote_maxpacket = rmaxpack;
617ae1f160dSDag-Erling Smørgrav 		if (c->type != SSH_CHANNEL_CONNECTING) {
618f02e3998SEd Maste 			if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
619f02e3998SEd Maste 			    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
620f02e3998SEd Maste 			    (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
621f02e3998SEd Maste 			    (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
622f02e3998SEd Maste 			    (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
623f02e3998SEd Maste 			    (r = sshpkt_send(ssh)) != 0) {
624f02e3998SEd Maste 				sshpkt_fatal(ssh, r,
625f02e3998SEd Maste 				    "%s: send open confirm", __func__);
626f02e3998SEd Maste 			}
627ae1f160dSDag-Erling Smørgrav 		}
628a04a10f8SKris Kennaway 	} else {
629206be79aSEd Maste 		debug_f("failure %s", ctype);
630f02e3998SEd Maste 		if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
631f02e3998SEd Maste 		    (r = sshpkt_put_u32(ssh, rchan)) != 0 ||
632f02e3998SEd Maste 		    (r = sshpkt_put_u32(ssh, reason)) != 0 ||
633f02e3998SEd Maste 		    (r = sshpkt_put_cstring(ssh, errmsg ? errmsg : "open failed")) != 0 ||
634f02e3998SEd Maste 		    (r = sshpkt_put_cstring(ssh, "")) != 0 ||
635f02e3998SEd Maste 		    (r = sshpkt_send(ssh)) != 0) {
636f02e3998SEd Maste 			sshpkt_fatal(ssh, r,
637f02e3998SEd Maste 			    "%s: send open failure", __func__);
638f02e3998SEd Maste 		}
639a04a10f8SKris Kennaway 	}
6400dddc34cSDag-Erling Smørgrav 	free(ctype);
641c1e08615SDag-Erling Smørgrav 	return 0;
642a04a10f8SKris Kennaway }
643a04a10f8SKris Kennaway 
644c1e08615SDag-Erling Smørgrav static int
server_input_hostkeys_prove(struct ssh * ssh,struct sshbuf ** respp)64520adc8f2SDag-Erling Smørgrav server_input_hostkeys_prove(struct ssh *ssh, struct sshbuf **respp)
646c1e08615SDag-Erling Smørgrav {
647c1e08615SDag-Erling Smørgrav 	struct sshbuf *resp = NULL;
648c1e08615SDag-Erling Smørgrav 	struct sshbuf *sigbuf = NULL;
649c1e08615SDag-Erling Smørgrav 	struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL;
65085d1f2d4SEd Maste 	int r, ndx, success = 0;
651c1e08615SDag-Erling Smørgrav 	const u_char *blob;
65285d1f2d4SEd Maste 	const char *sigalg, *kex_rsa_sigalg = NULL;
653c1e08615SDag-Erling Smørgrav 	u_char *sig = 0;
654c1e08615SDag-Erling Smørgrav 	size_t blen, slen;
655c1e08615SDag-Erling Smørgrav 
656c1e08615SDag-Erling Smørgrav 	if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL)
657206be79aSEd Maste 		fatal_f("sshbuf_new");
65885d1f2d4SEd Maste 	if (sshkey_type_plain(sshkey_type_from_name(
65985d1f2d4SEd Maste 	    ssh->kex->hostkey_alg)) == KEY_RSA)
66085d1f2d4SEd Maste 		kex_rsa_sigalg = ssh->kex->hostkey_alg;
661c1e08615SDag-Erling Smørgrav 	while (ssh_packet_remaining(ssh) > 0) {
662c1e08615SDag-Erling Smørgrav 		sshkey_free(key);
663c1e08615SDag-Erling Smørgrav 		key = NULL;
664c1e08615SDag-Erling Smørgrav 		if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 ||
665c1e08615SDag-Erling Smørgrav 		    (r = sshkey_from_blob(blob, blen, &key)) != 0) {
666206be79aSEd Maste 			error_fr(r, "parse key");
667c1e08615SDag-Erling Smørgrav 			goto out;
668c1e08615SDag-Erling Smørgrav 		}
669c1e08615SDag-Erling Smørgrav 		/*
670c1e08615SDag-Erling Smørgrav 		 * Better check that this is actually one of our hostkeys
671c1e08615SDag-Erling Smørgrav 		 * before attempting to sign anything with it.
672c1e08615SDag-Erling Smørgrav 		 */
673c1e08615SDag-Erling Smørgrav 		if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) {
674206be79aSEd Maste 			error_f("unknown host %s key", sshkey_type(key));
675c1e08615SDag-Erling Smørgrav 			goto out;
676c1e08615SDag-Erling Smørgrav 		}
677c1e08615SDag-Erling Smørgrav 		/*
678c1e08615SDag-Erling Smørgrav 		 * XXX refactor: make kex->sign just use an index rather
679c1e08615SDag-Erling Smørgrav 		 * than passing in public and private keys
680c1e08615SDag-Erling Smørgrav 		 */
681c1e08615SDag-Erling Smørgrav 		if ((key_prv = get_hostkey_by_index(ndx)) == NULL &&
682c1e08615SDag-Erling Smørgrav 		    (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) {
683206be79aSEd Maste 			error_f("can't retrieve hostkey %d", ndx);
684c1e08615SDag-Erling Smørgrav 			goto out;
685c1e08615SDag-Erling Smørgrav 		}
686c1e08615SDag-Erling Smørgrav 		sshbuf_reset(sigbuf);
687c1e08615SDag-Erling Smørgrav 		free(sig);
688c1e08615SDag-Erling Smørgrav 		sig = NULL;
689c8a2bf14SDag-Erling Smørgrav 		/*
690c8a2bf14SDag-Erling Smørgrav 		 * For RSA keys, prefer to use the signature type negotiated
691c8a2bf14SDag-Erling Smørgrav 		 * during KEX to the default (SHA1).
692c8a2bf14SDag-Erling Smørgrav 		 */
6939792a032SEd Maste 		sigalg = sshkey_ssh_name(key);
69485d1f2d4SEd Maste 		if (sshkey_type_plain(key->type) == KEY_RSA) {
69585d1f2d4SEd Maste 			if (kex_rsa_sigalg != NULL)
69685d1f2d4SEd Maste 				sigalg = kex_rsa_sigalg;
69785d1f2d4SEd Maste 			else if (ssh->kex->flags & KEX_RSA_SHA2_512_SUPPORTED)
69885d1f2d4SEd Maste 				sigalg = "rsa-sha2-512";
69985d1f2d4SEd Maste 			else if (ssh->kex->flags & KEX_RSA_SHA2_256_SUPPORTED)
70085d1f2d4SEd Maste 				sigalg = "rsa-sha2-256";
70185d1f2d4SEd Maste 		}
7029792a032SEd Maste 
70385d1f2d4SEd Maste 		debug3_f("sign %s key (index %d) using sigalg %s",
70485d1f2d4SEd Maste 		    sshkey_type(key), ndx, sigalg == NULL ? "default" : sigalg);
705c1e08615SDag-Erling Smørgrav 		if ((r = sshbuf_put_cstring(sigbuf,
706c1e08615SDag-Erling Smørgrav 		    "hostkeys-prove-00@openssh.com")) != 0 ||
707206be79aSEd Maste 		    (r = sshbuf_put_stringb(sigbuf,
708206be79aSEd Maste 		    ssh->kex->session_id)) != 0 ||
709c1e08615SDag-Erling Smørgrav 		    (r = sshkey_puts(key, sigbuf)) != 0 ||
710f02e3998SEd Maste 		    (r = ssh->kex->sign(ssh, key_prv, key_pub, &sig, &slen,
71185d1f2d4SEd Maste 		    sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), sigalg)) != 0 ||
712c1e08615SDag-Erling Smørgrav 		    (r = sshbuf_put_string(resp, sig, slen)) != 0) {
713206be79aSEd Maste 			error_fr(r, "assemble signature");
714c1e08615SDag-Erling Smørgrav 			goto out;
715c1e08615SDag-Erling Smørgrav 		}
716c1e08615SDag-Erling Smørgrav 	}
717c1e08615SDag-Erling Smørgrav 	/* Success */
718c1e08615SDag-Erling Smørgrav 	*respp = resp;
719c1e08615SDag-Erling Smørgrav 	resp = NULL; /* don't free it */
720c1e08615SDag-Erling Smørgrav 	success = 1;
721c1e08615SDag-Erling Smørgrav  out:
722c1e08615SDag-Erling Smørgrav 	free(sig);
723c1e08615SDag-Erling Smørgrav 	sshbuf_free(resp);
724c1e08615SDag-Erling Smørgrav 	sshbuf_free(sigbuf);
725c1e08615SDag-Erling Smørgrav 	sshkey_free(key);
726c1e08615SDag-Erling Smørgrav 	return success;
727c1e08615SDag-Erling Smørgrav }
728c1e08615SDag-Erling Smørgrav 
729c1e08615SDag-Erling Smørgrav static int
server_input_global_request(int type,u_int32_t seq,struct ssh * ssh)73020adc8f2SDag-Erling Smørgrav server_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
7311e8db6e2SBrian Feldman {
732f02e3998SEd Maste 	char *rtype = NULL;
733f02e3998SEd Maste 	u_char want_reply = 0;
734c1e08615SDag-Erling Smørgrav 	int r, success = 0, allocated_listen_port = 0;
735f02e3998SEd Maste 	u_int port = 0;
736c1e08615SDag-Erling Smørgrav 	struct sshbuf *resp = NULL;
737343d5771SDag-Erling Smørgrav 	struct passwd *pw = the_authctxt->pw;
738f02e3998SEd Maste 	struct Forward fwd;
739343d5771SDag-Erling Smørgrav 
740f02e3998SEd Maste 	memset(&fwd, 0, sizeof(fwd));
741343d5771SDag-Erling Smørgrav 	if (pw == NULL || !the_authctxt->valid)
742206be79aSEd Maste 		fatal_f("no/invalid user");
7431e8db6e2SBrian Feldman 
744f02e3998SEd Maste 	if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
745f02e3998SEd Maste 	    (r = sshpkt_get_u8(ssh, &want_reply)) != 0)
746f02e3998SEd Maste 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
747206be79aSEd Maste 	debug_f("rtype %s want_reply %d", rtype, want_reply);
7481e8db6e2SBrian Feldman 
7491e8db6e2SBrian Feldman 	/* -R style forwarding */
7501e8db6e2SBrian Feldman 	if (strcmp(rtype, "tcpip-forward") == 0) {
751f02e3998SEd Maste 		if ((r = sshpkt_get_cstring(ssh, &fwd.listen_host, NULL)) != 0 ||
752f02e3998SEd Maste 		    (r = sshpkt_get_u32(ssh, &port)) != 0)
753f02e3998SEd Maste 			sshpkt_fatal(ssh, r, "%s: parse tcpip-forward", __func__);
754206be79aSEd Maste 		debug_f("tcpip-forward listen %s port %u",
755f02e3998SEd Maste 		    fwd.listen_host, port);
756f02e3998SEd Maste 		if (port <= INT_MAX)
757f02e3998SEd Maste 			fwd.listen_port = (int)port;
7581e8db6e2SBrian Feldman 		/* check permissions */
759f02e3998SEd Maste 		if (port > INT_MAX ||
760f02e3998SEd Maste 		    (options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 ||
761c8a2bf14SDag-Erling Smørgrav 		    !auth_opts->permit_port_forwarding_flag ||
762c8a2bf14SDag-Erling Smørgrav 		    options.disable_forwarding ||
763d565364dSEd Maste 		    (!want_reply && fwd.listen_port == 0)) {
7641e8db6e2SBrian Feldman 			success = 0;
765f02e3998SEd Maste 			ssh_packet_send_debug(ssh, "Server has disabled port forwarding.");
7661e8db6e2SBrian Feldman 		} else {
7671e8db6e2SBrian Feldman 			/* Start listening on the port */
76820adc8f2SDag-Erling Smørgrav 			success = channel_setup_remote_fwd_listener(ssh, &fwd,
769c0bbca73SDag-Erling Smørgrav 			    &allocated_listen_port, &options.fwd_opts);
7701e8db6e2SBrian Feldman 		}
771c1e08615SDag-Erling Smørgrav 		if ((resp = sshbuf_new()) == NULL)
772206be79aSEd Maste 			fatal_f("sshbuf_new");
773ff4b04e0SDag-Erling Smørgrav 		if (allocated_listen_port != 0 &&
774ff4b04e0SDag-Erling Smørgrav 		    (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0)
775206be79aSEd Maste 			fatal_fr(r, "sshbuf_put_u32");
776d74d50a8SDag-Erling Smørgrav 	} else if (strcmp(rtype, "cancel-tcpip-forward") == 0) {
777f02e3998SEd Maste 		if ((r = sshpkt_get_cstring(ssh, &fwd.listen_host, NULL)) != 0 ||
778f02e3998SEd Maste 		    (r = sshpkt_get_u32(ssh, &port)) != 0)
779f02e3998SEd Maste 			sshpkt_fatal(ssh, r, "%s: parse cancel-tcpip-forward", __func__);
780d74d50a8SDag-Erling Smørgrav 
781206be79aSEd Maste 		debug_f("cancel-tcpip-forward addr %s port %d",
782f02e3998SEd Maste 		    fwd.listen_host, port);
783f02e3998SEd Maste 		if (port <= INT_MAX) {
784f02e3998SEd Maste 			fwd.listen_port = (int)port;
78520adc8f2SDag-Erling Smørgrav 			success = channel_cancel_rport_listener(ssh, &fwd);
786f02e3998SEd Maste 		}
787c0bbca73SDag-Erling Smørgrav 	} else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) {
788f02e3998SEd Maste 		if ((r = sshpkt_get_cstring(ssh, &fwd.listen_path, NULL)) != 0)
789f02e3998SEd Maste 			sshpkt_fatal(ssh, r, "%s: parse streamlocal-forward@openssh.com", __func__);
790206be79aSEd Maste 		debug_f("streamlocal-forward listen path %s",
791c0bbca73SDag-Erling Smørgrav 		    fwd.listen_path);
792c0bbca73SDag-Erling Smørgrav 
793c0bbca73SDag-Erling Smørgrav 		/* check permissions */
794c0bbca73SDag-Erling Smørgrav 		if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0
795c8a2bf14SDag-Erling Smørgrav 		    || !auth_opts->permit_port_forwarding_flag ||
796d565364dSEd Maste 		    options.disable_forwarding) {
797c0bbca73SDag-Erling Smørgrav 			success = 0;
798f02e3998SEd Maste 			ssh_packet_send_debug(ssh, "Server has disabled "
799343d5771SDag-Erling Smørgrav 			    "streamlocal forwarding.");
800c0bbca73SDag-Erling Smørgrav 		} else {
801c0bbca73SDag-Erling Smørgrav 			/* Start listening on the socket */
80220adc8f2SDag-Erling Smørgrav 			success = channel_setup_remote_fwd_listener(ssh,
803c0bbca73SDag-Erling Smørgrav 			    &fwd, NULL, &options.fwd_opts);
804c0bbca73SDag-Erling Smørgrav 		}
805c0bbca73SDag-Erling Smørgrav 	} else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) {
806f02e3998SEd Maste 		if ((r = sshpkt_get_cstring(ssh, &fwd.listen_path, NULL)) != 0)
807f02e3998SEd Maste 			sshpkt_fatal(ssh, r, "%s: parse cancel-streamlocal-forward@openssh.com", __func__);
808206be79aSEd Maste 		debug_f("cancel-streamlocal-forward path %s",
809c0bbca73SDag-Erling Smørgrav 		    fwd.listen_path);
810c0bbca73SDag-Erling Smørgrav 
81120adc8f2SDag-Erling Smørgrav 		success = channel_cancel_rport_listener(ssh, &fwd);
81255215393SDag-Erling Smørgrav 	} else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) {
81355215393SDag-Erling Smørgrav 		no_more_sessions = 1;
81455215393SDag-Erling Smørgrav 		success = 1;
815c1e08615SDag-Erling Smørgrav 	} else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) {
81620adc8f2SDag-Erling Smørgrav 		success = server_input_hostkeys_prove(ssh, &resp);
8171e8db6e2SBrian Feldman 	}
818f02e3998SEd Maste 	/* XXX sshpkt_get_end() */
8191e8db6e2SBrian Feldman 	if (want_reply) {
820f02e3998SEd Maste 		if ((r = sshpkt_start(ssh, success ?
821f02e3998SEd Maste 		    SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE)) != 0 ||
822f02e3998SEd Maste 		    (success && resp != NULL && (r = sshpkt_putb(ssh, resp)) != 0) ||
823f02e3998SEd Maste 		    (r = sshpkt_send(ssh)) != 0 ||
824f02e3998SEd Maste 		    (r = ssh_packet_write_wait(ssh)) != 0)
825f02e3998SEd Maste 			sshpkt_fatal(ssh, r, "%s: send reply", __func__);
8261e8db6e2SBrian Feldman 	}
827f02e3998SEd Maste 	free(fwd.listen_host);
828f02e3998SEd Maste 	free(fwd.listen_path);
8290dddc34cSDag-Erling Smørgrav 	free(rtype);
830c1e08615SDag-Erling Smørgrav 	sshbuf_free(resp);
831c1e08615SDag-Erling Smørgrav 	return 0;
8321e8db6e2SBrian Feldman }
833761efaa7SDag-Erling Smørgrav 
834c1e08615SDag-Erling Smørgrav static int
server_input_channel_req(int type,u_int32_t seq,struct ssh * ssh)83520adc8f2SDag-Erling Smørgrav server_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
836ae1f160dSDag-Erling Smørgrav {
837ae1f160dSDag-Erling Smørgrav 	Channel *c;
838f02e3998SEd Maste 	int r, success = 0;
839f02e3998SEd Maste 	char *rtype = NULL;
840f02e3998SEd Maste 	u_char want_reply = 0;
841f02e3998SEd Maste 	u_int id = 0;
8421e8db6e2SBrian Feldman 
843f02e3998SEd Maste 	if ((r = sshpkt_get_u32(ssh, &id)) != 0 ||
844f02e3998SEd Maste 	    (r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
845f02e3998SEd Maste 	    (r = sshpkt_get_u8(ssh, &want_reply)) != 0)
846f02e3998SEd Maste 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
847ae1f160dSDag-Erling Smørgrav 
848f02e3998SEd Maste 	debug("server_input_channel_req: channel %u request %s reply %d",
849f02e3998SEd Maste 	    id, rtype, want_reply);
850ae1f160dSDag-Erling Smørgrav 
851f02e3998SEd Maste 	if (id >= INT_MAX || (c = channel_lookup(ssh, (int)id)) == NULL) {
852f02e3998SEd Maste 		ssh_packet_disconnect(ssh, "%s: unknown channel %d",
853f02e3998SEd Maste 		    __func__, id);
854f02e3998SEd Maste 	}
85555215393SDag-Erling Smørgrav 	if (!strcmp(rtype, "eow@openssh.com")) {
856f02e3998SEd Maste 		if ((r = sshpkt_get_end(ssh)) != 0)
857f02e3998SEd Maste 			sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
85820adc8f2SDag-Erling Smørgrav 		chan_rcvd_eow(ssh, c);
85955215393SDag-Erling Smørgrav 	} else if ((c->type == SSH_CHANNEL_LARVAL ||
86055215393SDag-Erling Smørgrav 	    c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0)
86120adc8f2SDag-Erling Smørgrav 		success = session_input_channel_req(ssh, c, rtype);
862f02e3998SEd Maste 	if (want_reply && !(c->flags & CHAN_CLOSE_SENT)) {
86320adc8f2SDag-Erling Smørgrav 		if (!c->have_remote_id)
864206be79aSEd Maste 			fatal_f("channel %d: no remote_id", c->self);
865f02e3998SEd Maste 		if ((r = sshpkt_start(ssh, success ?
866f02e3998SEd Maste 		    SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE)) != 0 ||
867f02e3998SEd Maste 		    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
868f02e3998SEd Maste 		    (r = sshpkt_send(ssh)) != 0)
869f02e3998SEd Maste 			sshpkt_fatal(ssh, r, "%s: send reply", __func__);
870ae1f160dSDag-Erling Smørgrav 	}
8710dddc34cSDag-Erling Smørgrav 	free(rtype);
872c1e08615SDag-Erling Smørgrav 	return 0;
873ae1f160dSDag-Erling Smørgrav }
874ae1f160dSDag-Erling Smørgrav 
875ae1f160dSDag-Erling Smørgrav static void
server_init_dispatch(struct ssh * ssh)876f02e3998SEd Maste server_init_dispatch(struct ssh *ssh)
877a04a10f8SKris Kennaway {
87819ca8551SDag-Erling Smørgrav 	debug("server_init_dispatch");
879f02e3998SEd Maste 	ssh_dispatch_init(ssh, &dispatch_protocol_error);
880f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
881f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_DATA, &channel_input_data);
882f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
883f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
884f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
885f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
886f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
887f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req);
888f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
889f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request);
8901e8db6e2SBrian Feldman 	/* client_alive */
891f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive);
892f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive);
893f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive);
894f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive);
8951e8db6e2SBrian Feldman 	/* rekeying */
896f02e3998SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
897a04a10f8SKris Kennaway }
898