]>
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 MT |
23 | DNS_USE_LOCAL_RESOLVER=true |
24 | NETWORK_CONFIG_FILE_PARAMS="${NETWORK_CONFIG_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 | |
acc9efd5 MT |
29 | NETWORK_CONFIG_FILE_PARAMS="${NETWORK_CONFIG_FILE_PARAMS} DNS_RANDOMIZE" |
30 | ||
805da540 MT |
31 | DNS_SEARCH_DOMAINS="" |
32 | NETWORK_CONFIG_FILE_PARAMS="${NETWORK_CONFIG_FILE_PARAMS} DNS_SEARCH_DOMAINS" | |
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 | ||
cccb3a4b MT |
48 | function dns_get_hostname() { |
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 | ||
e5651e17 MT |
59 | function dns_server_list() { |
60 | [ -r "${DNS_SERVER_CONFIG_FILE}" ] || return ${EXIT_OK} | |
acc9efd5 | 61 | |
e5651e17 MT |
62 | local line |
63 | local ${NETWORK_CONFIG_DNS_SERVER_PARAMS} | |
64 | ||
65 | local format="%-20s %-8s" | |
66 | print "${format}" "SERVER" "PRIORITY" | |
acc9efd5 | 67 | |
e5651e17 MT |
68 | while read -r line; do |
69 | dns_server_parse_line ${line} || continue | |
70 | ||
71 | print "${format}" "${server}" "${priority}" | |
72 | done < ${DNS_SERVER_CONFIG_FILE} | |
73 | ||
74 | return ${EXIT_OK} | |
acc9efd5 MT |
75 | } |
76 | ||
e5651e17 | 77 | function dns_server_list_sorted() { |
acc9efd5 MT |
78 | [ -r "${DNS_SERVER_CONFIG_FILE}" ] || return ${EXIT_OK} |
79 | ||
e5651e17 MT |
80 | local servers=$( |
81 | # First get all servers from the configuration file. | |
82 | while read -r line; do | |
83 | dns_server_parse_line ${line} || continue | |
acc9efd5 | 84 | |
e5651e17 MT |
85 | print "%d %s" "${priority}" "${server}" |
86 | done < ${DNS_SERVER_CONFIG_FILE} | |
87 | ||
88 | # Then get all dynamically assigned DNS servers. | |
89 | dns_server_get_zone_name_servers | |
90 | ) | |
91 | ||
92 | # Nothing to do if we have got no DNS servers. | |
93 | isset servers || return ${EXIT_OK} | |
94 | ||
95 | # Sort everything we have got. | |
96 | servers=$(sort -g -k1 <<< "${servers}") | |
97 | ||
98 | # Remove the priority bit. | |
99 | local server server_list | |
100 | while read -r priority server; do | |
101 | list_append server_list "${server}" | |
102 | done <<< "${servers}" | |
103 | ||
104 | # Return the list but remove duplicate entries, keeping | |
105 | # the first and removing all others. | |
106 | list_unique ${server_list} | |
107 | ||
108 | return ${EXIT_OK} | |
6f923dac MT |
109 | } |
110 | ||
acc9efd5 MT |
111 | function dns_server_add() { |
112 | local server=${1} | |
113 | assert isset server | |
114 | ||
115 | local priority=${2} | |
116 | if ! isset priority; then | |
e5651e17 | 117 | priority="${DNS_SERVER_DEFAULT_PRIORITY}" |
acc9efd5 MT |
118 | fi |
119 | assert isinteger priority | |
120 | ||
e5651e17 MT |
121 | # Add a new line to the configuration file. |
122 | print "server=\"%s\" priority=\"%d\"" "${server}" "${priority}" \ | |
123 | >> ${DNS_SERVER_CONFIG_FILE} | |
acc9efd5 | 124 | |
e5651e17 MT |
125 | return ${EXIT_OK} |
126 | } | |
127 | ||
128 | function dns_server_exists() { | |
129 | local entry=${1} | |
130 | assert isset entry | |
131 | ||
132 | [ -r "${DNS_SERVER_CONFIG_FILE}" ] || return ${EXIT_FALSE} | |
133 | ||
134 | local line ${NETWORK_CONFIG_DNS_SERVER_PARAMS} | |
135 | while read -r line; do | |
136 | dns_server_parse_line ${line} || continue | |
137 | ||
138 | [ "${entry}" = "${server}" ] && return ${EXIT_TRUE} | |
139 | done < ${DNS_SERVER_CONFIG_FILE} | |
140 | ||
141 | return ${EXIT_FALSE} | |
acc9efd5 MT |
142 | } |
143 | ||
144 | function dns_server_remove() { | |
e5651e17 MT |
145 | local entry=${1} |
146 | assert isset entry | |
acc9efd5 | 147 | |
e5651e17 MT |
148 | # Check if the DNS server configuration file exists. |
149 | [ -r "${DNS_SERVER_CONFIG_FILE}" ] || return ${EXIT_ERROR} | |
acc9efd5 | 150 | |
e5651e17 MT |
151 | # Create a tempfile. |
152 | local tempfile=$(mktemp) | |
153 | ||
154 | local line ${NETWORK_CONFIG_DNS_SERVER_PARAMS} | |
155 | while read -r line; do | |
156 | dns_server_parse_line ${line} || continue | |
157 | ||
158 | # Skip the line with the server we are searching for. | |
b4b2fa50 | 159 | [ "${entry}" = "${server}" ] && continue |
e5651e17 MT |
160 | |
161 | # Re-add the old line. | |
162 | print "${line}" | |
163 | done < ${DNS_SERVER_CONFIG_FILE} > ${tempfile} | |
164 | ||
165 | # Overwrite the old content without the entry that has just been removed. | |
166 | fread "${tempfile}" > ${DNS_SERVER_CONFIG_FILE} | |
167 | ||
168 | # Remove the temporary file. | |
169 | rm -f ${tempfile} | |
170 | ||
171 | return ${EXIT_OK} | |
acc9efd5 MT |
172 | } |
173 | ||
174 | function dns_server_flush() { | |
175 | : > ${DNS_SERVER_CONFIG_FILE} | |
176 | } | |
177 | ||
e5651e17 MT |
178 | function dns_server_parse_line() { |
179 | local arg | |
180 | ||
181 | for arg in ${NETWORK_CONFIG_DNS_SERVER_PARAMS}; do | |
182 | assign "${arg}" "" | |
183 | done | |
184 | ||
185 | while read -r arg; do | |
186 | case "${arg}" in | |
187 | server=*) | |
188 | server=$(cli_get_val ${arg}) | |
189 | ;; | |
190 | priority=*) | |
191 | priority=$(cli_get_val ${arg}) | |
192 | ;; | |
193 | esac | |
194 | done <<< "$(args $@)" | |
195 | ||
196 | # The server address must be set. | |
197 | isset server || return ${EXIT_ERROR} | |
198 | ||
199 | # The server address must also be a valid IP address. | |
200 | ip_is_valid ${server} || return ${EXIT_ERROR} | |
201 | ||
202 | # If priority is set, it must be an integer number. | |
203 | if isset priority; then | |
204 | isinteger priority || return ${EXIT_ERROR} | |
205 | ||
206 | # Otherwise assign the default priority. | |
207 | else | |
208 | priority=${DNS_SERVER_DEFAULT_PRIORITY} | |
209 | fi | |
210 | ||
211 | return ${EXIT_OK} | |
212 | } | |
213 | ||
a469c542 MT |
214 | # Update resolv.conf(5) when initializing the network. |
215 | init_register dns_generate_resolvconf | |
216 | ||
acc9efd5 MT |
217 | function dns_generate_resolvconf() { |
218 | local file=${RESOLV_CONF} | |
219 | ||
220 | log INFO "Updating resolver configuration..." | |
221 | ||
222 | config_header "resolver configutation file" > ${file} | |
223 | ||
224 | if enabled DNS_RANDOMIZE; then | |
225 | print "option rotate\n" >> ${file} | |
226 | fi | |
227 | ||
805da540 | 228 | # Write search domains to file. |
e5651e17 | 229 | print "# Search domains" >> ${file} |
b1d1b5ce MT |
230 | |
231 | local domain | |
232 | for domain in $(dns_get_search_domains); do | |
233 | print "search ${domain}" | |
a9ebc53b | 234 | done >> ${file} |
acc9efd5 | 235 | |
e5651e17 MT |
236 | print "\n# Nameservers" >> ${file} |
237 | ||
acc9efd5 MT |
238 | # Add the local resolver as the first DNS server if enabled. |
239 | if enabled DNS_USE_LOCAL_RESOLVER; then | |
240 | print "nameserver ::1" >> ${file} | |
241 | fi | |
242 | ||
e5651e17 MT |
243 | # Dump all DNS servers. |
244 | for server in $(dns_server_list_sorted); do | |
acc9efd5 MT |
245 | print "nameserver ${server}" |
246 | done >> ${file} | |
cccb3a4b | 247 | } |
a9ebc53b | 248 | |
b1d1b5ce MT |
249 | function dns_get_search_domains() { |
250 | # Add search domains. | |
251 | local search_domains="$(unquote ${DNS_SEARCH_DOMAINS})" | |
252 | ||
253 | # Get search domains from DHCP clients, etc. | |
254 | local domain proto zone | |
255 | ||
256 | for zone in $(zones_get_all); do | |
257 | for proto in ${IP_SUPPORTED_PROTOCOLS}; do | |
258 | domain=$(routing_db_get ${zone} ${proto} domain-name) | |
259 | isset domain || continue | |
260 | ||
261 | list_append search_domains "${domainname}" | |
262 | done | |
263 | done | |
264 | ||
265 | # Sort out duplicate entries. | |
266 | list_unique ${search_domains} | |
267 | } | |
268 | ||
e5651e17 MT |
269 | function dns_server_get_zone_name_servers() { |
270 | local priority proto server servers zone | |
271 | ||
a9ebc53b | 272 | for zone in $(zones_get_all); do |
a9ebc53b | 273 | for proto in ${IP_SUPPORTED_PROTOCOLS}; do |
e5651e17 MT |
274 | priority=$(routing_db_get ${zone} ${proto} domain-name-servers-priority) |
275 | isset priority || priority="${DNS_SERVER_DYNAMIC_PRIORITY}" | |
a9ebc53b | 276 | |
e5651e17 | 277 | servers=$(routing_db_get ${zone} ${proto} domain-name-servers) |
a9ebc53b | 278 | for server in ${servers}; do |
e5651e17 | 279 | print "${priority} ${server}" |
a9ebc53b KB |
280 | done |
281 | done | |
282 | done | |
283 | } |