2020-10-21 16:16:32 +00:00
|
|
|
#! /bin/bash
|
2016-04-20 21:01:32 +00:00
|
|
|
|
2016-04-20 23:08:14 +00:00
|
|
|
touch /tmp/vhost.tmp
|
|
|
|
|
2018-03-02 21:17:18 +00:00
|
|
|
# if no keysize is provided, 2048 is default.
|
2021-01-27 13:09:24 +00:00
|
|
|
KEYSIZE=${1:-4096}
|
|
|
|
# optional domain names
|
|
|
|
DOMAINS=${2:-}
|
2018-03-02 21:17:18 +00:00
|
|
|
|
2021-01-27 13:09:24 +00:00
|
|
|
if [[ -z ${DOMAINS} ]]
|
2020-10-21 16:16:32 +00:00
|
|
|
then
|
2021-01-27 13:09:24 +00:00
|
|
|
# getting domains FROM mail accounts
|
|
|
|
if [[ -f /tmp/docker-mailserver/postfix-accounts.cf ]]
|
|
|
|
then
|
|
|
|
# shellcheck disable=SC2034
|
|
|
|
while IFS=$'|' read -r LOGIN PASS
|
|
|
|
do
|
|
|
|
DOMAIN=$(echo "${LOGIN}" | cut -d @ -f2)
|
|
|
|
echo "${DOMAIN}" >>/tmp/vhost.tmp
|
|
|
|
done < <(grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-accounts.cf || true)
|
|
|
|
fi
|
2016-04-20 21:01:32 +00:00
|
|
|
|
2021-01-27 13:09:24 +00:00
|
|
|
# Getting domains FROM mail aliases
|
|
|
|
if [[ -f /tmp/docker-mailserver/postfix-virtual.cf ]]
|
|
|
|
then
|
|
|
|
# shellcheck disable=SC2034
|
|
|
|
while read -r FROM TO
|
|
|
|
do
|
|
|
|
UNAME=$(echo "${FROM}" | cut -d @ -f1)
|
|
|
|
DOMAIN=$(echo "${FROM}" | cut -d @ -f2)
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2021-01-27 13:09:24 +00:00
|
|
|
test "${UNAME}" != "${DOMAIN}" && echo "${DOMAIN}" >>/tmp/vhost.tmp
|
|
|
|
done < <(grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-virtual.cf || true)
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
tr ',' '\n' <<<"${DOMAINS}" > /tmp/vhost.tmp
|
2016-08-24 08:06:59 +00:00
|
|
|
fi
|
2016-04-20 21:01:32 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
# keeping unique entries
|
|
|
|
if [[ -f /tmp/vhost.tmp ]]
|
|
|
|
then
|
|
|
|
sort < /tmp/vhost.tmp | uniq >/tmp/vhost && rm /tmp/vhost.tmp
|
2016-04-20 21:01:32 +00:00
|
|
|
fi
|
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
# exit if no entries found
|
|
|
|
if [[ ! -f /tmp/vhost ]]
|
|
|
|
then
|
|
|
|
echo "No entries found, no keys to make"
|
|
|
|
exit 0
|
2016-08-24 08:06:59 +00:00
|
|
|
fi
|
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
while read -r DOMAINNAME
|
|
|
|
do
|
|
|
|
mkdir -p "/tmp/docker-mailserver/opendkim/keys/${DOMAINNAME}"
|
|
|
|
|
|
|
|
if [[ ! -f "/tmp/docker-mailserver/opendkim/keys/${DOMAINNAME}/mail.private" ]]
|
|
|
|
then
|
|
|
|
echo "Creating DKIM private key /tmp/docker-mailserver/opendkim/keys/${DOMAINNAME}/mail.private"
|
2016-04-20 21:01:32 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
opendkim-genkey --bits="${KEYSIZE}" --subdomains --DOMAIN="${DOMAINNAME}" --selector=mail -D "/tmp/docker-mailserver/opendkim/keys/${DOMAINNAME}"
|
2016-08-28 19:08:37 +00:00
|
|
|
fi
|
2016-04-20 21:01:32 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
# write to KeyTable if necessary
|
|
|
|
KEYTABLEENTRY="mail._domainkey.${DOMAINNAME} ${DOMAINNAME}:mail:/etc/opendkim/keys/${DOMAINNAME}/mail.private"
|
|
|
|
if [[ ! -f "/tmp/docker-mailserver/opendkim/KeyTable" ]]
|
|
|
|
then
|
2016-08-28 19:08:37 +00:00
|
|
|
echo "Creating DKIM KeyTable"
|
2020-10-21 16:16:32 +00:00
|
|
|
echo "${KEYTABLEENTRY}" > /tmp/docker-mailserver/opendkim/KeyTable
|
2016-08-28 19:08:37 +00:00
|
|
|
else
|
2020-10-21 16:16:32 +00:00
|
|
|
if ! grep -q "${KEYTABLEENTRY}" "/tmp/docker-mailserver/opendkim/KeyTable"
|
|
|
|
then
|
|
|
|
echo "${KEYTABLEENTRY}" >>/tmp/docker-mailserver/opendkim/KeyTable
|
2016-08-28 19:08:37 +00:00
|
|
|
fi
|
|
|
|
fi
|
2016-04-20 21:01:32 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
# write to SigningTable if necessary
|
|
|
|
SIGNINGTABLEENTRY="*@${DOMAINNAME} mail._domainkey.${DOMAINNAME}"
|
|
|
|
if [[ ! -f /tmp/docker-mailserver/opendkim/SigningTable ]]
|
|
|
|
then
|
2016-08-28 19:08:37 +00:00
|
|
|
echo "Creating DKIM SigningTable"
|
2020-10-21 16:16:32 +00:00
|
|
|
echo "*@${DOMAINNAME} mail._domainkey.${DOMAINNAME}" >/tmp/docker-mailserver/opendkim/SigningTable
|
2016-08-28 19:08:37 +00:00
|
|
|
else
|
2020-10-21 16:16:32 +00:00
|
|
|
if ! grep -q "${SIGNINGTABLEENTRY}" /tmp/docker-mailserver/opendkim/SigningTable
|
|
|
|
then
|
|
|
|
echo "${SIGNINGTABLEENTRY}" >> /tmp/docker-mailserver/opendkim/SigningTable
|
2016-08-28 19:08:37 +00:00
|
|
|
fi
|
|
|
|
fi
|
2020-10-21 16:16:32 +00:00
|
|
|
done < <(grep -vE '^(\s*$|#)' /tmp/vhost)
|
2016-04-20 21:01:32 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
# creates TrustedHosts if missing
|
|
|
|
if [[ -d /tmp/docker-mailserver/opendkim ]] && [[ ! -f /tmp/docker-mailserver/opendkim/TrustedHosts ]]
|
|
|
|
then
|
|
|
|
echo "Creating DKIM TrustedHosts"
|
|
|
|
echo "127.0.0.1" >/tmp/docker-mailserver/opendkim/TrustedHosts
|
|
|
|
echo "localhost" >>/tmp/docker-mailserver/opendkim/TrustedHosts
|
2016-04-20 23:08:14 +00:00
|
|
|
fi
|