]>
Commit | Line | Data |
---|---|---|
cccb3a4b MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
b4b2fa50 MT |
4 | # IPFire.org - A linux based firewall # |
5 | # Copyright (C) 2012 IPFire Network Development Team # | |
cccb3a4b | 6 | # # |
b4b2fa50 MT |
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. # | |
cccb3a4b | 11 | # # |
b4b2fa50 MT |
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. # | |
cccb3a4b | 16 | # # |
b4b2fa50 MT |
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/>. # | |
cccb3a4b MT |
19 | # # |
20 | ############################################################################### | |
21 | ||
acc9efd5 | 22 | # Set this to true if localhost should be added as the first DNS server. |
b4b2fa50 | 23 | DNS_USE_LOCAL_RESOLVER=true |
e9df08ad | 24 | NETWORK_SETTINGS_FILE_PARAMS="${NETWORK_SETTINGS_FILE_PARAMS} DNS_USE_LOCAL_RESOLVER" |
acc9efd5 | 25 | |
b4b2fa50 MT |
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. | |
28 | DNS_RANDOMIZE=false | |
e9df08ad | 29 | NETWORK_SETTINGS_FILE_PARAMS="${NETWORK_SETTINGS_FILE_PARAMS} DNS_RANDOMIZE" |
acc9efd5 | 30 | |
805da540 | 31 | DNS_SEARCH_DOMAINS="" |
e9df08ad | 32 | NETWORK_SETTINGS_FILE_PARAMS="${NETWORK_SETTINGS_FILE_PARAMS} DNS_SEARCH_DOMAINS" |
805da540 | 33 | |
b4b2fa50 | 34 | # Set this option to true if the DNS servers should be advertised by |
6f923dac MT |
35 | # radvd. |
36 | DNS_ADVERTISE_SERVERS="true" | |
37 | ||
acc9efd5 | 38 | DNS_SERVER_CONFIG_FILE="${NETWORK_CONFIG_DIR}/dns-servers" |
e5651e17 MT |
39 | NETWORK_CONFIG_DNS_SERVER_PARAMS="server priority" |
40 | ||
41 | # Set the default priority for DNS servers. | |
42 | DNS_SERVER_DEFAULT_PRIORITY="100" | |
43 | DNS_SERVER_DYNAMIC_PRIORITY="10" | |
acc9efd5 MT |
44 | |
45 | # Path to the configuration file of the DNS resolver. | |
46 | RESOLV_CONF="/etc/resolv.conf" | |
47 | ||
1c6a4e30 | 48 | dns_get_hostname() { |
cccb3a4b MT |
49 | local address=${1} |
50 | assert isset address | |
51 | ||
acc9efd5 MT |
52 | ( |
53 | unset HOSTNAME | |
54 | eval $(ipcalc -h ${address} 2>/dev/null) | |
55 | echo "${HOSTNAME}" | |
56 | ) | |
57 | } | |
58 | ||
1c6a4e30 | 59 | dns_server_list() { |
e5651e17 | 60 | [ -r "${DNS_SERVER_CONFIG_FILE}" ] || return ${EXIT_OK} |
acc9efd5 | 61 | |
e5651e17 MT |
62 | local line |
63 | local ${NETWORK_CONFIG_DNS_SERVER_PARAMS} | |
8d1d2745 | 64 | while read line; do |
e5651e17 MT |
65 | dns_server_parse_line ${line} || continue |
66 | ||
8d1d2745 | 67 | print "${server}" |
e5651e17 MT |
68 | done < ${DNS_SERVER_CONFIG_FILE} |
69 | ||
70 | return ${EXIT_OK} | |
acc9efd5 MT |
71 | } |
72 | ||
1c6a4e30 | 73 | dns_server_list_sorted() { |
acc9efd5 MT |
74 | [ -r "${DNS_SERVER_CONFIG_FILE}" ] || return ${EXIT_OK} |
75 | ||
e5651e17 MT |
76 | local servers=$( |
77 | # First get all servers from the configuration file. | |
78 | while read -r line; do | |
79 | dns_server_parse_line ${line} || continue | |
acc9efd5 | 80 | |
e5651e17 MT |
81 | print "%d %s" "${priority}" "${server}" |
82 | done < ${DNS_SERVER_CONFIG_FILE} | |
83 | ||
84 | # Then get all dynamically assigned DNS servers. | |
85 | dns_server_get_zone_name_servers | |
86 | ) | |
87 | ||
88 | # Nothing to do if we have got no DNS servers. | |
89 | isset servers || return ${EXIT_OK} | |
90 | ||
91 | # Sort everything we have got. | |
92 | servers=$(sort -g -k1 <<< "${servers}") | |
93 | ||
94 | # Remove the priority bit. | |
95 | local server server_list | |
96 | while read -r priority server; do | |
97 | list_append server_list "${server}" | |
98 | done <<< "${servers}" | |
99 | ||
100 | # Return the list but remove duplicate entries, keeping | |
101 | # the first and removing all others. | |
102 | list_unique ${server_list} | |
103 | ||
104 | return ${EXIT_OK} | |
6f923dac MT |
105 | } |
106 | ||
1c6a4e30 | 107 | dns_server_show() { |
8d1d2745 MT |
108 | [ -r "${DNS_SERVER_CONFIG_FILE}" ] || return ${EXIT_OK} |
109 | ||
110 | local line | |
111 | local ${NETWORK_CONFIG_DNS_SERVER_PARAMS} | |
112 | ||
113 | local format="%-20s %-8s" | |
114 | print "${format}" "SERVER" "PRIORITY" | |
115 | ||
116 | while read -r line; do | |
117 | dns_server_parse_line ${line} || continue | |
118 | ||
119 | print "${format}" "${server}" "${priority}" | |
120 | done < ${DNS_SERVER_CONFIG_FILE} | |
121 | ||
122 | return ${EXIT_OK} | |
123 | } | |
124 | ||
1c6a4e30 | 125 | dns_server_add() { |
acc9efd5 MT |
126 | local server=${1} |
127 | assert isset server | |
128 | ||
129 | local priority=${2} | |
130 | if ! isset priority; then | |
e5651e17 | 131 | priority="${DNS_SERVER_DEFAULT_PRIORITY}" |
acc9efd5 MT |
132 | fi |
133 | assert isinteger priority | |
134 | ||
e5651e17 MT |
135 | # Add a new line to the configuration file. |
136 | print "server=\"%s\" priority=\"%d\"" "${server}" "${priority}" \ | |
137 | >> ${DNS_SERVER_CONFIG_FILE} | |
acc9efd5 | 138 | |
e5651e17 MT |
139 | return ${EXIT_OK} |
140 | } | |
141 | ||
1c6a4e30 | 142 | dns_server_exists() { |
e5651e17 MT |
143 | local entry=${1} |
144 | assert isset entry | |
145 | ||
146 | [ -r "${DNS_SERVER_CONFIG_FILE}" ] || return ${EXIT_FALSE} | |
147 | ||
148 | local line ${NETWORK_CONFIG_DNS_SERVER_PARAMS} | |
149 | while read -r line; do | |
150 | dns_server_parse_line ${line} || continue | |
151 | ||
152 | [ "${entry}" = "${server}" ] && return ${EXIT_TRUE} | |
153 | done < ${DNS_SERVER_CONFIG_FILE} | |
154 | ||
155 | return ${EXIT_FALSE} | |
acc9efd5 MT |
156 | } |
157 | ||
1c6a4e30 | 158 | dns_server_remove() { |
e5651e17 MT |
159 | local entry=${1} |
160 | assert isset entry | |
acc9efd5 | 161 | |
e5651e17 MT |
162 | # Check if the DNS server configuration file exists. |
163 | [ -r "${DNS_SERVER_CONFIG_FILE}" ] || return ${EXIT_ERROR} | |
acc9efd5 | 164 | |
e5651e17 MT |
165 | # Create a tempfile. |
166 | local tempfile=$(mktemp) | |
167 | ||
168 | local line ${NETWORK_CONFIG_DNS_SERVER_PARAMS} | |
169 | while read -r line; do | |
170 | dns_server_parse_line ${line} || continue | |
171 | ||
172 | # Skip the line with the server we are searching for. | |
b4b2fa50 | 173 | [ "${entry}" = "${server}" ] && continue |
e5651e17 MT |
174 | |
175 | # Re-add the old line. | |
176 | print "${line}" | |
177 | done < ${DNS_SERVER_CONFIG_FILE} > ${tempfile} | |
178 | ||
179 | # Overwrite the old content without the entry that has just been removed. | |
180 | fread "${tempfile}" > ${DNS_SERVER_CONFIG_FILE} | |
181 | ||
182 | # Remove the temporary file. | |
183 | rm -f ${tempfile} | |
184 | ||
185 | return ${EXIT_OK} | |
acc9efd5 MT |
186 | } |
187 | ||
1c6a4e30 | 188 | dns_server_flush() { |
acc9efd5 MT |
189 | : > ${DNS_SERVER_CONFIG_FILE} |
190 | } | |
191 | ||
1c6a4e30 | 192 | dns_server_parse_line() { |
e5651e17 MT |
193 | local arg |
194 | ||
195 | for arg in ${NETWORK_CONFIG_DNS_SERVER_PARAMS}; do | |
196 | assign "${arg}" "" | |
197 | done | |
198 | ||
199 | while read -r arg; do | |
200 | case "${arg}" in | |
201 | server=*) | |
202 | server=$(cli_get_val ${arg}) | |
203 | ;; | |
204 | priority=*) | |
205 | priority=$(cli_get_val ${arg}) | |
206 | ;; | |
207 | esac | |
208 | done <<< "$(args $@)" | |
209 | ||
210 | # The server address must be set. | |
211 | isset server || return ${EXIT_ERROR} | |
212 | ||
213 | # The server address must also be a valid IP address. | |
214 | ip_is_valid ${server} || return ${EXIT_ERROR} | |
215 | ||
216 | # If priority is set, it must be an integer number. | |
217 | if isset priority; then | |
218 | isinteger priority || return ${EXIT_ERROR} | |
219 | ||
220 | # Otherwise assign the default priority. | |
221 | else | |
222 | priority=${DNS_SERVER_DEFAULT_PRIORITY} | |
223 | fi | |
224 | ||
225 | return ${EXIT_OK} | |
226 | } | |
227 | ||
bf98f6fc SS |
228 | # This function should be called after any configuration |
229 | # changes have been made to the DNS servers. | |
230 | dns_server_update() { | |
231 | # Regenerate /etc/resolv.conf | |
232 | dns_generate_resolvconf | |
233 | ||
234 | # Restart radvd which propagates IPv6 DNS servers | |
235 | radvd_update | |
236 | } | |
237 | ||
a469c542 MT |
238 | # Update resolv.conf(5) when initializing the network. |
239 | init_register dns_generate_resolvconf | |
240 | ||
1c6a4e30 | 241 | dns_generate_resolvconf() { |
acc9efd5 MT |
242 | local file=${RESOLV_CONF} |
243 | ||
244 | log INFO "Updating resolver configuration..." | |
245 | ||
246 | config_header "resolver configutation file" > ${file} | |
247 | ||
248 | if enabled DNS_RANDOMIZE; then | |
249 | print "option rotate\n" >> ${file} | |
250 | fi | |
251 | ||
805da540 | 252 | # Write search domains to file. |
e5651e17 | 253 | print "# Search domains" >> ${file} |
b1d1b5ce MT |
254 | |
255 | local domain | |
256 | for domain in $(dns_get_search_domains); do | |
257 | print "search ${domain}" | |
a9ebc53b | 258 | done >> ${file} |
acc9efd5 | 259 | |
e5651e17 MT |
260 | print "\n# Nameservers" >> ${file} |
261 | ||
acc9efd5 MT |
262 | # Add the local resolver as the first DNS server if enabled. |
263 | if enabled DNS_USE_LOCAL_RESOLVER; then | |
264 | print "nameserver ::1" >> ${file} | |
265 | fi | |
266 | ||
e5651e17 MT |
267 | # Dump all DNS servers. |
268 | for server in $(dns_server_list_sorted); do | |
acc9efd5 MT |
269 | print "nameserver ${server}" |
270 | done >> ${file} | |
cccb3a4b | 271 | } |
a9ebc53b | 272 | |
1c6a4e30 | 273 | dns_get_search_domains() { |
b1d1b5ce MT |
274 | # Add search domains. |
275 | local search_domains="$(unquote ${DNS_SEARCH_DOMAINS})" | |
276 | ||
277 | # Get search domains from DHCP clients, etc. | |
278 | local domain proto zone | |
279 | ||
280 | for zone in $(zones_get_all); do | |
281 | for proto in ${IP_SUPPORTED_PROTOCOLS}; do | |
c041b631 | 282 | domain="$(db_get "${zone}/${proto}/domain-name")" |
b1d1b5ce MT |
283 | isset domain || continue |
284 | ||
285 | list_append search_domains "${domainname}" | |
286 | done | |
287 | done | |
288 | ||
b6e68799 MT |
289 | # Add our own domain |
290 | list_append search_domains "$(config_domainname)" | |
291 | ||
b1d1b5ce MT |
292 | # Sort out duplicate entries. |
293 | list_unique ${search_domains} | |
294 | } | |
295 | ||
1c6a4e30 | 296 | dns_server_get_zone_name_servers() { |
e5651e17 MT |
297 | local priority proto server servers zone |
298 | ||
a9ebc53b | 299 | for zone in $(zones_get_all); do |
a9ebc53b | 300 | for proto in ${IP_SUPPORTED_PROTOCOLS}; do |
c041b631 | 301 | priority="$(db_get "${zone}/${proto}/domain-name-servers-priority")" |
e5651e17 | 302 | isset priority || priority="${DNS_SERVER_DYNAMIC_PRIORITY}" |
a9ebc53b | 303 | |
c041b631 | 304 | servers="$(db_get "${zone}/${proto}/domain-name-servers")" |
a9ebc53b | 305 | for server in ${servers}; do |
e5651e17 | 306 | print "${priority} ${server}" |
a9ebc53b KB |
307 | done |
308 | done | |
309 | done | |
310 | } |