docker-entrypoint.sh 11 KB

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