freedombone-app-bdsmail 14KB

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