Subversion Repositories ALCASAR

Rev

Rev 3088 | 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 3099 2022-12-28 11:30:30Z 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(){
3099 rexy 36
	for dns in unbound unbound-blacklist unbound-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
		rm -f $LOCAL_DOMAIN_CONF_FILE
1607 franck 104
		$SED "s?^INT_DNS_ACTIVE.*?INT_DNS_ACTIVE=off?g" $ALCASAR_CONF_FILE
2559 rexy 105
		restart_dns
2688 lucas.echa 106
		/usr/local/bin/alcasar-iptables.sh
1607 franck 107
		;;
2947 rexy 108
	--off-without-restart|-off-without-restart) # disable DNS redirector
109
		rm -f $LOCAL_DOMAIN_CONF_FILE
110
		$SED "s?^INT_DNS_ACTIVE.*?INT_DNS_ACTIVE=off?g" $ALCASAR_CONF_FILE
111
		/usr/local/bin/alcasar-iptables.sh
112
		;;
2688 lucas.echa 113
	--on|-on) # enable DNS redirector
114
		cat > $LOCAL_DOMAIN_CONF_FILE << EOF
115
server:
116
    local-zone: "$INT_DNS_DOMAIN." transparent
117
forward-zone:
118
	name: "$INT_DNS_DOMAIN."
119
	forward-addr: $INT_DNS_IP
120
EOF
1607 franck 121
		$SED "s?^INT_DNS_ACTIVE.*?INT_DNS_ACTIVE=on?g" $ALCASAR_CONF_FILE
2964 rexy 122
		restart_dns
2688 lucas.echa 123
		/usr/local/bin/alcasar-iptables.sh
1607 franck 124
		;;
2947 rexy 125
	--on-without-restart|-on-without-restart) # enable DNS redirector
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