#!/bin/bash

u=$(whoami)
if [ "x$u" != "xroot" ]; then
  sudo /bin/xlinux-security-firewall "$1" "$2" "$3"
  exit 0
fi

function fw_off() {
  # Delete all Rules
  iptables -P INPUT ACCEPT
  iptables -P FORWARD ACCEPT
  iptables -P OUTPUT ACCEPT
  iptables -t nat -F
  iptables -t mangle -F
  iptables -F
  iptables -X
  fw_state "0"
}
function fw_block_all() {
  # Set default chain policies
  iptables -P INPUT DROP
  iptables -P FORWARD DROP
  iptables -P OUTPUT ACCEPT
  
  # Accept on localhost
  iptables -A INPUT -i lo -j ACCEPT
  iptables -A OUTPUT -o lo -j ACCEPT
  
  # Allow established sessions to receive traffic
  iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT  
  fw_state "1"
}
function fw_local_only() {
  # Set default chain policies
  iptables -P INPUT DROP
  iptables -P FORWARD DROP
  iptables -P OUTPUT ACCEPT
  
  # Accept on localhost
  iptables -A INPUT -i lo -j ACCEPT
  iptables -A OUTPUT -o lo -j ACCEPT
  
  # Allow In from Private Net
  iptables -A INPUT -s 192.168.0.0/16 -j ACCEPT
  iptables -A INPUT -s 10.0.0.0/8 -j ACCEPT
  iptables -A INPUT -s 172.16.0.0/12 -j ACCEPT
  iptables -A INPUT -s 169.254.0.0/16 -j ACCEPT
  iptables -A INPUT -s 224.0.0.0/4 -j ACCEPT
  
  # Allow established sessions to receive traffic
  iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
  fw_state "2"
}
function fw_state() {
  m=$1
  xlinux-settings set XLINUXSECURITY_FIREWALL_STATE "$m"
  echo "$m" >/tmp/xlinux-security-firewall.state
}

function notify_if_new() {
  title=$1
  text=$2
  e="x"
  if [ -f /tmp/xlinux-security-firewall.notify ]; then
    e=$(cat /tmp/xlinux-security-firewall.notify)
  fi
  if [ "$title$text" != "$e" ]; then
    notify-send "$title" "$text" >/dev/null 2>&1
    echo "$title$text" > /tmp/xlinux-security-firewall.notify
  fi
}

function get_gateway_mac_1() {
  r=0
  GATEWAY=$(ping -t 1 -c 1 -W 3 -4 8.8.8.8 2>/dev/null | grep ^From | tail -1 | awk '{print $2}') 
  if [ "x$GATEWAY" != "x" ]; then
    MAC=$(arp -n "$GATEWAY" | tail -1 | awk '{print $3}')
  else
    MAC="unbekannt"
  fi
}

function get_gateway_mac() {
  r=0
  ping -c 1 -W 3 -4 8.8.8.8 &>/dev/null ; o=$?
  MAC="unbekannt"
  GATEWAY=""
  if [ $o -eq 0 ]; then
    while read d_net d_route d_mask d_flags d_rest ; do
      if [ "x$d_net" = "x0.0.0.0" ]; then
        if [ "x$d_mask" = "x0.0.0.0" ]; then
          if [[ "x$d_flags" = *"G"* ]]; then
            GATEWAY=$d_route
          fi
        fi
      fi
    done < <(netstat -nr)
  fi
  #GATEWAY=$(ping -t 1 -c 1 -W 3 -4 8.8.8.8 2>/dev/null | grep ^From | tail -1 | awk '{print $2}') 
  if [ "x$GATEWAY" != "x" ]; then
    MAC=$(arp -n "$GATEWAY" | tail -1 | awk '{print $3}')
  else
    MAC="unbekannt"
  fi
}




# MAC-Liste anlegen falls diese nicht existiert
if [ ! -f /etc/xlinux/firewall_known_macs ]; then
  touch /etc/xlinux/firewall_known_macs
fi





if [ "x$1" == "xoff" ]; then
  fw_off
  notify_if_new "Firewall beendet" "Die Firewall wurde abgeschaltet." >/dev/null 2>&1
  exit 0
fi

en=$(xlinux-settings get WARTUNGSSERVICE_AVAILABLE || echo "0")
DEKSTOP_FIREWALL="0"
if [ "x$en" == "x1" ]; then
  DEKSTOP_FIREWALL=$(xlinux-settings get XLINUXSECURITY_FIREWALL || echo "1")
else
  fw_off
  notify_if_new "Geringe Sicherheit" "Das Sicherheitspaket ist nicht verfügbar." >/dev/null 2>&1
  exit 0
fi

if [ "x$1" == "xtrust" ]; then
  # MAC als vertrauenswuedig einstufen
  MAC=$2
  TRUST=$3
  if [ "x$MAC" != "x" ]; then
    if [ "x$TRUST" == "x" ]; then
      sed -i "/^$MAC;/d" /etc/xlinux/firewall_known_macs
    else
      sed -i "s/^$MAC;\([^;]*\);\([^;]*\)\(.*\)/$MAC;\1;$TRUST\3/" /etc/xlinux/firewall_known_macs
    fi
  fi
fi

if [ "x$DEKSTOP_FIREWALL" != "x1" ]; then
  fw_off
  #notify_if_new "Firewall deaktiviert" "Die Firewall wurde im XLinux\nSicherheitscenter deaktiviert." >/dev/null 2>&1
  notify_if_new "Geringe Sicherheit" "Die Firewall wurde im XLinux Sicherheitscenter deaktiviert." >/dev/null 2>&1
  exit 0
fi

get_gateway_mac

TRUSTED_NET=0
if [ "x$MAC" != "xunbekannt" ]; then
  xlinux-settings set XLINUXSECURITY_FIREWALL_CURRENT "$MAC"
  # Alte MAC-Liste
  c=$(xlinux-settings get XLINUXSECURITY_FIREWALL_TRUSTED || echo "1")
  if [[ ",$c," == *",$MAC,"* ]]; then
    TRUSTED_NET=1
  fi
  # Neue MAC-Liste
  c=$(grep "^$MAC;" /etc/xlinux/firewall_known_macs 2>/dev/null | cut -d\; -f3 )
  if [ "x$c" == "x1" ]; then
    TRUSTED_NET=1
  fi
  if [ "x$c" == "x0" ]; then
    TRUSTED_NET=0
  fi
fi


if [ "x$DEKSTOP_FIREWALL" == "x1" ]; then
  fw_off
  if [ $TRUSTED_NET -eq 0 ]; then
    fw_block_all
    #notify_if_new "Firewall aktiviert" "Alle eingehende Verbindungen werden geblockt." >/dev/null 2>&1
    notify_if_new "Hohe Sicherheit" "Internet und Email ist zugelassen, alle eingehende Verbindungen werden blockiert." >/dev/null 2>&1
  else
    fw_local_only
    #notify_if_new "Firewall aktiviert" "Lokale Verbindungen werden zugelassen." >/dev/null 2>&1
    notify_if_new "Normale Sicherheit" "Internet, Email und der Zugriff aus dem lokalen Netzwerk wird zugelassen. Nicht angeforderte Zugriffe aus dem Internet werden blockiert." >/dev/null 2>&1
  fi
else
  fw_off
  #notify_if_new "Firewall deaktiviert" "Die Firewall wurde im XLinux\nSicherheitscenter deaktiviert." >/dev/null 2>&1
  notify_if_new "Keine Sicherheit" "Die Firewall (Netzwerk-Zugriffsschutz) ist inaktiv." >/dev/null 2>&1
fi



# Nutzung Dokumentieren
#if [ "x$GATEWAY" != "x" ]; then
  if [ "x$MAC" != "xunbekannt" ]; then
    D=$(date "+%d.%m.%Y %H:%I:%S")
    if [ ! -f /etc/xlinux/firewall_known_macs ]; then
      touch /etc/xlinux/firewall_known_macs
    fi
    old=$(grep "^$MAC;" /etc/xlinux/firewall_known_macs 2>/dev/null | tail -1)
    if [ "x$old" == "x" ]; then
      # Neues Netzwerk
      #. /bin/xlinux-set-desktop-vars
      #xlinux-security-firewall-admin "$MAC" &
      #notify_if_new "Neues Netzwerk erkannt" "Das Netzwerk wurde als nicht vertrauenswürdiges Netzwerk gespeichert.\nDie Firewall blockiert alle eingehende Verbindungen.\n\nDie Einstellung kann über das Sicherheitscenter geändert werden." >/dev/null 2>&1
      notify_if_new "Hohe Sicherheit." "Neues Netzwerk erkannt und als nicht vertrauenswürdig gespeichert. Die Vertrauenswürdigkeit können Sie über das Sicherheitscenter einstellen." >/dev/null 2>&1
      #xlinux-security-firewall trust "$MAC" 0
      fw_local_only
    fi
    # Bekanntes Netzwerk wieder nach oben schieben
    name=$(grep "^$MAC;" /etc/xlinux/firewall_known_macs 2>/dev/null | cut -d\; -f4- | tail -1)
    echo "$MAC;$D;$TRUSTED_NET;$name" >/tmp/firewall_known_macs.tmp
    grep -v "^$MAC;" /etc/xlinux/firewall_known_macs >>/tmp/firewall_known_macs.tmp 2>/dev/null
    cat /tmp/firewall_known_macs.tmp >/etc/xlinux/firewall_known_macs 2>/dev/null
    rm /tmp/firewall_known_macs.tmp 2>/dev/null
    #sed -i "/^$MAC;/d" /etc/xlinux/firewall_known_macs
    #sed -i "1s/^/$MAC;$D;$TRUSTED_NET;$name\n/" /etc/xlinux/firewall_known_macs
    
    #echo "$MAC;$D;$TRUSTED_NET;" >/tmp/firewallmac      
    #grep -v "^$MAC;" /etc/xlinux/firewall_known_macs >>/tmp/firewallmac 2>/dev/null
    #cat /tmp/firewallmac >/etc/xlinux/firewall_known_macs
  fi
#fi

exit 0;
