]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 2 | * 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. | |
e78a59f5 | 11 | * |
12 | * SSH2 implementation, | |
13 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
5260325f | 14 | */ |
8efc0c15 | 15 | |
16 | #include "includes.h" | |
c345cf9d | 17 | RCSID("$OpenBSD: sshd.c,v 1.125 2000/08/17 20:06:34 markus Exp $"); |
8efc0c15 | 18 | |
19 | #include "xmalloc.h" | |
20 | #include "rsa.h" | |
21 | #include "ssh.h" | |
22 | #include "pty.h" | |
23 | #include "packet.h" | |
8efc0c15 | 24 | #include "cipher.h" |
25 | #include "mpaux.h" | |
26 | #include "servconf.h" | |
27 | #include "uidswap.h" | |
28 | #include "compat.h" | |
7368a6c8 | 29 | #include "buffer.h" |
30 | ||
e78a59f5 | 31 | #include "ssh2.h" |
35484284 | 32 | #include <openssl/dh.h> |
33 | #include <openssl/bn.h> | |
34 | #include <openssl/hmac.h> | |
e78a59f5 | 35 | #include "kex.h" |
35484284 | 36 | #include <openssl/dsa.h> |
37 | #include <openssl/rsa.h> | |
7368a6c8 | 38 | #include "key.h" |
e78a59f5 | 39 | #include "dsa.h" |
7368a6c8 | 40 | |
41 | #include "auth.h" | |
e78a59f5 | 42 | #include "myproposal.h" |
a306f2dd | 43 | #include "authfile.h" |
8efc0c15 | 44 | |
45 | #ifdef LIBWRAP | |
46 | #include <tcpd.h> | |
47 | #include <syslog.h> | |
48 | int allow_severity = LOG_INFO; | |
49 | int deny_severity = LOG_WARNING; | |
50 | #endif /* LIBWRAP */ | |
51 | ||
52 | #ifndef O_NOCTTY | |
53 | #define O_NOCTTY 0 | |
54 | #endif | |
55 | ||
8efc0c15 | 56 | /* Server configuration options. */ |
57 | ServerOptions options; | |
58 | ||
59 | /* Name of the server configuration file. */ | |
60 | char *config_file_name = SERVER_CONFIG_FILE; | |
61 | ||
6ae2364d | 62 | /* |
48e671d5 | 63 | * 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 | */ | |
59e76f33 | 66 | #ifdef IPV4_DEFAULT |
67 | int IPv4or6 = AF_INET; | |
68 | #else | |
48e671d5 | 69 | int IPv4or6 = AF_UNSPEC; |
59e76f33 | 70 | #endif |
48e671d5 | 71 | |
5260325f | 72 | /* |
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 | */ | |
8efc0c15 | 78 | int debug_flag = 0; |
79 | ||
80 | /* Flag indicating that the daemon is being started from inetd. */ | |
81 | int inetd_flag = 0; | |
82 | ||
6a17f9c2 | 83 | /* debug goes to stderr unless inetd_flag is set */ |
84 | int log_stderr = 0; | |
85 | ||
8efc0c15 | 86 | /* argv[0] without path. */ |
87 | char *av0; | |
88 | ||
89 | /* Saved arguments to main(). */ | |
90 | char **saved_argv; | |
4d33e531 | 91 | int saved_argc; |
8efc0c15 | 92 | |
aa3378df | 93 | /* |
48e671d5 | 94 | * The sockets that the server is listening; this is used in the SIGHUP |
95 | * signal handler. | |
aa3378df | 96 | */ |
48e671d5 | 97 | #define MAX_LISTEN_SOCKS 16 |
98 | int listen_socks[MAX_LISTEN_SOCKS]; | |
99 | int num_listen_socks = 0; | |
8efc0c15 | 100 | |
aa3378df | 101 | /* |
102 | * the client's version string, passed by sshd2 in compat mode. if != NULL, | |
103 | * sshd will skip the version-number exchange | |
104 | */ | |
5260325f | 105 | char *client_version_string = NULL; |
7368a6c8 | 106 | char *server_version_string = NULL; |
8efc0c15 | 107 | |
aa3378df | 108 | /* |
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 | */ | |
5260325f | 116 | struct { |
a306f2dd | 117 | RSA *private_key; /* Private part of empheral server key. */ |
5260325f | 118 | RSA *host_key; /* Private part of host key. */ |
a306f2dd | 119 | Key *dsa_host_key; /* Private DSA host key. */ |
8efc0c15 | 120 | } sensitive_data; |
121 | ||
aa3378df | 122 | /* |
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 | */ | |
8efc0c15 | 126 | int key_used = 0; |
127 | ||
128 | /* This is set to true when SIGHUP is received. */ | |
129 | int received_sighup = 0; | |
130 | ||
131 | /* Public side of the server key. This value is regenerated regularly with | |
132 | the private key. */ | |
133 | RSA *public_key; | |
134 | ||
7368a6c8 | 135 | /* session identifier, used by RSA-auth */ |
136 | unsigned char session_id[16]; | |
e7c0f9d5 | 137 | |
a306f2dd | 138 | /* same for ssh2 */ |
139 | unsigned char *session_id2 = NULL; | |
140 | int session_id2_len = 0; | |
141 | ||
c345cf9d | 142 | /* record remote hostname or ip */ |
143 | unsigned int utmp_len = MAXHOSTNAMELEN; | |
144 | ||
7368a6c8 | 145 | /* Prototypes for various functions defined later in this file. */ |
146 | void do_ssh1_kex(); | |
e78a59f5 | 147 | void do_ssh2_kex(); |
c8d54615 | 148 | |
48e671d5 | 149 | /* |
150 | * Close all listening sockets | |
151 | */ | |
152 | void | |
153 | close_listen_socks(void) | |
154 | { | |
155 | int i; | |
156 | for (i = 0; i < num_listen_socks; i++) | |
157 | close(listen_socks[i]); | |
158 | num_listen_socks = -1; | |
159 | } | |
160 | ||
5260325f | 161 | /* |
162 | * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP; | |
163 | * the effect is to reread the configuration file (and to regenerate | |
164 | * the server key). | |
165 | */ | |
6ae2364d | 166 | void |
5260325f | 167 | sighup_handler(int sig) |
8efc0c15 | 168 | { |
5260325f | 169 | received_sighup = 1; |
170 | signal(SIGHUP, sighup_handler); | |
8efc0c15 | 171 | } |
172 | ||
5260325f | 173 | /* |
174 | * Called from the main program after receiving SIGHUP. | |
175 | * Restarts the server. | |
176 | */ | |
6ae2364d | 177 | void |
5260325f | 178 | sighup_restart() |
8efc0c15 | 179 | { |
5260325f | 180 | log("Received SIGHUP; restarting."); |
48e671d5 | 181 | close_listen_socks(); |
5260325f | 182 | execv(saved_argv[0], saved_argv); |
183 | log("RESTART FAILED: av0='%s', error: %s.", av0, strerror(errno)); | |
184 | exit(1); | |
8efc0c15 | 185 | } |
186 | ||
5260325f | 187 | /* |
188 | * Generic signal handler for terminating signals in the master daemon. | |
189 | * These close the listen socket; not closing it seems to cause "Address | |
190 | * already in use" problems on some machines, which is inconvenient. | |
191 | */ | |
6ae2364d | 192 | void |
5260325f | 193 | sigterm_handler(int sig) |
8efc0c15 | 194 | { |
5260325f | 195 | log("Received signal %d; terminating.", sig); |
48e671d5 | 196 | close_listen_socks(); |
0fbe8c74 | 197 | unlink(options.pid_file); |
5260325f | 198 | exit(255); |
8efc0c15 | 199 | } |
200 | ||
5260325f | 201 | /* |
202 | * SIGCHLD handler. This is called whenever a child dies. This will then | |
203 | * reap any zombies left by exited c. | |
204 | */ | |
6ae2364d | 205 | void |
5260325f | 206 | main_sigchld_handler(int sig) |
8efc0c15 | 207 | { |
5260325f | 208 | int save_errno = errno; |
209 | int status; | |
5ad13cd7 | 210 | |
5260325f | 211 | while (waitpid(-1, &status, WNOHANG) > 0) |
212 | ; | |
5ad13cd7 | 213 | |
5260325f | 214 | signal(SIGCHLD, main_sigchld_handler); |
215 | errno = save_errno; | |
8efc0c15 | 216 | } |
217 | ||
5260325f | 218 | /* |
219 | * Signal handler for the alarm after the login grace period has expired. | |
220 | */ | |
6ae2364d | 221 | void |
5260325f | 222 | grace_alarm_handler(int sig) |
8efc0c15 | 223 | { |
5260325f | 224 | /* Close the connection. */ |
225 | packet_close(); | |
8efc0c15 | 226 | |
5260325f | 227 | /* Log error and exit. */ |
228 | fatal("Timeout before authentication for %s.", get_remote_ipaddr()); | |
229 | } | |
8efc0c15 | 230 | |
5260325f | 231 | /* |
232 | * Signal handler for the key regeneration alarm. Note that this | |
233 | * alarm only occurs in the daemon waiting for connections, and it does not | |
234 | * do anything with the private key or random state before forking. | |
235 | * Thus there should be no concurrency control/asynchronous execution | |
236 | * problems. | |
237 | */ | |
a306f2dd | 238 | /* XXX do we really want this work to be done in a signal handler ? -m */ |
6ae2364d | 239 | void |
5260325f | 240 | key_regeneration_alarm(int sig) |
241 | { | |
242 | int save_errno = errno; | |
243 | ||
244 | /* Check if we should generate a new key. */ | |
245 | if (key_used) { | |
246 | /* This should really be done in the background. */ | |
247 | log("Generating new %d bit RSA key.", options.server_key_bits); | |
248 | ||
249 | if (sensitive_data.private_key != NULL) | |
250 | RSA_free(sensitive_data.private_key); | |
251 | sensitive_data.private_key = RSA_new(); | |
252 | ||
253 | if (public_key != NULL) | |
254 | RSA_free(public_key); | |
255 | public_key = RSA_new(); | |
256 | ||
257 | rsa_generate_key(sensitive_data.private_key, public_key, | |
258 | options.server_key_bits); | |
259 | arc4random_stir(); | |
260 | key_used = 0; | |
261 | log("RSA key generation complete."); | |
262 | } | |
263 | /* Reschedule the alarm. */ | |
264 | signal(SIGALRM, key_regeneration_alarm); | |
265 | alarm(options.key_regeneration_time); | |
266 | errno = save_errno; | |
267 | } | |
8efc0c15 | 268 | |
7368a6c8 | 269 | void |
270 | sshd_exchange_identification(int sock_in, int sock_out) | |
271 | { | |
a8be9f80 | 272 | int i, mismatch; |
7368a6c8 | 273 | int remote_major, remote_minor; |
a8be9f80 | 274 | int major, minor; |
7368a6c8 | 275 | char *s; |
276 | char buf[256]; /* Must not be larger than remote_version. */ | |
277 | char remote_version[256]; /* Must be at least as big as buf. */ | |
278 | ||
a8be9f80 | 279 | if ((options.protocol & SSH_PROTO_1) && |
280 | (options.protocol & SSH_PROTO_2)) { | |
281 | major = PROTOCOL_MAJOR_1; | |
282 | minor = 99; | |
283 | } else if (options.protocol & SSH_PROTO_2) { | |
284 | major = PROTOCOL_MAJOR_2; | |
285 | minor = PROTOCOL_MINOR_2; | |
286 | } else { | |
287 | major = PROTOCOL_MAJOR_1; | |
288 | minor = PROTOCOL_MINOR_1; | |
289 | } | |
290 | snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION); | |
7368a6c8 | 291 | server_version_string = xstrdup(buf); |
292 | ||
293 | if (client_version_string == NULL) { | |
294 | /* Send our protocol version identification. */ | |
295 | if (atomicio(write, sock_out, server_version_string, strlen(server_version_string)) | |
296 | != strlen(server_version_string)) { | |
297 | log("Could not write ident string to %s.", get_remote_ipaddr()); | |
298 | fatal_cleanup(); | |
299 | } | |
300 | ||
301 | /* Read other side\'s version identification. */ | |
302 | for (i = 0; i < sizeof(buf) - 1; i++) { | |
e5a0294f | 303 | if (atomicio(read, sock_in, &buf[i], 1) != 1) { |
7368a6c8 | 304 | log("Did not receive ident string from %s.", get_remote_ipaddr()); |
305 | fatal_cleanup(); | |
306 | } | |
307 | if (buf[i] == '\r') { | |
308 | buf[i] = '\n'; | |
309 | buf[i + 1] = 0; | |
310 | continue; | |
7368a6c8 | 311 | } |
312 | if (buf[i] == '\n') { | |
313 | /* buf[i] == '\n' */ | |
314 | buf[i + 1] = 0; | |
315 | break; | |
316 | } | |
317 | } | |
318 | buf[sizeof(buf) - 1] = 0; | |
319 | client_version_string = xstrdup(buf); | |
320 | } | |
321 | ||
322 | /* | |
323 | * Check that the versions match. In future this might accept | |
324 | * several versions and set appropriate flags to handle them. | |
325 | */ | |
326 | if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n", | |
327 | &remote_major, &remote_minor, remote_version) != 3) { | |
6ae2364d | 328 | s = "Protocol mismatch.\n"; |
7368a6c8 | 329 | (void) atomicio(write, sock_out, s, strlen(s)); |
330 | close(sock_in); | |
331 | close(sock_out); | |
332 | log("Bad protocol version identification '%.100s' from %s", | |
333 | client_version_string, get_remote_ipaddr()); | |
334 | fatal_cleanup(); | |
335 | } | |
336 | debug("Client protocol version %d.%d; client software version %.100s", | |
337 | remote_major, remote_minor, remote_version); | |
338 | ||
e78a59f5 | 339 | compat_datafellows(remote_version); |
340 | ||
a8be9f80 | 341 | mismatch = 0; |
7368a6c8 | 342 | switch(remote_major) { |
343 | case 1: | |
a306f2dd | 344 | if (remote_minor == 99) { |
345 | if (options.protocol & SSH_PROTO_2) | |
346 | enable_compat20(); | |
347 | else | |
348 | mismatch = 1; | |
349 | break; | |
350 | } | |
a8be9f80 | 351 | if (!(options.protocol & SSH_PROTO_1)) { |
352 | mismatch = 1; | |
353 | break; | |
354 | } | |
7368a6c8 | 355 | if (remote_minor < 3) { |
089fbbd2 | 356 | packet_disconnect("Your ssh version is too old and " |
7368a6c8 | 357 | "is no longer supported. Please install a newer version."); |
358 | } else if (remote_minor == 3) { | |
359 | /* note that this disables agent-forwarding */ | |
360 | enable_compat13(); | |
361 | } | |
a8be9f80 | 362 | break; |
e78a59f5 | 363 | case 2: |
a8be9f80 | 364 | if (options.protocol & SSH_PROTO_2) { |
e78a59f5 | 365 | enable_compat20(); |
366 | break; | |
367 | } | |
368 | /* FALLTHROUGH */ | |
6ae2364d | 369 | default: |
a8be9f80 | 370 | mismatch = 1; |
371 | break; | |
372 | } | |
373 | chop(server_version_string); | |
374 | chop(client_version_string); | |
375 | debug("Local version string %.200s", server_version_string); | |
376 | ||
377 | if (mismatch) { | |
7368a6c8 | 378 | s = "Protocol major versions differ.\n"; |
379 | (void) atomicio(write, sock_out, s, strlen(s)); | |
380 | close(sock_in); | |
381 | close(sock_out); | |
a8be9f80 | 382 | log("Protocol major versions differ for %s: %.200s vs. %.200s", |
383 | get_remote_ipaddr(), | |
384 | server_version_string, client_version_string); | |
7368a6c8 | 385 | fatal_cleanup(); |
7368a6c8 | 386 | } |
a306f2dd | 387 | if (compat20) |
388 | packet_set_ssh2_format(); | |
389 | } | |
390 | ||
391 | ||
392 | void | |
393 | destroy_sensitive_data(void) | |
394 | { | |
395 | /* Destroy the private and public keys. They will no longer be needed. */ | |
afa5ee68 | 396 | if (public_key) |
397 | RSA_free(public_key); | |
398 | if (sensitive_data.private_key) | |
399 | RSA_free(sensitive_data.private_key); | |
400 | if (sensitive_data.host_key) | |
401 | RSA_free(sensitive_data.host_key); | |
a306f2dd | 402 | if (sensitive_data.dsa_host_key != NULL) |
403 | key_free(sensitive_data.dsa_host_key); | |
7368a6c8 | 404 | } |
405 | ||
c345cf9d | 406 | /* |
407 | * returns 1 if connection should be dropped, 0 otherwise. | |
408 | * dropping starts at connection #max_startups_begin with a probability | |
409 | * of (max_startups_rate/100). the probability increases linearly until | |
410 | * all connections are dropped for startups > max_startups | |
411 | */ | |
412 | int | |
413 | drop_connection(int startups) | |
414 | { | |
415 | double p, r; | |
416 | ||
417 | if (startups < options.max_startups_begin) | |
418 | return 0; | |
419 | if (startups >= options.max_startups) | |
420 | return 1; | |
421 | if (options.max_startups_rate == 100) | |
422 | return 1; | |
423 | ||
424 | p = 100 - options.max_startups_rate; | |
425 | p *= startups - options.max_startups_begin; | |
426 | p /= (double) (options.max_startups - options.max_startups_begin); | |
427 | p += options.max_startups_rate; | |
428 | p /= 100.0; | |
429 | r = arc4random() / (double) UINT_MAX; | |
430 | ||
431 | debug("drop_connection: p %g, r %g", p, r); | |
432 | return (r < p) ? 1 : 0; | |
433 | } | |
434 | ||
089fbbd2 | 435 | int *startup_pipes = NULL; /* options.max_startup sized array of fd ints */ |
436 | int startup_pipe; /* in child */ | |
437 | ||
5260325f | 438 | /* |
439 | * Main program for the daemon. | |
440 | */ | |
8efc0c15 | 441 | int |
442 | main(int ac, char **av) | |
443 | { | |
5260325f | 444 | extern char *optarg; |
445 | extern int optind; | |
089fbbd2 | 446 | int opt, sock_in = 0, sock_out = 0, newsock, j, i, fdsetsz, on = 1; |
9da5c3c9 | 447 | pid_t pid; |
48e671d5 | 448 | socklen_t fromlen; |
a306f2dd | 449 | int silent = 0; |
48e671d5 | 450 | fd_set *fdset; |
451 | struct sockaddr_storage from; | |
5260325f | 452 | const char *remote_ip; |
453 | int remote_port; | |
5260325f | 454 | FILE *f; |
455 | struct linger linger; | |
48e671d5 | 456 | struct addrinfo *ai; |
457 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; | |
458 | int listen_sock, maxfd; | |
089fbbd2 | 459 | int startup_p[2]; |
460 | int startups = 0; | |
5260325f | 461 | |
264dce47 | 462 | init_rng(); |
463 | ||
5260325f | 464 | /* Save argv[0]. */ |
4d33e531 | 465 | saved_argc = ac; |
5260325f | 466 | saved_argv = av; |
467 | if (strchr(av[0], '/')) | |
468 | av0 = strrchr(av[0], '/') + 1; | |
469 | else | |
470 | av0 = av[0]; | |
471 | ||
472 | /* Initialize configuration options to their default values. */ | |
473 | initialize_server_options(&options); | |
474 | ||
475 | /* Parse command-line arguments. */ | |
c345cf9d | 476 | while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:u:diqQ46")) != EOF) { |
5260325f | 477 | switch (opt) { |
48e671d5 | 478 | case '4': |
479 | IPv4or6 = AF_INET; | |
480 | break; | |
481 | case '6': | |
482 | IPv4or6 = AF_INET6; | |
483 | break; | |
5260325f | 484 | case 'f': |
485 | config_file_name = optarg; | |
486 | break; | |
487 | case 'd': | |
488 | debug_flag = 1; | |
489 | options.log_level = SYSLOG_LEVEL_DEBUG; | |
490 | break; | |
491 | case 'i': | |
492 | inetd_flag = 1; | |
493 | break; | |
494 | case 'Q': | |
a306f2dd | 495 | silent = 1; |
5260325f | 496 | break; |
497 | case 'q': | |
498 | options.log_level = SYSLOG_LEVEL_QUIET; | |
499 | break; | |
500 | case 'b': | |
501 | options.server_key_bits = atoi(optarg); | |
502 | break; | |
503 | case 'p': | |
48e671d5 | 504 | options.ports_from_cmdline = 1; |
505 | if (options.num_ports >= MAX_PORTS) | |
506 | fatal("too many ports.\n"); | |
507 | options.ports[options.num_ports++] = atoi(optarg); | |
5260325f | 508 | break; |
509 | case 'g': | |
510 | options.login_grace_time = atoi(optarg); | |
511 | break; | |
512 | case 'k': | |
513 | options.key_regeneration_time = atoi(optarg); | |
514 | break; | |
515 | case 'h': | |
516 | options.host_key_file = optarg; | |
517 | break; | |
518 | case 'V': | |
519 | client_version_string = optarg; | |
520 | /* only makes sense with inetd_flag, i.e. no listen() */ | |
521 | inetd_flag = 1; | |
522 | break; | |
c345cf9d | 523 | case 'u': |
524 | utmp_len = atoi(optarg); | |
525 | break; | |
5260325f | 526 | case '?': |
527 | default: | |
528 | fprintf(stderr, "sshd version %s\n", SSH_VERSION); | |
529 | fprintf(stderr, "Usage: %s [options]\n", av0); | |
530 | fprintf(stderr, "Options:\n"); | |
aa3378df | 531 | fprintf(stderr, " -f file Configuration file (default %s)\n", SERVER_CONFIG_FILE); |
5260325f | 532 | fprintf(stderr, " -d Debugging mode\n"); |
533 | fprintf(stderr, " -i Started from inetd\n"); | |
534 | fprintf(stderr, " -q Quiet (no logging)\n"); | |
535 | fprintf(stderr, " -p port Listen on the specified port (default: 22)\n"); | |
536 | fprintf(stderr, " -k seconds Regenerate server key every this many seconds (default: 3600)\n"); | |
537 | fprintf(stderr, " -g seconds Grace period for authentication (default: 300)\n"); | |
538 | fprintf(stderr, " -b bits Size of server RSA key (default: 768 bits)\n"); | |
539 | fprintf(stderr, " -h file File from which to read host key (default: %s)\n", | |
48e671d5 | 540 | HOST_KEY_FILE); |
c345cf9d | 541 | fprintf(stderr, " -u len Maximum hostname length for utmp recording\n"); |
48e671d5 | 542 | fprintf(stderr, " -4 Use IPv4 only\n"); |
543 | fprintf(stderr, " -6 Use IPv6 only\n"); | |
5260325f | 544 | exit(1); |
545 | } | |
546 | } | |
547 | ||
48e671d5 | 548 | /* |
549 | * Force logging to stderr until we have loaded the private host | |
550 | * key (unless started from inetd) | |
551 | */ | |
552 | log_init(av0, | |
553 | options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level, | |
554 | options.log_facility == -1 ? SYSLOG_FACILITY_AUTH : options.log_facility, | |
a306f2dd | 555 | !silent && !inetd_flag); |
48e671d5 | 556 | |
5260325f | 557 | /* Read server configuration options from the configuration file. */ |
558 | read_server_config(&options, config_file_name); | |
559 | ||
560 | /* Fill in default values for those options not explicitly set. */ | |
561 | fill_default_server_options(&options); | |
562 | ||
5260325f | 563 | /* Check that there are no remaining arguments. */ |
564 | if (optind < ac) { | |
565 | fprintf(stderr, "Extra argument %s.\n", av[optind]); | |
566 | exit(1); | |
8efc0c15 | 567 | } |
5260325f | 568 | |
569 | debug("sshd version %.100s", SSH_VERSION); | |
570 | ||
a306f2dd | 571 | sensitive_data.dsa_host_key = NULL; |
572 | sensitive_data.host_key = NULL; | |
573 | ||
574 | /* check if RSA support exists */ | |
575 | if ((options.protocol & SSH_PROTO_1) && | |
576 | rsa_alive() == 0) { | |
577 | log("no RSA support in libssl and libcrypto. See ssl(8)"); | |
578 | log("Disabling protocol version 1"); | |
579 | options.protocol &= ~SSH_PROTO_1; | |
580 | } | |
581 | /* Load the RSA/DSA host key. It must have empty passphrase. */ | |
582 | if (options.protocol & SSH_PROTO_1) { | |
583 | Key k; | |
584 | sensitive_data.host_key = RSA_new(); | |
585 | k.type = KEY_RSA; | |
586 | k.rsa = sensitive_data.host_key; | |
587 | errno = 0; | |
588 | if (!load_private_key(options.host_key_file, "", &k, NULL)) { | |
589 | error("Could not load host key: %.200s: %.100s", | |
590 | options.host_key_file, strerror(errno)); | |
591 | log("Disabling protocol version 1"); | |
592 | options.protocol &= ~SSH_PROTO_1; | |
593 | } | |
594 | k.rsa = NULL; | |
595 | } | |
596 | if (options.protocol & SSH_PROTO_2) { | |
597 | sensitive_data.dsa_host_key = key_new(KEY_DSA); | |
1d1ffb87 | 598 | if (!load_private_key(options.host_dsa_key_file, "", sensitive_data.dsa_host_key, NULL)) { |
599 | ||
600 | error("Could not load DSA host key: %.200s", options.host_dsa_key_file); | |
a306f2dd | 601 | log("Disabling protocol version 2"); |
602 | options.protocol &= ~SSH_PROTO_2; | |
603 | } | |
604 | } | |
605 | if (! options.protocol & (SSH_PROTO_1|SSH_PROTO_2)) { | |
606 | if (silent == 0) | |
607 | fprintf(stderr, "sshd: no hostkeys available -- exiting.\n"); | |
608 | log("sshd: no hostkeys available -- exiting.\n"); | |
5260325f | 609 | exit(1); |
610 | } | |
5260325f | 611 | |
a306f2dd | 612 | /* Check certain values for sanity. */ |
613 | if (options.protocol & SSH_PROTO_1) { | |
614 | if (options.server_key_bits < 512 || | |
615 | options.server_key_bits > 32768) { | |
616 | fprintf(stderr, "Bad server key size.\n"); | |
617 | exit(1); | |
618 | } | |
619 | /* | |
620 | * Check that server and host key lengths differ sufficiently. This | |
621 | * is necessary to make double encryption work with rsaref. Oh, I | |
622 | * hate software patents. I dont know if this can go? Niels | |
623 | */ | |
624 | if (options.server_key_bits > | |
625 | BN_num_bits(sensitive_data.host_key->n) - SSH_KEY_BITS_RESERVED && | |
626 | options.server_key_bits < | |
627 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) { | |
628 | options.server_key_bits = | |
629 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED; | |
630 | debug("Forcing server key to %d bits to make it differ from host key.", | |
631 | options.server_key_bits); | |
632 | } | |
633 | } | |
634 | ||
635 | /* Initialize the log (it is reinitialized below in case we forked). */ | |
5260325f | 636 | if (debug_flag && !inetd_flag) |
637 | log_stderr = 1; | |
638 | log_init(av0, options.log_level, options.log_facility, log_stderr); | |
639 | ||
a306f2dd | 640 | /* |
641 | * If not in debugging mode, and not started from inetd, disconnect | |
642 | * from the controlling terminal, and fork. The original process | |
643 | * exits. | |
644 | */ | |
5260325f | 645 | if (!debug_flag && !inetd_flag) { |
8efc0c15 | 646 | #ifdef TIOCNOTTY |
5260325f | 647 | int fd; |
8efc0c15 | 648 | #endif /* TIOCNOTTY */ |
5260325f | 649 | if (daemon(0, 0) < 0) |
650 | fatal("daemon() failed: %.200s", strerror(errno)); | |
651 | ||
652 | /* Disconnect from the controlling tty. */ | |
8efc0c15 | 653 | #ifdef TIOCNOTTY |
5260325f | 654 | fd = open("/dev/tty", O_RDWR | O_NOCTTY); |
655 | if (fd >= 0) { | |
656 | (void) ioctl(fd, TIOCNOTTY, NULL); | |
657 | close(fd); | |
658 | } | |
8efc0c15 | 659 | #endif /* TIOCNOTTY */ |
8efc0c15 | 660 | } |
5260325f | 661 | /* Reinitialize the log (because of the fork above). */ |
662 | log_init(av0, options.log_level, options.log_facility, log_stderr); | |
663 | ||
5260325f | 664 | /* Do not display messages to stdout in RSA code. */ |
665 | rsa_set_verbose(0); | |
666 | ||
667 | /* Initialize the random number generator. */ | |
668 | arc4random_stir(); | |
669 | ||
670 | /* Chdir to the root directory so that the current disk can be | |
671 | unmounted if desired. */ | |
672 | chdir("/"); | |
673 | ||
5260325f | 674 | /* Start listening for a socket, unless started from inetd. */ |
675 | if (inetd_flag) { | |
676 | int s1, s2; | |
677 | s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */ | |
678 | s2 = dup(s1); | |
679 | sock_in = dup(0); | |
680 | sock_out = dup(1); | |
4c8722d9 | 681 | startup_pipe = -1; |
a306f2dd | 682 | /* |
683 | * We intentionally do not close the descriptors 0, 1, and 2 | |
684 | * as our code for setting the descriptors won\'t work if | |
685 | * ttyfd happens to be one of those. | |
686 | */ | |
5260325f | 687 | debug("inetd sockets after dupping: %d, %d", sock_in, sock_out); |
688 | ||
a306f2dd | 689 | if (options.protocol & SSH_PROTO_1) { |
690 | public_key = RSA_new(); | |
691 | sensitive_data.private_key = RSA_new(); | |
692 | log("Generating %d bit RSA key.", options.server_key_bits); | |
693 | rsa_generate_key(sensitive_data.private_key, public_key, | |
694 | options.server_key_bits); | |
695 | arc4random_stir(); | |
696 | log("RSA key generation complete."); | |
697 | } | |
5260325f | 698 | } else { |
48e671d5 | 699 | for (ai = options.listen_addrs; ai; ai = ai->ai_next) { |
700 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) | |
701 | continue; | |
702 | if (num_listen_socks >= MAX_LISTEN_SOCKS) | |
703 | fatal("Too many listen sockets. " | |
704 | "Enlarge MAX_LISTEN_SOCKS"); | |
705 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, | |
706 | ntop, sizeof(ntop), strport, sizeof(strport), | |
707 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { | |
708 | error("getnameinfo failed"); | |
709 | continue; | |
710 | } | |
711 | /* Create socket for listening. */ | |
712 | listen_sock = socket(ai->ai_family, SOCK_STREAM, 0); | |
713 | if (listen_sock < 0) { | |
714 | /* kernel may not support ipv6 */ | |
715 | verbose("socket: %.100s", strerror(errno)); | |
716 | continue; | |
717 | } | |
718 | if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) { | |
719 | error("listen_sock O_NONBLOCK: %s", strerror(errno)); | |
720 | close(listen_sock); | |
721 | continue; | |
722 | } | |
723 | /* | |
724 | * Set socket options. We try to make the port | |
725 | * reusable and have it close as fast as possible | |
726 | * without waiting in unnecessary wait states on | |
727 | * close. | |
728 | */ | |
729 | setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, | |
730 | (void *) &on, sizeof(on)); | |
731 | linger.l_onoff = 1; | |
732 | linger.l_linger = 5; | |
733 | setsockopt(listen_sock, SOL_SOCKET, SO_LINGER, | |
734 | (void *) &linger, sizeof(linger)); | |
735 | ||
736 | debug("Bind to port %s on %s.", strport, ntop); | |
737 | ||
738 | /* Bind the socket to the desired port. */ | |
16218745 | 739 | if ((bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) && |
740 | (!ai->ai_next)) { | |
48e671d5 | 741 | error("Bind to port %s on %s failed: %.200s.", |
742 | strport, ntop, strerror(errno)); | |
743 | close(listen_sock); | |
744 | continue; | |
745 | } | |
746 | listen_socks[num_listen_socks] = listen_sock; | |
747 | num_listen_socks++; | |
748 | ||
749 | /* Start listening on the port. */ | |
750 | log("Server listening on %s port %s.", ntop, strport); | |
751 | if (listen(listen_sock, 5) < 0) | |
752 | fatal("listen: %.100s", strerror(errno)); | |
753 | ||
5260325f | 754 | } |
48e671d5 | 755 | freeaddrinfo(options.listen_addrs); |
756 | ||
757 | if (!num_listen_socks) | |
758 | fatal("Cannot bind any address."); | |
759 | ||
5260325f | 760 | if (!debug_flag) { |
aa3378df | 761 | /* |
762 | * Record our pid in /etc/sshd_pid to make it easier | |
763 | * to kill the correct sshd. We don\'t want to do | |
764 | * this before the bind above because the bind will | |
765 | * fail if there already is a daemon, and this will | |
766 | * overwrite any old pid in the file. | |
767 | */ | |
0fbe8c74 | 768 | f = fopen(options.pid_file, "w"); |
5260325f | 769 | if (f) { |
770 | fprintf(f, "%u\n", (unsigned int) getpid()); | |
771 | fclose(f); | |
772 | } | |
8efc0c15 | 773 | } |
a306f2dd | 774 | if (options.protocol & SSH_PROTO_1) { |
775 | public_key = RSA_new(); | |
776 | sensitive_data.private_key = RSA_new(); | |
8efc0c15 | 777 | |
a306f2dd | 778 | log("Generating %d bit RSA key.", options.server_key_bits); |
779 | rsa_generate_key(sensitive_data.private_key, public_key, | |
780 | options.server_key_bits); | |
781 | arc4random_stir(); | |
782 | log("RSA key generation complete."); | |
5260325f | 783 | |
a306f2dd | 784 | /* Schedule server key regeneration alarm. */ |
785 | signal(SIGALRM, key_regeneration_alarm); | |
786 | alarm(options.key_regeneration_time); | |
787 | } | |
5260325f | 788 | |
789 | /* Arrange to restart on SIGHUP. The handler needs listen_sock. */ | |
790 | signal(SIGHUP, sighup_handler); | |
089fbbd2 | 791 | |
5260325f | 792 | signal(SIGTERM, sigterm_handler); |
793 | signal(SIGQUIT, sigterm_handler); | |
794 | ||
795 | /* Arrange SIGCHLD to be caught. */ | |
796 | signal(SIGCHLD, main_sigchld_handler); | |
797 | ||
48e671d5 | 798 | /* setup fd set for listen */ |
089fbbd2 | 799 | fdset = NULL; |
48e671d5 | 800 | maxfd = 0; |
801 | for (i = 0; i < num_listen_socks; i++) | |
802 | if (listen_socks[i] > maxfd) | |
803 | maxfd = listen_socks[i]; | |
089fbbd2 | 804 | /* pipes connected to unauthenticated childs */ |
805 | startup_pipes = xmalloc(options.max_startups * sizeof(int)); | |
806 | for (i = 0; i < options.max_startups; i++) | |
807 | startup_pipes[i] = -1; | |
48e671d5 | 808 | |
aa3378df | 809 | /* |
810 | * Stay listening for connections until the system crashes or | |
811 | * the daemon is killed with a signal. | |
812 | */ | |
5260325f | 813 | for (;;) { |
814 | if (received_sighup) | |
815 | sighup_restart(); | |
089fbbd2 | 816 | if (fdset != NULL) |
817 | xfree(fdset); | |
818 | fdsetsz = howmany(maxfd, NFDBITS) * sizeof(fd_mask); | |
819 | fdset = (fd_set *)xmalloc(fdsetsz); | |
48e671d5 | 820 | memset(fdset, 0, fdsetsz); |
089fbbd2 | 821 | |
48e671d5 | 822 | for (i = 0; i < num_listen_socks; i++) |
823 | FD_SET(listen_socks[i], fdset); | |
089fbbd2 | 824 | for (i = 0; i < options.max_startups; i++) |
825 | if (startup_pipes[i] != -1) | |
826 | FD_SET(startup_pipes[i], fdset); | |
827 | ||
828 | /* Wait in select until there is a connection. */ | |
48e671d5 | 829 | if (select(maxfd + 1, fdset, NULL, NULL, NULL) < 0) { |
830 | if (errno != EINTR) | |
831 | error("select: %.100s", strerror(errno)); | |
2d86a6cc | 832 | continue; |
48e671d5 | 833 | } |
089fbbd2 | 834 | for (i = 0; i < options.max_startups; i++) |
835 | if (startup_pipes[i] != -1 && | |
836 | FD_ISSET(startup_pipes[i], fdset)) { | |
837 | /* | |
838 | * the read end of the pipe is ready | |
839 | * if the child has closed the pipe | |
840 | * after successfull authentication | |
841 | * or if the child has died | |
842 | */ | |
843 | close(startup_pipes[i]); | |
844 | startup_pipes[i] = -1; | |
845 | startups--; | |
846 | } | |
48e671d5 | 847 | for (i = 0; i < num_listen_socks; i++) { |
848 | if (!FD_ISSET(listen_socks[i], fdset)) | |
5260325f | 849 | continue; |
089fbbd2 | 850 | fromlen = sizeof(from); |
851 | newsock = accept(listen_socks[i], (struct sockaddr *)&from, | |
852 | &fromlen); | |
853 | if (newsock < 0) { | |
854 | if (errno != EINTR && errno != EWOULDBLOCK) | |
855 | error("accept: %.100s", strerror(errno)); | |
856 | continue; | |
857 | } | |
858 | if (fcntl(newsock, F_SETFL, 0) < 0) { | |
859 | error("newsock del O_NONBLOCK: %s", strerror(errno)); | |
860 | continue; | |
861 | } | |
c345cf9d | 862 | if (drop_connection(startups) == 1) { |
863 | debug("drop connection #%d", startups); | |
089fbbd2 | 864 | close(newsock); |
865 | continue; | |
866 | } | |
867 | if (pipe(startup_p) == -1) { | |
868 | close(newsock); | |
869 | continue; | |
870 | } | |
871 | ||
872 | for (j = 0; j < options.max_startups; j++) | |
873 | if (startup_pipes[j] == -1) { | |
874 | startup_pipes[j] = startup_p[0]; | |
875 | if (maxfd < startup_p[0]) | |
876 | maxfd = startup_p[0]; | |
877 | startups++; | |
878 | break; | |
879 | } | |
880 | ||
aa3378df | 881 | /* |
089fbbd2 | 882 | * Got connection. Fork a child to handle it, unless |
883 | * we are in debugging mode. | |
aa3378df | 884 | */ |
089fbbd2 | 885 | if (debug_flag) { |
aa3378df | 886 | /* |
089fbbd2 | 887 | * In debugging mode. Close the listening |
888 | * socket, and start processing the | |
889 | * connection without forking. | |
aa3378df | 890 | */ |
089fbbd2 | 891 | debug("Server will not fork when running in debugging mode."); |
48e671d5 | 892 | close_listen_socks(); |
5260325f | 893 | sock_in = newsock; |
894 | sock_out = newsock; | |
5540ea9b | 895 | startup_pipe = -1; |
3f7a7e4a | 896 | pid = getpid(); |
5260325f | 897 | break; |
089fbbd2 | 898 | } else { |
899 | /* | |
900 | * Normal production daemon. Fork, and have | |
901 | * the child process the connection. The | |
902 | * parent continues listening. | |
903 | */ | |
904 | if ((pid = fork()) == 0) { | |
905 | /* | |
906 | * Child. Close the listening and max_startup | |
907 | * sockets. Start using the accepted socket. | |
908 | * Reinitialize logging (since our pid has | |
909 | * changed). We break out of the loop to handle | |
910 | * the connection. | |
911 | */ | |
912 | startup_pipe = startup_p[1]; | |
913 | for (j = 0; j < options.max_startups; j++) | |
914 | if (startup_pipes[j] != -1) | |
915 | close(startup_pipes[j]); | |
916 | close_listen_socks(); | |
917 | sock_in = newsock; | |
918 | sock_out = newsock; | |
919 | log_init(av0, options.log_level, options.log_facility, log_stderr); | |
920 | break; | |
921 | } | |
5260325f | 922 | } |
5260325f | 923 | |
089fbbd2 | 924 | /* Parent. Stay in the loop. */ |
925 | if (pid < 0) | |
926 | error("fork: %.100s", strerror(errno)); | |
927 | else | |
928 | debug("Forked child %d.", pid); | |
5260325f | 929 | |
089fbbd2 | 930 | close(startup_p[1]); |
5260325f | 931 | |
089fbbd2 | 932 | /* Mark that the key has been used (it was "given" to the child). */ |
933 | key_used = 1; | |
5260325f | 934 | |
089fbbd2 | 935 | arc4random_stir(); |
936 | ||
937 | /* Close the new socket (the child is now taking care of it). */ | |
938 | close(newsock); | |
939 | } | |
48e671d5 | 940 | /* child process check (or debug mode) */ |
941 | if (num_listen_socks < 0) | |
942 | break; | |
5260325f | 943 | } |
944 | } | |
8efc0c15 | 945 | |
5260325f | 946 | /* This is the child processing a new connection. */ |
947 | ||
aa3378df | 948 | /* |
949 | * Disable the key regeneration alarm. We will not regenerate the | |
950 | * key since we are no longer in a position to give it to anyone. We | |
951 | * will not restart on SIGHUP since it no longer makes sense. | |
952 | */ | |
5260325f | 953 | alarm(0); |
954 | signal(SIGALRM, SIG_DFL); | |
955 | signal(SIGHUP, SIG_DFL); | |
956 | signal(SIGTERM, SIG_DFL); | |
957 | signal(SIGQUIT, SIG_DFL); | |
958 | signal(SIGCHLD, SIG_DFL); | |
959 | ||
aa3378df | 960 | /* |
961 | * Set socket options for the connection. We want the socket to | |
962 | * close as fast as possible without waiting for anything. If the | |
963 | * connection is not a socket, these will do nothing. | |
964 | */ | |
965 | /* setsockopt(sock_in, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */ | |
5260325f | 966 | linger.l_onoff = 1; |
967 | linger.l_linger = 5; | |
968 | setsockopt(sock_in, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger)); | |
969 | ||
aa3378df | 970 | /* |
971 | * Register our connection. This turns encryption off because we do | |
972 | * not have a key. | |
973 | */ | |
5260325f | 974 | packet_set_connection(sock_in, sock_out); |
975 | ||
976 | remote_port = get_remote_port(); | |
977 | remote_ip = get_remote_ipaddr(); | |
978 | ||
979 | /* Check whether logins are denied from this host. */ | |
980 | #ifdef LIBWRAP | |
48e671d5 | 981 | /* XXX LIBWRAP noes not know about IPv6 */ |
5260325f | 982 | { |
983 | struct request_info req; | |
8efc0c15 | 984 | |
5260325f | 985 | request_init(&req, RQ_DAEMON, av0, RQ_FILE, sock_in, NULL); |
986 | fromhost(&req); | |
8efc0c15 | 987 | |
5260325f | 988 | if (!hosts_access(&req)) { |
989 | close(sock_in); | |
990 | close(sock_out); | |
991 | refuse(&req); | |
992 | } | |
48e671d5 | 993 | /*XXX IPv6 verbose("Connection from %.500s port %d", eval_client(&req), remote_port); */ |
8efc0c15 | 994 | } |
48e671d5 | 995 | #endif /* LIBWRAP */ |
5260325f | 996 | /* Log the connection. */ |
997 | verbose("Connection from %.500s port %d", remote_ip, remote_port); | |
8efc0c15 | 998 | |
aa3378df | 999 | /* |
1000 | * We don\'t want to listen forever unless the other side | |
1001 | * successfully authenticates itself. So we set up an alarm which is | |
1002 | * cleared after successful authentication. A limit of zero | |
1003 | * indicates no limit. Note that we don\'t set the alarm in debugging | |
1004 | * mode; it is just annoying to have the server exit just when you | |
1005 | * are about to discover the bug. | |
1006 | */ | |
5260325f | 1007 | signal(SIGALRM, grace_alarm_handler); |
1008 | if (!debug_flag) | |
1009 | alarm(options.login_grace_time); | |
1010 | ||
7368a6c8 | 1011 | sshd_exchange_identification(sock_in, sock_out); |
aa3378df | 1012 | /* |
1013 | * Check that the connection comes from a privileged port. Rhosts- | |
1014 | * and Rhosts-RSA-Authentication only make sense from priviledged | |
1015 | * programs. Of course, if the intruder has root access on his local | |
1016 | * machine, he can connect from any port. So do not use these | |
1017 | * authentication methods from machines that you do not trust. | |
1018 | */ | |
5260325f | 1019 | if (remote_port >= IPPORT_RESERVED || |
1020 | remote_port < IPPORT_RESERVED / 2) { | |
1021 | options.rhosts_authentication = 0; | |
1022 | options.rhosts_rsa_authentication = 0; | |
1023 | } | |
48e671d5 | 1024 | #ifdef KRB4 |
1025 | if (!packet_connection_is_ipv4() && | |
1026 | options.kerberos_authentication) { | |
1027 | debug("Kerberos Authentication disabled, only available for IPv4."); | |
1028 | options.kerberos_authentication = 0; | |
1029 | } | |
1030 | #endif /* KRB4 */ | |
1031 | ||
5260325f | 1032 | packet_set_nonblocking(); |
1033 | ||
7b2ea3a1 | 1034 | /* perform the key exchange */ |
7b2ea3a1 | 1035 | /* authenticate user and start session */ |
e78a59f5 | 1036 | if (compat20) { |
1037 | do_ssh2_kex(); | |
1038 | do_authentication2(); | |
1039 | } else { | |
1040 | do_ssh1_kex(); | |
1041 | do_authentication(); | |
1042 | } | |
8efc0c15 | 1043 | |
1044 | #ifdef KRB4 | |
5260325f | 1045 | /* Cleanup user's ticket cache file. */ |
1046 | if (options.kerberos_ticket_cleanup) | |
1047 | (void) dest_tkt(); | |
8efc0c15 | 1048 | #endif /* KRB4 */ |
1049 | ||
5260325f | 1050 | /* The connection has been terminated. */ |
1051 | verbose("Closing connection to %.100s", remote_ip); | |
8efc0c15 | 1052 | |
d94aa2ae | 1053 | #ifdef USE_PAM |
a5c9cd31 | 1054 | finish_pam(); |
d94aa2ae | 1055 | #endif /* USE_PAM */ |
8efc0c15 | 1056 | |
5260325f | 1057 | packet_close(); |
1058 | exit(0); | |
1059 | } | |
8efc0c15 | 1060 | |
5260325f | 1061 | /* |
7b2ea3a1 | 1062 | * SSH1 key exchange |
5260325f | 1063 | */ |
e7c0f9d5 | 1064 | void |
7368a6c8 | 1065 | do_ssh1_kex() |
8efc0c15 | 1066 | { |
5260325f | 1067 | int i, len; |
7b2ea3a1 | 1068 | int plen, slen; |
5260325f | 1069 | BIGNUM *session_key_int; |
1070 | unsigned char session_key[SSH_SESSION_KEY_LENGTH]; | |
7b2ea3a1 | 1071 | unsigned char cookie[8]; |
5260325f | 1072 | unsigned int cipher_type, auth_mask, protocol_flags; |
5260325f | 1073 | u_int32_t rand = 0; |
1074 | ||
aa3378df | 1075 | /* |
1076 | * Generate check bytes that the client must send back in the user | |
1077 | * packet in order for it to be accepted; this is used to defy ip | |
1078 | * spoofing attacks. Note that this only works against somebody | |
1079 | * doing IP spoofing from a remote machine; any machine on the local | |
1080 | * network can still see outgoing packets and catch the random | |
1081 | * cookie. This only affects rhosts authentication, and this is one | |
1082 | * of the reasons why it is inherently insecure. | |
1083 | */ | |
5260325f | 1084 | for (i = 0; i < 8; i++) { |
1085 | if (i % 4 == 0) | |
1086 | rand = arc4random(); | |
7b2ea3a1 | 1087 | cookie[i] = rand & 0xff; |
5260325f | 1088 | rand >>= 8; |
1089 | } | |
1090 | ||
aa3378df | 1091 | /* |
1092 | * Send our public key. We include in the packet 64 bits of random | |
1093 | * data that must be matched in the reply in order to prevent IP | |
1094 | * spoofing. | |
1095 | */ | |
5260325f | 1096 | packet_start(SSH_SMSG_PUBLIC_KEY); |
1097 | for (i = 0; i < 8; i++) | |
7b2ea3a1 | 1098 | packet_put_char(cookie[i]); |
5260325f | 1099 | |
1100 | /* Store our public server RSA key. */ | |
1101 | packet_put_int(BN_num_bits(public_key->n)); | |
1102 | packet_put_bignum(public_key->e); | |
1103 | packet_put_bignum(public_key->n); | |
1104 | ||
1105 | /* Store our public host RSA key. */ | |
1106 | packet_put_int(BN_num_bits(sensitive_data.host_key->n)); | |
1107 | packet_put_bignum(sensitive_data.host_key->e); | |
1108 | packet_put_bignum(sensitive_data.host_key->n); | |
1109 | ||
1110 | /* Put protocol flags. */ | |
1111 | packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN); | |
1112 | ||
1113 | /* Declare which ciphers we support. */ | |
8ce64345 | 1114 | packet_put_int(cipher_mask1()); |
5260325f | 1115 | |
1116 | /* Declare supported authentication types. */ | |
1117 | auth_mask = 0; | |
1118 | if (options.rhosts_authentication) | |
1119 | auth_mask |= 1 << SSH_AUTH_RHOSTS; | |
1120 | if (options.rhosts_rsa_authentication) | |
1121 | auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA; | |
1122 | if (options.rsa_authentication) | |
1123 | auth_mask |= 1 << SSH_AUTH_RSA; | |
8efc0c15 | 1124 | #ifdef KRB4 |
5260325f | 1125 | if (options.kerberos_authentication) |
1126 | auth_mask |= 1 << SSH_AUTH_KERBEROS; | |
8efc0c15 | 1127 | #endif |
1128 | #ifdef AFS | |
5260325f | 1129 | if (options.kerberos_tgt_passing) |
1130 | auth_mask |= 1 << SSH_PASS_KERBEROS_TGT; | |
1131 | if (options.afs_token_passing) | |
1132 | auth_mask |= 1 << SSH_PASS_AFS_TOKEN; | |
8efc0c15 | 1133 | #endif |
5260325f | 1134 | #ifdef SKEY |
1135 | if (options.skey_authentication == 1) | |
1136 | auth_mask |= 1 << SSH_AUTH_TIS; | |
1137 | #endif | |
1138 | if (options.password_authentication) | |
1139 | auth_mask |= 1 << SSH_AUTH_PASSWORD; | |
1140 | packet_put_int(auth_mask); | |
1141 | ||
1142 | /* Send the packet and wait for it to be sent. */ | |
1143 | packet_send(); | |
1144 | packet_write_wait(); | |
1145 | ||
1146 | debug("Sent %d bit public key and %d bit host key.", | |
1147 | BN_num_bits(public_key->n), BN_num_bits(sensitive_data.host_key->n)); | |
1148 | ||
1149 | /* Read clients reply (cipher type and session key). */ | |
1150 | packet_read_expect(&plen, SSH_CMSG_SESSION_KEY); | |
1151 | ||
2d86a6cc | 1152 | /* Get cipher type and check whether we accept this. */ |
5260325f | 1153 | cipher_type = packet_get_char(); |
1154 | ||
6ae2364d | 1155 | if (!(cipher_mask() & (1 << cipher_type))) |
2d86a6cc | 1156 | packet_disconnect("Warning: client selects unsupported cipher."); |
1157 | ||
5260325f | 1158 | /* Get check bytes from the packet. These must match those we |
1159 | sent earlier with the public key packet. */ | |
1160 | for (i = 0; i < 8; i++) | |
7b2ea3a1 | 1161 | if (cookie[i] != packet_get_char()) |
5260325f | 1162 | packet_disconnect("IP Spoofing check bytes do not match."); |
1163 | ||
1164 | debug("Encryption type: %.200s", cipher_name(cipher_type)); | |
1165 | ||
1166 | /* Get the encrypted integer. */ | |
1167 | session_key_int = BN_new(); | |
1168 | packet_get_bignum(session_key_int, &slen); | |
1169 | ||
5260325f | 1170 | protocol_flags = packet_get_int(); |
1171 | packet_set_protocol_flags(protocol_flags); | |
1172 | ||
1173 | packet_integrity_check(plen, 1 + 8 + slen + 4, SSH_CMSG_SESSION_KEY); | |
1174 | ||
aa3378df | 1175 | /* |
1176 | * Decrypt it using our private server key and private host key (key | |
1177 | * with larger modulus first). | |
1178 | */ | |
5260325f | 1179 | if (BN_cmp(sensitive_data.private_key->n, sensitive_data.host_key->n) > 0) { |
1180 | /* Private key has bigger modulus. */ | |
1181 | if (BN_num_bits(sensitive_data.private_key->n) < | |
1182 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) { | |
1183 | fatal("do_connection: %s: private_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d", | |
1184 | get_remote_ipaddr(), | |
1185 | BN_num_bits(sensitive_data.private_key->n), | |
1186 | BN_num_bits(sensitive_data.host_key->n), | |
1187 | SSH_KEY_BITS_RESERVED); | |
1188 | } | |
1189 | rsa_private_decrypt(session_key_int, session_key_int, | |
1190 | sensitive_data.private_key); | |
1191 | rsa_private_decrypt(session_key_int, session_key_int, | |
1192 | sensitive_data.host_key); | |
1193 | } else { | |
1194 | /* Host key has bigger modulus (or they are equal). */ | |
1195 | if (BN_num_bits(sensitive_data.host_key->n) < | |
1196 | BN_num_bits(sensitive_data.private_key->n) + SSH_KEY_BITS_RESERVED) { | |
1197 | fatal("do_connection: %s: host_key %d < private_key %d + SSH_KEY_BITS_RESERVED %d", | |
1198 | get_remote_ipaddr(), | |
1199 | BN_num_bits(sensitive_data.host_key->n), | |
1200 | BN_num_bits(sensitive_data.private_key->n), | |
1201 | SSH_KEY_BITS_RESERVED); | |
1202 | } | |
1203 | rsa_private_decrypt(session_key_int, session_key_int, | |
1204 | sensitive_data.host_key); | |
1205 | rsa_private_decrypt(session_key_int, session_key_int, | |
1206 | sensitive_data.private_key); | |
1207 | } | |
1208 | ||
7b2ea3a1 | 1209 | compute_session_id(session_id, cookie, |
5260325f | 1210 | sensitive_data.host_key->n, |
1211 | sensitive_data.private_key->n); | |
1212 | ||
7b2ea3a1 | 1213 | /* Destroy the private and public keys. They will no longer be needed. */ |
a306f2dd | 1214 | destroy_sensitive_data(); |
7b2ea3a1 | 1215 | |
aa3378df | 1216 | /* |
1217 | * Extract session key from the decrypted integer. The key is in the | |
1218 | * least significant 256 bits of the integer; the first byte of the | |
1219 | * key is in the highest bits. | |
1220 | */ | |
5260325f | 1221 | BN_mask_bits(session_key_int, sizeof(session_key) * 8); |
1222 | len = BN_num_bytes(session_key_int); | |
1223 | if (len < 0 || len > sizeof(session_key)) | |
1224 | fatal("do_connection: bad len from %s: session_key_int %d > sizeof(session_key) %d", | |
1225 | get_remote_ipaddr(), | |
1226 | len, sizeof(session_key)); | |
1227 | memset(session_key, 0, sizeof(session_key)); | |
1228 | BN_bn2bin(session_key_int, session_key + sizeof(session_key) - len); | |
1229 | ||
7b2ea3a1 | 1230 | /* Destroy the decrypted integer. It is no longer needed. */ |
1231 | BN_clear_free(session_key_int); | |
1232 | ||
5260325f | 1233 | /* Xor the first 16 bytes of the session key with the session id. */ |
1234 | for (i = 0; i < 16; i++) | |
1235 | session_key[i] ^= session_id[i]; | |
1236 | ||
5260325f | 1237 | /* Set the session key. From this on all communications will be encrypted. */ |
1238 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type); | |
1239 | ||
1240 | /* Destroy our copy of the session key. It is no longer needed. */ | |
1241 | memset(session_key, 0, sizeof(session_key)); | |
1242 | ||
1243 | debug("Received session key; encryption turned on."); | |
1244 | ||
1245 | /* Send an acknowledgement packet. Note that this packet is sent encrypted. */ | |
1246 | packet_start(SSH_SMSG_SUCCESS); | |
1247 | packet_send(); | |
1248 | packet_write_wait(); | |
5260325f | 1249 | } |
e78a59f5 | 1250 | |
1251 | /* | |
1252 | * SSH2 key exchange: diffie-hellman-group1-sha1 | |
1253 | */ | |
1254 | void | |
1255 | do_ssh2_kex() | |
1256 | { | |
1257 | Buffer *server_kexinit; | |
1258 | Buffer *client_kexinit; | |
1259 | int payload_len, dlen; | |
1260 | int slen; | |
1261 | unsigned int klen, kout; | |
e78a59f5 | 1262 | unsigned char *signature = NULL; |
1263 | unsigned char *server_host_key_blob = NULL; | |
1264 | unsigned int sbloblen; | |
1265 | DH *dh; | |
1266 | BIGNUM *dh_client_pub = 0; | |
1267 | BIGNUM *shared_secret = 0; | |
1268 | int i; | |
1269 | unsigned char *kbuf; | |
1270 | unsigned char *hash; | |
1271 | Kex *kex; | |
e78a59f5 | 1272 | char *cprop[PROPOSAL_MAX]; |
e78a59f5 | 1273 | |
1274 | /* KEXINIT */ | |
1275 | ||
a8be9f80 | 1276 | if (options.ciphers != NULL) { |
6ae2364d | 1277 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
a8be9f80 | 1278 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; |
1279 | } | |
71276795 | 1280 | server_kexinit = kex_init(myproposal); |
e78a59f5 | 1281 | client_kexinit = xmalloc(sizeof(*client_kexinit)); |
1282 | buffer_init(client_kexinit); | |
e78a59f5 | 1283 | |
71276795 | 1284 | /* algorithm negotiation */ |
1285 | kex_exchange_kexinit(server_kexinit, client_kexinit, cprop); | |
1286 | kex = kex_choose_conf(cprop, myproposal, 1); | |
1287 | for (i = 0; i < PROPOSAL_MAX; i++) | |
1288 | xfree(cprop[i]); | |
e78a59f5 | 1289 | |
1290 | /* KEXDH */ | |
1291 | ||
1292 | debug("Wait SSH2_MSG_KEXDH_INIT."); | |
1293 | packet_read_expect(&payload_len, SSH2_MSG_KEXDH_INIT); | |
1294 | ||
1295 | /* key, cert */ | |
1296 | dh_client_pub = BN_new(); | |
1297 | if (dh_client_pub == NULL) | |
1298 | fatal("dh_client_pub == NULL"); | |
1299 | packet_get_bignum2(dh_client_pub, &dlen); | |
1300 | ||
1301 | #ifdef DEBUG_KEXDH | |
1302 | fprintf(stderr, "\ndh_client_pub= "); | |
1303 | bignum_print(dh_client_pub); | |
1304 | fprintf(stderr, "\n"); | |
1305 | debug("bits %d", BN_num_bits(dh_client_pub)); | |
1306 | #endif | |
1307 | ||
1308 | /* generate DH key */ | |
a8be9f80 | 1309 | dh = dh_new_group1(); /* XXX depends on 'kex' */ |
e78a59f5 | 1310 | |
1311 | #ifdef DEBUG_KEXDH | |
1312 | fprintf(stderr, "\np= "); | |
1313 | bignum_print(dh->p); | |
1314 | fprintf(stderr, "\ng= "); | |
1315 | bignum_print(dh->g); | |
1316 | fprintf(stderr, "\npub= "); | |
1317 | bignum_print(dh->pub_key); | |
1318 | fprintf(stderr, "\n"); | |
1319 | #endif | |
a8be9f80 | 1320 | if (!dh_pub_is_valid(dh, dh_client_pub)) |
1321 | packet_disconnect("bad client public DH value"); | |
e78a59f5 | 1322 | |
1323 | klen = DH_size(dh); | |
1324 | kbuf = xmalloc(klen); | |
1325 | kout = DH_compute_key(kbuf, dh_client_pub, dh); | |
1326 | ||
1327 | #ifdef DEBUG_KEXDH | |
1328 | debug("shared secret: len %d/%d", klen, kout); | |
1329 | fprintf(stderr, "shared secret == "); | |
1330 | for (i = 0; i< kout; i++) | |
1331 | fprintf(stderr, "%02x", (kbuf[i])&0xff); | |
1332 | fprintf(stderr, "\n"); | |
1333 | #endif | |
1334 | shared_secret = BN_new(); | |
1335 | ||
1336 | BN_bin2bn(kbuf, kout, shared_secret); | |
1337 | memset(kbuf, 0, klen); | |
1338 | xfree(kbuf); | |
1339 | ||
a306f2dd | 1340 | /* XXX precompute? */ |
1341 | dsa_make_key_blob(sensitive_data.dsa_host_key, &server_host_key_blob, &sbloblen); | |
e78a59f5 | 1342 | |
1343 | /* calc H */ /* XXX depends on 'kex' */ | |
1344 | hash = kex_hash( | |
1345 | client_version_string, | |
1346 | server_version_string, | |
1347 | buffer_ptr(client_kexinit), buffer_len(client_kexinit), | |
1348 | buffer_ptr(server_kexinit), buffer_len(server_kexinit), | |
1349 | (char *)server_host_key_blob, sbloblen, | |
1350 | dh_client_pub, | |
1351 | dh->pub_key, | |
1352 | shared_secret | |
1353 | ); | |
1354 | buffer_free(client_kexinit); | |
1355 | buffer_free(server_kexinit); | |
1356 | xfree(client_kexinit); | |
1357 | xfree(server_kexinit); | |
1358 | #ifdef DEBUG_KEXDH | |
6ae2364d | 1359 | fprintf(stderr, "hash == "); |
1360 | for (i = 0; i< 20; i++) | |
1361 | fprintf(stderr, "%02x", (hash[i])&0xff); | |
1362 | fprintf(stderr, "\n"); | |
e78a59f5 | 1363 | #endif |
a306f2dd | 1364 | /* save session id := H */ |
1365 | /* XXX hashlen depends on KEX */ | |
1366 | session_id2_len = 20; | |
1367 | session_id2 = xmalloc(session_id2_len); | |
1368 | memcpy(session_id2, hash, session_id2_len); | |
1369 | ||
e78a59f5 | 1370 | /* sign H */ |
a306f2dd | 1371 | /* XXX hashlen depends on KEX */ |
1372 | dsa_sign(sensitive_data.dsa_host_key, &signature, &slen, hash, 20); | |
1373 | ||
1374 | destroy_sensitive_data(); | |
e78a59f5 | 1375 | |
1376 | /* send server hostkey, DH pubkey 'f' and singed H */ | |
1377 | packet_start(SSH2_MSG_KEXDH_REPLY); | |
1378 | packet_put_string((char *)server_host_key_blob, sbloblen); | |
1d1ffb87 | 1379 | packet_put_bignum2(dh->pub_key); /* f */ |
e78a59f5 | 1380 | packet_put_string((char *)signature, slen); |
1381 | packet_send(); | |
d6f24e45 | 1382 | xfree(signature); |
a306f2dd | 1383 | xfree(server_host_key_blob); |
e78a59f5 | 1384 | packet_write_wait(); |
1385 | ||
1386 | kex_derive_keys(kex, hash, shared_secret); | |
1387 | packet_set_kex(kex); | |
1388 | ||
1389 | /* have keys, free DH */ | |
1390 | DH_free(dh); | |
1391 | ||
1392 | debug("send SSH2_MSG_NEWKEYS."); | |
1393 | packet_start(SSH2_MSG_NEWKEYS); | |
1394 | packet_send(); | |
1395 | packet_write_wait(); | |
1396 | debug("done: send SSH2_MSG_NEWKEYS."); | |
1397 | ||
1398 | debug("Wait SSH2_MSG_NEWKEYS."); | |
1399 | packet_read_expect(&payload_len, SSH2_MSG_NEWKEYS); | |
1400 | debug("GOT SSH2_MSG_NEWKEYS."); | |
1401 | ||
a8be9f80 | 1402 | #ifdef DEBUG_KEXDH |
e78a59f5 | 1403 | /* send 1st encrypted/maced/compressed message */ |
1404 | packet_start(SSH2_MSG_IGNORE); | |
1405 | packet_put_cstring("markus"); | |
1406 | packet_send(); | |
1407 | packet_write_wait(); | |
a8be9f80 | 1408 | #endif |
e78a59f5 | 1409 | debug("done: KEX2."); |
1410 | } |