blob: 28e8c1aa3dee92c1ecd0797c0c769a867be020d0 [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 Miller17e91c02006-03-15 11:28:34 +110045
Damien Miller9cf6d072006-03-15 11:29:24 +110046#include <sys/types.h>
Damien Millerf17883e2006-03-15 11:45:54 +110047#ifdef HAVE_SYS_STAT_H
48# include <sys/stat.h>
49#endif
50#include <sys/ioctl.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110051#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110052
Damien Miller6645e7a2006-03-15 14:42:54 +110053#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110054#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110055#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110056#include <signal.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include <openssl/dh.h>
59#include <openssl/bn.h>
Damien Miller6a47f302002-02-13 13:55:06 +110060#include <openssl/md5.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000061#include <openssl/rand.h>
Kevin Steves0ea1d9d2002-04-25 18:17:04 +000062#ifdef HAVE_SECUREWARE
63#include <sys/security.h>
64#include <prot.h>
65#endif
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066
67#include "ssh.h"
68#include "ssh1.h"
69#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070#include "xmalloc.h"
71#include "rsa.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000072#include "sshpty.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000074#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075#include "servconf.h"
76#include "uidswap.h"
77#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100078#include "buffer.h"
Darren Tucker645ab752004-06-25 13:33:20 +100079#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "cipher.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100081#include "kex.h"
Damien Millerb38eff82000-04-01 11:09:21 +100082#include "key.h"
Damien Miller874d77b2000-10-14 16:23:11 +110083#include "dh.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100084#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100085#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000086#include "pathnames.h"
87#include "atomicio.h"
88#include "canohost.h"
89#include "auth.h"
90#include "misc.h"
Darren Tucker645ab752004-06-25 13:33:20 +100091#include "msg.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000092#include "dispatch.h"
Ben Lindstrom1bae4042001-10-03 17:46:39 +000093#include "channels.h"
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +000094#include "session.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095#include "monitor_mm.h"
96#include "monitor.h"
97#include "monitor_wrap.h"
98#include "monitor_fdpass.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
100#ifdef LIBWRAP
101#include <tcpd.h>
102#include <syslog.h>
103int allow_severity = LOG_INFO;
104int deny_severity = LOG_WARNING;
105#endif /* LIBWRAP */
106
107#ifndef O_NOCTTY
108#define O_NOCTTY 0
109#endif
110
Damien Miller035a5b42004-06-26 08:16:31 +1000111/* Re-exec fds */
112#define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1)
113#define REEXEC_STARTUP_PIPE_FD (STDERR_FILENO + 2)
114#define REEXEC_CONFIG_PASS_FD (STDERR_FILENO + 3)
115#define REEXEC_MIN_FREE_FD (STDERR_FILENO + 4)
116
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000117extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000118
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119/* Server configuration options. */
120ServerOptions options;
121
122/* Name of the server configuration file. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000123char *config_file_name = _PATH_SERVER_CONFIG_FILE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124
Damien Miller4af51302000-04-16 11:18:38 +1000125/*
Damien Miller95def091999-11-25 00:26:21 +1100126 * Debug mode flag. This can be set on the command line. If debug
127 * mode is enabled, extra debugging output will be sent to the system
128 * log, the daemon will not go to background, and will exit after processing
129 * the first connection.
130 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131int debug_flag = 0;
132
Ben Lindstrom794325a2001-08-06 21:09:07 +0000133/* Flag indicating that the daemon should only test the configuration and keys. */
134int test_flag = 0;
135
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136/* Flag indicating that the daemon is being started from inetd. */
137int inetd_flag = 0;
138
Ben Lindstromc72745a2000-12-02 19:03:54 +0000139/* Flag indicating that sshd should not detach and become a daemon. */
140int no_daemon_flag = 0;
141
Damien Miller5ce662a1999-11-11 17:57:39 +1100142/* debug goes to stderr unless inetd_flag is set */
143int log_stderr = 0;
144
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145/* Saved arguments to main(). */
146char **saved_argv;
Damien Millerb8c656e2000-06-28 15:22:41 +1000147int saved_argc;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148
Darren Tucker645ab752004-06-25 13:33:20 +1000149/* re-exec */
150int rexeced_flag = 0;
151int rexec_flag = 1;
152int rexec_argc = 0;
153char **rexec_argv;
154
Damien Miller5428f641999-11-25 11:54:57 +1100155/*
Damien Miller34132e52000-01-14 15:45:46 +1100156 * The sockets that the server is listening; this is used in the SIGHUP
157 * signal handler.
Damien Miller5428f641999-11-25 11:54:57 +1100158 */
Damien Miller34132e52000-01-14 15:45:46 +1100159#define MAX_LISTEN_SOCKS 16
160int listen_socks[MAX_LISTEN_SOCKS];
161int num_listen_socks = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162
Damien Miller5428f641999-11-25 11:54:57 +1100163/*
164 * the client's version string, passed by sshd2 in compat mode. if != NULL,
165 * sshd will skip the version-number exchange
166 */
Damien Miller95def091999-11-25 00:26:21 +1100167char *client_version_string = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000168char *server_version_string = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000170/* for rekeying XXX fixme */
171Kex *xxx_kex;
172
Damien Miller5428f641999-11-25 11:54:57 +1100173/*
174 * Any really sensitive data in the application is contained in this
175 * structure. The idea is that this structure could be locked into memory so
176 * that the pages do not get written into swap. However, there are some
177 * problems. The private key contains BIGNUMs, and we do not (in principle)
178 * have access to the internals of them, and locking just the structure is
179 * not very useful. Currently, memory locking is not implemented.
180 */
Damien Miller95def091999-11-25 00:26:21 +1100181struct {
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000182 Key *server_key; /* ephemeral server key */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100183 Key *ssh1_host_key; /* ssh1 host key */
184 Key **host_keys; /* all private host keys */
185 int have_ssh1_key;
186 int have_ssh2_key;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000187 u_char ssh1_cookie[SSH_SESSION_KEY_LENGTH];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188} sensitive_data;
189
Damien Miller5428f641999-11-25 11:54:57 +1100190/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000191 * Flag indicating whether the RSA server key needs to be regenerated.
192 * Is set in the SIGALRM handler and cleared when the key is regenerated.
Damien Miller5428f641999-11-25 11:54:57 +1100193 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000194static volatile sig_atomic_t key_do_regen = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000196/* This is set to true when a signal is received. */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000197static volatile sig_atomic_t received_sighup = 0;
198static volatile sig_atomic_t received_sigterm = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199
Damien Millerb38eff82000-04-01 11:09:21 +1000200/* session identifier, used by RSA-auth */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000201u_char session_id[16];
Damien Millerb38eff82000-04-01 11:09:21 +1000202
Damien Millereba71ba2000-04-29 23:57:08 +1000203/* same for ssh2 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000204u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000205u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000206
Damien Miller942da032000-08-18 13:59:06 +1000207/* record remote hostname or ip */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000208u_int utmp_len = MAXHOSTNAMELEN;
Damien Miller942da032000-08-18 13:59:06 +1000209
Ben Lindstromd84df982001-12-06 16:35:40 +0000210/* options.max_startup sized array of fd ints */
211int *startup_pipes = NULL;
212int startup_pipe; /* in child */
213
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000214/* variables used for privilege separation */
Damien Miller8e7fb332003-02-24 12:03:03 +1100215int use_privsep;
Darren Tuckera8be9e22004-02-06 16:40:27 +1100216struct monitor *pmonitor = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000217
Darren Tucker3e33cec2003-10-02 16:12:36 +1000218/* global authentication context */
219Authctxt *the_authctxt = NULL;
220
Darren Tucker09991742004-07-17 17:05:14 +1000221/* message to be displayed after login */
222Buffer loginmsg;
223
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224/* Prototypes for various functions defined later in this file. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000225void destroy_sensitive_data(void);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000226void demote_sensitive_data(void);
Damien Miller98c7ad62000-03-09 21:27:49 +1100227
Ben Lindstrombba81212001-06-25 05:01:22 +0000228static void do_ssh1_kex(void);
229static void do_ssh2_kex(void);
Damien Miller874d77b2000-10-14 16:23:11 +1100230
Damien Miller98c7ad62000-03-09 21:27:49 +1100231/*
Damien Miller34132e52000-01-14 15:45:46 +1100232 * Close all listening sockets
233 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000234static void
Damien Miller34132e52000-01-14 15:45:46 +1100235close_listen_socks(void)
236{
237 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000238
Damien Miller34132e52000-01-14 15:45:46 +1100239 for (i = 0; i < num_listen_socks; i++)
240 close(listen_socks[i]);
241 num_listen_socks = -1;
242}
243
Ben Lindstromd84df982001-12-06 16:35:40 +0000244static void
245close_startup_pipes(void)
246{
247 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000248
Ben Lindstromd84df982001-12-06 16:35:40 +0000249 if (startup_pipes)
250 for (i = 0; i < options.max_startups; i++)
251 if (startup_pipes[i] != -1)
252 close(startup_pipes[i]);
253}
254
Damien Miller34132e52000-01-14 15:45:46 +1100255/*
Damien Miller95def091999-11-25 00:26:21 +1100256 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
257 * the effect is to reread the configuration file (and to regenerate
258 * the server key).
259 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100260
261/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000262static void
Damien Miller95def091999-11-25 00:26:21 +1100263sighup_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264{
Ben Lindstrom07958482001-12-06 16:19:01 +0000265 int save_errno = errno;
266
Damien Miller95def091999-11-25 00:26:21 +1100267 received_sighup = 1;
268 signal(SIGHUP, sighup_handler);
Ben Lindstrom07958482001-12-06 16:19:01 +0000269 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270}
271
Damien Miller95def091999-11-25 00:26:21 +1100272/*
273 * Called from the main program after receiving SIGHUP.
274 * Restarts the server.
275 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000276static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000277sighup_restart(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278{
Damien Miller996acd22003-04-09 20:59:48 +1000279 logit("Received SIGHUP; restarting.");
Damien Miller34132e52000-01-14 15:45:46 +1100280 close_listen_socks();
Ben Lindstromd84df982001-12-06 16:35:40 +0000281 close_startup_pipes();
Damien Miller95def091999-11-25 00:26:21 +1100282 execv(saved_argv[0], saved_argv);
Damien Miller996acd22003-04-09 20:59:48 +1000283 logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
Ben Lindstrom822b6342002-06-23 21:38:49 +0000284 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100285 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286}
287
Damien Miller95def091999-11-25 00:26:21 +1100288/*
289 * Generic signal handler for terminating signals in the master daemon.
Damien Miller95def091999-11-25 00:26:21 +1100290 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100291/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000292static void
Damien Miller95def091999-11-25 00:26:21 +1100293sigterm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000295 received_sigterm = sig;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296}
297
Damien Miller95def091999-11-25 00:26:21 +1100298/*
299 * SIGCHLD handler. This is called whenever a child dies. This will then
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000300 * reap any zombies left by exited children.
Damien Miller95def091999-11-25 00:26:21 +1100301 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100302/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000303static void
Damien Miller95def091999-11-25 00:26:21 +1100304main_sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305{
Damien Miller95def091999-11-25 00:26:21 +1100306 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000307 pid_t pid;
Damien Miller95def091999-11-25 00:26:21 +1100308 int status;
Damien Miller431f66b1999-11-21 18:31:57 +1100309
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000310 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
311 (pid < 0 && errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100312 ;
Damien Miller431f66b1999-11-21 18:31:57 +1100313
Damien Miller95def091999-11-25 00:26:21 +1100314 signal(SIGCHLD, main_sigchld_handler);
315 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316}
317
Damien Miller95def091999-11-25 00:26:21 +1100318/*
319 * Signal handler for the alarm after the login grace period has expired.
320 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100321/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000322static void
Damien Miller95def091999-11-25 00:26:21 +1100323grace_alarm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000325 /* XXX no idea how fix this signal handler */
326
Darren Tuckera8be9e22004-02-06 16:40:27 +1100327 if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0)
328 kill(pmonitor->m_pid, SIGALRM);
329
Damien Miller95def091999-11-25 00:26:21 +1100330 /* Log error and exit. */
Damien Millerd27a76d2002-09-27 13:22:31 +1000331 fatal("Timeout before authentication for %s", get_remote_ipaddr());
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332}
333
Damien Miller95def091999-11-25 00:26:21 +1100334/*
Damien Miller95def091999-11-25 00:26:21 +1100335 * Signal handler for the key regeneration alarm. Note that this
336 * alarm only occurs in the daemon waiting for connections, and it does not
337 * do anything with the private key or random state before forking.
338 * Thus there should be no concurrency control/asynchronous execution
339 * problems.
340 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000341static void
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000342generate_ephemeral_server_key(void)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100343{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000344 u_int32_t rnd = 0;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000345 int i;
346
Ben Lindstroma3700052001-04-05 23:26:32 +0000347 verbose("Generating %s%d bit RSA key.",
Damien Millerff75ac42001-03-30 10:50:32 +1000348 sensitive_data.server_key ? "new " : "", options.server_key_bits);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100349 if (sensitive_data.server_key != NULL)
350 key_free(sensitive_data.server_key);
Ben Lindstroma3700052001-04-05 23:26:32 +0000351 sensitive_data.server_key = key_generate(KEY_RSA1,
Damien Millerff75ac42001-03-30 10:50:32 +1000352 options.server_key_bits);
353 verbose("RSA key generation complete.");
Ben Lindstromeb648a72001-03-05 06:00:29 +0000354
355 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
356 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000357 rnd = arc4random();
358 sensitive_data.ssh1_cookie[i] = rnd & 0xff;
359 rnd >>= 8;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000360 }
361 arc4random_stir();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100362}
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000363
Damien Millerf0b15df2006-03-26 13:59:20 +1100364/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000365static void
Damien Miller95def091999-11-25 00:26:21 +1100366key_regeneration_alarm(int sig)
367{
368 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000369
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000370 signal(SIGALRM, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +1100371 errno = save_errno;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000372 key_do_regen = 1;
Damien Miller95def091999-11-25 00:26:21 +1100373}
374
Ben Lindstrombba81212001-06-25 05:01:22 +0000375static void
Damien Millerb38eff82000-04-01 11:09:21 +1000376sshd_exchange_identification(int sock_in, int sock_out)
377{
Damien Millereccb9de2005-06-17 12:59:34 +1000378 u_int i;
379 int mismatch;
Damien Millerb38eff82000-04-01 11:09:21 +1000380 int remote_major, remote_minor;
Damien Miller78928792000-04-12 20:17:38 +1000381 int major, minor;
Damien Millerb38eff82000-04-01 11:09:21 +1000382 char *s;
383 char buf[256]; /* Must not be larger than remote_version. */
384 char remote_version[256]; /* Must be at least as big as buf. */
385
Damien Miller78928792000-04-12 20:17:38 +1000386 if ((options.protocol & SSH_PROTO_1) &&
387 (options.protocol & SSH_PROTO_2)) {
388 major = PROTOCOL_MAJOR_1;
389 minor = 99;
390 } else if (options.protocol & SSH_PROTO_2) {
391 major = PROTOCOL_MAJOR_2;
392 minor = PROTOCOL_MINOR_2;
393 } else {
394 major = PROTOCOL_MAJOR_1;
395 minor = PROTOCOL_MINOR_1;
396 }
397 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
Damien Millerb38eff82000-04-01 11:09:21 +1000398 server_version_string = xstrdup(buf);
399
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000400 /* Send our protocol version identification. */
401 if (atomicio(vwrite, sock_out, server_version_string,
402 strlen(server_version_string))
403 != strlen(server_version_string)) {
404 logit("Could not write ident string to %s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000405 cleanup_exit(255);
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000406 }
407
408 /* Read other sides version identification. */
409 memset(buf, 0, sizeof(buf));
410 for (i = 0; i < sizeof(buf) - 1; i++) {
411 if (atomicio(read, sock_in, &buf[i], 1) != 1) {
412 logit("Did not receive identification string from %s",
413 get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000414 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000415 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000416 if (buf[i] == '\r') {
417 buf[i] = 0;
418 /* Kludge for F-Secure Macintosh < 1.0.2 */
419 if (i == 12 &&
420 strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000421 break;
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000422 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000423 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000424 if (buf[i] == '\n') {
425 buf[i] = 0;
426 break;
427 }
Damien Millerb38eff82000-04-01 11:09:21 +1000428 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000429 buf[sizeof(buf) - 1] = 0;
430 client_version_string = xstrdup(buf);
Damien Millerb38eff82000-04-01 11:09:21 +1000431
432 /*
433 * Check that the versions match. In future this might accept
434 * several versions and set appropriate flags to handle them.
435 */
436 if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
437 &remote_major, &remote_minor, remote_version) != 3) {
Damien Miller4af51302000-04-16 11:18:38 +1000438 s = "Protocol mismatch.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000439 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Millerb38eff82000-04-01 11:09:21 +1000440 close(sock_in);
441 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000442 logit("Bad protocol version identification '%.100s' from %s",
Damien Millerb38eff82000-04-01 11:09:21 +1000443 client_version_string, get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000444 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000445 }
446 debug("Client protocol version %d.%d; client software version %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100447 remote_major, remote_minor, remote_version);
Damien Millerb38eff82000-04-01 11:09:21 +1000448
Damien Millerefb4afe2000-04-12 18:45:05 +1000449 compat_datafellows(remote_version);
450
Damien Millere9264972002-09-30 11:59:21 +1000451 if (datafellows & SSH_BUG_PROBE) {
Damien Miller996acd22003-04-09 20:59:48 +1000452 logit("probed from %s with %s. Don't panic.",
Damien Millere9264972002-09-30 11:59:21 +1000453 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000454 cleanup_exit(255);
Damien Millere9264972002-09-30 11:59:21 +1000455 }
456
Damien Miller27dbe6f2001-03-19 22:36:20 +1100457 if (datafellows & SSH_BUG_SCANNER) {
Damien Miller996acd22003-04-09 20:59:48 +1000458 logit("scanned from %s with %s. Don't panic.",
Damien Miller27dbe6f2001-03-19 22:36:20 +1100459 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000460 cleanup_exit(255);
Damien Miller27dbe6f2001-03-19 22:36:20 +1100461 }
462
Damien Miller78928792000-04-12 20:17:38 +1000463 mismatch = 0;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000464 switch (remote_major) {
Damien Millerb38eff82000-04-01 11:09:21 +1000465 case 1:
Damien Millereba71ba2000-04-29 23:57:08 +1000466 if (remote_minor == 99) {
467 if (options.protocol & SSH_PROTO_2)
468 enable_compat20();
469 else
470 mismatch = 1;
471 break;
472 }
Damien Miller78928792000-04-12 20:17:38 +1000473 if (!(options.protocol & SSH_PROTO_1)) {
474 mismatch = 1;
475 break;
476 }
Damien Millerb38eff82000-04-01 11:09:21 +1000477 if (remote_minor < 3) {
Damien Miller37023962000-07-11 17:31:38 +1000478 packet_disconnect("Your ssh version is too old and "
Damien Millerb38eff82000-04-01 11:09:21 +1000479 "is no longer supported. Please install a newer version.");
480 } else if (remote_minor == 3) {
481 /* note that this disables agent-forwarding */
482 enable_compat13();
483 }
Damien Miller78928792000-04-12 20:17:38 +1000484 break;
Damien Millerefb4afe2000-04-12 18:45:05 +1000485 case 2:
Damien Miller78928792000-04-12 20:17:38 +1000486 if (options.protocol & SSH_PROTO_2) {
Damien Millerefb4afe2000-04-12 18:45:05 +1000487 enable_compat20();
488 break;
489 }
490 /* FALLTHROUGH */
Damien Miller4af51302000-04-16 11:18:38 +1000491 default:
Damien Miller78928792000-04-12 20:17:38 +1000492 mismatch = 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000493 break;
494 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000495 chop(server_version_string);
Damien Miller78928792000-04-12 20:17:38 +1000496 debug("Local version string %.200s", server_version_string);
497
498 if (mismatch) {
499 s = "Protocol major versions differ.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000500 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Miller78928792000-04-12 20:17:38 +1000501 close(sock_in);
502 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000503 logit("Protocol major versions differ for %s: %.200s vs. %.200s",
Damien Miller78928792000-04-12 20:17:38 +1000504 get_remote_ipaddr(),
505 server_version_string, client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000506 cleanup_exit(255);
Damien Miller78928792000-04-12 20:17:38 +1000507 }
Damien Millereba71ba2000-04-29 23:57:08 +1000508}
509
Damien Miller0bc1bd82000-11-13 22:57:25 +1100510/* Destroy the host and server keys. They will no longer be needed. */
Damien Millereba71ba2000-04-29 23:57:08 +1000511void
512destroy_sensitive_data(void)
513{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100514 int i;
515
516 if (sensitive_data.server_key) {
517 key_free(sensitive_data.server_key);
518 sensitive_data.server_key = NULL;
519 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100520 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100521 if (sensitive_data.host_keys[i]) {
522 key_free(sensitive_data.host_keys[i]);
523 sensitive_data.host_keys[i] = NULL;
524 }
525 }
526 sensitive_data.ssh1_host_key = NULL;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000527 memset(sensitive_data.ssh1_cookie, 0, SSH_SESSION_KEY_LENGTH);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100528}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100529
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000530/* Demote private to public keys for network child */
531void
532demote_sensitive_data(void)
533{
534 Key *tmp;
535 int i;
536
537 if (sensitive_data.server_key) {
538 tmp = key_demote(sensitive_data.server_key);
539 key_free(sensitive_data.server_key);
540 sensitive_data.server_key = tmp;
541 }
542
543 for (i = 0; i < options.num_host_key_files; i++) {
544 if (sensitive_data.host_keys[i]) {
545 tmp = key_demote(sensitive_data.host_keys[i]);
546 key_free(sensitive_data.host_keys[i]);
547 sensitive_data.host_keys[i] = tmp;
548 if (tmp->type == KEY_RSA1)
549 sensitive_data.ssh1_host_key = tmp;
550 }
551 }
552
553 /* We do not clear ssh1_host key and cookie. XXX - Okay Niels? */
554}
555
Ben Lindstrom08105192002-03-22 02:50:06 +0000556static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000557privsep_preauth_child(void)
558{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000559 u_int32_t rnd[256];
Ben Lindstrom810af962002-07-04 00:11:40 +0000560 gid_t gidset[1];
Ben Lindstromc7431342002-03-22 03:11:49 +0000561 struct passwd *pw;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000562 int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000563
564 /* Enable challenge-response authentication for privilege separation */
565 privsep_challenge_enable();
566
567 for (i = 0; i < 256; i++)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000568 rnd[i] = arc4random();
569 RAND_seed(rnd, sizeof(rnd));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000570
571 /* Demote the private keys to public keys. */
572 demote_sensitive_data();
573
Ben Lindstromc7431342002-03-22 03:11:49 +0000574 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
Damien Miller0150c652002-04-24 09:49:09 +1000575 fatal("Privilege separation user %s does not exist",
576 SSH_PRIVSEP_USER);
Ben Lindstromc7431342002-03-22 03:11:49 +0000577 memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
578 endpwent();
579
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000580 /* Change our root directory */
Ben Lindstrom7a7edf72002-03-22 02:42:37 +0000581 if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
582 fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
583 strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000584 if (chdir("/") == -1)
Ben Lindstrom1ee9ec32002-03-22 03:14:45 +0000585 fatal("chdir(\"/\"): %s", strerror(errno));
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000586
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587 /* Drop our privileges */
Ben Lindstromc7431342002-03-22 03:11:49 +0000588 debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
589 (u_int)pw->pw_gid);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000590#if 0
Darren Tuckerd5920482004-02-29 20:11:30 +1100591 /* XXX not ready, too heavy after chroot */
Ben Lindstromc7431342002-03-22 03:11:49 +0000592 do_setusercontext(pw);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000593#else
594 gidset[0] = pw->pw_gid;
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000595 if (setgroups(1, gidset) < 0)
596 fatal("setgroups: %.100s", strerror(errno));
597 permanently_set_uid(pw);
598#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000599}
600
Darren Tucker3e33cec2003-10-02 16:12:36 +1000601static int
602privsep_preauth(Authctxt *authctxt)
Ben Lindstrom943481c2002-03-22 03:43:46 +0000603{
Ben Lindstrom943481c2002-03-22 03:43:46 +0000604 int status;
605 pid_t pid;
606
607 /* Set up unprivileged child process to deal with network data */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000608 pmonitor = monitor_init();
Ben Lindstrom943481c2002-03-22 03:43:46 +0000609 /* Store a pointer to the kex for later rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000610 pmonitor->m_pkex = &xxx_kex;
Ben Lindstrom943481c2002-03-22 03:43:46 +0000611
612 pid = fork();
613 if (pid == -1) {
614 fatal("fork of unprivileged child failed");
615 } else if (pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000616 debug2("Network child is on pid %ld", (long)pid);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000617
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000618 close(pmonitor->m_recvfd);
Darren Tuckera8be9e22004-02-06 16:40:27 +1100619 pmonitor->m_pid = pid;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000620 monitor_child_preauth(authctxt, pmonitor);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000621 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000622
623 /* Sync memory */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000624 monitor_sync(pmonitor);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000625
626 /* Wait for the child's exit status */
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000627 while (waitpid(pid, &status, 0) < 0)
628 if (errno != EINTR)
629 break;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000630 return (1);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000631 } else {
632 /* child */
633
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000634 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000635
636 /* Demote the child */
637 if (getuid() == 0 || geteuid() == 0)
638 privsep_preauth_child();
Ben Lindstromf90f58d2002-03-26 01:53:03 +0000639 setproctitle("%s", "[net]");
Ben Lindstrom943481c2002-03-22 03:43:46 +0000640 }
Darren Tucker3e33cec2003-10-02 16:12:36 +1000641 return (0);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000642}
643
Ben Lindstrom08105192002-03-22 02:50:06 +0000644static void
Ben Lindstrom943481c2002-03-22 03:43:46 +0000645privsep_postauth(Authctxt *authctxt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000646{
Tim Rice9dd30812002-07-07 13:43:36 -0700647#ifdef DISABLE_FD_PASSING
Tim Rice8eff3192002-06-25 15:35:15 -0700648 if (1) {
649#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000650 if (authctxt->pw->pw_uid == 0 || options.use_login) {
Tim Rice8eff3192002-06-25 15:35:15 -0700651#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000652 /* File descriptor passing is broken or root login */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000653 use_privsep = 0;
Darren Tucker45b01422005-10-03 18:20:00 +1000654 goto skip;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000655 }
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000656
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000657 /* New socket pair */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000658 monitor_reinit(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000659
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000660 pmonitor->m_pid = fork();
661 if (pmonitor->m_pid == -1)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000662 fatal("fork of unprivileged child failed");
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000663 else if (pmonitor->m_pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000664 debug2("User child is on pid %ld", (long)pmonitor->m_pid);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000665 close(pmonitor->m_recvfd);
Darren Tuckereb578622004-08-12 23:08:14 +1000666 buffer_clear(&loginmsg);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000667 monitor_child_postauth(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000668
669 /* NEVERREACHED */
670 exit(0);
671 }
672
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000673 close(pmonitor->m_sendfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000674
675 /* Demote the private keys to public keys. */
676 demote_sensitive_data();
677
678 /* Drop privileges */
679 do_setusercontext(authctxt->pw);
680
Darren Tucker45b01422005-10-03 18:20:00 +1000681 skip:
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000682 /* It is safe now to apply the key state */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000683 monitor_apply_keystate(pmonitor);
Damien Miller9786e6e2005-07-26 21:54:56 +1000684
685 /*
686 * Tell the packet layer that authentication was successful, since
687 * this information is not part of the key state.
688 */
689 packet_set_authenticated();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000690}
691
Ben Lindstrombba81212001-06-25 05:01:22 +0000692static char *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100693list_hostkey_types(void)
694{
Damien Miller0e3b8722002-01-22 23:26:38 +1100695 Buffer b;
Damien Millerf58b58c2003-11-17 21:18:23 +1100696 const char *p;
697 char *ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100698 int i;
Damien Miller0e3b8722002-01-22 23:26:38 +1100699
700 buffer_init(&b);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100701 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100702 Key *key = sensitive_data.host_keys[i];
703 if (key == NULL)
704 continue;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000705 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100706 case KEY_RSA:
707 case KEY_DSA:
Damien Miller0e3b8722002-01-22 23:26:38 +1100708 if (buffer_len(&b) > 0)
709 buffer_append(&b, ",", 1);
710 p = key_ssh_name(key);
711 buffer_append(&b, p, strlen(p));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100712 break;
713 }
714 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100715 buffer_append(&b, "\0", 1);
Damien Millerf58b58c2003-11-17 21:18:23 +1100716 ret = xstrdup(buffer_ptr(&b));
Damien Miller0e3b8722002-01-22 23:26:38 +1100717 buffer_free(&b);
Damien Millerf58b58c2003-11-17 21:18:23 +1100718 debug("list_hostkey_types: %s", ret);
719 return ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100720}
721
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000722Key *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100723get_hostkey_by_type(int type)
724{
725 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000726
Damien Miller9f0f5c62001-12-21 14:45:46 +1100727 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100728 Key *key = sensitive_data.host_keys[i];
729 if (key != NULL && key->type == type)
730 return key;
731 }
732 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000733}
734
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000735Key *
736get_hostkey_by_index(int ind)
737{
738 if (ind < 0 || ind >= options.num_host_key_files)
739 return (NULL);
740 return (sensitive_data.host_keys[ind]);
741}
742
743int
744get_hostkey_index(Key *key)
745{
746 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000747
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000748 for (i = 0; i < options.num_host_key_files; i++) {
749 if (key == sensitive_data.host_keys[i])
750 return (i);
751 }
752 return (-1);
753}
754
Damien Miller942da032000-08-18 13:59:06 +1000755/*
756 * returns 1 if connection should be dropped, 0 otherwise.
757 * dropping starts at connection #max_startups_begin with a probability
758 * of (max_startups_rate/100). the probability increases linearly until
759 * all connections are dropped for startups > max_startups
760 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000761static int
Damien Miller942da032000-08-18 13:59:06 +1000762drop_connection(int startups)
763{
Darren Tucker178fa662004-11-05 20:09:09 +1100764 int p, r;
Damien Miller942da032000-08-18 13:59:06 +1000765
766 if (startups < options.max_startups_begin)
767 return 0;
768 if (startups >= options.max_startups)
769 return 1;
770 if (options.max_startups_rate == 100)
771 return 1;
772
773 p = 100 - options.max_startups_rate;
774 p *= startups - options.max_startups_begin;
Darren Tucker178fa662004-11-05 20:09:09 +1100775 p /= options.max_startups - options.max_startups_begin;
Damien Miller942da032000-08-18 13:59:06 +1000776 p += options.max_startups_rate;
Darren Tucker178fa662004-11-05 20:09:09 +1100777 r = arc4random() % 100;
Damien Miller942da032000-08-18 13:59:06 +1000778
Darren Tucker3269b132004-11-05 20:20:59 +1100779 debug("drop_connection: p %d, r %d", p, r);
Damien Miller942da032000-08-18 13:59:06 +1000780 return (r < p) ? 1 : 0;
781}
782
Ben Lindstromade03f62001-12-06 18:22:17 +0000783static void
784usage(void)
785{
Damien Miller0c889cd2004-03-22 09:36:00 +1100786 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000787 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Millerb4087862004-03-22 09:35:21 +1100788 fprintf(stderr,
789"usage: sshd [-46Ddeiqt] [-b bits] [-f config_file] [-g login_grace_time]\n"
790" [-h host_key_file] [-k key_gen_time] [-o option] [-p port] [-u len]\n"
791 );
Ben Lindstromade03f62001-12-06 18:22:17 +0000792 exit(1);
793}
794
Darren Tucker645ab752004-06-25 13:33:20 +1000795static void
796send_rexec_state(int fd, Buffer *conf)
797{
798 Buffer m;
799
800 debug3("%s: entering fd = %d config len %d", __func__, fd,
801 buffer_len(conf));
802
803 /*
804 * Protocol from reexec master to child:
805 * string configuration
806 * u_int ephemeral_key_follows
807 * bignum e (only if ephemeral_key_follows == 1)
808 * bignum n "
809 * bignum d "
810 * bignum iqmp "
811 * bignum p "
812 * bignum q "
Darren Tuckerc6f82192005-09-27 22:46:32 +1000813 * string rngseed (only if OpenSSL is not self-seeded)
Darren Tucker645ab752004-06-25 13:33:20 +1000814 */
815 buffer_init(&m);
816 buffer_put_cstring(&m, buffer_ptr(conf));
817
Darren Tuckerfc959702004-07-17 16:12:08 +1000818 if (sensitive_data.server_key != NULL &&
Darren Tucker645ab752004-06-25 13:33:20 +1000819 sensitive_data.server_key->type == KEY_RSA1) {
820 buffer_put_int(&m, 1);
821 buffer_put_bignum(&m, sensitive_data.server_key->rsa->e);
822 buffer_put_bignum(&m, sensitive_data.server_key->rsa->n);
823 buffer_put_bignum(&m, sensitive_data.server_key->rsa->d);
824 buffer_put_bignum(&m, sensitive_data.server_key->rsa->iqmp);
825 buffer_put_bignum(&m, sensitive_data.server_key->rsa->p);
826 buffer_put_bignum(&m, sensitive_data.server_key->rsa->q);
827 } else
828 buffer_put_int(&m, 0);
829
Darren Tuckerc6f82192005-09-27 22:46:32 +1000830#ifndef OPENSSL_PRNG_ONLY
831 rexec_send_rng_seed(&m);
832#endif
833
Darren Tucker645ab752004-06-25 13:33:20 +1000834 if (ssh_msg_send(fd, 0, &m) == -1)
835 fatal("%s: ssh_msg_send failed", __func__);
836
837 buffer_free(&m);
838
839 debug3("%s: done", __func__);
840}
841
842static void
843recv_rexec_state(int fd, Buffer *conf)
844{
845 Buffer m;
846 char *cp;
847 u_int len;
848
849 debug3("%s: entering fd = %d", __func__, fd);
850
851 buffer_init(&m);
852
853 if (ssh_msg_recv(fd, &m) == -1)
854 fatal("%s: ssh_msg_recv failed", __func__);
855 if (buffer_get_char(&m) != 0)
856 fatal("%s: rexec version mismatch", __func__);
857
858 cp = buffer_get_string(&m, &len);
859 if (conf != NULL)
860 buffer_append(conf, cp, len + 1);
861 xfree(cp);
862
863 if (buffer_get_int(&m)) {
864 if (sensitive_data.server_key != NULL)
865 key_free(sensitive_data.server_key);
866 sensitive_data.server_key = key_new_private(KEY_RSA1);
867 buffer_get_bignum(&m, sensitive_data.server_key->rsa->e);
868 buffer_get_bignum(&m, sensitive_data.server_key->rsa->n);
869 buffer_get_bignum(&m, sensitive_data.server_key->rsa->d);
870 buffer_get_bignum(&m, sensitive_data.server_key->rsa->iqmp);
871 buffer_get_bignum(&m, sensitive_data.server_key->rsa->p);
872 buffer_get_bignum(&m, sensitive_data.server_key->rsa->q);
873 rsa_generate_additional_parameters(
874 sensitive_data.server_key->rsa);
875 }
Darren Tuckerc6f82192005-09-27 22:46:32 +1000876
877#ifndef OPENSSL_PRNG_ONLY
878 rexec_recv_rng_seed(&m);
879#endif
880
Darren Tucker645ab752004-06-25 13:33:20 +1000881 buffer_free(&m);
882
883 debug3("%s: done", __func__);
884}
885
Damien Miller95def091999-11-25 00:26:21 +1100886/*
887 * Main program for the daemon.
888 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889int
890main(int ac, char **av)
891{
Damien Miller95def091999-11-25 00:26:21 +1100892 extern char *optarg;
893 extern int optind;
Damien Miller07d86be2006-03-26 14:19:21 +1100894 int opt, j, i, on = 1;
Damien Miller386c6a22004-06-30 22:40:20 +1000895 int sock_in = -1, sock_out = -1, newsock = -1;
Damien Miller166fca82000-04-20 07:42:21 +1000896 pid_t pid;
Damien Miller34132e52000-01-14 15:45:46 +1100897 socklen_t fromlen;
Damien Miller34132e52000-01-14 15:45:46 +1100898 fd_set *fdset;
899 struct sockaddr_storage from;
Damien Miller95def091999-11-25 00:26:21 +1100900 const char *remote_ip;
901 int remote_port;
Damien Miller95def091999-11-25 00:26:21 +1100902 FILE *f;
Damien Miller34132e52000-01-14 15:45:46 +1100903 struct addrinfo *ai;
904 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerb9997192003-12-17 16:29:22 +1100905 char *line;
Damien Miller34132e52000-01-14 15:45:46 +1100906 int listen_sock, maxfd;
Darren Tucker0f56ed12004-08-29 16:38:41 +1000907 int startup_p[2] = { -1 , -1 }, config_s[2] = { -1 , -1 };
Damien Miller37023962000-07-11 17:31:38 +1000908 int startups = 0;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000909 Key *key;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000910 Authctxt *authctxt;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000911 int ret, key_used = 0;
Darren Tucker645ab752004-06-25 13:33:20 +1000912 Buffer cfg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913
Kevin Steves0ea1d9d2002-04-25 18:17:04 +0000914#ifdef HAVE_SECUREWARE
915 (void)set_auth_parameters(ac, av);
916#endif
Damien Miller59d3d5b2003-08-22 09:34:41 +1000917 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000918 init_rng();
919
Damien Millera8ed44b2003-01-10 09:53:12 +1100920 /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
Damien Millerb8c656e2000-06-28 15:22:41 +1000921 saved_argc = ac;
Darren Tucker17c5d032004-06-25 14:22:23 +1000922 rexec_argc = ac;
Damien Miller04cb5362003-05-15 21:29:10 +1000923 saved_argv = xmalloc(sizeof(*saved_argv) * (ac + 1));
Damien Millera8ed44b2003-01-10 09:53:12 +1100924 for (i = 0; i < ac; i++)
925 saved_argv[i] = xstrdup(av[i]);
Damien Miller04cb5362003-05-15 21:29:10 +1000926 saved_argv[i] = NULL;
Damien Millera8ed44b2003-01-10 09:53:12 +1100927
928#ifndef HAVE_SETPROCTITLE
929 /* Prepare for later setproctitle emulation */
930 compat_init_setproctitle(ac, av);
Damien Millerf2e3e9d2003-06-02 12:15:54 +1000931 av = saved_argv;
Damien Millera8ed44b2003-01-10 09:53:12 +1100932#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933
Damien Millerbfba3542004-03-22 09:29:57 +1100934 if (geteuid() == 0 && setgroups(0, NULL) == -1)
935 debug("setgroups(): %.200s", strerror(errno));
936
Darren Tuckerce321d82005-10-03 18:11:24 +1000937 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
938 sanitise_stdfd();
939
Damien Miller95def091999-11-25 00:26:21 +1100940 /* Initialize configuration options to their default values. */
941 initialize_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942
Damien Miller95def091999-11-25 00:26:21 +1100943 /* Parse command-line arguments. */
Darren Tucker645ab752004-06-25 13:33:20 +1000944 while ((opt = getopt(ac, av, "f:p:b:k:h:g:u:o:dDeiqrtQR46")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100945 switch (opt) {
Damien Miller34132e52000-01-14 15:45:46 +1100946 case '4':
Darren Tucker0f383232005-01-20 10:57:56 +1100947 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100948 break;
949 case '6':
Darren Tucker0f383232005-01-20 10:57:56 +1100950 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100951 break;
Damien Miller95def091999-11-25 00:26:21 +1100952 case 'f':
953 config_file_name = optarg;
954 break;
955 case 'd':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000956 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100957 debug_flag = 1;
958 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000959 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
Damien Millere4340be2000-09-16 13:29:08 +1100960 options.log_level++;
Damien Miller95def091999-11-25 00:26:21 +1100961 break;
Ben Lindstromc72745a2000-12-02 19:03:54 +0000962 case 'D':
963 no_daemon_flag = 1;
964 break;
Ben Lindstrom9fce9f02001-04-11 23:10:09 +0000965 case 'e':
966 log_stderr = 1;
967 break;
Damien Miller95def091999-11-25 00:26:21 +1100968 case 'i':
969 inetd_flag = 1;
970 break;
Darren Tucker645ab752004-06-25 13:33:20 +1000971 case 'r':
972 rexec_flag = 0;
973 break;
974 case 'R':
975 rexeced_flag = 1;
976 inetd_flag = 1;
977 break;
Damien Miller95def091999-11-25 00:26:21 +1100978 case 'Q':
Ben Lindstromd5390202001-01-29 08:07:43 +0000979 /* ignored */
Damien Miller95def091999-11-25 00:26:21 +1100980 break;
981 case 'q':
982 options.log_level = SYSLOG_LEVEL_QUIET;
983 break;
984 case 'b':
Damien Millerf0b15df2006-03-26 13:59:20 +1100985 options.server_key_bits = (int)strtonum(optarg, 256,
986 32768, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100987 break;
988 case 'p':
Damien Miller34132e52000-01-14 15:45:46 +1100989 options.ports_from_cmdline = 1;
Damien Millere4340be2000-09-16 13:29:08 +1100990 if (options.num_ports >= MAX_PORTS) {
991 fprintf(stderr, "too many ports.\n");
992 exit(1);
993 }
Ben Lindstrom19066a12001-04-12 23:39:26 +0000994 options.ports[options.num_ports++] = a2port(optarg);
995 if (options.ports[options.num_ports-1] == 0) {
996 fprintf(stderr, "Bad port number.\n");
997 exit(1);
998 }
Damien Miller95def091999-11-25 00:26:21 +1100999 break;
1000 case 'g':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +00001001 if ((options.login_grace_time = convtime(optarg)) == -1) {
1002 fprintf(stderr, "Invalid login grace time.\n");
1003 exit(1);
1004 }
Damien Miller95def091999-11-25 00:26:21 +11001005 break;
1006 case 'k':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +00001007 if ((options.key_regeneration_time = convtime(optarg)) == -1) {
1008 fprintf(stderr, "Invalid key regeneration interval.\n");
1009 exit(1);
1010 }
Damien Miller95def091999-11-25 00:26:21 +11001011 break;
1012 case 'h':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001013 if (options.num_host_key_files >= MAX_HOSTKEYS) {
1014 fprintf(stderr, "too many host keys.\n");
1015 exit(1);
1016 }
1017 options.host_key_files[options.num_host_key_files++] = optarg;
Damien Miller95def091999-11-25 00:26:21 +11001018 break;
Ben Lindstrom794325a2001-08-06 21:09:07 +00001019 case 't':
1020 test_flag = 1;
1021 break;
Damien Miller942da032000-08-18 13:59:06 +10001022 case 'u':
Damien Millerf0b15df2006-03-26 13:59:20 +11001023 utmp_len = (u_int)strtonum(optarg, 0, MAXHOSTNAMELEN+1, NULL);
Ben Lindstrom41daec72002-07-23 21:15:13 +00001024 if (utmp_len > MAXHOSTNAMELEN) {
1025 fprintf(stderr, "Invalid utmp length.\n");
1026 exit(1);
1027 }
Damien Miller942da032000-08-18 13:59:06 +10001028 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001029 case 'o':
Damien Millerb9997192003-12-17 16:29:22 +11001030 line = xstrdup(optarg);
1031 if (process_server_config_line(&options, line,
Ben Lindstromade03f62001-12-06 18:22:17 +00001032 "command-line", 0) != 0)
Damien Miller9f0f5c62001-12-21 14:45:46 +11001033 exit(1);
Damien Millerb9997192003-12-17 16:29:22 +11001034 xfree(line);
Ben Lindstromade03f62001-12-06 18:22:17 +00001035 break;
Damien Miller95def091999-11-25 00:26:21 +11001036 case '?':
1037 default:
Ben Lindstromade03f62001-12-06 18:22:17 +00001038 usage();
1039 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041 }
Darren Tucker645ab752004-06-25 13:33:20 +10001042 if (rexeced_flag || inetd_flag)
1043 rexec_flag = 0;
1044 if (rexec_flag && (av[0] == NULL || *av[0] != '/'))
1045 fatal("sshd re-exec requires execution with an absolute path");
1046 if (rexeced_flag)
Damien Miller035a5b42004-06-26 08:16:31 +10001047 closefrom(REEXEC_MIN_FREE_FD);
1048 else
1049 closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001050
Ben Lindstrom425fb022001-03-29 00:31:20 +00001051 SSLeay_add_all_algorithms();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001052
Damien Miller34132e52000-01-14 15:45:46 +11001053 /*
1054 * Force logging to stderr until we have loaded the private host
1055 * key (unless started from inetd)
1056 */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001057 log_init(__progname,
Damien Miller5aa5d782002-02-08 22:01:54 +11001058 options.log_level == SYSLOG_LEVEL_NOT_SET ?
1059 SYSLOG_LEVEL_INFO : options.log_level,
1060 options.log_facility == SYSLOG_FACILITY_NOT_SET ?
1061 SYSLOG_FACILITY_AUTH : options.log_facility,
Ben Lindstromc2faa4a2002-11-09 15:50:03 +00001062 log_stderr || !inetd_flag);
Damien Miller34132e52000-01-14 15:45:46 +11001063
Darren Tucker86c093d2004-03-08 22:59:03 +11001064 /*
1065 * Unset KRB5CCNAME, otherwise the user's session may inherit it from
1066 * root's environment
Damien Miller94cf4c82005-07-17 17:04:47 +10001067 */
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001068 if (getenv("KRB5CCNAME") != NULL)
1069 unsetenv("KRB5CCNAME");
1070
Tim Rice81ed5182002-09-25 17:38:46 -07001071#ifdef _UNICOS
Darren Tucker12984962004-05-24 13:37:13 +10001072 /* Cray can define user privs drop all privs now!
Ben Lindstrom6db66ff2001-08-06 23:29:16 +00001073 * Not needed on PRIV_SU systems!
1074 */
1075 drop_cray_privs();
1076#endif
1077
Darren Tucker645ab752004-06-25 13:33:20 +10001078 sensitive_data.server_key = NULL;
1079 sensitive_data.ssh1_host_key = NULL;
1080 sensitive_data.have_ssh1_key = 0;
1081 sensitive_data.have_ssh2_key = 0;
1082
1083 /* Fetch our configuration */
1084 buffer_init(&cfg);
1085 if (rexeced_flag)
Damien Miller035a5b42004-06-26 08:16:31 +10001086 recv_rexec_state(REEXEC_CONFIG_PASS_FD, &cfg);
Darren Tucker645ab752004-06-25 13:33:20 +10001087 else
1088 load_server_config(config_file_name, &cfg);
1089
1090 parse_server_config(&options,
1091 rexeced_flag ? "rexec" : config_file_name, &cfg);
1092
1093 if (!rexec_flag)
1094 buffer_free(&cfg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001095
Darren Tuckerc6f82192005-09-27 22:46:32 +10001096 seed_rng();
1097
Damien Miller95def091999-11-25 00:26:21 +11001098 /* Fill in default values for those options not explicitly set. */
1099 fill_default_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001100
Darren Tucker0f383232005-01-20 10:57:56 +11001101 /* set default channel AF */
1102 channel_set_af(options.address_family);
1103
Damien Miller95def091999-11-25 00:26:21 +11001104 /* Check that there are no remaining arguments. */
1105 if (optind < ac) {
1106 fprintf(stderr, "Extra argument %s.\n", av[optind]);
1107 exit(1);
1108 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001109
Damien Miller2aa6d3c2004-09-12 16:53:04 +10001110 debug("sshd version %.100s", SSH_RELEASE);
Damien Miller2ccf6611999-11-15 15:25:10 +11001111
Damien Miller0bc1bd82000-11-13 22:57:25 +11001112 /* load private host keys */
Damien Miller07d86be2006-03-26 14:19:21 +11001113 sensitive_data.host_keys = xcalloc(options.num_host_key_files,
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001114 sizeof(Key *));
Damien Miller9f0f5c62001-12-21 14:45:46 +11001115 for (i = 0; i < options.num_host_key_files; i++)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001116 sensitive_data.host_keys[i] = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001117
Damien Miller9f0f5c62001-12-21 14:45:46 +11001118 for (i = 0; i < options.num_host_key_files; i++) {
Ben Lindstromd0fca422001-03-26 13:44:06 +00001119 key = key_load_private(options.host_key_files[i], "", NULL);
1120 sensitive_data.host_keys[i] = key;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001121 if (key == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001122 error("Could not load host key: %s",
1123 options.host_key_files[i]);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001124 sensitive_data.host_keys[i] = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001125 continue;
1126 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001127 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001128 case KEY_RSA1:
1129 sensitive_data.ssh1_host_key = key;
1130 sensitive_data.have_ssh1_key = 1;
1131 break;
1132 case KEY_RSA:
1133 case KEY_DSA:
1134 sensitive_data.have_ssh2_key = 1;
1135 break;
1136 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001137 debug("private host key: #%d type %d %s", i, key->type,
1138 key_type(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001139 }
1140 if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
Damien Miller996acd22003-04-09 20:59:48 +10001141 logit("Disabling protocol version 1. Could not load host key");
Damien Millereba71ba2000-04-29 23:57:08 +10001142 options.protocol &= ~SSH_PROTO_1;
1143 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001144 if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
Damien Miller996acd22003-04-09 20:59:48 +10001145 logit("Disabling protocol version 2. Could not load host key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001146 options.protocol &= ~SSH_PROTO_2;
Damien Millereba71ba2000-04-29 23:57:08 +10001147 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001148 if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
Damien Miller996acd22003-04-09 20:59:48 +10001149 logit("sshd: no hostkeys available -- exiting.");
Damien Miller95def091999-11-25 00:26:21 +11001150 exit(1);
1151 }
Damien Miller95def091999-11-25 00:26:21 +11001152
Damien Millereba71ba2000-04-29 23:57:08 +10001153 /* Check certain values for sanity. */
1154 if (options.protocol & SSH_PROTO_1) {
1155 if (options.server_key_bits < 512 ||
1156 options.server_key_bits > 32768) {
1157 fprintf(stderr, "Bad server key size.\n");
1158 exit(1);
1159 }
1160 /*
1161 * Check that server and host key lengths differ sufficiently. This
1162 * is necessary to make double encryption work with rsaref. Oh, I
1163 * hate software patents. I dont know if this can go? Niels
1164 */
1165 if (options.server_key_bits >
Ben Lindstrom822b6342002-06-23 21:38:49 +00001166 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) -
1167 SSH_KEY_BITS_RESERVED && options.server_key_bits <
1168 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1169 SSH_KEY_BITS_RESERVED) {
Damien Millereba71ba2000-04-29 23:57:08 +10001170 options.server_key_bits =
Ben Lindstrom822b6342002-06-23 21:38:49 +00001171 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1172 SSH_KEY_BITS_RESERVED;
Damien Millereba71ba2000-04-29 23:57:08 +10001173 debug("Forcing server key to %d bits to make it differ from host key.",
1174 options.server_key_bits);
1175 }
1176 }
1177
Ben Lindstroma26ea632002-06-06 20:46:25 +00001178 if (use_privsep) {
Ben Lindstroma26ea632002-06-06 20:46:25 +00001179 struct stat st;
1180
Damien Millerf0b15df2006-03-26 13:59:20 +11001181 if (getpwnam(SSH_PRIVSEP_USER) == NULL)
Ben Lindstroma26ea632002-06-06 20:46:25 +00001182 fatal("Privilege separation user %s does not exist",
1183 SSH_PRIVSEP_USER);
1184 if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
1185 (S_ISDIR(st.st_mode) == 0))
1186 fatal("Missing privilege separation directory: %s",
1187 _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstrom59627352002-06-27 18:02:21 +00001188
1189#ifdef HAVE_CYGWIN
1190 if (check_ntsec(_PATH_PRIVSEP_CHROOT_DIR) &&
1191 (st.st_uid != getuid () ||
1192 (st.st_mode & (S_IWGRP|S_IWOTH)) != 0))
1193#else
Ben Lindstrom2dfacb32002-06-23 00:33:47 +00001194 if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
Ben Lindstrom59627352002-06-27 18:02:21 +00001195#endif
Damien Miller180fc5b2003-02-24 11:50:18 +11001196 fatal("%s must be owned by root and not group or "
1197 "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstroma26ea632002-06-06 20:46:25 +00001198 }
1199
Ben Lindstrom794325a2001-08-06 21:09:07 +00001200 /* Configuration looks good, so exit if in test mode. */
1201 if (test_flag)
1202 exit(0);
1203
Damien Miller87aea252002-05-10 12:20:24 +10001204 /*
1205 * Clear out any supplemental groups we may have inherited. This
1206 * prevents inadvertent creation of files with bad modes (in the
Damien Millera8e06ce2003-11-21 23:48:55 +11001207 * portable version at least, it's certainly possible for PAM
1208 * to create a file, and we can't control the code in every
Damien Miller87aea252002-05-10 12:20:24 +10001209 * module which might be used).
1210 */
1211 if (setgroups(0, NULL) < 0)
1212 debug("setgroups() failed: %.200s", strerror(errno));
1213
Darren Tucker645ab752004-06-25 13:33:20 +10001214 if (rexec_flag) {
Damien Miller07d86be2006-03-26 14:19:21 +11001215 rexec_argv = xcalloc(rexec_argc + 2, sizeof(char *));
Darren Tucker645ab752004-06-25 13:33:20 +10001216 for (i = 0; i < rexec_argc; i++) {
1217 debug("rexec_argv[%d]='%s'", i, saved_argv[i]);
1218 rexec_argv[i] = saved_argv[i];
1219 }
1220 rexec_argv[rexec_argc] = "-R";
1221 rexec_argv[rexec_argc + 1] = NULL;
1222 }
1223
Damien Millereba71ba2000-04-29 23:57:08 +10001224 /* Initialize the log (it is reinitialized below in case we forked). */
Darren Tuckerea7c8122005-01-20 11:03:08 +11001225 if (debug_flag && (!inetd_flag || rexeced_flag))
Damien Miller95def091999-11-25 00:26:21 +11001226 log_stderr = 1;
Kevin Stevesec84dc12000-12-13 17:45:15 +00001227 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001228
Damien Millereba71ba2000-04-29 23:57:08 +10001229 /*
1230 * If not in debugging mode, and not started from inetd, disconnect
1231 * from the controlling terminal, and fork. The original process
1232 * exits.
1233 */
Ben Lindstromc72745a2000-12-02 19:03:54 +00001234 if (!(debug_flag || inetd_flag || no_daemon_flag)) {
Damien Miller95def091999-11-25 00:26:21 +11001235#ifdef TIOCNOTTY
1236 int fd;
1237#endif /* TIOCNOTTY */
1238 if (daemon(0, 0) < 0)
1239 fatal("daemon() failed: %.200s", strerror(errno));
1240
1241 /* Disconnect from the controlling tty. */
1242#ifdef TIOCNOTTY
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001243 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
Damien Miller95def091999-11-25 00:26:21 +11001244 if (fd >= 0) {
1245 (void) ioctl(fd, TIOCNOTTY, NULL);
1246 close(fd);
1247 }
1248#endif /* TIOCNOTTY */
1249 }
1250 /* Reinitialize the log (because of the fork above). */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001251 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001252
Damien Miller95def091999-11-25 00:26:21 +11001253 /* Initialize the random number generator. */
1254 arc4random_stir();
1255
1256 /* Chdir to the root directory so that the current disk can be
1257 unmounted if desired. */
1258 chdir("/");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001259
Ben Lindstromde71cda2001-03-24 00:43:26 +00001260 /* ignore SIGPIPE */
1261 signal(SIGPIPE, SIG_IGN);
Damien Miller95def091999-11-25 00:26:21 +11001262
Damien Miller95def091999-11-25 00:26:21 +11001263 /* Start listening for a socket, unless started from inetd. */
1264 if (inetd_flag) {
Darren Tucker645ab752004-06-25 13:33:20 +10001265 int fd;
1266
Damien Miller994cf142000-07-21 10:19:44 +10001267 startup_pipe = -1;
Darren Tucker645ab752004-06-25 13:33:20 +10001268 if (rexeced_flag) {
Damien Miller035a5b42004-06-26 08:16:31 +10001269 close(REEXEC_CONFIG_PASS_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001270 sock_in = sock_out = dup(STDIN_FILENO);
1271 if (!debug_flag) {
Damien Miller035a5b42004-06-26 08:16:31 +10001272 startup_pipe = dup(REEXEC_STARTUP_PIPE_FD);
1273 close(REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001274 }
1275 } else {
1276 sock_in = dup(STDIN_FILENO);
1277 sock_out = dup(STDOUT_FILENO);
1278 }
Damien Millereba71ba2000-04-29 23:57:08 +10001279 /*
1280 * We intentionally do not close the descriptors 0, 1, and 2
Darren Tucker645ab752004-06-25 13:33:20 +10001281 * as our code for setting the descriptors won't work if
Damien Millereba71ba2000-04-29 23:57:08 +10001282 * ttyfd happens to be one of those.
1283 */
Darren Tucker645ab752004-06-25 13:33:20 +10001284 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1285 dup2(fd, STDIN_FILENO);
1286 dup2(fd, STDOUT_FILENO);
1287 if (fd > STDOUT_FILENO)
1288 close(fd);
1289 }
Damien Miller95def091999-11-25 00:26:21 +11001290 debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
Darren Tucker645ab752004-06-25 13:33:20 +10001291 if ((options.protocol & SSH_PROTO_1) &&
1292 sensitive_data.server_key == NULL)
Ben Lindstromca42d5f2001-03-09 18:25:32 +00001293 generate_ephemeral_server_key();
Damien Miller95def091999-11-25 00:26:21 +11001294 } else {
Damien Miller34132e52000-01-14 15:45:46 +11001295 for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
1296 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1297 continue;
1298 if (num_listen_socks >= MAX_LISTEN_SOCKS)
1299 fatal("Too many listen sockets. "
1300 "Enlarge MAX_LISTEN_SOCKS");
Darren Tucker96d47102005-02-09 09:53:48 +11001301 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
Damien Miller34132e52000-01-14 15:45:46 +11001302 ntop, sizeof(ntop), strport, sizeof(strport),
Darren Tucker96d47102005-02-09 09:53:48 +11001303 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1304 error("getnameinfo failed: %.100s",
1305 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
1306 strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11001307 continue;
1308 }
1309 /* Create socket for listening. */
Damien Miller2372ace2003-05-14 13:42:23 +10001310 listen_sock = socket(ai->ai_family, ai->ai_socktype,
1311 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11001312 if (listen_sock < 0) {
1313 /* kernel may not support ipv6 */
1314 verbose("socket: %.100s", strerror(errno));
1315 continue;
1316 }
Damien Miller232711f2004-06-15 10:35:30 +10001317 if (set_nonblock(listen_sock) == -1) {
Darren Tuckerefa37062004-02-24 09:20:29 +11001318 close(listen_sock);
1319 continue;
1320 }
Damien Miller34132e52000-01-14 15:45:46 +11001321 /*
Damien Millere1383ce2002-09-19 11:49:37 +10001322 * Set socket options.
1323 * Allow local port reuse in TIME_WAIT.
Damien Miller34132e52000-01-14 15:45:46 +11001324 */
Damien Millere1383ce2002-09-19 11:49:37 +10001325 if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
1326 &on, sizeof(on)) == -1)
1327 error("setsockopt SO_REUSEADDR: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001328
Damien Miller34132e52000-01-14 15:45:46 +11001329 debug("Bind to port %s on %s.", strport, ntop);
Damien Miller95def091999-11-25 00:26:21 +11001330
Damien Miller34132e52000-01-14 15:45:46 +11001331 /* Bind the socket to the desired port. */
Damien Miller0a4e27d2001-02-18 12:36:39 +11001332 if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1333 if (!ai->ai_next)
1334 error("Bind to port %s on %s failed: %.200s.",
1335 strport, ntop, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11001336 close(listen_sock);
1337 continue;
1338 }
1339 listen_socks[num_listen_socks] = listen_sock;
1340 num_listen_socks++;
Damien Miller95def091999-11-25 00:26:21 +11001341
Damien Miller34132e52000-01-14 15:45:46 +11001342 /* Start listening on the port. */
Darren Tucker3175eb92003-12-09 19:15:11 +11001343 if (listen(listen_sock, SSH_LISTEN_BACKLOG) < 0)
Damien Millerb24c2f82006-03-15 12:04:36 +11001344 fatal("listen on [%s]:%s: %.100s",
1345 ntop, strport, strerror(errno));
1346 logit("Server listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001347 }
Damien Miller34132e52000-01-14 15:45:46 +11001348 freeaddrinfo(options.listen_addrs);
1349
1350 if (!num_listen_socks)
1351 fatal("Cannot bind any address.");
1352
Ben Lindstrom98097862001-06-25 05:10:20 +00001353 if (options.protocol & SSH_PROTO_1)
1354 generate_ephemeral_server_key();
1355
1356 /*
1357 * Arrange to restart on SIGHUP. The handler needs
1358 * listen_sock.
1359 */
1360 signal(SIGHUP, sighup_handler);
1361
1362 signal(SIGTERM, sigterm_handler);
1363 signal(SIGQUIT, sigterm_handler);
1364
1365 /* Arrange SIGCHLD to be caught. */
1366 signal(SIGCHLD, main_sigchld_handler);
1367
1368 /* Write out the pid file after the sigterm handler is setup */
Damien Miller95def091999-11-25 00:26:21 +11001369 if (!debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +11001370 /*
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001371 * Record our pid in /var/run/sshd.pid to make it
1372 * easier to kill the correct sshd. We don't want to
1373 * do this before the bind above because the bind will
Damien Miller5428f641999-11-25 11:54:57 +11001374 * fail if there already is a daemon, and this will
1375 * overwrite any old pid in the file.
1376 */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001377 f = fopen(options.pid_file, "wb");
Darren Tuckere5327042003-07-03 13:40:44 +10001378 if (f == NULL) {
1379 error("Couldn't create pid file \"%s\": %s",
1380 options.pid_file, strerror(errno));
1381 } else {
Ben Lindstromce0f6342002-06-11 16:42:49 +00001382 fprintf(f, "%ld\n", (long) getpid());
Damien Miller95def091999-11-25 00:26:21 +11001383 fclose(f);
1384 }
1385 }
Damien Miller95def091999-11-25 00:26:21 +11001386
Damien Miller34132e52000-01-14 15:45:46 +11001387 /* setup fd set for listen */
Damien Miller37023962000-07-11 17:31:38 +10001388 fdset = NULL;
Damien Miller34132e52000-01-14 15:45:46 +11001389 maxfd = 0;
1390 for (i = 0; i < num_listen_socks; i++)
1391 if (listen_socks[i] > maxfd)
1392 maxfd = listen_socks[i];
Damien Miller37023962000-07-11 17:31:38 +10001393 /* pipes connected to unauthenticated childs */
Damien Miller07d86be2006-03-26 14:19:21 +11001394 startup_pipes = xcalloc(options.max_startups, sizeof(int));
Damien Miller37023962000-07-11 17:31:38 +10001395 for (i = 0; i < options.max_startups; i++)
1396 startup_pipes[i] = -1;
Damien Miller34132e52000-01-14 15:45:46 +11001397
Damien Miller5428f641999-11-25 11:54:57 +11001398 /*
1399 * Stay listening for connections until the system crashes or
1400 * the daemon is killed with a signal.
1401 */
Damien Miller95def091999-11-25 00:26:21 +11001402 for (;;) {
1403 if (received_sighup)
1404 sighup_restart();
Damien Miller37023962000-07-11 17:31:38 +10001405 if (fdset != NULL)
1406 xfree(fdset);
Damien Miller07d86be2006-03-26 14:19:21 +11001407 fdset = (fd_set *)xcalloc(howmany(maxfd + 1, NFDBITS),
1408 sizeof(fd_mask));
Damien Miller37023962000-07-11 17:31:38 +10001409
Damien Miller34132e52000-01-14 15:45:46 +11001410 for (i = 0; i < num_listen_socks; i++)
1411 FD_SET(listen_socks[i], fdset);
Damien Miller37023962000-07-11 17:31:38 +10001412 for (i = 0; i < options.max_startups; i++)
1413 if (startup_pipes[i] != -1)
1414 FD_SET(startup_pipes[i], fdset);
1415
1416 /* Wait in select until there is a connection. */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001417 ret = select(maxfd+1, fdset, NULL, NULL, NULL);
1418 if (ret < 0 && errno != EINTR)
1419 error("select: %.100s", strerror(errno));
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001420 if (received_sigterm) {
Damien Miller996acd22003-04-09 20:59:48 +10001421 logit("Received signal %d; terminating.",
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001422 (int) received_sigterm);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001423 close_listen_socks();
1424 unlink(options.pid_file);
1425 exit(255);
1426 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001427 if (key_used && key_do_regen) {
Ben Lindstromca42d5f2001-03-09 18:25:32 +00001428 generate_ephemeral_server_key();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001429 key_used = 0;
1430 key_do_regen = 0;
Damien Miller34132e52000-01-14 15:45:46 +11001431 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001432 if (ret < 0)
1433 continue;
1434
Damien Miller37023962000-07-11 17:31:38 +10001435 for (i = 0; i < options.max_startups; i++)
1436 if (startup_pipes[i] != -1 &&
1437 FD_ISSET(startup_pipes[i], fdset)) {
1438 /*
1439 * the read end of the pipe is ready
1440 * if the child has closed the pipe
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001441 * after successful authentication
Damien Miller37023962000-07-11 17:31:38 +10001442 * or if the child has died
1443 */
1444 close(startup_pipes[i]);
1445 startup_pipes[i] = -1;
1446 startups--;
1447 }
Damien Miller34132e52000-01-14 15:45:46 +11001448 for (i = 0; i < num_listen_socks; i++) {
1449 if (!FD_ISSET(listen_socks[i], fdset))
Damien Miller95def091999-11-25 00:26:21 +11001450 continue;
Damien Miller37023962000-07-11 17:31:38 +10001451 fromlen = sizeof(from);
Damien Millerf0b15df2006-03-26 13:59:20 +11001452 newsock = accept(listen_socks[i],
1453 (struct sockaddr *)&from, &fromlen);
Damien Miller37023962000-07-11 17:31:38 +10001454 if (newsock < 0) {
1455 if (errno != EINTR && errno != EWOULDBLOCK)
1456 error("accept: %.100s", strerror(errno));
1457 continue;
1458 }
Damien Miller232711f2004-06-15 10:35:30 +10001459 if (unset_nonblock(newsock) == -1) {
Darren Tuckerefa37062004-02-24 09:20:29 +11001460 close(newsock);
1461 continue;
1462 }
Damien Miller942da032000-08-18 13:59:06 +10001463 if (drop_connection(startups) == 1) {
1464 debug("drop connection #%d", startups);
Damien Miller37023962000-07-11 17:31:38 +10001465 close(newsock);
1466 continue;
1467 }
1468 if (pipe(startup_p) == -1) {
1469 close(newsock);
1470 continue;
1471 }
1472
Darren Tucker645ab752004-06-25 13:33:20 +10001473 if (rexec_flag && socketpair(AF_UNIX,
1474 SOCK_STREAM, 0, config_s) == -1) {
1475 error("reexec socketpair: %s",
1476 strerror(errno));
1477 close(newsock);
1478 close(startup_p[0]);
1479 close(startup_p[1]);
1480 continue;
1481 }
1482
Damien Miller37023962000-07-11 17:31:38 +10001483 for (j = 0; j < options.max_startups; j++)
1484 if (startup_pipes[j] == -1) {
1485 startup_pipes[j] = startup_p[0];
1486 if (maxfd < startup_p[0])
1487 maxfd = startup_p[0];
1488 startups++;
1489 break;
1490 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001491
Damien Miller5428f641999-11-25 11:54:57 +11001492 /*
Damien Miller37023962000-07-11 17:31:38 +10001493 * Got connection. Fork a child to handle it, unless
1494 * we are in debugging mode.
Damien Miller5428f641999-11-25 11:54:57 +11001495 */
Damien Miller37023962000-07-11 17:31:38 +10001496 if (debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +11001497 /*
Damien Miller37023962000-07-11 17:31:38 +10001498 * In debugging mode. Close the listening
1499 * socket, and start processing the
1500 * connection without forking.
Damien Miller5428f641999-11-25 11:54:57 +11001501 */
Damien Miller37023962000-07-11 17:31:38 +10001502 debug("Server will not fork when running in debugging mode.");
Damien Miller34132e52000-01-14 15:45:46 +11001503 close_listen_socks();
Damien Miller95def091999-11-25 00:26:21 +11001504 sock_in = newsock;
1505 sock_out = newsock;
Darren Tucker645ab752004-06-25 13:33:20 +10001506 close(startup_p[0]);
1507 close(startup_p[1]);
Damien Miller4d97ba22000-07-11 18:15:50 +10001508 startup_pipe = -1;
Damien Miller182ee6e2000-07-12 09:45:27 +10001509 pid = getpid();
Darren Tucker645ab752004-06-25 13:33:20 +10001510 if (rexec_flag) {
1511 send_rexec_state(config_s[0],
1512 &cfg);
1513 close(config_s[0]);
1514 }
Damien Miller95def091999-11-25 00:26:21 +11001515 break;
Damien Miller37023962000-07-11 17:31:38 +10001516 } else {
1517 /*
1518 * Normal production daemon. Fork, and have
1519 * the child process the connection. The
1520 * parent continues listening.
1521 */
1522 if ((pid = fork()) == 0) {
1523 /*
Damien Millerf0b15df2006-03-26 13:59:20 +11001524 * Child. Close the listening and
1525 * max_startup sockets. Start using
1526 * the accepted socket. Reinitialize
1527 * logging (since our pid has changed).
1528 * We break out of the loop to handle
Damien Miller37023962000-07-11 17:31:38 +10001529 * the connection.
1530 */
1531 startup_pipe = startup_p[1];
Ben Lindstromd84df982001-12-06 16:35:40 +00001532 close_startup_pipes();
Damien Miller37023962000-07-11 17:31:38 +10001533 close_listen_socks();
1534 sock_in = newsock;
1535 sock_out = newsock;
Damien Millerf0b15df2006-03-26 13:59:20 +11001536 log_init(__progname,
1537 options.log_level,
1538 options.log_facility,
1539 log_stderr);
Darren Tucker0f56ed12004-08-29 16:38:41 +10001540 if (rexec_flag)
1541 close(config_s[0]);
Damien Miller37023962000-07-11 17:31:38 +10001542 break;
1543 }
Damien Miller95def091999-11-25 00:26:21 +11001544 }
Damien Miller37023962000-07-11 17:31:38 +10001545
1546 /* Parent. Stay in the loop. */
1547 if (pid < 0)
1548 error("fork: %.100s", strerror(errno));
1549 else
Ben Lindstromce0f6342002-06-11 16:42:49 +00001550 debug("Forked child %ld.", (long)pid);
Damien Miller37023962000-07-11 17:31:38 +10001551
1552 close(startup_p[1]);
1553
Darren Tucker645ab752004-06-25 13:33:20 +10001554 if (rexec_flag) {
1555 send_rexec_state(config_s[0], &cfg);
1556 close(config_s[0]);
1557 close(config_s[1]);
1558 }
1559
Damien Millerf0b15df2006-03-26 13:59:20 +11001560 /*
1561 * Mark that the key has been used (it
1562 * was "given" to the child).
1563 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001564 if ((options.protocol & SSH_PROTO_1) &&
1565 key_used == 0) {
1566 /* Schedule server key regeneration alarm. */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00001567 signal(SIGALRM, key_regeneration_alarm);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001568 alarm(options.key_regeneration_time);
1569 key_used = 1;
1570 }
Damien Miller37023962000-07-11 17:31:38 +10001571
1572 arc4random_stir();
Damien Miller37023962000-07-11 17:31:38 +10001573 close(newsock);
Damien Miller95def091999-11-25 00:26:21 +11001574 }
Damien Miller34132e52000-01-14 15:45:46 +11001575 /* child process check (or debug mode) */
1576 if (num_listen_socks < 0)
1577 break;
Damien Miller95def091999-11-25 00:26:21 +11001578 }
1579 }
1580
1581 /* This is the child processing a new connection. */
Damien Miller57aae982004-03-08 23:11:25 +11001582 setproctitle("%s", "[accepted]");
Damien Miller95def091999-11-25 00:26:21 +11001583
Darren Tucker6832b832004-08-12 22:36:51 +10001584 /*
1585 * Create a new session and process group since the 4.4BSD
1586 * setlogin() affects the entire process group. We don't
1587 * want the child to be able to affect the parent.
1588 */
1589#if !defined(SSHD_ACQUIRES_CTTY)
1590 /*
1591 * If setsid is called, on some platforms sshd will later acquire a
1592 * controlling terminal which will result in "could not set
1593 * controlling tty" errors.
1594 */
1595 if (!debug_flag && !inetd_flag && setsid() < 0)
1596 error("setsid: %.100s", strerror(errno));
1597#endif
1598
Darren Tucker645ab752004-06-25 13:33:20 +10001599 if (rexec_flag) {
1600 int fd;
1601
Damien Miller035a5b42004-06-26 08:16:31 +10001602 debug("rexec start in %d out %d newsock %d pipe %d sock %d",
1603 sock_in, sock_out, newsock, startup_pipe, config_s[0]);
Darren Tucker645ab752004-06-25 13:33:20 +10001604 dup2(newsock, STDIN_FILENO);
1605 dup2(STDIN_FILENO, STDOUT_FILENO);
1606 if (startup_pipe == -1)
Damien Miller035a5b42004-06-26 08:16:31 +10001607 close(REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001608 else
Damien Miller035a5b42004-06-26 08:16:31 +10001609 dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001610
Damien Miller035a5b42004-06-26 08:16:31 +10001611 dup2(config_s[1], REEXEC_CONFIG_PASS_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001612 close(config_s[1]);
Darren Tuckerd8835932004-08-12 22:42:29 +10001613 if (startup_pipe != -1)
1614 close(startup_pipe);
Damien Miller035a5b42004-06-26 08:16:31 +10001615
Darren Tucker645ab752004-06-25 13:33:20 +10001616 execv(rexec_argv[0], rexec_argv);
1617
1618 /* Reexec has failed, fall back and continue */
1619 error("rexec of %s failed: %s", rexec_argv[0], strerror(errno));
Damien Miller035a5b42004-06-26 08:16:31 +10001620 recv_rexec_state(REEXEC_CONFIG_PASS_FD, NULL);
Darren Tucker645ab752004-06-25 13:33:20 +10001621 log_init(__progname, options.log_level,
1622 options.log_facility, log_stderr);
1623
1624 /* Clean up fds */
Damien Miller035a5b42004-06-26 08:16:31 +10001625 startup_pipe = REEXEC_STARTUP_PIPE_FD;
Darren Tucker645ab752004-06-25 13:33:20 +10001626 close(config_s[1]);
Damien Miller035a5b42004-06-26 08:16:31 +10001627 close(REEXEC_CONFIG_PASS_FD);
1628 newsock = sock_out = sock_in = dup(STDIN_FILENO);
Darren Tucker645ab752004-06-25 13:33:20 +10001629 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1630 dup2(fd, STDIN_FILENO);
1631 dup2(fd, STDOUT_FILENO);
1632 if (fd > STDERR_FILENO)
1633 close(fd);
1634 }
Damien Miller035a5b42004-06-26 08:16:31 +10001635 debug("rexec cleanup in %d out %d newsock %d pipe %d sock %d",
1636 sock_in, sock_out, newsock, startup_pipe, config_s[0]);
Darren Tucker645ab752004-06-25 13:33:20 +10001637 }
1638
Damien Miller5428f641999-11-25 11:54:57 +11001639 /*
1640 * Disable the key regeneration alarm. We will not regenerate the
1641 * key since we are no longer in a position to give it to anyone. We
1642 * will not restart on SIGHUP since it no longer makes sense.
1643 */
Damien Miller95def091999-11-25 00:26:21 +11001644 alarm(0);
1645 signal(SIGALRM, SIG_DFL);
1646 signal(SIGHUP, SIG_DFL);
1647 signal(SIGTERM, SIG_DFL);
1648 signal(SIGQUIT, SIG_DFL);
1649 signal(SIGCHLD, SIG_DFL);
Damien Miller4e0f5e12000-08-29 11:05:50 +11001650 signal(SIGINT, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001651
Damien Miller5428f641999-11-25 11:54:57 +11001652 /*
1653 * Register our connection. This turns encryption off because we do
1654 * not have a key.
1655 */
Damien Miller95def091999-11-25 00:26:21 +11001656 packet_set_connection(sock_in, sock_out);
Damien Miller9786e6e2005-07-26 21:54:56 +10001657 packet_set_server();
Damien Miller95def091999-11-25 00:26:21 +11001658
Damien Miller4f1d6b22005-05-26 11:59:32 +10001659 /* Set SO_KEEPALIVE if requested. */
1660 if (options.tcp_keep_alive && packet_connection_is_on_socket() &&
1661 setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0)
1662 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1663
Damien Miller677257f2005-06-17 12:55:03 +10001664 if ((remote_port = get_remote_port()) < 0) {
1665 debug("get_remote_port failed");
1666 cleanup_exit(255);
1667 }
Damien Miller4d3fd542005-11-05 15:13:24 +11001668
1669 /*
1670 * We use get_canonical_hostname with usedns = 0 instead of
1671 * get_remote_ipaddr here so IP options will be checked.
1672 */
1673 remote_ip = get_canonical_hostname(0);
Damien Miller95def091999-11-25 00:26:21 +11001674
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001675#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001676 audit_connection_from(remote_ip, remote_port);
1677#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001678#ifdef LIBWRAP
Damien Miller6a4a4b92001-11-12 11:07:11 +11001679 /* Check whether logins are denied from this host. */
Darren Tucker586b0b92004-06-25 13:34:31 +10001680 if (packet_connection_is_on_socket()) {
Damien Miller95def091999-11-25 00:26:21 +11001681 struct request_info req;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001682
Ben Lindstromce89dac2001-09-12 16:58:04 +00001683 request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, 0);
Damien Miller95def091999-11-25 00:26:21 +11001684 fromhost(&req);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001685
Damien Miller95def091999-11-25 00:26:21 +11001686 if (!hosts_access(&req)) {
Damien Miller6a4a4b92001-11-12 11:07:11 +11001687 debug("Connection refused by tcp wrapper");
Ben Lindstrom7de696e2001-03-29 00:45:12 +00001688 refuse(&req);
Damien Miller6a4a4b92001-11-12 11:07:11 +11001689 /* NOTREACHED */
1690 fatal("libwrap refuse returns");
Damien Miller95def091999-11-25 00:26:21 +11001691 }
Damien Miller95def091999-11-25 00:26:21 +11001692 }
Damien Miller34132e52000-01-14 15:45:46 +11001693#endif /* LIBWRAP */
Damien Miller6a4a4b92001-11-12 11:07:11 +11001694
Damien Miller95def091999-11-25 00:26:21 +11001695 /* Log the connection. */
1696 verbose("Connection from %.500s port %d", remote_ip, remote_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001697
Damien Miller5428f641999-11-25 11:54:57 +11001698 /*
Damien Miller788f2122005-11-05 15:14:59 +11001699 * We don't want to listen forever unless the other side
Damien Miller5428f641999-11-25 11:54:57 +11001700 * successfully authenticates itself. So we set up an alarm which is
1701 * cleared after successful authentication. A limit of zero
Damien Miller788f2122005-11-05 15:14:59 +11001702 * indicates no limit. Note that we don't set the alarm in debugging
Damien Miller5428f641999-11-25 11:54:57 +11001703 * mode; it is just annoying to have the server exit just when you
1704 * are about to discover the bug.
1705 */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00001706 signal(SIGALRM, grace_alarm_handler);
Damien Miller95def091999-11-25 00:26:21 +11001707 if (!debug_flag)
1708 alarm(options.login_grace_time);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001709
Damien Millerb38eff82000-04-01 11:09:21 +10001710 sshd_exchange_identification(sock_in, sock_out);
Darren Tuckerec960f22003-08-13 20:37:05 +10001711
Damien Miller95def091999-11-25 00:26:21 +11001712 packet_set_nonblocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001713
Darren Tucker3e33cec2003-10-02 16:12:36 +10001714 /* allocate authentication context */
Damien Miller07d86be2006-03-26 14:19:21 +11001715 authctxt = xcalloc(1, sizeof(*authctxt));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001716
Darren Tuckerf3bb4342005-03-31 21:39:25 +10001717 authctxt->loginmsg = &loginmsg;
1718
Darren Tucker3e33cec2003-10-02 16:12:36 +10001719 /* XXX global for cleanup, access from other modules */
1720 the_authctxt = authctxt;
1721
Darren Tucker5c14c732005-01-24 21:55:49 +11001722 /* prepare buffer to collect messages to display to user after login */
1723 buffer_init(&loginmsg);
1724
Ben Lindstrom943481c2002-03-22 03:43:46 +00001725 if (use_privsep)
Darren Tucker3e33cec2003-10-02 16:12:36 +10001726 if (privsep_preauth(authctxt) == 1)
Ben Lindstrom943481c2002-03-22 03:43:46 +00001727 goto authenticated;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001728
Damien Miller396691a2000-01-20 22:44:08 +11001729 /* perform the key exchange */
Damien Miller396691a2000-01-20 22:44:08 +11001730 /* authenticate user and start session */
Damien Millerefb4afe2000-04-12 18:45:05 +10001731 if (compat20) {
1732 do_ssh2_kex();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001733 do_authentication2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001734 } else {
1735 do_ssh1_kex();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001736 do_authentication(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001737 }
Ben Lindstrom943481c2002-03-22 03:43:46 +00001738 /*
1739 * If we use privilege separation, the unprivileged child transfers
1740 * the current keystate and exits
1741 */
1742 if (use_privsep) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001743 mm_send_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001744 exit(0);
Ben Lindstrom943481c2002-03-22 03:43:46 +00001745 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001746
1747 authenticated:
Damien Miller7bff1a92005-12-24 14:59:12 +11001748 /*
1749 * Cancel the alarm we set to limit the time taken for
1750 * authentication.
1751 */
1752 alarm(0);
1753 signal(SIGALRM, SIG_DFL);
1754 if (startup_pipe != -1) {
1755 close(startup_pipe);
1756 startup_pipe = -1;
1757 }
1758
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001759#ifdef SSH_AUDIT_EVENTS
1760 audit_event(SSH_AUTH_SUCCESS);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001761#endif
1762
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001763 /*
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001764 * In privilege separation, we fork another child and prepare
1765 * file descriptor passing.
1766 */
1767 if (use_privsep) {
Ben Lindstrom943481c2002-03-22 03:43:46 +00001768 privsep_postauth(authctxt);
1769 /* the monitor process [priv] will not return */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001770 if (!compat20)
1771 destroy_sensitive_data();
1772 }
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00001773
Darren Tucker3e33cec2003-10-02 16:12:36 +10001774 /* Start session. */
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00001775 do_authenticated(authctxt);
1776
Damien Miller3a5b0232002-03-13 13:19:42 +11001777 /* The connection has been terminated. */
1778 verbose("Closing connection to %.100s", remote_ip);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001779
Damien Millerbeb4ba51999-12-28 15:09:35 +11001780#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +10001781 if (options.use_pam)
1782 finish_pam();
Damien Millerbeb4ba51999-12-28 15:09:35 +11001783#endif /* USE_PAM */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001784
Darren Tucker2b59a6d2005-03-06 22:38:51 +11001785#ifdef SSH_AUDIT_EVENTS
1786 PRIVSEP(audit_event(SSH_CONNECTION_CLOSE));
1787#endif
1788
Damien Miller95def091999-11-25 00:26:21 +11001789 packet_close();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001790
1791 if (use_privsep)
1792 mm_terminate();
1793
Damien Miller95def091999-11-25 00:26:21 +11001794 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001795}
1796
Damien Miller95def091999-11-25 00:26:21 +11001797/*
Ben Lindstromabcb1452002-03-22 01:10:21 +00001798 * Decrypt session_key_int using our private server key and private host key
1799 * (key with larger modulus first).
1800 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001801int
Ben Lindstromabcb1452002-03-22 01:10:21 +00001802ssh1_session_key(BIGNUM *session_key_int)
1803{
1804 int rsafail = 0;
1805
Damien Millerf0b15df2006-03-26 13:59:20 +11001806 if (BN_cmp(sensitive_data.server_key->rsa->n,
1807 sensitive_data.ssh1_host_key->rsa->n) > 0) {
Ben Lindstromabcb1452002-03-22 01:10:21 +00001808 /* Server key has bigger modulus. */
1809 if (BN_num_bits(sensitive_data.server_key->rsa->n) <
Damien Millerf0b15df2006-03-26 13:59:20 +11001810 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1811 SSH_KEY_BITS_RESERVED) {
1812 fatal("do_connection: %s: "
1813 "server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
Ben Lindstromabcb1452002-03-22 01:10:21 +00001814 get_remote_ipaddr(),
1815 BN_num_bits(sensitive_data.server_key->rsa->n),
1816 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1817 SSH_KEY_BITS_RESERVED);
1818 }
1819 if (rsa_private_decrypt(session_key_int, session_key_int,
1820 sensitive_data.server_key->rsa) <= 0)
1821 rsafail++;
1822 if (rsa_private_decrypt(session_key_int, session_key_int,
1823 sensitive_data.ssh1_host_key->rsa) <= 0)
1824 rsafail++;
1825 } else {
1826 /* Host key has bigger modulus (or they are equal). */
1827 if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
Damien Millerf0b15df2006-03-26 13:59:20 +11001828 BN_num_bits(sensitive_data.server_key->rsa->n) +
1829 SSH_KEY_BITS_RESERVED) {
1830 fatal("do_connection: %s: "
1831 "host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
Ben Lindstromabcb1452002-03-22 01:10:21 +00001832 get_remote_ipaddr(),
1833 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1834 BN_num_bits(sensitive_data.server_key->rsa->n),
1835 SSH_KEY_BITS_RESERVED);
1836 }
1837 if (rsa_private_decrypt(session_key_int, session_key_int,
1838 sensitive_data.ssh1_host_key->rsa) < 0)
1839 rsafail++;
1840 if (rsa_private_decrypt(session_key_int, session_key_int,
1841 sensitive_data.server_key->rsa) < 0)
1842 rsafail++;
1843 }
1844 return (rsafail);
1845}
1846/*
Damien Miller396691a2000-01-20 22:44:08 +11001847 * SSH1 key exchange
Damien Miller95def091999-11-25 00:26:21 +11001848 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001849static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001850do_ssh1_kex(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001851{
Damien Miller95def091999-11-25 00:26:21 +11001852 int i, len;
Damien Miller7650bc62001-01-30 09:27:26 +11001853 int rsafail = 0;
Damien Miller95def091999-11-25 00:26:21 +11001854 BIGNUM *session_key_int;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001855 u_char session_key[SSH_SESSION_KEY_LENGTH];
1856 u_char cookie[8];
1857 u_int cipher_type, auth_mask, protocol_flags;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001858 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001859
Damien Miller5428f641999-11-25 11:54:57 +11001860 /*
1861 * Generate check bytes that the client must send back in the user
1862 * packet in order for it to be accepted; this is used to defy ip
1863 * spoofing attacks. Note that this only works against somebody
1864 * doing IP spoofing from a remote machine; any machine on the local
1865 * network can still see outgoing packets and catch the random
1866 * cookie. This only affects rhosts authentication, and this is one
1867 * of the reasons why it is inherently insecure.
1868 */
Damien Miller95def091999-11-25 00:26:21 +11001869 for (i = 0; i < 8; i++) {
1870 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001871 rnd = arc4random();
1872 cookie[i] = rnd & 0xff;
1873 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +11001874 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001875
Damien Miller5428f641999-11-25 11:54:57 +11001876 /*
1877 * Send our public key. We include in the packet 64 bits of random
1878 * data that must be matched in the reply in order to prevent IP
1879 * spoofing.
1880 */
Damien Miller95def091999-11-25 00:26:21 +11001881 packet_start(SSH_SMSG_PUBLIC_KEY);
1882 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001883 packet_put_char(cookie[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001884
Damien Miller95def091999-11-25 00:26:21 +11001885 /* Store our public server RSA key. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001886 packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
1887 packet_put_bignum(sensitive_data.server_key->rsa->e);
1888 packet_put_bignum(sensitive_data.server_key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001889
Damien Miller95def091999-11-25 00:26:21 +11001890 /* Store our public host RSA key. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001891 packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1892 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
1893 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001894
Damien Miller95def091999-11-25 00:26:21 +11001895 /* Put protocol flags. */
1896 packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001897
Damien Miller95def091999-11-25 00:26:21 +11001898 /* Declare which ciphers we support. */
Damien Miller874d77b2000-10-14 16:23:11 +11001899 packet_put_int(cipher_mask_ssh1(0));
Damien Miller95def091999-11-25 00:26:21 +11001900
1901 /* Declare supported authentication types. */
1902 auth_mask = 0;
Damien Miller95def091999-11-25 00:26:21 +11001903 if (options.rhosts_rsa_authentication)
1904 auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
1905 if (options.rsa_authentication)
1906 auth_mask |= 1 << SSH_AUTH_RSA;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001907 if (options.challenge_response_authentication == 1)
Damien Miller95def091999-11-25 00:26:21 +11001908 auth_mask |= 1 << SSH_AUTH_TIS;
Damien Miller95def091999-11-25 00:26:21 +11001909 if (options.password_authentication)
1910 auth_mask |= 1 << SSH_AUTH_PASSWORD;
1911 packet_put_int(auth_mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001912
Damien Miller95def091999-11-25 00:26:21 +11001913 /* Send the packet and wait for it to be sent. */
1914 packet_send();
1915 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001916
Damien Miller0bc1bd82000-11-13 22:57:25 +11001917 debug("Sent %d bit server key and %d bit host key.",
1918 BN_num_bits(sensitive_data.server_key->rsa->n),
1919 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001920
Damien Miller95def091999-11-25 00:26:21 +11001921 /* Read clients reply (cipher type and session key). */
Damien Millerdff50992002-01-22 23:16:32 +11001922 packet_read_expect(SSH_CMSG_SESSION_KEY);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001923
Damien Miller50945fa1999-12-09 10:31:37 +11001924 /* Get cipher type and check whether we accept this. */
Damien Miller95def091999-11-25 00:26:21 +11001925 cipher_type = packet_get_char();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001926
Damien Miller874d77b2000-10-14 16:23:11 +11001927 if (!(cipher_mask_ssh1(0) & (1 << cipher_type)))
Damien Miller50945fa1999-12-09 10:31:37 +11001928 packet_disconnect("Warning: client selects unsupported cipher.");
1929
Damien Miller95def091999-11-25 00:26:21 +11001930 /* Get check bytes from the packet. These must match those we
1931 sent earlier with the public key packet. */
1932 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001933 if (cookie[i] != packet_get_char())
Damien Miller95def091999-11-25 00:26:21 +11001934 packet_disconnect("IP Spoofing check bytes do not match.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001935
Damien Miller95def091999-11-25 00:26:21 +11001936 debug("Encryption type: %.200s", cipher_name(cipher_type));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001937
Damien Miller95def091999-11-25 00:26:21 +11001938 /* Get the encrypted integer. */
Damien Millerda755162002-01-22 23:09:22 +11001939 if ((session_key_int = BN_new()) == NULL)
1940 fatal("do_ssh1_kex: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +11001941 packet_get_bignum(session_key_int);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001942
Damien Miller95def091999-11-25 00:26:21 +11001943 protocol_flags = packet_get_int();
1944 packet_set_protocol_flags(protocol_flags);
Damien Miller48b03fc2002-01-22 23:11:40 +11001945 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001946
Ben Lindstromabcb1452002-03-22 01:10:21 +00001947 /* Decrypt session_key_int using host/server keys */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001948 rsafail = PRIVSEP(ssh1_session_key(session_key_int));
1949
Damien Miller5428f641999-11-25 11:54:57 +11001950 /*
1951 * Extract session key from the decrypted integer. The key is in the
1952 * least significant 256 bits of the integer; the first byte of the
1953 * key is in the highest bits.
1954 */
Damien Miller7650bc62001-01-30 09:27:26 +11001955 if (!rsafail) {
1956 BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1957 len = BN_num_bytes(session_key_int);
Damien Millereccb9de2005-06-17 12:59:34 +10001958 if (len < 0 || (u_int)len > sizeof(session_key)) {
Damien Miller7650bc62001-01-30 09:27:26 +11001959 error("do_connection: bad session key len from %s: "
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001960 "session_key_int %d > sizeof(session_key) %lu",
1961 get_remote_ipaddr(), len, (u_long)sizeof(session_key));
Damien Miller7650bc62001-01-30 09:27:26 +11001962 rsafail++;
1963 } else {
1964 memset(session_key, 0, sizeof(session_key));
1965 BN_bn2bin(session_key_int,
1966 session_key + sizeof(session_key) - len);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001967
Darren Tuckere14e0052004-05-13 16:30:44 +10001968 derive_ssh1_session_id(
Darren Tuckerfc959702004-07-17 16:12:08 +10001969 sensitive_data.ssh1_host_key->rsa->n,
Darren Tuckere14e0052004-05-13 16:30:44 +10001970 sensitive_data.server_key->rsa->n,
1971 cookie, session_id);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001972 /*
1973 * Xor the first 16 bytes of the session key with the
1974 * session id.
1975 */
1976 for (i = 0; i < 16; i++)
1977 session_key[i] ^= session_id[i];
Damien Miller7650bc62001-01-30 09:27:26 +11001978 }
1979 }
1980 if (rsafail) {
Ben Lindstromeb648a72001-03-05 06:00:29 +00001981 int bytes = BN_num_bytes(session_key_int);
Ben Lindstrom13c5d3b2002-02-26 18:00:48 +00001982 u_char *buf = xmalloc(bytes);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001983 MD5_CTX md;
1984
Damien Miller996acd22003-04-09 20:59:48 +10001985 logit("do_connection: generating a fake encryption key");
Ben Lindstromeb648a72001-03-05 06:00:29 +00001986 BN_bn2bin(session_key_int, buf);
1987 MD5_Init(&md);
1988 MD5_Update(&md, buf, bytes);
1989 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1990 MD5_Final(session_key, &md);
1991 MD5_Init(&md);
1992 MD5_Update(&md, session_key, 16);
1993 MD5_Update(&md, buf, bytes);
1994 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1995 MD5_Final(session_key + 16, &md);
1996 memset(buf, 0, bytes);
1997 xfree(buf);
Ben Lindstrom941ac822001-03-05 06:25:23 +00001998 for (i = 0; i < 16; i++)
1999 session_id[i] = session_key[i] ^ session_key[i + 16];
Damien Miller7650bc62001-01-30 09:27:26 +11002000 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002001 /* Destroy the private and public keys. No longer. */
Damien Miller3a5b0232002-03-13 13:19:42 +11002002 destroy_sensitive_data();
Ben Lindstromeb648a72001-03-05 06:00:29 +00002003
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002004 if (use_privsep)
2005 mm_ssh1_session_id(session_id);
2006
Damien Miller396691a2000-01-20 22:44:08 +11002007 /* Destroy the decrypted integer. It is no longer needed. */
2008 BN_clear_free(session_key_int);
2009
Damien Miller95def091999-11-25 00:26:21 +11002010 /* Set the session key. From this on all communications will be encrypted. */
2011 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002012
Damien Miller95def091999-11-25 00:26:21 +11002013 /* Destroy our copy of the session key. It is no longer needed. */
2014 memset(session_key, 0, sizeof(session_key));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002015
Damien Miller95def091999-11-25 00:26:21 +11002016 debug("Received session key; encryption turned on.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002017
Ben Lindstromf666fec2002-06-06 19:51:58 +00002018 /* Send an acknowledgment packet. Note that this packet is sent encrypted. */
Damien Miller95def091999-11-25 00:26:21 +11002019 packet_start(SSH_SMSG_SUCCESS);
2020 packet_send();
2021 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002022}
Damien Millerefb4afe2000-04-12 18:45:05 +10002023
2024/*
2025 * SSH2 key exchange: diffie-hellman-group1-sha1
2026 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002027static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00002028do_ssh2_kex(void)
Damien Millerefb4afe2000-04-12 18:45:05 +10002029{
Damien Millerefb4afe2000-04-12 18:45:05 +10002030 Kex *kex;
Damien Millerefb4afe2000-04-12 18:45:05 +10002031
Damien Miller78928792000-04-12 20:17:38 +10002032 if (options.ciphers != NULL) {
Damien Miller4af51302000-04-16 11:18:38 +10002033 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Damien Miller78928792000-04-12 20:17:38 +10002034 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
2035 }
Damien Millera0ff4662001-03-30 10:49:35 +10002036 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
2037 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
2038 myproposal[PROPOSAL_ENC_ALGS_STOC] =
2039 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
2040
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00002041 if (options.macs != NULL) {
2042 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
2043 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
2044 }
Damien Miller9786e6e2005-07-26 21:54:56 +10002045 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +00002046 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
2047 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
Damien Miller9786e6e2005-07-26 21:54:56 +10002048 } else if (options.compression == COMP_DELAYED) {
2049 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
2050 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com";
Ben Lindstrom23e0f662002-06-21 01:09:47 +00002051 }
Damien Millerf0b15df2006-03-26 13:59:20 +11002052
Damien Miller0bc1bd82000-11-13 22:57:25 +11002053 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = list_hostkey_types();
2054
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002055 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +00002056 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +11002057 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10002058 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Miller8e7fb332003-02-24 12:03:03 +11002059 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11002060 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00002061 kex->server = 1;
2062 kex->client_version_string=client_version_string;
2063 kex->server_version_string=server_version_string;
2064 kex->load_host_key=&get_hostkey_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002065 kex->host_key_index=&get_hostkey_index;
Damien Millerefb4afe2000-04-12 18:45:05 +10002066
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002067 xxx_kex = kex;
2068
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002069 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +11002070
Ben Lindstrom2d90e002001-04-04 02:00:54 +00002071 session_id2 = kex->session_id;
2072 session_id2_len = kex->session_id_len;
2073
Damien Miller874d77b2000-10-14 16:23:11 +11002074#ifdef DEBUG_KEXDH
2075 /* send 1st encrypted/maced/compressed message */
2076 packet_start(SSH2_MSG_IGNORE);
2077 packet_put_cstring("markus");
2078 packet_send();
2079 packet_write_wait();
2080#endif
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00002081 debug("KEX done");
Damien Millerefb4afe2000-04-12 18:45:05 +10002082}
Darren Tucker3e33cec2003-10-02 16:12:36 +10002083
2084/* server specific fatal cleanup */
2085void
2086cleanup_exit(int i)
2087{
2088 if (the_authctxt)
2089 do_cleanup(the_authctxt);
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11002090#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11002091 /* done after do_cleanup so it can cancel the PAM auth 'thread' */
2092 if (!use_privsep || mm_is_monitor())
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11002093 audit_event(SSH_CONNECTION_ABANDON);
Darren Tucker269a1ea2005-02-03 00:20:53 +11002094#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002095 _exit(i);
2096}