summaryrefslogtreecommitdiffstats
path: root/configs/knot/dane.nix
blob: dad21fd698e1c22b05034bb1df2aa930aa649b1d (plain)
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
# DANE: TLSA records for ne's certificate, for SMTP and HTTPS.
# systems/ne/acme.nix reloads this when the certificate changes.
{ config, pkgs, ... }: {
  services.knot.settings = {
    acl = {
      dane_acl = {
        key = "dane";
        action = "update";
      };
    };
    template.internet-notify-he.acl = [ "dane_acl" ];
  };

  systemd.services."knsupdate-viljetic.de" = {
    serviceConfig = {
      Type = "oneshot";
      SyslogIdentifier = "knsupdate-viljetic.de";
      ExecStart = pkgs.writeDash "knsupdate-viljetic.de" /* sh */ ''
        set -efu

        mk_certificate_association_data() {
          ${pkgs.openssl}/bin/openssl x509 -noout -fingerprint -sha256 < "$1" |
          ${pkgs.coreutils}/bin/cut -d= -f2 |
          ${pkgs.coreutils}/bin/tr -d :
        }

        certfile=/var/lib/acme/viljetic.de/cert.pem
        certificate_association_data=$(mk_certificate_association_data "$certfile")
        keyfile=${config.krebs.secret.directory}/dane.tsig

        script=$(${pkgs.coreutils}/bin/mktemp -t knsupdate.XXXXXXXX)
        trap 'rm "$script"' EXIT
        (
          exec >"$script"
          echo server viljetic.de.
          echo zone viljetic.de.
          echo origin viljetic.de.
          echo del _25._tcp.ne TLSA
          echo del _443._tcp.ne TLSA
          echo add _25._tcp.ne 60 IN TLSA 3 0 1 $certificate_association_data
          echo add _443._tcp.ne 60 IN TLSA 3 0 1 $certificate_association_data
          echo show
          echo send
          echo answer
          echo quit
        )
        ${pkgs.knot-dns}/bin/knsupdate -k "$keyfile" "$script"
      '';
    };
  };
}