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-05-31 14:54:01 +00:00
|
|
|
# REQUIRE: LOGIN cleanvar sshd
|
2006-03-17 13:49:55 +00:00
|
|
|
# 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
|
2012-03-08 22:33:40 +00:00
|
|
|
rcvar=${name}_enable
|
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;
|
2010-01-11 03:57:13 +00:00
|
|
|
unset ezjail_list ezjail_pass ezjail_mds ezjail_stop
|
|
|
|
ezjail_cfgs=${ezjail_prefix}/etc/ezjail/
|
2006-03-21 14:06:09 +00:00
|
|
|
ezjail_fromrc="YES"
|
|
|
|
|
2010-01-11 03:57:13 +00:00
|
|
|
case "${action}" in *stop) ezjail_stop="YES";; esac
|
|
|
|
|
2006-03-21 14:06:09 +00:00
|
|
|
# If a jail list is given on command line, process it
|
|
|
|
# If not, fetch it from our config directory
|
2007-10-08 01:41:02 +00:00
|
|
|
if [ "$*" ]; then
|
2006-07-15 16:11:22 +00:00
|
|
|
ezjail_list=`echo -n $* | tr -c '[:alnum:] ' '_'`
|
2007-10-08 02:24:26 +00:00
|
|
|
unset ezjail_fromrc
|
2006-03-21 14:06:09 +00:00
|
|
|
else
|
2010-01-11 03:57:13 +00:00
|
|
|
[ "${ezjail_stop}" ] && reverse_command="tail -r" || reverse_command="cat"
|
|
|
|
[ -d "${ezjail_cfgs}" ] && cd "${ezjail_cfgs}" && ezjail_list=`ls | xargs rcorder | ${reverse_command}`
|
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
|
2010-01-11 04:06:54 +00:00
|
|
|
unset ezjail_config ezjail_norun
|
2010-01-11 03:57:13 +00:00
|
|
|
|
2010-01-11 04:01:30 +00:00
|
|
|
[ -e "${ezjail_cfgs}/${ezjail}" ] && ezjail_config="${ezjail_cfgs}/${ezjail}"
|
2010-01-11 04:06:54 +00:00
|
|
|
[ -e "${ezjail_cfgs}/${ezjail}.norun" ] && ezjail_config="${ezjail_cfgs}/${ezjail}.norun" && ezjail_norun="YES"
|
2006-03-21 14:06:09 +00:00
|
|
|
|
|
|
|
# Check for jails config
|
2010-01-11 04:01:30 +00:00
|
|
|
[ ! -f "${ezjail_config}" ] && echo " Warning: Jail ${ezjail} not found." && continue
|
2010-01-11 03:57:13 +00:00
|
|
|
|
|
|
|
# If jail is temporary disabled (dot in name), skip it for starts
|
|
|
|
[ "${ezjail_stop}" ] && ezjail="${ezjail%%.*}"
|
2010-01-11 04:06:54 +00:00
|
|
|
[ "${ezjail%.*}" != "${ezjail}" -o "${ezjail_norun}" ] && echo -n " skipping ${ezjail}" && continue
|
2006-03-21 14:06:09 +00:00
|
|
|
|
|
|
|
# Read config file
|
2010-01-11 03:57:13 +00:00
|
|
|
. ${ezjail_config}
|
2006-03-21 14:06:09 +00:00
|
|
|
|
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
|
|
|
|
2010-02-14 23:34:37 +00:00
|
|
|
# Fix backward compatibility issue
|
|
|
|
eval ezjail_exec_start=\"\$jail_${ezjail}_exec_start\"
|
|
|
|
eval ezjail_exec=\"\$jail_${ezjail}_exec\"
|
2010-02-16 22:02:10 +00:00
|
|
|
eval jail_${ezjail}_exec_start=\"\${ezjail_exec_start:-${ezjail_exec}}\"
|
2010-02-14 23:34:37 +00:00
|
|
|
eval unset jail_${ezjail}_exec
|
|
|
|
|
2006-09-12 11:17:02 +00:00
|
|
|
# Do we still have a root to run in?
|
|
|
|
[ ! -d "${ezjail_rootdir}" ] && echo " Warning: root directory ${ezjail_rootdir} of ${ezjail} does not exist." && continue
|
|
|
|
|
2007-10-08 01:41:02 +00:00
|
|
|
[ "${ezjail_attachblocking}" -o "${ezjail_forceblocking}" ] && ezjail_blocking="YES" || unset ezjail_blocking
|
2006-05-07 22:25:25 +00:00
|
|
|
|
|
|
|
# Cannot auto mount blocking jails without interrupting boot process
|
2007-10-08 01:41:02 +00:00
|
|
|
[ "${ezjail_fromrc}" -a "${action}" = "start" -a "${ezjail_blocking}" ] && echo -n " ...skipping blocking jail ${ezjail}" && 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
|
2007-10-08 02:19:41 +00:00
|
|
|
[ "${action%crypto}" = "${action}" -o "${ezjail_blocking}" ] || continue
|
2006-03-25 17:42:00 +00:00
|
|
|
|
2006-03-21 14:06:09 +00:00
|
|
|
# Try to attach (crypto) devices
|
2007-10-08 01:41:02 +00:00
|
|
|
if [ "${ezjail_image}" ]; then
|
2006-05-09 00:26:04 +00:00
|
|
|
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
|
2010-02-16 22:03:37 +00:00
|
|
|
[ "${ezjail_pass}" ] && sh /etc/rc.d/jail one${action%crypto} ${ezjail_pass}
|
2006-03-21 14:06:09 +00:00
|
|
|
|
2010-02-18 12:22:26 +00:00
|
|
|
# Configure settings that need to be done after the jail has been started
|
2010-03-16 23:48:18 +00:00
|
|
|
if [ "${action%crypto}" = "start" -o "${action}" = "restart" ]; then
|
2010-02-18 12:22:26 +00:00
|
|
|
for ezjail in ${ezjail_list}; do
|
|
|
|
ezjail_safename=`echo -n "${ezjail}" | tr -c '[:alnum:]' _`
|
|
|
|
# Get the JID of the jail
|
|
|
|
[ -f "/var/run/jail_${ezjail_safename}.id" ] && ezjail_id=`cat /var/run/jail_${ezjail_safename}.id` || return
|
2009-12-28 22:09:17 +00:00
|
|
|
|
2010-02-18 12:22:26 +00:00
|
|
|
eval ezjail_zfs_datasets=\"\$jail_${ezjail_safename}_zfs_datasets\"
|
|
|
|
eval ezjail_cpuset=\"\$jail_${ezjail_safename}_cpuset\"
|
|
|
|
|
|
|
|
# Attach ZFS-datasets to the jail
|
|
|
|
for zfs in ${ezjail_zfs_datasets}; do
|
|
|
|
/sbin/zfs jail ${ezjail_id} ${zfs} || echo -n "Error: ${zfs} could not be configured"
|
|
|
|
done
|
2009-12-28 22:09:17 +00:00
|
|
|
|
2010-02-18 12:22:26 +00:00
|
|
|
# Configure processor sets for the jail via cpuset(1)
|
|
|
|
[ -z "${ezjail_cpuset}" ] || /usr/bin/cpuset -l ${ezjail_cpuset} -j ${ezjail_id} || echo -n "Error: The defined cpuset is malformed"
|
|
|
|
done
|
2009-12-28 22:09:17 +00:00
|
|
|
fi
|
|
|
|
|
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 ()
|
|
|
|
{
|
2007-09-01 13:10:41 +00:00
|
|
|
case "${action%crypto}" in
|
|
|
|
start|restart)
|
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
|
2007-10-08 01:41:02 +00:00
|
|
|
[ -e "/var/run/jail_${ezjail}.id" ] && return 0
|
2006-05-09 00:26:04 +00:00
|
|
|
|
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`
|
2007-03-23 15:45:35 +00:00
|
|
|
|
2007-03-23 16:08:43 +00:00
|
|
|
mount -p -v | grep -E "^${ezjail_rootdir}.device.${ezjail_rootdir}" && echo "Warning: Skipping jail. Jail image file ${ezjail} already attached as ${ezjail_device}. 'ezjail-admin config -i detach' it first." && return 1
|
|
|
|
mount -p -v | grep -E "^${ezjail_device}.${ezjail_rootdir}" && echo "Warning: Skipping jail. Jail image file ${ezjail} already attached as ${ezjail_device}. 'ezjail-admin config -i detach' it first." && return 1
|
2007-03-23 15:45:35 +00:00
|
|
|
|
|
|
|
# Remove stale device link
|
2007-10-08 01:41:02 +00:00
|
|
|
rm -f "${ezjail_rootdir}.device"
|
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}..."
|
2007-10-08 01:41:02 +00:00
|
|
|
echo gbde attach "/dev/${ezjail_device}" ${ezjail_attachparams} | /bin/sh
|
2006-05-09 02:00:04 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
2007-10-08 01:41:02 +00:00
|
|
|
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
|
2007-10-08 01:41:02 +00:00
|
|
|
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}..."
|
2007-10-08 01:41:02 +00:00
|
|
|
echo geli attach ${ezjail_attachparams} "/dev/${ezjail_device}" | /bin/sh
|
2006-05-09 02:00:04 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
2007-10-08 01:41:02 +00:00
|
|
|
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
|
2007-10-08 01:41:02 +00:00
|
|
|
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
|
2007-10-08 01:41:02 +00:00
|
|
|
fsck -t ufs -p -B "/dev/${ezjail_device}"
|
2006-05-09 00:26:04 +00:00
|
|
|
|
2006-03-21 14:06:09 +00:00
|
|
|
# relink image device
|
2007-10-08 01:41:02 +00:00
|
|
|
rm -f "${ezjail_rootdir}.device"
|
|
|
|
ln -s "/dev/${ezjail_device}" "${ezjail_rootdir}.device"
|
2007-09-01 13:10:41 +00:00
|
|
|
;;
|
|
|
|
stop)
|
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
|
2007-10-08 01:41:02 +00:00
|
|
|
[ -e "/var/run/jail_${ezjail}.id" ] || return 1
|
2006-05-09 02:00:04 +00:00
|
|
|
|
2006-03-21 14:06:09 +00:00
|
|
|
# If soft link to device is not set, we cannot unmount
|
2007-10-08 01:41:02 +00:00
|
|
|
[ -e "${ezjail_rootdir}.device" ] || return
|
2006-03-21 14:06:09 +00:00
|
|
|
|
|
|
|
# Fetch destination of soft link
|
2007-10-08 01:41:02 +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)
|
2007-10-08 01:41:02 +00:00
|
|
|
rm -f "${ezjail_rootdir}.device"
|
2007-09-01 13:10:41 +00:00
|
|
|
;;
|
|
|
|
esac
|
2006-03-21 14:06:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
attach_detach_post () {
|
|
|
|
# In case of a stop, unmount image devices after stopping jails
|
|
|
|
for md in ${ezjail_mds}; do
|
2007-10-08 01:41:02 +00:00
|
|
|
[ -e "${md}.bde" ] && gbde detach "${md}"
|
|
|
|
[ -e "${md}.eli" ] && geli detach "${md}"
|
|
|
|
mdconfig -d -u "${md#/dev/}"
|
2006-03-21 14:06:09 +00:00
|
|
|
done
|
2005-09-26 01:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
run_rc_command $*
|