freedombone-base-email 57KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Email functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2018 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. # the default email address
  31. MY_EMAIL_ADDRESS=$MY_USERNAME@$DEFAULT_DOMAIN_NAME
  32. # If you want to run a public mailing list specify its name here.
  33. # There should be no spaces in the name
  34. PUBLIC_MAILING_LIST=
  35. # Optional different domain name for the public mailing list
  36. PUBLIC_MAILING_LIST_DOMAIN_NAME=
  37. # Directory where the public mailing list data is stored
  38. PUBLIC_MAILING_LIST_DIRECTORY="/var/spool/mlmmj"
  39. # If you want to run an encrypted mailing list specify its name here.
  40. # There should be no spaces in the name
  41. PRIVATE_MAILING_LIST=
  42. GPG_KEYSERVER="hkp://keys.gnupg.net"
  43. # whether to encrypt all incoming email with your public key
  44. GPG_ENCRYPT_STORED_EMAIL="yes"
  45. # optionally you can provide your exported GPG key pair here
  46. # Note that the private key file will be deleted after use
  47. # If these are unspecified then a new GPG key will be created
  48. MY_GPG_PUBLIC_KEY=
  49. MY_GPG_PRIVATE_KEY=
  50. # optionally specify your public key ID
  51. MY_GPG_PUBLIC_KEY_ID=
  52. EXIM_ONION_REPO="https://github.com/petterreinholdtsen/exim4-smtorp"
  53. # automatic archiving of email
  54. CLEANUP_MAILDIR_REPO="https://github.com/bashrc/cleanup-maildir"
  55. CLEANUP_MAILDIR_COMMIT='33241d2e3861f901ba17f5c77ada007e1ec06a86'
  56. # email encryption at rest
  57. GPGIT_REPO="https://gitlab.com/mikecardwell/gpgit"
  58. GPGIT_COMMIT='583dc76119f19420f8a33f606744faa7c8922738'
  59. # refresh gpg keys every few hours
  60. REFRESH_GPG_KEYS_HOURS=2
  61. function email_create_template {
  62. if [ ! -d /etc/skel/log ]; then
  63. mkdir -m 700 /etc/skel/log
  64. fi
  65. if [ ! -d /etc/skel/Maildir ]; then
  66. mkdir -m 700 /etc/skel/.mutt
  67. mkdir -m 700 /etc/skel/Maildir
  68. mkdir -m 700 /etc/skel/Maildir/new
  69. mkdir -m 700 /etc/skel/Maildir/cur
  70. mkdir -m 700 /etc/skel/Maildir/Sent
  71. mkdir -m 700 /etc/skel/Maildir/Sent/tmp
  72. mkdir -m 700 /etc/skel/Maildir/Sent/cur
  73. mkdir -m 700 /etc/skel/Maildir/Sent/new
  74. mkdir -m 700 /etc/skel/Maildir/.learn-spam
  75. mkdir -m 700 /etc/skel/Maildir/.learn-spam/cur
  76. mkdir -m 700 /etc/skel/Maildir/.learn-spam/new
  77. mkdir -m 700 /etc/skel/Maildir/.learn-spam/tmp
  78. mkdir -m 700 /etc/skel/Maildir/.learn-ham
  79. mkdir -m 700 /etc/skel/Maildir/.learn-ham/cur
  80. mkdir -m 700 /etc/skel/Maildir/.learn-ham/new
  81. mkdir -m 700 /etc/skel/Maildir/.learn-ham/tmp
  82. ln -s /etc/skel/Maildir/.learn-spam /etc/skel/Maildir/spam
  83. ln -s /etc/skel/Maildir/.learn-ham /etc/skel/Maildir/ham
  84. fi
  85. if [ ! -d "/home/$MY_USERNAME/Maildir" ]; then
  86. mkdir -m 700 "/home/$MY_USERNAME/.mutt"
  87. mkdir -m 700 "/home/$MY_USERNAME/Maildir"
  88. mkdir -m 700 "/home/$MY_USERNAME/Maildir/cur"
  89. mkdir -m 700 "/home/$MY_USERNAME/Maildir/tmp"
  90. mkdir -m 700 "/home/$MY_USERNAME/Maildir/new"
  91. mkdir -m 700 "/home/$MY_USERNAME/Maildir/Sent"
  92. mkdir -m 700 "/home/$MY_USERNAME/Maildir/Sent/cur"
  93. mkdir -m 700 "/home/$MY_USERNAME/Maildir/Sent/tmp"
  94. mkdir -m 700 "/home/$MY_USERNAME/Maildir/Sent/new"
  95. mkdir -m 700 "/home/$MY_USERNAME/Maildir/.learn-spam"
  96. mkdir -m 700 "/home/$MY_USERNAME/Maildir/.learn-spam/cur"
  97. mkdir -m 700 "/home/$MY_USERNAME/Maildir/.learn-spam/new"
  98. mkdir -m 700 "/home/$MY_USERNAME/Maildir/.learn-spam/tmp"
  99. mkdir -m 700 "/home/$MY_USERNAME/Maildir/.learn-ham"
  100. mkdir -m 700 "/home/$MY_USERNAME/Maildir/.learn-ham/cur"
  101. mkdir -m 700 "/home/$MY_USERNAME/Maildir/.learn-ham/new"
  102. mkdir -m 700 "/home/$MY_USERNAME/Maildir/.learn-ham/tmp"
  103. ln -s "/home/$MY_USERNAME/Maildir/.learn-spam" "/home/$MY_USERNAME/Maildir/spam"
  104. ln -s "/home/$MY_USERNAME/Maildir/.learn-ham" "/home/$MY_USERNAME/Maildir/ham"
  105. chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/Maildir"
  106. fi
  107. }
  108. function configure_email_onion {
  109. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  110. return
  111. fi
  112. if [[ "$SYSTEM_TYPE" == "mesh"* ]]; then
  113. return
  114. fi
  115. if ! grep -q "hidden_service_email" /etc/tor/torrc; then
  116. { echo 'HiddenServiceDir /var/lib/tor/hidden_service_email/';
  117. echo 'HiddenServicePort 25 127.0.0.1:25';
  118. echo 'HiddenServicePort 587 127.0.0.1:587';
  119. echo 'HiddenServicePort 465 127.0.0.1:465'; } >> /etc/tor/torrc
  120. fi
  121. function_check onion_update
  122. onion_update
  123. function_check wait_for_onion_service
  124. wait_for_onion_service email
  125. if [ ! -f /var/lib/tor/hidden_service_email/hostname ]; then
  126. echo $"email onion site hostname not found"
  127. systemctl restart tor
  128. exit 782352
  129. fi
  130. onion_address=$(cat /var/lib/tor/hidden_service_email/hostname)
  131. set_completion_param "email onion domain" "${onion_address}"
  132. add_email_hostname "$onion_address"
  133. mark_completed "${FUNCNAME[0]}"
  134. }
  135. function check_email_address_exists {
  136. read_config_param ONION_ONLY
  137. read_config_param MY_USERNAME
  138. read_config_param DEFAULT_DOMAIN_NAME
  139. read_config_param MY_EMAIL_ADDRESS
  140. read_config_param DH_KEYLENGTH
  141. if [ ! "$MY_USERNAME" ]; then
  142. echo $'No username for email installation'
  143. exit 73672
  144. fi
  145. if [ ! "$DEFAULT_DOMAIN_NAME" ]; then
  146. echo $'No default domain name for email installation'
  147. exit 57634
  148. fi
  149. my_email="$MY_EMAIL_ADDRESS"
  150. if [ ${#my_email} -lt 3 ]; then
  151. MY_EMAIL_ADDRESS="${MY_USERNAME}@${DEFAULT_DOMAIN_NAME}"
  152. write_config_param "MY_EMAIL_ADDRESS" "$MY_EMAIL_ADDRESS"
  153. fi
  154. }
  155. function backup_email {
  156. echo ''
  157. }
  158. function configure_firewall_for_email {
  159. if [[ "$INSTALLED_WITHIN_DOCKER" == "yes" ]]; then
  160. # docker does its own firewalling
  161. return
  162. fi
  163. if [[ "$ONION_ONLY" != "no" ]]; then
  164. return
  165. fi
  166. firewall_add Email 25 tcp
  167. firewall_add Email 587 tcp
  168. firewall_add Email 465 tcp
  169. firewall_add Imap 993 tcp
  170. }
  171. function encrypt_incoming_email {
  172. # encrypts incoming mail using your GPG public key
  173. # so even if an attacker gains access to the data at rest they still need
  174. # to know your GPG key password to be able to read anything
  175. if [ ! -d /etc/exim4 ]; then
  176. return
  177. fi
  178. # update to the next commit
  179. function_check set_repo_commit
  180. set_repo_commit "$INSTALL_DIR/gpgit" "gpgit commit" "$GPGIT_COMMIT" "$GPGIT_REPO"
  181. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  182. return
  183. fi
  184. if [[ "$GPG_ENCRYPT_STORED_EMAIL" != "yes" ]]; then
  185. return
  186. fi
  187. if [ ! -f /usr/bin/gpgit.pl ]; then
  188. apt-get -yq install git libmail-gnupg-perl
  189. cd "$INSTALL_DIR" || exit 246824624
  190. function_check git_clone
  191. git_clone "$GPGIT_REPO" "$INSTALL_DIR/gpgit"
  192. cd "$INSTALL_DIR/gpgit" || exit 7246725474
  193. git checkout "$GPGIT_COMMIT" -b "$GPGIT_COMMIT"
  194. set_completion_param "gpgit commit" "$GPGIT_COMMIT"
  195. cp gpgit.pl /usr/bin
  196. fi
  197. # add a procmail rule
  198. if ! grep -q "/usr/bin/gpgit.pl" "/home/$MY_USERNAME/.procmailrc"; then
  199. { echo '';
  200. echo ':0 f';
  201. echo "| /usr/bin/gpgit.pl --encrypt-mode prefer-inline --inline-flatten $MY_EMAIL_ADDRESS"; } >> "/home/$MY_USERNAME/.procmailrc"
  202. chown "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.procmailrc"
  203. { echo '';
  204. echo ':0 f';
  205. echo -n "| /usr/bin/gpgit.pl --encrypt-mode prefer-inline --inline-flatten \$USER@";
  206. echo "$DEFAULT_DOMAIN_NAME"; } >> /etc/skel/.procmailrc
  207. fi
  208. mark_completed "${FUNCNAME[0]}"
  209. }
  210. function encrypt_outgoing_email {
  211. # encrypts outgoing mail using your GPG public key
  212. # so even if an attacker gains access to the data at rest they still need
  213. # to know your GPG key password to be able to read sent mail
  214. if [ ! -d /etc/exim4 ]; then
  215. return
  216. fi
  217. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  218. return
  219. fi
  220. if [[ "$GPG_ENCRYPT_STORED_EMAIL" != "yes" ]]; then
  221. return
  222. fi
  223. if [ ! -d "/home/$MY_USERNAME/.gnupg" ]; then
  224. return
  225. fi
  226. if [ ! -f "/home/$MY_USERNAME/.muttrc" ]; then
  227. return
  228. fi
  229. # obtain your public key ID
  230. if [ ! "$MY_GPG_PUBLIC_KEY_ID" ]; then
  231. MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$MY_USERNAME" "$MY_EMAIL_ADDRESS")
  232. if [ ! "$MY_GPG_PUBLIC_KEY_ID" ]; then
  233. return
  234. fi
  235. if [ ${#MY_GPG_PUBLIC_KEY_ID} -lt 4 ]; then
  236. return
  237. fi
  238. fi
  239. if ! grep -q "pgp_encrypt_only_command" "/home/$MY_USERNAME/.muttrc"; then
  240. { echo '';
  241. echo $'# Encrypt items in the Sent folder';
  242. echo "set pgp_encrypt_only_command=\"/usr/lib/mutt/pgpewrap gpg --batch --quiet --no-verbose --output - --encrypt --textmode --armor --trust-model always --encrypt-to $MY_GPG_PUBLIC_KEY_ID -- -r %r -- %f\""; } >> "/home/$MY_USERNAME/.muttrc"
  243. else
  244. 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 $MY_GPG_PUBLIC_KEY_ID -- -r %r -- %f\"|g" "/home/$MY_USERNAME/.muttrc"
  245. fi
  246. if ! grep -q "pgp_encrypt_sign_command" "/home/$MY_USERNAME/.muttrc"; then
  247. echo "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 $MY_GPG_PUBLIC_KEY_ID -- -r %r -- %f\"" >> "/home/$MY_USERNAME/.muttrc"
  248. else
  249. 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 $MY_GPG_PUBLIC_KEY_ID -- -r %r -- %f\"|g" "/home/$MY_USERNAME/.muttrc"
  250. fi
  251. mark_completed "${FUNCNAME[0]}"
  252. }
  253. function encrypt_all_email {
  254. if [ ! -d /etc/exim4 ]; then
  255. return
  256. fi
  257. if [[ "$GPG_ENCRYPT_STORED_EMAIL" != "yes" ]]; then
  258. return
  259. fi
  260. if [ -f "/usr/local/bin/${PROJECT_NAME}-encrypt-mail" ]; then
  261. if [ ! -f /usr/bin/encmaildir ]; then
  262. cp "/usr/local/bin/${PROJECT_NAME}-encrypt-mail" /usr/bin/encmaildir
  263. else
  264. HASH1=$(sha256sum "/usr/local/bin/${PROJECT_NAME}-encrypt-mail" | awk -F ' ' '{print $1}')
  265. HASH2=$(sha256sum /usr/bin/encmaildir | awk -F ' ' '{print $1}')
  266. if [[ "$HASH1" != "$HASH2" ]]; then
  267. cp "/usr/local/bin/${PROJECT_NAME}-encrypt-mail" /usr/bin/encmaildir
  268. fi
  269. fi
  270. else
  271. if [ ! -f /usr/bin/encmaildir ]; then
  272. cp "/usr/bin/${PROJECT_NAME}-encrypt-mail" /usr/bin/encmaildir
  273. else
  274. HASH1=$(sha256sum "/usr/bin/${PROJECT_NAME}-encrypt-mail" | awk -F ' ' '{print $1}')
  275. HASH2=$(sha256sum /usr/bin/encmaildir | awk -F ' ' '{print $1}')
  276. if [[ "$HASH1" != "$HASH2" ]]; then
  277. cp "/usr/bin/${PROJECT_NAME}-encrypt-mail" /usr/bin/encmaildir
  278. fi
  279. fi
  280. fi
  281. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  282. return
  283. fi
  284. if [ ! -f "/home/$MY_USERNAME/README" ]; then
  285. touch "/home/$MY_USERNAME/README"
  286. fi
  287. if ! grep -q $"If you have imported legacy email which is not encrypted" "/home/$MY_USERNAME/README"; then
  288. { echo '';
  289. echo '';
  290. echo $'# Encrypting legacy email';
  291. echo $'If you have imported legacy email which is not encrypted';
  292. echo $'then it can be encrypted with the command:';
  293. echo '';
  294. echo ' encmaildir';
  295. echo '';
  296. echo $'But be warned that depending upon how much email you have';
  297. echo $'this could take a seriously LONG time on the Beaglebone';
  298. echo $'and may be better done on a faster machine.'; } >> "/home/$MY_USERNAME/README"
  299. chown "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/README"
  300. chmod 600 "/home/$MY_USERNAME/README"
  301. fi
  302. mark_completed "${FUNCNAME[0]}"
  303. }
  304. function email_client {
  305. if [ ! -d /etc/exim4 ]; then
  306. return
  307. fi
  308. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  309. return
  310. fi
  311. apt-get -yq install lynx abook urlview mutt
  312. if [ ! -f /etc/Muttrc ]; then
  313. echo $"ERROR: Mutt does not appear to have installed. $CHECK_MESSAGE"
  314. exit 49
  315. fi
  316. if [ ! -d "/home/$MY_USERNAME/.mutt" ]; then
  317. mkdir "/home/$MY_USERNAME/.mutt"
  318. fi
  319. echo "text/html; lynx -dump -width=78 -nolist %s | sed ‘s/^ //’; copiousoutput; needsterminal; nametemplate=%s.html" > "/home/$MY_USERNAME/.mutt/mailcap"
  320. cp "/home/$MY_USERNAME/.mutt/mailcap" /etc/skel/.mutt
  321. chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.mutt"
  322. chown -R root:root /etc/skel/.mutt
  323. { echo 'set mbox_type=Maildir';
  324. echo 'set folder="~/Maildir"';
  325. echo 'set mask="!^\\.[^.]"';
  326. echo 'set mbox="~/Maildir"';
  327. echo 'set record="+Sent"';
  328. echo 'set postponed="+Drafts"';
  329. echo 'set trash="+Trash"';
  330. echo 'set spoolfile="~/Maildir"';
  331. echo 'auto_view text/x-vcard text/html text/enriched';
  332. echo 'set header_cache="+.cache"';
  333. echo 'set markers=no';
  334. echo '';
  335. echo '# ctrl-u to view long URLs';
  336. echo 'macro pager \cu <pipe-entry>"urlview"<enter> "Follow links with urlview"';
  337. echo '';
  338. echo 'macro index S "<tag-prefix><decode-save>=.learn-spam<enter>" "move to learn-spam"';
  339. echo 'macro pager S "<decode-save>=.learn-spam<enter>" "move to learn-spam"';
  340. echo 'macro index H "<tag-prefix><decode-copy>=.learn-ham<enter>" "copy to learn-ham"';
  341. echo 'macro pager H "<decode-copy>=.learn-ham<enter>" "copy to learn-ham"';
  342. echo '';
  343. echo '# set up the sidebar';
  344. echo 'set sidebar_width=22';
  345. echo 'set sidebar_visible=yes';
  346. echo '';
  347. echo 'set rfc2047_parameters';
  348. echo '';
  349. echo '# Show inbox and sent items';
  350. echo 'mailboxes = =admin =Sent =maybe-spam =spam';
  351. echo '';
  352. echo '# Alter these colours as needed for maximum bling';
  353. echo 'color sidebar_new yellow default';
  354. echo 'color normal white default';
  355. echo 'color hdrdefault brightcyan default';
  356. echo 'color signature green default';
  357. echo 'color attachment brightyellow default';
  358. echo 'color quoted green default';
  359. echo 'color quoted1 white default';
  360. echo 'color tilde blue default';
  361. echo '';
  362. echo '# ctrl-n, ctrl-p to select next, prev folder';
  363. echo '# ctrl-o to open selected folder';
  364. echo 'bind index \Cp sidebar-prev';
  365. echo 'bind index \Cn sidebar-next';
  366. echo 'bind index \Co sidebar-open';
  367. echo 'bind pager \Cp sidebar-prev';
  368. echo 'bind pager \Cn sidebar-next';
  369. echo 'bind pager \Co sidebar-open';
  370. echo '';
  371. echo '# ctrl-b toggles sidebar visibility';
  372. echo "macro index,pager \\Cb '<enter-command>toggle sidebar_visible<enter><redraw-screen>' 'toggle sidebar'";
  373. echo '';
  374. echo '# esc-m Mark new messages as read';
  375. echo 'macro index <esc>m "T~N<enter>;WNT~O<enter>;WO\CT~T<enter>" "mark all messages read"';
  376. echo '';
  377. echo '# Collapsing threads';
  378. echo 'macro index [ "<collapse-thread>" "collapse/uncollapse thread"';
  379. echo 'macro index ] "<collapse-all>" "collapse/uncollapse all threads"';
  380. echo '';
  381. echo '# threads containing new messages';
  382. echo 'uncolor index "~(~N)"';
  383. echo 'color index brightblue default "~(~N)"';
  384. echo '';
  385. echo '# new messages themselves';
  386. echo 'uncolor index "~N"';
  387. echo 'color index brightyellow default "~N"';
  388. echo '';
  389. echo '# GPG/PGP integration';
  390. echo '# this set the number of seconds to keep in memory the passphrase used to encrypt/sign';
  391. echo 'set pgp_timeout=1800';
  392. echo '';
  393. echo '# automatically sign and encrypt with PGP/MIME';
  394. echo 'set pgp_autosign # autosign all outgoing mails';
  395. echo 'set pgp_autoencrypt # Try to encrypt automatically';
  396. echo 'set pgp_replyencrypt # autocrypt replies to crypted';
  397. echo 'set pgp_replysign # autosign replies to signed';
  398. echo 'set pgp_auto_decode=yes # decode attachments';
  399. echo 'set fcc_clear=no # Keep encrypted copy of sent encrypted mail';
  400. echo 'unset smime_is_default';
  401. echo '';
  402. echo 'set alias_file=~/.mutt-alias';
  403. echo 'source ~/.mutt-alias';
  404. echo 'set query_command= "abook --mutt-query \"%s\""';
  405. echo 'macro index,pager A "<pipe-message>abook --add-email-quiet<return>" "add the sender address to abook"';
  406. echo '';
  407. echo '# Optional relay of SMTP via ISP';
  408. echo '#set smtp_url="smtps://username:password@isp_mail_domain:465/"'; } > /etc/Muttrc
  409. # For viewing long URLs
  410. echo 'REGEXP (((http|https|ftp|gopher)|mailto)[.:][^ >"\t]*|www\.[-a-z0-9.]+)[^ .,;\t>">\):]' > "/home/$MY_USERNAME/.urlview"
  411. echo 'COMMAND lynx -dump -width=78 -nolist %s' >> "/home/$MY_USERNAME/.urlview"
  412. cp -f /etc/Muttrc "/home/$MY_USERNAME/.muttrc"
  413. cp -f /etc/Muttrc /etc/skel/.muttrc
  414. cp -f "/home/$MY_USERNAME/.urlview" /etc/skel/.urlview
  415. touch "/home/$MY_USERNAME/.mutt-alias"
  416. cp "/home/$MY_USERNAME/.mutt-alias" /etc/skel/.mutt-alias
  417. chown "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.muttrc"
  418. chown "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.mutt-alias"
  419. # default user on generic images
  420. if [ -d "/home/${GENERIC_IMAGE_USERNAME}" ]; then
  421. cp -f /etc/Muttrc "/home/${GENERIC_IMAGE_USERNAME}/.muttrc"
  422. chown "${GENERIC_IMAGE_USERNAME}":"${GENERIC_IMAGE_USERNAME}" "/home/${GENERIC_IMAGE_USERNAME}/.muttrc"
  423. touch "/home/${GENERIC_IMAGE_USERNAME}/.mutt-alias"
  424. chown "${GENERIC_IMAGE_USERNAME}":"${GENERIC_IMAGE_USERNAME}" "/home/${GENERIC_IMAGE_USERNAME}/.mutt-alias"
  425. fi
  426. mark_completed "${FUNCNAME[0]}"
  427. }
  428. function email_archiving {
  429. if [ ! -d /etc/exim4 ]; then
  430. return
  431. fi
  432. # ensure that the mail archive script is up to date
  433. if [ -f "/usr/local/bin/${PROJECT_NAME}-archive-mail" ]; then
  434. if [ ! -f /etc/cron.daily/archivemail ]; then
  435. cp "/usr/local/bin/${PROJECT_NAME}-archive-mail" /etc/cron.daily/archivemail
  436. chmod +x /etc/cron.daily/archivemail
  437. else
  438. HASH1=$(sha256sum "/usr/local/bin/${PROJECT_NAME}-archive-mail" | awk -F ' ' '{print $1}')
  439. HASH2=$(sha256sum /etc/cron.daily/archivemail | awk -F ' ' '{print $1}')
  440. if [[ "$HASH1" != "$HASH2" ]]; then
  441. cp "/usr/local/bin/${PROJECT_NAME}-archive-mail" /etc/cron.daily/archivemail
  442. chmod +x /etc/cron.daily/archivemail
  443. fi
  444. fi
  445. else
  446. if [ -f "/usr/bin/${PROJECT_NAME}-archive-mail" ]; then
  447. if [ ! -f /etc/cron.daily/archivemail ]; then
  448. cp "/usr/bin/${PROJECT_NAME}-archive-mail" /etc/cron.daily/archivemail
  449. chmod +x /etc/cron.daily/archivemail
  450. else
  451. HASH1=$(sha256sum "/usr/local/bin/${PROJECT_NAME}-archive-mail" | awk -F ' ' '{print $1}')
  452. HASH2=$(sha256sum /etc/cron.daily/archivemail | awk -F ' ' '{print $1}')
  453. if [[ "$HASH1" != "$HASH2" ]]; then
  454. cp "/usr/local/bin/${PROJECT_NAME}-archive-mail" /etc/cron.daily/archivemail
  455. chmod +x /etc/cron.daily/archivemail
  456. fi
  457. fi
  458. else
  459. echo "/usr/bin/${PROJECT_NAME}-archive-mail was not found. ${PROJECT_NAME} might not have fully installed."
  460. exit 62379
  461. fi
  462. fi
  463. # update to the next commit
  464. function_check set_repo_commit
  465. set_repo_commit "$INSTALL_DIR/cleanup-maildir" "cleanup-maildir commit" "$CLEANUP_MAILDIR_COMMIT" "$CLEANUP_MAILDIR_REPO"
  466. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  467. return
  468. fi
  469. if [ ! -d "$INSTALL_DIR" ]; then
  470. mkdir "$INSTALL_DIR"
  471. fi
  472. cd "$INSTALL_DIR" || exit 246824245242
  473. function_check git_clone
  474. git_clone "$CLEANUP_MAILDIR_REPO" "$INSTALL_DIR/cleanup-maildir"
  475. cd "$INSTALL_DIR/cleanup-maildir" || exit 6887242572
  476. git checkout $CLEANUP_MAILDIR_COMMIT -b $CLEANUP_MAILDIR_COMMIT
  477. set_completion_param "cleanup-maildir commit" "$CLEANUP_MAILDIR_COMMIT"
  478. if [ ! -f /usr/bin/cleanup-maildir ]; then
  479. cp "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" /usr/bin
  480. else
  481. HASH1=$(sha256sum "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" | awk -F ' ' '{print $1}')
  482. HASH2=$(sha256sum /usr/bin/cleanup-maildir | awk -F ' ' '{print $1}')
  483. if [[ "$HASH1" != "$HASH2" ]]; then
  484. cp "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" /usr/bin
  485. fi
  486. fi
  487. mark_completed "${FUNCNAME[0]}"
  488. }
  489. # Ensure that the from field is correct when sending email from Mutt
  490. function email_from_address {
  491. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  492. return
  493. fi
  494. if [ ! -f "/home/$MY_USERNAME/.muttrc" ]; then
  495. return
  496. fi
  497. if grep -q "set from=" "/home/$MY_USERNAME/.muttrc"; then
  498. sed -i "s|set from=.*|set from='$MY_NAME <$MY_EMAIL_ADDRESS>'|g" "/home/$MY_USERNAME/.muttrc"
  499. else
  500. echo "set from='$MY_NAME <$MY_EMAIL_ADDRESS>'" >> "/home/$MY_USERNAME/.muttrc"
  501. fi
  502. mark_completed "${FUNCNAME[0]}"
  503. }
  504. function create_public_mailing_list {
  505. if [ ! -d /etc/exim4 ]; then
  506. return
  507. fi
  508. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  509. return
  510. fi
  511. if [ ! "$PUBLIC_MAILING_LIST" ]; then
  512. return
  513. fi
  514. # does the mailing list have a separate domain name?
  515. if [ ! "$PUBLIC_MAILING_LIST_DOMAIN_NAME" ]; then
  516. PUBLIC_MAILING_LIST_DOMAIN_NAME="$DEFAULT_DOMAIN_NAME"
  517. fi
  518. PUBLIC_MAILING_LIST_USER="mlmmj"
  519. apt-get -yq install mlmmj
  520. adduser --system "$PUBLIC_MAILING_LIST_USER"
  521. addgroup "$PUBLIC_MAILING_LIST_USER"
  522. adduser "$PUBLIC_MAILING_LIST_USER" "$PUBLIC_MAILING_LIST_USER"
  523. echo ''
  524. echo $"Creating the $PUBLIC_MAILING_LIST mailing list"
  525. echo ''
  526. # create the list
  527. mlmmj-make-ml -a -L "$PUBLIC_MAILING_LIST" -c "$PUBLIC_MAILING_LIST_USER"
  528. { echo 'SYSTEM_ALIASES_PIPE_TRANSPORT = address_pipe';
  529. echo "SYSTEM_ALIASES_USER = $PUBLIC_MAILING_LIST_USER";
  530. echo "SYSTEM_ALIASES_GROUP = $PUBLIC_MAILING_LIST_USER"; } > /etc/exim4/conf.d/main/000_localmacros
  531. # router
  532. { echo 'mlmmj_router:';
  533. echo " debug_print = \"R: mlmmj_router for \$local_part@\$domain\"";
  534. echo ' driver = accept';
  535. echo ' domains = +mlmmj_domains';
  536. echo " #require_files = MLMMJ_HOME/\${lc::\$local_part}";
  537. echo ' # Use this instead, if you dont want to give Exim rx rights to mlmmj spool.';
  538. echo ' # Exim will then spawn a new process running under the UID of "mlmmj".';
  539. echo " require_files = mlmmj:MLMMJ_HOME/\${lc::\$local_part}";
  540. echo ' local_part_suffix = +*';
  541. echo ' local_part_suffix_optional';
  542. echo ' headers_remove = Delivered-To';
  543. echo " headers_add = Delivered-To: \$local_part\$local_part_suffix@\$domain";
  544. echo ' transport = mlmmj_transport'; } > /etc/exim4/conf.d/router/750_exim4-config_mlmmj
  545. # transport
  546. { echo 'mlmmj_transport:';
  547. echo " debug_print = \"T: mlmmj_transport for \$local_part@\$domain\"";
  548. echo ' driver = pipe';
  549. echo ' return_path_add';
  550. echo ' user = mlmmj';
  551. echo ' group = mlmmj';
  552. echo ' home_directory = MLMMJ_HOME';
  553. echo ' current_directory = MLMMJ_HOME';
  554. echo " command = /usr/bin/mlmmj-receive -F -L MLMMJ_HOME/\${lc:\$local_part}"; } > /etc/exim4/conf.d/transport/40_exim4-config_mlmmj
  555. if ! grep -q "MLMMJ_HOME=/var/spool/mlmmj" /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs; then
  556. sed -i '/MAIN CONFIGURATION SETTINGS/a\MLMMJ_HOME=/var/spool/mlmmj' /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs
  557. fi
  558. if ! grep -q "domainlist mlmmj_domains =" /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs; then
  559. sed -i "/MLMMJ_HOME/a\\domainlist mlmmj_domains = $PUBLIC_MAILING_LIST_DOMAIN_NAME" /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs
  560. fi
  561. if ! grep -q "delay_warning_condition =" /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs; then
  562. sed -i "/domainlist mlmmj_domains =/a\\delay_warning_condition = \${if match_domain{\$domain}{+mlmmj_domains}{no}{yes}}" /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs
  563. fi
  564. if ! grep -q ": +mlmmj_domains" /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs; then
  565. sed -i 's/domainlist relay_to_domains = MAIN_RELAY_TO_DOMAINS/domainlist relay_to_domains = MAIN_RELAY_TO_DOMAINS : +mlmmj_domains/g' /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs
  566. fi
  567. if ! grep -q "! +mlmmj_domains" /etc/exim4/conf.d/router/200_exim4-config_primary; then
  568. sed -i 's/domains = ! +local_domains/domains = ! +mlmmj_domains : ! +local_domains/g' /etc/exim4/conf.d/router/200_exim4-config_primary
  569. fi
  570. update-exim4.conf.template -r
  571. update-exim4.conf
  572. systemctl restart exim4
  573. if ! grep -q $"$PUBLIC_MAILING_LIST mailing list" "/home/$MY_USERNAME/README"; then
  574. { echo '';
  575. echo '';
  576. echo $"$PUBLIC_MAILING_LIST mailing list";
  577. echo '=================================';
  578. echo $"To subscribe to the $PUBLIC_MAILING_LIST mailing list send a";
  579. echo $"cleartext email to $PUBLIC_MAILING_LIST+subscribe@$DEFAULT_DOMAIN_NAME"; } >> "/home/$MY_USERNAME/README"
  580. chown "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/README"
  581. chmod 600 "/home/$MY_USERNAME/README"
  582. fi
  583. "${PROJECT_NAME}-addlist" -u "$MY_USERNAME" -l "$PUBLIC_MAILING_LIST" -s "$PUBLIC_MAILING_LIST"
  584. mark_completed "${FUNCNAME[0]}"
  585. }
  586. function split_gpg_key_into_fragments {
  587. # split the gpg key into fragments if social key management is enabled
  588. if [[ "$ENABLE_SOCIAL_KEY_MANAGEMENT" == "yes" ]]; then
  589. if [ "$IMAGE_PASSWORD_FILE" ]; then
  590. if [ -f "$IMAGE_PASSWORD_FILE" ]; then
  591. "${PROJECT_NAME}-splitkey" -u "$MY_USERNAME" -e "$MY_EMAIL_ADDRESS" --fullname "$MY_NAME" --passwordfile "$IMAGE_PASSWORD_FILE"
  592. return
  593. fi
  594. fi
  595. echo 'Splitting GPG key. You may need to enter your passphrase.'
  596. "${PROJECT_NAME}-splitkey" -u "$MY_USERNAME" -e "$MY_EMAIL_ADDRESS" --fullname "$MY_NAME"
  597. if [ ! -d "/home/$MY_USERNAME/.gnupg_fragments" ]; then
  598. echo 'Yhe GPG key could not be split'
  599. exit 86548
  600. fi
  601. fi
  602. }
  603. function import_email {
  604. if [ ! -d /etc/exim4 ]; then
  605. return
  606. fi
  607. EMAIL_COMPLETE_MSG=$"
  608. *** ${PROJECT_NAME} mailbox installation is complete ***
  609. Now on your internet router forward ports
  610. 25, 587, 465, 993 and 2222 to the ${PROJECT_NAME}
  611. "
  612. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  613. if [[ "$SYSTEM_TYPE" == "mail"* ]]; then
  614. function_check backup_to_friends_servers
  615. backup_to_friends_servers
  616. function_check install_tripwire
  617. install_tripwire
  618. function_check split_gpg_key_into_fragments
  619. split_gpg_key_into_fragments
  620. clear
  621. echo ''
  622. echo "$EMAIL_COMPLETE_MSG"
  623. if [ -d "$USB_MOUNT" ]; then
  624. umount "$USB_MOUNT"
  625. rm -rf "$USB_MOUNT"
  626. echo $' You can now remove the USB drive'
  627. fi
  628. exit 0
  629. fi
  630. return
  631. fi
  632. mark_completed "${FUNCNAME[0]}"
  633. if [[ "$SYSTEM_TYPE" == "mail"* ]]; then
  634. function_check backup_to_friends_servers
  635. backup_to_friends_servers
  636. function_check install_tripwire
  637. install_tripwire
  638. function_check split_gpg_key_into_fragments
  639. split_gpg_key_into_fragments
  640. # unmount any attached usb drive
  641. clear
  642. echo ''
  643. echo "$EMAIL_COMPLETE_MSG"
  644. echo ''
  645. if [ -d "$USB_MOUNT" ]; then
  646. umount "$USB_MOUNT"
  647. rm -rf "$USB_MOUNT"
  648. echo $' You can now remove the USB drive'
  649. fi
  650. exit 0
  651. fi
  652. }
  653. function remove_email {
  654. echo ''
  655. }
  656. function install_email_basic {
  657. apt-get -yq remove postfix
  658. apt-get -yq install exim4 sasl2-bin swaks libnet-ssleay-perl procmail
  659. if [ ! -d /etc/exim4 ]; then
  660. echo $"ERROR: Exim does not appear to have installed. $CHECK_MESSAGE"
  661. exit 48
  662. fi
  663. # configure for Maildir format
  664. sed -i 's/MAIL_DIR/#MAIL_DIR/g' /etc/login.defs
  665. sed -i 's|#MAIL_FILE.*|MAIL_FILE Maildir/|g' /etc/login.defs
  666. if ! grep -q "export MAIL" /etc/profile; then
  667. echo 'export MAIL=~/Maildir' >> /etc/profile
  668. fi
  669. sed -i 's|pam_mail.so standard|pam_mail.so dir=~/Maildir standard|g' /etc/pam.d/login
  670. sed -i 's|pam_mail.so standard noenv|pam_mail.so dir=~/Maildir standard|g' /etc/pam.d/sshd
  671. sed -i 's|pam_mail.so nopen|pam_mail.so dir=~/Maildir nopen|g' /etc/pam.d/su
  672. { echo "dc_eximconfig_configtype='internet'";
  673. echo "dc_other_hostnames='${DEFAULT_DOMAIN_NAME};mail.${DEFAULT_DOMAIN_NAME}'";
  674. echo "dc_local_interfaces=''";
  675. echo "dc_readhost=''";
  676. echo "dc_relay_domains=''";
  677. echo "dc_minimaldns='false'"; } > /etc/exim4/update-exim4.conf.conf
  678. IPv4_address=$(get_ipv4_address)
  679. IPv4_address_base=$(echo "$IPv4_address" | awk -F '.' '{print $1"."$2"."$3}')
  680. RELAY_NETS="${IPv4_address_base}.0/24"
  681. if [ "$LOCAL_NETWORK_STATIC_IP_ADDRESS" ]; then
  682. RELAY_NETS=$(awk "$LOCAL_NETWORK_STATIC_IP_ADDRESS" -F '.' '{print $1 "." $2 "." $3 ".0/24"}')
  683. fi
  684. { echo "dc_relay_nets='$RELAY_NETS'";
  685. echo "dc_smarthost=''";
  686. echo "CFILEMODE='644'";
  687. echo "dc_use_split_config='false'";
  688. echo "dc_hide_mailname=''";
  689. echo "dc_mailname_in_oh='true'";
  690. echo "dc_localdelivery='maildir_home'";
  691. echo "dc_main_log_selector=-all"; } >> /etc/exim4/update-exim4.conf.conf
  692. echo "chunking_advertise_hosts =" > /etc/exim4/conf.d/main/04_exim4-config_chunking
  693. update-exim4.conf
  694. sed -i "s/START=no/START=yes/g" /etc/default/saslauthd
  695. systemctl start saslauthd
  696. email_install_tls
  697. adduser "$MY_USERNAME" sasl
  698. addgroup Debian-exim sasl
  699. systemctl restart exim4
  700. email_create_template
  701. if [ -f /usr/sbin/exim ]; then
  702. chmod u+s /usr/sbin/exim
  703. fi
  704. if [ -f /usr/sbin/exim4 ]; then
  705. chmod u+s /usr/sbin/exim4
  706. fi
  707. function_check configure_firewall_for_email
  708. configure_firewall_for_email
  709. dpkg-reconfigure --frontend noninteractive exim4-config
  710. systemctl restart exim4
  711. }
  712. function email_change_relay {
  713. curr_ip_address="$1"
  714. email_relay_base=$(echo "$curr_ip_address" | awk -F '.' '{print $1"."$2"."$3}')
  715. RELAY_NETS="${email_relay_base}.0/24"
  716. sed -i "s|dc_relay_nets=.*|dc_relay_nets='$RELAY_NETS'|g" /etc/exim4/update-exim4.conf.conf
  717. dpkg-reconfigure --frontend noninteractive exim4-config
  718. }
  719. function create_procmail {
  720. if [ ! -d /etc/exim4 ]; then
  721. return
  722. fi
  723. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  724. return
  725. fi
  726. if [ ! -f "/home/$MY_USERNAME/.procmailrc" ]; then
  727. { echo "MAILDIR=\$HOME/Maildir";
  728. echo "DEFAULT=\$MAILDIR/";
  729. echo "LOGFILE=\$HOME/log/procmail.log";
  730. echo 'LOGABSTRACT=all';
  731. echo '';
  732. echo '# Test for an empty or missing subject line';
  733. echo "SUBJ_=\$(formail -xSubject: \\";
  734. echo " | expand | sed -e 's/^[ ]*//g' -e 's/[ ]*\$//g')";
  735. echo ':0';
  736. echo ' * SUBJ_ ?? ^^^^';
  737. echo '/dev/null';
  738. echo '';
  739. echo $"# Tripwire reports which have no violations don't need to be logged";
  740. echo ':0 BD:'; } > "/home/$MY_USERNAME/.procmailrc"
  741. TRIPWIRE_VIOLATIONS_STR=$'Total violations found: 0'
  742. { echo " * .*$TRIPWIRE_VIOLATIONS_STR";
  743. echo '/dev/null';
  744. echo ''; } >> "/home/$MY_USERNAME/.procmailrc"
  745. chown "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.procmailrc"
  746. fi
  747. mkdir -p "/home/$MY_USERNAME/Maildir/admin/new"
  748. mkdir -p "/home/$MY_USERNAME/Maildir/admin/cur"
  749. chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/Maildir/admin"
  750. if [ ! -f /etc/skel/.procmailrc ]; then
  751. cp "/home/$MY_USERNAME/.procmailrc" /etc/skel/.procmailrc
  752. chown root:root /etc/skel/.procmailrc
  753. fi
  754. if [ -f /usr/bin/procmail ]; then
  755. chmod 6755 /usr/bin/procmail
  756. fi
  757. mark_completed "${FUNCNAME[0]}"
  758. }
  759. function handle_admin_emails {
  760. # keep emails for root in a separate folder
  761. if [ -d "/home/$MY_USERNAME/Maildir/admin" ]; then
  762. return
  763. fi
  764. "${PROJECT_NAME}-addemail" -u "$MY_USERNAME" -e "root@$DEFAULT_DOMAIN_NAME" -g admin --public no
  765. }
  766. function spam_filtering {
  767. if [ ! -d /etc/exim4 ]; then
  768. return
  769. fi
  770. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  771. return
  772. fi
  773. apt-get -yq install exim4-daemon-heavy
  774. apt-get -yq install spamassassin
  775. if [ ! -f /etc/default/spamassassin ]; then
  776. echo 'Spamassassin was not installed'
  777. exit 72570
  778. fi
  779. sa-update -v
  780. sed -i 's/ENABLED=0/ENABLED=1/g' /etc/default/spamassassin
  781. sed -i 's/# spamd_address = 127.0.0.1 783/spamd_address = 127.0.0.1 783/g' /etc/exim4/exim4.conf.template
  782. # This configuration is based on https://wiki.debian.org/DebianSpamAssassin
  783. sed -i 's/local_parts = postmaster/local_parts = postmaster:abuse/g' /etc/exim4/conf.d/acl/30_exim4-config_check_rcpt
  784. sed -i '/domains = +local_domains : +relay_to_domains/a\ set acl_m0 = rfcnames' /etc/exim4/conf.d/acl/30_exim4-config_check_rcpt
  785. sed -i "s/accept/accept condition = \${if eq{\$acl_m0}{rfcnames} {1}{0}}/g" /etc/exim4/conf.d/acl/40_exim4-config_check_data
  786. { echo "warn message = X-Spam-Score: \$spam_score (\$spam_bar)";
  787. echo ' spam = nobody:true';
  788. echo 'warn message = X-Spam-Flag: YES';
  789. echo ' spam = nobody';
  790. echo "warn message = X-Spam-Report: \$spam_report";
  791. echo ' spam = nobody';
  792. echo '# reject spam at high scores (> 12)';
  793. echo "deny message = This message scored \$spam_score spam points.";
  794. echo ' spam = nobody:true';
  795. echo " condition = \${if >{\$spam_score_int}{120}{1}{0}}"; } >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  796. # procmail configuration
  797. { echo '# get spamassassin to check emails';
  798. echo ':0fw: .spamassassin.lock';
  799. echo ' * < 256000';
  800. echo '| spamc';
  801. echo '# strong spam are discarded';
  802. echo ':0';
  803. echo ' * ^X-Spam-Level: \*\*\*\*\*\*';
  804. echo '/dev/null';
  805. echo '# weak spam are kept just in case - clear this out every now and then';
  806. echo ':0';
  807. echo ' * ^X-Spam-Level: \*\*\*\*\*';
  808. echo 'maybe-spam/';
  809. echo '# otherwise, marginal spam goes here for revision';
  810. echo ':0';
  811. echo ' * ^X-Spam-Level: \*\*';
  812. echo 'spam/'; } >> "/home/$MY_USERNAME/.procmailrc"
  813. chown "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.procmailrc"
  814. { echo '# get spamassassin to check emails';
  815. echo ':0fw: .spamassassin.lock';
  816. echo ' * < 256000';
  817. echo '| spamc';
  818. echo '# strong spam are discarded';
  819. echo ':0';
  820. echo ' * ^X-Spam-Level: \*\*\*\*\*\*';
  821. echo '/dev/null';
  822. echo '# weak spam are kept just in case - clear this out every now and then';
  823. echo ':0';
  824. echo ' * ^X-Spam-Level: \*\*\*\*\*';
  825. echo 'maybe-spam/';
  826. echo '# otherwise, marginal spam goes here for revision';
  827. echo ':0';
  828. echo ' * ^X-Spam-Level: \*\*';
  829. echo 'spam/'; } >> /etc/skel/.procmailrc
  830. # filtering scripts
  831. { echo '#!/bin/bash';
  832. echo 'for d in /home/*/ ; do';
  833. echo " USERNAME=\$(echo \"\$d\" | awk -F '/' '{print \$3}')";
  834. echo " if [[ \$USERNAME != \"git\" && $USERNAME != \"go\" && \$USERNAME != \"gogs\" && \$USERNAME != \"sync\" && \$USERNAME != \"tahoelafs\" ]]; then";
  835. echo " MAILDIR=/home/\$USERNAME/Maildir/.learn-spam";
  836. echo " if [ ! -d \"\$MAILDIR\" ]; then";
  837. echo ' exit';
  838. echo ' fi';
  839. echo " for f in \$(ls \$MAILDIR/cur)";
  840. echo ' do';
  841. echo " spamc -L spam < \"\$MAILDIR/cur/\$f\" > /dev/null";
  842. echo " rm \"\$MAILDIR/cur/\$f\"";
  843. echo ' done';
  844. echo " for f in \$(ls \$MAILDIR/new)";
  845. echo ' do';
  846. echo " spamc -L spam < \"\$MAILDIR/new/\$f\" > /dev/null";
  847. echo " rm \"\$MAILDIR/new/\$f\"";
  848. echo ' done';
  849. echo ' fi';
  850. echo 'done';
  851. echo 'exit 0'; } > /usr/bin/filterspam
  852. { echo '#!/bin/bash';
  853. echo 'for d in /home/*/ ; do';
  854. echo " USERNAME=\$(echo \"\$d\" | awk -F '/' '{print \$3}')";
  855. echo " if [[ \$USERNAME != \"git\" && \$USERNAME != \"go\" && \$USERNAME != \"gogs\" && \$USERNAME != \"sync\" && \$USERNAME != \"tahoelafs\" ]]; then";
  856. echo " MAILDIR=/home/\$USERNAME/Maildir/.learn-ham";
  857. echo " if [ ! -d \"\$MAILDIR\" ]; then";
  858. echo ' exit';
  859. echo ' fi';
  860. echo " for f in \$(ls \$MAILDIR/cur)";
  861. echo ' do';
  862. echo " spamc -L ham < \"\$MAILDIR/cur/\$f\" > /dev/null";
  863. echo " rm \"\$MAILDIR/cur/\$f\"";
  864. echo ' done';
  865. echo " for f in \$(ls \$MAILDIR/new)";
  866. echo ' do';
  867. echo " spamc -L ham < \"\$MAILDIR/new/\$f\" > /dev/null";
  868. echo " rm \"\$MAILDIR/new/\$f\"";
  869. echo ' done';
  870. echo ' fi';
  871. echo 'done';
  872. echo 'exit 0'; } > /usr/bin/filterham
  873. function_check cron_add_mins
  874. cron_add_mins 3 '/usr/bin/timeout 120 /usr/bin/filterspam'
  875. cron_add_mins 3 '/usr/bin/timeout 120 /usr/bin/filterham'
  876. chmod 655 /usr/bin/filterspam /usr/bin/filterham
  877. sed -i 's/# use_bayes 1/use_bayes 1/g' /etc/mail/spamassassin/local.cf
  878. sed -i 's/# bayes_auto_learn 1/bayes_auto_learn 1/g' /etc/mail/spamassassin/local.cf
  879. # user preferences
  880. if [ ! -d "/home/$MY_USERNAME/.spamassassin" ]; then
  881. mkdir "/home/$MY_USERNAME/.spamassassin"
  882. { echo $'# How many points before a mail is considered spam.';
  883. echo '# required_score 5';
  884. echo '';
  885. echo $'# Whitelist and blacklist addresses are now file-glob-style patterns, so';
  886. echo $'# "friend@somewhere.com", "*@isp.com", or "*.domain.net" will all work.';
  887. echo '# whitelist_from someone@somewhere.com';
  888. echo '';
  889. echo $'# Add your own customised scores for some tests below. The default scores are';
  890. echo $'# read from the installed spamassassin rules files, but you can override them';
  891. echo $'# here. To see the list of tests and their default scores, go to';
  892. echo '# http://spamassassin.apache.org/tests.html .';
  893. echo '#';
  894. echo '# score SYMBOLIC_TEST_NAME n.nn';
  895. echo '';
  896. echo $'# Speakers of Asian languages, like Chinese, Japanese and Korean, will almost';
  897. echo $'# definitely want to uncomment the following lines. They will switch off some';
  898. echo $'# rules that detect 8-bit characters, which commonly trigger on mails using CJK';
  899. echo $'# character sets, or that assume a western-style charset is in use. ';
  900. echo '# ';
  901. echo '# score HTML_COMMENT_8BITS 0';
  902. echo '# score UPPERCASE_25_50 0';
  903. echo '# score UPPERCASE_50_75 0';
  904. echo '# score UPPERCASE_75_100 0';
  905. echo '# score OBSCURED_EMAIL 0';
  906. echo '';
  907. echo $'# Speakers of any language that uses non-English, accented characters may wish';
  908. echo $'# to uncomment the following lines. They turn off rules that fire on';
  909. echo $'# misformatted messages generated by common mail apps in contravention of the';
  910. echo $'# email RFCs.';
  911. echo '';
  912. echo '# score SUBJ_ILLEGAL_CHARS 0'; } > "/home/$MY_USERNAME/.spamassassin/user_prefs"
  913. fi
  914. # this must be accessible by root
  915. chown -R "$MY_USERNAME":root "/home/$MY_USERNAME/.spamassassin"
  916. # script to keep spamassassin running
  917. # There is a systemd script from the debian package, but it doesn't restart on failure
  918. # and also doesn't ensure start after networking is up. If that is eventually fixed
  919. # then this script and the cron job which runs it can be removed.
  920. script_name=/usr/bin/run-spamassassin
  921. { echo '#!/bin/bash';
  922. echo "current_state=\$(systemctl status spamassassin)";
  923. echo "if [[ \"\$current_state\" != *\"(running)\"* ]]; then";
  924. echo ' systemctl restart spamassassin';
  925. echo 'fi';
  926. echo 'exit 0'; } > $script_name
  927. chmod +x $script_name
  928. systemctl start spamassassin
  929. systemctl restart exim4
  930. systemctl restart cron
  931. function_check cron_add_mins
  932. cron_add_mins 10 "$script_name 2> /dev/null"
  933. mark_completed "${FUNCNAME[0]}"
  934. }
  935. function configure_imap {
  936. if [ ! -d /etc/exim4 ]; then
  937. return
  938. fi
  939. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  940. return
  941. fi
  942. apt-get -yq install dovecot-imapd
  943. if [ ! -d /etc/dovecot ]; then
  944. echo $"ERROR: Dovecot does not appear to have installed. $CHECK_MESSAGE"
  945. exit 48
  946. fi
  947. if [[ "$(cert_exists dovecot)" == "0" ]]; then
  948. "${PROJECT_NAME}-addcert" -h dovecot --dhkey "$DH_KEYLENGTH"
  949. CHECK_HOSTNAME=dovecot
  950. check_certificates dovecot
  951. fi
  952. chmod 600 /etc/shadow
  953. chmod 600 /etc/gshadow
  954. groupadd default
  955. usermod -g default dovecot
  956. chmod 0000 /etc/shadow
  957. chmod 0000 /etc/gshadow
  958. chown root:default /etc/ssl/certs/dovecot.*
  959. chown root:default /etc/ssl/private/dovecot.*
  960. chown root:default "/etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.*"
  961. chown root:default "/etc/ssl/private/${DEFAULT_DOMAIN_NAME}.*"
  962. if [ ! -f /etc/dovecot/conf.d/10-ssl.conf ]; then
  963. echo $'Unable to find /etc/dovecot/conf.d/10-ssl.conf'
  964. exit 83629
  965. fi
  966. sed -i 's|#ssl =.*|ssl = no|g' /etc/dovecot/conf.d/10-ssl.conf
  967. sed -i 's|ssl =.*|ssl = no|g' /etc/dovecot/conf.d/10-ssl.conf
  968. sed -i "s|#ssl_cert =.*|ssl_cert = </etc/ssl/certs/dovecot.crt|g" /etc/dovecot/conf.d/10-ssl.conf
  969. sed -i "s|ssl_cert =.*|ssl_cert = </etc/ssl/certs/dovecot.crt|g" /etc/dovecot/conf.d/10-ssl.conf
  970. sed -i "s|#ssl_key =.*|ssl_key = </etc/ssl/private/dovecot.key|g" /etc/dovecot/conf.d/10-ssl.conf
  971. sed -i "s|ssl_key =.*|ssl_key = </etc/ssl/private/dovecot.key|g" /etc/dovecot/conf.d/10-ssl.conf
  972. sed -i "s|#ssl_dh_parameters_length.*|ssl_dh_parameters_length = ${DH_KEYLENGTH}|g" /etc/dovecot/conf.d/10-ssl.conf
  973. sed -i 's/#ssl_prefer_server_ciphers.*/ssl_prefer_server_ciphers = yes/g' /etc/dovecot/conf.d/10-ssl.conf
  974. sed -i "s|#ssl_protocols =.*|ssl_protocols = '$SSL_PROTOCOLS'|g" /etc/dovecot/conf.d/10-ssl.conf
  975. sed -i "s|ssl_protocols =.*|ssl_protocols = '$SSL_PROTOCOLS'|g" /etc/dovecot/conf.d/10-ssl.conf
  976. echo "ssl_cipher_list = '$SSL_CIPHERS'" >> /etc/dovecot/conf.d/10-ssl.conf
  977. if [ ! -f /etc/dovecot/conf.d/10-master.conf ]; then
  978. echo $'Unable to find /etc/dovecot/conf.d/10-master.conf'
  979. exit 49259
  980. fi
  981. sed -i 's/#process_limit =.*/process_limit = 100/g' /etc/dovecot/conf.d/10-master.conf
  982. if [ ! -f /etc/dovecot/conf.d/10-logging.conf ]; then
  983. echo $'Unable to find /etc/dovecot/conf.d/10-logging.conf'
  984. exit 48936
  985. fi
  986. sed -i 's/#auth_verbose.*/auth_verbose = yes/g' /etc/dovecot/conf.d/10-logging.conf
  987. if [ ! -f /etc/dovecot/dovecot.conf ]; then
  988. echo $'Unable to find /etc/dovecot/dovecot.conf'
  989. exit 43890
  990. fi
  991. sed -i 's/#listen =.*/listen = */g' /etc/dovecot/dovecot.conf
  992. if [ ! -f /etc/dovecot/conf.d/10-auth.conf ]; then
  993. echo $'Unable to find /etc/dovecot/conf.d/10-auth.conf'
  994. exit 843256
  995. fi
  996. sed -i 's/#disable_plaintext_auth =.*/disable_plaintext_auth = no/g' /etc/dovecot/conf.d/10-auth.conf
  997. sed -i 's/auth_mechanisms =.*/auth_mechanisms = plain login/g' /etc/dovecot/conf.d/10-auth.conf
  998. if [ ! -f /etc/dovecot/conf.d/10-mail.conf ]; then
  999. echo $'Unable to find /etc/dovecot/conf.d/10-mail.conf'
  1000. exit 42036
  1001. fi
  1002. sed -i 's|mail_location =.*|mail_location = maildir:~/Maildir:LAYOUT=fs|g' /etc/dovecot/conf.d/10-mail.conf
  1003. # This long notify interval makes the system more suited for use with
  1004. # battery powered mobile devices
  1005. sed -i 's|#imap_idle_notify_interval =.*|imap_idle_notify_interval = 29|g' /etc/dovecot/conf.d/20-imap.conf
  1006. if [ -f /var/lib/dovecot/ssl-parameters.dat ]; then
  1007. rm /var/lib/dovecot/ssl-parameters.dat
  1008. fi
  1009. if [ -f /etc/systemd/system/sockets.target.wants/dovecot.socket ]; then
  1010. rm /etc/systemd/system/sockets.target.wants/dovecot.socket
  1011. fi
  1012. # Separate logging, otherwise syslog is used
  1013. if ! grep -q "# logging" /etc/dovecot/dovecot.conf; then
  1014. { echo '';
  1015. echo '# logging';
  1016. echo 'log_path = /var/log/dovecot.log';
  1017. echo 'info_log_path = /var/log/dovecot-info.log';
  1018. echo 'debug_log_path = /var/log/dovecot-debug.log'; } >> /etc/dovecot/dovecot.conf
  1019. fi
  1020. systemctl restart dovecot
  1021. mark_completed "${FUNCNAME[0]}"
  1022. }
  1023. function configure_imap_client_certs {
  1024. if [ ! -d /etc/exim4 ]; then
  1025. return
  1026. fi
  1027. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  1028. return
  1029. fi
  1030. # http://strange.systems/certificate-based-auth-with-dovecot-sendmail/
  1031. sed -i 's|#default_process_limit =.*|default_process_limit = 100|g' /etc/dovecot/conf.d/10-master.conf
  1032. sed -i 's/disable_plaintext_auth =.*/disable_plaintext_auth = yes/g' /etc/dovecot/conf.d/10-auth.conf
  1033. sed -i 's|#auth_ssl_require_client_cert =.*|auth_ssl_require_client_cert = yes|g' /etc/dovecot/conf.d/10-auth.conf
  1034. sed -i 's|#auth_ssl_username_from_cert =.*|auth_ssl_username_from_cert = yes|g' /etc/dovecot/conf.d/10-auth.conf
  1035. sed -i "s|#ssl_ca =.*|ssl_ca = /etc/ssl/certs/ca-$DEFAULT_DOMAIN_NAME.crt|g" /etc/dovecot/conf.d/10-ssl.conf
  1036. sed -i 's|#ssl_cert_username_field =.*|ssl_cert_username_field = commonName|g' /etc/dovecot/conf.d/10-ssl.conf
  1037. sed -i 's|#ssl_verify_client_cert =.*|ssl_verify_client_cert = yes|g' /etc/dovecot/conf.d/10-ssl.conf
  1038. if ! grep -q "passdb {" /etc/dovecot/conf.d/10-auth.conf; then
  1039. { echo '';
  1040. echo 'passdb {';
  1041. echo ' driver = passwd-file';
  1042. echo ' args = /etc/dovecot/passwd-file';
  1043. echo ' deny = no';
  1044. echo ' master = no';
  1045. echo ' pass = no';
  1046. echo '}'; } >> /etc/dovecot/conf.d/10-auth.conf
  1047. fi
  1048. if [[ "$ONION_ONLY" == "no" ]]; then
  1049. # make a CA cert
  1050. if [ ! -f "/etc/ssl/private/ca-$DEFAULT_DOMAIN_NAME.key" ]; then
  1051. if [[ "$LETSENCRYPT_ENABLED" != "yes" ]]; then
  1052. "${PROJECT_NAME}-addcert" -h "$DEFAULT_DOMAIN_NAME" --ca "" --dhkey "$DH_KEYLENGTH"
  1053. else
  1054. "${PROJECT_NAME}-addcert" -e "$DEFAULT_DOMAIN_NAME" -s "$LETSENCRYPT_SERVER" --ca "" --dhkey "$DH_KEYLENGTH" --email "$MY_EMAIL_ADDRESS"
  1055. fi
  1056. fi
  1057. fi
  1058. # CA configuration
  1059. { echo '[ ca ]';
  1060. echo "default_ca = dovecot-ca";
  1061. echo '';
  1062. echo '[ crl_ext ]';
  1063. echo 'authorityKeyIdentifier=keyid:always';
  1064. echo '';
  1065. echo '[ dovecot-ca ]';
  1066. echo 'new_certs_dir = .';
  1067. echo 'unique_subject = no';
  1068. echo "certificate = /etc/ssl/certs/ca-$DEFAULT_DOMAIN_NAME.crt";
  1069. echo 'database = ssldb';
  1070. echo "private_key = /etc/ssl/private/ca-$DEFAULT_DOMAIN_NAME.key";
  1071. echo 'serial = sslserial';
  1072. echo 'default_days = 3650';
  1073. echo 'default_md = sha256';
  1074. echo 'default_bits = 2048';
  1075. echo 'policy = dovecot-ca_policy';
  1076. echo 'x509_extensions = dovecot-ca_extensions';
  1077. echo '';
  1078. echo '[ dovecot-ca_policy ]';
  1079. echo 'commonName = supplied';
  1080. echo 'stateOrProvinceName = supplied';
  1081. echo 'countryName = supplied';
  1082. echo 'emailAddress = optional';
  1083. echo 'organizationName = supplied';
  1084. echo 'organizationalUnitName = optional';
  1085. echo '';
  1086. echo '[ dovecot-ca_extensions ]';
  1087. echo 'basicConstraints = CA:false';
  1088. echo 'subjectKeyIdentifier = hash';
  1089. echo 'authorityKeyIdentifier = keyid:always';
  1090. echo 'keyUsage = digitalSignature,keyEncipherment';
  1091. echo 'extendedKeyUsage = clientAuth'; } > /etc/ssl/dovecot-ca.cnf
  1092. if [ -f /etc/ssl/ssldb ]; then
  1093. rm /etc/ssl/ssldb
  1094. fi
  1095. if [ -f /etc/ssl/sslserial ]; then
  1096. rm /etc/ssl/sslserial
  1097. fi
  1098. touch /etc/ssl/ssldb
  1099. echo 0001 > /etc/ssl/sslserial
  1100. #${PROJECT_NAME}-clientcert -u $MY_USERNAME
  1101. systemctl restart dovecot
  1102. mark_completed "${FUNCNAME[0]}"
  1103. }
  1104. function create_gpg_subkey {
  1105. # Note: currently not used
  1106. if [ ! -d /etc/exim4 ]; then
  1107. return
  1108. fi
  1109. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  1110. return
  1111. fi
  1112. apt-get -yq install gnupg
  1113. GPG_KEY_USAGE=$1
  1114. if [[ "$GPG_KEY_USAGE" != "sign" && "$GPG_KEY_USAGE" != "auth" && "$GPG_KEY_USAGE" != "encrypt" ]]; then
  1115. echo $"Unknown subkey usage: $GPG_KEY_USAGE"
  1116. echo $'Available types: sign|auth|encrypt'
  1117. exit 14783
  1118. fi
  1119. KEYGRIP=$(gpg --fingerprint --fingerprint "$MY_EMAIL_ADDRESS" | grep fingerprint | tail -1 | cut -d= -f2 | sed -e 's/ //g')
  1120. # Generate a GPG subkey
  1121. { echo 'Key-Type: eddsa';
  1122. echo 'Key-Curve: Ed25519';
  1123. echo "Key-Grip: $KEYGRIP";
  1124. echo 'Subkey-Type: eddsa';
  1125. echo "subkey-Usage: $GPG_KEY_USAGE";
  1126. echo "Name-Real: $MY_NAME";
  1127. echo "Name-Email: $MY_EMAIL_ADDRESS";
  1128. echo "Name-Comment: $GPG_KEY_USAGE";
  1129. echo 'Expire-Date: 0';
  1130. echo "Passphrase: $PROJECT_NAME"; } > "/home/$MY_USERNAME/gpg-genkey.conf"
  1131. chown "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/gpg-genkey.conf"
  1132. su -m root -c "gpg --homedir /home/$MY_USERNAME/.gnupg --batch --full-gen-key /home/$MY_USERNAME/gpg-genkey.conf" - "$MY_USERNAME"
  1133. chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.gnupg"
  1134. shred -zu "/home/$MY_USERNAME/gpg-genkey.conf"
  1135. # shellcheck disable=SC2034
  1136. MY_GPG_SUBKEY_ID=$(gpg_pubkey_from_email "$MY_USERNAME" "$MY_EMAIL_ADDRESS")
  1137. mark_completed "${FUNCNAME[0]}"
  1138. }
  1139. function gpg_key_exists {
  1140. key_owner_username="$1"
  1141. key_search_text="$2"
  1142. if [[ $key_owner_username != "root" ]]; then
  1143. KEY_EXISTS=$(su -c "gpg --list-keys \"${key_search_text}\"" - "$key_owner_username")
  1144. else
  1145. KEY_EXISTS=$(gpg --list-keys "${key_search_text}")
  1146. fi
  1147. if [ ! "$KEY_EXISTS" ]; then
  1148. echo "no"
  1149. return
  1150. fi
  1151. if [[ "$KEY_EXISTS" == *"error"* ]]; then
  1152. echo "no"
  1153. return
  1154. fi
  1155. echo "yes"
  1156. }
  1157. function configure_gpg {
  1158. if [ ! -d /etc/exim4 ]; then
  1159. return
  1160. fi
  1161. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  1162. return
  1163. fi
  1164. apt-get -yq install gnupg dirmngr
  1165. printf '%%Assuan%%\nsocket=/dev/shm/S.dirmngr\n' > ~/.gnupg/S.dirmngr
  1166. check_email_address_exists
  1167. gpg_dir="/home/$MY_USERNAME/.gnupg"
  1168. # if gpg keys directory was previously imported from usb
  1169. if [ -d "$gpg_dir" ]; then
  1170. echo $'GPG directory exists'
  1171. else
  1172. echo $"GPG directory $gpg_dir was not found"
  1173. fi
  1174. if [ -d "$gpg_dir" ]; then
  1175. echo $'GPG keys were imported'
  1176. sed -i "s|keyserver hkp://keys.gnupg.net|keyserver $GPG_KEYSERVER|g" "$gpg_dir/gpg.conf"
  1177. MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$MY_USERNAME" "$MY_EMAIL_ADDRESS")
  1178. if [ ${#MY_GPG_PUBLIC_KEY_ID} -lt 4 ]; then
  1179. echo $'GPG public key ID could not be obtained'
  1180. else
  1181. if [[ "$MY_GPG_PUBLIC_KEY_ID" == *'error'* ]]; then
  1182. echo $"Can't locate gpg key"
  1183. else
  1184. chown -R "$MY_USERNAME":"$MY_USERNAME" "$gpg_dir"
  1185. chmod 700 "$gpg_dir"
  1186. chmod 600 "$gpg_dir/"*
  1187. printf '%%Assuan%%\nsocket=/dev/shm/S.dirmngr\n' > "/home/$MY_USERNAME/.gnupg/S.dirmngr"
  1188. if [ -d "/home/$MY_USERNAME/.gnupg/crls.d" ]; then
  1189. chmod +x "/home/$MY_USERNAME/.gnupg/crls.d"
  1190. fi
  1191. mark_completed "${FUNCNAME[0]}"
  1192. return
  1193. fi
  1194. fi
  1195. fi
  1196. if [ ! -d "$gpg_dir" ]; then
  1197. mkdir "$gpg_dir"
  1198. echo "keyserver $GPG_KEYSERVER" >> "$gpg_dir/gpg.conf"
  1199. echo 'keyserver-options auto-key-retrieve' >> "$gpg_dir/gpg.conf"
  1200. fi
  1201. sed -i "s|keyserver hkp://keys.gnupg.net|keyserver $GPG_KEYSERVER|g" "$gpg_dir/gpg.conf"
  1202. gpg_agent_setup root
  1203. gpg_agent_setup "$MY_USERNAME"
  1204. if ! grep -q "# default preferences" "$gpg_dir/gpg.conf"; then
  1205. { echo '';
  1206. echo '# default preferences';
  1207. echo 'personal-digest-preferences SHA256';
  1208. echo 'cert-digest-algo SHA256';
  1209. echo 'default-preference-list SHA512 SHA384 SHA256 SHA224 AES256 AES192 AES CAST5 ZLIB BZIP2 ZIP Uncompressed'; } >> "$gpg_dir/gpg.conf"
  1210. fi
  1211. chown -R "$MY_USERNAME":"$MY_USERNAME" "$gpg_dir"
  1212. chmod 700 "$gpg_dir"
  1213. chmod 600 "$gpg_dir/"*
  1214. printf '%%Assuan%%\nsocket=/dev/shm/S.dirmngr\n' > "$gpg_dir/S.dirmngr"
  1215. if [ -d "$gpg_dir/crls.d" ]; then
  1216. chmod +x "$gpg_dir/crls.d"
  1217. fi
  1218. if [[ "$MY_GPG_PUBLIC_KEY" && "$MY_GPG_PRIVATE_KEY" ]]; then
  1219. echo $'Importing GPG keys from file'
  1220. echo $"Public key: $MY_GPG_PUBLIC_KEY"
  1221. echo $"Private key: $MY_GPG_PRIVATE_KEY"
  1222. # use your existing GPG keys which were exported
  1223. if [ ! -f $MY_GPG_PUBLIC_KEY ]; then
  1224. echo $"GPG public key file $MY_GPG_PUBLIC_KEY was not found"
  1225. exit 2483
  1226. fi
  1227. if [ ! -f $MY_GPG_PRIVATE_KEY ]; then
  1228. echo $"GPG private key file $MY_GPG_PRIVATE_KEY was not found"
  1229. exit 5383
  1230. fi
  1231. gpg_import_public_key "$MY_USERNAME" "$MY_GPG_PUBLIC_KEY"
  1232. gpg_import_private_key "$MY_USERNAME" "$MY_GPG_PRIVATE_KEY"
  1233. KEY_EXISTS=$(gpg_key_exists "$MY_USERNAME" "$MY_EMAIL_ADDRESS")
  1234. if [[ $KEY_EXISTS == "no" ]]; then
  1235. echo $"The GPG key for $MY_EMAIL_ADDRESS could not be imported"
  1236. exit 13821
  1237. fi
  1238. # for security ensure that the private key file doesn't linger around
  1239. shred -zu $MY_GPG_PRIVATE_KEY
  1240. MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$MY_USERNAME" "$MY_EMAIL_ADDRESS")
  1241. if [ ${#MY_GPG_PUBLIC_KEY_ID} -lt 4 ]; then
  1242. echo $'GPG public key ID could not be obtained'
  1243. fi
  1244. else
  1245. # Generate a GPG key
  1246. if [ -f "$IMAGE_PASSWORD_FILE" ]; then
  1247. gpg_create_key "$MY_USERNAME" "$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  1248. else
  1249. gpg_create_key "$MY_USERNAME" "$PROJECT_NAME"
  1250. fi
  1251. MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$MY_USERNAME" "$MY_EMAIL_ADDRESS")
  1252. MY_GPG_PUBLIC_KEY=/tmp/public_key.gpg
  1253. gpg_export_public_key "$MY_USERNAME" "$MY_GPG_PUBLIC_KEY_ID" "$MY_GPG_PUBLIC_KEY"
  1254. fi
  1255. if [ ! -d /root/.gnupg ]; then
  1256. cp -r "/home/$MY_USERNAME/.gnupg" /root/
  1257. chmod 700 /root/.gnupg
  1258. chmod 600 /root/.gnupg/*
  1259. printf '%%Assuan%%\nsocket=/dev/shm/S.dirmngr\n' > /root/.gnupg/S.dirmngr
  1260. if [ -d /root/.gnupg/crls.d ]; then
  1261. chmod +x /root/.gnupg/crls.d
  1262. fi
  1263. fi
  1264. mark_completed "${FUNCNAME[0]}"
  1265. }
  1266. function refresh_gpg_keys {
  1267. REFRESH_GPG_KEYS_SCRIPT=/tmp/update-gpg-keys
  1268. { echo '#!/bin/bash';
  1269. echo "if [ -f /usr/local/bin/${PROJECT_NAME}-sec ]; then";
  1270. echo " /usr/bin/timeout 600 /usr/local/bin/${PROJECT_NAME}-sec --refresh yes";
  1271. echo 'else';
  1272. echo " /usr/bin/timeout 600 /usr/bin/${PROJECT_NAME}-sec --refresh yes";
  1273. echo 'fi';
  1274. echo 'exit 0'; } > "$REFRESH_GPG_KEYS_SCRIPT"
  1275. chmod +x "$REFRESH_GPG_KEYS_SCRIPT"
  1276. if [ ! -f /usr/bin/update-gpg-keys ]; then
  1277. cp "$REFRESH_GPG_KEYS_SCRIPT" /usr/bin/update-gpg-keys
  1278. else
  1279. HASH1=$(sha256sum "$REFRESH_GPG_KEYS_SCRIPT" | awk -F ' ' '{print $1}')
  1280. HASH2=$(sha256sum /usr/bin/update-gpg-keys | awk -F ' ' '{print $1}')
  1281. if [[ "$HASH1" != "$HASH2" ]]; then
  1282. cp $REFRESH_GPG_KEYS_SCRIPT /usr/bin/update-gpg-keys
  1283. fi
  1284. rm $REFRESH_GPG_KEYS_SCRIPT
  1285. fi
  1286. REFRESH_GPG_KEYS_SCRIPT=/usr/bin/update-gpg-keys
  1287. if grep -q "${PROJECT_NAME}-sec" /etc/crontab; then
  1288. sed -i "/${PROJECT_NAME}-sec /d" /etc/crontab
  1289. fi
  1290. if ! grep -q "$REFRESH_GPG_KEYS_SCRIPT" /etc/crontab; then
  1291. GPG_REFRESH_TIME=$(( RANDOM % 60 ))
  1292. echo "$GPG_REFRESH_TIME */$REFRESH_GPG_KEYS_HOURS * * * root cronic $REFRESH_GPG_KEYS_SCRIPT" >> /etc/crontab
  1293. systemctl restart cron
  1294. else
  1295. if ! grep "root cronic $REFRESH_GPG_KEYS_SCRIPT" /etc/crontab; then
  1296. sed -i "s|root $REFRESH_GPG_KEYS_SCRIPT.*|root cronic $REFRESH_GPG_KEYS_SCRIPT|g" /etc/crontab
  1297. fi
  1298. fi
  1299. }
  1300. function install_email {
  1301. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  1302. return
  1303. fi
  1304. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  1305. return
  1306. fi
  1307. check_email_address_exists
  1308. install_email_basic
  1309. configure_email_onion
  1310. mark_completed "${FUNCNAME[0]}"
  1311. }
  1312. # NOTE: deliberately no exit 0