Subversion Repositories ALCASAR

Rev

Rev 2947 | Rev 3088 | 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 2964 2021-07-04 09:23:08Z 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"
1607 franck 16
 
17
# define DNS parameters (LAN side)
2831 rexy 18
INT_DNS_DOMAIN=`grep ^DOMAIN $ALCASAR_CONF_FILE|cut -d"=" -f2`
19
INT_DNS_HOST=`grep ^HOSTNAME $ALCASAR_CONF_FILE|cut -d"=" -f2`
2833 rexy 20
INT_DNS_IP_MASK=`grep ^PRIVATE_IP $ALCASAR_CONF_FILE|cut -d"=" -f2`
2831 rexy 21
INT_DNS_IP=`grep ^PRIVATE_IP $ALCASAR_CONF_FILE|cut -d"=" -f2|cut -d"/" -f1`
22
INTIF=`grep ^INTIF $ALCASAR_CONF_FILE|cut -d"=" -f2`
23
INT_DNS_ACTIVE=`grep INT_DNS_ACTIVE $ALCASAR_CONF_FILE|cut -d"=" -f2`
24
LOCAL_DNS_FILE="/etc/unbound/conf.d/common/local-dns/$INTIF.conf"
1607 franck 25
 
2688 lucas.echa 26
usage="Usage: alcasar-dns-local.sh {--on | -on} | {--off | -off} | {--add | -add} ip domain | {--del | -del} ip domain | {--reload | -reload}"
1607 franck 27
nb_args=$#
28
args=$1
29
if [ $nb_args -eq 0 ]
30
then
31
	echo "$usage"
32
	exit 1
33
fi
2688 lucas.echa 34
 
2559 rexy 35
function restart_dns(){
2688 lucas.echa 36
	for dns in unbound unbound-blacklist unbound-whitelist dnsmasq-whitelist unbound-blackhole
2559 rexy 37
	do
2947 rexy 38
		echo "Restarting $dns. Please wait..."
2559 rexy 39
		systemctl restart $dns
40
	done
41
}
42
 
2861 rexy 43
function hosts_to_unbound(){  # configure the unbound conf file with local host names resolution
2831 rexy 44
		cat << EOF > $LOCAL_DNS_FILE
45
server:
46
	local-data: "$INT_DNS_HOST.$INT_DNS_DOMAIN A $INT_DNS_IP"
47
	local-data-ptr: "$INT_DNS_IP $INT_DNS_HOST.$INT_DNS_DOMAIN"
48
EOF
2688 lucas.echa 49
	while read -r line
50
	do
51
		ip_address=$(echo $line | awk '{ print $1 }')
52
		domain=$(echo $line | awk '{ print $2 }')
53
		if ! echo $line | grep -E -q "^([0-9\.\t ]+alcasar( |$)|127\.0\.0)"
54
		then
2833 rexy 55
			echo -e "\tlocal-data: \"$domain.$INT_DNS_DOMAIN A $ip_address\"" >> $LOCAL_DNS_FILE
2831 rexy 56
			echo -e "\tlocal-data-ptr: \"$ip_address $domain.$INT_DNS_DOMAIN\"" >> $LOCAL_DNS_FILE
2688 lucas.echa 57
		fi
58
	done < $LOCAL_HOSTNAME_FILE
59
}
60
 
1607 franck 61
case $args in
62
	-\? | -h | --h)
63
		echo "$usage"
64
		exit 0
65
		;;
2559 rexy 66
	--add|-add) # add a local host resolution
67
		if [ $nb_args -ne 3 ]
68
		then
69
			echo "$usage"
70
			exit 1
71
		else
2688 lucas.echa 72
			# removing if already exists
2838 rexy 73
			$SED "/^$2[ \t]*$3/d" $LOCAL_HOSTNAME_FILE
2688 lucas.echa 74
			# adding to the hosts file
2559 rexy 75
			echo -e "$2\t$3" >> $LOCAL_HOSTNAME_FILE
2688 lucas.echa 76
			hosts_to_unbound
2559 rexy 77
			restart_dns
78
		fi
79
		;;
80
	--del|-del) # remove a local host resolution
81
		if [ $nb_args -ne 3 ]
82
		then
83
			echo "$usage"
84
			exit 1
85
		else
2838 rexy 86
			$SED "/^$2[ \t]*$3/d" $LOCAL_HOSTNAME_FILE
2688 lucas.echa 87
			hosts_to_unbound
2559 rexy 88
			restart_dns
89
		fi
90
		;;
2688 lucas.echa 91
	--reload|-reload)
92
			hosts_to_unbound
93
			restart_dns
94
		;;
2825 rexy 95
	--hosts_to_unbound|-hosts_to_unbound)
96
			hosts_to_unbound
97
		;;
1607 franck 98
	--off|-off) # disable DNS redirector
2688 lucas.echa 99
		#$SED "s?^#filterwin2k.*?filterwin2k?g" $DNSMASQ_CONF_FILE
100
		rm -f $LOCAL_DOMAIN_CONF_FILE
1607 franck 101
		$SED "s?^INT_DNS_ACTIVE.*?INT_DNS_ACTIVE=off?g" $ALCASAR_CONF_FILE
2559 rexy 102
		restart_dns
2688 lucas.echa 103
		/usr/local/bin/alcasar-iptables.sh
1607 franck 104
		;;
2947 rexy 105
	--off-without-restart|-off-without-restart) # disable DNS redirector
106
		#$SED "s?^#filterwin2k.*?filterwin2k?g" $DNSMASQ_CONF_FILE
107
		rm -f $LOCAL_DOMAIN_CONF_FILE
108
		$SED "s?^INT_DNS_ACTIVE.*?INT_DNS_ACTIVE=off?g" $ALCASAR_CONF_FILE
109
		/usr/local/bin/alcasar-iptables.sh
110
		;;
2688 lucas.echa 111
	--on|-on) # enable DNS redirector
112
		#$SED "s?^filterwin2k.*?#filterwin2k?g" $DNSMASQ_CONF_FILE
113
		cat > $LOCAL_DOMAIN_CONF_FILE << EOF
114
server:
115
    local-zone: "$INT_DNS_DOMAIN." transparent
116
forward-zone:
117
	name: "$INT_DNS_DOMAIN."
118
	forward-addr: $INT_DNS_IP
119
EOF
1607 franck 120
		$SED "s?^INT_DNS_ACTIVE.*?INT_DNS_ACTIVE=on?g" $ALCASAR_CONF_FILE
2964 rexy 121
		restart_dns
2688 lucas.echa 122
		/usr/local/bin/alcasar-iptables.sh
1607 franck 123
		;;
2947 rexy 124
	--on-without-restart|-on-without-restart) # enable DNS redirector
125
		#$SED "s?^filterwin2k.*?#filterwin2k?g" $DNSMASQ_CONF_FILE
126
		cat > $LOCAL_DOMAIN_CONF_FILE << EOF
127
server:
128
    local-zone: "$INT_DNS_DOMAIN." transparent
129
forward-zone:
130
	name: "$INT_DNS_DOMAIN."
131
	forward-addr: $INT_DNS_IP
132
EOF
133
		$SED "s?^INT_DNS_ACTIVE.*?INT_DNS_ACTIVE=on?g" $ALCASAR_CONF_FILE
134
		/usr/local/bin/alcasar-iptables.sh
135
		;;
1607 franck 136
	*)
2688 lucas.echa 137
		echo "Argument inconnu : $1";
1607 franck 138
		echo "$usage"
139
		exit 1
140
		;;
141
esac