freedombone-base-email 79KB

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