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