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] |
| 11 | output preset file |
| 12 | module grp |
| 13 | [clinic start generated code]*/ |
| 14 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=68180a9a9efb8506]*/ |
| 15 | |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 16 | static PyStructSequence_Field struct_group_type_fields[] = { |
| 17 | {"gr_name", "group name"}, |
| 18 | {"gr_passwd", "password"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 19 | {"gr_gid", "group id"}, |
Mark Dickinson | fb29a16 | 2013-08-05 17:57:01 +0100 | [diff] [blame] | 20 | {"gr_mem", "group members"}, |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 21 | {0} |
| 22 | }; |
| 23 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 24 | PyDoc_STRVAR(struct_group__doc__, |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 25 | "grp.struct_group: Results from getgr*() routines.\n\n\ |
| 26 | This object may be accessed either as a tuple of\n\ |
| 27 | (gr_name,gr_passwd,gr_gid,gr_mem)\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 28 | 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] | 29 | |
| 30 | static PyStructSequence_Desc struct_group_type_desc = { |
| 31 | "grp.struct_group", |
| 32 | struct_group__doc__, |
| 33 | struct_group_type_fields, |
| 34 | 4, |
| 35 | }; |
| 36 | |
| 37 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 38 | static int initialized; |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 39 | static PyTypeObject StructGrpType; |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 40 | |
| 41 | static PyObject * |
| 42 | mkgrent(struct group *p) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 43 | { |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 44 | int setIndex = 0; |
| 45 | PyObject *v = PyStructSequence_New(&StructGrpType), *w; |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 46 | char **member; |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 47 | |
| 48 | if (v == NULL) |
| 49 | return NULL; |
| 50 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 51 | if ((w = PyList_New(0)) == NULL) { |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 52 | Py_DECREF(v); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 53 | return NULL; |
| 54 | } |
| 55 | for (member = p->gr_mem; *member != NULL; member++) { |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 56 | PyObject *x = PyUnicode_DecodeFSDefault(*member); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 57 | if (x == NULL || PyList_Append(w, x) != 0) { |
| 58 | Py_XDECREF(x); |
| 59 | Py_DECREF(w); |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 60 | Py_DECREF(v); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 61 | return NULL; |
| 62 | } |
| 63 | Py_DECREF(x); |
| 64 | } |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 65 | |
| 66 | #define SET(i,val) PyStructSequence_SET_ITEM(v, i, val) |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 67 | SET(setIndex++, PyUnicode_DecodeFSDefault(p->gr_name)); |
Martin v. Löwis | ceb7c18 | 2002-09-17 07:05:25 +0000 | [diff] [blame] | 68 | if (p->gr_passwd) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | SET(setIndex++, PyUnicode_DecodeFSDefault(p->gr_passwd)); |
Martin v. Löwis | ceb7c18 | 2002-09-17 07:05:25 +0000 | [diff] [blame] | 70 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | SET(setIndex++, Py_None); |
| 72 | Py_INCREF(Py_None); |
Martin v. Löwis | ceb7c18 | 2002-09-17 07:05:25 +0000 | [diff] [blame] | 73 | } |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 74 | SET(setIndex++, _PyLong_FromGid(p->gr_gid)); |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 75 | SET(setIndex++, w); |
| 76 | #undef SET |
| 77 | |
| 78 | if (PyErr_Occurred()) { |
| 79 | Py_DECREF(v); |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 80 | return NULL; |
| 81 | } |
| 82 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 83 | return v; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 86 | /*[clinic input] |
| 87 | grp.getgrgid |
| 88 | |
| 89 | id: object |
| 90 | |
| 91 | Return the group database entry for the given numeric group ID. |
| 92 | |
| 93 | If id is not valid, raise KeyError. |
| 94 | [clinic start generated code]*/ |
| 95 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 96 | static PyObject * |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 97 | grp_getgrgid_impl(PyModuleDef *module, PyObject *id) |
| 98 | /*[clinic end generated code: output=8a11f5fdeb8c78a0 input=15fa0e2ccf5cda25]*/ |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 99 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 100 | PyObject *py_int_id; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 101 | gid_t gid; |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 102 | struct group *p; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 103 | |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 104 | py_int_id = PyNumber_Long(id); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 105 | if (!py_int_id) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 107 | if (!_Py_Gid_Converter(py_int_id, &gid)) { |
| 108 | Py_DECREF(py_int_id); |
| 109 | return NULL; |
| 110 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 111 | Py_DECREF(py_int_id); |
| 112 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 113 | if ((p = getgrgid(gid)) == NULL) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 114 | PyObject *gid_obj = _PyLong_FromGid(gid); |
| 115 | if (gid_obj == NULL) |
| 116 | return NULL; |
| 117 | PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %S", gid_obj); |
| 118 | Py_DECREF(gid_obj); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 119 | return NULL; |
| 120 | } |
| 121 | return mkgrent(p); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 124 | /*[clinic input] |
| 125 | grp.getgrnam |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 126 | |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 127 | name: unicode |
| 128 | |
| 129 | Return the group database entry for the given group name. |
| 130 | |
| 131 | If name is not valid, raise KeyError. |
| 132 | [clinic start generated code]*/ |
| 133 | |
| 134 | static PyObject * |
| 135 | grp_getgrnam_impl(PyModuleDef *module, PyObject *name) |
| 136 | /*[clinic end generated code: output=cd47511f4854da8e input=08ded29affa3c863]*/ |
| 137 | { |
| 138 | char *name_chars; |
| 139 | struct group *p; |
| 140 | PyObject *bytes, *retval = NULL; |
| 141 | |
| 142 | if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL) |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 143 | return NULL; |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 144 | if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1) |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 145 | goto out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 147 | if ((p = getgrnam(name_chars)) == NULL) { |
| 148 | PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name_chars); |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 149 | goto out; |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 150 | } |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 151 | retval = mkgrent(p); |
| 152 | out: |
| 153 | Py_DECREF(bytes); |
| 154 | return retval; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 157 | /*[clinic input] |
| 158 | grp.getgrall |
| 159 | |
| 160 | Return a list of all available group entries, in arbitrary order. |
| 161 | |
| 162 | An entry whose name starts with '+' or '-' represents an instruction |
| 163 | to use YP/NIS and may not be accessible via getgrnam or getgrgid. |
| 164 | [clinic start generated code]*/ |
| 165 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 166 | static PyObject * |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 167 | grp_getgrall_impl(PyModuleDef *module) |
| 168 | /*[clinic end generated code: output=add9037a20c202de input=d7df76c825c367df]*/ |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 169 | { |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 170 | PyObject *d; |
| 171 | struct group *p; |
| 172 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 173 | if ((d = PyList_New(0)) == NULL) |
| 174 | return NULL; |
| 175 | setgrent(); |
| 176 | while ((p = getgrent()) != NULL) { |
| 177 | PyObject *v = mkgrent(p); |
| 178 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 179 | Py_XDECREF(v); |
| 180 | Py_DECREF(d); |
Martin v. Löwis | e23c868 | 2009-05-29 16:01:34 +0000 | [diff] [blame] | 181 | endgrent(); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 182 | return NULL; |
| 183 | } |
| 184 | Py_DECREF(v); |
| 185 | } |
Fred Drake | 8e68eb6 | 2001-03-11 03:03:07 +0000 | [diff] [blame] | 186 | endgrent(); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 187 | return d; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 190 | static PyMethodDef grp_methods[] = { |
Brett Cannon | 8fb7bb2 | 2014-08-22 11:52:46 -0400 | [diff] [blame] | 191 | GRP_GETGRGID_METHODDEF |
| 192 | GRP_GETGRNAM_METHODDEF |
| 193 | GRP_GETGRALL_METHODDEF |
| 194 | {NULL, NULL} |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 197 | PyDoc_STRVAR(grp__doc__, |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 198 | "Access to the Unix group database.\n\ |
| 199 | \n\ |
| 200 | Group entries are reported as 4-tuples containing the following fields\n\ |
| 201 | from the group database, in order:\n\ |
| 202 | \n\ |
Georg Brandl | 41ea1f4 | 2014-10-02 08:34:41 +0200 | [diff] [blame] | 203 | gr_name - name of the group\n\ |
| 204 | gr_passwd - group password (encrypted); often empty\n\ |
| 205 | gr_gid - numeric ID of the group\n\ |
| 206 | gr_mem - list of members\n\ |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 207 | \n\ |
| 208 | The gid is an integer, name and password are strings. (Note that most\n\ |
| 209 | users are not explicitly listed as members of the groups they are in\n\ |
| 210 | 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] | 211 | complete membership information.)"); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 212 | |
| 213 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 214 | |
| 215 | static struct PyModuleDef grpmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | PyModuleDef_HEAD_INIT, |
| 217 | "grp", |
| 218 | grp__doc__, |
| 219 | -1, |
| 220 | grp_methods, |
| 221 | NULL, |
| 222 | NULL, |
| 223 | NULL, |
| 224 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 227 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 228 | PyInit_grp(void) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 229 | { |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 230 | PyObject *m, *d; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 231 | m = PyModule_Create(&grpmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 232 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 233 | return NULL; |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 234 | d = PyModule_GetDict(m); |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 235 | if (!initialized) { |
| 236 | if (PyStructSequence_InitType2(&StructGrpType, |
| 237 | &struct_group_type_desc) < 0) |
| 238 | return NULL; |
| 239 | } |
| 240 | if (PyDict_SetItemString(d, "struct_group", |
| 241 | (PyObject *)&StructGrpType) < 0) |
| 242 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 243 | initialized = 1; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 244 | return m; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 245 | } |