2005-10-20 01:41:38 +00:00
|
|
|
#!/bin/sh
|
2006-05-04 14:02:17 +00:00
|
|
|
# $Id$
|
2005-10-20 01:41:38 +00:00
|
|
|
#
|
2005-09-26 01:30:00 +00:00
|
|
|
# $FreeBSD$
|
|
|
|
#
|
|
|
|
# PROVIDE: ezjail
|
2006-03-17 13:49:55 +00:00
|
|
|
# REQUIRE: LOGIN cleanvar
|
|
|
|
# BEFORE: securelevel
|
|
|
|
# KEYWORD: nojail shutdown
|
2005-09-26 01:30:00 +00:00
|
|
|
#
|
2006-02-03 17:38:25 +00:00
|
|
|
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf to enable ezjail
|
2005-09-26 01:30:00 +00:00
|
|
|
#
|
|
|
|
#ezjail_enable="YES"
|
|
|
|
#
|
2005-10-14 16:22:05 +00:00
|
|
|
# Please do not change this file, configure in EZJAIL_PREFIX/etc/ezjail.conf
|
2005-09-26 01:30:00 +00:00
|
|
|
|
2005-10-14 16:22:05 +00:00
|
|
|
# ugly: this variable is set on port install time
|
2006-03-25 11:41:38 +00:00
|
|
|
ezjail_prefix=EZJAIL_PREFIX
|
2005-09-26 01:30:00 +00:00
|
|
|
|
|
|
|
. /etc/rc.subr
|
|
|
|
|
|
|
|
name=ezjail
|
|
|
|
rcvar=`set_rcvar`
|
2006-03-25 17:42:00 +00:00
|
|
|
extra_commands="startcrypto stopcrypto"
|
2006-02-06 02:16:30 +00:00
|
|
|
load_rc_config ${name}
|
2005-09-26 01:30:00 +00:00
|
|
|
|
|
|
|
ezjail_enable=${ezjail_enable:-"NO"}
|
|
|
|
|
2006-02-03 17:38:25 +00:00
|
|
|
restart_cmd="do_cmd restart _"
|
|
|
|
start_cmd="do_cmd start '_ ezjail'"
|
|
|
|
stop_cmd="do_cmd stop '_ ezjail'"
|
2006-03-25 17:42:00 +00:00
|
|
|
startcrypto_cmd="do_cmd startcrypto _"
|
|
|
|
stopcrypto_cmd="do_cmd stopcrypto _"
|
2005-09-26 01:30:00 +00:00
|
|
|
|
2006-02-03 17:38:25 +00:00
|
|
|
do_cmd()
|
2005-09-26 01:30:00 +00:00
|
|
|
{
|
2006-02-03 17:38:25 +00:00
|
|
|
action=$1; message=$2; shift 2;
|
2006-04-24 10:59:04 +00:00
|
|
|
unset ezjail_list ezjail_pass ezjail_mds
|
2006-03-21 14:06:09 +00:00
|
|
|
ezjail_fromrc="YES"
|
|
|
|
|
|
|
|
# If a jail list is given on command line, process it
|
|
|
|
# If not, fetch it from our config directory
|
|
|
|
if [ -n "$*" ]; then
|
|
|
|
ezjail_list=`echo -n $* | tr -c "[:alnum:] " _`
|
|
|
|
ezjail_fromrc="NO"
|
|
|
|
else
|
2006-04-24 10:59:04 +00:00
|
|
|
ezjail_list=`find -X ${ezjail_prefix}/etc/ezjail/ 2> /dev/null | xargs rcorder | xargs basename -a`
|
2006-03-21 14:06:09 +00:00
|
|
|
echo -n "${message##_}"
|
|
|
|
fi
|
|
|
|
|
2006-03-06 18:18:33 +00:00
|
|
|
for ezjail in ${ezjail_list}; do
|
2006-03-21 14:06:09 +00:00
|
|
|
# If jail is temporary disabled (dot in name), skip it
|
2006-04-12 19:04:46 +00:00
|
|
|
[ "${ezjail%.*}" != "${ezjail}" ] && continue
|
2006-03-21 14:06:09 +00:00
|
|
|
|
|
|
|
# Check for jails config
|
|
|
|
[ ! -r ${ezjail_prefix}/etc/ezjail/${ezjail} ] && echo " Warning: Jail ${ezjail} not found." && continue
|
|
|
|
|
|
|
|
# Read config file
|
|
|
|
. ${ezjail_prefix}/etc/ezjail/${ezjail}
|
|
|
|
|
2006-05-09 02:00:04 +00:00
|
|
|
eval ezjail_rootdir=\"\$jail_${ezjail}_rootdir\"
|
2006-03-21 14:06:09 +00:00
|
|
|
eval ezjail_image=\"\$jail_${ezjail}_image\"
|
2006-04-15 20:20:12 +00:00
|
|
|
eval ezjail_imagetype=\"\$jail_${ezjail}_imagetype\"
|
2006-05-03 16:01:41 +00:00
|
|
|
eval ezjail_attachparams=\"\$jail_${ezjail}_attachparams\"
|
2006-05-04 18:49:52 +00:00
|
|
|
eval ezjail_attachblocking=\"\$jail_${ezjail}_attachblocking\"
|
2006-05-07 22:25:25 +00:00
|
|
|
eval ezjail_forceblocking=\"\$jail_${ezjail}_forceblocking\"
|
2006-03-21 14:06:09 +00:00
|
|
|
|
2006-05-09 02:00:04 +00:00
|
|
|
[ "${ezjail_attachblocking}" = "YES" -o "${ezjail_forceblocking}" = "YES" ] && ezjail_blocking="YES" || unset ezjail_blocking
|
2006-05-07 22:25:25 +00:00
|
|
|
|
|
|
|
# Cannot auto mount blocking jails without interrupting boot process
|
|
|
|
[ "${ezjail_fromrc}" = "YES" -a "${action}" = "start" -a "${ezjail_blocking}" = "YES" ] && continue
|
2006-05-03 16:28:15 +00:00
|
|
|
|
2006-05-04 18:49:52 +00:00
|
|
|
# Explicitely do only run blocking crypto jails when *crypto is requested
|
2006-05-07 22:25:25 +00:00
|
|
|
[ "${action%crypto}" != "${action}" -a -z "${ezjail_blocking}" ] && continue
|
2006-03-25 17:42:00 +00:00
|
|
|
|
2006-03-21 14:06:09 +00:00
|
|
|
# Try to attach (crypto) devices
|
2006-05-09 00:26:04 +00:00
|
|
|
if [ -n "${ezjail_image}" ]; then
|
|
|
|
attach_detach_pre || continue
|
|
|
|
fi
|
2006-03-21 14:06:09 +00:00
|
|
|
|
|
|
|
ezjail_pass="${ezjail_pass} ${ezjail}"
|
2006-02-03 17:38:25 +00:00
|
|
|
done
|
2006-03-21 14:06:09 +00:00
|
|
|
|
|
|
|
# Pass control to jail script which does the actual work
|
2006-03-25 17:42:00 +00:00
|
|
|
[ "${ezjail_pass}" ] && sh /etc/rc.d/jail one${action%crypto} ${ezjail_pass}
|
2006-03-21 14:06:09 +00:00
|
|
|
|
|
|
|
# Can only detach after unmounting (from fstab.JAILNAME in /etc/rc.d/jail)
|
|
|
|
attach_detach_post
|
|
|
|
}
|
|
|
|
|
|
|
|
attach_detach_pre ()
|
|
|
|
{
|
2006-03-25 17:57:50 +00:00
|
|
|
if [ "${action%crypto}" = "start" ]; then
|
2006-03-21 14:06:09 +00:00
|
|
|
# If jail is running, do not mount devices, this is the same check as
|
|
|
|
# /etc/rc.d/jail does
|
2006-05-09 00:26:04 +00:00
|
|
|
[ -e /var/run/jail_${ezjail}.id ] && return 1
|
|
|
|
|
2006-05-09 02:00:04 +00:00
|
|
|
if [ -L "${ezjail_rootdir}.device" ]; then
|
2006-05-09 00:26:04 +00:00
|
|
|
# Fetch destination of soft link
|
2006-05-09 02:00:04 +00:00
|
|
|
ezjail_device=`stat -f "%Y" ${ezjail_rootdir}.device`
|
|
|
|
[ -e "${ezjail_device}" ] && echo "Jail image file ${ezjail} already attached as ${ezjail_device}. 'ezjail-admin config -i detach' it first." && return 1
|
2006-05-09 00:26:04 +00:00
|
|
|
fi
|
2006-03-21 14:06:09 +00:00
|
|
|
|
|
|
|
# Create a memory disc from jail image
|
2006-05-09 00:26:04 +00:00
|
|
|
ezjail_device=`mdconfig -a -t vnode -f ${ezjail_image}` || return 1
|
2006-03-21 14:06:09 +00:00
|
|
|
|
|
|
|
# If this is a crypto jail, try to mount it, remind user, which jail
|
|
|
|
# this is. In this case, the device to mount is
|
2006-04-26 09:31:37 +00:00
|
|
|
case ${ezjail_imagetype} in
|
|
|
|
crypto|bde)
|
2006-05-04 18:49:52 +00:00
|
|
|
echo "Attaching bde device for image jail ${ezjail}..."
|
2006-05-03 16:01:41 +00:00
|
|
|
echo gbde attach /dev/${ezjail_device} ${ezjail_attachparams} | /bin/sh
|
2006-05-09 02:00:04 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
mdconfig -d -u ${ezjail_device} > /dev/null
|
2006-05-09 00:26:04 +00:00
|
|
|
echo "Error: Attaching bde device failed."; return 1
|
|
|
|
fi
|
2006-03-21 14:06:09 +00:00
|
|
|
# Device to mount is not md anymore
|
|
|
|
ezjail_device=${ezjail_device}.bde
|
2006-04-26 09:31:37 +00:00
|
|
|
;;
|
|
|
|
eli)
|
2006-05-04 18:49:52 +00:00
|
|
|
echo "Attaching eli device for image jail ${ezjail}..."
|
2006-05-03 16:01:41 +00:00
|
|
|
echo geli attach ${ezjail_attachparams} /dev/${ezjail_device} | /bin/sh
|
2006-05-09 02:00:04 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
mdconfig -d -u ${ezjail_device} > /dev/null
|
2006-05-09 00:26:04 +00:00
|
|
|
echo "Error: Attaching eli device failed."; return 1
|
|
|
|
fi
|
2006-05-03 16:01:41 +00:00
|
|
|
# Device to mount is not md anymore
|
|
|
|
ezjail_device=${ezjail_device}.eli
|
2006-04-26 09:31:37 +00:00
|
|
|
;;
|
|
|
|
esac
|
2006-03-21 14:06:09 +00:00
|
|
|
|
2006-05-09 00:26:04 +00:00
|
|
|
# Clean image
|
2006-05-09 02:00:04 +00:00
|
|
|
fsck_ufs -F -p /dev/${ezjail_device}
|
2006-05-09 00:26:04 +00:00
|
|
|
|
2006-03-21 14:06:09 +00:00
|
|
|
# relink image device
|
2006-05-09 02:00:04 +00:00
|
|
|
rm -f ${ezjail_rootdir}.device
|
|
|
|
ln -s /dev/${ezjail_device} ${ezjail_rootdir}.device
|
2006-03-21 14:06:09 +00:00
|
|
|
else
|
2006-05-09 02:00:04 +00:00
|
|
|
# If jail is not running, do not unmount devices, this is the same check
|
|
|
|
# as /etc/rc.d/jail does
|
|
|
|
[ -e /var/run/jail_${ezjail}.id ] || return 1
|
|
|
|
|
2006-03-21 14:06:09 +00:00
|
|
|
# If soft link to device is not set, we cannot unmount
|
2006-05-09 02:00:04 +00:00
|
|
|
[ -e ${ezjail_rootdir}.device ] || return
|
2006-03-21 14:06:09 +00:00
|
|
|
|
|
|
|
# Fetch destination of soft link
|
2006-05-09 02:00:04 +00:00
|
|
|
ezjail_device=`stat -f "%Y" ${ezjail_rootdir}.device`
|
2006-03-21 14:06:09 +00:00
|
|
|
|
|
|
|
# Add this device to the list of devices to be unmounted
|
2006-05-03 16:01:41 +00:00
|
|
|
case ${ezjail_imagetype} in
|
|
|
|
crypto|bde) ezjail_mds="${ezjail_mds} ${ezjail_device%.bde}" ;;
|
|
|
|
eli) ezjail_mds="${ezjail_mds} ${ezjail_device%.eli}" ;;
|
2006-05-15 13:16:49 +00:00
|
|
|
simple) ezjail_mds="${ezjail_mds} ${ezjail_device}" ;;
|
2006-05-03 16:01:41 +00:00
|
|
|
esac
|
2006-03-21 14:06:09 +00:00
|
|
|
|
|
|
|
# Remove soft link (which acts as a lock)
|
2006-05-09 02:00:04 +00:00
|
|
|
rm -f ${ezjail_rootdir}.device
|
2006-03-21 14:06:09 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
attach_detach_post () {
|
|
|
|
# In case of a stop, unmount image devices after stopping jails
|
|
|
|
for md in ${ezjail_mds}; do
|
|
|
|
[ -e ${md}.bde ] && gbde detach ${md}
|
2006-04-26 09:31:37 +00:00
|
|
|
[ -e ${md}.eli ] && geli detach ${md}
|
2006-03-21 14:06:09 +00:00
|
|
|
mdconfig -d -u ${md#/dev/}
|
|
|
|
done
|
2005-09-26 01:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
run_rc_command $*
|