Document resolv_conf_sortlist.
[openresolv] / libc.in
1 #!/bin/sh
2 # Copyright (c) 2007-2011 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" value= x= line=
40
41         shift
42         if [ $# -eq 0 ]; then
43                 while read line; do
44                         case "$line" in
45                         "$key"*) echo "${line##$key}";;
46                         esac
47                 done
48         else
49                 for x; do
50                         while read line; do
51                                 case "$line" in
52                                 "$key"*) echo "${line##$key}";;
53                                 esac
54                         done < "$x"
55                 done
56         fi
57 }
58
59 # Support original resolvconf configuration layout
60 # as well as the openresolv config file
61 if [ -f "$SYSCONFDIR"/resolvconf.conf ]; then
62         . "$SYSCONFDIR"/resolvconf.conf
63 elif [ -d "$SYSCONFDIR"/resolvconf ]; then
64         SYSCONFDIR="$SYSCONFDIR/resolvconf/resolv.conf.d"
65         base="$SYSCONFDIR/resolv.conf.d/base"
66         if [ -f "$base" ]; then
67                 name_servers="$(key_get_value "nameserver " "$base")"
68                 search_domains="$(key_get_value "search " "$base")"
69                 if [ -z "$search_domains" ]; then
70                         search_domains="$(key_get_value "domain " "$base")"
71                 fi
72                 resolv_conf_options="$(key_get_value "options " "$base")"
73                 resolv_conf_sortlist="$(key_get_value "sortlist " "$base")"
74         fi
75         if [ -f "$SYSCONFDIR"/resolv.conf.d/head ]; then
76                 resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.d/head)"
77         fi
78         if [ -f "$SYSCONFDIR"/resolv.conf.d/tail ]; then
79                 resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.d/tail)"
80         fi
81 fi
82 : ${resolv_conf:=/etc/resolv.conf}
83 : ${libc_service:=nscd}
84 : ${libc_restart:=@RESTARTCMD ${libc_service}@}
85 : ${list_resolv:=@PREFIX@/sbin/resolvconf -l}
86 if [ "${resolv_conf_head-x}" = x -a -f "$SYSCONFDIR"/resolv.conf.head ]; then
87         resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.head)"
88 fi
89 if [ "${resolv_conf_tail-x}" = x -a -f "$SYSCONFDIR"/resolv.conf.tail ]; then
90         resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.tail)"
91 fi
92
93 uniqify()
94 {
95         local result=
96         while [ -n "$1" ]; do
97                 case " $result " in
98                 *" $1 "*);;
99                 *) result="$result $1";;
100                 esac
101                 shift
102         done
103         echo "${result# *}"
104 }
105
106 case "${resolv_conf_passthrough:-NO}" in
107 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
108         newest=
109         for conf in "$IFACEDIR"/*; do
110                 if [ -z "$newest" -o "$conf" -nt "$newest" ]; then
111                         newest="$conf"
112                 fi
113         done
114         [ -z "$newest" ] && exit 0
115         newconf="$(cat "$newest")$NL"
116         ;;
117 *)
118         [ -z "$RESOLVCONF" ] && eval "$(@PREFIX@/sbin/resolvconf -v)"
119         newsearch="$(uniqify $search_domains $SEARCH $search_domains_append)"
120         NS="$LOCALNAMESERVERS $NAMESERVERS"
121         newns="$(uniqify $name_servers $NS $name_servers_append)"
122
123         # Hold our new resolv.conf in a variable to save on temporary files
124         newconf="# Generated by resolvconf$NL"
125         if [ -n "$resolv_conf_head" ]; then
126                 newconf="$newconf$resolv_conf_head$NL"
127         fi
128         [ -n "$newsearch" ] && newconf="${newconf}search $newsearch$NL"
129         for n in $newns; do
130                 newconf="${newconf}nameserver $n$NL"
131         done
132
133         # Now get any configured options
134         opts="$resolv_conf_options${resolv_conf_options:+ }"
135         opts="$opts$($list_resolv | key_get_value "options ")"
136         if [ -n "$opts" ]; then
137                 newconf="${newconf}options"
138                 for opt in $(uniqify $opts); do
139                         newconf="${newconf} $opt"
140                 done
141                 newconf="$newconf$NL"
142         fi
143
144         # and any sortlists
145         opts="$resolv_conf_sortlist${resolv_conf_sortlist:+ }"
146         opts="$opts$($list_resolv | key_get_value "sortlist ")"
147         if [ -n "$opts" ]; then
148                 newconf="${newconf}sortlist"
149                 for opt in $(uniqify $opts); do
150                         newconf="${newconf} $opt"
151                 done
152                 newconf="$newconf$NL"
153         fi
154
155         if [ -n "$resolv_conf_tail" ]; then
156                 newconf="$newconf$resolv_conf_tail$NL"
157         fi
158         ;;
159 esac
160
161 # Check if the file has actually changed or not
162 if [ -e "$resolv_conf" ]; then
163         [ "$(cat "$resolv_conf")" = "$(printf %s "$newconf")" ] && exit 0
164 fi
165
166 # Create our resolv.conf now
167 (umask 022; printf %s "$newconf" >"$resolv_conf")
168 eval $libc_restart
169
170 retval=0
171 # Notify users of the resolver
172 for script in "$LIBEXECDIR"/libc.d/*; do
173         if [ -f "$script" ]; then
174                 if [ -x "$script" ]; then
175                         "$script" "$@"
176                 else
177                         (. "$script")
178                 fi
179                 retval=$(($retval + $?))
180         fi
181 done
182 exit $retval