1 /* $OpenBSD: serverloop.c,v 1.185 2016/08/13 17:47:41 markus Exp $ */ 2 /* 3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 5 * All rights reserved 6 * Server main loop for handling the interactive session. 7 * 8 * As far as I am concerned, the code I have written for this software 9 * can be used freely for any purpose. Any derived versions of this 10 * software must be clearly marked as such, and if the derived work is 11 * incompatible with the protocol description in the RFC file, it must be 12 * called by a name other than "ssh" or "Secure Shell". 13 * 14 * SSH2 support by Markus Friedl. 15 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #include <sys/param.h> /* MIN MAX */ 39 #include <sys/types.h> 40 #include <sys/wait.h> 41 #include <sys/socket.h> 42 #include <sys/time.h> 43 #include <sys/queue.h> 44 45 #include <netinet/in.h> 46 47 #include <errno.h> 48 #include <fcntl.h> 49 #include <pwd.h> 50 #include <signal.h> 51 #include <string.h> 52 #include <termios.h> 53 #include <unistd.h> 54 #include <stdarg.h> 55 56 #include "xmalloc.h" 57 #include "packet.h" 58 #include "buffer.h" 59 #include "log.h" 60 #include "misc.h" 61 #include "servconf.h" 62 #include "canohost.h" 63 #include "sshpty.h" 64 #include "channels.h" 65 #include "compat.h" 66 #include "ssh2.h" 67 #include "key.h" 68 #include "cipher.h" 69 #include "kex.h" 70 #include "hostfile.h" 71 #include "auth.h" 72 #include "session.h" 73 #include "dispatch.h" 74 #include "auth-options.h" 75 #include "serverloop.h" 76 #include "ssherr.h" 77 78 extern ServerOptions options; 79 80 /* XXX */ 81 extern Authctxt *the_authctxt; 82 extern int use_privsep; 83 84 static int no_more_sessions = 0; /* Disallow further sessions. */ 85 86 /* 87 * This SIGCHLD kludge is used to detect when the child exits. The server 88 * will exit after that, as soon as forwarded connections have terminated. 89 */ 90 91 static volatile sig_atomic_t child_terminated = 0; /* The child has terminated. */ 92 93 /* Cleanup on signals (!use_privsep case only) */ 94 static volatile sig_atomic_t received_sigterm = 0; 95 96 /* prototypes */ 97 static void server_init_dispatch(void); 98 99 /* 100 * we write to this pipe if a SIGCHLD is caught in order to avoid 101 * the race between select() and child_terminated 102 */ 103 static int notify_pipe[2]; 104 static void 105 notify_setup(void) 106 { 107 if (pipe(notify_pipe) < 0) { 108 error("pipe(notify_pipe) failed %s", strerror(errno)); 109 } else if ((fcntl(notify_pipe[0], F_SETFD, FD_CLOEXEC) == -1) || 110 (fcntl(notify_pipe[1], F_SETFD, FD_CLOEXEC) == -1)) { 111 error("fcntl(notify_pipe, F_SETFD) failed %s", strerror(errno)); 112 close(notify_pipe[0]); 113 close(notify_pipe[1]); 114 } else { 115 set_nonblock(notify_pipe[0]); 116 set_nonblock(notify_pipe[1]); 117 return; 118 } 119 notify_pipe[0] = -1; /* read end */ 120 notify_pipe[1] = -1; /* write end */ 121 } 122 static void 123 notify_parent(void) 124 { 125 if (notify_pipe[1] != -1) 126 (void)write(notify_pipe[1], "", 1); 127 } 128 static void 129 notify_prepare(fd_set *readset) 130 { 131 if (notify_pipe[0] != -1) 132 FD_SET(notify_pipe[0], readset); 133 } 134 static void 135 notify_done(fd_set *readset) 136 { 137 char c; 138 139 if (notify_pipe[0] != -1 && FD_ISSET(notify_pipe[0], readset)) 140 while (read(notify_pipe[0], &c, 1) != -1) 141 debug2("notify_done: reading"); 142 } 143 144 /*ARGSUSED*/ 145 static void 146 sigchld_handler(int sig) 147 { 148 int save_errno = errno; 149 child_terminated = 1; 150 signal(SIGCHLD, sigchld_handler); 151 notify_parent(); 152 errno = save_errno; 153 } 154 155 /*ARGSUSED*/ 156 static void 157 sigterm_handler(int sig) 158 { 159 received_sigterm = sig; 160 } 161 162 static void 163 client_alive_check(void) 164 { 165 int channel_id; 166 167 /* timeout, check to see how many we have had */ 168 if (packet_inc_alive_timeouts() > options.client_alive_count_max) { 169 logit("Timeout, client not responding."); 170 cleanup_exit(255); 171 } 172 173 /* 174 * send a bogus global/channel request with "wantreply", 175 * we should get back a failure 176 */ 177 if ((channel_id = channel_find_open()) == -1) { 178 packet_start(SSH2_MSG_GLOBAL_REQUEST); 179 packet_put_cstring("keepalive@openssh.com"); 180 packet_put_char(1); /* boolean: want reply */ 181 } else { 182 channel_request_start(channel_id, "keepalive@openssh.com", 1); 183 } 184 packet_send(); 185 } 186 187 /* 188 * Sleep in select() until we can do something. This will initialize the 189 * select masks. Upon return, the masks will indicate which descriptors 190 * have data or can accept data. Optionally, a maximum time can be specified 191 * for the duration of the wait (0 = infinite). 192 */ 193 static void 194 wait_until_can_do_something(int connection_in, int connection_out, 195 fd_set **readsetp, fd_set **writesetp, int *maxfdp, 196 u_int *nallocp, u_int64_t max_time_ms) 197 { 198 struct timeval tv, *tvp; 199 int ret; 200 time_t minwait_secs = 0; 201 int client_alive_scheduled = 0; 202 203 /* Allocate and update select() masks for channel descriptors. */ 204 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, 205 &minwait_secs, 0); 206 207 /* XXX need proper deadline system for rekey/client alive */ 208 if (minwait_secs != 0) 209 max_time_ms = MIN(max_time_ms, (u_int)minwait_secs * 1000); 210 211 /* 212 * if using client_alive, set the max timeout accordingly, 213 * and indicate that this particular timeout was for client 214 * alive by setting the client_alive_scheduled flag. 215 * 216 * this could be randomized somewhat to make traffic 217 * analysis more difficult, but we're not doing it yet. 218 */ 219 if (options.client_alive_interval) { 220 uint64_t keepalive_ms = 221 (uint64_t)options.client_alive_interval * 1000; 222 223 client_alive_scheduled = 1; 224 if (max_time_ms == 0 || max_time_ms > keepalive_ms) 225 max_time_ms = keepalive_ms; 226 } 227 228 #if 0 229 /* wrong: bad condition XXX */ 230 if (channel_not_very_much_buffered_data()) 231 #endif 232 FD_SET(connection_in, *readsetp); 233 notify_prepare(*readsetp); 234 235 /* 236 * If we have buffered packet data going to the client, mark that 237 * descriptor. 238 */ 239 if (packet_have_data_to_write()) 240 FD_SET(connection_out, *writesetp); 241 242 /* 243 * If child has terminated and there is enough buffer space to read 244 * from it, then read as much as is available and exit. 245 */ 246 if (child_terminated && packet_not_very_much_data_to_write()) 247 if (max_time_ms == 0 || client_alive_scheduled) 248 max_time_ms = 100; 249 250 if (max_time_ms == 0) 251 tvp = NULL; 252 else { 253 tv.tv_sec = max_time_ms / 1000; 254 tv.tv_usec = 1000 * (max_time_ms % 1000); 255 tvp = &tv; 256 } 257 258 /* Wait for something to happen, or the timeout to expire. */ 259 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); 260 261 if (ret == -1) { 262 memset(*readsetp, 0, *nallocp); 263 memset(*writesetp, 0, *nallocp); 264 if (errno != EINTR) 265 error("select: %.100s", strerror(errno)); 266 } else if (ret == 0 && client_alive_scheduled) 267 client_alive_check(); 268 269 notify_done(*readsetp); 270 } 271 272 /* 273 * Processes input from the client and the program. Input data is stored 274 * in buffers and processed later. 275 */ 276 static int 277 process_input(fd_set *readset, int connection_in) 278 { 279 struct ssh *ssh = active_state; /* XXX */ 280 int len; 281 char buf[16384]; 282 283 /* Read and buffer any input data from the client. */ 284 if (FD_ISSET(connection_in, readset)) { 285 len = read(connection_in, buf, sizeof(buf)); 286 if (len == 0) { 287 verbose("Connection closed by %.100s port %d", 288 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); 289 return -1; 290 } else if (len < 0) { 291 if (errno != EINTR && errno != EAGAIN) { 292 verbose("Read error from remote host " 293 "%.100s port %d: %.100s", 294 ssh_remote_ipaddr(ssh), 295 ssh_remote_port(ssh), strerror(errno)); 296 cleanup_exit(255); 297 } 298 } else { 299 /* Buffer any received data. */ 300 packet_process_incoming(buf, len); 301 } 302 } 303 return 0; 304 } 305 306 /* 307 * Sends data from internal buffers to client program stdin. 308 */ 309 static void 310 process_output(fd_set *writeset, int connection_out) 311 { 312 /* Send any buffered packet data to the client. */ 313 if (FD_ISSET(connection_out, writeset)) 314 packet_write_poll(); 315 } 316 317 static void 318 process_buffered_input_packets(void) 319 { 320 dispatch_run(DISPATCH_NONBLOCK, NULL, active_state); 321 } 322 323 static void 324 collect_children(void) 325 { 326 pid_t pid; 327 sigset_t oset, nset; 328 int status; 329 330 /* block SIGCHLD while we check for dead children */ 331 sigemptyset(&nset); 332 sigaddset(&nset, SIGCHLD); 333 sigprocmask(SIG_BLOCK, &nset, &oset); 334 if (child_terminated) { 335 debug("Received SIGCHLD."); 336 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || 337 (pid < 0 && errno == EINTR)) 338 if (pid > 0) 339 session_close_by_pid(pid, status); 340 child_terminated = 0; 341 } 342 sigprocmask(SIG_SETMASK, &oset, NULL); 343 } 344 345 void 346 server_loop2(Authctxt *authctxt) 347 { 348 fd_set *readset = NULL, *writeset = NULL; 349 int max_fd; 350 u_int nalloc = 0, connection_in, connection_out; 351 u_int64_t rekey_timeout_ms = 0; 352 353 debug("Entering interactive session for SSH2."); 354 355 signal(SIGCHLD, sigchld_handler); 356 child_terminated = 0; 357 connection_in = packet_get_connection_in(); 358 connection_out = packet_get_connection_out(); 359 360 if (!use_privsep) { 361 signal(SIGTERM, sigterm_handler); 362 signal(SIGINT, sigterm_handler); 363 signal(SIGQUIT, sigterm_handler); 364 } 365 366 notify_setup(); 367 368 max_fd = MAX(connection_in, connection_out); 369 max_fd = MAX(max_fd, notify_pipe[0]); 370 371 server_init_dispatch(); 372 373 for (;;) { 374 process_buffered_input_packets(); 375 376 if (!ssh_packet_is_rekeying(active_state) && 377 packet_not_very_much_data_to_write()) 378 channel_output_poll(); 379 if (options.rekey_interval > 0 && 380 !ssh_packet_is_rekeying(active_state)) 381 rekey_timeout_ms = packet_get_rekey_timeout() * 1000; 382 else 383 rekey_timeout_ms = 0; 384 385 wait_until_can_do_something(connection_in, connection_out, 386 &readset, &writeset, &max_fd, &nalloc, rekey_timeout_ms); 387 388 if (received_sigterm) { 389 logit("Exiting on signal %d", (int)received_sigterm); 390 /* Clean up sessions, utmp, etc. */ 391 cleanup_exit(255); 392 } 393 394 collect_children(); 395 if (!ssh_packet_is_rekeying(active_state)) 396 channel_after_select(readset, writeset); 397 if (process_input(readset, connection_in) < 0) 398 break; 399 process_output(writeset, connection_out); 400 } 401 collect_children(); 402 403 free(readset); 404 free(writeset); 405 406 /* free all channels, no more reads and writes */ 407 channel_free_all(); 408 409 /* free remaining sessions, e.g. remove wtmp entries */ 410 session_destroy_all(NULL); 411 } 412 413 static int 414 server_input_keep_alive(int type, u_int32_t seq, void *ctxt) 415 { 416 debug("Got %d/%u for keepalive", type, seq); 417 /* 418 * reset timeout, since we got a sane answer from the client. 419 * even if this was generated by something other than 420 * the bogus CHANNEL_REQUEST we send for keepalives. 421 */ 422 packet_set_alive_timeouts(0); 423 return 0; 424 } 425 426 static Channel * 427 server_request_direct_tcpip(void) 428 { 429 Channel *c = NULL; 430 char *target, *originator; 431 u_short target_port, originator_port; 432 433 target = packet_get_string(NULL); 434 target_port = packet_get_int(); 435 originator = packet_get_string(NULL); 436 originator_port = packet_get_int(); 437 packet_check_eom(); 438 439 debug("server_request_direct_tcpip: originator %s port %d, target %s " 440 "port %d", originator, originator_port, target, target_port); 441 442 /* XXX fine grained permissions */ 443 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 && 444 !no_port_forwarding_flag) { 445 c = channel_connect_to_port(target, target_port, 446 "direct-tcpip", "direct-tcpip"); 447 } else { 448 logit("refused local port forward: " 449 "originator %s port %d, target %s port %d", 450 originator, originator_port, target, target_port); 451 } 452 453 free(originator); 454 free(target); 455 456 return c; 457 } 458 459 static Channel * 460 server_request_direct_streamlocal(void) 461 { 462 Channel *c = NULL; 463 char *target, *originator; 464 u_short originator_port; 465 466 target = packet_get_string(NULL); 467 originator = packet_get_string(NULL); 468 originator_port = packet_get_int(); 469 packet_check_eom(); 470 471 debug("server_request_direct_streamlocal: originator %s port %d, target %s", 472 originator, originator_port, target); 473 474 /* XXX fine grained permissions */ 475 if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 && 476 !no_port_forwarding_flag) { 477 c = channel_connect_to_path(target, 478 "direct-streamlocal@openssh.com", "direct-streamlocal"); 479 } else { 480 logit("refused streamlocal port forward: " 481 "originator %s port %d, target %s", 482 originator, originator_port, target); 483 } 484 485 free(originator); 486 free(target); 487 488 return c; 489 } 490 491 static Channel * 492 server_request_tun(void) 493 { 494 Channel *c = NULL; 495 int mode, tun; 496 int sock; 497 498 mode = packet_get_int(); 499 switch (mode) { 500 case SSH_TUNMODE_POINTOPOINT: 501 case SSH_TUNMODE_ETHERNET: 502 break; 503 default: 504 packet_send_debug("Unsupported tunnel device mode."); 505 return NULL; 506 } 507 if ((options.permit_tun & mode) == 0) { 508 packet_send_debug("Server has rejected tunnel device " 509 "forwarding"); 510 return NULL; 511 } 512 513 tun = packet_get_int(); 514 if (forced_tun_device != -1) { 515 if (tun != SSH_TUNID_ANY && forced_tun_device != tun) 516 goto done; 517 tun = forced_tun_device; 518 } 519 sock = tun_open(tun, mode); 520 if (sock < 0) 521 goto done; 522 c = channel_new("tun", SSH_CHANNEL_OPEN, sock, sock, -1, 523 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); 524 c->datagram = 1; 525 526 done: 527 if (c == NULL) 528 packet_send_debug("Failed to open the tunnel device."); 529 return c; 530 } 531 532 static Channel * 533 server_request_session(void) 534 { 535 Channel *c; 536 537 debug("input_session_request"); 538 packet_check_eom(); 539 540 if (no_more_sessions) { 541 packet_disconnect("Possible attack: attempt to open a session " 542 "after additional sessions disabled"); 543 } 544 545 /* 546 * A server session has no fd to read or write until a 547 * CHANNEL_REQUEST for a shell is made, so we set the type to 548 * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all 549 * CHANNEL_REQUEST messages is registered. 550 */ 551 c = channel_new("session", SSH_CHANNEL_LARVAL, 552 -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT, 553 0, "server-session", 1); 554 if (session_open(the_authctxt, c->self) != 1) { 555 debug("session open failed, free channel %d", c->self); 556 channel_free(c); 557 return NULL; 558 } 559 channel_register_cleanup(c->self, session_close_by_channel, 0); 560 return c; 561 } 562 563 static int 564 server_input_channel_open(int type, u_int32_t seq, void *ctxt) 565 { 566 Channel *c = NULL; 567 char *ctype; 568 int rchan; 569 u_int rmaxpack, rwindow, len; 570 571 ctype = packet_get_string(&len); 572 rchan = packet_get_int(); 573 rwindow = packet_get_int(); 574 rmaxpack = packet_get_int(); 575 576 debug("server_input_channel_open: ctype %s rchan %d win %d max %d", 577 ctype, rchan, rwindow, rmaxpack); 578 579 if (strcmp(ctype, "session") == 0) { 580 c = server_request_session(); 581 } else if (strcmp(ctype, "direct-tcpip") == 0) { 582 c = server_request_direct_tcpip(); 583 } else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) { 584 c = server_request_direct_streamlocal(); 585 } else if (strcmp(ctype, "tun@openssh.com") == 0) { 586 c = server_request_tun(); 587 } 588 if (c != NULL) { 589 debug("server_input_channel_open: confirm %s", ctype); 590 c->remote_id = rchan; 591 c->remote_window = rwindow; 592 c->remote_maxpacket = rmaxpack; 593 if (c->type != SSH_CHANNEL_CONNECTING) { 594 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); 595 packet_put_int(c->remote_id); 596 packet_put_int(c->self); 597 packet_put_int(c->local_window); 598 packet_put_int(c->local_maxpacket); 599 packet_send(); 600 } 601 } else { 602 debug("server_input_channel_open: failure %s", ctype); 603 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); 604 packet_put_int(rchan); 605 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); 606 if (!(datafellows & SSH_BUG_OPENFAILURE)) { 607 packet_put_cstring("open failed"); 608 packet_put_cstring(""); 609 } 610 packet_send(); 611 } 612 free(ctype); 613 return 0; 614 } 615 616 static int 617 server_input_hostkeys_prove(struct sshbuf **respp) 618 { 619 struct ssh *ssh = active_state; /* XXX */ 620 struct sshbuf *resp = NULL; 621 struct sshbuf *sigbuf = NULL; 622 struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL; 623 int r, ndx, success = 0; 624 const u_char *blob; 625 u_char *sig = 0; 626 size_t blen, slen; 627 628 if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL) 629 fatal("%s: sshbuf_new", __func__); 630 631 while (ssh_packet_remaining(ssh) > 0) { 632 sshkey_free(key); 633 key = NULL; 634 if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 || 635 (r = sshkey_from_blob(blob, blen, &key)) != 0) { 636 error("%s: couldn't parse key: %s", 637 __func__, ssh_err(r)); 638 goto out; 639 } 640 /* 641 * Better check that this is actually one of our hostkeys 642 * before attempting to sign anything with it. 643 */ 644 if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) { 645 error("%s: unknown host %s key", 646 __func__, sshkey_type(key)); 647 goto out; 648 } 649 /* 650 * XXX refactor: make kex->sign just use an index rather 651 * than passing in public and private keys 652 */ 653 if ((key_prv = get_hostkey_by_index(ndx)) == NULL && 654 (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) { 655 error("%s: can't retrieve hostkey %d", __func__, ndx); 656 goto out; 657 } 658 sshbuf_reset(sigbuf); 659 free(sig); 660 sig = NULL; 661 if ((r = sshbuf_put_cstring(sigbuf, 662 "hostkeys-prove-00@openssh.com")) != 0 || 663 (r = sshbuf_put_string(sigbuf, 664 ssh->kex->session_id, ssh->kex->session_id_len)) != 0 || 665 (r = sshkey_puts(key, sigbuf)) != 0 || 666 (r = ssh->kex->sign(key_prv, key_pub, &sig, &slen, 667 sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), NULL, 0)) != 0 || 668 (r = sshbuf_put_string(resp, sig, slen)) != 0) { 669 error("%s: couldn't prepare signature: %s", 670 __func__, ssh_err(r)); 671 goto out; 672 } 673 } 674 /* Success */ 675 *respp = resp; 676 resp = NULL; /* don't free it */ 677 success = 1; 678 out: 679 free(sig); 680 sshbuf_free(resp); 681 sshbuf_free(sigbuf); 682 sshkey_free(key); 683 return success; 684 } 685 686 static int 687 server_input_global_request(int type, u_int32_t seq, void *ctxt) 688 { 689 char *rtype; 690 int want_reply; 691 int r, success = 0, allocated_listen_port = 0; 692 struct sshbuf *resp = NULL; 693 694 rtype = packet_get_string(NULL); 695 want_reply = packet_get_char(); 696 debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply); 697 698 /* -R style forwarding */ 699 if (strcmp(rtype, "tcpip-forward") == 0) { 700 struct passwd *pw; 701 struct Forward fwd; 702 703 pw = the_authctxt->pw; 704 if (pw == NULL || !the_authctxt->valid) 705 fatal("server_input_global_request: no/invalid user"); 706 memset(&fwd, 0, sizeof(fwd)); 707 fwd.listen_host = packet_get_string(NULL); 708 fwd.listen_port = (u_short)packet_get_int(); 709 debug("server_input_global_request: tcpip-forward listen %s port %d", 710 fwd.listen_host, fwd.listen_port); 711 712 /* check permissions */ 713 if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 || 714 no_port_forwarding_flag || 715 (!want_reply && fwd.listen_port == 0) || 716 (fwd.listen_port != 0 && fwd.listen_port < IPPORT_RESERVED && 717 pw->pw_uid != 0)) { 718 success = 0; 719 packet_send_debug("Server has disabled port forwarding."); 720 } else { 721 /* Start listening on the port */ 722 success = channel_setup_remote_fwd_listener(&fwd, 723 &allocated_listen_port, &options.fwd_opts); 724 } 725 free(fwd.listen_host); 726 if ((resp = sshbuf_new()) == NULL) 727 fatal("%s: sshbuf_new", __func__); 728 if (allocated_listen_port != 0 && 729 (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0) 730 fatal("%s: sshbuf_put_u32: %s", __func__, ssh_err(r)); 731 } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) { 732 struct Forward fwd; 733 734 memset(&fwd, 0, sizeof(fwd)); 735 fwd.listen_host = packet_get_string(NULL); 736 fwd.listen_port = (u_short)packet_get_int(); 737 debug("%s: cancel-tcpip-forward addr %s port %d", __func__, 738 fwd.listen_host, fwd.listen_port); 739 740 success = channel_cancel_rport_listener(&fwd); 741 free(fwd.listen_host); 742 } else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) { 743 struct Forward fwd; 744 745 memset(&fwd, 0, sizeof(fwd)); 746 fwd.listen_path = packet_get_string(NULL); 747 debug("server_input_global_request: streamlocal-forward listen path %s", 748 fwd.listen_path); 749 750 /* check permissions */ 751 if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0 752 || no_port_forwarding_flag) { 753 success = 0; 754 packet_send_debug("Server has disabled port forwarding."); 755 } else { 756 /* Start listening on the socket */ 757 success = channel_setup_remote_fwd_listener( 758 &fwd, NULL, &options.fwd_opts); 759 } 760 free(fwd.listen_path); 761 } else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) { 762 struct Forward fwd; 763 764 memset(&fwd, 0, sizeof(fwd)); 765 fwd.listen_path = packet_get_string(NULL); 766 debug("%s: cancel-streamlocal-forward path %s", __func__, 767 fwd.listen_path); 768 769 success = channel_cancel_rport_listener(&fwd); 770 free(fwd.listen_path); 771 } else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) { 772 no_more_sessions = 1; 773 success = 1; 774 } else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) { 775 success = server_input_hostkeys_prove(&resp); 776 } 777 if (want_reply) { 778 packet_start(success ? 779 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); 780 if (success && resp != NULL) 781 ssh_packet_put_raw(active_state, sshbuf_ptr(resp), 782 sshbuf_len(resp)); 783 packet_send(); 784 packet_write_wait(); 785 } 786 free(rtype); 787 sshbuf_free(resp); 788 return 0; 789 } 790 791 static int 792 server_input_channel_req(int type, u_int32_t seq, void *ctxt) 793 { 794 Channel *c; 795 int id, reply, success = 0; 796 char *rtype; 797 798 id = packet_get_int(); 799 rtype = packet_get_string(NULL); 800 reply = packet_get_char(); 801 802 debug("server_input_channel_req: channel %d request %s reply %d", 803 id, rtype, reply); 804 805 if ((c = channel_lookup(id)) == NULL) 806 packet_disconnect("server_input_channel_req: " 807 "unknown channel %d", id); 808 if (!strcmp(rtype, "eow@openssh.com")) { 809 packet_check_eom(); 810 chan_rcvd_eow(c); 811 } else if ((c->type == SSH_CHANNEL_LARVAL || 812 c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0) 813 success = session_input_channel_req(c, rtype); 814 if (reply && !(c->flags & CHAN_CLOSE_SENT)) { 815 packet_start(success ? 816 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); 817 packet_put_int(c->remote_id); 818 packet_send(); 819 } 820 free(rtype); 821 return 0; 822 } 823 824 static void 825 server_init_dispatch(void) 826 { 827 debug("server_init_dispatch"); 828 dispatch_init(&dispatch_protocol_error); 829 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); 830 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); 831 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); 832 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); 833 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open); 834 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); 835 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); 836 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req); 837 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); 838 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request); 839 /* client_alive */ 840 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive); 841 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive); 842 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive); 843 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive); 844 /* rekeying */ 845 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); 846 } 847