-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add fallback for Path.home
on Unix
#11544
Add fallback for Path.home
on Unix
#11544
Conversation
Why real instead of effective? When I'm operating as an effective user, I would expect the effective user's home path. |
But why? I wouldn't expect the home directory to suddenly become that of the Crystal program's owner depending on whether the program is setuid or not. For reference, Ruby uses |
Another reason: starting a setuid program alone will not change the |
Yeah, you're probably right. Seeing a different home directory would be weir. You are often not even aware that a program runs with a different effective user. I was thinking that file ownership goes to the effective user, so it would be weird if they wouldn't have write permission in the home directory. But considering that effective user typically has elevated rights, it's not that relevant. |
src/crystal/system/unix/path.cr
Outdated
if pwd_pointer | ||
String.new(pwd.pw_dir) | ||
else | ||
raise RuntimeError.from_os_error("getpwuid_r", Errno.new(ret)) | ||
raise RuntimeError.from_os_error("getpwuid_r", Errno.new(ret.not_nil!)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is not exactly using the API correctly. getpwuid_r
returns 0
and sets the result to a null pointer if not matching password record was found. That would result in showing a runtime error "success".
We should handle the case ret.zero?
and pwd_pointer.nil?
to generate an appropriate error message.
Most actual error codes are already handled by retry_with_buffer
. I'm not sure if getpwuid_r
can even return any of the ones that are ignored (and make it return nil
): ENOENT
, ESRCH
, EBADF
, EPERM
. But having a generic fallback in cause anyone of these happens doesn't hurt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is a situation where getpwuid(getuid())
fails because no matching UID is found?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that would be ret.zero? && pwd_pointer.nil?
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is not what I am asking. If I pass an arbitrary user ID, then it can happen that no matching record is found. What is a situation where the current real user ID has no matching record?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
Defaults to the home directory of the real user (as opposed to effective) when
$HOME
is empty or null. This is equivalent to:but without constructing a
System::User
.