snappymail/integrations/hestiacp/v-add-sys-snappymail
2022-11-09 16:11:43 +01:00

176 lines
6.5 KiB
Bash

#!/bin/bash
# info: Install SnappyMail webmail client
# options: [MODE]
#
# This function installs the SnappyMail webmail client.
#----------------------------------------------------------#
# Variables & Functions #
#----------------------------------------------------------#
# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"
# upgrade config file
source "$HESTIA/install/upgrade/upgrade.conf"
MODE=$1
UPDATE="no"
# Version and Download paths
# Version to be moved to upgrade script
SM_FILE="snappymail-latest.tar.gz"
# For removal of folder
SM_EXTRACT_MAIN="snappymail"
# Downloading full version
SM_URL="https://snappymail.eu/repository/latest.tar.gz"
# Folder paths
SM_INSTALL_DIR="/var/lib/snappymail"
SM_CONFIG_DIR="/etc/snappymail"
SM_LOG="/var/log/snappymail"
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
# Checking root permissions
if [ "x$(id -u)" != 'x0' ]; then
echo "ERROR: v-add-sys-snappymail can only be executed by the root user"
exit 10
fi
# Ensure that $HESTIA (/usr/local/hestia/) and other variables are valid.
if [ -z "$HESTIA" ]; then
HESTIA="/usr/local/hestia"
fi
if [ -z "$HOMEDIR" ] || [ -z "$HESTIA_INSTALL_DIR" ]; then
echo "ERROR: Environment variables not present, installation aborted."
exit 2
fi
# Get current version
if [ -f "/var/lib/snappymail/data/VERSION" ]; then
version=$(cat $SM_INSTALL_DIR/data/VERSION);
if [ "$version" == "$rl_v" ]; then
echo "Error: Installed version ($version) is equal to the available version ($rl_v)"
exit 2;
else
UPDATE="yes"
fi
fi
# Perform verification if read-only mode is enabled
check_hestia_demo_mode
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
if [ "$UPDATE" == "no" ]; then
rm -f -r $SM_INSTALL_DIR
rm -f -r $SM_CONFIG_DIR
mkdir $SM_INSTALL_DIR
mkdir $SM_CONFIG_DIR
cd "$SM_INSTALL_DIR"
[ ! -f "${RC_INSTALL_DIR}/${RC_FILE}" ] && wget "$SM_URL" --retry-connrefused --quiet -O "${SM_INSTALL_DIR}/${SM_FILE}"
key=$(openssl rand -hex 4);
admin_account="admin_$key"
admin_password=$(generate_password)
r=$(generate_password)
echo "Username: admin_$key" > ~/.snappymail
echo "Password: $admin_password" >> ~/.snappymail
echo "Secret key: admin_$key" >> ~/.snappymail
tar -xzf ${SM_INSTALL_DIR}/${SM_FILE}
mv ./data $SM_CONFIG_DIR/
ln -s $SM_CONFIG_DIR/data/ ./data
SALT=$(openssl rand -base64 64)
cp ./data/VERSION ./data/INSTALLED
echo "<?php //$SALT" > ./data/SALT.php
echo "Forbidden" > ./data/index.php
echo "Forbidden" > ./data/index.html
# Create database
mysql -e "CREATE DATABASE snappymail"
# Mysql available on system
r=$(generate_password)
mysql -e "GRANT ALL ON snappymail.*
TO snappymail@localhost IDENTIFIED BY '$r'"
mkdir -p $SM_CONFIG_DIR/data/_data_/_default_/configs
php -f $HESTIA_INSTALL_DIR/snappymail/change_password.php "admin_$key" "$admin_password" "$r"
mkdir -p $SM_CONFIG_DIR/data/_data_/_default_/domains
cp -f $HESTIA_INSTALL_DIR/snappymail/default.ini $SM_CONFIG_DIR/data/_data_/_default_/domains
mkdir -p $SM_CONFIG_DIR/data/_data_/_default_/plugins
cp -f -r $HESTIA_INSTALL_DIR/snappymail/plugins/hestia-change-password/ $SM_CONFIG_DIR/data/_data_/_default_/plugins
mkdir -p $SM_CONFIG_DIR/data/_data_/_default_/plugins/add-x-originating-ip-header
# Download add-x-originating-ip-header from snappymail github
wget --quiet -O $SM_CONFIG_DIR/data/_data_/_default_/plugins/add-x-originating-ip-header/index.php https://raw.githubusercontent.com/the-djmaze/snappymail/master/plugins/add-x-originating-ip-header/index.php
wget --quiet -O $SM_CONFIG_DIR/data/_data_/_default_/plugins/add-x-originating-ip-header/LICENSE https://raw.githubusercontent.com/the-djmaze/snappymail/master/plugins/add-x-originating-ip-header/LICENSE
cp -f $HESTIA_INSTALL_DIR/snappymail/plugins/plugin-add-x-originating-ip-header.ini $SM_CONFIG_DIR/data/_data_/_default_/configs/plugin-add-x-originating-ip-header.ini
cp -f $HESTIA_INSTALL_DIR/snappymail/plugins/plugin-hestia-change-password.ini $SM_CONFIG_DIR/data/_data_/_default_/configs/plugin-hestia-change-password.ini
sed -i "s/%hostname%/$(hostname)/g" $SM_CONFIG_DIR/data/_data_/_default_/configs/plugin-hestia-change-password.ini
sed -i "s/%port%/$BACKEND_PORT/g" $SM_CONFIG_DIR/data/_data_/_default_/configs/plugin-hestia-change-password.ini
chown -R www-data:www-data ./data
chown -R www-data:www-data $SM_CONFIG_DIR/
rm ${SM_INSTALL_DIR}/${SM_FILE}
# Add robots.txt
echo "User-agent: *" > $SM_INSTALL_DIR/robots.txt
echo "Disallow: /" >> $SM_INSTALL_DIR/robots.txt
# Updating hestia.conf
if [ -z "$(grep WEBMAIL_SYSTEM $HESTIA/conf/hestia.conf)" ]; then
$BIN/v-change-sys-config-value 'WEBMAIL_SYSTEM' 'snappymail'
else
if [ -z "$(echo "$WEBMAIL_SYSTEM" | grep -w 'snappymail')" ]; then
if [ -n "$WEBMAIL_SYSTEM" ]; then
$BIN/v-change-sys-config-value 'WEBMAIL_SYSTEM' "snappymail,$WEBMAIL_SYSTEM"
else
$BIN/v-change-sys-config-value 'WEBMAIL_SYSTEM' "snappymail"
fi
fi
fi
else
[ ! -f "${RC_INSTALL_DIR}/${RC_FILE}" ] && wget "$SM_URL" --quiet -O "${SM_INSTALL_DIR}/${SM_FILE}"
version=$(cat $SM_INSTALL_DIR/data/VERSION);
tar -xzf snappymail-latest.tar.gz "data/VERSION" $SM_INSTALL_DIR/
version_source=$(cat $SM_INSTALL_DIR/VERSION);
# Check version inside .tar.gz file in case hestia didn't update yet
if [ "$version" != "$version_source" ]; then
tar -xzf ${SM_INSTALL_DIR}/${SM_FILE}
rm $SM_INSTALL_DIR/$SM_FILE
fi
rm ${SM_INSTALL_DIR}/VERSION
fi
#----------------------------------------------------------#
# Hestia #
#----------------------------------------------------------#
if [ "$UPDATE" = "yes" ]; then
$BIN/v-log-action "system" "Info" "Plugins" "SnappyMail updated (Version: $version)."
else
$BIN/v-log-action "system" "Info" "Plugins" "SnappyMail enabled (Version: $version)."
fi
log_event "$OK" "$ARGUMENTS"