1 /* $OpenBSD: sshd.c,v 1.521 2019/01/17 01:50:24 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 * This program is the ssh daemon. It listens for connections from clients, 7 * and performs authentication, executes use commands or shell, and forwards 8 * information to/from the application to the user client over an encrypted 9 * connection. This can also handle forwarding of X11, TCP/IP, and 10 * authentication agent connections. 11 * 12 * As far as I am concerned, the code I have written for this software 13 * can be used freely for any purpose. Any derived versions of this 14 * software must be clearly marked as such, and if the derived work is 15 * incompatible with the protocol description in the RFC file, it must be 16 * called by a name other than "ssh" or "Secure Shell". 17 * 18 * SSH2 implementation: 19 * Privilege Separation: 20 * 21 * Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved. 22 * Copyright (c) 2002 Niels Provos. All rights reserved. 23 * 24 * Redistribution and use in source and binary forms, with or without 25 * modification, are permitted provided that the following conditions 26 * are met: 27 * 1. Redistributions of source code must retain the above copyright 28 * notice, this list of conditions and the following disclaimer. 29 * 2. Redistributions in binary form must reproduce the above copyright 30 * notice, this list of conditions and the following disclaimer in the 31 * documentation and/or other materials provided with the distribution. 32 * 33 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 34 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 36 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 37 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 38 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 39 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 40 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 42 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 43 */ 44 45 #include <sys/types.h> 46 #include <sys/ioctl.h> 47 #include <sys/wait.h> 48 #include <sys/tree.h> 49 #include <sys/stat.h> 50 #include <sys/socket.h> 51 #include <sys/time.h> 52 #include <sys/queue.h> 53 54 #include <errno.h> 55 #include <fcntl.h> 56 #include <netdb.h> 57 #include <paths.h> 58 #include <pwd.h> 59 #include <signal.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 #include <limits.h> 65 66 #ifdef WITH_OPENSSL 67 #include <openssl/bn.h> 68 #endif 69 70 #include "xmalloc.h" 71 #include "ssh.h" 72 #include "ssh2.h" 73 #include "sshpty.h" 74 #include "packet.h" 75 #include "log.h" 76 #include "sshbuf.h" 77 #include "misc.h" 78 #include "match.h" 79 #include "servconf.h" 80 #include "uidswap.h" 81 #include "compat.h" 82 #include "cipher.h" 83 #include "digest.h" 84 #include "sshkey.h" 85 #include "kex.h" 86 #include "myproposal.h" 87 #include "authfile.h" 88 #include "pathnames.h" 89 #include "atomicio.h" 90 #include "canohost.h" 91 #include "hostfile.h" 92 #include "auth.h" 93 #include "authfd.h" 94 #include "msg.h" 95 #include "dispatch.h" 96 #include "channels.h" 97 #include "session.h" 98 #include "monitor.h" 99 #ifdef GSSAPI 100 #include "ssh-gss.h" 101 #endif 102 #include "monitor_wrap.h" 103 #include "ssh-sandbox.h" 104 #include "auth-options.h" 105 #include "version.h" 106 #include "ssherr.h" 107 108 /* Re-exec fds */ 109 #define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1) 110 #define REEXEC_STARTUP_PIPE_FD (STDERR_FILENO + 2) 111 #define REEXEC_CONFIG_PASS_FD (STDERR_FILENO + 3) 112 #define REEXEC_MIN_FREE_FD (STDERR_FILENO + 4) 113 114 extern char *__progname; 115 116 /* Server configuration options. */ 117 ServerOptions options; 118 119 /* Name of the server configuration file. */ 120 char *config_file_name = _PATH_SERVER_CONFIG_FILE; 121 122 /* 123 * Debug mode flag. This can be set on the command line. If debug 124 * mode is enabled, extra debugging output will be sent to the system 125 * log, the daemon will not go to background, and will exit after processing 126 * the first connection. 127 */ 128 int debug_flag = 0; 129 130 /* 131 * Indicating that the daemon should only test the configuration and keys. 132 * If test_flag > 1 ("-T" flag), then sshd will also dump the effective 133 * configuration, optionally using connection information provided by the 134 * "-C" flag. 135 */ 136 static int test_flag = 0; 137 138 /* Flag indicating that the daemon is being started from inetd. */ 139 static int inetd_flag = 0; 140 141 /* Flag indicating that sshd should not detach and become a daemon. */ 142 static int no_daemon_flag = 0; 143 144 /* debug goes to stderr unless inetd_flag is set */ 145 static int log_stderr = 0; 146 147 /* Saved arguments to main(). */ 148 static char **saved_argv; 149 150 /* re-exec */ 151 static int rexeced_flag = 0; 152 static int rexec_flag = 1; 153 static int rexec_argc = 0; 154 static char **rexec_argv; 155 156 /* 157 * The sockets that the server is listening; this is used in the SIGHUP 158 * signal handler. 159 */ 160 #define MAX_LISTEN_SOCKS 16 161 static int listen_socks[MAX_LISTEN_SOCKS]; 162 static int num_listen_socks = 0; 163 164 /* Daemon's agent connection */ 165 int auth_sock = -1; 166 static int have_agent = 0; 167 168 /* 169 * Any really sensitive data in the application is contained in this 170 * structure. The idea is that this structure could be locked into memory so 171 * that the pages do not get written into swap. However, there are some 172 * problems. The private key contains BIGNUMs, and we do not (in principle) 173 * have access to the internals of them, and locking just the structure is 174 * not very useful. Currently, memory locking is not implemented. 175 */ 176 struct { 177 struct sshkey **host_keys; /* all private host keys */ 178 struct sshkey **host_pubkeys; /* all public host keys */ 179 struct sshkey **host_certificates; /* all public host certificates */ 180 int have_ssh2_key; 181 } sensitive_data; 182 183 /* This is set to true when a signal is received. */ 184 static volatile sig_atomic_t received_sighup = 0; 185 static volatile sig_atomic_t received_sigterm = 0; 186 187 /* session identifier, used by RSA-auth */ 188 u_char session_id[16]; 189 190 /* same for ssh2 */ 191 u_char *session_id2 = NULL; 192 u_int session_id2_len = 0; 193 194 /* record remote hostname or ip */ 195 u_int utmp_len = HOST_NAME_MAX+1; 196 197 /* options.max_startup sized array of fd ints */ 198 static int *startup_pipes = NULL; 199 static int startup_pipe; /* in child */ 200 201 /* variables used for privilege separation */ 202 int use_privsep = -1; 203 struct monitor *pmonitor = NULL; 204 int privsep_is_preauth = 1; 205 206 /* global authentication context */ 207 Authctxt *the_authctxt = NULL; 208 209 /* global key/cert auth options. XXX move to permanent ssh->authctxt? */ 210 struct sshauthopt *auth_opts = NULL; 211 212 /* sshd_config buffer */ 213 struct sshbuf *cfg; 214 215 /* message to be displayed after login */ 216 struct sshbuf *loginmsg; 217 218 /* Prototypes for various functions defined later in this file. */ 219 void destroy_sensitive_data(void); 220 void demote_sensitive_data(void); 221 static void do_ssh2_kex(void); 222 223 /* 224 * Close all listening sockets 225 */ 226 static void 227 close_listen_socks(void) 228 { 229 int i; 230 231 for (i = 0; i < num_listen_socks; i++) 232 close(listen_socks[i]); 233 num_listen_socks = -1; 234 } 235 236 static void 237 close_startup_pipes(void) 238 { 239 int i; 240 241 if (startup_pipes) 242 for (i = 0; i < options.max_startups; i++) 243 if (startup_pipes[i] != -1) 244 close(startup_pipes[i]); 245 } 246 247 /* 248 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP; 249 * the effect is to reread the configuration file (and to regenerate 250 * the server key). 251 */ 252 253 /*ARGSUSED*/ 254 static void 255 sighup_handler(int sig) 256 { 257 int save_errno = errno; 258 259 received_sighup = 1; 260 errno = save_errno; 261 } 262 263 /* 264 * Called from the main program after receiving SIGHUP. 265 * Restarts the server. 266 */ 267 static void 268 sighup_restart(void) 269 { 270 logit("Received SIGHUP; restarting."); 271 if (options.pid_file != NULL) 272 unlink(options.pid_file); 273 close_listen_socks(); 274 close_startup_pipes(); 275 alarm(0); /* alarm timer persists across exec */ 276 signal(SIGHUP, SIG_IGN); /* will be restored after exec */ 277 execv(saved_argv[0], saved_argv); 278 logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0], 279 strerror(errno)); 280 exit(1); 281 } 282 283 /* 284 * Generic signal handler for terminating signals in the master daemon. 285 */ 286 /*ARGSUSED*/ 287 static void 288 sigterm_handler(int sig) 289 { 290 received_sigterm = sig; 291 } 292 293 /* 294 * SIGCHLD handler. This is called whenever a child dies. This will then 295 * reap any zombies left by exited children. 296 */ 297 /*ARGSUSED*/ 298 static void 299 main_sigchld_handler(int sig) 300 { 301 int save_errno = errno; 302 pid_t pid; 303 int status; 304 305 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || 306 (pid < 0 && errno == EINTR)) 307 ; 308 errno = save_errno; 309 } 310 311 /* 312 * Signal handler for the alarm after the login grace period has expired. 313 */ 314 /*ARGSUSED*/ 315 static void 316 grace_alarm_handler(int sig) 317 { 318 if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0) 319 kill(pmonitor->m_pid, SIGALRM); 320 321 /* 322 * Try to kill any processes that we have spawned, E.g. authorized 323 * keys command helpers. 324 */ 325 if (getpgid(0) == getpid()) { 326 signal(SIGTERM, SIG_IGN); 327 kill(0, SIGTERM); 328 } 329 330 /* Log error and exit. */ 331 sigdie("Timeout before authentication for %s port %d", 332 ssh_remote_ipaddr(active_state), ssh_remote_port(active_state)); 333 } 334 335 /* Destroy the host and server keys. They will no longer be needed. */ 336 void 337 destroy_sensitive_data(void) 338 { 339 u_int i; 340 341 for (i = 0; i < options.num_host_key_files; i++) { 342 if (sensitive_data.host_keys[i]) { 343 sshkey_free(sensitive_data.host_keys[i]); 344 sensitive_data.host_keys[i] = NULL; 345 } 346 if (sensitive_data.host_certificates[i]) { 347 sshkey_free(sensitive_data.host_certificates[i]); 348 sensitive_data.host_certificates[i] = NULL; 349 } 350 } 351 } 352 353 /* Demote private to public keys for network child */ 354 void 355 demote_sensitive_data(void) 356 { 357 struct sshkey *tmp; 358 u_int i; 359 int r; 360 361 for (i = 0; i < options.num_host_key_files; i++) { 362 if (sensitive_data.host_keys[i]) { 363 if ((r = sshkey_from_private( 364 sensitive_data.host_keys[i], &tmp)) != 0) 365 fatal("could not demote host %s key: %s", 366 sshkey_type(sensitive_data.host_keys[i]), 367 ssh_err(r)); 368 sshkey_free(sensitive_data.host_keys[i]); 369 sensitive_data.host_keys[i] = tmp; 370 } 371 /* Certs do not need demotion */ 372 } 373 } 374 375 static void 376 privsep_preauth_child(void) 377 { 378 gid_t gidset[1]; 379 struct passwd *pw; 380 381 /* Enable challenge-response authentication for privilege separation */ 382 privsep_challenge_enable(); 383 384 #ifdef GSSAPI 385 /* Cache supported mechanism OIDs for later use */ 386 ssh_gssapi_prepare_supported_oids(); 387 #endif 388 389 /* Demote the private keys to public keys. */ 390 demote_sensitive_data(); 391 392 /* Demote the child */ 393 if (getuid() == 0 || geteuid() == 0) { 394 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL) 395 fatal("Privilege separation user %s does not exist", 396 SSH_PRIVSEP_USER); 397 pw = pwcopy(pw); /* Ensure mutable */ 398 endpwent(); 399 freezero(pw->pw_passwd, strlen(pw->pw_passwd)); 400 401 /* Change our root directory */ 402 if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1) 403 fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR, 404 strerror(errno)); 405 if (chdir("/") == -1) 406 fatal("chdir(\"/\"): %s", strerror(errno)); 407 408 /* 409 * Drop our privileges 410 * NB. Can't use setusercontext() after chroot. 411 */ 412 debug3("privsep user:group %u:%u", (u_int)pw->pw_uid, 413 (u_int)pw->pw_gid); 414 gidset[0] = pw->pw_gid; 415 if (setgroups(1, gidset) < 0) 416 fatal("setgroups: %.100s", strerror(errno)); 417 permanently_set_uid(pw); 418 } 419 } 420 421 static int 422 privsep_preauth(Authctxt *authctxt) 423 { 424 int status, r; 425 pid_t pid; 426 struct ssh_sandbox *box = NULL; 427 428 /* Set up unprivileged child process to deal with network data */ 429 pmonitor = monitor_init(); 430 /* Store a pointer to the kex for later rekeying */ 431 pmonitor->m_pkex = &active_state->kex; 432 433 if (use_privsep == PRIVSEP_ON) 434 box = ssh_sandbox_init(); 435 pid = fork(); 436 if (pid == -1) { 437 fatal("fork of unprivileged child failed"); 438 } else if (pid != 0) { 439 debug2("Network child is on pid %ld", (long)pid); 440 441 pmonitor->m_pid = pid; 442 if (have_agent) { 443 r = ssh_get_authentication_socket(&auth_sock); 444 if (r != 0) { 445 error("Could not get agent socket: %s", 446 ssh_err(r)); 447 have_agent = 0; 448 } 449 } 450 if (box != NULL) 451 ssh_sandbox_parent_preauth(box, pid); 452 monitor_child_preauth(authctxt, pmonitor); 453 454 /* Wait for the child's exit status */ 455 while (waitpid(pid, &status, 0) < 0) { 456 if (errno == EINTR) 457 continue; 458 pmonitor->m_pid = -1; 459 fatal("%s: waitpid: %s", __func__, strerror(errno)); 460 } 461 privsep_is_preauth = 0; 462 pmonitor->m_pid = -1; 463 if (WIFEXITED(status)) { 464 if (WEXITSTATUS(status) != 0) 465 fatal("%s: preauth child exited with status %d", 466 __func__, WEXITSTATUS(status)); 467 } else if (WIFSIGNALED(status)) 468 fatal("%s: preauth child terminated by signal %d", 469 __func__, WTERMSIG(status)); 470 if (box != NULL) 471 ssh_sandbox_parent_finish(box); 472 return 1; 473 } else { 474 /* child */ 475 close(pmonitor->m_sendfd); 476 close(pmonitor->m_log_recvfd); 477 478 /* Arrange for logging to be sent to the monitor */ 479 set_log_handler(mm_log_handler, pmonitor); 480 481 privsep_preauth_child(); 482 setproctitle("%s", "[net]"); 483 if (box != NULL) 484 ssh_sandbox_child(box); 485 486 return 0; 487 } 488 } 489 490 static void 491 privsep_postauth(Authctxt *authctxt) 492 { 493 if (authctxt->pw->pw_uid == 0) { 494 /* File descriptor passing is broken or root login */ 495 use_privsep = 0; 496 goto skip; 497 } 498 499 /* New socket pair */ 500 monitor_reinit(pmonitor); 501 502 pmonitor->m_pid = fork(); 503 if (pmonitor->m_pid == -1) 504 fatal("fork of unprivileged child failed"); 505 else if (pmonitor->m_pid != 0) { 506 verbose("User child is on pid %ld", (long)pmonitor->m_pid); 507 sshbuf_reset(loginmsg); 508 monitor_clear_keystate(pmonitor); 509 monitor_child_postauth(pmonitor); 510 511 /* NEVERREACHED */ 512 exit(0); 513 } 514 515 /* child */ 516 517 close(pmonitor->m_sendfd); 518 pmonitor->m_sendfd = -1; 519 520 /* Demote the private keys to public keys. */ 521 demote_sensitive_data(); 522 523 /* Drop privileges */ 524 do_setusercontext(authctxt->pw); 525 526 skip: 527 /* It is safe now to apply the key state */ 528 monitor_apply_keystate(pmonitor); 529 530 /* 531 * Tell the packet layer that authentication was successful, since 532 * this information is not part of the key state. 533 */ 534 packet_set_authenticated(); 535 } 536 537 static void 538 append_hostkey_type(struct sshbuf *b, const char *s) 539 { 540 int r; 541 542 if (match_pattern_list(s, options.hostkeyalgorithms, 0) != 1) { 543 debug3("%s: %s key not permitted by HostkeyAlgorithms", 544 __func__, s); 545 return; 546 } 547 if ((r = sshbuf_putf(b, "%s%s", sshbuf_len(b) > 0 ? "," : "", s)) != 0) 548 fatal("%s: sshbuf_putf: %s", __func__, ssh_err(r)); 549 } 550 551 static char * 552 list_hostkey_types(void) 553 { 554 struct sshbuf *b; 555 struct sshkey *key; 556 char *ret; 557 u_int i; 558 559 if ((b = sshbuf_new()) == NULL) 560 fatal("%s: sshbuf_new failed", __func__); 561 for (i = 0; i < options.num_host_key_files; i++) { 562 key = sensitive_data.host_keys[i]; 563 if (key == NULL) 564 key = sensitive_data.host_pubkeys[i]; 565 if (key == NULL) 566 continue; 567 switch (key->type) { 568 case KEY_RSA: 569 /* for RSA we also support SHA2 signatures */ 570 append_hostkey_type(b, "rsa-sha2-512"); 571 append_hostkey_type(b, "rsa-sha2-256"); 572 /* FALLTHROUGH */ 573 case KEY_DSA: 574 case KEY_ECDSA: 575 case KEY_ED25519: 576 case KEY_XMSS: 577 append_hostkey_type(b, sshkey_ssh_name(key)); 578 break; 579 } 580 /* If the private key has a cert peer, then list that too */ 581 key = sensitive_data.host_certificates[i]; 582 if (key == NULL) 583 continue; 584 switch (key->type) { 585 case KEY_RSA_CERT: 586 /* for RSA we also support SHA2 signatures */ 587 append_hostkey_type(b, 588 "rsa-sha2-512-cert-v01@openssh.com"); 589 append_hostkey_type(b, 590 "rsa-sha2-256-cert-v01@openssh.com"); 591 /* FALLTHROUGH */ 592 case KEY_DSA_CERT: 593 case KEY_ECDSA_CERT: 594 case KEY_ED25519_CERT: 595 case KEY_XMSS_CERT: 596 append_hostkey_type(b, sshkey_ssh_name(key)); 597 break; 598 } 599 } 600 if ((ret = sshbuf_dup_string(b)) == NULL) 601 fatal("%s: sshbuf_dup_string failed", __func__); 602 sshbuf_free(b); 603 debug("%s: %s", __func__, ret); 604 return ret; 605 } 606 607 static struct sshkey * 608 get_hostkey_by_type(int type, int nid, int need_private, struct ssh *ssh) 609 { 610 u_int i; 611 struct sshkey *key; 612 613 for (i = 0; i < options.num_host_key_files; i++) { 614 switch (type) { 615 case KEY_RSA_CERT: 616 case KEY_DSA_CERT: 617 case KEY_ECDSA_CERT: 618 case KEY_ED25519_CERT: 619 case KEY_XMSS_CERT: 620 key = sensitive_data.host_certificates[i]; 621 break; 622 default: 623 key = sensitive_data.host_keys[i]; 624 if (key == NULL && !need_private) 625 key = sensitive_data.host_pubkeys[i]; 626 break; 627 } 628 if (key != NULL && key->type == type && 629 (key->type != KEY_ECDSA || key->ecdsa_nid == nid)) 630 return need_private ? 631 sensitive_data.host_keys[i] : key; 632 } 633 return NULL; 634 } 635 636 struct sshkey * 637 get_hostkey_public_by_type(int type, int nid, struct ssh *ssh) 638 { 639 return get_hostkey_by_type(type, nid, 0, ssh); 640 } 641 642 struct sshkey * 643 get_hostkey_private_by_type(int type, int nid, struct ssh *ssh) 644 { 645 return get_hostkey_by_type(type, nid, 1, ssh); 646 } 647 648 struct sshkey * 649 get_hostkey_by_index(int ind) 650 { 651 if (ind < 0 || (u_int)ind >= options.num_host_key_files) 652 return (NULL); 653 return (sensitive_data.host_keys[ind]); 654 } 655 656 struct sshkey * 657 get_hostkey_public_by_index(int ind, struct ssh *ssh) 658 { 659 if (ind < 0 || (u_int)ind >= options.num_host_key_files) 660 return (NULL); 661 return (sensitive_data.host_pubkeys[ind]); 662 } 663 664 int 665 get_hostkey_index(struct sshkey *key, int compare, struct ssh *ssh) 666 { 667 u_int i; 668 669 for (i = 0; i < options.num_host_key_files; i++) { 670 if (sshkey_is_cert(key)) { 671 if (key == sensitive_data.host_certificates[i] || 672 (compare && sensitive_data.host_certificates[i] && 673 sshkey_equal(key, 674 sensitive_data.host_certificates[i]))) 675 return (i); 676 } else { 677 if (key == sensitive_data.host_keys[i] || 678 (compare && sensitive_data.host_keys[i] && 679 sshkey_equal(key, sensitive_data.host_keys[i]))) 680 return (i); 681 if (key == sensitive_data.host_pubkeys[i] || 682 (compare && sensitive_data.host_pubkeys[i] && 683 sshkey_equal(key, sensitive_data.host_pubkeys[i]))) 684 return (i); 685 } 686 } 687 return (-1); 688 } 689 690 /* Inform the client of all hostkeys */ 691 static void 692 notify_hostkeys(struct ssh *ssh) 693 { 694 struct sshbuf *buf; 695 struct sshkey *key; 696 u_int i, nkeys; 697 int r; 698 char *fp; 699 700 /* Some clients cannot cope with the hostkeys message, skip those. */ 701 if (datafellows & SSH_BUG_HOSTKEYS) 702 return; 703 704 if ((buf = sshbuf_new()) == NULL) 705 fatal("%s: sshbuf_new", __func__); 706 for (i = nkeys = 0; i < options.num_host_key_files; i++) { 707 key = get_hostkey_public_by_index(i, ssh); 708 if (key == NULL || key->type == KEY_UNSPEC || 709 sshkey_is_cert(key)) 710 continue; 711 fp = sshkey_fingerprint(key, options.fingerprint_hash, 712 SSH_FP_DEFAULT); 713 debug3("%s: key %d: %s %s", __func__, i, 714 sshkey_ssh_name(key), fp); 715 free(fp); 716 if (nkeys == 0) { 717 packet_start(SSH2_MSG_GLOBAL_REQUEST); 718 packet_put_cstring("hostkeys-00@openssh.com"); 719 packet_put_char(0); /* want-reply */ 720 } 721 sshbuf_reset(buf); 722 if ((r = sshkey_putb(key, buf)) != 0) 723 fatal("%s: couldn't put hostkey %d: %s", 724 __func__, i, ssh_err(r)); 725 packet_put_string(sshbuf_ptr(buf), sshbuf_len(buf)); 726 nkeys++; 727 } 728 debug3("%s: sent %u hostkeys", __func__, nkeys); 729 if (nkeys == 0) 730 fatal("%s: no hostkeys", __func__); 731 packet_send(); 732 sshbuf_free(buf); 733 } 734 735 /* 736 * returns 1 if connection should be dropped, 0 otherwise. 737 * dropping starts at connection #max_startups_begin with a probability 738 * of (max_startups_rate/100). the probability increases linearly until 739 * all connections are dropped for startups > max_startups 740 */ 741 static int 742 drop_connection(int startups) 743 { 744 int p, r; 745 746 if (startups < options.max_startups_begin) 747 return 0; 748 if (startups >= options.max_startups) 749 return 1; 750 if (options.max_startups_rate == 100) 751 return 1; 752 753 p = 100 - options.max_startups_rate; 754 p *= startups - options.max_startups_begin; 755 p /= options.max_startups - options.max_startups_begin; 756 p += options.max_startups_rate; 757 r = arc4random_uniform(100); 758 759 debug("drop_connection: p %d, r %d", p, r); 760 return (r < p) ? 1 : 0; 761 } 762 763 static void 764 usage(void) 765 { 766 fprintf(stderr, "%s, %s\n", 767 SSH_VERSION, 768 #ifdef WITH_OPENSSL 769 OpenSSL_version(OPENSSL_VERSION) 770 #else 771 "without OpenSSL" 772 #endif 773 ); 774 fprintf(stderr, 775 "usage: sshd [-46DdeiqTt] [-C connection_spec] [-c host_cert_file]\n" 776 " [-E log_file] [-f config_file] [-g login_grace_time]\n" 777 " [-h host_key_file] [-o option] [-p port] [-u len]\n" 778 ); 779 exit(1); 780 } 781 782 static void 783 send_rexec_state(int fd, struct sshbuf *conf) 784 { 785 struct sshbuf *m; 786 int r; 787 788 debug3("%s: entering fd = %d config len %zu", __func__, fd, 789 sshbuf_len(conf)); 790 791 /* 792 * Protocol from reexec master to child: 793 * string configuration 794 */ 795 if ((m = sshbuf_new()) == NULL) 796 fatal("%s: sshbuf_new failed", __func__); 797 if ((r = sshbuf_put_stringb(m, conf)) != 0) 798 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 799 if (ssh_msg_send(fd, 0, m) == -1) 800 fatal("%s: ssh_msg_send failed", __func__); 801 802 sshbuf_free(m); 803 804 debug3("%s: done", __func__); 805 } 806 807 static void 808 recv_rexec_state(int fd, struct sshbuf *conf) 809 { 810 struct sshbuf *m; 811 u_char *cp, ver; 812 size_t len; 813 int r; 814 815 debug3("%s: entering fd = %d", __func__, fd); 816 817 if ((m = sshbuf_new()) == NULL) 818 fatal("%s: sshbuf_new failed", __func__); 819 if (ssh_msg_recv(fd, m) == -1) 820 fatal("%s: ssh_msg_recv failed", __func__); 821 if ((r = sshbuf_get_u8(m, &ver)) != 0) 822 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 823 if (ver != 0) 824 fatal("%s: rexec version mismatch", __func__); 825 if ((r = sshbuf_get_string(m, &cp, &len)) != 0) 826 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 827 if (conf != NULL && (r = sshbuf_put(conf, cp, len))) 828 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 829 830 free(cp); 831 sshbuf_free(m); 832 833 debug3("%s: done", __func__); 834 } 835 836 /* Accept a connection from inetd */ 837 static void 838 server_accept_inetd(int *sock_in, int *sock_out) 839 { 840 int fd; 841 842 startup_pipe = -1; 843 if (rexeced_flag) { 844 close(REEXEC_CONFIG_PASS_FD); 845 *sock_in = *sock_out = dup(STDIN_FILENO); 846 if (!debug_flag) { 847 startup_pipe = dup(REEXEC_STARTUP_PIPE_FD); 848 close(REEXEC_STARTUP_PIPE_FD); 849 } 850 } else { 851 *sock_in = dup(STDIN_FILENO); 852 *sock_out = dup(STDOUT_FILENO); 853 } 854 /* 855 * We intentionally do not close the descriptors 0, 1, and 2 856 * as our code for setting the descriptors won't work if 857 * ttyfd happens to be one of those. 858 */ 859 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 860 dup2(fd, STDIN_FILENO); 861 dup2(fd, STDOUT_FILENO); 862 if (!log_stderr) 863 dup2(fd, STDERR_FILENO); 864 if (fd > (log_stderr ? STDERR_FILENO : STDOUT_FILENO)) 865 close(fd); 866 } 867 debug("inetd sockets after dupping: %d, %d", *sock_in, *sock_out); 868 } 869 870 /* 871 * Listen for TCP connections 872 */ 873 static void 874 listen_on_addrs(struct listenaddr *la) 875 { 876 int ret, listen_sock; 877 struct addrinfo *ai; 878 char ntop[NI_MAXHOST], strport[NI_MAXSERV]; 879 880 for (ai = la->addrs; ai; ai = ai->ai_next) { 881 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) 882 continue; 883 if (num_listen_socks >= MAX_LISTEN_SOCKS) 884 fatal("Too many listen sockets. " 885 "Enlarge MAX_LISTEN_SOCKS"); 886 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, 887 ntop, sizeof(ntop), strport, sizeof(strport), 888 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) { 889 error("getnameinfo failed: %.100s", 890 ssh_gai_strerror(ret)); 891 continue; 892 } 893 /* Create socket for listening. */ 894 listen_sock = socket(ai->ai_family, ai->ai_socktype, 895 ai->ai_protocol); 896 if (listen_sock < 0) { 897 /* kernel may not support ipv6 */ 898 verbose("socket: %.100s", strerror(errno)); 899 continue; 900 } 901 if (set_nonblock(listen_sock) == -1) { 902 close(listen_sock); 903 continue; 904 } 905 if (fcntl(listen_sock, F_SETFD, FD_CLOEXEC) == -1) { 906 verbose("socket: CLOEXEC: %s", strerror(errno)); 907 close(listen_sock); 908 continue; 909 } 910 /* Socket options */ 911 set_reuseaddr(listen_sock); 912 if (la->rdomain != NULL && 913 set_rdomain(listen_sock, la->rdomain) == -1) { 914 close(listen_sock); 915 continue; 916 } 917 918 debug("Bind to port %s on %s.", strport, ntop); 919 920 /* Bind the socket to the desired port. */ 921 if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) { 922 error("Bind to port %s on %s failed: %.200s.", 923 strport, ntop, strerror(errno)); 924 close(listen_sock); 925 continue; 926 } 927 listen_socks[num_listen_socks] = listen_sock; 928 num_listen_socks++; 929 930 /* Start listening on the port. */ 931 if (listen(listen_sock, SSH_LISTEN_BACKLOG) < 0) 932 fatal("listen on [%s]:%s: %.100s", 933 ntop, strport, strerror(errno)); 934 logit("Server listening on %s port %s%s%s.", 935 ntop, strport, 936 la->rdomain == NULL ? "" : " rdomain ", 937 la->rdomain == NULL ? "" : la->rdomain); 938 } 939 } 940 941 static void 942 server_listen(void) 943 { 944 u_int i; 945 946 for (i = 0; i < options.num_listen_addrs; i++) { 947 listen_on_addrs(&options.listen_addrs[i]); 948 freeaddrinfo(options.listen_addrs[i].addrs); 949 free(options.listen_addrs[i].rdomain); 950 memset(&options.listen_addrs[i], 0, 951 sizeof(options.listen_addrs[i])); 952 } 953 free(options.listen_addrs); 954 options.listen_addrs = NULL; 955 options.num_listen_addrs = 0; 956 957 if (!num_listen_socks) 958 fatal("Cannot bind any address."); 959 } 960 961 /* 962 * The main TCP accept loop. Note that, for the non-debug case, returns 963 * from this function are in a forked subprocess. 964 */ 965 static void 966 server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s) 967 { 968 fd_set *fdset; 969 int i, j, ret, maxfd; 970 int startups = 0; 971 int startup_p[2] = { -1 , -1 }; 972 struct sockaddr_storage from; 973 socklen_t fromlen; 974 pid_t pid; 975 976 /* setup fd set for accept */ 977 fdset = NULL; 978 maxfd = 0; 979 for (i = 0; i < num_listen_socks; i++) 980 if (listen_socks[i] > maxfd) 981 maxfd = listen_socks[i]; 982 /* pipes connected to unauthenticated childs */ 983 startup_pipes = xcalloc(options.max_startups, sizeof(int)); 984 for (i = 0; i < options.max_startups; i++) 985 startup_pipes[i] = -1; 986 987 /* 988 * Stay listening for connections until the system crashes or 989 * the daemon is killed with a signal. 990 */ 991 for (;;) { 992 if (received_sighup) 993 sighup_restart(); 994 free(fdset); 995 fdset = xcalloc(howmany(maxfd + 1, NFDBITS), 996 sizeof(fd_mask)); 997 998 for (i = 0; i < num_listen_socks; i++) 999 FD_SET(listen_socks[i], fdset); 1000 for (i = 0; i < options.max_startups; i++) 1001 if (startup_pipes[i] != -1) 1002 FD_SET(startup_pipes[i], fdset); 1003 1004 /* Wait in select until there is a connection. */ 1005 ret = select(maxfd+1, fdset, NULL, NULL, NULL); 1006 if (ret < 0 && errno != EINTR) 1007 error("select: %.100s", strerror(errno)); 1008 if (received_sigterm) { 1009 logit("Received signal %d; terminating.", 1010 (int) received_sigterm); 1011 close_listen_socks(); 1012 if (options.pid_file != NULL) 1013 unlink(options.pid_file); 1014 exit(received_sigterm == SIGTERM ? 0 : 255); 1015 } 1016 if (ret < 0) 1017 continue; 1018 1019 for (i = 0; i < options.max_startups; i++) 1020 if (startup_pipes[i] != -1 && 1021 FD_ISSET(startup_pipes[i], fdset)) { 1022 /* 1023 * the read end of the pipe is ready 1024 * if the child has closed the pipe 1025 * after successful authentication 1026 * or if the child has died 1027 */ 1028 close(startup_pipes[i]); 1029 startup_pipes[i] = -1; 1030 startups--; 1031 } 1032 for (i = 0; i < num_listen_socks; i++) { 1033 if (!FD_ISSET(listen_socks[i], fdset)) 1034 continue; 1035 fromlen = sizeof(from); 1036 *newsock = accept(listen_socks[i], 1037 (struct sockaddr *)&from, &fromlen); 1038 if (*newsock < 0) { 1039 if (errno != EINTR && errno != EWOULDBLOCK && 1040 errno != ECONNABORTED) 1041 error("accept: %.100s", 1042 strerror(errno)); 1043 if (errno == EMFILE || errno == ENFILE) 1044 usleep(100 * 1000); 1045 continue; 1046 } 1047 if (unset_nonblock(*newsock) == -1) { 1048 close(*newsock); 1049 continue; 1050 } 1051 if (drop_connection(startups) == 1) { 1052 char *laddr = get_local_ipaddr(*newsock); 1053 char *raddr = get_peer_ipaddr(*newsock); 1054 1055 verbose("drop connection #%d from [%s]:%d " 1056 "on [%s]:%d past MaxStartups", startups, 1057 raddr, get_peer_port(*newsock), 1058 laddr, get_local_port(*newsock)); 1059 free(laddr); 1060 free(raddr); 1061 close(*newsock); 1062 continue; 1063 } 1064 if (pipe(startup_p) == -1) { 1065 close(*newsock); 1066 continue; 1067 } 1068 1069 if (rexec_flag && socketpair(AF_UNIX, 1070 SOCK_STREAM, 0, config_s) == -1) { 1071 error("reexec socketpair: %s", 1072 strerror(errno)); 1073 close(*newsock); 1074 close(startup_p[0]); 1075 close(startup_p[1]); 1076 continue; 1077 } 1078 1079 for (j = 0; j < options.max_startups; j++) 1080 if (startup_pipes[j] == -1) { 1081 startup_pipes[j] = startup_p[0]; 1082 if (maxfd < startup_p[0]) 1083 maxfd = startup_p[0]; 1084 startups++; 1085 break; 1086 } 1087 1088 /* 1089 * Got connection. Fork a child to handle it, unless 1090 * we are in debugging mode. 1091 */ 1092 if (debug_flag) { 1093 /* 1094 * In debugging mode. Close the listening 1095 * socket, and start processing the 1096 * connection without forking. 1097 */ 1098 debug("Server will not fork when running in debugging mode."); 1099 close_listen_socks(); 1100 *sock_in = *newsock; 1101 *sock_out = *newsock; 1102 close(startup_p[0]); 1103 close(startup_p[1]); 1104 startup_pipe = -1; 1105 pid = getpid(); 1106 if (rexec_flag) { 1107 send_rexec_state(config_s[0], cfg); 1108 close(config_s[0]); 1109 } 1110 break; 1111 } 1112 1113 /* 1114 * Normal production daemon. Fork, and have 1115 * the child process the connection. The 1116 * parent continues listening. 1117 */ 1118 if ((pid = fork()) == 0) { 1119 /* 1120 * Child. Close the listening and 1121 * max_startup sockets. Start using 1122 * the accepted socket. Reinitialize 1123 * logging (since our pid has changed). 1124 * We break out of the loop to handle 1125 * the connection. 1126 */ 1127 startup_pipe = startup_p[1]; 1128 close_startup_pipes(); 1129 close_listen_socks(); 1130 *sock_in = *newsock; 1131 *sock_out = *newsock; 1132 log_init(__progname, 1133 options.log_level, 1134 options.log_facility, 1135 log_stderr); 1136 if (rexec_flag) 1137 close(config_s[0]); 1138 break; 1139 } 1140 1141 /* Parent. Stay in the loop. */ 1142 if (pid < 0) 1143 error("fork: %.100s", strerror(errno)); 1144 else 1145 debug("Forked child %ld.", (long)pid); 1146 1147 close(startup_p[1]); 1148 1149 if (rexec_flag) { 1150 send_rexec_state(config_s[0], cfg); 1151 close(config_s[0]); 1152 close(config_s[1]); 1153 } 1154 close(*newsock); 1155 } 1156 1157 /* child process check (or debug mode) */ 1158 if (num_listen_socks < 0) 1159 break; 1160 } 1161 } 1162 1163 /* 1164 * If IP options are supported, make sure there are none (log and 1165 * return an error if any are found). Basically we are worried about 1166 * source routing; it can be used to pretend you are somebody 1167 * (ip-address) you are not. That itself may be "almost acceptable" 1168 * under certain circumstances, but rhosts authentication is useless 1169 * if source routing is accepted. Notice also that if we just dropped 1170 * source routing here, the other side could use IP spoofing to do 1171 * rest of the interaction and could still bypass security. So we 1172 * exit here if we detect any IP options. 1173 */ 1174 static void 1175 check_ip_options(struct ssh *ssh) 1176 { 1177 int sock_in = ssh_packet_get_connection_in(ssh); 1178 struct sockaddr_storage from; 1179 u_char opts[200]; 1180 socklen_t i, option_size = sizeof(opts), fromlen = sizeof(from); 1181 char text[sizeof(opts) * 3 + 1]; 1182 1183 memset(&from, 0, sizeof(from)); 1184 if (getpeername(sock_in, (struct sockaddr *)&from, 1185 &fromlen) < 0) 1186 return; 1187 if (from.ss_family != AF_INET) 1188 return; 1189 /* XXX IPv6 options? */ 1190 1191 if (getsockopt(sock_in, IPPROTO_IP, IP_OPTIONS, opts, 1192 &option_size) >= 0 && option_size != 0) { 1193 text[0] = '\0'; 1194 for (i = 0; i < option_size; i++) 1195 snprintf(text + i*3, sizeof(text) - i*3, 1196 " %2.2x", opts[i]); 1197 fatal("Connection from %.100s port %d with IP opts: %.800s", 1198 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), text); 1199 } 1200 return; 1201 } 1202 1203 /* Set the routing domain for this process */ 1204 static void 1205 set_process_rdomain(struct ssh *ssh, const char *name) 1206 { 1207 int rtable, ortable = getrtable(); 1208 const char *errstr; 1209 1210 if (name == NULL) 1211 return; /* default */ 1212 1213 if (strcmp(name, "%D") == 0) { 1214 /* "expands" to routing domain of connection */ 1215 if ((name = ssh_packet_rdomain_in(ssh)) == NULL) 1216 return; 1217 } 1218 1219 rtable = (int)strtonum(name, 0, 255, &errstr); 1220 if (errstr != NULL) /* Shouldn't happen */ 1221 fatal("Invalid routing domain \"%s\": %s", name, errstr); 1222 if (rtable != ortable && setrtable(rtable) != 0) 1223 fatal("Unable to set routing domain %d: %s", 1224 rtable, strerror(errno)); 1225 debug("%s: set routing domain %d (was %d)", __func__, rtable, ortable); 1226 } 1227 1228 static void 1229 accumulate_host_timing_secret(struct sshbuf *server_cfg, 1230 const struct sshkey *key) 1231 { 1232 static struct ssh_digest_ctx *ctx; 1233 u_char *hash; 1234 size_t len; 1235 struct sshbuf *buf; 1236 int r; 1237 1238 if (ctx == NULL && (ctx = ssh_digest_start(SSH_DIGEST_SHA512)) == NULL) 1239 fatal("%s: ssh_digest_start", __func__); 1240 if (key == NULL) { /* finalize */ 1241 /* add server config in case we are using agent for host keys */ 1242 if (ssh_digest_update(ctx, sshbuf_ptr(server_cfg), 1243 sshbuf_len(server_cfg)) != 0) 1244 fatal("%s: ssh_digest_update", __func__); 1245 len = ssh_digest_bytes(SSH_DIGEST_SHA512); 1246 hash = xmalloc(len); 1247 if (ssh_digest_final(ctx, hash, len) != 0) 1248 fatal("%s: ssh_digest_final", __func__); 1249 options.timing_secret = PEEK_U64(hash); 1250 freezero(hash, len); 1251 ssh_digest_free(ctx); 1252 ctx = NULL; 1253 return; 1254 } 1255 if ((buf = sshbuf_new()) == NULL) 1256 fatal("%s could not allocate buffer", __func__); 1257 if ((r = sshkey_private_serialize(key, buf)) != 0) 1258 fatal("sshkey_private_serialize: %s", ssh_err(r)); 1259 if (ssh_digest_update(ctx, sshbuf_ptr(buf), sshbuf_len(buf)) != 0) 1260 fatal("%s: ssh_digest_update", __func__); 1261 sshbuf_reset(buf); 1262 sshbuf_free(buf); 1263 } 1264 1265 /* 1266 * Main program for the daemon. 1267 */ 1268 int 1269 main(int ac, char **av) 1270 { 1271 struct ssh *ssh = NULL; 1272 extern char *optarg; 1273 extern int optind; 1274 int r, opt, on = 1, already_daemon, remote_port; 1275 int sock_in = -1, sock_out = -1, newsock = -1; 1276 const char *remote_ip, *rdomain; 1277 char *fp, *line, *laddr, *logfile = NULL; 1278 int config_s[2] = { -1 , -1 }; 1279 u_int i, j; 1280 u_int64_t ibytes, obytes; 1281 mode_t new_umask; 1282 struct sshkey *key; 1283 struct sshkey *pubkey; 1284 int keytype; 1285 Authctxt *authctxt; 1286 struct connection_info *connection_info = NULL; 1287 1288 ssh_malloc_init(); /* must be called before any mallocs */ 1289 /* Save argv. */ 1290 saved_argv = av; 1291 rexec_argc = ac; 1292 1293 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 1294 sanitise_stdfd(); 1295 1296 /* Initialize configuration options to their default values. */ 1297 initialize_server_options(&options); 1298 1299 /* Parse command-line arguments. */ 1300 while ((opt = getopt(ac, av, 1301 "C:E:b:c:f:g:h:k:o:p:u:46DQRTdeiqrt")) != -1) { 1302 switch (opt) { 1303 case '4': 1304 options.address_family = AF_INET; 1305 break; 1306 case '6': 1307 options.address_family = AF_INET6; 1308 break; 1309 case 'f': 1310 config_file_name = optarg; 1311 break; 1312 case 'c': 1313 servconf_add_hostcert("[command-line]", 0, 1314 &options, optarg); 1315 break; 1316 case 'd': 1317 if (debug_flag == 0) { 1318 debug_flag = 1; 1319 options.log_level = SYSLOG_LEVEL_DEBUG1; 1320 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) 1321 options.log_level++; 1322 break; 1323 case 'D': 1324 no_daemon_flag = 1; 1325 break; 1326 case 'E': 1327 logfile = optarg; 1328 /* FALLTHROUGH */ 1329 case 'e': 1330 log_stderr = 1; 1331 break; 1332 case 'i': 1333 inetd_flag = 1; 1334 break; 1335 case 'r': 1336 rexec_flag = 0; 1337 break; 1338 case 'R': 1339 rexeced_flag = 1; 1340 inetd_flag = 1; 1341 break; 1342 case 'Q': 1343 /* ignored */ 1344 break; 1345 case 'q': 1346 options.log_level = SYSLOG_LEVEL_QUIET; 1347 break; 1348 case 'b': 1349 /* protocol 1, ignored */ 1350 break; 1351 case 'p': 1352 options.ports_from_cmdline = 1; 1353 if (options.num_ports >= MAX_PORTS) { 1354 fprintf(stderr, "too many ports.\n"); 1355 exit(1); 1356 } 1357 options.ports[options.num_ports++] = a2port(optarg); 1358 if (options.ports[options.num_ports-1] <= 0) { 1359 fprintf(stderr, "Bad port number.\n"); 1360 exit(1); 1361 } 1362 break; 1363 case 'g': 1364 if ((options.login_grace_time = convtime(optarg)) == -1) { 1365 fprintf(stderr, "Invalid login grace time.\n"); 1366 exit(1); 1367 } 1368 break; 1369 case 'k': 1370 /* protocol 1, ignored */ 1371 break; 1372 case 'h': 1373 servconf_add_hostkey("[command-line]", 0, 1374 &options, optarg, 1); 1375 break; 1376 case 't': 1377 test_flag = 1; 1378 break; 1379 case 'T': 1380 test_flag = 2; 1381 break; 1382 case 'C': 1383 connection_info = get_connection_info(0, 0); 1384 if (parse_server_match_testspec(connection_info, 1385 optarg) == -1) 1386 exit(1); 1387 break; 1388 case 'u': 1389 utmp_len = (u_int)strtonum(optarg, 0, HOST_NAME_MAX+1+1, NULL); 1390 if (utmp_len > HOST_NAME_MAX+1) { 1391 fprintf(stderr, "Invalid utmp length.\n"); 1392 exit(1); 1393 } 1394 break; 1395 case 'o': 1396 line = xstrdup(optarg); 1397 if (process_server_config_line(&options, line, 1398 "command-line", 0, NULL, NULL) != 0) 1399 exit(1); 1400 free(line); 1401 break; 1402 case '?': 1403 default: 1404 usage(); 1405 break; 1406 } 1407 } 1408 if (rexeced_flag || inetd_flag) 1409 rexec_flag = 0; 1410 if (!test_flag && rexec_flag && !path_absolute(av[0])) 1411 fatal("sshd re-exec requires execution with an absolute path"); 1412 if (rexeced_flag) 1413 closefrom(REEXEC_MIN_FREE_FD); 1414 else 1415 closefrom(REEXEC_DEVCRYPTO_RESERVED_FD); 1416 1417 #ifdef WITH_OPENSSL 1418 OpenSSL_add_all_algorithms(); 1419 #endif 1420 1421 /* If requested, redirect the logs to the specified logfile. */ 1422 if (logfile != NULL) 1423 log_redirect_stderr_to(logfile); 1424 /* 1425 * Force logging to stderr until we have loaded the private host 1426 * key (unless started from inetd) 1427 */ 1428 log_init(__progname, 1429 options.log_level == SYSLOG_LEVEL_NOT_SET ? 1430 SYSLOG_LEVEL_INFO : options.log_level, 1431 options.log_facility == SYSLOG_FACILITY_NOT_SET ? 1432 SYSLOG_FACILITY_AUTH : options.log_facility, 1433 log_stderr || !inetd_flag); 1434 1435 sensitive_data.have_ssh2_key = 0; 1436 1437 /* 1438 * If we're not doing an extended test do not silently ignore connection 1439 * test params. 1440 */ 1441 if (test_flag < 2 && connection_info != NULL) 1442 fatal("Config test connection parameter (-C) provided without " 1443 "test mode (-T)"); 1444 1445 /* Fetch our configuration */ 1446 if ((cfg = sshbuf_new()) == NULL) 1447 fatal("%s: sshbuf_new failed", __func__); 1448 if (rexeced_flag) 1449 recv_rexec_state(REEXEC_CONFIG_PASS_FD, cfg); 1450 else if (strcasecmp(config_file_name, "none") != 0) 1451 load_server_config(config_file_name, cfg); 1452 1453 parse_server_config(&options, rexeced_flag ? "rexec" : config_file_name, 1454 cfg, NULL); 1455 1456 /* Fill in default values for those options not explicitly set. */ 1457 fill_default_server_options(&options); 1458 1459 /* challenge-response is implemented via keyboard interactive */ 1460 if (options.challenge_response_authentication) 1461 options.kbd_interactive_authentication = 1; 1462 1463 /* Check that options are sensible */ 1464 if (options.authorized_keys_command_user == NULL && 1465 (options.authorized_keys_command != NULL && 1466 strcasecmp(options.authorized_keys_command, "none") != 0)) 1467 fatal("AuthorizedKeysCommand set without " 1468 "AuthorizedKeysCommandUser"); 1469 if (options.authorized_principals_command_user == NULL && 1470 (options.authorized_principals_command != NULL && 1471 strcasecmp(options.authorized_principals_command, "none") != 0)) 1472 fatal("AuthorizedPrincipalsCommand set without " 1473 "AuthorizedPrincipalsCommandUser"); 1474 1475 /* 1476 * Check whether there is any path through configured auth methods. 1477 * Unfortunately it is not possible to verify this generally before 1478 * daemonisation in the presence of Match block, but this catches 1479 * and warns for trivial misconfigurations that could break login. 1480 */ 1481 if (options.num_auth_methods != 0) { 1482 for (i = 0; i < options.num_auth_methods; i++) { 1483 if (auth2_methods_valid(options.auth_methods[i], 1484 1) == 0) 1485 break; 1486 } 1487 if (i >= options.num_auth_methods) 1488 fatal("AuthenticationMethods cannot be satisfied by " 1489 "enabled authentication methods"); 1490 } 1491 1492 /* Check that there are no remaining arguments. */ 1493 if (optind < ac) { 1494 fprintf(stderr, "Extra argument %s.\n", av[optind]); 1495 exit(1); 1496 } 1497 1498 debug("sshd version %s, %s", SSH_VERSION, 1499 #ifdef WITH_OPENSSL 1500 OpenSSL_version(OPENSSL_VERSION) 1501 #else 1502 "without OpenSSL" 1503 #endif 1504 ); 1505 1506 /* load host keys */ 1507 sensitive_data.host_keys = xcalloc(options.num_host_key_files, 1508 sizeof(struct sshkey *)); 1509 sensitive_data.host_pubkeys = xcalloc(options.num_host_key_files, 1510 sizeof(struct sshkey *)); 1511 1512 if (options.host_key_agent) { 1513 if (strcmp(options.host_key_agent, SSH_AUTHSOCKET_ENV_NAME)) 1514 setenv(SSH_AUTHSOCKET_ENV_NAME, 1515 options.host_key_agent, 1); 1516 if ((r = ssh_get_authentication_socket(NULL)) == 0) 1517 have_agent = 1; 1518 else 1519 error("Could not connect to agent \"%s\": %s", 1520 options.host_key_agent, ssh_err(r)); 1521 } 1522 1523 for (i = 0; i < options.num_host_key_files; i++) { 1524 int ll = options.host_key_file_userprovided[i] ? 1525 SYSLOG_LEVEL_ERROR : SYSLOG_LEVEL_DEBUG1; 1526 1527 if (options.host_key_files[i] == NULL) 1528 continue; 1529 if ((r = sshkey_load_private(options.host_key_files[i], "", 1530 &key, NULL)) != 0 && r != SSH_ERR_SYSTEM_ERROR) 1531 do_log2(ll, "Unable to load host key \"%s\": %s", 1532 options.host_key_files[i], ssh_err(r)); 1533 if ((r = sshkey_load_public(options.host_key_files[i], 1534 &pubkey, NULL)) != 0 && r != SSH_ERR_SYSTEM_ERROR) 1535 do_log2(ll, "Unable to load host key \"%s\": %s", 1536 options.host_key_files[i], ssh_err(r)); 1537 if (pubkey == NULL && key != NULL) 1538 if ((r = sshkey_from_private(key, &pubkey)) != 0) 1539 fatal("Could not demote key: \"%s\": %s", 1540 options.host_key_files[i], ssh_err(r)); 1541 sensitive_data.host_keys[i] = key; 1542 sensitive_data.host_pubkeys[i] = pubkey; 1543 1544 if (key == NULL && pubkey != NULL && have_agent) { 1545 debug("will rely on agent for hostkey %s", 1546 options.host_key_files[i]); 1547 keytype = pubkey->type; 1548 } else if (key != NULL) { 1549 keytype = key->type; 1550 accumulate_host_timing_secret(cfg, key); 1551 } else { 1552 do_log2(ll, "Unable to load host key: %s", 1553 options.host_key_files[i]); 1554 sensitive_data.host_keys[i] = NULL; 1555 sensitive_data.host_pubkeys[i] = NULL; 1556 continue; 1557 } 1558 1559 switch (keytype) { 1560 case KEY_RSA: 1561 case KEY_DSA: 1562 case KEY_ECDSA: 1563 case KEY_ED25519: 1564 case KEY_XMSS: 1565 if (have_agent || key != NULL) 1566 sensitive_data.have_ssh2_key = 1; 1567 break; 1568 } 1569 if ((fp = sshkey_fingerprint(pubkey, options.fingerprint_hash, 1570 SSH_FP_DEFAULT)) == NULL) 1571 fatal("sshkey_fingerprint failed"); 1572 debug("%s host key #%d: %s %s", 1573 key ? "private" : "agent", i, sshkey_ssh_name(pubkey), fp); 1574 free(fp); 1575 } 1576 accumulate_host_timing_secret(cfg, NULL); 1577 if (!sensitive_data.have_ssh2_key) { 1578 logit("sshd: no hostkeys available -- exiting."); 1579 exit(1); 1580 } 1581 1582 /* 1583 * Load certificates. They are stored in an array at identical 1584 * indices to the public keys that they relate to. 1585 */ 1586 sensitive_data.host_certificates = xcalloc(options.num_host_key_files, 1587 sizeof(struct sshkey *)); 1588 for (i = 0; i < options.num_host_key_files; i++) 1589 sensitive_data.host_certificates[i] = NULL; 1590 1591 for (i = 0; i < options.num_host_cert_files; i++) { 1592 if (options.host_cert_files[i] == NULL) 1593 continue; 1594 if ((r = sshkey_load_public(options.host_cert_files[i], 1595 &key, NULL)) != 0) { 1596 error("Could not load host certificate \"%s\": %s", 1597 options.host_cert_files[i], ssh_err(r)); 1598 continue; 1599 } 1600 if (!sshkey_is_cert(key)) { 1601 error("Certificate file is not a certificate: %s", 1602 options.host_cert_files[i]); 1603 sshkey_free(key); 1604 continue; 1605 } 1606 /* Find matching private key */ 1607 for (j = 0; j < options.num_host_key_files; j++) { 1608 if (sshkey_equal_public(key, 1609 sensitive_data.host_keys[j])) { 1610 sensitive_data.host_certificates[j] = key; 1611 break; 1612 } 1613 } 1614 if (j >= options.num_host_key_files) { 1615 error("No matching private key for certificate: %s", 1616 options.host_cert_files[i]); 1617 sshkey_free(key); 1618 continue; 1619 } 1620 sensitive_data.host_certificates[j] = key; 1621 debug("host certificate: #%u type %d %s", j, key->type, 1622 sshkey_type(key)); 1623 } 1624 1625 if (use_privsep) { 1626 struct stat st; 1627 1628 if (getpwnam(SSH_PRIVSEP_USER) == NULL) 1629 fatal("Privilege separation user %s does not exist", 1630 SSH_PRIVSEP_USER); 1631 endpwent(); 1632 if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) || 1633 (S_ISDIR(st.st_mode) == 0)) 1634 fatal("Missing privilege separation directory: %s", 1635 _PATH_PRIVSEP_CHROOT_DIR); 1636 if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0) 1637 fatal("%s must be owned by root and not group or " 1638 "world-writable.", _PATH_PRIVSEP_CHROOT_DIR); 1639 } 1640 1641 if (test_flag > 1) { 1642 /* 1643 * If no connection info was provided by -C then use 1644 * use a blank one that will cause no predicate to match. 1645 */ 1646 if (connection_info == NULL) 1647 connection_info = get_connection_info(0, 0); 1648 parse_server_match_config(&options, connection_info); 1649 dump_config(&options); 1650 } 1651 1652 /* Configuration looks good, so exit if in test mode. */ 1653 if (test_flag) 1654 exit(0); 1655 1656 if (rexec_flag) { 1657 if (rexec_argc < 0) 1658 fatal("rexec_argc %d < 0", rexec_argc); 1659 rexec_argv = xcalloc(rexec_argc + 2, sizeof(char *)); 1660 for (i = 0; i < (u_int)rexec_argc; i++) { 1661 debug("rexec_argv[%d]='%s'", i, saved_argv[i]); 1662 rexec_argv[i] = saved_argv[i]; 1663 } 1664 rexec_argv[rexec_argc] = "-R"; 1665 rexec_argv[rexec_argc + 1] = NULL; 1666 } 1667 1668 /* Ensure that umask disallows at least group and world write */ 1669 new_umask = umask(0077) | 0022; 1670 (void) umask(new_umask); 1671 1672 /* Initialize the log (it is reinitialized below in case we forked). */ 1673 if (debug_flag && (!inetd_flag || rexeced_flag)) 1674 log_stderr = 1; 1675 log_init(__progname, options.log_level, options.log_facility, log_stderr); 1676 1677 /* 1678 * If not in debugging mode, not started from inetd and not already 1679 * daemonized (eg re-exec via SIGHUP), disconnect from the controlling 1680 * terminal, and fork. The original process exits. 1681 */ 1682 already_daemon = daemonized(); 1683 if (!(debug_flag || inetd_flag || no_daemon_flag || already_daemon)) { 1684 1685 if (daemon(0, 0) < 0) 1686 fatal("daemon() failed: %.200s", strerror(errno)); 1687 1688 disconnect_controlling_tty(); 1689 } 1690 /* Reinitialize the log (because of the fork above). */ 1691 log_init(__progname, options.log_level, options.log_facility, log_stderr); 1692 1693 /* Chdir to the root directory so that the current disk can be 1694 unmounted if desired. */ 1695 if (chdir("/") == -1) 1696 error("chdir(\"/\"): %s", strerror(errno)); 1697 1698 /* ignore SIGPIPE */ 1699 signal(SIGPIPE, SIG_IGN); 1700 1701 /* Get a connection, either from inetd or a listening TCP socket */ 1702 if (inetd_flag) { 1703 server_accept_inetd(&sock_in, &sock_out); 1704 } else { 1705 server_listen(); 1706 1707 signal(SIGHUP, sighup_handler); 1708 signal(SIGCHLD, main_sigchld_handler); 1709 signal(SIGTERM, sigterm_handler); 1710 signal(SIGQUIT, sigterm_handler); 1711 1712 /* 1713 * Write out the pid file after the sigterm handler 1714 * is setup and the listen sockets are bound 1715 */ 1716 if (options.pid_file != NULL && !debug_flag) { 1717 FILE *f = fopen(options.pid_file, "w"); 1718 1719 if (f == NULL) { 1720 error("Couldn't create pid file \"%s\": %s", 1721 options.pid_file, strerror(errno)); 1722 } else { 1723 fprintf(f, "%ld\n", (long) getpid()); 1724 fclose(f); 1725 } 1726 } 1727 1728 /* Accept a connection and return in a forked child */ 1729 server_accept_loop(&sock_in, &sock_out, 1730 &newsock, config_s); 1731 } 1732 1733 /* This is the child processing a new connection. */ 1734 setproctitle("%s", "[accepted]"); 1735 1736 /* 1737 * Create a new session and process group since the 4.4BSD 1738 * setlogin() affects the entire process group. We don't 1739 * want the child to be able to affect the parent. 1740 */ 1741 if (!debug_flag && !inetd_flag && setsid() < 0) 1742 error("setsid: %.100s", strerror(errno)); 1743 1744 if (rexec_flag) { 1745 int fd; 1746 1747 debug("rexec start in %d out %d newsock %d pipe %d sock %d", 1748 sock_in, sock_out, newsock, startup_pipe, config_s[0]); 1749 dup2(newsock, STDIN_FILENO); 1750 dup2(STDIN_FILENO, STDOUT_FILENO); 1751 if (startup_pipe == -1) 1752 close(REEXEC_STARTUP_PIPE_FD); 1753 else if (startup_pipe != REEXEC_STARTUP_PIPE_FD) { 1754 dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD); 1755 close(startup_pipe); 1756 startup_pipe = REEXEC_STARTUP_PIPE_FD; 1757 } 1758 1759 dup2(config_s[1], REEXEC_CONFIG_PASS_FD); 1760 close(config_s[1]); 1761 1762 execv(rexec_argv[0], rexec_argv); 1763 1764 /* Reexec has failed, fall back and continue */ 1765 error("rexec of %s failed: %s", rexec_argv[0], strerror(errno)); 1766 recv_rexec_state(REEXEC_CONFIG_PASS_FD, NULL); 1767 log_init(__progname, options.log_level, 1768 options.log_facility, log_stderr); 1769 1770 /* Clean up fds */ 1771 close(REEXEC_CONFIG_PASS_FD); 1772 newsock = sock_out = sock_in = dup(STDIN_FILENO); 1773 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 1774 dup2(fd, STDIN_FILENO); 1775 dup2(fd, STDOUT_FILENO); 1776 if (fd > STDERR_FILENO) 1777 close(fd); 1778 } 1779 debug("rexec cleanup in %d out %d newsock %d pipe %d sock %d", 1780 sock_in, sock_out, newsock, startup_pipe, config_s[0]); 1781 } 1782 1783 /* Executed child processes don't need these. */ 1784 fcntl(sock_out, F_SETFD, FD_CLOEXEC); 1785 fcntl(sock_in, F_SETFD, FD_CLOEXEC); 1786 1787 /* 1788 * Disable the key regeneration alarm. We will not regenerate the 1789 * key since we are no longer in a position to give it to anyone. We 1790 * will not restart on SIGHUP since it no longer makes sense. 1791 */ 1792 alarm(0); 1793 signal(SIGALRM, SIG_DFL); 1794 signal(SIGHUP, SIG_DFL); 1795 signal(SIGTERM, SIG_DFL); 1796 signal(SIGQUIT, SIG_DFL); 1797 signal(SIGCHLD, SIG_DFL); 1798 1799 /* 1800 * Register our connection. This turns encryption off because we do 1801 * not have a key. 1802 */ 1803 packet_set_connection(sock_in, sock_out); 1804 packet_set_server(); 1805 ssh = active_state; /* XXX */ 1806 1807 check_ip_options(ssh); 1808 1809 /* Prepare the channels layer */ 1810 channel_init_channels(ssh); 1811 channel_set_af(ssh, options.address_family); 1812 process_permitopen(ssh, &options); 1813 1814 /* Set SO_KEEPALIVE if requested. */ 1815 if (options.tcp_keep_alive && packet_connection_is_on_socket() && 1816 setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0) 1817 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno)); 1818 1819 if ((remote_port = ssh_remote_port(ssh)) < 0) { 1820 debug("ssh_remote_port failed"); 1821 cleanup_exit(255); 1822 } 1823 1824 /* 1825 * The rest of the code depends on the fact that 1826 * ssh_remote_ipaddr() caches the remote ip, even if 1827 * the socket goes away. 1828 */ 1829 remote_ip = ssh_remote_ipaddr(ssh); 1830 1831 rdomain = ssh_packet_rdomain_in(ssh); 1832 1833 /* Log the connection. */ 1834 laddr = get_local_ipaddr(sock_in); 1835 verbose("Connection from %s port %d on %s port %d%s%s%s", 1836 remote_ip, remote_port, laddr, ssh_local_port(ssh), 1837 rdomain == NULL ? "" : " rdomain \"", 1838 rdomain == NULL ? "" : rdomain, 1839 rdomain == NULL ? "" : "\""); 1840 free(laddr); 1841 1842 /* 1843 * We don't want to listen forever unless the other side 1844 * successfully authenticates itself. So we set up an alarm which is 1845 * cleared after successful authentication. A limit of zero 1846 * indicates no limit. Note that we don't set the alarm in debugging 1847 * mode; it is just annoying to have the server exit just when you 1848 * are about to discover the bug. 1849 */ 1850 signal(SIGALRM, grace_alarm_handler); 1851 if (!debug_flag) 1852 alarm(options.login_grace_time); 1853 1854 if (kex_exchange_identification(ssh, -1, options.version_addendum) != 0) 1855 cleanup_exit(255); /* error already logged */ 1856 1857 packet_set_nonblocking(); 1858 1859 /* allocate authentication context */ 1860 authctxt = xcalloc(1, sizeof(*authctxt)); 1861 1862 /* XXX global for cleanup, access from other modules */ 1863 the_authctxt = authctxt; 1864 1865 /* Set default key authentication options */ 1866 if ((auth_opts = sshauthopt_new_with_keys_defaults()) == NULL) 1867 fatal("allocation failed"); 1868 1869 /* prepare buffer to collect messages to display to user after login */ 1870 if ((loginmsg = sshbuf_new()) == NULL) 1871 fatal("%s: sshbuf_new failed", __func__); 1872 auth_debug_reset(); 1873 1874 if (use_privsep) { 1875 if (privsep_preauth(authctxt) == 1) 1876 goto authenticated; 1877 } else if (have_agent) { 1878 if ((r = ssh_get_authentication_socket(&auth_sock)) != 0) { 1879 error("Unable to get agent socket: %s", ssh_err(r)); 1880 have_agent = 0; 1881 } 1882 } 1883 1884 /* perform the key exchange */ 1885 /* authenticate user and start session */ 1886 do_ssh2_kex(); 1887 do_authentication2(authctxt); 1888 1889 /* 1890 * If we use privilege separation, the unprivileged child transfers 1891 * the current keystate and exits 1892 */ 1893 if (use_privsep) { 1894 mm_send_keystate(pmonitor); 1895 packet_clear_keys(); 1896 exit(0); 1897 } 1898 1899 authenticated: 1900 /* 1901 * Cancel the alarm we set to limit the time taken for 1902 * authentication. 1903 */ 1904 alarm(0); 1905 signal(SIGALRM, SIG_DFL); 1906 authctxt->authenticated = 1; 1907 if (startup_pipe != -1) { 1908 close(startup_pipe); 1909 startup_pipe = -1; 1910 } 1911 1912 if (options.routing_domain != NULL) 1913 set_process_rdomain(ssh, options.routing_domain); 1914 1915 /* 1916 * In privilege separation, we fork another child and prepare 1917 * file descriptor passing. 1918 */ 1919 if (use_privsep) { 1920 privsep_postauth(authctxt); 1921 /* the monitor process [priv] will not return */ 1922 } 1923 1924 packet_set_timeout(options.client_alive_interval, 1925 options.client_alive_count_max); 1926 1927 /* Try to send all our hostkeys to the client */ 1928 notify_hostkeys(ssh); 1929 1930 /* Start session. */ 1931 do_authenticated(ssh, authctxt); 1932 1933 /* The connection has been terminated. */ 1934 packet_get_bytes(&ibytes, &obytes); 1935 verbose("Transferred: sent %llu, received %llu bytes", 1936 (unsigned long long)obytes, (unsigned long long)ibytes); 1937 1938 verbose("Closing connection to %.500s port %d", remote_ip, remote_port); 1939 packet_close(); 1940 1941 if (use_privsep) 1942 mm_terminate(); 1943 1944 exit(0); 1945 } 1946 1947 int 1948 sshd_hostkey_sign(struct sshkey *privkey, struct sshkey *pubkey, 1949 u_char **signature, size_t *slenp, const u_char *data, size_t dlen, 1950 const char *alg, u_int flag) 1951 { 1952 int r; 1953 1954 if (privkey) { 1955 if (PRIVSEP(sshkey_sign(privkey, signature, slenp, data, dlen, 1956 alg, datafellows)) < 0) 1957 fatal("%s: key_sign failed", __func__); 1958 } else if (use_privsep) { 1959 if (mm_sshkey_sign(pubkey, signature, slenp, data, dlen, 1960 alg, datafellows) < 0) 1961 fatal("%s: pubkey_sign failed", __func__); 1962 } else { 1963 if ((r = ssh_agent_sign(auth_sock, pubkey, signature, slenp, 1964 data, dlen, alg, datafellows)) != 0) 1965 fatal("%s: ssh_agent_sign failed: %s", 1966 __func__, ssh_err(r)); 1967 } 1968 return 0; 1969 } 1970 1971 /* SSH2 key exchange */ 1972 static void 1973 do_ssh2_kex(void) 1974 { 1975 char *myproposal[PROPOSAL_MAX] = { KEX_SERVER }; 1976 struct kex *kex; 1977 int r; 1978 1979 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal( 1980 options.kex_algorithms); 1981 myproposal[PROPOSAL_ENC_ALGS_CTOS] = compat_cipher_proposal( 1982 options.ciphers); 1983 myproposal[PROPOSAL_ENC_ALGS_STOC] = compat_cipher_proposal( 1984 options.ciphers); 1985 myproposal[PROPOSAL_MAC_ALGS_CTOS] = 1986 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; 1987 1988 if (options.compression == COMP_NONE) { 1989 myproposal[PROPOSAL_COMP_ALGS_CTOS] = 1990 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none"; 1991 } 1992 1993 if (options.rekey_limit || options.rekey_interval) 1994 packet_set_rekey_limits(options.rekey_limit, 1995 options.rekey_interval); 1996 1997 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal( 1998 list_hostkey_types()); 1999 2000 /* start key exchange */ 2001 if ((r = kex_setup(active_state, myproposal)) != 0) 2002 fatal("kex_setup: %s", ssh_err(r)); 2003 kex = active_state->kex; 2004 #ifdef WITH_OPENSSL 2005 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server; 2006 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server; 2007 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server; 2008 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server; 2009 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server; 2010 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; 2011 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server; 2012 kex->kex[KEX_ECDH_SHA2] = kexecdh_server; 2013 #endif 2014 kex->kex[KEX_C25519_SHA256] = kexc25519_server; 2015 kex->load_host_public_key=&get_hostkey_public_by_type; 2016 kex->load_host_private_key=&get_hostkey_private_by_type; 2017 kex->host_key_index=&get_hostkey_index; 2018 kex->sign = sshd_hostkey_sign; 2019 2020 ssh_dispatch_run_fatal(active_state, DISPATCH_BLOCK, &kex->done); 2021 2022 session_id2 = kex->session_id; 2023 session_id2_len = kex->session_id_len; 2024 2025 #ifdef DEBUG_KEXDH 2026 /* send 1st encrypted/maced/compressed message */ 2027 packet_start(SSH2_MSG_IGNORE); 2028 packet_put_cstring("markus"); 2029 packet_send(); 2030 packet_write_wait(); 2031 #endif 2032 debug("KEX done"); 2033 } 2034 2035 /* server specific fatal cleanup */ 2036 void 2037 cleanup_exit(int i) 2038 { 2039 struct ssh *ssh = active_state; /* XXX */ 2040 2041 if (the_authctxt) { 2042 do_cleanup(ssh, the_authctxt); 2043 if (use_privsep && privsep_is_preauth && 2044 pmonitor != NULL && pmonitor->m_pid > 1) { 2045 debug("Killing privsep child %d", pmonitor->m_pid); 2046 if (kill(pmonitor->m_pid, SIGKILL) != 0 && 2047 errno != ESRCH) 2048 error("%s: kill(%d): %s", __func__, 2049 pmonitor->m_pid, strerror(errno)); 2050 } 2051 } 2052 _exit(i); 2053 } 2054