From 042d197b4d989ec6461204b9d897054e5f5dd492 Mon Sep 17 00:00:00 2001 From: Sebastien Luttringer Date: Tue, 31 May 2011 09:16:17 +0200 Subject: Ensure rc.d scripts are run as root By default all script should be run as root. If NEED_ROOT=0 is set before loading /etc/rc.d/functions in a rc.d scripts, this will not apply. This allow script with only some part which require root level to call need_root() function at beginning of those parts. Close FS#24095 Signed-off-by: Sebastien Luttringer --- functions | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'functions') diff --git a/functions b/functions index adf4ea9..2ee56b4 100644 --- a/functions +++ b/functions @@ -394,5 +394,15 @@ for f in /etc/rc.d/functions.d/*; do [[ -e $f ]] && . "$f" done +# Exit current shell if user is not root +need_root() { + (( $EUID != 0 )) && printf 'You need to be root.\n' && exit 1 +} + +# Quit script if it's not running by root +# This can be disabled in scripts sourcing functions by setting NEED_ROOT=0 +# A local call to need_root can be done to ensure part of script need root privilege +(( ${NEED_ROOT:-1} == 1 )) && need_root + # End of file # vim: set ts=2 sw=2 noet: -- cgit v1.2.3