]>
Commit | Line | Data |
---|---|---|
3c0ef626 | 1 | /* |
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 | * This program is the ssh daemon. It listens for connections from clients, | |
6 | * and performs authentication, executes use commands or shell, and forwards | |
7 | * information to/from the application to the user client over an encrypted | |
8 | * connection. This can also handle forwarding of X11, TCP/IP, and | |
9 | * authentication agent connections. | |
10 | * | |
11 | * As far as I am concerned, the code I have written for this software | |
12 | * can be used freely for any purpose. Any derived versions of this | |
13 | * software must be clearly marked as such, and if the derived work is | |
14 | * incompatible with the protocol description in the RFC file, it must be | |
15 | * called by a name other than "ssh" or "Secure Shell". | |
16 | * | |
17 | * SSH2 implementation: | |
700318f3 | 18 | * Privilege Separation: |
3c0ef626 | 19 | * |
700318f3 | 20 | * Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved. |
21 | * Copyright (c) 2002 Niels Provos. All rights reserved. | |
3c0ef626 | 22 | * |
23 | * Redistribution and use in source and binary forms, with or without | |
24 | * modification, are permitted provided that the following conditions | |
25 | * are met: | |
26 | * 1. Redistributions of source code must retain the above copyright | |
27 | * notice, this list of conditions and the following disclaimer. | |
28 | * 2. Redistributions in binary form must reproduce the above copyright | |
29 | * notice, this list of conditions and the following disclaimer in the | |
30 | * documentation and/or other materials provided with the distribution. | |
31 | * | |
32 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
33 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
34 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
35 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
36 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
37 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
38 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
39 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
40 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
41 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
42 | */ | |
43 | ||
44 | #include "includes.h" | |
6a9b3198 | 45 | RCSID("$OpenBSD: sshd.c,v 1.263 2003/02/16 17:09:57 markus Exp $"); |
3c0ef626 | 46 | |
47 | #include <openssl/dh.h> | |
48 | #include <openssl/bn.h> | |
e9a17296 | 49 | #include <openssl/md5.h> |
700318f3 | 50 | #include <openssl/rand.h> |
51 | #ifdef HAVE_SECUREWARE | |
52 | #include <sys/security.h> | |
53 | #include <prot.h> | |
54 | #endif | |
3c0ef626 | 55 | |
56 | #include "ssh.h" | |
57 | #include "ssh1.h" | |
58 | #include "ssh2.h" | |
59 | #include "xmalloc.h" | |
60 | #include "rsa.h" | |
61 | #include "sshpty.h" | |
62 | #include "packet.h" | |
63 | #include "mpaux.h" | |
64 | #include "log.h" | |
65 | #include "servconf.h" | |
66 | #include "uidswap.h" | |
67 | #include "compat.h" | |
68 | #include "buffer.h" | |
69 | #include "cipher.h" | |
70 | #include "kex.h" | |
71 | #include "key.h" | |
72 | #include "dh.h" | |
73 | #include "myproposal.h" | |
74 | #include "authfile.h" | |
75 | #include "pathnames.h" | |
76 | #include "atomicio.h" | |
77 | #include "canohost.h" | |
78 | #include "auth.h" | |
79 | #include "misc.h" | |
80 | #include "dispatch.h" | |
81 | #include "channels.h" | |
700318f3 | 82 | #include "session.h" |
83 | #include "monitor_mm.h" | |
84 | #include "monitor.h" | |
85 | #include "monitor_wrap.h" | |
86 | #include "monitor_fdpass.h" | |
3c0ef626 | 87 | |
c0fc5818 | 88 | #ifdef GSSAPI |
89 | #include "ssh-gss.h" | |
90 | #endif | |
91 | ||
3c0ef626 | 92 | #ifdef LIBWRAP |
93 | #include <tcpd.h> | |
94 | #include <syslog.h> | |
95 | int allow_severity = LOG_INFO; | |
96 | int deny_severity = LOG_WARNING; | |
97 | #endif /* LIBWRAP */ | |
98 | ||
99 | #ifndef O_NOCTTY | |
100 | #define O_NOCTTY 0 | |
101 | #endif | |
102 | ||
103 | #ifdef HAVE___PROGNAME | |
104 | extern char *__progname; | |
105 | #else | |
106 | char *__progname; | |
107 | #endif | |
108 | ||
109 | /* Server configuration options. */ | |
110 | ServerOptions options; | |
111 | ||
112 | /* Name of the server configuration file. */ | |
113 | char *config_file_name = _PATH_SERVER_CONFIG_FILE; | |
114 | ||
115 | /* | |
116 | * Flag indicating whether IPv4 or IPv6. This can be set on the command line. | |
117 | * Default value is AF_UNSPEC means both IPv4 and IPv6. | |
118 | */ | |
119 | #ifdef IPV4_DEFAULT | |
120 | int IPv4or6 = AF_INET; | |
121 | #else | |
122 | int IPv4or6 = AF_UNSPEC; | |
123 | #endif | |
124 | ||
125 | /* | |
126 | * Debug mode flag. This can be set on the command line. If debug | |
127 | * mode is enabled, extra debugging output will be sent to the system | |
128 | * log, the daemon will not go to background, and will exit after processing | |
129 | * the first connection. | |
130 | */ | |
131 | int debug_flag = 0; | |
132 | ||
133 | /* Flag indicating that the daemon should only test the configuration and keys. */ | |
134 | int test_flag = 0; | |
135 | ||
136 | /* Flag indicating that the daemon is being started from inetd. */ | |
137 | int inetd_flag = 0; | |
138 | ||
139 | /* Flag indicating that sshd should not detach and become a daemon. */ | |
140 | int no_daemon_flag = 0; | |
141 | ||
142 | /* debug goes to stderr unless inetd_flag is set */ | |
143 | int log_stderr = 0; | |
144 | ||
145 | /* Saved arguments to main(). */ | |
146 | char **saved_argv; | |
147 | int saved_argc; | |
148 | ||
149 | /* | |
150 | * The sockets that the server is listening; this is used in the SIGHUP | |
151 | * signal handler. | |
152 | */ | |
153 | #define MAX_LISTEN_SOCKS 16 | |
154 | int listen_socks[MAX_LISTEN_SOCKS]; | |
155 | int num_listen_socks = 0; | |
156 | ||
157 | /* | |
158 | * the client's version string, passed by sshd2 in compat mode. if != NULL, | |
159 | * sshd will skip the version-number exchange | |
160 | */ | |
161 | char *client_version_string = NULL; | |
162 | char *server_version_string = NULL; | |
163 | ||
164 | /* for rekeying XXX fixme */ | |
165 | Kex *xxx_kex; | |
166 | ||
167 | /* | |
168 | * Any really sensitive data in the application is contained in this | |
169 | * structure. The idea is that this structure could be locked into memory so | |
170 | * that the pages do not get written into swap. However, there are some | |
171 | * problems. The private key contains BIGNUMs, and we do not (in principle) | |
172 | * have access to the internals of them, and locking just the structure is | |
173 | * not very useful. Currently, memory locking is not implemented. | |
174 | */ | |
175 | struct { | |
176 | Key *server_key; /* ephemeral server key */ | |
177 | Key *ssh1_host_key; /* ssh1 host key */ | |
178 | Key **host_keys; /* all private host keys */ | |
179 | int have_ssh1_key; | |
180 | int have_ssh2_key; | |
181 | u_char ssh1_cookie[SSH_SESSION_KEY_LENGTH]; | |
182 | } sensitive_data; | |
183 | ||
184 | /* | |
185 | * Flag indicating whether the RSA server key needs to be regenerated. | |
186 | * Is set in the SIGALRM handler and cleared when the key is regenerated. | |
187 | */ | |
e9a17296 | 188 | static volatile sig_atomic_t key_do_regen = 0; |
3c0ef626 | 189 | |
190 | /* This is set to true when a signal is received. */ | |
e9a17296 | 191 | static volatile sig_atomic_t received_sighup = 0; |
192 | static volatile sig_atomic_t received_sigterm = 0; | |
3c0ef626 | 193 | |
194 | /* session identifier, used by RSA-auth */ | |
195 | u_char session_id[16]; | |
196 | ||
197 | /* same for ssh2 */ | |
198 | u_char *session_id2 = NULL; | |
199 | int session_id2_len = 0; | |
200 | ||
201 | /* record remote hostname or ip */ | |
202 | u_int utmp_len = MAXHOSTNAMELEN; | |
203 | ||
e9a17296 | 204 | /* options.max_startup sized array of fd ints */ |
205 | int *startup_pipes = NULL; | |
206 | int startup_pipe; /* in child */ | |
207 | ||
700318f3 | 208 | /* variables used for privilege separation */ |
6a9b3198 | 209 | int use_privsep; |
210 | struct monitor *pmonitor; | |
700318f3 | 211 | |
3c0ef626 | 212 | /* Prototypes for various functions defined later in this file. */ |
213 | void destroy_sensitive_data(void); | |
700318f3 | 214 | void demote_sensitive_data(void); |
3c0ef626 | 215 | |
216 | static void do_ssh1_kex(void); | |
217 | static void do_ssh2_kex(void); | |
218 | ||
219 | /* | |
220 | * Close all listening sockets | |
221 | */ | |
222 | static void | |
223 | close_listen_socks(void) | |
224 | { | |
225 | int i; | |
680cee3b | 226 | |
3c0ef626 | 227 | for (i = 0; i < num_listen_socks; i++) |
228 | close(listen_socks[i]); | |
229 | num_listen_socks = -1; | |
230 | } | |
231 | ||
e9a17296 | 232 | static void |
233 | close_startup_pipes(void) | |
234 | { | |
235 | int i; | |
680cee3b | 236 | |
e9a17296 | 237 | if (startup_pipes) |
238 | for (i = 0; i < options.max_startups; i++) | |
239 | if (startup_pipes[i] != -1) | |
240 | close(startup_pipes[i]); | |
241 | } | |
242 | ||
3c0ef626 | 243 | /* |
244 | * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP; | |
245 | * the effect is to reread the configuration file (and to regenerate | |
246 | * the server key). | |
247 | */ | |
248 | static void | |
249 | sighup_handler(int sig) | |
250 | { | |
e9a17296 | 251 | int save_errno = errno; |
252 | ||
3c0ef626 | 253 | received_sighup = 1; |
254 | signal(SIGHUP, sighup_handler); | |
e9a17296 | 255 | errno = save_errno; |
3c0ef626 | 256 | } |
257 | ||
258 | /* | |
259 | * Called from the main program after receiving SIGHUP. | |
260 | * Restarts the server. | |
261 | */ | |
262 | static void | |
263 | sighup_restart(void) | |
264 | { | |
265 | log("Received SIGHUP; restarting."); | |
266 | close_listen_socks(); | |
e9a17296 | 267 | close_startup_pipes(); |
3c0ef626 | 268 | execv(saved_argv[0], saved_argv); |
680cee3b | 269 | log("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0], |
270 | strerror(errno)); | |
3c0ef626 | 271 | exit(1); |
272 | } | |
273 | ||
274 | /* | |
275 | * Generic signal handler for terminating signals in the master daemon. | |
276 | */ | |
277 | static void | |
278 | sigterm_handler(int sig) | |
279 | { | |
280 | received_sigterm = sig; | |
281 | } | |
282 | ||
283 | /* | |
284 | * SIGCHLD handler. This is called whenever a child dies. This will then | |
285 | * reap any zombies left by exited children. | |
286 | */ | |
287 | static void | |
288 | main_sigchld_handler(int sig) | |
289 | { | |
290 | int save_errno = errno; | |
680cee3b | 291 | pid_t pid; |
3c0ef626 | 292 | int status; |
293 | ||
700318f3 | 294 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || |
295 | (pid < 0 && errno == EINTR)) | |
3c0ef626 | 296 | ; |
297 | ||
298 | signal(SIGCHLD, main_sigchld_handler); | |
299 | errno = save_errno; | |
300 | } | |
301 | ||
302 | /* | |
303 | * Signal handler for the alarm after the login grace period has expired. | |
304 | */ | |
305 | static void | |
306 | grace_alarm_handler(int sig) | |
307 | { | |
308 | /* XXX no idea how fix this signal handler */ | |
309 | ||
3c0ef626 | 310 | /* Log error and exit. */ |
41b2f314 | 311 | fatal("Timeout before authentication for %s", get_remote_ipaddr()); |
3c0ef626 | 312 | } |
313 | ||
314 | /* | |
315 | * Signal handler for the key regeneration alarm. Note that this | |
316 | * alarm only occurs in the daemon waiting for connections, and it does not | |
317 | * do anything with the private key or random state before forking. | |
318 | * Thus there should be no concurrency control/asynchronous execution | |
319 | * problems. | |
320 | */ | |
321 | static void | |
322 | generate_ephemeral_server_key(void) | |
323 | { | |
41b2f314 | 324 | u_int32_t rnd = 0; |
3c0ef626 | 325 | int i; |
326 | ||
327 | verbose("Generating %s%d bit RSA key.", | |
328 | sensitive_data.server_key ? "new " : "", options.server_key_bits); | |
329 | if (sensitive_data.server_key != NULL) | |
330 | key_free(sensitive_data.server_key); | |
331 | sensitive_data.server_key = key_generate(KEY_RSA1, | |
332 | options.server_key_bits); | |
333 | verbose("RSA key generation complete."); | |
334 | ||
335 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { | |
336 | if (i % 4 == 0) | |
41b2f314 | 337 | rnd = arc4random(); |
338 | sensitive_data.ssh1_cookie[i] = rnd & 0xff; | |
339 | rnd >>= 8; | |
3c0ef626 | 340 | } |
341 | arc4random_stir(); | |
342 | } | |
343 | ||
344 | static void | |
345 | key_regeneration_alarm(int sig) | |
346 | { | |
347 | int save_errno = errno; | |
680cee3b | 348 | |
3c0ef626 | 349 | signal(SIGALRM, SIG_DFL); |
350 | errno = save_errno; | |
351 | key_do_regen = 1; | |
352 | } | |
353 | ||
354 | static void | |
355 | sshd_exchange_identification(int sock_in, int sock_out) | |
356 | { | |
357 | int i, mismatch; | |
358 | int remote_major, remote_minor; | |
359 | int major, minor; | |
360 | char *s; | |
361 | char buf[256]; /* Must not be larger than remote_version. */ | |
362 | char remote_version[256]; /* Must be at least as big as buf. */ | |
363 | ||
364 | if ((options.protocol & SSH_PROTO_1) && | |
365 | (options.protocol & SSH_PROTO_2)) { | |
366 | major = PROTOCOL_MAJOR_1; | |
367 | minor = 99; | |
368 | } else if (options.protocol & SSH_PROTO_2) { | |
369 | major = PROTOCOL_MAJOR_2; | |
370 | minor = PROTOCOL_MINOR_2; | |
371 | } else { | |
372 | major = PROTOCOL_MAJOR_1; | |
373 | minor = PROTOCOL_MINOR_1; | |
374 | } | |
375 | snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION); | |
376 | server_version_string = xstrdup(buf); | |
377 | ||
378 | if (client_version_string == NULL) { | |
379 | /* Send our protocol version identification. */ | |
680cee3b | 380 | if (atomicio(write, sock_out, server_version_string, |
381 | strlen(server_version_string)) | |
3c0ef626 | 382 | != strlen(server_version_string)) { |
383 | log("Could not write ident string to %s", get_remote_ipaddr()); | |
384 | fatal_cleanup(); | |
385 | } | |
386 | ||
f5799ae1 | 387 | /* Read other sides version identification. */ |
3c0ef626 | 388 | memset(buf, 0, sizeof(buf)); |
389 | for (i = 0; i < sizeof(buf) - 1; i++) { | |
390 | if (atomicio(read, sock_in, &buf[i], 1) != 1) { | |
391 | log("Did not receive identification string from %s", | |
392 | get_remote_ipaddr()); | |
393 | fatal_cleanup(); | |
394 | } | |
395 | if (buf[i] == '\r') { | |
396 | buf[i] = 0; | |
397 | /* Kludge for F-Secure Macintosh < 1.0.2 */ | |
398 | if (i == 12 && | |
399 | strncmp(buf, "SSH-1.5-W1.0", 12) == 0) | |
400 | break; | |
401 | continue; | |
402 | } | |
403 | if (buf[i] == '\n') { | |
404 | buf[i] = 0; | |
405 | break; | |
406 | } | |
407 | } | |
408 | buf[sizeof(buf) - 1] = 0; | |
409 | client_version_string = xstrdup(buf); | |
410 | } | |
411 | ||
412 | /* | |
413 | * Check that the versions match. In future this might accept | |
414 | * several versions and set appropriate flags to handle them. | |
415 | */ | |
416 | if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n", | |
417 | &remote_major, &remote_minor, remote_version) != 3) { | |
418 | s = "Protocol mismatch.\n"; | |
419 | (void) atomicio(write, sock_out, s, strlen(s)); | |
420 | close(sock_in); | |
421 | close(sock_out); | |
422 | log("Bad protocol version identification '%.100s' from %s", | |
423 | client_version_string, get_remote_ipaddr()); | |
424 | fatal_cleanup(); | |
425 | } | |
426 | debug("Client protocol version %d.%d; client software version %.100s", | |
e9a17296 | 427 | remote_major, remote_minor, remote_version); |
3c0ef626 | 428 | |
429 | compat_datafellows(remote_version); | |
430 | ||
41b2f314 | 431 | if (datafellows & SSH_BUG_PROBE) { |
432 | log("probed from %s with %s. Don't panic.", | |
433 | get_remote_ipaddr(), client_version_string); | |
434 | fatal_cleanup(); | |
435 | } | |
436 | ||
3c0ef626 | 437 | if (datafellows & SSH_BUG_SCANNER) { |
438 | log("scanned from %s with %s. Don't panic.", | |
439 | get_remote_ipaddr(), client_version_string); | |
440 | fatal_cleanup(); | |
441 | } | |
442 | ||
443 | mismatch = 0; | |
e9a17296 | 444 | switch (remote_major) { |
3c0ef626 | 445 | case 1: |
446 | if (remote_minor == 99) { | |
447 | if (options.protocol & SSH_PROTO_2) | |
448 | enable_compat20(); | |
449 | else | |
450 | mismatch = 1; | |
451 | break; | |
452 | } | |
453 | if (!(options.protocol & SSH_PROTO_1)) { | |
454 | mismatch = 1; | |
455 | break; | |
456 | } | |
457 | if (remote_minor < 3) { | |
458 | packet_disconnect("Your ssh version is too old and " | |
459 | "is no longer supported. Please install a newer version."); | |
460 | } else if (remote_minor == 3) { | |
461 | /* note that this disables agent-forwarding */ | |
462 | enable_compat13(); | |
463 | } | |
464 | break; | |
465 | case 2: | |
466 | if (options.protocol & SSH_PROTO_2) { | |
467 | enable_compat20(); | |
468 | break; | |
469 | } | |
470 | /* FALLTHROUGH */ | |
471 | default: | |
472 | mismatch = 1; | |
473 | break; | |
474 | } | |
475 | chop(server_version_string); | |
476 | debug("Local version string %.200s", server_version_string); | |
477 | ||
478 | if (mismatch) { | |
479 | s = "Protocol major versions differ.\n"; | |
480 | (void) atomicio(write, sock_out, s, strlen(s)); | |
481 | close(sock_in); | |
482 | close(sock_out); | |
483 | log("Protocol major versions differ for %s: %.200s vs. %.200s", | |
484 | get_remote_ipaddr(), | |
485 | server_version_string, client_version_string); | |
486 | fatal_cleanup(); | |
487 | } | |
488 | } | |
489 | ||
3c0ef626 | 490 | /* Destroy the host and server keys. They will no longer be needed. */ |
491 | void | |
492 | destroy_sensitive_data(void) | |
493 | { | |
494 | int i; | |
495 | ||
496 | if (sensitive_data.server_key) { | |
497 | key_free(sensitive_data.server_key); | |
498 | sensitive_data.server_key = NULL; | |
499 | } | |
e9a17296 | 500 | for (i = 0; i < options.num_host_key_files; i++) { |
3c0ef626 | 501 | if (sensitive_data.host_keys[i]) { |
502 | key_free(sensitive_data.host_keys[i]); | |
503 | sensitive_data.host_keys[i] = NULL; | |
504 | } | |
505 | } | |
506 | sensitive_data.ssh1_host_key = NULL; | |
507 | memset(sensitive_data.ssh1_cookie, 0, SSH_SESSION_KEY_LENGTH); | |
508 | } | |
509 | ||
700318f3 | 510 | /* Demote private to public keys for network child */ |
511 | void | |
512 | demote_sensitive_data(void) | |
513 | { | |
514 | Key *tmp; | |
515 | int i; | |
516 | ||
517 | if (sensitive_data.server_key) { | |
518 | tmp = key_demote(sensitive_data.server_key); | |
519 | key_free(sensitive_data.server_key); | |
520 | sensitive_data.server_key = tmp; | |
521 | } | |
522 | ||
523 | for (i = 0; i < options.num_host_key_files; i++) { | |
524 | if (sensitive_data.host_keys[i]) { | |
525 | tmp = key_demote(sensitive_data.host_keys[i]); | |
526 | key_free(sensitive_data.host_keys[i]); | |
527 | sensitive_data.host_keys[i] = tmp; | |
528 | if (tmp->type == KEY_RSA1) | |
529 | sensitive_data.ssh1_host_key = tmp; | |
530 | } | |
531 | } | |
532 | ||
533 | /* We do not clear ssh1_host key and cookie. XXX - Okay Niels? */ | |
534 | } | |
535 | ||
536 | static void | |
537 | privsep_preauth_child(void) | |
538 | { | |
41b2f314 | 539 | u_int32_t rnd[256]; |
540 | gid_t gidset[1]; | |
700318f3 | 541 | struct passwd *pw; |
680cee3b | 542 | int i; |
700318f3 | 543 | |
544 | /* Enable challenge-response authentication for privilege separation */ | |
545 | privsep_challenge_enable(); | |
546 | ||
547 | for (i = 0; i < 256; i++) | |
41b2f314 | 548 | rnd[i] = arc4random(); |
549 | RAND_seed(rnd, sizeof(rnd)); | |
700318f3 | 550 | |
551 | /* Demote the private keys to public keys. */ | |
552 | demote_sensitive_data(); | |
553 | ||
554 | if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL) | |
555 | fatal("Privilege separation user %s does not exist", | |
556 | SSH_PRIVSEP_USER); | |
557 | memset(pw->pw_passwd, 0, strlen(pw->pw_passwd)); | |
558 | endpwent(); | |
559 | ||
41b2f314 | 560 | /* Change our root directory */ |
700318f3 | 561 | if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1) |
562 | fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR, | |
563 | strerror(errno)); | |
564 | if (chdir("/") == -1) | |
565 | fatal("chdir(\"/\"): %s", strerror(errno)); | |
566 | ||
567 | /* Drop our privileges */ | |
568 | debug3("privsep user:group %u:%u", (u_int)pw->pw_uid, | |
569 | (u_int)pw->pw_gid); | |
680cee3b | 570 | #if 0 |
571 | /* XXX not ready, to heavy after chroot */ | |
700318f3 | 572 | do_setusercontext(pw); |
680cee3b | 573 | #else |
574 | gidset[0] = pw->pw_gid; | |
575 | if (setgid(pw->pw_gid) < 0) | |
576 | fatal("setgid failed for %u", pw->pw_gid ); | |
577 | if (setgroups(1, gidset) < 0) | |
578 | fatal("setgroups: %.100s", strerror(errno)); | |
579 | permanently_set_uid(pw); | |
580 | #endif | |
700318f3 | 581 | } |
582 | ||
41b2f314 | 583 | static Authctxt * |
700318f3 | 584 | privsep_preauth(void) |
585 | { | |
586 | Authctxt *authctxt = NULL; | |
587 | int status; | |
588 | pid_t pid; | |
589 | ||
590 | /* Set up unprivileged child process to deal with network data */ | |
591 | pmonitor = monitor_init(); | |
592 | /* Store a pointer to the kex for later rekeying */ | |
593 | pmonitor->m_pkex = &xxx_kex; | |
594 | ||
595 | pid = fork(); | |
596 | if (pid == -1) { | |
597 | fatal("fork of unprivileged child failed"); | |
598 | } else if (pid != 0) { | |
41b2f314 | 599 | fatal_remove_cleanup((void (*) (void *)) packet_close, NULL); |
600 | ||
f5799ae1 | 601 | debug2("Network child is on pid %ld", (long)pid); |
700318f3 | 602 | |
603 | close(pmonitor->m_recvfd); | |
604 | authctxt = monitor_child_preauth(pmonitor); | |
605 | close(pmonitor->m_sendfd); | |
606 | ||
607 | /* Sync memory */ | |
608 | monitor_sync(pmonitor); | |
609 | ||
610 | /* Wait for the child's exit status */ | |
611 | while (waitpid(pid, &status, 0) < 0) | |
612 | if (errno != EINTR) | |
613 | break; | |
41b2f314 | 614 | |
615 | /* Reinstall, since the child has finished */ | |
616 | fatal_add_cleanup((void (*) (void *)) packet_close, NULL); | |
617 | ||
700318f3 | 618 | return (authctxt); |
619 | } else { | |
620 | /* child */ | |
621 | ||
622 | close(pmonitor->m_sendfd); | |
623 | ||
624 | /* Demote the child */ | |
625 | if (getuid() == 0 || geteuid() == 0) | |
626 | privsep_preauth_child(); | |
627 | setproctitle("%s", "[net]"); | |
628 | } | |
629 | return (NULL); | |
630 | } | |
631 | ||
632 | static void | |
633 | privsep_postauth(Authctxt *authctxt) | |
634 | { | |
635 | extern Authctxt *x_authctxt; | |
636 | ||
637 | /* XXX - Remote port forwarding */ | |
638 | x_authctxt = authctxt; | |
639 | ||
41b2f314 | 640 | #ifdef DISABLE_FD_PASSING |
680cee3b | 641 | if (1) { |
642 | #else | |
700318f3 | 643 | if (authctxt->pw->pw_uid == 0 || options.use_login) { |
680cee3b | 644 | #endif |
700318f3 | 645 | /* File descriptor passing is broken or root login */ |
646 | monitor_apply_keystate(pmonitor); | |
647 | use_privsep = 0; | |
648 | return; | |
649 | } | |
650 | ||
651 | /* Authentication complete */ | |
652 | alarm(0); | |
653 | if (startup_pipe != -1) { | |
654 | close(startup_pipe); | |
655 | startup_pipe = -1; | |
656 | } | |
657 | ||
658 | /* New socket pair */ | |
659 | monitor_reinit(pmonitor); | |
660 | ||
661 | pmonitor->m_pid = fork(); | |
662 | if (pmonitor->m_pid == -1) | |
663 | fatal("fork of unprivileged child failed"); | |
664 | else if (pmonitor->m_pid != 0) { | |
41b2f314 | 665 | fatal_remove_cleanup((void (*) (void *)) packet_close, NULL); |
666 | ||
f5799ae1 | 667 | debug2("User child is on pid %ld", (long)pmonitor->m_pid); |
700318f3 | 668 | close(pmonitor->m_recvfd); |
669 | monitor_child_postauth(pmonitor); | |
670 | ||
671 | /* NEVERREACHED */ | |
672 | exit(0); | |
673 | } | |
674 | ||
675 | close(pmonitor->m_sendfd); | |
676 | ||
677 | /* Demote the private keys to public keys. */ | |
678 | demote_sensitive_data(); | |
679 | ||
680 | /* Drop privileges */ | |
681 | do_setusercontext(authctxt->pw); | |
682 | ||
683 | /* It is safe now to apply the key state */ | |
684 | monitor_apply_keystate(pmonitor); | |
685 | } | |
686 | ||
3c0ef626 | 687 | static char * |
688 | list_hostkey_types(void) | |
689 | { | |
e9a17296 | 690 | Buffer b; |
691 | char *p; | |
3c0ef626 | 692 | int i; |
e9a17296 | 693 | |
694 | buffer_init(&b); | |
695 | for (i = 0; i < options.num_host_key_files; i++) { | |
3c0ef626 | 696 | Key *key = sensitive_data.host_keys[i]; |
697 | if (key == NULL) | |
698 | continue; | |
e9a17296 | 699 | switch (key->type) { |
3c0ef626 | 700 | case KEY_RSA: |
701 | case KEY_DSA: | |
e9a17296 | 702 | if (buffer_len(&b) > 0) |
703 | buffer_append(&b, ",", 1); | |
704 | p = key_ssh_name(key); | |
705 | buffer_append(&b, p, strlen(p)); | |
3c0ef626 | 706 | break; |
707 | } | |
708 | } | |
e9a17296 | 709 | buffer_append(&b, "\0", 1); |
710 | p = xstrdup(buffer_ptr(&b)); | |
711 | buffer_free(&b); | |
712 | debug("list_hostkey_types: %s", p); | |
713 | return p; | |
3c0ef626 | 714 | } |
715 | ||
700318f3 | 716 | Key * |
3c0ef626 | 717 | get_hostkey_by_type(int type) |
718 | { | |
719 | int i; | |
680cee3b | 720 | |
e9a17296 | 721 | for (i = 0; i < options.num_host_key_files; i++) { |
3c0ef626 | 722 | Key *key = sensitive_data.host_keys[i]; |
723 | if (key != NULL && key->type == type) | |
724 | return key; | |
725 | } | |
726 | return NULL; | |
727 | } | |
728 | ||
700318f3 | 729 | Key * |
730 | get_hostkey_by_index(int ind) | |
731 | { | |
732 | if (ind < 0 || ind >= options.num_host_key_files) | |
733 | return (NULL); | |
734 | return (sensitive_data.host_keys[ind]); | |
735 | } | |
736 | ||
737 | int | |
738 | get_hostkey_index(Key *key) | |
739 | { | |
740 | int i; | |
680cee3b | 741 | |
700318f3 | 742 | for (i = 0; i < options.num_host_key_files; i++) { |
743 | if (key == sensitive_data.host_keys[i]) | |
744 | return (i); | |
745 | } | |
746 | return (-1); | |
747 | } | |
748 | ||
3c0ef626 | 749 | /* |
750 | * returns 1 if connection should be dropped, 0 otherwise. | |
751 | * dropping starts at connection #max_startups_begin with a probability | |
752 | * of (max_startups_rate/100). the probability increases linearly until | |
753 | * all connections are dropped for startups > max_startups | |
754 | */ | |
755 | static int | |
756 | drop_connection(int startups) | |
757 | { | |
758 | double p, r; | |
759 | ||
760 | if (startups < options.max_startups_begin) | |
761 | return 0; | |
762 | if (startups >= options.max_startups) | |
763 | return 1; | |
764 | if (options.max_startups_rate == 100) | |
765 | return 1; | |
766 | ||
767 | p = 100 - options.max_startups_rate; | |
768 | p *= startups - options.max_startups_begin; | |
769 | p /= (double) (options.max_startups - options.max_startups_begin); | |
770 | p += options.max_startups_rate; | |
771 | p /= 100.0; | |
772 | r = arc4random() / (double) UINT_MAX; | |
773 | ||
774 | debug("drop_connection: p %g, r %g", p, r); | |
775 | return (r < p) ? 1 : 0; | |
776 | } | |
777 | ||
e9a17296 | 778 | static void |
779 | usage(void) | |
780 | { | |
781 | fprintf(stderr, "sshd version %s\n", SSH_VERSION); | |
782 | fprintf(stderr, "Usage: %s [options]\n", __progname); | |
783 | fprintf(stderr, "Options:\n"); | |
784 | fprintf(stderr, " -f file Configuration file (default %s)\n", _PATH_SERVER_CONFIG_FILE); | |
785 | fprintf(stderr, " -d Debugging mode (multiple -d means more debugging)\n"); | |
786 | fprintf(stderr, " -i Started from inetd\n"); | |
787 | fprintf(stderr, " -D Do not fork into daemon mode\n"); | |
788 | fprintf(stderr, " -t Only test configuration file and keys\n"); | |
789 | fprintf(stderr, " -q Quiet (no logging)\n"); | |
790 | fprintf(stderr, " -p port Listen on the specified port (default: 22)\n"); | |
791 | fprintf(stderr, " -k seconds Regenerate server key every this many seconds (default: 3600)\n"); | |
792 | fprintf(stderr, " -g seconds Grace period for authentication (default: 600)\n"); | |
793 | fprintf(stderr, " -b bits Size of server RSA key (default: 768 bits)\n"); | |
794 | fprintf(stderr, " -h file File from which to read host key (default: %s)\n", | |
795 | _PATH_HOST_KEY_FILE); | |
796 | fprintf(stderr, " -u len Maximum hostname length for utmp recording\n"); | |
797 | fprintf(stderr, " -4 Use IPv4 only\n"); | |
798 | fprintf(stderr, " -6 Use IPv6 only\n"); | |
799 | fprintf(stderr, " -o option Process the option as if it was read from a configuration file.\n"); | |
800 | exit(1); | |
801 | } | |
3c0ef626 | 802 | |
803 | /* | |
804 | * Main program for the daemon. | |
805 | */ | |
806 | int | |
807 | main(int ac, char **av) | |
808 | { | |
809 | extern char *optarg; | |
810 | extern int optind; | |
811 | int opt, sock_in = 0, sock_out = 0, newsock, j, i, fdsetsz, on = 1; | |
812 | pid_t pid; | |
813 | socklen_t fromlen; | |
814 | fd_set *fdset; | |
815 | struct sockaddr_storage from; | |
816 | const char *remote_ip; | |
817 | int remote_port; | |
818 | FILE *f; | |
3c0ef626 | 819 | struct addrinfo *ai; |
820 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; | |
821 | int listen_sock, maxfd; | |
822 | int startup_p[2]; | |
823 | int startups = 0; | |
700318f3 | 824 | Authctxt *authctxt; |
3c0ef626 | 825 | Key *key; |
826 | int ret, key_used = 0; | |
827 | ||
700318f3 | 828 | #ifdef HAVE_SECUREWARE |
829 | (void)set_auth_parameters(ac, av); | |
830 | #endif | |
3c0ef626 | 831 | __progname = get_progname(av[0]); |
832 | init_rng(); | |
833 | ||
6a9b3198 | 834 | /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */ |
3c0ef626 | 835 | saved_argc = ac; |
836 | saved_argv = av; | |
6a9b3198 | 837 | saved_argv = xmalloc(sizeof(*saved_argv) * ac); |
838 | for (i = 0; i < ac; i++) | |
839 | saved_argv[i] = xstrdup(av[i]); | |
840 | ||
841 | #ifndef HAVE_SETPROCTITLE | |
842 | /* Prepare for later setproctitle emulation */ | |
843 | compat_init_setproctitle(ac, av); | |
844 | #endif | |
3c0ef626 | 845 | |
846 | /* Initialize configuration options to their default values. */ | |
847 | initialize_server_options(&options); | |
848 | ||
849 | /* Parse command-line arguments. */ | |
e9a17296 | 850 | while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:u:o:dDeiqtQ46")) != -1) { |
3c0ef626 | 851 | switch (opt) { |
852 | case '4': | |
853 | IPv4or6 = AF_INET; | |
854 | break; | |
855 | case '6': | |
856 | IPv4or6 = AF_INET6; | |
857 | break; | |
858 | case 'f': | |
859 | config_file_name = optarg; | |
860 | break; | |
861 | case 'd': | |
862 | if (0 == debug_flag) { | |
863 | debug_flag = 1; | |
864 | options.log_level = SYSLOG_LEVEL_DEBUG1; | |
865 | } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) { | |
866 | options.log_level++; | |
867 | } else { | |
868 | fprintf(stderr, "Too high debugging level.\n"); | |
869 | exit(1); | |
870 | } | |
871 | break; | |
872 | case 'D': | |
873 | no_daemon_flag = 1; | |
874 | break; | |
875 | case 'e': | |
876 | log_stderr = 1; | |
877 | break; | |
878 | case 'i': | |
879 | inetd_flag = 1; | |
880 | break; | |
881 | case 'Q': | |
882 | /* ignored */ | |
883 | break; | |
884 | case 'q': | |
885 | options.log_level = SYSLOG_LEVEL_QUIET; | |
886 | break; | |
887 | case 'b': | |
888 | options.server_key_bits = atoi(optarg); | |
889 | break; | |
890 | case 'p': | |
891 | options.ports_from_cmdline = 1; | |
892 | if (options.num_ports >= MAX_PORTS) { | |
893 | fprintf(stderr, "too many ports.\n"); | |
894 | exit(1); | |
895 | } | |
896 | options.ports[options.num_ports++] = a2port(optarg); | |
897 | if (options.ports[options.num_ports-1] == 0) { | |
898 | fprintf(stderr, "Bad port number.\n"); | |
899 | exit(1); | |
900 | } | |
901 | break; | |
902 | case 'g': | |
903 | if ((options.login_grace_time = convtime(optarg)) == -1) { | |
904 | fprintf(stderr, "Invalid login grace time.\n"); | |
905 | exit(1); | |
906 | } | |
907 | break; | |
908 | case 'k': | |
909 | if ((options.key_regeneration_time = convtime(optarg)) == -1) { | |
910 | fprintf(stderr, "Invalid key regeneration interval.\n"); | |
911 | exit(1); | |
912 | } | |
913 | break; | |
914 | case 'h': | |
915 | if (options.num_host_key_files >= MAX_HOSTKEYS) { | |
916 | fprintf(stderr, "too many host keys.\n"); | |
917 | exit(1); | |
918 | } | |
919 | options.host_key_files[options.num_host_key_files++] = optarg; | |
920 | break; | |
921 | case 'V': | |
922 | client_version_string = optarg; | |
923 | /* only makes sense with inetd_flag, i.e. no listen() */ | |
924 | inetd_flag = 1; | |
925 | break; | |
926 | case 't': | |
927 | test_flag = 1; | |
928 | break; | |
929 | case 'u': | |
930 | utmp_len = atoi(optarg); | |
41b2f314 | 931 | if (utmp_len > MAXHOSTNAMELEN) { |
932 | fprintf(stderr, "Invalid utmp length.\n"); | |
933 | exit(1); | |
934 | } | |
3c0ef626 | 935 | break; |
e9a17296 | 936 | case 'o': |
937 | if (process_server_config_line(&options, optarg, | |
938 | "command-line", 0) != 0) | |
939 | exit(1); | |
940 | break; | |
3c0ef626 | 941 | case '?': |
942 | default: | |
e9a17296 | 943 | usage(); |
944 | break; | |
3c0ef626 | 945 | } |
946 | } | |
947 | SSLeay_add_all_algorithms(); | |
948 | channel_set_af(IPv4or6); | |
949 | ||
950 | /* | |
951 | * Force logging to stderr until we have loaded the private host | |
952 | * key (unless started from inetd) | |
953 | */ | |
954 | log_init(__progname, | |
e9a17296 | 955 | options.log_level == SYSLOG_LEVEL_NOT_SET ? |
956 | SYSLOG_LEVEL_INFO : options.log_level, | |
957 | options.log_facility == SYSLOG_FACILITY_NOT_SET ? | |
958 | SYSLOG_FACILITY_AUTH : options.log_facility, | |
6a9b3198 | 959 | log_stderr || !inetd_flag); |
3c0ef626 | 960 | |
41b2f314 | 961 | #ifdef _UNICOS |
3c0ef626 | 962 | /* Cray can define user privs drop all prives now! |
963 | * Not needed on PRIV_SU systems! | |
964 | */ | |
965 | drop_cray_privs(); | |
966 | #endif | |
967 | ||
968 | seed_rng(); | |
969 | ||
970 | /* Read server configuration options from the configuration file. */ | |
971 | read_server_config(&options, config_file_name); | |
972 | ||
973 | /* Fill in default values for those options not explicitly set. */ | |
974 | fill_default_server_options(&options); | |
975 | ||
976 | /* Check that there are no remaining arguments. */ | |
977 | if (optind < ac) { | |
978 | fprintf(stderr, "Extra argument %s.\n", av[optind]); | |
979 | exit(1); | |
980 | } | |
981 | ||
982 | debug("sshd version %.100s", SSH_VERSION); | |
983 | ||
984 | /* load private host keys */ | |
41b2f314 | 985 | sensitive_data.host_keys = xmalloc(options.num_host_key_files * |
986 | sizeof(Key *)); | |
e9a17296 | 987 | for (i = 0; i < options.num_host_key_files; i++) |
3c0ef626 | 988 | sensitive_data.host_keys[i] = NULL; |
989 | sensitive_data.server_key = NULL; | |
990 | sensitive_data.ssh1_host_key = NULL; | |
991 | sensitive_data.have_ssh1_key = 0; | |
992 | sensitive_data.have_ssh2_key = 0; | |
993 | ||
e9a17296 | 994 | for (i = 0; i < options.num_host_key_files; i++) { |
3c0ef626 | 995 | key = key_load_private(options.host_key_files[i], "", NULL); |
996 | sensitive_data.host_keys[i] = key; | |
997 | if (key == NULL) { | |
998 | error("Could not load host key: %s", | |
999 | options.host_key_files[i]); | |
1000 | sensitive_data.host_keys[i] = NULL; | |
1001 | continue; | |
1002 | } | |
e9a17296 | 1003 | switch (key->type) { |
3c0ef626 | 1004 | case KEY_RSA1: |
1005 | sensitive_data.ssh1_host_key = key; | |
1006 | sensitive_data.have_ssh1_key = 1; | |
1007 | break; | |
1008 | case KEY_RSA: | |
1009 | case KEY_DSA: | |
1010 | sensitive_data.have_ssh2_key = 1; | |
1011 | break; | |
1012 | } | |
1013 | debug("private host key: #%d type %d %s", i, key->type, | |
1014 | key_type(key)); | |
1015 | } | |
1016 | if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) { | |
1017 | log("Disabling protocol version 1. Could not load host key"); | |
1018 | options.protocol &= ~SSH_PROTO_1; | |
1019 | } | |
c0fc5818 | 1020 | #ifndef GSSAPI |
1021 | /* The GSSAPI key exchange can run without a host key */ | |
3c0ef626 | 1022 | if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) { |
1023 | log("Disabling protocol version 2. Could not load host key"); | |
1024 | options.protocol &= ~SSH_PROTO_2; | |
1025 | } | |
c0fc5818 | 1026 | #endif |
3c0ef626 | 1027 | if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) { |
1028 | log("sshd: no hostkeys available -- exiting."); | |
1029 | exit(1); | |
1030 | } | |
1031 | ||
1032 | /* Check certain values for sanity. */ | |
1033 | if (options.protocol & SSH_PROTO_1) { | |
1034 | if (options.server_key_bits < 512 || | |
1035 | options.server_key_bits > 32768) { | |
1036 | fprintf(stderr, "Bad server key size.\n"); | |
1037 | exit(1); | |
1038 | } | |
1039 | /* | |
1040 | * Check that server and host key lengths differ sufficiently. This | |
1041 | * is necessary to make double encryption work with rsaref. Oh, I | |
1042 | * hate software patents. I dont know if this can go? Niels | |
1043 | */ | |
1044 | if (options.server_key_bits > | |
680cee3b | 1045 | BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) - |
1046 | SSH_KEY_BITS_RESERVED && options.server_key_bits < | |
1047 | BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + | |
1048 | SSH_KEY_BITS_RESERVED) { | |
3c0ef626 | 1049 | options.server_key_bits = |
680cee3b | 1050 | BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + |
1051 | SSH_KEY_BITS_RESERVED; | |
3c0ef626 | 1052 | debug("Forcing server key to %d bits to make it differ from host key.", |
1053 | options.server_key_bits); | |
1054 | } | |
1055 | } | |
1056 | ||
f5799ae1 | 1057 | if (use_privsep) { |
1058 | struct passwd *pw; | |
1059 | struct stat st; | |
1060 | ||
1061 | if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL) | |
1062 | fatal("Privilege separation user %s does not exist", | |
1063 | SSH_PRIVSEP_USER); | |
1064 | if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) || | |
1065 | (S_ISDIR(st.st_mode) == 0)) | |
1066 | fatal("Missing privilege separation directory: %s", | |
1067 | _PATH_PRIVSEP_CHROOT_DIR); | |
41b2f314 | 1068 | |
1069 | #ifdef HAVE_CYGWIN | |
1070 | if (check_ntsec(_PATH_PRIVSEP_CHROOT_DIR) && | |
1071 | (st.st_uid != getuid () || | |
1072 | (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)) | |
1073 | #else | |
680cee3b | 1074 | if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0) |
41b2f314 | 1075 | #endif |
6a9b3198 | 1076 | fatal("%s must be owned by root and not group or " |
1077 | "world-writable.", _PATH_PRIVSEP_CHROOT_DIR); | |
f5799ae1 | 1078 | } |
1079 | ||
3c0ef626 | 1080 | /* Configuration looks good, so exit if in test mode. */ |
1081 | if (test_flag) | |
1082 | exit(0); | |
1083 | ||
700318f3 | 1084 | /* |
1085 | * Clear out any supplemental groups we may have inherited. This | |
1086 | * prevents inadvertent creation of files with bad modes (in the | |
1087 | * portable version at least, it's certainly possible for PAM | |
1088 | * to create a file, and we can't control the code in every | |
1089 | * module which might be used). | |
1090 | */ | |
1091 | if (setgroups(0, NULL) < 0) | |
1092 | debug("setgroups() failed: %.200s", strerror(errno)); | |
3c0ef626 | 1093 | |
1094 | /* Initialize the log (it is reinitialized below in case we forked). */ | |
1095 | if (debug_flag && !inetd_flag) | |
1096 | log_stderr = 1; | |
1097 | log_init(__progname, options.log_level, options.log_facility, log_stderr); | |
1098 | ||
1099 | /* | |
1100 | * If not in debugging mode, and not started from inetd, disconnect | |
1101 | * from the controlling terminal, and fork. The original process | |
1102 | * exits. | |
1103 | */ | |
1104 | if (!(debug_flag || inetd_flag || no_daemon_flag)) { | |
1105 | #ifdef TIOCNOTTY | |
1106 | int fd; | |
1107 | #endif /* TIOCNOTTY */ | |
1108 | if (daemon(0, 0) < 0) | |
1109 | fatal("daemon() failed: %.200s", strerror(errno)); | |
1110 | ||
1111 | /* Disconnect from the controlling tty. */ | |
1112 | #ifdef TIOCNOTTY | |
1113 | fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); | |
1114 | if (fd >= 0) { | |
1115 | (void) ioctl(fd, TIOCNOTTY, NULL); | |
1116 | close(fd); | |
1117 | } | |
1118 | #endif /* TIOCNOTTY */ | |
1119 | } | |
1120 | /* Reinitialize the log (because of the fork above). */ | |
1121 | log_init(__progname, options.log_level, options.log_facility, log_stderr); | |
1122 | ||
1123 | /* Initialize the random number generator. */ | |
1124 | arc4random_stir(); | |
1125 | ||
1126 | /* Chdir to the root directory so that the current disk can be | |
1127 | unmounted if desired. */ | |
1128 | chdir("/"); | |
e9a17296 | 1129 | |
3c0ef626 | 1130 | /* ignore SIGPIPE */ |
1131 | signal(SIGPIPE, SIG_IGN); | |
1132 | ||
1133 | /* Start listening for a socket, unless started from inetd. */ | |
1134 | if (inetd_flag) { | |
1135 | int s1; | |
1136 | s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */ | |
1137 | dup(s1); | |
1138 | sock_in = dup(0); | |
1139 | sock_out = dup(1); | |
1140 | startup_pipe = -1; | |
1141 | /* | |
1142 | * We intentionally do not close the descriptors 0, 1, and 2 | |
1143 | * as our code for setting the descriptors won\'t work if | |
1144 | * ttyfd happens to be one of those. | |
1145 | */ | |
1146 | debug("inetd sockets after dupping: %d, %d", sock_in, sock_out); | |
1147 | if (options.protocol & SSH_PROTO_1) | |
1148 | generate_ephemeral_server_key(); | |
1149 | } else { | |
1150 | for (ai = options.listen_addrs; ai; ai = ai->ai_next) { | |
1151 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) | |
1152 | continue; | |
1153 | if (num_listen_socks >= MAX_LISTEN_SOCKS) | |
1154 | fatal("Too many listen sockets. " | |
1155 | "Enlarge MAX_LISTEN_SOCKS"); | |
1156 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, | |
1157 | ntop, sizeof(ntop), strport, sizeof(strport), | |
1158 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { | |
1159 | error("getnameinfo failed"); | |
1160 | continue; | |
1161 | } | |
1162 | /* Create socket for listening. */ | |
1163 | listen_sock = socket(ai->ai_family, SOCK_STREAM, 0); | |
1164 | if (listen_sock < 0) { | |
1165 | /* kernel may not support ipv6 */ | |
1166 | verbose("socket: %.100s", strerror(errno)); | |
1167 | continue; | |
1168 | } | |
1169 | if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) { | |
1170 | error("listen_sock O_NONBLOCK: %s", strerror(errno)); | |
1171 | close(listen_sock); | |
1172 | continue; | |
1173 | } | |
1174 | /* | |
41b2f314 | 1175 | * Set socket options. |
1176 | * Allow local port reuse in TIME_WAIT. | |
3c0ef626 | 1177 | */ |
41b2f314 | 1178 | if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, |
1179 | &on, sizeof(on)) == -1) | |
1180 | error("setsockopt SO_REUSEADDR: %s", strerror(errno)); | |
3c0ef626 | 1181 | |
1182 | debug("Bind to port %s on %s.", strport, ntop); | |
1183 | ||
1184 | /* Bind the socket to the desired port. */ | |
1185 | if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) { | |
1186 | if (!ai->ai_next) | |
1187 | error("Bind to port %s on %s failed: %.200s.", | |
1188 | strport, ntop, strerror(errno)); | |
1189 | close(listen_sock); | |
1190 | continue; | |
1191 | } | |
1192 | listen_socks[num_listen_socks] = listen_sock; | |
1193 | num_listen_socks++; | |
1194 | ||
1195 | /* Start listening on the port. */ | |
1196 | log("Server listening on %s port %s.", ntop, strport); | |
1197 | if (listen(listen_sock, 5) < 0) | |
1198 | fatal("listen: %.100s", strerror(errno)); | |
1199 | ||
1200 | } | |
1201 | freeaddrinfo(options.listen_addrs); | |
1202 | ||
1203 | if (!num_listen_socks) | |
1204 | fatal("Cannot bind any address."); | |
1205 | ||
1206 | if (options.protocol & SSH_PROTO_1) | |
1207 | generate_ephemeral_server_key(); | |
1208 | ||
1209 | /* | |
1210 | * Arrange to restart on SIGHUP. The handler needs | |
1211 | * listen_sock. | |
1212 | */ | |
1213 | signal(SIGHUP, sighup_handler); | |
1214 | ||
1215 | signal(SIGTERM, sigterm_handler); | |
1216 | signal(SIGQUIT, sigterm_handler); | |
1217 | ||
1218 | /* Arrange SIGCHLD to be caught. */ | |
1219 | signal(SIGCHLD, main_sigchld_handler); | |
1220 | ||
1221 | /* Write out the pid file after the sigterm handler is setup */ | |
1222 | if (!debug_flag) { | |
1223 | /* | |
1224 | * Record our pid in /var/run/sshd.pid to make it | |
1225 | * easier to kill the correct sshd. We don't want to | |
1226 | * do this before the bind above because the bind will | |
1227 | * fail if there already is a daemon, and this will | |
1228 | * overwrite any old pid in the file. | |
1229 | */ | |
1230 | f = fopen(options.pid_file, "wb"); | |
1231 | if (f) { | |
f5799ae1 | 1232 | fprintf(f, "%ld\n", (long) getpid()); |
3c0ef626 | 1233 | fclose(f); |
1234 | } | |
1235 | } | |
1236 | ||
1237 | /* setup fd set for listen */ | |
1238 | fdset = NULL; | |
1239 | maxfd = 0; | |
1240 | for (i = 0; i < num_listen_socks; i++) | |
1241 | if (listen_socks[i] > maxfd) | |
1242 | maxfd = listen_socks[i]; | |
1243 | /* pipes connected to unauthenticated childs */ | |
1244 | startup_pipes = xmalloc(options.max_startups * sizeof(int)); | |
1245 | for (i = 0; i < options.max_startups; i++) | |
1246 | startup_pipes[i] = -1; | |
1247 | ||
1248 | /* | |
1249 | * Stay listening for connections until the system crashes or | |
1250 | * the daemon is killed with a signal. | |
1251 | */ | |
1252 | for (;;) { | |
1253 | if (received_sighup) | |
1254 | sighup_restart(); | |
1255 | if (fdset != NULL) | |
1256 | xfree(fdset); | |
1257 | fdsetsz = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask); | |
1258 | fdset = (fd_set *)xmalloc(fdsetsz); | |
1259 | memset(fdset, 0, fdsetsz); | |
1260 | ||
1261 | for (i = 0; i < num_listen_socks; i++) | |
1262 | FD_SET(listen_socks[i], fdset); | |
1263 | for (i = 0; i < options.max_startups; i++) | |
1264 | if (startup_pipes[i] != -1) | |
1265 | FD_SET(startup_pipes[i], fdset); | |
1266 | ||
1267 | /* Wait in select until there is a connection. */ | |
1268 | ret = select(maxfd+1, fdset, NULL, NULL, NULL); | |
1269 | if (ret < 0 && errno != EINTR) | |
1270 | error("select: %.100s", strerror(errno)); | |
1271 | if (received_sigterm) { | |
1272 | log("Received signal %d; terminating.", | |
e9a17296 | 1273 | (int) received_sigterm); |
3c0ef626 | 1274 | close_listen_socks(); |
1275 | unlink(options.pid_file); | |
1276 | exit(255); | |
1277 | } | |
1278 | if (key_used && key_do_regen) { | |
1279 | generate_ephemeral_server_key(); | |
1280 | key_used = 0; | |
1281 | key_do_regen = 0; | |
1282 | } | |
1283 | if (ret < 0) | |
1284 | continue; | |
1285 | ||
1286 | for (i = 0; i < options.max_startups; i++) | |
1287 | if (startup_pipes[i] != -1 && | |
1288 | FD_ISSET(startup_pipes[i], fdset)) { | |
1289 | /* | |
1290 | * the read end of the pipe is ready | |
1291 | * if the child has closed the pipe | |
1292 | * after successful authentication | |
1293 | * or if the child has died | |
1294 | */ | |
1295 | close(startup_pipes[i]); | |
1296 | startup_pipes[i] = -1; | |
1297 | startups--; | |
1298 | } | |
1299 | for (i = 0; i < num_listen_socks; i++) { | |
1300 | if (!FD_ISSET(listen_socks[i], fdset)) | |
1301 | continue; | |
1302 | fromlen = sizeof(from); | |
1303 | newsock = accept(listen_socks[i], (struct sockaddr *)&from, | |
1304 | &fromlen); | |
1305 | if (newsock < 0) { | |
1306 | if (errno != EINTR && errno != EWOULDBLOCK) | |
1307 | error("accept: %.100s", strerror(errno)); | |
1308 | continue; | |
1309 | } | |
1310 | if (fcntl(newsock, F_SETFL, 0) < 0) { | |
1311 | error("newsock del O_NONBLOCK: %s", strerror(errno)); | |
e9a17296 | 1312 | close(newsock); |
3c0ef626 | 1313 | continue; |
1314 | } | |
1315 | if (drop_connection(startups) == 1) { | |
1316 | debug("drop connection #%d", startups); | |
1317 | close(newsock); | |
1318 | continue; | |
1319 | } | |
1320 | if (pipe(startup_p) == -1) { | |
1321 | close(newsock); | |
1322 | continue; | |
1323 | } | |
1324 | ||
1325 | for (j = 0; j < options.max_startups; j++) | |
1326 | if (startup_pipes[j] == -1) { | |
1327 | startup_pipes[j] = startup_p[0]; | |
1328 | if (maxfd < startup_p[0]) | |
1329 | maxfd = startup_p[0]; | |
1330 | startups++; | |
1331 | break; | |
1332 | } | |
1333 | ||
1334 | /* | |
1335 | * Got connection. Fork a child to handle it, unless | |
1336 | * we are in debugging mode. | |
1337 | */ | |
1338 | if (debug_flag) { | |
1339 | /* | |
1340 | * In debugging mode. Close the listening | |
1341 | * socket, and start processing the | |
1342 | * connection without forking. | |
1343 | */ | |
1344 | debug("Server will not fork when running in debugging mode."); | |
1345 | close_listen_socks(); | |
1346 | sock_in = newsock; | |
1347 | sock_out = newsock; | |
1348 | startup_pipe = -1; | |
1349 | pid = getpid(); | |
1350 | break; | |
1351 | } else { | |
1352 | /* | |
1353 | * Normal production daemon. Fork, and have | |
1354 | * the child process the connection. The | |
1355 | * parent continues listening. | |
1356 | */ | |
1357 | if ((pid = fork()) == 0) { | |
1358 | /* | |
1359 | * Child. Close the listening and max_startup | |
1360 | * sockets. Start using the accepted socket. | |
1361 | * Reinitialize logging (since our pid has | |
1362 | * changed). We break out of the loop to handle | |
1363 | * the connection. | |
1364 | */ | |
1365 | startup_pipe = startup_p[1]; | |
e9a17296 | 1366 | close_startup_pipes(); |
3c0ef626 | 1367 | close_listen_socks(); |
1368 | sock_in = newsock; | |
1369 | sock_out = newsock; | |
1370 | log_init(__progname, options.log_level, options.log_facility, log_stderr); | |
1371 | break; | |
1372 | } | |
1373 | } | |
1374 | ||
1375 | /* Parent. Stay in the loop. */ | |
1376 | if (pid < 0) | |
1377 | error("fork: %.100s", strerror(errno)); | |
1378 | else | |
f5799ae1 | 1379 | debug("Forked child %ld.", (long)pid); |
3c0ef626 | 1380 | |
1381 | close(startup_p[1]); | |
1382 | ||
1383 | /* Mark that the key has been used (it was "given" to the child). */ | |
1384 | if ((options.protocol & SSH_PROTO_1) && | |
1385 | key_used == 0) { | |
1386 | /* Schedule server key regeneration alarm. */ | |
1387 | signal(SIGALRM, key_regeneration_alarm); | |
1388 | alarm(options.key_regeneration_time); | |
1389 | key_used = 1; | |
1390 | } | |
1391 | ||
1392 | arc4random_stir(); | |
1393 | ||
1394 | /* Close the new socket (the child is now taking care of it). */ | |
1395 | close(newsock); | |
1396 | } | |
1397 | /* child process check (or debug mode) */ | |
1398 | if (num_listen_socks < 0) | |
1399 | break; | |
1400 | } | |
1401 | } | |
1402 | ||
1403 | /* This is the child processing a new connection. */ | |
1404 | ||
700318f3 | 1405 | /* |
1406 | * Create a new session and process group since the 4.4BSD | |
1407 | * setlogin() affects the entire process group. We don't | |
1408 | * want the child to be able to affect the parent. | |
1409 | */ | |
6a9b3198 | 1410 | #if !defined(STREAMS_PUSH_ACQUIRES_CTTY) |
1411 | /* | |
1412 | * If setsid is called on Solaris, sshd will acquire the controlling | |
1413 | * terminal while pushing STREAMS modules. This will prevent the | |
1414 | * shell from acquiring it later. | |
1415 | */ | |
680cee3b | 1416 | if (!debug_flag && !inetd_flag && setsid() < 0) |
700318f3 | 1417 | error("setsid: %.100s", strerror(errno)); |
7435de97 | 1418 | #endif |
700318f3 | 1419 | |
3c0ef626 | 1420 | /* |
1421 | * Disable the key regeneration alarm. We will not regenerate the | |
1422 | * key since we are no longer in a position to give it to anyone. We | |
1423 | * will not restart on SIGHUP since it no longer makes sense. | |
1424 | */ | |
1425 | alarm(0); | |
1426 | signal(SIGALRM, SIG_DFL); | |
1427 | signal(SIGHUP, SIG_DFL); | |
1428 | signal(SIGTERM, SIG_DFL); | |
1429 | signal(SIGQUIT, SIG_DFL); | |
1430 | signal(SIGCHLD, SIG_DFL); | |
1431 | signal(SIGINT, SIG_DFL); | |
1432 | ||
3c0ef626 | 1433 | /* Set keepalives if requested. */ |
1434 | if (options.keepalives && | |
e9a17296 | 1435 | setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, |
3c0ef626 | 1436 | sizeof(on)) < 0) |
1437 | error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno)); | |
1438 | ||
1439 | /* | |
1440 | * Register our connection. This turns encryption off because we do | |
1441 | * not have a key. | |
1442 | */ | |
1443 | packet_set_connection(sock_in, sock_out); | |
1444 | ||
1445 | remote_port = get_remote_port(); | |
1446 | remote_ip = get_remote_ipaddr(); | |
1447 | ||
1448 | #ifdef LIBWRAP | |
1449 | /* Check whether logins are denied from this host. */ | |
1450 | { | |
1451 | struct request_info req; | |
1452 | ||
1453 | request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, 0); | |
1454 | fromhost(&req); | |
1455 | ||
1456 | if (!hosts_access(&req)) { | |
1457 | debug("Connection refused by tcp wrapper"); | |
1458 | refuse(&req); | |
1459 | /* NOTREACHED */ | |
1460 | fatal("libwrap refuse returns"); | |
1461 | } | |
1462 | } | |
1463 | #endif /* LIBWRAP */ | |
1464 | ||
1465 | /* Log the connection. */ | |
1466 | verbose("Connection from %.500s port %d", remote_ip, remote_port); | |
1467 | ||
1468 | /* | |
1469 | * We don\'t want to listen forever unless the other side | |
1470 | * successfully authenticates itself. So we set up an alarm which is | |
1471 | * cleared after successful authentication. A limit of zero | |
1472 | * indicates no limit. Note that we don\'t set the alarm in debugging | |
1473 | * mode; it is just annoying to have the server exit just when you | |
1474 | * are about to discover the bug. | |
1475 | */ | |
1476 | signal(SIGALRM, grace_alarm_handler); | |
1477 | if (!debug_flag) | |
1478 | alarm(options.login_grace_time); | |
1479 | ||
1480 | sshd_exchange_identification(sock_in, sock_out); | |
1481 | /* | |
1482 | * Check that the connection comes from a privileged port. | |
f5799ae1 | 1483 | * Rhosts-Authentication only makes sense from privileged |
3c0ef626 | 1484 | * programs. Of course, if the intruder has root access on his local |
1485 | * machine, he can connect from any port. So do not use these | |
1486 | * authentication methods from machines that you do not trust. | |
1487 | */ | |
e9a17296 | 1488 | if (options.rhosts_authentication && |
1489 | (remote_port >= IPPORT_RESERVED || | |
1490 | remote_port < IPPORT_RESERVED / 2)) { | |
3c0ef626 | 1491 | debug("Rhosts Authentication disabled, " |
1492 | "originating port %d not trusted.", remote_port); | |
1493 | options.rhosts_authentication = 0; | |
1494 | } | |
1495 | #if defined(KRB4) && !defined(KRB5) | |
1496 | if (!packet_connection_is_ipv4() && | |
1497 | options.kerberos_authentication) { | |
1498 | debug("Kerberos Authentication disabled, only available for IPv4."); | |
1499 | options.kerberos_authentication = 0; | |
1500 | } | |
1501 | #endif /* KRB4 && !KRB5 */ | |
1502 | #ifdef AFS | |
1503 | /* If machine has AFS, set process authentication group. */ | |
1504 | if (k_hasafs()) { | |
1505 | k_setpag(); | |
1506 | k_unlog(); | |
1507 | } | |
1508 | #endif /* AFS */ | |
1509 | ||
1510 | packet_set_nonblocking(); | |
1511 | ||
700318f3 | 1512 | if (use_privsep) |
1513 | if ((authctxt = privsep_preauth()) != NULL) | |
1514 | goto authenticated; | |
1515 | ||
3c0ef626 | 1516 | /* perform the key exchange */ |
1517 | /* authenticate user and start session */ | |
1518 | if (compat20) { | |
1519 | do_ssh2_kex(); | |
700318f3 | 1520 | authctxt = do_authentication2(); |
3c0ef626 | 1521 | } else { |
1522 | do_ssh1_kex(); | |
700318f3 | 1523 | authctxt = do_authentication(); |
1524 | } | |
1525 | /* | |
1526 | * If we use privilege separation, the unprivileged child transfers | |
1527 | * the current keystate and exits | |
1528 | */ | |
1529 | if (use_privsep) { | |
1530 | mm_send_keystate(pmonitor); | |
1531 | exit(0); | |
1532 | } | |
1533 | ||
1534 | authenticated: | |
1535 | /* | |
1536 | * In privilege separation, we fork another child and prepare | |
1537 | * file descriptor passing. | |
1538 | */ | |
1539 | if (use_privsep) { | |
1540 | privsep_postauth(authctxt); | |
1541 | /* the monitor process [priv] will not return */ | |
1542 | if (!compat20) | |
1543 | destroy_sensitive_data(); | |
3c0ef626 | 1544 | } |
700318f3 | 1545 | |
1546 | /* Perform session preparation. */ | |
1547 | do_authenticated(authctxt); | |
1548 | ||
3c0ef626 | 1549 | /* The connection has been terminated. */ |
1550 | verbose("Closing connection to %.100s", remote_ip); | |
1551 | ||
1552 | #ifdef USE_PAM | |
1553 | finish_pam(); | |
1554 | #endif /* USE_PAM */ | |
1555 | ||
1556 | packet_close(); | |
700318f3 | 1557 | |
1558 | if (use_privsep) | |
1559 | mm_terminate(); | |
1560 | ||
3c0ef626 | 1561 | exit(0); |
1562 | } | |
1563 | ||
700318f3 | 1564 | /* |
1565 | * Decrypt session_key_int using our private server key and private host key | |
1566 | * (key with larger modulus first). | |
1567 | */ | |
1568 | int | |
1569 | ssh1_session_key(BIGNUM *session_key_int) | |
1570 | { | |
1571 | int rsafail = 0; | |
1572 | ||
1573 | if (BN_cmp(sensitive_data.server_key->rsa->n, sensitive_data.ssh1_host_key->rsa->n) > 0) { | |
1574 | /* Server key has bigger modulus. */ | |
1575 | if (BN_num_bits(sensitive_data.server_key->rsa->n) < | |
1576 | BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED) { | |
1577 | fatal("do_connection: %s: server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d", | |
1578 | get_remote_ipaddr(), | |
1579 | BN_num_bits(sensitive_data.server_key->rsa->n), | |
1580 | BN_num_bits(sensitive_data.ssh1_host_key->rsa->n), | |
1581 | SSH_KEY_BITS_RESERVED); | |
1582 | } | |
1583 | if (rsa_private_decrypt(session_key_int, session_key_int, | |
1584 | sensitive_data.server_key->rsa) <= 0) | |
1585 | rsafail++; | |
1586 | if (rsa_private_decrypt(session_key_int, session_key_int, | |
1587 | sensitive_data.ssh1_host_key->rsa) <= 0) | |
1588 | rsafail++; | |
1589 | } else { | |
1590 | /* Host key has bigger modulus (or they are equal). */ | |
1591 | if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) < | |
1592 | BN_num_bits(sensitive_data.server_key->rsa->n) + SSH_KEY_BITS_RESERVED) { | |
1593 | fatal("do_connection: %s: host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d", | |
1594 | get_remote_ipaddr(), | |
1595 | BN_num_bits(sensitive_data.ssh1_host_key->rsa->n), | |
1596 | BN_num_bits(sensitive_data.server_key->rsa->n), | |
1597 | SSH_KEY_BITS_RESERVED); | |
1598 | } | |
1599 | if (rsa_private_decrypt(session_key_int, session_key_int, | |
1600 | sensitive_data.ssh1_host_key->rsa) < 0) | |
1601 | rsafail++; | |
1602 | if (rsa_private_decrypt(session_key_int, session_key_int, | |
1603 | sensitive_data.server_key->rsa) < 0) | |
1604 | rsafail++; | |
1605 | } | |
1606 | return (rsafail); | |
1607 | } | |
3c0ef626 | 1608 | /* |
1609 | * SSH1 key exchange | |
1610 | */ | |
1611 | static void | |
1612 | do_ssh1_kex(void) | |
1613 | { | |
1614 | int i, len; | |
3c0ef626 | 1615 | int rsafail = 0; |
1616 | BIGNUM *session_key_int; | |
1617 | u_char session_key[SSH_SESSION_KEY_LENGTH]; | |
1618 | u_char cookie[8]; | |
1619 | u_int cipher_type, auth_mask, protocol_flags; | |
41b2f314 | 1620 | u_int32_t rnd = 0; |
3c0ef626 | 1621 | |
1622 | /* | |
1623 | * Generate check bytes that the client must send back in the user | |
1624 | * packet in order for it to be accepted; this is used to defy ip | |
1625 | * spoofing attacks. Note that this only works against somebody | |
1626 | * doing IP spoofing from a remote machine; any machine on the local | |
1627 | * network can still see outgoing packets and catch the random | |
1628 | * cookie. This only affects rhosts authentication, and this is one | |
1629 | * of the reasons why it is inherently insecure. | |
1630 | */ | |
1631 | for (i = 0; i < 8; i++) { | |
1632 | if (i % 4 == 0) | |
41b2f314 | 1633 | rnd = arc4random(); |
1634 | cookie[i] = rnd & 0xff; | |
1635 | rnd >>= 8; | |
3c0ef626 | 1636 | } |
1637 | ||
1638 | /* | |
1639 | * Send our public key. We include in the packet 64 bits of random | |
1640 | * data that must be matched in the reply in order to prevent IP | |
1641 | * spoofing. | |
1642 | */ | |
1643 | packet_start(SSH_SMSG_PUBLIC_KEY); | |
1644 | for (i = 0; i < 8; i++) | |
1645 | packet_put_char(cookie[i]); | |
1646 | ||
1647 | /* Store our public server RSA key. */ | |
1648 | packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n)); | |
1649 | packet_put_bignum(sensitive_data.server_key->rsa->e); | |
1650 | packet_put_bignum(sensitive_data.server_key->rsa->n); | |
1651 | ||
1652 | /* Store our public host RSA key. */ | |
1653 | packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n)); | |
1654 | packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e); | |
1655 | packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n); | |
1656 | ||
1657 | /* Put protocol flags. */ | |
1658 | packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN); | |
1659 | ||
1660 | /* Declare which ciphers we support. */ | |
1661 | packet_put_int(cipher_mask_ssh1(0)); | |
1662 | ||
1663 | /* Declare supported authentication types. */ | |
1664 | auth_mask = 0; | |
1665 | if (options.rhosts_authentication) | |
1666 | auth_mask |= 1 << SSH_AUTH_RHOSTS; | |
1667 | if (options.rhosts_rsa_authentication) | |
1668 | auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA; | |
1669 | if (options.rsa_authentication) | |
1670 | auth_mask |= 1 << SSH_AUTH_RSA; | |
1671 | #if defined(KRB4) || defined(KRB5) | |
1672 | if (options.kerberos_authentication) | |
1673 | auth_mask |= 1 << SSH_AUTH_KERBEROS; | |
1674 | #endif | |
1675 | #if defined(AFS) || defined(KRB5) | |
1676 | if (options.kerberos_tgt_passing) | |
1677 | auth_mask |= 1 << SSH_PASS_KERBEROS_TGT; | |
1678 | #endif | |
1679 | #ifdef AFS | |
1680 | if (options.afs_token_passing) | |
1681 | auth_mask |= 1 << SSH_PASS_AFS_TOKEN; | |
1682 | #endif | |
1683 | if (options.challenge_response_authentication == 1) | |
1684 | auth_mask |= 1 << SSH_AUTH_TIS; | |
1685 | if (options.password_authentication) | |
1686 | auth_mask |= 1 << SSH_AUTH_PASSWORD; | |
1687 | packet_put_int(auth_mask); | |
1688 | ||
1689 | /* Send the packet and wait for it to be sent. */ | |
1690 | packet_send(); | |
1691 | packet_write_wait(); | |
1692 | ||
1693 | debug("Sent %d bit server key and %d bit host key.", | |
1694 | BN_num_bits(sensitive_data.server_key->rsa->n), | |
1695 | BN_num_bits(sensitive_data.ssh1_host_key->rsa->n)); | |
1696 | ||
1697 | /* Read clients reply (cipher type and session key). */ | |
e9a17296 | 1698 | packet_read_expect(SSH_CMSG_SESSION_KEY); |
3c0ef626 | 1699 | |
1700 | /* Get cipher type and check whether we accept this. */ | |
1701 | cipher_type = packet_get_char(); | |
1702 | ||
1703 | if (!(cipher_mask_ssh1(0) & (1 << cipher_type))) | |
1704 | packet_disconnect("Warning: client selects unsupported cipher."); | |
1705 | ||
1706 | /* Get check bytes from the packet. These must match those we | |
1707 | sent earlier with the public key packet. */ | |
1708 | for (i = 0; i < 8; i++) | |
1709 | if (cookie[i] != packet_get_char()) | |
1710 | packet_disconnect("IP Spoofing check bytes do not match."); | |
1711 | ||
1712 | debug("Encryption type: %.200s", cipher_name(cipher_type)); | |
1713 | ||
1714 | /* Get the encrypted integer. */ | |
e9a17296 | 1715 | if ((session_key_int = BN_new()) == NULL) |
1716 | fatal("do_ssh1_kex: BN_new failed"); | |
1717 | packet_get_bignum(session_key_int); | |
3c0ef626 | 1718 | |
1719 | protocol_flags = packet_get_int(); | |
1720 | packet_set_protocol_flags(protocol_flags); | |
e9a17296 | 1721 | packet_check_eom(); |
3c0ef626 | 1722 | |
700318f3 | 1723 | /* Decrypt session_key_int using host/server keys */ |
1724 | rsafail = PRIVSEP(ssh1_session_key(session_key_int)); | |
1725 | ||
3c0ef626 | 1726 | /* |
1727 | * Extract session key from the decrypted integer. The key is in the | |
1728 | * least significant 256 bits of the integer; the first byte of the | |
1729 | * key is in the highest bits. | |
1730 | */ | |
1731 | if (!rsafail) { | |
1732 | BN_mask_bits(session_key_int, sizeof(session_key) * 8); | |
1733 | len = BN_num_bytes(session_key_int); | |
1734 | if (len < 0 || len > sizeof(session_key)) { | |
1735 | error("do_connection: bad session key len from %s: " | |
1736 | "session_key_int %d > sizeof(session_key) %lu", | |
1737 | get_remote_ipaddr(), len, (u_long)sizeof(session_key)); | |
1738 | rsafail++; | |
1739 | } else { | |
1740 | memset(session_key, 0, sizeof(session_key)); | |
1741 | BN_bn2bin(session_key_int, | |
1742 | session_key + sizeof(session_key) - len); | |
1743 | ||
1744 | compute_session_id(session_id, cookie, | |
1745 | sensitive_data.ssh1_host_key->rsa->n, | |
1746 | sensitive_data.server_key->rsa->n); | |
1747 | /* | |
1748 | * Xor the first 16 bytes of the session key with the | |
1749 | * session id. | |
1750 | */ | |
1751 | for (i = 0; i < 16; i++) | |
1752 | session_key[i] ^= session_id[i]; | |
1753 | } | |
1754 | } | |
1755 | if (rsafail) { | |
1756 | int bytes = BN_num_bytes(session_key_int); | |
e9a17296 | 1757 | u_char *buf = xmalloc(bytes); |
3c0ef626 | 1758 | MD5_CTX md; |
1759 | ||
1760 | log("do_connection: generating a fake encryption key"); | |
1761 | BN_bn2bin(session_key_int, buf); | |
1762 | MD5_Init(&md); | |
1763 | MD5_Update(&md, buf, bytes); | |
1764 | MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH); | |
1765 | MD5_Final(session_key, &md); | |
1766 | MD5_Init(&md); | |
1767 | MD5_Update(&md, session_key, 16); | |
1768 | MD5_Update(&md, buf, bytes); | |
1769 | MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH); | |
1770 | MD5_Final(session_key + 16, &md); | |
1771 | memset(buf, 0, bytes); | |
1772 | xfree(buf); | |
1773 | for (i = 0; i < 16; i++) | |
1774 | session_id[i] = session_key[i] ^ session_key[i + 16]; | |
1775 | } | |
700318f3 | 1776 | /* Destroy the private and public keys. No longer. */ |
3c0ef626 | 1777 | destroy_sensitive_data(); |
1778 | ||
700318f3 | 1779 | if (use_privsep) |
1780 | mm_ssh1_session_id(session_id); | |
1781 | ||
3c0ef626 | 1782 | /* Destroy the decrypted integer. It is no longer needed. */ |
1783 | BN_clear_free(session_key_int); | |
1784 | ||
1785 | /* Set the session key. From this on all communications will be encrypted. */ | |
1786 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type); | |
1787 | ||
1788 | /* Destroy our copy of the session key. It is no longer needed. */ | |
1789 | memset(session_key, 0, sizeof(session_key)); | |
1790 | ||
1791 | debug("Received session key; encryption turned on."); | |
1792 | ||
f5799ae1 | 1793 | /* Send an acknowledgment packet. Note that this packet is sent encrypted. */ |
3c0ef626 | 1794 | packet_start(SSH_SMSG_SUCCESS); |
1795 | packet_send(); | |
1796 | packet_write_wait(); | |
1797 | } | |
1798 | ||
1799 | /* | |
1800 | * SSH2 key exchange: diffie-hellman-group1-sha1 | |
1801 | */ | |
1802 | static void | |
1803 | do_ssh2_kex(void) | |
1804 | { | |
1805 | Kex *kex; | |
1806 | ||
1807 | if (options.ciphers != NULL) { | |
1808 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
1809 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; | |
1810 | } | |
1811 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
1812 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]); | |
1813 | myproposal[PROPOSAL_ENC_ALGS_STOC] = | |
1814 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]); | |
1815 | ||
1816 | if (options.macs != NULL) { | |
1817 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = | |
1818 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; | |
1819 | } | |
f5799ae1 | 1820 | if (!options.compression) { |
1821 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = | |
1822 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none"; | |
1823 | } | |
3c0ef626 | 1824 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = list_hostkey_types(); |
1825 | ||
c0fc5818 | 1826 | #ifdef GSSAPI |
1827 | { | |
1828 | char *orig; | |
1829 | char *gss = NULL; | |
1830 | char *newstr = NULL; | |
1831 | orig = myproposal[PROPOSAL_KEX_ALGS]; | |
1832 | ||
1833 | /* If we don't have a host key, then all of the algorithms | |
1834 | * currently in myproposal are useless */ | |
1835 | if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS])==0) | |
1836 | orig= NULL; | |
1837 | ||
1838 | if (options.gss_keyex) | |
1839 | gss = ssh_gssapi_server_mechanisms(); | |
1840 | else | |
1841 | gss = NULL; | |
1842 | ||
1843 | if (gss && orig) { | |
1844 | int len = strlen(orig) + strlen(gss) +2; | |
1845 | newstr=xmalloc(len); | |
1846 | snprintf(newstr,len,"%s,%s",gss,orig); | |
1847 | } else if (gss) { | |
1848 | newstr=gss; | |
1849 | } else if (orig) { | |
1850 | newstr=orig; | |
1851 | } | |
1852 | /* If we've got GSSAPI mechanisms, then we've also got the 'null' | |
1853 | host key algorithm, but we're not allowed to advertise it, unless | |
1854 | its the only host key algorithm we're supporting */ | |
1855 | if (gss && (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS])) == 0) { | |
1856 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]="null"; | |
1857 | } | |
1858 | if (newstr) | |
1859 | myproposal[PROPOSAL_KEX_ALGS]=newstr; | |
1860 | else | |
1861 | fatal("No supported key exchange algorithms"); | |
1862 | } | |
1863 | #endif | |
1864 | ||
3c0ef626 | 1865 | /* start key exchange */ |
1866 | kex = kex_setup(myproposal); | |
6a9b3198 | 1867 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server; |
1868 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; | |
c0fc5818 | 1869 | #ifdef GSSAPI |
1870 | kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server; | |
1871 | #endif | |
3c0ef626 | 1872 | kex->server = 1; |
1873 | kex->client_version_string=client_version_string; | |
1874 | kex->server_version_string=server_version_string; | |
1875 | kex->load_host_key=&get_hostkey_by_type; | |
700318f3 | 1876 | kex->host_key_index=&get_hostkey_index; |
3c0ef626 | 1877 | |
1878 | xxx_kex = kex; | |
1879 | ||
1880 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); | |
1881 | ||
1882 | session_id2 = kex->session_id; | |
1883 | session_id2_len = kex->session_id_len; | |
1884 | ||
1885 | #ifdef DEBUG_KEXDH | |
1886 | /* send 1st encrypted/maced/compressed message */ | |
1887 | packet_start(SSH2_MSG_IGNORE); | |
1888 | packet_put_cstring("markus"); | |
1889 | packet_send(); | |
1890 | packet_write_wait(); | |
1891 | #endif | |
1892 | debug("KEX done"); | |
1893 | } |