#!/bin/bash
# +--------------------------------------------------------------------+
# eFa-Configure
# Version 20181224
# +--------------------------------------------------------------------+
# Copyright (C) 2012~2021  https://www.efa-project.org
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
# +--------------------------------------------------------------------+

# +---------------------------------------------------+
# Enable Extended Globs
# +---------------------------------------------------+
shopt -s extglob
# +---------------------------------------------------+

# +---------------------------------------------------+
# Load functions
# +---------------------------------------------------+
. /var/eFa/lib/eFa-Configure/func_getipsettings
. /var/eFa/lib/eFa-Configure/func_apachesettings
. /var/eFa/lib/eFa-Configure/func_mailsettings
. /var/eFa/lib/eFa-Configure/func_systemrestore
. /var/eFa/lib/eFa-Configure/func_recovermariadb
. /var/eFa/lib/eFa-Configure/func_spamsettings
. /var/eFa/lib/eFa-Configure/func_virussettings
. /var/eFa/lib/eFa-Configure/func_askcleandeliver
. /var/eFa/lib/eFa-Configure/func_asknonspam
. /var/eFa/lib/eFa-Configure/func_askspam
. /var/eFa/lib/eFa-Configure/func_ipsettings
. /var/eFa/lib/eFa-Configure/func_greylisting
. /var/eFa/lib/eFa-Configure/func_tunables
. /var/eFa/lib/eFa-Configure/func_tunables_children
. /var/eFa/lib/eFa-Configure/func_tunables_procdb
. /var/eFa/lib/eFa-Configure/func_setipsettings
. /var/eFa/lib/eFa-Configure/func_askmaxsize
. /var/eFa/lib/eFa-Configure/func_mailwatchsettings
. /var/eFa/lib/eFa-Configure/func_resetadmin
. /var/eFa/lib/eFa-Configure/func_askmaxsizemailwatch
. /var/eFa/lib/eFa-Configure/func_askhighspammailwatch
. /var/eFa/lib/eFa-Configure/func_askmalwarepatrol
. /var/eFa/lib/eFa-Configure/func_retention
. /var/eFa/lib/eFa-Configure/func_maintenance
. /var/eFa/lib/eFa-Configure/func_peruser
. /var/eFa/lib/eFa-Configure/func_letsencrypt
. /var/eFa/lib/eFa-Configure/func_askdccservers
. /var/eFa/lib/eFa-Configure/func_dkim_dmarc
. /var/eFa/lib/eFa-Configure/func_webmin
. /var/eFa/lib/eFa-Configure/func_maxmind
. /var/eFa/lib/eFa-Configure/func_fail2ban
. /var/eFa/lib/eFa-Configure/func_backup
. /var/eFa/lib/eFa-Configure/func_asksigrules
. /var/eFa/lib/eFa-Configure/func_trustednetworks

# +---------------------------------------------------+

# +---------------------------------------------------+
# Get version
# +---------------------------------------------------+
eFaVERSION="`head /etc/eFa-Version`"
# +---------------------------------------------------+

# +---------------------------------------------------+
# Display menus
# +---------------------------------------------------+
show_menu() {
  menu=1
  while [ $menu == "1" ]
    do
      func_echo-header
      echo -e ""
      echo -e "You are currently running $green$eFaVERSION$clean."
      echo -e ""
      echo -e "Please choose an option:"
      echo -e " "
      echo -e "0) Logout from ssh                    10) Apache Settings"
      echo -e "1) Shell                              11) Virus Settings"
      echo -e "2) Reboot system                      12) System Restore"
      echo -e "3) Halt system                        13) Update Now"
      echo -e "4) IP Settings                        14) Maintenance Mode"
      echo -e "5) Tunables                           15) Let's Encrypt"
      echo -e "6) MailWatch Settings                 16) DKIM and DMARC"
      echo -e "7) Mail Settings                      17) Webmin"
      echo -e "8) Spam Settings                      18) Fail2Ban"
      echo -e "9) MariaDB Recovery                   19) Auto-Backup settings"
      echo -e -n "$green[eFa]$clean : "
      local choice
      read choice
      case $choice in
                    0) clear; SSHTTY=`/usr/bin/tty |awk -F '/' '{print $3"/"$4}'`; SSHPID=`ps aux | egrep "sshd: [a-zA-Z]+@" | grep -w $SSHTTY | awk '{print $2}'`; kill $SSHPID ;;
                    1) exit 0 ;;
                    2) func_reboot ;;
                    3) func_halt ;;
                    4) func_ip-settings ;;
                    5) func_tunables ;;
                    6) func_mailwatch-settings ;;
                    7) func_mail-settings ;;
                    8) func_spam-settings ;;
                    9) func_recover-mariadb ;;
                   10) func_apache-settings ;;
                   11) func_virus-settings ;;
                   12) func_system-restore ;;
                   13) func_update-now ;;
                   14) func_maintenance-mode ;;
                   15) func_lets-encrypt ;;
                   16) func_dkim-dmarc ;;
                   17) func_webmin ;;
                   18) func_fail2ban ;;
                   19) func_backup-settings ;;
                    *) echo -e "Error \"$choice\" is not an option..." && sleep 2
      esac
    done
}
# +---------------------------------------------------+

# +---------------------------------------------------+
# Reboot function
# +---------------------------------------------------+
func_reboot() {
  menu=0
  rebootmenu=1
  while [ $rebootmenu == "1" ]
    do
      func_echo-header
      echo -e "Are you sure you want to reboot this host?"
      echo -e ""
      echo -e "Y)  Yes I am sure"
      echo -e "N)  No no no take me back!"
      echo -e ""
      echo -e -n "$green[eFa]$clean : "
      local choice
      read choice
      case $choice in
                    Y) reboot && exit 0 ;;
                    N) menu=1 && return  ;;
                    n) menu=1 && return  ;;
                    *) echo -e "Error \"$choice\" is not an option..." && sleep 2
      esac
    done
}
# +---------------------------------------------------+

# +---------------------------------------------------+
# Halt function
# +---------------------------------------------------+
func_halt() {
  menu=0
  haltmenu=1
  while [ $haltmenu == "1" ]
    do
      func_echo-header
      echo -e "Are you sure you want to halt this host?"
      echo -e ""
      echo -e "Y)  Yes I am sure"
      echo -e "N)  No no no take me back!"
      echo -e ""
      echo -e -n "$green[eFa]$clean : "
      local choice
      read choice
      case $choice in
                    Y) shutdown -h now && exit 0 ;;
                    N) menu=1 && return  ;;
                    n) menu=1 && return  ;;
                    *) echo -e "Error \"$choice\" is not an option..." && sleep 2
      esac
    done
}
# +---------------------------------------------------+

# +---------------------------------------------------+
# Update Now function
# +---------------------------------------------------+
func_update-now() {
  echo ""
  local yesno
  read -p "Update now, are you sure? (y/N): " yesno
  if [[ $yesno =~ ^[Yy] ]]; then
    yum -y update
  fi
  pause
}
# +---------------------------------------------------+

# +---------------------------------------------------+
# Function to test IP addresses
# +---------------------------------------------------+
function checkip4(){
  local ip=$1
  local stat=1

  if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        OIFS=$IFS
        IFS='.'
        ip=($ip)
        IFS=$OIFS
        [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
        stat=$?
  fi
  return $stat
}
# +---------------------------------------------------+

# +---------------------------------------------------+
# Function to test IP addresses in CIDR notation
# +---------------------------------------------------+
function checkip4cidr(){
  local ip=$1
  local stat=1

  if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(\/[0-9]{1,2})?$ ]]; then
        OIFS=$IFS
        IFS='/'
        ipcidr=($ip)
        cidr=${ipcidr[1]}
        ip=${ipcidr[0]}
        IFS='.'
        ip=($ip)
        IFS=$OIFS
        [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 && $cidr -lt 33 ]]
        stat=$?
  fi
  return $stat
}
# +---------------------------------------------------+

# +---------------------------------------------------+
function checkip6()
{
  local  ip=$1

  if [[ $ip =~ ^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])$ ]]; then
    return 0
  fi
  return 1
}
# +---------------------------------------------------+

# +---------------------------------------------------+
# Trap CTRL+C, CTRL+Z and quit singles
# +---------------------------------------------------+
trap '' SIGINT SIGQUIT SIGTSTP
# +---------------------------------------------------+

# +---------------------------------------------------+
# Pause
# +---------------------------------------------------+
pause(){
  read -p "Press [Enter] key to continue..." fackEnterKey
}
# +---------------------------------------------------+

# +---------------------------------------------------+
# Menu header
# +---------------------------------------------------+
func_echo-header(){
  clear
  echo -e "--------------------------------------------------------------"
  echo -e "---        Welcome to the eFa Configuration program        ---"
  echo -e "---               https://www.efa-project.org              ---"
  echo -e "--------------------------------------------------------------"
  echo ""
}
# +---------------------------------------------------+

# +---------------------------------------------------+
# Main logic
# +---------------------------------------------------+
clear
red='\E[00;31m'
green='\E[00;32m'
yellow='\E[00;33m'
blue='\E[00;34m'
magenta='\E[00;35'
cyan='\E[00;36m'
clean='\e[00m'

if [ `whoami` == root ]
    then
        menu="1"
        while [ $menu == "1" ]
        do
            show_menu
        done
    else
        echo -e "$red [eFa] ERROR: Please become root.$clean"
        exit 0
    fi
# +---------------------------------------------------+
