]> git.ipfire.org Git - people/stevee/network.git/blame - functions.dns
DNS: Add options to configure local DNS servers.
[people/stevee/network.git] / functions.dns
CommitLineData
cccb3a4b
MT
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
acc9efd5
MT
22# Set this to true if localhost should be added as the first DNS server.
23DNS_USE_LOCAL_RESOLVER=true
24NETWORK_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.
28DNS_RANDOMIZE=false
29NETWORK_CONFIG_FILE_PARAMS="${NETWORK_CONFIG_FILE_PARAMS} DNS_RANDOMIZE"
30
31DNS_SERVER_CONFIG_FILE="${NETWORK_CONFIG_DIR}/dns-servers"
32
33# Path to the configuration file of the DNS resolver.
34RESOLV_CONF="/etc/resolv.conf"
35
cccb3a4b
MT
36function dns_get_hostname() {
37 local address=${1}
38 assert isset address
39
acc9efd5
MT
40 (
41 unset HOSTNAME
42 eval $(ipcalc -h ${address} 2>/dev/null)
43 echo "${HOSTNAME}"
44 )
45}
46
47function __dns_server_println() {
48 local server=${1}
49 local priority=${2}
50
51 print "%-20s %s" "${server}" "${priority}"
52}
53
54function __dns_server_sort() {
55 sort -k2 -u -g
56}
57
58function dns_server_list() {
59 [ -r "${DNS_SERVER_CONFIG_FILE}" ] || return ${EXIT_OK}
60
61 local server priority
62 while read server priority; do
63 if [ -n "${server}" ] && [ -n "${priority}" ]; then
64 __dns_server_println "${server}" "${priority}"
65 fi
66 done < ${DNS_SERVER_CONFIG_FILE} | __dns_server_sort
67}
68
69function dns_server_add() {
70 local server=${1}
71 assert isset server
72
73 local priority=${2}
74 if ! isset priority; then
75 priority=20
76 fi
77 assert isinteger priority
78
79 (
80 dns_server_list
81 __dns_server_println "${server}" "${priority}"
82 ) | __dns_server_sort > ${DNS_SERVER_CONFIG_FILE}.new
83
84 mv ${DNS_SERVER_CONFIG_FILE}{.new,}
85}
86
87function dns_server_remove() {
88 local server=${1}
89 assert isset server
90
91 local entry priority
92 local entries=$(dns_server_list)
93
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}
98}
99
100function dns_server_flush() {
101 : > ${DNS_SERVER_CONFIG_FILE}
102}
103
104function dns_generate_resolvconf() {
105 local file=${RESOLV_CONF}
106
107 log INFO "Updating resolver configuration..."
108
109 config_header "resolver configutation file" > ${file}
110
111 if enabled DNS_RANDOMIZE; then
112 print "option rotate\n" >> ${file}
113 fi
114
115 # XXX Add search domain.
116
117 # Add the local resolver as the first DNS server if enabled.
118 if enabled DNS_USE_LOCAL_RESOLVER; then
119 print "nameserver ::1" >> ${file}
120 fi
121
122 # Dump all DNS servers (if any).
123 local server priority
124 dns_server_list | while read server priority; do
125 print "nameserver ${server}"
126 done >> ${file}
cccb3a4b 127}