docker-entrypoint.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. # Create missing directories
  9. [[ ! -d /etc/dovecot/sql/ ]] && mkdir -p /etc/dovecot/sql/
  10. [[ ! -d /etc/dovecot/lua/ ]] && mkdir -p /etc/dovecot/lua/
  11. [[ ! -d /var/vmail/_garbage ]] && mkdir -p /var/vmail/_garbage
  12. [[ ! -d /var/vmail/sieve ]] && mkdir -p /var/vmail/sieve
  13. [[ ! -d /etc/sogo ]] && mkdir -p /etc/sogo
  14. [[ ! -d /var/volatile ]] && mkdir -p /var/volatile
  15. # Set Dovecot sql config parameters, escape " in db password
  16. DBPASS=$(echo ${DBPASS} | sed 's/"/\\"/g')
  17. # Create quota dict for Dovecot
  18. cat <<EOF > /etc/dovecot/sql/dovecot-dict-sql-quota.conf
  19. # Autogenerated by mailcow
  20. connect = "host=/var/run/mysqld/mysqld.sock dbname=${DBNAME} user=${DBUSER} password=${DBPASS}"
  21. map {
  22. pattern = priv/quota/storage
  23. table = quota2
  24. username_field = username
  25. value_field = bytes
  26. }
  27. map {
  28. pattern = priv/quota/messages
  29. table = quota2
  30. username_field = username
  31. value_field = messages
  32. }
  33. EOF
  34. # Create dict used for sieve pre and postfilters
  35. cat <<EOF > /etc/dovecot/sql/dovecot-dict-sql-sieve_before.conf
  36. # Autogenerated by mailcow
  37. connect = "host=/var/run/mysqld/mysqld.sock dbname=${DBNAME} user=${DBUSER} password=${DBPASS}"
  38. map {
  39. pattern = priv/sieve/name/\$script_name
  40. table = sieve_before
  41. username_field = username
  42. value_field = id
  43. fields {
  44. script_name = \$script_name
  45. }
  46. }
  47. map {
  48. pattern = priv/sieve/data/\$id
  49. table = sieve_before
  50. username_field = username
  51. value_field = script_data
  52. fields {
  53. id = \$id
  54. }
  55. }
  56. EOF
  57. cat <<EOF > /etc/dovecot/sql/dovecot-dict-sql-sieve_after.conf
  58. # Autogenerated by mailcow
  59. connect = "host=/var/run/mysqld/mysqld.sock dbname=${DBNAME} user=${DBUSER} password=${DBPASS}"
  60. map {
  61. pattern = priv/sieve/name/\$script_name
  62. table = sieve_after
  63. username_field = username
  64. value_field = id
  65. fields {
  66. script_name = \$script_name
  67. }
  68. }
  69. map {
  70. pattern = priv/sieve/data/\$id
  71. table = sieve_after
  72. username_field = username
  73. value_field = script_data
  74. fields {
  75. id = \$id
  76. }
  77. }
  78. EOF
  79. echo -n ${ACL_ANYONE} > /etc/dovecot/acl_anyone
  80. if [[ "${SKIP_SOLR}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  81. echo -n 'quota acl zlib listescape mail_crypt mail_crypt_acl mail_log notify' > /etc/dovecot/mail_plugins
  82. echo -n 'quota imap_quota imap_acl acl zlib imap_zlib imap_sieve listescape mail_crypt mail_crypt_acl notify mail_log' > /etc/dovecot/mail_plugins_imap
  83. echo -n 'quota sieve acl zlib listescape mail_crypt mail_crypt_acl' > /etc/dovecot/mail_plugins_lmtp
  84. else
  85. echo -n 'quota acl zlib listescape mail_crypt mail_crypt_acl mail_log notify fts fts_solr' > /etc/dovecot/mail_plugins
  86. echo -n 'quota imap_quota imap_acl acl zlib imap_zlib imap_sieve listescape mail_crypt mail_crypt_acl notify mail_log fts fts_solr' > /etc/dovecot/mail_plugins_imap
  87. echo -n 'quota sieve acl zlib listescape mail_crypt mail_crypt_acl fts fts_solr' > /etc/dovecot/mail_plugins_lmtp
  88. fi
  89. chmod 644 /etc/dovecot/mail_plugins /etc/dovecot/mail_plugins_imap /etc/dovecot/mail_plugins_lmtp /templates/quarantine.tpl
  90. cat <<EOF > /etc/dovecot/sql/dovecot-dict-sql-userdb.conf
  91. # Autogenerated by mailcow
  92. driver = mysql
  93. connect = "host=/var/run/mysqld/mysqld.sock dbname=${DBNAME} user=${DBUSER} password=${DBPASS}"
  94. user_query = SELECT CONCAT(JSON_UNQUOTE(JSON_EXTRACT(attributes, '$.mailbox_format')), mailbox_path_prefix, '%d/%n/${MAILDIR_SUB}:VOLATILEDIR=/var/volatile/%u') AS mail, 5000 AS uid, 5000 AS gid, concat('*:bytes=', quota) AS quota_rule FROM mailbox WHERE username = '%u' AND active = '1'
  95. iterate_query = SELECT username FROM mailbox WHERE active='1';
  96. EOF
  97. # Create pass dict for Dovecot
  98. cat <<EOF > /etc/dovecot/sql/dovecot-dict-sql-passdb.conf
  99. # Autogenerated by mailcow
  100. driver = mysql
  101. connect = "host=/var/run/mysqld/mysqld.sock dbname=${DBNAME} user=${DBUSER} password=${DBPASS}"
  102. default_pass_scheme = SSHA256
  103. password_query = SELECT password FROM mailbox WHERE active = '1' AND username = '%u' AND domain IN (SELECT domain FROM domain WHERE domain='%d' AND active='1') AND JSON_EXTRACT(attributes, '$.force_pw_update') NOT LIKE '%%1%%'
  104. EOF
  105. cat <<EOF > /etc/dovecot/lua/app-passdb.lua
  106. function auth_password_verify(req, pass)
  107. if req.domain == nil then
  108. return dovecot.auth.PASSDB_RESULT_USER_UNKNOWN, "No such user"
  109. end
  110. local cur,errorString = con:execute(string.format([[SELECT mailbox, password FROM app_passwd
  111. WHERE mailbox = '%s'
  112. AND active = '1'
  113. AND domain IN (SELECT domain FROM domain WHERE domain='%s' AND active='1')]], con:escape(req.user), con:escape(req.domain)))
  114. local row = cur:fetch ({}, "a")
  115. while row do
  116. if req.password_verify(req, row.password, pass) == 1 then
  117. cur:close()
  118. return dovecot.auth.PASSDB_RESULT_OK, "password=" .. pass
  119. end
  120. row = cur:fetch (row, "a")
  121. end
  122. return dovecot.auth.PASSDB_RESULT_USER_UNKNOWN, "No such user"
  123. end
  124. function script_init()
  125. mysql = require "luasql.mysql"
  126. env = mysql.mysql()
  127. con = env:connect("__DBNAME__","__DBUSER__","__DBPASS__","mysql")
  128. return 0
  129. end
  130. function script_deinit()
  131. con:close()
  132. env:close()
  133. end
  134. EOF
  135. # Migrate old sieve_after file
  136. [[ -f /etc/dovecot/sieve_after ]] && mv /etc/dovecot/sieve_after /etc/dovecot/global_sieve_after
  137. # Create global sieve scripts
  138. cat /etc/dovecot/global_sieve_after > /var/vmail/sieve/global_sieve_after.sieve
  139. cat /etc/dovecot/global_sieve_before > /var/vmail/sieve/global_sieve_before.sieve
  140. # Check permissions of vmail/attachments directory.
  141. # Do not do this every start-up, it may take a very long time. So we use a stat check here.
  142. if [[ $(stat -c %U /var/vmail/) != "vmail" ]] ; then chown -R vmail:vmail /var/vmail ; fi
  143. if [[ $(stat -c %U /var/vmail/_garbage) != "vmail" ]] ; then chown -R vmail:vmail /var/vmail/_garbage ; fi
  144. if [[ $(stat -c %U /var/attachments) != "vmail" ]] ; then chown -R vmail:vmail /var/attachments ; fi
  145. # Cleanup random user maildirs
  146. rm -rf /var/vmail/mailcow.local/*
  147. # create sni configuration
  148. echo "" > /etc/dovecot/sni.conf
  149. for cert_dir in /etc/ssl/mail/*/ ; do
  150. if [[ ! -f ${cert_dir}domains ]] || [[ ! -f ${cert_dir}cert.pem ]] || [[ ! -f ${cert_dir}key.pem ]]; then
  151. continue
  152. fi
  153. domains=($(cat ${cert_dir}domains))
  154. for domain in ${domains[@]}; do
  155. echo 'local_name '${domain}' {' >> /etc/dovecot/sni.conf;
  156. echo ' ssl_cert = <'${cert_dir}'cert.pem' >> /etc/dovecot/sni.conf;
  157. echo ' ssl_key = <'${cert_dir}'key.pem' >> /etc/dovecot/sni.conf;
  158. echo '}' >> /etc/dovecot/sni.conf;
  159. done
  160. done
  161. # Create random master for SOGo sieve features
  162. RAND_USER=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 16 | head -n 1)
  163. RAND_PASS=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 24 | head -n 1)
  164. echo ${RAND_USER}@mailcow.local:{SHA1}$(echo -n ${RAND_PASS} | sha1sum | awk '{print $1}') > /etc/dovecot/dovecot-master.passwd
  165. echo ${RAND_USER}@mailcow.local::5000:5000:::: > /etc/dovecot/dovecot-master.userdb
  166. echo ${RAND_USER}@mailcow.local:${RAND_PASS} > /etc/sogo/sieve.creds
  167. if [[ -z ${MAILDIR_SUB} ]]; then
  168. MAILDIR_SUB_SHARED=
  169. else
  170. MAILDIR_SUB_SHARED=/${MAILDIR_SUB}
  171. fi
  172. cat <<EOF > /etc/dovecot/shared_namespace.conf
  173. # Autogenerated by mailcow
  174. namespace {
  175. type = shared
  176. separator = /
  177. prefix = Shared/%%u/
  178. location = maildir:%%h${MAILDIR_SUB_SHARED}:INDEX=~${MAILDIR_SUB_SHARED}/Shared/%%u;CONTROL=~${MAILDIR_SUB_SHARED}/Shared/%%u
  179. subscriptions = no
  180. list = children
  181. }
  182. EOF
  183. if [[ "${ALLOW_ADMIN_EMAIL_LOGIN}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  184. # Create random master Password for SOGo 'login as user' via proxy auth
  185. RAND_PASS=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
  186. echo -n ${RAND_PASS} > /etc/phpfpm/sogo-sso.pass
  187. cat <<EOF > /etc/dovecot/sogo-sso.conf
  188. # Autogenerated by mailcow
  189. passdb {
  190. driver = static
  191. args = allow_real_nets=${IPV4_NETWORK}.248/32 password={plain}${RAND_PASS}
  192. }
  193. EOF
  194. else
  195. rm -f /etc/dovecot/sogo-sso.pass
  196. rm -f /etc/dovecot/sogo-sso.conf
  197. fi
  198. # Hard-code env vars to scripts due to cron not passing them to the scripts
  199. sed -i "s/__DBUSER__/${DBUSER}/g" /usr/local/bin/imapsync_cron.pl /usr/local/bin/quarantine_notify.py /usr/local/bin/clean_q_aged.sh /etc/dovecot/lua/app-passdb.lua
  200. sed -i "s/__DBPASS__/${DBPASS}/g" /usr/local/bin/imapsync_cron.pl /usr/local/bin/quarantine_notify.py /usr/local/bin/clean_q_aged.sh /etc/dovecot/lua/app-passdb.lua
  201. sed -i "s/__DBNAME__/${DBNAME}/g" /usr/local/bin/imapsync_cron.pl /usr/local/bin/quarantine_notify.py /usr/local/bin/clean_q_aged.sh /etc/dovecot/lua/app-passdb.lua
  202. sed -i "s/__LOG_LINES__/${LOG_LINES}/g" /usr/local/bin/trim_logs.sh
  203. # 401 is user dovecot
  204. if [[ ! -s /mail_crypt/ecprivkey.pem || ! -s /mail_crypt/ecpubkey.pem ]]; then
  205. openssl ecparam -name prime256v1 -genkey | openssl pkey -out /mail_crypt/ecprivkey.pem
  206. openssl pkey -in /mail_crypt/ecprivkey.pem -pubout -out /mail_crypt/ecpubkey.pem
  207. chown 401 /mail_crypt/ecprivkey.pem /mail_crypt/ecpubkey.pem
  208. else
  209. chown 401 /mail_crypt/ecprivkey.pem /mail_crypt/ecpubkey.pem
  210. fi
  211. # Compile sieve scripts
  212. sievec /var/vmail/sieve/global_sieve_before.sieve
  213. sievec /var/vmail/sieve/global_sieve_after.sieve
  214. sievec /usr/lib/dovecot/sieve/report-spam.sieve
  215. sievec /usr/lib/dovecot/sieve/report-ham.sieve
  216. # Fix permissions
  217. chown root:root /etc/dovecot/sql/*.conf
  218. chown root:dovecot /etc/dovecot/sql/dovecot-dict-sql-sieve* /etc/dovecot/sql/dovecot-dict-sql-quota* /etc/dovecot/lua/app-passdb.lua
  219. chmod 640 /etc/dovecot/sql/*.conf /etc/dovecot/lua/app-passdb.lua
  220. chown -R vmail:vmail /var/vmail/sieve
  221. chown -R vmail:vmail /var/volatile
  222. adduser vmail tty
  223. chmod g+rw /dev/console
  224. chown root:tty /dev/console
  225. chmod +x /usr/lib/dovecot/sieve/rspamd-pipe-ham \
  226. /usr/lib/dovecot/sieve/rspamd-pipe-spam \
  227. /usr/local/bin/imapsync_cron.pl \
  228. /usr/local/bin/postlogin.sh \
  229. /usr/local/bin/imapsync \
  230. /usr/local/bin/trim_logs.sh \
  231. /usr/local/bin/sa-rules.sh \
  232. /usr/local/bin/clean_q_aged.sh \
  233. /usr/local/bin/maildir_gc.sh \
  234. /usr/local/sbin/stop-supervisor.sh \
  235. /usr/local/bin/quota_notify.py
  236. # Setup cronjobs
  237. echo '* * * * * root /usr/local/bin/imapsync_cron.pl 2>&1 | /usr/bin/logger' > /etc/cron.d/imapsync
  238. #echo '30 3 * * * vmail /usr/local/bin/doveadm quota recalc -A' > /etc/cron.d/dovecot-sync
  239. echo '* * * * * vmail /usr/local/bin/trim_logs.sh >> /dev/console 2>&1' > /etc/cron.d/trim_logs
  240. echo '25 * * * * vmail /usr/local/bin/maildir_gc.sh >> /dev/console 2>&1' > /etc/cron.d/maildir_gc
  241. echo '30 1 * * * root /usr/local/bin/sa-rules.sh >> /dev/console 2>&1' > /etc/cron.d/sa-rules
  242. echo '0 2 * * * root /usr/bin/curl http://solr:8983/solr/dovecot-fts/update?optimize=true >> /dev/console 2>&1' > /etc/cron.d/solr-optimize
  243. echo '*/20 * * * * vmail /usr/local/bin/quarantine_notify.py >> /dev/console 2>&1' > /etc/cron.d/quarantine_notify
  244. echo '15 4 * * * vmail /usr/local/bin/clean_q_aged.sh >> /dev/console 2>&1' > /etc/cron.d/clean_q_aged
  245. # Fix more than 1 hardlink issue
  246. touch /etc/crontab /etc/cron.*/*
  247. # Clean old PID if any
  248. [[ -f /var/run/dovecot/master.pid ]] && rm /var/run/dovecot/master.pid
  249. # Clean stopped imapsync jobs
  250. rm -f /tmp/imapsync_busy.lock
  251. IMAPSYNC_TABLE=$(mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "SHOW TABLES LIKE 'imapsync'" -Bs)
  252. [[ ! -z ${IMAPSYNC_TABLE} ]] && mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "UPDATE imapsync SET is_running='0'"
  253. # Envsubst maildir_gc
  254. echo "$(envsubst < /usr/local/bin/maildir_gc.sh)" > /usr/local/bin/maildir_gc.sh
  255. PUBKEY_MCRYPT=$(doveconf -P | grep -i mail_crypt_global_public_key | cut -d '<' -f2)
  256. if [ -f ${PUBKEY_MCRYPT} ]; then
  257. GUID=$(cat <(echo ${MAILCOW_HOSTNAME}) /mail_crypt/ecpubkey.pem | sha256sum | cut -d ' ' -f1 | tr -cd "[a-fA-F0-9.:/] ")
  258. if [ ${#GUID} -eq 64 ]; then
  259. mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} << EOF
  260. REPLACE INTO versions (application, version) VALUES ("GUID", "${GUID}");
  261. EOF
  262. else
  263. mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} << EOF
  264. REPLACE INTO versions (application, version) VALUES ("GUID", "INVALID");
  265. EOF
  266. fi
  267. fi
  268. # Collect SA rules once now
  269. /usr/local/bin/sa-rules.sh
  270. # Run hooks
  271. for file in /hooks/*; do
  272. if [ -x "${file}" ]; then
  273. echo "Running hook ${file}"
  274. "${file}"
  275. fi
  276. done
  277. # 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
  278. # May be related to something inside Docker, I seriously don't know
  279. touch /etc/dovecot/lua/app-passdb.lua
  280. exec "$@"