Working on a file server in c/c++ and need to do following:
- Validate user credentials (mac Username & password)
- Impersonate user security context in a thread running in a daemon, so that I can enumerate user's home directory and files/folders.
Regarding 2, found API: pthread_setugid_np - is this the right approach? If so, how do I verify user credentials and call this API?
Does this mean that its really not possible to impersonate user and access their home directory etc if the user isn't logged in via terminal/console? or if they have FileVault enabled?
Does this mean that its really not possible to impersonate user and access their home directory etc if the user isn't logged in via terminal/console? or if they have FileVault enabled?
Yes, that is absolutely the case. As TN2083 laid out in a detailed example:
"It is not possible for a daemon to act on behalf of a user with 100% fidelity. While this might seem like a controversial statement, it's actually pretty easy to prove. For example, consider something as simple as accessing a preference file in the user's home directory. It's not possible for a daemon to reliably do this. If the user has an AFP home directory, or their home directory is protected by FileVault, the volume containing the home directory will only be mounted when the user is logged in. Moreover, it is not possible to mount the that volume without the user's security credentials (typically their password). So, if a daemon tries to get a user preference when the user is not logged in, it will fail."
Note that in both of these example, this is a FEATURE not a bug. The system was intentionally designed so that this data would not be accessible unless the user had logged in.
Regarding 2, found API: pthread_setugid_np - is this the right approach?
Right approach for what? Quoting TN2093 again:
"In some cases it is helpful to impersonate the user, at least as far as the permissions checking done by the BSD subsystem of the kernel."
pthread_setugid_np lets a process running as root shift one of it's thread identies to be a different user, at least as far as the BSD system is concerned. If you want to create a file as a different user, then it works great. If you want access to the users home directory... then it does nothing whatsoever. It changes how the system "thinks" about that particular thread but that doesn't change the larger state of other components, like the home directory not being available.
If so, how do I verify user credentials and call this API?
I'm not sure what you mean here. That API assumes you're running as a privileged user ("root"), so there isn't anything to verify.
__
Kevin Elliott
DTS Engineer, CoreOS/Hardware