<def-group>
  <definition class="compliance" id="audit_rules_privileged_commands" version="1">
    {{{ oval_metadata("Audit rules about the information on the use of privileged commands are enabled.") }}}

    <criteria operator="OR">

      <!-- Test the augenrules case -->
      <criteria operator="AND">
        <extend_definition comment="audit augenrules" definition_ref="audit_rules_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">
        <extend_definition comment="audit auditctl" definition_ref="audit_rules_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_all_privileged_commands" comment="All privileged commands" datatype="string" version="1">
    <object_component object_ref="object_system_privileged_commands" item_field="filepath" />
  </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_all_privileged_commands" 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_all_privileged_commands" var_check="at least one"/>
  </ind:textfilecontent54_state>

  <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=([\S]+) )+-F auid>={{{ auid }}} -F auid!=(?:4294967295|unset)[\s]+(?:-k[\s]+|-F[\s]+key=)[\S]+[\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>

  <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=([\S]+) )+-F auid>={{{ auid }}} -F auid!=(?:4294967295|unset)[\s]+(?:-k[\s]+|-F[\s]+key=)[\S]+[\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>
