freedombone-base-email 75KB

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