Subversion Repositories ALCASAR

Rev

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