freedombone-base-email 80KB

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