From 7e8819d7d51f4f889a56977f4c5bdd46c34f50a3 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 1 Oct 2024 19:26:51 -0700 Subject: [PATCH] names: Try to install systemd-resolved during app setup - If installing systemd-resolved for the first time, set fallback DNS setting to True irrespective of the app version. Tests: - Ensure that systemd-resolved is not installed. On a fresh systemd without first setup done, run service. - Names app setup is run and systemd-resolved is installed if internet connection is available. Setup succeeds. Fallback DNS setting is true in privacy app. systemd-resolved has been restarted and current DNS known to Network Manager has been populated in it. Name resolution works. - If Internet connection is not available, setup still succeeds but systemd-resolved package is not installed. - Rerun setup without internet connectivity. Setup succeeds without installing systemd-resolved. - Rerun setup with internet connectivity. Setup succeeds and installs systemd-resolved. Fallback DNS setting is true in privacy app. systemd-resolved has been restarted and current DNS known to Network Manager has been populated in it. Name resolution works. Signed-off-by: Sunil Mohan Adapa Reviewed-by: Veiko Aasa --- plinth/modules/names/__init__.py | 37 +++++++++++++++++++----------- plinth/modules/names/privileged.py | 14 +++++++++++ 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/plinth/modules/names/__init__.py b/plinth/modules/names/__init__.py index 6236f78f7..c46ca09ad 100644 --- a/plinth/modules/names/__init__.py +++ b/plinth/modules/names/__init__.py @@ -102,22 +102,31 @@ class NamesApp(app_module.App): """Install and configure the app.""" super().setup(old_version) - # Fresh install or upgrading to version 2 - if old_version < 2: - privileged.set_resolved_configuration(dns_fallback=True) + if not is_resolved_installed(): + try: + # Requires internet connectivity and could fail + privileged.install_resolved() + privileged.set_resolved_configuration(dns_fallback=True) + except Exception: + pass - # Load the configuration files for systemd-resolved provided by - # FreedomBox. - service_privileged.restart('systemd-resolved') + if is_resolved_installed(): + # Fresh install or upgrading to version 2 + if old_version < 2: + privileged.set_resolved_configuration(dns_fallback=True) - # After systemd-resolved is freshly installed, /etc/resolve.conf - # becomes a symlink to configuration pointing to systemd-resovled stub - # resolver. However, the old contents are not fed from network-manager - # (if it was present earlier and wrote to /etc/resolve.conf). Ask - # network-manager to feed the DNS servers from the connections it has - # established to systemd-resolved so that using fallback DNS servers is - # not necessary. - network.refeed_dns() + # Load the configuration files for systemd-resolved provided by + # FreedomBox. + service_privileged.restart('systemd-resolved') + + # After systemd-resolved is freshly installed, /etc/resolve.conf + # becomes a symlink to configuration pointing to systemd-resovled + # stub resolver. However, the old contents are not fed from + # network-manager (if it was present earlier and wrote to + # /etc/resolve.conf). Ask network-manager to feed the DNS servers + # from the connections it has established to systemd-resolved so + # that using fallback DNS servers is not necessary. + network.refeed_dns() self.enable() diff --git a/plinth/modules/names/privileged.py b/plinth/modules/names/privileged.py index 8d492085e..d113f11b5 100644 --- a/plinth/modules/names/privileged.py +++ b/plinth/modules/names/privileged.py @@ -52,6 +52,20 @@ def set_domain_name(domain_name: str | None = None): hosts_path.write_text(''.join(new_lines), encoding='utf-8') +@privileged +def install_resolved(): + """Install systemd-resolved related packages.""" + packages = ['systemd-resolved', 'libnss-resolve'] + subprocess.run(['dpkg', '--configure', '-a'], check=False) + with action_utils.apt_hold_freedombox(): + action_utils.run_apt_command(['--fix-broken', 'install']) + returncode = action_utils.run_apt_command(['install'] + packages) + + if returncode: + raise RuntimeError( + f'Apt command failed with return code: {returncode}') + + @privileged def set_resolved_configuration(dns_fallback: bool | None = None, dns_over_tls: str | None = None,