about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/security/please.nix
blob: ff4bfc9f1be1afa03c344c00f9215f63a788557a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.security.please;
  ini = pkgs.formats.ini { };
in
{
  options.security.please = {
    enable = mkEnableOption (mdDoc ''
      please, a Sudo clone which allows a users to execute a command or edit a
      file as another user
    '');

    package = mkPackageOption pkgs "please" { };

    wheelNeedsPassword = mkOption {
      type = types.bool;
      default = true;
      description = lib.mdDoc ''
        Whether users of the `wheel` group must provide a password to run
        commands or edit files with {command}`please` and
        {command}`pleaseedit` respectively.
      '';
    };

    settings = mkOption {
      type = ini.type;
      default = { };
      example = {
        jim_run_any_as_root = {
          name = "jim";
          type = "run";
          target = "root";
          rule = ".*";
          require_pass = false;
        };
        jim_edit_etc_hosts_as_root = {
          name = "jim";
          type = "edit";
          target = "root";
          rule = "/etc/hosts";
          editmode = 644;
          require_pass = true;
        };
      };
      description = mdDoc ''
        Please configuration. Refer to
        <https://github.com/edneville/please/blob/master/please.ini.md> for
        details.
      '';
    };
  };

  config = mkIf cfg.enable {
    security.wrappers =
      let
        owner = "root";
        group = "root";
        setuid = true;
      in
      {
        please = {
          source = "${cfg.package}/bin/please";
          inherit owner group setuid;
        };
        pleaseedit = {
          source = "${cfg.package}/bin/pleaseedit";
          inherit owner group setuid;
        };
      };

    security.please.settings = rec {
      # The "wheel" group is allowed to do anything by default but this can be
      # overridden.
      wheel_run_as_any = {
        type = "run";
        group = true;
        name = "wheel";
        target = ".*";
        rule = ".*";
        require_pass = cfg.wheelNeedsPassword;
      };
      wheel_edit_as_any = wheel_run_as_any // { type = "edit"; };
      wheel_list_as_any = wheel_run_as_any // { type = "list"; };
    };

    environment = {
      systemPackages = [ cfg.package ];

      etc."please.ini".source = ini.generate "please.ini"
        (cfg.settings // (rec {
          # The "root" user is allowed to do anything by default and this cannot
          # be overridden.
          root_run_as_any = {
            type = "run";
            name = "root";
            target = ".*";
            rule = ".*";
            require_pass = false;
          };
          root_edit_as_any = root_run_as_any // { type = "edit"; };
          root_list_as_any = root_run_as_any // { type = "list"; };
        }));
    };

    security.pam.services.please = {
      sshAgentAuth = true;
      usshAuth = true;
    };

    meta.maintainers = with maintainers; [ azahi ];
  };
}