2 /* UNIX shadow password file access module */
3 /* A lot of code has been taken from pwdmodule.c */
4 /* For info also see http://www.unixpapa.com/incnote/passwd.html */
15 PyDoc_STRVAR(spwd__doc__
,
16 "This module provides access to the Unix shadow password database.\n\
17 It is available on various Unix versions.\n\
19 Shadow password database entries are reported as 9-tuples of type struct_spwd,\n\
20 containing the following items from the password database (see `<shadow.h>'):\n\
21 sp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max, sp_warn, sp_inact, sp_expire, sp_flag.\n\
22 The sp_namp and sp_pwdp are strings, the rest are integers.\n\
23 An exception is raised if the entry asked for cannot be found.\n\
24 You have to be root to be able to use this module.");
27 #if defined(HAVE_GETSPNAM) || defined(HAVE_GETSPENT)
29 static PyStructSequence_Field struct_spwd_type_fields
[] = {
30 {"sp_nam", "login name"},
31 {"sp_pwd", "encrypted password"},
32 {"sp_lstchg", "date of last change"},
33 {"sp_min", "min #days between changes"},
34 {"sp_max", "max #days between changes"},
35 {"sp_warn", "#days before pw expires to warn user about it"},
36 {"sp_inact", "#days after pw expires until account is blocked"},
37 {"sp_expire", "#days since 1970-01-01 until account is disabled"},
38 {"sp_flag", "reserved"},
42 PyDoc_STRVAR(struct_spwd__doc__
,
43 "spwd.struct_spwd: Results from getsp*() routines.\n\n\
44 This object may be accessed either as a 9-tuple of\n\
45 (sp_nam,sp_pwd,sp_lstchg,sp_min,sp_max,sp_warn,sp_inact,sp_expire,sp_flag)\n\
46 or via the object attributes as named in the above tuple.");
48 static PyStructSequence_Desc struct_spwd_type_desc
= {
51 struct_spwd_type_fields
,
55 static int initialized
;
56 static PyTypeObject StructSpwdType
;
60 sets(PyObject
*v
, int i
, const char* val
)
63 PyObject
*o
= PyUnicode_Decode(val
, strlen(val
),
64 Py_FileSystemDefaultEncoding
,
66 PyStructSequence_SET_ITEM(v
, i
, o
);
68 PyStructSequence_SET_ITEM(v
, i
, Py_None
);
73 static PyObject
*mkspent(struct spwd
*p
)
76 PyObject
*v
= PyStructSequence_New(&StructSpwdType
);
80 #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
81 #define SETS(i,val) sets(v, i, val)
83 SETS(setIndex
++, p
->sp_namp
);
84 SETS(setIndex
++, p
->sp_pwdp
);
85 SETI(setIndex
++, p
->sp_lstchg
);
86 SETI(setIndex
++, p
->sp_min
);
87 SETI(setIndex
++, p
->sp_max
);
88 SETI(setIndex
++, p
->sp_warn
);
89 SETI(setIndex
++, p
->sp_inact
);
90 SETI(setIndex
++, p
->sp_expire
);
91 SETI(setIndex
++, p
->sp_flag
);
96 if (PyErr_Occurred()) {
104 #endif /* HAVE_GETSPNAM || HAVE_GETSPENT */
109 PyDoc_STRVAR(spwd_getspnam__doc__
,
110 "getspnam(name) -> (sp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max,\n\
111 sp_warn, sp_inact, sp_expire, sp_flag)\n\
112 Return the shadow password database entry for the given user name.\n\
113 See spwd.__doc__ for more on shadow password database entries.");
115 static PyObject
* spwd_getspnam(PyObject
*self
, PyObject
*args
)
119 PyObject
*arg
, *bytes
, *retval
= NULL
;
121 if (!PyArg_ParseTuple(args
, "U:getspnam", &arg
))
123 if ((bytes
= PyUnicode_AsEncodedString(arg
,
124 Py_FileSystemDefaultEncoding
,
125 "surrogateescape")) == NULL
)
127 if (PyBytes_AsStringAndSize(bytes
, &name
, NULL
) == -1)
129 if ((p
= getspnam(name
)) == NULL
) {
130 PyErr_SetString(PyExc_KeyError
, "getspnam(): name not found");
139 #endif /* HAVE_GETSPNAM */
143 PyDoc_STRVAR(spwd_getspall__doc__
,
144 "getspall() -> list_of_entries\n\
145 Return a list of all available shadow password database entries, \
146 in arbitrary order.\n\
147 See spwd.__doc__ for more on shadow password database entries.");
150 spwd_getspall(PyObject
*self
, PyObject
*args
)
154 if ((d
= PyList_New(0)) == NULL
)
157 while ((p
= getspent()) != NULL
) {
158 PyObject
*v
= mkspent(p
);
159 if (v
== NULL
|| PyList_Append(d
, v
) != 0) {
171 #endif /* HAVE_GETSPENT */
173 static PyMethodDef spwd_methods
[] = {
175 {"getspnam", spwd_getspnam
, METH_VARARGS
, spwd_getspnam__doc__
},
178 {"getspall", spwd_getspall
, METH_NOARGS
, spwd_getspall__doc__
},
180 {NULL
, NULL
} /* sentinel */
185 static struct PyModuleDef spwdmodule
= {
186 PyModuleDef_HEAD_INIT
,
201 m
=PyModule_Create(&spwdmodule
);
205 PyStructSequence_InitType(&StructSpwdType
,
206 &struct_spwd_type_desc
);
207 Py_INCREF((PyObject
*) &StructSpwdType
);
208 PyModule_AddObject(m
, "struct_spwd", (PyObject
*) &StructSpwdType
);