<def-group>
  <definition class="compliance" id="audit_rules_privileged_commands" version="1">
    <metadata>
      <title>Ensure auditd Collects Information on the Use of Privileged Commands</title>
      <affected family="unix">
        <platform>Red Hat Enterprise Linux 7</platform>
        <platform>multi_platform_fedora</platform>
      </affected>
      <description>Audit rules about the information on the use of privileged commands are enabled.</description>
      <reference source="JL" ref_id="RHEL7_20150420" ref_url="test_attestation" />
      <reference source="JL" ref_id="FEDORA20_20150420" ref_url="test_attestation" />
    </metadata>

    <criteria operator="OR">

      <!-- Test the augenrules case -->
      <criteria operator="AND">
        <criterion comment="audit augenrules" test_ref="test_arpc_augenrules" />
        <criterion comment="audit augenrules suid sgid" test_ref="test_arpc_suid_sgid_augenrules" />
        <criterion comment="audit augenrules binaries count matches rules count" test_ref="test_arpc_bin_count_equals_rules_count_augenrules" />
      </criteria>

      <!-- Test the auditctl case -->
      <criteria operator="AND">
        <criterion comment="audit auditctl" test_ref="test_arpc_auditctl" />
        <criterion comment="audit auditctl suid sgid" test_ref="test_arpc_suid_sgid_auditctl" />
        <criterion comment="audit auditctl binaries count matches rules count" test_ref="test_arpc_bin_count_equals_rules_count_auditctl" />
      </criteria>

    </criteria>
  </definition>

  <!-- First define OVAL entities that can be reused across tests below -->
  <unix:file_object id="object_system_privileged_commands" comment="system files with setuid or setgid permission set" version="1">
    <unix:behaviors recurse="directories" recurse_direction="down" recurse_file_system="local" max_depth="-1" />
    <unix:path operation="equals">/</unix:path>
    <!-- [a-z]+ regex below is a workaround for OpenSCAP https://fedorahosted.org/openscap/ticket/457 bug -->
    <unix:filename operation="pattern match">[a-z]+</unix:filename>
    <filter action="include">state_setuid_or_setgid_set</filter>
    <filter action="exclude">state_dev_proc_sys_dirs</filter>
  </unix:file_object>

  <unix:file_state id="state_setuid_or_setgid_set" version="1" operator="OR">
    <unix:suid datatype="boolean">true</unix:suid>
    <unix:sgid datatype="boolean">true</unix:sgid>
  </unix:file_state>

  <unix:file_state id="state_dev_proc_sys_dirs" version="1">
    <unix:filepath operation="pattern match">^\/(dev|proc|sys)\/.*$</unix:filepath>
  </unix:file_state>

  <local_variable id="variable_full_form_of_audit_rule" comment="full form of audit rules for privileged commands" datatype="string" version="1">
    <concat>
      <literal_component>-a always,exit -F path=</literal_component>
      <object_component object_ref="object_system_privileged_commands" item_field="filepath" />
      <literal_component> -F perm=x -F auid>=1000 -F auid!=4294967295 -k privileged</literal_component>
    </concat>
  </local_variable>

  <local_variable id="variable_count_of_suid_sgid_binaries_on_system" comment="count of suid / sgid binaries actually present on the system" datatype="int" version="1">
    <count>
      <object_component object_ref="object_system_privileged_commands" item_field="filepath" />
    </count>
  </local_variable>

  <ind:variable_object id="object_count_of_suid_sgid_binaries_on_system" version="1">
    <ind:var_ref>variable_count_of_suid_sgid_binaries_on_system</ind:var_ref>
  </ind:variable_object>

  <ind:textfilecontent54_state id="state_proper_audit_rule_but_for_unprivileged_command" version="1">
    <ind:subexpression operation="not equal" datatype="string" var_ref="variable_full_form_of_audit_rule" var_check="all" />
  </ind:textfilecontent54_state>

  <ind:textfilecontent54_state id="state_audit_rules_privileged_commands" version="1">
    <ind:subexpression operation="pattern match" datatype="string" var_ref="variable_full_form_of_audit_rule" var_check="at least one"/>
  </ind:textfilecontent54_state>

  <!-- Test the augenrules case -->
  <ind:textfilecontent54_test check="all" comment="audit augenrules" id="test_arpc_augenrules" version="1">
    <ind:object object_ref="object_arpc_augenrules" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="object_arpc_augenrules" version="1">
    <ind:filepath>/usr/lib/systemd/system/auditd.service</ind:filepath>
    <ind:pattern operation="pattern match">^ExecStartPost=\-\/sbin\/augenrules.*$</ind:pattern>
    <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
  </ind:textfilecontent54_object>

  <ind:textfilecontent54_test check="all" check_existence="all_exist" comment="audit augenrules suid sgid" id="test_arpc_suid_sgid_augenrules" version="1">
    <ind:object object_ref="object_arpc_suid_sgid_augenrules" />
    <ind:state state_ref="state_audit_rules_privileged_commands" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="object_arpc_suid_sgid_augenrules" version="1">
    <ind:filepath operation="pattern match">/etc/audit/rules\.d/.*\.rules</ind:filepath>
    <ind:pattern operation="pattern match">^[\s]*(-a always,exit -F path=[^\n]+ -F perm=x -F auid>=1000 -F auid!=4294967295 -k privileged)[\s]*$</ind:pattern>
    <ind:instance operation="greater than or equal" datatype="int">1</ind:instance>
    <filter action="exclude">state_proper_audit_rule_but_for_unprivileged_command</filter>
  </ind:textfilecontent54_object>

  <local_variable id="variable_count_of_privileged_commands_having_audit_definition_augenrules" comment="count of suid / sgid binaries having full audit rule definition in some of /etc/audit/rules.d/*.rules files" datatype="int" version="1">
    <count>
      <object_component object_ref="object_arpc_suid_sgid_augenrules" item_field="subexpression" />
    </count>
  </local_variable>
  <ind:variable_state id="state_count_of_privileged_commands_having_audit_definition_augenrules" version="1">
    <ind:value operation="equals" datatype="int" var_ref="variable_count_of_privileged_commands_having_audit_definition_augenrules" var_check="at least one" />
  </ind:variable_state>
  <ind:variable_test check="all" check_existence="all_exist" id="test_arpc_bin_count_equals_rules_count_augenrules" comment="audit augenrules binaries count matches rules count" version="1">
    <ind:object object_ref="object_count_of_suid_sgid_binaries_on_system" />
    <ind:state state_ref="state_count_of_privileged_commands_having_audit_definition_augenrules" />
  </ind:variable_test>

  <!-- Test the auditctl case -->
  <ind:textfilecontent54_test check="all" comment="audit auditctl" id="test_arpc_auditctl" version="1">
    <ind:object object_ref="object_arpc_auditctl" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="object_arpc_auditctl" version="1">
    <ind:filepath>/usr/lib/systemd/system/auditd.service</ind:filepath>
    <ind:pattern operation="pattern match">^ExecStartPost=\-\/sbin\/auditctl.*$</ind:pattern>
    <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
  </ind:textfilecontent54_object>

  <ind:textfilecontent54_test check="all" check_existence="all_exist" comment="audit auditctl suid sgid" id="test_arpc_suid_sgid_auditctl" version="1">
    <ind:object object_ref="object_arpc_suid_sgid_auditctl" />
    <ind:state state_ref="state_audit_rules_privileged_commands" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="object_arpc_suid_sgid_auditctl" version="1">
    <ind:filepath>/etc/audit/audit.rules</ind:filepath>
    <ind:pattern operation="pattern match">^[\s]*(-a always,exit -F path=[^\n]+ -F perm=x -F auid>=1000 -F auid!=4294967295 -k privileged)[\s]*$</ind:pattern>
    <ind:instance operation="greater than or equal" datatype="int">1</ind:instance>
    <filter action="exclude">state_proper_audit_rule_but_for_unprivileged_command</filter>
  </ind:textfilecontent54_object>

  <local_variable id="variable_count_of_privileged_commands_having_audit_definition_auditctl" comment="count of suid / sgid binaries having full audit rule definition in /etc/audit/audit.rules file" datatype="int" version="1">
    <count>
      <object_component object_ref="object_arpc_suid_sgid_auditctl" item_field="subexpression" />
    </count>
  </local_variable>
  <ind:variable_state id="state_count_of_privileged_commands_having_audit_definition_auditctl" version="1">
    <ind:value operation="equals" datatype="int" var_ref="variable_count_of_privileged_commands_having_audit_definition_auditctl" var_check="at least one" />
  </ind:variable_state>
  <ind:variable_test check="all" check_existence="all_exist" id="test_arpc_bin_count_equals_rules_count_auditctl" comment="audit auditctl binaries count matches rules count" version="1">
    <ind:object object_ref="object_count_of_suid_sgid_binaries_on_system" />
    <ind:state state_ref="state_count_of_privileged_commands_having_audit_definition_auditctl" />
  </ind:variable_test>

</def-group>
