2014-04-23 22:54:05 +00:00
|
|
|
#!/bin/bash
|
2013-09-01 14:13:51 +00:00
|
|
|
# This is the entry point for configuring the system.
|
|
|
|
#####################################################
|
|
|
|
|
2014-06-07 17:54:58 +00:00
|
|
|
source setup/functions.sh # load our functions
|
|
|
|
|
2014-06-30 14:20:58 +00:00
|
|
|
if [ -t 0 ]; then
|
|
|
|
# In an interactive shell...
|
|
|
|
echo
|
|
|
|
echo "Hello and thanks for deploying a Mail-in-a-Box!"
|
|
|
|
echo "-----------------------------------------------"
|
|
|
|
echo
|
|
|
|
echo "I'm going to ask you a few questions. To change your answers later,"
|
|
|
|
echo "later, just re-run this script."
|
|
|
|
fi
|
|
|
|
|
2013-08-31 13:05:58 +00:00
|
|
|
# Check system setup.
|
2013-09-01 14:13:51 +00:00
|
|
|
|
2014-04-18 00:17:24 +00:00
|
|
|
if [ "`lsb_release -d | sed 's/.*:\s*//'`" != "Ubuntu 14.04 LTS" ]; then
|
2014-06-30 14:20:58 +00:00
|
|
|
echo
|
2014-04-18 00:17:24 +00:00
|
|
|
echo "Mail-in-a-Box only supports being installed on Ubuntu 14.04, sorry. You are running:"
|
2014-03-17 00:42:00 +00:00
|
|
|
echo
|
|
|
|
lsb_release -d | sed 's/.*:\s*//'
|
|
|
|
echo
|
|
|
|
echo "We can't write scripts that run on every possible setup, sorry."
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2014-06-03 21:31:13 +00:00
|
|
|
# Recall the last settings used if we're running this a second time.
|
|
|
|
if [ -f /etc/mailinabox.conf ]; then
|
2014-06-30 20:41:29 +00:00
|
|
|
# Run any system migrations before proceeding. Since this is a second run,
|
|
|
|
# we assume we have Python already installed.
|
2014-07-10 12:49:19 +00:00
|
|
|
setup/migrate.py --migrate
|
2014-06-30 20:41:29 +00:00
|
|
|
|
|
|
|
# Okay now load the old .conf file to get existing configuration options.
|
2014-06-03 21:31:13 +00:00
|
|
|
cat /etc/mailinabox.conf | sed s/^/DEFAULT_/ > /tmp/mailinabox.prev.conf
|
|
|
|
source /tmp/mailinabox.prev.conf
|
2014-07-07 11:29:21 +00:00
|
|
|
MIGRATIONID=$DEFAULT_MIGRATIONID
|
|
|
|
else
|
|
|
|
# What migration are we at for new installs?
|
2014-07-10 12:49:19 +00:00
|
|
|
MIGRATIONID=$(setup/migrate.py --current)
|
2014-06-03 21:31:13 +00:00
|
|
|
fi
|
2013-08-31 13:05:58 +00:00
|
|
|
|
2014-06-30 14:20:58 +00:00
|
|
|
# The box needs a name.
|
2014-06-30 13:15:36 +00:00
|
|
|
if [ -z "$PRIMARY_HOSTNAME" ]; then
|
|
|
|
if [ -z "$DEFAULT_PRIMARY_HOSTNAME" ]; then
|
2014-06-30 14:20:58 +00:00
|
|
|
# This is the first run. Ask the user for his email address so we can
|
|
|
|
# provide the best default for the box's hostname.
|
|
|
|
echo
|
|
|
|
echo "What email address are you setting this box up to manage?"
|
|
|
|
echo ""
|
|
|
|
echo "The part after the @-sign must be a domain name or subdomain"
|
|
|
|
echo "that you control. You can add other email addresses to this"
|
|
|
|
echo "box later (including email addresses on other domain names"
|
|
|
|
echo "or subdomains you control)."
|
|
|
|
echo
|
|
|
|
echo "We've guessed an email address. Backspace it and type in what"
|
|
|
|
echo "you really want."
|
|
|
|
echo
|
|
|
|
read -e -i "me@`get_default_hostname`" -p "Email Address: " EMAIL_ADDR
|
|
|
|
|
|
|
|
while ! management/mailconfig.py validate-email "$EMAIL_ADDR"
|
|
|
|
do
|
|
|
|
echo "That's not a valid email address."
|
|
|
|
echo
|
|
|
|
read -e -i "$EMAIL_ADDR" -p "Email Address: " EMAIL_ADDR
|
|
|
|
done
|
|
|
|
|
|
|
|
# Take the part after the @-sign as the user's domain name, and add
|
|
|
|
# 'box.' to the beginning to create a default hostname for this machine.
|
|
|
|
DEFAULT_PRIMARY_HOSTNAME=box.$(echo $EMAIL_ADDR | sed 's/.*@//')
|
2014-06-08 22:44:08 +00:00
|
|
|
fi
|
|
|
|
|
2013-08-26 21:01:48 +00:00
|
|
|
echo
|
2014-06-30 14:20:58 +00:00
|
|
|
echo "This box needs a name, called a 'hostname'. The name will form a part"
|
|
|
|
echo "of the box's web address."
|
|
|
|
echo
|
|
|
|
echo "We recommend that the name be a subdomain of the domain in your email"
|
|
|
|
echo "address, so we're suggesting $DEFAULT_PRIMARY_HOSTNAME."
|
|
|
|
echo
|
|
|
|
echo "You can change it, but we recommend you don't."
|
2013-08-31 14:46:10 +00:00
|
|
|
echo
|
2014-06-03 21:31:13 +00:00
|
|
|
|
2014-06-30 13:15:36 +00:00
|
|
|
read -e -i "$DEFAULT_PRIMARY_HOSTNAME" -p "Hostname: " PRIMARY_HOSTNAME
|
2013-08-26 21:01:48 +00:00
|
|
|
fi
|
|
|
|
|
2014-06-30 14:20:58 +00:00
|
|
|
# If the machine is behind a NAT, inside a VM, etc., it may not know
|
|
|
|
# its IP address on the public network / the Internet. We need to
|
|
|
|
# confirm our best guess with the user.
|
2013-08-26 21:01:48 +00:00
|
|
|
if [ -z "$PUBLIC_IP" ]; then
|
2014-06-08 22:44:08 +00:00
|
|
|
if [ -z "$DEFAULT_PUBLIC_IP" ]; then
|
|
|
|
# set a default on first run
|
|
|
|
DEFAULT_PUBLIC_IP=`get_default_publicip`
|
|
|
|
fi
|
|
|
|
|
2013-08-26 21:01:48 +00:00
|
|
|
echo
|
2014-06-30 14:20:58 +00:00
|
|
|
echo "Enter the public IP address of this machine, as given to you by your"
|
|
|
|
echo "ISP. We've guessed a value, but just backspace it if it's wrong."
|
2013-08-31 14:46:10 +00:00
|
|
|
echo
|
2014-06-03 21:31:13 +00:00
|
|
|
|
|
|
|
read -e -i "$DEFAULT_PUBLIC_IP" -p "Public IP: " PUBLIC_IP
|
2013-08-26 21:01:48 +00:00
|
|
|
fi
|
|
|
|
|
2014-06-30 14:20:58 +00:00
|
|
|
# Same for IPv6.
|
2014-06-08 22:32:52 +00:00
|
|
|
if [ -z "$PUBLIC_IPV6" ]; then
|
2014-06-08 22:44:08 +00:00
|
|
|
if [ -z "$DEFAULT_PUBLIC_IPV6" ]; then
|
2014-06-08 22:32:52 +00:00
|
|
|
# set a default on first run
|
|
|
|
DEFAULT_PUBLIC_IPV6=`get_default_publicipv6`
|
|
|
|
fi
|
|
|
|
|
2014-06-08 22:44:08 +00:00
|
|
|
echo
|
|
|
|
echo "(Optional) Enter the IPv6 address of this machine. Leave blank"
|
|
|
|
echo " if the machine does not have an IPv6 address."
|
|
|
|
|
2014-06-08 22:32:52 +00:00
|
|
|
read -e -i "$DEFAULT_PUBLIC_IPV6" -p "Public IPv6: " PUBLIC_IPV6
|
|
|
|
fi
|
|
|
|
|
2014-06-22 12:48:21 +00:00
|
|
|
# We need a country code to generate a certificate signing request. However
|
|
|
|
# if a CSR already exists then we won't be generating a new one and there's
|
|
|
|
# no reason to ask for the country code now. $STORAGE_ROOT has not yet been
|
|
|
|
# set so we'll check if $DEFAULT_STORAGE_ROOT and $DEFAULT_CSR_COUNTRY are
|
|
|
|
# set (the values from the current mailinabox.conf) and if the CSR exists
|
|
|
|
# in the expected location.
|
|
|
|
if [ ! -z "$DEFAULT_STORAGE_ROOT" ] && [ ! -z "$DEFAULT_CSR_COUNTRY" ] && [ -f $DEFAULT_STORAGE_ROOT/ssl/ssl_cert_sign_req.csr ]; then
|
|
|
|
CSR_COUNTRY=$DEFAULT_CSR_COUNTRY
|
|
|
|
fi
|
2014-06-03 21:17:10 +00:00
|
|
|
if [ -z "$CSR_COUNTRY" ]; then
|
|
|
|
echo
|
|
|
|
echo "Enter the two-letter, uppercase country code for where you"
|
|
|
|
echo "live or where your organization is based. (This is used to"
|
|
|
|
echo "create an SSL certificate.)"
|
|
|
|
echo
|
2014-06-03 21:31:13 +00:00
|
|
|
|
|
|
|
#if [ -z "$DEFAULT_CSR_COUNTRY" ]; then
|
|
|
|
# # set a default on first run
|
|
|
|
# DEFAULT_CSR_COUNTRY=...?
|
|
|
|
#fi
|
|
|
|
|
|
|
|
read -e -i "$DEFAULT_CSR_COUNTRY" -p "Country Code: " CSR_COUNTRY
|
2014-06-03 21:17:10 +00:00
|
|
|
fi
|
|
|
|
|
2014-06-04 23:39:58 +00:00
|
|
|
# Automatic configuration, e.g. as used in our Vagrant configuration.
|
2014-06-08 22:32:52 +00:00
|
|
|
if [ "$PUBLIC_IP" = "auto" ]; then
|
2014-06-04 23:39:58 +00:00
|
|
|
# Use a public API to get our public IP address.
|
2014-06-07 18:55:57 +00:00
|
|
|
PUBLIC_IP=`get_default_publicip`
|
2014-06-04 23:39:58 +00:00
|
|
|
echo "IP Address: $PUBLIC_IP"
|
|
|
|
fi
|
2014-06-08 22:32:52 +00:00
|
|
|
if [ "$PUBLIC_IPV6" = "auto" ]; then
|
|
|
|
# Use a public API to get our public IP address.
|
|
|
|
PUBLIC_IPV6=`get_default_publicipv6`
|
|
|
|
echo "IPv6 Address: $PUBLIC_IPV6"
|
|
|
|
fi
|
2014-06-30 13:15:36 +00:00
|
|
|
if [ "$PRIMARY_HOSTNAME" = "auto-easy" ]; then
|
2014-06-04 23:39:58 +00:00
|
|
|
# Generate a probably-unique subdomain under our justtesting.email domain.
|
2014-06-30 13:15:36 +00:00
|
|
|
PRIMARY_HOSTNAME=m`get_default_publicip | sha1sum | cut -c1-5`.justtesting.email
|
|
|
|
echo "Public Hostname: $PRIMARY_HOSTNAME"
|
2014-06-04 23:39:58 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2014-04-25 12:25:07 +00:00
|
|
|
# Create the user named "user-data" and store all persistent user
|
2013-09-01 14:13:51 +00:00
|
|
|
# data (mailboxes, etc.) in that user's home directory.
|
2013-08-26 21:01:48 +00:00
|
|
|
if [ -z "$STORAGE_ROOT" ]; then
|
2013-09-01 14:13:51 +00:00
|
|
|
STORAGE_USER=user-data
|
|
|
|
if [ ! -d /home/$STORAGE_USER ]; then useradd -m $STORAGE_USER; fi
|
|
|
|
STORAGE_ROOT=/home/$STORAGE_USER
|
2013-08-31 13:05:58 +00:00
|
|
|
mkdir -p $STORAGE_ROOT
|
2013-08-26 21:01:48 +00:00
|
|
|
fi
|
|
|
|
|
2013-09-01 14:13:51 +00:00
|
|
|
# Save the global options in /etc/mailinabox.conf so that standalone
|
|
|
|
# tools know where to look for data.
|
2013-08-31 14:46:10 +00:00
|
|
|
cat > /etc/mailinabox.conf << EOF;
|
2014-06-20 01:16:38 +00:00
|
|
|
STORAGE_USER=$STORAGE_USER
|
2013-08-31 14:46:10 +00:00
|
|
|
STORAGE_ROOT=$STORAGE_ROOT
|
2014-06-30 13:15:36 +00:00
|
|
|
PRIMARY_HOSTNAME=$PRIMARY_HOSTNAME
|
2013-09-08 11:47:27 +00:00
|
|
|
PUBLIC_IP=$PUBLIC_IP
|
2014-06-08 22:32:52 +00:00
|
|
|
PUBLIC_IPV6=$PUBLIC_IPV6
|
2014-06-03 21:17:10 +00:00
|
|
|
CSR_COUNTRY=$CSR_COUNTRY
|
2014-07-07 11:29:21 +00:00
|
|
|
MIGRATIONID=$MIGRATIONID
|
2013-08-31 14:46:10 +00:00
|
|
|
EOF
|
|
|
|
|
2013-09-01 14:13:51 +00:00
|
|
|
# Start service configuration.
|
2014-06-03 11:12:38 +00:00
|
|
|
. setup/system.sh
|
2014-06-21 22:15:53 +00:00
|
|
|
. setup/ssl.sh
|
2014-06-03 11:12:38 +00:00
|
|
|
. setup/dns.sh
|
|
|
|
. setup/mail.sh
|
|
|
|
. setup/dkim.sh
|
|
|
|
. setup/spamassassin.sh
|
|
|
|
. setup/web.sh
|
|
|
|
. setup/webmail.sh
|
2014-06-03 13:24:48 +00:00
|
|
|
. setup/management.sh
|
|
|
|
|
2014-06-20 01:16:38 +00:00
|
|
|
# Write the DNS and nginx configuration files.
|
2014-06-04 23:39:58 +00:00
|
|
|
sleep 5 # wait for the daemon to start
|
2014-06-22 00:02:52 +00:00
|
|
|
curl -s -d POSTDATA --user $(</var/lib/mailinabox/api.key): http://127.0.0.1:10222/dns/update
|
|
|
|
curl -s -d POSTDATA --user $(</var/lib/mailinabox/api.key): http://127.0.0.1:10222/web/update
|
2013-09-01 14:39:36 +00:00
|
|
|
|
2014-06-04 23:39:58 +00:00
|
|
|
# If there aren't any mail users yet, create one.
|
2014-01-27 15:41:59 +00:00
|
|
|
if [ -z "`tools/mail.py user`" ]; then
|
2013-09-01 14:39:36 +00:00
|
|
|
# The outut of "tools/mail.py user" is a list of mail users. If there
|
2014-06-04 23:39:58 +00:00
|
|
|
# aren't any yet, it'll be empty.
|
|
|
|
|
2014-06-30 14:20:58 +00:00
|
|
|
# If we didn't ask for an email address at the start, do so now.
|
|
|
|
if [ -z "$EMAIL_ADDR" ]; then
|
|
|
|
# In an interactive shell, ask the user for an email address.
|
|
|
|
if [ -t 0 ]; then
|
|
|
|
echo
|
|
|
|
echo "Let's create your first mail user."
|
|
|
|
read -e -i "user@$PRIMARY_HOSTNAME" -p "Email Address: " EMAIL_ADDR
|
|
|
|
|
|
|
|
# But in a non-interactive shell, just make something up. This
|
|
|
|
# is normally for testing.
|
|
|
|
else
|
|
|
|
# Use me@PRIMARY_HOSTNAME
|
|
|
|
EMAIL_ADDR=me@$PRIMARY_HOSTNAME
|
|
|
|
EMAIL_PW=1234
|
|
|
|
echo
|
|
|
|
echo "Creating a new mail account for $EMAIL_ADDR with password $EMAIL_PW."
|
|
|
|
echo
|
|
|
|
fi
|
2014-06-04 23:39:58 +00:00
|
|
|
else
|
|
|
|
echo
|
2014-06-30 14:20:58 +00:00
|
|
|
echo "Okay. I'm about to set up $EMAIL_ADDR for you."
|
2014-06-04 23:39:58 +00:00
|
|
|
fi
|
|
|
|
|
2014-07-09 19:29:46 +00:00
|
|
|
# Create the user's mail account. This will ask for a password if none was given above.
|
|
|
|
tools/mail.py user add $EMAIL_ADDR $EMAIL_PW
|
|
|
|
|
|
|
|
# Create an alias to which we'll direct all automatically-created administrative aliases.
|
|
|
|
tools/mail.py alias add administrator@$PRIMARY_HOSTNAME $EMAIL_ADDR
|
2013-09-01 14:39:36 +00:00
|
|
|
fi
|
2014-06-03 13:24:48 +00:00
|
|
|
|