freedombone-app-bdsmail 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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='6a2296b0b8e6c3da61081b85802e7b1cc88ca285'
  35. bdsmail=(MY_USERNAME)
  36. function bdsmail_configure_users {
  37. for d in /home/*/ ; do
  38. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  39. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  40. # Add the user to the i2p group
  41. usermod -a -G i2psvc $USERNAME
  42. if [ -f /home/$USERNAME/.muttrc ]; then
  43. # Create a mutt i2p folder
  44. if ! grep -q ' =i2p' /home/$USERNAME/.muttrc; then
  45. MUTT_MAILBOXES=$(grep "mailboxes =" /home/$USERNAME/.muttrc)
  46. sed -i "s|$MUTT_MAILBOXES|$MUTT_MAILBOXES =i2p|g" /home/$USERNAME/.muttrc
  47. fi
  48. # Create a mutt folder hook to the i2p config
  49. if ! grep -q 'folder-hook !i2p' /home/$USERNAME/.muttrc; then
  50. echo 'folder-hook !i2p/* source ~/.muttrc' >> /home/$USERNAME/.muttrc
  51. fi
  52. if ! grep -q 'folder-hook i2p' /home/$USERNAME/.muttrc; then
  53. echo 'folder-hook i2p/* source ~/.mutt/bdsmail' >> /home/$USERNAME/.muttrc
  54. fi
  55. fi
  56. # Create a directory where i2p mail will be stored
  57. if [ ! -d /home/$USERNAME/Maildir/i2p/cur ]; then
  58. mkdir -p /home/$USERNAME/Maildir/i2p/cur
  59. mkdir -p /home/$USERNAME/Maildir/i2p/new
  60. chown -R $USERNAME:$USERNAME /home/$USERNAME/Maildir/i2p
  61. fi
  62. fi
  63. done
  64. }
  65. function logging_on_bdsmail {
  66. echo -n ''
  67. }
  68. function logging_off_bdsmail {
  69. echo -n ''
  70. }
  71. function remove_user_bdsmail {
  72. remove_username="$1"
  73. }
  74. function add_user_bdsmail {
  75. new_username="$1"
  76. new_user_password="$2"
  77. if [ ! -d /home/$new_username/.mutt ]; then
  78. mkdir /home/$new_username/.mutt
  79. cp /etc/skel/.mutt/bdsmail /home/$new_username/.mutt
  80. fi
  81. sed -i "s|username|$new_username|g" /home/$new_username/.mutt/bdsmail
  82. bdsmail_configure_users
  83. chown -R $new_username:$new_username /home/$new_username/.mutt
  84. echo '0'
  85. }
  86. function install_interactive_bdsmail {
  87. echo -n ''
  88. APP_INSTALLED=1
  89. }
  90. function change_password_bdsmail {
  91. curr_username="$1"
  92. new_user_password="$2"
  93. }
  94. function reconfigure_bdsmail {
  95. # This is used if you need to switch identity. Dump old keys and generate new ones
  96. echo -n ''
  97. }
  98. function upgrade_bdsmail {
  99. CURR_BDSMAIL_COMMIT=$(get_completion_param "bdsmail commit")
  100. if [[ "$CURR_BDSMAIL_COMMIT" == "$BDSMAIL_COMMIT" ]]; then
  101. return
  102. fi
  103. # update to the next commit
  104. set_repo_commit /etc/bdsmail "bdsmail commit" "$BDSMAIL_COMMIT" $BDSMAIL_REPO
  105. chown -R i2psvc:i2psvc /etc/bdsmail
  106. }
  107. function backup_local_bdsmail {
  108. systemctl stop bdsmail
  109. source_directory=/etc/bdsmail
  110. function_check backup_directory_to_usb
  111. dest_directory=bdsmail
  112. backup_directory_to_usb $source_directory $dest_directory
  113. systemctl start bdsmail
  114. }
  115. function restore_local_bdsmail {
  116. systemctl stop bdsmail
  117. temp_restore_dir=/root/tempbdsmail
  118. bdsmail_dir=/etc/bdsmail
  119. function_check restore_directory_from_usb
  120. restore_directory_from_usb $temp_restore_dir bdsmail
  121. if [ -d $temp_restore_dir ]; then
  122. if [ -d cp $temp_restore_dir$bdsmail_dir ]; then
  123. cp -rp $temp_restore_dir$bdsmail_dir $bdsmail_dir/
  124. else
  125. if [ ! -d $bdsmail_dir ]; then
  126. mkdir $bdsmail_dir
  127. fi
  128. cp -rp $temp_restore_dir/* $bdsmail_dir
  129. fi
  130. chown -R i2psvc:i2psvc $bdsmail_dir
  131. rm -rf $temp_restore_dir
  132. fi
  133. systemctl start bdsmail
  134. }
  135. function backup_remote_bdsmail {
  136. systemctl stop bdsmail
  137. source_directory=/etc/bdsmail
  138. function_check backup_directory_to_friend
  139. dest_directory=bdsmail
  140. backup_directory_to_friend $source_directory $dest_directory
  141. systemctl start bdsmail
  142. }
  143. function restore_remote_bdsmail {
  144. systemctl stop bdsmail
  145. temp_restore_dir=/root/tempbdsmail
  146. bdsmail_dir=/etc/bdsmail
  147. function_check restore_directory_from_friend
  148. restore_directory_from_friend $temp_restore_dir bdsmail
  149. if [ -d $temp_restore_dir ]; then
  150. if [ -d cp $temp_restore_dir$bdsmail_dir ]; then
  151. cp -rp $temp_restore_dir$bdsmail_dir $bdsmail_dir/
  152. else
  153. if [ ! -d $bdsmail_dir ]; then
  154. mkdir $bdsmail_dir
  155. fi
  156. cp -rp $temp_restore_dir/* $bdsmail_dir
  157. fi
  158. chown -R i2psvc:i2psvc $bdsmail_dir
  159. rm -rf $temp_restore_dir
  160. fi
  161. systemctl start bdsmail
  162. }
  163. function remove_bdsmail {
  164. if [ -f /etc/systemd/system/bdsmail.service ]; then
  165. systemctl stop bdsmail
  166. systemctl disable bdsmail
  167. rm /etc/systemd/system/bdsmail.service
  168. fi
  169. for d in /home/*/ ; do
  170. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  171. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  172. # remove the user from the i2p group
  173. deluser $USERNAME i2psvc
  174. # Remove mutt folder hook to the i2p config
  175. if [ -f /home/$USERNAME/.muttrc ]; then
  176. if grep -q 'folder-hook !i2p' /home/$USERNAME/.muttrc; then
  177. sed -i '/folder-hook !i2p/d' /home/$USERNAME/.muttrc
  178. fi
  179. if grep -q 'folder-hook i2p' /home/$USERNAME/.muttrc; then
  180. sed -i '/folder-hook i2p/d' /home/$USERNAME/.muttrc
  181. fi
  182. fi
  183. # Remove folder
  184. if grep -q ' =i2p' /home/$USERNAME/.muttrc; then
  185. sed -i 's| =i2p||g' /home/$USERNAME/.muttrc
  186. fi
  187. # NOTE: leave Maildir/i2p. We might want to archive that
  188. # or just be reinstalling the system without losing mail
  189. fi
  190. done
  191. remove_i2p
  192. remove_app bdsmail
  193. remove_completion_param install_bdsmail
  194. sed -i '/bdsmail/d' $COMPLETION_FILE
  195. rm -rf /etc/skel/.mutt
  196. if [ -d /etc/bdsmail ]; then
  197. rm -rf /etc/bdsmail
  198. fi
  199. }
  200. function install_bdsmail {
  201. if [ -d /etc/bdsmail ]; then
  202. remove_bdsmail
  203. fi
  204. if [ -d /repos/bdsmail ]; then
  205. mkdir /etc/bdsmail
  206. cp -r -p /repos/bdsmail/. /etc/bdsmail
  207. cd /etc/bdsmail
  208. git pull
  209. else
  210. git_clone $BDSMAIL_REPO /etc/bdsmail
  211. fi
  212. if [ ! -d /etc/bdsmail ]; then
  213. echo $'Unable to clone bdsmail repo'
  214. exit 5735735
  215. fi
  216. cd /etc/bdsmail
  217. git checkout $BDSMAIL_COMMIT -b $BDSMAIL_COMMIT
  218. set_completion_param "bdsmail commit" "$BDSMAIL_COMMIT"
  219. make GOROOT=/home/go/go${GO_VERSION}
  220. if [ ! -f /etc/bdsmail/bin/bdsconfig ]; then
  221. echo $'Unable to make bdsmail'
  222. exit 87923567842
  223. fi
  224. install_i2p
  225. i2p_enable_sam
  226. # create configuration file
  227. /etc/bdsmail/bin/bdsconfig > /etc/bdsmail/config.ini
  228. echo '[maild]' > /etc/bdsmail/config.ini
  229. echo 'i2paddr = 127.0.0.1:7656' >> /etc/bdsmail/config.ini
  230. echo 'i2pkeyfile = bdsmail-privkey.dat' >> /etc/bdsmail/config.ini
  231. echo 'bindmail = 127.0.0.1:2525' >> /etc/bdsmail/config.ini
  232. echo 'bindweb = 127.0.0.1:8489' >> /etc/bdsmail/config.ini
  233. echo 'domain = localhost' >> /etc/bdsmail/config.ini
  234. echo 'maildir = mail' >> /etc/bdsmail/config.ini
  235. echo 'database = localhost.sqlite' >> /etc/bdsmail/config.ini
  236. echo 'assets = contrib/assets/web' >> /etc/bdsmail/config.ini
  237. echo '[Unit]' > /etc/systemd/system/bdsmail.service
  238. echo 'Description=bdsmail' >> /etc/systemd/system/bdsmail.service
  239. echo 'After=syslog.target' >> /etc/systemd/system/bdsmail.service
  240. echo 'After=network.target' >> /etc/systemd/system/bdsmail.service
  241. echo '' >> /etc/systemd/system/bdsmail.service
  242. echo '[Service]' >> /etc/systemd/system/bdsmail.service
  243. echo 'Type=simple' >> /etc/systemd/system/bdsmail.service
  244. echo 'User=i2psvc' >> /etc/systemd/system/bdsmail.service
  245. echo 'Group=i2psvc' >> /etc/systemd/system/bdsmail.service
  246. echo 'WorkingDirectory=/etc/bdsmail' >> /etc/systemd/system/bdsmail.service
  247. echo 'ExecStart=/etc/bdsmail/bin/maild /etc/bdsmail/config.ini' >> /etc/systemd/system/bdsmail.service
  248. echo 'Restart=always' >> /etc/systemd/system/bdsmail.service
  249. echo 'Environment="USER=i2psvc"' >> /etc/systemd/system/bdsmail.service
  250. echo '' >> /etc/systemd/system/bdsmail.service
  251. echo '[Install]' >> /etc/systemd/system/bdsmail.service
  252. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/bdsmail.service
  253. echo '#!/usr/bin/env python2' > /etc/bdsmail/get_address
  254. echo 'import base64, hashlib, sys' >> /etc/bdsmail/get_address
  255. echo 'with open(sys.argv[1]) as f:' >> /etc/bdsmail/get_address
  256. echo " print(base64.b32encode(hashlib.sha256(base64.b64decode(f.read(516), '-~')).digest()).strip('=')+\".b32.i2p\")" >> /etc/bdsmail/get_address
  257. chmod +x /etc/bdsmail/get_address
  258. chown -R i2psvc:i2psvc /etc/bdsmail
  259. systemctl enable bdsmail
  260. systemctl start bdsmail
  261. echo '#!/bin/bash' > /usr/bin/bdsmail_domain
  262. echo 'cd /etc/bdsmail' >> /usr/bin/bdsmail_domain
  263. echo 'if [ ! -f bdsmail-privkey.dat ]; then' >> /usr/bin/bdsmail_domain
  264. echo ' exit 1' >> /usr/bin/bdsmail_domain
  265. echo 'fi' >> /usr/bin/bdsmail_domain
  266. echo 'python2 get_address bdsmail-privkey.dat' >> /usr/bin/bdsmail_domain
  267. chmod +x /usr/bin/bdsmail_domain
  268. echo ''
  269. echo $'Now we will wait for i2p to connect and a private key to be generated'
  270. echo $'This may take a while.'
  271. echo ''
  272. # wait for domain to be generated by the daemon
  273. # This can take a while, probably because i2p is connecting
  274. bds_domain=
  275. sleep_ctr=0
  276. while [ ! $bds_domain ]; do
  277. echo $"Waiting for i2p domain to be generated ${sleep_ctr}/50"
  278. systemctl restart bdsmail
  279. sleep 10
  280. bds_domain=$(bdsmail_domain)
  281. sleep_ctr=$((sleep_ctr + 1))
  282. if [ $sleep_ctr -gt 50 ]; then
  283. break
  284. fi
  285. done
  286. if [ ! $bds_domain ]; then
  287. #systemctl stop bdsmail
  288. #systemctl disable bdsmail
  289. #remove_i2p
  290. echo $'Failed to get the bdsmail domain'
  291. exit 8934638
  292. fi
  293. # Create mutt configuration
  294. mkdir /etc/skel/.mutt
  295. echo 'set mbox_type=Maildir' > /etc/skel/.mutt/bdsmail
  296. echo 'set mbox="~/Maildir/i2p"' >> /etc/skel/.mutt/bdsmail
  297. echo 'set smtp_url=smtp://127.0.0.1:2525/' >> /etc/skel/.mutt/bdsmail
  298. echo 'set use_from=yes' >> /etc/skel/.mutt/bdsmail
  299. echo "set from=username@${bds_domain}" >> /etc/skel/.mutt/bdsmail
  300. echo 'set spoolfile=/etc/bdsmail/mail/username' >> /etc/skel/.mutt/bdsmail
  301. # mutt configuration for the admin user
  302. if [ ! -d /home/$MY_USERNAME/.mutt ]; then
  303. mkdir /home/$MY_USERNAME/.mutt
  304. fi
  305. cp /etc/skel/.mutt/bdsmail /home/$MY_USERNAME/.mutt
  306. sed -i "s|username|$MY_USERNAME|g" /home/$MY_USERNAME/.mutt/bdsmail
  307. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.mutt
  308. bdsmail_configure_users
  309. APP_INSTALLED=1
  310. }
  311. # NOTE: deliberately there is no "exit 0"