docker-entrypoint.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #!/bin/bash
  2. until nc phpfpm 9001 -z; do
  3. echo "Waiting for PHP on port 9001..."
  4. sleep 3
  5. done
  6. until nc phpfpm 9002 -z; do
  7. echo "Waiting for PHP on port 9002..."
  8. sleep 3
  9. done
  10. mkdir -p /etc/rspamd/plugins.d \
  11. /etc/rspamd/custom
  12. touch /etc/rspamd/rspamd.conf.local \
  13. /etc/rspamd/rspamd.conf.override
  14. chmod 755 /var/lib/rspamd
  15. [[ ! -f /etc/rspamd/override.d/worker-controller-password.inc ]] && echo '# Autogenerated by mailcow' > /etc/rspamd/override.d/worker-controller-password.inc
  16. echo ${IPV4_NETWORK}.0/24 > /etc/rspamd/custom/mailcow_networks.map
  17. echo ${IPV6_NETWORK} >> /etc/rspamd/custom/mailcow_networks.map
  18. DOVECOT_V4=
  19. DOVECOT_V6=
  20. until [[ ! -z ${DOVECOT_V4} ]]; do
  21. DOVECOT_V4=$(dig a dovecot +short)
  22. DOVECOT_V6=$(dig aaaa dovecot +short)
  23. [[ ! -z ${DOVECOT_V4} ]] && break;
  24. echo "Waiting for Dovecot..."
  25. sleep 3
  26. done
  27. echo ${DOVECOT_V4}/32 > /etc/rspamd/custom/dovecot_trusted.map
  28. if [[ ! -z ${DOVECOT_V6} ]]; then
  29. echo ${DOVECOT_V6}/128 >> /etc/rspamd/custom/dovecot_trusted.map
  30. fi
  31. RSPAMD_V4=
  32. RSPAMD_V6=
  33. until [[ ! -z ${RSPAMD_V4} ]]; do
  34. RSPAMD_V4=$(dig a rspamd +short)
  35. RSPAMD_V6=$(dig aaaa rspamd +short)
  36. [[ ! -z ${RSPAMD_V4} ]] && break;
  37. echo "Waiting for Rspamd..."
  38. sleep 3
  39. done
  40. echo ${RSPAMD_V4}/32 > /etc/rspamd/custom/rspamd_trusted.map
  41. if [[ ! -z ${RSPAMD_V6} ]]; then
  42. echo ${RSPAMD_V6}/128 >> /etc/rspamd/custom/rspamd_trusted.map
  43. fi
  44. if [[ ! -z ${REDIS_SLAVEOF_IP} ]]; then
  45. cat <<EOF > /etc/rspamd/local.d/redis.conf
  46. read_servers = "redis:6379";
  47. write_servers = "${REDIS_SLAVEOF_IP}:${REDIS_SLAVEOF_PORT}";
  48. password = "${REDISPASS}";
  49. timeout = 10;
  50. EOF
  51. until [[ $(redis-cli -h redis-mailcow -a ${REDISPASS} --no-auth-warning PING) == "PONG" ]]; do
  52. echo "Waiting for Redis @redis-mailcow..."
  53. sleep 2
  54. done
  55. until [[ $(redis-cli -h ${REDIS_SLAVEOF_IP} -p ${REDIS_SLAVEOF_PORT} -a ${REDISPASS} --no-auth-warning PING) == "PONG" ]]; do
  56. echo "Waiting for Redis @${REDIS_SLAVEOF_IP}..."
  57. sleep 2
  58. done
  59. redis-cli -h redis-mailcow -a ${REDISPASS} --no-auth-warning SLAVEOF ${REDIS_SLAVEOF_IP} ${REDIS_SLAVEOF_PORT}
  60. else
  61. cat <<EOF > /etc/rspamd/local.d/redis.conf
  62. servers = "redis:6379";
  63. password = "${REDISPASS}";
  64. timeout = 10;
  65. EOF
  66. until [[ $(redis-cli -h redis-mailcow -a ${REDISPASS} --no-auth-warning PING) == "PONG" ]]; do
  67. echo "Waiting for Redis slave..."
  68. sleep 2
  69. done
  70. redis-cli -h redis-mailcow -a ${REDISPASS} --no-auth-warning SLAVEOF NO ONE
  71. fi
  72. # Provide additional lua modules
  73. ln -s /usr/lib/$(uname -m)-linux-gnu/liblua5.1-cjson.so.0.0.0 /usr/lib/rspamd/cjson.so
  74. chown -R _rspamd:_rspamd /var/lib/rspamd \
  75. /etc/rspamd/local.d \
  76. /etc/rspamd/override.d \
  77. /etc/rspamd/rspamd.conf.local \
  78. /etc/rspamd/rspamd.conf.override \
  79. /etc/rspamd/plugins.d
  80. # Fix missing default global maps, if any
  81. # These exists in mailcow UI and should not be removed
  82. touch /etc/rspamd/custom/global_mime_from_blacklist.map \
  83. /etc/rspamd/custom/global_rcpt_blacklist.map \
  84. /etc/rspamd/custom/global_smtp_from_blacklist.map \
  85. /etc/rspamd/custom/global_mime_from_whitelist.map \
  86. /etc/rspamd/custom/global_rcpt_whitelist.map \
  87. /etc/rspamd/custom/global_smtp_from_whitelist.map \
  88. /etc/rspamd/custom/bad_languages.map \
  89. /etc/rspamd/custom/sa-rules \
  90. /etc/rspamd/custom/dovecot_trusted.map \
  91. /etc/rspamd/custom/rspamd_trusted.map \
  92. /etc/rspamd/custom/mailcow_networks.map \
  93. /etc/rspamd/custom/ip_wl.map \
  94. /etc/rspamd/custom/fishy_tlds.map \
  95. /etc/rspamd/custom/bad_words.map \
  96. /etc/rspamd/custom/bad_asn.map \
  97. /etc/rspamd/custom/bad_words_de.map \
  98. /etc/rspamd/custom/bulk_header.map \
  99. /etc/rspamd/custom/bad_header.map
  100. # www-data (82) group needs to write to these files
  101. chown _rspamd:_rspamd /etc/rspamd/custom/
  102. chmod 0755 /etc/rspamd/custom/.
  103. chown -R 82:82 /etc/rspamd/custom/*
  104. chmod 644 -R /etc/rspamd/custom/*
  105. # Run hooks
  106. for file in /hooks/*; do
  107. if [ -x "${file}" ]; then
  108. echo "Running hook ${file}"
  109. "${file}"
  110. fi
  111. done
  112. # If DQS KEY is set in mailcow.conf add Spamhaus DQS RBLs
  113. if [[ ! -z ${SPAMHAUS_DQS_KEY} ]]; then
  114. cat <<EOF > /etc/rspamd/custom/dqs-rbl.conf
  115. # Autogenerated by mailcow. DO NOT TOUCH!
  116. spamhaus {
  117. rbl = "${SPAMHAUS_DQS_KEY}.zen.dq.spamhaus.net";
  118. from = false;
  119. }
  120. spamhaus_from {
  121. from = true;
  122. received = false;
  123. rbl = "${SPAMHAUS_DQS_KEY}.zen.dq.spamhaus.net";
  124. returncodes {
  125. SPAMHAUS_ZEN = [ "127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.0.9", "127.0.0.10", "127.0.0.11" ];
  126. }
  127. }
  128. spamhaus_authbl_received {
  129. # Check if the sender client is listed in AuthBL (AuthBL is *not* part of ZEN)
  130. rbl = "${SPAMHAUS_DQS_KEY}.authbl.dq.spamhaus.net";
  131. from = false;
  132. received = true;
  133. ipv6 = true;
  134. returncodes {
  135. SH_AUTHBL_RECEIVED = "127.0.0.20"
  136. }
  137. }
  138. spamhaus_dbl {
  139. # Add checks on the HELO string
  140. rbl = "${SPAMHAUS_DQS_KEY}.dbl.dq.spamhaus.net";
  141. helo = true;
  142. rdns = true;
  143. dkim = true;
  144. disable_monitoring = true;
  145. returncodes {
  146. RBL_DBL_SPAM = "127.0.1.2";
  147. RBL_DBL_PHISH = "127.0.1.4";
  148. RBL_DBL_MALWARE = "127.0.1.5";
  149. RBL_DBL_BOTNET = "127.0.1.6";
  150. RBL_DBL_ABUSED_SPAM = "127.0.1.102";
  151. RBL_DBL_ABUSED_PHISH = "127.0.1.104";
  152. RBL_DBL_ABUSED_MALWARE = "127.0.1.105";
  153. RBL_DBL_ABUSED_BOTNET = "127.0.1.106";
  154. RBL_DBL_DONT_QUERY_IPS = "127.0.1.255";
  155. }
  156. }
  157. spamhaus_dbl_fullurls {
  158. ignore_defaults = true;
  159. no_ip = true;
  160. rbl = "${SPAMHAUS_DQS_KEY}.dbl.dq.spamhaus.net";
  161. selector = 'urls:get_host'
  162. disable_monitoring = true;
  163. returncodes {
  164. DBLABUSED_SPAM_FULLURLS = "127.0.1.102";
  165. DBLABUSED_PHISH_FULLURLS = "127.0.1.104";
  166. DBLABUSED_MALWARE_FULLURLS = "127.0.1.105";
  167. DBLABUSED_BOTNET_FULLURLS = "127.0.1.106";
  168. }
  169. }
  170. spamhaus_zrd {
  171. # Add checks on the HELO string also for DQS
  172. rbl = "${SPAMHAUS_DQS_KEY}.zrd.dq.spamhaus.net";
  173. helo = true;
  174. rdns = true;
  175. dkim = true;
  176. disable_monitoring = true;
  177. returncodes {
  178. RBL_ZRD_VERY_FRESH_DOMAIN = ["127.0.2.2", "127.0.2.3", "127.0.2.4"];
  179. RBL_ZRD_FRESH_DOMAIN = [
  180. "127.0.2.5", "127.0.2.6", "127.0.2.7", "127.0.2.8", "127.0.2.9", "127.0.2.10", "127.0.2.11", "127.0.2.12", "127.0.2.13", "127.0.2.14", "127.0.2.15", "127.0.2.16", "127.0.2.17", "127.0.2.18", "127.0.2.19", "127.0.2.20", "127.0.2.21", "127.0.2.22", "127.0.2.23", "127.0.2.24"
  181. ];
  182. RBL_ZRD_DONT_QUERY_IPS = "127.0.2.255";
  183. }
  184. }
  185. "SPAMHAUS_ZEN_URIBL" {
  186. enabled = true;
  187. rbl = "${SPAMHAUS_DQS_KEY}.zen.dq.spamhaus.net";
  188. resolve_ip = true;
  189. checks = ['urls'];
  190. replyto = true;
  191. emails = true;
  192. ipv4 = true;
  193. ipv6 = true;
  194. emails_domainonly = true;
  195. returncodes {
  196. URIBL_SBL = "127.0.0.2";
  197. URIBL_SBL_CSS = "127.0.0.3";
  198. URIBL_XBL = ["127.0.0.4", "127.0.0.5", "127.0.0.6", "127.0.0.7"];
  199. URIBL_PBL = ["127.0.0.10", "127.0.0.11"];
  200. URIBL_DROP = "127.0.0.9";
  201. }
  202. }
  203. SH_EMAIL_DBL {
  204. ignore_defaults = true;
  205. replyto = true;
  206. emails_domainonly = true;
  207. disable_monitoring = true;
  208. rbl = "${SPAMHAUS_DQS_KEY}.dbl.dq.spamhaus.net";
  209. returncodes = {
  210. SH_EMAIL_DBL = [
  211. "127.0.1.2",
  212. "127.0.1.4",
  213. "127.0.1.5",
  214. "127.0.1.6"
  215. ];
  216. SH_EMAIL_DBL_ABUSED = [
  217. "127.0.1.102",
  218. "127.0.1.104",
  219. "127.0.1.105",
  220. "127.0.1.106"
  221. ];
  222. SH_EMAIL_DBL_DONT_QUERY_IPS = [ "127.0.1.255" ];
  223. }
  224. }
  225. SH_EMAIL_ZRD {
  226. ignore_defaults = true;
  227. replyto = true;
  228. emails_domainonly = true;
  229. disable_monitoring = true;
  230. rbl = "${SPAMHAUS_DQS_KEY}.zrd.dq.spamhaus.net";
  231. returncodes = {
  232. SH_EMAIL_ZRD_VERY_FRESH_DOMAIN = ["127.0.2.2", "127.0.2.3", "127.0.2.4"];
  233. SH_EMAIL_ZRD_FRESH_DOMAIN = [
  234. "127.0.2.5", "127.0.2.6", "127.0.2.7", "127.0.2.8", "127.0.2.9", "127.0.2.10", "127.0.2.11", "127.0.2.12", "127.0.2.13", "127.0.2.14", "127.0.2.15", "127.0.2.16", "127.0.2.17", "127.0.2.18", "127.0.2.19", "127.0.2.20", "127.0.2.21", "127.0.2.22", "127.0.2.23", "127.0.2.24"
  235. ];
  236. SH_EMAIL_ZRD_DONT_QUERY_IPS = [ "127.0.2.255" ];
  237. }
  238. }
  239. "DBL" {
  240. # override the defaults for DBL defined in modules.d/rbl.conf
  241. rbl = "${SPAMHAUS_DQS_KEY}.dbl.dq.spamhaus.net";
  242. disable_monitoring = true;
  243. }
  244. "ZRD" {
  245. ignore_defaults = true;
  246. rbl = "${SPAMHAUS_DQS_KEY}.zrd.dq.spamhaus.net";
  247. no_ip = true;
  248. dkim = true;
  249. emails = true;
  250. emails_domainonly = true;
  251. urls = true;
  252. returncodes = {
  253. ZRD_VERY_FRESH_DOMAIN = ["127.0.2.2", "127.0.2.3", "127.0.2.4"];
  254. ZRD_FRESH_DOMAIN = ["127.0.2.5", "127.0.2.6", "127.0.2.7", "127.0.2.8", "127.0.2.9", "127.0.2.10", "127.0.2.11", "127.0.2.12", "127.0.2.13", "127.0.2.14", "127.0.2.15", "127.0.2.16", "127.0.2.17", "127.0.2.18", "127.0.2.19", "127.0.2.20", "127.0.2.21", "127.0.2.22", "127.0.2.23", "127.0.2.24"];
  255. }
  256. }
  257. spamhaus_sbl_url {
  258. ignore_defaults = true
  259. rbl = "${SPAMHAUS_DQS_KEY}.sbl.dq.spamhaus.net";
  260. checks = ['urls'];
  261. disable_monitoring = true;
  262. returncodes {
  263. SPAMHAUS_SBL_URL = "127.0.0.2";
  264. }
  265. }
  266. SH_HBL_EMAIL {
  267. ignore_defaults = true;
  268. rbl = "_email.${SPAMHAUS_DQS_KEY}.hbl.dq.spamhaus.net";
  269. emails_domainonly = false;
  270. selector = "from('smtp').lower;from('mime').lower";
  271. ignore_whitelist = true;
  272. checks = ['emails', 'replyto'];
  273. hash = "sha1";
  274. returncodes = {
  275. SH_HBL_EMAIL = [
  276. "127.0.3.2"
  277. ];
  278. }
  279. }
  280. spamhaus_dqs_hbl {
  281. symbol = "HBL_FILE_UNKNOWN";
  282. rbl = "_file.${SPAMHAUS_DQS_KEY}.hbl.dq.spamhaus.net.";
  283. selector = "attachments('rbase32', 'sha256')";
  284. ignore_whitelist = true;
  285. ignore_defaults = true;
  286. returncodes {
  287. SH_HBL_FILE_MALICIOUS = "127.0.3.10";
  288. SH_HBL_FILE_SUSPICIOUS = "127.0.3.15";
  289. }
  290. }
  291. EOF
  292. else
  293. rm -rf /etc/rspamd/custom/dqs-rbl.conf
  294. fi
  295. exec "$@"