Annotation of src/usr.bin/ssh/sshd.c, Revision 1.440
1.440 ! djm 1: /* $OpenBSD: sshd.c,v 1.439 2015/01/26 03:04:46 djm Exp $ */
1.86 markus 2: /*
1.65 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
1.126 deraadt 6: * This program is the ssh daemon. It listens for connections from clients,
7: * and performs authentication, executes use commands or shell, and forwards
1.65 deraadt 8: * information to/from the application to the user client over an encrypted
1.126 deraadt 9: * connection. This can also handle forwarding of X11, TCP/IP, and
10: * authentication agent connections.
1.98 markus 11: *
1.126 deraadt 12: * As far as I am concerned, the code I have written for this software
13: * can be used freely for any purpose. Any derived versions of this
14: * software must be clearly marked as such, and if the derived work is
15: * incompatible with the protocol description in the RFC file, it must be
16: * called by a name other than "ssh" or "Secure Shell".
17: *
18: * SSH2 implementation:
1.231 provos 19: * Privilege Separation:
1.126 deraadt 20: *
1.231 provos 21: * Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved.
22: * Copyright (c) 2002 Niels Provos. All rights reserved.
1.126 deraadt 23: *
24: * Redistribution and use in source and binary forms, with or without
25: * modification, are permitted provided that the following conditions
26: * are met:
27: * 1. Redistributions of source code must retain the above copyright
28: * notice, this list of conditions and the following disclaimer.
29: * 2. Redistributions in binary form must reproduce the above copyright
30: * notice, this list of conditions and the following disclaimer in the
31: * documentation and/or other materials provided with the distribution.
32: *
33: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
34: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
36: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
37: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
42: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.65 deraadt 43: */
1.1 deraadt 44:
1.343 deraadt 45: #include <sys/types.h>
1.320 stevesk 46: #include <sys/ioctl.h>
1.321 stevesk 47: #include <sys/wait.h>
1.343 deraadt 48: #include <sys/tree.h>
1.323 stevesk 49: #include <sys/stat.h>
1.334 stevesk 50: #include <sys/socket.h>
1.340 stevesk 51: #include <sys/time.h>
1.357 djm 52: #include <sys/queue.h>
1.319 stevesk 53:
1.336 stevesk 54: #include <errno.h>
1.335 stevesk 55: #include <fcntl.h>
1.338 stevesk 56: #include <netdb.h>
1.319 stevesk 57: #include <paths.h>
1.333 stevesk 58: #include <pwd.h>
1.322 stevesk 59: #include <signal.h>
1.342 stevesk 60: #include <stdio.h>
1.341 stevesk 61: #include <stdlib.h>
1.339 stevesk 62: #include <string.h>
1.344 dtucker 63: #include <unistd.h>
1.438 deraadt 64: #include <limits.h>
1.1 deraadt 65:
1.426 markus 66: #ifdef WITH_OPENSSL
1.155 markus 67: #include <openssl/bn.h>
1.426 markus 68: #endif
1.155 markus 69:
1.343 deraadt 70: #include "xmalloc.h"
1.155 markus 71: #include "ssh.h"
72: #include "ssh1.h"
73: #include "ssh2.h"
1.1 deraadt 74: #include "rsa.h"
1.171 djm 75: #include "sshpty.h"
1.1 deraadt 76: #include "packet.h"
1.155 markus 77: #include "log.h"
1.343 deraadt 78: #include "buffer.h"
1.428 millert 79: #include "misc.h"
1.1 deraadt 80: #include "servconf.h"
81: #include "uidswap.h"
1.33 markus 82: #include "compat.h"
1.155 markus 83: #include "cipher.h"
1.415 markus 84: #include "digest.h"
1.343 deraadt 85: #include "key.h"
1.98 markus 86: #include "kex.h"
87: #include "myproposal.h"
1.108 markus 88: #include "authfile.h"
1.154 markus 89: #include "pathnames.h"
1.155 markus 90: #include "atomicio.h"
91: #include "canohost.h"
1.343 deraadt 92: #include "hostfile.h"
1.155 markus 93: #include "auth.h"
1.404 markus 94: #include "authfd.h"
1.294 djm 95: #include "msg.h"
1.186 markus 96: #include "dispatch.h"
1.206 stevesk 97: #include "channels.h"
1.230 provos 98: #include "session.h"
1.231 provos 99: #include "monitor_mm.h"
100: #include "monitor.h"
1.343 deraadt 101: #ifdef GSSAPI
102: #include "ssh-gss.h"
103: #endif
1.231 provos 104: #include "monitor_wrap.h"
1.367 andreas 105: #include "roaming.h"
1.385 djm 106: #include "ssh-sandbox.h"
1.332 stevesk 107: #include "version.h"
1.432 djm 108: #include "ssherr.h"
1.1 deraadt 109:
110: #ifndef O_NOCTTY
111: #define O_NOCTTY 0
112: #endif
113:
1.296 djm 114: /* Re-exec fds */
115: #define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1)
116: #define REEXEC_STARTUP_PIPE_FD (STDERR_FILENO + 2)
117: #define REEXEC_CONFIG_PASS_FD (STDERR_FILENO + 3)
118: #define REEXEC_MIN_FREE_FD (STDERR_FILENO + 4)
119:
1.138 markus 120: extern char *__progname;
121:
1.1 deraadt 122: /* Server configuration options. */
123: ServerOptions options;
124:
125: /* Name of the server configuration file. */
1.154 markus 126: char *config_file_name = _PATH_SERVER_CONFIG_FILE;
1.1 deraadt 127:
1.105 markus 128: /*
1.65 deraadt 129: * Debug mode flag. This can be set on the command line. If debug
130: * mode is enabled, extra debugging output will be sent to the system
131: * log, the daemon will not go to background, and will exit after processing
132: * the first connection.
133: */
1.1 deraadt 134: int debug_flag = 0;
135:
1.203 stevesk 136: /* Flag indicating that the daemon should only test the configuration and keys. */
137: int test_flag = 0;
138:
1.1 deraadt 139: /* Flag indicating that the daemon is being started from inetd. */
140: int inetd_flag = 0;
141:
1.135 markus 142: /* Flag indicating that sshd should not detach and become a daemon. */
143: int no_daemon_flag = 0;
144:
1.47 markus 145: /* debug goes to stderr unless inetd_flag is set */
146: int log_stderr = 0;
147:
1.1 deraadt 148: /* Saved arguments to main(). */
149: char **saved_argv;
150:
1.294 djm 151: /* re-exec */
152: int rexeced_flag = 0;
153: int rexec_flag = 1;
154: int rexec_argc = 0;
155: char **rexec_argv;
156:
1.66 markus 157: /*
1.75 markus 158: * The sockets that the server is listening; this is used in the SIGHUP
159: * signal handler.
1.66 markus 160: */
1.75 markus 161: #define MAX_LISTEN_SOCKS 16
162: int listen_socks[MAX_LISTEN_SOCKS];
163: int num_listen_socks = 0;
1.1 deraadt 164:
1.66 markus 165: /*
166: * the client's version string, passed by sshd2 in compat mode. if != NULL,
167: * sshd will skip the version-number exchange
168: */
1.61 markus 169: char *client_version_string = NULL;
1.96 markus 170: char *server_version_string = NULL;
1.1 deraadt 171:
1.404 markus 172: /* Daemon's agent connection */
1.432 djm 173: int auth_sock = -1;
1.404 markus 174: int have_agent = 0;
175:
1.66 markus 176: /*
177: * Any really sensitive data in the application is contained in this
178: * structure. The idea is that this structure could be locked into memory so
179: * that the pages do not get written into swap. However, there are some
180: * problems. The private key contains BIGNUMs, and we do not (in principle)
181: * have access to the internals of them, and locking just the structure is
182: * not very useful. Currently, memory locking is not implemented.
183: */
1.64 markus 184: struct {
1.174 deraadt 185: Key *server_key; /* ephemeral server key */
1.134 markus 186: Key *ssh1_host_key; /* ssh1 host key */
187: Key **host_keys; /* all private host keys */
1.404 markus 188: Key **host_pubkeys; /* all public host keys */
1.373 djm 189: Key **host_certificates; /* all public host certificates */
1.134 markus 190: int have_ssh1_key;
191: int have_ssh2_key;
1.169 markus 192: u_char ssh1_cookie[SSH_SESSION_KEY_LENGTH];
1.1 deraadt 193: } sensitive_data;
194:
1.66 markus 195: /*
1.151 markus 196: * Flag indicating whether the RSA server key needs to be regenerated.
197: * Is set in the SIGALRM handler and cleared when the key is regenerated.
1.66 markus 198: */
1.212 markus 199: static volatile sig_atomic_t key_do_regen = 0;
1.1 deraadt 200:
1.199 markus 201: /* This is set to true when a signal is received. */
1.212 markus 202: static volatile sig_atomic_t received_sighup = 0;
203: static volatile sig_atomic_t received_sigterm = 0;
1.1 deraadt 204:
1.96 markus 205: /* session identifier, used by RSA-auth */
1.140 markus 206: u_char session_id[16];
1.96 markus 207:
1.108 markus 208: /* same for ssh2 */
1.140 markus 209: u_char *session_id2 = NULL;
1.269 markus 210: u_int session_id2_len = 0;
1.108 markus 211:
1.125 markus 212: /* record remote hostname or ip */
1.438 deraadt 213: u_int utmp_len = HOST_NAME_MAX+1;
1.125 markus 214:
1.211 markus 215: /* options.max_startup sized array of fd ints */
216: int *startup_pipes = NULL;
217: int startup_pipe; /* in child */
218:
1.231 provos 219: /* variables used for privilege separation */
1.337 dtucker 220: int use_privsep = -1;
1.285 dtucker 221: struct monitor *pmonitor = NULL;
1.386 djm 222: int privsep_is_preauth = 1;
1.231 provos 223:
1.278 markus 224: /* global authentication context */
225: Authctxt *the_authctxt = NULL;
226:
1.337 dtucker 227: /* sshd_config buffer */
228: Buffer cfg;
229:
1.299 dtucker 230: /* message to be displayed after login */
231: Buffer loginmsg;
232:
1.1 deraadt 233: /* Prototypes for various functions defined later in this file. */
1.200 itojun 234: void destroy_sensitive_data(void);
1.231 provos 235: void demote_sensitive_data(void);
1.87 markus 236:
1.426 markus 237: #ifdef WITH_SSH1
1.200 itojun 238: static void do_ssh1_kex(void);
1.426 markus 239: #endif
1.200 itojun 240: static void do_ssh2_kex(void);
1.129 provos 241:
1.87 markus 242: /*
1.75 markus 243: * Close all listening sockets
244: */
1.200 itojun 245: static void
1.75 markus 246: close_listen_socks(void)
247: {
248: int i;
1.250 deraadt 249:
1.75 markus 250: for (i = 0; i < num_listen_socks; i++)
251: close(listen_socks[i]);
252: num_listen_socks = -1;
253: }
254:
1.211 markus 255: static void
256: close_startup_pipes(void)
257: {
258: int i;
1.250 deraadt 259:
1.211 markus 260: if (startup_pipes)
261: for (i = 0; i < options.max_startups; i++)
262: if (startup_pipes[i] != -1)
263: close(startup_pipes[i]);
264: }
265:
1.75 markus 266: /*
1.65 deraadt 267: * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
268: * the effect is to reread the configuration file (and to regenerate
269: * the server key).
270: */
1.327 deraadt 271:
272: /*ARGSUSED*/
1.200 itojun 273: static void
1.64 markus 274: sighup_handler(int sig)
1.1 deraadt 275: {
1.210 deraadt 276: int save_errno = errno;
277:
1.64 markus 278: received_sighup = 1;
279: signal(SIGHUP, sighup_handler);
1.210 deraadt 280: errno = save_errno;
1.1 deraadt 281: }
282:
1.65 deraadt 283: /*
284: * Called from the main program after receiving SIGHUP.
285: * Restarts the server.
286: */
1.200 itojun 287: static void
1.165 itojun 288: sighup_restart(void)
1.1 deraadt 289: {
1.264 itojun 290: logit("Received SIGHUP; restarting.");
1.75 markus 291: close_listen_socks();
1.211 markus 292: close_startup_pipes();
1.349 dtucker 293: alarm(0); /* alarm timer persists across exec */
1.369 dtucker 294: signal(SIGHUP, SIG_IGN); /* will be restored after exec */
1.64 markus 295: execv(saved_argv[0], saved_argv);
1.264 itojun 296: logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
1.250 deraadt 297: strerror(errno));
1.64 markus 298: exit(1);
1.1 deraadt 299: }
300:
1.65 deraadt 301: /*
302: * Generic signal handler for terminating signals in the master daemon.
303: */
1.327 deraadt 304: /*ARGSUSED*/
1.200 itojun 305: static void
1.64 markus 306: sigterm_handler(int sig)
1.1 deraadt 307: {
1.199 markus 308: received_sigterm = sig;
1.1 deraadt 309: }
310:
1.65 deraadt 311: /*
312: * SIGCHLD handler. This is called whenever a child dies. This will then
1.199 markus 313: * reap any zombies left by exited children.
1.65 deraadt 314: */
1.327 deraadt 315: /*ARGSUSED*/
1.200 itojun 316: static void
1.64 markus 317: main_sigchld_handler(int sig)
1.1 deraadt 318: {
1.250 deraadt 319: int save_errno = errno;
1.239 markus 320: pid_t pid;
1.64 markus 321: int status;
1.60 deraadt 322:
1.239 markus 323: while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
324: (pid < 0 && errno == EINTR))
1.64 markus 325: ;
1.60 deraadt 326:
1.64 markus 327: signal(SIGCHLD, main_sigchld_handler);
328: errno = save_errno;
1.1 deraadt 329: }
330:
1.65 deraadt 331: /*
332: * Signal handler for the alarm after the login grace period has expired.
333: */
1.327 deraadt 334: /*ARGSUSED*/
1.200 itojun 335: static void
1.64 markus 336: grace_alarm_handler(int sig)
1.1 deraadt 337: {
1.285 dtucker 338: if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0)
339: kill(pmonitor->m_pid, SIGALRM);
1.394 djm 340:
341: /*
342: * Try to kill any processes that we have spawned, E.g. authorized
343: * keys command helpers.
344: */
345: if (getpgid(0) == getpid()) {
346: signal(SIGTERM, SIG_IGN);
1.416 djm 347: kill(0, SIGTERM);
1.394 djm 348: }
1.285 dtucker 349:
1.64 markus 350: /* Log error and exit. */
1.346 deraadt 351: sigdie("Timeout before authentication for %s", get_remote_ipaddr());
1.62 markus 352: }
353:
1.65 deraadt 354: /*
355: * Signal handler for the key regeneration alarm. Note that this
356: * alarm only occurs in the daemon waiting for connections, and it does not
357: * do anything with the private key or random state before forking.
358: * Thus there should be no concurrency control/asynchronous execution
359: * problems.
360: */
1.200 itojun 361: static void
1.174 deraadt 362: generate_ephemeral_server_key(void)
1.134 markus 363: {
1.191 markus 364: verbose("Generating %s%d bit RSA key.",
1.185 djm 365: sensitive_data.server_key ? "new " : "", options.server_key_bits);
1.134 markus 366: if (sensitive_data.server_key != NULL)
367: key_free(sensitive_data.server_key);
1.191 markus 368: sensitive_data.server_key = key_generate(KEY_RSA1,
1.185 djm 369: options.server_key_bits);
370: verbose("RSA key generation complete.");
1.169 markus 371:
1.356 djm 372: arc4random_buf(sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1.134 markus 373: }
1.147 deraadt 374:
1.327 deraadt 375: /*ARGSUSED*/
1.200 itojun 376: static void
1.64 markus 377: key_regeneration_alarm(int sig)
1.1 deraadt 378: {
1.64 markus 379: int save_errno = errno;
1.250 deraadt 380:
1.151 markus 381: signal(SIGALRM, SIG_DFL);
1.64 markus 382: errno = save_errno;
1.151 markus 383: key_do_regen = 1;
1.98 markus 384: }
385:
1.200 itojun 386: static void
1.96 markus 387: sshd_exchange_identification(int sock_in, int sock_out)
388: {
1.311 djm 389: u_int i;
390: int mismatch;
1.96 markus 391: int remote_major, remote_minor;
1.102 markus 392: int major, minor;
1.363 dtucker 393: char *s, *newline = "\n";
1.96 markus 394: char buf[256]; /* Must not be larger than remote_version. */
395: char remote_version[256]; /* Must be at least as big as buf. */
396:
1.103 markus 397: if ((options.protocol & SSH_PROTO_1) &&
398: (options.protocol & SSH_PROTO_2)) {
1.102 markus 399: major = PROTOCOL_MAJOR_1;
400: minor = 99;
401: } else if (options.protocol & SSH_PROTO_2) {
402: major = PROTOCOL_MAJOR_2;
403: minor = PROTOCOL_MINOR_2;
1.363 dtucker 404: newline = "\r\n";
1.102 markus 405: } else {
406: major = PROTOCOL_MAJOR_1;
407: minor = PROTOCOL_MINOR_1;
408: }
1.390 djm 409:
410: xasprintf(&server_version_string, "SSH-%d.%d-%.100s%s%s%s",
411: major, minor, SSH_VERSION,
412: *options.version_addendum == '\0' ? "" : " ",
413: options.version_addendum, newline);
1.96 markus 414:
1.272 markus 415: /* Send our protocol version identification. */
1.367 andreas 416: if (roaming_atomicio(vwrite, sock_out, server_version_string,
1.272 markus 417: strlen(server_version_string))
418: != strlen(server_version_string)) {
419: logit("Could not write ident string to %s", get_remote_ipaddr());
1.278 markus 420: cleanup_exit(255);
1.272 markus 421: }
422:
423: /* Read other sides version identification. */
424: memset(buf, 0, sizeof(buf));
425: for (i = 0; i < sizeof(buf) - 1; i++) {
1.367 andreas 426: if (roaming_atomicio(read, sock_in, &buf[i], 1) != 1) {
1.272 markus 427: logit("Did not receive identification string from %s",
428: get_remote_ipaddr());
1.278 markus 429: cleanup_exit(255);
1.96 markus 430: }
1.272 markus 431: if (buf[i] == '\r') {
432: buf[i] = 0;
433: /* Kludge for F-Secure Macintosh < 1.0.2 */
434: if (i == 12 &&
435: strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
1.96 markus 436: break;
1.272 markus 437: continue;
438: }
439: if (buf[i] == '\n') {
440: buf[i] = 0;
441: break;
1.96 markus 442: }
443: }
1.272 markus 444: buf[sizeof(buf) - 1] = 0;
445: client_version_string = xstrdup(buf);
1.96 markus 446:
447: /*
448: * Check that the versions match. In future this might accept
449: * several versions and set appropriate flags to handle them.
450: */
451: if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
452: &remote_major, &remote_minor, remote_version) != 3) {
1.105 markus 453: s = "Protocol mismatch.\n";
1.271 deraadt 454: (void) atomicio(vwrite, sock_out, s, strlen(s));
1.408 djm 455: logit("Bad protocol version identification '%.100s' "
456: "from %s port %d", client_version_string,
457: get_remote_ipaddr(), get_remote_port());
1.411 djm 458: close(sock_in);
459: close(sock_out);
1.278 markus 460: cleanup_exit(255);
1.96 markus 461: }
462: debug("Client protocol version %d.%d; client software version %.100s",
1.217 deraadt 463: remote_major, remote_minor, remote_version);
1.96 markus 464:
1.436 markus 465: active_state->compat = compat_datafellows(remote_version);
1.260 mickey 466:
1.413 djm 467: if ((datafellows & SSH_BUG_PROBE) != 0) {
1.264 itojun 468: logit("probed from %s with %s. Don't panic.",
1.260 mickey 469: get_remote_ipaddr(), client_version_string);
1.278 markus 470: cleanup_exit(255);
1.260 mickey 471: }
1.413 djm 472: if ((datafellows & SSH_BUG_SCANNER) != 0) {
1.264 itojun 473: logit("scanned from %s with %s. Don't panic.",
1.175 deraadt 474: get_remote_ipaddr(), client_version_string);
1.278 markus 475: cleanup_exit(255);
1.175 deraadt 476: }
1.414 djm 477: if ((datafellows & SSH_BUG_RSASIGMD5) != 0) {
1.413 djm 478: logit("Client version \"%.100s\" uses unsafe RSA signature "
479: "scheme; disabling use of RSA keys", remote_version);
1.414 djm 480: }
481: if ((datafellows & SSH_BUG_DERIVEKEY) != 0) {
482: fatal("Client version \"%.100s\" uses unsafe key agreement; "
483: "refusing connection", remote_version);
484: }
1.98 markus 485:
1.102 markus 486: mismatch = 0;
1.214 deraadt 487: switch (remote_major) {
1.96 markus 488: case 1:
1.108 markus 489: if (remote_minor == 99) {
490: if (options.protocol & SSH_PROTO_2)
491: enable_compat20();
492: else
493: mismatch = 1;
494: break;
495: }
1.102 markus 496: if (!(options.protocol & SSH_PROTO_1)) {
497: mismatch = 1;
498: break;
499: }
1.96 markus 500: if (remote_minor < 3) {
1.121 provos 501: packet_disconnect("Your ssh version is too old and "
1.96 markus 502: "is no longer supported. Please install a newer version.");
503: } else if (remote_minor == 3) {
504: /* note that this disables agent-forwarding */
505: enable_compat13();
506: }
1.102 markus 507: break;
1.98 markus 508: case 2:
1.102 markus 509: if (options.protocol & SSH_PROTO_2) {
1.98 markus 510: enable_compat20();
511: break;
512: }
1.99 markus 513: /* FALLTHROUGH */
1.105 markus 514: default:
1.102 markus 515: mismatch = 1;
516: break;
517: }
518: chop(server_version_string);
519: debug("Local version string %.200s", server_version_string);
520:
521: if (mismatch) {
1.96 markus 522: s = "Protocol major versions differ.\n";
1.271 deraadt 523: (void) atomicio(vwrite, sock_out, s, strlen(s));
1.96 markus 524: close(sock_in);
525: close(sock_out);
1.264 itojun 526: logit("Protocol major versions differ for %s: %.200s vs. %.200s",
1.102 markus 527: get_remote_ipaddr(),
528: server_version_string, client_version_string);
1.278 markus 529: cleanup_exit(255);
1.96 markus 530: }
1.108 markus 531: }
532:
1.134 markus 533: /* Destroy the host and server keys. They will no longer be needed. */
1.108 markus 534: void
535: destroy_sensitive_data(void)
536: {
1.134 markus 537: int i;
538:
539: if (sensitive_data.server_key) {
540: key_free(sensitive_data.server_key);
541: sensitive_data.server_key = NULL;
542: }
1.217 deraadt 543: for (i = 0; i < options.num_host_key_files; i++) {
1.134 markus 544: if (sensitive_data.host_keys[i]) {
545: key_free(sensitive_data.host_keys[i]);
546: sensitive_data.host_keys[i] = NULL;
547: }
1.373 djm 548: if (sensitive_data.host_certificates[i]) {
549: key_free(sensitive_data.host_certificates[i]);
550: sensitive_data.host_certificates[i] = NULL;
551: }
1.134 markus 552: }
553: sensitive_data.ssh1_host_key = NULL;
1.418 djm 554: explicit_bzero(sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1.134 markus 555: }
556:
1.231 provos 557: /* Demote private to public keys for network child */
558: void
559: demote_sensitive_data(void)
560: {
561: Key *tmp;
562: int i;
563:
564: if (sensitive_data.server_key) {
565: tmp = key_demote(sensitive_data.server_key);
566: key_free(sensitive_data.server_key);
567: sensitive_data.server_key = tmp;
568: }
569:
570: for (i = 0; i < options.num_host_key_files; i++) {
571: if (sensitive_data.host_keys[i]) {
572: tmp = key_demote(sensitive_data.host_keys[i]);
573: key_free(sensitive_data.host_keys[i]);
574: sensitive_data.host_keys[i] = tmp;
575: if (tmp->type == KEY_RSA1)
576: sensitive_data.ssh1_host_key = tmp;
577: }
1.373 djm 578: /* Certs do not need demotion */
1.231 provos 579: }
580:
581: /* We do not clear ssh1_host key and cookie. XXX - Okay Niels? */
582: }
583:
1.233 markus 584: static void
1.231 provos 585: privsep_preauth_child(void)
586: {
1.253 deraadt 587: gid_t gidset[1];
1.250 deraadt 588: struct passwd *pw;
1.231 provos 589:
590: /* Enable challenge-response authentication for privilege separation */
591: privsep_challenge_enable();
1.419 djm 592:
1.420 markus 593: #ifdef GSSAPI
1.419 djm 594: /* Cache supported mechanism OIDs for later use */
595: if (options.gss_authentication)
596: ssh_gssapi_prepare_supported_oids();
1.420 markus 597: #endif
1.231 provos 598:
599: /* Demote the private keys to public keys. */
600: demote_sensitive_data();
601:
1.235 stevesk 602: if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
1.240 djm 603: fatal("Privilege separation user %s does not exist",
604: SSH_PRIVSEP_USER);
1.418 djm 605: explicit_bzero(pw->pw_passwd, strlen(pw->pw_passwd));
1.235 stevesk 606: endpwent();
607:
1.255 deraadt 608: /* Change our root directory */
1.232 stevesk 609: if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
610: fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
611: strerror(errno));
1.231 provos 612: if (chdir("/") == -1)
1.236 stevesk 613: fatal("chdir(\"/\"): %s", strerror(errno));
1.234 markus 614:
1.231 provos 615: /* Drop our privileges */
1.235 stevesk 616: debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
617: (u_int)pw->pw_gid);
1.251 markus 618: #if 0
1.287 djm 619: /* XXX not ready, too heavy after chroot */
1.235 stevesk 620: do_setusercontext(pw);
1.251 markus 621: #else
622: gidset[0] = pw->pw_gid;
623: if (setgroups(1, gidset) < 0)
624: fatal("setgroups: %.100s", strerror(errno));
625: permanently_set_uid(pw);
626: #endif
1.231 provos 627: }
628:
1.278 markus 629: static int
630: privsep_preauth(Authctxt *authctxt)
1.237 markus 631: {
1.432 djm 632: int status, r;
1.237 markus 633: pid_t pid;
1.384 djm 634: struct ssh_sandbox *box = NULL;
1.237 markus 635:
636: /* Set up unprivileged child process to deal with network data */
1.242 mouring 637: pmonitor = monitor_init();
1.237 markus 638: /* Store a pointer to the kex for later rekeying */
1.434 markus 639: pmonitor->m_pkex = &active_state->kex;
1.237 markus 640:
1.393 djm 641: if (use_privsep == PRIVSEP_ON)
1.384 djm 642: box = ssh_sandbox_init();
1.237 markus 643: pid = fork();
644: if (pid == -1) {
645: fatal("fork of unprivileged child failed");
646: } else if (pid != 0) {
1.245 mpech 647: debug2("Network child is on pid %ld", (long)pid);
1.237 markus 648:
1.392 markus 649: pmonitor->m_pid = pid;
1.432 djm 650: if (have_agent) {
651: r = ssh_get_authentication_socket(&auth_sock);
652: if (r != 0) {
653: error("Could not get agent socket: %s",
654: ssh_err(r));
655: have_agent = 0;
656: }
657: }
1.384 djm 658: if (box != NULL)
659: ssh_sandbox_parent_preauth(box, pid);
1.278 markus 660: monitor_child_preauth(authctxt, pmonitor);
1.237 markus 661:
662: /* Sync memory */
1.242 mouring 663: monitor_sync(pmonitor);
1.237 markus 664:
665: /* Wait for the child's exit status */
1.384 djm 666: while (waitpid(pid, &status, 0) < 0) {
1.386 djm 667: if (errno == EINTR)
668: continue;
669: pmonitor->m_pid = -1;
670: fatal("%s: waitpid: %s", __func__, strerror(errno));
1.384 djm 671: }
1.386 djm 672: privsep_is_preauth = 0;
673: pmonitor->m_pid = -1;
1.384 djm 674: if (WIFEXITED(status)) {
675: if (WEXITSTATUS(status) != 0)
676: fatal("%s: preauth child exited with status %d",
677: __func__, WEXITSTATUS(status));
678: } else if (WIFSIGNALED(status))
679: fatal("%s: preauth child terminated by signal %d",
680: __func__, WTERMSIG(status));
681: if (box != NULL)
682: ssh_sandbox_parent_finish(box);
683: return 1;
1.237 markus 684: } else {
685: /* child */
1.383 djm 686: close(pmonitor->m_sendfd);
687: close(pmonitor->m_log_recvfd);
1.237 markus 688:
1.383 djm 689: /* Arrange for logging to be sent to the monitor */
690: set_log_handler(mm_log_handler, pmonitor);
1.237 markus 691:
692: /* Demote the child */
693: if (getuid() == 0 || geteuid() == 0)
694: privsep_preauth_child();
1.238 stevesk 695: setproctitle("%s", "[net]");
1.384 djm 696: if (box != NULL)
697: ssh_sandbox_child(box);
698:
699: return 0;
1.237 markus 700: }
701: }
702:
1.233 markus 703: static void
1.237 markus 704: privsep_postauth(Authctxt *authctxt)
1.231 provos 705: {
706: if (authctxt->pw->pw_uid == 0 || options.use_login) {
707: /* File descriptor passing is broken or root login */
708: use_privsep = 0;
1.315 djm 709: goto skip;
1.231 provos 710: }
1.234 markus 711:
1.231 provos 712: /* New socket pair */
1.242 mouring 713: monitor_reinit(pmonitor);
1.231 provos 714:
1.242 mouring 715: pmonitor->m_pid = fork();
716: if (pmonitor->m_pid == -1)
1.231 provos 717: fatal("fork of unprivileged child failed");
1.242 mouring 718: else if (pmonitor->m_pid != 0) {
1.364 markus 719: verbose("User child is on pid %ld", (long)pmonitor->m_pid);
1.307 otto 720: buffer_clear(&loginmsg);
1.242 mouring 721: monitor_child_postauth(pmonitor);
1.231 provos 722:
723: /* NEVERREACHED */
724: exit(0);
725: }
726:
1.383 djm 727: /* child */
728:
1.242 mouring 729: close(pmonitor->m_sendfd);
1.383 djm 730: pmonitor->m_sendfd = -1;
1.231 provos 731:
732: /* Demote the private keys to public keys. */
733: demote_sensitive_data();
1.354 djm 734:
1.231 provos 735: /* Drop privileges */
736: do_setusercontext(authctxt->pw);
737:
1.315 djm 738: skip:
1.231 provos 739: /* It is safe now to apply the key state */
1.242 mouring 740: monitor_apply_keystate(pmonitor);
1.312 markus 741:
742: /*
743: * Tell the packet layer that authentication was successful, since
744: * this information is not part of the key state.
745: */
746: packet_set_authenticated();
1.231 provos 747: }
748:
1.200 itojun 749: static char *
1.134 markus 750: list_hostkey_types(void)
751: {
1.223 markus 752: Buffer b;
1.281 jakob 753: const char *p;
754: char *ret;
1.134 markus 755: int i;
1.373 djm 756: Key *key;
1.223 markus 757:
758: buffer_init(&b);
1.217 deraadt 759: for (i = 0; i < options.num_host_key_files; i++) {
1.373 djm 760: key = sensitive_data.host_keys[i];
1.134 markus 761: if (key == NULL)
1.404 markus 762: key = sensitive_data.host_pubkeys[i];
763: if (key == NULL)
1.134 markus 764: continue;
1.214 deraadt 765: switch (key->type) {
1.134 markus 766: case KEY_RSA:
767: case KEY_DSA:
1.378 djm 768: case KEY_ECDSA:
1.412 markus 769: case KEY_ED25519:
1.223 markus 770: if (buffer_len(&b) > 0)
771: buffer_append(&b, ",", 1);
772: p = key_ssh_name(key);
773: buffer_append(&b, p, strlen(p));
1.134 markus 774: break;
775: }
1.373 djm 776: /* If the private key has a cert peer, then list that too */
777: key = sensitive_data.host_certificates[i];
778: if (key == NULL)
779: continue;
780: switch (key->type) {
1.375 djm 781: case KEY_RSA_CERT_V00:
782: case KEY_DSA_CERT_V00:
1.373 djm 783: case KEY_RSA_CERT:
784: case KEY_DSA_CERT:
1.378 djm 785: case KEY_ECDSA_CERT:
1.412 markus 786: case KEY_ED25519_CERT:
1.373 djm 787: if (buffer_len(&b) > 0)
788: buffer_append(&b, ",", 1);
789: p = key_ssh_name(key);
790: buffer_append(&b, p, strlen(p));
791: break;
792: }
1.134 markus 793: }
1.223 markus 794: buffer_append(&b, "\0", 1);
1.281 jakob 795: ret = xstrdup(buffer_ptr(&b));
1.223 markus 796: buffer_free(&b);
1.281 jakob 797: debug("list_hostkey_types: %s", ret);
798: return ret;
1.134 markus 799: }
800:
1.373 djm 801: static Key *
1.440 ! djm 802: get_hostkey_by_type(int type, int nid, int need_private, struct ssh *ssh)
1.134 markus 803: {
804: int i;
1.373 djm 805: Key *key;
1.250 deraadt 806:
1.217 deraadt 807: for (i = 0; i < options.num_host_key_files; i++) {
1.375 djm 808: switch (type) {
809: case KEY_RSA_CERT_V00:
810: case KEY_DSA_CERT_V00:
811: case KEY_RSA_CERT:
812: case KEY_DSA_CERT:
1.378 djm 813: case KEY_ECDSA_CERT:
1.412 markus 814: case KEY_ED25519_CERT:
1.373 djm 815: key = sensitive_data.host_certificates[i];
1.375 djm 816: break;
817: default:
1.373 djm 818: key = sensitive_data.host_keys[i];
1.404 markus 819: if (key == NULL && !need_private)
820: key = sensitive_data.host_pubkeys[i];
1.375 djm 821: break;
822: }
1.440 ! djm 823: if (key != NULL && key->type == type &&
! 824: (key->type != KEY_ECDSA || key->ecdsa_nid == nid))
1.373 djm 825: return need_private ?
826: sensitive_data.host_keys[i] : key;
1.134 markus 827: }
828: return NULL;
1.96 markus 829: }
830:
1.231 provos 831: Key *
1.440 ! djm 832: get_hostkey_public_by_type(int type, int nid, struct ssh *ssh)
1.373 djm 833: {
1.440 ! djm 834: return get_hostkey_by_type(type, nid, 0, ssh);
1.373 djm 835: }
836:
837: Key *
1.440 ! djm 838: get_hostkey_private_by_type(int type, int nid, struct ssh *ssh)
1.373 djm 839: {
1.440 ! djm 840: return get_hostkey_by_type(type, nid, 1, ssh);
1.373 djm 841: }
842:
843: Key *
1.231 provos 844: get_hostkey_by_index(int ind)
845: {
846: if (ind < 0 || ind >= options.num_host_key_files)
847: return (NULL);
848: return (sensitive_data.host_keys[ind]);
849: }
850:
1.404 markus 851: Key *
1.435 markus 852: get_hostkey_public_by_index(int ind, struct ssh *ssh)
1.404 markus 853: {
854: if (ind < 0 || ind >= options.num_host_key_files)
855: return (NULL);
856: return (sensitive_data.host_pubkeys[ind]);
857: }
858:
1.231 provos 859: int
1.435 markus 860: get_hostkey_index(Key *key, struct ssh *ssh)
1.231 provos 861: {
862: int i;
1.250 deraadt 863:
1.231 provos 864: for (i = 0; i < options.num_host_key_files; i++) {
1.373 djm 865: if (key_is_cert(key)) {
866: if (key == sensitive_data.host_certificates[i])
867: return (i);
868: } else {
869: if (key == sensitive_data.host_keys[i])
870: return (i);
1.404 markus 871: if (key == sensitive_data.host_pubkeys[i])
872: return (i);
1.373 djm 873: }
1.231 provos 874: }
875: return (-1);
876: }
877:
1.439 djm 878: /* Inform the client of all hostkeys */
879: static void
880: notify_hostkeys(struct ssh *ssh)
881: {
882: struct sshbuf *buf;
883: struct sshkey *key;
884: int i, nkeys, r;
885: char *fp;
886:
887: if ((buf = sshbuf_new()) == NULL)
888: fatal("%s: sshbuf_new", __func__);
889: for (i = nkeys = 0; i < options.num_host_key_files; i++) {
890: key = get_hostkey_public_by_index(i, ssh);
891: if (key == NULL || key->type == KEY_UNSPEC ||
892: key->type == KEY_RSA1 || sshkey_is_cert(key))
893: continue;
894: fp = sshkey_fingerprint(key, options.fingerprint_hash,
895: SSH_FP_DEFAULT);
896: debug3("%s: key %d: %s %s", __func__, i,
897: sshkey_ssh_name(key), fp);
898: free(fp);
899: if ((r = sshkey_puts(key, buf)) != 0)
900: fatal("%s: couldn't put hostkey %d: %s",
901: __func__, i, ssh_err(r));
902: nkeys++;
903: }
904: if (nkeys == 0)
905: fatal("%s: no hostkeys", __func__);
906: debug3("%s: send %d hostkeys", __func__, nkeys);
907: packet_start(SSH2_MSG_GLOBAL_REQUEST);
908: packet_put_cstring("hostkeys@openssh.com");
909: packet_put_char(0); /* want-reply */
910: packet_put_string(sshbuf_ptr(buf), sshbuf_len(buf));
911: packet_send();
912: }
913:
1.124 markus 914: /*
915: * returns 1 if connection should be dropped, 0 otherwise.
916: * dropping starts at connection #max_startups_begin with a probability
917: * of (max_startups_rate/100). the probability increases linearly until
918: * all connections are dropped for startups > max_startups
919: */
1.200 itojun 920: static int
1.124 markus 921: drop_connection(int startups)
922: {
1.303 mickey 923: int p, r;
1.124 markus 924:
925: if (startups < options.max_startups_begin)
926: return 0;
927: if (startups >= options.max_startups)
928: return 1;
929: if (options.max_startups_rate == 100)
930: return 1;
931:
932: p = 100 - options.max_startups_rate;
933: p *= startups - options.max_startups_begin;
1.303 mickey 934: p /= options.max_startups - options.max_startups_begin;
1.124 markus 935: p += options.max_startups_rate;
1.356 djm 936: r = arc4random_uniform(100);
1.124 markus 937:
1.304 djm 938: debug("drop_connection: p %d, r %d", p, r);
1.124 markus 939: return (r < p) ? 1 : 0;
940: }
941:
1.215 markus 942: static void
943: usage(void)
944: {
1.290 markus 945: fprintf(stderr, "%s, %s\n",
1.426 markus 946: SSH_VERSION,
947: #ifdef WITH_OPENSSL
948: SSLeay_version(SSLEAY_VERSION)
949: #else
950: "without OpenSSL"
951: #endif
952: );
1.289 markus 953: fprintf(stderr,
1.373 djm 954: "usage: sshd [-46DdeiqTt] [-b bits] [-C connection_spec] [-c host_cert_file]\n"
1.399 dtucker 955: " [-E log_file] [-f config_file] [-g login_grace_time]\n"
956: " [-h host_key_file] [-k key_gen_time] [-o option] [-p port]\n"
957: " [-u len]\n"
1.289 markus 958: );
1.215 markus 959: exit(1);
960: }
961:
1.294 djm 962: static void
963: send_rexec_state(int fd, Buffer *conf)
964: {
965: Buffer m;
966:
967: debug3("%s: entering fd = %d config len %d", __func__, fd,
968: buffer_len(conf));
969:
970: /*
971: * Protocol from reexec master to child:
972: * string configuration
973: * u_int ephemeral_key_follows
974: * bignum e (only if ephemeral_key_follows == 1)
975: * bignum n "
976: * bignum d "
977: * bignum iqmp "
978: * bignum p "
979: * bignum q "
980: */
981: buffer_init(&m);
982: buffer_put_cstring(&m, buffer_ptr(conf));
983:
1.426 markus 984: #ifdef WITH_SSH1
1.298 deraadt 985: if (sensitive_data.server_key != NULL &&
1.294 djm 986: sensitive_data.server_key->type == KEY_RSA1) {
987: buffer_put_int(&m, 1);
988: buffer_put_bignum(&m, sensitive_data.server_key->rsa->e);
989: buffer_put_bignum(&m, sensitive_data.server_key->rsa->n);
990: buffer_put_bignum(&m, sensitive_data.server_key->rsa->d);
991: buffer_put_bignum(&m, sensitive_data.server_key->rsa->iqmp);
992: buffer_put_bignum(&m, sensitive_data.server_key->rsa->p);
993: buffer_put_bignum(&m, sensitive_data.server_key->rsa->q);
994: } else
1.426 markus 995: #endif
1.294 djm 996: buffer_put_int(&m, 0);
997:
998: if (ssh_msg_send(fd, 0, &m) == -1)
999: fatal("%s: ssh_msg_send failed", __func__);
1000:
1001: buffer_free(&m);
1002:
1003: debug3("%s: done", __func__);
1004: }
1005:
1006: static void
1007: recv_rexec_state(int fd, Buffer *conf)
1008: {
1009: Buffer m;
1010: char *cp;
1011: u_int len;
1012:
1013: debug3("%s: entering fd = %d", __func__, fd);
1014:
1015: buffer_init(&m);
1016:
1017: if (ssh_msg_recv(fd, &m) == -1)
1018: fatal("%s: ssh_msg_recv failed", __func__);
1019: if (buffer_get_char(&m) != 0)
1020: fatal("%s: rexec version mismatch", __func__);
1021:
1022: cp = buffer_get_string(&m, &len);
1023: if (conf != NULL)
1024: buffer_append(conf, cp, len + 1);
1.402 djm 1025: free(cp);
1.294 djm 1026:
1027: if (buffer_get_int(&m)) {
1.426 markus 1028: #ifdef WITH_SSH1
1.294 djm 1029: if (sensitive_data.server_key != NULL)
1030: key_free(sensitive_data.server_key);
1031: sensitive_data.server_key = key_new_private(KEY_RSA1);
1032: buffer_get_bignum(&m, sensitive_data.server_key->rsa->e);
1033: buffer_get_bignum(&m, sensitive_data.server_key->rsa->n);
1034: buffer_get_bignum(&m, sensitive_data.server_key->rsa->d);
1035: buffer_get_bignum(&m, sensitive_data.server_key->rsa->iqmp);
1036: buffer_get_bignum(&m, sensitive_data.server_key->rsa->p);
1037: buffer_get_bignum(&m, sensitive_data.server_key->rsa->q);
1.427 djm 1038: if (rsa_generate_additional_parameters(
1039: sensitive_data.server_key->rsa) != 0)
1040: fatal("%s: rsa_generate_additional_parameters "
1041: "error", __func__);
1.426 markus 1042: #else
1043: fatal("ssh1 not supported");
1044: #endif
1.294 djm 1045: }
1046: buffer_free(&m);
1047:
1048: debug3("%s: done", __func__);
1049: }
1050:
1.345 djm 1051: /* Accept a connection from inetd */
1052: static void
1053: server_accept_inetd(int *sock_in, int *sock_out)
1054: {
1055: int fd;
1056:
1057: startup_pipe = -1;
1058: if (rexeced_flag) {
1059: close(REEXEC_CONFIG_PASS_FD);
1060: *sock_in = *sock_out = dup(STDIN_FILENO);
1061: if (!debug_flag) {
1062: startup_pipe = dup(REEXEC_STARTUP_PIPE_FD);
1063: close(REEXEC_STARTUP_PIPE_FD);
1064: }
1065: } else {
1066: *sock_in = dup(STDIN_FILENO);
1067: *sock_out = dup(STDOUT_FILENO);
1068: }
1069: /*
1070: * We intentionally do not close the descriptors 0, 1, and 2
1071: * as our code for setting the descriptors won't work if
1072: * ttyfd happens to be one of those.
1073: */
1074: if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1075: dup2(fd, STDIN_FILENO);
1076: dup2(fd, STDOUT_FILENO);
1.403 dtucker 1077: if (!log_stderr)
1078: dup2(fd, STDERR_FILENO);
1079: if (fd > (log_stderr ? STDERR_FILENO : STDOUT_FILENO))
1.345 djm 1080: close(fd);
1081: }
1082: debug("inetd sockets after dupping: %d, %d", *sock_in, *sock_out);
1083: }
1084:
1085: /*
1086: * Listen for TCP connections
1087: */
1088: static void
1089: server_listen(void)
1090: {
1091: int ret, listen_sock, on = 1;
1092: struct addrinfo *ai;
1093: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1094:
1095: for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
1096: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1097: continue;
1098: if (num_listen_socks >= MAX_LISTEN_SOCKS)
1099: fatal("Too many listen sockets. "
1100: "Enlarge MAX_LISTEN_SOCKS");
1101: if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
1102: ntop, sizeof(ntop), strport, sizeof(strport),
1103: NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1104: error("getnameinfo failed: %.100s",
1.352 dtucker 1105: ssh_gai_strerror(ret));
1.345 djm 1106: continue;
1107: }
1108: /* Create socket for listening. */
1.370 dtucker 1109: listen_sock = socket(ai->ai_family, ai->ai_socktype,
1110: ai->ai_protocol);
1.345 djm 1111: if (listen_sock < 0) {
1112: /* kernel may not support ipv6 */
1113: verbose("socket: %.100s", strerror(errno));
1114: continue;
1115: }
1116: if (set_nonblock(listen_sock) == -1) {
1117: close(listen_sock);
1118: continue;
1119: }
1120: /*
1121: * Set socket options.
1122: * Allow local port reuse in TIME_WAIT.
1123: */
1124: if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
1125: &on, sizeof(on)) == -1)
1126: error("setsockopt SO_REUSEADDR: %s", strerror(errno));
1127:
1128: debug("Bind to port %s on %s.", strport, ntop);
1129:
1130: /* Bind the socket to the desired port. */
1131: if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1132: error("Bind to port %s on %s failed: %.200s.",
1133: strport, ntop, strerror(errno));
1134: close(listen_sock);
1135: continue;
1136: }
1137: listen_socks[num_listen_socks] = listen_sock;
1138: num_listen_socks++;
1139:
1140: /* Start listening on the port. */
1141: if (listen(listen_sock, SSH_LISTEN_BACKLOG) < 0)
1142: fatal("listen on [%s]:%s: %.100s",
1143: ntop, strport, strerror(errno));
1144: logit("Server listening on %s port %s.", ntop, strport);
1145: }
1146: freeaddrinfo(options.listen_addrs);
1147:
1148: if (!num_listen_socks)
1149: fatal("Cannot bind any address.");
1150: }
1151:
1152: /*
1153: * The main TCP accept loop. Note that, for the non-debug case, returns
1154: * from this function are in a forked subprocess.
1155: */
1156: static void
1157: server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
1158: {
1159: fd_set *fdset;
1160: int i, j, ret, maxfd;
1161: int key_used = 0, startups = 0;
1162: int startup_p[2] = { -1 , -1 };
1163: struct sockaddr_storage from;
1164: socklen_t fromlen;
1165: pid_t pid;
1166:
1167: /* setup fd set for accept */
1168: fdset = NULL;
1169: maxfd = 0;
1170: for (i = 0; i < num_listen_socks; i++)
1171: if (listen_socks[i] > maxfd)
1172: maxfd = listen_socks[i];
1173: /* pipes connected to unauthenticated childs */
1174: startup_pipes = xcalloc(options.max_startups, sizeof(int));
1175: for (i = 0; i < options.max_startups; i++)
1176: startup_pipes[i] = -1;
1177:
1178: /*
1179: * Stay listening for connections until the system crashes or
1180: * the daemon is killed with a signal.
1181: */
1182: for (;;) {
1183: if (received_sighup)
1184: sighup_restart();
1185: if (fdset != NULL)
1.402 djm 1186: free(fdset);
1.345 djm 1187: fdset = (fd_set *)xcalloc(howmany(maxfd + 1, NFDBITS),
1188: sizeof(fd_mask));
1189:
1190: for (i = 0; i < num_listen_socks; i++)
1191: FD_SET(listen_socks[i], fdset);
1192: for (i = 0; i < options.max_startups; i++)
1193: if (startup_pipes[i] != -1)
1194: FD_SET(startup_pipes[i], fdset);
1195:
1196: /* Wait in select until there is a connection. */
1197: ret = select(maxfd+1, fdset, NULL, NULL, NULL);
1198: if (ret < 0 && errno != EINTR)
1199: error("select: %.100s", strerror(errno));
1200: if (received_sigterm) {
1201: logit("Received signal %d; terminating.",
1202: (int) received_sigterm);
1203: close_listen_socks();
1.430 djm 1204: if (options.pid_file != NULL)
1205: unlink(options.pid_file);
1.382 djm 1206: exit(received_sigterm == SIGTERM ? 0 : 255);
1.345 djm 1207: }
1208: if (key_used && key_do_regen) {
1209: generate_ephemeral_server_key();
1210: key_used = 0;
1211: key_do_regen = 0;
1212: }
1213: if (ret < 0)
1214: continue;
1215:
1216: for (i = 0; i < options.max_startups; i++)
1217: if (startup_pipes[i] != -1 &&
1218: FD_ISSET(startup_pipes[i], fdset)) {
1219: /*
1220: * the read end of the pipe is ready
1221: * if the child has closed the pipe
1222: * after successful authentication
1223: * or if the child has died
1224: */
1225: close(startup_pipes[i]);
1226: startup_pipes[i] = -1;
1227: startups--;
1228: }
1229: for (i = 0; i < num_listen_socks; i++) {
1230: if (!FD_ISSET(listen_socks[i], fdset))
1231: continue;
1232: fromlen = sizeof(from);
1233: *newsock = accept(listen_socks[i],
1234: (struct sockaddr *)&from, &fromlen);
1235: if (*newsock < 0) {
1.398 markus 1236: if (errno != EINTR && errno != EWOULDBLOCK &&
1237: errno != ECONNABORTED)
1.389 djm 1238: error("accept: %.100s",
1239: strerror(errno));
1240: if (errno == EMFILE || errno == ENFILE)
1241: usleep(100 * 1000);
1.345 djm 1242: continue;
1243: }
1244: if (unset_nonblock(*newsock) == -1) {
1245: close(*newsock);
1246: continue;
1247: }
1248: if (drop_connection(startups) == 1) {
1249: debug("drop connection #%d", startups);
1250: close(*newsock);
1251: continue;
1252: }
1253: if (pipe(startup_p) == -1) {
1254: close(*newsock);
1255: continue;
1256: }
1257:
1258: if (rexec_flag && socketpair(AF_UNIX,
1259: SOCK_STREAM, 0, config_s) == -1) {
1260: error("reexec socketpair: %s",
1261: strerror(errno));
1262: close(*newsock);
1263: close(startup_p[0]);
1264: close(startup_p[1]);
1265: continue;
1266: }
1267:
1268: for (j = 0; j < options.max_startups; j++)
1269: if (startup_pipes[j] == -1) {
1270: startup_pipes[j] = startup_p[0];
1271: if (maxfd < startup_p[0])
1272: maxfd = startup_p[0];
1273: startups++;
1274: break;
1275: }
1276:
1277: /*
1278: * Got connection. Fork a child to handle it, unless
1279: * we are in debugging mode.
1280: */
1281: if (debug_flag) {
1282: /*
1283: * In debugging mode. Close the listening
1284: * socket, and start processing the
1285: * connection without forking.
1286: */
1287: debug("Server will not fork when running in debugging mode.");
1288: close_listen_socks();
1289: *sock_in = *newsock;
1290: *sock_out = *newsock;
1291: close(startup_p[0]);
1292: close(startup_p[1]);
1293: startup_pipe = -1;
1294: pid = getpid();
1295: if (rexec_flag) {
1296: send_rexec_state(config_s[0],
1297: &cfg);
1298: close(config_s[0]);
1299: }
1300: break;
1301: }
1302:
1303: /*
1304: * Normal production daemon. Fork, and have
1305: * the child process the connection. The
1306: * parent continues listening.
1307: */
1308: if ((pid = fork()) == 0) {
1309: /*
1310: * Child. Close the listening and
1311: * max_startup sockets. Start using
1312: * the accepted socket. Reinitialize
1313: * logging (since our pid has changed).
1314: * We break out of the loop to handle
1315: * the connection.
1316: */
1317: startup_pipe = startup_p[1];
1318: close_startup_pipes();
1319: close_listen_socks();
1320: *sock_in = *newsock;
1321: *sock_out = *newsock;
1322: log_init(__progname,
1323: options.log_level,
1324: options.log_facility,
1325: log_stderr);
1326: if (rexec_flag)
1327: close(config_s[0]);
1328: break;
1329: }
1330:
1331: /* Parent. Stay in the loop. */
1332: if (pid < 0)
1333: error("fork: %.100s", strerror(errno));
1334: else
1335: debug("Forked child %ld.", (long)pid);
1336:
1337: close(startup_p[1]);
1338:
1339: if (rexec_flag) {
1340: send_rexec_state(config_s[0], &cfg);
1341: close(config_s[0]);
1342: close(config_s[1]);
1343: }
1344:
1345: /*
1346: * Mark that the key has been used (it
1347: * was "given" to the child).
1348: */
1349: if ((options.protocol & SSH_PROTO_1) &&
1350: key_used == 0) {
1351: /* Schedule server key regeneration alarm. */
1352: signal(SIGALRM, key_regeneration_alarm);
1353: alarm(options.key_regeneration_time);
1354: key_used = 1;
1355: }
1356:
1357: close(*newsock);
1358: }
1359:
1360: /* child process check (or debug mode) */
1361: if (num_listen_socks < 0)
1362: break;
1363: }
1364: }
1365:
1366:
1.65 deraadt 1367: /*
1368: * Main program for the daemon.
1369: */
1.2 provos 1370: int
1371: main(int ac, char **av)
1.1 deraadt 1372: {
1.64 markus 1373: extern char *optarg;
1374: extern int optind;
1.432 djm 1375: int r, opt, i, j, on = 1;
1.297 avsm 1376: int sock_in = -1, sock_out = -1, newsock = -1;
1.64 markus 1377: const char *remote_ip;
1378: int remote_port;
1.399 dtucker 1379: char *line, *logfile = NULL;
1.345 djm 1380: int config_s[2] = { -1 , -1 };
1.396 djm 1381: u_int n;
1.364 markus 1382: u_int64_t ibytes, obytes;
1.362 dtucker 1383: mode_t new_umask;
1.278 markus 1384: Key *key;
1.404 markus 1385: Key *pubkey;
1386: int keytype;
1.230 provos 1387: Authctxt *authctxt;
1.391 dtucker 1388: struct connection_info *connection_info = get_connection_info(0, 0);
1.64 markus 1389:
1.138 markus 1390: /* Save argv. */
1.64 markus 1391: saved_argv = av;
1.294 djm 1392: rexec_argc = ac;
1.313 djm 1393:
1394: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1395: sanitise_stdfd();
1.64 markus 1396:
1397: /* Initialize configuration options to their default values. */
1398: initialize_server_options(&options);
1399:
1400: /* Parse command-line arguments. */
1.399 dtucker 1401: while ((opt = getopt(ac, av, "f:p:b:k:h:g:u:o:C:dDeE:iqrtQRT46")) != -1) {
1.64 markus 1402: switch (opt) {
1.75 markus 1403: case '4':
1.305 djm 1404: options.address_family = AF_INET;
1.75 markus 1405: break;
1406: case '6':
1.305 djm 1407: options.address_family = AF_INET6;
1.75 markus 1408: break;
1.64 markus 1409: case 'f':
1410: config_file_name = optarg;
1411: break;
1.373 djm 1412: case 'c':
1413: if (options.num_host_cert_files >= MAX_HOSTCERTS) {
1414: fprintf(stderr, "too many host certificates.\n");
1415: exit(1);
1416: }
1417: options.host_cert_files[options.num_host_cert_files++] =
1418: derelativise_path(optarg);
1419: break;
1.64 markus 1420: case 'd':
1.273 markus 1421: if (debug_flag == 0) {
1.127 markus 1422: debug_flag = 1;
1423: options.log_level = SYSLOG_LEVEL_DEBUG1;
1.273 markus 1424: } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
1.127 markus 1425: options.log_level++;
1.64 markus 1426: break;
1.135 markus 1427: case 'D':
1428: no_daemon_flag = 1;
1.192 lebel 1429: break;
1.399 dtucker 1430: case 'E':
1431: logfile = xstrdup(optarg);
1432: /* FALLTHROUGH */
1.192 lebel 1433: case 'e':
1434: log_stderr = 1;
1.135 markus 1435: break;
1.64 markus 1436: case 'i':
1437: inetd_flag = 1;
1438: break;
1.294 djm 1439: case 'r':
1440: rexec_flag = 0;
1441: break;
1442: case 'R':
1443: rexeced_flag = 1;
1444: inetd_flag = 1;
1445: break;
1.64 markus 1446: case 'Q':
1.158 markus 1447: /* ignored */
1.64 markus 1448: break;
1449: case 'q':
1450: options.log_level = SYSLOG_LEVEL_QUIET;
1451: break;
1452: case 'b':
1.327 deraadt 1453: options.server_key_bits = (int)strtonum(optarg, 256,
1454: 32768, NULL);
1.64 markus 1455: break;
1456: case 'p':
1.75 markus 1457: options.ports_from_cmdline = 1;
1.127 markus 1458: if (options.num_ports >= MAX_PORTS) {
1459: fprintf(stderr, "too many ports.\n");
1460: exit(1);
1461: }
1.193 stevesk 1462: options.ports[options.num_ports++] = a2port(optarg);
1.366 djm 1463: if (options.ports[options.num_ports-1] <= 0) {
1.193 stevesk 1464: fprintf(stderr, "Bad port number.\n");
1465: exit(1);
1466: }
1.64 markus 1467: break;
1468: case 'g':
1.197 stevesk 1469: if ((options.login_grace_time = convtime(optarg)) == -1) {
1470: fprintf(stderr, "Invalid login grace time.\n");
1471: exit(1);
1472: }
1.64 markus 1473: break;
1474: case 'k':
1.197 stevesk 1475: if ((options.key_regeneration_time = convtime(optarg)) == -1) {
1476: fprintf(stderr, "Invalid key regeneration interval.\n");
1477: exit(1);
1478: }
1.64 markus 1479: break;
1480: case 'h':
1.134 markus 1481: if (options.num_host_key_files >= MAX_HOSTKEYS) {
1482: fprintf(stderr, "too many host keys.\n");
1483: exit(1);
1484: }
1.371 djm 1485: options.host_key_files[options.num_host_key_files++] =
1486: derelativise_path(optarg);
1.64 markus 1487: break;
1.203 stevesk 1488: case 't':
1489: test_flag = 1;
1490: break;
1.358 dtucker 1491: case 'T':
1492: test_flag = 2;
1493: break;
1494: case 'C':
1.391 dtucker 1495: if (parse_server_match_testspec(connection_info,
1496: optarg) == -1)
1497: exit(1);
1.358 dtucker 1498: break;
1.125 markus 1499: case 'u':
1.438 deraadt 1500: utmp_len = (u_int)strtonum(optarg, 0, HOST_NAME_MAX+1+1, NULL);
1501: if (utmp_len > HOST_NAME_MAX+1) {
1.257 stevesk 1502: fprintf(stderr, "Invalid utmp length.\n");
1503: exit(1);
1504: }
1.125 markus 1505: break;
1.215 markus 1506: case 'o':
1.283 markus 1507: line = xstrdup(optarg);
1508: if (process_server_config_line(&options, line,
1.391 dtucker 1509: "command-line", 0, NULL, NULL) != 0)
1.217 deraadt 1510: exit(1);
1.402 djm 1511: free(line);
1.215 markus 1512: break;
1.64 markus 1513: case '?':
1514: default:
1.215 markus 1515: usage();
1516: break;
1.64 markus 1517: }
1518: }
1.294 djm 1519: if (rexeced_flag || inetd_flag)
1520: rexec_flag = 0;
1.355 mbalmer 1521: if (!test_flag && (rexec_flag && (av[0] == NULL || *av[0] != '/')))
1.294 djm 1522: fatal("sshd re-exec requires execution with an absolute path");
1523: if (rexeced_flag)
1.296 djm 1524: closefrom(REEXEC_MIN_FREE_FD);
1525: else
1526: closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
1.294 djm 1527:
1.426 markus 1528: #ifdef WITH_OPENSSL
1.379 djm 1529: OpenSSL_add_all_algorithms();
1.426 markus 1530: #endif
1.64 markus 1531:
1.399 dtucker 1532: /* If requested, redirect the logs to the specified logfile. */
1533: if (logfile != NULL) {
1534: log_redirect_stderr_to(logfile);
1.402 djm 1535: free(logfile);
1.399 dtucker 1536: }
1.75 markus 1537: /*
1538: * Force logging to stderr until we have loaded the private host
1539: * key (unless started from inetd)
1540: */
1.138 markus 1541: log_init(__progname,
1.224 markus 1542: options.log_level == SYSLOG_LEVEL_NOT_SET ?
1543: SYSLOG_LEVEL_INFO : options.log_level,
1544: options.log_facility == SYSLOG_FACILITY_NOT_SET ?
1545: SYSLOG_FACILITY_AUTH : options.log_facility,
1.261 markus 1546: log_stderr || !inetd_flag);
1.75 markus 1547:
1.294 djm 1548: sensitive_data.server_key = NULL;
1549: sensitive_data.ssh1_host_key = NULL;
1550: sensitive_data.have_ssh1_key = 0;
1551: sensitive_data.have_ssh2_key = 0;
1552:
1.358 dtucker 1553: /*
1554: * If we're doing an extended config test, make sure we have all of
1555: * the parameters we need. If we're not doing an extended test,
1556: * do not silently ignore connection test params.
1557: */
1.391 dtucker 1558: if (test_flag >= 2 && server_match_spec_complete(connection_info) == 0)
1.358 dtucker 1559: fatal("user, host and addr are all required when testing "
1560: "Match configs");
1.391 dtucker 1561: if (test_flag < 2 && server_match_spec_complete(connection_info) >= 0)
1.358 dtucker 1562: fatal("Config test connection parameter (-C) provided without "
1563: "test mode (-T)");
1564:
1.294 djm 1565: /* Fetch our configuration */
1566: buffer_init(&cfg);
1567: if (rexeced_flag)
1.296 djm 1568: recv_rexec_state(REEXEC_CONFIG_PASS_FD, &cfg);
1.294 djm 1569: else
1570: load_server_config(config_file_name, &cfg);
1571:
1.337 dtucker 1572: parse_server_config(&options, rexeced_flag ? "rexec" : config_file_name,
1.391 dtucker 1573: &cfg, NULL);
1.64 markus 1574:
1575: /* Fill in default values for those options not explicitly set. */
1576: fill_default_server_options(&options);
1.350 dtucker 1577:
1578: /* challenge-response is implemented via keyboard interactive */
1579: if (options.challenge_response_authentication)
1580: options.kbd_interactive_authentication = 1;
1.395 djm 1581:
1582: /* Check that options are sensible */
1583: if (options.authorized_keys_command_user == NULL &&
1584: (options.authorized_keys_command != NULL &&
1585: strcasecmp(options.authorized_keys_command, "none") != 0))
1586: fatal("AuthorizedKeysCommand set without "
1587: "AuthorizedKeysCommandUser");
1.396 djm 1588:
1589: /*
1590: * Check whether there is any path through configured auth methods.
1591: * Unfortunately it is not possible to verify this generally before
1592: * daemonisation in the presence of Match block, but this catches
1593: * and warns for trivial misconfigurations that could break login.
1594: */
1595: if (options.num_auth_methods != 0) {
1596: if ((options.protocol & SSH_PROTO_1))
1597: fatal("AuthenticationMethods is not supported with "
1598: "SSH protocol 1");
1599: for (n = 0; n < options.num_auth_methods; n++) {
1600: if (auth2_methods_valid(options.auth_methods[n],
1601: 1) == 0)
1602: break;
1603: }
1604: if (n >= options.num_auth_methods)
1605: fatal("AuthenticationMethods cannot be satisfied by "
1606: "enabled authentication methods");
1607: }
1.305 djm 1608:
1.370 dtucker 1609: /* set default channel AF */
1.305 djm 1610: channel_set_af(options.address_family);
1.64 markus 1611:
1612: /* Check that there are no remaining arguments. */
1613: if (optind < ac) {
1614: fprintf(stderr, "Extra argument %s.\n", av[optind]);
1615: exit(1);
1616: }
1617:
1.397 dtucker 1618: debug("sshd version %s, %s", SSH_VERSION,
1.426 markus 1619: #ifdef WITH_OPENSSL
1620: SSLeay_version(SSLEAY_VERSION)
1621: #else
1622: "without OpenSSL"
1623: #endif
1624: );
1.64 markus 1625:
1.404 markus 1626: /* load host keys */
1.329 djm 1627: sensitive_data.host_keys = xcalloc(options.num_host_key_files,
1.255 deraadt 1628: sizeof(Key *));
1.404 markus 1629: sensitive_data.host_pubkeys = xcalloc(options.num_host_key_files,
1630: sizeof(Key *));
1631:
1632: if (options.host_key_agent) {
1633: if (strcmp(options.host_key_agent, SSH_AUTHSOCKET_ENV_NAME))
1634: setenv(SSH_AUTHSOCKET_ENV_NAME,
1635: options.host_key_agent, 1);
1.433 djm 1636: if ((r = ssh_get_authentication_socket(NULL)) == 0)
1637: have_agent = 1;
1638: else
1639: error("Could not connect to agent \"%s\": %s",
1640: options.host_key_agent, ssh_err(r));
1.404 markus 1641: }
1.134 markus 1642:
1.217 deraadt 1643: for (i = 0; i < options.num_host_key_files; i++) {
1.430 djm 1644: if (options.host_key_files[i] == NULL)
1645: continue;
1.179 markus 1646: key = key_load_private(options.host_key_files[i], "", NULL);
1.404 markus 1647: pubkey = key_load_public(options.host_key_files[i], NULL);
1.439 djm 1648: if (pubkey == NULL && key != NULL)
1649: pubkey = key_demote(key);
1.179 markus 1650: sensitive_data.host_keys[i] = key;
1.404 markus 1651: sensitive_data.host_pubkeys[i] = pubkey;
1652:
1653: if (key == NULL && pubkey != NULL && pubkey->type != KEY_RSA1 &&
1654: have_agent) {
1655: debug("will rely on agent for hostkey %s",
1656: options.host_key_files[i]);
1657: keytype = pubkey->type;
1658: } else if (key != NULL) {
1659: keytype = key->type;
1660: } else {
1.195 markus 1661: error("Could not load host key: %s",
1662: options.host_key_files[i]);
1.179 markus 1663: sensitive_data.host_keys[i] = NULL;
1.404 markus 1664: sensitive_data.host_pubkeys[i] = NULL;
1.134 markus 1665: continue;
1666: }
1.404 markus 1667:
1668: switch (keytype) {
1.134 markus 1669: case KEY_RSA1:
1670: sensitive_data.ssh1_host_key = key;
1671: sensitive_data.have_ssh1_key = 1;
1672: break;
1673: case KEY_RSA:
1674: case KEY_DSA:
1.378 djm 1675: case KEY_ECDSA:
1.412 markus 1676: case KEY_ED25519:
1.134 markus 1677: sensitive_data.have_ssh2_key = 1;
1678: break;
1679: }
1.404 markus 1680: debug("private host key: #%d type %d %s", i, keytype,
1681: key_type(key ? key : pubkey));
1.134 markus 1682: }
1683: if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
1.264 itojun 1684: logit("Disabling protocol version 1. Could not load host key");
1.108 markus 1685: options.protocol &= ~SSH_PROTO_1;
1686: }
1.134 markus 1687: if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
1.264 itojun 1688: logit("Disabling protocol version 2. Could not load host key");
1.134 markus 1689: options.protocol &= ~SSH_PROTO_2;
1.108 markus 1690: }
1.162 stevesk 1691: if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
1.264 itojun 1692: logit("sshd: no hostkeys available -- exiting.");
1.64 markus 1693: exit(1);
1694: }
1695:
1.373 djm 1696: /*
1697: * Load certificates. They are stored in an array at identical
1698: * indices to the public keys that they relate to.
1699: */
1700: sensitive_data.host_certificates = xcalloc(options.num_host_key_files,
1701: sizeof(Key *));
1702: for (i = 0; i < options.num_host_key_files; i++)
1703: sensitive_data.host_certificates[i] = NULL;
1704:
1705: for (i = 0; i < options.num_host_cert_files; i++) {
1.430 djm 1706: if (options.host_cert_files[i] == NULL)
1707: continue;
1.373 djm 1708: key = key_load_public(options.host_cert_files[i], NULL);
1709: if (key == NULL) {
1710: error("Could not load host certificate: %s",
1711: options.host_cert_files[i]);
1712: continue;
1713: }
1714: if (!key_is_cert(key)) {
1715: error("Certificate file is not a certificate: %s",
1716: options.host_cert_files[i]);
1717: key_free(key);
1718: continue;
1719: }
1720: /* Find matching private key */
1721: for (j = 0; j < options.num_host_key_files; j++) {
1722: if (key_equal_public(key,
1723: sensitive_data.host_keys[j])) {
1724: sensitive_data.host_certificates[j] = key;
1725: break;
1726: }
1727: }
1728: if (j >= options.num_host_key_files) {
1729: error("No matching private key for certificate: %s",
1730: options.host_cert_files[i]);
1731: key_free(key);
1732: continue;
1733: }
1734: sensitive_data.host_certificates[j] = key;
1735: debug("host certificate: #%d type %d %s", j, key->type,
1736: key_type(key));
1737: }
1.426 markus 1738:
1739: #ifdef WITH_SSH1
1.108 markus 1740: /* Check certain values for sanity. */
1741: if (options.protocol & SSH_PROTO_1) {
1742: if (options.server_key_bits < 512 ||
1743: options.server_key_bits > 32768) {
1744: fprintf(stderr, "Bad server key size.\n");
1745: exit(1);
1746: }
1747: /*
1748: * Check that server and host key lengths differ sufficiently. This
1749: * is necessary to make double encryption work with rsaref. Oh, I
1750: * hate software patents. I dont know if this can go? Niels
1751: */
1752: if (options.server_key_bits >
1.250 deraadt 1753: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) -
1754: SSH_KEY_BITS_RESERVED && options.server_key_bits <
1755: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1756: SSH_KEY_BITS_RESERVED) {
1.108 markus 1757: options.server_key_bits =
1.250 deraadt 1758: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1759: SSH_KEY_BITS_RESERVED;
1.108 markus 1760: debug("Forcing server key to %d bits to make it differ from host key.",
1761: options.server_key_bits);
1762: }
1.244 markus 1763: }
1.426 markus 1764: #endif
1.244 markus 1765:
1766: if (use_privsep) {
1767: struct stat st;
1768:
1.327 deraadt 1769: if (getpwnam(SSH_PRIVSEP_USER) == NULL)
1.244 markus 1770: fatal("Privilege separation user %s does not exist",
1771: SSH_PRIVSEP_USER);
1772: if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
1773: (S_ISDIR(st.st_mode) == 0))
1774: fatal("Missing privilege separation directory: %s",
1.247 stevesk 1775: _PATH_PRIVSEP_CHROOT_DIR);
1776: if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
1.262 markus 1777: fatal("%s must be owned by root and not group or "
1778: "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
1.358 dtucker 1779: }
1780:
1781: if (test_flag > 1) {
1.391 dtucker 1782: if (server_match_spec_complete(connection_info) == 1)
1783: parse_server_match_config(&options, connection_info);
1.358 dtucker 1784: dump_config(&options);
1.108 markus 1785: }
1.203 stevesk 1786:
1787: /* Configuration looks good, so exit if in test mode. */
1788: if (test_flag)
1789: exit(0);
1.108 markus 1790:
1.294 djm 1791: if (rexec_flag) {
1.329 djm 1792: rexec_argv = xcalloc(rexec_argc + 2, sizeof(char *));
1.294 djm 1793: for (i = 0; i < rexec_argc; i++) {
1794: debug("rexec_argv[%d]='%s'", i, saved_argv[i]);
1795: rexec_argv[i] = saved_argv[i];
1796: }
1797: rexec_argv[rexec_argc] = "-R";
1798: rexec_argv[rexec_argc + 1] = NULL;
1799: }
1.362 dtucker 1800:
1801: /* Ensure that umask disallows at least group and world write */
1802: new_umask = umask(0077) | 0022;
1803: (void) umask(new_umask);
1.294 djm 1804:
1.108 markus 1805: /* Initialize the log (it is reinitialized below in case we forked). */
1.306 dtucker 1806: if (debug_flag && (!inetd_flag || rexeced_flag))
1.64 markus 1807: log_stderr = 1;
1.138 markus 1808: log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64 markus 1809:
1.108 markus 1810: /*
1811: * If not in debugging mode, and not started from inetd, disconnect
1812: * from the controlling terminal, and fork. The original process
1813: * exits.
1814: */
1.135 markus 1815: if (!(debug_flag || inetd_flag || no_daemon_flag)) {
1.64 markus 1816: int fd;
1.345 djm 1817:
1.64 markus 1818: if (daemon(0, 0) < 0)
1819: fatal("daemon() failed: %.200s", strerror(errno));
1820:
1821: /* Disconnect from the controlling tty. */
1.165 itojun 1822: fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
1.64 markus 1823: if (fd >= 0) {
1824: (void) ioctl(fd, TIOCNOTTY, NULL);
1825: close(fd);
1826: }
1827: }
1828: /* Reinitialize the log (because of the fork above). */
1.138 markus 1829: log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64 markus 1830:
1831: /* Chdir to the root directory so that the current disk can be
1832: unmounted if desired. */
1.401 dtucker 1833: if (chdir("/") == -1)
1834: error("chdir(\"/\"): %s", strerror(errno));
1.217 deraadt 1835:
1.178 markus 1836: /* ignore SIGPIPE */
1837: signal(SIGPIPE, SIG_IGN);
1.64 markus 1838:
1.345 djm 1839: /* Get a connection, either from inetd or a listening TCP socket */
1.64 markus 1840: if (inetd_flag) {
1.345 djm 1841: server_accept_inetd(&sock_in, &sock_out);
1.64 markus 1842: } else {
1.345 djm 1843: server_listen();
1.75 markus 1844:
1.201 markus 1845: if (options.protocol & SSH_PROTO_1)
1846: generate_ephemeral_server_key();
1847:
1848: signal(SIGHUP, sighup_handler);
1.345 djm 1849: signal(SIGCHLD, main_sigchld_handler);
1.201 markus 1850: signal(SIGTERM, sigterm_handler);
1851: signal(SIGQUIT, sigterm_handler);
1852:
1.345 djm 1853: /*
1854: * Write out the pid file after the sigterm handler
1855: * is setup and the listen sockets are bound
1856: */
1.430 djm 1857: if (options.pid_file != NULL && !debug_flag) {
1.345 djm 1858: FILE *f = fopen(options.pid_file, "w");
1.201 markus 1859:
1.270 djm 1860: if (f == NULL) {
1861: error("Couldn't create pid file \"%s\": %s",
1862: options.pid_file, strerror(errno));
1863: } else {
1.245 mpech 1864: fprintf(f, "%ld\n", (long) getpid());
1.64 markus 1865: fclose(f);
1866: }
1867: }
1868:
1.345 djm 1869: /* Accept a connection and return in a forked child */
1870: server_accept_loop(&sock_in, &sock_out,
1871: &newsock, config_s);
1.1 deraadt 1872: }
1873:
1.64 markus 1874: /* This is the child processing a new connection. */
1.288 markus 1875: setproctitle("%s", "[accepted]");
1.294 djm 1876:
1.300 markus 1877: /*
1878: * Create a new session and process group since the 4.4BSD
1879: * setlogin() affects the entire process group. We don't
1880: * want the child to be able to affect the parent.
1881: */
1882: if (!debug_flag && !inetd_flag && setsid() < 0)
1883: error("setsid: %.100s", strerror(errno));
1884:
1.294 djm 1885: if (rexec_flag) {
1886: int fd;
1887:
1.296 djm 1888: debug("rexec start in %d out %d newsock %d pipe %d sock %d",
1889: sock_in, sock_out, newsock, startup_pipe, config_s[0]);
1.294 djm 1890: dup2(newsock, STDIN_FILENO);
1891: dup2(STDIN_FILENO, STDOUT_FILENO);
1892: if (startup_pipe == -1)
1.296 djm 1893: close(REEXEC_STARTUP_PIPE_FD);
1.407 djm 1894: else if (startup_pipe != REEXEC_STARTUP_PIPE_FD) {
1.296 djm 1895: dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD);
1.407 djm 1896: close(startup_pipe);
1897: startup_pipe = REEXEC_STARTUP_PIPE_FD;
1898: }
1.294 djm 1899:
1.296 djm 1900: dup2(config_s[1], REEXEC_CONFIG_PASS_FD);
1.294 djm 1901: close(config_s[1]);
1.296 djm 1902:
1.294 djm 1903: execv(rexec_argv[0], rexec_argv);
1904:
1905: /* Reexec has failed, fall back and continue */
1906: error("rexec of %s failed: %s", rexec_argv[0], strerror(errno));
1.296 djm 1907: recv_rexec_state(REEXEC_CONFIG_PASS_FD, NULL);
1.294 djm 1908: log_init(__progname, options.log_level,
1909: options.log_facility, log_stderr);
1910:
1911: /* Clean up fds */
1.296 djm 1912: close(REEXEC_CONFIG_PASS_FD);
1913: newsock = sock_out = sock_in = dup(STDIN_FILENO);
1.294 djm 1914: if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1915: dup2(fd, STDIN_FILENO);
1916: dup2(fd, STDOUT_FILENO);
1917: if (fd > STDERR_FILENO)
1918: close(fd);
1919: }
1.296 djm 1920: debug("rexec cleanup in %d out %d newsock %d pipe %d sock %d",
1921: sock_in, sock_out, newsock, startup_pipe, config_s[0]);
1.294 djm 1922: }
1.372 djm 1923:
1924: /* Executed child processes don't need these. */
1925: fcntl(sock_out, F_SETFD, FD_CLOEXEC);
1926: fcntl(sock_in, F_SETFD, FD_CLOEXEC);
1.64 markus 1927:
1.66 markus 1928: /*
1929: * Disable the key regeneration alarm. We will not regenerate the
1930: * key since we are no longer in a position to give it to anyone. We
1931: * will not restart on SIGHUP since it no longer makes sense.
1932: */
1.64 markus 1933: alarm(0);
1934: signal(SIGALRM, SIG_DFL);
1935: signal(SIGHUP, SIG_DFL);
1936: signal(SIGTERM, SIG_DFL);
1937: signal(SIGQUIT, SIG_DFL);
1938: signal(SIGCHLD, SIG_DFL);
1.150 markus 1939:
1.66 markus 1940: /*
1941: * Register our connection. This turns encryption off because we do
1942: * not have a key.
1943: */
1.64 markus 1944: packet_set_connection(sock_in, sock_out);
1.312 markus 1945: packet_set_server();
1.309 djm 1946:
1947: /* Set SO_KEEPALIVE if requested. */
1948: if (options.tcp_keep_alive && packet_connection_is_on_socket() &&
1949: setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0)
1950: error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1.1 deraadt 1951:
1.310 markus 1952: if ((remote_port = get_remote_port()) < 0) {
1953: debug("get_remote_port failed");
1954: cleanup_exit(255);
1955: }
1.316 dtucker 1956:
1957: /*
1958: * We use get_canonical_hostname with usedns = 0 instead of
1959: * get_remote_ipaddr here so IP options will be checked.
1960: */
1.331 markus 1961: (void) get_canonical_hostname(0);
1962: /*
1963: * The rest of the code depends on the fact that
1964: * get_remote_ipaddr() caches the remote ip, even if
1965: * the socket goes away.
1966: */
1967: remote_ip = get_remote_ipaddr();
1.209 markus 1968:
1.64 markus 1969: /* Log the connection. */
1.409 djm 1970: verbose("Connection from %s port %d on %s port %d",
1971: remote_ip, remote_port,
1972: get_local_ipaddr(sock_in), get_local_port());
1.1 deraadt 1973:
1.66 markus 1974: /*
1.317 djm 1975: * We don't want to listen forever unless the other side
1.66 markus 1976: * successfully authenticates itself. So we set up an alarm which is
1977: * cleared after successful authentication. A limit of zero
1.317 djm 1978: * indicates no limit. Note that we don't set the alarm in debugging
1.66 markus 1979: * mode; it is just annoying to have the server exit just when you
1980: * are about to discover the bug.
1981: */
1.64 markus 1982: signal(SIGALRM, grace_alarm_handler);
1983: if (!debug_flag)
1984: alarm(options.login_grace_time);
1985:
1.96 markus 1986: sshd_exchange_identification(sock_in, sock_out);
1.353 dtucker 1987:
1988: /* In inetd mode, generate ephemeral key only for proto 1 connections */
1989: if (!compat20 && inetd_flag && sensitive_data.server_key == NULL)
1990: generate_ephemeral_server_key();
1.275 markus 1991:
1.64 markus 1992: packet_set_nonblocking();
1.1 deraadt 1993:
1.278 markus 1994: /* allocate authentication context */
1.329 djm 1995: authctxt = xcalloc(1, sizeof(*authctxt));
1.278 markus 1996:
1997: /* XXX global for cleanup, access from other modules */
1998: the_authctxt = authctxt;
1999:
1.307 otto 2000: /* prepare buffer to collect messages to display to user after login */
2001: buffer_init(&loginmsg);
1.374 dtucker 2002: auth_debug_reset();
1.307 otto 2003:
1.404 markus 2004: if (use_privsep) {
1.278 markus 2005: if (privsep_preauth(authctxt) == 1)
1.237 markus 2006: goto authenticated;
1.432 djm 2007: } else if (compat20 && have_agent) {
2008: if ((r = ssh_get_authentication_socket(&auth_sock)) != 0) {
2009: error("Unable to get agent socket: %s", ssh_err(r));
1.433 djm 2010: have_agent = 0;
1.432 djm 2011: }
2012: }
1.231 provos 2013:
1.77 markus 2014: /* perform the key exchange */
2015: /* authenticate user and start session */
1.98 markus 2016: if (compat20) {
2017: do_ssh2_kex();
1.278 markus 2018: do_authentication2(authctxt);
1.98 markus 2019: } else {
1.426 markus 2020: #ifdef WITH_SSH1
1.98 markus 2021: do_ssh1_kex();
1.278 markus 2022: do_authentication(authctxt);
1.426 markus 2023: #else
2024: fatal("ssh1 not supported");
2025: #endif
1.98 markus 2026: }
1.237 markus 2027: /*
2028: * If we use privilege separation, the unprivileged child transfers
2029: * the current keystate and exits
2030: */
2031: if (use_privsep) {
1.242 mouring 2032: mm_send_keystate(pmonitor);
1.231 provos 2033: exit(0);
1.237 markus 2034: }
1.231 provos 2035:
2036: authenticated:
1.318 djm 2037: /*
2038: * Cancel the alarm we set to limit the time taken for
2039: * authentication.
2040: */
2041: alarm(0);
2042: signal(SIGALRM, SIG_DFL);
1.347 markus 2043: authctxt->authenticated = 1;
1.318 djm 2044: if (startup_pipe != -1) {
2045: close(startup_pipe);
2046: startup_pipe = -1;
2047: }
2048:
1.234 markus 2049: /*
1.231 provos 2050: * In privilege separation, we fork another child and prepare
2051: * file descriptor passing.
2052: */
2053: if (use_privsep) {
1.237 markus 2054: privsep_postauth(authctxt);
2055: /* the monitor process [priv] will not return */
1.231 provos 2056: if (!compat20)
2057: destroy_sensitive_data();
2058: }
1.360 dtucker 2059:
2060: packet_set_timeout(options.client_alive_interval,
2061: options.client_alive_count_max);
1.439 djm 2062:
2063: /* Try to send all our hostkeys to the client */
2064: if (compat20)
2065: notify_hostkeys(active_state);
1.230 provos 2066:
1.278 markus 2067: /* Start session. */
1.230 provos 2068: do_authenticated(authctxt);
2069:
1.64 markus 2070: /* The connection has been terminated. */
1.434 markus 2071: packet_get_bytes(&ibytes, &obytes);
1.381 djm 2072: verbose("Transferred: sent %llu, received %llu bytes",
2073: (unsigned long long)obytes, (unsigned long long)ibytes);
1.364 markus 2074:
2075: verbose("Closing connection to %.500s port %d", remote_ip, remote_port);
1.64 markus 2076: packet_close();
1.231 provos 2077:
2078: if (use_privsep)
2079: mm_terminate();
2080:
1.64 markus 2081: exit(0);
1.1 deraadt 2082: }
2083:
1.426 markus 2084: #ifdef WITH_SSH1
1.65 deraadt 2085: /*
1.229 markus 2086: * Decrypt session_key_int using our private server key and private host key
2087: * (key with larger modulus first).
2088: */
1.231 provos 2089: int
1.229 markus 2090: ssh1_session_key(BIGNUM *session_key_int)
2091: {
2092: int rsafail = 0;
2093:
1.327 deraadt 2094: if (BN_cmp(sensitive_data.server_key->rsa->n,
2095: sensitive_data.ssh1_host_key->rsa->n) > 0) {
1.229 markus 2096: /* Server key has bigger modulus. */
2097: if (BN_num_bits(sensitive_data.server_key->rsa->n) <
1.327 deraadt 2098: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
2099: SSH_KEY_BITS_RESERVED) {
2100: fatal("do_connection: %s: "
2101: "server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
1.229 markus 2102: get_remote_ipaddr(),
2103: BN_num_bits(sensitive_data.server_key->rsa->n),
2104: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
2105: SSH_KEY_BITS_RESERVED);
2106: }
2107: if (rsa_private_decrypt(session_key_int, session_key_int,
1.427 djm 2108: sensitive_data.server_key->rsa) != 0)
1.229 markus 2109: rsafail++;
2110: if (rsa_private_decrypt(session_key_int, session_key_int,
1.427 djm 2111: sensitive_data.ssh1_host_key->rsa) != 0)
1.229 markus 2112: rsafail++;
2113: } else {
2114: /* Host key has bigger modulus (or they are equal). */
2115: if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
1.327 deraadt 2116: BN_num_bits(sensitive_data.server_key->rsa->n) +
2117: SSH_KEY_BITS_RESERVED) {
2118: fatal("do_connection: %s: "
2119: "host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
1.229 markus 2120: get_remote_ipaddr(),
2121: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
2122: BN_num_bits(sensitive_data.server_key->rsa->n),
2123: SSH_KEY_BITS_RESERVED);
2124: }
2125: if (rsa_private_decrypt(session_key_int, session_key_int,
1.427 djm 2126: sensitive_data.ssh1_host_key->rsa) != 0)
1.229 markus 2127: rsafail++;
2128: if (rsa_private_decrypt(session_key_int, session_key_int,
1.427 djm 2129: sensitive_data.server_key->rsa) != 0)
1.229 markus 2130: rsafail++;
2131: }
2132: return (rsafail);
2133: }
1.426 markus 2134:
1.229 markus 2135: /*
1.77 markus 2136: * SSH1 key exchange
1.65 deraadt 2137: */
1.200 itojun 2138: static void
1.142 markus 2139: do_ssh1_kex(void)
1.1 deraadt 2140: {
1.64 markus 2141: int i, len;
1.159 markus 2142: int rsafail = 0;
1.431 tedu 2143: BIGNUM *session_key_int, *fake_key_int, *real_key_int;
1.140 markus 2144: u_char session_key[SSH_SESSION_KEY_LENGTH];
1.431 tedu 2145: u_char fake_key_bytes[4096 / 8];
2146: size_t fake_key_len;
1.140 markus 2147: u_char cookie[8];
2148: u_int cipher_type, auth_mask, protocol_flags;
1.64 markus 2149:
1.66 markus 2150: /*
2151: * Generate check bytes that the client must send back in the user
2152: * packet in order for it to be accepted; this is used to defy ip
2153: * spoofing attacks. Note that this only works against somebody
2154: * doing IP spoofing from a remote machine; any machine on the local
2155: * network can still see outgoing packets and catch the random
2156: * cookie. This only affects rhosts authentication, and this is one
2157: * of the reasons why it is inherently insecure.
2158: */
1.356 djm 2159: arc4random_buf(cookie, sizeof(cookie));
1.64 markus 2160:
1.66 markus 2161: /*
2162: * Send our public key. We include in the packet 64 bits of random
2163: * data that must be matched in the reply in order to prevent IP
2164: * spoofing.
2165: */
1.64 markus 2166: packet_start(SSH_SMSG_PUBLIC_KEY);
2167: for (i = 0; i < 8; i++)
1.77 markus 2168: packet_put_char(cookie[i]);
1.64 markus 2169:
2170: /* Store our public server RSA key. */
1.134 markus 2171: packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
2172: packet_put_bignum(sensitive_data.server_key->rsa->e);
2173: packet_put_bignum(sensitive_data.server_key->rsa->n);
1.64 markus 2174:
2175: /* Store our public host RSA key. */
1.134 markus 2176: packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
2177: packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
2178: packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
1.64 markus 2179:
2180: /* Put protocol flags. */
2181: packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
2182:
2183: /* Declare which ciphers we support. */
1.131 markus 2184: packet_put_int(cipher_mask_ssh1(0));
1.64 markus 2185:
2186: /* Declare supported authentication types. */
2187: auth_mask = 0;
2188: if (options.rhosts_rsa_authentication)
2189: auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
2190: if (options.rsa_authentication)
2191: auth_mask |= 1 << SSH_AUTH_RSA;
1.196 markus 2192: if (options.challenge_response_authentication == 1)
1.64 markus 2193: auth_mask |= 1 << SSH_AUTH_TIS;
2194: if (options.password_authentication)
2195: auth_mask |= 1 << SSH_AUTH_PASSWORD;
2196: packet_put_int(auth_mask);
2197:
2198: /* Send the packet and wait for it to be sent. */
2199: packet_send();
2200: packet_write_wait();
2201:
1.134 markus 2202: debug("Sent %d bit server key and %d bit host key.",
2203: BN_num_bits(sensitive_data.server_key->rsa->n),
2204: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1.64 markus 2205:
2206: /* Read clients reply (cipher type and session key). */
1.222 markus 2207: packet_read_expect(SSH_CMSG_SESSION_KEY);
1.64 markus 2208:
1.69 markus 2209: /* Get cipher type and check whether we accept this. */
1.64 markus 2210: cipher_type = packet_get_char();
1.69 markus 2211:
1.131 markus 2212: if (!(cipher_mask_ssh1(0) & (1 << cipher_type)))
1.69 markus 2213: packet_disconnect("Warning: client selects unsupported cipher.");
1.64 markus 2214:
2215: /* Get check bytes from the packet. These must match those we
2216: sent earlier with the public key packet. */
2217: for (i = 0; i < 8; i++)
1.77 markus 2218: if (cookie[i] != packet_get_char())
1.64 markus 2219: packet_disconnect("IP Spoofing check bytes do not match.");
2220:
2221: debug("Encryption type: %.200s", cipher_name(cipher_type));
2222:
2223: /* Get the encrypted integer. */
1.431 tedu 2224: if ((real_key_int = BN_new()) == NULL)
1.218 markus 2225: fatal("do_ssh1_kex: BN_new failed");
1.431 tedu 2226: packet_get_bignum(real_key_int);
1.64 markus 2227:
2228: protocol_flags = packet_get_int();
2229: packet_set_protocol_flags(protocol_flags);
1.220 markus 2230: packet_check_eom();
1.64 markus 2231:
1.431 tedu 2232: /* Setup a fake key in case RSA decryption fails */
2233: if ((fake_key_int = BN_new()) == NULL)
2234: fatal("do_ssh1_kex: BN_new failed");
2235: fake_key_len = BN_num_bytes(real_key_int);
2236: if (fake_key_len > sizeof(fake_key_bytes))
2237: fake_key_len = sizeof(fake_key_bytes);
2238: arc4random_buf(fake_key_bytes, fake_key_len);
2239: if (BN_bin2bn(fake_key_bytes, fake_key_len, fake_key_int) == NULL)
2240: fatal("do_ssh1_kex: BN_bin2bn failed");
2241:
2242: /* Decrypt real_key_int using host/server keys */
2243: rsafail = PRIVSEP(ssh1_session_key(real_key_int));
2244: /* If decryption failed, use the fake key. Else, the real key. */
2245: if (rsafail)
2246: session_key_int = fake_key_int;
2247: else
2248: session_key_int = real_key_int;
1.231 provos 2249:
1.66 markus 2250: /*
2251: * Extract session key from the decrypted integer. The key is in the
2252: * least significant 256 bits of the integer; the first byte of the
2253: * key is in the highest bits.
2254: */
1.431 tedu 2255: (void) BN_mask_bits(session_key_int, sizeof(session_key) * 8);
2256: len = BN_num_bytes(session_key_int);
2257: if (len < 0 || (u_int)len > sizeof(session_key)) {
2258: error("do_ssh1_kex: bad session key len from %s: "
2259: "session_key_int %d > sizeof(session_key) %lu",
2260: get_remote_ipaddr(), len, (u_long)sizeof(session_key));
2261: rsafail++;
2262: } else {
2263: explicit_bzero(session_key, sizeof(session_key));
2264: BN_bn2bin(session_key_int,
2265: session_key + sizeof(session_key) - len);
2266:
2267: derive_ssh1_session_id(
2268: sensitive_data.ssh1_host_key->rsa->n,
2269: sensitive_data.server_key->rsa->n,
2270: cookie, session_id);
2271: /*
2272: * Xor the first 16 bytes of the session key with the
2273: * session id.
2274: */
1.170 markus 2275: for (i = 0; i < 16; i++)
1.431 tedu 2276: session_key[i] ^= session_id[i];
1.159 markus 2277: }
1.431 tedu 2278:
1.231 provos 2279: /* Destroy the private and public keys. No longer. */
1.169 markus 2280: destroy_sensitive_data();
2281:
1.231 provos 2282: if (use_privsep)
2283: mm_ssh1_session_id(session_id);
2284:
1.77 markus 2285: /* Destroy the decrypted integer. It is no longer needed. */
1.431 tedu 2286: BN_clear_free(real_key_int);
2287: BN_clear_free(fake_key_int);
1.64 markus 2288:
2289: /* Set the session key. From this on all communications will be encrypted. */
2290: packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
2291:
2292: /* Destroy our copy of the session key. It is no longer needed. */
1.418 djm 2293: explicit_bzero(session_key, sizeof(session_key));
1.64 markus 2294:
2295: debug("Received session key; encryption turned on.");
2296:
1.243 deraadt 2297: /* Send an acknowledgment packet. Note that this packet is sent encrypted. */
1.64 markus 2298: packet_start(SSH_SMSG_SUCCESS);
2299: packet_send();
2300: packet_write_wait();
1.98 markus 2301: }
1.426 markus 2302: #endif
1.98 markus 2303:
1.435 markus 2304: int
2305: sshd_hostkey_sign(Key *privkey, Key *pubkey, u_char **signature, size_t *slen,
2306: u_char *data, size_t dlen, u_int flag)
1.404 markus 2307: {
1.432 djm 2308: int r;
1.435 markus 2309: u_int xxx_slen, xxx_dlen = dlen;
1.432 djm 2310:
1.404 markus 2311: if (privkey) {
1.435 markus 2312: if (PRIVSEP(key_sign(privkey, signature, &xxx_slen, data, xxx_dlen) < 0))
1.404 markus 2313: fatal("%s: key_sign failed", __func__);
1.435 markus 2314: if (slen)
2315: *slen = xxx_slen;
1.404 markus 2316: } else if (use_privsep) {
1.435 markus 2317: if (mm_key_sign(pubkey, signature, &xxx_slen, data, xxx_dlen) < 0)
1.404 markus 2318: fatal("%s: pubkey_sign failed", __func__);
1.435 markus 2319: if (slen)
2320: *slen = xxx_slen;
1.404 markus 2321: } else {
1.435 markus 2322: if ((r = ssh_agent_sign(auth_sock, pubkey, signature, slen,
1.432 djm 2323: data, dlen, datafellows)) != 0)
2324: fatal("%s: ssh_agent_sign failed: %s",
2325: __func__, ssh_err(r));
1.404 markus 2326: }
1.435 markus 2327: return 0;
1.404 markus 2328: }
2329:
1.98 markus 2330: /*
2331: * SSH2 key exchange: diffie-hellman-group1-sha1
2332: */
1.200 itojun 2333: static void
1.142 markus 2334: do_ssh2_kex(void)
1.98 markus 2335: {
1.422 markus 2336: char *myproposal[PROPOSAL_MAX] = { KEX_SERVER };
1.435 markus 2337: struct kex *kex;
1.437 markus 2338: int r;
1.102 markus 2339:
2340: if (options.ciphers != NULL) {
1.105 markus 2341: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1.102 markus 2342: myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
1.166 markus 2343: }
1.184 stevesk 2344: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
2345: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
2346: myproposal[PROPOSAL_ENC_ALGS_STOC] =
2347: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
2348:
1.166 markus 2349: if (options.macs != NULL) {
2350: myproposal[PROPOSAL_MAC_ALGS_CTOS] =
2351: myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1.246 markus 2352: }
1.312 markus 2353: if (options.compression == COMP_NONE) {
1.246 markus 2354: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
2355: myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
1.312 markus 2356: } else if (options.compression == COMP_DELAYED) {
2357: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
2358: myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com";
1.102 markus 2359: }
1.380 djm 2360: if (options.kex_algorithms != NULL)
2361: myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
1.424 djm 2362:
2363: myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
2364: myproposal[PROPOSAL_KEX_ALGS]);
1.400 dtucker 2365:
2366: if (options.rekey_limit || options.rekey_interval)
2367: packet_set_rekey_limits((u_int32_t)options.rekey_limit,
2368: (time_t)options.rekey_interval);
1.327 deraadt 2369:
1.413 djm 2370: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal(
2371: list_hostkey_types());
1.134 markus 2372:
1.189 markus 2373: /* start key exchange */
1.437 markus 2374: if ((r = kex_setup(active_state, myproposal)) != 0)
2375: fatal("kex_setup: %s", ssh_err(r));
1.435 markus 2376: kex = active_state->kex;
1.426 markus 2377: #ifdef WITH_OPENSSL
1.263 markus 2378: kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1.292 djm 2379: kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1.263 markus 2380: kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1.324 djm 2381: kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1.378 djm 2382: kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1.426 markus 2383: #endif
1.410 markus 2384: kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1.186 markus 2385: kex->server = 1;
2386: kex->client_version_string=client_version_string;
2387: kex->server_version_string=server_version_string;
1.373 djm 2388: kex->load_host_public_key=&get_hostkey_public_by_type;
2389: kex->load_host_private_key=&get_hostkey_private_by_type;
1.231 provos 2390: kex->host_key_index=&get_hostkey_index;
1.404 markus 2391: kex->sign = sshd_hostkey_sign;
1.189 markus 2392:
1.435 markus 2393: dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
1.187 markus 2394:
2395: session_id2 = kex->session_id;
2396: session_id2_len = kex->session_id_len;
1.129 provos 2397:
2398: #ifdef DEBUG_KEXDH
2399: /* send 1st encrypted/maced/compressed message */
2400: packet_start(SSH2_MSG_IGNORE);
2401: packet_put_cstring("markus");
2402: packet_send();
2403: packet_write_wait();
2404: #endif
1.186 markus 2405: debug("KEX done");
1.278 markus 2406: }
2407:
2408: /* server specific fatal cleanup */
2409: void
2410: cleanup_exit(int i)
2411: {
1.386 djm 2412: if (the_authctxt) {
1.278 markus 2413: do_cleanup(the_authctxt);
1.423 djm 2414: if (use_privsep && privsep_is_preauth &&
2415: pmonitor != NULL && pmonitor->m_pid > 1) {
1.386 djm 2416: debug("Killing privsep child %d", pmonitor->m_pid);
2417: if (kill(pmonitor->m_pid, SIGKILL) != 0 &&
1.388 djm 2418: errno != ESRCH)
1.386 djm 2419: error("%s: kill(%d): %s", __func__,
2420: pmonitor->m_pid, strerror(errno));
2421: }
2422: }
1.278 markus 2423: _exit(i);
1.1 deraadt 2424: }