forked from MarvAmBass/docker-versatile-postfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.sh
executable file
·162 lines (137 loc) · 3.89 KB
/
startup.sh
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
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
function print_help {
cat <<EOF
Generic Postfix Setup Script
===============================================
to create a new postfix server for your domain
you should use the following commands:
docker run -p 25:25 -v /maildirs:/var/mail \
dockerimage/postfix \
yourdomain.com \
user1:password \
user2:password \
userN:password
this creates a new smtp server which listens
on port 25, stores mail under /mailsdirs
and has serveral user accounts like
user1 with password "password" and a mail
address [email protected]
________________________________________________
by MarvAmBass
EOF
}
if [ "-h" == "$1" ] || [ "--help" == "$1" ] || [ -z $1 ] || [ "" == "$1" ]
then
print_help
exit 0
fi
if [ ! -f /etc/default/saslauthd ]
then
>&2 echo ">> you're not inside a valid docker container"
exit 1;
fi
echo ">> setting up postfix for: $1"
# add domain
postconf -e myhostname="$1"
postconf -e mydestination="$1"
echo "$1" > /etc/mailname
echo "Domain $1" >> /etc/opendkim.conf
if [ ${#@} -gt 1 ]
then
echo ">> adding users..."
# all arguments but skip first argumenti
i=0
for ARG in "$@"
do
if [ $i -gt 0 ] && [ "$ARG" != "${ARG/://}" ]
then
USER=`echo "$ARG" | cut -d":" -f1`
echo " >> adding user: $USER"
useradd -s /bin/bash $USER
echo "$ARG" | chpasswd
if [ ! -d /var/spool/mail/$USER ]
then
mkdir /var/spool/mail/$USER
fi
chown -R $USER:mail /var/spool/mail/$USER
chmod -R a=rwx /var/spool/mail/$USER
chmod -R o=- /var/spool/mail/$USER
fi
i=`expr $i + 1`
done
fi
# DKIM
if [ -z ${DISABLE_DKIM+x} ]
then
echo ">> enable DKIM support"
if [ -z ${DKIM_CANONICALIZATION+x} ]
then
DKIM_CANONICALIZATION="simple"
fi
echo "Canonicalization $DKIM_CANONICALIZATION" >> /etc/opendkim.conf
postconf -e milter_default_action="accept"
postconf -e milter_protocol="2"
postconf -e smtpd_milters="inet:localhost:8891"
postconf -e non_smtpd_milters="inet:localhost:8891"
# add dkim if necessary
if [ ! -f /etc/postfix/dkim/dkim.key ]
then
echo ">> no dkim.key found - generate one..."
opendkim-genkey -s $DKIM_SELECTOR -d $1
mv $DKIM_SELECTOR.private /etc/postfix/dkim/dkim.key
echo ">> printing out public dkim key:"
cat $DKIM_SELECTOR.txt
mv $DKIM_SELECTOR.txt /etc/postfix/dkim/dkim.public
echo ">> please at this key to your DNS System"
fi
echo ">> change user and group of /etc/postfix/dkim/dkim.key to opendkim"
chown -R opendkim:opendkim /etc/postfix/dkim/
chmod -R o-rwX /etc/postfix/dkim/
chmod o=- /etc/postfix/dkim/dkim.key
fi
# Configure /etc/opendkim/custom.conf file
cat <<EOF > /etc/opendkim/custom.conf
KeyFile /etc/postfix/dkim/dkim.key
Selector $DKIM_SELECTOR
SOCKET inet:8891@localhost
EOF
# add aliases
> /etc/aliases
if [ ! -z ${ALIASES+x} ]
then
IFS=';' read -ra ADDR <<< "$ALIASES"
for i in "${ADDR[@]}"; do
echo "$i" >> /etc/aliases
echo ">> adding $i to /etc/aliases"
done
fi
echo ">> the new /etc/aliases file:"
cat /etc/aliases
newaliases
##
# POSTFIX RAW Config ENVs
##
if env | grep '^POSTFIX_RAW_CONFIG_'
then
echo -e "\n## POSTFIX_RAW_CONFIG ##\n" >> /etc/postfix/main.cf
env | grep '^POSTFIX_RAW_CONFIG_' | while read I_CONF
do
CONFD_CONF_NAME=$(echo "$I_CONF" | cut -d'=' -f1 | sed 's/POSTFIX_RAW_CONFIG_//g' | tr '[:upper:]' '[:lower:]')
CONFD_CONF_VALUE=$(echo "$I_CONF" | sed 's/^[^=]*=//g')
echo "${CONFD_CONF_NAME} = ${CONFD_CONF_VALUE}" >> /etc/postfix/main.cf
done
fi
# starting services
echo ">> starting the services"
service rsyslog start
if [ -z ${DISABLE_DKIM+x} ]
then
service opendkim start
fi
service saslauthd start
service postfix start
# print logs
echo ">> printing the logs"
touch /var/log/mail.log /var/log/mail.err /var/log/mail.warn
chmod a+rw /var/log/mail.*
tail -F /var/log/mail.*