Skip to content

Commit

Permalink
Added a way to generate and configure a specific SSL certificate for …
Browse files Browse the repository at this point in the history
  • Loading branch information
tomav committed Aug 18, 2015
1 parent 63a7be0 commit a848a55
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
docker-compose.yml
postfix/ssl/*
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ RUN freshclam
ADD postfix/main.cf /etc/postfix/main.cf
ADD postfix/master.cf /etc/postfix/master.cf
ADD postfix/sasl/smtpd.conf /etc/postfix/sasl/smtpd.conf
ADD bin/generate-ssl-certificate /usr/local/bin/generate-ssl-certificate
RUN chmod +x /usr/local/bin/generate-ssl-certificate

# Start-mailserver script
ADD start-mailserver.sh /usr/local/bin/start-mailserver.sh
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ Volumes allow to:

docker-compose up -d mail

# configure ssl

## generate ssl certificate

You can easily generate en SSL certificate by using the following command:

docker run -ti --rm -v "$(pwd)"/postfix/ssl:/ssl -h mail.my-domain.com -t tvial/docker-mailserver generate-ssl-certificate

# will generate:
# postfix/ssl/mail.my-domain.com.key
# postfix/ssl/mail.my-domain.com.csr

Note that the certificate will be generate for the container `fqdn`, that is passed as `-h` argument.

## configure ssl certificate (convention over configuration)

If a matching certificate (with `.key` and `.csr` files) is found in `postfix/ssl`, it will be automatically configured in postfix. You just have to place `mail.my-domain.com.key` and `mail.my-domain.com.csr` for domain `mail.my-domain.com` in `postfix/ssl` folder.

# client configuration

# imap
Expand Down
4 changes: 4 additions & 0 deletions bin/generate-ssl-certificate
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

FQDN=$(hostname)
openssl req -new -newkey rsa:2048 -nodes -keyout /ssl/$FQDN.key -out /ssl/$FQDN.csr
6 changes: 2 additions & 4 deletions docker-compose.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ mail:
- "143:143"
- "587:587"
- "993:993"
environment:
docker_mail_domain: "my-domain.com"
volumes:
- ./spamassassin:/tmp/spamassassin/:ro
- ./postfix:/tmp/postfix/:ro
- ./spamassassin:/tmp/spamassassin/
- ./postfix:/tmp/postfix/
10 changes: 10 additions & 0 deletions start-mailserver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

echo "Regenerating postfix 'vmailbox' and 'virtual' for given users"
echo "# WARNING: this file is auto-generated. Modify accounts.cf in postfix directory on host" > /etc/postfix/vmailbox

# Checking that /tmp/postfix/accounts.cf ends with a newline
sed -i -e '$a\' /tmp/postfix/accounts.cf

# Creating users
while IFS=$'|' read login pass
do
Expand All @@ -30,6 +32,14 @@ postmap /etc/postfix/virtual
sed -i -r 's/DOCKER_MAIL_DOMAIN/'"$(hostname -d)"'/g' /etc/postfix/main.cf
cat /tmp/vhost.tmp | sort | uniq >> /etc/postfix/vhost && rm /tmp/vhost.tmp

# Adding SSL certificate if name provided as $docker_mail_cert env
if [ -e "/tmp/postfix/ssl/$(hostname).csr" ]; then
echo "Adding $(hostname) csr/key SSL certificate"
cp -r /tmp/postfix/ssl /etc/postfix/ssl
sed -i -r 's/smtpd_tls_cert_file=\/etc\/ssl\/certs\/ssl-cert-snakeoil.pem/smtpd_tls_cert_file=\/etc\/postfix\/ssl\/'$docker_mail_cert'.csr/g' /etc/postfix/main.cf
sed -i -r 's/smtpd_tls_key_file=\/etc\/ssl\/private\/ssl-cert-snakeoil.key/smtpd_tls_key_file=\/etc\/postfix\/ssl\/'$docker_mail_cert'.key/g' /etc/postfix/main.cf
fi

echo "Fixing permissions"
chown -R 5000:5000 /var/mail
mkdir -p /var/log/clamav && chown -R clamav:root /var/log/clamav
Expand Down

0 comments on commit a848a55

Please sign in to comment.