blob: 957de58e99fddc5252adbd2d826099237af66d5c [file] [log] [blame]
Martin v. Löwis79fbecf2005-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 Pitrouc43ba3b2010-05-09 15:15:40 +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öwis79fbecf2005-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 Pitrouc43ba3b2010-05-09 15:15:40 +000049 "spwd.struct_spwd",
50 struct_spwd__doc__,
51 struct_spwd_type_fields,
52 9,
Martin v. Löwis79fbecf2005-01-23 09:27:24 +000053};
54
Martin v. Löwis4c9ea542006-04-16 18:55:50 +000055static int initialized;
Martin v. Löwis79fbecf2005-01-23 09:27:24 +000056static PyTypeObject StructSpwdType;
57
58
59static void
60sets(PyObject *v, int i, char* val)
61{
62 if (val)
Antoine Pitrouc43ba3b2010-05-09 15:15:40 +000063 PyStructSequence_SET_ITEM(v, i, PyString_FromString(val));
Martin v. Löwis79fbecf2005-01-23 09:27:24 +000064 else {
Antoine Pitrouc43ba3b2010-05-09 15:15:40 +000065 PyStructSequence_SET_ITEM(v, i, Py_None);
66 Py_INCREF(Py_None);
Martin v. Löwis79fbecf2005-01-23 09:27:24 +000067 }
68}
69
70static PyObject *mkspent(struct spwd *p)
71{
Antoine Pitrouc43ba3b2010-05-09 15:15:40 +000072 int setIndex = 0;
73 PyObject *v = PyStructSequence_New(&StructSpwdType);
74 if (v == NULL)
75 return NULL;
Martin v. Löwis79fbecf2005-01-23 09:27:24 +000076
77#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
78#define SETS(i,val) sets(v, i, val)
79
Antoine Pitrouc43ba3b2010-05-09 15:15:40 +000080 SETS(setIndex++, p->sp_namp);
81 SETS(setIndex++, p->sp_pwdp);
82 SETI(setIndex++, p->sp_lstchg);
83 SETI(setIndex++, p->sp_min);
84 SETI(setIndex++, p->sp_max);
85 SETI(setIndex++, p->sp_warn);
86 SETI(setIndex++, p->sp_inact);
87 SETI(setIndex++, p->sp_expire);
88 SETI(setIndex++, p->sp_flag);
Martin v. Löwis79fbecf2005-01-23 09:27:24 +000089
90#undef SETS
91#undef SETI
92
Antoine Pitrouc43ba3b2010-05-09 15:15:40 +000093 if (PyErr_Occurred()) {
94 Py_DECREF(v);
95 return NULL;
96 }
Martin v. Löwis79fbecf2005-01-23 09:27:24 +000097
Antoine Pitrouc43ba3b2010-05-09 15:15:40 +000098 return v;
Martin v. Löwis79fbecf2005-01-23 09:27:24 +000099}
100
101#endif /* HAVE_GETSPNAM || HAVE_GETSPENT */
102
103
104#ifdef HAVE_GETSPNAM
105
106PyDoc_STRVAR(spwd_getspnam__doc__,
107"getspnam(name) -> (sp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max,\n\
108 sp_warn, sp_inact, sp_expire, sp_flag)\n\
109Return the shadow password database entry for the given user name.\n\
110See spwd.__doc__ for more on shadow password database entries.");
111
112static PyObject* spwd_getspnam(PyObject *self, PyObject *args)
113{
Antoine Pitrouc43ba3b2010-05-09 15:15:40 +0000114 char *name;
115 struct spwd *p;
116 if (!PyArg_ParseTuple(args, "s:getspnam", &name))
117 return NULL;
118 if ((p = getspnam(name)) == NULL) {
119 PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
120 return NULL;
121 }
122 return mkspent(p);
Martin v. Löwis79fbecf2005-01-23 09:27:24 +0000123}
124
Neal Norwitze4683b32005-01-24 23:33:50 +0000125#endif /* HAVE_GETSPNAM */
126
127#ifdef HAVE_GETSPENT
128
Martin v. Löwis79fbecf2005-01-23 09:27:24 +0000129PyDoc_STRVAR(spwd_getspall__doc__,
130"getspall() -> list_of_entries\n\
131Return a list of all available shadow password database entries, \
132in arbitrary order.\n\
133See spwd.__doc__ for more on shadow password database entries.");
134
Martin v. Löwis79fbecf2005-01-23 09:27:24 +0000135static PyObject *
136spwd_getspall(PyObject *self, PyObject *args)
137{
Antoine Pitrouc43ba3b2010-05-09 15:15:40 +0000138 PyObject *d;
139 struct spwd *p;
140 if ((d = PyList_New(0)) == NULL)
141 return NULL;
142 setspent();
143 while ((p = getspent()) != NULL) {
144 PyObject *v = mkspent(p);
145 if (v == NULL || PyList_Append(d, v) != 0) {
146 Py_XDECREF(v);
147 Py_DECREF(d);
148 endspent();
149 return NULL;
150 }
151 Py_DECREF(v);
152 }
153 endspent();
154 return d;
Martin v. Löwis79fbecf2005-01-23 09:27:24 +0000155}
156
157#endif /* HAVE_GETSPENT */
158
159static PyMethodDef spwd_methods[] = {
Antoine Pitrouc43ba3b2010-05-09 15:15:40 +0000160#ifdef HAVE_GETSPNAM
161 {"getspnam", spwd_getspnam, METH_VARARGS, spwd_getspnam__doc__},
Martin v. Löwis79fbecf2005-01-23 09:27:24 +0000162#endif
163#ifdef HAVE_GETSPENT
Antoine Pitrouc43ba3b2010-05-09 15:15:40 +0000164 {"getspall", spwd_getspall, METH_NOARGS, spwd_getspall__doc__},
Martin v. Löwis79fbecf2005-01-23 09:27:24 +0000165#endif
Antoine Pitrouc43ba3b2010-05-09 15:15:40 +0000166 {NULL, NULL} /* sentinel */
Martin v. Löwis79fbecf2005-01-23 09:27:24 +0000167};
168
169
170PyMODINIT_FUNC
171initspwd(void)
172{
Antoine Pitrouc43ba3b2010-05-09 15:15:40 +0000173 PyObject *m;
174 m=Py_InitModule3("spwd", spwd_methods, spwd__doc__);
175 if (m == NULL)
176 return;
177 if (!initialized)
178 PyStructSequence_InitType(&StructSpwdType,
179 &struct_spwd_type_desc);
180 Py_INCREF((PyObject *) &StructSpwdType);
181 PyModule_AddObject(m, "struct_spwd", (PyObject *) &StructSpwdType);
182 initialized = 1;
Martin v. Löwis79fbecf2005-01-23 09:27:24 +0000183}