blob: 5f38781190926e832e859bca53ec070e7e51b152 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 * This program is the ssh daemon. It listens for connections from clients,
6 * and performs authentication, executes use commands or shell, and forwards
Damien Miller95def091999-11-25 00:26:21 +11007 * information to/from the application to the user client over an encrypted
Damien Millere4340be2000-09-16 13:29:08 +11008 * connection. This can also handle forwarding of X11, TCP/IP, and
9 * authentication agent connections.
Damien Millerefb4afe2000-04-12 18:45:05 +100010 *
Damien Millere4340be2000-09-16 13:29:08 +110011 * As far as I am concerned, the code I have written for this software
12 * can be used freely for any purpose. Any derived versions of this
13 * software must be clearly marked as such, and if the derived work is
14 * incompatible with the protocol description in the RFC file, it must be
15 * called by a name other than "ssh" or "Secure Shell".
16 *
17 * SSH2 implementation:
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000018 * Privilege Separation:
Damien Millere4340be2000-09-16 13:29:08 +110019 *
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000020 * Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved.
21 * Copyright (c) 2002 Niels Provos. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110022 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
33 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
36 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110042 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
44#include "includes.h"
Damien Millerf675fc42004-06-15 10:30:09 +100045RCSID("$OpenBSD: sshd.c,v 1.292 2004/06/13 12:53:24 djm Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include <openssl/dh.h>
48#include <openssl/bn.h>
Damien Miller6a47f302002-02-13 13:55:06 +110049#include <openssl/md5.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000050#include <openssl/rand.h>
Kevin Steves0ea1d9d2002-04-25 18:17:04 +000051#ifdef HAVE_SECUREWARE
52#include <sys/security.h>
53#include <prot.h>
54#endif
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055
56#include "ssh.h"
57#include "ssh1.h"
58#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059#include "xmalloc.h"
60#include "rsa.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000061#include "sshpty.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064#include "servconf.h"
65#include "uidswap.h"
66#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100067#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000068#include "cipher.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100069#include "kex.h"
Damien Millerb38eff82000-04-01 11:09:21 +100070#include "key.h"
Damien Miller874d77b2000-10-14 16:23:11 +110071#include "dh.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100072#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100073#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000074#include "pathnames.h"
75#include "atomicio.h"
76#include "canohost.h"
77#include "auth.h"
78#include "misc.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000079#include "dispatch.h"
Ben Lindstrom1bae4042001-10-03 17:46:39 +000080#include "channels.h"
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +000081#include "session.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000082#include "monitor_mm.h"
83#include "monitor.h"
84#include "monitor_wrap.h"
85#include "monitor_fdpass.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
87#ifdef LIBWRAP
88#include <tcpd.h>
89#include <syslog.h>
90int allow_severity = LOG_INFO;
91int deny_severity = LOG_WARNING;
92#endif /* LIBWRAP */
93
94#ifndef O_NOCTTY
95#define O_NOCTTY 0
96#endif
97
Ben Lindstrom49a79c02000-11-17 03:47:20 +000098#ifdef HAVE___PROGNAME
99extern char *__progname;
100#else
101char *__progname;
102#endif
103
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104/* Server configuration options. */
105ServerOptions options;
106
107/* Name of the server configuration file. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000108char *config_file_name = _PATH_SERVER_CONFIG_FILE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Damien Miller4af51302000-04-16 11:18:38 +1000110/*
Damien Miller34132e52000-01-14 15:45:46 +1100111 * Flag indicating whether IPv4 or IPv6. This can be set on the command line.
112 * Default value is AF_UNSPEC means both IPv4 and IPv6.
113 */
114int IPv4or6 = AF_UNSPEC;
115
Damien Miller95def091999-11-25 00:26:21 +1100116/*
117 * Debug mode flag. This can be set on the command line. If debug
118 * mode is enabled, extra debugging output will be sent to the system
119 * log, the daemon will not go to background, and will exit after processing
120 * the first connection.
121 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122int debug_flag = 0;
123
Ben Lindstrom794325a2001-08-06 21:09:07 +0000124/* Flag indicating that the daemon should only test the configuration and keys. */
125int test_flag = 0;
126
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127/* Flag indicating that the daemon is being started from inetd. */
128int inetd_flag = 0;
129
Ben Lindstromc72745a2000-12-02 19:03:54 +0000130/* Flag indicating that sshd should not detach and become a daemon. */
131int no_daemon_flag = 0;
132
Damien Miller5ce662a1999-11-11 17:57:39 +1100133/* debug goes to stderr unless inetd_flag is set */
134int log_stderr = 0;
135
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136/* Saved arguments to main(). */
137char **saved_argv;
Damien Millerb8c656e2000-06-28 15:22:41 +1000138int saved_argc;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Miller5428f641999-11-25 11:54:57 +1100140/*
Damien Miller34132e52000-01-14 15:45:46 +1100141 * The sockets that the server is listening; this is used in the SIGHUP
142 * signal handler.
Damien Miller5428f641999-11-25 11:54:57 +1100143 */
Damien Miller34132e52000-01-14 15:45:46 +1100144#define MAX_LISTEN_SOCKS 16
145int listen_socks[MAX_LISTEN_SOCKS];
146int num_listen_socks = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147
Damien Miller5428f641999-11-25 11:54:57 +1100148/*
149 * the client's version string, passed by sshd2 in compat mode. if != NULL,
150 * sshd will skip the version-number exchange
151 */
Damien Miller95def091999-11-25 00:26:21 +1100152char *client_version_string = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000153char *server_version_string = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000155/* for rekeying XXX fixme */
156Kex *xxx_kex;
157
Damien Miller5428f641999-11-25 11:54:57 +1100158/*
159 * Any really sensitive data in the application is contained in this
160 * structure. The idea is that this structure could be locked into memory so
161 * that the pages do not get written into swap. However, there are some
162 * problems. The private key contains BIGNUMs, and we do not (in principle)
163 * have access to the internals of them, and locking just the structure is
164 * not very useful. Currently, memory locking is not implemented.
165 */
Damien Miller95def091999-11-25 00:26:21 +1100166struct {
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000167 Key *server_key; /* ephemeral server key */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100168 Key *ssh1_host_key; /* ssh1 host key */
169 Key **host_keys; /* all private host keys */
170 int have_ssh1_key;
171 int have_ssh2_key;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000172 u_char ssh1_cookie[SSH_SESSION_KEY_LENGTH];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173} sensitive_data;
174
Damien Miller5428f641999-11-25 11:54:57 +1100175/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000176 * Flag indicating whether the RSA server key needs to be regenerated.
177 * Is set in the SIGALRM handler and cleared when the key is regenerated.
Damien Miller5428f641999-11-25 11:54:57 +1100178 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000179static volatile sig_atomic_t key_do_regen = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000181/* This is set to true when a signal is received. */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000182static volatile sig_atomic_t received_sighup = 0;
183static volatile sig_atomic_t received_sigterm = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184
Damien Millerb38eff82000-04-01 11:09:21 +1000185/* session identifier, used by RSA-auth */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000186u_char session_id[16];
Damien Millerb38eff82000-04-01 11:09:21 +1000187
Damien Millereba71ba2000-04-29 23:57:08 +1000188/* same for ssh2 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000189u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000190u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000191
Damien Miller942da032000-08-18 13:59:06 +1000192/* record remote hostname or ip */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000193u_int utmp_len = MAXHOSTNAMELEN;
Damien Miller942da032000-08-18 13:59:06 +1000194
Ben Lindstromd84df982001-12-06 16:35:40 +0000195/* options.max_startup sized array of fd ints */
196int *startup_pipes = NULL;
197int startup_pipe; /* in child */
198
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000199/* variables used for privilege separation */
Damien Miller8e7fb332003-02-24 12:03:03 +1100200int use_privsep;
Darren Tuckera8be9e22004-02-06 16:40:27 +1100201struct monitor *pmonitor = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000203/* message to be displayed after login */
204Buffer loginmsg;
205
Darren Tucker3e33cec2003-10-02 16:12:36 +1000206/* global authentication context */
207Authctxt *the_authctxt = NULL;
208
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209/* Prototypes for various functions defined later in this file. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000210void destroy_sensitive_data(void);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000211void demote_sensitive_data(void);
Damien Miller98c7ad62000-03-09 21:27:49 +1100212
Ben Lindstrombba81212001-06-25 05:01:22 +0000213static void do_ssh1_kex(void);
214static void do_ssh2_kex(void);
Damien Miller874d77b2000-10-14 16:23:11 +1100215
Damien Miller98c7ad62000-03-09 21:27:49 +1100216/*
Damien Miller34132e52000-01-14 15:45:46 +1100217 * Close all listening sockets
218 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000219static void
Damien Miller34132e52000-01-14 15:45:46 +1100220close_listen_socks(void)
221{
222 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000223
Damien Miller34132e52000-01-14 15:45:46 +1100224 for (i = 0; i < num_listen_socks; i++)
225 close(listen_socks[i]);
226 num_listen_socks = -1;
227}
228
Ben Lindstromd84df982001-12-06 16:35:40 +0000229static void
230close_startup_pipes(void)
231{
232 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000233
Ben Lindstromd84df982001-12-06 16:35:40 +0000234 if (startup_pipes)
235 for (i = 0; i < options.max_startups; i++)
236 if (startup_pipes[i] != -1)
237 close(startup_pipes[i]);
238}
239
Damien Miller34132e52000-01-14 15:45:46 +1100240/*
Damien Miller95def091999-11-25 00:26:21 +1100241 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
242 * the effect is to reread the configuration file (and to regenerate
243 * the server key).
244 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000245static void
Damien Miller95def091999-11-25 00:26:21 +1100246sighup_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247{
Ben Lindstrom07958482001-12-06 16:19:01 +0000248 int save_errno = errno;
249
Damien Miller95def091999-11-25 00:26:21 +1100250 received_sighup = 1;
251 signal(SIGHUP, sighup_handler);
Ben Lindstrom07958482001-12-06 16:19:01 +0000252 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253}
254
Damien Miller95def091999-11-25 00:26:21 +1100255/*
256 * Called from the main program after receiving SIGHUP.
257 * Restarts the server.
258 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000259static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000260sighup_restart(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261{
Damien Miller996acd22003-04-09 20:59:48 +1000262 logit("Received SIGHUP; restarting.");
Damien Miller34132e52000-01-14 15:45:46 +1100263 close_listen_socks();
Ben Lindstromd84df982001-12-06 16:35:40 +0000264 close_startup_pipes();
Damien Miller95def091999-11-25 00:26:21 +1100265 execv(saved_argv[0], saved_argv);
Damien Miller996acd22003-04-09 20:59:48 +1000266 logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
Ben Lindstrom822b6342002-06-23 21:38:49 +0000267 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100268 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269}
270
Damien Miller95def091999-11-25 00:26:21 +1100271/*
272 * Generic signal handler for terminating signals in the master daemon.
Damien Miller95def091999-11-25 00:26:21 +1100273 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000274static void
Damien Miller95def091999-11-25 00:26:21 +1100275sigterm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000277 received_sigterm = sig;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278}
279
Damien Miller95def091999-11-25 00:26:21 +1100280/*
281 * SIGCHLD handler. This is called whenever a child dies. This will then
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000282 * reap any zombies left by exited children.
Damien Miller95def091999-11-25 00:26:21 +1100283 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000284static void
Damien Miller95def091999-11-25 00:26:21 +1100285main_sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286{
Damien Miller95def091999-11-25 00:26:21 +1100287 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000288 pid_t pid;
Damien Miller95def091999-11-25 00:26:21 +1100289 int status;
Damien Miller431f66b1999-11-21 18:31:57 +1100290
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000291 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
292 (pid < 0 && errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100293 ;
Damien Miller431f66b1999-11-21 18:31:57 +1100294
Damien Miller95def091999-11-25 00:26:21 +1100295 signal(SIGCHLD, main_sigchld_handler);
296 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297}
298
Damien Miller95def091999-11-25 00:26:21 +1100299/*
300 * Signal handler for the alarm after the login grace period has expired.
301 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000302static void
Damien Miller95def091999-11-25 00:26:21 +1100303grace_alarm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000305 /* XXX no idea how fix this signal handler */
306
Darren Tuckera8be9e22004-02-06 16:40:27 +1100307 if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0)
308 kill(pmonitor->m_pid, SIGALRM);
309
Damien Miller95def091999-11-25 00:26:21 +1100310 /* Log error and exit. */
Damien Millerd27a76d2002-09-27 13:22:31 +1000311 fatal("Timeout before authentication for %s", get_remote_ipaddr());
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312}
313
Damien Miller95def091999-11-25 00:26:21 +1100314/*
Damien Miller95def091999-11-25 00:26:21 +1100315 * Signal handler for the key regeneration alarm. Note that this
316 * alarm only occurs in the daemon waiting for connections, and it does not
317 * do anything with the private key or random state before forking.
318 * Thus there should be no concurrency control/asynchronous execution
319 * problems.
320 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000321static void
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000322generate_ephemeral_server_key(void)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100323{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000324 u_int32_t rnd = 0;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000325 int i;
326
Ben Lindstroma3700052001-04-05 23:26:32 +0000327 verbose("Generating %s%d bit RSA key.",
Damien Millerff75ac42001-03-30 10:50:32 +1000328 sensitive_data.server_key ? "new " : "", options.server_key_bits);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100329 if (sensitive_data.server_key != NULL)
330 key_free(sensitive_data.server_key);
Ben Lindstroma3700052001-04-05 23:26:32 +0000331 sensitive_data.server_key = key_generate(KEY_RSA1,
Damien Millerff75ac42001-03-30 10:50:32 +1000332 options.server_key_bits);
333 verbose("RSA key generation complete.");
Ben Lindstromeb648a72001-03-05 06:00:29 +0000334
335 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
336 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000337 rnd = arc4random();
338 sensitive_data.ssh1_cookie[i] = rnd & 0xff;
339 rnd >>= 8;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000340 }
341 arc4random_stir();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100342}
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000343
Ben Lindstrombba81212001-06-25 05:01:22 +0000344static void
Damien Miller95def091999-11-25 00:26:21 +1100345key_regeneration_alarm(int sig)
346{
347 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000348
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000349 signal(SIGALRM, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +1100350 errno = save_errno;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000351 key_do_regen = 1;
Damien Miller95def091999-11-25 00:26:21 +1100352}
353
Ben Lindstrombba81212001-06-25 05:01:22 +0000354static void
Damien Millerb38eff82000-04-01 11:09:21 +1000355sshd_exchange_identification(int sock_in, int sock_out)
356{
Damien Miller78928792000-04-12 20:17:38 +1000357 int i, mismatch;
Damien Millerb38eff82000-04-01 11:09:21 +1000358 int remote_major, remote_minor;
Damien Miller78928792000-04-12 20:17:38 +1000359 int major, minor;
Damien Millerb38eff82000-04-01 11:09:21 +1000360 char *s;
361 char buf[256]; /* Must not be larger than remote_version. */
362 char remote_version[256]; /* Must be at least as big as buf. */
363
Damien Miller78928792000-04-12 20:17:38 +1000364 if ((options.protocol & SSH_PROTO_1) &&
365 (options.protocol & SSH_PROTO_2)) {
366 major = PROTOCOL_MAJOR_1;
367 minor = 99;
368 } else if (options.protocol & SSH_PROTO_2) {
369 major = PROTOCOL_MAJOR_2;
370 minor = PROTOCOL_MINOR_2;
371 } else {
372 major = PROTOCOL_MAJOR_1;
373 minor = PROTOCOL_MINOR_1;
374 }
375 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
Damien Millerb38eff82000-04-01 11:09:21 +1000376 server_version_string = xstrdup(buf);
377
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000378 /* Send our protocol version identification. */
379 if (atomicio(vwrite, sock_out, server_version_string,
380 strlen(server_version_string))
381 != strlen(server_version_string)) {
382 logit("Could not write ident string to %s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000383 cleanup_exit(255);
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000384 }
385
386 /* Read other sides version identification. */
387 memset(buf, 0, sizeof(buf));
388 for (i = 0; i < sizeof(buf) - 1; i++) {
389 if (atomicio(read, sock_in, &buf[i], 1) != 1) {
390 logit("Did not receive identification string from %s",
391 get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000392 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000393 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000394 if (buf[i] == '\r') {
395 buf[i] = 0;
396 /* Kludge for F-Secure Macintosh < 1.0.2 */
397 if (i == 12 &&
398 strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000399 break;
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000400 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000401 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000402 if (buf[i] == '\n') {
403 buf[i] = 0;
404 break;
405 }
Damien Millerb38eff82000-04-01 11:09:21 +1000406 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000407 buf[sizeof(buf) - 1] = 0;
408 client_version_string = xstrdup(buf);
Damien Millerb38eff82000-04-01 11:09:21 +1000409
410 /*
411 * Check that the versions match. In future this might accept
412 * several versions and set appropriate flags to handle them.
413 */
414 if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
415 &remote_major, &remote_minor, remote_version) != 3) {
Damien Miller4af51302000-04-16 11:18:38 +1000416 s = "Protocol mismatch.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000417 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Millerb38eff82000-04-01 11:09:21 +1000418 close(sock_in);
419 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000420 logit("Bad protocol version identification '%.100s' from %s",
Damien Millerb38eff82000-04-01 11:09:21 +1000421 client_version_string, get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000422 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000423 }
424 debug("Client protocol version %d.%d; client software version %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100425 remote_major, remote_minor, remote_version);
Damien Millerb38eff82000-04-01 11:09:21 +1000426
Damien Millerefb4afe2000-04-12 18:45:05 +1000427 compat_datafellows(remote_version);
428
Damien Millere9264972002-09-30 11:59:21 +1000429 if (datafellows & SSH_BUG_PROBE) {
Damien Miller996acd22003-04-09 20:59:48 +1000430 logit("probed from %s with %s. Don't panic.",
Damien Millere9264972002-09-30 11:59:21 +1000431 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000432 cleanup_exit(255);
Damien Millere9264972002-09-30 11:59:21 +1000433 }
434
Damien Miller27dbe6f2001-03-19 22:36:20 +1100435 if (datafellows & SSH_BUG_SCANNER) {
Damien Miller996acd22003-04-09 20:59:48 +1000436 logit("scanned from %s with %s. Don't panic.",
Damien Miller27dbe6f2001-03-19 22:36:20 +1100437 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000438 cleanup_exit(255);
Damien Miller27dbe6f2001-03-19 22:36:20 +1100439 }
440
Damien Miller78928792000-04-12 20:17:38 +1000441 mismatch = 0;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000442 switch (remote_major) {
Damien Millerb38eff82000-04-01 11:09:21 +1000443 case 1:
Damien Millereba71ba2000-04-29 23:57:08 +1000444 if (remote_minor == 99) {
445 if (options.protocol & SSH_PROTO_2)
446 enable_compat20();
447 else
448 mismatch = 1;
449 break;
450 }
Damien Miller78928792000-04-12 20:17:38 +1000451 if (!(options.protocol & SSH_PROTO_1)) {
452 mismatch = 1;
453 break;
454 }
Damien Millerb38eff82000-04-01 11:09:21 +1000455 if (remote_minor < 3) {
Damien Miller37023962000-07-11 17:31:38 +1000456 packet_disconnect("Your ssh version is too old and "
Damien Millerb38eff82000-04-01 11:09:21 +1000457 "is no longer supported. Please install a newer version.");
458 } else if (remote_minor == 3) {
459 /* note that this disables agent-forwarding */
460 enable_compat13();
461 }
Damien Miller78928792000-04-12 20:17:38 +1000462 break;
Damien Millerefb4afe2000-04-12 18:45:05 +1000463 case 2:
Damien Miller78928792000-04-12 20:17:38 +1000464 if (options.protocol & SSH_PROTO_2) {
Damien Millerefb4afe2000-04-12 18:45:05 +1000465 enable_compat20();
466 break;
467 }
468 /* FALLTHROUGH */
Damien Miller4af51302000-04-16 11:18:38 +1000469 default:
Damien Miller78928792000-04-12 20:17:38 +1000470 mismatch = 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000471 break;
472 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000473 chop(server_version_string);
Damien Miller78928792000-04-12 20:17:38 +1000474 debug("Local version string %.200s", server_version_string);
475
476 if (mismatch) {
477 s = "Protocol major versions differ.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000478 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Miller78928792000-04-12 20:17:38 +1000479 close(sock_in);
480 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000481 logit("Protocol major versions differ for %s: %.200s vs. %.200s",
Damien Miller78928792000-04-12 20:17:38 +1000482 get_remote_ipaddr(),
483 server_version_string, client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000484 cleanup_exit(255);
Damien Miller78928792000-04-12 20:17:38 +1000485 }
Damien Millereba71ba2000-04-29 23:57:08 +1000486}
487
Damien Miller0bc1bd82000-11-13 22:57:25 +1100488/* Destroy the host and server keys. They will no longer be needed. */
Damien Millereba71ba2000-04-29 23:57:08 +1000489void
490destroy_sensitive_data(void)
491{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100492 int i;
493
494 if (sensitive_data.server_key) {
495 key_free(sensitive_data.server_key);
496 sensitive_data.server_key = NULL;
497 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100498 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100499 if (sensitive_data.host_keys[i]) {
500 key_free(sensitive_data.host_keys[i]);
501 sensitive_data.host_keys[i] = NULL;
502 }
503 }
504 sensitive_data.ssh1_host_key = NULL;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000505 memset(sensitive_data.ssh1_cookie, 0, SSH_SESSION_KEY_LENGTH);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100506}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100507
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000508/* Demote private to public keys for network child */
509void
510demote_sensitive_data(void)
511{
512 Key *tmp;
513 int i;
514
515 if (sensitive_data.server_key) {
516 tmp = key_demote(sensitive_data.server_key);
517 key_free(sensitive_data.server_key);
518 sensitive_data.server_key = tmp;
519 }
520
521 for (i = 0; i < options.num_host_key_files; i++) {
522 if (sensitive_data.host_keys[i]) {
523 tmp = key_demote(sensitive_data.host_keys[i]);
524 key_free(sensitive_data.host_keys[i]);
525 sensitive_data.host_keys[i] = tmp;
526 if (tmp->type == KEY_RSA1)
527 sensitive_data.ssh1_host_key = tmp;
528 }
529 }
530
531 /* We do not clear ssh1_host key and cookie. XXX - Okay Niels? */
532}
533
Ben Lindstrom08105192002-03-22 02:50:06 +0000534static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000535privsep_preauth_child(void)
536{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000537 u_int32_t rnd[256];
Ben Lindstrom810af962002-07-04 00:11:40 +0000538 gid_t gidset[1];
Ben Lindstromc7431342002-03-22 03:11:49 +0000539 struct passwd *pw;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000540 int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000541
542 /* Enable challenge-response authentication for privilege separation */
543 privsep_challenge_enable();
544
545 for (i = 0; i < 256; i++)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000546 rnd[i] = arc4random();
547 RAND_seed(rnd, sizeof(rnd));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000548
549 /* Demote the private keys to public keys. */
550 demote_sensitive_data();
551
Ben Lindstromc7431342002-03-22 03:11:49 +0000552 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
Damien Miller0150c652002-04-24 09:49:09 +1000553 fatal("Privilege separation user %s does not exist",
554 SSH_PRIVSEP_USER);
Ben Lindstromc7431342002-03-22 03:11:49 +0000555 memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
556 endpwent();
557
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000558 /* Change our root directory */
Ben Lindstrom7a7edf72002-03-22 02:42:37 +0000559 if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
560 fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
561 strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000562 if (chdir("/") == -1)
Ben Lindstrom1ee9ec32002-03-22 03:14:45 +0000563 fatal("chdir(\"/\"): %s", strerror(errno));
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000564
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000565 /* Drop our privileges */
Ben Lindstromc7431342002-03-22 03:11:49 +0000566 debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
567 (u_int)pw->pw_gid);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000568#if 0
Darren Tuckerd5920482004-02-29 20:11:30 +1100569 /* XXX not ready, too heavy after chroot */
Ben Lindstromc7431342002-03-22 03:11:49 +0000570 do_setusercontext(pw);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000571#else
572 gidset[0] = pw->pw_gid;
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000573 if (setgroups(1, gidset) < 0)
574 fatal("setgroups: %.100s", strerror(errno));
575 permanently_set_uid(pw);
576#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577}
578
Darren Tucker3e33cec2003-10-02 16:12:36 +1000579static int
580privsep_preauth(Authctxt *authctxt)
Ben Lindstrom943481c2002-03-22 03:43:46 +0000581{
Ben Lindstrom943481c2002-03-22 03:43:46 +0000582 int status;
583 pid_t pid;
584
585 /* Set up unprivileged child process to deal with network data */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000586 pmonitor = monitor_init();
Ben Lindstrom943481c2002-03-22 03:43:46 +0000587 /* Store a pointer to the kex for later rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000588 pmonitor->m_pkex = &xxx_kex;
Ben Lindstrom943481c2002-03-22 03:43:46 +0000589
590 pid = fork();
591 if (pid == -1) {
592 fatal("fork of unprivileged child failed");
593 } else if (pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000594 debug2("Network child is on pid %ld", (long)pid);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000595
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000596 close(pmonitor->m_recvfd);
Darren Tuckera8be9e22004-02-06 16:40:27 +1100597 pmonitor->m_pid = pid;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000598 monitor_child_preauth(authctxt, pmonitor);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000599 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000600
601 /* Sync memory */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000602 monitor_sync(pmonitor);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000603
604 /* Wait for the child's exit status */
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000605 while (waitpid(pid, &status, 0) < 0)
606 if (errno != EINTR)
607 break;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000608 return (1);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000609 } else {
610 /* child */
611
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000612 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000613
614 /* Demote the child */
615 if (getuid() == 0 || geteuid() == 0)
616 privsep_preauth_child();
Ben Lindstromf90f58d2002-03-26 01:53:03 +0000617 setproctitle("%s", "[net]");
Ben Lindstrom943481c2002-03-22 03:43:46 +0000618 }
Darren Tucker3e33cec2003-10-02 16:12:36 +1000619 return (0);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000620}
621
Ben Lindstrom08105192002-03-22 02:50:06 +0000622static void
Ben Lindstrom943481c2002-03-22 03:43:46 +0000623privsep_postauth(Authctxt *authctxt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000624{
Tim Rice9dd30812002-07-07 13:43:36 -0700625#ifdef DISABLE_FD_PASSING
Tim Rice8eff3192002-06-25 15:35:15 -0700626 if (1) {
627#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000628 if (authctxt->pw->pw_uid == 0 || options.use_login) {
Tim Rice8eff3192002-06-25 15:35:15 -0700629#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000630 /* File descriptor passing is broken or root login */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000631 monitor_apply_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000632 use_privsep = 0;
633 return;
634 }
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000635
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000636 /* Authentication complete */
637 alarm(0);
638 if (startup_pipe != -1) {
639 close(startup_pipe);
640 startup_pipe = -1;
641 }
642
643 /* New socket pair */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000644 monitor_reinit(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000645
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000646 pmonitor->m_pid = fork();
647 if (pmonitor->m_pid == -1)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000648 fatal("fork of unprivileged child failed");
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000649 else if (pmonitor->m_pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000650 debug2("User child is on pid %ld", (long)pmonitor->m_pid);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000651 close(pmonitor->m_recvfd);
652 monitor_child_postauth(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000653
654 /* NEVERREACHED */
655 exit(0);
656 }
657
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000658 close(pmonitor->m_sendfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000659
660 /* Demote the private keys to public keys. */
661 demote_sensitive_data();
662
663 /* Drop privileges */
664 do_setusercontext(authctxt->pw);
665
666 /* It is safe now to apply the key state */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000667 monitor_apply_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000668}
669
Ben Lindstrombba81212001-06-25 05:01:22 +0000670static char *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100671list_hostkey_types(void)
672{
Damien Miller0e3b8722002-01-22 23:26:38 +1100673 Buffer b;
Damien Millerf58b58c2003-11-17 21:18:23 +1100674 const char *p;
675 char *ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100676 int i;
Damien Miller0e3b8722002-01-22 23:26:38 +1100677
678 buffer_init(&b);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100679 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100680 Key *key = sensitive_data.host_keys[i];
681 if (key == NULL)
682 continue;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000683 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100684 case KEY_RSA:
685 case KEY_DSA:
Damien Miller0e3b8722002-01-22 23:26:38 +1100686 if (buffer_len(&b) > 0)
687 buffer_append(&b, ",", 1);
688 p = key_ssh_name(key);
689 buffer_append(&b, p, strlen(p));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100690 break;
691 }
692 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100693 buffer_append(&b, "\0", 1);
Damien Millerf58b58c2003-11-17 21:18:23 +1100694 ret = xstrdup(buffer_ptr(&b));
Damien Miller0e3b8722002-01-22 23:26:38 +1100695 buffer_free(&b);
Damien Millerf58b58c2003-11-17 21:18:23 +1100696 debug("list_hostkey_types: %s", ret);
697 return ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100698}
699
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000700Key *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100701get_hostkey_by_type(int type)
702{
703 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000704
Damien Miller9f0f5c62001-12-21 14:45:46 +1100705 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100706 Key *key = sensitive_data.host_keys[i];
707 if (key != NULL && key->type == type)
708 return key;
709 }
710 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000711}
712
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000713Key *
714get_hostkey_by_index(int ind)
715{
716 if (ind < 0 || ind >= options.num_host_key_files)
717 return (NULL);
718 return (sensitive_data.host_keys[ind]);
719}
720
721int
722get_hostkey_index(Key *key)
723{
724 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000725
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000726 for (i = 0; i < options.num_host_key_files; i++) {
727 if (key == sensitive_data.host_keys[i])
728 return (i);
729 }
730 return (-1);
731}
732
Damien Miller942da032000-08-18 13:59:06 +1000733/*
734 * returns 1 if connection should be dropped, 0 otherwise.
735 * dropping starts at connection #max_startups_begin with a probability
736 * of (max_startups_rate/100). the probability increases linearly until
737 * all connections are dropped for startups > max_startups
738 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000739static int
Damien Miller942da032000-08-18 13:59:06 +1000740drop_connection(int startups)
741{
742 double p, r;
743
744 if (startups < options.max_startups_begin)
745 return 0;
746 if (startups >= options.max_startups)
747 return 1;
748 if (options.max_startups_rate == 100)
749 return 1;
750
751 p = 100 - options.max_startups_rate;
752 p *= startups - options.max_startups_begin;
753 p /= (double) (options.max_startups - options.max_startups_begin);
754 p += options.max_startups_rate;
755 p /= 100.0;
756 r = arc4random() / (double) UINT_MAX;
757
758 debug("drop_connection: p %g, r %g", p, r);
759 return (r < p) ? 1 : 0;
760}
761
Ben Lindstromade03f62001-12-06 18:22:17 +0000762static void
763usage(void)
764{
Damien Miller0c889cd2004-03-22 09:36:00 +1100765 fprintf(stderr, "%s, %s\n",
Darren Tucker4a250542003-10-03 17:57:24 +1000766 SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
Damien Millerb4087862004-03-22 09:35:21 +1100767 fprintf(stderr,
768"usage: sshd [-46Ddeiqt] [-b bits] [-f config_file] [-g login_grace_time]\n"
769" [-h host_key_file] [-k key_gen_time] [-o option] [-p port] [-u len]\n"
770 );
Ben Lindstromade03f62001-12-06 18:22:17 +0000771 exit(1);
772}
773
Damien Miller95def091999-11-25 00:26:21 +1100774/*
775 * Main program for the daemon.
776 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000777int
778main(int ac, char **av)
779{
Damien Miller95def091999-11-25 00:26:21 +1100780 extern char *optarg;
781 extern int optind;
Damien Miller37023962000-07-11 17:31:38 +1000782 int opt, sock_in = 0, sock_out = 0, newsock, j, i, fdsetsz, on = 1;
Damien Miller166fca82000-04-20 07:42:21 +1000783 pid_t pid;
Damien Miller34132e52000-01-14 15:45:46 +1100784 socklen_t fromlen;
Damien Miller34132e52000-01-14 15:45:46 +1100785 fd_set *fdset;
786 struct sockaddr_storage from;
Damien Miller95def091999-11-25 00:26:21 +1100787 const char *remote_ip;
788 int remote_port;
Damien Miller95def091999-11-25 00:26:21 +1100789 FILE *f;
Damien Miller34132e52000-01-14 15:45:46 +1100790 struct addrinfo *ai;
791 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerb9997192003-12-17 16:29:22 +1100792 char *line;
Damien Miller34132e52000-01-14 15:45:46 +1100793 int listen_sock, maxfd;
Damien Miller37023962000-07-11 17:31:38 +1000794 int startup_p[2];
795 int startups = 0;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000796 Key *key;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000797 Authctxt *authctxt;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000798 int ret, key_used = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000799
Kevin Steves0ea1d9d2002-04-25 18:17:04 +0000800#ifdef HAVE_SECUREWARE
801 (void)set_auth_parameters(ac, av);
802#endif
Damien Miller59d3d5b2003-08-22 09:34:41 +1000803 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000804 init_rng();
805
Damien Millera8ed44b2003-01-10 09:53:12 +1100806 /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
Damien Millerb8c656e2000-06-28 15:22:41 +1000807 saved_argc = ac;
Damien Miller04cb5362003-05-15 21:29:10 +1000808 saved_argv = xmalloc(sizeof(*saved_argv) * (ac + 1));
Damien Millera8ed44b2003-01-10 09:53:12 +1100809 for (i = 0; i < ac; i++)
810 saved_argv[i] = xstrdup(av[i]);
Damien Miller04cb5362003-05-15 21:29:10 +1000811 saved_argv[i] = NULL;
Damien Millera8ed44b2003-01-10 09:53:12 +1100812
813#ifndef HAVE_SETPROCTITLE
814 /* Prepare for later setproctitle emulation */
815 compat_init_setproctitle(ac, av);
Damien Millerf2e3e9d2003-06-02 12:15:54 +1000816 av = saved_argv;
Damien Millera8ed44b2003-01-10 09:53:12 +1100817#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818
Damien Millerbfba3542004-03-22 09:29:57 +1100819 if (geteuid() == 0 && setgroups(0, NULL) == -1)
820 debug("setgroups(): %.200s", strerror(errno));
821
Damien Miller95def091999-11-25 00:26:21 +1100822 /* Initialize configuration options to their default values. */
823 initialize_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000824
Damien Miller95def091999-11-25 00:26:21 +1100825 /* Parse command-line arguments. */
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000826 while ((opt = getopt(ac, av, "f:p:b:k:h:g:u:o:dDeiqtQ46")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100827 switch (opt) {
Damien Miller34132e52000-01-14 15:45:46 +1100828 case '4':
829 IPv4or6 = AF_INET;
830 break;
831 case '6':
832 IPv4or6 = AF_INET6;
833 break;
Damien Miller95def091999-11-25 00:26:21 +1100834 case 'f':
835 config_file_name = optarg;
836 break;
837 case 'd':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000838 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100839 debug_flag = 1;
840 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000841 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
Damien Millere4340be2000-09-16 13:29:08 +1100842 options.log_level++;
Damien Miller95def091999-11-25 00:26:21 +1100843 break;
Ben Lindstromc72745a2000-12-02 19:03:54 +0000844 case 'D':
845 no_daemon_flag = 1;
846 break;
Ben Lindstrom9fce9f02001-04-11 23:10:09 +0000847 case 'e':
848 log_stderr = 1;
849 break;
Damien Miller95def091999-11-25 00:26:21 +1100850 case 'i':
851 inetd_flag = 1;
852 break;
853 case 'Q':
Ben Lindstromd5390202001-01-29 08:07:43 +0000854 /* ignored */
Damien Miller95def091999-11-25 00:26:21 +1100855 break;
856 case 'q':
857 options.log_level = SYSLOG_LEVEL_QUIET;
858 break;
859 case 'b':
860 options.server_key_bits = atoi(optarg);
861 break;
862 case 'p':
Damien Miller34132e52000-01-14 15:45:46 +1100863 options.ports_from_cmdline = 1;
Damien Millere4340be2000-09-16 13:29:08 +1100864 if (options.num_ports >= MAX_PORTS) {
865 fprintf(stderr, "too many ports.\n");
866 exit(1);
867 }
Ben Lindstrom19066a12001-04-12 23:39:26 +0000868 options.ports[options.num_ports++] = a2port(optarg);
869 if (options.ports[options.num_ports-1] == 0) {
870 fprintf(stderr, "Bad port number.\n");
871 exit(1);
872 }
Damien Miller95def091999-11-25 00:26:21 +1100873 break;
874 case 'g':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000875 if ((options.login_grace_time = convtime(optarg)) == -1) {
876 fprintf(stderr, "Invalid login grace time.\n");
877 exit(1);
878 }
Damien Miller95def091999-11-25 00:26:21 +1100879 break;
880 case 'k':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000881 if ((options.key_regeneration_time = convtime(optarg)) == -1) {
882 fprintf(stderr, "Invalid key regeneration interval.\n");
883 exit(1);
884 }
Damien Miller95def091999-11-25 00:26:21 +1100885 break;
886 case 'h':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100887 if (options.num_host_key_files >= MAX_HOSTKEYS) {
888 fprintf(stderr, "too many host keys.\n");
889 exit(1);
890 }
891 options.host_key_files[options.num_host_key_files++] = optarg;
Damien Miller95def091999-11-25 00:26:21 +1100892 break;
Ben Lindstrom794325a2001-08-06 21:09:07 +0000893 case 't':
894 test_flag = 1;
895 break;
Damien Miller942da032000-08-18 13:59:06 +1000896 case 'u':
897 utmp_len = atoi(optarg);
Ben Lindstrom41daec72002-07-23 21:15:13 +0000898 if (utmp_len > MAXHOSTNAMELEN) {
899 fprintf(stderr, "Invalid utmp length.\n");
900 exit(1);
901 }
Damien Miller942da032000-08-18 13:59:06 +1000902 break;
Ben Lindstromade03f62001-12-06 18:22:17 +0000903 case 'o':
Damien Millerb9997192003-12-17 16:29:22 +1100904 line = xstrdup(optarg);
905 if (process_server_config_line(&options, line,
Ben Lindstromade03f62001-12-06 18:22:17 +0000906 "command-line", 0) != 0)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100907 exit(1);
Damien Millerb9997192003-12-17 16:29:22 +1100908 xfree(line);
Ben Lindstromade03f62001-12-06 18:22:17 +0000909 break;
Damien Miller95def091999-11-25 00:26:21 +1100910 case '?':
911 default:
Ben Lindstromade03f62001-12-06 18:22:17 +0000912 usage();
913 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000915 }
Ben Lindstrom425fb022001-03-29 00:31:20 +0000916 SSLeay_add_all_algorithms();
Ben Lindstrom908afed2001-10-03 17:34:59 +0000917 channel_set_af(IPv4or6);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918
Damien Miller34132e52000-01-14 15:45:46 +1100919 /*
920 * Force logging to stderr until we have loaded the private host
921 * key (unless started from inetd)
922 */
Kevin Stevesec84dc12000-12-13 17:45:15 +0000923 log_init(__progname,
Damien Miller5aa5d782002-02-08 22:01:54 +1100924 options.log_level == SYSLOG_LEVEL_NOT_SET ?
925 SYSLOG_LEVEL_INFO : options.log_level,
926 options.log_facility == SYSLOG_FACILITY_NOT_SET ?
927 SYSLOG_FACILITY_AUTH : options.log_facility,
Ben Lindstromc2faa4a2002-11-09 15:50:03 +0000928 log_stderr || !inetd_flag);
Damien Miller34132e52000-01-14 15:45:46 +1100929
Darren Tucker86c093d2004-03-08 22:59:03 +1100930#ifdef _AIX
931 /*
932 * Unset KRB5CCNAME, otherwise the user's session may inherit it from
933 * root's environment
934 */
935 unsetenv("KRB5CCNAME");
936#endif /* _AIX */
Tim Rice81ed5182002-09-25 17:38:46 -0700937#ifdef _UNICOS
Darren Tucker12984962004-05-24 13:37:13 +1000938 /* Cray can define user privs drop all privs now!
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000939 * Not needed on PRIV_SU systems!
940 */
941 drop_cray_privs();
942#endif
943
Damien Miller60bc5172001-03-19 09:38:15 +1100944 seed_rng();
945
Damien Miller95def091999-11-25 00:26:21 +1100946 /* Read server configuration options from the configuration file. */
947 read_server_config(&options, config_file_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948
Damien Miller95def091999-11-25 00:26:21 +1100949 /* Fill in default values for those options not explicitly set. */
950 fill_default_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951
Damien Miller95def091999-11-25 00:26:21 +1100952 /* Check that there are no remaining arguments. */
953 if (optind < ac) {
954 fprintf(stderr, "Extra argument %s.\n", av[optind]);
955 exit(1);
956 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957
Damien Miller95def091999-11-25 00:26:21 +1100958 debug("sshd version %.100s", SSH_VERSION);
Damien Miller2ccf6611999-11-15 15:25:10 +1100959
Damien Miller0bc1bd82000-11-13 22:57:25 +1100960 /* load private host keys */
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000961 sensitive_data.host_keys = xmalloc(options.num_host_key_files *
962 sizeof(Key *));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100963 for (i = 0; i < options.num_host_key_files; i++)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000964 sensitive_data.host_keys[i] = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100965 sensitive_data.server_key = NULL;
966 sensitive_data.ssh1_host_key = NULL;
967 sensitive_data.have_ssh1_key = 0;
968 sensitive_data.have_ssh2_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000969
Damien Miller9f0f5c62001-12-21 14:45:46 +1100970 for (i = 0; i < options.num_host_key_files; i++) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000971 key = key_load_private(options.host_key_files[i], "", NULL);
972 sensitive_data.host_keys[i] = key;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100973 if (key == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000974 error("Could not load host key: %s",
975 options.host_key_files[i]);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000976 sensitive_data.host_keys[i] = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100977 continue;
978 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000979 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100980 case KEY_RSA1:
981 sensitive_data.ssh1_host_key = key;
982 sensitive_data.have_ssh1_key = 1;
983 break;
984 case KEY_RSA:
985 case KEY_DSA:
986 sensitive_data.have_ssh2_key = 1;
987 break;
988 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000989 debug("private host key: #%d type %d %s", i, key->type,
990 key_type(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100991 }
992 if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
Damien Miller996acd22003-04-09 20:59:48 +1000993 logit("Disabling protocol version 1. Could not load host key");
Damien Millereba71ba2000-04-29 23:57:08 +1000994 options.protocol &= ~SSH_PROTO_1;
995 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100996 if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
Damien Miller996acd22003-04-09 20:59:48 +1000997 logit("Disabling protocol version 2. Could not load host key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100998 options.protocol &= ~SSH_PROTO_2;
Damien Millereba71ba2000-04-29 23:57:08 +1000999 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001000 if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
Damien Miller996acd22003-04-09 20:59:48 +10001001 logit("sshd: no hostkeys available -- exiting.");
Damien Miller95def091999-11-25 00:26:21 +11001002 exit(1);
1003 }
Damien Miller95def091999-11-25 00:26:21 +11001004
Damien Millereba71ba2000-04-29 23:57:08 +10001005 /* Check certain values for sanity. */
1006 if (options.protocol & SSH_PROTO_1) {
1007 if (options.server_key_bits < 512 ||
1008 options.server_key_bits > 32768) {
1009 fprintf(stderr, "Bad server key size.\n");
1010 exit(1);
1011 }
1012 /*
1013 * Check that server and host key lengths differ sufficiently. This
1014 * is necessary to make double encryption work with rsaref. Oh, I
1015 * hate software patents. I dont know if this can go? Niels
1016 */
1017 if (options.server_key_bits >
Ben Lindstrom822b6342002-06-23 21:38:49 +00001018 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) -
1019 SSH_KEY_BITS_RESERVED && options.server_key_bits <
1020 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1021 SSH_KEY_BITS_RESERVED) {
Damien Millereba71ba2000-04-29 23:57:08 +10001022 options.server_key_bits =
Ben Lindstrom822b6342002-06-23 21:38:49 +00001023 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1024 SSH_KEY_BITS_RESERVED;
Damien Millereba71ba2000-04-29 23:57:08 +10001025 debug("Forcing server key to %d bits to make it differ from host key.",
1026 options.server_key_bits);
1027 }
1028 }
1029
Ben Lindstroma26ea632002-06-06 20:46:25 +00001030 if (use_privsep) {
1031 struct passwd *pw;
1032 struct stat st;
1033
1034 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
1035 fatal("Privilege separation user %s does not exist",
1036 SSH_PRIVSEP_USER);
1037 if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
1038 (S_ISDIR(st.st_mode) == 0))
1039 fatal("Missing privilege separation directory: %s",
1040 _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstrom59627352002-06-27 18:02:21 +00001041
1042#ifdef HAVE_CYGWIN
1043 if (check_ntsec(_PATH_PRIVSEP_CHROOT_DIR) &&
1044 (st.st_uid != getuid () ||
1045 (st.st_mode & (S_IWGRP|S_IWOTH)) != 0))
1046#else
Ben Lindstrom2dfacb32002-06-23 00:33:47 +00001047 if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
Ben Lindstrom59627352002-06-27 18:02:21 +00001048#endif
Damien Miller180fc5b2003-02-24 11:50:18 +11001049 fatal("%s must be owned by root and not group or "
1050 "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstroma26ea632002-06-06 20:46:25 +00001051 }
1052
Ben Lindstrom794325a2001-08-06 21:09:07 +00001053 /* Configuration looks good, so exit if in test mode. */
1054 if (test_flag)
1055 exit(0);
1056
Damien Miller87aea252002-05-10 12:20:24 +10001057 /*
1058 * Clear out any supplemental groups we may have inherited. This
1059 * prevents inadvertent creation of files with bad modes (in the
Damien Millera8e06ce2003-11-21 23:48:55 +11001060 * portable version at least, it's certainly possible for PAM
1061 * to create a file, and we can't control the code in every
Damien Miller87aea252002-05-10 12:20:24 +10001062 * module which might be used).
1063 */
1064 if (setgroups(0, NULL) < 0)
1065 debug("setgroups() failed: %.200s", strerror(errno));
1066
Damien Millereba71ba2000-04-29 23:57:08 +10001067 /* Initialize the log (it is reinitialized below in case we forked). */
Damien Miller95def091999-11-25 00:26:21 +11001068 if (debug_flag && !inetd_flag)
1069 log_stderr = 1;
Kevin Stevesec84dc12000-12-13 17:45:15 +00001070 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001071
Damien Millereba71ba2000-04-29 23:57:08 +10001072 /*
1073 * If not in debugging mode, and not started from inetd, disconnect
1074 * from the controlling terminal, and fork. The original process
1075 * exits.
1076 */
Ben Lindstromc72745a2000-12-02 19:03:54 +00001077 if (!(debug_flag || inetd_flag || no_daemon_flag)) {
Damien Miller95def091999-11-25 00:26:21 +11001078#ifdef TIOCNOTTY
1079 int fd;
1080#endif /* TIOCNOTTY */
1081 if (daemon(0, 0) < 0)
1082 fatal("daemon() failed: %.200s", strerror(errno));
1083
1084 /* Disconnect from the controlling tty. */
1085#ifdef TIOCNOTTY
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001086 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
Damien Miller95def091999-11-25 00:26:21 +11001087 if (fd >= 0) {
1088 (void) ioctl(fd, TIOCNOTTY, NULL);
1089 close(fd);
1090 }
1091#endif /* TIOCNOTTY */
1092 }
1093 /* Reinitialize the log (because of the fork above). */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001094 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001095
Damien Miller95def091999-11-25 00:26:21 +11001096 /* Initialize the random number generator. */
1097 arc4random_stir();
1098
1099 /* Chdir to the root directory so that the current disk can be
1100 unmounted if desired. */
1101 chdir("/");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001102
Ben Lindstromde71cda2001-03-24 00:43:26 +00001103 /* ignore SIGPIPE */
1104 signal(SIGPIPE, SIG_IGN);
Damien Miller95def091999-11-25 00:26:21 +11001105
Damien Miller95def091999-11-25 00:26:21 +11001106 /* Start listening for a socket, unless started from inetd. */
1107 if (inetd_flag) {
Ben Lindstrom206941f2001-04-15 14:27:16 +00001108 int s1;
Damien Miller95def091999-11-25 00:26:21 +11001109 s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */
Ben Lindstrom206941f2001-04-15 14:27:16 +00001110 dup(s1);
Damien Miller95def091999-11-25 00:26:21 +11001111 sock_in = dup(0);
1112 sock_out = dup(1);
Damien Miller994cf142000-07-21 10:19:44 +10001113 startup_pipe = -1;
Damien Millereba71ba2000-04-29 23:57:08 +10001114 /*
1115 * We intentionally do not close the descriptors 0, 1, and 2
1116 * as our code for setting the descriptors won\'t work if
1117 * ttyfd happens to be one of those.
1118 */
Damien Miller95def091999-11-25 00:26:21 +11001119 debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001120 if (options.protocol & SSH_PROTO_1)
Ben Lindstromca42d5f2001-03-09 18:25:32 +00001121 generate_ephemeral_server_key();
Damien Miller95def091999-11-25 00:26:21 +11001122 } else {
Damien Miller34132e52000-01-14 15:45:46 +11001123 for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
1124 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1125 continue;
1126 if (num_listen_socks >= MAX_LISTEN_SOCKS)
1127 fatal("Too many listen sockets. "
1128 "Enlarge MAX_LISTEN_SOCKS");
1129 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
1130 ntop, sizeof(ntop), strport, sizeof(strport),
1131 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1132 error("getnameinfo failed");
1133 continue;
1134 }
1135 /* Create socket for listening. */
Damien Miller2372ace2003-05-14 13:42:23 +10001136 listen_sock = socket(ai->ai_family, ai->ai_socktype,
1137 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11001138 if (listen_sock < 0) {
1139 /* kernel may not support ipv6 */
1140 verbose("socket: %.100s", strerror(errno));
1141 continue;
1142 }
Darren Tuckerefa37062004-02-24 09:20:29 +11001143 if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) {
1144 error("listen_sock O_NONBLOCK: %s", strerror(errno));
1145 close(listen_sock);
1146 continue;
1147 }
Damien Miller34132e52000-01-14 15:45:46 +11001148 /*
Damien Millere1383ce2002-09-19 11:49:37 +10001149 * Set socket options.
1150 * Allow local port reuse in TIME_WAIT.
Damien Miller34132e52000-01-14 15:45:46 +11001151 */
Damien Millere1383ce2002-09-19 11:49:37 +10001152 if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
1153 &on, sizeof(on)) == -1)
1154 error("setsockopt SO_REUSEADDR: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001155
Damien Miller34132e52000-01-14 15:45:46 +11001156 debug("Bind to port %s on %s.", strport, ntop);
Damien Miller95def091999-11-25 00:26:21 +11001157
Damien Miller34132e52000-01-14 15:45:46 +11001158 /* Bind the socket to the desired port. */
Damien Miller0a4e27d2001-02-18 12:36:39 +11001159 if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1160 if (!ai->ai_next)
1161 error("Bind to port %s on %s failed: %.200s.",
1162 strport, ntop, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11001163 close(listen_sock);
1164 continue;
1165 }
1166 listen_socks[num_listen_socks] = listen_sock;
1167 num_listen_socks++;
Damien Miller95def091999-11-25 00:26:21 +11001168
Damien Miller34132e52000-01-14 15:45:46 +11001169 /* Start listening on the port. */
Damien Miller996acd22003-04-09 20:59:48 +10001170 logit("Server listening on %s port %s.", ntop, strport);
Darren Tucker3175eb92003-12-09 19:15:11 +11001171 if (listen(listen_sock, SSH_LISTEN_BACKLOG) < 0)
Damien Miller34132e52000-01-14 15:45:46 +11001172 fatal("listen: %.100s", strerror(errno));
1173
Damien Miller95def091999-11-25 00:26:21 +11001174 }
Damien Miller34132e52000-01-14 15:45:46 +11001175 freeaddrinfo(options.listen_addrs);
1176
1177 if (!num_listen_socks)
1178 fatal("Cannot bind any address.");
1179
Ben Lindstrom98097862001-06-25 05:10:20 +00001180 if (options.protocol & SSH_PROTO_1)
1181 generate_ephemeral_server_key();
1182
1183 /*
1184 * Arrange to restart on SIGHUP. The handler needs
1185 * listen_sock.
1186 */
1187 signal(SIGHUP, sighup_handler);
1188
1189 signal(SIGTERM, sigterm_handler);
1190 signal(SIGQUIT, sigterm_handler);
1191
1192 /* Arrange SIGCHLD to be caught. */
1193 signal(SIGCHLD, main_sigchld_handler);
1194
1195 /* Write out the pid file after the sigterm handler is setup */
Damien Miller95def091999-11-25 00:26:21 +11001196 if (!debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +11001197 /*
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001198 * Record our pid in /var/run/sshd.pid to make it
1199 * easier to kill the correct sshd. We don't want to
1200 * do this before the bind above because the bind will
Damien Miller5428f641999-11-25 11:54:57 +11001201 * fail if there already is a daemon, and this will
1202 * overwrite any old pid in the file.
1203 */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001204 f = fopen(options.pid_file, "wb");
Darren Tuckere5327042003-07-03 13:40:44 +10001205 if (f == NULL) {
1206 error("Couldn't create pid file \"%s\": %s",
1207 options.pid_file, strerror(errno));
1208 } else {
Ben Lindstromce0f6342002-06-11 16:42:49 +00001209 fprintf(f, "%ld\n", (long) getpid());
Damien Miller95def091999-11-25 00:26:21 +11001210 fclose(f);
1211 }
1212 }
Damien Miller95def091999-11-25 00:26:21 +11001213
Damien Miller34132e52000-01-14 15:45:46 +11001214 /* setup fd set for listen */
Damien Miller37023962000-07-11 17:31:38 +10001215 fdset = NULL;
Damien Miller34132e52000-01-14 15:45:46 +11001216 maxfd = 0;
1217 for (i = 0; i < num_listen_socks; i++)
1218 if (listen_socks[i] > maxfd)
1219 maxfd = listen_socks[i];
Damien Miller37023962000-07-11 17:31:38 +10001220 /* pipes connected to unauthenticated childs */
1221 startup_pipes = xmalloc(options.max_startups * sizeof(int));
1222 for (i = 0; i < options.max_startups; i++)
1223 startup_pipes[i] = -1;
Damien Miller34132e52000-01-14 15:45:46 +11001224
Damien Miller5428f641999-11-25 11:54:57 +11001225 /*
1226 * Stay listening for connections until the system crashes or
1227 * the daemon is killed with a signal.
1228 */
Damien Miller95def091999-11-25 00:26:21 +11001229 for (;;) {
1230 if (received_sighup)
1231 sighup_restart();
Damien Miller37023962000-07-11 17:31:38 +10001232 if (fdset != NULL)
1233 xfree(fdset);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001234 fdsetsz = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
Damien Miller37023962000-07-11 17:31:38 +10001235 fdset = (fd_set *)xmalloc(fdsetsz);
Damien Miller34132e52000-01-14 15:45:46 +11001236 memset(fdset, 0, fdsetsz);
Damien Miller37023962000-07-11 17:31:38 +10001237
Damien Miller34132e52000-01-14 15:45:46 +11001238 for (i = 0; i < num_listen_socks; i++)
1239 FD_SET(listen_socks[i], fdset);
Damien Miller37023962000-07-11 17:31:38 +10001240 for (i = 0; i < options.max_startups; i++)
1241 if (startup_pipes[i] != -1)
1242 FD_SET(startup_pipes[i], fdset);
1243
1244 /* Wait in select until there is a connection. */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001245 ret = select(maxfd+1, fdset, NULL, NULL, NULL);
1246 if (ret < 0 && errno != EINTR)
1247 error("select: %.100s", strerror(errno));
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001248 if (received_sigterm) {
Damien Miller996acd22003-04-09 20:59:48 +10001249 logit("Received signal %d; terminating.",
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001250 (int) received_sigterm);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001251 close_listen_socks();
1252 unlink(options.pid_file);
1253 exit(255);
1254 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001255 if (key_used && key_do_regen) {
Ben Lindstromca42d5f2001-03-09 18:25:32 +00001256 generate_ephemeral_server_key();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001257 key_used = 0;
1258 key_do_regen = 0;
Damien Miller34132e52000-01-14 15:45:46 +11001259 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001260 if (ret < 0)
1261 continue;
1262
Damien Miller37023962000-07-11 17:31:38 +10001263 for (i = 0; i < options.max_startups; i++)
1264 if (startup_pipes[i] != -1 &&
1265 FD_ISSET(startup_pipes[i], fdset)) {
1266 /*
1267 * the read end of the pipe is ready
1268 * if the child has closed the pipe
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001269 * after successful authentication
Damien Miller37023962000-07-11 17:31:38 +10001270 * or if the child has died
1271 */
1272 close(startup_pipes[i]);
1273 startup_pipes[i] = -1;
1274 startups--;
1275 }
Damien Miller34132e52000-01-14 15:45:46 +11001276 for (i = 0; i < num_listen_socks; i++) {
1277 if (!FD_ISSET(listen_socks[i], fdset))
Damien Miller95def091999-11-25 00:26:21 +11001278 continue;
Damien Miller37023962000-07-11 17:31:38 +10001279 fromlen = sizeof(from);
1280 newsock = accept(listen_socks[i], (struct sockaddr *)&from,
1281 &fromlen);
1282 if (newsock < 0) {
1283 if (errno != EINTR && errno != EWOULDBLOCK)
1284 error("accept: %.100s", strerror(errno));
1285 continue;
1286 }
Darren Tuckerefa37062004-02-24 09:20:29 +11001287 if (fcntl(newsock, F_SETFL, 0) < 0) {
1288 error("newsock del O_NONBLOCK: %s", strerror(errno));
1289 close(newsock);
1290 continue;
1291 }
Damien Miller942da032000-08-18 13:59:06 +10001292 if (drop_connection(startups) == 1) {
1293 debug("drop connection #%d", startups);
Damien Miller37023962000-07-11 17:31:38 +10001294 close(newsock);
1295 continue;
1296 }
1297 if (pipe(startup_p) == -1) {
1298 close(newsock);
1299 continue;
1300 }
1301
1302 for (j = 0; j < options.max_startups; j++)
1303 if (startup_pipes[j] == -1) {
1304 startup_pipes[j] = startup_p[0];
1305 if (maxfd < startup_p[0])
1306 maxfd = startup_p[0];
1307 startups++;
1308 break;
1309 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001310
Damien Miller5428f641999-11-25 11:54:57 +11001311 /*
Damien Miller37023962000-07-11 17:31:38 +10001312 * Got connection. Fork a child to handle it, unless
1313 * we are in debugging mode.
Damien Miller5428f641999-11-25 11:54:57 +11001314 */
Damien Miller37023962000-07-11 17:31:38 +10001315 if (debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +11001316 /*
Damien Miller37023962000-07-11 17:31:38 +10001317 * In debugging mode. Close the listening
1318 * socket, and start processing the
1319 * connection without forking.
Damien Miller5428f641999-11-25 11:54:57 +11001320 */
Damien Miller37023962000-07-11 17:31:38 +10001321 debug("Server will not fork when running in debugging mode.");
Damien Miller34132e52000-01-14 15:45:46 +11001322 close_listen_socks();
Damien Miller95def091999-11-25 00:26:21 +11001323 sock_in = newsock;
1324 sock_out = newsock;
Damien Miller4d97ba22000-07-11 18:15:50 +10001325 startup_pipe = -1;
Damien Miller182ee6e2000-07-12 09:45:27 +10001326 pid = getpid();
Damien Miller95def091999-11-25 00:26:21 +11001327 break;
Damien Miller37023962000-07-11 17:31:38 +10001328 } else {
1329 /*
1330 * Normal production daemon. Fork, and have
1331 * the child process the connection. The
1332 * parent continues listening.
1333 */
1334 if ((pid = fork()) == 0) {
1335 /*
1336 * Child. Close the listening and max_startup
1337 * sockets. Start using the accepted socket.
1338 * Reinitialize logging (since our pid has
1339 * changed). We break out of the loop to handle
1340 * the connection.
1341 */
1342 startup_pipe = startup_p[1];
Ben Lindstromd84df982001-12-06 16:35:40 +00001343 close_startup_pipes();
Damien Miller37023962000-07-11 17:31:38 +10001344 close_listen_socks();
1345 sock_in = newsock;
1346 sock_out = newsock;
Kevin Stevesec84dc12000-12-13 17:45:15 +00001347 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller37023962000-07-11 17:31:38 +10001348 break;
1349 }
Damien Miller95def091999-11-25 00:26:21 +11001350 }
Damien Miller37023962000-07-11 17:31:38 +10001351
1352 /* Parent. Stay in the loop. */
1353 if (pid < 0)
1354 error("fork: %.100s", strerror(errno));
1355 else
Ben Lindstromce0f6342002-06-11 16:42:49 +00001356 debug("Forked child %ld.", (long)pid);
Damien Miller37023962000-07-11 17:31:38 +10001357
1358 close(startup_p[1]);
1359
1360 /* Mark that the key has been used (it was "given" to the child). */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001361 if ((options.protocol & SSH_PROTO_1) &&
1362 key_used == 0) {
1363 /* Schedule server key regeneration alarm. */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00001364 signal(SIGALRM, key_regeneration_alarm);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001365 alarm(options.key_regeneration_time);
1366 key_used = 1;
1367 }
Damien Miller37023962000-07-11 17:31:38 +10001368
1369 arc4random_stir();
1370
1371 /* Close the new socket (the child is now taking care of it). */
1372 close(newsock);
Damien Miller95def091999-11-25 00:26:21 +11001373 }
Damien Miller34132e52000-01-14 15:45:46 +11001374 /* child process check (or debug mode) */
1375 if (num_listen_socks < 0)
1376 break;
Damien Miller95def091999-11-25 00:26:21 +11001377 }
1378 }
1379
1380 /* This is the child processing a new connection. */
Damien Miller57aae982004-03-08 23:11:25 +11001381 setproctitle("%s", "[accepted]");
Damien Miller95def091999-11-25 00:26:21 +11001382
Damien Miller5428f641999-11-25 11:54:57 +11001383 /*
Ben Lindstrom17401b62002-05-15 16:17:56 +00001384 * Create a new session and process group since the 4.4BSD
1385 * setlogin() affects the entire process group. We don't
1386 * want the child to be able to affect the parent.
1387 */
Darren Tuckerc437cda2003-05-10 17:05:46 +10001388#if !defined(SSHD_ACQUIRES_CTTY)
Damien Miller933cc8f2003-03-10 11:38:10 +11001389 /*
Darren Tuckerc437cda2003-05-10 17:05:46 +10001390 * If setsid is called, on some platforms sshd will later acquire a
1391 * controlling terminal which will result in "could not set
1392 * controlling tty" errors.
Damien Miller933cc8f2003-03-10 11:38:10 +11001393 */
Ben Lindstrom57f08002002-06-23 00:37:10 +00001394 if (!debug_flag && !inetd_flag && setsid() < 0)
Ben Lindstrom17401b62002-05-15 16:17:56 +00001395 error("setsid: %.100s", strerror(errno));
Kevin Stevesc5041ac2002-05-21 17:50:21 +00001396#endif
Ben Lindstrom17401b62002-05-15 16:17:56 +00001397
1398 /*
Damien Miller5428f641999-11-25 11:54:57 +11001399 * Disable the key regeneration alarm. We will not regenerate the
1400 * key since we are no longer in a position to give it to anyone. We
1401 * will not restart on SIGHUP since it no longer makes sense.
1402 */
Damien Miller95def091999-11-25 00:26:21 +11001403 alarm(0);
1404 signal(SIGALRM, SIG_DFL);
1405 signal(SIGHUP, SIG_DFL);
1406 signal(SIGTERM, SIG_DFL);
1407 signal(SIGQUIT, SIG_DFL);
1408 signal(SIGCHLD, SIG_DFL);
Damien Miller4e0f5e12000-08-29 11:05:50 +11001409 signal(SIGINT, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001410
Damien Miller12c150e2003-12-17 16:31:10 +11001411 /* Set SO_KEEPALIVE if requested. */
1412 if (options.tcp_keep_alive &&
Ben Lindstrom733a2352002-03-05 01:31:28 +00001413 setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on,
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001414 sizeof(on)) < 0)
1415 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1416
Damien Miller5428f641999-11-25 11:54:57 +11001417 /*
1418 * Register our connection. This turns encryption off because we do
1419 * not have a key.
1420 */
Damien Miller95def091999-11-25 00:26:21 +11001421 packet_set_connection(sock_in, sock_out);
1422
1423 remote_port = get_remote_port();
1424 remote_ip = get_remote_ipaddr();
1425
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001426#ifdef LIBWRAP
Damien Miller6a4a4b92001-11-12 11:07:11 +11001427 /* Check whether logins are denied from this host. */
Damien Miller95def091999-11-25 00:26:21 +11001428 {
1429 struct request_info req;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001430
Ben Lindstromce89dac2001-09-12 16:58:04 +00001431 request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, 0);
Damien Miller95def091999-11-25 00:26:21 +11001432 fromhost(&req);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001433
Damien Miller95def091999-11-25 00:26:21 +11001434 if (!hosts_access(&req)) {
Damien Miller6a4a4b92001-11-12 11:07:11 +11001435 debug("Connection refused by tcp wrapper");
Ben Lindstrom7de696e2001-03-29 00:45:12 +00001436 refuse(&req);
Damien Miller6a4a4b92001-11-12 11:07:11 +11001437 /* NOTREACHED */
1438 fatal("libwrap refuse returns");
Damien Miller95def091999-11-25 00:26:21 +11001439 }
Damien Miller95def091999-11-25 00:26:21 +11001440 }
Damien Miller34132e52000-01-14 15:45:46 +11001441#endif /* LIBWRAP */
Damien Miller6a4a4b92001-11-12 11:07:11 +11001442
Damien Miller95def091999-11-25 00:26:21 +11001443 /* Log the connection. */
1444 verbose("Connection from %.500s port %d", remote_ip, remote_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001445
Damien Miller5428f641999-11-25 11:54:57 +11001446 /*
1447 * We don\'t want to listen forever unless the other side
1448 * successfully authenticates itself. So we set up an alarm which is
1449 * cleared after successful authentication. A limit of zero
1450 * indicates no limit. Note that we don\'t set the alarm in debugging
1451 * mode; it is just annoying to have the server exit just when you
1452 * are about to discover the bug.
1453 */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00001454 signal(SIGALRM, grace_alarm_handler);
Damien Miller95def091999-11-25 00:26:21 +11001455 if (!debug_flag)
1456 alarm(options.login_grace_time);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001457
Damien Millerb38eff82000-04-01 11:09:21 +10001458 sshd_exchange_identification(sock_in, sock_out);
Darren Tuckerec960f22003-08-13 20:37:05 +10001459
Damien Miller95def091999-11-25 00:26:21 +11001460 packet_set_nonblocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001461
Damien Millera8e06ce2003-11-21 23:48:55 +11001462 /* prepare buffers to collect authentication messages */
Darren Tuckerb9aa0a02003-07-08 22:59:59 +10001463 buffer_init(&loginmsg);
1464
Darren Tucker3e33cec2003-10-02 16:12:36 +10001465 /* allocate authentication context */
1466 authctxt = xmalloc(sizeof(*authctxt));
1467 memset(authctxt, 0, sizeof(*authctxt));
1468
1469 /* XXX global for cleanup, access from other modules */
1470 the_authctxt = authctxt;
1471
Ben Lindstrom943481c2002-03-22 03:43:46 +00001472 if (use_privsep)
Darren Tucker3e33cec2003-10-02 16:12:36 +10001473 if (privsep_preauth(authctxt) == 1)
Ben Lindstrom943481c2002-03-22 03:43:46 +00001474 goto authenticated;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001475
Damien Miller396691a2000-01-20 22:44:08 +11001476 /* perform the key exchange */
Damien Miller396691a2000-01-20 22:44:08 +11001477 /* authenticate user and start session */
Damien Millerefb4afe2000-04-12 18:45:05 +10001478 if (compat20) {
1479 do_ssh2_kex();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001480 do_authentication2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001481 } else {
1482 do_ssh1_kex();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001483 do_authentication(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001484 }
Ben Lindstrom943481c2002-03-22 03:43:46 +00001485 /*
1486 * If we use privilege separation, the unprivileged child transfers
1487 * the current keystate and exits
1488 */
1489 if (use_privsep) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001490 mm_send_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001491 exit(0);
Ben Lindstrom943481c2002-03-22 03:43:46 +00001492 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001493
1494 authenticated:
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001495 /*
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001496 * In privilege separation, we fork another child and prepare
1497 * file descriptor passing.
1498 */
1499 if (use_privsep) {
Ben Lindstrom943481c2002-03-22 03:43:46 +00001500 privsep_postauth(authctxt);
1501 /* the monitor process [priv] will not return */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001502 if (!compat20)
1503 destroy_sensitive_data();
1504 }
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00001505
Darren Tucker3e33cec2003-10-02 16:12:36 +10001506 /* Start session. */
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00001507 do_authenticated(authctxt);
1508
Damien Miller3a5b0232002-03-13 13:19:42 +11001509 /* The connection has been terminated. */
1510 verbose("Closing connection to %.100s", remote_ip);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001511
Damien Millerbeb4ba51999-12-28 15:09:35 +11001512#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +10001513 if (options.use_pam)
1514 finish_pam();
Damien Millerbeb4ba51999-12-28 15:09:35 +11001515#endif /* USE_PAM */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001516
Damien Miller95def091999-11-25 00:26:21 +11001517 packet_close();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001518
1519 if (use_privsep)
1520 mm_terminate();
1521
Damien Miller95def091999-11-25 00:26:21 +11001522 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001523}
1524
Damien Miller95def091999-11-25 00:26:21 +11001525/*
Ben Lindstromabcb1452002-03-22 01:10:21 +00001526 * Decrypt session_key_int using our private server key and private host key
1527 * (key with larger modulus first).
1528 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001529int
Ben Lindstromabcb1452002-03-22 01:10:21 +00001530ssh1_session_key(BIGNUM *session_key_int)
1531{
1532 int rsafail = 0;
1533
1534 if (BN_cmp(sensitive_data.server_key->rsa->n, sensitive_data.ssh1_host_key->rsa->n) > 0) {
1535 /* Server key has bigger modulus. */
1536 if (BN_num_bits(sensitive_data.server_key->rsa->n) <
1537 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1538 fatal("do_connection: %s: server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
1539 get_remote_ipaddr(),
1540 BN_num_bits(sensitive_data.server_key->rsa->n),
1541 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1542 SSH_KEY_BITS_RESERVED);
1543 }
1544 if (rsa_private_decrypt(session_key_int, session_key_int,
1545 sensitive_data.server_key->rsa) <= 0)
1546 rsafail++;
1547 if (rsa_private_decrypt(session_key_int, session_key_int,
1548 sensitive_data.ssh1_host_key->rsa) <= 0)
1549 rsafail++;
1550 } else {
1551 /* Host key has bigger modulus (or they are equal). */
1552 if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
1553 BN_num_bits(sensitive_data.server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1554 fatal("do_connection: %s: host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
1555 get_remote_ipaddr(),
1556 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1557 BN_num_bits(sensitive_data.server_key->rsa->n),
1558 SSH_KEY_BITS_RESERVED);
1559 }
1560 if (rsa_private_decrypt(session_key_int, session_key_int,
1561 sensitive_data.ssh1_host_key->rsa) < 0)
1562 rsafail++;
1563 if (rsa_private_decrypt(session_key_int, session_key_int,
1564 sensitive_data.server_key->rsa) < 0)
1565 rsafail++;
1566 }
1567 return (rsafail);
1568}
1569/*
Damien Miller396691a2000-01-20 22:44:08 +11001570 * SSH1 key exchange
Damien Miller95def091999-11-25 00:26:21 +11001571 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001572static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001573do_ssh1_kex(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001574{
Damien Miller95def091999-11-25 00:26:21 +11001575 int i, len;
Damien Miller7650bc62001-01-30 09:27:26 +11001576 int rsafail = 0;
Damien Miller95def091999-11-25 00:26:21 +11001577 BIGNUM *session_key_int;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001578 u_char session_key[SSH_SESSION_KEY_LENGTH];
1579 u_char cookie[8];
1580 u_int cipher_type, auth_mask, protocol_flags;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001581 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001582
Damien Miller5428f641999-11-25 11:54:57 +11001583 /*
1584 * Generate check bytes that the client must send back in the user
1585 * packet in order for it to be accepted; this is used to defy ip
1586 * spoofing attacks. Note that this only works against somebody
1587 * doing IP spoofing from a remote machine; any machine on the local
1588 * network can still see outgoing packets and catch the random
1589 * cookie. This only affects rhosts authentication, and this is one
1590 * of the reasons why it is inherently insecure.
1591 */
Damien Miller95def091999-11-25 00:26:21 +11001592 for (i = 0; i < 8; i++) {
1593 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001594 rnd = arc4random();
1595 cookie[i] = rnd & 0xff;
1596 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +11001597 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001598
Damien Miller5428f641999-11-25 11:54:57 +11001599 /*
1600 * Send our public key. We include in the packet 64 bits of random
1601 * data that must be matched in the reply in order to prevent IP
1602 * spoofing.
1603 */
Damien Miller95def091999-11-25 00:26:21 +11001604 packet_start(SSH_SMSG_PUBLIC_KEY);
1605 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001606 packet_put_char(cookie[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001607
Damien Miller95def091999-11-25 00:26:21 +11001608 /* Store our public server RSA key. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001609 packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
1610 packet_put_bignum(sensitive_data.server_key->rsa->e);
1611 packet_put_bignum(sensitive_data.server_key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001612
Damien Miller95def091999-11-25 00:26:21 +11001613 /* Store our public host RSA key. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001614 packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1615 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
1616 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001617
Damien Miller95def091999-11-25 00:26:21 +11001618 /* Put protocol flags. */
1619 packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001620
Damien Miller95def091999-11-25 00:26:21 +11001621 /* Declare which ciphers we support. */
Damien Miller874d77b2000-10-14 16:23:11 +11001622 packet_put_int(cipher_mask_ssh1(0));
Damien Miller95def091999-11-25 00:26:21 +11001623
1624 /* Declare supported authentication types. */
1625 auth_mask = 0;
Damien Miller95def091999-11-25 00:26:21 +11001626 if (options.rhosts_rsa_authentication)
1627 auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
1628 if (options.rsa_authentication)
1629 auth_mask |= 1 << SSH_AUTH_RSA;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001630 if (options.challenge_response_authentication == 1)
Damien Miller95def091999-11-25 00:26:21 +11001631 auth_mask |= 1 << SSH_AUTH_TIS;
Damien Miller95def091999-11-25 00:26:21 +11001632 if (options.password_authentication)
1633 auth_mask |= 1 << SSH_AUTH_PASSWORD;
1634 packet_put_int(auth_mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001635
Damien Miller95def091999-11-25 00:26:21 +11001636 /* Send the packet and wait for it to be sent. */
1637 packet_send();
1638 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001639
Damien Miller0bc1bd82000-11-13 22:57:25 +11001640 debug("Sent %d bit server key and %d bit host key.",
1641 BN_num_bits(sensitive_data.server_key->rsa->n),
1642 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001643
Damien Miller95def091999-11-25 00:26:21 +11001644 /* Read clients reply (cipher type and session key). */
Damien Millerdff50992002-01-22 23:16:32 +11001645 packet_read_expect(SSH_CMSG_SESSION_KEY);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001646
Damien Miller50945fa1999-12-09 10:31:37 +11001647 /* Get cipher type and check whether we accept this. */
Damien Miller95def091999-11-25 00:26:21 +11001648 cipher_type = packet_get_char();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001649
Damien Miller874d77b2000-10-14 16:23:11 +11001650 if (!(cipher_mask_ssh1(0) & (1 << cipher_type)))
Damien Miller50945fa1999-12-09 10:31:37 +11001651 packet_disconnect("Warning: client selects unsupported cipher.");
1652
Damien Miller95def091999-11-25 00:26:21 +11001653 /* Get check bytes from the packet. These must match those we
1654 sent earlier with the public key packet. */
1655 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001656 if (cookie[i] != packet_get_char())
Damien Miller95def091999-11-25 00:26:21 +11001657 packet_disconnect("IP Spoofing check bytes do not match.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001658
Damien Miller95def091999-11-25 00:26:21 +11001659 debug("Encryption type: %.200s", cipher_name(cipher_type));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001660
Damien Miller95def091999-11-25 00:26:21 +11001661 /* Get the encrypted integer. */
Damien Millerda755162002-01-22 23:09:22 +11001662 if ((session_key_int = BN_new()) == NULL)
1663 fatal("do_ssh1_kex: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +11001664 packet_get_bignum(session_key_int);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001665
Damien Miller95def091999-11-25 00:26:21 +11001666 protocol_flags = packet_get_int();
1667 packet_set_protocol_flags(protocol_flags);
Damien Miller48b03fc2002-01-22 23:11:40 +11001668 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001669
Ben Lindstromabcb1452002-03-22 01:10:21 +00001670 /* Decrypt session_key_int using host/server keys */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001671 rsafail = PRIVSEP(ssh1_session_key(session_key_int));
1672
Damien Miller5428f641999-11-25 11:54:57 +11001673 /*
1674 * Extract session key from the decrypted integer. The key is in the
1675 * least significant 256 bits of the integer; the first byte of the
1676 * key is in the highest bits.
1677 */
Damien Miller7650bc62001-01-30 09:27:26 +11001678 if (!rsafail) {
1679 BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1680 len = BN_num_bytes(session_key_int);
1681 if (len < 0 || len > sizeof(session_key)) {
1682 error("do_connection: bad session key len from %s: "
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001683 "session_key_int %d > sizeof(session_key) %lu",
1684 get_remote_ipaddr(), len, (u_long)sizeof(session_key));
Damien Miller7650bc62001-01-30 09:27:26 +11001685 rsafail++;
1686 } else {
1687 memset(session_key, 0, sizeof(session_key));
1688 BN_bn2bin(session_key_int,
1689 session_key + sizeof(session_key) - len);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001690
Darren Tuckere14e0052004-05-13 16:30:44 +10001691 derive_ssh1_session_id(
1692 sensitive_data.ssh1_host_key->rsa->n,
1693 sensitive_data.server_key->rsa->n,
1694 cookie, session_id);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001695 /*
1696 * Xor the first 16 bytes of the session key with the
1697 * session id.
1698 */
1699 for (i = 0; i < 16; i++)
1700 session_key[i] ^= session_id[i];
Damien Miller7650bc62001-01-30 09:27:26 +11001701 }
1702 }
1703 if (rsafail) {
Ben Lindstromeb648a72001-03-05 06:00:29 +00001704 int bytes = BN_num_bytes(session_key_int);
Ben Lindstrom13c5d3b2002-02-26 18:00:48 +00001705 u_char *buf = xmalloc(bytes);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001706 MD5_CTX md;
1707
Damien Miller996acd22003-04-09 20:59:48 +10001708 logit("do_connection: generating a fake encryption key");
Ben Lindstromeb648a72001-03-05 06:00:29 +00001709 BN_bn2bin(session_key_int, buf);
1710 MD5_Init(&md);
1711 MD5_Update(&md, buf, bytes);
1712 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1713 MD5_Final(session_key, &md);
1714 MD5_Init(&md);
1715 MD5_Update(&md, session_key, 16);
1716 MD5_Update(&md, buf, bytes);
1717 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1718 MD5_Final(session_key + 16, &md);
1719 memset(buf, 0, bytes);
1720 xfree(buf);
Ben Lindstrom941ac822001-03-05 06:25:23 +00001721 for (i = 0; i < 16; i++)
1722 session_id[i] = session_key[i] ^ session_key[i + 16];
Damien Miller7650bc62001-01-30 09:27:26 +11001723 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001724 /* Destroy the private and public keys. No longer. */
Damien Miller3a5b0232002-03-13 13:19:42 +11001725 destroy_sensitive_data();
Ben Lindstromeb648a72001-03-05 06:00:29 +00001726
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001727 if (use_privsep)
1728 mm_ssh1_session_id(session_id);
1729
Damien Miller396691a2000-01-20 22:44:08 +11001730 /* Destroy the decrypted integer. It is no longer needed. */
1731 BN_clear_free(session_key_int);
1732
Damien Miller95def091999-11-25 00:26:21 +11001733 /* Set the session key. From this on all communications will be encrypted. */
1734 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001735
Damien Miller95def091999-11-25 00:26:21 +11001736 /* Destroy our copy of the session key. It is no longer needed. */
1737 memset(session_key, 0, sizeof(session_key));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001738
Damien Miller95def091999-11-25 00:26:21 +11001739 debug("Received session key; encryption turned on.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001740
Ben Lindstromf666fec2002-06-06 19:51:58 +00001741 /* Send an acknowledgment packet. Note that this packet is sent encrypted. */
Damien Miller95def091999-11-25 00:26:21 +11001742 packet_start(SSH_SMSG_SUCCESS);
1743 packet_send();
1744 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001745}
Damien Millerefb4afe2000-04-12 18:45:05 +10001746
1747/*
1748 * SSH2 key exchange: diffie-hellman-group1-sha1
1749 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001750static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001751do_ssh2_kex(void)
Damien Millerefb4afe2000-04-12 18:45:05 +10001752{
Damien Millerefb4afe2000-04-12 18:45:05 +10001753 Kex *kex;
Damien Millerefb4afe2000-04-12 18:45:05 +10001754
Damien Miller78928792000-04-12 20:17:38 +10001755 if (options.ciphers != NULL) {
Damien Miller4af51302000-04-16 11:18:38 +10001756 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Damien Miller78928792000-04-12 20:17:38 +10001757 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
1758 }
Damien Millera0ff4662001-03-30 10:49:35 +10001759 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1760 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
1761 myproposal[PROPOSAL_ENC_ALGS_STOC] =
1762 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
1763
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001764 if (options.macs != NULL) {
1765 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
1766 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1767 }
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001768 if (!options.compression) {
1769 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1770 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
1771 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001772 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = list_hostkey_types();
1773
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001774 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +00001775 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +11001776 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001777 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Miller8e7fb332003-02-24 12:03:03 +11001778 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00001779 kex->server = 1;
1780 kex->client_version_string=client_version_string;
1781 kex->server_version_string=server_version_string;
1782 kex->load_host_key=&get_hostkey_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001783 kex->host_key_index=&get_hostkey_index;
Damien Millerefb4afe2000-04-12 18:45:05 +10001784
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001785 xxx_kex = kex;
1786
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001787 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +11001788
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001789 session_id2 = kex->session_id;
1790 session_id2_len = kex->session_id_len;
1791
Damien Miller874d77b2000-10-14 16:23:11 +11001792#ifdef DEBUG_KEXDH
1793 /* send 1st encrypted/maced/compressed message */
1794 packet_start(SSH2_MSG_IGNORE);
1795 packet_put_cstring("markus");
1796 packet_send();
1797 packet_write_wait();
1798#endif
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00001799 debug("KEX done");
Damien Millerefb4afe2000-04-12 18:45:05 +10001800}
Darren Tucker3e33cec2003-10-02 16:12:36 +10001801
1802/* server specific fatal cleanup */
1803void
1804cleanup_exit(int i)
1805{
1806 if (the_authctxt)
1807 do_cleanup(the_authctxt);
1808 _exit(i);
1809}