docker-entrypoint.sh 17 KB

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