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 |
| 134 | Py_BEGIN_ALLOW_THREADS |
| 135 | int status; |
| 136 | Py_ssize_t bufsize; |
| 137 | struct passwd pwd; |
| 138 | |
| 139 | bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 140 | if (bufsize == -1) { |
| 141 | bufsize = DEFAULT_BUFFER_SIZE; |
| 142 | } |
| 143 | |
| 144 | while(1) { |
| 145 | buf2 = PyMem_RawRealloc(buf, bufsize); |
| 146 | if (buf2 == NULL) { |
| 147 | nomem = 1; |
| 148 | break; |
| 149 | } |
| 150 | buf = buf2; |
| 151 | status = getpwuid_r(uid, &pwd, buf, bufsize, &p); |
| 152 | if (status != 0) { |
| 153 | p = NULL; |
| 154 | } |
| 155 | if (p != NULL || status != ERANGE) { |
| 156 | break; |
| 157 | } |
| 158 | if (bufsize > (PY_SSIZE_T_MAX >> 1)) { |
| 159 | nomem = 1; |
| 160 | break; |
| 161 | } |
| 162 | bufsize <<= 1; |
| 163 | } |
| 164 | |
| 165 | Py_END_ALLOW_THREADS |
| 166 | #else |
| 167 | p = getpwuid(uid); |
| 168 | #endif |
| 169 | if (p == NULL) { |
| 170 | PyMem_RawFree(buf); |
| 171 | if (nomem == 1) { |
| 172 | return PyErr_NoMemory(); |
| 173 | } |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 174 | PyObject *uid_obj = _PyLong_FromUid(uid); |
| 175 | if (uid_obj == NULL) |
| 176 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | PyErr_Format(PyExc_KeyError, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 178 | "getpwuid(): uid not found: %S", uid_obj); |
| 179 | Py_DECREF(uid_obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | return NULL; |
| 181 | } |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame^] | 182 | retval = mkpwent(p); |
| 183 | #ifdef HAVE_GETPWUID_R |
| 184 | PyMem_RawFree(buf); |
| 185 | #endif |
| 186 | return retval; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Brett Cannon | 3d25e16 | 2014-08-22 14:03:51 -0400 | [diff] [blame] | 189 | /*[clinic input] |
| 190 | pwd.getpwnam |
| 191 | |
| 192 | arg: unicode |
| 193 | / |
| 194 | |
| 195 | Return the password database entry for the given user name. |
| 196 | |
| 197 | See `help(pwd)` for more on password database entries. |
| 198 | [clinic start generated code]*/ |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 199 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 200 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 201 | pwd_getpwnam_impl(PyObject *module, PyObject *arg) |
| 202 | /*[clinic end generated code: output=6abeee92430e43d2 input=d5f7e700919b02d3]*/ |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 203 | { |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame^] | 204 | char *buf = NULL, *buf2 = NULL, *name; |
| 205 | int nomem = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 | struct passwd *p; |
Brett Cannon | 3d25e16 | 2014-08-22 14:03:51 -0400 | [diff] [blame] | 207 | PyObject *bytes, *retval = NULL; |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 208 | |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 209 | if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | return NULL; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 211 | /* check for embedded null bytes */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 212 | if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1) |
| 213 | goto out; |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame^] | 214 | #ifdef HAVE_GETPWNAM_R |
| 215 | Py_BEGIN_ALLOW_THREADS |
| 216 | int status; |
| 217 | Py_ssize_t bufsize; |
| 218 | struct passwd pwd; |
| 219 | |
| 220 | bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 221 | if (bufsize == -1) { |
| 222 | bufsize = DEFAULT_BUFFER_SIZE; |
| 223 | } |
| 224 | |
| 225 | while(1) { |
| 226 | buf2 = PyMem_RawRealloc(buf, bufsize); |
| 227 | if (buf2 == NULL) { |
| 228 | nomem = 1; |
| 229 | break; |
| 230 | } |
| 231 | buf = buf2; |
| 232 | status = getpwnam_r(name, &pwd, buf, bufsize, &p); |
| 233 | if (status != 0) { |
| 234 | p = NULL; |
| 235 | } |
| 236 | if (p != NULL || status != ERANGE) { |
| 237 | break; |
| 238 | } |
| 239 | if (bufsize > (PY_SSIZE_T_MAX >> 1)) { |
| 240 | nomem = 1; |
| 241 | break; |
| 242 | } |
| 243 | bufsize <<= 1; |
| 244 | } |
| 245 | |
| 246 | Py_END_ALLOW_THREADS |
| 247 | #else |
| 248 | p = getpwnam(name); |
| 249 | #endif |
| 250 | if (p == NULL) { |
| 251 | if (nomem == 1) { |
| 252 | PyErr_NoMemory(); |
| 253 | } |
| 254 | else { |
| 255 | PyErr_Format(PyExc_KeyError, |
| 256 | "getpwnam(): name not found: %s", name); |
| 257 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | goto out; |
| 259 | } |
| 260 | retval = mkpwent(p); |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 261 | out: |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame^] | 262 | PyMem_RawFree(buf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | Py_DECREF(bytes); |
| 264 | return retval; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 267 | #ifdef HAVE_GETPWENT |
Brett Cannon | 3d25e16 | 2014-08-22 14:03:51 -0400 | [diff] [blame] | 268 | /*[clinic input] |
| 269 | pwd.getpwall |
| 270 | |
| 271 | Return a list of all available password database entries, in arbitrary order. |
| 272 | |
| 273 | See help(pwd) for more on password database entries. |
| 274 | [clinic start generated code]*/ |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 275 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 276 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 277 | pwd_getpwall_impl(PyObject *module) |
| 278 | /*[clinic end generated code: output=4853d2f5a0afac8a input=d7ecebfd90219b85]*/ |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 279 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | PyObject *d; |
| 281 | struct passwd *p; |
| 282 | if ((d = PyList_New(0)) == NULL) |
| 283 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | setpwent(); |
| 285 | while ((p = getpwent()) != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | PyObject *v = mkpwent(p); |
| 287 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 288 | Py_XDECREF(v); |
| 289 | Py_DECREF(d); |
| 290 | endpwent(); |
| 291 | return NULL; |
| 292 | } |
| 293 | Py_DECREF(v); |
| 294 | } |
| 295 | endpwent(); |
| 296 | return d; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 297 | } |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 298 | #endif |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 299 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 300 | static PyMethodDef pwd_methods[] = { |
Brett Cannon | 3d25e16 | 2014-08-22 14:03:51 -0400 | [diff] [blame] | 301 | PWD_GETPWUID_METHODDEF |
| 302 | PWD_GETPWNAM_METHODDEF |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 303 | #ifdef HAVE_GETPWENT |
Brett Cannon | 3d25e16 | 2014-08-22 14:03:51 -0400 | [diff] [blame] | 304 | PWD_GETPWALL_METHODDEF |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 305 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 307 | }; |
| 308 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 309 | static struct PyModuleDef pwdmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | PyModuleDef_HEAD_INIT, |
| 311 | "pwd", |
| 312 | pwd__doc__, |
| 313 | -1, |
| 314 | pwd_methods, |
| 315 | NULL, |
| 316 | NULL, |
| 317 | NULL, |
| 318 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 322 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 323 | PyInit_pwd(void) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 324 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | PyObject *m; |
| 326 | m = PyModule_Create(&pwdmodule); |
| 327 | if (m == NULL) |
| 328 | return NULL; |
Fred Drake | 88c9344 | 2002-04-13 21:07:45 +0000 | [diff] [blame] | 329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 330 | if (!initialized) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 331 | if (PyStructSequence_InitType2(&StructPwdType, |
| 332 | &struct_pwd_type_desc) < 0) |
| 333 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | initialized = 1; |
| 335 | } |
| 336 | Py_INCREF((PyObject *) &StructPwdType); |
| 337 | PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType); |
| 338 | return m; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 339 | } |