blob: 5ea1609dfee4ade0663df0f75d01bf7605cf3a7f [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001#!/bin/bash
2set -e
3
4# Wait for MySQL to warm-up
5while ! mysqladmin status --socket=/var/run/mysqld/mysqld.sock -u${DBUSER} -p${DBPASS} --silent; do
6 echo "Waiting for database to come up..."
7 sleep 2
8done
9
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020010until dig +short mailcow.email > /dev/null; do
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010011 echo "Waiting for DNS..."
12 sleep 1
13done
14
15# Do not attempt to write to slave
16if [[ ! -z ${REDIS_SLAVEOF_IP} ]]; then
17 REDIS_CMDLINE="redis-cli -h ${REDIS_SLAVEOF_IP} -p ${REDIS_SLAVEOF_PORT}"
18else
19 REDIS_CMDLINE="redis-cli -h redis -p 6379"
20fi
21
22until [[ $(${REDIS_CMDLINE} PING) == "PONG" ]]; do
23 echo "Waiting for Redis..."
24 sleep 2
25done
26
27${REDIS_CMDLINE} SET DOVECOT_REPL_HEALTH 1 > /dev/null
28
29# Create missing directories
30[[ ! -d /etc/dovecot/sql/ ]] && mkdir -p /etc/dovecot/sql/
31[[ ! -d /etc/dovecot/lua/ ]] && mkdir -p /etc/dovecot/lua/
32[[ ! -d /var/vmail/_garbage ]] && mkdir -p /var/vmail/_garbage
33[[ ! -d /var/vmail/sieve ]] && mkdir -p /var/vmail/sieve
34[[ ! -d /etc/sogo ]] && mkdir -p /etc/sogo
35[[ ! -d /var/volatile ]] && mkdir -p /var/volatile
36
37# Set Dovecot sql config parameters, escape " in db password
38DBPASS=$(echo ${DBPASS} | sed 's/"/\\"/g')
39
40# Create quota dict for Dovecot
41if [[ "${MASTER}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
42 QUOTA_TABLE=quota2
43else
44 QUOTA_TABLE=quota2replica
45fi
46cat <<EOF > /etc/dovecot/sql/dovecot-dict-sql-quota.conf
47# Autogenerated by mailcow
48connect = "host=/var/run/mysqld/mysqld.sock dbname=${DBNAME} user=${DBUSER} password=${DBPASS}"
49map {
50 pattern = priv/quota/storage
51 table = ${QUOTA_TABLE}
52 username_field = username
53 value_field = bytes
54}
55map {
56 pattern = priv/quota/messages
57 table = ${QUOTA_TABLE}
58 username_field = username
59 value_field = messages
60}
61EOF
62
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010063# Create dict used for sieve pre and postfilters
64cat <<EOF > /etc/dovecot/sql/dovecot-dict-sql-sieve_before.conf
65# Autogenerated by mailcow
66connect = "host=/var/run/mysqld/mysqld.sock dbname=${DBNAME} user=${DBUSER} password=${DBPASS}"
67map {
68 pattern = priv/sieve/name/\$script_name
69 table = sieve_before
70 username_field = username
71 value_field = id
72 fields {
73 script_name = \$script_name
74 }
75}
76map {
77 pattern = priv/sieve/data/\$id
78 table = sieve_before
79 username_field = username
80 value_field = script_data
81 fields {
82 id = \$id
83 }
84}
85EOF
86
87cat <<EOF > /etc/dovecot/sql/dovecot-dict-sql-sieve_after.conf
88# Autogenerated by mailcow
89connect = "host=/var/run/mysqld/mysqld.sock dbname=${DBNAME} user=${DBUSER} password=${DBPASS}"
90map {
91 pattern = priv/sieve/name/\$script_name
92 table = sieve_after
93 username_field = username
94 value_field = id
95 fields {
96 script_name = \$script_name
97 }
98}
99map {
100 pattern = priv/sieve/data/\$id
101 table = sieve_after
102 username_field = username
103 value_field = script_data
104 fields {
105 id = \$id
106 }
107}
108EOF
109
110echo -n ${ACL_ANYONE} > /etc/dovecot/acl_anyone
111
112if [[ "${SKIP_SOLR}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200113echo -n 'quota acl zlib mail_crypt mail_crypt_acl mail_log notify listescape replication' > /etc/dovecot/mail_plugins
114echo -n 'quota imap_quota imap_acl acl zlib imap_zlib imap_sieve mail_crypt mail_crypt_acl notify listescape replication mail_log' > /etc/dovecot/mail_plugins_imap
115echo -n 'quota sieve acl zlib mail_crypt mail_crypt_acl notify listescape replication' > /etc/dovecot/mail_plugins_lmtp
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100116else
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200117echo -n 'quota acl zlib mail_crypt mail_crypt_acl mail_log notify fts fts_solr listescape replication' > /etc/dovecot/mail_plugins
118echo -n 'quota imap_quota imap_acl acl zlib imap_zlib imap_sieve mail_crypt mail_crypt_acl notify mail_log fts fts_solr listescape replication' > /etc/dovecot/mail_plugins_imap
119echo -n 'quota sieve acl zlib mail_crypt mail_crypt_acl fts fts_solr notify listescape replication' > /etc/dovecot/mail_plugins_lmtp
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100120fi
121chmod 644 /etc/dovecot/mail_plugins /etc/dovecot/mail_plugins_imap /etc/dovecot/mail_plugins_lmtp /templates/quarantine.tpl
122
123cat <<EOF > /etc/dovecot/sql/dovecot-dict-sql-userdb.conf
124# Autogenerated by mailcow
125driver = mysql
126connect = "host=/var/run/mysqld/mysqld.sock dbname=${DBNAME} user=${DBUSER} password=${DBPASS}"
127user_query = SELECT CONCAT(JSON_UNQUOTE(JSON_VALUE(attributes, '$.mailbox_format')), mailbox_path_prefix, '%d/%n/${MAILDIR_SUB}:VOLATILEDIR=/var/volatile/%u:INDEX=/var/vmail_index/%u') AS mail, '%s' AS protocol, 5000 AS uid, 5000 AS gid, concat('*:bytes=', quota) AS quota_rule FROM mailbox WHERE username = '%u' AND (active = '1' OR active = '2')
128iterate_query = SELECT username FROM mailbox WHERE active = '1' OR active = '2';
129EOF
130
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200131cat <<EOF > /etc/dovecot/lua/passwd-verify.lua
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100132function auth_password_verify(req, pass)
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200133
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100134 if req.domain == nil then
135 return dovecot.auth.PASSDB_RESULT_USER_UNKNOWN, "No such user"
136 end
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200137
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100138 if cur == nil then
139 script_init()
140 end
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200141
142 if req.user == nil then
143 req.user = ''
144 end
145
146 respbody = {}
147
148 -- check against mailbox passwds
149 local cur,errorString = con:execute(string.format([[SELECT password FROM mailbox
150 WHERE username = '%s'
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100151 AND active = '1'
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200152 AND domain IN (SELECT domain FROM domain WHERE domain='%s' AND active='1')
153 AND IFNULL(JSON_UNQUOTE(JSON_VALUE(mailbox.attributes, '$.force_pw_update')), 0) != '1'
154 AND IFNULL(JSON_UNQUOTE(JSON_VALUE(attributes, '$.%s_access')), 1) = '1']], con:escape(req.user), con:escape(req.domain), con:escape(req.service)))
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100155 local row = cur:fetch ({}, "a")
156 while row do
157 if req.password_verify(req, row.password, pass) == 1 then
158 cur:close()
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200159 con:execute(string.format([[REPLACE INTO sasl_log (service, app_password, username, real_rip)
160 VALUES ("%s", 0, "%s", "%s")]], con:escape(req.service), con:escape(req.user), con:escape(req.real_rip)))
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100161 return dovecot.auth.PASSDB_RESULT_OK, "password=" .. pass
162 end
163 row = cur:fetch (row, "a")
164 end
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200165
166 -- check against app passwds
167 local cur,errorString = con:execute(string.format([[SELECT app_passwd.id, app_passwd.password FROM app_passwd
168 INNER JOIN mailbox ON mailbox.username = app_passwd.mailbox
169 WHERE mailbox = '%s'
170 AND IFNULL(JSON_UNQUOTE(JSON_VALUE(mailbox.attributes, '$.%s_access')), 1) = '1'
171 AND IFNULL(JSON_UNQUOTE(JSON_VALUE(mailbox.attributes, '$.force_pw_update')), 0) != '1'
172 AND app_passwd.active = '1'
173 AND mailbox.active = '1'
174 AND app_passwd.domain IN (SELECT domain FROM domain WHERE domain='%s' AND active='1')]], con:escape(req.user), con:escape(req.service), con:escape(req.domain)))
175 local row = cur:fetch ({}, "a")
176 while row do
177 if req.password_verify(req, row.password, pass) == 1 then
178 cur:close()
179 con:execute(string.format([[REPLACE INTO sasl_log (service, app_password, username, real_rip)
180 VALUES ("%s", %d, "%s", "%s")]], con:escape(req.service), row.id, con:escape(req.user), con:escape(req.real_rip)))
181 return dovecot.auth.PASSDB_RESULT_OK, "password=" .. pass
182 end
183 row = cur:fetch (row, "a")
184 end
185
186 return dovecot.auth.PASSDB_RESULT_PASSWORD_MISMATCH, "Failed to authenticate"
187
188 -- PoC
189 -- local reqbody = string.format([[{
190 -- "success":0,
191 -- "service":"%s",
192 -- "app_password":false,
193 -- "username":"%s",
194 -- "real_rip":"%s"
195 -- }]], con:escape(req.service), con:escape(req.user), con:escape(req.real_rip))
196 -- http.request {
197 -- method = "POST",
198 -- url = "http://nginx:8081/sasl_log.php",
199 -- source = ltn12.source.string(reqbody),
200 -- headers = {
201 -- ["content-type"] = "application/json",
202 -- ["content-length"] = tostring(#reqbody)
203 -- },
204 -- sink = ltn12.sink.table(respbody)
205 -- }
206
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100207end
208
209function auth_passdb_lookup(req)
210 return dovecot.auth.PASSDB_RESULT_USER_UNKNOWN, ""
211end
212
213function script_init()
214 mysql = require "luasql.mysql"
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200215 http = require "socket.http"
216 http.TIMEOUT = 5
217 ltn12 = require "ltn12"
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100218 env = mysql.mysql()
219 con = env:connect("__DBNAME__","__DBUSER__","__DBPASS__","localhost")
220 return 0
221end
222
223function script_deinit()
224 con:close()
225 env:close()
226end
227EOF
228
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200229# Replace patterns in app-passdb.lua
230sed -i "s/__DBUSER__/${DBUSER}/g" /etc/dovecot/lua/passwd-verify.lua
231sed -i "s/__DBPASS__/${DBPASS}/g" /etc/dovecot/lua/passwd-verify.lua
232sed -i "s/__DBNAME__/${DBNAME}/g" /etc/dovecot/lua/passwd-verify.lua
233
234
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100235# Migrate old sieve_after file
236[[ -f /etc/dovecot/sieve_after ]] && mv /etc/dovecot/sieve_after /etc/dovecot/global_sieve_after
237# Create global sieve scripts
238cat /etc/dovecot/global_sieve_after > /var/vmail/sieve/global_sieve_after.sieve
239cat /etc/dovecot/global_sieve_before > /var/vmail/sieve/global_sieve_before.sieve
240
241# Check permissions of vmail/index/garbage directories.
242# Do not do this every start-up, it may take a very long time. So we use a stat check here.
243if [[ $(stat -c %U /var/vmail/) != "vmail" ]] ; then chown -R vmail:vmail /var/vmail ; fi
244if [[ $(stat -c %U /var/vmail/_garbage) != "vmail" ]] ; then chown -R vmail:vmail /var/vmail/_garbage ; fi
245if [[ $(stat -c %U /var/vmail_index) != "vmail" ]] ; then chown -R vmail:vmail /var/vmail_index ; fi
246
247# Cleanup random user maildirs
248rm -rf /var/vmail/mailcow.local/*
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200249# Cleanup PIDs
250[[ -f /tmp/quarantine_notify.pid ]] && rm /tmp/quarantine_notify.pid
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100251
252# create sni configuration
253echo "" > /etc/dovecot/sni.conf
254for cert_dir in /etc/ssl/mail/*/ ; do
255 if [[ ! -f ${cert_dir}domains ]] || [[ ! -f ${cert_dir}cert.pem ]] || [[ ! -f ${cert_dir}key.pem ]]; then
256 continue
257 fi
258 domains=($(cat ${cert_dir}domains))
259 for domain in ${domains[@]}; do
260 echo 'local_name '${domain}' {' >> /etc/dovecot/sni.conf;
261 echo ' ssl_cert = <'${cert_dir}'cert.pem' >> /etc/dovecot/sni.conf;
262 echo ' ssl_key = <'${cert_dir}'key.pem' >> /etc/dovecot/sni.conf;
263 echo '}' >> /etc/dovecot/sni.conf;
264 done
265done
266
267# Create random master for SOGo sieve features
268RAND_USER=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 16 | head -n 1)
269RAND_PASS=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 24 | head -n 1)
270
271if [[ ! -z ${DOVECOT_MASTER_USER} ]] && [[ ! -z ${DOVECOT_MASTER_PASS} ]]; then
272 RAND_USER=${DOVECOT_MASTER_USER}
273 RAND_PASS=${DOVECOT_MASTER_PASS}
274fi
275echo ${RAND_USER}@mailcow.local:{SHA1}$(echo -n ${RAND_PASS} | sha1sum | awk '{print $1}'):::::: > /etc/dovecot/dovecot-master.passwd
276echo ${RAND_USER}@mailcow.local::5000:5000:::: > /etc/dovecot/dovecot-master.userdb
277echo ${RAND_USER}@mailcow.local:${RAND_PASS} > /etc/sogo/sieve.creds
278
279if [[ -z ${MAILDIR_SUB} ]]; then
280 MAILDIR_SUB_SHARED=
281else
282 MAILDIR_SUB_SHARED=/${MAILDIR_SUB}
283fi
284cat <<EOF > /etc/dovecot/shared_namespace.conf
285# Autogenerated by mailcow
286namespace {
287 type = shared
288 separator = /
289 prefix = Shared/%%u/
290 location = maildir:%%h${MAILDIR_SUB_SHARED}:INDEX=~${MAILDIR_SUB_SHARED}/Shared/%%u
291 subscriptions = no
292 list = children
293}
294EOF
295
296cat <<EOF > /etc/dovecot/sogo_trusted_ip.conf
297# Autogenerated by mailcow
298remote ${IPV4_NETWORK}.248 {
299 disable_plaintext_auth = no
300}
301EOF
302
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200303# Create random master Password for SOGo SSO
304RAND_PASS=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
305echo -n ${RAND_PASS} > /etc/phpfpm/sogo-sso.pass
306cat <<EOF > /etc/dovecot/sogo-sso.conf
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100307# Autogenerated by mailcow
308passdb {
309 driver = static
310 args = allow_real_nets=${IPV4_NETWORK}.248/32 password={plain}${RAND_PASS}
311}
312EOF
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100313
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100314if [[ "${MASTER}" =~ ^([nN][oO]|[nN])+$ ]]; then
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200315 # Toggling MASTER will result in a rebuild of containers, so the quota script will be recreated
316 cat <<'EOF' > /usr/local/bin/quota_notify.py
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100317#!/usr/bin/python3
318import sys
319sys.exit()
320EOF
321fi
322
323# 401 is user dovecot
324if [[ ! -s /mail_crypt/ecprivkey.pem || ! -s /mail_crypt/ecpubkey.pem ]]; then
325 openssl ecparam -name prime256v1 -genkey | openssl pkey -out /mail_crypt/ecprivkey.pem
326 openssl pkey -in /mail_crypt/ecprivkey.pem -pubout -out /mail_crypt/ecpubkey.pem
327 chown 401 /mail_crypt/ecprivkey.pem /mail_crypt/ecpubkey.pem
328else
329 chown 401 /mail_crypt/ecprivkey.pem /mail_crypt/ecpubkey.pem
330fi
331
332# Compile sieve scripts
333sievec /var/vmail/sieve/global_sieve_before.sieve
334sievec /var/vmail/sieve/global_sieve_after.sieve
335sievec /usr/lib/dovecot/sieve/report-spam.sieve
336sievec /usr/lib/dovecot/sieve/report-ham.sieve
337
338# Fix permissions
339chown root:root /etc/dovecot/sql/*.conf
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200340chown root:dovecot /etc/dovecot/sql/dovecot-dict-sql-sieve* /etc/dovecot/sql/dovecot-dict-sql-quota* /etc/dovecot/lua/passwd-verify.lua
341chmod 640 /etc/dovecot/sql/*.conf /etc/dovecot/lua/passwd-verify.lua
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100342chown -R vmail:vmail /var/vmail/sieve
343chown -R vmail:vmail /var/volatile
344chown -R vmail:vmail /var/vmail_index
345adduser vmail tty
346chmod g+rw /dev/console
347chown root:tty /dev/console
348chmod +x /usr/lib/dovecot/sieve/rspamd-pipe-ham \
349 /usr/lib/dovecot/sieve/rspamd-pipe-spam \
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200350 /usr/local/bin/imapsync_runner.pl \
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100351 /usr/local/bin/imapsync \
352 /usr/local/bin/trim_logs.sh \
353 /usr/local/bin/sa-rules.sh \
354 /usr/local/bin/clean_q_aged.sh \
355 /usr/local/bin/maildir_gc.sh \
356 /usr/local/sbin/stop-supervisor.sh \
357 /usr/local/bin/quota_notify.py \
358 /usr/local/bin/repl_health.sh
359
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100360# Prepare environment file for cronjobs
361printenv | sed 's/^\(.*\)$/export \1/g' > /source_env.sh
362
363# Clean old PID if any
364[[ -f /var/run/dovecot/master.pid ]] && rm /var/run/dovecot/master.pid
365
366# Clean stopped imapsync jobs
367rm -f /tmp/imapsync_busy.lock
368IMAPSYNC_TABLE=$(mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "SHOW TABLES LIKE 'imapsync'" -Bs)
369[[ ! -z ${IMAPSYNC_TABLE} ]] && mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "UPDATE imapsync SET is_running='0'"
370
371# Envsubst maildir_gc
372echo "$(envsubst < /usr/local/bin/maildir_gc.sh)" > /usr/local/bin/maildir_gc.sh
373
374# GUID generation
375while [[ ${VERSIONS_OK} != 'OK' ]]; do
376 if [[ ! -z $(mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -B -e "SELECT 'OK' FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = \"${DBNAME}\" AND TABLE_NAME = 'versions'") ]]; then
377 VERSIONS_OK=OK
378 else
379 echo "Waiting for versions table to be created..."
380 sleep 3
381 fi
382done
383PUBKEY_MCRYPT=$(doveconf -P 2> /dev/null | grep -i mail_crypt_global_public_key | cut -d '<' -f2)
384if [ -f ${PUBKEY_MCRYPT} ]; then
385 GUID=$(cat <(echo ${MAILCOW_HOSTNAME}) /mail_crypt/ecpubkey.pem | sha256sum | cut -d ' ' -f1 | tr -cd "[a-fA-F0-9.:/] ")
386 if [ ${#GUID} -eq 64 ]; then
387 mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} << EOF
388REPLACE INTO versions (application, version) VALUES ("GUID", "${GUID}");
389EOF
390 else
391 mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} << EOF
392REPLACE INTO versions (application, version) VALUES ("GUID", "INVALID");
393EOF
394 fi
395fi
396
397# Collect SA rules once now
398/usr/local/bin/sa-rules.sh
399
400# Run hooks
401for file in /hooks/*; do
402 if [ -x "${file}" ]; then
403 echo "Running hook ${file}"
404 "${file}"
405 fi
406done
407
408# For some strange, unknown and stupid reason, Dovecot may run into a race condition, when this file is not touched before it is read by dovecot/auth
409# May be related to something inside Docker, I seriously don't know
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200410touch /etc/dovecot/lua/passwd-verify.lua
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100411
412exec "$@"