Subversion Repositories ALCASAR

Rev

Rev 2688 | Rev 2831 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2223 tom.houday 1
#!/bin/bash
2
# $Id: alcasar-dns-local.sh 2825 2020-05-31 17:01:33Z rexy $
1607 franck 3
 
2688 lucas.echa 4
# alcasar-dns-local.sh
1607 franck 5
# by Rexy - 3abtux
6
# This script is distributed under the Gnu General Public License (GPL)
7
 
8
# active ou desactive la redirection du service DNS sur le réseau de consultation
9
# enable or disable the redirector of internal DNS service on consultation LAN
10
 
11
SED="/bin/sed -i"
12
 
13
ALCASAR_CONF_FILE="/usr/local/etc/alcasar.conf"
2688 lucas.echa 14
LOCAL_DOMAIN_CONF_FILE="/etc/unbound/conf.d/common/local-forward/dns-redirector.conf"
2559 rexy 15
LOCAL_HOSTNAME_FILE="/etc/hosts"
2688 lucas.echa 16
LOCAL_DNS_FILE="/etc/unbound/conf.d/common/local-dns/global.conf"
1607 franck 17
 
18
# define DNS parameters (LAN side)
19
 
2688 lucas.echa 20
INT_DNS_DOMAIN=`grep INT_DNS_DOMAIN $ALCASAR_CONF_FILE|cut -d"=" -f2`		# Nom du domaine DNS interne
1607 franck 21
INT_DNS_IP=`grep INT_DNS_IP $ALCASAR_CONF_FILE|cut -d"=" -f2`				# Adresse du serveur DNS interne
2688 lucas.echa 22
INT_DNS_ACTIVE=`grep INT_DNS_ACTIVE $ALCASAR_CONF_FILE|cut -d"=" -f2`		# Activation de la redirection DNS interne
1607 franck 23
 
2688 lucas.echa 24
usage="Usage: alcasar-dns-local.sh {--on | -on} | {--off | -off} | {--add | -add} ip domain | {--del | -del} ip domain | {--reload | -reload}"
1607 franck 25
nb_args=$#
26
args=$1
27
if [ $nb_args -eq 0 ]
28
then
29
	echo "$usage"
30
	exit 1
31
fi
2688 lucas.echa 32
 
2559 rexy 33
function restart_dns(){
2688 lucas.echa 34
	for dns in unbound unbound-blacklist unbound-whitelist dnsmasq-whitelist unbound-blackhole
2559 rexy 35
	do
36
		systemctl restart $dns
37
	done
38
}
39
 
2688 lucas.echa 40
function hosts_to_unbound(){
41
	# Empty the local DNS file
42
	echo "server:" > $LOCAL_DNS_FILE
43
 
44
	while read -r line
45
	do
46
		ip_address=$(echo $line | awk '{ print $1 }')
47
		domain=$(echo $line | awk '{ print $2 }')
48
 
49
		if ! echo $line | grep -E -q "^([0-9\.\t ]+alcasar( |$)|127\.0\.0)"
50
		then
51
			echo -e "\tlocal-zone: \"$domain\" redirect" >> $LOCAL_DNS_FILE
52
			echo -e "\tlocal-data: \"$domain A $ip_address\"" >> $LOCAL_DNS_FILE
53
		fi
54
	done < $LOCAL_HOSTNAME_FILE
55
}
56
 
1607 franck 57
case $args in
58
	-\? | -h | --h)
59
		echo "$usage"
60
		exit 0
61
		;;
2559 rexy 62
	--add|-add) # add a local host resolution
63
		if [ $nb_args -ne 3 ]
64
		then
65
			echo "$usage"
66
			exit 1
67
		else
2688 lucas.echa 68
			# removing if already exists
69
			$SED "/^$2\t$3/d" $LOCAL_HOSTNAME_FILE
70
			# adding to the hosts file
2559 rexy 71
			echo -e "$2\t$3" >> $LOCAL_HOSTNAME_FILE
2688 lucas.echa 72
			hosts_to_unbound
2559 rexy 73
			restart_dns
74
		fi
75
		;;
76
	--del|-del) # remove a local host resolution
77
		if [ $nb_args -ne 3 ]
78
		then
79
			echo "$usage"
80
			exit 1
81
		else
2688 lucas.echa 82
			$SED "/^$2\t$3/d" $LOCAL_HOSTNAME_FILE
83
			hosts_to_unbound
2559 rexy 84
			restart_dns
85
		fi
86
		;;
2688 lucas.echa 87
	--reload|-reload)
88
			hosts_to_unbound
89
			restart_dns
90
		;;
2825 rexy 91
	--hosts_to_unbound|-hosts_to_unbound)
92
			hosts_to_unbound
93
		;;
1607 franck 94
	--off|-off) # disable DNS redirector
2688 lucas.echa 95
		#$SED "s?^#filterwin2k.*?filterwin2k?g" $DNSMASQ_CONF_FILE
96
		rm -f $LOCAL_DOMAIN_CONF_FILE
1607 franck 97
		$SED "s?^INT_DNS_ACTIVE.*?INT_DNS_ACTIVE=off?g" $ALCASAR_CONF_FILE
2559 rexy 98
		restart_dns
2688 lucas.echa 99
 
100
		# Reload firewall
101
		/usr/local/bin/alcasar-iptables.sh
1607 franck 102
		;;
2688 lucas.echa 103
	--on|-on) # enable DNS redirector
104
		#$SED "s?^filterwin2k.*?#filterwin2k?g" $DNSMASQ_CONF_FILE
105
		cat > $LOCAL_DOMAIN_CONF_FILE << EOF
106
server:
107
    local-zone: "$INT_DNS_DOMAIN." transparent
108
forward-zone:
109
	name: "$INT_DNS_DOMAIN."
110
	forward-addr: $INT_DNS_IP
111
EOF
1607 franck 112
		$SED "s?^INT_DNS_ACTIVE.*?INT_DNS_ACTIVE=on?g" $ALCASAR_CONF_FILE
2559 rexy 113
		restart_dns
2688 lucas.echa 114
		# Reload firewall
115
		/usr/local/bin/alcasar-iptables.sh
1607 franck 116
		;;
117
	*)
2688 lucas.echa 118
		echo "Argument inconnu : $1";
1607 franck 119
		echo "$usage"
120
		exit 1
121
		;;
122
esac