8000 Issue #18787: spwd.getspnam() now raises a PermissionError if the user · python/cpython@46cc42e · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 46cc42e

Browse files
committed
Issue #18787: spwd.getspnam() now raises a PermissionError if the user
doesn't have privileges.
1 parent c2a2911 commit 46cc42e

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

Doc/library/spwd.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ The following functions are defined:
5454

5555
Return the shadow password database entry for the given user name.
5656

57+
.. versionchanged:: 3.6
58+
Raises a :exc:`PermissionError` instead of :exc:`KeyError` if the user
59+
doesn't have privileges.
5760

5861
.. function:: getspall()
5962

Doc/whatsnew/3.6.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,8 @@ Changes in the Python API
471471
the exception will stop a single-threaded server. (Contributed by
472472
Martin Panter in :issue:`23430`.)
473473

474+
* :func:`spwd.getspnam` now raises a :exc:`PermissionError` instead of
475+
:exc:`KeyError` if the user doesn't have privileges.
474476

475477
Changes in the C API
476478
--------------------

Lib/test/test_spwd.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,15 @@ def test_getspnam(self):
5656
self.assertRaises(TypeError, spwd.getspnam, bytes_name)
5757

5858

59+
@unittest.skipUnless(hasattr(os, 'geteuid') and os.geteuid() != 0,
60+
'non-root user required')
61+
class TestSpwdNonRoot(unittest.TestCase):
62+
63+
def test_getspnam_exception(self):
64+
with self.assertRaises(PermissionError) as cm:
65+
spwd.getspnam('bin')
66+
self.assertEqual(str(cm.exception), '[Errno 13] Permission denied')
67+
68+
5969
if __name__ == "__main__":
6070
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ Core and Builtins
226226
Library
227227
-------
228228

229+
- Issue #18787: spwd.getspnam() now raises a PermissionError if the user
230+
doesn't have privileges.
231+
229232
- Issue #26560: Avoid potential ValueError in BaseHandler.start_response.
230233
Initial patch by Peter Inglesby.
231234

Modules/spwdmodule.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ spwd_getspnam_impl(PyModuleDef *module, PyObject *arg)
137137
if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
138138
goto out;
139139
if ((p = getspnam(name)) == NULL) {
140-
PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
140+
if (errno != 0)
141+
PyErr_SetFromErrno(PyExc_OSError);
142+
else
143+
PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
141144
goto out;
142145
}
143146
retval = mkspent(p);

0 commit comments

Comments
 (0)
0