Merged revisions 77838 via svnmerge from
[python/dscho.git] / Modules / spwdmodule.c
blob230b57cf3ea81a91395ab4e27cc9bbfaab664620
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 */
6 #include "Python.h"
7 #include "structseq.h"
9 #include <sys/types.h>
10 #ifdef HAVE_SHADOW_H
11 #include <shadow.h>
12 #endif
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\
18 \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"},
39 {0}
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 = {
49 "spwd.struct_spwd",
50 struct_spwd__doc__,
51 struct_spwd_type_fields,
55 static int initialized;
56 static PyTypeObject StructSpwdType;
59 static void
60 sets(PyObject *v, int i, const char* val)
62 if (val) {
63 PyObject *o = PyUnicode_Decode(val, strlen(val),
64 Py_FileSystemDefaultEncoding,
65 "surrogateescape");
66 PyStructSequence_SET_ITEM(v, i, o);
67 } else {
68 PyStructSequence_SET_ITEM(v, i, Py_None);
69 Py_INCREF(Py_None);
73 static PyObject *mkspent(struct spwd *p)
75 int setIndex = 0;
76 PyObject *v = PyStructSequence_New(&StructSpwdType);
77 if (v == NULL)
78 return NULL;
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);
93 #undef SETS
94 #undef SETI
96 if (PyErr_Occurred()) {
97 Py_DECREF(v);
98 return NULL;
101 return v;
104 #endif /* HAVE_GETSPNAM || HAVE_GETSPENT */
107 #ifdef HAVE_GETSPNAM
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)
117 char *name;
118 struct spwd *p;
119 PyObject *arg, *bytes, *retval = NULL;
121 if (!PyArg_ParseTuple(args, "U:getspnam", &arg))
122 return NULL;
123 if ((bytes = PyUnicode_AsEncodedString(arg,
124 Py_FileSystemDefaultEncoding,
125 "surrogateescape")) == NULL)
126 return NULL;
127 if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
128 goto out;
129 if ((p = getspnam(name)) == NULL) {
130 PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
131 goto out;
133 retval = mkspent(p);
134 out:
135 Py_DECREF(bytes);
136 return retval;
139 #endif /* HAVE_GETSPNAM */
141 #ifdef HAVE_GETSPENT
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.");
149 static PyObject *
150 spwd_getspall(PyObject *self, PyObject *args)
152 PyObject *d;
153 struct spwd *p;
154 if ((d = PyList_New(0)) == NULL)
155 return NULL;
156 setspent();
157 while ((p = getspent()) != NULL) {
158 PyObject *v = mkspent(p);
159 if (v == NULL || PyList_Append(d, v) != 0) {
160 Py_XDECREF(v);
161 Py_DECREF(d);
162 endspent();
163 return NULL;
165 Py_DECREF(v);
167 endspent();
168 return d;
171 #endif /* HAVE_GETSPENT */
173 static PyMethodDef spwd_methods[] = {
174 #ifdef HAVE_GETSPNAM
175 {"getspnam", spwd_getspnam, METH_VARARGS, spwd_getspnam__doc__},
176 #endif
177 #ifdef HAVE_GETSPENT
178 {"getspall", spwd_getspall, METH_NOARGS, spwd_getspall__doc__},
179 #endif
180 {NULL, NULL} /* sentinel */
185 static struct PyModuleDef spwdmodule = {
186 PyModuleDef_HEAD_INIT,
187 "spwd",
188 spwd__doc__,
190 spwd_methods,
191 NULL,
192 NULL,
193 NULL,
194 NULL
197 PyMODINIT_FUNC
198 PyInit_spwd(void)
200 PyObject *m;
201 m=PyModule_Create(&spwdmodule);
202 if (m == NULL)
203 return NULL;
204 if (!initialized)
205 PyStructSequence_InitType(&StructSpwdType,
206 &struct_spwd_type_desc);
207 Py_INCREF((PyObject *) &StructSpwdType);
208 PyModule_AddObject(m, "struct_spwd", (PyObject *) &StructSpwdType);
209 initialized = 1;
210 return m;