freedombone-base-email 79KB

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