BootstrapRspamd.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from jinja2 import Environment, FileSystemLoader
  2. from modules.BootstrapBase import BootstrapBase
  3. from pathlib import Path
  4. import os
  5. import sys
  6. import time
  7. import platform
  8. class Bootstrap(BootstrapBase):
  9. def bootstrap(self):
  10. # Connect to MySQL
  11. self.connect_mysql()
  12. # Connect to MySQL
  13. self.connect_redis()
  14. # get dovecot ips
  15. dovecot_v4 = []
  16. dovecot_v6 = []
  17. while not dovecot_v4 and not dovecot_v6:
  18. try:
  19. dovecot_v4 = self.resolve_docker_dns_record("dovecot-mailcow", "A")
  20. dovecot_v6 = self.resolve_docker_dns_record("dovecot-mailcow", "AAAA")
  21. except Exception as e:
  22. print(e)
  23. if not dovecot_v4 and not dovecot_v6:
  24. print("Waiting for Dovecot IPs...")
  25. time.sleep(3)
  26. # get rspamd ips
  27. rspamd_v4 = []
  28. rspamd_v6 = []
  29. while not rspamd_v4 and not rspamd_v6:
  30. try:
  31. rspamd_v4 = self.resolve_docker_dns_record("rspamd-mailcow", "A")
  32. rspamd_v6 = self.resolve_docker_dns_record("rspamd-mailcow", "AAAA")
  33. except Exception:
  34. print(e)
  35. if not rspamd_v4 and not rspamd_v6:
  36. print("Waiting for Rspamd IPs...")
  37. time.sleep(3)
  38. # wait for Services
  39. services = [
  40. ["php-fpm-mailcow", 9001],
  41. ["php-fpm-mailcow", 9002]
  42. ]
  43. for service in services:
  44. while not self.is_port_open(service[0], service[1]):
  45. print(f"Waiting for {service[0]} on port {service[1]}...")
  46. time.sleep(1)
  47. print(f"Service {service[0]} on port {service[1]} is ready!")
  48. for dir_path in ["/etc/rspamd/plugins.d", "/etc/rspamd/custom"]:
  49. Path(dir_path).mkdir(parents=True, exist_ok=True)
  50. for file_path in ["/etc/rspamd/rspamd.conf.local", "/etc/rspamd/rspamd.conf.override"]:
  51. Path(file_path).touch(exist_ok=True)
  52. self.set_permissions("/var/lib/rspamd", 0o755)
  53. # Setup Jinja2 Environment and load vars
  54. self.env = Environment(
  55. loader=FileSystemLoader('./etc/rspamd/config_templates'),
  56. keep_trailing_newline=True,
  57. lstrip_blocks=True,
  58. trim_blocks=True
  59. )
  60. extra_vars = {
  61. "DOVECOT_V4": dovecot_v4[0],
  62. "DOVECOT_V6": dovecot_v6[0],
  63. "RSPAMD_V4": rspamd_v4[0],
  64. "RSPAMD_V6": rspamd_v6[0],
  65. }
  66. self.env_vars = self.prepare_template_vars('/overwrites.json', extra_vars)
  67. print("Set Timezone")
  68. self.set_timezone()
  69. print("Render config")
  70. self.render_config("mailcow_networks.map.j2", "/etc/rspamd/custom/mailcow_networks.map")
  71. self.render_config("dovecot_trusted.map.j2", "/etc/rspamd/custom/dovecot_trusted.map")
  72. self.render_config("rspamd_trusted.map.j2", "/etc/rspamd/custom/rspamd_trusted.map")
  73. self.render_config("external_services.conf.j2", "/etc/rspamd/local.d/external_services.conf")
  74. self.render_config("redis.conf.j2", "/etc/rspamd/local.d/redis.conf")
  75. self.render_config("dqs-rbl.conf.j2", "/etc/rspamd/custom/dqs-rbl.conf")
  76. self.render_config("worker-controller-password.inc.j2", "/etc/rspamd/override.d/worker-controller-password.inc")
  77. # Fix missing default global maps, if any
  78. # These exists in mailcow UI and should not be removed
  79. files = [
  80. "/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. ]
  99. for file in files:
  100. path = Path(file)
  101. path.parent.mkdir(parents=True, exist_ok=True)
  102. path.touch(exist_ok=True)
  103. # Fix permissions
  104. paths_rspamd = [
  105. "/var/lib/rspamd",
  106. "/etc/rspamd/local.d",
  107. "/etc/rspamd/override.d",
  108. "/etc/rspamd/rspamd.conf.local",
  109. "/etc/rspamd/rspamd.conf.override",
  110. "/etc/rspamd/plugins.d"
  111. ]
  112. for path in paths_rspamd:
  113. self.set_owner(path, "_rspamd", "_rspamd", recursive=True)
  114. self.set_owner("/etc/rspamd/custom", "_rspamd", "_rspamd")
  115. self.set_permissions("/etc/rspamd/custom", 0o755)
  116. custom_path = Path("/etc/rspamd/custom")
  117. for child in custom_path.iterdir():
  118. if child.is_file():
  119. self.set_owner(child, 82, 82)
  120. self.set_permissions(child, 0o644)
  121. # Provide additional lua modules
  122. arch = platform.machine()
  123. self.run_command(["ln", "-s", f"/usr/lib/{arch}-linux-gnu/liblua5.1-cjson.so.0.0.0", "/usr/lib/rspamd/cjson.so"], check=False)