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 | |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 53 | #define DEFAULT_BUFFER_SIZE 1024 |
| 54 | |
Martin v. Löwis | 29275c9 | 2002-09-17 09:34:06 +0000 | [diff] [blame] | 55 | static void |
Neal Norwitz | eb8b3a6 | 2007-08-24 23:26:23 +0000 | [diff] [blame] | 56 | sets(PyObject *v, int i, const char* val) |
Martin v. Löwis | 29275c9 | 2002-09-17 09:34:06 +0000 | [diff] [blame] | 57 | { |
Neal Norwitz | 3d7a90d | 2007-10-27 05:40:06 +0000 | [diff] [blame] | 58 | if (val) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 59 | PyObject *o = PyUnicode_DecodeFSDefault(val); |
| 60 | PyStructSequence_SET_ITEM(v, i, o); |
Neal Norwitz | 3d7a90d | 2007-10-27 05:40:06 +0000 | [diff] [blame] | 61 | } |
Martin v. Löwis | 29275c9 | 2002-09-17 09:34:06 +0000 | [diff] [blame] | 62 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | PyStructSequence_SET_ITEM(v, i, Py_None); |
| 64 | Py_INCREF(Py_None); |
Martin v. Löwis | 29275c9 | 2002-09-17 09:34:06 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 68 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 69 | mkpwent(struct passwd *p) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 70 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | int setIndex = 0; |
| 72 | PyObject *v = PyStructSequence_New(&StructPwdType); |
| 73 | if (v == NULL) |
| 74 | return NULL; |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 75 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 76 | #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] | 77 | #define SETS(i,val) sets(v, i, val) |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 78 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | SETS(setIndex++, p->pw_name); |
Stefan Krah | 4500977 | 2016-04-26 11:43:21 +0200 | [diff] [blame] | 80 | #if defined(HAVE_STRUCT_PASSWD_PW_PASSWD) && !defined(__ANDROID__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | SETS(setIndex++, p->pw_passwd); |
Stefan Krah | 267b639 | 2016-04-26 01:09:18 +0200 | [diff] [blame] | 82 | #else |
| 83 | SETS(setIndex++, ""); |
| 84 | #endif |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 85 | PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid)); |
| 86 | PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid)); |
Stefan Krah | 267b639 | 2016-04-26 01:09:18 +0200 | [diff] [blame] | 87 | #if defined(HAVE_STRUCT_PASSWD_PW_GECOS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | SETS(setIndex++, p->pw_gecos); |
Stefan Krah | 267b639 | 2016-04-26 01:09:18 +0200 | [diff] [blame] | 89 | #else |
| 90 | SETS(setIndex++, ""); |
| 91 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | SETS(setIndex++, p->pw_dir); |
| 93 | SETS(setIndex++, p->pw_shell); |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 94 | |
| 95 | #undef SETS |
| 96 | #undef SETI |
| 97 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | if (PyErr_Occurred()) { |
| 99 | Py_XDECREF(v); |
| 100 | return NULL; |
| 101 | } |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | return v; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Brett Cannon | 3d25e16 | 2014-08-22 14:03:51 -0400 | [diff] [blame] | 106 | /*[clinic input] |
| 107 | pwd.getpwuid |
| 108 | |
| 109 | uidobj: object |
| 110 | / |
| 111 | |
| 112 | Return the password database entry for the given numeric user ID. |
| 113 | |
| 114 | See `help(pwd)` for more on password database entries. |
| 115 | [clinic start generated code]*/ |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 116 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 117 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 118 | pwd_getpwuid(PyObject *module, PyObject *uidobj) |
| 119 | /*[clinic end generated code: output=c4ee1d4d429b86c4 input=ae64d507a1c6d3e8]*/ |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 120 | { |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 121 | PyObject *retval = NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 122 | uid_t uid; |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 123 | int nomem = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | struct passwd *p; |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 125 | char *buf = NULL, *buf2 = NULL; |
Brett Cannon | 3d25e16 | 2014-08-22 14:03:51 -0400 | [diff] [blame] | 126 | |
| 127 | if (!_Py_Uid_Converter(uidobj, &uid)) { |
Serhiy Storchaka | 55e2238 | 2013-02-11 20:32:47 +0200 | [diff] [blame] | 128 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 129 | PyErr_Format(PyExc_KeyError, |
| 130 | "getpwuid(): uid not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | return NULL; |
Serhiy Storchaka | 55e2238 | 2013-02-11 20:32:47 +0200 | [diff] [blame] | 132 | } |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 133 | #ifdef HAVE_GETPWUID_R |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 134 | int status; |
| 135 | Py_ssize_t bufsize; |
Alexey Izbyshev | e359bc2 | 2018-11-04 18:44:16 +0300 | [diff] [blame] | 136 | /* Note: 'pwd' will be used via pointer 'p' on getpwuid_r success. */ |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 137 | struct passwd pwd; |
| 138 | |
Alexey Izbyshev | e359bc2 | 2018-11-04 18:44:16 +0300 | [diff] [blame] | 139 | Py_BEGIN_ALLOW_THREADS |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 140 | bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 141 | if (bufsize == -1) { |
| 142 | bufsize = DEFAULT_BUFFER_SIZE; |
| 143 | } |
| 144 | |
| 145 | while(1) { |
| 146 | buf2 = PyMem_RawRealloc(buf, bufsize); |
| 147 | if (buf2 == NULL) { |
Zackery Spytz | 570e371 | 2018-11-05 12:26:40 -0700 | [diff] [blame] | 148 | p = NULL; |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 149 | nomem = 1; |
| 150 | break; |
| 151 | } |
| 152 | buf = buf2; |
| 153 | status = getpwuid_r(uid, &pwd, buf, bufsize, &p); |
| 154 | if (status != 0) { |
| 155 | p = NULL; |
| 156 | } |
| 157 | if (p != NULL || status != ERANGE) { |
| 158 | break; |
| 159 | } |
| 160 | if (bufsize > (PY_SSIZE_T_MAX >> 1)) { |
| 161 | nomem = 1; |
| 162 | break; |
| 163 | } |
| 164 | bufsize <<= 1; |
| 165 | } |
| 166 | |
| 167 | Py_END_ALLOW_THREADS |
| 168 | #else |
| 169 | p = getpwuid(uid); |
| 170 | #endif |
| 171 | if (p == NULL) { |
| 172 | PyMem_RawFree(buf); |
| 173 | if (nomem == 1) { |
| 174 | return PyErr_NoMemory(); |
| 175 | } |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 176 | PyObject *uid_obj = _PyLong_FromUid(uid); |
| 177 | if (uid_obj == NULL) |
| 178 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | PyErr_Format(PyExc_KeyError, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 180 | "getpwuid(): uid not found: %S", uid_obj); |
| 181 | Py_DECREF(uid_obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | return NULL; |
| 183 | } |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 184 | retval = mkpwent(p); |
| 185 | #ifdef HAVE_GETPWUID_R |
| 186 | PyMem_RawFree(buf); |
| 187 | #endif |
| 188 | return retval; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Brett Cannon | 3d25e16 | 2014-08-22 14:03:51 -0400 | [diff] [blame] | 191 | /*[clinic input] |
| 192 | pwd.getpwnam |
| 193 | |
William Grzybowski | 2865848 | 2018-09-07 14:10:39 -0300 | [diff] [blame] | 194 | name: unicode |
Brett Cannon | 3d25e16 | 2014-08-22 14:03:51 -0400 | [diff] [blame] | 195 | / |
| 196 | |
| 197 | Return the password database entry for the given user name. |
| 198 | |
| 199 | See `help(pwd)` for more on password database entries. |
| 200 | [clinic start generated code]*/ |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 201 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 202 | static PyObject * |
William Grzybowski | 2865848 | 2018-09-07 14:10:39 -0300 | [diff] [blame] | 203 | pwd_getpwnam_impl(PyObject *module, PyObject *name) |
| 204 | /*[clinic end generated code: output=359ce1ddeb7a824f input=a6aeb5e3447fb9e0]*/ |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 205 | { |
William Grzybowski | 2865848 | 2018-09-07 14:10:39 -0300 | [diff] [blame] | 206 | char *buf = NULL, *buf2 = NULL, *name_chars; |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 207 | int nomem = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | struct passwd *p; |
Brett Cannon | 3d25e16 | 2014-08-22 14:03:51 -0400 | [diff] [blame] | 209 | PyObject *bytes, *retval = NULL; |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 210 | |
William Grzybowski | 2865848 | 2018-09-07 14:10:39 -0300 | [diff] [blame] | 211 | if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 212 | return NULL; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 213 | /* check for embedded null bytes */ |
William Grzybowski | 2865848 | 2018-09-07 14:10:39 -0300 | [diff] [blame] | 214 | if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | goto out; |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 216 | #ifdef HAVE_GETPWNAM_R |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 217 | int status; |
| 218 | Py_ssize_t bufsize; |
Alexey Izbyshev | e359bc2 | 2018-11-04 18:44:16 +0300 | [diff] [blame] | 219 | /* Note: 'pwd' will be used via pointer 'p' on getpwnam_r success. */ |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 220 | struct passwd pwd; |
| 221 | |
Alexey Izbyshev | e359bc2 | 2018-11-04 18:44:16 +0300 | [diff] [blame] | 222 | Py_BEGIN_ALLOW_THREADS |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 223 | bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 224 | if (bufsize == -1) { |
| 225 | bufsize = DEFAULT_BUFFER_SIZE; |
| 226 | } |
| 227 | |
| 228 | while(1) { |
| 229 | buf2 = PyMem_RawRealloc(buf, bufsize); |
| 230 | if (buf2 == NULL) { |
Zackery Spytz | 570e371 | 2018-11-05 12:26:40 -0700 | [diff] [blame] | 231 | p = NULL; |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 232 | nomem = 1; |
| 233 | break; |
| 234 | } |
| 235 | buf = buf2; |
William Grzybowski | 2865848 | 2018-09-07 14:10:39 -0300 | [diff] [blame] | 236 | status = getpwnam_r(name_chars, &pwd, buf, bufsize, &p); |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 237 | if (status != 0) { |
| 238 | p = NULL; |
| 239 | } |
| 240 | if (p != NULL || status != ERANGE) { |
| 241 | break; |
| 242 | } |
| 243 | if (bufsize > (PY_SSIZE_T_MAX >> 1)) { |
| 244 | nomem = 1; |
| 245 | break; |
| 246 | } |
| 247 | bufsize <<= 1; |
| 248 | } |
| 249 | |
| 250 | Py_END_ALLOW_THREADS |
| 251 | #else |
William Grzybowski | 2865848 | 2018-09-07 14:10:39 -0300 | [diff] [blame] | 252 | p = getpwnam(name_chars); |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 253 | #endif |
| 254 | if (p == NULL) { |
| 255 | if (nomem == 1) { |
| 256 | PyErr_NoMemory(); |
| 257 | } |
| 258 | else { |
| 259 | PyErr_Format(PyExc_KeyError, |
William Grzybowski | 34c7f0c | 2018-12-05 17:10:18 -0200 | [diff] [blame] | 260 | "getpwnam(): name not found: %R", name); |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 261 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | goto out; |
| 263 | } |
| 264 | retval = mkpwent(p); |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 265 | out: |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 266 | PyMem_RawFree(buf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | Py_DECREF(bytes); |
| 268 | return retval; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 271 | #ifdef HAVE_GETPWENT |
Brett Cannon | 3d25e16 | 2014-08-22 14:03:51 -0400 | [diff] [blame] | 272 | /*[clinic input] |
| 273 | pwd.getpwall |
| 274 | |
| 275 | Return a list of all available password database entries, in arbitrary order. |
| 276 | |
| 277 | See help(pwd) for more on password database entries. |
| 278 | [clinic start generated code]*/ |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 279 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 280 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 281 | pwd_getpwall_impl(PyObject *module) |
| 282 | /*[clinic end generated code: output=4853d2f5a0afac8a input=d7ecebfd90219b85]*/ |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 283 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | PyObject *d; |
| 285 | struct passwd *p; |
| 286 | if ((d = PyList_New(0)) == NULL) |
| 287 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | setpwent(); |
| 289 | while ((p = getpwent()) != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | PyObject *v = mkpwent(p); |
| 291 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 292 | Py_XDECREF(v); |
| 293 | Py_DECREF(d); |
| 294 | endpwent(); |
| 295 | return NULL; |
| 296 | } |
| 297 | Py_DECREF(v); |
| 298 | } |
| 299 | endpwent(); |
| 300 | return d; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 301 | } |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 302 | #endif |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 303 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 304 | static PyMethodDef pwd_methods[] = { |
Brett Cannon | 3d25e16 | 2014-08-22 14:03:51 -0400 | [diff] [blame] | 305 | PWD_GETPWUID_METHODDEF |
| 306 | PWD_GETPWNAM_METHODDEF |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 307 | #ifdef HAVE_GETPWENT |
Brett Cannon | 3d25e16 | 2014-08-22 14:03:51 -0400 | [diff] [blame] | 308 | PWD_GETPWALL_METHODDEF |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 309 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 311 | }; |
| 312 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 313 | static struct PyModuleDef pwdmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 314 | PyModuleDef_HEAD_INIT, |
| 315 | "pwd", |
| 316 | pwd__doc__, |
| 317 | -1, |
| 318 | pwd_methods, |
| 319 | NULL, |
| 320 | NULL, |
| 321 | NULL, |
| 322 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 323 | }; |
| 324 | |
| 325 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 326 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 327 | PyInit_pwd(void) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 328 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | PyObject *m; |
| 330 | m = PyModule_Create(&pwdmodule); |
| 331 | if (m == NULL) |
| 332 | return NULL; |
Fred Drake | 88c9344 | 2002-04-13 21:07:45 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | if (!initialized) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 335 | if (PyStructSequence_InitType2(&StructPwdType, |
| 336 | &struct_pwd_type_desc) < 0) |
| 337 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | initialized = 1; |
| 339 | } |
| 340 | Py_INCREF((PyObject *) &StructPwdType); |
| 341 | PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType); |
| 342 | return m; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 343 | } |