freedombone-encrypt-mail 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # GPG Encrypt a Maildir using gpgit.pl
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. USERNAME=$1
  31. PROJECT_NAME='freedombone'
  32. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  33. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  34. for f in $UTILS_FILES
  35. do
  36. source $f
  37. done
  38. ADMIN_USER=$(get_completion_param "Admin user")
  39. if [ ! $USERNAME ]; then
  40. USERNAME=$ADMIN_USER
  41. fi
  42. MAIL_DIR=/home/$USERNAME/Maildir
  43. EMAIL_ADDRESS=$USERNAME@$HOSTNAME
  44. # Does this key exist?
  45. gpg --list-keys "$EMAIL_ADDRESS" > /dev/null 2>&1
  46. if [ $? -gt 0 ]; then
  47. echo $"A GPG key for $EMAIL_ADDRESS could not be found!"
  48. exit 0
  49. fi
  50. # Find all files in the Maildir specified.
  51. echo $"Calling find"
  52. find "$MAIL_DIR" -type f -regex '.*/\(cur\|new\)/.*' $4|while read line; do
  53. gpgit.pl --encrypt-mode prefer-inline "$EMAIL_ADDRESS" "/tmp/msg_$USERNAME"
  54. # Check to see if there are differences between the existing
  55. # Maildir file and what was created by gpgit.pl
  56. diff -qa "$line" "/tmp/msg_$USERNAME" > /dev/null 2>&1;
  57. if [ $? -gt 0 ]; then
  58. # Preserve timestamps, set ownership.
  59. chown $USERNAME:$USERNAME "/tmp/msg_$USERNAME"
  60. chmod 600 "/tmp/msg_$USERNAME"
  61. touch "/tmp/msg_$USERNAME" --reference="$line"
  62. # Unlink the original Maildir message
  63. unlink "$line"
  64. # Strip message sizes, retain experimental flags
  65. # and status flags, and copy the file over.
  66. STRIPSIZES=$(/bin/echo "$line"|/bin/sed -e "s/W=[[:digit:]]*//" -e "s/S=[[:digit:]]*//" -e "s/,,//" -e "s/,:2/:2/")
  67. cp -av "/tmp/msg_$USERNAME" "$STRIPSIZES"
  68. #Indexes must be rebuilt, weve modified Maildir.
  69. touch "/tmp/rebuild_index_$USERNAME"
  70. else
  71. echo $"Not copying, no differences between /tmp/msg_$USERNAME and $line"
  72. fi
  73. # Remove the temporary file
  74. unlink "/tmp/msg_$USERNAME"
  75. done
  76. # Remove Dovecot index and uids for regeneration.
  77. if [ -f "/tmp/rebuild_index_$USERNAME" ]; then
  78. echo $"Removing Dovecot indexes and uids"
  79. find "$MAIL_DIR" -type f -regex '.*\(dovecot-\|dovecot\.\|\.uidvalidity\).*' -delete
  80. # Remove the temporary file
  81. unlink "/tmp/rebuild_index_$USERNAME"
  82. else
  83. echo -n $"No messages found needing GPG encryption, not"
  84. echo $"removing Dovecot indexes and UIDs."
  85. fi
  86. exit 0