freedombone-base-email 65KB

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