BootstrapSogo.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. class Bootstrap(BootstrapBase):
  8. def bootstrap(self):
  9. # Skip SOGo if set
  10. if self.isYes(os.getenv("SKIP_SOGO", "")):
  11. print("SKIP_SOGO is set, skipping SOGo startup...")
  12. time.sleep(365 * 24 * 60 * 60)
  13. sys.exit(1)
  14. # Connect to MySQL
  15. self.connect_mysql()
  16. # Wait until port is free
  17. while self.is_port_open("sogo-mailcow", 20000):
  18. print("Port 20000 still in use — terminating sogod...")
  19. self.kill_proc("sogod")
  20. time.sleep(3)
  21. # Wait for schema to update to expected version
  22. self.wait_for_schema_update(init_file_path="init_db.inc.php")
  23. # Setup Jinja2 Environment and load vars
  24. self.env = Environment(
  25. loader=FileSystemLoader([
  26. '/etc/sogo/custom_templates',
  27. '/etc/sogo/config_templates'
  28. ]),
  29. keep_trailing_newline=True,
  30. lstrip_blocks=True,
  31. trim_blocks=True
  32. )
  33. extra_vars = {
  34. "SQL_DOMAINS": self.get_domains(),
  35. "IAM_SETTINGS": self.get_identity_provider_settings()
  36. }
  37. self.env_vars = self.prepare_template_vars('/overwrites.json', extra_vars)
  38. print("Set Timezone")
  39. self.set_timezone()
  40. print("Set Syslog redis")
  41. self.set_syslog_redis()
  42. print("Render config")
  43. self.render_config("/etc/sogo/config.json")
  44. print("Fix permissions")
  45. self.set_owner("/var/lib/sogo", "sogo", "sogo", recursive=True)
  46. self.set_permissions("/var/lib/sogo/GNUstep/Defaults/sogod.plist", 0o600)
  47. # Rename custom logo
  48. logo_src = Path("/etc/sogo/sogo-full.svg")
  49. if logo_src.exists():
  50. print("Set Logo")
  51. self.move_file(logo_src, "/etc/sogo/custom-fulllogo.svg")
  52. # Rsync web content
  53. print("Syncing web content")
  54. self.rsync_file("/usr/lib/GNUstep/SOGo/", "/sogo_web/", recursive=True)
  55. # Chown backup path
  56. self.set_owner("/sogo_backup", "sogo", "sogo", recursive=True)
  57. def get_domains(self):
  58. """
  59. Retrieves a list of domains and their GAL (Global Address List) status.
  60. Executes a SQL query to select:
  61. - `domain`
  62. - a human-readable GAL status ("YES" or "NO")
  63. - `ldap_gal` as a boolean (True/False)
  64. Returns:
  65. list[dict]: A list of dicts with keys: domain, gal_status, ldap_gal.
  66. Example: [{"domain": "example.com", "gal_status": "YES", "ldap_gal": True}]
  67. Logs:
  68. Error messages if the query fails.
  69. """
  70. query = """
  71. SELECT domain,
  72. CASE gal WHEN '1' THEN 'YES' ELSE 'NO' END AS gal_status,
  73. ldap_gal = 1 AS ldap_gal
  74. FROM domain;
  75. """
  76. try:
  77. cursor = self.mysql_conn.cursor()
  78. cursor.execute(query)
  79. result = cursor.fetchall()
  80. cursor.close()
  81. return [
  82. {
  83. "domain": row[0],
  84. "gal_status": row[1],
  85. "ldap_gal": bool(row[2])
  86. }
  87. for row in result
  88. ]
  89. except Exception as e:
  90. print(f"Error fetching domains: {e}")
  91. return []
  92. def get_identity_provider_settings(self):
  93. """
  94. Retrieves all key-value identity provider settings.
  95. Returns:
  96. dict: Settings in the format { key: value }
  97. Logs:
  98. Error messages if the query fails.
  99. """
  100. query = "SELECT `key`, `value` FROM identity_provider;"
  101. try:
  102. cursor = self.mysql_conn.cursor()
  103. cursor.execute(query)
  104. result = cursor.fetchall()
  105. cursor.close()
  106. iam_settings = {row[0]: row[1] for row in result}
  107. if iam_settings['authsource'] == "ldap":
  108. protocol = "ldaps" if iam_settings.get("use_ssl") else "ldap"
  109. starttls = "/????!StartTLS" if iam_settings.get("use_tls") else ""
  110. iam_settings['ldap_url'] = f"{protocol}://{iam_settings['host']}:{iam_settings['port']}{starttls}"
  111. return iam_settings
  112. except Exception as e:
  113. print(f"Error fetching identity provider settings: {e}")
  114. return {}