blob: 80f3a64225d3138b65d3f4191e4989f8fb6e9b3c [file] [log] [blame]
Simon Kelley48493322013-05-28 14:49:54 +01001#!/bin/sh
Simon Kelleyc72daea2012-01-05 21:33:27 +00002#
3# Script to update the resolver list for dnsmasq
4#
Simon Kelley48493322013-05-28 14:49:54 +01005# N.B. Resolvconf may run us even if dnsmasq is not (yet) running.
6# If dnsmasq is installed then we go ahead and update the resolver list
7# in case dnsmasq is started later.
Simon Kelleyc72daea2012-01-05 21:33:27 +00008#
Simon Kelley48493322013-05-28 14:49:54 +01009# Assumption: On entry, PWD contains the resolv.conf-type files.
Simon Kelleyc72daea2012-01-05 21:33:27 +000010#
Simon Kelley48493322013-05-28 14:49:54 +010011# This file is part of the dnsmasq package.
Simon Kelleyc72daea2012-01-05 21:33:27 +000012#
13
14set -e
15
16RUN_DIR="/var/run/dnsmasq"
17RSLVRLIST_FILE="${RUN_DIR}/resolv.conf"
18TMP_FILE="${RSLVRLIST_FILE}_new.$$"
Simon Kelley48493322013-05-28 14:49:54 +010019MY_RECORD_NAME="lo.dnsmasq"
20DNSCRYPT_RECORD_NAME="lo.dnscrypt"
Simon Kelleyc72daea2012-01-05 21:33:27 +000021
22[ -x /usr/sbin/dnsmasq ] || exit 0
23[ -x /lib/resolvconf/list-records ] || exit 1
24
25PATH=/bin:/sbin
26
27report_err() { echo "$0: Error: $*" >&2 ; }
28
29# Stores arguments (minus duplicates) in RSLT, separated by spaces
Simon Kelley48493322013-05-28 14:49:54 +010030# Doesn't work properly if an argument itself contains whitespace
Simon Kelleyc72daea2012-01-05 21:33:27 +000031uniquify()
32{
33 RSLT=""
34 while [ "$1" ] ; do
35 for E in $RSLT ; do
36 [ "$1" = "$E" ] && { shift ; continue 2 ; }
37 done
38 RSLT="${RSLT:+$RSLT }$1"
39 shift
40 done
41}
42
43if [ ! -d "$RUN_DIR" ] && ! mkdir --parents --mode=0755 "$RUN_DIR" ; then
44 report_err "Failed trying to create directory $RUN_DIR"
45 exit 1
46fi
47
Simon Kelley48493322013-05-28 14:49:54 +010048RSLVCNFFILES=""
Simon Kelleyf086d392013-08-14 14:54:23 +010049for F in $(/lib/resolvconf/list-records --after "$MY_RECORD_NAME") ; do
Simon Kelley48493322013-05-28 14:49:54 +010050 case "$F" in
51 "$MY_RECORD_NAME")
52 # Omit
53 ;;
54 "$DNSCRYPT_RECORD_NAME")
55 # Dnscrypt, I only have eyes for you
56 RSLVCNFFILES="$DNSCRYPT_RECORD_NAME"
57 break
58 ;;
59 *)
60 RSLVCNFFILES="${RSLVCNFFILES:+$RSLVCNFFILES }$F"
61 ;;
62 esac
63done
Simon Kelleyc72daea2012-01-05 21:33:27 +000064
65NMSRVRS=""
66if [ "$RSLVCNFFILES" ] ; then
67 uniquify $(sed -n -e 's/^[[:space:]]*nameserver[[:space:]]\+//p' $RSLVCNFFILES)
68 NMSRVRS="$RSLT"
69fi
70
71# Dnsmasq uses the mtime of $RSLVRLIST_FILE, with a resolution of one second,
72# to detect changes in the file. This means that if a resolvconf update occurs
73# within one second of the previous one then dnsmasq may fail to notice the
Simon Kelley48493322013-05-28 14:49:54 +010074# more recent change. To work around this problem we sleep one second here
75# if necessary in order to ensure that the new mtime is different.
Simon Kelleyc72daea2012-01-05 21:33:27 +000076if [ -f "$RSLVRLIST_FILE" ] && [ "$(ls -go --time-style='+%s' "$RSLVRLIST_FILE" | { read p h s t n ; echo "$t" ; })" = "$(date +%s)" ] ; then
77 sleep 1
78fi
79
80clean_up() { rm -f "$TMP_FILE" ; }
81trap clean_up EXIT
82: >| "$TMP_FILE"
83for N in $NMSRVRS ; do echo "nameserver $N" >> "$TMP_FILE" ; done
84mv -f "$TMP_FILE" "$RSLVRLIST_FILE"
85