Update copyright.
[openresolv] / pdnsd.in
index ff3b9b23ab5a975c579b2a51474c10f6782c73a6..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
@@ -30,6 +30,8 @@
 . "@SYSCONFDIR@/resolvconf.conf" || exit 1
 [ -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,21 +85,29 @@ change_file()
 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
 
-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 {\n"
-               newconf="${newconf}\tlabel=resolvconf;\n"
+               newconf="${newconf}server {$NL"
+               newconf="${newconf}     label=resolvconf;$NL"
                if [ -n "$NAMESERVERS" ]; then
-                       newconf="${newconf}\tip="
+                       newconf="${newconf}     ip="
                        first=true
                        for n in $NAMESERVERS; do
                                if $first; then
@@ -107,16 +117,16 @@ if [ -n "$pdnsd_conf" ]; then
                                fi
                                newconf="$newconf$n"
                        done
-                       newconf="${newconf};\n"
+                       newconf="${newconf};$NL"
                fi
-               newconf="${newconf}}\n"
+               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%%,*}"
@@ -124,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
@@ -141,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