Subversion Repositories ALCASAR

Rev

Rev 2964 | 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 3088 2022-11-06 22:55: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:
3088 rexy 46
	local-data: "$INT_DNS_HOST.$INT_DNS_DOMAIN IN A $INT_DNS_IP"
2831 rexy 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
3088 rexy 55
			echo -e "\tlocal-data: \"$domain.$INT_DNS_DOMAIN IN 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
3088 rexy 59
	if [ $INT_DNS_DOMAIN == "localdomain" ]
60
	then
61
		echo -e "\tlocal-zone: \"$INT_DNS_DOMAIN\" static" >> $LOCAL_DNS_FILE  # Don't forward this local zone
62
	fi
2688 lucas.echa 63
}
64
 
1607 franck 65
case $args in
66
	-\? | -h | --h)
67
		echo "$usage"
68
		exit 0
69
		;;
2559 rexy 70
	--add|-add) # add a local host resolution
71
		if [ $nb_args -ne 3 ]
72
		then
73
			echo "$usage"
74
			exit 1
75
		else
2688 lucas.echa 76
			# removing if already exists
2838 rexy 77
			$SED "/^$2[ \t]*$3/d" $LOCAL_HOSTNAME_FILE
2688 lucas.echa 78
			# adding to the hosts file
2559 rexy 79
			echo -e "$2\t$3" >> $LOCAL_HOSTNAME_FILE
2688 lucas.echa 80
			hosts_to_unbound
2559 rexy 81
			restart_dns
82
		fi
83
		;;
84
	--del|-del) # remove a local host resolution
85
		if [ $nb_args -ne 3 ]
86
		then
87
			echo "$usage"
88
			exit 1
89
		else
2838 rexy 90
			$SED "/^$2[ \t]*$3/d" $LOCAL_HOSTNAME_FILE
2688 lucas.echa 91
			hosts_to_unbound
2559 rexy 92
			restart_dns
93
		fi
94
		;;
2688 lucas.echa 95
	--reload|-reload)
96
			hosts_to_unbound
97
			restart_dns
98
		;;
2825 rexy 99
	--hosts_to_unbound|-hosts_to_unbound)
100
			hosts_to_unbound
101
		;;
1607 franck 102
	--off|-off) # disable DNS redirector
2688 lucas.echa 103
		#$SED "s?^#filterwin2k.*?filterwin2k?g" $DNSMASQ_CONF_FILE
104
		rm -f $LOCAL_DOMAIN_CONF_FILE
1607 franck 105
		$SED "s?^INT_DNS_ACTIVE.*?INT_DNS_ACTIVE=off?g" $ALCASAR_CONF_FILE
2559 rexy 106
		restart_dns
2688 lucas.echa 107
		/usr/local/bin/alcasar-iptables.sh
1607 franck 108
		;;
2947 rexy 109
	--off-without-restart|-off-without-restart) # disable DNS redirector
110
		#$SED "s?^#filterwin2k.*?filterwin2k?g" $DNSMASQ_CONF_FILE
111
		rm -f $LOCAL_DOMAIN_CONF_FILE
112
		$SED "s?^INT_DNS_ACTIVE.*?INT_DNS_ACTIVE=off?g" $ALCASAR_CONF_FILE
113
		/usr/local/bin/alcasar-iptables.sh
114
		;;
2688 lucas.echa 115
	--on|-on) # enable DNS redirector
116
		#$SED "s?^filterwin2k.*?#filterwin2k?g" $DNSMASQ_CONF_FILE
117
		cat > $LOCAL_DOMAIN_CONF_FILE << EOF
118
server:
119
    local-zone: "$INT_DNS_DOMAIN." transparent
120
forward-zone:
121
	name: "$INT_DNS_DOMAIN."
122
	forward-addr: $INT_DNS_IP
123
EOF
1607 franck 124
		$SED "s?^INT_DNS_ACTIVE.*?INT_DNS_ACTIVE=on?g" $ALCASAR_CONF_FILE
2964 rexy 125
		restart_dns
2688 lucas.echa 126
		/usr/local/bin/alcasar-iptables.sh
1607 franck 127
		;;
2947 rexy 128
	--on-without-restart|-on-without-restart) # enable DNS redirector
129
		#$SED "s?^filterwin2k.*?#filterwin2k?g" $DNSMASQ_CONF_FILE
130
		cat > $LOCAL_DOMAIN_CONF_FILE << EOF
131
server:
132
    local-zone: "$INT_DNS_DOMAIN." transparent
133
forward-zone:
134
	name: "$INT_DNS_DOMAIN."
135
	forward-addr: $INT_DNS_IP
136
EOF
137
		$SED "s?^INT_DNS_ACTIVE.*?INT_DNS_ACTIVE=on?g" $ALCASAR_CONF_FILE
138
		/usr/local/bin/alcasar-iptables.sh
139
		;;
1607 franck 140
	*)
2688 lucas.echa 141
		echo "Argument inconnu : $1";
1607 franck 142
		echo "$usage"
143
		exit 1
144
		;;
145
esac