| 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" | 
| Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 5 |  | 
 | 6 | #include <sys/types.h> | 
 | 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"}, | 
 | 13 |    {"gr_mem", "group memebers"}, | 
| 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 | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 72 |     SET(setIndex++, PyLong_FromLong((long) 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; | 
| Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 88 |     unsigned int 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; | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 94 |     gid = PyLong_AS_LONG(py_int_id); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 95 |     Py_DECREF(py_int_id); | 
 | 96 |  | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 97 |     if ((p = getgrgid(gid)) == NULL) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 |         PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid); | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 99 |         return NULL; | 
 | 100 |     } | 
 | 101 |     return mkgrent(p); | 
| Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 102 | } | 
 | 103 |  | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 104 | static PyObject * | 
| Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 105 | grp_getgrnam(PyObject *self, PyObject *args) | 
| Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 106 | { | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 107 |     char *name; | 
 | 108 |     struct group *p; | 
| Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 109 |     PyObject *arg, *bytes, *retval = NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 110 |  | 
| Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 111 |     if (!PyArg_ParseTuple(args, "U:getgrnam", &arg)) | 
 | 112 |         return NULL; | 
| Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 113 |     if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL) | 
| Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 114 |         return NULL; | 
 | 115 |     if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1) | 
 | 116 |         goto out; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 |  | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 118 |     if ((p = getgrnam(name)) == NULL) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 |         PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name); | 
| Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 120 |         goto out; | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 121 |     } | 
| Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 122 |     retval = mkgrent(p); | 
 | 123 | out: | 
 | 124 |     Py_DECREF(bytes); | 
 | 125 |     return retval; | 
| Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 126 | } | 
 | 127 |  | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 128 | static PyObject * | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 129 | grp_getgrall(PyObject *self, PyObject *ignore) | 
| Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 130 | { | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 131 |     PyObject *d; | 
 | 132 |     struct group *p; | 
 | 133 |  | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 134 |     if ((d = PyList_New(0)) == NULL) | 
 | 135 |         return NULL; | 
 | 136 |     setgrent(); | 
 | 137 |     while ((p = getgrent()) != NULL) { | 
 | 138 |         PyObject *v = mkgrent(p); | 
 | 139 |         if (v == NULL || PyList_Append(d, v) != 0) { | 
 | 140 |             Py_XDECREF(v); | 
 | 141 |             Py_DECREF(d); | 
| Martin v. Löwis | e23c868 | 2009-05-29 16:01:34 +0000 | [diff] [blame] | 142 |             endgrent(); | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 143 |             return NULL; | 
 | 144 |         } | 
 | 145 |         Py_DECREF(v); | 
 | 146 |     } | 
| Fred Drake | 8e68eb6 | 2001-03-11 03:03:07 +0000 | [diff] [blame] | 147 |     endgrent(); | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 148 |     return d; | 
| Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 149 | } | 
 | 150 |  | 
| Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 151 | static PyMethodDef grp_methods[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 |     {"getgrgid",        grp_getgrgid,   METH_O, | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 153 |      "getgrgid(id) -> tuple\n\ | 
 | 154 | Return the group database entry for the given numeric group ID.  If\n\ | 
 | 155 | id is not valid, raise KeyError."}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 |     {"getgrnam",        grp_getgrnam,   METH_VARARGS, | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 157 |      "getgrnam(name) -> tuple\n\ | 
 | 158 | Return the group database entry for the given group name.  If\n\ | 
 | 159 | name is not valid, raise KeyError."}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 |     {"getgrall",        grp_getgrall,   METH_NOARGS, | 
| Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 161 |      "getgrall() -> list of tuples\n\ | 
| R. David Murray | ec07331 | 2010-12-14 16:20:53 +0000 | [diff] [blame] | 162 | Return a list of all available group entries, in arbitrary order.\n\ | 
 | 163 | An entry whose name starts with '+' or '-' represents an instruction\n\ | 
 | 164 | 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] | 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 | } |