dnsmasq.in revision 219732
1219732Sume#!/bin/sh 2219732Sume# Copyright (c) 2007-2009 Roy Marples 3219732Sume# All rights reserved 4219732Sume 5219732Sume# dnsmasq subscriber for resolvconf 6219732Sume 7219732Sume# Redistribution and use in source and binary forms, with or without 8219732Sume# modification, are permitted provided that the following conditions 9219732Sume# are met: 10219732Sume# * Redistributions of source code must retain the above copyright 11219732Sume# notice, this list of conditions and the following disclaimer. 12219732Sume# * Redistributions in binary form must reproduce the above 13219732Sume# copyright notice, this list of conditions and the following 14219732Sume# disclaimer in the documentation and/or other materials provided 15219732Sume# with the distribution. 16219732Sume# 17219732Sume# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18219732Sume# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19219732Sume# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20219732Sume# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21219732Sume# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22219732Sume# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23219732Sume# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24219732Sume# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25219732Sume# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26219732Sume# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27219732Sume# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28219732Sume 29219732Sume[ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0 30219732Sume. "@SYSCONFDIR@/resolvconf.conf" || exit 1 31219732Sume[ -z "$dnsmasq_conf" -a -z "$dnsmasq_resolv" ] && exit 0 32219732Sume[ -z "$RESOLVCONF" ] && eval "$(@PREFIX@/sbin/resolvconf -v)" 33219732Sume 34219732Sume: ${dnsmasq_pid:=/var/run/dnsmasq.pid} 35219732Sume[ -s "$dnsmasq_pid" ] || dnsmasq_pid=/var/run/dnsmasq/dnsmasq.pid 36219732Sume: ${dnsmasq_service:=dnsmasq} 37219732Sume: ${dnsmasq_restart:=@RESTARTCMD ${dnsmasq_service}@} 38219732Sumenewconf="# Generated by resolvconf\n" 39219732Sumenewresolv="$newconf" 40219732Sume 41219732Sume# Using dbus means that we never have to restart the daemon 42219732Sume# This is important as it means we should not drop DNS queries 43219732Sume# whilst changing DNS options around. However, dbus support is optional 44219732Sume# so we need to validate a few things first. 45219732Sume# Check for DBus support in the binary 46219732Sumedbus=false 47219732Sume: ${dbus_pid:=/var/run/dbus/dbus.pid} 48219732Sume[ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus.pid 49219732Sume[ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus/pid 50219732Sumeif [ -s "$dbus_pid" -a -s "$dnsmasq_pid" ]; then 51219732Sume if dnsmasq --version 2>/dev/null | \ 52219732Sume grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]" 53219732Sume then 54219732Sume # Sanity - check that dnsmasq and dbus are running 55219732Sume if kill -0 $(cat "$dbus_pid") 2>/dev/null && \ 56219732Sume kill -0 $(cat "$dnsmasq_pid") 2>/dev/null 57219732Sume then 58219732Sume dbus=true 59219732Sume newconf="$newconf\n# Domain specific servers will" 60219732Sume newconf="$newconf be sent over dbus\nenable-dbus\n" 61219732Sume fi 62219732Sume fi 63219732Sumefi 64219732Sume 65219732Sumefor n in $NAMESERVERS; do 66219732Sume newresolv="${newresolv}nameserver $n\n" 67219732Sumedone 68219732Sume 69219732Sumedbusdest= 70219732Sumefor d in $DOMAINS; do 71219732Sume dn="${d%%:*}" 72219732Sume ns="${d#*:}" 73219732Sume while [ -n "$ns" ]; do 74219732Sume if $dbus; then 75219732Sume SIFS=${IFS-y} OIFS=$IFS 76219732Sume IFS=. 77219732Sume set -- ${ns%%,*} 78219732Sume num="0x$(printf "%02x" $1 $2 $3 $4)" 79219732Sume if [ "$SIFS" = yi ]; then 80219732Sume unset IFS 81219732Sume else 82219732Sume IFS=$OIFS 83219732Sume fi 84219732Sume dbusdest="$dbusdest uint32:$(printf "%u" $num)" 85219732Sume dbusdest="$dbusdest string:$dn" 86219732Sume else 87219732Sume newconf="${newconf}server=/$dn/${ns%%,*}\n" 88219732Sume fi 89219732Sume [ "$ns" = "${ns#*,}" ] && break 90219732Sume ns="${ns#*,}" 91219732Sume done 92219732Sumedone 93219732Sume 94219732Sumechanged=false 95219732Sumeif [ -n "$dnsmasq_conf" ]; then 96219732Sume if [ ! -f "$dnsmasq_conf" ] || \ 97219732Sume [ "$(cat "$dnsmasq_conf")" != "$(printf "$newconf")" ] 98219732Sume then 99219732Sume changed=true 100219732Sume printf "$newconf" >"$dnsmasq_conf" 101219732Sume fi 102219732Sumefi 103219732Sumeif [ -n "$dnsmasq_resolv" ]; then 104219732Sume if [ -f "$dnsmasq_resolv" ]; then 105219732Sume if [ "$(cat "$dnsmasq_resolv")" != "$(printf "$newresolv")" ] 106219732Sume then 107219732Sume changed=true 108219732Sume printf "$newresolv" >"$dnsmasq_resolv" 109219732Sume fi 110219732Sume else 111219732Sume # dnsmasq polls this file so no need to set changed=true 112219732Sume printf "$newresolv" >"$dnsmasq_resolv" 113219732Sume fi 114219732Sumefi 115219732Sume 116219732Sumeif $changed; then 117219732Sume eval $dnsmasq_restart 118219732Sumefi 119219732Sumeif $dbus; then 120219732Sume $changed || kill -HUP $(cat "$dnsmasq_pid") 121219732Sume # Send even if empty so old servers are cleared 122219732Sume dbus-send --system --dest=uk.org.thekelleys.dnsmasq \ 123219732Sume /uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \ 124219732Sume $dbusdest 125219732Sumefi 126