docker-entrypoint.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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}"
  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/auth/ ]] && mkdir -p /etc/dovecot/auth/
  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 [[ "${FLATCURVE_EXPERIMENTAL}" =~ ^([yY][eE][sS]|[yY]) ]]; then
  102. echo -e "\e[33mActivating Flatcurve as FTS Backend...\e[0m"
  103. echo -e "\e[33mDepending on your previous setup a full reindex might be needed... \e[0m"
  104. echo -e "\e[34mVisit https://docs.mailcow.email/manual-guides/Dovecot/u_e-dovecot-fts/#fts-related-dovecot-commands to learn how to reindex\e[0m"
  105. echo -n 'quota acl zlib mail_crypt mail_crypt_acl mail_log notify fts fts_flatcurve 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_flatcurve listescape replication' > /etc/dovecot/mail_plugins_imap
  107. echo -n 'quota sieve acl zlib mail_crypt mail_crypt_acl fts fts_flatcurve notify listescape replication' > /etc/dovecot/mail_plugins_lmtp
  108. elif [[ "${SKIP_SOLR}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  109. echo -n 'quota acl zlib mail_crypt mail_crypt_acl mail_log notify listescape replication' > /etc/dovecot/mail_plugins
  110. 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
  111. echo -n 'quota sieve acl zlib mail_crypt mail_crypt_acl notify listescape replication' > /etc/dovecot/mail_plugins_lmtp
  112. else
  113. echo -n 'quota acl zlib mail_crypt mail_crypt_acl mail_log notify fts fts_solr listescape replication' > /etc/dovecot/mail_plugins
  114. 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
  115. echo -n 'quota sieve acl zlib mail_crypt mail_crypt_acl fts fts_solr notify listescape replication' > /etc/dovecot/mail_plugins_lmtp
  116. fi
  117. chmod 644 /etc/dovecot/mail_plugins /etc/dovecot/mail_plugins_imap /etc/dovecot/mail_plugins_lmtp /templates/quarantine.tpl
  118. cat <<EOF > /etc/dovecot/sql/dovecot-dict-sql-userdb.conf
  119. # Autogenerated by mailcow
  120. driver = mysql
  121. connect = "host=/var/run/mysqld/mysqld.sock dbname=${DBNAME} user=${DBUSER} password=${DBPASS}"
  122. 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')
  123. iterate_query = SELECT username FROM mailbox WHERE active = '1' OR active = '2';
  124. EOF
  125. # Temporarily set FTS depending on user choice inside mailcow.conf. Will be removed as soon as Solr is dropped
  126. if [[ "${FLATCURVE_EXPERIMENTAL}" =~ ^([yY][eE][sS]|[yY])$ ]]; then
  127. cat <<EOF > /etc/dovecot/conf.d/fts.conf
  128. # Autogenerated by mailcow
  129. plugin {
  130. fts_autoindex = yes
  131. fts_autoindex_exclude = \Junk
  132. fts_autoindex_exclude2 = \Trash
  133. fts = flatcurve
  134. # Maximum term length can be set via the 'maxlen' argument (maxlen is
  135. # specified in bytes, not number of UTF-8 characters)
  136. fts_tokenizer_email_address = maxlen=100
  137. fts_tokenizer_generic = algorithm=simple maxlen=30
  138. # These are not flatcurve settings, but required for Dovecot FTS. See
  139. # Dovecot FTS Configuration link above for further information.
  140. fts_languages = en es de
  141. fts_tokenizers = generic email-address
  142. # OPTIONAL: Recommended default FTS core configuration
  143. fts_filters = normalizer-icu snowball stopwords
  144. fts_filters_en = lowercase snowball english-possessive stopwords
  145. }
  146. EOF
  147. elif [[ ! "${SKIP_SOLR}" =~ ^([yY][eE][sS]|[yY])$ ]]; then
  148. cat <<EOF > /etc/dovecot/conf.d/fts.conf
  149. # Autogenerated by mailcow
  150. plugin {
  151. fts = solr
  152. fts_autoindex = yes
  153. fts_autoindex_exclude = \Junk
  154. fts_autoindex_exclude2 = \Trash
  155. fts_solr = url=http://solr:8983/solr/dovecot-fts/
  156. fts_tokenizers = generic email-address
  157. fts_tokenizer_generic = algorithm=simple
  158. fts_filters = normalizer-icu snowball stopwords
  159. fts_filters_en = lowercase snowball english-possessive stopwords
  160. }
  161. EOF
  162. fi
  163. # Migrate old sieve_after file
  164. [[ -f /etc/dovecot/sieve_after ]] && mv /etc/dovecot/sieve_after /etc/dovecot/global_sieve_after
  165. # Create global sieve scripts
  166. cat /etc/dovecot/global_sieve_after > /var/vmail/sieve/global_sieve_after.sieve
  167. cat /etc/dovecot/global_sieve_before > /var/vmail/sieve/global_sieve_before.sieve
  168. # Check permissions of vmail/index/garbage directories.
  169. # Do not do this every start-up, it may take a very long time. So we use a stat check here.
  170. if [[ $(stat -c %U /var/vmail/) != "vmail" ]] ; then chown -R vmail:vmail /var/vmail ; fi
  171. if [[ $(stat -c %U /var/vmail/_garbage) != "vmail" ]] ; then chown -R vmail:vmail /var/vmail/_garbage ; fi
  172. if [[ $(stat -c %U /var/vmail_index) != "vmail" ]] ; then chown -R vmail:vmail /var/vmail_index ; fi
  173. # Cleanup random user maildirs
  174. rm -rf /var/vmail/mailcow.local/*
  175. # Cleanup PIDs
  176. [[ -f /tmp/quarantine_notify.pid ]] && rm /tmp/quarantine_notify.pid
  177. # create sni configuration
  178. echo "" > /etc/dovecot/sni.conf
  179. for cert_dir in /etc/ssl/mail/*/ ; do
  180. if [[ ! -f ${cert_dir}domains ]] || [[ ! -f ${cert_dir}cert.pem ]] || [[ ! -f ${cert_dir}key.pem ]]; then
  181. continue
  182. fi
  183. domains=($(cat ${cert_dir}domains))
  184. for domain in ${domains[@]}; do
  185. echo 'local_name '${domain}' {' >> /etc/dovecot/sni.conf;
  186. echo ' ssl_cert = <'${cert_dir}'cert.pem' >> /etc/dovecot/sni.conf;
  187. echo ' ssl_key = <'${cert_dir}'key.pem' >> /etc/dovecot/sni.conf;
  188. echo '}' >> /etc/dovecot/sni.conf;
  189. done
  190. done
  191. # Create random master for SOGo sieve features
  192. RAND_USER=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 16 | head -n 1)
  193. RAND_PASS=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 24 | head -n 1)
  194. if [[ ! -z ${DOVECOT_MASTER_USER} ]] && [[ ! -z ${DOVECOT_MASTER_PASS} ]]; then
  195. RAND_USER=${DOVECOT_MASTER_USER}
  196. RAND_PASS=${DOVECOT_MASTER_PASS}
  197. fi
  198. echo ${RAND_USER}@mailcow.local:{SHA1}$(echo -n ${RAND_PASS} | sha1sum | awk '{print $1}'):::::: > /etc/dovecot/dovecot-master.passwd
  199. echo ${RAND_USER}@mailcow.local::5000:5000:::: > /etc/dovecot/dovecot-master.userdb
  200. echo ${RAND_USER}@mailcow.local:${RAND_PASS} > /etc/sogo/sieve.creds
  201. if [[ -z ${MAILDIR_SUB} ]]; then
  202. MAILDIR_SUB_SHARED=
  203. else
  204. MAILDIR_SUB_SHARED=/${MAILDIR_SUB}
  205. fi
  206. cat <<EOF > /etc/dovecot/shared_namespace.conf
  207. # Autogenerated by mailcow
  208. namespace {
  209. type = shared
  210. separator = /
  211. prefix = Shared/%%u/
  212. location = maildir:%%h${MAILDIR_SUB_SHARED}:INDEX=~${MAILDIR_SUB_SHARED}/Shared/%%u
  213. subscriptions = no
  214. list = children
  215. }
  216. EOF
  217. cat <<EOF > /etc/dovecot/sogo_trusted_ip.conf
  218. # Autogenerated by mailcow
  219. remote ${IPV4_NETWORK}.248 {
  220. disable_plaintext_auth = no
  221. }
  222. EOF
  223. # Create random master Password for SOGo SSO
  224. RAND_PASS=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
  225. echo -n ${RAND_PASS} > /etc/phpfpm/sogo-sso.pass
  226. if [[ "${MASTER}" =~ ^([nN][oO]|[nN])+$ ]]; then
  227. # Toggling MASTER will result in a rebuild of containers, so the quota script will be recreated
  228. cat <<'EOF' > /usr/local/bin/quota_notify.py
  229. #!/usr/bin/python3
  230. import sys
  231. sys.exit()
  232. EOF
  233. fi
  234. # Set mail_replica for HA setups
  235. if [[ -n ${MAILCOW_REPLICA_IP} && -n ${DOVEADM_REPLICA_PORT} ]]; then
  236. cat <<EOF > /etc/dovecot/mail_replica.conf
  237. # Autogenerated by mailcow
  238. mail_replica = tcp:${MAILCOW_REPLICA_IP}:${DOVEADM_REPLICA_PORT}
  239. EOF
  240. fi
  241. # 401 is user dovecot
  242. if [[ ! -s /mail_crypt/ecprivkey.pem || ! -s /mail_crypt/ecpubkey.pem ]]; then
  243. openssl ecparam -name prime256v1 -genkey | openssl pkey -out /mail_crypt/ecprivkey.pem
  244. openssl pkey -in /mail_crypt/ecprivkey.pem -pubout -out /mail_crypt/ecpubkey.pem
  245. chown 401 /mail_crypt/ecprivkey.pem /mail_crypt/ecpubkey.pem
  246. else
  247. chown 401 /mail_crypt/ecprivkey.pem /mail_crypt/ecpubkey.pem
  248. fi
  249. # Compile sieve scripts
  250. sievec /var/vmail/sieve/global_sieve_before.sieve
  251. sievec /var/vmail/sieve/global_sieve_after.sieve
  252. sievec /usr/lib/dovecot/sieve/report-spam.sieve
  253. sievec /usr/lib/dovecot/sieve/report-ham.sieve
  254. # Fix permissions
  255. chown root:root /etc/dovecot/sql/*.conf
  256. chown root:dovecot /etc/dovecot/sql/dovecot-dict-sql-sieve* /etc/dovecot/sql/dovecot-dict-sql-quota* /etc/dovecot/auth/passwd-verify.lua
  257. chmod 640 /etc/dovecot/sql/*.conf /etc/dovecot/auth/passwd-verify.lua
  258. chown -R vmail:vmail /var/vmail/sieve
  259. chown -R vmail:vmail /var/volatile
  260. chown -R vmail:vmail /var/vmail_index
  261. adduser vmail tty
  262. chmod g+rw /dev/console
  263. chown root:tty /dev/console
  264. chmod +x /usr/lib/dovecot/sieve/rspamd-pipe-ham \
  265. /usr/lib/dovecot/sieve/rspamd-pipe-spam \
  266. /usr/local/bin/imapsync_runner.pl \
  267. /usr/local/bin/imapsync \
  268. /usr/local/bin/trim_logs.sh \
  269. /usr/local/bin/sa-rules.sh \
  270. /usr/local/bin/clean_q_aged.sh \
  271. /usr/local/bin/maildir_gc.sh \
  272. /usr/local/sbin/stop-supervisor.sh \
  273. /usr/local/bin/quota_notify.py \
  274. /usr/local/bin/repl_health.sh \
  275. /usr/local/bin/optimize-fts.sh
  276. # Prepare environment file for cronjobs
  277. printenv | sed 's/^\(.*\)$/export \1/g' > /source_env.sh
  278. # Clean old PID if any
  279. [[ -f /var/run/dovecot/master.pid ]] && rm /var/run/dovecot/master.pid
  280. # Clean stopped imapsync jobs
  281. rm -f /tmp/imapsync_busy.lock
  282. IMAPSYNC_TABLE=$(mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "SHOW TABLES LIKE 'imapsync'" -Bs)
  283. [[ ! -z ${IMAPSYNC_TABLE} ]] && mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "UPDATE imapsync SET is_running='0'"
  284. # Envsubst maildir_gc
  285. echo "$(envsubst < /usr/local/bin/maildir_gc.sh)" > /usr/local/bin/maildir_gc.sh
  286. # GUID generation
  287. while [[ ${VERSIONS_OK} != 'OK' ]]; do
  288. 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
  289. VERSIONS_OK=OK
  290. else
  291. echo "Waiting for versions table to be created..."
  292. sleep 3
  293. fi
  294. done
  295. PUBKEY_MCRYPT=$(doveconf -P 2> /dev/null | grep -i mail_crypt_global_public_key | cut -d '<' -f2)
  296. if [ -f ${PUBKEY_MCRYPT} ]; then
  297. GUID=$(cat <(echo ${MAILCOW_HOSTNAME}) /mail_crypt/ecpubkey.pem | sha256sum | cut -d ' ' -f1 | tr -cd "[a-fA-F0-9.:/] ")
  298. if [ ${#GUID} -eq 64 ]; then
  299. mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} << EOF
  300. REPLACE INTO versions (application, version) VALUES ("GUID", "${GUID}");
  301. EOF
  302. else
  303. mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} << EOF
  304. REPLACE INTO versions (application, version) VALUES ("GUID", "INVALID");
  305. EOF
  306. fi
  307. fi
  308. # Collect SA rules once now
  309. /usr/local/bin/sa-rules.sh
  310. # Run hooks
  311. for file in /hooks/*; do
  312. if [ -x "${file}" ]; then
  313. echo "Running hook ${file}"
  314. "${file}"
  315. fi
  316. done
  317. # 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
  318. # May be related to something inside Docker, I seriously don't know
  319. touch /etc/dovecot/auth/passwd-verify.lua
  320. if [[ ! -z ${REDIS_SLAVEOF_IP} ]]; then
  321. cp /etc/syslog-ng/syslog-ng-redis_slave.conf /etc/syslog-ng/syslog-ng.conf
  322. fi
  323. exec "$@"