blob: 93d68404ff5c0bac71a3589cccdc43d013fa9b87 [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 Millerbf7f4662000-06-23 10:16:38 +100017RCSID("$OpenBSD: sshd.c,v 1.119 2000/06/22 16:32:27 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;
Damien Millerb8c656e2000-06-28 15:22:41 +100091int saved_argc;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
Damien Miller5428f641999-11-25 11:54:57 +110093/*
Damien Miller34132e52000-01-14 15:45:46 +110094 * The sockets that the server is listening; this is used in the SIGHUP
95 * signal handler.
Damien Miller5428f641999-11-25 11:54:57 +110096 */
Damien Miller34132e52000-01-14 15:45:46 +110097#define MAX_LISTEN_SOCKS 16
98int listen_socks[MAX_LISTEN_SOCKS];
99int num_listen_socks = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
Damien Miller5428f641999-11-25 11:54:57 +1100101/*
102 * the client's version string, passed by sshd2 in compat mode. if != NULL,
103 * sshd will skip the version-number exchange
104 */
Damien Miller95def091999-11-25 00:26:21 +1100105char *client_version_string = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000106char *server_version_string = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Miller5428f641999-11-25 11:54:57 +1100108/*
109 * Any really sensitive data in the application is contained in this
110 * structure. The idea is that this structure could be locked into memory so
111 * that the pages do not get written into swap. However, there are some
112 * problems. The private key contains BIGNUMs, and we do not (in principle)
113 * have access to the internals of them, and locking just the structure is
114 * not very useful. Currently, memory locking is not implemented.
115 */
Damien Miller95def091999-11-25 00:26:21 +1100116struct {
Damien Millereba71ba2000-04-29 23:57:08 +1000117 RSA *private_key; /* Private part of empheral server key. */
Damien Miller95def091999-11-25 00:26:21 +1100118 RSA *host_key; /* Private part of host key. */
Damien Millereba71ba2000-04-29 23:57:08 +1000119 Key *dsa_host_key; /* Private DSA host key. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120} sensitive_data;
121
Damien Miller5428f641999-11-25 11:54:57 +1100122/*
123 * Flag indicating whether the current session key has been used. This flag
124 * is set whenever the key is used, and cleared when the key is regenerated.
125 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126int key_used = 0;
127
128/* This is set to true when SIGHUP is received. */
129int received_sighup = 0;
130
131/* Public side of the server key. This value is regenerated regularly with
132 the private key. */
133RSA *public_key;
134
Damien Millerb38eff82000-04-01 11:09:21 +1000135/* session identifier, used by RSA-auth */
136unsigned char session_id[16];
137
Damien Millereba71ba2000-04-29 23:57:08 +1000138/* same for ssh2 */
139unsigned char *session_id2 = NULL;
140int session_id2_len = 0;
141
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142/* Prototypes for various functions defined later in this file. */
Damien Millerb38eff82000-04-01 11:09:21 +1000143void do_ssh1_kex();
Damien Millerefb4afe2000-04-12 18:45:05 +1000144void do_ssh2_kex();
Damien Miller98c7ad62000-03-09 21:27:49 +1100145
146/*
Damien Miller34132e52000-01-14 15:45:46 +1100147 * Close all listening sockets
148 */
149void
150close_listen_socks(void)
151{
152 int i;
153 for (i = 0; i < num_listen_socks; i++)
154 close(listen_socks[i]);
155 num_listen_socks = -1;
156}
157
158/*
Damien Miller95def091999-11-25 00:26:21 +1100159 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
160 * the effect is to reread the configuration file (and to regenerate
161 * the server key).
162 */
Damien Miller4af51302000-04-16 11:18:38 +1000163void
Damien Miller95def091999-11-25 00:26:21 +1100164sighup_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165{
Damien Miller95def091999-11-25 00:26:21 +1100166 received_sighup = 1;
167 signal(SIGHUP, sighup_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168}
169
Damien Miller95def091999-11-25 00:26:21 +1100170/*
171 * Called from the main program after receiving SIGHUP.
172 * Restarts the server.
173 */
Damien Miller4af51302000-04-16 11:18:38 +1000174void
Damien Miller95def091999-11-25 00:26:21 +1100175sighup_restart()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176{
Damien Miller95def091999-11-25 00:26:21 +1100177 log("Received SIGHUP; restarting.");
Damien Miller34132e52000-01-14 15:45:46 +1100178 close_listen_socks();
Damien Miller95def091999-11-25 00:26:21 +1100179 execv(saved_argv[0], saved_argv);
180 log("RESTART FAILED: av0='%s', error: %s.", av0, strerror(errno));
181 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182}
183
Damien Miller95def091999-11-25 00:26:21 +1100184/*
185 * Generic signal handler for terminating signals in the master daemon.
186 * These close the listen socket; not closing it seems to cause "Address
187 * already in use" problems on some machines, which is inconvenient.
188 */
Damien Miller4af51302000-04-16 11:18:38 +1000189void
Damien Miller95def091999-11-25 00:26:21 +1100190sigterm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191{
Damien Miller95def091999-11-25 00:26:21 +1100192 log("Received signal %d; terminating.", sig);
Damien Miller34132e52000-01-14 15:45:46 +1100193 close_listen_socks();
Damien Miller6f83b8e2000-05-02 09:23:45 +1000194 unlink(options.pid_file);
Damien Miller95def091999-11-25 00:26:21 +1100195 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196}
197
Damien Miller95def091999-11-25 00:26:21 +1100198/*
199 * SIGCHLD handler. This is called whenever a child dies. This will then
200 * reap any zombies left by exited c.
201 */
Damien Miller4af51302000-04-16 11:18:38 +1000202void
Damien Miller95def091999-11-25 00:26:21 +1100203main_sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204{
Damien Miller95def091999-11-25 00:26:21 +1100205 int save_errno = errno;
206 int status;
Damien Miller431f66b1999-11-21 18:31:57 +1100207
Damien Miller95def091999-11-25 00:26:21 +1100208 while (waitpid(-1, &status, WNOHANG) > 0)
209 ;
Damien Miller431f66b1999-11-21 18:31:57 +1100210
Damien Miller95def091999-11-25 00:26:21 +1100211 signal(SIGCHLD, main_sigchld_handler);
212 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213}
214
Damien Miller95def091999-11-25 00:26:21 +1100215/*
216 * Signal handler for the alarm after the login grace period has expired.
217 */
Damien Miller4af51302000-04-16 11:18:38 +1000218void
Damien Miller95def091999-11-25 00:26:21 +1100219grace_alarm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220{
Damien Miller95def091999-11-25 00:26:21 +1100221 /* Close the connection. */
222 packet_close();
223
224 /* Log error and exit. */
225 fatal("Timeout before authentication for %s.", get_remote_ipaddr());
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226}
227
Damien Miller95def091999-11-25 00:26:21 +1100228/*
Damien Miller95def091999-11-25 00:26:21 +1100229 * Signal handler for the key regeneration alarm. Note that this
230 * alarm only occurs in the daemon waiting for connections, and it does not
231 * do anything with the private key or random state before forking.
232 * Thus there should be no concurrency control/asynchronous execution
233 * problems.
234 */
Damien Millereba71ba2000-04-29 23:57:08 +1000235/* XXX do we really want this work to be done in a signal handler ? -m */
Damien Miller4af51302000-04-16 11:18:38 +1000236void
Damien Miller95def091999-11-25 00:26:21 +1100237key_regeneration_alarm(int sig)
238{
239 int save_errno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240
Damien Miller95def091999-11-25 00:26:21 +1100241 /* Check if we should generate a new key. */
242 if (key_used) {
243 /* This should really be done in the background. */
244 log("Generating new %d bit RSA key.", options.server_key_bits);
245
246 if (sensitive_data.private_key != NULL)
247 RSA_free(sensitive_data.private_key);
248 sensitive_data.private_key = RSA_new();
249
250 if (public_key != NULL)
251 RSA_free(public_key);
252 public_key = RSA_new();
253
254 rsa_generate_key(sensitive_data.private_key, public_key,
255 options.server_key_bits);
256 arc4random_stir();
257 key_used = 0;
258 log("RSA key generation complete.");
259 }
260 /* Reschedule the alarm. */
261 signal(SIGALRM, key_regeneration_alarm);
262 alarm(options.key_regeneration_time);
263 errno = save_errno;
264}
265
Damien Millerb38eff82000-04-01 11:09:21 +1000266void
267sshd_exchange_identification(int sock_in, int sock_out)
268{
Damien Miller78928792000-04-12 20:17:38 +1000269 int i, mismatch;
Damien Millerb38eff82000-04-01 11:09:21 +1000270 int remote_major, remote_minor;
Damien Miller78928792000-04-12 20:17:38 +1000271 int major, minor;
Damien Millerb38eff82000-04-01 11:09:21 +1000272 char *s;
273 char buf[256]; /* Must not be larger than remote_version. */
274 char remote_version[256]; /* Must be at least as big as buf. */
275
Damien Miller78928792000-04-12 20:17:38 +1000276 if ((options.protocol & SSH_PROTO_1) &&
277 (options.protocol & SSH_PROTO_2)) {
278 major = PROTOCOL_MAJOR_1;
279 minor = 99;
280 } else if (options.protocol & SSH_PROTO_2) {
281 major = PROTOCOL_MAJOR_2;
282 minor = PROTOCOL_MINOR_2;
283 } else {
284 major = PROTOCOL_MAJOR_1;
285 minor = PROTOCOL_MINOR_1;
286 }
287 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
Damien Millerb38eff82000-04-01 11:09:21 +1000288 server_version_string = xstrdup(buf);
289
290 if (client_version_string == NULL) {
291 /* Send our protocol version identification. */
292 if (atomicio(write, sock_out, server_version_string, strlen(server_version_string))
293 != strlen(server_version_string)) {
294 log("Could not write ident string to %s.", get_remote_ipaddr());
295 fatal_cleanup();
296 }
297
298 /* Read other side\'s version identification. */
299 for (i = 0; i < sizeof(buf) - 1; i++) {
Damien Millerbf7f4662000-06-23 10:16:38 +1000300 if (atomicio(read, sock_in, &buf[i], 1) != 1) {
Damien Millerb38eff82000-04-01 11:09:21 +1000301 log("Did not receive ident string from %s.", get_remote_ipaddr());
302 fatal_cleanup();
303 }
304 if (buf[i] == '\r') {
305 buf[i] = '\n';
306 buf[i + 1] = 0;
307 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000308 }
309 if (buf[i] == '\n') {
310 /* buf[i] == '\n' */
311 buf[i + 1] = 0;
312 break;
313 }
314 }
315 buf[sizeof(buf) - 1] = 0;
316 client_version_string = xstrdup(buf);
317 }
318
319 /*
320 * Check that the versions match. In future this might accept
321 * several versions and set appropriate flags to handle them.
322 */
323 if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
324 &remote_major, &remote_minor, remote_version) != 3) {
Damien Miller4af51302000-04-16 11:18:38 +1000325 s = "Protocol mismatch.\n";
Damien Millerb38eff82000-04-01 11:09:21 +1000326 (void) atomicio(write, sock_out, s, strlen(s));
327 close(sock_in);
328 close(sock_out);
329 log("Bad protocol version identification '%.100s' from %s",
330 client_version_string, get_remote_ipaddr());
331 fatal_cleanup();
332 }
333 debug("Client protocol version %d.%d; client software version %.100s",
334 remote_major, remote_minor, remote_version);
335
Damien Millerefb4afe2000-04-12 18:45:05 +1000336 compat_datafellows(remote_version);
337
Damien Miller78928792000-04-12 20:17:38 +1000338 mismatch = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000339 switch(remote_major) {
340 case 1:
Damien Millereba71ba2000-04-29 23:57:08 +1000341 if (remote_minor == 99) {
342 if (options.protocol & SSH_PROTO_2)
343 enable_compat20();
344 else
345 mismatch = 1;
346 break;
347 }
Damien Miller78928792000-04-12 20:17:38 +1000348 if (!(options.protocol & SSH_PROTO_1)) {
349 mismatch = 1;
350 break;
351 }
Damien Millerb38eff82000-04-01 11:09:21 +1000352 if (remote_minor < 3) {
353 packet_disconnect("Your ssh version is too old and"
354 "is no longer supported. Please install a newer version.");
355 } else if (remote_minor == 3) {
356 /* note that this disables agent-forwarding */
357 enable_compat13();
358 }
Damien Miller78928792000-04-12 20:17:38 +1000359 break;
Damien Millerefb4afe2000-04-12 18:45:05 +1000360 case 2:
Damien Miller78928792000-04-12 20:17:38 +1000361 if (options.protocol & SSH_PROTO_2) {
Damien Millerefb4afe2000-04-12 18:45:05 +1000362 enable_compat20();
363 break;
364 }
365 /* FALLTHROUGH */
Damien Miller4af51302000-04-16 11:18:38 +1000366 default:
Damien Miller78928792000-04-12 20:17:38 +1000367 mismatch = 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000368 break;
369 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000370 chop(server_version_string);
371 chop(client_version_string);
Damien Miller78928792000-04-12 20:17:38 +1000372 debug("Local version string %.200s", server_version_string);
373
374 if (mismatch) {
375 s = "Protocol major versions differ.\n";
376 (void) atomicio(write, sock_out, s, strlen(s));
377 close(sock_in);
378 close(sock_out);
379 log("Protocol major versions differ for %s: %.200s vs. %.200s",
380 get_remote_ipaddr(),
381 server_version_string, client_version_string);
382 fatal_cleanup();
383 }
Damien Millereba71ba2000-04-29 23:57:08 +1000384 if (compat20)
385 packet_set_ssh2_format();
386}
387
388
389void
390destroy_sensitive_data(void)
391{
392 /* Destroy the private and public keys. They will no longer be needed. */
Damien Millerc4be7ce2000-05-17 23:02:03 +1000393 if (public_key)
394 RSA_free(public_key);
395 if (sensitive_data.private_key)
396 RSA_free(sensitive_data.private_key);
397 if (sensitive_data.host_key)
398 RSA_free(sensitive_data.host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000399 if (sensitive_data.dsa_host_key != NULL)
400 key_free(sensitive_data.dsa_host_key);
Damien Millerb38eff82000-04-01 11:09:21 +1000401}
402
Damien Miller95def091999-11-25 00:26:21 +1100403/*
404 * Main program for the daemon.
405 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406int
407main(int ac, char **av)
408{
Damien Miller95def091999-11-25 00:26:21 +1100409 extern char *optarg;
410 extern int optind;
Damien Miller166fca82000-04-20 07:42:21 +1000411 int opt, sock_in = 0, sock_out = 0, newsock, i, fdsetsz, on = 1;
412 pid_t pid;
Damien Miller34132e52000-01-14 15:45:46 +1100413 socklen_t fromlen;
Damien Millereba71ba2000-04-29 23:57:08 +1000414 int silent = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100415 fd_set *fdset;
416 struct sockaddr_storage from;
Damien Miller95def091999-11-25 00:26:21 +1100417 const char *remote_ip;
418 int remote_port;
Damien Miller95def091999-11-25 00:26:21 +1100419 FILE *f;
420 struct linger linger;
Damien Miller34132e52000-01-14 15:45:46 +1100421 struct addrinfo *ai;
422 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
423 int listen_sock, maxfd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424
Damien Millerf9b625c2000-07-09 22:42:32 +1000425 init_rng();
426
Damien Miller95def091999-11-25 00:26:21 +1100427 /* Save argv[0]. */
Damien Millerb8c656e2000-06-28 15:22:41 +1000428 saved_argc = ac;
Damien Miller95def091999-11-25 00:26:21 +1100429 saved_argv = av;
430 if (strchr(av[0], '/'))
431 av0 = strrchr(av[0], '/') + 1;
432 else
433 av0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434
Damien Miller95def091999-11-25 00:26:21 +1100435 /* Initialize configuration options to their default values. */
436 initialize_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437
Damien Miller95def091999-11-25 00:26:21 +1100438 /* Parse command-line arguments. */
Damien Miller78928792000-04-12 20:17:38 +1000439 while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:diqQ46")) != EOF) {
Damien Miller95def091999-11-25 00:26:21 +1100440 switch (opt) {
Damien Miller34132e52000-01-14 15:45:46 +1100441 case '4':
442 IPv4or6 = AF_INET;
443 break;
444 case '6':
445 IPv4or6 = AF_INET6;
446 break;
Damien Miller95def091999-11-25 00:26:21 +1100447 case 'f':
448 config_file_name = optarg;
449 break;
450 case 'd':
451 debug_flag = 1;
452 options.log_level = SYSLOG_LEVEL_DEBUG;
453 break;
454 case 'i':
455 inetd_flag = 1;
456 break;
457 case 'Q':
Damien Millereba71ba2000-04-29 23:57:08 +1000458 silent = 1;
Damien Miller95def091999-11-25 00:26:21 +1100459 break;
460 case 'q':
461 options.log_level = SYSLOG_LEVEL_QUIET;
462 break;
463 case 'b':
464 options.server_key_bits = atoi(optarg);
465 break;
466 case 'p':
Damien Miller34132e52000-01-14 15:45:46 +1100467 options.ports_from_cmdline = 1;
468 if (options.num_ports >= MAX_PORTS)
469 fatal("too many ports.\n");
470 options.ports[options.num_ports++] = atoi(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100471 break;
472 case 'g':
473 options.login_grace_time = atoi(optarg);
474 break;
475 case 'k':
476 options.key_regeneration_time = atoi(optarg);
477 break;
478 case 'h':
479 options.host_key_file = optarg;
480 break;
481 case 'V':
482 client_version_string = optarg;
483 /* only makes sense with inetd_flag, i.e. no listen() */
484 inetd_flag = 1;
485 break;
486 case '?':
487 default:
488 fprintf(stderr, "sshd version %s\n", SSH_VERSION);
489 fprintf(stderr, "Usage: %s [options]\n", av0);
490 fprintf(stderr, "Options:\n");
Damien Miller5428f641999-11-25 11:54:57 +1100491 fprintf(stderr, " -f file Configuration file (default %s)\n", SERVER_CONFIG_FILE);
Damien Miller95def091999-11-25 00:26:21 +1100492 fprintf(stderr, " -d Debugging mode\n");
493 fprintf(stderr, " -i Started from inetd\n");
494 fprintf(stderr, " -q Quiet (no logging)\n");
495 fprintf(stderr, " -p port Listen on the specified port (default: 22)\n");
496 fprintf(stderr, " -k seconds Regenerate server key every this many seconds (default: 3600)\n");
497 fprintf(stderr, " -g seconds Grace period for authentication (default: 300)\n");
498 fprintf(stderr, " -b bits Size of server RSA key (default: 768 bits)\n");
499 fprintf(stderr, " -h file File from which to read host key (default: %s)\n",
Damien Miller34132e52000-01-14 15:45:46 +1100500 HOST_KEY_FILE);
501 fprintf(stderr, " -4 Use IPv4 only\n");
502 fprintf(stderr, " -6 Use IPv6 only\n");
Damien Miller95def091999-11-25 00:26:21 +1100503 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506
Damien Miller34132e52000-01-14 15:45:46 +1100507 /*
508 * Force logging to stderr until we have loaded the private host
509 * key (unless started from inetd)
510 */
511 log_init(av0,
512 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
513 options.log_facility == -1 ? SYSLOG_FACILITY_AUTH : options.log_facility,
Damien Millereba71ba2000-04-29 23:57:08 +1000514 !silent && !inetd_flag);
Damien Miller34132e52000-01-14 15:45:46 +1100515
Damien Miller95def091999-11-25 00:26:21 +1100516 /* Read server configuration options from the configuration file. */
517 read_server_config(&options, config_file_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518
Damien Miller95def091999-11-25 00:26:21 +1100519 /* Fill in default values for those options not explicitly set. */
520 fill_default_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521
Damien Miller95def091999-11-25 00:26:21 +1100522 /* Check that there are no remaining arguments. */
523 if (optind < ac) {
524 fprintf(stderr, "Extra argument %s.\n", av[optind]);
525 exit(1);
526 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000527
Damien Miller95def091999-11-25 00:26:21 +1100528 debug("sshd version %.100s", SSH_VERSION);
Damien Miller2ccf6611999-11-15 15:25:10 +1100529
Damien Millereba71ba2000-04-29 23:57:08 +1000530 sensitive_data.dsa_host_key = NULL;
531 sensitive_data.host_key = NULL;
532
533 /* check if RSA support exists */
534 if ((options.protocol & SSH_PROTO_1) &&
535 rsa_alive() == 0) {
536 log("no RSA support in libssl and libcrypto. See ssl(8)");
537 log("Disabling protocol version 1");
538 options.protocol &= ~SSH_PROTO_1;
539 }
540 /* Load the RSA/DSA host key. It must have empty passphrase. */
541 if (options.protocol & SSH_PROTO_1) {
542 Key k;
543 sensitive_data.host_key = RSA_new();
544 k.type = KEY_RSA;
545 k.rsa = sensitive_data.host_key;
546 errno = 0;
547 if (!load_private_key(options.host_key_file, "", &k, NULL)) {
548 error("Could not load host key: %.200s: %.100s",
549 options.host_key_file, strerror(errno));
550 log("Disabling protocol version 1");
551 options.protocol &= ~SSH_PROTO_1;
552 }
553 k.rsa = NULL;
554 }
555 if (options.protocol & SSH_PROTO_2) {
556 sensitive_data.dsa_host_key = key_new(KEY_DSA);
Damien Millere247cc42000-05-07 12:03:14 +1000557 if (!load_private_key(options.host_dsa_key_file, "", sensitive_data.dsa_host_key, NULL)) {
558
559 error("Could not load DSA host key: %.200s", options.host_dsa_key_file);
Damien Millereba71ba2000-04-29 23:57:08 +1000560 log("Disabling protocol version 2");
561 options.protocol &= ~SSH_PROTO_2;
562 }
563 }
564 if (! options.protocol & (SSH_PROTO_1|SSH_PROTO_2)) {
565 if (silent == 0)
566 fprintf(stderr, "sshd: no hostkeys available -- exiting.\n");
567 log("sshd: no hostkeys available -- exiting.\n");
Damien Miller95def091999-11-25 00:26:21 +1100568 exit(1);
569 }
Damien Miller95def091999-11-25 00:26:21 +1100570
Damien Millereba71ba2000-04-29 23:57:08 +1000571 /* Check certain values for sanity. */
572 if (options.protocol & SSH_PROTO_1) {
573 if (options.server_key_bits < 512 ||
574 options.server_key_bits > 32768) {
575 fprintf(stderr, "Bad server key size.\n");
576 exit(1);
577 }
578 /*
579 * Check that server and host key lengths differ sufficiently. This
580 * is necessary to make double encryption work with rsaref. Oh, I
581 * hate software patents. I dont know if this can go? Niels
582 */
583 if (options.server_key_bits >
584 BN_num_bits(sensitive_data.host_key->n) - SSH_KEY_BITS_RESERVED &&
585 options.server_key_bits <
586 BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) {
587 options.server_key_bits =
588 BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED;
589 debug("Forcing server key to %d bits to make it differ from host key.",
590 options.server_key_bits);
591 }
592 }
593
594 /* Initialize the log (it is reinitialized below in case we forked). */
Damien Miller95def091999-11-25 00:26:21 +1100595 if (debug_flag && !inetd_flag)
596 log_stderr = 1;
597 log_init(av0, options.log_level, options.log_facility, log_stderr);
598
Damien Millereba71ba2000-04-29 23:57:08 +1000599 /*
600 * If not in debugging mode, and not started from inetd, disconnect
601 * from the controlling terminal, and fork. The original process
602 * exits.
603 */
Damien Miller95def091999-11-25 00:26:21 +1100604 if (!debug_flag && !inetd_flag) {
605#ifdef TIOCNOTTY
606 int fd;
607#endif /* TIOCNOTTY */
608 if (daemon(0, 0) < 0)
609 fatal("daemon() failed: %.200s", strerror(errno));
610
611 /* Disconnect from the controlling tty. */
612#ifdef TIOCNOTTY
613 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
614 if (fd >= 0) {
615 (void) ioctl(fd, TIOCNOTTY, NULL);
616 close(fd);
617 }
618#endif /* TIOCNOTTY */
619 }
620 /* Reinitialize the log (because of the fork above). */
621 log_init(av0, options.log_level, options.log_facility, log_stderr);
622
Damien Miller95def091999-11-25 00:26:21 +1100623 /* Do not display messages to stdout in RSA code. */
624 rsa_set_verbose(0);
625
626 /* Initialize the random number generator. */
627 arc4random_stir();
628
629 /* Chdir to the root directory so that the current disk can be
630 unmounted if desired. */
631 chdir("/");
632
Damien Miller95def091999-11-25 00:26:21 +1100633 /* Start listening for a socket, unless started from inetd. */
634 if (inetd_flag) {
635 int s1, s2;
636 s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */
637 s2 = dup(s1);
638 sock_in = dup(0);
639 sock_out = dup(1);
Damien Millereba71ba2000-04-29 23:57:08 +1000640 /*
641 * We intentionally do not close the descriptors 0, 1, and 2
642 * as our code for setting the descriptors won\'t work if
643 * ttyfd happens to be one of those.
644 */
Damien Miller95def091999-11-25 00:26:21 +1100645 debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
646
Damien Millereba71ba2000-04-29 23:57:08 +1000647 if (options.protocol & SSH_PROTO_1) {
648 public_key = RSA_new();
649 sensitive_data.private_key = RSA_new();
650 log("Generating %d bit RSA key.", options.server_key_bits);
651 rsa_generate_key(sensitive_data.private_key, public_key,
652 options.server_key_bits);
653 arc4random_stir();
654 log("RSA key generation complete.");
655 }
Damien Miller95def091999-11-25 00:26:21 +1100656 } else {
Damien Miller34132e52000-01-14 15:45:46 +1100657 for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
658 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
659 continue;
660 if (num_listen_socks >= MAX_LISTEN_SOCKS)
661 fatal("Too many listen sockets. "
662 "Enlarge MAX_LISTEN_SOCKS");
663 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
664 ntop, sizeof(ntop), strport, sizeof(strport),
665 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
666 error("getnameinfo failed");
667 continue;
668 }
669 /* Create socket for listening. */
670 listen_sock = socket(ai->ai_family, SOCK_STREAM, 0);
671 if (listen_sock < 0) {
672 /* kernel may not support ipv6 */
673 verbose("socket: %.100s", strerror(errno));
674 continue;
675 }
676 if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) {
677 error("listen_sock O_NONBLOCK: %s", strerror(errno));
678 close(listen_sock);
679 continue;
680 }
681 /*
682 * Set socket options. We try to make the port
683 * reusable and have it close as fast as possible
684 * without waiting in unnecessary wait states on
685 * close.
686 */
687 setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
688 (void *) &on, sizeof(on));
689 linger.l_onoff = 1;
690 linger.l_linger = 5;
691 setsockopt(listen_sock, SOL_SOCKET, SO_LINGER,
692 (void *) &linger, sizeof(linger));
Damien Miller95def091999-11-25 00:26:21 +1100693
Damien Miller34132e52000-01-14 15:45:46 +1100694 debug("Bind to port %s on %s.", strport, ntop);
Damien Miller95def091999-11-25 00:26:21 +1100695
Damien Miller34132e52000-01-14 15:45:46 +1100696 /* Bind the socket to the desired port. */
Damien Miller3c7eeb22000-03-03 22:35:33 +1100697 if ((bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) &&
698 (!ai->ai_next)) {
Damien Miller34132e52000-01-14 15:45:46 +1100699 error("Bind to port %s on %s failed: %.200s.",
700 strport, ntop, strerror(errno));
701 close(listen_sock);
702 continue;
703 }
704 listen_socks[num_listen_socks] = listen_sock;
705 num_listen_socks++;
Damien Miller95def091999-11-25 00:26:21 +1100706
Damien Miller34132e52000-01-14 15:45:46 +1100707 /* Start listening on the port. */
708 log("Server listening on %s port %s.", ntop, strport);
709 if (listen(listen_sock, 5) < 0)
710 fatal("listen: %.100s", strerror(errno));
711
Damien Miller95def091999-11-25 00:26:21 +1100712 }
Damien Miller34132e52000-01-14 15:45:46 +1100713 freeaddrinfo(options.listen_addrs);
714
715 if (!num_listen_socks)
716 fatal("Cannot bind any address.");
717
Damien Miller95def091999-11-25 00:26:21 +1100718 if (!debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +1100719 /*
720 * Record our pid in /etc/sshd_pid to make it easier
721 * to kill the correct sshd. We don\'t want to do
722 * this before the bind above because the bind will
723 * fail if there already is a daemon, and this will
724 * overwrite any old pid in the file.
725 */
Damien Miller6f83b8e2000-05-02 09:23:45 +1000726 f = fopen(options.pid_file, "w");
Damien Miller95def091999-11-25 00:26:21 +1100727 if (f) {
728 fprintf(f, "%u\n", (unsigned int) getpid());
729 fclose(f);
730 }
731 }
Damien Millereba71ba2000-04-29 23:57:08 +1000732 if (options.protocol & SSH_PROTO_1) {
733 public_key = RSA_new();
734 sensitive_data.private_key = RSA_new();
Damien Miller95def091999-11-25 00:26:21 +1100735
Damien Millereba71ba2000-04-29 23:57:08 +1000736 log("Generating %d bit RSA key.", options.server_key_bits);
737 rsa_generate_key(sensitive_data.private_key, public_key,
738 options.server_key_bits);
739 arc4random_stir();
740 log("RSA key generation complete.");
Damien Miller95def091999-11-25 00:26:21 +1100741
Damien Millereba71ba2000-04-29 23:57:08 +1000742 /* Schedule server key regeneration alarm. */
743 signal(SIGALRM, key_regeneration_alarm);
744 alarm(options.key_regeneration_time);
745 }
Damien Miller95def091999-11-25 00:26:21 +1100746
747 /* Arrange to restart on SIGHUP. The handler needs listen_sock. */
748 signal(SIGHUP, sighup_handler);
749 signal(SIGTERM, sigterm_handler);
750 signal(SIGQUIT, sigterm_handler);
751
752 /* Arrange SIGCHLD to be caught. */
753 signal(SIGCHLD, main_sigchld_handler);
754
Damien Miller34132e52000-01-14 15:45:46 +1100755 /* setup fd set for listen */
756 maxfd = 0;
757 for (i = 0; i < num_listen_socks; i++)
758 if (listen_socks[i] > maxfd)
759 maxfd = listen_socks[i];
Damien Miller4af51302000-04-16 11:18:38 +1000760 fdsetsz = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
761 fdset = (fd_set *)xmalloc(fdsetsz);
Damien Miller34132e52000-01-14 15:45:46 +1100762
Damien Miller5428f641999-11-25 11:54:57 +1100763 /*
764 * Stay listening for connections until the system crashes or
765 * the daemon is killed with a signal.
766 */
Damien Miller95def091999-11-25 00:26:21 +1100767 for (;;) {
768 if (received_sighup)
769 sighup_restart();
Damien Miller34132e52000-01-14 15:45:46 +1100770 /* Wait in select until there is a connection. */
771 memset(fdset, 0, fdsetsz);
772 for (i = 0; i < num_listen_socks; i++)
773 FD_SET(listen_socks[i], fdset);
774 if (select(maxfd + 1, fdset, NULL, NULL, NULL) < 0) {
775 if (errno != EINTR)
776 error("select: %.100s", strerror(errno));
Damien Miller50945fa1999-12-09 10:31:37 +1100777 continue;
Damien Miller34132e52000-01-14 15:45:46 +1100778 }
779 for (i = 0; i < num_listen_socks; i++) {
780 if (!FD_ISSET(listen_socks[i], fdset))
Damien Miller95def091999-11-25 00:26:21 +1100781 continue;
Damien Miller34132e52000-01-14 15:45:46 +1100782 fromlen = sizeof(from);
783 newsock = accept(listen_socks[i], (struct sockaddr *)&from,
784 &fromlen);
785 if (newsock < 0) {
786 if (errno != EINTR && errno != EWOULDBLOCK)
787 error("accept: %.100s", strerror(errno));
788 continue;
789 }
790 if (fcntl(newsock, F_SETFL, 0) < 0) {
791 error("newsock del O_NONBLOCK: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100792 continue;
793 }
Damien Miller5428f641999-11-25 11:54:57 +1100794 /*
795 * Got connection. Fork a child to handle it, unless
796 * we are in debugging mode.
797 */
Damien Miller95def091999-11-25 00:26:21 +1100798 if (debug_flag) {
Damien Miller5428f641999-11-25 11:54:57 +1100799 /*
800 * In debugging mode. Close the listening
801 * socket, and start processing the
802 * connection without forking.
803 */
Damien Miller95def091999-11-25 00:26:21 +1100804 debug("Server will not fork when running in debugging mode.");
Damien Miller34132e52000-01-14 15:45:46 +1100805 close_listen_socks();
Damien Miller95def091999-11-25 00:26:21 +1100806 sock_in = newsock;
807 sock_out = newsock;
808 pid = getpid();
809 break;
810 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100811 /*
812 * Normal production daemon. Fork, and have
813 * the child process the connection. The
814 * parent continues listening.
815 */
Damien Miller95def091999-11-25 00:26:21 +1100816 if ((pid = fork()) == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100817 /*
818 * Child. Close the listening socket, and start using the
819 * accepted socket. Reinitialize logging (since our pid has
820 * changed). We break out of the loop to handle the connection.
821 */
Damien Miller34132e52000-01-14 15:45:46 +1100822 close_listen_socks();
Damien Miller95def091999-11-25 00:26:21 +1100823 sock_in = newsock;
824 sock_out = newsock;
825 log_init(av0, options.log_level, options.log_facility, log_stderr);
826 break;
827 }
828 }
829
830 /* Parent. Stay in the loop. */
831 if (pid < 0)
832 error("fork: %.100s", strerror(errno));
833 else
834 debug("Forked child %d.", pid);
835
836 /* Mark that the key has been used (it was "given" to the child). */
837 key_used = 1;
838
839 arc4random_stir();
840
841 /* Close the new socket (the child is now taking care of it). */
842 close(newsock);
Damien Miller34132e52000-01-14 15:45:46 +1100843 } /* for (i = 0; i < num_listen_socks; i++) */
844 /* child process check (or debug mode) */
845 if (num_listen_socks < 0)
846 break;
Damien Miller95def091999-11-25 00:26:21 +1100847 }
848 }
849
850 /* This is the child processing a new connection. */
851
Damien Miller5428f641999-11-25 11:54:57 +1100852 /*
853 * Disable the key regeneration alarm. We will not regenerate the
854 * key since we are no longer in a position to give it to anyone. We
855 * will not restart on SIGHUP since it no longer makes sense.
856 */
Damien Miller95def091999-11-25 00:26:21 +1100857 alarm(0);
858 signal(SIGALRM, SIG_DFL);
859 signal(SIGHUP, SIG_DFL);
860 signal(SIGTERM, SIG_DFL);
861 signal(SIGQUIT, SIG_DFL);
862 signal(SIGCHLD, SIG_DFL);
863
Damien Miller5428f641999-11-25 11:54:57 +1100864 /*
865 * Set socket options for the connection. We want the socket to
866 * close as fast as possible without waiting for anything. If the
867 * connection is not a socket, these will do nothing.
868 */
869 /* setsockopt(sock_in, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */
Damien Miller95def091999-11-25 00:26:21 +1100870 linger.l_onoff = 1;
871 linger.l_linger = 5;
872 setsockopt(sock_in, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger));
873
Damien Miller5428f641999-11-25 11:54:57 +1100874 /*
875 * Register our connection. This turns encryption off because we do
876 * not have a key.
877 */
Damien Miller95def091999-11-25 00:26:21 +1100878 packet_set_connection(sock_in, sock_out);
879
880 remote_port = get_remote_port();
881 remote_ip = get_remote_ipaddr();
882
883 /* Check whether logins are denied from this host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884#ifdef LIBWRAP
Damien Miller34132e52000-01-14 15:45:46 +1100885 /* XXX LIBWRAP noes not know about IPv6 */
Damien Miller95def091999-11-25 00:26:21 +1100886 {
887 struct request_info req;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888
Damien Miller95def091999-11-25 00:26:21 +1100889 request_init(&req, RQ_DAEMON, av0, RQ_FILE, sock_in, NULL);
890 fromhost(&req);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891
Damien Miller95def091999-11-25 00:26:21 +1100892 if (!hosts_access(&req)) {
893 close(sock_in);
894 close(sock_out);
895 refuse(&req);
896 }
Damien Miller34132e52000-01-14 15:45:46 +1100897/*XXX IPv6 verbose("Connection from %.500s port %d", eval_client(&req), remote_port); */
Damien Miller95def091999-11-25 00:26:21 +1100898 }
Damien Miller34132e52000-01-14 15:45:46 +1100899#endif /* LIBWRAP */
Damien Miller95def091999-11-25 00:26:21 +1100900 /* Log the connection. */
901 verbose("Connection from %.500s port %d", remote_ip, remote_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902
Damien Miller5428f641999-11-25 11:54:57 +1100903 /*
904 * We don\'t want to listen forever unless the other side
905 * successfully authenticates itself. So we set up an alarm which is
906 * cleared after successful authentication. A limit of zero
907 * indicates no limit. Note that we don\'t set the alarm in debugging
908 * mode; it is just annoying to have the server exit just when you
909 * are about to discover the bug.
910 */
Damien Miller95def091999-11-25 00:26:21 +1100911 signal(SIGALRM, grace_alarm_handler);
912 if (!debug_flag)
913 alarm(options.login_grace_time);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
Damien Millerb38eff82000-04-01 11:09:21 +1000915 sshd_exchange_identification(sock_in, sock_out);
Damien Miller5428f641999-11-25 11:54:57 +1100916 /*
917 * Check that the connection comes from a privileged port. Rhosts-
918 * and Rhosts-RSA-Authentication only make sense from priviledged
919 * programs. Of course, if the intruder has root access on his local
920 * machine, he can connect from any port. So do not use these
921 * authentication methods from machines that you do not trust.
922 */
Damien Miller95def091999-11-25 00:26:21 +1100923 if (remote_port >= IPPORT_RESERVED ||
924 remote_port < IPPORT_RESERVED / 2) {
925 options.rhosts_authentication = 0;
926 options.rhosts_rsa_authentication = 0;
927 }
Damien Miller34132e52000-01-14 15:45:46 +1100928#ifdef KRB4
929 if (!packet_connection_is_ipv4() &&
930 options.kerberos_authentication) {
931 debug("Kerberos Authentication disabled, only available for IPv4.");
932 options.kerberos_authentication = 0;
933 }
934#endif /* KRB4 */
935
Damien Miller95def091999-11-25 00:26:21 +1100936 packet_set_nonblocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000937
Damien Miller396691a2000-01-20 22:44:08 +1100938 /* perform the key exchange */
Damien Miller396691a2000-01-20 22:44:08 +1100939 /* authenticate user and start session */
Damien Millerefb4afe2000-04-12 18:45:05 +1000940 if (compat20) {
941 do_ssh2_kex();
942 do_authentication2();
943 } else {
944 do_ssh1_kex();
945 do_authentication();
946 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947
948#ifdef KRB4
Damien Miller95def091999-11-25 00:26:21 +1100949 /* Cleanup user's ticket cache file. */
950 if (options.kerberos_ticket_cleanup)
951 (void) dest_tkt();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952#endif /* KRB4 */
953
Damien Miller95def091999-11-25 00:26:21 +1100954 /* The connection has been terminated. */
955 verbose("Closing connection to %.100s", remote_ip);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956
Damien Millerbeb4ba51999-12-28 15:09:35 +1100957#ifdef USE_PAM
Damien Millere72b7af1999-12-30 15:08:44 +1100958 finish_pam();
Damien Millerbeb4ba51999-12-28 15:09:35 +1100959#endif /* USE_PAM */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960
Damien Miller95def091999-11-25 00:26:21 +1100961 packet_close();
962 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963}
964
Damien Miller95def091999-11-25 00:26:21 +1100965/*
Damien Miller396691a2000-01-20 22:44:08 +1100966 * SSH1 key exchange
Damien Miller95def091999-11-25 00:26:21 +1100967 */
Damien Miller2ccf6611999-11-15 15:25:10 +1100968void
Damien Millerb38eff82000-04-01 11:09:21 +1000969do_ssh1_kex()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970{
Damien Miller95def091999-11-25 00:26:21 +1100971 int i, len;
Damien Miller396691a2000-01-20 22:44:08 +1100972 int plen, slen;
Damien Miller95def091999-11-25 00:26:21 +1100973 BIGNUM *session_key_int;
974 unsigned char session_key[SSH_SESSION_KEY_LENGTH];
Damien Miller396691a2000-01-20 22:44:08 +1100975 unsigned char cookie[8];
Damien Miller95def091999-11-25 00:26:21 +1100976 unsigned int cipher_type, auth_mask, protocol_flags;
Damien Miller95def091999-11-25 00:26:21 +1100977 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Damien Miller5428f641999-11-25 11:54:57 +1100979 /*
980 * Generate check bytes that the client must send back in the user
981 * packet in order for it to be accepted; this is used to defy ip
982 * spoofing attacks. Note that this only works against somebody
983 * doing IP spoofing from a remote machine; any machine on the local
984 * network can still see outgoing packets and catch the random
985 * cookie. This only affects rhosts authentication, and this is one
986 * of the reasons why it is inherently insecure.
987 */
Damien Miller95def091999-11-25 00:26:21 +1100988 for (i = 0; i < 8; i++) {
989 if (i % 4 == 0)
990 rand = arc4random();
Damien Miller396691a2000-01-20 22:44:08 +1100991 cookie[i] = rand & 0xff;
Damien Miller95def091999-11-25 00:26:21 +1100992 rand >>= 8;
993 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Damien Miller5428f641999-11-25 11:54:57 +1100995 /*
996 * Send our public key. We include in the packet 64 bits of random
997 * data that must be matched in the reply in order to prevent IP
998 * spoofing.
999 */
Damien Miller95def091999-11-25 00:26:21 +11001000 packet_start(SSH_SMSG_PUBLIC_KEY);
1001 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001002 packet_put_char(cookie[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001003
Damien Miller95def091999-11-25 00:26:21 +11001004 /* Store our public server RSA key. */
1005 packet_put_int(BN_num_bits(public_key->n));
1006 packet_put_bignum(public_key->e);
1007 packet_put_bignum(public_key->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008
Damien Miller95def091999-11-25 00:26:21 +11001009 /* Store our public host RSA key. */
1010 packet_put_int(BN_num_bits(sensitive_data.host_key->n));
1011 packet_put_bignum(sensitive_data.host_key->e);
1012 packet_put_bignum(sensitive_data.host_key->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013
Damien Miller95def091999-11-25 00:26:21 +11001014 /* Put protocol flags. */
1015 packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016
Damien Miller95def091999-11-25 00:26:21 +11001017 /* Declare which ciphers we support. */
Damien Miller1383bd82000-04-06 12:32:37 +10001018 packet_put_int(cipher_mask1());
Damien Miller95def091999-11-25 00:26:21 +11001019
1020 /* Declare supported authentication types. */
1021 auth_mask = 0;
1022 if (options.rhosts_authentication)
1023 auth_mask |= 1 << SSH_AUTH_RHOSTS;
1024 if (options.rhosts_rsa_authentication)
1025 auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
1026 if (options.rsa_authentication)
1027 auth_mask |= 1 << SSH_AUTH_RSA;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001028#ifdef KRB4
Damien Miller95def091999-11-25 00:26:21 +11001029 if (options.kerberos_authentication)
1030 auth_mask |= 1 << SSH_AUTH_KERBEROS;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001031#endif
1032#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +11001033 if (options.kerberos_tgt_passing)
1034 auth_mask |= 1 << SSH_PASS_KERBEROS_TGT;
1035 if (options.afs_token_passing)
1036 auth_mask |= 1 << SSH_PASS_AFS_TOKEN;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001037#endif
Damien Miller95def091999-11-25 00:26:21 +11001038#ifdef SKEY
1039 if (options.skey_authentication == 1)
1040 auth_mask |= 1 << SSH_AUTH_TIS;
1041#endif
1042 if (options.password_authentication)
1043 auth_mask |= 1 << SSH_AUTH_PASSWORD;
1044 packet_put_int(auth_mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001045
Damien Miller95def091999-11-25 00:26:21 +11001046 /* Send the packet and wait for it to be sent. */
1047 packet_send();
1048 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049
Damien Miller95def091999-11-25 00:26:21 +11001050 debug("Sent %d bit public key and %d bit host key.",
1051 BN_num_bits(public_key->n), BN_num_bits(sensitive_data.host_key->n));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001052
Damien Miller95def091999-11-25 00:26:21 +11001053 /* Read clients reply (cipher type and session key). */
1054 packet_read_expect(&plen, SSH_CMSG_SESSION_KEY);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001055
Damien Miller50945fa1999-12-09 10:31:37 +11001056 /* Get cipher type and check whether we accept this. */
Damien Miller95def091999-11-25 00:26:21 +11001057 cipher_type = packet_get_char();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058
Damien Miller4af51302000-04-16 11:18:38 +10001059 if (!(cipher_mask() & (1 << cipher_type)))
Damien Miller50945fa1999-12-09 10:31:37 +11001060 packet_disconnect("Warning: client selects unsupported cipher.");
1061
Damien Miller95def091999-11-25 00:26:21 +11001062 /* Get check bytes from the packet. These must match those we
1063 sent earlier with the public key packet. */
1064 for (i = 0; i < 8; i++)
Damien Miller396691a2000-01-20 22:44:08 +11001065 if (cookie[i] != packet_get_char())
Damien Miller95def091999-11-25 00:26:21 +11001066 packet_disconnect("IP Spoofing check bytes do not match.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067
Damien Miller95def091999-11-25 00:26:21 +11001068 debug("Encryption type: %.200s", cipher_name(cipher_type));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001069
Damien Miller95def091999-11-25 00:26:21 +11001070 /* Get the encrypted integer. */
1071 session_key_int = BN_new();
1072 packet_get_bignum(session_key_int, &slen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001073
Damien Miller95def091999-11-25 00:26:21 +11001074 protocol_flags = packet_get_int();
1075 packet_set_protocol_flags(protocol_flags);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001076
Damien Miller95def091999-11-25 00:26:21 +11001077 packet_integrity_check(plen, 1 + 8 + slen + 4, SSH_CMSG_SESSION_KEY);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001078
Damien Miller5428f641999-11-25 11:54:57 +11001079 /*
1080 * Decrypt it using our private server key and private host key (key
1081 * with larger modulus first).
1082 */
Damien Miller95def091999-11-25 00:26:21 +11001083 if (BN_cmp(sensitive_data.private_key->n, sensitive_data.host_key->n) > 0) {
1084 /* Private key has bigger modulus. */
1085 if (BN_num_bits(sensitive_data.private_key->n) <
1086 BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) {
1087 fatal("do_connection: %s: private_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
1088 get_remote_ipaddr(),
1089 BN_num_bits(sensitive_data.private_key->n),
1090 BN_num_bits(sensitive_data.host_key->n),
1091 SSH_KEY_BITS_RESERVED);
1092 }
1093 rsa_private_decrypt(session_key_int, session_key_int,
1094 sensitive_data.private_key);
1095 rsa_private_decrypt(session_key_int, session_key_int,
1096 sensitive_data.host_key);
1097 } else {
1098 /* Host key has bigger modulus (or they are equal). */
1099 if (BN_num_bits(sensitive_data.host_key->n) <
1100 BN_num_bits(sensitive_data.private_key->n) + SSH_KEY_BITS_RESERVED) {
1101 fatal("do_connection: %s: host_key %d < private_key %d + SSH_KEY_BITS_RESERVED %d",
1102 get_remote_ipaddr(),
1103 BN_num_bits(sensitive_data.host_key->n),
1104 BN_num_bits(sensitive_data.private_key->n),
1105 SSH_KEY_BITS_RESERVED);
1106 }
1107 rsa_private_decrypt(session_key_int, session_key_int,
1108 sensitive_data.host_key);
1109 rsa_private_decrypt(session_key_int, session_key_int,
1110 sensitive_data.private_key);
1111 }
Damien Miller356a0b01999-11-08 15:30:59 +11001112
Damien Miller396691a2000-01-20 22:44:08 +11001113 compute_session_id(session_id, cookie,
Damien Miller95def091999-11-25 00:26:21 +11001114 sensitive_data.host_key->n,
1115 sensitive_data.private_key->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001116
Damien Miller396691a2000-01-20 22:44:08 +11001117 /* Destroy the private and public keys. They will no longer be needed. */
Damien Millereba71ba2000-04-29 23:57:08 +10001118 destroy_sensitive_data();
Damien Miller396691a2000-01-20 22:44:08 +11001119
Damien Miller5428f641999-11-25 11:54:57 +11001120 /*
1121 * Extract session key from the decrypted integer. The key is in the
1122 * least significant 256 bits of the integer; the first byte of the
1123 * key is in the highest bits.
1124 */
Damien Miller95def091999-11-25 00:26:21 +11001125 BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1126 len = BN_num_bytes(session_key_int);
1127 if (len < 0 || len > sizeof(session_key))
1128 fatal("do_connection: bad len from %s: session_key_int %d > sizeof(session_key) %d",
1129 get_remote_ipaddr(),
1130 len, sizeof(session_key));
1131 memset(session_key, 0, sizeof(session_key));
1132 BN_bn2bin(session_key_int, session_key + sizeof(session_key) - len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001133
Damien Miller396691a2000-01-20 22:44:08 +11001134 /* Destroy the decrypted integer. It is no longer needed. */
1135 BN_clear_free(session_key_int);
1136
Damien Miller95def091999-11-25 00:26:21 +11001137 /* Xor the first 16 bytes of the session key with the session id. */
1138 for (i = 0; i < 16; i++)
1139 session_key[i] ^= session_id[i];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001140
Damien Miller95def091999-11-25 00:26:21 +11001141 /* Set the session key. From this on all communications will be encrypted. */
1142 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143
Damien Miller95def091999-11-25 00:26:21 +11001144 /* Destroy our copy of the session key. It is no longer needed. */
1145 memset(session_key, 0, sizeof(session_key));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001146
Damien Miller95def091999-11-25 00:26:21 +11001147 debug("Received session key; encryption turned on.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001148
Damien Miller95def091999-11-25 00:26:21 +11001149 /* Send an acknowledgement packet. Note that this packet is sent encrypted. */
1150 packet_start(SSH_SMSG_SUCCESS);
1151 packet_send();
1152 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001153}
Damien Millerefb4afe2000-04-12 18:45:05 +10001154
1155/*
1156 * SSH2 key exchange: diffie-hellman-group1-sha1
1157 */
1158void
1159do_ssh2_kex()
1160{
1161 Buffer *server_kexinit;
1162 Buffer *client_kexinit;
1163 int payload_len, dlen;
1164 int slen;
1165 unsigned int klen, kout;
Damien Millerefb4afe2000-04-12 18:45:05 +10001166 unsigned char *signature = NULL;
1167 unsigned char *server_host_key_blob = NULL;
1168 unsigned int sbloblen;
1169 DH *dh;
1170 BIGNUM *dh_client_pub = 0;
1171 BIGNUM *shared_secret = 0;
1172 int i;
1173 unsigned char *kbuf;
1174 unsigned char *hash;
1175 Kex *kex;
Damien Millerefb4afe2000-04-12 18:45:05 +10001176 char *cprop[PROPOSAL_MAX];
Damien Millerefb4afe2000-04-12 18:45:05 +10001177
1178/* KEXINIT */
1179
Damien Miller78928792000-04-12 20:17:38 +10001180 if (options.ciphers != NULL) {
Damien Miller4af51302000-04-16 11:18:38 +10001181 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Damien Miller78928792000-04-12 20:17:38 +10001182 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
1183 }
Damien Millerb1715dc2000-05-30 13:44:51 +10001184 server_kexinit = kex_init(myproposal);
Damien Millerefb4afe2000-04-12 18:45:05 +10001185 client_kexinit = xmalloc(sizeof(*client_kexinit));
1186 buffer_init(client_kexinit);
Damien Millerefb4afe2000-04-12 18:45:05 +10001187
Damien Millerb1715dc2000-05-30 13:44:51 +10001188 /* algorithm negotiation */
1189 kex_exchange_kexinit(server_kexinit, client_kexinit, cprop);
1190 kex = kex_choose_conf(cprop, myproposal, 1);
1191 for (i = 0; i < PROPOSAL_MAX; i++)
1192 xfree(cprop[i]);
Damien Millerefb4afe2000-04-12 18:45:05 +10001193
1194/* KEXDH */
1195
1196 debug("Wait SSH2_MSG_KEXDH_INIT.");
1197 packet_read_expect(&payload_len, SSH2_MSG_KEXDH_INIT);
1198
1199 /* key, cert */
1200 dh_client_pub = BN_new();
1201 if (dh_client_pub == NULL)
1202 fatal("dh_client_pub == NULL");
1203 packet_get_bignum2(dh_client_pub, &dlen);
1204
1205#ifdef DEBUG_KEXDH
1206 fprintf(stderr, "\ndh_client_pub= ");
1207 bignum_print(dh_client_pub);
1208 fprintf(stderr, "\n");
1209 debug("bits %d", BN_num_bits(dh_client_pub));
1210#endif
1211
1212 /* generate DH key */
Damien Miller78928792000-04-12 20:17:38 +10001213 dh = dh_new_group1(); /* XXX depends on 'kex' */
Damien Millerefb4afe2000-04-12 18:45:05 +10001214
1215#ifdef DEBUG_KEXDH
1216 fprintf(stderr, "\np= ");
1217 bignum_print(dh->p);
1218 fprintf(stderr, "\ng= ");
1219 bignum_print(dh->g);
1220 fprintf(stderr, "\npub= ");
1221 bignum_print(dh->pub_key);
1222 fprintf(stderr, "\n");
1223#endif
Damien Miller78928792000-04-12 20:17:38 +10001224 if (!dh_pub_is_valid(dh, dh_client_pub))
1225 packet_disconnect("bad client public DH value");
Damien Millerefb4afe2000-04-12 18:45:05 +10001226
1227 klen = DH_size(dh);
1228 kbuf = xmalloc(klen);
1229 kout = DH_compute_key(kbuf, dh_client_pub, dh);
1230
1231#ifdef DEBUG_KEXDH
1232 debug("shared secret: len %d/%d", klen, kout);
1233 fprintf(stderr, "shared secret == ");
1234 for (i = 0; i< kout; i++)
1235 fprintf(stderr, "%02x", (kbuf[i])&0xff);
1236 fprintf(stderr, "\n");
1237#endif
1238 shared_secret = BN_new();
1239
1240 BN_bin2bn(kbuf, kout, shared_secret);
1241 memset(kbuf, 0, klen);
1242 xfree(kbuf);
1243
Damien Millereba71ba2000-04-29 23:57:08 +10001244 /* XXX precompute? */
1245 dsa_make_key_blob(sensitive_data.dsa_host_key, &server_host_key_blob, &sbloblen);
Damien Millerefb4afe2000-04-12 18:45:05 +10001246
1247 /* calc H */ /* XXX depends on 'kex' */
1248 hash = kex_hash(
1249 client_version_string,
1250 server_version_string,
1251 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
1252 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
1253 (char *)server_host_key_blob, sbloblen,
1254 dh_client_pub,
1255 dh->pub_key,
1256 shared_secret
1257 );
1258 buffer_free(client_kexinit);
1259 buffer_free(server_kexinit);
1260 xfree(client_kexinit);
1261 xfree(server_kexinit);
1262#ifdef DEBUG_KEXDH
Damien Miller4af51302000-04-16 11:18:38 +10001263 fprintf(stderr, "hash == ");
1264 for (i = 0; i< 20; i++)
1265 fprintf(stderr, "%02x", (hash[i])&0xff);
1266 fprintf(stderr, "\n");
Damien Millerefb4afe2000-04-12 18:45:05 +10001267#endif
Damien Millereba71ba2000-04-29 23:57:08 +10001268 /* save session id := H */
1269 /* XXX hashlen depends on KEX */
1270 session_id2_len = 20;
1271 session_id2 = xmalloc(session_id2_len);
1272 memcpy(session_id2, hash, session_id2_len);
1273
Damien Millerefb4afe2000-04-12 18:45:05 +10001274 /* sign H */
Damien Millereba71ba2000-04-29 23:57:08 +10001275 /* XXX hashlen depends on KEX */
1276 dsa_sign(sensitive_data.dsa_host_key, &signature, &slen, hash, 20);
1277
1278 destroy_sensitive_data();
Damien Millerefb4afe2000-04-12 18:45:05 +10001279
1280 /* send server hostkey, DH pubkey 'f' and singed H */
1281 packet_start(SSH2_MSG_KEXDH_REPLY);
1282 packet_put_string((char *)server_host_key_blob, sbloblen);
Damien Millere247cc42000-05-07 12:03:14 +10001283 packet_put_bignum2(dh->pub_key); /* f */
Damien Millerefb4afe2000-04-12 18:45:05 +10001284 packet_put_string((char *)signature, slen);
1285 packet_send();
Damien Miller8bb73be2000-04-19 16:26:12 +10001286 xfree(signature);
Damien Millereba71ba2000-04-29 23:57:08 +10001287 xfree(server_host_key_blob);
Damien Millerefb4afe2000-04-12 18:45:05 +10001288 packet_write_wait();
1289
1290 kex_derive_keys(kex, hash, shared_secret);
1291 packet_set_kex(kex);
1292
1293 /* have keys, free DH */
1294 DH_free(dh);
1295
1296 debug("send SSH2_MSG_NEWKEYS.");
1297 packet_start(SSH2_MSG_NEWKEYS);
1298 packet_send();
1299 packet_write_wait();
1300 debug("done: send SSH2_MSG_NEWKEYS.");
1301
1302 debug("Wait SSH2_MSG_NEWKEYS.");
1303 packet_read_expect(&payload_len, SSH2_MSG_NEWKEYS);
1304 debug("GOT SSH2_MSG_NEWKEYS.");
1305
Damien Miller78928792000-04-12 20:17:38 +10001306#ifdef DEBUG_KEXDH
Damien Millerefb4afe2000-04-12 18:45:05 +10001307 /* send 1st encrypted/maced/compressed message */
1308 packet_start(SSH2_MSG_IGNORE);
1309 packet_put_cstring("markus");
1310 packet_send();
1311 packet_write_wait();
Damien Miller78928792000-04-12 20:17:38 +10001312#endif
Damien Millerefb4afe2000-04-12 18:45:05 +10001313 debug("done: KEX2.");
1314}