xref: /freebsd/crypto/openssh/sshd.c (revision 97ca2ada80b870edbbb4f66b26e274cf8e55e0bc)
1 /* $OpenBSD: sshd.c,v 1.612 2024/09/15 01:11:26 djm Exp $ */
2 /*
3  * Copyright (c) 2000, 2001, 2002 Markus Friedl.  All rights reserved.
4  * Copyright (c) 2002 Niels Provos.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "includes.h"
28 
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/mman.h>
32 #include <sys/socket.h>
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
35 #endif
36 #ifdef HAVE_SYS_TIME_H
37 # include <sys/time.h>
38 #endif
39 #include "openbsd-compat/sys-tree.h"
40 #include "openbsd-compat/sys-queue.h"
41 #include <sys/wait.h>
42 
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <netdb.h>
46 #ifdef HAVE_PATHS_H
47 #include <paths.h>
48 #endif
49 #include <grp.h>
50 #ifdef HAVE_POLL_H
51 #include <poll.h>
52 #endif
53 #include <pwd.h>
54 #include <signal.h>
55 #include <stdarg.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <limits.h>
61 
62 #ifdef WITH_OPENSSL
63 #include <openssl/evp.h>
64 #include <openssl/rand.h>
65 #include "openbsd-compat/openssl-compat.h"
66 #endif
67 
68 #ifdef HAVE_SECUREWARE
69 #include <sys/security.h>
70 #include <prot.h>
71 #endif
72 
73 #ifdef __FreeBSD__
74 #include <resolv.h>
75 #if defined(GSSAPI) && defined(HAVE_GSSAPI_GSSAPI_H)
76 #include <gssapi/gssapi.h>
77 #elif defined(GSSAPI) && defined(HAVE_GSSAPI_H)
78 #include <gssapi.h>
79 #endif
80 #endif
81 
82 #include "xmalloc.h"
83 #include "ssh.h"
84 #include "sshpty.h"
85 #include "log.h"
86 #include "sshbuf.h"
87 #include "misc.h"
88 #include "servconf.h"
89 #include "compat.h"
90 #include "digest.h"
91 #include "sshkey.h"
92 #include "authfile.h"
93 #include "pathnames.h"
94 #include "canohost.h"
95 #include "hostfile.h"
96 #include "auth.h"
97 #include "authfd.h"
98 #include "msg.h"
99 #include "version.h"
100 #include "ssherr.h"
101 #include "sk-api.h"
102 #include "addr.h"
103 #include "srclimit.h"
104 #include "blacklist_client.h"
105 
106 #ifdef LIBWRAP
107 #include <tcpd.h>
108 #include <syslog.h>
109 #endif /* LIBWRAP */
110 
111 /* Re-exec fds */
112 #define REEXEC_DEVCRYPTO_RESERVED_FD	(STDERR_FILENO + 1)
113 #define REEXEC_STARTUP_PIPE_FD		(STDERR_FILENO + 2)
114 #define REEXEC_CONFIG_PASS_FD		(STDERR_FILENO + 3)
115 #define REEXEC_MIN_FREE_FD		(STDERR_FILENO + 4)
116 
117 extern char *__progname;
118 
119 /* Server configuration options. */
120 ServerOptions options;
121 
122 /*
123  * Debug mode flag.  This can be set on the command line.  If debug
124  * mode is enabled, extra debugging output will be sent to the system
125  * log, the daemon will not go to background, and will exit after processing
126  * the first connection.
127  */
128 int debug_flag = 0;
129 
130 /* Saved arguments to main(). */
131 static char **saved_argv;
132 static int saved_argc;
133 
134 /*
135  * The sockets that the server is listening; this is used in the SIGHUP
136  * signal handler.
137  */
138 #define	MAX_LISTEN_SOCKS	16
139 static int listen_socks[MAX_LISTEN_SOCKS];
140 static int num_listen_socks = 0;
141 
142 /*
143  * Any really sensitive data in the application is contained in this
144  * structure. The idea is that this structure could be locked into memory so
145  * that the pages do not get written into swap.  However, there are some
146  * problems. The private key contains BIGNUMs, and we do not (in principle)
147  * have access to the internals of them, and locking just the structure is
148  * not very useful.  Currently, memory locking is not implemented.
149  */
150 struct {
151 	struct sshkey	**host_keys;		/* all private host keys */
152 	struct sshkey	**host_pubkeys;		/* all public host keys */
153 	struct sshkey	**host_certificates;	/* all public host certificates */
154 	int		have_ssh2_key;
155 } sensitive_data;
156 
157 /* This is set to true when a signal is received. */
158 static volatile sig_atomic_t received_siginfo = 0;
159 static volatile sig_atomic_t received_sigchld = 0;
160 static volatile sig_atomic_t received_sighup = 0;
161 static volatile sig_atomic_t received_sigterm = 0;
162 
163 /* record remote hostname or ip */
164 u_int utmp_len = HOST_NAME_MAX+1;
165 
166 /*
167  * The early_child/children array below is used for tracking children of the
168  * listening sshd process early in their lifespans, before they have
169  * completed authentication. This tracking is needed for four things:
170  *
171  * 1) Implementing the MaxStartups limit of concurrent unauthenticated
172  *    connections.
173  * 2) Avoiding a race condition for SIGHUP processing, where child processes
174  *    may have listen_socks open that could collide with main listener process
175  *    after it restarts.
176  * 3) Ensuring that rexec'd sshd processes have received their initial state
177  *    from the parent listen process before handling SIGHUP.
178  * 4) Tracking and logging unsuccessful exits from the preauth sshd monitor,
179  *    including and especially those for LoginGraceTime timeouts.
180  *
181  * Child processes signal that they have completed closure of the listen_socks
182  * and (if applicable) received their rexec state by sending a char over their
183  * sock.
184  *
185  * Child processes signal that authentication has completed by sending a
186  * second char over the socket before closing it, otherwise the listener will
187  * continue tracking the child (and using up a MaxStartups slot) until the
188  * preauth subprocess exits, whereupon the listener will log its exit status.
189  * preauth processes will exit with a status of EXIT_LOGIN_GRACE to indicate
190  * they did not authenticate before the LoginGraceTime alarm fired.
191  */
192 struct early_child {
193 	int pipefd;
194 	int early;		/* Indicates child closed listener */
195 	char *id;		/* human readable connection identifier */
196 	pid_t pid;
197 	struct xaddr addr;
198 	int have_addr;
199 	int status, have_status;
200 };
201 static struct early_child *children;
202 static int children_active;
203 static int startup_pipe = -1;		/* in child */
204 
205 /* sshd_config buffer */
206 struct sshbuf *cfg;
207 
208 /* Included files from the configuration file */
209 struct include_list includes = TAILQ_HEAD_INITIALIZER(includes);
210 
211 /* message to be displayed after login */
212 struct sshbuf *loginmsg;
213 
214 /* Unprivileged user */
215 struct passwd *privsep_pw = NULL;
216 
217 static char *listener_proctitle;
218 
219 /*
220  * Close all listening sockets
221  */
222 static void
223 close_listen_socks(void)
224 {
225 	int i;
226 
227 	for (i = 0; i < num_listen_socks; i++)
228 		close(listen_socks[i]);
229 	num_listen_socks = 0;
230 }
231 
232 /* Allocate and initialise the children array */
233 static void
234 child_alloc(void)
235 {
236 	int i;
237 
238 	children = xcalloc(options.max_startups, sizeof(*children));
239 	for (i = 0; i < options.max_startups; i++) {
240 		children[i].pipefd = -1;
241 		children[i].pid = -1;
242 	}
243 }
244 
245 /* Register a new connection in the children array; child pid comes later */
246 static struct early_child *
247 child_register(int pipefd, int sockfd)
248 {
249 	int i, lport, rport;
250 	char *laddr = NULL, *raddr = NULL;
251 	struct early_child *child = NULL;
252 	struct sockaddr_storage addr;
253 	socklen_t addrlen = sizeof(addr);
254 	struct sockaddr *sa = (struct sockaddr *)&addr;
255 
256 	for (i = 0; i < options.max_startups; i++) {
257 		if (children[i].pipefd != -1 || children[i].pid > 0)
258 			continue;
259 		child = &(children[i]);
260 		break;
261 	}
262 	if (child == NULL) {
263 		fatal_f("error: accepted connection when all %d child "
264 		    " slots full", options.max_startups);
265 	}
266 	child->pipefd = pipefd;
267 	child->early = 1;
268 	/* record peer address, if available */
269 	if (getpeername(sockfd, sa, &addrlen) == 0 &&
270 	   addr_sa_to_xaddr(sa, addrlen, &child->addr) == 0)
271 		child->have_addr = 1;
272 	/* format peer address string for logs */
273 	if ((lport = get_local_port(sockfd)) == 0 ||
274 	    (rport = get_peer_port(sockfd)) == 0) {
275 		/* Not a TCP socket */
276 		raddr = get_peer_ipaddr(sockfd);
277 		xasprintf(&child->id, "connection from %s", raddr);
278 	} else {
279 		laddr = get_local_ipaddr(sockfd);
280 		raddr = get_peer_ipaddr(sockfd);
281 		xasprintf(&child->id, "connection from %s to %s", raddr, laddr);
282 	}
283 	free(laddr);
284 	free(raddr);
285 	if (++children_active > options.max_startups)
286 		fatal_f("internal error: more children than max_startups");
287 
288 	return child;
289 }
290 
291 /*
292  * Finally free a child entry. Don't call this directly.
293  */
294 static void
295 child_finish(struct early_child *child)
296 {
297 	if (children_active == 0)
298 		fatal_f("internal error: children_active underflow");
299 	if (child->pipefd != -1)
300 		close(child->pipefd);
301 	free(child->id);
302 	memset(child, '\0', sizeof(*child));
303 	child->pipefd = -1;
304 	child->pid = -1;
305 	children_active--;
306 }
307 
308 /*
309  * Close a child's pipe. This will not stop tracking the child immediately
310  * (it will still be tracked for waitpid()) unless force_final is set, or
311  * child has already exited.
312  */
313 static void
314 child_close(struct early_child *child, int force_final, int quiet)
315 {
316 	if (!quiet)
317 		debug_f("enter%s", force_final ? " (forcing)" : "");
318 	if (child->pipefd != -1) {
319 		close(child->pipefd);
320 		child->pipefd = -1;
321 	}
322 	if (child->pid == -1 || force_final)
323 		child_finish(child);
324 }
325 
326 /* Record a child exit. Safe to call from signal handlers */
327 static void
328 child_exit(pid_t pid, int status)
329 {
330 	int i;
331 
332 	if (children == NULL || pid <= 0)
333 		return;
334 	for (i = 0; i < options.max_startups; i++) {
335 		if (children[i].pid == pid) {
336 			children[i].have_status = 1;
337 			children[i].status = status;
338 			break;
339 		}
340 	}
341 }
342 
343 /*
344  * Reap a child entry that has exited, as previously flagged
345  * using child_exit().
346  * Handles logging of exit condition and will finalise the child if its pipe
347  * had already been closed.
348  */
349 static void
350 child_reap(struct early_child *child)
351 {
352 	LogLevel level = SYSLOG_LEVEL_DEBUG1;
353 	int was_crash, penalty_type = SRCLIMIT_PENALTY_NONE;
354 
355 	/* Log exit information */
356 	if (WIFSIGNALED(child->status)) {
357 		/*
358 		 * Increase logging for signals potentially associated
359 		 * with serious conditions.
360 		 */
361 		if ((was_crash = signal_is_crash(WTERMSIG(child->status))))
362 			level = SYSLOG_LEVEL_ERROR;
363 		do_log2(level, "session process %ld for %s killed by "
364 		    "signal %d%s", (long)child->pid, child->id,
365 		    WTERMSIG(child->status), child->early ? " (early)" : "");
366 		if (was_crash)
367 			penalty_type = SRCLIMIT_PENALTY_CRASH;
368 	} else if (!WIFEXITED(child->status)) {
369 		penalty_type = SRCLIMIT_PENALTY_CRASH;
370 		error("session process %ld for %s terminated abnormally, "
371 		    "status=0x%x%s", (long)child->pid, child->id, child->status,
372 		    child->early ? " (early)" : "");
373 	} else {
374 		/* Normal exit. We care about the status */
375 		switch (WEXITSTATUS(child->status)) {
376 		case 0:
377 			debug3_f("preauth child %ld for %s completed "
378 			    "normally %s", (long)child->pid, child->id,
379 			    child->early ? " (early)" : "");
380 			break;
381 		case EXIT_LOGIN_GRACE:
382 			penalty_type = SRCLIMIT_PENALTY_GRACE_EXCEEDED;
383 			logit("Timeout before authentication for %s, "
384 			    "pid = %ld%s", child->id, (long)child->pid,
385 			    child->early ? " (early)" : "");
386 			break;
387 		case EXIT_CHILD_CRASH:
388 			penalty_type = SRCLIMIT_PENALTY_CRASH;
389 			logit("Session process %ld unpriv child crash for %s%s",
390 			    (long)child->pid, child->id,
391 			    child->early ? " (early)" : "");
392 			break;
393 		case EXIT_AUTH_ATTEMPTED:
394 			penalty_type = SRCLIMIT_PENALTY_AUTHFAIL;
395 			debug_f("preauth child %ld for %s exited "
396 			    "after unsuccessful auth attempt %s",
397 			    (long)child->pid, child->id,
398 			    child->early ? " (early)" : "");
399 			break;
400 		case EXIT_CONFIG_REFUSED:
401 			penalty_type = SRCLIMIT_PENALTY_REFUSECONNECTION;
402 			debug_f("preauth child %ld for %s prohibited by"
403 			    "RefuseConnection %s",
404 			    (long)child->pid, child->id,
405 			    child->early ? " (early)" : "");
406 			break;
407 		default:
408 			penalty_type = SRCLIMIT_PENALTY_NOAUTH;
409 			debug_f("preauth child %ld for %s exited "
410 			    "with status %d%s", (long)child->pid, child->id,
411 			    WEXITSTATUS(child->status),
412 			    child->early ? " (early)" : "");
413 			break;
414 		}
415 	}
416 
417 	if (child->have_addr)
418 		srclimit_penalise(&child->addr, penalty_type);
419 
420 	child->pid = -1;
421 	child->have_status = 0;
422 	if (child->pipefd == -1)
423 		child_finish(child);
424 }
425 
426 /* Reap all children that have exited; called after SIGCHLD */
427 static void
428 child_reap_all_exited(void)
429 {
430 	int i;
431 	pid_t pid;
432 	int status;
433 
434 	if (children == NULL)
435 		return;
436 
437 	for (;;) {
438 		if ((pid = waitpid(-1, &status, WNOHANG)) == 0)
439 			break;
440 		else if (pid == -1) {
441 			if (errno == EINTR || errno == EAGAIN)
442 				continue;
443 			if (errno != ECHILD)
444 				error_f("waitpid: %s", strerror(errno));
445 			break;
446 		}
447 		child_exit(pid, status);
448 	}
449 
450 	for (i = 0; i < options.max_startups; i++) {
451 		if (!children[i].have_status)
452 			continue;
453 		child_reap(&(children[i]));
454 	}
455 }
456 
457 static void
458 close_startup_pipes(void)
459 {
460 	int i;
461 
462 	if (children == NULL)
463 		return;
464 	for (i = 0; i < options.max_startups; i++) {
465 		if (children[i].pipefd != -1)
466 			child_close(&(children[i]), 1, 1);
467 	}
468 }
469 
470 /* Called after SIGINFO */
471 static void
472 show_info(void)
473 {
474 	int i;
475 
476 	/* XXX print listening sockets here too */
477 	if (children == NULL)
478 		return;
479 	logit("%d active startups", children_active);
480 	for (i = 0; i < options.max_startups; i++) {
481 		if (children[i].pipefd == -1 && children[i].pid <= 0)
482 			continue;
483 		logit("child %d: fd=%d pid=%ld %s%s", i, children[i].pipefd,
484 		    (long)children[i].pid, children[i].id,
485 		    children[i].early ? " (early)" : "");
486 	}
487 	srclimit_penalty_info();
488 }
489 
490 /*
491  * Signal handler for SIGHUP.  Sshd execs itself when it receives SIGHUP;
492  * the effect is to reread the configuration file (and to regenerate
493  * the server key).
494  */
495 
496 static void
497 sighup_handler(int sig)
498 {
499 	received_sighup = 1;
500 }
501 
502 /*
503  * Called from the main program after receiving SIGHUP.
504  * Restarts the server.
505  */
506 static void
507 sighup_restart(void)
508 {
509 	logit("Received SIGHUP; restarting.");
510 	if (options.pid_file != NULL)
511 		unlink(options.pid_file);
512 	platform_pre_restart();
513 	close_listen_socks();
514 	close_startup_pipes();
515 	ssh_signal(SIGHUP, SIG_IGN); /* will be restored after exec */
516 	execv(saved_argv[0], saved_argv);
517 	logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
518 	    strerror(errno));
519 	exit(1);
520 }
521 
522 /*
523  * Generic signal handler for terminating signals in the master daemon.
524  */
525 static void
526 sigterm_handler(int sig)
527 {
528 	received_sigterm = sig;
529 }
530 
531 #ifdef SIGINFO
532 static void
533 siginfo_handler(int sig)
534 {
535 	received_siginfo = 1;
536 }
537 #endif
538 
539 static void
540 main_sigchld_handler(int sig)
541 {
542 	received_sigchld = 1;
543 }
544 
545 /*
546  * returns 1 if connection should be dropped, 0 otherwise.
547  * dropping starts at connection #max_startups_begin with a probability
548  * of (max_startups_rate/100). the probability increases linearly until
549  * all connections are dropped for startups > max_startups
550  */
551 static int
552 should_drop_connection(int startups)
553 {
554 	int p, r;
555 
556 	if (startups < options.max_startups_begin)
557 		return 0;
558 	if (startups >= options.max_startups)
559 		return 1;
560 	if (options.max_startups_rate == 100)
561 		return 1;
562 
563 	p  = 100 - options.max_startups_rate;
564 	p *= startups - options.max_startups_begin;
565 	p /= options.max_startups - options.max_startups_begin;
566 	p += options.max_startups_rate;
567 	r = arc4random_uniform(100);
568 
569 	debug_f("p %d, r %d", p, r);
570 	return (r < p) ? 1 : 0;
571 }
572 
573 /*
574  * Check whether connection should be accepted by MaxStartups or for penalty.
575  * Returns 0 if the connection is accepted. If the connection is refused,
576  * returns 1 and attempts to send notification to client.
577  * Logs when the MaxStartups condition is entered or exited, and periodically
578  * while in that state.
579  */
580 static int
581 drop_connection(int sock, int startups, int notify_pipe)
582 {
583 	char *laddr, *raddr;
584 	const char *reason = NULL, msg[] = "Not allowed at this time\r\n";
585 	static time_t last_drop, first_drop;
586 	static u_int ndropped;
587 	LogLevel drop_level = SYSLOG_LEVEL_VERBOSE;
588 	time_t now;
589 
590 	if (!srclimit_penalty_check_allow(sock, &reason)) {
591 		drop_level = SYSLOG_LEVEL_INFO;
592 		goto handle;
593 	}
594 
595 	now = monotime();
596 	if (!should_drop_connection(startups) &&
597 	    srclimit_check_allow(sock, notify_pipe) == 1) {
598 		if (last_drop != 0 &&
599 		    startups < options.max_startups_begin - 1) {
600 			/* XXX maybe need better hysteresis here */
601 			logit("exited MaxStartups throttling after %s, "
602 			    "%u connections dropped",
603 			    fmt_timeframe(now - first_drop), ndropped);
604 			last_drop = 0;
605 		}
606 		return 0;
607 	}
608 
609 #define SSHD_MAXSTARTUPS_LOG_INTERVAL	(5 * 60)
610 	if (last_drop == 0) {
611 		error("beginning MaxStartups throttling");
612 		drop_level = SYSLOG_LEVEL_INFO;
613 		first_drop = now;
614 		ndropped = 0;
615 	} else if (last_drop + SSHD_MAXSTARTUPS_LOG_INTERVAL < now) {
616 		/* Periodic logs */
617 		error("in MaxStartups throttling for %s, "
618 		    "%u connections dropped",
619 		    fmt_timeframe(now - first_drop), ndropped + 1);
620 		drop_level = SYSLOG_LEVEL_INFO;
621 	}
622 	last_drop = now;
623 	ndropped++;
624 	reason = "past Maxstartups";
625 
626  handle:
627 	laddr = get_local_ipaddr(sock);
628 	raddr = get_peer_ipaddr(sock);
629 	do_log2(drop_level, "drop connection #%d from [%s]:%d on [%s]:%d %s",
630 	    startups,
631 	    raddr, get_peer_port(sock),
632 	    laddr, get_local_port(sock),
633 	    reason);
634 	free(laddr);
635 	free(raddr);
636 	/* best-effort notification to client */
637 	(void)write(sock, msg, sizeof(msg) - 1);
638 	return 1;
639 }
640 
641 static void
642 usage(void)
643 {
644 	if (options.version_addendum != NULL &&
645 	    *options.version_addendum != '\0')
646 		fprintf(stderr, "%s %s, %s\n",
647 		    SSH_RELEASE,
648 		    options.version_addendum, SSH_OPENSSL_VERSION);
649 	else
650 		fprintf(stderr, "%s, %s\n",
651 		    SSH_RELEASE, SSH_OPENSSL_VERSION);
652 	fprintf(stderr,
653 "usage: sshd [-46DdeGiqTtV] [-C connection_spec] [-c host_cert_file]\n"
654 "            [-E log_file] [-f config_file] [-g login_grace_time]\n"
655 "            [-h host_key_file] [-o option] [-p port] [-u len]\n"
656 	);
657 	exit(1);
658 }
659 
660 static struct sshbuf *
661 pack_hostkeys(void)
662 {
663 	struct sshbuf *keybuf = NULL, *hostkeys = NULL;
664 	int r;
665 	u_int i;
666 
667 	if ((keybuf = sshbuf_new()) == NULL ||
668 	    (hostkeys = sshbuf_new()) == NULL)
669 		fatal_f("sshbuf_new failed");
670 
671 	/* pack hostkeys into a string. Empty key slots get empty strings */
672 	for (i = 0; i < options.num_host_key_files; i++) {
673 		/* private key */
674 		sshbuf_reset(keybuf);
675 		if (sensitive_data.host_keys[i] != NULL &&
676 		    (r = sshkey_private_serialize(sensitive_data.host_keys[i],
677 		    keybuf)) != 0)
678 			fatal_fr(r, "serialize hostkey private");
679 		if ((r = sshbuf_put_stringb(hostkeys, keybuf)) != 0)
680 			fatal_fr(r, "compose hostkey private");
681 		/* public key */
682 		if (sensitive_data.host_pubkeys[i] != NULL) {
683 			if ((r = sshkey_puts(sensitive_data.host_pubkeys[i],
684 			    hostkeys)) != 0)
685 				fatal_fr(r, "compose hostkey public");
686 		} else {
687 			if ((r = sshbuf_put_string(hostkeys, NULL, 0)) != 0)
688 				fatal_fr(r, "compose hostkey empty public");
689 		}
690 		/* cert */
691 		if (sensitive_data.host_certificates[i] != NULL) {
692 			if ((r = sshkey_puts(
693 			    sensitive_data.host_certificates[i],
694 			    hostkeys)) != 0)
695 				fatal_fr(r, "compose host cert");
696 		} else {
697 			if ((r = sshbuf_put_string(hostkeys, NULL, 0)) != 0)
698 				fatal_fr(r, "compose host cert empty");
699 		}
700 	}
701 
702 	sshbuf_free(keybuf);
703 	return hostkeys;
704 }
705 
706 static void
707 send_rexec_state(int fd, struct sshbuf *conf)
708 {
709 	struct sshbuf *m = NULL, *inc = NULL, *hostkeys = NULL;
710 	struct include_item *item = NULL;
711 	int r, sz;
712 
713 	debug3_f("entering fd = %d config len %zu", fd,
714 	    sshbuf_len(conf));
715 
716 	if ((m = sshbuf_new()) == NULL ||
717 	    (inc = sshbuf_new()) == NULL)
718 		fatal_f("sshbuf_new failed");
719 
720 	/* pack includes into a string */
721 	TAILQ_FOREACH(item, &includes, entry) {
722 		if ((r = sshbuf_put_cstring(inc, item->selector)) != 0 ||
723 		    (r = sshbuf_put_cstring(inc, item->filename)) != 0 ||
724 		    (r = sshbuf_put_stringb(inc, item->contents)) != 0)
725 			fatal_fr(r, "compose includes");
726 	}
727 
728 	hostkeys = pack_hostkeys();
729 
730 	/*
731 	 * Protocol from reexec master to child:
732 	 *	string	configuration
733 	 *	uint64	timing_secret
734 	 *	string	host_keys[] {
735 	 *		string private_key
736 	 *		string public_key
737 	 *		string certificate
738 	 *	}
739 	 *	string	included_files[] {
740 	 *		string	selector
741 	 *		string	filename
742 	 *		string	contents
743 	 *	}
744 	 */
745 	if ((r = sshbuf_put_stringb(m, conf)) != 0 ||
746 	    (r = sshbuf_put_u64(m, options.timing_secret)) != 0 ||
747 	    (r = sshbuf_put_stringb(m, hostkeys)) != 0 ||
748 	    (r = sshbuf_put_stringb(m, inc)) != 0)
749 		fatal_fr(r, "compose config");
750 
751 	/* We need to fit the entire message inside the socket send buffer */
752 	sz = ROUNDUP(sshbuf_len(m) + 5, 16*1024);
753 	if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sz, sizeof sz) == -1)
754 		fatal_f("setsockopt SO_SNDBUF: %s", strerror(errno));
755 
756 	if (ssh_msg_send(fd, 0, m) == -1)
757 		error_f("ssh_msg_send failed");
758 
759 	sshbuf_free(m);
760 	sshbuf_free(inc);
761 	sshbuf_free(hostkeys);
762 
763 	debug3_f("done");
764 }
765 
766 /*
767  * Listen for TCP connections
768  */
769 static void
770 listen_on_addrs(struct listenaddr *la)
771 {
772 	int ret, listen_sock;
773 	struct addrinfo *ai;
774 	char ntop[NI_MAXHOST], strport[NI_MAXSERV];
775 
776 	for (ai = la->addrs; ai; ai = ai->ai_next) {
777 		if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
778 			continue;
779 		if (num_listen_socks >= MAX_LISTEN_SOCKS)
780 			fatal("Too many listen sockets. "
781 			    "Enlarge MAX_LISTEN_SOCKS");
782 		if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
783 		    ntop, sizeof(ntop), strport, sizeof(strport),
784 		    NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
785 			error("getnameinfo failed: %.100s",
786 			    ssh_gai_strerror(ret));
787 			continue;
788 		}
789 		/* Create socket for listening. */
790 		listen_sock = socket(ai->ai_family, ai->ai_socktype,
791 		    ai->ai_protocol);
792 		if (listen_sock == -1) {
793 			/* kernel may not support ipv6 */
794 			verbose("socket: %.100s", strerror(errno));
795 			continue;
796 		}
797 		if (set_nonblock(listen_sock) == -1) {
798 			close(listen_sock);
799 			continue;
800 		}
801 		if (fcntl(listen_sock, F_SETFD, FD_CLOEXEC) == -1) {
802 			verbose("socket: CLOEXEC: %s", strerror(errno));
803 			close(listen_sock);
804 			continue;
805 		}
806 		/* Socket options */
807 		set_reuseaddr(listen_sock);
808 		if (la->rdomain != NULL &&
809 		    set_rdomain(listen_sock, la->rdomain) == -1) {
810 			close(listen_sock);
811 			continue;
812 		}
813 
814 		/* Only communicate in IPv6 over AF_INET6 sockets. */
815 		if (ai->ai_family == AF_INET6)
816 			sock_set_v6only(listen_sock);
817 
818 		debug("Bind to port %s on %s.", strport, ntop);
819 
820 		/* Bind the socket to the desired port. */
821 		if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) == -1) {
822 			error("Bind to port %s on %s failed: %.200s.",
823 			    strport, ntop, strerror(errno));
824 			close(listen_sock);
825 			continue;
826 		}
827 		listen_socks[num_listen_socks] = listen_sock;
828 		num_listen_socks++;
829 
830 		/* Start listening on the port. */
831 		if (listen(listen_sock, SSH_LISTEN_BACKLOG) == -1)
832 			fatal("listen on [%s]:%s: %.100s",
833 			    ntop, strport, strerror(errno));
834 		logit("Server listening on %s port %s%s%s.",
835 		    ntop, strport,
836 		    la->rdomain == NULL ? "" : " rdomain ",
837 		    la->rdomain == NULL ? "" : la->rdomain);
838 	}
839 }
840 
841 static void
842 server_listen(void)
843 {
844 	u_int i;
845 
846 	/* Initialise per-source limit tracking. */
847 	srclimit_init(options.max_startups,
848 	    options.per_source_max_startups,
849 	    options.per_source_masklen_ipv4,
850 	    options.per_source_masklen_ipv6,
851 	    &options.per_source_penalty,
852 	    options.per_source_penalty_exempt);
853 
854 	for (i = 0; i < options.num_listen_addrs; i++) {
855 		listen_on_addrs(&options.listen_addrs[i]);
856 		freeaddrinfo(options.listen_addrs[i].addrs);
857 		free(options.listen_addrs[i].rdomain);
858 		memset(&options.listen_addrs[i], 0,
859 		    sizeof(options.listen_addrs[i]));
860 	}
861 	free(options.listen_addrs);
862 	options.listen_addrs = NULL;
863 	options.num_listen_addrs = 0;
864 
865 	if (!num_listen_socks)
866 		fatal("Cannot bind any address.");
867 }
868 
869 /*
870  * The main TCP accept loop. Note that, for the non-debug case, returns
871  * from this function are in a forked subprocess.
872  */
873 static void
874 server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s,
875     int log_stderr)
876 {
877 	struct pollfd *pfd = NULL;
878 	int i, ret, npfd;
879 	int oactive = -1, listening = 0, lameduck = 0;
880 	int startup_p[2] = { -1 , -1 }, *startup_pollfd;
881 	char c = 0;
882 	struct sockaddr_storage from;
883 	struct early_child *child;
884 	socklen_t fromlen;
885 	u_char rnd[256];
886 	sigset_t nsigset, osigset;
887 #ifdef LIBWRAP
888 	struct request_info req;
889 
890 	request_init(&req, RQ_DAEMON, __progname, 0);
891 #endif
892 
893 	/* pipes connected to unauthenticated child sshd processes */
894 	child_alloc();
895 	startup_pollfd = xcalloc(options.max_startups, sizeof(int));
896 
897 	/*
898 	 * Prepare signal mask that we use to block signals that might set
899 	 * received_sigterm/hup/chld/info, so that we are guaranteed
900 	 * to immediately wake up the ppoll if a signal is received after
901 	 * the flag is checked.
902 	 */
903 	sigemptyset(&nsigset);
904 	sigaddset(&nsigset, SIGHUP);
905 	sigaddset(&nsigset, SIGCHLD);
906 #ifdef SIGINFO
907 	sigaddset(&nsigset, SIGINFO);
908 #endif
909 	sigaddset(&nsigset, SIGTERM);
910 	sigaddset(&nsigset, SIGQUIT);
911 
912 	/* sized for worst-case */
913 	pfd = xcalloc(num_listen_socks + options.max_startups,
914 	    sizeof(struct pollfd));
915 
916 	/*
917 	 * Stay listening for connections until the system crashes or
918 	 * the daemon is killed with a signal.
919 	 */
920 	for (;;) {
921 		sigprocmask(SIG_BLOCK, &nsigset, &osigset);
922 		if (received_sigterm) {
923 			logit("Received signal %d; terminating.",
924 			    (int) received_sigterm);
925 			close_listen_socks();
926 			if (options.pid_file != NULL)
927 				unlink(options.pid_file);
928 			exit(received_sigterm == SIGTERM ? 0 : 255);
929 		}
930 		if (received_sigchld) {
931 			child_reap_all_exited();
932 			received_sigchld = 0;
933 		}
934 		if (received_siginfo) {
935 			show_info();
936 			received_siginfo = 0;
937 		}
938 		if (oactive != children_active) {
939 			setproctitle("%s [listener] %d of %d-%d startups",
940 			    listener_proctitle, children_active,
941 			    options.max_startups_begin, options.max_startups);
942 			oactive = children_active;
943 		}
944 		if (received_sighup) {
945 			if (!lameduck) {
946 				debug("Received SIGHUP; waiting for children");
947 				close_listen_socks();
948 				lameduck = 1;
949 			}
950 			if (listening <= 0) {
951 				sigprocmask(SIG_SETMASK, &osigset, NULL);
952 				sighup_restart();
953 			}
954 		}
955 
956 		for (i = 0; i < num_listen_socks; i++) {
957 			pfd[i].fd = listen_socks[i];
958 			pfd[i].events = POLLIN;
959 		}
960 		npfd = num_listen_socks;
961 		for (i = 0; i < options.max_startups; i++) {
962 			startup_pollfd[i] = -1;
963 			if (children[i].pipefd != -1) {
964 				pfd[npfd].fd = children[i].pipefd;
965 				pfd[npfd].events = POLLIN;
966 				startup_pollfd[i] = npfd++;
967 			}
968 		}
969 
970 		/* Wait until a connection arrives or a child exits. */
971 		ret = ppoll(pfd, npfd, NULL, &osigset);
972 		if (ret == -1 && errno != EINTR) {
973 			error("ppoll: %.100s", strerror(errno));
974 			if (errno == EINVAL)
975 				cleanup_exit(1); /* can't recover */
976 		}
977 		sigprocmask(SIG_SETMASK, &osigset, NULL);
978 		if (ret == -1)
979 			continue;
980 
981 		for (i = 0; i < options.max_startups; i++) {
982 			if (children[i].pipefd == -1 ||
983 			    startup_pollfd[i] == -1 ||
984 			    !(pfd[startup_pollfd[i]].revents & (POLLIN|POLLHUP)))
985 				continue;
986 			switch (read(children[i].pipefd, &c, sizeof(c))) {
987 			case -1:
988 				if (errno == EINTR || errno == EAGAIN)
989 					continue;
990 				if (errno != EPIPE) {
991 					error_f("startup pipe %d (fd=%d): "
992 					    "read %s", i, children[i].pipefd,
993 					    strerror(errno));
994 				}
995 				/* FALLTHROUGH */
996 			case 0:
997 				/* child exited preauth */
998 				if (children[i].early)
999 					listening--;
1000 				srclimit_done(children[i].pipefd);
1001 				child_close(&(children[i]), 0, 0);
1002 				break;
1003 			case 1:
1004 				if (children[i].early && c == '\0') {
1005 					/* child has finished preliminaries */
1006 					listening--;
1007 					children[i].early = 0;
1008 					debug2_f("child %lu for %s received "
1009 					    "config", (long)children[i].pid,
1010 					    children[i].id);
1011 				} else if (!children[i].early && c == '\001') {
1012 					/* child has completed auth */
1013 					debug2_f("child %lu for %s auth done",
1014 					    (long)children[i].pid,
1015 					    children[i].id);
1016 					child_close(&(children[i]), 1, 0);
1017 				} else {
1018 					error_f("unexpected message 0x%02x "
1019 					    "child %ld for %s in state %d",
1020 					    (int)c, (long)children[i].pid,
1021 					    children[i].id, children[i].early);
1022 				}
1023 				break;
1024 			}
1025 		}
1026 		for (i = 0; i < num_listen_socks; i++) {
1027 			if (!(pfd[i].revents & POLLIN))
1028 				continue;
1029 			fromlen = sizeof(from);
1030 			*newsock = accept(listen_socks[i],
1031 			    (struct sockaddr *)&from, &fromlen);
1032 			if (*newsock == -1) {
1033 				if (errno != EINTR && errno != EWOULDBLOCK &&
1034 				    errno != ECONNABORTED && errno != EAGAIN)
1035 					error("accept: %.100s",
1036 					    strerror(errno));
1037 				if (errno == EMFILE || errno == ENFILE)
1038 					usleep(100 * 1000);
1039 				continue;
1040 			}
1041 #ifdef LIBWRAP
1042 			/* Check whether logins are denied from this host. */
1043 			request_set(&req, RQ_FILE, *newsock,
1044 			    RQ_CLIENT_NAME, "", RQ_CLIENT_ADDR, "", 0);
1045 			sock_host(&req);
1046 			if (!hosts_access(&req)) {
1047 				const struct linger l = { .l_onoff = 1,
1048 				    .l_linger  = 0 };
1049 
1050 				(void )setsockopt(*newsock, SOL_SOCKET,
1051 				    SO_LINGER, &l, sizeof(l));
1052 				(void )close(*newsock);
1053 				/*
1054 				 * Mimic message from libwrap's refuse() as
1055 				 * precisely as we can afford.  The authentic
1056 				 * message prints the IP address and the
1057 				 * hostname it resolves to in parentheses.  If
1058 				 * the IP address cannot be resolved to a
1059 				 * hostname, the IP address will be repeated
1060 				 * in parentheses.  As name resolution in the
1061 				 * main server loop could stall, and logging
1062 				 * resolved names adds little or no value to
1063 				 * incident investigation, this implementation
1064 				 * only repeats the IP address in parentheses.
1065 				 * This should resemble librwap's refuse()
1066 				 * closely enough not to break auditing
1067 				 * software like sshguard or custom scripts.
1068 				 */
1069 				syslog(LOG_WARNING,
1070 				    "refused connect from %s (%s)",
1071 				    eval_hostaddr(req.client),
1072 				    eval_hostaddr(req.client));
1073 				debug("Connection refused by tcp wrapper");
1074 				continue;
1075 			}
1076 #endif /* LIBWRAP */
1077 			if (unset_nonblock(*newsock) == -1) {
1078 				close(*newsock);
1079 				continue;
1080 			}
1081 			if (pipe(startup_p) == -1) {
1082 				error_f("pipe(startup_p): %s", strerror(errno));
1083 				close(*newsock);
1084 				continue;
1085 			}
1086 			if (drop_connection(*newsock,
1087 			    children_active, startup_p[0])) {
1088 				close(*newsock);
1089 				close(startup_p[0]);
1090 				close(startup_p[1]);
1091 				continue;
1092 			}
1093 
1094 			if (socketpair(AF_UNIX,
1095 			    SOCK_STREAM, 0, config_s) == -1) {
1096 				error("reexec socketpair: %s",
1097 				    strerror(errno));
1098 				close(*newsock);
1099 				close(startup_p[0]);
1100 				close(startup_p[1]);
1101 				continue;
1102 			}
1103 
1104 			/*
1105 			 * Got connection.  Fork a child to handle it, unless
1106 			 * we are in debugging mode.
1107 			 */
1108 			if (debug_flag) {
1109 				/*
1110 				 * In debugging mode.  Close the listening
1111 				 * socket, and start processing the
1112 				 * connection without forking.
1113 				 */
1114 				debug("Server will not fork when running in debugging mode.");
1115 				close_listen_socks();
1116 				*sock_in = *newsock;
1117 				*sock_out = *newsock;
1118 				close(startup_p[0]);
1119 				close(startup_p[1]);
1120 				startup_pipe = -1;
1121 				send_rexec_state(config_s[0], cfg);
1122 				close(config_s[0]);
1123 				free(pfd);
1124 				return;
1125 			}
1126 
1127 			/*
1128 			 * Normal production daemon.  Fork, and have
1129 			 * the child process the connection. The
1130 			 * parent continues listening.
1131 			 */
1132 			platform_pre_fork();
1133 			listening++;
1134 			child = child_register(startup_p[0], *newsock);
1135 			if ((child->pid = fork()) == 0) {
1136 				/*
1137 				 * Child.  Close the listening and
1138 				 * max_startup sockets.  Start using
1139 				 * the accepted socket. Reinitialize
1140 				 * logging (since our pid has changed).
1141 				 * We return from this function to handle
1142 				 * the connection.
1143 				 */
1144 				platform_post_fork_child();
1145 				startup_pipe = startup_p[1];
1146 				close_startup_pipes();
1147 				close_listen_socks();
1148 				*sock_in = *newsock;
1149 				*sock_out = *newsock;
1150 				log_init(__progname,
1151 				    options.log_level,
1152 				    options.log_facility,
1153 				    log_stderr);
1154 				close(config_s[0]);
1155 				free(pfd);
1156 				return;
1157 			}
1158 
1159 			/* Parent.  Stay in the loop. */
1160 			platform_post_fork_parent(child->pid);
1161 			if (child->pid == -1)
1162 				error("fork: %.100s", strerror(errno));
1163 			else
1164 				debug("Forked child %ld.", (long)child->pid);
1165 
1166 			close(startup_p[1]);
1167 
1168 			close(config_s[1]);
1169 			send_rexec_state(config_s[0], cfg);
1170 			close(config_s[0]);
1171 			close(*newsock);
1172 
1173 			/*
1174 			 * Ensure that our random state differs
1175 			 * from that of the child
1176 			 */
1177 			arc4random_stir();
1178 			arc4random_buf(rnd, sizeof(rnd));
1179 #ifdef WITH_OPENSSL
1180 			RAND_seed(rnd, sizeof(rnd));
1181 			if ((RAND_bytes((u_char *)rnd, 1)) != 1)
1182 				fatal("%s: RAND_bytes failed", __func__);
1183 #endif
1184 			explicit_bzero(rnd, sizeof(rnd));
1185 		}
1186 	}
1187 }
1188 
1189 static void
1190 accumulate_host_timing_secret(struct sshbuf *server_cfg,
1191     struct sshkey *key)
1192 {
1193 	static struct ssh_digest_ctx *ctx;
1194 	u_char *hash;
1195 	size_t len;
1196 	struct sshbuf *buf;
1197 	int r;
1198 
1199 	if (ctx == NULL && (ctx = ssh_digest_start(SSH_DIGEST_SHA512)) == NULL)
1200 		fatal_f("ssh_digest_start");
1201 	if (key == NULL) { /* finalize */
1202 		/* add server config in case we are using agent for host keys */
1203 		if (ssh_digest_update(ctx, sshbuf_ptr(server_cfg),
1204 		    sshbuf_len(server_cfg)) != 0)
1205 			fatal_f("ssh_digest_update");
1206 		len = ssh_digest_bytes(SSH_DIGEST_SHA512);
1207 		hash = xmalloc(len);
1208 		if (ssh_digest_final(ctx, hash, len) != 0)
1209 			fatal_f("ssh_digest_final");
1210 		options.timing_secret = PEEK_U64(hash);
1211 		freezero(hash, len);
1212 		ssh_digest_free(ctx);
1213 		ctx = NULL;
1214 		return;
1215 	}
1216 	if ((buf = sshbuf_new()) == NULL)
1217 		fatal_f("could not allocate buffer");
1218 	if ((r = sshkey_private_serialize(key, buf)) != 0)
1219 		fatal_fr(r, "encode %s key", sshkey_ssh_name(key));
1220 	if (ssh_digest_update(ctx, sshbuf_ptr(buf), sshbuf_len(buf)) != 0)
1221 		fatal_f("ssh_digest_update");
1222 	sshbuf_reset(buf);
1223 	sshbuf_free(buf);
1224 }
1225 
1226 static char *
1227 prepare_proctitle(int ac, char **av)
1228 {
1229 	char *ret = NULL;
1230 	int i;
1231 
1232 	for (i = 0; i < ac; i++)
1233 		xextendf(&ret, " ", "%s", av[i]);
1234 	return ret;
1235 }
1236 
1237 static void
1238 print_config(struct connection_info *connection_info)
1239 {
1240 	connection_info->test = 1;
1241 	parse_server_match_config(&options, &includes, connection_info);
1242 	dump_config(&options);
1243 	exit(0);
1244 }
1245 
1246 /*
1247  * Main program for the daemon.
1248  */
1249 int
1250 main(int ac, char **av)
1251 {
1252 	extern char *optarg;
1253 	extern int optind;
1254 	int log_stderr = 0, inetd_flag = 0, test_flag = 0, no_daemon_flag = 0;
1255 	char *config_file_name = _PATH_SERVER_CONFIG_FILE;
1256 	int r, opt, do_dump_cfg = 0, keytype, already_daemon, have_agent = 0;
1257 	int sock_in = -1, sock_out = -1, newsock = -1, rexec_argc = 0;
1258 	int devnull, config_s[2] = { -1 , -1 }, have_connection_info = 0;
1259 	int need_chroot = 1;
1260 	char *fp, *line, *logfile = NULL, **rexec_argv = NULL;
1261 	struct stat sb;
1262 	u_int i, j;
1263 	mode_t new_umask;
1264 	struct sshkey *key;
1265 	struct sshkey *pubkey;
1266 	struct connection_info connection_info;
1267 	sigset_t sigmask;
1268 
1269 	memset(&connection_info, 0, sizeof(connection_info));
1270 #ifdef HAVE_SECUREWARE
1271 	(void)set_auth_parameters(ac, av);
1272 #endif
1273 	__progname = ssh_get_progname(av[0]);
1274 
1275 	sigemptyset(&sigmask);
1276 	sigprocmask(SIG_SETMASK, &sigmask, NULL);
1277 
1278 	/* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
1279 	saved_argc = ac;
1280 	rexec_argc = ac;
1281 	saved_argv = xcalloc(ac + 1, sizeof(*saved_argv));
1282 	for (i = 0; (int)i < ac; i++)
1283 		saved_argv[i] = xstrdup(av[i]);
1284 	saved_argv[i] = NULL;
1285 
1286 #ifndef HAVE_SETPROCTITLE
1287 	/* Prepare for later setproctitle emulation */
1288 	compat_init_setproctitle(ac, av);
1289 	av = saved_argv;
1290 #endif
1291 
1292 	if (geteuid() == 0 && setgroups(0, NULL) == -1)
1293 		debug("setgroups(): %.200s", strerror(errno));
1294 
1295 	/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1296 	sanitise_stdfd();
1297 
1298 	/* Initialize configuration options to their default values. */
1299 	initialize_server_options(&options);
1300 
1301 	/* Parse command-line arguments. */
1302 	while ((opt = getopt(ac, av,
1303 	    "C:E:b:c:f:g:h:k:o:p:u:46DGQRTdeiqrtV")) != -1) {
1304 		switch (opt) {
1305 		case '4':
1306 			options.address_family = AF_INET;
1307 			break;
1308 		case '6':
1309 			options.address_family = AF_INET6;
1310 			break;
1311 		case 'f':
1312 			config_file_name = optarg;
1313 			break;
1314 		case 'c':
1315 			servconf_add_hostcert("[command-line]", 0,
1316 			    &options, optarg);
1317 			break;
1318 		case 'd':
1319 			if (debug_flag == 0) {
1320 				debug_flag = 1;
1321 				options.log_level = SYSLOG_LEVEL_DEBUG1;
1322 			} else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
1323 				options.log_level++;
1324 			break;
1325 		case 'D':
1326 			no_daemon_flag = 1;
1327 			break;
1328 		case 'G':
1329 			do_dump_cfg = 1;
1330 			break;
1331 		case 'E':
1332 			logfile = optarg;
1333 			/* FALLTHROUGH */
1334 		case 'e':
1335 			log_stderr = 1;
1336 			break;
1337 		case 'i':
1338 			inetd_flag = 1;
1339 			break;
1340 		case 'r':
1341 			logit("-r option is deprecated");
1342 			break;
1343 		case 'R':
1344 			fatal("-R not supported here");
1345 			break;
1346 		case 'Q':
1347 			/* ignored */
1348 			break;
1349 		case 'q':
1350 			options.log_level = SYSLOG_LEVEL_QUIET;
1351 			break;
1352 		case 'b':
1353 			/* protocol 1, ignored */
1354 			break;
1355 		case 'p':
1356 			options.ports_from_cmdline = 1;
1357 			if (options.num_ports >= MAX_PORTS) {
1358 				fprintf(stderr, "too many ports.\n");
1359 				exit(1);
1360 			}
1361 			options.ports[options.num_ports++] = a2port(optarg);
1362 			if (options.ports[options.num_ports-1] <= 0) {
1363 				fprintf(stderr, "Bad port number.\n");
1364 				exit(1);
1365 			}
1366 			break;
1367 		case 'g':
1368 			if ((options.login_grace_time = convtime(optarg)) == -1) {
1369 				fprintf(stderr, "Invalid login grace time.\n");
1370 				exit(1);
1371 			}
1372 			break;
1373 		case 'k':
1374 			/* protocol 1, ignored */
1375 			break;
1376 		case 'h':
1377 			servconf_add_hostkey("[command-line]", 0,
1378 			    &options, optarg, 1);
1379 			break;
1380 		case 't':
1381 			test_flag = 1;
1382 			break;
1383 		case 'T':
1384 			test_flag = 2;
1385 			break;
1386 		case 'C':
1387 			if (parse_server_match_testspec(&connection_info,
1388 			    optarg) == -1)
1389 				exit(1);
1390 			have_connection_info = 1;
1391 			break;
1392 		case 'u':
1393 			utmp_len = (u_int)strtonum(optarg, 0, HOST_NAME_MAX+1+1, NULL);
1394 			if (utmp_len > HOST_NAME_MAX+1) {
1395 				fprintf(stderr, "Invalid utmp length.\n");
1396 				exit(1);
1397 			}
1398 			break;
1399 		case 'o':
1400 			line = xstrdup(optarg);
1401 			if (process_server_config_line(&options, line,
1402 			    "command-line", 0, NULL, NULL, &includes) != 0)
1403 				exit(1);
1404 			free(line);
1405 			break;
1406 		case 'V':
1407 			fprintf(stderr, "%s, %s\n",
1408 			    SSH_RELEASE, SSH_OPENSSL_VERSION);
1409 			exit(0);
1410 		default:
1411 			usage();
1412 			break;
1413 		}
1414 	}
1415 	if (!test_flag && !inetd_flag && !do_dump_cfg && !path_absolute(av[0]))
1416 		fatal("sshd requires execution with an absolute path");
1417 
1418 	closefrom(STDERR_FILENO + 1);
1419 
1420 	/* Reserve fds we'll need later for reexec things */
1421 	if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
1422 		fatal("open %s: %s", _PATH_DEVNULL, strerror(errno));
1423 	while (devnull < REEXEC_MIN_FREE_FD) {
1424 		if ((devnull = dup(devnull)) == -1)
1425 			fatal("dup %s: %s", _PATH_DEVNULL, strerror(errno));
1426 	}
1427 
1428 	seed_rng();
1429 
1430 	/* If requested, redirect the logs to the specified logfile. */
1431 	if (logfile != NULL) {
1432 		char *cp, pid_s[32];
1433 
1434 		snprintf(pid_s, sizeof(pid_s), "%ld", (unsigned long)getpid());
1435 		cp = percent_expand(logfile,
1436 		    "p", pid_s,
1437 		    "P", "sshd",
1438 		    (char *)NULL);
1439 		log_redirect_stderr_to(cp);
1440 		free(cp);
1441 	}
1442 
1443 	/*
1444 	 * Force logging to stderr until we have loaded the private host
1445 	 * key (unless started from inetd)
1446 	 */
1447 	log_init(__progname,
1448 	    options.log_level == SYSLOG_LEVEL_NOT_SET ?
1449 	    SYSLOG_LEVEL_INFO : options.log_level,
1450 	    options.log_facility == SYSLOG_FACILITY_NOT_SET ?
1451 	    SYSLOG_FACILITY_AUTH : options.log_facility,
1452 	    log_stderr || !inetd_flag || debug_flag);
1453 
1454 	/*
1455 	 * Unset KRB5CCNAME, otherwise the user's session may inherit it from
1456 	 * root's environment
1457 	 */
1458 	if (getenv("KRB5CCNAME") != NULL)
1459 		(void) unsetenv("KRB5CCNAME");
1460 
1461 	sensitive_data.have_ssh2_key = 0;
1462 
1463 	/*
1464 	 * If we're not doing an extended test do not silently ignore connection
1465 	 * test params.
1466 	 */
1467 	if (test_flag < 2 && have_connection_info)
1468 		fatal("Config test connection parameter (-C) provided without "
1469 		    "test mode (-T)");
1470 
1471 	/* Fetch our configuration */
1472 	if ((cfg = sshbuf_new()) == NULL)
1473 		fatal("sshbuf_new config failed");
1474 	if (strcasecmp(config_file_name, "none") != 0)
1475 		load_server_config(config_file_name, cfg);
1476 
1477 	parse_server_config(&options, config_file_name, cfg,
1478 	    &includes, NULL, 0);
1479 
1480 	/* Fill in default values for those options not explicitly set. */
1481 	fill_default_server_options(&options);
1482 
1483 	/* Check that options are sensible */
1484 	if (options.authorized_keys_command_user == NULL &&
1485 	    (options.authorized_keys_command != NULL &&
1486 	    strcasecmp(options.authorized_keys_command, "none") != 0))
1487 		fatal("AuthorizedKeysCommand set without "
1488 		    "AuthorizedKeysCommandUser");
1489 	if (options.authorized_principals_command_user == NULL &&
1490 	    (options.authorized_principals_command != NULL &&
1491 	    strcasecmp(options.authorized_principals_command, "none") != 0))
1492 		fatal("AuthorizedPrincipalsCommand set without "
1493 		    "AuthorizedPrincipalsCommandUser");
1494 
1495 	/*
1496 	 * Check whether there is any path through configured auth methods.
1497 	 * Unfortunately it is not possible to verify this generally before
1498 	 * daemonisation in the presence of Match blocks, but this catches
1499 	 * and warns for trivial misconfigurations that could break login.
1500 	 */
1501 	if (options.num_auth_methods != 0) {
1502 		for (i = 0; i < options.num_auth_methods; i++) {
1503 			if (auth2_methods_valid(options.auth_methods[i],
1504 			    1) == 0)
1505 				break;
1506 		}
1507 		if (i >= options.num_auth_methods)
1508 			fatal("AuthenticationMethods cannot be satisfied by "
1509 			    "enabled authentication methods");
1510 	}
1511 
1512 	/* Check that there are no remaining arguments. */
1513 	if (optind < ac) {
1514 		fprintf(stderr, "Extra argument %s.\n", av[optind]);
1515 		exit(1);
1516 	}
1517 
1518 	debug("sshd version %s, %s", SSH_VERSION, SSH_OPENSSL_VERSION);
1519 
1520 	if (do_dump_cfg)
1521 		print_config(&connection_info);
1522 
1523 	/* load host keys */
1524 	sensitive_data.host_keys = xcalloc(options.num_host_key_files,
1525 	    sizeof(struct sshkey *));
1526 	sensitive_data.host_pubkeys = xcalloc(options.num_host_key_files,
1527 	    sizeof(struct sshkey *));
1528 
1529 	if (options.host_key_agent) {
1530 		if (strcmp(options.host_key_agent, SSH_AUTHSOCKET_ENV_NAME))
1531 			setenv(SSH_AUTHSOCKET_ENV_NAME,
1532 			    options.host_key_agent, 1);
1533 		if ((r = ssh_get_authentication_socket(NULL)) == 0)
1534 			have_agent = 1;
1535 		else
1536 			error_r(r, "Could not connect to agent \"%s\"",
1537 			    options.host_key_agent);
1538 	}
1539 
1540 	for (i = 0; i < options.num_host_key_files; i++) {
1541 		int ll = options.host_key_file_userprovided[i] ?
1542 		    SYSLOG_LEVEL_ERROR : SYSLOG_LEVEL_DEBUG1;
1543 
1544 		if (options.host_key_files[i] == NULL)
1545 			continue;
1546 		if ((r = sshkey_load_private(options.host_key_files[i], "",
1547 		    &key, NULL)) != 0 && r != SSH_ERR_SYSTEM_ERROR)
1548 			do_log2_r(r, ll, "Unable to load host key \"%s\"",
1549 			    options.host_key_files[i]);
1550 		if (sshkey_is_sk(key) &&
1551 		    key->sk_flags & SSH_SK_USER_PRESENCE_REQD) {
1552 			debug("host key %s requires user presence, ignoring",
1553 			    options.host_key_files[i]);
1554 			key->sk_flags &= ~SSH_SK_USER_PRESENCE_REQD;
1555 		}
1556 		if (r == 0 && key != NULL &&
1557 		    (r = sshkey_shield_private(key)) != 0) {
1558 			do_log2_r(r, ll, "Unable to shield host key \"%s\"",
1559 			    options.host_key_files[i]);
1560 			sshkey_free(key);
1561 			key = NULL;
1562 		}
1563 		if ((r = sshkey_load_public(options.host_key_files[i],
1564 		    &pubkey, NULL)) != 0 && r != SSH_ERR_SYSTEM_ERROR)
1565 			do_log2_r(r, ll, "Unable to load host key \"%s\"",
1566 			    options.host_key_files[i]);
1567 		if (pubkey != NULL && key != NULL) {
1568 			if (!sshkey_equal(pubkey, key)) {
1569 				error("Public key for %s does not match "
1570 				    "private key", options.host_key_files[i]);
1571 				sshkey_free(pubkey);
1572 				pubkey = NULL;
1573 			}
1574 		}
1575 		if (pubkey == NULL && key != NULL) {
1576 			if ((r = sshkey_from_private(key, &pubkey)) != 0)
1577 				fatal_r(r, "Could not demote key: \"%s\"",
1578 				    options.host_key_files[i]);
1579 		}
1580 		if (pubkey != NULL && (r = sshkey_check_rsa_length(pubkey,
1581 		    options.required_rsa_size)) != 0) {
1582 			error_fr(r, "Host key %s", options.host_key_files[i]);
1583 			sshkey_free(pubkey);
1584 			sshkey_free(key);
1585 			continue;
1586 		}
1587 		sensitive_data.host_keys[i] = key;
1588 		sensitive_data.host_pubkeys[i] = pubkey;
1589 
1590 		if (key == NULL && pubkey != NULL && have_agent) {
1591 			debug("will rely on agent for hostkey %s",
1592 			    options.host_key_files[i]);
1593 			keytype = pubkey->type;
1594 		} else if (key != NULL) {
1595 			keytype = key->type;
1596 			accumulate_host_timing_secret(cfg, key);
1597 		} else {
1598 			do_log2(ll, "Unable to load host key: %s",
1599 			    options.host_key_files[i]);
1600 			sensitive_data.host_keys[i] = NULL;
1601 			sensitive_data.host_pubkeys[i] = NULL;
1602 			continue;
1603 		}
1604 
1605 		switch (keytype) {
1606 		case KEY_RSA:
1607 		case KEY_DSA:
1608 		case KEY_ECDSA:
1609 		case KEY_ED25519:
1610 		case KEY_ECDSA_SK:
1611 		case KEY_ED25519_SK:
1612 		case KEY_XMSS:
1613 			if (have_agent || key != NULL)
1614 				sensitive_data.have_ssh2_key = 1;
1615 			break;
1616 		}
1617 		if ((fp = sshkey_fingerprint(pubkey, options.fingerprint_hash,
1618 		    SSH_FP_DEFAULT)) == NULL)
1619 			fatal("sshkey_fingerprint failed");
1620 		debug("%s host key #%d: %s %s",
1621 		    key ? "private" : "agent", i, sshkey_ssh_name(pubkey), fp);
1622 		free(fp);
1623 	}
1624 	accumulate_host_timing_secret(cfg, NULL);
1625 	if (!sensitive_data.have_ssh2_key) {
1626 		logit("sshd: no hostkeys available -- exiting.");
1627 		exit(1);
1628 	}
1629 
1630 	/*
1631 	 * Load certificates. They are stored in an array at identical
1632 	 * indices to the public keys that they relate to.
1633 	 */
1634 	sensitive_data.host_certificates = xcalloc(options.num_host_key_files,
1635 	    sizeof(struct sshkey *));
1636 	for (i = 0; i < options.num_host_key_files; i++)
1637 		sensitive_data.host_certificates[i] = NULL;
1638 
1639 	for (i = 0; i < options.num_host_cert_files; i++) {
1640 		if (options.host_cert_files[i] == NULL)
1641 			continue;
1642 		if ((r = sshkey_load_public(options.host_cert_files[i],
1643 		    &key, NULL)) != 0) {
1644 			error_r(r, "Could not load host certificate \"%s\"",
1645 			    options.host_cert_files[i]);
1646 			continue;
1647 		}
1648 		if (!sshkey_is_cert(key)) {
1649 			error("Certificate file is not a certificate: %s",
1650 			    options.host_cert_files[i]);
1651 			sshkey_free(key);
1652 			continue;
1653 		}
1654 		/* Find matching private key */
1655 		for (j = 0; j < options.num_host_key_files; j++) {
1656 			if (sshkey_equal_public(key,
1657 			    sensitive_data.host_pubkeys[j])) {
1658 				sensitive_data.host_certificates[j] = key;
1659 				break;
1660 			}
1661 		}
1662 		if (j >= options.num_host_key_files) {
1663 			error("No matching private key for certificate: %s",
1664 			    options.host_cert_files[i]);
1665 			sshkey_free(key);
1666 			continue;
1667 		}
1668 		sensitive_data.host_certificates[j] = key;
1669 		debug("host certificate: #%u type %d %s", j, key->type,
1670 		    sshkey_type(key));
1671 	}
1672 
1673 	/* Ensure privsep directory is correctly configured. */
1674 	need_chroot = ((getuid() == 0 || geteuid() == 0) ||
1675 	    options.kerberos_authentication);
1676 	if ((getpwnam(SSH_PRIVSEP_USER)) == NULL && need_chroot) {
1677 		fatal("Privilege separation user %s does not exist",
1678 		    SSH_PRIVSEP_USER);
1679 	}
1680 	endpwent();
1681 
1682 	if (need_chroot) {
1683 		if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &sb) == -1) ||
1684 		    (S_ISDIR(sb.st_mode) == 0))
1685 			fatal("Missing privilege separation directory: %s",
1686 			    _PATH_PRIVSEP_CHROOT_DIR);
1687 #ifdef HAVE_CYGWIN
1688 		if (check_ntsec(_PATH_PRIVSEP_CHROOT_DIR) &&
1689 		    (sb.st_uid != getuid () ||
1690 		    (sb.st_mode & (S_IWGRP|S_IWOTH)) != 0))
1691 #else
1692 		if (sb.st_uid != 0 || (sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
1693 #endif
1694 			fatal("%s must be owned by root and not group or "
1695 			    "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
1696 	}
1697 
1698 	if (test_flag > 1)
1699 		print_config(&connection_info);
1700 
1701 	/* Configuration looks good, so exit if in test mode. */
1702 	if (test_flag)
1703 		exit(0);
1704 
1705 	/*
1706 	 * Clear out any supplemental groups we may have inherited.  This
1707 	 * prevents inadvertent creation of files with bad modes (in the
1708 	 * portable version at least, it's certainly possible for PAM
1709 	 * to create a file, and we can't control the code in every
1710 	 * module which might be used).
1711 	 */
1712 	if (setgroups(0, NULL) < 0)
1713 		debug("setgroups() failed: %.200s", strerror(errno));
1714 
1715 	/* Prepare arguments for sshd-session */
1716 	if (rexec_argc < 0)
1717 		fatal("rexec_argc %d < 0", rexec_argc);
1718 	rexec_argv = xcalloc(rexec_argc + 3, sizeof(char *));
1719 	/* Point to the sshd-session binary instead of sshd */
1720 	rexec_argv[0] = options.sshd_session_path;
1721 	for (i = 1; i < (u_int)rexec_argc; i++) {
1722 		debug("rexec_argv[%d]='%s'", i, saved_argv[i]);
1723 		rexec_argv[i] = saved_argv[i];
1724 	}
1725 	rexec_argv[rexec_argc++] = "-R";
1726 	rexec_argv[rexec_argc] = NULL;
1727 	if (stat(rexec_argv[0], &sb) != 0 || !(sb.st_mode & (S_IXOTH|S_IXUSR)))
1728 		fatal("%s does not exist or is not executable", rexec_argv[0]);
1729 	debug3("using %s for re-exec", rexec_argv[0]);
1730 
1731 	listener_proctitle = prepare_proctitle(ac, av);
1732 
1733 	/* Ensure that umask disallows at least group and world write */
1734 	new_umask = umask(0077) | 0022;
1735 	(void) umask(new_umask);
1736 
1737 	/* Initialize the log (it is reinitialized below in case we forked). */
1738 	if (debug_flag && !inetd_flag)
1739 		log_stderr = 1;
1740 	log_init(__progname, options.log_level,
1741 	    options.log_facility, log_stderr);
1742 	for (i = 0; i < options.num_log_verbose; i++)
1743 		log_verbose_add(options.log_verbose[i]);
1744 
1745 	/*
1746 	 * If not in debugging mode, not started from inetd and not already
1747 	 * daemonized (eg re-exec via SIGHUP), disconnect from the controlling
1748 	 * terminal, and fork.  The original process exits.
1749 	 */
1750 	already_daemon = daemonized();
1751 	if (!(debug_flag || inetd_flag || no_daemon_flag || already_daemon)) {
1752 
1753 		if (daemon(0, 0) == -1)
1754 			fatal("daemon() failed: %.200s", strerror(errno));
1755 
1756 		disconnect_controlling_tty();
1757 	}
1758 	/* Reinitialize the log (because of the fork above). */
1759 	log_init(__progname, options.log_level, options.log_facility, log_stderr);
1760 
1761 	/* Avoid killing the process in high-pressure swapping environments. */
1762 	if (!inetd_flag && madvise(NULL, 0, MADV_PROTECT) != 0)
1763 		debug("madvise(): %.200s", strerror(errno));
1764 
1765 	/*
1766 	 * Chdir to the root directory so that the current disk can be
1767 	 * unmounted if desired.
1768 	 */
1769 	if (chdir("/") == -1)
1770 		error("chdir(\"/\"): %s", strerror(errno));
1771 
1772 	/* ignore SIGPIPE */
1773 	ssh_signal(SIGPIPE, SIG_IGN);
1774 
1775 	/* Get a connection, either from inetd or a listening TCP socket */
1776 	if (inetd_flag) {
1777 		/* Send configuration to ancestor sshd-session process */
1778 		if (socketpair(AF_UNIX, SOCK_STREAM, 0, config_s) == -1)
1779 			fatal("socketpair: %s", strerror(errno));
1780 		send_rexec_state(config_s[0], cfg);
1781 		close(config_s[0]);
1782 	} else {
1783 		platform_pre_listen();
1784 		server_listen();
1785 
1786 		ssh_signal(SIGHUP, sighup_handler);
1787 		ssh_signal(SIGCHLD, main_sigchld_handler);
1788 		ssh_signal(SIGTERM, sigterm_handler);
1789 		ssh_signal(SIGQUIT, sigterm_handler);
1790 #ifdef SIGINFO
1791 		ssh_signal(SIGINFO, siginfo_handler);
1792 #endif
1793 
1794 		platform_post_listen();
1795 
1796 		/*
1797 		 * Write out the pid file after the sigterm handler
1798 		 * is setup and the listen sockets are bound
1799 		 */
1800 		if (options.pid_file != NULL && !debug_flag) {
1801 			FILE *f = fopen(options.pid_file, "w");
1802 
1803 			if (f == NULL) {
1804 				error("Couldn't create pid file \"%s\": %s",
1805 				    options.pid_file, strerror(errno));
1806 			} else {
1807 				fprintf(f, "%ld\n", (long) getpid());
1808 				fclose(f);
1809 			}
1810 		}
1811 
1812 		/* Accept a connection and return in a forked child */
1813 		server_accept_loop(&sock_in, &sock_out,
1814 		    &newsock, config_s, log_stderr);
1815 	}
1816 
1817 	/* This is the child processing a new connection. */
1818 	setproctitle("%s", "[accepted]");
1819 
1820 	/*
1821 	 * Create a new session and process group since the 4.4BSD
1822 	 * setlogin() affects the entire process group.  We don't
1823 	 * want the child to be able to affect the parent.
1824 	 */
1825 	if (!debug_flag && !inetd_flag && setsid() == -1)
1826 		error("setsid: %.100s", strerror(errno));
1827 
1828 	debug("rexec start in %d out %d newsock %d pipe %d sock %d/%d",
1829 	    sock_in, sock_out, newsock, startup_pipe, config_s[0], config_s[1]);
1830 	if (!inetd_flag) {
1831 		if (dup2(newsock, STDIN_FILENO) == -1)
1832 			fatal("dup2 stdin: %s", strerror(errno));
1833 		if (dup2(STDIN_FILENO, STDOUT_FILENO) == -1)
1834 			fatal("dup2 stdout: %s", strerror(errno));
1835 		if (newsock > STDOUT_FILENO)
1836 			close(newsock);
1837 	}
1838 	if (config_s[1] != REEXEC_CONFIG_PASS_FD) {
1839 		if (dup2(config_s[1], REEXEC_CONFIG_PASS_FD) == -1)
1840 			fatal("dup2 config_s: %s", strerror(errno));
1841 		close(config_s[1]);
1842 	}
1843 	if (startup_pipe == -1)
1844 		close(REEXEC_STARTUP_PIPE_FD);
1845 	else if (startup_pipe != REEXEC_STARTUP_PIPE_FD) {
1846 		if (dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD) == -1)
1847 			fatal("dup2 startup_p: %s", strerror(errno));
1848 		close(startup_pipe);
1849 	}
1850 	log_redirect_stderr_to(NULL);
1851 	closefrom(REEXEC_MIN_FREE_FD);
1852 
1853 	ssh_signal(SIGHUP, SIG_IGN); /* avoid reset to SIG_DFL */
1854 	execv(rexec_argv[0], rexec_argv);
1855 
1856 	fatal("rexec of %s failed: %s", rexec_argv[0], strerror(errno));
1857 #ifdef __FreeBSD__
1858 	/*
1859 	 * Initialize the resolver.  This may not happen automatically
1860 	 * before privsep chroot().
1861 	 */
1862 	if ((_res.options & RES_INIT) == 0) {
1863 		debug("res_init()");
1864 		res_init();
1865 	}
1866 #ifdef GSSAPI
1867 	/*
1868 	 * Force GSS-API to parse its configuration and load any
1869 	 * mechanism plugins.
1870 	 */
1871 	{
1872 		gss_OID_set mechs;
1873 		OM_uint32 minor_status;
1874 		gss_indicate_mechs(&minor_status, &mechs);
1875 		gss_release_oid_set(&minor_status, &mechs);
1876 	}
1877 #endif
1878 #endif
1879 
1880 
1881 	BLACKLIST_INIT();
1882 }
1883 
1884 /* server specific fatal cleanup */
1885 void
1886 cleanup_exit(int i)
1887 {
1888 	_exit(i);
1889 }
1890