#!/bin/bash #GNANO - gpg-enabled nano #gnano enables you to edit a textfile encrypted for some recipients without outputting to cleartext the content (actually it outputs it in a tempfile which permissions are 600, I am working on yhis issue) if [[ -z $1 ]]; then echo "FILE NOT SPECIFIED, EXITING!" exit 1 fi #check program prerequisites: hash vipe 2>/dev/null || { echo >&2 "I require vipe but it's not installed. It's part of the package morutils, under debian-like systems: 'apt-get install moreutils' Aborting."; exit 1; } hash gpg 2>/dev/null || { echo >&2 "I require gpg but it's not installed. Aborting."; exit 1; } #define tempfile tempfile="/tmp/test2.temp" #define empty variables argument="" recipients="" new=0 #preparing tempfile if [ -f $tempfile ] ; then rm $tempfile fi touch $tempfile chmod 600 $tempfile #manage options without getopts option=$1 argument=$2 if [ "$argument" = "" ]; then argument=$option fi #OPTIONS: if [[ $option = "--list" ]] || [[ $option = "-l" ]]; then echo "RECIPIENTS:" gpg --batch --list-only --no-default-keyring --secret-keyring /dev/null $argument exit 1 fi if [[ $option = "--help" ]] || [[ $option = "-h" ]]; then echo "Usage: gnano [OPTIONS] FILE" echo "" echo "Option GNU long option Meaning" echo "-l --list Lists the recipients of the encrypted file" echo "-h --help Outputs this help" echo "-n --new Creates a new encrypted file" echo "no options giving only a filename the program decyphers the file, edits it, and recyphers it with the correct recipients" exit 1 fi if [[ $option = "--new" ]] || [[ $option = "-n" ]]; then new=1 echo "STARTING CREATION OF NEW FILE:" echo "your is the task to retrieve the keys or emails of the recipients, I can't do it" read -p "enter new filename: " argument if [ -f $argument ]; then echo "FILE EXISTS! EXITING!" exit 1 fi read -p "enter recipients key IDs separated by commas: " newrecipients fi #LOOP FOR EXISTING FILE: if [[ $new -eq "0" ]]; then if [ ! -f $argument ]; then echo "FILE NOT FOUND, EXITING" exit 1 fi cp $argument $argument"_BCK" gpg --batch --list-only --no-default-keyring --secret-keyring /dev/null $argument &> $tempfile cat $tempfile | grep "gpg: encrypted" | awk '{print $8}' | sed 's/\,//g' > $tempfile sleep 0.5 while read recipient; do echo $recipient recipients+="-r $recipient " done <$tempfile if [[ $recipients = "" ]]; then echo "ERROR retrieving recipients, known issue, retry!" exit 1 fi gpg -d $argument | EDITOR=nano vipe | gpg --batch --yes -e $recipients -o $argument fi #LOOP FOR NEW FILE: if [[ $new -eq "1" ]]; then for newrcpt in $(echo $newrecipients | tr "," "\n") do recipients+="-r $newrcpt " done nano $tempfile cat $tempfile | gpg --batch --yes -e $recipients -o $argument rm $tempfile echo "" echo "DONE!" exit 1 fi #clearing tempfile: rm $tempfile