gnano 2.8 KB

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