Test if a backup user is available from a list

This commit is contained in:
Jeremy Gardais 2019-05-28 14:07:44 +02:00
parent e0e6800c3c
commit c1e7d54ad1
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 17 additions and 12 deletions

View File

@ -10,7 +10,7 @@ ERROR=1
DEBUG=1 DEBUG=1
USER_ID=$(id -u) USER_ID=$(id -u)
BACKUP_USER_LOGIN="backup" BACKUP_USER_LIST="backup backuppc"
BACKUP_SUDOERS_LINE_REGEXP="backuppc.*ALL.*=.*(ALL:ALL).*NOEXEC:NOPASSWD:.*/usr/bin/rsync" BACKUP_SUDOERS_LINE_REGEXP="backuppc.*ALL.*=.*(ALL:ALL).*NOEXEC:NOPASSWD:.*/usr/bin/rsync"
BACKUP_SUDOERS_LINE="backup ALL=(ALL:ALL) NOEXEC:NOPASSWD: /usr/bin/rsync BACKUP_SUDOERS_LINE="backup ALL=(ALL:ALL) NOEXEC:NOPASSWD: /usr/bin/rsync
backuppc ALL=(ALL:ALL) NOEXEC:NOPASSWD: /usr/bin/rsync" backuppc ALL=(ALL:ALL) NOEXEC:NOPASSWD: /usr/bin/rsync"
@ -18,17 +18,22 @@ BACKUP_SUDOERS_FILE="/etc/sudoers.d/backuppc_noexec"
# ]]] # ]]]
# Functions [[[ # Functions [[[
is_user() ## [[[
{
user_to_check="${1}"
if [ "$(id -- "${user_to_check}" 2> /dev/null)" ] ; then ## Check if a backup user is present on the system from a list of users
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Function is_user() — ${user_to_check} user is available." is_backup_user() ## [[[
return "${SUCCESS}" {
else userlist_to_check="${1}"
printf '\e[1;31m%-6s\e[m\n' "ERROR: Function is_user() — ${user_to_check} user is unavailable."
exit "${ERROR}" for user_to_check in ${userlist_to_check}; do
fi if [ "$(id -- "${user_to_check}" 2> /dev/null)" ] ; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Function is_backup_user() — ${user_to_check} user is available."
return "${SUCCESS}"
fi
done
## Otherwise exit with error status
printf '\e[1;31m%-6s\e[m\n' "ERROR: Function is_backup_user() — none of these users: ${userlist_to_check} are available on the system."
exit "${ERROR}"
} }
## ]]] ## ]]]
is_sudoers_line() ## [[[ is_sudoers_line() ## [[[
@ -75,7 +80,7 @@ fi
# ]]] # ]]]
# Ensure the backup user is available # Ensure the backup user is available
is_user "${BACKUP_USER_LOGIN}" is_backup_user "${BACKUP_USER_LIST}"
# Test if sudoers conf is already set # Test if sudoers conf is already set
if ! is_sudoers_line "${BACKUP_SUDOERS_LINE_REGEXP}" if ! is_sudoers_line "${BACKUP_SUDOERS_LINE_REGEXP}"