blob: f3fe9d1842a06108f6bcbcac32370eb2b96b8ca6 [file] [log] [blame]
Darren Tucker39972492006-07-12 22:22:46 +10001/* $OpenBSD: sshd.c,v 1.336 2006/07/11 20:07:25 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11006 * This program is the ssh daemon. It listens for connections from clients,
7 * and performs authentication, executes use commands or shell, and forwards
Damien Miller95def091999-11-25 00:26:21 +11008 * information to/from the application to the user client over an encrypted
Damien Millere4340be2000-09-16 13:29:08 +11009 * connection. This can also handle forwarding of X11, TCP/IP, and
10 * authentication agent connections.
Damien Millerefb4afe2000-04-12 18:45:05 +100011 *
Damien Millere4340be2000-09-16 13:29:08 +110012 * As far as I am concerned, the code I have written for this software
13 * can be used freely for any purpose. Any derived versions of this
14 * software must be clearly marked as such, and if the derived work is
15 * incompatible with the protocol description in the RFC file, it must be
16 * called by a name other than "ssh" or "Secure Shell".
17 *
18 * SSH2 implementation:
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000019 * Privilege Separation:
Damien Millere4340be2000-09-16 13:29:08 +110020 *
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000021 * Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved.
22 * Copyright (c) 2002 Niels Provos. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110023 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
34 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
36 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
37 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
42 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110043 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110046
Damien Miller9cf6d072006-03-15 11:29:24 +110047#include <sys/types.h>
Damien Millerf17883e2006-03-15 11:45:54 +110048#ifdef HAVE_SYS_STAT_H
49# include <sys/stat.h>
50#endif
51#include <sys/ioctl.h>
Damien Millere3b60b52006-07-10 21:08:03 +100052#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110053#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110054
Darren Tucker39972492006-07-12 22:22:46 +100055#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100056#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110057#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110058#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110059#endif
Damien Millera1738e42006-07-10 21:33:04 +100060#include <grp.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100061#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110062#include <signal.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include <openssl/dh.h>
65#include <openssl/bn.h>
Damien Miller6a47f302002-02-13 13:55:06 +110066#include <openssl/md5.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000067#include <openssl/rand.h>
Kevin Steves0ea1d9d2002-04-25 18:17:04 +000068#ifdef HAVE_SECUREWARE
69#include <sys/security.h>
70#include <prot.h>
71#endif
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072
73#include "ssh.h"
74#include "ssh1.h"
75#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076#include "xmalloc.h"
77#include "rsa.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000078#include "sshpty.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081#include "servconf.h"
82#include "uidswap.h"
83#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100084#include "buffer.h"
Darren Tucker645ab752004-06-25 13:33:20 +100085#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000086#include "cipher.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100087#include "kex.h"
Damien Millerb38eff82000-04-01 11:09:21 +100088#include "key.h"
Damien Miller874d77b2000-10-14 16:23:11 +110089#include "dh.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100090#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100091#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "pathnames.h"
93#include "atomicio.h"
94#include "canohost.h"
95#include "auth.h"
96#include "misc.h"
Darren Tucker645ab752004-06-25 13:33:20 +100097#include "msg.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000098#include "dispatch.h"
Ben Lindstrom1bae4042001-10-03 17:46:39 +000099#include "channels.h"
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +0000100#include "session.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101#include "monitor_mm.h"
102#include "monitor.h"
103#include "monitor_wrap.h"
104#include "monitor_fdpass.h"
Damien Millerb7576772006-07-10 20:23:39 +1000105#include "version.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
107#ifdef LIBWRAP
108#include <tcpd.h>
109#include <syslog.h>
110int allow_severity = LOG_INFO;
111int deny_severity = LOG_WARNING;
112#endif /* LIBWRAP */
113
114#ifndef O_NOCTTY
115#define O_NOCTTY 0
116#endif
117
Damien Miller035a5b42004-06-26 08:16:31 +1000118/* Re-exec fds */
119#define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1)
120#define REEXEC_STARTUP_PIPE_FD (STDERR_FILENO + 2)
121#define REEXEC_CONFIG_PASS_FD (STDERR_FILENO + 3)
122#define REEXEC_MIN_FREE_FD (STDERR_FILENO + 4)
123
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000124extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000125
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126/* Server configuration options. */
127ServerOptions options;
128
129/* Name of the server configuration file. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000130char *config_file_name = _PATH_SERVER_CONFIG_FILE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
Damien Miller4af51302000-04-16 11:18:38 +1000132/*
Damien Miller95def091999-11-25 00:26:21 +1100133 * Debug mode flag. This can be set on the command line. If debug
134 * mode is enabled, extra debugging output will be sent to the system
135 * log, the daemon will not go to background, and will exit after processing
136 * the first connection.
137 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138int debug_flag = 0;
139
Ben Lindstrom794325a2001-08-06 21:09:07 +0000140/* Flag indicating that the daemon should only test the configuration and keys. */
141int test_flag = 0;
142
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143/* Flag indicating that the daemon is being started from inetd. */
144int inetd_flag = 0;
145
Ben Lindstromc72745a2000-12-02 19:03:54 +0000146/* Flag indicating that sshd should not detach and become a daemon. */
147int no_daemon_flag = 0;
148
Damien Miller5ce662a1999-11-11 17:57:39 +1100149/* debug goes to stderr unless inetd_flag is set */
150int log_stderr = 0;
151
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152/* Saved arguments to main(). */
153char **saved_argv;
Damien Millerb8c656e2000-06-28 15:22:41 +1000154int saved_argc;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Darren Tucker645ab752004-06-25 13:33:20 +1000156/* re-exec */
157int rexeced_flag = 0;
158int rexec_flag = 1;
159int rexec_argc = 0;
160char **rexec_argv;
161
Damien Miller5428f641999-11-25 11:54:57 +1100162/*
Damien Miller34132e52000-01-14 15:45:46 +1100163 * The sockets that the server is listening; this is used in the SIGHUP
164 * signal handler.
Damien Miller5428f641999-11-25 11:54:57 +1100165 */
Damien Miller34132e52000-01-14 15:45:46 +1100166#define MAX_LISTEN_SOCKS 16
167int listen_socks[MAX_LISTEN_SOCKS];
168int num_listen_socks = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169
Damien Miller5428f641999-11-25 11:54:57 +1100170/*
171 * the client's version string, passed by sshd2 in compat mode. if != NULL,
172 * sshd will skip the version-number exchange
173 */
Damien Miller95def091999-11-25 00:26:21 +1100174char *client_version_string = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000175char *server_version_string = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000177/* for rekeying XXX fixme */
178Kex *xxx_kex;
179
Damien Miller5428f641999-11-25 11:54:57 +1100180/*
181 * Any really sensitive data in the application is contained in this
182 * structure. The idea is that this structure could be locked into memory so
183 * that the pages do not get written into swap. However, there are some
184 * problems. The private key contains BIGNUMs, and we do not (in principle)
185 * have access to the internals of them, and locking just the structure is
186 * not very useful. Currently, memory locking is not implemented.
187 */
Damien Miller95def091999-11-25 00:26:21 +1100188struct {
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000189 Key *server_key; /* ephemeral server key */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100190 Key *ssh1_host_key; /* ssh1 host key */
191 Key **host_keys; /* all private host keys */
192 int have_ssh1_key;
193 int have_ssh2_key;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000194 u_char ssh1_cookie[SSH_SESSION_KEY_LENGTH];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195} sensitive_data;
196
Damien Miller5428f641999-11-25 11:54:57 +1100197/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000198 * Flag indicating whether the RSA server key needs to be regenerated.
199 * Is set in the SIGALRM handler and cleared when the key is regenerated.
Damien Miller5428f641999-11-25 11:54:57 +1100200 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000201static volatile sig_atomic_t key_do_regen = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000203/* This is set to true when a signal is received. */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000204static volatile sig_atomic_t received_sighup = 0;
205static volatile sig_atomic_t received_sigterm = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206
Damien Millerb38eff82000-04-01 11:09:21 +1000207/* session identifier, used by RSA-auth */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000208u_char session_id[16];
Damien Millerb38eff82000-04-01 11:09:21 +1000209
Damien Millereba71ba2000-04-29 23:57:08 +1000210/* same for ssh2 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000211u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000212u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000213
Damien Miller942da032000-08-18 13:59:06 +1000214/* record remote hostname or ip */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000215u_int utmp_len = MAXHOSTNAMELEN;
Damien Miller942da032000-08-18 13:59:06 +1000216
Ben Lindstromd84df982001-12-06 16:35:40 +0000217/* options.max_startup sized array of fd ints */
218int *startup_pipes = NULL;
219int startup_pipe; /* in child */
220
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000221/* variables used for privilege separation */
Damien Miller8e7fb332003-02-24 12:03:03 +1100222int use_privsep;
Darren Tuckera8be9e22004-02-06 16:40:27 +1100223struct monitor *pmonitor = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000224
Darren Tucker3e33cec2003-10-02 16:12:36 +1000225/* global authentication context */
226Authctxt *the_authctxt = NULL;
227
Darren Tucker09991742004-07-17 17:05:14 +1000228/* message to be displayed after login */
229Buffer loginmsg;
230
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231/* Prototypes for various functions defined later in this file. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000232void destroy_sensitive_data(void);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000233void demote_sensitive_data(void);
Damien Miller98c7ad62000-03-09 21:27:49 +1100234
Ben Lindstrombba81212001-06-25 05:01:22 +0000235static void do_ssh1_kex(void);
236static void do_ssh2_kex(void);
Damien Miller874d77b2000-10-14 16:23:11 +1100237
Damien Miller98c7ad62000-03-09 21:27:49 +1100238/*
Damien Miller34132e52000-01-14 15:45:46 +1100239 * Close all listening sockets
240 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000241static void
Damien Miller34132e52000-01-14 15:45:46 +1100242close_listen_socks(void)
243{
244 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000245
Damien Miller34132e52000-01-14 15:45:46 +1100246 for (i = 0; i < num_listen_socks; i++)
247 close(listen_socks[i]);
248 num_listen_socks = -1;
249}
250
Ben Lindstromd84df982001-12-06 16:35:40 +0000251static void
252close_startup_pipes(void)
253{
254 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000255
Ben Lindstromd84df982001-12-06 16:35:40 +0000256 if (startup_pipes)
257 for (i = 0; i < options.max_startups; i++)
258 if (startup_pipes[i] != -1)
259 close(startup_pipes[i]);
260}
261
Damien Miller34132e52000-01-14 15:45:46 +1100262/*
Damien Miller95def091999-11-25 00:26:21 +1100263 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
264 * the effect is to reread the configuration file (and to regenerate
265 * the server key).
266 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100267
268/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000269static void
Damien Miller95def091999-11-25 00:26:21 +1100270sighup_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271{
Ben Lindstrom07958482001-12-06 16:19:01 +0000272 int save_errno = errno;
273
Damien Miller95def091999-11-25 00:26:21 +1100274 received_sighup = 1;
275 signal(SIGHUP, sighup_handler);
Ben Lindstrom07958482001-12-06 16:19:01 +0000276 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277}
278
Damien Miller95def091999-11-25 00:26:21 +1100279/*
280 * Called from the main program after receiving SIGHUP.
281 * Restarts the server.
282 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000283static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000284sighup_restart(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285{
Damien Miller996acd22003-04-09 20:59:48 +1000286 logit("Received SIGHUP; restarting.");
Damien Miller34132e52000-01-14 15:45:46 +1100287 close_listen_socks();
Ben Lindstromd84df982001-12-06 16:35:40 +0000288 close_startup_pipes();
Damien Miller95def091999-11-25 00:26:21 +1100289 execv(saved_argv[0], saved_argv);
Damien Miller996acd22003-04-09 20:59:48 +1000290 logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
Ben Lindstrom822b6342002-06-23 21:38:49 +0000291 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100292 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293}
294
Damien Miller95def091999-11-25 00:26:21 +1100295/*
296 * Generic signal handler for terminating signals in the master daemon.
Damien Miller95def091999-11-25 00:26:21 +1100297 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100298/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000299static void
Damien Miller95def091999-11-25 00:26:21 +1100300sigterm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000302 received_sigterm = sig;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303}
304
Damien Miller95def091999-11-25 00:26:21 +1100305/*
306 * SIGCHLD handler. This is called whenever a child dies. This will then
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000307 * reap any zombies left by exited children.
Damien Miller95def091999-11-25 00:26:21 +1100308 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100309/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000310static void
Damien Miller95def091999-11-25 00:26:21 +1100311main_sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312{
Damien Miller95def091999-11-25 00:26:21 +1100313 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000314 pid_t pid;
Damien Miller95def091999-11-25 00:26:21 +1100315 int status;
Damien Miller431f66b1999-11-21 18:31:57 +1100316
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000317 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
318 (pid < 0 && errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100319 ;
Damien Miller431f66b1999-11-21 18:31:57 +1100320
Damien Miller95def091999-11-25 00:26:21 +1100321 signal(SIGCHLD, main_sigchld_handler);
322 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323}
324
Damien Miller95def091999-11-25 00:26:21 +1100325/*
326 * Signal handler for the alarm after the login grace period has expired.
327 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100328/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000329static void
Damien Miller95def091999-11-25 00:26:21 +1100330grace_alarm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000332 /* XXX no idea how fix this signal handler */
333
Darren Tuckera8be9e22004-02-06 16:40:27 +1100334 if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0)
335 kill(pmonitor->m_pid, SIGALRM);
336
Damien Miller95def091999-11-25 00:26:21 +1100337 /* Log error and exit. */
Damien Millerd27a76d2002-09-27 13:22:31 +1000338 fatal("Timeout before authentication for %s", get_remote_ipaddr());
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339}
340
Damien Miller95def091999-11-25 00:26:21 +1100341/*
Damien Miller95def091999-11-25 00:26:21 +1100342 * Signal handler for the key regeneration alarm. Note that this
343 * alarm only occurs in the daemon waiting for connections, and it does not
344 * do anything with the private key or random state before forking.
345 * Thus there should be no concurrency control/asynchronous execution
346 * problems.
347 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000348static void
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000349generate_ephemeral_server_key(void)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100350{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000351 u_int32_t rnd = 0;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000352 int i;
353
Ben Lindstroma3700052001-04-05 23:26:32 +0000354 verbose("Generating %s%d bit RSA key.",
Damien Millerff75ac42001-03-30 10:50:32 +1000355 sensitive_data.server_key ? "new " : "", options.server_key_bits);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100356 if (sensitive_data.server_key != NULL)
357 key_free(sensitive_data.server_key);
Ben Lindstroma3700052001-04-05 23:26:32 +0000358 sensitive_data.server_key = key_generate(KEY_RSA1,
Damien Millerff75ac42001-03-30 10:50:32 +1000359 options.server_key_bits);
360 verbose("RSA key generation complete.");
Ben Lindstromeb648a72001-03-05 06:00:29 +0000361
362 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
363 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000364 rnd = arc4random();
365 sensitive_data.ssh1_cookie[i] = rnd & 0xff;
366 rnd >>= 8;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000367 }
368 arc4random_stir();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100369}
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000370
Damien Millerf0b15df2006-03-26 13:59:20 +1100371/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000372static void
Damien Miller95def091999-11-25 00:26:21 +1100373key_regeneration_alarm(int sig)
374{
375 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000376
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000377 signal(SIGALRM, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +1100378 errno = save_errno;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000379 key_do_regen = 1;
Damien Miller95def091999-11-25 00:26:21 +1100380}
381
Ben Lindstrombba81212001-06-25 05:01:22 +0000382static void
Damien Millerb38eff82000-04-01 11:09:21 +1000383sshd_exchange_identification(int sock_in, int sock_out)
384{
Damien Millereccb9de2005-06-17 12:59:34 +1000385 u_int i;
386 int mismatch;
Damien Millerb38eff82000-04-01 11:09:21 +1000387 int remote_major, remote_minor;
Damien Miller78928792000-04-12 20:17:38 +1000388 int major, minor;
Damien Millerb38eff82000-04-01 11:09:21 +1000389 char *s;
390 char buf[256]; /* Must not be larger than remote_version. */
391 char remote_version[256]; /* Must be at least as big as buf. */
392
Damien Miller78928792000-04-12 20:17:38 +1000393 if ((options.protocol & SSH_PROTO_1) &&
394 (options.protocol & SSH_PROTO_2)) {
395 major = PROTOCOL_MAJOR_1;
396 minor = 99;
397 } else if (options.protocol & SSH_PROTO_2) {
398 major = PROTOCOL_MAJOR_2;
399 minor = PROTOCOL_MINOR_2;
400 } else {
401 major = PROTOCOL_MAJOR_1;
402 minor = PROTOCOL_MINOR_1;
403 }
404 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
Damien Millerb38eff82000-04-01 11:09:21 +1000405 server_version_string = xstrdup(buf);
406
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000407 /* Send our protocol version identification. */
408 if (atomicio(vwrite, sock_out, server_version_string,
409 strlen(server_version_string))
410 != strlen(server_version_string)) {
411 logit("Could not write ident string to %s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000412 cleanup_exit(255);
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000413 }
414
415 /* Read other sides version identification. */
416 memset(buf, 0, sizeof(buf));
417 for (i = 0; i < sizeof(buf) - 1; i++) {
418 if (atomicio(read, sock_in, &buf[i], 1) != 1) {
419 logit("Did not receive identification string from %s",
420 get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000421 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000422 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000423 if (buf[i] == '\r') {
424 buf[i] = 0;
425 /* Kludge for F-Secure Macintosh < 1.0.2 */
426 if (i == 12 &&
427 strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000428 break;
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000429 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000430 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000431 if (buf[i] == '\n') {
432 buf[i] = 0;
433 break;
434 }
Damien Millerb38eff82000-04-01 11:09:21 +1000435 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000436 buf[sizeof(buf) - 1] = 0;
437 client_version_string = xstrdup(buf);
Damien Millerb38eff82000-04-01 11:09:21 +1000438
439 /*
440 * Check that the versions match. In future this might accept
441 * several versions and set appropriate flags to handle them.
442 */
443 if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
444 &remote_major, &remote_minor, remote_version) != 3) {
Damien Miller4af51302000-04-16 11:18:38 +1000445 s = "Protocol mismatch.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000446 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Millerb38eff82000-04-01 11:09:21 +1000447 close(sock_in);
448 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000449 logit("Bad protocol version identification '%.100s' from %s",
Damien Millerb38eff82000-04-01 11:09:21 +1000450 client_version_string, get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000451 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000452 }
453 debug("Client protocol version %d.%d; client software version %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100454 remote_major, remote_minor, remote_version);
Damien Millerb38eff82000-04-01 11:09:21 +1000455
Damien Millerefb4afe2000-04-12 18:45:05 +1000456 compat_datafellows(remote_version);
457
Damien Millere9264972002-09-30 11:59:21 +1000458 if (datafellows & SSH_BUG_PROBE) {
Damien Miller996acd22003-04-09 20:59:48 +1000459 logit("probed from %s with %s. Don't panic.",
Damien Millere9264972002-09-30 11:59:21 +1000460 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000461 cleanup_exit(255);
Damien Millere9264972002-09-30 11:59:21 +1000462 }
463
Damien Miller27dbe6f2001-03-19 22:36:20 +1100464 if (datafellows & SSH_BUG_SCANNER) {
Damien Miller996acd22003-04-09 20:59:48 +1000465 logit("scanned from %s with %s. Don't panic.",
Damien Miller27dbe6f2001-03-19 22:36:20 +1100466 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000467 cleanup_exit(255);
Damien Miller27dbe6f2001-03-19 22:36:20 +1100468 }
469
Damien Miller78928792000-04-12 20:17:38 +1000470 mismatch = 0;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000471 switch (remote_major) {
Damien Millerb38eff82000-04-01 11:09:21 +1000472 case 1:
Damien Millereba71ba2000-04-29 23:57:08 +1000473 if (remote_minor == 99) {
474 if (options.protocol & SSH_PROTO_2)
475 enable_compat20();
476 else
477 mismatch = 1;
478 break;
479 }
Damien Miller78928792000-04-12 20:17:38 +1000480 if (!(options.protocol & SSH_PROTO_1)) {
481 mismatch = 1;
482 break;
483 }
Damien Millerb38eff82000-04-01 11:09:21 +1000484 if (remote_minor < 3) {
Damien Miller37023962000-07-11 17:31:38 +1000485 packet_disconnect("Your ssh version is too old and "
Damien Millerb38eff82000-04-01 11:09:21 +1000486 "is no longer supported. Please install a newer version.");
487 } else if (remote_minor == 3) {
488 /* note that this disables agent-forwarding */
489 enable_compat13();
490 }
Damien Miller78928792000-04-12 20:17:38 +1000491 break;
Damien Millerefb4afe2000-04-12 18:45:05 +1000492 case 2:
Damien Miller78928792000-04-12 20:17:38 +1000493 if (options.protocol & SSH_PROTO_2) {
Damien Millerefb4afe2000-04-12 18:45:05 +1000494 enable_compat20();
495 break;
496 }
497 /* FALLTHROUGH */
Damien Miller4af51302000-04-16 11:18:38 +1000498 default:
Damien Miller78928792000-04-12 20:17:38 +1000499 mismatch = 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000500 break;
501 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000502 chop(server_version_string);
Damien Miller78928792000-04-12 20:17:38 +1000503 debug("Local version string %.200s", server_version_string);
504
505 if (mismatch) {
506 s = "Protocol major versions differ.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000507 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Miller78928792000-04-12 20:17:38 +1000508 close(sock_in);
509 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000510 logit("Protocol major versions differ for %s: %.200s vs. %.200s",
Damien Miller78928792000-04-12 20:17:38 +1000511 get_remote_ipaddr(),
512 server_version_string, client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000513 cleanup_exit(255);
Damien Miller78928792000-04-12 20:17:38 +1000514 }
Damien Millereba71ba2000-04-29 23:57:08 +1000515}
516
Damien Miller0bc1bd82000-11-13 22:57:25 +1100517/* Destroy the host and server keys. They will no longer be needed. */
Damien Millereba71ba2000-04-29 23:57:08 +1000518void
519destroy_sensitive_data(void)
520{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100521 int i;
522
523 if (sensitive_data.server_key) {
524 key_free(sensitive_data.server_key);
525 sensitive_data.server_key = NULL;
526 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100527 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100528 if (sensitive_data.host_keys[i]) {
529 key_free(sensitive_data.host_keys[i]);
530 sensitive_data.host_keys[i] = NULL;
531 }
532 }
533 sensitive_data.ssh1_host_key = NULL;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000534 memset(sensitive_data.ssh1_cookie, 0, SSH_SESSION_KEY_LENGTH);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100535}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100536
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000537/* Demote private to public keys for network child */
538void
539demote_sensitive_data(void)
540{
541 Key *tmp;
542 int i;
543
544 if (sensitive_data.server_key) {
545 tmp = key_demote(sensitive_data.server_key);
546 key_free(sensitive_data.server_key);
547 sensitive_data.server_key = tmp;
548 }
549
550 for (i = 0; i < options.num_host_key_files; i++) {
551 if (sensitive_data.host_keys[i]) {
552 tmp = key_demote(sensitive_data.host_keys[i]);
553 key_free(sensitive_data.host_keys[i]);
554 sensitive_data.host_keys[i] = tmp;
555 if (tmp->type == KEY_RSA1)
556 sensitive_data.ssh1_host_key = tmp;
557 }
558 }
559
560 /* We do not clear ssh1_host key and cookie. XXX - Okay Niels? */
561}
562
Ben Lindstrom08105192002-03-22 02:50:06 +0000563static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000564privsep_preauth_child(void)
565{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000566 u_int32_t rnd[256];
Ben Lindstrom810af962002-07-04 00:11:40 +0000567 gid_t gidset[1];
Ben Lindstromc7431342002-03-22 03:11:49 +0000568 struct passwd *pw;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000569 int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000570
571 /* Enable challenge-response authentication for privilege separation */
572 privsep_challenge_enable();
573
574 for (i = 0; i < 256; i++)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000575 rnd[i] = arc4random();
576 RAND_seed(rnd, sizeof(rnd));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577
578 /* Demote the private keys to public keys. */
579 demote_sensitive_data();
580
Ben Lindstromc7431342002-03-22 03:11:49 +0000581 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
Damien Miller0150c652002-04-24 09:49:09 +1000582 fatal("Privilege separation user %s does not exist",
583 SSH_PRIVSEP_USER);
Ben Lindstromc7431342002-03-22 03:11:49 +0000584 memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
585 endpwent();
586
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000587 /* Change our root directory */
Ben Lindstrom7a7edf72002-03-22 02:42:37 +0000588 if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
589 fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
590 strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000591 if (chdir("/") == -1)
Ben Lindstrom1ee9ec32002-03-22 03:14:45 +0000592 fatal("chdir(\"/\"): %s", strerror(errno));
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000593
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594 /* Drop our privileges */
Ben Lindstromc7431342002-03-22 03:11:49 +0000595 debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
596 (u_int)pw->pw_gid);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000597#if 0
Darren Tuckerd5920482004-02-29 20:11:30 +1100598 /* XXX not ready, too heavy after chroot */
Ben Lindstromc7431342002-03-22 03:11:49 +0000599 do_setusercontext(pw);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000600#else
601 gidset[0] = pw->pw_gid;
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000602 if (setgroups(1, gidset) < 0)
603 fatal("setgroups: %.100s", strerror(errno));
604 permanently_set_uid(pw);
605#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000606}
607
Darren Tucker3e33cec2003-10-02 16:12:36 +1000608static int
609privsep_preauth(Authctxt *authctxt)
Ben Lindstrom943481c2002-03-22 03:43:46 +0000610{
Ben Lindstrom943481c2002-03-22 03:43:46 +0000611 int status;
612 pid_t pid;
613
614 /* Set up unprivileged child process to deal with network data */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000615 pmonitor = monitor_init();
Ben Lindstrom943481c2002-03-22 03:43:46 +0000616 /* Store a pointer to the kex for later rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000617 pmonitor->m_pkex = &xxx_kex;
Ben Lindstrom943481c2002-03-22 03:43:46 +0000618
619 pid = fork();
620 if (pid == -1) {
621 fatal("fork of unprivileged child failed");
622 } else if (pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000623 debug2("Network child is on pid %ld", (long)pid);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000624
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000625 close(pmonitor->m_recvfd);
Darren Tuckera8be9e22004-02-06 16:40:27 +1100626 pmonitor->m_pid = pid;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000627 monitor_child_preauth(authctxt, pmonitor);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000628 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000629
630 /* Sync memory */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000631 monitor_sync(pmonitor);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000632
633 /* Wait for the child's exit status */
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000634 while (waitpid(pid, &status, 0) < 0)
635 if (errno != EINTR)
636 break;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000637 return (1);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000638 } else {
639 /* child */
640
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000641 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000642
643 /* Demote the child */
644 if (getuid() == 0 || geteuid() == 0)
645 privsep_preauth_child();
Ben Lindstromf90f58d2002-03-26 01:53:03 +0000646 setproctitle("%s", "[net]");
Ben Lindstrom943481c2002-03-22 03:43:46 +0000647 }
Darren Tucker3e33cec2003-10-02 16:12:36 +1000648 return (0);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000649}
650
Ben Lindstrom08105192002-03-22 02:50:06 +0000651static void
Ben Lindstrom943481c2002-03-22 03:43:46 +0000652privsep_postauth(Authctxt *authctxt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000653{
Tim Rice9dd30812002-07-07 13:43:36 -0700654#ifdef DISABLE_FD_PASSING
Tim Rice8eff3192002-06-25 15:35:15 -0700655 if (1) {
656#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000657 if (authctxt->pw->pw_uid == 0 || options.use_login) {
Tim Rice8eff3192002-06-25 15:35:15 -0700658#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000659 /* File descriptor passing is broken or root login */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000660 use_privsep = 0;
Darren Tucker45b01422005-10-03 18:20:00 +1000661 goto skip;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000662 }
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000663
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000664 /* New socket pair */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000665 monitor_reinit(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000666
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000667 pmonitor->m_pid = fork();
668 if (pmonitor->m_pid == -1)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669 fatal("fork of unprivileged child failed");
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000670 else if (pmonitor->m_pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000671 debug2("User child is on pid %ld", (long)pmonitor->m_pid);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000672 close(pmonitor->m_recvfd);
Darren Tuckereb578622004-08-12 23:08:14 +1000673 buffer_clear(&loginmsg);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000674 monitor_child_postauth(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000675
676 /* NEVERREACHED */
677 exit(0);
678 }
679
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000680 close(pmonitor->m_sendfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000681
682 /* Demote the private keys to public keys. */
683 demote_sensitive_data();
684
685 /* Drop privileges */
686 do_setusercontext(authctxt->pw);
687
Darren Tucker45b01422005-10-03 18:20:00 +1000688 skip:
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000689 /* It is safe now to apply the key state */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000690 monitor_apply_keystate(pmonitor);
Damien Miller9786e6e2005-07-26 21:54:56 +1000691
692 /*
693 * Tell the packet layer that authentication was successful, since
694 * this information is not part of the key state.
695 */
696 packet_set_authenticated();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000697}
698
Ben Lindstrombba81212001-06-25 05:01:22 +0000699static char *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100700list_hostkey_types(void)
701{
Damien Miller0e3b8722002-01-22 23:26:38 +1100702 Buffer b;
Damien Millerf58b58c2003-11-17 21:18:23 +1100703 const char *p;
704 char *ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100705 int i;
Damien Miller0e3b8722002-01-22 23:26:38 +1100706
707 buffer_init(&b);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100708 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100709 Key *key = sensitive_data.host_keys[i];
710 if (key == NULL)
711 continue;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000712 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100713 case KEY_RSA:
714 case KEY_DSA:
Damien Miller0e3b8722002-01-22 23:26:38 +1100715 if (buffer_len(&b) > 0)
716 buffer_append(&b, ",", 1);
717 p = key_ssh_name(key);
718 buffer_append(&b, p, strlen(p));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100719 break;
720 }
721 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100722 buffer_append(&b, "\0", 1);
Damien Millerf58b58c2003-11-17 21:18:23 +1100723 ret = xstrdup(buffer_ptr(&b));
Damien Miller0e3b8722002-01-22 23:26:38 +1100724 buffer_free(&b);
Damien Millerf58b58c2003-11-17 21:18:23 +1100725 debug("list_hostkey_types: %s", ret);
726 return ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100727}
728
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000729Key *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100730get_hostkey_by_type(int type)
731{
732 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000733
Damien Miller9f0f5c62001-12-21 14:45:46 +1100734 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100735 Key *key = sensitive_data.host_keys[i];
736 if (key != NULL && key->type == type)
737 return key;
738 }
739 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000740}
741
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000742Key *
743get_hostkey_by_index(int ind)
744{
745 if (ind < 0 || ind >= options.num_host_key_files)
746 return (NULL);
747 return (sensitive_data.host_keys[ind]);
748}
749
750int
751get_hostkey_index(Key *key)
752{
753 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000754
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000755 for (i = 0; i < options.num_host_key_files; i++) {
756 if (key == sensitive_data.host_keys[i])
757 return (i);
758 }
759 return (-1);
760}
761
Damien Miller942da032000-08-18 13:59:06 +1000762/*
763 * returns 1 if connection should be dropped, 0 otherwise.
764 * dropping starts at connection #max_startups_begin with a probability
765 * of (max_startups_rate/100). the probability increases linearly until
766 * all connections are dropped for startups > max_startups
767 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000768static int
Damien Miller942da032000-08-18 13:59:06 +1000769drop_connection(int startups)
770{
Darren Tucker178fa662004-11-05 20:09:09 +1100771 int p, r;
Damien Miller942da032000-08-18 13:59:06 +1000772
773 if (startups < options.max_startups_begin)
774 return 0;
775 if (startups >= options.max_startups)
776 return 1;
777 if (options.max_startups_rate == 100)
778 return 1;
779
780 p = 100 - options.max_startups_rate;
781 p *= startups - options.max_startups_begin;
Darren Tucker178fa662004-11-05 20:09:09 +1100782 p /= options.max_startups - options.max_startups_begin;
Damien Miller942da032000-08-18 13:59:06 +1000783 p += options.max_startups_rate;
Darren Tucker178fa662004-11-05 20:09:09 +1100784 r = arc4random() % 100;
Damien Miller942da032000-08-18 13:59:06 +1000785
Darren Tucker3269b132004-11-05 20:20:59 +1100786 debug("drop_connection: p %d, r %d", p, r);
Damien Miller942da032000-08-18 13:59:06 +1000787 return (r < p) ? 1 : 0;
788}
789
Ben Lindstromade03f62001-12-06 18:22:17 +0000790static void
791usage(void)
792{
Damien Miller0c889cd2004-03-22 09:36:00 +1100793 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000794 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Millerb4087862004-03-22 09:35:21 +1100795 fprintf(stderr,
796"usage: sshd [-46Ddeiqt] [-b bits] [-f config_file] [-g login_grace_time]\n"
797" [-h host_key_file] [-k key_gen_time] [-o option] [-p port] [-u len]\n"
798 );
Ben Lindstromade03f62001-12-06 18:22:17 +0000799 exit(1);
800}
801
Darren Tucker645ab752004-06-25 13:33:20 +1000802static void
803send_rexec_state(int fd, Buffer *conf)
804{
805 Buffer m;
806
807 debug3("%s: entering fd = %d config len %d", __func__, fd,
808 buffer_len(conf));
809
810 /*
811 * Protocol from reexec master to child:
812 * string configuration
813 * u_int ephemeral_key_follows
814 * bignum e (only if ephemeral_key_follows == 1)
815 * bignum n "
816 * bignum d "
817 * bignum iqmp "
818 * bignum p "
819 * bignum q "
Darren Tuckerc6f82192005-09-27 22:46:32 +1000820 * string rngseed (only if OpenSSL is not self-seeded)
Darren Tucker645ab752004-06-25 13:33:20 +1000821 */
822 buffer_init(&m);
823 buffer_put_cstring(&m, buffer_ptr(conf));
824
Darren Tuckerfc959702004-07-17 16:12:08 +1000825 if (sensitive_data.server_key != NULL &&
Darren Tucker645ab752004-06-25 13:33:20 +1000826 sensitive_data.server_key->type == KEY_RSA1) {
827 buffer_put_int(&m, 1);
828 buffer_put_bignum(&m, sensitive_data.server_key->rsa->e);
829 buffer_put_bignum(&m, sensitive_data.server_key->rsa->n);
830 buffer_put_bignum(&m, sensitive_data.server_key->rsa->d);
831 buffer_put_bignum(&m, sensitive_data.server_key->rsa->iqmp);
832 buffer_put_bignum(&m, sensitive_data.server_key->rsa->p);
833 buffer_put_bignum(&m, sensitive_data.server_key->rsa->q);
834 } else
835 buffer_put_int(&m, 0);
836
Darren Tuckerc6f82192005-09-27 22:46:32 +1000837#ifndef OPENSSL_PRNG_ONLY
838 rexec_send_rng_seed(&m);
839#endif
840
Darren Tucker645ab752004-06-25 13:33:20 +1000841 if (ssh_msg_send(fd, 0, &m) == -1)
842 fatal("%s: ssh_msg_send failed", __func__);
843
844 buffer_free(&m);
845
846 debug3("%s: done", __func__);
847}
848
849static void
850recv_rexec_state(int fd, Buffer *conf)
851{
852 Buffer m;
853 char *cp;
854 u_int len;
855
856 debug3("%s: entering fd = %d", __func__, fd);
857
858 buffer_init(&m);
859
860 if (ssh_msg_recv(fd, &m) == -1)
861 fatal("%s: ssh_msg_recv failed", __func__);
862 if (buffer_get_char(&m) != 0)
863 fatal("%s: rexec version mismatch", __func__);
864
865 cp = buffer_get_string(&m, &len);
866 if (conf != NULL)
867 buffer_append(conf, cp, len + 1);
868 xfree(cp);
869
870 if (buffer_get_int(&m)) {
871 if (sensitive_data.server_key != NULL)
872 key_free(sensitive_data.server_key);
873 sensitive_data.server_key = key_new_private(KEY_RSA1);
874 buffer_get_bignum(&m, sensitive_data.server_key->rsa->e);
875 buffer_get_bignum(&m, sensitive_data.server_key->rsa->n);
876 buffer_get_bignum(&m, sensitive_data.server_key->rsa->d);
877 buffer_get_bignum(&m, sensitive_data.server_key->rsa->iqmp);
878 buffer_get_bignum(&m, sensitive_data.server_key->rsa->p);
879 buffer_get_bignum(&m, sensitive_data.server_key->rsa->q);
880 rsa_generate_additional_parameters(
881 sensitive_data.server_key->rsa);
882 }
Darren Tuckerc6f82192005-09-27 22:46:32 +1000883
884#ifndef OPENSSL_PRNG_ONLY
885 rexec_recv_rng_seed(&m);
886#endif
887
Darren Tucker645ab752004-06-25 13:33:20 +1000888 buffer_free(&m);
889
890 debug3("%s: done", __func__);
891}
892
Damien Miller95def091999-11-25 00:26:21 +1100893/*
894 * Main program for the daemon.
895 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896int
897main(int ac, char **av)
898{
Damien Miller95def091999-11-25 00:26:21 +1100899 extern char *optarg;
900 extern int optind;
Damien Miller07d86be2006-03-26 14:19:21 +1100901 int opt, j, i, on = 1;
Damien Miller386c6a22004-06-30 22:40:20 +1000902 int sock_in = -1, sock_out = -1, newsock = -1;
Damien Miller166fca82000-04-20 07:42:21 +1000903 pid_t pid;
Damien Miller34132e52000-01-14 15:45:46 +1100904 socklen_t fromlen;
Damien Miller34132e52000-01-14 15:45:46 +1100905 fd_set *fdset;
906 struct sockaddr_storage from;
Damien Miller95def091999-11-25 00:26:21 +1100907 const char *remote_ip;
908 int remote_port;
Damien Miller95def091999-11-25 00:26:21 +1100909 FILE *f;
Damien Miller34132e52000-01-14 15:45:46 +1100910 struct addrinfo *ai;
911 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerb9997192003-12-17 16:29:22 +1100912 char *line;
Damien Miller34132e52000-01-14 15:45:46 +1100913 int listen_sock, maxfd;
Darren Tucker0f56ed12004-08-29 16:38:41 +1000914 int startup_p[2] = { -1 , -1 }, config_s[2] = { -1 , -1 };
Damien Miller37023962000-07-11 17:31:38 +1000915 int startups = 0;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000916 Key *key;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000917 Authctxt *authctxt;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000918 int ret, key_used = 0;
Darren Tucker645ab752004-06-25 13:33:20 +1000919 Buffer cfg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920
Kevin Steves0ea1d9d2002-04-25 18:17:04 +0000921#ifdef HAVE_SECUREWARE
922 (void)set_auth_parameters(ac, av);
923#endif
Damien Miller59d3d5b2003-08-22 09:34:41 +1000924 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000925 init_rng();
926
Damien Millera8ed44b2003-01-10 09:53:12 +1100927 /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
Damien Millerb8c656e2000-06-28 15:22:41 +1000928 saved_argc = ac;
Darren Tucker17c5d032004-06-25 14:22:23 +1000929 rexec_argc = ac;
Darren Tuckerd8093e42006-05-04 16:24:34 +1000930 saved_argv = xcalloc(ac + 1, sizeof(*saved_argv));
Damien Millera8ed44b2003-01-10 09:53:12 +1100931 for (i = 0; i < ac; i++)
932 saved_argv[i] = xstrdup(av[i]);
Damien Miller04cb5362003-05-15 21:29:10 +1000933 saved_argv[i] = NULL;
Damien Millera8ed44b2003-01-10 09:53:12 +1100934
935#ifndef HAVE_SETPROCTITLE
936 /* Prepare for later setproctitle emulation */
937 compat_init_setproctitle(ac, av);
Damien Millerf2e3e9d2003-06-02 12:15:54 +1000938 av = saved_argv;
Damien Millera8ed44b2003-01-10 09:53:12 +1100939#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000940
Damien Millerbfba3542004-03-22 09:29:57 +1100941 if (geteuid() == 0 && setgroups(0, NULL) == -1)
942 debug("setgroups(): %.200s", strerror(errno));
943
Darren Tuckerce321d82005-10-03 18:11:24 +1000944 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
945 sanitise_stdfd();
946
Damien Miller95def091999-11-25 00:26:21 +1100947 /* Initialize configuration options to their default values. */
948 initialize_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949
Damien Miller95def091999-11-25 00:26:21 +1100950 /* Parse command-line arguments. */
Darren Tucker645ab752004-06-25 13:33:20 +1000951 while ((opt = getopt(ac, av, "f:p:b:k:h:g:u:o:dDeiqrtQR46")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100952 switch (opt) {
Damien Miller34132e52000-01-14 15:45:46 +1100953 case '4':
Darren Tucker0f383232005-01-20 10:57:56 +1100954 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100955 break;
956 case '6':
Darren Tucker0f383232005-01-20 10:57:56 +1100957 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100958 break;
Damien Miller95def091999-11-25 00:26:21 +1100959 case 'f':
960 config_file_name = optarg;
961 break;
962 case 'd':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000963 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100964 debug_flag = 1;
965 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000966 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
Damien Millere4340be2000-09-16 13:29:08 +1100967 options.log_level++;
Damien Miller95def091999-11-25 00:26:21 +1100968 break;
Ben Lindstromc72745a2000-12-02 19:03:54 +0000969 case 'D':
970 no_daemon_flag = 1;
971 break;
Ben Lindstrom9fce9f02001-04-11 23:10:09 +0000972 case 'e':
973 log_stderr = 1;
974 break;
Damien Miller95def091999-11-25 00:26:21 +1100975 case 'i':
976 inetd_flag = 1;
977 break;
Darren Tucker645ab752004-06-25 13:33:20 +1000978 case 'r':
979 rexec_flag = 0;
980 break;
981 case 'R':
982 rexeced_flag = 1;
983 inetd_flag = 1;
984 break;
Damien Miller95def091999-11-25 00:26:21 +1100985 case 'Q':
Ben Lindstromd5390202001-01-29 08:07:43 +0000986 /* ignored */
Damien Miller95def091999-11-25 00:26:21 +1100987 break;
988 case 'q':
989 options.log_level = SYSLOG_LEVEL_QUIET;
990 break;
991 case 'b':
Damien Millerf0b15df2006-03-26 13:59:20 +1100992 options.server_key_bits = (int)strtonum(optarg, 256,
993 32768, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100994 break;
995 case 'p':
Damien Miller34132e52000-01-14 15:45:46 +1100996 options.ports_from_cmdline = 1;
Damien Millere4340be2000-09-16 13:29:08 +1100997 if (options.num_ports >= MAX_PORTS) {
998 fprintf(stderr, "too many ports.\n");
999 exit(1);
1000 }
Ben Lindstrom19066a12001-04-12 23:39:26 +00001001 options.ports[options.num_ports++] = a2port(optarg);
1002 if (options.ports[options.num_ports-1] == 0) {
1003 fprintf(stderr, "Bad port number.\n");
1004 exit(1);
1005 }
Damien Miller95def091999-11-25 00:26:21 +11001006 break;
1007 case 'g':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +00001008 if ((options.login_grace_time = convtime(optarg)) == -1) {
1009 fprintf(stderr, "Invalid login grace time.\n");
1010 exit(1);
1011 }
Damien Miller95def091999-11-25 00:26:21 +11001012 break;
1013 case 'k':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +00001014 if ((options.key_regeneration_time = convtime(optarg)) == -1) {
1015 fprintf(stderr, "Invalid key regeneration interval.\n");
1016 exit(1);
1017 }
Damien Miller95def091999-11-25 00:26:21 +11001018 break;
1019 case 'h':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001020 if (options.num_host_key_files >= MAX_HOSTKEYS) {
1021 fprintf(stderr, "too many host keys.\n");
1022 exit(1);
1023 }
1024 options.host_key_files[options.num_host_key_files++] = optarg;
Damien Miller95def091999-11-25 00:26:21 +11001025 break;
Ben Lindstrom794325a2001-08-06 21:09:07 +00001026 case 't':
1027 test_flag = 1;
1028 break;
Damien Miller942da032000-08-18 13:59:06 +10001029 case 'u':
Damien Millerf0b15df2006-03-26 13:59:20 +11001030 utmp_len = (u_int)strtonum(optarg, 0, MAXHOSTNAMELEN+1, NULL);
Ben Lindstrom41daec72002-07-23 21:15:13 +00001031 if (utmp_len > MAXHOSTNAMELEN) {
1032 fprintf(stderr, "Invalid utmp length.\n");
1033 exit(1);
1034 }
Damien Miller942da032000-08-18 13:59:06 +10001035 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001036 case 'o':
Damien Millerb9997192003-12-17 16:29:22 +11001037 line = xstrdup(optarg);
1038 if (process_server_config_line(&options, line,
Ben Lindstromade03f62001-12-06 18:22:17 +00001039 "command-line", 0) != 0)
Damien Miller9f0f5c62001-12-21 14:45:46 +11001040 exit(1);
Damien Millerb9997192003-12-17 16:29:22 +11001041 xfree(line);
Ben Lindstromade03f62001-12-06 18:22:17 +00001042 break;
Damien Miller95def091999-11-25 00:26:21 +11001043 case '?':
1044 default:
Ben Lindstromade03f62001-12-06 18:22:17 +00001045 usage();
1046 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001048 }
Darren Tucker645ab752004-06-25 13:33:20 +10001049 if (rexeced_flag || inetd_flag)
1050 rexec_flag = 0;
1051 if (rexec_flag && (av[0] == NULL || *av[0] != '/'))
1052 fatal("sshd re-exec requires execution with an absolute path");
1053 if (rexeced_flag)
Damien Miller035a5b42004-06-26 08:16:31 +10001054 closefrom(REEXEC_MIN_FREE_FD);
1055 else
1056 closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001057
Ben Lindstrom425fb022001-03-29 00:31:20 +00001058 SSLeay_add_all_algorithms();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001059
Damien Miller34132e52000-01-14 15:45:46 +11001060 /*
1061 * Force logging to stderr until we have loaded the private host
1062 * key (unless started from inetd)
1063 */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001064 log_init(__progname,
Damien Miller5aa5d782002-02-08 22:01:54 +11001065 options.log_level == SYSLOG_LEVEL_NOT_SET ?
1066 SYSLOG_LEVEL_INFO : options.log_level,
1067 options.log_facility == SYSLOG_FACILITY_NOT_SET ?
1068 SYSLOG_FACILITY_AUTH : options.log_facility,
Ben Lindstromc2faa4a2002-11-09 15:50:03 +00001069 log_stderr || !inetd_flag);
Damien Miller34132e52000-01-14 15:45:46 +11001070
Darren Tucker86c093d2004-03-08 22:59:03 +11001071 /*
1072 * Unset KRB5CCNAME, otherwise the user's session may inherit it from
1073 * root's environment
Damien Miller94cf4c82005-07-17 17:04:47 +10001074 */
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001075 if (getenv("KRB5CCNAME") != NULL)
1076 unsetenv("KRB5CCNAME");
1077
Tim Rice81ed5182002-09-25 17:38:46 -07001078#ifdef _UNICOS
Darren Tucker12984962004-05-24 13:37:13 +10001079 /* Cray can define user privs drop all privs now!
Ben Lindstrom6db66ff2001-08-06 23:29:16 +00001080 * Not needed on PRIV_SU systems!
1081 */
1082 drop_cray_privs();
1083#endif
1084
Darren Tucker645ab752004-06-25 13:33:20 +10001085 sensitive_data.server_key = NULL;
1086 sensitive_data.ssh1_host_key = NULL;
1087 sensitive_data.have_ssh1_key = 0;
1088 sensitive_data.have_ssh2_key = 0;
1089
1090 /* Fetch our configuration */
1091 buffer_init(&cfg);
1092 if (rexeced_flag)
Damien Miller035a5b42004-06-26 08:16:31 +10001093 recv_rexec_state(REEXEC_CONFIG_PASS_FD, &cfg);
Darren Tucker645ab752004-06-25 13:33:20 +10001094 else
1095 load_server_config(config_file_name, &cfg);
1096
1097 parse_server_config(&options,
1098 rexeced_flag ? "rexec" : config_file_name, &cfg);
1099
1100 if (!rexec_flag)
1101 buffer_free(&cfg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001102
Darren Tuckerc6f82192005-09-27 22:46:32 +10001103 seed_rng();
1104
Damien Miller95def091999-11-25 00:26:21 +11001105 /* Fill in default values for those options not explicitly set. */
1106 fill_default_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107
Darren Tucker0f383232005-01-20 10:57:56 +11001108 /* set default channel AF */
1109 channel_set_af(options.address_family);
1110
Damien Miller95def091999-11-25 00:26:21 +11001111 /* Check that there are no remaining arguments. */
1112 if (optind < ac) {
1113 fprintf(stderr, "Extra argument %s.\n", av[optind]);
1114 exit(1);
1115 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001116
Damien Miller2aa6d3c2004-09-12 16:53:04 +10001117 debug("sshd version %.100s", SSH_RELEASE);
Damien Miller2ccf6611999-11-15 15:25:10 +11001118
Damien Miller0bc1bd82000-11-13 22:57:25 +11001119 /* load private host keys */
Damien Miller07d86be2006-03-26 14:19:21 +11001120 sensitive_data.host_keys = xcalloc(options.num_host_key_files,
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001121 sizeof(Key *));
Damien Miller9f0f5c62001-12-21 14:45:46 +11001122 for (i = 0; i < options.num_host_key_files; i++)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001123 sensitive_data.host_keys[i] = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001124
Damien Miller9f0f5c62001-12-21 14:45:46 +11001125 for (i = 0; i < options.num_host_key_files; i++) {
Ben Lindstromd0fca422001-03-26 13:44:06 +00001126 key = key_load_private(options.host_key_files[i], "", NULL);
1127 sensitive_data.host_keys[i] = key;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001128 if (key == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001129 error("Could not load host key: %s",
1130 options.host_key_files[i]);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001131 sensitive_data.host_keys[i] = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001132 continue;
1133 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001134 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001135 case KEY_RSA1:
1136 sensitive_data.ssh1_host_key = key;
1137 sensitive_data.have_ssh1_key = 1;
1138 break;
1139 case KEY_RSA:
1140 case KEY_DSA:
1141 sensitive_data.have_ssh2_key = 1;
1142 break;
1143 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001144 debug("private host key: #%d type %d %s", i, key->type,
1145 key_type(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001146 }
1147 if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
Damien Miller996acd22003-04-09 20:59:48 +10001148 logit("Disabling protocol version 1. Could not load host key");
Damien Millereba71ba2000-04-29 23:57:08 +10001149 options.protocol &= ~SSH_PROTO_1;
1150 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001151 if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
Damien Miller996acd22003-04-09 20:59:48 +10001152 logit("Disabling protocol version 2. Could not load host key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001153 options.protocol &= ~SSH_PROTO_2;
Damien Millereba71ba2000-04-29 23:57:08 +10001154 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001155 if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
Damien Miller996acd22003-04-09 20:59:48 +10001156 logit("sshd: no hostkeys available -- exiting.");
Damien Miller95def091999-11-25 00:26:21 +11001157 exit(1);
1158 }
Damien Miller95def091999-11-25 00:26:21 +11001159
Damien Millereba71ba2000-04-29 23:57:08 +10001160 /* Check certain values for sanity. */
1161 if (options.protocol & SSH_PROTO_1) {
1162 if (options.server_key_bits < 512 ||
1163 options.server_key_bits > 32768) {
1164 fprintf(stderr, "Bad server key size.\n");
1165 exit(1);
1166 }
1167 /*
1168 * Check that server and host key lengths differ sufficiently. This
1169 * is necessary to make double encryption work with rsaref. Oh, I
1170 * hate software patents. I dont know if this can go? Niels
1171 */
1172 if (options.server_key_bits >
Ben Lindstrom822b6342002-06-23 21:38:49 +00001173 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) -
1174 SSH_KEY_BITS_RESERVED && options.server_key_bits <
1175 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1176 SSH_KEY_BITS_RESERVED) {
Damien Millereba71ba2000-04-29 23:57:08 +10001177 options.server_key_bits =
Ben Lindstrom822b6342002-06-23 21:38:49 +00001178 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1179 SSH_KEY_BITS_RESERVED;
Damien Millereba71ba2000-04-29 23:57:08 +10001180 debug("Forcing server key to %d bits to make it differ from host key.",
1181 options.server_key_bits);
1182 }
1183 }
1184
Ben Lindstroma26ea632002-06-06 20:46:25 +00001185 if (use_privsep) {
Ben Lindstroma26ea632002-06-06 20:46:25 +00001186 struct stat st;
1187
Damien Millerf0b15df2006-03-26 13:59:20 +11001188 if (getpwnam(SSH_PRIVSEP_USER) == NULL)
Ben Lindstroma26ea632002-06-06 20:46:25 +00001189 fatal("Privilege separation user %s does not exist",
1190 SSH_PRIVSEP_USER);
1191 if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
1192 (S_ISDIR(st.st_mode) == 0))
1193 fatal("Missing privilege separation directory: %s",
1194 _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstrom59627352002-06-27 18:02:21 +00001195
1196#ifdef HAVE_CYGWIN
1197 if (check_ntsec(_PATH_PRIVSEP_CHROOT_DIR) &&
1198 (st.st_uid != getuid () ||
1199 (st.st_mode & (S_IWGRP|S_IWOTH)) != 0))
1200#else
Ben Lindstrom2dfacb32002-06-23 00:33:47 +00001201 if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
Ben Lindstrom59627352002-06-27 18:02:21 +00001202#endif
Damien Miller180fc5b2003-02-24 11:50:18 +11001203 fatal("%s must be owned by root and not group or "
1204 "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstroma26ea632002-06-06 20:46:25 +00001205 }
1206
Ben Lindstrom794325a2001-08-06 21:09:07 +00001207 /* Configuration looks good, so exit if in test mode. */
1208 if (test_flag)
1209 exit(0);
1210
Damien Miller87aea252002-05-10 12:20:24 +10001211 /*
1212 * Clear out any supplemental groups we may have inherited. This
1213 * prevents inadvertent creation of files with bad modes (in the
Damien Millera8e06ce2003-11-21 23:48:55 +11001214 * portable version at least, it's certainly possible for PAM
1215 * to create a file, and we can't control the code in every
Damien Miller87aea252002-05-10 12:20:24 +10001216 * module which might be used).
1217 */
1218 if (setgroups(0, NULL) < 0)
1219 debug("setgroups() failed: %.200s", strerror(errno));
1220
Darren Tucker645ab752004-06-25 13:33:20 +10001221 if (rexec_flag) {
Damien Miller07d86be2006-03-26 14:19:21 +11001222 rexec_argv = xcalloc(rexec_argc + 2, sizeof(char *));
Darren Tucker645ab752004-06-25 13:33:20 +10001223 for (i = 0; i < rexec_argc; i++) {
1224 debug("rexec_argv[%d]='%s'", i, saved_argv[i]);
1225 rexec_argv[i] = saved_argv[i];
1226 }
1227 rexec_argv[rexec_argc] = "-R";
1228 rexec_argv[rexec_argc + 1] = NULL;
1229 }
1230
Damien Millereba71ba2000-04-29 23:57:08 +10001231 /* Initialize the log (it is reinitialized below in case we forked). */
Darren Tuckerea7c8122005-01-20 11:03:08 +11001232 if (debug_flag && (!inetd_flag || rexeced_flag))
Damien Miller95def091999-11-25 00:26:21 +11001233 log_stderr = 1;
Kevin Stevesec84dc12000-12-13 17:45:15 +00001234 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001235
Damien Millereba71ba2000-04-29 23:57:08 +10001236 /*
1237 * If not in debugging mode, and not started from inetd, disconnect
1238 * from the controlling terminal, and fork. The original process
1239 * exits.
1240 */
Ben Lindstromc72745a2000-12-02 19:03:54 +00001241 if (!(debug_flag || inetd_flag || no_daemon_flag)) {
Damien Miller95def091999-11-25 00:26:21 +11001242#ifdef TIOCNOTTY
1243 int fd;
1244#endif /* TIOCNOTTY */
1245 if (daemon(0, 0) < 0)
1246 fatal("daemon() failed: %.200s", strerror(errno));
1247
1248 /* Disconnect from the controlling tty. */
1249#ifdef TIOCNOTTY
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001250 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
Damien Miller95def091999-11-25 00:26:21 +11001251 if (fd >= 0) {
1252 (void) ioctl(fd, TIOCNOTTY, NULL);
1253 close(fd);
1254 }
1255#endif /* TIOCNOTTY */
1256 }
1257 /* Reinitialize the log (because of the fork above). */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001258 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001259
Damien Miller95def091999-11-25 00:26:21 +11001260 /* Initialize the random number generator. */
1261 arc4random_stir();
1262
1263 /* Chdir to the root directory so that the current disk can be
1264 unmounted if desired. */
1265 chdir("/");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001266
Ben Lindstromde71cda2001-03-24 00:43:26 +00001267 /* ignore SIGPIPE */
1268 signal(SIGPIPE, SIG_IGN);
Damien Miller95def091999-11-25 00:26:21 +11001269
Damien Miller95def091999-11-25 00:26:21 +11001270 /* Start listening for a socket, unless started from inetd. */
1271 if (inetd_flag) {
Darren Tucker645ab752004-06-25 13:33:20 +10001272 int fd;
1273
Damien Miller994cf142000-07-21 10:19:44 +10001274 startup_pipe = -1;
Darren Tucker645ab752004-06-25 13:33:20 +10001275 if (rexeced_flag) {
Damien Miller035a5b42004-06-26 08:16:31 +10001276 close(REEXEC_CONFIG_PASS_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001277 sock_in = sock_out = dup(STDIN_FILENO);
1278 if (!debug_flag) {
Damien Miller035a5b42004-06-26 08:16:31 +10001279 startup_pipe = dup(REEXEC_STARTUP_PIPE_FD);
1280 close(REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001281 }
1282 } else {
1283 sock_in = dup(STDIN_FILENO);
1284 sock_out = dup(STDOUT_FILENO);
1285 }
Damien Millereba71ba2000-04-29 23:57:08 +10001286 /*
1287 * We intentionally do not close the descriptors 0, 1, and 2
Darren Tucker645ab752004-06-25 13:33:20 +10001288 * as our code for setting the descriptors won't work if
Damien Millereba71ba2000-04-29 23:57:08 +10001289 * ttyfd happens to be one of those.
1290 */
Darren Tucker645ab752004-06-25 13:33:20 +10001291 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1292 dup2(fd, STDIN_FILENO);
1293 dup2(fd, STDOUT_FILENO);
1294 if (fd > STDOUT_FILENO)
1295 close(fd);
1296 }
Damien Miller95def091999-11-25 00:26:21 +11001297 debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
Darren Tucker645ab752004-06-25 13:33:20 +10001298 if ((options.protocol & SSH_PROTO_1) &&
1299 sensitive_data.server_key == NULL)
Ben Lindstromca42d5f2001-03-09 18:25:32 +00001300 generate_ephemeral_server_key();
Damien Miller95def091999-11-25 00:26:21 +11001301 } else {
Damien Miller34132e52000-01-14 15:45:46 +11001302 for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
1303 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1304 continue;
1305 if (num_listen_socks >= MAX_LISTEN_SOCKS)
1306 fatal("Too many listen sockets. "
1307 "Enlarge MAX_LISTEN_SOCKS");
Darren Tucker96d47102005-02-09 09:53:48 +11001308 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
Damien Miller34132e52000-01-14 15:45:46 +11001309 ntop, sizeof(ntop), strport, sizeof(strport),
Darren Tucker96d47102005-02-09 09:53:48 +11001310 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1311 error("getnameinfo failed: %.100s",
1312 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
1313 strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11001314 continue;
1315 }
1316 /* Create socket for listening. */
Damien Miller2372ace2003-05-14 13:42:23 +10001317 listen_sock = socket(ai->ai_family, ai->ai_socktype,
1318 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11001319 if (listen_sock < 0) {
1320 /* kernel may not support ipv6 */
1321 verbose("socket: %.100s", strerror(errno));
1322 continue;
1323 }
Damien Miller232711f2004-06-15 10:35:30 +10001324 if (set_nonblock(listen_sock) == -1) {
Darren Tuckerefa37062004-02-24 09:20:29 +11001325 close(listen_sock);
1326 continue;
1327 }
Damien Miller34132e52000-01-14 15:45:46 +11001328 /*
Damien Millere1383ce2002-09-19 11:49:37 +10001329 * Set socket options.
1330 * Allow local port reuse in TIME_WAIT.
Damien Miller34132e52000-01-14 15:45:46 +11001331 */
Damien Millere1383ce2002-09-19 11:49:37 +10001332 if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
1333 &on, sizeof(on)) == -1)
1334 error("setsockopt SO_REUSEADDR: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001335
Damien Miller34132e52000-01-14 15:45:46 +11001336 debug("Bind to port %s on %s.", strport, ntop);
Damien Miller95def091999-11-25 00:26:21 +11001337
Damien Miller34132e52000-01-14 15:45:46 +11001338 /* Bind the socket to the desired port. */
Damien Miller0a4e27d2001-02-18 12:36:39 +11001339 if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1340 if (!ai->ai_next)
1341 error("Bind to port %s on %s failed: %.200s.",
1342 strport, ntop, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11001343 close(listen_sock);
1344 continue;
1345 }
1346 listen_socks[num_listen_socks] = listen_sock;
1347 num_listen_socks++;
Damien Miller95def091999-11-25 00:26:21 +11001348
Damien Miller34132e52000-01-14 15:45:46 +11001349 /* Start listening on the port. */
Darren Tucker3175eb92003-12-09 19:15:11 +11001350 if (listen(listen_sock, SSH_LISTEN_BACKLOG) < 0)
Damien Millerb24c2f82006-03-15 12:04:36 +11001351 fatal("listen on [%s]:%s: %.100s",
1352 ntop, strport, strerror(errno));
1353 logit("Server listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001354 }
Damien Miller34132e52000-01-14 15:45:46 +11001355 freeaddrinfo(options.listen_addrs);
1356
1357 if (!num_listen_socks)
1358 fatal("Cannot bind any address.");
1359
Ben Lindstrom98097862001-06-25 05:10:20 +00001360 if (options.protocol & SSH_PROTO_1)
1361 generate_ephemeral_server_key();
1362
1363 /*
1364 * Arrange to restart on SIGHUP. The handler needs
1365 * listen_sock.
1366 */
1367 signal(SIGHUP, sighup_handler);
1368
1369 signal(SIGTERM, sigterm_handler);
1370 signal(SIGQUIT, sigterm_handler);
1371
1372 /* Arrange SIGCHLD to be caught. */
1373 signal(SIGCHLD, main_sigchld_handler);
1374
1375 /* Write out the pid file after the sigterm handler is setup */
Damien Miller95def091999-11-25 00:26:21 +11001376 if (!debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +11001377 /*
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001378 * Record our pid in /var/run/sshd.pid to make it
1379 * easier to kill the correct sshd. We don't want to
1380 * do this before the bind above because the bind will
Damien Miller5428f641999-11-25 11:54:57 +11001381 * fail if there already is a daemon, and this will
1382 * overwrite any old pid in the file.
1383 */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001384 f = fopen(options.pid_file, "wb");
Darren Tuckere5327042003-07-03 13:40:44 +10001385 if (f == NULL) {
1386 error("Couldn't create pid file \"%s\": %s",
1387 options.pid_file, strerror(errno));
1388 } else {
Ben Lindstromce0f6342002-06-11 16:42:49 +00001389 fprintf(f, "%ld\n", (long) getpid());
Damien Miller95def091999-11-25 00:26:21 +11001390 fclose(f);
1391 }
1392 }
Damien Miller95def091999-11-25 00:26:21 +11001393
Damien Miller34132e52000-01-14 15:45:46 +11001394 /* setup fd set for listen */
Damien Miller37023962000-07-11 17:31:38 +10001395 fdset = NULL;
Damien Miller34132e52000-01-14 15:45:46 +11001396 maxfd = 0;
1397 for (i = 0; i < num_listen_socks; i++)
1398 if (listen_socks[i] > maxfd)
1399 maxfd = listen_socks[i];
Damien Miller37023962000-07-11 17:31:38 +10001400 /* pipes connected to unauthenticated childs */
Damien Miller07d86be2006-03-26 14:19:21 +11001401 startup_pipes = xcalloc(options.max_startups, sizeof(int));
Damien Miller37023962000-07-11 17:31:38 +10001402 for (i = 0; i < options.max_startups; i++)
1403 startup_pipes[i] = -1;
Damien Miller34132e52000-01-14 15:45:46 +11001404
Damien Miller5428f641999-11-25 11:54:57 +11001405 /*
1406 * Stay listening for connections until the system crashes or
1407 * the daemon is killed with a signal.
1408 */
Damien Miller95def091999-11-25 00:26:21 +11001409 for (;;) {
1410 if (received_sighup)
1411 sighup_restart();
Damien Miller37023962000-07-11 17:31:38 +10001412 if (fdset != NULL)
1413 xfree(fdset);
Damien Miller07d86be2006-03-26 14:19:21 +11001414 fdset = (fd_set *)xcalloc(howmany(maxfd + 1, NFDBITS),
1415 sizeof(fd_mask));
Damien Miller37023962000-07-11 17:31:38 +10001416
Damien Miller34132e52000-01-14 15:45:46 +11001417 for (i = 0; i < num_listen_socks; i++)
1418 FD_SET(listen_socks[i], fdset);
Damien Miller37023962000-07-11 17:31:38 +10001419 for (i = 0; i < options.max_startups; i++)
1420 if (startup_pipes[i] != -1)
1421 FD_SET(startup_pipes[i], fdset);
1422
1423 /* Wait in select until there is a connection. */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001424 ret = select(maxfd+1, fdset, NULL, NULL, NULL);
1425 if (ret < 0 && errno != EINTR)
1426 error("select: %.100s", strerror(errno));
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001427 if (received_sigterm) {
Damien Miller996acd22003-04-09 20:59:48 +10001428 logit("Received signal %d; terminating.",
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001429 (int) received_sigterm);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001430 close_listen_socks();
1431 unlink(options.pid_file);
1432 exit(255);
1433 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001434 if (key_used && key_do_regen) {
Ben Lindstromca42d5f2001-03-09 18:25:32 +00001435 generate_ephemeral_server_key();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001436 key_used = 0;
1437 key_do_regen = 0;
Damien Miller34132e52000-01-14 15:45:46 +11001438 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001439 if (ret < 0)
1440 continue;
1441
Damien Miller37023962000-07-11 17:31:38 +10001442 for (i = 0; i < options.max_startups; i++)
1443 if (startup_pipes[i] != -1 &&
1444 FD_ISSET(startup_pipes[i], fdset)) {
1445 /*
1446 * the read end of the pipe is ready
1447 * if the child has closed the pipe
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001448 * after successful authentication
Damien Miller37023962000-07-11 17:31:38 +10001449 * or if the child has died
1450 */
1451 close(startup_pipes[i]);
1452 startup_pipes[i] = -1;
1453 startups--;
1454 }
Damien Miller34132e52000-01-14 15:45:46 +11001455 for (i = 0; i < num_listen_socks; i++) {
1456 if (!FD_ISSET(listen_socks[i], fdset))
Damien Miller95def091999-11-25 00:26:21 +11001457 continue;
Damien Miller37023962000-07-11 17:31:38 +10001458 fromlen = sizeof(from);
Damien Millerf0b15df2006-03-26 13:59:20 +11001459 newsock = accept(listen_socks[i],
1460 (struct sockaddr *)&from, &fromlen);
Damien Miller37023962000-07-11 17:31:38 +10001461 if (newsock < 0) {
1462 if (errno != EINTR && errno != EWOULDBLOCK)
1463 error("accept: %.100s", strerror(errno));
1464 continue;
1465 }
Damien Miller232711f2004-06-15 10:35:30 +10001466 if (unset_nonblock(newsock) == -1) {
Darren Tuckerefa37062004-02-24 09:20:29 +11001467 close(newsock);
1468 continue;
1469 }
Damien Miller942da032000-08-18 13:59:06 +10001470 if (drop_connection(startups) == 1) {
1471 debug("drop connection #%d", startups);
Damien Miller37023962000-07-11 17:31:38 +10001472 close(newsock);
1473 continue;
1474 }
1475 if (pipe(startup_p) == -1) {
1476 close(newsock);
1477 continue;
1478 }
1479
Darren Tucker645ab752004-06-25 13:33:20 +10001480 if (rexec_flag && socketpair(AF_UNIX,
1481 SOCK_STREAM, 0, config_s) == -1) {
1482 error("reexec socketpair: %s",
1483 strerror(errno));
1484 close(newsock);
1485 close(startup_p[0]);
1486 close(startup_p[1]);
1487 continue;
1488 }
1489
Damien Miller37023962000-07-11 17:31:38 +10001490 for (j = 0; j < options.max_startups; j++)
1491 if (startup_pipes[j] == -1) {
1492 startup_pipes[j] = startup_p[0];
1493 if (maxfd < startup_p[0])
1494 maxfd = startup_p[0];
1495 startups++;
1496 break;
1497 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001498
Damien Miller5428f641999-11-25 11:54:57 +11001499 /*
Damien Miller37023962000-07-11 17:31:38 +10001500 * Got connection. Fork a child to handle it, unless
1501 * we are in debugging mode.
Damien Miller5428f641999-11-25 11:54:57 +11001502 */
Damien Miller37023962000-07-11 17:31:38 +10001503 if (debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +11001504 /*
Damien Miller37023962000-07-11 17:31:38 +10001505 * In debugging mode. Close the listening
1506 * socket, and start processing the
1507 * connection without forking.
Damien Miller5428f641999-11-25 11:54:57 +11001508 */
Damien Miller37023962000-07-11 17:31:38 +10001509 debug("Server will not fork when running in debugging mode.");
Damien Miller34132e52000-01-14 15:45:46 +11001510 close_listen_socks();
Damien Miller95def091999-11-25 00:26:21 +11001511 sock_in = newsock;
1512 sock_out = newsock;
Darren Tucker645ab752004-06-25 13:33:20 +10001513 close(startup_p[0]);
1514 close(startup_p[1]);
Damien Miller4d97ba22000-07-11 18:15:50 +10001515 startup_pipe = -1;
Damien Miller182ee6e2000-07-12 09:45:27 +10001516 pid = getpid();
Darren Tucker645ab752004-06-25 13:33:20 +10001517 if (rexec_flag) {
1518 send_rexec_state(config_s[0],
1519 &cfg);
1520 close(config_s[0]);
1521 }
Damien Miller95def091999-11-25 00:26:21 +11001522 break;
Damien Miller37023962000-07-11 17:31:38 +10001523 } else {
1524 /*
1525 * Normal production daemon. Fork, and have
1526 * the child process the connection. The
1527 * parent continues listening.
1528 */
1529 if ((pid = fork()) == 0) {
1530 /*
Damien Millerf0b15df2006-03-26 13:59:20 +11001531 * Child. Close the listening and
1532 * max_startup sockets. Start using
1533 * the accepted socket. Reinitialize
1534 * logging (since our pid has changed).
1535 * We break out of the loop to handle
Damien Miller37023962000-07-11 17:31:38 +10001536 * the connection.
1537 */
1538 startup_pipe = startup_p[1];
Ben Lindstromd84df982001-12-06 16:35:40 +00001539 close_startup_pipes();
Damien Miller37023962000-07-11 17:31:38 +10001540 close_listen_socks();
1541 sock_in = newsock;
1542 sock_out = newsock;
Damien Millerf0b15df2006-03-26 13:59:20 +11001543 log_init(__progname,
1544 options.log_level,
1545 options.log_facility,
1546 log_stderr);
Darren Tucker0f56ed12004-08-29 16:38:41 +10001547 if (rexec_flag)
1548 close(config_s[0]);
Damien Miller37023962000-07-11 17:31:38 +10001549 break;
1550 }
Damien Miller95def091999-11-25 00:26:21 +11001551 }
Damien Miller37023962000-07-11 17:31:38 +10001552
1553 /* Parent. Stay in the loop. */
1554 if (pid < 0)
1555 error("fork: %.100s", strerror(errno));
1556 else
Ben Lindstromce0f6342002-06-11 16:42:49 +00001557 debug("Forked child %ld.", (long)pid);
Damien Miller37023962000-07-11 17:31:38 +10001558
1559 close(startup_p[1]);
1560
Darren Tucker645ab752004-06-25 13:33:20 +10001561 if (rexec_flag) {
1562 send_rexec_state(config_s[0], &cfg);
1563 close(config_s[0]);
1564 close(config_s[1]);
1565 }
1566
Damien Millerf0b15df2006-03-26 13:59:20 +11001567 /*
1568 * Mark that the key has been used (it
1569 * was "given" to the child).
1570 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001571 if ((options.protocol & SSH_PROTO_1) &&
1572 key_used == 0) {
1573 /* Schedule server key regeneration alarm. */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00001574 signal(SIGALRM, key_regeneration_alarm);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001575 alarm(options.key_regeneration_time);
1576 key_used = 1;
1577 }
Damien Miller37023962000-07-11 17:31:38 +10001578
1579 arc4random_stir();
Damien Miller37023962000-07-11 17:31:38 +10001580 close(newsock);
Damien Miller95def091999-11-25 00:26:21 +11001581 }
Damien Miller34132e52000-01-14 15:45:46 +11001582 /* child process check (or debug mode) */
1583 if (num_listen_socks < 0)
1584 break;
Damien Miller95def091999-11-25 00:26:21 +11001585 }
1586 }
1587
1588 /* This is the child processing a new connection. */
Damien Miller57aae982004-03-08 23:11:25 +11001589 setproctitle("%s", "[accepted]");
Damien Miller95def091999-11-25 00:26:21 +11001590
Darren Tucker6832b832004-08-12 22:36:51 +10001591 /*
1592 * Create a new session and process group since the 4.4BSD
1593 * setlogin() affects the entire process group. We don't
1594 * want the child to be able to affect the parent.
1595 */
1596#if !defined(SSHD_ACQUIRES_CTTY)
1597 /*
1598 * If setsid is called, on some platforms sshd will later acquire a
1599 * controlling terminal which will result in "could not set
1600 * controlling tty" errors.
1601 */
1602 if (!debug_flag && !inetd_flag && setsid() < 0)
1603 error("setsid: %.100s", strerror(errno));
1604#endif
1605
Darren Tucker645ab752004-06-25 13:33:20 +10001606 if (rexec_flag) {
1607 int fd;
1608
Damien Miller035a5b42004-06-26 08:16:31 +10001609 debug("rexec start in %d out %d newsock %d pipe %d sock %d",
1610 sock_in, sock_out, newsock, startup_pipe, config_s[0]);
Darren Tucker645ab752004-06-25 13:33:20 +10001611 dup2(newsock, STDIN_FILENO);
1612 dup2(STDIN_FILENO, STDOUT_FILENO);
1613 if (startup_pipe == -1)
Damien Miller035a5b42004-06-26 08:16:31 +10001614 close(REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001615 else
Damien Miller035a5b42004-06-26 08:16:31 +10001616 dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001617
Damien Miller035a5b42004-06-26 08:16:31 +10001618 dup2(config_s[1], REEXEC_CONFIG_PASS_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001619 close(config_s[1]);
Darren Tuckerd8835932004-08-12 22:42:29 +10001620 if (startup_pipe != -1)
1621 close(startup_pipe);
Damien Miller035a5b42004-06-26 08:16:31 +10001622
Darren Tucker645ab752004-06-25 13:33:20 +10001623 execv(rexec_argv[0], rexec_argv);
1624
1625 /* Reexec has failed, fall back and continue */
1626 error("rexec of %s failed: %s", rexec_argv[0], strerror(errno));
Damien Miller035a5b42004-06-26 08:16:31 +10001627 recv_rexec_state(REEXEC_CONFIG_PASS_FD, NULL);
Darren Tucker645ab752004-06-25 13:33:20 +10001628 log_init(__progname, options.log_level,
1629 options.log_facility, log_stderr);
1630
1631 /* Clean up fds */
Damien Miller035a5b42004-06-26 08:16:31 +10001632 startup_pipe = REEXEC_STARTUP_PIPE_FD;
Darren Tucker645ab752004-06-25 13:33:20 +10001633 close(config_s[1]);
Damien Miller035a5b42004-06-26 08:16:31 +10001634 close(REEXEC_CONFIG_PASS_FD);
1635 newsock = sock_out = sock_in = dup(STDIN_FILENO);
Darren Tucker645ab752004-06-25 13:33:20 +10001636 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1637 dup2(fd, STDIN_FILENO);
1638 dup2(fd, STDOUT_FILENO);
1639 if (fd > STDERR_FILENO)
1640 close(fd);
1641 }
Damien Miller035a5b42004-06-26 08:16:31 +10001642 debug("rexec cleanup in %d out %d newsock %d pipe %d sock %d",
1643 sock_in, sock_out, newsock, startup_pipe, config_s[0]);
Darren Tucker645ab752004-06-25 13:33:20 +10001644 }
1645
Damien Miller5428f641999-11-25 11:54:57 +11001646 /*
1647 * Disable the key regeneration alarm. We will not regenerate the
1648 * key since we are no longer in a position to give it to anyone. We
1649 * will not restart on SIGHUP since it no longer makes sense.
1650 */
Damien Miller95def091999-11-25 00:26:21 +11001651 alarm(0);
1652 signal(SIGALRM, SIG_DFL);
1653 signal(SIGHUP, SIG_DFL);
1654 signal(SIGTERM, SIG_DFL);
1655 signal(SIGQUIT, SIG_DFL);
1656 signal(SIGCHLD, SIG_DFL);
Damien Miller4e0f5e12000-08-29 11:05:50 +11001657 signal(SIGINT, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001658
Damien Miller5428f641999-11-25 11:54:57 +11001659 /*
1660 * Register our connection. This turns encryption off because we do
1661 * not have a key.
1662 */
Damien Miller95def091999-11-25 00:26:21 +11001663 packet_set_connection(sock_in, sock_out);
Damien Miller9786e6e2005-07-26 21:54:56 +10001664 packet_set_server();
Damien Miller95def091999-11-25 00:26:21 +11001665
Damien Miller4f1d6b22005-05-26 11:59:32 +10001666 /* Set SO_KEEPALIVE if requested. */
1667 if (options.tcp_keep_alive && packet_connection_is_on_socket() &&
1668 setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0)
1669 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1670
Damien Miller677257f2005-06-17 12:55:03 +10001671 if ((remote_port = get_remote_port()) < 0) {
1672 debug("get_remote_port failed");
1673 cleanup_exit(255);
1674 }
Damien Miller4d3fd542005-11-05 15:13:24 +11001675
1676 /*
1677 * We use get_canonical_hostname with usedns = 0 instead of
1678 * get_remote_ipaddr here so IP options will be checked.
1679 */
Damien Millereb13e552006-06-13 13:03:53 +10001680 (void) get_canonical_hostname(0);
1681 /*
1682 * The rest of the code depends on the fact that
1683 * get_remote_ipaddr() caches the remote ip, even if
1684 * the socket goes away.
1685 */
1686 remote_ip = get_remote_ipaddr();
Damien Miller95def091999-11-25 00:26:21 +11001687
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001688#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001689 audit_connection_from(remote_ip, remote_port);
1690#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001691#ifdef LIBWRAP
Damien Miller6a4a4b92001-11-12 11:07:11 +11001692 /* Check whether logins are denied from this host. */
Darren Tucker586b0b92004-06-25 13:34:31 +10001693 if (packet_connection_is_on_socket()) {
Damien Miller95def091999-11-25 00:26:21 +11001694 struct request_info req;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001695
Ben Lindstromce89dac2001-09-12 16:58:04 +00001696 request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, 0);
Damien Miller95def091999-11-25 00:26:21 +11001697 fromhost(&req);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001698
Damien Miller95def091999-11-25 00:26:21 +11001699 if (!hosts_access(&req)) {
Damien Miller6a4a4b92001-11-12 11:07:11 +11001700 debug("Connection refused by tcp wrapper");
Ben Lindstrom7de696e2001-03-29 00:45:12 +00001701 refuse(&req);
Damien Miller6a4a4b92001-11-12 11:07:11 +11001702 /* NOTREACHED */
1703 fatal("libwrap refuse returns");
Damien Miller95def091999-11-25 00:26:21 +11001704 }
Damien Miller95def091999-11-25 00:26:21 +11001705 }
Damien Miller34132e52000-01-14 15:45:46 +11001706#endif /* LIBWRAP */
Damien Miller6a4a4b92001-11-12 11:07:11 +11001707
Damien Miller95def091999-11-25 00:26:21 +11001708 /* Log the connection. */
1709 verbose("Connection from %.500s port %d", remote_ip, remote_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001710
Damien Miller5428f641999-11-25 11:54:57 +11001711 /*
Damien Miller788f2122005-11-05 15:14:59 +11001712 * We don't want to listen forever unless the other side
Damien Miller5428f641999-11-25 11:54:57 +11001713 * successfully authenticates itself. So we set up an alarm which is
1714 * cleared after successful authentication. A limit of zero
Damien Miller788f2122005-11-05 15:14:59 +11001715 * indicates no limit. Note that we don't set the alarm in debugging
Damien Miller5428f641999-11-25 11:54:57 +11001716 * mode; it is just annoying to have the server exit just when you
1717 * are about to discover the bug.
1718 */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00001719 signal(SIGALRM, grace_alarm_handler);
Damien Miller95def091999-11-25 00:26:21 +11001720 if (!debug_flag)
1721 alarm(options.login_grace_time);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001722
Damien Millerb38eff82000-04-01 11:09:21 +10001723 sshd_exchange_identification(sock_in, sock_out);
Darren Tuckerec960f22003-08-13 20:37:05 +10001724
Damien Miller95def091999-11-25 00:26:21 +11001725 packet_set_nonblocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001726
Darren Tucker3e33cec2003-10-02 16:12:36 +10001727 /* allocate authentication context */
Damien Miller07d86be2006-03-26 14:19:21 +11001728 authctxt = xcalloc(1, sizeof(*authctxt));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001729
Darren Tuckerf3bb4342005-03-31 21:39:25 +10001730 authctxt->loginmsg = &loginmsg;
1731
Darren Tucker3e33cec2003-10-02 16:12:36 +10001732 /* XXX global for cleanup, access from other modules */
1733 the_authctxt = authctxt;
1734
Darren Tucker5c14c732005-01-24 21:55:49 +11001735 /* prepare buffer to collect messages to display to user after login */
1736 buffer_init(&loginmsg);
1737
Ben Lindstrom943481c2002-03-22 03:43:46 +00001738 if (use_privsep)
Darren Tucker3e33cec2003-10-02 16:12:36 +10001739 if (privsep_preauth(authctxt) == 1)
Ben Lindstrom943481c2002-03-22 03:43:46 +00001740 goto authenticated;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001741
Damien Miller396691a2000-01-20 22:44:08 +11001742 /* perform the key exchange */
Damien Miller396691a2000-01-20 22:44:08 +11001743 /* authenticate user and start session */
Damien Millerefb4afe2000-04-12 18:45:05 +10001744 if (compat20) {
1745 do_ssh2_kex();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001746 do_authentication2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001747 } else {
1748 do_ssh1_kex();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001749 do_authentication(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001750 }
Ben Lindstrom943481c2002-03-22 03:43:46 +00001751 /*
1752 * If we use privilege separation, the unprivileged child transfers
1753 * the current keystate and exits
1754 */
1755 if (use_privsep) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001756 mm_send_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001757 exit(0);
Ben Lindstrom943481c2002-03-22 03:43:46 +00001758 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001759
1760 authenticated:
Damien Miller7bff1a92005-12-24 14:59:12 +11001761 /*
1762 * Cancel the alarm we set to limit the time taken for
1763 * authentication.
1764 */
1765 alarm(0);
1766 signal(SIGALRM, SIG_DFL);
1767 if (startup_pipe != -1) {
1768 close(startup_pipe);
1769 startup_pipe = -1;
1770 }
1771
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001772#ifdef SSH_AUDIT_EVENTS
1773 audit_event(SSH_AUTH_SUCCESS);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001774#endif
1775
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001776 /*
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001777 * In privilege separation, we fork another child and prepare
1778 * file descriptor passing.
1779 */
1780 if (use_privsep) {
Ben Lindstrom943481c2002-03-22 03:43:46 +00001781 privsep_postauth(authctxt);
1782 /* the monitor process [priv] will not return */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001783 if (!compat20)
1784 destroy_sensitive_data();
1785 }
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00001786
Darren Tucker3e33cec2003-10-02 16:12:36 +10001787 /* Start session. */
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00001788 do_authenticated(authctxt);
1789
Damien Miller3a5b0232002-03-13 13:19:42 +11001790 /* The connection has been terminated. */
1791 verbose("Closing connection to %.100s", remote_ip);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001792
Damien Millerbeb4ba51999-12-28 15:09:35 +11001793#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +10001794 if (options.use_pam)
1795 finish_pam();
Damien Millerbeb4ba51999-12-28 15:09:35 +11001796#endif /* USE_PAM */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001797
Darren Tucker2b59a6d2005-03-06 22:38:51 +11001798#ifdef SSH_AUDIT_EVENTS
1799 PRIVSEP(audit_event(SSH_CONNECTION_CLOSE));
1800#endif
1801
Damien Miller95def091999-11-25 00:26:21 +11001802 packet_close();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001803
1804 if (use_privsep)
1805 mm_terminate();
1806
Damien Miller95def091999-11-25 00:26:21 +11001807 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001808}
1809
Damien Miller95def091999-11-25 00:26:21 +11001810/*
Ben Lindstromabcb1452002-03-22 01:10:21 +00001811 * Decrypt session_key_int using our private server key and private host key
1812 * (key with larger modulus first).
1813 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001814int
Ben Lindstromabcb1452002-03-22 01:10:21 +00001815ssh1_session_key(BIGNUM *session_key_int)
1816{
1817 int rsafail = 0;
1818
Damien Millerf0b15df2006-03-26 13:59:20 +11001819 if (BN_cmp(sensitive_data.server_key->rsa->n,
1820 sensitive_data.ssh1_host_key->rsa->n) > 0) {
Ben Lindstromabcb1452002-03-22 01:10:21 +00001821 /* Server key has bigger modulus. */
1822 if (BN_num_bits(sensitive_data.server_key->rsa->n) <
Damien Millerf0b15df2006-03-26 13:59:20 +11001823 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1824 SSH_KEY_BITS_RESERVED) {
1825 fatal("do_connection: %s: "
1826 "server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
Ben Lindstromabcb1452002-03-22 01:10:21 +00001827 get_remote_ipaddr(),
1828 BN_num_bits(sensitive_data.server_key->rsa->n),
1829 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1830 SSH_KEY_BITS_RESERVED);
1831 }
1832 if (rsa_private_decrypt(session_key_int, session_key_int,
1833 sensitive_data.server_key->rsa) <= 0)
1834 rsafail++;
1835 if (rsa_private_decrypt(session_key_int, session_key_int,
1836 sensitive_data.ssh1_host_key->rsa) <= 0)
1837 rsafail++;
1838 } else {
1839 /* Host key has bigger modulus (or they are equal). */
1840 if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
Damien Millerf0b15df2006-03-26 13:59:20 +11001841 BN_num_bits(sensitive_data.server_key->rsa->n) +
1842 SSH_KEY_BITS_RESERVED) {
1843 fatal("do_connection: %s: "
1844 "host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
Ben Lindstromabcb1452002-03-22 01:10:21 +00001845 get_remote_ipaddr(),
1846 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1847 BN_num_bits(sensitive_data.server_key->rsa->n),
1848 SSH_KEY_BITS_RESERVED);
1849 }
1850 if (rsa_private_decrypt(session_key_int, session_key_int,
1851 sensitive_data.ssh1_host_key->rsa) < 0)
1852 rsafail++;
1853 if (rsa_private_decrypt(session_key_int, session_key_int,
1854 sensitive_data.server_key->rsa) < 0)
1855 rsafail++;
1856 }
1857 return (rsafail);
1858}
1859/*
Damien Miller396691a2000-01-20 22:44:08 +11001860 * SSH1 key exchange
Damien Miller95def091999-11-25 00:26:21 +11001861 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001862static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001863do_ssh1_kex(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001864{
Damien Miller95def091999-11-25 00:26:21 +11001865 int i, len;
Damien Miller7650bc62001-01-30 09:27:26 +11001866 int rsafail = 0;
Damien Miller95def091999-11-25 00:26:21 +11001867 BIGNUM *session_key_int;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001868 u_char session_key[SSH_SESSION_KEY_LENGTH];
1869 u_char cookie[8];
1870 u_int cipher_type, auth_mask, protocol_flags;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001871 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001872
Damien Miller5428f641999-11-25 11:54:57 +11001873 /*
1874 * Generate check bytes that the client must send back in the user
1875 * packet in order for it to be accepted; this is used to defy ip
1876 * spoofing attacks. Note that this only works against somebody
1877 * doing IP spoofing from a remote machine; any machine on the local
1878 * network can still see outgoing packets and catch the random
1879 * cookie. This only affects rhosts authentication, and this is one
1880 * of the reasons why it is inherently insecure.
1881 */
Damien Miller95def091999-11-25 00:26:21 +11001882 for (i = 0; i < 8; i++) {
1883 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001884 rnd = arc4random();
1885 cookie[i] = rnd & 0xff;
1886 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +11001887 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001888
Damien Miller5428f641999-11-25 11:54:57 +11001889 /*
1890 * Send our public key. We include in the packet 64 bits of random
1891 * data that must be matched in the reply in order to prevent IP
1892 * spoofing.
1893 */
Damien Miller95def091999-11-25 00:26:21 +11001894 packet_start(SSH_SMSG_PUBLIC_KEY);
1895 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001896 packet_put_char(cookie[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001897
Damien Miller95def091999-11-25 00:26:21 +11001898 /* Store our public server RSA key. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001899 packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
1900 packet_put_bignum(sensitive_data.server_key->rsa->e);
1901 packet_put_bignum(sensitive_data.server_key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001902
Damien Miller95def091999-11-25 00:26:21 +11001903 /* Store our public host RSA key. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001904 packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1905 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
1906 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001907
Damien Miller95def091999-11-25 00:26:21 +11001908 /* Put protocol flags. */
1909 packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001910
Damien Miller95def091999-11-25 00:26:21 +11001911 /* Declare which ciphers we support. */
Damien Miller874d77b2000-10-14 16:23:11 +11001912 packet_put_int(cipher_mask_ssh1(0));
Damien Miller95def091999-11-25 00:26:21 +11001913
1914 /* Declare supported authentication types. */
1915 auth_mask = 0;
Damien Miller95def091999-11-25 00:26:21 +11001916 if (options.rhosts_rsa_authentication)
1917 auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
1918 if (options.rsa_authentication)
1919 auth_mask |= 1 << SSH_AUTH_RSA;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001920 if (options.challenge_response_authentication == 1)
Damien Miller95def091999-11-25 00:26:21 +11001921 auth_mask |= 1 << SSH_AUTH_TIS;
Damien Miller95def091999-11-25 00:26:21 +11001922 if (options.password_authentication)
1923 auth_mask |= 1 << SSH_AUTH_PASSWORD;
1924 packet_put_int(auth_mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001925
Damien Miller95def091999-11-25 00:26:21 +11001926 /* Send the packet and wait for it to be sent. */
1927 packet_send();
1928 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001929
Damien Miller0bc1bd82000-11-13 22:57:25 +11001930 debug("Sent %d bit server key and %d bit host key.",
1931 BN_num_bits(sensitive_data.server_key->rsa->n),
1932 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001933
Damien Miller95def091999-11-25 00:26:21 +11001934 /* Read clients reply (cipher type and session key). */
Damien Millerdff50992002-01-22 23:16:32 +11001935 packet_read_expect(SSH_CMSG_SESSION_KEY);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001936
Damien Miller50945fa1999-12-09 10:31:37 +11001937 /* Get cipher type and check whether we accept this. */
Damien Miller95def091999-11-25 00:26:21 +11001938 cipher_type = packet_get_char();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001939
Damien Miller874d77b2000-10-14 16:23:11 +11001940 if (!(cipher_mask_ssh1(0) & (1 << cipher_type)))
Damien Miller50945fa1999-12-09 10:31:37 +11001941 packet_disconnect("Warning: client selects unsupported cipher.");
1942
Damien Miller95def091999-11-25 00:26:21 +11001943 /* Get check bytes from the packet. These must match those we
1944 sent earlier with the public key packet. */
1945 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001946 if (cookie[i] != packet_get_char())
Damien Miller95def091999-11-25 00:26:21 +11001947 packet_disconnect("IP Spoofing check bytes do not match.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001948
Damien Miller95def091999-11-25 00:26:21 +11001949 debug("Encryption type: %.200s", cipher_name(cipher_type));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001950
Damien Miller95def091999-11-25 00:26:21 +11001951 /* Get the encrypted integer. */
Damien Millerda755162002-01-22 23:09:22 +11001952 if ((session_key_int = BN_new()) == NULL)
1953 fatal("do_ssh1_kex: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +11001954 packet_get_bignum(session_key_int);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001955
Damien Miller95def091999-11-25 00:26:21 +11001956 protocol_flags = packet_get_int();
1957 packet_set_protocol_flags(protocol_flags);
Damien Miller48b03fc2002-01-22 23:11:40 +11001958 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001959
Ben Lindstromabcb1452002-03-22 01:10:21 +00001960 /* Decrypt session_key_int using host/server keys */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001961 rsafail = PRIVSEP(ssh1_session_key(session_key_int));
1962
Damien Miller5428f641999-11-25 11:54:57 +11001963 /*
1964 * Extract session key from the decrypted integer. The key is in the
1965 * least significant 256 bits of the integer; the first byte of the
1966 * key is in the highest bits.
1967 */
Damien Miller7650bc62001-01-30 09:27:26 +11001968 if (!rsafail) {
1969 BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1970 len = BN_num_bytes(session_key_int);
Damien Millereccb9de2005-06-17 12:59:34 +10001971 if (len < 0 || (u_int)len > sizeof(session_key)) {
Damien Miller7650bc62001-01-30 09:27:26 +11001972 error("do_connection: bad session key len from %s: "
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001973 "session_key_int %d > sizeof(session_key) %lu",
1974 get_remote_ipaddr(), len, (u_long)sizeof(session_key));
Damien Miller7650bc62001-01-30 09:27:26 +11001975 rsafail++;
1976 } else {
1977 memset(session_key, 0, sizeof(session_key));
1978 BN_bn2bin(session_key_int,
1979 session_key + sizeof(session_key) - len);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001980
Darren Tuckere14e0052004-05-13 16:30:44 +10001981 derive_ssh1_session_id(
Darren Tuckerfc959702004-07-17 16:12:08 +10001982 sensitive_data.ssh1_host_key->rsa->n,
Darren Tuckere14e0052004-05-13 16:30:44 +10001983 sensitive_data.server_key->rsa->n,
1984 cookie, session_id);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001985 /*
1986 * Xor the first 16 bytes of the session key with the
1987 * session id.
1988 */
1989 for (i = 0; i < 16; i++)
1990 session_key[i] ^= session_id[i];
Damien Miller7650bc62001-01-30 09:27:26 +11001991 }
1992 }
1993 if (rsafail) {
Ben Lindstromeb648a72001-03-05 06:00:29 +00001994 int bytes = BN_num_bytes(session_key_int);
Ben Lindstrom13c5d3b2002-02-26 18:00:48 +00001995 u_char *buf = xmalloc(bytes);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001996 MD5_CTX md;
1997
Damien Miller996acd22003-04-09 20:59:48 +10001998 logit("do_connection: generating a fake encryption key");
Ben Lindstromeb648a72001-03-05 06:00:29 +00001999 BN_bn2bin(session_key_int, buf);
2000 MD5_Init(&md);
2001 MD5_Update(&md, buf, bytes);
2002 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
2003 MD5_Final(session_key, &md);
2004 MD5_Init(&md);
2005 MD5_Update(&md, session_key, 16);
2006 MD5_Update(&md, buf, bytes);
2007 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
2008 MD5_Final(session_key + 16, &md);
2009 memset(buf, 0, bytes);
2010 xfree(buf);
Ben Lindstrom941ac822001-03-05 06:25:23 +00002011 for (i = 0; i < 16; i++)
2012 session_id[i] = session_key[i] ^ session_key[i + 16];
Damien Miller7650bc62001-01-30 09:27:26 +11002013 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002014 /* Destroy the private and public keys. No longer. */
Damien Miller3a5b0232002-03-13 13:19:42 +11002015 destroy_sensitive_data();
Ben Lindstromeb648a72001-03-05 06:00:29 +00002016
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002017 if (use_privsep)
2018 mm_ssh1_session_id(session_id);
2019
Damien Miller396691a2000-01-20 22:44:08 +11002020 /* Destroy the decrypted integer. It is no longer needed. */
2021 BN_clear_free(session_key_int);
2022
Damien Miller95def091999-11-25 00:26:21 +11002023 /* Set the session key. From this on all communications will be encrypted. */
2024 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002025
Damien Miller95def091999-11-25 00:26:21 +11002026 /* Destroy our copy of the session key. It is no longer needed. */
2027 memset(session_key, 0, sizeof(session_key));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002028
Damien Miller95def091999-11-25 00:26:21 +11002029 debug("Received session key; encryption turned on.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002030
Ben Lindstromf666fec2002-06-06 19:51:58 +00002031 /* Send an acknowledgment packet. Note that this packet is sent encrypted. */
Damien Miller95def091999-11-25 00:26:21 +11002032 packet_start(SSH_SMSG_SUCCESS);
2033 packet_send();
2034 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002035}
Damien Millerefb4afe2000-04-12 18:45:05 +10002036
2037/*
2038 * SSH2 key exchange: diffie-hellman-group1-sha1
2039 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002040static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00002041do_ssh2_kex(void)
Damien Millerefb4afe2000-04-12 18:45:05 +10002042{
Damien Millerefb4afe2000-04-12 18:45:05 +10002043 Kex *kex;
Damien Millerefb4afe2000-04-12 18:45:05 +10002044
Damien Miller78928792000-04-12 20:17:38 +10002045 if (options.ciphers != NULL) {
Damien Miller4af51302000-04-16 11:18:38 +10002046 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Damien Miller78928792000-04-12 20:17:38 +10002047 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
2048 }
Damien Millera0ff4662001-03-30 10:49:35 +10002049 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
2050 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
2051 myproposal[PROPOSAL_ENC_ALGS_STOC] =
2052 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
2053
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00002054 if (options.macs != NULL) {
2055 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
2056 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
2057 }
Damien Miller9786e6e2005-07-26 21:54:56 +10002058 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +00002059 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
2060 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
Damien Miller9786e6e2005-07-26 21:54:56 +10002061 } else if (options.compression == COMP_DELAYED) {
2062 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
2063 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com";
Ben Lindstrom23e0f662002-06-21 01:09:47 +00002064 }
Damien Millerf0b15df2006-03-26 13:59:20 +11002065
Damien Miller0bc1bd82000-11-13 22:57:25 +11002066 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = list_hostkey_types();
2067
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002068 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +00002069 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +11002070 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10002071 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Miller8e7fb332003-02-24 12:03:03 +11002072 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11002073 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00002074 kex->server = 1;
2075 kex->client_version_string=client_version_string;
2076 kex->server_version_string=server_version_string;
2077 kex->load_host_key=&get_hostkey_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002078 kex->host_key_index=&get_hostkey_index;
Damien Millerefb4afe2000-04-12 18:45:05 +10002079
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002080 xxx_kex = kex;
2081
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002082 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +11002083
Ben Lindstrom2d90e002001-04-04 02:00:54 +00002084 session_id2 = kex->session_id;
2085 session_id2_len = kex->session_id_len;
2086
Damien Miller874d77b2000-10-14 16:23:11 +11002087#ifdef DEBUG_KEXDH
2088 /* send 1st encrypted/maced/compressed message */
2089 packet_start(SSH2_MSG_IGNORE);
2090 packet_put_cstring("markus");
2091 packet_send();
2092 packet_write_wait();
2093#endif
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00002094 debug("KEX done");
Damien Millerefb4afe2000-04-12 18:45:05 +10002095}
Darren Tucker3e33cec2003-10-02 16:12:36 +10002096
2097/* server specific fatal cleanup */
2098void
2099cleanup_exit(int i)
2100{
2101 if (the_authctxt)
2102 do_cleanup(the_authctxt);
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11002103#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11002104 /* done after do_cleanup so it can cancel the PAM auth 'thread' */
2105 if (!use_privsep || mm_is_monitor())
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11002106 audit_event(SSH_CONNECTION_ABANDON);
Darren Tucker269a1ea2005-02-03 00:20:53 +11002107#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002108 _exit(i);
2109}