#!/bin/bash # Automatisation de configuration backuppc pour poste MAC # Institut de Physique de Rennes UMR6251 # Jérémy GARDAIS, Guillaume RAFFY — Avril 2018 SUCCESS=0 ERROR=1 # Functions {{{ ## GetDefaultUser {{{ function GetDefaultUser() { for user in $(ListUsers); do echo $user return done } ## }}} ## AllowUserToConnectToThisMachineUsingSsh {{{ # this performs the equivalent as adding a remote login user in system preferences using the gui function AllowUserToConnectToThisMachineUsingSsh() { local userLogin="$1" #dscl . append '/Groups/com.apple.access_ssh' user "${userLogin}" #dscl . append /Groups/com.apple.access_ssh groupmembers $(dscl . read "/Users/${userLogin}" GeneratedUID | cut -d " " -f 2) printf '\e[1;31m%-6s\e[m\n' "DEBUG : Autoriser les accès SSH pour ${userLogin}." } ## }}} ## EnsurePingIsAllowed {{{ function EnsurePingIsAllowed() { #sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setstealthmode off printf '\e[1;31m%-6s\e[m\n' "DEBUG : Désactivation du mode furtif (ping)." if [ $? != 0 ]; then printf '\e[1;31m%-6s\e[m\n' "ERREUR : La désactivation du mode furtif a échoué"; return "$ERROR" fi } ## }}} ## EnsureSshdIsRunning {{{ function EnsureSshdIsRunning() { #sudo launchctl list | grep 'com.openssh.sshd' &> /dev/null printf '\e[1;31m%-6s\e[m\n' "DEBUG : Vérification si sshd est déjà en cours." if [ $? != 0 ]; then # enable 'Remote login' in 'system preferences' sudo launchctl enable system/com.openssh.sshd &> /dev/null sudo launchctl load /System/Library/LaunchDaemons/ssh.plist &> /dev/null fi #sudo launchctl list | grep 'com.openssh.sshd' &> /dev/null printf '\e[1;31m%-6s\e[m\n' "DEBUG : Activation du serveur ssh." if [ $? != 0 ]; then printf '\e[1;31m%-6s\e[m\n' "ERREUR : L'activation du serveur ssh a échoué"; return "$ERROR" fi } ## }}} ## GetMyHostKey {{{ function GetMyHostKey() { hostkey="/etc/ssh_host_rsa_key.pub" if [ ! -f "${hostkey}" ]; then hostkey="/etc/ssh/ssh_host_rsa_key.pub" if [ ! -f "${hostkey}" ]; then printf '\e[1;31m%-6s\e[m\n' "ERREUR : Impossible de trouver la clef ssh publique ce cette machine, SSH est-il bien installé?\n Installation annulée." return "$ERROR" fi fi echo "${hostkey}" } ## }}} ## IpAddress {{{ function IpAddress() { local strMyIpAddress='' local strOsName=$( uname ) strMyIpAddress=$(dig +short myip.opendns.com @resolver1.opendns.com) if [ "$strMyIpAddress" == '' ]; then error "failed to retrieve the ip address of this machine" return 1 fi echo $strMyIpAddress } ## }}} ## MyFqdn {{{ function MyFqdn() { local strMyIpAddress=$( IpAddress ) # eg '129.20.27.49' local strMyFqdn=$(host $strMyIpAddress | awk '{print $5}') echo ${strMyFqdn%?} # remove the trailing '.' } ## }}} ## ListUsers {{{ function ListUsers() { local users='' for user in $(ls -d /Users/[a-zA-Z]*); do user=$(basename $user) case "$user" in 'Shared'|'admin') ;; *) users="$users $user" ;; esac done 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 L’ON A BIEN LES DROITS ADMIN #### if [ "$EUID" -ne 0 ]; then printf '\e[1;31m%-6s\e[m\n' "ERREUR : À lancer avec les droits administrateur " exit fi #### NOM DU COMPTE À SAUVEGARDER #### default_user=$(GetDefaultUser) printf '\e[1;34m%-6s\e[m\n' "liste des comptes détectés sur cette machine : $(ListUsers)" printf "login de l’utilisateur dont les données sont à sauvegarder ?\n [${default_user}] : " read input_login if [[ ${input_login} != "" ]]; then usr="${input_login}" else usr="${default_user}" fi #### VÉRIFIER QUE LE COMPTE EST BIEN DANS LA LISTE DES USER ID #### if ! id "${usr}" &> /dev/null; then printf '\e[1;35m%-6s\e[m\n' "${usr} n’apparait pas dans la liste des user ids. Continuer tout de même ? [o/N] : " read input_continue if [[ "${input_continue}" != "o" ]]; then printf '\e[1;31m%-6s\e[m\n' "Installation annulée." exit fi fi #### DOSSIER À SAUVEGARDER #### default_dir=$(eval echo ~"${usr}") if [ ! -d "${default_dir}" ]; then default_dir="" fi printf "Par défaut, le dossier sauvegardé est le home de l’utilisateur. Il est possible d’en ajouter un supplémentaire ensuite. Dossier à sauvegarder ? [${default_dir}] : " read input_dir if [[ "${input_dir}" == "" ]]; then dir1="${default_dir}" else dir1="${input_dir}" fi #### DOSSIER À SAUVEGARDER INTROUVABLE, ANNULATION #### if [ ! -d "${dir1}" ]; then printf "\n" printf '\e[1;31m%-6s\e[m\n' "ERREUR : Dossier ${dir1} introuvable, installation annulée." exit fi #### DOSSIER SUPPLÉMENTAIRE #### printf "Si vous avez un dossier supplémentaire à sauvegarder (/mnt/data par exemple) entrer-le maintenant, sinon laissez vide. [] : " read input_dir2 if [[ "${input_dir2}" != "" ]]; then #### DOSSIER SUPPLÉMENTAIRE INTROUVABLE, ANNULATION DE CELUI-CI #### if [ ! -d "${input_dir2}" ]; then printf "\n" printf '\e[1;35m%-6s\e[m\n' "Dossier supplémentaire introuvable, non ajouté." else directories="'${dir1}','${input_dir2}'" fi else directories="'${dir1}'" fi #### AUTORISER LE PING #### EnsurePingIsAllowed #### INSTALLATION DE OPENSSH-SERVER #### EnsureSshdIsRunning if [ "$?" != "$SUCCESS" ]; then printf '\e[1;31m%-6s\e[m\n' "ERREUR : L'installation du serveur ssh a échoué, installation annulée."; exit fi hostkey=$(GetMyHostKey) #printf "hostkey=$hostkey" if [ "$?" != "$SUCCESS" ]; then printf '\e[1;31m%-6s\e[m\n' "ERREUR : Clé inaccessible, merci de contacter votre administrateur réseau, installation annulée."; exit fi #### CRÉATION DU FICHIER DE CONFIGURATION #### fqdn="$(MyFqdn)" filepl="${fqdn}.pl" exclude="['ownCloud','.local/share/Trash','.cache','.Play*','.steam','.wine','Perso','temp','tmp','.Trash*','.DS_Store','._*', '.thumbnails','.ssh/id_*','.xsession-*']" echo "\$Conf{XferMethod} = 'rsync';" > "${filepl}" echo "\$Conf{RsyncShareName} = [${directories}];" >> "${filepl}" echo "\$Conf{BackupFilesExclude} = {'*' => ${exclude} };" >> "${filepl}" printf "\n" printf '\e[1;34m%-6s\e[m\n' "Fichier de configuration créé (${filepl})" #### ADRESSE MAIL DE L’UTILISATEUR #### printf "Votre adresse e-mail ?\n : " read input_mail #### LE SERVEUR DOIT CONNAITRE #### # ssh_host_ecdsa_key.pub DU CLIENT # L’ADRESSE MAIL # L'IP # Le nom d'utilisateur echo "# host:${fqdn}" >> "${filepl}" cmd_hostkey=$(cat "${hostkey}") echo "# hostkey:${cmd_hostkey}" >> "${filepl}" echo "# mail:${input_mail}" >> "${filepl}" echo "# ip:$(IpAddress)" >> "${filepl}" echo "# username:${usr}" >> "${filepl}" #### Déplacer le fichier dans le dossier de l'utilisateur (plus accessible) mv "${filepl}" "${dir1}" #### CRÉATION DE L’UTILISATEUR BACKUPPC #### printf '\e[1;34m%-6s\e[m\n' "Création du compte backuppc…" homebackuppc='/var/lib/backuppc' AddUserBackuppc "${input_login}" "${homebackuppc}" #mkdir -p -- "${homebackuppc}"/.ssh printf '\e[1;31m%-6s\e[m\n' "DEBUG : Création du répertoire .ssh de l'utilisateur : ${homebackuppc}/.ssh" #echo "from=\"129.20.203.16\" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIhMc8ixQXfWDACJy4q0v8T877UxahhCjO51PQFzylwVpf88LX3yWeDrWIW0NRu0zoSm396mig918OpD5ggqML/QbYbQsoDdAFUV/tK4JU6UJgEQIl25MOcUBCFepsFBGS09CH/V07xSUqSP/+beeTRLNO2CQzk3S2y3YfkXpM7KmOGfeLgoCaQAcxIkgLXeM3TpCZEzJDlZ8c8k/DjVvsgwCpQktYzNo2b37KHLLfgyW9KSo6N9sReUuNQjS6lu8rjrXfc6+J0pY2D6IxWptTWL/JVrhFCUqe4QQy+xYjoR41wqnAQyl/kOcyBNhSvojMKwQT6vlPwru6pOno16/X backuppc@backuppc.ipr.univ-rennes1.fr" > "${homebackuppc}"/.ssh/authorized_keys printf '\e[1;31m%-6s\e[m\n' "DEBUG : Ajout de la clef SSH du server dans "${homebackuppc}"/.ssh/authorized_keys." #chown -R backuppc "${homebackuppc}"/.ssh/ printf '\e[1;31m%-6s\e[m\n' "DEBUG : chown -R backuppc ${homebackuppc}/.ssh/" #### END #### printf "\n" printf '\e[1;34m%-6s\e[m\n' "Configuration du poste terminée." printf '\e[1;34m%-6s\e[m\n' "Envoyez bien votre fichier de configuration (${dir1}/${filepl}) à Jérémy GARDAIS (jeremy.gardai@univ-rennes1.fr)." printf '\e[1;34m%-6s\e[m\n' "Vous pourrez affiner la configuration de votre sauvegarde depuis https://backuppc.ipr.univ-rennes1.fr" exit 0