diff --git a/src/coulobre/hosting/test_web_node_apply.py b/src/coulobre/hosting/test_web_node_apply.py index c5a3cec..dade1e0 100644 --- a/src/coulobre/hosting/test_web_node_apply.py +++ b/src/coulobre/hosting/test_web_node_apply.py @@ -10,6 +10,7 @@ from coulobre.web_node import WebSite from coulobre.web_node_apply import ( SitePaths, install_configurations, + reserve_account, site_status, ) @@ -34,6 +35,18 @@ class PhpRuntimeTests(SimpleTestCase): class WebNodeConfigurationTests(SimpleTestCase): + def test_primary_website_cannot_claim_a_managed_tool_hostname(self): + website = WebSite(7, "tools.example.test") + with ( + patch( + "coulobre.web_node_apply.read_root_file", + return_value="\n" + " ServerName tools.example.test\n\n", + ), + self.assertRaisesRegex(ValueError, "reserved for a panel tool"), + ): + reserve_account(website, "fixture", ()) + def setUp(self): directory = Path(self.enterContext(TemporaryDirectory())) apache_available = directory / "sites-available" diff --git a/src/coulobre/managed_tools.py b/src/coulobre/managed_tools.py index c2e5b77..bf1b670 100644 --- a/src/coulobre/managed_tools.py +++ b/src/coulobre/managed_tools.py @@ -1,5 +1,6 @@ """Install hosting tools on dedicated HTTPS hosts.""" +import fcntl import hashlib import json import os @@ -15,6 +16,7 @@ from types import SimpleNamespace from urllib.parse import urlsplit from . import local_update +from . import web_node_apply as web from .domain_names import canonical_domain from .linked_updates import REMOTE_MODES, receiver_status from .local_feature_setup import ( @@ -473,13 +475,8 @@ def apply_tool(request, config, origin, mail_host, command): raise ValueError("Review the existing tool integration before setup.") if not keys and original.rstrip("\n").endswith("\\"): raise ValueError("Review the end of the panel configuration.") - existing = command("/usr/sbin/apache2ctl", "-S") site = WEB_CONFIG / "sites-available" / f"{base}.conf" - if ( - re.search(rf"\b{re.escape(hostname)}\b", existing) - and f"/{site.name}:" not in existing - ): - raise ValueError("Choose a hostname Apache does not already serve.") + check_tool_hostname(hostname, site, command) chain, key = tool_certificate(hostname, command) php = install_packages(target, command) create_service_identity(base, base, command) @@ -508,8 +505,30 @@ def apply_tool(request, config, origin, mail_host, command): updated.rstrip("\n") + f"\n{marker}\n{prefix}_BRIDGE_KEY={bridge}\n" ) - configure_apache(target, hostname, chain, key, site, php, command) - activate_tool(site, php, config, original, updated, url, origin, command) + require_root_directory(web.STATE_DIRECTORY.parent, create_mode=0o750) + require_root_directory(web.STATE_DIRECTORY, create_mode=0o700) + with web.locked_web_node(fcntl.LOCK_EX): + check_tool_hostname(hostname, site, command) + configure_apache(target, hostname, chain, key, site, php, command) + activate_tool( + site, php, config, original, updated, url, origin, command + ) + + +def check_tool_hostname(hostname, site, command): + from .web_node_aliases import check_alias_domain_available + + web.reject_tool_hostname(hostname, except_site=site) + check_alias_domain_available(hostname) + for marker in web.STATE_DIRECTORY.glob("*.json"): + if json.loads(read_root_file(marker))["domain"] == hostname: + raise ValueError("Choose a hostname away from hosted websites.") + existing = command("/usr/sbin/apache2ctl", "-S") + if ( + re.search(rf"\b{re.escape(hostname)}\b", existing) + and f"/{site.name}:" not in existing + ): + raise ValueError("Choose a hostname Apache does not already serve.") def configure_apache(target, hostname, chain, key, site, php, command): diff --git a/src/coulobre/web_node_aliases.py b/src/coulobre/web_node_aliases.py index 7895c78..b2cd0b2 100644 --- a/src/coulobre/web_node_aliases.py +++ b/src/coulobre/web_node_aliases.py @@ -64,6 +64,7 @@ def check_apache_hostname_available(domain): def reserve_alias(alias): + web.reject_tool_hostname(alias.domain) for marker in web.STATE_DIRECTORY.glob("*.json"): if json.loads(read_root_file(marker))["domain"] == alias.domain: raise ValueError( diff --git a/src/coulobre/web_node_apply.py b/src/coulobre/web_node_apply.py index 8dd848e..265da2a 100644 --- a/src/coulobre/web_node_apply.py +++ b/src/coulobre/web_node_apply.py @@ -294,6 +294,7 @@ def check_services(runtime): def reserve_account(site, user, paths): + reject_tool_hostname(site.domain) marker = STATE_DIRECTORY / f"{site.node_key}.json" if os.path.lexists(marker.with_suffix(".retired")): raise ValueError("This website belongs to a retired hosting account.") @@ -333,6 +334,25 @@ def reserve_account(site, user, paths): write_new_file(marker, json.dumps(manifest) + "\n", mode=0o600) +def reject_tool_hostname(hostname, *, except_site=None): + for tool in ("phpmyadmin", "roundcube"): + site = APACHE_AVAILABLE / f"coulobre-{tool}.conf" + if site == except_site: + continue + try: + configuration = read_root_file(site) + except FileNotFoundError: + continue + for line in configuration.splitlines(): + directive = line.split() + if ( + len(directive) == 2 + and directive[0].lower() == "servername" + and directive[1].lower().rstrip(".") == hostname + ): + raise ValueError("This hostname is reserved for a panel tool.") + + def current_user(name): try: return pwd.getpwnam(name)