2017-03-02 11:23:23 +01:00
#!/bin/bash
set -e
2018-06-03 19:22:44 +02:00
# Wait for MySQL to warm-up
2018-10-11 11:53:22 +02:00
while ! mysqladmin status --socket= /var/run/mysqld/mysqld.sock -u${ DBUSER } -p${ DBPASS } --silent; do
2018-06-03 19:22:44 +02:00
echo "Waiting for database to come up..."
sleep 2
done
2018-08-02 12:14:13 +02:00
# Hard-code env vars to scripts due to cron not passing them to the perl script
2017-03-02 11:23:23 +01:00
sed -i " /^\$DBUSER/c\\\$DBUSER=' ${ DBUSER } '; " /usr/local/bin/imapsync_cron.pl
sed -i " /^\$DBPASS/c\\\$DBPASS=' ${ DBPASS } '; " /usr/local/bin/imapsync_cron.pl
sed -i " /^\$DBNAME/c\\\$DBNAME=' ${ DBNAME } '; " /usr/local/bin/imapsync_cron.pl
2018-08-02 12:14:13 +02:00
sed -i " s/LOG_LINES/ ${ LOG_LINES } /g " /usr/local/bin/trim_logs.sh
2017-03-02 11:23:23 +01:00
2017-04-05 22:25:16 +02:00
# Create missing directories
2017-04-03 20:06:49 +02:00
[ [ ! -d /usr/local/etc/dovecot/sql/ ] ] && mkdir -p /usr/local/etc/dovecot/sql/
[Docker API] Use TLS encryption for communication with "on-the-fly" created key paris (non-exposed)
[Docker API] Create pipe to pass Rspamd UI worker password
[Dovecot] Pull Spamassassin ruleset to be read by Rspamd (MANY THANKS to Peer Heinlein!)
[Dovecot] Garbage collector for deleted maildirs (set keep time via MAILDIR_GC_TIME which defaults to 1440 minutes)
[Web] Flush memcached after mailbox item changes, fixes #1808
[Web] Fix duplicate IDs, fixes #1792
[Compose] Use SQL sockets
[PHP-FPM] Update APCu and Redis libs
[Dovecot] Encrypt maildir with global key pair in crypt-vol-1 (BACKUP!), also fixes #1791
[Web] Fix deletion of spam aliases
[Helper] Add "crypt" to backup script
[Helper] Override file for external SQL socket (not supported!)
[Compose] New images for Rspamd, PHP-FPM, SOGo, Dovecot, Docker API, Watchdog, ACME, Postfix
2018-09-29 22:01:23 +02:00
[ [ ! -d /var/vmail/_garbage ] ] && mkdir -p /var/vmail/_garbage
2017-04-03 20:06:49 +02:00
[ [ ! -d /var/vmail/sieve ] ] && mkdir -p /var/vmail/sieve
[ [ ! -d /etc/sogo ] ] && mkdir -p /etc/sogo
2018-11-12 09:49:23 +01:00
[ [ ! -d /var/volatile ] ] && mkdir -p /var/volatile
2017-03-08 17:58:00 +01:00
2017-03-06 10:33:44 +01:00
# Set Dovecot sql config parameters, escape " in db password
2017-03-02 11:23:23 +01:00
DBPASS = $( echo ${ DBPASS } | sed 's/"/\\"/g' )
2017-03-06 10:33:44 +01:00
2017-04-03 20:06:49 +02:00
# Create quota dict for Dovecot
2017-11-03 20:25:38 +01:00
cat <<EOF > /usr/local/etc/dovecot/sql/dovecot-dict-sql-quota.conf
[Docker API] Use TLS encryption for communication with "on-the-fly" created key paris (non-exposed)
[Docker API] Create pipe to pass Rspamd UI worker password
[Dovecot] Pull Spamassassin ruleset to be read by Rspamd (MANY THANKS to Peer Heinlein!)
[Dovecot] Garbage collector for deleted maildirs (set keep time via MAILDIR_GC_TIME which defaults to 1440 minutes)
[Web] Flush memcached after mailbox item changes, fixes #1808
[Web] Fix duplicate IDs, fixes #1792
[Compose] Use SQL sockets
[PHP-FPM] Update APCu and Redis libs
[Dovecot] Encrypt maildir with global key pair in crypt-vol-1 (BACKUP!), also fixes #1791
[Web] Fix deletion of spam aliases
[Helper] Add "crypt" to backup script
[Helper] Override file for external SQL socket (not supported!)
[Compose] New images for Rspamd, PHP-FPM, SOGo, Dovecot, Docker API, Watchdog, ACME, Postfix
2018-09-29 22:01:23 +02:00
connect = " host=/var/run/mysqld/mysqld.sock dbname= ${ DBNAME } user= ${ DBUSER } password= ${ DBPASS } "
2017-03-06 10:33:44 +01:00
map {
pattern = priv/quota/storage
table = quota2
username_field = username
value_field = bytes
}
map {
pattern = priv/quota/messages
table = quota2
username_field = username
value_field = messages
}
EOF
2017-11-03 20:25:38 +01:00
# Create dict used for sieve pre and postfilters
cat <<EOF > /usr/local/etc/dovecot/sql/dovecot-dict-sql-sieve_before.conf
[Docker API] Use TLS encryption for communication with "on-the-fly" created key paris (non-exposed)
[Docker API] Create pipe to pass Rspamd UI worker password
[Dovecot] Pull Spamassassin ruleset to be read by Rspamd (MANY THANKS to Peer Heinlein!)
[Dovecot] Garbage collector for deleted maildirs (set keep time via MAILDIR_GC_TIME which defaults to 1440 minutes)
[Web] Flush memcached after mailbox item changes, fixes #1808
[Web] Fix duplicate IDs, fixes #1792
[Compose] Use SQL sockets
[PHP-FPM] Update APCu and Redis libs
[Dovecot] Encrypt maildir with global key pair in crypt-vol-1 (BACKUP!), also fixes #1791
[Web] Fix deletion of spam aliases
[Helper] Add "crypt" to backup script
[Helper] Override file for external SQL socket (not supported!)
[Compose] New images for Rspamd, PHP-FPM, SOGo, Dovecot, Docker API, Watchdog, ACME, Postfix
2018-09-29 22:01:23 +02:00
connect = " host=/var/run/mysqld/mysqld.sock dbname= ${ DBNAME } user= ${ DBUSER } password= ${ DBPASS } "
2017-11-03 20:25:38 +01:00
map {
pattern = priv/sieve/name/\$ script_name
table = sieve_before
username_field = username
value_field = id
fields {
script_name = \$ script_name
}
}
map {
pattern = priv/sieve/data/\$ id
table = sieve_before
username_field = username
value_field = script_data
fields {
id = \$ id
}
}
EOF
cat <<EOF > /usr/local/etc/dovecot/sql/dovecot-dict-sql-sieve_after.conf
[Docker API] Use TLS encryption for communication with "on-the-fly" created key paris (non-exposed)
[Docker API] Create pipe to pass Rspamd UI worker password
[Dovecot] Pull Spamassassin ruleset to be read by Rspamd (MANY THANKS to Peer Heinlein!)
[Dovecot] Garbage collector for deleted maildirs (set keep time via MAILDIR_GC_TIME which defaults to 1440 minutes)
[Web] Flush memcached after mailbox item changes, fixes #1808
[Web] Fix duplicate IDs, fixes #1792
[Compose] Use SQL sockets
[PHP-FPM] Update APCu and Redis libs
[Dovecot] Encrypt maildir with global key pair in crypt-vol-1 (BACKUP!), also fixes #1791
[Web] Fix deletion of spam aliases
[Helper] Add "crypt" to backup script
[Helper] Override file for external SQL socket (not supported!)
[Compose] New images for Rspamd, PHP-FPM, SOGo, Dovecot, Docker API, Watchdog, ACME, Postfix
2018-09-29 22:01:23 +02:00
connect = " host=/var/run/mysqld/mysqld.sock dbname= ${ DBNAME } user= ${ DBUSER } password= ${ DBPASS } "
2017-11-03 20:25:38 +01:00
map {
pattern = priv/sieve/name/\$ script_name
table = sieve_after
username_field = username
value_field = id
fields {
script_name = \$ script_name
}
}
map {
pattern = priv/sieve/data/\$ id
table = sieve_after
username_field = username
value_field = script_data
fields {
id = \$ id
}
}
EOF
2018-07-12 00:23:12 +02:00
# Create userdb dict for Dovecot
cat <<EOF > /usr/local/etc/dovecot/sql/dovecot-dict-sql-userdb.conf
2017-03-06 10:33:44 +01:00
driver = mysql
[Docker API] Use TLS encryption for communication with "on-the-fly" created key paris (non-exposed)
[Docker API] Create pipe to pass Rspamd UI worker password
[Dovecot] Pull Spamassassin ruleset to be read by Rspamd (MANY THANKS to Peer Heinlein!)
[Dovecot] Garbage collector for deleted maildirs (set keep time via MAILDIR_GC_TIME which defaults to 1440 minutes)
[Web] Flush memcached after mailbox item changes, fixes #1808
[Web] Fix duplicate IDs, fixes #1792
[Compose] Use SQL sockets
[PHP-FPM] Update APCu and Redis libs
[Dovecot] Encrypt maildir with global key pair in crypt-vol-1 (BACKUP!), also fixes #1791
[Web] Fix deletion of spam aliases
[Helper] Add "crypt" to backup script
[Helper] Override file for external SQL socket (not supported!)
[Compose] New images for Rspamd, PHP-FPM, SOGo, Dovecot, Docker API, Watchdog, ACME, Postfix
2018-09-29 22:01:23 +02:00
connect = " host=/var/run/mysqld/mysqld.sock dbname= ${ DBNAME } user= ${ DBUSER } password= ${ DBPASS } "
2018-11-12 20:59:39 +01:00
user_query = SELECT CONCAT( JSON_UNQUOTE( JSON_EXTRACT( attributes, '$.mailbox_format' ) ) , mailbox_path_prefix, '%d/%n/:UTF-8:VOLATILEDIR=/var/volatile/%u' ) AS mail, 5000 AS uid, 5000 AS gid, concat( '*:bytes=' , quota) AS quota_rule FROM mailbox WHERE username = '%u' AND active = '1'
2017-03-06 10:33:44 +01:00
iterate_query = SELECT username FROM mailbox WHERE active = '1' ;
EOF
2017-03-02 11:23:23 +01:00
2018-07-12 00:46:17 +02:00
# Create pass dict for Dovecot
cat <<EOF > /usr/local/etc/dovecot/sql/dovecot-dict-sql-passdb.conf
2018-07-12 00:23:12 +02:00
driver = mysql
[Docker API] Use TLS encryption for communication with "on-the-fly" created key paris (non-exposed)
[Docker API] Create pipe to pass Rspamd UI worker password
[Dovecot] Pull Spamassassin ruleset to be read by Rspamd (MANY THANKS to Peer Heinlein!)
[Dovecot] Garbage collector for deleted maildirs (set keep time via MAILDIR_GC_TIME which defaults to 1440 minutes)
[Web] Flush memcached after mailbox item changes, fixes #1808
[Web] Fix duplicate IDs, fixes #1792
[Compose] Use SQL sockets
[PHP-FPM] Update APCu and Redis libs
[Dovecot] Encrypt maildir with global key pair in crypt-vol-1 (BACKUP!), also fixes #1791
[Web] Fix deletion of spam aliases
[Helper] Add "crypt" to backup script
[Helper] Override file for external SQL socket (not supported!)
[Compose] New images for Rspamd, PHP-FPM, SOGo, Dovecot, Docker API, Watchdog, ACME, Postfix
2018-09-29 22:01:23 +02:00
connect = " host=/var/run/mysqld/mysqld.sock dbname= ${ DBNAME } user= ${ DBUSER } password= ${ DBPASS } ssl_verify_server_cert=no ssl_ca=/etc/ssl/certs/ca-certificates.crt "
2018-07-12 00:23:12 +02:00
default_pass_scheme = SSHA256
password_query = SELECT password FROM mailbox WHERE username = '%u' AND domain IN ( SELECT domain FROM domain WHERE domain = '%d' AND active = '1' ) AND JSON_EXTRACT( attributes, '$.force_pw_update' ) NOT LIKE '%%1%%'
EOF
2017-04-03 20:06:49 +02:00
# Create global sieve_after script
cat /usr/local/etc/dovecot/sieve_after > /var/vmail/sieve/global.sieve
2018-11-12 09:49:23 +01:00
# Check permissions of vmail/attachments directory.
2017-03-02 11:23:23 +01:00
# Do not do this every start-up, it may take a very long time. So we use a stat check here.
if [ [ $( stat -c %U /var/vmail/) != "vmail" ] ] ; then chown -R vmail:vmail /var/vmail ; fi
[Docker API] Use TLS encryption for communication with "on-the-fly" created key paris (non-exposed)
[Docker API] Create pipe to pass Rspamd UI worker password
[Dovecot] Pull Spamassassin ruleset to be read by Rspamd (MANY THANKS to Peer Heinlein!)
[Dovecot] Garbage collector for deleted maildirs (set keep time via MAILDIR_GC_TIME which defaults to 1440 minutes)
[Web] Flush memcached after mailbox item changes, fixes #1808
[Web] Fix duplicate IDs, fixes #1792
[Compose] Use SQL sockets
[PHP-FPM] Update APCu and Redis libs
[Dovecot] Encrypt maildir with global key pair in crypt-vol-1 (BACKUP!), also fixes #1791
[Web] Fix deletion of spam aliases
[Helper] Add "crypt" to backup script
[Helper] Override file for external SQL socket (not supported!)
[Compose] New images for Rspamd, PHP-FPM, SOGo, Dovecot, Docker API, Watchdog, ACME, Postfix
2018-09-29 22:01:23 +02:00
if [ [ $( stat -c %U /var/vmail/_garbage) != "vmail" ] ] ; then chown -R vmail:vmail /var/vmail/_garbage ; fi
2018-11-12 09:49:23 +01:00
if [ [ $( stat -c %U /var/attachments) != "vmail" ] ] ; then chown -R vmail:vmail /var/attachments ; fi
2017-03-02 11:23:23 +01:00
# Create random master for SOGo sieve features
RAND_USER = $( cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 16 | head -n 1)
RAND_PASS = $( cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 24 | head -n 1)
2018-08-28 17:14:05 +02:00
2018-11-12 09:49:23 +01:00
echo ${ RAND_USER } @mailcow.local:{ SHA1} $( echo -n ${ RAND_PASS } | sha1sum | awk '{print $1}' ) > /usr/local/etc/dovecot/dovecot-master.passwd
2018-08-28 17:14:05 +02:00
echo ${ RAND_USER } @mailcow.local:${ RAND_PASS } > /etc/sogo/sieve.creds
2017-03-02 11:23:23 +01:00
2017-04-05 22:25:16 +02:00
# 401 is user dovecot
2018-10-07 15:09:21 +02:00
if [ [ ! -s /mail_crypt/ecprivkey.pem || ! -s /mail_crypt/ecpubkey.pem ] ] ; then
2017-04-03 20:06:49 +02:00
openssl ecparam -name prime256v1 -genkey | openssl pkey -out /mail_crypt/ecprivkey.pem
openssl pkey -in /mail_crypt/ecprivkey.pem -pubout -out /mail_crypt/ecpubkey.pem
2017-04-05 22:25:16 +02:00
chown 401 /mail_crypt/ecprivkey.pem /mail_crypt/ecpubkey.pem
else
chown 401 /mail_crypt/ecprivkey.pem /mail_crypt/ecpubkey.pem
2017-04-03 20:06:49 +02:00
fi
2017-04-05 22:25:16 +02:00
# Compile sieve scripts
sievec /var/vmail/sieve/global.sieve
sievec /usr/local/lib/dovecot/sieve/report-spam.sieve
sievec /usr/local/lib/dovecot/sieve/report-ham.sieve
# Fix permissions
2018-10-12 10:57:00 +02:00
chown root:root /usr/local/etc/dovecot/sql/*.conf
chown root:dovecot /usr/local/etc/dovecot/sql/dovecot-dict-sql-sieve* /usr/local/etc/dovecot/sql/dovecot-dict-sql-quota*
chmod 640 /usr/local/etc/dovecot/sql/*.conf
2017-04-05 22:25:16 +02:00
chown -R vmail:vmail /var/vmail/sieve
2018-11-12 09:49:23 +01:00
chown -R vmail:vmail /var/volatile
adduser vmail tty
chmod g+rw /dev/console
2017-04-05 22:25:16 +02:00
2017-05-09 21:29:51 +02:00
# Fix more than 1 hardlink issue
touch /etc/crontab /etc/cron.*/*
2017-04-05 22:25:16 +02:00
2017-07-31 08:17:56 +02:00
# Clean old PID if any
[ [ -f /usr/local/var/run/dovecot/master.pid ] ] && rm /usr/local/var/run/dovecot/master.pid
2018-06-03 19:22:44 +02:00
# Clean stopped imapsync jobs
2018-07-27 22:19:14 +02:00
rm -f /tmp/imapsync_busy.lock
[Docker API] Use TLS encryption for communication with "on-the-fly" created key paris (non-exposed)
[Docker API] Create pipe to pass Rspamd UI worker password
[Dovecot] Pull Spamassassin ruleset to be read by Rspamd (MANY THANKS to Peer Heinlein!)
[Dovecot] Garbage collector for deleted maildirs (set keep time via MAILDIR_GC_TIME which defaults to 1440 minutes)
[Web] Flush memcached after mailbox item changes, fixes #1808
[Web] Fix duplicate IDs, fixes #1792
[Compose] Use SQL sockets
[PHP-FPM] Update APCu and Redis libs
[Dovecot] Encrypt maildir with global key pair in crypt-vol-1 (BACKUP!), also fixes #1791
[Web] Fix deletion of spam aliases
[Helper] Add "crypt" to backup script
[Helper] Override file for external SQL socket (not supported!)
[Compose] New images for Rspamd, PHP-FPM, SOGo, Dovecot, Docker API, Watchdog, ACME, Postfix
2018-09-29 22:01:23 +02:00
IMAPSYNC_TABLE = $( mysql --socket= /var/run/mysqld/mysqld.sock -u ${ DBUSER } -p${ DBPASS } ${ DBNAME } -e "SHOW TABLES LIKE 'imapsync'" -Bs)
[ [ ! -z ${ IMAPSYNC_TABLE } ] ] && mysql --socket= /var/run/mysqld/mysqld.sock -u ${ DBUSER } -p${ DBPASS } ${ DBNAME } -e "UPDATE imapsync SET is_running='0'"
2018-06-03 19:22:44 +02:00
2018-10-02 09:32:51 +02:00
# Collect SA rules once now
/usr/local/bin/sa-rules.sh
2017-03-02 11:23:23 +01:00
exec " $@ "