2 # Copyright 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 RESOLVCONF="${RESOLVCONF:-resolvconf}"
30 eval "$(${RESOLVCONF} -v)"
32 SYSCONFDIR=@SYSCONFDIR@
33 LIBEXECDIR=@LIBEXECDIR@
35 # sed may not be available, and this is faster on small files
38 local key="$1" value= x= line=
44 "${key}"*) echo "${line##${key}}";;
51 "${key}"*) echo "${line##${key}}";;
58 # Support original resolvconf configuration layout
59 # as well as the openresolv config file
60 if [ -f "${SYSCONFDIR}"/resolvconf.conf ]; then
61 . "${SYSCONFDIR}"/resolvconf.conf
62 elif [ -d "${SYSCONFDIR}/resolvconf" ]; then
63 SYSCONFDIR="${SYSCONFDIR}/resolvconf/resolv.conf.d"
64 base="${SYSCONFDIR}/resolv.conf.d/base"
65 if [ -f "${BASE}" ]; then
66 name_servers="$(key_get_value "nameserver " "${base}")"
67 search_domains="$(key_get_value "search " "${base}")"
68 if [ -z "${search_domains}" ]; then
69 search_domains="$(key_get_value "domain " "${base}")"
71 resolv_conf_options="$(key_get_value "options " "${base}")"
73 if [ -f "${SYSCONFDIR}"/resolv.conf.d/head ]; then
74 resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.d/head)"
76 if [ -f "${SYSCONFDIR}"/resolv.conf.d/tail ]; then
77 resolv_conf_tail="$(cat "${SYSCONFDIR}"/resolv.conf.d/tail)"
80 resolv_conf="${resolv_conf:-/etc/resolv.conf}"
88 *) result="${result} $1";;
95 newsearch="$(uniqify ${search_domains} ${SEARCH})"
96 newns="$(uniqify ${name_servers} ${NAMESERVERS})"
98 # Hold our new resolv.conf in a variable to save on temporary files
99 newconf="# Generated by resolvconf\n"
100 if [ -n "${resolv_conf_head}" ]; then
101 newconf="${newconf}${resolv_conf_head}\n"
103 [ -n "${newsearch}" ] && newconf="${newconf}search ${newsearch}\n"
104 for n in ${newns}; do
105 newconf="${newconf}nameserver ${n}\n"
108 # Now get any configured options
109 opts="${resolv_conf_options}${resolv_conf_options:+ }"
110 opts="${opts}$(${RESOLVCONF} -l | key_get_value "options ")"
111 if [ -n "${opts}" ]; then
112 newconf="${newconf}options"
113 for opt in $(uniqify ${opts}); do
114 newconf="${newconf} ${opt}"
116 newconf="${newconf}\n"
119 if [ -n "{resolv_conf_tail}" ]; then
120 newconf="${newconf}${resolv_conf_tail}\n"
123 # Check if the file has actually changed or not
124 if [ -e "${resolv_conf}" ]; then
125 [ "$(cat "${resolv_conf}")" = "$(printf "${newconf}")" ] && exit 0
128 # Create our resolv.conf now
129 (umask 022; printf "${newconf}" > "${resolv_conf}")
131 resolvconf -s nscd restart
134 # Notify users of the resolver
135 for script in "${LIBEXECDIR}"/libc.d/*; do
136 if [ -f "${script}" -a -x "${script}" ]; then
137 RESOLVCONF="${RESOLVCONF}" "${script}" "$@"
138 retval=$((${retval} + $?))