usr.sbin/makefs: Add -o c|C option to specify comp|check type
[dragonfly.git] / contrib / openresolv / pdnsd.in
blob149808c096685fe8dfcc4a0f4576268aadad1705
1 #!/bin/sh
2 # Copyright (c) 2010-2023 Roy Marples
3 # All rights reserved
5 # pdnsd subscriber for resolvconf
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.
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.
29 [ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
30 . "@SYSCONFDIR@/resolvconf.conf" || exit 1
31 [ -z "${pdnsd_conf}${pdnsd_resolv}" ] && exit 0
32 [ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
33 NL="
36 : ${pdnsd_restart:=pdnsd-ctl config $pdnsd_conf}
37 signature="# Generated by resolvconf"
38 signature_end="# End of resolvconf"
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()
44 m1="$1"
45 m2="$2"
46 in_marker=0
48 shift; shift
49 if command -v sed >/dev/null 2>&1; then
50 sed "/^$m1/,/^$m2/d" $@
51 else
52 for x do
53 while read line; do
54 case "$line" in
55 "$m1"*) in_marker=1;;
56 "$m2"*) in_marker=0;;
57 *) [ $in_marker = 0 ] && echo "$line";;
58 esac
59 done < "$x"
60 done
64 # Compare two files
65 # If different, replace first with second otherwise remove second
66 change_file()
68 if [ -e "$1" ]; then
69 if command -v cmp >/dev/null 2>&1; then
70 cmp -s "$1" "$2"
71 elif command -v diff >/dev/null 2>&1; then
72 diff -q "$1" "$2" >/dev/null
73 else
74 # Hopefully we're only working on small text files ...
75 [ "$(cat "$1")" = "$(cat "$2")" ]
77 if [ $? -eq 0 ]; then
78 rm -f "$2"
79 return 1
82 cat "$2" > "$1"
83 rm -f "$2"
84 return 0
87 newresolv="# Generated by resolvconf$NL"
88 changed=false
90 # Try to ensure that config dirs exist
91 if command -v config_mkdirs >/dev/null 2>&1; then
92 config_mkdirs "$pdnsd_resolv" "$pdnsd_conf"
93 else
94 @SBINDIR@/resolvconf -D "$pdnsd_resolv" "$pdnsd_conf"
97 if [ -n "$pdnsd_resolv" ]; then
98 for n in $NAMESERVERS; do
99 newresolv="${newresolv}nameserver $n$NL"
100 done
103 # Only modify the configuration if it exists and we can write to it
104 if [ -w "$pdnsd_conf" ]; then
105 cf="$pdnsd_conf.new"
106 newconf=
108 if [ -z "$pdnsd_resolv" ]; then
109 newconf="${newconf}server {$NL"
110 newconf="${newconf} label=resolvconf;$NL"
111 if [ -n "$NAMESERVERS" ]; then
112 newconf="${newconf} ip="
113 first=true
114 for n in $NAMESERVERS; do
115 if $first; then
116 first=false
117 else
118 newconf="${newconf},"
120 newconf="$newconf$n"
121 done
122 newconf="${newconf};$NL"
124 newconf="${newconf}}$NL"
127 for d in $DOMAINS; do
128 newconf="${newconf}server {$NL"
129 newconf="${newconf} include=.${d%%:*}.;$NL"
130 newconf="${newconf} policy=excluded;$NL"
131 newconf="${newconf} ip="
132 ns="${d#*:}"
133 while [ -n "$ns" ]; do
134 newconf="${newconf}${ns%%,*}"
135 [ "$ns" = "${ns#*,}" ] && break
136 ns="${ns#*,}"
137 newconf="${newconf},"
138 done
139 newconf="${newconf};$NL}$NL"
140 done
142 rm -f "$cf"
143 remove_markers "$signature" "$signature_end" "$pdnsd_conf" > "$cf"
144 if [ -n "$newconf" ]; then
145 echo "$signature" >> "$cf"
146 printf %s "$newconf" >> "$cf"
147 echo "$signature_end" >> "$cf"
149 if change_file "$pdnsd_conf" "$cf"; then
150 changed=true
154 if [ -n "$pdnsd_resolv" ]; then
155 if [ ! -f "$pdnsd_resolv" ] || \
156 [ "$(cat "$pdnsd_resolv")" != "$(printf %s "$newresolv")" ]
157 then
158 changed=true
159 printf %s "$newresolv" >"$pdnsd_resolv"
163 if $changed; then
164 eval $pdnsd_restart