Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 1 | |
| 2 | /* UNIX group file access module */ |
| 3 | |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +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 | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 7 | #include <grp.h> |
| 8 | |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 9 | #include "clinic/grpmodule.c.h" |
| 10 | /*[clinic input] |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 11 | module grp |
| 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=cade63f2ed1bd9f8]*/ |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 14 | |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 15 | static PyStructSequence_Field struct_group_type_fields[] = { |
| 16 | {"gr_name", "group name"}, |
| 17 | {"gr_passwd", "password"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | {"gr_gid", "group id"}, |
Mark Dickinson | fb29a16 | 2013-08-05 17:57:01 +0100 | [diff] [blame] | 19 | {"gr_mem", "group members"}, |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 20 | {0} |
| 21 | }; |
| 22 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 23 | PyDoc_STRVAR(struct_group__doc__, |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 24 | "grp.struct_group: Results from getgr*() routines.\n\n\ |
| 25 | This object may be accessed either as a tuple of\n\ |
| 26 | (gr_name,gr_passwd,gr_gid,gr_mem)\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 27 | or via the object attributes as named in the above tuple.\n"); |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 28 | |
| 29 | static PyStructSequence_Desc struct_group_type_desc = { |
| 30 | "grp.struct_group", |
| 31 | struct_group__doc__, |
| 32 | struct_group_type_fields, |
| 33 | 4, |
| 34 | }; |
| 35 | |
| 36 | |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 37 | typedef struct { |
| 38 | PyTypeObject *StructGrpType; |
| 39 | } grpmodulestate; |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 40 | |
| 41 | static inline grpmodulestate* |
| 42 | get_grp_state(PyObject *module) |
| 43 | { |
| 44 | void *state = PyModule_GetState(module); |
| 45 | assert(state != NULL); |
| 46 | return (grpmodulestate *)state; |
| 47 | } |
| 48 | |
| 49 | #define modulestate_global get_grp_state(PyState_FindModule(&grpmodule)) |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 50 | |
| 51 | static struct PyModuleDef grpmodule; |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 52 | |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 53 | #define DEFAULT_BUFFER_SIZE 1024 |
| 54 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 55 | static PyObject * |
| 56 | mkgrent(struct group *p) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 57 | { |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 58 | int setIndex = 0; |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 59 | PyObject *v, *w; |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 60 | char **member; |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 61 | |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 62 | if ((v = PyStructSequence_New(modulestate_global->StructGrpType)) == NULL) |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 63 | return NULL; |
| 64 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 65 | if ((w = PyList_New(0)) == NULL) { |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 66 | Py_DECREF(v); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 67 | return NULL; |
| 68 | } |
| 69 | for (member = p->gr_mem; *member != NULL; member++) { |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 70 | PyObject *x = PyUnicode_DecodeFSDefault(*member); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 71 | if (x == NULL || PyList_Append(w, x) != 0) { |
| 72 | Py_XDECREF(x); |
| 73 | Py_DECREF(w); |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 74 | Py_DECREF(v); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 75 | return NULL; |
| 76 | } |
| 77 | Py_DECREF(x); |
| 78 | } |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 79 | |
| 80 | #define SET(i,val) PyStructSequence_SET_ITEM(v, i, val) |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 81 | SET(setIndex++, PyUnicode_DecodeFSDefault(p->gr_name)); |
Martin v. Löwis | ceb7c18 | 2002-09-17 07:05:25 +0000 | [diff] [blame] | 82 | if (p->gr_passwd) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | SET(setIndex++, PyUnicode_DecodeFSDefault(p->gr_passwd)); |
Martin v. Löwis | ceb7c18 | 2002-09-17 07:05:25 +0000 | [diff] [blame] | 84 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | SET(setIndex++, Py_None); |
| 86 | Py_INCREF(Py_None); |
Martin v. Löwis | ceb7c18 | 2002-09-17 07:05:25 +0000 | [diff] [blame] | 87 | } |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 88 | SET(setIndex++, _PyLong_FromGid(p->gr_gid)); |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 89 | SET(setIndex++, w); |
| 90 | #undef SET |
| 91 | |
| 92 | if (PyErr_Occurred()) { |
| 93 | Py_DECREF(v); |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 94 | return NULL; |
| 95 | } |
| 96 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 97 | return v; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 100 | /*[clinic input] |
| 101 | grp.getgrgid |
| 102 | |
| 103 | id: object |
| 104 | |
| 105 | Return the group database entry for the given numeric group ID. |
| 106 | |
| 107 | If id is not valid, raise KeyError. |
| 108 | [clinic start generated code]*/ |
| 109 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 110 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 111 | grp_getgrgid_impl(PyObject *module, PyObject *id) |
| 112 | /*[clinic end generated code: output=30797c289504a1ba input=15fa0e2ccf5cda25]*/ |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 113 | { |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 114 | PyObject *retval = NULL; |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 115 | int nomem = 0; |
| 116 | char *buf = NULL, *buf2 = NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 117 | gid_t gid; |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 118 | struct group *p; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 119 | |
Serhiy Storchaka | 9cc4ed5 | 2016-01-18 18:49:57 +0200 | [diff] [blame] | 120 | if (!_Py_Gid_Converter(id, &gid)) { |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 121 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 122 | } |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 123 | #ifdef HAVE_GETGRGID_R |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 124 | int status; |
| 125 | Py_ssize_t bufsize; |
Alexey Izbyshev | e359bc2 | 2018-11-04 18:44:16 +0300 | [diff] [blame] | 126 | /* Note: 'grp' will be used via pointer 'p' on getgrgid_r success. */ |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 127 | struct group grp; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 128 | |
Alexey Izbyshev | e359bc2 | 2018-11-04 18:44:16 +0300 | [diff] [blame] | 129 | Py_BEGIN_ALLOW_THREADS |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 130 | bufsize = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 131 | if (bufsize == -1) { |
| 132 | bufsize = DEFAULT_BUFFER_SIZE; |
| 133 | } |
| 134 | |
| 135 | while (1) { |
| 136 | buf2 = PyMem_RawRealloc(buf, bufsize); |
| 137 | if (buf2 == NULL) { |
| 138 | p = NULL; |
| 139 | nomem = 1; |
| 140 | break; |
| 141 | } |
| 142 | buf = buf2; |
| 143 | status = getgrgid_r(gid, &grp, buf, bufsize, &p); |
| 144 | if (status != 0) { |
| 145 | p = NULL; |
| 146 | } |
| 147 | if (p != NULL || status != ERANGE) { |
| 148 | break; |
| 149 | } |
| 150 | if (bufsize > (PY_SSIZE_T_MAX >> 1)) { |
| 151 | nomem = 1; |
| 152 | break; |
| 153 | } |
| 154 | bufsize <<= 1; |
| 155 | } |
| 156 | |
| 157 | Py_END_ALLOW_THREADS |
| 158 | #else |
| 159 | p = getgrgid(gid); |
| 160 | #endif |
| 161 | if (p == NULL) { |
| 162 | PyMem_RawFree(buf); |
| 163 | if (nomem == 1) { |
| 164 | return PyErr_NoMemory(); |
| 165 | } |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 166 | PyObject *gid_obj = _PyLong_FromGid(gid); |
| 167 | if (gid_obj == NULL) |
| 168 | return NULL; |
| 169 | PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %S", gid_obj); |
| 170 | Py_DECREF(gid_obj); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 171 | return NULL; |
| 172 | } |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 173 | retval = mkgrent(p); |
| 174 | #ifdef HAVE_GETGRGID_R |
| 175 | PyMem_RawFree(buf); |
| 176 | #endif |
| 177 | return retval; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 180 | /*[clinic input] |
| 181 | grp.getgrnam |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 182 | |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 183 | name: unicode |
| 184 | |
| 185 | Return the group database entry for the given group name. |
| 186 | |
| 187 | If name is not valid, raise KeyError. |
| 188 | [clinic start generated code]*/ |
| 189 | |
| 190 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 191 | grp_getgrnam_impl(PyObject *module, PyObject *name) |
| 192 | /*[clinic end generated code: output=67905086f403c21c input=08ded29affa3c863]*/ |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 193 | { |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 194 | char *buf = NULL, *buf2 = NULL, *name_chars; |
| 195 | int nomem = 0; |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 196 | struct group *p; |
| 197 | PyObject *bytes, *retval = NULL; |
| 198 | |
| 199 | if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL) |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 200 | return NULL; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 201 | /* check for embedded null bytes */ |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 202 | if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1) |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 203 | goto out; |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 204 | #ifdef HAVE_GETGRNAM_R |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 205 | int status; |
| 206 | Py_ssize_t bufsize; |
Alexey Izbyshev | e359bc2 | 2018-11-04 18:44:16 +0300 | [diff] [blame] | 207 | /* Note: 'grp' will be used via pointer 'p' on getgrnam_r success. */ |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 208 | struct group grp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | |
Alexey Izbyshev | e359bc2 | 2018-11-04 18:44:16 +0300 | [diff] [blame] | 210 | Py_BEGIN_ALLOW_THREADS |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 211 | bufsize = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 212 | if (bufsize == -1) { |
| 213 | bufsize = DEFAULT_BUFFER_SIZE; |
| 214 | } |
| 215 | |
| 216 | while(1) { |
| 217 | buf2 = PyMem_RawRealloc(buf, bufsize); |
| 218 | if (buf2 == NULL) { |
| 219 | p = NULL; |
| 220 | nomem = 1; |
| 221 | break; |
| 222 | } |
| 223 | buf = buf2; |
| 224 | status = getgrnam_r(name_chars, &grp, buf, bufsize, &p); |
| 225 | if (status != 0) { |
| 226 | p = NULL; |
| 227 | } |
| 228 | if (p != NULL || status != ERANGE) { |
| 229 | break; |
| 230 | } |
| 231 | if (bufsize > (PY_SSIZE_T_MAX >> 1)) { |
| 232 | nomem = 1; |
| 233 | break; |
| 234 | } |
| 235 | bufsize <<= 1; |
| 236 | } |
| 237 | |
| 238 | Py_END_ALLOW_THREADS |
| 239 | #else |
| 240 | p = getgrnam(name_chars); |
| 241 | #endif |
| 242 | if (p == NULL) { |
| 243 | if (nomem == 1) { |
| 244 | PyErr_NoMemory(); |
| 245 | } |
| 246 | else { |
William Grzybowski | 34c7f0c | 2018-12-05 17:10:18 -0200 | [diff] [blame] | 247 | PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %R", name); |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 248 | } |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 249 | goto out; |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 250 | } |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 251 | retval = mkgrent(p); |
| 252 | out: |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 253 | PyMem_RawFree(buf); |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 254 | Py_DECREF(bytes); |
| 255 | return retval; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 258 | /*[clinic input] |
| 259 | grp.getgrall |
| 260 | |
| 261 | Return a list of all available group entries, in arbitrary order. |
| 262 | |
| 263 | An entry whose name starts with '+' or '-' represents an instruction |
| 264 | to use YP/NIS and may not be accessible via getgrnam or getgrgid. |
| 265 | [clinic start generated code]*/ |
| 266 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 267 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 268 | grp_getgrall_impl(PyObject *module) |
| 269 | /*[clinic end generated code: output=585dad35e2e763d7 input=d7df76c825c367df]*/ |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 270 | { |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 271 | PyObject *d; |
| 272 | struct group *p; |
| 273 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 274 | if ((d = PyList_New(0)) == NULL) |
| 275 | return NULL; |
| 276 | setgrent(); |
| 277 | while ((p = getgrent()) != NULL) { |
| 278 | PyObject *v = mkgrent(p); |
| 279 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 280 | Py_XDECREF(v); |
| 281 | Py_DECREF(d); |
Martin v. Löwis | e23c868 | 2009-05-29 16:01:34 +0000 | [diff] [blame] | 282 | endgrent(); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 283 | return NULL; |
| 284 | } |
| 285 | Py_DECREF(v); |
| 286 | } |
Fred Drake | 8e68eb6 | 2001-03-11 03:03:07 +0000 | [diff] [blame] | 287 | endgrent(); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 288 | return d; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 291 | static PyMethodDef grp_methods[] = { |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 292 | GRP_GETGRGID_METHODDEF |
| 293 | GRP_GETGRNAM_METHODDEF |
| 294 | GRP_GETGRALL_METHODDEF |
| 295 | {NULL, NULL} |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 296 | }; |
| 297 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 298 | PyDoc_STRVAR(grp__doc__, |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 299 | "Access to the Unix group database.\n\ |
| 300 | \n\ |
| 301 | Group entries are reported as 4-tuples containing the following fields\n\ |
| 302 | from the group database, in order:\n\ |
| 303 | \n\ |
Georg Brandl | 41ea1f4 | 2014-10-02 08:34:41 +0200 | [diff] [blame] | 304 | gr_name - name of the group\n\ |
| 305 | gr_passwd - group password (encrypted); often empty\n\ |
| 306 | gr_gid - numeric ID of the group\n\ |
| 307 | gr_mem - list of members\n\ |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 308 | \n\ |
| 309 | The gid is an integer, name and password are strings. (Note that most\n\ |
| 310 | users are not explicitly listed as members of the groups they are in\n\ |
| 311 | according to the password database. Check both databases to get\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 312 | complete membership information.)"); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 313 | |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 314 | static int grpmodule_traverse(PyObject *m, visitproc visit, void *arg) { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 315 | Py_VISIT(get_grp_state(m)->StructGrpType); |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 316 | return 0; |
| 317 | } |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 318 | |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 319 | static int grpmodule_clear(PyObject *m) { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 320 | Py_CLEAR(get_grp_state(m)->StructGrpType); |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static void grpmodule_free(void *m) { |
| 325 | grpmodule_clear((PyObject *)m); |
| 326 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 327 | |
| 328 | static struct PyModuleDef grpmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | PyModuleDef_HEAD_INIT, |
| 330 | "grp", |
| 331 | grp__doc__, |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 332 | sizeof(grpmodulestate), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | grp_methods, |
| 334 | NULL, |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 335 | grpmodule_traverse, |
| 336 | grpmodule_clear, |
| 337 | grpmodule_free, |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 338 | }; |
| 339 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 340 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 341 | PyInit_grp(void) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 342 | { |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 343 | PyObject *m; |
| 344 | if ((m = PyState_FindModule(&grpmodule)) != NULL) { |
| 345 | Py_INCREF(m); |
| 346 | return m; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 347 | } |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 348 | |
| 349 | if ((m = PyModule_Create(&grpmodule)) == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 350 | return NULL; |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | grpmodulestate *state = PyModule_GetState(m); |
| 354 | state->StructGrpType = PyStructSequence_NewType(&struct_group_type_desc); |
| 355 | if (state->StructGrpType == NULL) { |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | Py_INCREF(state->StructGrpType); |
| 360 | PyModule_AddObject(m, "struct_group", (PyObject *) state->StructGrpType); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 361 | return m; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 362 | } |