blob: 230b57cf3ea81a91395ab4e27cc9bbfaab664620 [file] [log] [blame]
Martin v. Löwisc3001752005-01-23 09:27:24 +00001
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 */
5
6#include "Python.h"
7#include "structseq.h"
8
9#include <sys/types.h>
10#ifdef HAVE_SHADOW_H
11#include <shadow.h>
12#endif
13
14
15PyDoc_STRVAR(spwd__doc__,
16"This module provides access to the Unix shadow password database.\n\
17It is available on various Unix versions.\n\
18\n\
19Shadow password database entries are reported as 9-tuples of type struct_spwd,\n\
20containing the following items from the password database (see `<shadow.h>'):\n\
21sp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max, sp_warn, sp_inact, sp_expire, sp_flag.\n\
22The sp_namp and sp_pwdp are strings, the rest are integers.\n\
23An exception is raised if the entry asked for cannot be found.\n\
24You have to be root to be able to use this module.");
25
26
27#if defined(HAVE_GETSPNAM) || defined(HAVE_GETSPENT)
28
29static 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}
40};
41
42PyDoc_STRVAR(struct_spwd__doc__,
43"spwd.struct_spwd: Results from getsp*() routines.\n\n\
44This 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\
46or via the object attributes as named in the above tuple.");
47
48static PyStructSequence_Desc struct_spwd_type_desc = {
49 "spwd.struct_spwd",
50 struct_spwd__doc__,
51 struct_spwd_type_fields,
52 9,
53};
54
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000055static int initialized;
Martin v. Löwisc3001752005-01-23 09:27:24 +000056static PyTypeObject StructSpwdType;
57
58
59static void
Neal Norwitzeb8b3a62007-08-24 23:26:23 +000060sets(PyObject *v, int i, const char* val)
Martin v. Löwisc3001752005-01-23 09:27:24 +000061{
Martin v. Löwisb6a748b2009-05-29 15:23:17 +000062 if (val) {
63 PyObject *o = PyUnicode_Decode(val, strlen(val),
64 Py_FileSystemDefaultEncoding,
65 "surrogateescape");
66 PyStructSequence_SET_ITEM(v, i, o);
67 } else {
Martin v. Löwisc3001752005-01-23 09:27:24 +000068 PyStructSequence_SET_ITEM(v, i, Py_None);
69 Py_INCREF(Py_None);
70 }
71}
72
73static PyObject *mkspent(struct spwd *p)
74{
75 int setIndex = 0;
76 PyObject *v = PyStructSequence_New(&StructSpwdType);
77 if (v == NULL)
78 return NULL;
79
Christian Heimes217cfd12007-12-02 14:31:20 +000080#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
Martin v. Löwisc3001752005-01-23 09:27:24 +000081#define SETS(i,val) sets(v, i, val)
82
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);
92
93#undef SETS
94#undef SETI
95
96 if (PyErr_Occurred()) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +000097 Py_DECREF(v);
Martin v. Löwisc3001752005-01-23 09:27:24 +000098 return NULL;
99 }
100
101 return v;
102}
103
104#endif /* HAVE_GETSPNAM || HAVE_GETSPENT */
105
106
107#ifdef HAVE_GETSPNAM
108
109PyDoc_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\
112Return the shadow password database entry for the given user name.\n\
113See spwd.__doc__ for more on shadow password database entries.");
114
115static PyObject* spwd_getspnam(PyObject *self, PyObject *args)
116{
117 char *name;
118 struct spwd *p;
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000119 PyObject *arg, *bytes, *retval = NULL;
120
121 if (!PyArg_ParseTuple(args, "U:getspnam", &arg))
Martin v. Löwisc3001752005-01-23 09:27:24 +0000122 return NULL;
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000123 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;
Martin v. Löwisc3001752005-01-23 09:27:24 +0000129 if ((p = getspnam(name)) == NULL) {
130 PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000131 goto out;
Martin v. Löwisc3001752005-01-23 09:27:24 +0000132 }
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000133 retval = mkspent(p);
134out:
135 Py_DECREF(bytes);
136 return retval;
Martin v. Löwisc3001752005-01-23 09:27:24 +0000137}
138
Neal Norwitz62679962005-01-24 23:33:50 +0000139#endif /* HAVE_GETSPNAM */
140
141#ifdef HAVE_GETSPENT
142
Martin v. Löwisc3001752005-01-23 09:27:24 +0000143PyDoc_STRVAR(spwd_getspall__doc__,
144"getspall() -> list_of_entries\n\
145Return a list of all available shadow password database entries, \
146in arbitrary order.\n\
147See spwd.__doc__ for more on shadow password database entries.");
148
Martin v. Löwisc3001752005-01-23 09:27:24 +0000149static PyObject *
150spwd_getspall(PyObject *self, PyObject *args)
151{
152 PyObject *d;
153 struct spwd *p;
Martin v. Löwisc3001752005-01-23 09:27:24 +0000154 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;
164 }
165 Py_DECREF(v);
166 }
167 endspent();
168 return d;
169}
170
171#endif /* HAVE_GETSPENT */
172
173static 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 */
181};
182
183
Martin v. Löwis1a214512008-06-11 05:26:20 +0000184
185static struct PyModuleDef spwdmodule = {
186 PyModuleDef_HEAD_INIT,
187 "spwd",
188 spwd__doc__,
189 -1,
190 spwd_methods,
191 NULL,
192 NULL,
193 NULL,
194 NULL
195};
196
Martin v. Löwisc3001752005-01-23 09:27:24 +0000197PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000198PyInit_spwd(void)
Martin v. Löwisc3001752005-01-23 09:27:24 +0000199{
200 PyObject *m;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000201 m=PyModule_Create(&spwdmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000202 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000203 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000204 if (!initialized)
205 PyStructSequence_InitType(&StructSpwdType,
206 &struct_spwd_type_desc);
Martin v. Löwisc3001752005-01-23 09:27:24 +0000207 Py_INCREF((PyObject *) &StructSpwdType);
208 PyModule_AddObject(m, "struct_spwd", (PyObject *) &StructSpwdType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000209 initialized = 1;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000210 return m;
Martin v. Löwisc3001752005-01-23 09:27:24 +0000211}