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