Add override for setuid mount.nfs
[nfs-utils.git] / debian / nfs-common.init
1 #!/bin/bash
2
3 ### BEGIN INIT INFO
4 # Provides:          nfs-common
5 # Required-Start:    $portmap $time
6 # Required-Stop:     $time
7 # Default-Start:     2 3 4 5 S
8 # Default-Stop:      0 1 6
9 # Short-Description: NFS support files common to client and server
10 # Description:       NFS is a popular protocol for file sharing across
11 #                    TCP/IP networks. This service provides various
12 #                    support functions for NFS mounts.
13 ### END INIT INFO
14
15 # What is this?
16 DESC="NFS common utilities"
17
18 # Read config
19 DEFAULTFILE=/etc/default/nfs-common
20 PREFIX=
21 NEED_STATD=
22 NEED_IDMAPD=
23 NEED_GSSD=
24 PIPEFS_MOUNTPOINT=/var/lib/nfs/rpc_pipefs
25 RPCGSSDOPTS=
26 if [ -f $DEFAULTFILE ]; then
27     . $DEFAULTFILE
28 fi
29
30 . /lib/lsb/init-functions
31
32 # Exit if required binaries are missing.
33 [ -x $PREFIX/sbin/rpc.statd ] || exit 0
34
35 #
36 # Parse the fstab file, and determine whether we need idmapd and gssd. (The
37 # /etc/defaults settings, if any, will override our autodetection.) This code
38 # is partially adapted from the mountnfs.sh script in the sysvinit package.
39 #
40 AUTO_NEED_IDMAPD=no
41 AUTO_NEED_GSSD=no
42
43 if [ -f /etc/fstab ]; then
44     exec 9<&0 </etc/fstab
45
46     while read DEV MTPT FSTYPE OPTS REST
47     do
48         case $DEV in
49             ''|\#*)
50                 continue
51                 ;;
52         esac
53         # FSTYPE "nfs" can be NFSv4 now
54         #if [ "$FSTYPE" = "nfs4" ]; then
55             AUTO_NEED_IDMAPD=yes
56         #fi
57         case "$OPTS" in
58
59             sec=krb5|*,sec=krb5|sec=krb5,*|*,sec=krb5i,*|sec=krb5i|*,sec=krb5i|sec=krb5i,*|*,sec=krb5i,*|sec=krb5p|*,sec=krb5p|sec=krb5p,*|*,sec=krb5p,*)
60             AUTO_NEED_GSSD=yes
61             ;;
62         esac
63     done
64
65     exec 0<&9 9<&-
66 fi
67
68 #
69 # We also need idmapd if we run an NFSv4 server. It's fairly difficult
70 # to autodetect whether there are NFSv4 exports or not, and idmapd is not a
71 # particularily heavy daemon, so we auto-enable it if we find an /etc/exports
72 # file. This does not mean that there are NFSv4 or other mounts active (or
73 # even that nfs-kernel-server is installed), but it matches what the "start"
74 # condition in nfs-kernel-server's init script does, which has a value in
75 # itself.
76 #
77 if [ -f /etc/exports ] && grep -q '^[[:space:]]*[^#]*/' /etc/exports; then
78     AUTO_NEED_IDMAPD=yes
79 fi
80
81 case "$NEED_STATD" in
82     yes|no)
83         ;;
84     *)
85         NEED_STATD=yes
86         ;;
87 esac
88
89 case "$NEED_IDMAPD" in
90     yes|no)     
91         ;;
92     *)
93         NEED_IDMAPD=$AUTO_NEED_IDMAPD
94         ;;
95 esac
96
97 case "$NEED_GSSD" in
98     yes|no)     
99         ;;
100     *)
101         NEED_GSSD=$AUTO_NEED_GSSD
102         ;;
103 esac
104
105 do_modprobe() {
106     if [ -x /sbin/modprobe -a -f /proc/modules ]
107     then
108         modprobe -q "$1" || true
109     fi
110 }
111
112 do_mount() {
113     if ! grep -E -qs "$1\$" /proc/filesystems
114     then
115         return 1
116     fi
117     if ! mountpoint -q "$2"
118     then
119         mount -t "$1" "$1" "$2"
120         return
121     fi
122     return 0
123 }
124
125 do_umount() {
126     if mountpoint -q "$1"
127     then
128         umount "$1"
129     fi
130     return 0
131 }
132
133 # See how we were called.
134 case "$1" in
135   start)
136         log_daemon_msg "Starting $DESC"
137
138         if [ "$NEED_STATD" = yes ]; then
139             log_progress_msg "statd"
140             
141             # See if rpcbind is running
142             /usr/sbin/rpcinfo -p >/dev/null 2>&1
143             RET=$?
144             if [ $RET != 0 ]; then
145                 echo
146                 log_warning_msg "Not starting: portmapper is not running"
147                 exit 0
148             fi
149
150             start-stop-daemon --start --oknodo --quiet \
151                 --pidfile /var/run/rpc.statd.pid \
152                 --exec $PREFIX/sbin/rpc.statd -- $STATDOPTS
153             RET=$?
154             if [ $RET != 0 ]; then
155                 log_end_msg $RET
156                 exit $RET
157             else
158                 mkdir -p /lib/init/rw/sendsigs.omit.d
159                 rm -f /lib/init/rw/sendsigs.omit.d/statd
160                 ln -s /var/run/rpc.statd.pid /lib/init/rw/sendsigs.omit.d/statd
161             fi
162         fi
163
164         # Don't start idmapd and gssd if we don't have them (say, if /usr is not
165         # up yet).
166         [ -x /usr/sbin/rpc.idmapd ] || NEED_IDMAPD=no
167         [ -x /usr/sbin/rpc.gssd   ] || NEED_GSSD=no
168
169         if [ "$NEED_IDMAPD" = yes ] || [ "$NEED_GSSD" = yes ]
170         then
171             do_modprobe sunrpc
172             do_modprobe nfs
173             do_modprobe nfsd
174             if do_mount rpc_pipefs $PIPEFS_MOUNTPOINT
175             then
176                 if [ "$NEED_IDMAPD" = yes ]
177                 then
178                     log_progress_msg "idmapd"
179                     start-stop-daemon --start --oknodo --quiet \
180                             --exec /usr/sbin/rpc.idmapd
181                     RET=$?
182                     if [ $RET != 0 ]; then
183                         log_end_msg $RET
184                         exit $RET
185                     fi
186                 fi
187                 if [ "$NEED_GSSD" = yes ]
188                 then
189                     do_modprobe rpcsec_gss_krb5
190                     log_progress_msg "gssd"
191
192                     # we need this available; better to fail now than
193                     # mysteriously on the first mount
194                     if ! grep -q -E '^nfs[       ]' /etc/services; then
195                         log_action_end_msg 1 "broken /etc/services, please see /usr/share/doc/nfs-common/README.Debian.nfsv4"
196                         exit 1
197                     fi
198
199                     start-stop-daemon --start --oknodo --quiet \
200                             --exec /usr/sbin/rpc.gssd -- $RPCGSSDOPTS
201                     RET=$?
202                     if [ $RET != 0 ]; then
203                         log_end_msg $RET
204                         exit $RET
205                     fi
206                 fi
207             fi
208         fi
209         log_end_msg 0
210         ;;
211
212   stop)
213         log_daemon_msg "Stopping $DESC"
214
215         if [ "$NEED_GSSD" = yes ]
216         then
217             log_progress_msg "gssd"
218             start-stop-daemon --stop --oknodo --quiet \
219                     --name rpc.gssd
220             RET=$?
221             if [ $RET != 0 ]; then
222                 log_end_msg $RET
223                 exit $RET
224             fi
225         fi
226         if [ "$NEED_IDMAPD" = yes ]
227         then
228             log_progress_msg "idmapd"
229             start-stop-daemon --stop --oknodo --quiet \
230                 --name rpc.idmapd
231             RET=$?
232             if [ $RET != 0 ]; then
233                 log_end_msg $RET
234                 exit $RET
235             fi
236         fi
237         if [ "$NEED_STATD" = yes ]
238         then
239             log_progress_msg "statd"
240             start-stop-daemon --stop --oknodo --quiet \
241                 --name rpc.statd
242             RET=$?
243             if [ $RET != 0 ]; then
244                 log_end_msg $RET
245                 exit $RET
246             fi
247         fi
248         do_umount $PIPEFS_MOUNTPOINT 2>/dev/null || true
249         log_end_msg 0
250         ;;
251
252   status)
253         if [ "$NEED_STATD" = yes ]
254         then
255             if ! pidof rpc.statd >/dev/null
256             then
257                 echo "rpc.statd not running"
258                 exit 3
259             fi
260         fi
261
262         if [ "$NEED_GSSD" = yes ]
263         then
264             if ! pidof rpc.gssd >/dev/null
265             then
266                 echo "rpc.gssd not running"
267                 exit 3
268             fi
269         fi
270
271         if [ "$NEED_IDMAPD" = yes ]
272         then
273             if ! pidof rpc.idmapd >/dev/null
274             then
275                 echo "rpc.idmapd not running"
276                 exit 3
277             fi
278         fi
279
280         echo "all daemons running"
281         exit 0
282         ;;
283
284   restart | force-reload)
285         $0 stop
286         sleep 1
287         $0 start
288         ;;
289
290   *)
291         log_success_msg "Usage: nfs-common {start|stop|status|restart}"
292         exit 1
293         ;;
294 esac
295
296 exit 0