freedombone-app-bdsmail 15KB

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