git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #91238 from rnhmjoj/users

nixos/users-groups: do not check validity of special hashes

commit01c4a388ee49f3df5c71fb2886c926beda660577 authorMichele Guerini Rocco <rnhmjoj@users.noreply.github.com> dateTue 7 Jul 2020, 10:57 (6 years ago) parent3734ac9c37347a
1 file changed+42−24
nixos/modules/config/users-groups.nix+42−24
nixos/modules/config/users-groups.nix+42−24
@@ -6,6 +6,16 @@ let
66 ids = config.ids;
77 cfg = config.users;
88
9+ # Check whether a password hash will allow login.
10+ allowsLogin = hash:
11+ hash == "" # login without password
12+ || !(lib.elem hash
13+ [ null # password login disabled
14+ "!" # password login disabled
15+ "!!" # a variant of "!"
16+ "*" # password unset
17+ ]);
18+
919 passwordDescription = ''
1020 The options <option>hashedPassword</option>,
1121 <option>password</option> and <option>passwordFile</option>
@@ -25,17 +35,19 @@ let
2535 '';
2636
2737 hashedPasswordDescription = ''
28− To generate hashed password install <literal>mkpasswd</literal>
38+ To generate a hashed password install the <literal>mkpasswd</literal>
2939 package and run <literal>mkpasswd -m sha-512</literal>.
3040
31− For password-less logins without password prompt, use
32− the empty string <literal>""</literal>.
33−
34− For logins with a fixed password (including the empty-string password with
35− prompt), use one of the un-hashed password options instead, such as
36− <option>users.users.&lt;name?&gt;.password</option>.
41+ If set to an empty string (<literal>""</literal>), this user will
42+ be able to log in without being asked for a password (but not via remote
43+ services such as SSH, or indirectly via <command>su</command> or
44+ <command>sudo</command>). This should only be used for e.g. bootable
45+ live systems. Note: this is different from setting an empty password,
46+ which ca be achieved using <option>users.users.&lt;name?&gt;.password</option>.
3747
38− Such unprotected logins should only be used for e.g. bootable live systems.
48+ If set to <literal>null</literal> (default) this user will not
49+ be able to log in using a password (i.e. via <command>login</command>
50+ command).
3951 '';
4052
4153 userOpts = { name, config, ... }: {
@@ -415,6 +427,12 @@ in {
415427 imports = [
416428 (mkAliasOptionModule [ "users" "extraUsers" ] [ "users" "users" ])
417429 (mkAliasOptionModule [ "users" "extraGroups" ] [ "users" "groups" ])
430+ (mkChangedOptionModule
431+ [ "security" "initialRootPassword" ]
432+ [ "users" "users" "root" "initialHashedPassword" ]
433+ (cfg: if cfg.security.initialHashedPassword == "!"
434+ then null
435+ else cfg.security.initialHashedPassword))
418436 ];
419437
420438 ###### interface
@@ -486,14 +504,6 @@ in {
486504 '';
487505 };
488506
489− # FIXME: obsolete - will remove.
490− security.initialRootPassword = mkOption {
491− type = types.str;
492− default = "!";
493− example = "";
494− visible = false;
495− };
496−
497507 };
498508
499509
@@ -508,7 +518,6 @@ in {
508518 home = "/root";
509519 shell = mkDefault cfg.defaultUserShell;
510520 group = "root";
511− initialHashedPassword = mkDefault config.security.initialRootPassword;
512521 };
513522 nobody = {
514523 uid = ids.uids.nobody;
@@ -597,7 +606,7 @@ in {
597606 || cfg.group == "wheel"
598607 || elem "wheel" cfg.extraGroups)
599608 &&
600− ((cfg.hashedPassword != null && cfg.hashedPassword != "!")
609+ (allowsLogin cfg.hashedPassword
601610 || cfg.password != null
602611 || cfg.passwordFile != null
603612 || cfg.openssh.authorizedKeys.keys != []
@@ -607,7 +616,17 @@ in {
607616 Neither the root account nor any wheel user has a password or SSH authorized key.
608617 You must set one to prevent being locked out of your system.'';
609618 }
610− ];
619+ ] ++ flip mapAttrsToList cfg.users (name: user:
620+ {
621+ assertion = (user.hashedPassword != null)
622+ -> (builtins.match ".*:.*" user.hashedPassword == null);
623+ message = ''
624+ The password hash of user "${name}" contains a ":" character.
625+ This is invalid and would break the login system because the fields
626+ of /etc/shadow (file where hashes are stored) are colon-separated.
627+ Please check the value of option `users.users."${name}".hashedPassword`.'';
628+ }
629+ );
611630
612631 warnings =
613632 builtins.filter (x: x != null) (
@@ -630,14 +649,13 @@ in {
630649 content = "${base64}${sep}${base64}";
631650 mcf = "^${sep}${scheme}${sep}${content}$";
632651 in
633− if (user.hashedPassword != null
652+ if (allowsLogin user.hashedPassword
653+ && user.hashedPassword != "" # login without password
634654 && builtins.match mcf user.hashedPassword == null)
635− then
636− ''
655+ then ''
637656 The password hash of user "${name}" may be invalid. You must set a
638657 valid hash or the user will be locked out of their account. Please
639− check the value of option `users.users."${name}".hashedPassword`.
640− ''
658+ check the value of option `users.users."${name}".hashedPassword`.''
641659 else null
642660 ));
643661