2 # Copyright (c) 2007-2020 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
43 while read -r line
; do
45 "$key"*) echo "${line##$key}";;
50 while read -r line
; do
52 "$key"*) echo "${line##$key}";;
61 while read -r line
; do
65 "$key"*|
"#"*|
" "*|
" "*|
"") found
=true
;;
69 $found ||
echo "$line"
73 local_nameservers
="127.* 0.0.0.0 255.255.255.255 ::1"
75 # Support original resolvconf configuration layout
76 # as well as the openresolv config file
77 if [ -f "$SYSCONFDIR"/resolvconf.conf
]; then
78 .
"$SYSCONFDIR"/resolvconf.conf
79 elif [ -d "$SYSCONFDIR"/resolvconf
]; then
80 SYSCONFDIR
="$SYSCONFDIR/resolvconf"
81 base
="$SYSCONFDIR/resolv.conf.d/base"
82 if [ -f "$base" ]; then
83 prepend_nameservers
="$(key_get_value "nameserver
" "$base")"
84 domain
="$(key_get_value "domain
" "$base")"
85 prepend_search
="$(key_get_value "search
" "$base")"
86 resolv_conf_options
="$(key_get_value "options
" "$base")"
87 resolv_conf_sortlist
="$(key_get_value "sortlist
" "$base")"
89 if [ -f "$SYSCONFDIR"/resolv.conf.d
/head ]; then
90 resolv_conf_head
="$(cat "${SYSCONFDIR}"/resolv.conf.d/head)"
92 if [ -f "$SYSCONFDIR"/resolv.conf.d
/tail ]; then
93 resolv_conf_tail
="$(cat "$SYSCONFDIR"/resolv.conf.d/tail)"
96 : ${resolv_conf:=/etc/resolv.conf}
97 : ${libc_service:=nscd}
98 : ${list_resolv:=@SBINDIR@/resolvconf -l}
99 if [ "${resolv_conf_head-x}" = x
] && [ -f "$SYSCONFDIR"/resolv.conf.
head ]
101 resolv_conf_head
="$(cat "${SYSCONFDIR}"/resolv.conf.head)"
103 if [ "${resolv_conf_tail-x}" = x
] && [ -f "$SYSCONFDIR"/resolv.conf.
tail ]
105 resolv_conf_tail
="$(cat "$SYSCONFDIR"/resolv.conf.tail)"
109 signature
="# Generated by resolvconf"
114 while [ -n "$1" ]; do
117 *) result
="$result $1";;
124 case "${resolv_conf_passthrough:-NO}" in
125 [Yy
][Ee
][Ss
]|
[Tt
][Rr
][Uu
][Ee
]|
[Oo
][Nn
]|
1)
128 for conf
in "$IFACEDIR"/*; do
129 if [ -z "$newest" ] ||
[ "$conf" -nt "$newest" ]; then
133 [ -z "$newest" ] && exit 0
134 newconf
="$(cat "$newest")$NL"
136 /dev
/null|
[Nn
][Uu
][Ll
][Ll
])
137 : ${resolv_conf_local_only:=NO}
138 if [ "$local_nameservers" = "127.* 0.0.0.0 255.255.255.255 ::1" ]; then
141 # Need to overwrite our variables.
142 eval "$(@SBINDIR@/resolvconf -V)"
146 [ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
149 case "${resolv_conf_passthrough:-NO}" in
150 [Yy
][Ee
][Ss
]|
[Tt
][Rr
][Uu
][Ee
]|
[Oo
][Nn
]|
1) ;;
153 newsearch
="$(uniqify $prepend_search $SEARCH $append_search)"
154 NS
="$LOCALNAMESERVERS $NAMESERVERS"
157 for n
in $
(uniqify
$prepend_nameservers $NS $append_nameservers); do
160 for l
in $local_nameservers; do
162 $l) islocal
=true
; gotlocal
=true
; break;;
166 case "${resolv_conf_local_only:-YES}" in
167 [Yy
][Ee
][Ss
]|
[Tt
][Rr
][Uu
][Ee
]|
[Oo
][Nn
]|
1)
168 $gotlocal && add
=false
;;
171 $add && newns
="$newns $n"
174 # Hold our new resolv.conf in a variable to save on temporary files
175 newconf
="$signature$NL"
176 if [ -n "$resolv_conf_head" ]; then
177 newconf
="$newconf$resolv_conf_head$NL"
180 [ -n "$domain" ] && newconf
="${newconf}domain $domain$NL"
181 if [ -n "$newsearch" ] && [ "$newsearch" != "$domain" ]; then
182 newconf
="${newconf}search $newsearch$NL"
185 newconf
="${newconf}nameserver $n$NL"
188 # Now add anything we don't care about such as sortlist and options
189 stuff
="$($list_resolv | keys_remove nameserver domain search)"
190 if [ -n "$stuff" ]; then
191 newconf
="$newconf$stuff$NL"
194 # Append any user defined ones
195 if [ -n "$resolv_conf_options" ]; then
196 newconf
="${newconf}options $resolv_conf_options$NL"
198 if [ -n "$resolv_conf_sortlist" ]; then
199 newconf
="${newconf}sortlist $resolv_conf_sortlist$NL"
202 if [ -n "$resolv_conf_tail" ]; then
203 newconf
="$newconf$resolv_conf_tail$NL"
208 # Check if the file has actually changed or not
209 if [ -e "$resolv_conf" ]; then
210 [ "$(cat "$resolv_conf")" = "$(printf %s "$newconf")" ] && exit 0
214 # If the old file does not have our signature, back it up.
215 # If the new file just has our signature, restore the backup.
217 if [ "$newconf" = "$signature$NL" ]; then
218 if [ -e "$resolv_conf.bak" ]; then
219 newconf
="$(cat "$resolv_conf.bak
")$NL"
221 elif [ -e "$resolv_conf" ]; then
222 read line
<"$resolv_conf"
223 if [ "$line" != "$signature" ]; then
224 cp "$resolv_conf" "$resolv_conf.bak"
229 # Create our resolv.conf now
230 (umask 022; printf %s
"$newconf" >"$resolv_conf")
231 if [ -n "$libc_restart" ]; then
233 elif [ -n "$RESTARTCMD" ]; then
234 set -- ${libc_service}
237 @SBINDIR@
/resolvconf
-r ${libc_service}
241 # Notify users of the resolver
242 for script in "$LIBEXECDIR"/libc.d
/*; do
243 if [ -f "$script" ]; then
244 if [ -x "$script" ]; then
249 retval
=$
(($retval + $?
))