Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore dns on unclean shutdown #1494

Merged
merged 35 commits into from Jan 30, 2024
Merged

Restore dns on unclean shutdown #1494

merged 35 commits into from Jan 30, 2024

Conversation

lixmal
Copy link
Contributor

@lixmal lixmal commented Jan 23, 2024

Describe your changes

  1. When the Netbird agent terminates ungracefully (kill, power outage, panic, etc.), the system DNS settings are not restored.

    This can result in broken DNS resolution (e.g. a DNS server provided by a peer), in which case the agent won't be able to connect to the management server.

    To fix this issue we create an indicator file after adjusting the system DNS settings.
    This file is deleted once the settings are restored.

    If the agent terminates ungracefully it won't be able to delete this file. Therefore when it is started again it will detect the existence of this file and restore the previous DNS state.

    • On Linux

      /var/lib/netbird/resolv.conf, contains the old /etc/resolv.conf
      /var/lib/netbird/manager, contains the name of the previous host manager and last netbird IP address

      • manager file

        DNS settings are restored by copying the /var/lib/netbird/resolv.conf back to /etc/resolv.conf

        Test:

        • Uninstall systemd-resolved, openresolv/resolvconf
        • echo nameserver 9.9.9.9 | sudo tee /etc/resolv.conf
        • Watch /var/log/netbird/client.log for restore message and /etc/resolv.conf
        • Start agent
        • killall -9 netbird
        • Start agent
      • manager resolvconf

        DNS settings are restored by running restoreHostDNS() (runs resolvconf -d <interface>)

        Test:

        • Uninstall systemd-resolved
        • Install openresolv package
        • echo "nameserver 9.9.9.9" | sudo resolvconf -x -a <physical int>
        • Watch /var/log/netbird/client.log for restore message and /etc/resolv.conf
        • Start agent
        • killall -9 netbird
        • Start agent
      • manager systemd

        DNS settings are restored by running restoreHostDNS() (runs dbus methods`)

        Test:

        • Watch /var/log/netbird/client.log for restore message and resolvectl status
        • Start agent
        • killall -9 netbird
        • Start agent
      • manager networkManager

        DNS settings are restored by running restoreHostDNS() (runs dbus methods`)

        Test:

        • Uninstall systemd-resolved
        • Edit /etc/NetworkManager/NetworkManager.conf, add dns=dnsmasq in the main section
        • sudo nmcli general reload or sudo nmcli networking off; sudo nmcli networking on
        • Watch /var/log/netbird/client.log for restore message and nmcli device show | grep -i dns
        • Start agent
        • killall -9 netbird
        • Start agent
    • On Windows

      %PROGRAMDATA%\Netbird\unclean_shutdown_dns.txt, contains the interface guid that is required to set up the host manager.

      DNS settings are restored by running restoreHostDNS() (removes registry keys)

      Test:

      • Watch %PROGRAMDATA%\Netbird\client.log for restore message and SYSTEM\CurrentControlSet\Services\Dnscache\Parameters\DnsPolicyConfig\NetBird-Match registry
      • Start agent
      • Start PowerShell as admin
      • Get-Process -Name netbird | Stop-Process -Force
      • (Re)start agent
    • On macOS

      /var/lib/netbird/unclean_shutdown_dns, empty

      DNS settings are restored by running restoreHostDNS() (runs scutil remove).

      Test:

      • Watch /var/log/netbird/client.log for restore message and scutil --dns for DNS settings
      • Start agent
      • sudo killall -9 netbird
      • Start agent
    • mobile should not be affected

  2. Moves the restore DNS logic before the remove interface logic so we don't get errors for missing interfaces

  3. Improves error handling and error messages for debugging

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • Extended the README / documentation, if necessary

@lixmal lixmal force-pushed the restore-dns-on-unclean-shutdown branch from 64bd7ef to 2b12336 Compare January 23, 2024 15:58
@lixmal lixmal marked this pull request as ready for review January 24, 2024 22:23
@lixmal lixmal merged commit 846d486 into main Jan 30, 2024
16 checks passed
@lixmal lixmal deleted the restore-dns-on-unclean-shutdown branch January 30, 2024 08:58
Foosec pushed a commit to Foosec/netbird that referenced this pull request May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants