blob: 602116edfac9d774af5b70335afd17839b98e652 [file] [log] [blame]
Damien Miller57cf6382006-07-10 21:13:46 +10001/* $OpenBSD: sshd.c,v 1.335 2006/07/09 15:15:11 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11006 * This program is the ssh daemon. It listens for connections from clients,
7 * and performs authentication, executes use commands or shell, and forwards
Damien Miller95def091999-11-25 00:26:21 +11008 * information to/from the application to the user client over an encrypted
Damien Millere4340be2000-09-16 13:29:08 +11009 * connection. This can also handle forwarding of X11, TCP/IP, and
10 * authentication agent connections.
Damien Millerefb4afe2000-04-12 18:45:05 +100011 *
Damien Millere4340be2000-09-16 13:29:08 +110012 * As far as I am concerned, the code I have written for this software
13 * can be used freely for any purpose. Any derived versions of this
14 * software must be clearly marked as such, and if the derived work is
15 * incompatible with the protocol description in the RFC file, it must be
16 * called by a name other than "ssh" or "Secure Shell".
17 *
18 * SSH2 implementation:
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000019 * Privilege Separation:
Damien Millere4340be2000-09-16 13:29:08 +110020 *
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000021 * Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved.
22 * Copyright (c) 2002 Niels Provos. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110023 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
34 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
36 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
37 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
42 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110043 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110046
Damien Miller9cf6d072006-03-15 11:29:24 +110047#include <sys/types.h>
Damien Millerf17883e2006-03-15 11:45:54 +110048#ifdef HAVE_SYS_STAT_H
49# include <sys/stat.h>
50#endif
51#include <sys/ioctl.h>
Damien Millere3b60b52006-07-10 21:08:03 +100052#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110053#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110054
Damien Miller57cf6382006-07-10 21:13:46 +100055#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110056#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110057#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110058#endif
Damien Millera1738e42006-07-10 21:33:04 +100059#include <grp.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100060#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110061#include <signal.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include <openssl/dh.h>
64#include <openssl/bn.h>
Damien Miller6a47f302002-02-13 13:55:06 +110065#include <openssl/md5.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000066#include <openssl/rand.h>
Kevin Steves0ea1d9d2002-04-25 18:17:04 +000067#ifdef HAVE_SECUREWARE
68#include <sys/security.h>
69#include <prot.h>
70#endif
Ben Lindstrom226cfa02001-01-22 05:34:40 +000071
72#include "ssh.h"
73#include "ssh1.h"
74#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075#include "xmalloc.h"
76#include "rsa.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000077#include "sshpty.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000079#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080#include "servconf.h"
81#include "uidswap.h"
82#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100083#include "buffer.h"
Darren Tucker645ab752004-06-25 13:33:20 +100084#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000085#include "cipher.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100086#include "kex.h"
Damien Millerb38eff82000-04-01 11:09:21 +100087#include "key.h"
Damien Miller874d77b2000-10-14 16:23:11 +110088#include "dh.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100089#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100090#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000091#include "pathnames.h"
92#include "atomicio.h"
93#include "canohost.h"
94#include "auth.h"
95#include "misc.h"
Darren Tucker645ab752004-06-25 13:33:20 +100096#include "msg.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000097#include "dispatch.h"
Ben Lindstrom1bae4042001-10-03 17:46:39 +000098#include "channels.h"
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +000099#include "session.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000100#include "monitor_mm.h"
101#include "monitor.h"
102#include "monitor_wrap.h"
103#include "monitor_fdpass.h"
Damien Millerb7576772006-07-10 20:23:39 +1000104#include "version.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
106#ifdef LIBWRAP
107#include <tcpd.h>
108#include <syslog.h>
109int allow_severity = LOG_INFO;
110int deny_severity = LOG_WARNING;
111#endif /* LIBWRAP */
112
113#ifndef O_NOCTTY
114#define O_NOCTTY 0
115#endif
116
Damien Miller035a5b42004-06-26 08:16:31 +1000117/* Re-exec fds */
118#define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1)
119#define REEXEC_STARTUP_PIPE_FD (STDERR_FILENO + 2)
120#define REEXEC_CONFIG_PASS_FD (STDERR_FILENO + 3)
121#define REEXEC_MIN_FREE_FD (STDERR_FILENO + 4)
122
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000123extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000124
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125/* Server configuration options. */
126ServerOptions options;
127
128/* Name of the server configuration file. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000129char *config_file_name = _PATH_SERVER_CONFIG_FILE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130
Damien Miller4af51302000-04-16 11:18:38 +1000131/*
Damien Miller95def091999-11-25 00:26:21 +1100132 * Debug mode flag. This can be set on the command line. If debug
133 * mode is enabled, extra debugging output will be sent to the system
134 * log, the daemon will not go to background, and will exit after processing
135 * the first connection.
136 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137int debug_flag = 0;
138
Ben Lindstrom794325a2001-08-06 21:09:07 +0000139/* Flag indicating that the daemon should only test the configuration and keys. */
140int test_flag = 0;
141
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142/* Flag indicating that the daemon is being started from inetd. */
143int inetd_flag = 0;
144
Ben Lindstromc72745a2000-12-02 19:03:54 +0000145/* Flag indicating that sshd should not detach and become a daemon. */
146int no_daemon_flag = 0;
147
Damien Miller5ce662a1999-11-11 17:57:39 +1100148/* debug goes to stderr unless inetd_flag is set */
149int log_stderr = 0;
150
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151/* Saved arguments to main(). */
152char **saved_argv;
Damien Millerb8c656e2000-06-28 15:22:41 +1000153int saved_argc;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Darren Tucker645ab752004-06-25 13:33:20 +1000155/* re-exec */
156int rexeced_flag = 0;
157int rexec_flag = 1;
158int rexec_argc = 0;
159char **rexec_argv;
160
Damien Miller5428f641999-11-25 11:54:57 +1100161/*
Damien Miller34132e52000-01-14 15:45:46 +1100162 * The sockets that the server is listening; this is used in the SIGHUP
163 * signal handler.
Damien Miller5428f641999-11-25 11:54:57 +1100164 */
Damien Miller34132e52000-01-14 15:45:46 +1100165#define MAX_LISTEN_SOCKS 16
166int listen_socks[MAX_LISTEN_SOCKS];
167int num_listen_socks = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168
Damien Miller5428f641999-11-25 11:54:57 +1100169/*
170 * the client's version string, passed by sshd2 in compat mode. if != NULL,
171 * sshd will skip the version-number exchange
172 */
Damien Miller95def091999-11-25 00:26:21 +1100173char *client_version_string = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000174char *server_version_string = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000176/* for rekeying XXX fixme */
177Kex *xxx_kex;
178
Damien Miller5428f641999-11-25 11:54:57 +1100179/*
180 * Any really sensitive data in the application is contained in this
181 * structure. The idea is that this structure could be locked into memory so
182 * that the pages do not get written into swap. However, there are some
183 * problems. The private key contains BIGNUMs, and we do not (in principle)
184 * have access to the internals of them, and locking just the structure is
185 * not very useful. Currently, memory locking is not implemented.
186 */
Damien Miller95def091999-11-25 00:26:21 +1100187struct {
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000188 Key *server_key; /* ephemeral server key */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100189 Key *ssh1_host_key; /* ssh1 host key */
190 Key **host_keys; /* all private host keys */
191 int have_ssh1_key;
192 int have_ssh2_key;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000193 u_char ssh1_cookie[SSH_SESSION_KEY_LENGTH];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194} sensitive_data;
195
Damien Miller5428f641999-11-25 11:54:57 +1100196/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000197 * Flag indicating whether the RSA server key needs to be regenerated.
198 * Is set in the SIGALRM handler and cleared when the key is regenerated.
Damien Miller5428f641999-11-25 11:54:57 +1100199 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000200static volatile sig_atomic_t key_do_regen = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000202/* This is set to true when a signal is received. */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000203static volatile sig_atomic_t received_sighup = 0;
204static volatile sig_atomic_t received_sigterm = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205
Damien Millerb38eff82000-04-01 11:09:21 +1000206/* session identifier, used by RSA-auth */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000207u_char session_id[16];
Damien Millerb38eff82000-04-01 11:09:21 +1000208
Damien Millereba71ba2000-04-29 23:57:08 +1000209/* same for ssh2 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000210u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000211u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000212
Damien Miller942da032000-08-18 13:59:06 +1000213/* record remote hostname or ip */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000214u_int utmp_len = MAXHOSTNAMELEN;
Damien Miller942da032000-08-18 13:59:06 +1000215
Ben Lindstromd84df982001-12-06 16:35:40 +0000216/* options.max_startup sized array of fd ints */
217int *startup_pipes = NULL;
218int startup_pipe; /* in child */
219
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000220/* variables used for privilege separation */
Damien Miller8e7fb332003-02-24 12:03:03 +1100221int use_privsep;
Darren Tuckera8be9e22004-02-06 16:40:27 +1100222struct monitor *pmonitor = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000223
Darren Tucker3e33cec2003-10-02 16:12:36 +1000224/* global authentication context */
225Authctxt *the_authctxt = NULL;
226
Darren Tucker09991742004-07-17 17:05:14 +1000227/* message to be displayed after login */
228Buffer loginmsg;
229
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230/* Prototypes for various functions defined later in this file. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000231void destroy_sensitive_data(void);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000232void demote_sensitive_data(void);
Damien Miller98c7ad62000-03-09 21:27:49 +1100233
Ben Lindstrombba81212001-06-25 05:01:22 +0000234static void do_ssh1_kex(void);
235static void do_ssh2_kex(void);
Damien Miller874d77b2000-10-14 16:23:11 +1100236
Damien Miller98c7ad62000-03-09 21:27:49 +1100237/*
Damien Miller34132e52000-01-14 15:45:46 +1100238 * Close all listening sockets
239 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000240static void
Damien Miller34132e52000-01-14 15:45:46 +1100241close_listen_socks(void)
242{
243 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000244
Damien Miller34132e52000-01-14 15:45:46 +1100245 for (i = 0; i < num_listen_socks; i++)
246 close(listen_socks[i]);
247 num_listen_socks = -1;
248}
249
Ben Lindstromd84df982001-12-06 16:35:40 +0000250static void
251close_startup_pipes(void)
252{
253 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000254
Ben Lindstromd84df982001-12-06 16:35:40 +0000255 if (startup_pipes)
256 for (i = 0; i < options.max_startups; i++)
257 if (startup_pipes[i] != -1)
258 close(startup_pipes[i]);
259}
260
Damien Miller34132e52000-01-14 15:45:46 +1100261/*
Damien Miller95def091999-11-25 00:26:21 +1100262 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
263 * the effect is to reread the configuration file (and to regenerate
264 * the server key).
265 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100266
267/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000268static void
Damien Miller95def091999-11-25 00:26:21 +1100269sighup_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270{
Ben Lindstrom07958482001-12-06 16:19:01 +0000271 int save_errno = errno;
272
Damien Miller95def091999-11-25 00:26:21 +1100273 received_sighup = 1;
274 signal(SIGHUP, sighup_handler);
Ben Lindstrom07958482001-12-06 16:19:01 +0000275 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276}
277
Damien Miller95def091999-11-25 00:26:21 +1100278/*
279 * Called from the main program after receiving SIGHUP.
280 * Restarts the server.
281 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000282static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000283sighup_restart(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284{
Damien Miller996acd22003-04-09 20:59:48 +1000285 logit("Received SIGHUP; restarting.");
Damien Miller34132e52000-01-14 15:45:46 +1100286 close_listen_socks();
Ben Lindstromd84df982001-12-06 16:35:40 +0000287 close_startup_pipes();
Damien Miller95def091999-11-25 00:26:21 +1100288 execv(saved_argv[0], saved_argv);
Damien Miller996acd22003-04-09 20:59:48 +1000289 logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
Ben Lindstrom822b6342002-06-23 21:38:49 +0000290 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100291 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292}
293
Damien Miller95def091999-11-25 00:26:21 +1100294/*
295 * Generic signal handler for terminating signals in the master daemon.
Damien Miller95def091999-11-25 00:26:21 +1100296 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100297/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000298static void
Damien Miller95def091999-11-25 00:26:21 +1100299sigterm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000301 received_sigterm = sig;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302}
303
Damien Miller95def091999-11-25 00:26:21 +1100304/*
305 * SIGCHLD handler. This is called whenever a child dies. This will then
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000306 * reap any zombies left by exited children.
Damien Miller95def091999-11-25 00:26:21 +1100307 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100308/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000309static void
Damien Miller95def091999-11-25 00:26:21 +1100310main_sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311{
Damien Miller95def091999-11-25 00:26:21 +1100312 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000313 pid_t pid;
Damien Miller95def091999-11-25 00:26:21 +1100314 int status;
Damien Miller431f66b1999-11-21 18:31:57 +1100315
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000316 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
317 (pid < 0 && errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100318 ;
Damien Miller431f66b1999-11-21 18:31:57 +1100319
Damien Miller95def091999-11-25 00:26:21 +1100320 signal(SIGCHLD, main_sigchld_handler);
321 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322}
323
Damien Miller95def091999-11-25 00:26:21 +1100324/*
325 * Signal handler for the alarm after the login grace period has expired.
326 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100327/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000328static void
Damien Miller95def091999-11-25 00:26:21 +1100329grace_alarm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000331 /* XXX no idea how fix this signal handler */
332
Darren Tuckera8be9e22004-02-06 16:40:27 +1100333 if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0)
334 kill(pmonitor->m_pid, SIGALRM);
335
Damien Miller95def091999-11-25 00:26:21 +1100336 /* Log error and exit. */
Damien Millerd27a76d2002-09-27 13:22:31 +1000337 fatal("Timeout before authentication for %s", get_remote_ipaddr());
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338}
339
Damien Miller95def091999-11-25 00:26:21 +1100340/*
Damien Miller95def091999-11-25 00:26:21 +1100341 * Signal handler for the key regeneration alarm. Note that this
342 * alarm only occurs in the daemon waiting for connections, and it does not
343 * do anything with the private key or random state before forking.
344 * Thus there should be no concurrency control/asynchronous execution
345 * problems.
346 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000347static void
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000348generate_ephemeral_server_key(void)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100349{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000350 u_int32_t rnd = 0;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000351 int i;
352
Ben Lindstroma3700052001-04-05 23:26:32 +0000353 verbose("Generating %s%d bit RSA key.",
Damien Millerff75ac42001-03-30 10:50:32 +1000354 sensitive_data.server_key ? "new " : "", options.server_key_bits);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100355 if (sensitive_data.server_key != NULL)
356 key_free(sensitive_data.server_key);
Ben Lindstroma3700052001-04-05 23:26:32 +0000357 sensitive_data.server_key = key_generate(KEY_RSA1,
Damien Millerff75ac42001-03-30 10:50:32 +1000358 options.server_key_bits);
359 verbose("RSA key generation complete.");
Ben Lindstromeb648a72001-03-05 06:00:29 +0000360
361 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
362 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000363 rnd = arc4random();
364 sensitive_data.ssh1_cookie[i] = rnd & 0xff;
365 rnd >>= 8;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000366 }
367 arc4random_stir();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100368}
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000369
Damien Millerf0b15df2006-03-26 13:59:20 +1100370/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000371static void
Damien Miller95def091999-11-25 00:26:21 +1100372key_regeneration_alarm(int sig)
373{
374 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000375
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000376 signal(SIGALRM, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +1100377 errno = save_errno;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000378 key_do_regen = 1;
Damien Miller95def091999-11-25 00:26:21 +1100379}
380
Ben Lindstrombba81212001-06-25 05:01:22 +0000381static void
Damien Millerb38eff82000-04-01 11:09:21 +1000382sshd_exchange_identification(int sock_in, int sock_out)
383{
Damien Millereccb9de2005-06-17 12:59:34 +1000384 u_int i;
385 int mismatch;
Damien Millerb38eff82000-04-01 11:09:21 +1000386 int remote_major, remote_minor;
Damien Miller78928792000-04-12 20:17:38 +1000387 int major, minor;
Damien Millerb38eff82000-04-01 11:09:21 +1000388 char *s;
389 char buf[256]; /* Must not be larger than remote_version. */
390 char remote_version[256]; /* Must be at least as big as buf. */
391
Damien Miller78928792000-04-12 20:17:38 +1000392 if ((options.protocol & SSH_PROTO_1) &&
393 (options.protocol & SSH_PROTO_2)) {
394 major = PROTOCOL_MAJOR_1;
395 minor = 99;
396 } else if (options.protocol & SSH_PROTO_2) {
397 major = PROTOCOL_MAJOR_2;
398 minor = PROTOCOL_MINOR_2;
399 } else {
400 major = PROTOCOL_MAJOR_1;
401 minor = PROTOCOL_MINOR_1;
402 }
403 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
Damien Millerb38eff82000-04-01 11:09:21 +1000404 server_version_string = xstrdup(buf);
405
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000406 /* Send our protocol version identification. */
407 if (atomicio(vwrite, sock_out, server_version_string,
408 strlen(server_version_string))
409 != strlen(server_version_string)) {
410 logit("Could not write ident string to %s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000411 cleanup_exit(255);
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000412 }
413
414 /* Read other sides version identification. */
415 memset(buf, 0, sizeof(buf));
416 for (i = 0; i < sizeof(buf) - 1; i++) {
417 if (atomicio(read, sock_in, &buf[i], 1) != 1) {
418 logit("Did not receive identification string from %s",
419 get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000420 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000421 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000422 if (buf[i] == '\r') {
423 buf[i] = 0;
424 /* Kludge for F-Secure Macintosh < 1.0.2 */
425 if (i == 12 &&
426 strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000427 break;
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000428 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000429 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000430 if (buf[i] == '\n') {
431 buf[i] = 0;
432 break;
433 }
Damien Millerb38eff82000-04-01 11:09:21 +1000434 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000435 buf[sizeof(buf) - 1] = 0;
436 client_version_string = xstrdup(buf);
Damien Millerb38eff82000-04-01 11:09:21 +1000437
438 /*
439 * Check that the versions match. In future this might accept
440 * several versions and set appropriate flags to handle them.
441 */
442 if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
443 &remote_major, &remote_minor, remote_version) != 3) {
Damien Miller4af51302000-04-16 11:18:38 +1000444 s = "Protocol mismatch.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000445 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Millerb38eff82000-04-01 11:09:21 +1000446 close(sock_in);
447 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000448 logit("Bad protocol version identification '%.100s' from %s",
Damien Millerb38eff82000-04-01 11:09:21 +1000449 client_version_string, get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000450 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000451 }
452 debug("Client protocol version %d.%d; client software version %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100453 remote_major, remote_minor, remote_version);
Damien Millerb38eff82000-04-01 11:09:21 +1000454
Damien Millerefb4afe2000-04-12 18:45:05 +1000455 compat_datafellows(remote_version);
456
Damien Millere9264972002-09-30 11:59:21 +1000457 if (datafellows & SSH_BUG_PROBE) {
Damien Miller996acd22003-04-09 20:59:48 +1000458 logit("probed from %s with %s. Don't panic.",
Damien Millere9264972002-09-30 11:59:21 +1000459 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000460 cleanup_exit(255);
Damien Millere9264972002-09-30 11:59:21 +1000461 }
462
Damien Miller27dbe6f2001-03-19 22:36:20 +1100463 if (datafellows & SSH_BUG_SCANNER) {
Damien Miller996acd22003-04-09 20:59:48 +1000464 logit("scanned from %s with %s. Don't panic.",
Damien Miller27dbe6f2001-03-19 22:36:20 +1100465 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000466 cleanup_exit(255);
Damien Miller27dbe6f2001-03-19 22:36:20 +1100467 }
468
Damien Miller78928792000-04-12 20:17:38 +1000469 mismatch = 0;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000470 switch (remote_major) {
Damien Millerb38eff82000-04-01 11:09:21 +1000471 case 1:
Damien Millereba71ba2000-04-29 23:57:08 +1000472 if (remote_minor == 99) {
473 if (options.protocol & SSH_PROTO_2)
474 enable_compat20();
475 else
476 mismatch = 1;
477 break;
478 }
Damien Miller78928792000-04-12 20:17:38 +1000479 if (!(options.protocol & SSH_PROTO_1)) {
480 mismatch = 1;
481 break;
482 }
Damien Millerb38eff82000-04-01 11:09:21 +1000483 if (remote_minor < 3) {
Damien Miller37023962000-07-11 17:31:38 +1000484 packet_disconnect("Your ssh version is too old and "
Damien Millerb38eff82000-04-01 11:09:21 +1000485 "is no longer supported. Please install a newer version.");
486 } else if (remote_minor == 3) {
487 /* note that this disables agent-forwarding */
488 enable_compat13();
489 }
Damien Miller78928792000-04-12 20:17:38 +1000490 break;
Damien Millerefb4afe2000-04-12 18:45:05 +1000491 case 2:
Damien Miller78928792000-04-12 20:17:38 +1000492 if (options.protocol & SSH_PROTO_2) {
Damien Millerefb4afe2000-04-12 18:45:05 +1000493 enable_compat20();
494 break;
495 }
496 /* FALLTHROUGH */
Damien Miller4af51302000-04-16 11:18:38 +1000497 default:
Damien Miller78928792000-04-12 20:17:38 +1000498 mismatch = 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000499 break;
500 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000501 chop(server_version_string);
Damien Miller78928792000-04-12 20:17:38 +1000502 debug("Local version string %.200s", server_version_string);
503
504 if (mismatch) {
505 s = "Protocol major versions differ.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000506 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Miller78928792000-04-12 20:17:38 +1000507 close(sock_in);
508 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000509 logit("Protocol major versions differ for %s: %.200s vs. %.200s",
Damien Miller78928792000-04-12 20:17:38 +1000510 get_remote_ipaddr(),
511 server_version_string, client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000512 cleanup_exit(255);
Damien Miller78928792000-04-12 20:17:38 +1000513 }
Damien Millereba71ba2000-04-29 23:57:08 +1000514}
515
Damien Miller0bc1bd82000-11-13 22:57:25 +1100516/* Destroy the host and server keys. They will no longer be needed. */
Damien Millereba71ba2000-04-29 23:57:08 +1000517void
518destroy_sensitive_data(void)
519{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100520 int i;
521
522 if (sensitive_data.server_key) {
523 key_free(sensitive_data.server_key);
524 sensitive_data.server_key = NULL;
525 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100526 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100527 if (sensitive_data.host_keys[i]) {
528 key_free(sensitive_data.host_keys[i]);
529 sensitive_data.host_keys[i] = NULL;
530 }
531 }
532 sensitive_data.ssh1_host_key = NULL;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000533 memset(sensitive_data.ssh1_cookie, 0, SSH_SESSION_KEY_LENGTH);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100534}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100535
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000536/* Demote private to public keys for network child */
537void
538demote_sensitive_data(void)
539{
540 Key *tmp;
541 int i;
542
543 if (sensitive_data.server_key) {
544 tmp = key_demote(sensitive_data.server_key);
545 key_free(sensitive_data.server_key);
546 sensitive_data.server_key = tmp;
547 }
548
549 for (i = 0; i < options.num_host_key_files; i++) {
550 if (sensitive_data.host_keys[i]) {
551 tmp = key_demote(sensitive_data.host_keys[i]);
552 key_free(sensitive_data.host_keys[i]);
553 sensitive_data.host_keys[i] = tmp;
554 if (tmp->type == KEY_RSA1)
555 sensitive_data.ssh1_host_key = tmp;
556 }
557 }
558
559 /* We do not clear ssh1_host key and cookie. XXX - Okay Niels? */
560}
561
Ben Lindstrom08105192002-03-22 02:50:06 +0000562static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000563privsep_preauth_child(void)
564{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000565 u_int32_t rnd[256];
Ben Lindstrom810af962002-07-04 00:11:40 +0000566 gid_t gidset[1];
Ben Lindstromc7431342002-03-22 03:11:49 +0000567 struct passwd *pw;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000568 int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000569
570 /* Enable challenge-response authentication for privilege separation */
571 privsep_challenge_enable();
572
573 for (i = 0; i < 256; i++)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000574 rnd[i] = arc4random();
575 RAND_seed(rnd, sizeof(rnd));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000576
577 /* Demote the private keys to public keys. */
578 demote_sensitive_data();
579
Ben Lindstromc7431342002-03-22 03:11:49 +0000580 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
Damien Miller0150c652002-04-24 09:49:09 +1000581 fatal("Privilege separation user %s does not exist",
582 SSH_PRIVSEP_USER);
Ben Lindstromc7431342002-03-22 03:11:49 +0000583 memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
584 endpwent();
585
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000586 /* Change our root directory */
Ben Lindstrom7a7edf72002-03-22 02:42:37 +0000587 if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
588 fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
589 strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000590 if (chdir("/") == -1)
Ben Lindstrom1ee9ec32002-03-22 03:14:45 +0000591 fatal("chdir(\"/\"): %s", strerror(errno));
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000592
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000593 /* Drop our privileges */
Ben Lindstromc7431342002-03-22 03:11:49 +0000594 debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
595 (u_int)pw->pw_gid);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000596#if 0
Darren Tuckerd5920482004-02-29 20:11:30 +1100597 /* XXX not ready, too heavy after chroot */
Ben Lindstromc7431342002-03-22 03:11:49 +0000598 do_setusercontext(pw);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000599#else
600 gidset[0] = pw->pw_gid;
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000601 if (setgroups(1, gidset) < 0)
602 fatal("setgroups: %.100s", strerror(errno));
603 permanently_set_uid(pw);
604#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605}
606
Darren Tucker3e33cec2003-10-02 16:12:36 +1000607static int
608privsep_preauth(Authctxt *authctxt)
Ben Lindstrom943481c2002-03-22 03:43:46 +0000609{
Ben Lindstrom943481c2002-03-22 03:43:46 +0000610 int status;
611 pid_t pid;
612
613 /* Set up unprivileged child process to deal with network data */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000614 pmonitor = monitor_init();
Ben Lindstrom943481c2002-03-22 03:43:46 +0000615 /* Store a pointer to the kex for later rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000616 pmonitor->m_pkex = &xxx_kex;
Ben Lindstrom943481c2002-03-22 03:43:46 +0000617
618 pid = fork();
619 if (pid == -1) {
620 fatal("fork of unprivileged child failed");
621 } else if (pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000622 debug2("Network child is on pid %ld", (long)pid);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000623
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000624 close(pmonitor->m_recvfd);
Darren Tuckera8be9e22004-02-06 16:40:27 +1100625 pmonitor->m_pid = pid;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000626 monitor_child_preauth(authctxt, pmonitor);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000627 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000628
629 /* Sync memory */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000630 monitor_sync(pmonitor);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000631
632 /* Wait for the child's exit status */
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000633 while (waitpid(pid, &status, 0) < 0)
634 if (errno != EINTR)
635 break;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000636 return (1);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000637 } else {
638 /* child */
639
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000640 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000641
642 /* Demote the child */
643 if (getuid() == 0 || geteuid() == 0)
644 privsep_preauth_child();
Ben Lindstromf90f58d2002-03-26 01:53:03 +0000645 setproctitle("%s", "[net]");
Ben Lindstrom943481c2002-03-22 03:43:46 +0000646 }
Darren Tucker3e33cec2003-10-02 16:12:36 +1000647 return (0);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000648}
649
Ben Lindstrom08105192002-03-22 02:50:06 +0000650static void
Ben Lindstrom943481c2002-03-22 03:43:46 +0000651privsep_postauth(Authctxt *authctxt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000652{
Tim Rice9dd30812002-07-07 13:43:36 -0700653#ifdef DISABLE_FD_PASSING
Tim Rice8eff3192002-06-25 15:35:15 -0700654 if (1) {
655#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000656 if (authctxt->pw->pw_uid == 0 || options.use_login) {
Tim Rice8eff3192002-06-25 15:35:15 -0700657#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000658 /* File descriptor passing is broken or root login */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000659 use_privsep = 0;
Darren Tucker45b01422005-10-03 18:20:00 +1000660 goto skip;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000661 }
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000662
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663 /* New socket pair */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000664 monitor_reinit(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000665
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000666 pmonitor->m_pid = fork();
667 if (pmonitor->m_pid == -1)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000668 fatal("fork of unprivileged child failed");
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000669 else if (pmonitor->m_pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000670 debug2("User child is on pid %ld", (long)pmonitor->m_pid);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000671 close(pmonitor->m_recvfd);
Darren Tuckereb578622004-08-12 23:08:14 +1000672 buffer_clear(&loginmsg);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000673 monitor_child_postauth(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000674
675 /* NEVERREACHED */
676 exit(0);
677 }
678
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000679 close(pmonitor->m_sendfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000680
681 /* Demote the private keys to public keys. */
682 demote_sensitive_data();
683
684 /* Drop privileges */
685 do_setusercontext(authctxt->pw);
686
Darren Tucker45b01422005-10-03 18:20:00 +1000687 skip:
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000688 /* It is safe now to apply the key state */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000689 monitor_apply_keystate(pmonitor);
Damien Miller9786e6e2005-07-26 21:54:56 +1000690
691 /*
692 * Tell the packet layer that authentication was successful, since
693 * this information is not part of the key state.
694 */
695 packet_set_authenticated();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000696}
697
Ben Lindstrombba81212001-06-25 05:01:22 +0000698static char *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100699list_hostkey_types(void)
700{
Damien Miller0e3b8722002-01-22 23:26:38 +1100701 Buffer b;
Damien Millerf58b58c2003-11-17 21:18:23 +1100702 const char *p;
703 char *ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100704 int i;
Damien Miller0e3b8722002-01-22 23:26:38 +1100705
706 buffer_init(&b);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100707 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100708 Key *key = sensitive_data.host_keys[i];
709 if (key == NULL)
710 continue;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000711 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100712 case KEY_RSA:
713 case KEY_DSA:
Damien Miller0e3b8722002-01-22 23:26:38 +1100714 if (buffer_len(&b) > 0)
715 buffer_append(&b, ",", 1);
716 p = key_ssh_name(key);
717 buffer_append(&b, p, strlen(p));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100718 break;
719 }
720 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100721 buffer_append(&b, "\0", 1);
Damien Millerf58b58c2003-11-17 21:18:23 +1100722 ret = xstrdup(buffer_ptr(&b));
Damien Miller0e3b8722002-01-22 23:26:38 +1100723 buffer_free(&b);
Damien Millerf58b58c2003-11-17 21:18:23 +1100724 debug("list_hostkey_types: %s", ret);
725 return ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100726}
727
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728Key *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100729get_hostkey_by_type(int type)
730{
731 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000732
Damien Miller9f0f5c62001-12-21 14:45:46 +1100733 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100734 Key *key = sensitive_data.host_keys[i];
735 if (key != NULL && key->type == type)
736 return key;
737 }
738 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000739}
740
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000741Key *
742get_hostkey_by_index(int ind)
743{
744 if (ind < 0 || ind >= options.num_host_key_files)
745 return (NULL);
746 return (sensitive_data.host_keys[ind]);
747}
748
749int
750get_hostkey_index(Key *key)
751{
752 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000753
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000754 for (i = 0; i < options.num_host_key_files; i++) {
755 if (key == sensitive_data.host_keys[i])
756 return (i);
757 }
758 return (-1);
759}
760
Damien Miller942da032000-08-18 13:59:06 +1000761/*
762 * returns 1 if connection should be dropped, 0 otherwise.
763 * dropping starts at connection #max_startups_begin with a probability
764 * of (max_startups_rate/100). the probability increases linearly until
765 * all connections are dropped for startups > max_startups
766 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000767static int
Damien Miller942da032000-08-18 13:59:06 +1000768drop_connection(int startups)
769{
Darren Tucker178fa662004-11-05 20:09:09 +1100770 int p, r;
Damien Miller942da032000-08-18 13:59:06 +1000771
772 if (startups < options.max_startups_begin)
773 return 0;
774 if (startups >= options.max_startups)
775 return 1;
776 if (options.max_startups_rate == 100)
777 return 1;
778
779 p = 100 - options.max_startups_rate;
780 p *= startups - options.max_startups_begin;
Darren Tucker178fa662004-11-05 20:09:09 +1100781 p /= options.max_startups - options.max_startups_begin;
Damien Miller942da032000-08-18 13:59:06 +1000782 p += options.max_startups_rate;
Darren Tucker178fa662004-11-05 20:09:09 +1100783 r = arc4random() % 100;
Damien Miller942da032000-08-18 13:59:06 +1000784
Darren Tucker3269b132004-11-05 20:20:59 +1100785 debug("drop_connection: p %d, r %d", p, r);
Damien Miller942da032000-08-18 13:59:06 +1000786 return (r < p) ? 1 : 0;
787}
788
Ben Lindstromade03f62001-12-06 18:22:17 +0000789static void
790usage(void)
791{
Damien Miller0c889cd2004-03-22 09:36:00 +1100792 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000793 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Millerb4087862004-03-22 09:35:21 +1100794 fprintf(stderr,
795"usage: sshd [-46Ddeiqt] [-b bits] [-f config_file] [-g login_grace_time]\n"
796" [-h host_key_file] [-k key_gen_time] [-o option] [-p port] [-u len]\n"
797 );
Ben Lindstromade03f62001-12-06 18:22:17 +0000798 exit(1);
799}
800
Darren Tucker645ab752004-06-25 13:33:20 +1000801static void
802send_rexec_state(int fd, Buffer *conf)
803{
804 Buffer m;
805
806 debug3("%s: entering fd = %d config len %d", __func__, fd,
807 buffer_len(conf));
808
809 /*
810 * Protocol from reexec master to child:
811 * string configuration
812 * u_int ephemeral_key_follows
813 * bignum e (only if ephemeral_key_follows == 1)
814 * bignum n "
815 * bignum d "
816 * bignum iqmp "
817 * bignum p "
818 * bignum q "
Darren Tuckerc6f82192005-09-27 22:46:32 +1000819 * string rngseed (only if OpenSSL is not self-seeded)
Darren Tucker645ab752004-06-25 13:33:20 +1000820 */
821 buffer_init(&m);
822 buffer_put_cstring(&m, buffer_ptr(conf));
823
Darren Tuckerfc959702004-07-17 16:12:08 +1000824 if (sensitive_data.server_key != NULL &&
Darren Tucker645ab752004-06-25 13:33:20 +1000825 sensitive_data.server_key->type == KEY_RSA1) {
826 buffer_put_int(&m, 1);
827 buffer_put_bignum(&m, sensitive_data.server_key->rsa->e);
828 buffer_put_bignum(&m, sensitive_data.server_key->rsa->n);
829 buffer_put_bignum(&m, sensitive_data.server_key->rsa->d);
830 buffer_put_bignum(&m, sensitive_data.server_key->rsa->iqmp);
831 buffer_put_bignum(&m, sensitive_data.server_key->rsa->p);
832 buffer_put_bignum(&m, sensitive_data.server_key->rsa->q);
833 } else
834 buffer_put_int(&m, 0);
835
Darren Tuckerc6f82192005-09-27 22:46:32 +1000836#ifndef OPENSSL_PRNG_ONLY
837 rexec_send_rng_seed(&m);
838#endif
839
Darren Tucker645ab752004-06-25 13:33:20 +1000840 if (ssh_msg_send(fd, 0, &m) == -1)
841 fatal("%s: ssh_msg_send failed", __func__);
842
843 buffer_free(&m);
844
845 debug3("%s: done", __func__);
846}
847
848static void
849recv_rexec_state(int fd, Buffer *conf)
850{
851 Buffer m;
852 char *cp;
853 u_int len;
854
855 debug3("%s: entering fd = %d", __func__, fd);
856
857 buffer_init(&m);
858
859 if (ssh_msg_recv(fd, &m) == -1)
860 fatal("%s: ssh_msg_recv failed", __func__);
861 if (buffer_get_char(&m) != 0)
862 fatal("%s: rexec version mismatch", __func__);
863
864 cp = buffer_get_string(&m, &len);
865 if (conf != NULL)
866 buffer_append(conf, cp, len + 1);
867 xfree(cp);
868
869 if (buffer_get_int(&m)) {
870 if (sensitive_data.server_key != NULL)
871 key_free(sensitive_data.server_key);
872 sensitive_data.server_key = key_new_private(KEY_RSA1);
873 buffer_get_bignum(&m, sensitive_data.server_key->rsa->e);
874 buffer_get_bignum(&m, sensitive_data.server_key->rsa->n);
875 buffer_get_bignum(&m, sensitive_data.server_key->rsa->d);
876 buffer_get_bignum(&m, sensitive_data.server_key->rsa->iqmp);
877 buffer_get_bignum(&m, sensitive_data.server_key->rsa->p);
878 buffer_get_bignum(&m, sensitive_data.server_key->rsa->q);
879 rsa_generate_additional_parameters(
880 sensitive_data.server_key->rsa);
881 }
Darren Tuckerc6f82192005-09-27 22:46:32 +1000882
883#ifndef OPENSSL_PRNG_ONLY
884 rexec_recv_rng_seed(&m);
885#endif
886
Darren Tucker645ab752004-06-25 13:33:20 +1000887 buffer_free(&m);
888
889 debug3("%s: done", __func__);
890}
891
Damien Miller95def091999-11-25 00:26:21 +1100892/*
893 * Main program for the daemon.
894 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895int
896main(int ac, char **av)
897{
Damien Miller95def091999-11-25 00:26:21 +1100898 extern char *optarg;
899 extern int optind;
Damien Miller07d86be2006-03-26 14:19:21 +1100900 int opt, j, i, on = 1;
Damien Miller386c6a22004-06-30 22:40:20 +1000901 int sock_in = -1, sock_out = -1, newsock = -1;
Damien Miller166fca82000-04-20 07:42:21 +1000902 pid_t pid;
Damien Miller34132e52000-01-14 15:45:46 +1100903 socklen_t fromlen;
Damien Miller34132e52000-01-14 15:45:46 +1100904 fd_set *fdset;
905 struct sockaddr_storage from;
Damien Miller95def091999-11-25 00:26:21 +1100906 const char *remote_ip;
907 int remote_port;
Damien Miller95def091999-11-25 00:26:21 +1100908 FILE *f;
Damien Miller34132e52000-01-14 15:45:46 +1100909 struct addrinfo *ai;
910 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerb9997192003-12-17 16:29:22 +1100911 char *line;
Damien Miller34132e52000-01-14 15:45:46 +1100912 int listen_sock, maxfd;
Darren Tucker0f56ed12004-08-29 16:38:41 +1000913 int startup_p[2] = { -1 , -1 }, config_s[2] = { -1 , -1 };
Damien Miller37023962000-07-11 17:31:38 +1000914 int startups = 0;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000915 Key *key;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000916 Authctxt *authctxt;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000917 int ret, key_used = 0;
Darren Tucker645ab752004-06-25 13:33:20 +1000918 Buffer cfg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000919
Kevin Steves0ea1d9d2002-04-25 18:17:04 +0000920#ifdef HAVE_SECUREWARE
921 (void)set_auth_parameters(ac, av);
922#endif
Damien Miller59d3d5b2003-08-22 09:34:41 +1000923 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000924 init_rng();
925
Damien Millera8ed44b2003-01-10 09:53:12 +1100926 /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
Damien Millerb8c656e2000-06-28 15:22:41 +1000927 saved_argc = ac;
Darren Tucker17c5d032004-06-25 14:22:23 +1000928 rexec_argc = ac;
Darren Tuckerd8093e42006-05-04 16:24:34 +1000929 saved_argv = xcalloc(ac + 1, sizeof(*saved_argv));
Damien Millera8ed44b2003-01-10 09:53:12 +1100930 for (i = 0; i < ac; i++)
931 saved_argv[i] = xstrdup(av[i]);
Damien Miller04cb5362003-05-15 21:29:10 +1000932 saved_argv[i] = NULL;
Damien Millera8ed44b2003-01-10 09:53:12 +1100933
934#ifndef HAVE_SETPROCTITLE
935 /* Prepare for later setproctitle emulation */
936 compat_init_setproctitle(ac, av);
Damien Millerf2e3e9d2003-06-02 12:15:54 +1000937 av = saved_argv;
Damien Millera8ed44b2003-01-10 09:53:12 +1100938#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939
Damien Millerbfba3542004-03-22 09:29:57 +1100940 if (geteuid() == 0 && setgroups(0, NULL) == -1)
941 debug("setgroups(): %.200s", strerror(errno));
942
Darren Tuckerce321d82005-10-03 18:11:24 +1000943 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
944 sanitise_stdfd();
945
Damien Miller95def091999-11-25 00:26:21 +1100946 /* Initialize configuration options to their default values. */
947 initialize_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948
Damien Miller95def091999-11-25 00:26:21 +1100949 /* Parse command-line arguments. */
Darren Tucker645ab752004-06-25 13:33:20 +1000950 while ((opt = getopt(ac, av, "f:p:b:k:h:g:u:o:dDeiqrtQR46")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100951 switch (opt) {
Damien Miller34132e52000-01-14 15:45:46 +1100952 case '4':
Darren Tucker0f383232005-01-20 10:57:56 +1100953 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100954 break;
955 case '6':
Darren Tucker0f383232005-01-20 10:57:56 +1100956 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100957 break;
Damien Miller95def091999-11-25 00:26:21 +1100958 case 'f':
959 config_file_name = optarg;
960 break;
961 case 'd':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000962 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100963 debug_flag = 1;
964 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000965 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
Damien Millere4340be2000-09-16 13:29:08 +1100966 options.log_level++;
Damien Miller95def091999-11-25 00:26:21 +1100967 break;
Ben Lindstromc72745a2000-12-02 19:03:54 +0000968 case 'D':
969 no_daemon_flag = 1;
970 break;
Ben Lindstrom9fce9f02001-04-11 23:10:09 +0000971 case 'e':
972 log_stderr = 1;
973 break;
Damien Miller95def091999-11-25 00:26:21 +1100974 case 'i':
975 inetd_flag = 1;
976 break;
Darren Tucker645ab752004-06-25 13:33:20 +1000977 case 'r':
978 rexec_flag = 0;
979 break;
980 case 'R':
981 rexeced_flag = 1;
982 inetd_flag = 1;
983 break;
Damien Miller95def091999-11-25 00:26:21 +1100984 case 'Q':
Ben Lindstromd5390202001-01-29 08:07:43 +0000985 /* ignored */
Damien Miller95def091999-11-25 00:26:21 +1100986 break;
987 case 'q':
988 options.log_level = SYSLOG_LEVEL_QUIET;
989 break;
990 case 'b':
Damien Millerf0b15df2006-03-26 13:59:20 +1100991 options.server_key_bits = (int)strtonum(optarg, 256,
992 32768, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100993 break;
994 case 'p':
Damien Miller34132e52000-01-14 15:45:46 +1100995 options.ports_from_cmdline = 1;
Damien Millere4340be2000-09-16 13:29:08 +1100996 if (options.num_ports >= MAX_PORTS) {
997 fprintf(stderr, "too many ports.\n");
998 exit(1);
999 }
Ben Lindstrom19066a12001-04-12 23:39:26 +00001000 options.ports[options.num_ports++] = a2port(optarg);
1001 if (options.ports[options.num_ports-1] == 0) {
1002 fprintf(stderr, "Bad port number.\n");
1003 exit(1);
1004 }
Damien Miller95def091999-11-25 00:26:21 +11001005 break;
1006 case 'g':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +00001007 if ((options.login_grace_time = convtime(optarg)) == -1) {
1008 fprintf(stderr, "Invalid login grace time.\n");
1009 exit(1);
1010 }
Damien Miller95def091999-11-25 00:26:21 +11001011 break;
1012 case 'k':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +00001013 if ((options.key_regeneration_time = convtime(optarg)) == -1) {
1014 fprintf(stderr, "Invalid key regeneration interval.\n");
1015 exit(1);
1016 }
Damien Miller95def091999-11-25 00:26:21 +11001017 break;
1018 case 'h':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001019 if (options.num_host_key_files >= MAX_HOSTKEYS) {
1020 fprintf(stderr, "too many host keys.\n");
1021 exit(1);
1022 }
1023 options.host_key_files[options.num_host_key_files++] = optarg;
Damien Miller95def091999-11-25 00:26:21 +11001024 break;
Ben Lindstrom794325a2001-08-06 21:09:07 +00001025 case 't':
1026 test_flag = 1;
1027 break;
Damien Miller942da032000-08-18 13:59:06 +10001028 case 'u':
Damien Millerf0b15df2006-03-26 13:59:20 +11001029 utmp_len = (u_int)strtonum(optarg, 0, MAXHOSTNAMELEN+1, NULL);
Ben Lindstrom41daec72002-07-23 21:15:13 +00001030 if (utmp_len > MAXHOSTNAMELEN) {
1031 fprintf(stderr, "Invalid utmp length.\n");
1032 exit(1);
1033 }
Damien Miller942da032000-08-18 13:59:06 +10001034 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001035 case 'o':
Damien Millerb9997192003-12-17 16:29:22 +11001036 line = xstrdup(optarg);
1037 if (process_server_config_line(&options, line,
Ben Lindstromade03f62001-12-06 18:22:17 +00001038 "command-line", 0) != 0)
Damien Miller9f0f5c62001-12-21 14:45:46 +11001039 exit(1);
Damien Millerb9997192003-12-17 16:29:22 +11001040 xfree(line);
Ben Lindstromade03f62001-12-06 18:22:17 +00001041 break;
Damien Miller95def091999-11-25 00:26:21 +11001042 case '?':
1043 default:
Ben Lindstromade03f62001-12-06 18:22:17 +00001044 usage();
1045 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001046 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047 }
Darren Tucker645ab752004-06-25 13:33:20 +10001048 if (rexeced_flag || inetd_flag)
1049 rexec_flag = 0;
1050 if (rexec_flag && (av[0] == NULL || *av[0] != '/'))
1051 fatal("sshd re-exec requires execution with an absolute path");
1052 if (rexeced_flag)
Damien Miller035a5b42004-06-26 08:16:31 +10001053 closefrom(REEXEC_MIN_FREE_FD);
1054 else
1055 closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001056
Ben Lindstrom425fb022001-03-29 00:31:20 +00001057 SSLeay_add_all_algorithms();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058
Damien Miller34132e52000-01-14 15:45:46 +11001059 /*
1060 * Force logging to stderr until we have loaded the private host
1061 * key (unless started from inetd)
1062 */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001063 log_init(__progname,
Damien Miller5aa5d782002-02-08 22:01:54 +11001064 options.log_level == SYSLOG_LEVEL_NOT_SET ?
1065 SYSLOG_LEVEL_INFO : options.log_level,
1066 options.log_facility == SYSLOG_FACILITY_NOT_SET ?
1067 SYSLOG_FACILITY_AUTH : options.log_facility,
Ben Lindstromc2faa4a2002-11-09 15:50:03 +00001068 log_stderr || !inetd_flag);
Damien Miller34132e52000-01-14 15:45:46 +11001069
Darren Tucker86c093d2004-03-08 22:59:03 +11001070 /*
1071 * Unset KRB5CCNAME, otherwise the user's session may inherit it from
1072 * root's environment
Damien Miller94cf4c82005-07-17 17:04:47 +10001073 */
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001074 if (getenv("KRB5CCNAME") != NULL)
1075 unsetenv("KRB5CCNAME");
1076
Tim Rice81ed5182002-09-25 17:38:46 -07001077#ifdef _UNICOS
Darren Tucker12984962004-05-24 13:37:13 +10001078 /* Cray can define user privs drop all privs now!
Ben Lindstrom6db66ff2001-08-06 23:29:16 +00001079 * Not needed on PRIV_SU systems!
1080 */
1081 drop_cray_privs();
1082#endif
1083
Darren Tucker645ab752004-06-25 13:33:20 +10001084 sensitive_data.server_key = NULL;
1085 sensitive_data.ssh1_host_key = NULL;
1086 sensitive_data.have_ssh1_key = 0;
1087 sensitive_data.have_ssh2_key = 0;
1088
1089 /* Fetch our configuration */
1090 buffer_init(&cfg);
1091 if (rexeced_flag)
Damien Miller035a5b42004-06-26 08:16:31 +10001092 recv_rexec_state(REEXEC_CONFIG_PASS_FD, &cfg);
Darren Tucker645ab752004-06-25 13:33:20 +10001093 else
1094 load_server_config(config_file_name, &cfg);
1095
1096 parse_server_config(&options,
1097 rexeced_flag ? "rexec" : config_file_name, &cfg);
1098
1099 if (!rexec_flag)
1100 buffer_free(&cfg);
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}