2023-01-25 11:48:44 +00:00
|
|
|
{ lib, pkgs, config, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.skynet_dns;
|
2023-04-23 12:37:42 +00:00
|
|
|
|
2023-04-29 01:35:58 +00:00
|
|
|
# reads that date to a string (will need to be fixed in 2038)
|
|
|
|
current_date = toString builtins.currentTime;
|
2023-04-29 00:54:17 +00:00
|
|
|
|
2023-05-21 19:22:54 +00:00
|
|
|
# base config for domains we own (skynet.ie, csn.ul.ie, ulcompsoc.ie)
|
2023-05-21 15:18:39 +00:00
|
|
|
get_config_file = (domain:
|
|
|
|
''
|
|
|
|
$TTL 60 ; 1 minute
|
|
|
|
; hostmaster@${domain} is an email address that recieves stuff related to dns
|
|
|
|
@ IN SOA ${cfg.own.nameserver}.${domain}. hostmaster.${domain}. (
|
|
|
|
; Serial (YYYYMMDDCC) this has to be updated for each time the record is updated
|
|
|
|
${current_date}
|
|
|
|
600 ; Refresh (10 minutes)
|
|
|
|
300 ; Retry (5 minutes)
|
|
|
|
604800 ; Expire (1 week)
|
|
|
|
3600 ; Minimum (1 hour)
|
|
|
|
)
|
|
|
|
|
|
|
|
@ NS ns1.${domain}.
|
|
|
|
@ NS ns2.${domain}.
|
|
|
|
; @ stands for teh root domain so teh A record below is where ${domain} points to
|
2023-05-21 18:06:30 +00:00
|
|
|
;@ A 193.1.99.76
|
2023-05-21 15:18:39 +00:00
|
|
|
;@ MX 5 ${domain}.
|
|
|
|
|
|
|
|
; can have multiple mailserves
|
|
|
|
;@ MX 20 mail2.${domain}.
|
|
|
|
|
|
|
|
|
|
|
|
; ------------------------------------------
|
|
|
|
; Server Names
|
|
|
|
; ------------------------------------------
|
|
|
|
|
|
|
|
; External addresses
|
|
|
|
; ------------------------------------------
|
|
|
|
${lib.strings.concatMapStrings (x: x + "\n") cfg.records.external}
|
|
|
|
|
|
|
|
|
|
|
|
; this is fixed for now
|
|
|
|
wintermute A 193.1.101.148
|
|
|
|
|
|
|
|
|
|
|
|
; internal addresses
|
|
|
|
; ------------------------------------------
|
|
|
|
; May come back to this idea in teh future
|
|
|
|
; agentjones.int A 172.20.20.1
|
|
|
|
|
|
|
|
|
|
|
|
; cname's
|
|
|
|
; ------------------------------------------
|
|
|
|
${lib.strings.concatMapStrings (x: x + "\n") cfg.records.cname}
|
|
|
|
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
2023-05-21 18:06:30 +00:00
|
|
|
|
|
|
|
# https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/4/html/reference_guide/s2-bind-configuration-zone-reverse
|
2023-05-21 19:22:54 +00:00
|
|
|
# config for our reverse dnspointers (not properly working)
|
2023-05-21 18:06:30 +00:00
|
|
|
get_config_file_rev = (domain:
|
|
|
|
''
|
|
|
|
$ORIGIN 99.1.193.in-addr.arpa.
|
|
|
|
$TTL 60 ; 1 minute
|
|
|
|
; hostmaster@${domain} is an email address that recieves stuff related to dns
|
|
|
|
@ IN SOA ${cfg.own.nameserver}.${domain}. hostmaster.${domain}. (
|
|
|
|
; Serial (YYYYMMDDCC) this has to be updated for each time the record is updated
|
|
|
|
${current_date}
|
|
|
|
600 ; Refresh (10 minutes)
|
|
|
|
300 ; Retry (5 minutes)
|
|
|
|
604800 ; Expire (1 week)
|
|
|
|
3600 ; Minimum (1 hour)
|
|
|
|
)
|
|
|
|
|
|
|
|
@ NS ns1.${domain}.
|
|
|
|
@ NS ns2.${domain}.
|
|
|
|
|
|
|
|
${lib.strings.concatMapStrings (x: x + "." + domain + ".\n") cfg.records.reverse}
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
2023-05-21 19:22:54 +00:00
|
|
|
# domains we dont have proper ownship over, only here to ensure the logs dont get cluttered.
|
2023-05-21 18:06:30 +00:00
|
|
|
get_config_file_old_domains = (domain:
|
|
|
|
''
|
|
|
|
$TTL 60 ; 1 minute
|
|
|
|
; hostmaster@skynet.ie is an email address that recieves stuff related to dns
|
|
|
|
@ IN SOA ${cfg.own.nameserver}.skynet.ie. hostmaster.skynet.ie. (
|
|
|
|
; Serial (YYYYMMDDCC) this has to be updated for each time the record is updated
|
|
|
|
${current_date}
|
|
|
|
600 ; Refresh (10 minutes)
|
|
|
|
300 ; Retry (5 minutes)
|
|
|
|
604800 ; Expire (1 week)
|
|
|
|
3600 ; Minimum (1 hour)
|
|
|
|
)
|
|
|
|
|
|
|
|
@ NS ns1.skynet.ie.
|
|
|
|
@ NS ns2.skynet.ie.
|
|
|
|
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
2023-05-21 18:38:13 +00:00
|
|
|
# arrys of teh two nameservers
|
|
|
|
tmp1 = ["193.1.99.109"];
|
|
|
|
tmp2 = ["193.1.99.120"];
|
|
|
|
|
|
|
|
primaries = (if cfg.primary then
|
|
|
|
# primary servers have no primaries (ones they listen to)
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
if builtins.elem cfg.own.ip tmp1 then
|
|
|
|
tmp2
|
|
|
|
else
|
|
|
|
tmp1
|
|
|
|
);
|
|
|
|
|
|
|
|
secondaries = (if cfg.primary then
|
|
|
|
if builtins.elem cfg.own.ip tmp1 then
|
|
|
|
tmp2
|
|
|
|
else
|
|
|
|
tmp1
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2023-05-21 19:22:54 +00:00
|
|
|
# small function to tidy up the spam of the cache networks, would use teh subnet except all external traffic has the ip of teh router
|
|
|
|
create_cache_networks = (map (x: "193.1.99.${toString x}/32" ) (lib.lists.range 71 126) );
|
2023-05-21 18:38:13 +00:00
|
|
|
|
2023-05-21 20:25:21 +00:00
|
|
|
|
2023-05-21 20:30:14 +00:00
|
|
|
# standard function to create the etc file, pass in the text and domain and it makes it
|
|
|
|
create_entry_etc_sub = domain: text: {
|
|
|
|
# Creates /etc/skynet/dns/domain
|
2023-05-21 20:25:21 +00:00
|
|
|
"skynet/dns/${domain}" = {
|
|
|
|
user = "named";
|
|
|
|
group = "named";
|
|
|
|
|
|
|
|
# The UNIX file mode bits
|
2023-05-21 20:51:17 +00:00
|
|
|
mode = "0664";
|
2023-05-21 20:25:21 +00:00
|
|
|
|
|
|
|
text = text;
|
|
|
|
};
|
|
|
|
};
|
2023-05-21 20:30:14 +00:00
|
|
|
# (text.owned "csn.ul.ie")
|
|
|
|
|
|
|
|
|
|
|
|
# standard function to create the etc file, pass in the text and domain and it makes it
|
|
|
|
create_entry_etc = domain: type:
|
|
|
|
if type == "owned" then
|
2023-05-21 20:48:30 +00:00
|
|
|
create_entry_etc_sub domain (text.owned domain)
|
|
|
|
else if type == "reverse" then
|
|
|
|
create_entry_etc_sub domain (text.reverse domain)
|
|
|
|
else if type == "old" then
|
|
|
|
create_entry_etc_sub domain (text.old domain)
|
2023-05-21 20:30:14 +00:00
|
|
|
else
|
2023-05-21 20:48:30 +00:00
|
|
|
{};
|
2023-05-21 20:25:21 +00:00
|
|
|
|
|
|
|
create_entry_zone = (domain: extraConfig: {
|
|
|
|
"${domain}" = {
|
|
|
|
extraConfig = ''
|
|
|
|
${extraConfig}
|
|
|
|
// for bumping the config
|
|
|
|
// ${current_date}
|
|
|
|
'';
|
|
|
|
# really wish teh nixos config didnt use master/slave
|
|
|
|
master = cfg.primary;
|
|
|
|
masters = primaries;
|
|
|
|
slaves = secondaries;
|
|
|
|
# need to write this to a file
|
|
|
|
# using the date in it so it will trigger a restart
|
|
|
|
file = "/etc/skynet/dns/${domain}";
|
|
|
|
# no leading whitespace for first line
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
text = {
|
|
|
|
owned = domain: get_config_file domain;
|
2023-05-21 20:48:30 +00:00
|
|
|
reverse = domain: get_config_file_rev domain;
|
|
|
|
old = domain: get_config_file_old_domains domain;
|
2023-05-21 20:25:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = {
|
|
|
|
owned =
|
|
|
|
if cfg.primary then
|
|
|
|
''
|
|
|
|
allow-update { key rfc2136key.skynet.ie.; };
|
|
|
|
|
|
|
|
dnssec-policy default;
|
|
|
|
inline-signing yes;
|
|
|
|
''
|
|
|
|
else
|
|
|
|
"";
|
|
|
|
|
2023-05-21 20:48:30 +00:00
|
|
|
# no extra config for reverse
|
|
|
|
reverse = "";
|
|
|
|
|
|
|
|
old = "";
|
2023-05-21 20:25:21 +00:00
|
|
|
};
|
|
|
|
|
2023-01-25 11:48:44 +00:00
|
|
|
in {
|
2023-05-24 15:12:48 +00:00
|
|
|
|
|
|
|
imports = [
|
|
|
|
../applications/firewall.nix
|
|
|
|
];
|
|
|
|
|
2023-01-25 11:48:44 +00:00
|
|
|
options = {
|
|
|
|
skynet_dns = {
|
|
|
|
enable = lib.mkEnableOption {
|
|
|
|
default = false;
|
|
|
|
example = true;
|
|
|
|
description = "Skynet DNS";
|
|
|
|
type = lib.types.bool;
|
|
|
|
};
|
|
|
|
|
2023-05-21 18:38:13 +00:00
|
|
|
primary = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
2023-01-25 11:48:44 +00:00
|
|
|
own = {
|
2023-05-21 18:38:13 +00:00
|
|
|
ip = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = ''
|
|
|
|
ip of this server
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-01-25 11:48:44 +00:00
|
|
|
nameserver = lib.mkOption {
|
|
|
|
default = "ns1";
|
|
|
|
type = lib.types.str;
|
|
|
|
description = ''
|
|
|
|
the hostname of this nameserver, eg ns1, ns2
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
external = lib.mkOption {
|
|
|
|
default = [ ];
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
description = ''
|
|
|
|
External records like: agentjones A 193.1.99.72
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
cname = lib.mkOption {
|
|
|
|
default = [ ];
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
description = ''
|
|
|
|
External records like: ns1 CNAME ns1
|
|
|
|
'';
|
|
|
|
};
|
2023-05-21 18:06:30 +00:00
|
|
|
|
|
|
|
reverse = lib.mkOption {
|
|
|
|
default = [ ];
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
description = ''
|
|
|
|
External records like: 20 IN PTR vigil
|
|
|
|
'';
|
|
|
|
};
|
2023-01-25 11:48:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
records = {
|
|
|
|
external = lib.mkOption {
|
|
|
|
default = [ ];
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
description = ''
|
|
|
|
External records like: agentjones A 193.1.99.72
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
cname = lib.mkOption {
|
|
|
|
default = [ ];
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
description = ''
|
|
|
|
External records like: ns1 CNAME ns1
|
|
|
|
'';
|
|
|
|
};
|
2023-05-21 18:06:30 +00:00
|
|
|
|
|
|
|
reverse = lib.mkOption {
|
|
|
|
default = [ ];
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
description = ''
|
|
|
|
External records like: 20 IN PTR vigil
|
|
|
|
'';
|
|
|
|
};
|
2023-01-25 11:48:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
2023-05-21 20:48:30 +00:00
|
|
|
|
2023-05-24 15:12:48 +00:00
|
|
|
# open the firewall for this
|
|
|
|
skynet_firewall.forward = [
|
|
|
|
"ip daddr ${cfg.own.ip} tcp dport 53 counter packets 0 bytes 0 accept"
|
|
|
|
"ip daddr ${cfg.own.ip} udp dport 53 counter packets 0 bytes 0 accept"
|
|
|
|
];
|
|
|
|
|
2023-05-21 20:48:30 +00:00
|
|
|
services.bind.zones =
|
|
|
|
(create_entry_zone "csn.ul.ie" extraConfig.owned ) //
|
2023-06-15 21:50:29 +00:00
|
|
|
(create_entry_zone "skynet.ie" extraConfig.owned ) //
|
|
|
|
(create_entry_zone "ulcompsoc.ie" extraConfig.owned ) //
|
2023-05-21 20:48:30 +00:00
|
|
|
|
|
|
|
(create_entry_zone "99.1.193.in-addr.arpa" extraConfig.reverse )//
|
|
|
|
|
|
|
|
(create_entry_zone "conradcollins.net" extraConfig.old )//
|
|
|
|
(create_entry_zone "edelharty.net" extraConfig.old );
|
|
|
|
|
|
|
|
environment.etc =
|
|
|
|
(create_entry_etc "csn.ul.ie" "owned") //
|
|
|
|
(create_entry_etc "skynet.ie" "owned") //
|
2023-06-15 21:50:29 +00:00
|
|
|
(create_entry_etc "ulcompsoc.ie" "owned") //
|
2023-05-21 20:48:30 +00:00
|
|
|
|
|
|
|
(create_entry_etc "99.1.193.in-addr.arpa" "reverse") //
|
|
|
|
|
|
|
|
(create_entry_etc "conradcollins.net" "old") //
|
|
|
|
(create_entry_etc "edelharty.net" "old");
|
|
|
|
|
|
|
|
|
2023-04-20 18:22:17 +00:00
|
|
|
# secrets required
|
2023-04-20 22:10:47 +00:00
|
|
|
age.secrets.dns_dnskeys = {
|
|
|
|
file = ../secrets/dns_dnskeys.conf.age;
|
|
|
|
owner = "named";
|
|
|
|
group = "named";
|
|
|
|
};
|
2023-04-20 18:22:17 +00:00
|
|
|
|
2023-04-20 23:53:25 +00:00
|
|
|
networking.firewall = {
|
|
|
|
allowedTCPPorts = [53];
|
|
|
|
allowedUDPPorts = [53];
|
|
|
|
};
|
|
|
|
|
2023-01-25 11:48:44 +00:00
|
|
|
services.bind = {
|
2023-04-23 03:22:01 +00:00
|
|
|
enable = true;
|
|
|
|
|
|
|
|
ipv4Only = true;
|
|
|
|
|
|
|
|
# need to take a look at https://nixos.org/manual/nixos/unstable/#module-security-acme-config-dns
|
|
|
|
extraConfig = ''
|
|
|
|
include "/run/agenix/dns_dnskeys";
|
|
|
|
'';
|
|
|
|
|
2023-04-24 19:17:24 +00:00
|
|
|
# piles of no valid RRSIG resolving 'com/DS/IN' errors
|
2023-04-25 14:11:02 +00:00
|
|
|
extraOptions = ''
|
|
|
|
dnssec-validation yes;
|
|
|
|
'';
|
2023-04-24 19:17:24 +00:00
|
|
|
|
2023-04-23 03:22:01 +00:00
|
|
|
# set the upstream dns servers
|
|
|
|
# overrides the default dns servers
|
|
|
|
forwarders = [
|
|
|
|
# Cloudflare
|
|
|
|
"1.1.1.1"
|
|
|
|
# Google
|
|
|
|
"8.8.8.8"
|
|
|
|
# Quad9
|
|
|
|
"9.9.9.9"
|
|
|
|
];
|
|
|
|
|
|
|
|
cacheNetworks = [
|
|
|
|
# this server itself
|
|
|
|
"127.0.0.0/24"
|
|
|
|
# all of skynet can use this as a resolver
|
2023-05-05 13:40:27 +00:00
|
|
|
/*
|
|
|
|
Origianl idea, however all external traffic had the ip of the router
|
2023-04-23 03:22:01 +00:00
|
|
|
"193.1.99.64/26"
|
2023-05-05 13:40:27 +00:00
|
|
|
|
|
|
|
So to fix this we need to allow smaller ranges? - Didnt work
|
|
|
|
Fallback is explisitly listing each ip we have
|
|
|
|
|
2023-05-21 19:22:54 +00:00
|
|
|
Now have a function for it
|
|
|
|
*/
|
|
|
|
] ++ create_cache_networks;
|
2023-04-23 03:22:01 +00:00
|
|
|
};
|
2023-01-25 11:48:44 +00:00
|
|
|
|
2023-04-23 03:22:01 +00:00
|
|
|
# creates a folder in /etc for the dns to use
|
|
|
|
users.users.named = {
|
|
|
|
createHome = true;
|
2023-05-21 20:25:21 +00:00
|
|
|
home = "/etc/skynet/dns";
|
2023-04-23 03:22:01 +00:00
|
|
|
};
|
2023-01-25 11:48:44 +00:00
|
|
|
|
2023-04-23 03:22:01 +00:00
|
|
|
};
|
2023-01-17 22:40:04 +00:00
|
|
|
}
|