freedombone-base-email 66KB

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