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