summaryrefslogtreecommitdiff
path: root/usr.sbin/rcctl/rcctl.sh
blob: 08fceaeda65df83667d29462a4e0e246c03b1489 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#!/bin/sh
#
# $OpenBSD: rcctl.sh,v 1.88 2016/01/24 08:51:50 ajacoutot Exp $
#
# Copyright (c) 2014, 2015 Antoine Jacoutot <ajacoutot@openbsd.org>
# Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

_special_svcs="accounting check_quotas ipsec multicast pf spamd_black"
readonly _special_svcs

# get local functions from rc.subr(8)
FUNCS_ONLY=1
. /etc/rc.d/rc.subr
_rc_parse_conf

usage()
{
	_rc_err \
	"usage:	rcctl get|getdef|set service | daemon [variable [arguments]]
	rcctl [-df] action daemon ...
	rcctl disable|enable|order [daemon ...]
	rcctl ls lsarg"
}

needs_root()
{
	[ "$(id -u)" -ne 0 ] && _rc_err "${0##*/}: \"$*\" needs root privileges"
}

rcctl_err()
{
	_rc_err "${0##*/}: ${1}" ${2}
}

ls_rcscripts()
{
	local _s

	cd /etc/rc.d && set -- *
	for _s; do
		[ "${_s}" = "rc.subr" ] && continue
		[ ! -d "${_s}" ] && echo "${_s}"
	done
}

pkg_scripts_append()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	rcconf_edit_begin
	if [ -z "${pkg_scripts}" ]; then
		echo pkg_scripts="${_svc}" >>${_TMP_RCCONF}
	elif ! echo ${pkg_scripts} | grep -qw -- ${_svc}; then
		grep -v "^pkg_scripts.*=" /etc/rc.conf.local >${_TMP_RCCONF}
		echo pkg_scripts="${pkg_scripts} ${_svc}" >>${_TMP_RCCONF}
	fi
	rcconf_edit_end
}

pkg_scripts_order()
{
	local _svcs="$*"
	[ -n "${_svcs}" ] || return

	needs_root ${action}
	local _pkg_scripts _svc
	for _svc in ${_svcs}; do
		if svc_is_base ${_svc} || svc_is_special ${_svc}; then
			rcctl_err "${_svc} is not a pkg script"
		elif ! svc_get ${_svc} status; then
			rcctl_err "${_svc} is not enabled"
		fi
	done
	_pkg_scripts=$(echo "${_svcs} ${pkg_scripts}" | tr "[:blank:]" "\n" | \
		     awk -v ORS=' ' '!x[$0]++')
	rcconf_edit_begin
	grep -v "^pkg_scripts.*=" /etc/rc.conf.local >${_TMP_RCCONF}
	echo pkg_scripts=${_pkg_scripts} >>${_TMP_RCCONF}
	rcconf_edit_end
}

pkg_scripts_rm()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	[ -z "${pkg_scripts}" ] && return

	rcconf_edit_begin
	sed "/^pkg_scripts[[:>:]]/{s/[[:<:]]${_svc}[[:>:]]//g
	    s/['\"]//g;s/ *= */=/;s/   */ /g;s/ $//;/=$/d;}" \
	    /etc/rc.conf.local >${_TMP_RCCONF}
	rcconf_edit_end
}

rcconf_edit_begin()
{
	_TMP_RCCONF=$(mktemp -p /etc -t rc.conf.local.XXXXXXXXXX) || \
		rcctl_err "cannot create temporary file under /etc"
	if [ -f /etc/rc.conf.local ]; then
		cat /etc/rc.conf.local >${_TMP_RCCONF} || \
			rcctl_err "cannot append to ${_TMP_RCCONF}"
	else
		touch /etc/rc.conf.local || \
			rcctl_err "cannot create /etc/rc.conf.local"
	fi
}

rcconf_edit_end()
{
	sort -u -o ${_TMP_RCCONF} ${_TMP_RCCONF} || \
		rcctl_err "cannot modify ${_TMP_RCCONF}"
	cat ${_TMP_RCCONF} >/etc/rc.conf.local || \
		rcctl_err "cannot append to /etc/rc.conf.local"
	if [ ! -s /etc/rc.conf.local ]; then
		rm /etc/rc.conf.local || \
			rcctl_err "cannot remove /etc/rc.conf.local"
	fi
	rm -f ${_TMP_RCCONF}
	_rc_parse_conf # reload new values
}

svc_is_avail()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	[ -x "/etc/rc.d/${_svc}" ] && return 0
	svc_is_special ${_svc}
}

svc_is_base()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	grep -E 'start_daemon[[:space:]]+[[:alnum:]]' /etc/rc | \
		cut -d ' ' -f2- | grep -qw -- ${_svc}
}

svc_is_meta()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	[ -r "/etc/rc.d/${_svc}" ] && ! grep -qw "^rc_cmd" /etc/rc.d/${_svc}
}

svc_is_special()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	echo ${_special_svcs} | grep -qw -- ${_svc}
}

svc_ls()
{
	local _lsarg=$1
	[ -n "${_lsarg}" ] || return

	# we do not want to return the "status" nor the rc.d(8) script retcode
	local _ret=0 _on _svc _started

	case ${_lsarg} in
		all)
			(
				ls_rcscripts
				echo ${_special_svcs} | tr "[:blank:]" "\n"
			) | sort
			;;
		faulty)
			for _svc in $(svc_ls on); do
				! svc_is_special ${_svc} && \
					! /etc/rc.d/${_svc} check >/dev/null && \
					echo ${_svc} && _ret=1
			done
			;;
		off|on)
			for _svc in $(svc_ls all); do
				svc_get ${_svc} status && _on=1
					[ "${_lsarg}" = "on" -a -n "${_on}" ] || \
						[ "${_lsarg}" = "off" -a -z "${_on}" ] && \
					echo ${_svc}
				unset _on
			done
			;;
		started|stopped)
			for _svc in $(ls_rcscripts); do
				/etc/rc.d/${_svc} check >/dev/null && _started=1
				[ "${_lsarg}" = "started" -a -n "${_started}" ] || \
					[ "${_lsarg}" = "stopped" -a -z "${_started}" ] && \
					echo ${_svc}
				unset _started
			done
			;;
		*)
			_ret=1
	esac

	return ${_ret}
}

svc_get()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	local _status=0 _val _var=$2
	local daemon_class daemon_flags daemon_timeout daemon_user

	if svc_is_special ${_svc}; then
		daemon_flags="$(eval echo \${${_svc}})"
	else
		# set pkg daemon_flags to "NO" to match base svc
		if ! svc_is_base ${_svc}; then
			if ! echo ${pkg_scripts} | grep -qw -- ${_svc}; then
				daemon_flags="NO"
			fi
		fi

		if ! svc_is_meta ${_svc}; then
			# these are expensive, make sure they are explicitely requested
			if [ -z "${_var}" -o "${_var}" = "class" ]; then
				getcap -f /etc/login.conf ${_svc} 1>/dev/null 2>&1 && \
					daemon_class=${_svc}
				[ -z "${daemon_class}" ] && \
					daemon_class="$(svc_getdef ${_svc} class)"
			fi
			if [[ -z ${_var} || ${_var} == @(flags|status) ]]; then
				[ -z "${daemon_flags}" ] && \
					daemon_flags="$(eval echo \"\${${_svc}_flags}\")"
				[ -z "${daemon_flags}" ] && \
					daemon_flags="$(svc_getdef ${_svc} flags)"
			fi
			if [ -z "${_var}" -o "${_var}" = "timeout" ]; then
				[ -z "${daemon_timeout}" ] && \
					daemon_timeout="$(eval echo \"\${${_svc}_timeout}\")"
				[ -z "${daemon_timeout}" ] && \
					daemon_timeout="$(svc_getdef ${_svc} timeout)"
			fi
			if [ -z "${_var}" -o "${_var}" = "user" ]; then
				[ -z "${daemon_user}" ] && \
					daemon_user="$(eval echo \"\${${_svc}_user}\")"
				[ -z "${daemon_user}" ] && \
					daemon_user="$(svc_getdef ${_svc} user)"
			fi
		fi
	fi

	[ "${daemon_flags}" = "NO" ] && _status=1

	if [ -n "${_var}" ]; then
		[ "${_var}" = "status" ] && return ${_status}
		eval _val=\${daemon_${_var}}
		[ -z "${_val}" ] || print -r -- "${_val}"
	else
		svc_is_meta ${_svc} && return ${_status}
		if svc_is_special ${_svc}; then
			echo "${_svc}=${daemon_flags}"
		else
			echo "${_svc}_class=${daemon_class}"
			echo "${_svc}_flags=${daemon_flags}"
			echo "${_svc}_timeout=${daemon_timeout}"
			echo "${_svc}_user=${daemon_user}"
		fi
		return ${_status}
	fi
}

# to prevent namespace pollution, only call in a subshell
svc_getdef()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	local _status=0 _val _var=$2
	local daemon_class daemon_flags daemon_timeout daemon_user

	if svc_is_special ${_svc}; then
		# unconditionally parse: we always output flags and/or status
		_rc_parse_conf /etc/rc.conf
		daemon_flags="$(eval echo \${${_svc}})"
		[ "${daemon_flags}" = "NO" ] && _status=1
	else
		if ! svc_is_base ${_svc}; then
			_status=1 # all pkg_scripts are off by default
		else
			# abuse /etc/rc.conf behavior of only setting flags
			# to empty or "NO" to get our default status;
			# we'll get our default flags from the rc.d script
			[[ -z ${_var} || ${_var} == status ]] && \
				_rc_parse_conf /etc/rc.conf
			[ "$(eval echo \${${_svc}_flags})" = "NO" ] && _status=1
		fi

		if ! svc_is_meta ${_svc}; then
			rc_cmd() { }
			. /etc/rc.d/${_svc} >/dev/null 2>&1

			daemon_class=daemon
			[ -z "${daemon_timeout}" ] && daemon_timeout=30
			[ -z "${daemon_user}" ] && daemon_user=root
		fi
	fi

	if [ -n "${_var}" ]; then
		[ "${_var}" = "status" ] && return ${_status}
		eval _val=\${daemon_${_var}}
		[ -z "${_val}" ] || print -r -- "${_val}"
	else
		svc_is_meta ${_svc} && return ${_status}
		if svc_is_special ${_svc}; then
			echo "${_svc}=${daemon_flags}"
		else
			echo "${_svc}_class=${daemon_class}"
			echo "${_svc}_flags=${daemon_flags}"
			echo "${_svc}_timeout=${daemon_timeout}"
			echo "${_svc}_user=${daemon_user}"
		fi
		return ${_status}
	fi
}

svc_rm()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	rcconf_edit_begin
	if svc_is_special ${_svc}; then
		grep -v "^${_svc}.*=" /etc/rc.conf.local >${_TMP_RCCONF}
		( svc_getdef ${_svc} status ) && \
			echo "${_svc}=NO" >>${_TMP_RCCONF}
	else
		grep -Ev "^${_svc}_(flags|timeout|user).*=" \
			/etc/rc.conf.local >${_TMP_RCCONF}
		( svc_getdef ${_svc} status ) && \
			echo "${_svc}_flags=NO" >>${_TMP_RCCONF}
	fi
	rcconf_edit_end
}

svc_set()
{
	local _svc=$1 _var=$2
	[ -n "${_svc}" -a -n "${_var}" ] || return

	shift 2
	local _args="$*"

	# don't check if we are already enabled or disabled because rc.conf(8)
	# defaults may have changed in which case we may have a matching
	# redundant entry in rc.conf.local that we can drop
	if [ "${_var}" = "status" ]; then
		if [ "${_args}" = "on" ]; then
			_var="flags"
			# keep our flags if we're already enabled
			eval "_args=\"\${${_svc}_${_var}}\""
			[ "${_args}" = "NO" ] && unset _args
			if ! svc_is_base ${_svc} && ! svc_is_special ${_svc}; then
				pkg_scripts_append ${_svc}
			fi
		elif [ "${_args}" = "off" ]; then
			if ! svc_is_base ${_svc} && ! svc_is_special ${_svc}; then
				pkg_scripts_rm ${_svc}
			fi
			svc_rm ${_svc}
			return
		else
			rcctl_err "invalid status \"${_args}\""
		fi
	else
		svc_get ${_svc} status || \
			rcctl_err "${svc} is not enabled"
	fi

	if svc_is_special ${_svc}; then
		[ "${_var}" = "flags" ] || return
		rcconf_edit_begin
		grep -v "^${_svc}.*=" /etc/rc.conf.local >${_TMP_RCCONF}
		( svc_getdef ${_svc} status ) || \
			echo "${_svc}=YES" >>${_TMP_RCCONF}
		rcconf_edit_end
		return
	fi

	if [ -n "${_args}" ]; then
		if [ "${_var}" = "timeout" ]; then
			[[ ${_args} != +([[:digit:]]) || ${_args} -le 0 ]] && \
				rcctl_err "\"${_args}\" is not a positive integer"
		fi
		if [ "${_var}" = "user" ]; then
			getent passwd "${_args}" >/dev/null || \
				rcctl_err "user \"${_args}\" does not exist"
		fi
		# unset flags if they match the default enabled ones
		[ "${_args}" = "$(svc_getdef ${_svc} ${_var})" ] && \
			unset _args
	fi

	# protect leading whitespace
	[ "${_args}" = "${_args# }" ] || _args="\"${_args}\""

	# reset: value may have changed
	unset ${_svc}_${_var}

	rcconf_edit_begin
	grep -v "^${_svc}_${_var}.*=" /etc/rc.conf.local >${_TMP_RCCONF}
	if [ -n "${_args}" ] || \
	   ( svc_is_base ${_svc} && ! svc_getdef ${_svc} status && [ "${_var}" == "flags" ] ); then
		echo "${_svc}_${_var}=${_args}" >>${_TMP_RCCONF}
	fi
	rcconf_edit_end
}

unset _RC_DEBUG _RC_FORCE
while getopts "df" c; do
	case "$c" in
		d) _RC_DEBUG=-d;;
		f) _RC_FORCE=-f;;
		*) usage;;
	esac
done
shift $((OPTIND-1))
[ $# -gt 0 ] || usage

action=$1
ret=0

case ${action} in
	ls)
		lsarg=$2
		[[ ${lsarg} == @(all|faulty|off|on|started|stopped) ]] || usage
		;;
	order)
		shift 1
		svcs="$*"
		for svc in ${svcs}; do
			svc_is_avail ${svc} || \
				rcctl_err "service ${svc} does not exist" 2
		done
		;;
	disable|enable|start|stop|restart|reload|check)
		shift 1
		svcs="$*"
		[ -z "${svcs}" ] && usage
		for svc in ${svcs}; do
			svc_is_avail ${svc} || \
				rcctl_err "service ${svc} does not exist" 2
		done
		;;
	get|getdef)
		svc=$2
		var=$3
		[ -z "${svc}" ] && usage
		svc_is_avail ${svc} || \
			rcctl_err "service ${svc} does not exist" 2
		if [ -n "${var}" ]; then
			[[ ${var} != @(class|flags|status|timeout|user) ]] && usage
			if svc_is_meta ${svc}; then
				[ "${var}" != "status" ] && \
					rcctl_err "/etc/rc.d/${svc} is a meta script, cannot \"${action} ${var}\""
			fi
			if svc_is_special ${svc}; then
				[[ ${var} == @(class|timeout|user) ]] && \
					rcctl_err "\"${svc}\" is a special variable, cannot \"${action} ${var}\""
			fi
		fi
		;;
	set)
		svc=$2
		var=$3
		[ $# -ge 3 ] && shift 3 || shift $#
		args="$*"
		[ -z "${svc}" ] && usage
		svc_is_avail ${svc} || \
			rcctl_err "service ${svc} does not exist" 2
		[[ ${var} != @(class|flags|status|timeout|user) ]] && usage
		svc_is_meta ${svc} && [ "${var}" != "status" ] && \
			rcctl_err "/etc/rc.d/${svc} is a meta script, cannot \"${action} ${var}\""
		[[ ${var} = flags && ${args} = NO ]] && \
			rcctl_err "\"flags NO\" contradicts \"${action}\""
		if svc_is_special ${svc}; then
			[[ ${var} != status ]] && \
				rcctl_err "\"${svc}\" is a special variable, cannot \"${action} ${var}\""
		fi
		[[ ${var} == class ]] && \
			rcctl_err "\"${svc}_class\" is a read-only variable set in login.conf(5)"
		;;
	*)
		usage
		;;
esac

case ${action} in
	disable)
		needs_root ${action}
		for svc in ${svcs}; do
			svc_set ${svc} status off || ret=$?;
		done
		exit ${ret}
		;;
	enable)
		needs_root ${action}
		for svc in ${svcs}; do
			svc_set ${svc} status on || ret=$?;
		done
		exit ${ret}
		;;
	get)
		svc_get ${svc} "${var}"
		;;
	getdef)
		( svc_getdef ${svc} "${var}" )
		;;
	ls)
		# some rc.d(8) scripts need root for rc_check()
		[[ ${lsarg} == @(started|stopped|faulty) ]] && needs_root ${action} ${lsarg}
		svc_ls ${lsarg}
		;;
	order)
		if [ -n "${svcs}" ]; then
			needs_root ${action}
			pkg_scripts_order ${svcs}
		else
			[[ -z ${pkg_scripts} ]] || echo ${pkg_scripts}
		fi
		;;
	set)
		needs_root ${action}
		svc_set ${svc} "${var}" "${args}"
		;;
	start|stop|restart|reload|check)
		for svc in ${svcs}; do
			if svc_is_special ${svc}; then
				rcctl_err "\"${svc}\" is a special variable, no rc.d(8) script"
			fi
			/etc/rc.d/${svc} ${_RC_DEBUG} ${_RC_FORCE} ${action} || ret=$?;
		done
		exit ${ret}
		;;
	*)
		usage
		;;
esac