2018-05-28 16:20:58 +02:00
|
|
|
|
#!/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"
|
2018-05-28 17:52:23 +02:00
|
|
|
|
SUDOERS_LINE="${BACKUP_USER_LOGIN} ALL=(ALL:ALL) NOEXEC:NOPASSWD: /usr/bin/rsync"
|
|
|
|
|
SUDOERS_FILE="/etc/sudoers.d/backuppc_noexec"
|
2018-05-28 16:20:58 +02:00
|
|
|
|
# ]]]
|
|
|
|
|
|
|
|
|
|
# Functions [[[
|
|
|
|
|
is_user() ## [[[
|
|
|
|
|
{
|
|
|
|
|
user_to_check="${1}"
|
|
|
|
|
|
|
|
|
|
if [ "$(id -- "${user_to_check}" 2> /dev/null)" ] ; then
|
2018-05-28 17:52:23 +02:00
|
|
|
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Function is_user() — ${user_to_check} user is available."
|
2018-05-28 16:20:58 +02:00
|
|
|
|
return "${SUCCESS}"
|
|
|
|
|
else
|
2018-05-28 17:52:23 +02:00
|
|
|
|
printf '\e[1;31m%-6s\e[m\n' "ERROR : Function is_user() — ${user_to_check} user is unavailable."
|
2018-05-28 16:20:58 +02:00
|
|
|
|
exit "${ERROR}"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
## ]]]
|
|
|
|
|
is_sudoers_line() ## [[[
|
|
|
|
|
{
|
|
|
|
|
line_to_check="${1}"
|
|
|
|
|
|
|
|
|
|
if grep -Rq -- "${line_to_check}" /etc/sudoers.d/
|
|
|
|
|
then
|
2018-05-28 17:52:23 +02:00
|
|
|
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Function is_sudoers_line() — ${line_to_check} line is available in sudo configuration."
|
2018-05-28 16:20:58 +02:00
|
|
|
|
return "${SUCCESS}"
|
|
|
|
|
else
|
2018-05-28 17:52:23 +02:00
|
|
|
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Function is_sudoers_line() — ${line_to_check} was not found in sudo configuration."
|
2018-05-28 17:00:21 +02:00
|
|
|
|
return "${ERROR}"
|
2018-05-28 16:20:58 +02:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
## ]]]
|
2018-05-28 17:52:23 +02:00
|
|
|
|
add_sudoers_conf() ## [[[
|
|
|
|
|
{
|
|
|
|
|
sudoers_conf="${1}"
|
|
|
|
|
sudoers_file="${2}"
|
|
|
|
|
|
|
|
|
|
## Empty sudoers file
|
|
|
|
|
true > "${sudoers_file}"
|
|
|
|
|
|
|
|
|
|
## Set sudoers configuration for BackupPC
|
|
|
|
|
cat << EOF >> "${sudoers_file}"
|
|
|
|
|
# This file was generated by fix_backuppc_linux_sudo.sh script.
|
|
|
|
|
|
|
|
|
|
# Permissions for BackupPC - Backup tool
|
|
|
|
|
${sudoers_conf}
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Function add_sudoers_conf() — ${sudoers_file} was modified."
|
|
|
|
|
}
|
|
|
|
|
## ]]]
|
2018-05-28 16:20:58 +02:00
|
|
|
|
# ]]]
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
# ]]]
|
|
|
|
|
|
2018-05-28 17:52:23 +02:00
|
|
|
|
# Ensure the backup user is available
|
2018-05-28 16:20:58 +02:00
|
|
|
|
is_user "${BACKUP_USER_LOGIN}"
|
|
|
|
|
|
2018-05-28 17:52:23 +02:00
|
|
|
|
# Test if sudoers conf is already set
|
|
|
|
|
if ! is_sudoers_line "${SUDOERS_LINE_REGEXP}"
|
2018-05-28 17:31:54 +02:00
|
|
|
|
then
|
|
|
|
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : First try — ${SUDOERS_LINE_REGEXP} was not found in sudo configuration."
|
2018-05-28 17:52:23 +02:00
|
|
|
|
## Add sudoers configuration
|
|
|
|
|
add_sudoers_conf "${SUDOERS_LINE}" "${SUDOERS_FILE}"
|
|
|
|
|
|
|
|
|
|
## Test if sudoers conf was successfully modified
|
|
|
|
|
if ! is_sudoers_line "${SUDOERS_LINE_REGEXP}"
|
|
|
|
|
then
|
|
|
|
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Second try — ${SUDOERS_LINE_REGEXP} was not found in sudo configuration."
|
|
|
|
|
printf '\e[1;31m%-6s\e[m\n' "ERROR : The sudo configuration was not successfully modified."
|
|
|
|
|
exit "${ERROR}"
|
|
|
|
|
fi
|
2018-05-28 17:31:54 +02:00
|
|
|
|
fi
|
2018-05-28 16:20:58 +02:00
|
|
|
|
|
|
|
|
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : End"
|
|
|
|
|
|
2018-05-28 17:52:23 +02:00
|
|
|
|
printf '%b\n' "Your configuration is set up."
|
|
|
|
|
|
|
|
|
|
exit "${SUCCESS}"
|