/encryptCalcurse.sh

https://bitbucket.org/oz123/encryptcalcurse · Shell · 113 lines · 59 code · 20 blank · 34 comment · 4 complexity · 793ae57405c6589f1936e105d116fa03 MD5 · raw file

  1. #!/bin/bash
  2. # ENCRYPTCALCURSE.SH
  3. # Written by Oz Nahum <nahumoz__at_you_know_where_no_spam_is_gmail.com>
  4. # This script is distributed under the terms of the GNU Public License
  5. # Version 3 or later.
  6. # You can obtaion copies of this license at:
  7. # http://www.gnu.org/licenses/gpl.html
  8. # A script to decrypt the calcurse_date dir, open it in
  9. # /home/<user>/calcurse_data
  10. # then launch calcurse pointing to it,
  11. # and upon closing calcurse, encrypt the data, move it to usb stick,
  12. # and delete all data from /home/<user>/calcurse_data
  13. ### Begin of Script
  14. INPUT_FILE="calcurse_d.tar.enc"
  15. OUTPUT_FILE="calcurse_d.tar.enc"
  16. #name of directory to encrypt (e.g. ~.calcurse)
  17. CALCURSE_DATA_DIR="~/.calcurse/"
  18. # usage:
  19. # $ bash encryptCalcurse.sh
  20. # $ bash encryptCalcurse.sh [ecnrypted_data_in.enc] [encrypted_data_out.enc]
  21. #TODO: test that modified script !
  22. ### Begin of Script
  23. #make files readable only by owner
  24. umask 077
  25. function Config {
  26. USB=`pwd`
  27. tar -cf calcurse_data.tar $CALCURSE_DATA_DIR
  28. openssl aes-256-cbc -salt -in calcurse_data.tar -out calcurse_d.tar.enc
  29. clc=`which calcurse`
  30. cp -v $clc $USB
  31. }
  32. function cleanUp {
  33. find /dev/shm/calcurse_data -type f | xargs shred -fuz;
  34. if [ -f /dev/shm/cdt.tar ]; then
  35. shred -fuz /dev/shm/cdt.tar
  36. fi
  37. if [ -f /dev/shm/calcurse_data_tmp.tar ]; then
  38. shred -fuz /dev/shm/calcurse_data_tmp.tar
  39. fi
  40. rmdir /dev/shm/calcurse_data/notes
  41. rmdir /dev/shm/calcurse_data
  42. }
  43. function readData {
  44. #first decrypt the data
  45. openssl enc -d -aes-256-cbc -salt -in $INPUT_FILE -out /dev/shm/calcurse_data_tmp.tar
  46. echo "extracting data"
  47. #silently extract data, no need for verbose output (v flag)
  48. tar -C /dev/shm -xf /dev/shm/calcurse_data_tmp.tar
  49. #note unpacking removes the original tar
  50. }
  51. function encryptData {
  52. openssl aes-256-cbc -salt -in /dev/shm/cdt.tar -out calcurse_d.tar.enc
  53. }
  54. case "$1" in
  55. "")
  56. echo "expecting parameter input... see header of script for usage"
  57. ;;
  58. "--config")
  59. CALCURSE_DATA_DIR=$2
  60. Config
  61. ;;
  62. "--read")
  63. trap "cleanUp" SIGHUP SIGINT SIGQUIT SIGKILL SIGABRT SIGTERM EXIT
  64. # when calcurse is done tar the direcotry
  65. readData
  66. calcurse -D /dev/shm/calcurse_data
  67. tar -cf /dev/shm/cdt.tar -C /dev/shm/ calcurse_data/
  68. # then encrypt
  69. # if encryption failed $? == 1 so repeat it again ...
  70. encryptData
  71. es=$?
  72. while [ "$es" = "1" ]; do
  73. echo "encrypting data"
  74. encryptData
  75. es=$?
  76. done
  77. ;;
  78. "--decrypt")
  79. readData
  80. ;;
  81. "--encrypt")
  82. tar -cvf /dev/shm/cdt.tar -C /dev/shm/ calcurse_data/
  83. encryptData
  84. ;;
  85. #if encryption succeeded clean up by calling the function
  86. #cleanUp
  87. esac
  88. #note about the salt option note found in openssl man page[1],[2]
  89. #note about lack of compresion with ssl [3]
  90. #sources:
  91. #[1]http://ubuntuforums.org/showpost.php?p=8287351&postcount=9
  92. #[2]http://linux.die.net/man/1/enc
  93. #[3]http://serverfault.com/questions/17855/can-i-compr:ess-an-encrypted-file