<def-group>
  <definition class="compliance" id="file_permissions_var_log_audit" version="2">
    {{{ oval_metadata("Checks for correct permissions for all audit log files.") }}}
    <criteria operator="OR">
      {{% if 'ol' not in product and "rhel" not in product and "rhcos4" not in product %}}
      <criteria operator="AND" comment="log_file set">
        <extend_definition comment="log_file set in auditd.conf" definition_ref="auditd_conf_log_file_not_set" negate="true" />
        <criteria operator="AND" comment="log_group in auditd.conf is not root">
          <extend_definition comment="log_group in auditd.conf is not root"
          definition_ref="auditd_conf_log_group_not_root" />
          <criterion test_ref="test_file_permissions_audit_log-non_root" negate="true" />
        </criteria>
        <criterion test_ref="test_file_permissions_audit_log" negate="true" />
      </criteria>
      <criteria operator="AND" comment="log_group in auditd.conf is not root">
        <extend_definition comment="log_group in auditd.conf is not root"
        definition_ref="auditd_conf_log_group_not_root" />
        <criterion test_ref="test_file_permissions_var_log_audit-non_root" negate="true" /> 
      </criteria>
      <criterion test_ref="test_file_permissions_var_log_audit" negate="true" />
      {{% else %}}
      <criteria operator="AND" comment="log_file set">
        <extend_definition comment="log_file set in auditd.conf" definition_ref="auditd_conf_log_file_not_set" negate="true" />
        <criterion test_ref="test_file_permissions_audit_log" negate="true" />
      </criteria>
      <criteria operator="AND" comment="log_file not set">
        <extend_definition comment="log_file not set in auditd.conf" definition_ref="auditd_conf_log_file_not_set"/>
        <criterion test_ref="test_file_permissions_default_audit_log" negate="true" />
      </criteria>
      {{% endif %}}
    </criteria>
  </definition>

  <unix:file_test check="all" check_existence="at_least_one_exists" comment="audit log files mode 0600" id="test_file_permissions_audit_log" version="1">
    <unix:object object_ref="object_audit_log_files" />
    <unix:state state_ref="state_not_mode_0600" />
  </unix:file_test>
  <unix:file_object comment="/var/log/audit files" id="object_audit_log_files" version="1">
    <unix:filepath operation="pattern match" var_ref="audit_log_file_path" />
    <filter action="include">state_not_mode_0600</filter>
  </unix:file_object>

  {{% if 'ol' in product or "rhel" in product or "rhcos4" in product %}}
  <unix:file_test check="all" check_existence="at_least_one_exists"
      comment="default audit log files mode 0600"
      id="test_file_permissions_default_audit_log" version="1">
    <unix:object object_ref="object_audit_default_log_files" />
    <unix:state state_ref="state_not_mode_0600" />
  </unix:file_test>
  <unix:file_object comment="/var/log/audit/audit.log files"
      id="object_audit_default_log_files" version="1">
    <unix:filepath operation="equals">/var/log/audit/audit.log</unix:filepath>
    <filter action="include">state_not_mode_0600</filter>
  </unix:file_object>

  {{% else %}}
  <unix:file_test check="all" check_existence="at_least_one_exists" comment="/var/log/audit files mode 0600" id="test_file_permissions_var_log_audit" version="1">
    <unix:object object_ref="object_var_log_audit_files" />
    <unix:state state_ref="state_not_mode_0600" />
  </unix:file_test>
  <unix:file_object comment="/var/log/audit files" id="object_var_log_audit_files" version="1">
    <unix:behaviors recurse="directories" recurse_direction="down" max_depth="-1" recurse_file_system="local" />
    <unix:path operation="equals">/var/log/audit</unix:path>
    <unix:filename operation="pattern match">^.*$</unix:filename>
    <filter action="include">state_not_mode_0600</filter>
  </unix:file_object>

  <unix:file_test check="all" check_existence="at_least_one_exists" comment="audit log files mode 0640" id="test_file_permissions_audit_log-non_root" version="1">
    <unix:object object_ref="object_audit_log_files-non_root" />
    <unix:state state_ref="state_not_mode_0640" />
  </unix:file_test>
  <unix:file_object comment="audit log files" id="object_audit_log_files-non_root" version="1">
    <unix:filepath operation="pattern match" var_ref="audit_log_file_path" />
    <filter action="include">state_not_mode_0640</filter>
  </unix:file_object>

  <unix:file_test check="all" check_existence="at_least_one_exists" comment="/var/log/audit files mode 0640" id="test_file_permissions_var_log_audit-non_root" version="1">
    <unix:object object_ref="object_var_log_audit_files-non_root" />
    <unix:state state_ref="state_not_mode_0640" />
  </unix:file_test>
  <unix:file_object comment="/var/log/audit files" id="object_var_log_audit_files-non_root" version="1">
    <unix:behaviors recurse="directories" recurse_direction="down" max_depth="-1" recurse_file_system="local" />
    <unix:path operation="equals">/var/log/audit</unix:path>
    <unix:filename operation="pattern match">^.*$</unix:filename>
    <filter action="include">state_not_mode_0640</filter>
  </unix:file_object>

  <unix:file_state id="state_not_mode_0640" version="1" operator="OR">
    <!-- if any one of these is true then mode is NOT 0640 (hence the OR operator) -->
    <unix:suid datatype="boolean">true</unix:suid>
    <unix:sgid datatype="boolean">true</unix:sgid>
    <unix:sticky datatype="boolean">true</unix:sticky>
    <unix:uexec datatype="boolean">true</unix:uexec>
    <unix:gwrite datatype="boolean">true</unix:gwrite>
    <unix:gexec datatype="boolean">true</unix:gexec>
    <unix:oread datatype="boolean">true</unix:oread>
    <unix:owrite datatype="boolean">true</unix:owrite>
    <unix:oexec datatype="boolean">true</unix:oexec>
  </unix:file_state>
  {{% endif %}}

  <unix:file_state id="state_not_mode_0600" version="1" operator="OR">
    <!-- if any one of these is true then mode is NOT 0600 (hence the OR operator) -->
    <unix:suid datatype="boolean">true</unix:suid>
    <unix:sgid datatype="boolean">true</unix:sgid>
    <unix:sticky datatype="boolean">true</unix:sticky>
    <unix:uexec datatype="boolean">true</unix:uexec>
    <unix:gread datatype="boolean">true</unix:gread>
    <unix:gwrite datatype="boolean">true</unix:gwrite>
    <unix:gexec datatype="boolean">true</unix:gexec>
    <unix:oread datatype="boolean">true</unix:oread>
    <unix:owrite datatype="boolean">true</unix:owrite>
    <unix:oexec datatype="boolean">true</unix:oexec>
  </unix:file_state>

</def-group>
