summaryrefslogtreecommitdiffstats
path: root/modules/slock/default.nix
blob: 4ac1a1eb5d9b01458fed456408837fd3414717e3 (plain)
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
# slock as a system service, so the OOM killer leaves it alone, started
# by its user through polkit.  With enstate, the password hash can come
# from the slot secrets/slock.hashed-password: then slock runs as its
# user, with that hash.  Without a hash, slock runs setuid root and reads
# /etc/shadow, like nixpkgs' programs.slock.
{ config, lib, mylib, pkgs, ... }: let
  cfg = config.hrm.slock;

  slot = config.enstate.channels.secrets.slots."slock.hashed-password";

  preload = pkgs.runCommandCC "slock-preload.so" {} /* sh */ ''
    gcc -Wall -Wextra -Werror -shared -fPIC -o $out ${./preload.c}
  '';

  validate = pkgs.runCommandCC "slock-validate" {
    buildInputs = [ pkgs.libxcrypt ];
  } /* sh */ ''
    gcc -Wall -Wextra -Werror -o $out ${./validate.c} -lcrypt
  '';
in {
  options.hrm.slock = {
    enable = lib.mkEnableOption "hrm.slock";
    package = lib.mkOption {
      default = pkgs.writeDashBin "slock" ''
        set -efu
        display=''${DISPLAY#:}
        service=slock-$LOGNAME@$display.service
        exec ${pkgs.systemd}/bin/systemctl start "$service"
      '';
      type = lib.types.package;
    };
    user = lib.mkOption {
      type = mylib.types.user;
    };
  };
  config = lib.mkIf cfg.enable (lib.mkMerge [
    {
      security.polkit.extraConfig = /* js */ ''
        polkit.addRule(function(action, subject) {
          if (action.id === "org.freedesktop.systemd1.manage-units" &&
              subject.user === ${builtins.toJSON cfg.user.name} &&
              /^slock-${cfg.user.name}@[0-9]+\.service$/.test(action.lookup("unit")) ) {
            return polkit.Result.YES;
          }
        });
      '';
      security.wrappers.slock = {
        setuid = true;
        owner = "root";
        group = "root";
        source = "${pkgs.slock}/bin/slock";
      };
      systemd.services."slock-${cfg.user.name}@" = {
        environment = {
          DISPLAY = ":%I";
        };
        restartIfChanged = false;
        serviceConfig = {
          ExecStart = pkgs.writeDash "slock-start" ''
            if test "$(${pkgs.coreutils}/bin/cat "$CREDENTIALS_DIRECTORY/hashed-password")" != none; then
              LD_PRELOAD=${preload} exec ${pkgs.slock}/bin/slock
            fi
            # Setuid slock opens the display as root.
            ${pkgs.xhost}/bin/xhost +SI:localuser:root >/dev/null
            exec /run/wrappers/bin/slock
          '';
          # The fallback when there's no hash: LoadCredential comes first.
          SetCredential = [ "hashed-password:none" ];
          OOMScoreAdjust = -1000;
          Restart = "on-failure";
          RestartSec = "100ms";
          StartLimitBurst = 0;
          SyslogIdentifier = "slock";
          User = cfg.user.name;
        };
      };
    }
    (lib.mkIf config.enstate.enable {
      # A hash the validator objects to is left out, so slock runs setuid.
      enstate.types."text/x-crypt-hash".validator = validate;
      enstate.channels.secrets.slots."slock.hashed-password" = {
        type = "text/x-crypt-hash";
        required = false;
        secret = true;
      };
      systemd.services."slock-${cfg.user.name}@".serviceConfig.LoadCredential = [
        "hashed-password:${slot.path}"
      ];
    })
  ]);
}