1 /* $OpenBSD: sshd-session.c,v 1.24 2026/06/14 03:59:34 djm Exp $ */ 2 /* 3 * SSH2 implementation: 4 * Privilege Separation: 5 * 6 * Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved. 7 * Copyright (c) 2002 Niels Provos. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include "includes.h" 31 32 #include <sys/types.h> 33 #include <sys/ioctl.h> 34 #include <sys/wait.h> 35 #include <sys/tree.h> 36 #include <sys/stat.h> 37 #include <sys/socket.h> 38 #include <sys/time.h> 39 #include <sys/queue.h> 40 41 #include <errno.h> 42 #include <fcntl.h> 43 #include <netdb.h> 44 #include <paths.h> 45 #include <pwd.h> 46 #include <grp.h> 47 #include <signal.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <stdarg.h> 52 #include <unistd.h> 53 #include <limits.h> 54 55 #ifdef HAVE_SECUREWARE 56 #include <sys/security.h> 57 #include <prot.h> 58 #endif 59 60 #include "xmalloc.h" 61 #include "ssh.h" 62 #include "ssh2.h" 63 #include "sshpty.h" 64 #include "packet.h" 65 #include "log.h" 66 #include "sshbuf.h" 67 #include "misc.h" 68 #include "match.h" 69 #include "servconf.h" 70 #include "uidswap.h" 71 #include "compat.h" 72 #include "cipher.h" 73 #include "digest.h" 74 #include "sshkey.h" 75 #include "kex.h" 76 #include "authfile.h" 77 #include "pathnames.h" 78 #include "atomicio.h" 79 #include "canohost.h" 80 #include "hostfile.h" 81 #include "auth.h" 82 #include "authfd.h" 83 #include "msg.h" 84 #include "dispatch.h" 85 #include "channels.h" 86 #include "session.h" 87 #include "monitor.h" 88 #ifdef GSSAPI 89 #include "ssh-gss.h" 90 #endif 91 #include "monitor_wrap.h" 92 #include "auth-options.h" 93 #include "version.h" 94 #include "ssherr.h" 95 #include "sk-api.h" 96 #include "srclimit.h" 97 #include "dh.h" 98 #include "blocklist_client.h" 99 100 /* Re-exec fds */ 101 #define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1) 102 #define REEXEC_CONFIG_PASS_FD (STDERR_FILENO + 2) 103 #define REEXEC_MIN_FREE_FD (STDERR_FILENO + 3) 104 105 /* Privsep fds */ 106 #define PRIVSEP_MONITOR_FD (STDERR_FILENO + 1) 107 #define PRIVSEP_LOG_FD (STDERR_FILENO + 2) 108 #define PRIVSEP_MIN_FREE_FD (STDERR_FILENO + 3) 109 110 extern char *__progname; 111 112 /* Server configuration options. */ 113 ServerOptions options; 114 115 /* Name of the server configuration file. */ 116 char *config_file_name = _PATH_SERVER_CONFIG_FILE; 117 118 /* 119 * Debug mode flag. This can be set on the command line. If debug 120 * mode is enabled, extra debugging output will be sent to the system 121 * log, the daemon will not go to background, and will exit after processing 122 * the first connection. 123 */ 124 int debug_flag = 0; 125 126 /* Flag indicating that the daemon is being started from inetd. */ 127 static int inetd_flag = 0; 128 129 /* debug goes to stderr unless inetd_flag is set */ 130 static int log_stderr = 0; 131 132 /* Saved arguments to main(). */ 133 static char **saved_argv; 134 static int saved_argc; 135 136 /* Daemon's agent connection */ 137 int auth_sock = -1; 138 static int have_agent = 0; 139 140 /* 141 * Any really sensitive data in the application is contained in this 142 * structure. The idea is that this structure could be locked into memory so 143 * that the pages do not get written into swap. However, there are some 144 * problems. The private key contains BIGNUMs, and we do not (in principle) 145 * have access to the internals of them, and locking just the structure is 146 * not very useful. Currently, memory locking is not implemented. 147 */ 148 struct { 149 u_int num_hostkeys; 150 struct sshkey **host_keys; /* all private host keys */ 151 struct sshkey **host_pubkeys; /* all public host keys */ 152 struct sshkey **host_certificates; /* all public host certificates */ 153 } sensitive_data; 154 155 /* record remote hostname or ip */ 156 u_int utmp_len = HOST_NAME_MAX+1; 157 158 static int startup_pipe = -1; /* in child */ 159 160 /* variables used for privilege separation */ 161 struct monitor *pmonitor = NULL; 162 int privsep_is_preauth = 1; 163 static int privsep_chroot = 1; 164 165 /* Unprivileged user */ 166 struct passwd *privsep_pw = NULL; 167 168 /* global connection state and authentication contexts */ 169 Authctxt *the_authctxt = NULL; 170 struct ssh *the_active_state; 171 172 /* global key/cert auth options. XXX move to permanent ssh->authctxt? */ 173 struct sshauthopt *auth_opts = NULL; 174 175 /* sshd_config buffer */ 176 struct sshbuf *cfg; 177 178 /* Included files from the configuration file */ 179 struct include_list includes = TAILQ_HEAD_INITIALIZER(includes); 180 181 /* message to be displayed after login */ 182 struct sshbuf *loginmsg; 183 184 /* Prototypes for various functions defined later in this file. */ 185 void destroy_sensitive_data(void); 186 void demote_sensitive_data(void); 187 188 /* XXX reduce to stub once postauth split */ 189 int 190 mm_is_monitor(void) 191 { 192 /* 193 * m_pid is only set in the privileged part, and 194 * points to the unprivileged child. 195 */ 196 return (pmonitor && pmonitor->m_pid > 0); 197 } 198 199 /* 200 * Signal handler for the alarm after the login grace period has expired. 201 * As usual, this may only take signal-safe actions, even though it is 202 * terminal. 203 */ 204 static void 205 grace_alarm_handler(int sig) 206 { 207 /* 208 * Try to kill any processes that we have spawned, E.g. authorized 209 * keys command helpers or privsep children. 210 */ 211 if (getpgid(0) == getpid()) { 212 struct sigaction sa; 213 214 /* mask all other signals while in handler */ 215 memset(&sa, 0, sizeof(sa)); 216 sa.sa_handler = SIG_IGN; 217 sigfillset(&sa.sa_mask); 218 #if defined(SA_RESTART) 219 sa.sa_flags = SA_RESTART; 220 #endif 221 (void)sigaction(SIGTERM, &sa, NULL); 222 kill(0, SIGTERM); 223 } 224 _exit(EXIT_LOGIN_GRACE); 225 } 226 227 /* Destroy the host and server keys. They will no longer be needed. */ 228 void 229 destroy_sensitive_data(void) 230 { 231 u_int i; 232 233 for (i = 0; i < options.num_host_key_files; i++) { 234 if (sensitive_data.host_keys[i]) { 235 sshkey_free(sensitive_data.host_keys[i]); 236 sensitive_data.host_keys[i] = NULL; 237 } 238 if (sensitive_data.host_certificates[i]) { 239 sshkey_free(sensitive_data.host_certificates[i]); 240 sensitive_data.host_certificates[i] = NULL; 241 } 242 } 243 } 244 245 /* Demote private to public keys for network child */ 246 void 247 demote_sensitive_data(void) 248 { 249 struct sshkey *tmp; 250 u_int i; 251 int r; 252 253 for (i = 0; i < options.num_host_key_files; i++) { 254 if (sensitive_data.host_keys[i]) { 255 if ((r = sshkey_from_private( 256 sensitive_data.host_keys[i], &tmp)) != 0) 257 fatal_r(r, "could not demote host %s key", 258 sshkey_type(sensitive_data.host_keys[i])); 259 sshkey_free(sensitive_data.host_keys[i]); 260 sensitive_data.host_keys[i] = tmp; 261 } 262 /* Certs do not need demotion */ 263 } 264 } 265 266 struct sshbuf * 267 pack_hostkeys(void) 268 { 269 struct sshbuf *keybuf = NULL, *hostkeys = NULL; 270 int r; 271 u_int i; 272 273 if ((hostkeys = sshbuf_new()) == NULL) 274 fatal_f("sshbuf_new failed"); 275 276 /* pack hostkeys into a string. Empty key slots get empty strings */ 277 for (i = 0; i < options.num_host_key_files; i++) { 278 /* public key */ 279 if (sensitive_data.host_pubkeys[i] != NULL) { 280 if ((r = sshkey_puts(sensitive_data.host_pubkeys[i], 281 hostkeys)) != 0) 282 fatal_fr(r, "compose hostkey public"); 283 } else { 284 if ((r = sshbuf_put_string(hostkeys, NULL, 0)) != 0) 285 fatal_fr(r, "compose hostkey empty public"); 286 } 287 /* cert */ 288 if (sensitive_data.host_certificates[i] != NULL) { 289 if ((r = sshkey_puts( 290 sensitive_data.host_certificates[i], 291 hostkeys)) != 0) 292 fatal_fr(r, "compose host cert"); 293 } else { 294 if ((r = sshbuf_put_string(hostkeys, NULL, 0)) != 0) 295 fatal_fr(r, "compose host cert empty"); 296 } 297 } 298 299 sshbuf_free(keybuf); 300 return hostkeys; 301 } 302 303 static int 304 privsep_preauth(struct ssh *ssh) 305 { 306 int r; 307 pid_t pid; 308 309 /* Set up unprivileged child process to deal with network data */ 310 pmonitor = monitor_init(); 311 /* Store a pointer to the kex for later rekeying */ 312 pmonitor->m_pkex = &ssh->kex; 313 314 if ((pid = fork()) == -1) 315 fatal("fork of unprivileged child failed"); 316 else if (pid != 0) { 317 debug2("Network child is on pid %ld", (long)pid); 318 319 pmonitor->m_pid = pid; 320 if (have_agent) { 321 r = ssh_get_authentication_socket(&auth_sock); 322 if (r != 0) { 323 error_r(r, "Could not get agent socket"); 324 have_agent = 0; 325 } 326 } 327 monitor_child_preauth(ssh, pmonitor); 328 privsep_is_preauth = 0; 329 return 1; 330 } else { 331 /* child */ 332 close(pmonitor->m_sendfd); 333 close(pmonitor->m_log_recvfd); 334 335 /* 336 * Arrange unpriv-preauth child process fds: 337 * 0, 1 network socket 338 * 2 optional stderr 339 * 3 reserved 340 * 4 monitor message socket 341 * 5 monitor logging socket 342 * 343 * We know that the monitor sockets will have fds > 4 because 344 * of the reserved fds in main() 345 */ 346 347 if (ssh_packet_get_connection_in(ssh) != STDIN_FILENO && 348 dup2(ssh_packet_get_connection_in(ssh), STDIN_FILENO) == -1) 349 fatal("dup2 stdin failed: %s", strerror(errno)); 350 if (ssh_packet_get_connection_out(ssh) != STDOUT_FILENO && 351 dup2(ssh_packet_get_connection_out(ssh), 352 STDOUT_FILENO) == -1) 353 fatal("dup2 stdout failed: %s", strerror(errno)); 354 /* leave stderr as-is */ 355 log_redirect_stderr_to(NULL); /* dup can clobber log fd */ 356 if (pmonitor->m_recvfd != PRIVSEP_MONITOR_FD && 357 dup2(pmonitor->m_recvfd, PRIVSEP_MONITOR_FD) == -1) 358 fatal("dup2 monitor fd: %s", strerror(errno)); 359 if (pmonitor->m_log_sendfd != PRIVSEP_LOG_FD && 360 dup2(pmonitor->m_log_sendfd, PRIVSEP_LOG_FD) == -1) 361 fatal("dup2 log fd: %s", strerror(errno)); 362 closefrom(PRIVSEP_MIN_FREE_FD); 363 364 saved_argv[0] = options.sshd_auth_path; 365 execv(options.sshd_auth_path, saved_argv); 366 367 fatal_f("exec of %s failed: %s", 368 options.sshd_auth_path, strerror(errno)); 369 } 370 } 371 372 static void 373 privsep_postauth(struct ssh *ssh, Authctxt *authctxt) 374 { 375 int skip_privdrop = 0; 376 377 /* 378 * Hack for systems that don't support FD passing: retain privileges 379 * in the post-auth privsep process so it can allocate PTYs directly. 380 * This is basically equivalent to what we did <= 9.7, which was to 381 * disable post-auth privsep entirely. 382 * Cygwin doesn't need to drop privs here although it doesn't support 383 * fd passing, as AFAIK PTY allocation on this platform doesn't require 384 * special privileges to begin with. 385 */ 386 #if defined(DISABLE_FD_PASSING) && !defined(HAVE_CYGWIN) 387 skip_privdrop = 1; 388 #endif 389 390 /* New socket pair */ 391 monitor_reinit(pmonitor); 392 393 pmonitor->m_pid = fork(); 394 if (pmonitor->m_pid == -1) 395 fatal("fork of unprivileged child failed"); 396 else if (pmonitor->m_pid != 0) { 397 verbose("User child is on pid %ld", (long)pmonitor->m_pid); 398 sshbuf_reset(loginmsg); 399 monitor_clear_keystate(ssh, pmonitor); 400 monitor_child_postauth(ssh, pmonitor); 401 402 /* NEVERREACHED */ 403 exit(0); 404 } 405 406 /* child */ 407 408 close(pmonitor->m_sendfd); 409 pmonitor->m_sendfd = -1; 410 411 /* Demote the private keys to public keys. */ 412 demote_sensitive_data(); 413 414 reseed_prngs(); 415 416 /* Drop privileges */ 417 if (!skip_privdrop) 418 do_setusercontext(authctxt->pw); 419 420 /* It is safe now to apply the key state */ 421 monitor_apply_keystate(ssh, pmonitor); 422 423 /* 424 * Tell the packet layer that authentication was successful, since 425 * this information is not part of the key state. 426 */ 427 ssh_packet_set_authenticated(ssh); 428 } 429 430 static struct sshkey * 431 get_hostkey_by_type(int type, int nid, int need_private, struct ssh *ssh) 432 { 433 u_int i; 434 struct sshkey *key; 435 436 for (i = 0; i < options.num_host_key_files; i++) { 437 switch (type) { 438 case KEY_RSA_CERT: 439 case KEY_ECDSA_CERT: 440 case KEY_ED25519_CERT: 441 case KEY_ECDSA_SK_CERT: 442 case KEY_ED25519_SK_CERT: 443 case KEY_MLDSA44_ED25519_CERT: 444 key = sensitive_data.host_certificates[i]; 445 break; 446 default: 447 key = sensitive_data.host_keys[i]; 448 if (key == NULL && !need_private) 449 key = sensitive_data.host_pubkeys[i]; 450 break; 451 } 452 if (key == NULL || key->type != type) 453 continue; 454 switch (type) { 455 case KEY_ECDSA: 456 case KEY_ECDSA_SK: 457 case KEY_ECDSA_CERT: 458 case KEY_ECDSA_SK_CERT: 459 if (key->ecdsa_nid != nid) 460 continue; 461 /* FALLTHROUGH */ 462 default: 463 return need_private ? 464 sensitive_data.host_keys[i] : key; 465 } 466 } 467 return NULL; 468 } 469 470 struct sshkey * 471 get_hostkey_public_by_type(int type, int nid, struct ssh *ssh) 472 { 473 return get_hostkey_by_type(type, nid, 0, ssh); 474 } 475 476 struct sshkey * 477 get_hostkey_private_by_type(int type, int nid, struct ssh *ssh) 478 { 479 return get_hostkey_by_type(type, nid, 1, ssh); 480 } 481 482 struct sshkey * 483 get_hostkey_by_index(int ind) 484 { 485 if (ind < 0 || (u_int)ind >= options.num_host_key_files) 486 return (NULL); 487 return (sensitive_data.host_keys[ind]); 488 } 489 490 struct sshkey * 491 get_hostkey_public_by_index(int ind, struct ssh *ssh) 492 { 493 if (ind < 0 || (u_int)ind >= options.num_host_key_files) 494 return (NULL); 495 return (sensitive_data.host_pubkeys[ind]); 496 } 497 498 int 499 get_hostkey_index(struct sshkey *key, int compare, struct ssh *ssh) 500 { 501 u_int i; 502 503 for (i = 0; i < options.num_host_key_files; i++) { 504 if (sshkey_is_cert(key)) { 505 if (key == sensitive_data.host_certificates[i] || 506 (compare && sensitive_data.host_certificates[i] && 507 sshkey_equal(key, 508 sensitive_data.host_certificates[i]))) 509 return (i); 510 } else { 511 if (key == sensitive_data.host_keys[i] || 512 (compare && sensitive_data.host_keys[i] && 513 sshkey_equal(key, sensitive_data.host_keys[i]))) 514 return (i); 515 if (key == sensitive_data.host_pubkeys[i] || 516 (compare && sensitive_data.host_pubkeys[i] && 517 sshkey_equal(key, sensitive_data.host_pubkeys[i]))) 518 return (i); 519 } 520 } 521 return (-1); 522 } 523 524 /* Inform the client of all hostkeys */ 525 static void 526 notify_hostkeys(struct ssh *ssh) 527 { 528 struct sshbuf *buf; 529 struct sshkey *key; 530 u_int i, nkeys; 531 int r; 532 char *fp; 533 534 /* Some clients cannot cope with the hostkeys message, skip those. */ 535 if (ssh->compat & SSH_BUG_HOSTKEYS) 536 return; 537 538 if ((buf = sshbuf_new()) == NULL) 539 fatal_f("sshbuf_new"); 540 for (i = nkeys = 0; i < options.num_host_key_files; i++) { 541 key = get_hostkey_public_by_index(i, ssh); 542 if (key == NULL || key->type == KEY_UNSPEC || 543 sshkey_is_cert(key)) 544 continue; 545 fp = sshkey_fingerprint(key, options.fingerprint_hash, 546 SSH_FP_DEFAULT); 547 debug3_f("key %d: %s %s", i, sshkey_ssh_name(key), fp); 548 free(fp); 549 if (nkeys == 0) { 550 /* 551 * Start building the request when we find the 552 * first usable key. 553 */ 554 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || 555 (r = sshpkt_put_cstring(ssh, "hostkeys-00@openssh.com")) != 0 || 556 (r = sshpkt_put_u8(ssh, 0)) != 0) /* want reply */ 557 sshpkt_fatal(ssh, r, "%s: start request", __func__); 558 } 559 /* Append the key to the request */ 560 sshbuf_reset(buf); 561 if ((r = sshkey_putb(key, buf)) != 0) 562 fatal_fr(r, "couldn't put hostkey %d", i); 563 if ((r = sshpkt_put_stringb(ssh, buf)) != 0) 564 sshpkt_fatal(ssh, r, "%s: append key", __func__); 565 nkeys++; 566 } 567 debug3_f("sent %u hostkeys", nkeys); 568 if (nkeys == 0) 569 fatal_f("no hostkeys"); 570 if ((r = sshpkt_send(ssh)) != 0) 571 sshpkt_fatal(ssh, r, "%s: send", __func__); 572 sshbuf_free(buf); 573 } 574 575 static void 576 usage(void) 577 { 578 fprintf(stderr, "%s, %s\n", SSH_RELEASE, SSH_OPENSSL_VERSION); 579 fprintf(stderr, 580 "usage: sshd [-46DdeGiqTtV] [-C connection_spec] [-c host_cert_file]\n" 581 " [-E log_file] [-f config_file] [-g login_grace_time]\n" 582 " [-h host_key_file] [-o option] [-p port] [-u len]\n" 583 ); 584 exit(1); 585 } 586 587 static void 588 parse_hostkeys(struct sshbuf *hostkeys) 589 { 590 int r; 591 u_int num_keys = 0; 592 struct sshkey *k; 593 struct sshbuf *kbuf; 594 const u_char *cp; 595 size_t len; 596 597 while (sshbuf_len(hostkeys) != 0) { 598 if (num_keys > 2048) 599 fatal_f("too many hostkeys"); 600 sensitive_data.host_keys = xrecallocarray( 601 sensitive_data.host_keys, num_keys, num_keys + 1, 602 sizeof(*sensitive_data.host_pubkeys)); 603 sensitive_data.host_pubkeys = xrecallocarray( 604 sensitive_data.host_pubkeys, num_keys, num_keys + 1, 605 sizeof(*sensitive_data.host_pubkeys)); 606 sensitive_data.host_certificates = xrecallocarray( 607 sensitive_data.host_certificates, num_keys, num_keys + 1, 608 sizeof(*sensitive_data.host_certificates)); 609 /* private key */ 610 k = NULL; 611 if ((r = sshbuf_froms(hostkeys, &kbuf)) != 0) 612 fatal_fr(r, "extract privkey"); 613 if (sshbuf_len(kbuf) != 0 && 614 (r = sshkey_private_deserialize(kbuf, &k)) != 0) 615 fatal_fr(r, "parse pubkey"); 616 sensitive_data.host_keys[num_keys] = k; 617 sshbuf_free(kbuf); 618 if (k) 619 debug2_f("privkey %u: %s", num_keys, sshkey_ssh_name(k)); 620 /* public key */ 621 k = NULL; 622 if ((r = sshbuf_get_string_direct(hostkeys, &cp, &len)) != 0) 623 fatal_fr(r, "extract pubkey"); 624 if (len != 0 && (r = sshkey_from_blob(cp, len, &k)) != 0) 625 fatal_fr(r, "parse pubkey"); 626 sensitive_data.host_pubkeys[num_keys] = k; 627 if (k) 628 debug2_f("pubkey %u: %s", num_keys, sshkey_ssh_name(k)); 629 /* certificate */ 630 k = NULL; 631 if ((r = sshbuf_get_string_direct(hostkeys, &cp, &len)) != 0) 632 fatal_fr(r, "extract pubkey"); 633 if (len != 0 && (r = sshkey_from_blob(cp, len, &k)) != 0) 634 fatal_fr(r, "parse pubkey"); 635 sensitive_data.host_certificates[num_keys] = k; 636 if (k) 637 debug2_f("cert %u: %s", num_keys, sshkey_ssh_name(k)); 638 num_keys++; 639 } 640 sensitive_data.num_hostkeys = num_keys; 641 } 642 643 static void 644 recv_rexec_state(int fd, struct sshbuf *conf, uint64_t *timing_secretp) 645 { 646 struct sshbuf *m, *inc, *hostkeys; 647 u_char *cp, ver; 648 size_t len; 649 int r; 650 struct include_item *item; 651 652 debug3_f("entering fd = %d", fd); 653 654 if ((m = sshbuf_new()) == NULL || (inc = sshbuf_new()) == NULL) 655 fatal_f("sshbuf_new failed"); 656 657 /* receive config */ 658 if (ssh_msg_recv(fd, m) == -1) 659 fatal_f("ssh_msg_recv failed"); 660 if ((r = sshbuf_get_u8(m, &ver)) != 0) 661 fatal_fr(r, "parse version"); 662 if (ver != 0) 663 fatal_f("rexec version mismatch"); 664 if ((r = sshbuf_get_string(m, &cp, &len)) != 0 || /* XXX _direct */ 665 (r = sshbuf_get_u64(m, timing_secretp)) != 0 || 666 (r = sshbuf_get_stringb(m, inc)) != 0) 667 fatal_fr(r, "parse config"); 668 669 if (conf != NULL && (r = sshbuf_put(conf, cp, len))) 670 fatal_fr(r, "sshbuf_put"); 671 672 while (sshbuf_len(inc) != 0) { 673 item = xcalloc(1, sizeof(*item)); 674 if ((item->contents = sshbuf_new()) == NULL) 675 fatal_f("sshbuf_new failed"); 676 if ((r = sshbuf_get_cstring(inc, &item->selector, NULL)) != 0 || 677 (r = sshbuf_get_cstring(inc, &item->filename, NULL)) != 0 || 678 (r = sshbuf_get_stringb(inc, item->contents)) != 0) 679 fatal_fr(r, "parse includes"); 680 TAILQ_INSERT_TAIL(&includes, item, entry); 681 } 682 683 /* receive hostkeys */ 684 sshbuf_reset(m); 685 if (ssh_msg_recv(fd, m) == -1) 686 fatal_f("ssh_msg_recv failed"); 687 if ((r = sshbuf_get_u8(m, NULL)) != 0 || 688 (r = sshbuf_froms(m, &hostkeys)) != 0) 689 fatal_fr(r, "parse config"); 690 parse_hostkeys(hostkeys); 691 692 free(cp); 693 sshbuf_free(m); 694 sshbuf_free(hostkeys); 695 sshbuf_free(inc); 696 697 debug3_f("done"); 698 } 699 700 /* 701 * If IP options are supported, make sure there are none (log and 702 * return an error if any are found). Basically we are worried about 703 * source routing; it can be used to pretend you are somebody 704 * (ip-address) you are not. That itself may be "almost acceptable" 705 * under certain circumstances, but rhosts authentication is useless 706 * if source routing is accepted. Notice also that if we just dropped 707 * source routing here, the other side could use IP spoofing to do 708 * rest of the interaction and could still bypass security. So we 709 * exit here if we detect any IP options. 710 */ 711 static void 712 check_ip_options(struct ssh *ssh) 713 { 714 #ifdef IP_OPTIONS 715 int sock_in = ssh_packet_get_connection_in(ssh); 716 struct sockaddr_storage from; 717 u_char opts[200]; 718 socklen_t i, option_size = sizeof(opts), fromlen = sizeof(from); 719 char text[sizeof(opts) * 3 + 1]; 720 721 memset(&from, 0, sizeof(from)); 722 if (getpeername(sock_in, (struct sockaddr *)&from, 723 &fromlen) == -1) 724 return; 725 if (from.ss_family != AF_INET) 726 return; 727 /* XXX IPv6 options? */ 728 729 if (getsockopt(sock_in, IPPROTO_IP, IP_OPTIONS, opts, 730 &option_size) >= 0 && option_size != 0) { 731 text[0] = '\0'; 732 for (i = 0; i < option_size; i++) 733 snprintf(text + i*3, sizeof(text) - i*3, 734 " %2.2x", opts[i]); 735 fatal("Connection from %.100s port %d with IP opts: %.800s", 736 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), text); 737 } 738 #endif /* IP_OPTIONS */ 739 } 740 741 /* Set the routing domain for this process */ 742 static void 743 set_process_rdomain(struct ssh *ssh, const char *name) 744 { 745 #if defined(HAVE_SYS_SET_PROCESS_RDOMAIN) 746 if (name == NULL) 747 return; /* default */ 748 749 if (strcmp(name, "%D") == 0) { 750 /* "expands" to routing domain of connection */ 751 if ((name = ssh_packet_rdomain_in(ssh)) == NULL) 752 return; 753 } 754 /* NB. We don't pass 'ssh' to sys_set_process_rdomain() */ 755 return sys_set_process_rdomain(name); 756 #elif defined(__OpenBSD__) 757 int rtable, ortable = getrtable(); 758 const char *errstr; 759 760 if (name == NULL) 761 return; /* default */ 762 763 if (strcmp(name, "%D") == 0) { 764 /* "expands" to routing domain of connection */ 765 if ((name = ssh_packet_rdomain_in(ssh)) == NULL) 766 return; 767 } 768 769 rtable = (int)strtonum(name, 0, 255, &errstr); 770 if (errstr != NULL) /* Shouldn't happen */ 771 fatal("Invalid routing domain \"%s\": %s", name, errstr); 772 if (rtable != ortable && setrtable(rtable) != 0) 773 fatal("Unable to set routing domain %d: %s", 774 rtable, strerror(errno)); 775 debug_f("set routing domain %d (was %d)", rtable, ortable); 776 #else /* defined(__OpenBSD__) */ 777 fatal("Unable to set routing domain: not supported in this platform"); 778 #endif 779 } 780 781 /* 782 * Main program for the daemon. 783 */ 784 int 785 main(int ac, char **av) 786 { 787 struct ssh *ssh = NULL; 788 extern char *optarg; 789 extern int optind; 790 int devnull, r, opt, on = 1, remote_port; 791 int sock_in = -1, sock_out = -1, rexeced_flag = 0, have_key = 0; 792 const char *remote_ip, *rdomain; 793 char *line, *laddr, *logfile = NULL; 794 u_int i; 795 uint64_t ibytes, obytes; 796 mode_t new_umask; 797 Authctxt *authctxt; 798 struct connection_info *connection_info = NULL; 799 sigset_t sigmask; 800 uint64_t timing_secret = 0; 801 struct itimerval itv; 802 803 sigemptyset(&sigmask); 804 sigprocmask(SIG_SETMASK, &sigmask, NULL); 805 806 #ifdef HAVE_SECUREWARE 807 (void)set_auth_parameters(ac, av); 808 #endif 809 __progname = ssh_get_progname(av[0]); 810 811 /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */ 812 saved_argc = ac; 813 saved_argv = xcalloc(ac + 1, sizeof(*saved_argv)); 814 for (i = 0; (int)i < ac; i++) 815 saved_argv[i] = xstrdup(av[i]); 816 saved_argv[i] = NULL; 817 818 #ifndef HAVE_SETPROCTITLE 819 /* Prepare for later setproctitle emulation */ 820 compat_init_setproctitle(ac, av); 821 av = saved_argv; 822 #endif 823 824 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 825 sanitise_stdfd(); 826 827 /* Initialize configuration options to their default values. */ 828 initialize_server_options(&options); 829 830 /* Parse command-line arguments. */ 831 while ((opt = getopt(ac, av, 832 "C:E:b:c:f:g:h:k:o:p:u:46DGQRTdeiqrtV")) != -1) { 833 switch (opt) { 834 case '4': 835 options.address_family = AF_INET; 836 break; 837 case '6': 838 options.address_family = AF_INET6; 839 break; 840 case 'f': 841 config_file_name = optarg; 842 break; 843 case 'c': 844 servconf_add_hostcert("[command-line]", 0, 845 &options, optarg); 846 break; 847 case 'd': 848 if (debug_flag == 0) { 849 debug_flag = 1; 850 options.log_level = SYSLOG_LEVEL_DEBUG1; 851 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) 852 options.log_level++; 853 break; 854 case 'D': 855 /* ignore */ 856 break; 857 case 'E': 858 logfile = optarg; 859 /* FALLTHROUGH */ 860 case 'e': 861 log_stderr = 1; 862 break; 863 case 'i': 864 inetd_flag = 1; 865 break; 866 case 'r': 867 /* ignore */ 868 break; 869 case 'R': 870 rexeced_flag = 1; 871 break; 872 case 'Q': 873 /* ignored */ 874 break; 875 case 'q': 876 options.log_level = SYSLOG_LEVEL_QUIET; 877 break; 878 case 'b': 879 /* protocol 1, ignored */ 880 break; 881 case 'p': 882 options.ports_from_cmdline = 1; 883 if (options.num_ports >= MAX_PORTS) { 884 fprintf(stderr, "too many ports.\n"); 885 exit(1); 886 } 887 options.ports[options.num_ports++] = a2port(optarg); 888 if (options.ports[options.num_ports-1] <= 0) { 889 fprintf(stderr, "Bad port number.\n"); 890 exit(1); 891 } 892 break; 893 case 'g': 894 if ((options.login_grace_time = convtime(optarg)) == -1) { 895 fprintf(stderr, "Invalid login grace time.\n"); 896 exit(1); 897 } 898 break; 899 case 'k': 900 /* protocol 1, ignored */ 901 break; 902 case 'h': 903 servconf_add_hostkey("[command-line]", 0, 904 &options, optarg, 1); 905 break; 906 case 't': 907 case 'T': 908 case 'G': 909 fatal("test/dump modes not supported"); 910 break; 911 case 'C': 912 connection_info = server_get_connection_info(ssh, 0, 0); 913 if (parse_server_match_testspec(connection_info, 914 optarg) == -1) 915 exit(1); 916 break; 917 case 'u': 918 utmp_len = (u_int)strtonum(optarg, 0, HOST_NAME_MAX+1+1, NULL); 919 if (utmp_len > HOST_NAME_MAX+1) { 920 fprintf(stderr, "Invalid utmp length.\n"); 921 exit(1); 922 } 923 break; 924 case 'o': 925 line = xstrdup(optarg); 926 if (process_server_config_line(&options, line, 927 "command-line", 0, NULL, NULL, &includes) != 0) 928 exit(1); 929 free(line); 930 break; 931 case 'V': 932 fprintf(stderr, "%s, %s\n", 933 SSH_RELEASE, SSH_OPENSSL_VERSION); 934 exit(0); 935 default: 936 usage(); 937 break; 938 } 939 } 940 941 /* Check that there are no remaining arguments. */ 942 if (optind < ac) { 943 fprintf(stderr, "Extra argument %s.\n", av[optind]); 944 exit(1); 945 } 946 947 if (!rexeced_flag) 948 fatal("sshd-session should not be executed directly"); 949 950 closefrom(REEXEC_MIN_FREE_FD); 951 952 platform_pre_session_start(); 953 954 /* Reserve fds we'll need later for reexec things */ 955 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) 956 fatal("open %s: %s", _PATH_DEVNULL, strerror(errno)); 957 while (devnull < PRIVSEP_MIN_FREE_FD) { 958 if ((devnull = dup(devnull)) == -1) 959 fatal("dup %s: %s", _PATH_DEVNULL, strerror(errno)); 960 } 961 962 seed_rng(); 963 964 /* If requested, redirect the logs to the specified logfile. */ 965 if (logfile != NULL) { 966 char *cp, pid_s[32]; 967 968 snprintf(pid_s, sizeof(pid_s), "%ld", (unsigned long)getpid()); 969 cp = percent_expand(logfile, 970 "p", pid_s, 971 "P", "sshd-session", 972 (char *)NULL); 973 log_redirect_stderr_to(cp); 974 free(cp); 975 } 976 977 /* 978 * Force logging to stderr until we have loaded the private host 979 * key (unless started from inetd) 980 */ 981 log_init(__progname, 982 options.log_level == SYSLOG_LEVEL_NOT_SET ? 983 SYSLOG_LEVEL_INFO : options.log_level, 984 options.log_facility == SYSLOG_FACILITY_NOT_SET ? 985 SYSLOG_FACILITY_AUTH : options.log_facility, 986 log_stderr || !inetd_flag || debug_flag); 987 988 /* Fetch our configuration */ 989 if ((cfg = sshbuf_new()) == NULL) 990 fatal("sshbuf_new config buf failed"); 991 setproctitle("%s", "[rexeced]"); 992 recv_rexec_state(REEXEC_CONFIG_PASS_FD, cfg, &timing_secret); 993 parse_server_config(&options, "rexec", cfg, &includes, NULL, 1); 994 /* Fill in default values for those options not explicitly set. */ 995 fill_default_server_options(&options); 996 options.timing_secret = timing_secret; 997 998 /* Reinit logging in case config set Level, Facility or Verbose. */ 999 log_init(__progname, options.log_level, options.log_facility, 1000 log_stderr || !inetd_flag || debug_flag); 1001 1002 debug("sshd-session version %s, %s", SSH_VERSION, SSH_OPENSSL_VERSION); 1003 1004 /* Store privilege separation user for later use if required. */ 1005 privsep_chroot = (getuid() == 0 || geteuid() == 0); 1006 if ((privsep_pw = getpwnam(SSH_PRIVSEP_USER)) == NULL) { 1007 if (privsep_chroot || options.kerberos_authentication) 1008 fatal("Privilege separation user %s does not exist", 1009 SSH_PRIVSEP_USER); 1010 } else { 1011 privsep_pw = pwcopy(privsep_pw); 1012 freezero(privsep_pw->pw_passwd, strlen(privsep_pw->pw_passwd)); 1013 privsep_pw->pw_passwd = xstrdup("*"); 1014 } 1015 endpwent(); 1016 1017 if (!debug_flag && !inetd_flag) { 1018 if ((startup_pipe = dup(REEXEC_CONFIG_PASS_FD)) == -1) 1019 fatal("internal error: no startup pipe"); 1020 1021 /* 1022 * Signal parent that this child is at a point where 1023 * they can go away if they have a SIGHUP pending. 1024 */ 1025 (void)atomicio(vwrite, startup_pipe, "\0", 1); 1026 } 1027 /* close the fd, but keep the slot reserved */ 1028 if (dup2(devnull, REEXEC_CONFIG_PASS_FD) == -1) 1029 fatal("dup2 devnull->config fd: %s", strerror(errno)); 1030 1031 /* Check that options are sensible */ 1032 if (options.authorized_keys_command_user == NULL && 1033 (options.authorized_keys_command != NULL && 1034 strcasecmp(options.authorized_keys_command, "none") != 0)) 1035 fatal("AuthorizedKeysCommand set without " 1036 "AuthorizedKeysCommandUser"); 1037 if (options.authorized_principals_command_user == NULL && 1038 (options.authorized_principals_command != NULL && 1039 strcasecmp(options.authorized_principals_command, "none") != 0)) 1040 fatal("AuthorizedPrincipalsCommand set without " 1041 "AuthorizedPrincipalsCommandUser"); 1042 1043 /* 1044 * Check whether there is any path through configured auth methods. 1045 * Unfortunately it is not possible to verify this generally before 1046 * daemonisation in the presence of Match block, but this catches 1047 * and warns for trivial misconfigurations that could break login. 1048 */ 1049 if (options.num_auth_methods != 0) { 1050 for (i = 0; i < options.num_auth_methods; i++) { 1051 if (auth2_methods_valid(options.auth_methods[i], 1052 1) == 0) 1053 break; 1054 } 1055 if (i >= options.num_auth_methods) 1056 fatal("AuthenticationMethods cannot be satisfied by " 1057 "enabled authentication methods"); 1058 } 1059 1060 #ifdef WITH_OPENSSL 1061 if (options.moduli_file != NULL) 1062 dh_set_moduli_file(options.moduli_file); 1063 #endif 1064 1065 if (options.host_key_agent) { 1066 if (strcmp(options.host_key_agent, SSH_AUTHSOCKET_ENV_NAME)) 1067 setenv(SSH_AUTHSOCKET_ENV_NAME, 1068 options.host_key_agent, 1); 1069 if ((r = ssh_get_authentication_socket(NULL)) == 0) 1070 have_agent = 1; 1071 else 1072 error_r(r, "Could not connect to agent \"%s\"", 1073 options.host_key_agent); 1074 } 1075 1076 if (options.num_host_key_files != sensitive_data.num_hostkeys) { 1077 fatal("internal error: hostkeys confused (config %u recvd %u)", 1078 options.num_host_key_files, sensitive_data.num_hostkeys); 1079 } 1080 1081 for (i = 0; i < options.num_host_key_files; i++) { 1082 if (sensitive_data.host_keys[i] != NULL || 1083 (have_agent && sensitive_data.host_pubkeys[i] != NULL)) { 1084 have_key = 1; 1085 break; 1086 } 1087 } 1088 if (!have_key) 1089 fatal("internal error: monitor received no hostkeys"); 1090 1091 /* Ensure that umask disallows at least group and world write */ 1092 new_umask = umask(0077) | 0022; 1093 (void) umask(new_umask); 1094 1095 /* Initialize the log (it is reinitialized below in case we forked). */ 1096 if (debug_flag) 1097 log_stderr = 1; 1098 log_init(__progname, options.log_level, 1099 options.log_facility, log_stderr); 1100 for (i = 0; i < options.num_log_verbose; i++) 1101 log_verbose_add(options.log_verbose[i]); 1102 1103 /* Reinitialize the log (because of the fork above). */ 1104 log_init(__progname, options.log_level, options.log_facility, log_stderr); 1105 1106 /* 1107 * Chdir to the root directory so that the current disk can be 1108 * unmounted if desired. 1109 */ 1110 if (chdir("/") == -1) 1111 error("chdir(\"/\"): %s", strerror(errno)); 1112 1113 /* ignore SIGPIPE */ 1114 ssh_signal(SIGPIPE, SIG_IGN); 1115 1116 /* Get a connection, either from inetd or rexec */ 1117 if (inetd_flag) { 1118 /* 1119 * NB. must be different fd numbers for the !socket case, 1120 * as packet_connection_is_on_socket() depends on this. 1121 */ 1122 sock_in = dup(STDIN_FILENO); 1123 sock_out = dup(STDOUT_FILENO); 1124 } else { 1125 /* rexec case; accept()ed socket in ancestor listener */ 1126 sock_in = sock_out = dup(STDIN_FILENO); 1127 } 1128 1129 /* 1130 * We intentionally do not close the descriptors 0, 1, and 2 1131 * as our code for setting the descriptors won't work if 1132 * ttyfd happens to be one of those. 1133 */ 1134 if (stdfd_devnull(1, 1, !log_stderr) == -1) 1135 error("stdfd_devnull failed"); 1136 debug("network sockets: %d, %d", sock_in, sock_out); 1137 1138 /* This is the child processing a new connection. */ 1139 setproctitle("%s", "[accepted]"); 1140 1141 /* Executed child processes don't need these. */ 1142 FD_CLOSEONEXEC(sock_out); 1143 FD_CLOSEONEXEC(sock_in); 1144 1145 /* We will not restart on SIGHUP since it no longer makes sense. */ 1146 ssh_signal(SIGALRM, SIG_DFL); 1147 ssh_signal(SIGHUP, SIG_DFL); 1148 ssh_signal(SIGTERM, SIG_DFL); 1149 ssh_signal(SIGQUIT, SIG_DFL); 1150 ssh_signal(SIGCHLD, SIG_DFL); 1151 ssh_signal(SIGINT, SIG_DFL); 1152 1153 BLOCKLIST_INIT(); 1154 1155 /* 1156 * Register our connection. This turns encryption off because we do 1157 * not have a key. 1158 */ 1159 if ((ssh = ssh_packet_set_connection(NULL, sock_in, sock_out)) == NULL) 1160 fatal("Unable to create connection"); 1161 the_active_state = ssh; 1162 ssh_packet_set_server(ssh); 1163 ssh_packet_set_qos(ssh, options.ip_qos_interactive, 1164 options.ip_qos_bulk); 1165 1166 check_ip_options(ssh); 1167 1168 /* Prepare the channels layer */ 1169 channel_init_channels(ssh); 1170 channel_set_af(ssh, options.address_family); 1171 server_process_channel_timeouts(ssh); 1172 server_process_permitopen(ssh); 1173 1174 /* Set SO_KEEPALIVE if requested. */ 1175 if (options.tcp_keep_alive && ssh_packet_connection_is_on_socket(ssh) && 1176 setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) == -1) 1177 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno)); 1178 1179 if ((remote_port = ssh_remote_port(ssh)) < 0) { 1180 debug("ssh_remote_port failed"); 1181 cleanup_exit(255); 1182 } 1183 1184 #ifdef HAVE_LOGIN_CAP 1185 /* Also caches remote hostname for sandboxed child. */ 1186 auth_get_canonical_hostname(ssh, options.use_dns); 1187 #endif 1188 1189 /* 1190 * The rest of the code depends on the fact that 1191 * ssh_remote_ipaddr() caches the remote ip, even if 1192 * the socket goes away. 1193 */ 1194 remote_ip = ssh_remote_ipaddr(ssh); 1195 1196 #ifdef SSH_AUDIT_EVENTS 1197 audit_connection_from(remote_ip, remote_port); 1198 #endif 1199 1200 rdomain = ssh_packet_rdomain_in(ssh); 1201 1202 /* Log the connection. */ 1203 laddr = get_local_ipaddr(sock_in); 1204 verbose("Connection from %s port %d on %s port %d%s%s%s", 1205 remote_ip, remote_port, laddr, ssh_local_port(ssh), 1206 rdomain == NULL ? "" : " rdomain \"", 1207 rdomain == NULL ? "" : rdomain, 1208 rdomain == NULL ? "" : "\""); 1209 free(laddr); 1210 1211 /* 1212 * We don't want to listen forever unless the other side 1213 * successfully authenticates itself. So we set up an alarm which is 1214 * cleared after successful authentication. A limit of zero 1215 * indicates no limit. Note that we don't set the alarm in debugging 1216 * mode; it is just annoying to have the server exit just when you 1217 * are about to discover the bug. 1218 */ 1219 ssh_signal(SIGALRM, grace_alarm_handler); 1220 if (!debug_flag && options.login_grace_time > 0) { 1221 int ujitter = arc4random_uniform(4 * 1000000); 1222 1223 timerclear(&itv.it_interval); 1224 itv.it_value.tv_sec = options.login_grace_time; 1225 itv.it_value.tv_sec += ujitter / 1000000; 1226 itv.it_value.tv_usec = ujitter % 1000000; 1227 1228 if (setitimer(ITIMER_REAL, &itv, NULL) == -1) 1229 fatal("login grace time setitimer failed"); 1230 } 1231 1232 ssh_packet_set_nonblocking(ssh); 1233 1234 /* allocate authentication context */ 1235 authctxt = xcalloc(1, sizeof(*authctxt)); 1236 ssh->authctxt = authctxt; 1237 1238 /* XXX global for cleanup, access from other modules */ 1239 the_authctxt = authctxt; 1240 1241 /* Set default key authentication options */ 1242 if ((auth_opts = sshauthopt_new_with_keys_defaults()) == NULL) 1243 fatal("allocation failed"); 1244 1245 /* prepare buffer to collect messages to display to user after login */ 1246 if ((loginmsg = sshbuf_new()) == NULL) 1247 fatal("sshbuf_new loginmsg failed"); 1248 auth_debug_reset(); 1249 1250 if (privsep_preauth(ssh) != 1) 1251 fatal("privsep_preauth failed"); 1252 1253 /* Now user is authenticated */ 1254 1255 /* 1256 * Cancel the alarm we set to limit the time taken for 1257 * authentication. 1258 */ 1259 timerclear(&itv.it_interval); 1260 timerclear(&itv.it_value); 1261 if (setitimer(ITIMER_REAL, &itv, NULL) == -1) 1262 fatal("login grace time clear failed"); 1263 ssh_signal(SIGALRM, SIG_DFL); 1264 authctxt->authenticated = 1; 1265 if (startup_pipe != -1) { 1266 /* signal listener that authentication completed successfully */ 1267 (void)atomicio(vwrite, startup_pipe, "\001", 1); 1268 close(startup_pipe); 1269 startup_pipe = -1; 1270 } 1271 1272 if (options.routing_domain != NULL) 1273 set_process_rdomain(ssh, options.routing_domain); 1274 1275 #ifdef SSH_AUDIT_EVENTS 1276 audit_event(ssh, SSH_AUTH_SUCCESS); 1277 #endif 1278 1279 #ifdef GSSAPI 1280 if (options.gss_authentication) { 1281 temporarily_use_uid(authctxt->pw); 1282 ssh_gssapi_storecreds(); 1283 restore_uid(); 1284 } 1285 #endif 1286 #ifdef USE_PAM 1287 if (options.use_pam) { 1288 do_pam_setcred(); 1289 do_pam_session(ssh); 1290 } 1291 #endif 1292 1293 /* 1294 * In privilege separation, we fork another child and prepare 1295 * file descriptor passing. 1296 */ 1297 privsep_postauth(ssh, authctxt); 1298 /* the monitor process [priv] will not return */ 1299 1300 ssh_packet_set_timeout(ssh, options.client_alive_interval, 1301 options.client_alive_count_max); 1302 1303 /* Try to send all our hostkeys to the client */ 1304 notify_hostkeys(ssh); 1305 1306 /* Start session. */ 1307 do_authenticated(ssh, authctxt); 1308 1309 /* The connection has been terminated. */ 1310 ssh_packet_get_bytes(ssh, &ibytes, &obytes); 1311 verbose("Transferred: sent %llu, received %llu bytes", 1312 (unsigned long long)obytes, (unsigned long long)ibytes); 1313 1314 verbose("Closing connection to %.500s port %d", remote_ip, remote_port); 1315 1316 #ifdef USE_PAM 1317 if (options.use_pam) 1318 finish_pam(); 1319 #endif /* USE_PAM */ 1320 1321 #ifdef SSH_AUDIT_EVENTS 1322 mm_audit_event(ssh, SSH_CONNECTION_CLOSE); 1323 #endif 1324 1325 ssh_packet_close(ssh); 1326 1327 mm_terminate(); 1328 1329 exit(0); 1330 } 1331 1332 int 1333 sshd_hostkey_sign(struct ssh *ssh, struct sshkey *privkey, 1334 struct sshkey *pubkey, u_char **signature, size_t *slenp, 1335 const u_char *data, size_t dlen, const char *alg) 1336 { 1337 if (privkey) { 1338 if (mm_sshkey_sign(ssh, privkey, signature, slenp, 1339 data, dlen, alg, options.sk_provider, NULL, 1340 ssh->compat) < 0) 1341 fatal_f("privkey sign failed"); 1342 } else { 1343 if (mm_sshkey_sign(ssh, pubkey, signature, slenp, 1344 data, dlen, alg, options.sk_provider, NULL, 1345 ssh->compat) < 0) 1346 fatal_f("pubkey sign failed"); 1347 } 1348 return 0; 1349 } 1350 1351 /* server specific fatal cleanup */ 1352 void 1353 cleanup_exit(int i) 1354 { 1355 if (the_active_state != NULL && the_authctxt != NULL) { 1356 do_cleanup(the_active_state, the_authctxt); 1357 if (privsep_is_preauth && 1358 pmonitor != NULL && pmonitor->m_pid > 1) { 1359 debug("Killing privsep child %d", pmonitor->m_pid); 1360 if (kill(pmonitor->m_pid, SIGKILL) != 0 && 1361 errno != ESRCH) { 1362 error_f("kill(%d): %s", pmonitor->m_pid, 1363 strerror(errno)); 1364 } 1365 } 1366 } 1367 #ifdef SSH_AUDIT_EVENTS 1368 /* done after do_cleanup so it can cancel the PAM auth 'thread' */ 1369 if (the_active_state != NULL && mm_is_monitor()) 1370 audit_event(the_active_state, SSH_CONNECTION_ABANDON); 1371 #endif 1372 /* Override default fatal exit value when auth was attempted */ 1373 if (i == 255 && monitor_auth_attempted()) { 1374 BLOCKLIST_NOTIFY(the_active_state, BLOCKLIST_AUTH_FAIL, 1375 "Fatal exit"); 1376 _exit(EXIT_AUTH_ATTEMPTED); 1377 } 1378 if (i == 255 && monitor_invalid_user()) 1379 _exit(EXIT_INVALID_USER); 1380 _exit(i); 1381 } 1382