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