xref: /dflybsd-src/crypto/openssh/serverloop.c (revision 50a69bb51183a7916e776f2c9f5fa64c999f1a2f)
1*50a69bb5SSascha Wildner /* $OpenBSD: serverloop.c,v 1.228 2021/07/16 09:00:23 djm Exp $ */
218de8d7fSPeter Avalos /*
318de8d7fSPeter Avalos  * Author: Tatu Ylonen <ylo@cs.hut.fi>
418de8d7fSPeter Avalos  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
518de8d7fSPeter Avalos  *                    All rights reserved
618de8d7fSPeter Avalos  * Server main loop for handling the interactive session.
718de8d7fSPeter Avalos  *
818de8d7fSPeter Avalos  * As far as I am concerned, the code I have written for this software
918de8d7fSPeter Avalos  * can be used freely for any purpose.  Any derived versions of this
1018de8d7fSPeter Avalos  * software must be clearly marked as such, and if the derived work is
1118de8d7fSPeter Avalos  * incompatible with the protocol description in the RFC file, it must be
1218de8d7fSPeter Avalos  * called by a name other than "ssh" or "Secure Shell".
1318de8d7fSPeter Avalos  *
1418de8d7fSPeter Avalos  * SSH2 support by Markus Friedl.
1518de8d7fSPeter Avalos  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
1618de8d7fSPeter Avalos  *
1718de8d7fSPeter Avalos  * Redistribution and use in source and binary forms, with or without
1818de8d7fSPeter Avalos  * modification, are permitted provided that the following conditions
1918de8d7fSPeter Avalos  * are met:
2018de8d7fSPeter Avalos  * 1. Redistributions of source code must retain the above copyright
2118de8d7fSPeter Avalos  *    notice, this list of conditions and the following disclaimer.
2218de8d7fSPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
2318de8d7fSPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
2418de8d7fSPeter Avalos  *    documentation and/or other materials provided with the distribution.
2518de8d7fSPeter Avalos  *
2618de8d7fSPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2718de8d7fSPeter Avalos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2818de8d7fSPeter Avalos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2918de8d7fSPeter Avalos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
3018de8d7fSPeter Avalos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
3118de8d7fSPeter Avalos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3218de8d7fSPeter Avalos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3318de8d7fSPeter Avalos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3418de8d7fSPeter Avalos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3518de8d7fSPeter Avalos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3618de8d7fSPeter Avalos  */
3718de8d7fSPeter Avalos 
3818de8d7fSPeter Avalos #include "includes.h"
3918de8d7fSPeter Avalos 
4018de8d7fSPeter Avalos #include <sys/types.h>
4118de8d7fSPeter Avalos #include <sys/wait.h>
4218de8d7fSPeter Avalos #include <sys/socket.h>
4318de8d7fSPeter Avalos #ifdef HAVE_SYS_TIME_H
4418de8d7fSPeter Avalos # include <sys/time.h>
4518de8d7fSPeter Avalos #endif
4618de8d7fSPeter Avalos 
4718de8d7fSPeter Avalos #include <netinet/in.h>
4818de8d7fSPeter Avalos 
4918de8d7fSPeter Avalos #include <errno.h>
5018de8d7fSPeter Avalos #include <fcntl.h>
5118de8d7fSPeter Avalos #include <pwd.h>
52664f4763Szrj #include <limits.h>
5318de8d7fSPeter Avalos #include <signal.h>
5418de8d7fSPeter Avalos #include <string.h>
5518de8d7fSPeter Avalos #include <termios.h>
5618de8d7fSPeter Avalos #include <unistd.h>
5718de8d7fSPeter Avalos #include <stdarg.h>
5818de8d7fSPeter Avalos 
5918de8d7fSPeter Avalos #include "openbsd-compat/sys-queue.h"
6018de8d7fSPeter Avalos #include "xmalloc.h"
6118de8d7fSPeter Avalos #include "packet.h"
62664f4763Szrj #include "sshbuf.h"
6318de8d7fSPeter Avalos #include "log.h"
6436e94dc5SPeter Avalos #include "misc.h"
6518de8d7fSPeter Avalos #include "servconf.h"
6618de8d7fSPeter Avalos #include "canohost.h"
6718de8d7fSPeter Avalos #include "sshpty.h"
6818de8d7fSPeter Avalos #include "channels.h"
6918de8d7fSPeter Avalos #include "compat.h"
7018de8d7fSPeter Avalos #include "ssh2.h"
71664f4763Szrj #include "sshkey.h"
7218de8d7fSPeter Avalos #include "cipher.h"
7318de8d7fSPeter Avalos #include "kex.h"
7418de8d7fSPeter Avalos #include "hostfile.h"
7518de8d7fSPeter Avalos #include "auth.h"
7618de8d7fSPeter Avalos #include "session.h"
7718de8d7fSPeter Avalos #include "dispatch.h"
7818de8d7fSPeter Avalos #include "auth-options.h"
7918de8d7fSPeter Avalos #include "serverloop.h"
80e9778795SPeter Avalos #include "ssherr.h"
8118de8d7fSPeter Avalos 
8218de8d7fSPeter Avalos extern ServerOptions options;
8318de8d7fSPeter Avalos 
8418de8d7fSPeter Avalos /* XXX */
8518de8d7fSPeter Avalos extern Authctxt *the_authctxt;
86664f4763Szrj extern struct sshauthopt *auth_opts;
8718de8d7fSPeter Avalos extern int use_privsep;
8818de8d7fSPeter Avalos 
8918de8d7fSPeter Avalos static int no_more_sessions = 0; /* Disallow further sessions. */
9018de8d7fSPeter Avalos 
9118de8d7fSPeter Avalos static volatile sig_atomic_t child_terminated = 0;	/* The child has terminated. */
9218de8d7fSPeter Avalos 
9318de8d7fSPeter Avalos /* Cleanup on signals (!use_privsep case only) */
9418de8d7fSPeter Avalos static volatile sig_atomic_t received_sigterm = 0;
9518de8d7fSPeter Avalos 
9618de8d7fSPeter Avalos /* prototypes */
97664f4763Szrj static void server_init_dispatch(struct ssh *);
98664f4763Szrj 
99664f4763Szrj /* requested tunnel forwarding interface(s), shared with session.c */
100664f4763Szrj char *tun_fwd_ifnames = NULL;
101664f4763Szrj 
102664f4763Szrj /* returns 1 if bind to specified port by specified user is permitted */
103664f4763Szrj static int
104664f4763Szrj bind_permitted(int port, uid_t uid)
105664f4763Szrj {
106664f4763Szrj 	if (use_privsep)
107664f4763Szrj 		return 1; /* allow system to decide */
108664f4763Szrj 	if (port < IPPORT_RESERVED && uid != 0)
109664f4763Szrj 		return 0;
110664f4763Szrj 	return 1;
111664f4763Szrj }
11218de8d7fSPeter Avalos 
11318de8d7fSPeter Avalos /*ARGSUSED*/
11418de8d7fSPeter Avalos static void
11518de8d7fSPeter Avalos sigchld_handler(int sig)
11618de8d7fSPeter Avalos {
11718de8d7fSPeter Avalos 	child_terminated = 1;
11818de8d7fSPeter Avalos }
11918de8d7fSPeter Avalos 
12018de8d7fSPeter Avalos /*ARGSUSED*/
12118de8d7fSPeter Avalos static void
12218de8d7fSPeter Avalos sigterm_handler(int sig)
12318de8d7fSPeter Avalos {
12418de8d7fSPeter Avalos 	received_sigterm = sig;
12518de8d7fSPeter Avalos }
12618de8d7fSPeter Avalos 
12718de8d7fSPeter Avalos static void
128ce74bacaSMatthew Dillon client_alive_check(struct ssh *ssh)
12918de8d7fSPeter Avalos {
130664f4763Szrj 	char remote_id[512];
131664f4763Szrj 	int r, channel_id;
13218de8d7fSPeter Avalos 
13318de8d7fSPeter Avalos 	/* timeout, check to see how many we have had */
1340cbfa66cSDaniel Fojt 	if (options.client_alive_count_max > 0 &&
1350cbfa66cSDaniel Fojt 	    ssh_packet_inc_alive_timeouts(ssh) >
136664f4763Szrj 	    options.client_alive_count_max) {
137664f4763Szrj 		sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
138664f4763Szrj 		logit("Timeout, client not responding from %s", remote_id);
13918de8d7fSPeter Avalos 		cleanup_exit(255);
14018de8d7fSPeter Avalos 	}
14118de8d7fSPeter Avalos 
14218de8d7fSPeter Avalos 	/*
14318de8d7fSPeter Avalos 	 * send a bogus global/channel request with "wantreply",
14418de8d7fSPeter Avalos 	 * we should get back a failure
14518de8d7fSPeter Avalos 	 */
146ce74bacaSMatthew Dillon 	if ((channel_id = channel_find_open(ssh)) == -1) {
147664f4763Szrj 		if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
148664f4763Szrj 		    (r = sshpkt_put_cstring(ssh, "keepalive@openssh.com"))
149664f4763Szrj 		    != 0 ||
150664f4763Szrj 		    (r = sshpkt_put_u8(ssh, 1)) != 0) /* boolean: want reply */
151*50a69bb5SSascha Wildner 			fatal_fr(r, "compose");
15218de8d7fSPeter Avalos 	} else {
153ce74bacaSMatthew Dillon 		channel_request_start(ssh, channel_id,
154ce74bacaSMatthew Dillon 		    "keepalive@openssh.com", 1);
15518de8d7fSPeter Avalos 	}
156664f4763Szrj 	if ((r = sshpkt_send(ssh)) != 0)
157*50a69bb5SSascha Wildner 		fatal_fr(r, "send");
15818de8d7fSPeter Avalos }
15918de8d7fSPeter Avalos 
16018de8d7fSPeter Avalos /*
161*50a69bb5SSascha Wildner  * Sleep in pselect() until we can do something.  This will initialize the
162*50a69bb5SSascha Wildner  * pselect masks.  Upon return, the masks will indicate which descriptors
16318de8d7fSPeter Avalos  * have data or can accept data.  Optionally, a maximum time can be specified
16418de8d7fSPeter Avalos  * for the duration of the wait (0 = infinite).
16518de8d7fSPeter Avalos  */
16618de8d7fSPeter Avalos static void
167ce74bacaSMatthew Dillon wait_until_can_do_something(struct ssh *ssh,
168ce74bacaSMatthew Dillon     int connection_in, int connection_out,
169ce74bacaSMatthew Dillon     fd_set **readsetp, fd_set **writesetp, int *maxfdp,
170*50a69bb5SSascha Wildner     u_int *nallocp, u_int64_t max_time_ms, sigset_t *sigsetp)
17118de8d7fSPeter Avalos {
172*50a69bb5SSascha Wildner 	struct timespec ts, *tsp;
17318de8d7fSPeter Avalos 	int ret;
17499e85e0dSPeter Avalos 	time_t minwait_secs = 0;
17518de8d7fSPeter Avalos 	int client_alive_scheduled = 0;
176664f4763Szrj 	/* time we last heard from the client OR sent a keepalive */
177ce74bacaSMatthew Dillon 	static time_t last_client_time;
17818de8d7fSPeter Avalos 
179*50a69bb5SSascha Wildner 	/* Allocate and update pselect() masks for channel descriptors. */
180ce74bacaSMatthew Dillon 	channel_prepare_select(ssh, readsetp, writesetp, maxfdp,
181ce74bacaSMatthew Dillon 	    nallocp, &minwait_secs);
18299e85e0dSPeter Avalos 
183e9778795SPeter Avalos 	/* XXX need proper deadline system for rekey/client alive */
18499e85e0dSPeter Avalos 	if (minwait_secs != 0)
185ce74bacaSMatthew Dillon 		max_time_ms = MINIMUM(max_time_ms, (u_int)minwait_secs * 1000);
18699e85e0dSPeter Avalos 
18718de8d7fSPeter Avalos 	/*
18818de8d7fSPeter Avalos 	 * if using client_alive, set the max timeout accordingly,
18918de8d7fSPeter Avalos 	 * and indicate that this particular timeout was for client
19018de8d7fSPeter Avalos 	 * alive by setting the client_alive_scheduled flag.
19118de8d7fSPeter Avalos 	 *
19218de8d7fSPeter Avalos 	 * this could be randomized somewhat to make traffic
19318de8d7fSPeter Avalos 	 * analysis more difficult, but we're not doing it yet.
19418de8d7fSPeter Avalos 	 */
195ce74bacaSMatthew Dillon 	if (options.client_alive_interval) {
196e9778795SPeter Avalos 		uint64_t keepalive_ms =
197e9778795SPeter Avalos 		    (uint64_t)options.client_alive_interval * 1000;
198e9778795SPeter Avalos 
199664f4763Szrj 		if (max_time_ms == 0 || max_time_ms > keepalive_ms) {
200e9778795SPeter Avalos 			max_time_ms = keepalive_ms;
201664f4763Szrj 			client_alive_scheduled = 1;
202664f4763Szrj 		}
203*50a69bb5SSascha Wildner 		if (last_client_time == 0)
204*50a69bb5SSascha Wildner 			last_client_time = monotime();
20518de8d7fSPeter Avalos 	}
20618de8d7fSPeter Avalos 
20718de8d7fSPeter Avalos #if 0
20818de8d7fSPeter Avalos 	/* wrong: bad condition XXX */
20918de8d7fSPeter Avalos 	if (channel_not_very_much_buffered_data())
21018de8d7fSPeter Avalos #endif
21118de8d7fSPeter Avalos 	FD_SET(connection_in, *readsetp);
21218de8d7fSPeter Avalos 
21318de8d7fSPeter Avalos 	/*
21418de8d7fSPeter Avalos 	 * If we have buffered packet data going to the client, mark that
21518de8d7fSPeter Avalos 	 * descriptor.
21618de8d7fSPeter Avalos 	 */
217664f4763Szrj 	if (ssh_packet_have_data_to_write(ssh))
21818de8d7fSPeter Avalos 		FD_SET(connection_out, *writesetp);
21918de8d7fSPeter Avalos 
22018de8d7fSPeter Avalos 	/*
22118de8d7fSPeter Avalos 	 * If child has terminated and there is enough buffer space to read
22218de8d7fSPeter Avalos 	 * from it, then read as much as is available and exit.
22318de8d7fSPeter Avalos 	 */
224664f4763Szrj 	if (child_terminated && ssh_packet_not_very_much_data_to_write(ssh))
225e9778795SPeter Avalos 		if (max_time_ms == 0 || client_alive_scheduled)
226e9778795SPeter Avalos 			max_time_ms = 100;
22718de8d7fSPeter Avalos 
228e9778795SPeter Avalos 	if (max_time_ms == 0)
229*50a69bb5SSascha Wildner 		tsp = NULL;
23018de8d7fSPeter Avalos 	else {
231*50a69bb5SSascha Wildner 		ts.tv_sec = max_time_ms / 1000;
232*50a69bb5SSascha Wildner 		ts.tv_nsec = 1000000 * (max_time_ms % 1000);
233*50a69bb5SSascha Wildner 		tsp = &ts;
23418de8d7fSPeter Avalos 	}
23518de8d7fSPeter Avalos 
23618de8d7fSPeter Avalos 	/* Wait for something to happen, or the timeout to expire. */
237*50a69bb5SSascha Wildner 	ret = pselect((*maxfdp)+1, *readsetp, *writesetp, NULL, tsp, sigsetp);
23818de8d7fSPeter Avalos 
23918de8d7fSPeter Avalos 	if (ret == -1) {
24018de8d7fSPeter Avalos 		memset(*readsetp, 0, *nallocp);
24118de8d7fSPeter Avalos 		memset(*writesetp, 0, *nallocp);
24218de8d7fSPeter Avalos 		if (errno != EINTR)
243*50a69bb5SSascha Wildner 			error("pselect: %.100s", strerror(errno));
244ce74bacaSMatthew Dillon 	} else if (client_alive_scheduled) {
245ce74bacaSMatthew Dillon 		time_t now = monotime();
246ce74bacaSMatthew Dillon 
247664f4763Szrj 		/*
248*50a69bb5SSascha Wildner 		 * If the pselect timed out, or returned for some other reason
249664f4763Szrj 		 * but we haven't heard from the client in time, send keepalive.
250664f4763Szrj 		 */
251664f4763Szrj 		if (ret == 0 || (last_client_time != 0 && last_client_time +
252664f4763Szrj 		    options.client_alive_interval <= now)) {
253ce74bacaSMatthew Dillon 			client_alive_check(ssh);
254ce74bacaSMatthew Dillon 			last_client_time = now;
255664f4763Szrj 		} else if (FD_ISSET(connection_in, *readsetp)) {
256ce74bacaSMatthew Dillon 			last_client_time = now;
25718de8d7fSPeter Avalos 		}
25818de8d7fSPeter Avalos 	}
25918de8d7fSPeter Avalos }
26018de8d7fSPeter Avalos 
26118de8d7fSPeter Avalos /*
26218de8d7fSPeter Avalos  * Processes input from the client and the program.  Input data is stored
26318de8d7fSPeter Avalos  * in buffers and processed later.
26418de8d7fSPeter Avalos  */
265ce74bacaSMatthew Dillon static int
266ce74bacaSMatthew Dillon process_input(struct ssh *ssh, fd_set *readset, int connection_in)
26718de8d7fSPeter Avalos {
268664f4763Szrj 	int r, len;
26918de8d7fSPeter Avalos 	char buf[16384];
27018de8d7fSPeter Avalos 
27118de8d7fSPeter Avalos 	/* Read and buffer any input data from the client. */
27218de8d7fSPeter Avalos 	if (FD_ISSET(connection_in, readset)) {
273e9778795SPeter Avalos 		len = read(connection_in, buf, sizeof(buf));
27418de8d7fSPeter Avalos 		if (len == 0) {
275e9778795SPeter Avalos 			verbose("Connection closed by %.100s port %d",
276e9778795SPeter Avalos 			    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
277ce74bacaSMatthew Dillon 			return -1;
2780cbfa66cSDaniel Fojt 		} else if (len == -1) {
279*50a69bb5SSascha Wildner 			if (errno == EINTR || errno == EAGAIN ||
280*50a69bb5SSascha Wildner 			    errno == EWOULDBLOCK)
281*50a69bb5SSascha Wildner 				return 0;
282*50a69bb5SSascha Wildner 			verbose("Read error from remote host %s port %d: %s",
283*50a69bb5SSascha Wildner 			    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
284*50a69bb5SSascha Wildner 			    strerror(errno));
28518de8d7fSPeter Avalos 			cleanup_exit(255);
28618de8d7fSPeter Avalos 		}
28718de8d7fSPeter Avalos 		/* Buffer any received data. */
288*50a69bb5SSascha Wildner 		if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
289*50a69bb5SSascha Wildner 			fatal_fr(r, "ssh_packet_process_incoming");
29018de8d7fSPeter Avalos 	}
291ce74bacaSMatthew Dillon 	return 0;
29218de8d7fSPeter Avalos }
29318de8d7fSPeter Avalos 
29418de8d7fSPeter Avalos /*
29518de8d7fSPeter Avalos  * Sends data from internal buffers to client program stdin.
29618de8d7fSPeter Avalos  */
29718de8d7fSPeter Avalos static void
298664f4763Szrj process_output(struct ssh *ssh, fd_set *writeset, int connection_out)
29918de8d7fSPeter Avalos {
300664f4763Szrj 	int r;
301664f4763Szrj 
30218de8d7fSPeter Avalos 	/* Send any buffered packet data to the client. */
303664f4763Szrj 	if (FD_ISSET(connection_out, writeset)) {
3040cbfa66cSDaniel Fojt 		if ((r = ssh_packet_write_poll(ssh)) != 0) {
3050cbfa66cSDaniel Fojt 			sshpkt_fatal(ssh, r, "%s: ssh_packet_write_poll",
3060cbfa66cSDaniel Fojt 			    __func__);
3070cbfa66cSDaniel Fojt 		}
308664f4763Szrj 	}
30918de8d7fSPeter Avalos }
31018de8d7fSPeter Avalos 
31118de8d7fSPeter Avalos static void
312ce74bacaSMatthew Dillon process_buffered_input_packets(struct ssh *ssh)
31318de8d7fSPeter Avalos {
314ce74bacaSMatthew Dillon 	ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, NULL);
31518de8d7fSPeter Avalos }
31618de8d7fSPeter Avalos 
31718de8d7fSPeter Avalos static void
318ce74bacaSMatthew Dillon collect_children(struct ssh *ssh)
31918de8d7fSPeter Avalos {
32018de8d7fSPeter Avalos 	pid_t pid;
32118de8d7fSPeter Avalos 	int status;
32218de8d7fSPeter Avalos 
32318de8d7fSPeter Avalos 	if (child_terminated) {
32418de8d7fSPeter Avalos 		debug("Received SIGCHLD.");
32518de8d7fSPeter Avalos 		while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
3260cbfa66cSDaniel Fojt 		    (pid == -1 && errno == EINTR))
32718de8d7fSPeter Avalos 			if (pid > 0)
328ce74bacaSMatthew Dillon 				session_close_by_pid(ssh, pid, status);
32918de8d7fSPeter Avalos 		child_terminated = 0;
33018de8d7fSPeter Avalos 	}
33118de8d7fSPeter Avalos }
33218de8d7fSPeter Avalos 
33318de8d7fSPeter Avalos void
334ce74bacaSMatthew Dillon server_loop2(struct ssh *ssh, Authctxt *authctxt)
33518de8d7fSPeter Avalos {
33618de8d7fSPeter Avalos 	fd_set *readset = NULL, *writeset = NULL;
337*50a69bb5SSascha Wildner 	int r, max_fd;
338ce74bacaSMatthew Dillon 	u_int nalloc = 0, connection_in, connection_out;
33936e94dc5SPeter Avalos 	u_int64_t rekey_timeout_ms = 0;
340*50a69bb5SSascha Wildner 	sigset_t bsigset, osigset;
34118de8d7fSPeter Avalos 
34218de8d7fSPeter Avalos 	debug("Entering interactive session for SSH2.");
34318de8d7fSPeter Avalos 
344*50a69bb5SSascha Wildner 	if (sigemptyset(&bsigset) == -1 || sigaddset(&bsigset, SIGCHLD) == -1)
345*50a69bb5SSascha Wildner 		error_f("bsigset setup: %s", strerror(errno));
3460cbfa66cSDaniel Fojt 	ssh_signal(SIGCHLD, sigchld_handler);
34718de8d7fSPeter Avalos 	child_terminated = 0;
348664f4763Szrj 	connection_in = ssh_packet_get_connection_in(ssh);
349664f4763Szrj 	connection_out = ssh_packet_get_connection_out(ssh);
35018de8d7fSPeter Avalos 
35118de8d7fSPeter Avalos 	if (!use_privsep) {
3520cbfa66cSDaniel Fojt 		ssh_signal(SIGTERM, sigterm_handler);
3530cbfa66cSDaniel Fojt 		ssh_signal(SIGINT, sigterm_handler);
3540cbfa66cSDaniel Fojt 		ssh_signal(SIGQUIT, sigterm_handler);
35518de8d7fSPeter Avalos 	}
35618de8d7fSPeter Avalos 
357ce74bacaSMatthew Dillon 	max_fd = MAXIMUM(connection_in, connection_out);
35818de8d7fSPeter Avalos 
359664f4763Szrj 	server_init_dispatch(ssh);
36018de8d7fSPeter Avalos 
36118de8d7fSPeter Avalos 	for (;;) {
362ce74bacaSMatthew Dillon 		process_buffered_input_packets(ssh);
36318de8d7fSPeter Avalos 
364ce74bacaSMatthew Dillon 		if (!ssh_packet_is_rekeying(ssh) &&
365664f4763Szrj 		    ssh_packet_not_very_much_data_to_write(ssh))
366ce74bacaSMatthew Dillon 			channel_output_poll(ssh);
367664f4763Szrj 		if (options.rekey_interval > 0 &&
368664f4763Szrj 		    !ssh_packet_is_rekeying(ssh)) {
369664f4763Szrj 			rekey_timeout_ms = ssh_packet_get_rekey_timeout(ssh) *
370664f4763Szrj 			    1000;
371664f4763Szrj 		} else {
37236e94dc5SPeter Avalos 			rekey_timeout_ms = 0;
373664f4763Szrj 		}
37436e94dc5SPeter Avalos 
375*50a69bb5SSascha Wildner 		/*
376*50a69bb5SSascha Wildner 		 * Block SIGCHLD while we check for dead children, then pass
377*50a69bb5SSascha Wildner 		 * the old signal mask through to pselect() so that it'll wake
378*50a69bb5SSascha Wildner 		 * up immediately if a child exits after we've called waitpid().
379*50a69bb5SSascha Wildner 		 */
380*50a69bb5SSascha Wildner 		if (sigprocmask(SIG_BLOCK, &bsigset, &osigset) == -1)
381*50a69bb5SSascha Wildner 			error_f("bsigset sigprocmask: %s", strerror(errno));
382*50a69bb5SSascha Wildner 		collect_children(ssh);
383ce74bacaSMatthew Dillon 		wait_until_can_do_something(ssh, connection_in, connection_out,
384*50a69bb5SSascha Wildner 		    &readset, &writeset, &max_fd, &nalloc, rekey_timeout_ms,
385*50a69bb5SSascha Wildner 		    &osigset);
386*50a69bb5SSascha Wildner 		if (sigprocmask(SIG_UNBLOCK, &bsigset, &osigset) == -1)
387*50a69bb5SSascha Wildner 			error_f("osigset sigprocmask: %s", strerror(errno));
38818de8d7fSPeter Avalos 
38918de8d7fSPeter Avalos 		if (received_sigterm) {
39036e94dc5SPeter Avalos 			logit("Exiting on signal %d", (int)received_sigterm);
39118de8d7fSPeter Avalos 			/* Clean up sessions, utmp, etc. */
39218de8d7fSPeter Avalos 			cleanup_exit(255);
39318de8d7fSPeter Avalos 		}
39418de8d7fSPeter Avalos 
395ce74bacaSMatthew Dillon 		if (!ssh_packet_is_rekeying(ssh))
396ce74bacaSMatthew Dillon 			channel_after_select(ssh, readset, writeset);
397ce74bacaSMatthew Dillon 		if (process_input(ssh, readset, connection_in) < 0)
39818de8d7fSPeter Avalos 			break;
399*50a69bb5SSascha Wildner 		/* A timeout may have triggered rekeying */
400*50a69bb5SSascha Wildner 		if ((r = ssh_packet_check_rekey(ssh)) != 0)
401*50a69bb5SSascha Wildner 			fatal_fr(r, "cannot start rekeying");
402664f4763Szrj 		process_output(ssh, writeset, connection_out);
40318de8d7fSPeter Avalos 	}
404ce74bacaSMatthew Dillon 	collect_children(ssh);
40518de8d7fSPeter Avalos 
40636e94dc5SPeter Avalos 	free(readset);
40736e94dc5SPeter Avalos 	free(writeset);
40818de8d7fSPeter Avalos 
40918de8d7fSPeter Avalos 	/* free all channels, no more reads and writes */
410ce74bacaSMatthew Dillon 	channel_free_all(ssh);
41118de8d7fSPeter Avalos 
41218de8d7fSPeter Avalos 	/* free remaining sessions, e.g. remove wtmp entries */
413ce74bacaSMatthew Dillon 	session_destroy_all(ssh, NULL);
41418de8d7fSPeter Avalos }
41518de8d7fSPeter Avalos 
416e9778795SPeter Avalos static int
417ce74bacaSMatthew Dillon server_input_keep_alive(int type, u_int32_t seq, struct ssh *ssh)
41818de8d7fSPeter Avalos {
41918de8d7fSPeter Avalos 	debug("Got %d/%u for keepalive", type, seq);
42018de8d7fSPeter Avalos 	/*
42118de8d7fSPeter Avalos 	 * reset timeout, since we got a sane answer from the client.
42218de8d7fSPeter Avalos 	 * even if this was generated by something other than
42318de8d7fSPeter Avalos 	 * the bogus CHANNEL_REQUEST we send for keepalives.
42418de8d7fSPeter Avalos 	 */
425664f4763Szrj 	ssh_packet_set_alive_timeouts(ssh, 0);
426e9778795SPeter Avalos 	return 0;
42718de8d7fSPeter Avalos }
42818de8d7fSPeter Avalos 
42918de8d7fSPeter Avalos static Channel *
430ce74bacaSMatthew Dillon server_request_direct_tcpip(struct ssh *ssh, int *reason, const char **errmsg)
43118de8d7fSPeter Avalos {
43236e94dc5SPeter Avalos 	Channel *c = NULL;
433664f4763Szrj 	char *target = NULL, *originator = NULL;
434664f4763Szrj 	u_int target_port = 0, originator_port = 0;
435664f4763Szrj 	int r;
43618de8d7fSPeter Avalos 
437664f4763Szrj 	if ((r = sshpkt_get_cstring(ssh, &target, NULL)) != 0 ||
438664f4763Szrj 	    (r = sshpkt_get_u32(ssh, &target_port)) != 0 ||
439664f4763Szrj 	    (r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 ||
440664f4763Szrj 	    (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
441664f4763Szrj 	    (r = sshpkt_get_end(ssh)) != 0)
442664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
443664f4763Szrj 	if (target_port > 0xFFFF) {
444*50a69bb5SSascha Wildner 		error_f("invalid target port");
445664f4763Szrj 		*reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
446664f4763Szrj 		goto out;
447664f4763Szrj 	}
448664f4763Szrj 	if (originator_port > 0xFFFF) {
449*50a69bb5SSascha Wildner 		error_f("invalid originator port");
450664f4763Szrj 		*reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
451664f4763Szrj 		goto out;
452664f4763Szrj 	}
45318de8d7fSPeter Avalos 
454*50a69bb5SSascha Wildner 	debug_f("originator %s port %u, target %s port %u",
455664f4763Szrj 	    originator, originator_port, target, target_port);
45618de8d7fSPeter Avalos 
45736e94dc5SPeter Avalos 	/* XXX fine grained permissions */
45836e94dc5SPeter Avalos 	if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 &&
459664f4763Szrj 	    auth_opts->permit_port_forwarding_flag &&
460664f4763Szrj 	    !options.disable_forwarding) {
461ce74bacaSMatthew Dillon 		c = channel_connect_to_port(ssh, target, target_port,
462ce74bacaSMatthew Dillon 		    "direct-tcpip", "direct-tcpip", reason, errmsg);
46336e94dc5SPeter Avalos 	} else {
46436e94dc5SPeter Avalos 		logit("refused local port forward: "
46536e94dc5SPeter Avalos 		    "originator %s port %d, target %s port %d",
46636e94dc5SPeter Avalos 		    originator, originator_port, target, target_port);
467ce74bacaSMatthew Dillon 		if (reason != NULL)
468ce74bacaSMatthew Dillon 			*reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
46936e94dc5SPeter Avalos 	}
47018de8d7fSPeter Avalos 
471664f4763Szrj  out:
47236e94dc5SPeter Avalos 	free(originator);
47336e94dc5SPeter Avalos 	free(target);
47436e94dc5SPeter Avalos 	return c;
47536e94dc5SPeter Avalos }
47636e94dc5SPeter Avalos 
47736e94dc5SPeter Avalos static Channel *
478ce74bacaSMatthew Dillon server_request_direct_streamlocal(struct ssh *ssh)
47936e94dc5SPeter Avalos {
48036e94dc5SPeter Avalos 	Channel *c = NULL;
481664f4763Szrj 	char *target = NULL, *originator = NULL;
482664f4763Szrj 	u_int originator_port = 0;
483ce74bacaSMatthew Dillon 	struct passwd *pw = the_authctxt->pw;
484664f4763Szrj 	int r;
485ce74bacaSMatthew Dillon 
486ce74bacaSMatthew Dillon 	if (pw == NULL || !the_authctxt->valid)
487*50a69bb5SSascha Wildner 		fatal_f("no/invalid user");
48836e94dc5SPeter Avalos 
489664f4763Szrj 	if ((r = sshpkt_get_cstring(ssh, &target, NULL)) != 0 ||
490664f4763Szrj 	    (r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 ||
491664f4763Szrj 	    (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
492664f4763Szrj 	    (r = sshpkt_get_end(ssh)) != 0)
493664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
494664f4763Szrj 	if (originator_port > 0xFFFF) {
495*50a69bb5SSascha Wildner 		error_f("invalid originator port");
496664f4763Szrj 		goto out;
497664f4763Szrj 	}
49836e94dc5SPeter Avalos 
499*50a69bb5SSascha Wildner 	debug_f("originator %s port %d, target %s",
50036e94dc5SPeter Avalos 	    originator, originator_port, target);
50136e94dc5SPeter Avalos 
50236e94dc5SPeter Avalos 	/* XXX fine grained permissions */
50336e94dc5SPeter Avalos 	if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 &&
504664f4763Szrj 	    auth_opts->permit_port_forwarding_flag &&
505664f4763Szrj 	    !options.disable_forwarding && (pw->pw_uid == 0 || use_privsep)) {
506ce74bacaSMatthew Dillon 		c = channel_connect_to_path(ssh, target,
50736e94dc5SPeter Avalos 		    "direct-streamlocal@openssh.com", "direct-streamlocal");
50836e94dc5SPeter Avalos 	} else {
50936e94dc5SPeter Avalos 		logit("refused streamlocal port forward: "
51036e94dc5SPeter Avalos 		    "originator %s port %d, target %s",
51136e94dc5SPeter Avalos 		    originator, originator_port, target);
51236e94dc5SPeter Avalos 	}
51336e94dc5SPeter Avalos 
514664f4763Szrj out:
51536e94dc5SPeter Avalos 	free(originator);
51636e94dc5SPeter Avalos 	free(target);
51718de8d7fSPeter Avalos 	return c;
51818de8d7fSPeter Avalos }
51918de8d7fSPeter Avalos 
52018de8d7fSPeter Avalos static Channel *
521ce74bacaSMatthew Dillon server_request_tun(struct ssh *ssh)
52218de8d7fSPeter Avalos {
52318de8d7fSPeter Avalos 	Channel *c = NULL;
524664f4763Szrj 	u_int mode, tun;
525664f4763Szrj 	int r, sock;
526664f4763Szrj 	char *tmp, *ifname = NULL;
52718de8d7fSPeter Avalos 
528664f4763Szrj 	if ((r = sshpkt_get_u32(ssh, &mode)) != 0)
529664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse mode", __func__);
53018de8d7fSPeter Avalos 	switch (mode) {
53118de8d7fSPeter Avalos 	case SSH_TUNMODE_POINTOPOINT:
53218de8d7fSPeter Avalos 	case SSH_TUNMODE_ETHERNET:
53318de8d7fSPeter Avalos 		break;
53418de8d7fSPeter Avalos 	default:
535664f4763Szrj 		ssh_packet_send_debug(ssh, "Unsupported tunnel device mode.");
53618de8d7fSPeter Avalos 		return NULL;
53718de8d7fSPeter Avalos 	}
53818de8d7fSPeter Avalos 	if ((options.permit_tun & mode) == 0) {
539664f4763Szrj 		ssh_packet_send_debug(ssh, "Server has rejected tunnel device "
54018de8d7fSPeter Avalos 		    "forwarding");
54118de8d7fSPeter Avalos 		return NULL;
54218de8d7fSPeter Avalos 	}
54318de8d7fSPeter Avalos 
544664f4763Szrj 	if ((r = sshpkt_get_u32(ssh, &tun)) != 0)
545664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse device", __func__);
546664f4763Szrj 	if (tun > INT_MAX) {
547*50a69bb5SSascha Wildner 		debug_f("invalid tun");
54818de8d7fSPeter Avalos 		goto done;
54918de8d7fSPeter Avalos 	}
550664f4763Szrj 	if (auth_opts->force_tun_device != -1) {
551664f4763Szrj 		if (tun != SSH_TUNID_ANY &&
552664f4763Szrj 		    auth_opts->force_tun_device != (int)tun)
553664f4763Szrj 			goto done;
554664f4763Szrj 		tun = auth_opts->force_tun_device;
555664f4763Szrj 	}
556664f4763Szrj 	sock = tun_open(tun, mode, &ifname);
55718de8d7fSPeter Avalos 	if (sock < 0)
55818de8d7fSPeter Avalos 		goto done;
559664f4763Szrj 	debug("Tunnel forwarding using interface %s", ifname);
560664f4763Szrj 
561ce74bacaSMatthew Dillon 	c = channel_new(ssh, "tun", SSH_CHANNEL_OPEN, sock, sock, -1,
56218de8d7fSPeter Avalos 	    CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
56318de8d7fSPeter Avalos 	c->datagram = 1;
56418de8d7fSPeter Avalos #if defined(SSH_TUN_FILTER)
56518de8d7fSPeter Avalos 	if (mode == SSH_TUNMODE_POINTOPOINT)
566ce74bacaSMatthew Dillon 		channel_register_filter(ssh, c->self, sys_tun_infilter,
56718de8d7fSPeter Avalos 		    sys_tun_outfilter, NULL, NULL);
56818de8d7fSPeter Avalos #endif
56918de8d7fSPeter Avalos 
570664f4763Szrj 	/*
571664f4763Szrj 	 * Update the list of names exposed to the session
572664f4763Szrj 	 * XXX remove these if the tunnels are closed (won't matter
573664f4763Szrj 	 * much if they are already in the environment though)
574664f4763Szrj 	 */
575664f4763Szrj 	tmp = tun_fwd_ifnames;
576664f4763Szrj 	xasprintf(&tun_fwd_ifnames, "%s%s%s",
577664f4763Szrj 	    tun_fwd_ifnames == NULL ? "" : tun_fwd_ifnames,
578664f4763Szrj 	    tun_fwd_ifnames == NULL ? "" : ",",
579664f4763Szrj 	    ifname);
580664f4763Szrj 	free(tmp);
581664f4763Szrj 	free(ifname);
582664f4763Szrj 
58318de8d7fSPeter Avalos  done:
58418de8d7fSPeter Avalos 	if (c == NULL)
585664f4763Szrj 		ssh_packet_send_debug(ssh, "Failed to open the tunnel device.");
58618de8d7fSPeter Avalos 	return c;
58718de8d7fSPeter Avalos }
58818de8d7fSPeter Avalos 
58918de8d7fSPeter Avalos static Channel *
590ce74bacaSMatthew Dillon server_request_session(struct ssh *ssh)
59118de8d7fSPeter Avalos {
59218de8d7fSPeter Avalos 	Channel *c;
593664f4763Szrj 	int r;
59418de8d7fSPeter Avalos 
59518de8d7fSPeter Avalos 	debug("input_session_request");
596664f4763Szrj 	if ((r = sshpkt_get_end(ssh)) != 0)
597664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
59818de8d7fSPeter Avalos 
59918de8d7fSPeter Avalos 	if (no_more_sessions) {
600664f4763Szrj 		ssh_packet_disconnect(ssh, "Possible attack: attempt to open a "
601664f4763Szrj 		    "session after additional sessions disabled");
60218de8d7fSPeter Avalos 	}
60318de8d7fSPeter Avalos 
60418de8d7fSPeter Avalos 	/*
60518de8d7fSPeter Avalos 	 * A server session has no fd to read or write until a
60618de8d7fSPeter Avalos 	 * CHANNEL_REQUEST for a shell is made, so we set the type to
60718de8d7fSPeter Avalos 	 * SSH_CHANNEL_LARVAL.  Additionally, a callback for handling all
60818de8d7fSPeter Avalos 	 * CHANNEL_REQUEST messages is registered.
60918de8d7fSPeter Avalos 	 */
610ce74bacaSMatthew Dillon 	c = channel_new(ssh, "session", SSH_CHANNEL_LARVAL,
61118de8d7fSPeter Avalos 	    -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT,
61218de8d7fSPeter Avalos 	    0, "server-session", 1);
61318de8d7fSPeter Avalos 	if (session_open(the_authctxt, c->self) != 1) {
61418de8d7fSPeter Avalos 		debug("session open failed, free channel %d", c->self);
615ce74bacaSMatthew Dillon 		channel_free(ssh, c);
61618de8d7fSPeter Avalos 		return NULL;
61718de8d7fSPeter Avalos 	}
618ce74bacaSMatthew Dillon 	channel_register_cleanup(ssh, c->self, session_close_by_channel, 0);
61918de8d7fSPeter Avalos 	return c;
62018de8d7fSPeter Avalos }
62118de8d7fSPeter Avalos 
622e9778795SPeter Avalos static int
623ce74bacaSMatthew Dillon server_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
62418de8d7fSPeter Avalos {
62518de8d7fSPeter Avalos 	Channel *c = NULL;
626664f4763Szrj 	char *ctype = NULL;
627ce74bacaSMatthew Dillon 	const char *errmsg = NULL;
628664f4763Szrj 	int r, reason = SSH2_OPEN_CONNECT_FAILED;
629664f4763Szrj 	u_int rchan = 0, rmaxpack = 0, rwindow = 0;
63018de8d7fSPeter Avalos 
631664f4763Szrj 	if ((r = sshpkt_get_cstring(ssh, &ctype, NULL)) != 0 ||
632664f4763Szrj 	    (r = sshpkt_get_u32(ssh, &rchan)) != 0 ||
633664f4763Szrj 	    (r = sshpkt_get_u32(ssh, &rwindow)) != 0 ||
634664f4763Szrj 	    (r = sshpkt_get_u32(ssh, &rmaxpack)) != 0)
635664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
636*50a69bb5SSascha Wildner 	debug_f("ctype %s rchan %u win %u max %u",
63718de8d7fSPeter Avalos 	    ctype, rchan, rwindow, rmaxpack);
63818de8d7fSPeter Avalos 
6390cbfa66cSDaniel Fojt 	if (strcmp(ctype, "session") == 0) {
640ce74bacaSMatthew Dillon 		c = server_request_session(ssh);
64118de8d7fSPeter Avalos 	} else if (strcmp(ctype, "direct-tcpip") == 0) {
642ce74bacaSMatthew Dillon 		c = server_request_direct_tcpip(ssh, &reason, &errmsg);
64336e94dc5SPeter Avalos 	} else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) {
644ce74bacaSMatthew Dillon 		c = server_request_direct_streamlocal(ssh);
64518de8d7fSPeter Avalos 	} else if (strcmp(ctype, "tun@openssh.com") == 0) {
646ce74bacaSMatthew Dillon 		c = server_request_tun(ssh);
64718de8d7fSPeter Avalos 	}
64818de8d7fSPeter Avalos 	if (c != NULL) {
649*50a69bb5SSascha Wildner 		debug_f("confirm %s", ctype);
6500cbfa66cSDaniel Fojt 		c->remote_id = rchan;
651ce74bacaSMatthew Dillon 		c->have_remote_id = 1;
65218de8d7fSPeter Avalos 		c->remote_window = rwindow;
65318de8d7fSPeter Avalos 		c->remote_maxpacket = rmaxpack;
65418de8d7fSPeter Avalos 		if (c->type != SSH_CHANNEL_CONNECTING) {
655664f4763Szrj 			if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
656664f4763Szrj 			    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
657664f4763Szrj 			    (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
658664f4763Szrj 			    (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
659664f4763Szrj 			    (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
660664f4763Szrj 			    (r = sshpkt_send(ssh)) != 0) {
661664f4763Szrj 				sshpkt_fatal(ssh, r,
662664f4763Szrj 				    "%s: send open confirm", __func__);
663664f4763Szrj 			}
66418de8d7fSPeter Avalos 		}
66518de8d7fSPeter Avalos 	} else {
666*50a69bb5SSascha Wildner 		debug_f("failure %s", ctype);
667664f4763Szrj 		if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
668664f4763Szrj 		    (r = sshpkt_put_u32(ssh, rchan)) != 0 ||
669664f4763Szrj 		    (r = sshpkt_put_u32(ssh, reason)) != 0 ||
670664f4763Szrj 		    (r = sshpkt_put_cstring(ssh, errmsg ? errmsg : "open failed")) != 0 ||
671664f4763Szrj 		    (r = sshpkt_put_cstring(ssh, "")) != 0 ||
672664f4763Szrj 		    (r = sshpkt_send(ssh)) != 0) {
673664f4763Szrj 			sshpkt_fatal(ssh, r,
674664f4763Szrj 			    "%s: send open failure", __func__);
67518de8d7fSPeter Avalos 		}
67618de8d7fSPeter Avalos 	}
67736e94dc5SPeter Avalos 	free(ctype);
678e9778795SPeter Avalos 	return 0;
67918de8d7fSPeter Avalos }
68018de8d7fSPeter Avalos 
681e9778795SPeter Avalos static int
682ce74bacaSMatthew Dillon server_input_hostkeys_prove(struct ssh *ssh, struct sshbuf **respp)
683e9778795SPeter Avalos {
684e9778795SPeter Avalos 	struct sshbuf *resp = NULL;
685e9778795SPeter Avalos 	struct sshbuf *sigbuf = NULL;
686e9778795SPeter Avalos 	struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL;
687664f4763Szrj 	int r, ndx, kexsigtype, use_kexsigtype, success = 0;
688e9778795SPeter Avalos 	const u_char *blob;
689e9778795SPeter Avalos 	u_char *sig = 0;
690e9778795SPeter Avalos 	size_t blen, slen;
691e9778795SPeter Avalos 
692e9778795SPeter Avalos 	if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL)
693*50a69bb5SSascha Wildner 		fatal_f("sshbuf_new");
694e9778795SPeter Avalos 
695664f4763Szrj 	kexsigtype = sshkey_type_plain(
696664f4763Szrj 	    sshkey_type_from_name(ssh->kex->hostkey_alg));
697e9778795SPeter Avalos 	while (ssh_packet_remaining(ssh) > 0) {
698e9778795SPeter Avalos 		sshkey_free(key);
699e9778795SPeter Avalos 		key = NULL;
700e9778795SPeter Avalos 		if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 ||
701e9778795SPeter Avalos 		    (r = sshkey_from_blob(blob, blen, &key)) != 0) {
702*50a69bb5SSascha Wildner 			error_fr(r, "parse key");
703e9778795SPeter Avalos 			goto out;
704e9778795SPeter Avalos 		}
705e9778795SPeter Avalos 		/*
706e9778795SPeter Avalos 		 * Better check that this is actually one of our hostkeys
707e9778795SPeter Avalos 		 * before attempting to sign anything with it.
708e9778795SPeter Avalos 		 */
709e9778795SPeter Avalos 		if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) {
710*50a69bb5SSascha Wildner 			error_f("unknown host %s key", sshkey_type(key));
711e9778795SPeter Avalos 			goto out;
712e9778795SPeter Avalos 		}
713e9778795SPeter Avalos 		/*
714e9778795SPeter Avalos 		 * XXX refactor: make kex->sign just use an index rather
715e9778795SPeter Avalos 		 * than passing in public and private keys
716e9778795SPeter Avalos 		 */
717e9778795SPeter Avalos 		if ((key_prv = get_hostkey_by_index(ndx)) == NULL &&
718e9778795SPeter Avalos 		    (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) {
719*50a69bb5SSascha Wildner 			error_f("can't retrieve hostkey %d", ndx);
720e9778795SPeter Avalos 			goto out;
721e9778795SPeter Avalos 		}
722e9778795SPeter Avalos 		sshbuf_reset(sigbuf);
723e9778795SPeter Avalos 		free(sig);
724e9778795SPeter Avalos 		sig = NULL;
725664f4763Szrj 		/*
726664f4763Szrj 		 * For RSA keys, prefer to use the signature type negotiated
727664f4763Szrj 		 * during KEX to the default (SHA1).
728664f4763Szrj 		 */
729664f4763Szrj 		use_kexsigtype = kexsigtype == KEY_RSA &&
730664f4763Szrj 		    sshkey_type_plain(key->type) == KEY_RSA;
731e9778795SPeter Avalos 		if ((r = sshbuf_put_cstring(sigbuf,
732e9778795SPeter Avalos 		    "hostkeys-prove-00@openssh.com")) != 0 ||
733*50a69bb5SSascha Wildner 		    (r = sshbuf_put_stringb(sigbuf,
734*50a69bb5SSascha Wildner 		    ssh->kex->session_id)) != 0 ||
735e9778795SPeter Avalos 		    (r = sshkey_puts(key, sigbuf)) != 0 ||
736664f4763Szrj 		    (r = ssh->kex->sign(ssh, key_prv, key_pub, &sig, &slen,
737664f4763Szrj 		    sshbuf_ptr(sigbuf), sshbuf_len(sigbuf),
738664f4763Szrj 		    use_kexsigtype ? ssh->kex->hostkey_alg : NULL)) != 0 ||
739e9778795SPeter Avalos 		    (r = sshbuf_put_string(resp, sig, slen)) != 0) {
740*50a69bb5SSascha Wildner 			error_fr(r, "assemble signature");
741e9778795SPeter Avalos 			goto out;
742e9778795SPeter Avalos 		}
743e9778795SPeter Avalos 	}
744e9778795SPeter Avalos 	/* Success */
745e9778795SPeter Avalos 	*respp = resp;
746e9778795SPeter Avalos 	resp = NULL; /* don't free it */
747e9778795SPeter Avalos 	success = 1;
748e9778795SPeter Avalos  out:
749e9778795SPeter Avalos 	free(sig);
750e9778795SPeter Avalos 	sshbuf_free(resp);
751e9778795SPeter Avalos 	sshbuf_free(sigbuf);
752e9778795SPeter Avalos 	sshkey_free(key);
753e9778795SPeter Avalos 	return success;
754e9778795SPeter Avalos }
755e9778795SPeter Avalos 
756e9778795SPeter Avalos static int
757ce74bacaSMatthew Dillon server_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
75818de8d7fSPeter Avalos {
759664f4763Szrj 	char *rtype = NULL;
760664f4763Szrj 	u_char want_reply = 0;
761e9778795SPeter Avalos 	int r, success = 0, allocated_listen_port = 0;
762664f4763Szrj 	u_int port = 0;
763e9778795SPeter Avalos 	struct sshbuf *resp = NULL;
764ce74bacaSMatthew Dillon 	struct passwd *pw = the_authctxt->pw;
76536e94dc5SPeter Avalos 	struct Forward fwd;
76618de8d7fSPeter Avalos 
76736e94dc5SPeter Avalos 	memset(&fwd, 0, sizeof(fwd));
768664f4763Szrj 	if (pw == NULL || !the_authctxt->valid)
769*50a69bb5SSascha Wildner 		fatal_f("no/invalid user");
77018de8d7fSPeter Avalos 
771664f4763Szrj 	if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
772664f4763Szrj 	    (r = sshpkt_get_u8(ssh, &want_reply)) != 0)
773664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
774*50a69bb5SSascha Wildner 	debug_f("rtype %s want_reply %d", rtype, want_reply);
775664f4763Szrj 
776664f4763Szrj 	/* -R style forwarding */
777664f4763Szrj 	if (strcmp(rtype, "tcpip-forward") == 0) {
778664f4763Szrj 		if ((r = sshpkt_get_cstring(ssh, &fwd.listen_host, NULL)) != 0 ||
779664f4763Szrj 		    (r = sshpkt_get_u32(ssh, &port)) != 0)
780664f4763Szrj 			sshpkt_fatal(ssh, r, "%s: parse tcpip-forward", __func__);
781*50a69bb5SSascha Wildner 		debug_f("tcpip-forward listen %s port %u",
782664f4763Szrj 		    fwd.listen_host, port);
783664f4763Szrj 		if (port <= INT_MAX)
784664f4763Szrj 			fwd.listen_port = (int)port;
78518de8d7fSPeter Avalos 		/* check permissions */
786664f4763Szrj 		if (port > INT_MAX ||
787664f4763Szrj 		    (options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 ||
788664f4763Szrj 		    !auth_opts->permit_port_forwarding_flag ||
789664f4763Szrj 		    options.disable_forwarding ||
790e9778795SPeter Avalos 		    (!want_reply && fwd.listen_port == 0) ||
791ce74bacaSMatthew Dillon 		    (fwd.listen_port != 0 &&
792ce74bacaSMatthew Dillon 		    !bind_permitted(fwd.listen_port, pw->pw_uid))) {
79318de8d7fSPeter Avalos 			success = 0;
794664f4763Szrj 			ssh_packet_send_debug(ssh, "Server has disabled port forwarding.");
79518de8d7fSPeter Avalos 		} else {
79618de8d7fSPeter Avalos 			/* Start listening on the port */
797ce74bacaSMatthew Dillon 			success = channel_setup_remote_fwd_listener(ssh, &fwd,
79836e94dc5SPeter Avalos 			    &allocated_listen_port, &options.fwd_opts);
79918de8d7fSPeter Avalos 		}
800e9778795SPeter Avalos 		if ((resp = sshbuf_new()) == NULL)
801*50a69bb5SSascha Wildner 			fatal_f("sshbuf_new");
802e9778795SPeter Avalos 		if (allocated_listen_port != 0 &&
803e9778795SPeter Avalos 		    (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0)
804*50a69bb5SSascha Wildner 			fatal_fr(r, "sshbuf_put_u32");
80518de8d7fSPeter Avalos 	} else if (strcmp(rtype, "cancel-tcpip-forward") == 0) {
806664f4763Szrj 		if ((r = sshpkt_get_cstring(ssh, &fwd.listen_host, NULL)) != 0 ||
807664f4763Szrj 		    (r = sshpkt_get_u32(ssh, &port)) != 0)
808664f4763Szrj 			sshpkt_fatal(ssh, r, "%s: parse cancel-tcpip-forward", __func__);
80918de8d7fSPeter Avalos 
810*50a69bb5SSascha Wildner 		debug_f("cancel-tcpip-forward addr %s port %d",
811664f4763Szrj 		    fwd.listen_host, port);
812664f4763Szrj 		if (port <= INT_MAX) {
813664f4763Szrj 			fwd.listen_port = (int)port;
814ce74bacaSMatthew Dillon 			success = channel_cancel_rport_listener(ssh, &fwd);
815664f4763Szrj 		}
81636e94dc5SPeter Avalos 	} else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) {
817664f4763Szrj 		if ((r = sshpkt_get_cstring(ssh, &fwd.listen_path, NULL)) != 0)
818664f4763Szrj 			sshpkt_fatal(ssh, r, "%s: parse streamlocal-forward@openssh.com", __func__);
819*50a69bb5SSascha Wildner 		debug_f("streamlocal-forward listen path %s",
82036e94dc5SPeter Avalos 		    fwd.listen_path);
82136e94dc5SPeter Avalos 
82236e94dc5SPeter Avalos 		/* check permissions */
82336e94dc5SPeter Avalos 		if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0
824664f4763Szrj 		    || !auth_opts->permit_port_forwarding_flag ||
825664f4763Szrj 		    options.disable_forwarding ||
826ce74bacaSMatthew Dillon 		    (pw->pw_uid != 0 && !use_privsep)) {
82736e94dc5SPeter Avalos 			success = 0;
828664f4763Szrj 			ssh_packet_send_debug(ssh, "Server has disabled "
829ce74bacaSMatthew Dillon 			    "streamlocal forwarding.");
83036e94dc5SPeter Avalos 		} else {
83136e94dc5SPeter Avalos 			/* Start listening on the socket */
832ce74bacaSMatthew Dillon 			success = channel_setup_remote_fwd_listener(ssh,
83336e94dc5SPeter Avalos 			    &fwd, NULL, &options.fwd_opts);
83436e94dc5SPeter Avalos 		}
83536e94dc5SPeter Avalos 	} else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) {
836664f4763Szrj 		if ((r = sshpkt_get_cstring(ssh, &fwd.listen_path, NULL)) != 0)
837664f4763Szrj 			sshpkt_fatal(ssh, r, "%s: parse cancel-streamlocal-forward@openssh.com", __func__);
838*50a69bb5SSascha Wildner 		debug_f("cancel-streamlocal-forward path %s",
83936e94dc5SPeter Avalos 		    fwd.listen_path);
84036e94dc5SPeter Avalos 
841ce74bacaSMatthew Dillon 		success = channel_cancel_rport_listener(ssh, &fwd);
84218de8d7fSPeter Avalos 	} else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) {
84318de8d7fSPeter Avalos 		no_more_sessions = 1;
84418de8d7fSPeter Avalos 		success = 1;
845e9778795SPeter Avalos 	} else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) {
846ce74bacaSMatthew Dillon 		success = server_input_hostkeys_prove(ssh, &resp);
84718de8d7fSPeter Avalos 	}
848664f4763Szrj 	/* XXX sshpkt_get_end() */
84918de8d7fSPeter Avalos 	if (want_reply) {
850664f4763Szrj 		if ((r = sshpkt_start(ssh, success ?
851664f4763Szrj 		    SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE)) != 0 ||
852664f4763Szrj 		    (success && resp != NULL && (r = sshpkt_putb(ssh, resp)) != 0) ||
853664f4763Szrj 		    (r = sshpkt_send(ssh)) != 0 ||
854664f4763Szrj 		    (r = ssh_packet_write_wait(ssh)) != 0)
855664f4763Szrj 			sshpkt_fatal(ssh, r, "%s: send reply", __func__);
85618de8d7fSPeter Avalos 	}
857664f4763Szrj 	free(fwd.listen_host);
858664f4763Szrj 	free(fwd.listen_path);
85936e94dc5SPeter Avalos 	free(rtype);
860e9778795SPeter Avalos 	sshbuf_free(resp);
861e9778795SPeter Avalos 	return 0;
86218de8d7fSPeter Avalos }
86318de8d7fSPeter Avalos 
864e9778795SPeter Avalos static int
865ce74bacaSMatthew Dillon server_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
86618de8d7fSPeter Avalos {
86718de8d7fSPeter Avalos 	Channel *c;
868664f4763Szrj 	int r, success = 0;
869664f4763Szrj 	char *rtype = NULL;
870664f4763Szrj 	u_char want_reply = 0;
871664f4763Szrj 	u_int id = 0;
87218de8d7fSPeter Avalos 
873664f4763Szrj 	if ((r = sshpkt_get_u32(ssh, &id)) != 0 ||
874664f4763Szrj 	    (r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
875664f4763Szrj 	    (r = sshpkt_get_u8(ssh, &want_reply)) != 0)
876664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
87718de8d7fSPeter Avalos 
878664f4763Szrj 	debug("server_input_channel_req: channel %u request %s reply %d",
879664f4763Szrj 	    id, rtype, want_reply);
88018de8d7fSPeter Avalos 
881664f4763Szrj 	if (id >= INT_MAX || (c = channel_lookup(ssh, (int)id)) == NULL) {
882664f4763Szrj 		ssh_packet_disconnect(ssh, "%s: unknown channel %d",
883664f4763Szrj 		    __func__, id);
884664f4763Szrj 	}
88518de8d7fSPeter Avalos 	if (!strcmp(rtype, "eow@openssh.com")) {
886664f4763Szrj 		if ((r = sshpkt_get_end(ssh)) != 0)
887664f4763Szrj 			sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
888ce74bacaSMatthew Dillon 		chan_rcvd_eow(ssh, c);
88918de8d7fSPeter Avalos 	} else if ((c->type == SSH_CHANNEL_LARVAL ||
89018de8d7fSPeter Avalos 	    c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0)
891ce74bacaSMatthew Dillon 		success = session_input_channel_req(ssh, c, rtype);
892664f4763Szrj 	if (want_reply && !(c->flags & CHAN_CLOSE_SENT)) {
893ce74bacaSMatthew Dillon 		if (!c->have_remote_id)
894*50a69bb5SSascha Wildner 			fatal_f("channel %d: no remote_id", c->self);
895664f4763Szrj 		if ((r = sshpkt_start(ssh, success ?
896664f4763Szrj 		    SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE)) != 0 ||
897664f4763Szrj 		    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
898664f4763Szrj 		    (r = sshpkt_send(ssh)) != 0)
899664f4763Szrj 			sshpkt_fatal(ssh, r, "%s: send reply", __func__);
90018de8d7fSPeter Avalos 	}
90136e94dc5SPeter Avalos 	free(rtype);
902e9778795SPeter Avalos 	return 0;
90318de8d7fSPeter Avalos }
90418de8d7fSPeter Avalos 
90518de8d7fSPeter Avalos static void
906664f4763Szrj server_init_dispatch(struct ssh *ssh)
90718de8d7fSPeter Avalos {
908ce74bacaSMatthew Dillon 	debug("server_init_dispatch");
909664f4763Szrj 	ssh_dispatch_init(ssh, &dispatch_protocol_error);
910664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
911664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_DATA, &channel_input_data);
912664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
913664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
914664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
915664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
916664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
917664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req);
918664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
919664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request);
92018de8d7fSPeter Avalos 	/* client_alive */
921664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive);
922664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive);
923664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive);
924664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive);
92518de8d7fSPeter Avalos 	/* rekeying */
926664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
92718de8d7fSPeter Avalos }
928