| 1 | n/a | |
|---|
| 2 | n/a | /* UNIX password file access module */ |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | #include "Python.h" |
|---|
| 5 | n/a | #include "posixmodule.h" |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | #include <pwd.h> |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | #include "clinic/pwdmodule.c.h" |
|---|
| 10 | n/a | /*[clinic input] |
|---|
| 11 | n/a | module pwd |
|---|
| 12 | n/a | [clinic start generated code]*/ |
|---|
| 13 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=60f628ef356b97b6]*/ |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | static PyStructSequence_Field struct_pwd_type_fields[] = { |
|---|
| 16 | n/a | {"pw_name", "user name"}, |
|---|
| 17 | n/a | {"pw_passwd", "password"}, |
|---|
| 18 | n/a | {"pw_uid", "user id"}, |
|---|
| 19 | n/a | {"pw_gid", "group id"}, |
|---|
| 20 | n/a | {"pw_gecos", "real name"}, |
|---|
| 21 | n/a | {"pw_dir", "home directory"}, |
|---|
| 22 | n/a | {"pw_shell", "shell program"}, |
|---|
| 23 | n/a | {0} |
|---|
| 24 | n/a | }; |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | PyDoc_STRVAR(struct_passwd__doc__, |
|---|
| 27 | n/a | "pwd.struct_passwd: Results from getpw*() routines.\n\n\ |
|---|
| 28 | n/a | This object may be accessed either as a tuple of\n\ |
|---|
| 29 | n/a | (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\ |
|---|
| 30 | n/a | or via the object attributes as named in the above tuple."); |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | static PyStructSequence_Desc struct_pwd_type_desc = { |
|---|
| 33 | n/a | "pwd.struct_passwd", |
|---|
| 34 | n/a | struct_passwd__doc__, |
|---|
| 35 | n/a | struct_pwd_type_fields, |
|---|
| 36 | n/a | 7, |
|---|
| 37 | n/a | }; |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | PyDoc_STRVAR(pwd__doc__, |
|---|
| 40 | n/a | "This module provides access to the Unix password database.\n\ |
|---|
| 41 | n/a | It is available on all Unix versions.\n\ |
|---|
| 42 | n/a | \n\ |
|---|
| 43 | n/a | Password database entries are reported as 7-tuples containing the following\n\ |
|---|
| 44 | n/a | items from the password database (see `<pwd.h>'), in order:\n\ |
|---|
| 45 | n/a | pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\ |
|---|
| 46 | n/a | The uid and gid items are integers, all others are strings. An\n\ |
|---|
| 47 | n/a | exception is raised if the entry asked for cannot be found."); |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | static int initialized; |
|---|
| 51 | n/a | static PyTypeObject StructPwdType; |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | static void |
|---|
| 54 | n/a | sets(PyObject *v, int i, const char* val) |
|---|
| 55 | n/a | { |
|---|
| 56 | n/a | if (val) { |
|---|
| 57 | n/a | PyObject *o = PyUnicode_DecodeFSDefault(val); |
|---|
| 58 | n/a | PyStructSequence_SET_ITEM(v, i, o); |
|---|
| 59 | n/a | } |
|---|
| 60 | n/a | else { |
|---|
| 61 | n/a | PyStructSequence_SET_ITEM(v, i, Py_None); |
|---|
| 62 | n/a | Py_INCREF(Py_None); |
|---|
| 63 | n/a | } |
|---|
| 64 | n/a | } |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | static PyObject * |
|---|
| 67 | n/a | mkpwent(struct passwd *p) |
|---|
| 68 | n/a | { |
|---|
| 69 | n/a | int setIndex = 0; |
|---|
| 70 | n/a | PyObject *v = PyStructSequence_New(&StructPwdType); |
|---|
| 71 | n/a | if (v == NULL) |
|---|
| 72 | n/a | return NULL; |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val)) |
|---|
| 75 | n/a | #define SETS(i,val) sets(v, i, val) |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | SETS(setIndex++, p->pw_name); |
|---|
| 78 | n/a | #if defined(HAVE_STRUCT_PASSWD_PW_PASSWD) && !defined(__ANDROID__) |
|---|
| 79 | n/a | SETS(setIndex++, p->pw_passwd); |
|---|
| 80 | n/a | #else |
|---|
| 81 | n/a | SETS(setIndex++, ""); |
|---|
| 82 | n/a | #endif |
|---|
| 83 | n/a | PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid)); |
|---|
| 84 | n/a | PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid)); |
|---|
| 85 | n/a | #if defined(HAVE_STRUCT_PASSWD_PW_GECOS) |
|---|
| 86 | n/a | SETS(setIndex++, p->pw_gecos); |
|---|
| 87 | n/a | #else |
|---|
| 88 | n/a | SETS(setIndex++, ""); |
|---|
| 89 | n/a | #endif |
|---|
| 90 | n/a | SETS(setIndex++, p->pw_dir); |
|---|
| 91 | n/a | SETS(setIndex++, p->pw_shell); |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | #undef SETS |
|---|
| 94 | n/a | #undef SETI |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | if (PyErr_Occurred()) { |
|---|
| 97 | n/a | Py_XDECREF(v); |
|---|
| 98 | n/a | return NULL; |
|---|
| 99 | n/a | } |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | return v; |
|---|
| 102 | n/a | } |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | /*[clinic input] |
|---|
| 105 | n/a | pwd.getpwuid |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | uidobj: object |
|---|
| 108 | n/a | / |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | Return the password database entry for the given numeric user ID. |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | See `help(pwd)` for more on password database entries. |
|---|
| 113 | n/a | [clinic start generated code]*/ |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | static PyObject * |
|---|
| 116 | n/a | pwd_getpwuid(PyObject *module, PyObject *uidobj) |
|---|
| 117 | n/a | /*[clinic end generated code: output=c4ee1d4d429b86c4 input=ae64d507a1c6d3e8]*/ |
|---|
| 118 | n/a | { |
|---|
| 119 | n/a | uid_t uid; |
|---|
| 120 | n/a | struct passwd *p; |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | if (!_Py_Uid_Converter(uidobj, &uid)) { |
|---|
| 123 | n/a | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
|---|
| 124 | n/a | PyErr_Format(PyExc_KeyError, |
|---|
| 125 | n/a | "getpwuid(): uid not found"); |
|---|
| 126 | n/a | return NULL; |
|---|
| 127 | n/a | } |
|---|
| 128 | n/a | if ((p = getpwuid(uid)) == NULL) { |
|---|
| 129 | n/a | PyObject *uid_obj = _PyLong_FromUid(uid); |
|---|
| 130 | n/a | if (uid_obj == NULL) |
|---|
| 131 | n/a | return NULL; |
|---|
| 132 | n/a | PyErr_Format(PyExc_KeyError, |
|---|
| 133 | n/a | "getpwuid(): uid not found: %S", uid_obj); |
|---|
| 134 | n/a | Py_DECREF(uid_obj); |
|---|
| 135 | n/a | return NULL; |
|---|
| 136 | n/a | } |
|---|
| 137 | n/a | return mkpwent(p); |
|---|
| 138 | n/a | } |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | /*[clinic input] |
|---|
| 141 | n/a | pwd.getpwnam |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | arg: unicode |
|---|
| 144 | n/a | / |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | Return the password database entry for the given user name. |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | See `help(pwd)` for more on password database entries. |
|---|
| 149 | n/a | [clinic start generated code]*/ |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | static PyObject * |
|---|
| 152 | n/a | pwd_getpwnam_impl(PyObject *module, PyObject *arg) |
|---|
| 153 | n/a | /*[clinic end generated code: output=6abeee92430e43d2 input=d5f7e700919b02d3]*/ |
|---|
| 154 | n/a | { |
|---|
| 155 | n/a | char *name; |
|---|
| 156 | n/a | struct passwd *p; |
|---|
| 157 | n/a | PyObject *bytes, *retval = NULL; |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL) |
|---|
| 160 | n/a | return NULL; |
|---|
| 161 | n/a | if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1) |
|---|
| 162 | n/a | goto out; |
|---|
| 163 | n/a | if ((p = getpwnam(name)) == NULL) { |
|---|
| 164 | n/a | PyErr_Format(PyExc_KeyError, |
|---|
| 165 | n/a | "getpwnam(): name not found: %s", name); |
|---|
| 166 | n/a | goto out; |
|---|
| 167 | n/a | } |
|---|
| 168 | n/a | retval = mkpwent(p); |
|---|
| 169 | n/a | out: |
|---|
| 170 | n/a | Py_DECREF(bytes); |
|---|
| 171 | n/a | return retval; |
|---|
| 172 | n/a | } |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | #ifdef HAVE_GETPWENT |
|---|
| 175 | n/a | /*[clinic input] |
|---|
| 176 | n/a | pwd.getpwall |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | Return a list of all available password database entries, in arbitrary order. |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | See help(pwd) for more on password database entries. |
|---|
| 181 | n/a | [clinic start generated code]*/ |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | static PyObject * |
|---|
| 184 | n/a | pwd_getpwall_impl(PyObject *module) |
|---|
| 185 | n/a | /*[clinic end generated code: output=4853d2f5a0afac8a input=d7ecebfd90219b85]*/ |
|---|
| 186 | n/a | { |
|---|
| 187 | n/a | PyObject *d; |
|---|
| 188 | n/a | struct passwd *p; |
|---|
| 189 | n/a | if ((d = PyList_New(0)) == NULL) |
|---|
| 190 | n/a | return NULL; |
|---|
| 191 | n/a | setpwent(); |
|---|
| 192 | n/a | while ((p = getpwent()) != NULL) { |
|---|
| 193 | n/a | PyObject *v = mkpwent(p); |
|---|
| 194 | n/a | if (v == NULL || PyList_Append(d, v) != 0) { |
|---|
| 195 | n/a | Py_XDECREF(v); |
|---|
| 196 | n/a | Py_DECREF(d); |
|---|
| 197 | n/a | endpwent(); |
|---|
| 198 | n/a | return NULL; |
|---|
| 199 | n/a | } |
|---|
| 200 | n/a | Py_DECREF(v); |
|---|
| 201 | n/a | } |
|---|
| 202 | n/a | endpwent(); |
|---|
| 203 | n/a | return d; |
|---|
| 204 | n/a | } |
|---|
| 205 | n/a | #endif |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | static PyMethodDef pwd_methods[] = { |
|---|
| 208 | n/a | PWD_GETPWUID_METHODDEF |
|---|
| 209 | n/a | PWD_GETPWNAM_METHODDEF |
|---|
| 210 | n/a | #ifdef HAVE_GETPWENT |
|---|
| 211 | n/a | PWD_GETPWALL_METHODDEF |
|---|
| 212 | n/a | #endif |
|---|
| 213 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 214 | n/a | }; |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | static struct PyModuleDef pwdmodule = { |
|---|
| 217 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 218 | n/a | "pwd", |
|---|
| 219 | n/a | pwd__doc__, |
|---|
| 220 | n/a | -1, |
|---|
| 221 | n/a | pwd_methods, |
|---|
| 222 | n/a | NULL, |
|---|
| 223 | n/a | NULL, |
|---|
| 224 | n/a | NULL, |
|---|
| 225 | n/a | NULL |
|---|
| 226 | n/a | }; |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | |
|---|
| 229 | n/a | PyMODINIT_FUNC |
|---|
| 230 | n/a | PyInit_pwd(void) |
|---|
| 231 | n/a | { |
|---|
| 232 | n/a | PyObject *m; |
|---|
| 233 | n/a | m = PyModule_Create(&pwdmodule); |
|---|
| 234 | n/a | if (m == NULL) |
|---|
| 235 | n/a | return NULL; |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | if (!initialized) { |
|---|
| 238 | n/a | if (PyStructSequence_InitType2(&StructPwdType, |
|---|
| 239 | n/a | &struct_pwd_type_desc) < 0) |
|---|
| 240 | n/a | return NULL; |
|---|
| 241 | n/a | initialized = 1; |
|---|
| 242 | n/a | } |
|---|
| 243 | n/a | Py_INCREF((PyObject *) &StructPwdType); |
|---|
| 244 | n/a | PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType); |
|---|
| 245 | n/a | return m; |
|---|
| 246 | n/a | } |
|---|