blob: 6342842519c97376014546fdb718ccc2aac29ba5 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 * This program is the ssh daemon. It listens for connections from clients,
6 * and performs authentication, executes use commands or shell, and forwards
Damien Miller95def091999-11-25 00:26:21 +11007 * information to/from the application to the user client over an encrypted
Damien Millere4340be2000-09-16 13:29:08 +11008 * connection. This can also handle forwarding of X11, TCP/IP, and
9 * authentication agent connections.
Damien Millerefb4afe2000-04-12 18:45:05 +100010 *
Damien Millere4340be2000-09-16 13:29:08 +110011 * As far as I am concerned, the code I have written for this software
12 * can be used freely for any purpose. Any derived versions of this
13 * software must be clearly marked as such, and if the derived work is
14 * incompatible with the protocol description in the RFC file, it must be
15 * called by a name other than "ssh" or "Secure Shell".
16 *
17 * SSH2 implementation:
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000018 * Privilege Separation:
Damien Millere4340be2000-09-16 13:29:08 +110019 *
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000020 * Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved.
21 * Copyright (c) 2002 Niels Provos. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110022 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
33 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
36 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110042 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
44#include "includes.h"
Damien Miller0c889cd2004-03-22 09:36:00 +110045RCSID("$OpenBSD: sshd.c,v 1.290 2004/03/11 10:21:17 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include <openssl/dh.h>
48#include <openssl/bn.h>
Damien Miller6a47f302002-02-13 13:55:06 +110049#include <openssl/md5.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000050#include <openssl/rand.h>
Kevin Steves0ea1d9d2002-04-25 18:17:04 +000051#ifdef HAVE_SECUREWARE
52#include <sys/security.h>
53#include <prot.h>
54#endif
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055
56#include "ssh.h"
57#include "ssh1.h"
58#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059#include "xmalloc.h"
60#include "rsa.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000061#include "sshpty.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062#include "packet.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063#include "mpaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065#include "servconf.h"
66#include "uidswap.h"
67#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100068#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000069#include "cipher.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100070#include "kex.h"
Damien Millerb38eff82000-04-01 11:09:21 +100071#include "key.h"
Damien Miller874d77b2000-10-14 16:23:11 +110072#include "dh.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100073#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100074#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000075#include "pathnames.h"
76#include "atomicio.h"
77#include "canohost.h"
78#include "auth.h"
79#include "misc.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000080#include "dispatch.h"
Ben Lindstrom1bae4042001-10-03 17:46:39 +000081#include "channels.h"
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +000082#include "session.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000083#include "monitor_mm.h"
84#include "monitor.h"
85#include "monitor_wrap.h"
86#include "monitor_fdpass.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
88#ifdef LIBWRAP
89#include <tcpd.h>
90#include <syslog.h>
91int allow_severity = LOG_INFO;
92int deny_severity = LOG_WARNING;
93#endif /* LIBWRAP */
94
95#ifndef O_NOCTTY
96#define O_NOCTTY 0
97#endif
98
Ben Lindstrom49a79c02000-11-17 03:47:20 +000099#ifdef HAVE___PROGNAME
100extern char *__progname;
101#else
102char *__progname;
103#endif
104
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105/* Server configuration options. */
106ServerOptions options;
107
108/* Name of the server configuration file. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000109char *config_file_name = _PATH_SERVER_CONFIG_FILE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
Damien Miller4af51302000-04-16 11:18:38 +1000111/*
Damien Miller34132e52000-01-14 15:45:46 +1100112 * Flag indicating whether IPv4 or IPv6. This can be set on the command line.
113 * Default value is AF_UNSPEC means both IPv4 and IPv6.
114 */
115int IPv4or6 = AF_UNSPEC;
116
Damien Miller95def091999-11-25 00:26:21 +1100117/*
118 * Debug mode flag. This can be set on the command line. If debug
119 * mode is enabled, extra debugging output will be sent to the system
120 * log, the daemon will not go to background, and will exit after processing
121 * the first connection.
122 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123int debug_flag = 0;
124
Ben Lindstrom794325a2001-08-06 21:09:07 +0000125/* Flag indicating that the daemon should only test the configuration and keys. */
126int test_flag = 0;
127
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128/* Flag indicating that the daemon is being started from inetd. */
129int inetd_flag = 0;
130
Ben Lindstromc72745a2000-12-02 19:03:54 +0000131/* Flag indicating that sshd should not detach and become a daemon. */
132int no_daemon_flag = 0;
133
Damien Miller5ce662a1999-11-11 17:57:39 +1100134/* debug goes to stderr unless inetd_flag is set */
135int log_stderr = 0;
136
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137/* Saved arguments to main(). */
138char **saved_argv;
Damien Millerb8c656e2000-06-28 15:22:41 +1000139int saved_argc;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140
Damien Miller5428f641999-11-25 11:54:57 +1100141/*
Damien Miller34132e52000-01-14 15:45:46 +1100142 * The sockets that the server is listening; this is used in the SIGHUP
143 * signal handler.
Damien Miller5428f641999-11-25 11:54:57 +1100144 */
Damien Miller34132e52000-01-14 15:45:46 +1100145#define MAX_LISTEN_SOCKS 16
146int listen_socks[MAX_LISTEN_SOCKS];
147int num_listen_socks = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148
Damien Miller5428f641999-11-25 11:54:57 +1100149/*
150 * the client's version string, passed by sshd2 in compat mode. if != NULL,
151 * sshd will skip the version-number exchange
152 */
Damien Miller95def091999-11-25 00:26:21 +1100153char *client_version_string = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000154char *server_version_string = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000156/* for rekeying XXX fixme */
157Kex *xxx_kex;
158
Damien Miller5428f641999-11-25 11:54:57 +1100159/*
160 * Any really sensitive data in the application is contained in this
161 * structure. The idea is that this structure could be locked into memory so
162 * that the pages do not get written into swap. However, there are some
163 * problems. The private key contains BIGNUMs, and we do not (in principle)
164 * have access to the internals of them, and locking just the structure is
165 * not very useful. Currently, memory locking is not implemented.
166 */
Damien Miller95def091999-11-25 00:26:21 +1100167struct {
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000168 Key *server_key; /* ephemeral server key */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100169 Key *ssh1_host_key; /* ssh1 host key */
170 Key **host_keys; /* all private host keys */
171 int have_ssh1_key;
172 int have_ssh2_key;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000173 u_char ssh1_cookie[SSH_SESSION_KEY_LENGTH];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174} sensitive_data;
175
Damien Miller5428f641999-11-25 11:54:57 +1100176/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000177 * Flag indicating whether the RSA server key needs to be regenerated.
178 * Is set in the SIGALRM handler and cleared when the key is regenerated.
Damien Miller5428f641999-11-25 11:54:57 +1100179 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000180static volatile sig_atomic_t key_do_regen = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000182/* This is set to true when a signal is received. */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000183static volatile sig_atomic_t received_sighup = 0;
184static volatile sig_atomic_t received_sigterm = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185
Damien Millerb38eff82000-04-01 11:09:21 +1000186/* session identifier, used by RSA-auth */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000187u_char session_id[16];
Damien Millerb38eff82000-04-01 11:09:21 +1000188
Damien Millereba71ba2000-04-29 23:57:08 +1000189/* same for ssh2 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000190u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000191u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000192
Damien Miller942da032000-08-18 13:59:06 +1000193/* record remote hostname or ip */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000194u_int utmp_len = MAXHOSTNAMELEN;
Damien Miller942da032000-08-18 13:59:06 +1000195
Ben Lindstromd84df982001-12-06 16:35:40 +0000196/* options.max_startup sized array of fd ints */
197int *startup_pipes = NULL;
198int startup_pipe; /* in child */
199
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000200/* variables used for privilege separation */
Damien Miller8e7fb332003-02-24 12:03:03 +1100201int use_privsep;
Darren Tuckera8be9e22004-02-06 16:40:27 +1100202struct monitor *pmonitor = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000203
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000204/* message to be displayed after login */
205Buffer loginmsg;
206
Darren Tucker3e33cec2003-10-02 16:12:36 +1000207/* global authentication context */
208Authctxt *the_authctxt = NULL;
209
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210/* Prototypes for various functions defined later in this file. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000211void destroy_sensitive_data(void);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000212void demote_sensitive_data(void);
Damien Miller98c7ad62000-03-09 21:27:49 +1100213
Ben Lindstrombba81212001-06-25 05:01:22 +0000214static void do_ssh1_kex(void);
215static void do_ssh2_kex(void);
Damien Miller874d77b2000-10-14 16:23:11 +1100216
Damien Miller98c7ad62000-03-09 21:27:49 +1100217/*
Damien Miller34132e52000-01-14 15:45:46 +1100218 * Close all listening sockets
219 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000220static void
Damien Miller34132e52000-01-14 15:45:46 +1100221close_listen_socks(void)
222{
223 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000224
Damien Miller34132e52000-01-14 15:45:46 +1100225 for (i = 0; i < num_listen_socks; i++)
226 close(listen_socks[i]);
227 num_listen_socks = -1;
228}
229
Ben Lindstromd84df982001-12-06 16:35:40 +0000230static void
231close_startup_pipes(void)
232{
233 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000234
Ben Lindstromd84df982001-12-06 16:35:40 +0000235 if (startup_pipes)
236 for (i = 0; i < options.max_startups; i++)
237 if (startup_pipes[i] != -1)
238 close(startup_pipes[i]);
239}
240
Damien Miller34132e52000-01-14 15:45:46 +1100241/*
Damien Miller95def091999-11-25 00:26:21 +1100242 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
243 * the effect is to reread the configuration file (and to regenerate
244 * the server key).
245 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000246static void
Damien Miller95def091999-11-25 00:26:21 +1100247sighup_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248{
Ben Lindstrom07958482001-12-06 16:19:01 +0000249 int save_errno = errno;
250
Damien Miller95def091999-11-25 00:26:21 +1100251 received_sighup = 1;
252 signal(SIGHUP, sighup_handler);
Ben Lindstrom07958482001-12-06 16:19:01 +0000253 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254}
255
Damien Miller95def091999-11-25 00:26:21 +1100256/*
257 * Called from the main program after receiving SIGHUP.
258 * Restarts the server.
259 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000260static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000261sighup_restart(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262{
Damien Miller996acd22003-04-09 20:59:48 +1000263 logit("Received SIGHUP; restarting.");
Damien Miller34132e52000-01-14 15:45:46 +1100264 close_listen_socks();
Ben Lindstromd84df982001-12-06 16:35:40 +0000265 close_startup_pipes();
Damien Miller95def091999-11-25 00:26:21 +1100266 execv(saved_argv[0], saved_argv);
Damien Miller996acd22003-04-09 20:59:48 +1000267 logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
Ben Lindstrom822b6342002-06-23 21:38:49 +0000268 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100269 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270}
271
Damien Miller95def091999-11-25 00:26:21 +1100272/*
273 * Generic signal handler for terminating signals in the master daemon.
Damien Miller95def091999-11-25 00:26:21 +1100274 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000275static void
Damien Miller95def091999-11-25 00:26:21 +1100276sigterm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000278 received_sigterm = sig;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279}
280
Damien Miller95def091999-11-25 00:26:21 +1100281/*
282 * SIGCHLD handler. This is called whenever a child dies. This will then
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000283 * reap any zombies left by exited children.
Damien Miller95def091999-11-25 00:26:21 +1100284 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000285static void
Damien Miller95def091999-11-25 00:26:21 +1100286main_sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287{
Damien Miller95def091999-11-25 00:26:21 +1100288 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000289 pid_t pid;
Damien Miller95def091999-11-25 00:26:21 +1100290 int status;
Damien Miller431f66b1999-11-21 18:31:57 +1100291
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000292 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
293 (pid < 0 && errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100294 ;
Damien Miller431f66b1999-11-21 18:31:57 +1100295
Damien Miller95def091999-11-25 00:26:21 +1100296 signal(SIGCHLD, main_sigchld_handler);
297 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298}
299
Damien Miller95def091999-11-25 00:26:21 +1100300/*
301 * Signal handler for the alarm after the login grace period has expired.
302 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000303static void
Damien Miller95def091999-11-25 00:26:21 +1100304grace_alarm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000306 /* XXX no idea how fix this signal handler */
307
Darren Tuckera8be9e22004-02-06 16:40:27 +1100308 if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0)
309 kill(pmonitor->m_pid, SIGALRM);
310
Damien Miller95def091999-11-25 00:26:21 +1100311 /* Log error and exit. */
Damien Millerd27a76d2002-09-27 13:22:31 +1000312 fatal("Timeout before authentication for %s", get_remote_ipaddr());
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313}
314
Damien Miller95def091999-11-25 00:26:21 +1100315/*
Damien Miller95def091999-11-25 00:26:21 +1100316 * Signal handler for the key regeneration alarm. Note that this
317 * alarm only occurs in the daemon waiting for connections, and it does not
318 * do anything with the private key or random state before forking.
319 * Thus there should be no concurrency control/asynchronous execution
320 * problems.
321 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000322static void
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000323generate_ephemeral_server_key(void)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100324{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000325 u_int32_t rnd = 0;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000326 int i;
327
Ben Lindstroma3700052001-04-05 23:26:32 +0000328 verbose("Generating %s%d bit RSA key.",
Damien Millerff75ac42001-03-30 10:50:32 +1000329 sensitive_data.server_key ? "new " : "", options.server_key_bits);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100330 if (sensitive_data.server_key != NULL)
331 key_free(sensitive_data.server_key);
Ben Lindstroma3700052001-04-05 23:26:32 +0000332 sensitive_data.server_key = key_generate(KEY_RSA1,
Damien Millerff75ac42001-03-30 10:50:32 +1000333 options.server_key_bits);
334 verbose("RSA key generation complete.");
Ben Lindstromeb648a72001-03-05 06:00:29 +0000335
336 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
337 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000338 rnd = arc4random();
339 sensitive_data.ssh1_cookie[i] = rnd & 0xff;
340 rnd >>= 8;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000341 }
342 arc4random_stir();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100343}
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000344
Ben Lindstrombba81212001-06-25 05:01:22 +0000345static void
Damien Miller95def091999-11-25 00:26:21 +1100346key_regeneration_alarm(int sig)
347{
348 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000349
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000350 signal(SIGALRM, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +1100351 errno = save_errno;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000352 key_do_regen = 1;
Damien Miller95def091999-11-25 00:26:21 +1100353}
354
Ben Lindstrombba81212001-06-25 05:01:22 +0000355static void
Damien Millerb38eff82000-04-01 11:09:21 +1000356sshd_exchange_identification(int sock_in, int sock_out)
357{
Damien Miller78928792000-04-12 20:17:38 +1000358 int i, mismatch;
Damien Millerb38eff82000-04-01 11:09:21 +1000359 int remote_major, remote_minor;
Damien Miller78928792000-04-12 20:17:38 +1000360 int major, minor;
Damien Millerb38eff82000-04-01 11:09:21 +1000361 char *s;
362 char buf[256]; /* Must not be larger than remote_version. */
363 char remote_version[256]; /* Must be at least as big as buf. */
364
Damien Miller78928792000-04-12 20:17:38 +1000365 if ((options.protocol & SSH_PROTO_1) &&
366 (options.protocol & SSH_PROTO_2)) {
367 major = PROTOCOL_MAJOR_1;
368 minor = 99;
369 } else if (options.protocol & SSH_PROTO_2) {
370 major = PROTOCOL_MAJOR_2;
371 minor = PROTOCOL_MINOR_2;
372 } else {
373 major = PROTOCOL_MAJOR_1;
374 minor = PROTOCOL_MINOR_1;
375 }
376 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
Damien Millerb38eff82000-04-01 11:09:21 +1000377 server_version_string = xstrdup(buf);
378
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000379 /* Send our protocol version identification. */
380 if (atomicio(vwrite, sock_out, server_version_string,
381 strlen(server_version_string))
382 != strlen(server_version_string)) {
383 logit("Could not write ident string to %s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000384 cleanup_exit(255);
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000385 }
386
387 /* Read other sides version identification. */
388 memset(buf, 0, sizeof(buf));
389 for (i = 0; i < sizeof(buf) - 1; i++) {
390 if (atomicio(read, sock_in, &buf[i], 1) != 1) {
391 logit("Did not receive identification string from %s",
392 get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000393 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000394 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000395 if (buf[i] == '\r') {
396 buf[i] = 0;
397 /* Kludge for F-Secure Macintosh < 1.0.2 */
398 if (i == 12 &&
399 strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000400 break;
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000401 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000402 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000403 if (buf[i] == '\n') {
404 buf[i] = 0;
405 break;
406 }
Damien Millerb38eff82000-04-01 11:09:21 +1000407 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000408 buf[sizeof(buf) - 1] = 0;
409 client_version_string = xstrdup(buf);
Damien Millerb38eff82000-04-01 11:09:21 +1000410
411 /*
412 * Check that the versions match. In future this might accept
413 * several versions and set appropriate flags to handle them.
414 */
415 if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
416 &remote_major, &remote_minor, remote_version) != 3) {
Damien Miller4af51302000-04-16 11:18:38 +1000417 s = "Protocol mismatch.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000418 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Millerb38eff82000-04-01 11:09:21 +1000419 close(sock_in);
420 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000421 logit("Bad protocol version identification '%.100s' from %s",
Damien Millerb38eff82000-04-01 11:09:21 +1000422 client_version_string, get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000423 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000424 }
425 debug("Client protocol version %d.%d; client software version %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100426 remote_major, remote_minor, remote_version);
Damien Millerb38eff82000-04-01 11:09:21 +1000427
Damien Millerefb4afe2000-04-12 18:45:05 +1000428 compat_datafellows(remote_version);
429
Damien Millere9264972002-09-30 11:59:21 +1000430 if (datafellows & SSH_BUG_PROBE) {
Damien Miller996acd22003-04-09 20:59:48 +1000431 logit("probed from %s with %s. Don't panic.",
Damien Millere9264972002-09-30 11:59:21 +1000432 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000433 cleanup_exit(255);
Damien Millere9264972002-09-30 11:59:21 +1000434 }
435
Damien Miller27dbe6f2001-03-19 22:36:20 +1100436 if (datafellows & SSH_BUG_SCANNER) {
Damien Miller996acd22003-04-09 20:59:48 +1000437 logit("scanned from %s with %s. Don't panic.",
Damien Miller27dbe6f2001-03-19 22:36:20 +1100438 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000439 cleanup_exit(255);
Damien Miller27dbe6f2001-03-19 22:36:20 +1100440 }
441
Damien Miller78928792000-04-12 20:17:38 +1000442 mismatch = 0;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000443 switch (remote_major) {
Damien Millerb38eff82000-04-01 11:09:21 +1000444 case 1:
Damien Millereba71ba2000-04-29 23:57:08 +1000445 if (remote_minor == 99) {
446 if (options.protocol & SSH_PROTO_2)
447 enable_compat20();
448 else
449 mismatch = 1;
450 break;
451 }
Damien Miller78928792000-04-12 20:17:38 +1000452 if (!(options.protocol & SSH_PROTO_1)) {
453 mismatch = 1;
454 break;
455 }
Damien Millerb38eff82000-04-01 11:09:21 +1000456 if (remote_minor < 3) {
Damien Miller37023962000-07-11 17:31:38 +1000457 packet_disconnect("Your ssh version is too old and "
Damien Millerb38eff82000-04-01 11:09:21 +1000458 "is no longer supported. Please install a newer version.");
459 } else if (remote_minor == 3) {
460 /* note that this disables agent-forwarding */
461 enable_compat13();
462 }
Damien Miller78928792000-04-12 20:17:38 +1000463 break;
Damien Millerefb4afe2000-04-12 18:45:05 +1000464 case 2:
Damien Miller78928792000-04-12 20:17:38 +1000465 if (options.protocol & SSH_PROTO_2) {
Damien Millerefb4afe2000-04-12 18:45:05 +1000466 enable_compat20();
467 break;
468 }
469 /* FALLTHROUGH */
Damien Miller4af51302000-04-16 11:18:38 +1000470 default:
Damien Miller78928792000-04-12 20:17:38 +1000471 mismatch = 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000472 break;
473 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000474 chop(server_version_string);
Damien Miller78928792000-04-12 20:17:38 +1000475 debug("Local version string %.200s", server_version_string);
476
477 if (mismatch) {
478 s = "Protocol major versions differ.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000479 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Miller78928792000-04-12 20:17:38 +1000480 close(sock_in);
481 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000482 logit("Protocol major versions differ for %s: %.200s vs. %.200s",
Damien Miller78928792000-04-12 20:17:38 +1000483 get_remote_ipaddr(),
484 server_version_string, client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000485 cleanup_exit(255);
Damien Miller78928792000-04-12 20:17:38 +1000486 }
Damien Millereba71ba2000-04-29 23:57:08 +1000487}
488
Damien Miller0bc1bd82000-11-13 22:57:25 +1100489/* Destroy the host and server keys. They will no longer be needed. */
Damien Millereba71ba2000-04-29 23:57:08 +1000490void
491destroy_sensitive_data(void)
492{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100493 int i;
494
495 if (sensitive_data.server_key) {
496 key_free(sensitive_data.server_key);
497 sensitive_data.server_key = NULL;
498 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100499 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100500 if (sensitive_data.host_keys[i]) {
501 key_free(sensitive_data.host_keys[i]);
502 sensitive_data.host_keys[i] = NULL;
503 }
504 }
505 sensitive_data.ssh1_host_key = NULL;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000506 memset(sensitive_data.ssh1_cookie, 0, SSH_SESSION_KEY_LENGTH);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100507}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100508
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000509/* Demote private to public keys for network child */
510void
511demote_sensitive_data(void)
512{
513 Key *tmp;
514 int i;
515
516 if (sensitive_data.server_key) {
517 tmp = key_demote(sensitive_data.server_key);
518 key_free(sensitive_data.server_key);
519 sensitive_data.server_key = tmp;
520 }
521
522 for (i = 0; i < options.num_host_key_files; i++) {
523 if (sensitive_data.host_keys[i]) {
524 tmp = key_demote(sensitive_data.host_keys[i]);
525 key_free(sensitive_data.host_keys[i]);
526 sensitive_data.host_keys[i] = tmp;
527 if (tmp->type == KEY_RSA1)
528 sensitive_data.ssh1_host_key = tmp;
529 }
530 }
531
532 /* We do not clear ssh1_host key and cookie. XXX - Okay Niels? */
533}
534
Ben Lindstrom08105192002-03-22 02:50:06 +0000535static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000536privsep_preauth_child(void)
537{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000538 u_int32_t rnd[256];
Ben Lindstrom810af962002-07-04 00:11:40 +0000539 gid_t gidset[1];
Ben Lindstromc7431342002-03-22 03:11:49 +0000540 struct passwd *pw;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000541 int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000542
543 /* Enable challenge-response authentication for privilege separation */
544 privsep_challenge_enable();
545
546 for (i = 0; i < 256; i++)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000547 rnd[i] = arc4random();
548 RAND_seed(rnd, sizeof(rnd));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000549
550 /* Demote the private keys to public keys. */
551 demote_sensitive_data();
552
Ben Lindstromc7431342002-03-22 03:11:49 +0000553 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
Damien Miller0150c652002-04-24 09:49:09 +1000554 fatal("Privilege separation user %s does not exist",
555 SSH_PRIVSEP_USER);
Ben Lindstromc7431342002-03-22 03:11:49 +0000556 memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
557 endpwent();
558
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000559 /* Change our root directory */
Ben Lindstrom7a7edf72002-03-22 02:42:37 +0000560 if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
561 fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
562 strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000563 if (chdir("/") == -1)
Ben Lindstrom1ee9ec32002-03-22 03:14:45 +0000564 fatal("chdir(\"/\"): %s", strerror(errno));
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000565
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000566 /* Drop our privileges */
Ben Lindstromc7431342002-03-22 03:11:49 +0000567 debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
568 (u_int)pw->pw_gid);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000569#if 0
Darren Tuckerd5920482004-02-29 20:11:30 +1100570 /* XXX not ready, too heavy after chroot */
Ben Lindstromc7431342002-03-22 03:11:49 +0000571 do_setusercontext(pw);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000572#else
573 gidset[0] = pw->pw_gid;
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000574 if (setgroups(1, gidset) < 0)
575 fatal("setgroups: %.100s", strerror(errno));
576 permanently_set_uid(pw);
577#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000578}
579
Darren Tucker3e33cec2003-10-02 16:12:36 +1000580static int
581privsep_preauth(Authctxt *authctxt)
Ben Lindstrom943481c2002-03-22 03:43:46 +0000582{
Ben Lindstrom943481c2002-03-22 03:43:46 +0000583 int status;
584 pid_t pid;
585
586 /* Set up unprivileged child process to deal with network data */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000587 pmonitor = monitor_init();
Ben Lindstrom943481c2002-03-22 03:43:46 +0000588 /* Store a pointer to the kex for later rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000589 pmonitor->m_pkex = &xxx_kex;
Ben Lindstrom943481c2002-03-22 03:43:46 +0000590
591 pid = fork();
592 if (pid == -1) {
593 fatal("fork of unprivileged child failed");
594 } else if (pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000595 debug2("Network child is on pid %ld", (long)pid);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000596
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000597 close(pmonitor->m_recvfd);
Darren Tuckera8be9e22004-02-06 16:40:27 +1100598 pmonitor->m_pid = pid;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000599 monitor_child_preauth(authctxt, pmonitor);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000600 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000601
602 /* Sync memory */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000603 monitor_sync(pmonitor);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000604
605 /* Wait for the child's exit status */
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000606 while (waitpid(pid, &status, 0) < 0)
607 if (errno != EINTR)
608 break;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000609 return (1);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000610 } else {
611 /* child */
612
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000613 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000614
615 /* Demote the child */
616 if (getuid() == 0 || geteuid() == 0)
617 privsep_preauth_child();
Ben Lindstromf90f58d2002-03-26 01:53:03 +0000618 setproctitle("%s", "[net]");
Ben Lindstrom943481c2002-03-22 03:43:46 +0000619 }
Darren Tucker3e33cec2003-10-02 16:12:36 +1000620 return (0);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000621}
622
Ben Lindstrom08105192002-03-22 02:50:06 +0000623static void
Ben Lindstrom943481c2002-03-22 03:43:46 +0000624privsep_postauth(Authctxt *authctxt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000625{
Tim Rice9dd30812002-07-07 13:43:36 -0700626#ifdef DISABLE_FD_PASSING
Tim Rice8eff3192002-06-25 15:35:15 -0700627 if (1) {
628#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000629 if (authctxt->pw->pw_uid == 0 || options.use_login) {
Tim Rice8eff3192002-06-25 15:35:15 -0700630#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000631 /* File descriptor passing is broken or root login */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000632 monitor_apply_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000633 use_privsep = 0;
634 return;
635 }
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000636
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000637 /* Authentication complete */
638 alarm(0);
639 if (startup_pipe != -1) {
640 close(startup_pipe);
641 startup_pipe = -1;
642 }
643
644 /* New socket pair */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000645 monitor_reinit(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000646
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000647 pmonitor->m_pid = fork();
648 if (pmonitor->m_pid == -1)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000649 fatal("fork of unprivileged child failed");
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000650 else if (pmonitor->m_pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000651 debug2("User child is on pid %ld", (long)pmonitor->m_pid);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000652 close(pmonitor->m_recvfd);
653 monitor_child_postauth(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000654
655 /* NEVERREACHED */
656 exit(0);
657 }
658
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000659 close(pmonitor->m_sendfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000660
661 /* Demote the private keys to public keys. */
662 demote_sensitive_data();
663
664 /* Drop privileges */
665 do_setusercontext(authctxt->pw);
666
667 /* It is safe now to apply the key state */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000668 monitor_apply_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669}
670
Ben Lindstrombba81212001-06-25 05:01:22 +0000671static char *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100672list_hostkey_types(void)
673{
Damien Miller0e3b8722002-01-22 23:26:38 +1100674 Buffer b;
Damien Millerf58b58c2003-11-17 21:18:23 +1100675 const char *p;
676 char *ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100677 int i;
Damien Miller0e3b8722002-01-22 23:26:38 +1100678
679 buffer_init(&b);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100680 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100681 Key *key = sensitive_data.host_keys[i];
682 if (key == NULL)
683 continue;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000684 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100685 case KEY_RSA:
686 case KEY_DSA:
Damien Miller0e3b8722002-01-22 23:26:38 +1100687 if (buffer_len(&b) > 0)
688 buffer_append(&b, ",", 1);
689 p = key_ssh_name(key);
690 buffer_append(&b, p, strlen(p));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100691 break;
692 }
693 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100694 buffer_append(&b, "\0", 1);
Damien Millerf58b58c2003-11-17 21:18:23 +1100695 ret = xstrdup(buffer_ptr(&b));
Damien Miller0e3b8722002-01-22 23:26:38 +1100696 buffer_free(&b);
Damien Millerf58b58c2003-11-17 21:18:23 +1100697 debug("list_hostkey_types: %s", ret);
698 return ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100699}
700
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000701Key *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100702get_hostkey_by_type(int type)
703{
704 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000705
Damien Miller9f0f5c62001-12-21 14:45:46 +1100706 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100707 Key *key = sensitive_data.host_keys[i];
708 if (key != NULL && key->type == type)
709 return key;
710 }
711 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000712}
713
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000714Key *
715get_hostkey_by_index(int ind)
716{
717 if (ind < 0 || ind >= options.num_host_key_files)
718 return (NULL);
719 return (sensitive_data.host_keys[ind]);
720}
721
722int
723get_hostkey_index(Key *key)
724{
725 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000726
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000727 for (i = 0; i < options.num_host_key_files; i++) {
728 if (key == sensitive_data.host_keys[i])
729 return (i);
730 }
731 return (-1);
732}
733
Damien Miller942da032000-08-18 13:59:06 +1000734/*
735 * returns 1 if connection should be dropped, 0 otherwise.
736 * dropping starts at connection #max_startups_begin with a probability
737 * of (max_startups_rate/100). the probability increases linearly until
738 * all connections are dropped for startups > max_startups
739 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000740static int
Damien Miller942da032000-08-18 13:59:06 +1000741drop_connection(int startups)
742{
743 double p, r;
744
745 if (startups < options.max_startups_begin)
746 return 0;
747 if (startups >= options.max_startups)
748 return 1;
749 if (options.max_startups_rate == 100)
750 return 1;
751
752 p = 100 - options.max_startups_rate;
753 p *= startups - options.max_startups_begin;
754 p /= (double) (options.max_startups - options.max_startups_begin);
755 p += options.max_startups_rate;
756 p /= 100.0;
757 r = arc4random() / (double) UINT_MAX;
758
759 debug("drop_connection: p %g, r %g", p, r);
760 return (r < p) ? 1 : 0;
761}
762
Ben Lindstromade03f62001-12-06 18:22:17 +0000763static void
764usage(void)
765{
Damien Miller0c889cd2004-03-22 09:36:00 +1100766 fprintf(stderr, "%s, %s\n",
Darren Tucker4a250542003-10-03 17:57:24 +1000767 SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
Damien Millerb4087862004-03-22 09:35:21 +1100768 fprintf(stderr,
769"usage: sshd [-46Ddeiqt] [-b bits] [-f config_file] [-g login_grace_time]\n"
770" [-h host_key_file] [-k key_gen_time] [-o option] [-p port] [-u len]\n"
771 );
Ben Lindstromade03f62001-12-06 18:22:17 +0000772 exit(1);
773}
774
Damien Miller95def091999-11-25 00:26:21 +1100775/*
776 * Main program for the daemon.
777 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778int
779main(int ac, char **av)
780{
Damien Miller95def091999-11-25 00:26:21 +1100781 extern char *optarg;
782 extern int optind;
Damien Miller37023962000-07-11 17:31:38 +1000783 int opt, sock_in = 0, sock_out = 0, newsock, j, i, fdsetsz, on = 1;
Damien Miller166fca82000-04-20 07:42:21 +1000784 pid_t pid;
Damien Miller34132e52000-01-14 15:45:46 +1100785 socklen_t fromlen;
Damien Miller34132e52000-01-14 15:45:46 +1100786 fd_set *fdset;
787 struct sockaddr_storage from;
Damien Miller95def091999-11-25 00:26:21 +1100788 const char *remote_ip;
789 int remote_port;
Damien Miller95def091999-11-25 00:26:21 +1100790 FILE *f;
Damien Miller34132e52000-01-14 15:45:46 +1100791 struct addrinfo *ai;
792 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerb9997192003-12-17 16:29:22 +1100793 char *line;
Damien Miller34132e52000-01-14 15:45:46 +1100794 int listen_sock, maxfd;
Damien Miller37023962000-07-11 17:31:38 +1000795 int startup_p[2];
796 int startups = 0;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000797 Key *key;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000798 Authctxt *authctxt;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000799 int ret, key_used = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000800
Kevin Steves0ea1d9d2002-04-25 18:17:04 +0000801#ifdef HAVE_SECUREWARE
802 (void)set_auth_parameters(ac, av);
803#endif
Damien Miller59d3d5b2003-08-22 09:34:41 +1000804 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000805 init_rng();
806
Damien Millera8ed44b2003-01-10 09:53:12 +1100807 /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
Damien Millerb8c656e2000-06-28 15:22:41 +1000808 saved_argc = ac;
Damien Miller04cb5362003-05-15 21:29:10 +1000809 saved_argv = xmalloc(sizeof(*saved_argv) * (ac + 1));
Damien Millera8ed44b2003-01-10 09:53:12 +1100810 for (i = 0; i < ac; i++)
811 saved_argv[i] = xstrdup(av[i]);
Damien Miller04cb5362003-05-15 21:29:10 +1000812 saved_argv[i] = NULL;
Damien Millera8ed44b2003-01-10 09:53:12 +1100813
814#ifndef HAVE_SETPROCTITLE
815 /* Prepare for later setproctitle emulation */
816 compat_init_setproctitle(ac, av);
Damien Millerf2e3e9d2003-06-02 12:15:54 +1000817 av = saved_argv;
Damien Millera8ed44b2003-01-10 09:53:12 +1100818#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819
Damien Millerbfba3542004-03-22 09:29:57 +1100820 if (geteuid() == 0 && setgroups(0, NULL) == -1)
821 debug("setgroups(): %.200s", strerror(errno));
822
Damien Miller95def091999-11-25 00:26:21 +1100823 /* Initialize configuration options to their default values. */
824 initialize_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000825
Damien Miller95def091999-11-25 00:26:21 +1100826 /* Parse command-line arguments. */
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000827 while ((opt = getopt(ac, av, "f:p:b:k:h:g:u:o:dDeiqtQ46")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100828 switch (opt) {
Damien Miller34132e52000-01-14 15:45:46 +1100829 case '4':
830 IPv4or6 = AF_INET;
831 break;
832 case '6':
833 IPv4or6 = AF_INET6;
834 break;
Damien Miller95def091999-11-25 00:26:21 +1100835 case 'f':
836 config_file_name = optarg;
837 break;
838 case 'd':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000839 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100840 debug_flag = 1;
841 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000842 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
Damien Millere4340be2000-09-16 13:29:08 +1100843 options.log_level++;
Damien Miller95def091999-11-25 00:26:21 +1100844 break;
Ben Lindstromc72745a2000-12-02 19:03:54 +0000845 case 'D':
846 no_daemon_flag = 1;
847 break;
Ben Lindstrom9fce9f02001-04-11 23:10:09 +0000848 case 'e':
849 log_stderr = 1;
850 break;
Damien Miller95def091999-11-25 00:26:21 +1100851 case 'i':
852 inetd_flag = 1;
853 break;
854 case 'Q':
Ben Lindstromd5390202001-01-29 08:07:43 +0000855 /* ignored */
Damien Miller95def091999-11-25 00:26:21 +1100856 break;
857 case 'q':
858 options.log_level = SYSLOG_LEVEL_QUIET;
859 break;
860 case 'b':
861 options.server_key_bits = atoi(optarg);
862 break;
863 case 'p':
Damien Miller34132e52000-01-14 15:45:46 +1100864 options.ports_from_cmdline = 1;
Damien Millere4340be2000-09-16 13:29:08 +1100865 if (options.num_ports >= MAX_PORTS) {
866 fprintf(stderr, "too many ports.\n");
867 exit(1);
868 }
Ben Lindstrom19066a12001-04-12 23:39:26 +0000869 options.ports[options.num_ports++] = a2port(optarg);
870 if (options.ports[options.num_ports-1] == 0) {
871 fprintf(stderr, "Bad port number.\n");
872 exit(1);
873 }
Damien Miller95def091999-11-25 00:26:21 +1100874 break;
875 case 'g':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000876 if ((options.login_grace_time = convtime(optarg)) == -1) {
877 fprintf(stderr, "Invalid login grace time.\n");
878 exit(1);
879 }
Damien Miller95def091999-11-25 00:26:21 +1100880 break;
881 case 'k':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000882 if ((options.key_regeneration_time = convtime(optarg)) == -1) {
883 fprintf(stderr, "Invalid key regeneration interval.\n");
884 exit(1);
885 }
Damien Miller95def091999-11-25 00:26:21 +1100886 break;
887 case 'h':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100888 if (options.num_host_key_files >= MAX_HOSTKEYS) {
889 fprintf(stderr, "too many host keys.\n");
890 exit(1);
891 }
892 options.host_key_files[options.num_host_key_files++] = optarg;
Damien Miller95def091999-11-25 00:26:21 +1100893 break;
Ben Lindstrom794325a2001-08-06 21:09:07 +0000894 case 't':
895 test_flag = 1;
896 break;
Damien Miller942da032000-08-18 13:59:06 +1000897 case 'u':
898 utmp_len = atoi(optarg);
Ben Lindstrom41daec72002-07-23 21:15:13 +0000899 if (utmp_len > MAXHOSTNAMELEN) {
900 fprintf(stderr, "Invalid utmp length.\n");
901 exit(1);
902 }
Damien Miller942da032000-08-18 13:59:06 +1000903 break;
Ben Lindstromade03f62001-12-06 18:22:17 +0000904 case 'o':
Damien Millerb9997192003-12-17 16:29:22 +1100905 line = xstrdup(optarg);
906 if (process_server_config_line(&options, line,
Ben Lindstromade03f62001-12-06 18:22:17 +0000907 "command-line", 0) != 0)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100908 exit(1);
Damien Millerb9997192003-12-17 16:29:22 +1100909 xfree(line);
Ben Lindstromade03f62001-12-06 18:22:17 +0000910 break;
Damien Miller95def091999-11-25 00:26:21 +1100911 case '?':
912 default:
Ben Lindstromade03f62001-12-06 18:22:17 +0000913 usage();
914 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000915 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000916 }
Ben Lindstrom425fb022001-03-29 00:31:20 +0000917 SSLeay_add_all_algorithms();
Ben Lindstrom908afed2001-10-03 17:34:59 +0000918 channel_set_af(IPv4or6);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000919
Damien Miller34132e52000-01-14 15:45:46 +1100920 /*
921 * Force logging to stderr until we have loaded the private host
922 * key (unless started from inetd)
923 */
Kevin Stevesec84dc12000-12-13 17:45:15 +0000924 log_init(__progname,
Damien Miller5aa5d782002-02-08 22:01:54 +1100925 options.log_level == SYSLOG_LEVEL_NOT_SET ?
926 SYSLOG_LEVEL_INFO : options.log_level,
927 options.log_facility == SYSLOG_FACILITY_NOT_SET ?
928 SYSLOG_FACILITY_AUTH : options.log_facility,
Ben Lindstromc2faa4a2002-11-09 15:50:03 +0000929 log_stderr || !inetd_flag);
Damien Miller34132e52000-01-14 15:45:46 +1100930
Darren Tucker86c093d2004-03-08 22:59:03 +1100931#ifdef _AIX
932 /*
933 * Unset KRB5CCNAME, otherwise the user's session may inherit it from
934 * root's environment
935 */
936 unsetenv("KRB5CCNAME");
937#endif /* _AIX */
Tim Rice81ed5182002-09-25 17:38:46 -0700938#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000939 /* Cray can define user privs drop all prives now!
940 * Not needed on PRIV_SU systems!
941 */
942 drop_cray_privs();
943#endif
944
Damien Miller60bc5172001-03-19 09:38:15 +1100945 seed_rng();
946
Damien Miller95def091999-11-25 00:26:21 +1100947 /* Read server configuration options from the configuration file. */
948 read_server_config(&options, config_file_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949
Damien Miller95def091999-11-25 00:26:21 +1100950 /* Fill in default values for those options not explicitly set. */
951 fill_default_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952
Damien Miller95def091999-11-25 00:26:21 +1100953 /* Check that there are no remaining arguments. */
954 if (optind < ac) {
955 fprintf(stderr, "Extra argument %s.\n", av[optind]);
956 exit(1);
957 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958
Damien Miller95def091999-11-25 00:26:21 +1100959 debug("sshd version %.100s", SSH_VERSION);
Damien Miller2ccf6611999-11-15 15:25:10 +1100960
Damien Miller0bc1bd82000-11-13 22:57:25 +1100961 /* load private host keys */
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000962 sensitive_data.host_keys = xmalloc(options.num_host_key_files *
963 sizeof(Key *));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100964 for (i = 0; i < options.num_host_key_files; i++)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000965 sensitive_data.host_keys[i] = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100966 sensitive_data.server_key = NULL;
967 sensitive_data.ssh1_host_key = NULL;
968 sensitive_data.have_ssh1_key = 0;
969 sensitive_data.have_ssh2_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000970
Damien Miller9f0f5c62001-12-21 14:45:46 +1100971 for (i = 0; i < options.num_host_key_files; i++) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000972 key = key_load_private(options.host_key_files[i], "", NULL);
973 sensitive_data.host_keys[i] = key;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100974 if (key == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000975 error("Could not load host key: %s",
976 options.host_key_files[i]);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000977 sensitive_data.host_keys[i] = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100978 continue;
979 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000980 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100981 case KEY_RSA1:
982 sensitive_data.ssh1_host_key = key;
983 sensitive_data.have_ssh1_key = 1;
984 break;
985 case KEY_RSA:
986 case KEY_DSA:
987 sensitive_data.have_ssh2_key = 1;
988 break;
989 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000990 debug("private host key: #%d type %d %s", i, key->type,
991 key_type(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100992 }
993 if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
Damien Miller996acd22003-04-09 20:59:48 +1000994 logit("Disabling protocol version 1. Could not load host key");
Damien Millereba71ba2000-04-29 23:57:08 +1000995 options.protocol &= ~SSH_PROTO_1;
996 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100997 if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
Damien Miller996acd22003-04-09 20:59:48 +1000998 logit("Disabling protocol version 2. Could not load host key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100999 options.protocol &= ~SSH_PROTO_2;
Damien Millereba71ba2000-04-29 23:57:08 +10001000 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001001 if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
Damien Miller996acd22003-04-09 20:59:48 +10001002 logit("sshd: no hostkeys available -- exiting.");
Damien Miller95def091999-11-25 00:26:21 +11001003 exit(1);
1004 }
Damien Miller95def091999-11-25 00:26:21 +11001005
Damien Millereba71ba2000-04-29 23:57:08 +10001006 /* Check certain values for sanity. */
1007 if (options.protocol & SSH_PROTO_1) {
1008 if (options.server_key_bits < 512 ||
1009 options.server_key_bits > 32768) {
1010 fprintf(stderr, "Bad server key size.\n");
1011 exit(1);
1012 }
1013 /*
1014 * Check that server and host key lengths differ sufficiently. This
1015 * is necessary to make double encryption work with rsaref. Oh, I
1016 * hate software patents. I dont know if this can go? Niels
1017 */
1018 if (options.server_key_bits >
Ben Lindstrom822b6342002-06-23 21:38:49 +00001019 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) -
1020 SSH_KEY_BITS_RESERVED && options.server_key_bits <
1021 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1022 SSH_KEY_BITS_RESERVED) {
Damien Millereba71ba2000-04-29 23:57:08 +10001023 options.server_key_bits =
Ben Lindstrom822b6342002-06-23 21:38:49 +00001024 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1025 SSH_KEY_BITS_RESERVED;
Damien Millereba71ba2000-04-29 23:57:08 +10001026 debug("Forcing server key to %d bits to make it differ from host key.",
1027 options.server_key_bits);
1028 }
1029 }
1030
Ben Lindstroma26ea632002-06-06 20:46:25 +00001031 if (use_privsep) {
1032 struct passwd *pw;
1033 struct stat st;
1034
1035 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
1036 fatal("Privilege separation user %s does not exist",
1037 SSH_PRIVSEP_USER);
1038 if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
1039 (S_ISDIR(st.st_mode) == 0))
1040 fatal("Missing privilege separation directory: %s",
1041 _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstrom59627352002-06-27 18:02:21 +00001042
1043#ifdef HAVE_CYGWIN
1044 if (check_ntsec(_PATH_PRIVSEP_CHROOT_DIR) &&
1045 (st.st_uid != getuid () ||
1046 (st.st_mode & (S_IWGRP|S_IWOTH)) != 0))
1047#else
Ben Lindstrom2dfacb32002-06-23 00:33:47 +00001048 if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
Ben Lindstrom59627352002-06-27 18:02:21 +00001049#endif
Damien Miller180fc5b2003-02-24 11:50:18 +11001050 fatal("%s must be owned by root and not group or "
1051 "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstroma26ea632002-06-06 20:46:25 +00001052 }
1053
Ben Lindstrom794325a2001-08-06 21:09:07 +00001054 /* Configuration looks good, so exit if in test mode. */
1055 if (test_flag)
1056 exit(0);
1057
Damien Miller87aea252002-05-10 12:20:24 +10001058 /*
1059 * Clear out any supplemental groups we may have inherited. This
1060 * prevents inadvertent creation of files with bad modes (in the
Damien Millera8e06ce2003-11-21 23:48:55 +11001061 * portable version at least, it's certainly possible for PAM
1062 * to create a file, and we can't control the code in every
Damien Miller87aea252002-05-10 12:20:24 +10001063 * module which might be used).
1064 */
1065 if (setgroups(0, NULL) < 0)
1066 debug("setgroups() failed: %.200s", strerror(errno));
1067
Damien Millereba71ba2000-04-29 23:57:08 +10001068 /* Initialize the log (it is reinitialized below in case we forked). */
Damien Miller95def091999-11-25 00:26:21 +11001069 if (debug_flag && !inetd_flag)
1070 log_stderr = 1;
Kevin Stevesec84dc12000-12-13 17:45:15 +00001071 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001072
Damien Millereba71ba2000-04-29 23:57:08 +10001073 /*
1074 * If not in debugging mode, and not started from inetd, disconnect
1075 * from the controlling terminal, and fork. The original process
1076 * exits.
1077 */
Ben Lindstromc72745a2000-12-02 19:03:54 +00001078 if (!(debug_flag || inetd_flag || no_daemon_flag)) {
Damien Miller95def091999-11-25 00:26:21 +11001079#ifdef TIOCNOTTY
1080 int fd;
1081#endif /* TIOCNOTTY */
1082 if (daemon(0, 0) < 0)
1083 fatal("daemon() failed: %.200s", strerror(errno));
1084
1085 /* Disconnect from the controlling tty. */
1086#ifdef TIOCNOTTY
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001087 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
Damien Miller95def091999-11-25 00:26:21 +11001088 if (fd >= 0) {
1089 (void) ioctl(fd, TIOCNOTTY, NULL);
1090 close(fd);
1091 }
1092#endif /* TIOCNOTTY */
1093 }
1094 /* Reinitialize the log (because of the fork above). */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001095 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001096
Damien Miller95def091999-11-25 00:26:21 +11001097 /* Initialize the random number generator. */
1098 arc4random_stir();
1099
1100 /* Chdir to the root directory so that the current disk can be
1101 unmounted if desired. */
1102 chdir("/");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001103
Ben Lindstromde71cda2001-03-24 00:43:26 +00001104 /* ignore SIGPIPE */
1105 signal(SIGPIPE, SIG_IGN);
Damien Miller95def091999-11-25 00:26:21 +11001106
Damien Miller95def091999-11-25 00:26:21 +11001107 /* Start listening for a socket, unless started from inetd. */
1108 if (inetd_flag) {
Ben Lindstrom206941f2001-04-15 14:27:16 +00001109 int s1;
Damien Miller95def091999-11-25 00:26:21 +11001110 s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */
Ben Lindstrom206941f2001-04-15 14:27:16 +00001111 dup(s1);
Damien Miller95def091999-11-25 00:26:21 +11001112 sock_in = dup(0);
1113 sock_out = dup(1);
Damien Miller994cf142000-07-21 10:19:44 +10001114 startup_pipe = -1;
Damien Millereba71ba2000-04-29 23:57:08 +10001115 /*
1116 * We intentionally do not close the descriptors 0, 1, and 2
1117 * as our code for setting the descriptors won\'t work if
1118 * ttyfd happens to be one of those.
1119 */
Damien Miller95def091999-11-25 00:26:21 +11001120 debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001121 if (options.protocol & SSH_PROTO_1)
Ben Lindstromca42d5f2001-03-09 18:25:32 +00001122 generate_ephemeral_server_key();
Damien Miller95def091999-11-25 00:26:21 +11001123 } else {
Damien Miller34132e52000-01-14 15:45:46 +11001124 for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
1125 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1126 continue;
1127 if (num_listen_socks >= MAX_LISTEN_SOCKS)
1128 fatal("Too many listen sockets. "
1129 "Enlarge MAX_LISTEN_SOCKS");
1130 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
1131 ntop, sizeof(ntop), strport, sizeof(strport),
1132 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1133 error("getnameinfo failed");
1134 continue;
1135 }
1136 /* Create socket for listening. */
Damien Miller2372ace2003-05-14 13:42:23 +10001137 listen_sock = socket(ai->ai_family, ai->ai_socktype,
1138 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11001139 if (listen_sock < 0) {
1140 /* kernel may not support ipv6 */
1141 verbose("socket: %.100s", strerror(errno));
1142 continue;
1143 }
Darren Tuckerefa37062004-02-24 09:20:29 +11001144 if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) {
1145 error("listen_sock O_NONBLOCK: %s", strerror(errno));
1146 close(listen_sock);
1147 continue;
1148 }
Damien Miller34132e52000-01-14 15:45:46 +11001149 /*
Damien Millere1383ce2002-09-19 11:49:37 +10001150 * Set socket options.
1151 * Allow local port reuse in TIME_WAIT.
Damien Miller34132e52000-01-14 15:45:46 +11001152 */
Damien Millere1383ce2002-09-19 11:49:37 +10001153 if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
1154 &on, sizeof(on)) == -1)
1155 error("setsockopt SO_REUSEADDR: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001156
Damien Miller34132e52000-01-14 15:45:46 +11001157 debug("Bind to port %s on %s.", strport, ntop);
Damien Miller95def091999-11-25 00:26:21 +11001158
Damien Miller34132e52000-01-14 15:45:46 +11001159 /* Bind the socket to the desired port. */
Damien Miller0a4e27d2001-02-18 12:36:39 +11001160 if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1161 if (!ai->ai_next)
1162 error("Bind to port %s on %s failed: %.200s.",
1163 strport, ntop, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11001164 close(listen_sock);
1165 continue;
1166 }
1167 listen_socks[num_listen_socks] = listen_sock;
1168 num_listen_socks++;
Damien Miller95def091999-11-25 00:26:21 +11001169
Damien Miller34132e52000-01-14 15:45:46 +11001170 /* Start listening on the port. */
Damien Miller996acd22003-04-09 20:59:48 +10001171 logit("Server listening on %s port %s.", ntop, strport);
Darren Tucker3175eb92003-12-09 19:15:11 +11001172 if (listen(listen_sock, SSH_LISTEN_BACKLOG) < 0)
Damien Miller34132e52000-01-14 15:45:46 +11001173 fatal("listen: %.100s", strerror(errno));
1174
Damien Miller95def091999-11-25 00:26:21 +11001175 }
Damien Miller34132e52000-01-14 15:45:46 +11001176 freeaddrinfo(options.listen_addrs);
1177
1178 if (!num_listen_socks)
1179 fatal("Cannot bind any address.");
1180
Ben Lindstrom98097862001-06-25 05:10:20 +00001181 if (options.protocol & SSH_PROTO_1)
1182 generate_ephemeral_server_key();
1183
1184 /*
1185 * Arrange to restart on SIGHUP. The handler needs
1186 * listen_sock.
1187 */
1188 signal(SIGHUP, sighup_handler);
1189
1190 signal(SIGTERM, sigterm_handler);
1191 signal(SIGQUIT, sigterm_handler);
1192
1193 /* Arrange SIGCHLD to be caught. */
1194 signal(SIGCHLD, main_sigchld_handler);
1195
1196 /* Write out the pid file after the sigterm handler is setup */
Damien Miller95def091999-11-25 00:26:21 +11001197 if (!debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +11001198 /*
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001199 * Record our pid in /var/run/sshd.pid to make it
1200 * easier to kill the correct sshd. We don't want to
1201 * do this before the bind above because the bind will
Damien Miller5428f641999-11-25 11:54:57 +11001202 * fail if there already is a daemon, and this will
1203 * overwrite any old pid in the file.
1204 */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001205 f = fopen(options.pid_file, "wb");
Darren Tuckere5327042003-07-03 13:40:44 +10001206 if (f == NULL) {
1207 error("Couldn't create pid file \"%s\": %s",
1208 options.pid_file, strerror(errno));
1209 } else {
Ben Lindstromce0f6342002-06-11 16:42:49 +00001210 fprintf(f, "%ld\n", (long) getpid());
Damien Miller95def091999-11-25 00:26:21 +11001211 fclose(f);
1212 }
1213 }
Damien Miller95def091999-11-25 00:26:21 +11001214
Damien Miller34132e52000-01-14 15:45:46 +11001215 /* setup fd set for listen */
Damien Miller37023962000-07-11 17:31:38 +10001216 fdset = NULL;
Damien Miller34132e52000-01-14 15:45:46 +11001217 maxfd = 0;
1218 for (i = 0; i < num_listen_socks; i++)
1219 if (listen_socks[i] > maxfd)
1220 maxfd = listen_socks[i];
Damien Miller37023962000-07-11 17:31:38 +10001221 /* pipes connected to unauthenticated childs */
1222 startup_pipes = xmalloc(options.max_startups * sizeof(int));
1223 for (i = 0; i < options.max_startups; i++)
1224 startup_pipes[i] = -1;
Damien Miller34132e52000-01-14 15:45:46 +11001225
Damien Miller5428f641999-11-25 11:54:57 +11001226 /*
1227 * Stay listening for connections until the system crashes or
1228 * the daemon is killed with a signal.
1229 */
Damien Miller95def091999-11-25 00:26:21 +11001230 for (;;) {
1231 if (received_sighup)
1232 sighup_restart();
Damien Miller37023962000-07-11 17:31:38 +10001233 if (fdset != NULL)
1234 xfree(fdset);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001235 fdsetsz = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
Damien Miller37023962000-07-11 17:31:38 +10001236 fdset = (fd_set *)xmalloc(fdsetsz);
Damien Miller34132e52000-01-14 15:45:46 +11001237 memset(fdset, 0, fdsetsz);
Damien Miller37023962000-07-11 17:31:38 +10001238
Damien Miller34132e52000-01-14 15:45:46 +11001239 for (i = 0; i < num_listen_socks; i++)
1240 FD_SET(listen_socks[i], fdset);
Damien Miller37023962000-07-11 17:31:38 +10001241 for (i = 0; i < options.max_startups; i++)
1242 if (startup_pipes[i] != -1)
1243 FD_SET(startup_pipes[i], fdset);
1244
1245 /* Wait in select until there is a connection. */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001246 ret = select(maxfd+1, fdset, NULL, NULL, NULL);
1247 if (ret < 0 && errno != EINTR)
1248 error("select: %.100s", strerror(errno));
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001249 if (received_sigterm) {
Damien Miller996acd22003-04-09 20:59:48 +10001250 logit("Received signal %d; terminating.",
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001251 (int) received_sigterm);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001252 close_listen_socks();
1253 unlink(options.pid_file);
1254 exit(255);
1255 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001256 if (key_used && key_do_regen) {
Ben Lindstromca42d5f2001-03-09 18:25:32 +00001257 generate_ephemeral_server_key();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001258 key_used = 0;
1259 key_do_regen = 0;
Damien Miller34132e52000-01-14 15:45:46 +11001260 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001261 if (ret < 0)
1262 continue;
1263
Damien Miller37023962000-07-11 17:31:38 +10001264 for (i = 0; i < options.max_startups; i++)
1265 if (startup_pipes[i] != -1 &&
1266 FD_ISSET(startup_pipes[i], fdset)) {
1267 /*
1268 * the read end of the pipe is ready
1269 * if the child has closed the pipe
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001270 * after successful authentication
Damien Miller37023962000-07-11 17:31:38 +10001271 * or if the child has died
1272 */
1273 close(startup_pipes[i]);
1274 startup_pipes[i] = -1;
1275 startups--;
1276 }
Damien Miller34132e52000-01-14 15:45:46 +11001277 for (i = 0; i < num_listen_socks; i++) {
1278 if (!FD_ISSET(listen_socks[i], fdset))
Damien Miller95def091999-11-25 00:26:21 +11001279 continue;
Damien Miller37023962000-07-11 17:31:38 +10001280 fromlen = sizeof(from);
1281 newsock = accept(listen_socks[i], (struct sockaddr *)&from,
1282 &fromlen);
1283 if (newsock < 0) {
1284 if (errno != EINTR && errno != EWOULDBLOCK)
1285 error("accept: %.100s", strerror(errno));
1286 continue;
1287 }
Darren Tuckerefa37062004-02-24 09:20:29 +11001288 if (fcntl(newsock, F_SETFL, 0) < 0) {
1289 error("newsock del O_NONBLOCK: %s", strerror(errno));
1290 close(newsock);
1291 continue;
1292 }
Damien Miller942da032000-08-18 13:59:06 +10001293 if (drop_connection(startups) == 1) {
1294 debug("drop connection #%d", startups);
Damien Miller37023962000-07-11 17:31:38 +10001295 close(newsock);
1296 continue;
1297 }
1298 if (pipe(startup_p) == -1) {
1299 close(newsock);
1300 continue;
1301 }
1302
1303 for (j = 0; j < options.max_startups; j++)
1304 if (startup_pipes[j] == -1) {
1305 startup_pipes[j] = startup_p[0];
1306 if (maxfd < startup_p[0])
1307 maxfd = startup_p[0];
1308 startups++;
1309 break;
1310 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001311
Damien Miller5428f641999-11-25 11:54:57 +11001312 /*
Damien Miller37023962000-07-11 17:31:38 +10001313 * Got connection. Fork a child to handle it, unless
1314 * we are in debugging mode.
Damien Miller5428f641999-11-25 11:54:57 +11001315 */
Damien Miller37023962000-07-11 17:31:38 +10001316 if (debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +11001317 /*
Damien Miller37023962000-07-11 17:31:38 +10001318 * In debugging mode. Close the listening
1319 * socket, and start processing the
1320 * connection without forking.
Damien Miller5428f641999-11-25 11:54:57 +11001321 */
Damien Miller37023962000-07-11 17:31:38 +10001322 debug("Server will not fork when running in debugging mode.");
Damien Miller34132e52000-01-14 15:45:46 +11001323 close_listen_socks();
Damien Miller95def091999-11-25 00:26:21 +11001324 sock_in = newsock;
1325 sock_out = newsock;
Damien Miller4d97ba22000-07-11 18:15:50 +10001326 startup_pipe = -1;
Damien Miller182ee6e2000-07-12 09:45:27 +10001327 pid = getpid();
Damien Miller95def091999-11-25 00:26:21 +11001328 break;
Damien Miller37023962000-07-11 17:31:38 +10001329 } else {
1330 /*
1331 * Normal production daemon. Fork, and have
1332 * the child process the connection. The
1333 * parent continues listening.
1334 */
1335 if ((pid = fork()) == 0) {
1336 /*
1337 * Child. Close the listening and max_startup
1338 * sockets. Start using the accepted socket.
1339 * Reinitialize logging (since our pid has
1340 * changed). We break out of the loop to handle
1341 * the connection.
1342 */
1343 startup_pipe = startup_p[1];
Ben Lindstromd84df982001-12-06 16:35:40 +00001344 close_startup_pipes();
Damien Miller37023962000-07-11 17:31:38 +10001345 close_listen_socks();
1346 sock_in = newsock;
1347 sock_out = newsock;
Kevin Stevesec84dc12000-12-13 17:45:15 +00001348 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller37023962000-07-11 17:31:38 +10001349 break;
1350 }
Damien Miller95def091999-11-25 00:26:21 +11001351 }
Damien Miller37023962000-07-11 17:31:38 +10001352
1353 /* Parent. Stay in the loop. */
1354 if (pid < 0)
1355 error("fork: %.100s", strerror(errno));
1356 else
Ben Lindstromce0f6342002-06-11 16:42:49 +00001357 debug("Forked child %ld.", (long)pid);
Damien Miller37023962000-07-11 17:31:38 +10001358
1359 close(startup_p[1]);
1360
1361 /* Mark that the key has been used (it was "given" to the child). */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001362 if ((options.protocol & SSH_PROTO_1) &&
1363 key_used == 0) {
1364 /* Schedule server key regeneration alarm. */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00001365 signal(SIGALRM, key_regeneration_alarm);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001366 alarm(options.key_regeneration_time);
1367 key_used = 1;
1368 }
Damien Miller37023962000-07-11 17:31:38 +10001369
1370 arc4random_stir();
1371
1372 /* Close the new socket (the child is now taking care of it). */
1373 close(newsock);
Damien Miller95def091999-11-25 00:26:21 +11001374 }
Damien Miller34132e52000-01-14 15:45:46 +11001375 /* child process check (or debug mode) */
1376 if (num_listen_socks < 0)
1377 break;
Damien Miller95def091999-11-25 00:26:21 +11001378 }
1379 }
1380
1381 /* This is the child processing a new connection. */
Damien Miller57aae982004-03-08 23:11:25 +11001382 setproctitle("%s", "[accepted]");
Damien Miller95def091999-11-25 00:26:21 +11001383
Damien Miller5428f641999-11-25 11:54:57 +11001384 /*
Ben Lindstrom17401b62002-05-15 16:17:56 +00001385 * Create a new session and process group since the 4.4BSD
1386 * setlogin() affects the entire process group. We don't
1387 * want the child to be able to affect the parent.
1388 */
Darren Tuckerc437cda2003-05-10 17:05:46 +10001389#if !defined(SSHD_ACQUIRES_CTTY)
Damien Miller933cc8f2003-03-10 11:38:10 +11001390 /*
Darren Tuckerc437cda2003-05-10 17:05:46 +10001391 * If setsid is called, on some platforms sshd will later acquire a
1392 * controlling terminal which will result in "could not set
1393 * controlling tty" errors.
Damien Miller933cc8f2003-03-10 11:38:10 +11001394 */
Ben Lindstrom57f08002002-06-23 00:37:10 +00001395 if (!debug_flag && !inetd_flag && setsid() < 0)
Ben Lindstrom17401b62002-05-15 16:17:56 +00001396 error("setsid: %.100s", strerror(errno));
Kevin Stevesc5041ac2002-05-21 17:50:21 +00001397#endif
Ben Lindstrom17401b62002-05-15 16:17:56 +00001398
1399 /*
Damien Miller5428f641999-11-25 11:54:57 +11001400 * Disable the key regeneration alarm. We will not regenerate the
1401 * key since we are no longer in a position to give it to anyone. We
1402 * will not restart on SIGHUP since it no longer makes sense.
1403 */
Damien Miller95def091999-11-25 00:26:21 +11001404 alarm(0);
1405 signal(SIGALRM, SIG_DFL);
1406 signal(SIGHUP, SIG_DFL);
1407 signal(SIGTERM, SIG_DFL);
1408 signal(SIGQUIT, SIG_DFL);
1409 signal(SIGCHLD, SIG_DFL);
Damien Miller4e0f5e12000-08-29 11:05:50 +11001410 signal(SIGINT, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001411
Damien Miller12c150e2003-12-17 16:31:10 +11001412 /* Set SO_KEEPALIVE if requested. */
1413 if (options.tcp_keep_alive &&
Ben Lindstrom733a2352002-03-05 01:31:28 +00001414 setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on,
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001415 sizeof(on)) < 0)
1416 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1417
Damien Miller5428f641999-11-25 11:54:57 +11001418 /*
1419 * Register our connection. This turns encryption off because we do
1420 * not have a key.
1421 */
Damien Miller95def091999-11-25 00:26:21 +11001422 packet_set_connection(sock_in, sock_out);
1423
1424 remote_port = get_remote_port();
1425 remote_ip = get_remote_ipaddr();
1426
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001427#ifdef LIBWRAP
Damien Miller6a4a4b92001-11-12 11:07:11 +11001428 /* Check whether logins are denied from this host. */
Damien Miller95def091999-11-25 00:26:21 +11001429 {
1430 struct request_info req;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001431
Ben Lindstromce89dac2001-09-12 16:58:04 +00001432 request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, 0);
Damien Miller95def091999-11-25 00:26:21 +11001433 fromhost(&req);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001434
Damien Miller95def091999-11-25 00:26:21 +11001435 if (!hosts_access(&req)) {
Damien Miller6a4a4b92001-11-12 11:07:11 +11001436 debug("Connection refused by tcp wrapper");
Ben Lindstrom7de696e2001-03-29 00:45:12 +00001437 refuse(&req);
Damien Miller6a4a4b92001-11-12 11:07:11 +11001438 /* NOTREACHED */
1439 fatal("libwrap refuse returns");
Damien Miller95def091999-11-25 00:26:21 +11001440 }
Damien Miller95def091999-11-25 00:26:21 +11001441 }
Damien Miller34132e52000-01-14 15:45:46 +11001442#endif /* LIBWRAP */
Damien Miller6a4a4b92001-11-12 11:07:11 +11001443
Damien Miller95def091999-11-25 00:26:21 +11001444 /* Log the connection. */
1445 verbose("Connection from %.500s port %d", remote_ip, remote_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001446
Damien Miller5428f641999-11-25 11:54:57 +11001447 /*
1448 * We don\'t want to listen forever unless the other side
1449 * successfully authenticates itself. So we set up an alarm which is
1450 * cleared after successful authentication. A limit of zero
1451 * indicates no limit. Note that we don\'t set the alarm in debugging
1452 * mode; it is just annoying to have the server exit just when you
1453 * are about to discover the bug.
1454 */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00001455 signal(SIGALRM, grace_alarm_handler);
Damien Miller95def091999-11-25 00:26:21 +11001456 if (!debug_flag)
1457 alarm(options.login_grace_time);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001458
Damien Millerb38eff82000-04-01 11:09:21 +10001459 sshd_exchange_identification(sock_in, sock_out);
Darren Tuckerec960f22003-08-13 20:37:05 +10001460
Damien Miller95def091999-11-25 00:26:21 +11001461 packet_set_nonblocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001462
Damien Millera8e06ce2003-11-21 23:48:55 +11001463 /* prepare buffers to collect authentication messages */
Darren Tuckerb9aa0a02003-07-08 22:59:59 +10001464 buffer_init(&loginmsg);
1465
Darren Tucker3e33cec2003-10-02 16:12:36 +10001466 /* allocate authentication context */
1467 authctxt = xmalloc(sizeof(*authctxt));
1468 memset(authctxt, 0, sizeof(*authctxt));
1469
1470 /* XXX global for cleanup, access from other modules */
1471 the_authctxt = authctxt;
1472
Ben Lindstrom943481c2002-03-22 03:43:46 +00001473 if (use_privsep)
Darren Tucker3e33cec2003-10-02 16:12:36 +10001474 if (privsep_preauth(authctxt) == 1)
Ben Lindstrom943481c2002-03-22 03:43:46 +00001475 goto authenticated;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001476
Damien Miller396691a2000-01-20 22:44:08 +11001477 /* perform the key exchange */
Damien Miller396691a2000-01-20 22:44:08 +11001478 /* authenticate user and start session */
Damien Millerefb4afe2000-04-12 18:45:05 +10001479 if (compat20) {
1480 do_ssh2_kex();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001481 do_authentication2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001482 } else {
1483 do_ssh1_kex();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001484 do_authentication(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001485 }
Ben Lindstrom943481c2002-03-22 03:43:46 +00001486 /*
1487 * If we use privilege separation, the unprivileged child transfers
1488 * the current keystate and exits
1489 */
1490 if (use_privsep) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001491 mm_send_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492 exit(0);
Ben Lindstrom943481c2002-03-22 03:43:46 +00001493 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001494
1495 authenticated:
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001496 /*
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001497 * In privilege separation, we fork another child and prepare
1498 * file descriptor passing.
1499 */
1500 if (use_privsep) {
Ben Lindstrom943481c2002-03-22 03:43:46 +00001501 privsep_postauth(authctxt);
1502 /* the monitor process [priv] will not return */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001503 if (!compat20)
1504 destroy_sensitive_data();
1505 }
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00001506
Darren Tucker3e33cec2003-10-02 16:12:36 +10001507 /* Start session. */
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00001508 do_authenticated(authctxt);
1509
Damien Miller3a5b0232002-03-13 13:19:42 +11001510 /* The connection has been terminated. */
1511 verbose("Closing connection to %.100s", remote_ip);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001512
Damien Millerbeb4ba51999-12-28 15:09:35 +11001513#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +10001514 if (options.use_pam)
1515 finish_pam();
Damien Millerbeb4ba51999-12-28 15:09:35 +11001516#endif /* USE_PAM */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001517
Damien Miller95def091999-11-25 00:26:21 +11001518 packet_close();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001519
1520 if (use_privsep)
1521 mm_terminate();
1522
Damien Miller95def091999-11-25 00:26:21 +11001523 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001524}
1525
Damien Miller95def091999-11-25 00:26:21 +11001526/*
Ben Lindstromabcb1452002-03-22 01:10:21 +00001527 * Decrypt session_key_int using our private server key and private host key
1528 * (key with larger modulus first).
1529 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001530int
Ben Lindstromabcb1452002-03-22 01:10:21 +00001531ssh1_session_key(BIGNUM *session_key_int)
1532{
1533 int rsafail = 0;
1534
1535 if (BN_cmp(sensitive_data.server_key->rsa->n, sensitive_data.ssh1_host_key->rsa->n) > 0) {
1536 /* Server key has bigger modulus. */
1537 if (BN_num_bits(sensitive_data.server_key->rsa->n) <
1538 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1539 fatal("do_connection: %s: server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
1540 get_remote_ipaddr(),
1541 BN_num_bits(sensitive_data.server_key->rsa->n),
1542 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1543 SSH_KEY_BITS_RESERVED);
1544 }
1545 if (rsa_private_decrypt(session_key_int, session_key_int,
1546 sensitive_data.server_key->rsa) <= 0)
1547 rsafail++;
1548 if (rsa_private_decrypt(session_key_int, session_key_int,
1549 sensitive_data.ssh1_host_key->rsa) <= 0)
1550 rsafail++;
1551 } else {
1552 /* Host key has bigger modulus (or they are equal). */
1553 if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
1554 BN_num_bits(sensitive_data.server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1555 fatal("do_connection: %s: host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
1556 get_remote_ipaddr(),
1557 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1558 BN_num_bits(sensitive_data.server_key->rsa->n),
1559 SSH_KEY_BITS_RESERVED);
1560 }
1561 if (rsa_private_decrypt(session_key_int, session_key_int,
1562 sensitive_data.ssh1_host_key->rsa) < 0)
1563 rsafail++;
1564 if (rsa_private_decrypt(session_key_int, session_key_int,
1565 sensitive_data.server_key->rsa) < 0)
1566 rsafail++;
1567 }
1568 return (rsafail);
1569}
1570/*
Damien Miller396691a2000-01-20 22:44:08 +11001571 * SSH1 key exchange
Damien Miller95def091999-11-25 00:26:21 +11001572 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001573static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001574do_ssh1_kex(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001575{
Damien Miller95def091999-11-25 00:26:21 +11001576 int i, len;
Damien Miller7650bc62001-01-30 09:27:26 +11001577 int rsafail = 0;
Damien Miller95def091999-11-25 00:26:21 +11001578 BIGNUM *session_key_int;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001579 u_char session_key[SSH_SESSION_KEY_LENGTH];
1580 u_char cookie[8];
1581 u_int cipher_type, auth_mask, protocol_flags;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001582 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001583
Damien Miller5428f641999-11-25 11:54:57 +11001584 /*
1585 * Generate check bytes that the client must send back in the user
1586 * packet in order for it to be accepted; this is used to defy ip
1587 * spoofing attacks. Note that this only works against somebody
1588 * doing IP spoofing from a remote machine; any machine on the local
1589 * network can still see outgoing packets and catch the random
1590 * cookie. This only affects rhosts authentication, and this is one
1591 * of the reasons why it is inherently insecure.
1592 */
Damien Miller95def091999-11-25 00:26:21 +11001593 for (i = 0; i < 8; i++) {
1594 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001595 rnd = arc4random();
1596 cookie[i] = rnd & 0xff;
1597 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +11001598 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001599
Damien Miller5428f641999-11-25 11:54:57 +11001600 /*
1601 * Send our public key. We include in the packet 64 bits of random
1602 * data that must be matched in the reply in order to prevent IP
1603 * spoofing.
1604 */
Damien Miller95def091999-11-25 00:26:21 +11001605 packet_start(SSH_SMSG_PUBLIC_KEY);
1606 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001607 packet_put_char(cookie[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001608
Damien Miller95def091999-11-25 00:26:21 +11001609 /* Store our public server RSA key. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001610 packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
1611 packet_put_bignum(sensitive_data.server_key->rsa->e);
1612 packet_put_bignum(sensitive_data.server_key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001613
Damien Miller95def091999-11-25 00:26:21 +11001614 /* Store our public host RSA key. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001615 packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1616 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
1617 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001618
Damien Miller95def091999-11-25 00:26:21 +11001619 /* Put protocol flags. */
1620 packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001621
Damien Miller95def091999-11-25 00:26:21 +11001622 /* Declare which ciphers we support. */
Damien Miller874d77b2000-10-14 16:23:11 +11001623 packet_put_int(cipher_mask_ssh1(0));
Damien Miller95def091999-11-25 00:26:21 +11001624
1625 /* Declare supported authentication types. */
1626 auth_mask = 0;
Damien Miller95def091999-11-25 00:26:21 +11001627 if (options.rhosts_rsa_authentication)
1628 auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
1629 if (options.rsa_authentication)
1630 auth_mask |= 1 << SSH_AUTH_RSA;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001631 if (options.challenge_response_authentication == 1)
Damien Miller95def091999-11-25 00:26:21 +11001632 auth_mask |= 1 << SSH_AUTH_TIS;
Damien Miller95def091999-11-25 00:26:21 +11001633 if (options.password_authentication)
1634 auth_mask |= 1 << SSH_AUTH_PASSWORD;
1635 packet_put_int(auth_mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001636
Damien Miller95def091999-11-25 00:26:21 +11001637 /* Send the packet and wait for it to be sent. */
1638 packet_send();
1639 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001640
Damien Miller0bc1bd82000-11-13 22:57:25 +11001641 debug("Sent %d bit server key and %d bit host key.",
1642 BN_num_bits(sensitive_data.server_key->rsa->n),
1643 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001644
Damien Miller95def091999-11-25 00:26:21 +11001645 /* Read clients reply (cipher type and session key). */
Damien Millerdff50992002-01-22 23:16:32 +11001646 packet_read_expect(SSH_CMSG_SESSION_KEY);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001647
Damien Miller50945fa1999-12-09 10:31:37 +11001648 /* Get cipher type and check whether we accept this. */
Damien Miller95def091999-11-25 00:26:21 +11001649 cipher_type = packet_get_char();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001650
Damien Miller874d77b2000-10-14 16:23:11 +11001651 if (!(cipher_mask_ssh1(0) & (1 << cipher_type)))
Damien Miller50945fa1999-12-09 10:31:37 +11001652 packet_disconnect("Warning: client selects unsupported cipher.");
1653
Damien Miller95def091999-11-25 00:26:21 +11001654 /* Get check bytes from the packet. These must match those we
1655 sent earlier with the public key packet. */
1656 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001657 if (cookie[i] != packet_get_char())
Damien Miller95def091999-11-25 00:26:21 +11001658 packet_disconnect("IP Spoofing check bytes do not match.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001659
Damien Miller95def091999-11-25 00:26:21 +11001660 debug("Encryption type: %.200s", cipher_name(cipher_type));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001661
Damien Miller95def091999-11-25 00:26:21 +11001662 /* Get the encrypted integer. */
Damien Millerda755162002-01-22 23:09:22 +11001663 if ((session_key_int = BN_new()) == NULL)
1664 fatal("do_ssh1_kex: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +11001665 packet_get_bignum(session_key_int);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001666
Damien Miller95def091999-11-25 00:26:21 +11001667 protocol_flags = packet_get_int();
1668 packet_set_protocol_flags(protocol_flags);
Damien Miller48b03fc2002-01-22 23:11:40 +11001669 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001670
Ben Lindstromabcb1452002-03-22 01:10:21 +00001671 /* Decrypt session_key_int using host/server keys */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001672 rsafail = PRIVSEP(ssh1_session_key(session_key_int));
1673
Damien Miller5428f641999-11-25 11:54:57 +11001674 /*
1675 * Extract session key from the decrypted integer. The key is in the
1676 * least significant 256 bits of the integer; the first byte of the
1677 * key is in the highest bits.
1678 */
Damien Miller7650bc62001-01-30 09:27:26 +11001679 if (!rsafail) {
1680 BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1681 len = BN_num_bytes(session_key_int);
1682 if (len < 0 || len > sizeof(session_key)) {
1683 error("do_connection: bad session key len from %s: "
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001684 "session_key_int %d > sizeof(session_key) %lu",
1685 get_remote_ipaddr(), len, (u_long)sizeof(session_key));
Damien Miller7650bc62001-01-30 09:27:26 +11001686 rsafail++;
1687 } else {
1688 memset(session_key, 0, sizeof(session_key));
1689 BN_bn2bin(session_key_int,
1690 session_key + sizeof(session_key) - len);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001691
1692 compute_session_id(session_id, cookie,
1693 sensitive_data.ssh1_host_key->rsa->n,
1694 sensitive_data.server_key->rsa->n);
1695 /*
1696 * Xor the first 16 bytes of the session key with the
1697 * session id.
1698 */
1699 for (i = 0; i < 16; i++)
1700 session_key[i] ^= session_id[i];
Damien Miller7650bc62001-01-30 09:27:26 +11001701 }
1702 }
1703 if (rsafail) {
Ben Lindstromeb648a72001-03-05 06:00:29 +00001704 int bytes = BN_num_bytes(session_key_int);
Ben Lindstrom13c5d3b2002-02-26 18:00:48 +00001705 u_char *buf = xmalloc(bytes);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001706 MD5_CTX md;
1707
Damien Miller996acd22003-04-09 20:59:48 +10001708 logit("do_connection: generating a fake encryption key");
Ben Lindstromeb648a72001-03-05 06:00:29 +00001709 BN_bn2bin(session_key_int, buf);
1710 MD5_Init(&md);
1711 MD5_Update(&md, buf, bytes);
1712 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1713 MD5_Final(session_key, &md);
1714 MD5_Init(&md);
1715 MD5_Update(&md, session_key, 16);
1716 MD5_Update(&md, buf, bytes);
1717 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1718 MD5_Final(session_key + 16, &md);
1719 memset(buf, 0, bytes);
1720 xfree(buf);
Ben Lindstrom941ac822001-03-05 06:25:23 +00001721 for (i = 0; i < 16; i++)
1722 session_id[i] = session_key[i] ^ session_key[i + 16];
Damien Miller7650bc62001-01-30 09:27:26 +11001723 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001724 /* Destroy the private and public keys. No longer. */
Damien Miller3a5b0232002-03-13 13:19:42 +11001725 destroy_sensitive_data();
Ben Lindstromeb648a72001-03-05 06:00:29 +00001726
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001727 if (use_privsep)
1728 mm_ssh1_session_id(session_id);
1729
Damien Miller396691a2000-01-20 22:44:08 +11001730 /* Destroy the decrypted integer. It is no longer needed. */
1731 BN_clear_free(session_key_int);
1732
Damien Miller95def091999-11-25 00:26:21 +11001733 /* Set the session key. From this on all communications will be encrypted. */
1734 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001735
Damien Miller95def091999-11-25 00:26:21 +11001736 /* Destroy our copy of the session key. It is no longer needed. */
1737 memset(session_key, 0, sizeof(session_key));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001738
Damien Miller95def091999-11-25 00:26:21 +11001739 debug("Received session key; encryption turned on.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001740
Ben Lindstromf666fec2002-06-06 19:51:58 +00001741 /* Send an acknowledgment packet. Note that this packet is sent encrypted. */
Damien Miller95def091999-11-25 00:26:21 +11001742 packet_start(SSH_SMSG_SUCCESS);
1743 packet_send();
1744 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001745}
Damien Millerefb4afe2000-04-12 18:45:05 +10001746
1747/*
1748 * SSH2 key exchange: diffie-hellman-group1-sha1
1749 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001750static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001751do_ssh2_kex(void)
Damien Millerefb4afe2000-04-12 18:45:05 +10001752{
Damien Millerefb4afe2000-04-12 18:45:05 +10001753 Kex *kex;
Damien Millerefb4afe2000-04-12 18:45:05 +10001754
Damien Miller78928792000-04-12 20:17:38 +10001755 if (options.ciphers != NULL) {
Damien Miller4af51302000-04-16 11:18:38 +10001756 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Damien Miller78928792000-04-12 20:17:38 +10001757 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
1758 }
Damien Millera0ff4662001-03-30 10:49:35 +10001759 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1760 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
1761 myproposal[PROPOSAL_ENC_ALGS_STOC] =
1762 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
1763
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001764 if (options.macs != NULL) {
1765 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
1766 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1767 }
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001768 if (!options.compression) {
1769 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1770 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
1771 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001772 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = list_hostkey_types();
1773
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001774 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +00001775 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +11001776 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1777 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00001778 kex->server = 1;
1779 kex->client_version_string=client_version_string;
1780 kex->server_version_string=server_version_string;
1781 kex->load_host_key=&get_hostkey_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001782 kex->host_key_index=&get_hostkey_index;
Damien Millerefb4afe2000-04-12 18:45:05 +10001783
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001784 xxx_kex = kex;
1785
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001786 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +11001787
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001788 session_id2 = kex->session_id;
1789 session_id2_len = kex->session_id_len;
1790
Damien Miller874d77b2000-10-14 16:23:11 +11001791#ifdef DEBUG_KEXDH
1792 /* send 1st encrypted/maced/compressed message */
1793 packet_start(SSH2_MSG_IGNORE);
1794 packet_put_cstring("markus");
1795 packet_send();
1796 packet_write_wait();
1797#endif
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00001798 debug("KEX done");
Damien Millerefb4afe2000-04-12 18:45:05 +10001799}
Darren Tucker3e33cec2003-10-02 16:12:36 +10001800
1801/* server specific fatal cleanup */
1802void
1803cleanup_exit(int i)
1804{
1805 if (the_authctxt)
1806 do_cleanup(the_authctxt);
1807 _exit(i);
1808}