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