blob: 7e2abd52f7936b7d1c31f57f8c002432b3ebee3c [file] [log] [blame]
dtucker@openbsd.org05046d92018-02-11 21:16:56 +00001/* $OpenBSD: serverloop.c,v 1.204 2018/02/11 21:16:56 dtucker 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;
Damien Miller24ecf612005-11-05 15:16:52 +110085extern int use_privsep;
Ben Lindstrom8ac91062001-04-04 17:57:54 +000086
Darren Tucker8901fa92008-06-11 09:34:01 +100087static int no_more_sessions = 0; /* Disallow further sessions. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Miller5428f641999-11-25 11:54:57 +110089/*
90 * This SIGCHLD kludge is used to detect when the child exits. The server
91 * will exit after that, as soon as forwarded connections have terminated.
92 */
Ben Lindstrombba81212001-06-25 05:01:22 +000093
Ben Lindstrom5e71c542001-12-06 16:48:14 +000094static volatile sig_atomic_t child_terminated = 0; /* The child has terminated. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
Damien Miller24ecf612005-11-05 15:16:52 +110096/* Cleanup on signals (!use_privsep case only) */
97static volatile sig_atomic_t received_sigterm = 0;
98
Ben Lindstrombba81212001-06-25 05:01:22 +000099/* prototypes */
100static void server_init_dispatch(void);
Damien Millerb38eff82000-04-01 11:09:21 +1000101
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000102/* requested tunnel forwarding interface(s), shared with session.c */
103char *tun_fwd_ifnames = NULL;
104
Damien Millerf6681a32001-12-21 14:53:11 +1100105/*
106 * we write to this pipe if a SIGCHLD is caught in order to avoid
107 * the race between select() and child_terminated
108 */
109static int notify_pipe[2];
110static void
111notify_setup(void)
112{
113 if (pipe(notify_pipe) < 0) {
114 error("pipe(notify_pipe) failed %s", strerror(errno));
Damien Miller814ace02011-05-20 19:02:47 +1000115 } else if ((fcntl(notify_pipe[0], F_SETFD, FD_CLOEXEC) == -1) ||
116 (fcntl(notify_pipe[1], F_SETFD, FD_CLOEXEC) == -1)) {
Damien Millerf6681a32001-12-21 14:53:11 +1100117 error("fcntl(notify_pipe, F_SETFD) failed %s", strerror(errno));
118 close(notify_pipe[0]);
119 close(notify_pipe[1]);
120 } else {
121 set_nonblock(notify_pipe[0]);
122 set_nonblock(notify_pipe[1]);
123 return;
124 }
125 notify_pipe[0] = -1; /* read end */
126 notify_pipe[1] = -1; /* write end */
127}
128static void
129notify_parent(void)
130{
131 if (notify_pipe[1] != -1)
Darren Tuckerdbee3082013-05-16 20:32:29 +1000132 (void)write(notify_pipe[1], "", 1);
Damien Millerf6681a32001-12-21 14:53:11 +1100133}
134static void
135notify_prepare(fd_set *readset)
136{
137 if (notify_pipe[0] != -1)
138 FD_SET(notify_pipe[0], readset);
139}
140static void
141notify_done(fd_set *readset)
142{
143 char c;
144
145 if (notify_pipe[0] != -1 && FD_ISSET(notify_pipe[0], readset))
146 while (read(notify_pipe[0], &c, 1) != -1)
147 debug2("notify_done: reading");
148}
149
Damien Millerf0b15df2006-03-26 13:59:20 +1100150/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000151static void
Damien Miller95def091999-11-25 00:26:21 +1100152sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153{
Damien Miller95def091999-11-25 00:26:21 +1100154 int save_errno = errno;
Damien Millerefb4afe2000-04-12 18:45:05 +1000155 child_terminated = 1;
Damien Millerf6681a32001-12-21 14:53:11 +1100156 notify_parent();
Damien Millerefb4afe2000-04-12 18:45:05 +1000157 errno = save_errno;
158}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159
Damien Millerf0b15df2006-03-26 13:59:20 +1100160/*ARGSUSED*/
Damien Miller24ecf612005-11-05 15:16:52 +1100161static void
162sigterm_handler(int sig)
163{
164 received_sigterm = sig;
165}
166
Damien Miller8c3902a2001-10-10 15:01:40 +1000167static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000168client_alive_check(struct ssh *ssh)
Damien Miller8c3902a2001-10-10 15:01:40 +1000169{
Damien Millerb5820f42003-12-17 16:27:32 +1100170 int channel_id;
dtucker@openbsd.org48c23a32017-12-10 05:55:29 +0000171 char remote_id[512];
Damien Miller056cf732002-01-22 23:21:39 +1100172
Damien Miller8c3902a2001-10-10 15:01:40 +1000173 /* timeout, check to see how many we have had */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000174 if (packet_inc_alive_timeouts() > options.client_alive_count_max) {
dtucker@openbsd.org48c23a32017-12-10 05:55:29 +0000175 sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
176 logit("Timeout, client not responding from %s", remote_id);
Damien Miller985a4482006-10-24 03:02:41 +1000177 cleanup_exit(255);
178 }
Damien Miller8c3902a2001-10-10 15:01:40 +1000179
Damien Miller8c3902a2001-10-10 15:01:40 +1000180 /*
Damien Millerb5820f42003-12-17 16:27:32 +1100181 * send a bogus global/channel request with "wantreply",
Damien Miller8c3902a2001-10-10 15:01:40 +1000182 * we should get back a failure
183 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000184 if ((channel_id = channel_find_open(ssh)) == -1) {
Damien Millerb5820f42003-12-17 16:27:32 +1100185 packet_start(SSH2_MSG_GLOBAL_REQUEST);
186 packet_put_cstring("keepalive@openssh.com");
187 packet_put_char(1); /* boolean: want reply */
188 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000189 channel_request_start(ssh, channel_id,
190 "keepalive@openssh.com", 1);
Damien Millerb5820f42003-12-17 16:27:32 +1100191 }
Damien Miller8c3902a2001-10-10 15:01:40 +1000192 packet_send();
193}
194
Damien Miller95def091999-11-25 00:26:21 +1100195/*
196 * Sleep in select() until we can do something. This will initialize the
197 * select masks. Upon return, the masks will indicate which descriptors
198 * have data or can accept data. Optionally, a maximum time can be specified
199 * for the duration of the wait (0 = infinite).
200 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000201static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000202wait_until_can_do_something(struct ssh *ssh,
203 int connection_in, int connection_out,
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000204 fd_set **readsetp, fd_set **writesetp, int *maxfdp,
djm@openbsd.org988e4292016-03-04 03:35:44 +0000205 u_int *nallocp, u_int64_t max_time_ms)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206{
Damien Miller95def091999-11-25 00:26:21 +1100207 struct timeval tv, *tvp;
208 int ret;
Damien Miller6c6da332012-06-20 22:31:26 +1000209 time_t minwait_secs = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000210 int client_alive_scheduled = 0;
dtucker@openbsd.orgb60ff202017-08-11 03:58:36 +0000211 static time_t last_client_time;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000212
Damien Millera6508752012-04-22 11:21:10 +1000213 /* Allocate and update select() masks for channel descriptors. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000214 channel_prepare_select(ssh, readsetp, writesetp, maxfdp,
djm@openbsd.org71e5a532017-08-30 03:59:08 +0000215 nallocp, &minwait_secs);
Damien Millera6508752012-04-22 11:21:10 +1000216
djm@openbsd.org988e4292016-03-04 03:35:44 +0000217 /* XXX need proper deadline system for rekey/client alive */
Damien Millera6508752012-04-22 11:21:10 +1000218 if (minwait_secs != 0)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000219 max_time_ms = MINIMUM(max_time_ms, (u_int)minwait_secs * 1000);
Damien Millera6508752012-04-22 11:21:10 +1000220
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000221 /*
Damien Miller9f0f5c62001-12-21 14:45:46 +1100222 * if using client_alive, set the max timeout accordingly,
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000223 * and indicate that this particular timeout was for client
224 * alive by setting the client_alive_scheduled flag.
225 *
226 * this could be randomized somewhat to make traffic
Damien Miller9f0f5c62001-12-21 14:45:46 +1100227 * analysis more difficult, but we're not doing it yet.
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000228 */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000229 if (options.client_alive_interval) {
djm@openbsd.org988e4292016-03-04 03:35:44 +0000230 uint64_t keepalive_ms =
231 (uint64_t)options.client_alive_interval * 1000;
232
Ben Lindstrom2f0304c2001-04-29 19:49:14 +0000233 client_alive_scheduled = 1;
djm@openbsd.org988e4292016-03-04 03:35:44 +0000234 if (max_time_ms == 0 || max_time_ms > keepalive_ms)
235 max_time_ms = keepalive_ms;
Ben Lindstrom36857f62001-07-18 15:48:57 +0000236 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237
Damien Milleraf5f2e62001-10-10 15:01:16 +1000238#if 0
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000239 /* wrong: bad condition XXX */
240 if (channel_not_very_much_buffered_data())
Damien Milleraf5f2e62001-10-10 15:01:16 +1000241#endif
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000242 FD_SET(connection_in, *readsetp);
Damien Millerf6681a32001-12-21 14:53:11 +1100243 notify_prepare(*readsetp);
Damien Miller95def091999-11-25 00:26:21 +1100244
Damien Miller5428f641999-11-25 11:54:57 +1100245 /*
246 * If we have buffered packet data going to the client, mark that
247 * descriptor.
248 */
Damien Miller95def091999-11-25 00:26:21 +1100249 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100250 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100251
Damien Miller5428f641999-11-25 11:54:57 +1100252 /*
253 * If child has terminated and there is enough buffer space to read
254 * from it, then read as much as is available and exit.
255 */
Damien Miller95def091999-11-25 00:26:21 +1100256 if (child_terminated && packet_not_very_much_data_to_write())
djm@openbsd.org988e4292016-03-04 03:35:44 +0000257 if (max_time_ms == 0 || client_alive_scheduled)
258 max_time_ms = 100;
Damien Miller95def091999-11-25 00:26:21 +1100259
djm@openbsd.org988e4292016-03-04 03:35:44 +0000260 if (max_time_ms == 0)
Damien Miller95def091999-11-25 00:26:21 +1100261 tvp = NULL;
262 else {
djm@openbsd.org988e4292016-03-04 03:35:44 +0000263 tv.tv_sec = max_time_ms / 1000;
264 tv.tv_usec = 1000 * (max_time_ms % 1000);
Damien Miller95def091999-11-25 00:26:21 +1100265 tvp = &tv;
266 }
267
268 /* Wait for something to happen, or the timeout to expire. */
Damien Miller5e953212001-01-30 09:14:00 +1100269 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
Damien Miller95def091999-11-25 00:26:21 +1100270
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000271 if (ret == -1) {
Damien Miller79faeff2001-11-12 11:06:32 +1100272 memset(*readsetp, 0, *nallocp);
273 memset(*writesetp, 0, *nallocp);
Damien Miller95def091999-11-25 00:26:21 +1100274 if (errno != EINTR)
275 error("select: %.100s", strerror(errno));
dtucker@openbsd.orgb60ff202017-08-11 03:58:36 +0000276 } else if (client_alive_scheduled) {
277 time_t now = monotime();
278
279 if (ret == 0) { /* timeout */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000280 client_alive_check(ssh);
dtucker@openbsd.orgb60ff202017-08-11 03:58:36 +0000281 } else if (FD_ISSET(connection_in, *readsetp)) {
282 last_client_time = now;
283 } else if (last_client_time != 0 && last_client_time +
dtucker@openbsd.org42a8f8b2017-08-11 04:16:35 +0000284 options.client_alive_interval <= now) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000285 client_alive_check(ssh);
dtucker@openbsd.orgb60ff202017-08-11 03:58:36 +0000286 last_client_time = now;
287 }
288 }
Damien Millerf6681a32001-12-21 14:53:11 +1100289
290 notify_done(*readsetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291}
292
Damien Miller95def091999-11-25 00:26:21 +1100293/*
294 * Processes input from the client and the program. Input data is stored
295 * in buffers and processed later.
296 */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000297static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000298process_input(struct ssh *ssh, fd_set *readset, int connection_in)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299{
Damien Miller95def091999-11-25 00:26:21 +1100300 int len;
301 char buf[16384];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302
Damien Miller95def091999-11-25 00:26:21 +1100303 /* Read and buffer any input data from the client. */
304 if (FD_ISSET(connection_in, readset)) {
markus@openbsd.orga3068632016-01-14 16:17:39 +0000305 len = read(connection_in, buf, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +1100306 if (len == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +0000307 verbose("Connection closed by %.100s port %d",
308 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000309 return -1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000310 } else if (len < 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +1000311 if (errno != EINTR && errno != EAGAIN &&
312 errno != EWOULDBLOCK) {
Damien Miller16aed052002-09-22 01:26:27 +1000313 verbose("Read error from remote host "
djm@openbsd.org95767262016-03-07 19:02:43 +0000314 "%.100s port %d: %.100s",
315 ssh_remote_ipaddr(ssh),
316 ssh_remote_port(ssh), strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000317 cleanup_exit(255);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000318 }
319 } else {
320 /* Buffer any received data. */
321 packet_process_incoming(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100322 }
Damien Miller95def091999-11-25 00:26:21 +1100323 }
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000324 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100325}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326
Damien Miller95def091999-11-25 00:26:21 +1100327/*
328 * Sends data from internal buffers to client program stdin.
329 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000330static void
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000331process_output(fd_set *writeset, int connection_out)
Damien Miller95def091999-11-25 00:26:21 +1100332{
Damien Miller95def091999-11-25 00:26:21 +1100333 /* Send any buffered packet data to the client. */
334 if (FD_ISSET(connection_out, writeset))
335 packet_write_poll();
336}
337
Ben Lindstrombba81212001-06-25 05:01:22 +0000338static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000339process_buffered_input_packets(struct ssh *ssh)
Damien Millerb38eff82000-04-01 11:09:21 +1000340{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000341 ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000342}
343
Damien Miller3ec27592001-10-12 11:35:04 +1000344static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000345collect_children(struct ssh *ssh)
Damien Miller3ec27592001-10-12 11:35:04 +1000346{
347 pid_t pid;
348 sigset_t oset, nset;
349 int status;
350
351 /* block SIGCHLD while we check for dead children */
352 sigemptyset(&nset);
353 sigaddset(&nset, SIGCHLD);
354 sigprocmask(SIG_BLOCK, &nset, &oset);
355 if (child_terminated) {
Damien Millerec04f362006-03-15 12:01:34 +1100356 debug("Received SIGCHLD.");
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000357 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
358 (pid < 0 && errno == EINTR))
359 if (pid > 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000360 session_close_by_pid(ssh, pid, status);
Damien Miller3ec27592001-10-12 11:35:04 +1000361 child_terminated = 0;
362 }
363 sigprocmask(SIG_SETMASK, &oset, NULL);
364}
365
Damien Miller4af51302000-04-16 11:18:38 +1000366void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000367server_loop2(struct ssh *ssh, Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +1000368{
Damien Miller5e953212001-01-30 09:14:00 +1100369 fd_set *readset = NULL, *writeset = NULL;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000370 int max_fd;
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000371 u_int nalloc = 0, connection_in, connection_out;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000372 u_int64_t rekey_timeout_ms = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +1000373
374 debug("Entering interactive session for SSH2.");
375
Darren Tucker389125b2018-02-15 21:43:01 +1100376 signal(SIGCHLD, sigchld_handler);
Damien Millerefb4afe2000-04-12 18:45:05 +1000377 child_terminated = 0;
378 connection_in = packet_get_connection_in();
379 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +1100380
Damien Miller24ecf612005-11-05 15:16:52 +1100381 if (!use_privsep) {
382 signal(SIGTERM, sigterm_handler);
383 signal(SIGINT, sigterm_handler);
384 signal(SIGQUIT, sigterm_handler);
385 }
386
Damien Millerf6681a32001-12-21 14:53:11 +1100387 notify_setup();
388
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000389 max_fd = MAXIMUM(connection_in, connection_out);
390 max_fd = MAXIMUM(max_fd, notify_pipe[0]);
Damien Millerf6681a32001-12-21 14:53:11 +1100391
Damien Millerefb4afe2000-04-12 18:45:05 +1000392 server_init_dispatch();
393
394 for (;;) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000395 process_buffered_input_packets(ssh);
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000396
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000397 if (!ssh_packet_is_rekeying(ssh) &&
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000398 packet_not_very_much_data_to_write())
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000399 channel_output_poll(ssh);
400 if (options.rekey_interval > 0 && !ssh_packet_is_rekeying(ssh))
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000401 rekey_timeout_ms = packet_get_rekey_timeout() * 1000;
402 else
403 rekey_timeout_ms = 0;
404
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000405 wait_until_can_do_something(ssh, connection_in, connection_out,
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000406 &readset, &writeset, &max_fd, &nalloc, rekey_timeout_ms);
Damien Miller52b77be2001-10-10 15:14:37 +1000407
Damien Miller24ecf612005-11-05 15:16:52 +1100408 if (received_sigterm) {
Darren Tucker3e1027c2012-12-07 13:07:46 +1100409 logit("Exiting on signal %d", (int)received_sigterm);
Damien Miller24ecf612005-11-05 15:16:52 +1100410 /* Clean up sessions, utmp, etc. */
411 cleanup_exit(255);
412 }
413
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000414 collect_children(ssh);
415 if (!ssh_packet_is_rekeying(ssh))
416 channel_after_select(ssh, readset, writeset);
417 if (process_input(ssh, readset, connection_in) < 0)
Ben Lindstrome34ab4c2001-04-07 01:12:11 +0000418 break;
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000419 process_output(writeset, connection_out);
Damien Millerefb4afe2000-04-12 18:45:05 +1000420 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000421 collect_children(ssh);
Damien Miller3ec27592001-10-12 11:35:04 +1000422
Darren Tuckera627d422013-06-02 07:31:17 +1000423 free(readset);
424 free(writeset);
Damien Miller5e953212001-01-30 09:14:00 +1100425
Damien Miller52b77be2001-10-10 15:14:37 +1000426 /* free all channels, no more reads and writes */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000427 channel_free_all(ssh);
Ben Lindstrom4983d5e2001-07-04 05:17:40 +0000428
Damien Miller3ec27592001-10-12 11:35:04 +1000429 /* free remaining sessions, e.g. remove wtmp entries */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000430 session_destroy_all(ssh, NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +1000431}
432
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000433static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000434server_input_keep_alive(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000435{
Damien Millerb5820f42003-12-17 16:27:32 +1100436 debug("Got %d/%u for keepalive", type, seq);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100437 /*
Ben Lindstrom2f0304c2001-04-29 19:49:14 +0000438 * reset timeout, since we got a sane answer from the client.
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000439 * even if this was generated by something other than
440 * the bogus CHANNEL_REQUEST we send for keepalives.
441 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000442 packet_set_alive_timeouts(0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000443 return 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000444}
445
Ben Lindstrombba81212001-06-25 05:01:22 +0000446static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000447server_request_direct_tcpip(struct ssh *ssh, int *reason, const char **errmsg)
Damien Millerefb4afe2000-04-12 18:45:05 +1000448{
Damien Milleraa5b3f82012-12-03 09:50:54 +1100449 Channel *c = NULL;
Damien Miller4af51302000-04-16 11:18:38 +1000450 char *target, *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100451 u_short target_port, originator_port;
Damien Millerefb4afe2000-04-12 18:45:05 +1000452
Damien Miller4af51302000-04-16 11:18:38 +1000453 target = packet_get_string(NULL);
454 target_port = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +1000455 originator = packet_get_string(NULL);
456 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100457 packet_check_eom();
Damien Millerb1715dc2000-05-30 13:44:51 +1000458
Damien Millerbd740252008-05-19 15:37:09 +1000459 debug("server_request_direct_tcpip: originator %s port %d, target %s "
460 "port %d", originator, originator_port, target, target_port);
Damien Millerf6d9e222000-06-18 14:50:44 +1000461
Damien Milleraa5b3f82012-12-03 09:50:54 +1100462 /* XXX fine grained permissions */
463 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 &&
djm@openbsd.org7844f352016-11-30 03:00:05 +0000464 !no_port_forwarding_flag && !options.disable_forwarding) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000465 c = channel_connect_to_port(ssh, target, target_port,
dtucker@openbsd.org858252f2017-02-01 02:59:09 +0000466 "direct-tcpip", "direct-tcpip", reason, errmsg);
Damien Milleraa5b3f82012-12-03 09:50:54 +1100467 } else {
468 logit("refused local port forward: "
469 "originator %s port %d, target %s port %d",
470 originator, originator_port, target, target_port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +0000471 if (reason != NULL)
472 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Milleraa5b3f82012-12-03 09:50:54 +1100473 }
Damien Millerbd740252008-05-19 15:37:09 +1000474
Darren Tuckera627d422013-06-02 07:31:17 +1000475 free(originator);
476 free(target);
Damien Millerbd740252008-05-19 15:37:09 +1000477
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000478 return c;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100479}
480
Ben Lindstrombba81212001-06-25 05:01:22 +0000481static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000482server_request_direct_streamlocal(struct ssh *ssh)
Damien Miller7acefbb2014-07-18 14:11:24 +1000483{
484 Channel *c = NULL;
485 char *target, *originator;
486 u_short originator_port;
djm@openbsd.org51045862017-01-04 05:37:40 +0000487 struct passwd *pw = the_authctxt->pw;
488
489 if (pw == NULL || !the_authctxt->valid)
490 fatal("server_input_global_request: no/invalid user");
Damien Miller7acefbb2014-07-18 14:11:24 +1000491
492 target = packet_get_string(NULL);
493 originator = packet_get_string(NULL);
494 originator_port = packet_get_int();
495 packet_check_eom();
496
497 debug("server_request_direct_streamlocal: originator %s port %d, target %s",
498 originator, originator_port, target);
499
500 /* XXX fine grained permissions */
501 if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 &&
djm@openbsd.orgb737e4d2016-12-14 00:36:34 +0000502 !no_port_forwarding_flag && !options.disable_forwarding &&
djm@openbsd.org51045862017-01-04 05:37:40 +0000503 (pw->pw_uid == 0 || use_privsep)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000504 c = channel_connect_to_path(ssh, target,
Damien Miller7acefbb2014-07-18 14:11:24 +1000505 "direct-streamlocal@openssh.com", "direct-streamlocal");
506 } else {
507 logit("refused streamlocal port forward: "
508 "originator %s port %d, target %s",
509 originator, originator_port, target);
510 }
511
512 free(originator);
513 free(target);
514
515 return c;
516}
517
518static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000519server_request_tun(struct ssh *ssh)
Damien Millerd27b9472005-12-13 19:29:02 +1100520{
521 Channel *c = NULL;
Damien Miller7b58e802005-12-13 19:33:19 +1100522 int mode, tun;
523 int sock;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000524 char *tmp, *ifname = NULL;
Damien Millerd27b9472005-12-13 19:29:02 +1100525
Damien Miller7b58e802005-12-13 19:33:19 +1100526 mode = packet_get_int();
527 switch (mode) {
528 case SSH_TUNMODE_POINTOPOINT:
529 case SSH_TUNMODE_ETHERNET:
530 break;
531 default:
532 packet_send_debug("Unsupported tunnel device mode.");
533 return NULL;
534 }
535 if ((options.permit_tun & mode) == 0) {
536 packet_send_debug("Server has rejected tunnel device "
537 "forwarding");
Damien Millerd27b9472005-12-13 19:29:02 +1100538 return NULL;
539 }
540
541 tun = packet_get_int();
Darren Tucker0d0e8f02005-12-20 16:08:42 +1100542 if (forced_tun_device != -1) {
Damien Millerf0b15df2006-03-26 13:59:20 +1100543 if (tun != SSH_TUNID_ANY && forced_tun_device != tun)
Damien Millerd27b9472005-12-13 19:29:02 +1100544 goto done;
545 tun = forced_tun_device;
546 }
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000547 sock = tun_open(tun, mode, &ifname);
Damien Millerd27b9472005-12-13 19:29:02 +1100548 if (sock < 0)
549 goto done;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000550 debug("Tunnel forwarding using interface %s", ifname);
Darren Tucker3c511432018-02-13 09:07:29 +1100551
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000552 c = channel_new(ssh, "tun", SSH_CHANNEL_OPEN, sock, sock, -1,
Damien Millerd27b9472005-12-13 19:29:02 +1100553 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
554 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +1100555#if defined(SSH_TUN_FILTER)
556 if (mode == SSH_TUNMODE_POINTOPOINT)
Damien Miller871f1e42017-09-12 18:01:35 +1000557 channel_register_filter(ssh, c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +1000558 sys_tun_outfilter, NULL, NULL);
Damien Miller598bbc22005-12-31 16:33:36 +1100559#endif
Damien Millerd27b9472005-12-13 19:29:02 +1100560
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000561 /*
562 * Update the list of names exposed to the session
563 * XXX remove these if the tunnels are closed (won't matter
564 * much if they are already in the environment though)
565 */
566 tmp = tun_fwd_ifnames;
567 xasprintf(&tun_fwd_ifnames, "%s%s%s",
568 tun_fwd_ifnames == NULL ? "" : tun_fwd_ifnames,
569 tun_fwd_ifnames == NULL ? "" : ",",
570 ifname);
571 free(tmp);
572 free(ifname);
573
Damien Millerd27b9472005-12-13 19:29:02 +1100574 done:
575 if (c == NULL)
576 packet_send_debug("Failed to open the tunnel device.");
577 return c;
578}
579
580static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000581server_request_session(struct ssh *ssh)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100582{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000583 Channel *c;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100584
585 debug("input_session_request");
Damien Miller48b03fc2002-01-22 23:11:40 +1100586 packet_check_eom();
Darren Tucker8901fa92008-06-11 09:34:01 +1000587
588 if (no_more_sessions) {
589 packet_disconnect("Possible attack: attempt to open a session "
590 "after additional sessions disabled");
591 }
592
Damien Miller0bc1bd82000-11-13 22:57:25 +1100593 /*
594 * A server session has no fd to read or write until a
595 * CHANNEL_REQUEST for a shell is made, so we set the type to
596 * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all
597 * CHANNEL_REQUEST messages is registered.
598 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000599 c = channel_new(ssh, "session", SSH_CHANNEL_LARVAL,
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000600 -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000601 0, "server-session", 1);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000602 if (session_open(the_authctxt, c->self) != 1) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000603 debug("session open failed, free channel %d", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000604 channel_free(ssh, c);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000605 return NULL;
606 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000607 channel_register_cleanup(ssh, c->self, session_close_by_channel, 0);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000608 return c;
Damien Millerefb4afe2000-04-12 18:45:05 +1000609}
610
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000611static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000612server_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerefb4afe2000-04-12 18:45:05 +1000613{
614 Channel *c = NULL;
615 char *ctype;
dtucker@openbsd.org858252f2017-02-01 02:59:09 +0000616 const char *errmsg = NULL;
617 int rchan, reason = SSH2_OPEN_CONNECT_FAILED;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000618 u_int rmaxpack, rwindow, len;
Damien Millerefb4afe2000-04-12 18:45:05 +1000619
620 ctype = packet_get_string(&len);
621 rchan = packet_get_int();
622 rwindow = packet_get_int();
623 rmaxpack = packet_get_int();
624
Damien Miller62cee002000-09-23 17:15:56 +1100625 debug("server_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerefb4afe2000-04-12 18:45:05 +1000626 ctype, rchan, rwindow, rmaxpack);
627
628 if (strcmp(ctype, "session") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000629 c = server_request_session(ssh);
Damien Millerefb4afe2000-04-12 18:45:05 +1000630 } else if (strcmp(ctype, "direct-tcpip") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000631 c = server_request_direct_tcpip(ssh, &reason, &errmsg);
Damien Miller7acefbb2014-07-18 14:11:24 +1000632 } else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000633 c = server_request_direct_streamlocal(ssh);
Damien Millerd27b9472005-12-13 19:29:02 +1100634 } else if (strcmp(ctype, "tun@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000635 c = server_request_tun(ssh);
Damien Millerefb4afe2000-04-12 18:45:05 +1000636 }
637 if (c != NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100638 debug("server_input_channel_open: confirm %s", ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000639 c->remote_id = rchan;
djm@openbsd.org9f532292017-09-12 06:35:31 +0000640 c->have_remote_id = 1;
Damien Millerefb4afe2000-04-12 18:45:05 +1000641 c->remote_window = rwindow;
642 c->remote_maxpacket = rmaxpack;
Ben Lindstrom69128662001-05-08 20:07:39 +0000643 if (c->type != SSH_CHANNEL_CONNECTING) {
644 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
645 packet_put_int(c->remote_id);
646 packet_put_int(c->self);
647 packet_put_int(c->local_window);
648 packet_put_int(c->local_maxpacket);
649 packet_send();
650 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000651 } else {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100652 debug("server_input_channel_open: failure %s", ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000653 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
654 packet_put_int(rchan);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +0000655 packet_put_int(reason);
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000656 packet_put_cstring(errmsg ? errmsg : "open failed");
657 packet_put_cstring("");
Damien Millerefb4afe2000-04-12 18:45:05 +1000658 packet_send();
659 }
Darren Tuckera627d422013-06-02 07:31:17 +1000660 free(ctype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000661 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +1000662}
663
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000664static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000665server_input_hostkeys_prove(struct ssh *ssh, struct sshbuf **respp)
djm@openbsd.org523463a2015-02-16 22:13:32 +0000666{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000667 struct sshbuf *resp = NULL;
668 struct sshbuf *sigbuf = NULL;
669 struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL;
djm@openbsd.org78607312017-12-18 23:16:23 +0000670 int r, ndx, kexsigtype, use_kexsigtype, success = 0;
djm@openbsd.org523463a2015-02-16 22:13:32 +0000671 const u_char *blob;
672 u_char *sig = 0;
673 size_t blen, slen;
674
675 if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL)
676 fatal("%s: sshbuf_new", __func__);
677
djm@openbsd.org78607312017-12-18 23:16:23 +0000678 kexsigtype = sshkey_type_plain(
679 sshkey_type_from_name(ssh->kex->hostkey_alg));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000680 while (ssh_packet_remaining(ssh) > 0) {
681 sshkey_free(key);
682 key = NULL;
683 if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 ||
684 (r = sshkey_from_blob(blob, blen, &key)) != 0) {
685 error("%s: couldn't parse key: %s",
686 __func__, ssh_err(r));
687 goto out;
688 }
689 /*
690 * Better check that this is actually one of our hostkeys
691 * before attempting to sign anything with it.
692 */
693 if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) {
694 error("%s: unknown host %s key",
695 __func__, sshkey_type(key));
696 goto out;
697 }
698 /*
699 * XXX refactor: make kex->sign just use an index rather
700 * than passing in public and private keys
701 */
702 if ((key_prv = get_hostkey_by_index(ndx)) == NULL &&
703 (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) {
704 error("%s: can't retrieve hostkey %d", __func__, ndx);
705 goto out;
706 }
707 sshbuf_reset(sigbuf);
708 free(sig);
709 sig = NULL;
djm@openbsd.org78607312017-12-18 23:16:23 +0000710 /*
711 * For RSA keys, prefer to use the signature type negotiated
712 * during KEX to the default (SHA1).
713 */
714 use_kexsigtype = kexsigtype == KEY_RSA &&
715 sshkey_type_plain(key->type) == KEY_RSA;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000716 if ((r = sshbuf_put_cstring(sigbuf,
717 "hostkeys-prove-00@openssh.com")) != 0 ||
718 (r = sshbuf_put_string(sigbuf,
djm@openbsd.org523463a2015-02-16 22:13:32 +0000719 ssh->kex->session_id, ssh->kex->session_id_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000720 (r = sshkey_puts(key, sigbuf)) != 0 ||
721 (r = ssh->kex->sign(key_prv, key_pub, &sig, &slen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000722 sshbuf_ptr(sigbuf), sshbuf_len(sigbuf),
djm@openbsd.org78607312017-12-18 23:16:23 +0000723 use_kexsigtype ? ssh->kex->hostkey_alg : NULL, 0)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000724 (r = sshbuf_put_string(resp, sig, slen)) != 0) {
725 error("%s: couldn't prepare signature: %s",
726 __func__, ssh_err(r));
727 goto out;
728 }
729 }
730 /* Success */
731 *respp = resp;
732 resp = NULL; /* don't free it */
733 success = 1;
734 out:
735 free(sig);
736 sshbuf_free(resp);
737 sshbuf_free(sigbuf);
738 sshkey_free(key);
739 return success;
740}
741
742static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000743server_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100744{
745 char *rtype;
746 int want_reply;
djm@openbsd.org523463a2015-02-16 22:13:32 +0000747 int r, success = 0, allocated_listen_port = 0;
748 struct sshbuf *resp = NULL;
djm@openbsd.org51045862017-01-04 05:37:40 +0000749 struct passwd *pw = the_authctxt->pw;
750
751 if (pw == NULL || !the_authctxt->valid)
752 fatal("server_input_global_request: no/invalid user");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100753
754 rtype = packet_get_string(NULL);
755 want_reply = packet_get_char();
756 debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000757
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000758 /* -R style forwarding */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100759 if (strcmp(rtype, "tcpip-forward") == 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000760 struct Forward fwd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100761
Damien Miller7acefbb2014-07-18 14:11:24 +1000762 memset(&fwd, 0, sizeof(fwd));
763 fwd.listen_host = packet_get_string(NULL);
764 fwd.listen_port = (u_short)packet_get_int();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100765 debug("server_input_global_request: tcpip-forward listen %s port %d",
Damien Miller7acefbb2014-07-18 14:11:24 +1000766 fwd.listen_host, fwd.listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100767
768 /* check permissions */
Damien Milleraa5b3f82012-12-03 09:50:54 +1100769 if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 ||
djm@openbsd.org7844f352016-11-30 03:00:05 +0000770 no_port_forwarding_flag || options.disable_forwarding ||
Darren Tucker5f41f032016-04-08 21:14:13 +1000771 (!want_reply && fwd.listen_port == 0) ||
dtucker@openbsd.org1c4ef0b2016-10-23 22:04:05 +0000772 (fwd.listen_port != 0 &&
773 !bind_permitted(fwd.listen_port, pw->pw_uid))) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100774 success = 0;
775 packet_send_debug("Server has disabled port forwarding.");
776 } else {
777 /* Start listening on the port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000778 success = channel_setup_remote_fwd_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000779 &allocated_listen_port, &options.fwd_opts);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100780 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000781 free(fwd.listen_host);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000782 if ((resp = sshbuf_new()) == NULL)
783 fatal("%s: sshbuf_new", __func__);
djm@openbsd.orgb1d6b392015-11-28 06:41:03 +0000784 if (allocated_listen_port != 0 &&
785 (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0)
djm@openbsd.org523463a2015-02-16 22:13:32 +0000786 fatal("%s: sshbuf_put_u32: %s", __func__, ssh_err(r));
Darren Tuckere7066df2004-05-24 10:18:05 +1000787 } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000788 struct Forward fwd;
Darren Tuckere7066df2004-05-24 10:18:05 +1000789
Damien Miller7acefbb2014-07-18 14:11:24 +1000790 memset(&fwd, 0, sizeof(fwd));
791 fwd.listen_host = packet_get_string(NULL);
792 fwd.listen_port = (u_short)packet_get_int();
Darren Tuckere7066df2004-05-24 10:18:05 +1000793 debug("%s: cancel-tcpip-forward addr %s port %d", __func__,
Damien Miller7acefbb2014-07-18 14:11:24 +1000794 fwd.listen_host, fwd.listen_port);
Darren Tuckere7066df2004-05-24 10:18:05 +1000795
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000796 success = channel_cancel_rport_listener(ssh, &fwd);
Damien Miller7acefbb2014-07-18 14:11:24 +1000797 free(fwd.listen_host);
798 } else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) {
799 struct Forward fwd;
800
801 memset(&fwd, 0, sizeof(fwd));
802 fwd.listen_path = packet_get_string(NULL);
803 debug("server_input_global_request: streamlocal-forward listen path %s",
804 fwd.listen_path);
805
806 /* check permissions */
807 if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0
djm@openbsd.orgb737e4d2016-12-14 00:36:34 +0000808 || no_port_forwarding_flag || options.disable_forwarding ||
djm@openbsd.org51045862017-01-04 05:37:40 +0000809 (pw->pw_uid != 0 && !use_privsep)) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000810 success = 0;
djm@openbsd.org51045862017-01-04 05:37:40 +0000811 packet_send_debug("Server has disabled "
812 "streamlocal forwarding.");
Damien Miller7acefbb2014-07-18 14:11:24 +1000813 } else {
814 /* Start listening on the socket */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000815 success = channel_setup_remote_fwd_listener(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +1000816 &fwd, NULL, &options.fwd_opts);
817 }
818 free(fwd.listen_path);
819 } else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) {
820 struct Forward fwd;
821
822 memset(&fwd, 0, sizeof(fwd));
823 fwd.listen_path = packet_get_string(NULL);
824 debug("%s: cancel-streamlocal-forward path %s", __func__,
825 fwd.listen_path);
826
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000827 success = channel_cancel_rport_listener(ssh, &fwd);
Damien Miller7acefbb2014-07-18 14:11:24 +1000828 free(fwd.listen_path);
Darren Tucker8901fa92008-06-11 09:34:01 +1000829 } else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) {
830 no_more_sessions = 1;
831 success = 1;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000832 } else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000833 success = server_input_hostkeys_prove(ssh, &resp);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100834 }
835 if (want_reply) {
836 packet_start(success ?
837 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000838 if (success && resp != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000839 ssh_packet_put_raw(ssh, sshbuf_ptr(resp),
djm@openbsd.org523463a2015-02-16 22:13:32 +0000840 sshbuf_len(resp));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100841 packet_send();
842 packet_write_wait();
843 }
Darren Tuckera627d422013-06-02 07:31:17 +1000844 free(rtype);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000845 sshbuf_free(resp);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000846 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100847}
Damien Miller4f7becb2006-03-26 14:10:14 +1100848
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000849static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000850server_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerc7ef63d2002-02-05 12:21:42 +1100851{
852 Channel *c;
853 int id, reply, success = 0;
854 char *rtype;
855
856 id = packet_get_int();
857 rtype = packet_get_string(NULL);
858 reply = packet_get_char();
859
860 debug("server_input_channel_req: channel %d request %s reply %d",
861 id, rtype, reply);
862
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000863 if ((c = channel_lookup(ssh, id)) == NULL)
Damien Millerc7ef63d2002-02-05 12:21:42 +1100864 packet_disconnect("server_input_channel_req: "
865 "unknown channel %d", id);
Damien Millerbab9bd42008-05-19 16:06:47 +1000866 if (!strcmp(rtype, "eow@openssh.com")) {
867 packet_check_eom();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000868 chan_rcvd_eow(ssh, c);
Darren Tucker8810dd42008-07-02 22:32:14 +1000869 } else if ((c->type == SSH_CHANNEL_LARVAL ||
870 c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000871 success = session_input_channel_req(ssh, c, rtype);
Damien Millerc5893782014-05-15 13:48:49 +1000872 if (reply && !(c->flags & CHAN_CLOSE_SENT)) {
djm@openbsd.org9f532292017-09-12 06:35:31 +0000873 if (!c->have_remote_id)
874 fatal("%s: channel %d: no remote_id",
875 __func__, c->self);
Damien Millerc7ef63d2002-02-05 12:21:42 +1100876 packet_start(success ?
877 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
878 packet_put_int(c->remote_id);
879 packet_send();
880 }
Darren Tuckera627d422013-06-02 07:31:17 +1000881 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000882 return 0;
Damien Millerc7ef63d2002-02-05 12:21:42 +1100883}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100884
Ben Lindstrombba81212001-06-25 05:01:22 +0000885static void
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000886server_init_dispatch(void)
Damien Millerefb4afe2000-04-12 18:45:05 +1000887{
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000888 debug("server_init_dispatch");
Damien Millerefb4afe2000-04-12 18:45:05 +1000889 dispatch_init(&dispatch_protocol_error);
890 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
891 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
892 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
893 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
894 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
895 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
896 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Damien Millerc7ef63d2002-02-05 12:21:42 +1100897 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req);
Damien Millerefb4afe2000-04-12 18:45:05 +1000898 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100899 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request);
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000900 /* client_alive */
Damien Miller5a33ec62008-12-08 09:55:02 +1100901 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive);
902 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive);
Damien Millerb5820f42003-12-17 16:27:32 +1100903 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive);
904 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive);
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000905 /* rekeying */
906 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Millerefb4afe2000-04-12 18:45:05 +1000907}