Remove vim comments and fix formatting a little.
[openresolv] / resolvconf.in
1 #!/bin/sh
2 # Copyright 2007-2008 Roy Marples
3 # All rights reserved
4
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #     * Redistributions of source code must retain the above copyright
9 #       notice, this list of conditions and the following disclaimer.
10 #     * Redistributions in binary form must reproduce the above
11 #       copyright notice, this list of conditions and the following
12 #       disclaimer in the documentation and/or other materials provided
13 #       with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 argv0="$0"
28
29 PREFIX=
30 RESOLVCONF="${PREFIX}"/etc/resolvconf
31 UPDATED="${RESOLVCONF}"/update.d
32 VARDIR="${RESOLVCONF}"/run
33 IFACEDIR="${VARDIR}/interfaces"
34
35 error_exit() {
36         echo "$*" >&2
37         exit 1
38 }
39
40 usage() {
41         cat <<-EOF
42         Usage: ${argv0##*/} [options]
43
44         Inform the system about any DNS updates.
45
46         Options:
47           -a \$INTERFACE    Add DNS information to the specified interface
48                            (DNS supplied via stdin in resolv.conf format)
49           -d \$INTERFACE    Delete DNS information from the specified interface
50           -u               Run updates from our current DNS information
51           -l [\$PATTERN]    Show DNS information, optionally from interfaces
52                            that match the specified pattern
53           -i [\$PATTERN]    Show interfaces that have supplied DNS information
54                    optionally from interfaces that match the specified
55                    pattern
56           -v [\$PATTERN]    echo NEWDOMAIN, NEWSEARCH and NEWNS variables to
57                            the console
58           -s \$SVC \$CMD     Do \$CMD for the system service \$SVC
59           -h               Show this help cruft
60         EOF
61         [ -z "$@" ] && exit 0
62         echo
63         error_exit "$*"
64 }
65
66 echo_resolv() {
67         [ -n "$1" -a -e "${IFACEDIR}/$1" ] || return 1
68         echo "# resolv.conf for interface $1"
69         cat "${IFACEDIR}/$1"
70         echo
71 }
72
73 uniqify() {
74     local result=
75     while [ -n "$1" ]; do
76                 case " ${result} " in
77                         *" $1 "*);;
78                         *) result="${result} $1";;
79                 esac
80                 shift
81         done
82     echo "${result# *}"
83 }
84
85 if [ -n "$1" ]; then
86         CMD="$1"
87         shift
88 fi
89
90 # We do our service restarting here so that our subscribers don't have to know
91 # about the OS's init system.
92 if [ "x${CMD}" = "x-s" ]; then
93         if [ -n "$1" ]; then
94                 SERVICE="$1"
95                 shift
96         fi
97         [ -z "${SERVICE}" ] && usage "Service not specified"
98         if [ -n "$1" ]; then
99                 ACTION="$1"
100                 shift
101         fi
102         [ -z "${ACTION}" ] && usage "Action not specified"
103
104         # If restarting check if service is running or not if we can
105         if [ "x${ACTION}" = "xrestart" ]; then
106                 if [ -s /var/run/"${SERVICE}".pid ]; then
107                         kill -0 $(cat /var/run/"${SERVICE}".pid) 2>/dev/null
108                 elif [ -s /var/run/"${SERVICE}"/"${SERVICE}".pid ]; then
109                         kill -0 $(cat /var/run/"${SERVICE}".pid) 2>/dev/null
110                 elif [ -s /var/run/"${SERVICE}"/pid ]; then
111                         kill -0 $(cat /var/run/"${SERVICE}"/pid) 2>/dev/null
112                 else
113                         false
114                 fi
115                 # Service not running, so don't restart
116                 [ $? != 0 ] && exit 0
117         fi      
118         if [ -x /sbin/service ]; then
119                 service "${SERVICE}" "${ACTION}" "$@" 
120         elif [ -x /etc/init.d/"${SERVICE}" -a -x /sbin/runscript ]; then
121                 if [ "x${ACTION}" = "xrestart" ]; then
122                         /etc/init.d/"${SERVICE}" --quiet --nodeps conditionalrestart "$@"
123                 else
124                         /etc/init.d/"${SERVICE}" --quiet --nodeps "${ACTION}" "$@"
125                 fi
126         elif [ -x /etc/init.d/"${SERVICE}" ]; then
127                 /etc/init.d/"${SERVICE}" "${ACTION}" "$@"
128         elif [ -x /etc/rc.d/"${SERVICE}" ]; then
129                 /etc/rc.d/"${SERVICE}" "${ACTION}" "$@" 
130         elif [ -x /etc/rc.d/rc."${SERVICE}" ]; then
131                 /etc/rc.d/rc."${SERVICE}" "${ACTION}" "$@"
132         else
133                 error_exit "Don't know how to interact with services on this platform"
134         fi
135         exit $?
136 fi
137
138 if [ -n "$1" ]; then
139         IFACE="$1"
140         shift
141 fi
142
143 # -l lists our resolv files, optionally for a specific interface
144 if [ "x${CMD}" = "x-l" -o "x${CMD}" = "x-i" ]; then
145         [ -d "${IFACEDIR}" ] || exit 0
146         
147         # If we have an interface ordering list, then use that.
148         # It works by just using pathname expansion in the interface directory.
149         if [ -n "${IFACE}" ]; then
150                 LIST="${IFACE} $@"
151         elif [ -r "${RESOLVCONF}"/interface-order ]; then
152                 LIST="$(cat "${RESOLVCONF}"/interface-order)"
153         fi
154
155         # If we don't have a list then prefer lo, tunnels, ppp
156         # and then anything else.
157         if [ -z "${LIST}" ]; then
158                 LIST="lo lo[0-9]* tap[0-9]* tun[0-9]* vpn vpn[0-9]* ppp[0-9]* ippp[0-9]* *"
159         fi
160
161         cd "${IFACEDIR}"
162         for IFACE in $(uniqify ${LIST}); do
163                 # Only list interfaces which we really have
164                 [ -e "${IFACE}" ] || continue
165                 
166                 if [ "x${CMD}" = "x-i" ]; then
167                         printf "${IFACE} "
168                 else
169                         echo_resolv "${IFACE}"
170                 fi
171         done
172         [ "x${CMD}" = "x-i" ] && echo
173         exit 0
174 fi
175
176 if [ "x${CMD}" = "x-v" ]; then
177         NS=
178         DOMAIN=
179         SEARCH=
180         NEWSEARCH=
181         NEWNS=
182         NEWDOMAIN=
183         LINES="$("${argv0}" -l "${IFACE}" |
184                 sed -e "s/'/'\\\\''/g" -e "s/^/'/g" -e "s/$/'/g")"
185         eval set -- ${LINES}
186         for LINE in "$@"; do
187                 case "${LINE}" in
188                         "nameserver "*)
189                                 case "${LINE#* }" in
190                                         127.*) continue;;
191                                 esac
192                                 NS="${NS}${LINE#* } "
193                                 ;;
194                         "domain "*)
195                                 [ -z "${SEARCH}" ] && DOMAIN="${LINE#* }"
196                                 ;;
197                         "search "*)
198                                 SEARCH="${LINE#* }"
199                                 DOMAIN=
200                                 ;;
201                         *)
202                                 if [ -z "${LINE}" ]; then
203                                         for N in ${NS}; do
204                                                 if [ -n "${DOMAIN}" ]; then
205                                                         NEWDOMAIN="${NEWDOMAIN} ${DOMAIN},${N}"
206                                                 elif [ -n "${SEARCH}" ]; then
207                                                         for S in ${SEARCH}; do
208                                                                 NEWSEARCH="${NEWSEARCH} ${S},${N}"
209                                                         done
210                                                 else
211                                                         NEWNS="${NEWNS} ${N}"
212                                                 fi
213                                         done
214                                         NS=
215                                         DOMAIN=
216                                         SEARCH=
217                                 fi
218                                 ;;
219                 esac
220         done
221
222         # Prefer DOMAIN nameservers over SEARCH nameservers
223         # if we are supplied both.
224         NEWDOMAIN="$(uniqify ${NEWDOMAIN})"
225         NEWSEARCH="$(uniqify ${NEWSEARCH})"
226         NEWNS="$(uniqify ${NEWNS})"
227         for S in ${NEWSEARCH}; do
228                 for DN in ${NEWDOMAIN}; do
229                         if [ "${S%,*}" = "${DN%,*}" ]; then
230                                 NEWSEARCH="$(echo "${NEWSEARCH}" |
231                                         sed -e "s/${S}/${DN}/g")"
232                                 NEWDOMAIN="$(echo "${NEWDOMAIN}" |
233                                         sed -e "s/${DN}//g")"
234                                 break
235                         fi
236                 done
237         done
238
239         echo "NEWDOMAIN='${NEWDOMAIN}'"
240         echo "NEWSEARCH='${NEWSEARCH}'"
241         echo "NEWNS='${NEWNS}'"
242         exit 0
243 fi
244
245 # Test that we have valid options
246 if [ "x${CMD}" = "x-a" -o "x${CMD}" = "x-d" ]; then
247         if [ -z "${IFACE}" ]; then
248                 usage "Interface not specified"
249         fi
250 elif [ "x${CMD}" != "x-u" ]; then
251         [ -n "x${CMD}" -a "x${CMD}" != "x-h" ] && usage "Unknown option ${CMD}"
252         usage
253 fi
254 if [ "x${CMD}" = "x-a" ]; then
255         for x in '/' \\ ' ' '*'; do
256                 case "${IFACE}" in
257                         *[${x}]*) error_exit "${x} not allowed in interface name";;
258                 esac
259         done
260         for x in '.' '-' '~'; do
261                 case "${IFACE}" in
262                         [${x}]*) error_exit "${x} not allowed at start of interface name";;
263                 esac
264         done
265         [ "x${CMD}" = "x-a" -a -t 0 ] && error_exit "No file given via stdin"
266         IFACERESOLV="${IFACEDIR}/${IFACE}"
267 fi
268
269 # Ensure that libdir exists
270 if [ ! -d "${IFACEDIR}" ]; then
271         if [ ! -d "${VARDIR}" ]; then
272                 if [ -L "${VARDIR}" ]; then
273                         DIR="$(readlink "${VARDIR}")"
274                         # Change to /etc as link maybe relative
275                         cd "${VARDIR%/*}"
276                         if ! mkdir -m 0755 -p "${DIR}"; then
277                                 error_exit "Failed to create needed directory ${DIR}"
278                         fi
279                 else
280                         if ! mkdir -m 0755 -p "${VARDIR}"; then
281                                 error_exit "Failed to create needed directory ${VARDIR}"
282                         fi
283                 fi
284         fi
285         mkdir -m 0755 -p "${IFACEDIR}" || \
286                 error_exit "Failed to create needed directory ${IFACEDIR}"
287 else
288         # Delete any existing information about the interface
289         if [ "x${CMD}" = "x-a" -o "x${CMD}" = "x-d" ]; then
290                 cd "${IFACEDIR}"
291                 for iface in ${IFACE}; do
292                         rm -f "${iface}" || exit $?
293                 done
294         fi
295 fi
296
297 if [ "x${CMD}" = "x-a" ]; then
298         # Create our resolv.conf file
299         cat >"${IFACEDIR}"/"${IFACE}" || exit $?
300 fi
301
302 retval=0
303 for x in "${UPDATED}"/*; do
304         if [ -e "${x}" ]; then
305                 "${x}" "${CMD}" "${IFACE}"
306                 retval=$((${retval} + $?))
307         fi
308 done
309
310 exit ${retval}