nix-community / impermanence

Modules to help you handle persistent state on systems with ephemeral root storage [maintainer=@talyz]
MIT License
1.03k stars 77 forks source link

Persisting passwords (/etc/shadow) #120

Open jordanisaacs opened 1 year ago

jordanisaacs commented 1 year ago

I would like to persist shadow without persisting the entire /etc directory. Are there any solutions to this? Mounting fails due to the way passwd works with renaming. I tried playing around with symlinking (manually) and that also seems to not work.

2xsaiko commented 1 year ago
environment.etc = {
  "group".source = "/nix/persist/etc/group";
  "passwd".source = "/nix/persist/etc/passwd";
  "shadow".source = "/nix/persist/etc/shadow";
};

This works for me. (Not sure if the other two are necessary, I assume probably not?)

talyz commented 1 year ago

The preferred solution is to use declarative users (setting mutableUsers = false and declaring passwords in the configuration). Does that not work for you? What issues are you seeing when you try to persist /etc/shadow?

jcszymansk commented 1 year ago

@2xsaiko It doesn't work for me. Linking all the three files (as @2xsaiko suggested) broke things for me when I tried to enable nginx afterwards (files were linked after the user nginx was created, effectively deleting it); linking only /etc/shadow doesn't work either - as soon as the first user changes their password, the file is no longer a link, but a regular file, to be recreated on reboot. Am I doing something wrong?

@talyz Declarative users cannot change their passwords, this may be a requirement (it is here...).

2xsaiko commented 1 year ago

Ahh, yeah shadow does get unlinked when I change the password, and I assume I would have the same issue with the rest of the files. I just haven't done that yet after setting this up so I didn't notice.

jcszymansk commented 1 year ago

I tried to modify pam to follow symlink to /etc/shadow on update; the change itself is quite straightforward, but:

I have no idea how to debug this without having to recompile the entire system on every change to pam, if someone wanted to help it's there: https://github.com/jacekszymanski/nur-packages/tree/master/pkgs/pam-impermalite/ ; I think I'll try to reproduce this abort on a non-nixos linux and if successful, go from there.

2xsaiko commented 1 year ago

FWIW after hitting the same problem as you when having to add a new user, I'm now using mutableUsers=false together with the users.users.USER.passwordFile option, which keeps the password (hash) out of the configuration which is mostly what I wanted.

jcszymansk commented 1 year ago

I got it, it was a "classic" off-by-one malloc error 🤦‍♀️ Had it right before my eyes and didn't see it...

But now it seems it can change users' passwords on symlinked shadow without errors, so linking by environment.etc should work. Of course, use at your own risk etc.

jacobranson commented 7 months ago

@jacekszymanski Is there any chance you'd consider trying to upstream the change to Pam?

jcszymansk commented 7 months ago

@jacekszymanski Is there any chance you'd consider trying to upstream the change to Pam?

No.

I gave this some thought several months back, but as PAM is at the very heart of the OS security, such a change would require a very thorough testing and auditing in many (and I mean many) possible setups. Given that this is a hack designed for an unsupported configuration of an unofficial customization of a niche Linux distro, I don't really think it'd ever happen.