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