2 # Copyright (c) 2007-2009 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" value= x= line=
45 "$key"*) echo "${line##$key}";;
52 "$key"*) echo "${line##$key}";;
59 # Support original resolvconf configuration layout
60 # as well as the openresolv config file
61 if [ -f "$SYSCONFDIR"/resolvconf.conf ]; then
62 . "$SYSCONFDIR"/resolvconf.conf
63 elif [ -d "$SYSCONFDIR"/resolvconf ]; then
64 SYSCONFDIR="$SYSCONFDIR/resolvconf/resolv.conf.d"
65 base="$SYSCONFDIR/resolv.conf.d/base"
66 if [ -f "$base" ]; then
67 name_servers="$(key_get_value "nameserver " "$base")"
68 search_domains="$(key_get_value "search " "$base")"
69 if [ -z "$search_domains" ]; then
70 search_domains="$(key_get_value "domain " "$base")"
72 resolv_conf_options="$(key_get_value "options " "$base")"
74 if [ -f "$SYSCONFDIR"/resolv.conf.d/head ]; then
75 resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.d/head)"
77 if [ -f "$SYSCONFDIR"/resolv.conf.d/tail ]; then
78 resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.d/tail)"
81 : ${resolv_conf:=/etc/resolv.conf}
82 : ${libc_service:=nscd}
83 : ${libc_restart:=@RESTARTCMD ${libc_service}@}
84 : ${list_resolv:=@PREFIX@/sbin/resolvconf -l}
85 if [ "${resolv_conf_head-x}" = x -a -f "$SYSCONFDIR"/resolv.conf.head ]; then
86 resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.head)"
88 if [ "${resolv_conf_tail-x}" = x -a -f "$SYSCONFDIR"/resolv.conf.tail ]; then
89 resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.tail)"
98 *) result="$result $1";;
105 case "${resolv_conf_passthrough:-NO}" in
106 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
108 for conf in "$IFACEDIR"/*; do
109 if [ -z "$newest" -o "$conf" -nt "$newest" ]; then
113 [ -z "$newest" ] && exit 0
114 newconf="$(cat "$newest")$NL"
117 [ -z "$RESOLVCONF" ] && eval "$(@PREFIX@/sbin/resolvconf -v)"
118 newsearch="$(uniqify $search_domains $SEARCH $search_domains_append)"
119 NS="$LOCALNAMESERVERS $NAMESERVERS"
120 newns="$(uniqify $name_servers $NS $name_servers_append)"
122 # Hold our new resolv.conf in a variable to save on temporary files
123 newconf="# Generated by resolvconf$NL"
124 if [ -n "$resolv_conf_head" ]; then
125 newconf="$newconf$resolv_conf_head$NL"
127 [ -n "$newsearch" ] && newconf="${newconf}search $newsearch$NL"
129 newconf="${newconf}nameserver $n$NL"
132 # Now get any configured options
133 opts="$resolv_conf_options${resolv_conf_options:+ }"
134 opts="$opts$($list_resolv | key_get_value "options ")"
135 if [ -n "$opts" ]; then
136 newconf="${newconf}options"
137 for opt in $(uniqify $opts); do
138 newconf="${newconf} $opt"
140 newconf="$newconf$NL"
143 if [ -n "$resolv_conf_tail" ]; then
144 newconf="$newconf$resolv_conf_tail$NL"
149 # Check if the file has actually changed or not
150 if [ -e "$resolv_conf" ]; then
151 [ "$(cat "$resolv_conf")" = "$(printf %s "$newconf")" ] && exit 0
154 # Create our resolv.conf now
155 (umask 022; echo "$newconf" >"$resolv_conf")
159 # Notify users of the resolver
160 for script in "$LIBEXECDIR"/libc.d/*; do
161 if [ -f "$script" ]; then
162 if [ -x "$script" ]; then
167 retval=$(($retval + $?))