sshd.c revision cd7d5faf5bbb52336a6f85578a90b31a648ac3fa
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
* All rights reserved
* This program is the ssh daemon. It listens for connections from clients,
* and performs authentication, executes use commands or shell, and forwards
* authentication agent connections.
*
* As far as I am concerned, the code I have written for this software
* can be used freely for any purpose. Any derived versions of this
* software must be clearly marked as such, and if the derived work is
* incompatible with the protocol description in the RFC file, it must be
* called by a name other than "ssh" or "Secure Shell".
*
* SSH2 implementation:
* Privilege Separation:
*
* Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved.
* Copyright (c) 2002 Niels Provos. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include "includes.h"
#ifdef HAVE_SECUREWARE
#include <sys/security.h>
#include <prot.h>
#endif
#include "ssh.h"
#include "ssh1.h"
#include "ssh2.h"
#include "xmalloc.h"
#include "rsa.h"
#include "sshpty.h"
#include "packet.h"
#include "mpaux.h"
#include "log.h"
#include "servconf.h"
#include "uidswap.h"
#include "compat.h"
#include "buffer.h"
#include "cipher.h"
#include "kex.h"
#include "key.h"
#include "dh.h"
#include "myproposal.h"
#include "authfile.h"
#include "pathnames.h"
#include "atomicio.h"
#include "canohost.h"
#include "auth.h"
#include "misc.h"
#include "dispatch.h"
#include "channels.h"
#include "session.h"
#include "g11n.h"
#include "sshlogin.h"
#include "xlist.h"
#include "engine.h"
#ifdef HAVE_BSM
#include "bsmaudit.h"
#endif /* HAVE_BSM */
#ifdef ALTPRIVSEP
#include "altprivsep.h"
#endif /* ALTPRIVSEP */
#ifdef HAVE_SOLARIS_CONTRACTS
#include <sys/contract.h>
#include <libcontract.h>
#endif /* HAVE_SOLARIS_CONTRACTS */
#ifdef GSSAPI
#include "ssh-gss.h"
#endif /* GSSAPI */
#ifdef LIBWRAP
#include <tcpd.h>
#include <syslog.h>
#ifndef lint
int allow_severity = LOG_INFO;
int deny_severity = LOG_WARNING;
#endif /* lint */
#endif /* LIBWRAP */
#ifndef O_NOCTTY
#define O_NOCTTY 0
#endif
#ifdef HAVE___PROGNAME
extern char *__progname;
#else
char *__progname;
#endif
/* Server configuration options. */
/* Name of the server configuration file. */
static char *config_file_name = _PATH_SERVER_CONFIG_FILE;
/*
* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
* Default value is AF_UNSPEC means both IPv4 and IPv6.
*/
#ifdef IPV4_DEFAULT
#else
#endif
/*
* Debug mode flag. This can be set on the command line. If debug
* mode is enabled, extra debugging output will be sent to the system
* log, the daemon will not go to background, and will exit after processing
* the first connection.
*/
int debug_flag = 0;
/* Flag indicating that the daemon should only test the configuration and keys. */
static int test_flag = 0;
/* Flag indicating that the daemon is being started from inetd. */
static int inetd_flag = 0;
/* Flag indicating that sshd should not detach and become a daemon. */
static int no_daemon_flag = 0;
/* debug goes to stderr unless inetd_flag is set */
int log_stderr = 0;
/* Saved arguments to main(). */
static char **saved_argv;
static int saved_argc;
/*
* The sockets that the server is listening; this is used in the SIGHUP
* signal handler.
*/
#define MAX_LISTEN_SOCKS 16
static int listen_socks[MAX_LISTEN_SOCKS];
static int num_listen_socks = 0;
/*
* the client's version string, passed by sshd2 in compat mode. if != NULL,
* sshd will skip the version-number exchange
*/
static char *client_version_string = NULL;
static char *server_version_string = NULL;
/* for rekeying XXX fixme */
/*
* Any really sensitive data in the application is contained in this
* structure. The idea is that this structure could be locked into memory so
* that the pages do not get written into swap. However, there are some
* problems. The private key contains BIGNUMs, and we do not (in principle)
* have access to the internals of them, and locking just the structure is
* not very useful. Currently, memory locking is not implemented.
*/
static struct {
int have_ssh1_key;
int have_ssh2_key;
/*
* Flag indicating whether the RSA server key needs to be regenerated.
* Is set in the SIGALRM handler and cleared when the key is regenerated.
*/
static volatile sig_atomic_t key_do_regen = 0;
/* This is set to true when a signal is received. */
static volatile sig_atomic_t received_sighup = 0;
static volatile sig_atomic_t received_sigterm = 0;
/* session identifier, used by RSA-auth */
/* same for ssh2 */
int session_id2_len = 0;
/* record remote hostname or ip */
/* options.max_startup sized array of fd ints */
static int *startup_pipes = NULL;
#ifdef GSSAPI
#endif /* GSSAPI */
/* Prototypes for various functions defined later in this file. */
void destroy_sensitive_data(void);
static void demote_sensitive_data(void);
static void do_ssh1_kex(void);
static void do_ssh2_kex(void);
/*
* Close all listening sockets
*/
static void
close_listen_socks(void)
{
int i;
for (i = 0; i < num_listen_socks; i++)
(void) close(listen_socks[i]);
num_listen_socks = -1;
}
static void
close_startup_pipes(void)
{
int i;
if (startup_pipes)
for (i = 0; i < options.max_startups; i++)
if (startup_pipes[i] != -1)
(void) close(startup_pipes[i]);
}
/*
* Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
* the effect is to reread the configuration file (and to regenerate
* the server key).
*/
static void
sighup_handler(int sig)
{
int save_errno = errno;
received_sighup = 1;
errno = save_errno;
}
/*
* Called from the main program after receiving SIGHUP.
* Restarts the server.
*/
static void
sighup_restart(void)
{
log("Received SIGHUP; restarting.");
exit(1);
}
/*
* Generic signal handler for terminating signals in the master daemon.
*/
static void
sigterm_handler(int sig)
{
}
/*
* SIGCHLD handler. This is called whenever a child dies. This will then
* reap any zombies left by exited children.
*/
static void
main_sigchld_handler(int sig)
{
int save_errno = errno;
int status;
;
errno = save_errno;
}
/*
* Signal handler for the alarm after the login grace period has expired.
*/
static void
grace_alarm_handler(int sig)
{
/* XXX no idea how fix this signal handler */
/* Log error and exit. */
}
#ifdef HAVE_SOLARIS_CONTRACTS
static int contracts_fd = -1;
void
{
const char *during = "opening process contract template";
/*
* Failure should not be treated as fatal on the theory that
* it's better to start with children in the same contract as
* the master listener than not at all.
*/
if (contracts_fd == -1) {
O_RDWR)) == -1)
goto cleanup;
during = "setting sundry contract terms";
goto cleanup;
goto cleanup;
goto cleanup;
goto cleanup;
}
during = "setting active template";
goto cleanup;
debug3("Set active contract");
return;
if (contracts_fd != -1)
(void) close(contracts_fd);
contracts_fd = -1;
if (errno)
debug2("Error while trying to set up active contract"
}
void
{
/* Clear active template so fork() creates no new contracts. */
if (contracts_fd == -1)
return;
debug2("Error while trying to clear active contract template"
else
debug3("Cleared active contract template (child)");
(void) close(contracts_fd);
contracts_fd = -1;
}
void
{
int cfd, n;
/* Clear active template, abandon latest contract. */
if (contracts_fd == -1)
return;
debug2("Error while clearing active contract template: %s",
else
debug3("Cleared active contract template (parent)");
if (!fork_succeeded)
return;
debug2("Error while getting latest contract: %s",
return;
}
debug2("Error while getting latest contract ID: %s",
return;
}
if (n >= PATH_MAX) {
debug2("Error while opening the latest contract ctl file: %s",
return;
}
debug2("Error while opening the latest contract ctl file: %s",
return;
}
debug2("Error while abandoning latest contract: %s",
else
debug3("Abandoned latest contract");
}
#endif /* HAVE_SOLARIS_CONTRACTS */
/*
* Signal handler for the key regeneration alarm. Note that this
* alarm only occurs in the daemon waiting for connections, and it does not
* do anything with the private key or random state before forking.
* Thus there should be no concurrency control/asynchronous execution
* problems.
*/
static void
{
int i;
verbose("Generating %s%d bit RSA key.",
verbose("RSA key generation complete.");
for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
if (i % 4 == 0)
rnd = arc4random();
rnd >>= 8;
}
}
static void
{
int save_errno = errno;
errno = save_errno;
key_do_regen = 1;
}
static void
{
int i, mismatch;
int remote_major, remote_minor;
char *s;
minor = 99;
} else {
}
if (client_version_string == NULL) {
/* Send our protocol version identification. */
!= strlen(server_version_string)) {
}
/* Read other sides version identification. */
for (i = 0; i < sizeof(buf) - 1; i++) {
log("Did not receive identification string from %s",
}
if (buf[i] == '\r') {
buf[i] = 0;
/* Kludge for F-Secure Macintosh < 1.0.2 */
if (i == 12 &&
break;
continue;
}
if (buf[i] == '\n') {
buf[i] = 0;
break;
}
}
}
/*
* Check that the versions match. In future this might accept
* several versions and set appropriate flags to handle them.
*/
s = "Protocol mismatch.\n";
log("Bad protocol version identification '%.100s' from %s",
}
debug("Client protocol version %d.%d; client software version %.100s",
if (datafellows & SSH_BUG_PROBE) {
log("probed from %s with %s. Don't panic.",
}
if (datafellows & SSH_BUG_SCANNER) {
log("scanned from %s with %s. Don't panic.",
}
mismatch = 0;
switch (remote_major) {
case 1:
if (remote_minor == 99) {
else
mismatch = 1;
break;
}
mismatch = 1;
break;
}
if (remote_minor < 3) {
packet_disconnect("Your ssh version is too old and "
"is no longer supported. Please install a newer version.");
} else if (remote_minor == 3) {
/* note that this disables agent-forwarding */
}
break;
case 2:
break;
}
/* FALLTHROUGH */
default:
mismatch = 1;
break;
}
if (mismatch) {
s = "Protocol major versions differ.\n";
log("Protocol major versions differ for %s: %.200s vs. %.200s",
}
}
/* Destroy the host and server keys. They will no longer be needed. */
void
destroy_sensitive_data(void)
{
int i;
if (sensitive_data.server_key) {
}
for (i = 0; i < options.num_host_key_files; i++) {
if (sensitive_data.host_keys[i]) {
}
}
}
/* Demote private to public keys for network child */
static void
demote_sensitive_data(void)
{
int i;
if (sensitive_data.server_key) {
}
for (i = 0; i < options.num_host_key_files; i++) {
if (sensitive_data.host_keys[i]) {
}
}
/* We do not clear ssh1_host key and cookie. XXX - Okay Niels? */
}
static char *
list_hostkey_types(void)
{
Buffer b;
char *p;
int i;
buffer_init(&b);
for (i = 0; i < options.num_host_key_files; i++) {
continue;
case KEY_RSA:
case KEY_DSA:
if (buffer_len(&b) > 0)
p = key_ssh_name(key);
buffer_append(&b, p, strlen(p));
break;
}
}
p = xstrdup(buffer_ptr(&b));
buffer_free(&b);
debug("list_hostkey_types: %s", p);
return p;
}
#ifdef lint
static
#endif /* lint */
Key *
get_hostkey_by_type(int type)
{
int i;
for (i = 0; i < options.num_host_key_files; i++) {
return key;
}
return NULL;
}
#ifdef lint
static
#endif /* lint */
Key *
get_hostkey_by_index(int ind)
{
return (NULL);
}
#ifdef lint
static
#endif /* lint */
int
{
int i;
for (i = 0; i < options.num_host_key_files; i++) {
return (i);
}
return (-1);
}
/*
* returns 1 if connection should be dropped, 0 otherwise.
* dropping starts at connection #max_startups_begin with a probability
* of (max_startups_rate/100). the probability increases linearly until
* all connections are dropped for startups > max_startups
*/
static int
drop_connection(int startups)
{
double p, r;
return 0;
return 1;
return 1;
p += options.max_startups_rate;
p /= 100.0;
r = arc4random() / (double) UINT_MAX;
debug("drop_connection: p %g, r %g", p, r);
return (r < p) ? 1 : 0;
}
static void
usage(void)
{
gettext("Usage: %s [options]\n"
"Options:\n"
" -f file Configuration file (default %s)\n"
" -d Debugging mode (multiple -d means more "
"debugging)\n"
" -i Started from inetd\n"
" -D Do not fork into daemon mode\n"
" -t Only test configuration file and keys\n"
" -q Quiet (no logging)\n"
" -p port Listen on the specified port (default: 22)\n"
" -k seconds Regenerate server key every this many seconds "
"(default: 3600)\n"
" -g seconds Grace period for authentication (default: 600)\n"
" -b bits Size of server RSA key (default: 768 bits)\n"
" -h file File from which to read host key (default: %s)\n"
" -4 Use IPv4 only\n"
" -6 Use IPv6 only\n"
" -o option Process the option as if it was read from "
"a configuration file.\n"),
exit(1);
}
/*
* Main program for the daemon.
*/
int
{
extern char *optarg;
extern int optind;
struct sockaddr_storage from;
const char *remote_ip;
int remote_port;
FILE *f;
int listen_sock, maxfd;
int startup_p[2];
int startups = 0;
#ifdef HAVE_BSM
#endif /* HAVE_BSM */
int mpipe;
#ifdef HAVE_SECUREWARE
#endif
init_rng();
/* Save argv. */
saved_argc = ac;
saved_argv = av;
/* Initialize configuration options to their default values. */
/* Parse command-line arguments. */
switch (opt) {
case '4':
break;
case '6':
break;
case 'f':
break;
case 'd':
if (0 == debug_flag) {
debug_flag = 1;
} else {
gettext("Debug level too high.\n"));
exit(1);
}
break;
case 'D':
no_daemon_flag = 1;
break;
case 'e':
log_stderr = 1;
break;
case 'i':
inetd_flag = 1;
break;
case 'Q':
/* ignored */
break;
case 'q':
break;
case 'b':
break;
case 'p':
exit(1);
}
exit(1);
}
break;
case 'g':
gettext("Invalid login grace time.\n"));
exit(1);
}
break;
case 'k':
gettext("Invalid key regeneration "
"interval.\n"));
exit(1);
}
break;
case 'h':
gettext("too many host keys.\n"));
exit(1);
}
break;
case 'V':
/* only makes sense with inetd_flag, i.e. no listen() */
inetd_flag = 1;
break;
case 't':
test_flag = 1;
break;
case 'o':
"command-line", 0) != 0)
exit(1);
break;
case '?':
default:
usage();
break;
}
}
/*
* There is no need to use the PKCS#11 engine in the master SSH process.
*/
seed_rng();
/*
* Force logging to stderr until we have loaded the private host
* key (unless started from inetd)
*/
!inetd_flag);
#ifdef _UNICOS
/* Cray can define user privs drop all prives now!
* Not needed on PRIV_SU systems!
*/
#endif
/* Read server configuration options from the configuration file. */
/* Fill in default values for those options not explicitly set. */
/* Check that there are no remaining arguments. */
exit(1);
}
/* load private host keys */
if (options.num_host_key_files > 0)
for (i = 0; i < options.num_host_key_files; i++)
for (i = 0; i < options.num_host_key_files; i++) {
error("Could not load host key: %s",
options.host_key_files[i]);
continue;
}
case KEY_RSA1:
break;
case KEY_RSA:
case KEY_DSA:
break;
}
}
log("Disabling protocol version 1. Could not load host key");
}
#ifdef GSSAPI
if (mechs == GSS_C_NULL_OID_SET) {
log("Disabling protocol version 2. Could not load host"
"key or GSS-API mechanisms");
}
#else
log("Disabling protocol version 2. Could not load host key");
#endif /* GSSAPI */
}
log("sshd: no hostkeys available -- exiting.");
exit(1);
}
/* Check certain values for sanity. */
exit(1);
}
/*
* Check that server and host key lengths differ sufficiently. This
* is necessary to make double encryption work with rsaref. Oh, I
* hate software patents. I dont know if this can go? Niels
*/
if (options.server_key_bits >
debug("Forcing server key to %d bits to make it differ from host key.",
}
}
/* Configuration looks good, so exit if in test mode. */
if (test_flag)
exit(0);
/*
* Clear out any supplemental groups we may have inherited. This
* prevents inadvertent creation of files with bad modes (in the
* portable version at least, it's certainly possible for PAM
* to create a file, and we can't control the code in every
* module which might be used).
*/
/* Initialize the log (it is reinitialized below in case we forked). */
if (debug_flag && !inetd_flag)
log_stderr = 1;
#ifdef HAVE_BSM
#endif /* HAVE_BSM */
/*
* If not in debugging mode, and not started from inetd, disconnect
* from the controlling terminal, and fork. The original process
* exits.
*/
#ifdef TIOCNOTTY
int fd;
#endif /* TIOCNOTTY */
if (daemon(0, 0) < 0)
/* Disconnect from the controlling tty. */
#ifdef TIOCNOTTY
if (fd >= 0) {
}
#endif /* TIOCNOTTY */
}
/* Reinitialize the log (because of the fork above). */
/* Initialize the random number generator. */
/* Chdir to the root directory so that the current disk can be
unmounted if desired. */
(void) chdir("/");
/* ignore SIGPIPE */
/* Start listening for a socket, unless started from inetd. */
if (inetd_flag) {
int s1;
startup_pipe = -1;
/* we need this later for setting audit context */
/*
* We intentionally do not close the descriptors 0, 1, and 2
* as our code for setting the descriptors won\'t work if
* ttyfd happens to be one of those.
*/
} else {
continue;
if (num_listen_socks >= MAX_LISTEN_SOCKS)
fatal("Too many listen sockets. "
"Enlarge MAX_LISTEN_SOCKS");
NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
error("getnameinfo failed");
continue;
}
/* Create socket for listening. */
if (listen_sock < 0) {
/* kernel may not support ipv6 */
continue;
}
(void) close(listen_sock);
continue;
}
/*
* Set socket options.
* Allow local port reuse in TIME_WAIT.
*/
/* Bind the socket to the desired port. */
error("Bind to port %s on %s failed: %.200s.",
(void) close(listen_sock);
continue;
}
/* Start listening on the port. */
}
if (!num_listen_socks)
fatal("Cannot bind any address.");
/*
* Arrange to restart on SIGHUP. The handler needs
* listen_sock.
*/
/* Arrange SIGCHLD to be caught. */
/* Write out the pid file after the sigterm handler is setup */
if (!debug_flag) {
/*
* easier to kill the correct sshd. We don't want to
* do this before the bind above because the bind will
* fail if there already is a daemon, and this will
* overwrite any old pid in the file.
*/
if (f) {
(void) fclose(f);
}
}
/* setup fd set for listen */
maxfd = 0;
for (i = 0; i < num_listen_socks; i++)
if (listen_socks[i] > maxfd)
maxfd = listen_socks[i];
/* pipes connected to unauthenticated childs */
for (i = 0; i < options.max_startups; i++)
startup_pipes[i] = -1;
/*
* Stay listening for connections until the system crashes or
* the daemon is killed with a signal.
*/
for (;;) {
if (received_sighup)
for (i = 0; i < num_listen_socks; i++)
for (i = 0; i < options.max_startups; i++)
if (startup_pipes[i] != -1)
/* Wait in select until there is a connection. */
if (received_sigterm) {
log("Received signal %d; terminating.",
(int) received_sigterm);
exit(255);
}
if (key_used && key_do_regen) {
key_used = 0;
key_do_regen = 0;
}
if (ret < 0)
continue;
for (i = 0; i < options.max_startups; i++)
if (startup_pipes[i] != -1 &&
/*
* the read end of the pipe is ready
* if the child has closed the pipe
* after successful authentication
* or if the child has died
*/
(void) close(startup_pipes[i]);
startup_pipes[i] = -1;
startups--;
}
for (i = 0; i < num_listen_socks; i++) {
continue;
&fromlen);
if (newsock < 0) {
continue;
}
continue;
}
continue;
}
continue;
}
for (j = 0; j < options.max_startups; j++)
if (startup_pipes[j] == -1) {
startup_pipes[j] = startup_p[0];
startups++;
break;
}
/*
* Got connection. Fork a child to handle it, unless
* we are in debugging mode.
*/
if (debug_flag) {
/*
* In debugging mode. Close the listening
* socket, and start processing the
* connection without forking.
*/
debug("Server will not fork when running in debugging mode.");
startup_pipe = -1;
break;
} else {
/*
* Normal production daemon. Fork, and have
* the child process the connection. The
* parent continues listening.
*/
#ifdef HAVE_SOLARIS_CONTRACTS
/*
* Setup Solaris contract template so
* the child process is in a different
* process contract than the parent;
* prevents established connections from
* being killed when the sshd master
* listener service is stopped.
*/
#endif /* HAVE_SOLARIS_CONTRACTS */
/*
* Child. Close the listening and max_startup
* sockets. Start using the accepted socket.
* Reinitialize logging (since our pid has
* changed). We break out of the loop to handle
* the connection.
*/
#ifdef HAVE_SOLARIS_CONTRACTS
#endif /* HAVE_SOLARIS_CONTRACTS */
break;
}
#ifdef HAVE_SOLARIS_CONTRACTS
contracts_post_fork_parent((pid > 0));
#endif /* HAVE_SOLARIS_CONTRACTS */
}
/* Parent. Stay in the loop. */
if (pid < 0)
else
/* Mark that the key has been used (it was "given" to the child). */
key_used == 0) {
/* Schedule server key regeneration alarm. */
key_used = 1;
}
/*
* Close the accepted socket since the child
* will now take care of the new connection.
*/
}
/* child process check (or debug mode) */
if (num_listen_socks < 0)
break;
}
}
/*
* This is the child processing a new connection, the SSH master process
* stays in the ( ; ; ) loop above.
*/
#ifdef HAVE_BSM
#endif
/*
* Create a new session and process group since the 4.4BSD
* setlogin() affects the entire process group. We don't
* want the child to be able to affect the parent.
*/
#if 0
/* XXX: this breaks Solaris */
#endif
/*
* Disable the key regeneration alarm. We will not regenerate the
* key since we are no longer in a position to give it to anyone. We
* will not restart on SIGHUP since it no longer makes sense.
*/
(void) alarm(0);
/* Set keepalives if requested. */
if (options.keepalives &&
sizeof(on)) < 0)
/*
* Register our connection. This turns encryption off because we do
* not have a key.
*/
#ifdef LIBWRAP
/* Check whether logins are denied from this host. */
{
struct request_info req;
if (!hosts_access(&req)) {
debug("Connection refused by tcp wrapper");
/* NOTREACHED */
fatal("libwrap refuse returns");
}
}
#endif /* LIBWRAP */
/* Log the connection. */
/*
* We don\'t want to listen forever unless the other side
* successfully authenticates itself. So we set up an alarm which is
* cleared after successful authentication. A limit of zero
* indicates no limit. Note that we don\'t set the alarm in debugging
* mode; it is just annoying to have the server exit just when you
* are about to discover the bug.
*/
if (!debug_flag)
/*
* Check that the connection comes from a privileged port.
* Rhosts-Authentication only makes sense from privileged
* programs. Of course, if the intruder has root access on his local
* machine, he can connect from any port. So do not use these
* authentication methods from machines that you do not trust.
*/
if (options.rhosts_authentication &&
(remote_port >= IPPORT_RESERVED ||
debug("Rhosts Authentication disabled, "
"originating port %d not trusted.", remote_port);
}
if (!packet_connection_is_ipv4() &&
debug("Kerberos Authentication disabled, only available for IPv4.");
}
#endif /* KRB4 && !KRB5 */
#ifdef AFS
/* If machine has AFS, set process authentication group. */
if (k_hasafs()) {
k_setpag();
k_unlog();
}
#endif /* AFS */
/*
* Start the monitor. That way both processes will have their own
* PKCS#11 sessions. See the PKCS#11 standard for more information on
* fork safety and packet.c for information about forking with the
* engine.
*/
/*
* The child is about to start the first key exchange while the monitor
* stays in altprivsep_start_and_do_monitor() function.
*/
/* perform the key exchange */
/* authenticate user and start session */
if (compat20) {
do_ssh2_kex();
} else {
do_ssh1_kex();
}
/* Authentication complete */
(void) alarm(0);
/* we no longer need an alarm handler */
if (startup_pipe != -1) {
(void) close(startup_pipe);
startup_pipe = -1;
}
/* ALTPRIVSEP Child */
/*
* Drop privileges, access to privileged resources.
*
* Destroy private host keys, if any.
*
* No need to release any GSS credentials -- sshd only acquires
* creds to determine what mechs it can negotiate then releases
* them right away and uses GSS_C_NO_CREDENTIAL to accept
* contexts.
*/
debug2("Unprivileged server process dropping privileges");
/* now send the authentication context to the monitor */
#ifdef HAVE_BSM
(void (*)(void *))audit_failed_login_cleanup,
(void *)authctxt);
#endif /* HAVE_BSM */
if (compat20) {
debug3("setting handler to forward re-key packets to the monitor");
}
/* Logged-in session. */
/* The connection has been terminated. */
packet_close();
#ifdef USE_PAM
#endif /* USE_PAM */
return (0);
}
/*
* Decrypt session_key_int using our private server key and private host key
* (key with larger modulus first).
*/
int
{
int rsafail = 0;
/* Server key has bigger modulus. */
fatal("do_connection: %s: server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
}
rsafail++;
rsafail++;
} else {
/* Host key has bigger modulus (or they are equal). */
fatal("do_connection: %s: host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
}
rsafail++;
rsafail++;
}
return (rsafail);
}
/*
* SSH1 key exchange
*/
static void
do_ssh1_kex(void)
{
int i, len;
int rsafail = 0;
/*
* Generate check bytes that the client must send back in the user
* packet in order for it to be accepted; this is used to defy ip
* spoofing attacks. Note that this only works against somebody
* doing IP spoofing from a remote machine; any machine on the local
* network can still see outgoing packets and catch the random
* cookie. This only affects rhosts authentication, and this is one
* of the reasons why it is inherently insecure.
*/
for (i = 0; i < 8; i++) {
if (i % 4 == 0)
rnd = arc4random();
rnd >>= 8;
}
/*
* Send our public key. We include in the packet 64 bits of random
* data that must be matched in the reply in order to prevent IP
* spoofing.
*/
for (i = 0; i < 8; i++)
packet_put_char(cookie[i]);
/* Store our public server RSA key. */
/* Store our public host RSA key. */
/* Put protocol flags. */
/* Declare which ciphers we support. */
/* Declare supported authentication types. */
auth_mask = 0;
#endif
#endif
#ifdef AFS
if (options.afs_token_passing)
#endif
/* Send the packet and wait for it to be sent. */
packet_send();
debug("Sent %d bit server key and %d bit host key.",
/* Read clients reply (cipher type and session key). */
/* Get cipher type and check whether we accept this. */
packet_disconnect("Warning: client selects unsupported cipher.");
}
/* Get check bytes from the packet. These must match those we
sent earlier with the public key packet. */
for (i = 0; i < 8; i++) {
if (cookie[i] != packet_get_char()) {
packet_disconnect("IP Spoofing check bytes do not match.");
}
}
/* Get the encrypted integer. */
fatal("do_ssh1_kex: BN_new failed");
/*
* Extract session key from the decrypted integer. The key is in the
* least significant 256 bits of the integer; the first byte of the
* key is in the highest bits.
*/
if (!rsafail) {
error("do_connection: bad session key len from %s: "
"session_key_int %d > sizeof(session_key) %lu",
rsafail++;
} else {
(void) BN_bn2bin(session_key_int,
/*
* Xor the first 16 bytes of the session key with the
* session id.
*/
for (i = 0; i < 16; i++)
session_key[i] ^= session_id[i];
}
}
if (rsafail) {
log("do_connection: generating a fake encryption key");
for (i = 0; i < 16; i++)
}
/* Destroy the private and public keys. No longer. */
/* Destroy the decrypted integer. It is no longer needed. */
/* Set the session key. From this on all communications will be encrypted. */
/* Destroy our copy of the session key. It is no longer needed. */
debug("Received session key; encryption turned on.");
/* Send an acknowledgment packet. Note that this packet is sent encrypted. */
packet_send();
}
/*
* Prepare for SSH2 key exchange.
*/
Kex *
prepare_for_ssh2_kex(void)
{
char **locales;
}
}
if (!options.compression) {
}
/*
* Prepare kex algs / hostkey algs (excluding GSS, which is
* handled in the kex hook.
*
* XXX This should probably move to the kex hook as well, where
* all non-constant kex offer material belongs.
*/
/* If we have no host key algs we can't offer KEXDH/KEX_DH_GEX */
/* Solaris 9 SSH expects a list of locales */
else
}
#ifdef GSSAPI
#endif /* GSSAPI */
#ifdef GSSAPI
#endif /* GSSAPI */
return (kex);
}
/*
* Do SSH2 key exchange.
*/
static void
do_ssh2_kex(void)
{
kex = prepare_for_ssh2_kex();
}
#ifdef DEBUG_KEXDH
/* send 1st encrypted/maced/compressed message */
packet_put_cstring("markus");
packet_send();
#endif
debug("KEX done");
}