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