blob: ac62cb5068995ce31720e6d1093fafdcf2269e01 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 * This program is the ssh daemon. It listens for connections from clients,
6 * and performs authentication, executes use commands or shell, and forwards
Damien Miller95def091999-11-25 00:26:21 +11007 * information to/from the application to the user client over an encrypted
Damien Millere4340be2000-09-16 13:29:08 +11008 * connection. This can also handle forwarding of X11, TCP/IP, and
9 * authentication agent connections.
Damien Millerefb4afe2000-04-12 18:45:05 +100010 *
Damien Millere4340be2000-09-16 13:29:08 +110011 * As far as I am concerned, the code I have written for this software
12 * can be used freely for any purpose. Any derived versions of this
13 * software must be clearly marked as such, and if the derived work is
14 * incompatible with the protocol description in the RFC file, it must be
15 * called by a name other than "ssh" or "Secure Shell".
16 *
17 * SSH2 implementation:
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000018 * Privilege Separation:
Damien Millere4340be2000-09-16 13:29:08 +110019 *
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000020 * Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved.
21 * Copyright (c) 2002 Niels Provos. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110022 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
33 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
36 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110042 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
44#include "includes.h"
Darren Tuckerfc959702004-07-17 16:12:08 +100045RCSID("$OpenBSD: sshd.c,v 1.298 2004/07/11 17:48:47 deraadt Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include <openssl/dh.h>
48#include <openssl/bn.h>
Damien Miller6a47f302002-02-13 13:55:06 +110049#include <openssl/md5.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000050#include <openssl/rand.h>
Kevin Steves0ea1d9d2002-04-25 18:17:04 +000051#ifdef HAVE_SECUREWARE
52#include <sys/security.h>
53#include <prot.h>
54#endif
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055
56#include "ssh.h"
57#include "ssh1.h"
58#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059#include "xmalloc.h"
60#include "rsa.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000061#include "sshpty.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064#include "servconf.h"
65#include "uidswap.h"
66#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100067#include "buffer.h"
Darren Tucker645ab752004-06-25 13:33:20 +100068#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000069#include "cipher.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100070#include "kex.h"
Damien Millerb38eff82000-04-01 11:09:21 +100071#include "key.h"
Damien Miller874d77b2000-10-14 16:23:11 +110072#include "dh.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100073#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100074#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000075#include "pathnames.h"
76#include "atomicio.h"
77#include "canohost.h"
78#include "auth.h"
79#include "misc.h"
Darren Tucker645ab752004-06-25 13:33:20 +100080#include "msg.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000081#include "dispatch.h"
Ben Lindstrom1bae4042001-10-03 17:46:39 +000082#include "channels.h"
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +000083#include "session.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000084#include "monitor_mm.h"
85#include "monitor.h"
86#include "monitor_wrap.h"
87#include "monitor_fdpass.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
89#ifdef LIBWRAP
90#include <tcpd.h>
91#include <syslog.h>
92int allow_severity = LOG_INFO;
93int deny_severity = LOG_WARNING;
94#endif /* LIBWRAP */
95
96#ifndef O_NOCTTY
97#define O_NOCTTY 0
98#endif
99
Damien Miller035a5b42004-06-26 08:16:31 +1000100/* Re-exec fds */
101#define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1)
102#define REEXEC_STARTUP_PIPE_FD (STDERR_FILENO + 2)
103#define REEXEC_CONFIG_PASS_FD (STDERR_FILENO + 3)
104#define REEXEC_MIN_FREE_FD (STDERR_FILENO + 4)
105
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000106extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000107
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108/* Server configuration options. */
109ServerOptions options;
110
111/* Name of the server configuration file. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000112char *config_file_name = _PATH_SERVER_CONFIG_FILE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
Damien Miller4af51302000-04-16 11:18:38 +1000114/*
Damien Miller34132e52000-01-14 15:45:46 +1100115 * Flag indicating whether IPv4 or IPv6. This can be set on the command line.
116 * Default value is AF_UNSPEC means both IPv4 and IPv6.
117 */
118int IPv4or6 = AF_UNSPEC;
119
Damien Miller95def091999-11-25 00:26:21 +1100120/*
121 * Debug mode flag. This can be set on the command line. If debug
122 * mode is enabled, extra debugging output will be sent to the system
123 * log, the daemon will not go to background, and will exit after processing
124 * the first connection.
125 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126int debug_flag = 0;
127
Ben Lindstrom794325a2001-08-06 21:09:07 +0000128/* Flag indicating that the daemon should only test the configuration and keys. */
129int test_flag = 0;
130
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131/* Flag indicating that the daemon is being started from inetd. */
132int inetd_flag = 0;
133
Ben Lindstromc72745a2000-12-02 19:03:54 +0000134/* Flag indicating that sshd should not detach and become a daemon. */
135int no_daemon_flag = 0;
136
Damien Miller5ce662a1999-11-11 17:57:39 +1100137/* debug goes to stderr unless inetd_flag is set */
138int log_stderr = 0;
139
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140/* Saved arguments to main(). */
141char **saved_argv;
Damien Millerb8c656e2000-06-28 15:22:41 +1000142int saved_argc;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Darren Tucker645ab752004-06-25 13:33:20 +1000144/* re-exec */
145int rexeced_flag = 0;
146int rexec_flag = 1;
147int rexec_argc = 0;
148char **rexec_argv;
149
Damien Miller5428f641999-11-25 11:54:57 +1100150/*
Damien Miller34132e52000-01-14 15:45:46 +1100151 * The sockets that the server is listening; this is used in the SIGHUP
152 * signal handler.
Damien Miller5428f641999-11-25 11:54:57 +1100153 */
Damien Miller34132e52000-01-14 15:45:46 +1100154#define MAX_LISTEN_SOCKS 16
155int listen_socks[MAX_LISTEN_SOCKS];
156int num_listen_socks = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157
Damien Miller5428f641999-11-25 11:54:57 +1100158/*
159 * the client's version string, passed by sshd2 in compat mode. if != NULL,
160 * sshd will skip the version-number exchange
161 */
Damien Miller95def091999-11-25 00:26:21 +1100162char *client_version_string = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000163char *server_version_string = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000165/* for rekeying XXX fixme */
166Kex *xxx_kex;
167
Damien Miller5428f641999-11-25 11:54:57 +1100168/*
169 * Any really sensitive data in the application is contained in this
170 * structure. The idea is that this structure could be locked into memory so
171 * that the pages do not get written into swap. However, there are some
172 * problems. The private key contains BIGNUMs, and we do not (in principle)
173 * have access to the internals of them, and locking just the structure is
174 * not very useful. Currently, memory locking is not implemented.
175 */
Damien Miller95def091999-11-25 00:26:21 +1100176struct {
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000177 Key *server_key; /* ephemeral server key */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100178 Key *ssh1_host_key; /* ssh1 host key */
179 Key **host_keys; /* all private host keys */
180 int have_ssh1_key;
181 int have_ssh2_key;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000182 u_char ssh1_cookie[SSH_SESSION_KEY_LENGTH];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183} sensitive_data;
184
Damien Miller5428f641999-11-25 11:54:57 +1100185/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000186 * Flag indicating whether the RSA server key needs to be regenerated.
187 * Is set in the SIGALRM handler and cleared when the key is regenerated.
Damien Miller5428f641999-11-25 11:54:57 +1100188 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000189static volatile sig_atomic_t key_do_regen = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000191/* This is set to true when a signal is received. */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000192static volatile sig_atomic_t received_sighup = 0;
193static volatile sig_atomic_t received_sigterm = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194
Damien Millerb38eff82000-04-01 11:09:21 +1000195/* session identifier, used by RSA-auth */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000196u_char session_id[16];
Damien Millerb38eff82000-04-01 11:09:21 +1000197
Damien Millereba71ba2000-04-29 23:57:08 +1000198/* same for ssh2 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000199u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000200u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000201
Damien Miller942da032000-08-18 13:59:06 +1000202/* record remote hostname or ip */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000203u_int utmp_len = MAXHOSTNAMELEN;
Damien Miller942da032000-08-18 13:59:06 +1000204
Ben Lindstromd84df982001-12-06 16:35:40 +0000205/* options.max_startup sized array of fd ints */
206int *startup_pipes = NULL;
207int startup_pipe; /* in child */
208
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000209/* variables used for privilege separation */
Damien Miller8e7fb332003-02-24 12:03:03 +1100210int use_privsep;
Darren Tuckera8be9e22004-02-06 16:40:27 +1100211struct monitor *pmonitor = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000212
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000213/* message to be displayed after login */
214Buffer loginmsg;
215
Darren Tucker3e33cec2003-10-02 16:12:36 +1000216/* global authentication context */
217Authctxt *the_authctxt = NULL;
218
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219/* Prototypes for various functions defined later in this file. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000220void destroy_sensitive_data(void);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000221void demote_sensitive_data(void);
Damien Miller98c7ad62000-03-09 21:27:49 +1100222
Ben Lindstrombba81212001-06-25 05:01:22 +0000223static void do_ssh1_kex(void);
224static void do_ssh2_kex(void);
Damien Miller874d77b2000-10-14 16:23:11 +1100225
Damien Miller98c7ad62000-03-09 21:27:49 +1100226/*
Damien Miller34132e52000-01-14 15:45:46 +1100227 * Close all listening sockets
228 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000229static void
Damien Miller34132e52000-01-14 15:45:46 +1100230close_listen_socks(void)
231{
232 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000233
Damien Miller34132e52000-01-14 15:45:46 +1100234 for (i = 0; i < num_listen_socks; i++)
235 close(listen_socks[i]);
236 num_listen_socks = -1;
237}
238
Ben Lindstromd84df982001-12-06 16:35:40 +0000239static void
240close_startup_pipes(void)
241{
242 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000243
Ben Lindstromd84df982001-12-06 16:35:40 +0000244 if (startup_pipes)
245 for (i = 0; i < options.max_startups; i++)
246 if (startup_pipes[i] != -1)
247 close(startup_pipes[i]);
248}
249
Damien Miller34132e52000-01-14 15:45:46 +1100250/*
Damien Miller95def091999-11-25 00:26:21 +1100251 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
252 * the effect is to reread the configuration file (and to regenerate
253 * the server key).
254 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000255static void
Damien Miller95def091999-11-25 00:26:21 +1100256sighup_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257{
Ben Lindstrom07958482001-12-06 16:19:01 +0000258 int save_errno = errno;
259
Damien Miller95def091999-11-25 00:26:21 +1100260 received_sighup = 1;
261 signal(SIGHUP, sighup_handler);
Ben Lindstrom07958482001-12-06 16:19:01 +0000262 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263}
264
Damien Miller95def091999-11-25 00:26:21 +1100265/*
266 * Called from the main program after receiving SIGHUP.
267 * Restarts the server.
268 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000269static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000270sighup_restart(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271{
Damien Miller996acd22003-04-09 20:59:48 +1000272 logit("Received SIGHUP; restarting.");
Damien Miller34132e52000-01-14 15:45:46 +1100273 close_listen_socks();
Ben Lindstromd84df982001-12-06 16:35:40 +0000274 close_startup_pipes();
Damien Miller95def091999-11-25 00:26:21 +1100275 execv(saved_argv[0], saved_argv);
Damien Miller996acd22003-04-09 20:59:48 +1000276 logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
Ben Lindstrom822b6342002-06-23 21:38:49 +0000277 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100278 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279}
280
Damien Miller95def091999-11-25 00:26:21 +1100281/*
282 * Generic signal handler for terminating signals in the master daemon.
Damien Miller95def091999-11-25 00:26:21 +1100283 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000284static void
Damien Miller95def091999-11-25 00:26:21 +1100285sigterm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000287 received_sigterm = sig;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288}
289
Damien Miller95def091999-11-25 00:26:21 +1100290/*
291 * SIGCHLD handler. This is called whenever a child dies. This will then
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000292 * reap any zombies left by exited children.
Damien Miller95def091999-11-25 00:26:21 +1100293 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000294static void
Damien Miller95def091999-11-25 00:26:21 +1100295main_sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296{
Damien Miller95def091999-11-25 00:26:21 +1100297 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000298 pid_t pid;
Damien Miller95def091999-11-25 00:26:21 +1100299 int status;
Damien Miller431f66b1999-11-21 18:31:57 +1100300
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000301 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
302 (pid < 0 && errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100303 ;
Damien Miller431f66b1999-11-21 18:31:57 +1100304
Damien Miller95def091999-11-25 00:26:21 +1100305 signal(SIGCHLD, main_sigchld_handler);
306 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307}
308
Damien Miller95def091999-11-25 00:26:21 +1100309/*
310 * Signal handler for the alarm after the login grace period has expired.
311 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000312static void
Damien Miller95def091999-11-25 00:26:21 +1100313grace_alarm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000315 /* XXX no idea how fix this signal handler */
316
Darren Tuckera8be9e22004-02-06 16:40:27 +1100317 if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0)
318 kill(pmonitor->m_pid, SIGALRM);
319
Damien Miller95def091999-11-25 00:26:21 +1100320 /* Log error and exit. */
Damien Millerd27a76d2002-09-27 13:22:31 +1000321 fatal("Timeout before authentication for %s", get_remote_ipaddr());
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322}
323
Damien Miller95def091999-11-25 00:26:21 +1100324/*
Damien Miller95def091999-11-25 00:26:21 +1100325 * Signal handler for the key regeneration alarm. Note that this
326 * alarm only occurs in the daemon waiting for connections, and it does not
327 * do anything with the private key or random state before forking.
328 * Thus there should be no concurrency control/asynchronous execution
329 * problems.
330 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000331static void
Ben Lindstromca42d5f2001-03-09 18:25:32 +0000332generate_ephemeral_server_key(void)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100333{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000334 u_int32_t rnd = 0;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000335 int i;
336
Ben Lindstroma3700052001-04-05 23:26:32 +0000337 verbose("Generating %s%d bit RSA key.",
Damien Millerff75ac42001-03-30 10:50:32 +1000338 sensitive_data.server_key ? "new " : "", options.server_key_bits);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100339 if (sensitive_data.server_key != NULL)
340 key_free(sensitive_data.server_key);
Ben Lindstroma3700052001-04-05 23:26:32 +0000341 sensitive_data.server_key = key_generate(KEY_RSA1,
Damien Millerff75ac42001-03-30 10:50:32 +1000342 options.server_key_bits);
343 verbose("RSA key generation complete.");
Ben Lindstromeb648a72001-03-05 06:00:29 +0000344
345 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
346 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000347 rnd = arc4random();
348 sensitive_data.ssh1_cookie[i] = rnd & 0xff;
349 rnd >>= 8;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000350 }
351 arc4random_stir();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100352}
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000353
Ben Lindstrombba81212001-06-25 05:01:22 +0000354static void
Damien Miller95def091999-11-25 00:26:21 +1100355key_regeneration_alarm(int sig)
356{
357 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000358
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000359 signal(SIGALRM, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +1100360 errno = save_errno;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000361 key_do_regen = 1;
Damien Miller95def091999-11-25 00:26:21 +1100362}
363
Ben Lindstrombba81212001-06-25 05:01:22 +0000364static void
Damien Millerb38eff82000-04-01 11:09:21 +1000365sshd_exchange_identification(int sock_in, int sock_out)
366{
Damien Miller78928792000-04-12 20:17:38 +1000367 int i, mismatch;
Damien Millerb38eff82000-04-01 11:09:21 +1000368 int remote_major, remote_minor;
Damien Miller78928792000-04-12 20:17:38 +1000369 int major, minor;
Damien Millerb38eff82000-04-01 11:09:21 +1000370 char *s;
371 char buf[256]; /* Must not be larger than remote_version. */
372 char remote_version[256]; /* Must be at least as big as buf. */
373
Damien Miller78928792000-04-12 20:17:38 +1000374 if ((options.protocol & SSH_PROTO_1) &&
375 (options.protocol & SSH_PROTO_2)) {
376 major = PROTOCOL_MAJOR_1;
377 minor = 99;
378 } else if (options.protocol & SSH_PROTO_2) {
379 major = PROTOCOL_MAJOR_2;
380 minor = PROTOCOL_MINOR_2;
381 } else {
382 major = PROTOCOL_MAJOR_1;
383 minor = PROTOCOL_MINOR_1;
384 }
385 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
Damien Millerb38eff82000-04-01 11:09:21 +1000386 server_version_string = xstrdup(buf);
387
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000388 /* Send our protocol version identification. */
389 if (atomicio(vwrite, sock_out, server_version_string,
390 strlen(server_version_string))
391 != strlen(server_version_string)) {
392 logit("Could not write ident string to %s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000393 cleanup_exit(255);
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000394 }
395
396 /* Read other sides version identification. */
397 memset(buf, 0, sizeof(buf));
398 for (i = 0; i < sizeof(buf) - 1; i++) {
399 if (atomicio(read, sock_in, &buf[i], 1) != 1) {
400 logit("Did not receive identification string from %s",
401 get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000402 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000403 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000404 if (buf[i] == '\r') {
405 buf[i] = 0;
406 /* Kludge for F-Secure Macintosh < 1.0.2 */
407 if (i == 12 &&
408 strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000409 break;
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000410 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000411 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000412 if (buf[i] == '\n') {
413 buf[i] = 0;
414 break;
415 }
Damien Millerb38eff82000-04-01 11:09:21 +1000416 }
Darren Tuckerfe0078a2003-07-19 19:52:28 +1000417 buf[sizeof(buf) - 1] = 0;
418 client_version_string = xstrdup(buf);
Damien Millerb38eff82000-04-01 11:09:21 +1000419
420 /*
421 * Check that the versions match. In future this might accept
422 * several versions and set appropriate flags to handle them.
423 */
424 if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
425 &remote_major, &remote_minor, remote_version) != 3) {
Damien Miller4af51302000-04-16 11:18:38 +1000426 s = "Protocol mismatch.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000427 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Millerb38eff82000-04-01 11:09:21 +1000428 close(sock_in);
429 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000430 logit("Bad protocol version identification '%.100s' from %s",
Damien Millerb38eff82000-04-01 11:09:21 +1000431 client_version_string, get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000432 cleanup_exit(255);
Damien Millerb38eff82000-04-01 11:09:21 +1000433 }
434 debug("Client protocol version %d.%d; client software version %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100435 remote_major, remote_minor, remote_version);
Damien Millerb38eff82000-04-01 11:09:21 +1000436
Damien Millerefb4afe2000-04-12 18:45:05 +1000437 compat_datafellows(remote_version);
438
Damien Millere9264972002-09-30 11:59:21 +1000439 if (datafellows & SSH_BUG_PROBE) {
Damien Miller996acd22003-04-09 20:59:48 +1000440 logit("probed from %s with %s. Don't panic.",
Damien Millere9264972002-09-30 11:59:21 +1000441 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000442 cleanup_exit(255);
Damien Millere9264972002-09-30 11:59:21 +1000443 }
444
Damien Miller27dbe6f2001-03-19 22:36:20 +1100445 if (datafellows & SSH_BUG_SCANNER) {
Damien Miller996acd22003-04-09 20:59:48 +1000446 logit("scanned from %s with %s. Don't panic.",
Damien Miller27dbe6f2001-03-19 22:36:20 +1100447 get_remote_ipaddr(), client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000448 cleanup_exit(255);
Damien Miller27dbe6f2001-03-19 22:36:20 +1100449 }
450
Damien Miller78928792000-04-12 20:17:38 +1000451 mismatch = 0;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000452 switch (remote_major) {
Damien Millerb38eff82000-04-01 11:09:21 +1000453 case 1:
Damien Millereba71ba2000-04-29 23:57:08 +1000454 if (remote_minor == 99) {
455 if (options.protocol & SSH_PROTO_2)
456 enable_compat20();
457 else
458 mismatch = 1;
459 break;
460 }
Damien Miller78928792000-04-12 20:17:38 +1000461 if (!(options.protocol & SSH_PROTO_1)) {
462 mismatch = 1;
463 break;
464 }
Damien Millerb38eff82000-04-01 11:09:21 +1000465 if (remote_minor < 3) {
Damien Miller37023962000-07-11 17:31:38 +1000466 packet_disconnect("Your ssh version is too old and "
Damien Millerb38eff82000-04-01 11:09:21 +1000467 "is no longer supported. Please install a newer version.");
468 } else if (remote_minor == 3) {
469 /* note that this disables agent-forwarding */
470 enable_compat13();
471 }
Damien Miller78928792000-04-12 20:17:38 +1000472 break;
Damien Millerefb4afe2000-04-12 18:45:05 +1000473 case 2:
Damien Miller78928792000-04-12 20:17:38 +1000474 if (options.protocol & SSH_PROTO_2) {
Damien Millerefb4afe2000-04-12 18:45:05 +1000475 enable_compat20();
476 break;
477 }
478 /* FALLTHROUGH */
Damien Miller4af51302000-04-16 11:18:38 +1000479 default:
Damien Miller78928792000-04-12 20:17:38 +1000480 mismatch = 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000481 break;
482 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000483 chop(server_version_string);
Damien Miller78928792000-04-12 20:17:38 +1000484 debug("Local version string %.200s", server_version_string);
485
486 if (mismatch) {
487 s = "Protocol major versions differ.\n";
Darren Tucker9f63f222003-07-03 13:46:56 +1000488 (void) atomicio(vwrite, sock_out, s, strlen(s));
Damien Miller78928792000-04-12 20:17:38 +1000489 close(sock_in);
490 close(sock_out);
Damien Miller996acd22003-04-09 20:59:48 +1000491 logit("Protocol major versions differ for %s: %.200s vs. %.200s",
Damien Miller78928792000-04-12 20:17:38 +1000492 get_remote_ipaddr(),
493 server_version_string, client_version_string);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000494 cleanup_exit(255);
Damien Miller78928792000-04-12 20:17:38 +1000495 }
Damien Millereba71ba2000-04-29 23:57:08 +1000496}
497
Damien Miller0bc1bd82000-11-13 22:57:25 +1100498/* Destroy the host and server keys. They will no longer be needed. */
Damien Millereba71ba2000-04-29 23:57:08 +1000499void
500destroy_sensitive_data(void)
501{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100502 int i;
503
504 if (sensitive_data.server_key) {
505 key_free(sensitive_data.server_key);
506 sensitive_data.server_key = NULL;
507 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100508 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100509 if (sensitive_data.host_keys[i]) {
510 key_free(sensitive_data.host_keys[i]);
511 sensitive_data.host_keys[i] = NULL;
512 }
513 }
514 sensitive_data.ssh1_host_key = NULL;
Ben Lindstromeb648a72001-03-05 06:00:29 +0000515 memset(sensitive_data.ssh1_cookie, 0, SSH_SESSION_KEY_LENGTH);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100516}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100517
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000518/* Demote private to public keys for network child */
519void
520demote_sensitive_data(void)
521{
522 Key *tmp;
523 int i;
524
525 if (sensitive_data.server_key) {
526 tmp = key_demote(sensitive_data.server_key);
527 key_free(sensitive_data.server_key);
528 sensitive_data.server_key = tmp;
529 }
530
531 for (i = 0; i < options.num_host_key_files; i++) {
532 if (sensitive_data.host_keys[i]) {
533 tmp = key_demote(sensitive_data.host_keys[i]);
534 key_free(sensitive_data.host_keys[i]);
535 sensitive_data.host_keys[i] = tmp;
536 if (tmp->type == KEY_RSA1)
537 sensitive_data.ssh1_host_key = tmp;
538 }
539 }
540
541 /* We do not clear ssh1_host key and cookie. XXX - Okay Niels? */
542}
543
Ben Lindstrom08105192002-03-22 02:50:06 +0000544static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000545privsep_preauth_child(void)
546{
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000547 u_int32_t rnd[256];
Ben Lindstrom810af962002-07-04 00:11:40 +0000548 gid_t gidset[1];
Ben Lindstromc7431342002-03-22 03:11:49 +0000549 struct passwd *pw;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000550 int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000551
552 /* Enable challenge-response authentication for privilege separation */
553 privsep_challenge_enable();
554
555 for (i = 0; i < 256; i++)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000556 rnd[i] = arc4random();
557 RAND_seed(rnd, sizeof(rnd));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000558
559 /* Demote the private keys to public keys. */
560 demote_sensitive_data();
561
Ben Lindstromc7431342002-03-22 03:11:49 +0000562 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
Damien Miller0150c652002-04-24 09:49:09 +1000563 fatal("Privilege separation user %s does not exist",
564 SSH_PRIVSEP_USER);
Ben Lindstromc7431342002-03-22 03:11:49 +0000565 memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
566 endpwent();
567
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000568 /* Change our root directory */
Ben Lindstrom7a7edf72002-03-22 02:42:37 +0000569 if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
570 fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
571 strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000572 if (chdir("/") == -1)
Ben Lindstrom1ee9ec32002-03-22 03:14:45 +0000573 fatal("chdir(\"/\"): %s", strerror(errno));
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000574
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000575 /* Drop our privileges */
Ben Lindstromc7431342002-03-22 03:11:49 +0000576 debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
577 (u_int)pw->pw_gid);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000578#if 0
Darren Tuckerd5920482004-02-29 20:11:30 +1100579 /* XXX not ready, too heavy after chroot */
Ben Lindstromc7431342002-03-22 03:11:49 +0000580 do_setusercontext(pw);
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000581#else
582 gidset[0] = pw->pw_gid;
Ben Lindstromfbcc3f72002-06-25 23:24:18 +0000583 if (setgroups(1, gidset) < 0)
584 fatal("setgroups: %.100s", strerror(errno));
585 permanently_set_uid(pw);
586#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587}
588
Darren Tucker3e33cec2003-10-02 16:12:36 +1000589static int
590privsep_preauth(Authctxt *authctxt)
Ben Lindstrom943481c2002-03-22 03:43:46 +0000591{
Ben Lindstrom943481c2002-03-22 03:43:46 +0000592 int status;
593 pid_t pid;
594
595 /* Set up unprivileged child process to deal with network data */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000596 pmonitor = monitor_init();
Ben Lindstrom943481c2002-03-22 03:43:46 +0000597 /* Store a pointer to the kex for later rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000598 pmonitor->m_pkex = &xxx_kex;
Ben Lindstrom943481c2002-03-22 03:43:46 +0000599
600 pid = fork();
601 if (pid == -1) {
602 fatal("fork of unprivileged child failed");
603 } else if (pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000604 debug2("Network child is on pid %ld", (long)pid);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000605
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000606 close(pmonitor->m_recvfd);
Darren Tuckera8be9e22004-02-06 16:40:27 +1100607 pmonitor->m_pid = pid;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000608 monitor_child_preauth(authctxt, pmonitor);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000609 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000610
611 /* Sync memory */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000612 monitor_sync(pmonitor);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000613
614 /* Wait for the child's exit status */
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000615 while (waitpid(pid, &status, 0) < 0)
616 if (errno != EINTR)
617 break;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000618 return (1);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000619 } else {
620 /* child */
621
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000622 close(pmonitor->m_sendfd);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000623
624 /* Demote the child */
625 if (getuid() == 0 || geteuid() == 0)
626 privsep_preauth_child();
Ben Lindstromf90f58d2002-03-26 01:53:03 +0000627 setproctitle("%s", "[net]");
Ben Lindstrom943481c2002-03-22 03:43:46 +0000628 }
Darren Tucker3e33cec2003-10-02 16:12:36 +1000629 return (0);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000630}
631
Ben Lindstrom08105192002-03-22 02:50:06 +0000632static void
Ben Lindstrom943481c2002-03-22 03:43:46 +0000633privsep_postauth(Authctxt *authctxt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000634{
Tim Rice9dd30812002-07-07 13:43:36 -0700635#ifdef DISABLE_FD_PASSING
Tim Rice8eff3192002-06-25 15:35:15 -0700636 if (1) {
637#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000638 if (authctxt->pw->pw_uid == 0 || options.use_login) {
Tim Rice8eff3192002-06-25 15:35:15 -0700639#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000640 /* File descriptor passing is broken or root login */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000641 monitor_apply_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000642 use_privsep = 0;
643 return;
644 }
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000645
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000646 /* Authentication complete */
647 alarm(0);
648 if (startup_pipe != -1) {
649 close(startup_pipe);
650 startup_pipe = -1;
651 }
652
653 /* New socket pair */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000654 monitor_reinit(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000655
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000656 pmonitor->m_pid = fork();
657 if (pmonitor->m_pid == -1)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000658 fatal("fork of unprivileged child failed");
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000659 else if (pmonitor->m_pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000660 debug2("User child is on pid %ld", (long)pmonitor->m_pid);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000661 close(pmonitor->m_recvfd);
662 monitor_child_postauth(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663
664 /* NEVERREACHED */
665 exit(0);
666 }
667
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000668 close(pmonitor->m_sendfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669
670 /* Demote the private keys to public keys. */
671 demote_sensitive_data();
672
673 /* Drop privileges */
674 do_setusercontext(authctxt->pw);
675
676 /* It is safe now to apply the key state */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000677 monitor_apply_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000678}
679
Ben Lindstrombba81212001-06-25 05:01:22 +0000680static char *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100681list_hostkey_types(void)
682{
Damien Miller0e3b8722002-01-22 23:26:38 +1100683 Buffer b;
Damien Millerf58b58c2003-11-17 21:18:23 +1100684 const char *p;
685 char *ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100686 int i;
Damien Miller0e3b8722002-01-22 23:26:38 +1100687
688 buffer_init(&b);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100689 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100690 Key *key = sensitive_data.host_keys[i];
691 if (key == NULL)
692 continue;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000693 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100694 case KEY_RSA:
695 case KEY_DSA:
Damien Miller0e3b8722002-01-22 23:26:38 +1100696 if (buffer_len(&b) > 0)
697 buffer_append(&b, ",", 1);
698 p = key_ssh_name(key);
699 buffer_append(&b, p, strlen(p));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100700 break;
701 }
702 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100703 buffer_append(&b, "\0", 1);
Damien Millerf58b58c2003-11-17 21:18:23 +1100704 ret = xstrdup(buffer_ptr(&b));
Damien Miller0e3b8722002-01-22 23:26:38 +1100705 buffer_free(&b);
Damien Millerf58b58c2003-11-17 21:18:23 +1100706 debug("list_hostkey_types: %s", ret);
707 return ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100708}
709
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000710Key *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100711get_hostkey_by_type(int type)
712{
713 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000714
Damien Miller9f0f5c62001-12-21 14:45:46 +1100715 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100716 Key *key = sensitive_data.host_keys[i];
717 if (key != NULL && key->type == type)
718 return key;
719 }
720 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000721}
722
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000723Key *
724get_hostkey_by_index(int ind)
725{
726 if (ind < 0 || ind >= options.num_host_key_files)
727 return (NULL);
728 return (sensitive_data.host_keys[ind]);
729}
730
731int
732get_hostkey_index(Key *key)
733{
734 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000735
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000736 for (i = 0; i < options.num_host_key_files; i++) {
737 if (key == sensitive_data.host_keys[i])
738 return (i);
739 }
740 return (-1);
741}
742
Damien Miller942da032000-08-18 13:59:06 +1000743/*
744 * returns 1 if connection should be dropped, 0 otherwise.
745 * dropping starts at connection #max_startups_begin with a probability
746 * of (max_startups_rate/100). the probability increases linearly until
747 * all connections are dropped for startups > max_startups
748 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000749static int
Damien Miller942da032000-08-18 13:59:06 +1000750drop_connection(int startups)
751{
752 double p, r;
753
754 if (startups < options.max_startups_begin)
755 return 0;
756 if (startups >= options.max_startups)
757 return 1;
758 if (options.max_startups_rate == 100)
759 return 1;
760
761 p = 100 - options.max_startups_rate;
762 p *= startups - options.max_startups_begin;
763 p /= (double) (options.max_startups - options.max_startups_begin);
764 p += options.max_startups_rate;
765 p /= 100.0;
766 r = arc4random() / (double) UINT_MAX;
767
768 debug("drop_connection: p %g, r %g", p, r);
769 return (r < p) ? 1 : 0;
770}
771
Ben Lindstromade03f62001-12-06 18:22:17 +0000772static void
773usage(void)
774{
Damien Miller0c889cd2004-03-22 09:36:00 +1100775 fprintf(stderr, "%s, %s\n",
Darren Tucker4a250542003-10-03 17:57:24 +1000776 SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
Damien Millerb4087862004-03-22 09:35:21 +1100777 fprintf(stderr,
778"usage: sshd [-46Ddeiqt] [-b bits] [-f config_file] [-g login_grace_time]\n"
779" [-h host_key_file] [-k key_gen_time] [-o option] [-p port] [-u len]\n"
780 );
Ben Lindstromade03f62001-12-06 18:22:17 +0000781 exit(1);
782}
783
Darren Tucker645ab752004-06-25 13:33:20 +1000784static void
785send_rexec_state(int fd, Buffer *conf)
786{
787 Buffer m;
788
789 debug3("%s: entering fd = %d config len %d", __func__, fd,
790 buffer_len(conf));
791
792 /*
793 * Protocol from reexec master to child:
794 * string configuration
795 * u_int ephemeral_key_follows
796 * bignum e (only if ephemeral_key_follows == 1)
797 * bignum n "
798 * bignum d "
799 * bignum iqmp "
800 * bignum p "
801 * bignum q "
802 */
803 buffer_init(&m);
804 buffer_put_cstring(&m, buffer_ptr(conf));
805
Darren Tuckerfc959702004-07-17 16:12:08 +1000806 if (sensitive_data.server_key != NULL &&
Darren Tucker645ab752004-06-25 13:33:20 +1000807 sensitive_data.server_key->type == KEY_RSA1) {
808 buffer_put_int(&m, 1);
809 buffer_put_bignum(&m, sensitive_data.server_key->rsa->e);
810 buffer_put_bignum(&m, sensitive_data.server_key->rsa->n);
811 buffer_put_bignum(&m, sensitive_data.server_key->rsa->d);
812 buffer_put_bignum(&m, sensitive_data.server_key->rsa->iqmp);
813 buffer_put_bignum(&m, sensitive_data.server_key->rsa->p);
814 buffer_put_bignum(&m, sensitive_data.server_key->rsa->q);
815 } else
816 buffer_put_int(&m, 0);
817
818 if (ssh_msg_send(fd, 0, &m) == -1)
819 fatal("%s: ssh_msg_send failed", __func__);
820
821 buffer_free(&m);
822
823 debug3("%s: done", __func__);
824}
825
826static void
827recv_rexec_state(int fd, Buffer *conf)
828{
829 Buffer m;
830 char *cp;
831 u_int len;
832
833 debug3("%s: entering fd = %d", __func__, fd);
834
835 buffer_init(&m);
836
837 if (ssh_msg_recv(fd, &m) == -1)
838 fatal("%s: ssh_msg_recv failed", __func__);
839 if (buffer_get_char(&m) != 0)
840 fatal("%s: rexec version mismatch", __func__);
841
842 cp = buffer_get_string(&m, &len);
843 if (conf != NULL)
844 buffer_append(conf, cp, len + 1);
845 xfree(cp);
846
847 if (buffer_get_int(&m)) {
848 if (sensitive_data.server_key != NULL)
849 key_free(sensitive_data.server_key);
850 sensitive_data.server_key = key_new_private(KEY_RSA1);
851 buffer_get_bignum(&m, sensitive_data.server_key->rsa->e);
852 buffer_get_bignum(&m, sensitive_data.server_key->rsa->n);
853 buffer_get_bignum(&m, sensitive_data.server_key->rsa->d);
854 buffer_get_bignum(&m, sensitive_data.server_key->rsa->iqmp);
855 buffer_get_bignum(&m, sensitive_data.server_key->rsa->p);
856 buffer_get_bignum(&m, sensitive_data.server_key->rsa->q);
857 rsa_generate_additional_parameters(
858 sensitive_data.server_key->rsa);
859 }
860 buffer_free(&m);
861
862 debug3("%s: done", __func__);
863}
864
Damien Miller95def091999-11-25 00:26:21 +1100865/*
866 * Main program for the daemon.
867 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868int
869main(int ac, char **av)
870{
Damien Miller95def091999-11-25 00:26:21 +1100871 extern char *optarg;
872 extern int optind;
Damien Miller386c6a22004-06-30 22:40:20 +1000873 int opt, j, i, fdsetsz, on = 1;
874 int sock_in = -1, sock_out = -1, newsock = -1;
Damien Miller166fca82000-04-20 07:42:21 +1000875 pid_t pid;
Damien Miller34132e52000-01-14 15:45:46 +1100876 socklen_t fromlen;
Damien Miller34132e52000-01-14 15:45:46 +1100877 fd_set *fdset;
878 struct sockaddr_storage from;
Damien Miller95def091999-11-25 00:26:21 +1100879 const char *remote_ip;
880 int remote_port;
Damien Miller95def091999-11-25 00:26:21 +1100881 FILE *f;
Damien Miller34132e52000-01-14 15:45:46 +1100882 struct addrinfo *ai;
883 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerb9997192003-12-17 16:29:22 +1100884 char *line;
Damien Miller34132e52000-01-14 15:45:46 +1100885 int listen_sock, maxfd;
Darren Tucker645ab752004-06-25 13:33:20 +1000886 int startup_p[2], config_s[2];
Damien Miller37023962000-07-11 17:31:38 +1000887 int startups = 0;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000888 Key *key;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000889 Authctxt *authctxt;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000890 int ret, key_used = 0;
Darren Tucker645ab752004-06-25 13:33:20 +1000891 Buffer cfg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
Kevin Steves0ea1d9d2002-04-25 18:17:04 +0000893#ifdef HAVE_SECUREWARE
894 (void)set_auth_parameters(ac, av);
895#endif
Damien Miller59d3d5b2003-08-22 09:34:41 +1000896 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000897 init_rng();
898
Damien Millera8ed44b2003-01-10 09:53:12 +1100899 /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
Damien Millerb8c656e2000-06-28 15:22:41 +1000900 saved_argc = ac;
Darren Tucker17c5d032004-06-25 14:22:23 +1000901 rexec_argc = ac;
Damien Miller04cb5362003-05-15 21:29:10 +1000902 saved_argv = xmalloc(sizeof(*saved_argv) * (ac + 1));
Damien Millera8ed44b2003-01-10 09:53:12 +1100903 for (i = 0; i < ac; i++)
904 saved_argv[i] = xstrdup(av[i]);
Damien Miller04cb5362003-05-15 21:29:10 +1000905 saved_argv[i] = NULL;
Damien Millera8ed44b2003-01-10 09:53:12 +1100906
907#ifndef HAVE_SETPROCTITLE
908 /* Prepare for later setproctitle emulation */
909 compat_init_setproctitle(ac, av);
Damien Millerf2e3e9d2003-06-02 12:15:54 +1000910 av = saved_argv;
Damien Millera8ed44b2003-01-10 09:53:12 +1100911#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000912
Damien Millerbfba3542004-03-22 09:29:57 +1100913 if (geteuid() == 0 && setgroups(0, NULL) == -1)
914 debug("setgroups(): %.200s", strerror(errno));
915
Damien Miller95def091999-11-25 00:26:21 +1100916 /* Initialize configuration options to their default values. */
917 initialize_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918
Damien Miller95def091999-11-25 00:26:21 +1100919 /* Parse command-line arguments. */
Darren Tucker645ab752004-06-25 13:33:20 +1000920 while ((opt = getopt(ac, av, "f:p:b:k:h:g:u:o:dDeiqrtQR46")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100921 switch (opt) {
Damien Miller34132e52000-01-14 15:45:46 +1100922 case '4':
923 IPv4or6 = AF_INET;
924 break;
925 case '6':
926 IPv4or6 = AF_INET6;
927 break;
Damien Miller95def091999-11-25 00:26:21 +1100928 case 'f':
929 config_file_name = optarg;
930 break;
931 case 'd':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000932 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100933 debug_flag = 1;
934 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000935 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
Damien Millere4340be2000-09-16 13:29:08 +1100936 options.log_level++;
Damien Miller95def091999-11-25 00:26:21 +1100937 break;
Ben Lindstromc72745a2000-12-02 19:03:54 +0000938 case 'D':
939 no_daemon_flag = 1;
940 break;
Ben Lindstrom9fce9f02001-04-11 23:10:09 +0000941 case 'e':
942 log_stderr = 1;
943 break;
Damien Miller95def091999-11-25 00:26:21 +1100944 case 'i':
945 inetd_flag = 1;
946 break;
Darren Tucker645ab752004-06-25 13:33:20 +1000947 case 'r':
948 rexec_flag = 0;
949 break;
950 case 'R':
951 rexeced_flag = 1;
952 inetd_flag = 1;
953 break;
Damien Miller95def091999-11-25 00:26:21 +1100954 case 'Q':
Ben Lindstromd5390202001-01-29 08:07:43 +0000955 /* ignored */
Damien Miller95def091999-11-25 00:26:21 +1100956 break;
957 case 'q':
958 options.log_level = SYSLOG_LEVEL_QUIET;
959 break;
960 case 'b':
961 options.server_key_bits = atoi(optarg);
962 break;
963 case 'p':
Damien Miller34132e52000-01-14 15:45:46 +1100964 options.ports_from_cmdline = 1;
Damien Millere4340be2000-09-16 13:29:08 +1100965 if (options.num_ports >= MAX_PORTS) {
966 fprintf(stderr, "too many ports.\n");
967 exit(1);
968 }
Ben Lindstrom19066a12001-04-12 23:39:26 +0000969 options.ports[options.num_ports++] = a2port(optarg);
970 if (options.ports[options.num_ports-1] == 0) {
971 fprintf(stderr, "Bad port number.\n");
972 exit(1);
973 }
Damien Miller95def091999-11-25 00:26:21 +1100974 break;
975 case 'g':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000976 if ((options.login_grace_time = convtime(optarg)) == -1) {
977 fprintf(stderr, "Invalid login grace time.\n");
978 exit(1);
979 }
Damien Miller95def091999-11-25 00:26:21 +1100980 break;
981 case 'k':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000982 if ((options.key_regeneration_time = convtime(optarg)) == -1) {
983 fprintf(stderr, "Invalid key regeneration interval.\n");
984 exit(1);
985 }
Damien Miller95def091999-11-25 00:26:21 +1100986 break;
987 case 'h':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100988 if (options.num_host_key_files >= MAX_HOSTKEYS) {
989 fprintf(stderr, "too many host keys.\n");
990 exit(1);
991 }
992 options.host_key_files[options.num_host_key_files++] = optarg;
Damien Miller95def091999-11-25 00:26:21 +1100993 break;
Ben Lindstrom794325a2001-08-06 21:09:07 +0000994 case 't':
995 test_flag = 1;
996 break;
Damien Miller942da032000-08-18 13:59:06 +1000997 case 'u':
998 utmp_len = atoi(optarg);
Ben Lindstrom41daec72002-07-23 21:15:13 +0000999 if (utmp_len > MAXHOSTNAMELEN) {
1000 fprintf(stderr, "Invalid utmp length.\n");
1001 exit(1);
1002 }
Damien Miller942da032000-08-18 13:59:06 +10001003 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001004 case 'o':
Damien Millerb9997192003-12-17 16:29:22 +11001005 line = xstrdup(optarg);
1006 if (process_server_config_line(&options, line,
Ben Lindstromade03f62001-12-06 18:22:17 +00001007 "command-line", 0) != 0)
Damien Miller9f0f5c62001-12-21 14:45:46 +11001008 exit(1);
Damien Millerb9997192003-12-17 16:29:22 +11001009 xfree(line);
Ben Lindstromade03f62001-12-06 18:22:17 +00001010 break;
Damien Miller95def091999-11-25 00:26:21 +11001011 case '?':
1012 default:
Ben Lindstromade03f62001-12-06 18:22:17 +00001013 usage();
1014 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001015 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016 }
Darren Tucker645ab752004-06-25 13:33:20 +10001017 if (rexeced_flag || inetd_flag)
1018 rexec_flag = 0;
1019 if (rexec_flag && (av[0] == NULL || *av[0] != '/'))
1020 fatal("sshd re-exec requires execution with an absolute path");
1021 if (rexeced_flag)
Damien Miller035a5b42004-06-26 08:16:31 +10001022 closefrom(REEXEC_MIN_FREE_FD);
1023 else
1024 closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001025
Ben Lindstrom425fb022001-03-29 00:31:20 +00001026 SSLeay_add_all_algorithms();
Ben Lindstrom908afed2001-10-03 17:34:59 +00001027 channel_set_af(IPv4or6);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001028
Damien Miller34132e52000-01-14 15:45:46 +11001029 /*
1030 * Force logging to stderr until we have loaded the private host
1031 * key (unless started from inetd)
1032 */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001033 log_init(__progname,
Damien Miller5aa5d782002-02-08 22:01:54 +11001034 options.log_level == SYSLOG_LEVEL_NOT_SET ?
1035 SYSLOG_LEVEL_INFO : options.log_level,
1036 options.log_facility == SYSLOG_FACILITY_NOT_SET ?
1037 SYSLOG_FACILITY_AUTH : options.log_facility,
Ben Lindstromc2faa4a2002-11-09 15:50:03 +00001038 log_stderr || !inetd_flag);
Damien Miller34132e52000-01-14 15:45:46 +11001039
Darren Tucker86c093d2004-03-08 22:59:03 +11001040#ifdef _AIX
1041 /*
1042 * Unset KRB5CCNAME, otherwise the user's session may inherit it from
1043 * root's environment
1044 */
1045 unsetenv("KRB5CCNAME");
1046#endif /* _AIX */
Tim Rice81ed5182002-09-25 17:38:46 -07001047#ifdef _UNICOS
Darren Tucker12984962004-05-24 13:37:13 +10001048 /* Cray can define user privs drop all privs now!
Ben Lindstrom6db66ff2001-08-06 23:29:16 +00001049 * Not needed on PRIV_SU systems!
1050 */
1051 drop_cray_privs();
1052#endif
1053
Damien Miller60bc5172001-03-19 09:38:15 +11001054 seed_rng();
1055
Darren Tucker645ab752004-06-25 13:33:20 +10001056 sensitive_data.server_key = NULL;
1057 sensitive_data.ssh1_host_key = NULL;
1058 sensitive_data.have_ssh1_key = 0;
1059 sensitive_data.have_ssh2_key = 0;
1060
1061 /* Fetch our configuration */
1062 buffer_init(&cfg);
1063 if (rexeced_flag)
Damien Miller035a5b42004-06-26 08:16:31 +10001064 recv_rexec_state(REEXEC_CONFIG_PASS_FD, &cfg);
Darren Tucker645ab752004-06-25 13:33:20 +10001065 else
1066 load_server_config(config_file_name, &cfg);
1067
1068 parse_server_config(&options,
1069 rexeced_flag ? "rexec" : config_file_name, &cfg);
1070
1071 if (!rexec_flag)
1072 buffer_free(&cfg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001073
Damien Miller95def091999-11-25 00:26:21 +11001074 /* Fill in default values for those options not explicitly set. */
1075 fill_default_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001076
Damien Miller95def091999-11-25 00:26:21 +11001077 /* Check that there are no remaining arguments. */
1078 if (optind < ac) {
1079 fprintf(stderr, "Extra argument %s.\n", av[optind]);
1080 exit(1);
1081 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001082
Damien Miller95def091999-11-25 00:26:21 +11001083 debug("sshd version %.100s", SSH_VERSION);
Damien Miller2ccf6611999-11-15 15:25:10 +11001084
Damien Miller0bc1bd82000-11-13 22:57:25 +11001085 /* load private host keys */
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001086 sensitive_data.host_keys = xmalloc(options.num_host_key_files *
1087 sizeof(Key *));
Damien Miller9f0f5c62001-12-21 14:45:46 +11001088 for (i = 0; i < options.num_host_key_files; i++)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001089 sensitive_data.host_keys[i] = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001090
Damien Miller9f0f5c62001-12-21 14:45:46 +11001091 for (i = 0; i < options.num_host_key_files; i++) {
Ben Lindstromd0fca422001-03-26 13:44:06 +00001092 key = key_load_private(options.host_key_files[i], "", NULL);
1093 sensitive_data.host_keys[i] = key;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001094 if (key == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001095 error("Could not load host key: %s",
1096 options.host_key_files[i]);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001097 sensitive_data.host_keys[i] = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001098 continue;
1099 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001100 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001101 case KEY_RSA1:
1102 sensitive_data.ssh1_host_key = key;
1103 sensitive_data.have_ssh1_key = 1;
1104 break;
1105 case KEY_RSA:
1106 case KEY_DSA:
1107 sensitive_data.have_ssh2_key = 1;
1108 break;
1109 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001110 debug("private host key: #%d type %d %s", i, key->type,
1111 key_type(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001112 }
1113 if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
Damien Miller996acd22003-04-09 20:59:48 +10001114 logit("Disabling protocol version 1. Could not load host key");
Damien Millereba71ba2000-04-29 23:57:08 +10001115 options.protocol &= ~SSH_PROTO_1;
1116 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001117 if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
Damien Miller996acd22003-04-09 20:59:48 +10001118 logit("Disabling protocol version 2. Could not load host key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001119 options.protocol &= ~SSH_PROTO_2;
Damien Millereba71ba2000-04-29 23:57:08 +10001120 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001121 if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
Damien Miller996acd22003-04-09 20:59:48 +10001122 logit("sshd: no hostkeys available -- exiting.");
Damien Miller95def091999-11-25 00:26:21 +11001123 exit(1);
1124 }
Damien Miller95def091999-11-25 00:26:21 +11001125
Damien Millereba71ba2000-04-29 23:57:08 +10001126 /* Check certain values for sanity. */
1127 if (options.protocol & SSH_PROTO_1) {
1128 if (options.server_key_bits < 512 ||
1129 options.server_key_bits > 32768) {
1130 fprintf(stderr, "Bad server key size.\n");
1131 exit(1);
1132 }
1133 /*
1134 * Check that server and host key lengths differ sufficiently. This
1135 * is necessary to make double encryption work with rsaref. Oh, I
1136 * hate software patents. I dont know if this can go? Niels
1137 */
1138 if (options.server_key_bits >
Ben Lindstrom822b6342002-06-23 21:38:49 +00001139 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) -
1140 SSH_KEY_BITS_RESERVED && options.server_key_bits <
1141 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1142 SSH_KEY_BITS_RESERVED) {
Damien Millereba71ba2000-04-29 23:57:08 +10001143 options.server_key_bits =
Ben Lindstrom822b6342002-06-23 21:38:49 +00001144 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1145 SSH_KEY_BITS_RESERVED;
Damien Millereba71ba2000-04-29 23:57:08 +10001146 debug("Forcing server key to %d bits to make it differ from host key.",
1147 options.server_key_bits);
1148 }
1149 }
1150
Ben Lindstroma26ea632002-06-06 20:46:25 +00001151 if (use_privsep) {
1152 struct passwd *pw;
1153 struct stat st;
1154
1155 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
1156 fatal("Privilege separation user %s does not exist",
1157 SSH_PRIVSEP_USER);
1158 if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
1159 (S_ISDIR(st.st_mode) == 0))
1160 fatal("Missing privilege separation directory: %s",
1161 _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstrom59627352002-06-27 18:02:21 +00001162
1163#ifdef HAVE_CYGWIN
1164 if (check_ntsec(_PATH_PRIVSEP_CHROOT_DIR) &&
1165 (st.st_uid != getuid () ||
1166 (st.st_mode & (S_IWGRP|S_IWOTH)) != 0))
1167#else
Ben Lindstrom2dfacb32002-06-23 00:33:47 +00001168 if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
Ben Lindstrom59627352002-06-27 18:02:21 +00001169#endif
Damien Miller180fc5b2003-02-24 11:50:18 +11001170 fatal("%s must be owned by root and not group or "
1171 "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstroma26ea632002-06-06 20:46:25 +00001172 }
1173
Ben Lindstrom794325a2001-08-06 21:09:07 +00001174 /* Configuration looks good, so exit if in test mode. */
1175 if (test_flag)
1176 exit(0);
1177
Damien Miller87aea252002-05-10 12:20:24 +10001178 /*
1179 * Clear out any supplemental groups we may have inherited. This
1180 * prevents inadvertent creation of files with bad modes (in the
Damien Millera8e06ce2003-11-21 23:48:55 +11001181 * portable version at least, it's certainly possible for PAM
1182 * to create a file, and we can't control the code in every
Damien Miller87aea252002-05-10 12:20:24 +10001183 * module which might be used).
1184 */
1185 if (setgroups(0, NULL) < 0)
1186 debug("setgroups() failed: %.200s", strerror(errno));
1187
Darren Tucker645ab752004-06-25 13:33:20 +10001188 if (rexec_flag) {
1189 rexec_argv = xmalloc(sizeof(char *) * (rexec_argc + 2));
1190 for (i = 0; i < rexec_argc; i++) {
1191 debug("rexec_argv[%d]='%s'", i, saved_argv[i]);
1192 rexec_argv[i] = saved_argv[i];
1193 }
1194 rexec_argv[rexec_argc] = "-R";
1195 rexec_argv[rexec_argc + 1] = NULL;
1196 }
1197
Damien Millereba71ba2000-04-29 23:57:08 +10001198 /* Initialize the log (it is reinitialized below in case we forked). */
Damien Miller95def091999-11-25 00:26:21 +11001199 if (debug_flag && !inetd_flag)
1200 log_stderr = 1;
Kevin Stevesec84dc12000-12-13 17:45:15 +00001201 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001202
Damien Millereba71ba2000-04-29 23:57:08 +10001203 /*
1204 * If not in debugging mode, and not started from inetd, disconnect
1205 * from the controlling terminal, and fork. The original process
1206 * exits.
1207 */
Ben Lindstromc72745a2000-12-02 19:03:54 +00001208 if (!(debug_flag || inetd_flag || no_daemon_flag)) {
Damien Miller95def091999-11-25 00:26:21 +11001209#ifdef TIOCNOTTY
1210 int fd;
1211#endif /* TIOCNOTTY */
1212 if (daemon(0, 0) < 0)
1213 fatal("daemon() failed: %.200s", strerror(errno));
1214
1215 /* Disconnect from the controlling tty. */
1216#ifdef TIOCNOTTY
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001217 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
Damien Miller95def091999-11-25 00:26:21 +11001218 if (fd >= 0) {
1219 (void) ioctl(fd, TIOCNOTTY, NULL);
1220 close(fd);
1221 }
1222#endif /* TIOCNOTTY */
1223 }
1224 /* Reinitialize the log (because of the fork above). */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001225 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001226
Damien Miller95def091999-11-25 00:26:21 +11001227 /* Initialize the random number generator. */
1228 arc4random_stir();
1229
1230 /* Chdir to the root directory so that the current disk can be
1231 unmounted if desired. */
1232 chdir("/");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001233
Ben Lindstromde71cda2001-03-24 00:43:26 +00001234 /* ignore SIGPIPE */
1235 signal(SIGPIPE, SIG_IGN);
Damien Miller95def091999-11-25 00:26:21 +11001236
Damien Miller95def091999-11-25 00:26:21 +11001237 /* Start listening for a socket, unless started from inetd. */
1238 if (inetd_flag) {
Darren Tucker645ab752004-06-25 13:33:20 +10001239 int fd;
1240
Damien Miller994cf142000-07-21 10:19:44 +10001241 startup_pipe = -1;
Darren Tucker645ab752004-06-25 13:33:20 +10001242 if (rexeced_flag) {
Damien Miller035a5b42004-06-26 08:16:31 +10001243 close(REEXEC_CONFIG_PASS_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001244 sock_in = sock_out = dup(STDIN_FILENO);
1245 if (!debug_flag) {
Damien Miller035a5b42004-06-26 08:16:31 +10001246 startup_pipe = dup(REEXEC_STARTUP_PIPE_FD);
1247 close(REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001248 }
1249 } else {
1250 sock_in = dup(STDIN_FILENO);
1251 sock_out = dup(STDOUT_FILENO);
1252 }
Damien Millereba71ba2000-04-29 23:57:08 +10001253 /*
1254 * We intentionally do not close the descriptors 0, 1, and 2
Darren Tucker645ab752004-06-25 13:33:20 +10001255 * as our code for setting the descriptors won't work if
Damien Millereba71ba2000-04-29 23:57:08 +10001256 * ttyfd happens to be one of those.
1257 */
Darren Tucker645ab752004-06-25 13:33:20 +10001258 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1259 dup2(fd, STDIN_FILENO);
1260 dup2(fd, STDOUT_FILENO);
1261 if (fd > STDOUT_FILENO)
1262 close(fd);
1263 }
Damien Miller95def091999-11-25 00:26:21 +11001264 debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
Darren Tucker645ab752004-06-25 13:33:20 +10001265 if ((options.protocol & SSH_PROTO_1) &&
1266 sensitive_data.server_key == NULL)
Ben Lindstromca42d5f2001-03-09 18:25:32 +00001267 generate_ephemeral_server_key();
Damien Miller95def091999-11-25 00:26:21 +11001268 } else {
Damien Miller34132e52000-01-14 15:45:46 +11001269 for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
1270 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1271 continue;
1272 if (num_listen_socks >= MAX_LISTEN_SOCKS)
1273 fatal("Too many listen sockets. "
1274 "Enlarge MAX_LISTEN_SOCKS");
1275 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
1276 ntop, sizeof(ntop), strport, sizeof(strport),
1277 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1278 error("getnameinfo failed");
1279 continue;
1280 }
1281 /* Create socket for listening. */
Damien Miller2372ace2003-05-14 13:42:23 +10001282 listen_sock = socket(ai->ai_family, ai->ai_socktype,
1283 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11001284 if (listen_sock < 0) {
1285 /* kernel may not support ipv6 */
1286 verbose("socket: %.100s", strerror(errno));
1287 continue;
1288 }
Damien Miller232711f2004-06-15 10:35:30 +10001289 if (set_nonblock(listen_sock) == -1) {
Darren Tuckerefa37062004-02-24 09:20:29 +11001290 close(listen_sock);
1291 continue;
1292 }
Damien Miller34132e52000-01-14 15:45:46 +11001293 /*
Damien Millere1383ce2002-09-19 11:49:37 +10001294 * Set socket options.
1295 * Allow local port reuse in TIME_WAIT.
Damien Miller34132e52000-01-14 15:45:46 +11001296 */
Damien Millere1383ce2002-09-19 11:49:37 +10001297 if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
1298 &on, sizeof(on)) == -1)
1299 error("setsockopt SO_REUSEADDR: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001300
Damien Miller34132e52000-01-14 15:45:46 +11001301 debug("Bind to port %s on %s.", strport, ntop);
Damien Miller95def091999-11-25 00:26:21 +11001302
Damien Miller34132e52000-01-14 15:45:46 +11001303 /* Bind the socket to the desired port. */
Damien Miller0a4e27d2001-02-18 12:36:39 +11001304 if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1305 if (!ai->ai_next)
1306 error("Bind to port %s on %s failed: %.200s.",
1307 strport, ntop, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11001308 close(listen_sock);
1309 continue;
1310 }
1311 listen_socks[num_listen_socks] = listen_sock;
1312 num_listen_socks++;
Damien Miller95def091999-11-25 00:26:21 +11001313
Damien Miller34132e52000-01-14 15:45:46 +11001314 /* Start listening on the port. */
Damien Miller996acd22003-04-09 20:59:48 +10001315 logit("Server listening on %s port %s.", ntop, strport);
Darren Tucker3175eb92003-12-09 19:15:11 +11001316 if (listen(listen_sock, SSH_LISTEN_BACKLOG) < 0)
Damien Miller34132e52000-01-14 15:45:46 +11001317 fatal("listen: %.100s", strerror(errno));
1318
Damien Miller95def091999-11-25 00:26:21 +11001319 }
Damien Miller34132e52000-01-14 15:45:46 +11001320 freeaddrinfo(options.listen_addrs);
1321
1322 if (!num_listen_socks)
1323 fatal("Cannot bind any address.");
1324
Ben Lindstrom98097862001-06-25 05:10:20 +00001325 if (options.protocol & SSH_PROTO_1)
1326 generate_ephemeral_server_key();
1327
1328 /*
1329 * Arrange to restart on SIGHUP. The handler needs
1330 * listen_sock.
1331 */
1332 signal(SIGHUP, sighup_handler);
1333
1334 signal(SIGTERM, sigterm_handler);
1335 signal(SIGQUIT, sigterm_handler);
1336
1337 /* Arrange SIGCHLD to be caught. */
1338 signal(SIGCHLD, main_sigchld_handler);
1339
1340 /* Write out the pid file after the sigterm handler is setup */
Damien Miller95def091999-11-25 00:26:21 +11001341 if (!debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +11001342 /*
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001343 * Record our pid in /var/run/sshd.pid to make it
1344 * easier to kill the correct sshd. We don't want to
1345 * do this before the bind above because the bind will
Damien Miller5428f641999-11-25 11:54:57 +11001346 * fail if there already is a daemon, and this will
1347 * overwrite any old pid in the file.
1348 */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001349 f = fopen(options.pid_file, "wb");
Darren Tuckere5327042003-07-03 13:40:44 +10001350 if (f == NULL) {
1351 error("Couldn't create pid file \"%s\": %s",
1352 options.pid_file, strerror(errno));
1353 } else {
Ben Lindstromce0f6342002-06-11 16:42:49 +00001354 fprintf(f, "%ld\n", (long) getpid());
Damien Miller95def091999-11-25 00:26:21 +11001355 fclose(f);
1356 }
1357 }
Damien Miller95def091999-11-25 00:26:21 +11001358
Damien Miller34132e52000-01-14 15:45:46 +11001359 /* setup fd set for listen */
Damien Miller37023962000-07-11 17:31:38 +10001360 fdset = NULL;
Damien Miller34132e52000-01-14 15:45:46 +11001361 maxfd = 0;
1362 for (i = 0; i < num_listen_socks; i++)
1363 if (listen_socks[i] > maxfd)
1364 maxfd = listen_socks[i];
Damien Miller37023962000-07-11 17:31:38 +10001365 /* pipes connected to unauthenticated childs */
1366 startup_pipes = xmalloc(options.max_startups * sizeof(int));
1367 for (i = 0; i < options.max_startups; i++)
1368 startup_pipes[i] = -1;
Damien Miller34132e52000-01-14 15:45:46 +11001369
Damien Miller5428f641999-11-25 11:54:57 +11001370 /*
1371 * Stay listening for connections until the system crashes or
1372 * the daemon is killed with a signal.
1373 */
Damien Miller95def091999-11-25 00:26:21 +11001374 for (;;) {
1375 if (received_sighup)
1376 sighup_restart();
Damien Miller37023962000-07-11 17:31:38 +10001377 if (fdset != NULL)
1378 xfree(fdset);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001379 fdsetsz = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
Damien Miller37023962000-07-11 17:31:38 +10001380 fdset = (fd_set *)xmalloc(fdsetsz);
Damien Miller34132e52000-01-14 15:45:46 +11001381 memset(fdset, 0, fdsetsz);
Damien Miller37023962000-07-11 17:31:38 +10001382
Damien Miller34132e52000-01-14 15:45:46 +11001383 for (i = 0; i < num_listen_socks; i++)
1384 FD_SET(listen_socks[i], fdset);
Damien Miller37023962000-07-11 17:31:38 +10001385 for (i = 0; i < options.max_startups; i++)
1386 if (startup_pipes[i] != -1)
1387 FD_SET(startup_pipes[i], fdset);
1388
1389 /* Wait in select until there is a connection. */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001390 ret = select(maxfd+1, fdset, NULL, NULL, NULL);
1391 if (ret < 0 && errno != EINTR)
1392 error("select: %.100s", strerror(errno));
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001393 if (received_sigterm) {
Damien Miller996acd22003-04-09 20:59:48 +10001394 logit("Received signal %d; terminating.",
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001395 (int) received_sigterm);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001396 close_listen_socks();
1397 unlink(options.pid_file);
1398 exit(255);
1399 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001400 if (key_used && key_do_regen) {
Ben Lindstromca42d5f2001-03-09 18:25:32 +00001401 generate_ephemeral_server_key();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001402 key_used = 0;
1403 key_do_regen = 0;
Damien Miller34132e52000-01-14 15:45:46 +11001404 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001405 if (ret < 0)
1406 continue;
1407
Damien Miller37023962000-07-11 17:31:38 +10001408 for (i = 0; i < options.max_startups; i++)
1409 if (startup_pipes[i] != -1 &&
1410 FD_ISSET(startup_pipes[i], fdset)) {
1411 /*
1412 * the read end of the pipe is ready
1413 * if the child has closed the pipe
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001414 * after successful authentication
Damien Miller37023962000-07-11 17:31:38 +10001415 * or if the child has died
1416 */
1417 close(startup_pipes[i]);
1418 startup_pipes[i] = -1;
1419 startups--;
1420 }
Damien Miller34132e52000-01-14 15:45:46 +11001421 for (i = 0; i < num_listen_socks; i++) {
1422 if (!FD_ISSET(listen_socks[i], fdset))
Damien Miller95def091999-11-25 00:26:21 +11001423 continue;
Damien Miller37023962000-07-11 17:31:38 +10001424 fromlen = sizeof(from);
1425 newsock = accept(listen_socks[i], (struct sockaddr *)&from,
1426 &fromlen);
1427 if (newsock < 0) {
1428 if (errno != EINTR && errno != EWOULDBLOCK)
1429 error("accept: %.100s", strerror(errno));
1430 continue;
1431 }
Damien Miller232711f2004-06-15 10:35:30 +10001432 if (unset_nonblock(newsock) == -1) {
Darren Tuckerefa37062004-02-24 09:20:29 +11001433 close(newsock);
1434 continue;
1435 }
Damien Miller942da032000-08-18 13:59:06 +10001436 if (drop_connection(startups) == 1) {
1437 debug("drop connection #%d", startups);
Damien Miller37023962000-07-11 17:31:38 +10001438 close(newsock);
1439 continue;
1440 }
1441 if (pipe(startup_p) == -1) {
1442 close(newsock);
1443 continue;
1444 }
1445
Darren Tucker645ab752004-06-25 13:33:20 +10001446 if (rexec_flag && socketpair(AF_UNIX,
1447 SOCK_STREAM, 0, config_s) == -1) {
1448 error("reexec socketpair: %s",
1449 strerror(errno));
1450 close(newsock);
1451 close(startup_p[0]);
1452 close(startup_p[1]);
1453 continue;
1454 }
1455
Damien Miller37023962000-07-11 17:31:38 +10001456 for (j = 0; j < options.max_startups; j++)
1457 if (startup_pipes[j] == -1) {
1458 startup_pipes[j] = startup_p[0];
1459 if (maxfd < startup_p[0])
1460 maxfd = startup_p[0];
1461 startups++;
1462 break;
1463 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001464
Damien Miller5428f641999-11-25 11:54:57 +11001465 /*
Damien Miller37023962000-07-11 17:31:38 +10001466 * Got connection. Fork a child to handle it, unless
1467 * we are in debugging mode.
Damien Miller5428f641999-11-25 11:54:57 +11001468 */
Damien Miller37023962000-07-11 17:31:38 +10001469 if (debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +11001470 /*
Damien Miller37023962000-07-11 17:31:38 +10001471 * In debugging mode. Close the listening
1472 * socket, and start processing the
1473 * connection without forking.
Damien Miller5428f641999-11-25 11:54:57 +11001474 */
Damien Miller37023962000-07-11 17:31:38 +10001475 debug("Server will not fork when running in debugging mode.");
Damien Miller34132e52000-01-14 15:45:46 +11001476 close_listen_socks();
Damien Miller95def091999-11-25 00:26:21 +11001477 sock_in = newsock;
1478 sock_out = newsock;
Darren Tucker645ab752004-06-25 13:33:20 +10001479 close(startup_p[0]);
1480 close(startup_p[1]);
Damien Miller4d97ba22000-07-11 18:15:50 +10001481 startup_pipe = -1;
Damien Miller182ee6e2000-07-12 09:45:27 +10001482 pid = getpid();
Darren Tucker645ab752004-06-25 13:33:20 +10001483 if (rexec_flag) {
1484 send_rexec_state(config_s[0],
1485 &cfg);
1486 close(config_s[0]);
1487 }
Damien Miller95def091999-11-25 00:26:21 +11001488 break;
Damien Miller37023962000-07-11 17:31:38 +10001489 } else {
1490 /*
1491 * Normal production daemon. Fork, and have
1492 * the child process the connection. The
1493 * parent continues listening.
1494 */
1495 if ((pid = fork()) == 0) {
1496 /*
1497 * Child. Close the listening and max_startup
1498 * sockets. Start using the accepted socket.
1499 * Reinitialize logging (since our pid has
1500 * changed). We break out of the loop to handle
1501 * the connection.
1502 */
1503 startup_pipe = startup_p[1];
Ben Lindstromd84df982001-12-06 16:35:40 +00001504 close_startup_pipes();
Damien Miller37023962000-07-11 17:31:38 +10001505 close_listen_socks();
1506 sock_in = newsock;
1507 sock_out = newsock;
Kevin Stevesec84dc12000-12-13 17:45:15 +00001508 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller035a5b42004-06-26 08:16:31 +10001509 close(config_s[0]);
Damien Miller37023962000-07-11 17:31:38 +10001510 break;
1511 }
Damien Miller95def091999-11-25 00:26:21 +11001512 }
Damien Miller37023962000-07-11 17:31:38 +10001513
1514 /* Parent. Stay in the loop. */
1515 if (pid < 0)
1516 error("fork: %.100s", strerror(errno));
1517 else
Ben Lindstromce0f6342002-06-11 16:42:49 +00001518 debug("Forked child %ld.", (long)pid);
Damien Miller37023962000-07-11 17:31:38 +10001519
1520 close(startup_p[1]);
1521
Darren Tucker645ab752004-06-25 13:33:20 +10001522 if (rexec_flag) {
1523 send_rexec_state(config_s[0], &cfg);
1524 close(config_s[0]);
1525 close(config_s[1]);
1526 }
1527
Damien Miller37023962000-07-11 17:31:38 +10001528 /* Mark that the key has been used (it was "given" to the child). */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001529 if ((options.protocol & SSH_PROTO_1) &&
1530 key_used == 0) {
1531 /* Schedule server key regeneration alarm. */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00001532 signal(SIGALRM, key_regeneration_alarm);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001533 alarm(options.key_regeneration_time);
1534 key_used = 1;
1535 }
Damien Miller37023962000-07-11 17:31:38 +10001536
1537 arc4random_stir();
1538
1539 /* Close the new socket (the child is now taking care of it). */
1540 close(newsock);
Damien Miller95def091999-11-25 00:26:21 +11001541 }
Damien Miller34132e52000-01-14 15:45:46 +11001542 /* child process check (or debug mode) */
1543 if (num_listen_socks < 0)
1544 break;
Damien Miller95def091999-11-25 00:26:21 +11001545 }
1546 }
1547
1548 /* This is the child processing a new connection. */
Damien Miller57aae982004-03-08 23:11:25 +11001549 setproctitle("%s", "[accepted]");
Damien Miller95def091999-11-25 00:26:21 +11001550
Darren Tucker645ab752004-06-25 13:33:20 +10001551 if (rexec_flag) {
1552 int fd;
1553
Damien Miller035a5b42004-06-26 08:16:31 +10001554 debug("rexec start in %d out %d newsock %d pipe %d sock %d",
1555 sock_in, sock_out, newsock, startup_pipe, config_s[0]);
Darren Tucker645ab752004-06-25 13:33:20 +10001556 dup2(newsock, STDIN_FILENO);
1557 dup2(STDIN_FILENO, STDOUT_FILENO);
1558 if (startup_pipe == -1)
Damien Miller035a5b42004-06-26 08:16:31 +10001559 close(REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001560 else
Damien Miller035a5b42004-06-26 08:16:31 +10001561 dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001562
Damien Miller035a5b42004-06-26 08:16:31 +10001563 dup2(config_s[1], REEXEC_CONFIG_PASS_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001564 close(config_s[1]);
Damien Miller035a5b42004-06-26 08:16:31 +10001565 close(startup_pipe);
1566
Darren Tucker645ab752004-06-25 13:33:20 +10001567 execv(rexec_argv[0], rexec_argv);
1568
1569 /* Reexec has failed, fall back and continue */
1570 error("rexec of %s failed: %s", rexec_argv[0], strerror(errno));
Damien Miller035a5b42004-06-26 08:16:31 +10001571 recv_rexec_state(REEXEC_CONFIG_PASS_FD, NULL);
Darren Tucker645ab752004-06-25 13:33:20 +10001572 log_init(__progname, options.log_level,
1573 options.log_facility, log_stderr);
1574
1575 /* Clean up fds */
Damien Miller035a5b42004-06-26 08:16:31 +10001576 startup_pipe = REEXEC_STARTUP_PIPE_FD;
Darren Tucker645ab752004-06-25 13:33:20 +10001577 close(config_s[1]);
Damien Miller035a5b42004-06-26 08:16:31 +10001578 close(REEXEC_CONFIG_PASS_FD);
1579 newsock = sock_out = sock_in = dup(STDIN_FILENO);
Darren Tucker645ab752004-06-25 13:33:20 +10001580 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1581 dup2(fd, STDIN_FILENO);
1582 dup2(fd, STDOUT_FILENO);
1583 if (fd > STDERR_FILENO)
1584 close(fd);
1585 }
Damien Miller035a5b42004-06-26 08:16:31 +10001586 debug("rexec cleanup in %d out %d newsock %d pipe %d sock %d",
1587 sock_in, sock_out, newsock, startup_pipe, config_s[0]);
Darren Tucker645ab752004-06-25 13:33:20 +10001588 }
1589
Damien Miller5428f641999-11-25 11:54:57 +11001590 /*
Ben Lindstrom17401b62002-05-15 16:17:56 +00001591 * 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 */
Darren Tuckerc437cda2003-05-10 17:05:46 +10001595#if !defined(SSHD_ACQUIRES_CTTY)
Damien Miller933cc8f2003-03-10 11:38:10 +11001596 /*
Darren Tuckerc437cda2003-05-10 17:05:46 +10001597 * 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.
Damien Miller933cc8f2003-03-10 11:38:10 +11001600 */
Ben Lindstrom57f08002002-06-23 00:37:10 +00001601 if (!debug_flag && !inetd_flag && setsid() < 0)
Ben Lindstrom17401b62002-05-15 16:17:56 +00001602 error("setsid: %.100s", strerror(errno));
Kevin Stevesc5041ac2002-05-21 17:50:21 +00001603#endif
Ben Lindstrom17401b62002-05-15 16:17:56 +00001604
1605 /*
Damien Miller5428f641999-11-25 11:54:57 +11001606 * Disable the key regeneration alarm. We will not regenerate the
1607 * key since we are no longer in a position to give it to anyone. We
1608 * will not restart on SIGHUP since it no longer makes sense.
1609 */
Damien Miller95def091999-11-25 00:26:21 +11001610 alarm(0);
1611 signal(SIGALRM, SIG_DFL);
1612 signal(SIGHUP, SIG_DFL);
1613 signal(SIGTERM, SIG_DFL);
1614 signal(SIGQUIT, SIG_DFL);
1615 signal(SIGCHLD, SIG_DFL);
Damien Miller4e0f5e12000-08-29 11:05:50 +11001616 signal(SIGINT, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001617
Damien Miller12c150e2003-12-17 16:31:10 +11001618 /* Set SO_KEEPALIVE if requested. */
1619 if (options.tcp_keep_alive &&
Ben Lindstrom733a2352002-03-05 01:31:28 +00001620 setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on,
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001621 sizeof(on)) < 0)
1622 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1623
Damien Miller5428f641999-11-25 11:54:57 +11001624 /*
1625 * Register our connection. This turns encryption off because we do
1626 * not have a key.
1627 */
Damien Miller95def091999-11-25 00:26:21 +11001628 packet_set_connection(sock_in, sock_out);
1629
1630 remote_port = get_remote_port();
1631 remote_ip = get_remote_ipaddr();
1632
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001633#ifdef LIBWRAP
Damien Miller6a4a4b92001-11-12 11:07:11 +11001634 /* Check whether logins are denied from this host. */
Darren Tucker586b0b92004-06-25 13:34:31 +10001635 if (packet_connection_is_on_socket()) {
Damien Miller95def091999-11-25 00:26:21 +11001636 struct request_info req;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001637
Ben Lindstromce89dac2001-09-12 16:58:04 +00001638 request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, 0);
Damien Miller95def091999-11-25 00:26:21 +11001639 fromhost(&req);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001640
Damien Miller95def091999-11-25 00:26:21 +11001641 if (!hosts_access(&req)) {
Damien Miller6a4a4b92001-11-12 11:07:11 +11001642 debug("Connection refused by tcp wrapper");
Ben Lindstrom7de696e2001-03-29 00:45:12 +00001643 refuse(&req);
Damien Miller6a4a4b92001-11-12 11:07:11 +11001644 /* NOTREACHED */
1645 fatal("libwrap refuse returns");
Damien Miller95def091999-11-25 00:26:21 +11001646 }
Damien Miller95def091999-11-25 00:26:21 +11001647 }
Damien Miller34132e52000-01-14 15:45:46 +11001648#endif /* LIBWRAP */
Damien Miller6a4a4b92001-11-12 11:07:11 +11001649
Damien Miller95def091999-11-25 00:26:21 +11001650 /* Log the connection. */
1651 verbose("Connection from %.500s port %d", remote_ip, remote_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001652
Damien Miller5428f641999-11-25 11:54:57 +11001653 /*
1654 * We don\'t want to listen forever unless the other side
1655 * successfully authenticates itself. So we set up an alarm which is
1656 * cleared after successful authentication. A limit of zero
1657 * indicates no limit. Note that we don\'t set the alarm in debugging
1658 * mode; it is just annoying to have the server exit just when you
1659 * are about to discover the bug.
1660 */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00001661 signal(SIGALRM, grace_alarm_handler);
Damien Miller95def091999-11-25 00:26:21 +11001662 if (!debug_flag)
1663 alarm(options.login_grace_time);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001664
Damien Millerb38eff82000-04-01 11:09:21 +10001665 sshd_exchange_identification(sock_in, sock_out);
Darren Tuckerec960f22003-08-13 20:37:05 +10001666
Damien Miller95def091999-11-25 00:26:21 +11001667 packet_set_nonblocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001668
Damien Millera8e06ce2003-11-21 23:48:55 +11001669 /* prepare buffers to collect authentication messages */
Darren Tuckerb9aa0a02003-07-08 22:59:59 +10001670 buffer_init(&loginmsg);
1671
Darren Tucker3e33cec2003-10-02 16:12:36 +10001672 /* allocate authentication context */
1673 authctxt = xmalloc(sizeof(*authctxt));
1674 memset(authctxt, 0, sizeof(*authctxt));
1675
1676 /* XXX global for cleanup, access from other modules */
1677 the_authctxt = authctxt;
1678
Ben Lindstrom943481c2002-03-22 03:43:46 +00001679 if (use_privsep)
Darren Tucker3e33cec2003-10-02 16:12:36 +10001680 if (privsep_preauth(authctxt) == 1)
Ben Lindstrom943481c2002-03-22 03:43:46 +00001681 goto authenticated;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001682
Damien Miller396691a2000-01-20 22:44:08 +11001683 /* perform the key exchange */
Damien Miller396691a2000-01-20 22:44:08 +11001684 /* authenticate user and start session */
Damien Millerefb4afe2000-04-12 18:45:05 +10001685 if (compat20) {
1686 do_ssh2_kex();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001687 do_authentication2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001688 } else {
1689 do_ssh1_kex();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001690 do_authentication(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001691 }
Ben Lindstrom943481c2002-03-22 03:43:46 +00001692 /*
1693 * If we use privilege separation, the unprivileged child transfers
1694 * the current keystate and exits
1695 */
1696 if (use_privsep) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001697 mm_send_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001698 exit(0);
Ben Lindstrom943481c2002-03-22 03:43:46 +00001699 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001700
1701 authenticated:
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001702 /*
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001703 * In privilege separation, we fork another child and prepare
1704 * file descriptor passing.
1705 */
1706 if (use_privsep) {
Ben Lindstrom943481c2002-03-22 03:43:46 +00001707 privsep_postauth(authctxt);
1708 /* the monitor process [priv] will not return */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001709 if (!compat20)
1710 destroy_sensitive_data();
1711 }
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00001712
Darren Tucker3e33cec2003-10-02 16:12:36 +10001713 /* Start session. */
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00001714 do_authenticated(authctxt);
1715
Damien Miller3a5b0232002-03-13 13:19:42 +11001716 /* The connection has been terminated. */
1717 verbose("Closing connection to %.100s", remote_ip);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001718
Damien Millerbeb4ba51999-12-28 15:09:35 +11001719#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +10001720 if (options.use_pam)
1721 finish_pam();
Damien Millerbeb4ba51999-12-28 15:09:35 +11001722#endif /* USE_PAM */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001723
Damien Miller95def091999-11-25 00:26:21 +11001724 packet_close();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001725
1726 if (use_privsep)
1727 mm_terminate();
1728
Damien Miller95def091999-11-25 00:26:21 +11001729 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001730}
1731
Damien Miller95def091999-11-25 00:26:21 +11001732/*
Ben Lindstromabcb1452002-03-22 01:10:21 +00001733 * Decrypt session_key_int using our private server key and private host key
1734 * (key with larger modulus first).
1735 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001736int
Ben Lindstromabcb1452002-03-22 01:10:21 +00001737ssh1_session_key(BIGNUM *session_key_int)
1738{
1739 int rsafail = 0;
1740
1741 if (BN_cmp(sensitive_data.server_key->rsa->n, sensitive_data.ssh1_host_key->rsa->n) > 0) {
1742 /* Server key has bigger modulus. */
1743 if (BN_num_bits(sensitive_data.server_key->rsa->n) <
1744 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1745 fatal("do_connection: %s: server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
1746 get_remote_ipaddr(),
1747 BN_num_bits(sensitive_data.server_key->rsa->n),
1748 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1749 SSH_KEY_BITS_RESERVED);
1750 }
1751 if (rsa_private_decrypt(session_key_int, session_key_int,
1752 sensitive_data.server_key->rsa) <= 0)
1753 rsafail++;
1754 if (rsa_private_decrypt(session_key_int, session_key_int,
1755 sensitive_data.ssh1_host_key->rsa) <= 0)
1756 rsafail++;
1757 } else {
1758 /* Host key has bigger modulus (or they are equal). */
1759 if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
1760 BN_num_bits(sensitive_data.server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1761 fatal("do_connection: %s: host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
1762 get_remote_ipaddr(),
1763 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1764 BN_num_bits(sensitive_data.server_key->rsa->n),
1765 SSH_KEY_BITS_RESERVED);
1766 }
1767 if (rsa_private_decrypt(session_key_int, session_key_int,
1768 sensitive_data.ssh1_host_key->rsa) < 0)
1769 rsafail++;
1770 if (rsa_private_decrypt(session_key_int, session_key_int,
1771 sensitive_data.server_key->rsa) < 0)
1772 rsafail++;
1773 }
1774 return (rsafail);
1775}
1776/*
Damien Miller396691a2000-01-20 22:44:08 +11001777 * SSH1 key exchange
Damien Miller95def091999-11-25 00:26:21 +11001778 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001779static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001780do_ssh1_kex(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001781{
Damien Miller95def091999-11-25 00:26:21 +11001782 int i, len;
Damien Miller7650bc62001-01-30 09:27:26 +11001783 int rsafail = 0;
Damien Miller95def091999-11-25 00:26:21 +11001784 BIGNUM *session_key_int;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001785 u_char session_key[SSH_SESSION_KEY_LENGTH];
1786 u_char cookie[8];
1787 u_int cipher_type, auth_mask, protocol_flags;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001788 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001789
Damien Miller5428f641999-11-25 11:54:57 +11001790 /*
1791 * Generate check bytes that the client must send back in the user
1792 * packet in order for it to be accepted; this is used to defy ip
1793 * spoofing attacks. Note that this only works against somebody
1794 * doing IP spoofing from a remote machine; any machine on the local
1795 * network can still see outgoing packets and catch the random
1796 * cookie. This only affects rhosts authentication, and this is one
1797 * of the reasons why it is inherently insecure.
1798 */
Damien Miller95def091999-11-25 00:26:21 +11001799 for (i = 0; i < 8; i++) {
1800 if (i % 4 == 0)
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001801 rnd = arc4random();
1802 cookie[i] = rnd & 0xff;
1803 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +11001804 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001805
Damien Miller5428f641999-11-25 11:54:57 +11001806 /*
1807 * Send our public key. We include in the packet 64 bits of random
1808 * data that must be matched in the reply in order to prevent IP
1809 * spoofing.
1810 */
Damien Miller95def091999-11-25 00:26:21 +11001811 packet_start(SSH_SMSG_PUBLIC_KEY);
1812 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001813 packet_put_char(cookie[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001814
Damien Miller95def091999-11-25 00:26:21 +11001815 /* Store our public server RSA key. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001816 packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
1817 packet_put_bignum(sensitive_data.server_key->rsa->e);
1818 packet_put_bignum(sensitive_data.server_key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001819
Damien Miller95def091999-11-25 00:26:21 +11001820 /* Store our public host RSA key. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001821 packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1822 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
1823 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001824
Damien Miller95def091999-11-25 00:26:21 +11001825 /* Put protocol flags. */
1826 packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001827
Damien Miller95def091999-11-25 00:26:21 +11001828 /* Declare which ciphers we support. */
Damien Miller874d77b2000-10-14 16:23:11 +11001829 packet_put_int(cipher_mask_ssh1(0));
Damien Miller95def091999-11-25 00:26:21 +11001830
1831 /* Declare supported authentication types. */
1832 auth_mask = 0;
Damien Miller95def091999-11-25 00:26:21 +11001833 if (options.rhosts_rsa_authentication)
1834 auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
1835 if (options.rsa_authentication)
1836 auth_mask |= 1 << SSH_AUTH_RSA;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001837 if (options.challenge_response_authentication == 1)
Damien Miller95def091999-11-25 00:26:21 +11001838 auth_mask |= 1 << SSH_AUTH_TIS;
Damien Miller95def091999-11-25 00:26:21 +11001839 if (options.password_authentication)
1840 auth_mask |= 1 << SSH_AUTH_PASSWORD;
1841 packet_put_int(auth_mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001842
Damien Miller95def091999-11-25 00:26:21 +11001843 /* Send the packet and wait for it to be sent. */
1844 packet_send();
1845 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001846
Damien Miller0bc1bd82000-11-13 22:57:25 +11001847 debug("Sent %d bit server key and %d bit host key.",
1848 BN_num_bits(sensitive_data.server_key->rsa->n),
1849 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001850
Damien Miller95def091999-11-25 00:26:21 +11001851 /* Read clients reply (cipher type and session key). */
Damien Millerdff50992002-01-22 23:16:32 +11001852 packet_read_expect(SSH_CMSG_SESSION_KEY);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001853
Damien Miller50945fa1999-12-09 10:31:37 +11001854 /* Get cipher type and check whether we accept this. */
Damien Miller95def091999-11-25 00:26:21 +11001855 cipher_type = packet_get_char();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001856
Damien Miller874d77b2000-10-14 16:23:11 +11001857 if (!(cipher_mask_ssh1(0) & (1 << cipher_type)))
Damien Miller50945fa1999-12-09 10:31:37 +11001858 packet_disconnect("Warning: client selects unsupported cipher.");
1859
Damien Miller95def091999-11-25 00:26:21 +11001860 /* Get check bytes from the packet. These must match those we
1861 sent earlier with the public key packet. */
1862 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001863 if (cookie[i] != packet_get_char())
Damien Miller95def091999-11-25 00:26:21 +11001864 packet_disconnect("IP Spoofing check bytes do not match.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001865
Damien Miller95def091999-11-25 00:26:21 +11001866 debug("Encryption type: %.200s", cipher_name(cipher_type));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001867
Damien Miller95def091999-11-25 00:26:21 +11001868 /* Get the encrypted integer. */
Damien Millerda755162002-01-22 23:09:22 +11001869 if ((session_key_int = BN_new()) == NULL)
1870 fatal("do_ssh1_kex: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +11001871 packet_get_bignum(session_key_int);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001872
Damien Miller95def091999-11-25 00:26:21 +11001873 protocol_flags = packet_get_int();
1874 packet_set_protocol_flags(protocol_flags);
Damien Miller48b03fc2002-01-22 23:11:40 +11001875 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001876
Ben Lindstromabcb1452002-03-22 01:10:21 +00001877 /* Decrypt session_key_int using host/server keys */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001878 rsafail = PRIVSEP(ssh1_session_key(session_key_int));
1879
Damien Miller5428f641999-11-25 11:54:57 +11001880 /*
1881 * Extract session key from the decrypted integer. The key is in the
1882 * least significant 256 bits of the integer; the first byte of the
1883 * key is in the highest bits.
1884 */
Damien Miller7650bc62001-01-30 09:27:26 +11001885 if (!rsafail) {
1886 BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1887 len = BN_num_bytes(session_key_int);
1888 if (len < 0 || len > sizeof(session_key)) {
1889 error("do_connection: bad session key len from %s: "
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001890 "session_key_int %d > sizeof(session_key) %lu",
1891 get_remote_ipaddr(), len, (u_long)sizeof(session_key));
Damien Miller7650bc62001-01-30 09:27:26 +11001892 rsafail++;
1893 } else {
1894 memset(session_key, 0, sizeof(session_key));
1895 BN_bn2bin(session_key_int,
1896 session_key + sizeof(session_key) - len);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001897
Darren Tuckere14e0052004-05-13 16:30:44 +10001898 derive_ssh1_session_id(
Darren Tuckerfc959702004-07-17 16:12:08 +10001899 sensitive_data.ssh1_host_key->rsa->n,
Darren Tuckere14e0052004-05-13 16:30:44 +10001900 sensitive_data.server_key->rsa->n,
1901 cookie, session_id);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001902 /*
1903 * Xor the first 16 bytes of the session key with the
1904 * session id.
1905 */
1906 for (i = 0; i < 16; i++)
1907 session_key[i] ^= session_id[i];
Damien Miller7650bc62001-01-30 09:27:26 +11001908 }
1909 }
1910 if (rsafail) {
Ben Lindstromeb648a72001-03-05 06:00:29 +00001911 int bytes = BN_num_bytes(session_key_int);
Ben Lindstrom13c5d3b2002-02-26 18:00:48 +00001912 u_char *buf = xmalloc(bytes);
Ben Lindstromeb648a72001-03-05 06:00:29 +00001913 MD5_CTX md;
1914
Damien Miller996acd22003-04-09 20:59:48 +10001915 logit("do_connection: generating a fake encryption key");
Ben Lindstromeb648a72001-03-05 06:00:29 +00001916 BN_bn2bin(session_key_int, buf);
1917 MD5_Init(&md);
1918 MD5_Update(&md, buf, bytes);
1919 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1920 MD5_Final(session_key, &md);
1921 MD5_Init(&md);
1922 MD5_Update(&md, session_key, 16);
1923 MD5_Update(&md, buf, bytes);
1924 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1925 MD5_Final(session_key + 16, &md);
1926 memset(buf, 0, bytes);
1927 xfree(buf);
Ben Lindstrom941ac822001-03-05 06:25:23 +00001928 for (i = 0; i < 16; i++)
1929 session_id[i] = session_key[i] ^ session_key[i + 16];
Damien Miller7650bc62001-01-30 09:27:26 +11001930 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001931 /* Destroy the private and public keys. No longer. */
Damien Miller3a5b0232002-03-13 13:19:42 +11001932 destroy_sensitive_data();
Ben Lindstromeb648a72001-03-05 06:00:29 +00001933
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001934 if (use_privsep)
1935 mm_ssh1_session_id(session_id);
1936
Damien Miller396691a2000-01-20 22:44:08 +11001937 /* Destroy the decrypted integer. It is no longer needed. */
1938 BN_clear_free(session_key_int);
1939
Damien Miller95def091999-11-25 00:26:21 +11001940 /* Set the session key. From this on all communications will be encrypted. */
1941 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001942
Damien Miller95def091999-11-25 00:26:21 +11001943 /* Destroy our copy of the session key. It is no longer needed. */
1944 memset(session_key, 0, sizeof(session_key));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001945
Damien Miller95def091999-11-25 00:26:21 +11001946 debug("Received session key; encryption turned on.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001947
Ben Lindstromf666fec2002-06-06 19:51:58 +00001948 /* Send an acknowledgment packet. Note that this packet is sent encrypted. */
Damien Miller95def091999-11-25 00:26:21 +11001949 packet_start(SSH_SMSG_SUCCESS);
1950 packet_send();
1951 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001952}
Damien Millerefb4afe2000-04-12 18:45:05 +10001953
1954/*
1955 * SSH2 key exchange: diffie-hellman-group1-sha1
1956 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001957static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001958do_ssh2_kex(void)
Damien Millerefb4afe2000-04-12 18:45:05 +10001959{
Damien Millerefb4afe2000-04-12 18:45:05 +10001960 Kex *kex;
Damien Millerefb4afe2000-04-12 18:45:05 +10001961
Damien Miller78928792000-04-12 20:17:38 +10001962 if (options.ciphers != NULL) {
Damien Miller4af51302000-04-16 11:18:38 +10001963 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Damien Miller78928792000-04-12 20:17:38 +10001964 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
1965 }
Damien Millera0ff4662001-03-30 10:49:35 +10001966 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1967 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
1968 myproposal[PROPOSAL_ENC_ALGS_STOC] =
1969 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
1970
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001971 if (options.macs != NULL) {
1972 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
1973 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1974 }
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001975 if (!options.compression) {
1976 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1977 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
1978 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001979 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = list_hostkey_types();
1980
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001981 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +00001982 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +11001983 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001984 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Miller8e7fb332003-02-24 12:03:03 +11001985 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00001986 kex->server = 1;
1987 kex->client_version_string=client_version_string;
1988 kex->server_version_string=server_version_string;
1989 kex->load_host_key=&get_hostkey_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001990 kex->host_key_index=&get_hostkey_index;
Damien Millerefb4afe2000-04-12 18:45:05 +10001991
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001992 xxx_kex = kex;
1993
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001994 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +11001995
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001996 session_id2 = kex->session_id;
1997 session_id2_len = kex->session_id_len;
1998
Damien Miller874d77b2000-10-14 16:23:11 +11001999#ifdef DEBUG_KEXDH
2000 /* send 1st encrypted/maced/compressed message */
2001 packet_start(SSH2_MSG_IGNORE);
2002 packet_put_cstring("markus");
2003 packet_send();
2004 packet_write_wait();
2005#endif
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00002006 debug("KEX done");
Damien Millerefb4afe2000-04-12 18:45:05 +10002007}
Darren Tucker3e33cec2003-10-02 16:12:36 +10002008
2009/* server specific fatal cleanup */
2010void
2011cleanup_exit(int i)
2012{
2013 if (the_authctxt)
2014 do_cleanup(the_authctxt);
2015 _exit(i);
2016}