2 # vim: set ts=2 sw=2 et:
5 # Copyright (c) 2012, Novell, Inc.
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
11 # * Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 # * Neither the name of the <ORGANIZATION> nor the names of its contributors
17 # may be used to endorse or promote products derived from this software
18 # without specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
33 # (Licensed under the simplified BSD license)
35 # Authors: Vincent Untz <vuntz@opensuse.org>
38 NSSWITCH=/etc/nsswitch.conf
41 echo "nss-mdns-config [--enable|--disable] [-4|-6] [--no-backup]"
51 while test $# -gt 0; do
53 --enable) ENABLE=1; shift;;
54 --disable) DISABLE=1; shift;;
57 --no-backup) BACKUP=0; shift;;
62 if test "$ENABLE" -eq 1 -a "$DISABLE" -eq 1; then
67 if test "$IPv4" -eq 1 -a "$IPv6" -eq 1; then
68 # Supporting -4 and -6 at the same time would make --enable more complex,
69 # since we'd like it to add both mdns4_minimal and mdns6_minimal. So just
70 # don't do it for now.
75 if test "$IPv4" -ne 1 -a "$IPv6" -ne 1; then
81 if test ! -f "$NSSWITCH"; then
82 if test "$ENABLE" -ne 1 -a "$DISABLE" -ne 1; then
83 echo "No $NSSWITCH file."
88 HOSTS=$(grep -m1 "^\s*hosts:" "$NSSWITCH")
90 if test $? -ne 0; then
91 echo "No hosts configuration in $NSSWITCH."
95 VALUE=$(echo $HOSTS | sed "s/^\s*hosts:\s*//g")
97 if test "$ENABLE" -ne 1 -a "$DISABLE" -ne 1; then
103 for ITEM in $VALUE; do
104 if test "$ITEM" == "mdns4_minimal" -o "$ITEM" == "mdns4"; then
106 elif test "$ITEM" == "mdns6_minimal" -o "$ITEM" == "mdns6"; then
108 elif test "$ITEM" == "mdns_minimal" -o "$ITEM" == "mdns"; then
113 if test "$ENABLED" -eq 1; then
114 echo "Full support for nss-mdns is enabled."
115 elif test "$IPv4_ENABLED" -eq 1 -a "$IPv6_ENABLED" -eq 1; then
116 echo "Support for nss-mdns is enabled for IPv4 and IPv6."
117 elif test "$IPv4_ENABLED" -eq 1; then
118 echo "Support for nss-mdns is enabled for IPv4."
119 elif test "$IPv6_ENABLED" -eq 1; then
120 echo "Support for nss-mdns is enabled for IPv6."
122 echo "Support for nss-mdns is disabled."
125 elif test "$DISABLE" -eq 1; then
130 for ITEM in $VALUE; do
131 if test \( "$ITEM" == "mdns4_minimal" -o "$ITEM" == "mdns4" \) -a "$IPv4" -eq 1; then
133 elif test \( "$ITEM" == "mdns6_minimal" -o "$ITEM" == "mdns6" \) -a "$IPv6" -eq 1; then
135 elif test "$ITEM" == "mdns_minimal" -o "$ITEM" == "mdns"; then
136 if test "$IPvALL" -eq 1; then
138 elif test "$IPv4" -eq 1; then
139 NEWVALUE="$NEWVALUE `echo $ITEM | sed 's:mdns:mdns6:'`"
141 elif test "$IPv6" -eq 1; then
142 NEWVALUE="$NEWVALUE `echo $ITEM | sed 's:mdns:mdns4:'`"
145 echo "Internal error when disabling $ITEM."
148 elif test "${ITEM::1}" == "[" -a "$DROPACTION" -eq 1; then
151 NEWVALUE="$NEWVALUE $ITEM"
156 NEWVALUE=$(echo $NEWVALUE | sed "s/^\s*//;s/^\s*$//")
158 if test "$BACKUP" -eq 1; then
159 cp -a "$NSSWITCH" "${NSSWITCH}bak"
161 sed -i "s/\(^\s*hosts:\s*\).*/\1$NEWVALUE/" "$NSSWITCH"
163 elif test "$ENABLE" -eq 1; then
170 for ITEM in $VALUE; do
171 if test \( "$ITEM" == "mdns_minimal" -o "$ITEM" == "mdns4_minimal" -o "$ITEM" == "mdns6_minimal" \); then
172 if test "$FOUND_DNS" -eq 1; then
173 # don't keep *_minimal after dns; it's not desired there
176 if test "$IPvALL" -eq 1 -a \( "$ITEM" == "mdns4_minimal" -o "$ITEM" == "mdns6_minimal" \); then
177 # no need to keep mdns[46]_minimal since we'll add mdns_minimal
180 if test "$ITEM" == "mdns_minimal"; then
182 elif test "$ITEM" == "mdns4_minimal" -a "$IPv4" -eq 1; then
184 elif test "$ITEM" == "mdns6_minimal" -a "$IPv6" -eq 1; then
187 # force the use of [NOTFOUND=return] after *_minimal
188 NEWVALUE="$NEWVALUE $ITEM [NOTFOUND=return]"
192 elif test \( "$ITEM" == "mdns" -o "$ITEM" == "mdns4" -o "$ITEM" == "mdns6" \); then
193 # we simply don't use the non-minimal version in our setup, so drop it
195 elif test "$ITEM" == "dns"; then
198 if test "$FOUND" -ne 1; then
200 if test "$IPvALL" -eq 1; then
201 NEWVALUE="$NEWVALUE mdns_minimal [NOTFOUND=return] $ITEM"
202 elif test "$IPv4" -eq 1; then
203 NEWVALUE="$NEWVALUE mdns4_minimal [NOTFOUND=return] $ITEM"
204 elif test "$IPv6" -eq 1; then
205 NEWVALUE="$NEWVALUE mdns6_minimal [NOTFOUND=return] $ITEM"
207 echo "Internal error when enabling."
211 NEWVALUE="$NEWVALUE $ITEM"
213 elif test "${ITEM::1}" == "[" -a "$DROPACTION" -eq 1; then
216 NEWVALUE="$NEWVALUE $ITEM"
221 if test "$FOUND_DNS" -ne 1; then
222 echo "No dns service for hosts configuration in $NSSWITCH."
226 NEWVALUE=$(echo $NEWVALUE | sed "s/^\s*//;s/^\s*$//")
228 if test "$BACKUP" -eq 1; then
229 cp -a "$NSSWITCH" "${NSSWITCH}bak"
231 sed -i "s/\(^\s*hosts:\s*\).*/\1$NEWVALUE/" "$NSSWITCH"