2024-02-02 10:51:31 +01:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
2023-12-03 17:35:07 +01:00
|
|
|
|
|
|
|
let
|
2024-02-02 10:51:31 +01:00
|
|
|
inherit (lib)
|
2024-05-23 16:28:13 +02:00
|
|
|
attrsToList
|
|
|
|
getExe'
|
|
|
|
imap0
|
|
|
|
mapAttrsToList
|
2024-02-02 10:51:31 +01:00
|
|
|
mkEnableOption
|
|
|
|
mkIf
|
|
|
|
mkOption
|
2024-05-13 18:00:35 +02:00
|
|
|
optionalString
|
2024-05-23 16:28:13 +02:00
|
|
|
types
|
2024-02-02 10:51:31 +01:00
|
|
|
;
|
2023-12-03 17:35:07 +01:00
|
|
|
|
|
|
|
settingsFormat = pkgs.formats.toml { };
|
|
|
|
|
2024-04-08 22:42:59 +02:00
|
|
|
pykanidm = pkgs.python3.pkgs.callPackage ./packages/pykanidm.nix { };
|
2023-12-12 19:18:57 +01:00
|
|
|
rlm_python = pkgs.callPackage ./packages/rlm_python.nix { inherit pykanidm; };
|
2023-12-03 17:35:07 +01:00
|
|
|
|
|
|
|
cfg = config.services.k-radius;
|
2024-02-02 10:51:31 +01:00
|
|
|
in
|
|
|
|
{
|
2023-12-03 17:35:07 +01:00
|
|
|
options.services.k-radius = {
|
|
|
|
enable = mkEnableOption "a freeradius service linked to kanidm.";
|
|
|
|
|
|
|
|
settings = mkOption { inherit (settingsFormat) type; };
|
|
|
|
|
|
|
|
freeradius = mkOption {
|
|
|
|
type = types.package;
|
2024-03-10 01:03:30 +01:00
|
|
|
default = pkgs.freeradius.overrideAttrs (old: {
|
|
|
|
buildInputs = (old.buildInputs or [ ]) ++ [ (pkgs.python3.withPackages (ps: [ ps.kanidm ])) ];
|
|
|
|
});
|
2023-12-03 17:35:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
configDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/radius/raddb";
|
2024-02-02 10:51:31 +01:00
|
|
|
description = "The path of the freeradius server configuration directory.";
|
2023-12-03 17:35:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
authTokenFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
description = "File to the auth token for the service account.";
|
|
|
|
};
|
|
|
|
|
2024-05-13 18:00:35 +02:00
|
|
|
extra-mods = mkOption {
|
|
|
|
type = types.attrsOf types.path;
|
|
|
|
default = { };
|
2024-05-23 16:28:13 +02:00
|
|
|
description = "Additional files to be linked in mods-enabled.";
|
2024-05-13 18:00:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extra-sites = mkOption {
|
|
|
|
type = types.attrsOf types.path;
|
|
|
|
default = { };
|
2024-05-23 16:28:13 +02:00
|
|
|
description = "Additional files to be linked in sites-enabled.";
|
2024-05-13 18:00:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
dictionary = mkOption {
|
|
|
|
type = types.attrsOf (
|
|
|
|
types.enum [
|
|
|
|
"abinary"
|
|
|
|
"date"
|
|
|
|
"ipaddr"
|
|
|
|
"integer"
|
|
|
|
"string"
|
|
|
|
]
|
|
|
|
);
|
|
|
|
default = { };
|
2024-05-23 16:28:13 +02:00
|
|
|
description = "Declare additionnal attributes to be listed in the dictionary.";
|
2024-05-13 18:00:35 +02:00
|
|
|
};
|
|
|
|
|
2023-12-03 17:35:07 +01:00
|
|
|
radiusClients = mkOption {
|
2024-02-02 10:51:31 +01:00
|
|
|
type = types.attrsOf (
|
|
|
|
types.submodule {
|
|
|
|
options = {
|
|
|
|
secret = mkOption { type = types.path; };
|
|
|
|
ipaddr = mkOption { type = types.str; };
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
2023-12-03 17:35:07 +01:00
|
|
|
default = { };
|
|
|
|
description = "A mapping of clients and their authentication tokens.";
|
|
|
|
};
|
|
|
|
|
|
|
|
certs = {
|
|
|
|
ca = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "The signing CA of the RADIUS certificate.";
|
|
|
|
};
|
|
|
|
dh = mkOption {
|
|
|
|
type = types.str;
|
2024-02-02 10:51:31 +01:00
|
|
|
description = "The output of `openssl dhparam -in ca.pem -out dh.pem 2048`.";
|
2023-12-03 17:35:07 +01:00
|
|
|
};
|
|
|
|
cert = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "The certificate for the RADIUS server.";
|
|
|
|
};
|
|
|
|
key = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "The signing key for the RADIUS certificate.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
privateKeyPasswordFile = mkOption { type = types.path; };
|
2024-05-13 18:00:35 +02:00
|
|
|
|
|
|
|
checkConfiguration = mkOption {
|
|
|
|
type = types.bool;
|
2024-05-23 16:28:13 +02:00
|
|
|
description = "Check the configuration before starting the deamon. Useful for debugging.";
|
2024-05-13 18:00:35 +02:00
|
|
|
default = false;
|
|
|
|
};
|
2023-12-03 17:35:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
users = {
|
|
|
|
users.radius = {
|
|
|
|
group = "radius";
|
|
|
|
description = "Radius daemon user";
|
|
|
|
isSystemUser = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
groups.radius = { };
|
|
|
|
};
|
|
|
|
|
|
|
|
services.k-radius.settings = {
|
|
|
|
ca_path = cfg.certs.ca;
|
|
|
|
|
|
|
|
radius_cert_path = cfg.certs.cert;
|
|
|
|
radius_key_path = cfg.certs.key;
|
|
|
|
radius_dh_path = cfg.certs.dh;
|
|
|
|
radius_ca_path = cfg.certs.ca;
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.radius = {
|
|
|
|
description = "FreeRadius server";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
wants = [ "network.target" ];
|
2024-05-13 18:00:35 +02:00
|
|
|
startLimitIntervalSec = 20;
|
|
|
|
startLimitBurst = 5;
|
2023-12-03 17:35:07 +01:00
|
|
|
|
|
|
|
preStart = ''
|
2024-05-22 18:34:51 +02:00
|
|
|
rm -rf ${cfg.configDir}
|
2024-04-08 23:10:59 +02:00
|
|
|
mkdir -p ${cfg.configDir}
|
|
|
|
|
|
|
|
cp -R --no-preserve=mode ${cfg.freeradius}/etc/raddb/* ${cfg.configDir}
|
|
|
|
cp -R --no-preserve=mode ${rlm_python}/etc/raddb/* ${cfg.configDir}
|
2023-12-03 17:35:07 +01:00
|
|
|
|
|
|
|
chmod -R u+w ${cfg.configDir}
|
|
|
|
|
|
|
|
# disable auth via methods kanidm doesn't support
|
|
|
|
rm ${cfg.configDir}/mods-available/sql
|
|
|
|
rm ${cfg.configDir}/mods-enabled/{passwd,totp}
|
|
|
|
|
|
|
|
# enable the python and cache modules
|
|
|
|
ln -nsf ${cfg.configDir}/mods-available/python3 ${cfg.configDir}/mods-enabled/python3
|
|
|
|
ln -nsf ${cfg.configDir}/sites-available/check-eap-tls ${cfg.configDir}/sites-enabled/check-eap-tls
|
|
|
|
|
|
|
|
# write the clients configuration
|
|
|
|
rm ${cfg.configDir}/clients.conf && touch ${cfg.configDir}/clients.conf
|
2024-02-02 10:51:31 +01:00
|
|
|
${builtins.concatStringsSep "\n" (
|
|
|
|
builtins.attrValues (
|
2024-03-10 01:03:30 +01:00
|
|
|
builtins.mapAttrs (
|
|
|
|
name:
|
|
|
|
{ secret, ipaddr }:
|
|
|
|
''
|
|
|
|
cat <<EOF >> ${cfg.configDir}/clients.conf
|
|
|
|
client ${name} {
|
|
|
|
ipaddr = ${ipaddr}
|
|
|
|
secret = $(cat "${secret}")
|
|
|
|
proto = *
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
''
|
|
|
|
) cfg.radiusClients
|
2024-02-02 10:51:31 +01:00
|
|
|
)
|
|
|
|
)}
|
2023-12-03 17:35:07 +01:00
|
|
|
|
|
|
|
# Copy the kanidm configuration
|
|
|
|
cat <<EOF > /var/lib/radius/kanidm.toml
|
|
|
|
auth_token = "$(cat "${cfg.authTokenFile}")"
|
|
|
|
EOF
|
|
|
|
|
2024-02-02 10:51:31 +01:00
|
|
|
cat ${settingsFormat.generate "kanidm.toml" cfg.settings} >> /var/lib/radius/kanidm.toml
|
2023-12-03 17:35:07 +01:00
|
|
|
chmod u+w /var/lib/radius/kanidm.toml
|
|
|
|
|
|
|
|
# Copy the certificates to the correct directory
|
|
|
|
rm -rf ${cfg.configDir}/certs && mkdir -p ${cfg.configDir}/certs
|
|
|
|
|
|
|
|
cp ${cfg.certs.ca} ${cfg.configDir}/certs/ca.pem
|
|
|
|
|
|
|
|
${pkgs.openssl}/bin/openssl rehash ${cfg.configDir}/certs
|
|
|
|
|
|
|
|
cp ${cfg.certs.dh} ${cfg.configDir}/certs/dh.pem
|
|
|
|
|
|
|
|
cat ${cfg.certs.cert} ${cfg.certs.key} > ${cfg.configDir}/certs/server.pem
|
|
|
|
|
|
|
|
# Write the password of the private_key in the eap module
|
|
|
|
sed -i ${cfg.configDir}/mods-available/eap \
|
|
|
|
-e "s/whatever/$(cat "${cfg.privateKeyPasswordFile}")/"
|
|
|
|
|
2024-05-23 16:28:13 +02:00
|
|
|
# Link the dictionary
|
|
|
|
ln -nsf ${
|
|
|
|
pkgs.writeText "radius-dictionary" (
|
|
|
|
builtins.concatStringsSep "\n" (
|
|
|
|
imap0 (i: { name, value }: "ATTRIBUTE ${name} ${builtins.toString (3000 + i)} ${value}") (
|
|
|
|
attrsToList cfg.dictionary
|
|
|
|
)
|
|
|
|
)
|
2024-05-13 18:00:35 +02:00
|
|
|
)
|
2024-05-23 16:28:13 +02:00
|
|
|
} ${cfg.configDir}/dictionary
|
2024-05-13 18:00:35 +02:00
|
|
|
|
|
|
|
# Link extra-mods
|
|
|
|
${builtins.concatStringsSep "\n" (
|
|
|
|
mapAttrsToList (name: path: "ln -nsf ${path} ${cfg.configDir}/mods-enabled/${name}") cfg.extra-mods
|
|
|
|
)}
|
|
|
|
|
|
|
|
# Link extra-sites
|
|
|
|
${builtins.concatStringsSep "\n" (
|
|
|
|
mapAttrsToList (
|
|
|
|
name: path: "ln -nsf ${path} ${cfg.configDir}/sites-enabled/${name}"
|
|
|
|
) cfg.extra-sites
|
|
|
|
)}
|
|
|
|
|
2023-12-03 17:35:07 +01:00
|
|
|
# Check the configuration
|
2024-05-23 16:28:13 +02:00
|
|
|
${optionalString cfg.checkConfiguration "${getExe' pkgs.freeradius "radiusd"} -C -d ${cfg.configDir} -l stdout"}
|
2023-12-03 17:35:07 +01:00
|
|
|
'';
|
|
|
|
|
2024-02-02 10:51:31 +01:00
|
|
|
path = [
|
|
|
|
pkgs.openssl
|
|
|
|
pkgs.gnused
|
|
|
|
];
|
2023-12-03 17:35:07 +01:00
|
|
|
|
|
|
|
serviceConfig = {
|
2024-02-17 23:57:40 +01:00
|
|
|
ExecStart = "${cfg.freeradius}/bin/radiusd -X -f -d ${cfg.configDir} -l stdout";
|
2023-12-03 17:35:07 +01:00
|
|
|
ExecReload = [
|
|
|
|
"${cfg.freeradius}/bin/radiusd -C -d ${cfg.configDir} -l stdout"
|
|
|
|
"${pkgs.coreutils}/bin/kill -HUP $MAINPID"
|
|
|
|
];
|
|
|
|
User = "radius";
|
|
|
|
Group = "radius";
|
|
|
|
DynamicUser = true;
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = 2;
|
|
|
|
LogsDirectory = "radius";
|
|
|
|
StateDirectory = "radius";
|
2024-02-17 23:57:40 +01:00
|
|
|
RuntimeDirectory = "radius";
|
2024-05-13 18:00:35 +02:00
|
|
|
AmbientCapabilities = "CAP_NET_BIND_SERVICE";
|
2023-12-03 17:35:07 +01:00
|
|
|
Environment = [
|
|
|
|
"KANIDM_RLM_CONFIG=/var/lib/radius/kanidm.toml"
|
|
|
|
"PYTHONPATH=${rlm_python.pythonPath}"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|