]> git.ipfire.org Git - people/stevee/network.git/blob - functions.dns
Undo the nano damage from a9ebc53bddcff8f13d7dd0cada8b9c03b20e344f.
[people/stevee/network.git] / functions.dns
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
6 # #
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. #
11 # #
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. #
16 # #
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/>. #
19 # #
20 ###############################################################################
21
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"
25
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
29 NETWORK_CONFIG_FILE_PARAMS="${NETWORK_CONFIG_FILE_PARAMS} DNS_RANDOMIZE"
30
31 # Set this option to true if the DNS servers should be advertised by
32 # radvd.
33 DNS_ADVERTISE_SERVERS="true"
34
35 DNS_SERVER_CONFIG_FILE="${NETWORK_CONFIG_DIR}/dns-servers"
36
37 # Path to the configuration file of the DNS resolver.
38 RESOLV_CONF="/etc/resolv.conf"
39
40 function dns_get_hostname() {
41 local address=${1}
42 assert isset address
43
44 (
45 unset HOSTNAME
46 eval $(ipcalc -h ${address} 2>/dev/null)
47 echo "${HOSTNAME}"
48 )
49 }
50
51 function __dns_server_println() {
52 local server=${1}
53 local priority=${2}
54
55 print "%-20s %s" "${server}" "${priority}"
56 }
57
58 function __dns_server_sort() {
59 sort -k2 -g | uniq
60 }
61
62 function dns_server_list() {
63 [ -r "${DNS_SERVER_CONFIG_FILE}" ] || return ${EXIT_OK}
64
65 local server priority
66 while read server priority; do
67 if [ -n "${server}" ] && [ -n "${priority}" ]; then
68 __dns_server_println "${server}" "${priority}"
69 fi
70 done < ${DNS_SERVER_CONFIG_FILE} | __dns_server_sort
71 }
72
73 function dns_server_list_no_priority() {
74 local server priority
75 dns_server_list | while read server priority; do
76 echo "${server}"
77 done
78 }
79
80 function dns_server_add() {
81 local server=${1}
82 assert isset server
83
84 local priority=${2}
85 if ! isset priority; then
86 priority=20
87 fi
88 assert isinteger priority
89
90 (
91 dns_server_list
92 __dns_server_println "${server}" "${priority}"
93 ) | __dns_server_sort > ${DNS_SERVER_CONFIG_FILE}.new
94
95 mv ${DNS_SERVER_CONFIG_FILE}{.new,}
96 }
97
98 function dns_server_remove() {
99 local server=${1}
100 assert isset server
101
102 local entry priority
103 local entries=$(dns_server_list)
104
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}
109 }
110
111 function dns_server_flush() {
112 : > ${DNS_SERVER_CONFIG_FILE}
113 }
114
115 # Update resolv.conf(5) when initializing the network.
116 init_register dns_generate_resolvconf
117
118 function dns_generate_resolvconf() {
119 local file=${RESOLV_CONF}
120
121 log INFO "Updating resolver configuration..."
122
123 config_header "resolver configutation file" > ${file}
124
125 if enabled DNS_RANDOMIZE; then
126 print "option rotate\n" >> ${file}
127 fi
128
129 # XXX Add search domain.
130 local proto
131 local zone
132 local domainname
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}"
138 fi
139 done
140 done >> ${file}
141
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}
145 fi
146
147 # First pull in zone name servers.
148 local server
149 for server in $(dns_get_zone_name_servers); do
150 print "nameserver ${server}"
151 done >> ${file}
152
153 # Dump all DNS servers (if any).
154 local priority
155 dns_server_list | while read server priority; do
156 print "nameserver ${server}"
157 done >> ${file}
158 }
159
160 function dns_get_zone_name_servers() {
161 local servers
162 local zone
163 for zone in $(zones_get_all); do
164 local proto
165 for proto in ${IP_SUPPORTED_PROTOCOLS}; do
166 servers=$(routing_db_get ${zone} ${proto} domain-name-servers)
167
168 local server
169 for server in ${servers}; do
170 print "${server}"
171 done
172 done
173 done
174 }