vishalmishra434 / rpms / openssh

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