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 # Set this option to true if the DNS servers should be advertised by
33 DNS_ADVERTISE_SERVERS
="true"
35 DNS_SERVER_CONFIG_FILE
="${NETWORK_CONFIG_DIR}/dns-servers"
37 # Path to the configuration file of the DNS resolver.
38 RESOLV_CONF
="/etc/resolv.conf"
40 function dns_get_hostname
() {
46 eval $
(ipcalc
-h ${address} 2>/dev
/null
)
51 function __dns_server_println
() {
55 print
"%-20s %s" "${server}" "${priority}"
58 function __dns_server_sort
() {
62 function dns_server_list
() {
63 [ -r "${DNS_SERVER_CONFIG_FILE}" ] ||
return ${EXIT_OK}
66 while read server priority
; do
67 if [ -n "${server}" ] && [ -n "${priority}" ]; then
68 __dns_server_println
"${server}" "${priority}"
70 done < ${DNS_SERVER_CONFIG_FILE} | __dns_server_sort
73 function dns_server_list_no_priority
() {
75 dns_server_list |
while read server priority
; do
80 function dns_server_add
() {
85 if ! isset priority
; then
88 assert isinteger priority
92 __dns_server_println
"${server}" "${priority}"
93 ) | __dns_server_sort
> ${DNS_SERVER_CONFIG_FILE}.new
95 mv ${DNS_SERVER_CONFIG_FILE}{.new
,}
98 function dns_server_remove
() {
103 local entries
=$
(dns_server_list
)
105 while read entry priority
; do
106 [ "${entry}" = "${server}" ] && continue
107 __dns_server_println
"${server}" "${priority}"
108 done <<< ${entries} | __dns_server_sort
> ${DNS_SERVER_CONFIG_FILE}
111 function dns_server_flush
() {
112 : > ${DNS_SERVER_CONFIG_FILE}
115 # Update resolv.conf(5) when initializing the network.
116 init_register dns_generate_resolvconf
118 function dns_generate_resolvconf
() {
119 local file=${RESOLV_CONF}
121 log INFO
"Updating resolver configuration..."
123 config_header
"resolver configutation file" > ${file}
125 if enabled DNS_RANDOMIZE
; then
126 print
"option rotate\n" >> ${file}
129 # XXX Add search domain.
133 for zone
in $
(zones_get_all
); do
134 for proto
in ${IP_SUPPORTED_PROTOCOLS}; do
135 domainname
=$
(routing_db_get
${zone} ${proto} domain-name
)
136 if [ -n "${domainname}" ]; then
137 print
"search ${domainname}"
142 # Add the local resolver as the first DNS server if enabled.
143 if enabled DNS_USE_LOCAL_RESOLVER
; then
144 print
"nameserver ::1" >> ${file}
147 # First pull in zone name servers.
149 for server
in $
(dns_get_zone_name_servers
); do
150 print
"nameserver ${server}"
153 # Dump all DNS servers (if any).
155 dns_server_list |
while read server priority
; do
156 print
"nameserver ${server}"
160 function dns_get_zone_name_servers
() {
163 for zone
in $
(zones_get_all
); do
165 for proto
in ${IP_SUPPORTED_PROTOCOLS}; do
166 servers
=$
(routing_db_get
${zone} ${proto} domain-name-servers
)
169 for server
in ${servers}; do