]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/scripts/convert-dns-settings
e9d4de86baac587a81b180f9165895b4cb70f25c
[people/pmueller/ipfire-2.x.git] / src / scripts / convert-dns-settings
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A Linux-based firewall #
5 # Copyright (C) 2020 IPFire Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 main() {
23 # Do not convert anything if we already have some servers set
24 if [ ! -s "/var/ipfire/dns/servers" ]; then
25 # Array to store all found DNS servers.
26 SERVERS=()
27
28 # Try to get the DNS servers from ethernet settings file.
29 local DNS1 DNS2
30 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
31
32 # Add the grabbed DNS servers to the servers array.
33 SERVERS+=($DNS1 $DNS2)
34
35 # Check if the ppp settings file is not empty.
36 if [ -s "/var/ipfire/ppp/settings" ]; then
37 # Loop though all profile files.
38 for file in /var/ipfire/ppp/settings*; do
39 local DNS1 DNS2
40 eval $(/usr/local/bin/readhash $file)
41
42 # Add the DNS servers to the array of SERVERS.
43 for var in DNS1 DNS2; do
44 local server="${!var}"
45
46 # Check if the current server is allready part
47 # of the array.
48 for element in "${SERVERS[@]}"; do
49 [[ $element == $server ]] && continue
50 done
51
52 SERVERS+=($server)
53 done
54
55 # Remove DNS1 and DNS2 settings from profile file.
56 sed -i "/^DNS[12]?=/d" $file
57 done
58
59 elif [ -s "/var/ipfire/dns/settings" ]; then
60 eval $(/usr/local/bin/readhash /var/ipfire/dns/settings)
61 fi
62
63 local server
64 local i=3
65 for server in "${SERVERS[@]}"; do
66 echo "${i},${server},,enabled,"
67 (( i++ ))
68 done > /var/ipfire/dns/servers
69
70 # Empty the old settings file
71 : > /var/ipfire/dns/settings
72
73 # Disable using ISP name servers when we already have some configured
74 if [ ${i} -gt 3 ]; then
75 echo "USE_ISP_NAMESERVERS=off" \
76 >> /var/ipfire/dns/settings
77 fi
78 fi
79
80 # Set correct ownership.
81 chown nobody:nobody /var/ipfire/dns/settings
82
83 # Convert old unbound settings file
84 if [ -e "/etc/sysconfig/unbound" ]; then
85 local USE_FORWARDERS
86 local ENABLE_SAFE_SEARCH
87 local FORCE_TCP
88
89 # Read settings
90 eval $(/usr/local/bin/readhash /etc/sysconfig/unbound)
91
92 # Safe Search
93 if [ "${ENABLE_SAFE_SEARCH}" = "on" ]; then
94 echo "ENABLE_SAFE_SEARCH=${ENABLE_SAFE_SEARCH}" \
95 >> /var/ipfire/dns/settings
96 fi
97
98 # Force TCP
99 if [ "${FORCE_TCP}" = "on" ]; then
100 echo "PROTO=TCP" >> /var/ipfire/dns/settings
101 fi
102
103 # Run in recursor mode
104 if [ "${USE_FORWARDERS}" = "0" ]; then
105 # Remove all servers
106 : > /var/ipfire/dns/servers
107 fi
108
109 rm -f "/etc/sysconfig/unbound"
110 fi
111
112 # Set correct ownership.
113 chown nobody:nobody /var/ipfire/dns/servers
114
115 # Make DHCP leases readable for nobody
116 chown 644 /etc/unbound/dhcp-leases.conf
117 }
118
119 main "$@" || exit $?