Make resolvconf_passthrough NULL work a bit better.
[openresolv] / libc.in
1 #!/bin/sh
2 # Copyright (c) 2007-2014 Roy Marples
3 # All rights reserved
4
5 # libc subscriber for resolvconf
6
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #     * Redistributions of source code must retain the above copyright
11 #       notice, this list of conditions and the following disclaimer.
12 #     * Redistributions in binary form must reproduce the above
13 #       copyright notice, this list of conditions and the following
14 #       disclaimer in the documentation and/or other materials provided
15 #       with the distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 SYSCONFDIR=@SYSCONFDIR@
30 LIBEXECDIR=@LIBEXECDIR@
31 VARDIR=@VARDIR@
32 IFACEDIR="$VARDIR/interfaces"
33 NL="
34 "
35
36 # sed may not be available, and this is faster on small files
37 key_get_value()
38 {
39         local key="$1" x= line=
40
41         shift
42         if [ $# -eq 0 ]; then
43                 while read -r line; do
44                         case "$line" in
45                         "$key"*) echo "${line##$key}";;
46                         esac
47                 done
48         else
49                 for x do
50                         while read -r line; do
51                                 case "$line" in
52                                 "$key"*) echo "${line##$key}";;
53                                 esac
54                         done < "$x"
55                 done
56         fi
57 }
58
59 keys_remove()
60 {
61         local key x line found
62
63         while read -r line; do
64                 found=false
65                 for key do
66                         case "$line" in
67                         "$key"*|"#"*|" "*|"     "*|"") found=true;;
68                         esac
69                         $found && break
70                 done
71                 $found || echo "$line"
72         done
73 }
74
75 local_nameservers="127.* 0.0.0.0 255.255.255.255 ::1"
76
77 # Support original resolvconf configuration layout
78 # as well as the openresolv config file
79 if [ -f "$SYSCONFDIR"/resolvconf.conf ]; then
80         . "$SYSCONFDIR"/resolvconf.conf
81 elif [ -d "$SYSCONFDIR"/resolvconf ]; then
82         SYSCONFDIR="$SYSCONFDIR/resolvconf/resolv.conf.d"
83         base="$SYSCONFDIR/resolv.conf.d/base"
84         if [ -f "$base" ]; then
85                 prepend_nameservers="$(key_get_value "nameserver " "$base")"
86                 domain="$(key_get_value "domain " "$base")"
87                 prepend_search="$(key_get_value "search " "$base")"
88                 resolv_conf_options="$(key_get_value "options " "$base")"
89                 resolv_conf_sortlist="$(key_get_value "sortlist " "$base")"
90         fi
91         if [ -f "$SYSCONFDIR"/resolv.conf.d/head ]; then
92                 resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.d/head)"
93         fi
94         if [ -f "$SYSCONFDIR"/resolv.conf.d/tail ]; then
95                 resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.d/tail)"
96         fi
97 fi
98 : ${resolv_conf:=/etc/resolv.conf}
99 : ${libc_service:=nscd}
100 : ${libc_restart:=@RESTARTCMD ${libc_service}@}
101 : ${list_resolv:=@SBINDIR@/resolvconf -l}
102 if [ "${resolv_conf_head-x}" = x -a -f "$SYSCONFDIR"/resolv.conf.head ]; then
103         resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.head)"
104 fi
105 if [ "${resolv_conf_tail-x}" = x -a -f "$SYSCONFDIR"/resolv.conf.tail ]; then
106         resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.tail)"
107 fi
108
109 uniqify()
110 {
111         local result=
112         while [ -n "$1" ]; do
113                 case " $result " in
114                 *" $1 "*);;
115                 *) result="$result $1";;
116                 esac
117                 shift
118         done
119         echo "${result# *}"
120 }
121
122 case "${resolv_conf_passthrough:-NO}" in
123 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
124         newest=
125         for conf in "$IFACEDIR"/*; do
126                 if [ -z "$newest" -o "$conf" -nt "$newest" ]; then
127                         newest="$conf"
128                 fi
129         done
130         [ -z "$newest" ] && exit 0
131         newconf="$(cat "$newest")$NL"
132         ;;
133 /dev/null|[Nn][Uu][Ll][Ll])
134         : ${resolv_conf_local_only:=NO}
135         if [ "$local_nameservers" = "127.* 0.0.0.0 255.255.255.255 ::1" ]; then
136                 local_nameservers=
137         fi
138         # Need to overwrite our variables.
139         eval "$(@SBINDIR@/resolvconf -V)"
140         ;;
141
142 *)
143         [ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
144         ;;
145 esac
146 case "${resolv_conf_passthrough:-NO}" in
147 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) ;;
148 *)
149         : ${domain:=$DOMAIN}
150         newsearch="$(uniqify $prepend_search $SEARCH)"
151         NS="$LOCALNAMESERVERS $NAMESERVERS"
152         newns=
153         gotlocal=false
154         for n in $(uniqify $prepend_nameservers $NS); do
155                 add=true
156                 islocal=false
157                 for l in $local_nameservers; do
158                         case "$n" in
159                         $l) islocal=true; gotlocal=true; break;;
160                         esac
161                 done
162                 if ! $islocal; then
163                         case "${resolv_conf_local_only:-YES}" in
164                         [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
165                                 $gotlocal && add=false;;
166                         esac
167                 fi
168                 $add && newns="$newns $n"
169         done
170
171         # Hold our new resolv.conf in a variable to save on temporary files
172         newconf="# Generated by resolvconf$NL"
173         if [ -n "$resolv_conf_head" ]; then
174                 newconf="$newconf$resolv_conf_head$NL"
175         fi
176
177         [ -n "$domain" ] && newconf="${newconf}domain $domain$NL"
178         if [ -n "$newsearch" -a "$newsearch" != "$domain" ]; then
179                 newconf="${newconf}search $newsearch$NL"
180         fi
181         for n in $newns; do
182                 newconf="${newconf}nameserver $n$NL"
183         done
184
185         # Now add anything we don't care about such as sortlist and options
186         stuff="$($list_resolv | keys_remove nameserver domain search)"
187         if [ -n "$stuff" ]; then
188                 newconf="$newconf$stuff$NL"
189         fi
190
191         # Append any user defined ones
192         if [ -n "$resolv_conf_options" ]; then
193                 newconf="${newconf}options $resolv_conf_options$NL"
194         fi
195         if [ -n "$resolv_conf_sortlist" ]; then
196                 newconf="${newconf}sortlist $resolv_conf_sortlist$NL"
197         fi
198
199         if [ -n "$resolv_conf_tail" ]; then
200                 newconf="$newconf$resolv_conf_tail$NL"
201         fi
202         ;;
203 esac
204
205 # Check if the file has actually changed or not
206 if [ -e "$resolv_conf" ]; then
207         [ "$(cat "$resolv_conf")" = "$(printf %s "$newconf")" ] && exit 0
208 fi
209
210 # Create our resolv.conf now
211 (umask 022; printf %s "$newconf" >"$resolv_conf")
212 eval $libc_restart
213
214 retval=0
215 # Notify users of the resolver
216 for script in "$LIBEXECDIR"/libc.d/*; do
217         if [ -f "$script" ]; then
218                 if [ -x "$script" ]; then
219                         "$script" "$@"
220                 else
221                         (. "$script")
222                 fi
223                 retval=$(($retval + $?))
224         fi
225 done
226 exit $retval