1 /* $NetBSD: serverloop.c,v 1.13 2016/01/14 22:30:04 christos Exp $ */ 2 /* $OpenBSD: serverloop.c,v 1.178 2015/02/20 22:17:21 djm Exp $ */ 3 /* 4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 6 * All rights reserved 7 * Server main loop for handling the interactive session. 8 * 9 * As far as I am concerned, the code I have written for this software 10 * can be used freely for any purpose. Any derived versions of this 11 * software must be clearly marked as such, and if the derived work is 12 * incompatible with the protocol description in the RFC file, it must be 13 * called by a name other than "ssh" or "Secure Shell". 14 * 15 * SSH2 support by Markus Friedl. 16 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 1. Redistributions of source code must retain the above copyright 22 * notice, this list of conditions and the following disclaimer. 23 * 2. Redistributions in binary form must reproduce the above copyright 24 * notice, this list of conditions and the following disclaimer in the 25 * documentation and/or other materials provided with the distribution. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include "includes.h" 40 __RCSID("$NetBSD: serverloop.c,v 1.13 2016/01/14 22:30:04 christos Exp $"); 41 #include <sys/param.h> /* MIN MAX */ 42 #include <sys/types.h> 43 #include <sys/wait.h> 44 #include <sys/socket.h> 45 #include <sys/time.h> 46 #include <sys/queue.h> 47 48 #include <netinet/in.h> 49 50 #include <errno.h> 51 #include <fcntl.h> 52 #include <pwd.h> 53 #include <signal.h> 54 #include <string.h> 55 #include <termios.h> 56 #include <unistd.h> 57 #include <stdarg.h> 58 59 #include "xmalloc.h" 60 #include "packet.h" 61 #include "buffer.h" 62 #include "log.h" 63 #include "misc.h" 64 #include "servconf.h" 65 #include "canohost.h" 66 #include "sshpty.h" 67 #include "channels.h" 68 #include "compat.h" 69 #include "ssh1.h" 70 #include "ssh2.h" 71 #include "key.h" 72 #include "cipher.h" 73 #include "kex.h" 74 #include "hostfile.h" 75 #include "auth.h" 76 #include "session.h" 77 #include "dispatch.h" 78 #include "auth-options.h" 79 #include "serverloop.h" 80 #include "ssherr.h" 81 82 extern ServerOptions options; 83 84 /* XXX */ 85 extern Authctxt *the_authctxt; 86 extern int use_privsep; 87 88 static Buffer stdin_buffer; /* Buffer for stdin data. */ 89 static Buffer stdout_buffer; /* Buffer for stdout data. */ 90 static Buffer stderr_buffer; /* Buffer for stderr data. */ 91 static int fdin; /* Descriptor for stdin (for writing) */ 92 static int fdout; /* Descriptor for stdout (for reading); 93 May be same number as fdin. */ 94 static int fderr; /* Descriptor for stderr. May be -1. */ 95 static u_long stdin_bytes = 0; /* Number of bytes written to stdin. */ 96 static u_long stdout_bytes = 0; /* Number of stdout bytes sent to client. */ 97 static u_long stderr_bytes = 0; /* Number of stderr bytes sent to client. */ 98 static u_long fdout_bytes = 0; /* Number of stdout bytes read from program. */ 99 static int stdin_eof = 0; /* EOF message received from client. */ 100 static int fdout_eof = 0; /* EOF encountered reading from fdout. */ 101 static int fderr_eof = 0; /* EOF encountered readung from fderr. */ 102 static int fdin_is_tty = 0; /* fdin points to a tty. */ 103 static int connection_in; /* Connection to client (input). */ 104 static int connection_out; /* Connection to client (output). */ 105 static int connection_closed = 0; /* Connection to client closed. */ 106 static u_int buffer_high; /* "Soft" max buffer size. */ 107 static int no_more_sessions = 0; /* Disallow further sessions. */ 108 109 /* 110 * This SIGCHLD kludge is used to detect when the child exits. The server 111 * will exit after that, as soon as forwarded connections have terminated. 112 */ 113 114 static volatile sig_atomic_t child_terminated = 0; /* The child has terminated. */ 115 116 /* Cleanup on signals (!use_privsep case only) */ 117 static volatile sig_atomic_t received_sigterm = 0; 118 119 /* prototypes */ 120 static void server_init_dispatch(void); 121 122 /* 123 * Returns current time in seconds from Jan 1, 1970 with the maximum 124 * available resolution. 125 */ 126 127 static double 128 get_current_time(void) 129 { 130 struct timeval tv; 131 gettimeofday(&tv, NULL); 132 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0; 133 } 134 135 /* 136 * we write to this pipe if a SIGCHLD is caught in order to avoid 137 * the race between select() and child_terminated 138 */ 139 static int notify_pipe[2]; 140 static void 141 notify_setup(void) 142 { 143 if (pipe(notify_pipe) < 0) { 144 error("pipe(notify_pipe) failed %s", strerror(errno)); 145 } else if ((fcntl(notify_pipe[0], F_SETFD, FD_CLOEXEC) == -1) || 146 (fcntl(notify_pipe[1], F_SETFD, FD_CLOEXEC) == -1)) { 147 error("fcntl(notify_pipe, F_SETFD) failed %s", strerror(errno)); 148 close(notify_pipe[0]); 149 close(notify_pipe[1]); 150 } else { 151 set_nonblock(notify_pipe[0]); 152 set_nonblock(notify_pipe[1]); 153 return; 154 } 155 notify_pipe[0] = -1; /* read end */ 156 notify_pipe[1] = -1; /* write end */ 157 } 158 static void 159 notify_parent(void) 160 { 161 if (notify_pipe[1] != -1) 162 (void)write(notify_pipe[1], "", 1); 163 } 164 static void 165 notify_prepare(fd_set *readset) 166 { 167 if (notify_pipe[0] != -1) 168 FD_SET(notify_pipe[0], readset); 169 } 170 static void 171 notify_done(fd_set *readset) 172 { 173 char c; 174 175 if (notify_pipe[0] != -1 && FD_ISSET(notify_pipe[0], readset)) 176 while (read(notify_pipe[0], &c, 1) != -1) 177 debug2("notify_done: reading"); 178 } 179 180 /*ARGSUSED*/ 181 static void 182 sigchld_handler(int sig) 183 { 184 int save_errno = errno; 185 child_terminated = 1; 186 signal(SIGCHLD, sigchld_handler); 187 notify_parent(); 188 errno = save_errno; 189 } 190 191 /*ARGSUSED*/ 192 static void 193 sigterm_handler(int sig) 194 { 195 received_sigterm = sig; 196 } 197 198 /* 199 * Make packets from buffered stderr data, and buffer it for sending 200 * to the client. 201 */ 202 static void 203 make_packets_from_stderr_data(void) 204 { 205 u_int len; 206 207 /* Send buffered stderr data to the client. */ 208 while (buffer_len(&stderr_buffer) > 0 && 209 packet_not_very_much_data_to_write()) { 210 len = buffer_len(&stderr_buffer); 211 if (packet_is_interactive()) { 212 if (len > 512) 213 len = 512; 214 } else { 215 /* Keep the packets at reasonable size. */ 216 if (len > packet_get_maxsize()) 217 len = packet_get_maxsize(); 218 } 219 packet_start(SSH_SMSG_STDERR_DATA); 220 packet_put_string(buffer_ptr(&stderr_buffer), len); 221 packet_send(); 222 buffer_consume(&stderr_buffer, len); 223 stderr_bytes += len; 224 } 225 } 226 227 /* 228 * Make packets from buffered stdout data, and buffer it for sending to the 229 * client. 230 */ 231 static void 232 make_packets_from_stdout_data(void) 233 { 234 u_int len; 235 236 /* Send buffered stdout data to the client. */ 237 while (buffer_len(&stdout_buffer) > 0 && 238 packet_not_very_much_data_to_write()) { 239 len = buffer_len(&stdout_buffer); 240 if (packet_is_interactive()) { 241 if (len > 512) 242 len = 512; 243 } else { 244 /* Keep the packets at reasonable size. */ 245 if (len > packet_get_maxsize()) 246 len = packet_get_maxsize(); 247 } 248 packet_start(SSH_SMSG_STDOUT_DATA); 249 packet_put_string(buffer_ptr(&stdout_buffer), len); 250 packet_send(); 251 buffer_consume(&stdout_buffer, len); 252 stdout_bytes += len; 253 } 254 } 255 256 static void 257 client_alive_check(void) 258 { 259 int channel_id; 260 261 /* timeout, check to see how many we have had */ 262 if (packet_inc_alive_timeouts() > options.client_alive_count_max) { 263 logit("Timeout, client not responding."); 264 cleanup_exit(255); 265 } 266 267 /* 268 * send a bogus global/channel request with "wantreply", 269 * we should get back a failure 270 */ 271 if ((channel_id = channel_find_open()) == -1) { 272 packet_start(SSH2_MSG_GLOBAL_REQUEST); 273 packet_put_cstring("keepalive@openssh.com"); 274 packet_put_char(1); /* boolean: want reply */ 275 } else { 276 channel_request_start(channel_id, "keepalive@openssh.com", 1); 277 } 278 packet_send(); 279 } 280 281 /* 282 * Sleep in select() until we can do something. This will initialize the 283 * select masks. Upon return, the masks will indicate which descriptors 284 * have data or can accept data. Optionally, a maximum time can be specified 285 * for the duration of the wait (0 = infinite). 286 */ 287 static void 288 wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, int *maxfdp, 289 u_int *nallocp, u_int64_t max_time_milliseconds) 290 { 291 struct timeval tv, *tvp; 292 int ret; 293 time_t minwait_secs = 0; 294 int client_alive_scheduled = 0; 295 296 /* Allocate and update select() masks for channel descriptors. */ 297 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, 298 &minwait_secs, 0); 299 300 if (minwait_secs != 0) 301 max_time_milliseconds = MIN(max_time_milliseconds, 302 (u_int)minwait_secs * 1000); 303 304 /* 305 * if using client_alive, set the max timeout accordingly, 306 * and indicate that this particular timeout was for client 307 * alive by setting the client_alive_scheduled flag. 308 * 309 * this could be randomized somewhat to make traffic 310 * analysis more difficult, but we're not doing it yet. 311 */ 312 if (compat20 && 313 max_time_milliseconds == 0 && options.client_alive_interval) { 314 client_alive_scheduled = 1; 315 max_time_milliseconds = 316 (u_int64_t)options.client_alive_interval * 1000; 317 } 318 319 if (compat20) { 320 #if 0 321 /* wrong: bad condition XXX */ 322 if (channel_not_very_much_buffered_data()) 323 #endif 324 FD_SET(connection_in, *readsetp); 325 } else { 326 /* 327 * Read packets from the client unless we have too much 328 * buffered stdin or channel data. 329 */ 330 if (buffer_len(&stdin_buffer) < buffer_high && 331 channel_not_very_much_buffered_data()) 332 FD_SET(connection_in, *readsetp); 333 /* 334 * If there is not too much data already buffered going to 335 * the client, try to get some more data from the program. 336 */ 337 if (packet_not_very_much_data_to_write()) { 338 if (!fdout_eof) 339 FD_SET(fdout, *readsetp); 340 if (!fderr_eof) 341 FD_SET(fderr, *readsetp); 342 } 343 /* 344 * If we have buffered data, try to write some of that data 345 * to the program. 346 */ 347 if (fdin != -1 && buffer_len(&stdin_buffer) > 0) 348 FD_SET(fdin, *writesetp); 349 } 350 notify_prepare(*readsetp); 351 352 /* 353 * If we have buffered packet data going to the client, mark that 354 * descriptor. 355 */ 356 if (packet_have_data_to_write()) 357 FD_SET(connection_out, *writesetp); 358 359 /* 360 * If child has terminated and there is enough buffer space to read 361 * from it, then read as much as is available and exit. 362 */ 363 if (child_terminated && packet_not_very_much_data_to_write()) 364 if (max_time_milliseconds == 0 || client_alive_scheduled) 365 max_time_milliseconds = 100; 366 367 if (max_time_milliseconds == 0) 368 tvp = NULL; 369 else { 370 tv.tv_sec = max_time_milliseconds / 1000; 371 tv.tv_usec = 1000 * (max_time_milliseconds % 1000); 372 tvp = &tv; 373 } 374 375 /* Wait for something to happen, or the timeout to expire. */ 376 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); 377 378 if (ret == -1) { 379 memset(*readsetp, 0, *nallocp); 380 memset(*writesetp, 0, *nallocp); 381 if (errno != EINTR) 382 error("select: %.100s", strerror(errno)); 383 } else if (ret == 0 && client_alive_scheduled) 384 client_alive_check(); 385 386 notify_done(*readsetp); 387 } 388 389 /* 390 * Processes input from the client and the program. Input data is stored 391 * in buffers and processed later. 392 */ 393 static void 394 process_input(fd_set *readset) 395 { 396 int len; 397 char buf[16384]; 398 399 /* Read and buffer any input data from the client. */ 400 if (FD_ISSET(connection_in, readset)) { 401 len = read(connection_in, buf, sizeof(buf)); 402 if (len == 0) { 403 verbose("Connection closed by %.100s", 404 get_remote_ipaddr()); 405 connection_closed = 1; 406 if (compat20) 407 return; 408 cleanup_exit(255); 409 } else if (len < 0) { 410 if (errno != EINTR && errno != EAGAIN) { 411 verbose("Read error from remote host " 412 "%.100s: %.100s", 413 get_remote_ipaddr(), strerror(errno)); 414 cleanup_exit(255); 415 } 416 } else { 417 /* Buffer any received data. */ 418 packet_process_incoming(buf, len); 419 fdout_bytes += len; 420 } 421 } 422 if (compat20) 423 return; 424 425 /* Read and buffer any available stdout data from the program. */ 426 if (!fdout_eof && FD_ISSET(fdout, readset)) { 427 len = read(fdout, buf, sizeof(buf)); 428 if (len < 0 && (errno == EINTR || errno == EAGAIN)) { 429 /* do nothing */ 430 } else if (len <= 0) { 431 fdout_eof = 1; 432 } else { 433 buffer_append(&stdout_buffer, buf, len); 434 debug ("FD out now: %ld", fdout_bytes); 435 fdout_bytes += len; 436 } 437 } 438 /* Read and buffer any available stderr data from the program. */ 439 if (!fderr_eof && FD_ISSET(fderr, readset)) { 440 len = read(fderr, buf, sizeof(buf)); 441 if (len < 0 && (errno == EINTR || errno == EAGAIN)) { 442 /* do nothing */ 443 } else if (len <= 0) { 444 fderr_eof = 1; 445 } else { 446 buffer_append(&stderr_buffer, buf, len); 447 } 448 } 449 } 450 451 /* 452 * Sends data from internal buffers to client program stdin. 453 */ 454 static void 455 process_output(fd_set *writeset) 456 { 457 struct termios tio; 458 u_char *data; 459 u_int dlen; 460 int len; 461 462 /* Write buffered data to program stdin. */ 463 if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) { 464 data = buffer_ptr(&stdin_buffer); 465 dlen = buffer_len(&stdin_buffer); 466 len = write(fdin, data, dlen); 467 if (len < 0 && (errno == EINTR || errno == EAGAIN)) { 468 /* do nothing */ 469 } else if (len <= 0) { 470 if (fdin != fdout) 471 close(fdin); 472 else 473 shutdown(fdin, SHUT_WR); /* We will no longer send. */ 474 fdin = -1; 475 } else { 476 /* Successful write. */ 477 if (fdin_is_tty && dlen >= 1 && data[0] != '\r' && 478 tcgetattr(fdin, &tio) == 0 && 479 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) { 480 /* 481 * Simulate echo to reduce the impact of 482 * traffic analysis 483 */ 484 packet_send_ignore(len); 485 packet_send(); 486 } 487 /* Consume the data from the buffer. */ 488 buffer_consume(&stdin_buffer, len); 489 /* Update the count of bytes written to the program. */ 490 stdin_bytes += len; 491 } 492 } 493 /* Send any buffered packet data to the client. */ 494 if (FD_ISSET(connection_out, writeset)) 495 stdin_bytes += packet_write_poll(); 496 } 497 498 /* 499 * Wait until all buffered output has been sent to the client. 500 * This is used when the program terminates. 501 */ 502 static void 503 drain_output(void) 504 { 505 /* Send any buffered stdout data to the client. */ 506 if (buffer_len(&stdout_buffer) > 0) { 507 packet_start(SSH_SMSG_STDOUT_DATA); 508 packet_put_string(buffer_ptr(&stdout_buffer), 509 buffer_len(&stdout_buffer)); 510 packet_send(); 511 /* Update the count of sent bytes. */ 512 stdout_bytes += buffer_len(&stdout_buffer); 513 } 514 /* Send any buffered stderr data to the client. */ 515 if (buffer_len(&stderr_buffer) > 0) { 516 packet_start(SSH_SMSG_STDERR_DATA); 517 packet_put_string(buffer_ptr(&stderr_buffer), 518 buffer_len(&stderr_buffer)); 519 packet_send(); 520 /* Update the count of sent bytes. */ 521 stderr_bytes += buffer_len(&stderr_buffer); 522 } 523 /* Wait until all buffered data has been written to the client. */ 524 packet_write_wait(); 525 } 526 527 static void 528 process_buffered_input_packets(void) 529 { 530 dispatch_run(DISPATCH_NONBLOCK, NULL, active_state); 531 } 532 533 /* 534 * Performs the interactive session. This handles data transmission between 535 * the client and the program. Note that the notion of stdin, stdout, and 536 * stderr in this function is sort of reversed: this function writes to 537 * stdin (of the child program), and reads from stdout and stderr (of the 538 * child program). 539 */ 540 void 541 server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg) 542 { 543 fd_set *readset = NULL, *writeset = NULL; 544 int max_fd = 0; 545 u_int nalloc = 0; 546 int wait_status; /* Status returned by wait(). */ 547 pid_t wait_pid; /* pid returned by wait(). */ 548 int waiting_termination = 0; /* Have displayed waiting close message. */ 549 u_int64_t max_time_milliseconds; 550 u_int previous_stdout_buffer_bytes; 551 u_int stdout_buffer_bytes; 552 int type; 553 554 debug("Entering interactive session."); 555 556 /* Initialize the SIGCHLD kludge. */ 557 child_terminated = 0; 558 signal(SIGCHLD, sigchld_handler); 559 560 if (!use_privsep) { 561 signal(SIGTERM, sigterm_handler); 562 signal(SIGINT, sigterm_handler); 563 signal(SIGQUIT, sigterm_handler); 564 } 565 566 /* Initialize our global variables. */ 567 fdin = fdin_arg; 568 fdout = fdout_arg; 569 fderr = fderr_arg; 570 571 /* nonblocking IO */ 572 set_nonblock(fdin); 573 set_nonblock(fdout); 574 /* we don't have stderr for interactive terminal sessions, see below */ 575 if (fderr != -1) 576 set_nonblock(fderr); 577 578 if (!(datafellows & SSH_BUG_IGNOREMSG) && isatty(fdin)) 579 fdin_is_tty = 1; 580 581 connection_in = packet_get_connection_in(); 582 connection_out = packet_get_connection_out(); 583 584 notify_setup(); 585 586 previous_stdout_buffer_bytes = 0; 587 588 /* Set approximate I/O buffer size. */ 589 if (packet_is_interactive()) 590 buffer_high = 4096; 591 else 592 buffer_high = 64 * 1024; 593 594 #if 0 595 /* Initialize max_fd to the maximum of the known file descriptors. */ 596 max_fd = MAX(connection_in, connection_out); 597 max_fd = MAX(max_fd, fdin); 598 max_fd = MAX(max_fd, fdout); 599 if (fderr != -1) 600 max_fd = MAX(max_fd, fderr); 601 #endif 602 603 /* Initialize Initialize buffers. */ 604 buffer_init(&stdin_buffer); 605 buffer_init(&stdout_buffer); 606 buffer_init(&stderr_buffer); 607 608 /* 609 * If we have no separate fderr (which is the case when we have a pty 610 * - there we cannot make difference between data sent to stdout and 611 * stderr), indicate that we have seen an EOF from stderr. This way 612 * we don't need to check the descriptor everywhere. 613 */ 614 if (fderr == -1) 615 fderr_eof = 1; 616 617 server_init_dispatch(); 618 619 /* Main loop of the server for the interactive session mode. */ 620 for (;;) { 621 622 /* Process buffered packets from the client. */ 623 process_buffered_input_packets(); 624 625 /* 626 * If we have received eof, and there is no more pending 627 * input data, cause a real eof by closing fdin. 628 */ 629 if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) { 630 if (fdin != fdout) 631 close(fdin); 632 else 633 shutdown(fdin, SHUT_WR); /* We will no longer send. */ 634 fdin = -1; 635 } 636 /* Make packets from buffered stderr data to send to the client. */ 637 make_packets_from_stderr_data(); 638 639 /* 640 * Make packets from buffered stdout data to send to the 641 * client. If there is very little to send, this arranges to 642 * not send them now, but to wait a short while to see if we 643 * are getting more data. This is necessary, as some systems 644 * wake up readers from a pty after each separate character. 645 */ 646 max_time_milliseconds = 0; 647 stdout_buffer_bytes = buffer_len(&stdout_buffer); 648 if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 && 649 stdout_buffer_bytes != previous_stdout_buffer_bytes) { 650 /* try again after a while */ 651 max_time_milliseconds = 10; 652 } else { 653 /* Send it now. */ 654 make_packets_from_stdout_data(); 655 } 656 previous_stdout_buffer_bytes = buffer_len(&stdout_buffer); 657 658 /* Send channel data to the client. */ 659 if (packet_not_very_much_data_to_write()) 660 channel_output_poll(); 661 662 /* 663 * Bail out of the loop if the program has closed its output 664 * descriptors, and we have no more data to send to the 665 * client, and there is no pending buffered data. 666 */ 667 if (fdout_eof && fderr_eof && !packet_have_data_to_write() && 668 buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) { 669 if (!channel_still_open()) 670 break; 671 if (!waiting_termination) { 672 const char *s = "Waiting for forwarded connections to terminate...\r\n"; 673 char *cp; 674 waiting_termination = 1; 675 buffer_append(&stderr_buffer, s, strlen(s)); 676 677 /* Display list of open channels. */ 678 cp = channel_open_message(); 679 buffer_append(&stderr_buffer, cp, strlen(cp)); 680 free(cp); 681 } 682 } 683 max_fd = MAX(connection_in, connection_out); 684 max_fd = MAX(max_fd, fdin); 685 max_fd = MAX(max_fd, fdout); 686 max_fd = MAX(max_fd, fderr); 687 max_fd = MAX(max_fd, notify_pipe[0]); 688 689 /* Sleep in select() until we can do something. */ 690 wait_until_can_do_something(&readset, &writeset, &max_fd, 691 &nalloc, max_time_milliseconds); 692 693 if (received_sigterm) { 694 logit("Exiting on signal %d", (int)received_sigterm); 695 /* Clean up sessions, utmp, etc. */ 696 cleanup_exit(255); 697 } 698 699 /* Process any channel events. */ 700 channel_after_select(readset, writeset); 701 702 /* Process input from the client and from program stdout/stderr. */ 703 process_input(readset); 704 705 /* Process output to the client and to program stdin. */ 706 process_output(writeset); 707 } 708 free(readset); 709 free(writeset); 710 711 /* Cleanup and termination code. */ 712 713 /* Wait until all output has been sent to the client. */ 714 drain_output(); 715 716 debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.", 717 stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes); 718 719 /* Free and clear the buffers. */ 720 buffer_free(&stdin_buffer); 721 buffer_free(&stdout_buffer); 722 buffer_free(&stderr_buffer); 723 724 /* Close the file descriptors. */ 725 if (fdout != -1) 726 close(fdout); 727 fdout = -1; 728 fdout_eof = 1; 729 if (fderr != -1) 730 close(fderr); 731 fderr = -1; 732 fderr_eof = 1; 733 if (fdin != -1) 734 close(fdin); 735 fdin = -1; 736 737 channel_free_all(); 738 739 /* We no longer want our SIGCHLD handler to be called. */ 740 signal(SIGCHLD, SIG_DFL); 741 742 while ((wait_pid = waitpid(-1, &wait_status, 0)) < 0) 743 if (errno != EINTR) 744 packet_disconnect("wait: %.100s", strerror(errno)); 745 if (wait_pid != pid) 746 error("Strange, wait returned pid %ld, expected %ld", 747 (long)wait_pid, (long)pid); 748 749 /* Check if it exited normally. */ 750 if (WIFEXITED(wait_status)) { 751 /* Yes, normal exit. Get exit status and send it to the client. */ 752 debug("Command exited with status %d.", WEXITSTATUS(wait_status)); 753 packet_start(SSH_SMSG_EXITSTATUS); 754 packet_put_int(WEXITSTATUS(wait_status)); 755 packet_send(); 756 packet_write_wait(); 757 758 /* 759 * Wait for exit confirmation. Note that there might be 760 * other packets coming before it; however, the program has 761 * already died so we just ignore them. The client is 762 * supposed to respond with the confirmation when it receives 763 * the exit status. 764 */ 765 do { 766 type = packet_read(); 767 } 768 while (type != SSH_CMSG_EXIT_CONFIRMATION); 769 770 debug("Received exit confirmation."); 771 return; 772 } 773 /* Check if the program terminated due to a signal. */ 774 if (WIFSIGNALED(wait_status)) 775 packet_disconnect("Command terminated on signal %d.", 776 WTERMSIG(wait_status)); 777 778 /* Some weird exit cause. Just exit. */ 779 packet_disconnect("wait returned status %04x.", wait_status); 780 /* NOTREACHED */ 781 } 782 783 static void 784 collect_children(void) 785 { 786 pid_t pid; 787 sigset_t oset, nset; 788 int status; 789 790 /* block SIGCHLD while we check for dead children */ 791 sigemptyset(&nset); 792 sigaddset(&nset, SIGCHLD); 793 sigprocmask(SIG_BLOCK, &nset, &oset); 794 if (child_terminated) { 795 debug("Received SIGCHLD."); 796 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || 797 (pid < 0 && errno == EINTR)) 798 if (pid > 0) 799 session_close_by_pid(pid, status); 800 child_terminated = 0; 801 } 802 sigprocmask(SIG_SETMASK, &oset, NULL); 803 } 804 805 void 806 server_loop2(Authctxt *authctxt) 807 { 808 fd_set *readset = NULL, *writeset = NULL; 809 int rekeying = 0, max_fd; 810 u_int nalloc = 0; 811 u_int64_t rekey_timeout_ms = 0; 812 double start_time, total_time; 813 814 debug("Entering interactive session for SSH2."); 815 start_time = get_current_time(); 816 817 signal(SIGCHLD, sigchld_handler); 818 child_terminated = 0; 819 connection_in = packet_get_connection_in(); 820 connection_out = packet_get_connection_out(); 821 822 if (!use_privsep) { 823 signal(SIGTERM, sigterm_handler); 824 signal(SIGINT, sigterm_handler); 825 signal(SIGQUIT, sigterm_handler); 826 } 827 828 notify_setup(); 829 830 max_fd = MAX(connection_in, connection_out); 831 max_fd = MAX(max_fd, notify_pipe[0]); 832 833 server_init_dispatch(); 834 835 for (;;) { 836 process_buffered_input_packets(); 837 838 rekeying = (active_state->kex != NULL && !active_state->kex->done); 839 840 if (!rekeying && packet_not_very_much_data_to_write()) 841 channel_output_poll(); 842 if (options.rekey_interval > 0 && compat20 && !rekeying) 843 rekey_timeout_ms = packet_get_rekey_timeout() * 1000; 844 else 845 rekey_timeout_ms = 0; 846 847 wait_until_can_do_something(&readset, &writeset, &max_fd, 848 &nalloc, rekey_timeout_ms); 849 850 if (received_sigterm) { 851 logit("Exiting on signal %d", (int)received_sigterm); 852 /* Clean up sessions, utmp, etc. */ 853 cleanup_exit(255); 854 } 855 856 collect_children(); 857 if (!rekeying) { 858 channel_after_select(readset, writeset); 859 if (packet_need_rekeying()) { 860 int r; 861 debug("need rekeying"); 862 if (active_state->kex) 863 active_state->kex->done = 0; 864 if ((r = kex_send_kexinit(active_state)) != 0) 865 logit("%s: kex_send_kexinit: %s", 866 __func__, ssh_err(r)); 867 } 868 } 869 process_input(readset); 870 if (connection_closed) 871 break; 872 process_output(writeset); 873 } 874 collect_children(); 875 876 free(readset); 877 free(writeset); 878 879 /* free all channels, no more reads and writes */ 880 channel_free_all(); 881 882 /* free remaining sessions, e.g. remove wtmp entries */ 883 session_destroy_all(NULL); 884 total_time = get_current_time() - start_time; 885 logit("SSH: Server;LType: Throughput;Remote: %s-%d;IN: %lu;OUT: %lu;Duration: %.1f;tPut_in: %.1f;tPut_out: %.1f", 886 get_remote_ipaddr(), get_remote_port(), 887 stdin_bytes, fdout_bytes, total_time, stdin_bytes / total_time, 888 fdout_bytes / total_time); 889 } 890 891 static int 892 server_input_keep_alive(int type, u_int32_t seq, void *ctxt) 893 { 894 debug("Got %d/%u for keepalive", type, seq); 895 /* 896 * reset timeout, since we got a sane answer from the client. 897 * even if this was generated by something other than 898 * the bogus CHANNEL_REQUEST we send for keepalives. 899 */ 900 packet_set_alive_timeouts(0); 901 return 0; 902 } 903 904 static int 905 server_input_stdin_data(int type, u_int32_t seq, void *ctxt) 906 { 907 char *data; 908 u_int data_len; 909 910 /* Stdin data from the client. Append it to the buffer. */ 911 /* Ignore any data if the client has closed stdin. */ 912 if (fdin == -1) 913 return 0; 914 data = packet_get_string(&data_len); 915 packet_check_eom(); 916 buffer_append(&stdin_buffer, data, data_len); 917 explicit_bzero(data, data_len); 918 free(data); 919 return 0; 920 } 921 922 static int 923 server_input_eof(int type, u_int32_t seq, void *ctxt) 924 { 925 /* 926 * Eof from the client. The stdin descriptor to the 927 * program will be closed when all buffered data has 928 * drained. 929 */ 930 debug("EOF received for stdin."); 931 packet_check_eom(); 932 stdin_eof = 1; 933 return 0; 934 } 935 936 static int 937 server_input_window_size(int type, u_int32_t seq, void *ctxt) 938 { 939 u_int row = packet_get_int(); 940 u_int col = packet_get_int(); 941 u_int xpixel = packet_get_int(); 942 u_int ypixel = packet_get_int(); 943 944 debug("Window change received."); 945 packet_check_eom(); 946 if (fdin != -1) 947 pty_change_window_size(fdin, row, col, xpixel, ypixel); 948 return 0; 949 } 950 951 static Channel * 952 server_request_direct_tcpip(void) 953 { 954 Channel *c = NULL; 955 char *target, *originator; 956 u_short target_port, originator_port; 957 958 target = packet_get_string(NULL); 959 target_port = packet_get_int(); 960 originator = packet_get_string(NULL); 961 originator_port = packet_get_int(); 962 packet_check_eom(); 963 964 debug("server_request_direct_tcpip: originator %s port %d, target %s " 965 "port %d", originator, originator_port, target, target_port); 966 967 /* XXX fine grained permissions */ 968 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 && 969 !no_port_forwarding_flag) { 970 c = channel_connect_to_port(target, target_port, 971 "direct-tcpip", "direct-tcpip"); 972 } else { 973 logit("refused local port forward: " 974 "originator %s port %d, target %s port %d", 975 originator, originator_port, target, target_port); 976 } 977 978 free(originator); 979 free(target); 980 981 return c; 982 } 983 984 static Channel * 985 server_request_direct_streamlocal(void) 986 { 987 Channel *c = NULL; 988 char *target, *originator; 989 u_short originator_port; 990 991 target = packet_get_string(NULL); 992 originator = packet_get_string(NULL); 993 originator_port = packet_get_int(); 994 packet_check_eom(); 995 996 debug("server_request_direct_streamlocal: originator %s port %d, target %s", 997 originator, originator_port, target); 998 999 /* XXX fine grained permissions */ 1000 if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 && 1001 !no_port_forwarding_flag) { 1002 c = channel_connect_to_path(target, 1003 "direct-streamlocal@openssh.com", "direct-streamlocal"); 1004 } else { 1005 logit("refused streamlocal port forward: " 1006 "originator %s port %d, target %s", 1007 originator, originator_port, target); 1008 } 1009 1010 free(originator); 1011 free(target); 1012 1013 return c; 1014 } 1015 1016 static Channel * 1017 server_request_tun(void) 1018 { 1019 Channel *c = NULL; 1020 int mode, tun; 1021 int sock; 1022 1023 mode = packet_get_int(); 1024 switch (mode) { 1025 case SSH_TUNMODE_POINTOPOINT: 1026 case SSH_TUNMODE_ETHERNET: 1027 break; 1028 default: 1029 packet_send_debug("Unsupported tunnel device mode."); 1030 return NULL; 1031 } 1032 if ((options.permit_tun & mode) == 0) { 1033 packet_send_debug("Server has rejected tunnel device " 1034 "forwarding"); 1035 return NULL; 1036 } 1037 1038 tun = packet_get_int(); 1039 if (forced_tun_device != -1) { 1040 if (tun != SSH_TUNID_ANY && forced_tun_device != tun) 1041 goto done; 1042 tun = forced_tun_device; 1043 } 1044 sock = tun_open(tun, mode); 1045 if (sock < 0) 1046 goto done; 1047 if (options.hpn_disabled) 1048 c = channel_new("tun", SSH_CHANNEL_OPEN, sock, sock, -1, 1049 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); 1050 else 1051 c = channel_new("tun", SSH_CHANNEL_OPEN, sock, sock, -1, 1052 options.hpn_buffer_size, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); 1053 c->datagram = 1; 1054 1055 done: 1056 if (c == NULL) 1057 packet_send_debug("Failed to open the tunnel device."); 1058 return c; 1059 } 1060 1061 static Channel * 1062 server_request_session(void) 1063 { 1064 Channel *c; 1065 1066 debug("input_session_request"); 1067 packet_check_eom(); 1068 1069 if (no_more_sessions) { 1070 packet_disconnect("Possible attack: attempt to open a session " 1071 "after additional sessions disabled"); 1072 } 1073 1074 /* 1075 * A server session has no fd to read or write until a 1076 * CHANNEL_REQUEST for a shell is made, so we set the type to 1077 * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all 1078 * CHANNEL_REQUEST messages is registered. 1079 */ 1080 c = channel_new("session", SSH_CHANNEL_LARVAL, 1081 -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT, 1082 0, "server-session", 1); 1083 if ((options.tcp_rcv_buf_poll > 0) && (!options.hpn_disabled)) 1084 c->dynamic_window = 1; 1085 if (session_open(the_authctxt, c->self) != 1) { 1086 debug("session open failed, free channel %d", c->self); 1087 channel_free(c); 1088 return NULL; 1089 } 1090 channel_register_cleanup(c->self, session_close_by_channel, 0); 1091 return c; 1092 } 1093 1094 static int 1095 server_input_channel_open(int type, u_int32_t seq, void *ctxt) 1096 { 1097 Channel *c = NULL; 1098 char *ctype; 1099 int rchan; 1100 u_int rmaxpack, rwindow, len; 1101 1102 ctype = packet_get_string(&len); 1103 rchan = packet_get_int(); 1104 rwindow = packet_get_int(); 1105 rmaxpack = packet_get_int(); 1106 1107 debug("server_input_channel_open: ctype %s rchan %d win %d max %d", 1108 ctype, rchan, rwindow, rmaxpack); 1109 1110 if (strcmp(ctype, "session") == 0) { 1111 c = server_request_session(); 1112 } else if (strcmp(ctype, "direct-tcpip") == 0) { 1113 c = server_request_direct_tcpip(); 1114 } else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) { 1115 c = server_request_direct_streamlocal(); 1116 } else if (strcmp(ctype, "tun@openssh.com") == 0) { 1117 c = server_request_tun(); 1118 } 1119 if (c != NULL) { 1120 debug("server_input_channel_open: confirm %s", ctype); 1121 c->remote_id = rchan; 1122 c->remote_window = rwindow; 1123 c->remote_maxpacket = rmaxpack; 1124 if (c->type != SSH_CHANNEL_CONNECTING) { 1125 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); 1126 packet_put_int(c->remote_id); 1127 packet_put_int(c->self); 1128 packet_put_int(c->local_window); 1129 packet_put_int(c->local_maxpacket); 1130 packet_send(); 1131 } 1132 } else { 1133 debug("server_input_channel_open: failure %s", ctype); 1134 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); 1135 packet_put_int(rchan); 1136 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); 1137 if (!(datafellows & SSH_BUG_OPENFAILURE)) { 1138 packet_put_cstring("open failed"); 1139 packet_put_cstring(""); 1140 } 1141 packet_send(); 1142 } 1143 free(ctype); 1144 return 0; 1145 } 1146 1147 static int 1148 server_input_hostkeys_prove(struct sshbuf **respp) 1149 { 1150 struct ssh *ssh = active_state; /* XXX */ 1151 struct sshbuf *resp = NULL; 1152 struct sshbuf *sigbuf = NULL; 1153 struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL; 1154 int r, ndx, success = 0; 1155 const u_char *blob; 1156 u_char *sig = 0; 1157 size_t blen, slen; 1158 1159 if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL) 1160 fatal("%s: sshbuf_new", __func__); 1161 1162 while (ssh_packet_remaining(ssh) > 0) { 1163 sshkey_free(key); 1164 key = NULL; 1165 if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 || 1166 (r = sshkey_from_blob(blob, blen, &key)) != 0) { 1167 error("%s: couldn't parse key: %s", 1168 __func__, ssh_err(r)); 1169 goto out; 1170 } 1171 /* 1172 * Better check that this is actually one of our hostkeys 1173 * before attempting to sign anything with it. 1174 */ 1175 if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) { 1176 error("%s: unknown host %s key", 1177 __func__, sshkey_type(key)); 1178 goto out; 1179 } 1180 /* 1181 * XXX refactor: make kex->sign just use an index rather 1182 * than passing in public and private keys 1183 */ 1184 if ((key_prv = get_hostkey_by_index(ndx)) == NULL && 1185 (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) { 1186 error("%s: can't retrieve hostkey %d", __func__, ndx); 1187 goto out; 1188 } 1189 sshbuf_reset(sigbuf); 1190 free(sig); 1191 sig = NULL; 1192 if ((r = sshbuf_put_cstring(sigbuf, 1193 "hostkeys-prove-00@openssh.com")) != 0 || 1194 (r = sshbuf_put_string(sigbuf, 1195 ssh->kex->session_id, ssh->kex->session_id_len)) != 0 || 1196 (r = sshkey_puts(key, sigbuf)) != 0 || 1197 (r = ssh->kex->sign(key_prv, key_pub, &sig, &slen, 1198 sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), 0)) != 0 || 1199 (r = sshbuf_put_string(resp, sig, slen)) != 0) { 1200 error("%s: couldn't prepare signature: %s", 1201 __func__, ssh_err(r)); 1202 goto out; 1203 } 1204 } 1205 /* Success */ 1206 *respp = resp; 1207 resp = NULL; /* don't free it */ 1208 success = 1; 1209 out: 1210 free(sig); 1211 sshbuf_free(resp); 1212 sshbuf_free(sigbuf); 1213 sshkey_free(key); 1214 return success; 1215 } 1216 1217 static int 1218 server_input_global_request(int type, u_int32_t seq, void *ctxt) 1219 { 1220 char *rtype; 1221 int want_reply; 1222 int r, success = 0, allocated_listen_port = 0; 1223 struct sshbuf *resp = NULL; 1224 1225 rtype = packet_get_string(NULL); 1226 want_reply = packet_get_char(); 1227 debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply); 1228 1229 /* -R style forwarding */ 1230 if (strcmp(rtype, "tcpip-forward") == 0) { 1231 struct passwd *pw; 1232 struct Forward fwd; 1233 1234 pw = the_authctxt->pw; 1235 if (pw == NULL || !the_authctxt->valid) 1236 fatal("server_input_global_request: no/invalid user"); 1237 memset(&fwd, 0, sizeof(fwd)); 1238 fwd.listen_host = packet_get_string(NULL); 1239 fwd.listen_port = (u_short)packet_get_int(); 1240 debug("server_input_global_request: tcpip-forward listen %s port %d", 1241 fwd.listen_host, fwd.listen_port); 1242 1243 /* check permissions */ 1244 if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 || 1245 no_port_forwarding_flag || 1246 (!want_reply && fwd.listen_port == 0) || 1247 (fwd.listen_port != 0 && fwd.listen_port < IPPORT_RESERVED && 1248 pw->pw_uid != 0)) { 1249 success = 0; 1250 packet_send_debug("Server has disabled port forwarding."); 1251 } else { 1252 /* Start listening on the port */ 1253 success = channel_setup_remote_fwd_listener(&fwd, 1254 &allocated_listen_port, &options.fwd_opts); 1255 } 1256 free(fwd.listen_host); 1257 if ((resp = sshbuf_new()) == NULL) 1258 fatal("%s: sshbuf_new", __func__); 1259 if ((r = sshbuf_put_u32(resp, allocated_listen_port)) != 0) 1260 fatal("%s: sshbuf_put_u32: %s", __func__, ssh_err(r)); 1261 } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) { 1262 struct Forward fwd; 1263 1264 memset(&fwd, 0, sizeof(fwd)); 1265 fwd.listen_host = packet_get_string(NULL); 1266 fwd.listen_port = (u_short)packet_get_int(); 1267 debug("%s: cancel-tcpip-forward addr %s port %d", __func__, 1268 fwd.listen_host, fwd.listen_port); 1269 1270 success = channel_cancel_rport_listener(&fwd); 1271 free(fwd.listen_host); 1272 } else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) { 1273 struct Forward fwd; 1274 1275 memset(&fwd, 0, sizeof(fwd)); 1276 fwd.listen_path = packet_get_string(NULL); 1277 debug("server_input_global_request: streamlocal-forward listen path %s", 1278 fwd.listen_path); 1279 1280 /* check permissions */ 1281 if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0 1282 || no_port_forwarding_flag) { 1283 success = 0; 1284 packet_send_debug("Server has disabled port forwarding."); 1285 } else { 1286 /* Start listening on the socket */ 1287 success = channel_setup_remote_fwd_listener( 1288 &fwd, NULL, &options.fwd_opts); 1289 } 1290 free(fwd.listen_path); 1291 } else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) { 1292 struct Forward fwd; 1293 1294 memset(&fwd, 0, sizeof(fwd)); 1295 fwd.listen_path = packet_get_string(NULL); 1296 debug("%s: cancel-streamlocal-forward path %s", __func__, 1297 fwd.listen_path); 1298 1299 success = channel_cancel_rport_listener(&fwd); 1300 free(fwd.listen_path); 1301 } else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) { 1302 no_more_sessions = 1; 1303 success = 1; 1304 } else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) { 1305 success = server_input_hostkeys_prove(&resp); 1306 } 1307 if (want_reply) { 1308 packet_start(success ? 1309 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); 1310 if (success && resp != NULL) 1311 ssh_packet_put_raw(active_state, sshbuf_ptr(resp), 1312 sshbuf_len(resp)); 1313 packet_send(); 1314 packet_write_wait(); 1315 } 1316 free(rtype); 1317 sshbuf_free(resp); 1318 return 0; 1319 } 1320 1321 static int 1322 server_input_channel_req(int type, u_int32_t seq, void *ctxt) 1323 { 1324 Channel *c; 1325 int id, reply, success = 0; 1326 char *rtype; 1327 1328 id = packet_get_int(); 1329 rtype = packet_get_string(NULL); 1330 reply = packet_get_char(); 1331 1332 debug("server_input_channel_req: channel %d request %s reply %d", 1333 id, rtype, reply); 1334 1335 if ((c = channel_lookup(id)) == NULL) 1336 packet_disconnect("server_input_channel_req: " 1337 "unknown channel %d", id); 1338 if (!strcmp(rtype, "eow@openssh.com")) { 1339 packet_check_eom(); 1340 chan_rcvd_eow(c); 1341 } else if ((c->type == SSH_CHANNEL_LARVAL || 1342 c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0) 1343 success = session_input_channel_req(c, rtype); 1344 if (reply && !(c->flags & CHAN_CLOSE_SENT)) { 1345 packet_start(success ? 1346 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); 1347 packet_put_int(c->remote_id); 1348 packet_send(); 1349 } 1350 free(rtype); 1351 return 0; 1352 } 1353 1354 static void 1355 server_init_dispatch_20(void) 1356 { 1357 debug("server_init_dispatch_20"); 1358 dispatch_init(&dispatch_protocol_error); 1359 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); 1360 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); 1361 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); 1362 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); 1363 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open); 1364 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); 1365 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); 1366 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req); 1367 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); 1368 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request); 1369 /* client_alive */ 1370 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive); 1371 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive); 1372 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive); 1373 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive); 1374 /* rekeying */ 1375 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); 1376 } 1377 static void 1378 server_init_dispatch_13(void) 1379 { 1380 debug("server_init_dispatch_13"); 1381 dispatch_init(NULL); 1382 dispatch_set(SSH_CMSG_EOF, &server_input_eof); 1383 dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data); 1384 dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size); 1385 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close); 1386 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation); 1387 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data); 1388 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); 1389 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); 1390 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open); 1391 } 1392 static void 1393 server_init_dispatch_15(void) 1394 { 1395 server_init_dispatch_13(); 1396 debug("server_init_dispatch_15"); 1397 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof); 1398 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose); 1399 } 1400 static void 1401 server_init_dispatch(void) 1402 { 1403 if (compat20) 1404 server_init_dispatch_20(); 1405 else if (compat13) 1406 server_init_dispatch_13(); 1407 else 1408 server_init_dispatch_15(); 1409 } 1410