-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.sh
169 lines (137 loc) · 5.41 KB
/
run.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
163
164
165
166
167
168
#!/bin/bash
# By d3fk
#set default email originator / and root user aliase
echo "root: ${LIST_ADMIN}" > /etc/email-addresses
/bin/sed -i "s/admin@example\.com/${LIST_ADMIN}/" /etc/aliases
#declare Hostname in hostname and mailname files
echo "${EMAIL_HOST}" > /etc/hostname
echo ${EMAIL_HOST} > /etc/mailname
hostname -I | awk -v hostname=${EMAIL_HOST} '{disp=$1" " hostname; print disp}' >> /etc/hosts
#Change owner:group of mailman directory
chown -R list:list /var/lib/mailman/
#Create docroot
mkdir /var/www/lists
echo "<html><h2>Welcome to ${URL_HOST}</h2></html>" > /var/www/lists/index.html
mailmancfg='/etc/mailman/mm_cfg.py'
# define the URL pattern for mailman
if [ $URL_PATTERN != "http" ]; then
echo "DEFAULT_URL_PATTERN = 'https://%s/${URL_ROOT}'" >> $mailmancfg
else
echo "DEFAULT_URL_PATTERN = 'http://%s/${URL_ROOT}'" >> $mailmancfg
fi
# enable spf check if requested
if [ $ENABLE_SPF_CHECK = "true" ]; then
echo "installing SPF tools before enabling..."
apt-get install -y spf-tools-perl
/bin/sed -i "s/#CHECK_RCPT_SPF/CHECK_RCPT_SPF/" /etc/exim4/conf.d/main/00_local_macros
echo "SPF CHECK is now enabled"
fi
# Replace default hostnames with runtime values:
/bin/sed -i "s/lists\.example\.com/${EMAIL_HOST}/" /etc/exim4/conf.d/main/00_local_macros
/bin/sed -i "s/lists\.example\.com/${EMAIL_HOST}/" /etc/exim4/conf.d/main/04_mailman_options
/bin/sed -i "s/lists\.example\.com/${EMAIL_HOST}/" /etc/exim4/update-exim4.conf.conf
/bin/sed -i "s/lists\.example\.com/${URL_HOST}/" /etc/apache2/apache2.conf
/bin/sed -i "s/lists\.example\.com/${URL_HOST}/" /etc/apache2/sites-available/mailman.conf
/bin/sed -i "s/URL_ROOT\//${URL_ROOT//\//\\/}/" /etc/apache2/sites-available/mailman.conf
/bin/sed -i "s/lists\.example\.com/${EMAIL_HOST}/" $mailmancfg
/bin/sed -i "s/DEFAULT_URL_HOST.*\=.*/DEFAULT_URL_HOST\ \=\ \'${URL_HOST}\'/" $mailmancfg
/bin/sed -i "s/DEFAULT_SERVER_LANGUAGE.*\=.*/DEFAULT_SERVER_LANGUAGE\ \=\ \'${LIST_LANGUAGE_CODE}\'/" $mailmancfg
echo -n "Setting up Mailman..."
{
dpkg-reconfigure mailman
# especialy for debian:buster
mkdir /var/run/mailman
chown list:list /var/run/mailman/
ln -s /var/lib/mailman/bin/mailmanctl /etc/init.d/mailman
}
echo -n "Initializing mailing lists..."
{
/usr/sbin/mmsitepass ${MASTER_PASSWORD}
/usr/sbin/newlist -q -l ${LIST_LANGUAGE_CODE} mailman ${LIST_ADMIN} ${MASTER_PASSWORD}
}
#update aliases
/usr/bin/newaliases
echo -n "Setting up Apache web server..."
{
a2enmod -q cgi
if [ $SSL_FROM_CONTAINER = "true" ]; then
if [ $SSL_SELFSIGNED = "true" ]; then
make-ssl-cert generate-default-snakeoil --force-overwrite
echo -n "self signed SSL certificate freshly regenerated..."
fi
a2enmod ssl
fi
a2dissite -q 000-default
a2ensite mailman.conf
# edit apache default security.conf for production
/bin/sed -i "s/ServerSignature On/ServerSignature Off/" /etc/apache2/conf-available/security.conf
/bin/sed -i "s/ServerTokens OS/ServerTokens Prod/" /etc/apache2/conf-available/security.conf
echo "Apache2 new configuration is now activated"
echo "The service apache2 will be started at the end of this container deployment"
}
echo "Setting up RSA keys for DKIM..."
{
if [ ! -f /etc/exim4/tls.d/private.pem ]; then
mkdir -p /etc/exim4/tls.d
openssl genrsa -out /etc/exim4/tls.d/private.pem 2048
openssl rsa -in /etc/exim4/tls.d/private.pem -out /etc/exim4/tls.d/public.pem -pubout
fi
}
key=$(sed -e '/^-/d' /etc/exim4/tls.d/public.pem|paste -sd '' -)
echo "setting up cert for TLS..."
{
if [ ! -f /etc/exim4/exim.key ]; then
openssl req -x509 -sha256 -days 9000 -nodes -newkey rsa:4096 -keyout /etc/exim4/exim.key -out /etc/exim4/exim.crt -subj "/O=${EMAIL_HOST}/OU=IT Department/CN=${EMAIL_HOST}"
echo "Cert for TLS now generated..."
fi
}
echo "Fixing exim4 permissions..."
{
chown -R Debian-exim:Debian-exim /etc/exim4
chown -R Debian-exim /var/log/exim4
}
#build updated exim config file
echo "Setting up Exim4..."
{
update-exim4.conf
}
echo "Fixing mailman permissons ..."
{
/usr/lib/mailman/bin/check_perms -f > /dev/null
}
echo -n "Starting up services..."
{
/etc/init.d/exim4 start
/etc/init.d/mailman start
echo " exim4 OK ... mailman OK ..."
}
echo '------------- Apache2 service is starting -------------'
echo
echo
cat << EOB
***********************************************
* *
* TO COMPLETE DKIM SETUP, COPY THE *
* FOLLOWING CODE INTO A NEW TXT RECORD *
* IN YOUR DNS SERVER: *
* *
***********************************************
EOB
echo "listsdkim._domainkey.${EMAIL_HOST} IN TXT \"v=DKIM1; k=rsa; p=$key\""
echo
echo
# defining stop actions in case of SIGTERM or SIGINT
graceful_stop() {
echo "The container was asked to terminate its processes gracefully..."
/etc/init.d/mailman stop
/etc/init.d/exim4 stop
apachectl -k stop
echo "Apache2 server is now stopped."
echo "Asking for exit with code 143 (SIGTERM)..."
exit 143
}
# trapping SIGTERM and SIGINT termination signals and trigger actions
trap 'graceful_stop' SIGTERM SIGINT
echo '------------- CONTAINER UP AND RUNNING! -------------'
# Starting apache2 in foreground & wait
apachectl -DFOREGROUND -k start & wait ${!}