2003-04-16 16:27:23 +00:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# $FreeBSD$
|
|
|
|
#
|
|
|
|
|
|
|
|
# PROVIDE: jail
|
2005-01-16 03:12:03 +00:00
|
|
|
# REQUIRE: LOGIN cleanvar
|
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
|
|
|
|
2008-01-13 14:27:53 +00:00
|
|
|
# WARNING: This script deals with untrusted data (the data and
|
|
|
|
# processes inside the jails) and care must be taken when changing the
|
|
|
|
# code related to this! If you have any doubt whether a change is
|
|
|
|
# correct and have security impact, please get the patch reviewed by
|
|
|
|
# the FreeBSD Security Team prior to commit.
|
|
|
|
|
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
|
|
|
|
|
2006-05-11 14:23:43 +00:00
|
|
|
eval _rootdir=\"\$jail_${_j}_rootdir\"
|
|
|
|
_devdir="${_rootdir}/dev"
|
|
|
|
_fdescdir="${_devdir}/fd"
|
|
|
|
_procdir="${_rootdir}/proc"
|
|
|
|
eval _hostname=\"\$jail_${_j}_hostname\"
|
|
|
|
eval _ip=\"\$jail_${_j}_ip\"
|
|
|
|
eval _interface=\"\${jail_${_j}_interface:-${jail_interface}}\"
|
|
|
|
eval _exec=\"\$jail_${_j}_exec\"
|
2009-04-28 09:45:32 +00:00
|
|
|
|
|
|
|
i=0
|
|
|
|
while : ; do
|
|
|
|
eval _exec_prestart${i}=\"\${jail_${_j}_exec_prestart${i}:-\${jail_exec_prestart${i}}}\"
|
|
|
|
[ -z "$(eval echo \"\$_exec_prestart${i}\")" ] && break
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
|
|
|
|
2006-05-11 14:23:43 +00:00
|
|
|
eval _exec_start=\"\${jail_${_j}_exec_start:-${jail_exec_start}}\"
|
2006-05-30 16:20:48 +00:00
|
|
|
|
|
|
|
i=1
|
|
|
|
while [ true ]; do
|
|
|
|
eval _exec_afterstart${i}=\"\${jail_${_j}_exec_afterstart${i}:-\${jail_exec_afterstart${i}}}\"
|
2007-05-24 06:01:06 +00:00
|
|
|
[ -z "$(eval echo \"\$_exec_afterstart${i}\")" ] && break
|
2006-05-30 16:20:48 +00:00
|
|
|
i=$((i + 1))
|
|
|
|
done
|
2009-04-28 09:45:32 +00:00
|
|
|
|
|
|
|
i=0
|
|
|
|
while : ; do
|
|
|
|
eval _exec_poststart${i}=\"\${jail_${_j}_exec_poststart${i}:-\${jail_exec_poststart${i}}}\"
|
|
|
|
[ -z "$(eval echo \"\$_exec_poststart${i}\")" ] && break
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
|
|
|
|
|
|
|
i=0
|
|
|
|
while : ; do
|
|
|
|
eval _exec_prestop${i}=\"\${jail_${_j}_exec_prestop${i}:-\${jail_exec_prestop${i}}}\"
|
|
|
|
[ -z "$(eval echo \"\$_exec_prestop${i}\")" ] && break
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
|
|
|
|
2006-05-11 14:23:43 +00:00
|
|
|
eval _exec_stop=\"\${jail_${_j}_exec_stop:-${jail_exec_stop}}\"
|
2009-04-28 09:45:32 +00:00
|
|
|
|
|
|
|
i=0
|
|
|
|
while : ; do
|
|
|
|
eval _exec_poststop${i}=\"\${jail_${_j}_exec_poststop${i}:-\${jail_exec_poststop${i}}}\"
|
|
|
|
[ -z "$(eval echo \"\$_exec_poststop${i}\")" ] && break
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
|
|
|
|
2006-05-11 14:23:43 +00:00
|
|
|
if [ -n "${_exec}" ]; then
|
2004-12-14 14:36:35 +00:00
|
|
|
# simple/backward-compatible execution
|
2006-05-11 14:23:43 +00:00
|
|
|
_exec_start="${_exec}"
|
|
|
|
_exec_stop=""
|
2004-12-14 14:36:35 +00:00
|
|
|
else
|
|
|
|
# flexible execution
|
2006-05-11 14:23:43 +00:00
|
|
|
if [ -z "${_exec_start}" ]; then
|
|
|
|
_exec_start="/bin/sh /etc/rc"
|
|
|
|
if [ -z "${_exec_stop}" ]; then
|
|
|
|
_exec_stop="/bin/sh /etc/rc.shutdown"
|
2004-12-14 14:36:35 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2003-08-24 06:29:32 +00:00
|
|
|
|
|
|
|
# The default jail ruleset will be used by rc.subr if none is specified.
|
2006-05-11 14:23:43 +00:00
|
|
|
eval _ruleset=\"\${jail_${_j}_devfs_ruleset:-${jail_devfs_ruleset}}\"
|
|
|
|
eval _devfs=\"\${jail_${_j}_devfs_enable:-${jail_devfs_enable}}\"
|
|
|
|
[ -z "${_devfs}" ] && _devfs="NO"
|
|
|
|
eval _fdescfs=\"\${jail_${_j}_fdescfs_enable:-${jail_fdescfs_enable}}\"
|
|
|
|
[ -z "${_fdescfs}" ] && _fdescfs="NO"
|
|
|
|
eval _procfs=\"\${jail_${_j}_procfs_enable:-${jail_procfs_enable}}\"
|
|
|
|
[ -z "${_procfs}" ] && _procfs="NO"
|
2003-08-24 06:29:32 +00:00
|
|
|
|
2006-05-11 14:23:43 +00:00
|
|
|
eval _mount=\"\${jail_${_j}_mount_enable:-${jail_mount_enable}}\"
|
|
|
|
[ -z "${_mount}" ] && _mount="NO"
|
2004-11-23 20:09:58 +00:00
|
|
|
# "/etc/fstab.${_j}" will be used for {,u}mount(8) if none is specified.
|
2006-05-11 14:23:43 +00:00
|
|
|
eval _fstab=\"\${jail_${_j}_fstab:-${jail_fstab}}\"
|
|
|
|
[ -z "${_fstab}" ] && _fstab="/etc/fstab.${_j}"
|
|
|
|
eval _flags=\"\${jail_${_j}_flags:-${jail_flags}}\"
|
|
|
|
[ -z "${_flags}" ] && _flags="-l -U root"
|
2007-01-11 18:18:57 +00:00
|
|
|
eval _consolelog=\"\${jail_${_j}_consolelog:-${jail_consolelog}}\"
|
|
|
|
[ -z "${_consolelog}" ] && _consolelog="/var/log/jail_${_j}_console.log"
|
2008-09-16 20:18:25 +00:00
|
|
|
eval _fib=\"\${jail_${_j}_fib:-${jail_fib}}\"
|
2004-11-23 20:09:58 +00:00
|
|
|
|
2004-02-03 12:59:30 +00:00
|
|
|
# Debugging aid
|
2003-08-24 06:29:32 +00:00
|
|
|
#
|
2006-05-11 14:23:43 +00:00
|
|
|
debug "$_j devfs enable: $_devfs"
|
|
|
|
debug "$_j fdescfs enable: $_fdescfs"
|
|
|
|
debug "$_j procfs enable: $_procfs"
|
|
|
|
debug "$_j mount enable: $_mount"
|
|
|
|
debug "$_j hostname: $_hostname"
|
|
|
|
debug "$_j ip: $_ip"
|
2009-01-26 12:59:11 +00:00
|
|
|
jail_show_addresses ${_j}
|
2006-05-11 14:23:43 +00:00
|
|
|
debug "$_j interface: $_interface"
|
2008-09-16 20:18:25 +00:00
|
|
|
debug "$_j fib: $_fib"
|
2006-05-11 14:23:43 +00:00
|
|
|
debug "$_j root: $_rootdir"
|
|
|
|
debug "$_j devdir: $_devdir"
|
|
|
|
debug "$_j fdescdir: $_fdescdir"
|
|
|
|
debug "$_j procdir: $_procdir"
|
|
|
|
debug "$_j ruleset: $_ruleset"
|
|
|
|
debug "$_j fstab: $_fstab"
|
2007-01-11 18:18:57 +00:00
|
|
|
debug "$_j consolelog: $_consolelog"
|
2006-05-30 16:20:48 +00:00
|
|
|
|
2009-04-28 09:45:32 +00:00
|
|
|
i=0
|
|
|
|
while : ; do
|
|
|
|
eval out=\"\${_exec_prestart${i}:-''}\"
|
|
|
|
if [ -z "$out" ]; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
debug "$_j exec pre-start #${i}: ${out}"
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
|
|
|
|
|
|
|
debug "$_j exec start: $_exec_start"
|
|
|
|
|
2006-05-30 16:20:48 +00:00
|
|
|
i=1
|
|
|
|
while [ true ]; do
|
|
|
|
eval out=\"\${_exec_afterstart${i}:-''}\"
|
|
|
|
|
|
|
|
if [ -z "$out" ]; then
|
|
|
|
break;
|
|
|
|
fi
|
|
|
|
|
|
|
|
debug "$_j exec after start #${i}: ${out}"
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
|
|
|
|
2009-04-28 09:45:32 +00:00
|
|
|
i=0
|
|
|
|
while : ; do
|
|
|
|
eval out=\"\${_exec_poststart${i}:-''}\"
|
|
|
|
if [ -z "$out" ]; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
debug "$_j exec post-start #${i}: ${out}"
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
|
|
|
|
|
|
|
i=0
|
|
|
|
while : ; do
|
|
|
|
eval out=\"\${_exec_prestop${i}:-''}\"
|
|
|
|
if [ -z "$out" ]; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
debug "$_j exec pre-stop #${i}: ${out}"
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
|
|
|
|
2006-05-11 14:23:43 +00:00
|
|
|
debug "$_j exec stop: $_exec_stop"
|
2009-04-28 09:45:32 +00:00
|
|
|
|
|
|
|
i=0
|
|
|
|
while : ; do
|
|
|
|
eval out=\"\${_exec_poststop${i}:-''}\"
|
|
|
|
if [ -z "$out" ]; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
debug "$_j exec post-stop #${i}: ${out}"
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
|
|
|
|
2006-05-11 14:23:43 +00:00
|
|
|
debug "$_j flags: $_flags"
|
2007-01-11 18:18:57 +00:00
|
|
|
debug "$_j consolelog: $_consolelog"
|
2006-05-07 23:15:39 +00:00
|
|
|
|
2006-05-11 14:23:43 +00:00
|
|
|
if [ -z "${_hostname}" ]; then
|
2006-05-07 23:15:39 +00:00
|
|
|
err 3 "$name: No hostname has been defined for ${_j}"
|
|
|
|
fi
|
2006-05-11 14:23:43 +00:00
|
|
|
if [ -z "${_rootdir}" ]; then
|
2006-05-07 23:15:39 +00:00
|
|
|
err 3 "$name: No root directory has been defined for ${_j}"
|
|
|
|
fi
|
2003-08-24 06:29:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2007-01-11 18:18:57 +00:00
|
|
|
# is_current_mountpoint()
|
|
|
|
# Is the directory mount point for a currently mounted file
|
|
|
|
# system?
|
|
|
|
#
|
|
|
|
is_current_mountpoint()
|
|
|
|
{
|
|
|
|
local _dir _dir2
|
|
|
|
|
|
|
|
_dir=$1
|
|
|
|
|
|
|
|
_dir=`echo $_dir | sed -Ee 's#//+#/#g' -e 's#/$##'`
|
|
|
|
[ ! -d "${_dir}" ] && return 1
|
|
|
|
_dir2=`df ${_dir} | tail +2 | awk '{ print $6 }'`
|
|
|
|
[ "${_dir}" = "${_dir2}" ]
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
# is_symlinked_mountpoint()
|
|
|
|
# Is a mount point, or any of its parent directories, a symlink?
|
|
|
|
#
|
|
|
|
is_symlinked_mountpoint()
|
|
|
|
{
|
|
|
|
local _dir
|
|
|
|
|
|
|
|
_dir=$1
|
|
|
|
|
|
|
|
[ -L "$_dir" ] && return 0
|
|
|
|
[ "$_dir" = "/" ] && return 1
|
|
|
|
is_symlinked_mountpoint `dirname $_dir`
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
# secure_umount
|
|
|
|
# Try to unmount a mount point without being vulnerable to
|
|
|
|
# symlink attacks.
|
|
|
|
#
|
|
|
|
secure_umount()
|
|
|
|
{
|
|
|
|
local _dir
|
|
|
|
|
|
|
|
_dir=$1
|
|
|
|
|
|
|
|
if is_current_mountpoint ${_dir}; then
|
|
|
|
umount -f ${_dir} >/dev/null 2>&1
|
|
|
|
else
|
|
|
|
debug "Nothing mounted on ${_dir} - not unmounting"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2007-01-11 18:18:57 +00:00
|
|
|
local _device _mountpt _rest
|
|
|
|
|
2006-05-11 14:23:43 +00:00
|
|
|
if checkyesno _fdescfs; then
|
|
|
|
if [ -d "${_fdescdir}" ] ; then
|
2007-01-11 18:18:57 +00:00
|
|
|
secure_umount ${_fdescdir}
|
2004-02-03 12:59:30 +00:00
|
|
|
fi
|
|
|
|
fi
|
2006-05-11 14:23:43 +00:00
|
|
|
if checkyesno _devfs; then
|
|
|
|
if [ -d "${_devdir}" ] ; then
|
2007-01-11 18:18:57 +00:00
|
|
|
secure_umount ${_devdir}
|
2004-02-03 12:59:30 +00:00
|
|
|
fi
|
|
|
|
fi
|
2006-05-11 14:23:43 +00:00
|
|
|
if checkyesno _procfs; then
|
|
|
|
if [ -d "${_procdir}" ] ; then
|
2007-01-11 18:18:57 +00:00
|
|
|
secure_umount ${_procdir}
|
2004-02-03 12:59:30 +00:00
|
|
|
fi
|
|
|
|
fi
|
2006-05-11 14:23:43 +00:00
|
|
|
if checkyesno _mount; then
|
|
|
|
[ -f "${_fstab}" ] || warn "${_fstab} does not exist"
|
2007-01-11 18:18:57 +00:00
|
|
|
tail -r ${_fstab} | while read _device _mountpt _rest; do
|
|
|
|
case ":${_device}" in
|
|
|
|
:#* | :)
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
secure_umount ${_mountpt}
|
|
|
|
done
|
2004-11-23 20:09:58 +00:00
|
|
|
fi
|
2004-02-03 12:59:30 +00:00
|
|
|
}
|
|
|
|
|
2007-01-11 18:18:57 +00:00
|
|
|
# jail_mount_fstab()
|
|
|
|
# Mount file systems from a per jail fstab while trying to
|
|
|
|
# secure against symlink attacks at the mount points.
|
|
|
|
#
|
|
|
|
# If we are certain we cannot secure against symlink attacks we
|
|
|
|
# do not mount all of the file systems (since we cannot just not
|
|
|
|
# mount the file system with the problematic mount point).
|
|
|
|
#
|
|
|
|
# The caller must call the init_variables() routine before
|
|
|
|
# calling this one.
|
|
|
|
#
|
|
|
|
jail_mount_fstab()
|
|
|
|
{
|
|
|
|
local _device _mountpt _rest
|
|
|
|
|
|
|
|
while read _device _mountpt _rest; do
|
|
|
|
case ":${_device}" in
|
|
|
|
:#* | :)
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
if is_symlinked_mountpoint ${_mountpt}; then
|
|
|
|
warn "${_mountpt} has symlink as parent - not mounting from ${_fstab}"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
done <${_fstab}
|
|
|
|
mount -a -F "${_fstab}"
|
|
|
|
}
|
|
|
|
|
2009-01-26 12:59:11 +00:00
|
|
|
# jail_show_addresses jail
|
|
|
|
# Debug print the input for the given _multi aliases
|
|
|
|
# for a jail for init_variables().
|
|
|
|
#
|
|
|
|
jail_show_addresses()
|
|
|
|
{
|
|
|
|
local _j _type alias
|
|
|
|
_j="$1"
|
|
|
|
alias=0
|
|
|
|
|
|
|
|
if [ -z "${_j}" ]; then
|
|
|
|
warn "jail_show_addresses: you must specify a jail"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
while : ; do
|
|
|
|
eval _addr=\"\$jail_${_j}_ip_multi${alias}\"
|
|
|
|
if [ -n "${_addr}" ]; then
|
|
|
|
debug "${_j} ip_multi${alias}: $_addr"
|
|
|
|
alias=$((${alias} + 1))
|
|
|
|
else
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# jail_extract_address argument
|
|
|
|
# The second argument is the string from one of the _ip
|
|
|
|
# or the _multi variables. In case of a comma separated list
|
|
|
|
# only one argument must be passed in at a time.
|
|
|
|
# The function alters the _type, _iface, _addr and _mask variables.
|
|
|
|
#
|
|
|
|
jail_extract_address()
|
|
|
|
{
|
|
|
|
local _i
|
|
|
|
_i=$1
|
|
|
|
|
|
|
|
if [ -z "${_i}" ]; then
|
|
|
|
warn "jail_extract_address: called without input"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if we have an interface prefix given and split into
|
|
|
|
# iFace and rest.
|
|
|
|
case "${_i}" in
|
|
|
|
*\|*) # ifN|.. prefix there
|
|
|
|
_iface=${_i%%|*}
|
|
|
|
_r=${_i##*|}
|
|
|
|
;;
|
|
|
|
*) _iface=""
|
|
|
|
_r=${_i}
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# In case the IP has no interface given, check if we have a global one.
|
|
|
|
_iface=${_iface:-${_interface}}
|
|
|
|
|
|
|
|
# Set address, cut off any prefix/netmask/prefixlen.
|
|
|
|
_addr=${_r}
|
|
|
|
_addr=${_addr%%[/ ]*}
|
|
|
|
|
|
|
|
# Theoretically we can return here if interface is not set,
|
|
|
|
# as we only care about the _mask if we call ifconfig.
|
|
|
|
# This is not done because we may want to santize IP addresses
|
|
|
|
# based on _type later, and optionally change the type as well.
|
|
|
|
|
|
|
|
# Extract the prefix/netmask/prefixlen part by cutting off the address.
|
|
|
|
_mask=${_r}
|
|
|
|
_mask=`expr "${_mask}" : "${_addr}\(.*\)"`
|
|
|
|
|
|
|
|
# Identify type {inet,inet6}.
|
|
|
|
case "${_addr}" in
|
|
|
|
*\.*\.*\.*) _type="inet" ;;
|
|
|
|
*:*) _type="inet6" ;;
|
|
|
|
*) warn "jail_extract_address: type not identified"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# Handle the special /netmask instead of /prefix or
|
|
|
|
# "netmask xxx" case for legacy IP.
|
|
|
|
# We do NOT support shortend class-full netmasks.
|
|
|
|
if [ "${_type}" = "inet" ]; then
|
|
|
|
case "${_mask}" in
|
|
|
|
/*\.*\.*\.*) _mask=" netmask ${_mask#/}" ;;
|
|
|
|
*) ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# In case _mask is still not set use /32.
|
|
|
|
_mask=${_mask:-/32}
|
|
|
|
|
|
|
|
elif [ "${_type}" = "inet6" ]; then
|
|
|
|
# In case _maske is not set for IPv6, use /128.
|
|
|
|
_mask=${_mask:-/128}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# jail_handle_ips_option {add,del} input
|
|
|
|
# Handle a single argument imput which can be a comma separated
|
|
|
|
# list of addresses (theoretically with an option interface and
|
|
|
|
# prefix/netmask/prefixlen).
|
|
|
|
#
|
|
|
|
jail_handle_ips_option()
|
|
|
|
{
|
|
|
|
local _x _action _type _i
|
|
|
|
_action=$1
|
|
|
|
_x=$2
|
|
|
|
|
|
|
|
if [ -z "${_x}" ]; then
|
|
|
|
# No IP given. This can happen for the primary address
|
|
|
|
# of each address family.
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Loop, in case we find a comma separated list, we need to handle
|
|
|
|
# each argument on its own.
|
|
|
|
while [ ${#_x} -gt 0 ]; do
|
|
|
|
case "${_x}" in
|
|
|
|
*,*) # Extract the first argument and strip it off the list.
|
|
|
|
_i=`expr "${_x}" : '^\([^,]*\)'`
|
|
|
|
_x=`expr "${_x}" : "^[^,]*,\(.*\)"`
|
|
|
|
;;
|
|
|
|
*) _i=${_x}
|
|
|
|
_x=""
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
_type=""
|
|
|
|
_iface=""
|
|
|
|
_addr=""
|
|
|
|
_mask=""
|
|
|
|
jail_extract_address "${_i}"
|
|
|
|
|
|
|
|
# make sure we got an address.
|
|
|
|
case "${_addr}" in
|
|
|
|
"") continue ;;
|
|
|
|
*) ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# Append address to list of addresses for the jail command.
|
|
|
|
case "${_addrl}" in
|
|
|
|
"") _addrl="${_addr}" ;;
|
|
|
|
*) _addrl="${_addrl},${_addr}" ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# Configure interface alias if requested by a given interface
|
|
|
|
# and if we could correctly parse everything.
|
|
|
|
case "${_iface}" in
|
|
|
|
"") continue ;;
|
|
|
|
esac
|
|
|
|
case "${_type}" in
|
|
|
|
inet) ;;
|
|
|
|
inet6) ;;
|
|
|
|
*) warn "Could not determine address family. Not going" \
|
|
|
|
"to ${_action} address '${_addr}' for ${_jail}."
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
case "${_action}" in
|
|
|
|
add) ifconfig ${_iface} ${_type} ${_addr}${_mask} alias
|
|
|
|
;;
|
|
|
|
del) # When removing the IP, ignore the _mask.
|
|
|
|
ifconfig ${_iface} ${_type} ${_addr} -alias
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# jail_ips {add,del}
|
|
|
|
# Extract the comma separated list of addresses and return them
|
|
|
|
# for the jail command.
|
|
|
|
# Handle more than one address via the _multi option as well.
|
|
|
|
# If an interface is given also add/remove an alias for the
|
|
|
|
# address with an optional netmask.
|
|
|
|
#
|
|
|
|
jail_ips()
|
|
|
|
{
|
|
|
|
local _action
|
|
|
|
_action=$1
|
|
|
|
|
|
|
|
case "${_action}" in
|
|
|
|
add) ;;
|
|
|
|
del) ;;
|
|
|
|
*) warn "jail_ips: invalid action '${_action}'"
|
|
|
|
return
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# Handle addresses.
|
|
|
|
jail_handle_ips_option ${_action} "${_ip}"
|
|
|
|
# Handle jail_xxx_ip_multi<N>
|
|
|
|
alias=0
|
|
|
|
while : ; do
|
|
|
|
eval _x=\"\$jail_${_jail}_ip_multi${alias}\"
|
|
|
|
case "${_x}" in
|
|
|
|
"") break ;;
|
|
|
|
*) jail_handle_ips_option ${_action} "${_x}"
|
|
|
|
alias=$((${alias} + 1))
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
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
|
2005-08-07 23:19:02 +00:00
|
|
|
if [ -f /var/run/jail_${_jail}.id ]; then
|
2006-05-11 14:23:43 +00:00
|
|
|
echo -n " [${_hostname} already running (/var/run/jail_${_jail}.id exists)]"
|
2005-08-07 23:19:02 +00:00
|
|
|
continue;
|
|
|
|
fi
|
2009-01-26 12:59:11 +00:00
|
|
|
_addrl=""
|
|
|
|
jail_ips "add"
|
2008-09-16 20:18:25 +00:00
|
|
|
if [ -n "${_fib}" ]; then
|
|
|
|
_setfib="setfib -F '${_fib}'"
|
|
|
|
else
|
|
|
|
_setfib=""
|
|
|
|
fi
|
2006-05-11 14:23:43 +00:00
|
|
|
if checkyesno _mount; then
|
|
|
|
info "Mounting fstab for jail ${_jail} (${_fstab})"
|
|
|
|
if [ ! -f "${_fstab}" ]; then
|
|
|
|
err 3 "$name: ${_fstab} does not exist"
|
2004-11-23 20:09:58 +00:00
|
|
|
fi
|
2007-01-11 18:18:57 +00:00
|
|
|
jail_mount_fstab
|
2004-11-23 20:09:58 +00:00
|
|
|
fi
|
2006-05-11 14:23:43 +00:00
|
|
|
if checkyesno _devfs; then
|
2005-04-30 00:16:00 +00:00
|
|
|
# If devfs is already mounted here, skip it.
|
2006-05-11 14:23:43 +00:00
|
|
|
df -t devfs "${_devdir}" >/dev/null
|
2005-04-30 00:16:00 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
2007-01-11 18:18:57 +00:00
|
|
|
if is_symlinked_mountpoint ${_devdir}; then
|
|
|
|
warn "${_devdir} has symlink as parent - not starting jail ${_jail}"
|
|
|
|
continue
|
|
|
|
fi
|
2006-05-11 14:23:43 +00:00
|
|
|
info "Mounting devfs on ${_devdir}"
|
|
|
|
devfs_mount_jail "${_devdir}" ${_ruleset}
|
2005-04-30 00:16:00 +00:00
|
|
|
# Transitional symlink for old binaries
|
2006-05-11 14:23:43 +00:00
|
|
|
if [ ! -L "${_devdir}/log" ]; then
|
2005-04-30 00:16:00 +00:00
|
|
|
__pwd="`pwd`"
|
2006-05-11 14:23:43 +00:00
|
|
|
cd "${_devdir}"
|
2005-04-30 00:16:00 +00:00
|
|
|
ln -sf ../var/run/log log
|
|
|
|
cd "$__pwd"
|
|
|
|
fi
|
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`"
|
2006-05-11 14:23:43 +00:00
|
|
|
# cd "${_devdir}"
|
2003-12-09 08:51:11 +00:00
|
|
|
# ln -sf ../var/log/console console
|
|
|
|
# cd "$__pwd"
|
2003-08-24 06:29:32 +00:00
|
|
|
fi
|
2006-05-11 14:23:43 +00:00
|
|
|
if checkyesno _fdescfs; then
|
2007-01-11 18:18:57 +00:00
|
|
|
if is_symlinked_mountpoint ${_fdescdir}; then
|
|
|
|
warn "${_fdescdir} has symlink as parent, not mounting"
|
|
|
|
else
|
|
|
|
info "Mounting fdescfs on ${_fdescdir}"
|
|
|
|
mount -t fdescfs fdesc "${_fdescdir}"
|
|
|
|
fi
|
2003-08-24 06:29:32 +00:00
|
|
|
fi
|
2006-05-11 14:23:43 +00:00
|
|
|
if checkyesno _procfs; then
|
2007-01-11 18:18:57 +00:00
|
|
|
if is_symlinked_mountpoint ${_procdir}; then
|
|
|
|
warn "${_procdir} has symlink as parent, not mounting"
|
|
|
|
else
|
|
|
|
info "Mounting procfs onto ${_procdir}"
|
|
|
|
if [ -d "${_procdir}" ] ; then
|
|
|
|
mount -t procfs proc "${_procdir}"
|
|
|
|
fi
|
2003-08-24 06:29:32 +00:00
|
|
|
fi
|
|
|
|
fi
|
2004-08-16 16:37:06 +00:00
|
|
|
_tmp_jail=${_tmp_dir}/jail.$$
|
2009-04-28 09:45:32 +00:00
|
|
|
|
|
|
|
i=0
|
|
|
|
while : ; do
|
|
|
|
eval out=\"\${_exec_prestart${i}:-''}\"
|
|
|
|
[ -z "$out" ] && break
|
|
|
|
${out}
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
|
|
|
|
2008-09-16 20:18:25 +00:00
|
|
|
eval ${_setfib} jail ${_flags} -i ${_rootdir} ${_hostname} \
|
2009-01-26 12:59:11 +00:00
|
|
|
\"${_addrl}\" ${_exec_start} > ${_tmp_jail} 2>&1
|
2006-05-30 16:20:48 +00:00
|
|
|
|
2006-05-30 16:07:59 +00:00
|
|
|
if [ "$?" -eq 0 ] ; then
|
|
|
|
_jail_id=$(head -1 ${_tmp_jail})
|
2006-05-30 16:20:48 +00:00
|
|
|
i=1
|
|
|
|
while [ true ]; do
|
|
|
|
eval out=\"\${_exec_afterstart${i}:-''}\"
|
|
|
|
|
|
|
|
if [ -z "$out" ]; then
|
|
|
|
break;
|
|
|
|
fi
|
|
|
|
|
|
|
|
jexec "${_jail_id}" ${out}
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
|
|
|
|
|
|
|
echo -n " $_hostname"
|
2007-01-11 18:18:57 +00:00
|
|
|
tail +2 ${_tmp_jail} >${_consolelog}
|
2006-05-30 16:07:59 +00:00
|
|
|
echo ${_jail_id} > /var/run/jail_${_jail}.id
|
2009-04-28 09:45:32 +00:00
|
|
|
|
|
|
|
i=0
|
|
|
|
while : ; do
|
|
|
|
eval out=\"\${_exec_poststart${i}:-''}\"
|
|
|
|
[ -z "$out" ] && break
|
|
|
|
${out}
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
2006-05-30 16:07:59 +00:00
|
|
|
else
|
|
|
|
jail_umount_fs
|
2009-01-26 12:59:11 +00:00
|
|
|
jail_ips "del"
|
2006-05-30 16:07:59 +00:00
|
|
|
echo " cannot start jail \"${_jail}\": "
|
|
|
|
tail +2 ${_tmp_jail}
|
|
|
|
fi
|
2004-02-02 13:25:28 +00:00
|
|
|
rm -f ${_tmp_jail}
|
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
|
2009-04-28 09:45:32 +00:00
|
|
|
|
|
|
|
i=0
|
|
|
|
while : ; do
|
|
|
|
eval out=\"\${_exec_prestop${i}:-''}\"
|
|
|
|
[ -z "$out" ] && break
|
|
|
|
${out}
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
|
|
|
|
2006-05-11 14:23:43 +00:00
|
|
|
if [ -n "${_exec_stop}" ]; then
|
|
|
|
eval env -i /usr/sbin/jexec ${_jail_id} ${_exec_stop} \
|
2007-01-11 18:18:57 +00:00
|
|
|
>> ${_consolelog} 2>&1
|
2004-12-14 14:36:35 +00:00
|
|
|
fi
|
2004-02-02 13:25:28 +00:00
|
|
|
killall -j ${_jail_id} -TERM > /dev/null 2>&1
|
2004-12-14 14:36:35 +00:00
|
|
|
sleep 1
|
|
|
|
killall -j ${_jail_id} -KILL > /dev/null 2>&1
|
2004-02-03 12:59:30 +00:00
|
|
|
jail_umount_fs
|
2006-05-11 14:23:43 +00:00
|
|
|
echo -n " $_hostname"
|
2009-04-28 09:45:32 +00:00
|
|
|
|
|
|
|
i=0
|
|
|
|
while : ; do
|
|
|
|
eval out=\"\${_exec_poststop${i}:-''}\"
|
|
|
|
[ -z "$out" ] && break
|
|
|
|
${out}
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
2004-02-02 13:25:28 +00:00
|
|
|
fi
|
2009-01-26 12:59:11 +00:00
|
|
|
jail_ips "del"
|
2004-02-02 13:25:28 +00:00
|
|
|
rm /var/run/jail_${_jail}.id
|
|
|
|
else
|
2006-05-09 17:50:16 +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
|
2005-08-07 22:38:41 +00:00
|
|
|
cmd="$1"
|
|
|
|
if [ $# -gt 0 ]; then
|
|
|
|
shift
|
|
|
|
fi
|
2006-03-08 20:40:37 +00:00
|
|
|
if [ -n "$*" ]; then
|
|
|
|
jail_list="$*"
|
|
|
|
fi
|
2005-08-07 22:38:41 +00:00
|
|
|
run_rc_command "${cmd}"
|