| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 | 
							- #!/bin/bash
 - #
 - # .---.                  .              .
 - # |                      |              |
 - # |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
 - # |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
 - # '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
 - #
 - #                    Freedom in the Cloud
 - #
 - # Brain Dead Simple Mail Server for i2p
 - #
 - # License
 - # =======
 - #
 - # Copyright (C) 2018 Bob Mottram <bob@freedombone.net>
 - #
 - # This program is free software: you can redistribute it and/or modify
 - # it under the terms of the GNU Affero General Public License as published by
 - # the Free Software Foundation, either version 3 of the License, or
 - # (at your option) any later version.
 - #
 - # This program is distributed in the hope that it will be useful,
 - # but WITHOUT ANY WARRANTY; without even the implied warranty of
 - # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 - # GNU Affero General Public License for more details.
 - #
 - # You should have received a copy of the GNU Affero General Public License
 - # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 - 
 - VARIANTS='full full-vim'
 - 
 - IN_DEFAULT_INSTALL=0
 - SHOW_ON_ABOUT=1
 - 
 - BDSMAIL_REPO="https://github.com/majestrate/bdsmail"
 - BDSMAIL_COMMIT='6a2296b0b8e6c3da61081b85802e7b1cc88ca285'
 - 
 - bdsmail=(MY_USERNAME)
 - 
 - function logging_on_bdsmail {
 -     echo -n ''
 - }
 - 
 - function logging_off_bdsmail {
 -     echo -n ''
 - }
 - 
 - function remove_user_bdsmail {
 -     remove_username="$1"
 - }
 - 
 - function add_user_bdsmail {
 -     new_username="$1"
 -     new_user_password="$2"
 - 
 -     if [ ! -d /home/$new_username/.mutt ]; then
 -         mkdir /home/$new_username/.mutt
 -         cp /etc/skel/.mutt/bdsmail /home/$new_username/.mutt
 -     fi
 -     sed -i "s|username|$new_username|g" /home/$new_username/.mutt/bdsmail
 -     chown -R $new_username:$new_username /home/$new_username/.mutt
 -     echo '0'
 - }
 - 
 - function install_interactive_bdsmail {
 -     echo -n ''
 -     APP_INSTALLED=1
 - }
 - 
 - function change_password_bdsmail {
 -     curr_username="$1"
 -     new_user_password="$2"
 - }
 - 
 - function reconfigure_bdsmail {
 -     # This is used if you need to switch identity. Dump old keys and generate new ones
 -     echo -n ''
 - }
 - 
 - function upgrade_bdsmail {
 -     CURR_BDSMAIL_COMMIT=$(get_completion_param "bdsmail commit")
 -     if [[ "$CURR_BDSMAIL_COMMIT" == "$BDSMAIL_COMMIT" ]]; then
 -         return
 -     fi
 - 
 -     # update to the next commit
 -     set_repo_commit /etc/bdsmail "bdsmail commit" "$BDSMAIL_COMMIT" $BDSMAIL_REPO
 -     chown -R i2psvc:i2psvc /etc/bdsmail
 - }
 - 
 - function backup_local_bdsmail {
 -     systemctl stop bdsmail
 - 
 -     source_directory=/etc/bdsmail
 - 
 -     function_check backup_directory_to_usb
 -     dest_directory=bdsmail
 -     backup_directory_to_usb $source_directory $dest_directory
 - 
 -     systemctl start bdsmail
 - }
 - 
 - function restore_local_bdsmail {
 -     systemctl stop bdsmail
 - 
 -     temp_restore_dir=/root/tempbdsmail
 -     bdsmail_dir=/etc/bdsmail
 - 
 -     function_check restore_directory_from_usb
 -     restore_directory_from_usb $temp_restore_dir bdsmail
 -     if [ -d $temp_restore_dir ]; then
 -         if [ -d cp $temp_restore_dir$bdsmail_dir ]; then
 -             cp -rp $temp_restore_dir$bdsmail_dir $bdsmail_dir/
 -         else
 -             if [ ! -d $bdsmail_dir ]; then
 -                 mkdir $bdsmail_dir
 -             fi
 -             cp -rp $temp_restore_dir/* $bdsmail_dir
 -         fi
 -         chown -R i2psvc:i2psvc $bdsmail_dir
 -         rm -rf $temp_restore_dir
 -     fi
 - 
 -     systemctl start bdsmail
 - }
 - 
 - function backup_remote_bdsmail {
 -     systemctl stop bdsmail
 - 
 -     source_directory=/etc/bdsmail
 - 
 -     function_check backup_directory_to_friend
 -     dest_directory=bdsmail
 -     backup_directory_to_friend $source_directory $dest_directory
 - 
 -     systemctl start bdsmail
 - }
 - 
 - function restore_remote_bdsmail {
 -     systemctl stop bdsmail
 - 
 -     temp_restore_dir=/root/tempbdsmail
 -     bdsmail_dir=/etc/bdsmail
 - 
 -     function_check restore_directory_from_friend
 -     restore_directory_from_friend $temp_restore_dir bdsmail
 -     if [ -d $temp_restore_dir ]; then
 -         if [ -d cp $temp_restore_dir$bdsmail_dir ]; then
 -             cp -rp $temp_restore_dir$bdsmail_dir $bdsmail_dir/
 -         else
 -             if [ ! -d $bdsmail_dir ]; then
 -                 mkdir $bdsmail_dir
 -             fi
 -             cp -rp $temp_restore_dir/* $bdsmail_dir
 -         fi
 -         chown -R i2psvc:i2psvc $bdsmail_dir
 -         rm -rf $temp_restore_dir
 -     fi
 - 
 -     systemctl start bdsmail
 - }
 - 
 - function remove_bdsmail {
 -     if [ -f /etc/systemd/system/bdsmail.service ]; then
 -         systemctl stop bdsmail
 -         systemctl disable bdsmail
 -         rm /etc/systemd/system/bdsmail.service
 -     fi
 - 
 -     remove_i2p
 -     remove_app bdsmail
 -     remove_completion_param install_bdsmail
 -     sed -i '/bdsmail/d' $COMPLETION_FILE
 -     rm -rf /etc/skel/.mutt
 -     if [ -d /etc/bdsmail ]; then
 -         rm -rf /etc/bdsmail
 -     fi
 - }
 - 
 - function install_bdsmail {
 -     if [ -d /etc/bdsmail ]; then
 -         remove_bdsmail
 -     fi
 - 
 -     if [ -d /repos/bdsmail ]; then
 -         mkdir /etc/bdsmail
 -         cp -r -p /repos/bdsmail/. /etc/bdsmail
 -         cd /etc/bdsmail
 -         git pull
 -     else
 -         git_clone $BDSMAIL_REPO /etc/bdsmail
 -     fi
 - 
 -     if [ ! -d /etc/bdsmail ]; then
 -         echo $'Unable to clone bdsmail repo'
 -         exit 5735735
 -     fi
 - 
 -     cd /etc/bdsmail
 -     git checkout $BDSMAIL_COMMIT -b $BDSMAIL_COMMIT
 -     set_completion_param "bdsmail commit" "$BDSMAIL_COMMIT"
 - 
 -     make GOROOT=/home/go/go${GO_VERSION}
 -     if [ ! -f /etc/bdsmail/bin/bdsconfig ]; then
 -         echo $'Unable to make bdsmail'
 -         exit 87923567842
 -     fi
 - 
 -     install_i2p
 -     i2p_enable_sam
 - 
 -     # create configuration file
 -     /etc/bdsmail/bin/bdsconfig > /etc/bdsmail/config.ini
 -     echo '[maild]' > /etc/bdsmail/config.ini
 -     echo 'i2paddr = 127.0.0.1:7656' >> /etc/bdsmail/config.ini
 -     echo 'i2pkeyfile = bdsmail-privkey.dat' >> /etc/bdsmail/config.ini
 -     echo 'bindmail = 127.0.0.1:2525' >> /etc/bdsmail/config.ini
 -     echo 'bindweb = 127.0.0.1:8489' >> /etc/bdsmail/config.ini
 -     echo 'domain = localhost' >> /etc/bdsmail/config.ini
 -     echo 'maildir = mail' >> /etc/bdsmail/config.ini
 -     echo 'database = localhost.sqlite' >> /etc/bdsmail/config.ini
 -     echo 'assets = contrib/assets/web' >> /etc/bdsmail/config.ini
 - 
 -     echo '[Unit]' > /etc/systemd/system/bdsmail.service
 -     echo 'Description=bdsmail' >> /etc/systemd/system/bdsmail.service
 -     echo 'After=syslog.target' >> /etc/systemd/system/bdsmail.service
 -     echo 'After=network.target' >> /etc/systemd/system/bdsmail.service
 -     echo '' >> /etc/systemd/system/bdsmail.service
 -     echo '[Service]' >> /etc/systemd/system/bdsmail.service
 -     echo 'Type=simple' >> /etc/systemd/system/bdsmail.service
 -     echo 'User=i2psvc' >> /etc/systemd/system/bdsmail.service
 -     echo 'Group=i2psvc' >> /etc/systemd/system/bdsmail.service
 -     echo 'WorkingDirectory=/etc/bdsmail' >> /etc/systemd/system/bdsmail.service
 -     echo 'ExecStart=/etc/bdsmail/bin/maild /etc/bdsmail/config.ini' >> /etc/systemd/system/bdsmail.service
 -     echo 'Restart=always' >> /etc/systemd/system/bdsmail.service
 -     echo 'Environment="USER=i2psvc"' >> /etc/systemd/system/bdsmail.service
 -     echo '' >> /etc/systemd/system/bdsmail.service
 -     echo '[Install]' >> /etc/systemd/system/bdsmail.service
 -     echo 'WantedBy=multi-user.target' >> /etc/systemd/system/bdsmail.service
 - 
 -     echo '#!/usr/bin/env python2' > /etc/bdsmail/get_address
 -     echo 'import base64, hashlib, sys' >> /etc/bdsmail/get_address
 -     echo 'with open(sys.argv[1]) as f:' >> /etc/bdsmail/get_address
 -     echo "    print(base64.b32encode(hashlib.sha256(base64.b64decode(f.read(516), '-~')).digest()).strip('=')+\".b32.i2p\")" >> /etc/bdsmail/get_address
 -     chmod +x /etc/bdsmail/get_address
 - 
 -     chown -R i2psvc:i2psvc /etc/bdsmail
 -     systemctl enable bdsmail
 -     systemctl start bdsmail
 - 
 -     echo '#!/bin/bash' > /usr/bin/bdsmail_domain
 -     echo 'cd /etc/bdsmail' >> /usr/bin/bdsmail_domain
 -     echo 'python2 get_address bdsmail-privkey.dat' >> /usr/bin/bdsmail_domain
 -     chmod +x /usr/bin/bdsmail_domain
 - 
 -     # wait for domain to be generated by the daemon
 -     echo $'Waiting for i2p domain to be generated...'
 -     sleep 15
 -     bds_domain=$(bdsmail_domain)
 -     if [ ! $bds_domain ]; then
 -         echo $'Waiting...'
 -         systemctl restart bdsmail
 -         sleep 15
 -         bds_domain=$(bdsmail_domain)
 -         if [ ! $bds_domain ]; then
 -             #systemctl stop bdsmail
 -             #systemctl disable bdsmail
 -             #remove_i2p
 -             echo $'Failed to get the bdsmail domain'
 -             exit 8934638
 -         fi
 -     fi
 - 
 -     # Create mutt configuration
 -     mkdir /etc/skel/.mutt
 -     echo 'set mbox_type=Maildir' > /etc/skel/.mutt/bdsmail
 -     echo 'set smtp_url=smtp://127.0.0.1:2525/' >> /etc/skel/.mutt/bdsmail
 -     echo 'set use_from=yes' >> /etc/skel/.mutt/bdsmail
 -     echo "set from=username@${bds_domain}" >> /etc/skel/.mutt/bdsmail
 -     echo 'set spoolfile=/etc/bdsmail/mail/username' >> /etc/skel/.mutt/bdsmail
 - 
 -     # mutt configuration for the admin user
 -     if [ ! -d /home/$MY_USERNAME/.mutt ]; then
 -         mkdir /home/$MY_USERNAME/.mutt
 -     fi
 -     cp /etc/skel/.mutt/bdsmail /home/$MY_USERNAME/.mutt
 -     sed -i "s|username|$MY_USERNAME|g" /home/$MY_USERNAME/.mutt/bdsmail
 -     chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.mutt
 - 
 -     APP_INSTALLED=1
 - }
 - 
 - # NOTE: deliberately there is no "exit 0"
 
 
  |