]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/dns.nix
Add keys handling for bind9
[perso/Immae/Config/Nix.git] / modules / private / dns.nix
1 { lib, pkgs, config, ... }:
2 {
3 options.myServices.dns.enable = lib.mkEnableOption "enable DNS resolver";
4 config = let
5 cfg = config.services.bind;
6 keyIncludes = builtins.concatStringsSep "\n" (map (v: "include \"/var/secrets/bind/${v}.key\";") (builtins.attrNames config.myEnv.dns.keys));
7 toKeyList = servers: keys: builtins.concatStringsSep "\n" (map (s: ''
8 server ${s} {
9 keys { ${builtins.concatStringsSep ";" keys}; };
10 };
11 '') servers);
12 serverIncludes = builtins.concatStringsSep "\n" (map (v:
13 lib.optionalString (builtins.length v.keys > 0) (toKeyList (lib.flatten (map (n: builtins.attrValues config.myEnv.dns.ns."${n}") v.masters)) v.keys)
14 ) config.myEnv.dns.slaveZones);
15 configFile = pkgs.writeText "named.conf" ''
16 include "/etc/bind/rndc.key";
17 controls {
18 inet 127.0.0.1 allow {localhost;} keys {"rndc-key";};
19 };
20
21 acl cachenetworks { ${lib.concatMapStrings (entry: " ${entry}; ") cfg.cacheNetworks} };
22 acl badnetworks { ${lib.concatMapStrings (entry: " ${entry}; ") cfg.blockedNetworks} };
23
24 options {
25 listen-on { ${lib.concatMapStrings (entry: " ${entry}; ") cfg.listenOn} };
26 listen-on-v6 { ${lib.concatMapStrings (entry: " ${entry}; ") cfg.listenOnIpv6} };
27 allow-query { cachenetworks; };
28 blackhole { badnetworks; };
29 forward first;
30 forwarders { ${lib.concatMapStrings (entry: " ${entry}; ") cfg.forwarders} };
31 directory "/var/run/named";
32 pid-file "/var/run/named/named.pid";
33 ${cfg.extraOptions}
34 };
35
36 ${keyIncludes}
37 ${serverIncludes}
38
39 ${cfg.extraConfig}
40
41 ${ lib.concatMapStrings
42 ({ name, file, master ? true, extra ? "", slaves ? [], masters ? [] }:
43 ''
44 zone "${name}" {
45 type ${if master then "master" else "slave"};
46 file "${file}";
47 ${ if lib.lists.length slaves > 0 then
48 ''
49 allow-transfer {
50 ${lib.concatMapStrings (ip: "${ip};\n") slaves}
51 };
52 '' else ""}
53 ${ if lib.lists.length masters > 0 then
54 ''
55 masters {
56 ${lib.concatMapStrings (ip: "${ip};\n") masters}
57 };
58 '' else ""}
59 allow-query { any; };
60 ${extra}
61 };
62 '')
63 cfg.zones }
64 '';
65 mxes = lib.attrsets.filterAttrs
66 (n: v: v.mx.enable)
67 config.myEnv.servers;
68 ip4mxes = builtins.concatStringsSep "\n" (lib.mapAttrsToList
69 (n: v: "${v.mx.subdomain} IN A ${v.ips.main.ip4}")
70 mxes);
71 ip6mxes = builtins.concatStringsSep "\n" (lib.mapAttrsToList
72 (n: v: builtins.concatStringsSep "\n" (map (i: "${v.mx.subdomain} IN AAAA ${i}") v.ips.main.ip6))
73 mxes);
74 mxmxes = n: conf: builtins.concatStringsSep "\n" (lib.mapAttrsToList
75 (_: v: "${n} IN MX ${v.mx.priority} ${v.mx.subdomain}.${conf.name}.")
76 mxes);
77 in lib.mkIf config.myServices.dns.enable {
78 networking.firewall.allowedUDPPorts = [ 53 ];
79 networking.firewall.allowedTCPPorts = [ 53 ];
80 users.users.named.extraGroups = [ "keys" ];
81 secrets.keys = lib.mapAttrsToList (k: v:
82 {
83 dest = "bind/${k}.key";
84 permissions = "0400";
85 user = "named";
86 text = ''
87 key "${k}"
88 {
89 algorithm ${v.algorithm};
90 secret "${v.secret}";
91 };
92 '';
93 }
94 ) config.myEnv.dns.keys;
95 services.bind = {
96 enable = true;
97 cacheNetworks = ["any"];
98 configFile = configFile;
99 extraOptions = ''
100 allow-recursion { 127.0.0.1; };
101 allow-transfer { none; };
102
103 notify-source ${config.myEnv.servers.eldiron.ips.main.ip4};
104 notify-source-v6 ${lib.head config.myEnv.servers.eldiron.ips.main.ip6};
105 version none;
106 hostname none;
107 server-id none;
108 '';
109 zones = with config.myEnv.dns;
110 assert (builtins.substring ((builtins.stringLength soa.email)-1) 1 soa.email) != ".";
111 assert (builtins.substring ((builtins.stringLength soa.primary)-1) 1 soa.primary) != ".";
112 (map (conf: {
113 name = conf.name;
114 master = false;
115 file = "/var/run/named/${conf.name}.zone";
116 masters = if lib.attrsets.hasAttr "masters" conf
117 then lib.lists.flatten (map (n: lib.attrsets.attrValues ns.${n}) conf.masters)
118 else [];
119 }) slaveZones)
120 ++ (map (conf: {
121 name = conf.name;
122 master = true;
123 extra = if lib.attrsets.hasAttr "extra" conf then conf.extra else "";
124 slaves = if lib.attrsets.hasAttr "slaves" conf
125 then lib.lists.flatten (map (n: lib.attrsets.attrValues ns.${n}) conf.slaves)
126 else [];
127 file = pkgs.writeText "${conf.name}.zone" ''
128 $TTL 10800
129 @ IN SOA ${soa.primary}. ${builtins.replaceStrings ["@"] ["."] soa.email}. ${soa.serial} ${soa.refresh} ${soa.retry} ${soa.expire} ${soa.ttl}
130
131 ${lib.concatStringsSep "\n" (map (x: "@ IN NS ${x}.") (lib.concatMap (n: lib.attrsets.mapAttrsToList (k: v: k) ns.${n}) conf.ns))}
132 ${lib.optionalString (conf.withCAA != null) ''
133 ${conf.name}. IN CAA 0 issue "${conf.withCAA}"
134 ''}
135
136 ${conf.entries}
137
138 ${if lib.attrsets.hasAttr "withEmail" conf && lib.lists.length conf.withEmail > 0 then ''
139 ${ip4mxes}
140 ${ip6mxes}
141 ${lib.concatStringsSep "\n\n" (map (e:
142 let
143 n = if e.domain == "" then "@" else "${e.domain} ";
144 suffix = if e.domain == "" then "" else ".${e.domain}";
145 in
146 ''
147 ; ------------------ mail: ${n} ---------------------------
148 ${mxmxes n conf}
149
150 ; https://tools.ietf.org/html/rfc6186
151 _submission._tcp${suffix} SRV 0 1 587 smtp.immae.eu.
152 _submissions._tcp${suffix} SRV 0 1 465 smtp.immae.eu.
153 _imap._tcp${suffix} SRV 0 1 143 imap.immae.eu.
154 _imaps._tcp${suffix} SRV 0 1 993 imap.immae.eu.
155 _pop3._tcp${suffix} SRV 10 1 110 pop3.immae.eu.
156 _pop3s._tcp${suffix} SRV 10 1 995 pop3.immae.eu.
157 _sieve._tcp${suffix} SRV 0 1 4190 imap.immae.eu.
158
159 ; MTA-STS
160 ; https://blog.delouw.ch/2018/12/16/using-mta-sts-to-enhance-email-transport-security-and-privacy/
161 ; https://support.google.com/a/answer/9261504
162 _mta-sts${suffix} IN TXT "v=STSv1;id=20200109150200Z"
163 _smtp._tls${suffix} IN TXT "v=TLSRPTv1;rua=mailto:postmaster+mta-sts@immae.eu"
164 mta-sts${suffix} IN A ${config.myEnv.servers.eldiron.ips.main.ip4}
165 ${builtins.concatStringsSep "\n" (map (i: "mta-sts${suffix} IN AAAA ${i}") config.myEnv.servers.eldiron.ips.main.ip6)}
166
167 ; Mail sender authentications
168 ${n} IN TXT "v=spf1 mx ~all"
169 _dmarc${suffix} IN TXT "v=DMARC1; p=none; adkim=r; aspf=r; fo=1; rua=mailto:postmaster+rua@immae.eu; ruf=mailto:postmaster+ruf@immae.eu;"
170 ${if e.send then ''
171 immae_eu._domainkey${suffix} IN TXT ( "v=DKIM1; k=rsa; s=email; "
172 "p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzl3vLd8W5YAuumC5+ZT9OV7/14Pmh5JYtwyqKI3cfe9NnAqInt3xO4bZ7oqIxRKWN4SD39vm7O/QOvFdBt00ENOOzdP90s5gKw6eIP/4+vPTh0IWltAsmu9B2agzdtWUE7t2xFKIzEn8l9niRE2QYbVaqZv4sub98vY55fIgFoHtjkmNC7325S8fjDJGp6OPbyhAs6Xl5/adjF"
173 "0ko4Y2p6RaxLQfjlS0bxmK4Qg6C14pIXHtzVeqOuWrwApqt5+AULSn97iUtqV/IJlEEjC6DUR44t3C/G0G/k46iFclCqRRi0hdPrOHCtZDbtMubnTN9eaUiNpkXh1WnCflHwtjQwIDAQAB" )
174 eldiron._domainkey${suffix} IN TXT ${config.myEnv.mail.dkim.eldiron.public}
175 '' else ""}
176 '') conf.withEmail)}
177 '' + (if conf.name == "immae.eu" then ''
178 ; ----------------- Accept DMARC reports -------------------
179 ${lib.concatStringsSep "\n" (
180 lib.flatten (
181 map (z: map (e: "${e.domain}${if builtins.stringLength e.domain > 0 then "." else ""}${z.name}._report._dmarc IN TXT \"v=DMARC1;\"") (z.withEmail or [])) masterZones
182 )
183 )}
184 '' else "") else ""}
185 '';
186 }) masterZones);
187 };
188 };
189 }