<def-group>
  <definition class="compliance" id="require_emergency_target_auth" version="1">
    {{{ oval_metadata("The requirement for a password to boot into emergency mode
      should be configured correctly.", rule_title=rule_title) }}}
{{% if 'sle' in product or 'rhel' in product or product == 'fedora' or product == 'slmicro5' or 'ol' in families %}}
    <criteria operator="OR">
{{% endif %}}
      <criteria operator="AND">
        <criterion comment="Conditions are satisfied"
          test_ref="test_require_emergency_service" />
        <criterion test_ref="test_require_emergency_service_emergency_target" />
        <criterion test_ref="test_no_custom_emergency_target" negate="true"/>
        <criterion test_ref="test_no_custom_emergency_service" negate="true"/>
        <criterion test_ref="test_{{{ rule_id }}}_drop_in_config_exist" negate="true"/>
      </criteria>
{{% if 'sle' in product or 'rhel' in product or product == 'fedora' or product == 'slmicro5' or 'ol' in families  %}}
      <criteria operator="ONE">
        <criterion comment="Check drop-in configuration"
          test_ref="test_require_emergency_service_drop_in" />
      </criteria>
    </criteria>
{{% endif %}}
  </definition>
  <ind:textfilecontent54_test check="all" check_existence="all_exist"
    comment="Tests that
    {{% if product in ["fedora", "ol8", "ol9", "sle12", "sle15", "sle16", "slmicro5"] or 'rhel' in product -%}}
    /usr/lib/systemd/systemd-sulogin-shell
    {{%- else -%}}
    /sbin/sulogin
    {{%- endif %}}
    was not removed from the default systemd emergency.service to ensure that a
    password must be entered to access single user mode"
    id="test_require_emergency_service" version="1">
    <ind:object object_ref="obj_require_emergency_service" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="obj_require_emergency_service" version="1">
    <ind:filepath>/usr/lib/systemd/system/emergency.service</ind:filepath>
    {{%- if product in ["fedora", "ol8", "ol9", "kylinserver10", "openeuler2203", "sle12", "sle15", "sle16", "slmicro5"] or 'rhel' in product -%}}
    <ind:pattern operation="pattern match">^ExecStart=\-/usr/lib/systemd/systemd-sulogin-shell[\s]+emergency</ind:pattern>
    {{%- else -%}}
    <ind:pattern operation="pattern match">^ExecStart=\-/bin/sh[\s]+-c[\s]+\"(/usr)?/sbin/sulogin;[\s]+/usr/bin/systemctl[\s]+--fail[\s]+--no-block[\s]+default\"</ind:pattern>
    {{%- endif -%}}
    <ind:instance datatype="int">1</ind:instance>
  </ind:textfilecontent54_object>

{{% if 'sle' in product or 'rhel' in product or product == 'fedora' or product == 'slmicro5' or 'ol' in families  %}}
  <ind:textfilecontent54_test check="all" check_existence="all_exist"
    comment="Tests that
    /usr/lib/systemd/systemd-sulogin-shell
    was not removed from the default systemd emergency.service to ensure that a
    password must be entered to access single user mode"
    id="test_require_emergency_service_drop_in" version="1">
    <ind:object object_ref="obj_require_emergency_service_drop_in" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="obj_require_emergency_service_drop_in" version="1">
    <ind:path>/etc/systemd/system/emergency.service.d</ind:path>
    <ind:filename operation="pattern match">^.*\.conf$</ind:filename>
    <ind:pattern operation="pattern match">^ExecStart=\-/usr/lib/systemd/systemd-sulogin-shell[\s]+emergency</ind:pattern>
    <ind:instance datatype="int">1</ind:instance>
  </ind:textfilecontent54_object>
{{% endif %}}

  <ind:textfilecontent54_test check="all" check_existence="all_exist"
    comment="Tests that the systemd emergency.service is in the emergency.target"
    id="test_require_emergency_service_emergency_target" version="1">
    <ind:object object_ref="obj_require_emergency_service_emergency" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="obj_require_emergency_service_emergency" version="1">
    <ind:filepath>/usr/lib/systemd/system/emergency.target</ind:filepath>
    <ind:pattern operation="pattern match">^Requires=.*emergency\.service</ind:pattern>
    <ind:instance datatype="int">1</ind:instance>
  </ind:textfilecontent54_object>

  <unix:file_test check="all" check_existence="at_least_one_exists"
  comment="look for emergency.service in /etc/systemd/system"
  id="test_no_custom_emergency_service" version="1">
    <unix:object object_ref="object_no_custom_emergency_service" />
  </unix:file_test>
  <unix:file_object comment="look for emergency.service in /etc/systemd/system"
    id="object_no_custom_emergency_service" version="1">
    <unix:behaviors recurse="directories" recurse_direction="down" recurse_file_system="all" />
    <unix:path operation="equals">/etc/systemd/system</unix:path>
    <unix:filename operation="pattern match">^emergency.service$</unix:filename>
  </unix:file_object>

  <unix:file_test check="all" check_existence="at_least_one_exists"
    comment="look for emergency.target in /etc/systemd/system"
    id="test_no_custom_emergency_target" version="1">
    <unix:object object_ref="object_no_custom_emergency_target" />
  </unix:file_test>
  <unix:file_object comment="look for emergency.target in /etc/systemd/system"
    id="object_no_custom_emergency_target" version="1">
    <unix:behaviors recurse="directories" recurse_direction="down" recurse_file_system="all" />
    <unix:path operation="equals">/etc/systemd/system</unix:path>
    <unix:filename operation="pattern match">^emergency.target$</unix:filename>
  </unix:file_object>

  <unix:file_test id="test_{{{ rule_id }}}_drop_in_config_exist"
                  comment="Look for drop in config files for emergency.service"
                  version="1"
                  check="all"
                  check_existence="at_least_one_exists">
     <unix:object object_ref="object_{{{ rule_id }}}_drop_in_config_exist" />
  </unix:file_test>

<unix:file_object id="object_{{{ rule_id }}}_drop_in_config_exist"
                  comment="Look for drop in config files for emergency.service"
                  version="1">
    <unix:path operation="equals">/etc/systemd/system/emergency.service.d</unix:path>
    <unix:filename operation="pattern match">^.*\.conf$</unix:filename>
</unix:file_object>

</def-group>
