blob: 3a62fe8c0d7fc0079c854037d463d0c2c1493433 [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[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000030 {"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}
Martin v. Löwisc3001752005-01-23 09:27:24 +000040};
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 = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000049 "spwd.struct_spwd",
50 struct_spwd__doc__,
51 struct_spwd_type_fields,
52 9,
Martin v. Löwisc3001752005-01-23 09:27:24 +000053};
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) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000063 PyObject *o = PyUnicode_Decode(val, strlen(val),
64 Py_FileSystemDefaultEncoding,
65 "surrogateescape");
66 PyStructSequence_SET_ITEM(v, i, o);
Martin v. Löwisb6a748b2009-05-29 15:23:17 +000067 } else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000068 PyStructSequence_SET_ITEM(v, i, Py_None);
69 Py_INCREF(Py_None);
Martin v. Löwisc3001752005-01-23 09:27:24 +000070 }
71}
72
73static PyObject *mkspent(struct spwd *p)
74{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000075 int setIndex = 0;
76 PyObject *v = PyStructSequence_New(&StructSpwdType);
77 if (v == NULL)
78 return NULL;
Martin v. Löwisc3001752005-01-23 09:27:24 +000079
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
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000083 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);
Martin v. Löwisc3001752005-01-23 09:27:24 +000092
93#undef SETS
94#undef SETI
95
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000096 if (PyErr_Occurred()) {
97 Py_DECREF(v);
98 return NULL;
99 }
Martin v. Löwisc3001752005-01-23 09:27:24 +0000100
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000101 return v;
Martin v. Löwisc3001752005-01-23 09:27:24 +0000102}
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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000117 char *name;
118 struct spwd *p;
119 PyObject *arg, *bytes, *retval = NULL;
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000120
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000121 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;
132 }
133 retval = mkspent(p);
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000134out:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000135 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000152 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;
164 }
165 Py_DECREF(v);
166 }
167 endspent();
168 return d;
Martin v. Löwisc3001752005-01-23 09:27:24 +0000169}
170
171#endif /* HAVE_GETSPENT */
172
173static PyMethodDef spwd_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000174#ifdef HAVE_GETSPNAM
175 {"getspnam", spwd_getspnam, METH_VARARGS, spwd_getspnam__doc__},
Martin v. Löwisc3001752005-01-23 09:27:24 +0000176#endif
177#ifdef HAVE_GETSPENT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000178 {"getspall", spwd_getspall, METH_NOARGS, spwd_getspall__doc__},
Martin v. Löwisc3001752005-01-23 09:27:24 +0000179#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000180 {NULL, NULL} /* sentinel */
Martin v. Löwisc3001752005-01-23 09:27:24 +0000181};
182
183
Martin v. Löwis1a214512008-06-11 05:26:20 +0000184
185static struct PyModuleDef spwdmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000186 PyModuleDef_HEAD_INIT,
187 "spwd",
188 spwd__doc__,
189 -1,
190 spwd_methods,
191 NULL,
192 NULL,
193 NULL,
194 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000195};
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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000200 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;
Martin v. Löwisc3001752005-01-23 09:27:24 +0000211}