freedombone-app-bdsmail 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 logging_on_bdsmail {
  37. echo -n ''
  38. }
  39. function logging_off_bdsmail {
  40. echo -n ''
  41. }
  42. function remove_user_bdsmail {
  43. remove_username="$1"
  44. }
  45. function add_user_bdsmail {
  46. new_username="$1"
  47. new_user_password="$2"
  48. if [ ! -d /home/$new_username/.mutt ]; then
  49. mkdir /home/$new_username/.mutt
  50. cp /etc/skel/.mutt/bdsmail /home/$new_username/.mutt
  51. fi
  52. sed -i "s|username|$new_username|g" /home/$new_username/.mutt/bdsmail
  53. chown -R $new_username:$new_username /home/$new_username/.mutt
  54. echo '0'
  55. }
  56. function install_interactive_bdsmail {
  57. echo -n ''
  58. APP_INSTALLED=1
  59. }
  60. function change_password_bdsmail {
  61. curr_username="$1"
  62. new_user_password="$2"
  63. }
  64. function reconfigure_bdsmail {
  65. # This is used if you need to switch identity. Dump old keys and generate new ones
  66. echo -n ''
  67. }
  68. function upgrade_bdsmail {
  69. CURR_BDSMAIL_COMMIT=$(get_completion_param "bdsmail commit")
  70. if [[ "$CURR_BDSMAIL_COMMIT" == "$BDSMAIL_COMMIT" ]]; then
  71. return
  72. fi
  73. # update to the next commit
  74. set_repo_commit /etc/bdsmail "bdsmail commit" "$BDSMAIL_COMMIT" $BDSMAIL_REPO
  75. chown -R bdsmail:bdsmail /etc/bdsmail
  76. }
  77. function backup_local_bdsmail {
  78. systemctl stop bdsmail
  79. source_directory=/etc/bdsmail
  80. function_check backup_directory_to_usb
  81. dest_directory=bdsmail
  82. backup_directory_to_usb $source_directory $dest_directory
  83. systemctl start bdsmail
  84. }
  85. function restore_local_bdsmail {
  86. systemctl stop bdsmail
  87. temp_restore_dir=/root/tempbdsmail
  88. bdsmail_dir=/etc/bdsmail
  89. function_check restore_directory_from_usb
  90. restore_directory_from_usb $temp_restore_dir bdsmail
  91. if [ -d $temp_restore_dir ]; then
  92. if [ -d cp $temp_restore_dir$bdsmail_dir ]; then
  93. cp -rp $temp_restore_dir$bdsmail_dir $bdsmail_dir/
  94. else
  95. if [ ! -d $bdsmail_dir ]; then
  96. mkdir $bdsmail_dir
  97. fi
  98. cp -rp $temp_restore_dir/* $bdsmail_dir
  99. fi
  100. chown -R bdsmail:bdsmail $bdsmail_dir
  101. rm -rf $temp_restore_dir
  102. fi
  103. systemctl start bdsmail
  104. }
  105. function backup_remote_bdsmail {
  106. systemctl stop bdsmail
  107. source_directory=/etc/bdsmail
  108. function_check backup_directory_to_friend
  109. dest_directory=bdsmail
  110. backup_directory_to_friend $source_directory $dest_directory
  111. systemctl start bdsmail
  112. }
  113. function restore_remote_bdsmail {
  114. systemctl stop bdsmail
  115. temp_restore_dir=/root/tempbdsmail
  116. bdsmail_dir=/etc/bdsmail
  117. function_check restore_directory_from_friend
  118. restore_directory_from_friend $temp_restore_dir bdsmail
  119. if [ -d $temp_restore_dir ]; then
  120. if [ -d cp $temp_restore_dir$bdsmail_dir ]; then
  121. cp -rp $temp_restore_dir$bdsmail_dir $bdsmail_dir/
  122. else
  123. if [ ! -d $bdsmail_dir ]; then
  124. mkdir $bdsmail_dir
  125. fi
  126. cp -rp $temp_restore_dir/* $bdsmail_dir
  127. fi
  128. chown -R bdsmail:bdsmail $bdsmail_dir
  129. rm -rf $temp_restore_dir
  130. fi
  131. systemctl start bdsmail
  132. }
  133. function remove_bdsmail {
  134. if [ -f /etc/systemd/system/bdsmail.service ]; then
  135. systemctl stop bdsmail
  136. systemctl disable bdsmail
  137. rm /etc/systemd/system/bdsmail.service
  138. fi
  139. userdel -r bdsmail
  140. remove_app bdsmail
  141. remove_completion_param install_bdsmail
  142. sed -i '/bdsmail/d' $COMPLETION_FILE
  143. rm -rf /etc/skel/.mutt
  144. if [ -d /etc/bdsmail ]; then
  145. rm -rf /etc/bdsmail
  146. fi
  147. remove_i2p
  148. }
  149. function install_bdsmail {
  150. if [ -d /etc/bdsmail ]; then
  151. remove_bdsmail
  152. fi
  153. if [ -d /repos/bdsmail ]; then
  154. mkdir /etc/bdsmail
  155. cp -r -p /repos/bdsmail/. /etc/bdsmail
  156. cd /etc/bdsmail
  157. git pull
  158. else
  159. git_clone $BDSMAIL_REPO /etc/bdsmail
  160. fi
  161. if [ ! -d /etc/bdsmail ]; then
  162. echo $'Unable to clone bdsmail repo'
  163. exit 5735735
  164. fi
  165. cd /etc/bdsmail
  166. git checkout $BDSMAIL_COMMIT -b $BDSMAIL_COMMIT
  167. set_completion_param "bdsmail commit" "$BDSMAIL_COMMIT"
  168. useradd -d /etc/bdsmail -s /bin/false bdsmail
  169. make GOROOT=/home/go/go${GO_VERSION}
  170. if [ ! -f /etc/bdsmail/bin/bdsconfig ]; then
  171. echo $'Unable to make bdsmail'
  172. exit 87923567842
  173. fi
  174. install_i2p
  175. # create configuration file
  176. /etc/bdsmail/bin/bdsconfig > /etc/bdsmail/config.ini
  177. echo '[maild]' > /etc/bdsmail/config.ini
  178. echo 'i2paddr = 127.0.0.1:7656' >> /etc/bdsmail/config.ini
  179. echo 'i2pkeyfile = bdsmail-privkey.dat' >> /etc/bdsmail/config.ini
  180. echo 'bindmail = 127.0.0.1:2525' >> /etc/bdsmail/config.ini
  181. echo 'bindweb = 127.0.0.1:8489' >> /etc/bdsmail/config.ini
  182. echo 'domain = localhost' >> /etc/bdsmail/config.ini
  183. echo 'maildir = mail' >> /etc/bdsmail/config.ini
  184. echo 'database = localhost.sqlite' >> /etc/bdsmail/config.ini
  185. echo 'assets = contrib/assets/web' >> /etc/bdsmail/config.ini
  186. echo '[Unit]' > /etc/systemd/system/bdsmail.service
  187. echo 'Description=bdsmail' >> /etc/systemd/system/bdsmail.service
  188. echo 'After=syslog.target' >> /etc/systemd/system/bdsmail.service
  189. echo 'After=network.target' >> /etc/systemd/system/bdsmail.service
  190. echo '' >> /etc/systemd/system/bdsmail.service
  191. echo '[Service]' >> /etc/systemd/system/bdsmail.service
  192. echo 'Type=simple' >> /etc/systemd/system/bdsmail.service
  193. echo 'User=bdsmail' >> /etc/systemd/system/bdsmail.service
  194. echo 'Group=bdsmail' >> /etc/systemd/system/bdsmail.service
  195. echo 'WorkingDirectory=/etc/bdsmail' >> /etc/systemd/system/bdsmail.service
  196. echo 'ExecStart=/etc/bdsmail/bin/maild /etc/bdsmail/config.ini' >> /etc/systemd/system/bdsmail.service
  197. echo 'Restart=always' >> /etc/systemd/system/bdsmail.service
  198. echo 'Environment="USER=bdsmail"' >> /etc/systemd/system/bdsmail.service
  199. echo '' >> /etc/systemd/system/bdsmail.service
  200. echo '[Install]' >> /etc/systemd/system/bdsmail.service
  201. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/bdsmail.service
  202. systemctl enable bdsmail
  203. chown -R bdsmail:bdsmail /etc/bdsmail
  204. systemctl start bdsmail
  205. echo '#!/usr/bin/env python2' > /etc/bdsmail/get_address
  206. echo 'import base64, hashlib, sys' >> /etc/bdsmail/get_address
  207. echo 'with open(sys.argv[1]) as f:' >> /etc/bdsmail/get_address
  208. echo " print(base64.b32encode(hashlib.sha256(base64.b64decode(f.read(516), '-~')).digest()).strip('=')+\".b32.i2p\")" >> /etc/bdsmail/get_address
  209. chmod +x /etc/bdsmail/get_address
  210. echo '#!/bin/bash' > /usr/bin/bdsmail_domain
  211. echo 'cd /etc/bdsmail' >> /usr/bin/bdsmail_domain
  212. echo 'python2 get_address bdsmail-privkey.dat' >> /usr/bin/bdsmail_domain
  213. chmod +x /usr/bin/bdsmail_domain
  214. # wait for domain to be generated by the daemon
  215. echo $'Waiting for i2p domain to be generated...'
  216. sleep 20
  217. bds_domain=$(bdsmail_domain)
  218. if [ ! $bds_domain ]; then
  219. echo $'Waiting...'
  220. sleep 20
  221. bds_domain=$(bdsmail_domain)
  222. if [ ! $bds_domain ]; then
  223. #systemctl stop bdsmail
  224. #systemctl disable bdsmail
  225. #remove_i2p
  226. echo $'Failed to get the bdsmail domain'
  227. exit 8934638
  228. fi
  229. fi
  230. # Create mutt configuration
  231. mkdir /etc/skel/.mutt
  232. echo 'set mbox_type=Maildir' > /etc/skel/.mutt/bdsmail
  233. echo 'set smtp_url=smtp://127.0.0.1:2525/' >> /etc/skel/.mutt/bdsmail
  234. echo 'set use_from=yes' >> /etc/skel/.mutt/bdsmail
  235. echo "set from=username@${bds_domain}" >> /etc/skel/.mutt/bdsmail
  236. echo 'set spoolfile=/etc/bdsmail/mail/username' >> /etc/skel/.mutt/bdsmail
  237. # mutt configuration for the admin user
  238. if [ ! -d /home/$MY_USERNAME/.mutt ]; then
  239. mkdir /home/$MY_USERNAME/.mutt
  240. fi
  241. cp /etc/skel/.mutt/bdsmail /home/$MY_USERNAME/.mutt
  242. sed -i "s|username|$MY_USERNAME|g" /home/$MY_USERNAME/.mutt/bdsmail
  243. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.mutt
  244. APP_INSTALLED=1
  245. }
  246. # NOTE: deliberately there is no "exit 0"