summary refs log tree commit diff
path: root/nixos/modules/config/mysql.nix
blob: 95c9ba76663eac4092374f1bc5169409cfe28bc9 (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
{ config, pkgs, lib, ... }:

with lib;

let
  cfg = config.users.mysql;
in
{
  options = {
    users.mysql = {
      enable = mkEnableOption (lib.mdDoc "Authentication against a MySQL/MariaDB database");
      host = mkOption {
        type = types.str;
        example = "localhost";
        description = lib.mdDoc "The hostname of the MySQL/MariaDB server";
      };
      database = mkOption {
        type = types.str;
        example = "auth";
        description = lib.mdDoc "The name of the database containing the users";
      };
      user = mkOption {
        type = types.str;
        example = "nss-user";
        description = lib.mdDoc "The username to use when connecting to the database";
      };
      passwordFile = mkOption {
        type = types.path;
        example = "/run/secrets/mysql-auth-db-passwd";
        description = lib.mdDoc "The path to the file containing the password for the user";
      };
      pam = mkOption {
        description = lib.mdDoc "Settings for `pam_mysql`";
        type = types.submodule {
          options = {
            table = mkOption {
              type = types.str;
              example = "users";
              description = lib.mdDoc "The name of table that maps unique login names to the passwords.";
            };
            updateTable = mkOption {
              type = types.nullOr types.str;
              default = null;
              example = "users_updates";
              description = lib.mdDoc ''
                The name of the table used for password alteration. If not defined, the value
                of the `table` option will be used instead.
              '';
            };
            userColumn = mkOption {
              type = types.str;
              example = "username";
              description = lib.mdDoc "The name of the column that contains a unix login name.";
            };
            passwordColumn = mkOption {
              type = types.str;
              example = "password";
              description = lib.mdDoc "The name of the column that contains a (encrypted) password string.";
            };
            statusColumn = mkOption {
              type = types.nullOr types.str;
              default = null;
              example = "status";
              description = lib.mdDoc ''
                The name of the column or an SQL expression that indicates the status of
                the user. The status is expressed by the combination of two bitfields
                shown below:

                - `bit 0 (0x01)`:
                   if flagged, `pam_mysql` deems the account to be expired and
                   returns `PAM_ACCT_EXPIRED`. That is, the account is supposed
                   to no longer be available. Note this doesn't mean that `pam_mysql`
                   rejects further authentication operations.
                -  `bit 1 (0x02)`:
                   if flagged, `pam_mysql` deems the authentication token
                   (password) to be expired and returns `PAM_NEW_AUTHTOK_REQD`.
                   This ends up requiring that the user enter a new password.
              '';
            };
            passwordCrypt = mkOption {
              example = "2";
              type = types.enum [
                "0" "plain"
                "1" "Y"
                "2" "mysql"
                "3" "md5"
                "4" "sha1"
                "5" "drupal7"
                "6" "joomla15"
                "7" "ssha"
                "8" "sha512"
                "9" "sha256"
              ];
              description = lib.mdDoc ''
                The method to encrypt the user's password:

                - `0` (or `"plain"`):
                  No encryption. Passwords are stored in plaintext. HIGHLY DISCOURAGED.
                - `1` (or `"Y"`):
                  Use crypt(3) function.
                - `2` (or `"mysql"`):
                  Use the MySQL PASSWORD() function. It is possible that the encryption function used
                  by `pam_mysql` is different from that of the MySQL server, as
                  `pam_mysql` uses the function defined in MySQL's C-client API
                  instead of using PASSWORD() SQL function in the query.
                - `3` (or `"md5"`):
                  Use plain hex MD5.
                - `4` (or `"sha1"`):
                  Use plain hex SHA1.
                - `5` (or `"drupal7"`):
                  Use Drupal7 salted passwords.
                - `6` (or `"joomla15"`):
                  Use Joomla15 salted passwords.
                - `7` (or `"ssha"`):
                  Use ssha hashed passwords.
                - `8` (or `"sha512"`):
                  Use sha512 hashed passwords.
                - `9` (or `"sha256"`):
                  Use sha256 hashed passwords.
              '';
            };
            cryptDefault = mkOption {
              type = types.nullOr (types.enum [ "md5" "sha256" "sha512" "blowfish" ]);
              default = null;
              example = "blowfish";
              description = lib.mdDoc "The default encryption method to use for `passwordCrypt = 1`.";
            };
            where = mkOption {
              type = types.nullOr types.str;
              default = null;
              example = "host.name='web' AND user.active=1";
              description = lib.mdDoc "Additional criteria for the query.";
            };
            verbose = mkOption {
              type = types.bool;
              default = false;
              description = lib.mdDoc ''
                If enabled, produces logs with detailed messages that describes what
                `pam_mysql` is doing. May be useful for debugging.
              '';
            };
            disconnectEveryOperation = mkOption {
              type = types.bool;
              default = false;
              description = lib.mdDoc ''
                By default, `pam_mysql` keeps the connection to the MySQL
                database until the session is closed. If this option is set to true it
                disconnects every time the PAM operation has finished. This option may
                be useful in case the session lasts quite long.
              '';
            };
            logging = {
              enable = mkOption {
                type = types.bool;
                default = false;
                description = lib.mdDoc "Enables logging of authentication attempts in the MySQL database.";
              };
              table = mkOption {
                type = types.str;
                example = "logs";
                description = lib.mdDoc "The name of the table to which logs are written.";
              };
              msgColumn = mkOption {
                type = types.str;
                example = "msg";
                description = lib.mdDoc ''
                  The name of the column in the log table to which the description
                  of the performed operation is stored.
                '';
              };
              userColumn = mkOption {
                type = types.str;
                example = "user";
                description = lib.mdDoc ''
                  The name of the column in the log table to which the name of the
                  user being authenticated is stored.
                '';
              };
              pidColumn = mkOption {
                type = types.str;
                example = "pid";
                description = lib.mdDoc ''
                  The name of the column in the log table to which the pid of the
                  process utilising the `pam_mysql` authentication
                  service is stored.
                '';
              };
              hostColumn = mkOption {
                type = types.str;
                example = "host";
                description = lib.mdDoc ''
                  The name of the column in the log table to which the name of the user
                  being authenticated is stored.
                '';
              };
              rHostColumn = mkOption {
                type = types.str;
                example = "rhost";
                description = lib.mdDoc ''
                  The name of the column in the log table to which the name of the remote
                  host that initiates the session is stored. The value is supposed to be
                  set by the PAM-aware application with `pam_set_item(PAM_RHOST)`.
                '';
              };
              timeColumn = mkOption {
                type = types.str;
                example = "timestamp";
                description = lib.mdDoc ''
                  The name of the column in the log table to which the timestamp of the
                  log entry is stored.
                '';
              };
            };
          };
        };
      };
      nss = mkOption {
        description = lib.mdDoc ''
          Settings for `libnss-mysql`.

          All examples are from the [minimal example](https://github.com/saknopper/libnss-mysql/tree/master/sample/minimal)
          of `libnss-mysql`, but they are modified with NixOS paths for bash.
        '';
        type = types.submodule {
          options = {
            getpwnam = mkOption {
              type = types.nullOr types.str;
              default = null;
              example = literalExpression ''
                SELECT username,'x',uid,'5000','MySQL User', CONCAT('/home/',username),'/run/sw/current-system/bin/bash' \
                FROM users \
                WHERE username='%1$s' \
                LIMIT 1
              '';
              description = lib.mdDoc ''
                SQL query for the [getpwnam](https://man7.org/linux/man-pages/man3/getpwnam.3.html)
                syscall.
              '';
            };
            getpwuid = mkOption {
              type = types.nullOr types.str;
              default = null;
              example = literalExpression ''
                SELECT username,'x',uid,'5000','MySQL User', CONCAT('/home/',username),'/run/sw/current-system/bin/bash' \
                FROM users \
                WHERE uid='%1$u' \
                LIMIT 1
              '';
              description = lib.mdDoc ''
                SQL query for the [getpwuid](https://man7.org/linux/man-pages/man3/getpwuid.3.html)
                syscall.
              '';
            };
            getspnam = mkOption {
              type = types.nullOr types.str;
              default = null;
              example = literalExpression ''
                SELECT username,password,'1','0','99999','0','0','-1','0' \
                FROM users \
                WHERE username='%1$s' \
                LIMIT 1
              '';
              description = lib.mdDoc ''
                SQL query for the [getspnam](https://man7.org/linux/man-pages/man3/getspnam.3.html)
                syscall.
              '';
            };
            getpwent = mkOption {
              type = types.nullOr types.str;
              default = null;
              example = literalExpression ''
                SELECT username,'x',uid,'5000','MySQL User', CONCAT('/home/',username),'/run/sw/current-system/bin/bash' FROM users
              '';
              description = lib.mdDoc ''
                SQL query for the [getpwent](https://man7.org/linux/man-pages/man3/getpwent.3.html)
                syscall.
              '';
            };
            getspent = mkOption {
              type = types.nullOr types.str;
              default = null;
              example = literalExpression ''
                SELECT username,password,'1','0','99999','0','0','-1','0' FROM users
              '';
              description = lib.mdDoc ''
                SQL query for the [getspent](https://man7.org/linux/man-pages/man3/getspent.3.html)
                syscall.
              '';
            };
            getgrnam = mkOption {
              type = types.nullOr types.str;
              default = null;
              example = literalExpression ''
                SELECT name,password,gid FROM groups WHERE name='%1$s' LIMIT 1
              '';
              description = lib.mdDoc ''
                SQL query for the [getgrnam](https://man7.org/linux/man-pages/man3/getgrnam.3.html)
                syscall.
              '';
            };
            getgrgid = mkOption {
              type = types.nullOr types.str;
              default = null;
              example = literalExpression ''
                SELECT name,password,gid FROM groups WHERE gid='%1$u' LIMIT 1
              '';
              description = lib.mdDoc ''
                SQL query for the [getgrgid](https://man7.org/linux/man-pages/man3/getgrgid.3.html)
                syscall.
              '';
            };
            getgrent = mkOption {
              type = types.nullOr types.str;
              default = null;
              example = literalExpression ''
                SELECT name,password,gid FROM groups
              '';
              description = lib.mdDoc ''
                SQL query for the [getgrent](https://man7.org/linux/man-pages/man3/getgrent.3.html)
                syscall.
              '';
            };
            memsbygid = mkOption {
              type = types.nullOr types.str;
              default = null;
              example = literalExpression ''
                SELECT username FROM grouplist WHERE gid='%1$u'
              '';
              description = lib.mdDoc ''
                SQL query for the [memsbygid](https://man7.org/linux/man-pages/man3/memsbygid.3.html)
                syscall.
              '';
            };
            gidsbymem = mkOption {
              type = types.nullOr types.str;
              default = null;
              example = literalExpression ''
                SELECT gid FROM grouplist WHERE username='%1$s'
              '';
              description = lib.mdDoc ''
                SQL query for the [gidsbymem](https://man7.org/linux/man-pages/man3/gidsbymem.3.html)
                syscall.
              '';
            };
          };
        };
      };
    };
  };

  config = mkIf cfg.enable {
    system.nssModules = [ pkgs.libnss-mysql ];
    system.nssDatabases.shadow = [ "mysql" ];
    system.nssDatabases.group = [ "mysql" ];
    system.nssDatabases.passwd = [ "mysql" ];

    environment.etc."security/pam_mysql.conf" = {
      user = "root";
      group = "root";
      mode = "0600";
      # password will be added from password file in activation script
      text = ''
        users.host=${cfg.host}
        users.db_user=${cfg.user}
        users.database=${cfg.database}
        users.table=${cfg.pam.table}
        users.user_column=${cfg.pam.userColumn}
        users.password_column=${cfg.pam.passwordColumn}
        users.password_crypt=${cfg.pam.passwordCrypt}
        users.disconnect_every_operation=${if cfg.pam.disconnectEveryOperation then "1" else "0"}
        verbose=${if cfg.pam.verbose then "1" else "0"}
      '' + optionalString (cfg.pam.cryptDefault != null) ''
        users.use_${cfg.pam.cryptDefault}=1
      '' + optionalString (cfg.pam.where != null) ''
        users.where_clause=${cfg.pam.where}
      '' + optionalString (cfg.pam.statusColumn != null) ''
        users.status_column=${cfg.pam.statusColumn}
      '' + optionalString (cfg.pam.updateTable != null) ''
        users.update_table=${cfg.pam.updateTable}
      '' + optionalString cfg.pam.logging.enable ''
        log.enabled=true
        log.table=${cfg.pam.logging.table}
        log.message_column=${cfg.pam.logging.msgColumn}
        log.pid_column=${cfg.pam.logging.pidColumn}
        log.user_column=${cfg.pam.logging.userColumn}
        log.host_column=${cfg.pam.logging.hostColumn}
        log.rhost_column=${cfg.pam.logging.rHostColumn}
        log.time_column=${cfg.pam.logging.timeColumn}
      '';
    };

    environment.etc."libnss-mysql.cfg" = {
      mode = "0600";
      user = config.services.nscd.user;
      group = config.services.nscd.group;
      text = optionalString (cfg.nss.getpwnam != null) ''
        getpwnam ${cfg.nss.getpwnam}
      '' + optionalString (cfg.nss.getpwuid != null) ''
        getpwuid ${cfg.nss.getpwuid}
      '' + optionalString (cfg.nss.getspnam != null) ''
        getspnam ${cfg.nss.getspnam}
      '' + optionalString (cfg.nss.getpwent != null) ''
        getpwent ${cfg.nss.getpwent}
      '' + optionalString (cfg.nss.getspent != null) ''
        getspent ${cfg.nss.getspent}
      '' + optionalString (cfg.nss.getgrnam != null) ''
        getgrnam ${cfg.nss.getgrnam}
      '' + optionalString (cfg.nss.getgrgid != null) ''
        getgrgid ${cfg.nss.getgrgid}
      '' + optionalString (cfg.nss.getgrent != null) ''
        getgrent ${cfg.nss.getgrent}
      '' + optionalString (cfg.nss.memsbygid != null) ''
        memsbygid ${cfg.nss.memsbygid}
      '' + optionalString (cfg.nss.gidsbymem != null) ''
        gidsbymem ${cfg.nss.gidsbymem}
      '' + ''
        host ${cfg.host}
        database ${cfg.database}
      '';
    };

    environment.etc."libnss-mysql-root.cfg" = {
      mode = "0600";
      user = config.services.nscd.user;
      group = config.services.nscd.group;
      # password will be added from password file in activation script
      text = ''
        username ${cfg.user}
      '';
    };

    # preStart script to append the password from the password file
    # to the configuration files. It also fixes the owner of the
    # libnss-mysql-root.cfg because it is changed to root after the
    # password is appended.
    systemd.services.mysql.preStart = ''
      if [[ -r ${cfg.passwordFile} ]]; then
        org_umask=$(umask)
        umask 0077

        conf_nss="$(mktemp)"
        cp /etc/libnss-mysql-root.cfg $conf_nss
        printf 'password %s\n' "$(cat ${cfg.passwordFile})" >> $conf_nss
        mv -fT "$conf_nss" /etc/libnss-mysql-root.cfg
        chown ${config.services.nscd.user}:${config.services.nscd.group} /etc/libnss-mysql-root.cfg

        conf_pam="$(mktemp)"
        cp /etc/security/pam_mysql.conf $conf_pam
        printf 'users.db_passwd=%s\n' "$(cat ${cfg.passwordFile})" >> $conf_pam
        mv -fT "$conf_pam" /etc/security/pam_mysql.conf

        umask $org_umask
      fi
    '';
  };
}