]> git.ipfire.org Git - ipfire-2.x.git/blob - src/scripts/convert-dns-settings
suricata: Change midstream policy to "pass-flow"
[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 FILES=(
23 /var/ipfire/ethernet/settings
24 /var/ipfire/dns/settings
25 /var/ipfire/ppp/settings
26 /var/ipfire/ppp/settings-*
27 )
28
29 main() {
30 # Do not convert anything if we already have some servers set
31 if [ ! -s "/var/ipfire/dns/servers" ]; then
32 # Array to store all found DNS servers.
33 local SERVERS=()
34
35 # Loop through all PPP profiles
36 local file
37 for file in "${FILES[@]}"; do
38 if [ -s "${file}" ]; then
39 local DNS0 DNS1 DNS2
40 eval $(/usr/local/bin/readhash "${file}")
41
42 # Add the DNS servers to the array of SERVERS
43 local var
44 for var in DNS0 DNS1 DNS2; do
45 SERVERS+=( "${!var}" )
46 done
47 fi
48 done
49
50 local server
51 local i=3
52 for server in $(printf "%s\n" "${SERVERS[@]}" | sort -u); do
53 if [ -n "${server}" ]; then
54 echo "${i},${server},,enabled,"
55 (( i++ ))
56 fi
57 done > /var/ipfire/dns/servers
58
59 # Empty the old settings file
60 : > /var/ipfire/dns/settings
61
62 # Enable using ISP name servers when no servers are configured
63 if [ ${i} -eq 3 ]; then
64 echo "USE_ISP_NAMESERVERS=on" \
65 >> /var/ipfire/dns/settings
66 fi
67 fi
68
69 # Remove all old settings from files
70 local file
71 for file in "${FILES[@]}"; do
72 # Remove DNS, DNS0, DNS1 and DNS2
73 sed -Ei "/^DNS[012]?=/d" "${file}"
74 done
75
76 # Set correct ownership.
77 chown nobody:nobody /var/ipfire/dns/settings
78
79 # Convert old unbound settings file
80 if [ -e "/etc/sysconfig/unbound" ]; then
81 local USE_FORWARDERS
82 local ENABLE_SAFE_SEARCH
83 local FORCE_TCP
84
85 # Read settings
86 eval $(/usr/local/bin/readhash /etc/sysconfig/unbound)
87
88 # Safe Search
89 if [ "${ENABLE_SAFE_SEARCH}" = "on" ]; then
90 echo "ENABLE_SAFE_SEARCH=${ENABLE_SAFE_SEARCH}" \
91 >> /var/ipfire/dns/settings
92 fi
93
94 # Force TCP
95 if [ "${FORCE_TCP}" = "on" ]; then
96 echo "PROTO=TCP" >> /var/ipfire/dns/settings
97 fi
98
99 # Run in recursor mode
100 if [ "${USE_FORWARDERS}" = "0" ]; then
101 # Remove all servers
102 : > /var/ipfire/dns/servers
103 fi
104
105 rm -f "/etc/sysconfig/unbound"
106 fi
107
108 # Set correct ownership.
109 chown nobody:nobody /var/ipfire/dns/servers
110
111 # Make DHCP leases readable for nobody
112 chmod 644 /etc/unbound/dhcp-leases.conf
113 }
114
115 main "$@" || exit $?