Ensure we have a newline.
[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 SYSCONFDIR=@SYSCONFDIR@
29 VARDIR=@VARBASE@/run/resolvconf
30 IFACEDIR="${VARDIR}/interfaces"
31
32 error_exit()
33 {
34         echo "$*" >&2
35         exit 1
36 }
37
38 usage()
39 {
40         cat <<-EOF
41         Usage: ${ARGV0##*/} [options]
42
43         Inform the system about any DNS updates.
44
45         Options:
46           -a \$INTERFACE    Add DNS information to the specified interface
47                            (DNS supplied via stdin in resolv.conf format)
48           -d \$INTERFACE    Delete DNS information from the specified interface
49           -f               Ignore non existant interfaces
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 {
68         [ -n "$1" -a -e "${IFACEDIR}/$1" ] || return 1
69         echo "# resolv.conf for interface $1"
70         cat "${IFACEDIR}/$1"
71         echo
72 }
73
74 # Parse resolv.conf's and make variables
75 # for domain name servers, search name servers and global nameservers
76 parse_resolv()
77 {
78         local LINE= NS= DOMAIN= SEARCH= N= NEWDOMAIN= NEWSEARCH= NEWNS=
79
80         echo "NEWDOMAIN="
81         echo "NEWSEARCH="
82         echo "NEWNS="
83
84         while read LINE; do
85                 case "${LINE}" in
86                 "nameserver "*)
87                         case "${LINE#* }" in
88                         127.*) continue;;
89                         esac
90                         NS="${NS}${LINE#* } "
91                         ;;
92                 "domain "*)
93                         [ -z "${SEARCH}" ] && DOMAIN="${LINE#* }"
94                         ;;
95                 "search "*)
96                         SEARCH="${LINE#* }"
97                         DOMAIN=
98                         ;;
99                 *)
100                         if [ -z "${LINE}" ]; then
101                                 for N in ${NS}; do
102                                         if [ -n "${DOMAIN}" ]; then
103                                                 echo "NEWDOMAIN=\"\${NEWDOMAIN} ${DOMAIN},${N}\""
104                                         elif [ -n "${SEARCH}" ]; then
105                                                 for S in ${SEARCH}; do
106                                                         echo "NEWSEARCH=\"\${NEWSEARCH} ${S},${N}\""
107                                                 done
108                                         else
109                                                 echo "NEWNS=\"\${NEWNS} ${N}\""
110                                         fi
111                                 done
112                                 NS=
113                                 DOMAIN=
114                                 SEARCH=
115                         fi
116                         ;;
117                 esac
118         done
119 }
120
121 uniqify()
122 {
123     local result=
124     while [ -n "$1" ]; do
125                 case " ${result} " in
126                 *" $1 "*);;
127                 *) result="${result} $1";;
128                 esac
129                 shift
130         done
131     echo "${result# *}"
132 }
133
134 FORCE=false
135 while getopts a:d:fhils:uv OPT; do
136         case "${OPT}" in
137         f) FORCE=true;;
138         h) usage;;
139         s) CMD=s; SERVICE="${OPTARG}";;
140         '?') exit 1;;
141         *) CMD="${OPT}"; IFACE="${OPTARG}";;
142         esac
143 done
144 shift $((${OPTIND} - 1))
145 ARGS="${IFACE}${IFACE:+ }$@"
146
147 # We do our service restarting here so that our subscribers don't have to know
148 # about the OS's init system.
149 if [ "${CMD}" = "s" ]; then
150         if [ -n "$1" ]; then
151                 ACTION="$1"
152                 shift
153         fi
154         [ -z "${ACTION}" ] && usage "Action not specified"
155
156         # If restarting check if service is running or not if we can
157         if [ "${ACTION}" = "restart" ]; then
158                 if [ -s /var/run/"${SERVICE}".pid ]; then
159                         kill -0 $(cat /var/run/"${SERVICE}".pid) 2>/dev/null
160                 elif [ -s /var/run/"${SERVICE}"/"${SERVICE}".pid ]; then
161                         kill -0 $(cat /var/run/"${SERVICE}"/"${SERVICE}".pid) 2>/dev/null
162                 elif [ -s /var/run/"${SERVICE}"/pid ]; then
163                         kill -0 $(cat /var/run/"${SERVICE}"/pid) 2>/dev/null
164                 else
165                         false
166                 fi
167                 # Service not running, so don't restart
168                 [ $? != 0 ] && exit 0
169         fi      
170         if [ -x /sbin/service ]; then
171                 service "${SERVICE}" "${ACTION}" "$@" 
172         elif [ -x /etc/init.d/"${SERVICE}" -a -x /sbin/runscript ]; then
173                 if [ "${ACTION}" = "restart" ]; then
174                         /etc/init.d/"${SERVICE}" --quiet --nodeps conditionalrestart "$@"
175                 else
176                         /etc/init.d/"${SERVICE}" --quiet --nodeps "${ACTION}" "$@"
177                 fi
178         elif [ -x /etc/init.d/"${SERVICE}" ]; then
179                 /etc/init.d/"${SERVICE}" "${ACTION}" "$@"
180         elif [ -x /etc/rc.d/"${SERVICE}" ]; then
181                 /etc/rc.d/"${SERVICE}" "${ACTION}" "$@" 
182         elif [ -x /etc/rc.d/rc."${SERVICE}" ]; then
183                 /etc/rc.d/rc."${SERVICE}" "${ACTION}" "$@"
184         else
185                 error_exit "Don't know how to interact with services on this platform"
186         fi
187         exit $?
188 fi
189
190 # -l lists our resolv files, optionally for a specific interface
191 if [ "${CMD}" = "l" -o "${CMD}" = "i" ]; then
192         [ -d "${IFACEDIR}" ] || exit 0
193
194         REPORT=false
195         # If we have an interface ordering list, then use that.
196         # It works by just using pathname expansion in the interface directory.
197         if [ -n "${ARGS}" ]; then
198                 LIST="${ARGS}"
199                 ${FORCE} || REPORT=true
200         elif [ -r "${SYSCONFDIR}"/interface-order ]; then
201                 LIST="$(cat "${SYSCONFDIR}"/interface-order)"
202         fi
203
204         # If we don't have a list then prefer lo, tunnels, ppp
205         # and then anything else.
206         if [ -z "${LIST}" ]; then
207                 LIST="lo lo[0-9]* tap[0-9]* tun[0-9]* vpn vpn[0-9]* ppp[0-9]* ippp[0-9]* *"
208         fi
209
210         RETVAL=0
211         cd "${IFACEDIR}"
212         for IFACE in $(uniqify ${LIST}); do
213                 # Only list interfaces which we really have
214                 if ! [ -e "${IFACE}" ]; then
215                         if ${REPORT}; then
216                                 echo "No resolv.conf for interface ${IFACE}" >&2
217                                 RETVAL=$((${RETVAL} + 1))
218                         fi
219                         continue
220                 fi
221                 
222                 if [ "${CMD}" = "i" ]; then
223                         printf "${IFACE} "
224                 else
225                         echo_resolv "${IFACE}"
226                 fi
227         done
228         [ "${CMD}" = "i" ] && echo
229         exit ${RETVAL} 
230 fi
231
232 if [ "${CMD}" = "v" ]; then
233         eval "$("${ARGV0}" -l "${IFACE}" | parse_resolv)"
234
235         # Prefer DOMAIN nameservers over SEARCH nameservers
236         # if we are supplied both, but put them in the SEARCH list
237         NEWDOMAIN="$(uniqify ${NEWDOMAIN})"
238         NEWSEARCH="$(uniqify ${NEWSEARCH})"
239         NEWNS="$(uniqify ${NEWNS})"
240         _NEWDOMAIN=
241         _NEWSEARCH=
242         for S in ${NEWSEARCH}; do
243                 for DN in ${NEWDOMAIN}; do
244                         if [ "${S%,*}" = "${DN%,*}" ]; then
245                                 S="${DN}"
246                                 break
247                         fi
248                 done
249                 _NEWSEARCH="${_NEWSEARCH}${_NEWSEARCH:+ }${S}"
250         done
251         for DN in ${NEWDOMAIN}; do
252                 for S in ${_NEWSEARCH}; do
253                         if [ "${S%,*}" = "${DN%,*}" ]; then
254                                 DN=
255                                 break
256                         fi
257                 done
258                 if [ -n "${DN}" ]; then
259                         _NEWDOMAIN="${_NEWDOMAIN}${_NEWDOMAIN:+ }${DN}"
260                 fi
261         done
262
263         echo "NEWDOMAIN='${_NEWDOMAIN}'"
264         echo "NEWSEARCH='${_NEWSEARCH}'"
265         echo "NEWNS='${NEWNS}'"
266         exit 0
267 fi
268
269 # Test that we have valid options
270 if [ "${CMD}" = "a" -o "${CMD}" = "d" ]; then
271         if [ -z "${IFACE}" ]; then
272                 usage "Interface not specified"
273         fi
274 elif [ "${CMD}" != "u" ]; then
275         [ -n "${CMD}" -a "${CMD}" != "h" ] && usage "Unknown option ${CMD}"
276         usage
277 fi
278 if [ "${CMD}" = "a" ]; then
279         for x in '/' \\ ' ' '*'; do
280                 case "${IFACE}" in
281                 *[${x}]*) error_exit "${x} not allowed in interface name";;
282                 esac
283         done
284         for x in '.' '-' '~'; do
285                 case "${IFACE}" in
286                 [${x}]*) error_exit "${x} not allowed at start of interface name";;
287                 esac
288         done
289         [ "${CMD}" = "a" -a -t 0 ] && error_exit "No file given via stdin"
290         IFACERESOLV="${IFACEDIR}/${IFACE}"
291 fi
292
293 # Ensure that libdir exists
294 if [ ! -d "${IFACEDIR}" ]; then
295         if [ ! -d "${VARDIR}" ]; then
296                 if [ -L "${VARDIR}" ]; then
297                         DIR="$(readlink "${VARDIR}")"
298                         # Change to /etc as link maybe relative
299                         cd "${VARDIR%/*}"
300                         if ! mkdir -m 0755 -p "${DIR}"; then
301                                 error_exit "Failed to create needed directory ${DIR}"
302                         fi
303                 else
304                         if ! mkdir -m 0755 -p "${VARDIR}"; then
305                                 error_exit "Failed to create needed directory ${VARDIR}"
306                         fi
307                 fi
308         fi
309         mkdir -m 0755 -p "${IFACEDIR}" || \
310                 error_exit "Failed to create needed directory ${IFACEDIR}"
311 else
312         # Delete any existing information about the interface
313         if [ "${CMD}" = "d" ]; then
314                 cd "${IFACEDIR}"
315                 for ARG in ${ARGS}; do
316                         if [ "${CMD}" = "d" -a ! -e "${ARG}" ]; then
317                                 ${FORCE} && continue
318                                 error_exit "No resolv.conf for interface ${ARG}"
319                         fi
320                         rm -f "${ARG}" || exit $?
321                 done
322         fi
323 fi
324
325 if [ "${CMD}" = "a" ]; then
326         # Read resolv.conf from stdin
327         RESOLV="$(cat)\n"
328         # If what we are given matches what we have, then do nothing
329         if [ -e "${IFACEDIR}/${IFACE}" ]; then
330                 if [ "$(printf "${RESOLV}")" = "$(cat "${IFACEDIR}/${IFACE}")" ]
331                 then
332                         exit 0
333                 fi
334                 rm "${IFACEDIR}/${IFACE}"
335         fi
336         printf "${RESOLV}" >"${IFACEDIR}/${IFACE}" || exit $?
337 fi
338
339 RETVAL=0
340 for SCRIPT in "${SYSCONFDIR}"/update.d/*; do
341         if [ -e "${SCRIPT}" ]; then
342                 "${SCRIPT}" "${CMD}" "${IFACE}"
343                 RETVAL=$((${RETVAL} + $?))
344         fi
345 done
346
347 exit ${RETVAL}