blob: be9edfafc8a4ef384188789c15a71c85d366146d [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"
38#include "xmalloc.h"
39#include "ssh.h"
40#include "packet.h"
41#include "buffer.h"
42#include "servconf.h"
43#include "pty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100044#include "channels.h"
45
46#include "compat.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100047#include "ssh2.h"
48#include "session.h"
Damien Millerb38eff82000-04-01 11:09:21 +100049#include "dispatch.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100050#include "auth-options.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
52static Buffer stdin_buffer; /* Buffer for stdin data. */
53static Buffer stdout_buffer; /* Buffer for stdout data. */
54static Buffer stderr_buffer; /* Buffer for stderr data. */
55static int fdin; /* Descriptor for stdin (for writing) */
56static int fdout; /* Descriptor for stdout (for reading);
57 May be same number as fdin. */
58static int fderr; /* Descriptor for stderr. May be -1. */
59static long stdin_bytes = 0; /* Number of bytes written to stdin. */
60static long stdout_bytes = 0; /* Number of stdout bytes sent to client. */
61static long stderr_bytes = 0; /* Number of stderr bytes sent to client. */
62static long fdout_bytes = 0; /* Number of stdout bytes read from program. */
63static int stdin_eof = 0; /* EOF message received from client. */
64static int fdout_eof = 0; /* EOF encountered reading from fdout. */
65static int fderr_eof = 0; /* EOF encountered readung from fderr. */
66static int connection_in; /* Connection to client (input). */
67static int connection_out; /* Connection to client (output). */
68static unsigned int buffer_high;/* "Soft" max buffer size. */
69static int max_fd; /* Max file descriptor number for select(). */
70
Damien Miller5428f641999-11-25 11:54:57 +110071/*
72 * This SIGCHLD kludge is used to detect when the child exits. The server
73 * will exit after that, as soon as forwarded connections have terminated.
Damien Millerb284b542000-01-17 20:55:18 +110074 *
75 * After SIGCHLD child_has_selected is set to 1 after the first pass
76 * through the wait_until_can_do_something() select(). This ensures
77 * that the child's output gets a chance to drain before it is yanked.
Damien Miller5428f641999-11-25 11:54:57 +110078 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Miller166fca82000-04-20 07:42:21 +100080static pid_t child_pid; /* Pid of the child. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081static volatile int child_terminated; /* The child has terminated. */
Damien Millerb284b542000-01-17 20:55:18 +110082static volatile int child_has_selected; /* Child has had chance to drain. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083static volatile int child_wait_status; /* Status from wait(). */
84
Damien Millerb38eff82000-04-01 11:09:21 +100085void server_init_dispatch(void);
86
Damien Miller4af51302000-04-16 11:18:38 +100087void
Damien Miller95def091999-11-25 00:26:21 +110088sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089{
Damien Miller95def091999-11-25 00:26:21 +110090 int save_errno = errno;
Damien Miller166fca82000-04-20 07:42:21 +100091 pid_t wait_pid;
92
Damien Miller95def091999-11-25 00:26:21 +110093 debug("Received SIGCHLD.");
94 wait_pid = wait((int *) &child_wait_status);
95 if (wait_pid != -1) {
96 if (wait_pid != child_pid)
97 error("Strange, got SIGCHLD and wait returned pid %d but child is %d",
98 wait_pid, child_pid);
99 if (WIFEXITED(child_wait_status) ||
100 WIFSIGNALED(child_wait_status))
101 child_terminated = 1;
Damien Millerb284b542000-01-17 20:55:18 +1100102 child_has_selected = 0;
Damien Miller95def091999-11-25 00:26:21 +1100103 }
104 signal(SIGCHLD, sigchld_handler);
105 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106}
Damien Miller4af51302000-04-16 11:18:38 +1000107void
Damien Millerefb4afe2000-04-12 18:45:05 +1000108sigchld_handler2(int sig)
109{
110 int save_errno = errno;
111 debug("Received SIGCHLD.");
112 child_terminated = 1;
Damien Millerf384c362000-09-13 10:43:26 +1100113 child_has_selected = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +1000114 errno = save_errno;
115}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
Damien Miller95def091999-11-25 00:26:21 +1100117/*
Damien Miller95def091999-11-25 00:26:21 +1100118 * Make packets from buffered stderr data, and buffer it for sending
119 * to the client.
120 */
Damien Miller4af51302000-04-16 11:18:38 +1000121void
Damien Miller95def091999-11-25 00:26:21 +1100122make_packets_from_stderr_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123{
Damien Miller95def091999-11-25 00:26:21 +1100124 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
Damien Miller95def091999-11-25 00:26:21 +1100126 /* Send buffered stderr data to the client. */
127 while (buffer_len(&stderr_buffer) > 0 &&
Damien Miller037a0dc1999-12-07 15:38:31 +1100128 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100129 len = buffer_len(&stderr_buffer);
130 if (packet_is_interactive()) {
131 if (len > 512)
132 len = 512;
133 } else {
134 /* Keep the packets at reasonable size. */
135 if (len > packet_get_maxsize())
136 len = packet_get_maxsize();
137 }
138 packet_start(SSH_SMSG_STDERR_DATA);
139 packet_put_string(buffer_ptr(&stderr_buffer), len);
140 packet_send();
141 buffer_consume(&stderr_buffer, len);
142 stderr_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144}
145
Damien Miller95def091999-11-25 00:26:21 +1100146/*
147 * Make packets from buffered stdout data, and buffer it for sending to the
148 * client.
149 */
Damien Miller4af51302000-04-16 11:18:38 +1000150void
Damien Miller95def091999-11-25 00:26:21 +1100151make_packets_from_stdout_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152{
Damien Miller95def091999-11-25 00:26:21 +1100153 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Damien Miller95def091999-11-25 00:26:21 +1100155 /* Send buffered stdout data to the client. */
156 while (buffer_len(&stdout_buffer) > 0 &&
Damien Miller037a0dc1999-12-07 15:38:31 +1100157 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100158 len = buffer_len(&stdout_buffer);
159 if (packet_is_interactive()) {
160 if (len > 512)
161 len = 512;
162 } else {
163 /* Keep the packets at reasonable size. */
164 if (len > packet_get_maxsize())
165 len = packet_get_maxsize();
166 }
167 packet_start(SSH_SMSG_STDOUT_DATA);
168 packet_put_string(buffer_ptr(&stdout_buffer), len);
169 packet_send();
170 buffer_consume(&stdout_buffer, len);
171 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173}
174
Damien Miller95def091999-11-25 00:26:21 +1100175/*
176 * Sleep in select() until we can do something. This will initialize the
177 * select masks. Upon return, the masks will indicate which descriptors
178 * have data or can accept data. Optionally, a maximum time can be specified
179 * for the duration of the wait (0 = infinite).
180 */
Damien Miller4af51302000-04-16 11:18:38 +1000181void
Damien Miller95def091999-11-25 00:26:21 +1100182wait_until_can_do_something(fd_set * readset, fd_set * writeset,
183 unsigned int max_time_milliseconds)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184{
Damien Miller95def091999-11-25 00:26:21 +1100185 struct timeval tv, *tvp;
186 int ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187
Damien Miller95def091999-11-25 00:26:21 +1100188 /* When select fails we restart from here. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189retry_select:
190
Damien Miller95def091999-11-25 00:26:21 +1100191 /* Initialize select() masks. */
192 FD_ZERO(readset);
Damien Millerb1715dc2000-05-30 13:44:51 +1000193 FD_ZERO(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194
Damien Millerefb4afe2000-04-12 18:45:05 +1000195 if (compat20) {
Damien Millere247cc42000-05-07 12:03:14 +1000196 /* wrong: bad condition XXX */
Damien Millerefb4afe2000-04-12 18:45:05 +1000197 if (channel_not_very_much_buffered_data())
198 FD_SET(connection_in, readset);
199 } else {
Damien Millerb1715dc2000-05-30 13:44:51 +1000200 /*
201 * Read packets from the client unless we have too much
202 * buffered stdin or channel data.
203 */
204 if (buffer_len(&stdin_buffer) < buffer_high &&
Damien Millerefb4afe2000-04-12 18:45:05 +1000205 channel_not_very_much_buffered_data())
206 FD_SET(connection_in, readset);
Damien Millerb1715dc2000-05-30 13:44:51 +1000207 /*
208 * If there is not too much data already buffered going to
209 * the client, try to get some more data from the program.
210 */
211 if (packet_not_very_much_data_to_write()) {
212 if (!fdout_eof)
213 FD_SET(fdout, readset);
214 if (!fderr_eof)
215 FD_SET(fderr, readset);
216 }
217 /*
218 * If we have buffered data, try to write some of that data
219 * to the program.
220 */
221 if (fdin != -1 && buffer_len(&stdin_buffer) > 0)
222 FD_SET(fdin, writeset);
Damien Millerefb4afe2000-04-12 18:45:05 +1000223 }
Damien Miller95def091999-11-25 00:26:21 +1100224 /* Set masks for channel descriptors. */
225 channel_prepare_select(readset, writeset);
226
Damien Miller5428f641999-11-25 11:54:57 +1100227 /*
228 * If we have buffered packet data going to the client, mark that
229 * descriptor.
230 */
Damien Miller95def091999-11-25 00:26:21 +1100231 if (packet_have_data_to_write())
232 FD_SET(connection_out, writeset);
233
Damien Miller95def091999-11-25 00:26:21 +1100234 /* Update the maximum descriptor number if appropriate. */
235 if (channel_max_fd() > max_fd)
236 max_fd = channel_max_fd();
237
Damien Miller5428f641999-11-25 11:54:57 +1100238 /*
239 * If child has terminated and there is enough buffer space to read
240 * from it, then read as much as is available and exit.
241 */
Damien Miller95def091999-11-25 00:26:21 +1100242 if (child_terminated && packet_not_very_much_data_to_write())
243 if (max_time_milliseconds == 0)
244 max_time_milliseconds = 100;
245
246 if (max_time_milliseconds == 0)
247 tvp = NULL;
248 else {
249 tv.tv_sec = max_time_milliseconds / 1000;
250 tv.tv_usec = 1000 * (max_time_milliseconds % 1000);
251 tvp = &tv;
252 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000253 if (tvp!=NULL)
254 debug("tvp!=NULL kid %d mili %d", child_terminated, max_time_milliseconds);
Damien Miller95def091999-11-25 00:26:21 +1100255
256 /* Wait for something to happen, or the timeout to expire. */
257 ret = select(max_fd + 1, readset, writeset, NULL, tvp);
258
259 if (ret < 0) {
260 if (errno != EINTR)
261 error("select: %.100s", strerror(errno));
262 else
263 goto retry_select;
264 }
Damien Millerb284b542000-01-17 20:55:18 +1100265
266 if (child_terminated)
267 child_has_selected = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268}
269
Damien Miller95def091999-11-25 00:26:21 +1100270/*
271 * Processes input from the client and the program. Input data is stored
272 * in buffers and processed later.
273 */
Damien Miller4af51302000-04-16 11:18:38 +1000274void
Damien Miller95def091999-11-25 00:26:21 +1100275process_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276{
Damien Miller95def091999-11-25 00:26:21 +1100277 int len;
278 char buf[16384];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279
Damien Miller95def091999-11-25 00:26:21 +1100280 /* Read and buffer any input data from the client. */
281 if (FD_ISSET(connection_in, readset)) {
282 len = read(connection_in, buf, sizeof(buf));
283 if (len == 0) {
284 verbose("Connection closed by remote host.");
285 fatal_cleanup();
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000286 } else if (len < 0) {
287 if (errno != EINTR && errno != EAGAIN) {
288 verbose("Read error from remote host: %.100s", strerror(errno));
289 fatal_cleanup();
290 }
291 } else {
292 /* Buffer any received data. */
293 packet_process_incoming(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100294 }
Damien Miller95def091999-11-25 00:26:21 +1100295 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000296 if (compat20)
297 return;
298
Damien Miller95def091999-11-25 00:26:21 +1100299 /* Read and buffer any available stdout data from the program. */
300 if (!fdout_eof && FD_ISSET(fdout, readset)) {
301 len = read(fdout, buf, sizeof(buf));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000302 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
303 /* do nothing */
304 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100305 fdout_eof = 1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000306 } else {
Damien Miller95def091999-11-25 00:26:21 +1100307 buffer_append(&stdout_buffer, buf, len);
308 fdout_bytes += len;
309 }
310 }
311 /* Read and buffer any available stderr data from the program. */
312 if (!fderr_eof && FD_ISSET(fderr, readset)) {
313 len = read(fderr, buf, sizeof(buf));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000314 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
315 /* do nothing */
316 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100317 fderr_eof = 1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000318 } else {
Damien Miller95def091999-11-25 00:26:21 +1100319 buffer_append(&stderr_buffer, buf, len);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000320 }
Damien Miller95def091999-11-25 00:26:21 +1100321 }
322}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323
Damien Miller95def091999-11-25 00:26:21 +1100324/*
325 * Sends data from internal buffers to client program stdin.
326 */
Damien Miller4af51302000-04-16 11:18:38 +1000327void
Damien Miller95def091999-11-25 00:26:21 +1100328process_output(fd_set * writeset)
329{
330 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331
Damien Miller95def091999-11-25 00:26:21 +1100332 /* Write buffered data to program stdin. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000333 if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) {
Damien Miller95def091999-11-25 00:26:21 +1100334 len = write(fdin, buffer_ptr(&stdin_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100335 buffer_len(&stdin_buffer));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000336 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
337 /* do nothing */
338 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100339#ifdef USE_PIPES
340 close(fdin);
341#else
Damien Millerb38eff82000-04-01 11:09:21 +1000342 if (fdin != fdout)
Damien Miller95def091999-11-25 00:26:21 +1100343 close(fdin);
344 else
345 shutdown(fdin, SHUT_WR); /* We will no longer send. */
346#endif
347 fdin = -1;
348 } else {
349 /* Successful write. Consume the data from the buffer. */
350 buffer_consume(&stdin_buffer, len);
351 /* Update the count of bytes written to the program. */
352 stdin_bytes += len;
353 }
354 }
355 /* Send any buffered packet data to the client. */
356 if (FD_ISSET(connection_out, writeset))
357 packet_write_poll();
358}
359
360/*
361 * Wait until all buffered output has been sent to the client.
362 * This is used when the program terminates.
363 */
Damien Miller4af51302000-04-16 11:18:38 +1000364void
Damien Miller95def091999-11-25 00:26:21 +1100365drain_output()
366{
367 /* Send any buffered stdout data to the client. */
368 if (buffer_len(&stdout_buffer) > 0) {
369 packet_start(SSH_SMSG_STDOUT_DATA);
370 packet_put_string(buffer_ptr(&stdout_buffer),
371 buffer_len(&stdout_buffer));
372 packet_send();
373 /* Update the count of sent bytes. */
374 stdout_bytes += buffer_len(&stdout_buffer);
375 }
376 /* Send any buffered stderr data to the client. */
377 if (buffer_len(&stderr_buffer) > 0) {
378 packet_start(SSH_SMSG_STDERR_DATA);
379 packet_put_string(buffer_ptr(&stderr_buffer),
380 buffer_len(&stderr_buffer));
381 packet_send();
382 /* Update the count of sent bytes. */
383 stderr_bytes += buffer_len(&stderr_buffer);
384 }
385 /* Wait until all buffered data has been written to the client. */
386 packet_write_wait();
387}
388
Damien Miller4af51302000-04-16 11:18:38 +1000389void
Damien Millerb38eff82000-04-01 11:09:21 +1000390process_buffered_input_packets()
391{
Damien Miller62cee002000-09-23 17:15:56 +1100392 dispatch_run(DISPATCH_NONBLOCK, NULL, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000393}
394
Damien Miller95def091999-11-25 00:26:21 +1100395/*
396 * Performs the interactive session. This handles data transmission between
397 * the client and the program. Note that the notion of stdin, stdout, and
398 * stderr in this function is sort of reversed: this function writes to
399 * stdin (of the child program), and reads from stdout and stderr (of the
400 * child program).
401 */
Damien Miller4af51302000-04-16 11:18:38 +1000402void
Damien Miller166fca82000-04-20 07:42:21 +1000403server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg)
Damien Miller95def091999-11-25 00:26:21 +1100404{
Damien Millerb1715dc2000-05-30 13:44:51 +1000405 fd_set readset, writeset;
Damien Miller166fca82000-04-20 07:42:21 +1000406 int wait_status; /* Status returned by wait(). */
407 pid_t wait_pid; /* pid returned by wait(). */
Damien Miller95def091999-11-25 00:26:21 +1100408 int waiting_termination = 0; /* Have displayed waiting close message. */
409 unsigned int max_time_milliseconds;
410 unsigned int previous_stdout_buffer_bytes;
411 unsigned int stdout_buffer_bytes;
412 int type;
413
414 debug("Entering interactive session.");
415
416 /* Initialize the SIGCHLD kludge. */
417 child_pid = pid;
418 child_terminated = 0;
Damien Millerb284b542000-01-17 20:55:18 +1100419 child_has_selected = 0;
Damien Miller95def091999-11-25 00:26:21 +1100420 signal(SIGCHLD, sigchld_handler);
421
422 /* Initialize our global variables. */
423 fdin = fdin_arg;
424 fdout = fdout_arg;
425 fderr = fderr_arg;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000426
427 /* nonblocking IO */
428 set_nonblock(fdin);
429 set_nonblock(fdout);
Damien Miller7d6656c2000-05-20 15:22:36 +1000430 /* we don't have stderr for interactive terminal sessions, see below */
431 if (fderr != -1)
432 set_nonblock(fderr);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000433
Damien Miller95def091999-11-25 00:26:21 +1100434 connection_in = packet_get_connection_in();
435 connection_out = packet_get_connection_out();
436
437 previous_stdout_buffer_bytes = 0;
438
439 /* Set approximate I/O buffer size. */
440 if (packet_is_interactive())
441 buffer_high = 4096;
442 else
443 buffer_high = 64 * 1024;
444
445 /* Initialize max_fd to the maximum of the known file descriptors. */
446 max_fd = fdin;
447 if (fdout > max_fd)
448 max_fd = fdout;
449 if (fderr != -1 && fderr > max_fd)
450 max_fd = fderr;
451 if (connection_in > max_fd)
452 max_fd = connection_in;
453 if (connection_out > max_fd)
454 max_fd = connection_out;
455
456 /* Initialize Initialize buffers. */
457 buffer_init(&stdin_buffer);
458 buffer_init(&stdout_buffer);
459 buffer_init(&stderr_buffer);
460
Damien Miller5428f641999-11-25 11:54:57 +1100461 /*
462 * If we have no separate fderr (which is the case when we have a pty
463 * - there we cannot make difference between data sent to stdout and
464 * stderr), indicate that we have seen an EOF from stderr. This way
465 * we don\'t need to check the descriptor everywhere.
466 */
Damien Miller95def091999-11-25 00:26:21 +1100467 if (fderr == -1)
468 fderr_eof = 1;
469
Damien Millerb38eff82000-04-01 11:09:21 +1000470 server_init_dispatch();
471
Damien Miller95def091999-11-25 00:26:21 +1100472 /* Main loop of the server for the interactive session mode. */
473 for (;;) {
Damien Miller95def091999-11-25 00:26:21 +1100474
475 /* Process buffered packets from the client. */
476 process_buffered_input_packets();
477
Damien Miller5428f641999-11-25 11:54:57 +1100478 /*
479 * If we have received eof, and there is no more pending
480 * input data, cause a real eof by closing fdin.
481 */
Damien Miller95def091999-11-25 00:26:21 +1100482 if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) {
483#ifdef USE_PIPES
484 close(fdin);
485#else
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. */
490#endif
491 fdin = -1;
492 }
Damien Miller5428f641999-11-25 11:54:57 +1100493 /* Make packets from buffered stderr data to send to the client. */
Damien Miller95def091999-11-25 00:26:21 +1100494 make_packets_from_stderr_data();
495
Damien Miller5428f641999-11-25 11:54:57 +1100496 /*
497 * Make packets from buffered stdout data to send to the
498 * client. If there is very little to send, this arranges to
499 * not send them now, but to wait a short while to see if we
500 * are getting more data. This is necessary, as some systems
501 * wake up readers from a pty after each separate character.
502 */
Damien Miller95def091999-11-25 00:26:21 +1100503 max_time_milliseconds = 0;
504 stdout_buffer_bytes = buffer_len(&stdout_buffer);
505 if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 &&
506 stdout_buffer_bytes != previous_stdout_buffer_bytes) {
507 /* try again after a while */
508 max_time_milliseconds = 10;
509 } else {
510 /* Send it now. */
511 make_packets_from_stdout_data();
512 }
513 previous_stdout_buffer_bytes = buffer_len(&stdout_buffer);
514
515 /* Send channel data to the client. */
516 if (packet_not_very_much_data_to_write())
517 channel_output_poll();
518
Damien Miller5428f641999-11-25 11:54:57 +1100519 /*
520 * Bail out of the loop if the program has closed its output
521 * descriptors, and we have no more data to send to the
522 * client, and there is no pending buffered data.
523 */
Damien Millerb284b542000-01-17 20:55:18 +1100524 if (((fdout_eof && fderr_eof) ||
525 (child_terminated && child_has_selected)) &&
526 !packet_have_data_to_write() &&
527 (buffer_len(&stdout_buffer) == 0) &&
528 (buffer_len(&stderr_buffer) == 0)) {
Damien Miller95def091999-11-25 00:26:21 +1100529 if (!channel_still_open())
Damien Millerb38eff82000-04-01 11:09:21 +1000530 break;
Damien Miller95def091999-11-25 00:26:21 +1100531 if (!waiting_termination) {
532 const char *s = "Waiting for forwarded connections to terminate...\r\n";
533 char *cp;
534 waiting_termination = 1;
535 buffer_append(&stderr_buffer, s, strlen(s));
536
537 /* Display list of open channels. */
538 cp = channel_open_message();
539 buffer_append(&stderr_buffer, cp, strlen(cp));
540 xfree(cp);
541 }
542 }
543 /* Sleep in select() until we can do something. */
544 wait_until_can_do_something(&readset, &writeset,
545 max_time_milliseconds);
546
547 /* Process any channel events. */
548 channel_after_select(&readset, &writeset);
549
550 /* Process input from the client and from program stdout/stderr. */
551 process_input(&readset);
552
553 /* Process output to the client and to program stdin. */
554 process_output(&writeset);
555 }
556
Damien Miller95def091999-11-25 00:26:21 +1100557 /* Cleanup and termination code. */
558
559 /* Wait until all output has been sent to the client. */
560 drain_output();
561
562 debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.",
563 stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes);
564
565 /* Free and clear the buffers. */
566 buffer_free(&stdin_buffer);
567 buffer_free(&stdout_buffer);
568 buffer_free(&stderr_buffer);
569
570 /* Close the file descriptors. */
571 if (fdout != -1)
572 close(fdout);
573 fdout = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574 fdout_eof = 1;
Damien Miller95def091999-11-25 00:26:21 +1100575 if (fderr != -1)
576 close(fderr);
577 fderr = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578 fderr_eof = 1;
Damien Miller95def091999-11-25 00:26:21 +1100579 if (fdin != -1)
580 close(fdin);
581 fdin = -1;
582
583 /* Stop listening for channels; this removes unix domain sockets. */
584 channel_stop_listening();
585
586 /* Wait for the child to exit. Get its exit status. */
587 wait_pid = wait(&wait_status);
588 if (wait_pid < 0) {
589 /*
590 * It is possible that the wait was handled by SIGCHLD
591 * handler. This may result in either: this call
592 * returning with EINTR, or: this call returning ECHILD.
593 */
594 if (child_terminated)
595 wait_status = child_wait_status;
596 else
597 packet_disconnect("wait: %.100s", strerror(errno));
598 } else {
599 /* Check if it matches the process we forked. */
600 if (wait_pid != pid)
601 error("Strange, wait returned pid %d, expected %d",
Damien Milleraae6c611999-12-06 11:47:28 +1100602 wait_pid, pid);
Damien Miller95def091999-11-25 00:26:21 +1100603 }
604
605 /* We no longer want our SIGCHLD handler to be called. */
606 signal(SIGCHLD, SIG_DFL);
607
608 /* Check if it exited normally. */
609 if (WIFEXITED(wait_status)) {
610 /* Yes, normal exit. Get exit status and send it to the client. */
611 debug("Command exited with status %d.", WEXITSTATUS(wait_status));
612 packet_start(SSH_SMSG_EXITSTATUS);
613 packet_put_int(WEXITSTATUS(wait_status));
614 packet_send();
615 packet_write_wait();
616
Damien Miller5428f641999-11-25 11:54:57 +1100617 /*
618 * Wait for exit confirmation. Note that there might be
619 * other packets coming before it; however, the program has
620 * already died so we just ignore them. The client is
621 * supposed to respond with the confirmation when it receives
622 * the exit status.
623 */
Damien Miller95def091999-11-25 00:26:21 +1100624 do {
625 int plen;
626 type = packet_read(&plen);
627 }
628 while (type != SSH_CMSG_EXIT_CONFIRMATION);
629
630 debug("Received exit confirmation.");
631 return;
632 }
633 /* Check if the program terminated due to a signal. */
634 if (WIFSIGNALED(wait_status))
635 packet_disconnect("Command terminated on signal %d.",
636 WTERMSIG(wait_status));
637
638 /* Some weird exit cause. Just exit. */
639 packet_disconnect("wait returned status %04x.", wait_status);
640 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000641}
Damien Millerb38eff82000-04-01 11:09:21 +1000642
Damien Miller4af51302000-04-16 11:18:38 +1000643void
Damien Millerefb4afe2000-04-12 18:45:05 +1000644server_loop2(void)
645{
646 fd_set readset, writeset;
647 int had_channel = 0;
648 int status;
649 pid_t pid;
650
651 debug("Entering interactive session for SSH2.");
652
653 signal(SIGCHLD, sigchld_handler2);
654 child_terminated = 0;
655 connection_in = packet_get_connection_in();
656 connection_out = packet_get_connection_out();
657 max_fd = connection_in;
658 if (connection_out > max_fd)
659 max_fd = connection_out;
660 server_init_dispatch();
661
662 for (;;) {
663 process_buffered_input_packets();
664 if (!had_channel && channel_still_open())
665 had_channel = 1;
666 if (had_channel && !channel_still_open()) {
667 debug("!channel_still_open.");
668 break;
669 }
670 if (packet_not_very_much_data_to_write())
671 channel_output_poll();
672 wait_until_can_do_something(&readset, &writeset, 0);
673 if (child_terminated) {
674 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
675 session_close_by_pid(pid, status);
676 child_terminated = 0;
Damien Millerfda78d92000-05-20 15:33:44 +1000677 signal(SIGCHLD, sigchld_handler2);
Damien Millerefb4afe2000-04-12 18:45:05 +1000678 }
679 channel_after_select(&readset, &writeset);
Damien Millerf384c362000-09-13 10:43:26 +1100680 if (child_terminated && child_has_selected)
681 break;
Damien Millerefb4afe2000-04-12 18:45:05 +1000682 process_input(&readset);
683 process_output(&writeset);
684 }
685 signal(SIGCHLD, SIG_DFL);
686 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
687 session_close_by_pid(pid, status);
688 channel_stop_listening();
689}
690
Damien Millerb38eff82000-04-01 11:09:21 +1000691void
Damien Miller62cee002000-09-23 17:15:56 +1100692server_input_stdin_data(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000693{
694 char *data;
695 unsigned int data_len;
696
697 /* Stdin data from the client. Append it to the buffer. */
698 /* Ignore any data if the client has closed stdin. */
699 if (fdin == -1)
700 return;
701 data = packet_get_string(&data_len);
702 packet_integrity_check(plen, (4 + data_len), type);
703 buffer_append(&stdin_buffer, data, data_len);
704 memset(data, 0, data_len);
705 xfree(data);
706}
707
708void
Damien Miller62cee002000-09-23 17:15:56 +1100709server_input_eof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000710{
711 /*
712 * Eof from the client. The stdin descriptor to the
713 * program will be closed when all buffered data has
714 * drained.
715 */
716 debug("EOF received for stdin.");
717 packet_integrity_check(plen, 0, type);
718 stdin_eof = 1;
719}
720
721void
Damien Miller62cee002000-09-23 17:15:56 +1100722server_input_window_size(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000723{
724 int row = packet_get_int();
725 int col = packet_get_int();
726 int xpixel = packet_get_int();
727 int ypixel = packet_get_int();
728
729 debug("Window change received.");
730 packet_integrity_check(plen, 4 * 4, type);
731 if (fdin != -1)
732 pty_change_window_size(fdin, row, col, xpixel, ypixel);
733}
734
Damien Millerefb4afe2000-04-12 18:45:05 +1000735int
736input_direct_tcpip(void)
737{
738 int sock;
Damien Miller4af51302000-04-16 11:18:38 +1000739 char *target, *originator;
740 int target_port, originator_port;
Damien Millerefb4afe2000-04-12 18:45:05 +1000741
Damien Miller4af51302000-04-16 11:18:38 +1000742 target = packet_get_string(NULL);
743 target_port = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +1000744 originator = packet_get_string(NULL);
745 originator_port = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +1000746 packet_done();
Damien Millerb1715dc2000-05-30 13:44:51 +1000747
748 debug("open direct-tcpip: from %s port %d to %s port %d",
749 originator, originator_port, target, target_port);
Damien Millerf6d9e222000-06-18 14:50:44 +1000750
Damien Millerefb4afe2000-04-12 18:45:05 +1000751 /* XXX check permission */
Damien Miller37023962000-07-11 17:31:38 +1000752 if (no_port_forwarding_flag) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000753 xfree(target);
754 xfree(originator);
755 return -1;
756 }
Damien Miller4af51302000-04-16 11:18:38 +1000757 sock = channel_connect_to(target, target_port);
758 xfree(target);
Damien Millerefb4afe2000-04-12 18:45:05 +1000759 xfree(originator);
760 if (sock < 0)
761 return -1;
762 return channel_new("direct-tcpip", SSH_CHANNEL_OPEN,
Damien Millere4340be2000-09-16 13:29:08 +1100763 sock, sock, -1, CHAN_TCP_WINDOW_DEFAULT,
764 CHAN_TCP_PACKET_DEFAULT, 0, xstrdup("direct-tcpip"));
Damien Millerefb4afe2000-04-12 18:45:05 +1000765}
766
Damien Miller4af51302000-04-16 11:18:38 +1000767void
Damien Miller62cee002000-09-23 17:15:56 +1100768server_input_channel_open(int type, int plen, void *ctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +1000769{
770 Channel *c = NULL;
771 char *ctype;
772 int id;
773 unsigned int len;
774 int rchan;
775 int rmaxpack;
776 int rwindow;
777
778 ctype = packet_get_string(&len);
779 rchan = packet_get_int();
780 rwindow = packet_get_int();
781 rmaxpack = packet_get_int();
782
Damien Miller62cee002000-09-23 17:15:56 +1100783 debug("server_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerefb4afe2000-04-12 18:45:05 +1000784 ctype, rchan, rwindow, rmaxpack);
785
786 if (strcmp(ctype, "session") == 0) {
787 debug("open session");
Damien Miller4af51302000-04-16 11:18:38 +1000788 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +1000789 /*
790 * A server session has no fd to read or write
791 * until a CHANNEL_REQUEST for a shell is made,
792 * so we set the type to SSH_CHANNEL_LARVAL.
793 * Additionally, a callback for handling all
794 * CHANNEL_REQUEST messages is registered.
795 */
796 id = channel_new(ctype, SSH_CHANNEL_LARVAL,
Damien Millere4340be2000-09-16 13:29:08 +1100797 -1, -1, -1, 0, CHAN_SES_PACKET_DEFAULT,
798 0, xstrdup("server-session"));
Damien Millerefb4afe2000-04-12 18:45:05 +1000799 if (session_open(id) == 1) {
800 channel_register_callback(id, SSH2_MSG_CHANNEL_REQUEST,
801 session_input_channel_req, (void *)0);
802 channel_register_cleanup(id, session_close_by_channel);
803 c = channel_lookup(id);
804 } else {
805 debug("session open failed, free channel %d", id);
806 channel_free(id);
807 }
808 } else if (strcmp(ctype, "direct-tcpip") == 0) {
Damien Millerefb4afe2000-04-12 18:45:05 +1000809 id = input_direct_tcpip();
810 if (id >= 0)
811 c = channel_lookup(id);
812 }
813 if (c != NULL) {
814 debug("confirm %s", ctype);
815 c->remote_id = rchan;
816 c->remote_window = rwindow;
817 c->remote_maxpacket = rmaxpack;
818
819 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
820 packet_put_int(c->remote_id);
821 packet_put_int(c->self);
822 packet_put_int(c->local_window);
823 packet_put_int(c->local_maxpacket);
824 packet_send();
825 } else {
826 debug("failure %s", ctype);
827 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
828 packet_put_int(rchan);
829 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
830 packet_put_cstring("bla bla");
831 packet_put_cstring("");
832 packet_send();
833 }
834 xfree(ctype);
835}
836
Damien Miller4af51302000-04-16 11:18:38 +1000837void
Damien Millerefb4afe2000-04-12 18:45:05 +1000838server_init_dispatch_20()
839{
840 debug("server_init_dispatch_20");
841 dispatch_init(&dispatch_protocol_error);
842 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
843 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
844 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
845 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
846 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
847 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
848 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
849 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
850 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
851}
Damien Miller4af51302000-04-16 11:18:38 +1000852void
Damien Millerb38eff82000-04-01 11:09:21 +1000853server_init_dispatch_13()
854{
855 debug("server_init_dispatch_13");
856 dispatch_init(NULL);
857 dispatch_set(SSH_CMSG_EOF, &server_input_eof);
858 dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data);
859 dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size);
860 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
861 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
862 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
863 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
864 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
865 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
866}
Damien Miller4af51302000-04-16 11:18:38 +1000867void
Damien Millerb38eff82000-04-01 11:09:21 +1000868server_init_dispatch_15()
869{
870 server_init_dispatch_13();
871 debug("server_init_dispatch_15");
872 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
873 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose);
874}
Damien Miller4af51302000-04-16 11:18:38 +1000875void
Damien Millerb38eff82000-04-01 11:09:21 +1000876server_init_dispatch()
877{
Damien Millerefb4afe2000-04-12 18:45:05 +1000878 if (compat20)
879 server_init_dispatch_20();
880 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +1000881 server_init_dispatch_13();
882 else
883 server_init_dispatch_15();
884}