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