2009-05-28 12:24:56 +00:00
|
|
|
# Configuration for the pwdutils suite of tools: passwd, useradd, etc.
|
2016-06-12 19:03:14 +00:00
|
|
|
{ config, lib, utils, pkgs, ... }:
|
2009-05-28 12:24:56 +00:00
|
|
|
let
|
2023-05-06 08:33:41 +00:00
|
|
|
cfg = config.security.loginDefs;
|
2010-06-02 21:10:48 +00:00
|
|
|
in
|
|
|
|
{
|
2024-05-18 20:12:53 +00:00
|
|
|
options = {
|
2024-05-16 12:09:13 +00:00
|
|
|
|
|
|
|
security.shadow.enable = lib.mkEnableOption "" // {
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Enable the shadow authentication suite, which provides critical programs such as su, login, passwd.
|
|
|
|
|
|
|
|
Note: This is currently experimental. Only disable this if you're
|
|
|
|
confident that you can recover your system if it breaks.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-05-06 08:33:41 +00:00
|
|
|
security.loginDefs = {
|
2024-04-17 11:37:58 +00:00
|
|
|
package = lib.mkPackageOption pkgs "shadow" { };
|
2023-05-06 08:33:41 +00:00
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
chfnRestrict = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = ''
|
|
|
|
Use chfn SUID to allow non-root users to change their account GECOS information.
|
|
|
|
'';
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.nullOr lib.types.str;
|
2023-05-06 08:33:41 +00:00
|
|
|
default = null;
|
|
|
|
};
|
2010-06-02 21:10:48 +00:00
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
settings = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = ''
|
|
|
|
Config options for the /etc/login.defs file, that defines
|
|
|
|
the site-specific configuration for the shadow password suite.
|
|
|
|
See login.defs(5) man page for available options.
|
|
|
|
'';
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.submodule {
|
2023-05-06 08:33:41 +00:00
|
|
|
freeformType = (pkgs.formats.keyValue { }).type;
|
|
|
|
/* There are three different sources for user/group id ranges, each of which gets
|
|
|
|
used by different programs:
|
|
|
|
- The login.defs file, used by the useradd, groupadd and newusers commands
|
|
|
|
- The update-users-groups.pl file, used by NixOS in the activation phase to
|
|
|
|
decide on which ids to use for declaratively defined users without a static
|
|
|
|
id
|
|
|
|
- Systemd compile time options -Dsystem-uid-max= and -Dsystem-gid-max=, used
|
|
|
|
by systemd for features like ConditionUser=@system and systemd-sysusers
|
|
|
|
*/
|
|
|
|
options = {
|
2024-04-17 11:37:58 +00:00
|
|
|
DEFAULT_HOME = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = "Indicate if login is allowed if we can't cd to the home directory.";
|
|
|
|
default = "yes";
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.enum [ "yes" "no" ];
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
ENCRYPT_METHOD = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = "This defines the system default encryption algorithm for encrypting passwords.";
|
|
|
|
# The default crypt() method, keep in sync with the PAM default
|
|
|
|
default = "YESCRYPT";
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.enum [ "YESCRYPT" "SHA512" "SHA256" "MD5" "DES"];
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
SYS_UID_MIN = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = "Range of user IDs used for the creation of system users by useradd or newusers.";
|
|
|
|
default = 400;
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.int;
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
SYS_UID_MAX = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = "Range of user IDs used for the creation of system users by useradd or newusers.";
|
|
|
|
default = 999;
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.int;
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
UID_MIN = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = "Range of user IDs used for the creation of regular users by useradd or newusers.";
|
|
|
|
default = 1000;
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.int;
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
UID_MAX = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = "Range of user IDs used for the creation of regular users by useradd or newusers.";
|
|
|
|
default = 29999;
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.int;
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
SYS_GID_MIN = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = "Range of group IDs used for the creation of system groups by useradd, groupadd, or newusers";
|
|
|
|
default = 400;
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.int;
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
SYS_GID_MAX = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = "Range of group IDs used for the creation of system groups by useradd, groupadd, or newusers";
|
|
|
|
default = 999;
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.int;
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
GID_MIN = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = "Range of group IDs used for the creation of regular groups by useradd, groupadd, or newusers.";
|
|
|
|
default = 1000;
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.int;
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
GID_MAX = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = "Range of group IDs used for the creation of regular groups by useradd, groupadd, or newusers.";
|
|
|
|
default = 29999;
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.int;
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
TTYGROUP = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = ''
|
|
|
|
The terminal permissions: the login tty will be owned by the TTYGROUP group,
|
|
|
|
and the permissions will be set to TTYPERM'';
|
|
|
|
default = "tty";
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.str;
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
TTYPERM = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = ''
|
|
|
|
The terminal permissions: the login tty will be owned by the TTYGROUP group,
|
|
|
|
and the permissions will be set to TTYPERM'';
|
|
|
|
default = "0620";
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.str;
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# Ensure privacy for newly created home directories.
|
2024-04-17 11:37:58 +00:00
|
|
|
UMASK = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = "The file mode creation mask is initialized to this value.";
|
|
|
|
default = "077";
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.str;
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
default = { };
|
|
|
|
};
|
|
|
|
};
|
2009-05-28 12:24:56 +00:00
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
users.defaultUserShell = lib.mkOption {
|
2023-05-06 08:33:41 +00:00
|
|
|
description = ''
|
2012-06-06 15:23:20 +00:00
|
|
|
This option defines the default shell assigned to user
|
2016-06-12 19:03:14 +00:00
|
|
|
accounts. This can be either a full system path or a shell package.
|
|
|
|
|
|
|
|
This must not be a store path, since the path is
|
2009-05-28 12:24:56 +00:00
|
|
|
used outside the store (in particular in /etc/passwd).
|
|
|
|
'';
|
2024-04-17 11:37:58 +00:00
|
|
|
example = lib.literalExpression "pkgs.zsh";
|
2024-05-18 20:12:53 +00:00
|
|
|
type = lib.types.either lib.types.path lib.types.shellPackage;
|
2009-05-28 12:24:56 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2010-06-02 21:10:48 +00:00
|
|
|
###### implementation
|
2009-05-28 12:24:56 +00:00
|
|
|
|
2024-05-16 12:09:13 +00:00
|
|
|
config = lib.mkMerge [
|
|
|
|
{
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = config.security.shadow.enable || config.services.greetd.enable;
|
|
|
|
message = "You must enable at least one VT login method, either security.shadow.enable or services.greetd.enable";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
(lib.mkIf config.security.shadow.enable {
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfg.settings.SYS_UID_MIN <= cfg.settings.SYS_UID_MAX;
|
|
|
|
message = "SYS_UID_MIN must be less than or equal to SYS_UID_MAX";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = cfg.settings.UID_MIN <= cfg.settings.UID_MAX;
|
|
|
|
message = "UID_MIN must be less than or equal to UID_MAX";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = cfg.settings.SYS_GID_MIN <= cfg.settings.SYS_GID_MAX;
|
|
|
|
message = "SYS_GID_MIN must be less than or equal to SYS_GID_MAX";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = cfg.settings.GID_MIN <= cfg.settings.GID_MAX;
|
|
|
|
message = "GID_MIN must be less than or equal to GID_MAX";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2024-05-18 20:12:53 +00:00
|
|
|
security.loginDefs.settings.CHFN_RESTRICT = lib.mkIf (cfg.chfnRestrict != null) cfg.chfnRestrict;
|
2024-05-16 12:09:13 +00:00
|
|
|
|
|
|
|
environment.systemPackages = lib.optional config.users.mutableUsers cfg.package
|
2024-05-18 20:12:53 +00:00
|
|
|
++ lib.optional (lib.types.shellPackage.check config.users.defaultUserShell) config.users.defaultUserShell
|
|
|
|
++ lib.optional (cfg.chfnRestrict != null) pkgs.util-linux;
|
2024-05-16 12:09:13 +00:00
|
|
|
|
|
|
|
environment.etc =
|
|
|
|
# Create custom toKeyValue generator
|
|
|
|
# see https://man7.org/linux/man-pages/man5/login.defs.5.html for config specification
|
|
|
|
let
|
|
|
|
toKeyValue = lib.generators.toKeyValue {
|
|
|
|
mkKeyValue = lib.generators.mkKeyValueDefault { } " ";
|
|
|
|
};
|
2024-05-18 20:12:53 +00:00
|
|
|
in {
|
|
|
|
# /etc/login.defs: global configuration for pwdutils.
|
|
|
|
# You cannot login without it!
|
|
|
|
"login.defs".source = pkgs.writeText "login.defs" (toKeyValue cfg.settings);
|
2024-05-16 12:09:13 +00:00
|
|
|
|
2024-05-18 20:12:53 +00:00
|
|
|
# /etc/default/useradd: configuration for useradd.
|
|
|
|
"default/useradd".source = pkgs.writeText "useradd" ''
|
|
|
|
GROUP=100
|
|
|
|
HOME=/home
|
|
|
|
SHELL=${utils.toShellPath config.users.defaultUserShell}
|
|
|
|
'';
|
|
|
|
};
|
2023-05-06 08:33:41 +00:00
|
|
|
|
2024-05-16 12:09:13 +00:00
|
|
|
security.pam.services = {
|
2024-05-18 20:12:53 +00:00
|
|
|
chsh.rootOK = true;
|
|
|
|
chfn.rootOK = true;
|
2024-05-16 12:09:13 +00:00
|
|
|
su = {
|
|
|
|
rootOK = true;
|
|
|
|
forwardXAuth = true;
|
|
|
|
logFailures = true;
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
2024-05-16 12:09:13 +00:00
|
|
|
passwd = { };
|
|
|
|
# Note: useradd, groupadd etc. aren't setuid root, so it
|
|
|
|
# doesn't really matter what the PAM config says as long as it
|
|
|
|
# lets root in.
|
|
|
|
useradd.rootOK = true;
|
|
|
|
usermod.rootOK = true;
|
|
|
|
userdel.rootOK = true;
|
|
|
|
groupadd.rootOK = true;
|
|
|
|
groupmod.rootOK = true;
|
|
|
|
groupmems.rootOK = true;
|
|
|
|
groupdel.rootOK = true;
|
|
|
|
login = {
|
|
|
|
startSession = true;
|
|
|
|
allowNullPassword = true;
|
|
|
|
showMotd = true;
|
|
|
|
updateWtmp = true;
|
|
|
|
};
|
2024-05-18 20:12:53 +00:00
|
|
|
chpasswd.rootOK = true;
|
2023-05-06 08:33:41 +00:00
|
|
|
};
|
2024-05-16 12:09:13 +00:00
|
|
|
|
|
|
|
security.wrappers =
|
|
|
|
let
|
|
|
|
mkSetuidRoot = source: {
|
|
|
|
setuid = true;
|
|
|
|
owner = "root";
|
|
|
|
group = "root";
|
|
|
|
inherit source;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
su = mkSetuidRoot "${cfg.package.su}/bin/su";
|
|
|
|
sg = mkSetuidRoot "${cfg.package.out}/bin/sg";
|
|
|
|
newgrp = mkSetuidRoot "${cfg.package.out}/bin/newgrp";
|
|
|
|
newuidmap = mkSetuidRoot "${cfg.package.out}/bin/newuidmap";
|
|
|
|
newgidmap = mkSetuidRoot "${cfg.package.out}/bin/newgidmap";
|
|
|
|
}
|
|
|
|
// lib.optionalAttrs config.users.mutableUsers {
|
|
|
|
chsh = mkSetuidRoot "${cfg.package.out}/bin/chsh";
|
|
|
|
passwd = mkSetuidRoot "${cfg.package.out}/bin/passwd";
|
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
2009-05-28 12:24:56 +00:00
|
|
|
}
|