blob: 1e211701e0a97fbe271aae595cafa8b034149d60 [file] [log] [blame]
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00001/* $OpenBSD: serverloop.c,v 1.185 2016/08/13 17:47:41 markus 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
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000040#include <sys/param.h> /* MIN MAX */
Damien Miller9cf6d072006-03-15 11:29:24 +110041#include <sys/types.h>
42#include <sys/wait.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100043#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100044#ifdef HAVE_SYS_TIME_H
45# include <sys/time.h>
46#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100047
48#include <netinet/in.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110049
Darren Tucker39972492006-07-12 22:22:46 +100050#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100051#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100052#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110053#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100054#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110055#include <termios.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100056#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100057#include <stdarg.h>
Damien Miller69b69aa2000-10-28 14:19:58 +110058
Damien Millerb84886b2008-05-19 15:05:07 +100059#include "openbsd-compat/sys-queue.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061#include "packet.h"
62#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100064#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065#include "servconf.h"
Damien Miller16aed052002-09-22 01:26:27 +100066#include "canohost.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000067#include "sshpty.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000068#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100069#include "compat.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100070#include "ssh2.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#include "key.h"
72#include "cipher.h"
73#include "kex.h"
74#include "hostfile.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000075#include "auth.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100076#include "session.h"
Damien Millerb38eff82000-04-01 11:09:21 +100077#include "dispatch.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100078#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000079#include "serverloop.h"
djm@openbsd.org523463a2015-02-16 22:13:32 +000080#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
Damien Miller50a41ed2000-10-16 12:14:42 +110082extern ServerOptions options;
83
Ben Lindstrom8ac91062001-04-04 17:57:54 +000084/* XXX */
Darren Tucker3e33cec2003-10-02 16:12:36 +100085extern Authctxt *the_authctxt;
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
Damien Millerf6681a32001-12-21 14:53:11 +1100103/*
104 * we write to this pipe if a SIGCHLD is caught in order to avoid
105 * the race between select() and child_terminated
106 */
107static int notify_pipe[2];
108static void
109notify_setup(void)
110{
111 if (pipe(notify_pipe) < 0) {
112 error("pipe(notify_pipe) failed %s", strerror(errno));
Damien Miller814ace02011-05-20 19:02:47 +1000113 } else if ((fcntl(notify_pipe[0], F_SETFD, FD_CLOEXEC) == -1) ||
114 (fcntl(notify_pipe[1], F_SETFD, FD_CLOEXEC) == -1)) {
Damien Millerf6681a32001-12-21 14:53:11 +1100115 error("fcntl(notify_pipe, F_SETFD) failed %s", strerror(errno));
116 close(notify_pipe[0]);
117 close(notify_pipe[1]);
118 } else {
119 set_nonblock(notify_pipe[0]);
120 set_nonblock(notify_pipe[1]);
121 return;
122 }
123 notify_pipe[0] = -1; /* read end */
124 notify_pipe[1] = -1; /* write end */
125}
126static void
127notify_parent(void)
128{
129 if (notify_pipe[1] != -1)
Darren Tuckerdbee3082013-05-16 20:32:29 +1000130 (void)write(notify_pipe[1], "", 1);
Damien Millerf6681a32001-12-21 14:53:11 +1100131}
132static void
133notify_prepare(fd_set *readset)
134{
135 if (notify_pipe[0] != -1)
136 FD_SET(notify_pipe[0], readset);
137}
138static void
139notify_done(fd_set *readset)
140{
141 char c;
142
143 if (notify_pipe[0] != -1 && FD_ISSET(notify_pipe[0], readset))
144 while (read(notify_pipe[0], &c, 1) != -1)
145 debug2("notify_done: reading");
146}
147
Damien Millerf0b15df2006-03-26 13:59:20 +1100148/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000149static void
Damien Miller95def091999-11-25 00:26:21 +1100150sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151{
Damien Miller95def091999-11-25 00:26:21 +1100152 int save_errno = errno;
Damien Millerefb4afe2000-04-12 18:45:05 +1000153 child_terminated = 1;
Tim Rice81ed5182002-09-25 17:38:46 -0700154#ifndef _UNICOS
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000155 mysignal(SIGCHLD, sigchld_handler);
Tim Rice81ed5182002-09-25 17:38:46 -0700156#endif
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
169client_alive_check(void)
170{
Damien Millerb5820f42003-12-17 16:27:32 +1100171 int channel_id;
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) {
Damien Miller985a4482006-10-24 03:02:41 +1000175 logit("Timeout, client not responding.");
176 cleanup_exit(255);
177 }
Damien Miller8c3902a2001-10-10 15:01:40 +1000178
Damien Miller8c3902a2001-10-10 15:01:40 +1000179 /*
Damien Millerb5820f42003-12-17 16:27:32 +1100180 * send a bogus global/channel request with "wantreply",
Damien Miller8c3902a2001-10-10 15:01:40 +1000181 * we should get back a failure
182 */
Damien Millerb5820f42003-12-17 16:27:32 +1100183 if ((channel_id = channel_find_open()) == -1) {
184 packet_start(SSH2_MSG_GLOBAL_REQUEST);
185 packet_put_cstring("keepalive@openssh.com");
186 packet_put_char(1); /* boolean: want reply */
187 } else {
188 channel_request_start(channel_id, "keepalive@openssh.com", 1);
189 }
Damien Miller8c3902a2001-10-10 15:01:40 +1000190 packet_send();
191}
192
Damien Miller95def091999-11-25 00:26:21 +1100193/*
194 * Sleep in select() until we can do something. This will initialize the
195 * select masks. Upon return, the masks will indicate which descriptors
196 * have data or can accept data. Optionally, a maximum time can be specified
197 * for the duration of the wait (0 = infinite).
198 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000199static void
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000200wait_until_can_do_something(int connection_in, int connection_out,
201 fd_set **readsetp, fd_set **writesetp, int *maxfdp,
djm@openbsd.org988e4292016-03-04 03:35:44 +0000202 u_int *nallocp, u_int64_t max_time_ms)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203{
Damien Miller95def091999-11-25 00:26:21 +1100204 struct timeval tv, *tvp;
205 int ret;
Damien Miller6c6da332012-06-20 22:31:26 +1000206 time_t minwait_secs = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000207 int client_alive_scheduled = 0;
208
Damien Millera6508752012-04-22 11:21:10 +1000209 /* Allocate and update select() masks for channel descriptors. */
210 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
211 &minwait_secs, 0);
212
djm@openbsd.org988e4292016-03-04 03:35:44 +0000213 /* XXX need proper deadline system for rekey/client alive */
Damien Millera6508752012-04-22 11:21:10 +1000214 if (minwait_secs != 0)
djm@openbsd.org988e4292016-03-04 03:35:44 +0000215 max_time_ms = MIN(max_time_ms, (u_int)minwait_secs * 1000);
Damien Millera6508752012-04-22 11:21:10 +1000216
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000217 /*
Damien Miller9f0f5c62001-12-21 14:45:46 +1100218 * if using client_alive, set the max timeout accordingly,
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000219 * and indicate that this particular timeout was for client
220 * alive by setting the client_alive_scheduled flag.
221 *
222 * this could be randomized somewhat to make traffic
Damien Miller9f0f5c62001-12-21 14:45:46 +1100223 * analysis more difficult, but we're not doing it yet.
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000224 */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000225 if (options.client_alive_interval) {
djm@openbsd.org988e4292016-03-04 03:35:44 +0000226 uint64_t keepalive_ms =
227 (uint64_t)options.client_alive_interval * 1000;
228
Ben Lindstrom2f0304c2001-04-29 19:49:14 +0000229 client_alive_scheduled = 1;
djm@openbsd.org988e4292016-03-04 03:35:44 +0000230 if (max_time_ms == 0 || max_time_ms > keepalive_ms)
231 max_time_ms = keepalive_ms;
Ben Lindstrom36857f62001-07-18 15:48:57 +0000232 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233
Damien Milleraf5f2e62001-10-10 15:01:16 +1000234#if 0
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000235 /* wrong: bad condition XXX */
236 if (channel_not_very_much_buffered_data())
Damien Milleraf5f2e62001-10-10 15:01:16 +1000237#endif
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000238 FD_SET(connection_in, *readsetp);
Damien Millerf6681a32001-12-21 14:53:11 +1100239 notify_prepare(*readsetp);
Damien Miller95def091999-11-25 00:26:21 +1100240
Damien Miller5428f641999-11-25 11:54:57 +1100241 /*
242 * If we have buffered packet data going to the client, mark that
243 * descriptor.
244 */
Damien Miller95def091999-11-25 00:26:21 +1100245 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100246 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100247
Damien Miller5428f641999-11-25 11:54:57 +1100248 /*
249 * If child has terminated and there is enough buffer space to read
250 * from it, then read as much as is available and exit.
251 */
Damien Miller95def091999-11-25 00:26:21 +1100252 if (child_terminated && packet_not_very_much_data_to_write())
djm@openbsd.org988e4292016-03-04 03:35:44 +0000253 if (max_time_ms == 0 || client_alive_scheduled)
254 max_time_ms = 100;
Damien Miller95def091999-11-25 00:26:21 +1100255
djm@openbsd.org988e4292016-03-04 03:35:44 +0000256 if (max_time_ms == 0)
Damien Miller95def091999-11-25 00:26:21 +1100257 tvp = NULL;
258 else {
djm@openbsd.org988e4292016-03-04 03:35:44 +0000259 tv.tv_sec = max_time_ms / 1000;
260 tv.tv_usec = 1000 * (max_time_ms % 1000);
Damien Miller95def091999-11-25 00:26:21 +1100261 tvp = &tv;
262 }
263
264 /* Wait for something to happen, or the timeout to expire. */
Damien Miller5e953212001-01-30 09:14:00 +1100265 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
Damien Miller95def091999-11-25 00:26:21 +1100266
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000267 if (ret == -1) {
Damien Miller79faeff2001-11-12 11:06:32 +1100268 memset(*readsetp, 0, *nallocp);
269 memset(*writesetp, 0, *nallocp);
Damien Miller95def091999-11-25 00:26:21 +1100270 if (errno != EINTR)
271 error("select: %.100s", strerror(errno));
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000272 } else if (ret == 0 && client_alive_scheduled)
273 client_alive_check();
Damien Millerf6681a32001-12-21 14:53:11 +1100274
275 notify_done(*readsetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276}
277
Damien Miller95def091999-11-25 00:26:21 +1100278/*
279 * Processes input from the client and the program. Input data is stored
280 * in buffers and processed later.
281 */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000282static int
283process_input(fd_set *readset, int connection_in)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284{
djm@openbsd.org95767262016-03-07 19:02:43 +0000285 struct ssh *ssh = active_state; /* XXX */
Damien Miller95def091999-11-25 00:26:21 +1100286 int len;
287 char buf[16384];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288
Damien Miller95def091999-11-25 00:26:21 +1100289 /* Read and buffer any input data from the client. */
290 if (FD_ISSET(connection_in, readset)) {
markus@openbsd.orga3068632016-01-14 16:17:39 +0000291 len = read(connection_in, buf, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +1100292 if (len == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +0000293 verbose("Connection closed by %.100s port %d",
294 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000295 return -1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000296 } else if (len < 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +1000297 if (errno != EINTR && errno != EAGAIN &&
298 errno != EWOULDBLOCK) {
Damien Miller16aed052002-09-22 01:26:27 +1000299 verbose("Read error from remote host "
djm@openbsd.org95767262016-03-07 19:02:43 +0000300 "%.100s port %d: %.100s",
301 ssh_remote_ipaddr(ssh),
302 ssh_remote_port(ssh), strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000303 cleanup_exit(255);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000304 }
305 } else {
306 /* Buffer any received data. */
307 packet_process_incoming(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100308 }
Damien Miller95def091999-11-25 00:26:21 +1100309 }
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000310 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100311}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312
Damien Miller95def091999-11-25 00:26:21 +1100313/*
314 * Sends data from internal buffers to client program stdin.
315 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000316static void
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000317process_output(fd_set *writeset, int connection_out)
Damien Miller95def091999-11-25 00:26:21 +1100318{
Damien Miller95def091999-11-25 00:26:21 +1100319 /* Send any buffered packet data to the client. */
320 if (FD_ISSET(connection_out, writeset))
321 packet_write_poll();
322}
323
Ben Lindstrombba81212001-06-25 05:01:22 +0000324static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000325process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000326{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000327 dispatch_run(DISPATCH_NONBLOCK, NULL, active_state);
Damien Millerb38eff82000-04-01 11:09:21 +1000328}
329
Damien Miller3ec27592001-10-12 11:35:04 +1000330static void
331collect_children(void)
332{
333 pid_t pid;
334 sigset_t oset, nset;
335 int status;
336
337 /* block SIGCHLD while we check for dead children */
338 sigemptyset(&nset);
339 sigaddset(&nset, SIGCHLD);
340 sigprocmask(SIG_BLOCK, &nset, &oset);
341 if (child_terminated) {
Damien Millerec04f362006-03-15 12:01:34 +1100342 debug("Received SIGCHLD.");
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000343 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
344 (pid < 0 && errno == EINTR))
345 if (pid > 0)
346 session_close_by_pid(pid, status);
Damien Miller3ec27592001-10-12 11:35:04 +1000347 child_terminated = 0;
348 }
349 sigprocmask(SIG_SETMASK, &oset, NULL);
350}
351
Damien Miller4af51302000-04-16 11:18:38 +1000352void
Ben Lindstrombddd5512001-07-04 04:53:53 +0000353server_loop2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +1000354{
Damien Miller5e953212001-01-30 09:14:00 +1100355 fd_set *readset = NULL, *writeset = NULL;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000356 int max_fd;
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000357 u_int nalloc = 0, connection_in, connection_out;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000358 u_int64_t rekey_timeout_ms = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +1000359
360 debug("Entering interactive session for SSH2.");
361
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000362 mysignal(SIGCHLD, sigchld_handler);
Damien Millerefb4afe2000-04-12 18:45:05 +1000363 child_terminated = 0;
364 connection_in = packet_get_connection_in();
365 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +1100366
Damien Miller24ecf612005-11-05 15:16:52 +1100367 if (!use_privsep) {
368 signal(SIGTERM, sigterm_handler);
369 signal(SIGINT, sigterm_handler);
370 signal(SIGQUIT, sigterm_handler);
371 }
372
Damien Millerf6681a32001-12-21 14:53:11 +1100373 notify_setup();
374
Damien Miller5e953212001-01-30 09:14:00 +1100375 max_fd = MAX(connection_in, connection_out);
Damien Millerf6681a32001-12-21 14:53:11 +1100376 max_fd = MAX(max_fd, notify_pipe[0]);
377
Damien Millerefb4afe2000-04-12 18:45:05 +1000378 server_init_dispatch();
379
380 for (;;) {
381 process_buffered_input_packets();
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000382
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000383 if (!ssh_packet_is_rekeying(active_state) &&
384 packet_not_very_much_data_to_write())
Damien Millerefb4afe2000-04-12 18:45:05 +1000385 channel_output_poll();
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000386 if (options.rekey_interval > 0 &&
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000387 !ssh_packet_is_rekeying(active_state))
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000388 rekey_timeout_ms = packet_get_rekey_timeout() * 1000;
389 else
390 rekey_timeout_ms = 0;
391
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000392 wait_until_can_do_something(connection_in, connection_out,
393 &readset, &writeset, &max_fd, &nalloc, rekey_timeout_ms);
Damien Miller52b77be2001-10-10 15:14:37 +1000394
Damien Miller24ecf612005-11-05 15:16:52 +1100395 if (received_sigterm) {
Darren Tucker3e1027c2012-12-07 13:07:46 +1100396 logit("Exiting on signal %d", (int)received_sigterm);
Damien Miller24ecf612005-11-05 15:16:52 +1100397 /* Clean up sessions, utmp, etc. */
398 cleanup_exit(255);
399 }
400
Damien Miller3ec27592001-10-12 11:35:04 +1000401 collect_children();
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000402 if (!ssh_packet_is_rekeying(active_state))
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000403 channel_after_select(readset, writeset);
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000404 if (process_input(readset, connection_in) < 0)
Ben Lindstrome34ab4c2001-04-07 01:12:11 +0000405 break;
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000406 process_output(writeset, connection_out);
Damien Millerefb4afe2000-04-12 18:45:05 +1000407 }
Damien Miller3ec27592001-10-12 11:35:04 +1000408 collect_children();
409
Darren Tuckera627d422013-06-02 07:31:17 +1000410 free(readset);
411 free(writeset);
Damien Miller5e953212001-01-30 09:14:00 +1100412
Damien Miller52b77be2001-10-10 15:14:37 +1000413 /* free all channels, no more reads and writes */
414 channel_free_all();
Ben Lindstrom4983d5e2001-07-04 05:17:40 +0000415
Damien Miller3ec27592001-10-12 11:35:04 +1000416 /* free remaining sessions, e.g. remove wtmp entries */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000417 session_destroy_all(NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +1000418}
419
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000420static int
Damien Millerb5820f42003-12-17 16:27:32 +1100421server_input_keep_alive(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000422{
Damien Millerb5820f42003-12-17 16:27:32 +1100423 debug("Got %d/%u for keepalive", type, seq);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100424 /*
Ben Lindstrom2f0304c2001-04-29 19:49:14 +0000425 * reset timeout, since we got a sane answer from the client.
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000426 * even if this was generated by something other than
427 * the bogus CHANNEL_REQUEST we send for keepalives.
428 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000429 packet_set_alive_timeouts(0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000430 return 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000431}
432
Ben Lindstrombba81212001-06-25 05:01:22 +0000433static Channel *
Damien Millerfb1310e2004-01-21 11:02:50 +1100434server_request_direct_tcpip(void)
Damien Millerefb4afe2000-04-12 18:45:05 +1000435{
Damien Milleraa5b3f82012-12-03 09:50:54 +1100436 Channel *c = NULL;
Damien Miller4af51302000-04-16 11:18:38 +1000437 char *target, *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100438 u_short target_port, originator_port;
Damien Millerefb4afe2000-04-12 18:45:05 +1000439
Damien Miller4af51302000-04-16 11:18:38 +1000440 target = packet_get_string(NULL);
441 target_port = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +1000442 originator = packet_get_string(NULL);
443 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100444 packet_check_eom();
Damien Millerb1715dc2000-05-30 13:44:51 +1000445
Damien Millerbd740252008-05-19 15:37:09 +1000446 debug("server_request_direct_tcpip: originator %s port %d, target %s "
447 "port %d", originator, originator_port, target, target_port);
Damien Millerf6d9e222000-06-18 14:50:44 +1000448
Damien Milleraa5b3f82012-12-03 09:50:54 +1100449 /* XXX fine grained permissions */
450 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 &&
451 !no_port_forwarding_flag) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000452 c = channel_connect_to_port(target, target_port,
Damien Milleraa5b3f82012-12-03 09:50:54 +1100453 "direct-tcpip", "direct-tcpip");
454 } else {
455 logit("refused local port forward: "
456 "originator %s port %d, target %s port %d",
457 originator, originator_port, target, target_port);
458 }
Damien Millerbd740252008-05-19 15:37:09 +1000459
Darren Tuckera627d422013-06-02 07:31:17 +1000460 free(originator);
461 free(target);
Damien Millerbd740252008-05-19 15:37:09 +1000462
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000463 return c;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100464}
465
Ben Lindstrombba81212001-06-25 05:01:22 +0000466static Channel *
Damien Miller7acefbb2014-07-18 14:11:24 +1000467server_request_direct_streamlocal(void)
468{
469 Channel *c = NULL;
470 char *target, *originator;
471 u_short originator_port;
472
473 target = packet_get_string(NULL);
474 originator = packet_get_string(NULL);
475 originator_port = packet_get_int();
476 packet_check_eom();
477
478 debug("server_request_direct_streamlocal: originator %s port %d, target %s",
479 originator, originator_port, target);
480
481 /* XXX fine grained permissions */
482 if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 &&
483 !no_port_forwarding_flag) {
484 c = channel_connect_to_path(target,
485 "direct-streamlocal@openssh.com", "direct-streamlocal");
486 } else {
487 logit("refused streamlocal port forward: "
488 "originator %s port %d, target %s",
489 originator, originator_port, target);
490 }
491
492 free(originator);
493 free(target);
494
495 return c;
496}
497
498static Channel *
Damien Millerd27b9472005-12-13 19:29:02 +1100499server_request_tun(void)
500{
501 Channel *c = NULL;
Damien Miller7b58e802005-12-13 19:33:19 +1100502 int mode, tun;
503 int sock;
Damien Millerd27b9472005-12-13 19:29:02 +1100504
Damien Miller7b58e802005-12-13 19:33:19 +1100505 mode = packet_get_int();
506 switch (mode) {
507 case SSH_TUNMODE_POINTOPOINT:
508 case SSH_TUNMODE_ETHERNET:
509 break;
510 default:
511 packet_send_debug("Unsupported tunnel device mode.");
512 return NULL;
513 }
514 if ((options.permit_tun & mode) == 0) {
515 packet_send_debug("Server has rejected tunnel device "
516 "forwarding");
Damien Millerd27b9472005-12-13 19:29:02 +1100517 return NULL;
518 }
519
520 tun = packet_get_int();
Darren Tucker0d0e8f02005-12-20 16:08:42 +1100521 if (forced_tun_device != -1) {
Damien Millerf0b15df2006-03-26 13:59:20 +1100522 if (tun != SSH_TUNID_ANY && forced_tun_device != tun)
Damien Millerd27b9472005-12-13 19:29:02 +1100523 goto done;
524 tun = forced_tun_device;
525 }
Damien Miller7b58e802005-12-13 19:33:19 +1100526 sock = tun_open(tun, mode);
Damien Millerd27b9472005-12-13 19:29:02 +1100527 if (sock < 0)
528 goto done;
529 c = channel_new("tun", SSH_CHANNEL_OPEN, sock, sock, -1,
530 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
531 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +1100532#if defined(SSH_TUN_FILTER)
533 if (mode == SSH_TUNMODE_POINTOPOINT)
534 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +1000535 sys_tun_outfilter, NULL, NULL);
Damien Miller598bbc22005-12-31 16:33:36 +1100536#endif
Damien Millerd27b9472005-12-13 19:29:02 +1100537
538 done:
539 if (c == NULL)
540 packet_send_debug("Failed to open the tunnel device.");
541 return c;
542}
543
544static Channel *
Damien Millerfb1310e2004-01-21 11:02:50 +1100545server_request_session(void)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100546{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000547 Channel *c;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100548
549 debug("input_session_request");
Damien Miller48b03fc2002-01-22 23:11:40 +1100550 packet_check_eom();
Darren Tucker8901fa92008-06-11 09:34:01 +1000551
552 if (no_more_sessions) {
553 packet_disconnect("Possible attack: attempt to open a session "
554 "after additional sessions disabled");
555 }
556
Damien Miller0bc1bd82000-11-13 22:57:25 +1100557 /*
558 * A server session has no fd to read or write until a
559 * CHANNEL_REQUEST for a shell is made, so we set the type to
560 * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all
561 * CHANNEL_REQUEST messages is registered.
562 */
Damien Millerfb1310e2004-01-21 11:02:50 +1100563 c = channel_new("session", SSH_CHANNEL_LARVAL,
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000564 -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000565 0, "server-session", 1);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000566 if (session_open(the_authctxt, c->self) != 1) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000567 debug("session open failed, free channel %d", c->self);
568 channel_free(c);
569 return NULL;
570 }
Damien Miller39eda6e2005-11-05 14:52:50 +1100571 channel_register_cleanup(c->self, session_close_by_channel, 0);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000572 return c;
Damien Millerefb4afe2000-04-12 18:45:05 +1000573}
574
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000575static int
Damien Miller630d6f42002-01-22 23:17:30 +1100576server_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +1000577{
578 Channel *c = NULL;
579 char *ctype;
Damien Millerefb4afe2000-04-12 18:45:05 +1000580 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000581 u_int rmaxpack, rwindow, len;
Damien Millerefb4afe2000-04-12 18:45:05 +1000582
583 ctype = packet_get_string(&len);
584 rchan = packet_get_int();
585 rwindow = packet_get_int();
586 rmaxpack = packet_get_int();
587
Damien Miller62cee002000-09-23 17:15:56 +1100588 debug("server_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerefb4afe2000-04-12 18:45:05 +1000589 ctype, rchan, rwindow, rmaxpack);
590
591 if (strcmp(ctype, "session") == 0) {
Damien Millerfb1310e2004-01-21 11:02:50 +1100592 c = server_request_session();
Damien Millerefb4afe2000-04-12 18:45:05 +1000593 } else if (strcmp(ctype, "direct-tcpip") == 0) {
Damien Millerfb1310e2004-01-21 11:02:50 +1100594 c = server_request_direct_tcpip();
Damien Miller7acefbb2014-07-18 14:11:24 +1000595 } else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) {
596 c = server_request_direct_streamlocal();
Damien Millerd27b9472005-12-13 19:29:02 +1100597 } else if (strcmp(ctype, "tun@openssh.com") == 0) {
598 c = server_request_tun();
Damien Millerefb4afe2000-04-12 18:45:05 +1000599 }
600 if (c != NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100601 debug("server_input_channel_open: confirm %s", ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000602 c->remote_id = rchan;
603 c->remote_window = rwindow;
604 c->remote_maxpacket = rmaxpack;
Ben Lindstrom69128662001-05-08 20:07:39 +0000605 if (c->type != SSH_CHANNEL_CONNECTING) {
606 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
607 packet_put_int(c->remote_id);
608 packet_put_int(c->self);
609 packet_put_int(c->local_window);
610 packet_put_int(c->local_maxpacket);
611 packet_send();
612 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000613 } else {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100614 debug("server_input_channel_open: failure %s", ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000615 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
616 packet_put_int(rchan);
617 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +0000618 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstrom69128662001-05-08 20:07:39 +0000619 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +0000620 packet_put_cstring("");
621 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000622 packet_send();
623 }
Darren Tuckera627d422013-06-02 07:31:17 +1000624 free(ctype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000625 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +1000626}
627
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000628static int
djm@openbsd.org523463a2015-02-16 22:13:32 +0000629server_input_hostkeys_prove(struct sshbuf **respp)
630{
631 struct ssh *ssh = active_state; /* XXX */
632 struct sshbuf *resp = NULL;
633 struct sshbuf *sigbuf = NULL;
634 struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL;
635 int r, ndx, success = 0;
636 const u_char *blob;
637 u_char *sig = 0;
638 size_t blen, slen;
639
640 if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL)
641 fatal("%s: sshbuf_new", __func__);
642
643 while (ssh_packet_remaining(ssh) > 0) {
644 sshkey_free(key);
645 key = NULL;
646 if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 ||
647 (r = sshkey_from_blob(blob, blen, &key)) != 0) {
648 error("%s: couldn't parse key: %s",
649 __func__, ssh_err(r));
650 goto out;
651 }
652 /*
653 * Better check that this is actually one of our hostkeys
654 * before attempting to sign anything with it.
655 */
656 if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) {
657 error("%s: unknown host %s key",
658 __func__, sshkey_type(key));
659 goto out;
660 }
661 /*
662 * XXX refactor: make kex->sign just use an index rather
663 * than passing in public and private keys
664 */
665 if ((key_prv = get_hostkey_by_index(ndx)) == NULL &&
666 (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) {
667 error("%s: can't retrieve hostkey %d", __func__, ndx);
668 goto out;
669 }
670 sshbuf_reset(sigbuf);
671 free(sig);
672 sig = NULL;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000673 if ((r = sshbuf_put_cstring(sigbuf,
674 "hostkeys-prove-00@openssh.com")) != 0 ||
675 (r = sshbuf_put_string(sigbuf,
djm@openbsd.org523463a2015-02-16 22:13:32 +0000676 ssh->kex->session_id, ssh->kex->session_id_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000677 (r = sshkey_puts(key, sigbuf)) != 0 ||
678 (r = ssh->kex->sign(key_prv, key_pub, &sig, &slen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000679 sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), NULL, 0)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000680 (r = sshbuf_put_string(resp, sig, slen)) != 0) {
681 error("%s: couldn't prepare signature: %s",
682 __func__, ssh_err(r));
683 goto out;
684 }
685 }
686 /* Success */
687 *respp = resp;
688 resp = NULL; /* don't free it */
689 success = 1;
690 out:
691 free(sig);
692 sshbuf_free(resp);
693 sshbuf_free(sigbuf);
694 sshkey_free(key);
695 return success;
696}
697
698static int
Damien Miller630d6f42002-01-22 23:17:30 +1100699server_input_global_request(int type, u_int32_t seq, void *ctxt)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100700{
701 char *rtype;
702 int want_reply;
djm@openbsd.org523463a2015-02-16 22:13:32 +0000703 int r, success = 0, allocated_listen_port = 0;
704 struct sshbuf *resp = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100705
706 rtype = packet_get_string(NULL);
707 want_reply = packet_get_char();
708 debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000709
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000710 /* -R style forwarding */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100711 if (strcmp(rtype, "tcpip-forward") == 0) {
712 struct passwd *pw;
Damien Miller7acefbb2014-07-18 14:11:24 +1000713 struct Forward fwd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100714
Darren Tucker3e33cec2003-10-02 16:12:36 +1000715 pw = the_authctxt->pw;
Damien Miller4bb1dd32003-11-18 22:01:25 +1100716 if (pw == NULL || !the_authctxt->valid)
Damien Miller3e3b5142003-11-17 21:13:40 +1100717 fatal("server_input_global_request: no/invalid user");
Damien Miller7acefbb2014-07-18 14:11:24 +1000718 memset(&fwd, 0, sizeof(fwd));
719 fwd.listen_host = packet_get_string(NULL);
720 fwd.listen_port = (u_short)packet_get_int();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100721 debug("server_input_global_request: tcpip-forward listen %s port %d",
Damien Miller7acefbb2014-07-18 14:11:24 +1000722 fwd.listen_host, fwd.listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100723
724 /* check permissions */
Damien Milleraa5b3f82012-12-03 09:50:54 +1100725 if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 ||
Damien Miller330d5852009-02-14 16:33:09 +1100726 no_port_forwarding_flag ||
Darren Tucker5f41f032016-04-08 21:14:13 +1000727 (!want_reply && fwd.listen_port == 0) ||
728 (fwd.listen_port != 0 && fwd.listen_port < IPPORT_RESERVED &&
729 pw->pw_uid != 0)) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100730 success = 0;
731 packet_send_debug("Server has disabled port forwarding.");
732 } else {
733 /* Start listening on the port */
Damien Miller7acefbb2014-07-18 14:11:24 +1000734 success = channel_setup_remote_fwd_listener(&fwd,
735 &allocated_listen_port, &options.fwd_opts);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100736 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000737 free(fwd.listen_host);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000738 if ((resp = sshbuf_new()) == NULL)
739 fatal("%s: sshbuf_new", __func__);
djm@openbsd.orgb1d6b392015-11-28 06:41:03 +0000740 if (allocated_listen_port != 0 &&
741 (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0)
djm@openbsd.org523463a2015-02-16 22:13:32 +0000742 fatal("%s: sshbuf_put_u32: %s", __func__, ssh_err(r));
Darren Tuckere7066df2004-05-24 10:18:05 +1000743 } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000744 struct Forward fwd;
Darren Tuckere7066df2004-05-24 10:18:05 +1000745
Damien Miller7acefbb2014-07-18 14:11:24 +1000746 memset(&fwd, 0, sizeof(fwd));
747 fwd.listen_host = packet_get_string(NULL);
748 fwd.listen_port = (u_short)packet_get_int();
Darren Tuckere7066df2004-05-24 10:18:05 +1000749 debug("%s: cancel-tcpip-forward addr %s port %d", __func__,
Damien Miller7acefbb2014-07-18 14:11:24 +1000750 fwd.listen_host, fwd.listen_port);
Darren Tuckere7066df2004-05-24 10:18:05 +1000751
Damien Miller7acefbb2014-07-18 14:11:24 +1000752 success = channel_cancel_rport_listener(&fwd);
753 free(fwd.listen_host);
754 } else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) {
755 struct Forward fwd;
756
757 memset(&fwd, 0, sizeof(fwd));
758 fwd.listen_path = packet_get_string(NULL);
759 debug("server_input_global_request: streamlocal-forward listen path %s",
760 fwd.listen_path);
761
762 /* check permissions */
763 if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0
764 || no_port_forwarding_flag) {
765 success = 0;
766 packet_send_debug("Server has disabled port forwarding.");
767 } else {
768 /* Start listening on the socket */
769 success = channel_setup_remote_fwd_listener(
770 &fwd, NULL, &options.fwd_opts);
771 }
772 free(fwd.listen_path);
773 } else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) {
774 struct Forward fwd;
775
776 memset(&fwd, 0, sizeof(fwd));
777 fwd.listen_path = packet_get_string(NULL);
778 debug("%s: cancel-streamlocal-forward path %s", __func__,
779 fwd.listen_path);
780
781 success = channel_cancel_rport_listener(&fwd);
782 free(fwd.listen_path);
Darren Tucker8901fa92008-06-11 09:34:01 +1000783 } else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) {
784 no_more_sessions = 1;
785 success = 1;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000786 } else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) {
djm@openbsd.org523463a2015-02-16 22:13:32 +0000787 success = server_input_hostkeys_prove(&resp);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100788 }
789 if (want_reply) {
790 packet_start(success ?
791 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000792 if (success && resp != NULL)
793 ssh_packet_put_raw(active_state, sshbuf_ptr(resp),
794 sshbuf_len(resp));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100795 packet_send();
796 packet_write_wait();
797 }
Darren Tuckera627d422013-06-02 07:31:17 +1000798 free(rtype);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000799 sshbuf_free(resp);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000800 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100801}
Damien Miller4f7becb2006-03-26 14:10:14 +1100802
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000803static int
Damien Millerc7ef63d2002-02-05 12:21:42 +1100804server_input_channel_req(int type, u_int32_t seq, void *ctxt)
805{
806 Channel *c;
807 int id, reply, success = 0;
808 char *rtype;
809
810 id = packet_get_int();
811 rtype = packet_get_string(NULL);
812 reply = packet_get_char();
813
814 debug("server_input_channel_req: channel %d request %s reply %d",
815 id, rtype, reply);
816
817 if ((c = channel_lookup(id)) == NULL)
818 packet_disconnect("server_input_channel_req: "
819 "unknown channel %d", id);
Damien Millerbab9bd42008-05-19 16:06:47 +1000820 if (!strcmp(rtype, "eow@openssh.com")) {
821 packet_check_eom();
822 chan_rcvd_eow(c);
Darren Tucker8810dd42008-07-02 22:32:14 +1000823 } else if ((c->type == SSH_CHANNEL_LARVAL ||
824 c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0)
Damien Millerc7ef63d2002-02-05 12:21:42 +1100825 success = session_input_channel_req(c, rtype);
Damien Millerc5893782014-05-15 13:48:49 +1000826 if (reply && !(c->flags & CHAN_CLOSE_SENT)) {
Damien Millerc7ef63d2002-02-05 12:21:42 +1100827 packet_start(success ?
828 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
829 packet_put_int(c->remote_id);
830 packet_send();
831 }
Darren Tuckera627d422013-06-02 07:31:17 +1000832 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000833 return 0;
Damien Millerc7ef63d2002-02-05 12:21:42 +1100834}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100835
Ben Lindstrombba81212001-06-25 05:01:22 +0000836static void
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000837server_init_dispatch(void)
Damien Millerefb4afe2000-04-12 18:45:05 +1000838{
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000839 debug("server_init_dispatch");
Damien Millerefb4afe2000-04-12 18:45:05 +1000840 dispatch_init(&dispatch_protocol_error);
841 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
842 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
843 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
844 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
845 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
846 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
847 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Damien Millerc7ef63d2002-02-05 12:21:42 +1100848 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req);
Damien Millerefb4afe2000-04-12 18:45:05 +1000849 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100850 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request);
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000851 /* client_alive */
Damien Miller5a33ec62008-12-08 09:55:02 +1100852 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive);
853 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive);
Damien Millerb5820f42003-12-17 16:27:32 +1100854 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive);
855 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive);
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000856 /* rekeying */
857 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Millerefb4afe2000-04-12 18:45:05 +1000858}