blob: 5ecafded89e5cfd75757efe27d472182a46e10f1 [file] [log] [blame]
djm@openbsd.org027607f2018-06-08 01:55:40 +00001/* $OpenBSD: serverloop.c,v 1.206 2018/06/08 01:55:40 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Server main loop for handling the interactive session.
Damien Millere4340be2000-09-16 13:29:08 +11007 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
Damien Millerefb4afe2000-04-12 18:45:05 +100014 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000015 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110016 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100036 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
38#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110039
40#include <sys/types.h>
41#include <sys/wait.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100042#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100043#ifdef HAVE_SYS_TIME_H
44# include <sys/time.h>
45#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100046
47#include <netinet/in.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110048
Darren Tucker39972492006-07-12 22:22:46 +100049#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100050#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100051#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110052#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100053#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110054#include <termios.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100055#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100056#include <stdarg.h>
Damien Miller69b69aa2000-10-28 14:19:58 +110057
Damien Millerb84886b2008-05-19 15:05:07 +100058#include "openbsd-compat/sys-queue.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060#include "packet.h"
61#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100063#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064#include "servconf.h"
Damien Miller16aed052002-09-22 01:26:27 +100065#include "canohost.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000066#include "sshpty.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000067#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100068#include "compat.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100069#include "ssh2.h"
Damien Millerd7834352006-08-05 12:39:39 +100070#include "key.h"
71#include "cipher.h"
72#include "kex.h"
73#include "hostfile.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000074#include "auth.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100075#include "session.h"
Damien Millerb38eff82000-04-01 11:09:21 +100076#include "dispatch.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100077#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000078#include "serverloop.h"
djm@openbsd.org523463a2015-02-16 22:13:32 +000079#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Damien Miller50a41ed2000-10-16 12:14:42 +110081extern ServerOptions options;
82
Ben Lindstrom8ac91062001-04-04 17:57:54 +000083/* XXX */
Darren Tucker3e33cec2003-10-02 16:12:36 +100084extern Authctxt *the_authctxt;
djm@openbsd.org7c856852018-03-03 03:15:51 +000085extern struct sshauthopt *auth_opts;
Damien Miller24ecf612005-11-05 15:16:52 +110086extern int use_privsep;
Ben Lindstrom8ac91062001-04-04 17:57:54 +000087
Darren Tucker8901fa92008-06-11 09:34:01 +100088static int no_more_sessions = 0; /* Disallow further sessions. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Miller5428f641999-11-25 11:54:57 +110090/*
91 * This SIGCHLD kludge is used to detect when the child exits. The server
92 * will exit after that, as soon as forwarded connections have terminated.
93 */
Ben Lindstrombba81212001-06-25 05:01:22 +000094
Ben Lindstrom5e71c542001-12-06 16:48:14 +000095static volatile sig_atomic_t child_terminated = 0; /* The child has terminated. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
Damien Miller24ecf612005-11-05 15:16:52 +110097/* Cleanup on signals (!use_privsep case only) */
98static volatile sig_atomic_t received_sigterm = 0;
99
Ben Lindstrombba81212001-06-25 05:01:22 +0000100/* prototypes */
101static void server_init_dispatch(void);
Damien Millerb38eff82000-04-01 11:09:21 +1000102
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000103/* requested tunnel forwarding interface(s), shared with session.c */
104char *tun_fwd_ifnames = NULL;
105
Damien Millerf6681a32001-12-21 14:53:11 +1100106/*
107 * we write to this pipe if a SIGCHLD is caught in order to avoid
108 * the race between select() and child_terminated
109 */
110static int notify_pipe[2];
111static void
112notify_setup(void)
113{
114 if (pipe(notify_pipe) < 0) {
115 error("pipe(notify_pipe) failed %s", strerror(errno));
Damien Miller814ace02011-05-20 19:02:47 +1000116 } else if ((fcntl(notify_pipe[0], F_SETFD, FD_CLOEXEC) == -1) ||
117 (fcntl(notify_pipe[1], F_SETFD, FD_CLOEXEC) == -1)) {
Damien Millerf6681a32001-12-21 14:53:11 +1100118 error("fcntl(notify_pipe, F_SETFD) failed %s", strerror(errno));
119 close(notify_pipe[0]);
120 close(notify_pipe[1]);
121 } else {
122 set_nonblock(notify_pipe[0]);
123 set_nonblock(notify_pipe[1]);
124 return;
125 }
126 notify_pipe[0] = -1; /* read end */
127 notify_pipe[1] = -1; /* write end */
128}
129static void
130notify_parent(void)
131{
132 if (notify_pipe[1] != -1)
Darren Tuckerdbee3082013-05-16 20:32:29 +1000133 (void)write(notify_pipe[1], "", 1);
Damien Millerf6681a32001-12-21 14:53:11 +1100134}
135static void
136notify_prepare(fd_set *readset)
137{
138 if (notify_pipe[0] != -1)
139 FD_SET(notify_pipe[0], readset);
140}
141static void
142notify_done(fd_set *readset)
143{
144 char c;
145
146 if (notify_pipe[0] != -1 && FD_ISSET(notify_pipe[0], readset))
147 while (read(notify_pipe[0], &c, 1) != -1)
djm@openbsd.org027607f2018-06-08 01:55:40 +0000148 debug2("%s: reading", __func__);
Damien Millerf6681a32001-12-21 14:53:11 +1100149}
150
Damien Millerf0b15df2006-03-26 13:59:20 +1100151/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000152static void
Damien Miller95def091999-11-25 00:26:21 +1100153sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154{
Damien Miller95def091999-11-25 00:26:21 +1100155 int save_errno = errno;
Damien Millerefb4afe2000-04-12 18:45:05 +1000156 child_terminated = 1;
Damien Millerf6681a32001-12-21 14:53:11 +1100157 notify_parent();
Damien Millerefb4afe2000-04-12 18:45:05 +1000158 errno = save_errno;
159}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Millerf0b15df2006-03-26 13:59:20 +1100161/*ARGSUSED*/
Damien Miller24ecf612005-11-05 15:16:52 +1100162static void
163sigterm_handler(int sig)
164{
165 received_sigterm = sig;
166}
167
Damien Miller8c3902a2001-10-10 15:01:40 +1000168static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000169client_alive_check(struct ssh *ssh)
Damien Miller8c3902a2001-10-10 15:01:40 +1000170{
Damien Millerb5820f42003-12-17 16:27:32 +1100171 int channel_id;
dtucker@openbsd.org48c23a32017-12-10 05:55:29 +0000172 char remote_id[512];
Damien Miller056cf732002-01-22 23:21:39 +1100173
Damien Miller8c3902a2001-10-10 15:01:40 +1000174 /* timeout, check to see how many we have had */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000175 if (packet_inc_alive_timeouts() > options.client_alive_count_max) {
dtucker@openbsd.org48c23a32017-12-10 05:55:29 +0000176 sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
177 logit("Timeout, client not responding from %s", remote_id);
Damien Miller985a4482006-10-24 03:02:41 +1000178 cleanup_exit(255);
179 }
Damien Miller8c3902a2001-10-10 15:01:40 +1000180
Damien Miller8c3902a2001-10-10 15:01:40 +1000181 /*
Damien Millerb5820f42003-12-17 16:27:32 +1100182 * send a bogus global/channel request with "wantreply",
Damien Miller8c3902a2001-10-10 15:01:40 +1000183 * we should get back a failure
184 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000185 if ((channel_id = channel_find_open(ssh)) == -1) {
Damien Millerb5820f42003-12-17 16:27:32 +1100186 packet_start(SSH2_MSG_GLOBAL_REQUEST);
187 packet_put_cstring("keepalive@openssh.com");
188 packet_put_char(1); /* boolean: want reply */
189 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000190 channel_request_start(ssh, channel_id,
191 "keepalive@openssh.com", 1);
Damien Millerb5820f42003-12-17 16:27:32 +1100192 }
Damien Miller8c3902a2001-10-10 15:01:40 +1000193 packet_send();
194}
195
Damien Miller95def091999-11-25 00:26:21 +1100196/*
197 * Sleep in select() until we can do something. This will initialize the
198 * select masks. Upon return, the masks will indicate which descriptors
199 * have data or can accept data. Optionally, a maximum time can be specified
200 * for the duration of the wait (0 = infinite).
201 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000202static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000203wait_until_can_do_something(struct ssh *ssh,
204 int connection_in, int connection_out,
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000205 fd_set **readsetp, fd_set **writesetp, int *maxfdp,
djm@openbsd.org988e4292016-03-04 03:35:44 +0000206 u_int *nallocp, u_int64_t max_time_ms)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207{
Damien Miller95def091999-11-25 00:26:21 +1100208 struct timeval tv, *tvp;
209 int ret;
Damien Miller6c6da332012-06-20 22:31:26 +1000210 time_t minwait_secs = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000211 int client_alive_scheduled = 0;
dtucker@openbsd.orgb60ff202017-08-11 03:58:36 +0000212 static time_t last_client_time;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000213
Damien Millera6508752012-04-22 11:21:10 +1000214 /* Allocate and update select() masks for channel descriptors. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000215 channel_prepare_select(ssh, readsetp, writesetp, maxfdp,
djm@openbsd.org71e5a532017-08-30 03:59:08 +0000216 nallocp, &minwait_secs);
Damien Millera6508752012-04-22 11:21:10 +1000217
djm@openbsd.org988e4292016-03-04 03:35:44 +0000218 /* XXX need proper deadline system for rekey/client alive */
Damien Millera6508752012-04-22 11:21:10 +1000219 if (minwait_secs != 0)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000220 max_time_ms = MINIMUM(max_time_ms, (u_int)minwait_secs * 1000);
Damien Millera6508752012-04-22 11:21:10 +1000221
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000222 /*
Damien Miller9f0f5c62001-12-21 14:45:46 +1100223 * if using client_alive, set the max timeout accordingly,
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000224 * and indicate that this particular timeout was for client
225 * alive by setting the client_alive_scheduled flag.
226 *
227 * this could be randomized somewhat to make traffic
Damien Miller9f0f5c62001-12-21 14:45:46 +1100228 * analysis more difficult, but we're not doing it yet.
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000229 */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000230 if (options.client_alive_interval) {
djm@openbsd.org988e4292016-03-04 03:35:44 +0000231 uint64_t keepalive_ms =
232 (uint64_t)options.client_alive_interval * 1000;
233
Ben Lindstrom2f0304c2001-04-29 19:49:14 +0000234 client_alive_scheduled = 1;
djm@openbsd.org988e4292016-03-04 03:35:44 +0000235 if (max_time_ms == 0 || max_time_ms > keepalive_ms)
236 max_time_ms = keepalive_ms;
Ben Lindstrom36857f62001-07-18 15:48:57 +0000237 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238
Damien Milleraf5f2e62001-10-10 15:01:16 +1000239#if 0
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000240 /* wrong: bad condition XXX */
241 if (channel_not_very_much_buffered_data())
Damien Milleraf5f2e62001-10-10 15:01:16 +1000242#endif
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000243 FD_SET(connection_in, *readsetp);
Damien Millerf6681a32001-12-21 14:53:11 +1100244 notify_prepare(*readsetp);
Damien Miller95def091999-11-25 00:26:21 +1100245
Damien Miller5428f641999-11-25 11:54:57 +1100246 /*
247 * If we have buffered packet data going to the client, mark that
248 * descriptor.
249 */
Damien Miller95def091999-11-25 00:26:21 +1100250 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100251 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100252
Damien Miller5428f641999-11-25 11:54:57 +1100253 /*
254 * If child has terminated and there is enough buffer space to read
255 * from it, then read as much as is available and exit.
256 */
Damien Miller95def091999-11-25 00:26:21 +1100257 if (child_terminated && packet_not_very_much_data_to_write())
djm@openbsd.org988e4292016-03-04 03:35:44 +0000258 if (max_time_ms == 0 || client_alive_scheduled)
259 max_time_ms = 100;
Damien Miller95def091999-11-25 00:26:21 +1100260
djm@openbsd.org988e4292016-03-04 03:35:44 +0000261 if (max_time_ms == 0)
Damien Miller95def091999-11-25 00:26:21 +1100262 tvp = NULL;
263 else {
djm@openbsd.org988e4292016-03-04 03:35:44 +0000264 tv.tv_sec = max_time_ms / 1000;
265 tv.tv_usec = 1000 * (max_time_ms % 1000);
Damien Miller95def091999-11-25 00:26:21 +1100266 tvp = &tv;
267 }
268
269 /* Wait for something to happen, or the timeout to expire. */
Damien Miller5e953212001-01-30 09:14:00 +1100270 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
Damien Miller95def091999-11-25 00:26:21 +1100271
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000272 if (ret == -1) {
Damien Miller79faeff2001-11-12 11:06:32 +1100273 memset(*readsetp, 0, *nallocp);
274 memset(*writesetp, 0, *nallocp);
Damien Miller95def091999-11-25 00:26:21 +1100275 if (errno != EINTR)
276 error("select: %.100s", strerror(errno));
dtucker@openbsd.orgb60ff202017-08-11 03:58:36 +0000277 } else if (client_alive_scheduled) {
278 time_t now = monotime();
279
280 if (ret == 0) { /* timeout */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000281 client_alive_check(ssh);
dtucker@openbsd.orgb60ff202017-08-11 03:58:36 +0000282 } else if (FD_ISSET(connection_in, *readsetp)) {
283 last_client_time = now;
284 } else if (last_client_time != 0 && last_client_time +
dtucker@openbsd.org42a8f8b2017-08-11 04:16:35 +0000285 options.client_alive_interval <= now) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000286 client_alive_check(ssh);
dtucker@openbsd.orgb60ff202017-08-11 03:58:36 +0000287 last_client_time = now;
288 }
289 }
Damien Millerf6681a32001-12-21 14:53:11 +1100290
291 notify_done(*readsetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292}
293
Damien Miller95def091999-11-25 00:26:21 +1100294/*
295 * Processes input from the client and the program. Input data is stored
296 * in buffers and processed later.
297 */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000298static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000299process_input(struct ssh *ssh, fd_set *readset, int connection_in)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300{
Damien Miller95def091999-11-25 00:26:21 +1100301 int len;
302 char buf[16384];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
Damien Miller95def091999-11-25 00:26:21 +1100304 /* Read and buffer any input data from the client. */
305 if (FD_ISSET(connection_in, readset)) {
markus@openbsd.orga3068632016-01-14 16:17:39 +0000306 len = read(connection_in, buf, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +1100307 if (len == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +0000308 verbose("Connection closed by %.100s port %d",
309 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000310 return -1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000311 } else if (len < 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +1000312 if (errno != EINTR && errno != EAGAIN &&
313 errno != EWOULDBLOCK) {
Damien Miller16aed052002-09-22 01:26:27 +1000314 verbose("Read error from remote host "
djm@openbsd.org95767262016-03-07 19:02:43 +0000315 "%.100s port %d: %.100s",
316 ssh_remote_ipaddr(ssh),
317 ssh_remote_port(ssh), strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000318 cleanup_exit(255);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000319 }
320 } else {
321 /* Buffer any received data. */
322 packet_process_incoming(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100323 }
Damien Miller95def091999-11-25 00:26:21 +1100324 }
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000325 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100326}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327
Damien Miller95def091999-11-25 00:26:21 +1100328/*
329 * Sends data from internal buffers to client program stdin.
330 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000331static void
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000332process_output(fd_set *writeset, int connection_out)
Damien Miller95def091999-11-25 00:26:21 +1100333{
Damien Miller95def091999-11-25 00:26:21 +1100334 /* Send any buffered packet data to the client. */
335 if (FD_ISSET(connection_out, writeset))
336 packet_write_poll();
337}
338
Ben Lindstrombba81212001-06-25 05:01:22 +0000339static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000340process_buffered_input_packets(struct ssh *ssh)
Damien Millerb38eff82000-04-01 11:09:21 +1000341{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000342 ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000343}
344
Damien Miller3ec27592001-10-12 11:35:04 +1000345static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000346collect_children(struct ssh *ssh)
Damien Miller3ec27592001-10-12 11:35:04 +1000347{
348 pid_t pid;
349 sigset_t oset, nset;
350 int status;
351
352 /* block SIGCHLD while we check for dead children */
353 sigemptyset(&nset);
354 sigaddset(&nset, SIGCHLD);
355 sigprocmask(SIG_BLOCK, &nset, &oset);
356 if (child_terminated) {
Damien Millerec04f362006-03-15 12:01:34 +1100357 debug("Received SIGCHLD.");
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000358 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
359 (pid < 0 && errno == EINTR))
360 if (pid > 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000361 session_close_by_pid(ssh, pid, status);
Damien Miller3ec27592001-10-12 11:35:04 +1000362 child_terminated = 0;
363 }
364 sigprocmask(SIG_SETMASK, &oset, NULL);
365}
366
Damien Miller4af51302000-04-16 11:18:38 +1000367void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000368server_loop2(struct ssh *ssh, Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +1000369{
Damien Miller5e953212001-01-30 09:14:00 +1100370 fd_set *readset = NULL, *writeset = NULL;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000371 int max_fd;
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000372 u_int nalloc = 0, connection_in, connection_out;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000373 u_int64_t rekey_timeout_ms = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +1000374
375 debug("Entering interactive session for SSH2.");
376
Darren Tucker389125b2018-02-15 21:43:01 +1100377 signal(SIGCHLD, sigchld_handler);
Damien Millerefb4afe2000-04-12 18:45:05 +1000378 child_terminated = 0;
379 connection_in = packet_get_connection_in();
380 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +1100381
Damien Miller24ecf612005-11-05 15:16:52 +1100382 if (!use_privsep) {
383 signal(SIGTERM, sigterm_handler);
384 signal(SIGINT, sigterm_handler);
385 signal(SIGQUIT, sigterm_handler);
386 }
387
Damien Millerf6681a32001-12-21 14:53:11 +1100388 notify_setup();
389
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000390 max_fd = MAXIMUM(connection_in, connection_out);
391 max_fd = MAXIMUM(max_fd, notify_pipe[0]);
Damien Millerf6681a32001-12-21 14:53:11 +1100392
Damien Millerefb4afe2000-04-12 18:45:05 +1000393 server_init_dispatch();
394
395 for (;;) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000396 process_buffered_input_packets(ssh);
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000397
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000398 if (!ssh_packet_is_rekeying(ssh) &&
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000399 packet_not_very_much_data_to_write())
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000400 channel_output_poll(ssh);
401 if (options.rekey_interval > 0 && !ssh_packet_is_rekeying(ssh))
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000402 rekey_timeout_ms = packet_get_rekey_timeout() * 1000;
403 else
404 rekey_timeout_ms = 0;
405
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000406 wait_until_can_do_something(ssh, connection_in, connection_out,
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000407 &readset, &writeset, &max_fd, &nalloc, rekey_timeout_ms);
Damien Miller52b77be2001-10-10 15:14:37 +1000408
Damien Miller24ecf612005-11-05 15:16:52 +1100409 if (received_sigterm) {
Darren Tucker3e1027c2012-12-07 13:07:46 +1100410 logit("Exiting on signal %d", (int)received_sigterm);
Damien Miller24ecf612005-11-05 15:16:52 +1100411 /* Clean up sessions, utmp, etc. */
412 cleanup_exit(255);
413 }
414
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000415 collect_children(ssh);
416 if (!ssh_packet_is_rekeying(ssh))
417 channel_after_select(ssh, readset, writeset);
418 if (process_input(ssh, readset, connection_in) < 0)
Ben Lindstrome34ab4c2001-04-07 01:12:11 +0000419 break;
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000420 process_output(writeset, connection_out);
Damien Millerefb4afe2000-04-12 18:45:05 +1000421 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000422 collect_children(ssh);
Damien Miller3ec27592001-10-12 11:35:04 +1000423
Darren Tuckera627d422013-06-02 07:31:17 +1000424 free(readset);
425 free(writeset);
Damien Miller5e953212001-01-30 09:14:00 +1100426
Damien Miller52b77be2001-10-10 15:14:37 +1000427 /* free all channels, no more reads and writes */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000428 channel_free_all(ssh);
Ben Lindstrom4983d5e2001-07-04 05:17:40 +0000429
Damien Miller3ec27592001-10-12 11:35:04 +1000430 /* free remaining sessions, e.g. remove wtmp entries */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000431 session_destroy_all(ssh, NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +1000432}
433
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000434static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000435server_input_keep_alive(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000436{
Damien Millerb5820f42003-12-17 16:27:32 +1100437 debug("Got %d/%u for keepalive", type, seq);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100438 /*
Ben Lindstrom2f0304c2001-04-29 19:49:14 +0000439 * reset timeout, since we got a sane answer from the client.
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000440 * even if this was generated by something other than
441 * the bogus CHANNEL_REQUEST we send for keepalives.
442 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000443 packet_set_alive_timeouts(0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000444 return 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000445}
446
Ben Lindstrombba81212001-06-25 05:01:22 +0000447static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000448server_request_direct_tcpip(struct ssh *ssh, int *reason, const char **errmsg)
Damien Millerefb4afe2000-04-12 18:45:05 +1000449{
Damien Milleraa5b3f82012-12-03 09:50:54 +1100450 Channel *c = NULL;
Damien Miller4af51302000-04-16 11:18:38 +1000451 char *target, *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100452 u_short target_port, originator_port;
Damien Millerefb4afe2000-04-12 18:45:05 +1000453
Damien Miller4af51302000-04-16 11:18:38 +1000454 target = packet_get_string(NULL);
455 target_port = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +1000456 originator = packet_get_string(NULL);
457 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100458 packet_check_eom();
Damien Millerb1715dc2000-05-30 13:44:51 +1000459
djm@openbsd.org7c856852018-03-03 03:15:51 +0000460 debug("%s: originator %s port %d, target %s port %d", __func__,
461 originator, originator_port, target, target_port);
Damien Millerf6d9e222000-06-18 14:50:44 +1000462
Damien Milleraa5b3f82012-12-03 09:50:54 +1100463 /* XXX fine grained permissions */
464 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 &&
djm@openbsd.org7c856852018-03-03 03:15:51 +0000465 auth_opts->permit_port_forwarding_flag &&
466 !options.disable_forwarding) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000467 c = channel_connect_to_port(ssh, target, target_port,
dtucker@openbsd.org858252f2017-02-01 02:59:09 +0000468 "direct-tcpip", "direct-tcpip", reason, errmsg);
Damien Milleraa5b3f82012-12-03 09:50:54 +1100469 } else {
470 logit("refused local port forward: "
471 "originator %s port %d, target %s port %d",
472 originator, originator_port, target, target_port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +0000473 if (reason != NULL)
474 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Milleraa5b3f82012-12-03 09:50:54 +1100475 }
Damien Millerbd740252008-05-19 15:37:09 +1000476
Darren Tuckera627d422013-06-02 07:31:17 +1000477 free(originator);
478 free(target);
Damien Millerbd740252008-05-19 15:37:09 +1000479
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000480 return c;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100481}
482
Ben Lindstrombba81212001-06-25 05:01:22 +0000483static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000484server_request_direct_streamlocal(struct ssh *ssh)
Damien Miller7acefbb2014-07-18 14:11:24 +1000485{
486 Channel *c = NULL;
487 char *target, *originator;
488 u_short originator_port;
djm@openbsd.org51045862017-01-04 05:37:40 +0000489 struct passwd *pw = the_authctxt->pw;
490
491 if (pw == NULL || !the_authctxt->valid)
djm@openbsd.org7c856852018-03-03 03:15:51 +0000492 fatal("%s: no/invalid user", __func__);
Damien Miller7acefbb2014-07-18 14:11:24 +1000493
494 target = packet_get_string(NULL);
495 originator = packet_get_string(NULL);
496 originator_port = packet_get_int();
497 packet_check_eom();
498
djm@openbsd.org7c856852018-03-03 03:15:51 +0000499 debug("%s: originator %s port %d, target %s", __func__,
Damien Miller7acefbb2014-07-18 14:11:24 +1000500 originator, originator_port, target);
501
502 /* XXX fine grained permissions */
503 if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 &&
djm@openbsd.org7c856852018-03-03 03:15:51 +0000504 auth_opts->permit_port_forwarding_flag &&
505 !options.disable_forwarding && (pw->pw_uid == 0 || use_privsep)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000506 c = channel_connect_to_path(ssh, target,
Damien Miller7acefbb2014-07-18 14:11:24 +1000507 "direct-streamlocal@openssh.com", "direct-streamlocal");
508 } else {
509 logit("refused streamlocal port forward: "
510 "originator %s port %d, target %s",
511 originator, originator_port, target);
512 }
513
514 free(originator);
515 free(target);
516
517 return c;
518}
519
520static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000521server_request_tun(struct ssh *ssh)
Damien Millerd27b9472005-12-13 19:29:02 +1100522{
523 Channel *c = NULL;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000524 int mode, tun, sock;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000525 char *tmp, *ifname = NULL;
Damien Millerd27b9472005-12-13 19:29:02 +1100526
Damien Miller7b58e802005-12-13 19:33:19 +1100527 mode = packet_get_int();
528 switch (mode) {
529 case SSH_TUNMODE_POINTOPOINT:
530 case SSH_TUNMODE_ETHERNET:
531 break;
532 default:
533 packet_send_debug("Unsupported tunnel device mode.");
534 return NULL;
535 }
536 if ((options.permit_tun & mode) == 0) {
537 packet_send_debug("Server has rejected tunnel device "
538 "forwarding");
Damien Millerd27b9472005-12-13 19:29:02 +1100539 return NULL;
540 }
541
542 tun = packet_get_int();
djm@openbsd.org7c856852018-03-03 03:15:51 +0000543 if (auth_opts->force_tun_device != -1) {
544 if (tun != SSH_TUNID_ANY && auth_opts->force_tun_device != tun)
Damien Millerd27b9472005-12-13 19:29:02 +1100545 goto done;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000546 tun = auth_opts->force_tun_device;
Damien Millerd27b9472005-12-13 19:29:02 +1100547 }
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000548 sock = tun_open(tun, mode, &ifname);
Damien Millerd27b9472005-12-13 19:29:02 +1100549 if (sock < 0)
550 goto done;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000551 debug("Tunnel forwarding using interface %s", ifname);
Darren Tucker3c511432018-02-13 09:07:29 +1100552
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000553 c = channel_new(ssh, "tun", SSH_CHANNEL_OPEN, sock, sock, -1,
Damien Millerd27b9472005-12-13 19:29:02 +1100554 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
555 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +1100556#if defined(SSH_TUN_FILTER)
557 if (mode == SSH_TUNMODE_POINTOPOINT)
Damien Miller871f1e42017-09-12 18:01:35 +1000558 channel_register_filter(ssh, c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +1000559 sys_tun_outfilter, NULL, NULL);
Damien Miller598bbc22005-12-31 16:33:36 +1100560#endif
Damien Millerd27b9472005-12-13 19:29:02 +1100561
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000562 /*
563 * Update the list of names exposed to the session
564 * XXX remove these if the tunnels are closed (won't matter
565 * much if they are already in the environment though)
566 */
567 tmp = tun_fwd_ifnames;
568 xasprintf(&tun_fwd_ifnames, "%s%s%s",
569 tun_fwd_ifnames == NULL ? "" : tun_fwd_ifnames,
570 tun_fwd_ifnames == NULL ? "" : ",",
571 ifname);
572 free(tmp);
573 free(ifname);
574
Damien Millerd27b9472005-12-13 19:29:02 +1100575 done:
576 if (c == NULL)
577 packet_send_debug("Failed to open the tunnel device.");
578 return c;
579}
580
581static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000582server_request_session(struct ssh *ssh)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100583{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000584 Channel *c;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100585
586 debug("input_session_request");
Damien Miller48b03fc2002-01-22 23:11:40 +1100587 packet_check_eom();
Darren Tucker8901fa92008-06-11 09:34:01 +1000588
589 if (no_more_sessions) {
590 packet_disconnect("Possible attack: attempt to open a session "
591 "after additional sessions disabled");
592 }
593
Damien Miller0bc1bd82000-11-13 22:57:25 +1100594 /*
595 * A server session has no fd to read or write until a
596 * CHANNEL_REQUEST for a shell is made, so we set the type to
597 * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all
598 * CHANNEL_REQUEST messages is registered.
599 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000600 c = channel_new(ssh, "session", SSH_CHANNEL_LARVAL,
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000601 -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000602 0, "server-session", 1);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000603 if (session_open(the_authctxt, c->self) != 1) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000604 debug("session open failed, free channel %d", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000605 channel_free(ssh, c);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000606 return NULL;
607 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000608 channel_register_cleanup(ssh, c->self, session_close_by_channel, 0);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000609 return c;
Damien Millerefb4afe2000-04-12 18:45:05 +1000610}
611
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000612static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000613server_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerefb4afe2000-04-12 18:45:05 +1000614{
615 Channel *c = NULL;
616 char *ctype;
dtucker@openbsd.org858252f2017-02-01 02:59:09 +0000617 const char *errmsg = NULL;
618 int rchan, reason = SSH2_OPEN_CONNECT_FAILED;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000619 u_int rmaxpack, rwindow, len;
Damien Millerefb4afe2000-04-12 18:45:05 +1000620
621 ctype = packet_get_string(&len);
622 rchan = packet_get_int();
623 rwindow = packet_get_int();
624 rmaxpack = packet_get_int();
625
djm@openbsd.org027607f2018-06-08 01:55:40 +0000626 debug("%s: ctype %s rchan %d win %d max %d", __func__,
Damien Millerefb4afe2000-04-12 18:45:05 +1000627 ctype, rchan, rwindow, rmaxpack);
628
629 if (strcmp(ctype, "session") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000630 c = server_request_session(ssh);
Damien Millerefb4afe2000-04-12 18:45:05 +1000631 } else if (strcmp(ctype, "direct-tcpip") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000632 c = server_request_direct_tcpip(ssh, &reason, &errmsg);
Damien Miller7acefbb2014-07-18 14:11:24 +1000633 } else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000634 c = server_request_direct_streamlocal(ssh);
Damien Millerd27b9472005-12-13 19:29:02 +1100635 } else if (strcmp(ctype, "tun@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000636 c = server_request_tun(ssh);
Damien Millerefb4afe2000-04-12 18:45:05 +1000637 }
638 if (c != NULL) {
djm@openbsd.org027607f2018-06-08 01:55:40 +0000639 debug("%s: confirm %s", __func__, ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000640 c->remote_id = rchan;
djm@openbsd.org9f532292017-09-12 06:35:31 +0000641 c->have_remote_id = 1;
Damien Millerefb4afe2000-04-12 18:45:05 +1000642 c->remote_window = rwindow;
643 c->remote_maxpacket = rmaxpack;
Ben Lindstrom69128662001-05-08 20:07:39 +0000644 if (c->type != SSH_CHANNEL_CONNECTING) {
645 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
646 packet_put_int(c->remote_id);
647 packet_put_int(c->self);
648 packet_put_int(c->local_window);
649 packet_put_int(c->local_maxpacket);
650 packet_send();
651 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000652 } else {
djm@openbsd.org027607f2018-06-08 01:55:40 +0000653 debug("%s: failure %s", __func__, ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000654 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
655 packet_put_int(rchan);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +0000656 packet_put_int(reason);
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000657 packet_put_cstring(errmsg ? errmsg : "open failed");
658 packet_put_cstring("");
Damien Millerefb4afe2000-04-12 18:45:05 +1000659 packet_send();
660 }
Darren Tuckera627d422013-06-02 07:31:17 +1000661 free(ctype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000662 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +1000663}
664
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000665static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000666server_input_hostkeys_prove(struct ssh *ssh, struct sshbuf **respp)
djm@openbsd.org523463a2015-02-16 22:13:32 +0000667{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000668 struct sshbuf *resp = NULL;
669 struct sshbuf *sigbuf = NULL;
670 struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL;
djm@openbsd.org78607312017-12-18 23:16:23 +0000671 int r, ndx, kexsigtype, use_kexsigtype, success = 0;
djm@openbsd.org523463a2015-02-16 22:13:32 +0000672 const u_char *blob;
673 u_char *sig = 0;
674 size_t blen, slen;
675
676 if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL)
677 fatal("%s: sshbuf_new", __func__);
678
djm@openbsd.org78607312017-12-18 23:16:23 +0000679 kexsigtype = sshkey_type_plain(
680 sshkey_type_from_name(ssh->kex->hostkey_alg));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000681 while (ssh_packet_remaining(ssh) > 0) {
682 sshkey_free(key);
683 key = NULL;
684 if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 ||
685 (r = sshkey_from_blob(blob, blen, &key)) != 0) {
686 error("%s: couldn't parse key: %s",
687 __func__, ssh_err(r));
688 goto out;
689 }
690 /*
691 * Better check that this is actually one of our hostkeys
692 * before attempting to sign anything with it.
693 */
694 if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) {
695 error("%s: unknown host %s key",
696 __func__, sshkey_type(key));
697 goto out;
698 }
699 /*
700 * XXX refactor: make kex->sign just use an index rather
701 * than passing in public and private keys
702 */
703 if ((key_prv = get_hostkey_by_index(ndx)) == NULL &&
704 (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) {
705 error("%s: can't retrieve hostkey %d", __func__, ndx);
706 goto out;
707 }
708 sshbuf_reset(sigbuf);
709 free(sig);
710 sig = NULL;
djm@openbsd.org78607312017-12-18 23:16:23 +0000711 /*
712 * For RSA keys, prefer to use the signature type negotiated
713 * during KEX to the default (SHA1).
714 */
715 use_kexsigtype = kexsigtype == KEY_RSA &&
716 sshkey_type_plain(key->type) == KEY_RSA;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000717 if ((r = sshbuf_put_cstring(sigbuf,
718 "hostkeys-prove-00@openssh.com")) != 0 ||
719 (r = sshbuf_put_string(sigbuf,
djm@openbsd.org523463a2015-02-16 22:13:32 +0000720 ssh->kex->session_id, ssh->kex->session_id_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000721 (r = sshkey_puts(key, sigbuf)) != 0 ||
722 (r = ssh->kex->sign(key_prv, key_pub, &sig, &slen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000723 sshbuf_ptr(sigbuf), sshbuf_len(sigbuf),
djm@openbsd.org78607312017-12-18 23:16:23 +0000724 use_kexsigtype ? ssh->kex->hostkey_alg : NULL, 0)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000725 (r = sshbuf_put_string(resp, sig, slen)) != 0) {
726 error("%s: couldn't prepare signature: %s",
727 __func__, ssh_err(r));
728 goto out;
729 }
730 }
731 /* Success */
732 *respp = resp;
733 resp = NULL; /* don't free it */
734 success = 1;
735 out:
736 free(sig);
737 sshbuf_free(resp);
738 sshbuf_free(sigbuf);
739 sshkey_free(key);
740 return success;
741}
742
743static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000744server_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100745{
746 char *rtype;
747 int want_reply;
djm@openbsd.org523463a2015-02-16 22:13:32 +0000748 int r, success = 0, allocated_listen_port = 0;
749 struct sshbuf *resp = NULL;
djm@openbsd.org51045862017-01-04 05:37:40 +0000750 struct passwd *pw = the_authctxt->pw;
751
752 if (pw == NULL || !the_authctxt->valid)
djm@openbsd.org027607f2018-06-08 01:55:40 +0000753 fatal("%s: no/invalid user", __func__);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100754
755 rtype = packet_get_string(NULL);
756 want_reply = packet_get_char();
djm@openbsd.org027607f2018-06-08 01:55:40 +0000757 debug("%s: rtype %s want_reply %d", __func__, rtype, want_reply);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000758
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000759 /* -R style forwarding */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100760 if (strcmp(rtype, "tcpip-forward") == 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000761 struct Forward fwd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100762
Damien Miller7acefbb2014-07-18 14:11:24 +1000763 memset(&fwd, 0, sizeof(fwd));
764 fwd.listen_host = packet_get_string(NULL);
765 fwd.listen_port = (u_short)packet_get_int();
djm@openbsd.org027607f2018-06-08 01:55:40 +0000766 debug("%s: tcpip-forward listen %s port %d", __func__,
Damien Miller7acefbb2014-07-18 14:11:24 +1000767 fwd.listen_host, fwd.listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100768
769 /* check permissions */
Damien Milleraa5b3f82012-12-03 09:50:54 +1100770 if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 ||
djm@openbsd.org7c856852018-03-03 03:15:51 +0000771 !auth_opts->permit_port_forwarding_flag ||
772 options.disable_forwarding ||
Darren Tucker5f41f032016-04-08 21:14:13 +1000773 (!want_reply && fwd.listen_port == 0) ||
dtucker@openbsd.org1c4ef0b2016-10-23 22:04:05 +0000774 (fwd.listen_port != 0 &&
775 !bind_permitted(fwd.listen_port, pw->pw_uid))) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100776 success = 0;
777 packet_send_debug("Server has disabled port forwarding.");
778 } else {
779 /* Start listening on the port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000780 success = channel_setup_remote_fwd_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000781 &allocated_listen_port, &options.fwd_opts);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100782 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000783 free(fwd.listen_host);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000784 if ((resp = sshbuf_new()) == NULL)
785 fatal("%s: sshbuf_new", __func__);
djm@openbsd.orgb1d6b392015-11-28 06:41:03 +0000786 if (allocated_listen_port != 0 &&
787 (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0)
djm@openbsd.org523463a2015-02-16 22:13:32 +0000788 fatal("%s: sshbuf_put_u32: %s", __func__, ssh_err(r));
Darren Tuckere7066df2004-05-24 10:18:05 +1000789 } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000790 struct Forward fwd;
Darren Tuckere7066df2004-05-24 10:18:05 +1000791
Damien Miller7acefbb2014-07-18 14:11:24 +1000792 memset(&fwd, 0, sizeof(fwd));
793 fwd.listen_host = packet_get_string(NULL);
794 fwd.listen_port = (u_short)packet_get_int();
Darren Tuckere7066df2004-05-24 10:18:05 +1000795 debug("%s: cancel-tcpip-forward addr %s port %d", __func__,
Damien Miller7acefbb2014-07-18 14:11:24 +1000796 fwd.listen_host, fwd.listen_port);
Darren Tuckere7066df2004-05-24 10:18:05 +1000797
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000798 success = channel_cancel_rport_listener(ssh, &fwd);
Damien Miller7acefbb2014-07-18 14:11:24 +1000799 free(fwd.listen_host);
800 } else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) {
801 struct Forward fwd;
802
803 memset(&fwd, 0, sizeof(fwd));
804 fwd.listen_path = packet_get_string(NULL);
djm@openbsd.org027607f2018-06-08 01:55:40 +0000805 debug("%s: streamlocal-forward listen path %s", __func__,
Damien Miller7acefbb2014-07-18 14:11:24 +1000806 fwd.listen_path);
807
808 /* check permissions */
809 if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0
djm@openbsd.org7c856852018-03-03 03:15:51 +0000810 || !auth_opts->permit_port_forwarding_flag ||
811 options.disable_forwarding ||
djm@openbsd.org51045862017-01-04 05:37:40 +0000812 (pw->pw_uid != 0 && !use_privsep)) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000813 success = 0;
djm@openbsd.org51045862017-01-04 05:37:40 +0000814 packet_send_debug("Server has disabled "
815 "streamlocal forwarding.");
Damien Miller7acefbb2014-07-18 14:11:24 +1000816 } else {
817 /* Start listening on the socket */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000818 success = channel_setup_remote_fwd_listener(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +1000819 &fwd, NULL, &options.fwd_opts);
820 }
821 free(fwd.listen_path);
822 } else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) {
823 struct Forward fwd;
824
825 memset(&fwd, 0, sizeof(fwd));
826 fwd.listen_path = packet_get_string(NULL);
827 debug("%s: cancel-streamlocal-forward path %s", __func__,
828 fwd.listen_path);
829
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000830 success = channel_cancel_rport_listener(ssh, &fwd);
Damien Miller7acefbb2014-07-18 14:11:24 +1000831 free(fwd.listen_path);
Darren Tucker8901fa92008-06-11 09:34:01 +1000832 } else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) {
833 no_more_sessions = 1;
834 success = 1;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000835 } else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000836 success = server_input_hostkeys_prove(ssh, &resp);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100837 }
838 if (want_reply) {
839 packet_start(success ?
840 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000841 if (success && resp != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000842 ssh_packet_put_raw(ssh, sshbuf_ptr(resp),
djm@openbsd.org523463a2015-02-16 22:13:32 +0000843 sshbuf_len(resp));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100844 packet_send();
845 packet_write_wait();
846 }
Darren Tuckera627d422013-06-02 07:31:17 +1000847 free(rtype);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000848 sshbuf_free(resp);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000849 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100850}
Damien Miller4f7becb2006-03-26 14:10:14 +1100851
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000852static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000853server_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerc7ef63d2002-02-05 12:21:42 +1100854{
855 Channel *c;
856 int id, reply, success = 0;
857 char *rtype;
858
859 id = packet_get_int();
860 rtype = packet_get_string(NULL);
861 reply = packet_get_char();
862
863 debug("server_input_channel_req: channel %d request %s reply %d",
864 id, rtype, reply);
865
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000866 if ((c = channel_lookup(ssh, id)) == NULL)
Damien Millerc7ef63d2002-02-05 12:21:42 +1100867 packet_disconnect("server_input_channel_req: "
868 "unknown channel %d", id);
Damien Millerbab9bd42008-05-19 16:06:47 +1000869 if (!strcmp(rtype, "eow@openssh.com")) {
870 packet_check_eom();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000871 chan_rcvd_eow(ssh, c);
Darren Tucker8810dd42008-07-02 22:32:14 +1000872 } else if ((c->type == SSH_CHANNEL_LARVAL ||
873 c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000874 success = session_input_channel_req(ssh, c, rtype);
Damien Millerc5893782014-05-15 13:48:49 +1000875 if (reply && !(c->flags & CHAN_CLOSE_SENT)) {
djm@openbsd.org9f532292017-09-12 06:35:31 +0000876 if (!c->have_remote_id)
877 fatal("%s: channel %d: no remote_id",
878 __func__, c->self);
Damien Millerc7ef63d2002-02-05 12:21:42 +1100879 packet_start(success ?
880 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
881 packet_put_int(c->remote_id);
882 packet_send();
883 }
Darren Tuckera627d422013-06-02 07:31:17 +1000884 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000885 return 0;
Damien Millerc7ef63d2002-02-05 12:21:42 +1100886}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100887
Ben Lindstrombba81212001-06-25 05:01:22 +0000888static void
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000889server_init_dispatch(void)
Damien Millerefb4afe2000-04-12 18:45:05 +1000890{
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000891 debug("server_init_dispatch");
Damien Millerefb4afe2000-04-12 18:45:05 +1000892 dispatch_init(&dispatch_protocol_error);
893 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
894 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
895 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
896 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
897 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
898 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
899 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Damien Millerc7ef63d2002-02-05 12:21:42 +1100900 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req);
Damien Millerefb4afe2000-04-12 18:45:05 +1000901 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100902 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request);
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000903 /* client_alive */
Damien Miller5a33ec62008-12-08 09:55:02 +1100904 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive);
905 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive);
Damien Millerb5820f42003-12-17 16:27:32 +1100906 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive);
907 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive);
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000908 /* rekeying */
909 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Millerefb4afe2000-04-12 18:45:05 +1000910}