<def-group>
  <definition class="compliance" id="file_permissions_var_log_audit" version="2">
    {{{ oval_metadata("Checks for correct permissions for all audit log files.", rule_title=rule_title) }}}

    <criteria operator="OR">
      <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="OR">
          <criteria operator="AND" comment="log_file set and log_group set to not root">
            <extend_definition comment="log_group in auditd.conf is not set to root" definition_ref="auditd_conf_log_group_not_root" />
            <criterion comment="non-default log_file and log_group set to non-root" test_ref="test_file_permissions_audit_log-non_root" negate="true" />
          </criteria>
          <criteria operator="AND" comment="log_file set and log_group root or not set">
            <extend_definition comment="log_group in auditd.conf is set to root or not set" definition_ref="auditd_conf_log_group_not_root" negate="true"/>
            <criterion comment="non-default log_file and log_group root or not set" test_ref="test_file_permissions_audit_log" negate="true" />
          </criteria>
        </criteria>

      </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" />

        <criteria operator="OR">
          <criteria operator="AND" comment="default log_file and log_group set to not root">
            <extend_definition comment="log_group in auditd.conf is not set to root" definition_ref="auditd_conf_log_group_not_root" />
            <criterion comment="default log_file and log_group set to non-root" test_ref="test_file_permissions_var_log_audit-non_root" negate="true" />
          </criteria>
          <criteria operator="AND" comment="default log_file and log_group root or not set">
            <extend_definition comment="log_group in auditd.conf is set to root or not set" definition_ref="auditd_conf_log_group_not_root" negate="true"/>
            <criterion comment="default log_file and log_group root or not set" test_ref="test_file_permissions_var_log_audit" negate="true" />
          </criteria>
        </criteria>

      </criteria>
    </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>

  <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>

  <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>

  <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>
