]> git.ipfire.org Git - people/stevee/network.git/blame - functions.dns
Revert "stp: Remove function that switches protocols."
[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
6f923dac
MT
31# Set this option to true if the DNS servers should be advertised by
32# radvd.
33DNS_ADVERTISE_SERVERS="true"
34
acc9efd5
MT
35DNS_SERVER_CONFIG_FILE="${NETWORK_CONFIG_DIR}/dns-servers"
36
37# Path to the configuration file of the DNS resolver.
38RESOLV_CONF="/etc/resolv.conf"
39
cccb3a4b
MT
40function dns_get_hostname() {
41 local address=${1}
42 assert isset address
43
acc9efd5
MT
44 (
45 unset HOSTNAME
46 eval $(ipcalc -h ${address} 2>/dev/null)
47 echo "${HOSTNAME}"
48 )
49}
50
51function __dns_server_println() {
52 local server=${1}
53 local priority=${2}
54
55 print "%-20s %s" "${server}" "${priority}"
56}
57
58function __dns_server_sort() {
6f923dac 59 sort -k2 -g | uniq
acc9efd5
MT
60}
61
62function 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
6f923dac
MT
73function 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
acc9efd5
MT
80function 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
98function 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
111function dns_server_flush() {
112 : > ${DNS_SERVER_CONFIG_FILE}
113}
114
a469c542
MT
115# Update resolv.conf(5) when initializing the network.
116init_register dns_generate_resolvconf
117
acc9efd5
MT
118function 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
131 # Add the local resolver as the first DNS server if enabled.
132 if enabled DNS_USE_LOCAL_RESOLVER; then
133 print "nameserver ::1" >> ${file}
134 fi
135
136 # Dump all DNS servers (if any).
137 local server priority
138 dns_server_list | while read server priority; do
139 print "nameserver ${server}"
140 done >> ${file}
cccb3a4b 141}