Subversion Repositories ALCASAR

Rev

Rev 2688 | Rev 2831 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

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