stockholm/krebs/3modules/setuid.nix

92 lines
2.5 KiB
Nix
Raw Normal View History

2016-10-20 18:54:38 +00:00
with import <stockholm/lib>;
2021-02-05 16:43:24 +00:00
{ config, pkgs, ... }: let
2016-02-14 12:26:37 +00:00
out = {
options.krebs.setuid = api;
2021-02-05 16:43:24 +00:00
config = mkIf (config.krebs.setuid != {}) imp;
2016-02-14 12:26:37 +00:00
};
api = mkOption {
default = {};
type = let
inherit (config.users) groups users;
2021-02-05 16:43:24 +00:00
in types.attrsOf (types.submodule (self: let cfg = self.config; in {
2016-02-14 12:26:37 +00:00
options = {
name = mkOption {
type = types.filename;
2021-02-05 16:43:24 +00:00
default = cfg._module.args.name;
2016-02-14 12:26:37 +00:00
};
2016-06-04 22:31:36 +00:00
envp = mkOption {
2019-04-13 11:44:39 +00:00
type = types.nullOr (types.attrsOf types.str);
default = null;
2016-06-04 22:31:36 +00:00
};
2016-02-14 12:26:37 +00:00
filename = mkOption {
type = mkOptionType {
# TODO unyuck string and merge with toC
name = "derivation or string";
check = x:
isDerivation x ||
isString x;
};
apply = toString;
};
capabilities = mkOption {
default = [];
type = types.listOf types.str;
};
2016-02-14 12:26:37 +00:00
owner = mkOption {
default = "root";
type = types.enum (attrNames users);
};
group = mkOption {
default = "root";
type = types.enum (attrNames groups);
};
mode = mkOption {
default = "4710";
type = mkOptionType {
# TODO admit symbolic mode
name = "octal mode";
2017-06-18 13:36:18 +00:00
check = test "[0-7][0-7][0-7][0-7]";
merge = mergeOneOption;
2016-02-14 12:26:37 +00:00
};
};
wrapperDir = mkOption {
default = config.security.wrapperDir;
type = types.absolute-pathname;
};
2016-02-14 12:26:37 +00:00
activate = mkOption {
type = types.str;
visible = false;
readOnly = true;
};
};
config.activate = let
2021-02-05 16:43:24 +00:00
src = pkgs.exec cfg.name {
inherit (cfg) envp filename;
2016-02-14 12:26:37 +00:00
};
dst = "${cfg.wrapperDir}/${cfg.name}";
in /* sh */ ''
mkdir -p ${cfg.wrapperDir}
2016-02-14 12:26:37 +00:00
cp ${src} ${dst}
2022-10-31 17:26:10 +00:00
chown ${cfg.owner}:${cfg.group} ${dst}
2021-02-05 16:43:24 +00:00
chmod ${cfg.mode} ${dst}
${optionalString (cfg.capabilities != []) /* sh */ ''
${pkgs.libcap.out}/bin/setcap ${concatMapStringsSep "," shell.escape cfg.capabilities} ${dst}
''}
2016-02-14 12:26:37 +00:00
'';
}));
};
imp = {
system.activationScripts."krebs.setuid" = stringAfter [ "wrappers" ]
(concatMapStringsSep "\n"
(cfg: /* sh */ ''
${cfg.activate}
rm -f ${cfg.wrapperDir}/${cfg.name}.real
'')
(attrValues config.krebs.setuid));
2016-02-14 12:26:37 +00:00
};
in out