freedombone/src/freedombone-app-bdsmail

468 lines
15 KiB
Plaintext
Raw Normal View History

2018-02-11 23:08:12 +01:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2018-02-11 23:08:12 +01:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2018-02-11 23:08:12 +01:00
#
# 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"
2018-02-14 18:27:02 +01:00
BDSMAIL_COMMIT='6fae61365306ad3c1966a107aa06310ea65fb239'
2018-02-12 20:56:54 +01:00
BDSMAIL_DIR=/etc/bdsmail
2018-02-13 00:21:49 +01:00
I2P_SAM_PORT=7656
I2P_SMTP_PORT=2525
2018-02-16 11:02:15 +01:00
I2P_WEB_PORT=8489
I2P_POP3_PORT=1110
2018-02-11 23:08:12 +01:00
bdsmail=(MY_USERNAME)
2018-02-12 14:02:29 +01:00
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
2018-02-26 14:50:40 +01:00
usermod -a -G i2psvc "$USERNAME"
2018-02-26 14:50:40 +01:00
if [ -f "/home/$USERNAME/.muttrc" ]; then
2018-02-12 14:02:29 +01:00
# Create a mutt i2p folder
2018-02-26 14:50:40 +01:00
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"
2018-02-12 14:02:29 +01:00
fi
# Create a mutt folder hook to the i2p config
2018-02-26 14:50:40 +01:00
if ! grep -q 'folder-hook !i2p' "/home/$USERNAME/.muttrc"; then
echo 'folder-hook !i2p/* source ~/.muttrc' >> "/home/$USERNAME/.muttrc"
2018-02-12 14:02:29 +01:00
fi
2018-02-26 14:50:40 +01:00
if ! grep -q 'folder-hook i2p' "/home/$USERNAME/.muttrc"; then
echo 'folder-hook i2p/* source ~/.mutt/bdsmail' >> "/home/$USERNAME/.muttrc"
2018-02-12 14:02:29 +01:00
fi
fi
# Create a directory where i2p mail will be stored
2018-02-26 14:50:40 +01:00
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"
2018-02-12 14:02:29 +01:00
fi
fi
done
}
2018-02-11 23:08:12 +01:00
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"
2018-02-26 14:50:40 +01:00
if [ ! -d "/home/$new_username/.mutt" ]; then
mkdir "/home/$new_username/.mutt"
cp /etc/skel/.mutt/bdsmail "/home/$new_username/.mutt"
2018-02-11 23:08:12 +01:00
fi
2018-02-15 22:14:21 +01:00
read_config_param MY_USERNAME
2018-02-26 14:50:40 +01:00
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"
2018-02-12 14:02:29 +01:00
bdsmail_configure_users
2018-02-26 14:50:40 +01:00
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"
2018-02-11 23:08:12 +01:00
echo '0'
}
function install_interactive_bdsmail {
echo -n ''
APP_INSTALLED=1
}
function change_password_bdsmail {
curr_username="$1"
new_user_password="$2"
2018-02-15 22:14:21 +01:00
2018-02-26 14:50:40 +01:00
"${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a bdsmail -p "$new_user_password"
2018-02-15 22:14:21 +01:00
for d in /home/*/ ; do
USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
2018-02-26 14:50:40 +01:00
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"
2018-03-02 20:39:00 +01:00
cd $BDSMAIL_DIR || exit 2468246
2018-02-26 14:50:40 +01:00
$BDSMAIL_DIR/bin/mailtool $BDSMAIL_DIR/config.ini "$curr_username" "/home/$curr_username/Maildir/i2p" "$new_user_password"
2018-02-15 22:14:21 +01:00
fi
done
2018-02-11 23:08:12 +01:00
}
2018-02-14 13:54:08 +01:00
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
2018-02-26 14:50:40 +01:00
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"
2018-02-14 13:54:08 +01:00
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))
2018-02-26 14:50:40 +01:00
if [ "$sleep_ctr" -gt 100 ]; then
2018-02-14 13:54:08 +01:00
break
fi
done
}
2018-02-11 23:08:12 +01:00
function reconfigure_bdsmail {
2018-02-14 13:54:08 +01:00
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
2018-02-11 23:08:12 +01:00
}
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
2018-02-26 14:50:40 +01:00
set_repo_commit $BDSMAIL_DIR "bdsmail commit" "$BDSMAIL_COMMIT" "$BDSMAIL_REPO"
cd $BDSMAIL_DIR || exit 2457245
make GOROOT="/home/go/go${GO_VERSION}"
2018-02-12 19:39:44 +01:00
chown -R i2psvc:i2psvc $BDSMAIL_DIR
2018-02-13 16:31:50 +01:00
systemctl restart bdsmail
if ! grep -q "$(bdsmail_domain)" /etc/skel/.mutt/bdsmail; then
bdsmail_update_domain
fi
2018-02-11 23:08:12 +01:00
}
function backup_local_bdsmail {
systemctl stop bdsmail
2018-02-12 19:39:44 +01:00
source_directory=$BDSMAIL_DIR
2018-02-11 23:08:12 +01:00
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
2018-02-12 19:39:44 +01:00
bdsmail_dir=$BDSMAIL_DIR
2018-02-11 23:08:12 +01:00
function_check restore_directory_from_usb
restore_directory_from_usb $temp_restore_dir bdsmail
if [ -d $temp_restore_dir ]; then
2018-02-26 14:50:40 +01:00
if [ -d $temp_restore_dir$bdsmail_dir ]; then
2018-02-11 23:08:12 +01:00
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
2018-02-12 11:34:24 +01:00
chown -R i2psvc:i2psvc $bdsmail_dir
2018-02-11 23:08:12 +01:00
rm -rf $temp_restore_dir
fi
systemctl start bdsmail
}
function backup_remote_bdsmail {
systemctl stop bdsmail
2018-02-12 19:39:44 +01:00
source_directory=$BDSMAIL_DIR
2018-02-11 23:08:12 +01:00
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
2018-02-12 19:39:44 +01:00
bdsmail_dir=$BDSMAIL_DIR
2018-02-11 23:08:12 +01:00
function_check restore_directory_from_friend
restore_directory_from_friend $temp_restore_dir bdsmail
if [ -d $temp_restore_dir ]; then
2018-02-26 14:50:40 +01:00
if [ -d $temp_restore_dir$bdsmail_dir ]; then
2018-02-11 23:08:12 +01:00
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
2018-02-12 11:34:24 +01:00
chown -R i2psvc:i2psvc $bdsmail_dir
2018-02-11 23:08:12 +01:00
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
2018-02-12 14:02:29 +01:00
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
2018-02-26 14:50:40 +01:00
deluser "$USERNAME" i2psvc
2018-02-12 14:02:29 +01:00
# Remove mutt folder hook to the i2p config
2018-02-26 14:50:40 +01:00
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"
2018-02-12 14:02:29 +01:00
fi
2018-02-26 14:50:40 +01:00
if grep -q 'folder-hook i2p' "/home/$USERNAME/.muttrc"; then
sed -i '/folder-hook i2p/d' "/home/$USERNAME/.muttrc"
2018-02-12 14:02:29 +01:00
fi
fi
# Remove folder
2018-02-26 14:50:40 +01:00
if grep -q ' =i2p' "/home/$USERNAME/.muttrc"; then
sed -i 's| =i2p||g' "/home/$USERNAME/.muttrc"
2018-02-12 14:02:29 +01:00
fi
2018-02-12 20:53:25 +01:00
# NOTE: leave Maildir/i2p/cur. We might want to archive that
2018-02-12 14:02:29 +01:00
# or just be reinstalling the system without losing mail
2018-02-26 14:50:40 +01:00
rm -rf "/home/$USERNAME/Maildir/i2p/new"
2018-02-12 14:02:29 +01:00
fi
done
remove_i2p
remove_app bdsmail
remove_completion_param install_bdsmail
2018-02-26 14:50:40 +01:00
sed -i '/bdsmail/d' "$COMPLETION_FILE"
rm -rf /etc/skel/.mutt
2018-02-12 19:39:44 +01:00
if [ -d $BDSMAIL_DIR ]; then
rm -rf $BDSMAIL_DIR
fi
rm /usr/bin/bdsmail_domain
sed -i '/bdsmail_distribute/d' /etc/crontab
rm /usr/bin/bdsmail_distribute
2018-02-13 23:58:07 +01:00
if [ -f /etc/skel/.mutt/bdsmail ]; then
rm /etc/skel/.mutt/bdsmail
fi
2018-02-11 23:08:12 +01:00
}
function install_bdsmail {
2018-02-12 19:39:44 +01:00
if [ -d $BDSMAIL_DIR ]; then
2018-02-11 23:48:07 +01:00
remove_bdsmail
2018-02-11 23:08:12 +01:00
fi
if [ -d /repos/bdsmail ]; then
2018-02-12 19:39:44 +01:00
mkdir $BDSMAIL_DIR
cp -r -p /repos/bdsmail/. $BDSMAIL_DIR
2018-03-02 20:39:00 +01:00
cd $BDSMAIL_DIR || exit 24687246
2018-02-11 23:08:12 +01:00
git pull
else
2018-02-26 14:50:40 +01:00
git_clone "$BDSMAIL_REPO" "$BDSMAIL_DIR"
2018-02-11 23:08:12 +01:00
fi
2018-02-12 19:39:44 +01:00
if [ ! -d $BDSMAIL_DIR ]; then
2018-02-11 23:08:12 +01:00
echo $'Unable to clone bdsmail repo'
exit 5735735
fi
2018-03-02 20:39:00 +01:00
cd $BDSMAIL_DIR || exit 2468246
2018-02-26 14:50:40 +01:00
git checkout "$BDSMAIL_COMMIT" -b "$BDSMAIL_COMMIT"
2018-02-11 23:08:12 +01:00
set_completion_param "bdsmail commit" "$BDSMAIL_COMMIT"
2018-02-13 16:31:50 +01:00
mkdir -p $BDSMAIL_DIR/Maildir/i2p
chmod -R 700 $BDSMAIL_DIR/Maildir
2018-02-26 14:50:40 +01:00
make GOROOT="/home/go/go${GO_VERSION}"
2018-02-12 19:39:44 +01:00
if [ ! -f $BDSMAIL_DIR/bin/bdsconfig ]; then
2018-02-11 23:08:12 +01:00
echo $'Unable to make bdsmail'
exit 87923567842
fi
install_i2p
2018-02-12 00:21:50 +01:00
i2p_enable_sam
2018-02-11 23:08:12 +01:00
# create configuration file
2018-02-26 14:50:40 +01:00
#$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
2018-02-12 19:39:44 +01:00
chmod +x $BDSMAIL_DIR/get_address
2018-02-11 23:08:12 +01:00
2018-02-12 19:39:44 +01:00
chown -R i2psvc:i2psvc $BDSMAIL_DIR
2018-02-12 11:41:38 +01:00
systemctl enable bdsmail
systemctl start bdsmail
2018-02-26 14:50:40 +01:00
{ 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
2018-02-21 22:40:30 +01:00
cron_add_mins 1 '/usr/bin/bdsmail_distribute 2> /dev/null'
fi
2018-02-26 14:50:40 +01:00
{ 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
2018-02-11 23:08:12 +01:00
chmod +x /usr/bin/bdsmail_domain
2018-02-12 12:05:43 +01:00
echo ''
echo $'Now we will wait for i2p to connect and a private key to be generated'
echo $'This may take a while.'
echo ''
2018-02-14 13:54:08 +01:00
bdsmail_wait_for_key_generation
2018-02-12 11:52:20 +01:00
2018-02-26 14:50:40 +01:00
if [ ! "$bds_domain" ]; then
systemctl stop bdsmail
systemctl disable bdsmail
remove_i2p
2018-02-12 11:52:20 +01:00
echo $'Failed to get the bdsmail domain'
exit 8934638
2018-02-11 23:18:46 +01:00
fi
2018-02-26 14:50:40 +01:00
BDSMAIL_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
"${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a bdsmail -p "$BDSMAIL_PASSWORD"
cd $BDSMAIL_DIR || exit 2346872427
2018-02-15 22:14:21 +01:00
$BDSMAIL_DIR/bin/mailtool $BDSMAIL_DIR/config.ini admin $BDSMAIL_DIR/Maildir/i2p/admin "$BDSMAIL_PASSWORD"
2018-02-13 21:20:14 +01:00
2018-02-11 23:08:12 +01:00
# Create mutt configuration
2018-02-13 23:58:07 +01:00
if [ ! -d /etc/skel/.mutt ]; then
mkdir /etc/skel/.mutt
fi
2018-02-26 14:50:40 +01:00
{ 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
2018-02-11 23:08:12 +01:00
# mutt configuration for the admin user
2018-02-26 14:50:40 +01:00
if [ ! -d "/home/$MY_USERNAME/.mutt" ]; then
mkdir "/home/$MY_USERNAME/.mutt"
2018-02-11 23:08:12 +01:00
fi
2018-02-26 14:50:40 +01:00
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"
2018-02-11 23:08:12 +01:00
2018-02-12 14:51:45 +01:00
bdsmail_configure_users
2018-02-26 14:50:40 +01:00
cd $BDSMAIL_DIR || exit 2457245
$BDSMAIL_DIR/bin/mailtool $BDSMAIL_DIR/config.ini "$MY_USERNAME" "/home/$MY_USERNAME/Maildir/i2p" "$BDSMAIL_PASSWORD"
2018-02-11 23:08:12 +01:00
APP_INSTALLED=1
}
# NOTE: deliberately there is no "exit 0"