freedombone-app-email 70KB

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