aboutsummaryrefslogtreecommitdiffstats
path: root/modules/by-name/dn/dns/module.nix
blob: c429bbacfc81f8ae9210586642ea96f21fb81e0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
{
  config,
  lib,
  ...
}: let
  cfg = config.vhack.dns;

  localInterface = "127.0.0.1@5353";

  zones =
    builtins.mapAttrs (name: value: {
      data =
        dns.types.zone.renderToString name value;
    })
    cfg.zones;

  stubZones =
    lib.mapAttrsToList (name: value: {
      name = "${name}";
      stub-addr = localInterface;

      # Don't use published NS records, always use stub-addr
      stub-prime = false;

      # Don't cache the stub responses, it is literally on the same host.
      stub-no-cache = true;
    })
    cfg.zones;
  zoneNames = builtins.attrNames cfg.zones;

  dns = import ./dns {inherit lib;};

  ports = let
    parsePorts = listeners: let
      splitAddress = addr: lib.splitString "@" addr;

      extractPort = addr: let
        split = splitAddress addr;
      in
        lib.toInt (
          if (builtins.length split) == 2
          then builtins.elemAt split 1
          else "53"
        );
    in
      builtins.map extractPort listeners;
  in
    lib.unique (parsePorts cfg.interfaces);
in {
  options.vhack.dns = {
    enable = lib.mkEnableOption "custom dns server";

    openFirewall = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = ''
        Open the following ports:
        TCP (${lib.concatStringsSep ", " (map toString ports)})
        UDP (${lib.concatStringsSep ", " (map toString ports)})
      '';
    };

    interfaces = lib.mkOption {
      type = lib.types.listOf lib.types.str;
      description = ''
        A list of the interfaces to bind to. To select the port add `@` to the end of the
        interface. The default port is 53.
      '';
      example = [
        "192.168.1.3"
        "2001:db8:1::3"
      ];
    };

    zones = lib.mkOption {
      type = lib.types.attrsOf dns.types.zone.zone;
      description = "DNS zones";
    };
  };

  config = lib.mkIf cfg.enable {
    vhack.persist.directories = [
      {
        directory = "/var/lib/unbound";
        user = "unbound";
        group = "unbound";
        mode = "0700";
      }
    ];

    # Use unbound for resolving all local DNS queries.
    # unbounds also has the upside to verify DNSSEC.
    services.unbound = {
      enable = true;
      stateDir = "/var/lib/unbound";
      resolveLocalQueries = true;
      settings = {
        server = {
          do-not-query-localhost = false;

          hide-identity = true;
          hide-version = true;
          harden-glue = true;
          harden-dnssec-stripped = true;
          # 0x20-encoded randomization
          use-caps-for-id = true;

          # DNSSEC - enabled by default with auto-trust-anchor-file
          # auto-trust-anchor-file = "/var/lib/unbound/root.key";
        };

        # Forward queries for in our zones to the local nsd.
        # It makes resolves little bit faster for the real deployment,
        # and importantly breaks the dependency on another ns server.
        stub-zone = stubZones;

        # Our local zones do not have DNSSEC, so we need to mark them as insecure.
        domain-insecure = zoneNames;
      };
    };

    # Use nsd to serve our own zone files, with the configured NS record pointing to the
    # servers ip address.
    services.nsd = {
      enable = true;
      verbosity = 4;
      interfaces = [localInterface] ++ cfg.interfaces;
      inherit zones;
    };

    networking.firewall.allowedUDPPorts = lib.mkIf cfg.openFirewall ports;
    networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall ports;

    systemd.services.nsd = {
      requires = [
        "network-online.target"
      ];
      after = [
        "network.target"
        "network-online.target"
      ];
    };
    users = {
      users = {
        "unbound".uid = config.vhack.constants.ids.uids.unbound;
      };
      groups = {
        "unbound".gid = config.vhack.constants.ids.gids.unbound;
      };
    };
  };
}