1#!/bin/bash 2# 3# Copyright (c) 2017, The OpenThread Authors. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are met: 8# 1. Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# 2. Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# 3. Neither the name of the copyright holder nor the 14# names of its contributors may be used to endorse or promote products 15# derived from this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27# POSSIBILITY OF SUCH DAMAGE. 28# 29# Description: 30# This script manipulates dns64 configuration. 31# 32 33BIND_CONF_OPTIONS=/etc/bind/named.conf.options 34NAT64_PREFIX=64:ff9b::/96 35 36DNS64_NAMESERVER_ADDR=127.0.0.1 37DNS64_CONF="dns64 $(echo $NAT64_PREFIX | tr \"/\" \"/\") { clients { thread; }; recursive-only yes; };" 38 39# Currently solution was verified only on raspbian and ubuntu. 40# 41without NAT64 || without DNS64 || test "$PLATFORM" = ubuntu || test "$PLATFORM" = beagleboneblack || test "$PLATFORM" = raspbian || die "dns64 is not tested under $PLATFORM." 42 43if [ "$PLATFORM" = raspbian ]; then 44 RESOLV_CONF_HEAD=/etc/resolv.conf.head 45elif [ "$PLATFORM" = beagleboneblack ]; then 46 RESOLV_CONF_HEAD=/etc/resolvconf/resolv.conf.d/head 47elif [ "$PLATFORM" = ubuntu ]; then 48 RESOLV_CONF_HEAD=/etc/resolvconf/resolv.conf.d/head 49fi 50 51dns64_update_resolvconf() 52{ 53 if [ "$PLATFORM" = ubuntu ]; then 54 sudo resolvconf -u || true 55 elif [ "$PLATFORM" = beagleboneblack ]; then 56 sudo resolvconf -u || true 57 elif [ "$PLATFORM" = raspbian ]; then 58 if systemctl is-enabled NetworkManager; then 59 sudo systemctl restart NetworkManager || true 60 fi 61 62 if systemctl is-enabled dhcpcd; then 63 sudo systemctl restart dhcpcd || true 64 fi 65 fi 66} 67 68_detect_service_name() 69{ 70 dpkg -L bind9 | grep /etc/init.d/ | cut -d/ -f4 71} 72 73dns64_install() 74{ 75 with NAT64 && with DNS64 || return 0 76 77 test -f $BIND_CONF_OPTIONS || die 'Cannot find bind9 configuration file!' 78 sudo sed -i '/^};/i\\tlisten-on-v6 { thread; };' $BIND_CONF_OPTIONS 79 sudo sed -i '/^\tlisten-on-v6 { a/d' $BIND_CONF_OPTIONS 80 sudo sed -i '/^};/i\\tallow-query { any; };' $BIND_CONF_OPTIONS 81 sudo sed -i '/^};/i\\tallow-recursion { thread; };' $BIND_CONF_OPTIONS 82 sudo sed -i '/^};/i\\tforwarders { 8.8.8.8; 8.8.8.4; };' $BIND_CONF_OPTIONS 83 sudo sed -i '/^};/i\\tforward only;' $BIND_CONF_OPTIONS 84 sudo sed -i '/^};/i\\t'"$DNS64_CONF" $BIND_CONF_OPTIONS 85 sudo sed -i '1s/^/acl thread {\n\tfe80::\/16;\n\tfc00::\/7;\n\t127.0.0.1;\n};\n\n/' $BIND_CONF_OPTIONS 86 87 service_name="$(_detect_service_name)" 88 89 if without DOCKER; then 90 sudo sh -c "echo \"nameserver $DNS64_NAMESERVER_ADDR\" >> $RESOLV_CONF_HEAD" 91 fi 92 93 if have systemctl; then 94 sudo systemctl stop dnsmasq || true 95 sudo systemctl disable dnsmasq || true 96 sudo systemctl enable "${service_name}" || true 97 sudo systemctl is-enabled "${service_name}" || die 'Failed to enable bind9!' 98 sudo systemctl start "${service_name}" || die 'Failed to start bind9!' 99 fi 100 101 if without DOCKER; then 102 dns64_update_resolvconf 103 fi 104} 105 106dns64_uninstall() 107{ 108 with NAT64 && with DNS64 || return 0 109 110 service_name="$(_detect_service_name)" 111 112 dns64_stop 113 sudo sed -i '/^\tlisten-on-v6/d' $BIND_CONF_OPTIONS 114 sudo sed -i '/^\tallow-query/d' $BIND_CONF_OPTIONS 115 sudo sed -i '/^\tallow-recursion/d' $BIND_CONF_OPTIONS 116 sudo sed -i '/^\tforward/d' $BIND_CONF_OPTIONS 117 sudo sed -i '/^};/i\\tlisten-on-v6 { any; };' $BIND_CONF_OPTIONS 118 sudo sed -i '/^\tdns64/d' $BIND_CONF_OPTIONS 119 sudo sed -i '/^acl/,/^options/{/^options/!d}' $BIND_CONF_OPTIONS 120 121 sudo sed -i '/^nameserver '$DNS64_NAMESERVER_ADDR'/d' $RESOLV_CONF_HEAD || true 122 123 if without DOCKER; then 124 dns64_update_resolvconf 125 fi 126 127 if have systemctl; then 128 sudo systemctl stop "${service_name}" || true 129 sudo systemctl disable "${service_name}" || true 130 fi 131} 132 133dns64_start() 134{ 135 with NAT64 && with DNS64 || return 0 136 137 service_name="$(_detect_service_name)" 138 139 if have systemctl; then 140 sudo systemctl start "${service_name}" || die 'Failed to start bind9!' 141 elif command -v service; then 142 sudo service "${service_name}" start || die 'Failed to start bind9!' 143 fi 144} 145 146dns64_stop() 147{ 148 with NAT64 && with DNS64 || return 0 149 150 service_name="$(_detect_service_name)" 151 152 if have systemctl; then 153 sudo systemctl stop "${service_name}" || true 154 elif command -v service; then 155 sudo service "${service_name}" stop || true 156 fi 157} 158