Fix pdnsd for printf(1)
[openresolv] / pdnsd.in
1 #!/bin/sh
2 # Copyright (c) 2010-2011 Roy Marples
3 # All rights reserved
4
5 # pdnsd subscriber for resolvconf
6
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #     * Redistributions of source code must retain the above copyright
11 #       notice, this list of conditions and the following disclaimer.
12 #     * Redistributions in binary form must reproduce the above
13 #       copyright notice, this list of conditions and the following
14 #       disclaimer in the documentation and/or other materials provided
15 #       with the distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 [ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
30 . "@SYSCONFDIR@/resolvconf.conf" || exit 1
31 [ -z "$pdnsd_conf" -a -z "$pdnsd_resolv" ] && exit 0
32 [ -z "$RESOLVCONF" ] && eval "$(@PREFIX@/sbin/resolvconf -v)"
33 NL="
34 "
35
36 : ${pdnsd_restart:=pdnsd-ctl config $pdnsd_conf}
37 signature="# Generated by resolvconf"
38 signature_end="# End of resolvconf"
39
40 # We normally use sed to remove markers from a configuration file
41 # but sed may not always be available at the time.
42 remove_markers()
43 {
44         local m1="$1" m2="$2" x= line= in_marker=0
45
46         shift; shift
47         if type sed >/dev/null 2>&1; then
48                 sed "/^$m1/,/^$m2/d" $@
49         else
50                 for x; do
51                         while read line; do
52                                 case "$line" in
53                                 "$m1"*) in_marker=1;;
54                                 "$m2"*) in_marker=0;;
55                                 *) [ $in_marker = 0 ] && echo "$line";;
56                                 esac
57                         done < "$x"
58                 done
59         fi
60 }
61
62 # Compare two files
63 # If different, replace first with second otherwise remove second
64 change_file()
65 {
66         if [ -e "$1" ]; then
67                 if type cmp >/dev/null 2>&1; then
68                         cmp -s "$1" "$2"
69                 elif type diff >/dev/null 2>&1; then
70                         diff -q "$1" "$2" >/dev/null
71                 else
72                         # Hopefully we're only working on small text files ...
73                         [ "$(cat "$1")" = "$(cat "$2")" ]
74                 fi
75                 if [ $? -eq 0 ]; then
76                         rm -f "$2"
77                         return 1
78                 fi
79         fi
80         cat "$2" > "$1"
81         rm -f "$2"
82         return 0
83 }
84
85 newresolv="# Generated by resolvconf\n"
86 changed=false
87
88 if [ -n "$pdnsd_resolv" ]; then
89         for n in $NAMESERVERS; do
90                 newresolv="${newresolv}nameserver $n\n"
91         done
92 fi
93
94 if [ -n "$pdnsd_conf" ]; then
95         cf="$pdnsd_conf.new"
96         newconf=
97
98         if [ -z "$pdnsd_resolv" ]; then
99                 newconf="${newconf}server {$NL"
100                 newconf="${newconf}     label=resolvconf;$NL"
101                 if [ -n "$NAMESERVERS" ]; then
102                         newconf="${newconf}     ip="
103                         first=true
104                         for n in $NAMESERVERS; do
105                                 if $first; then
106                                         first=false
107                                 else
108                                         newconf="${newconf},"
109                                 fi
110                                 newconf="$newconf$n"
111                         done
112                         newconf="${newconf};$NL"
113                 fi
114                 newconf="${newconf}}$NL"
115         fi
116
117         for d in $DOMAINS; do
118                 newconf="${newconf}server {$NL"
119                 newconf="${newconf}     include=.${d%%:*}.;$NL"
120                 newconf="${newconf}     policy=excluded;$NL"
121                 newconf="${newconf}     ip="
122                 ns="${d#*:}"
123                 while [ -n "$ns" ]; do
124                         newconf="${newconf}${ns%%,*}"
125                         [ "$ns" = "${ns#*,}" ] && break
126                         ns="${ns#*,}"
127                         newconf="${newconf},"
128                 done
129                 newconf="${newconf};$NL}$NL"
130         done
131
132         rm -f "$cf"
133         remove_markers "$signature" "$signature_end" "$pdnsd_conf" > "$cf"
134         if [ -n "$newconf" ]; then
135                 echo "$signature" >> "$cf"
136                 printf %s "$newconf" >> "$cf"
137                 echo "$signature_end" >> "$cf"
138         fi
139         if change_file "$pdnsd_conf" "$cf"; then
140                 changed=true
141         fi      
142 fi
143
144 if [ -n "$pdnsd_resolv" ]; then
145         if [ ! -f "$pdnsd_resolv" ] || \
146                 [ "$(cat "$pdnsd_resolv")" != "$(printf %s "$newresolv")" ]
147         then
148                 changed=true
149                 printf %s "$newresolv" >"$pdnsd_resolv"
150         fi
151 fi
152
153 if $changed; then
154         eval $pdnsd_restart
155 fi