Add functions for backuppc user

Create the user.
Allow the backuppc user to use rsync with sudo without password.
This commit is contained in:
Jeremy Gardais 2018-04-25 15:31:14 +02:00
parent 4d41973e92
commit febfa4ecc4
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 57 additions and 0 deletions

View File

@ -94,6 +94,63 @@ function ListUsers() {
echo $users
}
# AddUserBackuppc {{{
function AddUserBackuppc() {
local userToBackup="$1" # the login of the user to backup
local homeDir="$2" # par exemple '/var/lib/backuppc'
local userLogin='backuppc'
local groupId=$(id -g $userToBackup)
maxid=$(dscl . -list /Users UniqueID | awk '$2 < 1000 {print $2}' | sort -ug | tail -1)
newid=$((maxid+1))
mkdir -p "$homeDir"
id "$newid" &> /dev/null
if [ $? = 0 ]; then
echo "unable to find a suitable uid for user backuppc ($newid is already used)"
exit $ERROR
fi
# Create user
#dscl . -create "/Users/$userLogin"
#dscl . -create "/Users/$userLogin" UserShell /bin/bash
#dscl . -create "/Users/$userLogin" RealName "backuppc"
#dscl . -create "/Users/$userLogin" UniqueID "$newid"
#dscl . -create "/Users/$userLogin" PrimaryGroupID "$groupId"
#dscl . -create "/Users/$userLogin" NFSHomeDirectory "$homeDir"
#dscl . -create "/Users/$userLogin" IsHidden 1 # hide from login window
printf '\e[1;31m%-6s\e[m\n' "DEBUG: Création de l'utilisateur ${userLogin}."
# Homedir permissions
#chown -R "$userLogin:$groupId" "$homeDir"
printf '\e[1;31m%-6s\e[m\n' "DEBUG: chown -R $userLogin:$groupId $homeDir"
allowUserToConnectToThisMachineUsingSsh "${userLogin}"
AllowBackuppcSudo
}
# }}}
# AllowBackuppcSudo {{{
function AllowBackuppcSudo() {
# Get the configuration directory for sudoers
if [ -f /etc/sudoers ]; then
local sudoersDir=$(grep "^#includedir " /etc/sudoers | cut -d" " -f2)
elif [ -f /private/etc/sudoers ]; then
local sudoersDir=$(grep "^#includedir " /private/etc/sudoers | cut -d" " -f2)
else
printf '\e[1;31m%-6s\e[m\n' "Unable to find sudo configuration file."
return "$ERROR"
fi
#sudo mkdir -p -- "${sudoersDir}"
printf '\e[1;31m%-6s\e[m\n' "DEBUG: Création du dossier ${sudoersDir}."
# Allow user to use rsync with sudo
#sudo sh -c "echo '${userLogin} ALL=(ALL:ALL) NOEXEC:NOPASSWD: /usr/bin/rsync' > ${sudoersDir}/backuppc_noexec"
printf '\e[1;31m%-6s\e[m\n' "DEBUG: ajout de '${userLogin} ALL=(ALL:ALL) NOEXEC:NOPASSWD: /usr/bin/rsync'"
printf '\e[1;31m%-6s\e[m\n' "DEBUG: dans le fichier ${sudoersDir}/backuppc_noexec."
}
# }}}
#### VÉRIFIER QUE LON A BIEN LES DROITS ADMIN ####
if [ "$EUID" -ne 0 ]; then
printf '\e[1;31m%-6s\e[m' "À lancer avec les droits administrateur "