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

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