freedombone-base-email 66KB

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