blob: dfef6f333348814e52037191d4bae788e709142a [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Server main loop for handling the interactive session.
Damien Millere4340be2000-09-16 13:29:08 +11006 *
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
Damien Millerefb4afe2000-04-12 18:45:05 +100013 * SSH2 support by Markus Friedl.
14 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110038RCSID("$OpenBSD: serverloop.c,v 1.35 2000/11/06 23:04:56 markus Exp $");
Damien Miller69b69aa2000-10-28 14:19:58 +110039
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040#include "xmalloc.h"
41#include "ssh.h"
42#include "packet.h"
43#include "buffer.h"
44#include "servconf.h"
45#include "pty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100046#include "channels.h"
47
48#include "compat.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100049#include "ssh2.h"
50#include "session.h"
Damien Millerb38eff82000-04-01 11:09:21 +100051#include "dispatch.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100052#include "auth-options.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110053#include "auth.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Damien Miller50a41ed2000-10-16 12:14:42 +110055extern ServerOptions options;
56
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057static Buffer stdin_buffer; /* Buffer for stdin data. */
58static Buffer stdout_buffer; /* Buffer for stdout data. */
59static Buffer stderr_buffer; /* Buffer for stderr data. */
60static int fdin; /* Descriptor for stdin (for writing) */
61static int fdout; /* Descriptor for stdout (for reading);
62 May be same number as fdin. */
63static int fderr; /* Descriptor for stderr. May be -1. */
64static long stdin_bytes = 0; /* Number of bytes written to stdin. */
65static long stdout_bytes = 0; /* Number of stdout bytes sent to client. */
66static long stderr_bytes = 0; /* Number of stderr bytes sent to client. */
67static long fdout_bytes = 0; /* Number of stdout bytes read from program. */
68static int stdin_eof = 0; /* EOF message received from client. */
69static int fdout_eof = 0; /* EOF encountered reading from fdout. */
70static int fderr_eof = 0; /* EOF encountered readung from fderr. */
71static int connection_in; /* Connection to client (input). */
72static int connection_out; /* Connection to client (output). */
73static unsigned int buffer_high;/* "Soft" max buffer size. */
74static int max_fd; /* Max file descriptor number for select(). */
75
Damien Miller5428f641999-11-25 11:54:57 +110076/*
77 * This SIGCHLD kludge is used to detect when the child exits. The server
78 * will exit after that, as soon as forwarded connections have terminated.
Damien Millerb284b542000-01-17 20:55:18 +110079 *
80 * After SIGCHLD child_has_selected is set to 1 after the first pass
81 * through the wait_until_can_do_something() select(). This ensures
82 * that the child's output gets a chance to drain before it is yanked.
Damien Miller5428f641999-11-25 11:54:57 +110083 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Damien Miller166fca82000-04-20 07:42:21 +100085static pid_t child_pid; /* Pid of the child. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086static volatile int child_terminated; /* The child has terminated. */
Damien Millerb284b542000-01-17 20:55:18 +110087static volatile int child_has_selected; /* Child has had chance to drain. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088static volatile int child_wait_status; /* Status from wait(). */
89
Damien Millerb38eff82000-04-01 11:09:21 +100090void server_init_dispatch(void);
91
Damien Miller4af51302000-04-16 11:18:38 +100092void
Damien Miller95def091999-11-25 00:26:21 +110093sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094{
Damien Miller95def091999-11-25 00:26:21 +110095 int save_errno = errno;
Damien Miller166fca82000-04-20 07:42:21 +100096 pid_t wait_pid;
97
Damien Miller95def091999-11-25 00:26:21 +110098 debug("Received SIGCHLD.");
99 wait_pid = wait((int *) &child_wait_status);
100 if (wait_pid != -1) {
101 if (wait_pid != child_pid)
102 error("Strange, got SIGCHLD and wait returned pid %d but child is %d",
103 wait_pid, child_pid);
104 if (WIFEXITED(child_wait_status) ||
Damien Miller6bd90df2000-10-28 13:30:55 +1100105 WIFSIGNALED(child_wait_status)) {
Damien Miller95def091999-11-25 00:26:21 +1100106 child_terminated = 1;
Damien Millerb284b542000-01-17 20:55:18 +1100107 child_has_selected = 0;
Damien Miller6bd90df2000-10-28 13:30:55 +1100108 }
Damien Miller95def091999-11-25 00:26:21 +1100109 }
110 signal(SIGCHLD, sigchld_handler);
111 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112}
Damien Miller4af51302000-04-16 11:18:38 +1000113void
Damien Millerefb4afe2000-04-12 18:45:05 +1000114sigchld_handler2(int sig)
115{
116 int save_errno = errno;
117 debug("Received SIGCHLD.");
118 child_terminated = 1;
Damien Miller6bd90df2000-10-28 13:30:55 +1100119 child_has_selected = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +1000120 errno = save_errno;
121}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
Damien Miller95def091999-11-25 00:26:21 +1100123/*
Damien Miller95def091999-11-25 00:26:21 +1100124 * Make packets from buffered stderr data, and buffer it for sending
125 * to the client.
126 */
Damien Miller4af51302000-04-16 11:18:38 +1000127void
Damien Miller95def091999-11-25 00:26:21 +1100128make_packets_from_stderr_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129{
Damien Miller95def091999-11-25 00:26:21 +1100130 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
Damien Miller95def091999-11-25 00:26:21 +1100132 /* Send buffered stderr data to the client. */
133 while (buffer_len(&stderr_buffer) > 0 &&
Damien Miller037a0dc1999-12-07 15:38:31 +1100134 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100135 len = buffer_len(&stderr_buffer);
136 if (packet_is_interactive()) {
137 if (len > 512)
138 len = 512;
139 } else {
140 /* Keep the packets at reasonable size. */
141 if (len > packet_get_maxsize())
142 len = packet_get_maxsize();
143 }
144 packet_start(SSH_SMSG_STDERR_DATA);
145 packet_put_string(buffer_ptr(&stderr_buffer), len);
146 packet_send();
147 buffer_consume(&stderr_buffer, len);
148 stderr_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150}
151
Damien Miller95def091999-11-25 00:26:21 +1100152/*
153 * Make packets from buffered stdout data, and buffer it for sending to the
154 * client.
155 */
Damien Miller4af51302000-04-16 11:18:38 +1000156void
Damien Miller95def091999-11-25 00:26:21 +1100157make_packets_from_stdout_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158{
Damien Miller95def091999-11-25 00:26:21 +1100159 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Miller95def091999-11-25 00:26:21 +1100161 /* Send buffered stdout data to the client. */
162 while (buffer_len(&stdout_buffer) > 0 &&
Damien Miller037a0dc1999-12-07 15:38:31 +1100163 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100164 len = buffer_len(&stdout_buffer);
165 if (packet_is_interactive()) {
166 if (len > 512)
167 len = 512;
168 } else {
169 /* Keep the packets at reasonable size. */
170 if (len > packet_get_maxsize())
171 len = packet_get_maxsize();
172 }
173 packet_start(SSH_SMSG_STDOUT_DATA);
174 packet_put_string(buffer_ptr(&stdout_buffer), len);
175 packet_send();
176 buffer_consume(&stdout_buffer, len);
177 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179}
180
Damien Miller95def091999-11-25 00:26:21 +1100181/*
182 * Sleep in select() until we can do something. This will initialize the
183 * select masks. Upon return, the masks will indicate which descriptors
184 * have data or can accept data. Optionally, a maximum time can be specified
185 * for the duration of the wait (0 = infinite).
186 */
Damien Miller4af51302000-04-16 11:18:38 +1000187void
Damien Miller95def091999-11-25 00:26:21 +1100188wait_until_can_do_something(fd_set * readset, fd_set * writeset,
189 unsigned int max_time_milliseconds)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190{
Damien Miller95def091999-11-25 00:26:21 +1100191 struct timeval tv, *tvp;
192 int ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193
Damien Miller95def091999-11-25 00:26:21 +1100194 /* When select fails we restart from here. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195retry_select:
196
Damien Miller95def091999-11-25 00:26:21 +1100197 /* Initialize select() masks. */
198 FD_ZERO(readset);
Damien Millerb1715dc2000-05-30 13:44:51 +1000199 FD_ZERO(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200
Damien Millerefb4afe2000-04-12 18:45:05 +1000201 if (compat20) {
Damien Millere247cc42000-05-07 12:03:14 +1000202 /* wrong: bad condition XXX */
Damien Millerefb4afe2000-04-12 18:45:05 +1000203 if (channel_not_very_much_buffered_data())
204 FD_SET(connection_in, readset);
205 } else {
Damien Millerb1715dc2000-05-30 13:44:51 +1000206 /*
207 * Read packets from the client unless we have too much
208 * buffered stdin or channel data.
209 */
210 if (buffer_len(&stdin_buffer) < buffer_high &&
Damien Millerefb4afe2000-04-12 18:45:05 +1000211 channel_not_very_much_buffered_data())
212 FD_SET(connection_in, readset);
Damien Millerb1715dc2000-05-30 13:44:51 +1000213 /*
214 * If there is not too much data already buffered going to
215 * the client, try to get some more data from the program.
216 */
217 if (packet_not_very_much_data_to_write()) {
218 if (!fdout_eof)
219 FD_SET(fdout, readset);
220 if (!fderr_eof)
221 FD_SET(fderr, readset);
222 }
223 /*
224 * If we have buffered data, try to write some of that data
225 * to the program.
226 */
227 if (fdin != -1 && buffer_len(&stdin_buffer) > 0)
228 FD_SET(fdin, writeset);
Damien Millerefb4afe2000-04-12 18:45:05 +1000229 }
Damien Miller95def091999-11-25 00:26:21 +1100230 /* Set masks for channel descriptors. */
231 channel_prepare_select(readset, writeset);
232
Damien Miller5428f641999-11-25 11:54:57 +1100233 /*
234 * If we have buffered packet data going to the client, mark that
235 * descriptor.
236 */
Damien Miller95def091999-11-25 00:26:21 +1100237 if (packet_have_data_to_write())
238 FD_SET(connection_out, writeset);
239
Damien Miller95def091999-11-25 00:26:21 +1100240 /* Update the maximum descriptor number if appropriate. */
241 if (channel_max_fd() > max_fd)
242 max_fd = channel_max_fd();
243
Damien Miller5428f641999-11-25 11:54:57 +1100244 /*
245 * If child has terminated and there is enough buffer space to read
246 * from it, then read as much as is available and exit.
247 */
Damien Miller95def091999-11-25 00:26:21 +1100248 if (child_terminated && packet_not_very_much_data_to_write())
249 if (max_time_milliseconds == 0)
250 max_time_milliseconds = 100;
251
252 if (max_time_milliseconds == 0)
253 tvp = NULL;
254 else {
255 tv.tv_sec = max_time_milliseconds / 1000;
256 tv.tv_usec = 1000 * (max_time_milliseconds % 1000);
257 tvp = &tv;
258 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000259 if (tvp!=NULL)
260 debug("tvp!=NULL kid %d mili %d", child_terminated, max_time_milliseconds);
Damien Miller95def091999-11-25 00:26:21 +1100261
262 /* Wait for something to happen, or the timeout to expire. */
263 ret = select(max_fd + 1, readset, writeset, NULL, tvp);
264
265 if (ret < 0) {
266 if (errno != EINTR)
267 error("select: %.100s", strerror(errno));
268 else
269 goto retry_select;
270 }
Damien Millerb284b542000-01-17 20:55:18 +1100271
272 if (child_terminated)
273 child_has_selected = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274}
275
Damien Miller95def091999-11-25 00:26:21 +1100276/*
277 * Processes input from the client and the program. Input data is stored
278 * in buffers and processed later.
279 */
Damien Miller4af51302000-04-16 11:18:38 +1000280void
Damien Miller95def091999-11-25 00:26:21 +1100281process_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282{
Damien Miller95def091999-11-25 00:26:21 +1100283 int len;
284 char buf[16384];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285
Damien Miller95def091999-11-25 00:26:21 +1100286 /* Read and buffer any input data from the client. */
287 if (FD_ISSET(connection_in, readset)) {
288 len = read(connection_in, buf, sizeof(buf));
289 if (len == 0) {
290 verbose("Connection closed by remote host.");
291 fatal_cleanup();
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000292 } else if (len < 0) {
293 if (errno != EINTR && errno != EAGAIN) {
294 verbose("Read error from remote host: %.100s", strerror(errno));
295 fatal_cleanup();
296 }
297 } else {
298 /* Buffer any received data. */
299 packet_process_incoming(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100300 }
Damien Miller95def091999-11-25 00:26:21 +1100301 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000302 if (compat20)
303 return;
304
Damien Miller95def091999-11-25 00:26:21 +1100305 /* Read and buffer any available stdout data from the program. */
306 if (!fdout_eof && FD_ISSET(fdout, readset)) {
307 len = read(fdout, buf, sizeof(buf));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000308 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
309 /* do nothing */
310 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100311 fdout_eof = 1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000312 } else {
Damien Miller95def091999-11-25 00:26:21 +1100313 buffer_append(&stdout_buffer, buf, len);
314 fdout_bytes += len;
315 }
316 }
317 /* Read and buffer any available stderr data from the program. */
318 if (!fderr_eof && FD_ISSET(fderr, readset)) {
319 len = read(fderr, buf, sizeof(buf));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000320 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
321 /* do nothing */
322 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100323 fderr_eof = 1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000324 } else {
Damien Miller95def091999-11-25 00:26:21 +1100325 buffer_append(&stderr_buffer, buf, len);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000326 }
Damien Miller95def091999-11-25 00:26:21 +1100327 }
328}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329
Damien Miller95def091999-11-25 00:26:21 +1100330/*
331 * Sends data from internal buffers to client program stdin.
332 */
Damien Miller4af51302000-04-16 11:18:38 +1000333void
Damien Miller95def091999-11-25 00:26:21 +1100334process_output(fd_set * writeset)
335{
336 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337
Damien Miller95def091999-11-25 00:26:21 +1100338 /* Write buffered data to program stdin. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000339 if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) {
Damien Miller95def091999-11-25 00:26:21 +1100340 len = write(fdin, buffer_ptr(&stdin_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100341 buffer_len(&stdin_buffer));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000342 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
343 /* do nothing */
344 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100345#ifdef USE_PIPES
346 close(fdin);
347#else
Damien Millerb38eff82000-04-01 11:09:21 +1000348 if (fdin != fdout)
Damien Miller95def091999-11-25 00:26:21 +1100349 close(fdin);
350 else
351 shutdown(fdin, SHUT_WR); /* We will no longer send. */
352#endif
353 fdin = -1;
354 } else {
355 /* Successful write. Consume the data from the buffer. */
356 buffer_consume(&stdin_buffer, len);
357 /* Update the count of bytes written to the program. */
358 stdin_bytes += len;
359 }
360 }
361 /* Send any buffered packet data to the client. */
362 if (FD_ISSET(connection_out, writeset))
363 packet_write_poll();
364}
365
366/*
367 * Wait until all buffered output has been sent to the client.
368 * This is used when the program terminates.
369 */
Damien Miller4af51302000-04-16 11:18:38 +1000370void
Damien Miller95def091999-11-25 00:26:21 +1100371drain_output()
372{
373 /* Send any buffered stdout data to the client. */
374 if (buffer_len(&stdout_buffer) > 0) {
375 packet_start(SSH_SMSG_STDOUT_DATA);
376 packet_put_string(buffer_ptr(&stdout_buffer),
377 buffer_len(&stdout_buffer));
378 packet_send();
379 /* Update the count of sent bytes. */
380 stdout_bytes += buffer_len(&stdout_buffer);
381 }
382 /* Send any buffered stderr data to the client. */
383 if (buffer_len(&stderr_buffer) > 0) {
384 packet_start(SSH_SMSG_STDERR_DATA);
385 packet_put_string(buffer_ptr(&stderr_buffer),
386 buffer_len(&stderr_buffer));
387 packet_send();
388 /* Update the count of sent bytes. */
389 stderr_bytes += buffer_len(&stderr_buffer);
390 }
391 /* Wait until all buffered data has been written to the client. */
392 packet_write_wait();
393}
394
Damien Miller4af51302000-04-16 11:18:38 +1000395void
Damien Millerb38eff82000-04-01 11:09:21 +1000396process_buffered_input_packets()
397{
Damien Miller62cee002000-09-23 17:15:56 +1100398 dispatch_run(DISPATCH_NONBLOCK, NULL, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000399}
400
Damien Miller95def091999-11-25 00:26:21 +1100401/*
402 * Performs the interactive session. This handles data transmission between
403 * the client and the program. Note that the notion of stdin, stdout, and
404 * stderr in this function is sort of reversed: this function writes to
405 * stdin (of the child program), and reads from stdout and stderr (of the
406 * child program).
407 */
Damien Miller4af51302000-04-16 11:18:38 +1000408void
Damien Miller166fca82000-04-20 07:42:21 +1000409server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg)
Damien Miller95def091999-11-25 00:26:21 +1100410{
Damien Millerb1715dc2000-05-30 13:44:51 +1000411 fd_set readset, writeset;
Damien Miller166fca82000-04-20 07:42:21 +1000412 int wait_status; /* Status returned by wait(). */
413 pid_t wait_pid; /* pid returned by wait(). */
Damien Miller95def091999-11-25 00:26:21 +1100414 int waiting_termination = 0; /* Have displayed waiting close message. */
415 unsigned int max_time_milliseconds;
416 unsigned int previous_stdout_buffer_bytes;
417 unsigned int stdout_buffer_bytes;
418 int type;
419
420 debug("Entering interactive session.");
421
422 /* Initialize the SIGCHLD kludge. */
423 child_pid = pid;
424 child_terminated = 0;
Damien Millerb284b542000-01-17 20:55:18 +1100425 child_has_selected = 0;
Damien Miller95def091999-11-25 00:26:21 +1100426 signal(SIGCHLD, sigchld_handler);
Damien Millercf3888d2000-09-30 14:17:52 +1100427 signal(SIGPIPE, SIG_IGN);
Damien Miller95def091999-11-25 00:26:21 +1100428
429 /* Initialize our global variables. */
430 fdin = fdin_arg;
431 fdout = fdout_arg;
432 fderr = fderr_arg;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000433
434 /* nonblocking IO */
435 set_nonblock(fdin);
436 set_nonblock(fdout);
Damien Miller7d6656c2000-05-20 15:22:36 +1000437 /* we don't have stderr for interactive terminal sessions, see below */
438 if (fderr != -1)
439 set_nonblock(fderr);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000440
Damien Miller95def091999-11-25 00:26:21 +1100441 connection_in = packet_get_connection_in();
442 connection_out = packet_get_connection_out();
443
444 previous_stdout_buffer_bytes = 0;
445
446 /* Set approximate I/O buffer size. */
447 if (packet_is_interactive())
448 buffer_high = 4096;
449 else
450 buffer_high = 64 * 1024;
451
452 /* Initialize max_fd to the maximum of the known file descriptors. */
453 max_fd = fdin;
454 if (fdout > max_fd)
455 max_fd = fdout;
456 if (fderr != -1 && fderr > max_fd)
457 max_fd = fderr;
458 if (connection_in > max_fd)
459 max_fd = connection_in;
460 if (connection_out > max_fd)
461 max_fd = connection_out;
462
463 /* Initialize Initialize buffers. */
464 buffer_init(&stdin_buffer);
465 buffer_init(&stdout_buffer);
466 buffer_init(&stderr_buffer);
467
Damien Miller5428f641999-11-25 11:54:57 +1100468 /*
469 * If we have no separate fderr (which is the case when we have a pty
470 * - there we cannot make difference between data sent to stdout and
471 * stderr), indicate that we have seen an EOF from stderr. This way
472 * we don\'t need to check the descriptor everywhere.
473 */
Damien Miller95def091999-11-25 00:26:21 +1100474 if (fderr == -1)
475 fderr_eof = 1;
476
Damien Millerb38eff82000-04-01 11:09:21 +1000477 server_init_dispatch();
478
Damien Miller95def091999-11-25 00:26:21 +1100479 /* Main loop of the server for the interactive session mode. */
480 for (;;) {
Damien Miller95def091999-11-25 00:26:21 +1100481
482 /* Process buffered packets from the client. */
483 process_buffered_input_packets();
484
Damien Miller5428f641999-11-25 11:54:57 +1100485 /*
486 * If we have received eof, and there is no more pending
487 * input data, cause a real eof by closing fdin.
488 */
Damien Miller95def091999-11-25 00:26:21 +1100489 if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) {
490#ifdef USE_PIPES
491 close(fdin);
492#else
Damien Millerb38eff82000-04-01 11:09:21 +1000493 if (fdin != fdout)
Damien Miller95def091999-11-25 00:26:21 +1100494 close(fdin);
495 else
496 shutdown(fdin, SHUT_WR); /* We will no longer send. */
497#endif
498 fdin = -1;
499 }
Damien Miller5428f641999-11-25 11:54:57 +1100500 /* Make packets from buffered stderr data to send to the client. */
Damien Miller95def091999-11-25 00:26:21 +1100501 make_packets_from_stderr_data();
502
Damien Miller5428f641999-11-25 11:54:57 +1100503 /*
504 * Make packets from buffered stdout data to send to the
505 * client. If there is very little to send, this arranges to
506 * not send them now, but to wait a short while to see if we
507 * are getting more data. This is necessary, as some systems
508 * wake up readers from a pty after each separate character.
509 */
Damien Miller95def091999-11-25 00:26:21 +1100510 max_time_milliseconds = 0;
511 stdout_buffer_bytes = buffer_len(&stdout_buffer);
512 if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 &&
513 stdout_buffer_bytes != previous_stdout_buffer_bytes) {
514 /* try again after a while */
515 max_time_milliseconds = 10;
516 } else {
517 /* Send it now. */
518 make_packets_from_stdout_data();
519 }
520 previous_stdout_buffer_bytes = buffer_len(&stdout_buffer);
521
522 /* Send channel data to the client. */
523 if (packet_not_very_much_data_to_write())
524 channel_output_poll();
525
Damien Miller5428f641999-11-25 11:54:57 +1100526 /*
527 * Bail out of the loop if the program has closed its output
528 * descriptors, and we have no more data to send to the
529 * client, and there is no pending buffered data.
530 */
Damien Millerb284b542000-01-17 20:55:18 +1100531 if (((fdout_eof && fderr_eof) ||
532 (child_terminated && child_has_selected)) &&
533 !packet_have_data_to_write() &&
534 (buffer_len(&stdout_buffer) == 0) &&
535 (buffer_len(&stderr_buffer) == 0)) {
Damien Miller95def091999-11-25 00:26:21 +1100536 if (!channel_still_open())
Damien Millerb38eff82000-04-01 11:09:21 +1000537 break;
Damien Miller95def091999-11-25 00:26:21 +1100538 if (!waiting_termination) {
539 const char *s = "Waiting for forwarded connections to terminate...\r\n";
540 char *cp;
541 waiting_termination = 1;
542 buffer_append(&stderr_buffer, s, strlen(s));
543
544 /* Display list of open channels. */
545 cp = channel_open_message();
546 buffer_append(&stderr_buffer, cp, strlen(cp));
547 xfree(cp);
548 }
549 }
550 /* Sleep in select() until we can do something. */
551 wait_until_can_do_something(&readset, &writeset,
552 max_time_milliseconds);
553
554 /* Process any channel events. */
555 channel_after_select(&readset, &writeset);
556
557 /* Process input from the client and from program stdout/stderr. */
558 process_input(&readset);
559
560 /* Process output to the client and to program stdin. */
561 process_output(&writeset);
562 }
563
Damien Miller95def091999-11-25 00:26:21 +1100564 /* Cleanup and termination code. */
565
566 /* Wait until all output has been sent to the client. */
567 drain_output();
568
569 debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.",
570 stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes);
571
572 /* Free and clear the buffers. */
573 buffer_free(&stdin_buffer);
574 buffer_free(&stdout_buffer);
575 buffer_free(&stderr_buffer);
576
577 /* Close the file descriptors. */
578 if (fdout != -1)
579 close(fdout);
580 fdout = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581 fdout_eof = 1;
Damien Miller95def091999-11-25 00:26:21 +1100582 if (fderr != -1)
583 close(fderr);
584 fderr = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585 fderr_eof = 1;
Damien Miller95def091999-11-25 00:26:21 +1100586 if (fdin != -1)
587 close(fdin);
588 fdin = -1;
589
590 /* Stop listening for channels; this removes unix domain sockets. */
591 channel_stop_listening();
592
593 /* Wait for the child to exit. Get its exit status. */
594 wait_pid = wait(&wait_status);
595 if (wait_pid < 0) {
596 /*
597 * It is possible that the wait was handled by SIGCHLD
598 * handler. This may result in either: this call
599 * returning with EINTR, or: this call returning ECHILD.
600 */
601 if (child_terminated)
602 wait_status = child_wait_status;
603 else
604 packet_disconnect("wait: %.100s", strerror(errno));
605 } else {
606 /* Check if it matches the process we forked. */
607 if (wait_pid != pid)
608 error("Strange, wait returned pid %d, expected %d",
Damien Milleraae6c611999-12-06 11:47:28 +1100609 wait_pid, pid);
Damien Miller95def091999-11-25 00:26:21 +1100610 }
611
612 /* We no longer want our SIGCHLD handler to be called. */
613 signal(SIGCHLD, SIG_DFL);
614
615 /* Check if it exited normally. */
616 if (WIFEXITED(wait_status)) {
617 /* Yes, normal exit. Get exit status and send it to the client. */
618 debug("Command exited with status %d.", WEXITSTATUS(wait_status));
619 packet_start(SSH_SMSG_EXITSTATUS);
620 packet_put_int(WEXITSTATUS(wait_status));
621 packet_send();
622 packet_write_wait();
623
Damien Miller5428f641999-11-25 11:54:57 +1100624 /*
625 * Wait for exit confirmation. Note that there might be
626 * other packets coming before it; however, the program has
627 * already died so we just ignore them. The client is
628 * supposed to respond with the confirmation when it receives
629 * the exit status.
630 */
Damien Miller95def091999-11-25 00:26:21 +1100631 do {
632 int plen;
633 type = packet_read(&plen);
634 }
635 while (type != SSH_CMSG_EXIT_CONFIRMATION);
636
637 debug("Received exit confirmation.");
638 return;
639 }
640 /* Check if the program terminated due to a signal. */
641 if (WIFSIGNALED(wait_status))
642 packet_disconnect("Command terminated on signal %d.",
643 WTERMSIG(wait_status));
644
645 /* Some weird exit cause. Just exit. */
646 packet_disconnect("wait returned status %04x.", wait_status);
647 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000648}
Damien Millerb38eff82000-04-01 11:09:21 +1000649
Damien Miller4af51302000-04-16 11:18:38 +1000650void
Damien Millerefb4afe2000-04-12 18:45:05 +1000651server_loop2(void)
652{
653 fd_set readset, writeset;
654 int had_channel = 0;
655 int status;
656 pid_t pid;
657
658 debug("Entering interactive session for SSH2.");
659
660 signal(SIGCHLD, sigchld_handler2);
Damien Millercf3888d2000-09-30 14:17:52 +1100661 signal(SIGPIPE, SIG_IGN);
Damien Millerefb4afe2000-04-12 18:45:05 +1000662 child_terminated = 0;
663 connection_in = packet_get_connection_in();
664 connection_out = packet_get_connection_out();
665 max_fd = connection_in;
666 if (connection_out > max_fd)
667 max_fd = connection_out;
668 server_init_dispatch();
669
670 for (;;) {
671 process_buffered_input_packets();
672 if (!had_channel && channel_still_open())
673 had_channel = 1;
674 if (had_channel && !channel_still_open()) {
675 debug("!channel_still_open.");
676 break;
677 }
678 if (packet_not_very_much_data_to_write())
679 channel_output_poll();
680 wait_until_can_do_something(&readset, &writeset, 0);
Damien Miller59939352000-10-15 12:21:32 +1100681 if (child_terminated && child_has_selected) {
682 /* XXX: race - assumes only one child has terminated */
Damien Millerefb4afe2000-04-12 18:45:05 +1000683 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
684 session_close_by_pid(pid, status);
685 child_terminated = 0;
Damien Miller6bd90df2000-10-28 13:30:55 +1100686 child_has_selected = 0;
Damien Millerfda78d92000-05-20 15:33:44 +1000687 signal(SIGCHLD, sigchld_handler2);
Damien Millerefb4afe2000-04-12 18:45:05 +1000688 }
689 channel_after_select(&readset, &writeset);
690 process_input(&readset);
691 process_output(&writeset);
692 }
693 signal(SIGCHLD, SIG_DFL);
694 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
695 session_close_by_pid(pid, status);
696 channel_stop_listening();
697}
698
Damien Millerb38eff82000-04-01 11:09:21 +1000699void
Damien Miller62cee002000-09-23 17:15:56 +1100700server_input_stdin_data(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000701{
702 char *data;
703 unsigned int data_len;
704
705 /* Stdin data from the client. Append it to the buffer. */
706 /* Ignore any data if the client has closed stdin. */
707 if (fdin == -1)
708 return;
709 data = packet_get_string(&data_len);
710 packet_integrity_check(plen, (4 + data_len), type);
711 buffer_append(&stdin_buffer, data, data_len);
712 memset(data, 0, data_len);
713 xfree(data);
714}
715
716void
Damien Miller62cee002000-09-23 17:15:56 +1100717server_input_eof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000718{
719 /*
720 * Eof from the client. The stdin descriptor to the
721 * program will be closed when all buffered data has
722 * drained.
723 */
724 debug("EOF received for stdin.");
725 packet_integrity_check(plen, 0, type);
726 stdin_eof = 1;
727}
728
729void
Damien Miller62cee002000-09-23 17:15:56 +1100730server_input_window_size(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000731{
732 int row = packet_get_int();
733 int col = packet_get_int();
734 int xpixel = packet_get_int();
735 int ypixel = packet_get_int();
736
737 debug("Window change received.");
738 packet_integrity_check(plen, 4 * 4, type);
739 if (fdin != -1)
740 pty_change_window_size(fdin, row, col, xpixel, ypixel);
741}
742
Damien Miller0bc1bd82000-11-13 22:57:25 +1100743Channel *
744server_request_direct_tcpip(char *ctype)
Damien Millerefb4afe2000-04-12 18:45:05 +1000745{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100746 int sock, newch;
Damien Miller4af51302000-04-16 11:18:38 +1000747 char *target, *originator;
748 int target_port, originator_port;
Damien Millerefb4afe2000-04-12 18:45:05 +1000749
Damien Miller4af51302000-04-16 11:18:38 +1000750 target = packet_get_string(NULL);
751 target_port = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +1000752 originator = packet_get_string(NULL);
753 originator_port = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +1000754 packet_done();
Damien Millerb1715dc2000-05-30 13:44:51 +1000755
Damien Miller0bc1bd82000-11-13 22:57:25 +1100756 debug("server_request_direct_tcpip: originator %s port %d, target %s port %d",
Damien Millerb1715dc2000-05-30 13:44:51 +1000757 originator, originator_port, target, target_port);
Damien Millerf6d9e222000-06-18 14:50:44 +1000758
Damien Millerefb4afe2000-04-12 18:45:05 +1000759 /* XXX check permission */
Damien Miller50a41ed2000-10-16 12:14:42 +1100760 if (no_port_forwarding_flag || !options.allow_tcp_forwarding) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000761 xfree(target);
762 xfree(originator);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100763 return NULL;
Damien Millerf6d9e222000-06-18 14:50:44 +1000764 }
Damien Miller4af51302000-04-16 11:18:38 +1000765 sock = channel_connect_to(target, target_port);
766 xfree(target);
Damien Millerefb4afe2000-04-12 18:45:05 +1000767 xfree(originator);
768 if (sock < 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100769 return NULL;
770 newch = channel_new(ctype, SSH_CHANNEL_OPEN,
Damien Millere4340be2000-09-16 13:29:08 +1100771 sock, sock, -1, CHAN_TCP_WINDOW_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +1100772 CHAN_TCP_PACKET_DEFAULT, 0, xstrdup("direct-tcpip"), 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100773 return (newch >= 0) ? channel_lookup(newch) : NULL;
774}
775
776Channel *
777server_request_session(char *ctype)
778{
779 int newch;
780
781 debug("input_session_request");
782 packet_done();
783 /*
784 * A server session has no fd to read or write until a
785 * CHANNEL_REQUEST for a shell is made, so we set the type to
786 * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all
787 * CHANNEL_REQUEST messages is registered.
788 */
789 newch = channel_new(ctype, SSH_CHANNEL_LARVAL,
790 -1, -1, -1, 0, CHAN_SES_PACKET_DEFAULT,
791 0, xstrdup("server-session"), 1);
792 if (session_open(newch) == 1) {
793 channel_register_callback(newch, SSH2_MSG_CHANNEL_REQUEST,
794 session_input_channel_req, (void *)0);
795 channel_register_cleanup(newch, session_close_by_channel);
796 return channel_lookup(newch);
797 } else {
798 debug("session open failed, free channel %d", newch);
799 channel_free(newch);
800 }
801 return NULL;
Damien Millerefb4afe2000-04-12 18:45:05 +1000802}
803
Damien Miller4af51302000-04-16 11:18:38 +1000804void
Damien Miller62cee002000-09-23 17:15:56 +1100805server_input_channel_open(int type, int plen, void *ctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +1000806{
807 Channel *c = NULL;
808 char *ctype;
Damien Millerefb4afe2000-04-12 18:45:05 +1000809 unsigned int len;
810 int rchan;
811 int rmaxpack;
812 int rwindow;
813
814 ctype = packet_get_string(&len);
815 rchan = packet_get_int();
816 rwindow = packet_get_int();
817 rmaxpack = packet_get_int();
818
Damien Miller62cee002000-09-23 17:15:56 +1100819 debug("server_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerefb4afe2000-04-12 18:45:05 +1000820 ctype, rchan, rwindow, rmaxpack);
821
822 if (strcmp(ctype, "session") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100823 c = server_request_session(ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000824 } else if (strcmp(ctype, "direct-tcpip") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100825 c = server_request_direct_tcpip(ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000826 }
827 if (c != NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100828 debug("server_input_channel_open: confirm %s", ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000829 c->remote_id = rchan;
830 c->remote_window = rwindow;
831 c->remote_maxpacket = rmaxpack;
832
833 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
834 packet_put_int(c->remote_id);
835 packet_put_int(c->self);
836 packet_put_int(c->local_window);
837 packet_put_int(c->local_maxpacket);
838 packet_send();
839 } else {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100840 debug("server_input_channel_open: failure %s", ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000841 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
842 packet_put_int(rchan);
843 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
844 packet_put_cstring("bla bla");
845 packet_put_cstring("");
846 packet_send();
847 }
848 xfree(ctype);
849}
850
Damien Miller0bc1bd82000-11-13 22:57:25 +1100851void
852server_input_global_request(int type, int plen, void *ctxt)
853{
854 char *rtype;
855 int want_reply;
856 int success = 0;
857
858 rtype = packet_get_string(NULL);
859 want_reply = packet_get_char();
860 debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply);
861
862 if (strcmp(rtype, "tcpip-forward") == 0) {
863 struct passwd *pw;
864 char *listen_address;
865 u_short listen_port;
866
867 pw = auth_get_user();
868 if (pw == NULL)
869 fatal("server_input_global_request: no user");
870 listen_address = packet_get_string(NULL); /* XXX currently ignored */
871 listen_port = (u_short)packet_get_int();
872 debug("server_input_global_request: tcpip-forward listen %s port %d",
873 listen_address, listen_port);
874
875 /* check permissions */
876 if (!options.allow_tcp_forwarding ||
877 no_port_forwarding_flag ||
878 (listen_port < IPPORT_RESERVED && pw->pw_uid != 0)) {
879 success = 0;
880 packet_send_debug("Server has disabled port forwarding.");
881 } else {
882 /* Start listening on the port */
883 channel_request_forwarding(
884 listen_address, listen_port,
885 /*unspec host_to_connect*/ "<unspec host>",
886 /*unspec port_to_connect*/ 0,
887 options.gateway_ports, /*remote*/ 1);
888 success = 1;
889 }
890 xfree(listen_address);
891 }
892 if (want_reply) {
893 packet_start(success ?
894 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
895 packet_send();
896 packet_write_wait();
897 }
898 xfree(rtype);
899}
900
Damien Miller4af51302000-04-16 11:18:38 +1000901void
Damien Millerefb4afe2000-04-12 18:45:05 +1000902server_init_dispatch_20()
903{
904 debug("server_init_dispatch_20");
905 dispatch_init(&dispatch_protocol_error);
906 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
907 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
908 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
909 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
910 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
911 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
912 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
913 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
914 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100915 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request);
Damien Millerefb4afe2000-04-12 18:45:05 +1000916}
Damien Miller4af51302000-04-16 11:18:38 +1000917void
Damien Millerb38eff82000-04-01 11:09:21 +1000918server_init_dispatch_13()
919{
920 debug("server_init_dispatch_13");
921 dispatch_init(NULL);
922 dispatch_set(SSH_CMSG_EOF, &server_input_eof);
923 dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data);
924 dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size);
925 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
926 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
927 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
928 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
929 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
930 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
931}
Damien Miller4af51302000-04-16 11:18:38 +1000932void
Damien Millerb38eff82000-04-01 11:09:21 +1000933server_init_dispatch_15()
934{
935 server_init_dispatch_13();
936 debug("server_init_dispatch_15");
937 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
938 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose);
939}
Damien Miller4af51302000-04-16 11:18:38 +1000940void
Damien Millerb38eff82000-04-01 11:09:21 +1000941server_init_dispatch()
942{
Damien Millerefb4afe2000-04-12 18:45:05 +1000943 if (compat20)
944 server_init_dispatch_20();
945 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +1000946 server_init_dispatch_13();
947 else
948 server_init_dispatch_15();
949}