resetting manifest requested domain to floor
[platform/upstream/nss-mdns.git] / packaging / nss-mdns-config
1 #!/bin/sh
2 # vim: set ts=2 sw=2 et:
3
4 #
5 # Copyright (c) 2012, Novell, Inc.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
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.
19 #
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.
31 #
32 #
33 # (Licensed under the simplified BSD license)
34 #
35 # Authors: Vincent Untz <vuntz@opensuse.org>
36 #
37
38 NSSWITCH=/etc/nsswitch.conf
39
40 function usage() {
41   echo "nss-mdns-config [--enable|--disable] [-4|-6] [--no-backup]"
42 }
43
44 ENABLE=0
45 DISABLE=0
46 IPv4=0
47 IPv6=0
48 IPvALL=0
49 BACKUP=1
50
51 while test $# -gt 0; do
52   case "$1" in
53     --enable) ENABLE=1; shift;;
54     --disable) DISABLE=1; shift;;
55     -4) IPv4=1; shift;;
56     -6) IPv6=1; shift;;
57     --no-backup) BACKUP=0; shift;;
58     *) usage; exit 1;;
59   esac
60 done
61
62 if test "$ENABLE" -eq 1 -a "$DISABLE" -eq 1; then
63   usage
64   exit 1
65 fi
66
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.
71   usage
72   exit 1
73 fi
74
75 if test "$IPv4" -ne 1 -a "$IPv6" -ne 1; then
76   IPv4=1
77   IPv6=1
78   IPvALL=1
79 fi
80
81 if test ! -f "$NSSWITCH"; then
82   if test "$ENABLE" -ne 1 -a "$DISABLE" -ne 1; then
83     echo "No $NSSWITCH file."
84   fi
85   exit 0
86 fi
87
88 HOSTS=$(grep -m1 "^\s*hosts:" "$NSSWITCH")
89
90 if test $? -ne 0; then
91   echo "No hosts configuration in $NSSWITCH."
92   exit 0
93 fi
94
95 VALUE=$(echo $HOSTS | sed "s/^\s*hosts:\s*//g")
96
97 if test "$ENABLE" -ne 1 -a "$DISABLE" -ne 1; then
98
99   IPv4_ENABLED=0
100   IPv6_ENABLED=0
101   ENABLED=0
102
103   for ITEM in $VALUE; do
104     if test "$ITEM" == "mdns4_minimal" -o "$ITEM" == "mdns4"; then
105       IPv4_ENABLED=1
106     elif test "$ITEM" == "mdns6_minimal" -o "$ITEM" == "mdns6"; then
107       IPv6_ENABLED=1
108     elif test "$ITEM" == "mdns_minimal" -o "$ITEM" == "mdns"; then
109       ENABLED=1
110     fi
111   done
112
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."
121   else
122     echo "Support for nss-mdns is disabled."
123   fi
124
125 elif test "$DISABLE" -eq 1; then
126
127   NEWVALUE=""
128   DROPACTION=0
129
130   for ITEM in $VALUE; do
131     if test \( "$ITEM" == "mdns4_minimal" -o "$ITEM" == "mdns4" \) -a "$IPv4" -eq 1; then
132       DROPACTION=1
133     elif test \( "$ITEM" == "mdns6_minimal" -o "$ITEM" == "mdns6" \) -a "$IPv6" -eq 1; then
134       DROPACTION=1
135     elif test "$ITEM" == "mdns_minimal" -o "$ITEM" == "mdns"; then
136       if test "$IPvALL" -eq 1; then
137         DROPACTION=1
138       elif test "$IPv4" -eq 1; then
139         NEWVALUE="$NEWVALUE `echo $ITEM | sed 's:mdns:mdns6:'`"
140         DROPACTION=0
141       elif test "$IPv6" -eq 1; then
142         NEWVALUE="$NEWVALUE `echo $ITEM | sed 's:mdns:mdns4:'`"
143         DROPACTION=0
144       else
145         echo "Internal error when disabling $ITEM."
146         exit 1
147       fi
148     elif test "${ITEM::1}" == "[" -a "$DROPACTION" -eq 1; then
149       DROPACTION=0
150     else
151       NEWVALUE="$NEWVALUE $ITEM"
152       DROPACTION=0
153     fi
154   done
155
156   NEWVALUE=$(echo $NEWVALUE | sed "s/^\s*//;s/^\s*$//")
157
158   if test "$BACKUP" -eq 1; then
159     cp -a "$NSSWITCH" "${NSSWITCH}bak"
160   fi
161   sed -i "s/\(^\s*hosts:\s*\).*/\1$NEWVALUE/" "$NSSWITCH"
162
163 elif test "$ENABLE" -eq 1; then
164
165   NEWVALUE=""
166   FOUND=0
167   FOUND_DNS=0
168   DROPACTION=0
169
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
174         DROPACTION=1
175       else
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
178           DROPACTION=1
179         else
180           if test "$ITEM" == "mdns_minimal"; then
181             FOUND=1
182           elif test "$ITEM" == "mdns4_minimal" -a "$IPv4" -eq 1; then
183             FOUND=1
184           elif test "$ITEM" == "mdns6_minimal" -a "$IPv6" -eq 1; then
185             FOUND=1
186           fi
187           # force the use of [NOTFOUND=return] after *_minimal
188           NEWVALUE="$NEWVALUE $ITEM [NOTFOUND=return]"
189           DROPACTION=1
190         fi
191       fi
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
194       DROPACTION=1
195     elif test "$ITEM" == "dns"; then
196       FOUND_DNS=1
197       DROPACTION=0
198       if test "$FOUND" -ne 1; then
199         FOUND=1
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"
206         else
207           echo "Internal error when enabling."
208           exit 1
209         fi
210       else
211         NEWVALUE="$NEWVALUE $ITEM"
212       fi
213     elif test "${ITEM::1}" == "[" -a "$DROPACTION" -eq 1; then
214       DROPACTION=0
215     else
216       NEWVALUE="$NEWVALUE $ITEM"
217       DROPACTION=0
218     fi
219   done
220
221   if test "$FOUND_DNS" -ne 1; then
222     echo "No dns service for hosts configuration in $NSSWITCH."
223     exit 0
224   fi
225
226   NEWVALUE=$(echo $NEWVALUE | sed "s/^\s*//;s/^\s*$//")
227
228   if test "$BACKUP" -eq 1; then
229     cp -a "$NSSWITCH" "${NSSWITCH}bak"
230   fi
231   sed -i "s/\(^\s*hosts:\s*\).*/\1$NEWVALUE/" "$NSSWITCH"
232
233 fi