docker-entrypoint.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/auth/ ]] && mkdir -p /etc/dovecot/auth/
  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. # Migrate old sieve_after file
  118. [[ -f /etc/dovecot/sieve_after ]] && mv /etc/dovecot/sieve_after /etc/dovecot/global_sieve_after
  119. # Create global sieve scripts
  120. cat /etc/dovecot/global_sieve_after > /var/vmail/sieve/global_sieve_after.sieve
  121. cat /etc/dovecot/global_sieve_before > /var/vmail/sieve/global_sieve_before.sieve
  122. # Check permissions of vmail/index/garbage directories.
  123. # Do not do this every start-up, it may take a very long time. So we use a stat check here.
  124. if [[ $(stat -c %U /var/vmail/) != "vmail" ]] ; then chown -R vmail:vmail /var/vmail ; fi
  125. if [[ $(stat -c %U /var/vmail/_garbage) != "vmail" ]] ; then chown -R vmail:vmail /var/vmail/_garbage ; fi
  126. if [[ $(stat -c %U /var/vmail_index) != "vmail" ]] ; then chown -R vmail:vmail /var/vmail_index ; fi
  127. # Cleanup random user maildirs
  128. rm -rf /var/vmail/mailcow.local/*
  129. # Cleanup PIDs
  130. [[ -f /tmp/quarantine_notify.pid ]] && rm /tmp/quarantine_notify.pid
  131. # create sni configuration
  132. echo "" > /etc/dovecot/sni.conf
  133. for cert_dir in /etc/ssl/mail/*/ ; do
  134. if [[ ! -f ${cert_dir}domains ]] || [[ ! -f ${cert_dir}cert.pem ]] || [[ ! -f ${cert_dir}key.pem ]]; then
  135. continue
  136. fi
  137. domains=($(cat ${cert_dir}domains))
  138. for domain in ${domains[@]}; do
  139. echo 'local_name '${domain}' {' >> /etc/dovecot/sni.conf;
  140. echo ' ssl_cert = <'${cert_dir}'cert.pem' >> /etc/dovecot/sni.conf;
  141. echo ' ssl_key = <'${cert_dir}'key.pem' >> /etc/dovecot/sni.conf;
  142. echo '}' >> /etc/dovecot/sni.conf;
  143. done
  144. done
  145. # Create random master for SOGo sieve features
  146. RAND_USER=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 16 | head -n 1)
  147. RAND_PASS=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 24 | head -n 1)
  148. if [[ ! -z ${DOVECOT_MASTER_USER} ]] && [[ ! -z ${DOVECOT_MASTER_PASS} ]]; then
  149. RAND_USER=${DOVECOT_MASTER_USER}
  150. RAND_PASS=${DOVECOT_MASTER_PASS}
  151. fi
  152. echo ${RAND_USER}@mailcow.local:{SHA1}$(echo -n ${RAND_PASS} | sha1sum | awk '{print $1}'):::::: > /etc/dovecot/dovecot-master.passwd
  153. echo ${RAND_USER}@mailcow.local::5000:5000:::: > /etc/dovecot/dovecot-master.userdb
  154. echo ${RAND_USER}@mailcow.local:${RAND_PASS} > /etc/sogo/sieve.creds
  155. if [[ -z ${MAILDIR_SUB} ]]; then
  156. MAILDIR_SUB_SHARED=
  157. else
  158. MAILDIR_SUB_SHARED=/${MAILDIR_SUB}
  159. fi
  160. cat <<EOF > /etc/dovecot/shared_namespace.conf
  161. # Autogenerated by mailcow
  162. namespace {
  163. type = shared
  164. separator = /
  165. prefix = Shared/%%u/
  166. location = maildir:%%h${MAILDIR_SUB_SHARED}:INDEX=~${MAILDIR_SUB_SHARED}/Shared/%%u
  167. subscriptions = no
  168. list = children
  169. }
  170. EOF
  171. cat <<EOF > /etc/dovecot/sogo_trusted_ip.conf
  172. # Autogenerated by mailcow
  173. remote ${IPV4_NETWORK}.248 {
  174. disable_plaintext_auth = no
  175. }
  176. EOF
  177. # Create random master Password for SOGo SSO
  178. RAND_PASS=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
  179. echo -n ${RAND_PASS} > /etc/phpfpm/sogo-sso.pass
  180. cat <<EOF > /etc/dovecot/sogo-sso.conf
  181. # Autogenerated by mailcow
  182. passdb {
  183. driver = static
  184. args = allow_real_nets=${IPV4_NETWORK}.248/32 password={plain}${RAND_PASS}
  185. }
  186. EOF
  187. if [[ "${MASTER}" =~ ^([nN][oO]|[nN])+$ ]]; then
  188. # Toggling MASTER will result in a rebuild of containers, so the quota script will be recreated
  189. cat <<'EOF' > /usr/local/bin/quota_notify.py
  190. #!/usr/bin/python3
  191. import sys
  192. sys.exit()
  193. EOF
  194. fi
  195. # Set mail_replica for HA setups
  196. if [[ -n ${MAILCOW_REPLICA_IP} && -n ${DOVEADM_REPLICA_PORT} ]]; then
  197. cat <<EOF > /etc/dovecot/mail_replica.conf
  198. # Autogenerated by mailcow
  199. mail_replica = tcp:${MAILCOW_REPLICA_IP}:${DOVEADM_REPLICA_PORT}
  200. EOF
  201. fi
  202. # 401 is user dovecot
  203. if [[ ! -s /mail_crypt/ecprivkey.pem || ! -s /mail_crypt/ecpubkey.pem ]]; then
  204. openssl ecparam -name prime256v1 -genkey | openssl pkey -out /mail_crypt/ecprivkey.pem
  205. openssl pkey -in /mail_crypt/ecprivkey.pem -pubout -out /mail_crypt/ecpubkey.pem
  206. chown 401 /mail_crypt/ecprivkey.pem /mail_crypt/ecpubkey.pem
  207. else
  208. chown 401 /mail_crypt/ecprivkey.pem /mail_crypt/ecpubkey.pem
  209. fi
  210. # Compile sieve scripts
  211. sievec /var/vmail/sieve/global_sieve_before.sieve
  212. sievec /var/vmail/sieve/global_sieve_after.sieve
  213. sievec /usr/lib/dovecot/sieve/report-spam.sieve
  214. sievec /usr/lib/dovecot/sieve/report-ham.sieve
  215. for file in /var/vmail/*/*/sieve/*.sieve ; do
  216. if [[ "$file" == "/var/vmail/*/*/sieve/*.sieve" ]]; then
  217. continue
  218. fi
  219. sievec "$file" "$(dirname "$file")/../.dovecot.svbin"
  220. chown vmail:vmail "$(dirname "$file")/../.dovecot.svbin"
  221. done
  222. # Fix permissions
  223. chown root:root /etc/dovecot/sql/*.conf
  224. chown root:dovecot /etc/dovecot/sql/dovecot-dict-sql-sieve* /etc/dovecot/sql/dovecot-dict-sql-quota* /etc/dovecot/auth/passwd-verify.lua
  225. chmod 640 /etc/dovecot/sql/*.conf /etc/dovecot/auth/passwd-verify.lua
  226. chown -R vmail:vmail /var/vmail/sieve
  227. chown -R vmail:vmail /var/volatile
  228. chown -R vmail:vmail /var/vmail_index
  229. adduser vmail tty
  230. chmod g+rw /dev/console
  231. chown root:tty /dev/console
  232. chmod +x /usr/lib/dovecot/sieve/rspamd-pipe-ham \
  233. /usr/lib/dovecot/sieve/rspamd-pipe-spam \
  234. /usr/local/bin/imapsync_runner.pl \
  235. /usr/local/bin/imapsync \
  236. /usr/local/bin/trim_logs.sh \
  237. /usr/local/bin/sa-rules.sh \
  238. /usr/local/bin/clean_q_aged.sh \
  239. /usr/local/bin/maildir_gc.sh \
  240. /usr/local/sbin/stop-supervisor.sh \
  241. /usr/local/bin/quota_notify.py \
  242. /usr/local/bin/repl_health.sh
  243. # Prepare environment file for cronjobs
  244. printenv | sed 's/^\(.*\)$/export \1/g' > /source_env.sh
  245. # Clean old PID if any
  246. [[ -f /var/run/dovecot/master.pid ]] && rm /var/run/dovecot/master.pid
  247. # Clean stopped imapsync jobs
  248. rm -f /tmp/imapsync_busy.lock
  249. IMAPSYNC_TABLE=$(mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "SHOW TABLES LIKE 'imapsync'" -Bs)
  250. [[ ! -z ${IMAPSYNC_TABLE} ]] && mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "UPDATE imapsync SET is_running='0'"
  251. # Envsubst maildir_gc
  252. echo "$(envsubst < /usr/local/bin/maildir_gc.sh)" > /usr/local/bin/maildir_gc.sh
  253. # GUID generation
  254. while [[ ${VERSIONS_OK} != 'OK' ]]; do
  255. 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
  256. VERSIONS_OK=OK
  257. else
  258. echo "Waiting for versions table to be created..."
  259. sleep 3
  260. fi
  261. done
  262. PUBKEY_MCRYPT=$(doveconf -P 2> /dev/null | grep -i mail_crypt_global_public_key | cut -d '<' -f2)
  263. if [ -f ${PUBKEY_MCRYPT} ]; then
  264. GUID=$(cat <(echo ${MAILCOW_HOSTNAME}) /mail_crypt/ecpubkey.pem | sha256sum | cut -d ' ' -f1 | tr -cd "[a-fA-F0-9.:/] ")
  265. if [ ${#GUID} -eq 64 ]; then
  266. mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} << EOF
  267. REPLACE INTO versions (application, version) VALUES ("GUID", "${GUID}");
  268. EOF
  269. else
  270. mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} << EOF
  271. REPLACE INTO versions (application, version) VALUES ("GUID", "INVALID");
  272. EOF
  273. fi
  274. fi
  275. # Collect SA rules once now
  276. /usr/local/bin/sa-rules.sh
  277. # Run hooks
  278. for file in /hooks/*; do
  279. if [ -x "${file}" ]; then
  280. echo "Running hook ${file}"
  281. "${file}"
  282. fi
  283. done
  284. # 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
  285. # May be related to something inside Docker, I seriously don't know
  286. touch /etc/dovecot/auth/passwd-verify.lua
  287. if [[ ! -z ${REDIS_SLAVEOF_IP} ]]; then
  288. cp /etc/syslog-ng/syslog-ng-redis_slave.conf /etc/syslog-ng/syslog-ng.conf
  289. fi
  290. exec "$@"