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