Correction d'un problème introduit dans le correctif précédent. Les terminaux SH et ZSH déclenchaient la fonction de nettoyage "identities_destroy" dès la sortie de la fonction "identities_create". Auparavant, la commande trap était éxecutée en dehors des fonctions et donc la sortie de la fonction n'équivalait pas à un EXIT. Ce comportement n'affectait pas les utilisateurs sous Bash, dont la gestion des subshells est différente. Cette correction revoit l'intégralité du script et supprime la fonction devenue obsolète "identities_create" au profit d'un code exécuté directement. Cela permet également de mieux réarrager le code et de définir la fonction "identities_destroy" dès le lancement du script.
87 lines
2.8 KiB
Bash
87 lines
2.8 KiB
Bash
#!/bin/sh
|
|
|
|
# Variables à définir par l'utilisateur.
|
|
Bitwarden_Email="<EMAIL_DE_CONNEXION_A_BITWARDEN>"
|
|
Bitwarden_Server="<URL_DE_L_INSTANCE_BITWARDEN>"
|
|
|
|
# Variables internes au script
|
|
Inception_Detect=0 # Détection de terminaux parents/enfants
|
|
Local_Version="2.2.7"
|
|
|
|
# Fonction de suppression et nettoyage des identités
|
|
identities_destroy() {
|
|
tput setaf 4
|
|
echo "Déconnexion des identités en cours..."
|
|
|
|
bw logout
|
|
ssh-add -D
|
|
pkill ssh-agent
|
|
echo "Suppression des fichiers générés..."
|
|
# shellcheck disable=SC2016
|
|
grep -in "printf.*\"\%s\".*\"\$BW_List\"" "${HOME}"/.identities-loaded.sh | grep ">" | cut -d ">" -f 2 | sed -e "s,\~,${HOME},g ; s,\"\$HOME\",${HOME},g ; s,^\ ,,g" | while IFS= read -r each; do
|
|
rm -rf "${each}"
|
|
done
|
|
unset BW_Session
|
|
|
|
echo "Déconnexion des identités terminée"
|
|
tput sgr0
|
|
}
|
|
|
|
# Processus de création des identités
|
|
if [ -z "${BW_Session}" ] || [ "${#BW_Session}" -lt 10 ]; then
|
|
trap identities_destroy EXIT
|
|
Latest_Version=$(curl -s "https://gitea.cloudyfy.fr/Siphonight/bw-cli_autologger/src/branch/main/bw_cli_autolog.sh" | grep -m1 "Local_Version" | cut -d ';' -f 2 | cut -d '&' -f 1)
|
|
if [ "${Local_Version}" != "${Latest_Version}" ]; then
|
|
tput setaf 1
|
|
echo "Votre version de l'utilitaire n'est pas à jour."
|
|
echo "Version locale : ${Local_Version} | Dernière version : ${Latest_Version}"
|
|
echo "Récupérez la dernière version sur https://gitea.cloudyfy.fr/Siphonight/bw-cli_autologger ."
|
|
else
|
|
tput setaf 2
|
|
echo "Votre utilitaire est à jour avec la dernière version ! Happy Connect =)"
|
|
fi
|
|
tput setaf 4
|
|
echo "Bitwarden CLI autologger version ${Local_Version}"
|
|
echo "Lancement du autologin (ne prenez pas en compte les tokens, ils sont gérés automatiquement)."
|
|
tput sgr0
|
|
identities_destroy
|
|
bw config server "${Bitwarden_Server}"
|
|
# shellcheck disable=SC2155
|
|
export BW_Session="$(bw login "${Bitwarden_Email}" | tee /dev/tty | grep -m1 '==' | cut -d '"' -f 2)"
|
|
Inception_Detect=1
|
|
fi
|
|
|
|
BW_List=$(bw list items --session "${BW_Session}")
|
|
if [ "${BW_List}" != "" ]; then
|
|
|
|
if [ "${Inception_Detect}" != "1" ]; then
|
|
trap 'kill $SSH_AGENT_PID' EXIT
|
|
unset BW_Session
|
|
fi
|
|
|
|
# shellcheck disable=SC2009
|
|
if [ "$(ps -ax | grep 'sh -c while true; do sleep 10; bw sync -f; done' | grep -cv 'grep')" = "0" ]; then
|
|
nohup sh -c 'while true; do sleep 10; bw sync -f; done' >/dev/null 2>&1 &
|
|
fi
|
|
|
|
tput setaf 4
|
|
echo ""
|
|
echo "Bitwarden connecté, mise en place des identités..."
|
|
tput sgr0
|
|
|
|
eval "$(ssh-agent)"
|
|
|
|
# shellcheck disable=SC3037
|
|
printf "%s" "${BW_List}" | jq -r '.[] | select(.name=="identities - configs") | .notes' >"${HOME}"/.identities-loaded.sh
|
|
chmod 700 "${HOME}"/.identities-loaded.sh
|
|
# shellcheck disable=SC1091
|
|
. "${HOME}"/.identities-loaded.sh
|
|
|
|
unset BW_List
|
|
|
|
else
|
|
tput setaf 4
|
|
echo "Bitwarden non connecté, les identités n'ont pas été mises en place."
|
|
tput sgr0
|
|
fi
|