freedombone-base-email 78KB

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