freedombone-app-bdsmail 16KB

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