freedombone-app-bdsmail 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Brain Dead Simple Mail Server for i2p
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. VARIANTS='full full-vim'
  29. IN_DEFAULT_INSTALL=0
  30. SHOW_ON_ABOUT=1
  31. BDSMAIL_REPO="https://github.com/majestrate/bdsmail"
  32. BDSMAIL_COMMIT='6fae61365306ad3c1966a107aa06310ea65fb239'
  33. BDSMAIL_DIR=/etc/bdsmail
  34. I2P_SAM_PORT=7656
  35. I2P_SMTP_PORT=2525
  36. I2P_WEB_PORT=8489
  37. I2P_POP3_PORT=1110
  38. bdsmail=(MY_USERNAME)
  39. function bdsmail_configure_users {
  40. for d in /home/*/ ; do
  41. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  42. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  43. # Add the user to the i2p group
  44. usermod -a -G i2psvc "$USERNAME"
  45. if [ -f "/home/$USERNAME/.muttrc" ]; then
  46. # Create a mutt i2p folder
  47. if ! grep -q ' =i2p' "/home/$USERNAME/.muttrc"; then
  48. MUTT_MAILBOXES=$(grep "mailboxes =" "/home/$USERNAME/.muttrc")
  49. sed -i "s|$MUTT_MAILBOXES|$MUTT_MAILBOXES =i2p|g" "/home/$USERNAME/.muttrc"
  50. fi
  51. # Create a mutt folder hook to the i2p config
  52. if ! grep -q 'folder-hook !i2p' "/home/$USERNAME/.muttrc"; then
  53. echo 'folder-hook !i2p/* source ~/.muttrc' >> "/home/$USERNAME/.muttrc"
  54. fi
  55. if ! grep -q 'folder-hook i2p' "/home/$USERNAME/.muttrc"; then
  56. echo 'folder-hook i2p/* source ~/.mutt/bdsmail' >> "/home/$USERNAME/.muttrc"
  57. fi
  58. fi
  59. # Create a directory where i2p mail will be stored
  60. if [ ! -d "/home/$USERNAME/Maildir/i2p/new" ]; then
  61. mkdir -p "/home/$USERNAME/Maildir/i2p/cur"
  62. mkdir -p "/home/$USERNAME/Maildir/i2p/new"
  63. chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/Maildir/i2p"
  64. fi
  65. fi
  66. done
  67. }
  68. function logging_on_bdsmail {
  69. echo -n ''
  70. }
  71. function logging_off_bdsmail {
  72. echo -n ''
  73. }
  74. function remove_user_bdsmail {
  75. remove_username="$1"
  76. }
  77. function add_user_bdsmail {
  78. new_username="$1"
  79. new_user_password="$2"
  80. if [ ! -d "/home/$new_username/.mutt" ]; then
  81. mkdir "/home/$new_username/.mutt"
  82. cp /etc/skel/.mutt/bdsmail "/home/$new_username/.mutt"
  83. fi
  84. read_config_param MY_USERNAME
  85. BDSMAIL_PASSWORD=$("${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a bdsmail)
  86. sed -i "s|username|$new_username|g" "/home/$new_username/.mutt/bdsmail"
  87. sed -i "s|password|$BDSMAIL_PASSWORD|g" "/home/$new_username/.mutt/bdsmail"
  88. bdsmail_configure_users
  89. cd $BDSMAIL_DIR || exit 57247684234
  90. $BDSMAIL_DIR/bin/mailtool $BDSMAIL_DIR/config.ini "$new_username" "/home/$new_username/Maildir/i2p" "$BDSMAIL_PASSWORD"
  91. chown -R "$new_username":"$new_username" "/home/$new_username/.mutt"
  92. echo '0'
  93. }
  94. function install_interactive_bdsmail {
  95. echo -n ''
  96. APP_INSTALLED=1
  97. }
  98. function change_password_bdsmail {
  99. curr_username="$1"
  100. new_user_password="$2"
  101. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a bdsmail -p "$new_user_password"
  102. for d in /home/*/ ; do
  103. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  104. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  105. sed -i "s|set smtp_url=.*|set smtp_url=smtp://${curr_username}:${new_user_password}@127.0.0.1:$I2P_SMTP_PORT/" "/home/${USERNAME}/.mutt/bdsmail"
  106. sed -i "s|set from=.*|set from=${USERNAME}@$(bdsmail_domain)|g" "/home/${USERNAME}/.mutt/bdsmail"
  107. chown "${USERNAME}":"${USERNAME}" "/home/${USERNAME}/.mutt/bdsmail"
  108. cd $BDSMAIL_DIR || exit 2468246
  109. $BDSMAIL_DIR/bin/mailtool $BDSMAIL_DIR/config.ini "$curr_username" "/home/$curr_username/Maildir/i2p" "$new_user_password"
  110. fi
  111. done
  112. }
  113. function bdsmail_update_domain {
  114. sed -i "s|set from=.*|set from=username@$(bdsmail_domain)|g" /etc/skel/.mutt/bdsmail
  115. for d in /home/*/ ; do
  116. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  117. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  118. cp /etc/skel/.mutt/bdsmail "/home/${USERNAME}/.mutt/bdsmail"
  119. sed -i "s|set from=.*|set from=${USERNAME}@$(bdsmail_domain)|g" "/home/${USERNAME}/.mutt/bdsmail"
  120. chown "${USERNAME}":"${USERNAME}" "/home/${USERNAME}/.mutt/bdsmail"
  121. fi
  122. done
  123. }
  124. function bdsmail_wait_for_key_generation {
  125. # wait for domain to be generated by the daemon
  126. # This can take a while, probably because i2p is connecting
  127. bds_domain=
  128. sleep_ctr=0
  129. while [ ! $bds_domain ]; do
  130. echo $"Waiting for i2p domain to be generated ${sleep_ctr}/100"
  131. systemctl restart bdsmail
  132. sleep 30
  133. bds_domain=$(bdsmail_domain)
  134. sleep_ctr=$((sleep_ctr + 1))
  135. if [ "$sleep_ctr" -gt 100 ]; then
  136. break
  137. fi
  138. done
  139. }
  140. function reconfigure_bdsmail {
  141. systemctl stop bdsmail
  142. rm $BDSMAIL_DIR/*.dat
  143. rm $BDSMAIL_DIR/*.pem
  144. bdsmail_wait_for_key_generation
  145. if [ ! -f $BDSMAIL_DIR/bdsmail-privkey.dat ]; then
  146. return
  147. fi
  148. bdsmail_update_domain
  149. }
  150. function upgrade_bdsmail {
  151. CURR_BDSMAIL_COMMIT=$(get_completion_param "bdsmail commit")
  152. if [[ "$CURR_BDSMAIL_COMMIT" == "$BDSMAIL_COMMIT" ]]; then
  153. return
  154. fi
  155. # update to the next commit
  156. set_repo_commit $BDSMAIL_DIR "bdsmail commit" "$BDSMAIL_COMMIT" "$BDSMAIL_REPO"
  157. cd $BDSMAIL_DIR || exit 2457245
  158. make GOROOT="/home/go/go${GO_VERSION}"
  159. chown -R i2psvc:i2psvc $BDSMAIL_DIR
  160. systemctl restart bdsmail
  161. if ! grep -q "$(bdsmail_domain)" /etc/skel/.mutt/bdsmail; then
  162. bdsmail_update_domain
  163. fi
  164. }
  165. function backup_local_bdsmail {
  166. systemctl stop bdsmail
  167. source_directory=$BDSMAIL_DIR
  168. function_check backup_directory_to_usb
  169. dest_directory=bdsmail
  170. backup_directory_to_usb $source_directory $dest_directory
  171. systemctl start bdsmail
  172. }
  173. function restore_local_bdsmail {
  174. systemctl stop bdsmail
  175. temp_restore_dir=/root/tempbdsmail
  176. bdsmail_dir=$BDSMAIL_DIR
  177. function_check restore_directory_from_usb
  178. restore_directory_from_usb $temp_restore_dir bdsmail
  179. if [ -d $temp_restore_dir ]; then
  180. if [ -d $temp_restore_dir$bdsmail_dir ]; then
  181. cp -rp $temp_restore_dir$bdsmail_dir $bdsmail_dir/
  182. else
  183. if [ ! -d $bdsmail_dir ]; then
  184. mkdir $bdsmail_dir
  185. fi
  186. cp -rp $temp_restore_dir/* $bdsmail_dir
  187. fi
  188. chown -R i2psvc:i2psvc $bdsmail_dir
  189. rm -rf $temp_restore_dir
  190. fi
  191. systemctl start bdsmail
  192. }
  193. function backup_remote_bdsmail {
  194. systemctl stop bdsmail
  195. source_directory=$BDSMAIL_DIR
  196. function_check backup_directory_to_friend
  197. dest_directory=bdsmail
  198. backup_directory_to_friend $source_directory $dest_directory
  199. systemctl start bdsmail
  200. }
  201. function restore_remote_bdsmail {
  202. systemctl stop bdsmail
  203. temp_restore_dir=/root/tempbdsmail
  204. bdsmail_dir=$BDSMAIL_DIR
  205. function_check restore_directory_from_friend
  206. restore_directory_from_friend $temp_restore_dir bdsmail
  207. if [ -d $temp_restore_dir ]; then
  208. if [ -d $temp_restore_dir$bdsmail_dir ]; then
  209. cp -rp $temp_restore_dir$bdsmail_dir $bdsmail_dir/
  210. else
  211. if [ ! -d $bdsmail_dir ]; then
  212. mkdir $bdsmail_dir
  213. fi
  214. cp -rp $temp_restore_dir/* $bdsmail_dir
  215. fi
  216. chown -R i2psvc:i2psvc $bdsmail_dir
  217. rm -rf $temp_restore_dir
  218. fi
  219. systemctl start bdsmail
  220. }
  221. function remove_bdsmail {
  222. if [ -f /etc/systemd/system/bdsmail.service ]; then
  223. systemctl stop bdsmail
  224. systemctl disable bdsmail
  225. rm /etc/systemd/system/bdsmail.service
  226. fi
  227. for d in /home/*/ ; do
  228. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  229. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  230. # remove the user from the i2p group
  231. deluser "$USERNAME" i2psvc
  232. # Remove mutt folder hook to the i2p config
  233. if [ -f "/home/$USERNAME/.muttrc" ]; then
  234. if grep -q 'folder-hook !i2p' "/home/$USERNAME/.muttrc"; then
  235. sed -i '/folder-hook !i2p/d' "/home/$USERNAME/.muttrc"
  236. fi
  237. if grep -q 'folder-hook i2p' "/home/$USERNAME/.muttrc"; then
  238. sed -i '/folder-hook i2p/d' "/home/$USERNAME/.muttrc"
  239. fi
  240. fi
  241. # Remove folder
  242. if grep -q ' =i2p' "/home/$USERNAME/.muttrc"; then
  243. sed -i 's| =i2p||g' "/home/$USERNAME/.muttrc"
  244. fi
  245. # NOTE: leave Maildir/i2p/cur. We might want to archive that
  246. # or just be reinstalling the system without losing mail
  247. rm -rf "/home/$USERNAME/Maildir/i2p/new"
  248. fi
  249. done
  250. remove_i2p
  251. remove_app bdsmail
  252. remove_completion_param install_bdsmail
  253. sed -i '/bdsmail/d' "$COMPLETION_FILE"
  254. rm -rf /etc/skel/.mutt
  255. if [ -d $BDSMAIL_DIR ]; then
  256. rm -rf $BDSMAIL_DIR
  257. fi
  258. rm /usr/bin/bdsmail_domain
  259. sed -i '/bdsmail_distribute/d' /etc/crontab
  260. rm /usr/bin/bdsmail_distribute
  261. if [ -f /etc/skel/.mutt/bdsmail ]; then
  262. rm /etc/skel/.mutt/bdsmail
  263. fi
  264. }
  265. function install_bdsmail {
  266. if [ -d $BDSMAIL_DIR ]; then
  267. remove_bdsmail
  268. fi
  269. if [ -d /repos/bdsmail ]; then
  270. mkdir $BDSMAIL_DIR
  271. cp -r -p /repos/bdsmail/. $BDSMAIL_DIR
  272. cd $BDSMAIL_DIR || exit 24687246
  273. git pull
  274. else
  275. git_clone "$BDSMAIL_REPO" "$BDSMAIL_DIR"
  276. fi
  277. if [ ! -d $BDSMAIL_DIR ]; then
  278. echo $'Unable to clone bdsmail repo'
  279. exit 5735735
  280. fi
  281. cd $BDSMAIL_DIR || exit 2468246
  282. git checkout "$BDSMAIL_COMMIT" -b "$BDSMAIL_COMMIT"
  283. set_completion_param "bdsmail commit" "$BDSMAIL_COMMIT"
  284. mkdir -p $BDSMAIL_DIR/Maildir/i2p
  285. chmod -R 700 $BDSMAIL_DIR/Maildir
  286. make GOROOT="/home/go/go${GO_VERSION}"
  287. if [ ! -f $BDSMAIL_DIR/bin/bdsconfig ]; then
  288. echo $'Unable to make bdsmail'
  289. exit 87923567842
  290. fi
  291. install_i2p
  292. i2p_enable_sam
  293. # create configuration file
  294. #$BDSMAIL_DIR/bin/bdsconfig > $BDSMAIL_DIR/config.ini
  295. { echo '[maild]':
  296. echo "i2paddr = 127.0.0.1:$I2P_SAM_PORT";
  297. echo 'i2pkeyfile = bdsmail-privkey.dat';
  298. echo "bindmail = 127.0.0.1:$I2P_SMTP_PORT";
  299. echo "bindweb = 127.0.0.1:$I2P_WEB_PORT";
  300. echo "bindpop3 = 127.0.0.1:$I2P_POP3_PORT";
  301. echo 'domain = localhost';
  302. echo 'maildir = Maildir/i2p';
  303. echo 'database = localhost.sqlite';
  304. echo 'assets = contrib/assets/web'; } > $BDSMAIL_DIR/config.ini
  305. { echo '[Unit]';
  306. echo 'Description=bdsmail';
  307. echo 'After=syslog.target';
  308. echo 'After=network.target';
  309. echo '';
  310. echo '[Service]';
  311. echo 'Type=simple';
  312. echo 'User=i2psvc';
  313. echo 'Group=i2psvc';
  314. echo "WorkingDirectory=$BDSMAIL_DIR";
  315. echo "ExecStart=$BDSMAIL_DIR/bin/maild $BDSMAIL_DIR/config.ini";
  316. echo 'Restart=always';
  317. echo 'Environment="USER=i2psvc"';
  318. echo '';
  319. echo '[Install]';
  320. echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/bdsmail.service
  321. { echo '#!/usr/bin/env python2';
  322. echo 'import base64, hashlib, sys';
  323. echo 'with open(sys.argv[1]) as f:';
  324. echo " print(base64.b32encode(hashlib.sha256(base64.b64decode(f.read(516), '-~')).digest()).strip('=')+\".b32.i2p\")"; } > $BDSMAIL_DIR/get_address
  325. chmod +x $BDSMAIL_DIR/get_address
  326. chown -R i2psvc:i2psvc $BDSMAIL_DIR
  327. systemctl enable bdsmail
  328. systemctl start bdsmail
  329. { echo '#!/bin/bash';
  330. echo "BDSMAIL_DIR=$BDSMAIL_DIR";
  331. echo "MAIL_DIR=\$BDSMAIL_DIR/\$(cat \$BDSMAIL_DIR/config.ini | grep 'maildir =' | awk -F ' ' '{print \$3}')";
  332. echo "if [ ! -d \$MAIL_DIR/postmaster/new ]; then";
  333. echo ' exit 0';
  334. echo 'fi';
  335. echo "for filename in \$MAIL_DIR/postmaster/new/*; do";
  336. echo " to_line=\$(cat \$filename | grep \"To: \" | head -n 1)";
  337. echo " to_username=\$(echo \"\$to_line\" | awk -F ' ' '{print \$2}' | awk -F '@' '{print \$1}')";
  338. echo " if [ -d /home/\$to_username/Maildir/i2p/new ]; then";
  339. echo " chown \$to_username:\$to_username \$filename";
  340. echo " chmod 600 \$filename";
  341. echo " mv \$filename /home/\$to_username/Maildir/i2p/new";
  342. echo ' fi';
  343. echo 'done'; } > /usr/bin/bdsmail_distribute
  344. chmod +x /usr/bin/bdsmail_distribute
  345. if ! grep -q 'bdsmail_distribute' /etc/crontab; then
  346. cron_add_mins 1 '/usr/bin/bdsmail_distribute 2> /dev/null'
  347. fi
  348. { echo '#!/bin/bash';
  349. echo "cd $BDSMAIL_DIR";
  350. echo 'if [ ! -f bdsmail-privkey.dat ]; then';
  351. echo ' exit 1';
  352. echo 'fi';
  353. echo "python2 get_address bdsmail-privkey.dat | tr '[:upper:]' '[:lower:]'"; } > /usr/bin/bdsmail_domain
  354. chmod +x /usr/bin/bdsmail_domain
  355. echo ''
  356. echo $'Now we will wait for i2p to connect and a private key to be generated'
  357. echo $'This may take a while.'
  358. echo ''
  359. bdsmail_wait_for_key_generation
  360. if [ ! "$bds_domain" ]; then
  361. systemctl stop bdsmail
  362. systemctl disable bdsmail
  363. remove_i2p
  364. echo $'Failed to get the bdsmail domain'
  365. exit 8934638
  366. fi
  367. BDSMAIL_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  368. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a bdsmail -p "$BDSMAIL_PASSWORD"
  369. cd $BDSMAIL_DIR || exit 2346872427
  370. $BDSMAIL_DIR/bin/mailtool $BDSMAIL_DIR/config.ini admin $BDSMAIL_DIR/Maildir/i2p/admin "$BDSMAIL_PASSWORD"
  371. # Create mutt configuration
  372. if [ ! -d /etc/skel/.mutt ]; then
  373. mkdir /etc/skel/.mutt
  374. fi
  375. { echo 'set mbox_type=Maildir';
  376. echo "set smtp_url=smtp://username:password@127.0.0.1:$I2P_SMTP_PORT/";
  377. echo 'set use_from=yes';
  378. echo "set from=username@${bds_domain}";
  379. echo "set spoolfile=~/Maildir/i2p";
  380. echo 'set pgp_autoencrypt=no';
  381. echo 'set pgp_replyencrypt=no';
  382. echo 'set pgp_autosign=no';
  383. echo 'set pgp_replysign=no'; } > /etc/skel/.mutt/bdsmail
  384. # mutt configuration for the admin user
  385. if [ ! -d "/home/$MY_USERNAME/.mutt" ]; then
  386. mkdir "/home/$MY_USERNAME/.mutt"
  387. fi
  388. cp /etc/skel/.mutt/bdsmail "/home/$MY_USERNAME/.mutt"
  389. sed -i "s|username|$MY_USERNAME|g" "/home/$MY_USERNAME/.mutt/bdsmail"
  390. sed -i "s|password|$BDSMAIL_PASSWORD|g" "/home/$MY_USERNAME/.mutt/bdsmail"
  391. chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.mutt"
  392. bdsmail_configure_users
  393. cd $BDSMAIL_DIR || exit 2457245
  394. $BDSMAIL_DIR/bin/mailtool $BDSMAIL_DIR/config.ini "$MY_USERNAME" "/home/$MY_USERNAME/Maildir/i2p" "$BDSMAIL_PASSWORD"
  395. APP_INSTALLED=1
  396. }
  397. # NOTE: deliberately there is no "exit 0"