aboutsummaryrefslogtreecommitdiff
path: root/modules/private/databases/openldap_replication.nix
blob: 350eecfc2a02a41de3d02b102dc6e72473a4acac (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
{ pkgs, config, lib, ... }:
let
  cfg = config.myServices.databasesReplication.openldap;
  eldiron_schemas = pkgs.callPackage ./openldap/eldiron_schemas.nix {};
  ldapConfig = hcfg: name: pkgs.writeText "slapd.conf" ''
    include ${pkgs.openldap}/etc/schema/core.schema
    include ${pkgs.openldap}/etc/schema/cosine.schema
    include ${pkgs.openldap}/etc/schema/inetorgperson.schema
    include ${pkgs.openldap}/etc/schema/nis.schema
    ${eldiron_schemas}
    pidfile   /run/slapd_${name}/slapd.pid
    argsfile  /run/slapd_${name}/slapd.args

    moduleload  back_hdb
    backend     hdb
    database    hdb

    suffix    "${hcfg.base}"
    rootdn    "cn=root,${hcfg.base}"
    directory ${cfg.base}/${name}/openldap

    index   objectClass       eq
    index   uid               pres,eq
    index   entryUUID         eq

    include ${config.secrets.fullPaths."openldap_replication/${name}/replication_config"}
    '';
in
{
  options.myServices.databasesReplication.openldap = {
    enable = lib.mkEnableOption "Enable openldap replication";
    base = lib.mkOption {
      type = lib.types.path;
      description = ''
        Base path to put the replications
        '';
    };
    hosts = lib.mkOption {
      default = {};
      description = ''
        Hosts to backup
        '';
      type = lib.types.attrsOf (lib.types.submodule {
        options = {
          package = lib.mkOption {
            type = lib.types.package;
            default = pkgs.openldap;
            description = ''
              Openldap package for this host
            '';
          };
          url = lib.mkOption {
            type = lib.types.str;
            description = ''
              Host to connect to
              '';
          };
          base = lib.mkOption {
            type = lib.types.str;
            description = ''
              Base DN to replicate
              '';
          };
          dn = lib.mkOption {
            type = lib.types.str;
            description = ''
              DN to use
              '';
          };
          password = lib.mkOption {
            type = lib.types.str;
            description = ''
              Password to use
              '';
          };
        };
      });
    };
  };

  config = lib.mkIf cfg.enable {
    users.users.openldap = {
      description = "Openldap database user";
      group = "openldap";
      uid = config.ids.uids.openldap;
      extraGroups = [ "keys" ];
    };
    users.groups.openldap.gid = config.ids.gids.openldap;

    secrets.keys = lib.flatten (lib.mapAttrsToList (name: hcfg: [
      {
        dest = "openldap_replication/${name}/replication_config";
        user = "openldap";
        group = "openldap";
        permissions = "0400";
        text = ''
          syncrepl rid=000
                  provider=${hcfg.url}
                  type=refreshAndPersist
                  searchbase="${hcfg.base}"
                  retry="5 10 300 +"
                  attrs="*,+"
                  schemachecking=off
                  bindmethod=simple
                  binddn="${hcfg.dn}"
                  credentials="${hcfg.password}"
          '';
      }
      {
        dest = "openldap_replication/${name}/replication_password";
        user = "openldap";
        group = "openldap";
        permissions = "0400";
        text = hcfg.password;
      }
    ]) cfg.hosts);

    services.cron = {
      enable = true;
      systemCronJobs = lib.flatten (lib.mapAttrsToList (name: hcfg:
        let
          dataDir = "${cfg.base}/${name}/openldap";
          backupDir = "${cfg.base}/${name}/openldap_backup";
          backup_script = pkgs.writeScript "backup_openldap_${name}" ''
              #!${pkgs.stdenv.shell}

              ${hcfg.package}/bin/slapcat -b "${hcfg.base}" -f ${ldapConfig hcfg name} -l ${backupDir}/$(${pkgs.coreutils}/bin/date -Iminutes).ldif
            '';
          u = pkgs.callPackage ./utils.nix {};
          cleanup_script = pkgs.writeScript "cleanup_openldap_${name}" (u.exponentialDumps "ldif" backupDir);
        in [
          "0 22,4,10,16 * * * root ${backup_script}"
          "0 3 * * * root ${cleanup_script}"
        ]) cfg.hosts);
    };

    system.activationScripts = lib.attrsets.mapAttrs' (name: hcfg:
      lib.attrsets.nameValuePair "openldap_replication_${name}" {
        deps = [ "users" "groups" ];
        text = ''
          install -m 0700 -o openldap -g openldap -d ${cfg.base}/${name}/openldap
          install -m 0700 -o openldap -g openldap -d ${cfg.base}/${name}/openldap_backup
          '';
      }) cfg.hosts;

    systemd.services = lib.attrsets.mapAttrs' (name: hcfg:
      let
        dataDir = "${cfg.base}/${name}/openldap";
      in
      lib.attrsets.nameValuePair "openldap_backup_${name}" {
        description = "Openldap replication for ${name}";
        wantedBy = [ "multi-user.target" ];
        after = [ "network.target" ];
        unitConfig.RequiresMountsFor = dataDir;

        preStart = ''
          mkdir -p /run/slapd_${name}
          chown -R "openldap:openldap" /run/slapd_${name}
        '';

        serviceConfig = {
          ExecStart = "${hcfg.package}/libexec/slapd -d 0 -u openldap -g openldap -f ${ldapConfig hcfg name}";
        };
    }) cfg.hosts;
  };
}