xref: /illumos-gate/usr/src/cmd/tsol/labeld/svc-labeld (revision 3323877de6db742e6657b6081ffe6acd0b007436)
1#!/sbin/sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23# Use is subject to license terms.
24#
25#ident	"%Z%%M%	%I%	%E% SMI"
26
27. /lib/svc/share/smf_include.sh
28
29ROOT_PATH=""
30if [ $# -gt 1 ]; then
31	if [ $# -ne 3 -o "$2" != "-R" ]; then
32		echo "$0: invalid syntax"
33		exit $SMF_EXIT_ERR_CONFIG
34	fi
35	if [ "$3" != "/" ]; then
36		ROOT_PATH=$3
37	fi
38fi
39if [ -n "$ROOT_PATH" -a "$1" != "start" ]; then
40	echo "$0: invalid syntax: -R allowed for start method only"
41	exit $SMF_EXIT_ERR_CONFIG
42fi
43if [ -n "$ROOT_PATH" -a ! -d "$ROOT_PATH" ]; then
44	echo "$0: invalid -R rootpath dir specified"
45	exit $SMF_EXIT_ERR_CONFIG
46fi
47
48if smf_is_nonglobalzone; then
49	echo "$0: not supported in a local zone"
50	exit $SMF_EXIT_ERR_CONFIG
51fi
52
53rewrite_logindev()
54{
55	from="$1"
56	to="$2"
57	# Comment out audio, usb, removable-media, and hotpluggable device
58	# entries in /etc/logindevperm.
59	LOGINDEVPERM=$ROOT_PATH/etc/logindevperm
60	if [ ! -f $LOGINDEVPERM ]; then
61		return
62	fi
63	for line in \
64		"/dev/console	0600	/dev/sound/" \
65		"/dev/console	0400	/dev/removable-media/" \
66		"/dev/console	0400	/dev/hotpluggable/" \
67		"/dev/console	0600	/dev/usb/\[0-9a-f\]" \
68			; do
69		sed -e "s!^$from$line!$to$line!" $LOGINDEVPERM > /tmp/tmp.$$
70		cp /tmp/tmp.$$ $LOGINDEVPERM
71	done
72	rm -f /tmp/tmp.$$
73}
74
75do_logindev()
76{
77	rewrite_logindev "" "#"
78}
79
80do_otherservices()
81{
82	# Setup dependent services
83	cat >> $ROOT_PATH/var/svc/profile/upgrade <<\__ENABLE_OTHERS
84		/usr/sbin/svcadm enable -s svc:/network/tnd:default
85		/usr/sbin/svcadm enable -s svc:/system/tsol-zones:default
86		/usr/sbin/svccfg -s svc:/application/x11/x11-server \
87		    setprop options/tcp_listen = true
88		/usr/sbin/svcadm enable svc:/network/rpc/rstat:default
89__ENABLE_OTHERS
90
91}
92
93do_bsmconv()
94{
95	# Run bsmconv so audit and device allocation is enabled by
96	# default with Trusted Extensions.
97	if [ "$ROOT_PATH" = "/" -o "$ROOT_PATH" = "" ]; then
98		BSMDIR=""
99	else
100		BSMDIR=$ROOT_PATH
101	fi
102	echo "Running bsmconv ..."
103	echo `TEXTDOMAIN="SUNW_OST_OSCMD" gettext "y"` | \
104	    $ROOT_PATH/etc/security/bsmconv $ROOT_PATH
105}
106
107do_nscd()
108{
109# For Trusted Extensions, make nscd service transient in local zones.
110cat >> $ROOT_PATH/var/svc/profile/upgrade <<\_DEL_LOCAL_NSCD
111	if [ `/sbin/zonename` != "global" ]; then
112		nscd="svc:/system/name-service-cache"
113		duration=""
114		if /bin/svcprop -q -c -p startd/duration $nscd ; then
115			duration=`/bin/svcprop -c -p startd/duration $nscd`
116		fi
117		if [ "$duration" != "transient" ]; then
118			/usr/sbin/svccfg -s $nscd addpg startd framework
119			/usr/sbin/svccfg -s $nscd setprop \
120			    startd/duration = astring: transient
121			/usr/sbin/svccfg -s $nscd setprop stop/exec = :true
122			/usr/sbin/svcadm refresh $nscd
123		fi
124	fi
125_DEL_LOCAL_NSCD
126}
127
128do_bootupd()
129{
130	if [ -f $ROOT_PATH/platform/`/sbin/uname -m`/boot_archive ]; then
131		if [ -z "$ROOT_PATH" -o "$ROOT_PATH" = "/" ]; then
132			/sbin/bootadm update-archive
133		else
134			/sbin/bootadm update-archive -R $ROOT_PATH
135		fi
136	fi
137}
138
139setup_tx_changes(){
140#
141# No comments or blanks lines allowed in entries below
142#
143cat > ${TX_ENTRIES} << EOF
144dtlogin		account		requisite	pam_roles.so.1
145dtlogin		account		required	pam_unix_account.so.1
146dtsession	account		requisite	pam_roles.so.1
147dtsession	account		required	pam_unix_account.so.1
148gdm		account		requisite	pam_roles.so.1
149gdm		account		required	pam_unix_account.so.1
150xscreensaver	account		requisite	pam_roles.so.1
151xscreensaver	account		required	pam_unix_account.so.1
152passwd		account		requisite	pam_roles.so.1
153passwd		account		required	pam_unix_account.so.1
154dtpasswd	account		requisite	pam_roles.so.1
155dtpasswd	account		required	pam_unix_account.so.1
156other		account		required	pam_tsol_account.so.1
157EOF
158}
159
160do_addpam()
161{
162	PAM_TMP=/tmp/pam_conf.$$
163	TX_ENTRIES=$PAM_TMP/sct.$$
164	PAM_DEST=$ROOT_PATH/etc/pam.conf
165
166	mkdir $PAM_TMP  || exit $SMF_EXIT_ERR_FATAL
167	setup_tx_changes
168
169	# verify that pam.conf file exists...
170	if [ ! -f ${PAM_DEST} ]; then
171		echo "$0: ${PAM_DEST} not found; aborting"
172		exit $SMF_EXIT_ERR_FATAL
173	fi
174
175	#
176	# Update pam.conf to append Trusted Extensions entries if not
177	# already present.
178	#
179	rm -f /tmp/pamconf.$$
180	while read e1 e2 e3 e4 e5
181	do
182		# If this is the 'other' entry, add it unless it already
183		# exists.
184		if [ $e1 = "other" ]; then
185			grep \
186"^[# 	]*$e1[ 	][ 	]*$e2[ 	][ 	]*$e3[ 	][ 	]*$e4" \
187			    $PAM_DEST >/dev/null 2>&1
188			if [ $? = 1 ] ; then
189				# Doesn't exist, enter into pam.conf
190				echo "$e1\t$e2 $e3\t\t$e4 $e5" \
191				    >> /tmp/pamconf.$$
192			fi
193		else
194			# Add other entries unless they already have a
195			# stack of their own.
196			grep "^[# 	]*$e1[ 	][ 	]*$e2[ 	]" \
197			    $PAM_DEST >/dev/null 2>&1
198			if [ $? = 1 ] ; then
199				echo "$e1\t$e2 $e3\t\t$e4 $e5" \
200				    >> /tmp/pamconf.$$
201			fi
202		fi
203	done < ${TX_ENTRIES}
204	# Append TX lines if any were not present already.
205	if [ -f /tmp/pamconf.$$ ] ; then
206		echo "# Entries for Trusted Extensions" >> $PAM_DEST
207		cat /tmp/pamconf.$$ >> $PAM_DEST
208		echo "$0: updating $PAM_DEST entries for Trusted Extensions;"
209		echo "$0: please examine/update any new entries"
210    		rm -f /tmp/pamconf.$$
211    	fi
212
213	rm -rf $PAM_TMP
214}
215
216do_pamremove()
217{
218	PAM_TMP=/tmp/pam_conf.$$
219	TX_ENTRIES=$PAM_TMP/sct.$$
220	PAM_DEST=$ROOT_PATH/etc/pam.conf
221	TMPFILE=$PAM_TMP/pam.conf
222
223	mkdir $PAM_TMP  || exit $SMF_EXIT_ERR_FATAL
224
225	# verify that pam.conf file exists...
226	if [ ! -f ${PAM_DEST} ]; then
227		echo "$0: ${PAM_DEST} not found; aborting"
228		exit $SMF_EXIT_ERR_FATAL
229	fi
230
231
232	grep '^[a-z].*pam_tsol_account' $PAM_DEST > /dev/null 2>&1
233	if [ $? -ne 0 ]; then
234		echo "$0: pam_tsol_account module not present,"
235		echo "$0: No changes were made to $PAM_DEST."
236		return
237	fi
238
239	grep -v pam_tsol_account $PAM_DEST > $TMPFILE
240	echo "$0: $PAM_DEST "tsol" entries removed"
241	cp $TMPFILE $PAM_DEST
242
243	rm -rf $PAM_TMP
244}
245
246do_commonstart()
247{
248	echo "$0: Updating $ROOT_PATH/etc/system..."
249	if [ ! -f ${ROOT_PATH}/etc/system ]; then
250		touch ${ROOT_PATH}/etc/system
251	fi
252
253	# Set sys_labeling in etc/system
254	grep -v "sys_labeling=" ${ROOT_PATH}/etc/system > /tmp/etc.system.$$
255	echo "set sys_labeling=1" >> /tmp/etc.system.$$
256	mv /tmp/etc.system.$$ ${ROOT_PATH}/etc/system
257	grep "set sys_labeling=1" ${ROOT_PATH}/etc/system > /dev/null 2>&1
258	if [ $? -ne 0 ]; then
259    		echo "$0: ERROR: cannot set sys_labeling in $ROOT_PATH/etc/system"
260		exit $SMF_EXIT_ERR_FATAL
261	fi
262
263	# Setup dependent services
264	do_otherservices
265
266	do_logindev
267	do_bsmconv
268	do_nscd
269	do_addpam
270
271	do_bootupd
272}
273
274
275daemon_start()
276{
277	# If a labeld door exists, check for a labeld process and exit
278	# if the daemon is already running.
279	if [ -r /var/tsol/doors/labeld ]; then
280		if /usr/bin/pgrep -x -u 0 -P 1 labeld >/dev/null 2>&1; then
281			echo "$0: labeld is already running"
282			exit $SMF_EXIT_ERR_FATAL
283		fi
284	fi
285	/usr/bin/rm -f /var/tsol/doors/labeld
286	/usr/lib/labeld
287}
288
289PATH=/usr/sbin:/usr/bin; export PATH
290
291case "$1" in
292'start')
293	if [ -z "$ROOT_PATH" -o "$ROOT_PATH" = "/" ]; then
294		# native
295
296		if [ -z "$SMF_FMRI" ]; then
297			echo "$0: this script can only be invoked by smf(5)"
298			exit $SMF_EXIT_ERR_NOSMF
299		fi
300
301		tx_enabled=`/usr/bin/svcprop -c -p general/enabled $SMF_FMRI`
302		if [ "$tx_enabled" = "false" ]; then
303			# A sign of trying temporary enablement...no-no
304			echo "$0: Temporarily enabling Trusted Extensions is not allowed."
305			exit $SMF_EXIT_ERR_CONFIG
306		fi
307
308		if (smf_is_system_labeled); then
309			daemon_start
310			exit $SMF_EXIT_OK
311		fi
312
313		# Make changes to enable Trusted Extensions
314		grep "^set sys_labeling=1" ${ROOT_PATH}/etc/system > /dev/null 2>&1
315		if [ $? -eq 0 ]; then
316			echo "$0: already enabled. Exiting."
317			exit $SMF_EXIT_OK
318		fi
319
320		if [ "`/usr/sbin/zoneadm list -c`" != "global" ]; then
321			echo "$0: Must remove zones before enabling Trusted Extensions."
322			exit $SMF_EXIT_ERR_CONFIG
323		fi
324
325		do_commonstart
326
327		# start daemon proccess so our service doesn't go into
328		# maintenance state
329		daemon_start
330
331		echo "$0: Started.  Must reboot and configure Trusted Extensions."
332	else
333		# Support jumpstart etc
334
335		# Make changes to enable Trusted Extensions
336		grep "^set sys_labeling=1" ${ROOT_PATH}/etc/system > /dev/null 2>&1
337		if [ $? -eq 0 ]; then
338			echo "$0: already enabled. Exiting."
339			exit $SMF_EXIT_OK
340		fi
341
342		# Setup dependent services
343		cat >> $ROOT_PATH/var/svc/profile/upgrade <<\__TRUSTED_ENABLE
344			/usr/sbin/svcadm enable -s svc:/system/labeld:default
345__TRUSTED_ENABLE
346
347		do_commonstart
348		echo "$0: Started.  Must configure Trusted Extensions before booting."
349	fi
350	;;
351
352'stop')
353	tx_enabled=`/usr/bin/svcprop -c -p general/enabled $SMF_FMRI`
354	if [ "$tx_enabled" = "true" ]; then
355		/usr/bin/pkill -x -u 0 -P 1 -z `smf_zonename` labeld
356		exit $SMF_EXIT_OK
357	fi
358
359	if [ "`/usr/sbin/zoneadm list -c`" != "global" ]; then
360		echo "$0: Must remove zones before disabling Trusted Extensions."
361		exit $SMF_EXIT_ERR_CONFIG
362	fi
363
364	# Stop Trusted services.
365	/usr/sbin/svcadm disable svc:/system/tsol-zones:default 2>/dev/null
366	/usr/sbin/svcadm disable svc:/network/tnd:default 2>/dev/null
367
368	# Uncomment audio, usb, removable-media, and hotpluggable device
369	# entries in /etc/logindevperm.
370	rewrite_logindev "#" ""
371
372	# Remove sys_labeling from /etc/system
373	grep -v "sys_labeling" ${ROOT_PATH}/etc/system > /tmp/etc.system.$$
374	mv /tmp/etc.system.$$ ${ROOT_PATH}/etc/system
375	grep "sys_labeling" ${ROOT_PATH}/etc/system > /dev/null 2>&1
376	if [ $? -eq 0 ]; then
377    		echo "$0: ERROR: cannot remove sys_labeling in $ROOT_PATH/etc/system"
378		exit $SMF_EXIT_ERR_FATAL
379	fi
380
381	do_pamremove
382
383	do_bootupd
384
385	/usr/bin/pkill -x -u 0 -P 1 -z `smf_zonename` labeld
386	echo "$0: Stopped.  Will take effect at next boot."
387	;;
388
389*)
390	echo "Usage: $0 { start | stop }"
391	exit 1
392	;;
393esac
394
395exit $SMF_EXIT_OK
396
397