2014-06-03 13:24:48 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
source setup/functions.sh
|
2022-06-19 10:48:55 +00:00
|
|
|
source /etc/mailinabox.conf # load global vars
|
2014-06-03 13:24:48 +00:00
|
|
|
|
2015-08-19 19:58:35 +00:00
|
|
|
echo "Installing Mail-in-a-Box system management daemon..."
|
|
|
|
|
2017-02-01 14:14:01 +00:00
|
|
|
# DEPENDENCIES
|
|
|
|
|
2018-10-24 21:20:48 +00:00
|
|
|
# virtualenv is used to isolate the Python 3 packages we
|
2018-05-12 15:32:54 +00:00
|
|
|
# install via pip from the system-installed packages.
|
2018-05-13 00:02:25 +00:00
|
|
|
#
|
|
|
|
# certbot installs EFF's certbot which we use to
|
|
|
|
# provision free TLS certificates.
|
2022-10-16 18:21:07 +00:00
|
|
|
#
|
|
|
|
# gcc and build tools are required to install the latest version
|
|
|
|
# of duplicity
|
|
|
|
apt_install python3-pip python3-gpg virtualenv certbot rsync librsync2 python3-fasteners python3-future python3-lockfile \
|
2022-10-18 19:36:18 +00:00
|
|
|
gcc python3-dev librsync-dev gettext
|
2020-11-26 12:13:31 +00:00
|
|
|
|
2022-10-16 18:21:07 +00:00
|
|
|
apt_get_quiet remove --autoremove --purge duplicity || /bin/true
|
|
|
|
|
|
|
|
# Duplicity does the actual backups.
|
2021-02-28 12:54:55 +00:00
|
|
|
# b2sdk is used for backblaze backups.
|
2022-09-03 11:50:36 +00:00
|
|
|
# boto3 is used for amazon aws backups.
|
2020-11-26 12:13:31 +00:00
|
|
|
# Both are installed outside the pipenv, so they can be used by duplicity
|
2022-10-16 18:21:07 +00:00
|
|
|
hide_output pip3 install --upgrade b2sdk boto3 duplicity
|
2015-07-27 19:56:08 +00:00
|
|
|
|
2018-01-15 17:43:32 +00:00
|
|
|
# Create a virtualenv for the installation of Python 3 packages
|
|
|
|
# used by the management daemon.
|
|
|
|
inst_dir=/usr/local/lib/mailinabox
|
|
|
|
mkdir -p $inst_dir
|
|
|
|
venv=$inst_dir/env
|
|
|
|
if [ ! -d $venv ]; then
|
2019-05-15 18:59:32 +00:00
|
|
|
hide_output virtualenv -ppython3 $venv
|
2022-01-24 01:48:34 +00:00
|
|
|
elif [ ! -f $venv/.oscode ]; then
|
|
|
|
echo "Re-creating Python environment..."
|
|
|
|
rm -rf $venv
|
|
|
|
hide_output virtualenv -ppython3 $venv
|
|
|
|
elif [ "$(cat $venv/.oscode)" != $(get_os_code) ]; then
|
|
|
|
echo "Existing management environment is from an earlier version of the OS you're running."
|
|
|
|
echo "Re-creating Python environment..."
|
|
|
|
rm -rf $venv
|
|
|
|
hide_output virtualenv -ppython3 $venv
|
2018-01-15 17:43:32 +00:00
|
|
|
fi
|
2016-02-18 11:59:38 +00:00
|
|
|
|
2018-05-12 15:32:54 +00:00
|
|
|
# Upgrade pip because the Ubuntu-packaged version is out of date.
|
2018-01-15 17:43:32 +00:00
|
|
|
hide_output $venv/bin/pip install --upgrade pip
|
2017-02-01 14:14:01 +00:00
|
|
|
|
2016-02-18 11:59:38 +00:00
|
|
|
# Install other Python 3 packages used by the management daemon.
|
|
|
|
# The first line is the packages that Josh maintains himself!
|
|
|
|
# NOTE: email_validator is repeated in setup/questions.sh, so please keep the versions synced.
|
2018-01-15 17:43:32 +00:00
|
|
|
hide_output $venv/bin/pip install --upgrade \
|
2018-05-13 00:02:25 +00:00
|
|
|
rtyaml "email_validator>=1.0.0" "exclusiveprocess" \
|
2022-09-17 12:03:16 +00:00
|
|
|
flask dnspython python-dateutil expiringdict gunicorn \
|
2021-08-22 20:07:16 +00:00
|
|
|
qrcode[pil] pyotp \
|
2022-10-16 19:46:04 +00:00
|
|
|
"idna>=2.0.0" "cryptography==2.2.2" boto psutil postfix-mta-sts-resolver boto3 b2sdk
|
2014-06-03 20:21:17 +00:00
|
|
|
|
2020-10-04 15:35:59 +00:00
|
|
|
# Make the venv use the packaged gpgme bindings (the ones pip provides are severely out-of-date)
|
|
|
|
if [ ! -d $venv/lib/python$(python_version)/site-packages/gpg/ ]; then
|
|
|
|
ln -s /usr/lib/python3/dist-packages/gpg/ $venv/lib/python$(python_version)/site-packages/
|
|
|
|
fi
|
|
|
|
|
2022-01-24 01:48:34 +00:00
|
|
|
echo $(get_os_code) > $venv/.oscode
|
|
|
|
|
2017-02-01 14:14:01 +00:00
|
|
|
# CONFIGURATION
|
|
|
|
|
2014-06-03 20:21:17 +00:00
|
|
|
# Create a backup directory and a random key for encrypting backups.
|
|
|
|
mkdir -p $STORAGE_ROOT/backup
|
|
|
|
if [ ! -f $STORAGE_ROOT/backup/secret_key.txt ]; then
|
2014-08-22 21:23:56 +00:00
|
|
|
$(umask 077; openssl rand -base64 2048 > $STORAGE_ROOT/backup/secret_key.txt)
|
2014-06-03 20:21:17 +00:00
|
|
|
fi
|
2014-06-03 13:24:48 +00:00
|
|
|
|
2017-05-08 11:25:16 +00:00
|
|
|
|
|
|
|
# Download jQuery and Bootstrap local files
|
|
|
|
|
|
|
|
# Make sure we have the directory to save to.
|
2018-01-15 17:43:32 +00:00
|
|
|
assets_dir=$inst_dir/vendor/assets
|
2017-05-31 11:36:17 +00:00
|
|
|
rm -rf $assets_dir
|
2017-05-08 11:25:16 +00:00
|
|
|
mkdir -p $assets_dir
|
|
|
|
|
|
|
|
# jQuery CDN URL
|
2022-10-17 01:56:03 +00:00
|
|
|
jquery_version=3.6.1
|
2022-11-21 02:04:28 +00:00
|
|
|
jquery_url=https://code.jquery.com # Check this link for new versions
|
2017-05-08 11:25:16 +00:00
|
|
|
|
|
|
|
# Get jQuery
|
2022-10-17 01:56:03 +00:00
|
|
|
wget_verify $jquery_url/jquery-$jquery_version.min.js ea61688671d0c3044f2c5b2f2c4af0a6620ac6c2 $assets_dir/jquery.min.js
|
2017-05-08 11:25:16 +00:00
|
|
|
|
|
|
|
# Bootstrap CDN URL
|
2022-11-21 02:04:28 +00:00
|
|
|
# See https://github.com/twbs/bootstrap/releases to check for new versions
|
2022-10-17 01:56:03 +00:00
|
|
|
bootstrap_version=5.2.2
|
2017-05-31 11:36:17 +00:00
|
|
|
bootstrap_url=https://github.com/twbs/bootstrap/releases/download/v$bootstrap_version/bootstrap-$bootstrap_version-dist.zip
|
2017-05-08 11:25:16 +00:00
|
|
|
|
|
|
|
# Get Bootstrap
|
2022-10-17 01:56:03 +00:00
|
|
|
wget_verify $bootstrap_url 740b34c22cef5c2f12a34f084b813ea308fedf74 /tmp/bootstrap.zip
|
2018-01-15 17:43:32 +00:00
|
|
|
unzip -q /tmp/bootstrap.zip -d $assets_dir
|
|
|
|
mv $assets_dir/bootstrap-$bootstrap_version-dist $assets_dir/bootstrap
|
2017-05-31 11:36:17 +00:00
|
|
|
rm -f /tmp/bootstrap.zip
|
2017-05-08 11:25:16 +00:00
|
|
|
|
2020-11-21 02:27:52 +00:00
|
|
|
# FontAwesome CDN URL
|
2022-11-21 02:04:28 +00:00
|
|
|
# See https://github.com/FortAwesome/Font-Awesome/releases to check for new versions
|
|
|
|
fontawesome_version=6.2.1
|
2020-11-21 02:27:52 +00:00
|
|
|
fontawesome_url=https://github.com/FortAwesome/Font-Awesome/releases/download/$fontawesome_version/fontawesome-free-$fontawesome_version-web.zip
|
|
|
|
|
|
|
|
# Get FontAwesome
|
2022-11-21 02:04:28 +00:00
|
|
|
wget_verify $fontawesome_url cd0f2bcc9653b56e3e2dd82d6598aa6bbca8d796 /tmp/fontawesome.zip
|
2020-11-21 02:27:52 +00:00
|
|
|
unzip -q /tmp/fontawesome.zip -d $assets_dir
|
|
|
|
mv $assets_dir/fontawesome-free-$fontawesome_version-web $assets_dir/fontawesome
|
|
|
|
rm -f /tmp/fontawesome.zip
|
|
|
|
|
2014-06-03 13:24:48 +00:00
|
|
|
# Create an init script to start the management daemon and keep it
|
|
|
|
# running after a reboot.
|
2022-10-08 12:17:24 +00:00
|
|
|
# Set a long timeout since some commands take a while to run, matching
|
|
|
|
# the timeout we set for PHP (fastcgi_read_timeout in the nginx confs).
|
2022-09-17 12:03:16 +00:00
|
|
|
# Note: Authentication currently breaks with more than 1 gunicorn worker.
|
2018-01-15 17:43:32 +00:00
|
|
|
cat > $inst_dir/start <<EOF;
|
|
|
|
#!/bin/bash
|
2020-12-20 00:11:58 +00:00
|
|
|
# Set character encoding flags to ensure that any non-ASCII don't cause problems.
|
|
|
|
export LANGUAGE=en_US.UTF-8
|
|
|
|
export LC_ALL=en_US.UTF-8
|
|
|
|
export LANG=en_US.UTF-8
|
|
|
|
export LC_TYPE=en_US.UTF-8
|
|
|
|
|
2022-09-17 12:03:16 +00:00
|
|
|
mkdir -p /var/lib/mailinabox
|
|
|
|
tr -cd '[:xdigit:]' < /dev/urandom | head -c 32 > /var/lib/mailinabox/api.key
|
|
|
|
chmod 640 /var/lib/mailinabox/api.key
|
|
|
|
|
2018-01-15 17:43:32 +00:00
|
|
|
source $venv/bin/activate
|
2022-09-17 12:03:16 +00:00
|
|
|
export PYTHONPATH=$(pwd)/management
|
2022-10-08 12:17:24 +00:00
|
|
|
exec gunicorn -b localhost:10222 -w 1 --timeout 630 wsgi:app
|
2018-01-15 17:43:32 +00:00
|
|
|
EOF
|
|
|
|
chmod +x $inst_dir/start
|
2019-12-01 22:43:56 +00:00
|
|
|
cp --remove-destination conf/mailinabox.service /lib/systemd/system/mailinabox.service # target was previously a symlink so remove it first
|
2019-12-01 21:15:04 +00:00
|
|
|
hide_output systemctl link -f /lib/systemd/system/mailinabox.service
|
2018-07-07 18:41:41 +00:00
|
|
|
hide_output systemctl daemon-reload
|
|
|
|
hide_output systemctl enable mailinabox.service
|
2014-06-03 13:24:48 +00:00
|
|
|
|
2015-12-26 13:39:22 +00:00
|
|
|
# Perform nightly tasks at 3am in system time: take a backup, run
|
|
|
|
# status checks and email the administrator any changes.
|
2015-12-23 22:29:13 +00:00
|
|
|
|
2020-05-03 14:34:42 +00:00
|
|
|
minute=$((RANDOM % 60)) # avoid overloading mailinabox.email
|
2015-12-26 13:39:22 +00:00
|
|
|
cat > /etc/cron.d/mailinabox-nightly << EOF;
|
2015-03-08 21:56:28 +00:00
|
|
|
# Mail-in-a-Box --- Do not edit / will be overwritten on update.
|
2015-12-26 13:39:22 +00:00
|
|
|
# Run nightly tasks: backup, status checks.
|
2021-06-27 21:24:26 +00:00
|
|
|
$minute 3 * * * root (cd $(pwd) && management/daily_tasks.sh)
|
2015-03-08 21:56:28 +00:00
|
|
|
EOF
|
|
|
|
|
2015-10-10 22:03:55 +00:00
|
|
|
# Start the management server.
|
2014-07-16 13:06:45 +00:00
|
|
|
restart_service mailinabox
|
2021-02-01 02:20:38 +00:00
|
|
|
|
|
|
|
# FOR DEVELOPMENT PURPOSES ONLY:
|
|
|
|
# If there is a CA certificate in the folder, install it.
|
|
|
|
# MIAB will only accept a manual certificate installation
|
|
|
|
# if it is signed by a CA trusted by it.
|
|
|
|
if [[ -f mailinabox-ca.crt ]]; then
|
|
|
|
echo "Custom CA certificate detected. Installing..."
|
|
|
|
rm -f /usr/local/share/ca-certificates/mailinabox-ca.crt
|
|
|
|
cp mailinabox-ca.crt /usr/local/share/ca-certificates/
|
2021-07-02 23:46:31 +00:00
|
|
|
hide_output update-ca-certificates --fresh
|
2021-02-01 02:20:38 +00:00
|
|
|
fi
|