freedombone-app-bdsmail 15KB

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