freedombone-base-email 81KB

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