No issues found
1 /* UNIX shadow password file access module */
2 /* A lot of code has been taken from pwdmodule.c */
3 /* For info also see http://www.unixpapa.com/incnote/passwd.html */
4
5 #include "Python.h"
6 #include "structseq.h"
7
8 #include <sys/types.h>
9 #ifdef HAVE_SHADOW_H
10 #include <shadow.h>
11 #endif
12
13
14 PyDoc_STRVAR(spwd__doc__,
15 "This module provides access to the Unix shadow password database.\n\
16 It is available on various Unix versions.\n\
17 \n\
18 Shadow password database entries are reported as 9-tuples of type struct_spwd,\n\
19 containing the following items from the password database (see `<shadow.h>'):\n\
20 sp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max, sp_warn, sp_inact, sp_expire, sp_flag.\n\
21 The sp_namp and sp_pwdp are strings, the rest are integers.\n\
22 An exception is raised if the entry asked for cannot be found.\n\
23 You have to be root to be able to use this module.");
24
25
26 #if defined(HAVE_GETSPNAM) || defined(HAVE_GETSPENT)
27
28 static PyStructSequence_Field struct_spwd_type_fields[] = {
29 {"sp_nam", "login name"},
30 {"sp_pwd", "encrypted password"},
31 {"sp_lstchg", "date of last change"},
32 {"sp_min", "min #days between changes"},
33 {"sp_max", "max #days between changes"},
34 {"sp_warn", "#days before pw expires to warn user about it"},
35 {"sp_inact", "#days after pw expires until account is blocked"},
36 {"sp_expire", "#days since 1970-01-01 until account is disabled"},
37 {"sp_flag", "reserved"},
38 {0}
39 };
40
41 PyDoc_STRVAR(struct_spwd__doc__,
42 "spwd.struct_spwd: Results from getsp*() routines.\n\n\
43 This object may be accessed either as a 9-tuple of\n\
44 (sp_nam,sp_pwd,sp_lstchg,sp_min,sp_max,sp_warn,sp_inact,sp_expire,sp_flag)\n\
45 or via the object attributes as named in the above tuple.");
46
47 static PyStructSequence_Desc struct_spwd_type_desc = {
48 "spwd.struct_spwd",
49 struct_spwd__doc__,
50 struct_spwd_type_fields,
51 9,
52 };
53
54 static int initialized;
55 static PyTypeObject StructSpwdType;
56
57
58 static void
59 sets(PyObject *v, int i, char* val)
60 {
61 if (val)
62 PyStructSequence_SET_ITEM(v, i, PyString_FromString(val));
63 else {
64 PyStructSequence_SET_ITEM(v, i, Py_None);
65 Py_INCREF(Py_None);
66 }
67 }
68
69 static PyObject *mkspent(struct spwd *p)
70 {
71 int setIndex = 0;
72 PyObject *v = PyStructSequence_New(&StructSpwdType);
73 if (v == NULL)
74 return NULL;
75
76 #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
77 #define SETS(i,val) sets(v, i, val)
78
79 SETS(setIndex++, p->sp_namp);
80 SETS(setIndex++, p->sp_pwdp);
81 SETI(setIndex++, p->sp_lstchg);
82 SETI(setIndex++, p->sp_min);
83 SETI(setIndex++, p->sp_max);
84 SETI(setIndex++, p->sp_warn);
85 SETI(setIndex++, p->sp_inact);
86 SETI(setIndex++, p->sp_expire);
87 SETI(setIndex++, p->sp_flag);
88
89 #undef SETS
90 #undef SETI
91
92 if (PyErr_Occurred()) {
93 Py_DECREF(v);
94 return NULL;
95 }
96
97 return v;
98 }
99
100 #endif /* HAVE_GETSPNAM || HAVE_GETSPENT */
101
102
103 #ifdef HAVE_GETSPNAM
104
105 PyDoc_STRVAR(spwd_getspnam__doc__,
106 "getspnam(name) -> (sp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max,\n\
107 sp_warn, sp_inact, sp_expire, sp_flag)\n\
108 Return the shadow password database entry for the given user name.\n\
109 See spwd.__doc__ for more on shadow password database entries.");
110
111 static PyObject* spwd_getspnam(PyObject *self, PyObject *args)
112 {
113 char *name;
114 struct spwd *p;
115 if (!PyArg_ParseTuple(args, "s:getspnam", &name))
116 return NULL;
117 if ((p = getspnam(name)) == NULL) {
118 PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
119 return NULL;
120 }
121 return mkspent(p);
122 }
123
124 #endif /* HAVE_GETSPNAM */
125
126 #ifdef HAVE_GETSPENT
127
128 PyDoc_STRVAR(spwd_getspall__doc__,
129 "getspall() -> list_of_entries\n\
130 Return a list of all available shadow password database entries, \
131 in arbitrary order.\n\
132 See spwd.__doc__ for more on shadow password database entries.");
133
134 static PyObject *
135 spwd_getspall(PyObject *self, PyObject *args)
136 {
137 PyObject *d;
138 struct spwd *p;
139 if ((d = PyList_New(0)) == NULL)
140 return NULL;
141 setspent();
142 while ((p = getspent()) != NULL) {
143 PyObject *v = mkspent(p);
144 if (v == NULL || PyList_Append(d, v) != 0) {
145 Py_XDECREF(v);
146 Py_DECREF(d);
147 endspent();
148 return NULL;
149 }
150 Py_DECREF(v);
151 }
152 endspent();
153 return d;
154 }
155
156 #endif /* HAVE_GETSPENT */
157
158 static PyMethodDef spwd_methods[] = {
159 #ifdef HAVE_GETSPNAM
160 {"getspnam", spwd_getspnam, METH_VARARGS, spwd_getspnam__doc__},
161 #endif
162 #ifdef HAVE_GETSPENT
163 {"getspall", spwd_getspall, METH_NOARGS, spwd_getspall__doc__},
164 #endif
165 {NULL, NULL} /* sentinel */
166 };
167
168
169 PyMODINIT_FUNC
170 initspwd(void)
171 {
172 PyObject *m;
173 m=Py_InitModule3("spwd", spwd_methods, spwd__doc__);
174 if (m == NULL)
175 return;
176 if (!initialized)
177 PyStructSequence_InitType(&StructSpwdType,
178 &struct_spwd_type_desc);
179 Py_INCREF((PyObject *) &StructSpwdType);
180 PyModule_AddObject(m, "struct_spwd", (PyObject *) &StructSpwdType);
181 initialized = 1;
182 }