49cb10a736d1e0236ba5c42986cb419d1678b3af
[openresolv] / resolvconf.in
1 #!/bin/sh
2 # Copyright (c) 2007-2009 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 RESOLVCONF="$0"
28 SYSCONFDIR=@SYSCONFDIR@
29 LIBEXECDIR=@LIBEXECDIR@
30 VARDIR=@VARDIR@
31 # Support original resolvconf configuration layout
32 # as well as the openresolv config file
33 if [ -f "$SYSCONFDIR"/resolvconf.conf ]; then
34         . "$SYSCONFDIR"/resolvconf.conf
35         [ -n "$state_dir" ] && VARDIR="$state_dir"
36 elif [ -d "$SYSCONFDIR/resolvconf" ]; then
37         SYSCONFDIR="$SYSCONFDIR/resolvconf"
38         if [ -f "$SYSCONFDIR"/interface-order ]; then
39                 interface_order="$(cat "$SYSCONFDIR"/interface-order)"
40         fi
41 fi
42 IFACEDIR="$VARDIR/interfaces"
43 METRICDIR="$VARDIR/metrics"
44 PRIVATEDIR="$VARDIR/private"
45
46 : ${dynamic_order:=tap[0-9]* tun[0-9]* vpn vpn[0-9]* ppp[0-9]* ippp[0-9]*}
47 : ${interface_order:=lo lo[0-9]*}
48
49 error_exit()
50 {
51         echo "$*" >&2
52         exit 1
53 }
54
55 usage()
56 {
57         cat <<-EOF
58         Usage: ${RESOLVCONF##*/} [options]
59
60         Inform the system about any DNS updates.
61
62         Options:
63           -a \$INTERFACE    Add DNS information to the specified interface
64                            (DNS supplied via stdin in resolv.conf format)
65           -m metric        Give the added DNS information a metric
66           -p               Mark the interface as private
67           -d \$INTERFACE    Delete DNS information from the specified interface
68           -f               Ignore non existant interfaces
69           -I               Init the state dir
70           -u               Run updates from our current DNS information
71           -l [\$PATTERN]    Show DNS information, optionally from interfaces
72                            that match the specified pattern
73           -i [\$PATTERN]    Show interfaces that have supplied DNS information
74                    optionally from interfaces that match the specified
75                    pattern
76           -v [\$PATTERN]    echo NEWDOMAIN, NEWSEARCH and NEWNS variables to
77                            the console
78           -h               Show this help cruft
79         EOF
80         [ -z "$1" ] && exit 0
81         echo
82         error_exit "$*"
83 }
84
85 echo_resolv()
86 {
87         local line=
88         [ -n "$1" -a -e "$IFACEDIR/$1" ] || return 1
89         echo "# resolv.conf from $1"
90         # Our variable maker works of the fact each resolv.conf per interface
91         # is separated by blank lines.
92         # So we remove them when echoing them.
93         while read line; do
94                 [ -n "$line" ] && echo "$line"
95         done < "$IFACEDIR/$1"
96         echo
97 }
98
99 # Parse resolv.conf's and make variables
100 # for domain name servers, search name servers and global nameservers
101 parse_resolv()
102 {
103         local line= ns= ds= search= d= n= newns=
104         local new=true iface= private=false
105
106         echo "DOMAINS="
107         echo "SEARCH="
108         echo "NAMESERVERS="
109
110         while read line; do
111                 case "$line" in
112                 "# resolv.conf from "*)
113                         if ${new}; then
114                                 iface="${line#\# resolv.conf from *}"
115                                 new=false
116                                 case " $private_interfaces " in
117                                 *" $iface "*)
118                                         private=true
119                                         ;;
120                                 *)
121                                         if [ -e "$PRIVATEDIR/$iface" ]; then
122                                                 private=true
123                                         else
124                                                 private=false
125                                         fi
126                                         ;;
127                                 esac
128                         fi
129                         ;;
130                 "nameserver "*)
131                         case "${line#* }" in
132                         127.*|0.0.0.0|255.255.255.255) continue;;
133                         esac
134                         ns="$ns${line#* } "
135                         ;;
136                 "domain "*|"search "*)
137                         search="${line#* }"
138                         ;;
139                 *)
140                         [ -n "$line" ] && continue
141                         if [ -n "$ns" -a -n "$search" ]; then
142                                 newns=
143                                 for n in $ns; do
144                                         newns="$newns${newns:+,}$n"
145                                 done
146                                 ds=
147                                 for d in $search; do
148                                         ds="$ds${ds:+ }$d:$newns"
149                                 done
150                                 echo "DOMAINS=\"\$DOMAINS $ds\""
151                         fi
152                         echo "SEARCH=\"\$SEARCH $search\""
153                         if ! $private; then
154                                 echo "NAMESERVERS=\"\$NAMESERVERS $ns\""
155                         fi
156                         ns=
157                         search=
158                         new=true
159                         ;;
160                 esac
161         done
162 }
163
164 uniqify()
165 {
166         local result=
167         while [ -n "$1" ]; do
168                 case " $result " in
169                 *" $1 "*);;
170                 *) result="$result $1";;
171                 esac
172                 shift
173         done
174         echo "${result# *}"
175 }
176
177 list_resolv()
178 {
179         [ -d "$IFACEDIR" ] || return 0
180
181         local report=false list= retval=0 cmd="$1"
182         shift
183
184         # If we have an interface ordering list, then use that.
185         # It works by just using pathname expansion in the interface directory.
186         if [ -n "$1" ]; then
187                 list="$@"
188                 $force || report=true
189         else
190                 cd "$IFACEDIR"
191                 for i in $interface_order; do
192                         [ -e "$i" ] && list="$list $i"
193                 done
194                 for i in $dynamic_order; do
195                         if [ -e "$i" -a ! -e "$METRICDIR/"*" $i" ]; then
196                                 list="$list $i"
197                         fi
198                 done
199                 if [ -d "$METRICDIR" ]; then
200                         cd "$METRICDIR"
201                         for i in *; do
202                                 list="$list ${i#* }"
203                         done
204                 fi
205                 list="$list *"
206         fi
207
208         cd "$IFACEDIR"
209         for i in $(uniqify $list); do
210                 # Only list interfaces which we really have
211                 if ! [ -e "$i" ]; then
212                         if $report; then
213                                 echo "No resolv.conf for interface $i" >&2
214                                 retval=$(($retval + 1))
215                         fi
216                         continue
217                 fi
218                 
219                 if [ "$cmd" = i -o "$cmd" = "-i" ]; then
220                         printf "$i "
221                 else
222                         echo_resolv "$i"
223                 fi
224         done
225         [ "$cmd" = i -o "$cmd" = "-i" ] && echo
226         return $retval
227 }
228
229 make_vars()
230 {
231         eval "$(list_resolv -l "$@" | parse_resolv)"
232
233         # Ensure that we only list each domain once
234         newdomains=
235         for d in $DOMAINS; do
236                 dn="${d%%:*}"
237                 case " $newdomains" in
238                 *" ${dn}:"*) continue;;
239                 esac
240                 newdomains="$newdomains${newdomains:+ }$dn:"
241                 newns=
242                 for nd in $DOMAINS; do
243                         if [ "$dn" = "${nd%%:*}" ]; then
244                                 ns="${nd#*:}"
245                                 while [ -n "$ns" ]; do
246                                         case ",$newns," in
247                                         *,${ns%%,*},*) ;;
248                                         *) newns="$newns${newns:+,}${ns%%,*}";;
249                                         esac
250                                         [ "$ns" = "${ns#*,}" ] && break
251                                         ns="${ns#*,}"
252                                 done
253                         fi
254                 done
255                 newdomains="$newdomains$newns"
256         done
257         echo "DOMAINS='$newdomains'"
258         echo "SEARCH='$(uniqify $SEARCH)'"
259         echo "NAMESERVERS='$(uniqify $NAMESERVERS)'"
260 }
261
262 force=false
263 while getopts a:d:fhilm:puv OPT; do
264         case "$OPT" in
265         f) force=true;;
266         h) usage;;
267         m) IF_METRIC="$OPTARG";;
268         p) IF_PRIVATE=1;;
269         '?') ;;
270         *) cmd="$OPT"; iface="$OPTARG";;
271         esac
272 done
273 shift $(($OPTIND - 1))
274 args="$iface${iface:+ }$@"
275
276 # -I inits the state dir
277 if [ "$cmd" = I ]; then
278         if [ -d "$VARDIR" ]; then
279                 rm -rf "$VARDIR"/*
280         fi
281         exit $?
282 fi
283
284 # -l lists our resolv files, optionally for a specific interface
285 if [ "$cmd" = l -o "$cmd" = i ]; then
286         list_resolv "$cmd" "$args"
287         exit $?
288 fi
289
290 # Not normally needed, but subscribers should be able to run independently
291 if [ "$cmd" = v ]; then
292         make_vars "$iface"
293         exit $?
294 fi
295
296 # Test that we have valid options
297 if [ "$cmd" = a -o "$cmd" = d ]; then
298         if [ -z "$iface" ]; then
299                 usage "Interface not specified"
300         fi
301 elif [ "$cmd" != u ]; then
302         [ -n "$cmd" -a "$cmd" != h ] && usage "Unknown option $cmd"
303         usage
304 fi
305 if [ "$cmd" = a ]; then
306         for x in '/' \\ ' ' '*'; do
307                 case "$iface" in
308                 *[$x]*) error_exit "$x not allowed in interface name";;
309                 esac
310         done
311         for x in '.' '-' '~'; do
312                 case "$iface" in
313                 [$x]*) error_exit \
314                         "$x not allowed at start of interface name";;
315                 esac
316         done
317         [ "$cmd" = a -a -t 0 ] && error_exit "No file given via stdin"
318 fi
319
320 if [ ! -d "$IFACEDIR" ]; then
321         if [ ! -d "$VARDIR" ]; then
322                 if [ -L "$VARDIR" ]; then
323                         dir="$(readlink "$VARDIR")"
324                         # link maybe relative
325                         cd "${VARDIR%/*}"
326                         if ! mkdir -m 0755 -p "$dir"; then
327                                 error_exit "Failed to create needed" \
328                                         "directory $dir"
329                         fi
330                 else
331                         if ! mkdir -m 0755 -p "$VARDIR"; then
332                                 error_exit "Failed to create needed" \
333                                         "directory $VARDIR"
334                         fi
335                 fi
336         fi
337         mkdir -m 0755 -p "$IFACEDIR" || \
338                 error_exit "Failed to create needed directory $IFACEDIR"
339 else
340         # Delete any existing information about the interface
341         if [ "$cmd" = d ]; then
342                 cd "$IFACEDIR"
343                 for i in $args; do
344                         if [ "$cmd" = d -a ! -e "$i" ]; then
345                                 $force && continue
346                                 error_exit "No resolv.conf for" \
347                                         "interface $i"
348                         fi
349                         rm -f "$i" "$METRICDIR/"*" $i" \
350                                 "$PRIVATEDIR/$i" || exit $?
351                 done
352         fi
353 fi
354
355 if [ "$cmd" = a ]; then
356         # Read resolv.conf from stdin
357         resolv="$(cat)\n"
358         # If what we are given matches what we have, then do nothing
359         if [ -e "$IFACEDIR/$iface" ]; then
360                 if [ "$(printf "$resolv")" = \
361                         "$(cat "$IFACEDIR/$iface")" ]
362                 then
363                         exit 0
364                 fi
365                 rm "$IFACEDIR/$iface"
366         fi
367         printf "$resolv" >"$IFACEDIR/$iface" || exit $?
368         [ ! -d "$METRICDIR" ] && mkdir "$METRICDIR"
369         rm -f "$METRICDIR/"*" $iface"
370         if [ -n "$IF_METRIC" ]; then
371                 # Pad metric to 6 characters, so 5 is less than 10
372                 while [ ${#IF_METRIC} -le 6 ]; do
373                         IF_METRIC="0$IF_METRIC"
374                 done
375                 echo " " >"$METRICDIR/$IF_METRIC $iface"
376         fi
377         case "$IF_PRIVATE" in
378         [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
379                 if [ ! -d "$PRIVATEDIR" ]; then
380                         [ -e "$PRIVATEDIR" ] && rm "$PRIVATEDIR"
381                         mkdir "$PRIVATEDIR"
382                 fi
383                 [ -d "$PRIVATEDIR" ] && echo " " >"$PRIVATEDIR/$iface"
384                 ;;
385         *)
386                 if [ -e "$PRIVATEDIR/$iface" ]; then
387                         rm -f "$PRIVATEDIR/$iface"
388                 fi
389                 ;;
390         esac
391 fi
392
393 eval "$(make_vars)"
394 export RESOLVCONF DOMAINS SEARCH NAMESERVERS
395 : ${list_resolv:=list_resolv -l}
396 retval=0
397 for script in "$LIBEXECDIR"/*; do
398         if [ -f "$script" ]; then
399                 if [ -x "$script" ]; then
400                         "$script" "$cmd" "$iface"
401                 else
402                         (. "$script" "$cmd" "$fiace")
403                 fi
404                 retval=$(($retval + $?))
405         fi
406 done
407 exit $retval