<def-group>
  <definition class="compliance" id="{{{ rule_id }}}" version="5">
    {{{ oval_metadata("The root account should be configured to deny access after the number of
    defined failed attempts has been reached.", rule_title=rule_title) }}}
    <criteria operator="AND" comment="Check the proper configuration of pam_faillock.so">
      <criteria operator="AND" comment="Check if pam_faillock.so is properly enabled">
        <!-- pam_unix.so is a control module present in all realistic scenarios and also used
             as reference for the correct position of pam_faillock.so in auth section. If the
             system is properly configured, it must appear only once in auth section. -->

        <criterion
            test_ref="test_accounts_passwords_pam_faillock_deny_root_pam_unix_auth"
            comment="pam_unix.so appears only once in auth section of common-auth"/>


        <!-- pam_faillock.so parameters can be defined directly in pam files or, in newer
             versions, in {{{ pam_faillock_conf_path }}}. The last is the recommended option when
             available. Also, is the option used by auselect tool. However, regardless the
             approach, a minimal declaration is common in pam files. -->

        <criterion
            test_ref="test_accounts_passwords_pam_faillock_deny_root_pam_faillock_auth"
            comment="pam_faillock.so is properly defined in auth section of common-auth"/>
        <criterion
            test_ref="test_accounts_passwords_pam_faillock_deny_root_pam_faillock_account"
            comment="pam_faillock.so is properly defined in account section of common-account"/>
      </criteria>

      <!-- pam_faillock.so parameters should be defined in {{{ pam_faillock_conf_path }}} whenever
           possible. But due to backwards compatibility, they are also allowed in pam files
           directly. In case they are defined in both places, pam files have precedence and this
           may confuse the assessment. The following tests ensure only one option is used. Note
           that if {{{ pam_faillock_conf_path }}} is available, authselect tool only manage parameters on it -->
      <criteria operator="OR"
                comment="Check expected value for pam_faillock.so even_deny_root parameter">
        <criteria operator="AND"
                  comment="Check expected pam_faillock.so even_deny_root parameter in pam files">
          <criterion
              test_ref="test_accounts_passwords_pam_faillock_deny_root_parameter_pamd_auth"
              comment="Check the even_deny_root parameter in auth section of common-auth file"/>
          <criterion
              test_ref="test_accounts_passwords_pam_faillock_deny_root_parameter_no_faillock_conf"
              comment="Ensure {{{ pam_faillock_conf_path }}} is not used together with pam files"/>
        </criteria>
        <criteria operator="AND"
		  comment="Check expected pam_faillock.so even_deny_root parameter in {{{ pam_faillock_conf_path }}}">
          <criterion
              test_ref="test_accounts_passwords_pam_faillock_deny_root_parameter_no_pamd_auth"
              comment="Check the even_deny_root parameter is not present common-auth file"/>
          <criterion
              test_ref="test_accounts_passwords_pam_faillock_deny_root_parameter_faillock_conf"
              comment="Ensure the even_deny_root parameter is present in {{{ pam_faillock_conf_path }}}"/>
        </criteria>
      </criteria>
    </criteria>
  </definition>

  <!-- The following tests demand complex regex which are necessary more than once.
       These variables make simpler the usage of regex patterns. -->
  <constant_variable
      id="var_accounts_passwords_pam_faillock_deny_root_pam_unix_regex"
      datatype="string" version="1"
      comment="regex to identify pam_unix.so in auth section of pam files">
    <value>^[\s]*auth\N+pam_unix\.so</value>
  </constant_variable>

  <constant_variable
      id="var_accounts_passwords_pam_faillock_deny_root_pam_faillock_auth_regex"
      datatype="string" version="1"
      comment="regex to identify pam_faillock.so entries in common-auth">
    <value>^[\s]*auth[\s]+(required|\[(?=.*?\bsuccess=\b).*\])[\s]+pam_faillock\.so[\s\w\d=]+preauth[\s\S]*^[\s]*auth[\s]+(sufficient|\[(?=.*\bsuccess=\b)(?=.*?\bdefault=ignore\b)?.*\])[\s]+pam_unix\.so[\s\S]*^[\s]*auth[\s]+(required|\[(?=.*?\bsuccess=\b)?(?=.*?\bdefault=(bad|die)\b).*\])[\s]+pam_faillock\.so[\s\w\d=]+authfail[\s\S]*^[\s]*auth[\s]+(required|sufficient|\[(?=.*?\bsuccess=\b).*\])[\s]+pam_faillock\.so[\s\w\d=]+authsucc</value>
  </constant_variable>

  <constant_variable
      id="var_accounts_passwords_pam_faillock_deny_root_pam_faillock_account_regex"
      datatype="string" version="1"
      comment="regex to identify pam_faillock.so entry in common-account">
<value>^[\s]*account[\s]+(required|\[(?=.*?\bsuccess=\b)(?=.*?\bnew_authtok_reqd=(ok|done)\b)(?=.*?\bdefault=(bad|ignore)\b).*\])[\s]+pam_unix\.so[\s\S]*^[\s]*account[\s]+(required|\[(?=.*?\bsuccess=\b)(?=.*\bdefault=\b).*\])[\s]+pam_faillock\.so</value>
</constant_variable>

<constant_variable
    id="var_accounts_passwords_pam_faillock_deny_root_pam_faillock_parameter_definition_regex"
    datatype="string" version="1"
    comment="regex to identify pam_faillock.so deny entries definitions in auth section of pam files">
  <value>^[\s]*auth[\s]+.+[\s]+pam_faillock.so[\s]+(?=.*authfail)(?=.*even_deny_root)[\s\S]*^[\s]*auth[\s]+.+[\s]+pam_faillock.so[\s]+(?=.*authsucc)(?=.*even_deny_root)</value>
</constant_variable>

<constant_variable
    id="var_accounts_passwords_pam_faillock_deny_root_pam_faillock_parameter_presence_regex"
    datatype="string" version="1"
    comment="regex to identify pam_faillock.so deny entry presence in auth section of pam files">
  <value>^[\s]*auth[\s]+.+[\s]+pam_faillock.so[\s]+[^\n]*even_deny_root</value>
</constant_variable>

<constant_variable
    id="var_accounts_passwords_pam_faillock_deny_root_faillock_conf_parameter_regex"
    datatype="string" version="1"
    comment="regex to identify deny entry in {{{ pam_faillock_conf_path }}}">
<value>^[\s]*even_deny_root</value>
</constant_variable>

<!-- Check occurrences of pam_unix.so in auth section of common-auth file -->
<ind:textfilecontent54_object
    version="1"
    id="object_accounts_passwords_pam_faillock_deny_root_pam_unix_auth"
    comment="Get the second and subsequent occurrences of pam_unix.so in auth section of common-auth">
  <ind:filepath operation="pattern match">^/etc/pam.d/common-auth$</ind:filepath>
  <ind:pattern operation="pattern match"
               var_ref="var_accounts_passwords_pam_faillock_deny_root_pam_unix_regex"/>
  <!-- only one occurrence of pam_unix.so is expected -->
<ind:instance datatype="int" operation="equals">1</ind:instance>
</ind:textfilecontent54_object>

<ind:textfilecontent54_test
    check="all" check_existence="only_one_exists" version="1"
    id="test_accounts_passwords_pam_faillock_deny_root_pam_unix_auth"
    comment="No more than one pam_unix.so is expected in auth section of common-auth">
  <ind:object object_ref="object_accounts_passwords_pam_faillock_deny_root_pam_unix_auth"/>
</ind:textfilecontent54_test>

<!-- Check common definition of pam_faillock.so in common-auth -->
<ind:textfilecontent54_object
    version="1"
    id="object_accounts_passwords_pam_faillock_deny_root_pam_faillock_auth"
    comment="Check common definition of pam_faillock.so in auth section of common-auth">
<ind:filepath operation="pattern match">^/etc/pam.d/common-auth$</ind:filepath>
<ind:pattern operation="pattern match"
             var_ref="var_accounts_passwords_pam_faillock_deny_root_pam_faillock_auth_regex"/>
<ind:instance datatype="int" operation="equals">1</ind:instance>
</ind:textfilecontent54_object>

<ind:textfilecontent54_test
    check="all" check_existence="only_one_exists" version="1"
    id="test_accounts_passwords_pam_faillock_deny_root_pam_faillock_auth"
    comment="Check definition of pam_faillock in common-auth (preauth, authfail, authsucc)">
  <ind:object
      object_ref="object_accounts_passwords_pam_faillock_deny_root_pam_faillock_auth"/>
</ind:textfilecontent54_test>

<!-- Check common definition of pam_faillock.so in common-account -->
<ind:textfilecontent54_object
    version="1"
    id="object_accounts_passwords_pam_faillock_deny_root_pam_faillock_account"
    comment="Check common definition of pam_faillock.so in account section of common-account">
<ind:filepath operation="pattern match">^/etc/pam.d/common-account$</ind:filepath>
<ind:pattern operation="pattern match"
             var_ref="var_accounts_passwords_pam_faillock_deny_root_pam_faillock_account_regex"/>
<ind:instance datatype="int" operation="equals">1</ind:instance>
</ind:textfilecontent54_object>

<ind:textfilecontent54_test
    check="all" check_existence="only_one_exists" version="1"
    id="test_accounts_passwords_pam_faillock_deny_root_pam_faillock_account"
    comment="Check definition of pam_faillock in common-account">
  <ind:object
      object_ref="object_accounts_passwords_pam_faillock_deny_root_pam_faillock_account"/>
</ind:textfilecontent54_test>

<!-- Check the pam_faillock.so even_deny_root parameter in common-auth -->
<ind:textfilecontent54_object
    version="1"
    id="object_accounts_passwords_pam_faillock_deny_root_parameter_presence_pamd_auth"
    comment="Get the pam_faillock.so even_deny_root parameter from common-auth file">
  <ind:filepath operation="pattern match">^/etc/pam.d/common-auth$</ind:filepath>
  <ind:pattern
      operation="pattern match"
      var_ref="var_accounts_passwords_pam_faillock_deny_root_pam_faillock_parameter_presence_regex"/>
<ind:instance datatype="int" operation="equals">1</ind:instance>
</ind:textfilecontent54_object>

<ind:textfilecontent54_object
    version="1"
    id="object_accounts_passwords_pam_faillock_deny_root_parameter_definition_pamd_auth"
    comment="Get the pam_faillock.so even_deny_root parameter from common-auth file">
  <ind:filepath operation="pattern match">^/etc/pam.d/common-auth$</ind:filepath>
  <ind:pattern
      operation="pattern match"
      var_ref="var_accounts_passwords_pam_faillock_deny_root_pam_faillock_parameter_definition_regex"/>
<ind:instance datatype="int" operation="equals">1</ind:instance>
</ind:textfilecontent54_object>

<ind:textfilecontent54_test
    check="all" check_existence="all_exist" version="1"
    id="test_accounts_passwords_pam_faillock_deny_root_parameter_pamd_auth"
    comment="Check the expected even_deny_root parameters in common-auth">
  <ind:object
      object_ref="object_accounts_passwords_pam_faillock_deny_root_parameter_definition_pamd_auth"/>
</ind:textfilecontent54_test>

<ind:textfilecontent54_test
    check="all" check_existence="none_exist" version="1"
    id="test_accounts_passwords_pam_faillock_deny_root_parameter_no_pamd_auth"
    comment="Check the absence of even_deny_root parameter in common-auth">
  <ind:object
      object_ref="object_accounts_passwords_pam_faillock_deny_root_parameter_presence_pamd_auth"/>
</ind:textfilecontent54_test>

<!-- Check pam_faillock.so even_deny_root parameter in {{{ pam_faillock_conf_path }}} -->
<ind:textfilecontent54_object
    version="1"
    id="object_accounts_passwords_pam_faillock_deny_root_parameter_faillock_conf"
    comment="Try to get the even_deny_root parameter from {{{ pam_faillock_conf_path }}}">
  <ind:filepath operation="pattern match">^{{{ pam_faillock_conf_path }}}$</ind:filepath>
  <ind:pattern operation="pattern match"
               var_ref="var_accounts_passwords_pam_faillock_deny_root_faillock_conf_parameter_regex"/>
<ind:instance datatype="int" operation="equals">1</ind:instance>
</ind:textfilecontent54_object>

<ind:textfilecontent54_test
    check="all" check_existence="all_exist" version="1"
    id="test_accounts_passwords_pam_faillock_deny_root_parameter_faillock_conf"
    comment="Check the expected even_deny_root parameter in {{{ pam_faillock_conf_path }}}">
  <ind:object
      object_ref="object_accounts_passwords_pam_faillock_deny_root_parameter_faillock_conf"/>
</ind:textfilecontent54_test>

<ind:textfilecontent54_test
    check="all" check_existence="none_exist" version="1"
    id="test_accounts_passwords_pam_faillock_deny_root_parameter_no_faillock_conf"
    comment="Check the absence of even_deny_root parameter in {{{ pam_faillock_conf_path }}}">
  <ind:object
      object_ref="object_accounts_passwords_pam_faillock_deny_root_parameter_faillock_conf"/>
</ind:textfilecontent54_test>
</def-group>
