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