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