blob: ec860024b3d7e6a367eb7d08e2d36b18de0d1dc6 [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
5 * Created: Fri Mar 17 17:09:28 1995 ylo
6 * This program is the ssh daemon. It listens for connections from clients, and
7 * performs authentication, executes use commands or shell, and forwards
8 * information to/from the application to the user client over an encrypted
9 * connection. This can also handle forwarding of X11, TCP/IP, and authentication
10 * agent connections.
Damien Millerefb4afe2000-04-12 18:45:05 +100011 *
12 * SSH2 implementation,
13 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Miller95def091999-11-25 00:26:21 +110014 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "includes.h"
Damien Millerb1715dc2000-05-30 13:44:51 +100017RCSID("$OpenBSD: sshd.c,v 1.118 2000/05/25 20:45:20 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
19#include "xmalloc.h"
20#include "rsa.h"
21#include "ssh.h"
22#include "pty.h"
23#include "packet.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024#include "cipher.h"
25#include "mpaux.h"
26#include "servconf.h"
27#include "uidswap.h"
28#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100029#include "buffer.h"
30
Damien Millerefb4afe2000-04-12 18:45:05 +100031#include "ssh2.h"
Damien Miller5f056372000-04-16 12:31:48 +100032#include <openssl/dh.h>
33#include <openssl/bn.h>
34#include <openssl/hmac.h>
Damien Millerefb4afe2000-04-12 18:45:05 +100035#include "kex.h"
Damien Miller5f056372000-04-16 12:31:48 +100036#include <openssl/dsa.h>
37#include <openssl/rsa.h>
Damien Millerb38eff82000-04-01 11:09:21 +100038#include "key.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100039#include "dsa.h"
Damien Millerb38eff82000-04-01 11:09:21 +100040
41#include "auth.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100042#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100043#include "authfile.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45#ifdef LIBWRAP
46#include <tcpd.h>
47#include <syslog.h>
48int allow_severity = LOG_INFO;
49int deny_severity = LOG_WARNING;
50#endif /* LIBWRAP */
51
52#ifndef O_NOCTTY
53#define O_NOCTTY 0
54#endif
55
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056/* Server configuration options. */
57ServerOptions options;
58
59/* Name of the server configuration file. */
60char *config_file_name = SERVER_CONFIG_FILE;
61
Damien Miller4af51302000-04-16 11:18:38 +100062/*
Damien Miller34132e52000-01-14 15:45:46 +110063 * Flag indicating whether IPv4 or IPv6. This can be set on the command line.
64 * Default value is AF_UNSPEC means both IPv4 and IPv6.
65 */
Damien Miller7d80e342000-01-19 14:36:49 +110066#ifdef IPV4_DEFAULT
67int IPv4or6 = AF_INET;
68#else
Damien Miller34132e52000-01-14 15:45:46 +110069int IPv4or6 = AF_UNSPEC;
Damien Miller7d80e342000-01-19 14:36:49 +110070#endif
Damien Miller34132e52000-01-14 15:45:46 +110071
Damien Miller95def091999-11-25 00:26:21 +110072/*
73 * Debug mode flag. This can be set on the command line. If debug
74 * mode is enabled, extra debugging output will be sent to the system
75 * log, the daemon will not go to background, and will exit after processing
76 * the first connection.
77 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078int debug_flag = 0;
79
80/* Flag indicating that the daemon is being started from inetd. */
81int inetd_flag = 0;
82
Damien Miller5ce662a1999-11-11 17:57:39 +110083/* debug goes to stderr unless inetd_flag is set */
84int log_stderr = 0;
85
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086/* argv[0] without path. */
87char *av0;
88
89/* Saved arguments to main(). */
90char **saved_argv;
91
Damien Miller5428f641999-11-25 11:54:57 +110092/*
Damien Miller34132e52000-01-14 15:45:46 +110093 * The sockets that the server is listening; this is used in the SIGHUP
94 * signal handler.
Damien Miller5428f641999-11-25 11:54:57 +110095 */
Damien Miller34132e52000-01-14 15:45:46 +110096#define MAX_LISTEN_SOCKS 16
97int listen_socks[MAX_LISTEN_SOCKS];
98int num_listen_socks = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * the client's version string, passed by sshd2 in compat mode. if != NULL,
102 * sshd will skip the version-number exchange
103 */
Damien Miller95def091999-11-25 00:26:21 +1100104char *client_version_string = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000105char *server_version_string = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller5428f641999-11-25 11:54:57 +1100107/*
108 * Any really sensitive data in the application is contained in this
109 * structure. The idea is that this structure could be locked into memory so
110 * that the pages do not get written into swap. However, there are some
111 * problems. The private key contains BIGNUMs, and we do not (in principle)
112 * have access to the internals of them, and locking just the structure is
113 * not very useful. Currently, memory locking is not implemented.
114 */
Damien Miller95def091999-11-25 00:26:21 +1100115struct {
Damien Millereba71ba2000-04-29 23:57:08 +1000116 RSA *private_key; /* Private part of empheral server key. */
Damien Miller95def091999-11-25 00:26:21 +1100117 RSA *host_key; /* Private part of host key. */
Damien Millereba71ba2000-04-29 23:57:08 +1000118 Key *dsa_host_key; /* Private DSA host key. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119} sensitive_data;
120
Damien Miller5428f641999-11-25 11:54:57 +1100121/*
122 * Flag indicating whether the current session key has been used. This flag
123 * is set whenever the key is used, and cleared when the key is regenerated.
124 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125int key_used = 0;
126
127/* This is set to true when SIGHUP is received. */
128int received_sighup = 0;
129
130/* Public side of the server key. This value is regenerated regularly with
131 the private key. */
132RSA *public_key;
133
Damien Millerb38eff82000-04-01 11:09:21 +1000134/* session identifier, used by RSA-auth */
135unsigned char session_id[16];
136
Damien Millereba71ba2000-04-29 23:57:08 +1000137/* same for ssh2 */
138unsigned char *session_id2 = NULL;
139int session_id2_len = 0;
140
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141/* Prototypes for various functions defined later in this file. */
Damien Millerb38eff82000-04-01 11:09:21 +1000142void do_ssh1_kex();
Damien Millerefb4afe2000-04-12 18:45:05 +1000143void do_ssh2_kex();
Damien Miller98c7ad62000-03-09 21:27:49 +1100144
145/*
Damien Miller34132e52000-01-14 15:45:46 +1100146 * Close all listening sockets
147 */
148void
149close_listen_socks(void)
150{
151 int i;
152 for (i = 0; i < num_listen_socks; i++)
153 close(listen_socks[i]);
154 num_listen_socks = -1;
155}
156
157/*
Damien Miller95def091999-11-25 00:26:21 +1100158 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
159 * the effect is to reread the configuration file (and to regenerate
160 * the server key).
161 */
Damien Miller4af51302000-04-16 11:18:38 +1000162void
Damien Miller95def091999-11-25 00:26:21 +1100163sighup_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164{
Damien Miller95def091999-11-25 00:26:21 +1100165 received_sighup = 1;
166 signal(SIGHUP, sighup_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167}
168
Damien Miller95def091999-11-25 00:26:21 +1100169/*
170 * Called from the main program after receiving SIGHUP.
171 * Restarts the server.
172 */
Damien Miller4af51302000-04-16 11:18:38 +1000173void
Damien Miller95def091999-11-25 00:26:21 +1100174sighup_restart()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175{
Damien Miller95def091999-11-25 00:26:21 +1100176 log("Received SIGHUP; restarting.");
Damien Miller34132e52000-01-14 15:45:46 +1100177 close_listen_socks();
Damien Miller95def091999-11-25 00:26:21 +1100178 execv(saved_argv[0], saved_argv);
179 log("RESTART FAILED: av0='%s', error: %s.", av0, strerror(errno));
180 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181}
182
Damien Miller95def091999-11-25 00:26:21 +1100183/*
184 * Generic signal handler for terminating signals in the master daemon.
185 * These close the listen socket; not closing it seems to cause "Address
186 * already in use" problems on some machines, which is inconvenient.
187 */
Damien Miller4af51302000-04-16 11:18:38 +1000188void
Damien Miller95def091999-11-25 00:26:21 +1100189sigterm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190{
Damien Miller95def091999-11-25 00:26:21 +1100191 log("Received signal %d; terminating.", sig);
Damien Miller34132e52000-01-14 15:45:46 +1100192 close_listen_socks();
Damien Miller6f83b8e2000-05-02 09:23:45 +1000193 unlink(options.pid_file);
Damien Miller95def091999-11-25 00:26:21 +1100194 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195}
196
Damien Miller95def091999-11-25 00:26:21 +1100197/*
198 * SIGCHLD handler. This is called whenever a child dies. This will then
199 * reap any zombies left by exited c.
200 */
Damien Miller4af51302000-04-16 11:18:38 +1000201void
Damien Miller95def091999-11-25 00:26:21 +1100202main_sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203{
Damien Miller95def091999-11-25 00:26:21 +1100204 int save_errno = errno;
205 int status;
Damien Miller431f66b1999-11-21 18:31:57 +1100206
Damien Miller95def091999-11-25 00:26:21 +1100207 while (waitpid(-1, &status, WNOHANG) > 0)
208 ;
Damien Miller431f66b1999-11-21 18:31:57 +1100209
Damien Miller95def091999-11-25 00:26:21 +1100210 signal(SIGCHLD, main_sigchld_handler);
211 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212}
213
Damien Miller95def091999-11-25 00:26:21 +1100214/*
215 * Signal handler for the alarm after the login grace period has expired.
216 */
Damien Miller4af51302000-04-16 11:18:38 +1000217void
Damien Miller95def091999-11-25 00:26:21 +1100218grace_alarm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219{
Damien Miller95def091999-11-25 00:26:21 +1100220 /* Close the connection. */
221 packet_close();
222
223 /* Log error and exit. */
224 fatal("Timeout before authentication for %s.", get_remote_ipaddr());
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225}
226
Damien Miller95def091999-11-25 00:26:21 +1100227/*
Damien Miller95def091999-11-25 00:26:21 +1100228 * Signal handler for the key regeneration alarm. Note that this
229 * alarm only occurs in the daemon waiting for connections, and it does not
230 * do anything with the private key or random state before forking.
231 * Thus there should be no concurrency control/asynchronous execution
232 * problems.
233 */
Damien Millereba71ba2000-04-29 23:57:08 +1000234/* XXX do we really want this work to be done in a signal handler ? -m */
Damien Miller4af51302000-04-16 11:18:38 +1000235void
Damien Miller95def091999-11-25 00:26:21 +1100236key_regeneration_alarm(int sig)
237{
238 int save_errno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239
Damien Miller95def091999-11-25 00:26:21 +1100240 /* Check if we should generate a new key. */
241 if (key_used) {
242 /* This should really be done in the background. */
243 log("Generating new %d bit RSA key.", options.server_key_bits);
244
245 if (sensitive_data.private_key != NULL)
246 RSA_free(sensitive_data.private_key);
247 sensitive_data.private_key = RSA_new();
248
249 if (public_key != NULL)
250 RSA_free(public_key);
251 public_key = RSA_new();
252
253 rsa_generate_key(sensitive_data.private_key, public_key,
254 options.server_key_bits);
255 arc4random_stir();
256 key_used = 0;
257 log("RSA key generation complete.");
258 }
259 /* Reschedule the alarm. */
260 signal(SIGALRM, key_regeneration_alarm);
261 alarm(options.key_regeneration_time);
262 errno = save_errno;
263}
264
Damien Millerb38eff82000-04-01 11:09:21 +1000265void
266sshd_exchange_identification(int sock_in, int sock_out)
267{
Damien Miller78928792000-04-12 20:17:38 +1000268 int i, mismatch;
Damien Millerb38eff82000-04-01 11:09:21 +1000269 int remote_major, remote_minor;
Damien Miller78928792000-04-12 20:17:38 +1000270 int major, minor;
Damien Millerb38eff82000-04-01 11:09:21 +1000271 char *s;
272 char buf[256]; /* Must not be larger than remote_version. */
273 char remote_version[256]; /* Must be at least as big as buf. */
274
Damien Miller78928792000-04-12 20:17:38 +1000275 if ((options.protocol & SSH_PROTO_1) &&
276 (options.protocol & SSH_PROTO_2)) {
277 major = PROTOCOL_MAJOR_1;
278 minor = 99;
279 } else if (options.protocol & SSH_PROTO_2) {
280 major = PROTOCOL_MAJOR_2;
281 minor = PROTOCOL_MINOR_2;
282 } else {
283 major = PROTOCOL_MAJOR_1;
284 minor = PROTOCOL_MINOR_1;
285 }
286 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
Damien Millerb38eff82000-04-01 11:09:21 +1000287 server_version_string = xstrdup(buf);
288
289 if (client_version_string == NULL) {
290 /* Send our protocol version identification. */
291 if (atomicio(write, sock_out, server_version_string, strlen(server_version_string))
292 != strlen(server_version_string)) {
293 log("Could not write ident string to %s.", get_remote_ipaddr());
294 fatal_cleanup();
295 }
296
297 /* Read other side\'s version identification. */
298 for (i = 0; i < sizeof(buf) - 1; i++) {
299 if (read(sock_in, &buf[i], 1) != 1) {
300 log("Did not receive ident string from %s.", get_remote_ipaddr());
301 fatal_cleanup();
302 }
303 if (buf[i] == '\r') {
304 buf[i] = '\n';
305 buf[i + 1] = 0;
306 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000307 }
308 if (buf[i] == '\n') {
309 /* buf[i] == '\n' */
310 buf[i + 1] = 0;
311 break;
312 }
313 }
314 buf[sizeof(buf) - 1] = 0;
315 client_version_string = xstrdup(buf);
316 }
317
318 /*
319 * Check that the versions match. In future this might accept
320 * several versions and set appropriate flags to handle them.
321 */
322 if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
323 &remote_major, &remote_minor, remote_version) != 3) {
Damien Miller4af51302000-04-16 11:18:38 +1000324 s = "Protocol mismatch.\n";
Damien Millerb38eff82000-04-01 11:09:21 +1000325 (void) atomicio(write, sock_out, s, strlen(s));
326 close(sock_in);
327 close(sock_out);
328 log("Bad protocol version identification '%.100s' from %s",
329 client_version_string, get_remote_ipaddr());
330 fatal_cleanup();
331 }
332 debug("Client protocol version %d.%d; client software version %.100s",
333 remote_major, remote_minor, remote_version);
334
Damien Millerefb4afe2000-04-12 18:45:05 +1000335 compat_datafellows(remote_version);
336
Damien Miller78928792000-04-12 20:17:38 +1000337 mismatch = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000338 switch(remote_major) {
339 case 1:
Damien Millereba71ba2000-04-29 23:57:08 +1000340 if (remote_minor == 99) {
341 if (options.protocol & SSH_PROTO_2)
342 enable_compat20();
343 else
344 mismatch = 1;
345 break;
346 }
Damien Miller78928792000-04-12 20:17:38 +1000347 if (!(options.protocol & SSH_PROTO_1)) {
348 mismatch = 1;
349 break;
350 }
Damien Millerb38eff82000-04-01 11:09:21 +1000351 if (remote_minor < 3) {
352 packet_disconnect("Your ssh version is too old and"
353 "is no longer supported. Please install a newer version.");
354 } else if (remote_minor == 3) {
355 /* note that this disables agent-forwarding */
356 enable_compat13();
357 }
Damien Miller78928792000-04-12 20:17:38 +1000358 break;
Damien Millerefb4afe2000-04-12 18:45:05 +1000359 case 2:
Damien Miller78928792000-04-12 20:17:38 +1000360 if (options.protocol & SSH_PROTO_2) {
Damien Millerefb4afe2000-04-12 18:45:05 +1000361 enable_compat20();
362 break;
363 }
364 /* FALLTHROUGH */
Damien Miller4af51302000-04-16 11:18:38 +1000365 default:
Damien Miller78928792000-04-12 20:17:38 +1000366 mismatch = 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000367 break;
368 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000369 chop(server_version_string);
370 chop(client_version_string);
Damien Miller78928792000-04-12 20:17:38 +1000371 debug("Local version string %.200s", server_version_string);
372
373 if (mismatch) {
374 s = "Protocol major versions differ.\n";
375 (void) atomicio(write, sock_out, s, strlen(s));
376 close(sock_in);
377 close(sock_out);
378 log("Protocol major versions differ for %s: %.200s vs. %.200s",
379 get_remote_ipaddr(),
380 server_version_string, client_version_string);
381 fatal_cleanup();
382 }
Damien Millereba71ba2000-04-29 23:57:08 +1000383 if (compat20)
384 packet_set_ssh2_format();
385}
386
387
388void
389destroy_sensitive_data(void)
390{
391 /* Destroy the private and public keys. They will no longer be needed. */
Damien Millerc4be7ce2000-05-17 23:02:03 +1000392 if (public_key)
393 RSA_free(public_key);
394 if (sensitive_data.private_key)
395 RSA_free(sensitive_data.private_key);
396 if (sensitive_data.host_key)
397 RSA_free(sensitive_data.host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000398 if (sensitive_data.dsa_host_key != NULL)
399 key_free(sensitive_data.dsa_host_key);
Damien Millerb38eff82000-04-01 11:09:21 +1000400}
401
Damien Miller95def091999-11-25 00:26:21 +1100402/*
403 * Main program for the daemon.
404 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405int
406main(int ac, char **av)
407{
Damien Miller95def091999-11-25 00:26:21 +1100408 extern char *optarg;
409 extern int optind;
Damien Miller166fca82000-04-20 07:42:21 +1000410 int opt, sock_in = 0, sock_out = 0, newsock, i, fdsetsz, on = 1;
411 pid_t pid;
Damien Miller34132e52000-01-14 15:45:46 +1100412 socklen_t fromlen;
Damien Millereba71ba2000-04-29 23:57:08 +1000413 int silent = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100414 fd_set *fdset;
415 struct sockaddr_storage from;
Damien Miller95def091999-11-25 00:26:21 +1100416 const char *remote_ip;
417 int remote_port;
Damien Miller95def091999-11-25 00:26:21 +1100418 FILE *f;
419 struct linger linger;
Damien Miller34132e52000-01-14 15:45:46 +1100420 struct addrinfo *ai;
421 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
422 int listen_sock, maxfd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423
Damien Miller95def091999-11-25 00:26:21 +1100424 /* Save argv[0]. */
425 saved_argv = av;
426 if (strchr(av[0], '/'))
427 av0 = strrchr(av[0], '/') + 1;
428 else
429 av0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430
Damien Miller95def091999-11-25 00:26:21 +1100431 /* Initialize configuration options to their default values. */
432 initialize_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433
Damien Miller95def091999-11-25 00:26:21 +1100434 /* Parse command-line arguments. */
Damien Miller78928792000-04-12 20:17:38 +1000435 while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:diqQ46")) != EOF) {
Damien Miller95def091999-11-25 00:26:21 +1100436 switch (opt) {
Damien Miller34132e52000-01-14 15:45:46 +1100437 case '4':
438 IPv4or6 = AF_INET;
439 break;
440 case '6':
441 IPv4or6 = AF_INET6;
442 break;
Damien Miller95def091999-11-25 00:26:21 +1100443 case 'f':
444 config_file_name = optarg;
445 break;
446 case 'd':
447 debug_flag = 1;
448 options.log_level = SYSLOG_LEVEL_DEBUG;
449 break;
450 case 'i':
451 inetd_flag = 1;
452 break;
453 case 'Q':
Damien Millereba71ba2000-04-29 23:57:08 +1000454 silent = 1;
Damien Miller95def091999-11-25 00:26:21 +1100455 break;
456 case 'q':
457 options.log_level = SYSLOG_LEVEL_QUIET;
458 break;
459 case 'b':
460 options.server_key_bits = atoi(optarg);
461 break;
462 case 'p':
Damien Miller34132e52000-01-14 15:45:46 +1100463 options.ports_from_cmdline = 1;
464 if (options.num_ports >= MAX_PORTS)
465 fatal("too many ports.\n");
466 options.ports[options.num_ports++] = atoi(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100467 break;
468 case 'g':
469 options.login_grace_time = atoi(optarg);
470 break;
471 case 'k':
472 options.key_regeneration_time = atoi(optarg);
473 break;
474 case 'h':
475 options.host_key_file = optarg;
476 break;
477 case 'V':
478 client_version_string = optarg;
479 /* only makes sense with inetd_flag, i.e. no listen() */
480 inetd_flag = 1;
481 break;
482 case '?':
483 default:
484 fprintf(stderr, "sshd version %s\n", SSH_VERSION);
485 fprintf(stderr, "Usage: %s [options]\n", av0);
486 fprintf(stderr, "Options:\n");
Damien Miller5428f641999-11-25 11:54:57 +1100487 fprintf(stderr, " -f file Configuration file (default %s)\n", SERVER_CONFIG_FILE);
Damien Miller95def091999-11-25 00:26:21 +1100488 fprintf(stderr, " -d Debugging mode\n");
489 fprintf(stderr, " -i Started from inetd\n");
490 fprintf(stderr, " -q Quiet (no logging)\n");
491 fprintf(stderr, " -p port Listen on the specified port (default: 22)\n");
492 fprintf(stderr, " -k seconds Regenerate server key every this many seconds (default: 3600)\n");
493 fprintf(stderr, " -g seconds Grace period for authentication (default: 300)\n");
494 fprintf(stderr, " -b bits Size of server RSA key (default: 768 bits)\n");
495 fprintf(stderr, " -h file File from which to read host key (default: %s)\n",
Damien Miller34132e52000-01-14 15:45:46 +1100496 HOST_KEY_FILE);
497 fprintf(stderr, " -4 Use IPv4 only\n");
498 fprintf(stderr, " -6 Use IPv6 only\n");
Damien Miller95def091999-11-25 00:26:21 +1100499 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502
Damien Miller34132e52000-01-14 15:45:46 +1100503 /*
504 * Force logging to stderr until we have loaded the private host
505 * key (unless started from inetd)
506 */
507 log_init(av0,
508 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
509 options.log_facility == -1 ? SYSLOG_FACILITY_AUTH : options.log_facility,
Damien Millereba71ba2000-04-29 23:57:08 +1000510 !silent && !inetd_flag);
Damien Miller34132e52000-01-14 15:45:46 +1100511
Damien Miller95def091999-11-25 00:26:21 +1100512 /* Read server configuration options from the configuration file. */
513 read_server_config(&options, config_file_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514
Damien Miller95def091999-11-25 00:26:21 +1100515 /* Fill in default values for those options not explicitly set. */
516 fill_default_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517
Damien Miller95def091999-11-25 00:26:21 +1100518 /* Check that there are no remaining arguments. */
519 if (optind < ac) {
520 fprintf(stderr, "Extra argument %s.\n", av[optind]);
521 exit(1);
522 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523
Damien Miller95def091999-11-25 00:26:21 +1100524 debug("sshd version %.100s", SSH_VERSION);
Damien Miller2ccf6611999-11-15 15:25:10 +1100525
Damien Millereba71ba2000-04-29 23:57:08 +1000526 sensitive_data.dsa_host_key = NULL;
527 sensitive_data.host_key = NULL;
528
529 /* check if RSA support exists */
530 if ((options.protocol & SSH_PROTO_1) &&
531 rsa_alive() == 0) {
532 log("no RSA support in libssl and libcrypto. See ssl(8)");
533 log("Disabling protocol version 1");
534 options.protocol &= ~SSH_PROTO_1;
535 }
536 /* Load the RSA/DSA host key. It must have empty passphrase. */
537 if (options.protocol & SSH_PROTO_1) {
538 Key k;
539 sensitive_data.host_key = RSA_new();
540 k.type = KEY_RSA;
541 k.rsa = sensitive_data.host_key;
542 errno = 0;
543 if (!load_private_key(options.host_key_file, "", &k, NULL)) {
544 error("Could not load host key: %.200s: %.100s",
545 options.host_key_file, strerror(errno));
546 log("Disabling protocol version 1");
547 options.protocol &= ~SSH_PROTO_1;
548 }
549 k.rsa = NULL;
550 }
551 if (options.protocol & SSH_PROTO_2) {
552 sensitive_data.dsa_host_key = key_new(KEY_DSA);
Damien Millere247cc42000-05-07 12:03:14 +1000553 if (!load_private_key(options.host_dsa_key_file, "", sensitive_data.dsa_host_key, NULL)) {
554
555 error("Could not load DSA host key: %.200s", options.host_dsa_key_file);
Damien Millereba71ba2000-04-29 23:57:08 +1000556 log("Disabling protocol version 2");
557 options.protocol &= ~SSH_PROTO_2;
558 }
559 }
560 if (! options.protocol & (SSH_PROTO_1|SSH_PROTO_2)) {
561 if (silent == 0)
562 fprintf(stderr, "sshd: no hostkeys available -- exiting.\n");
563 log("sshd: no hostkeys available -- exiting.\n");
Damien Miller95def091999-11-25 00:26:21 +1100564 exit(1);
565 }
Damien Miller95def091999-11-25 00:26:21 +1100566
Damien Millereba71ba2000-04-29 23:57:08 +1000567 /* Check certain values for sanity. */
568 if (options.protocol & SSH_PROTO_1) {
569 if (options.server_key_bits < 512 ||
570 options.server_key_bits > 32768) {
571 fprintf(stderr, "Bad server key size.\n");
572 exit(1);
573 }
574 /*
575 * Check that server and host key lengths differ sufficiently. This
576 * is necessary to make double encryption work with rsaref. Oh, I
577 * hate software patents. I dont know if this can go? Niels
578 */
579 if (options.server_key_bits >
580 BN_num_bits(sensitive_data.host_key->n) - SSH_KEY_BITS_RESERVED &&
581 options.server_key_bits <
582 BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) {
583 options.server_key_bits =
584 BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED;
585 debug("Forcing server key to %d bits to make it differ from host key.",
586 options.server_key_bits);
587 }
588 }
589
590 /* Initialize the log (it is reinitialized below in case we forked). */
Damien Miller95def091999-11-25 00:26:21 +1100591 if (debug_flag && !inetd_flag)
592 log_stderr = 1;
593 log_init(av0, options.log_level, options.log_facility, log_stderr);
594
Damien Millereba71ba2000-04-29 23:57:08 +1000595 /*
596 * If not in debugging mode, and not started from inetd, disconnect
597 * from the controlling terminal, and fork. The original process
598 * exits.
599 */
Damien Miller95def091999-11-25 00:26:21 +1100600 if (!debug_flag && !inetd_flag) {
601#ifdef TIOCNOTTY
602 int fd;
603#endif /* TIOCNOTTY */
604 if (daemon(0, 0) < 0)
605 fatal("daemon() failed: %.200s", strerror(errno));
606
607 /* Disconnect from the controlling tty. */
608#ifdef TIOCNOTTY
609 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
610 if (fd >= 0) {
611 (void) ioctl(fd, TIOCNOTTY, NULL);
612 close(fd);
613 }
614#endif /* TIOCNOTTY */
615 }
616 /* Reinitialize the log (because of the fork above). */
617 log_init(av0, options.log_level, options.log_facility, log_stderr);
618
Damien Miller95def091999-11-25 00:26:21 +1100619 /* Do not display messages to stdout in RSA code. */
620 rsa_set_verbose(0);
621
622 /* Initialize the random number generator. */
623 arc4random_stir();
624
625 /* Chdir to the root directory so that the current disk can be
626 unmounted if desired. */
627 chdir("/");
628
Damien Miller95def091999-11-25 00:26:21 +1100629 /* Start listening for a socket, unless started from inetd. */
630 if (inetd_flag) {
631 int s1, s2;
632 s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */
633 s2 = dup(s1);
634 sock_in = dup(0);
635 sock_out = dup(1);
Damien Millereba71ba2000-04-29 23:57:08 +1000636 /*
637 * We intentionally do not close the descriptors 0, 1, and 2
638 * as our code for setting the descriptors won\'t work if
639 * ttyfd happens to be one of those.
640 */
Damien Miller95def091999-11-25 00:26:21 +1100641 debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
642
Damien Millereba71ba2000-04-29 23:57:08 +1000643 if (options.protocol & SSH_PROTO_1) {
644 public_key = RSA_new();
645 sensitive_data.private_key = RSA_new();
646 log("Generating %d bit RSA key.", options.server_key_bits);
647 rsa_generate_key(sensitive_data.private_key, public_key,
648 options.server_key_bits);
649 arc4random_stir();
650 log("RSA key generation complete.");
651 }
Damien Miller95def091999-11-25 00:26:21 +1100652 } else {
Damien Miller34132e52000-01-14 15:45:46 +1100653 for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
654 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
655 continue;
656 if (num_listen_socks >= MAX_LISTEN_SOCKS)
657 fatal("Too many listen sockets. "
658 "Enlarge MAX_LISTEN_SOCKS");
659 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
660 ntop, sizeof(ntop), strport, sizeof(strport),
661 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
662 error("getnameinfo failed");
663 continue;
664 }
665 /* Create socket for listening. */
666 listen_sock = socket(ai->ai_family, SOCK_STREAM, 0);
667 if (listen_sock < 0) {
668 /* kernel may not support ipv6 */
669 verbose("socket: %.100s", strerror(errno));
670 continue;
671 }
672 if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) {
673 error("listen_sock O_NONBLOCK: %s", strerror(errno));
674 close(listen_sock);
675 continue;
676 }
677 /*
678 * Set socket options. We try to make the port
679 * reusable and have it close as fast as possible
680 * without waiting in unnecessary wait states on
681 * close.
682 */
683 setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
684 (void *) &on, sizeof(on));
685 linger.l_onoff = 1;
686 linger.l_linger = 5;
687 setsockopt(listen_sock, SOL_SOCKET, SO_LINGER,
688 (void *) &linger, sizeof(linger));
Damien Miller95def091999-11-25 00:26:21 +1100689
Damien Miller34132e52000-01-14 15:45:46 +1100690 debug("Bind to port %s on %s.", strport, ntop);
Damien Miller95def091999-11-25 00:26:21 +1100691
Damien Miller34132e52000-01-14 15:45:46 +1100692 /* Bind the socket to the desired port. */
Damien Miller3c7eeb22000-03-03 22:35:33 +1100693 if ((bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) &&
694 (!ai->ai_next)) {
Damien Miller34132e52000-01-14 15:45:46 +1100695 error("Bind to port %s on %s failed: %.200s.",
696 strport, ntop, strerror(errno));
697 close(listen_sock);
698 continue;
699 }
700 listen_socks[num_listen_socks] = listen_sock;
701 num_listen_socks++;
Damien Miller95def091999-11-25 00:26:21 +1100702
Damien Miller34132e52000-01-14 15:45:46 +1100703 /* Start listening on the port. */
704 log("Server listening on %s port %s.", ntop, strport);
705 if (listen(listen_sock, 5) < 0)
706 fatal("listen: %.100s", strerror(errno));
707
Damien Miller95def091999-11-25 00:26:21 +1100708 }
Damien Miller34132e52000-01-14 15:45:46 +1100709 freeaddrinfo(options.listen_addrs);
710
711 if (!num_listen_socks)
712 fatal("Cannot bind any address.");
713
Damien Miller95def091999-11-25 00:26:21 +1100714 if (!debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +1100715 /*
716 * Record our pid in /etc/sshd_pid to make it easier
717 * to kill the correct sshd. We don\'t want to do
718 * this before the bind above because the bind will
719 * fail if there already is a daemon, and this will
720 * overwrite any old pid in the file.
721 */
Damien Miller6f83b8e2000-05-02 09:23:45 +1000722 f = fopen(options.pid_file, "w");
Damien Miller95def091999-11-25 00:26:21 +1100723 if (f) {
724 fprintf(f, "%u\n", (unsigned int) getpid());
725 fclose(f);
726 }
727 }
Damien Millereba71ba2000-04-29 23:57:08 +1000728 if (options.protocol & SSH_PROTO_1) {
729 public_key = RSA_new();
730 sensitive_data.private_key = RSA_new();
Damien Miller95def091999-11-25 00:26:21 +1100731
Damien Millereba71ba2000-04-29 23:57:08 +1000732 log("Generating %d bit RSA key.", options.server_key_bits);
733 rsa_generate_key(sensitive_data.private_key, public_key,
734 options.server_key_bits);
735 arc4random_stir();
736 log("RSA key generation complete.");
Damien Miller95def091999-11-25 00:26:21 +1100737
Damien Millereba71ba2000-04-29 23:57:08 +1000738 /* Schedule server key regeneration alarm. */
739 signal(SIGALRM, key_regeneration_alarm);
740 alarm(options.key_regeneration_time);
741 }
Damien Miller95def091999-11-25 00:26:21 +1100742
743 /* Arrange to restart on SIGHUP. The handler needs listen_sock. */
744 signal(SIGHUP, sighup_handler);
745 signal(SIGTERM, sigterm_handler);
746 signal(SIGQUIT, sigterm_handler);
747
748 /* Arrange SIGCHLD to be caught. */
749 signal(SIGCHLD, main_sigchld_handler);
750
Damien Miller34132e52000-01-14 15:45:46 +1100751 /* setup fd set for listen */
752 maxfd = 0;
753 for (i = 0; i < num_listen_socks; i++)
754 if (listen_socks[i] > maxfd)
755 maxfd = listen_socks[i];
Damien Miller4af51302000-04-16 11:18:38 +1000756 fdsetsz = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
757 fdset = (fd_set *)xmalloc(fdsetsz);
Damien Miller34132e52000-01-14 15:45:46 +1100758
Damien Miller5428f641999-11-25 11:54:57 +1100759 /*
760 * Stay listening for connections until the system crashes or
761 * the daemon is killed with a signal.
762 */
Damien Miller95def091999-11-25 00:26:21 +1100763 for (;;) {
764 if (received_sighup)
765 sighup_restart();
Damien Miller34132e52000-01-14 15:45:46 +1100766 /* Wait in select until there is a connection. */
767 memset(fdset, 0, fdsetsz);
768 for (i = 0; i < num_listen_socks; i++)
769 FD_SET(listen_socks[i], fdset);
770 if (select(maxfd + 1, fdset, NULL, NULL, NULL) < 0) {
771 if (errno != EINTR)
772 error("select: %.100s", strerror(errno));
Damien Miller50945fa1999-12-09 10:31:37 +1100773 continue;
Damien Miller34132e52000-01-14 15:45:46 +1100774 }
775 for (i = 0; i < num_listen_socks; i++) {
776 if (!FD_ISSET(listen_socks[i], fdset))
Damien Miller95def091999-11-25 00:26:21 +1100777 continue;
Damien Miller34132e52000-01-14 15:45:46 +1100778 fromlen = sizeof(from);
779 newsock = accept(listen_socks[i], (struct sockaddr *)&from,
780 &fromlen);
781 if (newsock < 0) {
782 if (errno != EINTR && errno != EWOULDBLOCK)
783 error("accept: %.100s", strerror(errno));
784 continue;
785 }
786 if (fcntl(newsock, F_SETFL, 0) < 0) {
787 error("newsock del O_NONBLOCK: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100788 continue;
789 }
Damien Miller5428f641999-11-25 11:54:57 +1100790 /*
791 * Got connection. Fork a child to handle it, unless
792 * we are in debugging mode.
793 */
Damien Miller95def091999-11-25 00:26:21 +1100794 if (debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +1100795 /*
796 * In debugging mode. Close the listening
797 * socket, and start processing the
798 * connection without forking.
799 */
Damien Miller95def091999-11-25 00:26:21 +1100800 debug("Server will not fork when running in debugging mode.");
Damien Miller34132e52000-01-14 15:45:46 +1100801 close_listen_socks();
Damien Miller95def091999-11-25 00:26:21 +1100802 sock_in = newsock;
803 sock_out = newsock;
804 pid = getpid();
805 break;
806 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100807 /*
808 * Normal production daemon. Fork, and have
809 * the child process the connection. The
810 * parent continues listening.
811 */
Damien Miller95def091999-11-25 00:26:21 +1100812 if ((pid = fork()) == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100813 /*
814 * Child. Close the listening socket, and start using the
815 * accepted socket. Reinitialize logging (since our pid has
816 * changed). We break out of the loop to handle the connection.
817 */
Damien Miller34132e52000-01-14 15:45:46 +1100818 close_listen_socks();
Damien Miller95def091999-11-25 00:26:21 +1100819 sock_in = newsock;
820 sock_out = newsock;
821 log_init(av0, options.log_level, options.log_facility, log_stderr);
822 break;
823 }
824 }
825
826 /* Parent. Stay in the loop. */
827 if (pid < 0)
828 error("fork: %.100s", strerror(errno));
829 else
830 debug("Forked child %d.", pid);
831
832 /* Mark that the key has been used (it was "given" to the child). */
833 key_used = 1;
834
835 arc4random_stir();
836
837 /* Close the new socket (the child is now taking care of it). */
838 close(newsock);
Damien Miller34132e52000-01-14 15:45:46 +1100839 } /* for (i = 0; i < num_listen_socks; i++) */
840 /* child process check (or debug mode) */
841 if (num_listen_socks < 0)
842 break;
Damien Miller95def091999-11-25 00:26:21 +1100843 }
844 }
845
846 /* This is the child processing a new connection. */
847
Damien Miller5428f641999-11-25 11:54:57 +1100848 /*
849 * Disable the key regeneration alarm. We will not regenerate the
850 * key since we are no longer in a position to give it to anyone. We
851 * will not restart on SIGHUP since it no longer makes sense.
852 */
Damien Miller95def091999-11-25 00:26:21 +1100853 alarm(0);
854 signal(SIGALRM, SIG_DFL);
855 signal(SIGHUP, SIG_DFL);
856 signal(SIGTERM, SIG_DFL);
857 signal(SIGQUIT, SIG_DFL);
858 signal(SIGCHLD, SIG_DFL);
859
Damien Miller5428f641999-11-25 11:54:57 +1100860 /*
861 * Set socket options for the connection. We want the socket to
862 * close as fast as possible without waiting for anything. If the
863 * connection is not a socket, these will do nothing.
864 */
865 /* setsockopt(sock_in, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */
Damien Miller95def091999-11-25 00:26:21 +1100866 linger.l_onoff = 1;
867 linger.l_linger = 5;
868 setsockopt(sock_in, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger));
869
Damien Miller5428f641999-11-25 11:54:57 +1100870 /*
871 * Register our connection. This turns encryption off because we do
872 * not have a key.
873 */
Damien Miller95def091999-11-25 00:26:21 +1100874 packet_set_connection(sock_in, sock_out);
875
876 remote_port = get_remote_port();
877 remote_ip = get_remote_ipaddr();
878
879 /* Check whether logins are denied from this host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880#ifdef LIBWRAP
Damien Miller34132e52000-01-14 15:45:46 +1100881 /* XXX LIBWRAP noes not know about IPv6 */
Damien Miller95def091999-11-25 00:26:21 +1100882 {
883 struct request_info req;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884
Damien Miller95def091999-11-25 00:26:21 +1100885 request_init(&req, RQ_DAEMON, av0, RQ_FILE, sock_in, NULL);
886 fromhost(&req);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887
Damien Miller95def091999-11-25 00:26:21 +1100888 if (!hosts_access(&req)) {
889 close(sock_in);
890 close(sock_out);
891 refuse(&req);
892 }
Damien Miller34132e52000-01-14 15:45:46 +1100893/*XXX IPv6 verbose("Connection from %.500s port %d", eval_client(&req), remote_port); */
Damien Miller95def091999-11-25 00:26:21 +1100894 }
Damien Miller34132e52000-01-14 15:45:46 +1100895#endif /* LIBWRAP */
Damien Miller95def091999-11-25 00:26:21 +1100896 /* Log the connection. */
897 verbose("Connection from %.500s port %d", remote_ip, remote_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000898
Damien Miller5428f641999-11-25 11:54:57 +1100899 /*
900 * We don\'t want to listen forever unless the other side
901 * successfully authenticates itself. So we set up an alarm which is
902 * cleared after successful authentication. A limit of zero
903 * indicates no limit. Note that we don\'t set the alarm in debugging
904 * mode; it is just annoying to have the server exit just when you
905 * are about to discover the bug.
906 */
Damien Miller95def091999-11-25 00:26:21 +1100907 signal(SIGALRM, grace_alarm_handler);
908 if (!debug_flag)
909 alarm(options.login_grace_time);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000910
Damien Millerb38eff82000-04-01 11:09:21 +1000911 sshd_exchange_identification(sock_in, sock_out);
Damien Miller5428f641999-11-25 11:54:57 +1100912 /*
913 * Check that the connection comes from a privileged port. Rhosts-
914 * and Rhosts-RSA-Authentication only make sense from priviledged
915 * programs. Of course, if the intruder has root access on his local
916 * machine, he can connect from any port. So do not use these
917 * authentication methods from machines that you do not trust.
918 */
Damien Miller95def091999-11-25 00:26:21 +1100919 if (remote_port >= IPPORT_RESERVED ||
920 remote_port < IPPORT_RESERVED / 2) {
921 options.rhosts_authentication = 0;
922 options.rhosts_rsa_authentication = 0;
923 }
Damien Miller34132e52000-01-14 15:45:46 +1100924#ifdef KRB4
925 if (!packet_connection_is_ipv4() &&
926 options.kerberos_authentication) {
927 debug("Kerberos Authentication disabled, only available for IPv4.");
928 options.kerberos_authentication = 0;
929 }
930#endif /* KRB4 */
931
Damien Miller95def091999-11-25 00:26:21 +1100932 packet_set_nonblocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933
Damien Miller396691a2000-01-20 22:44:08 +1100934 /* perform the key exchange */
Damien Miller396691a2000-01-20 22:44:08 +1100935 /* authenticate user and start session */
Damien Millerefb4afe2000-04-12 18:45:05 +1000936 if (compat20) {
937 do_ssh2_kex();
938 do_authentication2();
939 } else {
940 do_ssh1_kex();
941 do_authentication();
942 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000943
944#ifdef KRB4
Damien Miller95def091999-11-25 00:26:21 +1100945 /* Cleanup user's ticket cache file. */
946 if (options.kerberos_ticket_cleanup)
947 (void) dest_tkt();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948#endif /* KRB4 */
949
Damien Miller95def091999-11-25 00:26:21 +1100950 /* The connection has been terminated. */
951 verbose("Closing connection to %.100s", remote_ip);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952
Damien Millerbeb4ba51999-12-28 15:09:35 +1100953#ifdef USE_PAM
Damien Millere72b7af1999-12-30 15:08:44 +1100954 finish_pam();
Damien Millerbeb4ba51999-12-28 15:09:35 +1100955#endif /* USE_PAM */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956
Damien Miller95def091999-11-25 00:26:21 +1100957 packet_close();
958 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000959}
960
Damien Miller95def091999-11-25 00:26:21 +1100961/*
Damien Miller396691a2000-01-20 22:44:08 +1100962 * SSH1 key exchange
Damien Miller95def091999-11-25 00:26:21 +1100963 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100964void
Damien Millerb38eff82000-04-01 11:09:21 +1000965do_ssh1_kex()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000966{
Damien Miller95def091999-11-25 00:26:21 +1100967 int i, len;
Damien Miller396691a2000-01-20 22:44:08 +1100968 int plen, slen;
Damien Miller95def091999-11-25 00:26:21 +1100969 BIGNUM *session_key_int;
970 unsigned char session_key[SSH_SESSION_KEY_LENGTH];
Damien Miller396691a2000-01-20 22:44:08 +1100971 unsigned char cookie[8];
Damien Miller95def091999-11-25 00:26:21 +1100972 unsigned int cipher_type, auth_mask, protocol_flags;
Damien Miller95def091999-11-25 00:26:21 +1100973 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974
Damien Miller5428f641999-11-25 11:54:57 +1100975 /*
976 * Generate check bytes that the client must send back in the user
977 * packet in order for it to be accepted; this is used to defy ip
978 * spoofing attacks. Note that this only works against somebody
979 * doing IP spoofing from a remote machine; any machine on the local
980 * network can still see outgoing packets and catch the random
981 * cookie. This only affects rhosts authentication, and this is one
982 * of the reasons why it is inherently insecure.
983 */
Damien Miller95def091999-11-25 00:26:21 +1100984 for (i = 0; i < 8; i++) {
985 if (i % 4 == 0)
986 rand = arc4random();
Damien Miller396691a2000-01-20 22:44:08 +1100987 cookie[i] = rand & 0xff;
Damien Miller95def091999-11-25 00:26:21 +1100988 rand >>= 8;
989 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990
Damien Miller5428f641999-11-25 11:54:57 +1100991 /*
992 * Send our public key. We include in the packet 64 bits of random
993 * data that must be matched in the reply in order to prevent IP
994 * spoofing.
995 */
Damien Miller95def091999-11-25 00:26:21 +1100996 packet_start(SSH_SMSG_PUBLIC_KEY);
997 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +1100998 packet_put_char(cookie[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000999
Damien Miller95def091999-11-25 00:26:21 +11001000 /* Store our public server RSA key. */
1001 packet_put_int(BN_num_bits(public_key->n));
1002 packet_put_bignum(public_key->e);
1003 packet_put_bignum(public_key->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004
Damien Miller95def091999-11-25 00:26:21 +11001005 /* Store our public host RSA key. */
1006 packet_put_int(BN_num_bits(sensitive_data.host_key->n));
1007 packet_put_bignum(sensitive_data.host_key->e);
1008 packet_put_bignum(sensitive_data.host_key->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001009
Damien Miller95def091999-11-25 00:26:21 +11001010 /* Put protocol flags. */
1011 packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012
Damien Miller95def091999-11-25 00:26:21 +11001013 /* Declare which ciphers we support. */
Damien Miller1383bd82000-04-06 12:32:37 +10001014 packet_put_int(cipher_mask1());
Damien Miller95def091999-11-25 00:26:21 +11001015
1016 /* Declare supported authentication types. */
1017 auth_mask = 0;
1018 if (options.rhosts_authentication)
1019 auth_mask |= 1 << SSH_AUTH_RHOSTS;
1020 if (options.rhosts_rsa_authentication)
1021 auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
1022 if (options.rsa_authentication)
1023 auth_mask |= 1 << SSH_AUTH_RSA;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001024#ifdef KRB4
Damien Miller95def091999-11-25 00:26:21 +11001025 if (options.kerberos_authentication)
1026 auth_mask |= 1 << SSH_AUTH_KERBEROS;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001027#endif
1028#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +11001029 if (options.kerberos_tgt_passing)
1030 auth_mask |= 1 << SSH_PASS_KERBEROS_TGT;
1031 if (options.afs_token_passing)
1032 auth_mask |= 1 << SSH_PASS_AFS_TOKEN;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001033#endif
Damien Miller95def091999-11-25 00:26:21 +11001034#ifdef SKEY
1035 if (options.skey_authentication == 1)
1036 auth_mask |= 1 << SSH_AUTH_TIS;
1037#endif
1038 if (options.password_authentication)
1039 auth_mask |= 1 << SSH_AUTH_PASSWORD;
1040 packet_put_int(auth_mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041
Damien Miller95def091999-11-25 00:26:21 +11001042 /* Send the packet and wait for it to be sent. */
1043 packet_send();
1044 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001045
Damien Miller95def091999-11-25 00:26:21 +11001046 debug("Sent %d bit public key and %d bit host key.",
1047 BN_num_bits(public_key->n), BN_num_bits(sensitive_data.host_key->n));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001048
Damien Miller95def091999-11-25 00:26:21 +11001049 /* Read clients reply (cipher type and session key). */
1050 packet_read_expect(&plen, SSH_CMSG_SESSION_KEY);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001051
Damien Miller50945fa1999-12-09 10:31:37 +11001052 /* Get cipher type and check whether we accept this. */
Damien Miller95def091999-11-25 00:26:21 +11001053 cipher_type = packet_get_char();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001054
Damien Miller4af51302000-04-16 11:18:38 +10001055 if (!(cipher_mask() & (1 << cipher_type)))
Damien Miller50945fa1999-12-09 10:31:37 +11001056 packet_disconnect("Warning: client selects unsupported cipher.");
1057
Damien Miller95def091999-11-25 00:26:21 +11001058 /* Get check bytes from the packet. These must match those we
1059 sent earlier with the public key packet. */
1060 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001061 if (cookie[i] != packet_get_char())
Damien Miller95def091999-11-25 00:26:21 +11001062 packet_disconnect("IP Spoofing check bytes do not match.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001063
Damien Miller95def091999-11-25 00:26:21 +11001064 debug("Encryption type: %.200s", cipher_name(cipher_type));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001065
Damien Miller95def091999-11-25 00:26:21 +11001066 /* Get the encrypted integer. */
1067 session_key_int = BN_new();
1068 packet_get_bignum(session_key_int, &slen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001069
Damien Miller95def091999-11-25 00:26:21 +11001070 protocol_flags = packet_get_int();
1071 packet_set_protocol_flags(protocol_flags);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072
Damien Miller95def091999-11-25 00:26:21 +11001073 packet_integrity_check(plen, 1 + 8 + slen + 4, SSH_CMSG_SESSION_KEY);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001074
Damien Miller5428f641999-11-25 11:54:57 +11001075 /*
1076 * Decrypt it using our private server key and private host key (key
1077 * with larger modulus first).
1078 */
Damien Miller95def091999-11-25 00:26:21 +11001079 if (BN_cmp(sensitive_data.private_key->n, sensitive_data.host_key->n) > 0) {
1080 /* Private key has bigger modulus. */
1081 if (BN_num_bits(sensitive_data.private_key->n) <
1082 BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) {
1083 fatal("do_connection: %s: private_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
1084 get_remote_ipaddr(),
1085 BN_num_bits(sensitive_data.private_key->n),
1086 BN_num_bits(sensitive_data.host_key->n),
1087 SSH_KEY_BITS_RESERVED);
1088 }
1089 rsa_private_decrypt(session_key_int, session_key_int,
1090 sensitive_data.private_key);
1091 rsa_private_decrypt(session_key_int, session_key_int,
1092 sensitive_data.host_key);
1093 } else {
1094 /* Host key has bigger modulus (or they are equal). */
1095 if (BN_num_bits(sensitive_data.host_key->n) <
1096 BN_num_bits(sensitive_data.private_key->n) + SSH_KEY_BITS_RESERVED) {
1097 fatal("do_connection: %s: host_key %d < private_key %d + SSH_KEY_BITS_RESERVED %d",
1098 get_remote_ipaddr(),
1099 BN_num_bits(sensitive_data.host_key->n),
1100 BN_num_bits(sensitive_data.private_key->n),
1101 SSH_KEY_BITS_RESERVED);
1102 }
1103 rsa_private_decrypt(session_key_int, session_key_int,
1104 sensitive_data.host_key);
1105 rsa_private_decrypt(session_key_int, session_key_int,
1106 sensitive_data.private_key);
1107 }
Damien Miller356a0b01999-11-08 15:30:59 +11001108
Damien Miller396691a2000-01-20 22:44:08 +11001109 compute_session_id(session_id, cookie,
Damien Miller95def091999-11-25 00:26:21 +11001110 sensitive_data.host_key->n,
1111 sensitive_data.private_key->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001112
Damien Miller396691a2000-01-20 22:44:08 +11001113 /* Destroy the private and public keys. They will no longer be needed. */
Damien Millereba71ba2000-04-29 23:57:08 +10001114 destroy_sensitive_data();
Damien Miller396691a2000-01-20 22:44:08 +11001115
Damien Miller5428f641999-11-25 11:54:57 +11001116 /*
1117 * Extract session key from the decrypted integer. The key is in the
1118 * least significant 256 bits of the integer; the first byte of the
1119 * key is in the highest bits.
1120 */
Damien Miller95def091999-11-25 00:26:21 +11001121 BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1122 len = BN_num_bytes(session_key_int);
1123 if (len < 0 || len > sizeof(session_key))
1124 fatal("do_connection: bad len from %s: session_key_int %d > sizeof(session_key) %d",
1125 get_remote_ipaddr(),
1126 len, sizeof(session_key));
1127 memset(session_key, 0, sizeof(session_key));
1128 BN_bn2bin(session_key_int, session_key + sizeof(session_key) - len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129
Damien Miller396691a2000-01-20 22:44:08 +11001130 /* Destroy the decrypted integer. It is no longer needed. */
1131 BN_clear_free(session_key_int);
1132
Damien Miller95def091999-11-25 00:26:21 +11001133 /* Xor the first 16 bytes of the session key with the session id. */
1134 for (i = 0; i < 16; i++)
1135 session_key[i] ^= session_id[i];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136
Damien Miller95def091999-11-25 00:26:21 +11001137 /* Set the session key. From this on all communications will be encrypted. */
1138 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001139
Damien Miller95def091999-11-25 00:26:21 +11001140 /* Destroy our copy of the session key. It is no longer needed. */
1141 memset(session_key, 0, sizeof(session_key));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001142
Damien Miller95def091999-11-25 00:26:21 +11001143 debug("Received session key; encryption turned on.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144
Damien Miller95def091999-11-25 00:26:21 +11001145 /* Send an acknowledgement packet. Note that this packet is sent encrypted. */
1146 packet_start(SSH_SMSG_SUCCESS);
1147 packet_send();
1148 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001149}
Damien Millerefb4afe2000-04-12 18:45:05 +10001150
1151/*
1152 * SSH2 key exchange: diffie-hellman-group1-sha1
1153 */
1154void
1155do_ssh2_kex()
1156{
1157 Buffer *server_kexinit;
1158 Buffer *client_kexinit;
1159 int payload_len, dlen;
1160 int slen;
1161 unsigned int klen, kout;
Damien Millerefb4afe2000-04-12 18:45:05 +10001162 unsigned char *signature = NULL;
1163 unsigned char *server_host_key_blob = NULL;
1164 unsigned int sbloblen;
1165 DH *dh;
1166 BIGNUM *dh_client_pub = 0;
1167 BIGNUM *shared_secret = 0;
1168 int i;
1169 unsigned char *kbuf;
1170 unsigned char *hash;
1171 Kex *kex;
Damien Millerefb4afe2000-04-12 18:45:05 +10001172 char *cprop[PROPOSAL_MAX];
Damien Millerefb4afe2000-04-12 18:45:05 +10001173
1174/* KEXINIT */
1175
Damien Miller78928792000-04-12 20:17:38 +10001176 if (options.ciphers != NULL) {
Damien Miller4af51302000-04-16 11:18:38 +10001177 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Damien Miller78928792000-04-12 20:17:38 +10001178 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
1179 }
Damien Millerb1715dc2000-05-30 13:44:51 +10001180 server_kexinit = kex_init(myproposal);
Damien Millerefb4afe2000-04-12 18:45:05 +10001181 client_kexinit = xmalloc(sizeof(*client_kexinit));
1182 buffer_init(client_kexinit);
Damien Millerefb4afe2000-04-12 18:45:05 +10001183
Damien Millerb1715dc2000-05-30 13:44:51 +10001184 /* algorithm negotiation */
1185 kex_exchange_kexinit(server_kexinit, client_kexinit, cprop);
1186 kex = kex_choose_conf(cprop, myproposal, 1);
1187 for (i = 0; i < PROPOSAL_MAX; i++)
1188 xfree(cprop[i]);
Damien Millerefb4afe2000-04-12 18:45:05 +10001189
1190/* KEXDH */
1191
1192 debug("Wait SSH2_MSG_KEXDH_INIT.");
1193 packet_read_expect(&payload_len, SSH2_MSG_KEXDH_INIT);
1194
1195 /* key, cert */
1196 dh_client_pub = BN_new();
1197 if (dh_client_pub == NULL)
1198 fatal("dh_client_pub == NULL");
1199 packet_get_bignum2(dh_client_pub, &dlen);
1200
1201#ifdef DEBUG_KEXDH
1202 fprintf(stderr, "\ndh_client_pub= ");
1203 bignum_print(dh_client_pub);
1204 fprintf(stderr, "\n");
1205 debug("bits %d", BN_num_bits(dh_client_pub));
1206#endif
1207
1208 /* generate DH key */
Damien Miller78928792000-04-12 20:17:38 +10001209 dh = dh_new_group1(); /* XXX depends on 'kex' */
Damien Millerefb4afe2000-04-12 18:45:05 +10001210
1211#ifdef DEBUG_KEXDH
1212 fprintf(stderr, "\np= ");
1213 bignum_print(dh->p);
1214 fprintf(stderr, "\ng= ");
1215 bignum_print(dh->g);
1216 fprintf(stderr, "\npub= ");
1217 bignum_print(dh->pub_key);
1218 fprintf(stderr, "\n");
1219#endif
Damien Miller78928792000-04-12 20:17:38 +10001220 if (!dh_pub_is_valid(dh, dh_client_pub))
1221 packet_disconnect("bad client public DH value");
Damien Millerefb4afe2000-04-12 18:45:05 +10001222
1223 klen = DH_size(dh);
1224 kbuf = xmalloc(klen);
1225 kout = DH_compute_key(kbuf, dh_client_pub, dh);
1226
1227#ifdef DEBUG_KEXDH
1228 debug("shared secret: len %d/%d", klen, kout);
1229 fprintf(stderr, "shared secret == ");
1230 for (i = 0; i< kout; i++)
1231 fprintf(stderr, "%02x", (kbuf[i])&0xff);
1232 fprintf(stderr, "\n");
1233#endif
1234 shared_secret = BN_new();
1235
1236 BN_bin2bn(kbuf, kout, shared_secret);
1237 memset(kbuf, 0, klen);
1238 xfree(kbuf);
1239
Damien Millereba71ba2000-04-29 23:57:08 +10001240 /* XXX precompute? */
1241 dsa_make_key_blob(sensitive_data.dsa_host_key, &server_host_key_blob, &sbloblen);
Damien Millerefb4afe2000-04-12 18:45:05 +10001242
1243 /* calc H */ /* XXX depends on 'kex' */
1244 hash = kex_hash(
1245 client_version_string,
1246 server_version_string,
1247 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
1248 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
1249 (char *)server_host_key_blob, sbloblen,
1250 dh_client_pub,
1251 dh->pub_key,
1252 shared_secret
1253 );
1254 buffer_free(client_kexinit);
1255 buffer_free(server_kexinit);
1256 xfree(client_kexinit);
1257 xfree(server_kexinit);
1258#ifdef DEBUG_KEXDH
Damien Miller4af51302000-04-16 11:18:38 +10001259 fprintf(stderr, "hash == ");
1260 for (i = 0; i< 20; i++)
1261 fprintf(stderr, "%02x", (hash[i])&0xff);
1262 fprintf(stderr, "\n");
Damien Millerefb4afe2000-04-12 18:45:05 +10001263#endif
Damien Millereba71ba2000-04-29 23:57:08 +10001264 /* save session id := H */
1265 /* XXX hashlen depends on KEX */
1266 session_id2_len = 20;
1267 session_id2 = xmalloc(session_id2_len);
1268 memcpy(session_id2, hash, session_id2_len);
1269
Damien Millerefb4afe2000-04-12 18:45:05 +10001270 /* sign H */
Damien Millereba71ba2000-04-29 23:57:08 +10001271 /* XXX hashlen depends on KEX */
1272 dsa_sign(sensitive_data.dsa_host_key, &signature, &slen, hash, 20);
1273
1274 destroy_sensitive_data();
Damien Millerefb4afe2000-04-12 18:45:05 +10001275
1276 /* send server hostkey, DH pubkey 'f' and singed H */
1277 packet_start(SSH2_MSG_KEXDH_REPLY);
1278 packet_put_string((char *)server_host_key_blob, sbloblen);
Damien Millere247cc42000-05-07 12:03:14 +10001279 packet_put_bignum2(dh->pub_key); /* f */
Damien Millerefb4afe2000-04-12 18:45:05 +10001280 packet_put_string((char *)signature, slen);
1281 packet_send();
Damien Miller8bb73be2000-04-19 16:26:12 +10001282 xfree(signature);
Damien Millereba71ba2000-04-29 23:57:08 +10001283 xfree(server_host_key_blob);
Damien Millerefb4afe2000-04-12 18:45:05 +10001284 packet_write_wait();
1285
1286 kex_derive_keys(kex, hash, shared_secret);
1287 packet_set_kex(kex);
1288
1289 /* have keys, free DH */
1290 DH_free(dh);
1291
1292 debug("send SSH2_MSG_NEWKEYS.");
1293 packet_start(SSH2_MSG_NEWKEYS);
1294 packet_send();
1295 packet_write_wait();
1296 debug("done: send SSH2_MSG_NEWKEYS.");
1297
1298 debug("Wait SSH2_MSG_NEWKEYS.");
1299 packet_read_expect(&payload_len, SSH2_MSG_NEWKEYS);
1300 debug("GOT SSH2_MSG_NEWKEYS.");
1301
Damien Miller78928792000-04-12 20:17:38 +10001302#ifdef DEBUG_KEXDH
Damien Millerefb4afe2000-04-12 18:45:05 +10001303 /* send 1st encrypted/maced/compressed message */
1304 packet_start(SSH2_MSG_IGNORE);
1305 packet_put_cstring("markus");
1306 packet_send();
1307 packet_write_wait();
Damien Miller78928792000-04-12 20:17:38 +10001308#endif
Damien Millerefb4afe2000-04-12 18:45:05 +10001309 debug("done: KEX2.");
1310}