freedombone-base-email 79KB

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