1587392a5SHajimu UMEMOTO#!/bin/sh 2*9af6c78cSPedro F. Giffuni# Copyright (c) 2007-2019 Roy Marples 3587392a5SHajimu UMEMOTO# All rights reserved 4587392a5SHajimu UMEMOTO 5587392a5SHajimu UMEMOTO# dnsmasq subscriber for resolvconf 6587392a5SHajimu UMEMOTO 7587392a5SHajimu UMEMOTO# Redistribution and use in source and binary forms, with or without 8587392a5SHajimu UMEMOTO# modification, are permitted provided that the following conditions 9587392a5SHajimu UMEMOTO# are met: 10587392a5SHajimu UMEMOTO# * Redistributions of source code must retain the above copyright 11587392a5SHajimu UMEMOTO# notice, this list of conditions and the following disclaimer. 12587392a5SHajimu UMEMOTO# * Redistributions in binary form must reproduce the above 13587392a5SHajimu UMEMOTO# copyright notice, this list of conditions and the following 14587392a5SHajimu UMEMOTO# disclaimer in the documentation and/or other materials provided 15587392a5SHajimu UMEMOTO# with the distribution. 16587392a5SHajimu UMEMOTO# 17587392a5SHajimu UMEMOTO# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18587392a5SHajimu UMEMOTO# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19587392a5SHajimu UMEMOTO# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20587392a5SHajimu UMEMOTO# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21587392a5SHajimu UMEMOTO# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22587392a5SHajimu UMEMOTO# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23587392a5SHajimu UMEMOTO# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24587392a5SHajimu UMEMOTO# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25587392a5SHajimu UMEMOTO# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26587392a5SHajimu UMEMOTO# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27587392a5SHajimu UMEMOTO# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28587392a5SHajimu UMEMOTO 29587392a5SHajimu UMEMOTO[ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0 30587392a5SHajimu UMEMOTO. "@SYSCONFDIR@/resolvconf.conf" || exit 1 31*9af6c78cSPedro F. Giffuni[ -z "${dnsmasq_conf}${dnsmasq_resolv}" ] && exit 0 32d7149f4eSGlen Barber[ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)" 33a02aba5fSHiroki SatoNL=" 34a02aba5fSHiroki Sato" 35587392a5SHajimu UMEMOTO 36587392a5SHajimu UMEMOTO: ${dnsmasq_pid:=/var/run/dnsmasq.pid} 37587392a5SHajimu UMEMOTO[ -s "$dnsmasq_pid" ] || dnsmasq_pid=/var/run/dnsmasq/dnsmasq.pid 38d7149f4eSGlen Barber[ -s "$dnsmasq_pid" ] || unset dnsmasq_pid 39587392a5SHajimu UMEMOTO: ${dnsmasq_service:=dnsmasq} 40a02aba5fSHiroki Satonewconf="# Generated by resolvconf$NL" 41587392a5SHajimu UMEMOTOnewresolv="$newconf" 42587392a5SHajimu UMEMOTO 43587392a5SHajimu UMEMOTO# Using dbus means that we never have to restart the daemon 44587392a5SHajimu UMEMOTO# This is important as it means we should not drop DNS queries 45587392a5SHajimu UMEMOTO# whilst changing DNS options around. However, dbus support is optional 46587392a5SHajimu UMEMOTO# so we need to validate a few things first. 47587392a5SHajimu UMEMOTO# Check for DBus support in the binary 48587392a5SHajimu UMEMOTOdbus=false 49d7149f4eSGlen Barberdbus_ex=false 50d7149f4eSGlen Barberdbus_introspect=$(dbus-send --print-reply --system \ 51d7149f4eSGlen Barber --dest=uk.org.thekelleys.dnsmasq \ 52d7149f4eSGlen Barber /uk/org/thekelleys/dnsmasq \ 53d7149f4eSGlen Barber org.freedesktop.DBus.Introspectable.Introspect \ 54d7149f4eSGlen Barber 2>/dev/null) 55d7149f4eSGlen Barberif [ $? = 0 ]; then 56587392a5SHajimu UMEMOTO dbus=true 57d7149f4eSGlen Barber if printf %s "$dbus_introspect" | \ 58d7149f4eSGlen Barber grep -q '<method name="SetDomainServers">' 59d7149f4eSGlen Barber then 60d7149f4eSGlen Barber dbus_ex=true 61587392a5SHajimu UMEMOTO fi 62587392a5SHajimu UMEMOTOfi 63587392a5SHajimu UMEMOTO 64587392a5SHajimu UMEMOTOfor n in $NAMESERVERS; do 65a02aba5fSHiroki Sato newresolv="${newresolv}nameserver $n$NL" 66587392a5SHajimu UMEMOTOdone 67587392a5SHajimu UMEMOTO 68587392a5SHajimu UMEMOTOdbusdest= 69d7149f4eSGlen Barberdbusdest_ex= 70d7149f4eSGlen Barberconf= 71587392a5SHajimu UMEMOTOfor d in $DOMAINS; do 72587392a5SHajimu UMEMOTO dn="${d%%:*}" 73587392a5SHajimu UMEMOTO ns="${d#*:}" 74587392a5SHajimu UMEMOTO while [ -n "$ns" ]; do 75d7149f4eSGlen Barber n="${ns%%,*}" 76d7149f4eSGlen Barber if $dbus && ! $dbus_ex; then 77d7149f4eSGlen Barber case "$n" in 78d7149f4eSGlen Barber *.*.*.*) 79587392a5SHajimu UMEMOTO SIFS=${IFS-y} OIFS=$IFS 80587392a5SHajimu UMEMOTO IFS=. 81d7149f4eSGlen Barber set -- $n 82a02aba5fSHiroki Sato num="0x$(printf %02x $1 $2 $3 $4)" 83d7149f4eSGlen Barber if [ "$SIFS" = y ]; then 84587392a5SHajimu UMEMOTO unset IFS 85587392a5SHajimu UMEMOTO else 86587392a5SHajimu UMEMOTO IFS=$OIFS 87587392a5SHajimu UMEMOTO fi 88a02aba5fSHiroki Sato dbusdest="$dbusdest uint32:$(printf %u $num)" 89587392a5SHajimu UMEMOTO dbusdest="$dbusdest string:$dn" 90d7149f4eSGlen Barber ;; 91d7149f4eSGlen Barber *:*%*) 92d7149f4eSGlen Barber # This version of dnsmasq won't accept 93d7149f4eSGlen Barber # scoped IPv6 addresses 94d7149f4eSGlen Barber dbus=false 95d7149f4eSGlen Barber ;; 96d7149f4eSGlen Barber *:*) 97d7149f4eSGlen Barber SIFS=${IFS-y} OIFS=$IFS bytes= front= back= 98d7149f4eSGlen Barber empty=false i=0 99d7149f4eSGlen Barber IFS=: 100d7149f4eSGlen Barber set -- $n 101*9af6c78cSPedro F. Giffuni while [ -n "$1" ] || [ -n "$2" ]; do 102d7149f4eSGlen Barber addr="$1" 103d7149f4eSGlen Barber shift 104d7149f4eSGlen Barber if [ -z "$addr" ]; then 105d7149f4eSGlen Barber empty=true 106d7149f4eSGlen Barber continue 107587392a5SHajimu UMEMOTO fi 108d7149f4eSGlen Barber i=$(($i + 1)) 109d7149f4eSGlen Barber while [ ${#addr} -lt 4 ]; do 110d7149f4eSGlen Barber addr="0${addr}" 111d7149f4eSGlen Barber done 112d7149f4eSGlen Barber byte1="$(printf %d 0x${addr%??})" 113d7149f4eSGlen Barber byte2="$(printf %d 0x${addr#??})" 114d7149f4eSGlen Barber if $empty; then 115d7149f4eSGlen Barber back="$back byte:$byte1 byte:$byte2" 116d7149f4eSGlen Barber else 117d7149f4eSGlen Barber front="$front byte:$byte1 byte:$byte2" 118d7149f4eSGlen Barber fi 119d7149f4eSGlen Barber done 120d7149f4eSGlen Barber while [ $i != 8 ]; do 121d7149f4eSGlen Barber i=$(($i + 1)) 122d7149f4eSGlen Barber front="$front byte:0 byte:0" 123d7149f4eSGlen Barber done 124d7149f4eSGlen Barber front="${front}$back" 125d7149f4eSGlen Barber if [ "$SIFS" = y ]; then 126d7149f4eSGlen Barber unset IFS 127d7149f4eSGlen Barber else 128d7149f4eSGlen Barber IFS=$OIFS 129d7149f4eSGlen Barber fi 130d7149f4eSGlen Barber dbusdest="${dbusdest}$front string:$dn" 131d7149f4eSGlen Barber ;; 132d7149f4eSGlen Barber *) 133d7149f4eSGlen Barber if ! $dbus_ex; then 134d7149f4eSGlen Barber dbus=false 135d7149f4eSGlen Barber fi 136d7149f4eSGlen Barber ;; 137d7149f4eSGlen Barber esac 138d7149f4eSGlen Barber fi 139d7149f4eSGlen Barber dbusdest_ex="$dbusdest_ex${dbusdest_ex:+,}/$dn/$n" 140d7149f4eSGlen Barber conf="${conf}server=/$dn/$n$NL" 141587392a5SHajimu UMEMOTO [ "$ns" = "${ns#*,}" ] && break 142587392a5SHajimu UMEMOTO ns="${ns#*,}" 143587392a5SHajimu UMEMOTO done 144587392a5SHajimu UMEMOTOdone 145587392a5SHajimu UMEMOTO 146d7149f4eSGlen Barberif $dbus; then 147d7149f4eSGlen Barber newconf="$newconf$NL# Domain specific servers will" 148d7149f4eSGlen Barber newconf="$newconf be sent over dbus${NL}" 149d7149f4eSGlen Barberelse 150d7149f4eSGlen Barber newconf="$newconf$conf" 151d7149f4eSGlen Barberfi 152d7149f4eSGlen Barber 153d7149f4eSGlen Barber# Try to ensure that config dirs exist 154d7149f4eSGlen Barberif type config_mkdirs >/dev/null 2>&1; then 155d7149f4eSGlen Barber config_mkdirs "$dnsmasq_conf" "$dnsmasq_resolv" 156d7149f4eSGlen Barberelse 157d7149f4eSGlen Barber @SBINDIR@/resolvconf -D "$dnsmasq_conf" "$dnsmasq_resolv" 158d7149f4eSGlen Barberfi 159d7149f4eSGlen Barber 160587392a5SHajimu UMEMOTOchanged=false 161587392a5SHajimu UMEMOTOif [ -n "$dnsmasq_conf" ]; then 162587392a5SHajimu UMEMOTO if [ ! -f "$dnsmasq_conf" ] || \ 163a02aba5fSHiroki Sato [ "$(cat "$dnsmasq_conf")" != "$(printf %s "$newconf")" ] 164587392a5SHajimu UMEMOTO then 165587392a5SHajimu UMEMOTO changed=true 166a02aba5fSHiroki Sato printf %s "$newconf" >"$dnsmasq_conf" 167587392a5SHajimu UMEMOTO fi 168587392a5SHajimu UMEMOTOfi 169587392a5SHajimu UMEMOTOif [ -n "$dnsmasq_resolv" ]; then 170d7149f4eSGlen Barber # dnsmasq polls this file so no need to set changed=true 171587392a5SHajimu UMEMOTO if [ -f "$dnsmasq_resolv" ]; then 172a02aba5fSHiroki Sato if [ "$(cat "$dnsmasq_resolv")" != "$(printf %s "$newresolv")" ] 173587392a5SHajimu UMEMOTO then 174a02aba5fSHiroki Sato printf %s "$newresolv" >"$dnsmasq_resolv" 175587392a5SHajimu UMEMOTO fi 176587392a5SHajimu UMEMOTO else 177a02aba5fSHiroki Sato printf %s "$newresolv" >"$dnsmasq_resolv" 178587392a5SHajimu UMEMOTO fi 179587392a5SHajimu UMEMOTOfi 180587392a5SHajimu UMEMOTO 181587392a5SHajimu UMEMOTOif $changed; then 1823f2a60a1SPedro F. Giffuni # dnsmasq does not re-read the configuration file on SIGHUP 1833f2a60a1SPedro F. Giffuni if [ -n "$dnsmasq_restart" ]; then 184587392a5SHajimu UMEMOTO eval $dnsmasq_restart 1853f2a60a1SPedro F. Giffuni elif [ -n "$RESTARTCMD" ]; then 1863f2a60a1SPedro F. Giffuni set -- ${dnsmasq_service} 187*9af6c78cSPedro F. Giffuni eval "$RESTARTCMD" 1883f2a60a1SPedro F. Giffuni else 1893f2a60a1SPedro F. Giffuni @SBINDIR@/resolvconf -r ${dnsmasq_service} 1903f2a60a1SPedro F. Giffuni fi 191587392a5SHajimu UMEMOTOfi 192587392a5SHajimu UMEMOTOif $dbus; then 193d7149f4eSGlen Barber if [ -s "$dnsmasq_pid" ]; then 194587392a5SHajimu UMEMOTO $changed || kill -HUP $(cat "$dnsmasq_pid") 195d7149f4eSGlen Barber fi 196587392a5SHajimu UMEMOTO # Send even if empty so old servers are cleared 197d7149f4eSGlen Barber if $dbus_ex; then 198d7149f4eSGlen Barber method=SetDomainServers 199d7149f4eSGlen Barber if [ -n "$dbusdest_ex" ]; then 200d7149f4eSGlen Barber dbusdest_ex="array:string:$dbusdest_ex" 201d7149f4eSGlen Barber fi 202d7149f4eSGlen Barber dbusdest="$dbusdest_ex" 203d7149f4eSGlen Barber else 204d7149f4eSGlen Barber method=SetServers 205d7149f4eSGlen Barber fi 206587392a5SHajimu UMEMOTO dbus-send --system --dest=uk.org.thekelleys.dnsmasq \ 207d7149f4eSGlen Barber /uk/org/thekelleys/dnsmasq uk.org.thekelleys.$method \ 208587392a5SHajimu UMEMOTO $dbusdest 209*9af6c78cSPedro F. Giffuni dbus-send --system --dest=uk.org.thekelleys.dnsmasq \ 210*9af6c78cSPedro F. Giffuni /uk/org/thekelleys/dnsmasq uk.org.thekelleys.ClearCache 211587392a5SHajimu UMEMOTOfi 212