xref: /freebsd/libexec/rc/rc.d/jail (revision 1da7f3f6f72b2245e458fc7195733268ae4a1136)
1#!/bin/sh
2#
3#
4
5# PROVIDE: jail
6# REQUIRE: LOGIN FILESYSTEMS
7# BEFORE: securelevel
8# KEYWORD: shutdown
9
10. /etc/rc.subr
11
12name="jail"
13desc="Manage system jails"
14rcvar="jail_enable"
15
16start_cmd="jail_start"
17start_postcmd="jail_warn"
18stop_cmd="jail_stop"
19config_cmd="jail_config"
20console_cmd="jail_console"
21status_cmd="jail_status"
22extra_commands="config console status"
23: ${jail_program:=/usr/sbin/jail}
24: ${jail_consolecmd:=/usr/bin/login -f root}
25: ${jail_jexec:=/usr/sbin/jexec}
26: ${jail_jls:=/usr/sbin/jls}
27
28need_dad_wait=
29
30# extract_var jv name param num defval
31#	Extract value from ${jail_$jv_$name} or ${jail_$name} and
32#	set it to $param.  If not defined, $defval is used.
33#	When $num is [0-9]*, ${jail_$jv_$name$num} are looked up and
34#	$param is set by using +=.  $num=0 is optional (params may start at 1).
35#	When $num is YN or NY, the value is interpreted as boolean.
36#	When $num is @, the value is interpreted as an array separted by IFS.
37extract_var()
38{
39	local i _jv _name _param _num _def _name1 _name2
40	_jv=$1
41	_name=$2
42	_param=$3
43	_num=$4
44	_def=$5
45
46	case $_num in
47	YN)
48		_name1=jail_${_jv}_${_name}
49		_name2=jail_${_name}
50		eval $_name1=\"\${$_name1:-\${$_name2:-$_def}}\"
51		if checkyesno $_name1; then
52			echo "	$_param = 1;"
53		else
54			echo "	$_param = 0;"
55		fi
56	;;
57	NY)
58		_name1=jail_${_jv}_${_name}
59		_name2=jail_${_name}
60		eval $_name1=\"\${$_name1:-\${$_name2:-$_def}}\"
61		if checkyesno $_name1; then
62			echo "	$_param = 0;"
63		else
64			echo "	$_param = 1;"
65		fi
66	;;
67	[0-9]*)
68		i=$_num
69		while : ; do
70			_name1=jail_${_jv}_${_name}${i}
71			_name2=jail_${_name}${i}
72			eval _tmpargs=\"\${$_name1:-\${$_name2:-$_def}}\"
73			if [ -n "$_tmpargs" ]; then
74				echo "	$_param += \"$_tmpargs\";"
75			elif [ $i != 0 ]; then
76				break;
77			fi
78			i=$(($i + 1))
79		done
80	;;
81	@)
82		_name1=jail_${_jv}_${_name}
83		_name2=jail_${_name}
84		eval _tmpargs=\"\${$_name1:-\${$_name2:-$_def}}\"
85		set -- $_tmpargs
86		if [ $# -gt 0 ]; then
87			echo -n "	$_param = "
88			while [ $# -gt 1 ]; do
89				echo -n "\"$1\", "
90				shift
91			done
92			echo "\"$1\";"
93		fi
94	;;
95	*)
96		_name1=jail_${_jv}_${_name}
97		_name2=jail_${_name}
98		eval _tmpargs=\"\${$_name1:-\${$_name2:-$_def}}\"
99		if [ -n "$_tmpargs" ]; then
100			echo "	$_param = \"$_tmpargs\";"
101		fi
102	;;
103	esac
104}
105
106# parse_options _j _jv
107#	Parse options and create a temporary configuration file if necessary.
108#
109parse_options()
110{
111	local _j _jv _p
112	_j=$1
113	_jv=$2
114
115	_confwarn=0
116	if [ -z "$_j" ]; then
117		warn "parse_options: you must specify a jail"
118		return
119	fi
120	eval _jconf=\"\${jail_${_jv}_conf:-/etc/jail.${_j}.conf}\"
121	eval _rootdir=\"\$jail_${_jv}_rootdir\"
122	eval _jconfdir=\"/etc/jail.conf.d/${_j}.conf\"
123	eval _hostname=\"\$jail_${_jv}_hostname\"
124	if [ -z "$_rootdir" -o \
125	     -z "$_hostname" ]; then
126		if [ -r "$_jconf" ]; then
127			_conf="$_jconf"
128			return 0
129		elif [ -r "$_jconfdir" ] && ! egrep -q \
130		    '^\s*\.include\s*["'\'']?/etc/jail.conf.d/' "$jail_conf" \
131		    2>/dev/null; then
132			_conf="$_jconfdir"
133			return 0
134		elif [ -r "$jail_conf" ]; then
135			_conf="$jail_conf"
136			return 0
137		else
138			warn "Invalid configuration for $_j " \
139			    "(no jail.conf, no hostname, or no path).  " \
140			    "Jail $_j was ignored."
141		fi
142		return 1
143	fi
144	eval _ip=\"\$jail_${_jv}_ip\"
145	if [ -z "$_ip" ] && ! check_kern_features vimage; then
146		warn "no ipaddress specified and no vimage support.  " \
147		    "Jail $_j was ignored."
148		return 1
149	fi
150	_conf=/var/run/jail.${_j}.conf
151	#
152	# To relieve confusion, show a warning message.
153	#
154	: ${jail_confwarn:=YES}
155	checkyesno jail_confwarn && _confwarn=1
156	if [ -r "$jail_conf" -o -r "$_jconf" ]; then
157		if ! checkyesno jail_parallel_start; then
158			warn "$_conf is created and used for jail $_j."
159		fi
160	fi
161	/usr/bin/install -m 0644 -o root -g wheel /dev/null $_conf || return 1
162
163	eval : \${jail_${_jv}_flags:=${jail_flags}}
164	eval _exec=\"\$jail_${_jv}_exec\"
165	eval _exec_start=\"\$jail_${_jv}_exec_start\"
166	eval _exec_stop=\"\$jail_${_jv}_exec_stop\"
167	if [ -n "${_exec}" ]; then
168		#   simple/backward-compatible execution
169		_exec_start="${_exec}"
170		_exec_stop=""
171	else
172		#   flexible execution
173		if [ -z "${_exec_start}" ]; then
174			_exec_start="/bin/sh /etc/rc"
175			if [ -z "${_exec_stop}" ]; then
176				_exec_stop="/bin/sh /etc/rc.shutdown jail"
177			fi
178		fi
179	fi
180	eval _interface=\"\${jail_${_jv}_interface:-${jail_interface}}\"
181	eval _parameters=\"\${jail_${_jv}_parameters:-${jail_parameters}}\"
182	eval _fstab=\"\${jail_${_jv}_fstab:-${jail_fstab:-/etc/fstab.$_j}}\"
183	(
184		date +"# Generated by rc.d/jail at %Y-%m-%d %H:%M:%S"
185		echo "$_j {"
186		extract_var $_jv hostname host.hostname - ""
187		extract_var $_jv rootdir path - ""
188		if [ -n "$_ip" ]; then
189			extract_var $_jv interface interface - ""
190			jail_handle_ips_option $_ip $_interface
191			alias=0
192			while : ; do
193				eval _x=\"\$jail_${_jv}_ip_multi${alias}\"
194				[ -z "$_x" ] && break
195
196				jail_handle_ips_option $_x $_interface
197				alias=$(($alias + 1))
198			done
199			case $need_dad_wait in
200			1)
201				# Sleep to let DAD complete before
202				# starting services.
203				echo "	exec.start += \"sleep " \
204				$(($(${SYSCTL_N} net.inet6.ip6.dad_count) + 1)) \
205				"\";"
206			;;
207			esac
208			# These are applicable only to non-vimage jails.
209			extract_var $_jv fib exec.fib - ""
210			extract_var $_jv socket_unixiproute_only \
211			    allow.raw_sockets NY YES
212		else
213			echo "	vnet;"
214			extract_var $_jv vnet_interface vnet.interface @ ""
215		fi
216
217		echo "	exec.clean;"
218		echo "	exec.system_user = \"root\";"
219		echo "	exec.jail_user = \"root\";"
220		extract_var $_jv exec_prestart exec.prestart 0 ""
221		extract_var $_jv exec_poststart exec.poststart 0 ""
222		extract_var $_jv exec_prestop exec.prestop 0 ""
223		extract_var $_jv exec_poststop exec.poststop 0 ""
224
225		echo "	exec.start += \"$_exec_start\";"
226		extract_var $_jv exec_afterstart exec.start 0 ""
227		echo "	exec.stop = \"$_exec_stop\";"
228
229		extract_var $_jv consolelog exec.consolelog - \
230		    /var/log/jail_${_j}_console.log
231
232		if [ -r $_fstab ]; then
233			echo "	mount.fstab = \"$_fstab\";"
234		fi
235
236		eval : \${jail_${_jv}_devfs_enable:=${jail_devfs_enable:-NO}}
237		if checkyesno jail_${_jv}_devfs_enable; then
238			echo "	mount.devfs;"
239			eval _ruleset=\${jail_${_jv}_devfs_ruleset:-${jail_devfs_ruleset}}
240			case $_ruleset in
241			"")	;;
242			[0-9]*) echo "	devfs_ruleset = \"$_ruleset\";" ;;
243			devfsrules_jail)
244				# XXX: This is the default value,
245				# Let jail(8) to use the default because
246				# mount(8) only accepts an integer.
247				# This should accept a ruleset name.
248			;;
249			*)	warn "devfs_ruleset must be an integer." ;;
250			esac
251		fi
252		eval : \${jail_${_jv}_fdescfs_enable:=${jail_fdescfs_enable:-NO}}
253		if checkyesno jail_${_jv}_fdescfs_enable; then
254			echo "	mount.fdescfs;"
255		fi
256		eval : \${jail_${_jv}_procfs_enable:=${jail_procfs_enable:-NO}}
257		if checkyesno jail_${_jv}_procfs_enable; then
258			echo "	mount.procfs;"
259		fi
260
261		eval : \${jail_${_jv}_mount_enable:=${jail_mount_enable:-NO}}
262		if checkyesno jail_${_jv}_mount_enable; then
263			echo "	allow.mount;"
264		fi
265
266		extract_var $_jv set_hostname_allow allow.set_hostname YN NO
267		extract_var $_jv sysvipc_allow allow.sysvipc YN NO
268		extract_var $_jv enforce_statfs enforce_statfs - 2
269		extract_var $_jv osreldate osreldate
270		extract_var $_jv osrelease osrelease
271		for _p in $_parameters; do
272			echo "	${_p%\;};"
273		done
274		echo "}"
275	) >> $_conf
276
277	return 0
278}
279
280# jail_extract_address argument iface
281#	The second argument is the string from one of the _ip
282#	or the _multi variables. In case of a comma separated list
283#	only one argument must be passed in at a time.
284#	The function alters the _type, _iface, _addr and _mask variables.
285#
286jail_extract_address()
287{
288	local _i _interface
289	_i=$1
290	_interface=$2
291
292	if [ -z "${_i}" ]; then
293		warn "jail_extract_address: called without input"
294		return
295	fi
296
297	# Check if we have an interface prefix given and split into
298	# iFace and rest.
299	case "${_i}" in
300	*\|*)	# ifN|.. prefix there
301		_iface=${_i%%|*}
302		_r=${_i##*|}
303		;;
304	*)	_iface=""
305		_r=${_i}
306		;;
307	esac
308
309	# In case the IP has no interface given, check if we have a global one.
310	_iface=${_iface:-${_interface}}
311
312	# Set address, cut off any prefix/netmask/prefixlen.
313	_addr=${_r}
314	_addr=${_addr%%[/ ]*}
315
316	# Theoretically we can return here if interface is not set,
317	# as we only care about the _mask if we call ifconfig.
318	# This is not done because we may want to santize IP addresses
319	# based on _type later, and optionally change the type as well.
320
321	# Extract the prefix/netmask/prefixlen part by cutting off the address.
322	_mask=${_r}
323	_mask=`expr -- "${_mask}" : "${_addr}\(.*\)"`
324
325	# Identify type {inet,inet6}.
326	case "${_addr}" in
327	*\.*\.*\.*)	_type="inet" ;;
328	*:*)		_type="inet6" ;;
329	*)		warn "jail_extract_address: type not identified"
330			;;
331	esac
332
333	# Handle the special /netmask instead of /prefix or
334	# "netmask xxx" case for legacy IP.
335	# We do NOT support shortend class-full netmasks.
336	if [ "${_type}" = "inet" ]; then
337		case "${_mask}" in
338		/*\.*\.*\.*)	_mask=" netmask ${_mask#/}" ;;
339		*)		;;
340		esac
341
342		# In case _mask is still not set use /32.
343		_mask=${_mask:-/32}
344
345	elif [ "${_type}" = "inet6" ]; then
346		# In case _mask is not set for IPv6, use /128.
347		_mask=${_mask:-/128}
348	fi
349}
350
351# jail_handle_ips_option input iface
352#	Handle a single argument imput which can be a comma separated
353#	list of addresses (theoretically with an option interface and
354#	prefix/netmask/prefixlen).
355#
356jail_handle_ips_option()
357{
358	local _x _type _i _defif
359	_x=$1
360	_defif=$2
361
362	if [ -z "${_x}" ]; then
363		# No IP given. This can happen for the primary address
364		# of each address family.
365		return
366	fi
367
368	# Loop, in case we find a comma separated list, we need to handle
369	# each argument on its own.
370	while [ ${#_x} -gt 0 ]; do
371		case "${_x}" in
372		*,*)	# Extract the first argument and strip it off the list.
373			_i=`expr -- "${_x}" : '^\([^,]*\)'`
374			_x=`expr -- "${_x}" : "^[^,]*,\(.*\)"`
375		;;
376		*)	_i=${_x}
377			_x=""
378		;;
379		esac
380
381		_type=""
382		_addr=""
383		_mask=""
384		_iface=""
385		jail_extract_address $_i $_defif
386
387		# make sure we got an address.
388		case $_addr in
389		"")	continue ;;
390		*)	;;
391		esac
392
393		# Append address to list of addresses for the jail command.
394		case $_type in
395		inet)
396			echo "	ip4.addr += \"${_iface:+${_iface}|}${_addr}${_mask}\";"
397		;;
398		inet6)
399			echo "	ip6.addr += \"${_iface:+${_iface}|}${_addr}${_mask}\";"
400			need_dad_wait=1
401		;;
402		esac
403	done
404}
405
406jail_config()
407{
408	local _j _jv
409
410	case $1 in
411	_ALL)	return ;;
412	esac
413	for _j in $@; do
414		_j=$(echo $_j | tr /. _)
415		_jv=$(echo -n $_j | tr -c '[:alnum:]' _)
416		if parse_options $_j $_jv; then
417			echo "$_j: parameters are in $_conf."
418		fi
419	done
420}
421
422jail_console()
423{
424	local _j _jv _cmd
425
426	# One argument that is not _ALL.
427	case $#:$1 in
428	0:*|1:_ALL)	err 3 "Specify a jail name." ;;
429	1:*)		;;
430	esac
431	_j=$(echo $1 | tr /. _)
432	_jv=$(echo -n $1 | tr -c '[:alnum:]' _)
433	shift
434	case $# in
435	0)	eval _cmd=\${jail_${_jv}_consolecmd:-$jail_consolecmd} ;;
436	*)	_cmd=$@ ;;
437	esac
438	$jail_jexec $_j $_cmd
439}
440
441jail_status()
442{
443
444	$jail_jls -N
445}
446
447jail_start()
448{
449	local _j _jv _jid _id _name
450
451	if [ $# = 0 ]; then
452		return
453	fi
454	startmsg -n 'Starting jails:'
455	case $1 in
456	_ALL)
457		command=$jail_program
458		rc_flags=$jail_flags
459		command_args="-f $jail_conf -c"
460		if ! checkyesno jail_parallel_start; then
461			command_args="$command_args -p1"
462		fi
463		_tmp=`mktemp -t jail` || exit 3
464		if $command $rc_flags $command_args >> $_tmp 2>&1; then
465			$jail_jls jid name | while read _id _name; do
466				startmsg -n " $_name"
467				echo $_id > /var/run/jail_${_name}.id
468			done
469		else
470			cat $_tmp
471		fi
472		rm -f $_tmp
473		startmsg '.'
474		return
475	;;
476	esac
477	if checkyesno jail_parallel_start; then
478		#
479		# Start jails in parallel and then check jail id when
480		# jail_parallel_start is YES.
481		#
482		for _j in $@; do
483			_j=$(echo $_j | tr /. _)
484			_jv=$(echo -n $_j | tr -c '[:alnum:]' _)
485			parse_options $_j $_jv || continue
486
487			eval rc_flags=\${jail_${_jv}_flags:-$jail_flags}
488			eval command=\${jail_${_jv}_program:-$jail_program}
489			command_args="-i -f $_conf -c $_j"
490			(
491				_tmp=`mktemp -t jail_${_j}` || exit 3
492				if $command $rc_flags $command_args \
493				    >> $_tmp 2>&1 </dev/null; then
494					startmsg -n " ${_hostname:-${_j}}"
495					_jid=$($jail_jls -j $_j jid)
496					echo $_jid > /var/run/jail_${_j}.id
497				else
498					startmsg " cannot start jail " \
499					    "\"${_hostname:-${_j}}\": "
500					cat $_tmp
501				fi
502				rm -f $_tmp
503			) &
504		done
505		wait
506	else
507		#
508		# Start jails one-by-one when jail_parallel_start is NO.
509		#
510		for _j in $@; do
511			_j=$(echo $_j | tr /. _)
512			_jv=$(echo -n $_j | tr -c '[:alnum:]' _)
513			parse_options $_j $_jv || continue
514
515			eval rc_flags=\${jail_${_jv}_flags:-$jail_flags}
516			eval command=\${jail_${_jv}_program:-$jail_program}
517			command_args="-i -f $_conf -c $_j"
518			_tmp=`mktemp -t jail` || exit 3
519			if $command $rc_flags $command_args \
520			    >> $_tmp 2>&1 </dev/null; then
521				startmsg -n " ${_hostname:-${_j}}"
522				_jid=$($jail_jls -j $_j jid)
523				echo $_jid > /var/run/jail_${_j}.id
524			else
525				startmsg " cannot start jail " \
526				    "\"${_hostname:-${_j}}\": "
527				cat $_tmp
528			fi
529			rm -f $_tmp
530		done
531	fi
532	startmsg '.'
533}
534
535jail_stop()
536{
537	local _j _jv
538
539	if [ $# = 0 ]; then
540		return
541	fi
542	echo -n 'Stopping jails:'
543	case $1 in
544	_ALL)
545		command=$jail_program
546		rc_flags=$jail_flags
547		command_args="-f $jail_conf -r"
548		if checkyesno jail_reverse_stop; then
549			$jail_jls name | tail -r
550		else
551			$jail_jls name
552		fi | while read _j; do
553			echo -n " $_j"
554			_tmp=`mktemp -t jail` || exit 3
555			$command $rc_flags $command_args $_j >> $_tmp 2>&1
556			if $jail_jls -j $_j > /dev/null 2>&1; then
557				cat $_tmp
558			else
559				rm -f /var/run/jail_${_j}.id
560			fi
561			rm -f $_tmp
562		done
563		echo '.'
564		return
565	;;
566	esac
567	checkyesno jail_reverse_stop && set -- $(reverse_list $@)
568	for _j in $@; do
569		_j=$(echo $_j | tr /. _)
570		_jv=$(echo -n $_j | tr -c '[:alnum:]' _)
571		parse_options $_j $_jv || continue
572		if ! $jail_jls -j $_j > /dev/null 2>&1; then
573			continue
574		fi
575		eval command=\${jail_${_jv}_program:-$jail_program}
576		echo -n " ${_hostname:-${_j}}"
577		_tmp=`mktemp -t jail` || exit 3
578		$command -q -f $_conf -r $_j >> $_tmp 2>&1
579		if $jail_jls -j $_j > /dev/null 2>&1; then
580			cat $_tmp
581		else
582			rm -f /var/run/jail_${_j}.id
583		fi
584		rm -f $_tmp
585	done
586	echo '.'
587}
588
589jail_warn()
590{
591
592	# To relieve confusion, show a warning message.
593	case $_confwarn in
594	1)	warn "Per-jail configuration via jail_* variables " \
595		    "is obsolete.  Please consider migrating to $jail_conf."
596	;;
597	esac
598}
599
600load_rc_config $name
601case $# in
6021)	run_rc_command $@ ${jail_list:-_ALL} ;;
603*)	jail_reverse_stop="no"
604	run_rc_command $@ ;;
605esac
606