blob: a7f8e72b59d235cefc0ef22adb25423bdf7b8a0b [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"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000038RCSID("$OpenBSD: serverloop.c,v 1.42 2001/01/21 19:05:55 markus Exp $");
Damien Miller69b69aa2000-10-28 14:19:58 +110039
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041#include "packet.h"
42#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000043#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044#include "servconf.h"
45#include "pty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100046#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100047#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include "ssh1.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100049#include "ssh2.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000050#include "auth.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100051#include "session.h"
Damien Millerb38eff82000-04-01 11:09:21 +100052#include "dispatch.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100053#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include "serverloop.h"
55#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056
Damien Miller50a41ed2000-10-16 12:14:42 +110057extern ServerOptions options;
58
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059static Buffer stdin_buffer; /* Buffer for stdin data. */
60static Buffer stdout_buffer; /* Buffer for stdout data. */
61static Buffer stderr_buffer; /* Buffer for stderr data. */
62static int fdin; /* Descriptor for stdin (for writing) */
63static int fdout; /* Descriptor for stdout (for reading);
64 May be same number as fdin. */
65static int fderr; /* Descriptor for stderr. May be -1. */
66static long stdin_bytes = 0; /* Number of bytes written to stdin. */
67static long stdout_bytes = 0; /* Number of stdout bytes sent to client. */
68static long stderr_bytes = 0; /* Number of stderr bytes sent to client. */
69static long fdout_bytes = 0; /* Number of stdout bytes read from program. */
70static int stdin_eof = 0; /* EOF message received from client. */
71static int fdout_eof = 0; /* EOF encountered reading from fdout. */
72static int fderr_eof = 0; /* EOF encountered readung from fderr. */
73static int connection_in; /* Connection to client (input). */
74static int connection_out; /* Connection to client (output). */
Ben Lindstrom46c16222000-12-22 01:43:59 +000075static u_int buffer_high;/* "Soft" max buffer size. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076static int max_fd; /* Max file descriptor number for select(). */
77
Damien Miller5428f641999-11-25 11:54:57 +110078/*
79 * This SIGCHLD kludge is used to detect when the child exits. The server
80 * will exit after that, as soon as forwarded connections have terminated.
81 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller166fca82000-04-20 07:42:21 +100083static pid_t child_pid; /* Pid of the child. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084static volatile int child_terminated; /* The child has terminated. */
85static volatile int child_wait_status; /* Status from wait(). */
86
Damien Millerb38eff82000-04-01 11:09:21 +100087void server_init_dispatch(void);
88
Damien Miller4af51302000-04-16 11:18:38 +100089void
Damien Miller95def091999-11-25 00:26:21 +110090sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091{
Damien Miller95def091999-11-25 00:26:21 +110092 int save_errno = errno;
Damien Miller166fca82000-04-20 07:42:21 +100093 pid_t wait_pid;
94
Damien Miller95def091999-11-25 00:26:21 +110095 debug("Received SIGCHLD.");
96 wait_pid = wait((int *) &child_wait_status);
97 if (wait_pid != -1) {
98 if (wait_pid != child_pid)
99 error("Strange, got SIGCHLD and wait returned pid %d but child is %d",
100 wait_pid, child_pid);
101 if (WIFEXITED(child_wait_status) ||
Damien Miller43dc8da2000-11-29 15:55:17 +1100102 WIFSIGNALED(child_wait_status))
Damien Miller95def091999-11-25 00:26:21 +1100103 child_terminated = 1;
104 }
105 signal(SIGCHLD, sigchld_handler);
106 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107}
Damien Miller4af51302000-04-16 11:18:38 +1000108void
Damien Millerefb4afe2000-04-12 18:45:05 +1000109sigchld_handler2(int sig)
110{
111 int save_errno = errno;
112 debug("Received SIGCHLD.");
113 child_terminated = 1;
Damien Miller43dc8da2000-11-29 15:55:17 +1100114 signal(SIGCHLD, sigchld_handler2);
Damien Millerefb4afe2000-04-12 18:45:05 +1000115 errno = save_errno;
116}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117
Damien Miller95def091999-11-25 00:26:21 +1100118/*
Damien Miller95def091999-11-25 00:26:21 +1100119 * Make packets from buffered stderr data, and buffer it for sending
120 * to the client.
121 */
Damien Miller4af51302000-04-16 11:18:38 +1000122void
Damien Miller95def091999-11-25 00:26:21 +1100123make_packets_from_stderr_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124{
Damien Miller95def091999-11-25 00:26:21 +1100125 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Damien Miller95def091999-11-25 00:26:21 +1100127 /* Send buffered stderr data to the client. */
128 while (buffer_len(&stderr_buffer) > 0 &&
Damien Miller037a0dc1999-12-07 15:38:31 +1100129 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100130 len = buffer_len(&stderr_buffer);
131 if (packet_is_interactive()) {
132 if (len > 512)
133 len = 512;
134 } else {
135 /* Keep the packets at reasonable size. */
136 if (len > packet_get_maxsize())
137 len = packet_get_maxsize();
138 }
139 packet_start(SSH_SMSG_STDERR_DATA);
140 packet_put_string(buffer_ptr(&stderr_buffer), len);
141 packet_send();
142 buffer_consume(&stderr_buffer, len);
143 stderr_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145}
146
Damien Miller95def091999-11-25 00:26:21 +1100147/*
148 * Make packets from buffered stdout data, and buffer it for sending to the
149 * client.
150 */
Damien Miller4af51302000-04-16 11:18:38 +1000151void
Damien Miller95def091999-11-25 00:26:21 +1100152make_packets_from_stdout_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153{
Damien Miller95def091999-11-25 00:26:21 +1100154 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Miller95def091999-11-25 00:26:21 +1100156 /* Send buffered stdout data to the client. */
157 while (buffer_len(&stdout_buffer) > 0 &&
Damien Miller037a0dc1999-12-07 15:38:31 +1100158 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100159 len = buffer_len(&stdout_buffer);
160 if (packet_is_interactive()) {
161 if (len > 512)
162 len = 512;
163 } else {
164 /* Keep the packets at reasonable size. */
165 if (len > packet_get_maxsize())
166 len = packet_get_maxsize();
167 }
168 packet_start(SSH_SMSG_STDOUT_DATA);
169 packet_put_string(buffer_ptr(&stdout_buffer), len);
170 packet_send();
171 buffer_consume(&stdout_buffer, len);
172 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174}
175
Damien Miller95def091999-11-25 00:26:21 +1100176/*
177 * Sleep in select() until we can do something. This will initialize the
178 * select masks. Upon return, the masks will indicate which descriptors
179 * have data or can accept data. Optionally, a maximum time can be specified
180 * for the duration of the wait (0 = infinite).
181 */
Damien Miller4af51302000-04-16 11:18:38 +1000182void
Damien Miller95def091999-11-25 00:26:21 +1100183wait_until_can_do_something(fd_set * readset, fd_set * writeset,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000184 u_int max_time_milliseconds)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185{
Damien Miller95def091999-11-25 00:26:21 +1100186 struct timeval tv, *tvp;
187 int ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188
Damien Miller95def091999-11-25 00:26:21 +1100189 /* When select fails we restart from here. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190retry_select:
191
Damien Miller95def091999-11-25 00:26:21 +1100192 /* Initialize select() masks. */
193 FD_ZERO(readset);
Damien Millerb1715dc2000-05-30 13:44:51 +1000194 FD_ZERO(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195
Damien Millerefb4afe2000-04-12 18:45:05 +1000196 if (compat20) {
Damien Millere247cc42000-05-07 12:03:14 +1000197 /* wrong: bad condition XXX */
Damien Millerefb4afe2000-04-12 18:45:05 +1000198 if (channel_not_very_much_buffered_data())
199 FD_SET(connection_in, readset);
200 } else {
Damien Millerb1715dc2000-05-30 13:44:51 +1000201 /*
202 * Read packets from the client unless we have too much
203 * buffered stdin or channel data.
204 */
205 if (buffer_len(&stdin_buffer) < buffer_high &&
Damien Millerefb4afe2000-04-12 18:45:05 +1000206 channel_not_very_much_buffered_data())
207 FD_SET(connection_in, readset);
Damien Millerb1715dc2000-05-30 13:44:51 +1000208 /*
209 * If there is not too much data already buffered going to
210 * the client, try to get some more data from the program.
211 */
212 if (packet_not_very_much_data_to_write()) {
213 if (!fdout_eof)
214 FD_SET(fdout, readset);
215 if (!fderr_eof)
216 FD_SET(fderr, readset);
217 }
218 /*
219 * If we have buffered data, try to write some of that data
220 * to the program.
221 */
222 if (fdin != -1 && buffer_len(&stdin_buffer) > 0)
223 FD_SET(fdin, writeset);
Damien Millerefb4afe2000-04-12 18:45:05 +1000224 }
Damien Miller95def091999-11-25 00:26:21 +1100225 /* Set masks for channel descriptors. */
226 channel_prepare_select(readset, writeset);
227
Damien Miller5428f641999-11-25 11:54:57 +1100228 /*
229 * If we have buffered packet data going to the client, mark that
230 * descriptor.
231 */
Damien Miller95def091999-11-25 00:26:21 +1100232 if (packet_have_data_to_write())
233 FD_SET(connection_out, writeset);
234
Damien Miller95def091999-11-25 00:26:21 +1100235 /* Update the maximum descriptor number if appropriate. */
236 if (channel_max_fd() > max_fd)
237 max_fd = channel_max_fd();
238
Damien Miller5428f641999-11-25 11:54:57 +1100239 /*
240 * If child has terminated and there is enough buffer space to read
241 * from it, then read as much as is available and exit.
242 */
Damien Miller95def091999-11-25 00:26:21 +1100243 if (child_terminated && packet_not_very_much_data_to_write())
244 if (max_time_milliseconds == 0)
245 max_time_milliseconds = 100;
246
247 if (max_time_milliseconds == 0)
248 tvp = NULL;
249 else {
250 tv.tv_sec = max_time_milliseconds / 1000;
251 tv.tv_usec = 1000 * (max_time_milliseconds % 1000);
252 tvp = &tv;
253 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000254 if (tvp!=NULL)
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000255 debug2("tvp!=NULL kid %d mili %d", child_terminated, max_time_milliseconds);
Damien Miller95def091999-11-25 00:26:21 +1100256
257 /* Wait for something to happen, or the timeout to expire. */
258 ret = select(max_fd + 1, readset, writeset, NULL, tvp);
259
260 if (ret < 0) {
261 if (errno != EINTR)
262 error("select: %.100s", strerror(errno));
263 else
264 goto retry_select;
265 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266}
267
Damien Miller95def091999-11-25 00:26:21 +1100268/*
269 * Processes input from the client and the program. Input data is stored
270 * in buffers and processed later.
271 */
Damien Miller4af51302000-04-16 11:18:38 +1000272void
Damien Miller95def091999-11-25 00:26:21 +1100273process_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274{
Damien Miller95def091999-11-25 00:26:21 +1100275 int len;
276 char buf[16384];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277
Damien Miller95def091999-11-25 00:26:21 +1100278 /* Read and buffer any input data from the client. */
279 if (FD_ISSET(connection_in, readset)) {
280 len = read(connection_in, buf, sizeof(buf));
281 if (len == 0) {
282 verbose("Connection closed by remote host.");
283 fatal_cleanup();
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000284 } else if (len < 0) {
285 if (errno != EINTR && errno != EAGAIN) {
286 verbose("Read error from remote host: %.100s", strerror(errno));
287 fatal_cleanup();
288 }
289 } else {
290 /* Buffer any received data. */
291 packet_process_incoming(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100292 }
Damien Miller95def091999-11-25 00:26:21 +1100293 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000294 if (compat20)
295 return;
296
Damien Miller95def091999-11-25 00:26:21 +1100297 /* Read and buffer any available stdout data from the program. */
298 if (!fdout_eof && FD_ISSET(fdout, readset)) {
299 len = read(fdout, buf, sizeof(buf));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000300 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
301 /* do nothing */
302 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100303 fdout_eof = 1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000304 } else {
Damien Miller95def091999-11-25 00:26:21 +1100305 buffer_append(&stdout_buffer, buf, len);
306 fdout_bytes += len;
307 }
308 }
309 /* Read and buffer any available stderr data from the program. */
310 if (!fderr_eof && FD_ISSET(fderr, readset)) {
311 len = read(fderr, buf, sizeof(buf));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000312 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
313 /* do nothing */
314 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100315 fderr_eof = 1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000316 } else {
Damien Miller95def091999-11-25 00:26:21 +1100317 buffer_append(&stderr_buffer, buf, len);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000318 }
Damien Miller95def091999-11-25 00:26:21 +1100319 }
320}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321
Damien Miller95def091999-11-25 00:26:21 +1100322/*
323 * Sends data from internal buffers to client program stdin.
324 */
Damien Miller4af51302000-04-16 11:18:38 +1000325void
Damien Miller95def091999-11-25 00:26:21 +1100326process_output(fd_set * writeset)
327{
328 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329
Damien Miller95def091999-11-25 00:26:21 +1100330 /* Write buffered data to program stdin. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000331 if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) {
Damien Miller95def091999-11-25 00:26:21 +1100332 len = write(fdin, buffer_ptr(&stdin_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100333 buffer_len(&stdin_buffer));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000334 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
335 /* do nothing */
336 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100337#ifdef USE_PIPES
338 close(fdin);
339#else
Damien Millerb38eff82000-04-01 11:09:21 +1000340 if (fdin != fdout)
Damien Miller95def091999-11-25 00:26:21 +1100341 close(fdin);
342 else
343 shutdown(fdin, SHUT_WR); /* We will no longer send. */
344#endif
345 fdin = -1;
346 } else {
347 /* Successful write. Consume the data from the buffer. */
348 buffer_consume(&stdin_buffer, len);
349 /* Update the count of bytes written to the program. */
350 stdin_bytes += len;
351 }
352 }
353 /* Send any buffered packet data to the client. */
354 if (FD_ISSET(connection_out, writeset))
355 packet_write_poll();
356}
357
358/*
359 * Wait until all buffered output has been sent to the client.
360 * This is used when the program terminates.
361 */
Damien Miller4af51302000-04-16 11:18:38 +1000362void
Damien Miller95def091999-11-25 00:26:21 +1100363drain_output()
364{
365 /* Send any buffered stdout data to the client. */
366 if (buffer_len(&stdout_buffer) > 0) {
367 packet_start(SSH_SMSG_STDOUT_DATA);
368 packet_put_string(buffer_ptr(&stdout_buffer),
369 buffer_len(&stdout_buffer));
370 packet_send();
371 /* Update the count of sent bytes. */
372 stdout_bytes += buffer_len(&stdout_buffer);
373 }
374 /* Send any buffered stderr data to the client. */
375 if (buffer_len(&stderr_buffer) > 0) {
376 packet_start(SSH_SMSG_STDERR_DATA);
377 packet_put_string(buffer_ptr(&stderr_buffer),
378 buffer_len(&stderr_buffer));
379 packet_send();
380 /* Update the count of sent bytes. */
381 stderr_bytes += buffer_len(&stderr_buffer);
382 }
383 /* Wait until all buffered data has been written to the client. */
384 packet_write_wait();
385}
386
Damien Miller4af51302000-04-16 11:18:38 +1000387void
Damien Millerb38eff82000-04-01 11:09:21 +1000388process_buffered_input_packets()
389{
Damien Miller62cee002000-09-23 17:15:56 +1100390 dispatch_run(DISPATCH_NONBLOCK, NULL, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000391}
392
Damien Miller95def091999-11-25 00:26:21 +1100393/*
394 * Performs the interactive session. This handles data transmission between
395 * the client and the program. Note that the notion of stdin, stdout, and
396 * stderr in this function is sort of reversed: this function writes to
397 * stdin (of the child program), and reads from stdout and stderr (of the
398 * child program).
399 */
Damien Miller4af51302000-04-16 11:18:38 +1000400void
Damien Miller166fca82000-04-20 07:42:21 +1000401server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg)
Damien Miller95def091999-11-25 00:26:21 +1100402{
Damien Millerb1715dc2000-05-30 13:44:51 +1000403 fd_set readset, writeset;
Damien Miller166fca82000-04-20 07:42:21 +1000404 int wait_status; /* Status returned by wait(). */
405 pid_t wait_pid; /* pid returned by wait(). */
Damien Miller95def091999-11-25 00:26:21 +1100406 int waiting_termination = 0; /* Have displayed waiting close message. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000407 u_int max_time_milliseconds;
408 u_int previous_stdout_buffer_bytes;
409 u_int stdout_buffer_bytes;
Damien Miller95def091999-11-25 00:26:21 +1100410 int type;
411
412 debug("Entering interactive session.");
413
414 /* Initialize the SIGCHLD kludge. */
415 child_pid = pid;
416 child_terminated = 0;
417 signal(SIGCHLD, sigchld_handler);
Damien Millercf3888d2000-09-30 14:17:52 +1100418 signal(SIGPIPE, SIG_IGN);
Damien Miller95def091999-11-25 00:26:21 +1100419
420 /* Initialize our global variables. */
421 fdin = fdin_arg;
422 fdout = fdout_arg;
423 fderr = fderr_arg;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000424
425 /* nonblocking IO */
426 set_nonblock(fdin);
427 set_nonblock(fdout);
Damien Miller7d6656c2000-05-20 15:22:36 +1000428 /* we don't have stderr for interactive terminal sessions, see below */
429 if (fderr != -1)
430 set_nonblock(fderr);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000431
Damien Miller95def091999-11-25 00:26:21 +1100432 connection_in = packet_get_connection_in();
433 connection_out = packet_get_connection_out();
434
435 previous_stdout_buffer_bytes = 0;
436
437 /* Set approximate I/O buffer size. */
438 if (packet_is_interactive())
439 buffer_high = 4096;
440 else
441 buffer_high = 64 * 1024;
442
443 /* Initialize max_fd to the maximum of the known file descriptors. */
444 max_fd = fdin;
445 if (fdout > max_fd)
446 max_fd = fdout;
447 if (fderr != -1 && fderr > max_fd)
448 max_fd = fderr;
449 if (connection_in > max_fd)
450 max_fd = connection_in;
451 if (connection_out > max_fd)
452 max_fd = connection_out;
453
454 /* Initialize Initialize buffers. */
455 buffer_init(&stdin_buffer);
456 buffer_init(&stdout_buffer);
457 buffer_init(&stderr_buffer);
458
Damien Miller5428f641999-11-25 11:54:57 +1100459 /*
460 * If we have no separate fderr (which is the case when we have a pty
461 * - there we cannot make difference between data sent to stdout and
462 * stderr), indicate that we have seen an EOF from stderr. This way
463 * we don\'t need to check the descriptor everywhere.
464 */
Damien Miller95def091999-11-25 00:26:21 +1100465 if (fderr == -1)
466 fderr_eof = 1;
467
Damien Millerb38eff82000-04-01 11:09:21 +1000468 server_init_dispatch();
469
Damien Miller95def091999-11-25 00:26:21 +1100470 /* Main loop of the server for the interactive session mode. */
471 for (;;) {
Damien Miller95def091999-11-25 00:26:21 +1100472
473 /* Process buffered packets from the client. */
474 process_buffered_input_packets();
475
Damien Miller5428f641999-11-25 11:54:57 +1100476 /*
477 * If we have received eof, and there is no more pending
478 * input data, cause a real eof by closing fdin.
479 */
Damien Miller95def091999-11-25 00:26:21 +1100480 if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) {
481#ifdef USE_PIPES
482 close(fdin);
483#else
Damien Millerb38eff82000-04-01 11:09:21 +1000484 if (fdin != fdout)
Damien Miller95def091999-11-25 00:26:21 +1100485 close(fdin);
486 else
487 shutdown(fdin, SHUT_WR); /* We will no longer send. */
488#endif
489 fdin = -1;
490 }
Damien Miller5428f641999-11-25 11:54:57 +1100491 /* Make packets from buffered stderr data to send to the client. */
Damien Miller95def091999-11-25 00:26:21 +1100492 make_packets_from_stderr_data();
493
Damien Miller5428f641999-11-25 11:54:57 +1100494 /*
495 * Make packets from buffered stdout data to send to the
496 * client. If there is very little to send, this arranges to
497 * not send them now, but to wait a short while to see if we
498 * are getting more data. This is necessary, as some systems
499 * wake up readers from a pty after each separate character.
500 */
Damien Miller95def091999-11-25 00:26:21 +1100501 max_time_milliseconds = 0;
502 stdout_buffer_bytes = buffer_len(&stdout_buffer);
503 if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 &&
504 stdout_buffer_bytes != previous_stdout_buffer_bytes) {
505 /* try again after a while */
506 max_time_milliseconds = 10;
507 } else {
508 /* Send it now. */
509 make_packets_from_stdout_data();
510 }
511 previous_stdout_buffer_bytes = buffer_len(&stdout_buffer);
512
513 /* Send channel data to the client. */
514 if (packet_not_very_much_data_to_write())
515 channel_output_poll();
516
Damien Miller5428f641999-11-25 11:54:57 +1100517 /*
518 * Bail out of the loop if the program has closed its output
519 * descriptors, and we have no more data to send to the
520 * client, and there is no pending buffered data.
521 */
Damien Miller43dc8da2000-11-29 15:55:17 +1100522 if (fdout_eof && fderr_eof && !packet_have_data_to_write() &&
523 buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100524 if (!channel_still_open())
Damien Millerb38eff82000-04-01 11:09:21 +1000525 break;
Damien Miller95def091999-11-25 00:26:21 +1100526 if (!waiting_termination) {
527 const char *s = "Waiting for forwarded connections to terminate...\r\n";
528 char *cp;
529 waiting_termination = 1;
530 buffer_append(&stderr_buffer, s, strlen(s));
531
532 /* Display list of open channels. */
533 cp = channel_open_message();
534 buffer_append(&stderr_buffer, cp, strlen(cp));
535 xfree(cp);
536 }
537 }
538 /* Sleep in select() until we can do something. */
539 wait_until_can_do_something(&readset, &writeset,
540 max_time_milliseconds);
541
542 /* Process any channel events. */
543 channel_after_select(&readset, &writeset);
544
545 /* Process input from the client and from program stdout/stderr. */
546 process_input(&readset);
547
548 /* Process output to the client and to program stdin. */
549 process_output(&writeset);
550 }
551
Damien Miller95def091999-11-25 00:26:21 +1100552 /* Cleanup and termination code. */
553
554 /* Wait until all output has been sent to the client. */
555 drain_output();
556
557 debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.",
558 stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes);
559
560 /* Free and clear the buffers. */
561 buffer_free(&stdin_buffer);
562 buffer_free(&stdout_buffer);
563 buffer_free(&stderr_buffer);
564
565 /* Close the file descriptors. */
566 if (fdout != -1)
567 close(fdout);
568 fdout = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569 fdout_eof = 1;
Damien Miller95def091999-11-25 00:26:21 +1100570 if (fderr != -1)
571 close(fderr);
572 fderr = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573 fderr_eof = 1;
Damien Miller95def091999-11-25 00:26:21 +1100574 if (fdin != -1)
575 close(fdin);
576 fdin = -1;
577
578 /* Stop listening for channels; this removes unix domain sockets. */
579 channel_stop_listening();
580
581 /* Wait for the child to exit. Get its exit status. */
582 wait_pid = wait(&wait_status);
Ben Lindstrom46c16222000-12-22 01:43:59 +0000583 if (wait_pid == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100584 /*
585 * It is possible that the wait was handled by SIGCHLD
586 * handler. This may result in either: this call
587 * returning with EINTR, or: this call returning ECHILD.
588 */
589 if (child_terminated)
590 wait_status = child_wait_status;
591 else
592 packet_disconnect("wait: %.100s", strerror(errno));
593 } else {
594 /* Check if it matches the process we forked. */
595 if (wait_pid != pid)
596 error("Strange, wait returned pid %d, expected %d",
Damien Milleraae6c611999-12-06 11:47:28 +1100597 wait_pid, pid);
Damien Miller95def091999-11-25 00:26:21 +1100598 }
599
600 /* We no longer want our SIGCHLD handler to be called. */
601 signal(SIGCHLD, SIG_DFL);
602
603 /* Check if it exited normally. */
604 if (WIFEXITED(wait_status)) {
605 /* Yes, normal exit. Get exit status and send it to the client. */
606 debug("Command exited with status %d.", WEXITSTATUS(wait_status));
607 packet_start(SSH_SMSG_EXITSTATUS);
608 packet_put_int(WEXITSTATUS(wait_status));
609 packet_send();
610 packet_write_wait();
611
Damien Miller5428f641999-11-25 11:54:57 +1100612 /*
613 * Wait for exit confirmation. Note that there might be
614 * other packets coming before it; however, the program has
615 * already died so we just ignore them. The client is
616 * supposed to respond with the confirmation when it receives
617 * the exit status.
618 */
Damien Miller95def091999-11-25 00:26:21 +1100619 do {
620 int plen;
621 type = packet_read(&plen);
622 }
623 while (type != SSH_CMSG_EXIT_CONFIRMATION);
624
625 debug("Received exit confirmation.");
626 return;
627 }
628 /* Check if the program terminated due to a signal. */
629 if (WIFSIGNALED(wait_status))
630 packet_disconnect("Command terminated on signal %d.",
631 WTERMSIG(wait_status));
632
633 /* Some weird exit cause. Just exit. */
634 packet_disconnect("wait returned status %04x.", wait_status);
635 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000636}
Damien Millerb38eff82000-04-01 11:09:21 +1000637
Damien Miller4af51302000-04-16 11:18:38 +1000638void
Damien Millerefb4afe2000-04-12 18:45:05 +1000639server_loop2(void)
640{
641 fd_set readset, writeset;
642 int had_channel = 0;
643 int status;
644 pid_t pid;
645
646 debug("Entering interactive session for SSH2.");
647
648 signal(SIGCHLD, sigchld_handler2);
Damien Millercf3888d2000-09-30 14:17:52 +1100649 signal(SIGPIPE, SIG_IGN);
Damien Millerefb4afe2000-04-12 18:45:05 +1000650 child_terminated = 0;
651 connection_in = packet_get_connection_in();
652 connection_out = packet_get_connection_out();
653 max_fd = connection_in;
654 if (connection_out > max_fd)
655 max_fd = connection_out;
656 server_init_dispatch();
657
658 for (;;) {
659 process_buffered_input_packets();
660 if (!had_channel && channel_still_open())
661 had_channel = 1;
662 if (had_channel && !channel_still_open()) {
663 debug("!channel_still_open.");
664 break;
665 }
666 if (packet_not_very_much_data_to_write())
667 channel_output_poll();
668 wait_until_can_do_something(&readset, &writeset, 0);
Damien Miller43dc8da2000-11-29 15:55:17 +1100669 if (child_terminated) {
Damien Millerefb4afe2000-04-12 18:45:05 +1000670 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
671 session_close_by_pid(pid, status);
672 child_terminated = 0;
673 }
674 channel_after_select(&readset, &writeset);
675 process_input(&readset);
676 process_output(&writeset);
677 }
678 signal(SIGCHLD, SIG_DFL);
679 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
680 session_close_by_pid(pid, status);
681 channel_stop_listening();
682}
683
Damien Millerb38eff82000-04-01 11:09:21 +1000684void
Damien Miller62cee002000-09-23 17:15:56 +1100685server_input_stdin_data(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000686{
687 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000688 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000689
690 /* Stdin data from the client. Append it to the buffer. */
691 /* Ignore any data if the client has closed stdin. */
692 if (fdin == -1)
693 return;
694 data = packet_get_string(&data_len);
695 packet_integrity_check(plen, (4 + data_len), type);
696 buffer_append(&stdin_buffer, data, data_len);
697 memset(data, 0, data_len);
698 xfree(data);
699}
700
701void
Damien Miller62cee002000-09-23 17:15:56 +1100702server_input_eof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000703{
704 /*
705 * Eof from the client. The stdin descriptor to the
706 * program will be closed when all buffered data has
707 * drained.
708 */
709 debug("EOF received for stdin.");
710 packet_integrity_check(plen, 0, type);
711 stdin_eof = 1;
712}
713
714void
Damien Miller62cee002000-09-23 17:15:56 +1100715server_input_window_size(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000716{
717 int row = packet_get_int();
718 int col = packet_get_int();
719 int xpixel = packet_get_int();
720 int ypixel = packet_get_int();
721
722 debug("Window change received.");
723 packet_integrity_check(plen, 4 * 4, type);
724 if (fdin != -1)
725 pty_change_window_size(fdin, row, col, xpixel, ypixel);
726}
727
Damien Miller0bc1bd82000-11-13 22:57:25 +1100728Channel *
729server_request_direct_tcpip(char *ctype)
Damien Millerefb4afe2000-04-12 18:45:05 +1000730{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100731 int sock, newch;
Damien Miller4af51302000-04-16 11:18:38 +1000732 char *target, *originator;
733 int target_port, originator_port;
Damien Millerefb4afe2000-04-12 18:45:05 +1000734
Damien Miller4af51302000-04-16 11:18:38 +1000735 target = packet_get_string(NULL);
736 target_port = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +1000737 originator = packet_get_string(NULL);
738 originator_port = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +1000739 packet_done();
Damien Millerb1715dc2000-05-30 13:44:51 +1000740
Damien Miller0bc1bd82000-11-13 22:57:25 +1100741 debug("server_request_direct_tcpip: originator %s port %d, target %s port %d",
Damien Millerb1715dc2000-05-30 13:44:51 +1000742 originator, originator_port, target, target_port);
Damien Millerf6d9e222000-06-18 14:50:44 +1000743
Damien Millerefb4afe2000-04-12 18:45:05 +1000744 /* XXX check permission */
Damien Miller50a41ed2000-10-16 12:14:42 +1100745 if (no_port_forwarding_flag || !options.allow_tcp_forwarding) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000746 xfree(target);
747 xfree(originator);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100748 return NULL;
Damien Millerf6d9e222000-06-18 14:50:44 +1000749 }
Damien Miller4af51302000-04-16 11:18:38 +1000750 sock = channel_connect_to(target, target_port);
751 xfree(target);
Damien Millerefb4afe2000-04-12 18:45:05 +1000752 xfree(originator);
753 if (sock < 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100754 return NULL;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000755 newch = channel_new(ctype, SSH_CHANNEL_CONNECTING,
Damien Millere4340be2000-09-16 13:29:08 +1100756 sock, sock, -1, CHAN_TCP_WINDOW_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +1100757 CHAN_TCP_PACKET_DEFAULT, 0, xstrdup("direct-tcpip"), 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100758 return (newch >= 0) ? channel_lookup(newch) : NULL;
759}
760
761Channel *
762server_request_session(char *ctype)
763{
764 int newch;
765
766 debug("input_session_request");
767 packet_done();
768 /*
769 * A server session has no fd to read or write until a
770 * CHANNEL_REQUEST for a shell is made, so we set the type to
771 * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all
772 * CHANNEL_REQUEST messages is registered.
773 */
774 newch = channel_new(ctype, SSH_CHANNEL_LARVAL,
775 -1, -1, -1, 0, CHAN_SES_PACKET_DEFAULT,
776 0, xstrdup("server-session"), 1);
777 if (session_open(newch) == 1) {
778 channel_register_callback(newch, SSH2_MSG_CHANNEL_REQUEST,
779 session_input_channel_req, (void *)0);
780 channel_register_cleanup(newch, session_close_by_channel);
781 return channel_lookup(newch);
782 } else {
783 debug("session open failed, free channel %d", newch);
784 channel_free(newch);
785 }
786 return NULL;
Damien Millerefb4afe2000-04-12 18:45:05 +1000787}
788
Damien Miller4af51302000-04-16 11:18:38 +1000789void
Damien Miller62cee002000-09-23 17:15:56 +1100790server_input_channel_open(int type, int plen, void *ctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +1000791{
792 Channel *c = NULL;
793 char *ctype;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000794 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +1000795 int rchan;
796 int rmaxpack;
797 int rwindow;
798
799 ctype = packet_get_string(&len);
800 rchan = packet_get_int();
801 rwindow = packet_get_int();
802 rmaxpack = packet_get_int();
803
Damien Miller62cee002000-09-23 17:15:56 +1100804 debug("server_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerefb4afe2000-04-12 18:45:05 +1000805 ctype, rchan, rwindow, rmaxpack);
806
807 if (strcmp(ctype, "session") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100808 c = server_request_session(ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000809 } else if (strcmp(ctype, "direct-tcpip") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100810 c = server_request_direct_tcpip(ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000811 }
812 if (c != NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100813 debug("server_input_channel_open: confirm %s", ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000814 c->remote_id = rchan;
815 c->remote_window = rwindow;
816 c->remote_maxpacket = rmaxpack;
817
818 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
819 packet_put_int(c->remote_id);
820 packet_put_int(c->self);
821 packet_put_int(c->local_window);
822 packet_put_int(c->local_maxpacket);
823 packet_send();
824 } else {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100825 debug("server_input_channel_open: failure %s", ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000826 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
827 packet_put_int(rchan);
828 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
829 packet_put_cstring("bla bla");
830 packet_put_cstring("");
831 packet_send();
832 }
833 xfree(ctype);
834}
835
Damien Miller0bc1bd82000-11-13 22:57:25 +1100836void
837server_input_global_request(int type, int plen, void *ctxt)
838{
839 char *rtype;
840 int want_reply;
841 int success = 0;
842
843 rtype = packet_get_string(NULL);
844 want_reply = packet_get_char();
845 debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply);
846
847 if (strcmp(rtype, "tcpip-forward") == 0) {
848 struct passwd *pw;
849 char *listen_address;
850 u_short listen_port;
851
852 pw = auth_get_user();
853 if (pw == NULL)
854 fatal("server_input_global_request: no user");
855 listen_address = packet_get_string(NULL); /* XXX currently ignored */
856 listen_port = (u_short)packet_get_int();
857 debug("server_input_global_request: tcpip-forward listen %s port %d",
858 listen_address, listen_port);
859
860 /* check permissions */
861 if (!options.allow_tcp_forwarding ||
862 no_port_forwarding_flag ||
863 (listen_port < IPPORT_RESERVED && pw->pw_uid != 0)) {
864 success = 0;
865 packet_send_debug("Server has disabled port forwarding.");
866 } else {
867 /* Start listening on the port */
868 channel_request_forwarding(
869 listen_address, listen_port,
870 /*unspec host_to_connect*/ "<unspec host>",
871 /*unspec port_to_connect*/ 0,
872 options.gateway_ports, /*remote*/ 1);
873 success = 1;
874 }
875 xfree(listen_address);
876 }
877 if (want_reply) {
878 packet_start(success ?
879 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
880 packet_send();
881 packet_write_wait();
882 }
883 xfree(rtype);
884}
885
Damien Miller4af51302000-04-16 11:18:38 +1000886void
Damien Millerefb4afe2000-04-12 18:45:05 +1000887server_init_dispatch_20()
888{
889 debug("server_init_dispatch_20");
890 dispatch_init(&dispatch_protocol_error);
891 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
892 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
893 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
894 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
895 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
896 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
897 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
898 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
899 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100900 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request);
Damien Millerefb4afe2000-04-12 18:45:05 +1000901}
Damien Miller4af51302000-04-16 11:18:38 +1000902void
Damien Millerb38eff82000-04-01 11:09:21 +1000903server_init_dispatch_13()
904{
905 debug("server_init_dispatch_13");
906 dispatch_init(NULL);
907 dispatch_set(SSH_CMSG_EOF, &server_input_eof);
908 dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data);
909 dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size);
910 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
911 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
912 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
913 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
914 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
915 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
916}
Damien Miller4af51302000-04-16 11:18:38 +1000917void
Damien Millerb38eff82000-04-01 11:09:21 +1000918server_init_dispatch_15()
919{
920 server_init_dispatch_13();
921 debug("server_init_dispatch_15");
922 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
923 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose);
924}
Damien Miller4af51302000-04-16 11:18:38 +1000925void
Damien Millerb38eff82000-04-01 11:09:21 +1000926server_init_dispatch()
927{
Damien Millerefb4afe2000-04-12 18:45:05 +1000928 if (compat20)
929 server_init_dispatch_20();
930 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +1000931 server_init_dispatch_13();
932 else
933 server_init_dispatch_15();
934}