xref: /onnv-gate/usr/src/cmd/ssh/sshd/sshd.c (revision 12597:5fdb1d206be4)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
30Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
40Sstevel@tonic-gate  *                    All rights reserved
50Sstevel@tonic-gate  * This program is the ssh daemon.  It listens for connections from clients,
60Sstevel@tonic-gate  * and performs authentication, executes use commands or shell, and forwards
70Sstevel@tonic-gate  * information to/from the application to the user client over an encrypted
80Sstevel@tonic-gate  * connection.  This can also handle forwarding of X11, TCP/IP, and
90Sstevel@tonic-gate  * authentication agent connections.
100Sstevel@tonic-gate  *
110Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
120Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
130Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
140Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
150Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
160Sstevel@tonic-gate  *
170Sstevel@tonic-gate  * SSH2 implementation:
180Sstevel@tonic-gate  * Privilege Separation:
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * Copyright (c) 2000, 2001, 2002 Markus Friedl.  All rights reserved.
210Sstevel@tonic-gate  * Copyright (c) 2002 Niels Provos.  All rights reserved.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
330Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
340Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
350Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
360Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
370Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
380Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
390Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
400Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
410Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate /*
44*12597SJan.Pechanec@Sun.COM  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include "includes.h"
480Sstevel@tonic-gate RCSID("$OpenBSD: sshd.c,v 1.260 2002/09/27 10:42:09 mickey Exp $");
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <openssl/dh.h>
510Sstevel@tonic-gate #include <openssl/bn.h>
520Sstevel@tonic-gate #include <openssl/md5.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include <openssl/rand.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include "ssh.h"
570Sstevel@tonic-gate #include "ssh1.h"
580Sstevel@tonic-gate #include "ssh2.h"
590Sstevel@tonic-gate #include "xmalloc.h"
600Sstevel@tonic-gate #include "rsa.h"
610Sstevel@tonic-gate #include "sshpty.h"
620Sstevel@tonic-gate #include "packet.h"
630Sstevel@tonic-gate #include "mpaux.h"
640Sstevel@tonic-gate #include "log.h"
650Sstevel@tonic-gate #include "servconf.h"
660Sstevel@tonic-gate #include "uidswap.h"
670Sstevel@tonic-gate #include "compat.h"
680Sstevel@tonic-gate #include "buffer.h"
690Sstevel@tonic-gate #include "cipher.h"
700Sstevel@tonic-gate #include "kex.h"
710Sstevel@tonic-gate #include "key.h"
720Sstevel@tonic-gate #include "dh.h"
730Sstevel@tonic-gate #include "myproposal.h"
740Sstevel@tonic-gate #include "authfile.h"
750Sstevel@tonic-gate #include "pathnames.h"
760Sstevel@tonic-gate #include "atomicio.h"
770Sstevel@tonic-gate #include "canohost.h"
780Sstevel@tonic-gate #include "auth.h"
790Sstevel@tonic-gate #include "misc.h"
800Sstevel@tonic-gate #include "dispatch.h"
810Sstevel@tonic-gate #include "channels.h"
820Sstevel@tonic-gate #include "session.h"
830Sstevel@tonic-gate #include "g11n.h"
840Sstevel@tonic-gate #include "sshlogin.h"
850Sstevel@tonic-gate #include "xlist.h"
867574SJan.Pechanec@Sun.COM #include "engine.h"
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #ifdef HAVE_BSM
890Sstevel@tonic-gate #include "bsmaudit.h"
900Sstevel@tonic-gate #endif /* HAVE_BSM */
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #ifdef ALTPRIVSEP
930Sstevel@tonic-gate #include "altprivsep.h"
940Sstevel@tonic-gate #endif /* ALTPRIVSEP */
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #ifdef HAVE_SOLARIS_CONTRACTS
970Sstevel@tonic-gate #include <sys/ctfs.h>
980Sstevel@tonic-gate #include <sys/contract.h>
990Sstevel@tonic-gate #include <sys/contract/process.h>
1000Sstevel@tonic-gate #include <libcontract.h>
1010Sstevel@tonic-gate #endif /* HAVE_SOLARIS_CONTRACTS */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #ifdef GSSAPI
1040Sstevel@tonic-gate #include "ssh-gss.h"
1050Sstevel@tonic-gate #endif /* GSSAPI */
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate #ifdef LIBWRAP
1080Sstevel@tonic-gate #include <tcpd.h>
1090Sstevel@tonic-gate #include <syslog.h>
1100Sstevel@tonic-gate #ifndef lint
1110Sstevel@tonic-gate int allow_severity = LOG_INFO;
1120Sstevel@tonic-gate int deny_severity = LOG_WARNING;
1130Sstevel@tonic-gate #endif /* lint */
1140Sstevel@tonic-gate #endif /* LIBWRAP */
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate #ifndef O_NOCTTY
1170Sstevel@tonic-gate #define O_NOCTTY	0
1180Sstevel@tonic-gate #endif
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate #ifdef HAVE___PROGNAME
1210Sstevel@tonic-gate extern char *__progname;
1220Sstevel@tonic-gate #else
1230Sstevel@tonic-gate char *__progname;
1240Sstevel@tonic-gate #endif
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /* Server configuration options. */
1270Sstevel@tonic-gate ServerOptions options;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /* Name of the server configuration file. */
1300Sstevel@tonic-gate static char *config_file_name = _PATH_SERVER_CONFIG_FILE;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate  * Flag indicating whether IPv4 or IPv6.  This can be set on the command line.
1340Sstevel@tonic-gate  * Default value is AF_UNSPEC means both IPv4 and IPv6.
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate #ifdef IPV4_DEFAULT
1370Sstevel@tonic-gate int IPv4or6 = AF_INET;
1380Sstevel@tonic-gate #else
1390Sstevel@tonic-gate int IPv4or6 = AF_UNSPEC;
1400Sstevel@tonic-gate #endif
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate  * Debug mode flag.  This can be set on the command line.  If debug
1440Sstevel@tonic-gate  * mode is enabled, extra debugging output will be sent to the system
1450Sstevel@tonic-gate  * log, the daemon will not go to background, and will exit after processing
1460Sstevel@tonic-gate  * the first connection.
1470Sstevel@tonic-gate  */
1480Sstevel@tonic-gate int debug_flag = 0;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /* Flag indicating that the daemon should only test the configuration and keys. */
1510Sstevel@tonic-gate static int test_flag = 0;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /* Flag indicating that the daemon is being started from inetd. */
1540Sstevel@tonic-gate static int inetd_flag = 0;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate /* Flag indicating that sshd should not detach and become a daemon. */
1570Sstevel@tonic-gate static int no_daemon_flag = 0;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate /* debug goes to stderr unless inetd_flag is set */
1600Sstevel@tonic-gate int log_stderr = 0;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /* Saved arguments to main(). */
1630Sstevel@tonic-gate static char **saved_argv;
1640Sstevel@tonic-gate static int saved_argc;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * The sockets that the server is listening; this is used in the SIGHUP
1680Sstevel@tonic-gate  * signal handler.
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate #define	MAX_LISTEN_SOCKS	16
1710Sstevel@tonic-gate static int listen_socks[MAX_LISTEN_SOCKS];
1720Sstevel@tonic-gate static int num_listen_socks = 0;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * the client's version string, passed by sshd2 in compat mode. if != NULL,
1760Sstevel@tonic-gate  * sshd will skip the version-number exchange
1770Sstevel@tonic-gate  */
1780Sstevel@tonic-gate static char *client_version_string = NULL;
1790Sstevel@tonic-gate static char *server_version_string = NULL;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /* for rekeying XXX fixme */
1820Sstevel@tonic-gate Kex *xxx_kex;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate  * Any really sensitive data in the application is contained in this
1860Sstevel@tonic-gate  * structure. The idea is that this structure could be locked into memory so
1870Sstevel@tonic-gate  * that the pages do not get written into swap.  However, there are some
1880Sstevel@tonic-gate  * problems. The private key contains BIGNUMs, and we do not (in principle)
1890Sstevel@tonic-gate  * have access to the internals of them, and locking just the structure is
1900Sstevel@tonic-gate  * not very useful.  Currently, memory locking is not implemented.
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate static struct {
1930Sstevel@tonic-gate 	Key	*server_key;		/* ephemeral server key */
1940Sstevel@tonic-gate 	Key	*ssh1_host_key;		/* ssh1 host key */
1950Sstevel@tonic-gate 	Key	**host_keys;		/* all private host keys */
1960Sstevel@tonic-gate 	int	have_ssh1_key;
1970Sstevel@tonic-gate 	int	have_ssh2_key;
1980Sstevel@tonic-gate 	u_char	ssh1_cookie[SSH_SESSION_KEY_LENGTH];
1990Sstevel@tonic-gate } sensitive_data;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * Flag indicating whether the RSA server key needs to be regenerated.
2030Sstevel@tonic-gate  * Is set in the SIGALRM handler and cleared when the key is regenerated.
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate static volatile sig_atomic_t key_do_regen = 0;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate /* This is set to true when a signal is received. */
2080Sstevel@tonic-gate static volatile sig_atomic_t received_sighup = 0;
2090Sstevel@tonic-gate static volatile sig_atomic_t received_sigterm = 0;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate /* session identifier, used by RSA-auth */
2120Sstevel@tonic-gate u_char session_id[16];
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /* same for ssh2 */
2150Sstevel@tonic-gate u_char *session_id2 = NULL;
2160Sstevel@tonic-gate int session_id2_len = 0;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate /* record remote hostname or ip */
2190Sstevel@tonic-gate u_int utmp_len = MAXHOSTNAMELEN;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate /* options.max_startup sized array of fd ints */
2220Sstevel@tonic-gate static int *startup_pipes = NULL;
2230Sstevel@tonic-gate static int startup_pipe = -1;	/* in child */
2240Sstevel@tonic-gate 
22511044SHuie-Ying.Lee@Sun.COM /* sshd_config buffer */
22611044SHuie-Ying.Lee@Sun.COM Buffer cfg;
22711044SHuie-Ying.Lee@Sun.COM 
2280Sstevel@tonic-gate #ifdef GSSAPI
2290Sstevel@tonic-gate static gss_OID_set mechs = GSS_C_NULL_OID_SET;
2300Sstevel@tonic-gate #endif /* GSSAPI */
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate /* Prototypes for various functions defined later in this file. */
2330Sstevel@tonic-gate void destroy_sensitive_data(void);
2340Sstevel@tonic-gate static void demote_sensitive_data(void);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate static void do_ssh1_kex(void);
2370Sstevel@tonic-gate static void do_ssh2_kex(void);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate  * Close all listening sockets
2410Sstevel@tonic-gate  */
2420Sstevel@tonic-gate static void
close_listen_socks(void)2430Sstevel@tonic-gate close_listen_socks(void)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	int i;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	for (i = 0; i < num_listen_socks; i++)
2480Sstevel@tonic-gate 		(void) close(listen_socks[i]);
2490Sstevel@tonic-gate 	num_listen_socks = -1;
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate static void
close_startup_pipes(void)2530Sstevel@tonic-gate close_startup_pipes(void)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate 	int i;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if (startup_pipes)
2580Sstevel@tonic-gate 		for (i = 0; i < options.max_startups; i++)
2590Sstevel@tonic-gate 			if (startup_pipes[i] != -1)
2600Sstevel@tonic-gate 				(void) close(startup_pipes[i]);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate  * Signal handler for SIGHUP.  Sshd execs itself when it receives SIGHUP;
2650Sstevel@tonic-gate  * the effect is to reread the configuration file (and to regenerate
2660Sstevel@tonic-gate  * the server key).
2670Sstevel@tonic-gate  */
2680Sstevel@tonic-gate static void
sighup_handler(int sig)2690Sstevel@tonic-gate sighup_handler(int sig)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	int save_errno = errno;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	received_sighup = 1;
2740Sstevel@tonic-gate 	(void) signal(SIGHUP, sighup_handler);
2750Sstevel@tonic-gate 	errno = save_errno;
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * Called from the main program after receiving SIGHUP.
2800Sstevel@tonic-gate  * Restarts the server.
2810Sstevel@tonic-gate  */
2820Sstevel@tonic-gate static void
sighup_restart(void)2830Sstevel@tonic-gate sighup_restart(void)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	log("Received SIGHUP; restarting.");
2860Sstevel@tonic-gate 	close_listen_socks();
2870Sstevel@tonic-gate 	close_startup_pipes();
2880Sstevel@tonic-gate 	(void) execv(saved_argv[0], saved_argv);
2890Sstevel@tonic-gate 	log("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
2900Sstevel@tonic-gate 	    strerror(errno));
2910Sstevel@tonic-gate 	exit(1);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate  * Generic signal handler for terminating signals in the master daemon.
2960Sstevel@tonic-gate  */
2970Sstevel@tonic-gate static void
sigterm_handler(int sig)2980Sstevel@tonic-gate sigterm_handler(int sig)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	received_sigterm = sig;
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * SIGCHLD handler.  This is called whenever a child dies.  This will then
3050Sstevel@tonic-gate  * reap any zombies left by exited children.
3060Sstevel@tonic-gate  */
3070Sstevel@tonic-gate static void
main_sigchld_handler(int sig)3080Sstevel@tonic-gate main_sigchld_handler(int sig)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate 	int save_errno = errno;
3110Sstevel@tonic-gate 	pid_t pid;
3120Sstevel@tonic-gate 	int status;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
3150Sstevel@tonic-gate 	    (pid < 0 && errno == EINTR))
3160Sstevel@tonic-gate 		;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	(void) signal(SIGCHLD, main_sigchld_handler);
3190Sstevel@tonic-gate 	errno = save_errno;
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate /*
32310515SJan.Pechanec@Sun.COM  * Signal handler for the alarm after the login grace period has expired. This
32410515SJan.Pechanec@Sun.COM  * is for the (soon-to-be) unprivileged child only. The monitor gets an event on
32510515SJan.Pechanec@Sun.COM  * the communication pipe and exits as well.
3260Sstevel@tonic-gate  */
3270Sstevel@tonic-gate static void
grace_alarm_handler(int sig)3280Sstevel@tonic-gate grace_alarm_handler(int sig)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate 	/* Log error and exit. */
33110515SJan.Pechanec@Sun.COM 	fatal("Timeout before authentication for %.200s", get_remote_ipaddr());
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate #ifdef HAVE_SOLARIS_CONTRACTS
3350Sstevel@tonic-gate static int contracts_fd = -1;
3360Sstevel@tonic-gate void
contracts_pre_fork()3370Sstevel@tonic-gate contracts_pre_fork()
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	const char *during = "opening process contract template";
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/*
3420Sstevel@tonic-gate 	 * Failure should not be treated as fatal on the theory that
3430Sstevel@tonic-gate 	 * it's better to start with children in the same contract as
3440Sstevel@tonic-gate 	 * the master listener than not at all.
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if (contracts_fd == -1) {
3480Sstevel@tonic-gate 		if ((contracts_fd = open64(CTFS_ROOT "/process/template",
3490Sstevel@tonic-gate 				O_RDWR)) == -1)
3500Sstevel@tonic-gate 			goto cleanup;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 		during = "setting sundry contract terms";
3530Sstevel@tonic-gate 		if ((errno = ct_pr_tmpl_set_param(contracts_fd, CT_PR_PGRPONLY)))
3540Sstevel@tonic-gate 			goto cleanup;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 		if ((errno = ct_tmpl_set_informative(contracts_fd, CT_PR_EV_HWERR)))
3570Sstevel@tonic-gate 			goto cleanup;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 		if ((errno = ct_pr_tmpl_set_fatal(contracts_fd, CT_PR_EV_HWERR)))
3600Sstevel@tonic-gate 			goto cleanup;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 		if ((errno = ct_tmpl_set_critical(contracts_fd, 0)))
3630Sstevel@tonic-gate 			goto cleanup;
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	during = "setting active template";
3670Sstevel@tonic-gate 	if ((errno = ct_tmpl_activate(contracts_fd)))
3680Sstevel@tonic-gate 		goto cleanup;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	debug3("Set active contract");
3710Sstevel@tonic-gate 	return;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate cleanup:
3740Sstevel@tonic-gate 	if (contracts_fd != -1)
3750Sstevel@tonic-gate 		(void) close(contracts_fd);
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	contracts_fd = -1;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	if (errno)
3800Sstevel@tonic-gate 		debug2("Error while trying to set up active contract"
3810Sstevel@tonic-gate 			" template: %s while %s", strerror(errno), during);
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate void
contracts_post_fork_child()3850Sstevel@tonic-gate contracts_post_fork_child()
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate 	/* Clear active template so fork() creates no new contracts. */
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (contracts_fd == -1)
3900Sstevel@tonic-gate 		return;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if ((errno = (ct_tmpl_clear(contracts_fd))))
3930Sstevel@tonic-gate 		debug2("Error while trying to clear active contract template"
3940Sstevel@tonic-gate 			" (child): %s", strerror(errno));
3950Sstevel@tonic-gate 	else
3960Sstevel@tonic-gate 		debug3("Cleared active contract template (child)");
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	(void) close(contracts_fd);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	contracts_fd = -1;
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate void
contracts_post_fork_parent(int fork_succeeded)4040Sstevel@tonic-gate contracts_post_fork_parent(int fork_succeeded)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate 	char path[PATH_MAX];
4070Sstevel@tonic-gate 	int cfd, n;
4080Sstevel@tonic-gate 	ct_stathdl_t st;
4090Sstevel@tonic-gate 	ctid_t latest;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	/* Clear active template, abandon latest contract. */
4120Sstevel@tonic-gate 	if (contracts_fd == -1)
4130Sstevel@tonic-gate 		return;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if ((errno = ct_tmpl_clear(contracts_fd)))
4160Sstevel@tonic-gate 		debug2("Error while clearing active contract template: %s",
4170Sstevel@tonic-gate 			strerror(errno));
4180Sstevel@tonic-gate 	else
4190Sstevel@tonic-gate 		debug3("Cleared active contract template (parent)");
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (!fork_succeeded)
4220Sstevel@tonic-gate 		return;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	if ((cfd = open64(CTFS_ROOT "/process/latest", O_RDONLY)) == -1) {
4250Sstevel@tonic-gate 		debug2("Error while getting latest contract: %s",
4260Sstevel@tonic-gate 			strerror(errno));
4270Sstevel@tonic-gate 		return;
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	if ((errno = ct_status_read(cfd, CTD_COMMON, &st)) != 0) {
4310Sstevel@tonic-gate 		debug2("Error while getting latest contract ID: %s",
4320Sstevel@tonic-gate 			strerror(errno));
4330Sstevel@tonic-gate 		(void) close(cfd);
4340Sstevel@tonic-gate 		return;
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	latest = ct_status_get_id(st);
4380Sstevel@tonic-gate 	ct_status_free(st);
4390Sstevel@tonic-gate 	(void) close(cfd);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	n = snprintf(path, PATH_MAX, CTFS_ROOT "/all/%ld/ctl", latest);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	if (n >= PATH_MAX) {
4440Sstevel@tonic-gate 		debug2("Error while opening the latest contract ctl file: %s",
4450Sstevel@tonic-gate 			strerror(ENAMETOOLONG));
4460Sstevel@tonic-gate 		return;
4470Sstevel@tonic-gate 	}
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	if ((cfd = open64(path, O_WRONLY)) == -1) {
4500Sstevel@tonic-gate 		debug2("Error while opening the latest contract ctl file: %s",
4510Sstevel@tonic-gate 			strerror(errno));
4520Sstevel@tonic-gate 		return;
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if ((errno = ct_ctl_abandon(cfd)))
4560Sstevel@tonic-gate 		debug2("Error while abandoning latest contract: %s",
4570Sstevel@tonic-gate 			strerror(errno));
4580Sstevel@tonic-gate 	else
4590Sstevel@tonic-gate 		debug3("Abandoned latest contract");
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	(void) close(cfd);
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate #endif /* HAVE_SOLARIS_CONTRACTS */
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate /*
4660Sstevel@tonic-gate  * Signal handler for the key regeneration alarm.  Note that this
4670Sstevel@tonic-gate  * alarm only occurs in the daemon waiting for connections, and it does not
4680Sstevel@tonic-gate  * do anything with the private key or random state before forking.
4690Sstevel@tonic-gate  * Thus there should be no concurrency control/asynchronous execution
4700Sstevel@tonic-gate  * problems.
4710Sstevel@tonic-gate  */
4720Sstevel@tonic-gate static void
generate_ephemeral_server_key(void)4730Sstevel@tonic-gate generate_ephemeral_server_key(void)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate 	u_int32_t rnd = 0;
4760Sstevel@tonic-gate 	int i;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	verbose("Generating %s%d bit RSA key.",
4790Sstevel@tonic-gate 	    sensitive_data.server_key ? "new " : "", options.server_key_bits);
4800Sstevel@tonic-gate 	if (sensitive_data.server_key != NULL)
4810Sstevel@tonic-gate 		key_free(sensitive_data.server_key);
4820Sstevel@tonic-gate 	sensitive_data.server_key = key_generate(KEY_RSA1,
4830Sstevel@tonic-gate 	    options.server_key_bits);
4840Sstevel@tonic-gate 	verbose("RSA key generation complete.");
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
4870Sstevel@tonic-gate 		if (i % 4 == 0)
4880Sstevel@tonic-gate 			rnd = arc4random();
4890Sstevel@tonic-gate 		sensitive_data.ssh1_cookie[i] = rnd & 0xff;
4900Sstevel@tonic-gate 		rnd >>= 8;
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 	arc4random_stir();
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate static void
key_regeneration_alarm(int sig)4960Sstevel@tonic-gate key_regeneration_alarm(int sig)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate 	int save_errno = errno;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	(void) signal(SIGALRM, SIG_DFL);
5010Sstevel@tonic-gate 	errno = save_errno;
5020Sstevel@tonic-gate 	key_do_regen = 1;
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate static void
sshd_exchange_identification(int sock_in,int sock_out)5060Sstevel@tonic-gate sshd_exchange_identification(int sock_in, int sock_out)
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	int i, mismatch;
5090Sstevel@tonic-gate 	int remote_major, remote_minor;
5100Sstevel@tonic-gate 	int major, minor;
5110Sstevel@tonic-gate 	char *s;
5120Sstevel@tonic-gate 	char buf[256];			/* Must not be larger than remote_version. */
5130Sstevel@tonic-gate 	char remote_version[256];	/* Must be at least as big as buf. */
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if ((options.protocol & SSH_PROTO_1) &&
5160Sstevel@tonic-gate 	    (options.protocol & SSH_PROTO_2)) {
5170Sstevel@tonic-gate 		major = PROTOCOL_MAJOR_1;
5180Sstevel@tonic-gate 		minor = 99;
5190Sstevel@tonic-gate 	} else if (options.protocol & SSH_PROTO_2) {
5200Sstevel@tonic-gate 		major = PROTOCOL_MAJOR_2;
5210Sstevel@tonic-gate 		minor = PROTOCOL_MINOR_2;
5220Sstevel@tonic-gate 	} else {
5230Sstevel@tonic-gate 		major = PROTOCOL_MAJOR_1;
5240Sstevel@tonic-gate 		minor = PROTOCOL_MINOR_1;
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 	(void) snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
5270Sstevel@tonic-gate 	server_version_string = xstrdup(buf);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	if (client_version_string == NULL) {
5300Sstevel@tonic-gate 		/* Send our protocol version identification. */
5310Sstevel@tonic-gate 		if (atomicio(write, sock_out, server_version_string,
5320Sstevel@tonic-gate 		    strlen(server_version_string))
5330Sstevel@tonic-gate 		    != strlen(server_version_string)) {
5340Sstevel@tonic-gate 			log("Could not write ident string to %s", get_remote_ipaddr());
5350Sstevel@tonic-gate 			fatal_cleanup();
5360Sstevel@tonic-gate 		}
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 		/* Read other sides version identification. */
5390Sstevel@tonic-gate 		(void) memset(buf, 0, sizeof(buf));
5400Sstevel@tonic-gate 		for (i = 0; i < sizeof(buf) - 1; i++) {
5410Sstevel@tonic-gate 			if (atomicio(read, sock_in, &buf[i], 1) != 1) {
5420Sstevel@tonic-gate 				log("Did not receive identification string from %s",
5430Sstevel@tonic-gate 				    get_remote_ipaddr());
5440Sstevel@tonic-gate 				fatal_cleanup();
5450Sstevel@tonic-gate 			}
5460Sstevel@tonic-gate 			if (buf[i] == '\r') {
5470Sstevel@tonic-gate 				buf[i] = 0;
5480Sstevel@tonic-gate 				/* Kludge for F-Secure Macintosh < 1.0.2 */
5490Sstevel@tonic-gate 				if (i == 12 &&
5500Sstevel@tonic-gate 				    strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
5510Sstevel@tonic-gate 					break;
5520Sstevel@tonic-gate 				continue;
5530Sstevel@tonic-gate 			}
5540Sstevel@tonic-gate 			if (buf[i] == '\n') {
5550Sstevel@tonic-gate 				buf[i] = 0;
5560Sstevel@tonic-gate 				break;
5570Sstevel@tonic-gate 			}
5580Sstevel@tonic-gate 		}
5590Sstevel@tonic-gate 		buf[sizeof(buf) - 1] = 0;
5600Sstevel@tonic-gate 		client_version_string = xstrdup(buf);
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	/*
5640Sstevel@tonic-gate 	 * Check that the versions match.  In future this might accept
5650Sstevel@tonic-gate 	 * several versions and set appropriate flags to handle them.
5660Sstevel@tonic-gate 	 */
5670Sstevel@tonic-gate 	if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
5680Sstevel@tonic-gate 	    &remote_major, &remote_minor, remote_version) != 3) {
5690Sstevel@tonic-gate 		s = "Protocol mismatch.\n";
5700Sstevel@tonic-gate 		(void) atomicio(write, sock_out, s, strlen(s));
5710Sstevel@tonic-gate 		(void) close(sock_in);
5720Sstevel@tonic-gate 		(void) close(sock_out);
5730Sstevel@tonic-gate 		log("Bad protocol version identification '%.100s' from %s",
5740Sstevel@tonic-gate 		    client_version_string, get_remote_ipaddr());
5750Sstevel@tonic-gate 		fatal_cleanup();
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 	debug("Client protocol version %d.%d; client software version %.100s",
5780Sstevel@tonic-gate 	    remote_major, remote_minor, remote_version);
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	compat_datafellows(remote_version);
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	if (datafellows & SSH_BUG_PROBE) {
5830Sstevel@tonic-gate 		log("probed from %s with %s.  Don't panic.",
5840Sstevel@tonic-gate 		    get_remote_ipaddr(), client_version_string);
5850Sstevel@tonic-gate 		fatal_cleanup();
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	if (datafellows & SSH_BUG_SCANNER) {
5890Sstevel@tonic-gate 		log("scanned from %s with %s.  Don't panic.",
5900Sstevel@tonic-gate 		    get_remote_ipaddr(), client_version_string);
5910Sstevel@tonic-gate 		fatal_cleanup();
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	mismatch = 0;
5950Sstevel@tonic-gate 	switch (remote_major) {
5960Sstevel@tonic-gate 	case 1:
5970Sstevel@tonic-gate 		if (remote_minor == 99) {
5980Sstevel@tonic-gate 			if (options.protocol & SSH_PROTO_2)
5990Sstevel@tonic-gate 				enable_compat20();
6000Sstevel@tonic-gate 			else
6010Sstevel@tonic-gate 				mismatch = 1;
6020Sstevel@tonic-gate 			break;
6030Sstevel@tonic-gate 		}
6040Sstevel@tonic-gate 		if (!(options.protocol & SSH_PROTO_1)) {
6050Sstevel@tonic-gate 			mismatch = 1;
6060Sstevel@tonic-gate 			break;
6070Sstevel@tonic-gate 		}
6080Sstevel@tonic-gate 		if (remote_minor < 3) {
6090Sstevel@tonic-gate 			packet_disconnect("Your ssh version is too old and "
6100Sstevel@tonic-gate 			    "is no longer supported.  Please install a newer version.");
6110Sstevel@tonic-gate 		} else if (remote_minor == 3) {
6120Sstevel@tonic-gate 			/* note that this disables agent-forwarding */
6130Sstevel@tonic-gate 			enable_compat13();
6140Sstevel@tonic-gate 		}
6150Sstevel@tonic-gate 		break;
6160Sstevel@tonic-gate 	case 2:
6170Sstevel@tonic-gate 		if (options.protocol & SSH_PROTO_2) {
6180Sstevel@tonic-gate 			enable_compat20();
6190Sstevel@tonic-gate 			break;
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 		/* FALLTHROUGH */
6220Sstevel@tonic-gate 	default:
6230Sstevel@tonic-gate 		mismatch = 1;
6240Sstevel@tonic-gate 		break;
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 	chop(server_version_string);
6270Sstevel@tonic-gate 	debug("Local version string %.200s", server_version_string);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	if (mismatch) {
6300Sstevel@tonic-gate 		s = "Protocol major versions differ.\n";
6310Sstevel@tonic-gate 		(void) atomicio(write, sock_out, s, strlen(s));
6320Sstevel@tonic-gate 		(void) close(sock_in);
6330Sstevel@tonic-gate 		(void) close(sock_out);
6340Sstevel@tonic-gate 		log("Protocol major versions differ for %s: %.200s vs. %.200s",
6350Sstevel@tonic-gate 		    get_remote_ipaddr(),
6360Sstevel@tonic-gate 		    server_version_string, client_version_string);
6370Sstevel@tonic-gate 		fatal_cleanup();
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate /* Destroy the host and server keys.  They will no longer be needed. */
6420Sstevel@tonic-gate void
destroy_sensitive_data(void)6430Sstevel@tonic-gate destroy_sensitive_data(void)
6440Sstevel@tonic-gate {
6450Sstevel@tonic-gate 	int i;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	if (sensitive_data.server_key) {
6480Sstevel@tonic-gate 		key_free(sensitive_data.server_key);
6490Sstevel@tonic-gate 		sensitive_data.server_key = NULL;
6500Sstevel@tonic-gate 	}
6510Sstevel@tonic-gate 	for (i = 0; i < options.num_host_key_files; i++) {
6520Sstevel@tonic-gate 		if (sensitive_data.host_keys[i]) {
6530Sstevel@tonic-gate 			key_free(sensitive_data.host_keys[i]);
6540Sstevel@tonic-gate 			sensitive_data.host_keys[i] = NULL;
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 	sensitive_data.ssh1_host_key = NULL;
6580Sstevel@tonic-gate 	(void) memset(sensitive_data.ssh1_cookie, 0, SSH_SESSION_KEY_LENGTH);
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate /* Demote private to public keys for network child */
6620Sstevel@tonic-gate static void
demote_sensitive_data(void)6630Sstevel@tonic-gate demote_sensitive_data(void)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	Key *tmp;
6660Sstevel@tonic-gate 	int i;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	if (sensitive_data.server_key) {
6690Sstevel@tonic-gate 		tmp = key_demote(sensitive_data.server_key);
6700Sstevel@tonic-gate 		key_free(sensitive_data.server_key);
6710Sstevel@tonic-gate 		sensitive_data.server_key = tmp;
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	for (i = 0; i < options.num_host_key_files; i++) {
6750Sstevel@tonic-gate 		if (sensitive_data.host_keys[i]) {
6760Sstevel@tonic-gate 			tmp = key_demote(sensitive_data.host_keys[i]);
6770Sstevel@tonic-gate 			key_free(sensitive_data.host_keys[i]);
6780Sstevel@tonic-gate 			sensitive_data.host_keys[i] = tmp;
6790Sstevel@tonic-gate 			if (tmp->type == KEY_RSA1)
6800Sstevel@tonic-gate 				sensitive_data.ssh1_host_key = tmp;
6810Sstevel@tonic-gate 		}
6820Sstevel@tonic-gate 	}
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	/* We do not clear ssh1_host key and cookie.  XXX - Okay Niels? */
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate static char *
list_hostkey_types(void)6880Sstevel@tonic-gate list_hostkey_types(void)
6890Sstevel@tonic-gate {
6900Sstevel@tonic-gate 	Buffer b;
6910Sstevel@tonic-gate 	char *p;
6920Sstevel@tonic-gate 	int i;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	buffer_init(&b);
6950Sstevel@tonic-gate 	for (i = 0; i < options.num_host_key_files; i++) {
6960Sstevel@tonic-gate 		Key *key = sensitive_data.host_keys[i];
6970Sstevel@tonic-gate 		if (key == NULL)
6980Sstevel@tonic-gate 			continue;
6990Sstevel@tonic-gate 		switch (key->type) {
7000Sstevel@tonic-gate 		case KEY_RSA:
7010Sstevel@tonic-gate 		case KEY_DSA:
7020Sstevel@tonic-gate 			if (buffer_len(&b) > 0)
7030Sstevel@tonic-gate 				buffer_append(&b, ",", 1);
7040Sstevel@tonic-gate 			p = key_ssh_name(key);
7050Sstevel@tonic-gate 			buffer_append(&b, p, strlen(p));
7060Sstevel@tonic-gate 			break;
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 	}
7090Sstevel@tonic-gate 	buffer_append(&b, "\0", 1);
7100Sstevel@tonic-gate 	p = xstrdup(buffer_ptr(&b));
7110Sstevel@tonic-gate 	buffer_free(&b);
7120Sstevel@tonic-gate 	debug("list_hostkey_types: %s", p);
7130Sstevel@tonic-gate 	return p;
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate #ifdef lint
7170Sstevel@tonic-gate static
7180Sstevel@tonic-gate #endif /* lint */
7190Sstevel@tonic-gate Key *
get_hostkey_by_type(int type)7200Sstevel@tonic-gate get_hostkey_by_type(int type)
7210Sstevel@tonic-gate {
7220Sstevel@tonic-gate 	int i;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	for (i = 0; i < options.num_host_key_files; i++) {
7250Sstevel@tonic-gate 		Key *key = sensitive_data.host_keys[i];
7260Sstevel@tonic-gate 		if (key != NULL && key->type == type)
7270Sstevel@tonic-gate 			return key;
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 	return NULL;
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate #ifdef lint
7330Sstevel@tonic-gate static
7340Sstevel@tonic-gate #endif /* lint */
7350Sstevel@tonic-gate Key *
get_hostkey_by_index(int ind)7360Sstevel@tonic-gate get_hostkey_by_index(int ind)
7370Sstevel@tonic-gate {
7380Sstevel@tonic-gate 	if (ind < 0 || ind >= options.num_host_key_files)
7390Sstevel@tonic-gate 		return (NULL);
7400Sstevel@tonic-gate 	return (sensitive_data.host_keys[ind]);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate #ifdef lint
7440Sstevel@tonic-gate static
7450Sstevel@tonic-gate #endif /* lint */
7460Sstevel@tonic-gate int
get_hostkey_index(Key * key)7470Sstevel@tonic-gate get_hostkey_index(Key *key)
7480Sstevel@tonic-gate {
7490Sstevel@tonic-gate 	int i;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	for (i = 0; i < options.num_host_key_files; i++) {
7520Sstevel@tonic-gate 		if (key == sensitive_data.host_keys[i])
7530Sstevel@tonic-gate 			return (i);
7540Sstevel@tonic-gate 	}
7550Sstevel@tonic-gate 	return (-1);
7560Sstevel@tonic-gate }
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate /*
7590Sstevel@tonic-gate  * returns 1 if connection should be dropped, 0 otherwise.
7600Sstevel@tonic-gate  * dropping starts at connection #max_startups_begin with a probability
7610Sstevel@tonic-gate  * of (max_startups_rate/100). the probability increases linearly until
7620Sstevel@tonic-gate  * all connections are dropped for startups > max_startups
7630Sstevel@tonic-gate  */
7640Sstevel@tonic-gate static int
drop_connection(int startups)7650Sstevel@tonic-gate drop_connection(int startups)
7660Sstevel@tonic-gate {
7670Sstevel@tonic-gate 	double p, r;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	if (startups < options.max_startups_begin)
7700Sstevel@tonic-gate 		return 0;
7710Sstevel@tonic-gate 	if (startups >= options.max_startups)
7720Sstevel@tonic-gate 		return 1;
7730Sstevel@tonic-gate 	if (options.max_startups_rate == 100)
7740Sstevel@tonic-gate 		return 1;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	p  = 100 - options.max_startups_rate;
7770Sstevel@tonic-gate 	p *= startups - options.max_startups_begin;
7780Sstevel@tonic-gate 	p /= (double) (options.max_startups - options.max_startups_begin);
7790Sstevel@tonic-gate 	p += options.max_startups_rate;
7800Sstevel@tonic-gate 	p /= 100.0;
7810Sstevel@tonic-gate 	r = arc4random() / (double) UINT_MAX;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	debug("drop_connection: p %g, r %g", p, r);
7840Sstevel@tonic-gate 	return (r < p) ? 1 : 0;
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate static void
usage(void)7880Sstevel@tonic-gate usage(void)
7890Sstevel@tonic-gate {
7900Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("sshd version %s\n"), SSH_VERSION);
7910Sstevel@tonic-gate 	(void) fprintf(stderr,
7920Sstevel@tonic-gate 	    gettext("Usage: %s [options]\n"
7930Sstevel@tonic-gate 		"Options:\n"
7940Sstevel@tonic-gate 		"  -f file    Configuration file (default %s)\n"
7950Sstevel@tonic-gate 		"  -d         Debugging mode (multiple -d means more "
7960Sstevel@tonic-gate 		"debugging)\n"
7970Sstevel@tonic-gate 		"  -i         Started from inetd\n"
7980Sstevel@tonic-gate 		"  -D         Do not fork into daemon mode\n"
7990Sstevel@tonic-gate 		"  -t         Only test configuration file and keys\n"
8000Sstevel@tonic-gate 		"  -q         Quiet (no logging)\n"
8010Sstevel@tonic-gate 		"  -p port    Listen on the specified port (default: 22)\n"
8020Sstevel@tonic-gate 		"  -k seconds Regenerate server key every this many seconds "
8030Sstevel@tonic-gate 		"(default: 3600)\n"
8040Sstevel@tonic-gate 		"  -g seconds Grace period for authentication (default: 600)\n"
8050Sstevel@tonic-gate 		"  -b bits    Size of server RSA key (default: 768 bits)\n"
8060Sstevel@tonic-gate 		"  -h file    File from which to read host key (default: %s)\n"
8070Sstevel@tonic-gate 		"  -4         Use IPv4 only\n"
8080Sstevel@tonic-gate 		"  -6         Use IPv6 only\n"
8090Sstevel@tonic-gate 		"  -o option  Process the option as if it was read from "
8100Sstevel@tonic-gate 		"a configuration file.\n"),
8110Sstevel@tonic-gate 	    __progname, _PATH_SERVER_CONFIG_FILE, _PATH_HOST_KEY_FILE);
8120Sstevel@tonic-gate 	exit(1);
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate /*
8160Sstevel@tonic-gate  * Main program for the daemon.
8170Sstevel@tonic-gate  */
8180Sstevel@tonic-gate int
main(int ac,char ** av)8190Sstevel@tonic-gate main(int ac, char **av)
8200Sstevel@tonic-gate {
8210Sstevel@tonic-gate 	extern char *optarg;
8220Sstevel@tonic-gate 	extern int optind;
8237574SJan.Pechanec@Sun.COM 	int opt, j, i, fdsetsz, sock_in = 0, sock_out = 0, newsock = -1, on = 1;
8240Sstevel@tonic-gate 	pid_t pid;
8250Sstevel@tonic-gate 	socklen_t fromlen;
8260Sstevel@tonic-gate 	fd_set *fdset;
8270Sstevel@tonic-gate 	struct sockaddr_storage from;
8280Sstevel@tonic-gate 	const char *remote_ip;
8290Sstevel@tonic-gate 	int remote_port;
8300Sstevel@tonic-gate 	FILE *f;
8310Sstevel@tonic-gate 	struct addrinfo *ai;
8320Sstevel@tonic-gate 	char ntop[NI_MAXHOST], strport[NI_MAXSERV];
8330Sstevel@tonic-gate 	int listen_sock, maxfd;
8340Sstevel@tonic-gate 	int startup_p[2];
8350Sstevel@tonic-gate 	int startups = 0;
8367574SJan.Pechanec@Sun.COM 	Authctxt *authctxt = NULL;
8370Sstevel@tonic-gate 	Key *key;
8380Sstevel@tonic-gate 	int ret, key_used = 0;
8390Sstevel@tonic-gate #ifdef HAVE_BSM
8400Sstevel@tonic-gate 	au_id_t	    auid = AU_NOAUDITID;
8410Sstevel@tonic-gate #endif /* HAVE_BSM */
8427574SJan.Pechanec@Sun.COM 	int mpipe;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	__progname = get_progname(av[0]);
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	(void) g11n_setlocale(LC_ALL, "");
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	init_rng();
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	/* Save argv. */
8510Sstevel@tonic-gate 	saved_argc = ac;
8520Sstevel@tonic-gate 	saved_argv = av;
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	/* Initialize configuration options to their default values. */
8550Sstevel@tonic-gate 	initialize_server_options(&options);
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	/* Parse command-line arguments. */
8580Sstevel@tonic-gate 	while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:u:o:dDeiqtQ46")) != -1) {
8590Sstevel@tonic-gate 		switch (opt) {
8600Sstevel@tonic-gate 		case '4':
8610Sstevel@tonic-gate 			IPv4or6 = AF_INET;
8620Sstevel@tonic-gate 			break;
8630Sstevel@tonic-gate 		case '6':
8640Sstevel@tonic-gate 			IPv4or6 = AF_INET6;
8650Sstevel@tonic-gate 			break;
8660Sstevel@tonic-gate 		case 'f':
8670Sstevel@tonic-gate 			config_file_name = optarg;
8680Sstevel@tonic-gate 			break;
8690Sstevel@tonic-gate 		case 'd':
8700Sstevel@tonic-gate 			if (0 == debug_flag) {
8710Sstevel@tonic-gate 				debug_flag = 1;
8720Sstevel@tonic-gate 				options.log_level = SYSLOG_LEVEL_DEBUG1;
8730Sstevel@tonic-gate 			} else if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
8740Sstevel@tonic-gate 				options.log_level++;
8750Sstevel@tonic-gate 			} else {
8760Sstevel@tonic-gate 				(void) fprintf(stderr,
8770Sstevel@tonic-gate 					gettext("Debug level too high.\n"));
8780Sstevel@tonic-gate 				exit(1);
8790Sstevel@tonic-gate 			}
8800Sstevel@tonic-gate 			break;
8810Sstevel@tonic-gate 		case 'D':
8820Sstevel@tonic-gate 			no_daemon_flag = 1;
8830Sstevel@tonic-gate 			break;
8840Sstevel@tonic-gate 		case 'e':
8850Sstevel@tonic-gate 			log_stderr = 1;
8860Sstevel@tonic-gate 			break;
8870Sstevel@tonic-gate 		case 'i':
8880Sstevel@tonic-gate 			inetd_flag = 1;
8890Sstevel@tonic-gate 			break;
8900Sstevel@tonic-gate 		case 'Q':
8910Sstevel@tonic-gate 			/* ignored */
8920Sstevel@tonic-gate 			break;
8930Sstevel@tonic-gate 		case 'q':
8940Sstevel@tonic-gate 			options.log_level = SYSLOG_LEVEL_QUIET;
8950Sstevel@tonic-gate 			break;
8960Sstevel@tonic-gate 		case 'b':
8970Sstevel@tonic-gate 			options.server_key_bits = atoi(optarg);
8980Sstevel@tonic-gate 			break;
8990Sstevel@tonic-gate 		case 'p':
9000Sstevel@tonic-gate 			options.ports_from_cmdline = 1;
9010Sstevel@tonic-gate 			if (options.num_ports >= MAX_PORTS) {
9020Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("too many ports.\n"));
9030Sstevel@tonic-gate 				exit(1);
9040Sstevel@tonic-gate 			}
9050Sstevel@tonic-gate 			options.ports[options.num_ports++] = a2port(optarg);
9060Sstevel@tonic-gate 			if (options.ports[options.num_ports-1] == 0) {
9070Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("Bad port number.\n"));
9080Sstevel@tonic-gate 				exit(1);
9090Sstevel@tonic-gate 			}
9100Sstevel@tonic-gate 			break;
9110Sstevel@tonic-gate 		case 'g':
9120Sstevel@tonic-gate 			if ((options.login_grace_time = convtime(optarg)) == -1) {
9130Sstevel@tonic-gate 				(void) fprintf(stderr,
9140Sstevel@tonic-gate 					gettext("Invalid login grace time.\n"));
9150Sstevel@tonic-gate 				exit(1);
9160Sstevel@tonic-gate 			}
9170Sstevel@tonic-gate 			break;
9180Sstevel@tonic-gate 		case 'k':
9190Sstevel@tonic-gate 			if ((options.key_regeneration_time = convtime(optarg)) == -1) {
9200Sstevel@tonic-gate 				(void) fprintf(stderr,
9210Sstevel@tonic-gate 					gettext("Invalid key regeneration "
9220Sstevel@tonic-gate 						"interval.\n"));
9230Sstevel@tonic-gate 				exit(1);
9240Sstevel@tonic-gate 			}
9250Sstevel@tonic-gate 			break;
9260Sstevel@tonic-gate 		case 'h':
9270Sstevel@tonic-gate 			if (options.num_host_key_files >= MAX_HOSTKEYS) {
9280Sstevel@tonic-gate 				(void) fprintf(stderr,
9290Sstevel@tonic-gate 					gettext("too many host keys.\n"));
9300Sstevel@tonic-gate 				exit(1);
9310Sstevel@tonic-gate 			}
9320Sstevel@tonic-gate 			options.host_key_files[options.num_host_key_files++] = optarg;
9330Sstevel@tonic-gate 			break;
9340Sstevel@tonic-gate 		case 'V':
9350Sstevel@tonic-gate 			client_version_string = optarg;
9360Sstevel@tonic-gate 			/* only makes sense with inetd_flag, i.e. no listen() */
9370Sstevel@tonic-gate 			inetd_flag = 1;
9380Sstevel@tonic-gate 			break;
9390Sstevel@tonic-gate 		case 't':
9400Sstevel@tonic-gate 			test_flag = 1;
9410Sstevel@tonic-gate 			break;
9420Sstevel@tonic-gate 		case 'o':
9430Sstevel@tonic-gate 			if (process_server_config_line(&options, optarg,
94411044SHuie-Ying.Lee@Sun.COM 			    "command-line", 0, NULL, NULL, NULL, NULL) != 0)
9450Sstevel@tonic-gate 				exit(1);
9460Sstevel@tonic-gate 			break;
9470Sstevel@tonic-gate 		case '?':
9480Sstevel@tonic-gate 		default:
9490Sstevel@tonic-gate 			usage();
9500Sstevel@tonic-gate 			break;
9510Sstevel@tonic-gate 		}
9520Sstevel@tonic-gate 	}
9537574SJan.Pechanec@Sun.COM 
9547574SJan.Pechanec@Sun.COM 	/*
9557574SJan.Pechanec@Sun.COM 	 * There is no need to use the PKCS#11 engine in the master SSH process.
9567574SJan.Pechanec@Sun.COM 	 */
9570Sstevel@tonic-gate 	SSLeay_add_all_algorithms();
9587574SJan.Pechanec@Sun.COM 	seed_rng();
9590Sstevel@tonic-gate 	channel_set_af(IPv4or6);
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	/*
9620Sstevel@tonic-gate 	 * Force logging to stderr until we have loaded the private host
9630Sstevel@tonic-gate 	 * key (unless started from inetd)
9640Sstevel@tonic-gate 	 */
9650Sstevel@tonic-gate 	log_init(__progname,
9660Sstevel@tonic-gate 	    options.log_level == SYSLOG_LEVEL_NOT_SET ?
9670Sstevel@tonic-gate 	    SYSLOG_LEVEL_INFO : options.log_level,
9680Sstevel@tonic-gate 	    options.log_facility == SYSLOG_FACILITY_NOT_SET ?
9690Sstevel@tonic-gate 	    SYSLOG_FACILITY_AUTH : options.log_facility,
9700Sstevel@tonic-gate 	    !inetd_flag);
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate #ifdef _UNICOS
9730Sstevel@tonic-gate 	/* Cray can define user privs drop all prives now!
9740Sstevel@tonic-gate 	 * Not needed on PRIV_SU systems!
9750Sstevel@tonic-gate 	 */
9760Sstevel@tonic-gate 	drop_cray_privs();
9770Sstevel@tonic-gate #endif
9780Sstevel@tonic-gate 
97911044SHuie-Ying.Lee@Sun.COM 	/* Fetch our configuration */
98011044SHuie-Ying.Lee@Sun.COM 	buffer_init(&cfg);
98111044SHuie-Ying.Lee@Sun.COM 	load_server_config(config_file_name, &cfg);
98211044SHuie-Ying.Lee@Sun.COM 	parse_server_config(&options, config_file_name, &cfg, NULL, NULL, NULL);
9830Sstevel@tonic-gate 
984*12597SJan.Pechanec@Sun.COM 	/*
985*12597SJan.Pechanec@Sun.COM 	 * ChallengeResponseAuthentication is deprecated for protocol 2 which is
986*12597SJan.Pechanec@Sun.COM 	 * the default setting on Solaris. Warn the user about it. Note that
987*12597SJan.Pechanec@Sun.COM 	 * ChallengeResponseAuthentication is on by default but the option is
988*12597SJan.Pechanec@Sun.COM 	 * not set until fill_default_server_options() is called. If the option
989*12597SJan.Pechanec@Sun.COM 	 * is already set now, the user must have set it manually.
990*12597SJan.Pechanec@Sun.COM 	 */
991*12597SJan.Pechanec@Sun.COM 	if ((options.protocol & SSH_PROTO_2) &&
992*12597SJan.Pechanec@Sun.COM 	    !(options.protocol & SSH_PROTO_1) &&
993*12597SJan.Pechanec@Sun.COM 	    options.challenge_response_authentication != -1) {
994*12597SJan.Pechanec@Sun.COM 		log("ChallengeResponseAuthentication has been "
995*12597SJan.Pechanec@Sun.COM 		"deprecated for the SSH Protocol 2. You should use "
996*12597SJan.Pechanec@Sun.COM 		"KbdInteractiveAuthentication instead (which defaults to "
997*12597SJan.Pechanec@Sun.COM 		"\"yes\").");
998*12597SJan.Pechanec@Sun.COM 	}
999*12597SJan.Pechanec@Sun.COM 
1000*12597SJan.Pechanec@Sun.COM 	/*
1001*12597SJan.Pechanec@Sun.COM 	 * While PAMAuthenticationViaKbdInt was not documented, it was
1002*12597SJan.Pechanec@Sun.COM 	 * previously set in our default sshd_config and also the only way to
1003*12597SJan.Pechanec@Sun.COM 	 * switch off the keyboard-interactive authentication. To maintain
1004*12597SJan.Pechanec@Sun.COM 	 * backward compatibility, if PAMAuthenticationViaKbdInt is manually set
1005*12597SJan.Pechanec@Sun.COM 	 * to "no" and KbdInteractiveAuthentication is not set, switch off the
1006*12597SJan.Pechanec@Sun.COM 	 * keyboard-interactive authentication method as before. As with the
1007*12597SJan.Pechanec@Sun.COM 	 * challenge response auth situation dealt above, we have not called
1008*12597SJan.Pechanec@Sun.COM 	 * fill_default_server_options() yet so if KbdInteractiveAuthentication
1009*12597SJan.Pechanec@Sun.COM 	 * is already set to 1 here the admin must have set it manually and we
1010*12597SJan.Pechanec@Sun.COM 	 * will honour it.
1011*12597SJan.Pechanec@Sun.COM 	 */
1012*12597SJan.Pechanec@Sun.COM 	if (options.kbd_interactive_authentication != 1 &&
1013*12597SJan.Pechanec@Sun.COM 	    options.pam_authentication_via_kbd_int == 0) {
1014*12597SJan.Pechanec@Sun.COM 		options.kbd_interactive_authentication = 0;
1015*12597SJan.Pechanec@Sun.COM 	}
1016*12597SJan.Pechanec@Sun.COM 
10170Sstevel@tonic-gate 	/* Fill in default values for those options not explicitly set. */
10180Sstevel@tonic-gate 	fill_default_server_options(&options);
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	utmp_len = options.lookup_client_hostnames ? utmp_len : 0;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	/* Check that there are no remaining arguments. */
10230Sstevel@tonic-gate 	if (optind < ac) {
10240Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Extra argument %s.\n"), av[optind]);
10250Sstevel@tonic-gate 		exit(1);
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	debug("sshd version %.100s", SSH_VERSION);
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	/* load private host keys */
10310Sstevel@tonic-gate 	if (options.num_host_key_files > 0)
10320Sstevel@tonic-gate 		sensitive_data.host_keys =
10330Sstevel@tonic-gate 		    xmalloc(options.num_host_key_files * sizeof(Key *));
10340Sstevel@tonic-gate 	for (i = 0; i < options.num_host_key_files; i++)
10350Sstevel@tonic-gate 		sensitive_data.host_keys[i] = NULL;
10360Sstevel@tonic-gate 	sensitive_data.server_key = NULL;
10370Sstevel@tonic-gate 	sensitive_data.ssh1_host_key = NULL;
10380Sstevel@tonic-gate 	sensitive_data.have_ssh1_key = 0;
10390Sstevel@tonic-gate 	sensitive_data.have_ssh2_key = 0;
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	for (i = 0; i < options.num_host_key_files; i++) {
10420Sstevel@tonic-gate 		key = key_load_private(options.host_key_files[i], "", NULL);
10430Sstevel@tonic-gate 		sensitive_data.host_keys[i] = key;
10440Sstevel@tonic-gate 		if (key == NULL) {
10450Sstevel@tonic-gate 			error("Could not load host key: %s",
10460Sstevel@tonic-gate 			    options.host_key_files[i]);
10470Sstevel@tonic-gate 			sensitive_data.host_keys[i] = NULL;
10480Sstevel@tonic-gate 			continue;
10490Sstevel@tonic-gate 		}
10500Sstevel@tonic-gate 		switch (key->type) {
10510Sstevel@tonic-gate 		case KEY_RSA1:
10520Sstevel@tonic-gate 			sensitive_data.ssh1_host_key = key;
10530Sstevel@tonic-gate 			sensitive_data.have_ssh1_key = 1;
10540Sstevel@tonic-gate 			break;
10550Sstevel@tonic-gate 		case KEY_RSA:
10560Sstevel@tonic-gate 		case KEY_DSA:
10570Sstevel@tonic-gate 			sensitive_data.have_ssh2_key = 1;
10580Sstevel@tonic-gate 			break;
10590Sstevel@tonic-gate 		}
10600Sstevel@tonic-gate 		debug("private host key: #%d type %d %s", i, key->type,
10610Sstevel@tonic-gate 		    key_type(key));
10620Sstevel@tonic-gate 	}
10630Sstevel@tonic-gate 	if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
10640Sstevel@tonic-gate 		log("Disabling protocol version 1. Could not load host key");
10650Sstevel@tonic-gate 		options.protocol &= ~SSH_PROTO_1;
10660Sstevel@tonic-gate 	}
10670Sstevel@tonic-gate 	if ((options.protocol & SSH_PROTO_2) &&
10680Sstevel@tonic-gate 	    !sensitive_data.have_ssh2_key) {
10690Sstevel@tonic-gate #ifdef GSSAPI
10700Sstevel@tonic-gate 		if (options.gss_keyex)
10710Sstevel@tonic-gate 			ssh_gssapi_server_mechs(&mechs);
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 		if (mechs == GSS_C_NULL_OID_SET) {
10740Sstevel@tonic-gate 			log("Disabling protocol version 2. Could not load host"
10750Sstevel@tonic-gate 			    "key or GSS-API mechanisms");
10760Sstevel@tonic-gate 			options.protocol &= ~SSH_PROTO_2;
10770Sstevel@tonic-gate 		}
10780Sstevel@tonic-gate #else
10790Sstevel@tonic-gate 		log("Disabling protocol version 2. Could not load host key");
10800Sstevel@tonic-gate 		options.protocol &= ~SSH_PROTO_2;
10810Sstevel@tonic-gate #endif /* GSSAPI */
10820Sstevel@tonic-gate 	}
10830Sstevel@tonic-gate 	if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
10840Sstevel@tonic-gate 		log("sshd: no hostkeys available -- exiting.");
10850Sstevel@tonic-gate 		exit(1);
10860Sstevel@tonic-gate 	}
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	/* Check certain values for sanity. */
10890Sstevel@tonic-gate 	if (options.protocol & SSH_PROTO_1) {
10900Sstevel@tonic-gate 		if (options.server_key_bits < 512 ||
10910Sstevel@tonic-gate 		    options.server_key_bits > 32768) {
10920Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Bad server key size.\n"));
10930Sstevel@tonic-gate 			exit(1);
10940Sstevel@tonic-gate 		}
10950Sstevel@tonic-gate 		/*
10960Sstevel@tonic-gate 		 * Check that server and host key lengths differ sufficiently. This
10970Sstevel@tonic-gate 		 * is necessary to make double encryption work with rsaref. Oh, I
10980Sstevel@tonic-gate 		 * hate software patents. I dont know if this can go? Niels
10990Sstevel@tonic-gate 		 */
11000Sstevel@tonic-gate 		if (options.server_key_bits >
11010Sstevel@tonic-gate 		    BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) -
11020Sstevel@tonic-gate 		    SSH_KEY_BITS_RESERVED && options.server_key_bits <
11030Sstevel@tonic-gate 		    BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
11040Sstevel@tonic-gate 		    SSH_KEY_BITS_RESERVED) {
11050Sstevel@tonic-gate 			options.server_key_bits =
11060Sstevel@tonic-gate 			    BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
11070Sstevel@tonic-gate 			    SSH_KEY_BITS_RESERVED;
11080Sstevel@tonic-gate 			debug("Forcing server key to %d bits to make it differ from host key.",
11090Sstevel@tonic-gate 			    options.server_key_bits);
11100Sstevel@tonic-gate 		}
11110Sstevel@tonic-gate 	}
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	/* Configuration looks good, so exit if in test mode. */
11140Sstevel@tonic-gate 	if (test_flag)
11150Sstevel@tonic-gate 		exit(0);
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	/*
11180Sstevel@tonic-gate 	 * Clear out any supplemental groups we may have inherited.  This
11190Sstevel@tonic-gate 	 * prevents inadvertent creation of files with bad modes (in the
11200Sstevel@tonic-gate 	 * portable version at least, it's certainly possible for PAM
11210Sstevel@tonic-gate 	 * to create a file, and we can't control the code in every
11220Sstevel@tonic-gate 	 * module which might be used).
11230Sstevel@tonic-gate 	 */
11240Sstevel@tonic-gate 	if (setgroups(0, NULL) < 0)
11250Sstevel@tonic-gate 		debug("setgroups() failed: %.200s", strerror(errno));
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	/* Initialize the log (it is reinitialized below in case we forked). */
11280Sstevel@tonic-gate 	if (debug_flag && !inetd_flag)
11290Sstevel@tonic-gate 		log_stderr = 1;
11300Sstevel@tonic-gate 	log_init(__progname, options.log_level, options.log_facility, log_stderr);
11310Sstevel@tonic-gate 
11328658SJan.Pechanec@Sun.COM 	/*
11338658SJan.Pechanec@Sun.COM 	 * Solaris 9 and systems upgraded from it may have the Ciphers option
11348658SJan.Pechanec@Sun.COM 	 * explicitly set to "aes128-cbc,blowfish-cbc,3des-cbc" in the
11358658SJan.Pechanec@Sun.COM 	 * sshd_config. Since the default server cipher list completely changed
11368658SJan.Pechanec@Sun.COM 	 * since then we rather notify the administator on startup. We do this
11378658SJan.Pechanec@Sun.COM 	 * check after log_init() so that the message goes to syslogd and not to
11388658SJan.Pechanec@Sun.COM 	 * stderr (unless the server is in the debug mode). Note that since
11398658SJan.Pechanec@Sun.COM 	 * Solaris 10 we no longer ship sshd_config with explicit settings for
11408658SJan.Pechanec@Sun.COM 	 * Ciphers or MACs. Do not try to augment the cipher list here since
11418658SJan.Pechanec@Sun.COM 	 * that might end up in a very confusing situation.
11428658SJan.Pechanec@Sun.COM 	 */
11438658SJan.Pechanec@Sun.COM #define	OLD_DEFAULT_CIPHERS_LIST "aes128-cbc,blowfish-cbc,3des-cbc"
11448658SJan.Pechanec@Sun.COM 	if (options.ciphers != NULL &&
11458658SJan.Pechanec@Sun.COM 	    strcmp(options.ciphers, OLD_DEFAULT_CIPHERS_LIST) == 0) {
11468658SJan.Pechanec@Sun.COM 		notice("Old default value \"%s\" for the \"Ciphers\" "
11478658SJan.Pechanec@Sun.COM 		    "option found in use. In general it is prudent to let "
11488658SJan.Pechanec@Sun.COM 		    "the server choose the defaults unless your environment "
11498658SJan.Pechanec@Sun.COM 		    "specifically needs an explicit setting. See "
11508658SJan.Pechanec@Sun.COM 		    "sshd_config(4) for more information.",
11518658SJan.Pechanec@Sun.COM 		    OLD_DEFAULT_CIPHERS_LIST);
11528658SJan.Pechanec@Sun.COM 	}
11538658SJan.Pechanec@Sun.COM 
11540Sstevel@tonic-gate #ifdef HAVE_BSM
11550Sstevel@tonic-gate 	(void) setauid(&auid);
11560Sstevel@tonic-gate #endif /* HAVE_BSM */
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	/*
11590Sstevel@tonic-gate 	 * If not in debugging mode, and not started from inetd, disconnect
11600Sstevel@tonic-gate 	 * from the controlling terminal, and fork.  The original process
11610Sstevel@tonic-gate 	 * exits.
11620Sstevel@tonic-gate 	 */
11630Sstevel@tonic-gate 	if (!(debug_flag || inetd_flag || no_daemon_flag)) {
11640Sstevel@tonic-gate #ifdef TIOCNOTTY
11650Sstevel@tonic-gate 		int fd;
11660Sstevel@tonic-gate #endif /* TIOCNOTTY */
11670Sstevel@tonic-gate 		if (daemon(0, 0) < 0)
11680Sstevel@tonic-gate 			fatal("daemon() failed: %.200s", strerror(errno));
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 		/* Disconnect from the controlling tty. */
11710Sstevel@tonic-gate #ifdef TIOCNOTTY
11720Sstevel@tonic-gate 		fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
11730Sstevel@tonic-gate 		if (fd >= 0) {
11740Sstevel@tonic-gate 			(void) ioctl(fd, TIOCNOTTY, NULL);
11750Sstevel@tonic-gate 			(void) close(fd);
11760Sstevel@tonic-gate 		}
11770Sstevel@tonic-gate #endif /* TIOCNOTTY */
11780Sstevel@tonic-gate 	}
11790Sstevel@tonic-gate 	/* Reinitialize the log (because of the fork above). */
11800Sstevel@tonic-gate 	log_init(__progname, options.log_level, options.log_facility, log_stderr);
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	/* Initialize the random number generator. */
11830Sstevel@tonic-gate 	arc4random_stir();
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	/* Chdir to the root directory so that the current disk can be
11860Sstevel@tonic-gate 	   unmounted if desired. */
11870Sstevel@tonic-gate 	(void) chdir("/");
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	/* ignore SIGPIPE */
11900Sstevel@tonic-gate 	(void) signal(SIGPIPE, SIG_IGN);
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	/* Start listening for a socket, unless started from inetd. */
11930Sstevel@tonic-gate 	if (inetd_flag) {
11940Sstevel@tonic-gate 		int s1;
11950Sstevel@tonic-gate 		s1 = dup(0);	/* Make sure descriptors 0, 1, and 2 are in use. */
11960Sstevel@tonic-gate 		(void) dup(s1);
11970Sstevel@tonic-gate 		sock_in = dup(0);
11980Sstevel@tonic-gate 		sock_out = dup(1);
11990Sstevel@tonic-gate 		startup_pipe = -1;
12007574SJan.Pechanec@Sun.COM 		/* we need this later for setting audit context */
12016481Spaulson 		newsock = sock_in;
12020Sstevel@tonic-gate 		/*
12030Sstevel@tonic-gate 		 * We intentionally do not close the descriptors 0, 1, and 2
12040Sstevel@tonic-gate 		 * as our code for setting the descriptors won\'t work if
12050Sstevel@tonic-gate 		 * ttyfd happens to be one of those.
12060Sstevel@tonic-gate 		 */
12070Sstevel@tonic-gate 		debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
12080Sstevel@tonic-gate 		if (options.protocol & SSH_PROTO_1)
12090Sstevel@tonic-gate 			generate_ephemeral_server_key();
12100Sstevel@tonic-gate 	} else {
12110Sstevel@tonic-gate 		for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
12120Sstevel@tonic-gate 			if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
12130Sstevel@tonic-gate 				continue;
12140Sstevel@tonic-gate 			if (num_listen_socks >= MAX_LISTEN_SOCKS)
12150Sstevel@tonic-gate 				fatal("Too many listen sockets. "
12160Sstevel@tonic-gate 				    "Enlarge MAX_LISTEN_SOCKS");
12170Sstevel@tonic-gate 			if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
12180Sstevel@tonic-gate 			    ntop, sizeof(ntop), strport, sizeof(strport),
12190Sstevel@tonic-gate 			    NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
12200Sstevel@tonic-gate 				error("getnameinfo failed");
12210Sstevel@tonic-gate 				continue;
12220Sstevel@tonic-gate 			}
12230Sstevel@tonic-gate 			/* Create socket for listening. */
12240Sstevel@tonic-gate 			listen_sock = socket(ai->ai_family, SOCK_STREAM, 0);
12250Sstevel@tonic-gate 			if (listen_sock < 0) {
12260Sstevel@tonic-gate 				/* kernel may not support ipv6 */
12270Sstevel@tonic-gate 				verbose("socket: %.100s", strerror(errno));
12280Sstevel@tonic-gate 				continue;
12290Sstevel@tonic-gate 			}
12300Sstevel@tonic-gate 			if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) {
12310Sstevel@tonic-gate 				error("listen_sock O_NONBLOCK: %s", strerror(errno));
12320Sstevel@tonic-gate 				(void) close(listen_sock);
12330Sstevel@tonic-gate 				continue;
12340Sstevel@tonic-gate 			}
12350Sstevel@tonic-gate 			/*
12360Sstevel@tonic-gate 			 * Set socket options.
12370Sstevel@tonic-gate 			 * Allow local port reuse in TIME_WAIT.
12380Sstevel@tonic-gate 			 */
12390Sstevel@tonic-gate 			if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
12400Sstevel@tonic-gate 			    &on, sizeof(on)) == -1)
12410Sstevel@tonic-gate 				error("setsockopt SO_REUSEADDR: %s", strerror(errno));
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 			debug("Bind to port %s on %s.", strport, ntop);
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 			/* Bind the socket to the desired port. */
12460Sstevel@tonic-gate 			if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
12470Sstevel@tonic-gate 				if (!ai->ai_next)
12480Sstevel@tonic-gate 				    error("Bind to port %s on %s failed: %.200s.",
12490Sstevel@tonic-gate 					    strport, ntop, strerror(errno));
12500Sstevel@tonic-gate 				(void) close(listen_sock);
12510Sstevel@tonic-gate 				continue;
12520Sstevel@tonic-gate 			}
12530Sstevel@tonic-gate 			listen_socks[num_listen_socks] = listen_sock;
12540Sstevel@tonic-gate 			num_listen_socks++;
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 			/* Start listening on the port. */
12570Sstevel@tonic-gate 			log("Server listening on %s port %s.", ntop, strport);
12580Sstevel@tonic-gate 			if (listen(listen_sock, 5) < 0)
12590Sstevel@tonic-gate 				fatal("listen: %.100s", strerror(errno));
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 		}
12620Sstevel@tonic-gate 		freeaddrinfo(options.listen_addrs);
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 		if (!num_listen_socks)
12650Sstevel@tonic-gate 			fatal("Cannot bind any address.");
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 		if (options.protocol & SSH_PROTO_1)
12680Sstevel@tonic-gate 			generate_ephemeral_server_key();
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 		/*
12710Sstevel@tonic-gate 		 * Arrange to restart on SIGHUP.  The handler needs
12720Sstevel@tonic-gate 		 * listen_sock.
12730Sstevel@tonic-gate 		 */
12740Sstevel@tonic-gate 		(void) signal(SIGHUP, sighup_handler);
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 		(void) signal(SIGTERM, sigterm_handler);
12770Sstevel@tonic-gate 		(void) signal(SIGQUIT, sigterm_handler);
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 		/* Arrange SIGCHLD to be caught. */
12800Sstevel@tonic-gate 		(void) signal(SIGCHLD, main_sigchld_handler);
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 		/* Write out the pid file after the sigterm handler is setup */
12830Sstevel@tonic-gate 		if (!debug_flag) {
12840Sstevel@tonic-gate 			/*
12850Sstevel@tonic-gate 			 * Record our pid in /var/run/sshd.pid to make it
12860Sstevel@tonic-gate 			 * easier to kill the correct sshd.  We don't want to
12870Sstevel@tonic-gate 			 * do this before the bind above because the bind will
12880Sstevel@tonic-gate 			 * fail if there already is a daemon, and this will
12890Sstevel@tonic-gate 			 * overwrite any old pid in the file.
12900Sstevel@tonic-gate 			 */
12910Sstevel@tonic-gate 			f = fopen(options.pid_file, "wb");
12920Sstevel@tonic-gate 			if (f) {
12930Sstevel@tonic-gate 				(void) fprintf(f, "%ld\n", (long) getpid());
12940Sstevel@tonic-gate 				(void) fclose(f);
12950Sstevel@tonic-gate 			}
12960Sstevel@tonic-gate 		}
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 		/* setup fd set for listen */
12990Sstevel@tonic-gate 		fdset = NULL;
13000Sstevel@tonic-gate 		maxfd = 0;
13010Sstevel@tonic-gate 		for (i = 0; i < num_listen_socks; i++)
13020Sstevel@tonic-gate 			if (listen_socks[i] > maxfd)
13030Sstevel@tonic-gate 				maxfd = listen_socks[i];
13040Sstevel@tonic-gate 		/* pipes connected to unauthenticated childs */
13050Sstevel@tonic-gate 		startup_pipes = xmalloc(options.max_startups * sizeof(int));
13060Sstevel@tonic-gate 		for (i = 0; i < options.max_startups; i++)
13070Sstevel@tonic-gate 			startup_pipes[i] = -1;
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 		/*
13100Sstevel@tonic-gate 		 * Stay listening for connections until the system crashes or
13110Sstevel@tonic-gate 		 * the daemon is killed with a signal.
13120Sstevel@tonic-gate 		 */
13130Sstevel@tonic-gate 		for (;;) {
13140Sstevel@tonic-gate 			if (received_sighup)
13150Sstevel@tonic-gate 				sighup_restart();
13160Sstevel@tonic-gate 			if (fdset != NULL)
13170Sstevel@tonic-gate 				xfree(fdset);
13180Sstevel@tonic-gate 			fdsetsz = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
13190Sstevel@tonic-gate 			fdset = (fd_set *)xmalloc(fdsetsz);
13200Sstevel@tonic-gate 			(void) memset(fdset, 0, fdsetsz);
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 			for (i = 0; i < num_listen_socks; i++)
13230Sstevel@tonic-gate 				FD_SET(listen_socks[i], fdset);
13240Sstevel@tonic-gate 			for (i = 0; i < options.max_startups; i++)
13250Sstevel@tonic-gate 				if (startup_pipes[i] != -1)
13260Sstevel@tonic-gate 					FD_SET(startup_pipes[i], fdset);
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 			/* Wait in select until there is a connection. */
13290Sstevel@tonic-gate 			ret = select(maxfd+1, fdset, NULL, NULL, NULL);
13300Sstevel@tonic-gate 			if (ret < 0 && errno != EINTR)
13310Sstevel@tonic-gate 				error("select: %.100s", strerror(errno));
13320Sstevel@tonic-gate 			if (received_sigterm) {
13330Sstevel@tonic-gate 				log("Received signal %d; terminating.",
13340Sstevel@tonic-gate 				    (int) received_sigterm);
13350Sstevel@tonic-gate 				close_listen_socks();
13360Sstevel@tonic-gate 				(void) unlink(options.pid_file);
13370Sstevel@tonic-gate 				exit(255);
13380Sstevel@tonic-gate 			}
13390Sstevel@tonic-gate 			if (key_used && key_do_regen) {
13400Sstevel@tonic-gate 				generate_ephemeral_server_key();
13410Sstevel@tonic-gate 				key_used = 0;
13420Sstevel@tonic-gate 				key_do_regen = 0;
13430Sstevel@tonic-gate 			}
13440Sstevel@tonic-gate 			if (ret < 0)
13450Sstevel@tonic-gate 				continue;
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 			for (i = 0; i < options.max_startups; i++)
13480Sstevel@tonic-gate 				if (startup_pipes[i] != -1 &&
13490Sstevel@tonic-gate 				    FD_ISSET(startup_pipes[i], fdset)) {
13500Sstevel@tonic-gate 					/*
13510Sstevel@tonic-gate 					 * the read end of the pipe is ready
13520Sstevel@tonic-gate 					 * if the child has closed the pipe
13530Sstevel@tonic-gate 					 * after successful authentication
13540Sstevel@tonic-gate 					 * or if the child has died
13550Sstevel@tonic-gate 					 */
13560Sstevel@tonic-gate 					(void) close(startup_pipes[i]);
13570Sstevel@tonic-gate 					startup_pipes[i] = -1;
13580Sstevel@tonic-gate 					startups--;
13590Sstevel@tonic-gate 				}
13600Sstevel@tonic-gate 			for (i = 0; i < num_listen_socks; i++) {
13610Sstevel@tonic-gate 				if (!FD_ISSET(listen_socks[i], fdset))
13620Sstevel@tonic-gate 					continue;
13630Sstevel@tonic-gate 				fromlen = sizeof(from);
13640Sstevel@tonic-gate 				newsock = accept(listen_socks[i], (struct sockaddr *)&from,
13650Sstevel@tonic-gate 				    &fromlen);
13660Sstevel@tonic-gate 				if (newsock < 0) {
13670Sstevel@tonic-gate 					if (errno != EINTR && errno != EWOULDBLOCK)
13680Sstevel@tonic-gate 						error("accept: %.100s", strerror(errno));
13690Sstevel@tonic-gate 					continue;
13700Sstevel@tonic-gate 				}
13710Sstevel@tonic-gate 				if (fcntl(newsock, F_SETFL, 0) < 0) {
13720Sstevel@tonic-gate 					error("newsock del O_NONBLOCK: %s", strerror(errno));
13730Sstevel@tonic-gate 					(void) close(newsock);
13740Sstevel@tonic-gate 					continue;
13750Sstevel@tonic-gate 				}
13760Sstevel@tonic-gate 				if (drop_connection(startups) == 1) {
13770Sstevel@tonic-gate 					debug("drop connection #%d", startups);
13780Sstevel@tonic-gate 					(void) close(newsock);
13790Sstevel@tonic-gate 					continue;
13800Sstevel@tonic-gate 				}
13810Sstevel@tonic-gate 				if (pipe(startup_p) == -1) {
13820Sstevel@tonic-gate 					(void) close(newsock);
13830Sstevel@tonic-gate 					continue;
13840Sstevel@tonic-gate 				}
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 				for (j = 0; j < options.max_startups; j++)
13870Sstevel@tonic-gate 					if (startup_pipes[j] == -1) {
13880Sstevel@tonic-gate 						startup_pipes[j] = startup_p[0];
13890Sstevel@tonic-gate 						if (maxfd < startup_p[0])
13900Sstevel@tonic-gate 							maxfd = startup_p[0];
13910Sstevel@tonic-gate 						startups++;
13920Sstevel@tonic-gate 						break;
13930Sstevel@tonic-gate 					}
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 				/*
13960Sstevel@tonic-gate 				 * Got connection.  Fork a child to handle it, unless
13970Sstevel@tonic-gate 				 * we are in debugging mode.
13980Sstevel@tonic-gate 				 */
13990Sstevel@tonic-gate 				if (debug_flag) {
14000Sstevel@tonic-gate 					/*
14010Sstevel@tonic-gate 					 * In debugging mode.  Close the listening
14020Sstevel@tonic-gate 					 * socket, and start processing the
14030Sstevel@tonic-gate 					 * connection without forking.
14040Sstevel@tonic-gate 					 */
14050Sstevel@tonic-gate 					debug("Server will not fork when running in debugging mode.");
14060Sstevel@tonic-gate 					close_listen_socks();
14070Sstevel@tonic-gate 					sock_in = newsock;
14080Sstevel@tonic-gate 					sock_out = newsock;
14090Sstevel@tonic-gate 					startup_pipe = -1;
14100Sstevel@tonic-gate 					pid = getpid();
14110Sstevel@tonic-gate 					break;
14120Sstevel@tonic-gate 				} else {
14130Sstevel@tonic-gate 					/*
14140Sstevel@tonic-gate 					 * Normal production daemon.  Fork, and have
14150Sstevel@tonic-gate 					 * the child process the connection. The
14160Sstevel@tonic-gate 					 * parent continues listening.
14170Sstevel@tonic-gate 					 */
14180Sstevel@tonic-gate #ifdef HAVE_SOLARIS_CONTRACTS
14190Sstevel@tonic-gate 					/*
14200Sstevel@tonic-gate 					 * Setup Solaris contract template so
14210Sstevel@tonic-gate 					 * the child process is in a different
14220Sstevel@tonic-gate 					 * process contract than the parent;
14230Sstevel@tonic-gate 					 * prevents established connections from
14240Sstevel@tonic-gate 					 * being killed when the sshd master
14250Sstevel@tonic-gate 					 * listener service is stopped.
14260Sstevel@tonic-gate 					 */
14270Sstevel@tonic-gate 					contracts_pre_fork();
14280Sstevel@tonic-gate #endif /* HAVE_SOLARIS_CONTRACTS */
14290Sstevel@tonic-gate 					if ((pid = fork()) == 0) {
14300Sstevel@tonic-gate 						/*
14310Sstevel@tonic-gate 						 * Child.  Close the listening and max_startup
14320Sstevel@tonic-gate 						 * sockets.  Start using the accepted socket.
14330Sstevel@tonic-gate 						 * Reinitialize logging (since our pid has
14340Sstevel@tonic-gate 						 * changed).  We break out of the loop to handle
14350Sstevel@tonic-gate 						 * the connection.
14360Sstevel@tonic-gate 						 */
14370Sstevel@tonic-gate #ifdef HAVE_SOLARIS_CONTRACTS
14380Sstevel@tonic-gate 						contracts_post_fork_child();
14390Sstevel@tonic-gate #endif /* HAVE_SOLARIS_CONTRACTS */
14405562Sjp161948 						xfree(fdset);
14410Sstevel@tonic-gate 						startup_pipe = startup_p[1];
14420Sstevel@tonic-gate 						close_startup_pipes();
14430Sstevel@tonic-gate 						close_listen_socks();
14440Sstevel@tonic-gate 						sock_in = newsock;
14450Sstevel@tonic-gate 						sock_out = newsock;
14460Sstevel@tonic-gate 						log_init(__progname, options.log_level, options.log_facility, log_stderr);
14470Sstevel@tonic-gate 						break;
14480Sstevel@tonic-gate 					}
14490Sstevel@tonic-gate 
145011802SJan.Pechanec@Sun.COM 					/* Parent.  Stay in the loop. */
145111802SJan.Pechanec@Sun.COM 					if (pid < 0)
145211802SJan.Pechanec@Sun.COM 						error("fork: %.100s", strerror(errno));
145311802SJan.Pechanec@Sun.COM 					else
145411802SJan.Pechanec@Sun.COM 						debug("Forked child %ld.", (long)pid);
145511802SJan.Pechanec@Sun.COM 
14560Sstevel@tonic-gate #ifdef HAVE_SOLARIS_CONTRACTS
14570Sstevel@tonic-gate 					contracts_post_fork_parent((pid > 0));
14580Sstevel@tonic-gate #endif /* HAVE_SOLARIS_CONTRACTS */
14590Sstevel@tonic-gate 				}
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 				(void) close(startup_p[1]);
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 				/* Mark that the key has been used (it was "given" to the child). */
14640Sstevel@tonic-gate 				if ((options.protocol & SSH_PROTO_1) &&
14650Sstevel@tonic-gate 				    key_used == 0) {
14660Sstevel@tonic-gate 					/* Schedule server key regeneration alarm. */
14670Sstevel@tonic-gate 					(void) signal(SIGALRM, key_regeneration_alarm);
14680Sstevel@tonic-gate 					(void) alarm(options.key_regeneration_time);
14690Sstevel@tonic-gate 					key_used = 1;
14700Sstevel@tonic-gate 				}
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 				arc4random_stir();
14730Sstevel@tonic-gate 
14747574SJan.Pechanec@Sun.COM 				/*
14757574SJan.Pechanec@Sun.COM 				 * Close the accepted socket since the child
14767574SJan.Pechanec@Sun.COM 				 * will now take care of the new connection.
14777574SJan.Pechanec@Sun.COM 				 */
14780Sstevel@tonic-gate 				(void) close(newsock);
14790Sstevel@tonic-gate 			}
14800Sstevel@tonic-gate 			/* child process check (or debug mode) */
14810Sstevel@tonic-gate 			if (num_listen_socks < 0)
14820Sstevel@tonic-gate 				break;
14830Sstevel@tonic-gate 		}
14840Sstevel@tonic-gate 	}
14850Sstevel@tonic-gate 
14867574SJan.Pechanec@Sun.COM 	/*
14877574SJan.Pechanec@Sun.COM 	 * This is the child processing a new connection, the SSH master process
14887574SJan.Pechanec@Sun.COM 	 * stays in the ( ; ; ) loop above.
14897574SJan.Pechanec@Sun.COM 	 */
14900Sstevel@tonic-gate #ifdef HAVE_BSM
14910Sstevel@tonic-gate 	audit_sshd_settid(newsock);
14920Sstevel@tonic-gate #endif
14930Sstevel@tonic-gate 	/*
14940Sstevel@tonic-gate 	 * Create a new session and process group since the 4.4BSD
14950Sstevel@tonic-gate 	 * setlogin() affects the entire process group.  We don't
14960Sstevel@tonic-gate 	 * want the child to be able to affect the parent.
14970Sstevel@tonic-gate 	 */
14980Sstevel@tonic-gate #if 0
14990Sstevel@tonic-gate 	/* XXX: this breaks Solaris */
15000Sstevel@tonic-gate 	if (!debug_flag && !inetd_flag && setsid() < 0)
15010Sstevel@tonic-gate 		error("setsid: %.100s", strerror(errno));
15020Sstevel@tonic-gate #endif
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	/*
15050Sstevel@tonic-gate 	 * Disable the key regeneration alarm.  We will not regenerate the
15060Sstevel@tonic-gate 	 * key since we are no longer in a position to give it to anyone. We
15070Sstevel@tonic-gate 	 * will not restart on SIGHUP since it no longer makes sense.
15080Sstevel@tonic-gate 	 */
15090Sstevel@tonic-gate 	(void) alarm(0);
15100Sstevel@tonic-gate 	(void) signal(SIGALRM, SIG_DFL);
15110Sstevel@tonic-gate 	(void) signal(SIGHUP, SIG_DFL);
15120Sstevel@tonic-gate 	(void) signal(SIGTERM, SIG_DFL);
15130Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_DFL);
15140Sstevel@tonic-gate 	(void) signal(SIGCHLD, SIG_DFL);
15150Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_DFL);
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	/* Set keepalives if requested. */
15180Sstevel@tonic-gate 	if (options.keepalives &&
15190Sstevel@tonic-gate 	    setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on,
15200Sstevel@tonic-gate 	    sizeof(on)) < 0)
15210Sstevel@tonic-gate 		debug2("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	/*
15240Sstevel@tonic-gate 	 * Register our connection.  This turns encryption off because we do
15250Sstevel@tonic-gate 	 * not have a key.
15260Sstevel@tonic-gate 	 */
15270Sstevel@tonic-gate 	packet_set_connection(sock_in, sock_out);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	remote_port = get_remote_port();
15300Sstevel@tonic-gate 	remote_ip = get_remote_ipaddr();
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate #ifdef LIBWRAP
15330Sstevel@tonic-gate 	/* Check whether logins are denied from this host. */
15340Sstevel@tonic-gate 	{
15350Sstevel@tonic-gate 		struct request_info req;
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 		(void) request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, 0);
15380Sstevel@tonic-gate 		fromhost(&req);
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 		if (!hosts_access(&req)) {
15410Sstevel@tonic-gate 			debug("Connection refused by tcp wrapper");
15420Sstevel@tonic-gate 			refuse(&req);
15430Sstevel@tonic-gate 			/* NOTREACHED */
15440Sstevel@tonic-gate 			fatal("libwrap refuse returns");
15450Sstevel@tonic-gate 		}
15460Sstevel@tonic-gate 	}
15470Sstevel@tonic-gate #endif /* LIBWRAP */
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	/* Log the connection. */
15500Sstevel@tonic-gate 	verbose("Connection from %.500s port %d", remote_ip, remote_port);
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	sshd_exchange_identification(sock_in, sock_out);
15530Sstevel@tonic-gate 	/*
15540Sstevel@tonic-gate 	 * Check that the connection comes from a privileged port.
15550Sstevel@tonic-gate 	 * Rhosts-Authentication only makes sense from privileged
15560Sstevel@tonic-gate 	 * programs.  Of course, if the intruder has root access on his local
15570Sstevel@tonic-gate 	 * machine, he can connect from any port.  So do not use these
15580Sstevel@tonic-gate 	 * authentication methods from machines that you do not trust.
15590Sstevel@tonic-gate 	 */
15600Sstevel@tonic-gate 	if (options.rhosts_authentication &&
15610Sstevel@tonic-gate 	    (remote_port >= IPPORT_RESERVED ||
15620Sstevel@tonic-gate 	    remote_port < IPPORT_RESERVED / 2)) {
15630Sstevel@tonic-gate 		debug("Rhosts Authentication disabled, "
15640Sstevel@tonic-gate 		    "originating port %d not trusted.", remote_port);
15650Sstevel@tonic-gate 		options.rhosts_authentication = 0;
15660Sstevel@tonic-gate 	}
15670Sstevel@tonic-gate #if defined(KRB4) && !defined(KRB5)
15680Sstevel@tonic-gate 	if (!packet_connection_is_ipv4() &&
15690Sstevel@tonic-gate 	    options.kerberos_authentication) {
15700Sstevel@tonic-gate 		debug("Kerberos Authentication disabled, only available for IPv4.");
15710Sstevel@tonic-gate 		options.kerberos_authentication = 0;
15720Sstevel@tonic-gate 	}
15730Sstevel@tonic-gate #endif /* KRB4 && !KRB5 */
15740Sstevel@tonic-gate #ifdef AFS
15750Sstevel@tonic-gate 	/* If machine has AFS, set process authentication group. */
15760Sstevel@tonic-gate 	if (k_hasafs()) {
15770Sstevel@tonic-gate 		k_setpag();
15780Sstevel@tonic-gate 		k_unlog();
15790Sstevel@tonic-gate 	}
15800Sstevel@tonic-gate #endif /* AFS */
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 	packet_set_nonblocking();
15830Sstevel@tonic-gate 
15847574SJan.Pechanec@Sun.COM 	/*
15857574SJan.Pechanec@Sun.COM 	 * Start the monitor. That way both processes will have their own
15867574SJan.Pechanec@Sun.COM 	 * PKCS#11 sessions. See the PKCS#11 standard for more information on
15877574SJan.Pechanec@Sun.COM 	 * fork safety and packet.c for information about forking with the
15887574SJan.Pechanec@Sun.COM 	 * engine.
158910515SJan.Pechanec@Sun.COM 	 *
159010515SJan.Pechanec@Sun.COM 	 * Note that the monitor stays in the function while the child is the
159110515SJan.Pechanec@Sun.COM 	 * only one that returns.
15927574SJan.Pechanec@Sun.COM 	 */
15937574SJan.Pechanec@Sun.COM 	altprivsep_start_and_do_monitor(options.use_openssl_engine,
15947574SJan.Pechanec@Sun.COM 	    inetd_flag, newsock, startup_pipe);
15957574SJan.Pechanec@Sun.COM 
15967574SJan.Pechanec@Sun.COM 	/*
159710515SJan.Pechanec@Sun.COM 	 * We don't want to listen forever unless the other side successfully
159810515SJan.Pechanec@Sun.COM 	 * authenticates itself. So we set up an alarm which is cleared after
159910515SJan.Pechanec@Sun.COM 	 * successful authentication. A limit of zero indicates no limit. Note
160010515SJan.Pechanec@Sun.COM 	 * that we don't set the alarm in debugging mode; it is just annoying to
160110515SJan.Pechanec@Sun.COM 	 * have the server exit just when you are about to discover the bug.
160210515SJan.Pechanec@Sun.COM 	 */
160310515SJan.Pechanec@Sun.COM 	(void) signal(SIGALRM, grace_alarm_handler);
160410515SJan.Pechanec@Sun.COM 	if (!debug_flag)
160510515SJan.Pechanec@Sun.COM 		(void) alarm(options.login_grace_time);
160610515SJan.Pechanec@Sun.COM 
160710515SJan.Pechanec@Sun.COM 	/*
16087574SJan.Pechanec@Sun.COM 	 * The child is about to start the first key exchange while the monitor
16097574SJan.Pechanec@Sun.COM 	 * stays in altprivsep_start_and_do_monitor() function.
16107574SJan.Pechanec@Sun.COM 	 */
16117574SJan.Pechanec@Sun.COM 	(void) pkcs11_engine_load(options.use_openssl_engine);
16127574SJan.Pechanec@Sun.COM 
16130Sstevel@tonic-gate 	/* perform the key exchange */
16140Sstevel@tonic-gate 	/* authenticate user and start session */
16150Sstevel@tonic-gate 	if (compat20) {
16160Sstevel@tonic-gate 		do_ssh2_kex();
16170Sstevel@tonic-gate 		authctxt = do_authentication2();
16180Sstevel@tonic-gate 	} else {
16190Sstevel@tonic-gate 		do_ssh1_kex();
16200Sstevel@tonic-gate 		authctxt = do_authentication();
16210Sstevel@tonic-gate 	}
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	/* Authentication complete */
16240Sstevel@tonic-gate 	(void) alarm(0);
16257574SJan.Pechanec@Sun.COM 	/* we no longer need an alarm handler */
16267574SJan.Pechanec@Sun.COM 	(void) signal(SIGALRM, SIG_DFL);
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	if (startup_pipe != -1) {
16290Sstevel@tonic-gate 		(void) close(startup_pipe);
16300Sstevel@tonic-gate 		startup_pipe = -1;
16310Sstevel@tonic-gate 	}
16320Sstevel@tonic-gate 
16337574SJan.Pechanec@Sun.COM 	/* ALTPRIVSEP Child */
16340Sstevel@tonic-gate 
16357574SJan.Pechanec@Sun.COM 	/*
16367574SJan.Pechanec@Sun.COM 	 * Drop privileges, access to privileged resources.
16377574SJan.Pechanec@Sun.COM 	 *
16387574SJan.Pechanec@Sun.COM 	 * Destroy private host keys, if any.
16397574SJan.Pechanec@Sun.COM 	 *
16407574SJan.Pechanec@Sun.COM 	 * No need to release any GSS credentials -- sshd only acquires
16417574SJan.Pechanec@Sun.COM 	 * creds to determine what mechs it can negotiate then releases
16427574SJan.Pechanec@Sun.COM 	 * them right away and uses GSS_C_NO_CREDENTIAL to accept
16437574SJan.Pechanec@Sun.COM 	 * contexts.
16447574SJan.Pechanec@Sun.COM 	 */
16457574SJan.Pechanec@Sun.COM 	debug2("Unprivileged server process dropping privileges");
16469139SJan.Pechanec@Sun.COM 	permanently_set_uid(authctxt->pw, options.chroot_directory);
16477574SJan.Pechanec@Sun.COM 	destroy_sensitive_data();
16489139SJan.Pechanec@Sun.COM 
16499139SJan.Pechanec@Sun.COM 	/* Just another safety check. */
16509139SJan.Pechanec@Sun.COM 	if (getuid() != authctxt->pw->pw_uid ||
16519139SJan.Pechanec@Sun.COM 	    geteuid() != authctxt->pw->pw_uid) {
16529139SJan.Pechanec@Sun.COM 		fatal("Failed to set uids to %u.", (u_int)authctxt->pw->pw_uid);
16539139SJan.Pechanec@Sun.COM 	}
16549139SJan.Pechanec@Sun.COM 
16557574SJan.Pechanec@Sun.COM 	ssh_gssapi_server_mechs(NULL); /* release cached mechs list */
16567574SJan.Pechanec@Sun.COM 	packet_set_server();
16570Sstevel@tonic-gate 
16587574SJan.Pechanec@Sun.COM 	/* now send the authentication context to the monitor */
16597574SJan.Pechanec@Sun.COM 	altprivsep_send_auth_context(authctxt);
16607574SJan.Pechanec@Sun.COM 
16617574SJan.Pechanec@Sun.COM 	mpipe = altprivsep_get_pipe_fd();
16627574SJan.Pechanec@Sun.COM 	if (fcntl(mpipe, F_SETFL, O_NONBLOCK) < 0)
16637574SJan.Pechanec@Sun.COM 		error("fcntl O_NONBLOCK: %.100s", strerror(errno));
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate #ifdef HAVE_BSM
16667574SJan.Pechanec@Sun.COM 	fatal_remove_cleanup(
16677574SJan.Pechanec@Sun.COM 		(void (*)(void *))audit_failed_login_cleanup,
16687574SJan.Pechanec@Sun.COM 		(void *)authctxt);
16690Sstevel@tonic-gate #endif /* HAVE_BSM */
16700Sstevel@tonic-gate 
16717574SJan.Pechanec@Sun.COM 	if (compat20) {
16727574SJan.Pechanec@Sun.COM 		debug3("setting handler to forward re-key packets to the monitor");
16737574SJan.Pechanec@Sun.COM 		dispatch_range(SSH2_MSG_KEXINIT, SSH2_MSG_TRANSPORT_MAX,
16747574SJan.Pechanec@Sun.COM 			&altprivsep_rekey);
16757574SJan.Pechanec@Sun.COM 	}
16760Sstevel@tonic-gate 
16777574SJan.Pechanec@Sun.COM 	/* Logged-in session. */
16787574SJan.Pechanec@Sun.COM 	do_authenticated(authctxt);
16790Sstevel@tonic-gate 
16807574SJan.Pechanec@Sun.COM 	/* The connection has been terminated. */
16817574SJan.Pechanec@Sun.COM 	verbose("Closing connection to %.100s", remote_ip);
16820Sstevel@tonic-gate 
16837574SJan.Pechanec@Sun.COM 	packet_close();
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate #ifdef USE_PAM
16867574SJan.Pechanec@Sun.COM 	finish_pam(authctxt);
16870Sstevel@tonic-gate #endif /* USE_PAM */
16880Sstevel@tonic-gate 
16897574SJan.Pechanec@Sun.COM 	return (0);
16900Sstevel@tonic-gate }
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate /*
16930Sstevel@tonic-gate  * Decrypt session_key_int using our private server key and private host key
16940Sstevel@tonic-gate  * (key with larger modulus first).
16950Sstevel@tonic-gate  */
16960Sstevel@tonic-gate int
ssh1_session_key(BIGNUM * session_key_int)16970Sstevel@tonic-gate ssh1_session_key(BIGNUM *session_key_int)
16980Sstevel@tonic-gate {
16990Sstevel@tonic-gate 	int rsafail = 0;
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate 	if (BN_cmp(sensitive_data.server_key->rsa->n, sensitive_data.ssh1_host_key->rsa->n) > 0) {
17020Sstevel@tonic-gate 		/* Server key has bigger modulus. */
17030Sstevel@tonic-gate 		if (BN_num_bits(sensitive_data.server_key->rsa->n) <
17040Sstevel@tonic-gate 		    BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
17050Sstevel@tonic-gate 			fatal("do_connection: %s: server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
17060Sstevel@tonic-gate 			    get_remote_ipaddr(),
17070Sstevel@tonic-gate 			    BN_num_bits(sensitive_data.server_key->rsa->n),
17080Sstevel@tonic-gate 			    BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
17090Sstevel@tonic-gate 			    SSH_KEY_BITS_RESERVED);
17100Sstevel@tonic-gate 		}
17110Sstevel@tonic-gate 		if (rsa_private_decrypt(session_key_int, session_key_int,
17120Sstevel@tonic-gate 		    sensitive_data.server_key->rsa) <= 0)
17130Sstevel@tonic-gate 			rsafail++;
17140Sstevel@tonic-gate 		if (rsa_private_decrypt(session_key_int, session_key_int,
17150Sstevel@tonic-gate 		    sensitive_data.ssh1_host_key->rsa) <= 0)
17160Sstevel@tonic-gate 			rsafail++;
17170Sstevel@tonic-gate 	} else {
17180Sstevel@tonic-gate 		/* Host key has bigger modulus (or they are equal). */
17190Sstevel@tonic-gate 		if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
17200Sstevel@tonic-gate 		    BN_num_bits(sensitive_data.server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
17210Sstevel@tonic-gate 			fatal("do_connection: %s: host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
17220Sstevel@tonic-gate 			    get_remote_ipaddr(),
17230Sstevel@tonic-gate 			    BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
17240Sstevel@tonic-gate 			    BN_num_bits(sensitive_data.server_key->rsa->n),
17250Sstevel@tonic-gate 			    SSH_KEY_BITS_RESERVED);
17260Sstevel@tonic-gate 		}
17270Sstevel@tonic-gate 		if (rsa_private_decrypt(session_key_int, session_key_int,
17280Sstevel@tonic-gate 		    sensitive_data.ssh1_host_key->rsa) < 0)
17290Sstevel@tonic-gate 			rsafail++;
17300Sstevel@tonic-gate 		if (rsa_private_decrypt(session_key_int, session_key_int,
17310Sstevel@tonic-gate 		    sensitive_data.server_key->rsa) < 0)
17320Sstevel@tonic-gate 			rsafail++;
17330Sstevel@tonic-gate 	}
17340Sstevel@tonic-gate 	return (rsafail);
17350Sstevel@tonic-gate }
17360Sstevel@tonic-gate /*
17370Sstevel@tonic-gate  * SSH1 key exchange
17380Sstevel@tonic-gate  */
17390Sstevel@tonic-gate static void
do_ssh1_kex(void)17400Sstevel@tonic-gate do_ssh1_kex(void)
17410Sstevel@tonic-gate {
17420Sstevel@tonic-gate 	int i, len;
17430Sstevel@tonic-gate 	int rsafail = 0;
17440Sstevel@tonic-gate 	BIGNUM *session_key_int;
17450Sstevel@tonic-gate 	u_char session_key[SSH_SESSION_KEY_LENGTH];
17460Sstevel@tonic-gate 	u_char cookie[8];
17470Sstevel@tonic-gate 	u_int cipher_type, auth_mask, protocol_flags;
17480Sstevel@tonic-gate 	u_int32_t rnd = 0;
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	/*
17510Sstevel@tonic-gate 	 * Generate check bytes that the client must send back in the user
17520Sstevel@tonic-gate 	 * packet in order for it to be accepted; this is used to defy ip
17530Sstevel@tonic-gate 	 * spoofing attacks.  Note that this only works against somebody
17540Sstevel@tonic-gate 	 * doing IP spoofing from a remote machine; any machine on the local
17550Sstevel@tonic-gate 	 * network can still see outgoing packets and catch the random
17560Sstevel@tonic-gate 	 * cookie.  This only affects rhosts authentication, and this is one
17570Sstevel@tonic-gate 	 * of the reasons why it is inherently insecure.
17580Sstevel@tonic-gate 	 */
17590Sstevel@tonic-gate 	for (i = 0; i < 8; i++) {
17600Sstevel@tonic-gate 		if (i % 4 == 0)
17610Sstevel@tonic-gate 			rnd = arc4random();
17620Sstevel@tonic-gate 		cookie[i] = rnd & 0xff;
17630Sstevel@tonic-gate 		rnd >>= 8;
17640Sstevel@tonic-gate 	}
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 	/*
17670Sstevel@tonic-gate 	 * Send our public key.  We include in the packet 64 bits of random
17680Sstevel@tonic-gate 	 * data that must be matched in the reply in order to prevent IP
17690Sstevel@tonic-gate 	 * spoofing.
17700Sstevel@tonic-gate 	 */
17710Sstevel@tonic-gate 	packet_start(SSH_SMSG_PUBLIC_KEY);
17720Sstevel@tonic-gate 	for (i = 0; i < 8; i++)
17730Sstevel@tonic-gate 		packet_put_char(cookie[i]);
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	/* Store our public server RSA key. */
17760Sstevel@tonic-gate 	packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
17770Sstevel@tonic-gate 	packet_put_bignum(sensitive_data.server_key->rsa->e);
17780Sstevel@tonic-gate 	packet_put_bignum(sensitive_data.server_key->rsa->n);
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 	/* Store our public host RSA key. */
17810Sstevel@tonic-gate 	packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
17820Sstevel@tonic-gate 	packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
17830Sstevel@tonic-gate 	packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 	/* Put protocol flags. */
17860Sstevel@tonic-gate 	packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 	/* Declare which ciphers we support. */
17890Sstevel@tonic-gate 	packet_put_int(cipher_mask_ssh1(0));
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 	/* Declare supported authentication types. */
17920Sstevel@tonic-gate 	auth_mask = 0;
17930Sstevel@tonic-gate 	if (options.rhosts_authentication)
17940Sstevel@tonic-gate 		auth_mask |= 1 << SSH_AUTH_RHOSTS;
17950Sstevel@tonic-gate 	if (options.rhosts_rsa_authentication)
17960Sstevel@tonic-gate 		auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
17970Sstevel@tonic-gate 	if (options.rsa_authentication)
17980Sstevel@tonic-gate 		auth_mask |= 1 << SSH_AUTH_RSA;
17990Sstevel@tonic-gate #if defined(KRB4) || defined(KRB5)
18000Sstevel@tonic-gate 	if (options.kerberos_authentication)
18010Sstevel@tonic-gate 		auth_mask |= 1 << SSH_AUTH_KERBEROS;
18020Sstevel@tonic-gate #endif
18030Sstevel@tonic-gate #if defined(AFS) || defined(KRB5)
18040Sstevel@tonic-gate 	if (options.kerberos_tgt_passing)
18050Sstevel@tonic-gate 		auth_mask |= 1 << SSH_PASS_KERBEROS_TGT;
18060Sstevel@tonic-gate #endif
18070Sstevel@tonic-gate #ifdef AFS
18080Sstevel@tonic-gate 	if (options.afs_token_passing)
18090Sstevel@tonic-gate 		auth_mask |= 1 << SSH_PASS_AFS_TOKEN;
18100Sstevel@tonic-gate #endif
18110Sstevel@tonic-gate 	if (options.challenge_response_authentication == 1)
18120Sstevel@tonic-gate 		auth_mask |= 1 << SSH_AUTH_TIS;
18130Sstevel@tonic-gate 	if (options.password_authentication)
18140Sstevel@tonic-gate 		auth_mask |= 1 << SSH_AUTH_PASSWORD;
18150Sstevel@tonic-gate 	packet_put_int(auth_mask);
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	/* Send the packet and wait for it to be sent. */
18180Sstevel@tonic-gate 	packet_send();
18190Sstevel@tonic-gate 	packet_write_wait();
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 	debug("Sent %d bit server key and %d bit host key.",
18220Sstevel@tonic-gate 	    BN_num_bits(sensitive_data.server_key->rsa->n),
18230Sstevel@tonic-gate 	    BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	/* Read clients reply (cipher type and session key). */
18260Sstevel@tonic-gate 	packet_read_expect(SSH_CMSG_SESSION_KEY);
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 	/* Get cipher type and check whether we accept this. */
18290Sstevel@tonic-gate 	cipher_type = packet_get_char();
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 	if (!(cipher_mask_ssh1(0) & (1 << cipher_type))) {
18320Sstevel@tonic-gate 		packet_disconnect("Warning: client selects unsupported cipher.");
18330Sstevel@tonic-gate 	}
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	/* Get check bytes from the packet.  These must match those we
18360Sstevel@tonic-gate 	   sent earlier with the public key packet. */
18370Sstevel@tonic-gate 	for (i = 0; i < 8; i++) {
18380Sstevel@tonic-gate 		if (cookie[i] != packet_get_char()) {
18390Sstevel@tonic-gate 			packet_disconnect("IP Spoofing check bytes do not match.");
18400Sstevel@tonic-gate 		}
18410Sstevel@tonic-gate 	}
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 	debug("Encryption type: %.200s", cipher_name(cipher_type));
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 	/* Get the encrypted integer. */
18460Sstevel@tonic-gate 	if ((session_key_int = BN_new()) == NULL)
18470Sstevel@tonic-gate 		fatal("do_ssh1_kex: BN_new failed");
18480Sstevel@tonic-gate 	packet_get_bignum(session_key_int);
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	protocol_flags = packet_get_int();
18510Sstevel@tonic-gate 	packet_set_protocol_flags(protocol_flags);
18520Sstevel@tonic-gate 	packet_check_eom();
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 	/* Decrypt session_key_int using host/server keys */
18555562Sjp161948 	rsafail = ssh1_session_key(session_key_int);
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 	/*
18580Sstevel@tonic-gate 	 * Extract session key from the decrypted integer.  The key is in the
18590Sstevel@tonic-gate 	 * least significant 256 bits of the integer; the first byte of the
18600Sstevel@tonic-gate 	 * key is in the highest bits.
18610Sstevel@tonic-gate 	 */
18620Sstevel@tonic-gate 	if (!rsafail) {
18630Sstevel@tonic-gate 		(void) BN_mask_bits(session_key_int, sizeof(session_key) * 8);
18640Sstevel@tonic-gate 		len = BN_num_bytes(session_key_int);
18650Sstevel@tonic-gate 		if (len < 0 || len > sizeof(session_key)) {
18660Sstevel@tonic-gate 			error("do_connection: bad session key len from %s: "
18670Sstevel@tonic-gate 			    "session_key_int %d > sizeof(session_key) %lu",
18680Sstevel@tonic-gate 			    get_remote_ipaddr(), len, (u_long)sizeof(session_key));
18690Sstevel@tonic-gate 			rsafail++;
18700Sstevel@tonic-gate 		} else {
18710Sstevel@tonic-gate 			(void) memset(session_key, 0, sizeof(session_key));
18720Sstevel@tonic-gate 			(void) BN_bn2bin(session_key_int,
18730Sstevel@tonic-gate 			    session_key + sizeof(session_key) - len);
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 			compute_session_id(session_id, cookie,
18760Sstevel@tonic-gate 			    sensitive_data.ssh1_host_key->rsa->n,
18770Sstevel@tonic-gate 			    sensitive_data.server_key->rsa->n);
18780Sstevel@tonic-gate 			/*
18790Sstevel@tonic-gate 			 * Xor the first 16 bytes of the session key with the
18800Sstevel@tonic-gate 			 * session id.
18810Sstevel@tonic-gate 			 */
18820Sstevel@tonic-gate 			for (i = 0; i < 16; i++)
18830Sstevel@tonic-gate 				session_key[i] ^= session_id[i];
18840Sstevel@tonic-gate 		}
18850Sstevel@tonic-gate 	}
18860Sstevel@tonic-gate 	if (rsafail) {
18870Sstevel@tonic-gate 		int bytes = BN_num_bytes(session_key_int);
18880Sstevel@tonic-gate 		u_char *buf = xmalloc(bytes);
18890Sstevel@tonic-gate 		MD5_CTX md;
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate 		log("do_connection: generating a fake encryption key");
18920Sstevel@tonic-gate 		(void) BN_bn2bin(session_key_int, buf);
18930Sstevel@tonic-gate 		MD5_Init(&md);
18940Sstevel@tonic-gate 		MD5_Update(&md, buf, bytes);
18950Sstevel@tonic-gate 		MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
18960Sstevel@tonic-gate 		MD5_Final(session_key, &md);
18970Sstevel@tonic-gate 		MD5_Init(&md);
18980Sstevel@tonic-gate 		MD5_Update(&md, session_key, 16);
18990Sstevel@tonic-gate 		MD5_Update(&md, buf, bytes);
19000Sstevel@tonic-gate 		MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
19010Sstevel@tonic-gate 		MD5_Final(session_key + 16, &md);
19020Sstevel@tonic-gate 		(void) memset(buf, 0, bytes);
19030Sstevel@tonic-gate 		xfree(buf);
19040Sstevel@tonic-gate 		for (i = 0; i < 16; i++)
19050Sstevel@tonic-gate 			session_id[i] = session_key[i] ^ session_key[i + 16];
19060Sstevel@tonic-gate 	}
19070Sstevel@tonic-gate 	/* Destroy the private and public keys. No longer. */
19080Sstevel@tonic-gate 	destroy_sensitive_data();
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate 	/* Destroy the decrypted integer.  It is no longer needed. */
19110Sstevel@tonic-gate 	BN_clear_free(session_key_int);
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	/* Set the session key.  From this on all communications will be encrypted. */
19140Sstevel@tonic-gate 	packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	/* Destroy our copy of the session key.  It is no longer needed. */
19170Sstevel@tonic-gate 	(void) memset(session_key, 0, sizeof(session_key));
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	debug("Received session key; encryption turned on.");
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	/* Send an acknowledgment packet.  Note that this packet is sent encrypted. */
19220Sstevel@tonic-gate 	packet_start(SSH_SMSG_SUCCESS);
19230Sstevel@tonic-gate 	packet_send();
19240Sstevel@tonic-gate 	packet_write_wait();
19250Sstevel@tonic-gate }
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate /*
19287574SJan.Pechanec@Sun.COM  * Prepare for SSH2 key exchange.
19290Sstevel@tonic-gate  */
19307574SJan.Pechanec@Sun.COM Kex *
prepare_for_ssh2_kex(void)19317574SJan.Pechanec@Sun.COM prepare_for_ssh2_kex(void)
19320Sstevel@tonic-gate {
19330Sstevel@tonic-gate 	Kex *kex;
19340Sstevel@tonic-gate 	Kex_hook_func kex_hook = NULL;
19350Sstevel@tonic-gate 	char **locales;
19368658SJan.Pechanec@Sun.COM 	static char **myproposal;
19378658SJan.Pechanec@Sun.COM 
19388658SJan.Pechanec@Sun.COM 	myproposal = my_srv_proposal;
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 	if (options.ciphers != NULL) {
19410Sstevel@tonic-gate 		myproposal[PROPOSAL_ENC_ALGS_CTOS] =
19420Sstevel@tonic-gate 		myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
19430Sstevel@tonic-gate 	}
19440Sstevel@tonic-gate 	myproposal[PROPOSAL_ENC_ALGS_CTOS] =
19450Sstevel@tonic-gate 	    compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
19460Sstevel@tonic-gate 	myproposal[PROPOSAL_ENC_ALGS_STOC] =
19470Sstevel@tonic-gate 	    compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 	if (options.macs != NULL) {
19500Sstevel@tonic-gate 		myproposal[PROPOSAL_MAC_ALGS_CTOS] =
19510Sstevel@tonic-gate 		myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
19520Sstevel@tonic-gate 	}
19530Sstevel@tonic-gate 	if (!options.compression) {
19540Sstevel@tonic-gate 		myproposal[PROPOSAL_COMP_ALGS_CTOS] =
19550Sstevel@tonic-gate 		myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
19560Sstevel@tonic-gate 	}
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	/*
19590Sstevel@tonic-gate 	 * Prepare kex algs / hostkey algs (excluding GSS, which is
19600Sstevel@tonic-gate 	 * handled in the kex hook.
19610Sstevel@tonic-gate 	 *
19620Sstevel@tonic-gate 	 * XXX This should probably move to the kex hook as well, where
19630Sstevel@tonic-gate 	 * all non-constant kex offer material belongs.
19640Sstevel@tonic-gate 	 */
19650Sstevel@tonic-gate 	myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = list_hostkey_types();
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	/* If we have no host key algs we can't offer KEXDH/KEX_DH_GEX */
19680Sstevel@tonic-gate 	if (myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] == NULL ||
19690Sstevel@tonic-gate 	    *myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] == '\0')
19700Sstevel@tonic-gate 		myproposal[PROPOSAL_KEX_ALGS] = "";
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 	if ((locales = g11n_getlocales()) != NULL) {
19730Sstevel@tonic-gate 		/* Solaris 9 SSH expects a list of locales */
19740Sstevel@tonic-gate 		if (datafellows & SSH_BUG_LOCALES_NOT_LANGTAGS)
19750Sstevel@tonic-gate 			myproposal[PROPOSAL_LANG_STOC] = xjoin(locales, ',');
19760Sstevel@tonic-gate 		else
19770Sstevel@tonic-gate 			myproposal[PROPOSAL_LANG_STOC] =
19780Sstevel@tonic-gate 				g11n_locales2langs(locales);
19790Sstevel@tonic-gate 	}
19800Sstevel@tonic-gate 
19815562Sjp161948 	if (locales != NULL)
19825562Sjp161948 		g11n_freelist(locales);
19835562Sjp161948 
19849845SJan.Pechanec@Sun.COM 	if ((myproposal[PROPOSAL_LANG_STOC] != NULL) &&
19850Sstevel@tonic-gate 	    (strcmp(myproposal[PROPOSAL_LANG_STOC], "")) != 0)
19860Sstevel@tonic-gate 		myproposal[PROPOSAL_LANG_CTOS] =
19870Sstevel@tonic-gate 			xstrdup(myproposal[PROPOSAL_LANG_STOC]);
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate #ifdef GSSAPI
19900Sstevel@tonic-gate 	if (options.gss_keyex)
19910Sstevel@tonic-gate 		kex_hook = ssh_gssapi_server_kex_hook;
19920Sstevel@tonic-gate #endif /* GSSAPI */
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 	kex = kex_setup(NULL, myproposal, kex_hook);
19955562Sjp161948 
19969845SJan.Pechanec@Sun.COM 	/*
19979845SJan.Pechanec@Sun.COM 	 * Note that the my_srv_proposal variable (ie., myproposal) is staticly
19989845SJan.Pechanec@Sun.COM 	 * initialized with "" for the language fields; we must not xfree such
19999845SJan.Pechanec@Sun.COM 	 * strings.
20009845SJan.Pechanec@Sun.COM 	 */
20019845SJan.Pechanec@Sun.COM 	if (myproposal[PROPOSAL_LANG_STOC] != NULL &&
20029845SJan.Pechanec@Sun.COM 	    strcmp(myproposal[PROPOSAL_LANG_STOC], "") != 0)
20035562Sjp161948 		xfree(myproposal[PROPOSAL_LANG_STOC]);
20049845SJan.Pechanec@Sun.COM 	if (myproposal[PROPOSAL_LANG_CTOS] != NULL &&
20059845SJan.Pechanec@Sun.COM 	    strcmp(myproposal[PROPOSAL_LANG_STOC], "") != 0)
20065562Sjp161948 		xfree(myproposal[PROPOSAL_LANG_CTOS]);
20075562Sjp161948 
20080Sstevel@tonic-gate 	kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
20090Sstevel@tonic-gate 	kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
20100Sstevel@tonic-gate #ifdef GSSAPI
20110Sstevel@tonic-gate 	kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
20120Sstevel@tonic-gate #endif /* GSSAPI */
20130Sstevel@tonic-gate 	kex->server = 1;
20147574SJan.Pechanec@Sun.COM 	kex->client_version_string = client_version_string;
20157574SJan.Pechanec@Sun.COM 	kex->server_version_string = server_version_string;
20167574SJan.Pechanec@Sun.COM 	kex->load_host_key = &get_hostkey_by_type;
20177574SJan.Pechanec@Sun.COM 	kex->host_key_index = &get_hostkey_index;
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	xxx_kex = kex;
20207574SJan.Pechanec@Sun.COM 	return (kex);
20217574SJan.Pechanec@Sun.COM }
20227574SJan.Pechanec@Sun.COM 
20237574SJan.Pechanec@Sun.COM /*
20247574SJan.Pechanec@Sun.COM  * Do SSH2 key exchange.
20257574SJan.Pechanec@Sun.COM  */
20267574SJan.Pechanec@Sun.COM static void
do_ssh2_kex(void)20277574SJan.Pechanec@Sun.COM do_ssh2_kex(void)
20287574SJan.Pechanec@Sun.COM {
20297574SJan.Pechanec@Sun.COM 	Kex *kex;
20307574SJan.Pechanec@Sun.COM 
20317574SJan.Pechanec@Sun.COM 	kex = prepare_for_ssh2_kex();
20327574SJan.Pechanec@Sun.COM 	kex_start(kex);
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 	dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 	if (kex->name) {
20370Sstevel@tonic-gate 		xfree(kex->name);
20380Sstevel@tonic-gate 		kex->name = NULL;
20390Sstevel@tonic-gate 	}
20400Sstevel@tonic-gate 	session_id2 = kex->session_id;
20410Sstevel@tonic-gate 	session_id2_len = kex->session_id_len;
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate #ifdef DEBUG_KEXDH
20440Sstevel@tonic-gate 	/* send 1st encrypted/maced/compressed message */
20450Sstevel@tonic-gate 	packet_start(SSH2_MSG_IGNORE);
20460Sstevel@tonic-gate 	packet_put_cstring("markus");
20470Sstevel@tonic-gate 	packet_send();
20480Sstevel@tonic-gate 	packet_write_wait();
20490Sstevel@tonic-gate #endif
20500Sstevel@tonic-gate 	debug("KEX done");
20510Sstevel@tonic-gate }
2052