xref: /dflybsd-src/crypto/openssh/serverloop.c (revision ba1276acd1c8c22d225b1bcf370a14c878644f44)
1*ba1276acSMatthew Dillon /* $OpenBSD: serverloop.c,v 1.240 2024/06/17 08:28:31 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>
53ee116499SAntonio Huete Jimenez #ifdef HAVE_POLL_H
54ee116499SAntonio Huete Jimenez #include <poll.h>
55ee116499SAntonio Huete Jimenez #endif
5618de8d7fSPeter Avalos #include <signal.h>
5718de8d7fSPeter Avalos #include <string.h>
5818de8d7fSPeter Avalos #include <termios.h>
5918de8d7fSPeter Avalos #include <unistd.h>
6018de8d7fSPeter Avalos #include <stdarg.h>
6118de8d7fSPeter Avalos 
6218de8d7fSPeter Avalos #include "openbsd-compat/sys-queue.h"
6318de8d7fSPeter Avalos #include "xmalloc.h"
6418de8d7fSPeter Avalos #include "packet.h"
65664f4763Szrj #include "sshbuf.h"
6618de8d7fSPeter Avalos #include "log.h"
6736e94dc5SPeter Avalos #include "misc.h"
6818de8d7fSPeter Avalos #include "servconf.h"
6918de8d7fSPeter Avalos #include "canohost.h"
7018de8d7fSPeter Avalos #include "sshpty.h"
7118de8d7fSPeter Avalos #include "channels.h"
7218de8d7fSPeter Avalos #include "ssh2.h"
73664f4763Szrj #include "sshkey.h"
7418de8d7fSPeter Avalos #include "cipher.h"
7518de8d7fSPeter Avalos #include "kex.h"
7618de8d7fSPeter Avalos #include "hostfile.h"
7718de8d7fSPeter Avalos #include "auth.h"
7818de8d7fSPeter Avalos #include "session.h"
7918de8d7fSPeter Avalos #include "dispatch.h"
8018de8d7fSPeter Avalos #include "auth-options.h"
8118de8d7fSPeter Avalos #include "serverloop.h"
82e9778795SPeter Avalos #include "ssherr.h"
8318de8d7fSPeter Avalos 
8418de8d7fSPeter Avalos extern ServerOptions options;
8518de8d7fSPeter Avalos 
8618de8d7fSPeter Avalos /* XXX */
8718de8d7fSPeter Avalos extern Authctxt *the_authctxt;
88664f4763Szrj extern struct sshauthopt *auth_opts;
8918de8d7fSPeter Avalos 
9018de8d7fSPeter Avalos static int no_more_sessions = 0; /* Disallow further sessions. */
9118de8d7fSPeter Avalos 
9218de8d7fSPeter Avalos static volatile sig_atomic_t child_terminated = 0;	/* The child has terminated. */
9318de8d7fSPeter Avalos 
9418de8d7fSPeter Avalos /* prototypes */
95664f4763Szrj static void server_init_dispatch(struct ssh *);
96664f4763Szrj 
97664f4763Szrj /* requested tunnel forwarding interface(s), shared with session.c */
98664f4763Szrj char *tun_fwd_ifnames = NULL;
99664f4763Szrj 
10018de8d7fSPeter Avalos static void
sigchld_handler(int sig)10118de8d7fSPeter Avalos sigchld_handler(int sig)
10218de8d7fSPeter Avalos {
10318de8d7fSPeter Avalos 	child_terminated = 1;
10418de8d7fSPeter Avalos }
10518de8d7fSPeter Avalos 
10618de8d7fSPeter Avalos static void
client_alive_check(struct ssh * ssh)107ce74bacaSMatthew Dillon client_alive_check(struct ssh *ssh)
10818de8d7fSPeter Avalos {
109664f4763Szrj 	char remote_id[512];
110664f4763Szrj 	int r, channel_id;
11118de8d7fSPeter Avalos 
11218de8d7fSPeter Avalos 	/* timeout, check to see how many we have had */
1130cbfa66cSDaniel Fojt 	if (options.client_alive_count_max > 0 &&
1140cbfa66cSDaniel Fojt 	    ssh_packet_inc_alive_timeouts(ssh) >
115664f4763Szrj 	    options.client_alive_count_max) {
116664f4763Szrj 		sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
117664f4763Szrj 		logit("Timeout, client not responding from %s", remote_id);
11818de8d7fSPeter Avalos 		cleanup_exit(255);
11918de8d7fSPeter Avalos 	}
12018de8d7fSPeter Avalos 
12118de8d7fSPeter Avalos 	/*
12218de8d7fSPeter Avalos 	 * send a bogus global/channel request with "wantreply",
12318de8d7fSPeter Avalos 	 * we should get back a failure
12418de8d7fSPeter Avalos 	 */
125ce74bacaSMatthew Dillon 	if ((channel_id = channel_find_open(ssh)) == -1) {
126664f4763Szrj 		if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
127664f4763Szrj 		    (r = sshpkt_put_cstring(ssh, "keepalive@openssh.com"))
128664f4763Szrj 		    != 0 ||
129664f4763Szrj 		    (r = sshpkt_put_u8(ssh, 1)) != 0) /* boolean: want reply */
13050a69bb5SSascha Wildner 			fatal_fr(r, "compose");
13118de8d7fSPeter Avalos 	} else {
132ce74bacaSMatthew Dillon 		channel_request_start(ssh, channel_id,
133ce74bacaSMatthew Dillon 		    "keepalive@openssh.com", 1);
13418de8d7fSPeter Avalos 	}
135664f4763Szrj 	if ((r = sshpkt_send(ssh)) != 0)
13650a69bb5SSascha Wildner 		fatal_fr(r, "send");
13718de8d7fSPeter Avalos }
13818de8d7fSPeter Avalos 
13918de8d7fSPeter Avalos /*
140ee116499SAntonio Huete Jimenez  * Sleep in ppoll() until we can do something.
141ee116499SAntonio Huete Jimenez  * Optionally, a maximum time can be specified for the duration of
142ee116499SAntonio Huete Jimenez  * the wait (0 = infinite).
14318de8d7fSPeter Avalos  */
14418de8d7fSPeter Avalos 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)145ce74bacaSMatthew Dillon wait_until_can_do_something(struct ssh *ssh,
146ee116499SAntonio Huete Jimenez     int connection_in, int connection_out, struct pollfd **pfdp,
147*ba1276acSMatthew Dillon     u_int *npfd_allocp, u_int *npfd_activep, sigset_t *sigsetp,
148*ba1276acSMatthew Dillon     int *conn_in_readyp, int *conn_out_readyp)
14918de8d7fSPeter Avalos {
150*ba1276acSMatthew Dillon 	struct timespec timeout;
151*ba1276acSMatthew Dillon 	char remote_id[512];
15218de8d7fSPeter Avalos 	int ret;
15318de8d7fSPeter Avalos 	int client_alive_scheduled = 0;
154ee116499SAntonio Huete Jimenez 	u_int p;
155*ba1276acSMatthew Dillon 	time_t now;
156*ba1276acSMatthew Dillon 	static time_t last_client_time, unused_connection_expiry;
15718de8d7fSPeter Avalos 
158ee116499SAntonio Huete Jimenez 	*conn_in_readyp = *conn_out_readyp = 0;
159ee116499SAntonio Huete Jimenez 
160ee116499SAntonio Huete Jimenez 	/* Prepare channel poll. First two pollfd entries are reserved */
161*ba1276acSMatthew Dillon 	ptimeout_init(&timeout);
162*ba1276acSMatthew Dillon 	channel_prepare_poll(ssh, pfdp, npfd_allocp, npfd_activep, 2, &timeout);
163*ba1276acSMatthew Dillon 	now = monotime();
164ee116499SAntonio Huete Jimenez 	if (*npfd_activep < 2)
165ee116499SAntonio Huete Jimenez 		fatal_f("bad npfd %u", *npfd_activep); /* shouldn't happen */
166*ba1276acSMatthew Dillon 	if (options.rekey_interval > 0 && !ssh_packet_is_rekeying(ssh)) {
167*ba1276acSMatthew Dillon 		ptimeout_deadline_sec(&timeout,
168*ba1276acSMatthew Dillon 		    ssh_packet_get_rekey_timeout(ssh));
169*ba1276acSMatthew Dillon 	}
17099e85e0dSPeter Avalos 
171*ba1276acSMatthew Dillon 	/*
172*ba1276acSMatthew Dillon 	 * If no channels are open and UnusedConnectionTimeout is set, then
173*ba1276acSMatthew Dillon 	 * start the clock to terminate the connection.
174*ba1276acSMatthew Dillon 	 */
175*ba1276acSMatthew Dillon 	if (options.unused_connection_timeout != 0) {
176*ba1276acSMatthew Dillon 		if (channel_still_open(ssh) || unused_connection_expiry == 0) {
177*ba1276acSMatthew Dillon 			unused_connection_expiry = now +
178*ba1276acSMatthew Dillon 			    options.unused_connection_timeout;
179*ba1276acSMatthew Dillon 		}
180*ba1276acSMatthew Dillon 		ptimeout_deadline_monotime(&timeout, unused_connection_expiry);
181*ba1276acSMatthew Dillon 	}
18299e85e0dSPeter Avalos 
18318de8d7fSPeter Avalos 	/*
18418de8d7fSPeter Avalos 	 * if using client_alive, set the max timeout accordingly,
18518de8d7fSPeter Avalos 	 * and indicate that this particular timeout was for client
18618de8d7fSPeter Avalos 	 * alive by setting the client_alive_scheduled flag.
18718de8d7fSPeter Avalos 	 *
18818de8d7fSPeter Avalos 	 * this could be randomized somewhat to make traffic
18918de8d7fSPeter Avalos 	 * analysis more difficult, but we're not doing it yet.
19018de8d7fSPeter Avalos 	 */
191ce74bacaSMatthew Dillon 	if (options.client_alive_interval) {
192*ba1276acSMatthew Dillon 		/* Time we last heard from the client OR sent a keepalive */
19350a69bb5SSascha Wildner 		if (last_client_time == 0)
194*ba1276acSMatthew Dillon 			last_client_time = now;
195*ba1276acSMatthew Dillon 		ptimeout_deadline_sec(&timeout, options.client_alive_interval);
196*ba1276acSMatthew Dillon 		/* XXX ? deadline_monotime(last_client_time + alive_interval) */
197*ba1276acSMatthew Dillon 		client_alive_scheduled = 1;
19818de8d7fSPeter Avalos 	}
19918de8d7fSPeter Avalos 
20018de8d7fSPeter Avalos #if 0
20118de8d7fSPeter Avalos 	/* wrong: bad condition XXX */
20218de8d7fSPeter Avalos 	if (channel_not_very_much_buffered_data())
20318de8d7fSPeter Avalos #endif
204ee116499SAntonio Huete Jimenez 	/* Monitor client connection on reserved pollfd entries */
205ee116499SAntonio Huete Jimenez 	(*pfdp)[0].fd = connection_in;
206ee116499SAntonio Huete Jimenez 	(*pfdp)[0].events = POLLIN;
207ee116499SAntonio Huete Jimenez 	(*pfdp)[1].fd = connection_out;
208ee116499SAntonio Huete Jimenez 	(*pfdp)[1].events = ssh_packet_have_data_to_write(ssh) ? POLLOUT : 0;
20918de8d7fSPeter Avalos 
21018de8d7fSPeter Avalos 	/*
21118de8d7fSPeter Avalos 	 * If child has terminated and there is enough buffer space to read
21218de8d7fSPeter Avalos 	 * from it, then read as much as is available and exit.
21318de8d7fSPeter Avalos 	 */
214664f4763Szrj 	if (child_terminated && ssh_packet_not_very_much_data_to_write(ssh))
215*ba1276acSMatthew Dillon 		ptimeout_deadline_ms(&timeout, 100);
21618de8d7fSPeter Avalos 
21718de8d7fSPeter Avalos 	/* Wait for something to happen, or the timeout to expire. */
218*ba1276acSMatthew Dillon 	ret = ppoll(*pfdp, *npfd_activep, ptimeout_get_tsp(&timeout), sigsetp);
21918de8d7fSPeter Avalos 
22018de8d7fSPeter Avalos 	if (ret == -1) {
221ee116499SAntonio Huete Jimenez 		for (p = 0; p < *npfd_activep; p++)
222ee116499SAntonio Huete Jimenez 			(*pfdp)[p].revents = 0;
22318de8d7fSPeter Avalos 		if (errno != EINTR)
224ee116499SAntonio Huete Jimenez 			fatal_f("ppoll: %.100s", strerror(errno));
225ee116499SAntonio Huete Jimenez 		return;
226ee116499SAntonio Huete Jimenez 	}
227ee116499SAntonio Huete Jimenez 
228ee116499SAntonio Huete Jimenez 	*conn_in_readyp = (*pfdp)[0].revents != 0;
229ee116499SAntonio Huete Jimenez 	*conn_out_readyp = (*pfdp)[1].revents != 0;
230ee116499SAntonio Huete Jimenez 
231*ba1276acSMatthew Dillon 	now = monotime(); /* need to reset after ppoll() */
232*ba1276acSMatthew Dillon 	/* ClientAliveInterval probing */
233ee116499SAntonio Huete Jimenez 	if (client_alive_scheduled) {
234*ba1276acSMatthew Dillon 		if (ret == 0 &&
235*ba1276acSMatthew Dillon 		    now >= last_client_time + options.client_alive_interval) {
236*ba1276acSMatthew Dillon 			/* ppoll timed out and we're due to probe */
237ce74bacaSMatthew Dillon 			client_alive_check(ssh);
238ce74bacaSMatthew Dillon 			last_client_time = now;
239*ba1276acSMatthew Dillon 		} else if (ret != 0 && *conn_in_readyp) {
240*ba1276acSMatthew Dillon 			/* Data from peer; reset probe timer. */
241ce74bacaSMatthew Dillon 			last_client_time = now;
24218de8d7fSPeter Avalos 		}
24318de8d7fSPeter Avalos 	}
24418de8d7fSPeter Avalos 
245*ba1276acSMatthew Dillon 	/* UnusedConnectionTimeout handling */
246*ba1276acSMatthew Dillon 	if (unused_connection_expiry != 0 &&
247*ba1276acSMatthew Dillon 	    now > unused_connection_expiry && !channel_still_open(ssh)) {
248*ba1276acSMatthew Dillon 		sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
249*ba1276acSMatthew Dillon 		logit("terminating inactive connection from %s", remote_id);
250*ba1276acSMatthew Dillon 		cleanup_exit(255);
251*ba1276acSMatthew Dillon 	}
252*ba1276acSMatthew Dillon }
253*ba1276acSMatthew Dillon 
25418de8d7fSPeter Avalos /*
25518de8d7fSPeter Avalos  * Processes input from the client and the program.  Input data is stored
25618de8d7fSPeter Avalos  * in buffers and processed later.
25718de8d7fSPeter Avalos  */
258ce74bacaSMatthew Dillon static int
process_input(struct ssh * ssh,int connection_in)259ee116499SAntonio Huete Jimenez process_input(struct ssh *ssh, int connection_in)
26018de8d7fSPeter Avalos {
261ee116499SAntonio Huete Jimenez 	int r;
26218de8d7fSPeter Avalos 
263ee116499SAntonio Huete Jimenez 	if ((r = ssh_packet_process_read(ssh, connection_in)) == 0)
264ee116499SAntonio Huete Jimenez 		return 0; /* success */
265ee116499SAntonio Huete Jimenez 	if (r == SSH_ERR_SYSTEM_ERROR) {
266ee116499SAntonio Huete Jimenez 		if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)
267ee116499SAntonio Huete Jimenez 			return 0;
268ee116499SAntonio Huete Jimenez 		if (errno == EPIPE) {
269*ba1276acSMatthew Dillon 			logit("Connection closed by %.100s port %d",
270e9778795SPeter Avalos 			    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
271ce74bacaSMatthew Dillon 			return -1;
272ee116499SAntonio Huete Jimenez 		}
273*ba1276acSMatthew Dillon 		logit("Read error from remote host %s port %d: %s",
27450a69bb5SSascha Wildner 		    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
27550a69bb5SSascha Wildner 		    strerror(errno));
27618de8d7fSPeter Avalos 		cleanup_exit(255);
27718de8d7fSPeter Avalos 	}
278ee116499SAntonio Huete Jimenez 	return -1;
27918de8d7fSPeter Avalos }
28018de8d7fSPeter Avalos 
28118de8d7fSPeter Avalos /*
28218de8d7fSPeter Avalos  * Sends data from internal buffers to client program stdin.
28318de8d7fSPeter Avalos  */
28418de8d7fSPeter Avalos static void
process_output(struct ssh * ssh,int connection_out)285ee116499SAntonio Huete Jimenez process_output(struct ssh *ssh, int connection_out)
28618de8d7fSPeter Avalos {
287664f4763Szrj 	int r;
288664f4763Szrj 
28918de8d7fSPeter Avalos 	/* Send any buffered packet data to the client. */
2900cbfa66cSDaniel Fojt 	if ((r = ssh_packet_write_poll(ssh)) != 0) {
2910cbfa66cSDaniel Fojt 		sshpkt_fatal(ssh, r, "%s: ssh_packet_write_poll",
2920cbfa66cSDaniel Fojt 		    __func__);
2930cbfa66cSDaniel Fojt 	}
294664f4763Szrj }
29518de8d7fSPeter Avalos 
29618de8d7fSPeter Avalos static void
process_buffered_input_packets(struct ssh * ssh)297ce74bacaSMatthew Dillon process_buffered_input_packets(struct ssh *ssh)
29818de8d7fSPeter Avalos {
299ce74bacaSMatthew Dillon 	ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, NULL);
30018de8d7fSPeter Avalos }
30118de8d7fSPeter Avalos 
30218de8d7fSPeter Avalos static void
collect_children(struct ssh * ssh)303ce74bacaSMatthew Dillon collect_children(struct ssh *ssh)
30418de8d7fSPeter Avalos {
30518de8d7fSPeter Avalos 	pid_t pid;
30618de8d7fSPeter Avalos 	int status;
30718de8d7fSPeter Avalos 
30818de8d7fSPeter Avalos 	if (child_terminated) {
30918de8d7fSPeter Avalos 		debug("Received SIGCHLD.");
31018de8d7fSPeter Avalos 		while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
3110cbfa66cSDaniel Fojt 		    (pid == -1 && errno == EINTR))
31218de8d7fSPeter Avalos 			if (pid > 0)
313ce74bacaSMatthew Dillon 				session_close_by_pid(ssh, pid, status);
31418de8d7fSPeter Avalos 		child_terminated = 0;
31518de8d7fSPeter Avalos 	}
31618de8d7fSPeter Avalos }
31718de8d7fSPeter Avalos 
31818de8d7fSPeter Avalos void
server_loop2(struct ssh * ssh,Authctxt * authctxt)319ce74bacaSMatthew Dillon server_loop2(struct ssh *ssh, Authctxt *authctxt)
32018de8d7fSPeter Avalos {
321ee116499SAntonio Huete Jimenez 	struct pollfd *pfd = NULL;
322ee116499SAntonio Huete Jimenez 	u_int npfd_alloc = 0, npfd_active = 0;
323ee116499SAntonio Huete Jimenez 	int r, conn_in_ready, conn_out_ready;
324ee116499SAntonio Huete Jimenez 	u_int connection_in, connection_out;
32550a69bb5SSascha Wildner 	sigset_t bsigset, osigset;
32618de8d7fSPeter Avalos 
32718de8d7fSPeter Avalos 	debug("Entering interactive session for SSH2.");
32818de8d7fSPeter Avalos 
32950a69bb5SSascha Wildner 	if (sigemptyset(&bsigset) == -1 || sigaddset(&bsigset, SIGCHLD) == -1)
33050a69bb5SSascha Wildner 		error_f("bsigset setup: %s", strerror(errno));
3310cbfa66cSDaniel Fojt 	ssh_signal(SIGCHLD, sigchld_handler);
33218de8d7fSPeter Avalos 	child_terminated = 0;
333664f4763Szrj 	connection_in = ssh_packet_get_connection_in(ssh);
334664f4763Szrj 	connection_out = ssh_packet_get_connection_out(ssh);
33518de8d7fSPeter Avalos 
336664f4763Szrj 	server_init_dispatch(ssh);
33718de8d7fSPeter Avalos 
33818de8d7fSPeter Avalos 	for (;;) {
339ce74bacaSMatthew Dillon 		process_buffered_input_packets(ssh);
34018de8d7fSPeter Avalos 
341ce74bacaSMatthew Dillon 		if (!ssh_packet_is_rekeying(ssh) &&
342664f4763Szrj 		    ssh_packet_not_very_much_data_to_write(ssh))
343ce74bacaSMatthew Dillon 			channel_output_poll(ssh);
34436e94dc5SPeter Avalos 
34550a69bb5SSascha Wildner 		/*
34650a69bb5SSascha Wildner 		 * Block SIGCHLD while we check for dead children, then pass
347ee116499SAntonio Huete Jimenez 		 * the old signal mask through to ppoll() so that it'll wake
34850a69bb5SSascha Wildner 		 * up immediately if a child exits after we've called waitpid().
34950a69bb5SSascha Wildner 		 */
35050a69bb5SSascha Wildner 		if (sigprocmask(SIG_BLOCK, &bsigset, &osigset) == -1)
35150a69bb5SSascha Wildner 			error_f("bsigset sigprocmask: %s", strerror(errno));
35250a69bb5SSascha Wildner 		collect_children(ssh);
353ce74bacaSMatthew Dillon 		wait_until_can_do_something(ssh, connection_in, connection_out,
354*ba1276acSMatthew Dillon 		    &pfd, &npfd_alloc, &npfd_active, &osigset,
355ee116499SAntonio Huete Jimenez 		    &conn_in_ready, &conn_out_ready);
356*ba1276acSMatthew Dillon 		if (sigprocmask(SIG_SETMASK, &osigset, NULL) == -1)
35750a69bb5SSascha Wildner 			error_f("osigset sigprocmask: %s", strerror(errno));
35818de8d7fSPeter Avalos 
359ee116499SAntonio Huete Jimenez 		channel_after_poll(ssh, pfd, npfd_active);
360ee116499SAntonio Huete Jimenez 		if (conn_in_ready &&
361ee116499SAntonio Huete Jimenez 		    process_input(ssh, connection_in) < 0)
36218de8d7fSPeter Avalos 			break;
36350a69bb5SSascha Wildner 		/* A timeout may have triggered rekeying */
36450a69bb5SSascha Wildner 		if ((r = ssh_packet_check_rekey(ssh)) != 0)
36550a69bb5SSascha Wildner 			fatal_fr(r, "cannot start rekeying");
366ee116499SAntonio Huete Jimenez 		if (conn_out_ready)
367ee116499SAntonio Huete Jimenez 			process_output(ssh, connection_out);
36818de8d7fSPeter Avalos 	}
369ce74bacaSMatthew Dillon 	collect_children(ssh);
370ee116499SAntonio Huete Jimenez 	free(pfd);
37118de8d7fSPeter Avalos 
37218de8d7fSPeter Avalos 	/* free all channels, no more reads and writes */
373ce74bacaSMatthew Dillon 	channel_free_all(ssh);
37418de8d7fSPeter Avalos 
37518de8d7fSPeter Avalos 	/* free remaining sessions, e.g. remove wtmp entries */
376ce74bacaSMatthew Dillon 	session_destroy_all(ssh, NULL);
37718de8d7fSPeter Avalos }
37818de8d7fSPeter Avalos 
379e9778795SPeter Avalos static int
server_input_keep_alive(int type,u_int32_t seq,struct ssh * ssh)380ce74bacaSMatthew Dillon server_input_keep_alive(int type, u_int32_t seq, struct ssh *ssh)
38118de8d7fSPeter Avalos {
38218de8d7fSPeter Avalos 	debug("Got %d/%u for keepalive", type, seq);
38318de8d7fSPeter Avalos 	/*
38418de8d7fSPeter Avalos 	 * reset timeout, since we got a sane answer from the client.
38518de8d7fSPeter Avalos 	 * even if this was generated by something other than
38618de8d7fSPeter Avalos 	 * the bogus CHANNEL_REQUEST we send for keepalives.
38718de8d7fSPeter Avalos 	 */
388664f4763Szrj 	ssh_packet_set_alive_timeouts(ssh, 0);
389e9778795SPeter Avalos 	return 0;
39018de8d7fSPeter Avalos }
39118de8d7fSPeter Avalos 
39218de8d7fSPeter Avalos static Channel *
server_request_direct_tcpip(struct ssh * ssh,int * reason,const char ** errmsg)393ce74bacaSMatthew Dillon server_request_direct_tcpip(struct ssh *ssh, int *reason, const char **errmsg)
39418de8d7fSPeter Avalos {
39536e94dc5SPeter Avalos 	Channel *c = NULL;
396664f4763Szrj 	char *target = NULL, *originator = NULL;
397664f4763Szrj 	u_int target_port = 0, originator_port = 0;
398664f4763Szrj 	int r;
39918de8d7fSPeter Avalos 
400664f4763Szrj 	if ((r = sshpkt_get_cstring(ssh, &target, NULL)) != 0 ||
401664f4763Szrj 	    (r = sshpkt_get_u32(ssh, &target_port)) != 0 ||
402664f4763Szrj 	    (r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 ||
403664f4763Szrj 	    (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
404664f4763Szrj 	    (r = sshpkt_get_end(ssh)) != 0)
405664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
406664f4763Szrj 	if (target_port > 0xFFFF) {
40750a69bb5SSascha Wildner 		error_f("invalid target port");
408664f4763Szrj 		*reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
409664f4763Szrj 		goto out;
410664f4763Szrj 	}
411664f4763Szrj 	if (originator_port > 0xFFFF) {
41250a69bb5SSascha Wildner 		error_f("invalid originator port");
413664f4763Szrj 		*reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
414664f4763Szrj 		goto out;
415664f4763Szrj 	}
41618de8d7fSPeter Avalos 
41750a69bb5SSascha Wildner 	debug_f("originator %s port %u, target %s port %u",
418664f4763Szrj 	    originator, originator_port, target, target_port);
41918de8d7fSPeter Avalos 
42036e94dc5SPeter Avalos 	/* XXX fine grained permissions */
42136e94dc5SPeter Avalos 	if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 &&
422664f4763Szrj 	    auth_opts->permit_port_forwarding_flag &&
423664f4763Szrj 	    !options.disable_forwarding) {
424ce74bacaSMatthew Dillon 		c = channel_connect_to_port(ssh, target, target_port,
425ce74bacaSMatthew Dillon 		    "direct-tcpip", "direct-tcpip", reason, errmsg);
42636e94dc5SPeter Avalos 	} else {
42736e94dc5SPeter Avalos 		logit("refused local port forward: "
42836e94dc5SPeter Avalos 		    "originator %s port %d, target %s port %d",
42936e94dc5SPeter Avalos 		    originator, originator_port, target, target_port);
430ce74bacaSMatthew Dillon 		if (reason != NULL)
431ce74bacaSMatthew Dillon 			*reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
43236e94dc5SPeter Avalos 	}
43318de8d7fSPeter Avalos 
434664f4763Szrj  out:
43536e94dc5SPeter Avalos 	free(originator);
43636e94dc5SPeter Avalos 	free(target);
43736e94dc5SPeter Avalos 	return c;
43836e94dc5SPeter Avalos }
43936e94dc5SPeter Avalos 
44036e94dc5SPeter Avalos static Channel *
server_request_direct_streamlocal(struct ssh * ssh)441ce74bacaSMatthew Dillon server_request_direct_streamlocal(struct ssh *ssh)
44236e94dc5SPeter Avalos {
44336e94dc5SPeter Avalos 	Channel *c = NULL;
444664f4763Szrj 	char *target = NULL, *originator = NULL;
445664f4763Szrj 	u_int originator_port = 0;
446ce74bacaSMatthew Dillon 	struct passwd *pw = the_authctxt->pw;
447664f4763Szrj 	int r;
448ce74bacaSMatthew Dillon 
449ce74bacaSMatthew Dillon 	if (pw == NULL || !the_authctxt->valid)
45050a69bb5SSascha Wildner 		fatal_f("no/invalid user");
45136e94dc5SPeter Avalos 
452664f4763Szrj 	if ((r = sshpkt_get_cstring(ssh, &target, NULL)) != 0 ||
453664f4763Szrj 	    (r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 ||
454664f4763Szrj 	    (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
455664f4763Szrj 	    (r = sshpkt_get_end(ssh)) != 0)
456664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
457664f4763Szrj 	if (originator_port > 0xFFFF) {
45850a69bb5SSascha Wildner 		error_f("invalid originator port");
459664f4763Szrj 		goto out;
460664f4763Szrj 	}
46136e94dc5SPeter Avalos 
46250a69bb5SSascha Wildner 	debug_f("originator %s port %d, target %s",
46336e94dc5SPeter Avalos 	    originator, originator_port, target);
46436e94dc5SPeter Avalos 
46536e94dc5SPeter Avalos 	/* XXX fine grained permissions */
46636e94dc5SPeter Avalos 	if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 &&
467664f4763Szrj 	    auth_opts->permit_port_forwarding_flag &&
468*ba1276acSMatthew Dillon 	    !options.disable_forwarding) {
469ce74bacaSMatthew Dillon 		c = channel_connect_to_path(ssh, target,
47036e94dc5SPeter Avalos 		    "direct-streamlocal@openssh.com", "direct-streamlocal");
47136e94dc5SPeter Avalos 	} else {
47236e94dc5SPeter Avalos 		logit("refused streamlocal port forward: "
47336e94dc5SPeter Avalos 		    "originator %s port %d, target %s",
47436e94dc5SPeter Avalos 		    originator, originator_port, target);
47536e94dc5SPeter Avalos 	}
47636e94dc5SPeter Avalos 
477664f4763Szrj out:
47836e94dc5SPeter Avalos 	free(originator);
47936e94dc5SPeter Avalos 	free(target);
48018de8d7fSPeter Avalos 	return c;
48118de8d7fSPeter Avalos }
48218de8d7fSPeter Avalos 
48318de8d7fSPeter Avalos static Channel *
server_request_tun(struct ssh * ssh)484ce74bacaSMatthew Dillon server_request_tun(struct ssh *ssh)
48518de8d7fSPeter Avalos {
48618de8d7fSPeter Avalos 	Channel *c = NULL;
487664f4763Szrj 	u_int mode, tun;
488664f4763Szrj 	int r, sock;
489664f4763Szrj 	char *tmp, *ifname = NULL;
49018de8d7fSPeter Avalos 
491664f4763Szrj 	if ((r = sshpkt_get_u32(ssh, &mode)) != 0)
492664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse mode", __func__);
49318de8d7fSPeter Avalos 	switch (mode) {
49418de8d7fSPeter Avalos 	case SSH_TUNMODE_POINTOPOINT:
49518de8d7fSPeter Avalos 	case SSH_TUNMODE_ETHERNET:
49618de8d7fSPeter Avalos 		break;
49718de8d7fSPeter Avalos 	default:
498664f4763Szrj 		ssh_packet_send_debug(ssh, "Unsupported tunnel device mode.");
49918de8d7fSPeter Avalos 		return NULL;
50018de8d7fSPeter Avalos 	}
50118de8d7fSPeter Avalos 	if ((options.permit_tun & mode) == 0) {
502664f4763Szrj 		ssh_packet_send_debug(ssh, "Server has rejected tunnel device "
50318de8d7fSPeter Avalos 		    "forwarding");
50418de8d7fSPeter Avalos 		return NULL;
50518de8d7fSPeter Avalos 	}
50618de8d7fSPeter Avalos 
507664f4763Szrj 	if ((r = sshpkt_get_u32(ssh, &tun)) != 0)
508664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse device", __func__);
509664f4763Szrj 	if (tun > INT_MAX) {
51050a69bb5SSascha Wildner 		debug_f("invalid tun");
51118de8d7fSPeter Avalos 		goto done;
51218de8d7fSPeter Avalos 	}
513664f4763Szrj 	if (auth_opts->force_tun_device != -1) {
514664f4763Szrj 		if (tun != SSH_TUNID_ANY &&
515664f4763Szrj 		    auth_opts->force_tun_device != (int)tun)
516664f4763Szrj 			goto done;
517664f4763Szrj 		tun = auth_opts->force_tun_device;
518664f4763Szrj 	}
519664f4763Szrj 	sock = tun_open(tun, mode, &ifname);
52018de8d7fSPeter Avalos 	if (sock < 0)
52118de8d7fSPeter Avalos 		goto done;
522664f4763Szrj 	debug("Tunnel forwarding using interface %s", ifname);
523664f4763Szrj 
524ce74bacaSMatthew Dillon 	c = channel_new(ssh, "tun", SSH_CHANNEL_OPEN, sock, sock, -1,
52518de8d7fSPeter Avalos 	    CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
52618de8d7fSPeter Avalos 	c->datagram = 1;
52718de8d7fSPeter Avalos #if defined(SSH_TUN_FILTER)
52818de8d7fSPeter Avalos 	if (mode == SSH_TUNMODE_POINTOPOINT)
529ce74bacaSMatthew Dillon 		channel_register_filter(ssh, c->self, sys_tun_infilter,
53018de8d7fSPeter Avalos 		    sys_tun_outfilter, NULL, NULL);
53118de8d7fSPeter Avalos #endif
53218de8d7fSPeter Avalos 
533664f4763Szrj 	/*
534664f4763Szrj 	 * Update the list of names exposed to the session
535664f4763Szrj 	 * XXX remove these if the tunnels are closed (won't matter
536664f4763Szrj 	 * much if they are already in the environment though)
537664f4763Szrj 	 */
538664f4763Szrj 	tmp = tun_fwd_ifnames;
539664f4763Szrj 	xasprintf(&tun_fwd_ifnames, "%s%s%s",
540664f4763Szrj 	    tun_fwd_ifnames == NULL ? "" : tun_fwd_ifnames,
541664f4763Szrj 	    tun_fwd_ifnames == NULL ? "" : ",",
542664f4763Szrj 	    ifname);
543664f4763Szrj 	free(tmp);
544664f4763Szrj 	free(ifname);
545664f4763Szrj 
54618de8d7fSPeter Avalos  done:
54718de8d7fSPeter Avalos 	if (c == NULL)
548664f4763Szrj 		ssh_packet_send_debug(ssh, "Failed to open the tunnel device.");
54918de8d7fSPeter Avalos 	return c;
55018de8d7fSPeter Avalos }
55118de8d7fSPeter Avalos 
55218de8d7fSPeter Avalos static Channel *
server_request_session(struct ssh * ssh)553ce74bacaSMatthew Dillon server_request_session(struct ssh *ssh)
55418de8d7fSPeter Avalos {
55518de8d7fSPeter Avalos 	Channel *c;
556664f4763Szrj 	int r;
55718de8d7fSPeter Avalos 
55818de8d7fSPeter Avalos 	debug("input_session_request");
559664f4763Szrj 	if ((r = sshpkt_get_end(ssh)) != 0)
560664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
56118de8d7fSPeter Avalos 
56218de8d7fSPeter Avalos 	if (no_more_sessions) {
563664f4763Szrj 		ssh_packet_disconnect(ssh, "Possible attack: attempt to open a "
564664f4763Szrj 		    "session after additional sessions disabled");
56518de8d7fSPeter Avalos 	}
56618de8d7fSPeter Avalos 
56718de8d7fSPeter Avalos 	/*
56818de8d7fSPeter Avalos 	 * A server session has no fd to read or write until a
56918de8d7fSPeter Avalos 	 * CHANNEL_REQUEST for a shell is made, so we set the type to
57018de8d7fSPeter Avalos 	 * SSH_CHANNEL_LARVAL.  Additionally, a callback for handling all
57118de8d7fSPeter Avalos 	 * CHANNEL_REQUEST messages is registered.
57218de8d7fSPeter Avalos 	 */
573ce74bacaSMatthew Dillon 	c = channel_new(ssh, "session", SSH_CHANNEL_LARVAL,
57418de8d7fSPeter Avalos 	    -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT,
57518de8d7fSPeter Avalos 	    0, "server-session", 1);
57618de8d7fSPeter Avalos 	if (session_open(the_authctxt, c->self) != 1) {
57718de8d7fSPeter Avalos 		debug("session open failed, free channel %d", c->self);
578ce74bacaSMatthew Dillon 		channel_free(ssh, c);
57918de8d7fSPeter Avalos 		return NULL;
58018de8d7fSPeter Avalos 	}
581ce74bacaSMatthew Dillon 	channel_register_cleanup(ssh, c->self, session_close_by_channel, 0);
58218de8d7fSPeter Avalos 	return c;
58318de8d7fSPeter Avalos }
58418de8d7fSPeter Avalos 
585e9778795SPeter Avalos static int
server_input_channel_open(int type,u_int32_t seq,struct ssh * ssh)586ce74bacaSMatthew Dillon server_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
58718de8d7fSPeter Avalos {
58818de8d7fSPeter Avalos 	Channel *c = NULL;
589664f4763Szrj 	char *ctype = NULL;
590ce74bacaSMatthew Dillon 	const char *errmsg = NULL;
591664f4763Szrj 	int r, reason = SSH2_OPEN_CONNECT_FAILED;
592664f4763Szrj 	u_int rchan = 0, rmaxpack = 0, rwindow = 0;
59318de8d7fSPeter Avalos 
594664f4763Szrj 	if ((r = sshpkt_get_cstring(ssh, &ctype, NULL)) != 0 ||
595664f4763Szrj 	    (r = sshpkt_get_u32(ssh, &rchan)) != 0 ||
596664f4763Szrj 	    (r = sshpkt_get_u32(ssh, &rwindow)) != 0 ||
597664f4763Szrj 	    (r = sshpkt_get_u32(ssh, &rmaxpack)) != 0)
598664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
59950a69bb5SSascha Wildner 	debug_f("ctype %s rchan %u win %u max %u",
60018de8d7fSPeter Avalos 	    ctype, rchan, rwindow, rmaxpack);
60118de8d7fSPeter Avalos 
6020cbfa66cSDaniel Fojt 	if (strcmp(ctype, "session") == 0) {
603ce74bacaSMatthew Dillon 		c = server_request_session(ssh);
60418de8d7fSPeter Avalos 	} else if (strcmp(ctype, "direct-tcpip") == 0) {
605ce74bacaSMatthew Dillon 		c = server_request_direct_tcpip(ssh, &reason, &errmsg);
60636e94dc5SPeter Avalos 	} else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) {
607ce74bacaSMatthew Dillon 		c = server_request_direct_streamlocal(ssh);
60818de8d7fSPeter Avalos 	} else if (strcmp(ctype, "tun@openssh.com") == 0) {
609ce74bacaSMatthew Dillon 		c = server_request_tun(ssh);
61018de8d7fSPeter Avalos 	}
61118de8d7fSPeter Avalos 	if (c != NULL) {
61250a69bb5SSascha Wildner 		debug_f("confirm %s", ctype);
6130cbfa66cSDaniel Fojt 		c->remote_id = rchan;
614ce74bacaSMatthew Dillon 		c->have_remote_id = 1;
61518de8d7fSPeter Avalos 		c->remote_window = rwindow;
61618de8d7fSPeter Avalos 		c->remote_maxpacket = rmaxpack;
61718de8d7fSPeter Avalos 		if (c->type != SSH_CHANNEL_CONNECTING) {
618664f4763Szrj 			if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
619664f4763Szrj 			    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
620664f4763Szrj 			    (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
621664f4763Szrj 			    (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
622664f4763Szrj 			    (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
623664f4763Szrj 			    (r = sshpkt_send(ssh)) != 0) {
624664f4763Szrj 				sshpkt_fatal(ssh, r,
625664f4763Szrj 				    "%s: send open confirm", __func__);
626664f4763Szrj 			}
62718de8d7fSPeter Avalos 		}
62818de8d7fSPeter Avalos 	} else {
62950a69bb5SSascha Wildner 		debug_f("failure %s", ctype);
630664f4763Szrj 		if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
631664f4763Szrj 		    (r = sshpkt_put_u32(ssh, rchan)) != 0 ||
632664f4763Szrj 		    (r = sshpkt_put_u32(ssh, reason)) != 0 ||
633664f4763Szrj 		    (r = sshpkt_put_cstring(ssh, errmsg ? errmsg : "open failed")) != 0 ||
634664f4763Szrj 		    (r = sshpkt_put_cstring(ssh, "")) != 0 ||
635664f4763Szrj 		    (r = sshpkt_send(ssh)) != 0) {
636664f4763Szrj 			sshpkt_fatal(ssh, r,
637664f4763Szrj 			    "%s: send open failure", __func__);
63818de8d7fSPeter Avalos 		}
63918de8d7fSPeter Avalos 	}
64036e94dc5SPeter Avalos 	free(ctype);
641e9778795SPeter Avalos 	return 0;
64218de8d7fSPeter Avalos }
64318de8d7fSPeter Avalos 
644e9778795SPeter Avalos static int
server_input_hostkeys_prove(struct ssh * ssh,struct sshbuf ** respp)645ce74bacaSMatthew Dillon server_input_hostkeys_prove(struct ssh *ssh, struct sshbuf **respp)
646e9778795SPeter Avalos {
647e9778795SPeter Avalos 	struct sshbuf *resp = NULL;
648e9778795SPeter Avalos 	struct sshbuf *sigbuf = NULL;
649e9778795SPeter Avalos 	struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL;
650ee116499SAntonio Huete Jimenez 	int r, ndx, success = 0;
651e9778795SPeter Avalos 	const u_char *blob;
652ee116499SAntonio Huete Jimenez 	const char *sigalg, *kex_rsa_sigalg = NULL;
653e9778795SPeter Avalos 	u_char *sig = 0;
654e9778795SPeter Avalos 	size_t blen, slen;
655e9778795SPeter Avalos 
656e9778795SPeter Avalos 	if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL)
65750a69bb5SSascha Wildner 		fatal_f("sshbuf_new");
658ee116499SAntonio Huete Jimenez 	if (sshkey_type_plain(sshkey_type_from_name(
659ee116499SAntonio Huete Jimenez 	    ssh->kex->hostkey_alg)) == KEY_RSA)
660ee116499SAntonio Huete Jimenez 		kex_rsa_sigalg = ssh->kex->hostkey_alg;
661e9778795SPeter Avalos 	while (ssh_packet_remaining(ssh) > 0) {
662e9778795SPeter Avalos 		sshkey_free(key);
663e9778795SPeter Avalos 		key = NULL;
664e9778795SPeter Avalos 		if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 ||
665e9778795SPeter Avalos 		    (r = sshkey_from_blob(blob, blen, &key)) != 0) {
66650a69bb5SSascha Wildner 			error_fr(r, "parse key");
667e9778795SPeter Avalos 			goto out;
668e9778795SPeter Avalos 		}
669e9778795SPeter Avalos 		/*
670e9778795SPeter Avalos 		 * Better check that this is actually one of our hostkeys
671e9778795SPeter Avalos 		 * before attempting to sign anything with it.
672e9778795SPeter Avalos 		 */
673e9778795SPeter Avalos 		if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) {
67450a69bb5SSascha Wildner 			error_f("unknown host %s key", sshkey_type(key));
675e9778795SPeter Avalos 			goto out;
676e9778795SPeter Avalos 		}
677e9778795SPeter Avalos 		/*
678e9778795SPeter Avalos 		 * XXX refactor: make kex->sign just use an index rather
679e9778795SPeter Avalos 		 * than passing in public and private keys
680e9778795SPeter Avalos 		 */
681e9778795SPeter Avalos 		if ((key_prv = get_hostkey_by_index(ndx)) == NULL &&
682e9778795SPeter Avalos 		    (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) {
68350a69bb5SSascha Wildner 			error_f("can't retrieve hostkey %d", ndx);
684e9778795SPeter Avalos 			goto out;
685e9778795SPeter Avalos 		}
686e9778795SPeter Avalos 		sshbuf_reset(sigbuf);
687e9778795SPeter Avalos 		free(sig);
688e9778795SPeter Avalos 		sig = NULL;
689664f4763Szrj 		/*
690664f4763Szrj 		 * For RSA keys, prefer to use the signature type negotiated
691664f4763Szrj 		 * during KEX to the default (SHA1).
692664f4763Szrj 		 */
693ee116499SAntonio Huete Jimenez 		sigalg = NULL;
694ee116499SAntonio Huete Jimenez 		if (sshkey_type_plain(key->type) == KEY_RSA) {
695ee116499SAntonio Huete Jimenez 			if (kex_rsa_sigalg != NULL)
696ee116499SAntonio Huete Jimenez 				sigalg = kex_rsa_sigalg;
697ee116499SAntonio Huete Jimenez 			else if (ssh->kex->flags & KEX_RSA_SHA2_512_SUPPORTED)
698ee116499SAntonio Huete Jimenez 				sigalg = "rsa-sha2-512";
699ee116499SAntonio Huete Jimenez 			else if (ssh->kex->flags & KEX_RSA_SHA2_256_SUPPORTED)
700ee116499SAntonio Huete Jimenez 				sigalg = "rsa-sha2-256";
701ee116499SAntonio Huete Jimenez 		}
702ee116499SAntonio Huete Jimenez 		debug3_f("sign %s key (index %d) using sigalg %s",
703ee116499SAntonio Huete Jimenez 		    sshkey_type(key), ndx, sigalg == NULL ? "default" : sigalg);
704e9778795SPeter Avalos 		if ((r = sshbuf_put_cstring(sigbuf,
705e9778795SPeter Avalos 		    "hostkeys-prove-00@openssh.com")) != 0 ||
70650a69bb5SSascha Wildner 		    (r = sshbuf_put_stringb(sigbuf,
70750a69bb5SSascha Wildner 		    ssh->kex->session_id)) != 0 ||
708e9778795SPeter Avalos 		    (r = sshkey_puts(key, sigbuf)) != 0 ||
709664f4763Szrj 		    (r = ssh->kex->sign(ssh, key_prv, key_pub, &sig, &slen,
710ee116499SAntonio Huete Jimenez 		    sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), sigalg)) != 0 ||
711e9778795SPeter Avalos 		    (r = sshbuf_put_string(resp, sig, slen)) != 0) {
71250a69bb5SSascha Wildner 			error_fr(r, "assemble signature");
713e9778795SPeter Avalos 			goto out;
714e9778795SPeter Avalos 		}
715e9778795SPeter Avalos 	}
716e9778795SPeter Avalos 	/* Success */
717e9778795SPeter Avalos 	*respp = resp;
718e9778795SPeter Avalos 	resp = NULL; /* don't free it */
719e9778795SPeter Avalos 	success = 1;
720e9778795SPeter Avalos  out:
721e9778795SPeter Avalos 	free(sig);
722e9778795SPeter Avalos 	sshbuf_free(resp);
723e9778795SPeter Avalos 	sshbuf_free(sigbuf);
724e9778795SPeter Avalos 	sshkey_free(key);
725e9778795SPeter Avalos 	return success;
726e9778795SPeter Avalos }
727e9778795SPeter Avalos 
728e9778795SPeter Avalos static int
server_input_global_request(int type,u_int32_t seq,struct ssh * ssh)729ce74bacaSMatthew Dillon server_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
73018de8d7fSPeter Avalos {
731664f4763Szrj 	char *rtype = NULL;
732664f4763Szrj 	u_char want_reply = 0;
733e9778795SPeter Avalos 	int r, success = 0, allocated_listen_port = 0;
734664f4763Szrj 	u_int port = 0;
735e9778795SPeter Avalos 	struct sshbuf *resp = NULL;
736ce74bacaSMatthew Dillon 	struct passwd *pw = the_authctxt->pw;
73736e94dc5SPeter Avalos 	struct Forward fwd;
73818de8d7fSPeter Avalos 
73936e94dc5SPeter Avalos 	memset(&fwd, 0, sizeof(fwd));
740664f4763Szrj 	if (pw == NULL || !the_authctxt->valid)
74150a69bb5SSascha Wildner 		fatal_f("no/invalid user");
74218de8d7fSPeter Avalos 
743664f4763Szrj 	if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
744664f4763Szrj 	    (r = sshpkt_get_u8(ssh, &want_reply)) != 0)
745664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
74650a69bb5SSascha Wildner 	debug_f("rtype %s want_reply %d", rtype, want_reply);
747664f4763Szrj 
748664f4763Szrj 	/* -R style forwarding */
749664f4763Szrj 	if (strcmp(rtype, "tcpip-forward") == 0) {
750664f4763Szrj 		if ((r = sshpkt_get_cstring(ssh, &fwd.listen_host, NULL)) != 0 ||
751664f4763Szrj 		    (r = sshpkt_get_u32(ssh, &port)) != 0)
752664f4763Szrj 			sshpkt_fatal(ssh, r, "%s: parse tcpip-forward", __func__);
75350a69bb5SSascha Wildner 		debug_f("tcpip-forward listen %s port %u",
754664f4763Szrj 		    fwd.listen_host, port);
755664f4763Szrj 		if (port <= INT_MAX)
756664f4763Szrj 			fwd.listen_port = (int)port;
75718de8d7fSPeter Avalos 		/* check permissions */
758664f4763Szrj 		if (port > INT_MAX ||
759664f4763Szrj 		    (options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 ||
760664f4763Szrj 		    !auth_opts->permit_port_forwarding_flag ||
761664f4763Szrj 		    options.disable_forwarding ||
762*ba1276acSMatthew Dillon 		    (!want_reply && fwd.listen_port == 0)) {
76318de8d7fSPeter Avalos 			success = 0;
764664f4763Szrj 			ssh_packet_send_debug(ssh, "Server has disabled port forwarding.");
76518de8d7fSPeter Avalos 		} else {
76618de8d7fSPeter Avalos 			/* Start listening on the port */
767ce74bacaSMatthew Dillon 			success = channel_setup_remote_fwd_listener(ssh, &fwd,
76836e94dc5SPeter Avalos 			    &allocated_listen_port, &options.fwd_opts);
76918de8d7fSPeter Avalos 		}
770e9778795SPeter Avalos 		if ((resp = sshbuf_new()) == NULL)
77150a69bb5SSascha Wildner 			fatal_f("sshbuf_new");
772e9778795SPeter Avalos 		if (allocated_listen_port != 0 &&
773e9778795SPeter Avalos 		    (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0)
77450a69bb5SSascha Wildner 			fatal_fr(r, "sshbuf_put_u32");
77518de8d7fSPeter Avalos 	} else if (strcmp(rtype, "cancel-tcpip-forward") == 0) {
776664f4763Szrj 		if ((r = sshpkt_get_cstring(ssh, &fwd.listen_host, NULL)) != 0 ||
777664f4763Szrj 		    (r = sshpkt_get_u32(ssh, &port)) != 0)
778664f4763Szrj 			sshpkt_fatal(ssh, r, "%s: parse cancel-tcpip-forward", __func__);
77918de8d7fSPeter Avalos 
78050a69bb5SSascha Wildner 		debug_f("cancel-tcpip-forward addr %s port %d",
781664f4763Szrj 		    fwd.listen_host, port);
782664f4763Szrj 		if (port <= INT_MAX) {
783664f4763Szrj 			fwd.listen_port = (int)port;
784ce74bacaSMatthew Dillon 			success = channel_cancel_rport_listener(ssh, &fwd);
785664f4763Szrj 		}
78636e94dc5SPeter Avalos 	} else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) {
787664f4763Szrj 		if ((r = sshpkt_get_cstring(ssh, &fwd.listen_path, NULL)) != 0)
788664f4763Szrj 			sshpkt_fatal(ssh, r, "%s: parse streamlocal-forward@openssh.com", __func__);
78950a69bb5SSascha Wildner 		debug_f("streamlocal-forward listen path %s",
79036e94dc5SPeter Avalos 		    fwd.listen_path);
79136e94dc5SPeter Avalos 
79236e94dc5SPeter Avalos 		/* check permissions */
79336e94dc5SPeter Avalos 		if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0
794664f4763Szrj 		    || !auth_opts->permit_port_forwarding_flag ||
795*ba1276acSMatthew Dillon 		    options.disable_forwarding) {
79636e94dc5SPeter Avalos 			success = 0;
797664f4763Szrj 			ssh_packet_send_debug(ssh, "Server has disabled "
798ce74bacaSMatthew Dillon 			    "streamlocal forwarding.");
79936e94dc5SPeter Avalos 		} else {
80036e94dc5SPeter Avalos 			/* Start listening on the socket */
801ce74bacaSMatthew Dillon 			success = channel_setup_remote_fwd_listener(ssh,
80236e94dc5SPeter Avalos 			    &fwd, NULL, &options.fwd_opts);
80336e94dc5SPeter Avalos 		}
80436e94dc5SPeter Avalos 	} else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) {
805664f4763Szrj 		if ((r = sshpkt_get_cstring(ssh, &fwd.listen_path, NULL)) != 0)
806664f4763Szrj 			sshpkt_fatal(ssh, r, "%s: parse cancel-streamlocal-forward@openssh.com", __func__);
80750a69bb5SSascha Wildner 		debug_f("cancel-streamlocal-forward path %s",
80836e94dc5SPeter Avalos 		    fwd.listen_path);
80936e94dc5SPeter Avalos 
810ce74bacaSMatthew Dillon 		success = channel_cancel_rport_listener(ssh, &fwd);
81118de8d7fSPeter Avalos 	} else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) {
81218de8d7fSPeter Avalos 		no_more_sessions = 1;
81318de8d7fSPeter Avalos 		success = 1;
814e9778795SPeter Avalos 	} else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) {
815ce74bacaSMatthew Dillon 		success = server_input_hostkeys_prove(ssh, &resp);
81618de8d7fSPeter Avalos 	}
817664f4763Szrj 	/* XXX sshpkt_get_end() */
81818de8d7fSPeter Avalos 	if (want_reply) {
819664f4763Szrj 		if ((r = sshpkt_start(ssh, success ?
820664f4763Szrj 		    SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE)) != 0 ||
821664f4763Szrj 		    (success && resp != NULL && (r = sshpkt_putb(ssh, resp)) != 0) ||
822664f4763Szrj 		    (r = sshpkt_send(ssh)) != 0 ||
823664f4763Szrj 		    (r = ssh_packet_write_wait(ssh)) != 0)
824664f4763Szrj 			sshpkt_fatal(ssh, r, "%s: send reply", __func__);
82518de8d7fSPeter Avalos 	}
826664f4763Szrj 	free(fwd.listen_host);
827664f4763Szrj 	free(fwd.listen_path);
82836e94dc5SPeter Avalos 	free(rtype);
829e9778795SPeter Avalos 	sshbuf_free(resp);
830e9778795SPeter Avalos 	return 0;
83118de8d7fSPeter Avalos }
83218de8d7fSPeter Avalos 
833e9778795SPeter Avalos static int
server_input_channel_req(int type,u_int32_t seq,struct ssh * ssh)834ce74bacaSMatthew Dillon server_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
83518de8d7fSPeter Avalos {
83618de8d7fSPeter Avalos 	Channel *c;
837664f4763Szrj 	int r, success = 0;
838664f4763Szrj 	char *rtype = NULL;
839664f4763Szrj 	u_char want_reply = 0;
840664f4763Szrj 	u_int id = 0;
84118de8d7fSPeter Avalos 
842664f4763Szrj 	if ((r = sshpkt_get_u32(ssh, &id)) != 0 ||
843664f4763Szrj 	    (r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
844664f4763Szrj 	    (r = sshpkt_get_u8(ssh, &want_reply)) != 0)
845664f4763Szrj 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
84618de8d7fSPeter Avalos 
847664f4763Szrj 	debug("server_input_channel_req: channel %u request %s reply %d",
848664f4763Szrj 	    id, rtype, want_reply);
84918de8d7fSPeter Avalos 
850664f4763Szrj 	if (id >= INT_MAX || (c = channel_lookup(ssh, (int)id)) == NULL) {
851664f4763Szrj 		ssh_packet_disconnect(ssh, "%s: unknown channel %d",
852664f4763Szrj 		    __func__, id);
853664f4763Szrj 	}
85418de8d7fSPeter Avalos 	if (!strcmp(rtype, "eow@openssh.com")) {
855664f4763Szrj 		if ((r = sshpkt_get_end(ssh)) != 0)
856664f4763Szrj 			sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
857ce74bacaSMatthew Dillon 		chan_rcvd_eow(ssh, c);
85818de8d7fSPeter Avalos 	} else if ((c->type == SSH_CHANNEL_LARVAL ||
85918de8d7fSPeter Avalos 	    c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0)
860ce74bacaSMatthew Dillon 		success = session_input_channel_req(ssh, c, rtype);
861664f4763Szrj 	if (want_reply && !(c->flags & CHAN_CLOSE_SENT)) {
862ce74bacaSMatthew Dillon 		if (!c->have_remote_id)
86350a69bb5SSascha Wildner 			fatal_f("channel %d: no remote_id", c->self);
864664f4763Szrj 		if ((r = sshpkt_start(ssh, success ?
865664f4763Szrj 		    SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE)) != 0 ||
866664f4763Szrj 		    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
867664f4763Szrj 		    (r = sshpkt_send(ssh)) != 0)
868664f4763Szrj 			sshpkt_fatal(ssh, r, "%s: send reply", __func__);
86918de8d7fSPeter Avalos 	}
87036e94dc5SPeter Avalos 	free(rtype);
871e9778795SPeter Avalos 	return 0;
87218de8d7fSPeter Avalos }
87318de8d7fSPeter Avalos 
87418de8d7fSPeter Avalos static void
server_init_dispatch(struct ssh * ssh)875664f4763Szrj server_init_dispatch(struct ssh *ssh)
87618de8d7fSPeter Avalos {
877ce74bacaSMatthew Dillon 	debug("server_init_dispatch");
878664f4763Szrj 	ssh_dispatch_init(ssh, &dispatch_protocol_error);
879664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
880664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_DATA, &channel_input_data);
881664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
882664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
883664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
884664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
885664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
886664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req);
887664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
888664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request);
88918de8d7fSPeter Avalos 	/* client_alive */
890664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive);
891664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive);
892664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive);
893664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive);
89418de8d7fSPeter Avalos 	/* rekeying */
895664f4763Szrj 	ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
89618de8d7fSPeter Avalos }
897