| 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; | 
|  | 114 | if ((bytes = PyUnicode_AsEncodedString(arg, Py_FileSystemDefaultEncoding, | 
|  | 115 | "surrogateescape")) == NULL) | 
|  | 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\ | 
|  | 164 | Return a list of all available group entries, in arbitrary order."}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 165 | {NULL,              NULL}           /* sentinel */ | 
| Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 166 | }; | 
|  | 167 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 168 | PyDoc_STRVAR(grp__doc__, | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 169 | "Access to the Unix group database.\n\ | 
|  | 170 | \n\ | 
|  | 171 | Group entries are reported as 4-tuples containing the following fields\n\ | 
|  | 172 | from the group database, in order:\n\ | 
|  | 173 | \n\ | 
|  | 174 | name   - name of the group\n\ | 
|  | 175 | passwd - group password (encrypted); often empty\n\ | 
|  | 176 | gid    - numeric ID of the group\n\ | 
|  | 177 | mem    - list of members\n\ | 
|  | 178 | \n\ | 
|  | 179 | The gid is an integer, name and password are strings.  (Note that most\n\ | 
|  | 180 | users are not explicitly listed as members of the groups they are in\n\ | 
|  | 181 | 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] | 182 | complete membership information.)"); | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 183 |  | 
|  | 184 |  | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 185 |  | 
|  | 186 | static struct PyModuleDef grpmodule = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 187 | PyModuleDef_HEAD_INIT, | 
|  | 188 | "grp", | 
|  | 189 | grp__doc__, | 
|  | 190 | -1, | 
|  | 191 | grp_methods, | 
|  | 192 | NULL, | 
|  | 193 | NULL, | 
|  | 194 | NULL, | 
|  | 195 | NULL | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 196 | }; | 
|  | 197 |  | 
| Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 198 | PyMODINIT_FUNC | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 199 | PyInit_grp(void) | 
| Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 200 | { | 
| Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 201 | PyObject *m, *d; | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 202 | m = PyModule_Create(&grpmodule); | 
| Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 203 | if (m == NULL) | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 204 | return NULL; | 
| Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 205 | d = PyModule_GetDict(m); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 206 | if (!initialized) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 207 | PyStructSequence_InitType(&StructGrpType, &struct_group_type_desc); | 
| Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 208 | PyDict_SetItemString(d, "struct_group", (PyObject *) &StructGrpType); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 209 | initialized = 1; | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 210 | return m; | 
| Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 211 | } |