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