Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Permissons and some automations #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ RUN sed -i 's/#log_path = syslog/log_path = \/var\/log\/dovecot\/dovecot.log/' /
ADD dovecot-conf.d/20-stats.conf /etc/dovecot/conf.d/20-stats.conf

# setup entrypoint
ENV TZ=UTC
ENV CRON='*/30 * * * *'
ENV DEFAULT_PASSWD="replaceMeNow"
ADD entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
Expand Down
17 changes: 5 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ Open source project:
Usage
=====

Requirements:
Required volumes:

- `/home`: mounted users directories (`Maildir` in fs layout, `sieve`, `.getmail`)
- `/etc/cron.d`: mounted crontabs for executing all getmail accounts
- `/etc/ssl/private`: mounted SSL/TLS certificates (`dovecot.crt`, `dovecot.key`)

Environment Variables
- `CRON`: getmail will be called by cronjob to retrieve emails. Specify your schedule in cron format. Default is every 30 minutes `CRON="*/30 * * * *"` (don't forget the quotes). Keep in mind that some mail providers will block high frequent retrieving.
- `TZ`= set local timezone. Default is `TZ=UTC`

Prepare your getmailrc account configurations per user (`/srv/mail/home/user/.getmail/[email protected]`):

```
Expand Down Expand Up @@ -89,16 +92,6 @@ received = false
verbose = 1
```

Prepare crontab file (`/srv/mail/cron.d/getmail`) for periodically checking for new mail for each user and account:

```
# /etc/cron.d/getmail: system-wide crontab for getmail
SHELL=/bin/sh

# m h dom mon dow user command
*/20 * * * * user ACC="user-refilter" && (date; flock -n ~/.getmail/lock-$ACC getmail --rcfile="getmailrc-$ACC" --idle Refilter) >>"/var/log/getmail/$ACC.log" 2>&1
*/20 * * * * user ACC="[email protected]" && (date; flock -n ~/.getmail/lock-$ACC getmail --rcfile="getmailrc-$ACC" --idle INBOX) >>"/var/log/getmail/$ACC.log" 2>&1
```

Do not forget to place your SSL certificates as `/srv/mail/ssl/dovecot.crt` and `/srv/mail/ssl/dovecot.key`. SSL is required!

Expand Down
6 changes: 4 additions & 2 deletions docker-compose.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ services:
#build: http://github.com/gw0/docker-dovecot-getmail.git
image: gw000/dovecot-getmail:latest
volumes:
- /srv/mail/home:/home
- /srv/mail/cron.d:/etc/cron.d
- /srv/mail/home/:/home/
- /srv/mail/ssl:/etc/ssl/private:ro
evironment:
- CRON="*/30 * * * *"
- TZ=UTC
ports:
- "143:143" # imap
- "993:993" # imaps
Expand Down
23 changes: 19 additions & 4 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,40 @@ set -e

# initialize on first run
echo "Initializing..."
echo "Set Timezone to '$TZ' ..."
ln -sf /usr/share/zoneinfo/$TZ /etc/localtime
echo -e "... `date`"
echo "cron-schedule: $CRON"
touch /etc/cron.d/getmail
echo -e "# system-wide crontab for getmail\nSHELL=/bin/sh\n\n# m h dom mon dow user command" >> /etc/cron.d/getmail
mkdir -p /var/log/dovecot /var/log/getmail
touch /var/log/dovecot/dovecot.log
chown root:users /var/log/dovecot/dovecot.log
chmod 664 /var/log/dovecot/dovecot.log
for USER in $(ls -1 /home); do
echo "home permissions: $(stat -c '%u' /home):$(stat -c '%g' /home)"
echo "User '$USER':"
if ! id -u "$USER" >/dev/null 2>&1; then
# respect hosts permissions by cloning uid, gid and group into container
host_uid=$(stat -c '%u' /home/$USER)
host_gid=$(stat -c '%g' /home/$USER)
echo "Found uid:gid $host_uid:$host_gid"
# create user with default password
useradd --groups=users --no-create-home --shell='/bin/true' "$USER"
useradd --uid=$host_uid --gid=$host_gid --groups=users --no-create-home --shell='/bin/true' "$USER"
echo -e "$DEFAULT_PASSWD\n$DEFAULT_PASSWD\n" | passwd "$USER"
chown -R "$USER:$USER" "/home/$USER"
# create subfolders if necessary
mkdir -p /home/$USER/{Maildir,sieve}
chown -R "$host_uid:$host_gid" "/home/$USER/"
chmod 700 /home/$USER/{Maildir,sieve,.getmail} || true
fi
for RC in $(ls -1 /home/$USER/.getmail/getmailrc-*); do
echo "- $RC"
ACC=${RC##*/getmailrc-}
echo -e "${CRON:1:-1} $USER (date; flock -n ~/.getmail/lock-$ACC getmail --rcfile=\"$RC\" --idle INBOX) >> \"/var/log/getmail/$ACC.log\" 2>&1" >> /etc/cron.d/getmail
# fix log permissions
LOG="/var/log/getmail/${RC##*/getmailrc-}.log"
LOG="/var/log/getmail/$ACC.log"
touch "$LOG"
chown "$USER:$USER" "$LOG"
chown "$host_uid:$host_gid" "$LOG"
chmod 644 "$LOG"
done
done
Expand Down