summary refs log tree commit diff
path: root/nixos/modules/config/krb5/default.nix
blob: c2302451d702f6dbd0690f20fd4df35ce094b50f (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.krb5;

  # This is to provide support for old configuration options (as much as is
  # reasonable). This can be removed after 18.03 was released.
  defaultConfig = {
    libdefaults = optionalAttrs (cfg.defaultRealm != null)
      { default_realm = cfg.defaultRealm; };

    realms = optionalAttrs (lib.all (value: value != null) [
      cfg.defaultRealm cfg.kdc cfg.kerberosAdminServer
    ]) {
      ${cfg.defaultRealm} = {
        kdc = cfg.kdc;
        admin_server = cfg.kerberosAdminServer;
      };
    };

    domain_realm = optionalAttrs (lib.all (value: value != null) [
      cfg.domainRealm cfg.defaultRealm
    ]) {
      ".${cfg.domainRealm}" = cfg.defaultRealm;
      ${cfg.domainRealm} = cfg.defaultRealm;
    };
  };

  mergedConfig = (recursiveUpdate defaultConfig {
    inherit (config.krb5)
      kerberos libdefaults realms domain_realm capaths appdefaults plugins
      extraConfig config;
  });

  filterEmbeddedMetadata = value: if isAttrs value then
    (filterAttrs
      (attrName: attrValue: attrName != "_module" && attrValue != null)
        value)
    else value;

  indent = "  ";

  mkRelation = name: value:
    if (isList value) then
      concatMapStringsSep "\n" (mkRelation name) value
    else "${name} = ${mkVal value}";

  mkVal = value:
    if (value == true) then "true"
    else if (value == false) then "false"
    else if (isInt value) then (toString value)
    else if (isAttrs value) then
      let configLines = concatLists
        (map (splitString "\n")
          (mapAttrsToList mkRelation value));
      in
      (concatStringsSep "\n${indent}"
        ([ "{" ] ++ configLines))
      + "\n}"
    else value;

  mkMappedAttrsOrString = value: concatMapStringsSep "\n"
    (line: if builtins.stringLength line > 0
      then "${indent}${line}"
      else line)
    (splitString "\n"
      (if isAttrs value then
        concatStringsSep "\n"
            (mapAttrsToList mkRelation value)
        else value));

in {

  ###### interface

  options = {
    krb5 = {
      enable = mkEnableOption "building krb5.conf, configuration file for Kerberos V";

      kerberos = mkOption {
        type = types.package;
        default = pkgs.krb5Full;
        defaultText = "pkgs.krb5Full";
        example = literalExample "pkgs.heimdalFull";
        description = ''
          The Kerberos implementation that will be present in
          <literal>environment.systemPackages</literal> after enabling this
          service.
        '';
      };

      libdefaults = mkOption {
        type = with types; either attrs lines;
        default = {};
        apply = attrs: filterEmbeddedMetadata attrs;
        example = literalExample ''
          {
            default_realm = "ATHENA.MIT.EDU";
          };
        '';
        description = ''
          Settings used by the Kerberos V5 library.
        '';
      };

      realms = mkOption {
        type = with types; either attrs lines;
        default = {};
        example = literalExample ''
          {
            "ATHENA.MIT.EDU" = {
              admin_server = "athena.mit.edu";
              kdc = [
                "athena01.mit.edu"
                "athena02.mit.edu"
              ];
            };
          };
        '';
        apply = attrs: filterEmbeddedMetadata attrs;
        description = "Realm-specific contact information and settings.";
      };

      domain_realm = mkOption {
        type = with types; either attrs lines;
        default = {};
        example = literalExample ''
          {
            "example.com" = "EXAMPLE.COM";
            ".example.com" = "EXAMPLE.COM";
          };
        '';
        apply = attrs: filterEmbeddedMetadata attrs;
        description = ''
          Map of server hostnames to Kerberos realms.
        '';
      };

      capaths = mkOption {
        type = with types; either attrs lines;
        default = {};
        example = literalExample ''
          {
            "ATHENA.MIT.EDU" = {
              "EXAMPLE.COM" = ".";
            };
            "EXAMPLE.COM" = {
              "ATHENA.MIT.EDU" = ".";
            };
          };
        '';
        apply = attrs: filterEmbeddedMetadata attrs;
        description = ''
          Authentication paths for non-hierarchical cross-realm authentication.
        '';
      };

      appdefaults = mkOption {
        type = with types; either attrs lines;
        default = {};
        example = literalExample ''
          {
            pam = {
              debug = false;
              ticket_lifetime = 36000;
              renew_lifetime = 36000;
              max_timeout = 30;
              timeout_shift = 2;
              initial_timeout = 1;
            };
          };
        '';
        apply = attrs: filterEmbeddedMetadata attrs;
        description = ''
          Settings used by some Kerberos V5 applications.
        '';
      };

      plugins = mkOption {
        type = with types; either attrs lines;
        default = {};
        example = literalExample ''
          {
            ccselect = {
              disable = "k5identity";
            };
          };
        '';
        apply = attrs: filterEmbeddedMetadata attrs;
        description = ''
          Controls plugin module registration.
        '';
      };

      extraConfig = mkOption {
        type = with types; nullOr lines;
        default = null;
        example = ''
          [logging]
            kdc          = SYSLOG:NOTICE
            admin_server = SYSLOG:NOTICE
            default      = SYSLOG:NOTICE
        '';
        description = ''
          These lines go to the end of <literal>krb5.conf</literal> verbatim.
          <literal>krb5.conf</literal> may include any of the relations that are
          valid for <literal>kdc.conf</literal> (see <literal>man
          kdc.conf</literal>), but it is not a recommended practice.
        '';
      };

      config = mkOption {
        type = with types; nullOr lines;
        default = null;
        example = ''
          [libdefaults]
            default_realm = EXAMPLE.COM

          [realms]
            EXAMPLE.COM = {
              admin_server = kerberos.example.com
              kdc = kerberos.example.com
              default_principal_flags = +preauth
            }

          [domain_realm]
            example.com  = EXAMPLE.COM
            .example.com = EXAMPLE.COM

          [logging]
            kdc          = SYSLOG:NOTICE
            admin_server = SYSLOG:NOTICE
            default      = SYSLOG:NOTICE
        '';
        description = ''
          Verbatim <literal>krb5.conf</literal> configuration.  Note that this
          is mutually exclusive with configuration via
          <literal>libdefaults</literal>, <literal>realms</literal>,
          <literal>domain_realm</literal>, <literal>capaths</literal>,
          <literal>appdefaults</literal>, <literal>plugins</literal> and
          <literal>extraConfig</literal> configuration options.  Consult
          <literal>man krb5.conf</literal> for documentation.
        '';
      };

      defaultRealm = mkOption {
        type = with types; nullOr str;
        default = null;
        example = "ATHENA.MIT.EDU";
        description = ''
          DEPRECATED, please use
          <literal>krb5.libdefaults.default_realm</literal>.
        '';
      };

      domainRealm = mkOption {
        type = with types; nullOr str;
        default = null;
        example = "athena.mit.edu";
        description = ''
          DEPRECATED, please create a map of server hostnames to Kerberos realms
          in <literal>krb5.domain_realm</literal>.
        '';
      };

      kdc = mkOption {
        type = with types; nullOr str;
        default = null;
        example = "kerberos.mit.edu";
        description = ''
          DEPRECATED, please pass a <literal>kdc</literal> attribute to a realm
          in <literal>krb5.realms</literal>.
        '';
      };

      kerberosAdminServer = mkOption {
        type = with types; nullOr str;
        default = null;
        example = "kerberos.mit.edu";
        description = ''
          DEPRECATED, please pass an <literal>admin_server</literal> attribute
          to a realm in <literal>krb5.realms</literal>.
        '';
      };
    };
  };

  ###### implementation

  config = mkIf cfg.enable {

    environment.systemPackages = [ cfg.kerberos ];

    environment.etc."krb5.conf".text = if isString cfg.config
      then cfg.config
      else (''
        [libdefaults]
        ${mkMappedAttrsOrString mergedConfig.libdefaults}

        [realms]
        ${mkMappedAttrsOrString mergedConfig.realms}

        [domain_realm]
        ${mkMappedAttrsOrString mergedConfig.domain_realm}

        [capaths]
        ${mkMappedAttrsOrString mergedConfig.capaths}

        [appdefaults]
        ${mkMappedAttrsOrString mergedConfig.appdefaults}

        [plugins]
        ${mkMappedAttrsOrString mergedConfig.plugins}
      '' + optionalString (mergedConfig.extraConfig != null)
          ("\n" + mergedConfig.extraConfig));

    warnings = flatten [
      (optional (cfg.defaultRealm != null) ''
        The option krb5.defaultRealm is deprecated, please use
        krb5.libdefaults.default_realm.
      '')
      (optional (cfg.domainRealm != null) ''
        The option krb5.domainRealm is deprecated, please use krb5.domain_realm.
      '')
      (optional (cfg.kdc != null) ''
        The option krb5.kdc is deprecated, please pass a kdc attribute to a
        realm in krb5.realms.
      '')
      (optional (cfg.kerberosAdminServer != null) ''
        The option krb5.kerberosAdminServer is deprecated, please pass an
        admin_server attribute to a realm in krb5.realms.
      '')
    ];

    assertions = [
      { assertion = !((builtins.any (value: value != null) [
            cfg.defaultRealm cfg.domainRealm cfg.kdc cfg.kerberosAdminServer
          ]) && ((builtins.any (value: value != {}) [
              cfg.libdefaults cfg.realms cfg.domain_realm cfg.capaths
              cfg.appdefaults cfg.plugins
            ]) || (builtins.any (value: value != null) [
              cfg.config cfg.extraConfig
            ])));
        message = ''
          Configuration of krb5.conf by deprecated options is mutually exclusive
          with configuration by section.  Please migrate your config using the
          attributes suggested in the warnings.
        '';
      }
      { assertion = !(cfg.config != null
          && ((builtins.any (value: value != {}) [
              cfg.libdefaults cfg.realms cfg.domain_realm cfg.capaths
              cfg.appdefaults cfg.plugins
            ]) || (builtins.any (value: value != null) [
              cfg.extraConfig cfg.defaultRealm cfg.domainRealm cfg.kdc
              cfg.kerberosAdminServer
            ])));
        message = ''
          Configuration of krb5.conf using krb.config is mutually exclusive with
          configuration by section.  If you want to mix the two, you can pass
          lines to any configuration section or lines to krb5.extraConfig.
        '';
      }
    ];
  };
}