Release openresolv-3.5.4
[openresolv] / pdnsd.in
index 72a20d3dac735c49ab5d1b3399162c11df65eb31..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 $pdnsd_conf}
 signature="# Generated by resolvconf"
@@ -46,7 +48,7 @@ remove_markers()
                sed "/^$m1/,/^$m2/d" $@
        else
                for x; do
-                       while read line; do
+                       while read -r line; do
                                case "$line" in
                                "$m1"*) in_marker=1;;
                                "$m2"*) in_marker=0;;
@@ -83,18 +85,48 @@ change_file()
 newresolv="# Generated by resolvconf\n"
 changed=false
 
-for n in $NAMESERVERS; do
-       newresolv="${newresolv}nameserver $n\n"
-done
+# 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
 
-if [ -n "$pdnsd_conf" ]; then
+# 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 {\n"
-               newconf="${newconf}\tinclude=\"${d%%:*}\";\n"
-               newconf="${newconf}\tpolicy=excluded;\n"
-               newconf="${newconf}\tip="
+               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%%,*}"
@@ -102,14 +134,14 @@ if [ -n "$pdnsd_conf" ]; then
                        ns="${ns#*,}"
                        newconf="${newconf},"
                done
-               newconf="${newconf};\n}\n"
+               newconf="${newconf};$NL}$NL"
        done
 
        rm -f "$cf"
        remove_markers "$signature" "$signature_end" "$pdnsd_conf" > "$cf"
        if [ -n "$newconf" ]; then
                echo "$signature" >> "$cf"
-               printf "$newconf" >> "$cf"
+               printf %s "$newconf" >> "$cf"
                echo "$signature_end" >> "$cf"
        fi
        if change_file "$pdnsd_conf" "$cf"; then
@@ -119,10 +151,10 @@ 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