Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move pam configuration to sudo_local #1020

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

dlubawy
Copy link
Contributor

@dlubawy dlubawy commented Jul 27, 2024

Addresses #985 and #787.

  • Moves pam configuration to /etc/pam.d/sudo_local
  • Performs configuration through environment.etc
  • Adds a pam_reattach option to fix sudo TouchID in tmux

Implementation uses environment.etc to create the /etc/pam.d/sudo_local file and adds the pkgs.pam-reattach option provided in #662. Follows the comment by @emilazy to have nix-darwin manage the file entirely. If the file exists already, nix-darwin should handle it through the usual warning telling the user to rename the file to sudo_local.before-nix-darwin. As identified by @lilyball in their comment, the symlink approach here shouldn't impact users since it doesn't touch the main /etc/pam.d/sudo file. As long as /etc/pam.d/sudo remains a regular file than this should work fine without disrupting sudo to apply nix-darwin configurations.

I recognize this may be a duplicate PR given all the other open issues/PRs, but I haven't seen movement on them in a while. Feel free to close this if it's unnecessary.

@emilazy
Copy link
Collaborator

emilazy commented Jul 27, 2024

Thanks! This is definitely the direction I want to go, but we need to keep support for macOS < 14. That means that ensuring the auth include sudo_local line is added to /etc/pam.d/sudo if it’s not present (and cleaning up after the old # nix-darwin: security.pam.enableSudoTouchIdAuth method). So we still need to do some amount of file wrangling here, but the upside is that users of Sonoma and higher won’t have to re‐apply their configuration after every update.

@dlubawy
Copy link
Contributor Author

dlubawy commented Jul 27, 2024

Thanks! This is definitely the direction I want to go, but we need to keep support for macOS < 14. That means that ensuring the auth include sudo_local line is added to /etc/pam.d/sudo if it’s not present (and cleaning up after the old # nix-darwin: security.pam.enableSudoTouchIdAuth method). So we still need to do some amount of file wrangling here, but the upside is that users of Sonoma and higher won’t have to re‐apply their configuration after every update.

Good call on the support for older versions and older nix-darwin support. What do you think of the recent changes? I added back an activation script like the old one which will check for the old implementation and delete it using sed -i '/security.pam.enableSudoTouchIdAuth/d'. It will then grep to check if sudo_local is already in the /etc/pam.d/sudo file and add this line using sed -i '2i.... if any option in security.pam is set:

auth       include        sudo_local # nix-darwin: security.pam

Otherwise, if all security.pam options are disabled, then it performs sed -i '/security.pam/d' /etc/pam.d/sudo which should also act to disable it for older implementations as well. Then if enabled again they will just have the single line for including sudo_local.

For users on macOS >=14, the entire thing will just be managed through environment.etc as the /etc/pam.d/sudo file will already include sudo_local. Hopefully, there will be a point where we can remove the activation script entirely as older version support is eventually dropped.

Copy link
Collaborator

@Enzime Enzime left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it took so long to getting around to reviewing this PR

Let us know if you're still interested on working on this PR

{
environment.etc."pam.d/sudo_local" = {
enable = isPamEnabled;
text = lib.strings.concatStringsSep "\n" [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
text = lib.strings.concatStringsSep "\n" [
text = lib.concatLines [

Comment on lines +76 to +77
(lib.optionalString cfg.enablePamReattach "auth optional ${pkgs.pam-reattach}/lib/pam/pam_reattach.so")
(lib.optionalString cfg.enableSudoTouchIdAuth "auth sufficient pam_tid.so")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(lib.optionalString cfg.enablePamReattach "auth optional ${pkgs.pam-reattach}/lib/pam/pam_reattach.so")
(lib.optionalString cfg.enableSudoTouchIdAuth "auth sufficient pam_tid.so")
(lib.mkIf cfg.enablePamReattach "auth optional ${pkgs.pam-reattach}/lib/pam/pam_reattach.so")
(lib.mkIf cfg.enableSudoTouchIdAuth "auth sufficient pam_tid.so")

I think this will mean the output file won't have blank lines in it

# NOTE: this can be removed at some point when support for older versions are dropped
# Always clear out older implementation if it exists
if grep '${deprecatedOption}' ${file} > /dev/null; then
${sed} -i '/${option}/d' ${file}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
${sed} -i '/${option}/d' ${file}
${sed} -i '/${deprecatedOption}/d' ${file}

file = "/etc/pam.d/sudo";
option = "security.pam.enableSudoTouchIdAuth";
file = "/etc/pam.d/sudo";
option = "security.pam";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
option = "security.pam";
marker = "security.pam.sudo_local";

fi
# Check if include line is needed (macOS < 14)
if ! grep 'sudo_local' ${file} > /dev/null; then
${sed} -i '2iauth include sudo_local # nix-darwin: ${option}' ${file}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
${sed} -i '2iauth include sudo_local # nix-darwin: ${option}' ${file}
${sed} -i '2iauth include sudo_local # nix-darwin: ${marker}' ${file}

# for the existance of the line `auth include sudo_local`. This is included
# in macOS Sonoma and later. If the line is not there already then `sed` will add it.
# In those cases, the line will include the name of the option root (`security.pam`),
# to make it easier to identify the line that should be deleted when the option is disabled.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add a comment about what happens to the marker line in /etc/pam.d/sudo when upgrading to Sonoma

# in macOS Sonoma and later. If the line is not there already then `sed` will add it.
# In those cases, the line will include the name of the option root (`security.pam`),
# to make it easier to identify the line that should be deleted when the option is disabled.
mkIncludeSudoLocalScript = isEnabled:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mkIncludeSudoLocalScript = isEnabled:

This being a function seems unnecessary

${sed} -i '2i\
auth sufficient pam_tid.so # nix-darwin: ${option}
' ${file}
# NOTE: this can be removed at some point when support for older versions are dropped
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# NOTE: this can be removed at some point when support for older versions are dropped
# REMOVEME when macOS 13 no longer supported

@emilazy
Copy link
Collaborator

emilazy commented Oct 25, 2024

I think we can skip the isPamEnabled and just unconditionally apply this stuff too, since it’s harmless to have an empty file.

I also think that we shouldn’t include a marker on the include line: we should just attempt to make older systems match Sonoma.

@Enzime
Copy link
Collaborator

Enzime commented Oct 25, 2024

The benefit of the marker would be that if a user uninstalled nix-darwin on a pre-Sonoma version we'd be able to restore it to closer to the original state, but either way is fine as auth include sudo_local still supports sudo_local not existing

As environment.etc.<file>.copy doesn't exist, that means /etc/pam.d/sudo_local will be a symlink to a path in the /nix/store, I wonder if this is going to lead to some race conditions with FDE

@emilazy
Copy link
Collaborator

emilazy commented Oct 25, 2024

I think not being able to use touch ID very early in the user session is an acceptable failure mode if that’s all it is.

@dlubawy
Copy link
Contributor Author

dlubawy commented Oct 28, 2024

@Enzime, thanks for taking the time to review. I should still be able to address the points you made and will request a re-review when I get that done.

@emilazy emilazy mentioned this pull request Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants