#!/bin/bash
function determineInit() {
    if which systemctl >/dev/null 2>&1; then
        local init='systemd'
    else
        local init='sysvinit'
    fi
    echo $init
}

function determineFirewall() {
    local init=$1
    if [[ -f /etc/csf/csf.conf ]] && [[ -x $(which csf) ]]; then
        if [[ $init == 'systemd' ]]; then
            if systemctl status csf >/dev/null 2>&1; then
                local fw='csf'
                echo $fw
                return
            fi
        elif [[ $init == 'sysvinit' ]]; then
            if /etc/init.d/csf status >/dev/null 2>&1; then
                local fw='csf'
                echo $fw
                return
            fi
        fi
    elif [[ -x /usr/sbin/openport ]] && rpm -qa |grep -q eig-firewall; then
        local fw='eig-firewall'
        echo $fw
        return
    elif [[ -x /usr/sbin/firewall ]] && [[ -f /etc/firewall/INPUT ]]; then
        if [[ $init == 'systemd' ]]; then
            if systemctl status firewall; then
                local fw='hg-legacy'
                echo $fw
                return
            fi
        elif [[ $init == 'sysvinit' ]]; then
            if chkconfig --list firewall |grep -q '4:on'; then
                local fw='hg-legacy'
                echo $fw
                return
            fi
        fi
    elif [[ -x /usr/local/sbin/apf ]]; then
        local fw='apf'
        echo $fw
        return
    fi
    if [[ ! -z $fw ]]; then
        local fw='undetermined'
        echo $fw
        return
    fi
}

function doCSF() {
    local init=$1
    local port=$2
    local openTCP=$(grep '^TCP_OUT' /etc/csf/csf.conf |awk '{print $3}' |sed "s/\"//g" |tr ',' ' ')
    local openUDP=$(grep '^UDP_OUT' /etc/csf/csf.conf |awk '{print $3}' |sed "s/\"//g" |tr ',' ' ')
    if ! echo $openTCP | grep -qo $port; then
        openTCP="$openTCP $port"
    else
        echo "[!] Port [$port] already opened for TCP, not adding it"
    fi
    if ! echo $openUDP | grep -qo $port; then
        openUDP="$openUDP $port"
    else
        echo "[!] Port [$port] already opened for UDP, not adding it"
    fi
    local timestamp=$(date +%s)
    echo "[*] Backing up config as /etc/csf/csf.conf.$timestamp"
    cp -p /etc/csf/csf.conf /etc/csf/csf.conf.$timestamp
    sed -i "s/^TCP_OUT\ =\ .*$/TCP_OUT = \"$(echo $openTCP | tr ' ' ',')\"/g" /etc/csf/csf.conf
    sed -i "s/^UDP_OUT\ =\ .*$/UDP_OUT = \"$(echo $openUDP | tr ' ' ',')\"/g" /etc/csf/csf.conf
    echo "[*] Rules added, restarting CSF"
    csf -r >/dev/null
}

function doEIG() {
    local port=$1
    local taskID=$@
    /usr/sbin/openport --port $port --ticket $taskID
}

function doHGLegacy() {
    local init=$1
    local port=$2
    if ! grep -q "^$port$" /etc/firewall/OUTPUT; then
        echo $port >> /etc/firewall/OUTPUT
    else
        echo "[!] Port [$port] is already opened, not adding it"
        return
    fi
    echo "[*] Rules added, restarting firewall"
    if [[ $init == 'systemd' ]]; then
        systemctl restart firewall
    elif [[ $init == 'sysvinit' ]]; then
        /etc/init.d/firewall restart >/dev/null
    fi
}

function doAPF() {
    local port=$1
    local timestamp=$(date +%s)
    echo "[*] Backing up config as /etc/apf/allow_hosts.rules.$timestamp"
    cp -p /etc/apf/allow_hosts.rules /etc/apf/allow_hosts.rules.$timestamp
    if ! grep -q "tcp:out:d=$port:s=0.0.0.0" /etc/apf/allow_hosts.rules; then
        echo "tcp:out:d=$port:s=0.0.0.0" >> /etc/apf/allow_hosts.rules
    else
        echo "[!] Port [$port] already opened for TCP, not adding it"
    fi
    if ! grep -q "udp:out:d=$port:s=0.0.0.0" /etc/apf/allow_hosts.rules; then
        echo "udp:out:d=$port:s=0.0.0.0" >> /etc/apf/allow_hosts.rules
    else
        echo "[!] Port [$port] already opened for UDP, not adding it"
    fi
    echo "[*] Rules added, restarting apf"
    apf -r >/dev/null
}

function doGeneric() {
    local port=$1
    iptables -I OUTPUT -p tcp --dport $port -j ACCEPT
    iptables -I OUTPUT -p udp --dport $port -j ACCEPT
    echo "[!] Unable to reliably determine what type of firewall is installed on this server"
    echo "[!!!] Temporarily opening port in the firewall. This *IS NOT* permanent. Please create a ticket so an admin can follow up"
}


declare -i port='port_placeholder'
declare -u taskID='task_placeholder'

init=$(determineInit)
fw=$(determineFirewall $init)
echo "[*] Firewall type: $fw"
echo "[*] Opening port: $port"

case $fw in
    csf)
        doCSF $init $port
    ;;
    eig-firewall)
        doEIG $port $taskID
    ;;
    hg-legacy)
        doHGLegacy $init $port
    ;;
    apf)
        doAPF $port
    ;;
    undetermined)
        doGeneric $port
    ;;
esac
echo "[*] Done"