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-09-01 15:40:59 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
inherit (lib.types)
|
|
|
|
attrsOf
|
|
|
|
bool
|
|
|
|
enum
|
|
|
|
package
|
|
|
|
path
|
|
|
|
str
|
|
|
|
submodule
|
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-09-01 15:40:59 +02:00
|
|
|
|
|
|
|
acmeDirectory = config.security.acme.certs.${cfg.domain}.directory;
|
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.";
|
|
|
|
|
2024-09-01 15:40:59 +02:00
|
|
|
domain = mkOption {
|
|
|
|
type = str;
|
|
|
|
description = "The domain used for the RADIUS server.";
|
|
|
|
};
|
|
|
|
|
|
|
|
raddb = mkOption {
|
|
|
|
type = path;
|
|
|
|
default = "/var/lib/radius/raddb/";
|
|
|
|
description = "The location of the raddb directory.";
|
|
|
|
};
|
|
|
|
|
2023-12-03 17:35:07 +01:00
|
|
|
settings = mkOption { inherit (settingsFormat) type; };
|
|
|
|
|
|
|
|
freeradius = mkOption {
|
2024-09-01 15:40:59 +02:00
|
|
|
type = 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 {
|
2024-09-01 15:40:59 +02:00
|
|
|
type = path;
|
2023-12-03 17:35:07 +01:00
|
|
|
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 {
|
2024-09-01 15:40:59 +02:00
|
|
|
type = path;
|
2023-12-03 17:35:07 +01:00
|
|
|
description = "File to the auth token for the service account.";
|
|
|
|
};
|
|
|
|
|
2024-05-13 18:00:35 +02:00
|
|
|
extra-mods = mkOption {
|
2024-09-01 15:40:59 +02:00
|
|
|
type = attrsOf path;
|
2024-05-13 18:00:35 +02:00
|
|
|
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 {
|
2024-09-01 15:40:59 +02:00
|
|
|
type = attrsOf path;
|
2024-05-13 18:00:35 +02:00
|
|
|
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 {
|
2024-09-01 15:40:59 +02:00
|
|
|
type = attrsOf (enum [
|
|
|
|
"abinary"
|
|
|
|
"date"
|
|
|
|
"ipaddr"
|
|
|
|
"integer"
|
|
|
|
"string"
|
|
|
|
]);
|
2024-05-13 18:00:35 +02:00
|
|
|
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-09-01 15:40:59 +02:00
|
|
|
type = attrsOf (submodule {
|
|
|
|
options = {
|
|
|
|
secret = mkOption { type = path; };
|
|
|
|
ipaddr = mkOption { type = str; };
|
|
|
|
};
|
|
|
|
});
|
2023-12-03 17:35:07 +01:00
|
|
|
default = { };
|
|
|
|
description = "A mapping of clients and their authentication tokens.";
|
|
|
|
};
|
|
|
|
|
2024-05-13 18:00:35 +02:00
|
|
|
checkConfiguration = mkOption {
|
2024-09-01 15:40:59 +02:00
|
|
|
type = 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 {
|
2024-09-01 15:40:59 +02:00
|
|
|
# Certificate setup
|
|
|
|
services.nginx.virtualHosts.${cfg.domain} = {
|
|
|
|
http2 = false;
|
|
|
|
enableACME = true;
|
|
|
|
forceSSL = true;
|
|
|
|
};
|
|
|
|
|
2023-12-03 17:35:07 +01:00
|
|
|
users = {
|
|
|
|
users.radius = {
|
|
|
|
group = "radius";
|
|
|
|
description = "Radius daemon user";
|
|
|
|
isSystemUser = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
groups.radius = { };
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.radius = {
|
|
|
|
description = "FreeRadius server";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2024-09-01 15:40:59 +02:00
|
|
|
after = [
|
|
|
|
"network.target"
|
|
|
|
"acme-finished-${cfg.domain}.target"
|
|
|
|
];
|
2023-12-03 17:35:07 +01:00
|
|
|
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-09-01 15:40:59 +02:00
|
|
|
raddb=${cfg.raddb}
|
2024-04-08 23:10:59 +02:00
|
|
|
|
2024-09-01 15:40:59 +02:00
|
|
|
# Recreate the configuration directory
|
|
|
|
rm -rf $raddb && mkdir -p $raddb
|
2023-12-03 17:35:07 +01:00
|
|
|
|
2024-09-01 15:40:59 +02:00
|
|
|
cp -R --no-preserve=mode ${cfg.freeradius}/etc/raddb/* $raddb
|
|
|
|
cp -R --no-preserve=mode ${rlm_python}/etc/raddb/* $raddb
|
|
|
|
|
|
|
|
chmod -R u+w $raddb
|
2023-12-03 17:35:07 +01:00
|
|
|
|
|
|
|
# disable auth via methods kanidm doesn't support
|
2024-09-01 15:40:59 +02:00
|
|
|
rm $raddb/mods-available/sql
|
|
|
|
rm $raddb/mods-enabled/{passwd,totp}
|
2023-12-03 17:35:07 +01:00
|
|
|
|
|
|
|
# enable the python and cache modules
|
2024-09-01 15:40:59 +02:00
|
|
|
ln -nsf $raddb/mods-available/python3 $raddb/mods-enabled/python3
|
|
|
|
ln -nsf $raddb/sites-available/check-eap-tls $raddb/sites-enabled/check-eap-tls
|
2023-12-03 17:35:07 +01:00
|
|
|
|
|
|
|
# write the clients configuration
|
2024-09-01 15:40:59 +02:00
|
|
|
> $raddb/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 }:
|
|
|
|
''
|
2024-09-01 15:40:59 +02:00
|
|
|
cat <<EOF >> $raddb/clients.conf
|
2024-03-10 01:03:30 +01:00
|
|
|
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
|
2024-09-01 15:40:59 +02:00
|
|
|
rm -rf $raddb/certs && mkdir -p $raddb/certs
|
2023-12-03 17:35:07 +01:00
|
|
|
|
2024-09-01 15:40:59 +02:00
|
|
|
cp ${acmeDirectory}/chain.pem $raddb/certs/ca.pem
|
2023-12-03 17:35:07 +01:00
|
|
|
|
2024-09-01 15:40:59 +02:00
|
|
|
${lib.getExe pkgs.openssl} rehash $raddb/certs
|
2023-12-03 17:35:07 +01:00
|
|
|
|
2024-09-01 15:40:59 +02:00
|
|
|
# Recreate the dh.pem file
|
|
|
|
${lib.getExe pkgs.openssl} dhparam -in $raddb/certs/ca.pem -out $raddb/certs/dh.pem 2048
|
2023-12-03 17:35:07 +01:00
|
|
|
|
2024-09-01 15:40:59 +02:00
|
|
|
cp ${acmeDirectory}/full.pem $raddb/certs/server.pem
|
2023-12-03 17:35:07 +01:00
|
|
|
|
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-09-01 15:40:59 +02:00
|
|
|
} $raddb/dictionary
|
2024-05-13 18:00:35 +02:00
|
|
|
|
|
|
|
# Link extra-mods
|
|
|
|
${builtins.concatStringsSep "\n" (
|
2024-09-01 15:40:59 +02:00
|
|
|
mapAttrsToList (name: path: "ln -nsf ${path} $raddb/mods-enabled/${name}") cfg.extra-mods
|
2024-05-13 18:00:35 +02:00
|
|
|
)}
|
|
|
|
|
|
|
|
# Link extra-sites
|
|
|
|
${builtins.concatStringsSep "\n" (
|
2024-09-01 15:40:59 +02:00
|
|
|
mapAttrsToList (name: path: "ln -nsf ${path} $raddb/sites-enabled/${name}") cfg.extra-sites
|
2024-05-13 18:00:35 +02:00
|
|
|
)}
|
|
|
|
|
2023-12-03 17:35:07 +01:00
|
|
|
# Check the configuration
|
2024-09-01 15:40:59 +02:00
|
|
|
${optionalString cfg.checkConfiguration "${getExe' pkgs.freeradius "radiusd"} -C -d $raddb -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
|
|
|
|
2024-09-01 15:40:59 +02:00
|
|
|
environment = {
|
|
|
|
KANIDM_RLM_CONFIG = "/var/lib/radius/kanidm.toml";
|
|
|
|
PYTHONPATH = rlm_python.pythonPath;
|
|
|
|
};
|
|
|
|
|
2023-12-03 17:35:07 +01:00
|
|
|
serviceConfig = {
|
2024-09-01 15:40:59 +02:00
|
|
|
ExecStart = "${cfg.freeradius}/bin/radiusd -X -f -d /var/lib/radius/raddb -l stdout";
|
2023-12-03 17:35:07 +01:00
|
|
|
ExecReload = [
|
2024-09-01 15:40:59 +02:00
|
|
|
"${cfg.freeradius}/bin/radiusd -C -d /var/lib/radius/raddb -l stdout"
|
2023-12-03 17:35:07 +01:00
|
|
|
"${pkgs.coreutils}/bin/kill -HUP $MAINPID"
|
|
|
|
];
|
2024-09-01 15:40:59 +02:00
|
|
|
AmbientCapabilities = "CAP_NET_BIND_SERVICE";
|
2023-12-03 17:35:07 +01:00
|
|
|
DynamicUser = true;
|
2024-09-01 15:40:59 +02:00
|
|
|
Group = "radius";
|
|
|
|
LogsDirectory = "radius";
|
|
|
|
ReadOnlyPaths = [ acmeDirectory ];
|
2023-12-03 17:35:07 +01:00
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = 2;
|
2024-02-17 23:57:40 +01:00
|
|
|
RuntimeDirectory = "radius";
|
2024-09-01 15:40:59 +02:00
|
|
|
StateDirectory = "radius";
|
|
|
|
SupplementaryGroups = [ "nginx" ];
|
|
|
|
User = "radius";
|
2023-12-03 17:35:07 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|