freedombone-base-email 80KB

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