summary refs log tree commit diff
path: root/nixos/modules/services/networking/gale.nix
blob: f4c75c17290f2e55907031346e94de9f7bee5994 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.gale;
  # we convert the path to a string to avoid it being copied to the nix store,
  # otherwise users could read the private key as all files in the store are
  # world-readable
  keyPath = toString cfg.keyPath;
  # ...but we refer to the pubkey file using a path so that we can ensure the
  # config gets rebuilt if the public key changes (we can assume the private key
  # will never change without the public key having changed)
  gpubFile = cfg.keyPath + "/${cfg.domain}.gpub";
  home = "/var/lib/gale";
  keysPrepared = cfg.keyPath != null && lib.pathExists cfg.keyPath;
in
{
  options = {
    services.gale = {
      enable = mkEnableOption "the Gale messaging daemon";

      user = mkOption {
        default = "gale";
        type = types.str;
        description = "Username for the Gale daemon.";
      };

      group = mkOption {
        default = "gale";
        type = types.str;
        description = "Group name for the Gale daemon.";
      };

      setuidWrapper = mkOption {
        default = null;
        description = "Configuration for the Gale gksign setuid wrapper.";
      };

      domain = mkOption {
        default = "";
        type = types.str;
        description = "Domain name for the Gale system.";
      };

      keyPath = mkOption {
        default = null;
        type = types.nullOr types.path;
        description = ''
          Directory containing the key pair for this Gale domain.  The expected
          filename will be taken from the domain option with ".gpri" and ".gpub"
          appended.
        '';
      };

      extraConfig = mkOption {
        type = types.lines;
        default = "";
        description = ''
          Additional text to be added to <filename>/etc/gale/conf</filename>.
        '';
      };
    };
  };

  config = mkMerge [
    (mkIf cfg.enable {
       assertions = [{
         assertion = cfg.domain != "";
         message = "A domain must be set for Gale.";
       }];

       warnings = mkIf (!keysPrepared) [
         "You must run gale-install in order to generate a domain key."
       ];

       system.activationScripts.gale = mkIf cfg.enable (
         stringAfter [ "users" "groups" ] ''
           chmod 755 ${home}
           mkdir -m 0777 -p ${home}/auth/cache
           mkdir -m 1777 -p ${home}/auth/local # GALE_DOMAIN.gpub
           mkdir -m 0700 -p ${home}/auth/private # ROOT.gpub
           mkdir -m 0755 -p ${home}/auth/trusted # ROOT
           mkdir -m 0700 -p ${home}/.gale
           mkdir -m 0700 -p ${home}/.gale/auth
           mkdir -m 0700 -p ${home}/.gale/auth/private # GALE_DOMAIN.gpri

           ln -sf ${pkgs.gale}/etc/gale/auth/trusted/ROOT "${home}/auth/trusted/ROOT"
           chown ${cfg.user}:${cfg.group} ${home} ${home}/auth ${home}/auth/*
           chown ${cfg.user}:${cfg.group} ${home}/.gale ${home}/.gale/auth ${home}/.gale/auth/private
         ''
       );

       environment = {
         etc = {
           "gale/auth".source = home + "/auth"; # symlink /var/lib/gale/auth
           "gale/conf".text = ''
             GALE_USER ${cfg.user}
             GALE_DOMAIN ${cfg.domain}
             ${cfg.extraConfig}
           '';
         };

         systemPackages = [ pkgs.gale ];
       };

       users.extraUsers = [{
         name = cfg.user;
         description = "Gale daemon";
         uid = config.ids.uids.gale;
         group = cfg.group;
         home = home;
         createHome = true;
       }];

       users.extraGroups = [{
         name = cfg.group;
         gid = config.ids.gids.gale;
       }];
    })
    (mkIf (cfg.enable && keysPrepared) {
       assertions = [
         {
           assertion = cfg.keyPath != null
                    && lib.pathExists (cfg.keyPath + "/${cfg.domain}.gpub");
           message = "Couldn't find a Gale public key for ${cfg.domain}.";
         }
         {
           assertion = cfg.keyPath != null
                    && lib.pathExists (cfg.keyPath + "/${cfg.domain}.gpri");
           message = "Couldn't find a Gale private key for ${cfg.domain}.";
         }
       ];

       services.gale.setuidWrapper = {
         program = "gksign";
         source = "${pkgs.gale}/bin/gksign";
         owner = cfg.user;
         group = cfg.group;
         setuid = true;
         setgid = false;
       };

       security.wrappers.setuid = [ cfg.setuidWrapper ];

       systemd.services.gale-galed = {
         description = "Gale messaging daemon";
         wantedBy = [ "multi-user.target" ];
         wants = [ "gale-gdomain.service" ];
         after = [ "network.target" ];

         preStart = ''
           install -m 0640 -o ${cfg.user} -g ${cfg.group} ${keyPath}/${cfg.domain}.gpri "${home}/.gale/auth/private/"
           install -m 0644 -o ${cfg.user} -g ${cfg.group} ${gpubFile} "${home}/.gale/auth/private/${cfg.domain}.gpub"
           install -m 0644 -o ${cfg.user} -g ${cfg.group} ${gpubFile} "${home}/auth/local/${cfg.domain}.gpub"
         '';

         serviceConfig = {
           Type = "forking";
           ExecStart = "@${pkgs.gale}/bin/galed galed";
           User = cfg.user;
           Group = cfg.group;
           PermissionsStartOnly = true;
         };
       };

       systemd.services.gale-gdomain = {
         description = "Gale AKD daemon";
         wantedBy = [ "multi-user.target" ];
         requires = [ "gale-galed.service" ];
         after = [ "gale-galed.service" ];

         serviceConfig = {
           Type = "forking";
           ExecStart = "@${pkgs.gale}/bin/gdomain gdomain";
           User = cfg.user;
           Group = cfg.group;
         };
       };
    })
  ];
}