2023-06-10 10:50:53 +00:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
with import ../../lib/pure.nix { inherit lib; };
|
|
|
|
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;
|
|
|
|
};
|
2022-01-26 11:48:24 +00:00
|
|
|
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]";
|
2016-02-27 12:10:59 +00:00
|
|
|
merge = mergeOneOption;
|
2016-02-14 12:26:37 +00:00
|
|
|
};
|
|
|
|
};
|
2021-02-05 16:48:54 +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
|
|
|
};
|
2021-02-05 16:48:54 +00:00
|
|
|
dst = "${cfg.wrapperDir}/${cfg.name}";
|
2022-01-27 04:37:06 +00:00
|
|
|
in /* sh */ ''
|
2021-02-05 16:48:54 +00:00
|
|
|
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}
|
2022-01-26 11:48:24 +00:00
|
|
|
${optionalString (cfg.capabilities != []) /* sh */ ''
|
|
|
|
${pkgs.libcap.out}/bin/setcap ${concatMapStringsSep "," shell.escape cfg.capabilities} ${dst}
|
|
|
|
''}
|
2016-02-14 12:26:37 +00:00
|
|
|
'';
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
imp = {
|
2023-12-06 21:30:12 +00:00
|
|
|
systemd.services."krebs.setuid" = {
|
|
|
|
wantedBy = [ "suid-sgid-wrappers.service" ];
|
|
|
|
after = [ "suid-sgid-wrappers.service" ];
|
|
|
|
path = [
|
|
|
|
pkgs.coreutils
|
|
|
|
];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = pkgs.writeDash "krebs.setuid.sh" ''
|
|
|
|
${concatMapStringsSep "\n"
|
|
|
|
(getAttr "activate")
|
|
|
|
(attrValues config.krebs.setuid)
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
unitConfig = {
|
|
|
|
DefaultDependencies = false;
|
|
|
|
};
|
|
|
|
};
|
2016-02-14 12:26:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
in out
|