docker-entrypoint.sh 11 KB

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