Use Aq macro
[openresolv] / libc.in
1 #!/bin/sh
2 # Copyright 2007-2008 Roy Marples
3 # All rights reserved
4
5 # libc subscriber for resolvconf
6 # You could symlink /etc/resolv.conf to resolvconf/run/resolv.conf
7 # for a read only etc, if resolvconf/run is linked to /var/run/resolvconf.
8
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #     * Redistributions of source code must retain the above copyright
13 #       notice, this list of conditions and the following disclaimer.
14 #     * Redistributions in binary form must reproduce the above
15 #       copyright notice, this list of conditions and the following
16 #       disclaimer in the documentation and/or other materials provided
17 #       with the distribution.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 # Load our variables from resolvconf
32 eval "$(resolvconf -v)"
33
34 RESOLVCONF=/etc/resolv.conf
35 PREFIX=@PREFIX@
36 SYSCONFDIR=@SYSCONFDIR@
37 BASE="${SYSCONFDIR}/resolv.conf.d/base"
38
39 uniqify()
40 {
41     local result=
42     while [ -n "$1" ]; do
43                 case " ${result} " in
44                 *" $1 "*);;
45                 *) result="${result} $1";;
46                 esac
47                 shift
48         done
49     echo "${result# *}"
50 }
51
52 # sed may not be available, and this is faster on small files
53 key_get_value()
54 {
55         local key="$1" value= x= line=
56
57         shift
58         if [ $# -eq 0 ]; then
59                 while read line; do
60                         case "${line}" in
61                         "${key}"*) echo "${line##${key}}";;
62                         esac
63                 done
64         else
65                 for x; do
66                         while read line; do
67                                 case "${line}" in
68                                 "${key}"*) echo "${line##${key}}";;
69                                 esac
70                         done < "${x}"
71                 done
72         fi
73 }
74
75 NEWNS=
76 NEWSEARCH=
77 if [ -e "${BASE}" ]; then
78         NEWNS="$(key_get_value "nameserver " "${BASE}")"
79         NEWSEARCH="$(key_get_value "search " "${BASE}")"
80         if [ -z "${NEWSEARCH}" ]; then
81                 NEWSEARCH="$(key_get_value "domain " "${BASE}")"
82         fi
83 fi
84 NEWSEARCH="$(uniqify ${NEWSEARCH} ${SEARCH})"
85 NEWNS="$(uniqify ${NEWNS} ${NAMESERVERS})"
86
87 # Hold our new resolv.conf in a variable to save on temporary files
88 NEWCONF=""
89 [ -e "${SYSCONFDIR}"/resolv.conf.d/head ] \
90         && NEWCONF="${NEWCONF}$(cat "${SYSCONFDIR}"/resolv.conf.d/head)\n"
91 [ -n "${NEWSEARCH}" ] && NEWCONF="${NEWCONF}search ${NEWSEARCH}\n"
92 for N in ${NEWNS}; do
93         NEWCONF="${NEWCONF}nameserver ${N}\n"
94 done
95
96 # Now get any configured options
97 OPTS=
98 if [ -e "${BASE}" ]; then
99         OPTS="$(key_get_value "options " "${BASE}")"
100 fi
101 OPTS="${OPTS}${OPTS:+ }$(resolvconf -l | key_get_value "options ")"
102 if [ -n "${OPTS}" ]; then
103         NEWCONF="${NEWCONF}options"
104         for OPT in $(uniqify ${OPTS}); do
105                 NEWCONF="${NEWCONF} ${OPT}"
106         done
107         NEWCONF="${NEWCONF}\n"
108 fi
109
110 [ -e "${SYSCONFDIR}"/resolv.conf.d/tail ] \
111         && NEWCONF="${NEWCONF}$(cat "${SYSCONFDIR}"/resolv.conf.d/tail)\n"
112
113 # Check if the file has actually changed or not
114 if [ -e "${RESOLVCONF}" ]; then
115         [ "$(cat "${RESOLVCONF}")" = "$(printf "${NEWCONF}")" ] && exit 0
116 fi
117
118 # Create our resolv.conf now
119 (umask 022; printf "${NEWCONF}" > "${RESOLVCONF}")
120
121 resolvconf -s nscd restart
122 retval=$?
123
124 # Notify users of the resolver
125 for x in "${SYSCONFDIR}"/update-libc.d/*; do
126         if [ -e "${x}" ]; then
127                 "${x}" "$@"
128                 retval=$((${retval} + $?))
129         fi
130 done
131 exit ${retval}