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 | { |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 114 | PyObject *py_int_id, *retval = NULL; |
| 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)) { |
| 121 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | return NULL; |
Serhiy Storchaka | 9cc4ed5 | 2016-01-18 18:49:57 +0200 | [diff] [blame] | 123 | } |
| 124 | PyErr_Clear(); |
| 125 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 126 | "group id must be int, not %.200", |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 127 | Py_TYPE(id)->tp_name) < 0) { |
Serhiy Storchaka | 9cc4ed5 | 2016-01-18 18:49:57 +0200 | [diff] [blame] | 128 | return NULL; |
| 129 | } |
| 130 | py_int_id = PyNumber_Long(id); |
| 131 | if (!py_int_id) |
| 132 | return NULL; |
| 133 | if (!_Py_Gid_Converter(py_int_id, &gid)) { |
| 134 | Py_DECREF(py_int_id); |
| 135 | return NULL; |
| 136 | } |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 137 | Py_DECREF(py_int_id); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 138 | } |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 139 | #ifdef HAVE_GETGRGID_R |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 140 | int status; |
| 141 | Py_ssize_t bufsize; |
Alexey Izbyshev | e359bc2 | 2018-11-04 18:44:16 +0300 | [diff] [blame] | 142 | /* Note: 'grp' will be used via pointer 'p' on getgrgid_r success. */ |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 143 | struct group grp; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 144 | |
Alexey Izbyshev | e359bc2 | 2018-11-04 18:44:16 +0300 | [diff] [blame] | 145 | Py_BEGIN_ALLOW_THREADS |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 146 | bufsize = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 147 | if (bufsize == -1) { |
| 148 | bufsize = DEFAULT_BUFFER_SIZE; |
| 149 | } |
| 150 | |
| 151 | while (1) { |
| 152 | buf2 = PyMem_RawRealloc(buf, bufsize); |
| 153 | if (buf2 == NULL) { |
| 154 | p = NULL; |
| 155 | nomem = 1; |
| 156 | break; |
| 157 | } |
| 158 | buf = buf2; |
| 159 | status = getgrgid_r(gid, &grp, buf, bufsize, &p); |
| 160 | if (status != 0) { |
| 161 | p = NULL; |
| 162 | } |
| 163 | if (p != NULL || status != ERANGE) { |
| 164 | break; |
| 165 | } |
| 166 | if (bufsize > (PY_SSIZE_T_MAX >> 1)) { |
| 167 | nomem = 1; |
| 168 | break; |
| 169 | } |
| 170 | bufsize <<= 1; |
| 171 | } |
| 172 | |
| 173 | Py_END_ALLOW_THREADS |
| 174 | #else |
| 175 | p = getgrgid(gid); |
| 176 | #endif |
| 177 | if (p == NULL) { |
| 178 | PyMem_RawFree(buf); |
| 179 | if (nomem == 1) { |
| 180 | return PyErr_NoMemory(); |
| 181 | } |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 182 | PyObject *gid_obj = _PyLong_FromGid(gid); |
| 183 | if (gid_obj == NULL) |
| 184 | return NULL; |
| 185 | PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %S", gid_obj); |
| 186 | Py_DECREF(gid_obj); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 187 | return NULL; |
| 188 | } |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 189 | retval = mkgrent(p); |
| 190 | #ifdef HAVE_GETGRGID_R |
| 191 | PyMem_RawFree(buf); |
| 192 | #endif |
| 193 | return retval; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 196 | /*[clinic input] |
| 197 | grp.getgrnam |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 198 | |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 199 | name: unicode |
| 200 | |
| 201 | Return the group database entry for the given group name. |
| 202 | |
| 203 | If name is not valid, raise KeyError. |
| 204 | [clinic start generated code]*/ |
| 205 | |
| 206 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 207 | grp_getgrnam_impl(PyObject *module, PyObject *name) |
| 208 | /*[clinic end generated code: output=67905086f403c21c input=08ded29affa3c863]*/ |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 209 | { |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 210 | char *buf = NULL, *buf2 = NULL, *name_chars; |
| 211 | int nomem = 0; |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 212 | struct group *p; |
| 213 | PyObject *bytes, *retval = NULL; |
| 214 | |
| 215 | if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL) |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 216 | return NULL; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 217 | /* check for embedded null bytes */ |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 218 | if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1) |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 219 | goto out; |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 220 | #ifdef HAVE_GETGRNAM_R |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 221 | int status; |
| 222 | Py_ssize_t bufsize; |
Alexey Izbyshev | e359bc2 | 2018-11-04 18:44:16 +0300 | [diff] [blame] | 223 | /* Note: 'grp' will be used via pointer 'p' on getgrnam_r success. */ |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 224 | struct group grp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | |
Alexey Izbyshev | e359bc2 | 2018-11-04 18:44:16 +0300 | [diff] [blame] | 226 | Py_BEGIN_ALLOW_THREADS |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 227 | bufsize = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 228 | if (bufsize == -1) { |
| 229 | bufsize = DEFAULT_BUFFER_SIZE; |
| 230 | } |
| 231 | |
| 232 | while(1) { |
| 233 | buf2 = PyMem_RawRealloc(buf, bufsize); |
| 234 | if (buf2 == NULL) { |
| 235 | p = NULL; |
| 236 | nomem = 1; |
| 237 | break; |
| 238 | } |
| 239 | buf = buf2; |
| 240 | status = getgrnam_r(name_chars, &grp, buf, bufsize, &p); |
| 241 | if (status != 0) { |
| 242 | p = NULL; |
| 243 | } |
| 244 | if (p != NULL || status != ERANGE) { |
| 245 | break; |
| 246 | } |
| 247 | if (bufsize > (PY_SSIZE_T_MAX >> 1)) { |
| 248 | nomem = 1; |
| 249 | break; |
| 250 | } |
| 251 | bufsize <<= 1; |
| 252 | } |
| 253 | |
| 254 | Py_END_ALLOW_THREADS |
| 255 | #else |
| 256 | p = getgrnam(name_chars); |
| 257 | #endif |
| 258 | if (p == NULL) { |
| 259 | if (nomem == 1) { |
| 260 | PyErr_NoMemory(); |
| 261 | } |
| 262 | else { |
William Grzybowski | 34c7f0c | 2018-12-05 17:10:18 -0200 | [diff] [blame] | 263 | PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %R", name); |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 264 | } |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 265 | goto out; |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 266 | } |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 267 | retval = mkgrent(p); |
| 268 | out: |
William Grzybowski | 23e65b2 | 2018-09-07 09:06:15 -0300 | [diff] [blame] | 269 | PyMem_RawFree(buf); |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 270 | Py_DECREF(bytes); |
| 271 | return retval; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 274 | /*[clinic input] |
| 275 | grp.getgrall |
| 276 | |
| 277 | Return a list of all available group entries, in arbitrary order. |
| 278 | |
| 279 | An entry whose name starts with '+' or '-' represents an instruction |
| 280 | to use YP/NIS and may not be accessible via getgrnam or getgrgid. |
| 281 | [clinic start generated code]*/ |
| 282 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 283 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 284 | grp_getgrall_impl(PyObject *module) |
| 285 | /*[clinic end generated code: output=585dad35e2e763d7 input=d7df76c825c367df]*/ |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 286 | { |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 287 | PyObject *d; |
| 288 | struct group *p; |
| 289 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 290 | if ((d = PyList_New(0)) == NULL) |
| 291 | return NULL; |
| 292 | setgrent(); |
| 293 | while ((p = getgrent()) != NULL) { |
| 294 | PyObject *v = mkgrent(p); |
| 295 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 296 | Py_XDECREF(v); |
| 297 | Py_DECREF(d); |
Martin v. Löwis | e23c868 | 2009-05-29 16:01:34 +0000 | [diff] [blame] | 298 | endgrent(); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 299 | return NULL; |
| 300 | } |
| 301 | Py_DECREF(v); |
| 302 | } |
Fred Drake | 8e68eb6 | 2001-03-11 03:03:07 +0000 | [diff] [blame] | 303 | endgrent(); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 304 | return d; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 307 | static PyMethodDef grp_methods[] = { |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 308 | GRP_GETGRGID_METHODDEF |
| 309 | GRP_GETGRNAM_METHODDEF |
| 310 | GRP_GETGRALL_METHODDEF |
| 311 | {NULL, NULL} |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 312 | }; |
| 313 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 314 | PyDoc_STRVAR(grp__doc__, |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 315 | "Access to the Unix group database.\n\ |
| 316 | \n\ |
| 317 | Group entries are reported as 4-tuples containing the following fields\n\ |
| 318 | from the group database, in order:\n\ |
| 319 | \n\ |
Georg Brandl | 41ea1f4 | 2014-10-02 08:34:41 +0200 | [diff] [blame] | 320 | gr_name - name of the group\n\ |
| 321 | gr_passwd - group password (encrypted); often empty\n\ |
| 322 | gr_gid - numeric ID of the group\n\ |
| 323 | gr_mem - list of members\n\ |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 324 | \n\ |
| 325 | The gid is an integer, name and password are strings. (Note that most\n\ |
| 326 | users are not explicitly listed as members of the groups they are in\n\ |
| 327 | 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] | 328 | complete membership information.)"); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 329 | |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 330 | static int grpmodule_traverse(PyObject *m, visitproc visit, void *arg) { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 331 | Py_VISIT(get_grp_state(m)->StructGrpType); |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 332 | return 0; |
| 333 | } |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 334 | |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 335 | static int grpmodule_clear(PyObject *m) { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 336 | Py_CLEAR(get_grp_state(m)->StructGrpType); |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static void grpmodule_free(void *m) { |
| 341 | grpmodule_clear((PyObject *)m); |
| 342 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 343 | |
| 344 | static struct PyModuleDef grpmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | PyModuleDef_HEAD_INIT, |
| 346 | "grp", |
| 347 | grp__doc__, |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 348 | sizeof(grpmodulestate), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | grp_methods, |
| 350 | NULL, |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 351 | grpmodule_traverse, |
| 352 | grpmodule_clear, |
| 353 | grpmodule_free, |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 354 | }; |
| 355 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 356 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 357 | PyInit_grp(void) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 358 | { |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 359 | PyObject *m; |
| 360 | if ((m = PyState_FindModule(&grpmodule)) != NULL) { |
| 361 | Py_INCREF(m); |
| 362 | return m; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 363 | } |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 364 | |
| 365 | if ((m = PyModule_Create(&grpmodule)) == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 366 | return NULL; |
Dino Viehland | 40a5313 | 2019-09-10 11:30:36 +0100 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | grpmodulestate *state = PyModule_GetState(m); |
| 370 | state->StructGrpType = PyStructSequence_NewType(&struct_group_type_desc); |
| 371 | if (state->StructGrpType == NULL) { |
| 372 | return NULL; |
| 373 | } |
| 374 | |
| 375 | Py_INCREF(state->StructGrpType); |
| 376 | PyModule_AddObject(m, "struct_group", (PyObject *) state->StructGrpType); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 377 | return m; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 378 | } |