7 # REQUIRE: LOGIN cleanvar
9 # KEYWORD: nojail shutdown
11 # WARNING: This script deals with untrusted data (the data and
12 # processes inside the jails) and care must be taken when changing the
13 # code related to this! If you have any doubt whether a change is
14 # correct and have security impact, please get the patch reviewed by
15 # the FreeBSD Security Team prior to commit.
21 start_cmd="jail_start"
25 # Initialize the various jail variables for jail _j.
32 warn "init_variables: you must specify a jail"
36 eval _rootdir=\"\$jail_${_j}_rootdir\"
37 _devdir="${_rootdir}/dev"
38 _fdescdir="${_devdir}/fd"
39 _procdir="${_rootdir}/proc"
40 eval _hostname=\"\$jail_${_j}_hostname\"
41 eval _ip=\"\$jail_${_j}_ip\"
42 eval _interface=\"\${jail_${_j}_interface:-${jail_interface}}\"
43 eval _exec=\"\$jail_${_j}_exec\"
47 eval _exec_prestart${i}=\"\${jail_${_j}_exec_prestart${i}:-\${jail_exec_prestart${i}}}\"
48 [ -z "$(eval echo \"\$_exec_prestart${i}\")" ] && break
52 eval _exec_start=\"\${jail_${_j}_exec_start:-${jail_exec_start}}\"
56 eval _exec_afterstart${i}=\"\${jail_${_j}_exec_afterstart${i}:-\${jail_exec_afterstart${i}}}\"
57 [ -z "$(eval echo \"\$_exec_afterstart${i}\")" ] && break
63 eval _exec_poststart${i}=\"\${jail_${_j}_exec_poststart${i}:-\${jail_exec_poststart${i}}}\"
64 [ -z "$(eval echo \"\$_exec_poststart${i}\")" ] && break
70 eval _exec_prestop${i}=\"\${jail_${_j}_exec_prestop${i}:-\${jail_exec_prestop${i}}}\"
71 [ -z "$(eval echo \"\$_exec_prestop${i}\")" ] && break
75 eval _exec_stop=\"\${jail_${_j}_exec_stop:-${jail_exec_stop}}\"
79 eval _exec_poststop${i}=\"\${jail_${_j}_exec_poststop${i}:-\${jail_exec_poststop${i}}}\"
80 [ -z "$(eval echo \"\$_exec_poststop${i}\")" ] && break
84 if [ -n "${_exec}" ]; then
85 # simple/backward-compatible execution
86 _exec_start="${_exec}"
90 if [ -z "${_exec_start}" ]; then
91 _exec_start="/bin/sh /etc/rc"
92 if [ -z "${_exec_stop}" ]; then
93 _exec_stop="/bin/sh /etc/rc.shutdown"
98 # The default jail ruleset will be used by rc.subr if none is specified.
99 eval _ruleset=\"\${jail_${_j}_devfs_ruleset:-${jail_devfs_ruleset}}\"
100 eval _devfs=\"\${jail_${_j}_devfs_enable:-${jail_devfs_enable}}\"
101 [ -z "${_devfs}" ] && _devfs="NO"
102 eval _fdescfs=\"\${jail_${_j}_fdescfs_enable:-${jail_fdescfs_enable}}\"
103 [ -z "${_fdescfs}" ] && _fdescfs="NO"
104 eval _procfs=\"\${jail_${_j}_procfs_enable:-${jail_procfs_enable}}\"
105 [ -z "${_procfs}" ] && _procfs="NO"
107 eval _mount=\"\${jail_${_j}_mount_enable:-${jail_mount_enable}}\"
108 [ -z "${_mount}" ] && _mount="NO"
109 # "/etc/fstab.${_j}" will be used for {,u}mount(8) if none is specified.
110 eval _fstab=\"\${jail_${_j}_fstab:-${jail_fstab}}\"
111 [ -z "${_fstab}" ] && _fstab="/etc/fstab.${_j}"
112 eval _flags=\"\${jail_${_j}_flags:-${jail_flags}}\"
113 [ -z "${_flags}" ] && _flags="-l -U root"
114 eval _consolelog=\"\${jail_${_j}_consolelog:-${jail_consolelog}}\"
115 [ -z "${_consolelog}" ] && _consolelog="/var/log/jail_${_j}_console.log"
116 eval _fib=\"\${jail_${_j}_fib:-${jail_fib}}\"
120 debug "$_j devfs enable: $_devfs"
121 debug "$_j fdescfs enable: $_fdescfs"
122 debug "$_j procfs enable: $_procfs"
123 debug "$_j mount enable: $_mount"
124 debug "$_j hostname: $_hostname"
126 jail_show_addresses ${_j}
127 debug "$_j interface: $_interface"
128 debug "$_j fib: $_fib"
129 debug "$_j root: $_rootdir"
130 debug "$_j devdir: $_devdir"
131 debug "$_j fdescdir: $_fdescdir"
132 debug "$_j procdir: $_procdir"
133 debug "$_j ruleset: $_ruleset"
134 debug "$_j fstab: $_fstab"
138 eval out=\"\${_exec_prestart${i}:-''}\"
139 if [ -z "$out" ]; then
142 debug "$_j exec pre-start #${i}: ${out}"
146 debug "$_j exec start: $_exec_start"
150 eval out=\"\${_exec_afterstart${i}:-''}\"
152 if [ -z "$out" ]; then
156 debug "$_j exec after start #${i}: ${out}"
162 eval out=\"\${_exec_poststart${i}:-''}\"
163 if [ -z "$out" ]; then
166 debug "$_j exec post-start #${i}: ${out}"
172 eval out=\"\${_exec_prestop${i}:-''}\"
173 if [ -z "$out" ]; then
176 debug "$_j exec pre-stop #${i}: ${out}"
180 debug "$_j exec stop: $_exec_stop"
184 eval out=\"\${_exec_poststop${i}:-''}\"
185 if [ -z "$out" ]; then
188 debug "$_j exec post-stop #${i}: ${out}"
192 debug "$_j flags: $_flags"
193 debug "$_j consolelog: $_consolelog"
195 if [ -z "${_hostname}" ]; then
196 err 3 "$name: No hostname has been defined for ${_j}"
198 if [ -z "${_rootdir}" ]; then
199 err 3 "$name: No root directory has been defined for ${_j}"
203 # set_sysctl rc_knob mib msg
204 # If the mib sysctl is set according to what rc_knob
205 # specifies, this function does nothing. However if
206 # rc_knob is set differently than mib, then the mib
207 # is set accordingly and msg is displayed followed by
208 # an '=" sign and the word 'YES' or 'NO'.
216 _current=`${SYSCTL} -n $_mib 2>/dev/null`
217 if checkyesno $_knob ; then
218 if [ "$_current" -ne 1 ]; then
219 echo -n " ${_msg}=YES"
220 ${SYSCTL_W} 1>/dev/null ${_mib}=1
223 if [ "$_current" -ne 0 ]; then
224 echo -n " ${_msg}=NO"
225 ${SYSCTL_W} 1>/dev/null ${_mib}=0
230 # is_current_mountpoint()
231 # Is the directory mount point for a currently mounted file
234 is_current_mountpoint()
240 _dir=`echo $_dir | sed -Ee 's#//+#/#g' -e 's#/$##'`
241 [ ! -d "${_dir}" ] && return 1
242 _dir2=`df ${_dir} | tail +2 | awk '{ print $6 }'`
243 [ "${_dir}" = "${_dir2}" ]
247 # is_symlinked_mountpoint()
248 # Is a mount point, or any of its parent directories, a symlink?
250 is_symlinked_mountpoint()
256 [ -L "$_dir" ] && return 0
257 [ "$_dir" = "/" ] && return 1
258 is_symlinked_mountpoint `dirname $_dir`
263 # Try to unmount a mount point without being vulnerable to
272 if is_current_mountpoint ${_dir}; then
273 umount -f ${_dir} >/dev/null 2>&1
275 debug "Nothing mounted on ${_dir} - not unmounting"
281 # This function unmounts certain special filesystems in the
282 # currently selected jail. The caller must call the init_variables()
283 # routine before calling this one.
287 local _device _mountpt _rest
289 if checkyesno _fdescfs; then
290 if [ -d "${_fdescdir}" ] ; then
291 secure_umount ${_fdescdir}
294 if checkyesno _devfs; then
295 if [ -d "${_devdir}" ] ; then
296 secure_umount ${_devdir}
299 if checkyesno _procfs; then
300 if [ -d "${_procdir}" ] ; then
301 secure_umount ${_procdir}
304 if checkyesno _mount; then
305 [ -f "${_fstab}" ] || warn "${_fstab} does not exist"
306 tail -r ${_fstab} | while read _device _mountpt _rest; do
307 case ":${_device}" in
312 secure_umount ${_mountpt}
318 # Mount file systems from a per jail fstab while trying to
319 # secure against symlink attacks at the mount points.
321 # If we are certain we cannot secure against symlink attacks we
322 # do not mount all of the file systems (since we cannot just not
323 # mount the file system with the problematic mount point).
325 # The caller must call the init_variables() routine before
330 local _device _mountpt _rest
332 while read _device _mountpt _rest; do
333 case ":${_device}" in
338 if is_symlinked_mountpoint ${_mountpt}; then
339 warn "${_mountpt} has symlink as parent - not mounting from ${_fstab}"
343 mount -a -F "${_fstab}"
346 # jail_show_addresses jail
347 # Debug print the input for the given _multi aliases
348 # for a jail for init_variables().
350 jail_show_addresses()
356 if [ -z "${_j}" ]; then
357 warn "jail_show_addresses: you must specify a jail"
362 eval _addr=\"\$jail_${_j}_ip_multi${alias}\"
363 if [ -n "${_addr}" ]; then
364 debug "${_j} ip_multi${alias}: $_addr"
365 alias=$((${alias} + 1))
372 # jail_extract_address argument
373 # The second argument is the string from one of the _ip
374 # or the _multi variables. In case of a comma separated list
375 # only one argument must be passed in at a time.
376 # The function alters the _type, _iface, _addr and _mask variables.
378 jail_extract_address()
383 if [ -z "${_i}" ]; then
384 warn "jail_extract_address: called without input"
388 # Check if we have an interface prefix given and split into
391 *\|*) # ifN|.. prefix there
400 # In case the IP has no interface given, check if we have a global one.
401 _iface=${_iface:-${_interface}}
403 # Set address, cut off any prefix/netmask/prefixlen.
405 _addr=${_addr%%[/ ]*}
407 # Theoretically we can return here if interface is not set,
408 # as we only care about the _mask if we call ifconfig.
409 # This is not done because we may want to santize IP addresses
410 # based on _type later, and optionally change the type as well.
412 # Extract the prefix/netmask/prefixlen part by cutting off the address.
414 _mask=`expr "${_mask}" : "${_addr}\(.*\)"`
416 # Identify type {inet,inet6}.
418 *\.*\.*\.*) _type="inet" ;;
419 *:*) _type="inet6" ;;
420 *) warn "jail_extract_address: type not identified"
424 # Handle the special /netmask instead of /prefix or
425 # "netmask xxx" case for legacy IP.
426 # We do NOT support shortend class-full netmasks.
427 if [ "${_type}" = "inet" ]; then
429 /*\.*\.*\.*) _mask=" netmask ${_mask#/}" ;;
433 # In case _mask is still not set use /32.
436 elif [ "${_type}" = "inet6" ]; then
437 # In case _maske is not set for IPv6, use /128.
442 # jail_handle_ips_option {add,del} input
443 # Handle a single argument imput which can be a comma separated
444 # list of addresses (theoretically with an option interface and
445 # prefix/netmask/prefixlen).
447 jail_handle_ips_option()
449 local _x _action _type _i
453 if [ -z "${_x}" ]; then
454 # No IP given. This can happen for the primary address
455 # of each address family.
459 # Loop, in case we find a comma separated list, we need to handle
460 # each argument on its own.
461 while [ ${#_x} -gt 0 ]; do
463 *,*) # Extract the first argument and strip it off the list.
464 _i=`expr "${_x}" : '^\([^,]*\)'`
465 _x=`expr "${_x}" : "^[^,]*,\(.*\)"`
476 jail_extract_address "${_i}"
478 # make sure we got an address.
484 # Append address to list of addresses for the jail command.
486 "") _addrl="${_addr}" ;;
487 *) _addrl="${_addrl},${_addr}" ;;
490 # Configure interface alias if requested by a given interface
491 # and if we could correctly parse everything.
498 *) warn "Could not determine address family. Not going" \
499 "to ${_action} address '${_addr}' for ${_jail}."
504 add) ifconfig ${_iface} ${_type} ${_addr}${_mask} alias
506 del) # When removing the IP, ignore the _mask.
507 ifconfig ${_iface} ${_type} ${_addr} -alias
514 # Extract the comma separated list of addresses and return them
515 # for the jail command.
516 # Handle more than one address via the _multi option as well.
517 # If an interface is given also add/remove an alias for the
518 # address with an optional netmask.
528 *) warn "jail_ips: invalid action '${_action}'"
534 jail_handle_ips_option ${_action} "${_ip}"
535 # Handle jail_xxx_ip_multi<N>
538 eval _x=\"\$jail_${_jail}_ip_multi${alias}\"
541 *) jail_handle_ips_option ${_action} "${_x}"
542 alias=$((${alias} + 1))
550 echo -n 'Configuring jails:'
551 set_sysctl jail_set_hostname_allow security.jail.set_hostname_allowed \
553 set_sysctl jail_socket_unixiproute_only \
554 security.jail.socket_unixiproute_only unixiproute_only
555 set_sysctl jail_sysvipc_allow security.jail.sysvipc_allowed \
559 echo -n 'Starting jails:'
560 _tmp_dir=`mktemp -d /tmp/jail.XXXXXXXX` || \
561 err 3 "$name: Can't create temp dir, exiting..."
562 for _jail in ${jail_list}
564 init_variables $_jail
565 if [ -f /var/run/jail_${_jail}.id ]; then
566 echo -n " [${_hostname} already running (/var/run/jail_${_jail}.id exists)]"
571 if [ -n "${_fib}" ]; then
572 _setfib="setfib -F '${_fib}'"
576 if checkyesno _mount; then
577 info "Mounting fstab for jail ${_jail} (${_fstab})"
578 if [ ! -f "${_fstab}" ]; then
579 err 3 "$name: ${_fstab} does not exist"
583 if checkyesno _devfs; then
584 # If devfs is already mounted here, skip it.
585 df -t devfs "${_devdir}" >/dev/null
586 if [ $? -ne 0 ]; then
587 if is_symlinked_mountpoint ${_devdir}; then
588 warn "${_devdir} has symlink as parent - not starting jail ${_jail}"
591 info "Mounting devfs on ${_devdir}"
592 devfs_mount_jail "${_devdir}" ${_ruleset}
593 # Transitional symlink for old binaries
594 if [ ! -L "${_devdir}/log" ]; then
597 ln -sf ../var/run/log log
602 # XXX - It seems symlinks don't work when there
603 # is a devfs(5) device of the same name.
604 # Jail console output
607 # ln -sf ../var/log/console console
610 if checkyesno _fdescfs; then
611 if is_symlinked_mountpoint ${_fdescdir}; then
612 warn "${_fdescdir} has symlink as parent, not mounting"
614 info "Mounting fdescfs on ${_fdescdir}"
615 mount -t fdescfs fdesc "${_fdescdir}"
618 if checkyesno _procfs; then
619 if is_symlinked_mountpoint ${_procdir}; then
620 warn "${_procdir} has symlink as parent, not mounting"
622 info "Mounting procfs onto ${_procdir}"
623 if [ -d "${_procdir}" ] ; then
624 mount -t procfs proc "${_procdir}"
628 _tmp_jail=${_tmp_dir}/jail.$$
632 eval out=\"\${_exec_prestart${i}:-''}\"
633 [ -z "$out" ] && break
638 eval ${_setfib} jail ${_flags} -i ${_rootdir} ${_hostname} \
639 \"${_addrl}\" ${_exec_start} > ${_tmp_jail} 2>&1
641 if [ "$?" -eq 0 ] ; then
642 _jail_id=$(head -1 ${_tmp_jail})
645 eval out=\"\${_exec_afterstart${i}:-''}\"
647 if [ -z "$out" ]; then
651 jexec "${_jail_id}" ${out}
655 echo -n " $_hostname"
656 tail +2 ${_tmp_jail} >${_consolelog}
657 echo ${_jail_id} > /var/run/jail_${_jail}.id
661 eval out=\"\${_exec_poststart${i}:-''}\"
662 [ -z "$out" ] && break
669 echo " cannot start jail \"${_jail}\": "
680 echo -n 'Stopping jails:'
681 for _jail in ${jail_list}
683 if [ -f "/var/run/jail_${_jail}.id" ]; then
684 _jail_id=$(cat /var/run/jail_${_jail}.id)
685 if [ ! -z "${_jail_id}" ]; then
686 init_variables $_jail
690 eval out=\"\${_exec_prestop${i}:-''}\"
691 [ -z "$out" ] && break
696 if [ -n "${_exec_stop}" ]; then
697 eval env -i /usr/sbin/jexec ${_jail_id} ${_exec_stop} \
698 >> ${_consolelog} 2>&1
700 killall -j ${_jail_id} -TERM > /dev/null 2>&1
702 killall -j ${_jail_id} -KILL > /dev/null 2>&1
704 echo -n " $_hostname"
708 eval out=\"\${_exec_poststop${i}:-''}\"
709 [ -z "$out" ] && break
715 rm /var/run/jail_${_jail}.id
717 echo " cannot stop jail ${_jail}. No jail id in /var/run"
725 if [ $# -gt 0 ]; then
731 run_rc_command "${cmd}"