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