blob: e6a92476f7fdad6d391d9745176bbebe89c965e6 [file] [log] [blame]
djm@openbsd.org988e4292016-03-04 03:35:44 +00001/* $OpenBSD: serverloop.c,v 1.183 2016/03/04 03:35:44 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Server main loop for handling the interactive session.
Damien Millere4340be2000-09-16 13:29:08 +11007 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
Damien Millerefb4afe2000-04-12 18:45:05 +100014 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000015 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110016 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100036 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
38#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110039
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"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000070#include "ssh1.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100071#include "ssh2.h"
Damien Millerd7834352006-08-05 12:39:39 +100072#include "key.h"
73#include "cipher.h"
74#include "kex.h"
75#include "hostfile.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000076#include "auth.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100077#include "session.h"
Damien Millerb38eff82000-04-01 11:09:21 +100078#include "dispatch.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100079#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "serverloop.h"
djm@openbsd.org523463a2015-02-16 22:13:32 +000081#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller50a41ed2000-10-16 12:14:42 +110083extern ServerOptions options;
84
Ben Lindstrom8ac91062001-04-04 17:57:54 +000085/* XXX */
Darren Tucker3e33cec2003-10-02 16:12:36 +100086extern Authctxt *the_authctxt;
Damien Miller24ecf612005-11-05 15:16:52 +110087extern int use_privsep;
Ben Lindstrom8ac91062001-04-04 17:57:54 +000088
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089static Buffer stdin_buffer; /* Buffer for stdin data. */
90static Buffer stdout_buffer; /* Buffer for stdout data. */
91static Buffer stderr_buffer; /* Buffer for stderr data. */
92static int fdin; /* Descriptor for stdin (for writing) */
93static int fdout; /* Descriptor for stdout (for reading);
94 May be same number as fdin. */
95static int fderr; /* Descriptor for stderr. May be -1. */
96static long stdin_bytes = 0; /* Number of bytes written to stdin. */
97static long stdout_bytes = 0; /* Number of stdout bytes sent to client. */
98static long stderr_bytes = 0; /* Number of stderr bytes sent to client. */
99static long fdout_bytes = 0; /* Number of stdout bytes read from program. */
100static int stdin_eof = 0; /* EOF message received from client. */
101static int fdout_eof = 0; /* EOF encountered reading from fdout. */
102static int fderr_eof = 0; /* EOF encountered readung from fderr. */
Damien Miller225736c2001-02-19 21:51:08 +1100103static int fdin_is_tty = 0; /* fdin points to a tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104static int connection_in; /* Connection to client (input). */
105static int connection_out; /* Connection to client (output). */
Ben Lindstrome34ab4c2001-04-07 01:12:11 +0000106static int connection_closed = 0; /* Connection to client closed. */
107static u_int buffer_high; /* "Soft" max buffer size. */
Darren Tucker8901fa92008-06-11 09:34:01 +1000108static int no_more_sessions = 0; /* Disallow further sessions. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Damien Miller5428f641999-11-25 11:54:57 +1100110/*
111 * This SIGCHLD kludge is used to detect when the child exits. The server
112 * will exit after that, as soon as forwarded connections have terminated.
113 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000114
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000115static volatile sig_atomic_t child_terminated = 0; /* The child has terminated. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
Damien Miller24ecf612005-11-05 15:16:52 +1100117/* Cleanup on signals (!use_privsep case only) */
118static volatile sig_atomic_t received_sigterm = 0;
119
Ben Lindstrombba81212001-06-25 05:01:22 +0000120/* prototypes */
121static void server_init_dispatch(void);
Damien Millerb38eff82000-04-01 11:09:21 +1000122
Damien Millerf6681a32001-12-21 14:53:11 +1100123/*
124 * we write to this pipe if a SIGCHLD is caught in order to avoid
125 * the race between select() and child_terminated
126 */
127static int notify_pipe[2];
128static void
129notify_setup(void)
130{
131 if (pipe(notify_pipe) < 0) {
132 error("pipe(notify_pipe) failed %s", strerror(errno));
Damien Miller814ace02011-05-20 19:02:47 +1000133 } else if ((fcntl(notify_pipe[0], F_SETFD, FD_CLOEXEC) == -1) ||
134 (fcntl(notify_pipe[1], F_SETFD, FD_CLOEXEC) == -1)) {
Damien Millerf6681a32001-12-21 14:53:11 +1100135 error("fcntl(notify_pipe, F_SETFD) failed %s", strerror(errno));
136 close(notify_pipe[0]);
137 close(notify_pipe[1]);
138 } else {
139 set_nonblock(notify_pipe[0]);
140 set_nonblock(notify_pipe[1]);
141 return;
142 }
143 notify_pipe[0] = -1; /* read end */
144 notify_pipe[1] = -1; /* write end */
145}
146static void
147notify_parent(void)
148{
149 if (notify_pipe[1] != -1)
Darren Tuckerdbee3082013-05-16 20:32:29 +1000150 (void)write(notify_pipe[1], "", 1);
Damien Millerf6681a32001-12-21 14:53:11 +1100151}
152static void
153notify_prepare(fd_set *readset)
154{
155 if (notify_pipe[0] != -1)
156 FD_SET(notify_pipe[0], readset);
157}
158static void
159notify_done(fd_set *readset)
160{
161 char c;
162
163 if (notify_pipe[0] != -1 && FD_ISSET(notify_pipe[0], readset))
164 while (read(notify_pipe[0], &c, 1) != -1)
165 debug2("notify_done: reading");
166}
167
Damien Millerf0b15df2006-03-26 13:59:20 +1100168/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000169static void
Damien Miller95def091999-11-25 00:26:21 +1100170sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171{
Damien Miller95def091999-11-25 00:26:21 +1100172 int save_errno = errno;
Damien Millerefb4afe2000-04-12 18:45:05 +1000173 child_terminated = 1;
Tim Rice81ed5182002-09-25 17:38:46 -0700174#ifndef _UNICOS
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000175 mysignal(SIGCHLD, sigchld_handler);
Tim Rice81ed5182002-09-25 17:38:46 -0700176#endif
Damien Millerf6681a32001-12-21 14:53:11 +1100177 notify_parent();
Damien Millerefb4afe2000-04-12 18:45:05 +1000178 errno = save_errno;
179}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180
Damien Millerf0b15df2006-03-26 13:59:20 +1100181/*ARGSUSED*/
Damien Miller24ecf612005-11-05 15:16:52 +1100182static void
183sigterm_handler(int sig)
184{
185 received_sigterm = sig;
186}
187
Damien Miller95def091999-11-25 00:26:21 +1100188/*
Damien Miller95def091999-11-25 00:26:21 +1100189 * Make packets from buffered stderr data, and buffer it for sending
190 * to the client.
191 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000192static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000193make_packets_from_stderr_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194{
Darren Tucker502d3842003-06-28 12:38:01 +1000195 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196
Damien Miller95def091999-11-25 00:26:21 +1100197 /* Send buffered stderr data to the client. */
198 while (buffer_len(&stderr_buffer) > 0 &&
Damien Miller037a0dc1999-12-07 15:38:31 +1100199 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100200 len = buffer_len(&stderr_buffer);
201 if (packet_is_interactive()) {
202 if (len > 512)
203 len = 512;
204 } else {
205 /* Keep the packets at reasonable size. */
206 if (len > packet_get_maxsize())
207 len = packet_get_maxsize();
208 }
209 packet_start(SSH_SMSG_STDERR_DATA);
210 packet_put_string(buffer_ptr(&stderr_buffer), len);
211 packet_send();
212 buffer_consume(&stderr_buffer, len);
213 stderr_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215}
216
Damien Miller95def091999-11-25 00:26:21 +1100217/*
218 * Make packets from buffered stdout data, and buffer it for sending to the
219 * client.
220 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000221static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000222make_packets_from_stdout_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223{
Darren Tucker502d3842003-06-28 12:38:01 +1000224 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225
Damien Miller95def091999-11-25 00:26:21 +1100226 /* Send buffered stdout data to the client. */
227 while (buffer_len(&stdout_buffer) > 0 &&
Damien Miller037a0dc1999-12-07 15:38:31 +1100228 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100229 len = buffer_len(&stdout_buffer);
230 if (packet_is_interactive()) {
231 if (len > 512)
232 len = 512;
233 } else {
234 /* Keep the packets at reasonable size. */
235 if (len > packet_get_maxsize())
Kevin Stevesef4eea92001-02-05 12:42:17 +0000236 len = packet_get_maxsize();
Damien Miller95def091999-11-25 00:26:21 +1100237 }
238 packet_start(SSH_SMSG_STDOUT_DATA);
239 packet_put_string(buffer_ptr(&stdout_buffer), len);
240 packet_send();
241 buffer_consume(&stdout_buffer, len);
242 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244}
245
Damien Miller8c3902a2001-10-10 15:01:40 +1000246static void
247client_alive_check(void)
248{
Damien Millerb5820f42003-12-17 16:27:32 +1100249 int channel_id;
Damien Miller056cf732002-01-22 23:21:39 +1100250
Damien Miller8c3902a2001-10-10 15:01:40 +1000251 /* timeout, check to see how many we have had */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000252 if (packet_inc_alive_timeouts() > options.client_alive_count_max) {
Damien Miller985a4482006-10-24 03:02:41 +1000253 logit("Timeout, client not responding.");
254 cleanup_exit(255);
255 }
Damien Miller8c3902a2001-10-10 15:01:40 +1000256
Damien Miller8c3902a2001-10-10 15:01:40 +1000257 /*
Damien Millerb5820f42003-12-17 16:27:32 +1100258 * send a bogus global/channel request with "wantreply",
Damien Miller8c3902a2001-10-10 15:01:40 +1000259 * we should get back a failure
260 */
Damien Millerb5820f42003-12-17 16:27:32 +1100261 if ((channel_id = channel_find_open()) == -1) {
262 packet_start(SSH2_MSG_GLOBAL_REQUEST);
263 packet_put_cstring("keepalive@openssh.com");
264 packet_put_char(1); /* boolean: want reply */
265 } else {
266 channel_request_start(channel_id, "keepalive@openssh.com", 1);
267 }
Damien Miller8c3902a2001-10-10 15:01:40 +1000268 packet_send();
269}
270
Damien Miller95def091999-11-25 00:26:21 +1100271/*
272 * Sleep in select() until we can do something. This will initialize the
273 * select masks. Upon return, the masks will indicate which descriptors
274 * have data or can accept data. Optionally, a maximum time can be specified
275 * for the duration of the wait (0 = infinite).
276 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000277static void
Damien Miller5e953212001-01-30 09:14:00 +1100278wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
djm@openbsd.org988e4292016-03-04 03:35:44 +0000279 u_int *nallocp, u_int64_t max_time_ms)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280{
Damien Miller95def091999-11-25 00:26:21 +1100281 struct timeval tv, *tvp;
282 int ret;
Damien Miller6c6da332012-06-20 22:31:26 +1000283 time_t minwait_secs = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000284 int client_alive_scheduled = 0;
Damien Millere42bd242007-01-29 10:16:28 +1100285 int program_alive_scheduled = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000286
Damien Millera6508752012-04-22 11:21:10 +1000287 /* Allocate and update select() masks for channel descriptors. */
288 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
289 &minwait_secs, 0);
290
djm@openbsd.org988e4292016-03-04 03:35:44 +0000291 /* XXX need proper deadline system for rekey/client alive */
Damien Millera6508752012-04-22 11:21:10 +1000292 if (minwait_secs != 0)
djm@openbsd.org988e4292016-03-04 03:35:44 +0000293 max_time_ms = MIN(max_time_ms, (u_int)minwait_secs * 1000);
Damien Millera6508752012-04-22 11:21:10 +1000294
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000295 /*
Damien Miller9f0f5c62001-12-21 14:45:46 +1100296 * if using client_alive, set the max timeout accordingly,
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000297 * and indicate that this particular timeout was for client
298 * alive by setting the client_alive_scheduled flag.
299 *
300 * this could be randomized somewhat to make traffic
Damien Miller9f0f5c62001-12-21 14:45:46 +1100301 * analysis more difficult, but we're not doing it yet.
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000302 */
djm@openbsd.org988e4292016-03-04 03:35:44 +0000303 if (compat20 && options.client_alive_interval) {
304 uint64_t keepalive_ms =
305 (uint64_t)options.client_alive_interval * 1000;
306
Ben Lindstrom2f0304c2001-04-29 19:49:14 +0000307 client_alive_scheduled = 1;
djm@openbsd.org988e4292016-03-04 03:35:44 +0000308 if (max_time_ms == 0 || max_time_ms > keepalive_ms)
309 max_time_ms = keepalive_ms;
Ben Lindstrom36857f62001-07-18 15:48:57 +0000310 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311
Damien Millerefb4afe2000-04-12 18:45:05 +1000312 if (compat20) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000313#if 0
Damien Millere247cc42000-05-07 12:03:14 +1000314 /* wrong: bad condition XXX */
Damien Millerefb4afe2000-04-12 18:45:05 +1000315 if (channel_not_very_much_buffered_data())
Damien Milleraf5f2e62001-10-10 15:01:16 +1000316#endif
317 FD_SET(connection_in, *readsetp);
Damien Millerefb4afe2000-04-12 18:45:05 +1000318 } else {
Damien Millerb1715dc2000-05-30 13:44:51 +1000319 /*
320 * Read packets from the client unless we have too much
321 * buffered stdin or channel data.
322 */
323 if (buffer_len(&stdin_buffer) < buffer_high &&
Damien Millerefb4afe2000-04-12 18:45:05 +1000324 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100325 FD_SET(connection_in, *readsetp);
Damien Millerb1715dc2000-05-30 13:44:51 +1000326 /*
327 * If there is not too much data already buffered going to
328 * the client, try to get some more data from the program.
329 */
330 if (packet_not_very_much_data_to_write()) {
Damien Millere42bd242007-01-29 10:16:28 +1100331 program_alive_scheduled = child_terminated;
Damien Millerb1715dc2000-05-30 13:44:51 +1000332 if (!fdout_eof)
Damien Miller5e953212001-01-30 09:14:00 +1100333 FD_SET(fdout, *readsetp);
Damien Millerb1715dc2000-05-30 13:44:51 +1000334 if (!fderr_eof)
Damien Miller5e953212001-01-30 09:14:00 +1100335 FD_SET(fderr, *readsetp);
Damien Millerb1715dc2000-05-30 13:44:51 +1000336 }
337 /*
338 * If we have buffered data, try to write some of that data
339 * to the program.
340 */
341 if (fdin != -1 && buffer_len(&stdin_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100342 FD_SET(fdin, *writesetp);
Damien Millerefb4afe2000-04-12 18:45:05 +1000343 }
Damien Millerf6681a32001-12-21 14:53:11 +1100344 notify_prepare(*readsetp);
Damien Miller95def091999-11-25 00:26:21 +1100345
Damien Miller5428f641999-11-25 11:54:57 +1100346 /*
347 * If we have buffered packet data going to the client, mark that
348 * descriptor.
349 */
Damien Miller95def091999-11-25 00:26:21 +1100350 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100351 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100352
Damien Miller5428f641999-11-25 11:54:57 +1100353 /*
354 * If child has terminated and there is enough buffer space to read
355 * from it, then read as much as is available and exit.
356 */
Damien Miller95def091999-11-25 00:26:21 +1100357 if (child_terminated && packet_not_very_much_data_to_write())
djm@openbsd.org988e4292016-03-04 03:35:44 +0000358 if (max_time_ms == 0 || client_alive_scheduled)
359 max_time_ms = 100;
Damien Miller95def091999-11-25 00:26:21 +1100360
djm@openbsd.org988e4292016-03-04 03:35:44 +0000361 if (max_time_ms == 0)
Damien Miller95def091999-11-25 00:26:21 +1100362 tvp = NULL;
363 else {
djm@openbsd.org988e4292016-03-04 03:35:44 +0000364 tv.tv_sec = max_time_ms / 1000;
365 tv.tv_usec = 1000 * (max_time_ms % 1000);
Damien Miller95def091999-11-25 00:26:21 +1100366 tvp = &tv;
367 }
368
369 /* Wait for something to happen, or the timeout to expire. */
Damien Miller5e953212001-01-30 09:14:00 +1100370 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
Damien Miller95def091999-11-25 00:26:21 +1100371
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000372 if (ret == -1) {
Damien Miller79faeff2001-11-12 11:06:32 +1100373 memset(*readsetp, 0, *nallocp);
374 memset(*writesetp, 0, *nallocp);
Damien Miller95def091999-11-25 00:26:21 +1100375 if (errno != EINTR)
376 error("select: %.100s", strerror(errno));
Damien Millere42bd242007-01-29 10:16:28 +1100377 } else {
378 if (ret == 0 && client_alive_scheduled)
379 client_alive_check();
380 if (!compat20 && program_alive_scheduled && fdin_is_tty) {
381 if (!fdout_eof)
382 FD_SET(fdout, *readsetp);
383 if (!fderr_eof)
384 FD_SET(fderr, *readsetp);
385 }
386 }
Damien Millerf6681a32001-12-21 14:53:11 +1100387
388 notify_done(*readsetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389}
390
Damien Miller95def091999-11-25 00:26:21 +1100391/*
392 * Processes input from the client and the program. Input data is stored
393 * in buffers and processed later.
394 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000395static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100396process_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397{
Damien Miller95def091999-11-25 00:26:21 +1100398 int len;
399 char buf[16384];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400
Damien Miller95def091999-11-25 00:26:21 +1100401 /* Read and buffer any input data from the client. */
402 if (FD_ISSET(connection_in, readset)) {
markus@openbsd.orga3068632016-01-14 16:17:39 +0000403 len = read(connection_in, buf, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +1100404 if (len == 0) {
Damien Miller16aed052002-09-22 01:26:27 +1000405 verbose("Connection closed by %.100s",
406 get_remote_ipaddr());
Ben Lindstrome34ab4c2001-04-07 01:12:11 +0000407 connection_closed = 1;
408 if (compat20)
409 return;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000410 cleanup_exit(255);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000411 } else if (len < 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +1000412 if (errno != EINTR && errno != EAGAIN &&
413 errno != EWOULDBLOCK) {
Damien Miller16aed052002-09-22 01:26:27 +1000414 verbose("Read error from remote host "
415 "%.100s: %.100s",
416 get_remote_ipaddr(), strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000417 cleanup_exit(255);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000418 }
419 } else {
420 /* Buffer any received data. */
421 packet_process_incoming(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100422 }
Damien Miller95def091999-11-25 00:26:21 +1100423 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000424 if (compat20)
425 return;
426
Damien Miller95def091999-11-25 00:26:21 +1100427 /* Read and buffer any available stdout data from the program. */
428 if (!fdout_eof && FD_ISSET(fdout, readset)) {
Darren Tucker9afe1152006-06-23 21:24:12 +1000429 errno = 0;
Damien Miller95def091999-11-25 00:26:21 +1100430 len = read(fdout, buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +1000431 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
432 errno == EWOULDBLOCK) && !child_terminated))) {
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000433 /* do nothing */
Darren Tucker03890e42006-06-24 16:58:45 +1000434#ifndef PTY_ZEROREAD
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000435 } else if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +1000436#else
Darren Tucker144e8d62006-06-25 08:25:25 +1000437 } else if ((!isatty(fdout) && len <= 0) ||
438 (isatty(fdout) && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +1000439#endif
Damien Miller95def091999-11-25 00:26:21 +1100440 fdout_eof = 1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000441 } else {
Damien Miller95def091999-11-25 00:26:21 +1100442 buffer_append(&stdout_buffer, buf, len);
443 fdout_bytes += len;
444 }
445 }
446 /* Read and buffer any available stderr data from the program. */
447 if (!fderr_eof && FD_ISSET(fderr, readset)) {
Darren Tucker9afe1152006-06-23 21:24:12 +1000448 errno = 0;
Damien Miller95def091999-11-25 00:26:21 +1100449 len = read(fderr, buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +1000450 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
451 errno == EWOULDBLOCK) && !child_terminated))) {
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000452 /* do nothing */
Darren Tucker03890e42006-06-24 16:58:45 +1000453#ifndef PTY_ZEROREAD
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000454 } else if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +1000455#else
Darren Tucker144e8d62006-06-25 08:25:25 +1000456 } else if ((!isatty(fderr) && len <= 0) ||
457 (isatty(fderr) && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +1000458#endif
Damien Miller95def091999-11-25 00:26:21 +1100459 fderr_eof = 1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000460 } else {
Damien Miller95def091999-11-25 00:26:21 +1100461 buffer_append(&stderr_buffer, buf, len);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000462 }
Damien Miller95def091999-11-25 00:26:21 +1100463 }
464}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465
Damien Miller95def091999-11-25 00:26:21 +1100466/*
467 * Sends data from internal buffers to client program stdin.
468 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000469static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100470process_output(fd_set *writeset)
Damien Miller95def091999-11-25 00:26:21 +1100471{
Ben Lindstromaa630de2001-02-10 23:44:47 +0000472 struct termios tio;
Ben Lindstrom6d218f42001-09-18 05:53:12 +0000473 u_char *data;
474 u_int dlen;
Damien Miller95def091999-11-25 00:26:21 +1100475 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476
Damien Miller95def091999-11-25 00:26:21 +1100477 /* Write buffered data to program stdin. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000478 if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) {
Ben Lindstrom6d218f42001-09-18 05:53:12 +0000479 data = buffer_ptr(&stdin_buffer);
480 dlen = buffer_len(&stdin_buffer);
481 len = write(fdin, data, dlen);
Damien Millerd8968ad2008-07-04 23:10:49 +1000482 if (len < 0 &&
483 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) {
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000484 /* do nothing */
485 } else if (len <= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +1000486 if (fdin != fdout)
Damien Miller95def091999-11-25 00:26:21 +1100487 close(fdin);
488 else
489 shutdown(fdin, SHUT_WR); /* We will no longer send. */
Damien Miller95def091999-11-25 00:26:21 +1100490 fdin = -1;
491 } else {
Ben Lindstromaa630de2001-02-10 23:44:47 +0000492 /* Successful write. */
Ben Lindstrom6d218f42001-09-18 05:53:12 +0000493 if (fdin_is_tty && dlen >= 1 && data[0] != '\r' &&
494 tcgetattr(fdin, &tio) == 0 &&
Damien Miller79438cc2001-02-16 12:34:57 +1100495 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
Kevin Stevesb7f036f2001-02-15 17:27:15 +0000496 /*
497 * Simulate echo to reduce the impact of
498 * traffic analysis
499 */
Ben Lindstrome229b252001-03-05 06:28:06 +0000500 packet_send_ignore(len);
Ben Lindstromaa630de2001-02-10 23:44:47 +0000501 packet_send();
502 }
503 /* Consume the data from the buffer. */
Damien Miller95def091999-11-25 00:26:21 +1100504 buffer_consume(&stdin_buffer, len);
505 /* Update the count of bytes written to the program. */
506 stdin_bytes += len;
507 }
508 }
509 /* Send any buffered packet data to the client. */
510 if (FD_ISSET(connection_out, writeset))
511 packet_write_poll();
512}
513
514/*
515 * Wait until all buffered output has been sent to the client.
516 * This is used when the program terminates.
517 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000518static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000519drain_output(void)
Damien Miller95def091999-11-25 00:26:21 +1100520{
521 /* Send any buffered stdout data to the client. */
522 if (buffer_len(&stdout_buffer) > 0) {
523 packet_start(SSH_SMSG_STDOUT_DATA);
524 packet_put_string(buffer_ptr(&stdout_buffer),
525 buffer_len(&stdout_buffer));
526 packet_send();
527 /* Update the count of sent bytes. */
528 stdout_bytes += buffer_len(&stdout_buffer);
529 }
530 /* Send any buffered stderr data to the client. */
531 if (buffer_len(&stderr_buffer) > 0) {
532 packet_start(SSH_SMSG_STDERR_DATA);
533 packet_put_string(buffer_ptr(&stderr_buffer),
534 buffer_len(&stderr_buffer));
535 packet_send();
536 /* Update the count of sent bytes. */
537 stderr_bytes += buffer_len(&stderr_buffer);
538 }
539 /* Wait until all buffered data has been written to the client. */
540 packet_write_wait();
541}
542
Ben Lindstrombba81212001-06-25 05:01:22 +0000543static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000544process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000545{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000546 dispatch_run(DISPATCH_NONBLOCK, NULL, active_state);
Damien Millerb38eff82000-04-01 11:09:21 +1000547}
548
Damien Miller95def091999-11-25 00:26:21 +1100549/*
550 * Performs the interactive session. This handles data transmission between
551 * the client and the program. Note that the notion of stdin, stdout, and
552 * stderr in this function is sort of reversed: this function writes to
553 * stdin (of the child program), and reads from stdout and stderr (of the
554 * child program).
555 */
Damien Miller4af51302000-04-16 11:18:38 +1000556void
Damien Miller166fca82000-04-20 07:42:21 +1000557server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg)
Damien Miller95def091999-11-25 00:26:21 +1100558{
Damien Miller5e953212001-01-30 09:14:00 +1100559 fd_set *readset = NULL, *writeset = NULL;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000560 int max_fd = 0;
561 u_int nalloc = 0;
Damien Miller166fca82000-04-20 07:42:21 +1000562 int wait_status; /* Status returned by wait(). */
563 pid_t wait_pid; /* pid returned by wait(). */
Damien Miller95def091999-11-25 00:26:21 +1100564 int waiting_termination = 0; /* Have displayed waiting close message. */
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000565 u_int64_t max_time_milliseconds;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000566 u_int previous_stdout_buffer_bytes;
567 u_int stdout_buffer_bytes;
Damien Miller95def091999-11-25 00:26:21 +1100568 int type;
569
570 debug("Entering interactive session.");
571
572 /* Initialize the SIGCHLD kludge. */
Damien Miller95def091999-11-25 00:26:21 +1100573 child_terminated = 0;
Kevin Stevese26a1552001-07-26 17:51:49 +0000574 mysignal(SIGCHLD, sigchld_handler);
Damien Miller95def091999-11-25 00:26:21 +1100575
Damien Miller24ecf612005-11-05 15:16:52 +1100576 if (!use_privsep) {
577 signal(SIGTERM, sigterm_handler);
578 signal(SIGINT, sigterm_handler);
579 signal(SIGQUIT, sigterm_handler);
580 }
581
Damien Miller95def091999-11-25 00:26:21 +1100582 /* Initialize our global variables. */
583 fdin = fdin_arg;
584 fdout = fdout_arg;
585 fderr = fderr_arg;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000586
587 /* nonblocking IO */
588 set_nonblock(fdin);
589 set_nonblock(fdout);
Damien Miller7d6656c2000-05-20 15:22:36 +1000590 /* we don't have stderr for interactive terminal sessions, see below */
591 if (fderr != -1)
592 set_nonblock(fderr);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000593
Damien Miller225736c2001-02-19 21:51:08 +1100594 if (!(datafellows & SSH_BUG_IGNOREMSG) && isatty(fdin))
595 fdin_is_tty = 1;
596
Damien Miller95def091999-11-25 00:26:21 +1100597 connection_in = packet_get_connection_in();
598 connection_out = packet_get_connection_out();
599
Damien Millerf6681a32001-12-21 14:53:11 +1100600 notify_setup();
601
Damien Miller95def091999-11-25 00:26:21 +1100602 previous_stdout_buffer_bytes = 0;
603
604 /* Set approximate I/O buffer size. */
605 if (packet_is_interactive())
606 buffer_high = 4096;
607 else
608 buffer_high = 64 * 1024;
609
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000610#if 0
Damien Miller95def091999-11-25 00:26:21 +1100611 /* Initialize max_fd to the maximum of the known file descriptors. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000612 max_fd = MAX(connection_in, connection_out);
613 max_fd = MAX(max_fd, fdin);
614 max_fd = MAX(max_fd, fdout);
Damien Miller5e953212001-01-30 09:14:00 +1100615 if (fderr != -1)
616 max_fd = MAX(max_fd, fderr);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000617#endif
Damien Miller95def091999-11-25 00:26:21 +1100618
619 /* Initialize Initialize buffers. */
620 buffer_init(&stdin_buffer);
621 buffer_init(&stdout_buffer);
622 buffer_init(&stderr_buffer);
623
Damien Miller5428f641999-11-25 11:54:57 +1100624 /*
625 * If we have no separate fderr (which is the case when we have a pty
626 * - there we cannot make difference between data sent to stdout and
627 * stderr), indicate that we have seen an EOF from stderr. This way
Damien Miller788f2122005-11-05 15:14:59 +1100628 * we don't need to check the descriptor everywhere.
Damien Miller5428f641999-11-25 11:54:57 +1100629 */
Damien Miller95def091999-11-25 00:26:21 +1100630 if (fderr == -1)
631 fderr_eof = 1;
632
Damien Millerb38eff82000-04-01 11:09:21 +1000633 server_init_dispatch();
634
Damien Miller95def091999-11-25 00:26:21 +1100635 /* Main loop of the server for the interactive session mode. */
636 for (;;) {
Damien Miller95def091999-11-25 00:26:21 +1100637
638 /* Process buffered packets from the client. */
639 process_buffered_input_packets();
640
Damien Miller5428f641999-11-25 11:54:57 +1100641 /*
642 * If we have received eof, and there is no more pending
643 * input data, cause a real eof by closing fdin.
644 */
Damien Miller95def091999-11-25 00:26:21 +1100645 if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) {
Damien Millerb38eff82000-04-01 11:09:21 +1000646 if (fdin != fdout)
Damien Miller95def091999-11-25 00:26:21 +1100647 close(fdin);
648 else
649 shutdown(fdin, SHUT_WR); /* We will no longer send. */
Damien Miller95def091999-11-25 00:26:21 +1100650 fdin = -1;
651 }
Damien Miller5428f641999-11-25 11:54:57 +1100652 /* Make packets from buffered stderr data to send to the client. */
Damien Miller95def091999-11-25 00:26:21 +1100653 make_packets_from_stderr_data();
654
Damien Miller5428f641999-11-25 11:54:57 +1100655 /*
656 * Make packets from buffered stdout data to send to the
657 * client. If there is very little to send, this arranges to
658 * not send them now, but to wait a short while to see if we
659 * are getting more data. This is necessary, as some systems
660 * wake up readers from a pty after each separate character.
661 */
Damien Miller95def091999-11-25 00:26:21 +1100662 max_time_milliseconds = 0;
663 stdout_buffer_bytes = buffer_len(&stdout_buffer);
664 if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 &&
665 stdout_buffer_bytes != previous_stdout_buffer_bytes) {
666 /* try again after a while */
667 max_time_milliseconds = 10;
668 } else {
669 /* Send it now. */
670 make_packets_from_stdout_data();
671 }
672 previous_stdout_buffer_bytes = buffer_len(&stdout_buffer);
673
674 /* Send channel data to the client. */
675 if (packet_not_very_much_data_to_write())
676 channel_output_poll();
677
Damien Miller5428f641999-11-25 11:54:57 +1100678 /*
679 * Bail out of the loop if the program has closed its output
680 * descriptors, and we have no more data to send to the
681 * client, and there is no pending buffered data.
682 */
Damien Miller43dc8da2000-11-29 15:55:17 +1100683 if (fdout_eof && fderr_eof && !packet_have_data_to_write() &&
684 buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100685 if (!channel_still_open())
Damien Millerb38eff82000-04-01 11:09:21 +1000686 break;
Damien Miller95def091999-11-25 00:26:21 +1100687 if (!waiting_termination) {
688 const char *s = "Waiting for forwarded connections to terminate...\r\n";
689 char *cp;
690 waiting_termination = 1;
691 buffer_append(&stderr_buffer, s, strlen(s));
692
693 /* Display list of open channels. */
694 cp = channel_open_message();
695 buffer_append(&stderr_buffer, cp, strlen(cp));
Darren Tuckera627d422013-06-02 07:31:17 +1000696 free(cp);
Damien Miller95def091999-11-25 00:26:21 +1100697 }
698 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000699 max_fd = MAX(connection_in, connection_out);
700 max_fd = MAX(max_fd, fdin);
701 max_fd = MAX(max_fd, fdout);
702 max_fd = MAX(max_fd, fderr);
Damien Millerf6681a32001-12-21 14:53:11 +1100703 max_fd = MAX(max_fd, notify_pipe[0]);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000704
Damien Miller95def091999-11-25 00:26:21 +1100705 /* Sleep in select() until we can do something. */
Damien Miller5e953212001-01-30 09:14:00 +1100706 wait_until_can_do_something(&readset, &writeset, &max_fd,
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000707 &nalloc, max_time_milliseconds);
Damien Miller95def091999-11-25 00:26:21 +1100708
Damien Miller24ecf612005-11-05 15:16:52 +1100709 if (received_sigterm) {
Darren Tucker3e1027c2012-12-07 13:07:46 +1100710 logit("Exiting on signal %d", (int)received_sigterm);
Damien Miller24ecf612005-11-05 15:16:52 +1100711 /* Clean up sessions, utmp, etc. */
712 cleanup_exit(255);
713 }
714
Damien Miller95def091999-11-25 00:26:21 +1100715 /* Process any channel events. */
Damien Miller5e953212001-01-30 09:14:00 +1100716 channel_after_select(readset, writeset);
Damien Miller95def091999-11-25 00:26:21 +1100717
718 /* Process input from the client and from program stdout/stderr. */
Damien Miller5e953212001-01-30 09:14:00 +1100719 process_input(readset);
Damien Miller95def091999-11-25 00:26:21 +1100720
721 /* Process output to the client and to program stdin. */
Damien Miller5e953212001-01-30 09:14:00 +1100722 process_output(writeset);
Damien Miller95def091999-11-25 00:26:21 +1100723 }
Darren Tuckera627d422013-06-02 07:31:17 +1000724 free(readset);
725 free(writeset);
Damien Miller95def091999-11-25 00:26:21 +1100726
Damien Miller95def091999-11-25 00:26:21 +1100727 /* Cleanup and termination code. */
728
729 /* Wait until all output has been sent to the client. */
730 drain_output();
731
732 debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100733 stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes);
Damien Miller95def091999-11-25 00:26:21 +1100734
735 /* Free and clear the buffers. */
736 buffer_free(&stdin_buffer);
737 buffer_free(&stdout_buffer);
738 buffer_free(&stderr_buffer);
739
740 /* Close the file descriptors. */
741 if (fdout != -1)
742 close(fdout);
743 fdout = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000744 fdout_eof = 1;
Damien Miller95def091999-11-25 00:26:21 +1100745 if (fderr != -1)
746 close(fderr);
747 fderr = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000748 fderr_eof = 1;
Damien Miller95def091999-11-25 00:26:21 +1100749 if (fdin != -1)
750 close(fdin);
751 fdin = -1;
752
Ben Lindstrom601e4362001-06-21 03:19:23 +0000753 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +1100754
Damien Miller95def091999-11-25 00:26:21 +1100755 /* We no longer want our SIGCHLD handler to be called. */
Kevin Stevese26a1552001-07-26 17:51:49 +0000756 mysignal(SIGCHLD, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +1100757
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000758 while ((wait_pid = waitpid(-1, &wait_status, 0)) < 0)
759 if (errno != EINTR)
760 packet_disconnect("wait: %.100s", strerror(errno));
761 if (wait_pid != pid)
Ben Lindstromce0f6342002-06-11 16:42:49 +0000762 error("Strange, wait returned pid %ld, expected %ld",
763 (long)wait_pid, (long)pid);
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000764
Damien Miller95def091999-11-25 00:26:21 +1100765 /* Check if it exited normally. */
766 if (WIFEXITED(wait_status)) {
767 /* Yes, normal exit. Get exit status and send it to the client. */
768 debug("Command exited with status %d.", WEXITSTATUS(wait_status));
769 packet_start(SSH_SMSG_EXITSTATUS);
770 packet_put_int(WEXITSTATUS(wait_status));
771 packet_send();
772 packet_write_wait();
773
Damien Miller5428f641999-11-25 11:54:57 +1100774 /*
775 * Wait for exit confirmation. Note that there might be
776 * other packets coming before it; however, the program has
777 * already died so we just ignore them. The client is
778 * supposed to respond with the confirmation when it receives
779 * the exit status.
780 */
Damien Miller95def091999-11-25 00:26:21 +1100781 do {
Damien Millerdff50992002-01-22 23:16:32 +1100782 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100783 }
784 while (type != SSH_CMSG_EXIT_CONFIRMATION);
785
786 debug("Received exit confirmation.");
787 return;
788 }
789 /* Check if the program terminated due to a signal. */
790 if (WIFSIGNALED(wait_status))
791 packet_disconnect("Command terminated on signal %d.",
792 WTERMSIG(wait_status));
793
794 /* Some weird exit cause. Just exit. */
795 packet_disconnect("wait returned status %04x.", wait_status);
796 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000797}
Damien Millerb38eff82000-04-01 11:09:21 +1000798
Damien Miller3ec27592001-10-12 11:35:04 +1000799static void
800collect_children(void)
801{
802 pid_t pid;
803 sigset_t oset, nset;
804 int status;
805
806 /* block SIGCHLD while we check for dead children */
807 sigemptyset(&nset);
808 sigaddset(&nset, SIGCHLD);
809 sigprocmask(SIG_BLOCK, &nset, &oset);
810 if (child_terminated) {
Damien Millerec04f362006-03-15 12:01:34 +1100811 debug("Received SIGCHLD.");
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000812 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
813 (pid < 0 && errno == EINTR))
814 if (pid > 0)
815 session_close_by_pid(pid, status);
Damien Miller3ec27592001-10-12 11:35:04 +1000816 child_terminated = 0;
817 }
818 sigprocmask(SIG_SETMASK, &oset, NULL);
819}
820
Damien Miller4af51302000-04-16 11:18:38 +1000821void
Ben Lindstrombddd5512001-07-04 04:53:53 +0000822server_loop2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +1000823{
Damien Miller5e953212001-01-30 09:14:00 +1100824 fd_set *readset = NULL, *writeset = NULL;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000825 int max_fd;
Damien Millerce986542013-07-18 16:12:44 +1000826 u_int nalloc = 0;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000827 u_int64_t rekey_timeout_ms = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +1000828
829 debug("Entering interactive session for SSH2.");
830
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000831 mysignal(SIGCHLD, sigchld_handler);
Damien Millerefb4afe2000-04-12 18:45:05 +1000832 child_terminated = 0;
833 connection_in = packet_get_connection_in();
834 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +1100835
Damien Miller24ecf612005-11-05 15:16:52 +1100836 if (!use_privsep) {
837 signal(SIGTERM, sigterm_handler);
838 signal(SIGINT, sigterm_handler);
839 signal(SIGQUIT, sigterm_handler);
840 }
841
Damien Millerf6681a32001-12-21 14:53:11 +1100842 notify_setup();
843
Damien Miller5e953212001-01-30 09:14:00 +1100844 max_fd = MAX(connection_in, connection_out);
Damien Millerf6681a32001-12-21 14:53:11 +1100845 max_fd = MAX(max_fd, notify_pipe[0]);
846
Damien Millerefb4afe2000-04-12 18:45:05 +1000847 server_init_dispatch();
848
849 for (;;) {
850 process_buffered_input_packets();
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000851
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000852 if (!ssh_packet_is_rekeying(active_state) &&
853 packet_not_very_much_data_to_write())
Damien Millerefb4afe2000-04-12 18:45:05 +1000854 channel_output_poll();
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000855 if (options.rekey_interval > 0 && compat20 &&
856 !ssh_packet_is_rekeying(active_state))
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000857 rekey_timeout_ms = packet_get_rekey_timeout() * 1000;
858 else
859 rekey_timeout_ms = 0;
860
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000861 wait_until_can_do_something(&readset, &writeset, &max_fd,
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000862 &nalloc, rekey_timeout_ms);
Damien Miller52b77be2001-10-10 15:14:37 +1000863
Damien Miller24ecf612005-11-05 15:16:52 +1100864 if (received_sigterm) {
Darren Tucker3e1027c2012-12-07 13:07:46 +1100865 logit("Exiting on signal %d", (int)received_sigterm);
Damien Miller24ecf612005-11-05 15:16:52 +1100866 /* Clean up sessions, utmp, etc. */
867 cleanup_exit(255);
868 }
869
Damien Miller3ec27592001-10-12 11:35:04 +1000870 collect_children();
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000871 if (!ssh_packet_is_rekeying(active_state))
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000872 channel_after_select(readset, writeset);
Damien Miller5e953212001-01-30 09:14:00 +1100873 process_input(readset);
Ben Lindstrome34ab4c2001-04-07 01:12:11 +0000874 if (connection_closed)
875 break;
Damien Miller5e953212001-01-30 09:14:00 +1100876 process_output(writeset);
Damien Millerefb4afe2000-04-12 18:45:05 +1000877 }
Damien Miller3ec27592001-10-12 11:35:04 +1000878 collect_children();
879
Darren Tuckera627d422013-06-02 07:31:17 +1000880 free(readset);
881 free(writeset);
Damien Miller5e953212001-01-30 09:14:00 +1100882
Damien Miller52b77be2001-10-10 15:14:37 +1000883 /* free all channels, no more reads and writes */
884 channel_free_all();
Ben Lindstrom4983d5e2001-07-04 05:17:40 +0000885
Damien Miller3ec27592001-10-12 11:35:04 +1000886 /* free remaining sessions, e.g. remove wtmp entries */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000887 session_destroy_all(NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +1000888}
889
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000890static int
Damien Millerb5820f42003-12-17 16:27:32 +1100891server_input_keep_alive(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000892{
Damien Millerb5820f42003-12-17 16:27:32 +1100893 debug("Got %d/%u for keepalive", type, seq);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100894 /*
Ben Lindstrom2f0304c2001-04-29 19:49:14 +0000895 * reset timeout, since we got a sane answer from the client.
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000896 * even if this was generated by something other than
897 * the bogus CHANNEL_REQUEST we send for keepalives.
898 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000899 packet_set_alive_timeouts(0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000900 return 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000901}
902
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000903static int
Damien Miller630d6f42002-01-22 23:17:30 +1100904server_input_stdin_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000905{
906 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000907 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000908
909 /* Stdin data from the client. Append it to the buffer. */
910 /* Ignore any data if the client has closed stdin. */
911 if (fdin == -1)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000912 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000913 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +1100914 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000915 buffer_append(&stdin_buffer, data, data_len);
Damien Millera5103f42014-02-04 11:20:14 +1100916 explicit_bzero(data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000917 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000918 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000919}
920
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000921static int
Damien Miller630d6f42002-01-22 23:17:30 +1100922server_input_eof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000923{
924 /*
925 * Eof from the client. The stdin descriptor to the
926 * program will be closed when all buffered data has
927 * drained.
928 */
929 debug("EOF received for stdin.");
Damien Miller48b03fc2002-01-22 23:11:40 +1100930 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000931 stdin_eof = 1;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000932 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000933}
934
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000935static int
Damien Miller630d6f42002-01-22 23:17:30 +1100936server_input_window_size(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000937{
Damien Miller71a73672006-03-26 14:04:36 +1100938 u_int row = packet_get_int();
939 u_int col = packet_get_int();
940 u_int xpixel = packet_get_int();
941 u_int ypixel = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +1000942
943 debug("Window change received.");
Damien Miller48b03fc2002-01-22 23:11:40 +1100944 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000945 if (fdin != -1)
946 pty_change_window_size(fdin, row, col, xpixel, ypixel);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000947 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000948}
949
Ben Lindstrombba81212001-06-25 05:01:22 +0000950static Channel *
Damien Millerfb1310e2004-01-21 11:02:50 +1100951server_request_direct_tcpip(void)
Damien Millerefb4afe2000-04-12 18:45:05 +1000952{
Damien Milleraa5b3f82012-12-03 09:50:54 +1100953 Channel *c = NULL;
Damien Miller4af51302000-04-16 11:18:38 +1000954 char *target, *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100955 u_short target_port, originator_port;
Damien Millerefb4afe2000-04-12 18:45:05 +1000956
Damien Miller4af51302000-04-16 11:18:38 +1000957 target = packet_get_string(NULL);
958 target_port = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +1000959 originator = packet_get_string(NULL);
960 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100961 packet_check_eom();
Damien Millerb1715dc2000-05-30 13:44:51 +1000962
Damien Millerbd740252008-05-19 15:37:09 +1000963 debug("server_request_direct_tcpip: originator %s port %d, target %s "
964 "port %d", originator, originator_port, target, target_port);
Damien Millerf6d9e222000-06-18 14:50:44 +1000965
Damien Milleraa5b3f82012-12-03 09:50:54 +1100966 /* XXX fine grained permissions */
967 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 &&
968 !no_port_forwarding_flag) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000969 c = channel_connect_to_port(target, target_port,
Damien Milleraa5b3f82012-12-03 09:50:54 +1100970 "direct-tcpip", "direct-tcpip");
971 } else {
972 logit("refused local port forward: "
973 "originator %s port %d, target %s port %d",
974 originator, originator_port, target, target_port);
975 }
Damien Millerbd740252008-05-19 15:37:09 +1000976
Darren Tuckera627d422013-06-02 07:31:17 +1000977 free(originator);
978 free(target);
Damien Millerbd740252008-05-19 15:37:09 +1000979
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000980 return c;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100981}
982
Ben Lindstrombba81212001-06-25 05:01:22 +0000983static Channel *
Damien Miller7acefbb2014-07-18 14:11:24 +1000984server_request_direct_streamlocal(void)
985{
986 Channel *c = NULL;
987 char *target, *originator;
988 u_short originator_port;
989
990 target = packet_get_string(NULL);
991 originator = packet_get_string(NULL);
992 originator_port = packet_get_int();
993 packet_check_eom();
994
995 debug("server_request_direct_streamlocal: originator %s port %d, target %s",
996 originator, originator_port, target);
997
998 /* XXX fine grained permissions */
999 if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 &&
1000 !no_port_forwarding_flag) {
1001 c = channel_connect_to_path(target,
1002 "direct-streamlocal@openssh.com", "direct-streamlocal");
1003 } else {
1004 logit("refused streamlocal port forward: "
1005 "originator %s port %d, target %s",
1006 originator, originator_port, target);
1007 }
1008
1009 free(originator);
1010 free(target);
1011
1012 return c;
1013}
1014
1015static Channel *
Damien Millerd27b9472005-12-13 19:29:02 +11001016server_request_tun(void)
1017{
1018 Channel *c = NULL;
Damien Miller7b58e802005-12-13 19:33:19 +11001019 int mode, tun;
1020 int sock;
Damien Millerd27b9472005-12-13 19:29:02 +11001021
Damien Miller7b58e802005-12-13 19:33:19 +11001022 mode = packet_get_int();
1023 switch (mode) {
1024 case SSH_TUNMODE_POINTOPOINT:
1025 case SSH_TUNMODE_ETHERNET:
1026 break;
1027 default:
1028 packet_send_debug("Unsupported tunnel device mode.");
1029 return NULL;
1030 }
1031 if ((options.permit_tun & mode) == 0) {
1032 packet_send_debug("Server has rejected tunnel device "
1033 "forwarding");
Damien Millerd27b9472005-12-13 19:29:02 +11001034 return NULL;
1035 }
1036
1037 tun = packet_get_int();
Darren Tucker0d0e8f02005-12-20 16:08:42 +11001038 if (forced_tun_device != -1) {
Damien Millerf0b15df2006-03-26 13:59:20 +11001039 if (tun != SSH_TUNID_ANY && forced_tun_device != tun)
Damien Millerd27b9472005-12-13 19:29:02 +11001040 goto done;
1041 tun = forced_tun_device;
1042 }
Damien Miller7b58e802005-12-13 19:33:19 +11001043 sock = tun_open(tun, mode);
Damien Millerd27b9472005-12-13 19:29:02 +11001044 if (sock < 0)
1045 goto done;
1046 c = channel_new("tun", SSH_CHANNEL_OPEN, sock, sock, -1,
1047 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1048 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001049#if defined(SSH_TUN_FILTER)
1050 if (mode == SSH_TUNMODE_POINTOPOINT)
1051 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001052 sys_tun_outfilter, NULL, NULL);
Damien Miller598bbc22005-12-31 16:33:36 +11001053#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001054
1055 done:
1056 if (c == NULL)
1057 packet_send_debug("Failed to open the tunnel device.");
1058 return c;
1059}
1060
1061static Channel *
Damien Millerfb1310e2004-01-21 11:02:50 +11001062server_request_session(void)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001063{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001064 Channel *c;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001065
1066 debug("input_session_request");
Damien Miller48b03fc2002-01-22 23:11:40 +11001067 packet_check_eom();
Darren Tucker8901fa92008-06-11 09:34:01 +10001068
1069 if (no_more_sessions) {
1070 packet_disconnect("Possible attack: attempt to open a session "
1071 "after additional sessions disabled");
1072 }
1073
Damien Miller0bc1bd82000-11-13 22:57:25 +11001074 /*
1075 * A server session has no fd to read or write until a
1076 * CHANNEL_REQUEST for a shell is made, so we set the type to
1077 * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all
1078 * CHANNEL_REQUEST messages is registered.
1079 */
Damien Millerfb1310e2004-01-21 11:02:50 +11001080 c = channel_new("session", SSH_CHANNEL_LARVAL,
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001081 -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001082 0, "server-session", 1);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001083 if (session_open(the_authctxt, c->self) != 1) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001084 debug("session open failed, free channel %d", c->self);
1085 channel_free(c);
1086 return NULL;
1087 }
Damien Miller39eda6e2005-11-05 14:52:50 +11001088 channel_register_cleanup(c->self, session_close_by_channel, 0);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001089 return c;
Damien Millerefb4afe2000-04-12 18:45:05 +10001090}
1091
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001092static int
Damien Miller630d6f42002-01-22 23:17:30 +11001093server_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10001094{
1095 Channel *c = NULL;
1096 char *ctype;
Damien Millerefb4afe2000-04-12 18:45:05 +10001097 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001098 u_int rmaxpack, rwindow, len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001099
1100 ctype = packet_get_string(&len);
1101 rchan = packet_get_int();
1102 rwindow = packet_get_int();
1103 rmaxpack = packet_get_int();
1104
Damien Miller62cee002000-09-23 17:15:56 +11001105 debug("server_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001106 ctype, rchan, rwindow, rmaxpack);
1107
1108 if (strcmp(ctype, "session") == 0) {
Damien Millerfb1310e2004-01-21 11:02:50 +11001109 c = server_request_session();
Damien Millerefb4afe2000-04-12 18:45:05 +10001110 } else if (strcmp(ctype, "direct-tcpip") == 0) {
Damien Millerfb1310e2004-01-21 11:02:50 +11001111 c = server_request_direct_tcpip();
Damien Miller7acefbb2014-07-18 14:11:24 +10001112 } else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) {
1113 c = server_request_direct_streamlocal();
Damien Millerd27b9472005-12-13 19:29:02 +11001114 } else if (strcmp(ctype, "tun@openssh.com") == 0) {
1115 c = server_request_tun();
Damien Millerefb4afe2000-04-12 18:45:05 +10001116 }
1117 if (c != NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001118 debug("server_input_channel_open: confirm %s", ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +10001119 c->remote_id = rchan;
1120 c->remote_window = rwindow;
1121 c->remote_maxpacket = rmaxpack;
Ben Lindstrom69128662001-05-08 20:07:39 +00001122 if (c->type != SSH_CHANNEL_CONNECTING) {
1123 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1124 packet_put_int(c->remote_id);
1125 packet_put_int(c->self);
1126 packet_put_int(c->local_window);
1127 packet_put_int(c->local_maxpacket);
1128 packet_send();
1129 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001130 } else {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001131 debug("server_input_channel_open: failure %s", ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +10001132 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1133 packet_put_int(rchan);
1134 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001135 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001136 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001137 packet_put_cstring("");
1138 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001139 packet_send();
1140 }
Darren Tuckera627d422013-06-02 07:31:17 +10001141 free(ctype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001142 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001143}
1144
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001145static int
djm@openbsd.org523463a2015-02-16 22:13:32 +00001146server_input_hostkeys_prove(struct sshbuf **respp)
1147{
1148 struct ssh *ssh = active_state; /* XXX */
1149 struct sshbuf *resp = NULL;
1150 struct sshbuf *sigbuf = NULL;
1151 struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL;
1152 int r, ndx, success = 0;
1153 const u_char *blob;
1154 u_char *sig = 0;
1155 size_t blen, slen;
1156
1157 if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL)
1158 fatal("%s: sshbuf_new", __func__);
1159
1160 while (ssh_packet_remaining(ssh) > 0) {
1161 sshkey_free(key);
1162 key = NULL;
1163 if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 ||
1164 (r = sshkey_from_blob(blob, blen, &key)) != 0) {
1165 error("%s: couldn't parse key: %s",
1166 __func__, ssh_err(r));
1167 goto out;
1168 }
1169 /*
1170 * Better check that this is actually one of our hostkeys
1171 * before attempting to sign anything with it.
1172 */
1173 if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) {
1174 error("%s: unknown host %s key",
1175 __func__, sshkey_type(key));
1176 goto out;
1177 }
1178 /*
1179 * XXX refactor: make kex->sign just use an index rather
1180 * than passing in public and private keys
1181 */
1182 if ((key_prv = get_hostkey_by_index(ndx)) == NULL &&
1183 (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) {
1184 error("%s: can't retrieve hostkey %d", __func__, ndx);
1185 goto out;
1186 }
1187 sshbuf_reset(sigbuf);
1188 free(sig);
1189 sig = NULL;
djm@openbsd.org44732de2015-02-20 22:17:21 +00001190 if ((r = sshbuf_put_cstring(sigbuf,
1191 "hostkeys-prove-00@openssh.com")) != 0 ||
1192 (r = sshbuf_put_string(sigbuf,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001193 ssh->kex->session_id, ssh->kex->session_id_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00001194 (r = sshkey_puts(key, sigbuf)) != 0 ||
1195 (r = ssh->kex->sign(key_prv, key_pub, &sig, &slen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001196 sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), NULL, 0)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00001197 (r = sshbuf_put_string(resp, sig, slen)) != 0) {
1198 error("%s: couldn't prepare signature: %s",
1199 __func__, ssh_err(r));
1200 goto out;
1201 }
1202 }
1203 /* Success */
1204 *respp = resp;
1205 resp = NULL; /* don't free it */
1206 success = 1;
1207 out:
1208 free(sig);
1209 sshbuf_free(resp);
1210 sshbuf_free(sigbuf);
1211 sshkey_free(key);
1212 return success;
1213}
1214
1215static int
Damien Miller630d6f42002-01-22 23:17:30 +11001216server_input_global_request(int type, u_int32_t seq, void *ctxt)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001217{
1218 char *rtype;
1219 int want_reply;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001220 int r, success = 0, allocated_listen_port = 0;
1221 struct sshbuf *resp = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001222
1223 rtype = packet_get_string(NULL);
1224 want_reply = packet_get_char();
1225 debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001226
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001227 /* -R style forwarding */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001228 if (strcmp(rtype, "tcpip-forward") == 0) {
1229 struct passwd *pw;
Damien Miller7acefbb2014-07-18 14:11:24 +10001230 struct Forward fwd;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001231
Darren Tucker3e33cec2003-10-02 16:12:36 +10001232 pw = the_authctxt->pw;
Damien Miller4bb1dd32003-11-18 22:01:25 +11001233 if (pw == NULL || !the_authctxt->valid)
Damien Miller3e3b5142003-11-17 21:13:40 +11001234 fatal("server_input_global_request: no/invalid user");
Damien Miller7acefbb2014-07-18 14:11:24 +10001235 memset(&fwd, 0, sizeof(fwd));
1236 fwd.listen_host = packet_get_string(NULL);
1237 fwd.listen_port = (u_short)packet_get_int();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001238 debug("server_input_global_request: tcpip-forward listen %s port %d",
Damien Miller7acefbb2014-07-18 14:11:24 +10001239 fwd.listen_host, fwd.listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001240
1241 /* check permissions */
Damien Milleraa5b3f82012-12-03 09:50:54 +11001242 if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 ||
Damien Miller330d5852009-02-14 16:33:09 +11001243 no_port_forwarding_flag ||
Damien Millerd4e7d592014-08-19 11:14:17 +10001244 (!want_reply && fwd.listen_port == 0)
Ben Lindstrom99a4e142002-07-09 14:06:40 +00001245#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Millerd4e7d592014-08-19 11:14:17 +10001246 || (fwd.listen_port != 0 && fwd.listen_port < IPPORT_RESERVED &&
Damien Miller7acefbb2014-07-18 14:11:24 +10001247 pw->pw_uid != 0)
Ben Lindstrom99a4e142002-07-09 14:06:40 +00001248#endif
Damien Millerb6f72f52005-07-17 17:26:43 +10001249 ) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001250 success = 0;
1251 packet_send_debug("Server has disabled port forwarding.");
1252 } else {
1253 /* Start listening on the port */
Damien Miller7acefbb2014-07-18 14:11:24 +10001254 success = channel_setup_remote_fwd_listener(&fwd,
1255 &allocated_listen_port, &options.fwd_opts);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001256 }
Damien Miller7acefbb2014-07-18 14:11:24 +10001257 free(fwd.listen_host);
djm@openbsd.org523463a2015-02-16 22:13:32 +00001258 if ((resp = sshbuf_new()) == NULL)
1259 fatal("%s: sshbuf_new", __func__);
djm@openbsd.orgb1d6b392015-11-28 06:41:03 +00001260 if (allocated_listen_port != 0 &&
1261 (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001262 fatal("%s: sshbuf_put_u32: %s", __func__, ssh_err(r));
Darren Tuckere7066df2004-05-24 10:18:05 +10001263 } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001264 struct Forward fwd;
Darren Tuckere7066df2004-05-24 10:18:05 +10001265
Damien Miller7acefbb2014-07-18 14:11:24 +10001266 memset(&fwd, 0, sizeof(fwd));
1267 fwd.listen_host = packet_get_string(NULL);
1268 fwd.listen_port = (u_short)packet_get_int();
Darren Tuckere7066df2004-05-24 10:18:05 +10001269 debug("%s: cancel-tcpip-forward addr %s port %d", __func__,
Damien Miller7acefbb2014-07-18 14:11:24 +10001270 fwd.listen_host, fwd.listen_port);
Darren Tuckere7066df2004-05-24 10:18:05 +10001271
Damien Miller7acefbb2014-07-18 14:11:24 +10001272 success = channel_cancel_rport_listener(&fwd);
1273 free(fwd.listen_host);
1274 } else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) {
1275 struct Forward fwd;
1276
1277 memset(&fwd, 0, sizeof(fwd));
1278 fwd.listen_path = packet_get_string(NULL);
1279 debug("server_input_global_request: streamlocal-forward listen path %s",
1280 fwd.listen_path);
1281
1282 /* check permissions */
1283 if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0
1284 || no_port_forwarding_flag) {
1285 success = 0;
1286 packet_send_debug("Server has disabled port forwarding.");
1287 } else {
1288 /* Start listening on the socket */
1289 success = channel_setup_remote_fwd_listener(
1290 &fwd, NULL, &options.fwd_opts);
1291 }
1292 free(fwd.listen_path);
1293 } else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) {
1294 struct Forward fwd;
1295
1296 memset(&fwd, 0, sizeof(fwd));
1297 fwd.listen_path = packet_get_string(NULL);
1298 debug("%s: cancel-streamlocal-forward path %s", __func__,
1299 fwd.listen_path);
1300
1301 success = channel_cancel_rport_listener(&fwd);
1302 free(fwd.listen_path);
Darren Tucker8901fa92008-06-11 09:34:01 +10001303 } else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) {
1304 no_more_sessions = 1;
1305 success = 1;
djm@openbsd.org44732de2015-02-20 22:17:21 +00001306 } else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) {
djm@openbsd.org523463a2015-02-16 22:13:32 +00001307 success = server_input_hostkeys_prove(&resp);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001308 }
1309 if (want_reply) {
1310 packet_start(success ?
1311 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
djm@openbsd.org523463a2015-02-16 22:13:32 +00001312 if (success && resp != NULL)
1313 ssh_packet_put_raw(active_state, sshbuf_ptr(resp),
1314 sshbuf_len(resp));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001315 packet_send();
1316 packet_write_wait();
1317 }
Darren Tuckera627d422013-06-02 07:31:17 +10001318 free(rtype);
djm@openbsd.org523463a2015-02-16 22:13:32 +00001319 sshbuf_free(resp);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001320 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001321}
Damien Miller4f7becb2006-03-26 14:10:14 +11001322
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001323static int
Damien Millerc7ef63d2002-02-05 12:21:42 +11001324server_input_channel_req(int type, u_int32_t seq, void *ctxt)
1325{
1326 Channel *c;
1327 int id, reply, success = 0;
1328 char *rtype;
1329
1330 id = packet_get_int();
1331 rtype = packet_get_string(NULL);
1332 reply = packet_get_char();
1333
1334 debug("server_input_channel_req: channel %d request %s reply %d",
1335 id, rtype, reply);
1336
1337 if ((c = channel_lookup(id)) == NULL)
1338 packet_disconnect("server_input_channel_req: "
1339 "unknown channel %d", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001340 if (!strcmp(rtype, "eow@openssh.com")) {
1341 packet_check_eom();
1342 chan_rcvd_eow(c);
Darren Tucker8810dd42008-07-02 22:32:14 +10001343 } else if ((c->type == SSH_CHANNEL_LARVAL ||
1344 c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0)
Damien Millerc7ef63d2002-02-05 12:21:42 +11001345 success = session_input_channel_req(c, rtype);
Damien Millerc5893782014-05-15 13:48:49 +10001346 if (reply && !(c->flags & CHAN_CLOSE_SENT)) {
Damien Millerc7ef63d2002-02-05 12:21:42 +11001347 packet_start(success ?
1348 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1349 packet_put_int(c->remote_id);
1350 packet_send();
1351 }
Darren Tuckera627d422013-06-02 07:31:17 +10001352 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001353 return 0;
Damien Millerc7ef63d2002-02-05 12:21:42 +11001354}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001355
Ben Lindstrombba81212001-06-25 05:01:22 +00001356static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001357server_init_dispatch_20(void)
Damien Millerefb4afe2000-04-12 18:45:05 +10001358{
1359 debug("server_init_dispatch_20");
1360 dispatch_init(&dispatch_protocol_error);
1361 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1362 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1363 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1364 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
1365 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
1366 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1367 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Damien Millerc7ef63d2002-02-05 12:21:42 +11001368 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req);
Damien Millerefb4afe2000-04-12 18:45:05 +10001369 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001370 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001371 /* client_alive */
Damien Miller5a33ec62008-12-08 09:55:02 +11001372 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive);
1373 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive);
Damien Millerb5820f42003-12-17 16:27:32 +11001374 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive);
1375 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001376 /* rekeying */
1377 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Millerefb4afe2000-04-12 18:45:05 +10001378}
Ben Lindstrombba81212001-06-25 05:01:22 +00001379static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001380server_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001381{
1382 debug("server_init_dispatch_13");
1383 dispatch_init(NULL);
1384 dispatch_set(SSH_CMSG_EOF, &server_input_eof);
1385 dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data);
1386 dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size);
1387 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1388 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1389 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
1390 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1391 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1392 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
1393}
Ben Lindstrombba81212001-06-25 05:01:22 +00001394static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001395server_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001396{
1397 server_init_dispatch_13();
1398 debug("server_init_dispatch_15");
1399 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1400 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose);
1401}
Ben Lindstrombba81212001-06-25 05:01:22 +00001402static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001403server_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001404{
Damien Millerefb4afe2000-04-12 18:45:05 +10001405 if (compat20)
1406 server_init_dispatch_20();
1407 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001408 server_init_dispatch_13();
1409 else
1410 server_init_dispatch_15();
1411}