Add missing -I option to getopt(1p) call.
[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 p=
105
106         echo "DOMAINS="
107         echo "SEARCH=\"$search_domains\""
108         # let our subscribers know about global nameservers
109         for n in $name_servers; do
110                 case "$n" in
111                 127.*|0.0.0.0|255.255.255.255|::1) continue;;
112                 *) newns="$newns${newns:+ }$n"
113                 esac
114         done
115         echo "NAMESERVERS=\"$newns\""
116         newns=
117
118         while read line; do
119                 case "$line" in
120                 "# resolv.conf from "*)
121                         if ${new}; then
122                                 iface="${line#\# resolv.conf from *}"
123                                 new=false
124                                 if [ -e "$PRIVATEDIR/$iface" ]; then
125                                         private=true
126                                 else
127                                         # Allow expansion
128                                         cd "$IFACEDIR"
129                                         private=false
130                                         for p in $private_interfaces; do
131                                                 if [ "$p" = "$iface" ]; then
132                                                         private=true
133                                                         break
134                                                 fi
135                                         done
136                                 fi
137                         fi
138                         ;;
139                 "nameserver "*)
140                         case "${line#* }" in
141                         127.*|0.0.0.0|255.255.255.255|::1) continue;;
142                         esac
143                         ns="$ns${line#* } "
144                         ;;
145                 "domain "*|"search "*)
146                         search="${line#* }"
147                         ;;
148                 *)
149                         [ -n "$line" ] && continue
150                         if [ -n "$ns" -a -n "$search" ]; then
151                                 newns=
152                                 for n in $ns; do
153                                         newns="$newns${newns:+,}$n"
154                                 done
155                                 ds=
156                                 for d in $search; do
157                                         ds="$ds${ds:+ }$d:$newns"
158                                 done
159                                 echo "DOMAINS=\"\$DOMAINS $ds\""
160                         fi
161                         echo "SEARCH=\"\$SEARCH $search\""
162                         if ! $private; then
163                                 echo "NAMESERVERS=\"\$NAMESERVERS $ns\""
164                         fi
165                         ns=
166                         search=
167                         new=true
168                         ;;
169                 esac
170         done
171 }
172
173 uniqify()
174 {
175         local result=
176         while [ -n "$1" ]; do
177                 case " $result " in
178                 *" $1 "*);;
179                 *) result="$result $1";;
180                 esac
181                 shift
182         done
183         echo "${result# *}"
184 }
185
186 list_resolv()
187 {
188         [ -d "$IFACEDIR" ] || return 0
189
190         local report=false list= retval=0 cmd="$1"
191         shift
192
193         # If we have an interface ordering list, then use that.
194         # It works by just using pathname expansion in the interface directory.
195         if [ -n "$1" ]; then
196                 list="$@"
197                 $force || report=true
198         else
199                 cd "$IFACEDIR"
200                 for i in $interface_order; do
201                         [ -e "$i" ] && list="$list $i"
202                 done
203                 for i in $dynamic_order; do
204                         if [ -e "$i" -a ! -e "$METRICDIR/"*" $i" ]; then
205                                 list="$list $i"
206                         fi
207                 done
208                 if [ -d "$METRICDIR" ]; then
209                         cd "$METRICDIR"
210                         for i in *; do
211                                 list="$list ${i#* }"
212                         done
213                 fi
214                 list="$list *"
215         fi
216
217         cd "$IFACEDIR"
218         for i in $(uniqify $list); do
219                 # Only list interfaces which we really have
220                 if ! [ -e "$i" ]; then
221                         if $report; then
222                                 echo "No resolv.conf for interface $i" >&2
223                                 retval=$(($retval + 1))
224                         fi
225                         continue
226                 fi
227                 
228                 if [ "$cmd" = i -o "$cmd" = "-i" ]; then
229                         printf "$i "
230                 else
231                         echo_resolv "$i"
232                 fi
233         done
234         [ "$cmd" = i -o "$cmd" = "-i" ] && echo
235         return $retval
236 }
237
238 make_vars()
239 {
240         eval "$(list_resolv -l "$@" | parse_resolv)"
241
242         # Ensure that we only list each domain once
243         newdomains=
244         for d in $DOMAINS; do
245                 dn="${d%%:*}"
246                 case " $newdomains" in
247                 *" ${dn}:"*) continue;;
248                 esac
249                 newdomains="$newdomains${newdomains:+ }$dn:"
250                 newns=
251                 for nd in $DOMAINS; do
252                         if [ "$dn" = "${nd%%:*}" ]; then
253                                 ns="${nd#*:}"
254                                 while [ -n "$ns" ]; do
255                                         case ",$newns," in
256                                         *,${ns%%,*},*) ;;
257                                         *) newns="$newns${newns:+,}${ns%%,*}";;
258                                         esac
259                                         [ "$ns" = "${ns#*,}" ] && break
260                                         ns="${ns#*,}"
261                                 done
262                         fi
263                 done
264                 newdomains="$newdomains$newns"
265         done
266         echo "DOMAINS='$newdomains'"
267         echo "SEARCH='$(uniqify $SEARCH)'"
268         echo "NAMESERVERS='$(uniqify $NAMESERVERS)'"
269 }
270
271 force=false
272 while getopts a:d:fhIilm:puv OPT; do
273         case "$OPT" in
274         f) force=true;;
275         h) usage;;
276         m) IF_METRIC="$OPTARG";;
277         p) IF_PRIVATE=1;;
278         '?') ;;
279         *) cmd="$OPT"; iface="$OPTARG";;
280         esac
281 done
282 shift $(($OPTIND - 1))
283 args="$iface${iface:+ }$@"
284
285 # -I inits the state dir
286 if [ "$cmd" = I ]; then
287         if [ -d "$VARDIR" ]; then
288                 rm -rf "$VARDIR"/*
289         fi
290         exit $?
291 fi
292
293 # -l lists our resolv files, optionally for a specific interface
294 if [ "$cmd" = l -o "$cmd" = i ]; then
295         list_resolv "$cmd" "$args"
296         exit $?
297 fi
298
299 # Not normally needed, but subscribers should be able to run independently
300 if [ "$cmd" = v ]; then
301         make_vars "$iface"
302         exit $?
303 fi
304
305 # Test that we have valid options
306 if [ "$cmd" = a -o "$cmd" = d ]; then
307         if [ -z "$iface" ]; then
308                 usage "Interface not specified"
309         fi
310 elif [ "$cmd" != u ]; then
311         [ -n "$cmd" -a "$cmd" != h ] && usage "Unknown option $cmd"
312         usage
313 fi
314 if [ "$cmd" = a ]; then
315         for x in '/' \\ ' ' '*'; do
316                 case "$iface" in
317                 *[$x]*) error_exit "$x not allowed in interface name";;
318                 esac
319         done
320         for x in '.' '-' '~'; do
321                 case "$iface" in
322                 [$x]*) error_exit \
323                         "$x not allowed at start of interface name";;
324                 esac
325         done
326         [ "$cmd" = a -a -t 0 ] && error_exit "No file given via stdin"
327 fi
328
329 if [ ! -d "$IFACEDIR" ]; then
330         if [ ! -d "$VARDIR" ]; then
331                 if [ -L "$VARDIR" ]; then
332                         dir="$(readlink "$VARDIR")"
333                         # link maybe relative
334                         cd "${VARDIR%/*}"
335                         if ! mkdir -m 0755 -p "$dir"; then
336                                 error_exit "Failed to create needed" \
337                                         "directory $dir"
338                         fi
339                 else
340                         if ! mkdir -m 0755 -p "$VARDIR"; then
341                                 error_exit "Failed to create needed" \
342                                         "directory $VARDIR"
343                         fi
344                 fi
345         fi
346         mkdir -m 0755 -p "$IFACEDIR" || \
347                 error_exit "Failed to create needed directory $IFACEDIR"
348 else
349         # Delete any existing information about the interface
350         if [ "$cmd" = d ]; then
351                 cd "$IFACEDIR"
352                 for i in $args; do
353                         if [ "$cmd" = d -a ! -e "$i" ]; then
354                                 $force && continue
355                                 error_exit "No resolv.conf for" \
356                                         "interface $i"
357                         fi
358                         rm -f "$i" "$METRICDIR/"*" $i" \
359                                 "$PRIVATEDIR/$i" || exit $?
360                 done
361         fi
362 fi
363
364 if [ "$cmd" = a ]; then
365         # Read resolv.conf from stdin
366         resolv="$(cat)\n"
367         # If what we are given matches what we have, then do nothing
368         if [ -e "$IFACEDIR/$iface" ]; then
369                 if [ "$(printf "$resolv")" = \
370                         "$(cat "$IFACEDIR/$iface")" ]
371                 then
372                         exit 0
373                 fi
374                 rm "$IFACEDIR/$iface"
375         fi
376         printf "$resolv" >"$IFACEDIR/$iface" || exit $?
377         [ ! -d "$METRICDIR" ] && mkdir "$METRICDIR"
378         rm -f "$METRICDIR/"*" $iface"
379         if [ -n "$IF_METRIC" ]; then
380                 # Pad metric to 6 characters, so 5 is less than 10
381                 while [ ${#IF_METRIC} -le 6 ]; do
382                         IF_METRIC="0$IF_METRIC"
383                 done
384                 echo " " >"$METRICDIR/$IF_METRIC $iface"
385         fi
386         case "$IF_PRIVATE" in
387         [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
388                 if [ ! -d "$PRIVATEDIR" ]; then
389                         [ -e "$PRIVATEDIR" ] && rm "$PRIVATEDIR"
390                         mkdir "$PRIVATEDIR"
391                 fi
392                 [ -d "$PRIVATEDIR" ] && echo " " >"$PRIVATEDIR/$iface"
393                 ;;
394         *)
395                 if [ -e "$PRIVATEDIR/$iface" ]; then
396                         rm -f "$PRIVATEDIR/$iface"
397                 fi
398                 ;;
399         esac
400 fi
401
402 eval "$(make_vars)"
403 export RESOLVCONF DOMAINS SEARCH NAMESERVERS
404 : ${list_resolv:=list_resolv -l}
405 retval=0
406 for script in "$LIBEXECDIR"/*; do
407         if [ -f "$script" ]; then
408                 if [ -x "$script" ]; then
409                         "$script" "$cmd" "$iface"
410                 else
411                         (. "$script" "$cmd" "$iface")
412                 fi
413                 retval=$(($retval + $?))
414         fi
415 done
416 exit $retval