blob: 79bdf77bace07696329e64f77082fac6619c125f [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
5 * Created: Sun Sep 10 00:30:37 1995 ylo
6 * Server main loop for handling the interactive session.
7 */
Damien Millerefb4afe2000-04-12 18:45:05 +10008/*
9 * SSH2 support by Markus Friedl.
10 * Copyright (c) 2000 Markus Friedl. All rights reserved.
11 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100012
13#include "includes.h"
14#include "xmalloc.h"
15#include "ssh.h"
16#include "packet.h"
17#include "buffer.h"
18#include "servconf.h"
19#include "pty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100020#include "channels.h"
21
22#include "compat.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100023#include "ssh2.h"
24#include "session.h"
Damien Millerb38eff82000-04-01 11:09:21 +100025#include "dispatch.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026
27static Buffer stdin_buffer; /* Buffer for stdin data. */
28static Buffer stdout_buffer; /* Buffer for stdout data. */
29static Buffer stderr_buffer; /* Buffer for stderr data. */
30static int fdin; /* Descriptor for stdin (for writing) */
31static int fdout; /* Descriptor for stdout (for reading);
32 May be same number as fdin. */
33static int fderr; /* Descriptor for stderr. May be -1. */
34static long stdin_bytes = 0; /* Number of bytes written to stdin. */
35static long stdout_bytes = 0; /* Number of stdout bytes sent to client. */
36static long stderr_bytes = 0; /* Number of stderr bytes sent to client. */
37static long fdout_bytes = 0; /* Number of stdout bytes read from program. */
38static int stdin_eof = 0; /* EOF message received from client. */
39static int fdout_eof = 0; /* EOF encountered reading from fdout. */
40static int fderr_eof = 0; /* EOF encountered readung from fderr. */
41static int connection_in; /* Connection to client (input). */
42static int connection_out; /* Connection to client (output). */
43static unsigned int buffer_high;/* "Soft" max buffer size. */
44static int max_fd; /* Max file descriptor number for select(). */
45
Damien Miller5428f641999-11-25 11:54:57 +110046/*
47 * This SIGCHLD kludge is used to detect when the child exits. The server
48 * will exit after that, as soon as forwarded connections have terminated.
Damien Millerb284b542000-01-17 20:55:18 +110049 *
50 * After SIGCHLD child_has_selected is set to 1 after the first pass
51 * through the wait_until_can_do_something() select(). This ensures
52 * that the child's output gets a chance to drain before it is yanked.
Damien Miller5428f641999-11-25 11:54:57 +110053 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Damien Miller166fca82000-04-20 07:42:21 +100055static pid_t child_pid; /* Pid of the child. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056static volatile int child_terminated; /* The child has terminated. */
Damien Millerb284b542000-01-17 20:55:18 +110057static volatile int child_has_selected; /* Child has had chance to drain. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058static volatile int child_wait_status; /* Status from wait(). */
59
Damien Millerb38eff82000-04-01 11:09:21 +100060void server_init_dispatch(void);
61
Damien Miller4af51302000-04-16 11:18:38 +100062void
Damien Miller95def091999-11-25 00:26:21 +110063sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064{
Damien Miller95def091999-11-25 00:26:21 +110065 int save_errno = errno;
Damien Miller166fca82000-04-20 07:42:21 +100066 pid_t wait_pid;
67
Damien Miller95def091999-11-25 00:26:21 +110068 debug("Received SIGCHLD.");
69 wait_pid = wait((int *) &child_wait_status);
70 if (wait_pid != -1) {
71 if (wait_pid != child_pid)
72 error("Strange, got SIGCHLD and wait returned pid %d but child is %d",
73 wait_pid, child_pid);
74 if (WIFEXITED(child_wait_status) ||
75 WIFSIGNALED(child_wait_status))
76 child_terminated = 1;
Damien Millerb284b542000-01-17 20:55:18 +110077 child_has_selected = 0;
Damien Miller95def091999-11-25 00:26:21 +110078 }
79 signal(SIGCHLD, sigchld_handler);
80 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081}
Damien Miller4af51302000-04-16 11:18:38 +100082void
Damien Millerefb4afe2000-04-12 18:45:05 +100083sigchld_handler2(int sig)
84{
85 int save_errno = errno;
86 debug("Received SIGCHLD.");
87 child_terminated = 1;
88 signal(SIGCHLD, sigchld_handler2);
89 errno = save_errno;
90}
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091
Damien Miller95def091999-11-25 00:26:21 +110092/*
Damien Miller95def091999-11-25 00:26:21 +110093 * Make packets from buffered stderr data, and buffer it for sending
94 * to the client.
95 */
Damien Miller4af51302000-04-16 11:18:38 +100096void
Damien Miller95def091999-11-25 00:26:21 +110097make_packets_from_stderr_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098{
Damien Miller95def091999-11-25 00:26:21 +110099 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
Damien Miller95def091999-11-25 00:26:21 +1100101 /* Send buffered stderr data to the client. */
102 while (buffer_len(&stderr_buffer) > 0 &&
Damien Miller037a0dc1999-12-07 15:38:31 +1100103 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100104 len = buffer_len(&stderr_buffer);
105 if (packet_is_interactive()) {
106 if (len > 512)
107 len = 512;
108 } else {
109 /* Keep the packets at reasonable size. */
110 if (len > packet_get_maxsize())
111 len = packet_get_maxsize();
112 }
113 packet_start(SSH_SMSG_STDERR_DATA);
114 packet_put_string(buffer_ptr(&stderr_buffer), len);
115 packet_send();
116 buffer_consume(&stderr_buffer, len);
117 stderr_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119}
120
Damien Miller95def091999-11-25 00:26:21 +1100121/*
122 * Make packets from buffered stdout data, and buffer it for sending to the
123 * client.
124 */
Damien Miller4af51302000-04-16 11:18:38 +1000125void
Damien Miller95def091999-11-25 00:26:21 +1100126make_packets_from_stdout_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127{
Damien Miller95def091999-11-25 00:26:21 +1100128 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129
Damien Miller95def091999-11-25 00:26:21 +1100130 /* Send buffered stdout data to the client. */
131 while (buffer_len(&stdout_buffer) > 0 &&
Damien Miller037a0dc1999-12-07 15:38:31 +1100132 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100133 len = buffer_len(&stdout_buffer);
134 if (packet_is_interactive()) {
135 if (len > 512)
136 len = 512;
137 } else {
138 /* Keep the packets at reasonable size. */
139 if (len > packet_get_maxsize())
140 len = packet_get_maxsize();
141 }
142 packet_start(SSH_SMSG_STDOUT_DATA);
143 packet_put_string(buffer_ptr(&stdout_buffer), len);
144 packet_send();
145 buffer_consume(&stdout_buffer, len);
146 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148}
149
Damien Miller95def091999-11-25 00:26:21 +1100150/*
151 * Sleep in select() until we can do something. This will initialize the
152 * select masks. Upon return, the masks will indicate which descriptors
153 * have data or can accept data. Optionally, a maximum time can be specified
154 * for the duration of the wait (0 = infinite).
155 */
Damien Miller4af51302000-04-16 11:18:38 +1000156void
Damien Miller95def091999-11-25 00:26:21 +1100157wait_until_can_do_something(fd_set * readset, fd_set * writeset,
158 unsigned int max_time_milliseconds)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159{
Damien Miller95def091999-11-25 00:26:21 +1100160 struct timeval tv, *tvp;
161 int ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162
Damien Miller95def091999-11-25 00:26:21 +1100163 /* When select fails we restart from here. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164retry_select:
165
Damien Miller95def091999-11-25 00:26:21 +1100166 /* Initialize select() masks. */
167 FD_ZERO(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168
Damien Miller5428f641999-11-25 11:54:57 +1100169 /*
170 * Read packets from the client unless we have too much buffered
171 * stdin or channel data.
172 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000173 if (compat20) {
Damien Millere247cc42000-05-07 12:03:14 +1000174 /* wrong: bad condition XXX */
Damien Millerefb4afe2000-04-12 18:45:05 +1000175 if (channel_not_very_much_buffered_data())
176 FD_SET(connection_in, readset);
177 } else {
178 if (buffer_len(&stdin_buffer) < 4096 &&
179 channel_not_very_much_buffered_data())
180 FD_SET(connection_in, readset);
181 }
Damien Miller95def091999-11-25 00:26:21 +1100182
Damien Miller5428f641999-11-25 11:54:57 +1100183 /*
184 * If there is not too much data already buffered going to the
185 * client, try to get some more data from the program.
186 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000187 if (!compat20 && packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100188 if (!fdout_eof)
189 FD_SET(fdout, readset);
190 if (!fderr_eof)
191 FD_SET(fderr, readset);
192 }
193 FD_ZERO(writeset);
194
195 /* Set masks for channel descriptors. */
196 channel_prepare_select(readset, writeset);
197
Damien Miller5428f641999-11-25 11:54:57 +1100198 /*
199 * If we have buffered packet data going to the client, mark that
200 * descriptor.
201 */
Damien Miller95def091999-11-25 00:26:21 +1100202 if (packet_have_data_to_write())
203 FD_SET(connection_out, writeset);
204
205 /* If we have buffered data, try to write some of that data to the
206 program. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000207 if (!compat20 && fdin != -1 && buffer_len(&stdin_buffer) > 0)
Damien Miller95def091999-11-25 00:26:21 +1100208 FD_SET(fdin, writeset);
209
210 /* Update the maximum descriptor number if appropriate. */
211 if (channel_max_fd() > max_fd)
212 max_fd = channel_max_fd();
213
Damien Miller5428f641999-11-25 11:54:57 +1100214 /*
215 * If child has terminated and there is enough buffer space to read
216 * from it, then read as much as is available and exit.
217 */
Damien Miller95def091999-11-25 00:26:21 +1100218 if (child_terminated && packet_not_very_much_data_to_write())
219 if (max_time_milliseconds == 0)
220 max_time_milliseconds = 100;
221
222 if (max_time_milliseconds == 0)
223 tvp = NULL;
224 else {
225 tv.tv_sec = max_time_milliseconds / 1000;
226 tv.tv_usec = 1000 * (max_time_milliseconds % 1000);
227 tvp = &tv;
228 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000229 if (tvp!=NULL)
230 debug("tvp!=NULL kid %d mili %d", child_terminated, max_time_milliseconds);
Damien Miller95def091999-11-25 00:26:21 +1100231
232 /* Wait for something to happen, or the timeout to expire. */
233 ret = select(max_fd + 1, readset, writeset, NULL, tvp);
234
235 if (ret < 0) {
236 if (errno != EINTR)
237 error("select: %.100s", strerror(errno));
238 else
239 goto retry_select;
240 }
Damien Millerb284b542000-01-17 20:55:18 +1100241
242 if (child_terminated)
243 child_has_selected = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244}
245
Damien Miller95def091999-11-25 00:26:21 +1100246/*
247 * Processes input from the client and the program. Input data is stored
248 * in buffers and processed later.
249 */
Damien Miller4af51302000-04-16 11:18:38 +1000250void
Damien Miller95def091999-11-25 00:26:21 +1100251process_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252{
Damien Miller95def091999-11-25 00:26:21 +1100253 int len;
254 char buf[16384];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255
Damien Miller95def091999-11-25 00:26:21 +1100256 /* Read and buffer any input data from the client. */
257 if (FD_ISSET(connection_in, readset)) {
258 len = read(connection_in, buf, sizeof(buf));
259 if (len == 0) {
260 verbose("Connection closed by remote host.");
261 fatal_cleanup();
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000262 } else if (len < 0) {
263 if (errno != EINTR && errno != EAGAIN) {
264 verbose("Read error from remote host: %.100s", strerror(errno));
265 fatal_cleanup();
266 }
267 } else {
268 /* Buffer any received data. */
269 packet_process_incoming(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100270 }
Damien Miller95def091999-11-25 00:26:21 +1100271 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000272 if (compat20)
273 return;
274
Damien Miller95def091999-11-25 00:26:21 +1100275 /* Read and buffer any available stdout data from the program. */
276 if (!fdout_eof && FD_ISSET(fdout, readset)) {
277 len = read(fdout, buf, sizeof(buf));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000278 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
279 /* do nothing */
280 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100281 fdout_eof = 1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000282 } else {
Damien Miller95def091999-11-25 00:26:21 +1100283 buffer_append(&stdout_buffer, buf, len);
284 fdout_bytes += len;
285 }
286 }
287 /* Read and buffer any available stderr data from the program. */
288 if (!fderr_eof && FD_ISSET(fderr, readset)) {
289 len = read(fderr, buf, sizeof(buf));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000290 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
291 /* do nothing */
292 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100293 fderr_eof = 1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000294 } else {
Damien Miller95def091999-11-25 00:26:21 +1100295 buffer_append(&stderr_buffer, buf, len);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000296 }
Damien Miller95def091999-11-25 00:26:21 +1100297 }
298}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299
Damien Miller95def091999-11-25 00:26:21 +1100300/*
301 * Sends data from internal buffers to client program stdin.
302 */
Damien Miller4af51302000-04-16 11:18:38 +1000303void
Damien Miller95def091999-11-25 00:26:21 +1100304process_output(fd_set * writeset)
305{
306 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307
Damien Miller95def091999-11-25 00:26:21 +1100308 /* Write buffered data to program stdin. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000309 if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) {
Damien Miller95def091999-11-25 00:26:21 +1100310 len = write(fdin, buffer_ptr(&stdin_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100311 buffer_len(&stdin_buffer));
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#ifdef USE_PIPES
316 close(fdin);
317#else
Damien Millerb38eff82000-04-01 11:09:21 +1000318 if (fdin != fdout)
Damien Miller95def091999-11-25 00:26:21 +1100319 close(fdin);
320 else
321 shutdown(fdin, SHUT_WR); /* We will no longer send. */
322#endif
323 fdin = -1;
324 } else {
325 /* Successful write. Consume the data from the buffer. */
326 buffer_consume(&stdin_buffer, len);
327 /* Update the count of bytes written to the program. */
328 stdin_bytes += len;
329 }
330 }
331 /* Send any buffered packet data to the client. */
332 if (FD_ISSET(connection_out, writeset))
333 packet_write_poll();
334}
335
336/*
337 * Wait until all buffered output has been sent to the client.
338 * This is used when the program terminates.
339 */
Damien Miller4af51302000-04-16 11:18:38 +1000340void
Damien Miller95def091999-11-25 00:26:21 +1100341drain_output()
342{
343 /* Send any buffered stdout data to the client. */
344 if (buffer_len(&stdout_buffer) > 0) {
345 packet_start(SSH_SMSG_STDOUT_DATA);
346 packet_put_string(buffer_ptr(&stdout_buffer),
347 buffer_len(&stdout_buffer));
348 packet_send();
349 /* Update the count of sent bytes. */
350 stdout_bytes += buffer_len(&stdout_buffer);
351 }
352 /* Send any buffered stderr data to the client. */
353 if (buffer_len(&stderr_buffer) > 0) {
354 packet_start(SSH_SMSG_STDERR_DATA);
355 packet_put_string(buffer_ptr(&stderr_buffer),
356 buffer_len(&stderr_buffer));
357 packet_send();
358 /* Update the count of sent bytes. */
359 stderr_bytes += buffer_len(&stderr_buffer);
360 }
361 /* Wait until all buffered data has been written to the client. */
362 packet_write_wait();
363}
364
Damien Miller4af51302000-04-16 11:18:38 +1000365void
Damien Millerb38eff82000-04-01 11:09:21 +1000366process_buffered_input_packets()
367{
368 dispatch_run(DISPATCH_NONBLOCK, NULL);
369}
370
Damien Miller95def091999-11-25 00:26:21 +1100371/*
372 * Performs the interactive session. This handles data transmission between
373 * the client and the program. Note that the notion of stdin, stdout, and
374 * stderr in this function is sort of reversed: this function writes to
375 * stdin (of the child program), and reads from stdout and stderr (of the
376 * child program).
377 */
Damien Miller4af51302000-04-16 11:18:38 +1000378void
Damien Miller166fca82000-04-20 07:42:21 +1000379server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg)
Damien Miller95def091999-11-25 00:26:21 +1100380{
Damien Miller166fca82000-04-20 07:42:21 +1000381 int wait_status; /* Status returned by wait(). */
382 pid_t wait_pid; /* pid returned by wait(). */
Damien Miller95def091999-11-25 00:26:21 +1100383 int waiting_termination = 0; /* Have displayed waiting close message. */
384 unsigned int max_time_milliseconds;
385 unsigned int previous_stdout_buffer_bytes;
386 unsigned int stdout_buffer_bytes;
387 int type;
388
389 debug("Entering interactive session.");
390
391 /* Initialize the SIGCHLD kludge. */
392 child_pid = pid;
393 child_terminated = 0;
Damien Millerb284b542000-01-17 20:55:18 +1100394 child_has_selected = 0;
Damien Miller95def091999-11-25 00:26:21 +1100395 signal(SIGCHLD, sigchld_handler);
396
397 /* Initialize our global variables. */
398 fdin = fdin_arg;
399 fdout = fdout_arg;
400 fderr = fderr_arg;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000401
402 /* nonblocking IO */
403 set_nonblock(fdin);
404 set_nonblock(fdout);
405 set_nonblock(fderr);
406
Damien Miller95def091999-11-25 00:26:21 +1100407 connection_in = packet_get_connection_in();
408 connection_out = packet_get_connection_out();
409
410 previous_stdout_buffer_bytes = 0;
411
412 /* Set approximate I/O buffer size. */
413 if (packet_is_interactive())
414 buffer_high = 4096;
415 else
416 buffer_high = 64 * 1024;
417
418 /* Initialize max_fd to the maximum of the known file descriptors. */
419 max_fd = fdin;
420 if (fdout > max_fd)
421 max_fd = fdout;
422 if (fderr != -1 && fderr > max_fd)
423 max_fd = fderr;
424 if (connection_in > max_fd)
425 max_fd = connection_in;
426 if (connection_out > max_fd)
427 max_fd = connection_out;
428
429 /* Initialize Initialize buffers. */
430 buffer_init(&stdin_buffer);
431 buffer_init(&stdout_buffer);
432 buffer_init(&stderr_buffer);
433
Damien Miller5428f641999-11-25 11:54:57 +1100434 /*
435 * If we have no separate fderr (which is the case when we have a pty
436 * - there we cannot make difference between data sent to stdout and
437 * stderr), indicate that we have seen an EOF from stderr. This way
438 * we don\'t need to check the descriptor everywhere.
439 */
Damien Miller95def091999-11-25 00:26:21 +1100440 if (fderr == -1)
441 fderr_eof = 1;
442
Damien Millerb38eff82000-04-01 11:09:21 +1000443 server_init_dispatch();
444
Damien Miller95def091999-11-25 00:26:21 +1100445 /* Main loop of the server for the interactive session mode. */
446 for (;;) {
447 fd_set readset, writeset;
448
449 /* Process buffered packets from the client. */
450 process_buffered_input_packets();
451
Damien Miller5428f641999-11-25 11:54:57 +1100452 /*
453 * If we have received eof, and there is no more pending
454 * input data, cause a real eof by closing fdin.
455 */
Damien Miller95def091999-11-25 00:26:21 +1100456 if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) {
457#ifdef USE_PIPES
458 close(fdin);
459#else
Damien Millerb38eff82000-04-01 11:09:21 +1000460 if (fdin != fdout)
Damien Miller95def091999-11-25 00:26:21 +1100461 close(fdin);
462 else
463 shutdown(fdin, SHUT_WR); /* We will no longer send. */
464#endif
465 fdin = -1;
466 }
Damien Miller5428f641999-11-25 11:54:57 +1100467 /* Make packets from buffered stderr data to send to the client. */
Damien Miller95def091999-11-25 00:26:21 +1100468 make_packets_from_stderr_data();
469
Damien Miller5428f641999-11-25 11:54:57 +1100470 /*
471 * Make packets from buffered stdout data to send to the
472 * client. If there is very little to send, this arranges to
473 * not send them now, but to wait a short while to see if we
474 * are getting more data. This is necessary, as some systems
475 * wake up readers from a pty after each separate character.
476 */
Damien Miller95def091999-11-25 00:26:21 +1100477 max_time_milliseconds = 0;
478 stdout_buffer_bytes = buffer_len(&stdout_buffer);
479 if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 &&
480 stdout_buffer_bytes != previous_stdout_buffer_bytes) {
481 /* try again after a while */
482 max_time_milliseconds = 10;
483 } else {
484 /* Send it now. */
485 make_packets_from_stdout_data();
486 }
487 previous_stdout_buffer_bytes = buffer_len(&stdout_buffer);
488
489 /* Send channel data to the client. */
490 if (packet_not_very_much_data_to_write())
491 channel_output_poll();
492
Damien Miller5428f641999-11-25 11:54:57 +1100493 /*
494 * Bail out of the loop if the program has closed its output
495 * descriptors, and we have no more data to send to the
496 * client, and there is no pending buffered data.
497 */
Damien Millerb284b542000-01-17 20:55:18 +1100498 if (((fdout_eof && fderr_eof) ||
499 (child_terminated && child_has_selected)) &&
500 !packet_have_data_to_write() &&
501 (buffer_len(&stdout_buffer) == 0) &&
502 (buffer_len(&stderr_buffer) == 0)) {
Damien Miller95def091999-11-25 00:26:21 +1100503 if (!channel_still_open())
Damien Millerb38eff82000-04-01 11:09:21 +1000504 break;
Damien Miller95def091999-11-25 00:26:21 +1100505 if (!waiting_termination) {
506 const char *s = "Waiting for forwarded connections to terminate...\r\n";
507 char *cp;
508 waiting_termination = 1;
509 buffer_append(&stderr_buffer, s, strlen(s));
510
511 /* Display list of open channels. */
512 cp = channel_open_message();
513 buffer_append(&stderr_buffer, cp, strlen(cp));
514 xfree(cp);
515 }
516 }
517 /* Sleep in select() until we can do something. */
518 wait_until_can_do_something(&readset, &writeset,
519 max_time_milliseconds);
520
521 /* Process any channel events. */
522 channel_after_select(&readset, &writeset);
523
524 /* Process input from the client and from program stdout/stderr. */
525 process_input(&readset);
526
527 /* Process output to the client and to program stdin. */
528 process_output(&writeset);
529 }
530
Damien Miller95def091999-11-25 00:26:21 +1100531 /* Cleanup and termination code. */
532
533 /* Wait until all output has been sent to the client. */
534 drain_output();
535
536 debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.",
537 stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes);
538
539 /* Free and clear the buffers. */
540 buffer_free(&stdin_buffer);
541 buffer_free(&stdout_buffer);
542 buffer_free(&stderr_buffer);
543
544 /* Close the file descriptors. */
545 if (fdout != -1)
546 close(fdout);
547 fdout = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000548 fdout_eof = 1;
Damien Miller95def091999-11-25 00:26:21 +1100549 if (fderr != -1)
550 close(fderr);
551 fderr = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552 fderr_eof = 1;
Damien Miller95def091999-11-25 00:26:21 +1100553 if (fdin != -1)
554 close(fdin);
555 fdin = -1;
556
557 /* Stop listening for channels; this removes unix domain sockets. */
558 channel_stop_listening();
559
560 /* Wait for the child to exit. Get its exit status. */
561 wait_pid = wait(&wait_status);
562 if (wait_pid < 0) {
563 /*
564 * It is possible that the wait was handled by SIGCHLD
565 * handler. This may result in either: this call
566 * returning with EINTR, or: this call returning ECHILD.
567 */
568 if (child_terminated)
569 wait_status = child_wait_status;
570 else
571 packet_disconnect("wait: %.100s", strerror(errno));
572 } else {
573 /* Check if it matches the process we forked. */
574 if (wait_pid != pid)
575 error("Strange, wait returned pid %d, expected %d",
Damien Milleraae6c611999-12-06 11:47:28 +1100576 wait_pid, pid);
Damien Miller95def091999-11-25 00:26:21 +1100577 }
578
579 /* We no longer want our SIGCHLD handler to be called. */
580 signal(SIGCHLD, SIG_DFL);
581
582 /* Check if it exited normally. */
583 if (WIFEXITED(wait_status)) {
584 /* Yes, normal exit. Get exit status and send it to the client. */
585 debug("Command exited with status %d.", WEXITSTATUS(wait_status));
586 packet_start(SSH_SMSG_EXITSTATUS);
587 packet_put_int(WEXITSTATUS(wait_status));
588 packet_send();
589 packet_write_wait();
590
Damien Miller5428f641999-11-25 11:54:57 +1100591 /*
592 * Wait for exit confirmation. Note that there might be
593 * other packets coming before it; however, the program has
594 * already died so we just ignore them. The client is
595 * supposed to respond with the confirmation when it receives
596 * the exit status.
597 */
Damien Miller95def091999-11-25 00:26:21 +1100598 do {
599 int plen;
600 type = packet_read(&plen);
601 }
602 while (type != SSH_CMSG_EXIT_CONFIRMATION);
603
604 debug("Received exit confirmation.");
605 return;
606 }
607 /* Check if the program terminated due to a signal. */
608 if (WIFSIGNALED(wait_status))
609 packet_disconnect("Command terminated on signal %d.",
610 WTERMSIG(wait_status));
611
612 /* Some weird exit cause. Just exit. */
613 packet_disconnect("wait returned status %04x.", wait_status);
614 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000615}
Damien Millerb38eff82000-04-01 11:09:21 +1000616
Damien Miller4af51302000-04-16 11:18:38 +1000617void
Damien Millerefb4afe2000-04-12 18:45:05 +1000618server_loop2(void)
619{
620 fd_set readset, writeset;
621 int had_channel = 0;
622 int status;
623 pid_t pid;
624
625 debug("Entering interactive session for SSH2.");
626
627 signal(SIGCHLD, sigchld_handler2);
628 child_terminated = 0;
629 connection_in = packet_get_connection_in();
630 connection_out = packet_get_connection_out();
631 max_fd = connection_in;
632 if (connection_out > max_fd)
633 max_fd = connection_out;
634 server_init_dispatch();
635
636 for (;;) {
637 process_buffered_input_packets();
638 if (!had_channel && channel_still_open())
639 had_channel = 1;
640 if (had_channel && !channel_still_open()) {
641 debug("!channel_still_open.");
642 break;
643 }
644 if (packet_not_very_much_data_to_write())
645 channel_output_poll();
646 wait_until_can_do_something(&readset, &writeset, 0);
647 if (child_terminated) {
648 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
649 session_close_by_pid(pid, status);
650 child_terminated = 0;
651 }
652 channel_after_select(&readset, &writeset);
653 process_input(&readset);
654 process_output(&writeset);
655 }
656 signal(SIGCHLD, SIG_DFL);
657 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
658 session_close_by_pid(pid, status);
659 channel_stop_listening();
660}
661
Damien Millerb38eff82000-04-01 11:09:21 +1000662void
663server_input_stdin_data(int type, int plen)
664{
665 char *data;
666 unsigned int data_len;
667
668 /* Stdin data from the client. Append it to the buffer. */
669 /* Ignore any data if the client has closed stdin. */
670 if (fdin == -1)
671 return;
672 data = packet_get_string(&data_len);
673 packet_integrity_check(plen, (4 + data_len), type);
674 buffer_append(&stdin_buffer, data, data_len);
675 memset(data, 0, data_len);
676 xfree(data);
677}
678
679void
680server_input_eof(int type, int plen)
681{
682 /*
683 * Eof from the client. The stdin descriptor to the
684 * program will be closed when all buffered data has
685 * drained.
686 */
687 debug("EOF received for stdin.");
688 packet_integrity_check(plen, 0, type);
689 stdin_eof = 1;
690}
691
692void
693server_input_window_size(int type, int plen)
694{
695 int row = packet_get_int();
696 int col = packet_get_int();
697 int xpixel = packet_get_int();
698 int ypixel = packet_get_int();
699
700 debug("Window change received.");
701 packet_integrity_check(plen, 4 * 4, type);
702 if (fdin != -1)
703 pty_change_window_size(fdin, row, col, xpixel, ypixel);
704}
705
Damien Millerefb4afe2000-04-12 18:45:05 +1000706int
707input_direct_tcpip(void)
708{
709 int sock;
Damien Miller4af51302000-04-16 11:18:38 +1000710 char *target, *originator;
711 int target_port, originator_port;
Damien Millerefb4afe2000-04-12 18:45:05 +1000712
Damien Miller4af51302000-04-16 11:18:38 +1000713 target = packet_get_string(NULL);
714 target_port = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +1000715 originator = packet_get_string(NULL);
716 originator_port = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +1000717 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +1000718 /* XXX check permission */
Damien Miller4af51302000-04-16 11:18:38 +1000719 sock = channel_connect_to(target, target_port);
720 xfree(target);
Damien Millerefb4afe2000-04-12 18:45:05 +1000721 xfree(originator);
722 if (sock < 0)
723 return -1;
724 return channel_new("direct-tcpip", SSH_CHANNEL_OPEN,
725 sock, sock, -1, 4*1024, 32*1024, 0, xstrdup("direct-tcpip"));
726}
727
Damien Miller4af51302000-04-16 11:18:38 +1000728void
Damien Millerefb4afe2000-04-12 18:45:05 +1000729server_input_channel_open(int type, int plen)
730{
731 Channel *c = NULL;
732 char *ctype;
733 int id;
734 unsigned int len;
735 int rchan;
736 int rmaxpack;
737 int rwindow;
738
739 ctype = packet_get_string(&len);
740 rchan = packet_get_int();
741 rwindow = packet_get_int();
742 rmaxpack = packet_get_int();
743
Damien Millereba71ba2000-04-29 23:57:08 +1000744 debug("channel_input_open: ctype %s rchan %d win %d max %d",
Damien Millerefb4afe2000-04-12 18:45:05 +1000745 ctype, rchan, rwindow, rmaxpack);
746
747 if (strcmp(ctype, "session") == 0) {
748 debug("open session");
Damien Miller4af51302000-04-16 11:18:38 +1000749 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +1000750 /*
751 * A server session has no fd to read or write
752 * until a CHANNEL_REQUEST for a shell is made,
753 * so we set the type to SSH_CHANNEL_LARVAL.
754 * Additionally, a callback for handling all
755 * CHANNEL_REQUEST messages is registered.
756 */
757 id = channel_new(ctype, SSH_CHANNEL_LARVAL,
758 -1, -1, -1, 0, 32*1024, 0, xstrdup("server-session"));
759 if (session_open(id) == 1) {
760 channel_register_callback(id, SSH2_MSG_CHANNEL_REQUEST,
761 session_input_channel_req, (void *)0);
762 channel_register_cleanup(id, session_close_by_channel);
763 c = channel_lookup(id);
764 } else {
765 debug("session open failed, free channel %d", id);
766 channel_free(id);
767 }
768 } else if (strcmp(ctype, "direct-tcpip") == 0) {
769 debug("open direct-tcpip");
770 id = input_direct_tcpip();
771 if (id >= 0)
772 c = channel_lookup(id);
773 }
774 if (c != NULL) {
775 debug("confirm %s", ctype);
776 c->remote_id = rchan;
777 c->remote_window = rwindow;
778 c->remote_maxpacket = rmaxpack;
779
780 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
781 packet_put_int(c->remote_id);
782 packet_put_int(c->self);
783 packet_put_int(c->local_window);
784 packet_put_int(c->local_maxpacket);
785 packet_send();
786 } else {
787 debug("failure %s", ctype);
788 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
789 packet_put_int(rchan);
790 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
791 packet_put_cstring("bla bla");
792 packet_put_cstring("");
793 packet_send();
794 }
795 xfree(ctype);
796}
797
Damien Miller4af51302000-04-16 11:18:38 +1000798void
Damien Millerefb4afe2000-04-12 18:45:05 +1000799server_init_dispatch_20()
800{
801 debug("server_init_dispatch_20");
802 dispatch_init(&dispatch_protocol_error);
803 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
804 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
805 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
806 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
807 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
808 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
809 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
810 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
811 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
812}
Damien Miller4af51302000-04-16 11:18:38 +1000813void
Damien Millerb38eff82000-04-01 11:09:21 +1000814server_init_dispatch_13()
815{
816 debug("server_init_dispatch_13");
817 dispatch_init(NULL);
818 dispatch_set(SSH_CMSG_EOF, &server_input_eof);
819 dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data);
820 dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size);
821 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
822 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
823 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
824 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
825 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
826 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
827}
Damien Miller4af51302000-04-16 11:18:38 +1000828void
Damien Millerb38eff82000-04-01 11:09:21 +1000829server_init_dispatch_15()
830{
831 server_init_dispatch_13();
832 debug("server_init_dispatch_15");
833 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
834 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose);
835}
Damien Miller4af51302000-04-16 11:18:38 +1000836void
Damien Millerb38eff82000-04-01 11:09:21 +1000837server_init_dispatch()
838{
Damien Millerefb4afe2000-04-12 18:45:05 +1000839 if (compat20)
840 server_init_dispatch_20();
841 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +1000842 server_init_dispatch_13();
843 else
844 server_init_dispatch_15();
845}