2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
2024-07-27 06:49:29 +00:00
|
|
|
inherit (lib) mkEnableOption mkIf mkMerge mkOption mkRenamedOptionModule types;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
cfg = config.users.ldap;
|
|
|
|
|
|
|
|
# Careful: OpenLDAP seems to be very picky about the indentation of
|
|
|
|
# this file. Directives HAVE to start in the first column!
|
|
|
|
ldapConfig = {
|
|
|
|
target = "ldap.conf";
|
2024-07-27 06:49:29 +00:00
|
|
|
source = pkgs.writeText "ldap.conf" ''
|
2020-04-24 23:36:52 +00:00
|
|
|
uri ${config.users.ldap.server}
|
|
|
|
base ${config.users.ldap.base}
|
|
|
|
timelimit ${toString config.users.ldap.timeLimit}
|
|
|
|
bind_timelimit ${toString config.users.ldap.bind.timeLimit}
|
|
|
|
bind_policy ${config.users.ldap.bind.policy}
|
2024-07-27 06:49:29 +00:00
|
|
|
${lib.optionalString config.users.ldap.useTLS ''
|
2020-04-24 23:36:52 +00:00
|
|
|
ssl start_tls
|
|
|
|
''}
|
2024-07-27 06:49:29 +00:00
|
|
|
${lib.optionalString (config.users.ldap.bind.distinguishedName != "") ''
|
2020-04-24 23:36:52 +00:00
|
|
|
binddn ${config.users.ldap.bind.distinguishedName}
|
|
|
|
''}
|
2024-07-27 06:49:29 +00:00
|
|
|
${lib.optionalString (cfg.extraConfig != "") cfg.extraConfig }
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-07-27 06:49:29 +00:00
|
|
|
nslcdConfig = pkgs.writeText "nslcd.conf" ''
|
2020-04-24 23:36:52 +00:00
|
|
|
uri ${cfg.server}
|
|
|
|
base ${cfg.base}
|
|
|
|
timelimit ${toString cfg.timeLimit}
|
|
|
|
bind_timelimit ${toString cfg.bind.timeLimit}
|
2024-07-27 06:49:29 +00:00
|
|
|
${lib.optionalString (cfg.bind.distinguishedName != "")
|
2020-04-24 23:36:52 +00:00
|
|
|
"binddn ${cfg.bind.distinguishedName}" }
|
2024-07-27 06:49:29 +00:00
|
|
|
${lib.optionalString (cfg.daemon.rootpwmoddn != "")
|
2020-04-24 23:36:52 +00:00
|
|
|
"rootpwmoddn ${cfg.daemon.rootpwmoddn}" }
|
2024-07-27 06:49:29 +00:00
|
|
|
${lib.optionalString (cfg.daemon.extraConfig != "") cfg.daemon.extraConfig }
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
# nslcd normally reads configuration from /etc/nslcd.conf.
|
|
|
|
# this file might contain secrets. We append those at runtime,
|
|
|
|
# so redirect its location to something more temporary.
|
2024-07-27 06:49:29 +00:00
|
|
|
nslcdWrapped = pkgs.runCommand "nslcd-wrapped" { nativeBuildInputs = [ pkgs.makeWrapper ]; } ''
|
2020-04-24 23:36:52 +00:00
|
|
|
mkdir -p $out/bin
|
2024-07-27 06:49:29 +00:00
|
|
|
makeWrapper ${pkgs.nss_pam_ldapd}/sbin/nslcd $out/bin/nslcd \
|
2020-04-24 23:36:52 +00:00
|
|
|
--set LD_PRELOAD "${pkgs.libredirect}/lib/libredirect.so" \
|
|
|
|
--set NIX_REDIRECTS "/etc/nslcd.conf=/run/nslcd/nslcd.conf"
|
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
users.ldap = {
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = mkEnableOption "authentication against an LDAP server";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
loginPam = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whether to include authentication against LDAP in login PAM.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
nsswitch = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whether to include lookup against LDAP in NSS.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
server = mkOption {
|
2020-12-25 13:55:36 +00:00
|
|
|
type = types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
example = "ldap://ldap.example.org/";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The URL of the LDAP server.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
base = mkOption {
|
2020-12-25 13:55:36 +00:00
|
|
|
type = types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
example = "dc=example,dc=org";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The distinguished name of the search base.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useTLS = mkOption {
|
2020-05-03 17:38:23 +00:00
|
|
|
type = types.bool;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = false;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
If enabled, use TLS (encryption) over an LDAP (port 389)
|
|
|
|
connection. The alternative is to specify an LDAPS server (port
|
2022-08-12 12:06:08 +00:00
|
|
|
636) in {option}`users.ldap.server` or to forego
|
2020-04-24 23:36:52 +00:00
|
|
|
security.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
timeLimit = mkOption {
|
|
|
|
default = 0;
|
|
|
|
type = types.int;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Specifies the time limit (in seconds) to use when performing
|
|
|
|
searches. A value of zero (0), which is the default, is to
|
|
|
|
wait indefinitely for searches to be completed.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
daemon = {
|
|
|
|
enable = mkOption {
|
2020-05-03 17:38:23 +00:00
|
|
|
type = types.bool;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = false;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Whether to let the nslcd daemon (nss-pam-ldapd) handle the
|
|
|
|
LDAP lookups for NSS and PAM. This can improve performance,
|
|
|
|
and if you need to bind to the LDAP server with a password,
|
|
|
|
it increases security, since only the nslcd user needs to
|
|
|
|
have access to the bindpw file, not everyone that uses NSS
|
|
|
|
and/or PAM. If this option is enabled, a local nscd user is
|
|
|
|
created automatically, and the nslcd service is started
|
|
|
|
automatically when the network get up.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Extra configuration options that will be added verbatim at
|
2022-08-12 12:06:08 +00:00
|
|
|
the end of the nslcd configuration file (`nslcd.conf(5)`).
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
} ;
|
|
|
|
|
|
|
|
rootpwmoddn = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "cn=admin,dc=example,dc=com";
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The distinguished name to use to bind to the LDAP server
|
|
|
|
when the root user tries to modify a user's password.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
rootpwmodpwFile = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "/run/keys/nslcd.rootpwmodpw";
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The path to a file containing the credentials with which to bind to
|
|
|
|
the LDAP server if the root user tries to change a user's password.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
bind = {
|
|
|
|
distinguishedName = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "cn=admin,dc=example,dc=com";
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The distinguished name to bind to the LDAP server with. If this
|
|
|
|
is not specified, an anonymous bind will be done.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = mkOption {
|
|
|
|
default = "/etc/ldap/bind.password";
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The path to a file containing the credentials to use when binding
|
|
|
|
to the LDAP server (if not binding anonymously).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
timeLimit = mkOption {
|
|
|
|
default = 30;
|
|
|
|
type = types.int;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Specifies the time limit (in seconds) to use when connecting
|
|
|
|
to the directory server. This is distinct from the time limit
|
2022-08-12 12:06:08 +00:00
|
|
|
specified in {option}`users.ldap.timeLimit` and affects
|
2020-04-24 23:36:52 +00:00
|
|
|
the initial server connection only.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
policy = mkOption {
|
|
|
|
default = "hard_open";
|
|
|
|
type = types.enum [ "hard_open" "hard_init" "soft" ];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Specifies the policy to use for reconnecting to an unavailable
|
2022-08-21 13:32:41 +00:00
|
|
|
LDAP server. The default is `hard_open`, which
|
2020-04-24 23:36:52 +00:00
|
|
|
reconnects if opening the connection to the directory server
|
2022-08-21 13:32:41 +00:00
|
|
|
failed. By contrast, `hard_init` reconnects if
|
2020-04-24 23:36:52 +00:00
|
|
|
initializing the connection failed. Initializing may not
|
|
|
|
actually contact the directory server, and it is possible that
|
|
|
|
a malformed configuration file will trigger reconnection. If
|
2022-08-21 13:32:41 +00:00
|
|
|
`soft` is specified, then
|
|
|
|
`nss_ldap` will return immediately on server
|
2020-04-24 23:36:52 +00:00
|
|
|
failure. All hard reconnect policies block with exponential
|
|
|
|
backoff before retrying.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Extra configuration options that will be added verbatim at
|
2022-08-12 12:06:08 +00:00
|
|
|
the end of the ldap configuration file (`ldap.conf(5)`).
|
|
|
|
If {option}`users.ldap.daemon` is enabled, this
|
2020-04-24 23:36:52 +00:00
|
|
|
configuration will not be used. In that case, use
|
2022-08-12 12:06:08 +00:00
|
|
|
{option}`users.ldap.daemon.extraConfig` instead.
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2024-07-27 06:49:29 +00:00
|
|
|
environment.etc = lib.optionalAttrs (!cfg.daemon.enable) {
|
2020-04-24 23:36:52 +00:00
|
|
|
"ldap.conf" = ldapConfig;
|
|
|
|
};
|
|
|
|
|
2024-07-27 06:49:29 +00:00
|
|
|
system.nssModules = mkIf cfg.nsswitch (lib.singleton (
|
|
|
|
if cfg.daemon.enable then pkgs.nss_pam_ldapd else pkgs.nss_ldap
|
2020-12-25 13:55:36 +00:00
|
|
|
));
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-07-27 06:49:29 +00:00
|
|
|
system.nssDatabases.group = lib.optional cfg.nsswitch "ldap";
|
|
|
|
system.nssDatabases.passwd = lib.optional cfg.nsswitch "ldap";
|
|
|
|
system.nssDatabases.shadow = lib.optional cfg.nsswitch "ldap";
|
2020-05-15 21:57:56 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
users = mkIf cfg.daemon.enable {
|
|
|
|
groups.nslcd = {
|
|
|
|
gid = config.ids.gids.nslcd;
|
|
|
|
};
|
|
|
|
|
|
|
|
users.nslcd = {
|
|
|
|
uid = config.ids.uids.nslcd;
|
|
|
|
description = "nslcd user.";
|
|
|
|
group = "nslcd";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
systemd.services = mkMerge [
|
|
|
|
(mkIf (!cfg.daemon.enable) {
|
|
|
|
ldap-password = {
|
|
|
|
wantedBy = [ "sysinit.target" ];
|
|
|
|
before = [ "sysinit.target" "shutdown.target" ];
|
|
|
|
conflicts = [ "shutdown.target" ];
|
|
|
|
unitConfig.DefaultDependencies = false;
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
script = ''
|
|
|
|
if test -f "${cfg.bind.passwordFile}" ; then
|
|
|
|
umask 0077
|
|
|
|
conf="$(mktemp)"
|
|
|
|
printf 'bindpw %s\n' "$(cat ${cfg.bind.passwordFile})" |
|
|
|
|
cat ${ldapConfig.source} - >"$conf"
|
|
|
|
mv -fT "$conf" /etc/ldap.conf
|
|
|
|
fi
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
2024-01-02 11:29:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf cfg.daemon.enable {
|
|
|
|
nslcd = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
umask 0077
|
|
|
|
conf="$(mktemp)"
|
|
|
|
{
|
|
|
|
cat ${nslcdConfig}
|
|
|
|
test -z '${cfg.bind.distinguishedName}' -o ! -f '${cfg.bind.passwordFile}' ||
|
|
|
|
printf 'bindpw %s\n' "$(cat '${cfg.bind.passwordFile}')"
|
|
|
|
test -z '${cfg.daemon.rootpwmoddn}' -o ! -f '${cfg.daemon.rootpwmodpwFile}' ||
|
|
|
|
printf 'rootpwmodpw %s\n' "$(cat '${cfg.daemon.rootpwmodpwFile}')"
|
|
|
|
} >"$conf"
|
|
|
|
mv -fT "$conf" /run/nslcd/nslcd.conf
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
restartTriggers = [
|
|
|
|
nslcdConfig
|
|
|
|
cfg.bind.passwordFile
|
|
|
|
cfg.daemon.rootpwmodpwFile
|
|
|
|
];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${nslcdWrapped}/bin/nslcd";
|
|
|
|
Type = "forking";
|
|
|
|
Restart = "always";
|
|
|
|
User = "nslcd";
|
|
|
|
Group = "nslcd";
|
|
|
|
RuntimeDirectory = [ "nslcd" ];
|
|
|
|
PIDFile = "/run/nslcd/nslcd.pid";
|
|
|
|
AmbientCapabilities = "CAP_SYS_RESOURCE";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
imports =
|
|
|
|
[ (mkRenamedOptionModule [ "users" "ldap" "bind" "password"] [ "users" "ldap" "bind" "passwordFile"])
|
|
|
|
];
|
|
|
|
}
|