Stop quering the dnsmasq pidfile, compile options and dbus pidfile.
[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 # 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 : ${name_server_blacklist:=0.0.0.0}
49
50 error_exit()
51 {
52         echo "$*" >&2
53         exit 1
54 }
55
56 usage()
57 {
58         cat <<-EOF
59         Usage: ${RESOLVCONF##*/} [options]
60
61         Inform the system about any DNS updates.
62
63         Options:
64           -a \$INTERFACE    Add DNS information to the specified interface
65                            (DNS supplied via stdin in resolv.conf format)
66           -m metric        Give the added DNS information a metric
67           -p               Mark the interface as private
68           -d \$INTERFACE    Delete DNS information from the specified interface
69           -f               Ignore non existant interfaces
70           -I               Init the state dir
71           -u               Run updates from our current DNS information
72           -l [\$PATTERN]    Show DNS information, optionally from interfaces
73                            that match the specified pattern
74           -i [\$PATTERN]    Show interfaces that have supplied DNS information
75                    optionally from interfaces that match the specified
76                    pattern
77           -v [\$PATTERN]    echo NEWDOMAIN, NEWSEARCH and NEWNS variables to
78                            the console
79           -h               Show this help cruft
80         EOF
81         [ -z "$1" ] && exit 0
82         echo
83         error_exit "$*"
84 }
85
86 echo_resolv()
87 {
88         local line= OIFS="$IFS"
89
90         [ -n "$1" -a -e "$IFACEDIR/$1" ] || return 1
91         echo "# resolv.conf from $1"
92         # Our variable maker works of the fact each resolv.conf per interface
93         # is separated by blank lines.
94         # So we remove them when echoing them.
95         while read -r line; do
96                 IFS="$OIFS"
97                 if [ -n "$line" ]; then
98                         # We need to set IFS here to preserve any whitespace
99                         IFS=''
100                         printf "%s\n" "$line"
101                 fi
102         done < "$IFACEDIR/$1"
103         echo
104         IFS="$OIFS"
105 }
106
107 # Parse resolv.conf's and make variables
108 # for domain name servers, search name servers and global nameservers
109 parse_resolv()
110 {
111         local line= ns= ds= search= d= n= newns=
112         local new=true iface= private=false p= domain=
113
114         echo "DOMAIN="
115         echo "DOMAINS="
116         echo "SEARCH=\"$search_domains\""
117         # let our subscribers know about global nameservers
118         for n in $name_servers; do
119                 case "$n" in
120                 127.*|0.0.0.0|255.255.255.255|::1) :;;
121                 *) newns="$newns${newns:+ }$n";;
122                 esac
123         done
124         echo "NAMESERVERS=\"$newns\""
125         echo "LOCALNAMESERVERS="
126         newns=
127
128         while read -r line; do
129                 case "$line" in
130                 "# resolv.conf from "*)
131                         if ${new}; then
132                                 iface="${line#\# resolv.conf from *}"
133                                 new=false
134                                 if [ -e "$PRIVATEDIR/$iface" ]; then
135                                         private=true
136                                 else
137                                         # Allow expansion
138                                         cd "$IFACEDIR"
139                                         private=false
140                                         for p in $private_interfaces; do
141                                                 case "$iface" in
142                                                 "$p"|"$p":*) private=true; break;;
143                                                 esac
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                         for ii in "$i":*; do
257                                 [ -e "$ii" ] && list="$list $ii"
258                         done
259                 done
260                 for i in $dynamic_order; do
261                         if [ -e "$i" -a ! -e "$METRICDIR/"*" $i" ]; then
262                                 list="$list $i"
263                         fi
264                         for ii in "$i":*; do
265                                 if [ -e "$ii" -a ! -e "$METRICDIR/"*" $ii" ]; then
266                                         list="$list $ii"
267                                 fi
268                         done
269                 done
270                 if [ -d "$METRICDIR" ]; then
271                         cd "$METRICDIR"
272                         for i in *; do
273                                 list="$list ${i#* }"
274                         done
275                 fi
276                 list="$list *"
277         fi
278
279         cd "$IFACEDIR"
280         for i in $(uniqify $list); do
281                 # Only list interfaces which we really have
282                 if ! [ -e "$i" ]; then
283                         if $report; then
284                                 echo "No resolv.conf for interface $i" >&2
285                                 retval=$(($retval + 1))
286                         fi
287                         continue
288                 fi
289                 
290                 if [ "$cmd" = i -o "$cmd" = "-i" ]; then
291                         printf %s "$i "
292                 else
293                         echo_resolv "$i"
294                 fi
295         done
296         [ "$cmd" = i -o "$cmd" = "-i" ] && echo
297         return $retval
298 }
299
300 list_remove() {
301         local list= e= l= result= found= retval=0
302
303         [ -z "$2" ] && return 0
304         eval list=\"\$$1\"
305         shift
306
307         set -f
308         for e; do
309                 found=false
310                 for l in $list; do
311                         case "$e" in
312                         $l) found=true;;
313                         esac
314                         $found && break
315                 done
316                 if $found; then
317                         retval=$(($retval + 1))
318                 else
319                         result="$result $e"
320                 fi
321         done
322         set +f
323         echo "${result# *}"
324         return $retval
325 }
326
327 make_vars()
328 {
329         local newdomains= d= dn= newns= ns=
330         eval "$(list_resolv -l "$@" | parse_resolv)"
331
332         # Ensure that we only list each domain once
333         for d in $DOMAINS; do
334                 dn="${d%%:*}"
335                 list_remove domain_blacklist "$dn" >/dev/null || continue
336                 case " $newdomains" in
337                 *" ${dn}:"*) continue;;
338                 esac
339                 newns=
340                 for nd in $DOMAINS; do
341                         if [ "$dn" = "${nd%%:*}" ]; then
342                                 ns="${nd#*:}"
343                                 while [ -n "$ns" ]; do
344                                         case ",$newns," in
345                                         *,${ns%%,*},*) ;;
346                                         *) list_remove name_server_blacklist \
347                                                 "$ns" >/dev/null \
348                                         && newns="$newns${newns:+,}${ns%%,*}";;
349                                         esac
350                                         [ "$ns" = "${ns#*,}" ] && break
351                                         ns="${ns#*,}"
352                                 done
353                         fi
354                 done
355                 if [ -n "$newns" ]; then
356                         newdomains="$newdomains${newdomains:+ }$dn:$newns"
357                 fi
358         done
359         DOMAIN="$(list_remove domain_blacklist $DOMAIN)"
360         SEARCH="$(uniqify $SEARCH)"
361         SEARCH="$(list_remove domain_blacklist $SEARCH)"
362         NAMESERVERS="$(uniqify $NAMESERVERS)"
363         NAMESERVERS="$(list_remove name_server_blacklist $NAMESERVERS)"
364         LOCALNAMESERVERS="$(uniqify $LOCALNAMESERVERS)"
365         LOCALNAMESERVERS="$(list_remove name_server_blacklist $LOCALNAMESERVERS)"
366         echo "DOMAIN='$DOMAIN'"
367         echo "SEARCH='$SEARCH'"
368         echo "NAMESERVERS='$NAMESERVERS'"
369         echo "LOCALNAMESERVERS='$LOCALNAMESERVERS'"
370         echo "DOMAINS='$newdomains'"
371 }
372
373 force=false
374 while getopts a:Dd:fhIilm:puv OPT; do
375         case "$OPT" in
376         f) force=true;;
377         h) usage;;
378         m) IF_METRIC="$OPTARG";;
379         p) IF_PRIVATE=1;;
380         '?') ;;
381         *) cmd="$OPT"; iface="$OPTARG";;
382         esac
383 done
384 shift $(($OPTIND - 1))
385 args="$iface${iface:+ }$*"
386
387 # -I inits the state dir
388 if [ "$cmd" = I ]; then
389         if [ -d "$VARDIR" ]; then
390                 rm -rf "$VARDIR"/*
391         fi
392         exit $?
393 fi
394
395 # -D ensures that the listed config file base dirs exist
396 if [ "$cmd" = D ]; then
397         config_mkdirs "$@"
398         exit $?
399 fi
400
401 # -l lists our resolv files, optionally for a specific interface
402 if [ "$cmd" = l -o "$cmd" = i ]; then
403         list_resolv "$cmd" "$args"
404         exit $?
405 fi
406
407 # Not normally needed, but subscribers should be able to run independently
408 if [ "$cmd" = v ]; then
409         make_vars "$iface"
410         exit $?
411 fi
412
413 # Test that we have valid options
414 if [ "$cmd" = a -o "$cmd" = d ]; then
415         if [ -z "$iface" ]; then
416                 usage "Interface not specified"
417         fi
418 elif [ "$cmd" != u ]; then
419         [ -n "$cmd" -a "$cmd" != h ] && usage "Unknown option $cmd"
420         usage
421 fi
422 if [ "$cmd" = a ]; then
423         for x in '/' \\ ' ' '*'; do
424                 case "$iface" in
425                 *[$x]*) error_exit "$x not allowed in interface name";;
426                 esac
427         done
428         for x in '.' '-' '~'; do
429                 case "$iface" in
430                 [$x]*) error_exit \
431                         "$x not allowed at start of interface name";;
432                 esac
433         done
434         [ "$cmd" = a -a -t 0 ] && error_exit "No file given via stdin"
435 fi
436
437 if [ ! -d "$IFACEDIR" ]; then
438         if [ ! -d "$VARDIR" ]; then
439                 if [ -L "$VARDIR" ]; then
440                         dir="$(readlink "$VARDIR")"
441                         # link maybe relative
442                         cd "${VARDIR%/*}"
443                         if ! mkdir -m 0755 -p "$dir"; then
444                                 error_exit "Failed to create needed" \
445                                         "directory $dir"
446                         fi
447                 else
448                         if ! mkdir -m 0755 -p "$VARDIR"; then
449                                 error_exit "Failed to create needed" \
450                                         "directory $VARDIR"
451                         fi
452                 fi
453         fi
454         mkdir -m 0755 -p "$IFACEDIR" || \
455                 error_exit "Failed to create needed directory $IFACEDIR"
456 else
457         # Delete any existing information about the interface
458         if [ "$cmd" = d ]; then
459                 cd "$IFACEDIR"
460                 for i in $args; do
461                         if [ "$cmd" = d -a ! -e "$i" ]; then
462                                 $force && continue
463                                 error_exit "No resolv.conf for" \
464                                         "interface $i"
465                         fi
466                         rm -f "$i" "$METRICDIR/"*" $i" \
467                                 "$PRIVATEDIR/$i" || exit $?
468                 done
469         fi
470 fi
471
472 if [ "$cmd" = a ]; then
473         # Read resolv.conf from stdin
474         resolv="$(cat)"
475         changed=false
476         # If what we are given matches what we have, then do nothing
477         if [ -e "$IFACEDIR/$iface" ]; then
478                 if [ "$(echo "$resolv")" != \
479                         "$(cat "$IFACEDIR/$iface")" ]
480                 then
481                         rm "$IFACEDIR/$iface"
482                         changed=true
483                 fi
484         else
485                 changed=true
486         fi
487         if $changed; then
488                 echo "$resolv" >"$IFACEDIR/$iface" || exit $?
489         fi
490         [ ! -d "$METRICDIR" ] && mkdir "$METRICDIR"
491         oldmetric="$METRICDIR/"*" $iface"
492         newmetric=
493         if [ -n "$IF_METRIC" ]; then
494                 # Pad metric to 6 characters, so 5 is less than 10
495                 while [ ${#IF_METRIC} -le 6 ]; do
496                         IF_METRIC="0$IF_METRIC"
497                 done
498                 newmetric="$METRICDIR/$IF_METRIC $iface"
499         fi
500         rm -f "$METRICDIR/"*" $iface"
501         [ "$oldmetric" != "$newmetric" -a \
502             "$oldmetric" != "$METRICDIR/* $iface" ] &&
503                 changed=true
504         [ -n "$newmetric" ] && echo " " >"$newmetric"
505         case "$IF_PRIVATE" in
506         [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
507                 if [ ! -d "$PRIVATEDIR" ]; then
508                         [ -e "$PRIVATEDIR" ] && rm "$PRIVATEDIR"
509                         mkdir "$PRIVATEDIR"
510                 fi
511                 [ -e "$PRIVATEDIR/$iface" ] || changed=true
512                 [ -d "$PRIVATEDIR" ] && echo " " >"$PRIVATEDIR/$iface"
513                 ;;
514         *)
515                 if [ -e "$PRIVATEDIR/$iface" ]; then
516                         rm -f "$PRIVATEDIR/$iface"
517                         changed=true
518                 fi
519                 ;;
520         esac
521         $changed || exit 0
522         unset changed oldmetric newmetric
523 fi
524
525 eval "$(make_vars)"
526 export RESOLVCONF DOMAINS SEARCH NAMESERVERS LOCALNAMESERVERS
527 : ${list_resolv:=list_resolv -l}
528 retval=0
529 for script in "$LIBEXECDIR"/*; do
530         if [ -f "$script" ]; then
531                 if [ -x "$script" ]; then
532                         "$script" "$cmd" "$iface"
533                 else
534                         (set -- "$cmd" "$iface"; . "$script")
535                 fi
536                 retval=$(($retval + $?))
537         fi
538 done
539 exit $retval