blob: 497525df87158a581b5f7b532f4868aeeeb9e879 [file] [log] [blame]
Darren Tucker45150472006-07-12 22:34:17 +10001/* $OpenBSD: sshd.c,v 1.337 2006/07/12 11:34:58 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11006 * This program is the ssh daemon. It listens for connections from clients,
7 * and performs authentication, executes use commands or shell, and forwards
Damien Miller95def091999-11-25 00:26:21 +11008 * information to/from the application to the user client over an encrypted
Damien Millere4340be2000-09-16 13:29:08 +11009 * connection. This can also handle forwarding of X11, TCP/IP, and
10 * authentication agent connections.
Damien Millerefb4afe2000-04-12 18:45:05 +100011 *
Damien Millere4340be2000-09-16 13:29:08 +110012 * As far as I am concerned, the code I have written for this software
13 * can be used freely for any purpose. Any derived versions of this
14 * software must be clearly marked as such, and if the derived work is
15 * incompatible with the protocol description in the RFC file, it must be
16 * called by a name other than "ssh" or "Secure Shell".
17 *
18 * SSH2 implementation:
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000019 * Privilege Separation:
Damien Millere4340be2000-09-16 13:29:08 +110020 *
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000021 * Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved.
22 * Copyright (c) 2002 Niels Provos. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110023 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
34 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
36 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
37 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
42 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110043 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110046
Damien Miller9cf6d072006-03-15 11:29:24 +110047#include <sys/types.h>
Damien Millerf17883e2006-03-15 11:45:54 +110048#ifdef HAVE_SYS_STAT_H
49# include <sys/stat.h>
50#endif
51#include <sys/ioctl.h>
Damien Millere3b60b52006-07-10 21:08:03 +100052#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110053#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110054
Darren Tucker39972492006-07-12 22:22:46 +100055#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100056#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110057#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110058#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110059#endif
Damien Millera1738e42006-07-10 21:33:04 +100060#include <grp.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100061#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110062#include <signal.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include <openssl/dh.h>
65#include <openssl/bn.h>
Damien Miller6a47f302002-02-13 13:55:06 +110066#include <openssl/md5.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000067#include <openssl/rand.h>
Kevin Steves0ea1d9d2002-04-25 18:17:04 +000068#ifdef HAVE_SECUREWARE
69#include <sys/security.h>
70#include <prot.h>
71#endif
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072
73#include "ssh.h"
74#include "ssh1.h"
75#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076#include "xmalloc.h"
77#include "rsa.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000078#include "sshpty.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081#include "servconf.h"
82#include "uidswap.h"
83#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100084#include "buffer.h"
Darren Tucker645ab752004-06-25 13:33:20 +100085#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000086#include "cipher.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100087#include "kex.h"
Damien Millerb38eff82000-04-01 11:09:21 +100088#include "key.h"
Damien Miller874d77b2000-10-14 16:23:11 +110089#include "dh.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100090#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100091#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "pathnames.h"
93#include "atomicio.h"
94#include "canohost.h"
95#include "auth.h"
96#include "misc.h"
Darren Tucker645ab752004-06-25 13:33:20 +100097#include "msg.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000098#include "dispatch.h"
Ben Lindstrom1bae4042001-10-03 17:46:39 +000099#include "channels.h"
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +0000100#include "session.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101#include "monitor_mm.h"
102#include "monitor.h"
103#include "monitor_wrap.h"
104#include "monitor_fdpass.h"
Damien Millerb7576772006-07-10 20:23:39 +1000105#include "version.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
107#ifdef LIBWRAP
108#include <tcpd.h>
109#include <syslog.h>
110int allow_severity = LOG_INFO;
111int deny_severity = LOG_WARNING;
112#endif /* LIBWRAP */
113
114#ifndef O_NOCTTY
115#define O_NOCTTY 0
116#endif
117
Damien Miller035a5b42004-06-26 08:16:31 +1000118/* Re-exec fds */
119#define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1)
120#define REEXEC_STARTUP_PIPE_FD (STDERR_FILENO + 2)
121#define REEXEC_CONFIG_PASS_FD (STDERR_FILENO + 3)
122#define REEXEC_MIN_FREE_FD (STDERR_FILENO + 4)
123
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000124extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000125
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126/* Server configuration options. */
127ServerOptions options;
128
129/* Name of the server configuration file. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000130char *config_file_name = _PATH_SERVER_CONFIG_FILE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
Damien Miller4af51302000-04-16 11:18:38 +1000132/*
Damien Miller95def091999-11-25 00:26:21 +1100133 * Debug mode flag. This can be set on the command line. If debug
134 * mode is enabled, extra debugging output will be sent to the system
135 * log, the daemon will not go to background, and will exit after processing
136 * the first connection.
137 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138int debug_flag = 0;
139
Ben Lindstrom794325a2001-08-06 21:09:07 +0000140/* Flag indicating that the daemon should only test the configuration and keys. */
141int test_flag = 0;
142
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143/* Flag indicating that the daemon is being started from inetd. */
144int inetd_flag = 0;
145
Ben Lindstromc72745a2000-12-02 19:03:54 +0000146/* Flag indicating that sshd should not detach and become a daemon. */
147int no_daemon_flag = 0;
148
Damien Miller5ce662a1999-11-11 17:57:39 +1100149/* debug goes to stderr unless inetd_flag is set */
150int log_stderr = 0;
151
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152/* Saved arguments to main(). */
153char **saved_argv;
Damien Millerb8c656e2000-06-28 15:22:41 +1000154int saved_argc;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Darren Tucker645ab752004-06-25 13:33:20 +1000156/* re-exec */
157int rexeced_flag = 0;
158int rexec_flag = 1;
159int rexec_argc = 0;
160char **rexec_argv;
161
Damien Miller5428f641999-11-25 11:54:57 +1100162/*
Damien Miller34132e52000-01-14 15:45:46 +1100163 * The sockets that the server is listening; this is used in the SIGHUP
164 * signal handler.
Damien Miller5428f641999-11-25 11:54:57 +1100165 */
Damien Miller34132e52000-01-14 15:45:46 +1100166#define MAX_LISTEN_SOCKS 16
167int listen_socks[MAX_LISTEN_SOCKS];
168int num_listen_socks = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169
Damien Miller5428f641999-11-25 11:54:57 +1100170/*
171 * the client's version string, passed by sshd2 in compat mode. if != NULL,
172 * sshd will skip the version-number exchange
173 */
Damien Miller95def091999-11-25 00:26:21 +1100174char *client_version_string = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000175char *server_version_string = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000177/* for rekeying XXX fixme */
178Kex *xxx_kex;
179
Damien Miller5428f641999-11-25 11:54:57 +1100180/*
181 * Any really sensitive data in the application is contained in this
182 * structure. The idea is that this structure could be locked into memory so
183 * that the pages do not get written into swap. However, there are some
184 * problems. The private key contains BIGNUMs, and we do not (in principle)
185 * have access to the internals of them, and locking just the structure is
186 * not very useful. Currently, memory locking is not implemented.
187 */
Damien Miller95def091999-11-25 00:26:21 +1100188struct {
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000189 Key *server_key; /* ephemeral server key */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100190 Key *ssh1_host_key; /* ssh1 host key */
191 Key **host_keys; /* all private host keys */
192 int have_ssh1_key;
193 int have_ssh2_key;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000194 u_char ssh1_cookie[SSH_SESSION_KEY_LENGTH];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195} sensitive_data;
196
Damien Miller5428f641999-11-25 11:54:57 +1100197/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000198 * Flag indicating whether the RSA server key needs to be regenerated.
199 * Is set in the SIGALRM handler and cleared when the key is regenerated.
Damien Miller5428f641999-11-25 11:54:57 +1100200 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000201static volatile sig_atomic_t key_do_regen = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000203/* This is set to true when a signal is received. */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000204static volatile sig_atomic_t received_sighup = 0;
205static volatile sig_atomic_t received_sigterm = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206
Damien Millerb38eff82000-04-01 11:09:21 +1000207/* session identifier, used by RSA-auth */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000208u_char session_id[16];
Damien Millerb38eff82000-04-01 11:09:21 +1000209
Damien Millereba71ba2000-04-29 23:57:08 +1000210/* same for ssh2 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000211u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000212u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000213
Damien Miller942da032000-08-18 13:59:06 +1000214/* record remote hostname or ip */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000215u_int utmp_len = MAXHOSTNAMELEN;
Damien Miller942da032000-08-18 13:59:06 +1000216
Ben Lindstromd84df982001-12-06 16:35:40 +0000217/* options.max_startup sized array of fd ints */
218int *startup_pipes = NULL;
219int startup_pipe; /* in child */
220
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000221/* variables used for privilege separation */
Darren Tucker45150472006-07-12 22:34:17 +1000222int use_privsep = -1;
Darren Tuckera8be9e22004-02-06 16:40:27 +1100223struct monitor *pmonitor = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000224
Darren Tucker3e33cec2003-10-02 16:12:36 +1000225/* global authentication context */
226Authctxt *the_authctxt = NULL;
227
Darren Tucker45150472006-07-12 22:34:17 +1000228/* sshd_config buffer */
229Buffer cfg;
230
Darren Tucker09991742004-07-17 17:05:14 +1000231/* message to be displayed after login */
232Buffer loginmsg;
233
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234/* Prototypes for various functions defined later in this file. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000235void destroy_sensitive_data(void);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000236void demote_sensitive_data(void);
Damien Miller98c7ad62000-03-09 21:27:49 +1100237
Ben Lindstrombba81212001-06-25 05:01:22 +0000238static void do_ssh1_kex(void);
239static void do_ssh2_kex(void);
Damien Miller874d77b2000-10-14 16:23:11 +1100240
Damien Miller98c7ad62000-03-09 21:27:49 +1100241/*
Damien Miller34132e52000-01-14 15:45:46 +1100242 * Close all listening sockets
243 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000244static void
Damien Miller34132e52000-01-14 15:45:46 +1100245close_listen_socks(void)
246{
247 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000248
Damien Miller34132e52000-01-14 15:45:46 +1100249 for (i = 0; i < num_listen_socks; i++)
250 close(listen_socks[i]);
251 num_listen_socks = -1;
252}
253
Ben Lindstromd84df982001-12-06 16:35:40 +0000254static void
255close_startup_pipes(void)
256{
257 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000258
Ben Lindstromd84df982001-12-06 16:35:40 +0000259 if (startup_pipes)
260 for (i = 0; i < options.max_startups; i++)
261 if (startup_pipes[i] != -1)
262 close(startup_pipes[i]);
263}
264
Damien Miller34132e52000-01-14 15:45:46 +1100265/*
Damien Miller95def091999-11-25 00:26:21 +1100266 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
267 * the effect is to reread the configuration file (and to regenerate
268 * the server key).
269 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100270
271/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000272static void
Damien Miller95def091999-11-25 00:26:21 +1100273sighup_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274{
Ben Lindstrom07958482001-12-06 16:19:01 +0000275 int save_errno = errno;
276
Damien Miller95def091999-11-25 00:26:21 +1100277 received_sighup = 1;
278 signal(SIGHUP, sighup_handler);
Ben Lindstrom07958482001-12-06 16:19:01 +0000279 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280}
281
Damien Miller95def091999-11-25 00:26:21 +1100282/*
283 * Called from the main program after receiving SIGHUP.
284 * Restarts the server.
285 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000286static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000287sighup_restart(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288{
Damien Miller996acd22003-04-09 20:59:48 +1000289 logit("Received SIGHUP; restarting.");
Damien Miller34132e52000-01-14 15:45:46 +1100290 close_listen_socks();
Ben Lindstromd84df982001-12-06 16:35:40 +0000291 close_startup_pipes();
Damien Miller95def091999-11-25 00:26:21 +1100292 execv(saved_argv[0], saved_argv);
Damien Miller996acd22003-04-09 20:59:48 +1000293 logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
Ben Lindstrom822b6342002-06-23 21:38:49 +0000294 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100295 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296}
297
Damien Miller95def091999-11-25 00:26:21 +1100298/*
299 * Generic signal handler for terminating signals in the master daemon.
Damien Miller95def091999-11-25 00:26:21 +1100300 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100301/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000302static void
Damien Miller95def091999-11-25 00:26:21 +1100303sigterm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000305 received_sigterm = sig;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306}
307
Damien Miller95def091999-11-25 00:26:21 +1100308/*
309 * SIGCHLD handler. This is called whenever a child dies. This will then
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000310 * reap any zombies left by exited children.
Damien Miller95def091999-11-25 00:26:21 +1100311 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100312/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000313static void
Damien Miller95def091999-11-25 00:26:21 +1100314main_sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315{
Damien Miller95def091999-11-25 00:26:21 +1100316 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000317 pid_t pid;
Damien Miller95def091999-11-25 00:26:21 +1100318 int status;
Damien Miller431f66b1999-11-21 18:31:57 +1100319
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000320 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
321 (pid < 0 && errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100322 ;
Damien Miller431f66b1999-11-21 18:31:57 +1100323
Damien Miller95def091999-11-25 00:26:21 +1100324 signal(SIGCHLD, main_sigchld_handler);
325 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326}
327
Damien Miller95def091999-11-25 00:26:21 +1100328/*
329 * Signal handler for the alarm after the login grace period has expired.
330 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100331/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000332static void
Damien Miller95def091999-11-25 00:26:21 +1100333grace_alarm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000335 /* XXX no idea how fix this signal handler */
336
Darren Tuckera8be9e22004-02-06 16:40:27 +1100337 if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0)
338 kill(pmonitor->m_pid, SIGALRM);
339
Damien Miller95def091999-11-25 00:26:21 +1100340 /* Log error and exit. */
Damien Millerd27a76d2002-09-27 13:22:31 +1000341 fatal("Timeout before authentication for %s", get_remote_ipaddr());
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342}
343
Damien Miller95def091999-11-25 00:26:21 +1100344/*
Damien Miller95def091999-11-25 00:26:21 +1100345 * Signal handler for the key regeneration alarm. Note that this
346 * alarm only occurs in the daemon waiting for connections, and it does not
347 * do anything with the private key or random state before forking.
348 * Thus there should be no concurrency control/asynchronous execution
349 * problems.
350 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000351static void
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000352generate_ephemeral_server_key(void)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100353{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000354 u_int32_t rnd = 0;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000355 int i;
356
Ben Lindstroma3700052001-04-05 23:26:32 +0000357 verbose("Generating %s%d bit RSA key.",
Damien Millerff75ac42001-03-30 10:50:32 +1000358 sensitive_data.server_key ? "new " : "", options.server_key_bits);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100359 if (sensitive_data.server_key != NULL)
360 key_free(sensitive_data.server_key);
Ben Lindstroma3700052001-04-05 23:26:32 +0000361 sensitive_data.server_key = key_generate(KEY_RSA1,
Damien Millerff75ac42001-03-30 10:50:32 +1000362 options.server_key_bits);
363 verbose("RSA key generation complete.");
Ben Lindstromeb648a72001-03-05 06:00:29 +0000364
365 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
366 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000367 rnd = arc4random();
368 sensitive_data.ssh1_cookie[i] = rnd & 0xff;
369 rnd >>= 8;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000370 }
371 arc4random_stir();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100372}
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000373
Damien Millerf0b15df2006-03-26 13:59:20 +1100374/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000375static void
Damien Miller95def091999-11-25 00:26:21 +1100376key_regeneration_alarm(int sig)
377{
378 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000379
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000380 signal(SIGALRM, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +1100381 errno = save_errno;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000382 key_do_regen = 1;
Damien Miller95def091999-11-25 00:26:21 +1100383}
384
Ben Lindstrombba81212001-06-25 05:01:22 +0000385static void
Damien Millerb38eff82000-04-01 11:09:21 +1000386sshd_exchange_identification(int sock_in, int sock_out)
387{
Damien Millereccb9de2005-06-17 12:59:34 +1000388 u_int i;
389 int mismatch;
Damien Millerb38eff82000-04-01 11:09:21 +1000390 int remote_major, remote_minor;
Damien Miller78928792000-04-12 20:17:38 +1000391 int major, minor;
Damien Millerb38eff82000-04-01 11:09:21 +1000392 char *s;
393 char buf[256]; /* Must not be larger than remote_version. */
394 char remote_version[256]; /* Must be at least as big as buf. */
395
Damien Miller78928792000-04-12 20:17:38 +1000396 if ((options.protocol & SSH_PROTO_1) &&
397 (options.protocol & SSH_PROTO_2)) {
398 major = PROTOCOL_MAJOR_1;
399 minor = 99;
400 } else if (options.protocol & SSH_PROTO_2) {
401 major = PROTOCOL_MAJOR_2;
402 minor = PROTOCOL_MINOR_2;
403 } else {
404 major = PROTOCOL_MAJOR_1;
405 minor = PROTOCOL_MINOR_1;
406 }
407 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
Damien Millerb38eff82000-04-01 11:09:21 +1000408 server_version_string = xstrdup(buf);
409
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000410 /* Send our protocol version identification. */
411 if (atomicio(vwrite, sock_out, server_version_string,
412 strlen(server_version_string))
413 != strlen(server_version_string)) {
414 logit("Could not write ident string to %s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000415 cleanup_exit(255);
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000416 }
417
418 /* Read other sides version identification. */
419 memset(buf, 0, sizeof(buf));
420 for (i = 0; i < sizeof(buf) - 1; i++) {
421 if (atomicio(read, sock_in, &buf[i], 1) != 1) {
422 logit("Did not receive identification string from %s",
423 get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000424 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000425 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000426 if (buf[i] == '\r') {
427 buf[i] = 0;
428 /* Kludge for F-Secure Macintosh < 1.0.2 */
429 if (i == 12 &&
430 strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000431 break;
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000432 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000433 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000434 if (buf[i] == '\n') {
435 buf[i] = 0;
436 break;
437 }
Damien Millerb38eff82000-04-01 11:09:21 +1000438 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000439 buf[sizeof(buf) - 1] = 0;
440 client_version_string = xstrdup(buf);
Damien Millerb38eff82000-04-01 11:09:21 +1000441
442 /*
443 * Check that the versions match. In future this might accept
444 * several versions and set appropriate flags to handle them.
445 */
446 if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
447 &remote_major, &remote_minor, remote_version) != 3) {
Damien Miller4af51302000-04-16 11:18:38 +1000448 s = "Protocol mismatch.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000449 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Millerb38eff82000-04-01 11:09:21 +1000450 close(sock_in);
451 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000452 logit("Bad protocol version identification '%.100s' from %s",
Damien Millerb38eff82000-04-01 11:09:21 +1000453 client_version_string, get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000454 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000455 }
456 debug("Client protocol version %d.%d; client software version %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100457 remote_major, remote_minor, remote_version);
Damien Millerb38eff82000-04-01 11:09:21 +1000458
Damien Millerefb4afe2000-04-12 18:45:05 +1000459 compat_datafellows(remote_version);
460
Damien Millere9264972002-09-30 11:59:21 +1000461 if (datafellows & SSH_BUG_PROBE) {
Damien Miller996acd22003-04-09 20:59:48 +1000462 logit("probed from %s with %s. Don't panic.",
Damien Millere9264972002-09-30 11:59:21 +1000463 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000464 cleanup_exit(255);
Damien Millere9264972002-09-30 11:59:21 +1000465 }
466
Damien Miller27dbe6f2001-03-19 22:36:20 +1100467 if (datafellows & SSH_BUG_SCANNER) {
Damien Miller996acd22003-04-09 20:59:48 +1000468 logit("scanned from %s with %s. Don't panic.",
Damien Miller27dbe6f2001-03-19 22:36:20 +1100469 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000470 cleanup_exit(255);
Damien Miller27dbe6f2001-03-19 22:36:20 +1100471 }
472
Damien Miller78928792000-04-12 20:17:38 +1000473 mismatch = 0;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000474 switch (remote_major) {
Damien Millerb38eff82000-04-01 11:09:21 +1000475 case 1:
Damien Millereba71ba2000-04-29 23:57:08 +1000476 if (remote_minor == 99) {
477 if (options.protocol & SSH_PROTO_2)
478 enable_compat20();
479 else
480 mismatch = 1;
481 break;
482 }
Damien Miller78928792000-04-12 20:17:38 +1000483 if (!(options.protocol & SSH_PROTO_1)) {
484 mismatch = 1;
485 break;
486 }
Damien Millerb38eff82000-04-01 11:09:21 +1000487 if (remote_minor < 3) {
Damien Miller37023962000-07-11 17:31:38 +1000488 packet_disconnect("Your ssh version is too old and "
Damien Millerb38eff82000-04-01 11:09:21 +1000489 "is no longer supported. Please install a newer version.");
490 } else if (remote_minor == 3) {
491 /* note that this disables agent-forwarding */
492 enable_compat13();
493 }
Damien Miller78928792000-04-12 20:17:38 +1000494 break;
Damien Millerefb4afe2000-04-12 18:45:05 +1000495 case 2:
Damien Miller78928792000-04-12 20:17:38 +1000496 if (options.protocol & SSH_PROTO_2) {
Damien Millerefb4afe2000-04-12 18:45:05 +1000497 enable_compat20();
498 break;
499 }
500 /* FALLTHROUGH */
Damien Miller4af51302000-04-16 11:18:38 +1000501 default:
Damien Miller78928792000-04-12 20:17:38 +1000502 mismatch = 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000503 break;
504 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000505 chop(server_version_string);
Damien Miller78928792000-04-12 20:17:38 +1000506 debug("Local version string %.200s", server_version_string);
507
508 if (mismatch) {
509 s = "Protocol major versions differ.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000510 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Miller78928792000-04-12 20:17:38 +1000511 close(sock_in);
512 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000513 logit("Protocol major versions differ for %s: %.200s vs. %.200s",
Damien Miller78928792000-04-12 20:17:38 +1000514 get_remote_ipaddr(),
515 server_version_string, client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000516 cleanup_exit(255);
Damien Miller78928792000-04-12 20:17:38 +1000517 }
Damien Millereba71ba2000-04-29 23:57:08 +1000518}
519
Damien Miller0bc1bd82000-11-13 22:57:25 +1100520/* Destroy the host and server keys. They will no longer be needed. */
Damien Millereba71ba2000-04-29 23:57:08 +1000521void
522destroy_sensitive_data(void)
523{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100524 int i;
525
526 if (sensitive_data.server_key) {
527 key_free(sensitive_data.server_key);
528 sensitive_data.server_key = NULL;
529 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100530 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100531 if (sensitive_data.host_keys[i]) {
532 key_free(sensitive_data.host_keys[i]);
533 sensitive_data.host_keys[i] = NULL;
534 }
535 }
536 sensitive_data.ssh1_host_key = NULL;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000537 memset(sensitive_data.ssh1_cookie, 0, SSH_SESSION_KEY_LENGTH);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100538}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100539
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000540/* Demote private to public keys for network child */
541void
542demote_sensitive_data(void)
543{
544 Key *tmp;
545 int i;
546
547 if (sensitive_data.server_key) {
548 tmp = key_demote(sensitive_data.server_key);
549 key_free(sensitive_data.server_key);
550 sensitive_data.server_key = tmp;
551 }
552
553 for (i = 0; i < options.num_host_key_files; i++) {
554 if (sensitive_data.host_keys[i]) {
555 tmp = key_demote(sensitive_data.host_keys[i]);
556 key_free(sensitive_data.host_keys[i]);
557 sensitive_data.host_keys[i] = tmp;
558 if (tmp->type == KEY_RSA1)
559 sensitive_data.ssh1_host_key = tmp;
560 }
561 }
562
563 /* We do not clear ssh1_host key and cookie. XXX - Okay Niels? */
564}
565
Ben Lindstrom08105192002-03-22 02:50:06 +0000566static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000567privsep_preauth_child(void)
568{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000569 u_int32_t rnd[256];
Ben Lindstrom810af962002-07-04 00:11:40 +0000570 gid_t gidset[1];
Ben Lindstromc7431342002-03-22 03:11:49 +0000571 struct passwd *pw;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000572 int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000573
574 /* Enable challenge-response authentication for privilege separation */
575 privsep_challenge_enable();
576
577 for (i = 0; i < 256; i++)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000578 rnd[i] = arc4random();
579 RAND_seed(rnd, sizeof(rnd));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000580
581 /* Demote the private keys to public keys. */
582 demote_sensitive_data();
583
Ben Lindstromc7431342002-03-22 03:11:49 +0000584 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
Damien Miller0150c652002-04-24 09:49:09 +1000585 fatal("Privilege separation user %s does not exist",
586 SSH_PRIVSEP_USER);
Ben Lindstromc7431342002-03-22 03:11:49 +0000587 memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
588 endpwent();
589
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000590 /* Change our root directory */
Ben Lindstrom7a7edf72002-03-22 02:42:37 +0000591 if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
592 fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
593 strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594 if (chdir("/") == -1)
Ben Lindstrom1ee9ec32002-03-22 03:14:45 +0000595 fatal("chdir(\"/\"): %s", strerror(errno));
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000596
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597 /* Drop our privileges */
Ben Lindstromc7431342002-03-22 03:11:49 +0000598 debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
599 (u_int)pw->pw_gid);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000600#if 0
Darren Tuckerd5920482004-02-29 20:11:30 +1100601 /* XXX not ready, too heavy after chroot */
Ben Lindstromc7431342002-03-22 03:11:49 +0000602 do_setusercontext(pw);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000603#else
604 gidset[0] = pw->pw_gid;
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000605 if (setgroups(1, gidset) < 0)
606 fatal("setgroups: %.100s", strerror(errno));
607 permanently_set_uid(pw);
608#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609}
610
Darren Tucker3e33cec2003-10-02 16:12:36 +1000611static int
612privsep_preauth(Authctxt *authctxt)
Ben Lindstrom943481c2002-03-22 03:43:46 +0000613{
Ben Lindstrom943481c2002-03-22 03:43:46 +0000614 int status;
615 pid_t pid;
616
617 /* Set up unprivileged child process to deal with network data */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000618 pmonitor = monitor_init();
Ben Lindstrom943481c2002-03-22 03:43:46 +0000619 /* Store a pointer to the kex for later rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000620 pmonitor->m_pkex = &xxx_kex;
Ben Lindstrom943481c2002-03-22 03:43:46 +0000621
622 pid = fork();
623 if (pid == -1) {
624 fatal("fork of unprivileged child failed");
625 } else if (pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000626 debug2("Network child is on pid %ld", (long)pid);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000627
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000628 close(pmonitor->m_recvfd);
Darren Tuckera8be9e22004-02-06 16:40:27 +1100629 pmonitor->m_pid = pid;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000630 monitor_child_preauth(authctxt, pmonitor);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000631 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000632
633 /* Sync memory */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000634 monitor_sync(pmonitor);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000635
636 /* Wait for the child's exit status */
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000637 while (waitpid(pid, &status, 0) < 0)
638 if (errno != EINTR)
639 break;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000640 return (1);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000641 } else {
642 /* child */
643
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000644 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000645
646 /* Demote the child */
647 if (getuid() == 0 || geteuid() == 0)
648 privsep_preauth_child();
Ben Lindstromf90f58d2002-03-26 01:53:03 +0000649 setproctitle("%s", "[net]");
Ben Lindstrom943481c2002-03-22 03:43:46 +0000650 }
Darren Tucker3e33cec2003-10-02 16:12:36 +1000651 return (0);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000652}
653
Ben Lindstrom08105192002-03-22 02:50:06 +0000654static void
Ben Lindstrom943481c2002-03-22 03:43:46 +0000655privsep_postauth(Authctxt *authctxt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000656{
Tim Rice9dd30812002-07-07 13:43:36 -0700657#ifdef DISABLE_FD_PASSING
Tim Rice8eff3192002-06-25 15:35:15 -0700658 if (1) {
659#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000660 if (authctxt->pw->pw_uid == 0 || options.use_login) {
Tim Rice8eff3192002-06-25 15:35:15 -0700661#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000662 /* File descriptor passing is broken or root login */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663 use_privsep = 0;
Darren Tucker45b01422005-10-03 18:20:00 +1000664 goto skip;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000665 }
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000666
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000667 /* New socket pair */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000668 monitor_reinit(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000670 pmonitor->m_pid = fork();
671 if (pmonitor->m_pid == -1)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000672 fatal("fork of unprivileged child failed");
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000673 else if (pmonitor->m_pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000674 debug2("User child is on pid %ld", (long)pmonitor->m_pid);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000675 close(pmonitor->m_recvfd);
Darren Tuckereb578622004-08-12 23:08:14 +1000676 buffer_clear(&loginmsg);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000677 monitor_child_postauth(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000678
679 /* NEVERREACHED */
680 exit(0);
681 }
682
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000683 close(pmonitor->m_sendfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000684
685 /* Demote the private keys to public keys. */
686 demote_sensitive_data();
687
688 /* Drop privileges */
689 do_setusercontext(authctxt->pw);
690
Darren Tucker45b01422005-10-03 18:20:00 +1000691 skip:
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000692 /* It is safe now to apply the key state */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000693 monitor_apply_keystate(pmonitor);
Damien Miller9786e6e2005-07-26 21:54:56 +1000694
695 /*
696 * Tell the packet layer that authentication was successful, since
697 * this information is not part of the key state.
698 */
699 packet_set_authenticated();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000700}
701
Ben Lindstrombba81212001-06-25 05:01:22 +0000702static char *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100703list_hostkey_types(void)
704{
Damien Miller0e3b8722002-01-22 23:26:38 +1100705 Buffer b;
Damien Millerf58b58c2003-11-17 21:18:23 +1100706 const char *p;
707 char *ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100708 int i;
Damien Miller0e3b8722002-01-22 23:26:38 +1100709
710 buffer_init(&b);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100711 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100712 Key *key = sensitive_data.host_keys[i];
713 if (key == NULL)
714 continue;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000715 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100716 case KEY_RSA:
717 case KEY_DSA:
Damien Miller0e3b8722002-01-22 23:26:38 +1100718 if (buffer_len(&b) > 0)
719 buffer_append(&b, ",", 1);
720 p = key_ssh_name(key);
721 buffer_append(&b, p, strlen(p));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100722 break;
723 }
724 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100725 buffer_append(&b, "\0", 1);
Damien Millerf58b58c2003-11-17 21:18:23 +1100726 ret = xstrdup(buffer_ptr(&b));
Damien Miller0e3b8722002-01-22 23:26:38 +1100727 buffer_free(&b);
Damien Millerf58b58c2003-11-17 21:18:23 +1100728 debug("list_hostkey_types: %s", ret);
729 return ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100730}
731
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000732Key *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100733get_hostkey_by_type(int type)
734{
735 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000736
Damien Miller9f0f5c62001-12-21 14:45:46 +1100737 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100738 Key *key = sensitive_data.host_keys[i];
739 if (key != NULL && key->type == type)
740 return key;
741 }
742 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000743}
744
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000745Key *
746get_hostkey_by_index(int ind)
747{
748 if (ind < 0 || ind >= options.num_host_key_files)
749 return (NULL);
750 return (sensitive_data.host_keys[ind]);
751}
752
753int
754get_hostkey_index(Key *key)
755{
756 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000757
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000758 for (i = 0; i < options.num_host_key_files; i++) {
759 if (key == sensitive_data.host_keys[i])
760 return (i);
761 }
762 return (-1);
763}
764
Damien Miller942da032000-08-18 13:59:06 +1000765/*
766 * returns 1 if connection should be dropped, 0 otherwise.
767 * dropping starts at connection #max_startups_begin with a probability
768 * of (max_startups_rate/100). the probability increases linearly until
769 * all connections are dropped for startups > max_startups
770 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000771static int
Damien Miller942da032000-08-18 13:59:06 +1000772drop_connection(int startups)
773{
Darren Tucker178fa662004-11-05 20:09:09 +1100774 int p, r;
Damien Miller942da032000-08-18 13:59:06 +1000775
776 if (startups < options.max_startups_begin)
777 return 0;
778 if (startups >= options.max_startups)
779 return 1;
780 if (options.max_startups_rate == 100)
781 return 1;
782
783 p = 100 - options.max_startups_rate;
784 p *= startups - options.max_startups_begin;
Darren Tucker178fa662004-11-05 20:09:09 +1100785 p /= options.max_startups - options.max_startups_begin;
Damien Miller942da032000-08-18 13:59:06 +1000786 p += options.max_startups_rate;
Darren Tucker178fa662004-11-05 20:09:09 +1100787 r = arc4random() % 100;
Damien Miller942da032000-08-18 13:59:06 +1000788
Darren Tucker3269b132004-11-05 20:20:59 +1100789 debug("drop_connection: p %d, r %d", p, r);
Damien Miller942da032000-08-18 13:59:06 +1000790 return (r < p) ? 1 : 0;
791}
792
Ben Lindstromade03f62001-12-06 18:22:17 +0000793static void
794usage(void)
795{
Damien Miller0c889cd2004-03-22 09:36:00 +1100796 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000797 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Millerb4087862004-03-22 09:35:21 +1100798 fprintf(stderr,
799"usage: sshd [-46Ddeiqt] [-b bits] [-f config_file] [-g login_grace_time]\n"
800" [-h host_key_file] [-k key_gen_time] [-o option] [-p port] [-u len]\n"
801 );
Ben Lindstromade03f62001-12-06 18:22:17 +0000802 exit(1);
803}
804
Darren Tucker645ab752004-06-25 13:33:20 +1000805static void
806send_rexec_state(int fd, Buffer *conf)
807{
808 Buffer m;
809
810 debug3("%s: entering fd = %d config len %d", __func__, fd,
811 buffer_len(conf));
812
813 /*
814 * Protocol from reexec master to child:
815 * string configuration
816 * u_int ephemeral_key_follows
817 * bignum e (only if ephemeral_key_follows == 1)
818 * bignum n "
819 * bignum d "
820 * bignum iqmp "
821 * bignum p "
822 * bignum q "
Darren Tuckerc6f82192005-09-27 22:46:32 +1000823 * string rngseed (only if OpenSSL is not self-seeded)
Darren Tucker645ab752004-06-25 13:33:20 +1000824 */
825 buffer_init(&m);
826 buffer_put_cstring(&m, buffer_ptr(conf));
827
Darren Tuckerfc959702004-07-17 16:12:08 +1000828 if (sensitive_data.server_key != NULL &&
Darren Tucker645ab752004-06-25 13:33:20 +1000829 sensitive_data.server_key->type == KEY_RSA1) {
830 buffer_put_int(&m, 1);
831 buffer_put_bignum(&m, sensitive_data.server_key->rsa->e);
832 buffer_put_bignum(&m, sensitive_data.server_key->rsa->n);
833 buffer_put_bignum(&m, sensitive_data.server_key->rsa->d);
834 buffer_put_bignum(&m, sensitive_data.server_key->rsa->iqmp);
835 buffer_put_bignum(&m, sensitive_data.server_key->rsa->p);
836 buffer_put_bignum(&m, sensitive_data.server_key->rsa->q);
837 } else
838 buffer_put_int(&m, 0);
839
Darren Tuckerc6f82192005-09-27 22:46:32 +1000840#ifndef OPENSSL_PRNG_ONLY
841 rexec_send_rng_seed(&m);
842#endif
843
Darren Tucker645ab752004-06-25 13:33:20 +1000844 if (ssh_msg_send(fd, 0, &m) == -1)
845 fatal("%s: ssh_msg_send failed", __func__);
846
847 buffer_free(&m);
848
849 debug3("%s: done", __func__);
850}
851
852static void
853recv_rexec_state(int fd, Buffer *conf)
854{
855 Buffer m;
856 char *cp;
857 u_int len;
858
859 debug3("%s: entering fd = %d", __func__, fd);
860
861 buffer_init(&m);
862
863 if (ssh_msg_recv(fd, &m) == -1)
864 fatal("%s: ssh_msg_recv failed", __func__);
865 if (buffer_get_char(&m) != 0)
866 fatal("%s: rexec version mismatch", __func__);
867
868 cp = buffer_get_string(&m, &len);
869 if (conf != NULL)
870 buffer_append(conf, cp, len + 1);
871 xfree(cp);
872
873 if (buffer_get_int(&m)) {
874 if (sensitive_data.server_key != NULL)
875 key_free(sensitive_data.server_key);
876 sensitive_data.server_key = key_new_private(KEY_RSA1);
877 buffer_get_bignum(&m, sensitive_data.server_key->rsa->e);
878 buffer_get_bignum(&m, sensitive_data.server_key->rsa->n);
879 buffer_get_bignum(&m, sensitive_data.server_key->rsa->d);
880 buffer_get_bignum(&m, sensitive_data.server_key->rsa->iqmp);
881 buffer_get_bignum(&m, sensitive_data.server_key->rsa->p);
882 buffer_get_bignum(&m, sensitive_data.server_key->rsa->q);
883 rsa_generate_additional_parameters(
884 sensitive_data.server_key->rsa);
885 }
Darren Tuckerc6f82192005-09-27 22:46:32 +1000886
887#ifndef OPENSSL_PRNG_ONLY
888 rexec_recv_rng_seed(&m);
889#endif
890
Darren Tucker645ab752004-06-25 13:33:20 +1000891 buffer_free(&m);
892
893 debug3("%s: done", __func__);
894}
895
Damien Miller95def091999-11-25 00:26:21 +1100896/*
897 * Main program for the daemon.
898 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899int
900main(int ac, char **av)
901{
Damien Miller95def091999-11-25 00:26:21 +1100902 extern char *optarg;
903 extern int optind;
Damien Miller07d86be2006-03-26 14:19:21 +1100904 int opt, j, i, on = 1;
Damien Miller386c6a22004-06-30 22:40:20 +1000905 int sock_in = -1, sock_out = -1, newsock = -1;
Damien Miller166fca82000-04-20 07:42:21 +1000906 pid_t pid;
Damien Miller34132e52000-01-14 15:45:46 +1100907 socklen_t fromlen;
Damien Miller34132e52000-01-14 15:45:46 +1100908 fd_set *fdset;
909 struct sockaddr_storage from;
Damien Miller95def091999-11-25 00:26:21 +1100910 const char *remote_ip;
911 int remote_port;
Damien Miller95def091999-11-25 00:26:21 +1100912 FILE *f;
Damien Miller34132e52000-01-14 15:45:46 +1100913 struct addrinfo *ai;
914 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerb9997192003-12-17 16:29:22 +1100915 char *line;
Damien Miller34132e52000-01-14 15:45:46 +1100916 int listen_sock, maxfd;
Darren Tucker0f56ed12004-08-29 16:38:41 +1000917 int startup_p[2] = { -1 , -1 }, config_s[2] = { -1 , -1 };
Damien Miller37023962000-07-11 17:31:38 +1000918 int startups = 0;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000919 Key *key;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000920 Authctxt *authctxt;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000921 int ret, key_used = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000922
Kevin Steves0ea1d9d2002-04-25 18:17:04 +0000923#ifdef HAVE_SECUREWARE
924 (void)set_auth_parameters(ac, av);
925#endif
Damien Miller59d3d5b2003-08-22 09:34:41 +1000926 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000927 init_rng();
928
Damien Millera8ed44b2003-01-10 09:53:12 +1100929 /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
Damien Millerb8c656e2000-06-28 15:22:41 +1000930 saved_argc = ac;
Darren Tucker17c5d032004-06-25 14:22:23 +1000931 rexec_argc = ac;
Darren Tuckerd8093e42006-05-04 16:24:34 +1000932 saved_argv = xcalloc(ac + 1, sizeof(*saved_argv));
Damien Millera8ed44b2003-01-10 09:53:12 +1100933 for (i = 0; i < ac; i++)
934 saved_argv[i] = xstrdup(av[i]);
Damien Miller04cb5362003-05-15 21:29:10 +1000935 saved_argv[i] = NULL;
Damien Millera8ed44b2003-01-10 09:53:12 +1100936
937#ifndef HAVE_SETPROCTITLE
938 /* Prepare for later setproctitle emulation */
939 compat_init_setproctitle(ac, av);
Damien Millerf2e3e9d2003-06-02 12:15:54 +1000940 av = saved_argv;
Damien Millera8ed44b2003-01-10 09:53:12 +1100941#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942
Damien Millerbfba3542004-03-22 09:29:57 +1100943 if (geteuid() == 0 && setgroups(0, NULL) == -1)
944 debug("setgroups(): %.200s", strerror(errno));
945
Darren Tuckerce321d82005-10-03 18:11:24 +1000946 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
947 sanitise_stdfd();
948
Damien Miller95def091999-11-25 00:26:21 +1100949 /* Initialize configuration options to their default values. */
950 initialize_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951
Damien Miller95def091999-11-25 00:26:21 +1100952 /* Parse command-line arguments. */
Darren Tucker645ab752004-06-25 13:33:20 +1000953 while ((opt = getopt(ac, av, "f:p:b:k:h:g:u:o:dDeiqrtQR46")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100954 switch (opt) {
Damien Miller34132e52000-01-14 15:45:46 +1100955 case '4':
Darren Tucker0f383232005-01-20 10:57:56 +1100956 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100957 break;
958 case '6':
Darren Tucker0f383232005-01-20 10:57:56 +1100959 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100960 break;
Damien Miller95def091999-11-25 00:26:21 +1100961 case 'f':
962 config_file_name = optarg;
963 break;
964 case 'd':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000965 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100966 debug_flag = 1;
967 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000968 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
Damien Millere4340be2000-09-16 13:29:08 +1100969 options.log_level++;
Damien Miller95def091999-11-25 00:26:21 +1100970 break;
Ben Lindstromc72745a2000-12-02 19:03:54 +0000971 case 'D':
972 no_daemon_flag = 1;
973 break;
Ben Lindstrom9fce9f02001-04-11 23:10:09 +0000974 case 'e':
975 log_stderr = 1;
976 break;
Damien Miller95def091999-11-25 00:26:21 +1100977 case 'i':
978 inetd_flag = 1;
979 break;
Darren Tucker645ab752004-06-25 13:33:20 +1000980 case 'r':
981 rexec_flag = 0;
982 break;
983 case 'R':
984 rexeced_flag = 1;
985 inetd_flag = 1;
986 break;
Damien Miller95def091999-11-25 00:26:21 +1100987 case 'Q':
Ben Lindstromd5390202001-01-29 08:07:43 +0000988 /* ignored */
Damien Miller95def091999-11-25 00:26:21 +1100989 break;
990 case 'q':
991 options.log_level = SYSLOG_LEVEL_QUIET;
992 break;
993 case 'b':
Damien Millerf0b15df2006-03-26 13:59:20 +1100994 options.server_key_bits = (int)strtonum(optarg, 256,
995 32768, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100996 break;
997 case 'p':
Damien Miller34132e52000-01-14 15:45:46 +1100998 options.ports_from_cmdline = 1;
Damien Millere4340be2000-09-16 13:29:08 +1100999 if (options.num_ports >= MAX_PORTS) {
1000 fprintf(stderr, "too many ports.\n");
1001 exit(1);
1002 }
Ben Lindstrom19066a12001-04-12 23:39:26 +00001003 options.ports[options.num_ports++] = a2port(optarg);
1004 if (options.ports[options.num_ports-1] == 0) {
1005 fprintf(stderr, "Bad port number.\n");
1006 exit(1);
1007 }
Damien Miller95def091999-11-25 00:26:21 +11001008 break;
1009 case 'g':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +00001010 if ((options.login_grace_time = convtime(optarg)) == -1) {
1011 fprintf(stderr, "Invalid login grace time.\n");
1012 exit(1);
1013 }
Damien Miller95def091999-11-25 00:26:21 +11001014 break;
1015 case 'k':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +00001016 if ((options.key_regeneration_time = convtime(optarg)) == -1) {
1017 fprintf(stderr, "Invalid key regeneration interval.\n");
1018 exit(1);
1019 }
Damien Miller95def091999-11-25 00:26:21 +11001020 break;
1021 case 'h':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001022 if (options.num_host_key_files >= MAX_HOSTKEYS) {
1023 fprintf(stderr, "too many host keys.\n");
1024 exit(1);
1025 }
1026 options.host_key_files[options.num_host_key_files++] = optarg;
Damien Miller95def091999-11-25 00:26:21 +11001027 break;
Ben Lindstrom794325a2001-08-06 21:09:07 +00001028 case 't':
1029 test_flag = 1;
1030 break;
Damien Miller942da032000-08-18 13:59:06 +10001031 case 'u':
Damien Millerf0b15df2006-03-26 13:59:20 +11001032 utmp_len = (u_int)strtonum(optarg, 0, MAXHOSTNAMELEN+1, NULL);
Ben Lindstrom41daec72002-07-23 21:15:13 +00001033 if (utmp_len > MAXHOSTNAMELEN) {
1034 fprintf(stderr, "Invalid utmp length.\n");
1035 exit(1);
1036 }
Damien Miller942da032000-08-18 13:59:06 +10001037 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001038 case 'o':
Damien Millerb9997192003-12-17 16:29:22 +11001039 line = xstrdup(optarg);
1040 if (process_server_config_line(&options, line,
Darren Tucker45150472006-07-12 22:34:17 +10001041 "command-line", 0, NULL, NULL, NULL, NULL) != 0)
Damien Miller9f0f5c62001-12-21 14:45:46 +11001042 exit(1);
Damien Millerb9997192003-12-17 16:29:22 +11001043 xfree(line);
Ben Lindstromade03f62001-12-06 18:22:17 +00001044 break;
Damien Miller95def091999-11-25 00:26:21 +11001045 case '?':
1046 default:
Ben Lindstromade03f62001-12-06 18:22:17 +00001047 usage();
1048 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001050 }
Darren Tucker645ab752004-06-25 13:33:20 +10001051 if (rexeced_flag || inetd_flag)
1052 rexec_flag = 0;
1053 if (rexec_flag && (av[0] == NULL || *av[0] != '/'))
1054 fatal("sshd re-exec requires execution with an absolute path");
1055 if (rexeced_flag)
Damien Miller035a5b42004-06-26 08:16:31 +10001056 closefrom(REEXEC_MIN_FREE_FD);
1057 else
1058 closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001059
Ben Lindstrom425fb022001-03-29 00:31:20 +00001060 SSLeay_add_all_algorithms();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001061
Damien Miller34132e52000-01-14 15:45:46 +11001062 /*
1063 * Force logging to stderr until we have loaded the private host
1064 * key (unless started from inetd)
1065 */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001066 log_init(__progname,
Damien Miller5aa5d782002-02-08 22:01:54 +11001067 options.log_level == SYSLOG_LEVEL_NOT_SET ?
1068 SYSLOG_LEVEL_INFO : options.log_level,
1069 options.log_facility == SYSLOG_FACILITY_NOT_SET ?
1070 SYSLOG_FACILITY_AUTH : options.log_facility,
Ben Lindstromc2faa4a2002-11-09 15:50:03 +00001071 log_stderr || !inetd_flag);
Damien Miller34132e52000-01-14 15:45:46 +11001072
Darren Tucker86c093d2004-03-08 22:59:03 +11001073 /*
1074 * Unset KRB5CCNAME, otherwise the user's session may inherit it from
1075 * root's environment
Damien Miller94cf4c82005-07-17 17:04:47 +10001076 */
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001077 if (getenv("KRB5CCNAME") != NULL)
1078 unsetenv("KRB5CCNAME");
1079
Tim Rice81ed5182002-09-25 17:38:46 -07001080#ifdef _UNICOS
Darren Tucker12984962004-05-24 13:37:13 +10001081 /* Cray can define user privs drop all privs now!
Ben Lindstrom6db66ff2001-08-06 23:29:16 +00001082 * Not needed on PRIV_SU systems!
1083 */
1084 drop_cray_privs();
1085#endif
1086
Darren Tucker645ab752004-06-25 13:33:20 +10001087 sensitive_data.server_key = NULL;
1088 sensitive_data.ssh1_host_key = NULL;
1089 sensitive_data.have_ssh1_key = 0;
1090 sensitive_data.have_ssh2_key = 0;
1091
1092 /* Fetch our configuration */
1093 buffer_init(&cfg);
1094 if (rexeced_flag)
Damien Miller035a5b42004-06-26 08:16:31 +10001095 recv_rexec_state(REEXEC_CONFIG_PASS_FD, &cfg);
Darren Tucker645ab752004-06-25 13:33:20 +10001096 else
1097 load_server_config(config_file_name, &cfg);
1098
Darren Tucker45150472006-07-12 22:34:17 +10001099 parse_server_config(&options, rexeced_flag ? "rexec" : config_file_name,
1100 &cfg, NULL, NULL, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101
Darren Tuckerc6f82192005-09-27 22:46:32 +10001102 seed_rng();
1103
Damien Miller95def091999-11-25 00:26:21 +11001104 /* Fill in default values for those options not explicitly set. */
1105 fill_default_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001106
Darren Tucker0f383232005-01-20 10:57:56 +11001107 /* set default channel AF */
1108 channel_set_af(options.address_family);
1109
Damien Miller95def091999-11-25 00:26:21 +11001110 /* Check that there are no remaining arguments. */
1111 if (optind < ac) {
1112 fprintf(stderr, "Extra argument %s.\n", av[optind]);
1113 exit(1);
1114 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001115
Damien Miller2aa6d3c2004-09-12 16:53:04 +10001116 debug("sshd version %.100s", SSH_RELEASE);
Damien Miller2ccf6611999-11-15 15:25:10 +11001117
Damien Miller0bc1bd82000-11-13 22:57:25 +11001118 /* load private host keys */
Damien Miller07d86be2006-03-26 14:19:21 +11001119 sensitive_data.host_keys = xcalloc(options.num_host_key_files,
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001120 sizeof(Key *));
Damien Miller9f0f5c62001-12-21 14:45:46 +11001121 for (i = 0; i < options.num_host_key_files; i++)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001122 sensitive_data.host_keys[i] = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001123
Damien Miller9f0f5c62001-12-21 14:45:46 +11001124 for (i = 0; i < options.num_host_key_files; i++) {
Ben Lindstromd0fca422001-03-26 13:44:06 +00001125 key = key_load_private(options.host_key_files[i], "", NULL);
1126 sensitive_data.host_keys[i] = key;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001127 if (key == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001128 error("Could not load host key: %s",
1129 options.host_key_files[i]);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001130 sensitive_data.host_keys[i] = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001131 continue;
1132 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001133 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001134 case KEY_RSA1:
1135 sensitive_data.ssh1_host_key = key;
1136 sensitive_data.have_ssh1_key = 1;
1137 break;
1138 case KEY_RSA:
1139 case KEY_DSA:
1140 sensitive_data.have_ssh2_key = 1;
1141 break;
1142 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001143 debug("private host key: #%d type %d %s", i, key->type,
1144 key_type(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001145 }
1146 if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
Damien Miller996acd22003-04-09 20:59:48 +10001147 logit("Disabling protocol version 1. Could not load host key");
Damien Millereba71ba2000-04-29 23:57:08 +10001148 options.protocol &= ~SSH_PROTO_1;
1149 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001150 if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
Damien Miller996acd22003-04-09 20:59:48 +10001151 logit("Disabling protocol version 2. Could not load host key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001152 options.protocol &= ~SSH_PROTO_2;
Damien Millereba71ba2000-04-29 23:57:08 +10001153 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001154 if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
Damien Miller996acd22003-04-09 20:59:48 +10001155 logit("sshd: no hostkeys available -- exiting.");
Damien Miller95def091999-11-25 00:26:21 +11001156 exit(1);
1157 }
Damien Miller95def091999-11-25 00:26:21 +11001158
Damien Millereba71ba2000-04-29 23:57:08 +10001159 /* Check certain values for sanity. */
1160 if (options.protocol & SSH_PROTO_1) {
1161 if (options.server_key_bits < 512 ||
1162 options.server_key_bits > 32768) {
1163 fprintf(stderr, "Bad server key size.\n");
1164 exit(1);
1165 }
1166 /*
1167 * Check that server and host key lengths differ sufficiently. This
1168 * is necessary to make double encryption work with rsaref. Oh, I
1169 * hate software patents. I dont know if this can go? Niels
1170 */
1171 if (options.server_key_bits >
Ben Lindstrom822b6342002-06-23 21:38:49 +00001172 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) -
1173 SSH_KEY_BITS_RESERVED && options.server_key_bits <
1174 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1175 SSH_KEY_BITS_RESERVED) {
Damien Millereba71ba2000-04-29 23:57:08 +10001176 options.server_key_bits =
Ben Lindstrom822b6342002-06-23 21:38:49 +00001177 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1178 SSH_KEY_BITS_RESERVED;
Damien Millereba71ba2000-04-29 23:57:08 +10001179 debug("Forcing server key to %d bits to make it differ from host key.",
1180 options.server_key_bits);
1181 }
1182 }
1183
Ben Lindstroma26ea632002-06-06 20:46:25 +00001184 if (use_privsep) {
Ben Lindstroma26ea632002-06-06 20:46:25 +00001185 struct stat st;
1186
Damien Millerf0b15df2006-03-26 13:59:20 +11001187 if (getpwnam(SSH_PRIVSEP_USER) == NULL)
Ben Lindstroma26ea632002-06-06 20:46:25 +00001188 fatal("Privilege separation user %s does not exist",
1189 SSH_PRIVSEP_USER);
1190 if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
1191 (S_ISDIR(st.st_mode) == 0))
1192 fatal("Missing privilege separation directory: %s",
1193 _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstrom59627352002-06-27 18:02:21 +00001194
1195#ifdef HAVE_CYGWIN
1196 if (check_ntsec(_PATH_PRIVSEP_CHROOT_DIR) &&
1197 (st.st_uid != getuid () ||
1198 (st.st_mode & (S_IWGRP|S_IWOTH)) != 0))
1199#else
Ben Lindstrom2dfacb32002-06-23 00:33:47 +00001200 if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
Ben Lindstrom59627352002-06-27 18:02:21 +00001201#endif
Damien Miller180fc5b2003-02-24 11:50:18 +11001202 fatal("%s must be owned by root and not group or "
1203 "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstroma26ea632002-06-06 20:46:25 +00001204 }
1205
Ben Lindstrom794325a2001-08-06 21:09:07 +00001206 /* Configuration looks good, so exit if in test mode. */
1207 if (test_flag)
1208 exit(0);
1209
Damien Miller87aea252002-05-10 12:20:24 +10001210 /*
1211 * Clear out any supplemental groups we may have inherited. This
1212 * prevents inadvertent creation of files with bad modes (in the
Damien Millera8e06ce2003-11-21 23:48:55 +11001213 * portable version at least, it's certainly possible for PAM
1214 * to create a file, and we can't control the code in every
Damien Miller87aea252002-05-10 12:20:24 +10001215 * module which might be used).
1216 */
1217 if (setgroups(0, NULL) < 0)
1218 debug("setgroups() failed: %.200s", strerror(errno));
1219
Darren Tucker645ab752004-06-25 13:33:20 +10001220 if (rexec_flag) {
Damien Miller07d86be2006-03-26 14:19:21 +11001221 rexec_argv = xcalloc(rexec_argc + 2, sizeof(char *));
Darren Tucker645ab752004-06-25 13:33:20 +10001222 for (i = 0; i < rexec_argc; i++) {
1223 debug("rexec_argv[%d]='%s'", i, saved_argv[i]);
1224 rexec_argv[i] = saved_argv[i];
1225 }
1226 rexec_argv[rexec_argc] = "-R";
1227 rexec_argv[rexec_argc + 1] = NULL;
1228 }
1229
Damien Millereba71ba2000-04-29 23:57:08 +10001230 /* Initialize the log (it is reinitialized below in case we forked). */
Darren Tuckerea7c8122005-01-20 11:03:08 +11001231 if (debug_flag && (!inetd_flag || rexeced_flag))
Damien Miller95def091999-11-25 00:26:21 +11001232 log_stderr = 1;
Kevin Stevesec84dc12000-12-13 17:45:15 +00001233 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001234
Damien Millereba71ba2000-04-29 23:57:08 +10001235 /*
1236 * If not in debugging mode, and not started from inetd, disconnect
1237 * from the controlling terminal, and fork. The original process
1238 * exits.
1239 */
Ben Lindstromc72745a2000-12-02 19:03:54 +00001240 if (!(debug_flag || inetd_flag || no_daemon_flag)) {
Damien Miller95def091999-11-25 00:26:21 +11001241#ifdef TIOCNOTTY
1242 int fd;
1243#endif /* TIOCNOTTY */
1244 if (daemon(0, 0) < 0)
1245 fatal("daemon() failed: %.200s", strerror(errno));
1246
1247 /* Disconnect from the controlling tty. */
1248#ifdef TIOCNOTTY
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001249 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
Damien Miller95def091999-11-25 00:26:21 +11001250 if (fd >= 0) {
1251 (void) ioctl(fd, TIOCNOTTY, NULL);
1252 close(fd);
1253 }
1254#endif /* TIOCNOTTY */
1255 }
1256 /* Reinitialize the log (because of the fork above). */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001257 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001258
Damien Miller95def091999-11-25 00:26:21 +11001259 /* Initialize the random number generator. */
1260 arc4random_stir();
1261
1262 /* Chdir to the root directory so that the current disk can be
1263 unmounted if desired. */
1264 chdir("/");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001265
Ben Lindstromde71cda2001-03-24 00:43:26 +00001266 /* ignore SIGPIPE */
1267 signal(SIGPIPE, SIG_IGN);
Damien Miller95def091999-11-25 00:26:21 +11001268
Damien Miller95def091999-11-25 00:26:21 +11001269 /* Start listening for a socket, unless started from inetd. */
1270 if (inetd_flag) {
Darren Tucker645ab752004-06-25 13:33:20 +10001271 int fd;
1272
Damien Miller994cf142000-07-21 10:19:44 +10001273 startup_pipe = -1;
Darren Tucker645ab752004-06-25 13:33:20 +10001274 if (rexeced_flag) {
Damien Miller035a5b42004-06-26 08:16:31 +10001275 close(REEXEC_CONFIG_PASS_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001276 sock_in = sock_out = dup(STDIN_FILENO);
1277 if (!debug_flag) {
Damien Miller035a5b42004-06-26 08:16:31 +10001278 startup_pipe = dup(REEXEC_STARTUP_PIPE_FD);
1279 close(REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001280 }
1281 } else {
1282 sock_in = dup(STDIN_FILENO);
1283 sock_out = dup(STDOUT_FILENO);
1284 }
Damien Millereba71ba2000-04-29 23:57:08 +10001285 /*
1286 * We intentionally do not close the descriptors 0, 1, and 2
Darren Tucker645ab752004-06-25 13:33:20 +10001287 * as our code for setting the descriptors won't work if
Damien Millereba71ba2000-04-29 23:57:08 +10001288 * ttyfd happens to be one of those.
1289 */
Darren Tucker645ab752004-06-25 13:33:20 +10001290 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1291 dup2(fd, STDIN_FILENO);
1292 dup2(fd, STDOUT_FILENO);
1293 if (fd > STDOUT_FILENO)
1294 close(fd);
1295 }
Damien Miller95def091999-11-25 00:26:21 +11001296 debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
Darren Tucker645ab752004-06-25 13:33:20 +10001297 if ((options.protocol & SSH_PROTO_1) &&
1298 sensitive_data.server_key == NULL)
Ben Lindstromca42d5f2001-03-09 18:25:32 +00001299 generate_ephemeral_server_key();
Damien Miller95def091999-11-25 00:26:21 +11001300 } else {
Damien Miller34132e52000-01-14 15:45:46 +11001301 for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
1302 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1303 continue;
1304 if (num_listen_socks >= MAX_LISTEN_SOCKS)
1305 fatal("Too many listen sockets. "
1306 "Enlarge MAX_LISTEN_SOCKS");
Darren Tucker96d47102005-02-09 09:53:48 +11001307 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
Damien Miller34132e52000-01-14 15:45:46 +11001308 ntop, sizeof(ntop), strport, sizeof(strport),
Darren Tucker96d47102005-02-09 09:53:48 +11001309 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1310 error("getnameinfo failed: %.100s",
1311 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
1312 strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11001313 continue;
1314 }
1315 /* Create socket for listening. */
Damien Miller2372ace2003-05-14 13:42:23 +10001316 listen_sock = socket(ai->ai_family, ai->ai_socktype,
1317 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11001318 if (listen_sock < 0) {
1319 /* kernel may not support ipv6 */
1320 verbose("socket: %.100s", strerror(errno));
1321 continue;
1322 }
Damien Miller232711f2004-06-15 10:35:30 +10001323 if (set_nonblock(listen_sock) == -1) {
Darren Tuckerefa37062004-02-24 09:20:29 +11001324 close(listen_sock);
1325 continue;
1326 }
Damien Miller34132e52000-01-14 15:45:46 +11001327 /*
Damien Millere1383ce2002-09-19 11:49:37 +10001328 * Set socket options.
1329 * Allow local port reuse in TIME_WAIT.
Damien Miller34132e52000-01-14 15:45:46 +11001330 */
Damien Millere1383ce2002-09-19 11:49:37 +10001331 if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
1332 &on, sizeof(on)) == -1)
1333 error("setsockopt SO_REUSEADDR: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001334
Damien Miller34132e52000-01-14 15:45:46 +11001335 debug("Bind to port %s on %s.", strport, ntop);
Damien Miller95def091999-11-25 00:26:21 +11001336
Damien Miller34132e52000-01-14 15:45:46 +11001337 /* Bind the socket to the desired port. */
Damien Miller0a4e27d2001-02-18 12:36:39 +11001338 if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1339 if (!ai->ai_next)
1340 error("Bind to port %s on %s failed: %.200s.",
1341 strport, ntop, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11001342 close(listen_sock);
1343 continue;
1344 }
1345 listen_socks[num_listen_socks] = listen_sock;
1346 num_listen_socks++;
Damien Miller95def091999-11-25 00:26:21 +11001347
Damien Miller34132e52000-01-14 15:45:46 +11001348 /* Start listening on the port. */
Darren Tucker3175eb92003-12-09 19:15:11 +11001349 if (listen(listen_sock, SSH_LISTEN_BACKLOG) < 0)
Damien Millerb24c2f82006-03-15 12:04:36 +11001350 fatal("listen on [%s]:%s: %.100s",
1351 ntop, strport, strerror(errno));
1352 logit("Server listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001353 }
Damien Miller34132e52000-01-14 15:45:46 +11001354 freeaddrinfo(options.listen_addrs);
1355
1356 if (!num_listen_socks)
1357 fatal("Cannot bind any address.");
1358
Ben Lindstrom98097862001-06-25 05:10:20 +00001359 if (options.protocol & SSH_PROTO_1)
1360 generate_ephemeral_server_key();
1361
1362 /*
1363 * Arrange to restart on SIGHUP. The handler needs
1364 * listen_sock.
1365 */
1366 signal(SIGHUP, sighup_handler);
1367
1368 signal(SIGTERM, sigterm_handler);
1369 signal(SIGQUIT, sigterm_handler);
1370
1371 /* Arrange SIGCHLD to be caught. */
1372 signal(SIGCHLD, main_sigchld_handler);
1373
1374 /* Write out the pid file after the sigterm handler is setup */
Damien Miller95def091999-11-25 00:26:21 +11001375 if (!debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +11001376 /*
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001377 * Record our pid in /var/run/sshd.pid to make it
1378 * easier to kill the correct sshd. We don't want to
1379 * do this before the bind above because the bind will
Damien Miller5428f641999-11-25 11:54:57 +11001380 * fail if there already is a daemon, and this will
1381 * overwrite any old pid in the file.
1382 */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001383 f = fopen(options.pid_file, "wb");
Darren Tuckere5327042003-07-03 13:40:44 +10001384 if (f == NULL) {
1385 error("Couldn't create pid file \"%s\": %s",
1386 options.pid_file, strerror(errno));
1387 } else {
Ben Lindstromce0f6342002-06-11 16:42:49 +00001388 fprintf(f, "%ld\n", (long) getpid());
Damien Miller95def091999-11-25 00:26:21 +11001389 fclose(f);
1390 }
1391 }
Damien Miller95def091999-11-25 00:26:21 +11001392
Damien Miller34132e52000-01-14 15:45:46 +11001393 /* setup fd set for listen */
Damien Miller37023962000-07-11 17:31:38 +10001394 fdset = NULL;
Damien Miller34132e52000-01-14 15:45:46 +11001395 maxfd = 0;
1396 for (i = 0; i < num_listen_socks; i++)
1397 if (listen_socks[i] > maxfd)
1398 maxfd = listen_socks[i];
Damien Miller37023962000-07-11 17:31:38 +10001399 /* pipes connected to unauthenticated childs */
Damien Miller07d86be2006-03-26 14:19:21 +11001400 startup_pipes = xcalloc(options.max_startups, sizeof(int));
Damien Miller37023962000-07-11 17:31:38 +10001401 for (i = 0; i < options.max_startups; i++)
1402 startup_pipes[i] = -1;
Damien Miller34132e52000-01-14 15:45:46 +11001403
Damien Miller5428f641999-11-25 11:54:57 +11001404 /*
1405 * Stay listening for connections until the system crashes or
1406 * the daemon is killed with a signal.
1407 */
Damien Miller95def091999-11-25 00:26:21 +11001408 for (;;) {
1409 if (received_sighup)
1410 sighup_restart();
Damien Miller37023962000-07-11 17:31:38 +10001411 if (fdset != NULL)
1412 xfree(fdset);
Damien Miller07d86be2006-03-26 14:19:21 +11001413 fdset = (fd_set *)xcalloc(howmany(maxfd + 1, NFDBITS),
1414 sizeof(fd_mask));
Damien Miller37023962000-07-11 17:31:38 +10001415
Damien Miller34132e52000-01-14 15:45:46 +11001416 for (i = 0; i < num_listen_socks; i++)
1417 FD_SET(listen_socks[i], fdset);
Damien Miller37023962000-07-11 17:31:38 +10001418 for (i = 0; i < options.max_startups; i++)
1419 if (startup_pipes[i] != -1)
1420 FD_SET(startup_pipes[i], fdset);
1421
1422 /* Wait in select until there is a connection. */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001423 ret = select(maxfd+1, fdset, NULL, NULL, NULL);
1424 if (ret < 0 && errno != EINTR)
1425 error("select: %.100s", strerror(errno));
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001426 if (received_sigterm) {
Damien Miller996acd22003-04-09 20:59:48 +10001427 logit("Received signal %d; terminating.",
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001428 (int) received_sigterm);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001429 close_listen_socks();
1430 unlink(options.pid_file);
1431 exit(255);
1432 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001433 if (key_used && key_do_regen) {
Ben Lindstromca42d5f2001-03-09 18:25:32 +00001434 generate_ephemeral_server_key();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001435 key_used = 0;
1436 key_do_regen = 0;
Damien Miller34132e52000-01-14 15:45:46 +11001437 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001438 if (ret < 0)
1439 continue;
1440
Damien Miller37023962000-07-11 17:31:38 +10001441 for (i = 0; i < options.max_startups; i++)
1442 if (startup_pipes[i] != -1 &&
1443 FD_ISSET(startup_pipes[i], fdset)) {
1444 /*
1445 * the read end of the pipe is ready
1446 * if the child has closed the pipe
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001447 * after successful authentication
Damien Miller37023962000-07-11 17:31:38 +10001448 * or if the child has died
1449 */
1450 close(startup_pipes[i]);
1451 startup_pipes[i] = -1;
1452 startups--;
1453 }
Damien Miller34132e52000-01-14 15:45:46 +11001454 for (i = 0; i < num_listen_socks; i++) {
1455 if (!FD_ISSET(listen_socks[i], fdset))
Damien Miller95def091999-11-25 00:26:21 +11001456 continue;
Damien Miller37023962000-07-11 17:31:38 +10001457 fromlen = sizeof(from);
Damien Millerf0b15df2006-03-26 13:59:20 +11001458 newsock = accept(listen_socks[i],
1459 (struct sockaddr *)&from, &fromlen);
Damien Miller37023962000-07-11 17:31:38 +10001460 if (newsock < 0) {
1461 if (errno != EINTR && errno != EWOULDBLOCK)
1462 error("accept: %.100s", strerror(errno));
1463 continue;
1464 }
Damien Miller232711f2004-06-15 10:35:30 +10001465 if (unset_nonblock(newsock) == -1) {
Darren Tuckerefa37062004-02-24 09:20:29 +11001466 close(newsock);
1467 continue;
1468 }
Damien Miller942da032000-08-18 13:59:06 +10001469 if (drop_connection(startups) == 1) {
1470 debug("drop connection #%d", startups);
Damien Miller37023962000-07-11 17:31:38 +10001471 close(newsock);
1472 continue;
1473 }
1474 if (pipe(startup_p) == -1) {
1475 close(newsock);
1476 continue;
1477 }
1478
Darren Tucker645ab752004-06-25 13:33:20 +10001479 if (rexec_flag && socketpair(AF_UNIX,
1480 SOCK_STREAM, 0, config_s) == -1) {
1481 error("reexec socketpair: %s",
1482 strerror(errno));
1483 close(newsock);
1484 close(startup_p[0]);
1485 close(startup_p[1]);
1486 continue;
1487 }
1488
Damien Miller37023962000-07-11 17:31:38 +10001489 for (j = 0; j < options.max_startups; j++)
1490 if (startup_pipes[j] == -1) {
1491 startup_pipes[j] = startup_p[0];
1492 if (maxfd < startup_p[0])
1493 maxfd = startup_p[0];
1494 startups++;
1495 break;
1496 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001497
Damien Miller5428f641999-11-25 11:54:57 +11001498 /*
Damien Miller37023962000-07-11 17:31:38 +10001499 * Got connection. Fork a child to handle it, unless
1500 * we are in debugging mode.
Damien Miller5428f641999-11-25 11:54:57 +11001501 */
Damien Miller37023962000-07-11 17:31:38 +10001502 if (debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +11001503 /*
Damien Miller37023962000-07-11 17:31:38 +10001504 * In debugging mode. Close the listening
1505 * socket, and start processing the
1506 * connection without forking.
Damien Miller5428f641999-11-25 11:54:57 +11001507 */
Damien Miller37023962000-07-11 17:31:38 +10001508 debug("Server will not fork when running in debugging mode.");
Damien Miller34132e52000-01-14 15:45:46 +11001509 close_listen_socks();
Damien Miller95def091999-11-25 00:26:21 +11001510 sock_in = newsock;
1511 sock_out = newsock;
Darren Tucker645ab752004-06-25 13:33:20 +10001512 close(startup_p[0]);
1513 close(startup_p[1]);
Damien Miller4d97ba22000-07-11 18:15:50 +10001514 startup_pipe = -1;
Damien Miller182ee6e2000-07-12 09:45:27 +10001515 pid = getpid();
Darren Tucker645ab752004-06-25 13:33:20 +10001516 if (rexec_flag) {
1517 send_rexec_state(config_s[0],
1518 &cfg);
1519 close(config_s[0]);
1520 }
Damien Miller95def091999-11-25 00:26:21 +11001521 break;
Damien Miller37023962000-07-11 17:31:38 +10001522 } else {
1523 /*
1524 * Normal production daemon. Fork, and have
1525 * the child process the connection. The
1526 * parent continues listening.
1527 */
1528 if ((pid = fork()) == 0) {
1529 /*
Damien Millerf0b15df2006-03-26 13:59:20 +11001530 * Child. Close the listening and
1531 * max_startup sockets. Start using
1532 * the accepted socket. Reinitialize
1533 * logging (since our pid has changed).
1534 * We break out of the loop to handle
Damien Miller37023962000-07-11 17:31:38 +10001535 * the connection.
1536 */
1537 startup_pipe = startup_p[1];
Ben Lindstromd84df982001-12-06 16:35:40 +00001538 close_startup_pipes();
Damien Miller37023962000-07-11 17:31:38 +10001539 close_listen_socks();
1540 sock_in = newsock;
1541 sock_out = newsock;
Damien Millerf0b15df2006-03-26 13:59:20 +11001542 log_init(__progname,
1543 options.log_level,
1544 options.log_facility,
1545 log_stderr);
Darren Tucker0f56ed12004-08-29 16:38:41 +10001546 if (rexec_flag)
1547 close(config_s[0]);
Damien Miller37023962000-07-11 17:31:38 +10001548 break;
1549 }
Damien Miller95def091999-11-25 00:26:21 +11001550 }
Damien Miller37023962000-07-11 17:31:38 +10001551
1552 /* Parent. Stay in the loop. */
1553 if (pid < 0)
1554 error("fork: %.100s", strerror(errno));
1555 else
Ben Lindstromce0f6342002-06-11 16:42:49 +00001556 debug("Forked child %ld.", (long)pid);
Damien Miller37023962000-07-11 17:31:38 +10001557
1558 close(startup_p[1]);
1559
Darren Tucker645ab752004-06-25 13:33:20 +10001560 if (rexec_flag) {
1561 send_rexec_state(config_s[0], &cfg);
1562 close(config_s[0]);
1563 close(config_s[1]);
1564 }
1565
Damien Millerf0b15df2006-03-26 13:59:20 +11001566 /*
1567 * Mark that the key has been used (it
1568 * was "given" to the child).
1569 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001570 if ((options.protocol & SSH_PROTO_1) &&
1571 key_used == 0) {
1572 /* Schedule server key regeneration alarm. */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00001573 signal(SIGALRM, key_regeneration_alarm);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001574 alarm(options.key_regeneration_time);
1575 key_used = 1;
1576 }
Damien Miller37023962000-07-11 17:31:38 +10001577
1578 arc4random_stir();
Damien Miller37023962000-07-11 17:31:38 +10001579 close(newsock);
Damien Miller95def091999-11-25 00:26:21 +11001580 }
Damien Miller34132e52000-01-14 15:45:46 +11001581 /* child process check (or debug mode) */
1582 if (num_listen_socks < 0)
1583 break;
Damien Miller95def091999-11-25 00:26:21 +11001584 }
1585 }
1586
1587 /* This is the child processing a new connection. */
Damien Miller57aae982004-03-08 23:11:25 +11001588 setproctitle("%s", "[accepted]");
Damien Miller95def091999-11-25 00:26:21 +11001589
Darren Tucker6832b832004-08-12 22:36:51 +10001590 /*
1591 * Create a new session and process group since the 4.4BSD
1592 * setlogin() affects the entire process group. We don't
1593 * want the child to be able to affect the parent.
1594 */
1595#if !defined(SSHD_ACQUIRES_CTTY)
1596 /*
1597 * If setsid is called, on some platforms sshd will later acquire a
1598 * controlling terminal which will result in "could not set
1599 * controlling tty" errors.
1600 */
1601 if (!debug_flag && !inetd_flag && setsid() < 0)
1602 error("setsid: %.100s", strerror(errno));
1603#endif
1604
Darren Tucker645ab752004-06-25 13:33:20 +10001605 if (rexec_flag) {
1606 int fd;
1607
Damien Miller035a5b42004-06-26 08:16:31 +10001608 debug("rexec start in %d out %d newsock %d pipe %d sock %d",
1609 sock_in, sock_out, newsock, startup_pipe, config_s[0]);
Darren Tucker645ab752004-06-25 13:33:20 +10001610 dup2(newsock, STDIN_FILENO);
1611 dup2(STDIN_FILENO, STDOUT_FILENO);
1612 if (startup_pipe == -1)
Damien Miller035a5b42004-06-26 08:16:31 +10001613 close(REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001614 else
Damien Miller035a5b42004-06-26 08:16:31 +10001615 dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001616
Damien Miller035a5b42004-06-26 08:16:31 +10001617 dup2(config_s[1], REEXEC_CONFIG_PASS_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001618 close(config_s[1]);
Darren Tuckerd8835932004-08-12 22:42:29 +10001619 if (startup_pipe != -1)
1620 close(startup_pipe);
Damien Miller035a5b42004-06-26 08:16:31 +10001621
Darren Tucker645ab752004-06-25 13:33:20 +10001622 execv(rexec_argv[0], rexec_argv);
1623
1624 /* Reexec has failed, fall back and continue */
1625 error("rexec of %s failed: %s", rexec_argv[0], strerror(errno));
Damien Miller035a5b42004-06-26 08:16:31 +10001626 recv_rexec_state(REEXEC_CONFIG_PASS_FD, NULL);
Darren Tucker645ab752004-06-25 13:33:20 +10001627 log_init(__progname, options.log_level,
1628 options.log_facility, log_stderr);
1629
1630 /* Clean up fds */
Damien Miller035a5b42004-06-26 08:16:31 +10001631 startup_pipe = REEXEC_STARTUP_PIPE_FD;
Darren Tucker645ab752004-06-25 13:33:20 +10001632 close(config_s[1]);
Damien Miller035a5b42004-06-26 08:16:31 +10001633 close(REEXEC_CONFIG_PASS_FD);
1634 newsock = sock_out = sock_in = dup(STDIN_FILENO);
Darren Tucker645ab752004-06-25 13:33:20 +10001635 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1636 dup2(fd, STDIN_FILENO);
1637 dup2(fd, STDOUT_FILENO);
1638 if (fd > STDERR_FILENO)
1639 close(fd);
1640 }
Damien Miller035a5b42004-06-26 08:16:31 +10001641 debug("rexec cleanup in %d out %d newsock %d pipe %d sock %d",
1642 sock_in, sock_out, newsock, startup_pipe, config_s[0]);
Darren Tucker645ab752004-06-25 13:33:20 +10001643 }
1644
Damien Miller5428f641999-11-25 11:54:57 +11001645 /*
1646 * Disable the key regeneration alarm. We will not regenerate the
1647 * key since we are no longer in a position to give it to anyone. We
1648 * will not restart on SIGHUP since it no longer makes sense.
1649 */
Damien Miller95def091999-11-25 00:26:21 +11001650 alarm(0);
1651 signal(SIGALRM, SIG_DFL);
1652 signal(SIGHUP, SIG_DFL);
1653 signal(SIGTERM, SIG_DFL);
1654 signal(SIGQUIT, SIG_DFL);
1655 signal(SIGCHLD, SIG_DFL);
Damien Miller4e0f5e12000-08-29 11:05:50 +11001656 signal(SIGINT, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001657
Damien Miller5428f641999-11-25 11:54:57 +11001658 /*
1659 * Register our connection. This turns encryption off because we do
1660 * not have a key.
1661 */
Damien Miller95def091999-11-25 00:26:21 +11001662 packet_set_connection(sock_in, sock_out);
Damien Miller9786e6e2005-07-26 21:54:56 +10001663 packet_set_server();
Damien Miller95def091999-11-25 00:26:21 +11001664
Damien Miller4f1d6b22005-05-26 11:59:32 +10001665 /* Set SO_KEEPALIVE if requested. */
1666 if (options.tcp_keep_alive && packet_connection_is_on_socket() &&
1667 setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0)
1668 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1669
Damien Miller677257f2005-06-17 12:55:03 +10001670 if ((remote_port = get_remote_port()) < 0) {
1671 debug("get_remote_port failed");
1672 cleanup_exit(255);
1673 }
Damien Miller4d3fd542005-11-05 15:13:24 +11001674
1675 /*
1676 * We use get_canonical_hostname with usedns = 0 instead of
1677 * get_remote_ipaddr here so IP options will be checked.
1678 */
Damien Millereb13e552006-06-13 13:03:53 +10001679 (void) get_canonical_hostname(0);
1680 /*
1681 * The rest of the code depends on the fact that
1682 * get_remote_ipaddr() caches the remote ip, even if
1683 * the socket goes away.
1684 */
1685 remote_ip = get_remote_ipaddr();
Damien Miller95def091999-11-25 00:26:21 +11001686
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001687#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001688 audit_connection_from(remote_ip, remote_port);
1689#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001690#ifdef LIBWRAP
Damien Miller6a4a4b92001-11-12 11:07:11 +11001691 /* Check whether logins are denied from this host. */
Darren Tucker586b0b92004-06-25 13:34:31 +10001692 if (packet_connection_is_on_socket()) {
Damien Miller95def091999-11-25 00:26:21 +11001693 struct request_info req;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001694
Ben Lindstromce89dac2001-09-12 16:58:04 +00001695 request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, 0);
Damien Miller95def091999-11-25 00:26:21 +11001696 fromhost(&req);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001697
Damien Miller95def091999-11-25 00:26:21 +11001698 if (!hosts_access(&req)) {
Damien Miller6a4a4b92001-11-12 11:07:11 +11001699 debug("Connection refused by tcp wrapper");
Ben Lindstrom7de696e2001-03-29 00:45:12 +00001700 refuse(&req);
Damien Miller6a4a4b92001-11-12 11:07:11 +11001701 /* NOTREACHED */
1702 fatal("libwrap refuse returns");
Damien Miller95def091999-11-25 00:26:21 +11001703 }
Damien Miller95def091999-11-25 00:26:21 +11001704 }
Damien Miller34132e52000-01-14 15:45:46 +11001705#endif /* LIBWRAP */
Damien Miller6a4a4b92001-11-12 11:07:11 +11001706
Damien Miller95def091999-11-25 00:26:21 +11001707 /* Log the connection. */
1708 verbose("Connection from %.500s port %d", remote_ip, remote_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001709
Damien Miller5428f641999-11-25 11:54:57 +11001710 /*
Damien Miller788f2122005-11-05 15:14:59 +11001711 * We don't want to listen forever unless the other side
Damien Miller5428f641999-11-25 11:54:57 +11001712 * successfully authenticates itself. So we set up an alarm which is
1713 * cleared after successful authentication. A limit of zero
Damien Miller788f2122005-11-05 15:14:59 +11001714 * indicates no limit. Note that we don't set the alarm in debugging
Damien Miller5428f641999-11-25 11:54:57 +11001715 * mode; it is just annoying to have the server exit just when you
1716 * are about to discover the bug.
1717 */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00001718 signal(SIGALRM, grace_alarm_handler);
Damien Miller95def091999-11-25 00:26:21 +11001719 if (!debug_flag)
1720 alarm(options.login_grace_time);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001721
Damien Millerb38eff82000-04-01 11:09:21 +10001722 sshd_exchange_identification(sock_in, sock_out);
Darren Tuckerec960f22003-08-13 20:37:05 +10001723
Damien Miller95def091999-11-25 00:26:21 +11001724 packet_set_nonblocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001725
Darren Tucker3e33cec2003-10-02 16:12:36 +10001726 /* allocate authentication context */
Damien Miller07d86be2006-03-26 14:19:21 +11001727 authctxt = xcalloc(1, sizeof(*authctxt));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001728
Darren Tuckerf3bb4342005-03-31 21:39:25 +10001729 authctxt->loginmsg = &loginmsg;
1730
Darren Tucker3e33cec2003-10-02 16:12:36 +10001731 /* XXX global for cleanup, access from other modules */
1732 the_authctxt = authctxt;
1733
Darren Tucker5c14c732005-01-24 21:55:49 +11001734 /* prepare buffer to collect messages to display to user after login */
1735 buffer_init(&loginmsg);
1736
Ben Lindstrom943481c2002-03-22 03:43:46 +00001737 if (use_privsep)
Darren Tucker3e33cec2003-10-02 16:12:36 +10001738 if (privsep_preauth(authctxt) == 1)
Ben Lindstrom943481c2002-03-22 03:43:46 +00001739 goto authenticated;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001740
Damien Miller396691a2000-01-20 22:44:08 +11001741 /* perform the key exchange */
Damien Miller396691a2000-01-20 22:44:08 +11001742 /* authenticate user and start session */
Damien Millerefb4afe2000-04-12 18:45:05 +10001743 if (compat20) {
1744 do_ssh2_kex();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001745 do_authentication2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001746 } else {
1747 do_ssh1_kex();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001748 do_authentication(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001749 }
Ben Lindstrom943481c2002-03-22 03:43:46 +00001750 /*
1751 * If we use privilege separation, the unprivileged child transfers
1752 * the current keystate and exits
1753 */
1754 if (use_privsep) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001755 mm_send_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001756 exit(0);
Ben Lindstrom943481c2002-03-22 03:43:46 +00001757 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001758
1759 authenticated:
Damien Miller7bff1a92005-12-24 14:59:12 +11001760 /*
1761 * Cancel the alarm we set to limit the time taken for
1762 * authentication.
1763 */
1764 alarm(0);
1765 signal(SIGALRM, SIG_DFL);
1766 if (startup_pipe != -1) {
1767 close(startup_pipe);
1768 startup_pipe = -1;
1769 }
1770
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001771#ifdef SSH_AUDIT_EVENTS
1772 audit_event(SSH_AUTH_SUCCESS);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001773#endif
1774
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001775 /*
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001776 * In privilege separation, we fork another child and prepare
1777 * file descriptor passing.
1778 */
1779 if (use_privsep) {
Ben Lindstrom943481c2002-03-22 03:43:46 +00001780 privsep_postauth(authctxt);
1781 /* the monitor process [priv] will not return */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001782 if (!compat20)
1783 destroy_sensitive_data();
1784 }
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00001785
Darren Tucker3e33cec2003-10-02 16:12:36 +10001786 /* Start session. */
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00001787 do_authenticated(authctxt);
1788
Damien Miller3a5b0232002-03-13 13:19:42 +11001789 /* The connection has been terminated. */
1790 verbose("Closing connection to %.100s", remote_ip);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001791
Damien Millerbeb4ba51999-12-28 15:09:35 +11001792#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +10001793 if (options.use_pam)
1794 finish_pam();
Damien Millerbeb4ba51999-12-28 15:09:35 +11001795#endif /* USE_PAM */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001796
Darren Tucker2b59a6d2005-03-06 22:38:51 +11001797#ifdef SSH_AUDIT_EVENTS
1798 PRIVSEP(audit_event(SSH_CONNECTION_CLOSE));
1799#endif
1800
Damien Miller95def091999-11-25 00:26:21 +11001801 packet_close();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001802
1803 if (use_privsep)
1804 mm_terminate();
1805
Damien Miller95def091999-11-25 00:26:21 +11001806 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001807}
1808
Damien Miller95def091999-11-25 00:26:21 +11001809/*
Ben Lindstromabcb1452002-03-22 01:10:21 +00001810 * Decrypt session_key_int using our private server key and private host key
1811 * (key with larger modulus first).
1812 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001813int
Ben Lindstromabcb1452002-03-22 01:10:21 +00001814ssh1_session_key(BIGNUM *session_key_int)
1815{
1816 int rsafail = 0;
1817
Damien Millerf0b15df2006-03-26 13:59:20 +11001818 if (BN_cmp(sensitive_data.server_key->rsa->n,
1819 sensitive_data.ssh1_host_key->rsa->n) > 0) {
Ben Lindstromabcb1452002-03-22 01:10:21 +00001820 /* Server key has bigger modulus. */
1821 if (BN_num_bits(sensitive_data.server_key->rsa->n) <
Damien Millerf0b15df2006-03-26 13:59:20 +11001822 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1823 SSH_KEY_BITS_RESERVED) {
1824 fatal("do_connection: %s: "
1825 "server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
Ben Lindstromabcb1452002-03-22 01:10:21 +00001826 get_remote_ipaddr(),
1827 BN_num_bits(sensitive_data.server_key->rsa->n),
1828 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1829 SSH_KEY_BITS_RESERVED);
1830 }
1831 if (rsa_private_decrypt(session_key_int, session_key_int,
1832 sensitive_data.server_key->rsa) <= 0)
1833 rsafail++;
1834 if (rsa_private_decrypt(session_key_int, session_key_int,
1835 sensitive_data.ssh1_host_key->rsa) <= 0)
1836 rsafail++;
1837 } else {
1838 /* Host key has bigger modulus (or they are equal). */
1839 if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
Damien Millerf0b15df2006-03-26 13:59:20 +11001840 BN_num_bits(sensitive_data.server_key->rsa->n) +
1841 SSH_KEY_BITS_RESERVED) {
1842 fatal("do_connection: %s: "
1843 "host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
Ben Lindstromabcb1452002-03-22 01:10:21 +00001844 get_remote_ipaddr(),
1845 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1846 BN_num_bits(sensitive_data.server_key->rsa->n),
1847 SSH_KEY_BITS_RESERVED);
1848 }
1849 if (rsa_private_decrypt(session_key_int, session_key_int,
1850 sensitive_data.ssh1_host_key->rsa) < 0)
1851 rsafail++;
1852 if (rsa_private_decrypt(session_key_int, session_key_int,
1853 sensitive_data.server_key->rsa) < 0)
1854 rsafail++;
1855 }
1856 return (rsafail);
1857}
1858/*
Damien Miller396691a2000-01-20 22:44:08 +11001859 * SSH1 key exchange
Damien Miller95def091999-11-25 00:26:21 +11001860 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001861static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001862do_ssh1_kex(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001863{
Damien Miller95def091999-11-25 00:26:21 +11001864 int i, len;
Damien Miller7650bc62001-01-30 09:27:26 +11001865 int rsafail = 0;
Damien Miller95def091999-11-25 00:26:21 +11001866 BIGNUM *session_key_int;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001867 u_char session_key[SSH_SESSION_KEY_LENGTH];
1868 u_char cookie[8];
1869 u_int cipher_type, auth_mask, protocol_flags;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001870 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001871
Damien Miller5428f641999-11-25 11:54:57 +11001872 /*
1873 * Generate check bytes that the client must send back in the user
1874 * packet in order for it to be accepted; this is used to defy ip
1875 * spoofing attacks. Note that this only works against somebody
1876 * doing IP spoofing from a remote machine; any machine on the local
1877 * network can still see outgoing packets and catch the random
1878 * cookie. This only affects rhosts authentication, and this is one
1879 * of the reasons why it is inherently insecure.
1880 */
Damien Miller95def091999-11-25 00:26:21 +11001881 for (i = 0; i < 8; i++) {
1882 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001883 rnd = arc4random();
1884 cookie[i] = rnd & 0xff;
1885 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +11001886 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001887
Damien Miller5428f641999-11-25 11:54:57 +11001888 /*
1889 * Send our public key. We include in the packet 64 bits of random
1890 * data that must be matched in the reply in order to prevent IP
1891 * spoofing.
1892 */
Damien Miller95def091999-11-25 00:26:21 +11001893 packet_start(SSH_SMSG_PUBLIC_KEY);
1894 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001895 packet_put_char(cookie[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001896
Damien Miller95def091999-11-25 00:26:21 +11001897 /* Store our public server RSA key. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001898 packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
1899 packet_put_bignum(sensitive_data.server_key->rsa->e);
1900 packet_put_bignum(sensitive_data.server_key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001901
Damien Miller95def091999-11-25 00:26:21 +11001902 /* Store our public host RSA key. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001903 packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1904 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
1905 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001906
Damien Miller95def091999-11-25 00:26:21 +11001907 /* Put protocol flags. */
1908 packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001909
Damien Miller95def091999-11-25 00:26:21 +11001910 /* Declare which ciphers we support. */
Damien Miller874d77b2000-10-14 16:23:11 +11001911 packet_put_int(cipher_mask_ssh1(0));
Damien Miller95def091999-11-25 00:26:21 +11001912
1913 /* Declare supported authentication types. */
1914 auth_mask = 0;
Damien Miller95def091999-11-25 00:26:21 +11001915 if (options.rhosts_rsa_authentication)
1916 auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
1917 if (options.rsa_authentication)
1918 auth_mask |= 1 << SSH_AUTH_RSA;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001919 if (options.challenge_response_authentication == 1)
Damien Miller95def091999-11-25 00:26:21 +11001920 auth_mask |= 1 << SSH_AUTH_TIS;
Damien Miller95def091999-11-25 00:26:21 +11001921 if (options.password_authentication)
1922 auth_mask |= 1 << SSH_AUTH_PASSWORD;
1923 packet_put_int(auth_mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001924
Damien Miller95def091999-11-25 00:26:21 +11001925 /* Send the packet and wait for it to be sent. */
1926 packet_send();
1927 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001928
Damien Miller0bc1bd82000-11-13 22:57:25 +11001929 debug("Sent %d bit server key and %d bit host key.",
1930 BN_num_bits(sensitive_data.server_key->rsa->n),
1931 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001932
Damien Miller95def091999-11-25 00:26:21 +11001933 /* Read clients reply (cipher type and session key). */
Damien Millerdff50992002-01-22 23:16:32 +11001934 packet_read_expect(SSH_CMSG_SESSION_KEY);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001935
Damien Miller50945fa1999-12-09 10:31:37 +11001936 /* Get cipher type and check whether we accept this. */
Damien Miller95def091999-11-25 00:26:21 +11001937 cipher_type = packet_get_char();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001938
Damien Miller874d77b2000-10-14 16:23:11 +11001939 if (!(cipher_mask_ssh1(0) & (1 << cipher_type)))
Damien Miller50945fa1999-12-09 10:31:37 +11001940 packet_disconnect("Warning: client selects unsupported cipher.");
1941
Damien Miller95def091999-11-25 00:26:21 +11001942 /* Get check bytes from the packet. These must match those we
1943 sent earlier with the public key packet. */
1944 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001945 if (cookie[i] != packet_get_char())
Damien Miller95def091999-11-25 00:26:21 +11001946 packet_disconnect("IP Spoofing check bytes do not match.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001947
Damien Miller95def091999-11-25 00:26:21 +11001948 debug("Encryption type: %.200s", cipher_name(cipher_type));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001949
Damien Miller95def091999-11-25 00:26:21 +11001950 /* Get the encrypted integer. */
Damien Millerda755162002-01-22 23:09:22 +11001951 if ((session_key_int = BN_new()) == NULL)
1952 fatal("do_ssh1_kex: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +11001953 packet_get_bignum(session_key_int);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001954
Damien Miller95def091999-11-25 00:26:21 +11001955 protocol_flags = packet_get_int();
1956 packet_set_protocol_flags(protocol_flags);
Damien Miller48b03fc2002-01-22 23:11:40 +11001957 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001958
Ben Lindstromabcb1452002-03-22 01:10:21 +00001959 /* Decrypt session_key_int using host/server keys */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001960 rsafail = PRIVSEP(ssh1_session_key(session_key_int));
1961
Damien Miller5428f641999-11-25 11:54:57 +11001962 /*
1963 * Extract session key from the decrypted integer. The key is in the
1964 * least significant 256 bits of the integer; the first byte of the
1965 * key is in the highest bits.
1966 */
Damien Miller7650bc62001-01-30 09:27:26 +11001967 if (!rsafail) {
1968 BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1969 len = BN_num_bytes(session_key_int);
Damien Millereccb9de2005-06-17 12:59:34 +10001970 if (len < 0 || (u_int)len > sizeof(session_key)) {
Damien Miller7650bc62001-01-30 09:27:26 +11001971 error("do_connection: bad session key len from %s: "
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001972 "session_key_int %d > sizeof(session_key) %lu",
1973 get_remote_ipaddr(), len, (u_long)sizeof(session_key));
Damien Miller7650bc62001-01-30 09:27:26 +11001974 rsafail++;
1975 } else {
1976 memset(session_key, 0, sizeof(session_key));
1977 BN_bn2bin(session_key_int,
1978 session_key + sizeof(session_key) - len);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001979
Darren Tuckere14e0052004-05-13 16:30:44 +10001980 derive_ssh1_session_id(
Darren Tuckerfc959702004-07-17 16:12:08 +10001981 sensitive_data.ssh1_host_key->rsa->n,
Darren Tuckere14e0052004-05-13 16:30:44 +10001982 sensitive_data.server_key->rsa->n,
1983 cookie, session_id);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001984 /*
1985 * Xor the first 16 bytes of the session key with the
1986 * session id.
1987 */
1988 for (i = 0; i < 16; i++)
1989 session_key[i] ^= session_id[i];
Damien Miller7650bc62001-01-30 09:27:26 +11001990 }
1991 }
1992 if (rsafail) {
Ben Lindstromeb648a72001-03-05 06:00:29 +00001993 int bytes = BN_num_bytes(session_key_int);
Ben Lindstrom13c5d3b2002-02-26 18:00:48 +00001994 u_char *buf = xmalloc(bytes);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001995 MD5_CTX md;
1996
Damien Miller996acd22003-04-09 20:59:48 +10001997 logit("do_connection: generating a fake encryption key");
Ben Lindstromeb648a72001-03-05 06:00:29 +00001998 BN_bn2bin(session_key_int, buf);
1999 MD5_Init(&md);
2000 MD5_Update(&md, buf, bytes);
2001 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
2002 MD5_Final(session_key, &md);
2003 MD5_Init(&md);
2004 MD5_Update(&md, session_key, 16);
2005 MD5_Update(&md, buf, bytes);
2006 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
2007 MD5_Final(session_key + 16, &md);
2008 memset(buf, 0, bytes);
2009 xfree(buf);
Ben Lindstrom941ac822001-03-05 06:25:23 +00002010 for (i = 0; i < 16; i++)
2011 session_id[i] = session_key[i] ^ session_key[i + 16];
Damien Miller7650bc62001-01-30 09:27:26 +11002012 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002013 /* Destroy the private and public keys. No longer. */
Damien Miller3a5b0232002-03-13 13:19:42 +11002014 destroy_sensitive_data();
Ben Lindstromeb648a72001-03-05 06:00:29 +00002015
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002016 if (use_privsep)
2017 mm_ssh1_session_id(session_id);
2018
Damien Miller396691a2000-01-20 22:44:08 +11002019 /* Destroy the decrypted integer. It is no longer needed. */
2020 BN_clear_free(session_key_int);
2021
Damien Miller95def091999-11-25 00:26:21 +11002022 /* Set the session key. From this on all communications will be encrypted. */
2023 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002024
Damien Miller95def091999-11-25 00:26:21 +11002025 /* Destroy our copy of the session key. It is no longer needed. */
2026 memset(session_key, 0, sizeof(session_key));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002027
Damien Miller95def091999-11-25 00:26:21 +11002028 debug("Received session key; encryption turned on.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002029
Ben Lindstromf666fec2002-06-06 19:51:58 +00002030 /* Send an acknowledgment packet. Note that this packet is sent encrypted. */
Damien Miller95def091999-11-25 00:26:21 +11002031 packet_start(SSH_SMSG_SUCCESS);
2032 packet_send();
2033 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002034}
Damien Millerefb4afe2000-04-12 18:45:05 +10002035
2036/*
2037 * SSH2 key exchange: diffie-hellman-group1-sha1
2038 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002039static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00002040do_ssh2_kex(void)
Damien Millerefb4afe2000-04-12 18:45:05 +10002041{
Damien Millerefb4afe2000-04-12 18:45:05 +10002042 Kex *kex;
Damien Millerefb4afe2000-04-12 18:45:05 +10002043
Damien Miller78928792000-04-12 20:17:38 +10002044 if (options.ciphers != NULL) {
Damien Miller4af51302000-04-16 11:18:38 +10002045 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Damien Miller78928792000-04-12 20:17:38 +10002046 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
2047 }
Damien Millera0ff4662001-03-30 10:49:35 +10002048 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
2049 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
2050 myproposal[PROPOSAL_ENC_ALGS_STOC] =
2051 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
2052
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00002053 if (options.macs != NULL) {
2054 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
2055 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
2056 }
Damien Miller9786e6e2005-07-26 21:54:56 +10002057 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +00002058 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
2059 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
Damien Miller9786e6e2005-07-26 21:54:56 +10002060 } else if (options.compression == COMP_DELAYED) {
2061 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
2062 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com";
Ben Lindstrom23e0f662002-06-21 01:09:47 +00002063 }
Damien Millerf0b15df2006-03-26 13:59:20 +11002064
Damien Miller0bc1bd82000-11-13 22:57:25 +11002065 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = list_hostkey_types();
2066
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002067 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +00002068 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +11002069 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10002070 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Miller8e7fb332003-02-24 12:03:03 +11002071 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11002072 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00002073 kex->server = 1;
2074 kex->client_version_string=client_version_string;
2075 kex->server_version_string=server_version_string;
2076 kex->load_host_key=&get_hostkey_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002077 kex->host_key_index=&get_hostkey_index;
Damien Millerefb4afe2000-04-12 18:45:05 +10002078
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002079 xxx_kex = kex;
2080
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002081 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +11002082
Ben Lindstrom2d90e002001-04-04 02:00:54 +00002083 session_id2 = kex->session_id;
2084 session_id2_len = kex->session_id_len;
2085
Damien Miller874d77b2000-10-14 16:23:11 +11002086#ifdef DEBUG_KEXDH
2087 /* send 1st encrypted/maced/compressed message */
2088 packet_start(SSH2_MSG_IGNORE);
2089 packet_put_cstring("markus");
2090 packet_send();
2091 packet_write_wait();
2092#endif
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00002093 debug("KEX done");
Damien Millerefb4afe2000-04-12 18:45:05 +10002094}
Darren Tucker3e33cec2003-10-02 16:12:36 +10002095
2096/* server specific fatal cleanup */
2097void
2098cleanup_exit(int i)
2099{
2100 if (the_authctxt)
2101 do_cleanup(the_authctxt);
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11002102#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11002103 /* done after do_cleanup so it can cancel the PAM auth 'thread' */
2104 if (!use_privsep || mm_is_monitor())
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11002105 audit_event(SSH_CONNECTION_ABANDON);
Darren Tucker269a1ea2005-02-03 00:20:53 +11002106#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002107 _exit(i);
2108}