scripts/utils/cranspasswords
Olivier Huber 9df1d0d9f0 [./utils/cranspasswords] Ajout de ma clé GPG
darcs-hash:20090213122222-8fbb1-79ec390852b8114f88d3e778cf04fc73e32dc832.gz
2009-02-13 13:22:22 +01:00

176 lines
5.4 KiB
Bash
Executable file

#! /bin/bash
###############################################################################
# cranspasswords : Outil de (dé)chiffrage de mots de passe
###############################################################################
#
# Copyright (C) 2006 Etienne Chové
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
###############################################################################
# variables générales
SRV=vert.adm.crans.org
REP=/root
TMP=`if which tempfile &> /dev/null; then tempfile -m 600; else mktemp -t cranspasswords.XXXX; fi`
trap "rm -f ${TMP} ${TMP}.asc" EXIT
##############################################################################
# parsage des arguments
if [[ -z "$1" ]] ; then
ACTION="--view"
CHOIX=""
elif echo $1 | grep -q '^--' ; then
ACTION=$1
CHOIX=$2
else
ACTION='--view'
CHOIX=$1
fi
##############################################################################
# fonctions utiles
function liste () {
# donne la liste des mot de passe
echo -e "\033[1;34mListe des mot de passe disponibles :\033[1;0m" >&2
ssh ${SRV} "ls ${REP} | grep -e '.asc$' | sed 's/\.asc$//g' | sed 's/^/ /g'" 2> /dev/null
}
function choix () {
# choix du fichier
if [[ -z "$CHOIX" ]] ; then
liste
echo
echo -e -n "\033[1;34mChoix : \033[1;0m"
read CHOIX
else
echo -e "\033[1;34mChoix :\033[1;0m $CHOIX"
fi
echo
}
function dechiffre () {
# récupère le mot de passe $CHOIX, le déchiffre et le colle dans $TMP
# ${TMP} sera vide si les pass n'existe pas
echo -e "\033[1;34mConnection à ${SRV}\033[1;0m"
ssh ${SRV} -t "sudo -p 'Mot de passe sudo : ' echo -n" 2> /dev/null
echo
echo -e "\033[1;34mRécupération du fichier et déchifrage\033[1;0m"
ssh ${SRV} "sudo cat ${REP}/$CHOIX.asc 2> /dev/null" | gpg --decrypt 2> /dev/null > ${TMP}
echo
}
function chiffre () {
# chiffre les fichier $TMP et les colle dans le fichier $CHOIX
echo -e "\033[1;34mChiffrage et envoi sur ${SRV}\033[1;0m"
if [[ ! -s ${TMP} ]] ; then
echo -e "\033[1;31mFichier vide, abandon...\033[1;0m"
exit 1
fi
RECIPIENTS=$(while read a b c; do echo -n "--recipient $c "; done << EOF
# 6D1DF0FA pauget@crans.org
# BD156CC4 dubost@crans.org
# 98E76332 etienne.chove@crans.org
# 4EF9E1D1 xavier.pessoles@crans.org
# F22A794E Vincent.Bernat@crans.org
# FBFA4781 Nicolas.Stransky@crans.org
# FCE03DAA Stephane.Glondu@crans.org
# B7B4AEA6 Bobot@crans.org
# 7D980513 parret-freaud@crans.org
# CDF91D96 cohen@crans.org
# 81DDB065 gregoire.detrez@crans.org
# 3603EFD9 alexandre@alexandre-bos.fr
# 2127F85A jdimino@dptinfo.ens-cachan.fr
# C5C4ACC0 vincent.thomas@crans.org
# F2530FCE pierre.chambart@crans.org
# D6ADFD5A carlos@crans.org
# 9AA56A6D dandrimont@crans.org
# 8E96ACDA adg@crans.org
# AF087A52 blockelet@crans.org
# 0BF3708E Xavier.Lagorce@crans.org
# E0DCF376 olivier.huber@crans.org
EOF)
gpg --armor --encrypt ${RECIPIENTS} ${TMP}
[[ "$#" == "0" ]] && ssh ${SRV} "sudo sh -c \"cat > ${REP}/${CHOIX}.asc\"" < ${TMP}.asc || echo -e "\033[1;31mErreur, abandon...\033[1;0m"
}
function droits () {
# modifie les droits du fichier chiffré
echo -e "\033[1;34mChangement des droits sur le fichier\033[1;0m"
ssh ${SRV} "sudo chmod 600 ${REP}/${CHOIX}.asc"
}
function edite () {
# édite le fichier temporaire
$EDITOR ${TMP}
}
function voir () {
# affiche le contenu du fichier temporaire
echo -e "\033[1;34mAffichage du fichier\033[1;0m"
less ${TMP}
}
function supprime () {
# supprime un fichier
echo -e "\033[1;34mSuppression du fichier chiffré : \033[1;0m${CHOIX}"
CONFIRM="Oui, je suis certain."
echo -n "Tapez \"$CONFIRM\" : "
read CONFIRM2
echo
if [[ "${CONFIRM}" == "${CONFIRM2}" ]] ; then
echo -e "\033[1;34mConnexion à ${SRV}\033[1;0m"
ssh ${SRV} -t "sudo -p 'Mot de passe sudo : ' echo -n" 2> /dev/null
echo
echo -e "\033[1;34mSuppression du fichier\033[1;0m"
ssh ${SRV} "sudo rm -f ${REP}/$CHOIX.asc 2> /dev/null"
echo -e "\033[1;32mFichier supprimé...\033[1;0m"
else
echo -e "\033[1;31mAbandon...\033[1;0m"
fi
}
##############################################################################
if [[ "$ACTION" == "--re-encrypt-all" ]] ; then
for CHOIX in `liste 2> /dev/null` ; do
echo -e "\033[1;33mTraitement de : \033[1;0m${CHOIX}"
echo
dechiffre
chiffre
droits
rm -f ${TMP} ${TMP}.asc
echo
done
elif [[ "$ACTION" == "--edit" ]] ; then
choix
dechiffre
edite
chiffre
droits
elif [[ "$ACTION" == "--remove" ]] ; then
choix
supprime
elif [[ "$ACTION" == "--view" ]] ; then
choix
dechiffre
voir
elif [[ "$ACTION" == "--list" ]] ; then
liste 2> /dev/null | cut -b 5-
else
echo "Usage : cranspasswords [--re-encrypt-all|--edit|--remove|--view|--list] [FICHIER]"
fi