1 /* $NetBSD: serverloop.c,v 1.15 2016/08/02 13:45:12 christos Exp $ */ 2 /* $OpenBSD: serverloop.c,v 1.184 2016/03/07 19:02:43 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.15 2016/08/02 13:45:12 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_ms) 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 /* XXX need proper deadline system for rekey/client alive */ 301 if (minwait_secs != 0) 302 max_time_ms = MIN(max_time_ms, (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 && options.client_alive_interval) { 313 uint64_t keepalive_ms = 314 (uint64_t)options.client_alive_interval * 1000; 315 316 client_alive_scheduled = 1; 317 if (max_time_ms == 0 || max_time_ms > keepalive_ms) 318 max_time_ms = keepalive_ms; 319 } 320 321 if (compat20) { 322 #if 0 323 /* wrong: bad condition XXX */ 324 if (channel_not_very_much_buffered_data()) 325 #endif 326 FD_SET(connection_in, *readsetp); 327 } else { 328 /* 329 * Read packets from the client unless we have too much 330 * buffered stdin or channel data. 331 */ 332 if (buffer_len(&stdin_buffer) < buffer_high && 333 channel_not_very_much_buffered_data()) 334 FD_SET(connection_in, *readsetp); 335 /* 336 * If there is not too much data already buffered going to 337 * the client, try to get some more data from the program. 338 */ 339 if (packet_not_very_much_data_to_write()) { 340 if (!fdout_eof) 341 FD_SET(fdout, *readsetp); 342 if (!fderr_eof) 343 FD_SET(fderr, *readsetp); 344 } 345 /* 346 * If we have buffered data, try to write some of that data 347 * to the program. 348 */ 349 if (fdin != -1 && buffer_len(&stdin_buffer) > 0) 350 FD_SET(fdin, *writesetp); 351 } 352 notify_prepare(*readsetp); 353 354 /* 355 * If we have buffered packet data going to the client, mark that 356 * descriptor. 357 */ 358 if (packet_have_data_to_write()) 359 FD_SET(connection_out, *writesetp); 360 361 /* 362 * If child has terminated and there is enough buffer space to read 363 * from it, then read as much as is available and exit. 364 */ 365 if (child_terminated && packet_not_very_much_data_to_write()) 366 if (max_time_ms == 0 || client_alive_scheduled) 367 max_time_ms = 100; 368 369 if (max_time_ms == 0) 370 tvp = NULL; 371 else { 372 tv.tv_sec = max_time_ms / 1000; 373 tv.tv_usec = 1000 * (max_time_ms % 1000); 374 tvp = &tv; 375 } 376 377 /* Wait for something to happen, or the timeout to expire. */ 378 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); 379 380 if (ret == -1) { 381 memset(*readsetp, 0, *nallocp); 382 memset(*writesetp, 0, *nallocp); 383 if (errno != EINTR) 384 error("select: %.100s", strerror(errno)); 385 } else if (ret == 0 && client_alive_scheduled) 386 client_alive_check(); 387 388 notify_done(*readsetp); 389 } 390 391 /* 392 * Processes input from the client and the program. Input data is stored 393 * in buffers and processed later. 394 */ 395 static void 396 process_input(fd_set *readset) 397 { 398 struct ssh *ssh = active_state; /* XXX */ 399 int len; 400 char buf[16384]; 401 402 /* Read and buffer any input data from the client. */ 403 if (FD_ISSET(connection_in, readset)) { 404 len = read(connection_in, buf, sizeof(buf)); 405 if (len == 0) { 406 verbose("Connection closed by %.100s port %d", 407 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); 408 connection_closed = 1; 409 if (compat20) 410 return; 411 cleanup_exit(255); 412 } else if (len < 0) { 413 if (errno != EINTR && errno != EAGAIN) { 414 verbose("Read error from remote host " 415 "%.100s port %d: %.100s", 416 ssh_remote_ipaddr(ssh), 417 ssh_remote_port(ssh), strerror(errno)); 418 cleanup_exit(255); 419 } 420 } else { 421 /* Buffer any received data. */ 422 packet_process_incoming(buf, len); 423 fdout_bytes += len; 424 } 425 } 426 if (compat20) 427 return; 428 429 /* Read and buffer any available stdout data from the program. */ 430 if (!fdout_eof && FD_ISSET(fdout, readset)) { 431 len = read(fdout, buf, sizeof(buf)); 432 if (len < 0 && (errno == EINTR || errno == EAGAIN)) { 433 /* do nothing */ 434 } else if (len <= 0) { 435 fdout_eof = 1; 436 } else { 437 buffer_append(&stdout_buffer, buf, len); 438 debug ("FD out now: %ld", fdout_bytes); 439 fdout_bytes += len; 440 } 441 } 442 /* Read and buffer any available stderr data from the program. */ 443 if (!fderr_eof && FD_ISSET(fderr, readset)) { 444 len = read(fderr, buf, sizeof(buf)); 445 if (len < 0 && (errno == EINTR || errno == EAGAIN)) { 446 /* do nothing */ 447 } else if (len <= 0) { 448 fderr_eof = 1; 449 } else { 450 buffer_append(&stderr_buffer, buf, len); 451 } 452 } 453 } 454 455 /* 456 * Sends data from internal buffers to client program stdin. 457 */ 458 static void 459 process_output(fd_set *writeset) 460 { 461 struct termios tio; 462 u_char *data; 463 u_int dlen; 464 int len; 465 466 /* Write buffered data to program stdin. */ 467 if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) { 468 data = buffer_ptr(&stdin_buffer); 469 dlen = buffer_len(&stdin_buffer); 470 len = write(fdin, data, dlen); 471 if (len < 0 && (errno == EINTR || errno == EAGAIN)) { 472 /* do nothing */ 473 } else if (len <= 0) { 474 if (fdin != fdout) 475 close(fdin); 476 else 477 shutdown(fdin, SHUT_WR); /* We will no longer send. */ 478 fdin = -1; 479 } else { 480 /* Successful write. */ 481 if (fdin_is_tty && dlen >= 1 && data[0] != '\r' && 482 tcgetattr(fdin, &tio) == 0 && 483 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) { 484 /* 485 * Simulate echo to reduce the impact of 486 * traffic analysis 487 */ 488 packet_send_ignore(len); 489 packet_send(); 490 } 491 /* Consume the data from the buffer. */ 492 buffer_consume(&stdin_buffer, len); 493 /* Update the count of bytes written to the program. */ 494 stdin_bytes += len; 495 } 496 } 497 /* Send any buffered packet data to the client. */ 498 if (FD_ISSET(connection_out, writeset)) 499 stdin_bytes += packet_write_poll(); 500 } 501 502 /* 503 * Wait until all buffered output has been sent to the client. 504 * This is used when the program terminates. 505 */ 506 static void 507 drain_output(void) 508 { 509 /* Send any buffered stdout data to the client. */ 510 if (buffer_len(&stdout_buffer) > 0) { 511 packet_start(SSH_SMSG_STDOUT_DATA); 512 packet_put_string(buffer_ptr(&stdout_buffer), 513 buffer_len(&stdout_buffer)); 514 packet_send(); 515 /* Update the count of sent bytes. */ 516 stdout_bytes += buffer_len(&stdout_buffer); 517 } 518 /* Send any buffered stderr data to the client. */ 519 if (buffer_len(&stderr_buffer) > 0) { 520 packet_start(SSH_SMSG_STDERR_DATA); 521 packet_put_string(buffer_ptr(&stderr_buffer), 522 buffer_len(&stderr_buffer)); 523 packet_send(); 524 /* Update the count of sent bytes. */ 525 stderr_bytes += buffer_len(&stderr_buffer); 526 } 527 /* Wait until all buffered data has been written to the client. */ 528 packet_write_wait(); 529 } 530 531 static void 532 process_buffered_input_packets(void) 533 { 534 dispatch_run(DISPATCH_NONBLOCK, NULL, active_state); 535 } 536 537 /* 538 * Performs the interactive session. This handles data transmission between 539 * the client and the program. Note that the notion of stdin, stdout, and 540 * stderr in this function is sort of reversed: this function writes to 541 * stdin (of the child program), and reads from stdout and stderr (of the 542 * child program). 543 */ 544 void 545 server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg) 546 { 547 fd_set *readset = NULL, *writeset = NULL; 548 int max_fd = 0; 549 u_int nalloc = 0; 550 int wait_status; /* Status returned by wait(). */ 551 pid_t wait_pid; /* pid returned by wait(). */ 552 int waiting_termination = 0; /* Have displayed waiting close message. */ 553 u_int64_t max_time_milliseconds; 554 u_int previous_stdout_buffer_bytes; 555 u_int stdout_buffer_bytes; 556 int type; 557 558 debug("Entering interactive session."); 559 560 /* Initialize the SIGCHLD kludge. */ 561 child_terminated = 0; 562 signal(SIGCHLD, sigchld_handler); 563 564 if (!use_privsep) { 565 signal(SIGTERM, sigterm_handler); 566 signal(SIGINT, sigterm_handler); 567 signal(SIGQUIT, sigterm_handler); 568 } 569 570 /* Initialize our global variables. */ 571 fdin = fdin_arg; 572 fdout = fdout_arg; 573 fderr = fderr_arg; 574 575 /* nonblocking IO */ 576 set_nonblock(fdin); 577 set_nonblock(fdout); 578 /* we don't have stderr for interactive terminal sessions, see below */ 579 if (fderr != -1) 580 set_nonblock(fderr); 581 582 if (!(datafellows & SSH_BUG_IGNOREMSG) && isatty(fdin)) 583 fdin_is_tty = 1; 584 585 connection_in = packet_get_connection_in(); 586 connection_out = packet_get_connection_out(); 587 588 notify_setup(); 589 590 previous_stdout_buffer_bytes = 0; 591 592 /* Set approximate I/O buffer size. */ 593 if (packet_is_interactive()) 594 buffer_high = 4096; 595 else 596 buffer_high = 64 * 1024; 597 598 #if 0 599 /* Initialize max_fd to the maximum of the known file descriptors. */ 600 max_fd = MAX(connection_in, connection_out); 601 max_fd = MAX(max_fd, fdin); 602 max_fd = MAX(max_fd, fdout); 603 if (fderr != -1) 604 max_fd = MAX(max_fd, fderr); 605 #endif 606 607 /* Initialize Initialize buffers. */ 608 buffer_init(&stdin_buffer); 609 buffer_init(&stdout_buffer); 610 buffer_init(&stderr_buffer); 611 612 /* 613 * If we have no separate fderr (which is the case when we have a pty 614 * - there we cannot make difference between data sent to stdout and 615 * stderr), indicate that we have seen an EOF from stderr. This way 616 * we don't need to check the descriptor everywhere. 617 */ 618 if (fderr == -1) 619 fderr_eof = 1; 620 621 server_init_dispatch(); 622 623 /* Main loop of the server for the interactive session mode. */ 624 for (;;) { 625 626 /* Process buffered packets from the client. */ 627 process_buffered_input_packets(); 628 629 /* 630 * If we have received eof, and there is no more pending 631 * input data, cause a real eof by closing fdin. 632 */ 633 if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) { 634 if (fdin != fdout) 635 close(fdin); 636 else 637 shutdown(fdin, SHUT_WR); /* We will no longer send. */ 638 fdin = -1; 639 } 640 /* Make packets from buffered stderr data to send to the client. */ 641 make_packets_from_stderr_data(); 642 643 /* 644 * Make packets from buffered stdout data to send to the 645 * client. If there is very little to send, this arranges to 646 * not send them now, but to wait a short while to see if we 647 * are getting more data. This is necessary, as some systems 648 * wake up readers from a pty after each separate character. 649 */ 650 max_time_milliseconds = 0; 651 stdout_buffer_bytes = buffer_len(&stdout_buffer); 652 if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 && 653 stdout_buffer_bytes != previous_stdout_buffer_bytes) { 654 /* try again after a while */ 655 max_time_milliseconds = 10; 656 } else { 657 /* Send it now. */ 658 make_packets_from_stdout_data(); 659 } 660 previous_stdout_buffer_bytes = buffer_len(&stdout_buffer); 661 662 /* Send channel data to the client. */ 663 if (packet_not_very_much_data_to_write()) 664 channel_output_poll(); 665 666 /* 667 * Bail out of the loop if the program has closed its output 668 * descriptors, and we have no more data to send to the 669 * client, and there is no pending buffered data. 670 */ 671 if (fdout_eof && fderr_eof && !packet_have_data_to_write() && 672 buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) { 673 if (!channel_still_open()) 674 break; 675 if (!waiting_termination) { 676 const char *s = "Waiting for forwarded connections to terminate...\r\n"; 677 char *cp; 678 waiting_termination = 1; 679 buffer_append(&stderr_buffer, s, strlen(s)); 680 681 /* Display list of open channels. */ 682 cp = channel_open_message(); 683 buffer_append(&stderr_buffer, cp, strlen(cp)); 684 free(cp); 685 } 686 } 687 max_fd = MAX(connection_in, connection_out); 688 max_fd = MAX(max_fd, fdin); 689 max_fd = MAX(max_fd, fdout); 690 max_fd = MAX(max_fd, fderr); 691 max_fd = MAX(max_fd, notify_pipe[0]); 692 693 /* Sleep in select() until we can do something. */ 694 wait_until_can_do_something(&readset, &writeset, &max_fd, 695 &nalloc, max_time_milliseconds); 696 697 if (received_sigterm) { 698 logit("Exiting on signal %d", (int)received_sigterm); 699 /* Clean up sessions, utmp, etc. */ 700 cleanup_exit(255); 701 } 702 703 /* Process any channel events. */ 704 channel_after_select(readset, writeset); 705 706 /* Process input from the client and from program stdout/stderr. */ 707 process_input(readset); 708 709 /* Process output to the client and to program stdin. */ 710 process_output(writeset); 711 } 712 free(readset); 713 free(writeset); 714 715 /* Cleanup and termination code. */ 716 717 /* Wait until all output has been sent to the client. */ 718 drain_output(); 719 720 debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.", 721 stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes); 722 723 /* Free and clear the buffers. */ 724 buffer_free(&stdin_buffer); 725 buffer_free(&stdout_buffer); 726 buffer_free(&stderr_buffer); 727 728 /* Close the file descriptors. */ 729 if (fdout != -1) 730 close(fdout); 731 fdout = -1; 732 fdout_eof = 1; 733 if (fderr != -1) 734 close(fderr); 735 fderr = -1; 736 fderr_eof = 1; 737 if (fdin != -1) 738 close(fdin); 739 fdin = -1; 740 741 channel_free_all(); 742 743 /* We no longer want our SIGCHLD handler to be called. */ 744 signal(SIGCHLD, SIG_DFL); 745 746 while ((wait_pid = waitpid(-1, &wait_status, 0)) < 0) 747 if (errno != EINTR) 748 packet_disconnect("wait: %.100s", strerror(errno)); 749 if (wait_pid != pid) 750 error("Strange, wait returned pid %ld, expected %ld", 751 (long)wait_pid, (long)pid); 752 753 /* Check if it exited normally. */ 754 if (WIFEXITED(wait_status)) { 755 /* Yes, normal exit. Get exit status and send it to the client. */ 756 debug("Command exited with status %d.", WEXITSTATUS(wait_status)); 757 packet_start(SSH_SMSG_EXITSTATUS); 758 packet_put_int(WEXITSTATUS(wait_status)); 759 packet_send(); 760 packet_write_wait(); 761 762 /* 763 * Wait for exit confirmation. Note that there might be 764 * other packets coming before it; however, the program has 765 * already died so we just ignore them. The client is 766 * supposed to respond with the confirmation when it receives 767 * the exit status. 768 */ 769 do { 770 type = packet_read(); 771 } 772 while (type != SSH_CMSG_EXIT_CONFIRMATION); 773 774 debug("Received exit confirmation."); 775 return; 776 } 777 /* Check if the program terminated due to a signal. */ 778 if (WIFSIGNALED(wait_status)) 779 packet_disconnect("Command terminated on signal %d.", 780 WTERMSIG(wait_status)); 781 782 /* Some weird exit cause. Just exit. */ 783 packet_disconnect("wait returned status %04x.", wait_status); 784 /* NOTREACHED */ 785 } 786 787 static void 788 collect_children(void) 789 { 790 pid_t pid; 791 sigset_t oset, nset; 792 int status; 793 794 /* block SIGCHLD while we check for dead children */ 795 sigemptyset(&nset); 796 sigaddset(&nset, SIGCHLD); 797 sigprocmask(SIG_BLOCK, &nset, &oset); 798 if (child_terminated) { 799 debug("Received SIGCHLD."); 800 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || 801 (pid < 0 && errno == EINTR)) 802 if (pid > 0) 803 session_close_by_pid(pid, status); 804 child_terminated = 0; 805 } 806 sigprocmask(SIG_SETMASK, &oset, NULL); 807 } 808 809 void 810 server_loop2(Authctxt *authctxt) 811 { 812 fd_set *readset = NULL, *writeset = NULL; 813 int max_fd; 814 u_int nalloc = 0; 815 u_int64_t rekey_timeout_ms = 0; 816 double start_time, total_time; 817 struct ssh *ssh = active_state; /* XXX */ 818 819 debug("Entering interactive session for SSH2."); 820 start_time = get_current_time(); 821 822 signal(SIGCHLD, sigchld_handler); 823 child_terminated = 0; 824 connection_in = packet_get_connection_in(); 825 connection_out = packet_get_connection_out(); 826 827 if (!use_privsep) { 828 signal(SIGTERM, sigterm_handler); 829 signal(SIGINT, sigterm_handler); 830 signal(SIGQUIT, sigterm_handler); 831 } 832 833 notify_setup(); 834 835 max_fd = MAX(connection_in, connection_out); 836 max_fd = MAX(max_fd, notify_pipe[0]); 837 838 server_init_dispatch(); 839 840 for (;;) { 841 process_buffered_input_packets(); 842 843 if (!ssh_packet_is_rekeying(active_state) && 844 packet_not_very_much_data_to_write()) 845 channel_output_poll(); 846 if (options.rekey_interval > 0 && compat20 && 847 !ssh_packet_is_rekeying(active_state)) 848 rekey_timeout_ms = packet_get_rekey_timeout() * 1000; 849 else 850 rekey_timeout_ms = 0; 851 852 wait_until_can_do_something(&readset, &writeset, &max_fd, 853 &nalloc, rekey_timeout_ms); 854 855 if (received_sigterm) { 856 logit("Exiting on signal %d", (int)received_sigterm); 857 /* Clean up sessions, utmp, etc. */ 858 cleanup_exit(255); 859 } 860 861 collect_children(); 862 if (!ssh_packet_is_rekeying(active_state)) 863 channel_after_select(readset, writeset); 864 process_input(readset); 865 if (connection_closed) 866 break; 867 process_output(writeset); 868 } 869 collect_children(); 870 871 free(readset); 872 free(writeset); 873 874 /* free all channels, no more reads and writes */ 875 channel_free_all(); 876 877 /* free remaining sessions, e.g. remove wtmp entries */ 878 session_destroy_all(NULL); 879 total_time = get_current_time() - start_time; 880 logit("SSH: Server;LType: Throughput;Remote: %s-%d;IN: %lu;OUT: %lu;Duration: %.1f;tPut_in: %.1f;tPut_out: %.1f", 881 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), 882 stdin_bytes, fdout_bytes, total_time, stdin_bytes / total_time, 883 fdout_bytes / total_time); 884 } 885 886 static int 887 server_input_keep_alive(int type, u_int32_t seq, void *ctxt) 888 { 889 debug("Got %d/%u for keepalive", type, seq); 890 /* 891 * reset timeout, since we got a sane answer from the client. 892 * even if this was generated by something other than 893 * the bogus CHANNEL_REQUEST we send for keepalives. 894 */ 895 packet_set_alive_timeouts(0); 896 return 0; 897 } 898 899 static int 900 server_input_stdin_data(int type, u_int32_t seq, void *ctxt) 901 { 902 char *data; 903 u_int data_len; 904 905 /* Stdin data from the client. Append it to the buffer. */ 906 /* Ignore any data if the client has closed stdin. */ 907 if (fdin == -1) 908 return 0; 909 data = packet_get_string(&data_len); 910 packet_check_eom(); 911 buffer_append(&stdin_buffer, data, data_len); 912 explicit_bzero(data, data_len); 913 free(data); 914 return 0; 915 } 916 917 static int 918 server_input_eof(int type, u_int32_t seq, void *ctxt) 919 { 920 /* 921 * Eof from the client. The stdin descriptor to the 922 * program will be closed when all buffered data has 923 * drained. 924 */ 925 debug("EOF received for stdin."); 926 packet_check_eom(); 927 stdin_eof = 1; 928 return 0; 929 } 930 931 static int 932 server_input_window_size(int type, u_int32_t seq, void *ctxt) 933 { 934 u_int row = packet_get_int(); 935 u_int col = packet_get_int(); 936 u_int xpixel = packet_get_int(); 937 u_int ypixel = packet_get_int(); 938 939 debug("Window change received."); 940 packet_check_eom(); 941 if (fdin != -1) 942 pty_change_window_size(fdin, row, col, xpixel, ypixel); 943 return 0; 944 } 945 946 static Channel * 947 server_request_direct_tcpip(void) 948 { 949 Channel *c = NULL; 950 char *target, *originator; 951 u_short target_port, originator_port; 952 953 target = packet_get_string(NULL); 954 target_port = packet_get_int(); 955 originator = packet_get_string(NULL); 956 originator_port = packet_get_int(); 957 packet_check_eom(); 958 959 debug("server_request_direct_tcpip: originator %s port %d, target %s " 960 "port %d", originator, originator_port, target, target_port); 961 962 /* XXX fine grained permissions */ 963 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 && 964 !no_port_forwarding_flag) { 965 c = channel_connect_to_port(target, target_port, 966 "direct-tcpip", "direct-tcpip"); 967 } else { 968 logit("refused local port forward: " 969 "originator %s port %d, target %s port %d", 970 originator, originator_port, target, target_port); 971 } 972 973 free(originator); 974 free(target); 975 976 return c; 977 } 978 979 static Channel * 980 server_request_direct_streamlocal(void) 981 { 982 Channel *c = NULL; 983 char *target, *originator; 984 u_short originator_port; 985 986 target = packet_get_string(NULL); 987 originator = packet_get_string(NULL); 988 originator_port = packet_get_int(); 989 packet_check_eom(); 990 991 debug("server_request_direct_streamlocal: originator %s port %d, target %s", 992 originator, originator_port, target); 993 994 /* XXX fine grained permissions */ 995 if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 && 996 !no_port_forwarding_flag) { 997 c = channel_connect_to_path(target, 998 "direct-streamlocal@openssh.com", "direct-streamlocal"); 999 } else { 1000 logit("refused streamlocal port forward: " 1001 "originator %s port %d, target %s", 1002 originator, originator_port, target); 1003 } 1004 1005 free(originator); 1006 free(target); 1007 1008 return c; 1009 } 1010 1011 static Channel * 1012 server_request_tun(void) 1013 { 1014 Channel *c = NULL; 1015 int mode, tun; 1016 int sock; 1017 1018 mode = packet_get_int(); 1019 switch (mode) { 1020 case SSH_TUNMODE_POINTOPOINT: 1021 case SSH_TUNMODE_ETHERNET: 1022 break; 1023 default: 1024 packet_send_debug("Unsupported tunnel device mode."); 1025 return NULL; 1026 } 1027 if ((options.permit_tun & mode) == 0) { 1028 packet_send_debug("Server has rejected tunnel device " 1029 "forwarding"); 1030 return NULL; 1031 } 1032 1033 tun = packet_get_int(); 1034 if (forced_tun_device != -1) { 1035 if (tun != SSH_TUNID_ANY && forced_tun_device != tun) 1036 goto done; 1037 tun = forced_tun_device; 1038 } 1039 sock = tun_open(tun, mode); 1040 if (sock < 0) 1041 goto done; 1042 if (options.hpn_disabled) 1043 c = channel_new("tun", SSH_CHANNEL_OPEN, sock, sock, -1, 1044 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); 1045 else 1046 c = channel_new("tun", SSH_CHANNEL_OPEN, sock, sock, -1, 1047 options.hpn_buffer_size, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); 1048 c->datagram = 1; 1049 1050 done: 1051 if (c == NULL) 1052 packet_send_debug("Failed to open the tunnel device."); 1053 return c; 1054 } 1055 1056 static Channel * 1057 server_request_session(void) 1058 { 1059 Channel *c; 1060 1061 debug("input_session_request"); 1062 packet_check_eom(); 1063 1064 if (no_more_sessions) { 1065 packet_disconnect("Possible attack: attempt to open a session " 1066 "after additional sessions disabled"); 1067 } 1068 1069 /* 1070 * A server session has no fd to read or write until a 1071 * CHANNEL_REQUEST for a shell is made, so we set the type to 1072 * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all 1073 * CHANNEL_REQUEST messages is registered. 1074 */ 1075 c = channel_new("session", SSH_CHANNEL_LARVAL, 1076 -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT, 1077 0, "server-session", 1); 1078 if ((options.tcp_rcv_buf_poll > 0) && (!options.hpn_disabled)) 1079 c->dynamic_window = 1; 1080 if (session_open(the_authctxt, c->self) != 1) { 1081 debug("session open failed, free channel %d", c->self); 1082 channel_free(c); 1083 return NULL; 1084 } 1085 channel_register_cleanup(c->self, session_close_by_channel, 0); 1086 return c; 1087 } 1088 1089 static int 1090 server_input_channel_open(int type, u_int32_t seq, void *ctxt) 1091 { 1092 Channel *c = NULL; 1093 char *ctype; 1094 int rchan; 1095 u_int rmaxpack, rwindow, len; 1096 1097 ctype = packet_get_string(&len); 1098 rchan = packet_get_int(); 1099 rwindow = packet_get_int(); 1100 rmaxpack = packet_get_int(); 1101 1102 debug("server_input_channel_open: ctype %s rchan %d win %d max %d", 1103 ctype, rchan, rwindow, rmaxpack); 1104 1105 if (strcmp(ctype, "session") == 0) { 1106 c = server_request_session(); 1107 } else if (strcmp(ctype, "direct-tcpip") == 0) { 1108 c = server_request_direct_tcpip(); 1109 } else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) { 1110 c = server_request_direct_streamlocal(); 1111 } else if (strcmp(ctype, "tun@openssh.com") == 0) { 1112 c = server_request_tun(); 1113 } 1114 if (c != NULL) { 1115 debug("server_input_channel_open: confirm %s", ctype); 1116 c->remote_id = rchan; 1117 c->remote_window = rwindow; 1118 c->remote_maxpacket = rmaxpack; 1119 if (c->type != SSH_CHANNEL_CONNECTING) { 1120 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); 1121 packet_put_int(c->remote_id); 1122 packet_put_int(c->self); 1123 packet_put_int(c->local_window); 1124 packet_put_int(c->local_maxpacket); 1125 packet_send(); 1126 } 1127 } else { 1128 debug("server_input_channel_open: failure %s", ctype); 1129 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); 1130 packet_put_int(rchan); 1131 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); 1132 if (!(datafellows & SSH_BUG_OPENFAILURE)) { 1133 packet_put_cstring("open failed"); 1134 packet_put_cstring(""); 1135 } 1136 packet_send(); 1137 } 1138 free(ctype); 1139 return 0; 1140 } 1141 1142 static int 1143 server_input_hostkeys_prove(struct sshbuf **respp) 1144 { 1145 struct ssh *ssh = active_state; /* XXX */ 1146 struct sshbuf *resp = NULL; 1147 struct sshbuf *sigbuf = NULL; 1148 struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL; 1149 int r, ndx, success = 0; 1150 const u_char *blob; 1151 u_char *sig = 0; 1152 size_t blen, slen; 1153 1154 if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL) 1155 fatal("%s: sshbuf_new", __func__); 1156 1157 while (ssh_packet_remaining(ssh) > 0) { 1158 sshkey_free(key); 1159 key = NULL; 1160 if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 || 1161 (r = sshkey_from_blob(blob, blen, &key)) != 0) { 1162 error("%s: couldn't parse key: %s", 1163 __func__, ssh_err(r)); 1164 goto out; 1165 } 1166 /* 1167 * Better check that this is actually one of our hostkeys 1168 * before attempting to sign anything with it. 1169 */ 1170 if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) { 1171 error("%s: unknown host %s key", 1172 __func__, sshkey_type(key)); 1173 goto out; 1174 } 1175 /* 1176 * XXX refactor: make kex->sign just use an index rather 1177 * than passing in public and private keys 1178 */ 1179 if ((key_prv = get_hostkey_by_index(ndx)) == NULL && 1180 (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) { 1181 error("%s: can't retrieve hostkey %d", __func__, ndx); 1182 goto out; 1183 } 1184 sshbuf_reset(sigbuf); 1185 free(sig); 1186 sig = NULL; 1187 if ((r = sshbuf_put_cstring(sigbuf, 1188 "hostkeys-prove-00@openssh.com")) != 0 || 1189 (r = sshbuf_put_string(sigbuf, 1190 ssh->kex->session_id, ssh->kex->session_id_len)) != 0 || 1191 (r = sshkey_puts(key, sigbuf)) != 0 || 1192 (r = ssh->kex->sign(key_prv, key_pub, &sig, &slen, 1193 sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), NULL, 0)) != 0 || 1194 (r = sshbuf_put_string(resp, sig, slen)) != 0) { 1195 error("%s: couldn't prepare signature: %s", 1196 __func__, ssh_err(r)); 1197 goto out; 1198 } 1199 } 1200 /* Success */ 1201 *respp = resp; 1202 resp = NULL; /* don't free it */ 1203 success = 1; 1204 out: 1205 free(sig); 1206 sshbuf_free(resp); 1207 sshbuf_free(sigbuf); 1208 sshkey_free(key); 1209 return success; 1210 } 1211 1212 static int 1213 server_input_global_request(int type, u_int32_t seq, void *ctxt) 1214 { 1215 char *rtype; 1216 int want_reply; 1217 int r, success = 0, allocated_listen_port = 0; 1218 struct sshbuf *resp = NULL; 1219 1220 rtype = packet_get_string(NULL); 1221 want_reply = packet_get_char(); 1222 debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply); 1223 1224 /* -R style forwarding */ 1225 if (strcmp(rtype, "tcpip-forward") == 0) { 1226 struct passwd *pw; 1227 struct Forward fwd; 1228 1229 pw = the_authctxt->pw; 1230 if (pw == NULL || !the_authctxt->valid) 1231 fatal("server_input_global_request: no/invalid user"); 1232 memset(&fwd, 0, sizeof(fwd)); 1233 fwd.listen_host = packet_get_string(NULL); 1234 fwd.listen_port = (u_short)packet_get_int(); 1235 debug("server_input_global_request: tcpip-forward listen %s port %d", 1236 fwd.listen_host, fwd.listen_port); 1237 1238 /* check permissions */ 1239 if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 || 1240 no_port_forwarding_flag || 1241 (!want_reply && fwd.listen_port == 0) || 1242 (fwd.listen_port != 0 && fwd.listen_port < IPPORT_RESERVED && 1243 pw->pw_uid != 0)) { 1244 success = 0; 1245 packet_send_debug("Server has disabled port forwarding."); 1246 } else { 1247 /* Start listening on the port */ 1248 success = channel_setup_remote_fwd_listener(&fwd, 1249 &allocated_listen_port, &options.fwd_opts); 1250 } 1251 free(fwd.listen_host); 1252 if ((resp = sshbuf_new()) == NULL) 1253 fatal("%s: sshbuf_new", __func__); 1254 if (allocated_listen_port != 0 && 1255 (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0) 1256 fatal("%s: sshbuf_put_u32: %s", __func__, ssh_err(r)); 1257 } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) { 1258 struct Forward fwd; 1259 1260 memset(&fwd, 0, sizeof(fwd)); 1261 fwd.listen_host = packet_get_string(NULL); 1262 fwd.listen_port = (u_short)packet_get_int(); 1263 debug("%s: cancel-tcpip-forward addr %s port %d", __func__, 1264 fwd.listen_host, fwd.listen_port); 1265 1266 success = channel_cancel_rport_listener(&fwd); 1267 free(fwd.listen_host); 1268 } else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) { 1269 struct Forward fwd; 1270 1271 memset(&fwd, 0, sizeof(fwd)); 1272 fwd.listen_path = packet_get_string(NULL); 1273 debug("server_input_global_request: streamlocal-forward listen path %s", 1274 fwd.listen_path); 1275 1276 /* check permissions */ 1277 if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0 1278 || no_port_forwarding_flag) { 1279 success = 0; 1280 packet_send_debug("Server has disabled port forwarding."); 1281 } else { 1282 /* Start listening on the socket */ 1283 success = channel_setup_remote_fwd_listener( 1284 &fwd, NULL, &options.fwd_opts); 1285 } 1286 free(fwd.listen_path); 1287 } else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) { 1288 struct Forward fwd; 1289 1290 memset(&fwd, 0, sizeof(fwd)); 1291 fwd.listen_path = packet_get_string(NULL); 1292 debug("%s: cancel-streamlocal-forward path %s", __func__, 1293 fwd.listen_path); 1294 1295 success = channel_cancel_rport_listener(&fwd); 1296 free(fwd.listen_path); 1297 } else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) { 1298 no_more_sessions = 1; 1299 success = 1; 1300 } else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) { 1301 success = server_input_hostkeys_prove(&resp); 1302 } 1303 if (want_reply) { 1304 packet_start(success ? 1305 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); 1306 if (success && resp != NULL) 1307 ssh_packet_put_raw(active_state, sshbuf_ptr(resp), 1308 sshbuf_len(resp)); 1309 packet_send(); 1310 packet_write_wait(); 1311 } 1312 free(rtype); 1313 sshbuf_free(resp); 1314 return 0; 1315 } 1316 1317 static int 1318 server_input_channel_req(int type, u_int32_t seq, void *ctxt) 1319 { 1320 Channel *c; 1321 int id, reply, success = 0; 1322 char *rtype; 1323 1324 id = packet_get_int(); 1325 rtype = packet_get_string(NULL); 1326 reply = packet_get_char(); 1327 1328 debug("server_input_channel_req: channel %d request %s reply %d", 1329 id, rtype, reply); 1330 1331 if ((c = channel_lookup(id)) == NULL) 1332 packet_disconnect("server_input_channel_req: " 1333 "unknown channel %d", id); 1334 if (!strcmp(rtype, "eow@openssh.com")) { 1335 packet_check_eom(); 1336 chan_rcvd_eow(c); 1337 } else if ((c->type == SSH_CHANNEL_LARVAL || 1338 c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0) 1339 success = session_input_channel_req(c, rtype); 1340 if (reply && !(c->flags & CHAN_CLOSE_SENT)) { 1341 packet_start(success ? 1342 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); 1343 packet_put_int(c->remote_id); 1344 packet_send(); 1345 } 1346 free(rtype); 1347 return 0; 1348 } 1349 1350 static void 1351 server_init_dispatch_20(void) 1352 { 1353 debug("server_init_dispatch_20"); 1354 dispatch_init(&dispatch_protocol_error); 1355 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); 1356 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); 1357 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); 1358 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); 1359 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open); 1360 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); 1361 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); 1362 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req); 1363 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); 1364 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request); 1365 /* client_alive */ 1366 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive); 1367 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive); 1368 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive); 1369 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive); 1370 /* rekeying */ 1371 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); 1372 } 1373 static void 1374 server_init_dispatch_13(void) 1375 { 1376 debug("server_init_dispatch_13"); 1377 dispatch_init(NULL); 1378 dispatch_set(SSH_CMSG_EOF, &server_input_eof); 1379 dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data); 1380 dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size); 1381 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close); 1382 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation); 1383 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data); 1384 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); 1385 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); 1386 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open); 1387 } 1388 static void 1389 server_init_dispatch_15(void) 1390 { 1391 server_init_dispatch_13(); 1392 debug("server_init_dispatch_15"); 1393 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof); 1394 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose); 1395 } 1396 static void 1397 server_init_dispatch(void) 1398 { 1399 if (compat20) 1400 server_init_dispatch_20(); 1401 else if (compat13) 1402 server_init_dispatch_13(); 1403 else 1404 server_init_dispatch_15(); 1405 } 1406