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