2003-04-16 16:27:23 +00:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# $FreeBSD$
|
|
|
|
#
|
|
|
|
|
|
|
|
# PROVIDE: jail
|
|
|
|
# REQUIRE: LOGIN
|
2003-05-05 15:38:41 +00:00
|
|
|
# BEFORE: securelevel
|
2004-10-07 13:55:26 +00:00
|
|
|
# KEYWORD: nojail shutdown
|
2003-04-16 16:27:23 +00:00
|
|
|
|
|
|
|
. /etc/rc.subr
|
|
|
|
|
|
|
|
name="jail"
|
|
|
|
rcvar=`set_rcvar`
|
|
|
|
start_cmd="jail_start"
|
|
|
|
stop_cmd="jail_stop"
|
|
|
|
|
2003-08-24 06:29:32 +00:00
|
|
|
# init_variables _j
|
|
|
|
# Initialize the various jail variables for jail _j.
|
|
|
|
#
|
|
|
|
init_variables()
|
|
|
|
{
|
|
|
|
_j="$1"
|
|
|
|
|
|
|
|
if [ -z "$_j" ]; then
|
|
|
|
warn "init_variables: you must specify a jail"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
eval jail_rootdir=\"\$jail_${_j}_rootdir\"
|
|
|
|
jail_devdir="${jail_rootdir}/dev"
|
|
|
|
jail_fdescdir="${jail_devdir}/fd"
|
|
|
|
jail_procdir="${jail_rootdir}/proc"
|
|
|
|
eval jail_hostname=\"\$jail_${_j}_hostname\"
|
|
|
|
eval jail_ip=\"\$jail_${_j}_ip\"
|
|
|
|
eval jail_exec=\"\$jail_${_j}_exec\"
|
|
|
|
[ -z "${jail_exec}" ] && jail_exec="/bin/sh /etc/rc"
|
|
|
|
|
|
|
|
# The default jail ruleset will be used by rc.subr if none is specified.
|
|
|
|
eval jail_ruleset=\"\$jail_${_j}_devfs_ruleset\"
|
|
|
|
eval jail_devfs=\"\$jail_${_j}_devfs_enable\"
|
|
|
|
[ -z "${jail_devfs}" ] && jail_devfs="NO"
|
|
|
|
eval jail_fdescfs=\"\$jail_${_j}_fdescfs_enable\"
|
|
|
|
[ -z "${jail_fdescfs}" ] && jail_fdescfs="NO"
|
|
|
|
eval jail_procfs=\"\$jail_${_j}_procfs_enable\"
|
|
|
|
[ -z "${jail_procfs}" ] && jail_procfs="NO"
|
|
|
|
|
2004-02-03 12:59:30 +00:00
|
|
|
# Debugging aid
|
2003-08-24 06:29:32 +00:00
|
|
|
#
|
|
|
|
debug "$_j devfs enable: $jail_devfs"
|
|
|
|
debug "$_j fdescfs enable: $jail_fdescfs"
|
|
|
|
debug "$_j procfs enable: $jail_procfs"
|
|
|
|
debug "$_j hostname: $jail_hostname"
|
|
|
|
debug "$_j ip: $jail_ip"
|
|
|
|
debug "$_j root: $jail_rootdir"
|
|
|
|
debug "$_j devdir: $jail_devdir"
|
|
|
|
debug "$_j fdescdir: $jail_fdescdir"
|
|
|
|
debug "$_j procdir: $jail_procdir"
|
|
|
|
debug "$_j ruleset: $jail_ruleset"
|
|
|
|
}
|
|
|
|
|
2004-02-03 07:15:32 +00:00
|
|
|
# set_sysctl rc_knob mib msg
|
|
|
|
# If the mib sysctl is set according to what rc_knob
|
|
|
|
# specifies, this function does nothing. However if
|
|
|
|
# rc_knob is set differently than mib, then the mib
|
|
|
|
# is set accordingly and msg is displayed followed by
|
|
|
|
# an '=" sign and the word 'YES' or 'NO'.
|
|
|
|
#
|
|
|
|
set_sysctl()
|
2003-04-16 16:27:23 +00:00
|
|
|
{
|
2004-02-03 07:15:32 +00:00
|
|
|
_knob="$1"
|
|
|
|
_mib="$2"
|
|
|
|
_msg="$3"
|
2003-04-16 16:27:23 +00:00
|
|
|
|
2004-02-03 07:15:32 +00:00
|
|
|
_current=`${SYSCTL} -n $_mib 2>/dev/null`
|
|
|
|
if checkyesno $_knob ; then
|
|
|
|
if [ "$_current" -ne 1 ]; then
|
|
|
|
echo -n " ${_msg}=YES"
|
|
|
|
${SYSCTL_W} 1>/dev/null ${_mib}=1
|
|
|
|
fi
|
2003-04-16 16:27:23 +00:00
|
|
|
else
|
2004-02-03 07:15:32 +00:00
|
|
|
if [ "$_current" -ne 0 ]; then
|
|
|
|
echo -n " ${_msg}=NO"
|
|
|
|
${SYSCTL_W} 1>/dev/null ${_mib}=0
|
|
|
|
fi
|
2003-04-16 16:27:23 +00:00
|
|
|
fi
|
2004-02-03 07:15:32 +00:00
|
|
|
}
|
2003-04-16 16:27:23 +00:00
|
|
|
|
2004-02-03 12:59:30 +00:00
|
|
|
# jail_umount_fs
|
|
|
|
# This function unmounts certain special filesystems in the
|
|
|
|
# currently selected jail. The caller must call the init_variables()
|
|
|
|
# routine before calling this one.
|
|
|
|
#
|
|
|
|
jail_umount_fs()
|
|
|
|
{
|
|
|
|
if checkyesno jail_fdescfs; then
|
2004-08-19 08:55:24 +00:00
|
|
|
if [ -d "${jail_fdescdir}" ] ; then
|
2004-02-03 12:59:30 +00:00
|
|
|
umount -f ${jail_fdescdir} >/dev/null 2>&1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if checkyesno jail_devfs; then
|
2004-08-19 08:55:24 +00:00
|
|
|
if [ -d "${jail_devdir}" ] ; then
|
2004-02-03 12:59:30 +00:00
|
|
|
umount -f ${jail_devdir} >/dev/null 2>&1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if checkyesno jail_procfs; then
|
2004-08-19 08:55:24 +00:00
|
|
|
if [ -d "${jail_procdir}" ] ; then
|
2004-02-03 12:59:30 +00:00
|
|
|
umount -f ${jail_procdir} >/dev/null 2>&1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2004-02-03 07:15:32 +00:00
|
|
|
jail_start()
|
|
|
|
{
|
|
|
|
echo -n 'Configuring jails:'
|
|
|
|
set_sysctl jail_set_hostname_allow security.jail.set_hostname_allowed \
|
|
|
|
set_hostname_allow
|
|
|
|
set_sysctl jail_socket_unixiproute_only \
|
|
|
|
security.jail.socket_unixiproute_only unixiproute_only
|
|
|
|
set_sysctl jail_sysvipc_allow security.jail.sysvipc_allowed \
|
|
|
|
sysvipc_allow
|
2003-04-16 16:27:23 +00:00
|
|
|
echo '.'
|
|
|
|
|
2004-02-03 07:15:32 +00:00
|
|
|
echo -n 'Starting jails:'
|
2004-08-16 16:37:06 +00:00
|
|
|
_tmp_dir=`mktemp -d /tmp/jail.XXXXXXXX` || \
|
|
|
|
err 3 "$name: Can't create temp dir, exiting..."
|
2003-10-13 08:20:55 +00:00
|
|
|
for _jail in ${jail_list}
|
2003-04-16 16:27:23 +00:00
|
|
|
do
|
2003-08-24 06:29:32 +00:00
|
|
|
init_variables $_jail
|
|
|
|
if checkyesno jail_devfs; then
|
|
|
|
info "Mounting devfs on ${jail_devdir}"
|
|
|
|
devfs_mount_jail "${jail_devdir}" ${jail_ruleset}
|
|
|
|
|
|
|
|
# Transitional symlink for old binaries
|
2004-08-19 08:55:24 +00:00
|
|
|
if [ ! -L "${jail_devdir}/log" ]; then
|
2003-12-09 08:51:11 +00:00
|
|
|
__pwd="`pwd`"
|
|
|
|
cd "${jail_devdir}"
|
|
|
|
ln -sf ../var/run/log log
|
|
|
|
cd "$__pwd"
|
2003-08-24 06:29:32 +00:00
|
|
|
fi
|
|
|
|
|
2003-12-09 08:51:11 +00:00
|
|
|
# XXX - It seems symlinks don't work when there
|
|
|
|
# is a devfs(5) device of the same name.
|
2003-08-24 06:29:32 +00:00
|
|
|
# Jail console output
|
2003-12-09 08:51:11 +00:00
|
|
|
# __pwd="`pwd`"
|
|
|
|
# cd "${jail_devdir}"
|
|
|
|
# ln -sf ../var/log/console console
|
|
|
|
# cd "$__pwd"
|
2003-08-24 06:29:32 +00:00
|
|
|
fi
|
|
|
|
if checkyesno jail_fdescfs; then
|
|
|
|
info "Mounting fdescfs on ${jail_fdescdir}"
|
|
|
|
mount -t fdescfs fdesc "${jail_fdescdir}"
|
|
|
|
fi
|
|
|
|
if checkyesno jail_procfs; then
|
|
|
|
info "Mounting procfs onto ${jail_procdir}"
|
2004-08-19 08:55:24 +00:00
|
|
|
if [ -d "${jail_procdir}" ] ; then
|
2003-08-24 06:29:32 +00:00
|
|
|
mount -t procfs proc "${jail_procdir}"
|
|
|
|
fi
|
|
|
|
fi
|
2004-08-16 16:37:06 +00:00
|
|
|
_tmp_jail=${_tmp_dir}/jail.$$
|
2004-02-02 13:25:28 +00:00
|
|
|
jail -i ${jail_rootdir} ${jail_hostname} \
|
|
|
|
${jail_ip} ${jail_exec} > ${_tmp_jail} 2>&1
|
2003-08-24 06:29:32 +00:00
|
|
|
[ "$?" -eq 0 ] && echo -n " $jail_hostname"
|
2004-02-02 13:25:28 +00:00
|
|
|
_jail_id=$(head -1 ${_tmp_jail})
|
|
|
|
tail +2 ${_tmp_jail} >${jail_rootdir}/var/log/console.log
|
|
|
|
rm -f ${_tmp_jail}
|
|
|
|
echo ${_jail_id} > /var/run/jail_${_jail}.id
|
2003-04-16 16:27:23 +00:00
|
|
|
done
|
2004-08-16 16:37:06 +00:00
|
|
|
rmdir ${_tmp_dir}
|
2003-08-24 06:29:32 +00:00
|
|
|
echo '.'
|
2003-04-16 16:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jail_stop()
|
|
|
|
{
|
2004-02-03 12:59:30 +00:00
|
|
|
echo -n 'Stopping jails:'
|
2004-02-02 13:25:28 +00:00
|
|
|
for _jail in ${jail_list}
|
|
|
|
do
|
2004-08-19 08:55:24 +00:00
|
|
|
if [ -f "/var/run/jail_${_jail}.id" ]; then
|
2004-02-02 13:25:28 +00:00
|
|
|
_jail_id=$(cat /var/run/jail_${_jail}.id)
|
2004-08-19 08:55:24 +00:00
|
|
|
if [ ! -z "${_jail_id}" ]; then
|
2004-02-03 12:59:30 +00:00
|
|
|
init_variables $_jail
|
2004-02-02 13:25:28 +00:00
|
|
|
killall -j ${_jail_id} -TERM > /dev/null 2>&1
|
2004-02-03 12:59:30 +00:00
|
|
|
jail_umount_fs
|
|
|
|
echo -n " $jail_hostname"
|
2004-02-02 13:25:28 +00:00
|
|
|
fi
|
|
|
|
rm /var/run/jail_${_jail}.id
|
|
|
|
else
|
2004-02-03 12:59:30 +00:00
|
|
|
echo "cannot stop jail ${_jail}. No jail id in /var/run"
|
2003-08-24 06:29:32 +00:00
|
|
|
fi
|
|
|
|
done
|
2004-02-03 12:59:30 +00:00
|
|
|
echo '.'
|
2003-04-16 16:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
load_rc_config $name
|
2004-02-03 12:59:30 +00:00
|
|
|
[ -n "$2" ] && jail_list="$2"
|
2003-04-16 16:27:23 +00:00
|
|
|
run_rc_command "$1"
|