a67dc82079bc3821506a536351619d4d4d32f5dd
[openresolv] / libc.in
1 #!/bin/sh
2 # Copyright 2007-2009 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 LIBEXECDIR=@LIBEXECDIR@
38 BASE="${SYSCONFDIR}/resolv.conf.base"
39
40 uniqify()
41 {
42     local result=
43     while [ -n "$1" ]; do
44                 case " ${result} " in
45                 *" $1 "*);;
46                 *) result="${result} $1";;
47                 esac
48                 shift
49         done
50     echo "${result# *}"
51 }
52
53 # sed may not be available, and this is faster on small files
54 key_get_value()
55 {
56         local key="$1" value= x= line=
57
58         shift
59         if [ $# -eq 0 ]; then
60                 while read line; do
61                         case "${line}" in
62                         "${key}"*) echo "${line##${key}}";;
63                         esac
64                 done
65         else
66                 for x; do
67                         while read line; do
68                                 case "${line}" in
69                                 "${key}"*) echo "${line##${key}}";;
70                                 esac
71                         done < "${x}"
72                 done
73         fi
74 }
75
76 NEWNS=
77 NEWSEARCH=
78 if [ -e "${BASE}" ]; then
79         NEWNS="$(key_get_value "nameserver " "${BASE}")"
80         NEWSEARCH="$(key_get_value "search " "${BASE}")"
81         if [ -z "${NEWSEARCH}" ]; then
82                 NEWSEARCH="$(key_get_value "domain " "${BASE}")"
83         fi
84 fi
85 NEWSEARCH="$(uniqify ${NEWSEARCH} ${SEARCH})"
86 NEWNS="$(uniqify ${NEWNS} ${NAMESERVERS})"
87
88 # Hold our new resolv.conf in a variable to save on temporary files
89 NEWCONF="# Generated by resolvconf\n"
90 [ -e "${SYSCONFDIR}"/resolv.conf.head ] \
91         && NEWCONF="${NEWCONF}$(cat "${SYSCONFDIR}"/resolv.conf.head)\n"
92 [ -n "${NEWSEARCH}" ] && NEWCONF="${NEWCONF}search ${NEWSEARCH}\n"
93 for N in ${NEWNS}; do
94         NEWCONF="${NEWCONF}nameserver ${N}\n"
95 done
96
97 # Now get any configured options
98 OPTS=
99 if [ -e "${BASE}" ]; then
100         OPTS="$(key_get_value "options " "${BASE}")"
101 fi
102 OPTS="${OPTS}${OPTS:+ }$(resolvconf -l | key_get_value "options ")"
103 if [ -n "${OPTS}" ]; then
104         NEWCONF="${NEWCONF}options"
105         for OPT in $(uniqify ${OPTS}); do
106                 NEWCONF="${NEWCONF} ${OPT}"
107         done
108         NEWCONF="${NEWCONF}\n"
109 fi
110
111 [ -e "${SYSCONFDIR}"/resolv.conf.tail ] \
112         && NEWCONF="${NEWCONF}$(cat "${SYSCONFDIR}"/resolv.conf.tail)\n"
113
114 # Check if the file has actually changed or not
115 if [ -e "${RESOLVCONF}" ]; then
116         [ "$(cat "${RESOLVCONF}")" = "$(printf "${NEWCONF}")" ] && exit 0
117 fi
118
119 # Create our resolv.conf now
120 (umask 022; printf "${NEWCONF}" > "${RESOLVCONF}")
121
122 resolvconf -s nscd restart
123 retval=$?
124
125 # Notify users of the resolver
126 for x in "${LIBEXECDIR}"/libc.d/*; do
127         if [ -e "${x}" ]; then
128                 "${x}" "$@"
129                 retval=$((${retval} + $?))
130         fi
131 done
132 exit ${retval}