2 # Copyright (c) 2007-2014 Roy Marples
5 # libc subscriber for resolvconf
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
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 SYSCONFDIR=@SYSCONFDIR@
30 LIBEXECDIR=@LIBEXECDIR@
32 IFACEDIR="$VARDIR/interfaces"
36 # sed may not be available, and this is faster on small files
39 local key="$1" x= line=
43 while read -r line; do
45 "$key"*) echo "${line##$key}";;
50 while read -r line; do
52 "$key"*) echo "${line##$key}";;
61 local key x line found
63 while read -r line; do
67 "$key"*|"#"*|" "*|" "*|"") found=true;;
71 $found || echo "$line"
75 local_nameservers="127.* 0.0.0.0 255.255.255.255 ::1"
77 # Support original resolvconf configuration layout
78 # as well as the openresolv config file
79 if [ -f "$SYSCONFDIR"/resolvconf.conf ]; then
80 . "$SYSCONFDIR"/resolvconf.conf
81 elif [ -d "$SYSCONFDIR"/resolvconf ]; then
82 SYSCONFDIR="$SYSCONFDIR/resolvconf/resolv.conf.d"
83 base="$SYSCONFDIR/resolv.conf.d/base"
84 if [ -f "$base" ]; then
85 prepend_nameservers="$(key_get_value "nameserver " "$base")"
86 domain="$(key_get_value "domain " "$base")"
87 prepend_search="$(key_get_value "search " "$base")"
88 resolv_conf_options="$(key_get_value "options " "$base")"
89 resolv_conf_sortlist="$(key_get_value "sortlist " "$base")"
91 if [ -f "$SYSCONFDIR"/resolv.conf.d/head ]; then
92 resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.d/head)"
94 if [ -f "$SYSCONFDIR"/resolv.conf.d/tail ]; then
95 resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.d/tail)"
98 : ${resolv_conf:=/etc/resolv.conf}
99 : ${libc_service:=nscd}
100 : ${libc_restart:=@RESTARTCMD ${libc_service}@}
101 : ${list_resolv:=@SBINDIR@/resolvconf -l}
102 if [ "${resolv_conf_head-x}" = x -a -f "$SYSCONFDIR"/resolv.conf.head ]; then
103 resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.head)"
105 if [ "${resolv_conf_tail-x}" = x -a -f "$SYSCONFDIR"/resolv.conf.tail ]; then
106 resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.tail)"
110 signature="# Generated by resolvconf"
115 while [ -n "$1" ]; do
118 *) result="$result $1";;
125 case "${resolv_conf_passthrough:-NO}" in
126 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
129 for conf in "$IFACEDIR"/*; do
130 if [ -z "$newest" -o "$conf" -nt "$newest" ]; then
134 [ -z "$newest" ] && exit 0
135 newconf="$(cat "$newest")$NL"
137 /dev/null|[Nn][Uu][Ll][Ll])
138 : ${resolv_conf_local_only:=NO}
139 if [ "$local_nameservers" = "127.* 0.0.0.0 255.255.255.255 ::1" ]; then
142 # Need to overwrite our variables.
143 eval "$(@SBINDIR@/resolvconf -V)"
147 [ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
150 case "${resolv_conf_passthrough:-NO}" in
151 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) ;;
154 newsearch="$(uniqify $prepend_search $SEARCH $append_search)"
155 NS="$LOCALNAMESERVERS $NAMESERVERS"
158 for n in $(uniqify $prepend_nameservers $NS $append_nameservers); do
161 for l in $local_nameservers; do
163 $l) islocal=true; gotlocal=true; break;;
167 case "${resolv_conf_local_only:-YES}" in
168 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
169 $gotlocal && add=false;;
172 $add && newns="$newns $n"
175 # Hold our new resolv.conf in a variable to save on temporary files
176 newconf="$signature$NL"
177 if [ -n "$resolv_conf_head" ]; then
178 newconf="$newconf$resolv_conf_head$NL"
181 [ -n "$domain" ] && newconf="${newconf}domain $domain$NL"
182 if [ -n "$newsearch" -a "$newsearch" != "$domain" ]; then
183 newconf="${newconf}search $newsearch$NL"
186 newconf="${newconf}nameserver $n$NL"
189 # Now add anything we don't care about such as sortlist and options
190 stuff="$($list_resolv | keys_remove nameserver domain search)"
191 if [ -n "$stuff" ]; then
192 newconf="$newconf$stuff$NL"
195 # Append any user defined ones
196 if [ -n "$resolv_conf_options" ]; then
197 newconf="${newconf}options $resolv_conf_options$NL"
199 if [ -n "$resolv_conf_sortlist" ]; then
200 newconf="${newconf}sortlist $resolv_conf_sortlist$NL"
203 if [ -n "$resolv_conf_tail" ]; then
204 newconf="$newconf$resolv_conf_tail$NL"
209 # Check if the file has actually changed or not
210 if [ -e "$resolv_conf" ]; then
211 [ "$(cat "$resolv_conf")" = "$(printf %s "$newconf")" ] && exit 0
215 # If the old file does not have our signature, back it up.
216 # If the new file just has our signature, restore the backup.
218 if [ "$newconf" = "$signature$NL" ]; then
219 if [ -e "$resolv_conf.bak" ]; then
220 newconf="$(cat "$resolv_conf.bak")"
222 elif [ -e "$resolv_conf" ]; then
223 read line <"$resolv_conf"
224 if [ "$line" != "$signature" ]; then
225 cp "$resolv_conf" "$resolv_conf.bak"
230 # Create our resolv.conf now
231 (umask 022; echo "$newconf" >"$resolv_conf")
235 # Notify users of the resolver
236 for script in "$LIBEXECDIR"/libc.d/*; do
237 if [ -f "$script" ]; then
238 if [ -x "$script" ]; then
243 retval=$(($retval + $?))