#!/bin/bash
# Switch DNS forwarders in relation if vpn is active or not
# Version 0.2
# (c) 2019-2022 Stefan Schäfer / invis-server.org / stefan@invis-server.org

# License: GPLv3
# Questions: info@invis-server.org

# Dieses Script führt alle zur Installation eines invis Servers AD notwendigen 
# Konfigurationsschritte aus.
# Das Script subsine nutzt /var/lib/sine als Arbeitsverzeichnis und /usr/share/sine
# als Ressourcen-Verzeichnis.

# This scrips leads you through all the invis-server setup scripts.
# subsine uses /var/lib/sine as its working-  and /usr/share/sine as it's ressource-
# directory.

# Dieses Programm ist freie Software. Sie können es unter den Bedingungen der 
# GNU General Public License, wie von der Free Software Foundation veröffentlicht,
# weitergeben und/oder modifizieren, entweder gemäß Version 3 der Lizenz oder
# (nach Ihrer Option) jeder späteren Version.

# Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen
# von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite 
# Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. 
# Details finden Sie in der GNU General Public License.

# Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem 
# Programm erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>. 

# 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 <http://www.gnu.org/licenses/>.

# Konfigurationsdaten
conffile="/etc/invis/invis-sub.conf"

# Funktionen
# Werte aus Konfigurationsdatendatei extrahieren
# $1 = Konfigurationsdatei, $2 = Parameter, $3 Wert (Feld)
getconfdata() {
    cat $1 |grep "$2:" | cut -d ":" -f $3
}

# Empfaenger der Warnmails
invisip=`getconfdata $conffile "invisIP" "2"`
ping -c3 $invisip > /dev/null 2>&1
ok=$?

# Switch
if [[ `ip a |grep "vpn:"` && $ok == 0 ]]; then
    if [[ `ls -l /etc/named.d/named.forwarder |grep direct` ]]; then
	ln -fs /etc/named.d/vpn.forwarders /etc/named.d/named.forwarder
	systemctl reload named.service
	rndc flush
    fi
else
    if [[ `ls -l /etc/named.d/named.forwarder |grep vpn` ]]; then
	ln -fs /etc/named.d/direct.forwarders /etc/named.d/named.forwarder
	systemctl reload named.service
	rndc flush
    fi
fi