summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/nfs/svc/nfs-server
blob: bf8e73be98d25db1c7984265a77a65f75390e00b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/sbin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
# Copyright 2016 Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
# Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
#

# Start/stop processes required for server NFS

. /lib/svc/share/smf_include.sh
. /lib/svc/share/ipf_include.sh
zone=`smf_zonename`

#
# Handling a corner case here. If we were in offline state due to an
# unsatisfied dependency, the ipf_method process wouldn't have generated
# the ipfilter configuration. When we transition to online because the
# dependency is satisfied, the start method will have to generate the
# ipfilter configuration. To avoid all possible deadlock scenarios,
# we restart ipfilter which will regenerate the ipfilter configuration
# for the entire system.
#
# The ipf_method process signals that it didn't generate ipf rules by
# removing the service's ipf file. Thus we only restart network/ipfilter
# when the file is missing.
#
configure_ipfilter()
{
	ipfile=`fmri_to_file $SMF_FMRI $IPF_SUFFIX`
	ip6file=`fmri_to_file $SMF_FMRI $IPF6_SUFFIX`
	[ -f "$ipfile" -a -f "$ip6file" ] && return 0

        #
	# Nothing to do if:
        # - ipfilter isn't online
	# - global policy is 'custom'
	# - service's policy is 'use_global'
        #
        service_check_state $IPF_FMRI $SMF_ONLINE || return 0
        [ "`get_global_def_policy`" = "custom" ] && return 0
	[ "`get_policy $SMF_FMRI`" = "use_global" ] && return 0

	svcadm restart $IPF_FMRI
}

case "$1" in
'start')
	# Share all file systems enabled for sharing. sharemgr understands
	# regular shares and ZFS shares and will handle both. Technically,
	# the shares would have been started long before getting here since
	# nfsd has a dependency on them.

	# restart stopped shares from the repository
	/usr/sbin/sharemgr start -P nfs -a

	# Options for nfsd are now set in SMF

	/usr/lib/nfs/mountd
	rc=$?
	if [ $rc != 0 ]; then
		/usr/sbin/svcadm mark -t maintenance svc:/network/nfs/server
		echo "$0: mountd failed with $rc"
		sleep 5 &
		exit $SMF_EXIT_ERR_FATAL
	fi

	/usr/lib/nfs/nfsd
	rc=$?
	if [ $rc != 0 ]; then
		/usr/sbin/svcadm mark -t maintenance svc:/network/nfs/server
		echo "$0: nfsd failed with $rc"
		sleep 5 &
		exit $SMF_EXIT_ERR_FATAL
	fi

	configure_ipfilter
	;;

'refresh')
	/usr/sbin/sharemgr start -P nfs -a
	;;

'stop')
	/usr/bin/pkill -x -u 0,1 -z $zone '(nfsd|mountd)'

	# Unshare all shared file systems using NFS

	/usr/sbin/sharemgr stop -P nfs -a

	# Kill any processes left in service contract
	smf_kill_contract $2 TERM 1
	[ $? -ne 0 ] && exit 1
	;;

'ipfilter')
	#
	# NFS related services are RPC. nfs/server has nfsd which has
	# well-defined port number but mountd is an RPC daemon.
	#
	# Essentially, we generate rules for the following "services"
	#  - nfs/server which has nfsd and mountd
	#  - nfs/rquota
	#
	# The following services are enabled for both nfs client and
	# server, if nfs/client is enabled we'll treat them as client
	# services and simply allow incoming traffic.
	#  - nfs/status
	#  - nfs/nlockmgr
	#  - nfs/cbd
	#
	NFS_FMRI="svc:/network/nfs/server:default"
	NFSCLI_FMRI="svc:/network/nfs/client:default"
	RQUOTA_FMRI="svc:/network/nfs/rquota:default"
	FMRI=$2

	file=`fmri_to_file $FMRI $IPF_SUFFIX`
	file6=`fmri_to_file $FMRI $IPF6_SUFFIX`
	echo "# $FMRI" >$file
	echo "# $FMRI" >$file6
	policy=`get_policy $NFS_FMRI`

	#
	# nfs/server configuration is processed in the start method.
	#
	if [ "$FMRI" = "$NFS_FMRI" ]; then
		service_check_state $FMRI $SMF_ONLINE
		if [ $? -ne 0 ]; then
			rm  $file
			exit $SMF_EXIT_OK
		fi

		nfs_name=`svcprop -p $FW_CONTEXT_PG/name $FMRI 2>/dev/null`
		tport=`$SERVINFO -p -t -s $nfs_name 2>/dev/null`
		if [ -n "$tport" ]; then
			generate_rules $FMRI $policy "tcp" $tport $file
		fi

		tport6=`$SERVINFO -p -t6 -s $nfs_name 2>/dev/null`
		if [ -n "$tport6" ]; then
			generate_rules $FMRI $policy "tcp" $tport6 $file6 _6
		fi

		uport=`$SERVINFO -p -u -s $nfs_name 2>/dev/null`
		if [ -n "$uport" ]; then
			generate_rules $FMRI $policy "udp" $uport $file
		fi

		uport6=`$SERVINFO -p -u6 -s $nfs_name 2>/dev/null`
		if [ -n "$uport6" ]; then
			generate_rules $FMRI $policy "udp" $uport6 $file6 _6
		fi

		# mountd IPv6 ports are also reachable through IPv4, so include
		# them when generating IPv4 rules.
		tports=`$SERVINFO -R -p -t -s "mountd" 2>/dev/null`
		tports6=`$SERVINFO -R -p -t6 -s "mountd" 2>/dev/null`
		if [ -n "$tports" -o -n "$tports6" ]; then
			tports=`unique_ports $tports $tports6`
			for tport in $tports; do
				generate_rules $FMRI $policy "tcp" \
				    $tport $file
			done
		fi

		if [ -n "$tports6" ]; then
			for tport6 in $tports6; do
				generate_rules $FMRI $policy "tcp" \
				    $tport6 $file6 _6
			done
		fi

		uports=`$SERVINFO -R -p -u -s "mountd" 2>/dev/null`
		uports6=`$SERVINFO -R -p -u6 -s "mountd" 2>/dev/null`
		if [ -n "$uports" -o -n "$uports6" ]; then
			uports=`unique_ports $uports $uports6`
			for uport in $uports; do
				generate_rules $FMRI $policy "udp" \
				    $uport $file
			done
		fi

		if [ -n "$uports6" ]; then
			for uport6 in $uports6; do
				generate_rules $FMRI $policy "udp" \
				    $uport6 $file6 _6
			done
		fi

	elif [ "$FMRI" = "$RQUOTA_FMRI" ]; then
		iana_name=`svcprop -p inetd/name $FMRI`

		# rquota IPv6 ports are also reachable through IPv4, so include
		# them when generating IPv4 rules.
		tports=`$SERVINFO -R -p -t -s $iana_name 2>/dev/null`
		tports6=`$SERVINFO -R -p -t6 -s $iana_name 2>/dev/null`
		if [ -n "$tports" -o -n "$tports6" ]; then
			tports=`unique_ports $tports $tports6`
			for tport in $tports; do
				generate_rules $NFS_FMRI $policy "tcp" \
				    $tport $file
			done
		fi

		if [ -n "$tports6" ]; then
			for tport6 in $tports6; do
				generate_rules $NFS_FMRI $policy "tcp" \
				    $tport6 $file6 _6
			done
		fi

		uports=`$SERVINFO -R -p -u -s $iana_name 2>/dev/null`
		uports6=`$SERVINFO -R -p -u6 -s $iana_name 2>/dev/null`
		if [ -n "$uports" -o -n "$uports6" ]; then
			uports=`unique_ports $uports $uports6`
			for uport in $uports; do
				generate_rules $NFS_FMRI $policy "udp" \
				    $uport $file
			done
		fi

		if [ -n "$uports6" ]; then
			for uport6 in $uports6; do
				generate_rules $NFS_FMRI $policy "udp" \
				    $uport6 $file6 _6
			done
		fi
	else
		#
		# Handle the client services here
		#
		if service_check_state $NFSCLI_FMRI $SMF_ONLINE; then
			policy=none
			ip=any
		fi

		restarter=`svcprop -p general/restarter $FMRI 2>/dev/null`
		if [ "$restarter" = "$INETDFMRI" ]; then
			iana_name=`svcprop -p inetd/name $FMRI`
			isrpc=`svcprop -p inetd/isrpc $FMRI`
		else
			iana_name=`svcprop -p $FW_CONTEXT_PG/name $FMRI`
			isrpc=`svcprop -p $FW_CONTEXT_PG/isrpc $FMRI`
		fi

		if [ "$isrpc" = "true" ]; then
			tports=`$SERVINFO -R -p -t -s $iana_name 2>/dev/null`
			tports6=`$SERVINFO -R -p -t6 -s $iana_name 2>/dev/null`
			uports=`$SERVINFO -R -p -u -s $iana_name 2>/dev/null`
			uports6=`$SERVINFO -R -p -u6 -s $iana_name 2>/dev/null`
		else
			tports=`$SERVINFO -p -t -s $iana_name 2>/dev/null`
			tports6=`$SERVINFO -p -t6 -s $iana_name 2>/dev/null`
			uports=`$SERVINFO -p -u -s $iana_name 2>/dev/null`
			uports6=`$SERVINFO -p -u6 -s $iana_name 2>/dev/null`
		fi

		# IPv6 ports are also reachable through IPv4, so include
		# them when generating IPv4 rules.
		if [ -n "$tports" -o -n "$tports6" ]; then
			tports=`unique_ports $tports $tports6`
			for tport in $tports; do
				generate_rules $FMRI $policy "tcp" $tport $file
			done
		fi

		if [ -n "$tports6" ]; then
			for tport6 in $tports6; do
				generate_rules $FMRI $policy "tcp" $tport6 $file6 _6
			done
		fi

		if [ -n "$uports" -o -n "$uports6" ]; then
			uports=`unique_ports $uports $uports6`
			for uport in $uports; do
				generate_rules $FMRI $policy "udp" $uport $file
			done
		fi

		if [ -n "$uports6" ]; then
			for uport6 in $uports6; do
				generate_rules $FMRI $policy "udp" $uport6 $file6 _6
			done
		fi
	fi

	;;

*)
	echo "Usage: $0 { start | stop | refresh }"
	exit 1
	;;
esac
exit $SMF_EXIT_OK