freedombone-base-email 66KB

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