#!/bin/bash
source /etc/profile.d/red[bB]order-manager.sh
##########  Variable Declarations - Start  ##########
#name of the current script
SCRIPT_NAME=configure-fortinet.sh
#version of the current script
SCRIPT_VERSION=1.0

LOGSTASH_ETCDIR_CONF=$LOGSTASH_PIPELINES_PATH/vault

#name of the service, in this case fortinet
SERVICE="fortinet"

RB_PATH=${RB_PATH:=""}
RB_EXTENSIONS_DIR="$RB_PATH/var/rb-extensions"

#name and location of fortinet logstash file
LOGSTASH_CONFFILE=$LOGSTASH_ETCDIR_CONF/30_fortinet.conf

MANUAL_CONFIG_INSTRUCTION="Manual instructions to configure fortinet is available at https://suport.redborder.com/docs/fortinet-server-logs#manual. Logstash troubleshooting instructions are available at https://support.redborder.com/docs/troubleshooting-logstash/"

INSTALLED_FILE=$EXTENSION_DIR/extension-installed.txt


##########  Variable Declarations - End  ##########

##########  Qradar Functions  - Start   ###########

#function to write the contents of  logstash config file

writeFileContents()
{
  echo "INFO" "INFO: Creating file $LOGSTASH_CONFFILE"
  sudo touch $LOGSTASH_CONFFILE
  sudo chmod o+w $LOGSTASH_CONFFILE

        if [ ! -f $DIMENSIONS_CONFFILE ]; then
          echo "cannot find the dimension file to setup the logstash pipeline. exiting..."
          exit 1
        fi

        # build the array of original dimensions
ORIG_DIM=$(
ruby - <<END
  require 'yaml'
  original_dimensions = []
  dimensions = {}
  dimensions = dimensions.merge(YAML.load_file("$DIMENSIONS_CONFFILE"))
  dimensions.each { | key, dimension | original_dimensions.push(dimension['original_dimension']) }
  puts original_dimensions.to_s
END
)

  imfileStr="filter {
        if \"devid\" in [message] and \"devname\" in [message] {


  kv {
    source => \"message\"
    prefix => \"fortinet_\"
  }

 }
}
"
  
  
  #change the fortinet-30 file to variable from above and also take the directory of the fortinet log file.
sudo cat << EOIPFW > $LOGSTASH_CONFFILE
$imfileStr
EOIPFW

  restartLogstash
}



#function to write the contents of qradar logstash config file
writeDimensionsFile()
{
	echo "INFO: Creating dimensions file $DIMENSIONS_CONFFILE"
	sudo touch $DIMENSIONS_CONFFILE
	sudo chmod o+w $DIMENSIONS_CONFFILE
         
imfileStr=$SERVICE'_devname:
  name: "devname"
  category: "fortinet"
  type: string
  original_dimension: "devname"
#'$SERVICE'_time:
#  name: "time"
#  category: "fortinet"
#  type: string
#  original_dimension: "time"
'$SERVICE'_devid:
  name: "devid"
  category: "fortinet"
  type: string
  original_dimension: "devid"
#'$SERVICE'_logid:
#  name: "logid"
#  category: "fortinet"
#  type: string
#  original_dimension: "logid"
'$SERVICE'_type:
  name: "type"
  category: "fortinet"
  type: string
  original_dimension: "type"
'$SERVICE'_subtype:
  name: "subtype"
  category: "fortinet"
  type: string
  original_dimension: "subtype"
#'$SERVICE'_eventtype:
#  name: "eventtype"
#  category: "fortinet"
#  type: string
#  original_dimension: "eventtype"
'$SERVICE'_level:
  name: "level"
  category: "fortinet"
  type: string
  original_dimension: "level"
#'$SERVICE'_vd:
#  name: "vd"
#  category: "fortinet"
#  type: string
#  original_dimension: "vd"
#'$SERVICE'_appid:
#  name: "appid"
#  category: "fortinet"
#  type: string
#  original_dimension: "appid"
#'$SERVICE'_user:
#  name: "user"
#  category: "fortinet"
#  type: string
#  original_dimension: "user"
#'$SERVICE'_group:
#  name: "group"
#  category: "fortinet"
#  type: string
#  original_dimension: "group"
'$SERVICE'_srcip:
  name: "srcip"
  category: "fortinet"
  type: string
  original_dimension: "srcip"
'$SERVICE'_srcport:
  name: "srcport"
  category: "fortinet"
  type: string
  original_dimension: "srcport"
#'$SERVICE'_srcintf:
#  name: "srcintf"
#  category: "fortinet"
#  type: string
#  original_dimension: "srcintf"
'$SERVICE'_dstip:
  name: "dstip"
  category: "fortinet"
  type: string
  original_dimension: "dstip"
'$SERVICE'_dstport:
  name: "dstport"
  category: "fortinet"
  type: string
  original_dimension: "dstport"
#'$SERVICE'_dstintf:
#  name: "dstintf"
#  category: "fortinet"
#  type: string
#  original_dimension: "dstintf"
#'$SERVICE'_proto:
#  name: "proto"
#  category: "fortinet"
#  type: string
#  original_dimension: "proto"
#'$SERVICE'_policyid:
#  name: "policyid"
#  category: "fortinet"
#  type: string
#  original_dimension: "policyid"
#'$SERVICE'_sessionid:
#  name: "sessionid"
#  category: "fortinet"
#  type: string
#  original_dimension: "sessionid"
#'$SERVICE'_applist:
#  name: "applist"
#  category: "fortinet"
#  type: string
#  original_dimension: "applist"
#'$SERVICE'_appcat:
#  name: "appcat"
#  category: "fortinet"
#  type: string
#  original_dimension: "appcat"
'$SERVICE'_app:
  name: "app"
  category: "fortinet"
  type: string
  original_dimension: "app"
'$SERVICE'_action:
  name: "action"
  category: "fortinet"
  type: string
  original_dimension: "action"
#'$SERVICE'_hostname:
#  name: "hostname"
#  category: "fortinet"
#  type: string
#  original_dimension: "hostname"
#'$SERVICE'_url:
#  name: "url"
#  category: "fortinet"
#  type: string
#  original_dimension: "url"
#'$SERVICE'_msg:
#  name: "msg"
#  category: "fortinet"
#  type: string
#  original_dimension: "msg"
#'$SERVICE'_apprisk:
#  name: "apprisk"
#  category: "fortinet"
#  type: string
#  original_dimension: "apprisk"
#'$SERVICE'_poluuid:
#  name: "poluuid"
#  category: "fortinet"
#  type: string
#  original_dimension: "poluuid"
#'$SERVICE'_status:
#  name: "status"
#  category: "fortinet"
#  type: string
#  original_dimension: "status"
#'$SERVICE'_trandisp:
#  name: "trandisp"
#  category: "fortinet"
#  type: string
#  original_dimension: "trandisp"
#'$SERVICE'_duration:
#  name: "duration"
#  category: "fortinet"
#  type: string
#  original_dimension: "duration"
'$SERVICE'_sentbyte:
  name: "sentbyte"
  category: "fortinet"
  type: string
  original_dimension: "sentbyte"
'$SERVICE'_rcvbyte:
  name: "rcvbyte"
  category: "fortinet"
  type: string
  original_dimension: "rcvbyte"
#'$SERVICE'_devtype:
#  name: "devtype"
#  category: "fortinet"
#  type: string
#  original_dimension: "devtype"
#'$SERVICE'_osname:
#  name: "osname"
#  category: "fortinet"
#  type: string
#  original_dimension: "osname"
#'$SERVICE'_mastersrcmac:
#  name: "mastersrcmac"
#  category: "fortinet"
#  type: string
#  original_dimension: "mastersrcmac"
#'$SERVICE'_srcmac:
#  name: "srcmac"
#  category: "fortinet"
#  type: string
#  original_dimension: "srcmac"
#'$SERVICE'_crscore:
#  name: "crscore"
#  category: "fortinet"
#  type: string
#  original_dimension: "csrcore"
#'$SERVICE'_craction:
#  name: "craction"
#  category: "fortinet"
#  type: string
#  original_dimension: "craction"
#'$SERVICE'_crlevel:
#  name: "crlevel"
#  category: "fortinet"
#  type: string
#  original_dimension: "crlevel"'

sudo cat << EOIPFW > $DIMENSIONS_CONFFILE
$imfileStr
EOIPFW
	
}

##########  Qradar Functions  - End   ###########


##########  Start of the common part  ###########


#downloads configure-extensions.sh
setupDependencies() {
  ERROR="true"
  # Check if there is internet or/and can access to the web of the script
  response=$(curl --write-out %{http_code} --silent --output /dev/null https://repo.redborder.com/install/common/configure-extensions.sh)
  if [[ $response -eq 200 ]]; then
    echo "INFO: Downloading dependencies.."
    curl -s -o configure-extensions.sh https://repo.redborder.com/install/common/configure-extensions.sh
  else
    echo "configure-extensions script could not be retrieved, local copy is needed"
  fi
  
  if [ -f configure-extensions.sh ]; then
    if [ `head -1 configure-extensions.sh | grep bash | wc -l` -gt 0 ]; then
      source configure-extensions.sh "being-invoked"

      #definitions of variables relying on the configure-extensions-script
      EXTENSION_DIR=$RB_EXTENSIONS_DIR/$SERVICE
      DIMENSIONS_CONFFILE=$EXTENSION_DIR/dimensions.yml

      ERROR="false"
    else
      echo "local configure-extensions script is not valid, we need to exit"  
    fi
  fi
  
  if [ "$ERROR" != "false" ]; then
    echo "ERROR: Missing dependencies!"
    exit 1
  fi
}


#display usage syntax
usage()
{
cat << EOF
usage: configure-$SERVICE [-i to install]
usage: configure-$SERVICE [-r to rollback]
usage: configure-$SERVICE [-v for version]
usage: configure_$SERVICE [-u to update]
usage: configure-$SERVICE [-h for help]
EOF
}

##########  Get Inputs from User - Start  ##########

if [ $# -eq 0 ]; then
    usage
  exit
else
while [ "$1" != "" ]; do
    case $1 in
      -r | --rollback )
      REDBORDER_ROLLBACK="true"
          ;;
      -i | --install )
                  #SUPPRESS_PROMPT="true"
                  INSTALL="true"
          ;;    
      -v | --version )
          echo "The version of $SERVICE is $SCRIPT_VERSION, to check for any update use configure-$SERVICE -u"
          exit
          ;;
      -u | --update )
          echo "Checking for updates..."
          UPDATING="true"
          ;;
      -h | --help )
          usage
          exit
          ;;

    esac
    shift
done
fi

if [ "$REDBORDER_ROLLBACK" != "" ]; then
    setupDependencies
    checkIfUserHasRootPrivileges
    removeRedborderConf
elif [ "$INSTALL" == "true" ]; then
    setupDependencies
    checkIfUserHasRootPrivileges
    installRedborderConf
elif [ "$UPDATING" == "true" ]; then
    setupDependencies
    checkIfUserHasRootPrivileges
    checkForUpdate

else
    usage
fi

##########  Get Inputs from User - End  ##########

