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

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