Run daily.sh as the user set by $config['user'] (#5036)

This commit is contained in:
Tony Murray
2016-12-01 13:45:53 -06:00
committed by Neil Lathwood
parent 694c3bc566
commit 287449510e

View File

@@ -19,12 +19,17 @@
#######################################
# CONSTANTS
#######################################
# define DIR_LIBRENMS as the directory this script is called from
DIR_LIBRENMS=$(dirname "$(readlink -f "$0")")
# define DAILY_SCRIPT as the full path to this script and LIBRENMS_DIR as the directory this script is in
DAILY_SCRIPT=$(readlink -f "$0")
LIBRENMS_DIR=$(dirname "$DAILY_SCRIPT")
# set log_file, using librenms $config['log_dir'], if set
# otherwise we default to <LibreNMS Install Directory>/logs
LOG_DIR=$(php -r "include '${DIR_LIBRENMS}/config.php'; echo isset(\$config['log_dir']) ? \$config['log_dir'] : '${DIR_LIBRENMS}/logs';")
LOG_DIR=$(php -r "@include '${LIBRENMS_DIR}/config.php'; echo isset(\$config['log_dir']) ? \$config['log_dir'] : '${LIBRENMS_DIR}/logs';")
# get the librenms user
LIBRENMS_USER=$(php -r "@include '${LIBRENMS_DIR}/config.php'; echo isset(\$config['user']) ? \$config['user'] : 'root';")
LIBRENMS_USER_ID=$(id -u "$LIBRENMS_USER")
#######################################
@@ -70,7 +75,7 @@ status_run() {
#######################################
# Call daily.php
# Globals:
# DIR_LIBRENMS
# LIBRENMS_DIR
# Arguments:
# args:
# Array of arguments to pass to
@@ -82,14 +87,14 @@ call_daily_php() {
local args=( "$@" );
for arg in "${args[@]}"; do
php "${DIR_LIBRENMS}/daily.php" -f "${arg}";
php "${LIBRENMS_DIR}/daily.php" -f "${arg}";
done
}
#######################################
# Entry into program
# Globals:
# DIR_LIBRENMS
# LIBRENMS_DIR
# Arguments:
#
# Returns:
@@ -97,12 +102,26 @@ call_daily_php() {
#######################################
main () {
local arg="$1";
cd ${DIR_LIBRENMS};
cd ${LIBRENMS_DIR};
# if not running as $LIBRENMS_USER (unless $LIBRENMS_USER = root), relaunch
if [[ "$LIBRENMS_USER" != "root" ]]; then
# only try to su if we are root (or sudo)
if [[ "$EUID" -eq 0 ]]; then
echo "Re-running ${DAILY_SCRIPT} as ${LIBRENMS_USER} user"
su -l "$LIBRENMS_USER" -c "$DAILY_SCRIPT $@"
exit;
fi
if [[ "$EUID" -ne "$LIBRENMS_USER_ID" ]]; then
printf "\033[0;93mWARNING\033[0m: You should run this script as ${LIBRENMS_USER}\n";
fi
fi
if [[ -z "$arg" ]]; then
up=$(php daily.php -f update >&2; echo $?)
if [[ "$up" == "0" ]]; then
$0 no-code-update
$DAILY_SCRIPT no-code-update
exit
elif [[ "$up" == "1" ]]; then
# Update to Master-Branch
@@ -131,7 +150,7 @@ main () {
if [[ -z "$cnf" ]] || [[ "$cnf" == "0" ]] || [[ "$cnf" == "false" ]]; then
# Call ourself again in case above pull changed or added something to daily.sh
$0 post-pull
$DAILY_SCRIPT post-pull
fi
else
case $arg in
@@ -139,14 +158,14 @@ main () {
# Updates of the code are disabled, just check for schema updates
# and clean up the db.
status_run 'Updating SQL-Schema' 'php includes/sql-schema/update.php'
status_run 'Cleaning up DB' "$0 cleanup"
status_run 'Cleaning up DB' "$DAILY_SCRIPT cleanup"
;;
post-pull)
# List all tasks to do after pull in the order of execution
status_run 'Updating SQL-Schema' 'php includes/sql-schema/update.php'
status_run 'Updating submodules' "$0 submodules"
status_run 'Cleaning up DB' "$0 cleanup"
status_run 'Fetching notifications' "$0 notifications"
status_run 'Updating submodules' "$DAILY_SCRIPT submodules"
status_run 'Cleaning up DB' "$DAILY_SCRIPT cleanup"
status_run 'Fetching notifications' "$DAILY_SCRIPT notifications"
;;
cleanup)
# Cleanups