Blame SOURCES/scap-security-guide-0.1.58-sshd_directory_config-PR_6926.patch

889f2b
From b951a896d3ef1e678e5d6b580521053e7a076ab0 Mon Sep 17 00:00:00 2001
889f2b
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
889f2b
Date: Thu, 29 Apr 2021 16:54:03 +0200
889f2b
Subject: [PATCH 1/6] Updated checks and remediations of the sshd template.
889f2b
889f2b
Configuration of sshd moves from one config file to a config directory.
889f2b
Therefore, checks should consider all those files, and the remediation should aim
889f2b
to deliver fixes to one of those files in the config directory.
889f2b
889f2b
Tests that interact with this behavior have been added and are applicable for Fedora and RHEL9 products.
889f2b
---
889f2b
 .../tests/commented.fail.sh                   |  7 ++
889f2b
 .../tests/conflict.fail.sh                    | 15 ++++
889f2b
 .../tests/correct_value_directory.pass.sh     | 14 ++++
889f2b
 shared/macros-bash.jinja                      |  9 +++
889f2b
 shared/macros-oval.jinja                      | 61 +++++++++++------
889f2b
 .../templates/sshd_lineinfile/bash.template   | 22 ++++++
889f2b
 .../templates/sshd_lineinfile/oval.template   | 68 +++++++++++++++++--
889f2b
 7 files changed, 168 insertions(+), 28 deletions(-)
889f2b
 create mode 100644 linux_os/guide/services/ssh/ssh_server/sshd_disable_pubkey_auth/tests/commented.fail.sh
889f2b
 create mode 100644 linux_os/guide/services/ssh/ssh_server/sshd_disable_pubkey_auth/tests/conflict.fail.sh
889f2b
 create mode 100644 linux_os/guide/services/ssh/ssh_server/sshd_disable_pubkey_auth/tests/correct_value_directory.pass.sh
889f2b
889f2b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_disable_pubkey_auth/tests/commented.fail.sh b/linux_os/guide/services/ssh/ssh_server/sshd_disable_pubkey_auth/tests/commented.fail.sh
889f2b
new file mode 100644
889f2b
index 00000000000..484c2165532
889f2b
--- /dev/null
889f2b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_disable_pubkey_auth/tests/commented.fail.sh
889f2b
@@ -0,0 +1,7 @@
889f2b
+#!/bin/bash
889f2b
+
889f2b
+if grep -q "^PubkeyAuthentication" /etc/ssh/sshd_config; then
889f2b
+	sed -i "s/^PubkeyAuthentication.*/# PubkeyAuthentication no/" /etc/ssh/sshd_config
889f2b
+else
889f2b
+	echo "# PubkeyAuthentication no" >> /etc/ssh/sshd_config
889f2b
+fi
889f2b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_disable_pubkey_auth/tests/conflict.fail.sh b/linux_os/guide/services/ssh/ssh_server/sshd_disable_pubkey_auth/tests/conflict.fail.sh
889f2b
new file mode 100644
889f2b
index 00000000000..177a99e0b82
889f2b
--- /dev/null
889f2b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_disable_pubkey_auth/tests/conflict.fail.sh
889f2b
@@ -0,0 +1,15 @@
889f2b
+#!/bin/bash
889f2b
+
889f2b
+# platform = Fedora,Red Hat Enterprise Linux 9
889f2b
+
889f2b
+mkdir -p /etc/ssh/sshd_config.d
889f2b
+touch /etc/ssh/sshd_config.d/nothing
889f2b
+
889f2b
+if grep -q "^PubkeyAuthentication" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/* ; then
889f2b
+	sed -i "s/^PubkeyAuthentication.*/# PubkeyAuthentication no/" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*
889f2b
+else
889f2b
+	echo "# PubkeyAuthentication no" >> /etc/ssh/sshd_config
889f2b
+fi
889f2b
+
889f2b
+echo "PubkeyAuthentication no" > /etc/ssh/sshd_config.d/good_config
889f2b
+echo "PubkeyAuthentication yes" > /etc/ssh/sshd_config.d/rogue_config
889f2b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_disable_pubkey_auth/tests/correct_value_directory.pass.sh b/linux_os/guide/services/ssh/ssh_server/sshd_disable_pubkey_auth/tests/correct_value_directory.pass.sh
889f2b
new file mode 100644
889f2b
index 00000000000..0aa2e775dbe
889f2b
--- /dev/null
889f2b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_disable_pubkey_auth/tests/correct_value_directory.pass.sh
889f2b
@@ -0,0 +1,14 @@
889f2b
+#!/bin/bash
889f2b
+
889f2b
+# platform = Fedora,Red Hat Enterprise Linux 9
889f2b
+
889f2b
+mkdir -p /etc/ssh/sshd_config.d
889f2b
+touch /etc/ssh/sshd_config.d/nothing
889f2b
+
889f2b
+if grep -q "^PubkeyAuthentication" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/* ; then
889f2b
+	sed -i "s/^PubkeyAuthentication.*/# PubkeyAuthentication no/" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*
889f2b
+else
889f2b
+	echo "# PubkeyAuthentication no" >> /etc/ssh/sshd_config
889f2b
+fi
889f2b
+
889f2b
+echo "PubkeyAuthentication no" > /etc/ssh/sshd_config.d/correct
889f2b
diff --git a/shared/macros-bash.jinja b/shared/macros-bash.jinja
889f2b
index 1cd2c62b5e0..b4518d83c19 100644
889f2b
--- a/shared/macros-bash.jinja
889f2b
+++ b/shared/macros-bash.jinja
889f2b
@@ -471,6 +471,15 @@ fi
889f2b
 LC_ALL=C sed -i "/{{{ regex }}}/{{{ modifier }}}" "{{{ path }}}"
889f2b
 {{%- endmacro -%}}
889f2b
 
889f2b
+{{%- macro lineinfile_absent_in_directory(dirname, regex, insensitive=true) -%}}
889f2b
+    {{%- if insensitive -%}}
889f2b
+        {{%- set modifier="Id" -%}}
889f2b
+    {{%- else -%}}
889f2b
+        {{%- set modifier="d" -%}}
889f2b
+    {{%- endif -%}}
889f2b
+LC_ALL=C sed -i "/{{{ regex }}}/{{{ modifier }}}" "{{{ dirname }}}"/*
889f2b
+{{%- endmacro -%}}
889f2b
+
889f2b
 {{%- macro lineinfile_present(path, line, insert_after="", insert_before="", insensitive=true) -%}}
889f2b
     {{%- if insensitive -%}}
889f2b
         {{%- set grep_args="-q -m 1 -i" -%}}
889f2b
diff --git a/shared/macros-oval.jinja b/shared/macros-oval.jinja
889f2b
index be2ac268206..d38db96d9e3 100644
889f2b
--- a/shared/macros-oval.jinja
889f2b
+++ b/shared/macros-oval.jinja
889f2b
@@ -92,15 +92,18 @@
889f2b
     - parameter (String): The parameter to be checked in the configuration file.
889f2b
     - missing_parameter_pass (boolean): If set, the check will also pass if the parameter is not present in the configuration file (default is applied).
889f2b
 #}}
889f2b
-{{%- macro oval_line_in_file_criterion(path='', parameter='', missing_parameter_pass=false) -%}}
889f2b
+{{%- macro oval_line_in_file_criterion(path='', parameter='', missing_parameter_pass=false, comment='', id_stem=rule_id) -%}}
889f2b
 {{%- set suffix_id = "" -%}}
889f2b
 {{%- set prefix_text = "Check the" -%}}
889f2b
 {{%- if missing_parameter_pass %}}
889f2b
 {{%- set suffix_id = suffix_id_default_not_overriden -%}}
889f2b
 {{%- set prefix_text = prefix_text + " absence of" -%}}
889f2b
 {{%- endif %}}
889f2b
-      
889f2b
-      test_ref="test_{{{ rule_id }}}{{{ suffix_id }}}" />
889f2b
+{{%- if not comment -%}}
889f2b
+{{%- set comment = prefix_text ~ " " ~ parameter ~ " in " ~ path -%}}
889f2b
+{{%- endif -%}}
889f2b
+
889f2b
+  test_ref="test_{{{ id_stem }}}{{{ suffix_id }}}" />
889f2b
 {{%- endmacro %}}
889f2b
 
889f2b
 {{#
889f2b
@@ -110,7 +113,7 @@
889f2b
     - parameter (String): The parameter to be checked in the configuration file.
889f2b
     - missing_parameter_pass (boolean): If set, the check will also pass if the parameter is not present in the configuration file (default is applied).
889f2b
 #}}
889f2b
-{{%- macro oval_line_in_file_test(path='', parameter='', missing_parameter_pass=false) -%}}
889f2b
+{{%- macro oval_line_in_file_test(path='', parameter='', missing_parameter_pass=false, id_stem=rule_id) -%}}
889f2b
 {{%- set suffix_id = "" -%}}
889f2b
 {{%- if missing_parameter_pass %}}
889f2b
 {{%- set check_existence = "none_exist" -%}}
889f2b
@@ -120,14 +123,14 @@
889f2b
 {{%- set check_existence = "all_exist" -%}}
889f2b
 {{%- set prefix_text = "value" -%}}
889f2b
 {{%- endif %}}
889f2b
-  
889f2b
+
889f2b
   comment="tests the {{{ prefix_text }}} of {{{ parameter }}} setting in the {{{ path }}} file"
889f2b
-  id="test_{{{ rule_id }}}{{{ suffix_id }}}" version="1">
889f2b
-  <ind:object object_ref="obj_{{{ rule_id }}}{{{ suffix_id }}}" />
889f2b
+  id="test_{{{ id_stem }}}{{{ suffix_id }}}" version="1">
889f2b
+  <ind:object object_ref="obj_{{{ id_stem }}}{{{ suffix_id }}}" />
889f2b
   {{%- if not missing_parameter_pass %}}
889f2b
-  <ind:state state_ref="state_{{{ rule_id }}}{{{ suffix_id }}}" />
889f2b
+  <ind:state state_ref="state_{{{ id_stem }}}{{{ suffix_id }}}" />
889f2b
   {{%- endif %}}
889f2b
-  </ind:textfilecontent54_test>
889f2b
+</ind:textfilecontent54_test>
889f2b
 {{%- endmacro %}}
889f2b
 
889f2b
 {{#
889f2b
@@ -141,7 +144,7 @@
889f2b
     - missing_parameter_pass (boolean): If set, the check will also pass if the parameter is not present in the configuration file (default is applied).
889f2b
     - multi_value (boolean): If set, it means that the parameter can accept multiple values and the expected value must be present in the current list of values.
889f2b
 #}}
889f2b
-{{%- macro oval_line_in_file_object(path='', section='', prefix_regex='^[ \\t]*', parameter='', separator_regex='[ \\t]+', missing_parameter_pass=false, multi_value=false, filepath_regex='') -%}}
889f2b
+{{%- macro oval_line_in_file_object(path='', section='', prefix_regex='^[ \\t]*', parameter='', separator_regex='[ \\t]+', missing_parameter_pass=false, multi_value=false, filepath_regex='', id_stem=rule_id) -%}}
889f2b
 {{%- set suffix_id = "" -%}}
889f2b
 {{%- if multi_value -%}}
889f2b
 {{%- set group_regex = "([^#]*).*$" -%}}
889f2b
@@ -173,16 +176,16 @@
889f2b
 {{%- set regex = prefix_regex+parameter+separator_regex+group_regex -%}}
889f2b
 {{%- endif %}}
889f2b
 {{%- endif %}}
889f2b
-  <ind:textfilecontent54_object id="obj_{{{ rule_id }}}{{{ suffix_id }}}" version="1">
889f2b
+<ind:textfilecontent54_object id="obj_{{{ id_stem }}}{{{ suffix_id }}}" version="1">
889f2b
 {{%- if filepath_regex %}}
889f2b
-    <ind:path>{{{ path }}}</ind:path>
889f2b
-    <ind:filename operation="pattern match">{{{ filepath_regex }}}</ind:filename>
889f2b
+  <ind:path>{{{ path }}}</ind:path>
889f2b
+  <ind:filename operation="pattern match">{{{ filepath_regex }}}</ind:filename>
889f2b
 {{%- else %}}
889f2b
-    <ind:filepath>{{{ path }}}</ind:filepath>
889f2b
+  <ind:filepath>{{{ path }}}</ind:filepath>
889f2b
 {{%- endif %}}
889f2b
-    <ind:pattern operation="pattern match">{{{ regex }}}</ind:pattern>
889f2b
-    <ind:instance operation="greater than or equal" datatype="int">1</ind:instance>
889f2b
-  </ind:textfilecontent54_object>
889f2b
+  <ind:pattern operation="pattern match">{{{ regex }}}</ind:pattern>
889f2b
+  <ind:instance operation="greater than or equal" datatype="int">1</ind:instance>
889f2b
+</ind:textfilecontent54_object>
889f2b
 {{%- endmacro %}}
889f2b
 
889f2b
 {{#
889f2b
@@ -193,7 +196,7 @@
889f2b
     - quotes (String): If non-empty, one level of matching quotes is considered when checking the value. Specify one or more quote types as a string.
889f2b
       For example, for shell quoting, specify quotes="'\""), which will make sure that value, 'value' and "value" are matched, but 'value" or '"value"' won't be.
889f2b
 #}}
889f2b
-{{%- macro oval_line_in_file_state(value='', multi_value='', quotes='') -%}}
889f2b
+{{%- macro oval_line_in_file_state(value='', multi_value='', quotes='', id_stem=rule_id) -%}}
889f2b
 {{%- set regex = value -%}}
889f2b
 {{%- if quotes != "" %}}
889f2b
 {{%- if "\\1" in value > 0 %}}
889f2b
@@ -206,9 +209,25 @@
889f2b
 {{%- else %}}
889f2b
 {{%- set regex = "^"+regex+"$" -%}}
889f2b
 {{%- endif %}}
889f2b
-  <ind:textfilecontent54_state id="state_{{{ rule_id }}}" version="1">
889f2b
-    <ind:subexpression datatype="string" operation="pattern match">{{{ regex }}}</ind:subexpression>
889f2b
-  </ind:textfilecontent54_state>
889f2b
+<ind:textfilecontent54_state id="state_{{{ id_stem }}}" version="1">
889f2b
+  <ind:subexpression datatype="string" operation="pattern match">{{{ regex }}}</ind:subexpression>
889f2b
+</ind:textfilecontent54_state>
889f2b
+{{%- endmacro %}}
889f2b
+
889f2b
+{{%- macro oval_line_in_directory_criterion(path='', parameter='', missing_parameter_pass=false) -%}}
889f2b
+{{{- oval_line_in_file_criterion(path, parameter, missing_parameter_pass, id_stem=rule_id ~ "_config_dir") -}}}
889f2b
+{{%- endmacro %}}
889f2b
+
889f2b
+{{%- macro oval_line_in_directory_test(path='', parameter='', missing_parameter_pass=false) -%}}
889f2b
+{{{ oval_line_in_file_test(path, parameter, missing_parameter_pass, id_stem=rule_id ~ "_config_dir") }}}
889f2b
+{{%- endmacro %}}
889f2b
+
889f2b
+{{%- macro oval_line_in_directory_object(path='', section='', prefix_regex='^[ \\t]*', parameter='', separator_regex='[ \\t]+', missing_parameter_pass=false, multi_value=false) -%}}
889f2b
+{{{- oval_line_in_file_object(path=path, section=section, prefix_regex=prefix_regex, parameter=parameter, separator_regex=separator_regex, missing_parameter_pass=missing_parameter_pass, multi_value=multi_value, filepath_regex=".*", id_stem=rule_id ~ "_config_dir") -}}}
889f2b
+{{%- endmacro %}}
889f2b
+
889f2b
+{{%- macro oval_line_in_directory_state(value='', multi_value='', quotes='') -%}}
889f2b
+{{{- oval_line_in_file_state(value, multi_value, quotes, id_stem=rule_id ~ "_config_dir") -}}}
889f2b
 {{%- endmacro %}}
889f2b
 
889f2b
 {{#
889f2b
diff --git a/shared/templates/sshd_lineinfile/bash.template b/shared/templates/sshd_lineinfile/bash.template
889f2b
index ca1b512bb3d..eac758e310b 100644
889f2b
--- a/shared/templates/sshd_lineinfile/bash.template
889f2b
+++ b/shared/templates/sshd_lineinfile/bash.template
889f2b
@@ -3,4 +3,26 @@
889f2b
 # strategy = restrict
889f2b
 # complexity = low
889f2b
 # disruption = low
889f2b
+{{%- if product in ("fedora", "rhel9") %}}
889f2b
+{{%- set prefix_regex = "^\s*" -%}}
889f2b
+{{%- set separator_regex = "\s\+" -%}}
889f2b
+{{%- set line_regex = prefix_regex ~ PARAMETER ~ separator_regex %}}
889f2b
+mkdir -p /etc/ssh/sshd_config.d
889f2b
+touch /etc/ssh/sshd_config.d/hardening
889f2b
+{{{ lineinfile_absent("/etc/ssh/sshd_config", line_regex, insensitive=true) }}}
889f2b
+{{{ lineinfile_absent_in_directory("/etc/ssh/sshd_config.d", line_regex, insensitive=true) }}}
889f2b
+{{{ set_config_file(
889f2b
+        path="/etc/ssh/sshd_config.d/hardening",
889f2b
+        parameter=PARAMETER,
889f2b
+        value=VALUE,
889f2b
+        create=true,
889f2b
+        insert_after="",
889f2b
+        insert_before="^Match",
889f2b
+        insensitive=true,
889f2b
+        separator=" ",
889f2b
+        separator_regex=separator_regex,
889f2b
+        prefix_regex=prefix_regex)
889f2b
+    }}}
889f2b
+{{%- else %}}
889f2b
 {{{ bash_sshd_config_set(parameter=PARAMETER, value=VALUE) }}}
889f2b
+{{%- endif %}}
889f2b
diff --git a/shared/templates/sshd_lineinfile/oval.template b/shared/templates/sshd_lineinfile/oval.template
889f2b
index df63d542505..2cc38776eb2 100644
889f2b
--- a/shared/templates/sshd_lineinfile/oval.template
889f2b
+++ b/shared/templates/sshd_lineinfile/oval.template
889f2b
@@ -1,7 +1,61 @@
889f2b
-{{{
889f2b
-oval_sshd_config(
889f2b
-	parameter=PARAMETER,
889f2b
-	value=VALUE,
889f2b
-	missing_parameter_pass=MISSING_PARAMETER_PASS
889f2b
-)
889f2b
-}}}
889f2b
+{{%- set config_path = "/etc/ssh/sshd_config" %}}
889f2b
+{{%- set config_dir = "/etc/ssh/sshd_config.d" -%}}
889f2b
+{{%- set products_with_distributed_configuration = ("rhel9", "fedora") -%}}
889f2b
+{{%- set description = "Ensure '" ~ PARAMETER ~ "' is configured with value '" ~ VALUE ~ "' in " ~ config_path %}}
889f2b
+{{%- if product in products_with_distributed_configuration %}}
889f2b
+{{%- set description = description  ~ " and in " ~ config_dir -%}}
889f2b
+{{%- endif %}}
889f2b
+{{%- set case_insensitivity_kwargs = dict(prefix_regex="^[ \\t]*(?i)", separator_regex = "(?-i)[ \\t]+") -%}}
889f2b
+
889f2b
+<def-group>
889f2b
+  <definition class="compliance" id="{{{ rule_id }}}" version="1">
889f2b
+   {{{ oval_metadata(description) }}}
889f2b
+   <criteria comment="sshd is configured correctly or is not installed" operator="OR">
889f2b
+     <criteria comment="sshd is not installed" operator="AND">
889f2b
+        
889f2b
+          definition_ref="sshd_not_required_or_unset" />
889f2b
+        
889f2b
+          definition_ref="package_openssh-server_removed" />
889f2b
+     </criteria>
889f2b
+     <criteria comment="sshd is installed and configured" operator="AND">
889f2b
+        
889f2b
+          definition_ref="sshd_required_or_unset" />
889f2b
+        
889f2b
+          definition_ref="package_openssh-server_installed" />
889f2b
+        <criteria comment="sshd is configured correctly" operator="OR">
889f2b
+          {{{- oval_line_in_file_criterion(config_path, PARAMETER) | indent(8) }}}
889f2b
+          {{%- if MISSING_PARAMETER_PASS %}}
889f2b
+          <criteria comment="sshd is not configured incorrectly" operator="AND">
889f2b
+          {{{- oval_line_in_file_criterion(config_path, PARAMETER, MISSING_PARAMETER_PASS) | indent(10)}}}
889f2b
+          {{%- if product in products_with_distributed_configuration %}}
889f2b
+          {{{- oval_line_in_directory_criterion(config_dir, PARAMETER, MISSING_PARAMETER_PASS) | indent(10) }}}
889f2b
+          {{%- endif %}}
889f2b
+          </criteria>
889f2b
+          {{%- endif %}}
889f2b
+          {{%- if product in products_with_distributed_configuration %}}
889f2b
+          {{{- oval_line_in_directory_criterion(config_dir, PARAMETER) | indent(8) }}}
889f2b
+          {{%- endif %}}
889f2b
+        </criteria>
889f2b
+      </criteria>
889f2b
+    </criteria> 
889f2b
+  </definition>
889f2b
+  {{{ oval_line_in_file_test(config_path, PARAMETER) | indent (2) }}}
889f2b
+  {{{ oval_line_in_file_object(config_path, parameter=PARAMETER, ** case_insensitivity_kwargs)| indent (2) }}}
889f2b
+  {{{ oval_line_in_file_state(VALUE) | indent (2) }}}
889f2b
+
889f2b
+  {{%- if MISSING_PARAMETER_PASS %}}
889f2b
+  {{{ oval_line_in_file_test(config_path, PARAMETER, MISSING_PARAMETER_PASS) | indent(2) }}}
889f2b
+  {{{ oval_line_in_file_object(config_path, parameter=PARAMETER, missing_parameter_pass=MISSING_PARAMETER_PASS, ** case_insensitivity_kwargs) | indent(2) }}}
889f2b
+  {{%- endif %}}
889f2b
+
889f2b
+  {{%- if product in products_with_distributed_configuration %}}
889f2b
+  {{{ oval_line_in_directory_test(config_dir, PARAMETER) | indent (2) }}}
889f2b
+  {{{ oval_line_in_directory_object(config_dir, parameter=PARAMETER, ** case_insensitivity_kwargs) | indent (2) }}}
889f2b
+  {{{ oval_line_in_directory_state(VALUE) | indent (2) }}}
889f2b
+
889f2b
+  {{%- if MISSING_PARAMETER_PASS %}}
889f2b
+  {{{ oval_line_in_directory_test(config_path, PARAMETER, MISSING_PARAMETER_PASS) | indent(2) }}}
889f2b
+  {{{ oval_line_in_directory_object(config_path, parameter=PARAMETER, missing_parameter_pass=MISSING_PARAMETER_PASS, ** case_insensitivity_kwargs) | indent(2) }}}
889f2b
+  {{%- endif %}}
889f2b
+  {{%- endif %}}
889f2b
+</def-group>
889f2b
889f2b
From b0f86c11fa0fb45b32b53833b5d3565c7eb73cfe Mon Sep 17 00:00:00 2001
889f2b
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
889f2b
Date: Fri, 30 Apr 2021 11:52:22 +0200
889f2b
Subject: [PATCH 2/6] Improved the lineinfile template.
889f2b
889f2b
It now escapes the text contents if parts of them could be incorrectly interpreted as regexes.
889f2b
---
889f2b
 shared/macros-bash.jinja                  | 2 +-
889f2b
 shared/templates/lineinfile/oval.template | 2 +-
889f2b
 2 files changed, 2 insertions(+), 2 deletions(-)
889f2b
889f2b
diff --git a/shared/macros-bash.jinja b/shared/macros-bash.jinja
889f2b
index b4518d83c19..d654a0e0e89 100644
889f2b
--- a/shared/macros-bash.jinja
889f2b
+++ b/shared/macros-bash.jinja
889f2b
@@ -445,7 +445,7 @@ printf '%s\n' "{{{ message | replace('"', '\\"') }}}" >&2
889f2b
 # prefix_regex: regular expression describing allowed leading characters at each line
889f2b
 #}}
889f2b
 {{%- macro set_config_file(path, parameter, value, create, insert_after, insert_before, insensitive=true, separator=" ", separator_regex="\s\+", prefix_regex="^\s*") -%}}
889f2b
-    {{%- set line_regex = prefix_regex+parameter+separator_regex -%}}
889f2b
+    {{%- set line_regex = prefix_regex + ((parameter | escape_regex) | replace("/", "\/")) + separator_regex -%}}
889f2b
     {{%- set new_line = parameter+separator+value -%}}
889f2b
 if [ -e "{{{ path }}}" ] ; then
889f2b
     {{{ lineinfile_absent(path, line_regex, insensitive) | indent(4) }}}
889f2b
diff --git a/shared/templates/lineinfile/oval.template b/shared/templates/lineinfile/oval.template
889f2b
index a38856d9177..644327b7d6e 100644
889f2b
--- a/shared/templates/lineinfile/oval.template
889f2b
+++ b/shared/templates/lineinfile/oval.template
889f2b
@@ -1,4 +1,4 @@
889f2b
-{{%- set regex = "^[\s]*" + TEXT + "[\s]*$" -%}}
889f2b
+{{%- set regex = "^[\s]*" ~ (TEXT | escape_regex) ~ "[\s]*$" -%}}
889f2b
 <def-group>
889f2b
   <definition class="compliance" id="{{{ rule_id }}}" version="1">
889f2b
     {{{ oval_metadata("Check presence of " + TEXT + " in " + PATH) }}}
889f2b
889f2b
From 6953f74d1ab168e7ccc3f28877621edff317fef2 Mon Sep 17 00:00:00 2001
889f2b
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
889f2b
Date: Fri, 30 Apr 2021 11:54:12 +0200
889f2b
Subject: [PATCH 3/6] Introduced the sshd_use_directory_configuration rule.
889f2b
889f2b
The rule makes sure that the sshd configuration is distributed in the
889f2b
/etc/ssh/sshd_config.d/ directory, and therefore it makes sense to scan that directory
889f2b
in another rules.
889f2b
---
889f2b
 .../bash/shared.sh                            | 15 ++++++++++
889f2b
 .../oval/shared.xml                           | 29 +++++++++++++++++++
889f2b
 .../sshd_use_directory_configuration/rule.yml | 26 +++++++++++++++++
889f2b
 .../tests/match.fail.sh                       |  4 +++
889f2b
 .../tests/simple.fail.sh                      |  3 ++
889f2b
 .../tests/simple.pass.sh                      |  4 +++
889f2b
 shared/references/cce-redhat-avail.txt        |  1 -
889f2b
 shared/templates/extra_ovals.yml              |  6 ++++
889f2b
 8 files changed, 87 insertions(+), 1 deletion(-)
889f2b
 create mode 100644 linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/bash/shared.sh
889f2b
 create mode 100644 linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/oval/shared.xml
889f2b
 create mode 100644 linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/rule.yml
889f2b
 create mode 100644 linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/tests/match.fail.sh
889f2b
 create mode 100644 linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/tests/simple.fail.sh
889f2b
 create mode 100644 linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/tests/simple.pass.sh
889f2b
889f2b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/bash/shared.sh b/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/bash/shared.sh
889f2b
new file mode 100644
889f2b
index 00000000000..2ff58ec373c
889f2b
--- /dev/null
889f2b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/bash/shared.sh
889f2b
@@ -0,0 +1,15 @@
889f2b
+# platform = multi_platform_all
889f2b
+
889f2b
+{{% set target_file = "/etc/ssh/sshd_config.d/sshd_config_original.conf" -%}}
889f2b
+if test -f {{{ target_file}}}; then
889f2b
+	{{{ die("Remediation probably already happened, '" ~ target_file ~ "' already exists, not doing anything.", action="false") }}}
889f2b
+else
889f2b
+	mkdir -p /etc/ssh/sshd_config.d
889f2b
+	mv /etc/ssh/sshd_config {{{ target_file }}}
889f2b
+cat > /etc/ssh/sshd_config << EOF
889f2b
+# To modify the system-wide sshd configuration, create a  *.conf  file under
889f2b
+#  /etc/ssh/sshd_config.d/  which will be automatically included below
889f2b
+
889f2b
+Include /etc/ssh/sshd_config.d/*.conf
889f2b
+EOF
889f2b
+fi
889f2b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/oval/shared.xml b/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/oval/shared.xml
889f2b
new file mode 100644
889f2b
index 00000000000..0ffb429adff
889f2b
--- /dev/null
889f2b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/oval/shared.xml
889f2b
@@ -0,0 +1,29 @@
889f2b
+{{%- set config_path = "/etc/ssh/sshd_config" %}}
889f2b
+
889f2b
+<def-group>
889f2b
+  <definition class="compliance" id="{{{ rule_id }}}" version="1">
889f2b
+   {{{ oval_metadata("foo") }}}
889f2b
+   <criteria comment="sshd is configured correctly or is not installed" operator="OR">
889f2b
+     <criteria comment="sshd is not installed" operator="AND">
889f2b
+        
889f2b
+          definition_ref="sshd_not_required_or_unset" />
889f2b
+        
889f2b
+          definition_ref="package_openssh-server_removed" />
889f2b
+     </criteria>
889f2b
+     <criteria comment="sshd is installed and configured" operator="AND">
889f2b
+        
889f2b
+          definition_ref="sshd_required_or_unset" />
889f2b
+        
889f2b
+          definition_ref="package_openssh-server_installed" />
889f2b
+        <criteria comment="sshd is configured correctly" operator="AND">
889f2b
+          
889f2b
+            definition_ref="sshd_includes_config_files" />
889f2b
+          {{{- oval_line_in_file_criterion(config_path, "match", missing_parameter_pass=true) | indent(8) }}}
889f2b
+        </criteria>
889f2b
+      </criteria>
889f2b
+    </criteria> 
889f2b
+  </definition>
889f2b
+  {{{ oval_line_in_file_test(config_path, "match", missing_parameter_pass=true) | indent (2) }}}
889f2b
+  {{{ oval_line_in_file_object(config_path, parameter="match", missing_parameter_pass=true, prefix_regex="^[ \\t]*(?i)", separator_regex="(?-i)\s+\S+") | indent (2) }}}
889f2b
+</def-group>
889f2b
+
889f2b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/rule.yml b/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/rule.yml
889f2b
new file mode 100644
889f2b
index 00000000000..8c370036e61
889f2b
--- /dev/null
889f2b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/rule.yml
889f2b
@@ -0,0 +1,26 @@
889f2b
+documentation_complete: true
889f2b
+
889f2b
+prodtype: fedora,rhel9
889f2b
+
889f2b
+title: 'Distribute the SSH Server configuration to multiple files in a config directory.'
889f2b
+
889f2b
+description: |-
889f2b
+    Make sure to have the <tt>Include /etc/ssh/sshd_config.d/*.conf</tt> line in the <tt>/etc/ssh/sshd_config</tt> file.
889f2b
+    Ideally, don't have any active configuration directives in that file, and distribute the service configuration
889f2b
+    to several files in the <tt>/etc/ssh/sshd_config.d</tt> directory.
889f2b
+
889f2b
+rationale: |-
889f2b
+    This form of distributed configuration is considered as a good practice, and as other sshd rules assume that directives in files in the <tt>/etc/ssh/sshd_config.d</tt> config directory are effective, there has to be a rule that ensures this.
889f2b
+    Aside from that, having multiple configuration files makes the SSH Server configuration changes easier to partition according to the reason that they were introduced, and therefore it should help to perform merges of hardening updates.
889f2b
+
889f2b
+severity: medium
889f2b
+
889f2b
+identifiers:
889f2b
+    cce@rhel9: CCE-87681-3
889f2b
+
889f2b
+ocil_clause: "you don't include other configuration files from the main configuration file"
889f2b
+
889f2b
+ocil: |-
889f2b
+    To determine whether the SSH server includes configuration files from the right directory, run the following command:
889f2b
+    
$ sudo grep -i '^Include' /etc/ssh/sshd_config
889f2b
+    If a line <tt>Include /etc/ssh/sshd_config.d/*.conf</tt> is returned, then the configuration file inclusion is set correctly.
889f2b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/tests/match.fail.sh b/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/tests/match.fail.sh
889f2b
new file mode 100644
889f2b
index 00000000000..fa2ee0654f2
889f2b
--- /dev/null
889f2b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/tests/match.fail.sh
889f2b
@@ -0,0 +1,4 @@
889f2b
+# platform = multi_platform_all
889f2b
+
889f2b
+echo "Match something" >> /etc/ssh/sshd_config
889f2b
+echo "Include /etc/ssh/sshd_config.d/*.conf" >> /etc/ssh/sshd_config
889f2b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/tests/simple.fail.sh b/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/tests/simple.fail.sh
889f2b
new file mode 100644
889f2b
index 00000000000..a6013ad7cfa
889f2b
--- /dev/null
889f2b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/tests/simple.fail.sh
889f2b
@@ -0,0 +1,3 @@
889f2b
+# platform = multi_platform_all
889f2b
+
889f2b
+echo "include /etc/ssh/sshd_config.d/.*" > /etc/ssh/sshd_config
889f2b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/tests/simple.pass.sh b/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/tests/simple.pass.sh
889f2b
new file mode 100644
889f2b
index 00000000000..7a26f521415
889f2b
--- /dev/null
889f2b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/tests/simple.pass.sh
889f2b
@@ -0,0 +1,4 @@
889f2b
+# platform = multi_platform_all
889f2b
+
889f2b
+# Handling of case-insensitivity of include is tricky
889f2b
+echo "Include /etc/ssh/sshd_config.d/*.conf" > /etc/ssh/sshd_config
889f2b
diff --git a/shared/references/cce-redhat-avail.txt b/shared/references/cce-redhat-avail.txt
889f2b
index 73d025484e6..40a2b9b5868 100644
889f2b
--- a/shared/references/cce-redhat-avail.txt
889f2b
+++ b/shared/references/cce-redhat-avail.txt
889f2b
@@ -1780,7 +1780,6 @@ CCE-87677-1
889f2b
 CCE-87678-9
889f2b
 CCE-87679-7
889f2b
 CCE-87680-5
889f2b
-CCE-87681-3
889f2b
 CCE-87682-1
889f2b
 CCE-87683-9
889f2b
 CCE-87684-7
889f2b
diff --git a/shared/templates/extra_ovals.yml b/shared/templates/extra_ovals.yml
889f2b
index 095d911ee1c..69062ebe541 100644
889f2b
--- a/shared/templates/extra_ovals.yml
889f2b
+++ b/shared/templates/extra_ovals.yml
889f2b
@@ -57,3 +57,9 @@ service_syslog_disabled:
889f2b
   vars:
889f2b
     servicename: syslog
889f2b
     packagename: rsyslog
889f2b
+
889f2b
+sshd_includes_config_files:
889f2b
+  name: lineinfile
889f2b
+  vars:
889f2b
+    path: /etc/ssh/sshd_config
889f2b
+    text: "Include /etc/ssh/sshd_config.d/*.conf"
889f2b
889f2b
From d7fcab7ad66e77bb7ccba507e3f024bc892c3864 Mon Sep 17 00:00:00 2001
889f2b
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
889f2b
Date: Tue, 11 May 2021 16:06:29 +0200
889f2b
Subject: [PATCH 4/6] Improved error reporting related to macros.
889f2b
889f2b
---
889f2b
 ssg/jinja.py | 22 +++++++++++++---------
889f2b
 1 file changed, 13 insertions(+), 9 deletions(-)
889f2b
889f2b
diff --git a/ssg/jinja.py b/ssg/jinja.py
889f2b
index a46246ad0fb..28edd9a6dcd 100644
889f2b
--- a/ssg/jinja.py
889f2b
+++ b/ssg/jinja.py
889f2b
@@ -153,16 +153,20 @@ def load_macros(substitutions_dict=None):
889f2b
 
889f2b
     add_python_functions(substitutions_dict)
889f2b
     try:
889f2b
-        update_substitutions_dict(JINJA_MACROS_BASE_DEFINITIONS, substitutions_dict)
889f2b
-        update_substitutions_dict(JINJA_MACROS_HIGHLEVEL_DEFINITIONS, substitutions_dict)
889f2b
-        update_substitutions_dict(JINJA_MACROS_ANSIBLE_DEFINITIONS, substitutions_dict)
889f2b
-        update_substitutions_dict(JINJA_MACROS_BASH_DEFINITIONS, substitutions_dict)
889f2b
-        update_substitutions_dict(JINJA_MACROS_OVAL_DEFINITIONS, substitutions_dict)
889f2b
-        update_substitutions_dict(JINJA_MACROS_IGNITION_DEFINITIONS, substitutions_dict)
889f2b
-        update_substitutions_dict(JINJA_MACROS_KUBERNETES_DEFINITIONS, substitutions_dict)
889f2b
+        filenames = [
889f2b
+            JINJA_MACROS_BASE_DEFINITIONS,
889f2b
+            JINJA_MACROS_HIGHLEVEL_DEFINITIONS,
889f2b
+            JINJA_MACROS_ANSIBLE_DEFINITIONS,
889f2b
+            JINJA_MACROS_BASH_DEFINITIONS,
889f2b
+            JINJA_MACROS_OVAL_DEFINITIONS,
889f2b
+            JINJA_MACROS_IGNITION_DEFINITIONS,
889f2b
+            JINJA_MACROS_KUBERNETES_DEFINITIONS,
889f2b
+        ]
889f2b
+        for filename in filenames:
889f2b
+            update_substitutions_dict(filename, substitutions_dict)
889f2b
     except Exception as exc:
889f2b
-        msg = ("Error extracting macro definitions: {0}"
889f2b
-               .format(str(exc)))
889f2b
+        msg = ("Error extracting macro definitions from '{1}': {0}"
889f2b
+               .format(str(exc), filename))
889f2b
         raise RuntimeError(msg)
889f2b
 
889f2b
     return substitutions_dict
889f2b
889f2b
From df45c3fa295a2dc5a23cc347657964df6453cbae Mon Sep 17 00:00:00 2001
889f2b
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
889f2b
Date: Tue, 11 May 2021 16:44:50 +0200
889f2b
Subject: [PATCH 5/6] Removed devault values that are variables from Jinja
889f2b
889f2b
Support in older jinja2 packages is not in a good shape.
889f2b
---
889f2b
 shared/macros-oval.jinja | 12 ++++++++----
889f2b
 1 file changed, 8 insertions(+), 4 deletions(-)
889f2b
889f2b
diff --git a/shared/macros-oval.jinja b/shared/macros-oval.jinja
889f2b
index d38db96d9e3..87e0fd7d87d 100644
889f2b
--- a/shared/macros-oval.jinja
889f2b
+++ b/shared/macros-oval.jinja
889f2b
@@ -92,7 +92,8 @@
889f2b
     - parameter (String): The parameter to be checked in the configuration file.
889f2b
     - missing_parameter_pass (boolean): If set, the check will also pass if the parameter is not present in the configuration file (default is applied).
889f2b
 #}}
889f2b
-{{%- macro oval_line_in_file_criterion(path='', parameter='', missing_parameter_pass=false, comment='', id_stem=rule_id) -%}}
889f2b
+{{%- macro oval_line_in_file_criterion(path='', parameter='', missing_parameter_pass=false, comment='', id_stem='') -%}}
889f2b
+{{%- set id_stem = id_stem or rule_id -%}}
889f2b
 {{%- set suffix_id = "" -%}}
889f2b
 {{%- set prefix_text = "Check the" -%}}
889f2b
 {{%- if missing_parameter_pass %}}
889f2b
@@ -113,7 +114,8 @@
889f2b
     - parameter (String): The parameter to be checked in the configuration file.
889f2b
     - missing_parameter_pass (boolean): If set, the check will also pass if the parameter is not present in the configuration file (default is applied).
889f2b
 #}}
889f2b
-{{%- macro oval_line_in_file_test(path='', parameter='', missing_parameter_pass=false, id_stem=rule_id) -%}}
889f2b
+{{%- macro oval_line_in_file_test(path='', parameter='', missing_parameter_pass=false, id_stem='') -%}}
889f2b
+{{%- set id_stem = id_stem or rule_id -%}}
889f2b
 {{%- set suffix_id = "" -%}}
889f2b
 {{%- if missing_parameter_pass %}}
889f2b
 {{%- set check_existence = "none_exist" -%}}
889f2b
@@ -144,7 +146,8 @@
889f2b
     - missing_parameter_pass (boolean): If set, the check will also pass if the parameter is not present in the configuration file (default is applied).
889f2b
     - multi_value (boolean): If set, it means that the parameter can accept multiple values and the expected value must be present in the current list of values.
889f2b
 #}}
889f2b
-{{%- macro oval_line_in_file_object(path='', section='', prefix_regex='^[ \\t]*', parameter='', separator_regex='[ \\t]+', missing_parameter_pass=false, multi_value=false, filepath_regex='', id_stem=rule_id) -%}}
889f2b
+{{%- macro oval_line_in_file_object(path='', section='', prefix_regex='^[ \\t]*', parameter='', separator_regex='[ \\t]+', missing_parameter_pass=false, multi_value=false, filepath_regex='', id_stem='') -%}}
889f2b
+{{%- set id_stem = id_stem or rule_id -%}}
889f2b
 {{%- set suffix_id = "" -%}}
889f2b
 {{%- if multi_value -%}}
889f2b
 {{%- set group_regex = "([^#]*).*$" -%}}
889f2b
@@ -196,7 +199,8 @@
889f2b
     - quotes (String): If non-empty, one level of matching quotes is considered when checking the value. Specify one or more quote types as a string.
889f2b
       For example, for shell quoting, specify quotes="'\""), which will make sure that value, 'value' and "value" are matched, but 'value" or '"value"' won't be.
889f2b
 #}}
889f2b
-{{%- macro oval_line_in_file_state(value='', multi_value='', quotes='', id_stem=rule_id) -%}}
889f2b
+{{%- macro oval_line_in_file_state(value='', multi_value='', quotes='', id_stem='') -%}}
889f2b
+{{%- set id_stem = id_stem or rule_id -%}}
889f2b
 {{%- set regex = value -%}}
889f2b
 {{%- if quotes != "" %}}
889f2b
 {{%- if "\\1" in value > 0 %}}
889f2b
889f2b
From a3ec49f75ac3059d7096985e08e10005db96330a Mon Sep 17 00:00:00 2001
889f2b
From: Matej Tyc <matyc@redhat.com>
889f2b
Date: Fri, 30 Jul 2021 17:25:25 +0200
889f2b
Subject: [PATCH 6/6] Don't remediate when it is inappropriate
889f2b
889f2b
Don't remediate when the config file already contains the include
889f2b
directive.
889f2b
---
889f2b
 .../sshd_use_directory_configuration/bash/shared.sh        | 7 +++++--
889f2b
 1 file changed, 5 insertions(+), 2 deletions(-)
889f2b
889f2b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/bash/shared.sh b/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/bash/shared.sh
889f2b
index 2ff58ec373c..9317b23992d 100644
889f2b
--- a/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/bash/shared.sh
889f2b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_use_directory_configuration/bash/shared.sh
889f2b
@@ -1,12 +1,15 @@
889f2b
 # platform = multi_platform_all
889f2b
 
889f2b
 {{% set target_file = "/etc/ssh/sshd_config.d/sshd_config_original.conf" -%}}
889f2b
+{{% set base_config = "/etc/ssh/sshd_config" -%}}
889f2b
 if test -f {{{ target_file}}}; then
889f2b
 	{{{ die("Remediation probably already happened, '" ~ target_file ~ "' already exists, not doing anything.", action="false") }}}
889f2b
+elif grep -Eq '^\s*Include\s+/etc/ssh/sshd_config\.d/\*\.conf' {{{ base_config }}} && ! grep -Eq '^\s*Match\s' {{{ base_config }}}; then
889f2b
+	{{{ die("Remediation probably already happened, '" ~ base_config ~ "' already contains the include directive.", action="false") }}}
889f2b
 else
889f2b
 	mkdir -p /etc/ssh/sshd_config.d
889f2b
-	mv /etc/ssh/sshd_config {{{ target_file }}}
889f2b
-cat > /etc/ssh/sshd_config << EOF
889f2b
+	mv {{{ base_config }}} {{{ target_file }}}
889f2b
+cat > {{{ base_config }}} << EOF
889f2b
 # To modify the system-wide sshd configuration, create a  *.conf  file under
889f2b
 #  /etc/ssh/sshd_config.d/  which will be automatically included below
889f2b