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