Skip to content
Snippets Groups Projects
freedombone-app-bdsmail 14.8 KiB
Newer Older
Bob Mottram's avatar
Bob Mottram committed
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
Bob Mottram's avatar
Bob Mottram committed
#
# 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
NOT_ON_HOMEPAGE=1
Bob Mottram's avatar
Bob Mottram committed

BDSMAIL_REPO="https://github.com/majestrate/bdsmail"
Bob Mottram's avatar
Bob Mottram committed
BDSMAIL_COMMIT='6fae61365306ad3c1966a107aa06310ea65fb239'
Bob Mottram's avatar
Bob Mottram committed
BDSMAIL_DIR=/etc/bdsmail
Bob Mottram's avatar
Bob Mottram committed
I2P_SAM_PORT=7656
I2P_SMTP_PORT=2525
Bob Mottram's avatar
Bob Mottram committed
I2P_WEB_PORT=8489
I2P_POP3_PORT=1110
Bob Mottram's avatar
Bob Mottram committed

bdsmail=(MY_USERNAME)

function bdsmail_configure_users {
    for d in /home/*/ ; do
        USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
        if [[ $(is_valid_user "$USERNAME") == "1" ]]; then

            # Add the user to the i2p group
Bob Mottram's avatar
Bob Mottram committed
            usermod -a -G i2psvc "$USERNAME"
Bob Mottram's avatar
Bob Mottram committed
            if [ -f "/home/$USERNAME/.muttrc" ]; then
                # Create a mutt i2p folder
Bob Mottram's avatar
Bob Mottram committed
                if ! grep -q ' =i2p' "/home/$USERNAME/.muttrc"; then
                    MUTT_MAILBOXES=$(grep "mailboxes =" "/home/$USERNAME/.muttrc")
                    sed -i "s|$MUTT_MAILBOXES|$MUTT_MAILBOXES =i2p|g" "/home/$USERNAME/.muttrc"
                fi

                # Create a mutt folder hook to the i2p config
Bob Mottram's avatar
Bob Mottram committed
                if ! grep -q 'folder-hook !i2p' "/home/$USERNAME/.muttrc"; then
                    echo 'folder-hook !i2p/*     source ~/.muttrc' >> "/home/$USERNAME/.muttrc"
Bob Mottram's avatar
Bob Mottram committed
                if ! grep -q 'folder-hook i2p' "/home/$USERNAME/.muttrc"; then
                    echo 'folder-hook i2p/*     source ~/.mutt/bdsmail' >> "/home/$USERNAME/.muttrc"
                fi
            fi

            # Create a directory where i2p mail will be stored
Bob Mottram's avatar
Bob Mottram committed
            if [ ! -d "/home/$USERNAME/Maildir/i2p/new" ]; then
                mkdir -p "/home/$USERNAME/Maildir/i2p/cur"
                mkdir -p "/home/$USERNAME/Maildir/i2p/new"
                chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/Maildir/i2p"
Bob Mottram's avatar
Bob Mottram committed
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"

Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "/home/$new_username/.mutt" ]; then
        mkdir "/home/$new_username/.mutt"
        cp /etc/skel/.mutt/bdsmail "/home/$new_username/.mutt"
Bob Mottram's avatar
Bob Mottram committed
    fi
    read_config_param MY_USERNAME
Bob Mottram's avatar
Bob Mottram committed
    BDSMAIL_PASSWORD=$("${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a bdsmail)
    sed -i "s|username|$new_username|g" "/home/$new_username/.mutt/bdsmail"
    sed -i "s|password|$BDSMAIL_PASSWORD|g" "/home/$new_username/.mutt/bdsmail"
    bdsmail_configure_users
Bob Mottram's avatar
Bob Mottram committed
    cd $BDSMAIL_DIR || exit 57247684234
    $BDSMAIL_DIR/bin/mailtool $BDSMAIL_DIR/config.ini "$new_username" "/home/$new_username/Maildir/i2p" "$BDSMAIL_PASSWORD"
    chown -R "$new_username":"$new_username" "/home/$new_username/.mutt"
Bob Mottram's avatar
Bob Mottram committed
    echo '0'
}

function install_interactive_bdsmail {
    echo -n ''
    APP_INSTALLED=1
}

function change_password_bdsmail {
    curr_username="$1"
    new_user_password="$2"
Bob Mottram's avatar
Bob Mottram committed
    "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a bdsmail -p "$new_user_password"

    for d in /home/*/ ; do
        USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
        if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
Bob Mottram's avatar
Bob Mottram committed
            sed -i "s|set smtp_url=.*|set smtp_url=smtp://${curr_username}:${new_user_password}@127.0.0.1:$I2P_SMTP_PORT/" "/home/${USERNAME}/.mutt/bdsmail"
            sed -i "s|set from=.*|set from=${USERNAME}@$(bdsmail_domain)|g" "/home/${USERNAME}/.mutt/bdsmail"
            chown "${USERNAME}":"${USERNAME}" "/home/${USERNAME}/.mutt/bdsmail"
            cd $BDSMAIL_DIR || exit 2468246
Bob Mottram's avatar
Bob Mottram committed
            $BDSMAIL_DIR/bin/mailtool $BDSMAIL_DIR/config.ini "$curr_username" "/home/$curr_username/Maildir/i2p" "$new_user_password"
Bob Mottram's avatar
Bob Mottram committed
}

Bob Mottram's avatar
Bob Mottram committed
function bdsmail_update_domain {
    sed -i "s|set from=.*|set from=username@$(bdsmail_domain)|g" /etc/skel/.mutt/bdsmail
    for d in /home/*/ ; do
        USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
        if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
Bob Mottram's avatar
Bob Mottram committed
            cp /etc/skel/.mutt/bdsmail "/home/${USERNAME}/.mutt/bdsmail"
            sed -i "s|set from=.*|set from=${USERNAME}@$(bdsmail_domain)|g" "/home/${USERNAME}/.mutt/bdsmail"
            chown "${USERNAME}":"${USERNAME}" "/home/${USERNAME}/.mutt/bdsmail"
Bob Mottram's avatar
Bob Mottram committed
        fi
    done
}

function bdsmail_wait_for_key_generation {
    # wait for domain to be generated by the daemon
    # This can take a while, probably because i2p is connecting
    bds_domain=
    sleep_ctr=0
    while [ ! $bds_domain ]; do
        echo $"Waiting for i2p domain to be generated ${sleep_ctr}/100"
        systemctl restart bdsmail
        sleep 30
        bds_domain=$(bdsmail_domain)
        sleep_ctr=$((sleep_ctr + 1))
Bob Mottram's avatar
Bob Mottram committed
        if [ "$sleep_ctr" -gt 100 ]; then
Bob Mottram's avatar
Bob Mottram committed
function reconfigure_bdsmail {
Bob Mottram's avatar
Bob Mottram committed
    systemctl stop bdsmail
    rm $BDSMAIL_DIR/*.dat
    rm $BDSMAIL_DIR/*.pem
    bdsmail_wait_for_key_generation
    if [ ! -f $BDSMAIL_DIR/bdsmail-privkey.dat ]; then
        return
    fi
    bdsmail_update_domain
Bob Mottram's avatar
Bob Mottram committed
}

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
Bob Mottram's avatar
Bob Mottram committed
    set_repo_commit $BDSMAIL_DIR "bdsmail commit" "$BDSMAIL_COMMIT" "$BDSMAIL_REPO"
    cd $BDSMAIL_DIR || exit 2457245
    make GOROOT="/home/go/go${GO_VERSION}"
Bob Mottram's avatar
Bob Mottram committed
    chown -R i2psvc:i2psvc $BDSMAIL_DIR
Bob Mottram's avatar
Bob Mottram committed
    systemctl restart bdsmail

    if ! grep -q "$(bdsmail_domain)" /etc/skel/.mutt/bdsmail; then
        bdsmail_update_domain
    fi
Bob Mottram's avatar
Bob Mottram committed
}

function backup_local_bdsmail {
    systemctl stop bdsmail

Bob Mottram's avatar
Bob Mottram committed
    source_directory=$BDSMAIL_DIR
Bob Mottram's avatar
Bob Mottram committed

    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
Bob Mottram's avatar
Bob Mottram committed
    bdsmail_dir=$BDSMAIL_DIR
Bob Mottram's avatar
Bob Mottram committed

    function_check restore_directory_from_usb
    restore_directory_from_usb $temp_restore_dir bdsmail
    if [ -d $temp_restore_dir ]; then
Bob Mottram's avatar
Bob Mottram committed
        if [ -d $temp_restore_dir$bdsmail_dir ]; then
Bob Mottram's avatar
Bob Mottram committed
            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
Bob Mottram's avatar
Bob Mottram committed
        rm -rf $temp_restore_dir
    fi

    systemctl start bdsmail
}

function backup_remote_bdsmail {
    echo -n ''
Bob Mottram's avatar
Bob Mottram committed
}

function restore_remote_bdsmail {
    echo -n ''
Bob Mottram's avatar
Bob Mottram committed
}

function remove_bdsmail {
    if [ -f /etc/systemd/system/bdsmail.service ]; then
        systemctl stop bdsmail
        systemctl disable bdsmail
        rm /etc/systemd/system/bdsmail.service
    fi

    for d in /home/*/ ; do
        USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
        if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
            # remove the user from the i2p group
Bob Mottram's avatar
Bob Mottram committed
            deluser "$USERNAME" i2psvc
            # Remove mutt folder hook to the i2p config
Bob Mottram's avatar
Bob Mottram committed
            if [ -f "/home/$USERNAME/.muttrc" ]; then
                if grep -q 'folder-hook !i2p' "/home/$USERNAME/.muttrc"; then
                    sed -i '/folder-hook !i2p/d' "/home/$USERNAME/.muttrc"
Bob Mottram's avatar
Bob Mottram committed
                if grep -q 'folder-hook i2p' "/home/$USERNAME/.muttrc"; then
                    sed -i '/folder-hook i2p/d' "/home/$USERNAME/.muttrc"
                fi
            fi

            # Remove folder
Bob Mottram's avatar
Bob Mottram committed
            if grep -q ' =i2p' "/home/$USERNAME/.muttrc"; then
                sed -i 's| =i2p||g' "/home/$USERNAME/.muttrc"
Bob Mottram's avatar
Bob Mottram committed
            # NOTE: leave Maildir/i2p/cur. We might want to archive that
            # or just be reinstalling the system without losing mail
Bob Mottram's avatar
Bob Mottram committed
            rm -rf "/home/$USERNAME/Maildir/i2p/new"

    remove_i2p
    remove_app bdsmail
    remove_completion_param install_bdsmail
Bob Mottram's avatar
Bob Mottram committed
    sed -i '/bdsmail/d' "$COMPLETION_FILE"
    rm -rf /etc/skel/.mutt
Bob Mottram's avatar
Bob Mottram committed
    if [ -d $BDSMAIL_DIR ]; then
        rm -rf $BDSMAIL_DIR
    rm /usr/bin/bdsmail_domain
    sed -i '/bdsmail_distribute/d' /etc/crontab
    rm /usr/bin/bdsmail_distribute
Bob Mottram's avatar
Bob Mottram committed
    if [ -f /etc/skel/.mutt/bdsmail ]; then
        rm /etc/skel/.mutt/bdsmail
    fi
Bob Mottram's avatar
Bob Mottram committed
}

function install_bdsmail {
    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    if [ -d $BDSMAIL_DIR ]; then
        remove_bdsmail
Bob Mottram's avatar
Bob Mottram committed
    fi

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    if [ -d /repos/bdsmail ]; then
Bob Mottram's avatar
Bob Mottram committed
        mkdir -p $BDSMAIL_DIR
Bob Mottram's avatar
Bob Mottram committed
        cp -r -p /repos/bdsmail/. $BDSMAIL_DIR
        cd $BDSMAIL_DIR || exit 24687246
Bob Mottram's avatar
Bob Mottram committed
        git pull
    else
Bob Mottram's avatar
Bob Mottram committed
        git_clone "$BDSMAIL_REPO" "$BDSMAIL_DIR"
Bob Mottram's avatar
Bob Mottram committed
    fi

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d $BDSMAIL_DIR ]; then
Bob Mottram's avatar
Bob Mottram committed
        echo $'Unable to clone bdsmail repo'
        exit 5735735
    fi

    cd $BDSMAIL_DIR || exit 2468246
Bob Mottram's avatar
Bob Mottram committed
    git checkout "$BDSMAIL_COMMIT" -b "$BDSMAIL_COMMIT"
Bob Mottram's avatar
Bob Mottram committed
    set_completion_param "bdsmail commit" "$BDSMAIL_COMMIT"

Bob Mottram's avatar
Bob Mottram committed
    mkdir -p $BDSMAIL_DIR/Maildir/i2p
    chmod -R 700 $BDSMAIL_DIR/Maildir

Bob Mottram's avatar
Bob Mottram committed
    make GOROOT="/home/go/go${GO_VERSION}"
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f $BDSMAIL_DIR/bin/bdsconfig ]; then
Bob Mottram's avatar
Bob Mottram committed
        echo $'Unable to make bdsmail'
        exit 87923567842
    fi

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    install_i2p
Bob Mottram's avatar
Bob Mottram committed
    i2p_enable_sam
Bob Mottram's avatar
Bob Mottram committed

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    # create configuration file
Bob Mottram's avatar
Bob Mottram committed
    #$BDSMAIL_DIR/bin/bdsconfig > $BDSMAIL_DIR/config.ini
    { echo '[maild]':
      echo "i2paddr = 127.0.0.1:$I2P_SAM_PORT";
      echo 'i2pkeyfile = bdsmail-privkey.dat';
      echo "bindmail = 127.0.0.1:$I2P_SMTP_PORT";
      echo "bindweb = 127.0.0.1:$I2P_WEB_PORT";
      echo "bindpop3 = 127.0.0.1:$I2P_POP3_PORT";
      echo 'domain = localhost';
      echo 'maildir = Maildir/i2p';
      echo 'database = localhost.sqlite';
      echo 'assets = contrib/assets/web'; } > $BDSMAIL_DIR/config.ini

    { echo '[Unit]';
      echo 'Description=bdsmail';
      echo 'After=syslog.target';
      echo 'After=network.target';
      echo '';
      echo '[Service]';
      echo 'Type=simple';
      echo 'User=i2psvc';
      echo 'Group=i2psvc';
      echo "WorkingDirectory=$BDSMAIL_DIR";
      echo "ExecStart=$BDSMAIL_DIR/bin/maild $BDSMAIL_DIR/config.ini";
      echo 'Restart=always';
      echo 'Environment="USER=i2psvc"';
      echo '';
      echo '[Install]';
      echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/bdsmail.service

    { echo '#!/usr/bin/env python2';
      echo 'import base64, hashlib, sys';
      echo 'with open(sys.argv[1]) as f:';
      echo "    print(base64.b32encode(hashlib.sha256(base64.b64decode(f.read(516), '-~')).digest()).strip('=')+\".b32.i2p\")"; } > $BDSMAIL_DIR/get_address
Bob Mottram's avatar
Bob Mottram committed
    chmod +x $BDSMAIL_DIR/get_address
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
    chown -R i2psvc:i2psvc $BDSMAIL_DIR
Bob Mottram's avatar
Bob Mottram committed
    systemctl enable bdsmail
    systemctl start bdsmail

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    { echo '#!/bin/bash';
      echo "BDSMAIL_DIR=$BDSMAIL_DIR";
      echo "MAIL_DIR=\$BDSMAIL_DIR/\$(cat \$BDSMAIL_DIR/config.ini | grep 'maildir =' | awk -F ' ' '{print \$3}')";
      echo "if [ ! -d \$MAIL_DIR/postmaster/new ]; then";
      echo '    exit 0';
      echo 'fi';
      echo "for filename in \$MAIL_DIR/postmaster/new/*; do";
      echo "    to_line=\$(cat \$filename | grep \"To: \" | head -n 1)";
      echo "    to_username=\$(echo \"\$to_line\" | awk -F ' ' '{print \$2}' | awk -F '@' '{print \$1}')";
      echo "    if [ -d /home/\$to_username/Maildir/i2p/new ]; then";
      echo "        chown \$to_username:\$to_username \$filename";
      echo "        chmod 600 \$filename";
      echo "        mv \$filename /home/\$to_username/Maildir/i2p/new";
      echo '    fi';
      echo 'done'; } > /usr/bin/bdsmail_distribute
    chmod +x /usr/bin/bdsmail_distribute

    if ! grep -q 'bdsmail_distribute' /etc/crontab; then
Bob Mottram's avatar
Bob Mottram committed
        cron_add_mins 1 '/usr/bin/bdsmail_distribute 2> /dev/null'
Bob Mottram's avatar
Bob Mottram committed
    { echo '#!/bin/bash';
      echo "cd $BDSMAIL_DIR";
      echo 'if [ ! -f bdsmail-privkey.dat ]; then';
      echo '    exit 1';
      echo 'fi';
      echo "python2 get_address bdsmail-privkey.dat | tr '[:upper:]' '[:lower:]'"; } > /usr/bin/bdsmail_domain
Bob Mottram's avatar
Bob Mottram committed
    chmod +x /usr/bin/bdsmail_domain

    echo ''
    echo $'Now we will wait for i2p to connect and a private key to be generated'
    echo $'This may take a while.'
    echo ''

Bob Mottram's avatar
Bob Mottram committed
    bdsmail_wait_for_key_generation
Bob Mottram's avatar
Bob Mottram committed

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$bds_domain" ]; then
        systemctl stop bdsmail
        systemctl disable bdsmail
        remove_i2p
Bob Mottram's avatar
Bob Mottram committed
        echo $'Failed to get the bdsmail domain'
        exit 8934638
Bob Mottram's avatar
Bob Mottram committed
    BDSMAIL_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
    "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a bdsmail -p "$BDSMAIL_PASSWORD"
    cd $BDSMAIL_DIR || exit 2346872427
    $BDSMAIL_DIR/bin/mailtool $BDSMAIL_DIR/config.ini admin $BDSMAIL_DIR/Maildir/i2p/admin "$BDSMAIL_PASSWORD"
    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    # Create mutt configuration
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d /etc/skel/.mutt ]; then
        mkdir /etc/skel/.mutt
    fi
Bob Mottram's avatar
Bob Mottram committed
    { echo 'set mbox_type=Maildir';
      echo "set smtp_url=smtp://username:password@127.0.0.1:$I2P_SMTP_PORT/";
      echo 'set use_from=yes';
      echo "set from=username@${bds_domain}";
      echo "set spoolfile=~/Maildir/i2p";
      echo 'set pgp_autoencrypt=no';
      echo 'set pgp_replyencrypt=no';
      echo 'set pgp_autosign=no';
      echo 'set pgp_replysign=no'; } > /etc/skel/.mutt/bdsmail
Bob Mottram's avatar
Bob Mottram committed

    # mutt configuration for the admin user
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "/home/$MY_USERNAME/.mutt" ]; then
        mkdir "/home/$MY_USERNAME/.mutt"
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    cp /etc/skel/.mutt/bdsmail "/home/$MY_USERNAME/.mutt"
    sed -i "s|username|$MY_USERNAME|g" "/home/$MY_USERNAME/.mutt/bdsmail"
    sed -i "s|password|$BDSMAIL_PASSWORD|g" "/home/$MY_USERNAME/.mutt/bdsmail"
    chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.mutt"
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
    bdsmail_configure_users

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    cd $BDSMAIL_DIR || exit 2457245
    $BDSMAIL_DIR/bin/mailtool $BDSMAIL_DIR/config.ini "$MY_USERNAME" "/home/$MY_USERNAME/Maildir/i2p" "$BDSMAIL_PASSWORD"
Bob Mottram's avatar
Bob Mottram committed
    APP_INSTALLED=1
}

# NOTE: deliberately there is no "exit 0"