jcs's openbsd hax
openbsd
at jcs 910 lines 29 kB view raw
1/* $OpenBSD: serverloop.c,v 1.245 2025/10/30 03:19:54 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 <limits.h> 50#include <poll.h> 51#include <signal.h> 52#include <string.h> 53#include <termios.h> 54#include <unistd.h> 55#include <stdarg.h> 56 57#include "xmalloc.h" 58#include "packet.h" 59#include "sshbuf.h" 60#include "log.h" 61#include "misc.h" 62#include "servconf.h" 63#include "canohost.h" 64#include "sshpty.h" 65#include "channels.h" 66#include "ssh2.h" 67#include "sshkey.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 78extern ServerOptions options; 79 80/* XXX */ 81extern Authctxt *the_authctxt; 82extern struct sshauthopt *auth_opts; 83 84static int no_more_sessions = 0; /* Disallow further sessions. */ 85 86static volatile sig_atomic_t child_terminated = 0; /* set on SIGCHLD */ 87static volatile sig_atomic_t siginfo_received = 0; 88 89/* prototypes */ 90static void server_init_dispatch(struct ssh *); 91 92/* requested tunnel forwarding interface(s), shared with session.c */ 93char *tun_fwd_ifnames = NULL; 94 95static void 96sigchld_handler(int sig) 97{ 98 child_terminated = 1; 99} 100 101static void 102siginfo_handler(int sig) 103{ 104 siginfo_received = 1; 105} 106 107static void 108client_alive_check(struct ssh *ssh) 109{ 110 char remote_id[512]; 111 int r, channel_id; 112 113 /* timeout, check to see how many we have had */ 114 if (options.client_alive_count_max > 0 && 115 ssh_packet_inc_alive_timeouts(ssh) > 116 options.client_alive_count_max) { 117 sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id)); 118 logit("Timeout, client not responding from %s", remote_id); 119 cleanup_exit(255); 120 } 121 122 /* 123 * send a bogus global/channel request with "wantreply", 124 * we should get back a failure 125 */ 126 if ((channel_id = channel_find_open(ssh)) == -1) { 127 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || 128 (r = sshpkt_put_cstring(ssh, "keepalive@openssh.com")) 129 != 0 || 130 (r = sshpkt_put_u8(ssh, 1)) != 0) /* boolean: want reply */ 131 fatal_fr(r, "compose"); 132 } else { 133 channel_request_start(ssh, channel_id, 134 "keepalive@openssh.com", 1); 135 } 136 if ((r = sshpkt_send(ssh)) != 0) 137 fatal_fr(r, "send"); 138} 139 140/* 141 * Sleep in ppoll() until we can do something. 142 * Optionally, a maximum time can be specified for the duration of 143 * the wait (0 = infinite). 144 */ 145static void 146wait_until_can_do_something(struct ssh *ssh, 147 int connection_in, int connection_out, struct pollfd **pfdp, 148 u_int *npfd_allocp, u_int *npfd_activep, sigset_t *sigsetp, 149 int *conn_in_readyp, int *conn_out_readyp) 150{ 151 struct timespec timeout; 152 char remote_id[512]; 153 int ret; 154 int client_alive_scheduled = 0; 155 u_int p; 156 time_t now; 157 static time_t last_client_time, unused_connection_expiry; 158 159 *conn_in_readyp = *conn_out_readyp = 0; 160 161 /* Prepare channel poll. First two pollfd entries are reserved */ 162 ptimeout_init(&timeout); 163 channel_prepare_poll(ssh, pfdp, npfd_allocp, npfd_activep, 2, &timeout); 164 now = monotime(); 165 if (*npfd_activep < 2) 166 fatal_f("bad npfd %u", *npfd_activep); /* shouldn't happen */ 167 if (options.rekey_interval > 0 && !ssh_packet_is_rekeying(ssh)) { 168 ptimeout_deadline_sec(&timeout, 169 ssh_packet_get_rekey_timeout(ssh)); 170 } 171 172 /* 173 * If no channels are open and UnusedConnectionTimeout is set, then 174 * start the clock to terminate the connection. 175 */ 176 if (options.unused_connection_timeout != 0) { 177 if (channel_still_open(ssh)) 178 unused_connection_expiry = 0; 179 else if (unused_connection_expiry == 0) { 180 unused_connection_expiry = now + 181 options.unused_connection_timeout; 182 } 183 } 184 if (unused_connection_expiry != 0) 185 ptimeout_deadline_monotime(&timeout, unused_connection_expiry); 186 187 /* 188 * if using client_alive, set the max timeout accordingly, 189 * and indicate that this particular timeout was for client 190 * alive by setting the client_alive_scheduled flag. 191 * 192 * this could be randomized somewhat to make traffic 193 * analysis more difficult, but we're not doing it yet. 194 */ 195 if (options.client_alive_interval) { 196 /* Time we last heard from the client OR sent a keepalive */ 197 if (last_client_time == 0) 198 last_client_time = now; 199 ptimeout_deadline_sec(&timeout, options.client_alive_interval); 200 /* XXX ? deadline_monotime(last_client_time + alive_interval) */ 201 client_alive_scheduled = 1; 202 } 203 204#if 0 205 /* wrong: bad condition XXX */ 206 if (channel_not_very_much_buffered_data()) 207#endif 208 /* Monitor client connection on reserved pollfd entries */ 209 (*pfdp)[0].fd = connection_in; 210 (*pfdp)[0].events = POLLIN; 211 (*pfdp)[1].fd = connection_out; 212 (*pfdp)[1].events = ssh_packet_have_data_to_write(ssh) ? POLLOUT : 0; 213 214 /* 215 * If child has terminated and there is enough buffer space to read 216 * from it, then read as much as is available and exit. 217 */ 218 if (child_terminated && ssh_packet_not_very_much_data_to_write(ssh)) 219 ptimeout_deadline_ms(&timeout, 100); 220 221 /* Wait for something to happen, or the timeout to expire. */ 222 ret = ppoll(*pfdp, *npfd_activep, ptimeout_get_tsp(&timeout), sigsetp); 223 224 if (ret == -1) { 225 for (p = 0; p < *npfd_activep; p++) 226 (*pfdp)[p].revents = 0; 227 if (errno != EINTR) 228 fatal_f("ppoll: %.100s", strerror(errno)); 229 return; 230 } 231 232 *conn_in_readyp = (*pfdp)[0].revents != 0; 233 *conn_out_readyp = (*pfdp)[1].revents != 0; 234 235 now = monotime(); /* need to reset after ppoll() */ 236 /* ClientAliveInterval probing */ 237 if (client_alive_scheduled) { 238 if (ret == 0 && 239 now >= last_client_time + options.client_alive_interval) { 240 /* ppoll timed out and we're due to probe */ 241 client_alive_check(ssh); 242 last_client_time = now; 243 } else if (ret != 0 && *conn_in_readyp) { 244 /* Data from peer; reset probe timer. */ 245 last_client_time = now; 246 } 247 } 248 249 /* UnusedConnectionTimeout handling */ 250 if (unused_connection_expiry != 0 && 251 now > unused_connection_expiry && !channel_still_open(ssh)) { 252 sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id)); 253 logit("terminating inactive connection from %s", remote_id); 254 cleanup_exit(255); 255 } 256} 257 258/* 259 * Processes input from the client and the program. Input data is stored 260 * in buffers and processed later. 261 */ 262static int 263process_input(struct ssh *ssh, int connection_in) 264{ 265 int r; 266 267 if ((r = ssh_packet_process_read(ssh, connection_in)) == 0) 268 return 0; /* success */ 269 if (r == SSH_ERR_SYSTEM_ERROR) { 270 if (errno == EAGAIN || errno == EINTR) 271 return 0; 272 if (errno == EPIPE) { 273 logit("Connection closed by %.100s port %d", 274 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); 275 return -1; 276 } 277 logit("Read error from remote host %s port %d: %s", 278 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), 279 strerror(errno)); 280 cleanup_exit(255); 281 } 282 return -1; 283} 284 285/* 286 * Sends data from internal buffers to client program stdin. 287 */ 288static void 289process_output(struct ssh *ssh, int connection_out) 290{ 291 int r; 292 static int interactive = -1; 293 294 /* Send any buffered packet data to the client. */ 295 if (interactive != !channel_has_bulk(ssh)) { 296 interactive = !channel_has_bulk(ssh); 297 debug2_f("session QoS is now %s", interactive ? 298 "interactive" : "non-interactive"); 299 ssh_packet_set_interactive(ssh, interactive); 300 } 301 if ((r = ssh_packet_write_poll(ssh)) != 0) { 302 sshpkt_fatal(ssh, r, "%s: ssh_packet_write_poll", 303 __func__); 304 } 305} 306 307static void 308process_buffered_input_packets(struct ssh *ssh) 309{ 310 ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, NULL); 311} 312 313static void 314collect_children(struct ssh *ssh) 315{ 316 pid_t pid; 317 int status; 318 319 if (child_terminated) { 320 debug("Received SIGCHLD."); 321 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || 322 (pid == -1 && errno == EINTR)) 323 if (pid > 0) 324 session_close_by_pid(ssh, pid, status); 325 child_terminated = 0; 326 } 327} 328 329void 330server_loop2(struct ssh *ssh, Authctxt *authctxt) 331{ 332 struct pollfd *pfd = NULL; 333 u_int npfd_alloc = 0, npfd_active = 0; 334 int r, conn_in_ready, conn_out_ready; 335 u_int connection_in, connection_out; 336 sigset_t bsigset, osigset; 337 338 debug("Entering interactive session for SSH2."); 339 340 if (sigemptyset(&bsigset) == -1 || 341 sigaddset(&bsigset, SIGCHLD) == -1 || 342 sigaddset(&bsigset, SIGINFO) == -1) 343 error_f("bsigset setup: %s", strerror(errno)); 344 ssh_signal(SIGCHLD, sigchld_handler); 345 ssh_signal(SIGINFO, siginfo_handler); 346 child_terminated = 0; 347 connection_in = ssh_packet_get_connection_in(ssh); 348 connection_out = ssh_packet_get_connection_out(ssh); 349 350 server_init_dispatch(ssh); 351 352 for (;;) { 353 process_buffered_input_packets(ssh); 354 355 if (!ssh_packet_is_rekeying(ssh) && 356 ssh_packet_not_very_much_data_to_write(ssh)) 357 channel_output_poll(ssh); 358 359 /* 360 * Block SIGCHLD while we check for dead children, then pass 361 * the old signal mask through to ppoll() so that it'll wake 362 * up immediately if a child exits after we've called waitpid(). 363 */ 364 if (sigprocmask(SIG_BLOCK, &bsigset, &osigset) == -1) 365 error_f("bsigset sigprocmask: %s", strerror(errno)); 366 collect_children(ssh); 367 if (siginfo_received) { 368 siginfo_received = 0; 369 channel_report_open(ssh, SYSLOG_LEVEL_INFO); 370 } 371 wait_until_can_do_something(ssh, connection_in, connection_out, 372 &pfd, &npfd_alloc, &npfd_active, &osigset, 373 &conn_in_ready, &conn_out_ready); 374 if (sigprocmask(SIG_SETMASK, &osigset, NULL) == -1) 375 error_f("osigset sigprocmask: %s", strerror(errno)); 376 377 channel_after_poll(ssh, pfd, npfd_active); 378 if (conn_in_ready && 379 process_input(ssh, connection_in) < 0) 380 break; 381 /* A timeout may have triggered rekeying */ 382 if ((r = ssh_packet_check_rekey(ssh)) != 0) 383 fatal_fr(r, "cannot start rekeying"); 384 if (conn_out_ready) 385 process_output(ssh, connection_out); 386 } 387 collect_children(ssh); 388 free(pfd); 389 390 /* free all channels, no more reads and writes */ 391 channel_free_all(ssh); 392 393 /* free remaining sessions, e.g. remove wtmp entries */ 394 session_destroy_all(ssh, NULL); 395} 396 397static int 398server_input_keep_alive(int type, u_int32_t seq, struct ssh *ssh) 399{ 400 debug("Got %d/%u for keepalive", type, seq); 401 /* 402 * reset timeout, since we got a sane answer from the client. 403 * even if this was generated by something other than 404 * the bogus CHANNEL_REQUEST we send for keepalives. 405 */ 406 ssh_packet_set_alive_timeouts(ssh, 0); 407 return 0; 408} 409 410static Channel * 411server_request_direct_tcpip(struct ssh *ssh, int *reason, const char **errmsg) 412{ 413 Channel *c = NULL; 414 char *target = NULL, *originator = NULL; 415 u_int target_port = 0, originator_port = 0; 416 int r; 417 418 if ((r = sshpkt_get_cstring(ssh, &target, NULL)) != 0 || 419 (r = sshpkt_get_u32(ssh, &target_port)) != 0 || 420 (r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 || 421 (r = sshpkt_get_u32(ssh, &originator_port)) != 0 || 422 (r = sshpkt_get_end(ssh)) != 0) 423 sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 424 if (target_port > 0xFFFF) { 425 error_f("invalid target port"); 426 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; 427 goto out; 428 } 429 if (originator_port > 0xFFFF) { 430 error_f("invalid originator port"); 431 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; 432 goto out; 433 } 434 435 debug_f("originator %s port %u, target %s port %u", 436 originator, originator_port, target, target_port); 437 438 /* XXX fine grained permissions */ 439 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 && 440 auth_opts->permit_port_forwarding_flag && 441 !options.disable_forwarding) { 442 c = channel_connect_to_port(ssh, target, target_port, 443 "direct-tcpip", "direct-tcpip", reason, errmsg); 444 } else { 445 logit("refused local port forward: " 446 "originator %s port %d, target %s port %d", 447 originator, originator_port, target, target_port); 448 if (reason != NULL) 449 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; 450 } 451 452 out: 453 free(originator); 454 free(target); 455 return c; 456} 457 458static Channel * 459server_request_direct_streamlocal(struct ssh *ssh) 460{ 461 Channel *c = NULL; 462 char *target = NULL, *originator = NULL; 463 u_int originator_port = 0; 464 struct passwd *pw = the_authctxt->pw; 465 int r; 466 467 if (pw == NULL || !the_authctxt->valid) 468 fatal_f("no/invalid user"); 469 470 if ((r = sshpkt_get_cstring(ssh, &target, NULL)) != 0 || 471 (r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 || 472 (r = sshpkt_get_u32(ssh, &originator_port)) != 0 || 473 (r = sshpkt_get_end(ssh)) != 0) 474 sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 475 if (originator_port > 0xFFFF) { 476 error_f("invalid originator port"); 477 goto out; 478 } 479 480 debug_f("originator %s port %d, target %s", 481 originator, originator_port, target); 482 483 /* XXX fine grained permissions */ 484 if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 && 485 auth_opts->permit_port_forwarding_flag && 486 !options.disable_forwarding) { 487 c = channel_connect_to_path(ssh, target, 488 "direct-streamlocal@openssh.com", "direct-streamlocal"); 489 } else { 490 logit("refused streamlocal port forward: " 491 "originator %s port %d, target %s", 492 originator, originator_port, target); 493 } 494 495out: 496 free(originator); 497 free(target); 498 return c; 499} 500 501static Channel * 502server_request_tun(struct ssh *ssh) 503{ 504 Channel *c = NULL; 505 u_int mode, tun; 506 int r, sock; 507 char *tmp, *ifname = NULL; 508 509 if ((r = sshpkt_get_u32(ssh, &mode)) != 0) 510 sshpkt_fatal(ssh, r, "%s: parse mode", __func__); 511 switch (mode) { 512 case SSH_TUNMODE_POINTOPOINT: 513 case SSH_TUNMODE_ETHERNET: 514 break; 515 default: 516 ssh_packet_send_debug(ssh, "Unsupported tunnel device mode."); 517 return NULL; 518 } 519 if ((options.permit_tun & mode) == 0) { 520 ssh_packet_send_debug(ssh, "Server has rejected tunnel device " 521 "forwarding"); 522 return NULL; 523 } 524 525 if ((r = sshpkt_get_u32(ssh, &tun)) != 0) 526 sshpkt_fatal(ssh, r, "%s: parse device", __func__); 527 if (tun > INT_MAX) { 528 debug_f("invalid tun"); 529 goto done; 530 } 531 if (auth_opts->force_tun_device != -1) { 532 if (tun != SSH_TUNID_ANY && 533 auth_opts->force_tun_device != (int)tun) 534 goto done; 535 tun = auth_opts->force_tun_device; 536 } 537 sock = tun_open(tun, mode, &ifname); 538 if (sock < 0) 539 goto done; 540 debug("Tunnel forwarding using interface %s", ifname); 541 542 c = channel_new(ssh, "tun", SSH_CHANNEL_OPEN, sock, sock, -1, 543 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); 544 c->datagram = 1; 545 546 /* 547 * Update the list of names exposed to the session 548 * XXX remove these if the tunnels are closed (won't matter 549 * much if they are already in the environment though) 550 */ 551 tmp = tun_fwd_ifnames; 552 xasprintf(&tun_fwd_ifnames, "%s%s%s", 553 tun_fwd_ifnames == NULL ? "" : tun_fwd_ifnames, 554 tun_fwd_ifnames == NULL ? "" : ",", 555 ifname); 556 free(tmp); 557 free(ifname); 558 559 done: 560 if (c == NULL) 561 ssh_packet_send_debug(ssh, "Failed to open the tunnel device."); 562 return c; 563} 564 565static Channel * 566server_request_session(struct ssh *ssh) 567{ 568 Channel *c; 569 int r; 570 571 debug("input_session_request"); 572 if ((r = sshpkt_get_end(ssh)) != 0) 573 sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 574 575 if (no_more_sessions) { 576 ssh_packet_disconnect(ssh, "Possible attack: attempt to open a " 577 "session after additional sessions disabled"); 578 } 579 580 /* 581 * A server session has no fd to read or write until a 582 * CHANNEL_REQUEST for a shell is made, so we set the type to 583 * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all 584 * CHANNEL_REQUEST messages is registered. 585 */ 586 c = channel_new(ssh, "session", SSH_CHANNEL_LARVAL, 587 -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT, 588 0, "server-session", 1); 589 if (session_open(the_authctxt, c->self) != 1) { 590 debug("session open failed, free channel %d", c->self); 591 channel_free(ssh, c); 592 return NULL; 593 } 594 channel_register_cleanup(ssh, c->self, session_close_by_channel, 0); 595 return c; 596} 597 598static int 599server_input_channel_open(int type, u_int32_t seq, struct ssh *ssh) 600{ 601 Channel *c = NULL; 602 char *ctype = NULL; 603 const char *errmsg = NULL; 604 int r, reason = SSH2_OPEN_CONNECT_FAILED; 605 u_int rchan = 0, rmaxpack = 0, rwindow = 0; 606 607 if ((r = sshpkt_get_cstring(ssh, &ctype, NULL)) != 0 || 608 (r = sshpkt_get_u32(ssh, &rchan)) != 0 || 609 (r = sshpkt_get_u32(ssh, &rwindow)) != 0 || 610 (r = sshpkt_get_u32(ssh, &rmaxpack)) != 0) 611 sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 612 debug_f("ctype %s rchan %u win %u max %u", 613 ctype, rchan, rwindow, rmaxpack); 614 615 if (strcmp(ctype, "session") == 0) { 616 c = server_request_session(ssh); 617 } else if (strcmp(ctype, "direct-tcpip") == 0) { 618 c = server_request_direct_tcpip(ssh, &reason, &errmsg); 619 } else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) { 620 c = server_request_direct_streamlocal(ssh); 621 } else if (strcmp(ctype, "tun@openssh.com") == 0) { 622 c = server_request_tun(ssh); 623 } 624 if (c != NULL) { 625 debug_f("confirm %s", ctype); 626 c->remote_id = rchan; 627 c->have_remote_id = 1; 628 c->remote_window = rwindow; 629 c->remote_maxpacket = rmaxpack; 630 if (c->type != SSH_CHANNEL_CONNECTING) { 631 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || 632 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || 633 (r = sshpkt_put_u32(ssh, c->self)) != 0 || 634 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || 635 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 || 636 (r = sshpkt_send(ssh)) != 0) { 637 sshpkt_fatal(ssh, r, 638 "%s: send open confirm", __func__); 639 } 640 } 641 } else { 642 debug_f("failure %s", ctype); 643 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 || 644 (r = sshpkt_put_u32(ssh, rchan)) != 0 || 645 (r = sshpkt_put_u32(ssh, reason)) != 0 || 646 (r = sshpkt_put_cstring(ssh, errmsg ? errmsg : "open failed")) != 0 || 647 (r = sshpkt_put_cstring(ssh, "")) != 0 || 648 (r = sshpkt_send(ssh)) != 0) { 649 sshpkt_fatal(ssh, r, 650 "%s: send open failure", __func__); 651 } 652 } 653 free(ctype); 654 return 0; 655} 656 657static int 658server_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, success = 0; 664 const u_char *blob; 665 const char *sigalg, *kex_rsa_sigalg = NULL; 666 u_char *sig = NULL; 667 size_t blen, slen; 668 669 if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL) 670 fatal_f("sshbuf_new"); 671 if (sshkey_type_plain(sshkey_type_from_name( 672 ssh->kex->hostkey_alg)) == KEY_RSA) 673 kex_rsa_sigalg = ssh->kex->hostkey_alg; 674 while (ssh_packet_remaining(ssh) > 0) { 675 sshkey_free(key); 676 key = NULL; 677 if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 || 678 (r = sshkey_from_blob(blob, blen, &key)) != 0) { 679 error_fr(r, "parse key"); 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_f("unknown host %s key", sshkey_type(key)); 688 goto out; 689 } 690 /* 691 * XXX refactor: make kex->sign just use an index rather 692 * than passing in public and private keys 693 */ 694 if ((key_prv = get_hostkey_by_index(ndx)) == NULL && 695 (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) { 696 error_f("can't retrieve hostkey %d", ndx); 697 goto out; 698 } 699 sshbuf_reset(sigbuf); 700 free(sig); 701 sig = NULL; 702 /* 703 * For RSA keys, prefer to use the signature type negotiated 704 * during KEX to the default (SHA1). 705 */ 706 sigalg = sshkey_ssh_name(key); 707 if (sshkey_type_plain(key->type) == KEY_RSA) { 708 if (kex_rsa_sigalg != NULL) 709 sigalg = kex_rsa_sigalg; 710 else if (ssh->kex->flags & KEX_RSA_SHA2_512_SUPPORTED) 711 sigalg = "rsa-sha2-512"; 712 else if (ssh->kex->flags & KEX_RSA_SHA2_256_SUPPORTED) 713 sigalg = "rsa-sha2-256"; 714 } 715 716 debug3_f("sign %s key (index %d) using sigalg %s", 717 sshkey_type(key), ndx, sigalg == NULL ? "default" : sigalg); 718 if ((r = sshbuf_put_cstring(sigbuf, 719 "hostkeys-prove-00@openssh.com")) != 0 || 720 (r = sshbuf_put_stringb(sigbuf, 721 ssh->kex->session_id)) != 0 || 722 (r = sshkey_puts(key, sigbuf)) != 0 || 723 (r = ssh->kex->sign(ssh, key_prv, key_pub, &sig, &slen, 724 sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), sigalg)) != 0 || 725 (r = sshbuf_put_string(resp, sig, slen)) != 0) { 726 error_fr(r, "assemble signature"); 727 goto out; 728 } 729 } 730 /* Success */ 731 *respp = resp; 732 resp = NULL; /* don't free it */ 733 success = 1; 734 out: 735 free(sig); 736 sshbuf_free(resp); 737 sshbuf_free(sigbuf); 738 sshkey_free(key); 739 return success; 740} 741 742static int 743server_input_global_request(int type, u_int32_t seq, struct ssh *ssh) 744{ 745 char *rtype = NULL; 746 u_char want_reply = 0; 747 int r, success = 0, allocated_listen_port = 0; 748 u_int port = 0; 749 struct sshbuf *resp = NULL; 750 struct passwd *pw = the_authctxt->pw; 751 struct Forward fwd; 752 753 memset(&fwd, 0, sizeof(fwd)); 754 if (pw == NULL || !the_authctxt->valid) 755 fatal_f("no/invalid user"); 756 757 if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 || 758 (r = sshpkt_get_u8(ssh, &want_reply)) != 0) 759 sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 760 debug_f("rtype %s want_reply %d", rtype, want_reply); 761 762 /* -R style forwarding */ 763 if (strcmp(rtype, "tcpip-forward") == 0) { 764 if ((r = sshpkt_get_cstring(ssh, &fwd.listen_host, NULL)) != 0 || 765 (r = sshpkt_get_u32(ssh, &port)) != 0) 766 sshpkt_fatal(ssh, r, "%s: parse tcpip-forward", __func__); 767 debug_f("tcpip-forward listen %s port %u", 768 fwd.listen_host, port); 769 if (port <= INT_MAX) 770 fwd.listen_port = (int)port; 771 /* check permissions */ 772 if (port > INT_MAX || 773 (options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 || 774 !auth_opts->permit_port_forwarding_flag || 775 options.disable_forwarding || 776 (!want_reply && fwd.listen_port == 0)) { 777 success = 0; 778 ssh_packet_send_debug(ssh, "Server has disabled port forwarding."); 779 } else { 780 /* Start listening on the port */ 781 success = channel_setup_remote_fwd_listener(ssh, &fwd, 782 &allocated_listen_port, &options.fwd_opts); 783 } 784 if ((resp = sshbuf_new()) == NULL) 785 fatal_f("sshbuf_new"); 786 if (allocated_listen_port != 0 && 787 (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0) 788 fatal_fr(r, "sshbuf_put_u32"); 789 } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) { 790 if ((r = sshpkt_get_cstring(ssh, &fwd.listen_host, NULL)) != 0 || 791 (r = sshpkt_get_u32(ssh, &port)) != 0) 792 sshpkt_fatal(ssh, r, "%s: parse cancel-tcpip-forward", __func__); 793 794 debug_f("cancel-tcpip-forward addr %s port %d", 795 fwd.listen_host, port); 796 if (port <= INT_MAX) { 797 fwd.listen_port = (int)port; 798 success = channel_cancel_rport_listener(ssh, &fwd); 799 } 800 } else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) { 801 if ((r = sshpkt_get_cstring(ssh, &fwd.listen_path, NULL)) != 0) 802 sshpkt_fatal(ssh, r, "%s: parse streamlocal-forward@openssh.com", __func__); 803 debug_f("streamlocal-forward listen path %s", 804 fwd.listen_path); 805 806 /* check permissions */ 807 if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0 808 || !auth_opts->permit_port_forwarding_flag || 809 options.disable_forwarding) { 810 success = 0; 811 ssh_packet_send_debug(ssh, "Server has disabled " 812 "streamlocal forwarding."); 813 } else { 814 /* Start listening on the socket */ 815 success = channel_setup_remote_fwd_listener(ssh, 816 &fwd, NULL, &options.fwd_opts); 817 } 818 } else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) { 819 if ((r = sshpkt_get_cstring(ssh, &fwd.listen_path, NULL)) != 0) 820 sshpkt_fatal(ssh, r, "%s: parse cancel-streamlocal-forward@openssh.com", __func__); 821 debug_f("cancel-streamlocal-forward path %s", 822 fwd.listen_path); 823 824 success = channel_cancel_rport_listener(ssh, &fwd); 825 } else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) { 826 no_more_sessions = 1; 827 success = 1; 828 } else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) { 829 success = server_input_hostkeys_prove(ssh, &resp); 830 } 831 /* XXX sshpkt_get_end() */ 832 if (want_reply) { 833 if ((r = sshpkt_start(ssh, success ? 834 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE)) != 0 || 835 (success && resp != NULL && (r = sshpkt_putb(ssh, resp)) != 0) || 836 (r = sshpkt_send(ssh)) != 0 || 837 (r = ssh_packet_write_wait(ssh)) != 0) 838 sshpkt_fatal(ssh, r, "%s: send reply", __func__); 839 } 840 free(fwd.listen_host); 841 free(fwd.listen_path); 842 free(rtype); 843 sshbuf_free(resp); 844 return 0; 845} 846 847static int 848server_input_channel_req(int type, u_int32_t seq, struct ssh *ssh) 849{ 850 Channel *c; 851 int r, success = 0; 852 char *rtype = NULL; 853 u_char want_reply = 0; 854 u_int id = 0; 855 856 if ((r = sshpkt_get_u32(ssh, &id)) != 0 || 857 (r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 || 858 (r = sshpkt_get_u8(ssh, &want_reply)) != 0) 859 sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 860 861 debug("server_input_channel_req: channel %u request %s reply %d", 862 id, rtype, want_reply); 863 864 if (id >= INT_MAX || (c = channel_lookup(ssh, (int)id)) == NULL) { 865 ssh_packet_disconnect(ssh, "%s: unknown channel %d", 866 __func__, id); 867 } 868 if (!strcmp(rtype, "eow@openssh.com")) { 869 if ((r = sshpkt_get_end(ssh)) != 0) 870 sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 871 chan_rcvd_eow(ssh, c); 872 } else if ((c->type == SSH_CHANNEL_LARVAL || 873 c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0) 874 success = session_input_channel_req(ssh, c, rtype); 875 if (want_reply && !(c->flags & CHAN_CLOSE_SENT)) { 876 if (!c->have_remote_id) 877 fatal_f("channel %d: no remote_id", c->self); 878 if ((r = sshpkt_start(ssh, success ? 879 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE)) != 0 || 880 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || 881 (r = sshpkt_send(ssh)) != 0) 882 sshpkt_fatal(ssh, r, "%s: send reply", __func__); 883 } 884 free(rtype); 885 return 0; 886} 887 888static void 889server_init_dispatch(struct ssh *ssh) 890{ 891 debug("server_init_dispatch"); 892 ssh_dispatch_init(ssh, &dispatch_protocol_error); 893 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); 894 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_DATA, &channel_input_data); 895 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); 896 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); 897 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open); 898 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); 899 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); 900 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req); 901 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); 902 ssh_dispatch_set(ssh, SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request); 903 /* client_alive */ 904 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive); 905 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive); 906 ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive); 907 ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive); 908 /* rekeying */ 909 ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit); 910}