BootstrapRspamd.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from jinja2 import Environment, FileSystemLoader
  2. from modules.BootstrapBase import BootstrapBase
  3. from pathlib import Path
  4. import time
  5. import platform
  6. class BootstrapRspamd(BootstrapBase):
  7. def bootstrap(self):
  8. # Connect to MySQL
  9. self.connect_mysql()
  10. # Connect to MySQL
  11. self.connect_redis()
  12. # get dovecot ips
  13. dovecot_v4 = []
  14. dovecot_v6 = []
  15. while not dovecot_v4 and not dovecot_v6:
  16. try:
  17. dovecot_v4 = self.resolve_docker_dns_record("dovecot-mailcow", "A")
  18. dovecot_v6 = self.resolve_docker_dns_record("dovecot-mailcow", "AAAA")
  19. except Exception as e:
  20. print(e)
  21. if not dovecot_v4 and not dovecot_v6:
  22. print("Waiting for Dovecot IPs...")
  23. time.sleep(3)
  24. # get rspamd ips
  25. rspamd_v4 = []
  26. rspamd_v6 = []
  27. while not rspamd_v4 and not rspamd_v6:
  28. try:
  29. rspamd_v4 = self.resolve_docker_dns_record("rspamd-mailcow", "A")
  30. rspamd_v6 = self.resolve_docker_dns_record("rspamd-mailcow", "AAAA")
  31. except Exception:
  32. print(e)
  33. if not rspamd_v4 and not rspamd_v6:
  34. print("Waiting for Rspamd IPs...")
  35. time.sleep(3)
  36. # wait for Services
  37. services = [
  38. ["php-fpm-mailcow", 9001],
  39. ["php-fpm-mailcow", 9002]
  40. ]
  41. for service in services:
  42. while not self.is_port_open(service[0], service[1]):
  43. print(f"Waiting for {service[0]} on port {service[1]}...")
  44. time.sleep(1)
  45. print(f"Service {service[0]} on port {service[1]} is ready!")
  46. for dir_path in ["/etc/rspamd/plugins.d", "/etc/rspamd/custom"]:
  47. Path(dir_path).mkdir(parents=True, exist_ok=True)
  48. for file_path in ["/etc/rspamd/rspamd.conf.local", "/etc/rspamd/rspamd.conf.override"]:
  49. Path(file_path).touch(exist_ok=True)
  50. self.set_permissions("/var/lib/rspamd", 0o755)
  51. # Setup Jinja2 Environment and load vars
  52. self.env = Environment(
  53. loader=FileSystemLoader([
  54. '/service_config/custom_templates',
  55. '/service_config/config_templates'
  56. ]),
  57. keep_trailing_newline=True,
  58. lstrip_blocks=True,
  59. trim_blocks=True
  60. )
  61. extra_vars = {
  62. "DOVECOT_V4": dovecot_v4[0],
  63. "DOVECOT_V6": dovecot_v6[0],
  64. "RSPAMD_V4": rspamd_v4[0],
  65. "RSPAMD_V6": rspamd_v6[0],
  66. }
  67. self.env_vars = self.prepare_template_vars('/service_config/overwrites.json', extra_vars)
  68. print("Set Timezone")
  69. self.set_timezone()
  70. print("Render config")
  71. self.render_config("/service_config")
  72. # Fix missing default global maps, if any
  73. # These exists in mailcow UI and should not be removed
  74. files = [
  75. "/etc/rspamd/custom/global_mime_from_blacklist.map",
  76. "/etc/rspamd/custom/global_rcpt_blacklist.map",
  77. "/etc/rspamd/custom/global_smtp_from_blacklist.map",
  78. "/etc/rspamd/custom/global_mime_from_whitelist.map",
  79. "/etc/rspamd/custom/global_rcpt_whitelist.map",
  80. "/etc/rspamd/custom/global_smtp_from_whitelist.map",
  81. "/etc/rspamd/custom/bad_languages.map",
  82. "/etc/rspamd/custom/sa-rules",
  83. "/etc/rspamd/custom/dovecot_trusted.map",
  84. "/etc/rspamd/custom/rspamd_trusted.map",
  85. "/etc/rspamd/custom/mailcow_networks.map",
  86. "/etc/rspamd/custom/ip_wl.map",
  87. "/etc/rspamd/custom/fishy_tlds.map",
  88. "/etc/rspamd/custom/bad_words.map",
  89. "/etc/rspamd/custom/bad_asn.map",
  90. "/etc/rspamd/custom/bad_words_de.map",
  91. "/etc/rspamd/custom/bulk_header.map",
  92. "/etc/rspamd/custom/bad_header.map"
  93. ]
  94. for file in files:
  95. path = Path(file)
  96. path.parent.mkdir(parents=True, exist_ok=True)
  97. path.touch(exist_ok=True)
  98. # Fix permissions
  99. paths_rspamd = [
  100. "/var/lib/rspamd",
  101. "/etc/rspamd/local.d",
  102. "/etc/rspamd/override.d",
  103. "/etc/rspamd/rspamd.conf.local",
  104. "/etc/rspamd/rspamd.conf.override",
  105. "/etc/rspamd/plugins.d"
  106. ]
  107. for path in paths_rspamd:
  108. self.set_owner(path, "_rspamd", "_rspamd", recursive=True)
  109. self.set_owner("/etc/rspamd/custom", "_rspamd", "_rspamd")
  110. self.set_permissions("/etc/rspamd/custom", 0o755)
  111. custom_path = Path("/etc/rspamd/custom")
  112. for child in custom_path.iterdir():
  113. if child.is_file():
  114. self.set_owner(child, 82, 82)
  115. self.set_permissions(child, 0o644)
  116. # Provide additional lua modules
  117. arch = platform.machine()
  118. 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)