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

The pwd module doesn't distinguish between errors and no user #48511

Open
astrand mannequin opened this issue Nov 5, 2008 · 11 comments
Open

The pwd module doesn't distinguish between errors and no user #48511

astrand mannequin opened this issue Nov 5, 2008 · 11 comments
Labels
easy extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@astrand
Copy link
Mannequin

astrand mannequin commented Nov 5, 2008

BPO 4261
Nosy @vstinner, @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2008-11-05.08:19:21.716>
labels = ['extension-modules', 'easy', 'type-bug']
title = "The pwd module doesn't distinguish between errors and no user"
updated_at = <Date 2014-03-23.17:59:38.828>
user = 'https://bugs.python.org/astrand'

bugs.python.org fields:

activity = <Date 2014-03-23.17:59:38.828>
actor = 'r.david.murray'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Extension Modules']
creation = <Date 2008-11-05.08:19:21.716>
creator = 'astrand'
dependencies = []
files = []
hgrepos = []
issue_num = 4261
keywords = ['easy']
message_count = 11.0
messages = ['75516', '75519', '101505', '101506', '214528', '214557', '214566', '214574', '214584', '214615', '214623']
nosy_count = 4.0
nosy_names = ['astrand', 'vstinner', 'r.david.murray', 'BreamoreBoy']
pr_nums = []
priority = 'normal'
resolution = 'accepted'
stage = 'test needed'
status = 'languishing'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue4261'
versions = ['Python 3.5']

Linked PRs

@astrand
Copy link
Mannequin Author

astrand mannequin commented Nov 5, 2008

The getpwnam function in the pwd module does not correctly distinguish
between the case when the user does not exist and error
conditions. getpwnam() returns NULL in both cases, the the calling
code needs to check errno to determine if an error occured or not.

This bug is problematic in conjunction with bug
https://bugzilla.redhat.com/show_bug.cgi?id=470003, since it means
that Pythons getpwnam() will sometimes raise KeyError even for valid
users. How to reproduce:

$ python
Python 2.4.3 (#1, Jan 14 2008, 18:32:40)
[GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pwd
>>> pwd.getpwnam("astrand")
('astrand', '*', 164, 20164, 'Peter Astrand', '/home/astrand', '/bin/bash')
>>>
>>> pwd.getpwnam("astrand")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
KeyError: 'getpwnam(): name not found: astrand'
>>>
>>> pwd.getpwnam("astrand")
('astrand', '*', 164, 20164, 'Peter Astrand', '/home/astrand', '/bin/bash')
>>>

I was restarting the OpenLDAP server between the first successful call
and the traceback one.

@astrand astrand mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Nov 5, 2008
@vstinner
Copy link
Member

vstinner commented Nov 5, 2008

You're right. getpwnam() and getpwuid() have to check errno. A
different error should be raised, something like:
RuntimeError("getpwnam(%s) failure: %s" % (name, strerror(errno)))

The grp should also be affected.

@vstinner
Copy link
Member

This issue is open since 2 years without any patch. It looks like the feature request is not really important, and I consider that we can close it. Reopen the issue (with a patch!) if you consider that this ticket is important.

@bitdancer
Copy link
Member

Victor, since this is a real,fixable bug but nobody has stepped forward with a patch, I think it is better make its status 'languishing' with the reason 'no one has stepped forward with a patch'. This kind of thing is exactly what we introduced languishing for. And two years is nothing ;)

@bitdancer bitdancer added extension-modules C modules in the Modules dir easy and removed stdlib Python modules in the Lib dir labels Mar 22, 2010
@bitdancer bitdancer reopened this Mar 22, 2010
@bitdancer bitdancer added the stale Stale PR or inactive for long period of time. label Mar 22, 2010
@BreamoreBoy
Copy link
Mannequin

BreamoreBoy mannequin commented Mar 23, 2014

I'm assuming that 2.7 and 3.5 would need patches.

@vstinner
Copy link
Member

According to the manual page, there is no guarantee that missing user and error give a different result. Extracts of the manual page.

"""
Return Value

The getpwnam() and getpwuid() functions return a pointer to a passwd structure, or NULL if the matching entry is not found or an error occurs. If an error occurs, errno is set appropriately. If one wants to check errno after the call, it should be set to zero before the call.

(...)

Errors

0 or ENOENT or ESRCH or EBADF or EPERM or ...

The given name or uid was not found. 

"""

errno=0 is not always the case for mising user.

@bitdancer
Copy link
Member

I read that first paragraph as the controlling one, and that says that errno will be zero if the problem was that the entry could not be found, and non-zero if some other error occurred.

Raising an error would be a backward incompatible change, so it could only be done in 3.5.

@vstinner
Copy link
Member

The current code doesn't check errno, but if the result is NULL. NULL is
returned if the user doesn't exist, or if an error occurred. But it looks
like errno is not always 0 when the user doesn't exist.

@bitdancer
Copy link
Member

Can you think of any circumstance in which getpwnam would be able to read the file to look for the user, and yet errno is non-zero?

@vstinner
Copy link
Member

Getpwnam() can use a lot of various auth method. For example, an external
LDAP server.

@bitdancer
Copy link
Member

I don't think that changes the picture. If the error code is non-zero, that means that one of the access methods failed, which means getpwnam can't report reliably whether or not that user exists according to the system configuration.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
arhadthedev added a commit to arhadthedev/cpython that referenced this issue Aug 1, 2022
simaoafonso-pwt added a commit to simaoafonso-pwt/cpython that referenced this issue Feb 24, 2023
Distinguish
between the case when the user/group does not exist and error
conditions.
If the user does not exit, raise KeyError as  now, for errors raise OSError.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants