docker-entrypoint.sh 10.0 KB

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