7 # REQUIRE: LOGIN cleanvar
9 # KEYWORD: nojail shutdown
15 start_cmd="jail_start"
19 # Initialize the various jail variables for jail _j.
26 warn "init_variables: you must specify a jail"
30 eval _rootdir=\"\$jail_${_j}_rootdir\"
31 _devdir="${_rootdir}/dev"
32 _fdescdir="${_devdir}/fd"
33 _procdir="${_rootdir}/proc"
34 eval _hostname=\"\$jail_${_j}_hostname\"
35 eval _ip=\"\$jail_${_j}_ip\"
36 eval _interface=\"\${jail_${_j}_interface:-${jail_interface}}\"
37 eval _exec=\"\$jail_${_j}_exec\"
38 eval _exec_start=\"\${jail_${_j}_exec_start:-${jail_exec_start}}\"
42 eval _exec_afterstart${i}=\"\${jail_${_j}_exec_afterstart${i}:-\${jail_exec_afterstart${i}}}\"
43 [ -z "$(eval echo \"\$_exec_afterstart${i}\")" ] && break
47 eval _exec_stop=\"\${jail_${_j}_exec_stop:-${jail_exec_stop}}\"
48 if [ -n "${_exec}" ]; then
49 # simple/backward-compatible execution
50 _exec_start="${_exec}"
54 if [ -z "${_exec_start}" ]; then
55 _exec_start="/bin/sh /etc/rc"
56 if [ -z "${_exec_stop}" ]; then
57 _exec_stop="/bin/sh /etc/rc.shutdown"
62 # The default jail ruleset will be used by rc.subr if none is specified.
63 eval _ruleset=\"\${jail_${_j}_devfs_ruleset:-${jail_devfs_ruleset}}\"
64 eval _devfs=\"\${jail_${_j}_devfs_enable:-${jail_devfs_enable}}\"
65 [ -z "${_devfs}" ] && _devfs="NO"
66 eval _fdescfs=\"\${jail_${_j}_fdescfs_enable:-${jail_fdescfs_enable}}\"
67 [ -z "${_fdescfs}" ] && _fdescfs="NO"
68 eval _procfs=\"\${jail_${_j}_procfs_enable:-${jail_procfs_enable}}\"
69 [ -z "${_procfs}" ] && _procfs="NO"
71 eval _mount=\"\${jail_${_j}_mount_enable:-${jail_mount_enable}}\"
72 [ -z "${_mount}" ] && _mount="NO"
73 # "/etc/fstab.${_j}" will be used for {,u}mount(8) if none is specified.
74 eval _fstab=\"\${jail_${_j}_fstab:-${jail_fstab}}\"
75 [ -z "${_fstab}" ] && _fstab="/etc/fstab.${_j}"
76 eval _flags=\"\${jail_${_j}_flags:-${jail_flags}}\"
77 [ -z "${_flags}" ] && _flags="-l -U root"
78 eval _consolelog=\"\${jail_${_j}_consolelog:-${jail_consolelog}}\"
79 [ -z "${_consolelog}" ] && _consolelog="/var/log/jail_${_j}_console.log"
83 debug "$_j devfs enable: $_devfs"
84 debug "$_j fdescfs enable: $_fdescfs"
85 debug "$_j procfs enable: $_procfs"
86 debug "$_j mount enable: $_mount"
87 debug "$_j hostname: $_hostname"
89 debug "$_j interface: $_interface"
90 debug "$_j root: $_rootdir"
91 debug "$_j devdir: $_devdir"
92 debug "$_j fdescdir: $_fdescdir"
93 debug "$_j procdir: $_procdir"
94 debug "$_j ruleset: $_ruleset"
95 debug "$_j fstab: $_fstab"
96 debug "$_j exec start: $_exec_start"
97 debug "$_j consolelog: $_consolelog"
101 eval out=\"\${_exec_afterstart${i}:-''}\"
103 if [ -z "$out" ]; then
107 debug "$_j exec after start #${i}: ${out}"
111 debug "$_j exec stop: $_exec_stop"
112 debug "$_j flags: $_flags"
113 debug "$_j consolelog: $_consolelog"
115 if [ -z "${_hostname}" ]; then
116 err 3 "$name: No hostname has been defined for ${_j}"
118 if [ -z "${_rootdir}" ]; then
119 err 3 "$name: No root directory has been defined for ${_j}"
121 if [ -z "${_ip}" ]; then
122 err 3 "$name: No IP address has been defined for ${_j}"
127 # set_sysctl rc_knob mib msg
128 # If the mib sysctl is set according to what rc_knob
129 # specifies, this function does nothing. However if
130 # rc_knob is set differently than mib, then the mib
131 # is set accordingly and msg is displayed followed by
132 # an '=" sign and the word 'YES' or 'NO'.
140 _current=`${SYSCTL} -n $_mib 2>/dev/null`
141 if checkyesno $_knob ; then
142 if [ "$_current" -ne 1 ]; then
143 echo -n " ${_msg}=YES"
144 ${SYSCTL_W} 1>/dev/null ${_mib}=1
147 if [ "$_current" -ne 0 ]; then
148 echo -n " ${_msg}=NO"
149 ${SYSCTL_W} 1>/dev/null ${_mib}=0
154 # is_current_mountpoint()
155 # Is the directory mount point for a currently mounted file
158 is_current_mountpoint()
164 _dir=`echo $_dir | sed -Ee 's#//+#/#g' -e 's#/$##'`
165 [ ! -d "${_dir}" ] && return 1
166 _dir2=`df ${_dir} | tail +2 | awk '{ print $6 }'`
167 [ "${_dir}" = "${_dir2}" ]
171 # is_symlinked_mountpoint()
172 # Is a mount point, or any of its parent directories, a symlink?
174 is_symlinked_mountpoint()
180 [ -L "$_dir" ] && return 0
181 [ "$_dir" = "/" ] && return 1
182 is_symlinked_mountpoint `dirname $_dir`
187 # Try to unmount a mount point without being vulnerable to
196 if is_current_mountpoint ${_dir}; then
197 umount -f ${_dir} >/dev/null 2>&1
199 debug "Nothing mounted on ${_dir} - not unmounting"
205 # This function unmounts certain special filesystems in the
206 # currently selected jail. The caller must call the init_variables()
207 # routine before calling this one.
211 local _device _mountpt _rest
213 if checkyesno _fdescfs; then
214 if [ -d "${_fdescdir}" ] ; then
215 secure_umount ${_fdescdir}
218 if checkyesno _devfs; then
219 if [ -d "${_devdir}" ] ; then
220 secure_umount ${_devdir}
223 if checkyesno _procfs; then
224 if [ -d "${_procdir}" ] ; then
225 secure_umount ${_procdir}
228 if checkyesno _mount; then
229 [ -f "${_fstab}" ] || warn "${_fstab} does not exist"
230 tail -r ${_fstab} | while read _device _mountpt _rest; do
231 case ":${_device}" in
236 secure_umount ${_mountpt}
242 # Mount file systems from a per jail fstab while trying to
243 # secure against symlink attacks at the mount points.
245 # If we are certain we cannot secure against symlink attacks we
246 # do not mount all of the file systems (since we cannot just not
247 # mount the file system with the problematic mount point).
249 # The caller must call the init_variables() routine before
254 local _device _mountpt _rest
256 while read _device _mountpt _rest; do
257 case ":${_device}" in
262 if is_symlinked_mountpoint ${_mountpt}; then
263 warn "${_mountpt} has symlink as parent - not mounting from ${_fstab}"
267 mount -a -F "${_fstab}"
272 echo -n 'Configuring jails:'
273 set_sysctl jail_set_hostname_allow security.jail.set_hostname_allowed \
275 set_sysctl jail_socket_unixiproute_only \
276 security.jail.socket_unixiproute_only unixiproute_only
277 set_sysctl jail_sysvipc_allow security.jail.sysvipc_allowed \
281 echo -n 'Starting jails:'
282 _tmp_dir=`mktemp -d /tmp/jail.XXXXXXXX` || \
283 err 3 "$name: Can't create temp dir, exiting..."
284 for _jail in ${jail_list}
286 init_variables $_jail
287 if [ -f /var/run/jail_${_jail}.id ]; then
288 echo -n " [${_hostname} already running (/var/run/jail_${_jail}.id exists)]"
291 if [ -n "${_interface}" ]; then
292 ifconfig ${_interface} alias ${_ip} netmask 255.255.255.255
294 if checkyesno _mount; then
295 info "Mounting fstab for jail ${_jail} (${_fstab})"
296 if [ ! -f "${_fstab}" ]; then
297 err 3 "$name: ${_fstab} does not exist"
301 if checkyesno _devfs; then
302 # If devfs is already mounted here, skip it.
303 df -t devfs "${_devdir}" >/dev/null
304 if [ $? -ne 0 ]; then
305 if is_symlinked_mountpoint ${_devdir}; then
306 warn "${_devdir} has symlink as parent - not starting jail ${_jail}"
309 info "Mounting devfs on ${_devdir}"
310 devfs_mount_jail "${_devdir}" ${_ruleset}
311 # Transitional symlink for old binaries
312 if [ ! -L "${_devdir}/log" ]; then
315 ln -sf ../var/run/log log
320 # XXX - It seems symlinks don't work when there
321 # is a devfs(5) device of the same name.
322 # Jail console output
325 # ln -sf ../var/log/console console
328 if checkyesno _fdescfs; then
329 if is_symlinked_mountpoint ${_fdescdir}; then
330 warn "${_fdescdir} has symlink as parent, not mounting"
332 info "Mounting fdescfs on ${_fdescdir}"
333 mount -t fdescfs fdesc "${_fdescdir}"
336 if checkyesno _procfs; then
337 if is_symlinked_mountpoint ${_procdir}; then
338 warn "${_procdir} has symlink as parent, not mounting"
340 info "Mounting procfs onto ${_procdir}"
341 if [ -d "${_procdir}" ] ; then
342 mount -t procfs proc "${_procdir}"
346 _tmp_jail=${_tmp_dir}/jail.$$
347 eval jail ${_flags} -i ${_rootdir} ${_hostname} \
348 ${_ip} ${_exec_start} > ${_tmp_jail} 2>&1
350 if [ "$?" -eq 0 ] ; then
351 _jail_id=$(head -1 ${_tmp_jail})
354 eval out=\"\${_exec_afterstart${i}:-''}\"
356 if [ -z "$out" ]; then
360 jexec "${_jail_id}" ${out}
364 echo -n " $_hostname"
365 tail +2 ${_tmp_jail} >${_consolelog}
366 echo ${_jail_id} > /var/run/jail_${_jail}.id
369 if [ -n "${_interface}" ]; then
370 ifconfig ${_interface} -alias ${_ip}
372 echo " cannot start jail \"${_jail}\": "
383 echo -n 'Stopping jails:'
384 for _jail in ${jail_list}
386 if [ -f "/var/run/jail_${_jail}.id" ]; then
387 _jail_id=$(cat /var/run/jail_${_jail}.id)
388 if [ ! -z "${_jail_id}" ]; then
389 init_variables $_jail
390 if [ -n "${_exec_stop}" ]; then
391 eval env -i /usr/sbin/jexec ${_jail_id} ${_exec_stop} \
392 >> ${_consolelog} 2>&1
394 killall -j ${_jail_id} -TERM > /dev/null 2>&1
396 killall -j ${_jail_id} -KILL > /dev/null 2>&1
398 echo -n " $_hostname"
400 if [ -n "${_interface}" ]; then
401 ifconfig ${_interface} -alias ${_ip}
403 rm /var/run/jail_${_jail}.id
405 echo " cannot stop jail ${_jail}. No jail id in /var/run"
413 if [ $# -gt 0 ]; then
419 run_rc_command "${cmd}"