Send IPv6 domain servers to dnsmasq via dbus.
[openresolv] / resolvconf.in
1 #!/bin/sh
2 # Copyright (c) 2007-2011 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= OIFS="$IFS"
88
89         [ -n "$1" -a -e "$IFACEDIR/$1" ] || return 1
90         echo "# resolv.conf from $1"
91         # Our variable maker works of the fact each resolv.conf per interface
92         # is separated by blank lines.
93         # So we remove them when echoing them.
94         while read -r line; do
95                 IFS="$OIFS"
96                 if [ -n "$line" ]; then
97                         # We need to set IFS here to preserve any whitespace
98                         IFS=''
99                         printf "%s\n" "$line"
100                 fi
101         done < "$IFACEDIR/$1"
102         echo
103         IFS="$OIFS"
104 }
105
106 # Parse resolv.conf's and make variables
107 # for domain name servers, search name servers and global nameservers
108 parse_resolv()
109 {
110         local line= ns= ds= search= d= n= newns=
111         local new=true iface= private=false p= domain=
112
113         echo "DOMAIN="
114         echo "DOMAINS="
115         echo "SEARCH=\"$search_domains\""
116         # let our subscribers know about global nameservers
117         for n in $name_servers; do
118                 case "$n" in
119                 127.*|0.0.0.0|255.255.255.255|::1) :;;
120                 *) newns="$newns${newns:+ }$n";;
121                 esac
122         done
123         echo "NAMESERVERS=\"$newns\""
124         echo "LOCALNAMESERVERS="
125         newns=
126
127         while read -r line; do
128                 case "$line" in
129                 "# resolv.conf from "*)
130                         if ${new}; then
131                                 iface="${line#\# resolv.conf from *}"
132                                 new=false
133                                 if [ -e "$PRIVATEDIR/$iface" ]; then
134                                         private=true
135                                 else
136                                         # Allow expansion
137                                         cd "$IFACEDIR"
138                                         private=false
139                                         for p in $private_interfaces; do
140                                                 if [ "$p" = "$iface" ]; then
141                                                         private=true
142                                                         break
143                                                 fi
144                                         done
145                                 fi
146                         fi
147                         ;;
148                 "nameserver "*)
149                         case "${line#* }" in
150                         127.*|0.0.0.0|255.255.255.255|::1)
151                                 echo "LOCALNAMESERVERS=\"\$LOCALNAMESERVERS ${line#* }\""
152                                 continue
153                                 ;;
154                         esac
155                         ns="$ns${line#* } "
156                         ;;
157                 "domain "*)
158                         if [ -z "$domain" ]; then
159                                 domain="${line#* }"
160                                 echo "DOMAIN=\"$domain\""
161                         fi
162                         search="${line#* }"
163                         ;;
164                 "search "*)
165                         search="${line#* }"
166                         ;;
167                 *)
168                         [ -n "$line" ] && continue
169                         if [ -n "$ns" -a -n "$search" ]; then
170                                 newns=
171                                 for n in $ns; do
172                                         newns="$newns${newns:+,}$n"
173                                 done
174                                 ds=
175                                 for d in $search; do
176                                         ds="$ds${ds:+ }$d:$newns"
177                                 done
178                                 echo "DOMAINS=\"\$DOMAINS $ds\""
179                         fi
180                         echo "SEARCH=\"\$SEARCH $search\""
181                         if ! $private; then
182                                 echo "NAMESERVERS=\"\$NAMESERVERS $ns\""
183                         fi
184                         ns=
185                         search=
186                         new=true
187                         ;;
188                 esac
189         done
190 }
191
192 uniqify()
193 {
194         local result=
195         while [ -n "$1" ]; do
196                 case " $result " in
197                 *" $1 "*);;
198                 *) result="$result $1";;
199                 esac
200                 shift
201         done
202         echo "${result# *}"
203 }
204
205 dirname()
206 {
207         local dir= OIFS="$IFS"
208         local IFS=/
209         set -- $@
210         IFS="$OIFS"
211         if [ -n "$1" ]; then
212                 printf %s .
213         else
214                 shift
215         fi
216         while [ -n "$2" ]; do
217                 printf "/%s" "$1"
218                 shift
219         done
220         printf "\n"
221 }
222
223 config_mkdirs()
224 {
225         local e=0 f d
226         for f; do
227                 [ -n "$f" ] || continue
228                 d="$(dirname "$f")"
229                 if [ ! -d "$d" ]; then
230                         if type install >/dev/null 2>&1; then
231                                 install -d "$d" || e=$?
232                         else
233                                 mkdir "$d" || e=$?
234                         fi
235                 fi
236         done
237         return $e
238 }
239
240 list_resolv()
241 {
242         [ -d "$IFACEDIR" ] || return 0
243
244         local report=false list= retval=0 cmd="$1"
245         shift
246
247         # If we have an interface ordering list, then use that.
248         # It works by just using pathname expansion in the interface directory.
249         if [ -n "$1" ]; then
250                 list="$*"
251                 $force || report=true
252         else
253                 cd "$IFACEDIR"
254                 for i in $interface_order; do
255                         [ -e "$i" ] && list="$list $i"
256                 done
257                 for i in $dynamic_order; do
258                         if [ -e "$i" -a ! -e "$METRICDIR/"*" $i" ]; then
259                                 list="$list $i"
260                         fi
261                 done
262                 if [ -d "$METRICDIR" ]; then
263                         cd "$METRICDIR"
264                         for i in *; do
265                                 list="$list ${i#* }"
266                         done
267                 fi
268                 list="$list *"
269         fi
270
271         cd "$IFACEDIR"
272         for i in $(uniqify $list); do
273                 # Only list interfaces which we really have
274                 if ! [ -e "$i" ]; then
275                         if $report; then
276                                 echo "No resolv.conf for interface $i" >&2
277                                 retval=$(($retval + 1))
278                         fi
279                         continue
280                 fi
281                 
282                 if [ "$cmd" = i -o "$cmd" = "-i" ]; then
283                         printf %s "$i "
284                 else
285                         echo_resolv "$i"
286                 fi
287         done
288         [ "$cmd" = i -o "$cmd" = "-i" ] && echo
289         return $retval
290 }
291
292 make_vars()
293 {
294         eval "$(list_resolv -l "$@" | parse_resolv)"
295
296         # Ensure that we only list each domain once
297         newdomains=
298         for d in $DOMAINS; do
299                 dn="${d%%:*}"
300                 case " $newdomains" in
301                 *" ${dn}:"*) continue;;
302                 esac
303                 newdomains="$newdomains${newdomains:+ }$dn:"
304                 newns=
305                 for nd in $DOMAINS; do
306                         if [ "$dn" = "${nd%%:*}" ]; then
307                                 ns="${nd#*:}"
308                                 while [ -n "$ns" ]; do
309                                         case ",$newns," in
310                                         *,${ns%%,*},*) ;;
311                                         *) newns="$newns${newns:+,}${ns%%,*}";;
312                                         esac
313                                         [ "$ns" = "${ns#*,}" ] && break
314                                         ns="${ns#*,}"
315                                 done
316                         fi
317                 done
318                 newdomains="$newdomains$newns"
319         done
320         echo "DOMAIN='$DOMAIN'"
321         echo "SEARCH='$(uniqify $SEARCH)'"
322         echo "NAMESERVERS='$(uniqify $NAMESERVERS)'"
323         echo "LOCALNAMESERVERS='$(uniqify $LOCALNAMESERVERS)'"
324         echo "DOMAINS='$newdomains'"
325 }
326
327 force=false
328 while getopts a:Dd:fhIilm:puv OPT; do
329         case "$OPT" in
330         f) force=true;;
331         h) usage;;
332         m) IF_METRIC="$OPTARG";;
333         p) IF_PRIVATE=1;;
334         '?') ;;
335         *) cmd="$OPT"; iface="$OPTARG";;
336         esac
337 done
338 shift $(($OPTIND - 1))
339 args="$iface${iface:+ }$*"
340
341 # -I inits the state dir
342 if [ "$cmd" = I ]; then
343         if [ -d "$VARDIR" ]; then
344                 rm -rf "$VARDIR"/*
345         fi
346         exit $?
347 fi
348
349 # -D ensures that the listed config file base dirs exist
350 if [ "$cmd" = D ]; then
351         config_mkdirs "$@"
352         exit $?
353 fi
354
355 # -l lists our resolv files, optionally for a specific interface
356 if [ "$cmd" = l -o "$cmd" = i ]; then
357         list_resolv "$cmd" "$args"
358         exit $?
359 fi
360
361 # Not normally needed, but subscribers should be able to run independently
362 if [ "$cmd" = v ]; then
363         make_vars "$iface"
364         exit $?
365 fi
366
367 # Test that we have valid options
368 if [ "$cmd" = a -o "$cmd" = d ]; then
369         if [ -z "$iface" ]; then
370                 usage "Interface not specified"
371         fi
372 elif [ "$cmd" != u ]; then
373         [ -n "$cmd" -a "$cmd" != h ] && usage "Unknown option $cmd"
374         usage
375 fi
376 if [ "$cmd" = a ]; then
377         for x in '/' \\ ' ' '*'; do
378                 case "$iface" in
379                 *[$x]*) error_exit "$x not allowed in interface name";;
380                 esac
381         done
382         for x in '.' '-' '~'; do
383                 case "$iface" in
384                 [$x]*) error_exit \
385                         "$x not allowed at start of interface name";;
386                 esac
387         done
388         [ "$cmd" = a -a -t 0 ] && error_exit "No file given via stdin"
389 fi
390
391 if [ ! -d "$IFACEDIR" ]; then
392         if [ ! -d "$VARDIR" ]; then
393                 if [ -L "$VARDIR" ]; then
394                         dir="$(readlink "$VARDIR")"
395                         # link maybe relative
396                         cd "${VARDIR%/*}"
397                         if ! mkdir -m 0755 -p "$dir"; then
398                                 error_exit "Failed to create needed" \
399                                         "directory $dir"
400                         fi
401                 else
402                         if ! mkdir -m 0755 -p "$VARDIR"; then
403                                 error_exit "Failed to create needed" \
404                                         "directory $VARDIR"
405                         fi
406                 fi
407         fi
408         mkdir -m 0755 -p "$IFACEDIR" || \
409                 error_exit "Failed to create needed directory $IFACEDIR"
410 else
411         # Delete any existing information about the interface
412         if [ "$cmd" = d ]; then
413                 cd "$IFACEDIR"
414                 for i in $args; do
415                         if [ "$cmd" = d -a ! -e "$i" ]; then
416                                 $force && continue
417                                 error_exit "No resolv.conf for" \
418                                         "interface $i"
419                         fi
420                         rm -f "$i" "$METRICDIR/"*" $i" \
421                                 "$PRIVATEDIR/$i" || exit $?
422                 done
423         fi
424 fi
425
426 if [ "$cmd" = a ]; then
427         # Read resolv.conf from stdin
428         resolv="$(cat)"
429         changed=false
430         # If what we are given matches what we have, then do nothing
431         if [ -e "$IFACEDIR/$iface" ]; then
432                 if [ "$(echo "$resolv")" != \
433                         "$(cat "$IFACEDIR/$iface")" ]
434                 then
435                         rm "$IFACEDIR/$iface"
436                         changed=true
437                 fi
438         else
439                 changed=true
440         fi
441         if $changed; then
442                 echo "$resolv" >"$IFACEDIR/$iface" || exit $?
443         fi
444         [ ! -d "$METRICDIR" ] && mkdir "$METRICDIR"
445         oldmetric="$METRICDIR/"*" $iface"
446         newmetric=
447         if [ -n "$IF_METRIC" ]; then
448                 # Pad metric to 6 characters, so 5 is less than 10
449                 while [ ${#IF_METRIC} -le 6 ]; do
450                         IF_METRIC="0$IF_METRIC"
451                 done
452                 newmetric="$METRICDIR/$IF_METRIC $iface"
453         fi
454         rm -f "$METRICDIR/"*" $iface"
455         [ "$oldmetric" != "$newmetric" -a \
456             "$oldmetric" != "$METRICDIR/* $iface" ] &&
457                 changed=true
458         [ -n "$newmetric" ] && echo " " >"$newmetric"
459         case "$IF_PRIVATE" in
460         [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
461                 if [ ! -d "$PRIVATEDIR" ]; then
462                         [ -e "$PRIVATEDIR" ] && rm "$PRIVATEDIR"
463                         mkdir "$PRIVATEDIR"
464                 fi
465                 [ -e "$PRIVATEDIR/$iface" ] || changed=true
466                 [ -d "$PRIVATEDIR" ] && echo " " >"$PRIVATEDIR/$iface"
467                 ;;
468         *)
469                 if [ -e "$PRIVATEDIR/$iface" ]; then
470                         rm -f "$PRIVATEDIR/$iface"
471                         changed=true
472                 fi
473                 ;;
474         esac
475         $changed || exit 0
476         unset changed oldmetric newmetric
477 fi
478
479 eval "$(make_vars)"
480 export RESOLVCONF DOMAINS SEARCH NAMESERVERS LOCALNAMESERVERS
481 : ${list_resolv:=list_resolv -l}
482 retval=0
483 for script in "$LIBEXECDIR"/*; do
484         if [ -f "$script" ]; then
485                 if [ -x "$script" ]; then
486                         "$script" "$cmd" "$iface"
487                 else
488                         (set -- "$cmd" "$iface"; . "$script")
489                 fi
490                 retval=$(($retval + $?))
491         fi
492 done
493 exit $retval