freedombone-utils-gpg 12KB

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