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_CONFIG_FILE_PARAMS
="${NETWORK_CONFIG_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_CONFIG_FILE_PARAMS
="${NETWORK_CONFIG_FILE_PARAMS} DNS_RANDOMIZE"
31 DNS_SERVER_CONFIG_FILE
="${NETWORK_CONFIG_DIR}/dns-servers"
33 # Path to the configuration file of the DNS resolver.
34 RESOLV_CONF
="/etc/resolv.conf"
36 function dns_get_hostname
() {
42 eval $
(ipcalc
-h ${address} 2>/dev
/null
)
47 function __dns_server_println
() {
51 print
"%-20s %s" "${server}" "${priority}"
54 function __dns_server_sort
() {
58 function dns_server_list
() {
59 [ -r "${DNS_SERVER_CONFIG_FILE}" ] ||
return ${EXIT_OK}
62 while read server priority
; do
63 if [ -n "${server}" ] && [ -n "${priority}" ]; then
64 __dns_server_println
"${server}" "${priority}"
66 done < ${DNS_SERVER_CONFIG_FILE} | __dns_server_sort
69 function dns_server_add
() {
74 if ! isset priority
; then
77 assert isinteger priority
81 __dns_server_println
"${server}" "${priority}"
82 ) | __dns_server_sort
> ${DNS_SERVER_CONFIG_FILE}.new
84 mv ${DNS_SERVER_CONFIG_FILE}{.new
,}
87 function dns_server_remove
() {
92 local entries
=$
(dns_server_list
)
94 while read entry priority
; do
95 [ "${entry}" = "${server}" ] && continue
96 __dns_server_println
"${server}" "${priority}"
97 done <<< ${entries} | __dns_server_sort
> ${DNS_SERVER_CONFIG_FILE}
100 function dns_server_flush
() {
101 : > ${DNS_SERVER_CONFIG_FILE}
104 # Update resolv.conf(5) when initializing the network.
105 init_register dns_generate_resolvconf
107 function dns_generate_resolvconf
() {
108 local file=${RESOLV_CONF}
110 log INFO
"Updating resolver configuration..."
112 config_header
"resolver configutation file" > ${file}
114 if enabled DNS_RANDOMIZE
; then
115 print
"option rotate\n" >> ${file}
118 # XXX Add search domain.
120 # Add the local resolver as the first DNS server if enabled.
121 if enabled DNS_USE_LOCAL_RESOLVER
; then
122 print
"nameserver ::1" >> ${file}
125 # Dump all DNS servers (if any).
126 local server priority
127 dns_server_list |
while read server priority
; do
128 print
"nameserver ${server}"