-
Notifications
You must be signed in to change notification settings - Fork 50
/
entrypoint
150 lines (119 loc) · 4.82 KB
/
entrypoint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
set -e
DEBUG_ADDRESS="debug@${MAILNAME}"
DEBUG_PASS="debug"
MAIL_LOCATION='/var/mail'
POSTMASTER_ADDRESS="postmaster@${MAILNAME}"
HOSTNAME="$(hostname -f)"
_add_line() {
local check="$1"
local line="$2"
local file="$3"
if ! grep -q "$check" "$file"; then
echo "$line" >> "$file"
fi
}
_log() {
local logfile=/var/log/mail.log
if [ ! -f $logfile ]; then
touch $logfile
chown syslog:adm $logfile
chmod 640 $logfile
fi
echo "$(date '+%b %e %H:%I:%S') $(hostname) entrypoint[$$]: $1" >> $logfile
}
_stop() {
/etc/init.d/postfix stop
/etc/init.d/rsyslog stop
if [ -f /var/run/dovecot/master.pid ]; then
kill -15 $(cat /var/run/dovecot/master.pid)
rm -rf /var/run/dovecot/master.pid
fi
_log "Docker entrypoint stop"
}
_account_add() {
local login=$1
local user=$(echo ${login} | cut -d @ -f1)
local domain=$(echo ${login} | cut -d @ -f2)
local pass=$(doveadm pw -s SHA512-CRYPT -u $login -p $2)
local uid=$(cat /etc/passwd | grep $MAIL_FS_USER | cut -d':' -f3)
local gid=$(cat /etc/passwd | grep $MAIL_FS_USER | cut -d':' -f4)
if grep -q "${domain}/${user}" /etc/postfix/vmailbox; then
_log "User '$login' already configured"
return 0
fi
echo "${login}:${pass}:${uid}:${gid}::${MAIL_FS_HOME}/${domain}/${user}::" >> /etc/dovecot/userdb
if [ ! -d "${MAIL_LOCATION}/${domain}" ]; then
mkdir -p "${MAIL_LOCATION}/${domain}"
chown ${uid}:${gid} "${MAIL_LOCATION}/${domain}"
fi
if [ ! -d "${MAIL_LOCATION}/${domain}/${user}" ]; then
maildirmake.dovecot "${MAIL_LOCATION}/${domain}/${user}"
maildirmake.dovecot "${MAIL_LOCATION}/${domain}/${user}/.Sent"
maildirmake.dovecot "${MAIL_LOCATION}/${domain}/${user}/.Trash"
maildirmake.dovecot "${MAIL_LOCATION}/${domain}/${user}/.Drafts"
echo -e "INBOX\nSent\nTrash\nDrafts" >> "${MAIL_LOCATION}/${domain}/${user}/subscriptions"
touch "${MAIL_LOCATION}/${domain}/${user}/.Sent/maildirfolder"
chown -R ${uid}:${gid} "${MAIL_LOCATION}/${domain}/${user}"
fi
if ! grep -q "$domain" /etc/postfix/vhost; then
echo "$domain" >> /etc/postfix/vhost
fi
echo "${login} ${domain}/${user}/" >> /etc/postfix/vmailbox
/usr/sbin/postmap /etc/postfix/vmailbox
}
trap "_stop" SIGINT SIGTERM SIGHUP
_log "Docker entrypoint start"
# Configuring Postfix
if [ -n "$MAILNAME" ]; then
echo $MAILNAME > /etc/mailname;
/usr/sbin/postconf -e mydomain=$MAILNAME
if ! grep -q "$MAILNAME" /etc/postfix/vhost; then
echo "$MAILNAME" >> /etc/postfix/vhost
fi
/usr/sbin/postconf -e smtp_helo_name=\$myhostname.\$mydomain
fi
if [ -n "$HOSTNAME" ]; then
/usr/sbin/postconf -e myhostname=$HOSTNAME
fi
routes=$(ip route | grep -v default | cut -d' ' -f1)
if [ -n "$routes" ]; then
/usr/sbin/postconf -e mynetworks="127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 $routes"
/usr/sbin/postconf -e inet_interfaces=all
fi
/usr/sbin/postconf -e relayhost=localhost
/usr/sbin/postconf -e mydestination=localhost
/usr/sbin/postconf -e virtual_maps='hash:/etc/postfix/virtual, regexp:/etc/postfix/virtual_regexp'
/usr/sbin/postconf -e sender_canonical_maps=regexp:/etc/postfix/sender_canonical_regexp
/usr/sbin/postconf -e virtual_transport=lmtp:unix:private/dovecot-lmtp
/usr/sbin/postconf -e mailbox_transport=lmtp:unix:private/dovecot-lmtp
/usr/sbin/postconf -e virtual_mailbox_domains=/etc/postfix/vhost
/usr/sbin/postconf -e virtual_mailbox_maps=hash:/etc/postfix/vmailbox
/usr/sbin/postconf compatibility_level=2
# Create user account
if [ -n "$MAIL_ADDRESS" ]; then
_account_add "$MAIL_ADDRESS" "$MAIL_PASS"
_add_line "$MAIL_ADDRESS" "/$MAIL_ADDRESS/ $MAIL_ADDRESS" /etc/postfix/virtual_regexp
fi
# Create debug account
_account_add "$DEBUG_ADDRESS" "$DEBUG_PASS"
# Force to send all emails to debug email address, no email to outside
_add_line "$DEBUG_ADDRESS" "/.+@.+/ $DEBUG_ADDRESS" /etc/postfix/virtual_regexp
/usr/sbin/postmap /etc/postfix/virtual_regexp
/usr/sbin/postmap /etc/postfix/virtual
/usr/sbin/postmap /etc/postfix/vmailbox
/usr/sbin/postmap /etc/postfix/sender_canonical_regexp
# Configuring Dovecot
cp -a /usr/share/dovecot/protocols.d /etc/dovecot/
sed -i -e 's/include_try \/usr\/share\/dovecot\/protocols\.d/include_try \/etc\/dovecot\/protocols\.d/g' /etc/dovecot/dovecot.conf
sed -i -e 's/^.*postmaster_address.*/postmaster_address = '${POSTMASTER_ADDRESS}'/g' /etc/dovecot/conf.d/15-lda.conf
sed -i -e 's/^.*postmaster_address.*/ postmaster_address = '${POSTMASTER_ADDRESS}'/g' /etc/dovecot/conf.d/20-lmtp.conf
_log "Docker entrypoint configured"
# Start syslog daemon
/etc/init.d/rsyslog start
# Start Postfix daemon
/etc/init.d/postfix start
# Start Dovecot daemon
rm -rf /var/run/dovecot/master.pid
/usr/sbin/dovecot -c /etc/dovecot/dovecot.conf
$@ & wait ${!}