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 | |
Brett Cannon | 52d67ef | 2014-08-22 14:01:56 -0400 | [diff] [blame] | 13 | #include "clinic/spwdmodule.c.h" |
| 14 | |
Brett Cannon | 20cf6dd | 2014-08-22 13:59:24 -0400 | [diff] [blame] | 15 | /*[clinic input] |
Brett Cannon | 20cf6dd | 2014-08-22 13:59:24 -0400 | [diff] [blame] | 16 | module spwd |
| 17 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 18 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c0b841b90a6a07ce]*/ |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 19 | |
| 20 | PyDoc_STRVAR(spwd__doc__, |
| 21 | "This module provides access to the Unix shadow password database.\n\ |
| 22 | It is available on various Unix versions.\n\ |
| 23 | \n\ |
| 24 | Shadow password database entries are reported as 9-tuples of type struct_spwd,\n\ |
| 25 | containing the following items from the password database (see `<shadow.h>'):\n\ |
| 26 | sp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max, sp_warn, sp_inact, sp_expire, sp_flag.\n\ |
| 27 | The sp_namp and sp_pwdp are strings, the rest are integers.\n\ |
| 28 | An exception is raised if the entry asked for cannot be found.\n\ |
| 29 | You have to be root to be able to use this module."); |
| 30 | |
| 31 | |
| 32 | #if defined(HAVE_GETSPNAM) || defined(HAVE_GETSPENT) |
| 33 | |
| 34 | static PyStructSequence_Field struct_spwd_type_fields[] = { |
R David Murray | bd90d09 | 2013-11-03 19:54:05 -0500 | [diff] [blame] | 35 | {"sp_namp", "login name"}, |
| 36 | {"sp_pwdp", "encrypted password"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | {"sp_lstchg", "date of last change"}, |
| 38 | {"sp_min", "min #days between changes"}, |
| 39 | {"sp_max", "max #days between changes"}, |
| 40 | {"sp_warn", "#days before pw expires to warn user about it"}, |
R David Murray | bd90d09 | 2013-11-03 19:54:05 -0500 | [diff] [blame] | 41 | {"sp_inact", "#days after pw expires until account is disabled"}, |
| 42 | {"sp_expire", "#days since 1970-01-01 when account expires"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | {"sp_flag", "reserved"}, |
R David Murray | bd90d09 | 2013-11-03 19:54:05 -0500 | [diff] [blame] | 44 | {"sp_nam", "login name; deprecated"}, /* Backward compatibility */ |
| 45 | {"sp_pwd", "encrypted password; deprecated"}, /* Backward compatibility */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | {0} |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | PyDoc_STRVAR(struct_spwd__doc__, |
| 50 | "spwd.struct_spwd: Results from getsp*() routines.\n\n\ |
| 51 | 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] | 52 | (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] | 53 | or via the object attributes as named in the above tuple."); |
| 54 | |
| 55 | static PyStructSequence_Desc struct_spwd_type_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | "spwd.struct_spwd", |
| 57 | struct_spwd__doc__, |
| 58 | struct_spwd_type_fields, |
| 59 | 9, |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
Christian Heimes | bf9d70a | 2020-11-19 10:54:03 +0100 | [diff] [blame] | 62 | typedef struct { |
| 63 | PyTypeObject *StructSpwdType; |
| 64 | } spwdmodulestate; |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 65 | |
Christian Heimes | bf9d70a | 2020-11-19 10:54:03 +0100 | [diff] [blame] | 66 | static inline spwdmodulestate* |
| 67 | get_spwd_state(PyObject *module) |
| 68 | { |
| 69 | void *state = PyModule_GetState(module); |
| 70 | assert(state != NULL); |
| 71 | return (spwdmodulestate *)state; |
| 72 | } |
| 73 | |
| 74 | static struct PyModuleDef spwdmodule; |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 75 | |
| 76 | static void |
Neal Norwitz | eb8b3a6 | 2007-08-24 23:26:23 +0000 | [diff] [blame] | 77 | sets(PyObject *v, int i, const char* val) |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 78 | { |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 79 | if (val) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | PyObject *o = PyUnicode_DecodeFSDefault(val); |
| 81 | PyStructSequence_SET_ITEM(v, i, o); |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 82 | } else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | PyStructSequence_SET_ITEM(v, i, Py_None); |
| 84 | Py_INCREF(Py_None); |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Christian Heimes | bf9d70a | 2020-11-19 10:54:03 +0100 | [diff] [blame] | 88 | static PyObject *mkspent(PyObject *module, struct spwd *p) |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 89 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 90 | int setIndex = 0; |
Christian Heimes | bf9d70a | 2020-11-19 10:54:03 +0100 | [diff] [blame] | 91 | PyObject *v = PyStructSequence_New(get_spwd_state(module)->StructSpwdType); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | if (v == NULL) |
| 93 | return NULL; |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 94 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 95 | #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] | 96 | #define SETS(i,val) sets(v, i, val) |
| 97 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | SETS(setIndex++, p->sp_namp); |
| 99 | SETS(setIndex++, p->sp_pwdp); |
| 100 | SETI(setIndex++, p->sp_lstchg); |
| 101 | SETI(setIndex++, p->sp_min); |
| 102 | SETI(setIndex++, p->sp_max); |
| 103 | SETI(setIndex++, p->sp_warn); |
| 104 | SETI(setIndex++, p->sp_inact); |
| 105 | SETI(setIndex++, p->sp_expire); |
| 106 | SETI(setIndex++, p->sp_flag); |
R David Murray | bd90d09 | 2013-11-03 19:54:05 -0500 | [diff] [blame] | 107 | SETS(setIndex++, p->sp_namp); /* Backward compatibility for sp_nam */ |
| 108 | SETS(setIndex++, p->sp_pwdp); /* Backward compatibility for sp_pwd */ |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 109 | |
| 110 | #undef SETS |
| 111 | #undef SETI |
| 112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | if (PyErr_Occurred()) { |
| 114 | Py_DECREF(v); |
| 115 | return NULL; |
| 116 | } |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | return v; |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | #endif /* HAVE_GETSPNAM || HAVE_GETSPENT */ |
| 122 | |
| 123 | |
| 124 | #ifdef HAVE_GETSPNAM |
| 125 | |
Brett Cannon | 20cf6dd | 2014-08-22 13:59:24 -0400 | [diff] [blame] | 126 | /*[clinic input] |
| 127 | spwd.getspnam |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 128 | |
Brett Cannon | 20cf6dd | 2014-08-22 13:59:24 -0400 | [diff] [blame] | 129 | arg: unicode |
| 130 | / |
| 131 | |
| 132 | Return the shadow password database entry for the given user name. |
| 133 | |
| 134 | See `help(spwd)` for more on shadow password database entries. |
| 135 | [clinic start generated code]*/ |
| 136 | |
| 137 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 138 | spwd_getspnam_impl(PyObject *module, PyObject *arg) |
| 139 | /*[clinic end generated code: output=701250cf57dc6ebe input=dd89429e6167a00f]*/ |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 140 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | char *name; |
| 142 | struct spwd *p; |
Brett Cannon | 20cf6dd | 2014-08-22 13:59:24 -0400 | [diff] [blame] | 143 | PyObject *bytes, *retval = NULL; |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 144 | |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 145 | if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | return NULL; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 147 | /* check for embedded null bytes */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1) |
| 149 | goto out; |
| 150 | if ((p = getspnam(name)) == NULL) { |
Berker Peksag | 3c3d7f4 | 2016-03-19 11:44:17 +0200 | [diff] [blame] | 151 | if (errno != 0) |
| 152 | PyErr_SetFromErrno(PyExc_OSError); |
| 153 | else |
| 154 | PyErr_SetString(PyExc_KeyError, "getspnam(): name not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | goto out; |
| 156 | } |
Christian Heimes | bf9d70a | 2020-11-19 10:54:03 +0100 | [diff] [blame] | 157 | retval = mkspent(module, p); |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 158 | out: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | Py_DECREF(bytes); |
| 160 | return retval; |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Neal Norwitz | 6267996 | 2005-01-24 23:33:50 +0000 | [diff] [blame] | 163 | #endif /* HAVE_GETSPNAM */ |
| 164 | |
| 165 | #ifdef HAVE_GETSPENT |
| 166 | |
Brett Cannon | 20cf6dd | 2014-08-22 13:59:24 -0400 | [diff] [blame] | 167 | /*[clinic input] |
| 168 | spwd.getspall |
| 169 | |
| 170 | Return a list of all available shadow password database entries, in arbitrary order. |
| 171 | |
| 172 | See `help(spwd)` for more on shadow password database entries. |
| 173 | [clinic start generated code]*/ |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 174 | |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 175 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 176 | spwd_getspall_impl(PyObject *module) |
| 177 | /*[clinic end generated code: output=4fda298d6bf6d057 input=b2c84b7857d622bd]*/ |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 178 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | PyObject *d; |
| 180 | struct spwd *p; |
| 181 | if ((d = PyList_New(0)) == NULL) |
| 182 | return NULL; |
| 183 | setspent(); |
| 184 | while ((p = getspent()) != NULL) { |
Christian Heimes | bf9d70a | 2020-11-19 10:54:03 +0100 | [diff] [blame] | 185 | PyObject *v = mkspent(module, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 187 | Py_XDECREF(v); |
| 188 | Py_DECREF(d); |
| 189 | endspent(); |
| 190 | return NULL; |
| 191 | } |
| 192 | Py_DECREF(v); |
| 193 | } |
| 194 | endspent(); |
| 195 | return d; |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | #endif /* HAVE_GETSPENT */ |
| 199 | |
| 200 | static PyMethodDef spwd_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | #ifdef HAVE_GETSPNAM |
Brett Cannon | 20cf6dd | 2014-08-22 13:59:24 -0400 | [diff] [blame] | 202 | SPWD_GETSPNAM_METHODDEF |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 203 | #endif |
| 204 | #ifdef HAVE_GETSPENT |
Brett Cannon | 20cf6dd | 2014-08-22 13:59:24 -0400 | [diff] [blame] | 205 | SPWD_GETSPALL_METHODDEF |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 206 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | {NULL, NULL} /* sentinel */ |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
Christian Heimes | bf9d70a | 2020-11-19 10:54:03 +0100 | [diff] [blame] | 210 | static int |
| 211 | spwdmodule_exec(PyObject *module) |
| 212 | { |
| 213 | spwdmodulestate *state = get_spwd_state(module); |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 214 | |
Christian Heimes | bf9d70a | 2020-11-19 10:54:03 +0100 | [diff] [blame] | 215 | state->StructSpwdType = PyStructSequence_NewType(&struct_spwd_type_desc); |
| 216 | if (state->StructSpwdType == NULL) { |
| 217 | return -1; |
| 218 | } |
| 219 | if (PyModule_AddType(module, state->StructSpwdType) < 0) { |
| 220 | return -1; |
| 221 | } |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static PyModuleDef_Slot spwdmodule_slots[] = { |
| 226 | {Py_mod_exec, spwdmodule_exec}, |
| 227 | {0, NULL} |
| 228 | }; |
| 229 | |
| 230 | static int spwdmodule_traverse(PyObject *m, visitproc visit, void *arg) { |
| 231 | Py_VISIT(get_spwd_state(m)->StructSpwdType); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static int spwdmodule_clear(PyObject *m) { |
| 236 | Py_CLEAR(get_spwd_state(m)->StructSpwdType); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static void spwdmodule_free(void *m) { |
| 241 | spwdmodule_clear((PyObject *)m); |
| 242 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 243 | |
| 244 | static struct PyModuleDef spwdmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | PyModuleDef_HEAD_INIT, |
Christian Heimes | bf9d70a | 2020-11-19 10:54:03 +0100 | [diff] [blame] | 246 | .m_name = "spwd", |
| 247 | .m_doc = spwd__doc__, |
| 248 | .m_size = sizeof(spwdmodulestate), |
| 249 | .m_methods = spwd_methods, |
| 250 | .m_slots = spwdmodule_slots, |
| 251 | .m_traverse = spwdmodule_traverse, |
| 252 | .m_clear = spwdmodule_clear, |
| 253 | .m_free = spwdmodule_free, |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 256 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 257 | PyInit_spwd(void) |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 258 | { |
Christian Heimes | bf9d70a | 2020-11-19 10:54:03 +0100 | [diff] [blame] | 259 | return PyModuleDef_Init(&spwdmodule); |
Martin v. Löwis | c300175 | 2005-01-23 09:27:24 +0000 | [diff] [blame] | 260 | } |