2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
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. #
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. #
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/>. #
20 ###############################################################################
22 # Set this to true if localhost should be added as the first DNS server.
23 DNS_USE_LOCAL_RESOLVER
=true
24 NETWORK_SETTINGS_FILE_PARAMS
="${NETWORK_SETTINGS_FILE_PARAMS} DNS_USE_LOCAL_RESOLVER"
26 # Set this option to true if the DNS servers should be queried in a random order.
27 # This is useful to load balance between multiple servers.
29 NETWORK_SETTINGS_FILE_PARAMS
="${NETWORK_SETTINGS_FILE_PARAMS} DNS_RANDOMIZE"
32 NETWORK_SETTINGS_FILE_PARAMS
="${NETWORK_SETTINGS_FILE_PARAMS} DNS_SEARCH_DOMAINS"
34 # Set this option to true if the DNS servers should be advertised by
36 DNS_ADVERTISE_SERVERS
="true"
38 DNS_SERVER_CONFIG_FILE
="${NETWORK_CONFIG_DIR}/dns-servers"
39 NETWORK_CONFIG_DNS_SERVER_PARAMS
="server priority"
41 # Set the default priority for DNS servers.
42 DNS_SERVER_DEFAULT_PRIORITY
="100"
43 DNS_SERVER_DYNAMIC_PRIORITY
="10"
45 # Path to the configuration file of the DNS resolver.
46 RESOLV_CONF
="/etc/resolv.conf"
54 eval $
(ipcalc
-h ${address} 2>/dev
/null
)
60 [ -r "${DNS_SERVER_CONFIG_FILE}" ] ||
return ${EXIT_OK}
63 local ${NETWORK_CONFIG_DNS_SERVER_PARAMS}
65 dns_server_parse_line
${line} ||
continue
68 done < ${DNS_SERVER_CONFIG_FILE}
73 dns_server_list_sorted
() {
74 [ -r "${DNS_SERVER_CONFIG_FILE}" ] ||
return ${EXIT_OK}
77 # First get all servers from the configuration file.
78 while read -r line
; do
79 dns_server_parse_line
${line} ||
continue
81 print
"%d %s" "${priority}" "${server}"
82 done < ${DNS_SERVER_CONFIG_FILE}
84 # Then get all dynamically assigned DNS servers.
85 dns_server_get_zone_name_servers
88 # Nothing to do if we have got no DNS servers.
89 isset servers ||
return ${EXIT_OK}
91 # Sort everything we have got.
92 servers
=$
(sort -g -k1 <<< "${servers}")
94 # Remove the priority bit.
95 local server server_list
96 while read -r priority server
; do
97 list_append server_list
"${server}"
100 # Return the list but remove duplicate entries, keeping
101 # the first and removing all others.
102 list_unique
${server_list}
108 [ -r "${DNS_SERVER_CONFIG_FILE}" ] ||
return ${EXIT_OK}
111 local ${NETWORK_CONFIG_DNS_SERVER_PARAMS}
113 local format
="%-20s %-8s"
114 print
"${format}" "SERVER" "PRIORITY"
116 while read -r line
; do
117 dns_server_parse_line
${line} ||
continue
119 print
"${format}" "${server}" "${priority}"
120 done < ${DNS_SERVER_CONFIG_FILE}
130 if ! isset priority
; then
131 priority
="${DNS_SERVER_DEFAULT_PRIORITY}"
133 assert isinteger priority
135 # Add a new line to the configuration file.
136 print
"server=\"%s\" priority=\"%d\"" "${server}" "${priority}" \
137 >> ${DNS_SERVER_CONFIG_FILE}
142 dns_server_exists
() {
146 [ -r "${DNS_SERVER_CONFIG_FILE}" ] ||
return ${EXIT_FALSE}
148 local line
${NETWORK_CONFIG_DNS_SERVER_PARAMS}
149 while read -r line
; do
150 dns_server_parse_line
${line} ||
continue
152 [ "${entry}" = "${server}" ] && return ${EXIT_TRUE}
153 done < ${DNS_SERVER_CONFIG_FILE}
158 dns_server_remove() {
162 # Check if the DNS server configuration file exists.
163 [ -r "${DNS_SERVER_CONFIG_FILE}" ] || return ${EXIT_ERROR}
166 local tempfile=$(mktemp)
168 local line ${NETWORK_CONFIG_DNS_SERVER_PARAMS}
169 while read -r line; do
170 dns_server_parse_line ${line} || continue
172 # Skip the line with the server we are searching for.
173 [ "${entry}" = "${server}" ] && continue
175 # Re-add the old line.
177 done < ${DNS_SERVER_CONFIG_FILE} > ${tempfile}
179 # Overwrite the old content without the entry that has just been removed.
180 fread "${tempfile}" > ${DNS_SERVER_CONFIG_FILE}
182 # Remove the temporary file.
189 : > ${DNS_SERVER_CONFIG_FILE}
191 # Re-generate resolv.conf
192 dns_generate_resolvconf
195 dns_server_parse_line() {
198 for arg in ${NETWORK_CONFIG_DNS_SERVER_PARAMS}; do
202 while read -r arg; do
205 server=$(cli_get_val "${arg}")
208 priority=$(cli_get_val "${arg}")
211 done <<< "$
(args
"$@")"
213 # The server address must be set.
214 isset server || return ${EXIT_ERROR}
216 # The server address must also be a valid IP address.
217 ip_is_valid ${server} || return ${EXIT_ERROR}
219 # If priority is set, it must be an integer number.
220 if isset priority; then
221 isinteger priority || return ${EXIT_ERROR}
223 # Otherwise assign the default priority.
225 priority=${DNS_SERVER_DEFAULT_PRIORITY}
231 # This function should be called after any configuration
232 # changes have been made to the DNS servers.
233 dns_server_update() {
234 # Regenerate /etc/resolv.conf
235 dns_generate_resolvconf
237 # Restart radvd which propagates IPv6 DNS servers
241 dns_generate_resolvconf() {
242 local file=${RESOLV_CONF}
244 log INFO "Updating resolver configuration...
"
246 config_header "resolver configutation
file" > ${file}
248 # Always enable EDNS0
249 print "option edns0
\n" >> "${file}"
251 if enabled DNS_RANDOMIZE; then
252 print "option rotate
\n" >> ${file}
255 # Write search domains to file.
256 print "# Search domains" >> ${file}
259 for domain
in $
(dns_get_search_domains
); do
260 print
"search ${domain}"
263 print
"\n# Nameservers" >> ${file}
265 # Add the local resolver as the first DNS server if enabled.
266 if enabled DNS_USE_LOCAL_RESOLVER
; then
267 print
"nameserver ::1" >> ${file}
270 # Dump all DNS servers.
271 for server
in $
(dns_server_list_sorted
); do
272 print
"nameserver ${server}"
276 dns_get_search_domains
() {
277 # Add search domains.
278 local search_domains
="$(unquote ${DNS_SEARCH_DOMAINS})"
280 # Get search domains from DHCP clients, etc.
281 local domain proto zone
283 for zone
in $
(zones_get_all
); do
284 for proto
in ${IP_SUPPORTED_PROTOCOLS}; do
285 domain
="$(db_get "${zone}/${proto}/domain-name
")"
286 isset domain ||
continue
288 list_append search_domains
"${domainname}"
293 list_append search_domains
"$(config_domainname)"
295 # Sort out duplicate entries.
296 list_unique
${search_domains}
299 dns_server_get_zone_name_servers
() {
300 local priority proto server servers zone
302 for zone
in $
(zones_get_all
); do
303 for proto
in ${IP_SUPPORTED_PROTOCOLS}; do
304 priority
="$(db_get "${zone}/${proto}/domain-name-servers-priority
")"
305 isset priority || priority
="${DNS_SERVER_DYNAMIC_PRIORITY}"
307 servers
="$(db_get "${zone}/${proto}/domain-name-servers
")"
308 for server
in ${servers}; do
309 print
"${priority} ${server}"