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

Python code coverage for Modules/spwdmodule.c

#countcontent
1n/a
2n/a/* UNIX shadow password file access module */
3n/a/* A lot of code has been taken from pwdmodule.c */
4n/a/* For info also see http://www.unixpapa.com/incnote/passwd.html */
5n/a
6n/a#include "Python.h"
7n/a
8n/a#include <sys/types.h>
9n/a#ifdef HAVE_SHADOW_H
10n/a#include <shadow.h>
11n/a#endif
12n/a
13n/a#include "clinic/spwdmodule.c.h"
14n/a
15n/a/*[clinic input]
16n/amodule spwd
17n/a[clinic start generated code]*/
18n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c0b841b90a6a07ce]*/
19n/a
20n/aPyDoc_STRVAR(spwd__doc__,
21n/a"This module provides access to the Unix shadow password database.\n\
22n/aIt is available on various Unix versions.\n\
23n/a\n\
24n/aShadow password database entries are reported as 9-tuples of type struct_spwd,\n\
25n/acontaining the following items from the password database (see `<shadow.h>'):\n\
26n/asp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max, sp_warn, sp_inact, sp_expire, sp_flag.\n\
27n/aThe sp_namp and sp_pwdp are strings, the rest are integers.\n\
28n/aAn exception is raised if the entry asked for cannot be found.\n\
29n/aYou have to be root to be able to use this module.");
30n/a
31n/a
32n/a#if defined(HAVE_GETSPNAM) || defined(HAVE_GETSPENT)
33n/a
34n/astatic PyStructSequence_Field struct_spwd_type_fields[] = {
35n/a {"sp_namp", "login name"},
36n/a {"sp_pwdp", "encrypted password"},
37n/a {"sp_lstchg", "date of last change"},
38n/a {"sp_min", "min #days between changes"},
39n/a {"sp_max", "max #days between changes"},
40n/a {"sp_warn", "#days before pw expires to warn user about it"},
41n/a {"sp_inact", "#days after pw expires until account is disabled"},
42n/a {"sp_expire", "#days since 1970-01-01 when account expires"},
43n/a {"sp_flag", "reserved"},
44n/a {"sp_nam", "login name; deprecated"}, /* Backward compatibility */
45n/a {"sp_pwd", "encrypted password; deprecated"}, /* Backward compatibility */
46n/a {0}
47n/a};
48n/a
49n/aPyDoc_STRVAR(struct_spwd__doc__,
50n/a"spwd.struct_spwd: Results from getsp*() routines.\n\n\
51n/aThis object may be accessed either as a 9-tuple of\n\
52n/a (sp_namp,sp_pwdp,sp_lstchg,sp_min,sp_max,sp_warn,sp_inact,sp_expire,sp_flag)\n\
53n/aor via the object attributes as named in the above tuple.");
54n/a
55n/astatic PyStructSequence_Desc struct_spwd_type_desc = {
56n/a "spwd.struct_spwd",
57n/a struct_spwd__doc__,
58n/a struct_spwd_type_fields,
59n/a 9,
60n/a};
61n/a
62n/astatic int initialized;
63n/astatic PyTypeObject StructSpwdType;
64n/a
65n/a
66n/astatic void
67n/asets(PyObject *v, int i, const char* val)
68n/a{
69n/a if (val) {
70n/a PyObject *o = PyUnicode_DecodeFSDefault(val);
71n/a PyStructSequence_SET_ITEM(v, i, o);
72n/a } else {
73n/a PyStructSequence_SET_ITEM(v, i, Py_None);
74n/a Py_INCREF(Py_None);
75n/a }
76n/a}
77n/a
78n/astatic PyObject *mkspent(struct spwd *p)
79n/a{
80n/a int setIndex = 0;
81n/a PyObject *v = PyStructSequence_New(&StructSpwdType);
82n/a if (v == NULL)
83n/a return NULL;
84n/a
85n/a#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
86n/a#define SETS(i,val) sets(v, i, val)
87n/a
88n/a SETS(setIndex++, p->sp_namp);
89n/a SETS(setIndex++, p->sp_pwdp);
90n/a SETI(setIndex++, p->sp_lstchg);
91n/a SETI(setIndex++, p->sp_min);
92n/a SETI(setIndex++, p->sp_max);
93n/a SETI(setIndex++, p->sp_warn);
94n/a SETI(setIndex++, p->sp_inact);
95n/a SETI(setIndex++, p->sp_expire);
96n/a SETI(setIndex++, p->sp_flag);
97n/a SETS(setIndex++, p->sp_namp); /* Backward compatibility for sp_nam */
98n/a SETS(setIndex++, p->sp_pwdp); /* Backward compatibility for sp_pwd */
99n/a
100n/a#undef SETS
101n/a#undef SETI
102n/a
103n/a if (PyErr_Occurred()) {
104n/a Py_DECREF(v);
105n/a return NULL;
106n/a }
107n/a
108n/a return v;
109n/a}
110n/a
111n/a#endif /* HAVE_GETSPNAM || HAVE_GETSPENT */
112n/a
113n/a
114n/a#ifdef HAVE_GETSPNAM
115n/a
116n/a/*[clinic input]
117n/aspwd.getspnam
118n/a
119n/a arg: unicode
120n/a /
121n/a
122n/aReturn the shadow password database entry for the given user name.
123n/a
124n/aSee `help(spwd)` for more on shadow password database entries.
125n/a[clinic start generated code]*/
126n/a
127n/astatic PyObject *
128n/aspwd_getspnam_impl(PyObject *module, PyObject *arg)
129n/a/*[clinic end generated code: output=701250cf57dc6ebe input=dd89429e6167a00f]*/
130n/a{
131n/a char *name;
132n/a struct spwd *p;
133n/a PyObject *bytes, *retval = NULL;
134n/a
135n/a if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
136n/a return NULL;
137n/a if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
138n/a goto out;
139n/a if ((p = getspnam(name)) == NULL) {
140n/a if (errno != 0)
141n/a PyErr_SetFromErrno(PyExc_OSError);
142n/a else
143n/a PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
144n/a goto out;
145n/a }
146n/a retval = mkspent(p);
147n/aout:
148n/a Py_DECREF(bytes);
149n/a return retval;
150n/a}
151n/a
152n/a#endif /* HAVE_GETSPNAM */
153n/a
154n/a#ifdef HAVE_GETSPENT
155n/a
156n/a/*[clinic input]
157n/aspwd.getspall
158n/a
159n/aReturn a list of all available shadow password database entries, in arbitrary order.
160n/a
161n/aSee `help(spwd)` for more on shadow password database entries.
162n/a[clinic start generated code]*/
163n/a
164n/astatic PyObject *
165n/aspwd_getspall_impl(PyObject *module)
166n/a/*[clinic end generated code: output=4fda298d6bf6d057 input=b2c84b7857d622bd]*/
167n/a{
168n/a PyObject *d;
169n/a struct spwd *p;
170n/a if ((d = PyList_New(0)) == NULL)
171n/a return NULL;
172n/a setspent();
173n/a while ((p = getspent()) != NULL) {
174n/a PyObject *v = mkspent(p);
175n/a if (v == NULL || PyList_Append(d, v) != 0) {
176n/a Py_XDECREF(v);
177n/a Py_DECREF(d);
178n/a endspent();
179n/a return NULL;
180n/a }
181n/a Py_DECREF(v);
182n/a }
183n/a endspent();
184n/a return d;
185n/a}
186n/a
187n/a#endif /* HAVE_GETSPENT */
188n/a
189n/astatic PyMethodDef spwd_methods[] = {
190n/a#ifdef HAVE_GETSPNAM
191n/a SPWD_GETSPNAM_METHODDEF
192n/a#endif
193n/a#ifdef HAVE_GETSPENT
194n/a SPWD_GETSPALL_METHODDEF
195n/a#endif
196n/a {NULL, NULL} /* sentinel */
197n/a};
198n/a
199n/a
200n/a
201n/astatic struct PyModuleDef spwdmodule = {
202n/a PyModuleDef_HEAD_INIT,
203n/a "spwd",
204n/a spwd__doc__,
205n/a -1,
206n/a spwd_methods,
207n/a NULL,
208n/a NULL,
209n/a NULL,
210n/a NULL
211n/a};
212n/a
213n/aPyMODINIT_FUNC
214n/aPyInit_spwd(void)
215n/a{
216n/a PyObject *m;
217n/a m=PyModule_Create(&spwdmodule);
218n/a if (m == NULL)
219n/a return NULL;
220n/a if (!initialized) {
221n/a if (PyStructSequence_InitType2(&StructSpwdType,
222n/a &struct_spwd_type_desc) < 0)
223n/a return NULL;
224n/a }
225n/a Py_INCREF((PyObject *) &StructSpwdType);
226n/a PyModule_AddObject(m, "struct_spwd", (PyObject *) &StructSpwdType);
227n/a initialized = 1;
228n/a return m;
229n/a}