2
/* UNIX password file access module */
5
#include "posixmodule.h"
9
static PyStructSequence_Field struct_pwd_type_fields[] = {
10
{"pw_name", "user name"},
11
{"pw_passwd", "password"},
12
{"pw_uid", "user id"},
13
{"pw_gid", "group id"},
14
{"pw_gecos", "real name"},
15
{"pw_dir", "home directory"},
16
{"pw_shell", "shell program"},
20
PyDoc_STRVAR(struct_passwd__doc__,
21
"pwd.struct_passwd: Results from getpw*() routines.\n\n\
22
This object may be accessed either as a tuple of\n\
23
(pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
24
or via the object attributes as named in the above tuple.");
26
static PyStructSequence_Desc struct_pwd_type_desc = {
29
struct_pwd_type_fields,
33
PyDoc_STRVAR(pwd__doc__,
34
"This module provides access to the Unix password database.\n\
35
It is available on all Unix versions.\n\
37
Password database entries are reported as 7-tuples containing the following\n\
38
items from the password database (see `<pwd.h>'), in order:\n\
39
pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
40
The uid and gid items are integers, all others are strings. An\n\
41
exception is raised if the entry asked for cannot be found.");
44
static int initialized;
45
static PyTypeObject StructPwdType;
48
sets(PyObject *v, int i, const char* val)
51
PyObject *o = PyUnicode_DecodeFSDefault(val);
52
PyStructSequence_SET_ITEM(v, i, o);
55
PyStructSequence_SET_ITEM(v, i, Py_None);
61
mkpwent(struct passwd *p)
64
PyObject *v = PyStructSequence_New(&StructPwdType);
68
#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
69
#define SETS(i,val) sets(v, i, val)
71
SETS(setIndex++, p->pw_name);
75
SETS(setIndex++, p->pw_passwd);
77
PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
78
PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
82
SETS(setIndex++, p->pw_gecos);
84
SETS(setIndex++, p->pw_dir);
85
SETS(setIndex++, p->pw_shell);
90
if (PyErr_Occurred()) {
98
PyDoc_STRVAR(pwd_getpwuid__doc__,
99
"getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
100
pw_gid,pw_gecos,pw_dir,pw_shell)\n\
101
Return the password database entry for the given numeric user ID.\n\
102
See help(pwd) for more on password database entries.");
105
pwd_getpwuid(PyObject *self, PyObject *args)
109
if (!PyArg_ParseTuple(args, "O&:getpwuid", _Py_Uid_Converter, &uid)) {
110
if (PyErr_ExceptionMatches(PyExc_OverflowError))
111
PyErr_Format(PyExc_KeyError,
112
"getpwuid(): uid not found");
115
if ((p = getpwuid(uid)) == NULL) {
116
PyObject *uid_obj = _PyLong_FromUid(uid);
119
PyErr_Format(PyExc_KeyError,
120
"getpwuid(): uid not found: %S", uid_obj);
127
PyDoc_STRVAR(pwd_getpwnam__doc__,
128
"getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
129
pw_gid,pw_gecos,pw_dir,pw_shell)\n\
130
Return the password database entry for the given user name.\n\
131
See help(pwd) for more on password database entries.");
134
pwd_getpwnam(PyObject *self, PyObject *args)
138
PyObject *arg, *bytes, *retval = NULL;
140
if (!PyArg_ParseTuple(args, "U:getpwnam", &arg))
142
if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
144
if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
146
if ((p = getpwnam(name)) == NULL) {
147
PyErr_Format(PyExc_KeyError,
148
"getpwnam(): name not found: %s", name);
158
PyDoc_STRVAR(pwd_getpwall__doc__,
159
"getpwall() -> list_of_entries\n\
160
Return a list of all available password database entries, \
161
in arbitrary order.\n\
162
See help(pwd) for more on password database entries.");
165
pwd_getpwall(PyObject *self)
169
if ((d = PyList_New(0)) == NULL)
172
while ((p = getpwent()) != NULL) {
173
PyObject *v = mkpwent(p);
174
if (v == NULL || PyList_Append(d, v) != 0) {
187
static PyMethodDef pwd_methods[] = {
188
{"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
189
{"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
191
{"getpwall", (PyCFunction)pwd_getpwall,
192
METH_NOARGS, pwd_getpwall__doc__},
194
{NULL, NULL} /* sentinel */
197
static struct PyModuleDef pwdmodule = {
198
PyModuleDef_HEAD_INIT,
214
m = PyModule_Create(&pwdmodule);
219
if (PyStructSequence_InitType2(&StructPwdType,
220
&struct_pwd_type_desc) < 0)
224
Py_INCREF((PyObject *) &StructPwdType);
225
PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);