gnano 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/bin/bash
  2. #GNANO - gpg-enabled nano
  3. #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)
  4. if [[ -z $1 ]]; then
  5. echo "FILE NOT SPECIFIED, EXITING!"
  6. exit 1
  7. fi
  8. #check program prerequisites:
  9. 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; }
  10. hash gpg 2>/dev/null || { echo >&2 "I require gpg but it's not installed. Aborting."; exit 1; }
  11. #define tempfile
  12. tempfile="/tmp/test2.temp"
  13. #define empty variables
  14. argument=""
  15. recipients=""
  16. new=0
  17. #preparing tempfile
  18. if [ -f $tempfile ] ; then
  19. rm $tempfile
  20. fi
  21. touch $tempfile
  22. chmod 600 $tempfile
  23. #manage options without getopts
  24. option=$1
  25. argument=$2
  26. if [ "$argument" = "" ]; then
  27. argument=$option
  28. fi
  29. #OPTIONS:
  30. if [[ $option = "--list" ]] || [[ $option = "-l" ]]; then
  31. echo "RECIPIENTS:"
  32. gpg --batch --list-only --no-default-keyring --secret-keyring /dev/null $argument
  33. exit 1
  34. fi
  35. if [[ $option = "--help" ]] || [[ $option = "-h" ]]; then
  36. echo "Usage: gnano [OPTIONS] FILE"
  37. echo ""
  38. echo "Option GNU long option Meaning"
  39. echo "-l --list Lists the recipients of the encrypted file"
  40. echo "-h --help Outputs this help"
  41. echo "-n --new Creates a new encrypted file"
  42. echo "no options giving only a filename the program decyphers the file, edits it, and recyphers it with the correct recipients"
  43. exit 1
  44. fi
  45. if [[ $option = "--new" ]] || [[ $option = "-n" ]]; then
  46. new=1
  47. echo "STARTING CREATION OF NEW FILE:"
  48. echo "your is the task to retrieve the keys or emails of the recipients, I can't do it"
  49. read -p "enter new filename: " argument
  50. if [ -f $argument ]; then
  51. echo "FILE EXISTS! EXITING!"
  52. exit 1
  53. fi
  54. read -p "enter recipients key IDs separated by commas: " newrecipients
  55. fi
  56. #LOOP FOR EXISTING FILE:
  57. if [[ $new -eq "0" ]]; then
  58. if [ ! -f $argument ]; then
  59. echo "FILE NOT FOUND, EXITING"
  60. exit 1
  61. fi
  62. cp $argument $argument"_BCK"
  63. gpg --batch --list-only --no-default-keyring --secret-keyring /dev/null $argument &> $tempfile
  64. cat $tempfile | grep "gpg: encrypted" | awk '{print $8}' | sed 's/\,//g' > $tempfile
  65. sleep 0.5
  66. while read recipient; do
  67. echo $recipient
  68. recipients+="-r $recipient "
  69. done <$tempfile
  70. if [[ $recipients = "" ]]; then
  71. echo "ERROR retrieving recipients, known issue, retry!"
  72. exit 1
  73. fi
  74. gpg -d $argument | EDITOR=nano vipe | gpg --batch --yes -e $recipients -o $argument
  75. fi
  76. #LOOP FOR NEW FILE:
  77. if [[ $new -eq "1" ]]; then
  78. for newrcpt in $(echo $newrecipients | tr "," "\n")
  79. do
  80. recipients+="-r $newrcpt "
  81. done
  82. nano $tempfile
  83. cat $tempfile | gpg --batch --yes -e $recipients -o $argument
  84. rm $tempfile
  85. echo ""
  86. echo "DONE!"
  87. exit 1
  88. fi
  89. #clearing tempfile:
  90. rm $tempfile