ยปCore Development>Code coverage>Modules/pwdmodule.c

Python code coverage for Modules/pwdmodule.c

#countcontent
1n/a
2n/a/* UNIX password file access module */
3n/a
4n/a#include "Python.h"
5n/a#include "posixmodule.h"
6n/a
7n/a#include <pwd.h>
8n/a
9n/a#include "clinic/pwdmodule.c.h"
10n/a/*[clinic input]
11n/amodule pwd
12n/a[clinic start generated code]*/
13n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=60f628ef356b97b6]*/
14n/a
15n/astatic PyStructSequence_Field struct_pwd_type_fields[] = {
16n/a {"pw_name", "user name"},
17n/a {"pw_passwd", "password"},
18n/a {"pw_uid", "user id"},
19n/a {"pw_gid", "group id"},
20n/a {"pw_gecos", "real name"},
21n/a {"pw_dir", "home directory"},
22n/a {"pw_shell", "shell program"},
23n/a {0}
24n/a};
25n/a
26n/aPyDoc_STRVAR(struct_passwd__doc__,
27n/a"pwd.struct_passwd: Results from getpw*() routines.\n\n\
28n/aThis object may be accessed either as a tuple of\n\
29n/a (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
30n/aor via the object attributes as named in the above tuple.");
31n/a
32n/astatic PyStructSequence_Desc struct_pwd_type_desc = {
33n/a "pwd.struct_passwd",
34n/a struct_passwd__doc__,
35n/a struct_pwd_type_fields,
36n/a 7,
37n/a};
38n/a
39n/aPyDoc_STRVAR(pwd__doc__,
40n/a"This module provides access to the Unix password database.\n\
41n/aIt is available on all Unix versions.\n\
42n/a\n\
43n/aPassword database entries are reported as 7-tuples containing the following\n\
44n/aitems from the password database (see `<pwd.h>'), in order:\n\
45n/apw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
46n/aThe uid and gid items are integers, all others are strings. An\n\
47n/aexception is raised if the entry asked for cannot be found.");
48n/a
49n/a
50n/astatic int initialized;
51n/astatic PyTypeObject StructPwdType;
52n/a
53n/astatic void
54n/asets(PyObject *v, int i, const char* val)
55n/a{
56n/a if (val) {
57n/a PyObject *o = PyUnicode_DecodeFSDefault(val);
58n/a PyStructSequence_SET_ITEM(v, i, o);
59n/a }
60n/a else {
61n/a PyStructSequence_SET_ITEM(v, i, Py_None);
62n/a Py_INCREF(Py_None);
63n/a }
64n/a}
65n/a
66n/astatic PyObject *
67n/amkpwent(struct passwd *p)
68n/a{
69n/a int setIndex = 0;
70n/a PyObject *v = PyStructSequence_New(&StructPwdType);
71n/a if (v == NULL)
72n/a return NULL;
73n/a
74n/a#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
75n/a#define SETS(i,val) sets(v, i, val)
76n/a
77n/a SETS(setIndex++, p->pw_name);
78n/a#if defined(HAVE_STRUCT_PASSWD_PW_PASSWD) && !defined(__ANDROID__)
79n/a SETS(setIndex++, p->pw_passwd);
80n/a#else
81n/a SETS(setIndex++, "");
82n/a#endif
83n/a PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
84n/a PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
85n/a#if defined(HAVE_STRUCT_PASSWD_PW_GECOS)
86n/a SETS(setIndex++, p->pw_gecos);
87n/a#else
88n/a SETS(setIndex++, "");
89n/a#endif
90n/a SETS(setIndex++, p->pw_dir);
91n/a SETS(setIndex++, p->pw_shell);
92n/a
93n/a#undef SETS
94n/a#undef SETI
95n/a
96n/a if (PyErr_Occurred()) {
97n/a Py_XDECREF(v);
98n/a return NULL;
99n/a }
100n/a
101n/a return v;
102n/a}
103n/a
104n/a/*[clinic input]
105n/apwd.getpwuid
106n/a
107n/a uidobj: object
108n/a /
109n/a
110n/aReturn the password database entry for the given numeric user ID.
111n/a
112n/aSee `help(pwd)` for more on password database entries.
113n/a[clinic start generated code]*/
114n/a
115n/astatic PyObject *
116n/apwd_getpwuid(PyObject *module, PyObject *uidobj)
117n/a/*[clinic end generated code: output=c4ee1d4d429b86c4 input=ae64d507a1c6d3e8]*/
118n/a{
119n/a uid_t uid;
120n/a struct passwd *p;
121n/a
122n/a if (!_Py_Uid_Converter(uidobj, &uid)) {
123n/a if (PyErr_ExceptionMatches(PyExc_OverflowError))
124n/a PyErr_Format(PyExc_KeyError,
125n/a "getpwuid(): uid not found");
126n/a return NULL;
127n/a }
128n/a if ((p = getpwuid(uid)) == NULL) {
129n/a PyObject *uid_obj = _PyLong_FromUid(uid);
130n/a if (uid_obj == NULL)
131n/a return NULL;
132n/a PyErr_Format(PyExc_KeyError,
133n/a "getpwuid(): uid not found: %S", uid_obj);
134n/a Py_DECREF(uid_obj);
135n/a return NULL;
136n/a }
137n/a return mkpwent(p);
138n/a}
139n/a
140n/a/*[clinic input]
141n/apwd.getpwnam
142n/a
143n/a arg: unicode
144n/a /
145n/a
146n/aReturn the password database entry for the given user name.
147n/a
148n/aSee `help(pwd)` for more on password database entries.
149n/a[clinic start generated code]*/
150n/a
151n/astatic PyObject *
152n/apwd_getpwnam_impl(PyObject *module, PyObject *arg)
153n/a/*[clinic end generated code: output=6abeee92430e43d2 input=d5f7e700919b02d3]*/
154n/a{
155n/a char *name;
156n/a struct passwd *p;
157n/a PyObject *bytes, *retval = NULL;
158n/a
159n/a if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
160n/a return NULL;
161n/a if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
162n/a goto out;
163n/a if ((p = getpwnam(name)) == NULL) {
164n/a PyErr_Format(PyExc_KeyError,
165n/a "getpwnam(): name not found: %s", name);
166n/a goto out;
167n/a }
168n/a retval = mkpwent(p);
169n/aout:
170n/a Py_DECREF(bytes);
171n/a return retval;
172n/a}
173n/a
174n/a#ifdef HAVE_GETPWENT
175n/a/*[clinic input]
176n/apwd.getpwall
177n/a
178n/aReturn a list of all available password database entries, in arbitrary order.
179n/a
180n/aSee help(pwd) for more on password database entries.
181n/a[clinic start generated code]*/
182n/a
183n/astatic PyObject *
184n/apwd_getpwall_impl(PyObject *module)
185n/a/*[clinic end generated code: output=4853d2f5a0afac8a input=d7ecebfd90219b85]*/
186n/a{
187n/a PyObject *d;
188n/a struct passwd *p;
189n/a if ((d = PyList_New(0)) == NULL)
190n/a return NULL;
191n/a setpwent();
192n/a while ((p = getpwent()) != NULL) {
193n/a PyObject *v = mkpwent(p);
194n/a if (v == NULL || PyList_Append(d, v) != 0) {
195n/a Py_XDECREF(v);
196n/a Py_DECREF(d);
197n/a endpwent();
198n/a return NULL;
199n/a }
200n/a Py_DECREF(v);
201n/a }
202n/a endpwent();
203n/a return d;
204n/a}
205n/a#endif
206n/a
207n/astatic PyMethodDef pwd_methods[] = {
208n/a PWD_GETPWUID_METHODDEF
209n/a PWD_GETPWNAM_METHODDEF
210n/a#ifdef HAVE_GETPWENT
211n/a PWD_GETPWALL_METHODDEF
212n/a#endif
213n/a {NULL, NULL} /* sentinel */
214n/a};
215n/a
216n/astatic struct PyModuleDef pwdmodule = {
217n/a PyModuleDef_HEAD_INIT,
218n/a "pwd",
219n/a pwd__doc__,
220n/a -1,
221n/a pwd_methods,
222n/a NULL,
223n/a NULL,
224n/a NULL,
225n/a NULL
226n/a};
227n/a
228n/a
229n/aPyMODINIT_FUNC
230n/aPyInit_pwd(void)
231n/a{
232n/a PyObject *m;
233n/a m = PyModule_Create(&pwdmodule);
234n/a if (m == NULL)
235n/a return NULL;
236n/a
237n/a if (!initialized) {
238n/a if (PyStructSequence_InitType2(&StructPwdType,
239n/a &struct_pwd_type_desc) < 0)
240n/a return NULL;
241n/a initialized = 1;
242n/a }
243n/a Py_INCREF((PyObject *) &StructPwdType);
244n/a PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
245n/a return m;
246n/a}