2014-04-14 14:26:48 +00:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
2014-02-18 09:38:35 +00:00
|
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
|
with lib;
|
2014-02-18 09:38:35 +00:00
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
cfg = config.security.duosec;
|
|
|
|
|
|
|
|
|
|
boolToStr = b: if b then "yes" else "no";
|
|
|
|
|
|
2019-02-13 01:39:22 +00:00
|
|
|
|
configFilePam = ''
|
2014-02-18 09:38:35 +00:00
|
|
|
|
[duo]
|
2020-01-31 02:21:47 +00:00
|
|
|
|
ikey=${cfg.integrationKey}
|
2014-02-18 09:38:35 +00:00
|
|
|
|
host=${cfg.host}
|
2020-01-30 19:16:17 +00:00
|
|
|
|
${optionalString (cfg.groups != "") ("groups="+cfg.groups)}
|
2014-02-18 09:38:35 +00:00
|
|
|
|
failmode=${cfg.failmode}
|
|
|
|
|
pushinfo=${boolToStr cfg.pushinfo}
|
|
|
|
|
autopush=${boolToStr cfg.autopush}
|
|
|
|
|
prompts=${toString cfg.prompts}
|
|
|
|
|
fallback_local_ip=${boolToStr cfg.fallbackLocalIP}
|
|
|
|
|
'';
|
|
|
|
|
|
2019-02-13 01:39:22 +00:00
|
|
|
|
configFileLogin = configFilePam + ''
|
|
|
|
|
motd=${boolToStr cfg.motd}
|
|
|
|
|
accept_env_factor=${boolToStr cfg.acceptEnvFactor}
|
|
|
|
|
'';
|
2014-02-18 09:38:35 +00:00
|
|
|
|
in
|
|
|
|
|
{
|
2020-01-30 19:16:17 +00:00
|
|
|
|
imports = [
|
|
|
|
|
(mkRenamedOptionModule [ "security" "duosec" "group" ] [ "security" "duosec" "groups" ])
|
2020-01-31 02:21:47 +00:00
|
|
|
|
(mkRenamedOptionModule [ "security" "duosec" "ikey" ] [ "security" "duosec" "integrationKey" ])
|
2020-01-31 02:18:43 +00:00
|
|
|
|
(mkRemovedOptionModule [ "security" "duosec" "skey" ] "The insecure security.duosec.skey option has been replaced by a new security.duosec.secretKeyFile option. Use this new option to store a secure copy of your key instead.")
|
2020-01-30 19:16:17 +00:00
|
|
|
|
];
|
|
|
|
|
|
2014-02-18 09:38:35 +00:00
|
|
|
|
options = {
|
|
|
|
|
security.duosec = {
|
|
|
|
|
ssh.enable = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
|
description = lib.mdDoc "If enabled, protect SSH logins with Duo Security.";
|
2014-02-18 09:38:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pam.enable = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
|
description = lib.mdDoc "If enabled, protect logins with Duo Security using PAM support.";
|
2014-02-18 09:38:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-01-31 02:21:47 +00:00
|
|
|
|
integrationKey = mkOption {
|
2014-02-18 09:38:35 +00:00
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
|
description = lib.mdDoc "Integration key.";
|
2014-02-18 09:38:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-01-31 02:18:43 +00:00
|
|
|
|
secretKeyFile = mkOption {
|
2020-04-28 17:13:21 +00:00
|
|
|
|
type = types.nullOr types.path;
|
2020-01-31 02:18:43 +00:00
|
|
|
|
default = null;
|
2022-07-28 21:19:15 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-01-31 02:18:43 +00:00
|
|
|
|
A file containing your secret key. The security of your Duo application is tied to the security of your secret key.
|
|
|
|
|
'';
|
|
|
|
|
example = "/run/keys/duo-skey";
|
2014-02-18 09:38:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
host = mkOption {
|
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
|
description = lib.mdDoc "Duo API hostname.";
|
2014-02-18 09:38:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-01-30 19:16:17 +00:00
|
|
|
|
groups = mkOption {
|
2014-02-18 09:38:35 +00:00
|
|
|
|
type = types.str;
|
|
|
|
|
default = "";
|
2020-01-30 19:16:17 +00:00
|
|
|
|
example = "users,!wheel,!*admin guests";
|
2022-07-28 21:19:15 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-01-30 19:16:17 +00:00
|
|
|
|
If specified, Duo authentication is required only for users
|
|
|
|
|
whose primary group or supplementary group list matches one
|
|
|
|
|
of the space-separated pattern lists. Refer to
|
2022-07-28 21:19:15 +00:00
|
|
|
|
<https://duo.com/docs/duounix> for details.
|
2020-01-30 19:16:17 +00:00
|
|
|
|
'';
|
2014-02-18 09:38:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
failmode = mkOption {
|
2019-03-18 01:25:20 +00:00
|
|
|
|
type = types.enum [ "safe" "secure" ];
|
2014-02-18 09:38:35 +00:00
|
|
|
|
default = "safe";
|
2022-07-28 21:19:15 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2014-02-18 09:38:35 +00:00
|
|
|
|
On service or configuration errors that prevent Duo
|
|
|
|
|
authentication, fail "safe" (allow access) or "secure" (deny
|
|
|
|
|
access). The default is "safe".
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pushinfo = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2014-02-18 09:38:35 +00:00
|
|
|
|
Include information such as the command to be executed in
|
|
|
|
|
the Duo Push message.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
autopush = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
If `true`, Duo Unix will automatically send
|
2014-02-18 09:38:35 +00:00
|
|
|
|
a push login request to the user’s phone, falling back on a
|
|
|
|
|
phone call if push is unavailable. If
|
2022-07-28 21:19:15 +00:00
|
|
|
|
`false`, the user will be prompted to
|
2014-02-18 09:38:35 +00:00
|
|
|
|
choose an authentication method. When configured with
|
2022-07-28 21:19:15 +00:00
|
|
|
|
`autopush = yes`, we recommend setting
|
|
|
|
|
`prompts = 1`.
|
2014-02-18 09:38:35 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
motd = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
Print the contents of `/etc/motd` to screen
|
2014-12-30 02:31:03 +00:00
|
|
|
|
after a successful login.
|
2014-02-18 09:38:35 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
prompts = mkOption {
|
2016-11-16 13:36:05 +00:00
|
|
|
|
type = types.enum [ 1 2 3 ];
|
2014-02-18 09:38:35 +00:00
|
|
|
|
default = 3;
|
2022-07-28 21:19:15 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2014-02-18 09:38:35 +00:00
|
|
|
|
If a user fails to authenticate with a second factor, Duo
|
|
|
|
|
Unix will prompt the user to authenticate again. This option
|
|
|
|
|
sets the maximum number of prompts that Duo Unix will
|
|
|
|
|
display before denying access. Must be 1, 2, or 3. Default
|
|
|
|
|
is 3.
|
|
|
|
|
|
2022-07-28 21:19:15 +00:00
|
|
|
|
For example, when `prompts = 1`, the user
|
2014-02-18 09:38:35 +00:00
|
|
|
|
will have to successfully authenticate on the first prompt,
|
2022-07-28 21:19:15 +00:00
|
|
|
|
whereas if `prompts = 2`, if the user
|
2014-02-18 09:38:35 +00:00
|
|
|
|
enters incorrect information at the initial prompt, he/she
|
|
|
|
|
will be prompted to authenticate again.
|
|
|
|
|
|
2022-07-28 21:19:15 +00:00
|
|
|
|
When configured with `autopush = true`, we
|
|
|
|
|
recommend setting `prompts = 1`.
|
2014-02-18 09:38:35 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
acceptEnvFactor = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2014-02-18 09:38:35 +00:00
|
|
|
|
Look for factor selection or passcode in the
|
2022-07-28 21:19:15 +00:00
|
|
|
|
`$DUO_PASSCODE` environment variable before
|
2014-02-18 09:38:35 +00:00
|
|
|
|
prompting the user for input.
|
|
|
|
|
|
|
|
|
|
When $DUO_PASSCODE is non-empty, it will override
|
|
|
|
|
autopush. The SSH client will need SendEnv DUO_PASSCODE in
|
2014-12-30 02:31:03 +00:00
|
|
|
|
its configuration, and the SSH server will similarly need
|
2014-02-18 09:38:35 +00:00
|
|
|
|
AcceptEnv DUO_PASSCODE.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fallbackLocalIP = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2014-02-18 09:38:35 +00:00
|
|
|
|
Duo Unix reports the IP address of the authorizing user, for
|
|
|
|
|
the purposes of authorization and whitelisting. If Duo Unix
|
|
|
|
|
cannot detect the IP address of the client, setting
|
2022-07-28 21:19:15 +00:00
|
|
|
|
`fallbackLocalIP = yes` will cause Duo Unix
|
2014-02-18 09:38:35 +00:00
|
|
|
|
to send the IP address of the server it is running on.
|
|
|
|
|
|
|
|
|
|
If you are using IP whitelisting, enabling this option could
|
|
|
|
|
cause unauthorized logins if the local IP is listed in the
|
|
|
|
|
whitelist.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2014-05-20 07:42:31 +00:00
|
|
|
|
|
|
|
|
|
allowTcpForwarding = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2014-05-20 07:42:31 +00:00
|
|
|
|
By default, when SSH forwarding, enabling Duo Security will
|
|
|
|
|
disable TCP forwarding. By enabling this, you potentially
|
|
|
|
|
undermine some of the SSH based login security. Note this is
|
|
|
|
|
not needed if you use PAM.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2014-02-18 09:38:35 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = mkIf (cfg.ssh.enable || cfg.pam.enable) {
|
2020-01-31 02:15:56 +00:00
|
|
|
|
environment.systemPackages = [ pkgs.duo-unix ];
|
|
|
|
|
|
2021-09-12 16:53:48 +00:00
|
|
|
|
security.wrappers.login_duo =
|
|
|
|
|
{ setuid = true;
|
|
|
|
|
owner = "root";
|
|
|
|
|
group = "root";
|
|
|
|
|
source = "${pkgs.duo-unix.out}/bin/login_duo";
|
|
|
|
|
};
|
2020-01-31 02:18:43 +00:00
|
|
|
|
|
|
|
|
|
system.activationScripts = {
|
|
|
|
|
login_duo = mkIf cfg.ssh.enable ''
|
|
|
|
|
if test -f "${cfg.secretKeyFile}"; then
|
|
|
|
|
mkdir -m 0755 -p /etc/duo
|
|
|
|
|
|
|
|
|
|
umask 0077
|
|
|
|
|
conf="$(mktemp)"
|
|
|
|
|
{
|
|
|
|
|
cat ${pkgs.writeText "login_duo.conf" configFileLogin}
|
|
|
|
|
printf 'skey = %s\n' "$(cat ${cfg.secretKeyFile})"
|
|
|
|
|
} >"$conf"
|
|
|
|
|
|
|
|
|
|
chown sshd "$conf"
|
|
|
|
|
mv -fT "$conf" /etc/duo/login_duo.conf
|
|
|
|
|
fi
|
|
|
|
|
'';
|
|
|
|
|
pam_duo = mkIf cfg.pam.enable ''
|
|
|
|
|
if test -f "${cfg.secretKeyFile}"; then
|
|
|
|
|
mkdir -m 0755 -p /etc/duo
|
|
|
|
|
|
|
|
|
|
umask 0077
|
|
|
|
|
conf="$(mktemp)"
|
|
|
|
|
{
|
|
|
|
|
cat ${pkgs.writeText "login_duo.conf" configFilePam}
|
|
|
|
|
printf 'skey = %s\n' "$(cat ${cfg.secretKeyFile})"
|
|
|
|
|
} >"$conf"
|
|
|
|
|
|
|
|
|
|
mv -fT "$conf" /etc/duo/pam_duo.conf
|
|
|
|
|
fi
|
|
|
|
|
'';
|
|
|
|
|
};
|
2020-01-31 02:15:56 +00:00
|
|
|
|
|
|
|
|
|
/* If PAM *and* SSH are enabled, then don't do anything special.
|
|
|
|
|
If PAM isn't used, set the default SSH-only options. */
|
|
|
|
|
services.openssh.extraConfig = mkIf (cfg.ssh.enable || cfg.pam.enable) (
|
|
|
|
|
if cfg.pam.enable then "UseDNS no" else ''
|
|
|
|
|
# Duo Security configuration
|
|
|
|
|
ForceCommand ${config.security.wrapperDir}/login_duo
|
|
|
|
|
PermitTunnel no
|
|
|
|
|
${optionalString (!cfg.allowTcpForwarding) ''
|
|
|
|
|
AllowTcpForwarding no
|
|
|
|
|
''}
|
|
|
|
|
'');
|
2014-02-18 09:38:35 +00:00
|
|
|
|
};
|
|
|
|
|
}
|