]>
Commit | Line | Data |
---|---|---|
ab8f306d | 1 | { pkgs, config, lib, ... }: |
16b80abd IB |
2 | let |
3 | cfg = config.myServices.databasesReplication.openldap; | |
4 | eldiron_schemas = pkgs.callPackage ./openldap/eldiron_schemas.nix {}; | |
5 | ldapConfig = hcfg: name: pkgs.writeText "slapd.conf" '' | |
5400b9b6 IB |
6 | include ${pkgs.openldap}/etc/schema/core.schema |
7 | include ${pkgs.openldap}/etc/schema/cosine.schema | |
8 | include ${pkgs.openldap}/etc/schema/inetorgperson.schema | |
9 | include ${pkgs.openldap}/etc/schema/nis.schema | |
16b80abd IB |
10 | ${eldiron_schemas} |
11 | pidfile /run/slapd_${name}/slapd.pid | |
12 | argsfile /run/slapd_${name}/slapd.args | |
13 | ||
14 | moduleload back_hdb | |
15 | backend hdb | |
16 | database hdb | |
17 | ||
18 | suffix "${hcfg.base}" | |
19 | rootdn "cn=root,${hcfg.base}" | |
20 | directory ${cfg.base}/${name}/openldap | |
21 | ||
22 | index objectClass eq | |
23 | index uid pres,eq | |
24 | index entryUUID eq | |
25 | ||
26 | include ${config.secrets.location}/openldap_replication/${name}/replication_config | |
27 | ''; | |
28 | in | |
29 | { | |
30 | options.myServices.databasesReplication.openldap = { | |
31 | enable = lib.mkEnableOption "Enable openldap replication"; | |
32 | base = lib.mkOption { | |
33 | type = lib.types.path; | |
34 | description = '' | |
35 | Base path to put the replications | |
36 | ''; | |
37 | }; | |
38 | hosts = lib.mkOption { | |
39 | default = {}; | |
40 | description = '' | |
41 | Hosts to backup | |
42 | ''; | |
43 | type = lib.types.attrsOf (lib.types.submodule { | |
44 | options = { | |
45 | package = lib.mkOption { | |
46 | type = lib.types.package; | |
47 | default = pkgs.openldap; | |
48 | description = '' | |
49 | Openldap package for this host | |
50 | ''; | |
51 | }; | |
52 | url = lib.mkOption { | |
53 | type = lib.types.str; | |
54 | description = '' | |
55 | Host to connect to | |
56 | ''; | |
57 | }; | |
58 | base = lib.mkOption { | |
59 | type = lib.types.str; | |
60 | description = '' | |
61 | Base DN to replicate | |
62 | ''; | |
63 | }; | |
64 | dn = lib.mkOption { | |
65 | type = lib.types.str; | |
66 | description = '' | |
67 | DN to use | |
68 | ''; | |
69 | }; | |
70 | password = lib.mkOption { | |
71 | type = lib.types.str; | |
72 | description = '' | |
73 | Password to use | |
74 | ''; | |
75 | }; | |
76 | }; | |
77 | }); | |
78 | }; | |
79 | }; | |
80 | ||
81 | config = lib.mkIf cfg.enable { | |
82 | users.users.openldap = { | |
83 | description = "Openldap database user"; | |
84 | group = "openldap"; | |
85 | uid = config.ids.uids.openldap; | |
86 | extraGroups = [ "keys" ]; | |
87 | }; | |
88 | users.groups.openldap.gid = config.ids.gids.openldap; | |
89 | ||
90 | secrets.keys = lib.flatten (lib.mapAttrsToList (name: hcfg: [ | |
91 | { | |
92 | dest = "openldap_replication/${name}/replication_config"; | |
93 | user = "openldap"; | |
94 | group = "openldap"; | |
95 | permissions = "0400"; | |
96 | text = '' | |
97 | syncrepl rid=000 | |
98 | provider=${hcfg.url} | |
99 | type=refreshAndPersist | |
100 | searchbase="${hcfg.base}" | |
101 | retry="5 10 300 +" | |
102 | attrs="*,+" | |
103 | schemachecking=off | |
104 | bindmethod=simple | |
105 | binddn="${hcfg.dn}" | |
106 | credentials="${hcfg.password}" | |
107 | ''; | |
108 | } | |
109 | { | |
110 | dest = "openldap_replication/${name}/replication_password"; | |
111 | user = "openldap"; | |
112 | group = "openldap"; | |
113 | permissions = "0400"; | |
114 | text = hcfg.password; | |
115 | } | |
116 | ]) cfg.hosts); | |
117 | ||
118 | services.cron = { | |
119 | enable = true; | |
120 | systemCronJobs = lib.flatten (lib.mapAttrsToList (name: hcfg: | |
121 | let | |
122 | dataDir = "${cfg.base}/${name}/openldap"; | |
123 | backupDir = "${cfg.base}/${name}/openldap_backup"; | |
124 | backup_script = pkgs.writeScript "backup_openldap_${name}" '' | |
125 | #!${pkgs.stdenv.shell} | |
126 | ||
4c853ba6 | 127 | ${hcfg.package}/bin/slapcat -b "${hcfg.base}" -f ${ldapConfig hcfg name} -l ${backupDir}/$(${pkgs.coreutils}/bin/date -Iminutes).ldif |
16b80abd IB |
128 | ''; |
129 | u = pkgs.callPackage ./utils.nix {}; | |
130 | cleanup_script = pkgs.writeScript "cleanup_openldap_${name}" (u.exponentialDumps "ldif" backupDir); | |
131 | in [ | |
132 | "0 22,4,10,16 * * * root ${backup_script}" | |
133 | "0 3 * * * root ${cleanup_script}" | |
134 | ]) cfg.hosts); | |
135 | }; | |
136 | ||
137 | system.activationScripts = lib.attrsets.mapAttrs' (name: hcfg: | |
138 | lib.attrsets.nameValuePair "openldap_replication_${name}" { | |
139 | deps = [ "users" "groups" ]; | |
140 | text = '' | |
141 | install -m 0700 -o openldap -g openldap -d ${cfg.base}/${name}/openldap | |
142 | install -m 0700 -o openldap -g openldap -d ${cfg.base}/${name}/openldap_backup | |
143 | ''; | |
144 | }) cfg.hosts; | |
145 | ||
146 | systemd.services = lib.attrsets.mapAttrs' (name: hcfg: | |
147 | let | |
148 | dataDir = "${cfg.base}/${name}/openldap"; | |
149 | in | |
150 | lib.attrsets.nameValuePair "openldap_backup_${name}" { | |
151 | description = "Openldap replication for ${name}"; | |
152 | wantedBy = [ "multi-user.target" ]; | |
153 | after = [ "network.target" ]; | |
154 | unitConfig.RequiresMountsFor = dataDir; | |
155 | ||
156 | preStart = '' | |
157 | mkdir -p /run/slapd_${name} | |
158 | chown -R "openldap:openldap" /run/slapd_${name} | |
159 | ''; | |
160 | ||
161 | serviceConfig = { | |
162 | ExecStart = "${hcfg.package}/libexec/slapd -d 0 -u openldap -g openldap -f ${ldapConfig hcfg name}"; | |
163 | }; | |
164 | }) cfg.hosts; | |
165 | }; | |
166 | } | |
167 | ||
168 |