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