1#!/bin/sh
2#
3
4# PROVIDE: scponlyc
5# REQUIRE: LOGIN cleanvar
6# KEYWORD: shutdown
7
8# Add the following lines to /etc/rc.conf to enable scponly:
9# scponlyc_enable (bool):		Set to "NO" by default.
10#					Set it to "YES" to enable scponly
11# scponlyc_shells (str):		Set to "/etc/shells" by default.
12# scponlyc_passwd (str):		Set to "/etc/passwd" by default.
13
14. /etc/rc.subr
15
16scponlyc_shells="${scponlyc_shells:-/etc/shells}"
17scponlyc_passwd="${scponlyc_passwd:-/etc/passwd}"
18
19name="scponlyc"
20rcvar=scponlyc_enable
21
22start_cmd="scponlyc_startcmd"
23stop_cmd="scponlyc_stopcmd"
24
25required_files="$scponlyc_shells $scponlyc_passwd"
26
27scponlyc=%%TRUE_PREFIX%%/sbin/scponlyc
28
29make_devfs() {
30	# $1 is the user name whose home directory needs a minimal
31	# devfs created. If ~/dev exists, it will be deleted.
32
33	eval DEV="~$1/dev"
34	if /sbin/mount | grep "${DEV}" >/dev/null 2>&1; then
35		/sbin/umount "${DEV}" 2>/dev/null
36	fi
37	/bin/rmdir "${DEV}" || err 1 "Unable to remove $DEV"
38	/bin/mkdir -p "${DEV}"
39	devfs_domount "${DEV}"
40	if devfs_init_rulesets; then
41		devfs_apply_ruleset "devfsrules_hide_all" "${DEV}" && \
42		devfs_apply_ruleset "devfsrules_unhide_basic" "${DEV}" || \
43		/sbin/umount "${DEV}" 2>/dev/null
44	fi
45}
46
47users_configured() {
48
49	if [ `/usr/bin/grep -c "/scponlyc$" ${scponlyc_shells} 2>/dev/null` -ne 1 ]; then
50		exit 1
51	fi
52}
53
54scponlyc_startcmd() {
55
56	users_configured
57
58	/usr/bin/grep "^[^#]*:.*:.*:.*:.*:.*:${scponlyc}$" ${scponlyc_passwd} |
59		/usr/bin/awk -F: {'print $1'} |
60		while read USER; do
61			/bin/echo "${USER}/dev"
62			make_devfs "${USER}"
63		done
64}
65
66scponlyc_stopcmd() {
67
68	users_configured
69
70	/usr/bin/grep "^[^#]*:.*:.*:.*:.*:.*:${scponlyc}$" ${scponlyc_passwd} |
71		/usr/bin/awk -F: {'print $1'} |
72		while read USER; do
73			/bin/echo "${USER}/dev"
74			eval DEV="~${USER}/dev"
75			/sbin/umount ${DEV} 2>/dev/null
76		done
77}
78
79load_rc_config $name
80run_rc_command "$1"
81