freedombone/src/freedombone-app-bdsmail

406 lines
13 KiB
Plaintext
Raw Normal View History

2018-02-11 23:08:12 +01:00
#!/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"
2018-02-13 17:10:14 +01:00
BDSMAIL_COMMIT='48838b84adca7a5a89a54b47599f6375ab9dbb68'
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-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
usermod -a -G i2psvc $USERNAME
2018-02-12 14:02:29 +01:00
if [ -f /home/$USERNAME/.muttrc ]; then
# Create a mutt i2p folder
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
if ! grep -q 'folder-hook !i2p' /home/$USERNAME/.muttrc; then
echo 'folder-hook !i2p/* source ~/.muttrc' >> /home/$USERNAME/.muttrc
fi
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
2018-02-12 20:53:25 +01:00
if [ ! -d /home/$USERNAME/Maildir/i2p/new ]; then
2018-02-12 14:02:29 +01:00
mkdir -p /home/$USERNAME/Maildir/i2p/cur
mkdir -p /home/$USERNAME/Maildir/i2p/new
chown -R $USERNAME:$USERNAME /home/$USERNAME/Maildir/i2p
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"
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
2018-02-12 14:02:29 +01:00
bdsmail_configure_users
2018-02-13 16:31:50 +01:00
$BDSMAIL_DIR/bin/newmail $BDSMAIL_DIR/config.ini $new_username /home/$new_username/Maildir/i2p "$new_user_password"
2018-02-11 23:08:12 +01:00
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
2018-02-12 19:39:44 +01:00
set_repo_commit $BDSMAIL_DIR "bdsmail commit" "$BDSMAIL_COMMIT" $BDSMAIL_REPO
2018-02-13 16:31:50 +01:00
cd $BDSMAIL_DIR
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
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
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
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
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
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
deluser $USERNAME i2psvc
2018-02-12 14:02:29 +01:00
# Remove mutt folder hook to the i2p config
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
fi
if grep -q 'folder-hook i2p' /home/$USERNAME/.muttrc; then
sed -i '/folder-hook i2p/d' /home/$USERNAME/.muttrc
fi
fi
# Remove folder
if grep -q ' =i2p' /home/$USERNAME/.muttrc; then
sed -i 's| =i2p||g' /home/$USERNAME/.muttrc
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-12 20:53:25 +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
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
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
# ask to the ssh login password for the admin user
# This is then used to create the maildir account
user_account_password=''
data=$(tempfile 2>/dev/null)
trap "rm -f $data" 0 1 2 5 15
dialog --title $"Password" \
--clear \
2018-02-13 11:21:04 +01:00
--passwordbox $"Enter your ssh login password.\n\nThis is the same as the original ssh login password shown when you first installed the system." 12 60 2> $data
ret=$?
case $ret in
0)
user_account_password=$(cat $data)
;;
esac
if [ ${#user_account_password} -lt 2 ]; then
echo ''
echo $'A password must be provided for your user account.'
exit 3656358
fi
2018-02-11 23:08:12 +01:00
if [ -d /repos/bdsmail ]; then
2018-02-12 19:39:44 +01:00
mkdir $BDSMAIL_DIR
cp -r -p /repos/bdsmail/. $BDSMAIL_DIR
cd $BDSMAIL_DIR
2018-02-11 23:08:12 +01:00
git pull
else
2018-02-12 19:39:44 +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-02-12 19:39:44 +01:00
cd $BDSMAIL_DIR
2018-02-11 23:08:12 +01:00
git checkout $BDSMAIL_COMMIT -b $BDSMAIL_COMMIT
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-11 23:10:52 +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-12 19:39:44 +01:00
$BDSMAIL_DIR/bin/bdsconfig > $BDSMAIL_DIR/config.ini
echo '[maild]' > $BDSMAIL_DIR/config.ini
2018-02-13 00:21:49 +01:00
echo "i2paddr = 127.0.0.1:$I2P_SAM_PORT" >> $BDSMAIL_DIR/config.ini
2018-02-12 19:39:44 +01:00
echo 'i2pkeyfile = bdsmail-privkey.dat' >> $BDSMAIL_DIR/config.ini
2018-02-13 00:21:49 +01:00
echo "bindmail = 127.0.0.1:$I2P_SMTP_PORT" >> $BDSMAIL_DIR/config.ini
2018-02-12 19:39:44 +01:00
echo 'bindweb = 127.0.0.1:8489' >> $BDSMAIL_DIR/config.ini
echo 'domain = localhost' >> $BDSMAIL_DIR/config.ini
2018-02-13 16:31:50 +01:00
echo 'maildir = Maildir/i2p' >> $BDSMAIL_DIR/config.ini
2018-02-12 19:39:44 +01:00
echo 'database = localhost.sqlite' >> $BDSMAIL_DIR/config.ini
echo 'assets = contrib/assets/web' >> $BDSMAIL_DIR/config.ini
2018-02-11 23:08:12 +01:00
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
2018-02-12 11:34:24 +01:00
echo 'User=i2psvc' >> /etc/systemd/system/bdsmail.service
echo 'Group=i2psvc' >> /etc/systemd/system/bdsmail.service
2018-02-12 19:39:44 +01:00
echo "WorkingDirectory=$BDSMAIL_DIR" >> /etc/systemd/system/bdsmail.service
echo "ExecStart=$BDSMAIL_DIR/bin/maild $BDSMAIL_DIR/config.ini" >> /etc/systemd/system/bdsmail.service
2018-02-11 23:08:12 +01:00
echo 'Restart=always' >> /etc/systemd/system/bdsmail.service
2018-02-12 11:34:24 +01:00
echo 'Environment="USER=i2psvc"' >> /etc/systemd/system/bdsmail.service
2018-02-11 23:08:12 +01:00
echo '' >> /etc/systemd/system/bdsmail.service
echo '[Install]' >> /etc/systemd/system/bdsmail.service
echo 'WantedBy=multi-user.target' >> /etc/systemd/system/bdsmail.service
2018-02-12 19:39:44 +01:00
echo '#!/usr/bin/env python2' > $BDSMAIL_DIR/get_address
echo 'import base64, hashlib, sys' >> $BDSMAIL_DIR/get_address
echo 'with open(sys.argv[1]) as f:' >> $BDSMAIL_DIR/get_address
echo " print(base64.b32encode(hashlib.sha256(base64.b64decode(f.read(516), '-~')).digest()).strip('=')+\".b32.i2p\")" >> $BDSMAIL_DIR/get_address
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-11 23:08:12 +01:00
echo '#!/bin/bash' > /usr/bin/bdsmail_domain
2018-02-12 22:03:47 +01:00
echo "cd $BDSMAIL_DIR" >> /usr/bin/bdsmail_domain
echo 'if [ ! -f bdsmail-privkey.dat ]; then' >> /usr/bin/bdsmail_domain
echo ' exit 1' >> /usr/bin/bdsmail_domain
echo 'fi' >> /usr/bin/bdsmail_domain
2018-02-12 23:13:31 +01:00
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-11 23:18:46 +01:00
# wait for domain to be generated by the daemon
2018-02-12 11:52:20 +01:00
# This can take a while, probably because i2p is connecting
bds_domain=
sleep_ctr=0
while [ ! $bds_domain ]; do
2018-02-12 22:00:45 +01:00
echo $"Waiting for i2p domain to be generated ${sleep_ctr}/100"
2018-02-12 11:23:22 +01:00
systemctl restart bdsmail
2018-02-12 16:00:02 +01:00
sleep 20
2018-02-11 23:24:53 +01:00
bds_domain=$(bdsmail_domain)
2018-02-12 11:52:20 +01:00
sleep_ctr=$((sleep_ctr + 1))
2018-02-12 16:00:02 +01:00
if [ $sleep_ctr -gt 100 ]; then
2018-02-12 11:52:20 +01:00
break
2018-02-11 23:24:53 +01:00
fi
2018-02-12 11:52:20 +01:00
done
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-11 23:08:12 +01:00
# Create mutt configuration
mkdir /etc/skel/.mutt
echo 'set mbox_type=Maildir' > /etc/skel/.mutt/bdsmail
2018-02-13 00:21:49 +01:00
echo "set smtp_url=smtp://127.0.0.1:$I2P_SMTP_PORT/" >> /etc/skel/.mutt/bdsmail
2018-02-11 23:08:12 +01:00
echo 'set use_from=yes' >> /etc/skel/.mutt/bdsmail
2018-02-11 23:18:46 +01:00
echo "set from=username@${bds_domain}" >> /etc/skel/.mutt/bdsmail
2018-02-12 20:53:25 +01:00
echo "set spoolfile=~/Maildir/i2p" >> /etc/skel/.mutt/bdsmail
2018-02-11 23:08:12 +01:00
# 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
2018-02-12 14:51:45 +01:00
bdsmail_configure_users
2018-02-13 16:31:50 +01:00
$BDSMAIL_DIR/bin/newmail $BDSMAIL_DIR/config.ini $MY_USERNAME /home/$MY_USERNAME/Maildir/i2p "$user_account_password"
2018-02-13 11:23:32 +01:00
user_account_password=
2018-02-11 23:08:12 +01:00
APP_INSTALLED=1
}
# NOTE: deliberately there is no "exit 0"