summaryrefslogtreecommitdiff
path: root/serverloop.c
diff options
context:
space:
mode:
authormarkus@openbsd.org <markus@openbsd.org>2016-08-13 17:47:40 +0000
committerDamien Miller <djm@mindrot.org>2016-08-14 11:19:14 +1000
commit6cb6dcffe1a2204ba9006de20f73255c268fcb6b (patch)
tree235267a1264f9363c39c4c0b11b59384e9acbdcf /serverloop.c
parent42d47adc5ad1187f22c726cbc52e71d6b1767ca2 (diff)
upstream commit
remove ssh1 server code; ok djm@ Upstream-ID: c24c0c32c49b91740d5a94ae914fb1898ea5f534
Diffstat (limited to 'serverloop.c')
-rw-r--r--serverloop.c596
1 files changed, 22 insertions, 574 deletions
diff --git a/serverloop.c b/serverloop.c
index 3563e5d42..1e211701e 100644
--- a/serverloop.c
+++ b/serverloop.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: serverloop.c,v 1.184 2016/03/07 19:02:43 djm Exp $ */ 1/* $OpenBSD: serverloop.c,v 1.185 2016/08/13 17:47:41 markus Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -67,7 +67,6 @@
67#include "sshpty.h" 67#include "sshpty.h"
68#include "channels.h" 68#include "channels.h"
69#include "compat.h" 69#include "compat.h"
70#include "ssh1.h"
71#include "ssh2.h" 70#include "ssh2.h"
72#include "key.h" 71#include "key.h"
73#include "cipher.h" 72#include "cipher.h"
@@ -86,25 +85,6 @@ extern ServerOptions options;
86extern Authctxt *the_authctxt; 85extern Authctxt *the_authctxt;
87extern int use_privsep; 86extern int use_privsep;
88 87
89static Buffer stdin_buffer; /* Buffer for stdin data. */
90static Buffer stdout_buffer; /* Buffer for stdout data. */
91static Buffer stderr_buffer; /* Buffer for stderr data. */
92static int fdin; /* Descriptor for stdin (for writing) */
93static int fdout; /* Descriptor for stdout (for reading);
94 May be same number as fdin. */
95static int fderr; /* Descriptor for stderr. May be -1. */
96static long stdin_bytes = 0; /* Number of bytes written to stdin. */
97static long stdout_bytes = 0; /* Number of stdout bytes sent to client. */
98static long stderr_bytes = 0; /* Number of stderr bytes sent to client. */
99static long fdout_bytes = 0; /* Number of stdout bytes read from program. */
100static int stdin_eof = 0; /* EOF message received from client. */
101static int fdout_eof = 0; /* EOF encountered reading from fdout. */
102static int fderr_eof = 0; /* EOF encountered readung from fderr. */
103static int fdin_is_tty = 0; /* fdin points to a tty. */
104static int connection_in; /* Connection to client (input). */
105static int connection_out; /* Connection to client (output). */
106static int connection_closed = 0; /* Connection to client closed. */
107static u_int buffer_high; /* "Soft" max buffer size. */
108static int no_more_sessions = 0; /* Disallow further sessions. */ 88static int no_more_sessions = 0; /* Disallow further sessions. */
109 89
110/* 90/*
@@ -185,64 +165,6 @@ sigterm_handler(int sig)
185 received_sigterm = sig; 165 received_sigterm = sig;
186} 166}
187 167
188/*
189 * Make packets from buffered stderr data, and buffer it for sending
190 * to the client.
191 */
192static void
193make_packets_from_stderr_data(void)
194{
195 u_int len;
196
197 /* Send buffered stderr data to the client. */
198 while (buffer_len(&stderr_buffer) > 0 &&
199 packet_not_very_much_data_to_write()) {
200 len = buffer_len(&stderr_buffer);
201 if (packet_is_interactive()) {
202 if (len > 512)
203 len = 512;
204 } else {
205 /* Keep the packets at reasonable size. */
206 if (len > packet_get_maxsize())
207 len = packet_get_maxsize();
208 }
209 packet_start(SSH_SMSG_STDERR_DATA);
210 packet_put_string(buffer_ptr(&stderr_buffer), len);
211 packet_send();
212 buffer_consume(&stderr_buffer, len);
213 stderr_bytes += len;
214 }
215}
216
217/*
218 * Make packets from buffered stdout data, and buffer it for sending to the
219 * client.
220 */
221static void
222make_packets_from_stdout_data(void)
223{
224 u_int len;
225
226 /* Send buffered stdout data to the client. */
227 while (buffer_len(&stdout_buffer) > 0 &&
228 packet_not_very_much_data_to_write()) {
229 len = buffer_len(&stdout_buffer);
230 if (packet_is_interactive()) {
231 if (len > 512)
232 len = 512;
233 } else {
234 /* Keep the packets at reasonable size. */
235 if (len > packet_get_maxsize())
236 len = packet_get_maxsize();
237 }
238 packet_start(SSH_SMSG_STDOUT_DATA);
239 packet_put_string(buffer_ptr(&stdout_buffer), len);
240 packet_send();
241 buffer_consume(&stdout_buffer, len);
242 stdout_bytes += len;
243 }
244}
245
246static void 168static void
247client_alive_check(void) 169client_alive_check(void)
248{ 170{
@@ -275,14 +197,14 @@ client_alive_check(void)
275 * for the duration of the wait (0 = infinite). 197 * for the duration of the wait (0 = infinite).
276 */ 198 */
277static void 199static void
278wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, int *maxfdp, 200wait_until_can_do_something(int connection_in, int connection_out,
201 fd_set **readsetp, fd_set **writesetp, int *maxfdp,
279 u_int *nallocp, u_int64_t max_time_ms) 202 u_int *nallocp, u_int64_t max_time_ms)
280{ 203{
281 struct timeval tv, *tvp; 204 struct timeval tv, *tvp;
282 int ret; 205 int ret;
283 time_t minwait_secs = 0; 206 time_t minwait_secs = 0;
284 int client_alive_scheduled = 0; 207 int client_alive_scheduled = 0;
285 int program_alive_scheduled = 0;
286 208
287 /* Allocate and update select() masks for channel descriptors. */ 209 /* Allocate and update select() masks for channel descriptors. */
288 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, 210 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
@@ -300,7 +222,7 @@ wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
300 * this could be randomized somewhat to make traffic 222 * this could be randomized somewhat to make traffic
301 * analysis more difficult, but we're not doing it yet. 223 * analysis more difficult, but we're not doing it yet.
302 */ 224 */
303 if (compat20 && options.client_alive_interval) { 225 if (options.client_alive_interval) {
304 uint64_t keepalive_ms = 226 uint64_t keepalive_ms =
305 (uint64_t)options.client_alive_interval * 1000; 227 (uint64_t)options.client_alive_interval * 1000;
306 228
@@ -309,38 +231,11 @@ wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
309 max_time_ms = keepalive_ms; 231 max_time_ms = keepalive_ms;
310 } 232 }
311 233
312 if (compat20) {
313#if 0 234#if 0
314 /* wrong: bad condition XXX */ 235 /* wrong: bad condition XXX */
315 if (channel_not_very_much_buffered_data()) 236 if (channel_not_very_much_buffered_data())
316#endif 237#endif
317 FD_SET(connection_in, *readsetp); 238 FD_SET(connection_in, *readsetp);
318 } else {
319 /*
320 * Read packets from the client unless we have too much
321 * buffered stdin or channel data.
322 */
323 if (buffer_len(&stdin_buffer) < buffer_high &&
324 channel_not_very_much_buffered_data())
325 FD_SET(connection_in, *readsetp);
326 /*
327 * If there is not too much data already buffered going to
328 * the client, try to get some more data from the program.
329 */
330 if (packet_not_very_much_data_to_write()) {
331 program_alive_scheduled = child_terminated;
332 if (!fdout_eof)
333 FD_SET(fdout, *readsetp);
334 if (!fderr_eof)
335 FD_SET(fderr, *readsetp);
336 }
337 /*
338 * If we have buffered data, try to write some of that data
339 * to the program.
340 */
341 if (fdin != -1 && buffer_len(&stdin_buffer) > 0)
342 FD_SET(fdin, *writesetp);
343 }
344 notify_prepare(*readsetp); 239 notify_prepare(*readsetp);
345 240
346 /* 241 /*
@@ -374,16 +269,8 @@ wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
374 memset(*writesetp, 0, *nallocp); 269 memset(*writesetp, 0, *nallocp);
375 if (errno != EINTR) 270 if (errno != EINTR)
376 error("select: %.100s", strerror(errno)); 271 error("select: %.100s", strerror(errno));
377 } else { 272 } else if (ret == 0 && client_alive_scheduled)
378 if (ret == 0 && client_alive_scheduled) 273 client_alive_check();
379 client_alive_check();
380 if (!compat20 && program_alive_scheduled && fdin_is_tty) {
381 if (!fdout_eof)
382 FD_SET(fdout, *readsetp);
383 if (!fderr_eof)
384 FD_SET(fderr, *readsetp);
385 }
386 }
387 274
388 notify_done(*readsetp); 275 notify_done(*readsetp);
389} 276}
@@ -392,8 +279,8 @@ wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
392 * Processes input from the client and the program. Input data is stored 279 * Processes input from the client and the program. Input data is stored
393 * in buffers and processed later. 280 * in buffers and processed later.
394 */ 281 */
395static void 282static int
396process_input(fd_set *readset) 283process_input(fd_set *readset, int connection_in)
397{ 284{
398 struct ssh *ssh = active_state; /* XXX */ 285 struct ssh *ssh = active_state; /* XXX */
399 int len; 286 int len;
@@ -405,10 +292,7 @@ process_input(fd_set *readset)
405 if (len == 0) { 292 if (len == 0) {
406 verbose("Connection closed by %.100s port %d", 293 verbose("Connection closed by %.100s port %d",
407 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); 294 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
408 connection_closed = 1; 295 return -1;
409 if (compat20)
410 return;
411 cleanup_exit(255);
412 } else if (len < 0) { 296 } else if (len < 0) {
413 if (errno != EINTR && errno != EAGAIN && 297 if (errno != EINTR && errno != EAGAIN &&
414 errno != EWOULDBLOCK) { 298 errno != EWOULDBLOCK) {
@@ -423,381 +307,26 @@ process_input(fd_set *readset)
423 packet_process_incoming(buf, len); 307 packet_process_incoming(buf, len);
424 } 308 }
425 } 309 }
426 if (compat20) 310 return 0;
427 return;
428
429 /* Read and buffer any available stdout data from the program. */
430 if (!fdout_eof && FD_ISSET(fdout, readset)) {
431 errno = 0;
432 len = read(fdout, buf, sizeof(buf));
433 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
434 errno == EWOULDBLOCK) && !child_terminated))) {
435 /* do nothing */
436#ifndef PTY_ZEROREAD
437 } else if (len <= 0) {
438#else
439 } else if ((!isatty(fdout) && len <= 0) ||
440 (isatty(fdout) && (len < 0 || (len == 0 && errno != 0)))) {
441#endif
442 fdout_eof = 1;
443 } else {
444 buffer_append(&stdout_buffer, buf, len);
445 fdout_bytes += len;
446 }
447 }
448 /* Read and buffer any available stderr data from the program. */
449 if (!fderr_eof && FD_ISSET(fderr, readset)) {
450 errno = 0;
451 len = read(fderr, buf, sizeof(buf));
452 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
453 errno == EWOULDBLOCK) && !child_terminated))) {
454 /* do nothing */
455#ifndef PTY_ZEROREAD
456 } else if (len <= 0) {
457#else
458 } else if ((!isatty(fderr) && len <= 0) ||
459 (isatty(fderr) && (len < 0 || (len == 0 && errno != 0)))) {
460#endif
461 fderr_eof = 1;
462 } else {
463 buffer_append(&stderr_buffer, buf, len);
464 }
465 }
466} 311}
467 312
468/* 313/*
469 * Sends data from internal buffers to client program stdin. 314 * Sends data from internal buffers to client program stdin.
470 */ 315 */
471static void 316static void
472process_output(fd_set *writeset) 317process_output(fd_set *writeset, int connection_out)
473{ 318{
474 struct termios tio;
475 u_char *data;
476 u_int dlen;
477 int len;
478
479 /* Write buffered data to program stdin. */
480 if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) {
481 data = buffer_ptr(&stdin_buffer);
482 dlen = buffer_len(&stdin_buffer);
483 len = write(fdin, data, dlen);
484 if (len < 0 &&
485 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) {
486 /* do nothing */
487 } else if (len <= 0) {
488 if (fdin != fdout)
489 close(fdin);
490 else
491 shutdown(fdin, SHUT_WR); /* We will no longer send. */
492 fdin = -1;
493 } else {
494 /* Successful write. */
495 if (fdin_is_tty && dlen >= 1 && data[0] != '\r' &&
496 tcgetattr(fdin, &tio) == 0 &&
497 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
498 /*
499 * Simulate echo to reduce the impact of
500 * traffic analysis
501 */
502 packet_send_ignore(len);
503 packet_send();
504 }
505 /* Consume the data from the buffer. */
506 buffer_consume(&stdin_buffer, len);
507 /* Update the count of bytes written to the program. */
508 stdin_bytes += len;
509 }
510 }
511 /* Send any buffered packet data to the client. */ 319 /* Send any buffered packet data to the client. */
512 if (FD_ISSET(connection_out, writeset)) 320 if (FD_ISSET(connection_out, writeset))
513 packet_write_poll(); 321 packet_write_poll();
514} 322}
515 323
516/*
517 * Wait until all buffered output has been sent to the client.
518 * This is used when the program terminates.
519 */
520static void
521drain_output(void)
522{
523 /* Send any buffered stdout data to the client. */
524 if (buffer_len(&stdout_buffer) > 0) {
525 packet_start(SSH_SMSG_STDOUT_DATA);
526 packet_put_string(buffer_ptr(&stdout_buffer),
527 buffer_len(&stdout_buffer));
528 packet_send();
529 /* Update the count of sent bytes. */
530 stdout_bytes += buffer_len(&stdout_buffer);
531 }
532 /* Send any buffered stderr data to the client. */
533 if (buffer_len(&stderr_buffer) > 0) {
534 packet_start(SSH_SMSG_STDERR_DATA);
535 packet_put_string(buffer_ptr(&stderr_buffer),
536 buffer_len(&stderr_buffer));
537 packet_send();
538 /* Update the count of sent bytes. */
539 stderr_bytes += buffer_len(&stderr_buffer);
540 }
541 /* Wait until all buffered data has been written to the client. */
542 packet_write_wait();
543}
544
545static void 324static void
546process_buffered_input_packets(void) 325process_buffered_input_packets(void)
547{ 326{
548 dispatch_run(DISPATCH_NONBLOCK, NULL, active_state); 327 dispatch_run(DISPATCH_NONBLOCK, NULL, active_state);
549} 328}
550 329
551/*
552 * Performs the interactive session. This handles data transmission between
553 * the client and the program. Note that the notion of stdin, stdout, and
554 * stderr in this function is sort of reversed: this function writes to
555 * stdin (of the child program), and reads from stdout and stderr (of the
556 * child program).
557 */
558void
559server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg)
560{
561 fd_set *readset = NULL, *writeset = NULL;
562 int max_fd = 0;
563 u_int nalloc = 0;
564 int wait_status; /* Status returned by wait(). */
565 pid_t wait_pid; /* pid returned by wait(). */
566 int waiting_termination = 0; /* Have displayed waiting close message. */
567 u_int64_t max_time_milliseconds;
568 u_int previous_stdout_buffer_bytes;
569 u_int stdout_buffer_bytes;
570 int type;
571
572 debug("Entering interactive session.");
573
574 /* Initialize the SIGCHLD kludge. */
575 child_terminated = 0;
576 mysignal(SIGCHLD, sigchld_handler);
577
578 if (!use_privsep) {
579 signal(SIGTERM, sigterm_handler);
580 signal(SIGINT, sigterm_handler);
581 signal(SIGQUIT, sigterm_handler);
582 }
583
584 /* Initialize our global variables. */
585 fdin = fdin_arg;
586 fdout = fdout_arg;
587 fderr = fderr_arg;
588
589 /* nonblocking IO */
590 set_nonblock(fdin);
591 set_nonblock(fdout);
592 /* we don't have stderr for interactive terminal sessions, see below */
593 if (fderr != -1)
594 set_nonblock(fderr);
595
596 if (!(datafellows & SSH_BUG_IGNOREMSG) && isatty(fdin))
597 fdin_is_tty = 1;
598
599 connection_in = packet_get_connection_in();
600 connection_out = packet_get_connection_out();
601
602 notify_setup();
603
604 previous_stdout_buffer_bytes = 0;
605
606 /* Set approximate I/O buffer size. */
607 if (packet_is_interactive())
608 buffer_high = 4096;
609 else
610 buffer_high = 64 * 1024;
611
612#if 0
613 /* Initialize max_fd to the maximum of the known file descriptors. */
614 max_fd = MAX(connection_in, connection_out);
615 max_fd = MAX(max_fd, fdin);
616 max_fd = MAX(max_fd, fdout);
617 if (fderr != -1)
618 max_fd = MAX(max_fd, fderr);
619#endif
620
621 /* Initialize Initialize buffers. */
622 buffer_init(&stdin_buffer);
623 buffer_init(&stdout_buffer);
624 buffer_init(&stderr_buffer);
625
626 /*
627 * If we have no separate fderr (which is the case when we have a pty
628 * - there we cannot make difference between data sent to stdout and
629 * stderr), indicate that we have seen an EOF from stderr. This way
630 * we don't need to check the descriptor everywhere.
631 */
632 if (fderr == -1)
633 fderr_eof = 1;
634
635 server_init_dispatch();
636
637 /* Main loop of the server for the interactive session mode. */
638 for (;;) {
639
640 /* Process buffered packets from the client. */
641 process_buffered_input_packets();
642
643 /*
644 * If we have received eof, and there is no more pending
645 * input data, cause a real eof by closing fdin.
646 */
647 if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) {
648 if (fdin != fdout)
649 close(fdin);
650 else
651 shutdown(fdin, SHUT_WR); /* We will no longer send. */
652 fdin = -1;
653 }
654 /* Make packets from buffered stderr data to send to the client. */
655 make_packets_from_stderr_data();
656
657 /*
658 * Make packets from buffered stdout data to send to the
659 * client. If there is very little to send, this arranges to
660 * not send them now, but to wait a short while to see if we
661 * are getting more data. This is necessary, as some systems
662 * wake up readers from a pty after each separate character.
663 */
664 max_time_milliseconds = 0;
665 stdout_buffer_bytes = buffer_len(&stdout_buffer);
666 if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 &&
667 stdout_buffer_bytes != previous_stdout_buffer_bytes) {
668 /* try again after a while */
669 max_time_milliseconds = 10;
670 } else {
671 /* Send it now. */
672 make_packets_from_stdout_data();
673 }
674 previous_stdout_buffer_bytes = buffer_len(&stdout_buffer);
675
676 /* Send channel data to the client. */
677 if (packet_not_very_much_data_to_write())
678 channel_output_poll();
679
680 /*
681 * Bail out of the loop if the program has closed its output
682 * descriptors, and we have no more data to send to the
683 * client, and there is no pending buffered data.
684 */
685 if (fdout_eof && fderr_eof && !packet_have_data_to_write() &&
686 buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) {
687 if (!channel_still_open())
688 break;
689 if (!waiting_termination) {
690 const char *s = "Waiting for forwarded connections to terminate...\r\n";
691 char *cp;
692 waiting_termination = 1;
693 buffer_append(&stderr_buffer, s, strlen(s));
694
695 /* Display list of open channels. */
696 cp = channel_open_message();
697 buffer_append(&stderr_buffer, cp, strlen(cp));
698 free(cp);
699 }
700 }
701 max_fd = MAX(connection_in, connection_out);
702 max_fd = MAX(max_fd, fdin);
703 max_fd = MAX(max_fd, fdout);
704 max_fd = MAX(max_fd, fderr);
705 max_fd = MAX(max_fd, notify_pipe[0]);
706
707 /* Sleep in select() until we can do something. */
708 wait_until_can_do_something(&readset, &writeset, &max_fd,
709 &nalloc, max_time_milliseconds);
710
711 if (received_sigterm) {
712 logit("Exiting on signal %d", (int)received_sigterm);
713 /* Clean up sessions, utmp, etc. */
714 cleanup_exit(255);
715 }
716
717 /* Process any channel events. */
718 channel_after_select(readset, writeset);
719
720 /* Process input from the client and from program stdout/stderr. */
721 process_input(readset);
722
723 /* Process output to the client and to program stdin. */
724 process_output(writeset);
725 }
726 free(readset);
727 free(writeset);
728
729 /* Cleanup and termination code. */
730
731 /* Wait until all output has been sent to the client. */
732 drain_output();
733
734 debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.",
735 stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes);
736
737 /* Free and clear the buffers. */
738 buffer_free(&stdin_buffer);
739 buffer_free(&stdout_buffer);
740 buffer_free(&stderr_buffer);
741
742 /* Close the file descriptors. */
743 if (fdout != -1)
744 close(fdout);
745 fdout = -1;
746 fdout_eof = 1;
747 if (fderr != -1)
748 close(fderr);
749 fderr = -1;
750 fderr_eof = 1;
751 if (fdin != -1)
752 close(fdin);
753 fdin = -1;
754
755 channel_free_all();
756
757 /* We no longer want our SIGCHLD handler to be called. */
758 mysignal(SIGCHLD, SIG_DFL);
759
760 while ((wait_pid = waitpid(-1, &wait_status, 0)) < 0)
761 if (errno != EINTR)
762 packet_disconnect("wait: %.100s", strerror(errno));
763 if (wait_pid != pid)
764 error("Strange, wait returned pid %ld, expected %ld",
765 (long)wait_pid, (long)pid);
766
767 /* Check if it exited normally. */
768 if (WIFEXITED(wait_status)) {
769 /* Yes, normal exit. Get exit status and send it to the client. */
770 debug("Command exited with status %d.", WEXITSTATUS(wait_status));
771 packet_start(SSH_SMSG_EXITSTATUS);
772 packet_put_int(WEXITSTATUS(wait_status));
773 packet_send();
774 packet_write_wait();
775
776 /*
777 * Wait for exit confirmation. Note that there might be
778 * other packets coming before it; however, the program has
779 * already died so we just ignore them. The client is
780 * supposed to respond with the confirmation when it receives
781 * the exit status.
782 */
783 do {
784 type = packet_read();
785 }
786 while (type != SSH_CMSG_EXIT_CONFIRMATION);
787
788 debug("Received exit confirmation.");
789 return;
790 }
791 /* Check if the program terminated due to a signal. */
792 if (WIFSIGNALED(wait_status))
793 packet_disconnect("Command terminated on signal %d.",
794 WTERMSIG(wait_status));
795
796 /* Some weird exit cause. Just exit. */
797 packet_disconnect("wait returned status %04x.", wait_status);
798 /* NOTREACHED */
799}
800
801static void 330static void
802collect_children(void) 331collect_children(void)
803{ 332{
@@ -825,7 +354,7 @@ server_loop2(Authctxt *authctxt)
825{ 354{
826 fd_set *readset = NULL, *writeset = NULL; 355 fd_set *readset = NULL, *writeset = NULL;
827 int max_fd; 356 int max_fd;
828 u_int nalloc = 0; 357 u_int nalloc = 0, connection_in, connection_out;
829 u_int64_t rekey_timeout_ms = 0; 358 u_int64_t rekey_timeout_ms = 0;
830 359
831 debug("Entering interactive session for SSH2."); 360 debug("Entering interactive session for SSH2.");
@@ -854,14 +383,14 @@ server_loop2(Authctxt *authctxt)
854 if (!ssh_packet_is_rekeying(active_state) && 383 if (!ssh_packet_is_rekeying(active_state) &&
855 packet_not_very_much_data_to_write()) 384 packet_not_very_much_data_to_write())
856 channel_output_poll(); 385 channel_output_poll();
857 if (options.rekey_interval > 0 && compat20 && 386 if (options.rekey_interval > 0 &&
858 !ssh_packet_is_rekeying(active_state)) 387 !ssh_packet_is_rekeying(active_state))
859 rekey_timeout_ms = packet_get_rekey_timeout() * 1000; 388 rekey_timeout_ms = packet_get_rekey_timeout() * 1000;
860 else 389 else
861 rekey_timeout_ms = 0; 390 rekey_timeout_ms = 0;
862 391
863 wait_until_can_do_something(&readset, &writeset, &max_fd, 392 wait_until_can_do_something(connection_in, connection_out,
864 &nalloc, rekey_timeout_ms); 393 &readset, &writeset, &max_fd, &nalloc, rekey_timeout_ms);
865 394
866 if (received_sigterm) { 395 if (received_sigterm) {
867 logit("Exiting on signal %d", (int)received_sigterm); 396 logit("Exiting on signal %d", (int)received_sigterm);
@@ -872,10 +401,9 @@ server_loop2(Authctxt *authctxt)
872 collect_children(); 401 collect_children();
873 if (!ssh_packet_is_rekeying(active_state)) 402 if (!ssh_packet_is_rekeying(active_state))
874 channel_after_select(readset, writeset); 403 channel_after_select(readset, writeset);
875 process_input(readset); 404 if (process_input(readset, connection_in) < 0)
876 if (connection_closed)
877 break; 405 break;
878 process_output(writeset); 406 process_output(writeset, connection_out);
879 } 407 }
880 collect_children(); 408 collect_children();
881 409
@@ -902,53 +430,6 @@ server_input_keep_alive(int type, u_int32_t seq, void *ctxt)
902 return 0; 430 return 0;
903} 431}
904 432
905static int
906server_input_stdin_data(int type, u_int32_t seq, void *ctxt)
907{
908 char *data;
909 u_int data_len;
910
911 /* Stdin data from the client. Append it to the buffer. */
912 /* Ignore any data if the client has closed stdin. */
913 if (fdin == -1)
914 return 0;
915 data = packet_get_string(&data_len);
916 packet_check_eom();
917 buffer_append(&stdin_buffer, data, data_len);
918 explicit_bzero(data, data_len);
919 free(data);
920 return 0;
921}
922
923static int
924server_input_eof(int type, u_int32_t seq, void *ctxt)
925{
926 /*
927 * Eof from the client. The stdin descriptor to the
928 * program will be closed when all buffered data has
929 * drained.
930 */
931 debug("EOF received for stdin.");
932 packet_check_eom();
933 stdin_eof = 1;
934 return 0;
935}
936
937static int
938server_input_window_size(int type, u_int32_t seq, void *ctxt)
939{
940 u_int row = packet_get_int();
941 u_int col = packet_get_int();
942 u_int xpixel = packet_get_int();
943 u_int ypixel = packet_get_int();
944
945 debug("Window change received.");
946 packet_check_eom();
947 if (fdin != -1)
948 pty_change_window_size(fdin, row, col, xpixel, ypixel);
949 return 0;
950}
951
952static Channel * 433static Channel *
953server_request_direct_tcpip(void) 434server_request_direct_tcpip(void)
954{ 435{
@@ -1353,9 +834,9 @@ server_input_channel_req(int type, u_int32_t seq, void *ctxt)
1353} 834}
1354 835
1355static void 836static void
1356server_init_dispatch_20(void) 837server_init_dispatch(void)
1357{ 838{
1358 debug("server_init_dispatch_20"); 839 debug("server_init_dispatch");
1359 dispatch_init(&dispatch_protocol_error); 840 dispatch_init(&dispatch_protocol_error);
1360 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); 841 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1361 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); 842 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
@@ -1375,36 +856,3 @@ server_init_dispatch_20(void)
1375 /* rekeying */ 856 /* rekeying */
1376 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); 857 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
1377} 858}
1378static void
1379server_init_dispatch_13(void)
1380{
1381 debug("server_init_dispatch_13");
1382 dispatch_init(NULL);
1383 dispatch_set(SSH_CMSG_EOF, &server_input_eof);
1384 dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data);
1385 dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size);
1386 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1387 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1388 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
1389 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1390 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1391 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
1392}
1393static void
1394server_init_dispatch_15(void)
1395{
1396 server_init_dispatch_13();
1397 debug("server_init_dispatch_15");
1398 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1399 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose);
1400}
1401static void
1402server_init_dispatch(void)
1403{
1404 if (compat20)
1405 server_init_dispatch_20();
1406 else if (compat13)
1407 server_init_dispatch_13();
1408 else
1409 server_init_dispatch_15();
1410}