2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2011-04-13 17:35:19 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2011-04-13 17:35:19 +00:00
|
|
|
let
|
|
|
|
cfg = config.services.openldap;
|
2020-07-01 18:11:00 +00:00
|
|
|
openldap = cfg.package;
|
2020-08-23 23:19:35 +00:00
|
|
|
configDir = if cfg.configDir != null then cfg.configDir else "/etc/openldap/slapd.d";
|
2011-04-13 17:35:19 +00:00
|
|
|
|
2020-08-02 22:52:37 +00:00
|
|
|
ldapValueType = let
|
2020-09-27 17:03:40 +00:00
|
|
|
# Can't do types.either with multiple non-overlapping submodules, so define our own
|
|
|
|
singleLdapValueType = lib.mkOptionType rec {
|
|
|
|
name = "LDAP";
|
|
|
|
description = "LDAP value";
|
2020-09-27 20:50:25 +00:00
|
|
|
check = x: lib.isString x || (lib.isAttrs x && (x ? path || x ? base64));
|
2020-09-27 17:03:40 +00:00
|
|
|
merge = lib.mergeEqualOption;
|
|
|
|
};
|
|
|
|
# We don't coerce to lists of single values, as some values must be unique
|
2020-08-02 22:52:37 +00:00
|
|
|
in types.either singleLdapValueType (types.listOf singleLdapValueType);
|
|
|
|
|
|
|
|
ldapAttrsType =
|
|
|
|
let
|
|
|
|
options = {
|
|
|
|
attrs = mkOption {
|
|
|
|
type = types.attrsOf ldapValueType;
|
|
|
|
default = {};
|
|
|
|
description = "Attributes of the parent entry.";
|
|
|
|
};
|
|
|
|
children = mkOption {
|
|
|
|
# Hide the child attributes, to avoid infinite recursion in e.g. documentation
|
|
|
|
# Actual Nix evaluation is lazy, so this is not an issue there
|
|
|
|
type = let
|
|
|
|
hiddenOptions = lib.mapAttrs (name: attr: attr // { visible = false; }) options;
|
|
|
|
in types.attrsOf (types.submodule { options = hiddenOptions; });
|
|
|
|
default = {};
|
|
|
|
description = "Child entries of the current entry, with recursively the same structure.";
|
2021-10-03 16:06:03 +00:00
|
|
|
example = lib.literalExpression ''
|
2020-09-27 16:10:13 +00:00
|
|
|
{
|
|
|
|
"cn=schema" = {
|
|
|
|
# The attribute used in the DN must be defined
|
|
|
|
attrs = { cn = "schema"; };
|
|
|
|
children = {
|
|
|
|
# This entry's DN is expanded to "cn=foo,cn=schema"
|
|
|
|
"cn=foo" = { ... };
|
|
|
|
};
|
|
|
|
# These includes are inserted after "cn=schema", but before "cn=foo,cn=schema"
|
|
|
|
includes = [ ... ];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
2020-08-02 22:52:37 +00:00
|
|
|
};
|
|
|
|
includes = mkOption {
|
|
|
|
type = types.listOf types.path;
|
|
|
|
default = [];
|
|
|
|
description = ''
|
2020-09-27 16:10:13 +00:00
|
|
|
LDIF files to include after the parent's attributes but before its children.
|
|
|
|
'';
|
2020-08-02 22:52:37 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
in types.submodule { inherit options; };
|
|
|
|
|
|
|
|
valueToLdif = attr: values: let
|
2020-09-13 21:42:14 +00:00
|
|
|
listValues = if lib.isList values then values else lib.singleton values;
|
|
|
|
in map (value:
|
|
|
|
if lib.isAttrs value then
|
|
|
|
if lib.hasAttr "path" value
|
|
|
|
then "${attr}:< file://${value.path}"
|
|
|
|
else "${attr}:: ${value.base64}"
|
|
|
|
else "${attr}: ${lib.replaceStrings [ "\n" ] [ "\n " ] value}"
|
|
|
|
) listValues;
|
2020-08-02 22:52:37 +00:00
|
|
|
|
|
|
|
attrsToLdif = dn: { attrs, children, includes, ... }: [''
|
|
|
|
dn: ${dn}
|
|
|
|
${lib.concatStringsSep "\n" (lib.flatten (lib.mapAttrsToList valueToLdif attrs))}
|
|
|
|
''] ++ (map (path: "include: file://${path}\n") includes) ++ (
|
|
|
|
lib.flatten (lib.mapAttrsToList (name: value: attrsToLdif "${name},${dn}" value) children)
|
|
|
|
);
|
|
|
|
in {
|
2011-04-13 17:35:19 +00:00
|
|
|
options = {
|
|
|
|
services.openldap = {
|
|
|
|
enable = mkOption {
|
2015-01-26 08:35:56 +00:00
|
|
|
type = types.bool;
|
2011-04-13 17:35:19 +00:00
|
|
|
default = false;
|
|
|
|
description = "
|
|
|
|
Whether to enable the ldap server.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2020-07-01 18:11:00 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.openldap;
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression "pkgs.openldap";
|
2020-07-01 18:11:00 +00:00
|
|
|
description = ''
|
|
|
|
OpenLDAP package to use.
|
|
|
|
|
|
|
|
This can be used to, for example, set an OpenLDAP package
|
|
|
|
with custom overrides to enable modules or other
|
|
|
|
functionality.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-11-28 21:21:50 +00:00
|
|
|
user = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.str;
|
2013-11-28 21:21:50 +00:00
|
|
|
default = "openldap";
|
|
|
|
description = "User account under which slapd runs.";
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.str;
|
2013-11-28 21:21:50 +00:00
|
|
|
default = "openldap";
|
|
|
|
description = "Group account under which slapd runs.";
|
|
|
|
};
|
|
|
|
|
2016-05-21 11:49:14 +00:00
|
|
|
urlList = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.listOf types.str;
|
2016-05-21 11:49:14 +00:00
|
|
|
default = [ "ldap:///" ];
|
|
|
|
description = "URL list slapd should listen on.";
|
|
|
|
example = [ "ldaps:///" ];
|
|
|
|
};
|
|
|
|
|
2020-08-02 22:52:37 +00:00
|
|
|
settings = mkOption {
|
|
|
|
type = ldapAttrsType;
|
|
|
|
description = "Configuration for OpenLDAP, in OLC format";
|
2021-10-03 16:06:03 +00:00
|
|
|
example = lib.literalExpression ''
|
2020-08-02 22:52:37 +00:00
|
|
|
{
|
|
|
|
attrs.olcLogLevel = [ "stats" ];
|
|
|
|
children = {
|
|
|
|
"cn=schema".includes = [
|
2021-10-03 16:06:03 +00:00
|
|
|
"''${pkgs.openldap}/etc/schema/core.ldif"
|
|
|
|
"''${pkgs.openldap}/etc/schema/cosine.ldif"
|
|
|
|
"''${pkgs.openldap}/etc/schema/inetorgperson.ldif"
|
2020-08-02 22:52:37 +00:00
|
|
|
];
|
|
|
|
"olcDatabase={-1}frontend" = {
|
|
|
|
attrs = {
|
|
|
|
objectClass = "olcDatabaseConfig";
|
|
|
|
olcDatabase = "{-1}frontend";
|
|
|
|
olcAccess = [ "{0}to * by dn.exact=uidNumber=0+gidNumber=0,cn=peercred,cn=external,cn=auth manage stop by * none stop" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
"olcDatabase={0}config" = {
|
|
|
|
attrs = {
|
|
|
|
objectClass = "olcDatabaseConfig";
|
|
|
|
olcDatabase = "{0}config";
|
|
|
|
olcAccess = [ "{0}to * by * none break" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
"olcDatabase={1}mdb" = {
|
|
|
|
attrs = {
|
|
|
|
objectClass = [ "olcDatabaseConfig" "olcMdbConfig" ];
|
|
|
|
olcDatabase = "{1}mdb";
|
|
|
|
olcDbDirectory = "/var/db/ldap";
|
|
|
|
olcDbIndex = [
|
|
|
|
"objectClass eq"
|
|
|
|
"cn pres,eq"
|
|
|
|
"uid pres,eq"
|
|
|
|
"sn pres,eq,subany"
|
|
|
|
];
|
|
|
|
olcSuffix = "dc=example,dc=com";
|
|
|
|
olcAccess = [ "{0}to * by * read break" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
# This option overrides settings
|
2016-10-06 08:01:38 +00:00
|
|
|
configDir = mkOption {
|
2016-10-23 13:58:37 +00:00
|
|
|
type = types.nullOr types.path;
|
2016-10-23 11:55:23 +00:00
|
|
|
default = null;
|
2020-08-02 22:52:37 +00:00
|
|
|
description = ''
|
2020-09-13 22:20:23 +00:00
|
|
|
Use this config directory instead of generating one from the
|
|
|
|
<literal>settings</literal> option. Overrides all NixOS settings. If
|
|
|
|
you use this option,ensure `olcPidFile` is set to `/run/slapd/slapd.conf`.
|
2020-08-02 22:52:37 +00:00
|
|
|
'';
|
2016-10-06 08:01:38 +00:00
|
|
|
example = "/var/db/slapd.d";
|
|
|
|
};
|
|
|
|
|
2018-03-03 14:33:23 +00:00
|
|
|
declarativeContents = mkOption {
|
2020-09-27 20:50:25 +00:00
|
|
|
type = with types; attrsOf lines;
|
2020-08-23 23:07:24 +00:00
|
|
|
default = {};
|
2018-03-03 14:33:23 +00:00
|
|
|
description = ''
|
2020-09-27 20:50:25 +00:00
|
|
|
Declarative contents for the LDAP database, in LDIF format by suffix.
|
2018-03-03 14:33:23 +00:00
|
|
|
|
2020-09-27 20:50:25 +00:00
|
|
|
All data will be erased when starting the LDAP server. Modifications
|
|
|
|
to the database are not prevented, they are just dropped on the next
|
|
|
|
reboot of the server. Performance-wise the database and indexes are
|
|
|
|
rebuilt on each server startup, so this will slow down server startup,
|
2018-03-03 14:33:23 +00:00
|
|
|
especially with large databases.
|
|
|
|
'';
|
2021-10-03 16:06:03 +00:00
|
|
|
example = lib.literalExpression ''
|
2020-09-27 20:50:25 +00:00
|
|
|
{
|
|
|
|
"dc=example,dc=org" = '''
|
|
|
|
dn= dn: dc=example,dc=org
|
|
|
|
objectClass: domain
|
|
|
|
dc: example
|
|
|
|
|
|
|
|
dn: ou=users,dc=example,dc=org
|
|
|
|
objectClass = organizationalUnit
|
|
|
|
ou: users
|
|
|
|
|
|
|
|
# ...
|
|
|
|
''';
|
|
|
|
}
|
2018-03-03 14:33:23 +00:00
|
|
|
'';
|
|
|
|
};
|
2011-04-13 17:35:19 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-01-13 18:15:09 +00:00
|
|
|
meta.maintainers = with lib.maintainers; [ mic92 kwohlfahrt ];
|
2020-05-05 20:58:50 +00:00
|
|
|
|
2018-03-03 14:33:01 +00:00
|
|
|
config = mkIf cfg.enable {
|
2011-04-13 17:35:19 +00:00
|
|
|
environment.systemPackages = [ openldap ];
|
|
|
|
|
2020-09-27 20:50:25 +00:00
|
|
|
# Literal attributes must always be set
|
2020-08-02 22:52:37 +00:00
|
|
|
services.openldap.settings = {
|
|
|
|
attrs = {
|
|
|
|
objectClass = "olcGlobal";
|
|
|
|
cn = "config";
|
|
|
|
olcPidFile = "/run/slapd/slapd.pid";
|
2020-09-27 20:50:25 +00:00
|
|
|
};
|
2020-09-27 22:19:01 +00:00
|
|
|
children."cn=schema".attrs = {
|
|
|
|
cn = "schema";
|
|
|
|
objectClass = "olcSchemaConfig";
|
2020-09-27 20:50:25 +00:00
|
|
|
};
|
2020-08-02 22:52:37 +00:00
|
|
|
};
|
|
|
|
|
2013-04-09 15:50:53 +00:00
|
|
|
systemd.services.openldap = {
|
2022-05-19 20:06:41 +00:00
|
|
|
description = "OpenLDAP Server Daemon";
|
|
|
|
documentation = [
|
|
|
|
"man:slapd"
|
|
|
|
"man:slapd-config"
|
|
|
|
"man:slapd-mdb"
|
|
|
|
];
|
2013-04-09 15:50:53 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-05-19 20:06:41 +00:00
|
|
|
after = [ "network-online.target" ];
|
2020-08-02 22:52:37 +00:00
|
|
|
preStart = let
|
2020-09-27 20:50:25 +00:00
|
|
|
settingsFile = pkgs.writeText "config.ldif" (lib.concatStringsSep "\n" (attrsToLdif "cn=config" cfg.settings));
|
|
|
|
|
2020-08-02 22:52:37 +00:00
|
|
|
dbSettings = lib.filterAttrs (name: value: lib.hasPrefix "olcDatabase=" name) cfg.settings.children;
|
2020-08-23 23:07:24 +00:00
|
|
|
dataDirs = lib.mapAttrs' (name: value: lib.nameValuePair value.attrs.olcSuffix value.attrs.olcDbDirectory)
|
|
|
|
(lib.filterAttrs (_: value: value.attrs ? olcDbDirectory) dbSettings);
|
2020-09-27 20:50:25 +00:00
|
|
|
dataFiles = lib.mapAttrs (dn: contents: pkgs.writeText "${dn}.ldif" contents) cfg.declarativeContents;
|
|
|
|
mkLoadScript = dn: let
|
|
|
|
dataDir = lib.escapeShellArg (getAttr dn dataDirs);
|
|
|
|
in ''
|
|
|
|
rm -rf ${dataDir}/*
|
|
|
|
${openldap}/bin/slapadd -F ${lib.escapeShellArg configDir} -b ${dn} -l ${getAttr dn dataFiles}
|
|
|
|
chown -R "${cfg.user}:${cfg.group}" ${dataDir}
|
|
|
|
'';
|
2020-08-02 22:52:37 +00:00
|
|
|
in ''
|
2018-12-19 21:36:00 +00:00
|
|
|
mkdir -p /run/slapd
|
|
|
|
chown -R "${cfg.user}:${cfg.group}" /run/slapd
|
2020-08-02 22:52:37 +00:00
|
|
|
|
2020-08-23 23:19:35 +00:00
|
|
|
mkdir -p ${lib.escapeShellArg configDir} ${lib.escapeShellArgs (lib.attrValues dataDirs)}
|
|
|
|
chown "${cfg.user}:${cfg.group}" ${lib.escapeShellArg configDir} ${lib.escapeShellArgs (lib.attrValues dataDirs)}
|
2020-08-02 22:52:37 +00:00
|
|
|
|
2020-09-13 22:20:23 +00:00
|
|
|
${lib.optionalString (cfg.configDir == null) (''
|
|
|
|
rm -Rf ${configDir}/*
|
|
|
|
${openldap}/bin/slapadd -F ${configDir} -bcn=config -l ${settingsFile}
|
|
|
|
'')}
|
2020-08-23 23:19:35 +00:00
|
|
|
chown -R "${cfg.user}:${cfg.group}" ${lib.escapeShellArg configDir}
|
2020-08-23 23:07:24 +00:00
|
|
|
|
2020-09-27 20:50:25 +00:00
|
|
|
${lib.concatStrings (map mkLoadScript (lib.attrNames cfg.declarativeContents))}
|
2020-08-23 23:19:35 +00:00
|
|
|
${openldap}/bin/slaptest -u -F ${lib.escapeShellArg configDir}
|
2013-04-09 15:50:53 +00:00
|
|
|
'';
|
2020-08-02 22:52:37 +00:00
|
|
|
serviceConfig = {
|
2020-08-23 23:19:35 +00:00
|
|
|
ExecStart = lib.escapeShellArgs ([
|
|
|
|
"${openldap}/libexec/slapd" "-u" cfg.user "-g" cfg.group "-F" configDir
|
|
|
|
"-h" (lib.concatStringsSep " " cfg.urlList)
|
|
|
|
]);
|
2022-05-19 20:06:41 +00:00
|
|
|
Type = "notify";
|
2022-06-04 18:22:05 +00:00
|
|
|
# Fixes an error where openldap attempts to notify from a thread
|
|
|
|
# outside the main process:
|
|
|
|
# Got notification message from PID 6378, but reception only permitted for main PID 6377
|
2022-06-13 13:28:12 +00:00
|
|
|
NotifyAccess = "all";
|
2020-08-02 22:52:37 +00:00
|
|
|
PIDFile = cfg.settings.attrs.olcPidFile;
|
2015-01-26 08:35:56 +00:00
|
|
|
};
|
2020-08-02 22:52:37 +00:00
|
|
|
};
|
2013-11-28 21:21:50 +00:00
|
|
|
|
2020-08-02 22:52:37 +00:00
|
|
|
users.users = lib.optionalAttrs (cfg.user == "openldap") {
|
2020-08-23 23:19:35 +00:00
|
|
|
openldap = {
|
|
|
|
group = cfg.group;
|
|
|
|
isSystemUser = true;
|
|
|
|
};
|
2020-08-02 22:52:37 +00:00
|
|
|
};
|
2011-04-13 17:35:19 +00:00
|
|
|
|
2020-08-02 22:52:37 +00:00
|
|
|
users.groups = lib.optionalAttrs (cfg.group == "openldap") {
|
|
|
|
openldap = {};
|
|
|
|
};
|
2013-11-28 21:21:50 +00:00
|
|
|
};
|
2011-04-13 17:35:19 +00:00
|
|
|
}
|