/* UNIX shadow password file access module */ #include "Python.h" #include #include static char spwd__doc__ [] = "\ This module provides access to the Unix shadow password database.\n\ It is available on all Unix versions.\n\ \n\ Shadow password database entries are reported as 9-tuples containing the following\n\ items from the password database (see `'), in order:\n\ sp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max, sp_warn, sp_inact, sp_expire, sp_flag.\n\ The sp_namp and sp_pwdp are strings, the rest are integers. An\n\ exception is raised if the entry asked for cannot be found."; static PyObject * mkspent(struct spwd *p) { return Py_BuildValue( "(sslllllll)", p->sp_namp, p->sp_pwdp, (long)p->sp_lstchg, (long)p->sp_min, (long)p->sp_max, (long)p->sp_warn, (long)p->sp_inact, (long)p->sp_expire, (long)p->sp_flag ); } static char spwd_getspnam__doc__[] = "\ getspnam(name) -> entry\n\ Return the shadow password database entry for the given user name.\n\ See spwd.__doc__ for more on shadow password database entries."; static PyObject * spwd_getspnam(PyObject *self, PyObject *args) { char *name; struct spwd *p; if (!PyArg_Parse(args, "s", &name)) return NULL; if ((p = getspnam(name)) == NULL) { PyErr_SetString(PyExc_KeyError, "getspnam(): name not found"); return NULL; } return mkspent(p); } static char spwd_getspall__doc__[] = "\ getspall() -> list_of_entries\n\ Return a list of all available shadow password database entries, \ in arbitrary order.\n\ See spwd.__doc__ for more on shadow password database entries."; static PyObject * spwd_getspall(PyObject *self, PyObject *args) { PyObject *d; struct spwd *p; if (!PyArg_NoArgs(args)) return NULL; if ((d = PyList_New(0)) == NULL) return NULL; setspent(); while ((p = getspent()) != NULL) { PyObject *v = mkspent(p); if (v == NULL || PyList_Append(d, v) != 0) { Py_XDECREF(v); Py_DECREF(d); return NULL; } Py_DECREF(v); } endspent(); return d; } static PyMethodDef spwd_methods[] = { {"getspnam", spwd_getspnam, METH_OLDARGS, spwd_getspnam__doc__}, {"getspall", spwd_getspall, METH_OLDARGS, spwd_getspall__doc__}, {NULL, NULL} /* sentinel */ }; DL_EXPORT(void) initspwd(void) { Py_InitModule4("spwd", spwd_methods, spwd__doc__, (PyObject *)NULL, PYTHON_API_VERSION); }