mailcow/data/Dockerfiles/php-fpm/docker-entrypoint.sh

87 lines
2.7 KiB
Bash
Raw Normal View History

2017-03-02 18:23:23 +08:00
#!/bin/bash
set -e
if [[ ! -d "/data/dkim/txt" || ! -d "/data/dkim/keys" ]] ; then mkdir -p /data/dkim/{txt,keys} ; chown -R www-data:www-data /data/dkim; fi
2017-03-02 18:23:23 +08:00
if [[ $(stat -c %U /data/dkim/) != "www-data" ]] ; then chown -R www-data:www-data /data/dkim ; fi
# Wait for containers
while ! mysqladmin ping --host mysql --silent; do
sleep 2
done
2017-05-29 05:28:41 +08:00
until [ "$(redis-cli -h redis-mailcow PING)" == "PONG" ]; do
sleep 2
done
# Migrate domain map
declare -a DOMAIN_ARR
redis-cli -h redis-mailcow DEL DOMAIN_MAP
2017-05-29 05:28:41 +08:00
while read -r line
do
DOMAIN_ARR+=("$line")
2017-05-29 05:28:41 +08:00
done < <(mysql -h mysql-mailcow -u "${DBUSER}" -p"${DBPASS}" "${DBNAME}" -e "SELECT domain FROM domain" -Bs)
if [[ ! -z ${DOMAIN_ARR} ]]; then
for domain in "${DOMAIN_ARR[@]}"; do
2017-05-29 05:28:41 +08:00
redis-cli -h redis-mailcow HSET DOMAIN_MAP "${domain}" 1
done
fi
# Migrate tag settings map
declare -a SUBJ_TAG_ARR
redis-cli -h redis-mailcow DEL SUBJ_TAG_ARR
2017-05-29 05:28:41 +08:00
while read -r line
do
SUBJ_TAG_ARR+=("$line")
2017-05-29 05:28:41 +08:00
done < <(mysql -h mysql-mailcow -u "${DBUSER}" -p"${DBPASS}" "${DBNAME}" -e "SELECT username FROM mailbox WHERE wants_tagged_subject='1'" -Bs)
if [[ ! -z ${SUBJ_TAG_ARR} ]]; then
for user in "${SUBJ_TAG_ARR[@]}"; do
2017-05-29 05:28:41 +08:00
redis-cli -h redis-mailcow HSET RCPT_WANTS_SUBJECT_TAG "${user}" 1
mysql -h mysql-mailcow -u "${DBUSER}" -p"${DBPASS}" "${DBNAME}" -e "UPDATE mailbox SET wants_tagged_subject='2' WHERE username = '${user}'"
done
fi
# Migrate DKIM keys
2017-05-29 05:28:41 +08:00
for file in /data/dkim/keys/*; do
[[ -e $file ]] || break # handle the case of no files
domain=${file%.dkim}
if [[ -f /data/dkim/txt/${file} ]]; then
2017-05-14 20:23:26 +08:00
redis-cli -h redis-mailcow HSET DKIM_PUB_KEYS "${domain}" "$(cat /data/dkim/txt/${file})"
2017-05-18 03:16:48 +08:00
redis-cli -h redis-mailcow HSET DKIM_PRIV_KEYS "dkim.${domain}" "$(cat /data/dkim/keys/${file})"
2017-05-15 02:07:43 +08:00
redis-cli -h redis-mailcow HSET DKIM_SELECTORS "${domain}" "dkim"
fi
2017-05-29 05:28:41 +08:00
rm /data/dkim/{keys,txt}/"${file}"
done
2017-05-18 03:16:48 +08:00
# Fix DKIM keys
# Fetch domains
declare -a DOMAIN_ARRAY
2017-05-29 05:28:41 +08:00
while read -r line
2017-05-18 03:16:48 +08:00
do
DOMAIN_ARRAY+=("$line")
2017-05-29 05:28:41 +08:00
done < <(mysql -h mysql-mailcow -u "${DBUSER}" -p"${DBPASS}" "${DBNAME}" -e "SELECT domain FROM domain" -Bs)
while read -r line
2017-05-18 03:16:48 +08:00
do
DOMAIN_ARRAY+=("$line")
2017-05-29 05:28:41 +08:00
done < <(mysql -h mysql-mailcow -u "${DBUSER}" -p"${DBPASS}" "${DBNAME}" -e "SELECT alias_domain FROM alias_domain" -Bs)
2017-05-18 03:16:48 +08:00
# Loop through array and fix keys
if [[ ! -z ${DOMAIN_ARRAY} ]]; then
for domain in "${DOMAIN_ARRAY[@]}"; do
2017-05-29 05:28:41 +08:00
WRONG_KEY=$(redis-cli -h redis-mailcow HGET DKIM_PRIV_KEYS "${domain}" | tr -d \")
2017-05-18 03:16:48 +08:00
if [[ ! -z ${WRONG_KEY} ]]; then
echo "Migrating defect key for domain ${domain}"
2017-05-29 05:28:41 +08:00
redis-cli -h redis-mailcow HSET DKIM_PRIV_KEYS "dkim.${domain}" "${WRONG_KEY}"
2017-05-18 03:16:48 +08:00
redis-cli -h redis-mailcow HDEL DKIM_PRIV_KEYS "${domain}"
fi
done
fi
2017-03-02 18:23:23 +08:00
exec "$@"