blob: 9fa179933c1307c677148f382a9da59d8bb2eb9d [file] [log] [blame]
Damien Millere7a1e5c2006-08-05 11:34:19 +10001/* $OpenBSD: sshd.c,v 1.341 2006/07/26 13:57:17 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 Miller9aec9192006-08-05 10:57:45 +100053#ifdef HAVE_SYS_TIME_H
54# include <sys/time.h>
55#endif
Damien Miller9cf6d072006-03-15 11:29:24 +110056#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110057
Darren Tucker39972492006-07-12 22:22:46 +100058#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100059#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100060#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110061#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110062#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110063#endif
Damien Millera1738e42006-07-10 21:33:04 +100064#include <grp.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100065#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110066#include <signal.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100067#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100068#include <string.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
Ben Lindstrom226cfa02001-01-22 05:34:40 +000070#include <openssl/dh.h>
71#include <openssl/bn.h>
Damien Miller6a47f302002-02-13 13:55:06 +110072#include <openssl/md5.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000073#include <openssl/rand.h>
Kevin Steves0ea1d9d2002-04-25 18:17:04 +000074#ifdef HAVE_SECUREWARE
75#include <sys/security.h>
76#include <prot.h>
77#endif
Ben Lindstrom226cfa02001-01-22 05:34:40 +000078
79#include "ssh.h"
80#include "ssh1.h"
81#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082#include "xmalloc.h"
83#include "rsa.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000084#include "sshpty.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000086#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087#include "servconf.h"
88#include "uidswap.h"
89#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100090#include "buffer.h"
Darren Tucker645ab752004-06-25 13:33:20 +100091#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "cipher.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100093#include "kex.h"
Damien Millerb38eff82000-04-01 11:09:21 +100094#include "key.h"
Damien Miller874d77b2000-10-14 16:23:11 +110095#include "dh.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100096#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100097#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000098#include "pathnames.h"
99#include "atomicio.h"
100#include "canohost.h"
101#include "auth.h"
102#include "misc.h"
Darren Tucker645ab752004-06-25 13:33:20 +1000103#include "msg.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000104#include "dispatch.h"
Ben Lindstrom1bae4042001-10-03 17:46:39 +0000105#include "channels.h"
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +0000106#include "session.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107#include "monitor_mm.h"
108#include "monitor.h"
109#include "monitor_wrap.h"
110#include "monitor_fdpass.h"
Damien Millerb7576772006-07-10 20:23:39 +1000111#include "version.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112
113#ifdef LIBWRAP
114#include <tcpd.h>
115#include <syslog.h>
116int allow_severity = LOG_INFO;
117int deny_severity = LOG_WARNING;
118#endif /* LIBWRAP */
119
120#ifndef O_NOCTTY
121#define O_NOCTTY 0
122#endif
123
Damien Miller035a5b42004-06-26 08:16:31 +1000124/* Re-exec fds */
125#define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1)
126#define REEXEC_STARTUP_PIPE_FD (STDERR_FILENO + 2)
127#define REEXEC_CONFIG_PASS_FD (STDERR_FILENO + 3)
128#define REEXEC_MIN_FREE_FD (STDERR_FILENO + 4)
129
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000130extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000131
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132/* Server configuration options. */
133ServerOptions options;
134
135/* Name of the server configuration file. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000136char *config_file_name = _PATH_SERVER_CONFIG_FILE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137
Damien Miller4af51302000-04-16 11:18:38 +1000138/*
Damien Miller95def091999-11-25 00:26:21 +1100139 * Debug mode flag. This can be set on the command line. If debug
140 * mode is enabled, extra debugging output will be sent to the system
141 * log, the daemon will not go to background, and will exit after processing
142 * the first connection.
143 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144int debug_flag = 0;
145
Ben Lindstrom794325a2001-08-06 21:09:07 +0000146/* Flag indicating that the daemon should only test the configuration and keys. */
147int test_flag = 0;
148
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149/* Flag indicating that the daemon is being started from inetd. */
150int inetd_flag = 0;
151
Ben Lindstromc72745a2000-12-02 19:03:54 +0000152/* Flag indicating that sshd should not detach and become a daemon. */
153int no_daemon_flag = 0;
154
Damien Miller5ce662a1999-11-11 17:57:39 +1100155/* debug goes to stderr unless inetd_flag is set */
156int log_stderr = 0;
157
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158/* Saved arguments to main(). */
159char **saved_argv;
Damien Millerb8c656e2000-06-28 15:22:41 +1000160int saved_argc;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161
Darren Tucker645ab752004-06-25 13:33:20 +1000162/* re-exec */
163int rexeced_flag = 0;
164int rexec_flag = 1;
165int rexec_argc = 0;
166char **rexec_argv;
167
Damien Miller5428f641999-11-25 11:54:57 +1100168/*
Damien Miller34132e52000-01-14 15:45:46 +1100169 * The sockets that the server is listening; this is used in the SIGHUP
170 * signal handler.
Damien Miller5428f641999-11-25 11:54:57 +1100171 */
Damien Miller34132e52000-01-14 15:45:46 +1100172#define MAX_LISTEN_SOCKS 16
173int listen_socks[MAX_LISTEN_SOCKS];
174int num_listen_socks = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175
Damien Miller5428f641999-11-25 11:54:57 +1100176/*
177 * the client's version string, passed by sshd2 in compat mode. if != NULL,
178 * sshd will skip the version-number exchange
179 */
Damien Miller95def091999-11-25 00:26:21 +1100180char *client_version_string = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000181char *server_version_string = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000183/* for rekeying XXX fixme */
184Kex *xxx_kex;
185
Damien Miller5428f641999-11-25 11:54:57 +1100186/*
187 * Any really sensitive data in the application is contained in this
188 * structure. The idea is that this structure could be locked into memory so
189 * that the pages do not get written into swap. However, there are some
190 * problems. The private key contains BIGNUMs, and we do not (in principle)
191 * have access to the internals of them, and locking just the structure is
192 * not very useful. Currently, memory locking is not implemented.
193 */
Damien Miller95def091999-11-25 00:26:21 +1100194struct {
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000195 Key *server_key; /* ephemeral server key */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100196 Key *ssh1_host_key; /* ssh1 host key */
197 Key **host_keys; /* all private host keys */
198 int have_ssh1_key;
199 int have_ssh2_key;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000200 u_char ssh1_cookie[SSH_SESSION_KEY_LENGTH];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201} sensitive_data;
202
Damien Miller5428f641999-11-25 11:54:57 +1100203/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000204 * Flag indicating whether the RSA server key needs to be regenerated.
205 * Is set in the SIGALRM handler and cleared when the key is regenerated.
Damien Miller5428f641999-11-25 11:54:57 +1100206 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000207static volatile sig_atomic_t key_do_regen = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000209/* This is set to true when a signal is received. */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000210static volatile sig_atomic_t received_sighup = 0;
211static volatile sig_atomic_t received_sigterm = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212
Damien Millerb38eff82000-04-01 11:09:21 +1000213/* session identifier, used by RSA-auth */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000214u_char session_id[16];
Damien Millerb38eff82000-04-01 11:09:21 +1000215
Damien Millereba71ba2000-04-29 23:57:08 +1000216/* same for ssh2 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000217u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000218u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000219
Damien Miller942da032000-08-18 13:59:06 +1000220/* record remote hostname or ip */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000221u_int utmp_len = MAXHOSTNAMELEN;
Damien Miller942da032000-08-18 13:59:06 +1000222
Ben Lindstromd84df982001-12-06 16:35:40 +0000223/* options.max_startup sized array of fd ints */
224int *startup_pipes = NULL;
225int startup_pipe; /* in child */
226
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000227/* variables used for privilege separation */
Darren Tucker45150472006-07-12 22:34:17 +1000228int use_privsep = -1;
Darren Tuckera8be9e22004-02-06 16:40:27 +1100229struct monitor *pmonitor = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000230
Darren Tucker3e33cec2003-10-02 16:12:36 +1000231/* global authentication context */
232Authctxt *the_authctxt = NULL;
233
Darren Tucker45150472006-07-12 22:34:17 +1000234/* sshd_config buffer */
235Buffer cfg;
236
Darren Tucker09991742004-07-17 17:05:14 +1000237/* message to be displayed after login */
238Buffer loginmsg;
239
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240/* Prototypes for various functions defined later in this file. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000241void destroy_sensitive_data(void);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000242void demote_sensitive_data(void);
Damien Miller98c7ad62000-03-09 21:27:49 +1100243
Ben Lindstrombba81212001-06-25 05:01:22 +0000244static void do_ssh1_kex(void);
245static void do_ssh2_kex(void);
Damien Miller874d77b2000-10-14 16:23:11 +1100246
Damien Miller98c7ad62000-03-09 21:27:49 +1100247/*
Damien Miller34132e52000-01-14 15:45:46 +1100248 * Close all listening sockets
249 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000250static void
Damien Miller34132e52000-01-14 15:45:46 +1100251close_listen_socks(void)
252{
253 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000254
Damien Miller34132e52000-01-14 15:45:46 +1100255 for (i = 0; i < num_listen_socks; i++)
256 close(listen_socks[i]);
257 num_listen_socks = -1;
258}
259
Ben Lindstromd84df982001-12-06 16:35:40 +0000260static void
261close_startup_pipes(void)
262{
263 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000264
Ben Lindstromd84df982001-12-06 16:35:40 +0000265 if (startup_pipes)
266 for (i = 0; i < options.max_startups; i++)
267 if (startup_pipes[i] != -1)
268 close(startup_pipes[i]);
269}
270
Damien Miller34132e52000-01-14 15:45:46 +1100271/*
Damien Miller95def091999-11-25 00:26:21 +1100272 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
273 * the effect is to reread the configuration file (and to regenerate
274 * the server key).
275 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100276
277/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000278static void
Damien Miller95def091999-11-25 00:26:21 +1100279sighup_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280{
Ben Lindstrom07958482001-12-06 16:19:01 +0000281 int save_errno = errno;
282
Damien Miller95def091999-11-25 00:26:21 +1100283 received_sighup = 1;
284 signal(SIGHUP, sighup_handler);
Ben Lindstrom07958482001-12-06 16:19:01 +0000285 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286}
287
Damien Miller95def091999-11-25 00:26:21 +1100288/*
289 * Called from the main program after receiving SIGHUP.
290 * Restarts the server.
291 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000292static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000293sighup_restart(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294{
Damien Miller996acd22003-04-09 20:59:48 +1000295 logit("Received SIGHUP; restarting.");
Damien Miller34132e52000-01-14 15:45:46 +1100296 close_listen_socks();
Ben Lindstromd84df982001-12-06 16:35:40 +0000297 close_startup_pipes();
Damien Miller95def091999-11-25 00:26:21 +1100298 execv(saved_argv[0], saved_argv);
Damien Miller996acd22003-04-09 20:59:48 +1000299 logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
Ben Lindstrom822b6342002-06-23 21:38:49 +0000300 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100301 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302}
303
Damien Miller95def091999-11-25 00:26:21 +1100304/*
305 * Generic signal handler for terminating signals in the master daemon.
Damien Miller95def091999-11-25 00:26:21 +1100306 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100307/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000308static void
Damien Miller95def091999-11-25 00:26:21 +1100309sigterm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000311 received_sigterm = sig;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312}
313
Damien Miller95def091999-11-25 00:26:21 +1100314/*
315 * SIGCHLD handler. This is called whenever a child dies. This will then
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000316 * reap any zombies left by exited children.
Damien Miller95def091999-11-25 00:26:21 +1100317 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100318/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000319static void
Damien Miller95def091999-11-25 00:26:21 +1100320main_sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321{
Damien Miller95def091999-11-25 00:26:21 +1100322 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000323 pid_t pid;
Damien Miller95def091999-11-25 00:26:21 +1100324 int status;
Damien Miller431f66b1999-11-21 18:31:57 +1100325
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000326 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
327 (pid < 0 && errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100328 ;
Damien Miller431f66b1999-11-21 18:31:57 +1100329
Damien Miller95def091999-11-25 00:26:21 +1100330 signal(SIGCHLD, main_sigchld_handler);
331 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332}
333
Damien Miller95def091999-11-25 00:26:21 +1100334/*
335 * Signal handler for the alarm after the login grace period has expired.
336 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100337/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000338static void
Damien Miller95def091999-11-25 00:26:21 +1100339grace_alarm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000341 /* XXX no idea how fix this signal handler */
342
Darren Tuckera8be9e22004-02-06 16:40:27 +1100343 if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0)
344 kill(pmonitor->m_pid, SIGALRM);
345
Damien Miller95def091999-11-25 00:26:21 +1100346 /* Log error and exit. */
Damien Millerd27a76d2002-09-27 13:22:31 +1000347 fatal("Timeout before authentication for %s", get_remote_ipaddr());
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348}
349
Damien Miller95def091999-11-25 00:26:21 +1100350/*
Damien Miller95def091999-11-25 00:26:21 +1100351 * Signal handler for the key regeneration alarm. Note that this
352 * alarm only occurs in the daemon waiting for connections, and it does not
353 * do anything with the private key or random state before forking.
354 * Thus there should be no concurrency control/asynchronous execution
355 * problems.
356 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000357static void
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000358generate_ephemeral_server_key(void)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100359{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000360 u_int32_t rnd = 0;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000361 int i;
362
Ben Lindstroma3700052001-04-05 23:26:32 +0000363 verbose("Generating %s%d bit RSA key.",
Damien Millerff75ac42001-03-30 10:50:32 +1000364 sensitive_data.server_key ? "new " : "", options.server_key_bits);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100365 if (sensitive_data.server_key != NULL)
366 key_free(sensitive_data.server_key);
Ben Lindstroma3700052001-04-05 23:26:32 +0000367 sensitive_data.server_key = key_generate(KEY_RSA1,
Damien Millerff75ac42001-03-30 10:50:32 +1000368 options.server_key_bits);
369 verbose("RSA key generation complete.");
Ben Lindstromeb648a72001-03-05 06:00:29 +0000370
371 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
372 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000373 rnd = arc4random();
374 sensitive_data.ssh1_cookie[i] = rnd & 0xff;
375 rnd >>= 8;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000376 }
377 arc4random_stir();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100378}
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000379
Damien Millerf0b15df2006-03-26 13:59:20 +1100380/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000381static void
Damien Miller95def091999-11-25 00:26:21 +1100382key_regeneration_alarm(int sig)
383{
384 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000385
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000386 signal(SIGALRM, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +1100387 errno = save_errno;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000388 key_do_regen = 1;
Damien Miller95def091999-11-25 00:26:21 +1100389}
390
Ben Lindstrombba81212001-06-25 05:01:22 +0000391static void
Damien Millerb38eff82000-04-01 11:09:21 +1000392sshd_exchange_identification(int sock_in, int sock_out)
393{
Damien Millereccb9de2005-06-17 12:59:34 +1000394 u_int i;
395 int mismatch;
Damien Millerb38eff82000-04-01 11:09:21 +1000396 int remote_major, remote_minor;
Damien Miller78928792000-04-12 20:17:38 +1000397 int major, minor;
Damien Millerb38eff82000-04-01 11:09:21 +1000398 char *s;
399 char buf[256]; /* Must not be larger than remote_version. */
400 char remote_version[256]; /* Must be at least as big as buf. */
401
Damien Miller78928792000-04-12 20:17:38 +1000402 if ((options.protocol & SSH_PROTO_1) &&
403 (options.protocol & SSH_PROTO_2)) {
404 major = PROTOCOL_MAJOR_1;
405 minor = 99;
406 } else if (options.protocol & SSH_PROTO_2) {
407 major = PROTOCOL_MAJOR_2;
408 minor = PROTOCOL_MINOR_2;
409 } else {
410 major = PROTOCOL_MAJOR_1;
411 minor = PROTOCOL_MINOR_1;
412 }
413 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
Damien Millerb38eff82000-04-01 11:09:21 +1000414 server_version_string = xstrdup(buf);
415
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000416 /* Send our protocol version identification. */
417 if (atomicio(vwrite, sock_out, server_version_string,
418 strlen(server_version_string))
419 != strlen(server_version_string)) {
420 logit("Could not write ident string to %s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000421 cleanup_exit(255);
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000422 }
423
424 /* Read other sides version identification. */
425 memset(buf, 0, sizeof(buf));
426 for (i = 0; i < sizeof(buf) - 1; i++) {
427 if (atomicio(read, sock_in, &buf[i], 1) != 1) {
428 logit("Did not receive identification string from %s",
429 get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000430 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000431 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000432 if (buf[i] == '\r') {
433 buf[i] = 0;
434 /* Kludge for F-Secure Macintosh < 1.0.2 */
435 if (i == 12 &&
436 strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000437 break;
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000438 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000439 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000440 if (buf[i] == '\n') {
441 buf[i] = 0;
442 break;
443 }
Damien Millerb38eff82000-04-01 11:09:21 +1000444 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000445 buf[sizeof(buf) - 1] = 0;
446 client_version_string = xstrdup(buf);
Damien Millerb38eff82000-04-01 11:09:21 +1000447
448 /*
449 * Check that the versions match. In future this might accept
450 * several versions and set appropriate flags to handle them.
451 */
452 if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
453 &remote_major, &remote_minor, remote_version) != 3) {
Damien Miller4af51302000-04-16 11:18:38 +1000454 s = "Protocol mismatch.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000455 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Millerb38eff82000-04-01 11:09:21 +1000456 close(sock_in);
457 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000458 logit("Bad protocol version identification '%.100s' from %s",
Damien Millerb38eff82000-04-01 11:09:21 +1000459 client_version_string, get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000460 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000461 }
462 debug("Client protocol version %d.%d; client software version %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100463 remote_major, remote_minor, remote_version);
Damien Millerb38eff82000-04-01 11:09:21 +1000464
Damien Millerefb4afe2000-04-12 18:45:05 +1000465 compat_datafellows(remote_version);
466
Damien Millere9264972002-09-30 11:59:21 +1000467 if (datafellows & SSH_BUG_PROBE) {
Damien Miller996acd22003-04-09 20:59:48 +1000468 logit("probed from %s with %s. Don't panic.",
Damien Millere9264972002-09-30 11:59:21 +1000469 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000470 cleanup_exit(255);
Damien Millere9264972002-09-30 11:59:21 +1000471 }
472
Damien Miller27dbe6f2001-03-19 22:36:20 +1100473 if (datafellows & SSH_BUG_SCANNER) {
Damien Miller996acd22003-04-09 20:59:48 +1000474 logit("scanned from %s with %s. Don't panic.",
Damien Miller27dbe6f2001-03-19 22:36:20 +1100475 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000476 cleanup_exit(255);
Damien Miller27dbe6f2001-03-19 22:36:20 +1100477 }
478
Damien Miller78928792000-04-12 20:17:38 +1000479 mismatch = 0;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000480 switch (remote_major) {
Damien Millerb38eff82000-04-01 11:09:21 +1000481 case 1:
Damien Millereba71ba2000-04-29 23:57:08 +1000482 if (remote_minor == 99) {
483 if (options.protocol & SSH_PROTO_2)
484 enable_compat20();
485 else
486 mismatch = 1;
487 break;
488 }
Damien Miller78928792000-04-12 20:17:38 +1000489 if (!(options.protocol & SSH_PROTO_1)) {
490 mismatch = 1;
491 break;
492 }
Damien Millerb38eff82000-04-01 11:09:21 +1000493 if (remote_minor < 3) {
Damien Miller37023962000-07-11 17:31:38 +1000494 packet_disconnect("Your ssh version is too old and "
Damien Millerb38eff82000-04-01 11:09:21 +1000495 "is no longer supported. Please install a newer version.");
496 } else if (remote_minor == 3) {
497 /* note that this disables agent-forwarding */
498 enable_compat13();
499 }
Damien Miller78928792000-04-12 20:17:38 +1000500 break;
Damien Millerefb4afe2000-04-12 18:45:05 +1000501 case 2:
Damien Miller78928792000-04-12 20:17:38 +1000502 if (options.protocol & SSH_PROTO_2) {
Damien Millerefb4afe2000-04-12 18:45:05 +1000503 enable_compat20();
504 break;
505 }
506 /* FALLTHROUGH */
Damien Miller4af51302000-04-16 11:18:38 +1000507 default:
Damien Miller78928792000-04-12 20:17:38 +1000508 mismatch = 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000509 break;
510 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000511 chop(server_version_string);
Damien Miller78928792000-04-12 20:17:38 +1000512 debug("Local version string %.200s", server_version_string);
513
514 if (mismatch) {
515 s = "Protocol major versions differ.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000516 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Miller78928792000-04-12 20:17:38 +1000517 close(sock_in);
518 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000519 logit("Protocol major versions differ for %s: %.200s vs. %.200s",
Damien Miller78928792000-04-12 20:17:38 +1000520 get_remote_ipaddr(),
521 server_version_string, client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000522 cleanup_exit(255);
Damien Miller78928792000-04-12 20:17:38 +1000523 }
Damien Millereba71ba2000-04-29 23:57:08 +1000524}
525
Damien Miller0bc1bd82000-11-13 22:57:25 +1100526/* Destroy the host and server keys. They will no longer be needed. */
Damien Millereba71ba2000-04-29 23:57:08 +1000527void
528destroy_sensitive_data(void)
529{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100530 int i;
531
532 if (sensitive_data.server_key) {
533 key_free(sensitive_data.server_key);
534 sensitive_data.server_key = NULL;
535 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100536 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100537 if (sensitive_data.host_keys[i]) {
538 key_free(sensitive_data.host_keys[i]);
539 sensitive_data.host_keys[i] = NULL;
540 }
541 }
542 sensitive_data.ssh1_host_key = NULL;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000543 memset(sensitive_data.ssh1_cookie, 0, SSH_SESSION_KEY_LENGTH);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100544}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100545
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000546/* Demote private to public keys for network child */
547void
548demote_sensitive_data(void)
549{
550 Key *tmp;
551 int i;
552
553 if (sensitive_data.server_key) {
554 tmp = key_demote(sensitive_data.server_key);
555 key_free(sensitive_data.server_key);
556 sensitive_data.server_key = tmp;
557 }
558
559 for (i = 0; i < options.num_host_key_files; i++) {
560 if (sensitive_data.host_keys[i]) {
561 tmp = key_demote(sensitive_data.host_keys[i]);
562 key_free(sensitive_data.host_keys[i]);
563 sensitive_data.host_keys[i] = tmp;
564 if (tmp->type == KEY_RSA1)
565 sensitive_data.ssh1_host_key = tmp;
566 }
567 }
568
569 /* We do not clear ssh1_host key and cookie. XXX - Okay Niels? */
570}
571
Ben Lindstrom08105192002-03-22 02:50:06 +0000572static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000573privsep_preauth_child(void)
574{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000575 u_int32_t rnd[256];
Ben Lindstrom810af962002-07-04 00:11:40 +0000576 gid_t gidset[1];
Ben Lindstromc7431342002-03-22 03:11:49 +0000577 struct passwd *pw;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000578 int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000579
580 /* Enable challenge-response authentication for privilege separation */
581 privsep_challenge_enable();
582
583 for (i = 0; i < 256; i++)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000584 rnd[i] = arc4random();
585 RAND_seed(rnd, sizeof(rnd));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586
587 /* Demote the private keys to public keys. */
588 demote_sensitive_data();
589
Ben Lindstromc7431342002-03-22 03:11:49 +0000590 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
Damien Miller0150c652002-04-24 09:49:09 +1000591 fatal("Privilege separation user %s does not exist",
592 SSH_PRIVSEP_USER);
Ben Lindstromc7431342002-03-22 03:11:49 +0000593 memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
594 endpwent();
595
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000596 /* Change our root directory */
Ben Lindstrom7a7edf72002-03-22 02:42:37 +0000597 if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
598 fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
599 strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000600 if (chdir("/") == -1)
Ben Lindstrom1ee9ec32002-03-22 03:14:45 +0000601 fatal("chdir(\"/\"): %s", strerror(errno));
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000602
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000603 /* Drop our privileges */
Ben Lindstromc7431342002-03-22 03:11:49 +0000604 debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
605 (u_int)pw->pw_gid);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000606#if 0
Darren Tuckerd5920482004-02-29 20:11:30 +1100607 /* XXX not ready, too heavy after chroot */
Ben Lindstromc7431342002-03-22 03:11:49 +0000608 do_setusercontext(pw);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000609#else
610 gidset[0] = pw->pw_gid;
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000611 if (setgroups(1, gidset) < 0)
612 fatal("setgroups: %.100s", strerror(errno));
613 permanently_set_uid(pw);
614#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000615}
616
Darren Tucker3e33cec2003-10-02 16:12:36 +1000617static int
618privsep_preauth(Authctxt *authctxt)
Ben Lindstrom943481c2002-03-22 03:43:46 +0000619{
Ben Lindstrom943481c2002-03-22 03:43:46 +0000620 int status;
621 pid_t pid;
622
623 /* Set up unprivileged child process to deal with network data */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000624 pmonitor = monitor_init();
Ben Lindstrom943481c2002-03-22 03:43:46 +0000625 /* Store a pointer to the kex for later rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000626 pmonitor->m_pkex = &xxx_kex;
Ben Lindstrom943481c2002-03-22 03:43:46 +0000627
628 pid = fork();
629 if (pid == -1) {
630 fatal("fork of unprivileged child failed");
631 } else if (pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000632 debug2("Network child is on pid %ld", (long)pid);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000633
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000634 close(pmonitor->m_recvfd);
Darren Tuckera8be9e22004-02-06 16:40:27 +1100635 pmonitor->m_pid = pid;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000636 monitor_child_preauth(authctxt, pmonitor);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000637 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000638
639 /* Sync memory */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000640 monitor_sync(pmonitor);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000641
642 /* Wait for the child's exit status */
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000643 while (waitpid(pid, &status, 0) < 0)
644 if (errno != EINTR)
645 break;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000646 return (1);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000647 } else {
648 /* child */
649
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000650 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000651
652 /* Demote the child */
653 if (getuid() == 0 || geteuid() == 0)
654 privsep_preauth_child();
Ben Lindstromf90f58d2002-03-26 01:53:03 +0000655 setproctitle("%s", "[net]");
Ben Lindstrom943481c2002-03-22 03:43:46 +0000656 }
Darren Tucker3e33cec2003-10-02 16:12:36 +1000657 return (0);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000658}
659
Ben Lindstrom08105192002-03-22 02:50:06 +0000660static void
Ben Lindstrom943481c2002-03-22 03:43:46 +0000661privsep_postauth(Authctxt *authctxt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000662{
Tim Rice9dd30812002-07-07 13:43:36 -0700663#ifdef DISABLE_FD_PASSING
Tim Rice8eff3192002-06-25 15:35:15 -0700664 if (1) {
665#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000666 if (authctxt->pw->pw_uid == 0 || options.use_login) {
Tim Rice8eff3192002-06-25 15:35:15 -0700667#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000668 /* File descriptor passing is broken or root login */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669 use_privsep = 0;
Darren Tucker45b01422005-10-03 18:20:00 +1000670 goto skip;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000671 }
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000672
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000673 /* New socket pair */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000674 monitor_reinit(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000675
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000676 pmonitor->m_pid = fork();
677 if (pmonitor->m_pid == -1)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000678 fatal("fork of unprivileged child failed");
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000679 else if (pmonitor->m_pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000680 debug2("User child is on pid %ld", (long)pmonitor->m_pid);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000681 close(pmonitor->m_recvfd);
Darren Tuckereb578622004-08-12 23:08:14 +1000682 buffer_clear(&loginmsg);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000683 monitor_child_postauth(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000684
685 /* NEVERREACHED */
686 exit(0);
687 }
688
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000689 close(pmonitor->m_sendfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000690
691 /* Demote the private keys to public keys. */
692 demote_sensitive_data();
693
694 /* Drop privileges */
695 do_setusercontext(authctxt->pw);
696
Darren Tucker45b01422005-10-03 18:20:00 +1000697 skip:
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000698 /* It is safe now to apply the key state */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000699 monitor_apply_keystate(pmonitor);
Damien Miller9786e6e2005-07-26 21:54:56 +1000700
701 /*
702 * Tell the packet layer that authentication was successful, since
703 * this information is not part of the key state.
704 */
705 packet_set_authenticated();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000706}
707
Ben Lindstrombba81212001-06-25 05:01:22 +0000708static char *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100709list_hostkey_types(void)
710{
Damien Miller0e3b8722002-01-22 23:26:38 +1100711 Buffer b;
Damien Millerf58b58c2003-11-17 21:18:23 +1100712 const char *p;
713 char *ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100714 int i;
Damien Miller0e3b8722002-01-22 23:26:38 +1100715
716 buffer_init(&b);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100717 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100718 Key *key = sensitive_data.host_keys[i];
719 if (key == NULL)
720 continue;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000721 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100722 case KEY_RSA:
723 case KEY_DSA:
Damien Miller0e3b8722002-01-22 23:26:38 +1100724 if (buffer_len(&b) > 0)
725 buffer_append(&b, ",", 1);
726 p = key_ssh_name(key);
727 buffer_append(&b, p, strlen(p));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100728 break;
729 }
730 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100731 buffer_append(&b, "\0", 1);
Damien Millerf58b58c2003-11-17 21:18:23 +1100732 ret = xstrdup(buffer_ptr(&b));
Damien Miller0e3b8722002-01-22 23:26:38 +1100733 buffer_free(&b);
Damien Millerf58b58c2003-11-17 21:18:23 +1100734 debug("list_hostkey_types: %s", ret);
735 return ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100736}
737
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000738Key *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100739get_hostkey_by_type(int type)
740{
741 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000742
Damien Miller9f0f5c62001-12-21 14:45:46 +1100743 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100744 Key *key = sensitive_data.host_keys[i];
745 if (key != NULL && key->type == type)
746 return key;
747 }
748 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000749}
750
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000751Key *
752get_hostkey_by_index(int ind)
753{
754 if (ind < 0 || ind >= options.num_host_key_files)
755 return (NULL);
756 return (sensitive_data.host_keys[ind]);
757}
758
759int
760get_hostkey_index(Key *key)
761{
762 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000763
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000764 for (i = 0; i < options.num_host_key_files; i++) {
765 if (key == sensitive_data.host_keys[i])
766 return (i);
767 }
768 return (-1);
769}
770
Damien Miller942da032000-08-18 13:59:06 +1000771/*
772 * returns 1 if connection should be dropped, 0 otherwise.
773 * dropping starts at connection #max_startups_begin with a probability
774 * of (max_startups_rate/100). the probability increases linearly until
775 * all connections are dropped for startups > max_startups
776 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000777static int
Damien Miller942da032000-08-18 13:59:06 +1000778drop_connection(int startups)
779{
Darren Tucker178fa662004-11-05 20:09:09 +1100780 int p, r;
Damien Miller942da032000-08-18 13:59:06 +1000781
782 if (startups < options.max_startups_begin)
783 return 0;
784 if (startups >= options.max_startups)
785 return 1;
786 if (options.max_startups_rate == 100)
787 return 1;
788
789 p = 100 - options.max_startups_rate;
790 p *= startups - options.max_startups_begin;
Darren Tucker178fa662004-11-05 20:09:09 +1100791 p /= options.max_startups - options.max_startups_begin;
Damien Miller942da032000-08-18 13:59:06 +1000792 p += options.max_startups_rate;
Darren Tucker178fa662004-11-05 20:09:09 +1100793 r = arc4random() % 100;
Damien Miller942da032000-08-18 13:59:06 +1000794
Darren Tucker3269b132004-11-05 20:20:59 +1100795 debug("drop_connection: p %d, r %d", p, r);
Damien Miller942da032000-08-18 13:59:06 +1000796 return (r < p) ? 1 : 0;
797}
798
Ben Lindstromade03f62001-12-06 18:22:17 +0000799static void
800usage(void)
801{
Damien Miller0c889cd2004-03-22 09:36:00 +1100802 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000803 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Millerb4087862004-03-22 09:35:21 +1100804 fprintf(stderr,
805"usage: sshd [-46Ddeiqt] [-b bits] [-f config_file] [-g login_grace_time]\n"
806" [-h host_key_file] [-k key_gen_time] [-o option] [-p port] [-u len]\n"
807 );
Ben Lindstromade03f62001-12-06 18:22:17 +0000808 exit(1);
809}
810
Darren Tucker645ab752004-06-25 13:33:20 +1000811static void
812send_rexec_state(int fd, Buffer *conf)
813{
814 Buffer m;
815
816 debug3("%s: entering fd = %d config len %d", __func__, fd,
817 buffer_len(conf));
818
819 /*
820 * Protocol from reexec master to child:
821 * string configuration
822 * u_int ephemeral_key_follows
823 * bignum e (only if ephemeral_key_follows == 1)
824 * bignum n "
825 * bignum d "
826 * bignum iqmp "
827 * bignum p "
828 * bignum q "
Darren Tuckerc6f82192005-09-27 22:46:32 +1000829 * string rngseed (only if OpenSSL is not self-seeded)
Darren Tucker645ab752004-06-25 13:33:20 +1000830 */
831 buffer_init(&m);
832 buffer_put_cstring(&m, buffer_ptr(conf));
833
Darren Tuckerfc959702004-07-17 16:12:08 +1000834 if (sensitive_data.server_key != NULL &&
Darren Tucker645ab752004-06-25 13:33:20 +1000835 sensitive_data.server_key->type == KEY_RSA1) {
836 buffer_put_int(&m, 1);
837 buffer_put_bignum(&m, sensitive_data.server_key->rsa->e);
838 buffer_put_bignum(&m, sensitive_data.server_key->rsa->n);
839 buffer_put_bignum(&m, sensitive_data.server_key->rsa->d);
840 buffer_put_bignum(&m, sensitive_data.server_key->rsa->iqmp);
841 buffer_put_bignum(&m, sensitive_data.server_key->rsa->p);
842 buffer_put_bignum(&m, sensitive_data.server_key->rsa->q);
843 } else
844 buffer_put_int(&m, 0);
845
Darren Tuckerc6f82192005-09-27 22:46:32 +1000846#ifndef OPENSSL_PRNG_ONLY
847 rexec_send_rng_seed(&m);
848#endif
849
Darren Tucker645ab752004-06-25 13:33:20 +1000850 if (ssh_msg_send(fd, 0, &m) == -1)
851 fatal("%s: ssh_msg_send failed", __func__);
852
853 buffer_free(&m);
854
855 debug3("%s: done", __func__);
856}
857
858static void
859recv_rexec_state(int fd, Buffer *conf)
860{
861 Buffer m;
862 char *cp;
863 u_int len;
864
865 debug3("%s: entering fd = %d", __func__, fd);
866
867 buffer_init(&m);
868
869 if (ssh_msg_recv(fd, &m) == -1)
870 fatal("%s: ssh_msg_recv failed", __func__);
871 if (buffer_get_char(&m) != 0)
872 fatal("%s: rexec version mismatch", __func__);
873
874 cp = buffer_get_string(&m, &len);
875 if (conf != NULL)
876 buffer_append(conf, cp, len + 1);
877 xfree(cp);
878
879 if (buffer_get_int(&m)) {
880 if (sensitive_data.server_key != NULL)
881 key_free(sensitive_data.server_key);
882 sensitive_data.server_key = key_new_private(KEY_RSA1);
883 buffer_get_bignum(&m, sensitive_data.server_key->rsa->e);
884 buffer_get_bignum(&m, sensitive_data.server_key->rsa->n);
885 buffer_get_bignum(&m, sensitive_data.server_key->rsa->d);
886 buffer_get_bignum(&m, sensitive_data.server_key->rsa->iqmp);
887 buffer_get_bignum(&m, sensitive_data.server_key->rsa->p);
888 buffer_get_bignum(&m, sensitive_data.server_key->rsa->q);
889 rsa_generate_additional_parameters(
890 sensitive_data.server_key->rsa);
891 }
Darren Tuckerc6f82192005-09-27 22:46:32 +1000892
893#ifndef OPENSSL_PRNG_ONLY
894 rexec_recv_rng_seed(&m);
895#endif
896
Darren Tucker645ab752004-06-25 13:33:20 +1000897 buffer_free(&m);
898
899 debug3("%s: done", __func__);
900}
901
Damien Miller95def091999-11-25 00:26:21 +1100902/*
903 * Main program for the daemon.
904 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000905int
906main(int ac, char **av)
907{
Damien Miller95def091999-11-25 00:26:21 +1100908 extern char *optarg;
909 extern int optind;
Damien Miller07d86be2006-03-26 14:19:21 +1100910 int opt, j, i, on = 1;
Damien Miller386c6a22004-06-30 22:40:20 +1000911 int sock_in = -1, sock_out = -1, newsock = -1;
Damien Miller166fca82000-04-20 07:42:21 +1000912 pid_t pid;
Damien Miller34132e52000-01-14 15:45:46 +1100913 socklen_t fromlen;
Damien Miller34132e52000-01-14 15:45:46 +1100914 fd_set *fdset;
915 struct sockaddr_storage from;
Damien Miller95def091999-11-25 00:26:21 +1100916 const char *remote_ip;
917 int remote_port;
Damien Miller95def091999-11-25 00:26:21 +1100918 FILE *f;
Damien Miller34132e52000-01-14 15:45:46 +1100919 struct addrinfo *ai;
920 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerb9997192003-12-17 16:29:22 +1100921 char *line;
Damien Miller34132e52000-01-14 15:45:46 +1100922 int listen_sock, maxfd;
Darren Tucker0f56ed12004-08-29 16:38:41 +1000923 int startup_p[2] = { -1 , -1 }, config_s[2] = { -1 , -1 };
Damien Miller37023962000-07-11 17:31:38 +1000924 int startups = 0;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000925 Key *key;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000926 Authctxt *authctxt;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000927 int ret, key_used = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000928
Kevin Steves0ea1d9d2002-04-25 18:17:04 +0000929#ifdef HAVE_SECUREWARE
930 (void)set_auth_parameters(ac, av);
931#endif
Damien Miller59d3d5b2003-08-22 09:34:41 +1000932 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000933 init_rng();
934
Damien Millera8ed44b2003-01-10 09:53:12 +1100935 /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
Damien Millerb8c656e2000-06-28 15:22:41 +1000936 saved_argc = ac;
Darren Tucker17c5d032004-06-25 14:22:23 +1000937 rexec_argc = ac;
Darren Tuckerd8093e42006-05-04 16:24:34 +1000938 saved_argv = xcalloc(ac + 1, sizeof(*saved_argv));
Damien Millera8ed44b2003-01-10 09:53:12 +1100939 for (i = 0; i < ac; i++)
940 saved_argv[i] = xstrdup(av[i]);
Damien Miller04cb5362003-05-15 21:29:10 +1000941 saved_argv[i] = NULL;
Damien Millera8ed44b2003-01-10 09:53:12 +1100942
943#ifndef HAVE_SETPROCTITLE
944 /* Prepare for later setproctitle emulation */
945 compat_init_setproctitle(ac, av);
Damien Millerf2e3e9d2003-06-02 12:15:54 +1000946 av = saved_argv;
Damien Millera8ed44b2003-01-10 09:53:12 +1100947#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948
Damien Millerbfba3542004-03-22 09:29:57 +1100949 if (geteuid() == 0 && setgroups(0, NULL) == -1)
950 debug("setgroups(): %.200s", strerror(errno));
951
Darren Tuckerce321d82005-10-03 18:11:24 +1000952 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
953 sanitise_stdfd();
954
Damien Miller95def091999-11-25 00:26:21 +1100955 /* Initialize configuration options to their default values. */
956 initialize_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957
Damien Miller95def091999-11-25 00:26:21 +1100958 /* Parse command-line arguments. */
Darren Tucker645ab752004-06-25 13:33:20 +1000959 while ((opt = getopt(ac, av, "f:p:b:k:h:g:u:o:dDeiqrtQR46")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100960 switch (opt) {
Damien Miller34132e52000-01-14 15:45:46 +1100961 case '4':
Darren Tucker0f383232005-01-20 10:57:56 +1100962 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100963 break;
964 case '6':
Darren Tucker0f383232005-01-20 10:57:56 +1100965 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100966 break;
Damien Miller95def091999-11-25 00:26:21 +1100967 case 'f':
968 config_file_name = optarg;
969 break;
970 case 'd':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000971 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100972 debug_flag = 1;
973 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000974 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
Damien Millere4340be2000-09-16 13:29:08 +1100975 options.log_level++;
Damien Miller95def091999-11-25 00:26:21 +1100976 break;
Ben Lindstromc72745a2000-12-02 19:03:54 +0000977 case 'D':
978 no_daemon_flag = 1;
979 break;
Ben Lindstrom9fce9f02001-04-11 23:10:09 +0000980 case 'e':
981 log_stderr = 1;
982 break;
Damien Miller95def091999-11-25 00:26:21 +1100983 case 'i':
984 inetd_flag = 1;
985 break;
Darren Tucker645ab752004-06-25 13:33:20 +1000986 case 'r':
987 rexec_flag = 0;
988 break;
989 case 'R':
990 rexeced_flag = 1;
991 inetd_flag = 1;
992 break;
Damien Miller95def091999-11-25 00:26:21 +1100993 case 'Q':
Ben Lindstromd5390202001-01-29 08:07:43 +0000994 /* ignored */
Damien Miller95def091999-11-25 00:26:21 +1100995 break;
996 case 'q':
997 options.log_level = SYSLOG_LEVEL_QUIET;
998 break;
999 case 'b':
Damien Millerf0b15df2006-03-26 13:59:20 +11001000 options.server_key_bits = (int)strtonum(optarg, 256,
1001 32768, NULL);
Damien Miller95def091999-11-25 00:26:21 +11001002 break;
1003 case 'p':
Damien Miller34132e52000-01-14 15:45:46 +11001004 options.ports_from_cmdline = 1;
Damien Millere4340be2000-09-16 13:29:08 +11001005 if (options.num_ports >= MAX_PORTS) {
1006 fprintf(stderr, "too many ports.\n");
1007 exit(1);
1008 }
Ben Lindstrom19066a12001-04-12 23:39:26 +00001009 options.ports[options.num_ports++] = a2port(optarg);
1010 if (options.ports[options.num_ports-1] == 0) {
1011 fprintf(stderr, "Bad port number.\n");
1012 exit(1);
1013 }
Damien Miller95def091999-11-25 00:26:21 +11001014 break;
1015 case 'g':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +00001016 if ((options.login_grace_time = convtime(optarg)) == -1) {
1017 fprintf(stderr, "Invalid login grace time.\n");
1018 exit(1);
1019 }
Damien Miller95def091999-11-25 00:26:21 +11001020 break;
1021 case 'k':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +00001022 if ((options.key_regeneration_time = convtime(optarg)) == -1) {
1023 fprintf(stderr, "Invalid key regeneration interval.\n");
1024 exit(1);
1025 }
Damien Miller95def091999-11-25 00:26:21 +11001026 break;
1027 case 'h':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001028 if (options.num_host_key_files >= MAX_HOSTKEYS) {
1029 fprintf(stderr, "too many host keys.\n");
1030 exit(1);
1031 }
1032 options.host_key_files[options.num_host_key_files++] = optarg;
Damien Miller95def091999-11-25 00:26:21 +11001033 break;
Ben Lindstrom794325a2001-08-06 21:09:07 +00001034 case 't':
1035 test_flag = 1;
1036 break;
Damien Miller942da032000-08-18 13:59:06 +10001037 case 'u':
Damien Millerf0b15df2006-03-26 13:59:20 +11001038 utmp_len = (u_int)strtonum(optarg, 0, MAXHOSTNAMELEN+1, NULL);
Ben Lindstrom41daec72002-07-23 21:15:13 +00001039 if (utmp_len > MAXHOSTNAMELEN) {
1040 fprintf(stderr, "Invalid utmp length.\n");
1041 exit(1);
1042 }
Damien Miller942da032000-08-18 13:59:06 +10001043 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001044 case 'o':
Damien Millerb9997192003-12-17 16:29:22 +11001045 line = xstrdup(optarg);
1046 if (process_server_config_line(&options, line,
Darren Tucker45150472006-07-12 22:34:17 +10001047 "command-line", 0, NULL, NULL, NULL, NULL) != 0)
Damien Miller9f0f5c62001-12-21 14:45:46 +11001048 exit(1);
Damien Millerb9997192003-12-17 16:29:22 +11001049 xfree(line);
Ben Lindstromade03f62001-12-06 18:22:17 +00001050 break;
Damien Miller95def091999-11-25 00:26:21 +11001051 case '?':
1052 default:
Ben Lindstromade03f62001-12-06 18:22:17 +00001053 usage();
1054 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001055 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001056 }
Darren Tucker645ab752004-06-25 13:33:20 +10001057 if (rexeced_flag || inetd_flag)
1058 rexec_flag = 0;
1059 if (rexec_flag && (av[0] == NULL || *av[0] != '/'))
1060 fatal("sshd re-exec requires execution with an absolute path");
1061 if (rexeced_flag)
Damien Miller035a5b42004-06-26 08:16:31 +10001062 closefrom(REEXEC_MIN_FREE_FD);
1063 else
1064 closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001065
Ben Lindstrom425fb022001-03-29 00:31:20 +00001066 SSLeay_add_all_algorithms();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067
Damien Miller34132e52000-01-14 15:45:46 +11001068 /*
1069 * Force logging to stderr until we have loaded the private host
1070 * key (unless started from inetd)
1071 */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001072 log_init(__progname,
Damien Miller5aa5d782002-02-08 22:01:54 +11001073 options.log_level == SYSLOG_LEVEL_NOT_SET ?
1074 SYSLOG_LEVEL_INFO : options.log_level,
1075 options.log_facility == SYSLOG_FACILITY_NOT_SET ?
1076 SYSLOG_FACILITY_AUTH : options.log_facility,
Ben Lindstromc2faa4a2002-11-09 15:50:03 +00001077 log_stderr || !inetd_flag);
Damien Miller34132e52000-01-14 15:45:46 +11001078
Darren Tucker86c093d2004-03-08 22:59:03 +11001079 /*
1080 * Unset KRB5CCNAME, otherwise the user's session may inherit it from
1081 * root's environment
Damien Miller94cf4c82005-07-17 17:04:47 +10001082 */
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001083 if (getenv("KRB5CCNAME") != NULL)
1084 unsetenv("KRB5CCNAME");
1085
Tim Rice81ed5182002-09-25 17:38:46 -07001086#ifdef _UNICOS
Darren Tucker12984962004-05-24 13:37:13 +10001087 /* Cray can define user privs drop all privs now!
Ben Lindstrom6db66ff2001-08-06 23:29:16 +00001088 * Not needed on PRIV_SU systems!
1089 */
1090 drop_cray_privs();
1091#endif
1092
Darren Tucker645ab752004-06-25 13:33:20 +10001093 sensitive_data.server_key = NULL;
1094 sensitive_data.ssh1_host_key = NULL;
1095 sensitive_data.have_ssh1_key = 0;
1096 sensitive_data.have_ssh2_key = 0;
1097
1098 /* Fetch our configuration */
1099 buffer_init(&cfg);
1100 if (rexeced_flag)
Damien Miller035a5b42004-06-26 08:16:31 +10001101 recv_rexec_state(REEXEC_CONFIG_PASS_FD, &cfg);
Darren Tucker645ab752004-06-25 13:33:20 +10001102 else
1103 load_server_config(config_file_name, &cfg);
1104
Darren Tucker45150472006-07-12 22:34:17 +10001105 parse_server_config(&options, rexeced_flag ? "rexec" : config_file_name,
1106 &cfg, NULL, NULL, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107
Darren Tuckerc6f82192005-09-27 22:46:32 +10001108 seed_rng();
1109
Damien Miller95def091999-11-25 00:26:21 +11001110 /* Fill in default values for those options not explicitly set. */
1111 fill_default_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001112
Darren Tucker0f383232005-01-20 10:57:56 +11001113 /* set default channel AF */
1114 channel_set_af(options.address_family);
1115
Damien Miller95def091999-11-25 00:26:21 +11001116 /* Check that there are no remaining arguments. */
1117 if (optind < ac) {
1118 fprintf(stderr, "Extra argument %s.\n", av[optind]);
1119 exit(1);
1120 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001121
Damien Miller2aa6d3c2004-09-12 16:53:04 +10001122 debug("sshd version %.100s", SSH_RELEASE);
Damien Miller2ccf6611999-11-15 15:25:10 +11001123
Damien Miller0bc1bd82000-11-13 22:57:25 +11001124 /* load private host keys */
Damien Miller07d86be2006-03-26 14:19:21 +11001125 sensitive_data.host_keys = xcalloc(options.num_host_key_files,
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001126 sizeof(Key *));
Damien Miller9f0f5c62001-12-21 14:45:46 +11001127 for (i = 0; i < options.num_host_key_files; i++)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001128 sensitive_data.host_keys[i] = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001129
Damien Miller9f0f5c62001-12-21 14:45:46 +11001130 for (i = 0; i < options.num_host_key_files; i++) {
Ben Lindstromd0fca422001-03-26 13:44:06 +00001131 key = key_load_private(options.host_key_files[i], "", NULL);
1132 sensitive_data.host_keys[i] = key;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001133 if (key == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001134 error("Could not load host key: %s",
1135 options.host_key_files[i]);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001136 sensitive_data.host_keys[i] = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001137 continue;
1138 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001139 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001140 case KEY_RSA1:
1141 sensitive_data.ssh1_host_key = key;
1142 sensitive_data.have_ssh1_key = 1;
1143 break;
1144 case KEY_RSA:
1145 case KEY_DSA:
1146 sensitive_data.have_ssh2_key = 1;
1147 break;
1148 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001149 debug("private host key: #%d type %d %s", i, key->type,
1150 key_type(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001151 }
1152 if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
Damien Miller996acd22003-04-09 20:59:48 +10001153 logit("Disabling protocol version 1. Could not load host key");
Damien Millereba71ba2000-04-29 23:57:08 +10001154 options.protocol &= ~SSH_PROTO_1;
1155 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001156 if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
Damien Miller996acd22003-04-09 20:59:48 +10001157 logit("Disabling protocol version 2. Could not load host key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001158 options.protocol &= ~SSH_PROTO_2;
Damien Millereba71ba2000-04-29 23:57:08 +10001159 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001160 if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
Damien Miller996acd22003-04-09 20:59:48 +10001161 logit("sshd: no hostkeys available -- exiting.");
Damien Miller95def091999-11-25 00:26:21 +11001162 exit(1);
1163 }
Damien Miller95def091999-11-25 00:26:21 +11001164
Damien Millereba71ba2000-04-29 23:57:08 +10001165 /* Check certain values for sanity. */
1166 if (options.protocol & SSH_PROTO_1) {
1167 if (options.server_key_bits < 512 ||
1168 options.server_key_bits > 32768) {
1169 fprintf(stderr, "Bad server key size.\n");
1170 exit(1);
1171 }
1172 /*
1173 * Check that server and host key lengths differ sufficiently. This
1174 * is necessary to make double encryption work with rsaref. Oh, I
1175 * hate software patents. I dont know if this can go? Niels
1176 */
1177 if (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 && options.server_key_bits <
1180 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1181 SSH_KEY_BITS_RESERVED) {
Damien Millereba71ba2000-04-29 23:57:08 +10001182 options.server_key_bits =
Ben Lindstrom822b6342002-06-23 21:38:49 +00001183 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1184 SSH_KEY_BITS_RESERVED;
Damien Millereba71ba2000-04-29 23:57:08 +10001185 debug("Forcing server key to %d bits to make it differ from host key.",
1186 options.server_key_bits);
1187 }
1188 }
1189
Ben Lindstroma26ea632002-06-06 20:46:25 +00001190 if (use_privsep) {
Ben Lindstroma26ea632002-06-06 20:46:25 +00001191 struct stat st;
1192
Damien Millerf0b15df2006-03-26 13:59:20 +11001193 if (getpwnam(SSH_PRIVSEP_USER) == NULL)
Ben Lindstroma26ea632002-06-06 20:46:25 +00001194 fatal("Privilege separation user %s does not exist",
1195 SSH_PRIVSEP_USER);
1196 if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
1197 (S_ISDIR(st.st_mode) == 0))
1198 fatal("Missing privilege separation directory: %s",
1199 _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstrom59627352002-06-27 18:02:21 +00001200
1201#ifdef HAVE_CYGWIN
1202 if (check_ntsec(_PATH_PRIVSEP_CHROOT_DIR) &&
1203 (st.st_uid != getuid () ||
1204 (st.st_mode & (S_IWGRP|S_IWOTH)) != 0))
1205#else
Ben Lindstrom2dfacb32002-06-23 00:33:47 +00001206 if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
Ben Lindstrom59627352002-06-27 18:02:21 +00001207#endif
Damien Miller180fc5b2003-02-24 11:50:18 +11001208 fatal("%s must be owned by root and not group or "
1209 "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstroma26ea632002-06-06 20:46:25 +00001210 }
1211
Ben Lindstrom794325a2001-08-06 21:09:07 +00001212 /* Configuration looks good, so exit if in test mode. */
1213 if (test_flag)
1214 exit(0);
1215
Damien Miller87aea252002-05-10 12:20:24 +10001216 /*
1217 * Clear out any supplemental groups we may have inherited. This
1218 * prevents inadvertent creation of files with bad modes (in the
Damien Millera8e06ce2003-11-21 23:48:55 +11001219 * portable version at least, it's certainly possible for PAM
1220 * to create a file, and we can't control the code in every
Damien Miller87aea252002-05-10 12:20:24 +10001221 * module which might be used).
1222 */
1223 if (setgroups(0, NULL) < 0)
1224 debug("setgroups() failed: %.200s", strerror(errno));
1225
Darren Tucker645ab752004-06-25 13:33:20 +10001226 if (rexec_flag) {
Damien Miller07d86be2006-03-26 14:19:21 +11001227 rexec_argv = xcalloc(rexec_argc + 2, sizeof(char *));
Darren Tucker645ab752004-06-25 13:33:20 +10001228 for (i = 0; i < rexec_argc; i++) {
1229 debug("rexec_argv[%d]='%s'", i, saved_argv[i]);
1230 rexec_argv[i] = saved_argv[i];
1231 }
1232 rexec_argv[rexec_argc] = "-R";
1233 rexec_argv[rexec_argc + 1] = NULL;
1234 }
1235
Damien Millereba71ba2000-04-29 23:57:08 +10001236 /* Initialize the log (it is reinitialized below in case we forked). */
Darren Tuckerea7c8122005-01-20 11:03:08 +11001237 if (debug_flag && (!inetd_flag || rexeced_flag))
Damien Miller95def091999-11-25 00:26:21 +11001238 log_stderr = 1;
Kevin Stevesec84dc12000-12-13 17:45:15 +00001239 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001240
Damien Millereba71ba2000-04-29 23:57:08 +10001241 /*
1242 * If not in debugging mode, and not started from inetd, disconnect
1243 * from the controlling terminal, and fork. The original process
1244 * exits.
1245 */
Ben Lindstromc72745a2000-12-02 19:03:54 +00001246 if (!(debug_flag || inetd_flag || no_daemon_flag)) {
Damien Miller95def091999-11-25 00:26:21 +11001247#ifdef TIOCNOTTY
1248 int fd;
1249#endif /* TIOCNOTTY */
1250 if (daemon(0, 0) < 0)
1251 fatal("daemon() failed: %.200s", strerror(errno));
1252
1253 /* Disconnect from the controlling tty. */
1254#ifdef TIOCNOTTY
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001255 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
Damien Miller95def091999-11-25 00:26:21 +11001256 if (fd >= 0) {
1257 (void) ioctl(fd, TIOCNOTTY, NULL);
1258 close(fd);
1259 }
1260#endif /* TIOCNOTTY */
1261 }
1262 /* Reinitialize the log (because of the fork above). */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001263 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001264
Damien Miller95def091999-11-25 00:26:21 +11001265 /* Initialize the random number generator. */
1266 arc4random_stir();
1267
1268 /* Chdir to the root directory so that the current disk can be
1269 unmounted if desired. */
1270 chdir("/");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001271
Ben Lindstromde71cda2001-03-24 00:43:26 +00001272 /* ignore SIGPIPE */
1273 signal(SIGPIPE, SIG_IGN);
Damien Miller95def091999-11-25 00:26:21 +11001274
Damien Miller95def091999-11-25 00:26:21 +11001275 /* Start listening for a socket, unless started from inetd. */
1276 if (inetd_flag) {
Darren Tucker645ab752004-06-25 13:33:20 +10001277 int fd;
1278
Damien Miller994cf142000-07-21 10:19:44 +10001279 startup_pipe = -1;
Darren Tucker645ab752004-06-25 13:33:20 +10001280 if (rexeced_flag) {
Damien Miller035a5b42004-06-26 08:16:31 +10001281 close(REEXEC_CONFIG_PASS_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001282 sock_in = sock_out = dup(STDIN_FILENO);
1283 if (!debug_flag) {
Damien Miller035a5b42004-06-26 08:16:31 +10001284 startup_pipe = dup(REEXEC_STARTUP_PIPE_FD);
1285 close(REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001286 }
1287 } else {
1288 sock_in = dup(STDIN_FILENO);
1289 sock_out = dup(STDOUT_FILENO);
1290 }
Damien Millereba71ba2000-04-29 23:57:08 +10001291 /*
1292 * We intentionally do not close the descriptors 0, 1, and 2
Darren Tucker645ab752004-06-25 13:33:20 +10001293 * as our code for setting the descriptors won't work if
Damien Millereba71ba2000-04-29 23:57:08 +10001294 * ttyfd happens to be one of those.
1295 */
Darren Tucker645ab752004-06-25 13:33:20 +10001296 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1297 dup2(fd, STDIN_FILENO);
1298 dup2(fd, STDOUT_FILENO);
1299 if (fd > STDOUT_FILENO)
1300 close(fd);
1301 }
Damien Miller95def091999-11-25 00:26:21 +11001302 debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
Darren Tucker645ab752004-06-25 13:33:20 +10001303 if ((options.protocol & SSH_PROTO_1) &&
1304 sensitive_data.server_key == NULL)
Ben Lindstromca42d5f2001-03-09 18:25:32 +00001305 generate_ephemeral_server_key();
Damien Miller95def091999-11-25 00:26:21 +11001306 } else {
Damien Miller34132e52000-01-14 15:45:46 +11001307 for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
1308 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1309 continue;
1310 if (num_listen_socks >= MAX_LISTEN_SOCKS)
1311 fatal("Too many listen sockets. "
1312 "Enlarge MAX_LISTEN_SOCKS");
Darren Tucker96d47102005-02-09 09:53:48 +11001313 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
Damien Miller34132e52000-01-14 15:45:46 +11001314 ntop, sizeof(ntop), strport, sizeof(strport),
Darren Tucker96d47102005-02-09 09:53:48 +11001315 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1316 error("getnameinfo failed: %.100s",
1317 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
1318 strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11001319 continue;
1320 }
1321 /* Create socket for listening. */
Damien Miller2372ace2003-05-14 13:42:23 +10001322 listen_sock = socket(ai->ai_family, ai->ai_socktype,
1323 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11001324 if (listen_sock < 0) {
1325 /* kernel may not support ipv6 */
1326 verbose("socket: %.100s", strerror(errno));
1327 continue;
1328 }
Damien Miller232711f2004-06-15 10:35:30 +10001329 if (set_nonblock(listen_sock) == -1) {
Darren Tuckerefa37062004-02-24 09:20:29 +11001330 close(listen_sock);
1331 continue;
1332 }
Damien Miller34132e52000-01-14 15:45:46 +11001333 /*
Damien Millere1383ce2002-09-19 11:49:37 +10001334 * Set socket options.
1335 * Allow local port reuse in TIME_WAIT.
Damien Miller34132e52000-01-14 15:45:46 +11001336 */
Damien Millere1383ce2002-09-19 11:49:37 +10001337 if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
1338 &on, sizeof(on)) == -1)
1339 error("setsockopt SO_REUSEADDR: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001340
Damien Miller34132e52000-01-14 15:45:46 +11001341 debug("Bind to port %s on %s.", strport, ntop);
Damien Miller95def091999-11-25 00:26:21 +11001342
Damien Miller34132e52000-01-14 15:45:46 +11001343 /* Bind the socket to the desired port. */
Damien Miller0a4e27d2001-02-18 12:36:39 +11001344 if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1345 if (!ai->ai_next)
1346 error("Bind to port %s on %s failed: %.200s.",
1347 strport, ntop, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11001348 close(listen_sock);
1349 continue;
1350 }
1351 listen_socks[num_listen_socks] = listen_sock;
1352 num_listen_socks++;
Damien Miller95def091999-11-25 00:26:21 +11001353
Damien Miller34132e52000-01-14 15:45:46 +11001354 /* Start listening on the port. */
Darren Tucker3175eb92003-12-09 19:15:11 +11001355 if (listen(listen_sock, SSH_LISTEN_BACKLOG) < 0)
Damien Millerb24c2f82006-03-15 12:04:36 +11001356 fatal("listen on [%s]:%s: %.100s",
1357 ntop, strport, strerror(errno));
1358 logit("Server listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001359 }
Damien Miller34132e52000-01-14 15:45:46 +11001360 freeaddrinfo(options.listen_addrs);
1361
1362 if (!num_listen_socks)
1363 fatal("Cannot bind any address.");
1364
Ben Lindstrom98097862001-06-25 05:10:20 +00001365 if (options.protocol & SSH_PROTO_1)
1366 generate_ephemeral_server_key();
1367
1368 /*
1369 * Arrange to restart on SIGHUP. The handler needs
1370 * listen_sock.
1371 */
1372 signal(SIGHUP, sighup_handler);
1373
1374 signal(SIGTERM, sigterm_handler);
1375 signal(SIGQUIT, sigterm_handler);
1376
1377 /* Arrange SIGCHLD to be caught. */
1378 signal(SIGCHLD, main_sigchld_handler);
1379
1380 /* Write out the pid file after the sigterm handler is setup */
Damien Miller95def091999-11-25 00:26:21 +11001381 if (!debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +11001382 /*
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001383 * Record our pid in /var/run/sshd.pid to make it
1384 * easier to kill the correct sshd. We don't want to
1385 * do this before the bind above because the bind will
Damien Miller5428f641999-11-25 11:54:57 +11001386 * fail if there already is a daemon, and this will
1387 * overwrite any old pid in the file.
1388 */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001389 f = fopen(options.pid_file, "wb");
Darren Tuckere5327042003-07-03 13:40:44 +10001390 if (f == NULL) {
1391 error("Couldn't create pid file \"%s\": %s",
1392 options.pid_file, strerror(errno));
1393 } else {
Ben Lindstromce0f6342002-06-11 16:42:49 +00001394 fprintf(f, "%ld\n", (long) getpid());
Damien Miller95def091999-11-25 00:26:21 +11001395 fclose(f);
1396 }
1397 }
Damien Miller95def091999-11-25 00:26:21 +11001398
Damien Miller34132e52000-01-14 15:45:46 +11001399 /* setup fd set for listen */
Damien Miller37023962000-07-11 17:31:38 +10001400 fdset = NULL;
Damien Miller34132e52000-01-14 15:45:46 +11001401 maxfd = 0;
1402 for (i = 0; i < num_listen_socks; i++)
1403 if (listen_socks[i] > maxfd)
1404 maxfd = listen_socks[i];
Damien Miller37023962000-07-11 17:31:38 +10001405 /* pipes connected to unauthenticated childs */
Damien Miller07d86be2006-03-26 14:19:21 +11001406 startup_pipes = xcalloc(options.max_startups, sizeof(int));
Damien Miller37023962000-07-11 17:31:38 +10001407 for (i = 0; i < options.max_startups; i++)
1408 startup_pipes[i] = -1;
Damien Miller34132e52000-01-14 15:45:46 +11001409
Damien Miller5428f641999-11-25 11:54:57 +11001410 /*
1411 * Stay listening for connections until the system crashes or
1412 * the daemon is killed with a signal.
1413 */
Damien Miller95def091999-11-25 00:26:21 +11001414 for (;;) {
1415 if (received_sighup)
1416 sighup_restart();
Damien Miller37023962000-07-11 17:31:38 +10001417 if (fdset != NULL)
1418 xfree(fdset);
Damien Miller07d86be2006-03-26 14:19:21 +11001419 fdset = (fd_set *)xcalloc(howmany(maxfd + 1, NFDBITS),
1420 sizeof(fd_mask));
Damien Miller37023962000-07-11 17:31:38 +10001421
Damien Miller34132e52000-01-14 15:45:46 +11001422 for (i = 0; i < num_listen_socks; i++)
1423 FD_SET(listen_socks[i], fdset);
Damien Miller37023962000-07-11 17:31:38 +10001424 for (i = 0; i < options.max_startups; i++)
1425 if (startup_pipes[i] != -1)
1426 FD_SET(startup_pipes[i], fdset);
1427
1428 /* Wait in select until there is a connection. */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001429 ret = select(maxfd+1, fdset, NULL, NULL, NULL);
1430 if (ret < 0 && errno != EINTR)
1431 error("select: %.100s", strerror(errno));
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001432 if (received_sigterm) {
Damien Miller996acd22003-04-09 20:59:48 +10001433 logit("Received signal %d; terminating.",
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001434 (int) received_sigterm);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001435 close_listen_socks();
1436 unlink(options.pid_file);
1437 exit(255);
1438 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001439 if (key_used && key_do_regen) {
Ben Lindstromca42d5f2001-03-09 18:25:32 +00001440 generate_ephemeral_server_key();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001441 key_used = 0;
1442 key_do_regen = 0;
Damien Miller34132e52000-01-14 15:45:46 +11001443 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001444 if (ret < 0)
1445 continue;
1446
Damien Miller37023962000-07-11 17:31:38 +10001447 for (i = 0; i < options.max_startups; i++)
1448 if (startup_pipes[i] != -1 &&
1449 FD_ISSET(startup_pipes[i], fdset)) {
1450 /*
1451 * the read end of the pipe is ready
1452 * if the child has closed the pipe
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001453 * after successful authentication
Damien Miller37023962000-07-11 17:31:38 +10001454 * or if the child has died
1455 */
1456 close(startup_pipes[i]);
1457 startup_pipes[i] = -1;
1458 startups--;
1459 }
Damien Miller34132e52000-01-14 15:45:46 +11001460 for (i = 0; i < num_listen_socks; i++) {
1461 if (!FD_ISSET(listen_socks[i], fdset))
Damien Miller95def091999-11-25 00:26:21 +11001462 continue;
Damien Miller37023962000-07-11 17:31:38 +10001463 fromlen = sizeof(from);
Damien Millerf0b15df2006-03-26 13:59:20 +11001464 newsock = accept(listen_socks[i],
1465 (struct sockaddr *)&from, &fromlen);
Damien Miller37023962000-07-11 17:31:38 +10001466 if (newsock < 0) {
1467 if (errno != EINTR && errno != EWOULDBLOCK)
1468 error("accept: %.100s", strerror(errno));
1469 continue;
1470 }
Damien Miller232711f2004-06-15 10:35:30 +10001471 if (unset_nonblock(newsock) == -1) {
Darren Tuckerefa37062004-02-24 09:20:29 +11001472 close(newsock);
1473 continue;
1474 }
Damien Miller942da032000-08-18 13:59:06 +10001475 if (drop_connection(startups) == 1) {
1476 debug("drop connection #%d", startups);
Damien Miller37023962000-07-11 17:31:38 +10001477 close(newsock);
1478 continue;
1479 }
1480 if (pipe(startup_p) == -1) {
1481 close(newsock);
1482 continue;
1483 }
1484
Darren Tucker645ab752004-06-25 13:33:20 +10001485 if (rexec_flag && socketpair(AF_UNIX,
1486 SOCK_STREAM, 0, config_s) == -1) {
1487 error("reexec socketpair: %s",
1488 strerror(errno));
1489 close(newsock);
1490 close(startup_p[0]);
1491 close(startup_p[1]);
1492 continue;
1493 }
1494
Damien Miller37023962000-07-11 17:31:38 +10001495 for (j = 0; j < options.max_startups; j++)
1496 if (startup_pipes[j] == -1) {
1497 startup_pipes[j] = startup_p[0];
1498 if (maxfd < startup_p[0])
1499 maxfd = startup_p[0];
1500 startups++;
1501 break;
1502 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001503
Damien Miller5428f641999-11-25 11:54:57 +11001504 /*
Damien Miller37023962000-07-11 17:31:38 +10001505 * Got connection. Fork a child to handle it, unless
1506 * we are in debugging mode.
Damien Miller5428f641999-11-25 11:54:57 +11001507 */
Damien Miller37023962000-07-11 17:31:38 +10001508 if (debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +11001509 /*
Damien Miller37023962000-07-11 17:31:38 +10001510 * In debugging mode. Close the listening
1511 * socket, and start processing the
1512 * connection without forking.
Damien Miller5428f641999-11-25 11:54:57 +11001513 */
Damien Miller37023962000-07-11 17:31:38 +10001514 debug("Server will not fork when running in debugging mode.");
Damien Miller34132e52000-01-14 15:45:46 +11001515 close_listen_socks();
Damien Miller95def091999-11-25 00:26:21 +11001516 sock_in = newsock;
1517 sock_out = newsock;
Darren Tucker645ab752004-06-25 13:33:20 +10001518 close(startup_p[0]);
1519 close(startup_p[1]);
Damien Miller4d97ba22000-07-11 18:15:50 +10001520 startup_pipe = -1;
Damien Miller182ee6e2000-07-12 09:45:27 +10001521 pid = getpid();
Darren Tucker645ab752004-06-25 13:33:20 +10001522 if (rexec_flag) {
1523 send_rexec_state(config_s[0],
1524 &cfg);
1525 close(config_s[0]);
1526 }
Damien Miller95def091999-11-25 00:26:21 +11001527 break;
Damien Miller37023962000-07-11 17:31:38 +10001528 } else {
1529 /*
1530 * Normal production daemon. Fork, and have
1531 * the child process the connection. The
1532 * parent continues listening.
1533 */
1534 if ((pid = fork()) == 0) {
1535 /*
Damien Millerf0b15df2006-03-26 13:59:20 +11001536 * Child. Close the listening and
1537 * max_startup sockets. Start using
1538 * the accepted socket. Reinitialize
1539 * logging (since our pid has changed).
1540 * We break out of the loop to handle
Damien Miller37023962000-07-11 17:31:38 +10001541 * the connection.
1542 */
1543 startup_pipe = startup_p[1];
Ben Lindstromd84df982001-12-06 16:35:40 +00001544 close_startup_pipes();
Damien Miller37023962000-07-11 17:31:38 +10001545 close_listen_socks();
1546 sock_in = newsock;
1547 sock_out = newsock;
Damien Millerf0b15df2006-03-26 13:59:20 +11001548 log_init(__progname,
1549 options.log_level,
1550 options.log_facility,
1551 log_stderr);
Darren Tucker0f56ed12004-08-29 16:38:41 +10001552 if (rexec_flag)
1553 close(config_s[0]);
Damien Miller37023962000-07-11 17:31:38 +10001554 break;
1555 }
Damien Miller95def091999-11-25 00:26:21 +11001556 }
Damien Miller37023962000-07-11 17:31:38 +10001557
1558 /* Parent. Stay in the loop. */
1559 if (pid < 0)
1560 error("fork: %.100s", strerror(errno));
1561 else
Ben Lindstromce0f6342002-06-11 16:42:49 +00001562 debug("Forked child %ld.", (long)pid);
Damien Miller37023962000-07-11 17:31:38 +10001563
1564 close(startup_p[1]);
1565
Darren Tucker645ab752004-06-25 13:33:20 +10001566 if (rexec_flag) {
1567 send_rexec_state(config_s[0], &cfg);
1568 close(config_s[0]);
1569 close(config_s[1]);
1570 }
1571
Damien Millerf0b15df2006-03-26 13:59:20 +11001572 /*
1573 * Mark that the key has been used (it
1574 * was "given" to the child).
1575 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001576 if ((options.protocol & SSH_PROTO_1) &&
1577 key_used == 0) {
1578 /* Schedule server key regeneration alarm. */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00001579 signal(SIGALRM, key_regeneration_alarm);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001580 alarm(options.key_regeneration_time);
1581 key_used = 1;
1582 }
Damien Miller37023962000-07-11 17:31:38 +10001583
1584 arc4random_stir();
Damien Miller37023962000-07-11 17:31:38 +10001585 close(newsock);
Damien Miller95def091999-11-25 00:26:21 +11001586 }
Damien Miller34132e52000-01-14 15:45:46 +11001587 /* child process check (or debug mode) */
1588 if (num_listen_socks < 0)
1589 break;
Damien Miller95def091999-11-25 00:26:21 +11001590 }
1591 }
1592
1593 /* This is the child processing a new connection. */
Damien Miller57aae982004-03-08 23:11:25 +11001594 setproctitle("%s", "[accepted]");
Damien Miller95def091999-11-25 00:26:21 +11001595
Darren Tucker6832b832004-08-12 22:36:51 +10001596 /*
1597 * Create a new session and process group since the 4.4BSD
1598 * setlogin() affects the entire process group. We don't
1599 * want the child to be able to affect the parent.
1600 */
1601#if !defined(SSHD_ACQUIRES_CTTY)
1602 /*
1603 * If setsid is called, on some platforms sshd will later acquire a
1604 * controlling terminal which will result in "could not set
1605 * controlling tty" errors.
1606 */
1607 if (!debug_flag && !inetd_flag && setsid() < 0)
1608 error("setsid: %.100s", strerror(errno));
1609#endif
1610
Darren Tucker645ab752004-06-25 13:33:20 +10001611 if (rexec_flag) {
1612 int fd;
1613
Damien Miller035a5b42004-06-26 08:16:31 +10001614 debug("rexec start in %d out %d newsock %d pipe %d sock %d",
1615 sock_in, sock_out, newsock, startup_pipe, config_s[0]);
Darren Tucker645ab752004-06-25 13:33:20 +10001616 dup2(newsock, STDIN_FILENO);
1617 dup2(STDIN_FILENO, STDOUT_FILENO);
1618 if (startup_pipe == -1)
Damien Miller035a5b42004-06-26 08:16:31 +10001619 close(REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001620 else
Damien Miller035a5b42004-06-26 08:16:31 +10001621 dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001622
Damien Miller035a5b42004-06-26 08:16:31 +10001623 dup2(config_s[1], REEXEC_CONFIG_PASS_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001624 close(config_s[1]);
Darren Tuckerd8835932004-08-12 22:42:29 +10001625 if (startup_pipe != -1)
1626 close(startup_pipe);
Damien Miller035a5b42004-06-26 08:16:31 +10001627
Darren Tucker645ab752004-06-25 13:33:20 +10001628 execv(rexec_argv[0], rexec_argv);
1629
1630 /* Reexec has failed, fall back and continue */
1631 error("rexec of %s failed: %s", rexec_argv[0], strerror(errno));
Damien Miller035a5b42004-06-26 08:16:31 +10001632 recv_rexec_state(REEXEC_CONFIG_PASS_FD, NULL);
Darren Tucker645ab752004-06-25 13:33:20 +10001633 log_init(__progname, options.log_level,
1634 options.log_facility, log_stderr);
1635
1636 /* Clean up fds */
Damien Miller035a5b42004-06-26 08:16:31 +10001637 startup_pipe = REEXEC_STARTUP_PIPE_FD;
Darren Tucker645ab752004-06-25 13:33:20 +10001638 close(config_s[1]);
Damien Miller035a5b42004-06-26 08:16:31 +10001639 close(REEXEC_CONFIG_PASS_FD);
1640 newsock = sock_out = sock_in = dup(STDIN_FILENO);
Darren Tucker645ab752004-06-25 13:33:20 +10001641 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1642 dup2(fd, STDIN_FILENO);
1643 dup2(fd, STDOUT_FILENO);
1644 if (fd > STDERR_FILENO)
1645 close(fd);
1646 }
Damien Miller035a5b42004-06-26 08:16:31 +10001647 debug("rexec cleanup in %d out %d newsock %d pipe %d sock %d",
1648 sock_in, sock_out, newsock, startup_pipe, config_s[0]);
Darren Tucker645ab752004-06-25 13:33:20 +10001649 }
1650
Damien Miller5428f641999-11-25 11:54:57 +11001651 /*
1652 * Disable the key regeneration alarm. We will not regenerate the
1653 * key since we are no longer in a position to give it to anyone. We
1654 * will not restart on SIGHUP since it no longer makes sense.
1655 */
Damien Miller95def091999-11-25 00:26:21 +11001656 alarm(0);
1657 signal(SIGALRM, SIG_DFL);
1658 signal(SIGHUP, SIG_DFL);
1659 signal(SIGTERM, SIG_DFL);
1660 signal(SIGQUIT, SIG_DFL);
1661 signal(SIGCHLD, SIG_DFL);
Damien Miller4e0f5e12000-08-29 11:05:50 +11001662 signal(SIGINT, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001663
Damien Miller5428f641999-11-25 11:54:57 +11001664 /*
1665 * Register our connection. This turns encryption off because we do
1666 * not have a key.
1667 */
Damien Miller95def091999-11-25 00:26:21 +11001668 packet_set_connection(sock_in, sock_out);
Damien Miller9786e6e2005-07-26 21:54:56 +10001669 packet_set_server();
Damien Miller95def091999-11-25 00:26:21 +11001670
Damien Miller4f1d6b22005-05-26 11:59:32 +10001671 /* Set SO_KEEPALIVE if requested. */
1672 if (options.tcp_keep_alive && packet_connection_is_on_socket() &&
1673 setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0)
1674 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1675
Damien Miller677257f2005-06-17 12:55:03 +10001676 if ((remote_port = get_remote_port()) < 0) {
1677 debug("get_remote_port failed");
1678 cleanup_exit(255);
1679 }
Damien Miller4d3fd542005-11-05 15:13:24 +11001680
1681 /*
1682 * We use get_canonical_hostname with usedns = 0 instead of
1683 * get_remote_ipaddr here so IP options will be checked.
1684 */
Damien Millereb13e552006-06-13 13:03:53 +10001685 (void) get_canonical_hostname(0);
1686 /*
1687 * The rest of the code depends on the fact that
1688 * get_remote_ipaddr() caches the remote ip, even if
1689 * the socket goes away.
1690 */
1691 remote_ip = get_remote_ipaddr();
Damien Miller95def091999-11-25 00:26:21 +11001692
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001693#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001694 audit_connection_from(remote_ip, remote_port);
1695#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001696#ifdef LIBWRAP
Damien Miller6a4a4b92001-11-12 11:07:11 +11001697 /* Check whether logins are denied from this host. */
Darren Tucker586b0b92004-06-25 13:34:31 +10001698 if (packet_connection_is_on_socket()) {
Damien Miller95def091999-11-25 00:26:21 +11001699 struct request_info req;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001700
Ben Lindstromce89dac2001-09-12 16:58:04 +00001701 request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, 0);
Damien Miller95def091999-11-25 00:26:21 +11001702 fromhost(&req);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001703
Damien Miller95def091999-11-25 00:26:21 +11001704 if (!hosts_access(&req)) {
Damien Miller6a4a4b92001-11-12 11:07:11 +11001705 debug("Connection refused by tcp wrapper");
Ben Lindstrom7de696e2001-03-29 00:45:12 +00001706 refuse(&req);
Damien Miller6a4a4b92001-11-12 11:07:11 +11001707 /* NOTREACHED */
1708 fatal("libwrap refuse returns");
Damien Miller95def091999-11-25 00:26:21 +11001709 }
Damien Miller95def091999-11-25 00:26:21 +11001710 }
Damien Miller34132e52000-01-14 15:45:46 +11001711#endif /* LIBWRAP */
Damien Miller6a4a4b92001-11-12 11:07:11 +11001712
Damien Miller95def091999-11-25 00:26:21 +11001713 /* Log the connection. */
1714 verbose("Connection from %.500s port %d", remote_ip, remote_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001715
Damien Miller5428f641999-11-25 11:54:57 +11001716 /*
Damien Miller788f2122005-11-05 15:14:59 +11001717 * We don't want to listen forever unless the other side
Damien Miller5428f641999-11-25 11:54:57 +11001718 * successfully authenticates itself. So we set up an alarm which is
1719 * cleared after successful authentication. A limit of zero
Damien Miller788f2122005-11-05 15:14:59 +11001720 * indicates no limit. Note that we don't set the alarm in debugging
Damien Miller5428f641999-11-25 11:54:57 +11001721 * mode; it is just annoying to have the server exit just when you
1722 * are about to discover the bug.
1723 */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00001724 signal(SIGALRM, grace_alarm_handler);
Damien Miller95def091999-11-25 00:26:21 +11001725 if (!debug_flag)
1726 alarm(options.login_grace_time);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001727
Damien Millerb38eff82000-04-01 11:09:21 +10001728 sshd_exchange_identification(sock_in, sock_out);
Darren Tuckerec960f22003-08-13 20:37:05 +10001729
Damien Miller95def091999-11-25 00:26:21 +11001730 packet_set_nonblocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001731
Darren Tucker3e33cec2003-10-02 16:12:36 +10001732 /* allocate authentication context */
Damien Miller07d86be2006-03-26 14:19:21 +11001733 authctxt = xcalloc(1, sizeof(*authctxt));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001734
Darren Tuckerf3bb4342005-03-31 21:39:25 +10001735 authctxt->loginmsg = &loginmsg;
1736
Darren Tucker3e33cec2003-10-02 16:12:36 +10001737 /* XXX global for cleanup, access from other modules */
1738 the_authctxt = authctxt;
1739
Darren Tucker5c14c732005-01-24 21:55:49 +11001740 /* prepare buffer to collect messages to display to user after login */
1741 buffer_init(&loginmsg);
1742
Ben Lindstrom943481c2002-03-22 03:43:46 +00001743 if (use_privsep)
Darren Tucker3e33cec2003-10-02 16:12:36 +10001744 if (privsep_preauth(authctxt) == 1)
Ben Lindstrom943481c2002-03-22 03:43:46 +00001745 goto authenticated;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001746
Damien Miller396691a2000-01-20 22:44:08 +11001747 /* perform the key exchange */
Damien Miller396691a2000-01-20 22:44:08 +11001748 /* authenticate user and start session */
Damien Millerefb4afe2000-04-12 18:45:05 +10001749 if (compat20) {
1750 do_ssh2_kex();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001751 do_authentication2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001752 } else {
1753 do_ssh1_kex();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001754 do_authentication(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001755 }
Ben Lindstrom943481c2002-03-22 03:43:46 +00001756 /*
1757 * If we use privilege separation, the unprivileged child transfers
1758 * the current keystate and exits
1759 */
1760 if (use_privsep) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001761 mm_send_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001762 exit(0);
Ben Lindstrom943481c2002-03-22 03:43:46 +00001763 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001764
1765 authenticated:
Damien Miller7bff1a92005-12-24 14:59:12 +11001766 /*
1767 * Cancel the alarm we set to limit the time taken for
1768 * authentication.
1769 */
1770 alarm(0);
1771 signal(SIGALRM, SIG_DFL);
1772 if (startup_pipe != -1) {
1773 close(startup_pipe);
1774 startup_pipe = -1;
1775 }
1776
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001777#ifdef SSH_AUDIT_EVENTS
1778 audit_event(SSH_AUTH_SUCCESS);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001779#endif
1780
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001781 /*
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001782 * In privilege separation, we fork another child and prepare
1783 * file descriptor passing.
1784 */
1785 if (use_privsep) {
Ben Lindstrom943481c2002-03-22 03:43:46 +00001786 privsep_postauth(authctxt);
1787 /* the monitor process [priv] will not return */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001788 if (!compat20)
1789 destroy_sensitive_data();
1790 }
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00001791
Darren Tucker3e33cec2003-10-02 16:12:36 +10001792 /* Start session. */
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00001793 do_authenticated(authctxt);
1794
Damien Miller3a5b0232002-03-13 13:19:42 +11001795 /* The connection has been terminated. */
1796 verbose("Closing connection to %.100s", remote_ip);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001797
Damien Millerbeb4ba51999-12-28 15:09:35 +11001798#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +10001799 if (options.use_pam)
1800 finish_pam();
Damien Millerbeb4ba51999-12-28 15:09:35 +11001801#endif /* USE_PAM */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001802
Darren Tucker2b59a6d2005-03-06 22:38:51 +11001803#ifdef SSH_AUDIT_EVENTS
1804 PRIVSEP(audit_event(SSH_CONNECTION_CLOSE));
1805#endif
1806
Damien Miller95def091999-11-25 00:26:21 +11001807 packet_close();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001808
1809 if (use_privsep)
1810 mm_terminate();
1811
Damien Miller95def091999-11-25 00:26:21 +11001812 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001813}
1814
Damien Miller95def091999-11-25 00:26:21 +11001815/*
Ben Lindstromabcb1452002-03-22 01:10:21 +00001816 * Decrypt session_key_int using our private server key and private host key
1817 * (key with larger modulus first).
1818 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001819int
Ben Lindstromabcb1452002-03-22 01:10:21 +00001820ssh1_session_key(BIGNUM *session_key_int)
1821{
1822 int rsafail = 0;
1823
Damien Millerf0b15df2006-03-26 13:59:20 +11001824 if (BN_cmp(sensitive_data.server_key->rsa->n,
1825 sensitive_data.ssh1_host_key->rsa->n) > 0) {
Ben Lindstromabcb1452002-03-22 01:10:21 +00001826 /* Server key has bigger modulus. */
1827 if (BN_num_bits(sensitive_data.server_key->rsa->n) <
Damien Millerf0b15df2006-03-26 13:59:20 +11001828 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1829 SSH_KEY_BITS_RESERVED) {
1830 fatal("do_connection: %s: "
1831 "server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
Ben Lindstromabcb1452002-03-22 01:10:21 +00001832 get_remote_ipaddr(),
1833 BN_num_bits(sensitive_data.server_key->rsa->n),
1834 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1835 SSH_KEY_BITS_RESERVED);
1836 }
1837 if (rsa_private_decrypt(session_key_int, session_key_int,
1838 sensitive_data.server_key->rsa) <= 0)
1839 rsafail++;
1840 if (rsa_private_decrypt(session_key_int, session_key_int,
1841 sensitive_data.ssh1_host_key->rsa) <= 0)
1842 rsafail++;
1843 } else {
1844 /* Host key has bigger modulus (or they are equal). */
1845 if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
Damien Millerf0b15df2006-03-26 13:59:20 +11001846 BN_num_bits(sensitive_data.server_key->rsa->n) +
1847 SSH_KEY_BITS_RESERVED) {
1848 fatal("do_connection: %s: "
1849 "host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
Ben Lindstromabcb1452002-03-22 01:10:21 +00001850 get_remote_ipaddr(),
1851 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1852 BN_num_bits(sensitive_data.server_key->rsa->n),
1853 SSH_KEY_BITS_RESERVED);
1854 }
1855 if (rsa_private_decrypt(session_key_int, session_key_int,
1856 sensitive_data.ssh1_host_key->rsa) < 0)
1857 rsafail++;
1858 if (rsa_private_decrypt(session_key_int, session_key_int,
1859 sensitive_data.server_key->rsa) < 0)
1860 rsafail++;
1861 }
1862 return (rsafail);
1863}
1864/*
Damien Miller396691a2000-01-20 22:44:08 +11001865 * SSH1 key exchange
Damien Miller95def091999-11-25 00:26:21 +11001866 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001867static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001868do_ssh1_kex(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001869{
Damien Miller95def091999-11-25 00:26:21 +11001870 int i, len;
Damien Miller7650bc62001-01-30 09:27:26 +11001871 int rsafail = 0;
Damien Miller95def091999-11-25 00:26:21 +11001872 BIGNUM *session_key_int;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001873 u_char session_key[SSH_SESSION_KEY_LENGTH];
1874 u_char cookie[8];
1875 u_int cipher_type, auth_mask, protocol_flags;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001876 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001877
Damien Miller5428f641999-11-25 11:54:57 +11001878 /*
1879 * Generate check bytes that the client must send back in the user
1880 * packet in order for it to be accepted; this is used to defy ip
1881 * spoofing attacks. Note that this only works against somebody
1882 * doing IP spoofing from a remote machine; any machine on the local
1883 * network can still see outgoing packets and catch the random
1884 * cookie. This only affects rhosts authentication, and this is one
1885 * of the reasons why it is inherently insecure.
1886 */
Damien Miller95def091999-11-25 00:26:21 +11001887 for (i = 0; i < 8; i++) {
1888 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001889 rnd = arc4random();
1890 cookie[i] = rnd & 0xff;
1891 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +11001892 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001893
Damien Miller5428f641999-11-25 11:54:57 +11001894 /*
1895 * Send our public key. We include in the packet 64 bits of random
1896 * data that must be matched in the reply in order to prevent IP
1897 * spoofing.
1898 */
Damien Miller95def091999-11-25 00:26:21 +11001899 packet_start(SSH_SMSG_PUBLIC_KEY);
1900 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001901 packet_put_char(cookie[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001902
Damien Miller95def091999-11-25 00:26:21 +11001903 /* Store our public server RSA key. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001904 packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
1905 packet_put_bignum(sensitive_data.server_key->rsa->e);
1906 packet_put_bignum(sensitive_data.server_key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001907
Damien Miller95def091999-11-25 00:26:21 +11001908 /* Store our public host RSA key. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001909 packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1910 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
1911 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001912
Damien Miller95def091999-11-25 00:26:21 +11001913 /* Put protocol flags. */
1914 packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001915
Damien Miller95def091999-11-25 00:26:21 +11001916 /* Declare which ciphers we support. */
Damien Miller874d77b2000-10-14 16:23:11 +11001917 packet_put_int(cipher_mask_ssh1(0));
Damien Miller95def091999-11-25 00:26:21 +11001918
1919 /* Declare supported authentication types. */
1920 auth_mask = 0;
Damien Miller95def091999-11-25 00:26:21 +11001921 if (options.rhosts_rsa_authentication)
1922 auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
1923 if (options.rsa_authentication)
1924 auth_mask |= 1 << SSH_AUTH_RSA;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001925 if (options.challenge_response_authentication == 1)
Damien Miller95def091999-11-25 00:26:21 +11001926 auth_mask |= 1 << SSH_AUTH_TIS;
Damien Miller95def091999-11-25 00:26:21 +11001927 if (options.password_authentication)
1928 auth_mask |= 1 << SSH_AUTH_PASSWORD;
1929 packet_put_int(auth_mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001930
Damien Miller95def091999-11-25 00:26:21 +11001931 /* Send the packet and wait for it to be sent. */
1932 packet_send();
1933 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001934
Damien Miller0bc1bd82000-11-13 22:57:25 +11001935 debug("Sent %d bit server key and %d bit host key.",
1936 BN_num_bits(sensitive_data.server_key->rsa->n),
1937 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001938
Damien Miller95def091999-11-25 00:26:21 +11001939 /* Read clients reply (cipher type and session key). */
Damien Millerdff50992002-01-22 23:16:32 +11001940 packet_read_expect(SSH_CMSG_SESSION_KEY);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001941
Damien Miller50945fa1999-12-09 10:31:37 +11001942 /* Get cipher type and check whether we accept this. */
Damien Miller95def091999-11-25 00:26:21 +11001943 cipher_type = packet_get_char();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001944
Damien Miller874d77b2000-10-14 16:23:11 +11001945 if (!(cipher_mask_ssh1(0) & (1 << cipher_type)))
Damien Miller50945fa1999-12-09 10:31:37 +11001946 packet_disconnect("Warning: client selects unsupported cipher.");
1947
Damien Miller95def091999-11-25 00:26:21 +11001948 /* Get check bytes from the packet. These must match those we
1949 sent earlier with the public key packet. */
1950 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001951 if (cookie[i] != packet_get_char())
Damien Miller95def091999-11-25 00:26:21 +11001952 packet_disconnect("IP Spoofing check bytes do not match.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001953
Damien Miller95def091999-11-25 00:26:21 +11001954 debug("Encryption type: %.200s", cipher_name(cipher_type));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001955
Damien Miller95def091999-11-25 00:26:21 +11001956 /* Get the encrypted integer. */
Damien Millerda755162002-01-22 23:09:22 +11001957 if ((session_key_int = BN_new()) == NULL)
1958 fatal("do_ssh1_kex: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +11001959 packet_get_bignum(session_key_int);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001960
Damien Miller95def091999-11-25 00:26:21 +11001961 protocol_flags = packet_get_int();
1962 packet_set_protocol_flags(protocol_flags);
Damien Miller48b03fc2002-01-22 23:11:40 +11001963 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001964
Ben Lindstromabcb1452002-03-22 01:10:21 +00001965 /* Decrypt session_key_int using host/server keys */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001966 rsafail = PRIVSEP(ssh1_session_key(session_key_int));
1967
Damien Miller5428f641999-11-25 11:54:57 +11001968 /*
1969 * Extract session key from the decrypted integer. The key is in the
1970 * least significant 256 bits of the integer; the first byte of the
1971 * key is in the highest bits.
1972 */
Damien Miller7650bc62001-01-30 09:27:26 +11001973 if (!rsafail) {
1974 BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1975 len = BN_num_bytes(session_key_int);
Damien Millereccb9de2005-06-17 12:59:34 +10001976 if (len < 0 || (u_int)len > sizeof(session_key)) {
Damien Miller7650bc62001-01-30 09:27:26 +11001977 error("do_connection: bad session key len from %s: "
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001978 "session_key_int %d > sizeof(session_key) %lu",
1979 get_remote_ipaddr(), len, (u_long)sizeof(session_key));
Damien Miller7650bc62001-01-30 09:27:26 +11001980 rsafail++;
1981 } else {
1982 memset(session_key, 0, sizeof(session_key));
1983 BN_bn2bin(session_key_int,
1984 session_key + sizeof(session_key) - len);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001985
Darren Tuckere14e0052004-05-13 16:30:44 +10001986 derive_ssh1_session_id(
Darren Tuckerfc959702004-07-17 16:12:08 +10001987 sensitive_data.ssh1_host_key->rsa->n,
Darren Tuckere14e0052004-05-13 16:30:44 +10001988 sensitive_data.server_key->rsa->n,
1989 cookie, session_id);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001990 /*
1991 * Xor the first 16 bytes of the session key with the
1992 * session id.
1993 */
1994 for (i = 0; i < 16; i++)
1995 session_key[i] ^= session_id[i];
Damien Miller7650bc62001-01-30 09:27:26 +11001996 }
1997 }
1998 if (rsafail) {
Ben Lindstromeb648a72001-03-05 06:00:29 +00001999 int bytes = BN_num_bytes(session_key_int);
Ben Lindstrom13c5d3b2002-02-26 18:00:48 +00002000 u_char *buf = xmalloc(bytes);
Ben Lindstromeb648a72001-03-05 06:00:29 +00002001 MD5_CTX md;
2002
Damien Miller996acd22003-04-09 20:59:48 +10002003 logit("do_connection: generating a fake encryption key");
Ben Lindstromeb648a72001-03-05 06:00:29 +00002004 BN_bn2bin(session_key_int, buf);
2005 MD5_Init(&md);
2006 MD5_Update(&md, buf, bytes);
2007 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
2008 MD5_Final(session_key, &md);
2009 MD5_Init(&md);
2010 MD5_Update(&md, session_key, 16);
2011 MD5_Update(&md, buf, bytes);
2012 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
2013 MD5_Final(session_key + 16, &md);
2014 memset(buf, 0, bytes);
2015 xfree(buf);
Ben Lindstrom941ac822001-03-05 06:25:23 +00002016 for (i = 0; i < 16; i++)
2017 session_id[i] = session_key[i] ^ session_key[i + 16];
Damien Miller7650bc62001-01-30 09:27:26 +11002018 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002019 /* Destroy the private and public keys. No longer. */
Damien Miller3a5b0232002-03-13 13:19:42 +11002020 destroy_sensitive_data();
Ben Lindstromeb648a72001-03-05 06:00:29 +00002021
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002022 if (use_privsep)
2023 mm_ssh1_session_id(session_id);
2024
Damien Miller396691a2000-01-20 22:44:08 +11002025 /* Destroy the decrypted integer. It is no longer needed. */
2026 BN_clear_free(session_key_int);
2027
Damien Miller95def091999-11-25 00:26:21 +11002028 /* Set the session key. From this on all communications will be encrypted. */
2029 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002030
Damien Miller95def091999-11-25 00:26:21 +11002031 /* Destroy our copy of the session key. It is no longer needed. */
2032 memset(session_key, 0, sizeof(session_key));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002033
Damien Miller95def091999-11-25 00:26:21 +11002034 debug("Received session key; encryption turned on.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002035
Ben Lindstromf666fec2002-06-06 19:51:58 +00002036 /* Send an acknowledgment packet. Note that this packet is sent encrypted. */
Damien Miller95def091999-11-25 00:26:21 +11002037 packet_start(SSH_SMSG_SUCCESS);
2038 packet_send();
2039 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002040}
Damien Millerefb4afe2000-04-12 18:45:05 +10002041
2042/*
2043 * SSH2 key exchange: diffie-hellman-group1-sha1
2044 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002045static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00002046do_ssh2_kex(void)
Damien Millerefb4afe2000-04-12 18:45:05 +10002047{
Damien Millerefb4afe2000-04-12 18:45:05 +10002048 Kex *kex;
Damien Millerefb4afe2000-04-12 18:45:05 +10002049
Damien Miller78928792000-04-12 20:17:38 +10002050 if (options.ciphers != NULL) {
Damien Miller4af51302000-04-16 11:18:38 +10002051 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Damien Miller78928792000-04-12 20:17:38 +10002052 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
2053 }
Damien Millera0ff4662001-03-30 10:49:35 +10002054 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
2055 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
2056 myproposal[PROPOSAL_ENC_ALGS_STOC] =
2057 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
2058
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00002059 if (options.macs != NULL) {
2060 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
2061 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
2062 }
Damien Miller9786e6e2005-07-26 21:54:56 +10002063 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +00002064 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
2065 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
Damien Miller9786e6e2005-07-26 21:54:56 +10002066 } else if (options.compression == COMP_DELAYED) {
2067 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
2068 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com";
Ben Lindstrom23e0f662002-06-21 01:09:47 +00002069 }
Damien Millerf0b15df2006-03-26 13:59:20 +11002070
Damien Miller0bc1bd82000-11-13 22:57:25 +11002071 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = list_hostkey_types();
2072
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002073 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +00002074 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +11002075 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10002076 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Miller8e7fb332003-02-24 12:03:03 +11002077 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11002078 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00002079 kex->server = 1;
2080 kex->client_version_string=client_version_string;
2081 kex->server_version_string=server_version_string;
2082 kex->load_host_key=&get_hostkey_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002083 kex->host_key_index=&get_hostkey_index;
Damien Millerefb4afe2000-04-12 18:45:05 +10002084
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002085 xxx_kex = kex;
2086
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002087 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +11002088
Ben Lindstrom2d90e002001-04-04 02:00:54 +00002089 session_id2 = kex->session_id;
2090 session_id2_len = kex->session_id_len;
2091
Damien Miller874d77b2000-10-14 16:23:11 +11002092#ifdef DEBUG_KEXDH
2093 /* send 1st encrypted/maced/compressed message */
2094 packet_start(SSH2_MSG_IGNORE);
2095 packet_put_cstring("markus");
2096 packet_send();
2097 packet_write_wait();
2098#endif
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00002099 debug("KEX done");
Damien Millerefb4afe2000-04-12 18:45:05 +10002100}
Darren Tucker3e33cec2003-10-02 16:12:36 +10002101
2102/* server specific fatal cleanup */
2103void
2104cleanup_exit(int i)
2105{
2106 if (the_authctxt)
2107 do_cleanup(the_authctxt);
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11002108#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11002109 /* done after do_cleanup so it can cancel the PAM auth 'thread' */
2110 if (!use_privsep || mm_is_monitor())
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11002111 audit_event(SSH_CONNECTION_ABANDON);
Darren Tucker269a1ea2005-02-03 00:20:53 +11002112#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002113 _exit(i);
2114}