7 # REQUIRE: LOGIN FILESYSTEMS
9 # KEYWORD: nojail shutdown
16 start_cmd="jail_start"
17 start_postcmd="jail_warn"
19 config_cmd="jail_config"
20 console_cmd="jail_console"
21 status_cmd="jail_status"
22 extra_commands="config console status"
23 : ${jail_conf:=/etc/jail.conf}
24 : ${jail_program:=/usr/sbin/jail}
25 : ${jail_consolecmd:=/usr/bin/login -f root}
26 : ${jail_jexec:=/usr/sbin/jexec}
27 : ${jail_jls:=/usr/sbin/jls}
31 # extact_var jail name param num defval
32 # Extract value from ${jail_$jail_$name} or ${jail_$name} and
33 # set it to $param. If not defined, $defval is used.
34 # When $num is [0-9]*, ${jail_$jail_$name$num} are looked up and
35 # $param is set by using +=.
36 # When $num is YN or NY, the value is interpret as boolean.
39 local i _j _name _param _num _def _name1 _name2
48 _name1=jail_${_j}_${_name}
50 eval $_name1=\"\${$_name1:-\${$_name2:-$_def}}\"
51 if checkyesno $_name1; then
58 _name1=jail_${_j}_${_name}
60 eval $_name1=\"\${$_name1:-\${$_name2:-$_def}}\"
61 if checkyesno $_name1; then
70 _name1=jail_${_j}_${_name}${i}
71 _name2=jail_${_name}${i}
72 eval _tmpargs=\"\${$_name1:-\${$_name2:-$_def}}\"
73 if [ -n "$_tmpargs" ]; then
74 echo " $_param += \"$_tmpargs\";"
82 _name1=jail_${_j}_${_name}
84 eval _tmpargs=\"\${$_name1:-\${$_name2:-$_def}}\"
85 if [ -n "$_tmpargs" ]; then
86 echo " $_param = \"$_tmpargs\";"
93 # Parse options and create a temporary configuration file if necessary.
101 if [ -z "$_j" ]; then
102 warn "parse_options: you must specify a jail"
105 eval _jconf=\"\${jail_${_j}_conf:-/etc/jail.${_j}.conf}\"
106 eval _rootdir=\"\$jail_${_j}_rootdir\"
107 eval _hostname=\"\$jail_${_j}_hostname\"
108 if [ -z "$_rootdir" -o \
109 -z "$_hostname" ]; then
110 if [ -r "$_jconf" ]; then
113 elif [ -r "$jail_conf" ]; then
117 warn "Invalid configuration for $_j " \
118 "(no jail.conf, no hostname, or no path). " \
119 "Jail $_j was ignored."
123 eval _ip=\"\$jail_${_j}_ip\"
124 if [ -z "$_ip" ] && ! check_kern_features vimage; then
125 warn "no ipaddress specified and no vimage support. " \
126 "Jail $_j was ignored."
129 _conf=/var/run/jail.${_j}.conf
131 # To relieve confusion, show a warning message.
134 if [ -r "$jail_conf" -o -r "$_jconf" ]; then
135 if ! checkyesno jail_parallel_start; then
136 warn "$_conf is created and used for jail $_j."
139 /usr/bin/install -m 0644 -o root -g wheel /dev/null $_conf || return 1
141 eval : \${jail_${_j}_flags:=${jail_flags}}
142 eval _exec=\"\$jail_${_j}_exec\"
143 eval _exec_start=\"\$jail_${_j}_exec_start\"
144 eval _exec_stop=\"\$jail_${_j}_exec_stop\"
145 if [ -n "${_exec}" ]; then
146 # simple/backward-compatible execution
147 _exec_start="${_exec}"
151 if [ -z "${_exec_start}" ]; then
152 _exec_start="/bin/sh /etc/rc"
153 if [ -z "${_exec_stop}" ]; then
154 _exec_stop="/bin/sh /etc/rc.shutdown"
158 eval _interface=\"\${jail_${_j}_interface:-${jail_interface}}\"
159 eval _parameters=\"\${jail_${_j}_parameters:-${jail_parameters}}\"
160 eval _fstab=\"\${jail_${_j}_fstab:-${jail_fstab:-/etc/fstab.$_j}}\"
162 date +"# Generated by rc.d/jail at %Y-%m-%d %H:%M:%S"
164 extract_var $_j hostname host.hostname - ""
165 extract_var $_j rootdir path - ""
166 if [ -n "$_ip" ]; then
167 extract_var $_j interface interface - ""
168 jail_handle_ips_option $_ip $_interface
171 eval _x=\"\$jail_${_j}_ip_multi${alias}\"
172 [ -z "$_x" ] && break
174 jail_handle_ips_option $_x $_interface
175 alias=$(($alias + 1))
177 case $need_dad_wait in
179 # Sleep to let DAD complete before
181 echo " exec.start += \"sleep " \
182 $(($(${SYSCTL_N} net.inet6.ip6.dad_count) + 1)) \
186 # These are applicable only to non-vimage jails.
187 extract_var $_j fib exec.fib - ""
188 extract_var $_j socket_unixiproute_only \
189 allow.raw_sockets NY YES
192 extract_var $_j vnet_interface vnet.interface - ""
196 echo " exec.system_user = \"root\";"
197 echo " exec.jail_user = \"root\";"
198 extract_var $_j exec_prestart exec.prestart 0 ""
199 extract_var $_j exec_poststart exec.poststart 0 ""
200 extract_var $_j exec_prestop exec.prestop 0 ""
201 extract_var $_j exec_poststop exec.poststop 0 ""
203 echo " exec.start += \"$_exec_start\";"
204 extract_var $_j exec_afterstart exec.start 1 ""
205 echo " exec.stop = \"$_exec_stop\";"
207 extract_var $_j consolelog exec.consolelog - \
208 /var/log/jail_${_j}_console.log
210 if [ -r $_fstab ]; then
211 echo " mount.fstab = \"$_fstab\";"
214 eval : \${jail_${_j}_devfs_enable:=${jail_devfs_enable:-NO}}
215 if checkyesno jail_${_j}_devfs_enable; then
217 eval _ruleset=\${jail_${_j}_devfs_ruleset:-${jail_devfs_ruleset}}
220 [0-9]*) echo " devfs_ruleset = \"$_ruleset\";" ;;
222 # XXX: This is the default value,
223 # Let jail(8) to use the default because
224 # mount(8) only accepts an integer.
225 # This should accept a ruleset name.
227 *) warn "devfs_ruleset must be an integer." ;;
230 eval : \${jail_${_j}_fdescfs_enable:=${jail_fdescfs_enable:-NO}}
231 if checkyesno jail_${_j}_fdescfs_enable; then
232 echo " mount.fdescfs;"
234 eval : \${jail_${_j}_procfs_enable:=${jail_procfs_enable:-NO}}
235 if checkyesno jail_${_j}_procfs_enable; then
237 "\"procfs ${_rootdir%/}/proc procfs rw 0 0\";"
240 eval : \${jail_${_j}_mount_enable:=${jail_mount_enable:-NO}}
241 if checkyesno jail_${_j}_mount_enable; then
242 echo " allow.mount;" >> $_conf
245 extract_var $_j set_hostname_allow allow.set_hostname YN NO
246 extract_var $_j sysvipc_allow allow.sysvipc YN NO
247 for _p in $_parameters; do
256 # jail_extract_address argument iface
257 # The second argument is the string from one of the _ip
258 # or the _multi variables. In case of a comma separated list
259 # only one argument must be passed in at a time.
260 # The function alters the _type, _iface, _addr and _mask variables.
262 jail_extract_address()
268 if [ -z "${_i}" ]; then
269 warn "jail_extract_address: called without input"
273 # Check if we have an interface prefix given and split into
276 *\|*) # ifN|.. prefix there
285 # In case the IP has no interface given, check if we have a global one.
286 _iface=${_iface:-${_interface}}
288 # Set address, cut off any prefix/netmask/prefixlen.
290 _addr=${_addr%%[/ ]*}
292 # Theoretically we can return here if interface is not set,
293 # as we only care about the _mask if we call ifconfig.
294 # This is not done because we may want to santize IP addresses
295 # based on _type later, and optionally change the type as well.
297 # Extract the prefix/netmask/prefixlen part by cutting off the address.
299 _mask=`expr "${_mask}" : "${_addr}\(.*\)"`
301 # Identify type {inet,inet6}.
303 *\.*\.*\.*) _type="inet" ;;
304 *:*) _type="inet6" ;;
305 *) warn "jail_extract_address: type not identified"
309 # Handle the special /netmask instead of /prefix or
310 # "netmask xxx" case for legacy IP.
311 # We do NOT support shortend class-full netmasks.
312 if [ "${_type}" = "inet" ]; then
314 /*\.*\.*\.*) _mask=" netmask ${_mask#/}" ;;
318 # In case _mask is still not set use /32.
321 elif [ "${_type}" = "inet6" ]; then
322 # In case _mask is not set for IPv6, use /128.
327 # jail_handle_ips_option input iface
328 # Handle a single argument imput which can be a comma separated
329 # list of addresses (theoretically with an option interface and
330 # prefix/netmask/prefixlen).
332 jail_handle_ips_option()
334 local _x _type _i _defif
338 if [ -z "${_x}" ]; then
339 # No IP given. This can happen for the primary address
340 # of each address family.
344 # Loop, in case we find a comma separated list, we need to handle
345 # each argument on its own.
346 while [ ${#_x} -gt 0 ]; do
348 *,*) # Extract the first argument and strip it off the list.
349 _i=`expr "${_x}" : '^\([^,]*\)'`
350 _x=`expr "${_x}" : "^[^,]*,\(.*\)"`
361 jail_extract_address $_i $_defif
363 # make sure we got an address.
369 # Append address to list of addresses for the jail command.
372 echo " ip4.addr += \"${_iface:+${_iface}|}${_addr}${_mask}\";"
375 echo " ip6.addr += \"${_iface:+${_iface}|}${_addr}${_mask}\";"
390 _j=$(echo $_j | tr /. _)
391 if parse_options $_j; then
392 echo "$_j: parameters are in $_conf."
401 # One argument that is not _ALL.
403 0:*|1:_ALL) err 3 "Specify a jail name." ;;
406 _j=$(echo $1 | tr /. _)
409 0) eval _cmd=\${jail_${_j}_consolecmd:-$jail_consolecmd} ;;
412 $jail_jexec $_j $_cmd
428 echo -n 'Starting jails:'
431 command=$jail_program
433 command_args="-f $jail_conf -c"
434 _tmp=`mktemp -t jail` || exit 3
435 if $command $rc_flags $command_args >> $_tmp 2>&1; then
436 $jail_jls jid name | while read IN; do
439 echo $1 > /var/run/jail_$2.id
449 if checkyesno jail_parallel_start; then
451 # Start jails in parallel and then check jail id when
452 # jail_parallel_start is YES.
456 _j=$(echo $_j | tr /. _)
457 parse_options $_j || continue
460 eval rc_flags=\${jail_${_j}_flags:-$jail_flags}
461 eval command=\${jail_${_j}_program:-$jail_program}
462 command_args="-i -f $_conf -c $_j"
463 $command $rc_flags $command_args \
464 >/dev/null 2>&1 </dev/null &
468 echo -n " ${_hostname:-${_j}}"
469 if _jid=$($jail_jls -j $_j jid); then
470 echo "$_jid" > /var/run/jail_${_j}.id
472 rm -f /var/run/jail_${_j}.id
473 echo " cannot start jail " \
474 "\"${_hostname:-${_j}}\": "
479 # Start jails one-by-one when jail_parallel_start is NO.
482 _j=$(echo $_j | tr /. _)
483 parse_options $_j || continue
485 eval rc_flags=\${jail_${_j}_flags:-$jail_flags}
486 eval command=\${jail_${_j}_program:-$jail_program}
487 command_args="-i -f $_conf -c $_j"
488 _tmp=`mktemp -t jail` || exit 3
489 if $command $rc_flags $command_args \
490 >> $_tmp 2>&1 </dev/null; then
491 echo -n " ${_hostname:-${_j}}"
492 _jid=$($jail_jls -j $_j jid)
493 echo $_jid > /var/run/jail_${_j}.id
495 rm -f /var/run/jail_${_j}.id
496 echo " cannot start jail " \
497 "\"${_hostname:-${_j}}\": "
513 echo -n 'Stopping jails:'
516 command=$jail_program
518 command_args="-f $jail_conf -r"
519 $jail_jls name | while read _j; do
521 _tmp=`mktemp -t jail` || exit 3
522 $command $rc_flags $command_args $_j >> $_tmp 2>&1
523 if $jail_jls -j $_j > /dev/null 2>&1; then
526 rm -f /var/run/jail_${_j}.id
535 _j=$(echo $_j | tr /. _)
536 parse_options $_j || continue
537 if ! $jail_jls -j $_j > /dev/null 2>&1; then
540 eval command=\${jail_${_j}_program:-$jail_program}
541 echo -n " ${_hostname:-${_j}}"
542 _tmp=`mktemp -t jail` || exit 3
543 $command -q -f $_conf -r $_j >> $_tmp 2>&1
544 if $jail_jls -j $_j > /dev/null 2>&1; then
547 rm -f /var/run/jail_${_j}.id
557 # To relieve confusion, show a warning message.
559 1) warn "Per-jail configuration via jail_* variables " \
560 "is obsolete. Please consider to migrate to $jail_conf."
567 1) run_rc_command $@ ${jail_list:-_ALL} ;;
568 *) run_rc_command $@ ;;