#!/bin/sh

# shellcheck disable=SC1090
# shellcheck disable=SC2034
# shellcheck disable=SC2154

# audit_dvfilter
#
# Check Dvfilter
#
# Refer to http://pubs.vmware.com/vsphere-55/topic/com.vmware.vsphere.ext_solutions.doc/GUID-6013E15D-92CE-4970-953C-ACCB36ADA8AD.html
# Refer to http://pubs.vmware.com/vsphere-55/topic/com.vmware.vsphere.security.doc/GUID-CD0783C9-1734-4B9A-B821-ED17A77B0206.html
#.

audit_dvfilter () {
  print_function "audit_dvfilter"
  string="Dvfilter"
  check_message  "${string}"
  if [ "${os_name}" = "VMkernel" ]; then
    backup_file="${work_dir}/dvfilter"
    command="esxcli --formatter=csv --format-param=fields=Path,Int Value system settings advanced list | grep /Net/DVFilterBindIpAddress | cut -f2 -d,"
    command_message "${command}"
    current_value=$( eval "${command}" )
    if [ "${audit_mode}" != "2" ]; then
      if [ "${current_value}" != "0" ]; then
        if [ "${audit_mode}" = "0" ]; then
          echo "${current_value}" > "${backup_file}"
          set_message "Dvfilter to disabled"
          command="esxcli system settings advanced set -o /Net/DVFilterBindIpAddress -d"
          command_message "${command}"
          eval "${command}"
        fi
        if [ "${audit_mode}" = "1" ]; then
          inc_insecure "Dvfilter enabled"
          fix_message  "esxcli system settings advanced set -o /Net/DVFilterBindIpAddress -d"
        fi
      else
        if [ "${audit_mode}" = "1" ]; then
          inc_secure   "Dvfilter disabled"
        fi
      fi
    else
      restore_file="${restore_dir}/dvfilter"
      if [ -f "${restore_file}" ]; then
        previous_value=$( cat "${restore_file}" )
        if [ "${previous_value}" != "${current_value}" ]; then
          restore_message "Dvfilter to \"${previous_value}\""
          command="esxcli system settings advanced set -o /Net/DVFilterBindIpAddress -i \"${previous_value}\""
          command_message "${command}"
          eval "${command}"
        fi
      fi
    fi
  else
    na_message "${string}"
  fi
}
