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