1 /* $OpenBSD: sshd.c,v 1.444 2015/02/20 22:17:21 djm Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
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.
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".
18 * SSH2 implementation:
19 * Privilege Separation:
21 * Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved.
22 * Copyright (c) 2002 Niels Provos. All rights reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
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.
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.
47 #include <sys/types.h>
48 #include <sys/ioctl.h>
49 #include <sys/socket.h>
50 #ifdef HAVE_SYS_STAT_H
51 # include <sys/stat.h>
53 #ifdef HAVE_SYS_TIME_H
54 # include <sys/time.h>
56 #include "openbsd-compat/sys-tree.h"
57 #include "openbsd-compat/sys-queue.h"
77 #include <openssl/dh.h>
78 #include <openssl/bn.h>
79 #include <openssl/rand.h>
80 #include "openbsd-compat/openssl-compat.h"
83 #ifdef HAVE_SECUREWARE
84 #include <sys/security.h>
105 #include "myproposal.h"
106 #include "authfile.h"
107 #include "pathnames.h"
108 #include "atomicio.h"
109 #include "canohost.h"
110 #include "hostfile.h"
114 #include "dispatch.h"
115 #include "channels.h"
117 #include "monitor_mm.h"
122 #include "monitor_wrap.h"
124 #include "ssh-sandbox.h"
133 #define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1)
134 #define REEXEC_STARTUP_PIPE_FD (STDERR_FILENO + 2)
135 #define REEXEC_CONFIG_PASS_FD (STDERR_FILENO + 3)
136 #define REEXEC_MIN_FREE_FD (STDERR_FILENO + 4)
138 extern char *__progname;
140 /* Server configuration options. */
141 ServerOptions options;
143 /* Name of the server configuration file. */
144 char *config_file_name = _PATH_SERVER_CONFIG_FILE;
147 * Debug mode flag. This can be set on the command line. If debug
148 * mode is enabled, extra debugging output will be sent to the system
149 * log, the daemon will not go to background, and will exit after processing
150 * the first connection.
154 /* Flag indicating that the daemon should only test the configuration and keys. */
157 /* Flag indicating that the daemon is being started from inetd. */
160 /* Flag indicating that sshd should not detach and become a daemon. */
161 int no_daemon_flag = 0;
163 /* debug goes to stderr unless inetd_flag is set */
166 /* Saved arguments to main(). */
171 int rexeced_flag = 0;
177 * The sockets that the server is listening; this is used in the SIGHUP
180 #define MAX_LISTEN_SOCKS 16
181 int listen_socks[MAX_LISTEN_SOCKS];
182 int num_listen_socks = 0;
185 * the client's version string, passed by sshd2 in compat mode. if != NULL,
186 * sshd will skip the version-number exchange
188 char *client_version_string = NULL;
189 char *server_version_string = NULL;
191 /* Daemon's agent connection */
196 * Any really sensitive data in the application is contained in this
197 * structure. The idea is that this structure could be locked into memory so
198 * that the pages do not get written into swap. However, there are some
199 * problems. The private key contains BIGNUMs, and we do not (in principle)
200 * have access to the internals of them, and locking just the structure is
201 * not very useful. Currently, memory locking is not implemented.
204 Key *server_key; /* ephemeral server key */
205 Key *ssh1_host_key; /* ssh1 host key */
206 Key **host_keys; /* all private host keys */
207 Key **host_pubkeys; /* all public host keys */
208 Key **host_certificates; /* all public host certificates */
211 u_char ssh1_cookie[SSH_SESSION_KEY_LENGTH];
215 * Flag indicating whether the RSA server key needs to be regenerated.
216 * Is set in the SIGALRM handler and cleared when the key is regenerated.
218 static volatile sig_atomic_t key_do_regen = 0;
220 /* This is set to true when a signal is received. */
221 static volatile sig_atomic_t received_sighup = 0;
222 static volatile sig_atomic_t received_sigterm = 0;
224 /* session identifier, used by RSA-auth */
225 u_char session_id[16];
228 u_char *session_id2 = NULL;
229 u_int session_id2_len = 0;
231 /* record remote hostname or ip */
232 u_int utmp_len = HOST_NAME_MAX+1;
234 /* options.max_startup sized array of fd ints */
235 int *startup_pipes = NULL;
236 int startup_pipe; /* in child */
238 /* variables used for privilege separation */
239 int use_privsep = -1;
240 struct monitor *pmonitor = NULL;
241 int privsep_is_preauth = 1;
243 /* global authentication context */
244 Authctxt *the_authctxt = NULL;
246 /* sshd_config buffer */
249 /* message to be displayed after login */
252 /* Unprivileged user */
253 struct passwd *privsep_pw = NULL;
255 /* Prototypes for various functions defined later in this file. */
256 void destroy_sensitive_data(void);
257 void demote_sensitive_data(void);
260 static void do_ssh1_kex(void);
262 static void do_ssh2_kex(void);
265 * Close all listening sockets
268 close_listen_socks(void)
272 for (i = 0; i < num_listen_socks; i++)
273 close(listen_socks[i]);
274 num_listen_socks = -1;
278 close_startup_pipes(void)
283 for (i = 0; i < options.max_startups; i++)
284 if (startup_pipes[i] != -1)
285 close(startup_pipes[i]);
289 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
290 * the effect is to reread the configuration file (and to regenerate
296 sighup_handler(int sig)
298 int save_errno = errno;
301 signal(SIGHUP, sighup_handler);
306 * Called from the main program after receiving SIGHUP.
307 * Restarts the server.
312 logit("Received SIGHUP; restarting.");
313 platform_pre_restart();
314 close_listen_socks();
315 close_startup_pipes();
316 alarm(0); /* alarm timer persists across exec */
317 signal(SIGHUP, SIG_IGN); /* will be restored after exec */
318 execv(saved_argv[0], saved_argv);
319 logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
325 * Generic signal handler for terminating signals in the master daemon.
329 sigterm_handler(int sig)
331 received_sigterm = sig;
335 * SIGCHLD handler. This is called whenever a child dies. This will then
336 * reap any zombies left by exited children.
340 main_sigchld_handler(int sig)
342 int save_errno = errno;
346 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
347 (pid < 0 && errno == EINTR))
350 signal(SIGCHLD, main_sigchld_handler);
355 * Signal handler for the alarm after the login grace period has expired.
359 grace_alarm_handler(int sig)
361 if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0)
362 kill(pmonitor->m_pid, SIGALRM);
365 * Try to kill any processes that we have spawned, E.g. authorized
366 * keys command helpers.
368 if (getpgid(0) == getpid()) {
369 signal(SIGTERM, SIG_IGN);
373 /* Log error and exit. */
374 sigdie("Timeout before authentication for %s", get_remote_ipaddr());
378 * Signal handler for the key regeneration alarm. Note that this
379 * alarm only occurs in the daemon waiting for connections, and it does not
380 * do anything with the private key or random state before forking.
381 * Thus there should be no concurrency control/asynchronous execution
385 generate_ephemeral_server_key(void)
387 verbose("Generating %s%d bit RSA key.",
388 sensitive_data.server_key ? "new " : "", options.server_key_bits);
389 if (sensitive_data.server_key != NULL)
390 key_free(sensitive_data.server_key);
391 sensitive_data.server_key = key_generate(KEY_RSA1,
392 options.server_key_bits);
393 verbose("RSA key generation complete.");
395 arc4random_buf(sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
400 key_regeneration_alarm(int sig)
402 int save_errno = errno;
404 signal(SIGALRM, SIG_DFL);
410 sshd_exchange_identification(int sock_in, int sock_out)
414 int remote_major, remote_minor;
416 char *s, *newline = "\n";
417 char buf[256]; /* Must not be larger than remote_version. */
418 char remote_version[256]; /* Must be at least as big as buf. */
420 if ((options.protocol & SSH_PROTO_1) &&
421 (options.protocol & SSH_PROTO_2)) {
422 major = PROTOCOL_MAJOR_1;
424 } else if (options.protocol & SSH_PROTO_2) {
425 major = PROTOCOL_MAJOR_2;
426 minor = PROTOCOL_MINOR_2;
429 major = PROTOCOL_MAJOR_1;
430 minor = PROTOCOL_MINOR_1;
433 xasprintf(&server_version_string, "SSH-%d.%d-%.100s%s%s%s",
434 major, minor, SSH_VERSION,
435 *options.version_addendum == '\0' ? "" : " ",
436 options.version_addendum, newline);
438 /* Send our protocol version identification. */
439 if (roaming_atomicio(vwrite, sock_out, server_version_string,
440 strlen(server_version_string))
441 != strlen(server_version_string)) {
442 logit("Could not write ident string to %s", get_remote_ipaddr());
446 /* Read other sides version identification. */
447 memset(buf, 0, sizeof(buf));
448 for (i = 0; i < sizeof(buf) - 1; i++) {
449 if (roaming_atomicio(read, sock_in, &buf[i], 1) != 1) {
450 logit("Did not receive identification string from %s",
451 get_remote_ipaddr());
454 if (buf[i] == '\r') {
456 /* Kludge for F-Secure Macintosh < 1.0.2 */
458 strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
462 if (buf[i] == '\n') {
467 buf[sizeof(buf) - 1] = 0;
468 client_version_string = xstrdup(buf);
471 * Check that the versions match. In future this might accept
472 * several versions and set appropriate flags to handle them.
474 if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
475 &remote_major, &remote_minor, remote_version) != 3) {
476 s = "Protocol mismatch.\n";
477 (void) atomicio(vwrite, sock_out, s, strlen(s));
478 logit("Bad protocol version identification '%.100s' "
479 "from %s port %d", client_version_string,
480 get_remote_ipaddr(), get_remote_port());
485 debug("Client protocol version %d.%d; client software version %.100s",
486 remote_major, remote_minor, remote_version);
488 active_state->compat = compat_datafellows(remote_version);
490 if ((datafellows & SSH_BUG_PROBE) != 0) {
491 logit("probed from %s with %s. Don't panic.",
492 get_remote_ipaddr(), client_version_string);
495 if ((datafellows & SSH_BUG_SCANNER) != 0) {
496 logit("scanned from %s with %s. Don't panic.",
497 get_remote_ipaddr(), client_version_string);
500 if ((datafellows & SSH_BUG_RSASIGMD5) != 0) {
501 logit("Client version \"%.100s\" uses unsafe RSA signature "
502 "scheme; disabling use of RSA keys", remote_version);
504 if ((datafellows & SSH_BUG_DERIVEKEY) != 0) {
505 fatal("Client version \"%.100s\" uses unsafe key agreement; "
506 "refusing connection", remote_version);
510 switch (remote_major) {
512 if (remote_minor == 99) {
513 if (options.protocol & SSH_PROTO_2)
519 if (!(options.protocol & SSH_PROTO_1)) {
523 if (remote_minor < 3) {
524 packet_disconnect("Your ssh version is too old and "
525 "is no longer supported. Please install a newer version.");
526 } else if (remote_minor == 3) {
527 /* note that this disables agent-forwarding */
532 if (options.protocol & SSH_PROTO_2) {
541 chop(server_version_string);
542 debug("Local version string %.200s", server_version_string);
545 s = "Protocol major versions differ.\n";
546 (void) atomicio(vwrite, sock_out, s, strlen(s));
549 logit("Protocol major versions differ for %s: %.200s vs. %.200s",
551 server_version_string, client_version_string);
556 /* Destroy the host and server keys. They will no longer be needed. */
558 destroy_sensitive_data(void)
562 if (sensitive_data.server_key) {
563 key_free(sensitive_data.server_key);
564 sensitive_data.server_key = NULL;
566 for (i = 0; i < options.num_host_key_files; i++) {
567 if (sensitive_data.host_keys[i]) {
568 key_free(sensitive_data.host_keys[i]);
569 sensitive_data.host_keys[i] = NULL;
571 if (sensitive_data.host_certificates[i]) {
572 key_free(sensitive_data.host_certificates[i]);
573 sensitive_data.host_certificates[i] = NULL;
576 sensitive_data.ssh1_host_key = NULL;
577 explicit_bzero(sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
580 /* Demote private to public keys for network child */
582 demote_sensitive_data(void)
587 if (sensitive_data.server_key) {
588 tmp = key_demote(sensitive_data.server_key);
589 key_free(sensitive_data.server_key);
590 sensitive_data.server_key = tmp;
593 for (i = 0; i < options.num_host_key_files; i++) {
594 if (sensitive_data.host_keys[i]) {
595 tmp = key_demote(sensitive_data.host_keys[i]);
596 key_free(sensitive_data.host_keys[i]);
597 sensitive_data.host_keys[i] = tmp;
598 if (tmp->type == KEY_RSA1)
599 sensitive_data.ssh1_host_key = tmp;
601 /* Certs do not need demotion */
604 /* We do not clear ssh1_host key and cookie. XXX - Okay Niels? */
608 privsep_preauth_child(void)
613 /* Enable challenge-response authentication for privilege separation */
614 privsep_challenge_enable();
617 /* Cache supported mechanism OIDs for later use */
618 if (options.gss_authentication)
619 ssh_gssapi_prepare_supported_oids();
623 arc4random_buf(rnd, sizeof(rnd));
625 RAND_seed(rnd, sizeof(rnd));
626 if ((RAND_bytes((u_char *)rnd, 1)) != 1)
627 fatal("%s: RAND_bytes failed", __func__);
629 explicit_bzero(rnd, sizeof(rnd));
631 /* Demote the private keys to public keys. */
632 demote_sensitive_data();
634 /* Change our root directory */
635 if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
636 fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
638 if (chdir("/") == -1)
639 fatal("chdir(\"/\"): %s", strerror(errno));
641 /* Drop our privileges */
642 debug3("privsep user:group %u:%u", (u_int)privsep_pw->pw_uid,
643 (u_int)privsep_pw->pw_gid);
645 /* XXX not ready, too heavy after chroot */
646 do_setusercontext(privsep_pw);
648 gidset[0] = privsep_pw->pw_gid;
649 if (setgroups(1, gidset) < 0)
650 fatal("setgroups: %.100s", strerror(errno));
651 permanently_set_uid(privsep_pw);
656 privsep_preauth(Authctxt *authctxt)
660 struct ssh_sandbox *box = NULL;
662 /* Set up unprivileged child process to deal with network data */
663 pmonitor = monitor_init();
664 /* Store a pointer to the kex for later rekeying */
665 pmonitor->m_pkex = &active_state->kex;
667 if (use_privsep == PRIVSEP_ON)
668 box = ssh_sandbox_init(pmonitor);
671 fatal("fork of unprivileged child failed");
672 } else if (pid != 0) {
673 debug2("Network child is on pid %ld", (long)pid);
675 pmonitor->m_pid = pid;
677 r = ssh_get_authentication_socket(&auth_sock);
679 error("Could not get agent socket: %s",
685 ssh_sandbox_parent_preauth(box, pid);
686 monitor_child_preauth(authctxt, pmonitor);
689 monitor_sync(pmonitor);
691 /* Wait for the child's exit status */
692 while (waitpid(pid, &status, 0) < 0) {
695 pmonitor->m_pid = -1;
696 fatal("%s: waitpid: %s", __func__, strerror(errno));
698 privsep_is_preauth = 0;
699 pmonitor->m_pid = -1;
700 if (WIFEXITED(status)) {
701 if (WEXITSTATUS(status) != 0)
702 fatal("%s: preauth child exited with status %d",
703 __func__, WEXITSTATUS(status));
704 } else if (WIFSIGNALED(status))
705 fatal("%s: preauth child terminated by signal %d",
706 __func__, WTERMSIG(status));
708 ssh_sandbox_parent_finish(box);
712 close(pmonitor->m_sendfd);
713 close(pmonitor->m_log_recvfd);
715 /* Arrange for logging to be sent to the monitor */
716 set_log_handler(mm_log_handler, pmonitor);
718 /* Demote the child */
719 if (getuid() == 0 || geteuid() == 0)
720 privsep_preauth_child();
721 setproctitle("%s", "[net]");
723 ssh_sandbox_child(box);
730 privsep_postauth(Authctxt *authctxt)
734 #ifdef DISABLE_FD_PASSING
737 if (authctxt->pw->pw_uid == 0 || options.use_login) {
739 /* File descriptor passing is broken or root login */
744 /* New socket pair */
745 monitor_reinit(pmonitor);
747 pmonitor->m_pid = fork();
748 if (pmonitor->m_pid == -1)
749 fatal("fork of unprivileged child failed");
750 else if (pmonitor->m_pid != 0) {
751 verbose("User child is on pid %ld", (long)pmonitor->m_pid);
752 buffer_clear(&loginmsg);
753 monitor_child_postauth(pmonitor);
761 close(pmonitor->m_sendfd);
762 pmonitor->m_sendfd = -1;
764 /* Demote the private keys to public keys. */
765 demote_sensitive_data();
768 arc4random_buf(rnd, sizeof(rnd));
770 RAND_seed(rnd, sizeof(rnd));
771 if ((RAND_bytes((u_char *)rnd, 1)) != 1)
772 fatal("%s: RAND_bytes failed", __func__);
774 explicit_bzero(rnd, sizeof(rnd));
776 /* Drop privileges */
777 do_setusercontext(authctxt->pw);
780 /* It is safe now to apply the key state */
781 monitor_apply_keystate(pmonitor);
784 * Tell the packet layer that authentication was successful, since
785 * this information is not part of the key state.
787 packet_set_authenticated();
791 list_hostkey_types(void)
800 for (i = 0; i < options.num_host_key_files; i++) {
801 key = sensitive_data.host_keys[i];
803 key = sensitive_data.host_pubkeys[i];
811 if (buffer_len(&b) > 0)
812 buffer_append(&b, ",", 1);
813 p = key_ssh_name(key);
814 buffer_append(&b, p, strlen(p));
817 /* If the private key has a cert peer, then list that too */
818 key = sensitive_data.host_certificates[i];
822 case KEY_RSA_CERT_V00:
823 case KEY_DSA_CERT_V00:
827 case KEY_ED25519_CERT:
828 if (buffer_len(&b) > 0)
829 buffer_append(&b, ",", 1);
830 p = key_ssh_name(key);
831 buffer_append(&b, p, strlen(p));
835 buffer_append(&b, "\0", 1);
836 ret = xstrdup(buffer_ptr(&b));
838 debug("list_hostkey_types: %s", ret);
843 get_hostkey_by_type(int type, int nid, int need_private, struct ssh *ssh)
848 for (i = 0; i < options.num_host_key_files; i++) {
850 case KEY_RSA_CERT_V00:
851 case KEY_DSA_CERT_V00:
855 case KEY_ED25519_CERT:
856 key = sensitive_data.host_certificates[i];
859 key = sensitive_data.host_keys[i];
860 if (key == NULL && !need_private)
861 key = sensitive_data.host_pubkeys[i];
864 if (key != NULL && key->type == type &&
865 (key->type != KEY_ECDSA || key->ecdsa_nid == nid))
866 return need_private ?
867 sensitive_data.host_keys[i] : key;
873 get_hostkey_public_by_type(int type, int nid, struct ssh *ssh)
875 return get_hostkey_by_type(type, nid, 0, ssh);
879 get_hostkey_private_by_type(int type, int nid, struct ssh *ssh)
881 return get_hostkey_by_type(type, nid, 1, ssh);
885 get_hostkey_by_index(int ind)
887 if (ind < 0 || ind >= options.num_host_key_files)
889 return (sensitive_data.host_keys[ind]);
893 get_hostkey_public_by_index(int ind, struct ssh *ssh)
895 if (ind < 0 || ind >= options.num_host_key_files)
897 return (sensitive_data.host_pubkeys[ind]);
901 get_hostkey_index(Key *key, int compare, struct ssh *ssh)
905 for (i = 0; i < options.num_host_key_files; i++) {
906 if (key_is_cert(key)) {
907 if (key == sensitive_data.host_certificates[i] ||
908 (compare && sensitive_data.host_certificates[i] &&
910 sensitive_data.host_certificates[i])))
913 if (key == sensitive_data.host_keys[i] ||
914 (compare && sensitive_data.host_keys[i] &&
915 sshkey_equal(key, sensitive_data.host_keys[i])))
917 if (key == sensitive_data.host_pubkeys[i] ||
918 (compare && sensitive_data.host_pubkeys[i] &&
919 sshkey_equal(key, sensitive_data.host_pubkeys[i])))
926 /* Inform the client of all hostkeys */
928 notify_hostkeys(struct ssh *ssh)
935 if ((buf = sshbuf_new()) == NULL)
936 fatal("%s: sshbuf_new", __func__);
937 for (i = nkeys = 0; i < options.num_host_key_files; i++) {
938 key = get_hostkey_public_by_index(i, ssh);
939 if (key == NULL || key->type == KEY_UNSPEC ||
940 key->type == KEY_RSA1 || sshkey_is_cert(key))
942 fp = sshkey_fingerprint(key, options.fingerprint_hash,
944 debug3("%s: key %d: %s %s", __func__, i,
945 sshkey_ssh_name(key), fp);
948 packet_start(SSH2_MSG_GLOBAL_REQUEST);
949 packet_put_cstring("hostkeys-00@openssh.com");
950 packet_put_char(0); /* want-reply */
953 if ((r = sshkey_putb(key, buf)) != 0)
954 fatal("%s: couldn't put hostkey %d: %s",
955 __func__, i, ssh_err(r));
956 packet_put_string(sshbuf_ptr(buf), sshbuf_len(buf));
959 debug3("%s: sent %d hostkeys", __func__, nkeys);
961 fatal("%s: no hostkeys", __func__);
967 * returns 1 if connection should be dropped, 0 otherwise.
968 * dropping starts at connection #max_startups_begin with a probability
969 * of (max_startups_rate/100). the probability increases linearly until
970 * all connections are dropped for startups > max_startups
973 drop_connection(int startups)
977 if (startups < options.max_startups_begin)
979 if (startups >= options.max_startups)
981 if (options.max_startups_rate == 100)
984 p = 100 - options.max_startups_rate;
985 p *= startups - options.max_startups_begin;
986 p /= options.max_startups - options.max_startups_begin;
987 p += options.max_startups_rate;
988 r = arc4random_uniform(100);
990 debug("drop_connection: p %d, r %d", p, r);
991 return (r < p) ? 1 : 0;
997 fprintf(stderr, "%s, %s\n",
1000 SSLeay_version(SSLEAY_VERSION)
1006 "usage: sshd [-46DdeiqTt] [-b bits] [-C connection_spec] [-c host_cert_file]\n"
1007 " [-E log_file] [-f config_file] [-g login_grace_time]\n"
1008 " [-h host_key_file] [-k key_gen_time] [-o option] [-p port]\n"
1015 send_rexec_state(int fd, Buffer *conf)
1019 debug3("%s: entering fd = %d config len %d", __func__, fd,
1023 * Protocol from reexec master to child:
1024 * string configuration
1025 * u_int ephemeral_key_follows
1026 * bignum e (only if ephemeral_key_follows == 1)
1032 * string rngseed (only if OpenSSL is not self-seeded)
1035 buffer_put_cstring(&m, buffer_ptr(conf));
1038 if (sensitive_data.server_key != NULL &&
1039 sensitive_data.server_key->type == KEY_RSA1) {
1040 buffer_put_int(&m, 1);
1041 buffer_put_bignum(&m, sensitive_data.server_key->rsa->e);
1042 buffer_put_bignum(&m, sensitive_data.server_key->rsa->n);
1043 buffer_put_bignum(&m, sensitive_data.server_key->rsa->d);
1044 buffer_put_bignum(&m, sensitive_data.server_key->rsa->iqmp);
1045 buffer_put_bignum(&m, sensitive_data.server_key->rsa->p);
1046 buffer_put_bignum(&m, sensitive_data.server_key->rsa->q);
1049 buffer_put_int(&m, 0);
1051 #if defined(WITH_OPENSSL) && !defined(OPENSSL_PRNG_ONLY)
1052 rexec_send_rng_seed(&m);
1055 if (ssh_msg_send(fd, 0, &m) == -1)
1056 fatal("%s: ssh_msg_send failed", __func__);
1060 debug3("%s: done", __func__);
1064 recv_rexec_state(int fd, Buffer *conf)
1070 debug3("%s: entering fd = %d", __func__, fd);
1074 if (ssh_msg_recv(fd, &m) == -1)
1075 fatal("%s: ssh_msg_recv failed", __func__);
1076 if (buffer_get_char(&m) != 0)
1077 fatal("%s: rexec version mismatch", __func__);
1079 cp = buffer_get_string(&m, &len);
1081 buffer_append(conf, cp, len + 1);
1084 if (buffer_get_int(&m)) {
1086 if (sensitive_data.server_key != NULL)
1087 key_free(sensitive_data.server_key);
1088 sensitive_data.server_key = key_new_private(KEY_RSA1);
1089 buffer_get_bignum(&m, sensitive_data.server_key->rsa->e);
1090 buffer_get_bignum(&m, sensitive_data.server_key->rsa->n);
1091 buffer_get_bignum(&m, sensitive_data.server_key->rsa->d);
1092 buffer_get_bignum(&m, sensitive_data.server_key->rsa->iqmp);
1093 buffer_get_bignum(&m, sensitive_data.server_key->rsa->p);
1094 buffer_get_bignum(&m, sensitive_data.server_key->rsa->q);
1095 if (rsa_generate_additional_parameters(
1096 sensitive_data.server_key->rsa) != 0)
1097 fatal("%s: rsa_generate_additional_parameters "
1100 fatal("ssh1 not supported");
1104 #if defined(WITH_OPENSSL) && !defined(OPENSSL_PRNG_ONLY)
1105 rexec_recv_rng_seed(&m);
1110 debug3("%s: done", __func__);
1113 /* Accept a connection from inetd */
1115 server_accept_inetd(int *sock_in, int *sock_out)
1121 close(REEXEC_CONFIG_PASS_FD);
1122 *sock_in = *sock_out = dup(STDIN_FILENO);
1124 startup_pipe = dup(REEXEC_STARTUP_PIPE_FD);
1125 close(REEXEC_STARTUP_PIPE_FD);
1128 *sock_in = dup(STDIN_FILENO);
1129 *sock_out = dup(STDOUT_FILENO);
1132 * We intentionally do not close the descriptors 0, 1, and 2
1133 * as our code for setting the descriptors won't work if
1134 * ttyfd happens to be one of those.
1136 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1137 dup2(fd, STDIN_FILENO);
1138 dup2(fd, STDOUT_FILENO);
1140 dup2(fd, STDERR_FILENO);
1141 if (fd > (log_stderr ? STDERR_FILENO : STDOUT_FILENO))
1144 debug("inetd sockets after dupping: %d, %d", *sock_in, *sock_out);
1148 * Listen for TCP connections
1153 int ret, listen_sock, on = 1;
1154 struct addrinfo *ai;
1155 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1157 for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
1158 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1160 if (num_listen_socks >= MAX_LISTEN_SOCKS)
1161 fatal("Too many listen sockets. "
1162 "Enlarge MAX_LISTEN_SOCKS");
1163 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
1164 ntop, sizeof(ntop), strport, sizeof(strport),
1165 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1166 error("getnameinfo failed: %.100s",
1167 ssh_gai_strerror(ret));
1170 /* Create socket for listening. */
1171 listen_sock = socket(ai->ai_family, ai->ai_socktype,
1173 if (listen_sock < 0) {
1174 /* kernel may not support ipv6 */
1175 verbose("socket: %.100s", strerror(errno));
1178 if (set_nonblock(listen_sock) == -1) {
1183 * Set socket options.
1184 * Allow local port reuse in TIME_WAIT.
1186 if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
1187 &on, sizeof(on)) == -1)
1188 error("setsockopt SO_REUSEADDR: %s", strerror(errno));
1190 /* Only communicate in IPv6 over AF_INET6 sockets. */
1191 if (ai->ai_family == AF_INET6)
1192 sock_set_v6only(listen_sock);
1194 debug("Bind to port %s on %s.", strport, ntop);
1196 /* Bind the socket to the desired port. */
1197 if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1198 error("Bind to port %s on %s failed: %.200s.",
1199 strport, ntop, strerror(errno));
1203 listen_socks[num_listen_socks] = listen_sock;
1206 /* Start listening on the port. */
1207 if (listen(listen_sock, SSH_LISTEN_BACKLOG) < 0)
1208 fatal("listen on [%s]:%s: %.100s",
1209 ntop, strport, strerror(errno));
1210 logit("Server listening on %s port %s.", ntop, strport);
1212 freeaddrinfo(options.listen_addrs);
1214 if (!num_listen_socks)
1215 fatal("Cannot bind any address.");
1219 * The main TCP accept loop. Note that, for the non-debug case, returns
1220 * from this function are in a forked subprocess.
1223 server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
1226 int i, j, ret, maxfd;
1227 int key_used = 0, startups = 0;
1228 int startup_p[2] = { -1 , -1 };
1229 struct sockaddr_storage from;
1234 /* setup fd set for accept */
1237 for (i = 0; i < num_listen_socks; i++)
1238 if (listen_socks[i] > maxfd)
1239 maxfd = listen_socks[i];
1240 /* pipes connected to unauthenticated childs */
1241 startup_pipes = xcalloc(options.max_startups, sizeof(int));
1242 for (i = 0; i < options.max_startups; i++)
1243 startup_pipes[i] = -1;
1246 * Stay listening for connections until the system crashes or
1247 * the daemon is killed with a signal.
1250 if (received_sighup)
1254 fdset = (fd_set *)xcalloc(howmany(maxfd + 1, NFDBITS),
1257 for (i = 0; i < num_listen_socks; i++)
1258 FD_SET(listen_socks[i], fdset);
1259 for (i = 0; i < options.max_startups; i++)
1260 if (startup_pipes[i] != -1)
1261 FD_SET(startup_pipes[i], fdset);
1263 /* Wait in select until there is a connection. */
1264 ret = select(maxfd+1, fdset, NULL, NULL, NULL);
1265 if (ret < 0 && errno != EINTR)
1266 error("select: %.100s", strerror(errno));
1267 if (received_sigterm) {
1268 logit("Received signal %d; terminating.",
1269 (int) received_sigterm);
1270 close_listen_socks();
1271 if (options.pid_file != NULL)
1272 unlink(options.pid_file);
1273 exit(received_sigterm == SIGTERM ? 0 : 255);
1275 if (key_used && key_do_regen) {
1276 generate_ephemeral_server_key();
1283 for (i = 0; i < options.max_startups; i++)
1284 if (startup_pipes[i] != -1 &&
1285 FD_ISSET(startup_pipes[i], fdset)) {
1287 * the read end of the pipe is ready
1288 * if the child has closed the pipe
1289 * after successful authentication
1290 * or if the child has died
1292 close(startup_pipes[i]);
1293 startup_pipes[i] = -1;
1296 for (i = 0; i < num_listen_socks; i++) {
1297 if (!FD_ISSET(listen_socks[i], fdset))
1299 fromlen = sizeof(from);
1300 *newsock = accept(listen_socks[i],
1301 (struct sockaddr *)&from, &fromlen);
1303 if (errno != EINTR && errno != EWOULDBLOCK &&
1304 errno != ECONNABORTED && errno != EAGAIN)
1305 error("accept: %.100s",
1307 if (errno == EMFILE || errno == ENFILE)
1311 if (unset_nonblock(*newsock) == -1) {
1315 if (drop_connection(startups) == 1) {
1316 debug("drop connection #%d", startups);
1320 if (pipe(startup_p) == -1) {
1325 if (rexec_flag && socketpair(AF_UNIX,
1326 SOCK_STREAM, 0, config_s) == -1) {
1327 error("reexec socketpair: %s",
1330 close(startup_p[0]);
1331 close(startup_p[1]);
1335 for (j = 0; j < options.max_startups; j++)
1336 if (startup_pipes[j] == -1) {
1337 startup_pipes[j] = startup_p[0];
1338 if (maxfd < startup_p[0])
1339 maxfd = startup_p[0];
1345 * Got connection. Fork a child to handle it, unless
1346 * we are in debugging mode.
1350 * In debugging mode. Close the listening
1351 * socket, and start processing the
1352 * connection without forking.
1354 debug("Server will not fork when running in debugging mode.");
1355 close_listen_socks();
1356 *sock_in = *newsock;
1357 *sock_out = *newsock;
1358 close(startup_p[0]);
1359 close(startup_p[1]);
1363 send_rexec_state(config_s[0],
1371 * Normal production daemon. Fork, and have
1372 * the child process the connection. The
1373 * parent continues listening.
1375 platform_pre_fork();
1376 if ((pid = fork()) == 0) {
1378 * Child. Close the listening and
1379 * max_startup sockets. Start using
1380 * the accepted socket. Reinitialize
1381 * logging (since our pid has changed).
1382 * We break out of the loop to handle
1385 platform_post_fork_child();
1386 startup_pipe = startup_p[1];
1387 close_startup_pipes();
1388 close_listen_socks();
1389 *sock_in = *newsock;
1390 *sock_out = *newsock;
1391 log_init(__progname,
1393 options.log_facility,
1400 /* Parent. Stay in the loop. */
1401 platform_post_fork_parent(pid);
1403 error("fork: %.100s", strerror(errno));
1405 debug("Forked child %ld.", (long)pid);
1407 close(startup_p[1]);
1410 send_rexec_state(config_s[0], &cfg);
1416 * Mark that the key has been used (it
1417 * was "given" to the child).
1419 if ((options.protocol & SSH_PROTO_1) &&
1421 /* Schedule server key regeneration alarm. */
1422 signal(SIGALRM, key_regeneration_alarm);
1423 alarm(options.key_regeneration_time);
1430 * Ensure that our random state differs
1431 * from that of the child
1434 arc4random_buf(rnd, sizeof(rnd));
1436 RAND_seed(rnd, sizeof(rnd));
1437 if ((RAND_bytes((u_char *)rnd, 1)) != 1)
1438 fatal("%s: RAND_bytes failed", __func__);
1440 explicit_bzero(rnd, sizeof(rnd));
1443 /* child process check (or debug mode) */
1444 if (num_listen_socks < 0)
1451 * Main program for the daemon.
1454 main(int ac, char **av)
1456 extern char *optarg;
1458 int r, opt, i, j, on = 1;
1459 int sock_in = -1, sock_out = -1, newsock = -1;
1460 const char *remote_ip;
1462 char *fp, *line, *logfile = NULL;
1463 int config_s[2] = { -1 , -1 };
1465 u_int64_t ibytes, obytes;
1471 struct connection_info *connection_info = get_connection_info(0, 0);
1473 #ifdef HAVE_SECUREWARE
1474 (void)set_auth_parameters(ac, av);
1476 __progname = ssh_get_progname(av[0]);
1478 /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
1481 saved_argv = xcalloc(ac + 1, sizeof(*saved_argv));
1482 for (i = 0; i < ac; i++)
1483 saved_argv[i] = xstrdup(av[i]);
1484 saved_argv[i] = NULL;
1486 #ifndef HAVE_SETPROCTITLE
1487 /* Prepare for later setproctitle emulation */
1488 compat_init_setproctitle(ac, av);
1492 if (geteuid() == 0 && setgroups(0, NULL) == -1)
1493 debug("setgroups(): %.200s", strerror(errno));
1495 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1498 /* Initialize configuration options to their default values. */
1499 initialize_server_options(&options);
1501 /* Parse command-line arguments. */
1502 while ((opt = getopt(ac, av, "f:p:b:k:h:g:u:o:C:dDeE:iqrtQRT46")) != -1) {
1505 options.address_family = AF_INET;
1508 options.address_family = AF_INET6;
1511 config_file_name = optarg;
1514 if (options.num_host_cert_files >= MAX_HOSTCERTS) {
1515 fprintf(stderr, "too many host certificates.\n");
1518 options.host_cert_files[options.num_host_cert_files++] =
1519 derelativise_path(optarg);
1522 if (debug_flag == 0) {
1524 options.log_level = SYSLOG_LEVEL_DEBUG1;
1525 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
1526 options.log_level++;
1532 logfile = xstrdup(optarg);
1551 options.log_level = SYSLOG_LEVEL_QUIET;
1554 options.server_key_bits = (int)strtonum(optarg, 256,
1558 options.ports_from_cmdline = 1;
1559 if (options.num_ports >= MAX_PORTS) {
1560 fprintf(stderr, "too many ports.\n");
1563 options.ports[options.num_ports++] = a2port(optarg);
1564 if (options.ports[options.num_ports-1] <= 0) {
1565 fprintf(stderr, "Bad port number.\n");
1570 if ((options.login_grace_time = convtime(optarg)) == -1) {
1571 fprintf(stderr, "Invalid login grace time.\n");
1576 if ((options.key_regeneration_time = convtime(optarg)) == -1) {
1577 fprintf(stderr, "Invalid key regeneration interval.\n");
1582 if (options.num_host_key_files >= MAX_HOSTKEYS) {
1583 fprintf(stderr, "too many host keys.\n");
1586 options.host_key_files[options.num_host_key_files++] =
1587 derelativise_path(optarg);
1596 if (parse_server_match_testspec(connection_info,
1601 utmp_len = (u_int)strtonum(optarg, 0, HOST_NAME_MAX+1+1, NULL);
1602 if (utmp_len > HOST_NAME_MAX+1) {
1603 fprintf(stderr, "Invalid utmp length.\n");
1608 line = xstrdup(optarg);
1609 if (process_server_config_line(&options, line,
1610 "command-line", 0, NULL, NULL) != 0)
1620 if (rexeced_flag || inetd_flag)
1622 if (!test_flag && (rexec_flag && (av[0] == NULL || *av[0] != '/')))
1623 fatal("sshd re-exec requires execution with an absolute path");
1625 closefrom(REEXEC_MIN_FREE_FD);
1627 closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
1630 OpenSSL_add_all_algorithms();
1633 /* If requested, redirect the logs to the specified logfile. */
1634 if (logfile != NULL) {
1635 log_redirect_stderr_to(logfile);
1639 * Force logging to stderr until we have loaded the private host
1640 * key (unless started from inetd)
1642 log_init(__progname,
1643 options.log_level == SYSLOG_LEVEL_NOT_SET ?
1644 SYSLOG_LEVEL_INFO : options.log_level,
1645 options.log_facility == SYSLOG_FACILITY_NOT_SET ?
1646 SYSLOG_FACILITY_AUTH : options.log_facility,
1647 log_stderr || !inetd_flag);
1650 * Unset KRB5CCNAME, otherwise the user's session may inherit it from
1651 * root's environment
1653 if (getenv("KRB5CCNAME") != NULL)
1654 (void) unsetenv("KRB5CCNAME");
1657 /* Cray can define user privs drop all privs now!
1658 * Not needed on PRIV_SU systems!
1663 sensitive_data.server_key = NULL;
1664 sensitive_data.ssh1_host_key = NULL;
1665 sensitive_data.have_ssh1_key = 0;
1666 sensitive_data.have_ssh2_key = 0;
1669 * If we're doing an extended config test, make sure we have all of
1670 * the parameters we need. If we're not doing an extended test,
1671 * do not silently ignore connection test params.
1673 if (test_flag >= 2 && server_match_spec_complete(connection_info) == 0)
1674 fatal("user, host and addr are all required when testing "
1676 if (test_flag < 2 && server_match_spec_complete(connection_info) >= 0)
1677 fatal("Config test connection parameter (-C) provided without "
1680 /* Fetch our configuration */
1683 recv_rexec_state(REEXEC_CONFIG_PASS_FD, &cfg);
1685 load_server_config(config_file_name, &cfg);
1687 parse_server_config(&options, rexeced_flag ? "rexec" : config_file_name,
1692 /* Fill in default values for those options not explicitly set. */
1693 fill_default_server_options(&options);
1695 /* challenge-response is implemented via keyboard interactive */
1696 if (options.challenge_response_authentication)
1697 options.kbd_interactive_authentication = 1;
1699 /* Check that options are sensible */
1700 if (options.authorized_keys_command_user == NULL &&
1701 (options.authorized_keys_command != NULL &&
1702 strcasecmp(options.authorized_keys_command, "none") != 0))
1703 fatal("AuthorizedKeysCommand set without "
1704 "AuthorizedKeysCommandUser");
1707 * Check whether there is any path through configured auth methods.
1708 * Unfortunately it is not possible to verify this generally before
1709 * daemonisation in the presence of Match block, but this catches
1710 * and warns for trivial misconfigurations that could break login.
1712 if (options.num_auth_methods != 0) {
1713 if ((options.protocol & SSH_PROTO_1))
1714 fatal("AuthenticationMethods is not supported with "
1716 for (n = 0; n < options.num_auth_methods; n++) {
1717 if (auth2_methods_valid(options.auth_methods[n],
1721 if (n >= options.num_auth_methods)
1722 fatal("AuthenticationMethods cannot be satisfied by "
1723 "enabled authentication methods");
1726 /* set default channel AF */
1727 channel_set_af(options.address_family);
1729 /* Check that there are no remaining arguments. */
1731 fprintf(stderr, "Extra argument %s.\n", av[optind]);
1735 debug("sshd version %s, %s", SSH_VERSION,
1737 SSLeay_version(SSLEAY_VERSION)
1743 /* Store privilege separation user for later use if required. */
1744 if ((privsep_pw = getpwnam(SSH_PRIVSEP_USER)) == NULL) {
1745 if (use_privsep || options.kerberos_authentication)
1746 fatal("Privilege separation user %s does not exist",
1749 if (privsep_pw->pw_passwd != NULL) {
1750 explicit_bzero(privsep_pw->pw_passwd,
1751 strlen(privsep_pw->pw_passwd));
1753 privsep_pw = pwcopy(privsep_pw);
1754 if (privsep_pw->pw_passwd != NULL) {
1755 free(privsep_pw->pw_passwd);
1757 privsep_pw->pw_passwd = xstrdup("*");
1759 #if !defined(ANDROID)
1763 /* load host keys */
1764 sensitive_data.host_keys = xcalloc(options.num_host_key_files,
1766 sensitive_data.host_pubkeys = xcalloc(options.num_host_key_files,
1769 if (options.host_key_agent) {
1770 if (strcmp(options.host_key_agent, SSH_AUTHSOCKET_ENV_NAME))
1771 setenv(SSH_AUTHSOCKET_ENV_NAME,
1772 options.host_key_agent, 1);
1773 if ((r = ssh_get_authentication_socket(NULL)) == 0)
1776 error("Could not connect to agent \"%s\": %s",
1777 options.host_key_agent, ssh_err(r));
1780 for (i = 0; i < options.num_host_key_files; i++) {
1781 if (options.host_key_files[i] == NULL)
1783 key = key_load_private(options.host_key_files[i], "", NULL);
1784 pubkey = key_load_public(options.host_key_files[i], NULL);
1785 if (pubkey == NULL && key != NULL)
1786 pubkey = key_demote(key);
1787 sensitive_data.host_keys[i] = key;
1788 sensitive_data.host_pubkeys[i] = pubkey;
1790 if (key == NULL && pubkey != NULL && pubkey->type != KEY_RSA1 &&
1792 debug("will rely on agent for hostkey %s",
1793 options.host_key_files[i]);
1794 keytype = pubkey->type;
1795 } else if (key != NULL) {
1796 keytype = key->type;
1798 error("Could not load host key: %s",
1799 options.host_key_files[i]);
1800 sensitive_data.host_keys[i] = NULL;
1801 sensitive_data.host_pubkeys[i] = NULL;
1807 sensitive_data.ssh1_host_key = key;
1808 sensitive_data.have_ssh1_key = 1;
1814 if (have_agent || key != NULL)
1815 sensitive_data.have_ssh2_key = 1;
1818 if ((fp = sshkey_fingerprint(pubkey, options.fingerprint_hash,
1819 SSH_FP_DEFAULT)) == NULL)
1820 fatal("sshkey_fingerprint failed");
1821 debug("%s host key #%d: %s %s",
1822 key ? "private" : "agent", i, keytype == KEY_RSA1 ?
1823 sshkey_type(pubkey) : sshkey_ssh_name(pubkey), fp);
1826 if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
1827 logit("Disabling protocol version 1. Could not load host key");
1828 options.protocol &= ~SSH_PROTO_1;
1830 if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
1831 logit("Disabling protocol version 2. Could not load host key");
1832 options.protocol &= ~SSH_PROTO_2;
1834 if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
1835 logit("sshd: no hostkeys available -- exiting.");
1840 * Load certificates. They are stored in an array at identical
1841 * indices to the public keys that they relate to.
1843 sensitive_data.host_certificates = xcalloc(options.num_host_key_files,
1845 for (i = 0; i < options.num_host_key_files; i++)
1846 sensitive_data.host_certificates[i] = NULL;
1848 for (i = 0; i < options.num_host_cert_files; i++) {
1849 if (options.host_cert_files[i] == NULL)
1851 key = key_load_public(options.host_cert_files[i], NULL);
1853 error("Could not load host certificate: %s",
1854 options.host_cert_files[i]);
1857 if (!key_is_cert(key)) {
1858 error("Certificate file is not a certificate: %s",
1859 options.host_cert_files[i]);
1863 /* Find matching private key */
1864 for (j = 0; j < options.num_host_key_files; j++) {
1865 if (key_equal_public(key,
1866 sensitive_data.host_keys[j])) {
1867 sensitive_data.host_certificates[j] = key;
1871 if (j >= options.num_host_key_files) {
1872 error("No matching private key for certificate: %s",
1873 options.host_cert_files[i]);
1877 sensitive_data.host_certificates[j] = key;
1878 debug("host certificate: #%d type %d %s", j, key->type,
1883 /* Check certain values for sanity. */
1884 if (options.protocol & SSH_PROTO_1) {
1885 if (options.server_key_bits < 512 ||
1886 options.server_key_bits > 32768) {
1887 fprintf(stderr, "Bad server key size.\n");
1891 * Check that server and host key lengths differ sufficiently. This
1892 * is necessary to make double encryption work with rsaref. Oh, I
1893 * hate software patents. I dont know if this can go? Niels
1895 if (options.server_key_bits >
1896 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) -
1897 SSH_KEY_BITS_RESERVED && options.server_key_bits <
1898 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1899 SSH_KEY_BITS_RESERVED) {
1900 options.server_key_bits =
1901 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1902 SSH_KEY_BITS_RESERVED;
1903 debug("Forcing server key to %d bits to make it differ from host key.",
1904 options.server_key_bits);
1912 if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
1913 (S_ISDIR(st.st_mode) == 0))
1914 fatal("Missing privilege separation directory: %s",
1915 _PATH_PRIVSEP_CHROOT_DIR);
1918 if (check_ntsec(_PATH_PRIVSEP_CHROOT_DIR) &&
1919 (st.st_uid != getuid () ||
1920 (st.st_mode & (S_IWGRP|S_IWOTH)) != 0))
1922 if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
1924 fatal("%s must be owned by root and not group or "
1925 "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
1928 if (test_flag > 1) {
1929 if (server_match_spec_complete(connection_info) == 1)
1930 parse_server_match_config(&options, connection_info);
1931 dump_config(&options);
1934 /* Configuration looks good, so exit if in test mode. */
1939 * Clear out any supplemental groups we may have inherited. This
1940 * prevents inadvertent creation of files with bad modes (in the
1941 * portable version at least, it's certainly possible for PAM
1942 * to create a file, and we can't control the code in every
1943 * module which might be used).
1945 if (setgroups(0, NULL) < 0)
1946 debug("setgroups() failed: %.200s", strerror(errno));
1949 rexec_argv = xcalloc(rexec_argc + 2, sizeof(char *));
1950 for (i = 0; i < rexec_argc; i++) {
1951 debug("rexec_argv[%d]='%s'", i, saved_argv[i]);
1952 rexec_argv[i] = saved_argv[i];
1954 rexec_argv[rexec_argc] = "-R";
1955 rexec_argv[rexec_argc + 1] = NULL;
1958 /* Ensure that umask disallows at least group and world write */
1959 new_umask = umask(0077) | 0022;
1960 (void) umask(new_umask);
1962 /* Initialize the log (it is reinitialized below in case we forked). */
1963 if (debug_flag && (!inetd_flag || rexeced_flag))
1965 log_init(__progname, options.log_level, options.log_facility, log_stderr);
1968 * If not in debugging mode, and not started from inetd, disconnect
1969 * from the controlling terminal, and fork. The original process
1972 if (!(debug_flag || inetd_flag || no_daemon_flag)) {
1975 #endif /* TIOCNOTTY */
1976 if (daemon(0, 0) < 0)
1977 fatal("daemon() failed: %.200s", strerror(errno));
1979 /* Disconnect from the controlling tty. */
1981 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
1983 (void) ioctl(fd, TIOCNOTTY, NULL);
1986 #endif /* TIOCNOTTY */
1988 /* Reinitialize the log (because of the fork above). */
1989 log_init(__progname, options.log_level, options.log_facility, log_stderr);
1991 /* Chdir to the root directory so that the current disk can be
1992 unmounted if desired. */
1993 if (chdir("/") == -1)
1994 error("chdir(\"/\"): %s", strerror(errno));
1996 /* ignore SIGPIPE */
1997 signal(SIGPIPE, SIG_IGN);
1999 /* Get a connection, either from inetd or a listening TCP socket */
2001 server_accept_inetd(&sock_in, &sock_out);
2003 platform_pre_listen();
2006 if (options.protocol & SSH_PROTO_1)
2007 generate_ephemeral_server_key();
2009 signal(SIGHUP, sighup_handler);
2010 signal(SIGCHLD, main_sigchld_handler);
2011 signal(SIGTERM, sigterm_handler);
2012 signal(SIGQUIT, sigterm_handler);
2015 * Write out the pid file after the sigterm handler
2016 * is setup and the listen sockets are bound
2018 if (options.pid_file != NULL && !debug_flag) {
2019 FILE *f = fopen(options.pid_file, "w");
2022 error("Couldn't create pid file \"%s\": %s",
2023 options.pid_file, strerror(errno));
2025 fprintf(f, "%ld\n", (long) getpid());
2030 /* Accept a connection and return in a forked child */
2031 server_accept_loop(&sock_in, &sock_out,
2032 &newsock, config_s);
2035 /* This is the child processing a new connection. */
2036 setproctitle("%s", "[accepted]");
2039 * Create a new session and process group since the 4.4BSD
2040 * setlogin() affects the entire process group. We don't
2041 * want the child to be able to affect the parent.
2043 #if !defined(SSHD_ACQUIRES_CTTY)
2045 * If setsid is called, on some platforms sshd will later acquire a
2046 * controlling terminal which will result in "could not set
2047 * controlling tty" errors.
2049 if (!debug_flag && !inetd_flag && setsid() < 0)
2050 error("setsid: %.100s", strerror(errno));
2056 debug("rexec start in %d out %d newsock %d pipe %d sock %d",
2057 sock_in, sock_out, newsock, startup_pipe, config_s[0]);
2058 dup2(newsock, STDIN_FILENO);
2059 dup2(STDIN_FILENO, STDOUT_FILENO);
2060 if (startup_pipe == -1)
2061 close(REEXEC_STARTUP_PIPE_FD);
2062 else if (startup_pipe != REEXEC_STARTUP_PIPE_FD) {
2063 dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD);
2064 close(startup_pipe);
2065 startup_pipe = REEXEC_STARTUP_PIPE_FD;
2068 dup2(config_s[1], REEXEC_CONFIG_PASS_FD);
2071 execv(rexec_argv[0], rexec_argv);
2073 /* Reexec has failed, fall back and continue */
2074 error("rexec of %s failed: %s", rexec_argv[0], strerror(errno));
2075 recv_rexec_state(REEXEC_CONFIG_PASS_FD, NULL);
2076 log_init(__progname, options.log_level,
2077 options.log_facility, log_stderr);
2080 close(REEXEC_CONFIG_PASS_FD);
2081 newsock = sock_out = sock_in = dup(STDIN_FILENO);
2082 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
2083 dup2(fd, STDIN_FILENO);
2084 dup2(fd, STDOUT_FILENO);
2085 if (fd > STDERR_FILENO)
2088 debug("rexec cleanup in %d out %d newsock %d pipe %d sock %d",
2089 sock_in, sock_out, newsock, startup_pipe, config_s[0]);
2092 /* Executed child processes don't need these. */
2093 fcntl(sock_out, F_SETFD, FD_CLOEXEC);
2094 fcntl(sock_in, F_SETFD, FD_CLOEXEC);
2097 * Disable the key regeneration alarm. We will not regenerate the
2098 * key since we are no longer in a position to give it to anyone. We
2099 * will not restart on SIGHUP since it no longer makes sense.
2102 signal(SIGALRM, SIG_DFL);
2103 signal(SIGHUP, SIG_DFL);
2104 signal(SIGTERM, SIG_DFL);
2105 signal(SIGQUIT, SIG_DFL);
2106 signal(SIGCHLD, SIG_DFL);
2107 signal(SIGINT, SIG_DFL);
2110 * Register our connection. This turns encryption off because we do
2113 packet_set_connection(sock_in, sock_out);
2114 packet_set_server();
2116 /* Set SO_KEEPALIVE if requested. */
2117 if (options.tcp_keep_alive && packet_connection_is_on_socket() &&
2118 setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0)
2119 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
2121 if ((remote_port = get_remote_port()) < 0) {
2122 debug("get_remote_port failed");
2127 * We use get_canonical_hostname with usedns = 0 instead of
2128 * get_remote_ipaddr here so IP options will be checked.
2130 (void) get_canonical_hostname(0);
2132 * The rest of the code depends on the fact that
2133 * get_remote_ipaddr() caches the remote ip, even if
2134 * the socket goes away.
2136 remote_ip = get_remote_ipaddr();
2138 #ifdef SSH_AUDIT_EVENTS
2139 audit_connection_from(remote_ip, remote_port);
2142 /* Log the connection. */
2143 verbose("Connection from %s port %d on %s port %d",
2144 remote_ip, remote_port,
2145 get_local_ipaddr(sock_in), get_local_port());
2148 * We don't want to listen forever unless the other side
2149 * successfully authenticates itself. So we set up an alarm which is
2150 * cleared after successful authentication. A limit of zero
2151 * indicates no limit. Note that we don't set the alarm in debugging
2152 * mode; it is just annoying to have the server exit just when you
2153 * are about to discover the bug.
2155 signal(SIGALRM, grace_alarm_handler);
2157 alarm(options.login_grace_time);
2159 sshd_exchange_identification(sock_in, sock_out);
2161 /* In inetd mode, generate ephemeral key only for proto 1 connections */
2162 if (!compat20 && inetd_flag && sensitive_data.server_key == NULL)
2163 generate_ephemeral_server_key();
2165 packet_set_nonblocking();
2167 /* allocate authentication context */
2168 authctxt = xcalloc(1, sizeof(*authctxt));
2170 authctxt->loginmsg = &loginmsg;
2172 /* XXX global for cleanup, access from other modules */
2173 the_authctxt = authctxt;
2175 /* prepare buffer to collect messages to display to user after login */
2176 buffer_init(&loginmsg);
2180 if (privsep_preauth(authctxt) == 1)
2182 } else if (compat20 && have_agent) {
2183 if ((r = ssh_get_authentication_socket(&auth_sock)) != 0) {
2184 error("Unable to get agent socket: %s", ssh_err(r));
2189 /* perform the key exchange */
2190 /* authenticate user and start session */
2193 do_authentication2(authctxt);
2197 do_authentication(authctxt);
2199 fatal("ssh1 not supported");
2203 * If we use privilege separation, the unprivileged child transfers
2204 * the current keystate and exits
2207 mm_send_keystate(pmonitor);
2213 * Cancel the alarm we set to limit the time taken for
2217 signal(SIGALRM, SIG_DFL);
2218 authctxt->authenticated = 1;
2219 if (startup_pipe != -1) {
2220 close(startup_pipe);
2224 #ifdef SSH_AUDIT_EVENTS
2225 audit_event(SSH_AUTH_SUCCESS);
2229 if (options.gss_authentication) {
2230 temporarily_use_uid(authctxt->pw);
2231 ssh_gssapi_storecreds();
2236 if (options.use_pam) {
2243 * In privilege separation, we fork another child and prepare
2244 * file descriptor passing.
2247 privsep_postauth(authctxt);
2248 /* the monitor process [priv] will not return */
2250 destroy_sensitive_data();
2253 packet_set_timeout(options.client_alive_interval,
2254 options.client_alive_count_max);
2256 /* Try to send all our hostkeys to the client */
2258 notify_hostkeys(active_state);
2260 /* Start session. */
2261 do_authenticated(authctxt);
2263 /* The connection has been terminated. */
2264 packet_get_bytes(&ibytes, &obytes);
2265 verbose("Transferred: sent %llu, received %llu bytes",
2266 (unsigned long long)obytes, (unsigned long long)ibytes);
2268 verbose("Closing connection to %.500s port %d", remote_ip, remote_port);
2271 if (options.use_pam)
2273 #endif /* USE_PAM */
2275 #ifdef SSH_AUDIT_EVENTS
2276 PRIVSEP(audit_event(SSH_CONNECTION_CLOSE));
2289 * Decrypt session_key_int using our private server key and private host key
2290 * (key with larger modulus first).
2293 ssh1_session_key(BIGNUM *session_key_int)
2297 if (BN_cmp(sensitive_data.server_key->rsa->n,
2298 sensitive_data.ssh1_host_key->rsa->n) > 0) {
2299 /* Server key has bigger modulus. */
2300 if (BN_num_bits(sensitive_data.server_key->rsa->n) <
2301 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
2302 SSH_KEY_BITS_RESERVED) {
2303 fatal("do_connection: %s: "
2304 "server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
2305 get_remote_ipaddr(),
2306 BN_num_bits(sensitive_data.server_key->rsa->n),
2307 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
2308 SSH_KEY_BITS_RESERVED);
2310 if (rsa_private_decrypt(session_key_int, session_key_int,
2311 sensitive_data.server_key->rsa) != 0)
2313 if (rsa_private_decrypt(session_key_int, session_key_int,
2314 sensitive_data.ssh1_host_key->rsa) != 0)
2317 /* Host key has bigger modulus (or they are equal). */
2318 if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
2319 BN_num_bits(sensitive_data.server_key->rsa->n) +
2320 SSH_KEY_BITS_RESERVED) {
2321 fatal("do_connection: %s: "
2322 "host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
2323 get_remote_ipaddr(),
2324 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
2325 BN_num_bits(sensitive_data.server_key->rsa->n),
2326 SSH_KEY_BITS_RESERVED);
2328 if (rsa_private_decrypt(session_key_int, session_key_int,
2329 sensitive_data.ssh1_host_key->rsa) != 0)
2331 if (rsa_private_decrypt(session_key_int, session_key_int,
2332 sensitive_data.server_key->rsa) != 0)
2346 BIGNUM *session_key_int, *fake_key_int, *real_key_int;
2347 u_char session_key[SSH_SESSION_KEY_LENGTH];
2348 u_char fake_key_bytes[4096 / 8];
2349 size_t fake_key_len;
2351 u_int cipher_type, auth_mask, protocol_flags;
2354 * Generate check bytes that the client must send back in the user
2355 * packet in order for it to be accepted; this is used to defy ip
2356 * spoofing attacks. Note that this only works against somebody
2357 * doing IP spoofing from a remote machine; any machine on the local
2358 * network can still see outgoing packets and catch the random
2359 * cookie. This only affects rhosts authentication, and this is one
2360 * of the reasons why it is inherently insecure.
2362 arc4random_buf(cookie, sizeof(cookie));
2365 * Send our public key. We include in the packet 64 bits of random
2366 * data that must be matched in the reply in order to prevent IP
2369 packet_start(SSH_SMSG_PUBLIC_KEY);
2370 for (i = 0; i < 8; i++)
2371 packet_put_char(cookie[i]);
2373 /* Store our public server RSA key. */
2374 packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
2375 packet_put_bignum(sensitive_data.server_key->rsa->e);
2376 packet_put_bignum(sensitive_data.server_key->rsa->n);
2378 /* Store our public host RSA key. */
2379 packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
2380 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
2381 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
2383 /* Put protocol flags. */
2384 packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
2386 /* Declare which ciphers we support. */
2387 packet_put_int(cipher_mask_ssh1(0));
2389 /* Declare supported authentication types. */
2391 if (options.rhosts_rsa_authentication)
2392 auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
2393 if (options.rsa_authentication)
2394 auth_mask |= 1 << SSH_AUTH_RSA;
2395 if (options.challenge_response_authentication == 1)
2396 auth_mask |= 1 << SSH_AUTH_TIS;
2397 if (options.password_authentication)
2398 auth_mask |= 1 << SSH_AUTH_PASSWORD;
2399 packet_put_int(auth_mask);
2401 /* Send the packet and wait for it to be sent. */
2403 packet_write_wait();
2405 debug("Sent %d bit server key and %d bit host key.",
2406 BN_num_bits(sensitive_data.server_key->rsa->n),
2407 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
2409 /* Read clients reply (cipher type and session key). */
2410 packet_read_expect(SSH_CMSG_SESSION_KEY);
2412 /* Get cipher type and check whether we accept this. */
2413 cipher_type = packet_get_char();
2415 if (!(cipher_mask_ssh1(0) & (1 << cipher_type)))
2416 packet_disconnect("Warning: client selects unsupported cipher.");
2418 /* Get check bytes from the packet. These must match those we
2419 sent earlier with the public key packet. */
2420 for (i = 0; i < 8; i++)
2421 if (cookie[i] != packet_get_char())
2422 packet_disconnect("IP Spoofing check bytes do not match.");
2424 debug("Encryption type: %.200s", cipher_name(cipher_type));
2426 /* Get the encrypted integer. */
2427 if ((real_key_int = BN_new()) == NULL)
2428 fatal("do_ssh1_kex: BN_new failed");
2429 packet_get_bignum(real_key_int);
2431 protocol_flags = packet_get_int();
2432 packet_set_protocol_flags(protocol_flags);
2435 /* Setup a fake key in case RSA decryption fails */
2436 if ((fake_key_int = BN_new()) == NULL)
2437 fatal("do_ssh1_kex: BN_new failed");
2438 fake_key_len = BN_num_bytes(real_key_int);
2439 if (fake_key_len > sizeof(fake_key_bytes))
2440 fake_key_len = sizeof(fake_key_bytes);
2441 arc4random_buf(fake_key_bytes, fake_key_len);
2442 if (BN_bin2bn(fake_key_bytes, fake_key_len, fake_key_int) == NULL)
2443 fatal("do_ssh1_kex: BN_bin2bn failed");
2445 /* Decrypt real_key_int using host/server keys */
2446 rsafail = PRIVSEP(ssh1_session_key(real_key_int));
2447 /* If decryption failed, use the fake key. Else, the real key. */
2449 session_key_int = fake_key_int;
2451 session_key_int = real_key_int;
2454 * Extract session key from the decrypted integer. The key is in the
2455 * least significant 256 bits of the integer; the first byte of the
2456 * key is in the highest bits.
2458 (void) BN_mask_bits(session_key_int, sizeof(session_key) * 8);
2459 len = BN_num_bytes(session_key_int);
2460 if (len < 0 || (u_int)len > sizeof(session_key)) {
2461 error("do_ssh1_kex: bad session key len from %s: "
2462 "session_key_int %d > sizeof(session_key) %lu",
2463 get_remote_ipaddr(), len, (u_long)sizeof(session_key));
2466 explicit_bzero(session_key, sizeof(session_key));
2467 BN_bn2bin(session_key_int,
2468 session_key + sizeof(session_key) - len);
2470 derive_ssh1_session_id(
2471 sensitive_data.ssh1_host_key->rsa->n,
2472 sensitive_data.server_key->rsa->n,
2473 cookie, session_id);
2475 * Xor the first 16 bytes of the session key with the
2478 for (i = 0; i < 16; i++)
2479 session_key[i] ^= session_id[i];
2482 /* Destroy the private and public keys. No longer. */
2483 destroy_sensitive_data();
2486 mm_ssh1_session_id(session_id);
2488 /* Destroy the decrypted integer. It is no longer needed. */
2489 BN_clear_free(real_key_int);
2490 BN_clear_free(fake_key_int);
2492 /* Set the session key. From this on all communications will be encrypted. */
2493 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
2495 /* Destroy our copy of the session key. It is no longer needed. */
2496 explicit_bzero(session_key, sizeof(session_key));
2498 debug("Received session key; encryption turned on.");
2500 /* Send an acknowledgment packet. Note that this packet is sent encrypted. */
2501 packet_start(SSH_SMSG_SUCCESS);
2503 packet_write_wait();
2508 sshd_hostkey_sign(Key *privkey, Key *pubkey, u_char **signature, size_t *slen,
2509 const u_char *data, size_t dlen, u_int flag)
2512 u_int xxx_slen, xxx_dlen = dlen;
2515 if (PRIVSEP(key_sign(privkey, signature, &xxx_slen, data, xxx_dlen) < 0))
2516 fatal("%s: key_sign failed", __func__);
2519 } else if (use_privsep) {
2520 if (mm_key_sign(pubkey, signature, &xxx_slen, data, xxx_dlen) < 0)
2521 fatal("%s: pubkey_sign failed", __func__);
2525 if ((r = ssh_agent_sign(auth_sock, pubkey, signature, slen,
2526 data, dlen, datafellows)) != 0)
2527 fatal("%s: ssh_agent_sign failed: %s",
2528 __func__, ssh_err(r));
2534 * SSH2 key exchange: diffie-hellman-group1-sha1
2539 char *myproposal[PROPOSAL_MAX] = { KEX_SERVER };
2543 if (options.ciphers != NULL) {
2544 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
2545 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
2547 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
2548 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
2549 myproposal[PROPOSAL_ENC_ALGS_STOC] =
2550 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
2552 if (options.macs != NULL) {
2553 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
2554 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
2556 if (options.compression == COMP_NONE) {
2557 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
2558 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
2559 } else if (options.compression == COMP_DELAYED) {
2560 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
2561 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com";
2563 if (options.kex_algorithms != NULL)
2564 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
2566 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
2567 myproposal[PROPOSAL_KEX_ALGS]);
2569 if (options.rekey_limit || options.rekey_interval)
2570 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
2571 (time_t)options.rekey_interval);
2573 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal(
2574 list_hostkey_types());
2576 /* start key exchange */
2577 if ((r = kex_setup(active_state, myproposal)) != 0)
2578 fatal("kex_setup: %s", ssh_err(r));
2579 kex = active_state->kex;
2581 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
2582 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
2583 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
2584 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
2585 # ifdef OPENSSL_HAS_ECC
2586 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
2589 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
2591 kex->client_version_string=client_version_string;
2592 kex->server_version_string=server_version_string;
2593 kex->load_host_public_key=&get_hostkey_public_by_type;
2594 kex->load_host_private_key=&get_hostkey_private_by_type;
2595 kex->host_key_index=&get_hostkey_index;
2596 kex->sign = sshd_hostkey_sign;
2598 dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
2600 session_id2 = kex->session_id;
2601 session_id2_len = kex->session_id_len;
2604 /* send 1st encrypted/maced/compressed message */
2605 packet_start(SSH2_MSG_IGNORE);
2606 packet_put_cstring("markus");
2608 packet_write_wait();
2613 /* server specific fatal cleanup */
2618 do_cleanup(the_authctxt);
2619 if (use_privsep && privsep_is_preauth &&
2620 pmonitor != NULL && pmonitor->m_pid > 1) {
2621 debug("Killing privsep child %d", pmonitor->m_pid);
2622 if (kill(pmonitor->m_pid, SIGKILL) != 0 &&
2624 error("%s: kill(%d): %s", __func__,
2625 pmonitor->m_pid, strerror(errno));
2628 #ifdef SSH_AUDIT_EVENTS
2629 /* done after do_cleanup so it can cancel the PAM auth 'thread' */
2630 if (!use_privsep || mm_is_monitor())
2631 audit_event(SSH_CONNECTION_ABANDON);