docker-entrypoint.sh 15 KB

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