2 # Copyright 2006 Gentoo Foundation
3 # Copyright 2007 Roy Marples
4 # Distributed under the terms of the GNU General Public License v2
6 # libc resolv.conf updater
8 # Written by Roy Marples (uberlord@gentoo.org)
9 # Heavily based on Debian resolvconf by Thomas Hood
12 RESOLVCONF="${PREFIX}"/etc/resolvconf
13 RESOLVCONFS="$(resolvconf -l)"
14 BASE="${RESOLVCONF}/resolv.conf.d/base"
21 *) result="${result} $1" ;;
29 if [ -e "${BASE}" ]; then
30 OUR_NS="$(sed -n -e 's/^[[:space:]]*nameserver[[:space:]]*//p' "${BASE}")"
34 $(echo "${RESOLVCONFS}" \
35 | sed -n -e 's/^[[:space:]]*nameserver[[:space:]]*//p') \
38 # libc only allows for 3 nameservers
39 # truncate after 127 as well
43 for N in ${OUR_NS}; do
46 [ "${i}" = "3" ] && break
48 127.*) LOCALH=true; break ;;
53 # If we have a local nameserver then assume they are intelligent enough
54 # to be forwarding domain requests to the correct nameserver and not search
55 # ones. This means we prefer search then domain, otherwise, we use them in
56 # the order given to us.
59 if [ -e "${BASE}" ]; then
60 OUR_SEARCH="$(sed -n -e 's/^[[:space:]]*search[[:space:]]*//p' "${BASE}")"
62 OUR_SEARCH="${OUR_SEARCH} $(echo "${RESOLVCONFS}" \
63 | sed -n 's/^[[:space:]]*search[[:space:]]*//p')"
64 if [ -e "${BASE}" ]; then
65 OUR_SEARCH="${OUR_SEARCH} $(sed -n -e 's/^[[:space:]]*domain[[:space:]]*//p' "${BASE}")"
67 OUR_SEARCH="${OUR_SEARCH} $( echo "${RESOLVCONFS}" \
68 | sed -n -e 's/^[[:space:]]*domain[[:space:]]*//p')"
70 if [ -e "${BASE}" ]; then
71 OUR_SEARCH="$(sed -n -e 's/^[[:space:]]*search[[:space:]]*//p' \
72 -e 's/^[[:space:]]*domain[[:space:]]*//p' "${BASE}")"
74 OUR_SEARCH="${OUR_SEARCH} $(echo "${RESOLVCONFS}" \
75 | sed -n -e 's/^[[:space:]]*search[[:space:]]*//p' \
76 -e 's/^[[:space:]]*domain[[:space:]]*//p')"
79 # libc only allows for 6 search domains
82 for S in $(uniqify ${OUR_SEARCH}); do
84 SEARCH="${SEARCH} ${S}"
85 [ "${i}" = "6" ] && break
87 [ -n "${SEARCH}" ] && SEARCH="search${SEARCH}"
89 # Hold our new resolv.conf in a variable to save on temporary files
90 NEWCONF="# Generated by resolvconf\n"
91 [ -e "${RESOLVCONF}"/resolv.conf.d/head ] \
92 && NEWCONF="${NEWCONF}$(cat "${RESOLVCONF}"/resolv.conf.d/head)\n"
93 [ -n "${SEARCH}" ] && NEWCONF="${NEWCONF}${SEARCH}\n"
95 NEWCONF="${NEWCONF}nameserver ${N}\n"
98 # Now dump everything else from our resolvs
99 if [ -e "${BASE}" ]; then
100 NEWCONF="${NEWCONF}$(sed -e '/^[[:space:]]*$/d' \
101 -e '/^[[:space:]]*nameserver[[:space:]]*.*/d' \
102 -e '/^[[:space:]]*search[[:space:]]*.*/d' \
103 -e '/^[[:space:]]*domain[[:space:]]*.*/d' \
107 # We don't know we're using GNU sed, so we do it like this
108 NEWCONF="${NEWCONF}$(echo "${RESOLVCONFS}" | sed -e '/^[[:space:]]*$/d' \
109 -e '/^[[:space:]]*#/d' \
110 -e '/^[[:space:]]*nameserver[[:space:]]*.*/d' \
111 -e '/^[[:space:]]*search[[:space:]]*.*/d' \
112 -e '/^[[:space:]]*domain[[:space:]]*.*/d' \
114 [ -e "${RESOLVCONF}"/resolv.conf.d/tail ] \
115 && NEWCONF="${NEWCONF}$(cat "${RESOLVCONF}"/resolv.conf.d/tail)"
117 # Check if the file has actually changed or not
118 if [ -e "${RESOLVCONF}"/run/resolv.conf ]; then
119 [ "$(cat "${RESOLVCONF}"/run/resolv.conf)" = "$(printf "${NEWCONF}")" ] && exit 0
122 # Create our resolv.conf now
123 printf "${NEWCONF}" > "${RESOLVCONF}"/run/resolv.conf
125 # Restart nscd if it's running
126 if [ -x /etc/init.d/nscd ]; then
127 /etc/init.d/nscd --nodeps --quiet conditionalrestart
129 if [ -x /etc/rc.d/nscd ]; then
130 /etc/rc.d/nscd restart
133 # Notify users of the resolver
134 for x in "${REVOLVCONF}"/update-libc.d/*; do
135 [ -e "${x}" ] && "${x}" "$@"