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