2020-04-14 03:13:36 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.security.doas;
|
|
|
|
|
|
|
|
inherit (pkgs) doas;
|
|
|
|
|
|
|
|
mkUsrString = user: toString user;
|
|
|
|
|
|
|
|
mkGrpString = group: ":${toString group}";
|
|
|
|
|
|
|
|
mkOpts = rule: concatStringsSep " " [
|
|
|
|
(optionalString rule.noPass "nopass")
|
2020-11-15 03:15:11 +00:00
|
|
|
(optionalString rule.noLog "nolog")
|
2020-04-14 03:13:36 +00:00
|
|
|
(optionalString rule.persist "persist")
|
|
|
|
(optionalString rule.keepEnv "keepenv")
|
2021-09-05 12:35:40 +00:00
|
|
|
"setenv { SSH_AUTH_SOCK TERMINFO TERMINFO_DIRS ${concatStringsSep " " rule.setEnv} }"
|
2020-04-14 03:13:36 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
mkArgs = rule:
|
2023-03-05 21:08:45 +00:00
|
|
|
if (rule.args == null) then ""
|
2020-04-14 03:13:36 +00:00
|
|
|
else if (length rule.args == 0) then "args"
|
|
|
|
else "args ${concatStringsSep " " rule.args}";
|
|
|
|
|
|
|
|
mkRule = rule:
|
|
|
|
let
|
|
|
|
opts = mkOpts rule;
|
|
|
|
|
2023-03-05 21:08:45 +00:00
|
|
|
as = optionalString (rule.runAs != null) "as ${rule.runAs}";
|
2020-04-14 03:13:36 +00:00
|
|
|
|
2023-03-05 21:08:45 +00:00
|
|
|
cmd = optionalString (rule.cmd != null) "cmd ${rule.cmd}";
|
2020-04-14 03:13:36 +00:00
|
|
|
|
|
|
|
args = mkArgs rule;
|
|
|
|
in
|
|
|
|
optionals (length cfg.extraRules > 0) [
|
|
|
|
(
|
|
|
|
optionalString (length rule.users > 0)
|
|
|
|
(map (usr: "permit ${opts} ${mkUsrString usr} ${as} ${cmd} ${args}") rule.users)
|
|
|
|
)
|
|
|
|
(
|
|
|
|
optionalString (length rule.groups > 0)
|
|
|
|
(map (grp: "permit ${opts} ${mkGrpString grp} ${as} ${cmd} ${args}") rule.groups)
|
|
|
|
)
|
|
|
|
];
|
|
|
|
in
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options.security.doas = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
type = with types; bool;
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to enable the {command}`doas` command, which allows
|
2020-04-14 03:13:36 +00:00
|
|
|
non-root users to execute commands as root.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
wheelNeedsPassword = mkOption {
|
|
|
|
type = with types; bool;
|
|
|
|
default = true;
|
2022-08-03 20:46:41 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether users of the `wheel` group must provide a password to
|
|
|
|
run commands as super user via {command}`doas`.
|
2020-04-14 03:13:36 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraRules = mkOption {
|
|
|
|
default = [];
|
2022-08-03 20:46:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-14 03:13:36 +00:00
|
|
|
Define specific rules to be set in the
|
2022-08-03 20:46:41 +00:00
|
|
|
{file}`/etc/doas.conf` file. More specific rules should
|
2020-04-14 03:13:36 +00:00
|
|
|
come after more general ones in order to yield the expected behavior.
|
2022-08-03 20:46:41 +00:00
|
|
|
You can use `mkBefore` and/or `mkAfter` to ensure
|
2020-04-14 03:13:36 +00:00
|
|
|
this is the case when configuration options are merged.
|
|
|
|
'';
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2020-04-14 03:13:36 +00:00
|
|
|
[
|
|
|
|
# Allow execution of any command by any user in group doas, requiring
|
|
|
|
# a password and keeping any previously-defined environment variables.
|
|
|
|
{ groups = [ "doas" ]; noPass = false; keepEnv = true; }
|
|
|
|
|
|
|
|
# Allow execution of "/home/root/secret.sh" by user `backup` OR user
|
|
|
|
# `database` OR any member of the group with GID `1006`, without a
|
|
|
|
# password.
|
|
|
|
{ users = [ "backup" "database" ]; groups = [ 1006 ];
|
|
|
|
cmd = "/home/root/secret.sh"; noPass = true; }
|
|
|
|
|
|
|
|
# Allow any member of group `bar` to run `/home/baz/cmd1.sh` as user
|
|
|
|
# `foo` with argument `hello-doas`.
|
|
|
|
{ groups = [ "bar" ]; runAs = "foo";
|
|
|
|
cmd = "/home/baz/cmd1.sh"; args = [ "hello-doas" ]; }
|
|
|
|
|
|
|
|
# Allow any member of group `bar` to run `/home/baz/cmd2.sh` as user
|
|
|
|
# `foo` with no arguments.
|
|
|
|
{ groups = [ "bar" ]; runAs = "foo";
|
|
|
|
cmd = "/home/baz/cmd2.sh"; args = [ ]; }
|
|
|
|
|
|
|
|
# Allow user `abusers` to execute "nano" and unset the value of
|
|
|
|
# SSH_AUTH_SOCK, override the value of ALPHA to 1, and inherit the
|
|
|
|
# value of BETA from the current environment.
|
|
|
|
{ users = [ "abusers" ]; cmd = "nano";
|
|
|
|
setEnv = [ "-SSH_AUTH_SOCK" "ALPHA=1" "BETA" ]; }
|
|
|
|
]
|
|
|
|
'';
|
|
|
|
type = with types; listOf (
|
|
|
|
submodule {
|
|
|
|
options = {
|
|
|
|
|
|
|
|
noPass = mkOption {
|
|
|
|
type = with types; bool;
|
|
|
|
default = false;
|
2022-08-03 20:46:41 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
If `true`, the user is not required to enter a
|
2020-04-14 03:13:36 +00:00
|
|
|
password.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-11-15 03:15:11 +00:00
|
|
|
noLog = mkOption {
|
|
|
|
type = with types; bool;
|
|
|
|
default = false;
|
2022-08-03 20:46:41 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
If `true`, successful executions will not be logged
|
2020-11-15 03:15:11 +00:00
|
|
|
to
|
2022-08-03 20:46:41 +00:00
|
|
|
{manpage}`syslogd(8)`.
|
2020-11-15 03:15:11 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-04-14 03:13:36 +00:00
|
|
|
persist = mkOption {
|
|
|
|
type = with types; bool;
|
|
|
|
default = false;
|
2022-08-03 20:46:41 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
If `true`, do not ask for a password again for some
|
2020-04-14 03:13:36 +00:00
|
|
|
time after the user successfully authenticates.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
keepEnv = mkOption {
|
|
|
|
type = with types; bool;
|
|
|
|
default = false;
|
2022-08-03 20:46:41 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
If `true`, environment variables other than those
|
2020-04-14 03:13:36 +00:00
|
|
|
listed in
|
2022-08-03 20:46:41 +00:00
|
|
|
{manpage}`doas(1)`
|
2020-04-14 03:13:36 +00:00
|
|
|
are kept when creating the environment for the new process.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
setEnv = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [];
|
2022-08-03 20:46:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-14 03:13:36 +00:00
|
|
|
Keep or set the specified variables. Variables may also be
|
|
|
|
removed with a leading '-' or set using
|
2022-08-03 20:46:41 +00:00
|
|
|
`variable=value`. If the first character of
|
|
|
|
`value` is a '$', the value to be set is taken from
|
2020-04-14 03:13:36 +00:00
|
|
|
the existing environment variable of the indicated name. This
|
|
|
|
option is processed after the default environment has been
|
|
|
|
created.
|
|
|
|
|
2022-08-03 20:46:41 +00:00
|
|
|
NOTE: All rules have `setenv { SSH_AUTH_SOCK }` by
|
|
|
|
default. To prevent `SSH_AUTH_SOCK` from being
|
|
|
|
inherited, add `"-SSH_AUTH_SOCK"` anywhere in this
|
2020-04-14 03:13:36 +00:00
|
|
|
list.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
users = mkOption {
|
|
|
|
type = with types; listOf (either str int);
|
|
|
|
default = [];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "The usernames / UIDs this rule should apply for.";
|
2020-04-14 03:13:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
groups = mkOption {
|
|
|
|
type = with types; listOf (either str int);
|
|
|
|
default = [];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "The groups / GIDs this rule should apply for.";
|
2020-04-14 03:13:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
runAs = mkOption {
|
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
2022-08-03 20:46:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-14 03:13:36 +00:00
|
|
|
Which user or group the specified command is allowed to run as.
|
2022-08-03 20:46:41 +00:00
|
|
|
When set to `null` (the default), all users are
|
2020-04-14 03:13:36 +00:00
|
|
|
allowed.
|
|
|
|
|
|
|
|
A user can be specified using just the username:
|
2022-08-03 20:46:41 +00:00
|
|
|
`"foo"`. It is also possible to only allow running as
|
|
|
|
a specific group with `":bar"`.
|
2020-04-14 03:13:36 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
cmd = mkOption {
|
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
2022-08-03 20:46:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-14 03:13:36 +00:00
|
|
|
The command the user is allowed to run. When set to
|
2022-08-03 20:46:41 +00:00
|
|
|
`null` (the default), all commands are allowed.
|
2020-04-14 03:13:36 +00:00
|
|
|
|
|
|
|
NOTE: It is best practice to specify absolute paths. If a
|
|
|
|
relative path is specified, only a restricted PATH will be
|
|
|
|
searched.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
args = mkOption {
|
|
|
|
type = with types; nullOr (listOf str);
|
|
|
|
default = null;
|
2022-08-03 20:46:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-14 03:13:36 +00:00
|
|
|
Arguments that must be provided to the command. When set to
|
2022-08-03 20:46:41 +00:00
|
|
|
`[]`, the command must be run without any arguments.
|
2020-04-14 03:13:36 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = with types; lines;
|
|
|
|
default = "";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Extra configuration text appended to {file}`doas.conf`.
|
2020-04-14 03:13:36 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2020-05-11 05:14:16 +00:00
|
|
|
security.doas.extraRules = mkOrder 600 [
|
2020-04-14 03:13:36 +00:00
|
|
|
{
|
|
|
|
groups = [ "wheel" ];
|
|
|
|
noPass = !cfg.wheelNeedsPassword;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-09-12 16:53:48 +00:00
|
|
|
security.wrappers.doas =
|
|
|
|
{ setuid = true;
|
|
|
|
owner = "root";
|
|
|
|
group = "root";
|
|
|
|
source = "${doas}/bin/doas";
|
|
|
|
};
|
2020-04-14 03:13:36 +00:00
|
|
|
|
|
|
|
environment.systemPackages = [
|
|
|
|
doas
|
|
|
|
];
|
|
|
|
|
|
|
|
security.pam.services.doas = {
|
|
|
|
allowNullPassword = true;
|
|
|
|
sshAgentAuth = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
environment.etc."doas.conf" = {
|
|
|
|
source = pkgs.runCommand "doas-conf"
|
|
|
|
{
|
|
|
|
src = pkgs.writeText "doas-conf-in" ''
|
|
|
|
# To modify this file, set the NixOS options
|
|
|
|
# `security.doas.extraRules` or `security.doas.extraConfig`. To
|
|
|
|
# completely replace the contents of this file, use
|
|
|
|
# `environment.etc."doas.conf"`.
|
|
|
|
|
|
|
|
# "root" is allowed to do anything.
|
|
|
|
permit nopass keepenv root
|
|
|
|
|
|
|
|
# extraRules
|
|
|
|
${concatStringsSep "\n" (lists.flatten (map mkRule cfg.extraRules))}
|
|
|
|
|
|
|
|
# extraConfig
|
|
|
|
${cfg.extraConfig}
|
|
|
|
'';
|
|
|
|
preferLocalBuild = true;
|
|
|
|
}
|
|
|
|
# Make sure that the doas.conf file is syntactically valid.
|
|
|
|
"${pkgs.buildPackages.doas}/bin/doas -C $src && cp $src $out";
|
|
|
|
mode = "0440";
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
meta.maintainers = with maintainers; [ cole-h ];
|
|
|
|
}
|