jonathancammack / rpms / openssh

Forked from rpms/openssh 8 months ago
Clone

Blame SOURCES/openssh-8.0p1-sshd_include.patch

d60c81
diff -up openssh-8.0p1/auth.c.sshdinclude openssh-8.0p1/auth.c
d60c81
--- openssh-8.0p1/auth.c.sshdinclude	2021-10-20 15:18:49.740331098 +0200
d60c81
+++ openssh-8.0p1/auth.c	2021-10-20 15:19:41.324781344 +0200
d60c81
@@ -80,6 +80,7 @@
d60c81
 
d60c81
 /* import */
d60c81
 extern ServerOptions options;
d60c81
+extern struct include_list includes;
d60c81
 extern int use_privsep;
d60c81
 extern struct sshbuf *loginmsg;
d60c81
 extern struct passwd *privsep_pw;
d60c81
@@ -573,7 +574,7 @@ getpwnamallow(struct ssh *ssh, const cha
d60c81
 
d60c81
 	ci = get_connection_info(ssh, 1, options.use_dns);
d60c81
 	ci->user = user;
d60c81
-	parse_server_match_config(&options, ci);
d60c81
+	parse_server_match_config(&options, &includes, ci);
d60c81
 	log_change_level(options.log_level);
d60c81
 	process_permitopen(ssh, &options);
d60c81
 
d60c81
diff -up openssh-8.0p1/readconf.c.sshdinclude openssh-8.0p1/readconf.c
d60c81
--- openssh-8.0p1/readconf.c.sshdinclude	2021-10-20 15:21:43.541848103 +0200
d60c81
+++ openssh-8.0p1/readconf.c	2021-10-20 15:22:06.302046768 +0200
d60c81
@@ -711,7 +711,7 @@ match_cfg_line(Options *options, char **
d60c81
 static void
d60c81
 rm_env(Options *options, const char *arg, const char *filename, int linenum)
d60c81
 {
d60c81
-	int i, j;
d60c81
+	int i, j, onum_send_env = options->num_send_env;
d60c81
 	char *cp;
d60c81
 
d60c81
 	/* Remove an environment variable */
d60c81
@@ -734,6 +734,11 @@ rm_env(Options *options, const char *arg
d60c81
 		options->num_send_env--;
d60c81
 		/* NB. don't increment i */
d60c81
 	}
d60c81
+	if (onum_send_env != options->num_send_env) {
d60c81
+		options->send_env = xrecallocarray(options->send_env,
d60c81
+		    onum_send_env, options->num_send_env,
d60c81
+		    sizeof(*options->send_env));
d60c81
+	}
d60c81
 }
d60c81
 
d60c81
 /*
d60c81
diff -up openssh-8.0p1/regress/Makefile.sshdinclude openssh-8.0p1/regress/Makefile
d60c81
--- openssh-8.0p1/regress/Makefile.sshdinclude	2021-10-20 15:18:49.742331115 +0200
d60c81
+++ openssh-8.0p1/regress/Makefile	2021-10-20 15:19:41.324781344 +0200
d60c81
@@ -82,6 +82,7 @@ LTESTS= 	connect \
d60c81
 		principals-command \
d60c81
 		cert-file \
d60c81
 		cfginclude \
d60c81
+		servcfginclude \
d60c81
 		allow-deny-users \
d60c81
 		authinfo
d60c81
 
d60c81
@@ -118,7 +119,7 @@ CLEANFILES=	*.core actual agent-key.* au
d60c81
 		sftp-server.sh sftp.log ssh-log-wrapper.sh ssh.log \
d60c81
 		ssh_config ssh_config.* ssh_proxy ssh_proxy_bak \
d60c81
 		ssh_proxy_envpass sshd.log sshd_config sshd_config_minimal \
d60c81
-		sshd_config.orig sshd_proxy sshd_proxy.* sshd_proxy_bak \
d60c81
+		sshd_config.* sshd_proxy sshd_proxy.* sshd_proxy_bak \
d60c81
 		sshd_proxy_orig t10.out t10.out.pub t12.out t12.out.pub \
d60c81
 		t2.out t3.out t6.out1 t6.out2 t7.out t7.out.pub \
d60c81
 		t8.out t8.out.pub t9.out t9.out.pub testdata \
d60c81
diff -up openssh-8.0p1/regress/servcfginclude.sh.sshdinclude openssh-8.0p1/regress/servcfginclude.sh
d60c81
--- openssh-8.0p1/regress/servcfginclude.sh.sshdinclude	2021-10-20 15:18:49.744331132 +0200
d60c81
+++ openssh-8.0p1/regress/servcfginclude.sh	2021-10-20 15:22:06.303046777 +0200
d60c81
@@ -0,0 +1,188 @@
d60c81
+#	Placed in the Public Domain.
d60c81
+
d60c81
+tid="server config include"
d60c81
+
d60c81
+cat > $OBJ/sshd_config.i << _EOF
d60c81
+HostKey $OBJ/host.ssh-ed25519
d60c81
+Match host a
d60c81
+	Banner /aa
d60c81
+
d60c81
+Match host b
d60c81
+	Banner /bb
d60c81
+	Include $OBJ/sshd_config.i.*
d60c81
+
d60c81
+Match host c
d60c81
+	Include $OBJ/sshd_config.i.*
d60c81
+	Banner /cc
d60c81
+
d60c81
+Match host m
d60c81
+	Include $OBJ/sshd_config.i.*
d60c81
+
d60c81
+Match Host d
d60c81
+	Banner /dd
d60c81
+
d60c81
+Match Host e
d60c81
+	Banner /ee
d60c81
+	Include $OBJ/sshd_config.i.*
d60c81
+
d60c81
+Match Host f
d60c81
+	Include $OBJ/sshd_config.i.*
d60c81
+	Banner /ff
d60c81
+
d60c81
+Match Host n
d60c81
+	Include $OBJ/sshd_config.i.*
d60c81
+_EOF
d60c81
+
d60c81
+cat > $OBJ/sshd_config.i.0 << _EOF
d60c81
+Match host xxxxxx
d60c81
+_EOF
d60c81
+
d60c81
+cat > $OBJ/sshd_config.i.1 << _EOF
d60c81
+Match host a
d60c81
+	Banner /aaa
d60c81
+
d60c81
+Match host b
d60c81
+	Banner /bbb
d60c81
+
d60c81
+Match host c
d60c81
+	Banner /ccc
d60c81
+
d60c81
+Match Host d
d60c81
+	Banner /ddd
d60c81
+
d60c81
+Match Host e
d60c81
+	Banner /eee
d60c81
+
d60c81
+Match Host f
d60c81
+	Banner /fff
d60c81
+_EOF
d60c81
+
d60c81
+cat > $OBJ/sshd_config.i.2 << _EOF
d60c81
+Match host a
d60c81
+	Banner /aaaa
d60c81
+
d60c81
+Match host b
d60c81
+	Banner /bbbb
d60c81
+
d60c81
+Match host c
d60c81
+	Banner /cccc
d60c81
+
d60c81
+Match Host d
d60c81
+	Banner /dddd
d60c81
+
d60c81
+Match Host e
d60c81
+	Banner /eeee
d60c81
+
d60c81
+Match Host f
d60c81
+	Banner /ffff
d60c81
+
d60c81
+Match all
d60c81
+	Banner /xxxx
d60c81
+_EOF
d60c81
+
d60c81
+trial() {
d60c81
+	_host="$1"
d60c81
+	_exp="$2"
d60c81
+	_desc="$3"
d60c81
+	test -z "$_desc" && _desc="test match"
d60c81
+	trace "$_desc host=$_host expect=$_exp"
d60c81
+	${SUDO} ${REAL_SSHD} -f $OBJ/sshd_config.i -T \
d60c81
+	    -C "host=$_host,user=test,addr=127.0.0.1" > $OBJ/sshd_config.out ||
d60c81
+		fatal "ssh config parse failed: $_desc host=$_host expect=$_exp"
d60c81
+	_got=`grep -i '^banner ' $OBJ/sshd_config.out | awk '{print $2}'`
d60c81
+	if test "x$_exp" != "x$_got" ; then
d60c81
+		fail "$desc_ host $_host include fail: expected $_exp got $_got"
d60c81
+	fi
d60c81
+}
d60c81
+
d60c81
+trial a /aa
d60c81
+trial b /bb
d60c81
+trial c /ccc
d60c81
+trial d /dd
d60c81
+trial e /ee
d60c81
+trial f /fff
d60c81
+trial m /xxxx
d60c81
+trial n /xxxx
d60c81
+trial x none
d60c81
+
d60c81
+# Prepare an included config with an error.
d60c81
+
d60c81
+cat > $OBJ/sshd_config.i.3 << _EOF
d60c81
+Banner xxxx
d60c81
+	Junk
d60c81
+_EOF
d60c81
+
d60c81
+trace "disallow invalid config host=a"
d60c81
+${SUDO} ${REAL_SSHD} -f $OBJ/sshd_config.i \
d60c81
+    -C "host=a,user=test,addr=127.0.0.1" 2>/dev/null && \
d60c81
+	fail "sshd include allowed invalid config"
d60c81
+
d60c81
+trace "disallow invalid config host=x"
d60c81
+${SUDO} ${REAL_SSHD} -f $OBJ/sshd_config.i \
d60c81
+    -C "host=x,user=test,addr=127.0.0.1" 2>/dev/null && \
d60c81
+	fail "sshd include allowed invalid config"
d60c81
+
d60c81
+rm -f $OBJ/sshd_config.i.*
d60c81
+
d60c81
+# Ensure that a missing include is not fatal.
d60c81
+cat > $OBJ/sshd_config.i << _EOF
d60c81
+HostKey $OBJ/host.ssh-ed25519
d60c81
+Include $OBJ/sshd_config.i.*
d60c81
+Banner /aa
d60c81
+_EOF
d60c81
+
d60c81
+trial a /aa "missing include non-fatal"
d60c81
+
d60c81
+# Ensure that Match/Host in an included config does not affect parent.
d60c81
+cat > $OBJ/sshd_config.i.x << _EOF
d60c81
+Match host x
d60c81
+_EOF
d60c81
+
d60c81
+trial a /aa "included file does not affect match state"
d60c81
+
d60c81
+# Ensure the empty include directive is not accepted
d60c81
+cat > $OBJ/sshd_config.i.x << _EOF
d60c81
+Include
d60c81
+_EOF
d60c81
+
d60c81
+trace "disallow invalid with no argument"
d60c81
+${SUDO} ${REAL_SSHD} -f $OBJ/sshd_config.i.x -T \
d60c81
+    -C "host=x,user=test,addr=127.0.0.1" 2>/dev/null && \
d60c81
+	fail "sshd allowed Include with no argument"
d60c81
+
d60c81
+# Ensure the Include before any Match block works as expected (bug #3122)
d60c81
+cat > $OBJ/sshd_config.i << _EOF
d60c81
+Banner /xx
d60c81
+HostKey $OBJ/host.ssh-ed25519
d60c81
+Include $OBJ/sshd_config.i.2
d60c81
+Match host a
d60c81
+	Banner /aaaa
d60c81
+_EOF
d60c81
+cat > $OBJ/sshd_config.i.2 << _EOF
d60c81
+Match host a
d60c81
+	Banner /aa
d60c81
+_EOF
d60c81
+
d60c81
+trace "Include before match blocks"
d60c81
+trial a /aa "included file before match blocks is properly evaluated"
d60c81
+
d60c81
+# Port in included file is correctly interpretted (bug #3169)
d60c81
+cat > $OBJ/sshd_config.i << _EOF
d60c81
+Include $OBJ/sshd_config.i.2
d60c81
+Port 7722
d60c81
+_EOF
d60c81
+cat > $OBJ/sshd_config.i.2 << _EOF
d60c81
+HostKey $OBJ/host.ssh-ed25519
d60c81
+_EOF
d60c81
+
d60c81
+trace "Port after included files"
d60c81
+${SUDO} ${REAL_SSHD} -f $OBJ/sshd_config.i -T \
d60c81
+    -C "host=x,user=test,addr=127.0.0.1" > $OBJ/sshd_config.out || \
d60c81
+	fail "failed to parse Port after included files"
d60c81
+_port=`grep -i '^port ' $OBJ/sshd_config.out | awk '{print $2}'`
d60c81
+if test "x7722" != "x$_port" ; then
d60c81
+	fail "The Port in included file was intertepretted wrongly. Expected 7722, got $_port"
d60c81
+fi
d60c81
+
d60c81
+# cleanup
d60c81
+rm -f $OBJ/sshd_config.i $OBJ/sshd_config.i.* $OBJ/sshd_config.out
d60c81
diff -up openssh-8.0p1/regress/test-exec.sh.sshdinclude openssh-8.0p1/regress/test-exec.sh
d60c81
--- openssh-8.0p1/regress/test-exec.sh.sshdinclude	2021-10-20 15:18:49.746331150 +0200
d60c81
+++ openssh-8.0p1/regress/test-exec.sh	2021-10-20 15:19:41.324781344 +0200
d60c81
@@ -220,6 +220,7 @@ echo "exec ${SSH} -E${TEST_SSH_LOGFILE}
d60c81
 
d60c81
 chmod a+rx $OBJ/ssh-log-wrapper.sh
d60c81
 REAL_SSH="$SSH"
d60c81
+REAL_SSHD="$SSHD"
d60c81
 SSH="$SSHLOGWRAP"
d60c81
 
d60c81
 # Some test data.  We make a copy because some tests will overwrite it.
d60c81
diff -up openssh-8.0p1/servconf.c.sshdinclude openssh-8.0p1/servconf.c
d60c81
--- openssh-8.0p1/servconf.c.sshdinclude	2021-10-20 15:18:49.748331167 +0200
d60c81
+++ openssh-8.0p1/servconf.c	2021-10-20 15:22:06.303046777 +0200
d60c81
@@ -40,6 +40,11 @@
d60c81
 #ifdef HAVE_UTIL_H
d60c81
 #include <util.h>
d60c81
 #endif
d60c81
+#ifdef USE_SYSTEM_GLOB
d60c81
+# include <glob.h>
d60c81
+#else
d60c81
+# include "openbsd-compat/glob.h"
d60c81
+#endif
d60c81
 
d60c81
 #include "openbsd-compat/sys-queue.h"
d60c81
 #include "xmalloc.h"
d60c81
@@ -70,6 +75,9 @@ static void add_listen_addr(ServerOption
d60c81
     const char *, int);
d60c81
 static void add_one_listen_addr(ServerOptions *, const char *,
d60c81
     const char *, int);
d60c81
+static void parse_server_config_depth(ServerOptions *options,
d60c81
+    const char *filename, struct sshbuf *conf, struct include_list *includes,
d60c81
+    struct connection_info *connectinfo, int flags, int *activep, int depth);
d60c81
 
d60c81
 /* Use of privilege separation or not */
d60c81
 extern int use_privsep;
d60c81
@@ -528,7 +536,7 @@ typedef enum {
d60c81
 	sAcceptEnv, sSetEnv, sPermitTunnel,
d60c81
 	sMatch, sPermitOpen, sPermitListen, sForceCommand, sChrootDirectory,
d60c81
 	sUsePrivilegeSeparation, sAllowAgentForwarding,
d60c81
-	sHostCertificate,
d60c81
+	sHostCertificate, sInclude,
d60c81
 	sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
d60c81
 	sAuthorizedPrincipalsCommand, sAuthorizedPrincipalsCommandUser,
d60c81
 	sKexAlgorithms, sCASignatureAlgorithms, sIPQoS, sVersionAddendum,
d60c81
@@ -540,9 +548,11 @@ typedef enum {
d60c81
 	sDeprecated, sIgnore, sUnsupported
d60c81
 } ServerOpCodes;
d60c81
 
d60c81
-#define SSHCFG_GLOBAL	0x01	/* allowed in main section of sshd_config */
d60c81
-#define SSHCFG_MATCH	0x02	/* allowed inside a Match section */
d60c81
-#define SSHCFG_ALL	(SSHCFG_GLOBAL|SSHCFG_MATCH)
d60c81
+#define SSHCFG_GLOBAL		0x01	/* allowed in main section of config */
d60c81
+#define SSHCFG_MATCH		0x02	/* allowed inside a Match section */
d60c81
+#define SSHCFG_ALL		(SSHCFG_GLOBAL|SSHCFG_MATCH)
d60c81
+#define SSHCFG_NEVERMATCH	0x04  /* Match never matches; internal only */
d60c81
+#define SSHCFG_MATCH_ONLY	0x08  /* Match only in conditional blocks; internal only */
d60c81
 
d60c81
 /* Textual representation of the tokens. */
d60c81
 static struct {
d60c81
@@ -687,6 +697,7 @@ static struct {
d60c81
 	{ "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
d60c81
 	{ "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
d60c81
 	{ "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
d60c81
+	{ "include", sInclude, SSHCFG_ALL },
d60c81
 	{ "ipqos", sIPQoS, SSHCFG_ALL },
d60c81
 	{ "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
d60c81
 	{ "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
d60c81
@@ -1259,13 +1270,14 @@ static const struct multistate multistat
d60c81
 	{ NULL, -1 }
d60c81
 };
d60c81
 
d60c81
-int
d60c81
-process_server_config_line(ServerOptions *options, char *line,
d60c81
+static int
d60c81
+process_server_config_line_depth(ServerOptions *options, char *line,
d60c81
     const char *filename, int linenum, int *activep,
d60c81
-    struct connection_info *connectinfo)
d60c81
+    struct connection_info *connectinfo, int *inc_flags, int depth,
d60c81
+    struct include_list *includes)
d60c81
 {
d60c81
 	char ch, *cp, ***chararrayptr, **charptr, *arg, *arg2, *p;
d60c81
-	int cmdline = 0, *intptr, value, value2, n, port;
d60c81
+	int cmdline = 0, *intptr, value, value2, n, port, oactive, r, found;
d60c81
 	SyslogFacility *log_facility_ptr;
d60c81
 	LogLevel *log_level_ptr;
d60c81
 	ServerOpCodes opcode;
d60c81
@@ -1274,6 +1286,8 @@ process_server_config_line(ServerOptions
d60c81
 	long long val64;
d60c81
 	const struct multistate *multistate_ptr;
d60c81
 	const char *errstr;
d60c81
+	struct include_item *item;
d60c81
+	glob_t gbuf;
d60c81
 
d60c81
 	/* Strip trailing whitespace. Allow \f (form feed) at EOL only */
d60c81
 	if ((len = strlen(line)) == 0)
d60c81
@@ -1300,7 +1314,7 @@ process_server_config_line(ServerOptions
d60c81
 		cmdline = 1;
d60c81
 		activep = &cmdline;
d60c81
 	}
d60c81
-	if (*activep && opcode != sMatch)
d60c81
+	if (*activep && opcode != sMatch && opcode != sInclude)
d60c81
 		debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
d60c81
 	if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
d60c81
 		if (connectinfo == NULL) {
d60c81
@@ -1980,15 +1994,112 @@ process_server_config_line(ServerOptions
d60c81
 			*intptr = value;
d60c81
 		break;
d60c81
 
d60c81
+	case sInclude:
d60c81
+		if (cmdline) {
d60c81
+			fatal("Include directive not supported as a "
d60c81
+			    "command-line option");
d60c81
+		}
d60c81
+		value = 0;
d60c81
+		while ((arg2 = strdelim(&cp)) != NULL && *arg2 != '\0') {
d60c81
+			value++;
d60c81
+			found = 0;
d60c81
+			if (*arg2 != '/' && *arg2 != '~') {
d60c81
+				xasprintf(&arg, "%s/%s", SSHDIR, arg2);
d60c81
+			} else
d60c81
+				arg = xstrdup(arg2);
d60c81
+
d60c81
+			/*
d60c81
+			 * Don't let included files clobber the containing
d60c81
+			 * file's Match state.
d60c81
+			 */
d60c81
+			oactive = *activep;
d60c81
+
d60c81
+			/* consult cache of include files */
d60c81
+			TAILQ_FOREACH(item, includes, entry) {
d60c81
+				if (strcmp(item->selector, arg) != 0)
d60c81
+					continue;
d60c81
+				if (item->filename != NULL) {
d60c81
+					parse_server_config_depth(options,
d60c81
+					    item->filename, item->contents,
d60c81
+					    includes, connectinfo,
d60c81
+					    (*inc_flags & SSHCFG_MATCH_ONLY
d60c81
+					        ? SSHCFG_MATCH_ONLY : (oactive
d60c81
+					            ? 0 : SSHCFG_NEVERMATCH)),
d60c81
+					    activep, depth + 1);
d60c81
+				}
d60c81
+				found = 1;
d60c81
+				*activep = oactive;
d60c81
+			}
d60c81
+			if (found != 0) {
d60c81
+				free(arg);
d60c81
+				continue;
d60c81
+			}
d60c81
+
d60c81
+			/* requested glob was not in cache */
d60c81
+			debug2("%s line %d: new include %s",
d60c81
+			    filename, linenum, arg);
d60c81
+			if ((r = glob(arg, 0, NULL, &gbuf)) != 0) {
d60c81
+				if (r != GLOB_NOMATCH) {
d60c81
+					fatal("%s line %d: include \"%s\" "
d60c81
+					    "glob failed", filename,
d60c81
+					    linenum, arg);
d60c81
+				}
d60c81
+				/*
d60c81
+				 * If no entry matched then record a
d60c81
+				 * placeholder to skip later glob calls.
d60c81
+				 */
d60c81
+				debug2("%s line %d: no match for %s",
d60c81
+				    filename, linenum, arg);
d60c81
+				item = xcalloc(1, sizeof(*item));
d60c81
+				item->selector = strdup(arg);
d60c81
+				TAILQ_INSERT_TAIL(includes,
d60c81
+				    item, entry);
d60c81
+			}
d60c81
+			if (gbuf.gl_pathc > INT_MAX)
d60c81
+				fatal("%s: too many glob results", __func__);
d60c81
+			for (n = 0; n < (int)gbuf.gl_pathc; n++) {
d60c81
+				debug2("%s line %d: including %s",
d60c81
+				    filename, linenum, gbuf.gl_pathv[n]);
d60c81
+				item = xcalloc(1, sizeof(*item));
d60c81
+				item->selector = strdup(arg);
d60c81
+				item->filename = strdup(gbuf.gl_pathv[n]);
d60c81
+				if ((item->contents = sshbuf_new()) == NULL) {
d60c81
+					fatal("%s: sshbuf_new failed",
d60c81
+					    __func__);
d60c81
+				}
d60c81
+				load_server_config(item->filename,
d60c81
+				    item->contents);
d60c81
+				parse_server_config_depth(options,
d60c81
+				    item->filename, item->contents,
d60c81
+				    includes, connectinfo,
d60c81
+				    (*inc_flags & SSHCFG_MATCH_ONLY
d60c81
+				        ? SSHCFG_MATCH_ONLY : (oactive
d60c81
+				            ? 0 : SSHCFG_NEVERMATCH)),
d60c81
+				    activep, depth + 1);
d60c81
+				*activep = oactive;
d60c81
+				TAILQ_INSERT_TAIL(includes, item, entry);
d60c81
+			}
d60c81
+			globfree(&gbuf);
d60c81
+			free(arg);
d60c81
+		}
d60c81
+		if (value == 0) {
d60c81
+			fatal("%s line %d: Include missing filename argument",
d60c81
+			    filename, linenum);
d60c81
+		}
d60c81
+		break;
d60c81
+
d60c81
 	case sMatch:
d60c81
 		if (cmdline)
d60c81
 			fatal("Match directive not supported as a command-line "
d60c81
 			   "option");
d60c81
-		value = match_cfg_line(&cp, linenum, connectinfo);
d60c81
+		value = match_cfg_line(&cp, linenum,
d60c81
+		    (*inc_flags & SSHCFG_NEVERMATCH ? NULL : connectinfo));
d60c81
 		if (value < 0)
d60c81
 			fatal("%s line %d: Bad Match condition", filename,
d60c81
 			    linenum);
d60c81
-		*activep = value;
d60c81
+		*activep = (*inc_flags & SSHCFG_NEVERMATCH) ? 0 : value;
d60c81
+		/* The MATCH_ONLY is applicable only until the first match block */
d60c81
+		*inc_flags &= ~SSHCFG_MATCH_ONLY;
d60c81
 		break;
d60c81
 
d60c81
 	case sKerberosUseKuserok:
d60c81
@@ -2275,6 +2386,18 @@ process_server_config_line(ServerOptions
d60c81
 	return 0;
d60c81
 }
d60c81
 
d60c81
+int
d60c81
+process_server_config_line(ServerOptions *options, char *line,
d60c81
+    const char *filename, int linenum, int *activep,
d60c81
+    struct connection_info *connectinfo, struct include_list *includes)
d60c81
+{
d60c81
+	int inc_flags = 0;
d60c81
+
d60c81
+	return process_server_config_line_depth(options, line, filename,
d60c81
+	    linenum, activep, connectinfo, &inc_flags, 0, includes);
d60c81
+}
d60c81
+
d60c81
+
d60c81
 /* Reads the server configuration file. */
d60c81
 
d60c81
 void
d60c81
@@ -2313,12 +2436,13 @@ load_server_config(const char *filename,
d60c81
 
d60c81
 void
d60c81
 parse_server_match_config(ServerOptions *options,
d60c81
-   struct connection_info *connectinfo)
d60c81
+   struct include_list *includes, struct connection_info *connectinfo)
d60c81
 {
d60c81
 	ServerOptions mo;
d60c81
 
d60c81
 	initialize_server_options(&mo);
d60c81
-	parse_server_config(&mo, "reprocess config", cfg, connectinfo);
d60c81
+	parse_server_config(&mo, "reprocess config", cfg, includes,
d60c81
+	    connectinfo);
d60c81
 	copy_set_server_options(options, &mo, 0);
d60c81
 }
d60c81
 
d60c81
@@ -2464,28 +2588,44 @@ copy_set_server_options(ServerOptions *d
d60c81
 #undef M_CP_STROPT
d60c81
 #undef M_CP_STRARRAYOPT
d60c81
 
d60c81
-void
d60c81
-parse_server_config(ServerOptions *options, const char *filename,
d60c81
-    struct sshbuf *conf, struct connection_info *connectinfo)
d60c81
+#define SERVCONF_MAX_DEPTH	16
d60c81
+static void
d60c81
+parse_server_config_depth(ServerOptions *options, const char *filename,
d60c81
+    struct sshbuf *conf, struct include_list *includes,
d60c81
+    struct connection_info *connectinfo, int flags, int *activep, int depth)
d60c81
 {
d60c81
-	int active, linenum, bad_options = 0;
d60c81
+	int linenum, bad_options = 0;
d60c81
 	char *cp, *obuf, *cbuf;
d60c81
 
d60c81
-	debug2("%s: config %s len %zu", __func__, filename, sshbuf_len(conf));
d60c81
+	if (depth < 0 || depth > SERVCONF_MAX_DEPTH)
d60c81
+		fatal("Too many recursive configuration includes");
d60c81
+
d60c81
+	debug2("%s: config %s len %zu%s", __func__, filename, sshbuf_len(conf),
d60c81
+	    (flags & SSHCFG_NEVERMATCH ? " [checking syntax only]" : ""));
d60c81
 
d60c81
 	if ((obuf = cbuf = sshbuf_dup_string(conf)) == NULL)
d60c81
 		fatal("%s: sshbuf_dup_string failed", __func__);
d60c81
-	active = connectinfo ? 0 : 1;
d60c81
 	linenum = 1;
d60c81
 	while ((cp = strsep(&cbuf, "\n")) != NULL) {
d60c81
-		if (process_server_config_line(options, cp, filename,
d60c81
-		    linenum++, &active, connectinfo) != 0)
d60c81
+		if (process_server_config_line_depth(options, cp,
d60c81
+		    filename, linenum++, activep, connectinfo, &flags,
d60c81
+		    depth, includes) != 0)
d60c81
 			bad_options++;
d60c81
 	}
d60c81
 	free(obuf);
d60c81
 	if (bad_options > 0)
d60c81
 		fatal("%s: terminating, %d bad configuration options",
d60c81
 		    filename, bad_options);
d60c81
+}
d60c81
+
d60c81
+void
d60c81
+parse_server_config(ServerOptions *options, const char *filename,
d60c81
+    struct sshbuf *conf, struct include_list *includes,
d60c81
+    struct connection_info *connectinfo)
d60c81
+{
d60c81
+	int active = connectinfo ? 0 : 1;
d60c81
+	parse_server_config_depth(options, filename, conf, includes,
d60c81
+	    connectinfo, (connectinfo ? SSHCFG_MATCH_ONLY : 0), &active, 0);
d60c81
 	process_queued_listen_addrs(options);
d60c81
 }
d60c81
 
d60c81
diff -up openssh-8.0p1/servconf.h.sshdinclude openssh-8.0p1/servconf.h
d60c81
--- openssh-8.0p1/servconf.h.sshdinclude	2021-10-20 15:18:49.750331185 +0200
d60c81
+++ openssh-8.0p1/servconf.h	2021-10-20 15:19:41.325781353 +0200
d60c81
@@ -16,6 +16,8 @@
d60c81
 #ifndef SERVCONF_H
d60c81
 #define SERVCONF_H
d60c81
 
d60c81
+#include <sys/queue.h>
d60c81
+
d60c81
 #define MAX_PORTS		256	/* Max # ports. */
d60c81
 
d60c81
 #define MAX_SUBSYSTEMS		256	/* Max # subsystems. */
d60c81
@@ -234,6 +236,15 @@ struct connection_info {
d60c81
 				 * unspecified */
d60c81
 };
d60c81
 
d60c81
+/* List of included files for re-exec from the parsed configuration */
d60c81
+struct include_item {
d60c81
+	char *selector;
d60c81
+	char *filename;
d60c81
+	struct sshbuf *contents;
d60c81
+	TAILQ_ENTRY(include_item) entry;
d60c81
+};
d60c81
+TAILQ_HEAD(include_list, include_item);
d60c81
+
d60c81
 
d60c81
 /*
d60c81
  * These are string config options that must be copied between the
d60c81
@@ -273,12 +284,13 @@ struct connection_info *get_connection_i
d60c81
 void	 initialize_server_options(ServerOptions *);
d60c81
 void	 fill_default_server_options(ServerOptions *);
d60c81
 int	 process_server_config_line(ServerOptions *, char *, const char *, int,
d60c81
-	     int *, struct connection_info *);
d60c81
+	     int *, struct connection_info *, struct include_list *includes);
d60c81
 void	 process_permitopen(struct ssh *ssh, ServerOptions *options);
d60c81
 void	 load_server_config(const char *, struct sshbuf *);
d60c81
 void	 parse_server_config(ServerOptions *, const char *, struct sshbuf *,
d60c81
-	     struct connection_info *);
d60c81
-void	 parse_server_match_config(ServerOptions *, struct connection_info *);
d60c81
+	     struct include_list *includes, struct connection_info *);
d60c81
+void	 parse_server_match_config(ServerOptions *,
d60c81
+	     struct include_list *includes, struct connection_info *);
d60c81
 int	 parse_server_match_testspec(struct connection_info *, char *);
d60c81
 int	 server_match_spec_complete(struct connection_info *);
d60c81
 void	 copy_set_server_options(ServerOptions *, ServerOptions *, int);
d60c81
diff -up openssh-8.0p1/sshd_config.5.sshdinclude openssh-8.0p1/sshd_config.5
d60c81
--- openssh-8.0p1/sshd_config.5.sshdinclude	2021-10-20 15:18:49.754331220 +0200
d60c81
+++ openssh-8.0p1/sshd_config.5	2021-10-20 15:19:41.325781353 +0200
d60c81
@@ -825,7 +825,20 @@ during
d60c81
 and use only the system-wide known hosts file
d60c81
 .Pa /etc/ssh/known_hosts .
d60c81
 The default is
d60c81
-.Cm no .
d60c81
+.Dq no .
d60c81
+.It Cm Include
d60c81
+Include the specified configuration file(s).
d60c81
+Multiple path names may be specified and each pathname may contain
d60c81
+.Xr glob 7
d60c81
+wildcards.
d60c81
+Files without absolute paths are assumed to be in
d60c81
+.Pa /etc/ssh .
d60c81
+A
d60c81
+.Cm Include
d60c81
+directive may appear inside a
d60c81
+.Cm Match
d60c81
+block
d60c81
+to perform conditional inclusion.
d60c81
 .It Cm IPQoS
d60c81
 Specifies the IPv4 type-of-service or DSCP class for the connection.
d60c81
 Accepted values are
d60c81
diff -up openssh-8.0p1/sshd.c.sshdinclude openssh-8.0p1/sshd.c
d60c81
--- openssh-8.0p1/sshd.c.sshdinclude	2021-10-20 15:18:49.752331202 +0200
d60c81
+++ openssh-8.0p1/sshd.c	2021-10-20 15:19:41.325781353 +0200
d60c81
@@ -257,6 +257,9 @@ struct sshauthopt *auth_opts = NULL;
d60c81
 /* sshd_config buffer */
d60c81
 struct sshbuf *cfg;
d60c81
 
d60c81
+/* Included files from the configuration file */
d60c81
+struct include_list includes = TAILQ_HEAD_INITIALIZER(includes);
d60c81
+
d60c81
 /* message to be displayed after login */
d60c81
 struct sshbuf *loginmsg;
d60c81
 
d60c81
@@ -927,30 +930,45 @@ usage(void)
d60c81
 static void
d60c81
 send_rexec_state(int fd, struct sshbuf *conf)
d60c81
 {
d60c81
-	struct sshbuf *m;
d60c81
+	struct sshbuf *m = NULL, *inc = NULL;
d60c81
+	struct include_item *item = NULL;
d60c81
 	int r;
d60c81
 
d60c81
 	debug3("%s: entering fd = %d config len %zu", __func__, fd,
d60c81
 	    sshbuf_len(conf));
d60c81
 
d60c81
+	if ((m = sshbuf_new()) == NULL || (inc = sshbuf_new()) == NULL)
d60c81
+		fatal("%s: sshbuf_new failed", __func__);
d60c81
+
d60c81
+	/* pack includes into a string */
d60c81
+	TAILQ_FOREACH(item, &includes, entry) {
d60c81
+		if ((r = sshbuf_put_cstring(inc, item->selector)) != 0 ||
d60c81
+		    (r = sshbuf_put_cstring(inc, item->filename)) != 0 ||
d60c81
+		    (r = sshbuf_put_stringb(inc, item->contents)) != 0)
d60c81
+			fatal("%s: buffer error: %s", __func__, ssh_err(r));
d60c81
+	}
d60c81
+
d60c81
 	/*
d60c81
 	 * Protocol from reexec master to child:
d60c81
 	 *	string	configuration
d60c81
-	 *	string rngseed		(only if OpenSSL is not self-seeded)
d60c81
+	 *	string	included_files[] {
d60c81
+	 *		string	selector
d60c81
+	 *		string	filename
d60c81
+	 *		string	contents
d60c81
+	 *	}
d60c81
+	 *	string	rng_seed (if required)
d60c81
 	 */
d60c81
-	if ((m = sshbuf_new()) == NULL)
d60c81
-		fatal("%s: sshbuf_new failed", __func__);
d60c81
-	if ((r = sshbuf_put_stringb(m, conf)) != 0)
d60c81
+	if ((r = sshbuf_put_stringb(m, conf)) != 0 ||
d60c81
+	    (r = sshbuf_put_stringb(m, inc)) != 0)
d60c81
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
d60c81
-
d60c81
 #if defined(WITH_OPENSSL) && !defined(OPENSSL_PRNG_ONLY)
d60c81
 	rexec_send_rng_seed(m);
d60c81
 #endif
d60c81
-
d60c81
 	if (ssh_msg_send(fd, 0, m) == -1)
d60c81
 		fatal("%s: ssh_msg_send failed", __func__);
d60c81
 
d60c81
 	sshbuf_free(m);
d60c81
+	sshbuf_free(inc);
d60c81
 
d60c81
 	debug3("%s: done", __func__);
d60c81
 }
d60c81
@@ -958,14 +976,15 @@ send_rexec_state(int fd, struct sshbuf *
d60c81
 static void
d60c81
 recv_rexec_state(int fd, struct sshbuf *conf)
d60c81
 {
d60c81
-	struct sshbuf *m;
d60c81
+	struct sshbuf *m, *inc;
d60c81
 	u_char *cp, ver;
d60c81
 	size_t len;
d60c81
 	int r;
d60c81
+	struct include_item *item;
d60c81
 
d60c81
 	debug3("%s: entering fd = %d", __func__, fd);
d60c81
 
d60c81
-	if ((m = sshbuf_new()) == NULL)
d60c81
+	if ((m = sshbuf_new()) == NULL || (inc = sshbuf_new()) == NULL)
d60c81
 		fatal("%s: sshbuf_new failed", __func__);
d60c81
 	if (ssh_msg_recv(fd, m) == -1)
d60c81
 		fatal("%s: ssh_msg_recv failed", __func__);
d60c81
@@ -973,14 +992,28 @@ recv_rexec_state(int fd, struct sshbuf *
d60c81
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
d60c81
 	if (ver != 0)
d60c81
 		fatal("%s: rexec version mismatch", __func__);
d60c81
-	if ((r = sshbuf_get_string(m, &cp, &len)) != 0)
d60c81
-		fatal("%s: buffer error: %s", __func__, ssh_err(r));
d60c81
-	if (conf != NULL && (r = sshbuf_put(conf, cp, len)))
d60c81
+	if ((r = sshbuf_get_string(m, &cp, &len)) != 0 ||
d60c81
+	    (r = sshbuf_get_stringb(m, inc)) != 0)
d60c81
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
d60c81
+
d60c81
 #if defined(WITH_OPENSSL) && !defined(OPENSSL_PRNG_ONLY)
d60c81
 	rexec_recv_rng_seed(m);
d60c81
 #endif
d60c81
 
d60c81
+	if (conf != NULL && (r = sshbuf_put(conf, cp, len)))
d60c81
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
d60c81
+
d60c81
+	while (sshbuf_len(inc) != 0) {
d60c81
+		item = xcalloc(1, sizeof(*item));
d60c81
+		if ((item->contents = sshbuf_new()) == NULL)
d60c81
+			fatal("%s: sshbuf_new failed", __func__);
d60c81
+		if ((r = sshbuf_get_cstring(inc, &item->selector, NULL)) != 0 ||
d60c81
+		    (r = sshbuf_get_cstring(inc, &item->filename, NULL)) != 0 ||
d60c81
+		    (r = sshbuf_get_stringb(inc, item->contents)) != 0)
d60c81
+			fatal("%s: buffer error: %s", __func__, ssh_err(r));
d60c81
+		TAILQ_INSERT_TAIL(&includes, item, entry);
d60c81
+	}
d60c81
+
d60c81
 	free(cp);
d60c81
 	sshbuf_free(m);
d60c81
 
d60c81
@@ -1661,7 +1694,7 @@ main(int ac, char **av)
d60c81
 		case 'o':
d60c81
 			line = xstrdup(optarg);
d60c81
 			if (process_server_config_line(&options, line,
d60c81
-			    "command-line", 0, NULL, NULL) != 0)
d60c81
+			    "command-line", 0, NULL, NULL, &includes) != 0)
d60c81
 				exit(1);
d60c81
 			free(line);
d60c81
 			break;
d60c81
@@ -1692,7 +1725,7 @@ main(int ac, char **av)
d60c81
 	    SYSLOG_LEVEL_INFO : options.log_level,
d60c81
 	    options.log_facility == SYSLOG_FACILITY_NOT_SET ?
d60c81
 	    SYSLOG_FACILITY_AUTH : options.log_facility,
d60c81
-	    log_stderr || !inetd_flag);
d60c81
+	    log_stderr || !inetd_flag || debug_flag);
d60c81
 
d60c81
 	/*
d60c81
 	 * Unset KRB5CCNAME, otherwise the user's session may inherit it from
d60c81
@@ -1725,12 +1758,11 @@ main(int ac, char **av)
d60c81
 			 */
d60c81
 			(void)atomicio(vwrite, startup_pipe, "\0", 1);
d60c81
 		}
d60c81
-	}
d60c81
-	else if (strcasecmp(config_file_name, "none") != 0)
d60c81
+	} else if (strcasecmp(config_file_name, "none") != 0)
d60c81
 		load_server_config(config_file_name, cfg);
d60c81
 
d60c81
 	parse_server_config(&options, rexeced_flag ? "rexec" : config_file_name,
d60c81
-	    cfg, NULL);
d60c81
+	    cfg, &includes, NULL);
d60c81
 
d60c81
 	/* 'UsePAM no' is not supported in Fedora */
d60c81
 	if (! options.use_pam)
d60c81
@@ -1946,7 +1978,7 @@ main(int ac, char **av)
d60c81
 		if (connection_info == NULL)
d60c81
 			connection_info = get_connection_info(ssh, 0, 0);
d60c81
 		connection_info->test = 1;
d60c81
-		parse_server_match_config(&options, connection_info);
d60c81
+		parse_server_match_config(&options, &includes, connection_info);
d60c81
 		dump_config(&options);
d60c81
 	}
d60c81