Skip to content

Commit

Permalink
Version upgrade and other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dogukancagatay committed Oct 20, 2019
1 parent e8b07a7 commit d9dabf1
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 53 deletions.
4 changes: 0 additions & 4 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
.git
.gitignore
.gitattributes
README.md
emailrelay*.tar.gz
emailrelay-*/
2 changes: 0 additions & 2 deletions .gitattributes

This file was deleted.

2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

59 changes: 34 additions & 25 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
FROM alpine:latest
LABEL maintainer="Aleksei Zhukov <[email protected]>"

RUN echo "@testing http://nl.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories \
&& apk add --no-cache libstdc++ openssl ca-certificates dumb-init swaks@testing perl-net-ssleay \
&& update-ca-certificates

ARG DOWNLOAD_URL=https://downloads.sourceforge.net/project/emailrelay/emailrelay/1.9/emailrelay-1.9-src.tar.gz

RUN apk add --no-cache --virtual .deps curl g++ make autoconf automake openssl-dev \
&& mkdir -p /tmp/build && cd /tmp/build \
&& curl -o emailrelay.tar.gz -L "${DOWNLOAD_URL}" \
&& tar xzf emailrelay.tar.gz \
&& cd emailrelay-1.9 \
&& ./configure --prefix=/usr --with-openssl \
&& make \
&& make install \
&& apk --no-cache del .deps \
&& cd / \
&& rm -rf /tmp/build /var/tmp/* /var/cache/apk/* /var/cache/distfiles/* \
&& mkdir -p /var/spool/emailrelay

ENV PORT=587
FROM alpine:3.10 as builder

ARG DOWNLOAD_URL=https://downloads.sourceforge.net/project/emailrelay/emailrelay/2.1/emailrelay-2.1a-src.tar.gz

RUN apk add --no-cache curl g++ make autoconf automake openssl-dev \
&& mkdir -p /tmp/build && cd /tmp/build \
&& curl -o emailrelay.tar.gz -L "${DOWNLOAD_URL}" \
&& tar xzf emailrelay.tar.gz \
&& cd emailrelay-* \
&& ./configure --prefix=/app --with-openssl \
&& make -j $(nproc --all) \
&& make install

FROM alpine:3.10
MAINTAINER "Dogukan Cagatay <[email protected]>"

ENV PORT="25" \
SWAKS_OPTS="" \
DEFAULT_OPTS="--no-daemon --no-syslog --log --log-time --remote-clients" \
SPOOL_DIR="/var/spool/emailrelay"

RUN apk add --update --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing \
libstdc++ \
openssl \
ca-certificates \
dumb-init \
swaks \
bash \
perl-net-ssleay \
&& rm -rf /var/tmp/* /var/cache/apk/* /var/cache/distfiles/* \
&& mkdir -p "${SPOOL_DIR}"

COPY run.sh /run.sh
COPY --from=builder /app /app

ENTRYPOINT ["/usr/bin/dumb-init", "--", "/run.sh"]
CMD []

ENV SWAKS_OPTS="-tls"
HEALTHCHECK --interval=2m --timeout=5s \
CMD swaks -S -h localhost -s localhost:${PORT:-587} -q HELO ${SWAKS_OPTS} || exit 1
CMD swaks -S -h localhost -s localhost:${PORT} -q HELO ${SWAKS_OPTS} || exit 1
83 changes: 68 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,75 @@
Docker image for E-MailRelay
============================
# Docker image for E-MailRelay
![MicroBadger Size](https://img.shields.io/microbadger/image-size/dcagatay/emailrelay)
![MicroBadger Layers](https://img.shields.io/microbadger/layers/dcagatay/emailrelay)
![Docker Pulls](https://img.shields.io/docker/pulls/dcagatay/emailrelay)
![Docker Stars](https://img.shields.io/docker/stars/dcagatay/emailrelay)

Usage example:
Available Tags:
- [2.1](https://github.com/dogukancagatay/docker-emailrelay/blob/2.1/Dockerfile), [latest](https://github.com/dogukancagatay/docker-emailrelay/blob/master/Dockerfile)

docker run --name emailrelay \
-v /etc/ssl/private/key_and_cert.pem:/etc/ssl/server.pem:ro \
drdaeman/emailrelay \
-D msa.example.com --immediate --forward-to mail.example.org:smtp \
--server-tls /etc/ssl/server.pem --client-tls
Alpine based Docker image for E-MailRelay. You can read capabilities, configuration etc. of E-MailRelay on its [website](http://emailrelay.sourceforge.net).

To get help, check emailrelay documentation or use:
Container configuration is done via *environment variables* and *command line arguments*. Command line arguments are given directly to ``emailrelay`` executable.

docker run --rm drdaeman/emailrelay --help
To see all command line options of ``emailrelay`` command:

Note, the `--remote-clients --port $PORT` (and some more) options are
passed automatically, so to change client port just redefine the `PORT`
environment variable. The default port number is 587.
```bash
docker run --rm dcagatay/emailrelay --help --verbose
```

For a full control, run a full command starting with `emailrelay`, i.e.:
## Usage
Some usage examples are given in ``docker-compose.yml``.

docker run drdaeman/emailrelay emailrelay --help
#### Example Usage with for Gmail SMTP Service
Sample configuration for sending emails from your Gmail account.

Add your credentials to ``client-auth.txt``.

```
client plain [email protected] gmail-or-app-password
```

Run the docker container
```bash
docker run --rm \
-p "25:25" \
-v "$PWD/client-auth.txt:/client-auth.txt" \
dcagatay/emailrelay --forward-on-disconnect --forward-to smtp.gmail.com:587 --client-tls --client-auth=/client-auth.txt
```

## Environment Variables

#### ``DEFAULT_OPTS``
By default the following arguments are given on runtime. You can overwrite ``DEFAULT_OPTS`` environment variable to change or disable this behaviour.
```
--no-daemon --no-syslog --log --log-time --remote-clients
```

#### ``PORT``
The port that E-MailRelay runs on. Default value is ``25``. If you did TLS configuration you need to set this variable to ``587`` or something else.

#### ``SPOOL_DIR``
Spool directory for E-MailRelay. No need to change. Default value: ``/var/spool/emailrelay``

#### ``SWAKS_OPTS``
This variable is used to give options to _swaks_, it is used on built-in health-check functionality. If you serve with TLS configuration you need to set this variable to ``-tls``. Default value: _empty-string_

## Filter Scripts, Client/Server Authentication, and Others
Inside ``config`` directory you will find sample files for usage with filter functionality, SMTP client authentication and relay server authentication.

For any further configuration or details, refer to the [E-MailRelay documentation](http://emailrelay.sourceforge.net).

## Testing
You can test your configuration with _swaks_.
```bash
docker run --rm \
flowman/swaks \
echo "This is a test message." | swaks --to [email protected] --from [email protected] --server localhost --port 25
```

## Additions to ``drdaeman/docker-emailrelay``
- E-MailRelay version upgrade.
- Multi stage build for quicker builds.
- ``bash`` shell in included for further scripting.
- Default TLS configuration is changed to insecure configuration.
- Sample files for advanced configuration.
9 changes: 9 additions & 0 deletions config/client-auth.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# emailrelay secrets file
#
# Format: client plain user password
#
# Plain password should be in xtext encoded format (defined in RFC-1891) (space should be as +20, + as +2B, ## as +23, = as +3D)
# http://emailrelay.sourceforge.net/index.html#reference_md_Authentication
#
#client plain user password
24 changes: 24 additions & 0 deletions config/filter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

#############################################################################
CONTENT_FILE="${1}"
ENVELOPE_FILE="${2}"

FROM="`awk '/X-MailRelay-From:/ {print $2;exit}' \"${ENVELOPE_FILE}\"`"
TO="`awk '/X-MailRelay-To-Remote:/ {print $2;exit}' \"${ENVELOPE_FILE}\"`"
CLIENT_IP="`awk '/MailRelay-Client:/ {print $2;exit}' \"${ENVELOPE_FILE}\"`"
#############################################################################


BLOCKED_FROM_EMAIL="[email protected] [email protected] [email protected]"

for BLOCKED_EMAIL in $BLOCKED_FROM_EMAIL
do
if [[ $FROM =~ $BLOCKED_EMAIL ]]; then
echo "<<You cannot send e-mail from address: $BLOCKED_EMAIL >>"
exit 1 # <= cancel further processing by emailrelay (send error code)
# exit 100 # <= cancel further processing by emailrelay (forget email, silent rejection)
fi
done

exit 0 # <= continue processing
13 changes: 13 additions & 0 deletions config/server-auth.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# emailrelay secrets file
#
# Format: server plain user password
# server none 192.168.1.* localipv4
# server none 192.168.1.0/24 localipv4
#
# Plain password should be in xtext encoded format (defined in RFC-1891) (space should be as +20, + as +2B, ## as +23, = as +3D)
# http://emailrelay.sourceforge.net/index.html#reference_md_Authentication
#
#server none 127.0.0.1 localipv4
#server none 192.168.1.* localipv4
#server plain user password
43 changes: 43 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: "3"
services:

# emailrelay:
# build: ./
# image: dcagatay/emailrelay:latest
# command: --domain msa.example.com --forward-on-disconnect --forward-to mail.example.org:25
# ports:
# - "25:25"
# restart: unless-stopped

emailrelay-gmail:
build: ./
image: dcagatay/emailrelay:latest
command: --forward-on-disconnect --forward-to smtp.gmail.com:587 --client-tls --client-auth=/config/client-auth.txt
volumes:
- ./config:/config:ro
ports:
- "25:25"
restart: unless-stopped

# emailrelay-tls:
# build: ./
# image: dcagatay/emailrelay:latest
# command: --domain msa.example.com --forward-on-disconnect --forward-to mail.example.org:587 --client-tls --server-tls /etc/ssl/server.pem
# volumes:
# - ./config/key_and_cert.pem:/etc/ssl/server.pem:ro
# environment:
# PORT: "587"
# SWAKS_OPTS: "-tls"
# ports:
# - "587:587"
# restart: unless-stopped

# emailrelay-server-client-auth:
# build: ./
# image: dcagatay/emailrelay:latest
# command: --domain msa.example.com --filter /config/filter.sh --forward-on-disconnect --forward-to mail.example.org:587 --client-tls --client-auth=/config/client-auth.txt --server-auth=/config/server-auth.txt
# volumes:
# - ./config:/config:ro
# ports:
# - "25:25"
# restart: unless-stopped
12 changes: 7 additions & 5 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
#!/bin/sh
set -e

# Some (hopefully) sensible defaults. Users don't have to specify those.
DEFAULT_OPTS=${DEFAULT_OPTS:---no-daemon --no-syslog --log --remote-clients --port "${PORT:-587}"}
export PATH="/app/sbin:${PATH}"

# Update default options with specified port and spool dir
DEFAULT_OPTS="$DEFAULT_OPTS --port ${PORT} --spool-dir ${SPOOL_DIR}"

# Running this image without any command-line argument doesn't make sense
if [ -z "$*" ]; then
echo "FATAL: Please provide some command-line arguments to emailrelay"
echo "E.g., -D msa.example.com --immediate --forward-to mail.example.org:smtp"
echo "E.g., --domain msa.example.com --forward-on-disconnect --forward-to mail.example.org:smtp"
echo "Note, we already pass ${DEFAULT_OPTS}"
exit 2
fi

case "$*" in
-h\ *|--help\ *|-V|--version)
# A request for help or version are special cases.
exec /usr/sbin/emailrelay $@ ;;
exec /app/sbin/emailrelay $@ ;;
-*)
# If the arguments (CMD, assuming we're an ENTRYPOINT) starts
# with a minus-dash, it's the options to emailrelay.
# Prefill some defaults (so we don't have to specify them),
# then run.
exec /usr/sbin/emailrelay $DEFAULT_OPTS $@ ;;
exec /app/sbin/emailrelay $DEFAULT_OPTS $@ ;;
*)
# If arguments don't start with a minus-dash, let's assume
# it's a proper shell command to execute and do so.
Expand Down

0 comments on commit d9dabf1

Please sign in to comment.