Add .Lk macro to URLs
[openresolv] / libc.in
1 #!/bin/sh
2 # Copyright (c) 2007-2012 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 # Support original resolvconf configuration layout
76 # as well as the openresolv config file
77 if [ -f "$SYSCONFDIR"/resolvconf.conf ]; then
78         . "$SYSCONFDIR"/resolvconf.conf
79 elif [ -d "$SYSCONFDIR"/resolvconf ]; then
80         SYSCONFDIR="$SYSCONFDIR/resolvconf/resolv.conf.d"
81         base="$SYSCONFDIR/resolv.conf.d/base"
82         if [ -f "$base" ]; then
83                 name_servers="$(key_get_value "nameserver " "$base")"
84                 domain="$(key_get_value "domain " "$base")"
85                 search_domains="$(key_get_value "search " "$base")"
86                 resolv_conf_options="$(key_get_value "options " "$base")"
87                 resolv_conf_sortlist="$(key_get_value "sortlist " "$base")"
88         fi
89         if [ -f "$SYSCONFDIR"/resolv.conf.d/head ]; then
90                 resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.d/head)"
91         fi
92         if [ -f "$SYSCONFDIR"/resolv.conf.d/tail ]; then
93                 resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.d/tail)"
94         fi
95 fi
96 : ${domain:=$DOMAIN}
97 : ${resolv_conf:=/etc/resolv.conf}
98 : ${libc_service:=nscd}
99 : ${libc_restart:=@RESTARTCMD ${libc_service}@}
100 : ${list_resolv:=@PREFIX@/sbin/resolvconf -l}
101 if [ "${resolv_conf_head-x}" = x -a -f "$SYSCONFDIR"/resolv.conf.head ]; then
102         resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.head)"
103 fi
104 if [ "${resolv_conf_tail-x}" = x -a -f "$SYSCONFDIR"/resolv.conf.tail ]; then
105         resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.tail)"
106 fi
107
108 uniqify()
109 {
110         local result=
111         while [ -n "$1" ]; do
112                 case " $result " in
113                 *" $1 "*);;
114                 *) result="$result $1";;
115                 esac
116                 shift
117         done
118         echo "${result# *}"
119 }
120
121 case "${resolv_conf_passthrough:-NO}" in
122 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
123         newest=
124         for conf in "$IFACEDIR"/*; do
125                 if [ -z "$newest" -o "$conf" -nt "$newest" ]; then
126                         newest="$conf"
127                 fi
128         done
129         [ -z "$newest" ] && exit 0
130         newconf="$(cat "$newest")$NL"
131         ;;
132 *)
133         [ -z "$RESOLVCONF" ] && eval "$(@PREFIX@/sbin/resolvconf -v)"
134         newsearch="$(uniqify $search_domains $SEARCH $search_domains_append)"
135         NS="$LOCALNAMESERVERS"
136         case "${resolv_conf_local_only:-1}" in
137         [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) ;;
138         *) NS="$NS $NAMESERVERS";;
139         esac
140         newns="$(uniqify $name_servers $NS $name_servers_append)"
141
142         # Hold our new resolv.conf in a variable to save on temporary files
143         newconf="# Generated by resolvconf$NL"
144         if [ -n "$resolv_conf_head" ]; then
145                 newconf="$newconf$resolv_conf_head$NL"
146         fi
147
148         [ -n "$domain" ] && newconf="${newconf}domain $domain$NL"
149         if [ -n "$newsearch" -a "$newsearch" != "$domain" ]; then
150                 newconf="${newconf}search $newsearch$NL"
151         fi
152         for n in $newns; do
153                 newconf="${newconf}nameserver $n$NL"
154         done
155
156         # Now add anything we don't care about such as sortlist and options
157         stuff="$($list_resolv | keys_remove nameserver domain search)"
158         if [ -n "$stuff" ]; then
159                 newconf="$newconf$stuff$NL"
160         fi
161
162         # Append any user defined ones
163         if [ -n "$resolv_conf_options" ]; then
164                 newconf="${newconf}options $resolv_conf_options$NL"
165         fi
166         if [ -n "$resolv_conf_sortlist" ]; then
167                 newconf="${newconf}sortlist $resolv_conf_sortlist$NL"
168         fi
169
170         if [ -n "$resolv_conf_tail" ]; then
171                 newconf="$newconf$resolv_conf_tail$NL"
172         fi
173         ;;
174 esac
175
176 # Check if the file has actually changed or not
177 if [ -e "$resolv_conf" ]; then
178         [ "$(cat "$resolv_conf")" = "$(printf %s "$newconf")" ] && exit 0
179 fi
180
181 # Create our resolv.conf now
182 (umask 022; printf %s "$newconf" >"$resolv_conf")
183 eval $libc_restart
184
185 retval=0
186 # Notify users of the resolver
187 for script in "$LIBEXECDIR"/libc.d/*; do
188         if [ -f "$script" ]; then
189                 if [ -x "$script" ]; then
190                         "$script" "$@"
191                 else
192                         (. "$script")
193                 fi
194                 retval=$(($retval + $?))
195         fi
196 done
197 exit $retval