freedombone-base-email 57KB

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