freedombone-utils-gpg 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # gpg functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2016-2017 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. function gpg_update_mutt {
  31. key_username=$1
  32. if [ ! -f /home/$key_username/.muttrc ]; then
  33. return
  34. fi
  35. CURR_EMAIL_ADDRESS=$key_username@$HOSTNAME
  36. CURR_GPG_ID=$(gpg --homedir=/home/$key_username/.gnupg --list-keys $CURR_EMAIL_ADDRESS | sed -n '2p' | sed 's/^[ \t]*//')
  37. sed -i "s|set pgp_encrypt_only_command.*|set pgp_encrypt_only_command=\"/usr/lib/mutt/pgpewrap gpg --batch --quiet --no-verbose --output - --encrypt --textmode --armor --trust-model always --encrypt-to $CURR_GPG_ID -- -r %r -- %f\"|g" /home/$key_username/.muttrc
  38. sed -i "s|set pgp_encrypt_sign_command.*|set pgp_encrypt_sign_command=\"/usr/lib/mutt/pgpewrap gpg %?p?--passphrase-fd 0? --batch --quiet --no-verbose --textmode --output - --encrypt --sign %?a?-u %a? --armor --trust-model always --encrypt-to $CURR_GPG_ID -- -r %r -- %f\"|g" /home/$key_username/.muttrc
  39. chown $key_username:$key_username /home/$key_username/.muttrc
  40. }
  41. function gpg_import_public_key {
  42. key_username=$1
  43. key_filename=$2
  44. gpg --homedir=/home/$key_username/.gnupg --import $key_filename
  45. gpg_set_permissions $key_username
  46. }
  47. function gpg_import_private_key {
  48. key_username=$1
  49. key_filename=$2
  50. gpg --homedir=/home/$key_username/.gnupg --allow-secret-key-import --import $key_filename
  51. gpg_set_permissions $key_username
  52. }
  53. function gpg_export_public_key {
  54. key_username=$1
  55. key_id=$2
  56. key_filename=$3
  57. su -m root -c "gpg --homedir /home/$key_username/.gnupg --output $key_filename --armor --export $key_id" - $key_username
  58. }
  59. function gpg_export_private_key {
  60. key_username=$1
  61. key_id=$2
  62. key_filename=$3
  63. su -m root -c "gpg --homedir=/home/$key_username/.gnupg --armor --output $key_filename --export-secret-key $key_id" - $key_username
  64. }
  65. function gpg_create_key {
  66. key_username=$1
  67. key_passphrase=$2
  68. gpg_dir=/home/$key_username/.gnupg
  69. echo 'Key-Type: eddsa' > /home/$key_username/gpg-genkey.conf
  70. echo 'Key-Curve: Ed25519' >> /home/$key_username/gpg-genkey.conf
  71. echo 'Subkey-Type: eddsa' >> /home/$key_username/gpg-genkey.conf
  72. echo 'Subkey-Curve: Ed25519' >> /home/$key_username/gpg-genkey.conf
  73. echo "Name-Real: $MY_NAME" >> /home/$key_username/gpg-genkey.conf
  74. echo "Name-Email: $MY_EMAIL_ADDRESS" >> /home/$key_username/gpg-genkey.conf
  75. echo 'Expire-Date: 0' >> /home/$key_username/gpg-genkey.conf
  76. cat /home/$key_username/gpg-genkey.conf
  77. if [ $key_passphrase ]; then
  78. echo "Passphrase: $key_passphrase" >> /home/$key_username/gpg-genkey.conf
  79. else
  80. echo "Passphrase: $PROJECT_NAME" >> /home/$key_username/gpg-genkey.conf
  81. fi
  82. chown $key_username:$key_username /home/$key_username/gpg-genkey.conf
  83. echo $'Generating a new GPG key'
  84. su -m root -c "gpg --homedir /home/$key_username/.gnupg --batch --full-gen-key /home/$key_username/gpg-genkey.conf" - $key_username
  85. chown -R $key_username:$key_username /home/$key_username/.gnupg
  86. KEY_EXISTS=$(gpg_key_exists "$key_username" "${key_username}@${HOSTNAME}")
  87. if [[ $KEY_EXISTS == "no" ]]; then
  88. echo $"A GPG key for ${key_username}@${HOSTNAME} could not be created"
  89. exit 63621
  90. fi
  91. shred -zu /home/$key_username/gpg-genkey.conf
  92. CURR_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$key_username" "${key_username}@${HOSTNAME}")
  93. if [ ${#CURR_GPG_PUBLIC_KEY_ID} -lt 4 ]; then
  94. echo $"GPG public key ID could not be obtained for ${key_username}@${HOSTNAME}"
  95. exit 825292
  96. fi
  97. gpg_set_permissions $key_username
  98. }
  99. function gpg_delete_key {
  100. key_username=$1
  101. key_id=$2
  102. chown -R $key_username:$key_username /home/$key_username/.gnupg
  103. su -c "gpg --batch --quiet --homedir=/home/$key_username/.gnupg --delete-secret-key $key_id" - $key_username
  104. su -c "gpg --batch --quiet --homedir=/home/$key_username/.gnupg --delete-key $key_id" - $key_username
  105. }
  106. function gpg_set_permissions {
  107. key_username=$1
  108. if [[ "$key_username" != 'root' ]]; then
  109. chmod 700 /home/$key_username/.gnupg
  110. chmod -R 600 /home/$key_username/.gnupg/*
  111. chown -R $key_username:$key_username /home/$key_username/.gnupg
  112. else
  113. chmod 700 /root/.gnupg
  114. chmod -R 600 /root/.gnupg/*
  115. chown -R $key_username:$key_username /root/.gnupg
  116. fi
  117. }
  118. function gpg_reconstruct_key {
  119. key_username=$1
  120. key_interactive=$2
  121. if [ ! -d /home/$key_username/.gnupg_fragments ]; then
  122. return
  123. fi
  124. cd /home/$key_username/.gnupg_fragments
  125. no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
  126. if (( no_of_shares < 4 )); then
  127. if [ $key_interactive ]; then
  128. dialog --title $"Recover Encryption Keys" --msgbox $'Not enough fragments to reconstruct the key' 6 70
  129. else
  130. echo $'Not enough fragments to reconstruct the key'
  131. fi
  132. exit 7348
  133. fi
  134. gfcombine /home/$key_username/.gnupg_fragments/keyshare*
  135. if [ ! "$?" = "0" ]; then
  136. if [ $key_interactive ]; then
  137. dialog --title $"Recover Encryption Keys" --msgbox $'Unable to reconstruct the key' 6 70
  138. else
  139. echo $'Unable to reconstruct the key'
  140. fi
  141. exit 7348
  142. fi
  143. KEYS_FILE=/home/$key_username/.gnupg_fragments/keyshare.asc
  144. if [ ! -f $KEYS_FILE ]; then
  145. if [ $key_interactive ]; then
  146. dialog --title $"Recover Encryption Keys" --msgbox $'Unable to reconstruct the key' 6 70
  147. else
  148. echo $'Unable to reconstruct the key'
  149. fi
  150. exit 52852
  151. fi
  152. gpg --homedir=/home/$key_username/.gnupg --allow-secret-key-import --import $KEYS_FILE
  153. if [ ! "$?" = "0" ]; then
  154. shred -zu $KEYS_FILE
  155. rm -rf /home/$key_username/.tempgnupg
  156. if [ $key_interactive ]; then
  157. dialog --title $"Recover Encryption Keys" --msgbox $'Unable to import gpg key' 6 70
  158. else
  159. echo $'Unable to import gpg key'
  160. fi
  161. exit 96547
  162. fi
  163. shred -zu $KEYS_FILE
  164. gpg_set_permissions $key_username
  165. if [ $key_interactive ]; then
  166. dialog --title $"Recover Encryption Keys" --msgbox $'Key has been reconstructed' 6 70
  167. else
  168. echo $'Key has been reconstructed'
  169. fi
  170. }
  171. function gpg_agent_setup {
  172. gpg_username=$1
  173. if [[ $gpg_username == 'root' ]]; then
  174. if ! grep -q 'GPG_TTY' /root/.bashrc; then
  175. echo '' >> /root/.bashrc
  176. echo 'GPG_TTY=$(tty)' >> /root/.bashrc
  177. echo 'export GPG_TTY' >> /root/.bashrc
  178. fi
  179. if ! grep -q 'use-agent' /root/.gnupg/gpg.conf; then
  180. echo 'use-agent' >> /root/.gnupg/gpg.conf
  181. fi
  182. if ! grep -q 'pinentry-mode loopback' /root/.gnupg/gpg.conf; then
  183. echo 'pinentry-mode loopback' >> /root/.gnupg/gpg.conf
  184. fi
  185. if [ ! -f /root/.gnupg/gpg-agent.conf ]; then
  186. touch /root/.gnupg/gpg-agent.conf
  187. fi
  188. if ! grep -q 'allow-loopback-pinentry' /root/.gnupg/gpg-agent.conf; then
  189. echo 'allow-loopback-pinentry' >> /root/.gnupg/gpg-agent.conf
  190. fi
  191. echo RELOADAGENT | gpg-connect-agent
  192. else
  193. if ! grep -q 'GPG_TTY' /home/$gpg_username/.bashrc; then
  194. echo '' >> /home/$gpg_username/.bashrc
  195. echo 'GPG_TTY=$(tty)' >> /home/$gpg_username/.bashrc
  196. echo 'export GPG_TTY' >> /home/$gpg_username/.bashrc
  197. chown $gpg_username:$gpg_username /home/$gpg_username/.bashrc
  198. fi
  199. if ! grep -q 'use-agent' /home/$gpg_username/.gnupg/gpg.conf; then
  200. echo 'use-agent' >> /home/$gpg_username/.gnupg/gpg.conf
  201. fi
  202. if ! grep -q 'pinentry-mode loopback' /home/$gpg_username/.gnupg/gpg.conf; then
  203. echo 'pinentry-mode loopback' >> /home/$gpg_username/.gnupg/gpg.conf
  204. fi
  205. if [ ! -f /home/$gpg_username/.gnupg/gpg-agent.conf ]; then
  206. touch /home/$gpg_username/.gnupg/gpg-agent.conf
  207. fi
  208. if ! grep -q 'allow-loopback-pinentry' /home/$gpg_username/.gnupg/gpg-agent.conf; then
  209. echo 'allow-loopback-pinentry' >> /home/$gpg_username/.gnupg/gpg-agent.conf
  210. fi
  211. su -c "echo RELOADAGENT | gpg-connect-agent" - $gpg_username
  212. fi
  213. }
  214. function gpg_pubkey_from_email {
  215. key_owner_username=$1
  216. key_email_address=$2
  217. key_id=
  218. if [[ $key_owner_username != "root" ]]; then
  219. key_id=$(su -c "gpg --list-keys $key_email_address" - $key_owner_username | sed -n '2p' | sed 's/^[ \t]*//')
  220. else
  221. key_id=$(gpg --list-keys $key_email_address | sed -n '2p' | sed 's/^[ \t]*//')
  222. fi
  223. echo $key_id
  224. }
  225. function enable_email_encryption_at_rest {
  226. for d in /home/*/ ; do
  227. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  228. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  229. if grep '#| /usr/bin/gpgit.pl' /home/$USERNAME/.procmailrc; then
  230. sed -i 's@#| /usr/bin/gpgit.pl@| /usr/bin/gpgit.pl@g' /home/$USERNAME/.procmailrc
  231. sed -i 's|#:0 f|:0 f|g' /home/$USERNAME/.procmailrc
  232. fi
  233. fi
  234. done
  235. if grep '#| /usr/bin/gpgit.pl' /etc/skel/.procmailrc; then
  236. sed -i 's@#| /usr/bin/gpgit.pl@| /usr/bin/gpgit.pl@g' /etc/skel/.procmailrc
  237. sed -i 's|#:0 f|:0 f|g' /etc/skel/.procmailrc
  238. fi
  239. }
  240. function disable_email_encryption_at_rest {
  241. for d in /home/*/ ; do
  242. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  243. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  244. if ! grep '#| /usr/bin/gpgit.pl' /home/$USERNAME/.procmailrc; then
  245. sed -i 's@| /usr/bin/gpgit.pl@#| /usr/bin/gpgit.pl@g' /home/$USERNAME/.procmailrc
  246. sed -i 's|:0 f|#:0 f|g' /home/$USERNAME/.procmailrc
  247. fi
  248. fi
  249. done
  250. if ! grep '#| /usr/bin/gpgit.pl' /etc/skel/.procmailrc; then
  251. sed -i 's@| /usr/bin/gpgit.pl@#| /usr/bin/gpgit.pl@g' /etc/skel/.procmailrc
  252. sed -i 's|:0 f|#:0 f|g' /etc/skel/.procmailrc
  253. fi
  254. }
  255. # NOTE: deliberately no exit 0