]> andersk Git - openssh.git/blame - serverloop.c
- markus@cvs.openbsd.org 2001/04/04 20:32:56
[openssh.git] / serverloop.c
CommitLineData
8efc0c15 1/*
5260325f 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5260325f 5 * Server main loop for handling the interactive session.
bcbf86ec 6 *
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 *
e78a59f5 13 * SSH2 support by Markus Friedl.
f3c7c613 14 * Copyright (c) 2000 Markus Friedl. All rights reserved.
bcbf86ec 15 *
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.
e78a59f5 35 */
8efc0c15 36
37#include "includes.h"
c422989b 38RCSID("$OpenBSD: serverloop.c,v 1.57 2001/04/04 20:25:37 markus Exp $");
a22aff1f 39
8efc0c15 40#include "xmalloc.h"
8efc0c15 41#include "packet.h"
42#include "buffer.h"
42f11eb2 43#include "log.h"
8efc0c15 44#include "servconf.h"
1729c161 45#include "sshpty.h"
7368a6c8 46#include "channels.h"
7368a6c8 47#include "compat.h"
42f11eb2 48#include "ssh1.h"
e78a59f5 49#include "ssh2.h"
59c97189 50#include "auth.h"
e78a59f5 51#include "session.h"
7368a6c8 52#include "dispatch.h"
38c295d6 53#include "auth-options.h"
42f11eb2 54#include "serverloop.h"
55#include "misc.h"
7a37c112 56#include "kex.h"
8efc0c15 57
33de75a3 58extern ServerOptions options;
59
7a37c112 60/* XXX */
61extern Kex *xxx_kex;
62
8efc0c15 63static Buffer stdin_buffer; /* Buffer for stdin data. */
64static Buffer stdout_buffer; /* Buffer for stdout data. */
65static Buffer stderr_buffer; /* Buffer for stderr data. */
66static int fdin; /* Descriptor for stdin (for writing) */
67static int fdout; /* Descriptor for stdout (for reading);
68 May be same number as fdin. */
69static int fderr; /* Descriptor for stderr. May be -1. */
70static long stdin_bytes = 0; /* Number of bytes written to stdin. */
71static long stdout_bytes = 0; /* Number of stdout bytes sent to client. */
72static long stderr_bytes = 0; /* Number of stderr bytes sent to client. */
73static long fdout_bytes = 0; /* Number of stdout bytes read from program. */
74static int stdin_eof = 0; /* EOF message received from client. */
75static int fdout_eof = 0; /* EOF encountered reading from fdout. */
76static int fderr_eof = 0; /* EOF encountered readung from fderr. */
e68225b2 77static int fdin_is_tty = 0; /* fdin points to a tty. */
8efc0c15 78static int connection_in; /* Connection to client (input). */
79static int connection_out; /* Connection to client (output). */
1e3b8b07 80static u_int buffer_high;/* "Soft" max buffer size. */
8efc0c15 81
aa3378df 82/*
83 * This SIGCHLD kludge is used to detect when the child exits. The server
84 * will exit after that, as soon as forwarded connections have terminated.
85 */
8efc0c15 86
9da5c3c9 87static pid_t child_pid; /* Pid of the child. */
8efc0c15 88static volatile int child_terminated; /* The child has terminated. */
89static volatile int child_wait_status; /* Status from wait(). */
90
7368a6c8 91void server_init_dispatch(void);
92
6ae2364d 93void
5260325f 94sigchld_handler(int sig)
8efc0c15 95{
5260325f 96 int save_errno = errno;
9da5c3c9 97 pid_t wait_pid;
98
5260325f 99 debug("Received SIGCHLD.");
100 wait_pid = wait((int *) &child_wait_status);
101 if (wait_pid != -1) {
102 if (wait_pid != child_pid)
103 error("Strange, got SIGCHLD and wait returned pid %d but child is %d",
104 wait_pid, child_pid);
105 if (WIFEXITED(child_wait_status) ||
7062c40f 106 WIFSIGNALED(child_wait_status))
5260325f 107 child_terminated = 1;
108 }
109 signal(SIGCHLD, sigchld_handler);
110 errno = save_errno;
8efc0c15 111}
6ae2364d 112void
e78a59f5 113sigchld_handler2(int sig)
114{
115 int save_errno = errno;
116 debug("Received SIGCHLD.");
117 child_terminated = 1;
1aa00dcb 118 mysignal(SIGCHLD, sigchld_handler2);
e78a59f5 119 errno = save_errno;
120}
8efc0c15 121
5260325f 122/*
123 * Make packets from buffered stderr data, and buffer it for sending
124 * to the client.
125 */
6ae2364d 126void
5ca51e19 127make_packets_from_stderr_data(void)
8efc0c15 128{
5260325f 129 int len;
130
131 /* Send buffered stderr data to the client. */
132 while (buffer_len(&stderr_buffer) > 0 &&
a408af76 133 packet_not_very_much_data_to_write()) {
5260325f 134 len = buffer_len(&stderr_buffer);
135 if (packet_is_interactive()) {
136 if (len > 512)
137 len = 512;
138 } else {
139 /* Keep the packets at reasonable size. */
140 if (len > packet_get_maxsize())
141 len = packet_get_maxsize();
142 }
143 packet_start(SSH_SMSG_STDERR_DATA);
144 packet_put_string(buffer_ptr(&stderr_buffer), len);
145 packet_send();
146 buffer_consume(&stderr_buffer, len);
147 stderr_bytes += len;
8efc0c15 148 }
8efc0c15 149}
150
5260325f 151/*
152 * Make packets from buffered stdout data, and buffer it for sending to the
153 * client.
154 */
6ae2364d 155void
5ca51e19 156make_packets_from_stdout_data(void)
8efc0c15 157{
5260325f 158 int len;
159
160 /* Send buffered stdout data to the client. */
161 while (buffer_len(&stdout_buffer) > 0 &&
a408af76 162 packet_not_very_much_data_to_write()) {
5260325f 163 len = buffer_len(&stdout_buffer);
164 if (packet_is_interactive()) {
165 if (len > 512)
166 len = 512;
167 } else {
168 /* Keep the packets at reasonable size. */
169 if (len > packet_get_maxsize())
2b87da3b 170 len = packet_get_maxsize();
5260325f 171 }
172 packet_start(SSH_SMSG_STDOUT_DATA);
173 packet_put_string(buffer_ptr(&stdout_buffer), len);
174 packet_send();
175 buffer_consume(&stdout_buffer, len);
176 stdout_bytes += len;
8efc0c15 177 }
8efc0c15 178}
179
5260325f 180/*
181 * Sleep in select() until we can do something. This will initialize the
182 * select masks. Upon return, the masks will indicate which descriptors
183 * have data or can accept data. Optionally, a maximum time can be specified
184 * for the duration of the wait (0 = infinite).
185 */
6ae2364d 186void
39929cdb 187wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
188 u_int max_time_milliseconds)
8efc0c15 189{
5260325f 190 struct timeval tv, *tvp;
191 int ret;
8efc0c15 192
5260325f 193 /* When select fails we restart from here. */
8efc0c15 194retry_select:
195
39929cdb 196 /* Allocate and update select() masks for channel descriptors. */
c422989b 197 channel_prepare_select(readsetp, writesetp, maxfdp, 0);
5260325f 198
e78a59f5 199 if (compat20) {
1d1ffb87 200 /* wrong: bad condition XXX */
e78a59f5 201 if (channel_not_very_much_buffered_data())
39929cdb 202 FD_SET(connection_in, *readsetp);
e78a59f5 203 } else {
71276795 204 /*
205 * Read packets from the client unless we have too much
206 * buffered stdin or channel data.
207 */
208 if (buffer_len(&stdin_buffer) < buffer_high &&
e78a59f5 209 channel_not_very_much_buffered_data())
39929cdb 210 FD_SET(connection_in, *readsetp);
71276795 211 /*
212 * If there is not too much data already buffered going to
213 * the client, try to get some more data from the program.
214 */
215 if (packet_not_very_much_data_to_write()) {
216 if (!fdout_eof)
39929cdb 217 FD_SET(fdout, *readsetp);
71276795 218 if (!fderr_eof)
39929cdb 219 FD_SET(fderr, *readsetp);
71276795 220 }
221 /*
222 * If we have buffered data, try to write some of that data
223 * to the program.
224 */
225 if (fdin != -1 && buffer_len(&stdin_buffer) > 0)
39929cdb 226 FD_SET(fdin, *writesetp);
e78a59f5 227 }
5260325f 228
aa3378df 229 /*
230 * If we have buffered packet data going to the client, mark that
231 * descriptor.
232 */
5260325f 233 if (packet_have_data_to_write())
39929cdb 234 FD_SET(connection_out, *writesetp);
5260325f 235
aa3378df 236 /*
237 * If child has terminated and there is enough buffer space to read
238 * from it, then read as much as is available and exit.
239 */
5260325f 240 if (child_terminated && packet_not_very_much_data_to_write())
241 if (max_time_milliseconds == 0)
242 max_time_milliseconds = 100;
243
244 if (max_time_milliseconds == 0)
245 tvp = NULL;
246 else {
247 tv.tv_sec = max_time_milliseconds / 1000;
248 tv.tv_usec = 1000 * (max_time_milliseconds % 1000);
249 tvp = &tv;
250 }
e78a59f5 251 if (tvp!=NULL)
19d57054 252 debug3("tvp!=NULL kid %d mili %d", child_terminated, max_time_milliseconds);
8efc0c15 253
5260325f 254 /* Wait for something to happen, or the timeout to expire. */
39929cdb 255 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
8efc0c15 256
5260325f 257 if (ret < 0) {
258 if (errno != EINTR)
259 error("select: %.100s", strerror(errno));
260 else
261 goto retry_select;
8efc0c15 262 }
8efc0c15 263}
264
5260325f 265/*
266 * Processes input from the client and the program. Input data is stored
267 * in buffers and processed later.
268 */
6ae2364d 269void
5260325f 270process_input(fd_set * readset)
271{
272 int len;
273 char buf[16384];
274
275 /* Read and buffer any input data from the client. */
276 if (FD_ISSET(connection_in, readset)) {
277 len = read(connection_in, buf, sizeof(buf));
278 if (len == 0) {
279 verbose("Connection closed by remote host.");
280 fatal_cleanup();
0e73cc53 281 } else if (len < 0) {
282 if (errno != EINTR && errno != EAGAIN) {
283 verbose("Read error from remote host: %.100s", strerror(errno));
284 fatal_cleanup();
285 }
286 } else {
287 /* Buffer any received data. */
288 packet_process_incoming(buf, len);
5260325f 289 }
5260325f 290 }
e78a59f5 291 if (compat20)
292 return;
293
5260325f 294 /* Read and buffer any available stdout data from the program. */
295 if (!fdout_eof && FD_ISSET(fdout, readset)) {
296 len = read(fdout, buf, sizeof(buf));
0e73cc53 297 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
298 /* do nothing */
299 } else if (len <= 0) {
5260325f 300 fdout_eof = 1;
0e73cc53 301 } else {
5260325f 302 buffer_append(&stdout_buffer, buf, len);
303 fdout_bytes += len;
304 }
305 }
306 /* Read and buffer any available stderr data from the program. */
307 if (!fderr_eof && FD_ISSET(fderr, readset)) {
308 len = read(fderr, buf, sizeof(buf));
0e73cc53 309 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
310 /* do nothing */
311 } else if (len <= 0) {
5260325f 312 fderr_eof = 1;
0e73cc53 313 } else {
5260325f 314 buffer_append(&stderr_buffer, buf, len);
0e73cc53 315 }
5260325f 316 }
317}
8efc0c15 318
5260325f 319/*
320 * Sends data from internal buffers to client program stdin.
321 */
6ae2364d 322void
5260325f 323process_output(fd_set * writeset)
8efc0c15 324{
4ef922e3 325 struct termios tio;
5260325f 326 int len;
327
328 /* Write buffered data to program stdin. */
e78a59f5 329 if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) {
5260325f 330 len = write(fdin, buffer_ptr(&stdin_buffer),
a408af76 331 buffer_len(&stdin_buffer));
0e73cc53 332 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
333 /* do nothing */
334 } else if (len <= 0) {
8efc0c15 335#ifdef USE_PIPES
5260325f 336 close(fdin);
8efc0c15 337#else
7368a6c8 338 if (fdin != fdout)
5260325f 339 close(fdin);
340 else
341 shutdown(fdin, SHUT_WR); /* We will no longer send. */
8efc0c15 342#endif
5260325f 343 fdin = -1;
344 } else {
4ef922e3 345 /* Successful write. */
e68225b2 346 if (fdin_is_tty && tcgetattr(fdin, &tio) == 0 &&
0ae4fe1d 347 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
62da27dd 348 /*
349 * Simulate echo to reduce the impact of
350 * traffic analysis
351 */
95ce5599 352 packet_send_ignore(len);
4ef922e3 353 packet_send();
354 }
355 /* Consume the data from the buffer. */
5260325f 356 buffer_consume(&stdin_buffer, len);
357 /* Update the count of bytes written to the program. */
358 stdin_bytes += len;
359 }
8efc0c15 360 }
5260325f 361 /* Send any buffered packet data to the client. */
362 if (FD_ISSET(connection_out, writeset))
363 packet_write_poll();
8efc0c15 364}
365
5260325f 366/*
367 * Wait until all buffered output has been sent to the client.
368 * This is used when the program terminates.
369 */
6ae2364d 370void
5ca51e19 371drain_output(void)
8efc0c15 372{
5260325f 373 /* Send any buffered stdout data to the client. */
374 if (buffer_len(&stdout_buffer) > 0) {
375 packet_start(SSH_SMSG_STDOUT_DATA);
376 packet_put_string(buffer_ptr(&stdout_buffer),
377 buffer_len(&stdout_buffer));
378 packet_send();
379 /* Update the count of sent bytes. */
380 stdout_bytes += buffer_len(&stdout_buffer);
381 }
382 /* Send any buffered stderr data to the client. */
383 if (buffer_len(&stderr_buffer) > 0) {
384 packet_start(SSH_SMSG_STDERR_DATA);
385 packet_put_string(buffer_ptr(&stderr_buffer),
386 buffer_len(&stderr_buffer));
387 packet_send();
388 /* Update the count of sent bytes. */
389 stderr_bytes += buffer_len(&stderr_buffer);
390 }
391 /* Wait until all buffered data has been written to the client. */
392 packet_write_wait();
8efc0c15 393}
394
6ae2364d 395void
5ca51e19 396process_buffered_input_packets(void)
7368a6c8 397{
7a37c112 398 dispatch_run(DISPATCH_NONBLOCK, NULL, compat20 ? xxx_kex : NULL);
7368a6c8 399}
400
5260325f 401/*
402 * Performs the interactive session. This handles data transmission between
403 * the client and the program. Note that the notion of stdin, stdout, and
404 * stderr in this function is sort of reversed: this function writes to
405 * stdin (of the child program), and reads from stdout and stderr (of the
406 * child program).
407 */
6ae2364d 408void
9da5c3c9 409server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg)
8efc0c15 410{
39929cdb 411 fd_set *readset = NULL, *writeset = NULL;
412 int max_fd;
9da5c3c9 413 int wait_status; /* Status returned by wait(). */
414 pid_t wait_pid; /* pid returned by wait(). */
5260325f 415 int waiting_termination = 0; /* Have displayed waiting close message. */
1e3b8b07 416 u_int max_time_milliseconds;
417 u_int previous_stdout_buffer_bytes;
418 u_int stdout_buffer_bytes;
5260325f 419 int type;
420
421 debug("Entering interactive session.");
422
423 /* Initialize the SIGCHLD kludge. */
424 child_pid = pid;
425 child_terminated = 0;
426 signal(SIGCHLD, sigchld_handler);
427
428 /* Initialize our global variables. */
429 fdin = fdin_arg;
430 fdout = fdout_arg;
431 fderr = fderr_arg;
0e73cc53 432
433 /* nonblocking IO */
434 set_nonblock(fdin);
435 set_nonblock(fdout);
ff873e98 436 /* we don't have stderr for interactive terminal sessions, see below */
437 if (fderr != -1)
438 set_nonblock(fderr);
0e73cc53 439
e68225b2 440 if (!(datafellows & SSH_BUG_IGNOREMSG) && isatty(fdin))
441 fdin_is_tty = 1;
442
5260325f 443 connection_in = packet_get_connection_in();
444 connection_out = packet_get_connection_out();
445
446 previous_stdout_buffer_bytes = 0;
447
448 /* Set approximate I/O buffer size. */
449 if (packet_is_interactive())
450 buffer_high = 4096;
451 else
452 buffer_high = 64 * 1024;
453
454 /* Initialize max_fd to the maximum of the known file descriptors. */
39929cdb 455 max_fd = MAX(fdin, fdout);
456 if (fderr != -1)
457 max_fd = MAX(max_fd, fderr);
458 max_fd = MAX(max_fd, connection_in);
459 max_fd = MAX(max_fd, connection_out);
5260325f 460
461 /* Initialize Initialize buffers. */
462 buffer_init(&stdin_buffer);
463 buffer_init(&stdout_buffer);
464 buffer_init(&stderr_buffer);
465
aa3378df 466 /*
467 * If we have no separate fderr (which is the case when we have a pty
468 * - there we cannot make difference between data sent to stdout and
469 * stderr), indicate that we have seen an EOF from stderr. This way
470 * we don\'t need to check the descriptor everywhere.
471 */
5260325f 472 if (fderr == -1)
473 fderr_eof = 1;
474
7368a6c8 475 server_init_dispatch();
476
5260325f 477 /* Main loop of the server for the interactive session mode. */
478 for (;;) {
5260325f 479
480 /* Process buffered packets from the client. */
481 process_buffered_input_packets();
482
aa3378df 483 /*
484 * If we have received eof, and there is no more pending
485 * input data, cause a real eof by closing fdin.
486 */
5260325f 487 if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) {
8efc0c15 488#ifdef USE_PIPES
5260325f 489 close(fdin);
8efc0c15 490#else
7368a6c8 491 if (fdin != fdout)
5260325f 492 close(fdin);
493 else
494 shutdown(fdin, SHUT_WR); /* We will no longer send. */
8efc0c15 495#endif
5260325f 496 fdin = -1;
497 }
aa3378df 498 /* Make packets from buffered stderr data to send to the client. */
5260325f 499 make_packets_from_stderr_data();
500
aa3378df 501 /*
502 * Make packets from buffered stdout data to send to the
503 * client. If there is very little to send, this arranges to
504 * not send them now, but to wait a short while to see if we
505 * are getting more data. This is necessary, as some systems
506 * wake up readers from a pty after each separate character.
507 */
5260325f 508 max_time_milliseconds = 0;
509 stdout_buffer_bytes = buffer_len(&stdout_buffer);
510 if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 &&
511 stdout_buffer_bytes != previous_stdout_buffer_bytes) {
512 /* try again after a while */
513 max_time_milliseconds = 10;
514 } else {
515 /* Send it now. */
516 make_packets_from_stdout_data();
517 }
518 previous_stdout_buffer_bytes = buffer_len(&stdout_buffer);
519
520 /* Send channel data to the client. */
521 if (packet_not_very_much_data_to_write())
522 channel_output_poll();
523
aa3378df 524 /*
525 * Bail out of the loop if the program has closed its output
526 * descriptors, and we have no more data to send to the
527 * client, and there is no pending buffered data.
528 */
7062c40f 529 if (fdout_eof && fderr_eof && !packet_have_data_to_write() &&
530 buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) {
5260325f 531 if (!channel_still_open())
7368a6c8 532 break;
5260325f 533 if (!waiting_termination) {
534 const char *s = "Waiting for forwarded connections to terminate...\r\n";
535 char *cp;
536 waiting_termination = 1;
537 buffer_append(&stderr_buffer, s, strlen(s));
538
539 /* Display list of open channels. */
540 cp = channel_open_message();
541 buffer_append(&stderr_buffer, cp, strlen(cp));
542 xfree(cp);
543 }
544 }
545 /* Sleep in select() until we can do something. */
39929cdb 546 wait_until_can_do_something(&readset, &writeset, &max_fd,
547 max_time_milliseconds);
5260325f 548
549 /* Process any channel events. */
39929cdb 550 channel_after_select(readset, writeset);
5260325f 551
552 /* Process input from the client and from program stdout/stderr. */
39929cdb 553 process_input(readset);
5260325f 554
555 /* Process output to the client and to program stdin. */
39929cdb 556 process_output(writeset);
8efc0c15 557 }
39929cdb 558 if (readset)
559 xfree(readset);
560 if (writeset)
561 xfree(writeset);
8efc0c15 562
5260325f 563 /* Cleanup and termination code. */
8efc0c15 564
5260325f 565 /* Wait until all output has been sent to the client. */
566 drain_output();
8efc0c15 567
5260325f 568 debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.",
569 stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes);
8efc0c15 570
5260325f 571 /* Free and clear the buffers. */
572 buffer_free(&stdin_buffer);
573 buffer_free(&stdout_buffer);
574 buffer_free(&stderr_buffer);
8efc0c15 575
5260325f 576 /* Close the file descriptors. */
577 if (fdout != -1)
578 close(fdout);
579 fdout = -1;
580 fdout_eof = 1;
581 if (fderr != -1)
582 close(fderr);
583 fderr = -1;
584 fderr_eof = 1;
585 if (fdin != -1)
586 close(fdin);
587 fdin = -1;
588
589 /* Stop listening for channels; this removes unix domain sockets. */
590 channel_stop_listening();
591
592 /* Wait for the child to exit. Get its exit status. */
593 wait_pid = wait(&wait_status);
1e3b8b07 594 if (wait_pid == -1) {
5260325f 595 /*
596 * It is possible that the wait was handled by SIGCHLD
597 * handler. This may result in either: this call
598 * returning with EINTR, or: this call returning ECHILD.
599 */
600 if (child_terminated)
601 wait_status = child_wait_status;
602 else
603 packet_disconnect("wait: %.100s", strerror(errno));
604 } else {
605 /* Check if it matches the process we forked. */
606 if (wait_pid != pid)
607 error("Strange, wait returned pid %d, expected %d",
57112b5a 608 wait_pid, pid);
5260325f 609 }
8efc0c15 610
5260325f 611 /* We no longer want our SIGCHLD handler to be called. */
612 signal(SIGCHLD, SIG_DFL);
613
614 /* Check if it exited normally. */
615 if (WIFEXITED(wait_status)) {
616 /* Yes, normal exit. Get exit status and send it to the client. */
617 debug("Command exited with status %d.", WEXITSTATUS(wait_status));
618 packet_start(SSH_SMSG_EXITSTATUS);
619 packet_put_int(WEXITSTATUS(wait_status));
620 packet_send();
621 packet_write_wait();
622
aa3378df 623 /*
624 * Wait for exit confirmation. Note that there might be
625 * other packets coming before it; however, the program has
626 * already died so we just ignore them. The client is
627 * supposed to respond with the confirmation when it receives
628 * the exit status.
629 */
5260325f 630 do {
631 int plen;
632 type = packet_read(&plen);
633 }
634 while (type != SSH_CMSG_EXIT_CONFIRMATION);
635
636 debug("Received exit confirmation.");
637 return;
638 }
639 /* Check if the program terminated due to a signal. */
640 if (WIFSIGNALED(wait_status))
641 packet_disconnect("Command terminated on signal %d.",
642 WTERMSIG(wait_status));
643
644 /* Some weird exit cause. Just exit. */
645 packet_disconnect("wait returned status %04x.", wait_status);
646 /* NOTREACHED */
647}
7368a6c8 648
6ae2364d 649void
e78a59f5 650server_loop2(void)
651{
39929cdb 652 fd_set *readset = NULL, *writeset = NULL;
653 int max_fd;
e78a59f5 654 int had_channel = 0;
655 int status;
656 pid_t pid;
657
658 debug("Entering interactive session for SSH2.");
659
1aa00dcb 660 mysignal(SIGCHLD, sigchld_handler2);
e78a59f5 661 child_terminated = 0;
662 connection_in = packet_get_connection_in();
663 connection_out = packet_get_connection_out();
39929cdb 664
665 max_fd = MAX(connection_in, connection_out);
666
e78a59f5 667 server_init_dispatch();
668
669 for (;;) {
670 process_buffered_input_packets();
671 if (!had_channel && channel_still_open())
672 had_channel = 1;
673 if (had_channel && !channel_still_open()) {
674 debug("!channel_still_open.");
675 break;
676 }
677 if (packet_not_very_much_data_to_write())
678 channel_output_poll();
39929cdb 679 wait_until_can_do_something(&readset, &writeset, &max_fd, 0);
7062c40f 680 if (child_terminated) {
e78a59f5 681 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
682 session_close_by_pid(pid, status);
683 child_terminated = 0;
684 }
39929cdb 685 channel_after_select(readset, writeset);
686 process_input(readset);
687 process_output(writeset);
e78a59f5 688 }
39929cdb 689 if (readset)
690 xfree(readset);
691 if (writeset)
692 xfree(writeset);
693
e78a59f5 694 signal(SIGCHLD, SIG_DFL);
695 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
696 session_close_by_pid(pid, status);
697 channel_stop_listening();
698}
699
7368a6c8 700void
188adeb2 701server_input_stdin_data(int type, int plen, void *ctxt)
7368a6c8 702{
703 char *data;
1e3b8b07 704 u_int data_len;
7368a6c8 705
706 /* Stdin data from the client. Append it to the buffer. */
707 /* Ignore any data if the client has closed stdin. */
708 if (fdin == -1)
709 return;
710 data = packet_get_string(&data_len);
711 packet_integrity_check(plen, (4 + data_len), type);
712 buffer_append(&stdin_buffer, data, data_len);
713 memset(data, 0, data_len);
714 xfree(data);
715}
716
717void
188adeb2 718server_input_eof(int type, int plen, void *ctxt)
7368a6c8 719{
720 /*
721 * Eof from the client. The stdin descriptor to the
722 * program will be closed when all buffered data has
723 * drained.
724 */
725 debug("EOF received for stdin.");
726 packet_integrity_check(plen, 0, type);
727 stdin_eof = 1;
728}
729
730void
188adeb2 731server_input_window_size(int type, int plen, void *ctxt)
7368a6c8 732{
733 int row = packet_get_int();
734 int col = packet_get_int();
735 int xpixel = packet_get_int();
736 int ypixel = packet_get_int();
737
738 debug("Window change received.");
739 packet_integrity_check(plen, 4 * 4, type);
740 if (fdin != -1)
741 pty_change_window_size(fdin, row, col, xpixel, ypixel);
742}
743
fa08c86b 744Channel *
745server_request_direct_tcpip(char *ctype)
e78a59f5 746{
fa08c86b 747 int sock, newch;
6ae2364d 748 char *target, *originator;
749 int target_port, originator_port;
e78a59f5 750
6ae2364d 751 target = packet_get_string(NULL);
752 target_port = packet_get_int();
e78a59f5 753 originator = packet_get_string(NULL);
754 originator_port = packet_get_int();
6ae2364d 755 packet_done();
71276795 756
fa08c86b 757 debug("server_request_direct_tcpip: originator %s port %d, target %s port %d",
71276795 758 originator, originator_port, target, target_port);
38c295d6 759
e78a59f5 760 /* XXX check permission */
6ae2364d 761 sock = channel_connect_to(target, target_port);
762 xfree(target);
e78a59f5 763 xfree(originator);
764 if (sock < 0)
fa08c86b 765 return NULL;
97fb6912 766 newch = channel_new(ctype, SSH_CHANNEL_CONNECTING,
bcbf86ec 767 sock, sock, -1, CHAN_TCP_WINDOW_DEFAULT,
a22aff1f 768 CHAN_TCP_PACKET_DEFAULT, 0, xstrdup("direct-tcpip"), 1);
fa08c86b 769 return (newch >= 0) ? channel_lookup(newch) : NULL;
770}
771
772Channel *
773server_request_session(char *ctype)
774{
775 int newch;
776
777 debug("input_session_request");
778 packet_done();
779 /*
780 * A server session has no fd to read or write until a
781 * CHANNEL_REQUEST for a shell is made, so we set the type to
782 * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all
783 * CHANNEL_REQUEST messages is registered.
784 */
785 newch = channel_new(ctype, SSH_CHANNEL_LARVAL,
786 -1, -1, -1, 0, CHAN_SES_PACKET_DEFAULT,
787 0, xstrdup("server-session"), 1);
788 if (session_open(newch) == 1) {
789 channel_register_callback(newch, SSH2_MSG_CHANNEL_REQUEST,
790 session_input_channel_req, (void *)0);
791 channel_register_cleanup(newch, session_close_by_channel);
792 return channel_lookup(newch);
793 } else {
794 debug("session open failed, free channel %d", newch);
795 channel_free(newch);
796 }
797 return NULL;
e78a59f5 798}
799
6ae2364d 800void
188adeb2 801server_input_channel_open(int type, int plen, void *ctxt)
e78a59f5 802{
803 Channel *c = NULL;
804 char *ctype;
1e3b8b07 805 u_int len;
e78a59f5 806 int rchan;
807 int rmaxpack;
808 int rwindow;
809
810 ctype = packet_get_string(&len);
811 rchan = packet_get_int();
812 rwindow = packet_get_int();
813 rmaxpack = packet_get_int();
814
188adeb2 815 debug("server_input_channel_open: ctype %s rchan %d win %d max %d",
e78a59f5 816 ctype, rchan, rwindow, rmaxpack);
817
818 if (strcmp(ctype, "session") == 0) {
fa08c86b 819 c = server_request_session(ctype);
e78a59f5 820 } else if (strcmp(ctype, "direct-tcpip") == 0) {
fa08c86b 821 c = server_request_direct_tcpip(ctype);
e78a59f5 822 }
823 if (c != NULL) {
fa08c86b 824 debug("server_input_channel_open: confirm %s", ctype);
e78a59f5 825 c->remote_id = rchan;
826 c->remote_window = rwindow;
827 c->remote_maxpacket = rmaxpack;
828
829 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
830 packet_put_int(c->remote_id);
831 packet_put_int(c->self);
832 packet_put_int(c->local_window);
833 packet_put_int(c->local_maxpacket);
834 packet_send();
835 } else {
fa08c86b 836 debug("server_input_channel_open: failure %s", ctype);
e78a59f5 837 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
838 packet_put_int(rchan);
839 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
840 packet_put_cstring("bla bla");
841 packet_put_cstring("");
842 packet_send();
843 }
844 xfree(ctype);
845}
846
2b87da3b 847void
fa08c86b 848server_input_global_request(int type, int plen, void *ctxt)
849{
850 char *rtype;
851 int want_reply;
852 int success = 0;
853
854 rtype = packet_get_string(NULL);
855 want_reply = packet_get_char();
856 debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply);
2b87da3b 857
01794848 858 /* -R style forwarding */
fa08c86b 859 if (strcmp(rtype, "tcpip-forward") == 0) {
860 struct passwd *pw;
861 char *listen_address;
862 u_short listen_port;
863
864 pw = auth_get_user();
865 if (pw == NULL)
866 fatal("server_input_global_request: no user");
867 listen_address = packet_get_string(NULL); /* XXX currently ignored */
868 listen_port = (u_short)packet_get_int();
869 debug("server_input_global_request: tcpip-forward listen %s port %d",
870 listen_address, listen_port);
871
872 /* check permissions */
873 if (!options.allow_tcp_forwarding ||
874 no_port_forwarding_flag ||
875 (listen_port < IPPORT_RESERVED && pw->pw_uid != 0)) {
876 success = 0;
877 packet_send_debug("Server has disabled port forwarding.");
878 } else {
879 /* Start listening on the port */
02a024dd 880 success = channel_request_forwarding(
fa08c86b 881 listen_address, listen_port,
882 /*unspec host_to_connect*/ "<unspec host>",
883 /*unspec port_to_connect*/ 0,
884 options.gateway_ports, /*remote*/ 1);
fa08c86b 885 }
886 xfree(listen_address);
887 }
888 if (want_reply) {
889 packet_start(success ?
890 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
891 packet_send();
892 packet_write_wait();
893 }
894 xfree(rtype);
895}
896
6ae2364d 897void
5ca51e19 898server_init_dispatch_20(void)
e78a59f5 899{
900 debug("server_init_dispatch_20");
901 dispatch_init(&dispatch_protocol_error);
902 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
903 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
904 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
905 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
906 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
907 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
908 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
909 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
910 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
fa08c86b 911 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request);
7a37c112 912
913 /* rekeying */
914 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
e78a59f5 915}
6ae2364d 916void
5ca51e19 917server_init_dispatch_13(void)
7368a6c8 918{
919 debug("server_init_dispatch_13");
920 dispatch_init(NULL);
921 dispatch_set(SSH_CMSG_EOF, &server_input_eof);
922 dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data);
923 dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size);
924 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
925 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
926 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
927 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
928 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
929 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
930}
6ae2364d 931void
5ca51e19 932server_init_dispatch_15(void)
7368a6c8 933{
934 server_init_dispatch_13();
935 debug("server_init_dispatch_15");
936 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
937 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose);
938}
6ae2364d 939void
5ca51e19 940server_init_dispatch(void)
7368a6c8 941{
e78a59f5 942 if (compat20)
943 server_init_dispatch_20();
944 else if (compat13)
7368a6c8 945 server_init_dispatch_13();
946 else
947 server_init_dispatch_15();
948}
This page took 3.174542 seconds and 5 git commands to generate.