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