Backup resolv.conf if it doesn't have our signature.
authorRoy Marples <roy@marples.name>
Sat, 25 Oct 2014 00:29:57 +0000 (00:29 +0000)
committerRoy Marples <roy@marples.name>
Sat, 25 Oct 2014 00:29:57 +0000 (00:29 +0000)
Restore it when our new config only has our signature.

libc.in
resolvconf.8.in

diff --git a/libc.in b/libc.in
index 09c81baa49f2ab0db2ca35f6f7c41292cd6e2e44..25295a96d0b2ad98110bcf6f286ce4d261f6ec2a 100644 (file)
--- a/libc.in
+++ b/libc.in
@@ -106,6 +106,9 @@ if [ "${resolv_conf_tail-x}" = x -a -f "$SYSCONFDIR"/resolv.conf.tail ]; then
        resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.tail)"
 fi
 
+backup=true
+signature="# Generated by resolvconf"
 uniqify()
 {
        local result=
@@ -121,6 +124,7 @@ uniqify()
 
 case "${resolv_conf_passthrough:-NO}" in
 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
+       backup=false
        newest=
        for conf in "$IFACEDIR"/*; do
                if [ -z "$newest" -o "$conf" -nt "$newest" ]; then
@@ -169,7 +173,7 @@ case "${resolv_conf_passthrough:-NO}" in
        done
 
        # Hold our new resolv.conf in a variable to save on temporary files
-       newconf="# Generated by resolvconf$NL"
+       newconf="$signature$NL"
        if [ -n "$resolv_conf_head" ]; then
                newconf="$newconf$resolv_conf_head$NL"
        fi
@@ -207,6 +211,22 @@ if [ -e "$resolv_conf" ]; then
        [ "$(cat "$resolv_conf")" = "$(printf %s "$newconf")" ] && exit 0
 fi
 
+# Change is good.
+# If the old file does not have our signature, back it up.
+# If the new file just has our signature, restore the backup.
+if $backup; then
+       if [ "$newconf" = "$signature$NL" ]; then
+               if [ -e "$resolv_conf.bak" ]; then
+                       newconf="$(cat "$resolv_conf.bak")"
+               fi
+       elif [ -e "$resolv_conf" ]; then
+               read line <"$resolv_conf"
+               if [ "$line" != "$signature" ]; then
+                       cp "$resolv_conf" "$resolv_conf.bak"
+               fi
+       fi
+fi
+
 # Create our resolv.conf now
 (umask 022; printf %s "$newconf" >"$resolv_conf")
 eval $libc_restart
index cf7000fee392e67f7efc50ced8a26e94a0dbe900..5678df4f79f3bb0656e01ee2eea91d02cd77ca7c 100644 (file)
@@ -220,6 +220,8 @@ as private.
 .El
 .Sh FILES
 .Bl -ohang
+.It Pa /etc/resolv.conf.bak
+Backup file of the original resolv.conf.
 .It Pa @SYSCONFDIR@/resolvconf.conf
 Configuration file for
 .Nm .