64 lines
1.5 KiB
Bash
64 lines
1.5 KiB
Bash
|
#!/bin/sh
|
|||
|
# S'assurer que BackupPC peux exécuter rsync avec les droits sudo
|
|||
|
# Jérémy GARDAIS — Mai 2018
|
|||
|
|
|||
|
# .. vim: foldmarker=[[[,]]]:foldmethod=marker
|
|||
|
|
|||
|
# Vars [[[
|
|||
|
SUCCESS=0
|
|||
|
ERROR=1
|
|||
|
DEBUG=0
|
|||
|
EUID=$(id -u)
|
|||
|
|
|||
|
BACKUP_USER_LOGIN="backup"
|
|||
|
#BACKUP_USER_LOGIN="backuppc"
|
|||
|
SUDOERS_LINE_REGEXP="${BACKUP_USER_LOGIN}.*ALL.*=.*(ALL:ALL).*NOEXEC:NOPASSWD:.*/usr/bin/rsync"
|
|||
|
# ]]]
|
|||
|
|
|||
|
# Functions [[[
|
|||
|
is_user() ## [[[
|
|||
|
{
|
|||
|
user_to_check="${1}"
|
|||
|
|
|||
|
if [ "$(id -- "${user_to_check}" 2> /dev/null)" ] ; then
|
|||
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : ${user_to_check} user is available."
|
|||
|
return "${SUCCESS}"
|
|||
|
else
|
|||
|
printf '\e[1;31m%-6s\e[m\n' "ERROR : ${user_to_check} user is unavailable."
|
|||
|
exit "${ERROR}"
|
|||
|
fi
|
|||
|
}
|
|||
|
## ]]]
|
|||
|
|
|||
|
is_sudoers_line() ## [[[
|
|||
|
{
|
|||
|
line_to_check="${1}"
|
|||
|
|
|||
|
if grep -Rq -- "${line_to_check}" /etc/sudoers.d/
|
|||
|
then
|
|||
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : ${line_to_check} line is available in sudo configuration."
|
|||
|
return "${SUCCESS}"
|
|||
|
else
|
|||
|
printf '\e[1;31m%-6s\e[m\n' "ERROR : ${line_to_check} was not found in sudo configuration."
|
|||
|
exit "${ERROR}"
|
|||
|
fi
|
|||
|
}
|
|||
|
## ]]]
|
|||
|
# ]]]
|
|||
|
|
|||
|
# Test permissions [[[
|
|||
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Check admin rights."
|
|||
|
if [ "${EUID}" -ne "0" ]; then
|
|||
|
printf '\e[1;31m%-6s\e[m\n' "ERROR : You need to run with SUDO/Admin permissions."
|
|||
|
exit "${ERROR}"
|
|||
|
fi
|
|||
|
# ]]]
|
|||
|
|
|||
|
is_user "${BACKUP_USER_LOGIN}"
|
|||
|
|
|||
|
is_sudoers_line "${SUDOERS_LINE_REGEXP}"
|
|||
|
|
|||
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : End"
|
|||
|
|
|||
|
exit 0
|