Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 1 | |
| 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" |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 7 | |
| 8 | #include <sys/types.h> |
| 9 | #ifdef HAVE_SHADOW_H |
| 10 | #include <shadow.h> |
| 11 | #endif |
| 12 | |
| 13 | |
| 14 | PyDoc_STRVAR(spwd__doc__, |
| 15 | "This module provides access to the Unix shadow password database.\n\ |
| 16 | It is available on various Unix versions.\n\ |
| 17 | \n\ |
| 18 | Shadow password database entries are reported as 9-tuples of type struct_spwd,\n\ |
| 19 | containing the following items from the password database (see `<shadow.h>'):\n\ |
| 20 | sp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max, sp_warn, sp_inact, sp_expire, sp_flag.\n\ |
| 21 | The sp_namp and sp_pwdp are strings, the rest are integers.\n\ |
| 22 | An exception is raised if the entry asked for cannot be found.\n\ |
| 23 | You have to be root to be able to use this module."); |
| 24 | |
| 25 | |
| 26 | #if defined(HAVE_GETSPNAM) || defined(HAVE_GETSPENT) |
| 27 | |
| 28 | static PyStructSequence_Field struct_spwd_type_fields[] = { |
R David Murray | bd90d09 | 2013-11-03 19:54:05 -0500 | [diff] [blame] | 29 | {"sp_namp", "login name"}, |
| 30 | {"sp_pwdp", "encrypted password"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | {"sp_lstchg", "date of last change"}, |
| 32 | {"sp_min", "min #days between changes"}, |
| 33 | {"sp_max", "max #days between changes"}, |
| 34 | {"sp_warn", "#days before pw expires to warn user about it"}, |
R David Murray | bd90d09 | 2013-11-03 19:54:05 -0500 | [diff] [blame] | 35 | {"sp_inact", "#days after pw expires until account is disabled"}, |
| 36 | {"sp_expire", "#days since 1970-01-01 when account expires"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | {"sp_flag", "reserved"}, |
R David Murray | bd90d09 | 2013-11-03 19:54:05 -0500 | [diff] [blame] | 38 | {"sp_nam", "login name; deprecated"}, /* Backward compatibility */ |
| 39 | {"sp_pwd", "encrypted password; deprecated"}, /* Backward compatibility */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 40 | {0} |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | PyDoc_STRVAR(struct_spwd__doc__, |
| 44 | "spwd.struct_spwd: Results from getsp*() routines.\n\n\ |
| 45 | This object may be accessed either as a 9-tuple of\n\ |
R David Murray | bd90d09 | 2013-11-03 19:54:05 -0500 | [diff] [blame] | 46 | (sp_namp,sp_pwdp,sp_lstchg,sp_min,sp_max,sp_warn,sp_inact,sp_expire,sp_flag)\n\ |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 47 | or via the object attributes as named in the above tuple."); |
| 48 | |
| 49 | static PyStructSequence_Desc struct_spwd_type_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | "spwd.struct_spwd", |
| 51 | struct_spwd__doc__, |
| 52 | struct_spwd_type_fields, |
| 53 | 9, |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 56 | static int initialized; |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 57 | static PyTypeObject StructSpwdType; |
| 58 | |
| 59 | |
| 60 | static void |
Neal Norwitz | eb8b3a6 | 2007-08-24 23:26:23 +0000 | [diff] [blame] | 61 | sets(PyObject *v, int i, const char* val) |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 62 | { |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 63 | if (val) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | PyObject *o = PyUnicode_DecodeFSDefault(val); |
| 65 | PyStructSequence_SET_ITEM(v, i, o); |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 66 | } else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | PyStructSequence_SET_ITEM(v, i, Py_None); |
| 68 | Py_INCREF(Py_None); |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | static PyObject *mkspent(struct spwd *p) |
| 73 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | int setIndex = 0; |
| 75 | PyObject *v = PyStructSequence_New(&StructSpwdType); |
| 76 | if (v == NULL) |
| 77 | return NULL; |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 78 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 79 | #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val)) |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 80 | #define SETS(i,val) sets(v, i, val) |
| 81 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | SETS(setIndex++, p->sp_namp); |
| 83 | SETS(setIndex++, p->sp_pwdp); |
| 84 | SETI(setIndex++, p->sp_lstchg); |
| 85 | SETI(setIndex++, p->sp_min); |
| 86 | SETI(setIndex++, p->sp_max); |
| 87 | SETI(setIndex++, p->sp_warn); |
| 88 | SETI(setIndex++, p->sp_inact); |
| 89 | SETI(setIndex++, p->sp_expire); |
| 90 | SETI(setIndex++, p->sp_flag); |
R David Murray | bd90d09 | 2013-11-03 19:54:05 -0500 | [diff] [blame] | 91 | SETS(setIndex++, p->sp_namp); /* Backward compatibility for sp_nam */ |
| 92 | SETS(setIndex++, p->sp_pwdp); /* Backward compatibility for sp_pwd */ |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 93 | |
| 94 | #undef SETS |
| 95 | #undef SETI |
| 96 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | if (PyErr_Occurred()) { |
| 98 | Py_DECREF(v); |
| 99 | return NULL; |
| 100 | } |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | return v; |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | #endif /* HAVE_GETSPNAM || HAVE_GETSPENT */ |
| 106 | |
| 107 | |
| 108 | #ifdef HAVE_GETSPNAM |
| 109 | |
| 110 | PyDoc_STRVAR(spwd_getspnam__doc__, |
| 111 | "getspnam(name) -> (sp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max,\n\ |
| 112 | sp_warn, sp_inact, sp_expire, sp_flag)\n\ |
| 113 | Return the shadow password database entry for the given user name.\n\ |
| 114 | See spwd.__doc__ for more on shadow password database entries."); |
| 115 | |
| 116 | static PyObject* spwd_getspnam(PyObject *self, PyObject *args) |
| 117 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | char *name; |
| 119 | struct spwd *p; |
| 120 | PyObject *arg, *bytes, *retval = NULL; |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | if (!PyArg_ParseTuple(args, "U:getspnam", &arg)) |
| 123 | return NULL; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 124 | if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 125 | return NULL; |
| 126 | if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1) |
| 127 | goto out; |
| 128 | if ((p = getspnam(name)) == NULL) { |
| 129 | PyErr_SetString(PyExc_KeyError, "getspnam(): name not found"); |
| 130 | goto out; |
| 131 | } |
| 132 | retval = mkspent(p); |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 133 | out: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | Py_DECREF(bytes); |
| 135 | return retval; |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Neal Norwitz | 6267996 | 2005-01-24 23:33:50 +0000 | [diff] [blame] | 138 | #endif /* HAVE_GETSPNAM */ |
| 139 | |
| 140 | #ifdef HAVE_GETSPENT |
| 141 | |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 142 | PyDoc_STRVAR(spwd_getspall__doc__, |
| 143 | "getspall() -> list_of_entries\n\ |
| 144 | Return a list of all available shadow password database entries, \ |
| 145 | in arbitrary order.\n\ |
| 146 | See spwd.__doc__ for more on shadow password database entries."); |
| 147 | |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 148 | static PyObject * |
| 149 | spwd_getspall(PyObject *self, PyObject *args) |
| 150 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | PyObject *d; |
| 152 | struct spwd *p; |
| 153 | if ((d = PyList_New(0)) == NULL) |
| 154 | return NULL; |
| 155 | setspent(); |
| 156 | while ((p = getspent()) != NULL) { |
| 157 | PyObject *v = mkspent(p); |
| 158 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 159 | Py_XDECREF(v); |
| 160 | Py_DECREF(d); |
| 161 | endspent(); |
| 162 | return NULL; |
| 163 | } |
| 164 | Py_DECREF(v); |
| 165 | } |
| 166 | endspent(); |
| 167 | return d; |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | #endif /* HAVE_GETSPENT */ |
| 171 | |
| 172 | static PyMethodDef spwd_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 173 | #ifdef HAVE_GETSPNAM |
| 174 | {"getspnam", spwd_getspnam, METH_VARARGS, spwd_getspnam__doc__}, |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 175 | #endif |
| 176 | #ifdef HAVE_GETSPENT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | {"getspall", spwd_getspall, METH_NOARGS, spwd_getspall__doc__}, |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 178 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | {NULL, NULL} /* sentinel */ |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 183 | |
| 184 | static struct PyModuleDef spwdmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | PyModuleDef_HEAD_INIT, |
| 186 | "spwd", |
| 187 | spwd__doc__, |
| 188 | -1, |
| 189 | spwd_methods, |
| 190 | NULL, |
| 191 | NULL, |
| 192 | NULL, |
| 193 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 196 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 197 | PyInit_spwd(void) |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 198 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | PyObject *m; |
| 200 | m=PyModule_Create(&spwdmodule); |
| 201 | if (m == NULL) |
| 202 | return NULL; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 203 | if (!initialized) { |
| 204 | if (PyStructSequence_InitType2(&StructSpwdType, |
| 205 | &struct_spwd_type_desc) < 0) |
| 206 | return NULL; |
| 207 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | Py_INCREF((PyObject *) &StructSpwdType); |
| 209 | PyModule_AddObject(m, "struct_spwd", (PyObject *) &StructSpwdType); |
| 210 | initialized = 1; |
| 211 | return m; |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 212 | } |