]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
4 | * All rights reserved | |
bcbf86ec | 5 | * This program is the ssh daemon. It listens for connections from clients, |
6 | * and performs authentication, executes use commands or shell, and forwards | |
5260325f | 7 | * information to/from the application to the user client over an encrypted |
bcbf86ec | 8 | * connection. This can also handle forwarding of X11, TCP/IP, and |
9 | * authentication agent connections. | |
e78a59f5 | 10 | * |
bcbf86ec | 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 | * | |
19 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
20 | * | |
21 | * Redistribution and use in source and binary forms, with or without | |
22 | * modification, are permitted provided that the following conditions | |
23 | * are met: | |
24 | * 1. Redistributions of source code must retain the above copyright | |
25 | * notice, this list of conditions and the following disclaimer. | |
26 | * 2. Redistributions in binary form must reproduce the above copyright | |
27 | * notice, this list of conditions and the following disclaimer in the | |
28 | * documentation and/or other materials provided with the distribution. | |
29 | * | |
30 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
31 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
32 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
33 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
34 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
35 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
36 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
37 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
38 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
39 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
5260325f | 40 | */ |
8efc0c15 | 41 | |
42 | #include "includes.h" | |
1b34c1b3 | 43 | RCSID("$OpenBSD: sshd.c,v 1.230 2002/03/18 01:12:14 provos Exp $"); |
8efc0c15 | 44 | |
42f11eb2 | 45 | #include <openssl/dh.h> |
46 | #include <openssl/bn.h> | |
436c347c | 47 | #include <openssl/md5.h> |
42f11eb2 | 48 | |
49 | #include "ssh.h" | |
50 | #include "ssh1.h" | |
51 | #include "ssh2.h" | |
8efc0c15 | 52 | #include "xmalloc.h" |
53 | #include "rsa.h" | |
1729c161 | 54 | #include "sshpty.h" |
8efc0c15 | 55 | #include "packet.h" |
8efc0c15 | 56 | #include "mpaux.h" |
42f11eb2 | 57 | #include "log.h" |
8efc0c15 | 58 | #include "servconf.h" |
59 | #include "uidswap.h" | |
60 | #include "compat.h" | |
7368a6c8 | 61 | #include "buffer.h" |
42f11eb2 | 62 | #include "cipher.h" |
e78a59f5 | 63 | #include "kex.h" |
7368a6c8 | 64 | #include "key.h" |
94ec8c6b | 65 | #include "dh.h" |
e78a59f5 | 66 | #include "myproposal.h" |
a306f2dd | 67 | #include "authfile.h" |
42f11eb2 | 68 | #include "pathnames.h" |
69 | #include "atomicio.h" | |
70 | #include "canohost.h" | |
71 | #include "auth.h" | |
72 | #include "misc.h" | |
a5c9ffdb | 73 | #include "dispatch.h" |
e4d7f734 | 74 | #include "channels.h" |
1b34c1b3 | 75 | #include "session.h" |
8efc0c15 | 76 | |
77 | #ifdef LIBWRAP | |
78 | #include <tcpd.h> | |
79 | #include <syslog.h> | |
80 | int allow_severity = LOG_INFO; | |
81 | int deny_severity = LOG_WARNING; | |
82 | #endif /* LIBWRAP */ | |
83 | ||
84 | #ifndef O_NOCTTY | |
85 | #define O_NOCTTY 0 | |
86 | #endif | |
87 | ||
260d427b | 88 | #ifdef HAVE___PROGNAME |
89 | extern char *__progname; | |
90 | #else | |
91 | char *__progname; | |
92 | #endif | |
93 | ||
8efc0c15 | 94 | /* Server configuration options. */ |
95 | ServerOptions options; | |
96 | ||
97 | /* Name of the server configuration file. */ | |
42f11eb2 | 98 | char *config_file_name = _PATH_SERVER_CONFIG_FILE; |
8efc0c15 | 99 | |
6ae2364d | 100 | /* |
48e671d5 | 101 | * Flag indicating whether IPv4 or IPv6. This can be set on the command line. |
102 | * Default value is AF_UNSPEC means both IPv4 and IPv6. | |
103 | */ | |
59e76f33 | 104 | #ifdef IPV4_DEFAULT |
105 | int IPv4or6 = AF_INET; | |
106 | #else | |
48e671d5 | 107 | int IPv4or6 = AF_UNSPEC; |
59e76f33 | 108 | #endif |
48e671d5 | 109 | |
5260325f | 110 | /* |
111 | * Debug mode flag. This can be set on the command line. If debug | |
112 | * mode is enabled, extra debugging output will be sent to the system | |
113 | * log, the daemon will not go to background, and will exit after processing | |
114 | * the first connection. | |
115 | */ | |
8efc0c15 | 116 | int debug_flag = 0; |
117 | ||
f87f09aa | 118 | /* Flag indicating that the daemon should only test the configuration and keys. */ |
119 | int test_flag = 0; | |
120 | ||
8efc0c15 | 121 | /* Flag indicating that the daemon is being started from inetd. */ |
122 | int inetd_flag = 0; | |
123 | ||
0b6fbf03 | 124 | /* Flag indicating that sshd should not detach and become a daemon. */ |
125 | int no_daemon_flag = 0; | |
126 | ||
6a17f9c2 | 127 | /* debug goes to stderr unless inetd_flag is set */ |
128 | int log_stderr = 0; | |
129 | ||
8efc0c15 | 130 | /* Saved arguments to main(). */ |
131 | char **saved_argv; | |
4d33e531 | 132 | int saved_argc; |
8efc0c15 | 133 | |
aa3378df | 134 | /* |
48e671d5 | 135 | * The sockets that the server is listening; this is used in the SIGHUP |
136 | * signal handler. | |
aa3378df | 137 | */ |
48e671d5 | 138 | #define MAX_LISTEN_SOCKS 16 |
139 | int listen_socks[MAX_LISTEN_SOCKS]; | |
140 | int num_listen_socks = 0; | |
8efc0c15 | 141 | |
aa3378df | 142 | /* |
143 | * the client's version string, passed by sshd2 in compat mode. if != NULL, | |
144 | * sshd will skip the version-number exchange | |
145 | */ | |
5260325f | 146 | char *client_version_string = NULL; |
7368a6c8 | 147 | char *server_version_string = NULL; |
8efc0c15 | 148 | |
7a37c112 | 149 | /* for rekeying XXX fixme */ |
150 | Kex *xxx_kex; | |
151 | ||
aa3378df | 152 | /* |
153 | * Any really sensitive data in the application is contained in this | |
154 | * structure. The idea is that this structure could be locked into memory so | |
155 | * that the pages do not get written into swap. However, there are some | |
156 | * problems. The private key contains BIGNUMs, and we do not (in principle) | |
157 | * have access to the internals of them, and locking just the structure is | |
158 | * not very useful. Currently, memory locking is not implemented. | |
159 | */ | |
5260325f | 160 | struct { |
cb7bd922 | 161 | Key *server_key; /* ephemeral server key */ |
fa08c86b | 162 | Key *ssh1_host_key; /* ssh1 host key */ |
163 | Key **host_keys; /* all private host keys */ | |
164 | int have_ssh1_key; | |
165 | int have_ssh2_key; | |
00be5382 | 166 | u_char ssh1_cookie[SSH_SESSION_KEY_LENGTH]; |
8efc0c15 | 167 | } sensitive_data; |
168 | ||
aa3378df | 169 | /* |
59c97189 | 170 | * Flag indicating whether the RSA server key needs to be regenerated. |
171 | * Is set in the SIGALRM handler and cleared when the key is regenerated. | |
aa3378df | 172 | */ |
7a934d1b | 173 | static volatile sig_atomic_t key_do_regen = 0; |
8efc0c15 | 174 | |
c9130033 | 175 | /* This is set to true when a signal is received. */ |
7a934d1b | 176 | static volatile sig_atomic_t received_sighup = 0; |
177 | static volatile sig_atomic_t received_sigterm = 0; | |
8efc0c15 | 178 | |
7368a6c8 | 179 | /* session identifier, used by RSA-auth */ |
1e3b8b07 | 180 | u_char session_id[16]; |
e7c0f9d5 | 181 | |
a306f2dd | 182 | /* same for ssh2 */ |
1e3b8b07 | 183 | u_char *session_id2 = NULL; |
a306f2dd | 184 | int session_id2_len = 0; |
185 | ||
c345cf9d | 186 | /* record remote hostname or ip */ |
1e3b8b07 | 187 | u_int utmp_len = MAXHOSTNAMELEN; |
c345cf9d | 188 | |
7ace8c3b | 189 | /* options.max_startup sized array of fd ints */ |
190 | int *startup_pipes = NULL; | |
191 | int startup_pipe; /* in child */ | |
192 | ||
7368a6c8 | 193 | /* Prototypes for various functions defined later in this file. */ |
396c147e | 194 | void destroy_sensitive_data(void); |
c8d54615 | 195 | |
396c147e | 196 | static void do_ssh1_kex(void); |
197 | static void do_ssh2_kex(void); | |
94ec8c6b | 198 | |
48e671d5 | 199 | /* |
200 | * Close all listening sockets | |
201 | */ | |
396c147e | 202 | static void |
48e671d5 | 203 | close_listen_socks(void) |
204 | { | |
205 | int i; | |
206 | for (i = 0; i < num_listen_socks; i++) | |
207 | close(listen_socks[i]); | |
208 | num_listen_socks = -1; | |
209 | } | |
210 | ||
7ace8c3b | 211 | static void |
212 | close_startup_pipes(void) | |
213 | { | |
214 | int i; | |
215 | if (startup_pipes) | |
216 | for (i = 0; i < options.max_startups; i++) | |
217 | if (startup_pipes[i] != -1) | |
218 | close(startup_pipes[i]); | |
219 | } | |
220 | ||
5260325f | 221 | /* |
222 | * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP; | |
223 | * the effect is to reread the configuration file (and to regenerate | |
224 | * the server key). | |
225 | */ | |
396c147e | 226 | static void |
5260325f | 227 | sighup_handler(int sig) |
8efc0c15 | 228 | { |
6056eb35 | 229 | int save_errno = errno; |
230 | ||
5260325f | 231 | received_sighup = 1; |
232 | signal(SIGHUP, sighup_handler); | |
6056eb35 | 233 | errno = save_errno; |
8efc0c15 | 234 | } |
235 | ||
5260325f | 236 | /* |
237 | * Called from the main program after receiving SIGHUP. | |
238 | * Restarts the server. | |
239 | */ | |
396c147e | 240 | static void |
5ca51e19 | 241 | sighup_restart(void) |
8efc0c15 | 242 | { |
5260325f | 243 | log("Received SIGHUP; restarting."); |
48e671d5 | 244 | close_listen_socks(); |
7ace8c3b | 245 | close_startup_pipes(); |
5260325f | 246 | execv(saved_argv[0], saved_argv); |
1fe6a48f | 247 | log("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0], strerror(errno)); |
5260325f | 248 | exit(1); |
8efc0c15 | 249 | } |
250 | ||
5260325f | 251 | /* |
252 | * Generic signal handler for terminating signals in the master daemon. | |
5260325f | 253 | */ |
396c147e | 254 | static void |
5260325f | 255 | sigterm_handler(int sig) |
8efc0c15 | 256 | { |
c9130033 | 257 | received_sigterm = sig; |
8efc0c15 | 258 | } |
259 | ||
5260325f | 260 | /* |
261 | * SIGCHLD handler. This is called whenever a child dies. This will then | |
c9130033 | 262 | * reap any zombies left by exited children. |
5260325f | 263 | */ |
396c147e | 264 | static void |
5260325f | 265 | main_sigchld_handler(int sig) |
8efc0c15 | 266 | { |
5260325f | 267 | int save_errno = errno; |
268 | int status; | |
5ad13cd7 | 269 | |
5260325f | 270 | while (waitpid(-1, &status, WNOHANG) > 0) |
271 | ; | |
5ad13cd7 | 272 | |
5260325f | 273 | signal(SIGCHLD, main_sigchld_handler); |
274 | errno = save_errno; | |
8efc0c15 | 275 | } |
276 | ||
5260325f | 277 | /* |
278 | * Signal handler for the alarm after the login grace period has expired. | |
279 | */ | |
396c147e | 280 | static void |
5260325f | 281 | grace_alarm_handler(int sig) |
8efc0c15 | 282 | { |
c9130033 | 283 | /* XXX no idea how fix this signal handler */ |
284 | ||
5260325f | 285 | /* Close the connection. */ |
286 | packet_close(); | |
8efc0c15 | 287 | |
5260325f | 288 | /* Log error and exit. */ |
289 | fatal("Timeout before authentication for %s.", get_remote_ipaddr()); | |
290 | } | |
8efc0c15 | 291 | |
5260325f | 292 | /* |
293 | * Signal handler for the key regeneration alarm. Note that this | |
294 | * alarm only occurs in the daemon waiting for connections, and it does not | |
295 | * do anything with the private key or random state before forking. | |
296 | * Thus there should be no concurrency control/asynchronous execution | |
297 | * problems. | |
298 | */ | |
396c147e | 299 | static void |
cb7bd922 | 300 | generate_ephemeral_server_key(void) |
fa08c86b | 301 | { |
00be5382 | 302 | u_int32_t rand = 0; |
303 | int i; | |
304 | ||
cd332296 | 305 | verbose("Generating %s%d bit RSA key.", |
76ca7b01 | 306 | sensitive_data.server_key ? "new " : "", options.server_key_bits); |
fa08c86b | 307 | if (sensitive_data.server_key != NULL) |
308 | key_free(sensitive_data.server_key); | |
cd332296 | 309 | sensitive_data.server_key = key_generate(KEY_RSA1, |
76ca7b01 | 310 | options.server_key_bits); |
311 | verbose("RSA key generation complete."); | |
00be5382 | 312 | |
313 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { | |
314 | if (i % 4 == 0) | |
315 | rand = arc4random(); | |
316 | sensitive_data.ssh1_cookie[i] = rand & 0xff; | |
317 | rand >>= 8; | |
318 | } | |
319 | arc4random_stir(); | |
fa08c86b | 320 | } |
f546c780 | 321 | |
396c147e | 322 | static void |
5260325f | 323 | key_regeneration_alarm(int sig) |
324 | { | |
325 | int save_errno = errno; | |
59c97189 | 326 | signal(SIGALRM, SIG_DFL); |
5260325f | 327 | errno = save_errno; |
59c97189 | 328 | key_do_regen = 1; |
5260325f | 329 | } |
8efc0c15 | 330 | |
396c147e | 331 | static void |
7368a6c8 | 332 | sshd_exchange_identification(int sock_in, int sock_out) |
333 | { | |
a8be9f80 | 334 | int i, mismatch; |
7368a6c8 | 335 | int remote_major, remote_minor; |
a8be9f80 | 336 | int major, minor; |
7368a6c8 | 337 | char *s; |
338 | char buf[256]; /* Must not be larger than remote_version. */ | |
339 | char remote_version[256]; /* Must be at least as big as buf. */ | |
340 | ||
a8be9f80 | 341 | if ((options.protocol & SSH_PROTO_1) && |
342 | (options.protocol & SSH_PROTO_2)) { | |
343 | major = PROTOCOL_MAJOR_1; | |
344 | minor = 99; | |
345 | } else if (options.protocol & SSH_PROTO_2) { | |
346 | major = PROTOCOL_MAJOR_2; | |
347 | minor = PROTOCOL_MINOR_2; | |
348 | } else { | |
349 | major = PROTOCOL_MAJOR_1; | |
350 | minor = PROTOCOL_MINOR_1; | |
351 | } | |
352 | snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION); | |
7368a6c8 | 353 | server_version_string = xstrdup(buf); |
354 | ||
355 | if (client_version_string == NULL) { | |
356 | /* Send our protocol version identification. */ | |
357 | if (atomicio(write, sock_out, server_version_string, strlen(server_version_string)) | |
358 | != strlen(server_version_string)) { | |
30f60c34 | 359 | log("Could not write ident string to %s", get_remote_ipaddr()); |
7368a6c8 | 360 | fatal_cleanup(); |
361 | } | |
362 | ||
2ce1adf9 | 363 | /* Read other side's version identification. */ |
cd332296 | 364 | memset(buf, 0, sizeof(buf)); |
7368a6c8 | 365 | for (i = 0; i < sizeof(buf) - 1; i++) { |
e5a0294f | 366 | if (atomicio(read, sock_in, &buf[i], 1) != 1) { |
30f60c34 | 367 | log("Did not receive identification string from %s", |
c2b544a5 | 368 | get_remote_ipaddr()); |
7368a6c8 | 369 | fatal_cleanup(); |
370 | } | |
371 | if (buf[i] == '\r') { | |
8a169574 | 372 | buf[i] = 0; |
94ec8c6b | 373 | /* Kludge for F-Secure Macintosh < 1.0.2 */ |
374 | if (i == 12 && | |
375 | strncmp(buf, "SSH-1.5-W1.0", 12) == 0) | |
376 | break; | |
7368a6c8 | 377 | continue; |
7368a6c8 | 378 | } |
379 | if (buf[i] == '\n') { | |
8a169574 | 380 | buf[i] = 0; |
7368a6c8 | 381 | break; |
382 | } | |
383 | } | |
384 | buf[sizeof(buf) - 1] = 0; | |
385 | client_version_string = xstrdup(buf); | |
386 | } | |
387 | ||
388 | /* | |
389 | * Check that the versions match. In future this might accept | |
390 | * several versions and set appropriate flags to handle them. | |
391 | */ | |
392 | if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n", | |
393 | &remote_major, &remote_minor, remote_version) != 3) { | |
6ae2364d | 394 | s = "Protocol mismatch.\n"; |
7368a6c8 | 395 | (void) atomicio(write, sock_out, s, strlen(s)); |
396 | close(sock_in); | |
397 | close(sock_out); | |
398 | log("Bad protocol version identification '%.100s' from %s", | |
399 | client_version_string, get_remote_ipaddr()); | |
400 | fatal_cleanup(); | |
401 | } | |
402 | debug("Client protocol version %d.%d; client software version %.100s", | |
184eed6a | 403 | remote_major, remote_minor, remote_version); |
7368a6c8 | 404 | |
e78a59f5 | 405 | compat_datafellows(remote_version); |
406 | ||
3a1c54d4 | 407 | if (datafellows & SSH_BUG_SCANNER) { |
408 | log("scanned from %s with %s. Don't panic.", | |
409 | get_remote_ipaddr(), client_version_string); | |
410 | fatal_cleanup(); | |
411 | } | |
412 | ||
a8be9f80 | 413 | mismatch = 0; |
6aacefa7 | 414 | switch (remote_major) { |
7368a6c8 | 415 | case 1: |
a306f2dd | 416 | if (remote_minor == 99) { |
417 | if (options.protocol & SSH_PROTO_2) | |
418 | enable_compat20(); | |
419 | else | |
420 | mismatch = 1; | |
421 | break; | |
422 | } | |
a8be9f80 | 423 | if (!(options.protocol & SSH_PROTO_1)) { |
424 | mismatch = 1; | |
425 | break; | |
426 | } | |
7368a6c8 | 427 | if (remote_minor < 3) { |
089fbbd2 | 428 | packet_disconnect("Your ssh version is too old and " |
7368a6c8 | 429 | "is no longer supported. Please install a newer version."); |
430 | } else if (remote_minor == 3) { | |
431 | /* note that this disables agent-forwarding */ | |
432 | enable_compat13(); | |
433 | } | |
a8be9f80 | 434 | break; |
e78a59f5 | 435 | case 2: |
a8be9f80 | 436 | if (options.protocol & SSH_PROTO_2) { |
e78a59f5 | 437 | enable_compat20(); |
438 | break; | |
439 | } | |
440 | /* FALLTHROUGH */ | |
6ae2364d | 441 | default: |
a8be9f80 | 442 | mismatch = 1; |
443 | break; | |
444 | } | |
445 | chop(server_version_string); | |
a8be9f80 | 446 | debug("Local version string %.200s", server_version_string); |
447 | ||
448 | if (mismatch) { | |
7368a6c8 | 449 | s = "Protocol major versions differ.\n"; |
450 | (void) atomicio(write, sock_out, s, strlen(s)); | |
451 | close(sock_in); | |
452 | close(sock_out); | |
a8be9f80 | 453 | log("Protocol major versions differ for %s: %.200s vs. %.200s", |
454 | get_remote_ipaddr(), | |
455 | server_version_string, client_version_string); | |
7368a6c8 | 456 | fatal_cleanup(); |
7368a6c8 | 457 | } |
a306f2dd | 458 | } |
459 | ||
460 | ||
fa08c86b | 461 | /* Destroy the host and server keys. They will no longer be needed. */ |
a306f2dd | 462 | void |
463 | destroy_sensitive_data(void) | |
464 | { | |
fa08c86b | 465 | int i; |
466 | ||
467 | if (sensitive_data.server_key) { | |
468 | key_free(sensitive_data.server_key); | |
469 | sensitive_data.server_key = NULL; | |
470 | } | |
184eed6a | 471 | for (i = 0; i < options.num_host_key_files; i++) { |
fa08c86b | 472 | if (sensitive_data.host_keys[i]) { |
473 | key_free(sensitive_data.host_keys[i]); | |
474 | sensitive_data.host_keys[i] = NULL; | |
475 | } | |
476 | } | |
477 | sensitive_data.ssh1_host_key = NULL; | |
00be5382 | 478 | memset(sensitive_data.ssh1_cookie, 0, SSH_SESSION_KEY_LENGTH); |
fa08c86b | 479 | } |
fa08c86b | 480 | |
396c147e | 481 | static char * |
fa08c86b | 482 | list_hostkey_types(void) |
483 | { | |
3469eac4 | 484 | Buffer b; |
485 | char *p; | |
fa08c86b | 486 | int i; |
3469eac4 | 487 | |
488 | buffer_init(&b); | |
184eed6a | 489 | for (i = 0; i < options.num_host_key_files; i++) { |
fa08c86b | 490 | Key *key = sensitive_data.host_keys[i]; |
491 | if (key == NULL) | |
492 | continue; | |
6aacefa7 | 493 | switch (key->type) { |
fa08c86b | 494 | case KEY_RSA: |
495 | case KEY_DSA: | |
3469eac4 | 496 | if (buffer_len(&b) > 0) |
497 | buffer_append(&b, ",", 1); | |
498 | p = key_ssh_name(key); | |
499 | buffer_append(&b, p, strlen(p)); | |
fa08c86b | 500 | break; |
501 | } | |
502 | } | |
3469eac4 | 503 | buffer_append(&b, "\0", 1); |
504 | p = xstrdup(buffer_ptr(&b)); | |
505 | buffer_free(&b); | |
506 | debug("list_hostkey_types: %s", p); | |
507 | return p; | |
fa08c86b | 508 | } |
509 | ||
396c147e | 510 | static Key * |
fa08c86b | 511 | get_hostkey_by_type(int type) |
512 | { | |
513 | int i; | |
184eed6a | 514 | for (i = 0; i < options.num_host_key_files; i++) { |
fa08c86b | 515 | Key *key = sensitive_data.host_keys[i]; |
516 | if (key != NULL && key->type == type) | |
517 | return key; | |
518 | } | |
519 | return NULL; | |
7368a6c8 | 520 | } |
521 | ||
c345cf9d | 522 | /* |
523 | * returns 1 if connection should be dropped, 0 otherwise. | |
524 | * dropping starts at connection #max_startups_begin with a probability | |
525 | * of (max_startups_rate/100). the probability increases linearly until | |
526 | * all connections are dropped for startups > max_startups | |
527 | */ | |
396c147e | 528 | static int |
c345cf9d | 529 | drop_connection(int startups) |
530 | { | |
531 | double p, r; | |
532 | ||
533 | if (startups < options.max_startups_begin) | |
534 | return 0; | |
535 | if (startups >= options.max_startups) | |
536 | return 1; | |
537 | if (options.max_startups_rate == 100) | |
538 | return 1; | |
539 | ||
540 | p = 100 - options.max_startups_rate; | |
541 | p *= startups - options.max_startups_begin; | |
542 | p /= (double) (options.max_startups - options.max_startups_begin); | |
543 | p += options.max_startups_rate; | |
544 | p /= 100.0; | |
545 | r = arc4random() / (double) UINT_MAX; | |
546 | ||
547 | debug("drop_connection: p %g, r %g", p, r); | |
548 | return (r < p) ? 1 : 0; | |
549 | } | |
550 | ||
2717fa0f | 551 | static void |
552 | usage(void) | |
553 | { | |
554 | fprintf(stderr, "sshd version %s\n", SSH_VERSION); | |
555 | fprintf(stderr, "Usage: %s [options]\n", __progname); | |
556 | fprintf(stderr, "Options:\n"); | |
557 | fprintf(stderr, " -f file Configuration file (default %s)\n", _PATH_SERVER_CONFIG_FILE); | |
558 | fprintf(stderr, " -d Debugging mode (multiple -d means more debugging)\n"); | |
559 | fprintf(stderr, " -i Started from inetd\n"); | |
560 | fprintf(stderr, " -D Do not fork into daemon mode\n"); | |
561 | fprintf(stderr, " -t Only test configuration file and keys\n"); | |
562 | fprintf(stderr, " -q Quiet (no logging)\n"); | |
563 | fprintf(stderr, " -p port Listen on the specified port (default: 22)\n"); | |
564 | fprintf(stderr, " -k seconds Regenerate server key every this many seconds (default: 3600)\n"); | |
565 | fprintf(stderr, " -g seconds Grace period for authentication (default: 600)\n"); | |
566 | fprintf(stderr, " -b bits Size of server RSA key (default: 768 bits)\n"); | |
567 | fprintf(stderr, " -h file File from which to read host key (default: %s)\n", | |
568 | _PATH_HOST_KEY_FILE); | |
569 | fprintf(stderr, " -u len Maximum hostname length for utmp recording\n"); | |
570 | fprintf(stderr, " -4 Use IPv4 only\n"); | |
571 | fprintf(stderr, " -6 Use IPv6 only\n"); | |
572 | fprintf(stderr, " -o option Process the option as if it was read from a configuration file.\n"); | |
573 | exit(1); | |
574 | } | |
575 | ||
5260325f | 576 | /* |
577 | * Main program for the daemon. | |
578 | */ | |
8efc0c15 | 579 | int |
580 | main(int ac, char **av) | |
581 | { | |
5260325f | 582 | extern char *optarg; |
583 | extern int optind; | |
089fbbd2 | 584 | int opt, sock_in = 0, sock_out = 0, newsock, j, i, fdsetsz, on = 1; |
9da5c3c9 | 585 | pid_t pid; |
48e671d5 | 586 | socklen_t fromlen; |
48e671d5 | 587 | fd_set *fdset; |
588 | struct sockaddr_storage from; | |
5260325f | 589 | const char *remote_ip; |
590 | int remote_port; | |
5260325f | 591 | FILE *f; |
592 | struct linger linger; | |
48e671d5 | 593 | struct addrinfo *ai; |
594 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; | |
595 | int listen_sock, maxfd; | |
089fbbd2 | 596 | int startup_p[2]; |
597 | int startups = 0; | |
1b34c1b3 | 598 | Authctxt *authctxt; |
aeaa3d9e | 599 | Key *key; |
59c97189 | 600 | int ret, key_used = 0; |
5260325f | 601 | |
260d427b | 602 | __progname = get_progname(av[0]); |
264dce47 | 603 | init_rng(); |
604 | ||
1fe6a48f | 605 | /* Save argv. */ |
4d33e531 | 606 | saved_argc = ac; |
5260325f | 607 | saved_argv = av; |
5260325f | 608 | |
609 | /* Initialize configuration options to their default values. */ | |
610 | initialize_server_options(&options); | |
611 | ||
612 | /* Parse command-line arguments. */ | |
2717fa0f | 613 | while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:u:o:dDeiqtQ46")) != -1) { |
5260325f | 614 | switch (opt) { |
48e671d5 | 615 | case '4': |
616 | IPv4or6 = AF_INET; | |
617 | break; | |
618 | case '6': | |
619 | IPv4or6 = AF_INET6; | |
620 | break; | |
5260325f | 621 | case 'f': |
622 | config_file_name = optarg; | |
623 | break; | |
624 | case 'd': | |
bcbf86ec | 625 | if (0 == debug_flag) { |
626 | debug_flag = 1; | |
627 | options.log_level = SYSLOG_LEVEL_DEBUG1; | |
628 | } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) { | |
629 | options.log_level++; | |
630 | } else { | |
631 | fprintf(stderr, "Too high debugging level.\n"); | |
632 | exit(1); | |
633 | } | |
5260325f | 634 | break; |
0b6fbf03 | 635 | case 'D': |
636 | no_daemon_flag = 1; | |
637 | break; | |
ff14faf1 | 638 | case 'e': |
639 | log_stderr = 1; | |
640 | break; | |
5260325f | 641 | case 'i': |
642 | inetd_flag = 1; | |
643 | break; | |
644 | case 'Q': | |
9bd5b720 | 645 | /* ignored */ |
5260325f | 646 | break; |
647 | case 'q': | |
648 | options.log_level = SYSLOG_LEVEL_QUIET; | |
649 | break; | |
650 | case 'b': | |
651 | options.server_key_bits = atoi(optarg); | |
652 | break; | |
653 | case 'p': | |
48e671d5 | 654 | options.ports_from_cmdline = 1; |
bcbf86ec | 655 | if (options.num_ports >= MAX_PORTS) { |
656 | fprintf(stderr, "too many ports.\n"); | |
657 | exit(1); | |
658 | } | |
2d2a2c65 | 659 | options.ports[options.num_ports++] = a2port(optarg); |
660 | if (options.ports[options.num_ports-1] == 0) { | |
661 | fprintf(stderr, "Bad port number.\n"); | |
662 | exit(1); | |
663 | } | |
5260325f | 664 | break; |
665 | case 'g': | |
e2b1fb42 | 666 | if ((options.login_grace_time = convtime(optarg)) == -1) { |
667 | fprintf(stderr, "Invalid login grace time.\n"); | |
668 | exit(1); | |
669 | } | |
5260325f | 670 | break; |
671 | case 'k': | |
e2b1fb42 | 672 | if ((options.key_regeneration_time = convtime(optarg)) == -1) { |
673 | fprintf(stderr, "Invalid key regeneration interval.\n"); | |
674 | exit(1); | |
675 | } | |
5260325f | 676 | break; |
677 | case 'h': | |
fa08c86b | 678 | if (options.num_host_key_files >= MAX_HOSTKEYS) { |
679 | fprintf(stderr, "too many host keys.\n"); | |
680 | exit(1); | |
681 | } | |
682 | options.host_key_files[options.num_host_key_files++] = optarg; | |
5260325f | 683 | break; |
684 | case 'V': | |
685 | client_version_string = optarg; | |
686 | /* only makes sense with inetd_flag, i.e. no listen() */ | |
687 | inetd_flag = 1; | |
688 | break; | |
f87f09aa | 689 | case 't': |
690 | test_flag = 1; | |
691 | break; | |
c345cf9d | 692 | case 'u': |
693 | utmp_len = atoi(optarg); | |
694 | break; | |
2717fa0f | 695 | case 'o': |
184eed6a | 696 | if (process_server_config_line(&options, optarg, |
2717fa0f | 697 | "command-line", 0) != 0) |
184eed6a | 698 | exit(1); |
2717fa0f | 699 | break; |
5260325f | 700 | case '?': |
701 | default: | |
2717fa0f | 702 | usage(); |
703 | break; | |
5260325f | 704 | } |
705 | } | |
1a92bd7e | 706 | SSLeay_add_all_algorithms(); |
5e4a7219 | 707 | channel_set_af(IPv4or6); |
5260325f | 708 | |
48e671d5 | 709 | /* |
710 | * Force logging to stderr until we have loaded the private host | |
711 | * key (unless started from inetd) | |
712 | */ | |
1fe6a48f | 713 | log_init(__progname, |
980c9344 | 714 | options.log_level == SYSLOG_LEVEL_NOT_SET ? |
715 | SYSLOG_LEVEL_INFO : options.log_level, | |
716 | options.log_facility == SYSLOG_FACILITY_NOT_SET ? | |
717 | SYSLOG_FACILITY_AUTH : options.log_facility, | |
9bd5b720 | 718 | !inetd_flag); |
48e671d5 | 719 | |
1a23ac2c | 720 | #ifdef _CRAY |
721 | /* Cray can define user privs drop all prives now! | |
722 | * Not needed on PRIV_SU systems! | |
723 | */ | |
724 | drop_cray_privs(); | |
725 | #endif | |
726 | ||
e339aa53 | 727 | seed_rng(); |
728 | ||
5260325f | 729 | /* Read server configuration options from the configuration file. */ |
730 | read_server_config(&options, config_file_name); | |
731 | ||
732 | /* Fill in default values for those options not explicitly set. */ | |
733 | fill_default_server_options(&options); | |
734 | ||
5260325f | 735 | /* Check that there are no remaining arguments. */ |
736 | if (optind < ac) { | |
737 | fprintf(stderr, "Extra argument %s.\n", av[optind]); | |
738 | exit(1); | |
8efc0c15 | 739 | } |
5260325f | 740 | |
741 | debug("sshd version %.100s", SSH_VERSION); | |
742 | ||
fa08c86b | 743 | /* load private host keys */ |
744 | sensitive_data.host_keys = xmalloc(options.num_host_key_files*sizeof(Key*)); | |
184eed6a | 745 | for (i = 0; i < options.num_host_key_files; i++) |
1e3b8b07 | 746 | sensitive_data.host_keys[i] = NULL; |
fa08c86b | 747 | sensitive_data.server_key = NULL; |
748 | sensitive_data.ssh1_host_key = NULL; | |
749 | sensitive_data.have_ssh1_key = 0; | |
750 | sensitive_data.have_ssh2_key = 0; | |
a306f2dd | 751 | |
184eed6a | 752 | for (i = 0; i < options.num_host_key_files; i++) { |
aeaa3d9e | 753 | key = key_load_private(options.host_key_files[i], "", NULL); |
754 | sensitive_data.host_keys[i] = key; | |
fa08c86b | 755 | if (key == NULL) { |
58cfa257 | 756 | error("Could not load host key: %s", |
757 | options.host_key_files[i]); | |
aeaa3d9e | 758 | sensitive_data.host_keys[i] = NULL; |
fa08c86b | 759 | continue; |
a306f2dd | 760 | } |
6aacefa7 | 761 | switch (key->type) { |
fa08c86b | 762 | case KEY_RSA1: |
763 | sensitive_data.ssh1_host_key = key; | |
764 | sensitive_data.have_ssh1_key = 1; | |
765 | break; | |
766 | case KEY_RSA: | |
767 | case KEY_DSA: | |
768 | sensitive_data.have_ssh2_key = 1; | |
769 | break; | |
a306f2dd | 770 | } |
aeaa3d9e | 771 | debug("private host key: #%d type %d %s", i, key->type, |
772 | key_type(key)); | |
fa08c86b | 773 | } |
774 | if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) { | |
775 | log("Disabling protocol version 1. Could not load host key"); | |
776 | options.protocol &= ~SSH_PROTO_1; | |
777 | } | |
778 | if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) { | |
779 | log("Disabling protocol version 2. Could not load host key"); | |
780 | options.protocol &= ~SSH_PROTO_2; | |
a306f2dd | 781 | } |
6a416424 | 782 | if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) { |
54b974dc | 783 | log("sshd: no hostkeys available -- exiting."); |
5260325f | 784 | exit(1); |
785 | } | |
5260325f | 786 | |
a306f2dd | 787 | /* Check certain values for sanity. */ |
788 | if (options.protocol & SSH_PROTO_1) { | |
789 | if (options.server_key_bits < 512 || | |
790 | options.server_key_bits > 32768) { | |
791 | fprintf(stderr, "Bad server key size.\n"); | |
792 | exit(1); | |
793 | } | |
794 | /* | |
795 | * Check that server and host key lengths differ sufficiently. This | |
796 | * is necessary to make double encryption work with rsaref. Oh, I | |
797 | * hate software patents. I dont know if this can go? Niels | |
798 | */ | |
799 | if (options.server_key_bits > | |
fa08c86b | 800 | BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) - SSH_KEY_BITS_RESERVED && |
a306f2dd | 801 | options.server_key_bits < |
fa08c86b | 802 | BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED) { |
a306f2dd | 803 | options.server_key_bits = |
fa08c86b | 804 | BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED; |
a306f2dd | 805 | debug("Forcing server key to %d bits to make it differ from host key.", |
806 | options.server_key_bits); | |
807 | } | |
808 | } | |
809 | ||
f87f09aa | 810 | /* Configuration looks good, so exit if in test mode. */ |
811 | if (test_flag) | |
812 | exit(0); | |
813 | ||
77bb0bca | 814 | #ifdef HAVE_SCO_PROTECTED_PW |
815 | (void) set_auth_parameters(ac, av); | |
816 | #endif | |
817 | ||
a306f2dd | 818 | /* Initialize the log (it is reinitialized below in case we forked). */ |
5260325f | 819 | if (debug_flag && !inetd_flag) |
820 | log_stderr = 1; | |
1fe6a48f | 821 | log_init(__progname, options.log_level, options.log_facility, log_stderr); |
5260325f | 822 | |
a306f2dd | 823 | /* |
824 | * If not in debugging mode, and not started from inetd, disconnect | |
825 | * from the controlling terminal, and fork. The original process | |
826 | * exits. | |
827 | */ | |
0b6fbf03 | 828 | if (!(debug_flag || inetd_flag || no_daemon_flag)) { |
8efc0c15 | 829 | #ifdef TIOCNOTTY |
5260325f | 830 | int fd; |
8efc0c15 | 831 | #endif /* TIOCNOTTY */ |
5260325f | 832 | if (daemon(0, 0) < 0) |
833 | fatal("daemon() failed: %.200s", strerror(errno)); | |
834 | ||
835 | /* Disconnect from the controlling tty. */ | |
8efc0c15 | 836 | #ifdef TIOCNOTTY |
5ca51e19 | 837 | fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); |
5260325f | 838 | if (fd >= 0) { |
839 | (void) ioctl(fd, TIOCNOTTY, NULL); | |
840 | close(fd); | |
841 | } | |
8efc0c15 | 842 | #endif /* TIOCNOTTY */ |
8efc0c15 | 843 | } |
5260325f | 844 | /* Reinitialize the log (because of the fork above). */ |
1fe6a48f | 845 | log_init(__progname, options.log_level, options.log_facility, log_stderr); |
5260325f | 846 | |
5260325f | 847 | /* Initialize the random number generator. */ |
848 | arc4random_stir(); | |
849 | ||
850 | /* Chdir to the root directory so that the current disk can be | |
851 | unmounted if desired. */ | |
852 | chdir("/"); | |
184eed6a | 853 | |
1d3c30db | 854 | /* ignore SIGPIPE */ |
855 | signal(SIGPIPE, SIG_IGN); | |
5260325f | 856 | |
5260325f | 857 | /* Start listening for a socket, unless started from inetd. */ |
858 | if (inetd_flag) { | |
ec1f12d3 | 859 | int s1; |
5260325f | 860 | s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */ |
ec1f12d3 | 861 | dup(s1); |
5260325f | 862 | sock_in = dup(0); |
863 | sock_out = dup(1); | |
4c8722d9 | 864 | startup_pipe = -1; |
a306f2dd | 865 | /* |
866 | * We intentionally do not close the descriptors 0, 1, and 2 | |
867 | * as our code for setting the descriptors won\'t work if | |
868 | * ttyfd happens to be one of those. | |
869 | */ | |
5260325f | 870 | debug("inetd sockets after dupping: %d, %d", sock_in, sock_out); |
fa08c86b | 871 | if (options.protocol & SSH_PROTO_1) |
cb7bd922 | 872 | generate_ephemeral_server_key(); |
5260325f | 873 | } else { |
48e671d5 | 874 | for (ai = options.listen_addrs; ai; ai = ai->ai_next) { |
875 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) | |
876 | continue; | |
877 | if (num_listen_socks >= MAX_LISTEN_SOCKS) | |
878 | fatal("Too many listen sockets. " | |
879 | "Enlarge MAX_LISTEN_SOCKS"); | |
880 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, | |
881 | ntop, sizeof(ntop), strport, sizeof(strport), | |
882 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { | |
883 | error("getnameinfo failed"); | |
884 | continue; | |
885 | } | |
886 | /* Create socket for listening. */ | |
887 | listen_sock = socket(ai->ai_family, SOCK_STREAM, 0); | |
888 | if (listen_sock < 0) { | |
889 | /* kernel may not support ipv6 */ | |
890 | verbose("socket: %.100s", strerror(errno)); | |
891 | continue; | |
892 | } | |
893 | if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) { | |
894 | error("listen_sock O_NONBLOCK: %s", strerror(errno)); | |
895 | close(listen_sock); | |
896 | continue; | |
897 | } | |
898 | /* | |
899 | * Set socket options. We try to make the port | |
900 | * reusable and have it close as fast as possible | |
901 | * without waiting in unnecessary wait states on | |
902 | * close. | |
903 | */ | |
904 | setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, | |
db518d9b | 905 | &on, sizeof(on)); |
48e671d5 | 906 | linger.l_onoff = 1; |
907 | linger.l_linger = 5; | |
908 | setsockopt(listen_sock, SOL_SOCKET, SO_LINGER, | |
db518d9b | 909 | &linger, sizeof(linger)); |
48e671d5 | 910 | |
911 | debug("Bind to port %s on %s.", strport, ntop); | |
912 | ||
913 | /* Bind the socket to the desired port. */ | |
32ced054 | 914 | if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
915 | if (!ai->ai_next) | |
916 | error("Bind to port %s on %s failed: %.200s.", | |
917 | strport, ntop, strerror(errno)); | |
48e671d5 | 918 | close(listen_sock); |
919 | continue; | |
920 | } | |
921 | listen_socks[num_listen_socks] = listen_sock; | |
922 | num_listen_socks++; | |
923 | ||
924 | /* Start listening on the port. */ | |
925 | log("Server listening on %s port %s.", ntop, strport); | |
926 | if (listen(listen_sock, 5) < 0) | |
927 | fatal("listen: %.100s", strerror(errno)); | |
928 | ||
5260325f | 929 | } |
48e671d5 | 930 | freeaddrinfo(options.listen_addrs); |
931 | ||
932 | if (!num_listen_socks) | |
933 | fatal("Cannot bind any address."); | |
934 | ||
3aca00a3 | 935 | if (options.protocol & SSH_PROTO_1) |
936 | generate_ephemeral_server_key(); | |
937 | ||
938 | /* | |
939 | * Arrange to restart on SIGHUP. The handler needs | |
940 | * listen_sock. | |
941 | */ | |
942 | signal(SIGHUP, sighup_handler); | |
943 | ||
944 | signal(SIGTERM, sigterm_handler); | |
945 | signal(SIGQUIT, sigterm_handler); | |
946 | ||
947 | /* Arrange SIGCHLD to be caught. */ | |
948 | signal(SIGCHLD, main_sigchld_handler); | |
949 | ||
950 | /* Write out the pid file after the sigterm handler is setup */ | |
5260325f | 951 | if (!debug_flag) { |
aa3378df | 952 | /* |
97fb6912 | 953 | * Record our pid in /var/run/sshd.pid to make it |
954 | * easier to kill the correct sshd. We don't want to | |
955 | * do this before the bind above because the bind will | |
aa3378df | 956 | * fail if there already is a daemon, and this will |
957 | * overwrite any old pid in the file. | |
958 | */ | |
3c62e7eb | 959 | f = fopen(options.pid_file, "wb"); |
5260325f | 960 | if (f) { |
1e3b8b07 | 961 | fprintf(f, "%u\n", (u_int) getpid()); |
5260325f | 962 | fclose(f); |
963 | } | |
8efc0c15 | 964 | } |
5260325f | 965 | |
48e671d5 | 966 | /* setup fd set for listen */ |
089fbbd2 | 967 | fdset = NULL; |
48e671d5 | 968 | maxfd = 0; |
969 | for (i = 0; i < num_listen_socks; i++) | |
970 | if (listen_socks[i] > maxfd) | |
971 | maxfd = listen_socks[i]; | |
089fbbd2 | 972 | /* pipes connected to unauthenticated childs */ |
973 | startup_pipes = xmalloc(options.max_startups * sizeof(int)); | |
974 | for (i = 0; i < options.max_startups; i++) | |
975 | startup_pipes[i] = -1; | |
48e671d5 | 976 | |
aa3378df | 977 | /* |
978 | * Stay listening for connections until the system crashes or | |
979 | * the daemon is killed with a signal. | |
980 | */ | |
5260325f | 981 | for (;;) { |
982 | if (received_sighup) | |
983 | sighup_restart(); | |
089fbbd2 | 984 | if (fdset != NULL) |
985 | xfree(fdset); | |
b5c334cc | 986 | fdsetsz = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask); |
089fbbd2 | 987 | fdset = (fd_set *)xmalloc(fdsetsz); |
48e671d5 | 988 | memset(fdset, 0, fdsetsz); |
089fbbd2 | 989 | |
48e671d5 | 990 | for (i = 0; i < num_listen_socks; i++) |
991 | FD_SET(listen_socks[i], fdset); | |
089fbbd2 | 992 | for (i = 0; i < options.max_startups; i++) |
993 | if (startup_pipes[i] != -1) | |
994 | FD_SET(startup_pipes[i], fdset); | |
995 | ||
996 | /* Wait in select until there is a connection. */ | |
59c97189 | 997 | ret = select(maxfd+1, fdset, NULL, NULL, NULL); |
998 | if (ret < 0 && errno != EINTR) | |
999 | error("select: %.100s", strerror(errno)); | |
c9130033 | 1000 | if (received_sigterm) { |
1001 | log("Received signal %d; terminating.", | |
010f9726 | 1002 | (int) received_sigterm); |
c9130033 | 1003 | close_listen_socks(); |
1004 | unlink(options.pid_file); | |
1005 | exit(255); | |
1006 | } | |
59c97189 | 1007 | if (key_used && key_do_regen) { |
cb7bd922 | 1008 | generate_ephemeral_server_key(); |
59c97189 | 1009 | key_used = 0; |
1010 | key_do_regen = 0; | |
48e671d5 | 1011 | } |
59c97189 | 1012 | if (ret < 0) |
1013 | continue; | |
1014 | ||
089fbbd2 | 1015 | for (i = 0; i < options.max_startups; i++) |
1016 | if (startup_pipes[i] != -1 && | |
1017 | FD_ISSET(startup_pipes[i], fdset)) { | |
1018 | /* | |
1019 | * the read end of the pipe is ready | |
1020 | * if the child has closed the pipe | |
8abcdba4 | 1021 | * after successful authentication |
089fbbd2 | 1022 | * or if the child has died |
1023 | */ | |
1024 | close(startup_pipes[i]); | |
1025 | startup_pipes[i] = -1; | |
1026 | startups--; | |
1027 | } | |
48e671d5 | 1028 | for (i = 0; i < num_listen_socks; i++) { |
1029 | if (!FD_ISSET(listen_socks[i], fdset)) | |
5260325f | 1030 | continue; |
089fbbd2 | 1031 | fromlen = sizeof(from); |
1032 | newsock = accept(listen_socks[i], (struct sockaddr *)&from, | |
1033 | &fromlen); | |
1034 | if (newsock < 0) { | |
1035 | if (errno != EINTR && errno != EWOULDBLOCK) | |
1036 | error("accept: %.100s", strerror(errno)); | |
1037 | continue; | |
1038 | } | |
1039 | if (fcntl(newsock, F_SETFL, 0) < 0) { | |
1040 | error("newsock del O_NONBLOCK: %s", strerror(errno)); | |
5e8948af | 1041 | close(newsock); |
089fbbd2 | 1042 | continue; |
1043 | } | |
c345cf9d | 1044 | if (drop_connection(startups) == 1) { |
1045 | debug("drop connection #%d", startups); | |
089fbbd2 | 1046 | close(newsock); |
1047 | continue; | |
1048 | } | |
1049 | if (pipe(startup_p) == -1) { | |
1050 | close(newsock); | |
1051 | continue; | |
1052 | } | |
1053 | ||
1054 | for (j = 0; j < options.max_startups; j++) | |
1055 | if (startup_pipes[j] == -1) { | |
1056 | startup_pipes[j] = startup_p[0]; | |
1057 | if (maxfd < startup_p[0]) | |
1058 | maxfd = startup_p[0]; | |
1059 | startups++; | |
1060 | break; | |
1061 | } | |
2b87da3b | 1062 | |
aa3378df | 1063 | /* |
089fbbd2 | 1064 | * Got connection. Fork a child to handle it, unless |
1065 | * we are in debugging mode. | |
aa3378df | 1066 | */ |
089fbbd2 | 1067 | if (debug_flag) { |
aa3378df | 1068 | /* |
089fbbd2 | 1069 | * In debugging mode. Close the listening |
1070 | * socket, and start processing the | |
1071 | * connection without forking. | |
aa3378df | 1072 | */ |
089fbbd2 | 1073 | debug("Server will not fork when running in debugging mode."); |
48e671d5 | 1074 | close_listen_socks(); |
5260325f | 1075 | sock_in = newsock; |
1076 | sock_out = newsock; | |
5540ea9b | 1077 | startup_pipe = -1; |
3f7a7e4a | 1078 | pid = getpid(); |
5260325f | 1079 | break; |
089fbbd2 | 1080 | } else { |
1081 | /* | |
1082 | * Normal production daemon. Fork, and have | |
1083 | * the child process the connection. The | |
1084 | * parent continues listening. | |
1085 | */ | |
1086 | if ((pid = fork()) == 0) { | |
1087 | /* | |
1088 | * Child. Close the listening and max_startup | |
1089 | * sockets. Start using the accepted socket. | |
1090 | * Reinitialize logging (since our pid has | |
1091 | * changed). We break out of the loop to handle | |
1092 | * the connection. | |
1093 | */ | |
1094 | startup_pipe = startup_p[1]; | |
7ace8c3b | 1095 | close_startup_pipes(); |
089fbbd2 | 1096 | close_listen_socks(); |
1097 | sock_in = newsock; | |
1098 | sock_out = newsock; | |
1fe6a48f | 1099 | log_init(__progname, options.log_level, options.log_facility, log_stderr); |
089fbbd2 | 1100 | break; |
1101 | } | |
5260325f | 1102 | } |
5260325f | 1103 | |
089fbbd2 | 1104 | /* Parent. Stay in the loop. */ |
1105 | if (pid < 0) | |
1106 | error("fork: %.100s", strerror(errno)); | |
1107 | else | |
1108 | debug("Forked child %d.", pid); | |
5260325f | 1109 | |
089fbbd2 | 1110 | close(startup_p[1]); |
5260325f | 1111 | |
089fbbd2 | 1112 | /* Mark that the key has been used (it was "given" to the child). */ |
59c97189 | 1113 | if ((options.protocol & SSH_PROTO_1) && |
1114 | key_used == 0) { | |
1115 | /* Schedule server key regeneration alarm. */ | |
1116 | signal(SIGALRM, key_regeneration_alarm); | |
1117 | alarm(options.key_regeneration_time); | |
1118 | key_used = 1; | |
1119 | } | |
5260325f | 1120 | |
089fbbd2 | 1121 | arc4random_stir(); |
1122 | ||
1123 | /* Close the new socket (the child is now taking care of it). */ | |
1124 | close(newsock); | |
1125 | } | |
48e671d5 | 1126 | /* child process check (or debug mode) */ |
1127 | if (num_listen_socks < 0) | |
1128 | break; | |
5260325f | 1129 | } |
1130 | } | |
8efc0c15 | 1131 | |
5260325f | 1132 | /* This is the child processing a new connection. */ |
1133 | ||
aa3378df | 1134 | /* |
1135 | * Disable the key regeneration alarm. We will not regenerate the | |
1136 | * key since we are no longer in a position to give it to anyone. We | |
1137 | * will not restart on SIGHUP since it no longer makes sense. | |
1138 | */ | |
5260325f | 1139 | alarm(0); |
1140 | signal(SIGALRM, SIG_DFL); | |
1141 | signal(SIGHUP, SIG_DFL); | |
1142 | signal(SIGTERM, SIG_DFL); | |
1143 | signal(SIGQUIT, SIG_DFL); | |
1144 | signal(SIGCHLD, SIG_DFL); | |
9aaf9be4 | 1145 | signal(SIGINT, SIG_DFL); |
5260325f | 1146 | |
aa3378df | 1147 | /* |
1148 | * Set socket options for the connection. We want the socket to | |
1149 | * close as fast as possible without waiting for anything. If the | |
1150 | * connection is not a socket, these will do nothing. | |
1151 | */ | |
1152 | /* setsockopt(sock_in, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */ | |
5260325f | 1153 | linger.l_onoff = 1; |
1154 | linger.l_linger = 5; | |
db518d9b | 1155 | setsockopt(sock_in, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger)); |
5260325f | 1156 | |
b5c334cc | 1157 | /* Set keepalives if requested. */ |
1158 | if (options.keepalives && | |
db518d9b | 1159 | setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, |
b5c334cc | 1160 | sizeof(on)) < 0) |
1161 | error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno)); | |
1162 | ||
aa3378df | 1163 | /* |
1164 | * Register our connection. This turns encryption off because we do | |
1165 | * not have a key. | |
1166 | */ | |
5260325f | 1167 | packet_set_connection(sock_in, sock_out); |
1168 | ||
1169 | remote_port = get_remote_port(); | |
1170 | remote_ip = get_remote_ipaddr(); | |
1171 | ||
5260325f | 1172 | #ifdef LIBWRAP |
ac28afd8 | 1173 | /* Check whether logins are denied from this host. */ |
5260325f | 1174 | { |
1175 | struct request_info req; | |
8efc0c15 | 1176 | |
8fbc356d | 1177 | request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, 0); |
5260325f | 1178 | fromhost(&req); |
8efc0c15 | 1179 | |
5260325f | 1180 | if (!hosts_access(&req)) { |
ac28afd8 | 1181 | debug("Connection refused by tcp wrapper"); |
563834bb | 1182 | refuse(&req); |
ac28afd8 | 1183 | /* NOTREACHED */ |
1184 | fatal("libwrap refuse returns"); | |
5260325f | 1185 | } |
8efc0c15 | 1186 | } |
48e671d5 | 1187 | #endif /* LIBWRAP */ |
ac28afd8 | 1188 | |
5260325f | 1189 | /* Log the connection. */ |
1190 | verbose("Connection from %.500s port %d", remote_ip, remote_port); | |
8efc0c15 | 1191 | |
aa3378df | 1192 | /* |
1193 | * We don\'t want to listen forever unless the other side | |
1194 | * successfully authenticates itself. So we set up an alarm which is | |
1195 | * cleared after successful authentication. A limit of zero | |
1196 | * indicates no limit. Note that we don\'t set the alarm in debugging | |
1197 | * mode; it is just annoying to have the server exit just when you | |
1198 | * are about to discover the bug. | |
1199 | */ | |
5260325f | 1200 | signal(SIGALRM, grace_alarm_handler); |
1201 | if (!debug_flag) | |
1202 | alarm(options.login_grace_time); | |
1203 | ||
7368a6c8 | 1204 | sshd_exchange_identification(sock_in, sock_out); |
aa3378df | 1205 | /* |
6ffc9c88 | 1206 | * Check that the connection comes from a privileged port. |
1207 | * Rhosts-Authentication only makes sense from priviledged | |
aa3378df | 1208 | * programs. Of course, if the intruder has root access on his local |
1209 | * machine, he can connect from any port. So do not use these | |
1210 | * authentication methods from machines that you do not trust. | |
1211 | */ | |
44b1a8e5 | 1212 | if (options.rhosts_authentication && |
1213 | (remote_port >= IPPORT_RESERVED || | |
1214 | remote_port < IPPORT_RESERVED / 2)) { | |
6ffc9c88 | 1215 | debug("Rhosts Authentication disabled, " |
9ae3f727 | 1216 | "originating port %d not trusted.", remote_port); |
5260325f | 1217 | options.rhosts_authentication = 0; |
5260325f | 1218 | } |
ced49be2 | 1219 | #if defined(KRB4) && !defined(KRB5) |
48e671d5 | 1220 | if (!packet_connection_is_ipv4() && |
1221 | options.kerberos_authentication) { | |
1222 | debug("Kerberos Authentication disabled, only available for IPv4."); | |
1223 | options.kerberos_authentication = 0; | |
1224 | } | |
ced49be2 | 1225 | #endif /* KRB4 && !KRB5 */ |
abf1f107 | 1226 | #ifdef AFS |
1227 | /* If machine has AFS, set process authentication group. */ | |
1228 | if (k_hasafs()) { | |
1229 | k_setpag(); | |
1230 | k_unlog(); | |
1231 | } | |
1232 | #endif /* AFS */ | |
48e671d5 | 1233 | |
5260325f | 1234 | packet_set_nonblocking(); |
1235 | ||
7b2ea3a1 | 1236 | /* perform the key exchange */ |
7b2ea3a1 | 1237 | /* authenticate user and start session */ |
e78a59f5 | 1238 | if (compat20) { |
1239 | do_ssh2_kex(); | |
1b34c1b3 | 1240 | authctxt = do_authentication2(); |
e78a59f5 | 1241 | } else { |
1242 | do_ssh1_kex(); | |
1b34c1b3 | 1243 | authctxt = do_authentication(); |
e78a59f5 | 1244 | } |
1b34c1b3 | 1245 | |
1246 | /* Perform session preparation. */ | |
1247 | do_authenticated(authctxt); | |
1248 | ||
63284fbb | 1249 | /* The connection has been terminated. */ |
1250 | verbose("Closing connection to %.100s", remote_ip); | |
8efc0c15 | 1251 | |
d94aa2ae | 1252 | #ifdef USE_PAM |
a5c9cd31 | 1253 | finish_pam(); |
d94aa2ae | 1254 | #endif /* USE_PAM */ |
8efc0c15 | 1255 | |
5260325f | 1256 | packet_close(); |
1257 | exit(0); | |
1258 | } | |
8efc0c15 | 1259 | |
4f08e98d | 1260 | /* |
1261 | * Decrypt session_key_int using our private server key and private host key | |
1262 | * (key with larger modulus first). | |
1263 | */ | |
1264 | static int | |
1265 | ssh1_session_key(BIGNUM *session_key_int) | |
1266 | { | |
1267 | int rsafail = 0; | |
1268 | ||
1269 | if (BN_cmp(sensitive_data.server_key->rsa->n, sensitive_data.ssh1_host_key->rsa->n) > 0) { | |
1270 | /* Server key has bigger modulus. */ | |
1271 | if (BN_num_bits(sensitive_data.server_key->rsa->n) < | |
1272 | BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED) { | |
1273 | fatal("do_connection: %s: server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d", | |
1274 | get_remote_ipaddr(), | |
1275 | BN_num_bits(sensitive_data.server_key->rsa->n), | |
1276 | BN_num_bits(sensitive_data.ssh1_host_key->rsa->n), | |
1277 | SSH_KEY_BITS_RESERVED); | |
1278 | } | |
1279 | if (rsa_private_decrypt(session_key_int, session_key_int, | |
1280 | sensitive_data.server_key->rsa) <= 0) | |
1281 | rsafail++; | |
1282 | if (rsa_private_decrypt(session_key_int, session_key_int, | |
1283 | sensitive_data.ssh1_host_key->rsa) <= 0) | |
1284 | rsafail++; | |
1285 | } else { | |
1286 | /* Host key has bigger modulus (or they are equal). */ | |
1287 | if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) < | |
1288 | BN_num_bits(sensitive_data.server_key->rsa->n) + SSH_KEY_BITS_RESERVED) { | |
1289 | fatal("do_connection: %s: host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d", | |
1290 | get_remote_ipaddr(), | |
1291 | BN_num_bits(sensitive_data.ssh1_host_key->rsa->n), | |
1292 | BN_num_bits(sensitive_data.server_key->rsa->n), | |
1293 | SSH_KEY_BITS_RESERVED); | |
1294 | } | |
1295 | if (rsa_private_decrypt(session_key_int, session_key_int, | |
1296 | sensitive_data.ssh1_host_key->rsa) < 0) | |
1297 | rsafail++; | |
1298 | if (rsa_private_decrypt(session_key_int, session_key_int, | |
1299 | sensitive_data.server_key->rsa) < 0) | |
1300 | rsafail++; | |
1301 | } | |
1302 | return (rsafail); | |
1303 | } | |
5260325f | 1304 | /* |
7b2ea3a1 | 1305 | * SSH1 key exchange |
5260325f | 1306 | */ |
396c147e | 1307 | static void |
1e3b8b07 | 1308 | do_ssh1_kex(void) |
8efc0c15 | 1309 | { |
5260325f | 1310 | int i, len; |
46aa2d1f | 1311 | int rsafail = 0; |
5260325f | 1312 | BIGNUM *session_key_int; |
1e3b8b07 | 1313 | u_char session_key[SSH_SESSION_KEY_LENGTH]; |
1314 | u_char cookie[8]; | |
1315 | u_int cipher_type, auth_mask, protocol_flags; | |
5260325f | 1316 | u_int32_t rand = 0; |
1317 | ||
aa3378df | 1318 | /* |
1319 | * Generate check bytes that the client must send back in the user | |
1320 | * packet in order for it to be accepted; this is used to defy ip | |
1321 | * spoofing attacks. Note that this only works against somebody | |
1322 | * doing IP spoofing from a remote machine; any machine on the local | |
1323 | * network can still see outgoing packets and catch the random | |
1324 | * cookie. This only affects rhosts authentication, and this is one | |
1325 | * of the reasons why it is inherently insecure. | |
1326 | */ | |
5260325f | 1327 | for (i = 0; i < 8; i++) { |
1328 | if (i % 4 == 0) | |
1329 | rand = arc4random(); | |
7b2ea3a1 | 1330 | cookie[i] = rand & 0xff; |
5260325f | 1331 | rand >>= 8; |
1332 | } | |
1333 | ||
aa3378df | 1334 | /* |
1335 | * Send our public key. We include in the packet 64 bits of random | |
1336 | * data that must be matched in the reply in order to prevent IP | |
1337 | * spoofing. | |
1338 | */ | |
5260325f | 1339 | packet_start(SSH_SMSG_PUBLIC_KEY); |
1340 | for (i = 0; i < 8; i++) | |
7b2ea3a1 | 1341 | packet_put_char(cookie[i]); |
5260325f | 1342 | |
1343 | /* Store our public server RSA key. */ | |
fa08c86b | 1344 | packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n)); |
1345 | packet_put_bignum(sensitive_data.server_key->rsa->e); | |
1346 | packet_put_bignum(sensitive_data.server_key->rsa->n); | |
5260325f | 1347 | |
1348 | /* Store our public host RSA key. */ | |
fa08c86b | 1349 | packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n)); |
1350 | packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e); | |
1351 | packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n); | |
5260325f | 1352 | |
1353 | /* Put protocol flags. */ | |
1354 | packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN); | |
1355 | ||
1356 | /* Declare which ciphers we support. */ | |
94ec8c6b | 1357 | packet_put_int(cipher_mask_ssh1(0)); |
5260325f | 1358 | |
1359 | /* Declare supported authentication types. */ | |
1360 | auth_mask = 0; | |
1361 | if (options.rhosts_authentication) | |
1362 | auth_mask |= 1 << SSH_AUTH_RHOSTS; | |
1363 | if (options.rhosts_rsa_authentication) | |
1364 | auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA; | |
1365 | if (options.rsa_authentication) | |
1366 | auth_mask |= 1 << SSH_AUTH_RSA; | |
ced49be2 | 1367 | #if defined(KRB4) || defined(KRB5) |
5260325f | 1368 | if (options.kerberos_authentication) |
1369 | auth_mask |= 1 << SSH_AUTH_KERBEROS; | |
8efc0c15 | 1370 | #endif |
ced49be2 | 1371 | #if defined(AFS) || defined(KRB5) |
5260325f | 1372 | if (options.kerberos_tgt_passing) |
1373 | auth_mask |= 1 << SSH_PASS_KERBEROS_TGT; | |
ced49be2 | 1374 | #endif |
1375 | #ifdef AFS | |
5260325f | 1376 | if (options.afs_token_passing) |
1377 | auth_mask |= 1 << SSH_PASS_AFS_TOKEN; | |
8efc0c15 | 1378 | #endif |
5ba55ada | 1379 | if (options.challenge_response_authentication == 1) |
5260325f | 1380 | auth_mask |= 1 << SSH_AUTH_TIS; |
5260325f | 1381 | if (options.password_authentication) |
1382 | auth_mask |= 1 << SSH_AUTH_PASSWORD; | |
1383 | packet_put_int(auth_mask); | |
1384 | ||
1385 | /* Send the packet and wait for it to be sent. */ | |
1386 | packet_send(); | |
1387 | packet_write_wait(); | |
1388 | ||
fa08c86b | 1389 | debug("Sent %d bit server key and %d bit host key.", |
1390 | BN_num_bits(sensitive_data.server_key->rsa->n), | |
1391 | BN_num_bits(sensitive_data.ssh1_host_key->rsa->n)); | |
5260325f | 1392 | |
1393 | /* Read clients reply (cipher type and session key). */ | |
54a5250f | 1394 | packet_read_expect(SSH_CMSG_SESSION_KEY); |
5260325f | 1395 | |
2d86a6cc | 1396 | /* Get cipher type and check whether we accept this. */ |
5260325f | 1397 | cipher_type = packet_get_char(); |
1398 | ||
94ec8c6b | 1399 | if (!(cipher_mask_ssh1(0) & (1 << cipher_type))) |
2d86a6cc | 1400 | packet_disconnect("Warning: client selects unsupported cipher."); |
1401 | ||
5260325f | 1402 | /* Get check bytes from the packet. These must match those we |
1403 | sent earlier with the public key packet. */ | |
1404 | for (i = 0; i < 8; i++) | |
7b2ea3a1 | 1405 | if (cookie[i] != packet_get_char()) |
5260325f | 1406 | packet_disconnect("IP Spoofing check bytes do not match."); |
1407 | ||
1408 | debug("Encryption type: %.200s", cipher_name(cipher_type)); | |
1409 | ||
1410 | /* Get the encrypted integer. */ | |
b775c6f2 | 1411 | if ((session_key_int = BN_new()) == NULL) |
1412 | fatal("do_ssh1_kex: BN_new failed"); | |
20b279e6 | 1413 | packet_get_bignum(session_key_int); |
5260325f | 1414 | |
5260325f | 1415 | protocol_flags = packet_get_int(); |
1416 | packet_set_protocol_flags(protocol_flags); | |
95500969 | 1417 | packet_check_eom(); |
5260325f | 1418 | |
4f08e98d | 1419 | /* Decrypt session_key_int using host/server keys */ |
1420 | rsafail = ssh1_session_key(session_key_int); | |
aa3378df | 1421 | /* |
1422 | * Extract session key from the decrypted integer. The key is in the | |
1423 | * least significant 256 bits of the integer; the first byte of the | |
1424 | * key is in the highest bits. | |
1425 | */ | |
46aa2d1f | 1426 | if (!rsafail) { |
1427 | BN_mask_bits(session_key_int, sizeof(session_key) * 8); | |
1428 | len = BN_num_bytes(session_key_int); | |
1429 | if (len < 0 || len > sizeof(session_key)) { | |
1430 | error("do_connection: bad session key len from %s: " | |
5ca51e19 | 1431 | "session_key_int %d > sizeof(session_key) %lu", |
1432 | get_remote_ipaddr(), len, (u_long)sizeof(session_key)); | |
46aa2d1f | 1433 | rsafail++; |
1434 | } else { | |
1435 | memset(session_key, 0, sizeof(session_key)); | |
1436 | BN_bn2bin(session_key_int, | |
1437 | session_key + sizeof(session_key) - len); | |
00be5382 | 1438 | |
1439 | compute_session_id(session_id, cookie, | |
1440 | sensitive_data.ssh1_host_key->rsa->n, | |
1441 | sensitive_data.server_key->rsa->n); | |
1442 | /* | |
1443 | * Xor the first 16 bytes of the session key with the | |
1444 | * session id. | |
1445 | */ | |
1446 | for (i = 0; i < 16; i++) | |
1447 | session_key[i] ^= session_id[i]; | |
46aa2d1f | 1448 | } |
1449 | } | |
1450 | if (rsafail) { | |
00be5382 | 1451 | int bytes = BN_num_bytes(session_key_int); |
f6b1ba8f | 1452 | u_char *buf = xmalloc(bytes); |
00be5382 | 1453 | MD5_CTX md; |
1454 | ||
46aa2d1f | 1455 | log("do_connection: generating a fake encryption key"); |
00be5382 | 1456 | BN_bn2bin(session_key_int, buf); |
1457 | MD5_Init(&md); | |
1458 | MD5_Update(&md, buf, bytes); | |
1459 | MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH); | |
1460 | MD5_Final(session_key, &md); | |
1461 | MD5_Init(&md); | |
1462 | MD5_Update(&md, session_key, 16); | |
1463 | MD5_Update(&md, buf, bytes); | |
1464 | MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH); | |
1465 | MD5_Final(session_key + 16, &md); | |
1466 | memset(buf, 0, bytes); | |
1467 | xfree(buf); | |
0572fe75 | 1468 | for (i = 0; i < 16; i++) |
1469 | session_id[i] = session_key[i] ^ session_key[i + 16]; | |
46aa2d1f | 1470 | } |
63284fbb | 1471 | /* Destroy the private and public keys. They will no longer be needed. */ |
1472 | destroy_sensitive_data(); | |
00be5382 | 1473 | |
7b2ea3a1 | 1474 | /* Destroy the decrypted integer. It is no longer needed. */ |
1475 | BN_clear_free(session_key_int); | |
1476 | ||
5260325f | 1477 | /* Set the session key. From this on all communications will be encrypted. */ |
1478 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type); | |
1479 | ||
1480 | /* Destroy our copy of the session key. It is no longer needed. */ | |
1481 | memset(session_key, 0, sizeof(session_key)); | |
1482 | ||
1483 | debug("Received session key; encryption turned on."); | |
1484 | ||
1485 | /* Send an acknowledgement packet. Note that this packet is sent encrypted. */ | |
1486 | packet_start(SSH_SMSG_SUCCESS); | |
1487 | packet_send(); | |
1488 | packet_write_wait(); | |
5260325f | 1489 | } |
e78a59f5 | 1490 | |
1491 | /* | |
1492 | * SSH2 key exchange: diffie-hellman-group1-sha1 | |
1493 | */ | |
396c147e | 1494 | static void |
1e3b8b07 | 1495 | do_ssh2_kex(void) |
e78a59f5 | 1496 | { |
e78a59f5 | 1497 | Kex *kex; |
e78a59f5 | 1498 | |
a8be9f80 | 1499 | if (options.ciphers != NULL) { |
6ae2364d | 1500 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
a8be9f80 | 1501 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; |
1502 | } | |
1ad64a93 | 1503 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
1504 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]); | |
1505 | myproposal[PROPOSAL_ENC_ALGS_STOC] = | |
1506 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]); | |
1507 | ||
b2552997 | 1508 | if (options.macs != NULL) { |
1509 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = | |
1510 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; | |
1511 | } | |
fa08c86b | 1512 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = list_hostkey_types(); |
1513 | ||
7a37c112 | 1514 | /* start key exchange */ |
d8ee838b | 1515 | kex = kex_setup(myproposal); |
a5c9ffdb | 1516 | kex->server = 1; |
1517 | kex->client_version_string=client_version_string; | |
1518 | kex->server_version_string=server_version_string; | |
1519 | kex->load_host_key=&get_hostkey_by_type; | |
94ec8c6b | 1520 | |
7a37c112 | 1521 | xxx_kex = kex; |
1522 | ||
c422989b | 1523 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); |
94ec8c6b | 1524 | |
d1ac6175 | 1525 | session_id2 = kex->session_id; |
1526 | session_id2_len = kex->session_id_len; | |
1527 | ||
94ec8c6b | 1528 | #ifdef DEBUG_KEXDH |
1529 | /* send 1st encrypted/maced/compressed message */ | |
1530 | packet_start(SSH2_MSG_IGNORE); | |
1531 | packet_put_cstring("markus"); | |
1532 | packet_send(); | |
1533 | packet_write_wait(); | |
1534 | #endif | |
a5c9ffdb | 1535 | debug("KEX done"); |
e78a59f5 | 1536 | } |