Annotation of src/usr.bin/ssh/sshd.c, Revision 1.582
1.582 ! djm 1: /* $OpenBSD: sshd.c,v 1.581 2021/11/18 03:07:20 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.579 deraadt 58: #include <poll.h>
1.333 stevesk 59: #include <pwd.h>
1.322 stevesk 60: #include <signal.h>
1.342 stevesk 61: #include <stdio.h>
1.341 stevesk 62: #include <stdlib.h>
1.339 stevesk 63: #include <string.h>
1.541 naddy 64: #include <stdarg.h>
1.344 dtucker 65: #include <unistd.h>
1.438 deraadt 66: #include <limits.h>
1.1 deraadt 67:
1.426 markus 68: #ifdef WITH_OPENSSL
1.155 markus 69: #include <openssl/bn.h>
1.426 markus 70: #endif
1.155 markus 71:
1.343 deraadt 72: #include "xmalloc.h"
1.155 markus 73: #include "ssh.h"
74: #include "ssh2.h"
1.171 djm 75: #include "sshpty.h"
1.1 deraadt 76: #include "packet.h"
1.155 markus 77: #include "log.h"
1.511 markus 78: #include "sshbuf.h"
1.428 millert 79: #include "misc.h"
1.454 markus 80: #include "match.h"
1.1 deraadt 81: #include "servconf.h"
82: #include "uidswap.h"
1.33 markus 83: #include "compat.h"
1.155 markus 84: #include "cipher.h"
1.415 markus 85: #include "digest.h"
1.512 markus 86: #include "sshkey.h"
1.98 markus 87: #include "kex.h"
88: #include "myproposal.h"
1.108 markus 89: #include "authfile.h"
1.154 markus 90: #include "pathnames.h"
1.155 markus 91: #include "atomicio.h"
92: #include "canohost.h"
1.343 deraadt 93: #include "hostfile.h"
1.155 markus 94: #include "auth.h"
1.404 markus 95: #include "authfd.h"
1.294 djm 96: #include "msg.h"
1.186 markus 97: #include "dispatch.h"
1.206 stevesk 98: #include "channels.h"
1.230 provos 99: #include "session.h"
1.231 provos 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.385 djm 105: #include "ssh-sandbox.h"
1.506 djm 106: #include "auth-options.h"
1.332 stevesk 107: #include "version.h"
1.432 djm 108: #include "ssherr.h"
1.542 djm 109: #include "sk-api.h"
1.567 dtucker 110: #include "srclimit.h"
1.571 dtucker 111: #include "dh.h"
1.522 djm 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.499 djm 135: /*
136: * Indicating that the daemon should only test the configuration and keys.
137: * If test_flag > 1 ("-T" flag), then sshd will also dump the effective
138: * configuration, optionally using connection information provided by the
139: * "-C" flag.
140: */
1.521 djm 141: static int test_flag = 0;
1.203 stevesk 142:
1.1 deraadt 143: /* Flag indicating that the daemon is being started from inetd. */
1.521 djm 144: static int inetd_flag = 0;
1.1 deraadt 145:
1.135 markus 146: /* Flag indicating that sshd should not detach and become a daemon. */
1.521 djm 147: static int no_daemon_flag = 0;
1.135 markus 148:
1.47 markus 149: /* debug goes to stderr unless inetd_flag is set */
1.521 djm 150: static int log_stderr = 0;
1.47 markus 151:
1.1 deraadt 152: /* Saved arguments to main(). */
1.521 djm 153: static char **saved_argv;
1.1 deraadt 154:
1.294 djm 155: /* re-exec */
1.521 djm 156: static int rexeced_flag = 0;
157: static int rexec_flag = 1;
158: static int rexec_argc = 0;
159: static char **rexec_argv;
1.294 djm 160:
1.66 markus 161: /*
1.75 markus 162: * The sockets that the server is listening; this is used in the SIGHUP
163: * signal handler.
1.66 markus 164: */
1.75 markus 165: #define MAX_LISTEN_SOCKS 16
1.521 djm 166: static int listen_socks[MAX_LISTEN_SOCKS];
167: static int num_listen_socks = 0;
1.1 deraadt 168:
1.404 markus 169: /* Daemon's agent connection */
1.432 djm 170: int auth_sock = -1;
1.521 djm 171: static int have_agent = 0;
1.404 markus 172:
1.66 markus 173: /*
174: * Any really sensitive data in the application is contained in this
175: * structure. The idea is that this structure could be locked into memory so
176: * that the pages do not get written into swap. However, there are some
177: * problems. The private key contains BIGNUMs, and we do not (in principle)
178: * have access to the internals of them, and locking just the structure is
179: * not very useful. Currently, memory locking is not implemented.
180: */
1.64 markus 181: struct {
1.488 markus 182: struct sshkey **host_keys; /* all private host keys */
183: struct sshkey **host_pubkeys; /* all public host keys */
184: struct sshkey **host_certificates; /* all public host certificates */
185: int have_ssh2_key;
1.1 deraadt 186: } sensitive_data;
187:
1.199 markus 188: /* This is set to true when a signal is received. */
1.212 markus 189: static volatile sig_atomic_t received_sighup = 0;
190: static volatile sig_atomic_t received_sigterm = 0;
1.1 deraadt 191:
1.125 markus 192: /* record remote hostname or ip */
1.438 deraadt 193: u_int utmp_len = HOST_NAME_MAX+1;
1.125 markus 194:
1.533 djm 195: /*
196: * startup_pipes/flags are used for tracking children of the listening sshd
197: * process early in their lifespans. This tracking is needed for three things:
198: *
199: * 1) Implementing the MaxStartups limit of concurrent unauthenticated
200: * connections.
201: * 2) Avoiding a race condition for SIGHUP processing, where child processes
202: * may have listen_socks open that could collide with main listener process
203: * after it restarts.
204: * 3) Ensuring that rexec'd sshd processes have received their initial state
205: * from the parent listen process before handling SIGHUP.
206: *
207: * Child processes signal that they have completed closure of the listen_socks
208: * and (if applicable) received their rexec state by sending a char over their
209: * sock. Child processes signal that authentication has completed by closing
210: * the sock (or by exiting).
211: */
1.521 djm 212: static int *startup_pipes = NULL;
1.533 djm 213: static int *startup_flags = NULL; /* Indicates child closed listener */
214: static int startup_pipe = -1; /* in child */
1.211 markus 215:
1.231 provos 216: /* variables used for privilege separation */
1.337 dtucker 217: int use_privsep = -1;
1.285 dtucker 218: struct monitor *pmonitor = NULL;
1.386 djm 219: int privsep_is_preauth = 1;
1.231 provos 220:
1.527 djm 221: /* global connection state and authentication contexts */
1.278 markus 222: Authctxt *the_authctxt = NULL;
1.527 djm 223: struct ssh *the_active_state;
1.278 markus 224:
1.506 djm 225: /* global key/cert auth options. XXX move to permanent ssh->authctxt? */
226: struct sshauthopt *auth_opts = NULL;
227:
1.337 dtucker 228: /* sshd_config buffer */
1.511 markus 229: struct sshbuf *cfg;
1.337 dtucker 230:
1.546 djm 231: /* Included files from the configuration file */
232: struct include_list includes = TAILQ_HEAD_INITIALIZER(includes);
233:
1.299 dtucker 234: /* message to be displayed after login */
1.510 markus 235: struct sshbuf *loginmsg;
1.299 dtucker 236:
1.1 deraadt 237: /* Prototypes for various functions defined later in this file. */
1.200 itojun 238: void destroy_sensitive_data(void);
1.231 provos 239: void demote_sensitive_data(void);
1.525 djm 240: static void do_ssh2_kex(struct ssh *);
1.129 provos 241:
1.545 djm 242: static char *listener_proctitle;
243:
1.87 markus 244: /*
1.75 markus 245: * Close all listening sockets
246: */
1.200 itojun 247: static void
1.75 markus 248: close_listen_socks(void)
249: {
250: int i;
1.250 deraadt 251:
1.75 markus 252: for (i = 0; i < num_listen_socks; i++)
253: close(listen_socks[i]);
1.580 djm 254: num_listen_socks = 0;
1.75 markus 255: }
256:
1.211 markus 257: static void
258: close_startup_pipes(void)
259: {
260: int i;
1.250 deraadt 261:
1.211 markus 262: if (startup_pipes)
263: for (i = 0; i < options.max_startups; i++)
264: if (startup_pipes[i] != -1)
265: close(startup_pipes[i]);
266: }
267:
1.75 markus 268: /*
1.65 deraadt 269: * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
270: * the effect is to reread the configuration file (and to regenerate
271: * the server key).
272: */
1.327 deraadt 273:
274: /*ARGSUSED*/
1.200 itojun 275: static void
1.64 markus 276: sighup_handler(int sig)
1.1 deraadt 277: {
1.64 markus 278: received_sighup = 1;
1.1 deraadt 279: }
280:
1.65 deraadt 281: /*
282: * Called from the main program after receiving SIGHUP.
283: * Restarts the server.
284: */
1.200 itojun 285: static void
1.165 itojun 286: sighup_restart(void)
1.1 deraadt 287: {
1.264 itojun 288: logit("Received SIGHUP; restarting.");
1.479 dtucker 289: if (options.pid_file != NULL)
290: unlink(options.pid_file);
1.75 markus 291: close_listen_socks();
1.211 markus 292: close_startup_pipes();
1.544 dtucker 293: ssh_signal(SIGHUP, SIG_IGN); /* will be restored after exec */
1.64 markus 294: execv(saved_argv[0], saved_argv);
1.264 itojun 295: logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
1.250 deraadt 296: strerror(errno));
1.64 markus 297: exit(1);
1.1 deraadt 298: }
299:
1.65 deraadt 300: /*
301: * Generic signal handler for terminating signals in the master daemon.
302: */
1.327 deraadt 303: /*ARGSUSED*/
1.200 itojun 304: static void
1.64 markus 305: sigterm_handler(int sig)
1.1 deraadt 306: {
1.199 markus 307: received_sigterm = sig;
1.1 deraadt 308: }
309:
1.65 deraadt 310: /*
311: * SIGCHLD handler. This is called whenever a child dies. This will then
1.199 markus 312: * reap any zombies left by exited children.
1.65 deraadt 313: */
1.327 deraadt 314: /*ARGSUSED*/
1.200 itojun 315: static void
1.64 markus 316: main_sigchld_handler(int sig)
1.1 deraadt 317: {
1.250 deraadt 318: int save_errno = errno;
1.239 markus 319: pid_t pid;
1.64 markus 320: int status;
1.544 dtucker 321:
1.239 markus 322: while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
1.537 deraadt 323: (pid == -1 && errno == EINTR))
1.64 markus 324: ;
325: errno = save_errno;
1.1 deraadt 326: }
327:
1.65 deraadt 328: /*
329: * Signal handler for the alarm after the login grace period has expired.
330: */
1.327 deraadt 331: /*ARGSUSED*/
1.200 itojun 332: static void
1.64 markus 333: grace_alarm_handler(int sig)
1.1 deraadt 334: {
1.285 dtucker 335: if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0)
336: kill(pmonitor->m_pid, SIGALRM);
1.394 djm 337:
338: /*
339: * Try to kill any processes that we have spawned, E.g. authorized
340: * keys command helpers.
341: */
342: if (getpgid(0) == getpid()) {
1.544 dtucker 343: ssh_signal(SIGTERM, SIG_IGN);
1.416 djm 344: kill(0, SIGTERM);
1.394 djm 345: }
1.285 dtucker 346:
1.64 markus 347: /* Log error and exit. */
1.573 djm 348: if (use_privsep && pmonitor != NULL && pmonitor->m_pid <= 0)
349: cleanup_exit(255); /* don't log in privsep child */
350: else {
351: sigdie("Timeout before authentication for %s port %d",
352: ssh_remote_ipaddr(the_active_state),
353: ssh_remote_port(the_active_state));
354: }
1.62 markus 355: }
356:
1.134 markus 357: /* Destroy the host and server keys. They will no longer be needed. */
1.108 markus 358: void
359: destroy_sensitive_data(void)
360: {
1.493 djm 361: u_int i;
1.134 markus 362:
1.217 deraadt 363: for (i = 0; i < options.num_host_key_files; i++) {
1.134 markus 364: if (sensitive_data.host_keys[i]) {
1.512 markus 365: sshkey_free(sensitive_data.host_keys[i]);
1.134 markus 366: sensitive_data.host_keys[i] = NULL;
367: }
1.373 djm 368: if (sensitive_data.host_certificates[i]) {
1.512 markus 369: sshkey_free(sensitive_data.host_certificates[i]);
1.373 djm 370: sensitive_data.host_certificates[i] = NULL;
371: }
1.134 markus 372: }
373: }
374:
1.231 provos 375: /* Demote private to public keys for network child */
376: void
377: demote_sensitive_data(void)
378: {
1.488 markus 379: struct sshkey *tmp;
1.493 djm 380: u_int i;
1.512 markus 381: int r;
1.231 provos 382:
383: for (i = 0; i < options.num_host_key_files; i++) {
384: if (sensitive_data.host_keys[i]) {
1.515 djm 385: if ((r = sshkey_from_private(
386: sensitive_data.host_keys[i], &tmp)) != 0)
1.564 djm 387: fatal_r(r, "could not demote host %s key",
388: sshkey_type(sensitive_data.host_keys[i]));
1.512 markus 389: sshkey_free(sensitive_data.host_keys[i]);
1.231 provos 390: sensitive_data.host_keys[i] = tmp;
391: }
1.373 djm 392: /* Certs do not need demotion */
1.231 provos 393: }
394: }
395:
1.233 markus 396: static void
1.231 provos 397: privsep_preauth_child(void)
398: {
1.253 deraadt 399: gid_t gidset[1];
1.250 deraadt 400: struct passwd *pw;
1.231 provos 401:
402: /* Enable challenge-response authentication for privilege separation */
403: privsep_challenge_enable();
1.419 djm 404:
1.420 markus 405: #ifdef GSSAPI
1.419 djm 406: /* Cache supported mechanism OIDs for later use */
1.516 djm 407: ssh_gssapi_prepare_supported_oids();
1.420 markus 408: #endif
1.231 provos 409:
410: /* Demote the private keys to public keys. */
411: demote_sensitive_data();
412:
1.460 djm 413: /* Demote the child */
414: if (getuid() == 0 || geteuid() == 0) {
415: if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
416: fatal("Privilege separation user %s does not exist",
417: SSH_PRIVSEP_USER);
1.500 djm 418: pw = pwcopy(pw); /* Ensure mutable */
1.460 djm 419: endpwent();
1.500 djm 420: freezero(pw->pw_passwd, strlen(pw->pw_passwd));
1.460 djm 421:
422: /* Change our root directory */
423: if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
424: fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
425: strerror(errno));
426: if (chdir("/") == -1)
427: fatal("chdir(\"/\"): %s", strerror(errno));
428:
429: /*
430: * Drop our privileges
431: * NB. Can't use setusercontext() after chroot.
432: */
433: debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
434: (u_int)pw->pw_gid);
435: gidset[0] = pw->pw_gid;
1.537 deraadt 436: if (setgroups(1, gidset) == -1)
1.460 djm 437: fatal("setgroups: %.100s", strerror(errno));
438: permanently_set_uid(pw);
439: }
1.231 provos 440: }
441:
1.278 markus 442: static int
1.525 djm 443: privsep_preauth(struct ssh *ssh)
1.237 markus 444: {
1.432 djm 445: int status, r;
1.237 markus 446: pid_t pid;
1.384 djm 447: struct ssh_sandbox *box = NULL;
1.237 markus 448:
449: /* Set up unprivileged child process to deal with network data */
1.242 mouring 450: pmonitor = monitor_init();
1.237 markus 451: /* Store a pointer to the kex for later rekeying */
1.525 djm 452: pmonitor->m_pkex = &ssh->kex;
1.237 markus 453:
1.393 djm 454: if (use_privsep == PRIVSEP_ON)
1.384 djm 455: box = ssh_sandbox_init();
1.237 markus 456: pid = fork();
457: if (pid == -1) {
458: fatal("fork of unprivileged child failed");
459: } else if (pid != 0) {
1.245 mpech 460: debug2("Network child is on pid %ld", (long)pid);
1.237 markus 461:
1.392 markus 462: pmonitor->m_pid = pid;
1.432 djm 463: if (have_agent) {
464: r = ssh_get_authentication_socket(&auth_sock);
465: if (r != 0) {
1.564 djm 466: error_r(r, "Could not get agent socket");
1.432 djm 467: have_agent = 0;
468: }
469: }
1.384 djm 470: if (box != NULL)
471: ssh_sandbox_parent_preauth(box, pid);
1.526 djm 472: monitor_child_preauth(ssh, pmonitor);
1.237 markus 473:
474: /* Wait for the child's exit status */
1.537 deraadt 475: while (waitpid(pid, &status, 0) == -1) {
1.386 djm 476: if (errno == EINTR)
477: continue;
478: pmonitor->m_pid = -1;
1.564 djm 479: fatal_f("waitpid: %s", strerror(errno));
1.384 djm 480: }
1.386 djm 481: privsep_is_preauth = 0;
482: pmonitor->m_pid = -1;
1.384 djm 483: if (WIFEXITED(status)) {
484: if (WEXITSTATUS(status) != 0)
1.564 djm 485: fatal_f("preauth child exited with status %d",
486: WEXITSTATUS(status));
1.384 djm 487: } else if (WIFSIGNALED(status))
1.564 djm 488: fatal_f("preauth child terminated by signal %d",
489: WTERMSIG(status));
1.384 djm 490: if (box != NULL)
491: ssh_sandbox_parent_finish(box);
492: return 1;
1.237 markus 493: } else {
494: /* child */
1.383 djm 495: close(pmonitor->m_sendfd);
496: close(pmonitor->m_log_recvfd);
1.237 markus 497:
1.383 djm 498: /* Arrange for logging to be sent to the monitor */
499: set_log_handler(mm_log_handler, pmonitor);
1.237 markus 500:
1.460 djm 501: privsep_preauth_child();
1.238 stevesk 502: setproctitle("%s", "[net]");
1.384 djm 503: if (box != NULL)
504: ssh_sandbox_child(box);
505:
506: return 0;
1.237 markus 507: }
508: }
509:
1.233 markus 510: static void
1.525 djm 511: privsep_postauth(struct ssh *ssh, Authctxt *authctxt)
1.231 provos 512: {
1.474 djm 513: if (authctxt->pw->pw_uid == 0) {
1.231 provos 514: /* File descriptor passing is broken or root login */
515: use_privsep = 0;
1.315 djm 516: goto skip;
1.231 provos 517: }
1.234 markus 518:
1.231 provos 519: /* New socket pair */
1.242 mouring 520: monitor_reinit(pmonitor);
1.231 provos 521:
1.242 mouring 522: pmonitor->m_pid = fork();
523: if (pmonitor->m_pid == -1)
1.231 provos 524: fatal("fork of unprivileged child failed");
1.242 mouring 525: else if (pmonitor->m_pid != 0) {
1.364 markus 526: verbose("User child is on pid %ld", (long)pmonitor->m_pid);
1.510 markus 527: sshbuf_reset(loginmsg);
1.526 djm 528: monitor_clear_keystate(ssh, pmonitor);
529: monitor_child_postauth(ssh, pmonitor);
1.231 provos 530:
531: /* NEVERREACHED */
532: exit(0);
533: }
534:
1.383 djm 535: /* child */
536:
1.242 mouring 537: close(pmonitor->m_sendfd);
1.383 djm 538: pmonitor->m_sendfd = -1;
1.231 provos 539:
540: /* Demote the private keys to public keys. */
541: demote_sensitive_data();
1.354 djm 542:
1.231 provos 543: /* Drop privileges */
544: do_setusercontext(authctxt->pw);
545:
1.315 djm 546: skip:
1.231 provos 547: /* It is safe now to apply the key state */
1.526 djm 548: monitor_apply_keystate(ssh, pmonitor);
1.312 markus 549:
550: /*
551: * Tell the packet layer that authentication was successful, since
552: * this information is not part of the key state.
553: */
1.525 djm 554: ssh_packet_set_authenticated(ssh);
1.231 provos 555: }
556:
1.509 djm 557: static void
558: append_hostkey_type(struct sshbuf *b, const char *s)
559: {
560: int r;
561:
562: if (match_pattern_list(s, options.hostkeyalgorithms, 0) != 1) {
1.564 djm 563: debug3_f("%s key not permitted by HostkeyAlgorithms", s);
1.509 djm 564: return;
565: }
566: if ((r = sshbuf_putf(b, "%s%s", sshbuf_len(b) > 0 ? "," : "", s)) != 0)
1.564 djm 567: fatal_fr(r, "sshbuf_putf");
1.509 djm 568: }
569:
1.200 itojun 570: static char *
1.134 markus 571: list_hostkey_types(void)
572: {
1.509 djm 573: struct sshbuf *b;
574: struct sshkey *key;
1.281 jakob 575: char *ret;
1.493 djm 576: u_int i;
1.223 markus 577:
1.509 djm 578: if ((b = sshbuf_new()) == NULL)
1.564 djm 579: fatal_f("sshbuf_new failed");
1.217 deraadt 580: for (i = 0; i < options.num_host_key_files; i++) {
1.373 djm 581: key = sensitive_data.host_keys[i];
1.443 djm 582: if (key == NULL)
1.404 markus 583: key = sensitive_data.host_pubkeys[i];
1.472 markus 584: if (key == NULL)
1.134 markus 585: continue;
1.214 deraadt 586: switch (key->type) {
1.134 markus 587: case KEY_RSA:
1.509 djm 588: /* for RSA we also support SHA2 signatures */
589: append_hostkey_type(b, "rsa-sha2-512");
590: append_hostkey_type(b, "rsa-sha2-256");
591: /* FALLTHROUGH */
1.134 markus 592: case KEY_DSA:
1.378 djm 593: case KEY_ECDSA:
1.412 markus 594: case KEY_ED25519:
1.542 djm 595: case KEY_ECDSA_SK:
596: case KEY_ED25519_SK:
1.505 markus 597: case KEY_XMSS:
1.509 djm 598: append_hostkey_type(b, sshkey_ssh_name(key));
1.134 markus 599: break;
600: }
1.373 djm 601: /* If the private key has a cert peer, then list that too */
602: key = sensitive_data.host_certificates[i];
603: if (key == NULL)
604: continue;
605: switch (key->type) {
606: case KEY_RSA_CERT:
1.509 djm 607: /* for RSA we also support SHA2 signatures */
608: append_hostkey_type(b,
609: "rsa-sha2-512-cert-v01@openssh.com");
610: append_hostkey_type(b,
611: "rsa-sha2-256-cert-v01@openssh.com");
612: /* FALLTHROUGH */
1.373 djm 613: case KEY_DSA_CERT:
1.378 djm 614: case KEY_ECDSA_CERT:
1.412 markus 615: case KEY_ED25519_CERT:
1.542 djm 616: case KEY_ECDSA_SK_CERT:
617: case KEY_ED25519_SK_CERT:
1.505 markus 618: case KEY_XMSS_CERT:
1.509 djm 619: append_hostkey_type(b, sshkey_ssh_name(key));
1.373 djm 620: break;
621: }
1.134 markus 622: }
1.509 djm 623: if ((ret = sshbuf_dup_string(b)) == NULL)
1.564 djm 624: fatal_f("sshbuf_dup_string failed");
1.509 djm 625: sshbuf_free(b);
1.564 djm 626: debug_f("%s", ret);
1.281 jakob 627: return ret;
1.134 markus 628: }
629:
1.488 markus 630: static struct sshkey *
1.440 djm 631: get_hostkey_by_type(int type, int nid, int need_private, struct ssh *ssh)
1.134 markus 632: {
1.493 djm 633: u_int i;
1.488 markus 634: struct sshkey *key;
1.250 deraadt 635:
1.217 deraadt 636: for (i = 0; i < options.num_host_key_files; i++) {
1.375 djm 637: switch (type) {
638: case KEY_RSA_CERT:
639: case KEY_DSA_CERT:
1.378 djm 640: case KEY_ECDSA_CERT:
1.412 markus 641: case KEY_ED25519_CERT:
1.542 djm 642: case KEY_ECDSA_SK_CERT:
643: case KEY_ED25519_SK_CERT:
1.505 markus 644: case KEY_XMSS_CERT:
1.373 djm 645: key = sensitive_data.host_certificates[i];
1.375 djm 646: break;
647: default:
1.373 djm 648: key = sensitive_data.host_keys[i];
1.404 markus 649: if (key == NULL && !need_private)
650: key = sensitive_data.host_pubkeys[i];
1.375 djm 651: break;
652: }
1.542 djm 653: if (key == NULL || key->type != type)
654: continue;
655: switch (type) {
656: case KEY_ECDSA:
657: case KEY_ECDSA_SK:
658: case KEY_ECDSA_CERT:
659: case KEY_ECDSA_SK_CERT:
660: if (key->ecdsa_nid != nid)
661: continue;
662: /* FALLTHROUGH */
663: default:
1.373 djm 664: return need_private ?
665: sensitive_data.host_keys[i] : key;
1.542 djm 666: }
1.134 markus 667: }
668: return NULL;
1.96 markus 669: }
670:
1.488 markus 671: struct sshkey *
1.440 djm 672: get_hostkey_public_by_type(int type, int nid, struct ssh *ssh)
1.373 djm 673: {
1.440 djm 674: return get_hostkey_by_type(type, nid, 0, ssh);
1.373 djm 675: }
676:
1.488 markus 677: struct sshkey *
1.440 djm 678: get_hostkey_private_by_type(int type, int nid, struct ssh *ssh)
1.373 djm 679: {
1.440 djm 680: return get_hostkey_by_type(type, nid, 1, ssh);
1.373 djm 681: }
682:
1.488 markus 683: struct sshkey *
1.231 provos 684: get_hostkey_by_index(int ind)
685: {
1.493 djm 686: if (ind < 0 || (u_int)ind >= options.num_host_key_files)
1.231 provos 687: return (NULL);
688: return (sensitive_data.host_keys[ind]);
689: }
690:
1.488 markus 691: struct sshkey *
1.435 markus 692: get_hostkey_public_by_index(int ind, struct ssh *ssh)
1.404 markus 693: {
1.493 djm 694: if (ind < 0 || (u_int)ind >= options.num_host_key_files)
1.404 markus 695: return (NULL);
696: return (sensitive_data.host_pubkeys[ind]);
697: }
698:
1.231 provos 699: int
1.488 markus 700: get_hostkey_index(struct sshkey *key, int compare, struct ssh *ssh)
1.231 provos 701: {
1.493 djm 702: u_int i;
1.250 deraadt 703:
1.231 provos 704: for (i = 0; i < options.num_host_key_files; i++) {
1.512 markus 705: if (sshkey_is_cert(key)) {
1.442 djm 706: if (key == sensitive_data.host_certificates[i] ||
707: (compare && sensitive_data.host_certificates[i] &&
708: sshkey_equal(key,
709: sensitive_data.host_certificates[i])))
1.373 djm 710: return (i);
711: } else {
1.442 djm 712: if (key == sensitive_data.host_keys[i] ||
713: (compare && sensitive_data.host_keys[i] &&
714: sshkey_equal(key, sensitive_data.host_keys[i])))
1.373 djm 715: return (i);
1.442 djm 716: if (key == sensitive_data.host_pubkeys[i] ||
717: (compare && sensitive_data.host_pubkeys[i] &&
718: sshkey_equal(key, sensitive_data.host_pubkeys[i])))
1.404 markus 719: return (i);
1.373 djm 720: }
1.231 provos 721: }
722: return (-1);
723: }
724:
1.439 djm 725: /* Inform the client of all hostkeys */
726: static void
727: notify_hostkeys(struct ssh *ssh)
728: {
729: struct sshbuf *buf;
730: struct sshkey *key;
1.493 djm 731: u_int i, nkeys;
732: int r;
1.439 djm 733: char *fp;
1.446 dtucker 734:
735: /* Some clients cannot cope with the hostkeys message, skip those. */
1.527 djm 736: if (ssh->compat & SSH_BUG_HOSTKEYS)
1.446 dtucker 737: return;
1.439 djm 738:
739: if ((buf = sshbuf_new()) == NULL)
1.564 djm 740: fatal_f("sshbuf_new");
1.439 djm 741: for (i = nkeys = 0; i < options.num_host_key_files; i++) {
742: key = get_hostkey_public_by_index(i, ssh);
743: if (key == NULL || key->type == KEY_UNSPEC ||
1.472 markus 744: sshkey_is_cert(key))
1.439 djm 745: continue;
746: fp = sshkey_fingerprint(key, options.fingerprint_hash,
747: SSH_FP_DEFAULT);
1.564 djm 748: debug3_f("key %d: %s %s", i, sshkey_ssh_name(key), fp);
1.439 djm 749: free(fp);
1.442 djm 750: if (nkeys == 0) {
1.525 djm 751: /*
752: * Start building the request when we find the
753: * first usable key.
754: */
755: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
756: (r = sshpkt_put_cstring(ssh, "hostkeys-00@openssh.com")) != 0 ||
757: (r = sshpkt_put_u8(ssh, 0)) != 0) /* want reply */
758: sshpkt_fatal(ssh, r, "%s: start request", __func__);
1.442 djm 759: }
1.525 djm 760: /* Append the key to the request */
1.442 djm 761: sshbuf_reset(buf);
762: if ((r = sshkey_putb(key, buf)) != 0)
1.564 djm 763: fatal_fr(r, "couldn't put hostkey %d", i);
1.525 djm 764: if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
765: sshpkt_fatal(ssh, r, "%s: append key", __func__);
1.439 djm 766: nkeys++;
767: }
1.564 djm 768: debug3_f("sent %u hostkeys", nkeys);
1.439 djm 769: if (nkeys == 0)
1.564 djm 770: fatal_f("no hostkeys");
1.525 djm 771: if ((r = sshpkt_send(ssh)) != 0)
772: sshpkt_fatal(ssh, r, "%s: send", __func__);
1.442 djm 773: sshbuf_free(buf);
1.439 djm 774: }
775:
1.124 markus 776: /*
777: * returns 1 if connection should be dropped, 0 otherwise.
778: * dropping starts at connection #max_startups_begin with a probability
779: * of (max_startups_rate/100). the probability increases linearly until
780: * all connections are dropped for startups > max_startups
781: */
1.200 itojun 782: static int
1.555 djm 783: should_drop_connection(int startups)
1.124 markus 784: {
1.303 mickey 785: int p, r;
1.124 markus 786:
787: if (startups < options.max_startups_begin)
788: return 0;
789: if (startups >= options.max_startups)
790: return 1;
791: if (options.max_startups_rate == 100)
792: return 1;
793:
794: p = 100 - options.max_startups_rate;
795: p *= startups - options.max_startups_begin;
1.303 mickey 796: p /= options.max_startups - options.max_startups_begin;
1.124 markus 797: p += options.max_startups_rate;
1.356 djm 798: r = arc4random_uniform(100);
1.124 markus 799:
1.564 djm 800: debug_f("p %d, r %d", p, r);
1.124 markus 801: return (r < p) ? 1 : 0;
802: }
803:
1.555 djm 804: /*
805: * Check whether connection should be accepted by MaxStartups.
806: * Returns 0 if the connection is accepted. If the connection is refused,
807: * returns 1 and attempts to send notification to client.
808: * Logs when the MaxStartups condition is entered or exited, and periodically
809: * while in that state.
810: */
811: static int
1.567 dtucker 812: drop_connection(int sock, int startups, int notify_pipe)
1.555 djm 813: {
814: char *laddr, *raddr;
815: const char msg[] = "Exceeded MaxStartups\r\n";
816: static time_t last_drop, first_drop;
817: static u_int ndropped;
818: LogLevel drop_level = SYSLOG_LEVEL_VERBOSE;
819: time_t now;
820:
821: now = monotime();
1.567 dtucker 822: if (!should_drop_connection(startups) &&
823: srclimit_check_allow(sock, notify_pipe) == 1) {
1.555 djm 824: if (last_drop != 0 &&
825: startups < options.max_startups_begin - 1) {
826: /* XXX maybe need better hysteresis here */
827: logit("exited MaxStartups throttling after %s, "
828: "%u connections dropped",
829: fmt_timeframe(now - first_drop), ndropped);
830: last_drop = 0;
831: }
832: return 0;
833: }
834:
835: #define SSHD_MAXSTARTUPS_LOG_INTERVAL (5 * 60)
836: if (last_drop == 0) {
837: error("beginning MaxStartups throttling");
838: drop_level = SYSLOG_LEVEL_INFO;
839: first_drop = now;
840: ndropped = 0;
841: } else if (last_drop + SSHD_MAXSTARTUPS_LOG_INTERVAL < now) {
842: /* Periodic logs */
843: error("in MaxStartups throttling for %s, "
844: "%u connections dropped",
845: fmt_timeframe(now - first_drop), ndropped + 1);
846: drop_level = SYSLOG_LEVEL_INFO;
847: }
848: last_drop = now;
849: ndropped++;
850:
851: laddr = get_local_ipaddr(sock);
852: raddr = get_peer_ipaddr(sock);
853: do_log2(drop_level, "drop connection #%d from [%s]:%d on [%s]:%d "
854: "past MaxStartups", startups, raddr, get_peer_port(sock),
855: laddr, get_local_port(sock));
856: free(laddr);
857: free(raddr);
858: /* best-effort notification to client */
859: (void)write(sock, msg, sizeof(msg) - 1);
860: return 1;
861: }
862:
1.215 markus 863: static void
864: usage(void)
865: {
1.565 dtucker 866: fprintf(stderr, "%s, %s\n", SSH_VERSION, SSH_OPENSSL_VERSION);
1.289 markus 867: fprintf(stderr,
1.473 naddy 868: "usage: sshd [-46DdeiqTt] [-C connection_spec] [-c host_cert_file]\n"
1.399 dtucker 869: " [-E log_file] [-f config_file] [-g login_grace_time]\n"
1.473 naddy 870: " [-h host_key_file] [-o option] [-p port] [-u len]\n"
1.289 markus 871: );
1.215 markus 872: exit(1);
873: }
874:
1.294 djm 875: static void
1.467 djm 876: send_rexec_state(int fd, struct sshbuf *conf)
1.294 djm 877: {
1.546 djm 878: struct sshbuf *m = NULL, *inc = NULL;
879: struct include_item *item = NULL;
1.467 djm 880: int r;
1.294 djm 881:
1.564 djm 882: debug3_f("entering fd = %d config len %zu", fd,
1.467 djm 883: sshbuf_len(conf));
1.294 djm 884:
1.546 djm 885: if ((m = sshbuf_new()) == NULL || (inc = sshbuf_new()) == NULL)
1.564 djm 886: fatal_f("sshbuf_new failed");
1.546 djm 887:
888: /* pack includes into a string */
889: TAILQ_FOREACH(item, &includes, entry) {
890: if ((r = sshbuf_put_cstring(inc, item->selector)) != 0 ||
891: (r = sshbuf_put_cstring(inc, item->filename)) != 0 ||
1.547 djm 892: (r = sshbuf_put_stringb(inc, item->contents)) != 0)
1.564 djm 893: fatal_fr(r, "compose includes");
1.546 djm 894: }
895:
1.294 djm 896: /*
897: * Protocol from reexec master to child:
898: * string configuration
1.546 djm 899: * string included_files[] {
900: * string selector
901: * string filename
902: * string contents
903: * }
1.294 djm 904: */
1.546 djm 905: if ((r = sshbuf_put_stringb(m, conf)) != 0 ||
906: (r = sshbuf_put_stringb(m, inc)) != 0)
1.564 djm 907: fatal_fr(r, "compose config");
1.467 djm 908: if (ssh_msg_send(fd, 0, m) == -1)
1.564 djm 909: error_f("ssh_msg_send failed");
1.294 djm 910:
1.467 djm 911: sshbuf_free(m);
1.546 djm 912: sshbuf_free(inc);
1.294 djm 913:
1.564 djm 914: debug3_f("done");
1.294 djm 915: }
916:
917: static void
1.511 markus 918: recv_rexec_state(int fd, struct sshbuf *conf)
1.294 djm 919: {
1.546 djm 920: struct sshbuf *m, *inc;
1.511 markus 921: u_char *cp, ver;
922: size_t len;
923: int r;
1.546 djm 924: struct include_item *item;
1.294 djm 925:
1.564 djm 926: debug3_f("entering fd = %d", fd);
1.294 djm 927:
1.546 djm 928: if ((m = sshbuf_new()) == NULL || (inc = sshbuf_new()) == NULL)
1.564 djm 929: fatal_f("sshbuf_new failed");
1.511 markus 930: if (ssh_msg_recv(fd, m) == -1)
1.564 djm 931: fatal_f("ssh_msg_recv failed");
1.511 markus 932: if ((r = sshbuf_get_u8(m, &ver)) != 0)
1.564 djm 933: fatal_fr(r, "parse version");
1.511 markus 934: if (ver != 0)
1.564 djm 935: fatal_f("rexec version mismatch");
1.546 djm 936: if ((r = sshbuf_get_string(m, &cp, &len)) != 0 ||
937: (r = sshbuf_get_stringb(m, inc)) != 0)
1.564 djm 938: fatal_fr(r, "parse config");
1.546 djm 939:
1.511 markus 940: if (conf != NULL && (r = sshbuf_put(conf, cp, len)))
1.564 djm 941: fatal_fr(r, "sshbuf_put");
1.294 djm 942:
1.546 djm 943: while (sshbuf_len(inc) != 0) {
944: item = xcalloc(1, sizeof(*item));
945: if ((item->contents = sshbuf_new()) == NULL)
1.564 djm 946: fatal_f("sshbuf_new failed");
1.546 djm 947: if ((r = sshbuf_get_cstring(inc, &item->selector, NULL)) != 0 ||
948: (r = sshbuf_get_cstring(inc, &item->filename, NULL)) != 0 ||
949: (r = sshbuf_get_stringb(inc, item->contents)) != 0)
1.564 djm 950: fatal_fr(r, "parse includes");
1.546 djm 951: TAILQ_INSERT_TAIL(&includes, item, entry);
952: }
953:
1.402 djm 954: free(cp);
1.511 markus 955: sshbuf_free(m);
1.294 djm 956:
1.564 djm 957: debug3_f("done");
1.294 djm 958: }
959:
1.345 djm 960: /* Accept a connection from inetd */
961: static void
962: server_accept_inetd(int *sock_in, int *sock_out)
963: {
964: if (rexeced_flag) {
965: close(REEXEC_CONFIG_PASS_FD);
966: *sock_in = *sock_out = dup(STDIN_FILENO);
967: } else {
968: *sock_in = dup(STDIN_FILENO);
969: *sock_out = dup(STDOUT_FILENO);
970: }
971: /*
972: * We intentionally do not close the descriptors 0, 1, and 2
973: * as our code for setting the descriptors won't work if
974: * ttyfd happens to be one of those.
975: */
1.562 djm 976: if (stdfd_devnull(1, 1, !log_stderr) == -1)
1.564 djm 977: error_f("stdfd_devnull failed");
1.345 djm 978: debug("inetd sockets after dupping: %d, %d", *sock_in, *sock_out);
979: }
980:
981: /*
982: * Listen for TCP connections
983: */
984: static void
1.494 djm 985: listen_on_addrs(struct listenaddr *la)
1.345 djm 986: {
1.494 djm 987: int ret, listen_sock;
1.345 djm 988: struct addrinfo *ai;
989: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
990:
1.494 djm 991: for (ai = la->addrs; ai; ai = ai->ai_next) {
1.345 djm 992: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
993: continue;
994: if (num_listen_socks >= MAX_LISTEN_SOCKS)
995: fatal("Too many listen sockets. "
996: "Enlarge MAX_LISTEN_SOCKS");
997: if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
998: ntop, sizeof(ntop), strport, sizeof(strport),
999: NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1000: error("getnameinfo failed: %.100s",
1.352 dtucker 1001: ssh_gai_strerror(ret));
1.345 djm 1002: continue;
1003: }
1004: /* Create socket for listening. */
1.370 dtucker 1005: listen_sock = socket(ai->ai_family, ai->ai_socktype,
1006: ai->ai_protocol);
1.537 deraadt 1007: if (listen_sock == -1) {
1.345 djm 1008: /* kernel may not support ipv6 */
1009: verbose("socket: %.100s", strerror(errno));
1010: continue;
1011: }
1012: if (set_nonblock(listen_sock) == -1) {
1.483 djm 1013: close(listen_sock);
1014: continue;
1015: }
1016: if (fcntl(listen_sock, F_SETFD, FD_CLOEXEC) == -1) {
1017: verbose("socket: CLOEXEC: %s", strerror(errno));
1.345 djm 1018: close(listen_sock);
1019: continue;
1020: }
1.494 djm 1021: /* Socket options */
1022: set_reuseaddr(listen_sock);
1023: if (la->rdomain != NULL &&
1024: set_rdomain(listen_sock, la->rdomain) == -1) {
1025: close(listen_sock);
1026: continue;
1027: }
1.345 djm 1028:
1029: debug("Bind to port %s on %s.", strport, ntop);
1030:
1031: /* Bind the socket to the desired port. */
1.537 deraadt 1032: if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) == -1) {
1.345 djm 1033: error("Bind to port %s on %s failed: %.200s.",
1034: strport, ntop, strerror(errno));
1035: close(listen_sock);
1036: continue;
1037: }
1038: listen_socks[num_listen_socks] = listen_sock;
1039: num_listen_socks++;
1040:
1041: /* Start listening on the port. */
1.537 deraadt 1042: if (listen(listen_sock, SSH_LISTEN_BACKLOG) == -1)
1.345 djm 1043: fatal("listen on [%s]:%s: %.100s",
1044: ntop, strport, strerror(errno));
1.494 djm 1045: logit("Server listening on %s port %s%s%s.",
1046: ntop, strport,
1047: la->rdomain == NULL ? "" : " rdomain ",
1048: la->rdomain == NULL ? "" : la->rdomain);
1.345 djm 1049: }
1.494 djm 1050: }
1051:
1052: static void
1053: server_listen(void)
1054: {
1055: u_int i;
1056:
1.567 dtucker 1057: /* Initialise per-source limit tracking. */
1058: srclimit_init(options.max_startups, options.per_source_max_startups,
1059: options.per_source_masklen_ipv4, options.per_source_masklen_ipv6);
1060:
1.494 djm 1061: for (i = 0; i < options.num_listen_addrs; i++) {
1062: listen_on_addrs(&options.listen_addrs[i]);
1063: freeaddrinfo(options.listen_addrs[i].addrs);
1064: free(options.listen_addrs[i].rdomain);
1065: memset(&options.listen_addrs[i], 0,
1066: sizeof(options.listen_addrs[i]));
1067: }
1068: free(options.listen_addrs);
1069: options.listen_addrs = NULL;
1070: options.num_listen_addrs = 0;
1.345 djm 1071:
1072: if (!num_listen_socks)
1073: fatal("Cannot bind any address.");
1074: }
1075:
1076: /*
1077: * The main TCP accept loop. Note that, for the non-debug case, returns
1078: * from this function are in a forked subprocess.
1079: */
1080: static void
1081: server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
1082: {
1.579 deraadt 1083: struct pollfd *pfd = NULL;
1084: int i, j, ret;
1.543 djm 1085: int ostartups = -1, startups = 0, listening = 0, lameduck = 0;
1.345 djm 1086: int startup_p[2] = { -1 , -1 };
1.533 djm 1087: char c = 0;
1.345 djm 1088: struct sockaddr_storage from;
1089: socklen_t fromlen;
1090: pid_t pid;
1.574 dtucker 1091: sigset_t nsigset, osigset;
1.345 djm 1092:
1093: /* setup fd set for accept */
1.550 djm 1094: /* pipes connected to unauthenticated child sshd processes */
1.345 djm 1095: startup_pipes = xcalloc(options.max_startups, sizeof(int));
1.533 djm 1096: startup_flags = xcalloc(options.max_startups, sizeof(int));
1.345 djm 1097: for (i = 0; i < options.max_startups; i++)
1098: startup_pipes[i] = -1;
1099:
1100: /*
1.574 dtucker 1101: * Prepare signal mask that we use to block signals that might set
1102: * received_sigterm or received_sighup, so that we are guaranteed
1.579 deraadt 1103: * to immediately wake up the ppoll if a signal is received after
1.574 dtucker 1104: * the flag is checked.
1105: */
1106: sigemptyset(&nsigset);
1107: sigaddset(&nsigset, SIGHUP);
1108: sigaddset(&nsigset, SIGCHLD);
1109: sigaddset(&nsigset, SIGTERM);
1110: sigaddset(&nsigset, SIGQUIT);
1111:
1.579 deraadt 1112: pfd = xcalloc(num_listen_socks + options.max_startups,
1113: sizeof(struct pollfd));
1114:
1.574 dtucker 1115: /*
1.345 djm 1116: * Stay listening for connections until the system crashes or
1117: * the daemon is killed with a signal.
1118: */
1119: for (;;) {
1.574 dtucker 1120: sigprocmask(SIG_BLOCK, &nsigset, &osigset);
1121: if (received_sigterm) {
1122: logit("Received signal %d; terminating.",
1123: (int) received_sigterm);
1124: close_listen_socks();
1125: if (options.pid_file != NULL)
1126: unlink(options.pid_file);
1127: exit(received_sigterm == SIGTERM ? 0 : 255);
1128: }
1.543 djm 1129: if (ostartups != startups) {
1.545 djm 1130: setproctitle("%s [listener] %d of %d-%d startups",
1131: listener_proctitle, startups,
1132: options.max_startups_begin, options.max_startups);
1.543 djm 1133: ostartups = startups;
1134: }
1.533 djm 1135: if (received_sighup) {
1136: if (!lameduck) {
1137: debug("Received SIGHUP; waiting for children");
1138: close_listen_socks();
1139: lameduck = 1;
1140: }
1.574 dtucker 1141: if (listening <= 0) {
1142: sigprocmask(SIG_SETMASK, &osigset, NULL);
1.533 djm 1143: sighup_restart();
1.574 dtucker 1144: }
1.533 djm 1145: }
1.345 djm 1146:
1.579 deraadt 1147: for (i = 0; i < num_listen_socks; i++) {
1148: pfd[i].fd = listen_socks[i];
1149: pfd[i].events = POLLIN;
1150: }
1151: for (i = 0; i < options.max_startups; i++) {
1152: pfd[num_listen_socks+i].fd = startup_pipes[i];
1.345 djm 1153: if (startup_pipes[i] != -1)
1.579 deraadt 1154: pfd[num_listen_socks+i].events = POLLIN;
1155: }
1.345 djm 1156:
1.574 dtucker 1157: /* Wait until a connection arrives or a child exits. */
1.579 deraadt 1158: ret = ppoll(pfd, num_listen_socks + options.max_startups,
1159: NULL, &osigset);
1.537 deraadt 1160: if (ret == -1 && errno != EINTR)
1.579 deraadt 1161: error("ppoll: %.100s", strerror(errno));
1.574 dtucker 1162: sigprocmask(SIG_SETMASK, &osigset, NULL);
1.576 dtucker 1163: if (ret == -1)
1.345 djm 1164: continue;
1165:
1.533 djm 1166: for (i = 0; i < options.max_startups; i++) {
1167: if (startup_pipes[i] == -1 ||
1.581 djm 1168: !(pfd[num_listen_socks+i].revents & (POLLIN|POLLHUP)))
1.533 djm 1169: continue;
1170: switch (read(startup_pipes[i], &c, sizeof(c))) {
1171: case -1:
1172: if (errno == EINTR || errno == EAGAIN)
1173: continue;
1174: if (errno != EPIPE) {
1.564 djm 1175: error_f("startup pipe %d (fd=%d): "
1176: "read %s", i, startup_pipes[i],
1177: strerror(errno));
1.533 djm 1178: }
1179: /* FALLTHROUGH */
1180: case 0:
1181: /* child exited or completed auth */
1.345 djm 1182: close(startup_pipes[i]);
1.567 dtucker 1183: srclimit_done(startup_pipes[i]);
1.345 djm 1184: startup_pipes[i] = -1;
1185: startups--;
1.533 djm 1186: if (startup_flags[i])
1187: listening--;
1188: break;
1189: case 1:
1190: /* child has finished preliminaries */
1191: if (startup_flags[i]) {
1192: listening--;
1193: startup_flags[i] = 0;
1194: }
1195: break;
1.345 djm 1196: }
1.533 djm 1197: }
1.345 djm 1198: for (i = 0; i < num_listen_socks; i++) {
1.579 deraadt 1199: if (!(pfd[i].revents & POLLIN))
1.345 djm 1200: continue;
1201: fromlen = sizeof(from);
1202: *newsock = accept(listen_socks[i],
1203: (struct sockaddr *)&from, &fromlen);
1.537 deraadt 1204: if (*newsock == -1) {
1.398 markus 1205: if (errno != EINTR && errno != EWOULDBLOCK &&
1206: errno != ECONNABORTED)
1.389 djm 1207: error("accept: %.100s",
1208: strerror(errno));
1209: if (errno == EMFILE || errno == ENFILE)
1210: usleep(100 * 1000);
1.345 djm 1211: continue;
1212: }
1.555 djm 1213: if (unset_nonblock(*newsock) == -1 ||
1.582 ! djm 1214: pipe(startup_p) == -1) {
! 1215: close(*newsock);
1.567 dtucker 1216: continue;
1.582 ! djm 1217: }
1.567 dtucker 1218: if (drop_connection(*newsock, startups, startup_p[0])) {
1.345 djm 1219: close(*newsock);
1.567 dtucker 1220: close(startup_p[0]);
1221: close(startup_p[1]);
1.345 djm 1222: continue;
1223: }
1224:
1225: if (rexec_flag && socketpair(AF_UNIX,
1226: SOCK_STREAM, 0, config_s) == -1) {
1227: error("reexec socketpair: %s",
1228: strerror(errno));
1229: close(*newsock);
1230: close(startup_p[0]);
1231: close(startup_p[1]);
1232: continue;
1233: }
1234:
1235: for (j = 0; j < options.max_startups; j++)
1236: if (startup_pipes[j] == -1) {
1237: startup_pipes[j] = startup_p[0];
1238: startups++;
1.533 djm 1239: startup_flags[j] = 1;
1.345 djm 1240: break;
1241: }
1242:
1243: /*
1244: * Got connection. Fork a child to handle it, unless
1245: * we are in debugging mode.
1246: */
1247: if (debug_flag) {
1248: /*
1249: * In debugging mode. Close the listening
1250: * socket, and start processing the
1251: * connection without forking.
1252: */
1253: debug("Server will not fork when running in debugging mode.");
1254: close_listen_socks();
1255: *sock_in = *newsock;
1256: *sock_out = *newsock;
1257: close(startup_p[0]);
1258: close(startup_p[1]);
1259: startup_pipe = -1;
1260: pid = getpid();
1261: if (rexec_flag) {
1.511 markus 1262: send_rexec_state(config_s[0], cfg);
1.345 djm 1263: close(config_s[0]);
1264: }
1.579 deraadt 1265: free(pfd);
1.533 djm 1266: return;
1.345 djm 1267: }
1268:
1269: /*
1270: * Normal production daemon. Fork, and have
1271: * the child process the connection. The
1272: * parent continues listening.
1273: */
1.533 djm 1274: listening++;
1.345 djm 1275: if ((pid = fork()) == 0) {
1276: /*
1277: * Child. Close the listening and
1278: * max_startup sockets. Start using
1279: * the accepted socket. Reinitialize
1280: * logging (since our pid has changed).
1.533 djm 1281: * We return from this function to handle
1.345 djm 1282: * the connection.
1283: */
1284: startup_pipe = startup_p[1];
1285: close_startup_pipes();
1286: close_listen_socks();
1287: *sock_in = *newsock;
1288: *sock_out = *newsock;
1289: log_init(__progname,
1290: options.log_level,
1291: options.log_facility,
1292: log_stderr);
1293: if (rexec_flag)
1294: close(config_s[0]);
1.533 djm 1295: else {
1296: /*
1297: * Signal parent that the preliminaries
1298: * for this child are complete. For the
1299: * re-exec case, this happens after the
1300: * child has received the rexec state
1301: * from the server.
1302: */
1303: (void)atomicio(vwrite, startup_pipe,
1304: "\0", 1);
1305: }
1.579 deraadt 1306: free(pfd);
1.533 djm 1307: return;
1.345 djm 1308: }
1309:
1310: /* Parent. Stay in the loop. */
1.537 deraadt 1311: if (pid == -1)
1.345 djm 1312: error("fork: %.100s", strerror(errno));
1313: else
1314: debug("Forked child %ld.", (long)pid);
1315:
1316: close(startup_p[1]);
1317:
1318: if (rexec_flag) {
1.554 markus 1319: close(config_s[1]);
1.511 markus 1320: send_rexec_state(config_s[0], cfg);
1.345 djm 1321: close(config_s[0]);
1322: }
1323: close(*newsock);
1324: }
1325: }
1326: }
1327:
1.466 djm 1328: /*
1329: * If IP options are supported, make sure there are none (log and
1330: * return an error if any are found). Basically we are worried about
1331: * source routing; it can be used to pretend you are somebody
1332: * (ip-address) you are not. That itself may be "almost acceptable"
1.507 djm 1333: * under certain circumstances, but rhosts authentication is useless
1.466 djm 1334: * if source routing is accepted. Notice also that if we just dropped
1335: * source routing here, the other side could use IP spoofing to do
1336: * rest of the interaction and could still bypass security. So we
1337: * exit here if we detect any IP options.
1338: */
1339: static void
1340: check_ip_options(struct ssh *ssh)
1341: {
1342: int sock_in = ssh_packet_get_connection_in(ssh);
1343: struct sockaddr_storage from;
1344: u_char opts[200];
1.475 djm 1345: socklen_t i, option_size = sizeof(opts), fromlen = sizeof(from);
1.466 djm 1346: char text[sizeof(opts) * 3 + 1];
1347:
1348: memset(&from, 0, sizeof(from));
1349: if (getpeername(sock_in, (struct sockaddr *)&from,
1.537 deraadt 1350: &fromlen) == -1)
1.466 djm 1351: return;
1352: if (from.ss_family != AF_INET)
1353: return;
1354: /* XXX IPv6 options? */
1355:
1356: if (getsockopt(sock_in, IPPROTO_IP, IP_OPTIONS, opts,
1357: &option_size) >= 0 && option_size != 0) {
1358: text[0] = '\0';
1359: for (i = 0; i < option_size; i++)
1360: snprintf(text + i*3, sizeof(text) - i*3,
1361: " %2.2x", opts[i]);
1362: fatal("Connection from %.100s port %d with IP opts: %.800s",
1363: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), text);
1364: }
1365: return;
1366: }
1.345 djm 1367:
1.495 djm 1368: /* Set the routing domain for this process */
1369: static void
1370: set_process_rdomain(struct ssh *ssh, const char *name)
1371: {
1372: int rtable, ortable = getrtable();
1373: const char *errstr;
1374:
1375: if (name == NULL)
1376: return; /* default */
1377:
1378: if (strcmp(name, "%D") == 0) {
1379: /* "expands" to routing domain of connection */
1380: if ((name = ssh_packet_rdomain_in(ssh)) == NULL)
1381: return;
1382: }
1383:
1384: rtable = (int)strtonum(name, 0, 255, &errstr);
1385: if (errstr != NULL) /* Shouldn't happen */
1386: fatal("Invalid routing domain \"%s\": %s", name, errstr);
1387: if (rtable != ortable && setrtable(rtable) != 0)
1388: fatal("Unable to set routing domain %d: %s",
1389: rtable, strerror(errno));
1.564 djm 1390: debug_f("set routing domain %d (was %d)", rtable, ortable);
1.495 djm 1391: }
1392:
1.508 dtucker 1393: static void
1394: accumulate_host_timing_secret(struct sshbuf *server_cfg,
1.536 djm 1395: struct sshkey *key)
1.508 dtucker 1396: {
1397: static struct ssh_digest_ctx *ctx;
1398: u_char *hash;
1399: size_t len;
1400: struct sshbuf *buf;
1401: int r;
1402:
1403: if (ctx == NULL && (ctx = ssh_digest_start(SSH_DIGEST_SHA512)) == NULL)
1.564 djm 1404: fatal_f("ssh_digest_start");
1.508 dtucker 1405: if (key == NULL) { /* finalize */
1406: /* add server config in case we are using agent for host keys */
1407: if (ssh_digest_update(ctx, sshbuf_ptr(server_cfg),
1408: sshbuf_len(server_cfg)) != 0)
1.564 djm 1409: fatal_f("ssh_digest_update");
1.508 dtucker 1410: len = ssh_digest_bytes(SSH_DIGEST_SHA512);
1411: hash = xmalloc(len);
1412: if (ssh_digest_final(ctx, hash, len) != 0)
1.564 djm 1413: fatal_f("ssh_digest_final");
1.508 dtucker 1414: options.timing_secret = PEEK_U64(hash);
1415: freezero(hash, len);
1416: ssh_digest_free(ctx);
1417: ctx = NULL;
1418: return;
1419: }
1420: if ((buf = sshbuf_new()) == NULL)
1.564 djm 1421: fatal_f("could not allocate buffer");
1.508 dtucker 1422: if ((r = sshkey_private_serialize(key, buf)) != 0)
1.564 djm 1423: fatal_fr(r, "decode key");
1.508 dtucker 1424: if (ssh_digest_update(ctx, sshbuf_ptr(buf), sshbuf_len(buf)) != 0)
1.564 djm 1425: fatal_f("ssh_digest_update");
1.508 dtucker 1426: sshbuf_reset(buf);
1427: sshbuf_free(buf);
1428: }
1429:
1.545 djm 1430: static char *
1431: prepare_proctitle(int ac, char **av)
1432: {
1433: char *ret = NULL;
1434: int i;
1435:
1436: for (i = 0; i < ac; i++)
1437: xextendf(&ret, " ", "%s", av[i]);
1438: return ret;
1439: }
1440:
1.65 deraadt 1441: /*
1442: * Main program for the daemon.
1443: */
1.2 provos 1444: int
1445: main(int ac, char **av)
1.1 deraadt 1446: {
1.466 djm 1447: struct ssh *ssh = NULL;
1.64 markus 1448: extern char *optarg;
1449: extern int optind;
1.493 djm 1450: int r, opt, on = 1, already_daemon, remote_port;
1.297 avsm 1451: int sock_in = -1, sock_out = -1, newsock = -1;
1.496 djm 1452: const char *remote_ip, *rdomain;
1.447 dtucker 1453: char *fp, *line, *laddr, *logfile = NULL;
1.345 djm 1454: int config_s[2] = { -1 , -1 };
1.493 djm 1455: u_int i, j;
1.364 markus 1456: u_int64_t ibytes, obytes;
1.362 dtucker 1457: mode_t new_umask;
1.488 markus 1458: struct sshkey *key;
1459: struct sshkey *pubkey;
1.404 markus 1460: int keytype;
1.230 provos 1461: Authctxt *authctxt;
1.498 dtucker 1462: struct connection_info *connection_info = NULL;
1.64 markus 1463:
1.138 markus 1464: /* Save argv. */
1.64 markus 1465: saved_argv = av;
1.294 djm 1466: rexec_argc = ac;
1.313 djm 1467:
1468: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1469: sanitise_stdfd();
1.64 markus 1470:
1471: /* Initialize configuration options to their default values. */
1472: initialize_server_options(&options);
1473:
1474: /* Parse command-line arguments. */
1.450 djm 1475: while ((opt = getopt(ac, av,
1476: "C:E:b:c:f:g:h:k:o:p:u:46DQRTdeiqrt")) != -1) {
1.64 markus 1477: switch (opt) {
1.75 markus 1478: case '4':
1.305 djm 1479: options.address_family = AF_INET;
1.75 markus 1480: break;
1481: case '6':
1.305 djm 1482: options.address_family = AF_INET6;
1.75 markus 1483: break;
1.64 markus 1484: case 'f':
1485: config_file_name = optarg;
1486: break;
1.373 djm 1487: case 'c':
1.493 djm 1488: servconf_add_hostcert("[command-line]", 0,
1489: &options, optarg);
1.373 djm 1490: break;
1.64 markus 1491: case 'd':
1.273 markus 1492: if (debug_flag == 0) {
1.127 markus 1493: debug_flag = 1;
1494: options.log_level = SYSLOG_LEVEL_DEBUG1;
1.273 markus 1495: } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
1.127 markus 1496: options.log_level++;
1.64 markus 1497: break;
1.135 markus 1498: case 'D':
1499: no_daemon_flag = 1;
1.192 lebel 1500: break;
1.399 dtucker 1501: case 'E':
1.459 dtucker 1502: logfile = optarg;
1.399 dtucker 1503: /* FALLTHROUGH */
1.192 lebel 1504: case 'e':
1505: log_stderr = 1;
1.135 markus 1506: break;
1.64 markus 1507: case 'i':
1508: inetd_flag = 1;
1509: break;
1.294 djm 1510: case 'r':
1511: rexec_flag = 0;
1512: break;
1513: case 'R':
1514: rexeced_flag = 1;
1515: inetd_flag = 1;
1516: break;
1.64 markus 1517: case 'Q':
1.158 markus 1518: /* ignored */
1.64 markus 1519: break;
1520: case 'q':
1521: options.log_level = SYSLOG_LEVEL_QUIET;
1522: break;
1523: case 'b':
1.473 naddy 1524: /* protocol 1, ignored */
1.64 markus 1525: break;
1526: case 'p':
1.75 markus 1527: options.ports_from_cmdline = 1;
1.127 markus 1528: if (options.num_ports >= MAX_PORTS) {
1529: fprintf(stderr, "too many ports.\n");
1530: exit(1);
1531: }
1.193 stevesk 1532: options.ports[options.num_ports++] = a2port(optarg);
1.366 djm 1533: if (options.ports[options.num_ports-1] <= 0) {
1.193 stevesk 1534: fprintf(stderr, "Bad port number.\n");
1535: exit(1);
1536: }
1.64 markus 1537: break;
1538: case 'g':
1.197 stevesk 1539: if ((options.login_grace_time = convtime(optarg)) == -1) {
1540: fprintf(stderr, "Invalid login grace time.\n");
1541: exit(1);
1542: }
1.64 markus 1543: break;
1544: case 'k':
1.473 naddy 1545: /* protocol 1, ignored */
1.64 markus 1546: break;
1547: case 'h':
1.493 djm 1548: servconf_add_hostkey("[command-line]", 0,
1.519 djm 1549: &options, optarg, 1);
1.64 markus 1550: break;
1.203 stevesk 1551: case 't':
1552: test_flag = 1;
1553: break;
1.358 dtucker 1554: case 'T':
1555: test_flag = 2;
1556: break;
1557: case 'C':
1.523 djm 1558: connection_info = get_connection_info(ssh, 0, 0);
1.391 dtucker 1559: if (parse_server_match_testspec(connection_info,
1560: optarg) == -1)
1561: exit(1);
1.358 dtucker 1562: break;
1.125 markus 1563: case 'u':
1.438 deraadt 1564: utmp_len = (u_int)strtonum(optarg, 0, HOST_NAME_MAX+1+1, NULL);
1565: if (utmp_len > HOST_NAME_MAX+1) {
1.257 stevesk 1566: fprintf(stderr, "Invalid utmp length.\n");
1567: exit(1);
1568: }
1.125 markus 1569: break;
1.215 markus 1570: case 'o':
1.283 markus 1571: line = xstrdup(optarg);
1572: if (process_server_config_line(&options, line,
1.546 djm 1573: "command-line", 0, NULL, NULL, &includes) != 0)
1.217 deraadt 1574: exit(1);
1.402 djm 1575: free(line);
1.215 markus 1576: break;
1.64 markus 1577: case '?':
1578: default:
1.215 markus 1579: usage();
1580: break;
1.64 markus 1581: }
1582: }
1.294 djm 1583: if (rexeced_flag || inetd_flag)
1584: rexec_flag = 0;
1.518 djm 1585: if (!test_flag && rexec_flag && !path_absolute(av[0]))
1.294 djm 1586: fatal("sshd re-exec requires execution with an absolute path");
1587: if (rexeced_flag)
1.296 djm 1588: closefrom(REEXEC_MIN_FREE_FD);
1589: else
1590: closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
1.294 djm 1591:
1.426 markus 1592: #ifdef WITH_OPENSSL
1.379 djm 1593: OpenSSL_add_all_algorithms();
1.426 markus 1594: #endif
1.64 markus 1595:
1.399 dtucker 1596: /* If requested, redirect the logs to the specified logfile. */
1.459 dtucker 1597: if (logfile != NULL)
1.399 dtucker 1598: log_redirect_stderr_to(logfile);
1.75 markus 1599: /*
1600: * Force logging to stderr until we have loaded the private host
1601: * key (unless started from inetd)
1602: */
1.138 markus 1603: log_init(__progname,
1.224 markus 1604: options.log_level == SYSLOG_LEVEL_NOT_SET ?
1605: SYSLOG_LEVEL_INFO : options.log_level,
1606: options.log_facility == SYSLOG_FACILITY_NOT_SET ?
1607: SYSLOG_FACILITY_AUTH : options.log_facility,
1.548 djm 1608: log_stderr || !inetd_flag || debug_flag);
1.75 markus 1609:
1.294 djm 1610: sensitive_data.have_ssh2_key = 0;
1611:
1.358 dtucker 1612: /*
1.498 dtucker 1613: * If we're not doing an extended test do not silently ignore connection
1614: * test params.
1.358 dtucker 1615: */
1.498 dtucker 1616: if (test_flag < 2 && connection_info != NULL)
1.358 dtucker 1617: fatal("Config test connection parameter (-C) provided without "
1.572 djm 1618: "test mode (-T)");
1.358 dtucker 1619:
1.294 djm 1620: /* Fetch our configuration */
1.511 markus 1621: if ((cfg = sshbuf_new()) == NULL)
1.564 djm 1622: fatal_f("sshbuf_new failed");
1.533 djm 1623: if (rexeced_flag) {
1.560 markus 1624: setproctitle("%s", "[rexeced]");
1.511 markus 1625: recv_rexec_state(REEXEC_CONFIG_PASS_FD, cfg);
1.533 djm 1626: if (!debug_flag) {
1627: startup_pipe = dup(REEXEC_STARTUP_PIPE_FD);
1628: close(REEXEC_STARTUP_PIPE_FD);
1629: /*
1630: * Signal parent that this child is at a point where
1631: * they can go away if they have a SIGHUP pending.
1632: */
1633: (void)atomicio(vwrite, startup_pipe, "\0", 1);
1634: }
1.549 djm 1635: } else if (strcasecmp(config_file_name, "none") != 0)
1.511 markus 1636: load_server_config(config_file_name, cfg);
1.294 djm 1637:
1.337 dtucker 1638: parse_server_config(&options, rexeced_flag ? "rexec" : config_file_name,
1.546 djm 1639: cfg, &includes, NULL);
1.571 dtucker 1640:
1.578 dtucker 1641: #ifdef WITH_OPENSSL
1.571 dtucker 1642: if (options.moduli_file != NULL)
1643: dh_set_moduli_file(options.moduli_file);
1.578 dtucker 1644: #endif
1.64 markus 1645:
1646: /* Fill in default values for those options not explicitly set. */
1647: fill_default_server_options(&options);
1.395 djm 1648:
1649: /* Check that options are sensible */
1650: if (options.authorized_keys_command_user == NULL &&
1651: (options.authorized_keys_command != NULL &&
1652: strcasecmp(options.authorized_keys_command, "none") != 0))
1653: fatal("AuthorizedKeysCommand set without "
1654: "AuthorizedKeysCommandUser");
1.449 djm 1655: if (options.authorized_principals_command_user == NULL &&
1656: (options.authorized_principals_command != NULL &&
1657: strcasecmp(options.authorized_principals_command, "none") != 0))
1658: fatal("AuthorizedPrincipalsCommand set without "
1659: "AuthorizedPrincipalsCommandUser");
1.396 djm 1660:
1661: /*
1662: * Check whether there is any path through configured auth methods.
1663: * Unfortunately it is not possible to verify this generally before
1664: * daemonisation in the presence of Match block, but this catches
1665: * and warns for trivial misconfigurations that could break login.
1666: */
1667: if (options.num_auth_methods != 0) {
1.493 djm 1668: for (i = 0; i < options.num_auth_methods; i++) {
1669: if (auth2_methods_valid(options.auth_methods[i],
1.396 djm 1670: 1) == 0)
1671: break;
1672: }
1.493 djm 1673: if (i >= options.num_auth_methods)
1.396 djm 1674: fatal("AuthenticationMethods cannot be satisfied by "
1675: "enabled authentication methods");
1676: }
1.305 djm 1677:
1.64 markus 1678: /* Check that there are no remaining arguments. */
1679: if (optind < ac) {
1680: fprintf(stderr, "Extra argument %s.\n", av[optind]);
1681: exit(1);
1682: }
1683:
1.565 dtucker 1684: debug("sshd version %s, %s", SSH_VERSION, SSH_OPENSSL_VERSION);
1.64 markus 1685:
1.404 markus 1686: /* load host keys */
1.329 djm 1687: sensitive_data.host_keys = xcalloc(options.num_host_key_files,
1.488 markus 1688: sizeof(struct sshkey *));
1.404 markus 1689: sensitive_data.host_pubkeys = xcalloc(options.num_host_key_files,
1.488 markus 1690: sizeof(struct sshkey *));
1.404 markus 1691:
1692: if (options.host_key_agent) {
1693: if (strcmp(options.host_key_agent, SSH_AUTHSOCKET_ENV_NAME))
1694: setenv(SSH_AUTHSOCKET_ENV_NAME,
1695: options.host_key_agent, 1);
1.433 djm 1696: if ((r = ssh_get_authentication_socket(NULL)) == 0)
1697: have_agent = 1;
1698: else
1.564 djm 1699: error_r(r, "Could not connect to agent \"%s\"",
1700: options.host_key_agent);
1.404 markus 1701: }
1.134 markus 1702:
1.217 deraadt 1703: for (i = 0; i < options.num_host_key_files; i++) {
1.519 djm 1704: int ll = options.host_key_file_userprovided[i] ?
1705: SYSLOG_LEVEL_ERROR : SYSLOG_LEVEL_DEBUG1;
1706:
1.430 djm 1707: if (options.host_key_files[i] == NULL)
1708: continue;
1.512 markus 1709: if ((r = sshkey_load_private(options.host_key_files[i], "",
1710: &key, NULL)) != 0 && r != SSH_ERR_SYSTEM_ERROR)
1.564 djm 1711: do_log2_r(r, ll, "Unable to load host key \"%s\"",
1712: options.host_key_files[i]);
1.542 djm 1713: if (sshkey_is_sk(key) &&
1714: key->sk_flags & SSH_SK_USER_PRESENCE_REQD) {
1715: debug("host key %s requires user presence, ignoring",
1716: options.host_key_files[i]);
1717: key->sk_flags &= ~SSH_SK_USER_PRESENCE_REQD;
1718: }
1719: if (r == 0 && key != NULL &&
1720: (r = sshkey_shield_private(key)) != 0) {
1.564 djm 1721: do_log2_r(r, ll, "Unable to shield host key \"%s\"",
1722: options.host_key_files[i]);
1.536 djm 1723: sshkey_free(key);
1724: key = NULL;
1725: }
1.512 markus 1726: if ((r = sshkey_load_public(options.host_key_files[i],
1727: &pubkey, NULL)) != 0 && r != SSH_ERR_SYSTEM_ERROR)
1.564 djm 1728: do_log2_r(r, ll, "Unable to load host key \"%s\"",
1729: options.host_key_files[i]);
1.557 djm 1730: if (pubkey != NULL && key != NULL) {
1731: if (!sshkey_equal(pubkey, key)) {
1732: error("Public key for %s does not match "
1733: "private key", options.host_key_files[i]);
1734: sshkey_free(pubkey);
1735: pubkey = NULL;
1736: }
1737: }
1738: if (pubkey == NULL && key != NULL) {
1.515 djm 1739: if ((r = sshkey_from_private(key, &pubkey)) != 0)
1.564 djm 1740: fatal_r(r, "Could not demote key: \"%s\"",
1741: options.host_key_files[i]);
1.557 djm 1742: }
1.179 markus 1743: sensitive_data.host_keys[i] = key;
1.404 markus 1744: sensitive_data.host_pubkeys[i] = pubkey;
1745:
1.472 markus 1746: if (key == NULL && pubkey != NULL && have_agent) {
1.443 djm 1747: debug("will rely on agent for hostkey %s",
1748: options.host_key_files[i]);
1.404 markus 1749: keytype = pubkey->type;
1750: } else if (key != NULL) {
1751: keytype = key->type;
1.511 markus 1752: accumulate_host_timing_secret(cfg, key);
1.404 markus 1753: } else {
1.519 djm 1754: do_log2(ll, "Unable to load host key: %s",
1.195 markus 1755: options.host_key_files[i]);
1.179 markus 1756: sensitive_data.host_keys[i] = NULL;
1.404 markus 1757: sensitive_data.host_pubkeys[i] = NULL;
1.134 markus 1758: continue;
1759: }
1.404 markus 1760:
1761: switch (keytype) {
1.134 markus 1762: case KEY_RSA:
1763: case KEY_DSA:
1.378 djm 1764: case KEY_ECDSA:
1.412 markus 1765: case KEY_ED25519:
1.542 djm 1766: case KEY_ECDSA_SK:
1767: case KEY_ED25519_SK:
1.505 markus 1768: case KEY_XMSS:
1.441 djm 1769: if (have_agent || key != NULL)
1770: sensitive_data.have_ssh2_key = 1;
1.134 markus 1771: break;
1772: }
1.441 djm 1773: if ((fp = sshkey_fingerprint(pubkey, options.fingerprint_hash,
1774: SSH_FP_DEFAULT)) == NULL)
1775: fatal("sshkey_fingerprint failed");
1776: debug("%s host key #%d: %s %s",
1.472 markus 1777: key ? "private" : "agent", i, sshkey_ssh_name(pubkey), fp);
1.441 djm 1778: free(fp);
1.134 markus 1779: }
1.511 markus 1780: accumulate_host_timing_secret(cfg, NULL);
1.472 markus 1781: if (!sensitive_data.have_ssh2_key) {
1.264 itojun 1782: logit("sshd: no hostkeys available -- exiting.");
1.64 markus 1783: exit(1);
1784: }
1785:
1.373 djm 1786: /*
1787: * Load certificates. They are stored in an array at identical
1788: * indices to the public keys that they relate to.
1789: */
1790: sensitive_data.host_certificates = xcalloc(options.num_host_key_files,
1.488 markus 1791: sizeof(struct sshkey *));
1.373 djm 1792: for (i = 0; i < options.num_host_key_files; i++)
1793: sensitive_data.host_certificates[i] = NULL;
1794:
1795: for (i = 0; i < options.num_host_cert_files; i++) {
1.430 djm 1796: if (options.host_cert_files[i] == NULL)
1797: continue;
1.512 markus 1798: if ((r = sshkey_load_public(options.host_cert_files[i],
1799: &key, NULL)) != 0) {
1.564 djm 1800: error_r(r, "Could not load host certificate \"%s\"",
1801: options.host_cert_files[i]);
1.373 djm 1802: continue;
1803: }
1.512 markus 1804: if (!sshkey_is_cert(key)) {
1.373 djm 1805: error("Certificate file is not a certificate: %s",
1806: options.host_cert_files[i]);
1.512 markus 1807: sshkey_free(key);
1.373 djm 1808: continue;
1809: }
1810: /* Find matching private key */
1811: for (j = 0; j < options.num_host_key_files; j++) {
1.512 markus 1812: if (sshkey_equal_public(key,
1.575 djm 1813: sensitive_data.host_pubkeys[j])) {
1.373 djm 1814: sensitive_data.host_certificates[j] = key;
1815: break;
1816: }
1817: }
1818: if (j >= options.num_host_key_files) {
1819: error("No matching private key for certificate: %s",
1820: options.host_cert_files[i]);
1.512 markus 1821: sshkey_free(key);
1.373 djm 1822: continue;
1823: }
1824: sensitive_data.host_certificates[j] = key;
1.493 djm 1825: debug("host certificate: #%u type %d %s", j, key->type,
1.512 markus 1826: sshkey_type(key));
1.373 djm 1827: }
1.426 markus 1828:
1.244 markus 1829: if (use_privsep) {
1830: struct stat st;
1831:
1.327 deraadt 1832: if (getpwnam(SSH_PRIVSEP_USER) == NULL)
1.244 markus 1833: fatal("Privilege separation user %s does not exist",
1834: SSH_PRIVSEP_USER);
1.500 djm 1835: endpwent();
1.244 markus 1836: if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
1837: (S_ISDIR(st.st_mode) == 0))
1838: fatal("Missing privilege separation directory: %s",
1.247 stevesk 1839: _PATH_PRIVSEP_CHROOT_DIR);
1840: if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
1.262 markus 1841: fatal("%s must be owned by root and not group or "
1842: "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
1.358 dtucker 1843: }
1844:
1845: if (test_flag > 1) {
1.499 djm 1846: /*
1847: * If no connection info was provided by -C then use
1848: * use a blank one that will cause no predicate to match.
1849: */
1850: if (connection_info == NULL)
1.523 djm 1851: connection_info = get_connection_info(ssh, 0, 0);
1.534 dtucker 1852: connection_info->test = 1;
1.546 djm 1853: parse_server_match_config(&options, &includes, connection_info);
1.358 dtucker 1854: dump_config(&options);
1.108 markus 1855: }
1.203 stevesk 1856:
1857: /* Configuration looks good, so exit if in test mode. */
1858: if (test_flag)
1859: exit(0);
1.108 markus 1860:
1.294 djm 1861: if (rexec_flag) {
1.493 djm 1862: if (rexec_argc < 0)
1863: fatal("rexec_argc %d < 0", rexec_argc);
1.329 djm 1864: rexec_argv = xcalloc(rexec_argc + 2, sizeof(char *));
1.493 djm 1865: for (i = 0; i < (u_int)rexec_argc; i++) {
1.294 djm 1866: debug("rexec_argv[%d]='%s'", i, saved_argv[i]);
1867: rexec_argv[i] = saved_argv[i];
1868: }
1869: rexec_argv[rexec_argc] = "-R";
1870: rexec_argv[rexec_argc + 1] = NULL;
1871: }
1.545 djm 1872: listener_proctitle = prepare_proctitle(ac, av);
1.362 dtucker 1873:
1874: /* Ensure that umask disallows at least group and world write */
1875: new_umask = umask(0077) | 0022;
1876: (void) umask(new_umask);
1.294 djm 1877:
1.108 markus 1878: /* Initialize the log (it is reinitialized below in case we forked). */
1.306 dtucker 1879: if (debug_flag && (!inetd_flag || rexeced_flag))
1.64 markus 1880: log_stderr = 1;
1.563 djm 1881: log_init(__progname, options.log_level,
1882: options.log_facility, log_stderr);
1883: for (i = 0; i < options.num_log_verbose; i++)
1884: log_verbose_add(options.log_verbose[i]);
1.64 markus 1885:
1.108 markus 1886: /*
1.478 dtucker 1887: * If not in debugging mode, not started from inetd and not already
1888: * daemonized (eg re-exec via SIGHUP), disconnect from the controlling
1889: * terminal, and fork. The original process exits.
1.108 markus 1890: */
1.478 dtucker 1891: already_daemon = daemonized();
1892: if (!(debug_flag || inetd_flag || no_daemon_flag || already_daemon)) {
1.345 djm 1893:
1.537 deraadt 1894: if (daemon(0, 0) == -1)
1.64 markus 1895: fatal("daemon() failed: %.200s", strerror(errno));
1896:
1.477 dtucker 1897: disconnect_controlling_tty();
1.64 markus 1898: }
1899: /* Reinitialize the log (because of the fork above). */
1.138 markus 1900: log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64 markus 1901:
1.572 djm 1902: /*
1903: * Chdir to the root directory so that the current disk can be
1904: * unmounted if desired.
1905: */
1.401 dtucker 1906: if (chdir("/") == -1)
1907: error("chdir(\"/\"): %s", strerror(errno));
1.217 deraadt 1908:
1.178 markus 1909: /* ignore SIGPIPE */
1.544 dtucker 1910: ssh_signal(SIGPIPE, SIG_IGN);
1.64 markus 1911:
1.345 djm 1912: /* Get a connection, either from inetd or a listening TCP socket */
1.64 markus 1913: if (inetd_flag) {
1.345 djm 1914: server_accept_inetd(&sock_in, &sock_out);
1.64 markus 1915: } else {
1.345 djm 1916: server_listen();
1.75 markus 1917:
1.544 dtucker 1918: ssh_signal(SIGHUP, sighup_handler);
1919: ssh_signal(SIGCHLD, main_sigchld_handler);
1920: ssh_signal(SIGTERM, sigterm_handler);
1921: ssh_signal(SIGQUIT, sigterm_handler);
1.201 markus 1922:
1.345 djm 1923: /*
1924: * Write out the pid file after the sigterm handler
1925: * is setup and the listen sockets are bound
1926: */
1.479 dtucker 1927: if (options.pid_file != NULL && !debug_flag) {
1.345 djm 1928: FILE *f = fopen(options.pid_file, "w");
1.201 markus 1929:
1.270 djm 1930: if (f == NULL) {
1931: error("Couldn't create pid file \"%s\": %s",
1932: options.pid_file, strerror(errno));
1933: } else {
1.245 mpech 1934: fprintf(f, "%ld\n", (long) getpid());
1.64 markus 1935: fclose(f);
1936: }
1937: }
1938:
1.345 djm 1939: /* Accept a connection and return in a forked child */
1940: server_accept_loop(&sock_in, &sock_out,
1941: &newsock, config_s);
1.1 deraadt 1942: }
1943:
1.64 markus 1944: /* This is the child processing a new connection. */
1.288 markus 1945: setproctitle("%s", "[accepted]");
1.294 djm 1946:
1.300 markus 1947: /*
1948: * Create a new session and process group since the 4.4BSD
1949: * setlogin() affects the entire process group. We don't
1950: * want the child to be able to affect the parent.
1951: */
1.537 deraadt 1952: if (!debug_flag && !inetd_flag && setsid() == -1)
1.300 markus 1953: error("setsid: %.100s", strerror(errno));
1954:
1.294 djm 1955: if (rexec_flag) {
1.296 djm 1956: debug("rexec start in %d out %d newsock %d pipe %d sock %d",
1957: sock_in, sock_out, newsock, startup_pipe, config_s[0]);
1.294 djm 1958: dup2(newsock, STDIN_FILENO);
1959: dup2(STDIN_FILENO, STDOUT_FILENO);
1960: if (startup_pipe == -1)
1.296 djm 1961: close(REEXEC_STARTUP_PIPE_FD);
1.407 djm 1962: else if (startup_pipe != REEXEC_STARTUP_PIPE_FD) {
1.296 djm 1963: dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD);
1.407 djm 1964: close(startup_pipe);
1965: startup_pipe = REEXEC_STARTUP_PIPE_FD;
1966: }
1.294 djm 1967:
1.296 djm 1968: dup2(config_s[1], REEXEC_CONFIG_PASS_FD);
1.294 djm 1969: close(config_s[1]);
1.296 djm 1970:
1.559 markus 1971: ssh_signal(SIGHUP, SIG_IGN); /* avoid reset to SIG_DFL */
1.294 djm 1972: execv(rexec_argv[0], rexec_argv);
1973:
1974: /* Reexec has failed, fall back and continue */
1975: error("rexec of %s failed: %s", rexec_argv[0], strerror(errno));
1.296 djm 1976: recv_rexec_state(REEXEC_CONFIG_PASS_FD, NULL);
1.294 djm 1977: log_init(__progname, options.log_level,
1978: options.log_facility, log_stderr);
1979:
1980: /* Clean up fds */
1.296 djm 1981: close(REEXEC_CONFIG_PASS_FD);
1982: newsock = sock_out = sock_in = dup(STDIN_FILENO);
1.562 djm 1983: if (stdfd_devnull(1, 1, 0) == -1)
1.564 djm 1984: error_f("stdfd_devnull failed");
1.296 djm 1985: debug("rexec cleanup in %d out %d newsock %d pipe %d sock %d",
1986: sock_in, sock_out, newsock, startup_pipe, config_s[0]);
1.294 djm 1987: }
1.372 djm 1988:
1989: /* Executed child processes don't need these. */
1990: fcntl(sock_out, F_SETFD, FD_CLOEXEC);
1991: fcntl(sock_in, F_SETFD, FD_CLOEXEC);
1.64 markus 1992:
1.551 dtucker 1993: /* We will not restart on SIGHUP since it no longer makes sense. */
1.544 dtucker 1994: ssh_signal(SIGALRM, SIG_DFL);
1995: ssh_signal(SIGHUP, SIG_DFL);
1996: ssh_signal(SIGTERM, SIG_DFL);
1997: ssh_signal(SIGQUIT, SIG_DFL);
1998: ssh_signal(SIGCHLD, SIG_DFL);
1.150 markus 1999:
1.66 markus 2000: /*
2001: * Register our connection. This turns encryption off because we do
2002: * not have a key.
2003: */
1.525 djm 2004: if ((ssh = ssh_packet_set_connection(NULL, sock_in, sock_out)) == NULL)
2005: fatal("Unable to create connection");
1.527 djm 2006: the_active_state = ssh;
1.525 djm 2007: ssh_packet_set_server(ssh);
1.492 djm 2008:
1.466 djm 2009: check_ip_options(ssh);
1.309 djm 2010:
1.492 djm 2011: /* Prepare the channels layer */
2012: channel_init_channels(ssh);
2013: channel_set_af(ssh, options.address_family);
2014: process_permitopen(ssh, &options);
2015:
1.309 djm 2016: /* Set SO_KEEPALIVE if requested. */
1.525 djm 2017: if (options.tcp_keep_alive && ssh_packet_connection_is_on_socket(ssh) &&
1.537 deraadt 2018: setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) == -1)
1.309 djm 2019: error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1.1 deraadt 2020:
1.466 djm 2021: if ((remote_port = ssh_remote_port(ssh)) < 0) {
2022: debug("ssh_remote_port failed");
1.310 markus 2023: cleanup_exit(255);
2024: }
1.316 dtucker 2025:
2026: /*
1.331 markus 2027: * The rest of the code depends on the fact that
1.466 djm 2028: * ssh_remote_ipaddr() caches the remote ip, even if
1.331 markus 2029: * the socket goes away.
2030: */
1.466 djm 2031: remote_ip = ssh_remote_ipaddr(ssh);
1.209 markus 2032:
1.496 djm 2033: rdomain = ssh_packet_rdomain_in(ssh);
2034:
1.64 markus 2035: /* Log the connection. */
1.447 dtucker 2036: laddr = get_local_ipaddr(sock_in);
1.497 djm 2037: verbose("Connection from %s port %d on %s port %d%s%s%s",
1.496 djm 2038: remote_ip, remote_port, laddr, ssh_local_port(ssh),
1.497 djm 2039: rdomain == NULL ? "" : " rdomain \"",
2040: rdomain == NULL ? "" : rdomain,
2041: rdomain == NULL ? "" : "\"");
1.447 dtucker 2042: free(laddr);
1.1 deraadt 2043:
1.66 markus 2044: /*
1.317 djm 2045: * We don't want to listen forever unless the other side
1.66 markus 2046: * successfully authenticates itself. So we set up an alarm which is
2047: * cleared after successful authentication. A limit of zero
1.317 djm 2048: * indicates no limit. Note that we don't set the alarm in debugging
1.66 markus 2049: * mode; it is just annoying to have the server exit just when you
2050: * are about to discover the bug.
2051: */
1.544 dtucker 2052: ssh_signal(SIGALRM, grace_alarm_handler);
1.64 markus 2053: if (!debug_flag)
2054: alarm(options.login_grace_time);
2055:
1.552 djm 2056: if ((r = kex_exchange_identification(ssh, -1,
2057: options.version_addendum)) != 0)
2058: sshpkt_fatal(ssh, r, "banner exchange");
1.520 djm 2059:
1.525 djm 2060: ssh_packet_set_nonblocking(ssh);
1.1 deraadt 2061:
1.278 markus 2062: /* allocate authentication context */
1.329 djm 2063: authctxt = xcalloc(1, sizeof(*authctxt));
1.525 djm 2064: ssh->authctxt = authctxt;
1.278 markus 2065:
2066: /* XXX global for cleanup, access from other modules */
2067: the_authctxt = authctxt;
1.506 djm 2068:
2069: /* Set default key authentication options */
2070: if ((auth_opts = sshauthopt_new_with_keys_defaults()) == NULL)
2071: fatal("allocation failed");
1.278 markus 2072:
1.307 otto 2073: /* prepare buffer to collect messages to display to user after login */
1.510 markus 2074: if ((loginmsg = sshbuf_new()) == NULL)
1.564 djm 2075: fatal_f("sshbuf_new failed");
1.374 dtucker 2076: auth_debug_reset();
1.307 otto 2077:
1.404 markus 2078: if (use_privsep) {
1.525 djm 2079: if (privsep_preauth(ssh) == 1)
1.237 markus 2080: goto authenticated;
1.472 markus 2081: } else if (have_agent) {
1.432 djm 2082: if ((r = ssh_get_authentication_socket(&auth_sock)) != 0) {
1.564 djm 2083: error_r(r, "Unable to get agent socket");
1.433 djm 2084: have_agent = 0;
1.432 djm 2085: }
2086: }
1.231 provos 2087:
1.77 markus 2088: /* perform the key exchange */
2089: /* authenticate user and start session */
1.525 djm 2090: do_ssh2_kex(ssh);
1.524 djm 2091: do_authentication2(ssh);
1.472 markus 2092:
1.237 markus 2093: /*
2094: * If we use privilege separation, the unprivileged child transfers
2095: * the current keystate and exits
2096: */
2097: if (use_privsep) {
1.527 djm 2098: mm_send_keystate(ssh, pmonitor);
1.525 djm 2099: ssh_packet_clear_keys(ssh);
1.231 provos 2100: exit(0);
1.237 markus 2101: }
1.231 provos 2102:
2103: authenticated:
1.318 djm 2104: /*
2105: * Cancel the alarm we set to limit the time taken for
2106: * authentication.
2107: */
2108: alarm(0);
1.544 dtucker 2109: ssh_signal(SIGALRM, SIG_DFL);
1.347 markus 2110: authctxt->authenticated = 1;
1.318 djm 2111: if (startup_pipe != -1) {
2112: close(startup_pipe);
2113: startup_pipe = -1;
2114: }
1.495 djm 2115:
2116: if (options.routing_domain != NULL)
2117: set_process_rdomain(ssh, options.routing_domain);
1.318 djm 2118:
1.234 markus 2119: /*
1.231 provos 2120: * In privilege separation, we fork another child and prepare
2121: * file descriptor passing.
2122: */
2123: if (use_privsep) {
1.525 djm 2124: privsep_postauth(ssh, authctxt);
1.237 markus 2125: /* the monitor process [priv] will not return */
1.231 provos 2126: }
1.360 dtucker 2127:
1.525 djm 2128: ssh_packet_set_timeout(ssh, options.client_alive_interval,
1.360 dtucker 2129: options.client_alive_count_max);
1.439 djm 2130:
2131: /* Try to send all our hostkeys to the client */
1.492 djm 2132: notify_hostkeys(ssh);
1.230 provos 2133:
1.278 markus 2134: /* Start session. */
1.492 djm 2135: do_authenticated(ssh, authctxt);
1.230 provos 2136:
1.64 markus 2137: /* The connection has been terminated. */
1.525 djm 2138: ssh_packet_get_bytes(ssh, &ibytes, &obytes);
1.381 djm 2139: verbose("Transferred: sent %llu, received %llu bytes",
2140: (unsigned long long)obytes, (unsigned long long)ibytes);
1.364 markus 2141:
2142: verbose("Closing connection to %.500s port %d", remote_ip, remote_port);
1.525 djm 2143: ssh_packet_close(ssh);
1.231 provos 2144:
2145: if (use_privsep)
2146: mm_terminate();
2147:
1.64 markus 2148: exit(0);
1.1 deraadt 2149: }
1.98 markus 2150:
1.435 markus 2151: int
1.527 djm 2152: sshd_hostkey_sign(struct ssh *ssh, struct sshkey *privkey,
2153: struct sshkey *pubkey, u_char **signature, size_t *slenp,
2154: const u_char *data, size_t dlen, const char *alg)
1.404 markus 2155: {
1.432 djm 2156: int r;
2157:
1.527 djm 2158: if (use_privsep) {
2159: if (privkey) {
2160: if (mm_sshkey_sign(ssh, privkey, signature, slenp,
1.561 djm 2161: data, dlen, alg, options.sk_provider, NULL,
1.542 djm 2162: ssh->compat) < 0)
1.564 djm 2163: fatal_f("privkey sign failed");
1.527 djm 2164: } else {
2165: if (mm_sshkey_sign(ssh, pubkey, signature, slenp,
1.561 djm 2166: data, dlen, alg, options.sk_provider, NULL,
1.542 djm 2167: ssh->compat) < 0)
1.564 djm 2168: fatal_f("pubkey sign failed");
1.527 djm 2169: }
1.404 markus 2170: } else {
1.527 djm 2171: if (privkey) {
2172: if (sshkey_sign(privkey, signature, slenp, data, dlen,
1.561 djm 2173: alg, options.sk_provider, NULL, ssh->compat) < 0)
1.564 djm 2174: fatal_f("privkey sign failed");
1.527 djm 2175: } else {
2176: if ((r = ssh_agent_sign(auth_sock, pubkey,
2177: signature, slenp, data, dlen, alg,
2178: ssh->compat)) != 0) {
1.564 djm 2179: fatal_fr(r, "agent sign failed");
1.527 djm 2180: }
2181: }
1.404 markus 2182: }
1.435 markus 2183: return 0;
1.404 markus 2184: }
2185:
1.452 djm 2186: /* SSH2 key exchange */
1.200 itojun 2187: static void
1.525 djm 2188: do_ssh2_kex(struct ssh *ssh)
1.98 markus 2189: {
1.422 markus 2190: char *myproposal[PROPOSAL_MAX] = { KEX_SERVER };
1.435 markus 2191: struct kex *kex;
1.437 markus 2192: int r;
1.102 markus 2193:
1.568 djm 2194: myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(ssh,
1.514 djm 2195: options.kex_algorithms);
1.568 djm 2196: myproposal[PROPOSAL_ENC_ALGS_CTOS] = compat_cipher_proposal(ssh,
1.514 djm 2197: options.ciphers);
1.568 djm 2198: myproposal[PROPOSAL_ENC_ALGS_STOC] = compat_cipher_proposal(ssh,
1.514 djm 2199: options.ciphers);
1.457 djm 2200: myproposal[PROPOSAL_MAC_ALGS_CTOS] =
2201: myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
2202:
1.312 markus 2203: if (options.compression == COMP_NONE) {
1.246 markus 2204: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.470 dtucker 2205: myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
1.102 markus 2206: }
1.400 dtucker 2207:
2208: if (options.rekey_limit || options.rekey_interval)
1.525 djm 2209: ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
1.481 dtucker 2210: options.rekey_interval);
1.327 deraadt 2211:
1.413 djm 2212: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal(
1.568 djm 2213: ssh, list_hostkey_types());
1.134 markus 2214:
1.189 markus 2215: /* start key exchange */
1.525 djm 2216: if ((r = kex_setup(ssh, myproposal)) != 0)
1.564 djm 2217: fatal_r(r, "kex_setup");
1.525 djm 2218: kex = ssh->kex;
1.426 markus 2219: #ifdef WITH_OPENSSL
1.532 djm 2220: kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
2221: kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
2222: kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
2223: kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
2224: kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
1.263 markus 2225: kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1.324 djm 2226: kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1.532 djm 2227: kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
1.426 markus 2228: #endif
1.532 djm 2229: kex->kex[KEX_C25519_SHA256] = kex_gen_server;
1.566 djm 2230: kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_server;
1.373 djm 2231: kex->load_host_public_key=&get_hostkey_public_by_type;
2232: kex->load_host_private_key=&get_hostkey_private_by_type;
1.231 provos 2233: kex->host_key_index=&get_hostkey_index;
1.404 markus 2234: kex->sign = sshd_hostkey_sign;
1.189 markus 2235:
1.525 djm 2236: ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &kex->done);
1.129 provos 2237:
2238: #ifdef DEBUG_KEXDH
2239: /* send 1st encrypted/maced/compressed message */
1.553 djm 2240: if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
2241: (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
2242: (r = sshpkt_send(ssh)) != 0 ||
2243: (r = ssh_packet_write_wait(ssh)) != 0)
1.564 djm 2244: fatal_fr(r, "send test");
1.129 provos 2245: #endif
1.186 markus 2246: debug("KEX done");
1.278 markus 2247: }
2248:
2249: /* server specific fatal cleanup */
2250: void
2251: cleanup_exit(int i)
2252: {
1.527 djm 2253: if (the_active_state != NULL && the_authctxt != NULL) {
2254: do_cleanup(the_active_state, the_authctxt);
1.423 djm 2255: if (use_privsep && privsep_is_preauth &&
2256: pmonitor != NULL && pmonitor->m_pid > 1) {
1.386 djm 2257: debug("Killing privsep child %d", pmonitor->m_pid);
2258: if (kill(pmonitor->m_pid, SIGKILL) != 0 &&
1.564 djm 2259: errno != ESRCH) {
2260: error_f("kill(%d): %s", pmonitor->m_pid,
2261: strerror(errno));
2262: }
1.386 djm 2263: }
2264: }
1.278 markus 2265: _exit(i);
1.1 deraadt 2266: }