Release openresolv-3.5.4
[openresolv] / pdnsd.in
index d94d2fe625d57f85b2c9b0ff3a98a8214e03b720..0a0241a7c7b3df92e85fee20a020f8b6d598083e 100644 (file)
--- a/pdnsd.in
+++ b/pdnsd.in
@@ -1,5 +1,5 @@
 #!/bin/sh
-# Copyright (c) 2010 Roy Marples
+# Copyright (c) 2010-2011 Roy Marples
 # All rights reserved
 
 # pdnsd subscriber for resolvconf
 
 [ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
 . "@SYSCONFDIR@/resolvconf.conf" || exit 1
-[ -z "$pdnsd_resolv" ] && exit 0
+[ -z "$pdnsd_conf" -a -z "$pdnsd_resolv" ] && exit 0
 [ -z "$RESOLVCONF" ] && eval "$(@PREFIX@/sbin/resolvconf -v)"
+NL="
+"
 
-: ${pdnsd_restart:=pdnsd-ctl config}
-newresolv="# Generated by resolvconf\n"
+: ${pdnsd_restart:=pdnsd-ctl config $pdnsd_conf}
+signature="# Generated by resolvconf"
+signature_end="# End of resolvconf"
 
-for n in $NAMESERVERS; do
-       newresolv="${newresolv}nameserver $n\n"
-done
+# We normally use sed to remove markers from a configuration file
+# but sed may not always be available at the time.
+remove_markers()
+{
+       local m1="$1" m2="$2" x= line= in_marker=0
 
-# pdnsd does now support domain forwarding
+       shift; shift
+       if type sed >/dev/null 2>&1; then
+               sed "/^$m1/,/^$m2/d" $@
+       else
+               for x; do
+                       while read -r line; do
+                               case "$line" in
+                               "$m1"*) in_marker=1;;
+                               "$m2"*) in_marker=0;;
+                               *) [ $in_marker = 0 ] && echo "$line";;
+                               esac
+                       done < "$x"
+               done
+       fi
+}
 
+# Compare two files
+# If different, replace first with second otherwise remove second
+change_file()
+{
+       if [ -e "$1" ]; then
+               if type cmp >/dev/null 2>&1; then
+                       cmp -s "$1" "$2"
+               elif type diff >/dev/null 2>&1; then
+                       diff -q "$1" "$2" >/dev/null
+               else
+                       # Hopefully we're only working on small text files ...
+                       [ "$(cat "$1")" = "$(cat "$2")" ]
+               fi
+               if [ $? -eq 0 ]; then
+                       rm -f "$2"
+                       return 1
+               fi
+       fi
+       cat "$2" > "$1"
+       rm -f "$2"
+       return 0
+}
+
+newresolv="# Generated by resolvconf\n"
 changed=false
+
+# Try to ensure that config dirs exist
+if type config_mkdirs >/dev/null 2>&1; then
+       config_mkdirs "$pdnsd_resolv" "$pdnsd_conf"
+else
+       @PREFIX@/sbin/resolvconf -D "$pdnsd_resolv" "$pdnsd_conf"
+fi
+
+if [ -n "$pdnsd_resolv" ]; then
+       for n in $NAMESERVERS; do
+               newresolv="${newresolv}nameserver $n\n"
+       done
+fi
+
+# Only modify the configuration if it exists and we can write to it
+if [ -w "$pdnsd_conf" ]; then
+       cf="$pdnsd_conf.new"
+       newconf=
+
+       if [ -z "$pdnsd_resolv" ]; then
+               newconf="${newconf}server {$NL"
+               newconf="${newconf}     label=resolvconf;$NL"
+               if [ -n "$NAMESERVERS" ]; then
+                       newconf="${newconf}     ip="
+                       first=true
+                       for n in $NAMESERVERS; do
+                               if $first; then
+                                       first=false
+                               else
+                                       newconf="${newconf},"
+                               fi
+                               newconf="$newconf$n"
+                       done
+                       newconf="${newconf};$NL"
+               fi
+               newconf="${newconf}}$NL"
+       fi
+
+       for d in $DOMAINS; do
+               newconf="${newconf}server {$NL"
+               newconf="${newconf}     include=.${d%%:*}.;$NL"
+               newconf="${newconf}     policy=excluded;$NL"
+               newconf="${newconf}     ip="
+               ns="${d#*:}"
+               while [ -n "$ns" ]; do
+                       newconf="${newconf}${ns%%,*}"
+                       [ "$ns" = "${ns#*,}" ] && break
+                       ns="${ns#*,}"
+                       newconf="${newconf},"
+               done
+               newconf="${newconf};$NL}$NL"
+       done
+
+       rm -f "$cf"
+       remove_markers "$signature" "$signature_end" "$pdnsd_conf" > "$cf"
+       if [ -n "$newconf" ]; then
+               echo "$signature" >> "$cf"
+               printf %s "$newconf" >> "$cf"
+               echo "$signature_end" >> "$cf"
+       fi
+       if change_file "$pdnsd_conf" "$cf"; then
+               changed=true
+       fi      
+fi
+
 if [ -n "$pdnsd_resolv" ]; then
        if [ ! -f "$pdnsd_resolv" ] || \
-               [ "$(cat "$pdnsd_resolv")" != "$(printf "$newresolv")" ]
+               [ "$(cat "$pdnsd_resolv")" != "$(printf %s "$newresolv")" ]
        then
                changed=true
-               printf "$newresolv" >"$pdnsd_resolv"
+               printf %s "$newresolv" >"$pdnsd_resolv"
        fi
 fi