#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # Copyright (C) 2012 IPFire # Network Development Team # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # but # WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU # General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # # ############################################################################### # Set this to true if localhost should be added as the first DNS server. DNS_USE_LOCAL_RESOLVER=true NETWORK_CONFIG_FILE_PARAMS="${NETWORK_CONFIG_FILE_PARAMS} DNS_USE_LOCAL_RESOLVER" # Set this option to true if the DNS servers should be queried in a # random order. This is useful to load balance between multiple servers. DNS_RANDOMIZE=false NETWORK_CONFIG_FILE_PARAMS="${NETWORK_CONFIG_FILE_PARAMS} DNS_RANDOMIZE" # Set this option to true if the DNS servers should be advertised by # radvd. DNS_ADVERTISE_SERVERS="true" DNS_SERVER_CONFIG_FILE="${NETWORK_CONFIG_DIR}/dns-servers" # Path to the configuration file of the DNS resolver. RESOLV_CONF="/etc/resolv.conf" function dns_get_hostname() { local address=${1} assert isset address ( unset HOSTNAME eval $(ipcalc -h ${address} 2>/dev/null) echo "${HOSTNAME}" ) } function __dns_server_println() { local server=${1} local priority=${2} print "%-20s %s" "${server}" "${priority}" } function __dns_server_sort() { sort -k2 -g | uniq } function dns_server_list() { [ -r "${DNS_SERVER_CONFIG_FILE}" ] || return ${EXIT_OK} local server priority while read server priority; do if [ -n "${server}" ] && [ -n "${priority}" ]; then __dns_server_println "${server}" "${priority}" fi done < ${DNS_SERVER_CONFIG_FILE} | __dns_server_sort } function dns_server_list_no_priority() { local server priority dns_server_list | while read server priority; do echo "${server}" done } function dns_server_add() { local server=${1} assert isset server local priority=${2} if ! isset priority; then priority=20 fi assert isinteger priority ( dns_server_list __dns_server_println "${server}" "${priority}" ) | __dns_server_sort > ${DNS_SERVER_CONFIG_FILE}.new mv ${DNS_SERVER_CONFIG_FILE}{.new,} } function dns_server_remove() { local server=${1} assert isset server local entry priority local entries=$(dns_server_list) while read entry priority; do [ "${entry}" = "${server}" ] && continue __dns_server_println "${server}" "${priority}" done <<< ${entries} | __dns_server_sort > ${DNS_SERVER_CONFIG_FILE} } function dns_server_flush() { : > ${DNS_SERVER_CONFIG_FILE} } # Update resolv.conf(5) when initializing the network. init_register dns_generate_resolvconf function dns_generate_resolvconf() { local file=${RESOLV_CONF} log INFO "Updating resolver configuration..." config_header "resolver configutation file" > ${file} if enabled DNS_RANDOMIZE; then print "option rotate\n" >> ${file} fi # XXX Add search domain. local proto local zone local domainname for zone in $(zones_get_all); do for proto in ${IP_SUPPORTED_PROTOCOLS}; do domainname=$(routing_db_get ${zone} ${proto} domain-name) if [ -n "${domainname}" ]; then print "search ${domainname}" fi done done >> ${file} # Add the local resolver as the first DNS server if enabled. if enabled DNS_USE_LOCAL_RESOLVER; then print "nameserver ::1" >> ${file} fi # First pull in zone name servers. local server for server in $(dns_get_zone_name_servers); do print "nameserver ${server}" done >> ${file} # Dump all DNS servers (if any). local priority dns_server_list | while read server priority; do print "nameserver ${server}" done >> ${file} } function dns_get_zone_name_servers() { local servers local zone for zone in $(zones_get_all); do local proto for proto in ${IP_SUPPORTED_PROTOCOLS}; do servers=$(routing_db_get ${zone} ${proto} domain-name-servers) local server for server in ${servers}; do print "${server}" done done done }