Annotation of src/usr.bin/ssh/sshd.c, Revision 1.424
1.424 ! djm 1: /* $OpenBSD: sshd.c,v 1.423 2014/04/12 04:55:53 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.1 deraadt 64:
1.155 markus 65: #include <openssl/dh.h>
66: #include <openssl/bn.h>
1.231 provos 67: #include <openssl/rand.h>
1.155 markus 68:
1.343 deraadt 69: #include "xmalloc.h"
1.155 markus 70: #include "ssh.h"
71: #include "ssh1.h"
72: #include "ssh2.h"
1.1 deraadt 73: #include "rsa.h"
1.171 djm 74: #include "sshpty.h"
1.1 deraadt 75: #include "packet.h"
1.155 markus 76: #include "log.h"
1.343 deraadt 77: #include "buffer.h"
1.1 deraadt 78: #include "servconf.h"
79: #include "uidswap.h"
1.33 markus 80: #include "compat.h"
1.155 markus 81: #include "cipher.h"
1.415 markus 82: #include "digest.h"
1.343 deraadt 83: #include "key.h"
1.98 markus 84: #include "kex.h"
1.129 provos 85: #include "dh.h"
1.98 markus 86: #include "myproposal.h"
1.108 markus 87: #include "authfile.h"
1.154 markus 88: #include "pathnames.h"
1.155 markus 89: #include "atomicio.h"
90: #include "canohost.h"
1.343 deraadt 91: #include "hostfile.h"
1.155 markus 92: #include "auth.h"
1.404 markus 93: #include "authfd.h"
1.155 markus 94: #include "misc.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.1 deraadt 108:
109: #ifndef O_NOCTTY
110: #define O_NOCTTY 0
111: #endif
112:
1.296 djm 113: /* Re-exec fds */
114: #define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1)
115: #define REEXEC_STARTUP_PIPE_FD (STDERR_FILENO + 2)
116: #define REEXEC_CONFIG_PASS_FD (STDERR_FILENO + 3)
117: #define REEXEC_MIN_FREE_FD (STDERR_FILENO + 4)
118:
1.138 markus 119: extern char *__progname;
120:
1.1 deraadt 121: /* Server configuration options. */
122: ServerOptions options;
123:
124: /* Name of the server configuration file. */
1.154 markus 125: char *config_file_name = _PATH_SERVER_CONFIG_FILE;
1.1 deraadt 126:
1.105 markus 127: /*
1.65 deraadt 128: * Debug mode flag. This can be set on the command line. If debug
129: * mode is enabled, extra debugging output will be sent to the system
130: * log, the daemon will not go to background, and will exit after processing
131: * the first connection.
132: */
1.1 deraadt 133: int debug_flag = 0;
134:
1.203 stevesk 135: /* Flag indicating that the daemon should only test the configuration and keys. */
136: int test_flag = 0;
137:
1.1 deraadt 138: /* Flag indicating that the daemon is being started from inetd. */
139: int inetd_flag = 0;
140:
1.135 markus 141: /* Flag indicating that sshd should not detach and become a daemon. */
142: int no_daemon_flag = 0;
143:
1.47 markus 144: /* debug goes to stderr unless inetd_flag is set */
145: int log_stderr = 0;
146:
1.1 deraadt 147: /* Saved arguments to main(). */
148: char **saved_argv;
149:
1.294 djm 150: /* re-exec */
151: int rexeced_flag = 0;
152: int rexec_flag = 1;
153: int rexec_argc = 0;
154: char **rexec_argv;
155:
1.66 markus 156: /*
1.75 markus 157: * The sockets that the server is listening; this is used in the SIGHUP
158: * signal handler.
1.66 markus 159: */
1.75 markus 160: #define MAX_LISTEN_SOCKS 16
161: int listen_socks[MAX_LISTEN_SOCKS];
162: int num_listen_socks = 0;
1.1 deraadt 163:
1.66 markus 164: /*
165: * the client's version string, passed by sshd2 in compat mode. if != NULL,
166: * sshd will skip the version-number exchange
167: */
1.61 markus 168: char *client_version_string = NULL;
1.96 markus 169: char *server_version_string = NULL;
1.1 deraadt 170:
1.189 markus 171: /* for rekeying XXX fixme */
172: Kex *xxx_kex;
173:
1.404 markus 174: /* Daemon's agent connection */
175: AuthenticationConnection *auth_conn = NULL;
176: int have_agent = 0;
177:
1.66 markus 178: /*
179: * Any really sensitive data in the application is contained in this
180: * structure. The idea is that this structure could be locked into memory so
181: * that the pages do not get written into swap. However, there are some
182: * problems. The private key contains BIGNUMs, and we do not (in principle)
183: * have access to the internals of them, and locking just the structure is
184: * not very useful. Currently, memory locking is not implemented.
185: */
1.64 markus 186: struct {
1.174 deraadt 187: Key *server_key; /* ephemeral server key */
1.134 markus 188: Key *ssh1_host_key; /* ssh1 host key */
189: Key **host_keys; /* all private host keys */
1.404 markus 190: Key **host_pubkeys; /* all public host keys */
1.373 djm 191: Key **host_certificates; /* all public host certificates */
1.134 markus 192: int have_ssh1_key;
193: int have_ssh2_key;
1.169 markus 194: u_char ssh1_cookie[SSH_SESSION_KEY_LENGTH];
1.1 deraadt 195: } sensitive_data;
196:
1.66 markus 197: /*
1.151 markus 198: * Flag indicating whether the RSA server key needs to be regenerated.
199: * Is set in the SIGALRM handler and cleared when the key is regenerated.
1.66 markus 200: */
1.212 markus 201: static volatile sig_atomic_t key_do_regen = 0;
1.1 deraadt 202:
1.199 markus 203: /* This is set to true when a signal is received. */
1.212 markus 204: static volatile sig_atomic_t received_sighup = 0;
205: static volatile sig_atomic_t received_sigterm = 0;
1.1 deraadt 206:
1.96 markus 207: /* session identifier, used by RSA-auth */
1.140 markus 208: u_char session_id[16];
1.96 markus 209:
1.108 markus 210: /* same for ssh2 */
1.140 markus 211: u_char *session_id2 = NULL;
1.269 markus 212: u_int session_id2_len = 0;
1.108 markus 213:
1.125 markus 214: /* record remote hostname or ip */
1.140 markus 215: u_int utmp_len = MAXHOSTNAMELEN;
1.125 markus 216:
1.211 markus 217: /* options.max_startup sized array of fd ints */
218: int *startup_pipes = NULL;
219: int startup_pipe; /* in child */
220:
1.231 provos 221: /* variables used for privilege separation */
1.337 dtucker 222: int use_privsep = -1;
1.285 dtucker 223: struct monitor *pmonitor = NULL;
1.386 djm 224: int privsep_is_preauth = 1;
1.231 provos 225:
1.278 markus 226: /* global authentication context */
227: Authctxt *the_authctxt = NULL;
228:
1.337 dtucker 229: /* sshd_config buffer */
230: Buffer cfg;
231:
1.299 dtucker 232: /* message to be displayed after login */
233: Buffer loginmsg;
234:
1.1 deraadt 235: /* Prototypes for various functions defined later in this file. */
1.200 itojun 236: void destroy_sensitive_data(void);
1.231 provos 237: void demote_sensitive_data(void);
1.87 markus 238:
1.200 itojun 239: static void do_ssh1_kex(void);
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.98 markus 465: 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.254 deraadt 587: u_int32_t rnd[256];
1.253 deraadt 588: gid_t gidset[1];
1.250 deraadt 589: struct passwd *pw;
1.231 provos 590:
591: /* Enable challenge-response authentication for privilege separation */
592: privsep_challenge_enable();
1.419 djm 593:
1.420 markus 594: #ifdef GSSAPI
1.419 djm 595: /* Cache supported mechanism OIDs for later use */
596: if (options.gss_authentication)
597: ssh_gssapi_prepare_supported_oids();
1.420 markus 598: #endif
1.231 provos 599:
1.356 djm 600: arc4random_buf(rnd, sizeof(rnd));
1.254 deraadt 601: RAND_seed(rnd, sizeof(rnd));
1.417 tedu 602: explicit_bzero(rnd, sizeof(rnd));
1.231 provos 603:
604: /* Demote the private keys to public keys. */
605: demote_sensitive_data();
606:
1.235 stevesk 607: if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
1.240 djm 608: fatal("Privilege separation user %s does not exist",
609: SSH_PRIVSEP_USER);
1.418 djm 610: explicit_bzero(pw->pw_passwd, strlen(pw->pw_passwd));
1.235 stevesk 611: endpwent();
612:
1.255 deraadt 613: /* Change our root directory */
1.232 stevesk 614: if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
615: fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
616: strerror(errno));
1.231 provos 617: if (chdir("/") == -1)
1.236 stevesk 618: fatal("chdir(\"/\"): %s", strerror(errno));
1.234 markus 619:
1.231 provos 620: /* Drop our privileges */
1.235 stevesk 621: debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
622: (u_int)pw->pw_gid);
1.251 markus 623: #if 0
1.287 djm 624: /* XXX not ready, too heavy after chroot */
1.235 stevesk 625: do_setusercontext(pw);
1.251 markus 626: #else
627: gidset[0] = pw->pw_gid;
628: if (setgroups(1, gidset) < 0)
629: fatal("setgroups: %.100s", strerror(errno));
630: permanently_set_uid(pw);
631: #endif
1.231 provos 632: }
633:
1.278 markus 634: static int
635: privsep_preauth(Authctxt *authctxt)
1.237 markus 636: {
637: int status;
638: pid_t pid;
1.384 djm 639: struct ssh_sandbox *box = NULL;
1.237 markus 640:
641: /* Set up unprivileged child process to deal with network data */
1.242 mouring 642: pmonitor = monitor_init();
1.237 markus 643: /* Store a pointer to the kex for later rekeying */
1.242 mouring 644: pmonitor->m_pkex = &xxx_kex;
1.237 markus 645:
1.393 djm 646: if (use_privsep == PRIVSEP_ON)
1.384 djm 647: box = ssh_sandbox_init();
1.237 markus 648: pid = fork();
649: if (pid == -1) {
650: fatal("fork of unprivileged child failed");
651: } else if (pid != 0) {
1.245 mpech 652: debug2("Network child is on pid %ld", (long)pid);
1.237 markus 653:
1.392 markus 654: pmonitor->m_pid = pid;
1.404 markus 655: if (have_agent)
656: auth_conn = ssh_get_authentication_connection();
1.384 djm 657: if (box != NULL)
658: ssh_sandbox_parent_preauth(box, pid);
1.278 markus 659: monitor_child_preauth(authctxt, pmonitor);
1.237 markus 660:
661: /* Sync memory */
1.242 mouring 662: monitor_sync(pmonitor);
1.237 markus 663:
664: /* Wait for the child's exit status */
1.384 djm 665: while (waitpid(pid, &status, 0) < 0) {
1.386 djm 666: if (errno == EINTR)
667: continue;
668: pmonitor->m_pid = -1;
669: fatal("%s: waitpid: %s", __func__, strerror(errno));
1.384 djm 670: }
1.386 djm 671: privsep_is_preauth = 0;
672: pmonitor->m_pid = -1;
1.384 djm 673: if (WIFEXITED(status)) {
674: if (WEXITSTATUS(status) != 0)
675: fatal("%s: preauth child exited with status %d",
676: __func__, WEXITSTATUS(status));
677: } else if (WIFSIGNALED(status))
678: fatal("%s: preauth child terminated by signal %d",
679: __func__, WTERMSIG(status));
680: if (box != NULL)
681: ssh_sandbox_parent_finish(box);
682: return 1;
1.237 markus 683: } else {
684: /* child */
1.383 djm 685: close(pmonitor->m_sendfd);
686: close(pmonitor->m_log_recvfd);
1.237 markus 687:
1.383 djm 688: /* Arrange for logging to be sent to the monitor */
689: set_log_handler(mm_log_handler, pmonitor);
1.237 markus 690:
691: /* Demote the child */
692: if (getuid() == 0 || geteuid() == 0)
693: privsep_preauth_child();
1.238 stevesk 694: setproctitle("%s", "[net]");
1.384 djm 695: if (box != NULL)
696: ssh_sandbox_child(box);
697:
698: return 0;
1.237 markus 699: }
700: }
701:
1.233 markus 702: static void
1.237 markus 703: privsep_postauth(Authctxt *authctxt)
1.231 provos 704: {
1.354 djm 705: u_int32_t rnd[256];
706:
1.231 provos 707: if (authctxt->pw->pw_uid == 0 || options.use_login) {
708: /* File descriptor passing is broken or root login */
709: use_privsep = 0;
1.315 djm 710: goto skip;
1.231 provos 711: }
1.234 markus 712:
1.231 provos 713: /* New socket pair */
1.242 mouring 714: monitor_reinit(pmonitor);
1.231 provos 715:
1.242 mouring 716: pmonitor->m_pid = fork();
717: if (pmonitor->m_pid == -1)
1.231 provos 718: fatal("fork of unprivileged child failed");
1.242 mouring 719: else if (pmonitor->m_pid != 0) {
1.364 markus 720: verbose("User child is on pid %ld", (long)pmonitor->m_pid);
1.307 otto 721: buffer_clear(&loginmsg);
1.242 mouring 722: monitor_child_postauth(pmonitor);
1.231 provos 723:
724: /* NEVERREACHED */
725: exit(0);
726: }
727:
1.383 djm 728: /* child */
729:
1.242 mouring 730: close(pmonitor->m_sendfd);
1.383 djm 731: pmonitor->m_sendfd = -1;
1.231 provos 732:
733: /* Demote the private keys to public keys. */
734: demote_sensitive_data();
1.354 djm 735:
1.356 djm 736: arc4random_buf(rnd, sizeof(rnd));
1.354 djm 737: RAND_seed(rnd, sizeof(rnd));
1.417 tedu 738: explicit_bzero(rnd, sizeof(rnd));
1.231 provos 739:
740: /* Drop privileges */
741: do_setusercontext(authctxt->pw);
742:
1.315 djm 743: skip:
1.231 provos 744: /* It is safe now to apply the key state */
1.242 mouring 745: monitor_apply_keystate(pmonitor);
1.312 markus 746:
747: /*
748: * Tell the packet layer that authentication was successful, since
749: * this information is not part of the key state.
750: */
751: packet_set_authenticated();
1.231 provos 752: }
753:
1.200 itojun 754: static char *
1.134 markus 755: list_hostkey_types(void)
756: {
1.223 markus 757: Buffer b;
1.281 jakob 758: const char *p;
759: char *ret;
1.134 markus 760: int i;
1.373 djm 761: Key *key;
1.223 markus 762:
763: buffer_init(&b);
1.217 deraadt 764: for (i = 0; i < options.num_host_key_files; i++) {
1.373 djm 765: key = sensitive_data.host_keys[i];
1.134 markus 766: if (key == NULL)
1.404 markus 767: key = sensitive_data.host_pubkeys[i];
768: if (key == NULL)
1.134 markus 769: continue;
1.214 deraadt 770: switch (key->type) {
1.134 markus 771: case KEY_RSA:
772: case KEY_DSA:
1.378 djm 773: case KEY_ECDSA:
1.412 markus 774: case KEY_ED25519:
1.223 markus 775: if (buffer_len(&b) > 0)
776: buffer_append(&b, ",", 1);
777: p = key_ssh_name(key);
778: buffer_append(&b, p, strlen(p));
1.134 markus 779: break;
780: }
1.373 djm 781: /* If the private key has a cert peer, then list that too */
782: key = sensitive_data.host_certificates[i];
783: if (key == NULL)
784: continue;
785: switch (key->type) {
1.375 djm 786: case KEY_RSA_CERT_V00:
787: case KEY_DSA_CERT_V00:
1.373 djm 788: case KEY_RSA_CERT:
789: case KEY_DSA_CERT:
1.378 djm 790: case KEY_ECDSA_CERT:
1.412 markus 791: case KEY_ED25519_CERT:
1.373 djm 792: if (buffer_len(&b) > 0)
793: buffer_append(&b, ",", 1);
794: p = key_ssh_name(key);
795: buffer_append(&b, p, strlen(p));
796: break;
797: }
1.134 markus 798: }
1.223 markus 799: buffer_append(&b, "\0", 1);
1.281 jakob 800: ret = xstrdup(buffer_ptr(&b));
1.223 markus 801: buffer_free(&b);
1.281 jakob 802: debug("list_hostkey_types: %s", ret);
803: return ret;
1.134 markus 804: }
805:
1.373 djm 806: static Key *
807: get_hostkey_by_type(int type, int need_private)
1.134 markus 808: {
809: int i;
1.373 djm 810: Key *key;
1.250 deraadt 811:
1.217 deraadt 812: for (i = 0; i < options.num_host_key_files; i++) {
1.375 djm 813: switch (type) {
814: case KEY_RSA_CERT_V00:
815: case KEY_DSA_CERT_V00:
816: case KEY_RSA_CERT:
817: case KEY_DSA_CERT:
1.378 djm 818: case KEY_ECDSA_CERT:
1.412 markus 819: case KEY_ED25519_CERT:
1.373 djm 820: key = sensitive_data.host_certificates[i];
1.375 djm 821: break;
822: default:
1.373 djm 823: key = sensitive_data.host_keys[i];
1.404 markus 824: if (key == NULL && !need_private)
825: key = sensitive_data.host_pubkeys[i];
1.375 djm 826: break;
827: }
1.134 markus 828: if (key != NULL && key->type == type)
1.373 djm 829: return need_private ?
830: sensitive_data.host_keys[i] : key;
1.134 markus 831: }
832: return NULL;
1.96 markus 833: }
834:
1.231 provos 835: Key *
1.373 djm 836: get_hostkey_public_by_type(int type)
837: {
838: return get_hostkey_by_type(type, 0);
839: }
840:
841: Key *
842: get_hostkey_private_by_type(int type)
843: {
844: return get_hostkey_by_type(type, 1);
845: }
846:
847: Key *
1.231 provos 848: get_hostkey_by_index(int ind)
849: {
850: if (ind < 0 || ind >= options.num_host_key_files)
851: return (NULL);
852: return (sensitive_data.host_keys[ind]);
853: }
854:
1.404 markus 855: Key *
856: get_hostkey_public_by_index(int ind)
857: {
858: if (ind < 0 || ind >= options.num_host_key_files)
859: return (NULL);
860: return (sensitive_data.host_pubkeys[ind]);
861: }
862:
1.231 provos 863: int
864: get_hostkey_index(Key *key)
865: {
866: int i;
1.250 deraadt 867:
1.231 provos 868: for (i = 0; i < options.num_host_key_files; i++) {
1.373 djm 869: if (key_is_cert(key)) {
870: if (key == sensitive_data.host_certificates[i])
871: return (i);
872: } else {
873: if (key == sensitive_data.host_keys[i])
874: return (i);
1.404 markus 875: if (key == sensitive_data.host_pubkeys[i])
876: return (i);
1.373 djm 877: }
1.231 provos 878: }
879: return (-1);
880: }
881:
1.124 markus 882: /*
883: * returns 1 if connection should be dropped, 0 otherwise.
884: * dropping starts at connection #max_startups_begin with a probability
885: * of (max_startups_rate/100). the probability increases linearly until
886: * all connections are dropped for startups > max_startups
887: */
1.200 itojun 888: static int
1.124 markus 889: drop_connection(int startups)
890: {
1.303 mickey 891: int p, r;
1.124 markus 892:
893: if (startups < options.max_startups_begin)
894: return 0;
895: if (startups >= options.max_startups)
896: return 1;
897: if (options.max_startups_rate == 100)
898: return 1;
899:
900: p = 100 - options.max_startups_rate;
901: p *= startups - options.max_startups_begin;
1.303 mickey 902: p /= options.max_startups - options.max_startups_begin;
1.124 markus 903: p += options.max_startups_rate;
1.356 djm 904: r = arc4random_uniform(100);
1.124 markus 905:
1.304 djm 906: debug("drop_connection: p %d, r %d", p, r);
1.124 markus 907: return (r < p) ? 1 : 0;
908: }
909:
1.215 markus 910: static void
911: usage(void)
912: {
1.290 markus 913: fprintf(stderr, "%s, %s\n",
1.280 markus 914: SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
1.289 markus 915: fprintf(stderr,
1.373 djm 916: "usage: sshd [-46DdeiqTt] [-b bits] [-C connection_spec] [-c host_cert_file]\n"
1.399 dtucker 917: " [-E log_file] [-f config_file] [-g login_grace_time]\n"
918: " [-h host_key_file] [-k key_gen_time] [-o option] [-p port]\n"
919: " [-u len]\n"
1.289 markus 920: );
1.215 markus 921: exit(1);
922: }
923:
1.294 djm 924: static void
925: send_rexec_state(int fd, Buffer *conf)
926: {
927: Buffer m;
928:
929: debug3("%s: entering fd = %d config len %d", __func__, fd,
930: buffer_len(conf));
931:
932: /*
933: * Protocol from reexec master to child:
934: * string configuration
935: * u_int ephemeral_key_follows
936: * bignum e (only if ephemeral_key_follows == 1)
937: * bignum n "
938: * bignum d "
939: * bignum iqmp "
940: * bignum p "
941: * bignum q "
942: */
943: buffer_init(&m);
944: buffer_put_cstring(&m, buffer_ptr(conf));
945:
1.298 deraadt 946: if (sensitive_data.server_key != NULL &&
1.294 djm 947: sensitive_data.server_key->type == KEY_RSA1) {
948: buffer_put_int(&m, 1);
949: buffer_put_bignum(&m, sensitive_data.server_key->rsa->e);
950: buffer_put_bignum(&m, sensitive_data.server_key->rsa->n);
951: buffer_put_bignum(&m, sensitive_data.server_key->rsa->d);
952: buffer_put_bignum(&m, sensitive_data.server_key->rsa->iqmp);
953: buffer_put_bignum(&m, sensitive_data.server_key->rsa->p);
954: buffer_put_bignum(&m, sensitive_data.server_key->rsa->q);
955: } else
956: buffer_put_int(&m, 0);
957:
958: if (ssh_msg_send(fd, 0, &m) == -1)
959: fatal("%s: ssh_msg_send failed", __func__);
960:
961: buffer_free(&m);
962:
963: debug3("%s: done", __func__);
964: }
965:
966: static void
967: recv_rexec_state(int fd, Buffer *conf)
968: {
969: Buffer m;
970: char *cp;
971: u_int len;
972:
973: debug3("%s: entering fd = %d", __func__, fd);
974:
975: buffer_init(&m);
976:
977: if (ssh_msg_recv(fd, &m) == -1)
978: fatal("%s: ssh_msg_recv failed", __func__);
979: if (buffer_get_char(&m) != 0)
980: fatal("%s: rexec version mismatch", __func__);
981:
982: cp = buffer_get_string(&m, &len);
983: if (conf != NULL)
984: buffer_append(conf, cp, len + 1);
1.402 djm 985: free(cp);
1.294 djm 986:
987: if (buffer_get_int(&m)) {
988: if (sensitive_data.server_key != NULL)
989: key_free(sensitive_data.server_key);
990: sensitive_data.server_key = key_new_private(KEY_RSA1);
991: buffer_get_bignum(&m, sensitive_data.server_key->rsa->e);
992: buffer_get_bignum(&m, sensitive_data.server_key->rsa->n);
993: buffer_get_bignum(&m, sensitive_data.server_key->rsa->d);
994: buffer_get_bignum(&m, sensitive_data.server_key->rsa->iqmp);
995: buffer_get_bignum(&m, sensitive_data.server_key->rsa->p);
996: buffer_get_bignum(&m, sensitive_data.server_key->rsa->q);
997: rsa_generate_additional_parameters(
998: sensitive_data.server_key->rsa);
999: }
1000: buffer_free(&m);
1001:
1002: debug3("%s: done", __func__);
1003: }
1004:
1.345 djm 1005: /* Accept a connection from inetd */
1006: static void
1007: server_accept_inetd(int *sock_in, int *sock_out)
1008: {
1009: int fd;
1010:
1011: startup_pipe = -1;
1012: if (rexeced_flag) {
1013: close(REEXEC_CONFIG_PASS_FD);
1014: *sock_in = *sock_out = dup(STDIN_FILENO);
1015: if (!debug_flag) {
1016: startup_pipe = dup(REEXEC_STARTUP_PIPE_FD);
1017: close(REEXEC_STARTUP_PIPE_FD);
1018: }
1019: } else {
1020: *sock_in = dup(STDIN_FILENO);
1021: *sock_out = dup(STDOUT_FILENO);
1022: }
1023: /*
1024: * We intentionally do not close the descriptors 0, 1, and 2
1025: * as our code for setting the descriptors won't work if
1026: * ttyfd happens to be one of those.
1027: */
1028: if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1029: dup2(fd, STDIN_FILENO);
1030: dup2(fd, STDOUT_FILENO);
1.403 dtucker 1031: if (!log_stderr)
1032: dup2(fd, STDERR_FILENO);
1033: if (fd > (log_stderr ? STDERR_FILENO : STDOUT_FILENO))
1.345 djm 1034: close(fd);
1035: }
1036: debug("inetd sockets after dupping: %d, %d", *sock_in, *sock_out);
1037: }
1038:
1039: /*
1040: * Listen for TCP connections
1041: */
1042: static void
1043: server_listen(void)
1044: {
1045: int ret, listen_sock, on = 1;
1046: struct addrinfo *ai;
1047: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1048:
1049: for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
1050: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1051: continue;
1052: if (num_listen_socks >= MAX_LISTEN_SOCKS)
1053: fatal("Too many listen sockets. "
1054: "Enlarge MAX_LISTEN_SOCKS");
1055: if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
1056: ntop, sizeof(ntop), strport, sizeof(strport),
1057: NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1058: error("getnameinfo failed: %.100s",
1.352 dtucker 1059: ssh_gai_strerror(ret));
1.345 djm 1060: continue;
1061: }
1062: /* Create socket for listening. */
1.370 dtucker 1063: listen_sock = socket(ai->ai_family, ai->ai_socktype,
1064: ai->ai_protocol);
1.345 djm 1065: if (listen_sock < 0) {
1066: /* kernel may not support ipv6 */
1067: verbose("socket: %.100s", strerror(errno));
1068: continue;
1069: }
1070: if (set_nonblock(listen_sock) == -1) {
1071: close(listen_sock);
1072: continue;
1073: }
1074: /*
1075: * Set socket options.
1076: * Allow local port reuse in TIME_WAIT.
1077: */
1078: if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
1079: &on, sizeof(on)) == -1)
1080: error("setsockopt SO_REUSEADDR: %s", strerror(errno));
1081:
1082: debug("Bind to port %s on %s.", strport, ntop);
1083:
1084: /* Bind the socket to the desired port. */
1085: if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1086: error("Bind to port %s on %s failed: %.200s.",
1087: strport, ntop, strerror(errno));
1088: close(listen_sock);
1089: continue;
1090: }
1091: listen_socks[num_listen_socks] = listen_sock;
1092: num_listen_socks++;
1093:
1094: /* Start listening on the port. */
1095: if (listen(listen_sock, SSH_LISTEN_BACKLOG) < 0)
1096: fatal("listen on [%s]:%s: %.100s",
1097: ntop, strport, strerror(errno));
1098: logit("Server listening on %s port %s.", ntop, strport);
1099: }
1100: freeaddrinfo(options.listen_addrs);
1101:
1102: if (!num_listen_socks)
1103: fatal("Cannot bind any address.");
1104: }
1105:
1106: /*
1107: * The main TCP accept loop. Note that, for the non-debug case, returns
1108: * from this function are in a forked subprocess.
1109: */
1110: static void
1111: server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
1112: {
1113: fd_set *fdset;
1114: int i, j, ret, maxfd;
1115: int key_used = 0, startups = 0;
1116: int startup_p[2] = { -1 , -1 };
1117: struct sockaddr_storage from;
1118: socklen_t fromlen;
1119: pid_t pid;
1.405 djm 1120: u_char rnd[256];
1.345 djm 1121:
1122: /* setup fd set for accept */
1123: fdset = NULL;
1124: maxfd = 0;
1125: for (i = 0; i < num_listen_socks; i++)
1126: if (listen_socks[i] > maxfd)
1127: maxfd = listen_socks[i];
1128: /* pipes connected to unauthenticated childs */
1129: startup_pipes = xcalloc(options.max_startups, sizeof(int));
1130: for (i = 0; i < options.max_startups; i++)
1131: startup_pipes[i] = -1;
1132:
1133: /*
1134: * Stay listening for connections until the system crashes or
1135: * the daemon is killed with a signal.
1136: */
1137: for (;;) {
1138: if (received_sighup)
1139: sighup_restart();
1140: if (fdset != NULL)
1.402 djm 1141: free(fdset);
1.345 djm 1142: fdset = (fd_set *)xcalloc(howmany(maxfd + 1, NFDBITS),
1143: sizeof(fd_mask));
1144:
1145: for (i = 0; i < num_listen_socks; i++)
1146: FD_SET(listen_socks[i], fdset);
1147: for (i = 0; i < options.max_startups; i++)
1148: if (startup_pipes[i] != -1)
1149: FD_SET(startup_pipes[i], fdset);
1150:
1151: /* Wait in select until there is a connection. */
1152: ret = select(maxfd+1, fdset, NULL, NULL, NULL);
1153: if (ret < 0 && errno != EINTR)
1154: error("select: %.100s", strerror(errno));
1155: if (received_sigterm) {
1156: logit("Received signal %d; terminating.",
1157: (int) received_sigterm);
1158: close_listen_socks();
1159: unlink(options.pid_file);
1.382 djm 1160: exit(received_sigterm == SIGTERM ? 0 : 255);
1.345 djm 1161: }
1162: if (key_used && key_do_regen) {
1163: generate_ephemeral_server_key();
1164: key_used = 0;
1165: key_do_regen = 0;
1166: }
1167: if (ret < 0)
1168: continue;
1169:
1170: for (i = 0; i < options.max_startups; i++)
1171: if (startup_pipes[i] != -1 &&
1172: FD_ISSET(startup_pipes[i], fdset)) {
1173: /*
1174: * the read end of the pipe is ready
1175: * if the child has closed the pipe
1176: * after successful authentication
1177: * or if the child has died
1178: */
1179: close(startup_pipes[i]);
1180: startup_pipes[i] = -1;
1181: startups--;
1182: }
1183: for (i = 0; i < num_listen_socks; i++) {
1184: if (!FD_ISSET(listen_socks[i], fdset))
1185: continue;
1186: fromlen = sizeof(from);
1187: *newsock = accept(listen_socks[i],
1188: (struct sockaddr *)&from, &fromlen);
1189: if (*newsock < 0) {
1.398 markus 1190: if (errno != EINTR && errno != EWOULDBLOCK &&
1191: errno != ECONNABORTED)
1.389 djm 1192: error("accept: %.100s",
1193: strerror(errno));
1194: if (errno == EMFILE || errno == ENFILE)
1195: usleep(100 * 1000);
1.345 djm 1196: continue;
1197: }
1198: if (unset_nonblock(*newsock) == -1) {
1199: close(*newsock);
1200: continue;
1201: }
1202: if (drop_connection(startups) == 1) {
1203: debug("drop connection #%d", startups);
1204: close(*newsock);
1205: continue;
1206: }
1207: if (pipe(startup_p) == -1) {
1208: close(*newsock);
1209: continue;
1210: }
1211:
1212: if (rexec_flag && socketpair(AF_UNIX,
1213: SOCK_STREAM, 0, config_s) == -1) {
1214: error("reexec socketpair: %s",
1215: strerror(errno));
1216: close(*newsock);
1217: close(startup_p[0]);
1218: close(startup_p[1]);
1219: continue;
1220: }
1221:
1222: for (j = 0; j < options.max_startups; j++)
1223: if (startup_pipes[j] == -1) {
1224: startup_pipes[j] = startup_p[0];
1225: if (maxfd < startup_p[0])
1226: maxfd = startup_p[0];
1227: startups++;
1228: break;
1229: }
1230:
1231: /*
1232: * Got connection. Fork a child to handle it, unless
1233: * we are in debugging mode.
1234: */
1235: if (debug_flag) {
1236: /*
1237: * In debugging mode. Close the listening
1238: * socket, and start processing the
1239: * connection without forking.
1240: */
1241: debug("Server will not fork when running in debugging mode.");
1242: close_listen_socks();
1243: *sock_in = *newsock;
1244: *sock_out = *newsock;
1245: close(startup_p[0]);
1246: close(startup_p[1]);
1247: startup_pipe = -1;
1248: pid = getpid();
1249: if (rexec_flag) {
1250: send_rexec_state(config_s[0],
1251: &cfg);
1252: close(config_s[0]);
1253: }
1254: break;
1255: }
1256:
1257: /*
1258: * Normal production daemon. Fork, and have
1259: * the child process the connection. The
1260: * parent continues listening.
1261: */
1262: if ((pid = fork()) == 0) {
1263: /*
1264: * Child. Close the listening and
1265: * max_startup sockets. Start using
1266: * the accepted socket. Reinitialize
1267: * logging (since our pid has changed).
1268: * We break out of the loop to handle
1269: * the connection.
1270: */
1271: startup_pipe = startup_p[1];
1272: close_startup_pipes();
1273: close_listen_socks();
1274: *sock_in = *newsock;
1275: *sock_out = *newsock;
1276: log_init(__progname,
1277: options.log_level,
1278: options.log_facility,
1279: log_stderr);
1280: if (rexec_flag)
1281: close(config_s[0]);
1282: break;
1283: }
1284:
1285: /* Parent. Stay in the loop. */
1286: if (pid < 0)
1287: error("fork: %.100s", strerror(errno));
1288: else
1289: debug("Forked child %ld.", (long)pid);
1290:
1291: close(startup_p[1]);
1292:
1293: if (rexec_flag) {
1294: send_rexec_state(config_s[0], &cfg);
1295: close(config_s[0]);
1296: close(config_s[1]);
1297: }
1298:
1299: /*
1300: * Mark that the key has been used (it
1301: * was "given" to the child).
1302: */
1303: if ((options.protocol & SSH_PROTO_1) &&
1304: key_used == 0) {
1305: /* Schedule server key regeneration alarm. */
1306: signal(SIGALRM, key_regeneration_alarm);
1307: alarm(options.key_regeneration_time);
1308: key_used = 1;
1309: }
1310:
1311: close(*newsock);
1312:
1313: /*
1314: * Ensure that our random state differs
1315: * from that of the child
1316: */
1.405 djm 1317: arc4random_buf(rnd, sizeof(rnd));
1318: RAND_seed(rnd, sizeof(rnd));
1.417 tedu 1319: explicit_bzero(rnd, sizeof(rnd));
1.345 djm 1320: }
1321:
1322: /* child process check (or debug mode) */
1323: if (num_listen_socks < 0)
1324: break;
1325: }
1326: }
1327:
1328:
1.65 deraadt 1329: /*
1330: * Main program for the daemon.
1331: */
1.2 provos 1332: int
1333: main(int ac, char **av)
1.1 deraadt 1334: {
1.64 markus 1335: extern char *optarg;
1336: extern int optind;
1.373 djm 1337: int opt, i, j, on = 1;
1.297 avsm 1338: int sock_in = -1, sock_out = -1, newsock = -1;
1.64 markus 1339: const char *remote_ip;
1340: int remote_port;
1.399 dtucker 1341: char *line, *logfile = NULL;
1.345 djm 1342: int config_s[2] = { -1 , -1 };
1.396 djm 1343: u_int n;
1.364 markus 1344: u_int64_t ibytes, obytes;
1.362 dtucker 1345: mode_t new_umask;
1.278 markus 1346: Key *key;
1.404 markus 1347: Key *pubkey;
1348: int keytype;
1.230 provos 1349: Authctxt *authctxt;
1.391 dtucker 1350: struct connection_info *connection_info = get_connection_info(0, 0);
1.64 markus 1351:
1.138 markus 1352: /* Save argv. */
1.64 markus 1353: saved_argv = av;
1.294 djm 1354: rexec_argc = ac;
1.313 djm 1355:
1356: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1357: sanitise_stdfd();
1.64 markus 1358:
1359: /* Initialize configuration options to their default values. */
1360: initialize_server_options(&options);
1361:
1362: /* Parse command-line arguments. */
1.399 dtucker 1363: while ((opt = getopt(ac, av, "f:p:b:k:h:g:u:o:C:dDeE:iqrtQRT46")) != -1) {
1.64 markus 1364: switch (opt) {
1.75 markus 1365: case '4':
1.305 djm 1366: options.address_family = AF_INET;
1.75 markus 1367: break;
1368: case '6':
1.305 djm 1369: options.address_family = AF_INET6;
1.75 markus 1370: break;
1.64 markus 1371: case 'f':
1372: config_file_name = optarg;
1373: break;
1.373 djm 1374: case 'c':
1375: if (options.num_host_cert_files >= MAX_HOSTCERTS) {
1376: fprintf(stderr, "too many host certificates.\n");
1377: exit(1);
1378: }
1379: options.host_cert_files[options.num_host_cert_files++] =
1380: derelativise_path(optarg);
1381: break;
1.64 markus 1382: case 'd':
1.273 markus 1383: if (debug_flag == 0) {
1.127 markus 1384: debug_flag = 1;
1385: options.log_level = SYSLOG_LEVEL_DEBUG1;
1.273 markus 1386: } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
1.127 markus 1387: options.log_level++;
1.64 markus 1388: break;
1.135 markus 1389: case 'D':
1390: no_daemon_flag = 1;
1.192 lebel 1391: break;
1.399 dtucker 1392: case 'E':
1393: logfile = xstrdup(optarg);
1394: /* FALLTHROUGH */
1.192 lebel 1395: case 'e':
1396: log_stderr = 1;
1.135 markus 1397: break;
1.64 markus 1398: case 'i':
1399: inetd_flag = 1;
1400: break;
1.294 djm 1401: case 'r':
1402: rexec_flag = 0;
1403: break;
1404: case 'R':
1405: rexeced_flag = 1;
1406: inetd_flag = 1;
1407: break;
1.64 markus 1408: case 'Q':
1.158 markus 1409: /* ignored */
1.64 markus 1410: break;
1411: case 'q':
1412: options.log_level = SYSLOG_LEVEL_QUIET;
1413: break;
1414: case 'b':
1.327 deraadt 1415: options.server_key_bits = (int)strtonum(optarg, 256,
1416: 32768, NULL);
1.64 markus 1417: break;
1418: case 'p':
1.75 markus 1419: options.ports_from_cmdline = 1;
1.127 markus 1420: if (options.num_ports >= MAX_PORTS) {
1421: fprintf(stderr, "too many ports.\n");
1422: exit(1);
1423: }
1.193 stevesk 1424: options.ports[options.num_ports++] = a2port(optarg);
1.366 djm 1425: if (options.ports[options.num_ports-1] <= 0) {
1.193 stevesk 1426: fprintf(stderr, "Bad port number.\n");
1427: exit(1);
1428: }
1.64 markus 1429: break;
1430: case 'g':
1.197 stevesk 1431: if ((options.login_grace_time = convtime(optarg)) == -1) {
1432: fprintf(stderr, "Invalid login grace time.\n");
1433: exit(1);
1434: }
1.64 markus 1435: break;
1436: case 'k':
1.197 stevesk 1437: if ((options.key_regeneration_time = convtime(optarg)) == -1) {
1438: fprintf(stderr, "Invalid key regeneration interval.\n");
1439: exit(1);
1440: }
1.64 markus 1441: break;
1442: case 'h':
1.134 markus 1443: if (options.num_host_key_files >= MAX_HOSTKEYS) {
1444: fprintf(stderr, "too many host keys.\n");
1445: exit(1);
1446: }
1.371 djm 1447: options.host_key_files[options.num_host_key_files++] =
1448: derelativise_path(optarg);
1.64 markus 1449: break;
1.203 stevesk 1450: case 't':
1451: test_flag = 1;
1452: break;
1.358 dtucker 1453: case 'T':
1454: test_flag = 2;
1455: break;
1456: case 'C':
1.391 dtucker 1457: if (parse_server_match_testspec(connection_info,
1458: optarg) == -1)
1459: exit(1);
1.358 dtucker 1460: break;
1.125 markus 1461: case 'u':
1.327 deraadt 1462: utmp_len = (u_int)strtonum(optarg, 0, MAXHOSTNAMELEN+1, NULL);
1.257 stevesk 1463: if (utmp_len > MAXHOSTNAMELEN) {
1464: fprintf(stderr, "Invalid utmp length.\n");
1465: exit(1);
1466: }
1.125 markus 1467: break;
1.215 markus 1468: case 'o':
1.283 markus 1469: line = xstrdup(optarg);
1470: if (process_server_config_line(&options, line,
1.391 dtucker 1471: "command-line", 0, NULL, NULL) != 0)
1.217 deraadt 1472: exit(1);
1.402 djm 1473: free(line);
1.215 markus 1474: break;
1.64 markus 1475: case '?':
1476: default:
1.215 markus 1477: usage();
1478: break;
1.64 markus 1479: }
1480: }
1.294 djm 1481: if (rexeced_flag || inetd_flag)
1482: rexec_flag = 0;
1.355 mbalmer 1483: if (!test_flag && (rexec_flag && (av[0] == NULL || *av[0] != '/')))
1.294 djm 1484: fatal("sshd re-exec requires execution with an absolute path");
1485: if (rexeced_flag)
1.296 djm 1486: closefrom(REEXEC_MIN_FREE_FD);
1487: else
1488: closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
1.294 djm 1489:
1.379 djm 1490: OpenSSL_add_all_algorithms();
1.64 markus 1491:
1.399 dtucker 1492: /* If requested, redirect the logs to the specified logfile. */
1493: if (logfile != NULL) {
1494: log_redirect_stderr_to(logfile);
1.402 djm 1495: free(logfile);
1.399 dtucker 1496: }
1.75 markus 1497: /*
1498: * Force logging to stderr until we have loaded the private host
1499: * key (unless started from inetd)
1500: */
1.138 markus 1501: log_init(__progname,
1.224 markus 1502: options.log_level == SYSLOG_LEVEL_NOT_SET ?
1503: SYSLOG_LEVEL_INFO : options.log_level,
1504: options.log_facility == SYSLOG_FACILITY_NOT_SET ?
1505: SYSLOG_FACILITY_AUTH : options.log_facility,
1.261 markus 1506: log_stderr || !inetd_flag);
1.75 markus 1507:
1.294 djm 1508: sensitive_data.server_key = NULL;
1509: sensitive_data.ssh1_host_key = NULL;
1510: sensitive_data.have_ssh1_key = 0;
1511: sensitive_data.have_ssh2_key = 0;
1512:
1.358 dtucker 1513: /*
1514: * If we're doing an extended config test, make sure we have all of
1515: * the parameters we need. If we're not doing an extended test,
1516: * do not silently ignore connection test params.
1517: */
1.391 dtucker 1518: if (test_flag >= 2 && server_match_spec_complete(connection_info) == 0)
1.358 dtucker 1519: fatal("user, host and addr are all required when testing "
1520: "Match configs");
1.391 dtucker 1521: if (test_flag < 2 && server_match_spec_complete(connection_info) >= 0)
1.358 dtucker 1522: fatal("Config test connection parameter (-C) provided without "
1523: "test mode (-T)");
1524:
1.294 djm 1525: /* Fetch our configuration */
1526: buffer_init(&cfg);
1527: if (rexeced_flag)
1.296 djm 1528: recv_rexec_state(REEXEC_CONFIG_PASS_FD, &cfg);
1.294 djm 1529: else
1530: load_server_config(config_file_name, &cfg);
1531:
1.337 dtucker 1532: parse_server_config(&options, rexeced_flag ? "rexec" : config_file_name,
1.391 dtucker 1533: &cfg, NULL);
1.64 markus 1534:
1535: /* Fill in default values for those options not explicitly set. */
1536: fill_default_server_options(&options);
1.350 dtucker 1537:
1538: /* challenge-response is implemented via keyboard interactive */
1539: if (options.challenge_response_authentication)
1540: options.kbd_interactive_authentication = 1;
1.395 djm 1541:
1542: /* Check that options are sensible */
1543: if (options.authorized_keys_command_user == NULL &&
1544: (options.authorized_keys_command != NULL &&
1545: strcasecmp(options.authorized_keys_command, "none") != 0))
1546: fatal("AuthorizedKeysCommand set without "
1547: "AuthorizedKeysCommandUser");
1.396 djm 1548:
1549: /*
1550: * Check whether there is any path through configured auth methods.
1551: * Unfortunately it is not possible to verify this generally before
1552: * daemonisation in the presence of Match block, but this catches
1553: * and warns for trivial misconfigurations that could break login.
1554: */
1555: if (options.num_auth_methods != 0) {
1556: if ((options.protocol & SSH_PROTO_1))
1557: fatal("AuthenticationMethods is not supported with "
1558: "SSH protocol 1");
1559: for (n = 0; n < options.num_auth_methods; n++) {
1560: if (auth2_methods_valid(options.auth_methods[n],
1561: 1) == 0)
1562: break;
1563: }
1564: if (n >= options.num_auth_methods)
1565: fatal("AuthenticationMethods cannot be satisfied by "
1566: "enabled authentication methods");
1567: }
1.305 djm 1568:
1.370 dtucker 1569: /* set default channel AF */
1.305 djm 1570: channel_set_af(options.address_family);
1.64 markus 1571:
1572: /* Check that there are no remaining arguments. */
1573: if (optind < ac) {
1574: fprintf(stderr, "Extra argument %s.\n", av[optind]);
1575: exit(1);
1576: }
1577:
1.397 dtucker 1578: debug("sshd version %s, %s", SSH_VERSION,
1579: SSLeay_version(SSLEAY_VERSION));
1.64 markus 1580:
1.404 markus 1581: /* load host keys */
1.329 djm 1582: sensitive_data.host_keys = xcalloc(options.num_host_key_files,
1.255 deraadt 1583: sizeof(Key *));
1.404 markus 1584: sensitive_data.host_pubkeys = xcalloc(options.num_host_key_files,
1585: sizeof(Key *));
1586: for (i = 0; i < options.num_host_key_files; i++) {
1.141 markus 1587: sensitive_data.host_keys[i] = NULL;
1.404 markus 1588: sensitive_data.host_pubkeys[i] = NULL;
1589: }
1590:
1591: if (options.host_key_agent) {
1592: if (strcmp(options.host_key_agent, SSH_AUTHSOCKET_ENV_NAME))
1593: setenv(SSH_AUTHSOCKET_ENV_NAME,
1594: options.host_key_agent, 1);
1595: have_agent = ssh_agent_present();
1596: }
1.134 markus 1597:
1.217 deraadt 1598: for (i = 0; i < options.num_host_key_files; i++) {
1.179 markus 1599: key = key_load_private(options.host_key_files[i], "", NULL);
1.404 markus 1600: pubkey = key_load_public(options.host_key_files[i], NULL);
1.179 markus 1601: sensitive_data.host_keys[i] = key;
1.404 markus 1602: sensitive_data.host_pubkeys[i] = pubkey;
1603:
1604: if (key == NULL && pubkey != NULL && pubkey->type != KEY_RSA1 &&
1605: have_agent) {
1606: debug("will rely on agent for hostkey %s",
1607: options.host_key_files[i]);
1608: keytype = pubkey->type;
1609: } else if (key != NULL) {
1610: keytype = key->type;
1611: } else {
1.195 markus 1612: error("Could not load host key: %s",
1613: options.host_key_files[i]);
1.179 markus 1614: sensitive_data.host_keys[i] = NULL;
1.404 markus 1615: sensitive_data.host_pubkeys[i] = NULL;
1.134 markus 1616: continue;
1617: }
1.404 markus 1618:
1619: switch (keytype) {
1.134 markus 1620: case KEY_RSA1:
1621: sensitive_data.ssh1_host_key = key;
1622: sensitive_data.have_ssh1_key = 1;
1623: break;
1624: case KEY_RSA:
1625: case KEY_DSA:
1.378 djm 1626: case KEY_ECDSA:
1.412 markus 1627: case KEY_ED25519:
1.134 markus 1628: sensitive_data.have_ssh2_key = 1;
1629: break;
1630: }
1.404 markus 1631: debug("private host key: #%d type %d %s", i, keytype,
1632: key_type(key ? key : pubkey));
1.134 markus 1633: }
1634: if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
1.264 itojun 1635: logit("Disabling protocol version 1. Could not load host key");
1.108 markus 1636: options.protocol &= ~SSH_PROTO_1;
1637: }
1.134 markus 1638: if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
1.264 itojun 1639: logit("Disabling protocol version 2. Could not load host key");
1.134 markus 1640: options.protocol &= ~SSH_PROTO_2;
1.108 markus 1641: }
1.162 stevesk 1642: if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
1.264 itojun 1643: logit("sshd: no hostkeys available -- exiting.");
1.64 markus 1644: exit(1);
1645: }
1646:
1.373 djm 1647: /*
1648: * Load certificates. They are stored in an array at identical
1649: * indices to the public keys that they relate to.
1650: */
1651: sensitive_data.host_certificates = xcalloc(options.num_host_key_files,
1652: sizeof(Key *));
1653: for (i = 0; i < options.num_host_key_files; i++)
1654: sensitive_data.host_certificates[i] = NULL;
1655:
1656: for (i = 0; i < options.num_host_cert_files; i++) {
1657: key = key_load_public(options.host_cert_files[i], NULL);
1658: if (key == NULL) {
1659: error("Could not load host certificate: %s",
1660: options.host_cert_files[i]);
1661: continue;
1662: }
1663: if (!key_is_cert(key)) {
1664: error("Certificate file is not a certificate: %s",
1665: options.host_cert_files[i]);
1666: key_free(key);
1667: continue;
1668: }
1669: /* Find matching private key */
1670: for (j = 0; j < options.num_host_key_files; j++) {
1671: if (key_equal_public(key,
1672: sensitive_data.host_keys[j])) {
1673: sensitive_data.host_certificates[j] = key;
1674: break;
1675: }
1676: }
1677: if (j >= options.num_host_key_files) {
1678: error("No matching private key for certificate: %s",
1679: options.host_cert_files[i]);
1680: key_free(key);
1681: continue;
1682: }
1683: sensitive_data.host_certificates[j] = key;
1684: debug("host certificate: #%d type %d %s", j, key->type,
1685: key_type(key));
1686: }
1.108 markus 1687: /* Check certain values for sanity. */
1688: if (options.protocol & SSH_PROTO_1) {
1689: if (options.server_key_bits < 512 ||
1690: options.server_key_bits > 32768) {
1691: fprintf(stderr, "Bad server key size.\n");
1692: exit(1);
1693: }
1694: /*
1695: * Check that server and host key lengths differ sufficiently. This
1696: * is necessary to make double encryption work with rsaref. Oh, I
1697: * hate software patents. I dont know if this can go? Niels
1698: */
1699: if (options.server_key_bits >
1.250 deraadt 1700: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) -
1701: SSH_KEY_BITS_RESERVED && options.server_key_bits <
1702: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1703: SSH_KEY_BITS_RESERVED) {
1.108 markus 1704: options.server_key_bits =
1.250 deraadt 1705: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
1706: SSH_KEY_BITS_RESERVED;
1.108 markus 1707: debug("Forcing server key to %d bits to make it differ from host key.",
1708: options.server_key_bits);
1709: }
1.244 markus 1710: }
1711:
1712: if (use_privsep) {
1713: struct stat st;
1714:
1.327 deraadt 1715: if (getpwnam(SSH_PRIVSEP_USER) == NULL)
1.244 markus 1716: fatal("Privilege separation user %s does not exist",
1717: SSH_PRIVSEP_USER);
1718: if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
1719: (S_ISDIR(st.st_mode) == 0))
1720: fatal("Missing privilege separation directory: %s",
1.247 stevesk 1721: _PATH_PRIVSEP_CHROOT_DIR);
1722: if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
1.262 markus 1723: fatal("%s must be owned by root and not group or "
1724: "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
1.358 dtucker 1725: }
1726:
1727: if (test_flag > 1) {
1.391 dtucker 1728: if (server_match_spec_complete(connection_info) == 1)
1729: parse_server_match_config(&options, connection_info);
1.358 dtucker 1730: dump_config(&options);
1.108 markus 1731: }
1.203 stevesk 1732:
1733: /* Configuration looks good, so exit if in test mode. */
1734: if (test_flag)
1735: exit(0);
1.108 markus 1736:
1.294 djm 1737: if (rexec_flag) {
1.329 djm 1738: rexec_argv = xcalloc(rexec_argc + 2, sizeof(char *));
1.294 djm 1739: for (i = 0; i < rexec_argc; i++) {
1740: debug("rexec_argv[%d]='%s'", i, saved_argv[i]);
1741: rexec_argv[i] = saved_argv[i];
1742: }
1743: rexec_argv[rexec_argc] = "-R";
1744: rexec_argv[rexec_argc + 1] = NULL;
1745: }
1.362 dtucker 1746:
1747: /* Ensure that umask disallows at least group and world write */
1748: new_umask = umask(0077) | 0022;
1749: (void) umask(new_umask);
1.294 djm 1750:
1.108 markus 1751: /* Initialize the log (it is reinitialized below in case we forked). */
1.306 dtucker 1752: if (debug_flag && (!inetd_flag || rexeced_flag))
1.64 markus 1753: log_stderr = 1;
1.138 markus 1754: log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64 markus 1755:
1.108 markus 1756: /*
1757: * If not in debugging mode, and not started from inetd, disconnect
1758: * from the controlling terminal, and fork. The original process
1759: * exits.
1760: */
1.135 markus 1761: if (!(debug_flag || inetd_flag || no_daemon_flag)) {
1.64 markus 1762: int fd;
1.345 djm 1763:
1.64 markus 1764: if (daemon(0, 0) < 0)
1765: fatal("daemon() failed: %.200s", strerror(errno));
1766:
1767: /* Disconnect from the controlling tty. */
1.165 itojun 1768: fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
1.64 markus 1769: if (fd >= 0) {
1770: (void) ioctl(fd, TIOCNOTTY, NULL);
1771: close(fd);
1772: }
1773: }
1774: /* Reinitialize the log (because of the fork above). */
1.138 markus 1775: log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64 markus 1776:
1777: /* Chdir to the root directory so that the current disk can be
1778: unmounted if desired. */
1.401 dtucker 1779: if (chdir("/") == -1)
1780: error("chdir(\"/\"): %s", strerror(errno));
1.217 deraadt 1781:
1.178 markus 1782: /* ignore SIGPIPE */
1783: signal(SIGPIPE, SIG_IGN);
1.64 markus 1784:
1.345 djm 1785: /* Get a connection, either from inetd or a listening TCP socket */
1.64 markus 1786: if (inetd_flag) {
1.345 djm 1787: server_accept_inetd(&sock_in, &sock_out);
1.64 markus 1788: } else {
1.345 djm 1789: server_listen();
1.75 markus 1790:
1.201 markus 1791: if (options.protocol & SSH_PROTO_1)
1792: generate_ephemeral_server_key();
1793:
1794: signal(SIGHUP, sighup_handler);
1.345 djm 1795: signal(SIGCHLD, main_sigchld_handler);
1.201 markus 1796: signal(SIGTERM, sigterm_handler);
1797: signal(SIGQUIT, sigterm_handler);
1798:
1.345 djm 1799: /*
1800: * Write out the pid file after the sigterm handler
1801: * is setup and the listen sockets are bound
1802: */
1803: if (!debug_flag) {
1804: FILE *f = fopen(options.pid_file, "w");
1.201 markus 1805:
1.270 djm 1806: if (f == NULL) {
1807: error("Couldn't create pid file \"%s\": %s",
1808: options.pid_file, strerror(errno));
1809: } else {
1.245 mpech 1810: fprintf(f, "%ld\n", (long) getpid());
1.64 markus 1811: fclose(f);
1812: }
1813: }
1814:
1.345 djm 1815: /* Accept a connection and return in a forked child */
1816: server_accept_loop(&sock_in, &sock_out,
1817: &newsock, config_s);
1.1 deraadt 1818: }
1819:
1.64 markus 1820: /* This is the child processing a new connection. */
1.288 markus 1821: setproctitle("%s", "[accepted]");
1.294 djm 1822:
1.300 markus 1823: /*
1824: * Create a new session and process group since the 4.4BSD
1825: * setlogin() affects the entire process group. We don't
1826: * want the child to be able to affect the parent.
1827: */
1828: if (!debug_flag && !inetd_flag && setsid() < 0)
1829: error("setsid: %.100s", strerror(errno));
1830:
1.294 djm 1831: if (rexec_flag) {
1832: int fd;
1833:
1.296 djm 1834: debug("rexec start in %d out %d newsock %d pipe %d sock %d",
1835: sock_in, sock_out, newsock, startup_pipe, config_s[0]);
1.294 djm 1836: dup2(newsock, STDIN_FILENO);
1837: dup2(STDIN_FILENO, STDOUT_FILENO);
1838: if (startup_pipe == -1)
1.296 djm 1839: close(REEXEC_STARTUP_PIPE_FD);
1.407 djm 1840: else if (startup_pipe != REEXEC_STARTUP_PIPE_FD) {
1.296 djm 1841: dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD);
1.407 djm 1842: close(startup_pipe);
1843: startup_pipe = REEXEC_STARTUP_PIPE_FD;
1844: }
1.294 djm 1845:
1.296 djm 1846: dup2(config_s[1], REEXEC_CONFIG_PASS_FD);
1.294 djm 1847: close(config_s[1]);
1.296 djm 1848:
1.294 djm 1849: execv(rexec_argv[0], rexec_argv);
1850:
1851: /* Reexec has failed, fall back and continue */
1852: error("rexec of %s failed: %s", rexec_argv[0], strerror(errno));
1.296 djm 1853: recv_rexec_state(REEXEC_CONFIG_PASS_FD, NULL);
1.294 djm 1854: log_init(__progname, options.log_level,
1855: options.log_facility, log_stderr);
1856:
1857: /* Clean up fds */
1.296 djm 1858: close(REEXEC_CONFIG_PASS_FD);
1859: newsock = sock_out = sock_in = dup(STDIN_FILENO);
1.294 djm 1860: if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1861: dup2(fd, STDIN_FILENO);
1862: dup2(fd, STDOUT_FILENO);
1863: if (fd > STDERR_FILENO)
1864: close(fd);
1865: }
1.296 djm 1866: debug("rexec cleanup in %d out %d newsock %d pipe %d sock %d",
1867: sock_in, sock_out, newsock, startup_pipe, config_s[0]);
1.294 djm 1868: }
1.372 djm 1869:
1870: /* Executed child processes don't need these. */
1871: fcntl(sock_out, F_SETFD, FD_CLOEXEC);
1872: fcntl(sock_in, F_SETFD, FD_CLOEXEC);
1.64 markus 1873:
1.66 markus 1874: /*
1875: * Disable the key regeneration alarm. We will not regenerate the
1876: * key since we are no longer in a position to give it to anyone. We
1877: * will not restart on SIGHUP since it no longer makes sense.
1878: */
1.64 markus 1879: alarm(0);
1880: signal(SIGALRM, SIG_DFL);
1881: signal(SIGHUP, SIG_DFL);
1882: signal(SIGTERM, SIG_DFL);
1883: signal(SIGQUIT, SIG_DFL);
1884: signal(SIGCHLD, SIG_DFL);
1.150 markus 1885:
1.66 markus 1886: /*
1887: * Register our connection. This turns encryption off because we do
1888: * not have a key.
1889: */
1.64 markus 1890: packet_set_connection(sock_in, sock_out);
1.312 markus 1891: packet_set_server();
1.309 djm 1892:
1893: /* Set SO_KEEPALIVE if requested. */
1894: if (options.tcp_keep_alive && packet_connection_is_on_socket() &&
1895: setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0)
1896: error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1.1 deraadt 1897:
1.310 markus 1898: if ((remote_port = get_remote_port()) < 0) {
1899: debug("get_remote_port failed");
1900: cleanup_exit(255);
1901: }
1.316 dtucker 1902:
1903: /*
1904: * We use get_canonical_hostname with usedns = 0 instead of
1905: * get_remote_ipaddr here so IP options will be checked.
1906: */
1.331 markus 1907: (void) get_canonical_hostname(0);
1908: /*
1909: * The rest of the code depends on the fact that
1910: * get_remote_ipaddr() caches the remote ip, even if
1911: * the socket goes away.
1912: */
1913: remote_ip = get_remote_ipaddr();
1.209 markus 1914:
1.64 markus 1915: /* Log the connection. */
1.409 djm 1916: verbose("Connection from %s port %d on %s port %d",
1917: remote_ip, remote_port,
1918: get_local_ipaddr(sock_in), get_local_port());
1.1 deraadt 1919:
1.66 markus 1920: /*
1.317 djm 1921: * We don't want to listen forever unless the other side
1.66 markus 1922: * successfully authenticates itself. So we set up an alarm which is
1923: * cleared after successful authentication. A limit of zero
1.317 djm 1924: * indicates no limit. Note that we don't set the alarm in debugging
1.66 markus 1925: * mode; it is just annoying to have the server exit just when you
1926: * are about to discover the bug.
1927: */
1.64 markus 1928: signal(SIGALRM, grace_alarm_handler);
1929: if (!debug_flag)
1930: alarm(options.login_grace_time);
1931:
1.96 markus 1932: sshd_exchange_identification(sock_in, sock_out);
1.353 dtucker 1933:
1934: /* In inetd mode, generate ephemeral key only for proto 1 connections */
1935: if (!compat20 && inetd_flag && sensitive_data.server_key == NULL)
1936: generate_ephemeral_server_key();
1.275 markus 1937:
1.64 markus 1938: packet_set_nonblocking();
1.1 deraadt 1939:
1.278 markus 1940: /* allocate authentication context */
1.329 djm 1941: authctxt = xcalloc(1, sizeof(*authctxt));
1.278 markus 1942:
1943: /* XXX global for cleanup, access from other modules */
1944: the_authctxt = authctxt;
1945:
1.307 otto 1946: /* prepare buffer to collect messages to display to user after login */
1947: buffer_init(&loginmsg);
1.374 dtucker 1948: auth_debug_reset();
1.307 otto 1949:
1.404 markus 1950: if (use_privsep) {
1.278 markus 1951: if (privsep_preauth(authctxt) == 1)
1.237 markus 1952: goto authenticated;
1.404 markus 1953: } else if (compat20 && have_agent)
1954: auth_conn = ssh_get_authentication_connection();
1.231 provos 1955:
1.77 markus 1956: /* perform the key exchange */
1957: /* authenticate user and start session */
1.98 markus 1958: if (compat20) {
1959: do_ssh2_kex();
1.278 markus 1960: do_authentication2(authctxt);
1.98 markus 1961: } else {
1962: do_ssh1_kex();
1.278 markus 1963: do_authentication(authctxt);
1.98 markus 1964: }
1.237 markus 1965: /*
1966: * If we use privilege separation, the unprivileged child transfers
1967: * the current keystate and exits
1968: */
1969: if (use_privsep) {
1.242 mouring 1970: mm_send_keystate(pmonitor);
1.231 provos 1971: exit(0);
1.237 markus 1972: }
1.231 provos 1973:
1974: authenticated:
1.318 djm 1975: /*
1976: * Cancel the alarm we set to limit the time taken for
1977: * authentication.
1978: */
1979: alarm(0);
1980: signal(SIGALRM, SIG_DFL);
1.347 markus 1981: authctxt->authenticated = 1;
1.318 djm 1982: if (startup_pipe != -1) {
1983: close(startup_pipe);
1984: startup_pipe = -1;
1985: }
1986:
1.234 markus 1987: /*
1.231 provos 1988: * In privilege separation, we fork another child and prepare
1989: * file descriptor passing.
1990: */
1991: if (use_privsep) {
1.237 markus 1992: privsep_postauth(authctxt);
1993: /* the monitor process [priv] will not return */
1.231 provos 1994: if (!compat20)
1995: destroy_sensitive_data();
1996: }
1.360 dtucker 1997:
1998: packet_set_timeout(options.client_alive_interval,
1999: options.client_alive_count_max);
1.230 provos 2000:
1.278 markus 2001: /* Start session. */
1.230 provos 2002: do_authenticated(authctxt);
2003:
1.64 markus 2004: /* The connection has been terminated. */
1.364 markus 2005: packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
2006: packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
1.381 djm 2007: verbose("Transferred: sent %llu, received %llu bytes",
2008: (unsigned long long)obytes, (unsigned long long)ibytes);
1.364 markus 2009:
2010: verbose("Closing connection to %.500s port %d", remote_ip, remote_port);
1.64 markus 2011: packet_close();
1.231 provos 2012:
2013: if (use_privsep)
2014: mm_terminate();
2015:
1.64 markus 2016: exit(0);
1.1 deraadt 2017: }
2018:
1.65 deraadt 2019: /*
1.229 markus 2020: * Decrypt session_key_int using our private server key and private host key
2021: * (key with larger modulus first).
2022: */
1.231 provos 2023: int
1.229 markus 2024: ssh1_session_key(BIGNUM *session_key_int)
2025: {
2026: int rsafail = 0;
2027:
1.327 deraadt 2028: if (BN_cmp(sensitive_data.server_key->rsa->n,
2029: sensitive_data.ssh1_host_key->rsa->n) > 0) {
1.229 markus 2030: /* Server key has bigger modulus. */
2031: if (BN_num_bits(sensitive_data.server_key->rsa->n) <
1.327 deraadt 2032: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
2033: SSH_KEY_BITS_RESERVED) {
2034: fatal("do_connection: %s: "
2035: "server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
1.229 markus 2036: get_remote_ipaddr(),
2037: BN_num_bits(sensitive_data.server_key->rsa->n),
2038: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
2039: SSH_KEY_BITS_RESERVED);
2040: }
2041: if (rsa_private_decrypt(session_key_int, session_key_int,
2042: sensitive_data.server_key->rsa) <= 0)
2043: rsafail++;
2044: if (rsa_private_decrypt(session_key_int, session_key_int,
2045: sensitive_data.ssh1_host_key->rsa) <= 0)
2046: rsafail++;
2047: } else {
2048: /* Host key has bigger modulus (or they are equal). */
2049: if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
1.327 deraadt 2050: BN_num_bits(sensitive_data.server_key->rsa->n) +
2051: SSH_KEY_BITS_RESERVED) {
2052: fatal("do_connection: %s: "
2053: "host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
1.229 markus 2054: get_remote_ipaddr(),
2055: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
2056: BN_num_bits(sensitive_data.server_key->rsa->n),
2057: SSH_KEY_BITS_RESERVED);
2058: }
2059: if (rsa_private_decrypt(session_key_int, session_key_int,
2060: sensitive_data.ssh1_host_key->rsa) < 0)
2061: rsafail++;
2062: if (rsa_private_decrypt(session_key_int, session_key_int,
2063: sensitive_data.server_key->rsa) < 0)
2064: rsafail++;
2065: }
2066: return (rsafail);
2067: }
2068: /*
1.77 markus 2069: * SSH1 key exchange
1.65 deraadt 2070: */
1.200 itojun 2071: static void
1.142 markus 2072: do_ssh1_kex(void)
1.1 deraadt 2073: {
1.64 markus 2074: int i, len;
1.159 markus 2075: int rsafail = 0;
1.64 markus 2076: BIGNUM *session_key_int;
1.140 markus 2077: u_char session_key[SSH_SESSION_KEY_LENGTH];
2078: u_char cookie[8];
2079: u_int cipher_type, auth_mask, protocol_flags;
1.64 markus 2080:
1.66 markus 2081: /*
2082: * Generate check bytes that the client must send back in the user
2083: * packet in order for it to be accepted; this is used to defy ip
2084: * spoofing attacks. Note that this only works against somebody
2085: * doing IP spoofing from a remote machine; any machine on the local
2086: * network can still see outgoing packets and catch the random
2087: * cookie. This only affects rhosts authentication, and this is one
2088: * of the reasons why it is inherently insecure.
2089: */
1.356 djm 2090: arc4random_buf(cookie, sizeof(cookie));
1.64 markus 2091:
1.66 markus 2092: /*
2093: * Send our public key. We include in the packet 64 bits of random
2094: * data that must be matched in the reply in order to prevent IP
2095: * spoofing.
2096: */
1.64 markus 2097: packet_start(SSH_SMSG_PUBLIC_KEY);
2098: for (i = 0; i < 8; i++)
1.77 markus 2099: packet_put_char(cookie[i]);
1.64 markus 2100:
2101: /* Store our public server RSA key. */
1.134 markus 2102: packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
2103: packet_put_bignum(sensitive_data.server_key->rsa->e);
2104: packet_put_bignum(sensitive_data.server_key->rsa->n);
1.64 markus 2105:
2106: /* Store our public host RSA key. */
1.134 markus 2107: packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
2108: packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
2109: packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
1.64 markus 2110:
2111: /* Put protocol flags. */
2112: packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
2113:
2114: /* Declare which ciphers we support. */
1.131 markus 2115: packet_put_int(cipher_mask_ssh1(0));
1.64 markus 2116:
2117: /* Declare supported authentication types. */
2118: auth_mask = 0;
2119: if (options.rhosts_rsa_authentication)
2120: auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
2121: if (options.rsa_authentication)
2122: auth_mask |= 1 << SSH_AUTH_RSA;
1.196 markus 2123: if (options.challenge_response_authentication == 1)
1.64 markus 2124: auth_mask |= 1 << SSH_AUTH_TIS;
2125: if (options.password_authentication)
2126: auth_mask |= 1 << SSH_AUTH_PASSWORD;
2127: packet_put_int(auth_mask);
2128:
2129: /* Send the packet and wait for it to be sent. */
2130: packet_send();
2131: packet_write_wait();
2132:
1.134 markus 2133: debug("Sent %d bit server key and %d bit host key.",
2134: BN_num_bits(sensitive_data.server_key->rsa->n),
2135: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1.64 markus 2136:
2137: /* Read clients reply (cipher type and session key). */
1.222 markus 2138: packet_read_expect(SSH_CMSG_SESSION_KEY);
1.64 markus 2139:
1.69 markus 2140: /* Get cipher type and check whether we accept this. */
1.64 markus 2141: cipher_type = packet_get_char();
1.69 markus 2142:
1.131 markus 2143: if (!(cipher_mask_ssh1(0) & (1 << cipher_type)))
1.69 markus 2144: packet_disconnect("Warning: client selects unsupported cipher.");
1.64 markus 2145:
2146: /* Get check bytes from the packet. These must match those we
2147: sent earlier with the public key packet. */
2148: for (i = 0; i < 8; i++)
1.77 markus 2149: if (cookie[i] != packet_get_char())
1.64 markus 2150: packet_disconnect("IP Spoofing check bytes do not match.");
2151:
2152: debug("Encryption type: %.200s", cipher_name(cipher_type));
2153:
2154: /* Get the encrypted integer. */
1.218 markus 2155: if ((session_key_int = BN_new()) == NULL)
2156: fatal("do_ssh1_kex: BN_new failed");
1.221 markus 2157: packet_get_bignum(session_key_int);
1.64 markus 2158:
2159: protocol_flags = packet_get_int();
2160: packet_set_protocol_flags(protocol_flags);
1.220 markus 2161: packet_check_eom();
1.64 markus 2162:
1.229 markus 2163: /* Decrypt session_key_int using host/server keys */
1.231 provos 2164: rsafail = PRIVSEP(ssh1_session_key(session_key_int));
2165:
1.66 markus 2166: /*
2167: * Extract session key from the decrypted integer. The key is in the
2168: * least significant 256 bits of the integer; the first byte of the
2169: * key is in the highest bits.
2170: */
1.159 markus 2171: if (!rsafail) {
1.348 markus 2172: (void) BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1.159 markus 2173: len = BN_num_bytes(session_key_int);
1.311 djm 2174: if (len < 0 || (u_int)len > sizeof(session_key)) {
1.348 markus 2175: error("do_ssh1_kex: bad session key len from %s: "
1.165 itojun 2176: "session_key_int %d > sizeof(session_key) %lu",
2177: get_remote_ipaddr(), len, (u_long)sizeof(session_key));
1.159 markus 2178: rsafail++;
2179: } else {
1.418 djm 2180: explicit_bzero(session_key, sizeof(session_key));
1.159 markus 2181: BN_bn2bin(session_key_int,
2182: session_key + sizeof(session_key) - len);
1.169 markus 2183:
1.291 djm 2184: derive_ssh1_session_id(
1.298 deraadt 2185: sensitive_data.ssh1_host_key->rsa->n,
1.291 djm 2186: sensitive_data.server_key->rsa->n,
2187: cookie, session_id);
1.169 markus 2188: /*
2189: * Xor the first 16 bytes of the session key with the
2190: * session id.
2191: */
2192: for (i = 0; i < 16; i++)
2193: session_key[i] ^= session_id[i];
1.159 markus 2194: }
2195: }
2196: if (rsafail) {
1.169 markus 2197: int bytes = BN_num_bytes(session_key_int);
1.227 stevesk 2198: u_char *buf = xmalloc(bytes);
1.415 markus 2199: struct ssh_digest_ctx *md;
1.169 markus 2200:
1.264 itojun 2201: logit("do_connection: generating a fake encryption key");
1.169 markus 2202: BN_bn2bin(session_key_int, buf);
1.415 markus 2203: if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
2204: ssh_digest_update(md, buf, bytes) < 0 ||
2205: ssh_digest_update(md, sensitive_data.ssh1_cookie,
2206: SSH_SESSION_KEY_LENGTH) < 0 ||
2207: ssh_digest_final(md, session_key, sizeof(session_key)) < 0)
2208: fatal("%s: md5 failed", __func__);
2209: ssh_digest_free(md);
2210: if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
2211: ssh_digest_update(md, session_key, 16) < 0 ||
2212: ssh_digest_update(md, sensitive_data.ssh1_cookie,
2213: SSH_SESSION_KEY_LENGTH) < 0 ||
2214: ssh_digest_final(md, session_key + 16,
2215: sizeof(session_key) - 16) < 0)
2216: fatal("%s: md5 failed", __func__);
2217: ssh_digest_free(md);
1.418 djm 2218: explicit_bzero(buf, bytes);
1.402 djm 2219: free(buf);
1.170 markus 2220: for (i = 0; i < 16; i++)
2221: session_id[i] = session_key[i] ^ session_key[i + 16];
1.159 markus 2222: }
1.231 provos 2223: /* Destroy the private and public keys. No longer. */
1.169 markus 2224: destroy_sensitive_data();
2225:
1.231 provos 2226: if (use_privsep)
2227: mm_ssh1_session_id(session_id);
2228:
1.77 markus 2229: /* Destroy the decrypted integer. It is no longer needed. */
2230: BN_clear_free(session_key_int);
1.64 markus 2231:
2232: /* Set the session key. From this on all communications will be encrypted. */
2233: packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
2234:
2235: /* Destroy our copy of the session key. It is no longer needed. */
1.418 djm 2236: explicit_bzero(session_key, sizeof(session_key));
1.64 markus 2237:
2238: debug("Received session key; encryption turned on.");
2239:
1.243 deraadt 2240: /* Send an acknowledgment packet. Note that this packet is sent encrypted. */
1.64 markus 2241: packet_start(SSH_SMSG_SUCCESS);
2242: packet_send();
2243: packet_write_wait();
1.98 markus 2244: }
2245:
1.404 markus 2246: void
2247: sshd_hostkey_sign(Key *privkey, Key *pubkey, u_char **signature, u_int *slen,
2248: u_char *data, u_int dlen)
2249: {
2250: if (privkey) {
2251: if (PRIVSEP(key_sign(privkey, signature, slen, data, dlen) < 0))
2252: fatal("%s: key_sign failed", __func__);
2253: } else if (use_privsep) {
2254: if (mm_key_sign(pubkey, signature, slen, data, dlen) < 0)
2255: fatal("%s: pubkey_sign failed", __func__);
2256: } else {
2257: if (ssh_agent_sign(auth_conn, pubkey, signature, slen, data,
2258: dlen))
2259: fatal("%s: ssh_agent_sign failed", __func__);
2260: }
2261: }
2262:
1.98 markus 2263: /*
2264: * SSH2 key exchange: diffie-hellman-group1-sha1
2265: */
1.200 itojun 2266: static void
1.142 markus 2267: do_ssh2_kex(void)
1.98 markus 2268: {
1.422 markus 2269: char *myproposal[PROPOSAL_MAX] = { KEX_SERVER };
1.98 markus 2270: Kex *kex;
1.102 markus 2271:
2272: if (options.ciphers != NULL) {
1.105 markus 2273: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1.102 markus 2274: myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
1.166 markus 2275: }
1.184 stevesk 2276: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
2277: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
2278: myproposal[PROPOSAL_ENC_ALGS_STOC] =
2279: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
2280:
1.166 markus 2281: if (options.macs != NULL) {
2282: myproposal[PROPOSAL_MAC_ALGS_CTOS] =
2283: myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1.246 markus 2284: }
1.312 markus 2285: if (options.compression == COMP_NONE) {
1.246 markus 2286: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
2287: myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
1.312 markus 2288: } else if (options.compression == COMP_DELAYED) {
2289: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
2290: myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com";
1.102 markus 2291: }
1.380 djm 2292: if (options.kex_algorithms != NULL)
2293: myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
1.424 ! djm 2294:
! 2295: myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
! 2296: myproposal[PROPOSAL_KEX_ALGS]);
1.400 dtucker 2297:
2298: if (options.rekey_limit || options.rekey_interval)
2299: packet_set_rekey_limits((u_int32_t)options.rekey_limit,
2300: (time_t)options.rekey_interval);
1.327 deraadt 2301:
1.413 djm 2302: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal(
2303: list_hostkey_types());
1.134 markus 2304:
1.189 markus 2305: /* start key exchange */
1.188 markus 2306: kex = kex_setup(myproposal);
1.263 markus 2307: kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1.292 djm 2308: kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1.263 markus 2309: kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1.324 djm 2310: kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1.378 djm 2311: kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1.410 markus 2312: kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1.186 markus 2313: kex->server = 1;
2314: kex->client_version_string=client_version_string;
2315: kex->server_version_string=server_version_string;
1.373 djm 2316: kex->load_host_public_key=&get_hostkey_public_by_type;
2317: kex->load_host_private_key=&get_hostkey_private_by_type;
1.231 provos 2318: kex->host_key_index=&get_hostkey_index;
1.404 markus 2319: kex->sign = sshd_hostkey_sign;
1.129 provos 2320:
1.189 markus 2321: xxx_kex = kex;
2322:
1.190 markus 2323: dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
1.187 markus 2324:
2325: session_id2 = kex->session_id;
2326: session_id2_len = kex->session_id_len;
1.129 provos 2327:
2328: #ifdef DEBUG_KEXDH
2329: /* send 1st encrypted/maced/compressed message */
2330: packet_start(SSH2_MSG_IGNORE);
2331: packet_put_cstring("markus");
2332: packet_send();
2333: packet_write_wait();
2334: #endif
1.186 markus 2335: debug("KEX done");
1.278 markus 2336: }
2337:
2338: /* server specific fatal cleanup */
2339: void
2340: cleanup_exit(int i)
2341: {
1.386 djm 2342: if (the_authctxt) {
1.278 markus 2343: do_cleanup(the_authctxt);
1.423 djm 2344: if (use_privsep && privsep_is_preauth &&
2345: pmonitor != NULL && pmonitor->m_pid > 1) {
1.386 djm 2346: debug("Killing privsep child %d", pmonitor->m_pid);
2347: if (kill(pmonitor->m_pid, SIGKILL) != 0 &&
1.388 djm 2348: errno != ESRCH)
1.386 djm 2349: error("%s: kill(%d): %s", __func__,
2350: pmonitor->m_pid, strerror(errno));
2351: }
2352: }
1.278 markus 2353: _exit(i);
1.1 deraadt 2354: }