2016-04-20 21:01:32 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
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.
|
|
|
|
keysize=${1:-2048}
|
|
|
|
|
2016-04-20 21:01:32 +00:00
|
|
|
# Getting domains from mail accounts
|
2016-08-24 08:06:59 +00:00
|
|
|
if [ -f /tmp/docker-mailserver/postfix-accounts.cf ]; then
|
2016-08-28 19:08:37 +00:00
|
|
|
while IFS=$'|' read login pass
|
|
|
|
do
|
|
|
|
domain=$(echo ${login} | cut -d @ -f2)
|
|
|
|
echo ${domain} >> /tmp/vhost.tmp
|
|
|
|
done < /tmp/docker-mailserver/postfix-accounts.cf
|
2016-08-24 08:06:59 +00:00
|
|
|
fi
|
2016-04-20 21:01:32 +00:00
|
|
|
|
|
|
|
# Getting domains from mail aliases
|
2016-08-24 08:06:59 +00:00
|
|
|
if [ -f /tmp/docker-mailserver/postfix-virtual.cf ]; then
|
2016-08-28 19:08:37 +00:00
|
|
|
while read from to
|
|
|
|
do
|
|
|
|
# Setting variables for better readability
|
|
|
|
uname=$(echo ${from} | cut -d @ -f1)
|
|
|
|
domain=$(echo ${from} | cut -d @ -f2)
|
|
|
|
# if they are equal it means the line looks like: "user1 other@domain.tld"
|
|
|
|
test "$uname" != "$domain" && echo ${domain} >> /tmp/vhost.tmp
|
|
|
|
done < /tmp/docker-mailserver/postfix-virtual.cf
|
2016-08-24 08:06:59 +00:00
|
|
|
fi
|
2016-04-20 21:01:32 +00:00
|
|
|
|
|
|
|
# Keeping unique entries
|
2016-04-20 23:08:14 +00:00
|
|
|
if [ -f /tmp/vhost.tmp ]; then
|
2016-08-28 19:08:37 +00:00
|
|
|
cat /tmp/vhost.tmp | sort | uniq > /tmp/vhost && rm /tmp/vhost.tmp
|
2016-04-20 21:01:32 +00:00
|
|
|
fi
|
|
|
|
|
2016-08-24 08:06:59 +00:00
|
|
|
# Exit if no entries found
|
|
|
|
if [ ! -f /tmp/vhost ]; then
|
|
|
|
echo "No entries found, no keys to make"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2016-04-20 23:08:14 +00:00
|
|
|
grep -vE '^(\s*$|#)' /tmp/vhost | while read domainname; do
|
2016-08-28 19:08:37 +00:00
|
|
|
mkdir -p /tmp/docker-mailserver/opendkim/keys/$domainname
|
2016-04-20 21:01:32 +00:00
|
|
|
|
2016-08-28 19:08:37 +00:00
|
|
|
if [ ! -f "/tmp/docker-mailserver/opendkim/keys/$domainname/mail.private" ]; then
|
|
|
|
echo "Creating DKIM private key /tmp/docker-mailserver/opendkim/keys/$domainname/mail.private"
|
2018-03-02 21:17:18 +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
|
|
|
|
2016-08-28 19:08:37 +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
|
|
|
|
echo "Creating DKIM KeyTable"
|
|
|
|
echo $keytableentry > /tmp/docker-mailserver/opendkim/KeyTable
|
|
|
|
else
|
|
|
|
if ! grep -q "$keytableentry" "/tmp/docker-mailserver/opendkim/KeyTable" ; then
|
|
|
|
echo $keytableentry >> /tmp/docker-mailserver/opendkim/KeyTable
|
|
|
|
fi
|
|
|
|
fi
|
2016-04-20 21:01:32 +00:00
|
|
|
|
2016-08-28 19:08:37 +00:00
|
|
|
# Write to SigningTable if necessary
|
|
|
|
signingtableentry="*@$domainname mail._domainkey.$domainname"
|
|
|
|
if [ ! -f "/tmp/docker-mailserver/opendkim/SigningTable" ]; then
|
|
|
|
echo "Creating DKIM SigningTable"
|
|
|
|
echo "*@$domainname mail._domainkey.$domainname" > /tmp/docker-mailserver/opendkim/SigningTable
|
|
|
|
else
|
|
|
|
if ! grep -q "$signingtableentry" "/tmp/docker-mailserver/opendkim/SigningTable" ; then
|
|
|
|
echo $signingtableentry >> /tmp/docker-mailserver/opendkim/SigningTable
|
|
|
|
fi
|
|
|
|
fi
|
2016-04-20 21:01:32 +00:00
|
|
|
done
|
|
|
|
|
2016-04-20 23:08:14 +00:00
|
|
|
# Creates TrustedHosts if missing
|
2016-08-24 08:06:59 +00:00
|
|
|
if [ -d "/tmp/docker-mailserver/opendkim" ] && [ ! -f "/tmp/docker-mailserver/opendkim/TrustedHosts" ]; then
|
2016-08-28 19:08:37 +00:00
|
|
|
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
|