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"}, |
| 13 | {"gr_gid", "group id"}, |
| 14 | {"gr_mem", "group memebers"}, |
| 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 | |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +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++) { |
| 50 | PyObject *x = PyString_FromString(*member); |
| 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) |
| 61 | SET(setIndex++, PyString_FromString(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) |
| 67 | SET(setIndex++, PyString_FromString(p->gr_passwd)); |
| 68 | else { |
| 69 | SET(setIndex++, Py_None); |
| 70 | Py_INCREF(Py_None); |
| 71 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 72 | #endif |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 73 | SET(setIndex++, PyInt_FromLong((long) p->gr_gid)); |
| 74 | SET(setIndex++, w); |
| 75 | #undef SET |
| 76 | |
| 77 | if (PyErr_Occurred()) { |
| 78 | Py_DECREF(v); |
| 79 | Py_DECREF(w); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 83 | return v; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 86 | static PyObject * |
Brett Cannon | 4c803f1 | 2006-05-25 22:00:14 +0000 | [diff] [blame] | 87 | grp_getgrgid(PyObject *self, PyObject *pyo_id) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 88 | { |
Brett Cannon | 4c803f1 | 2006-05-25 22:00:14 +0000 | [diff] [blame] | 89 | PyObject *py_int_id; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 90 | unsigned int gid; |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 91 | struct group *p; |
Brett Cannon | 4c803f1 | 2006-05-25 22:00:14 +0000 | [diff] [blame] | 92 | |
| 93 | py_int_id = PyNumber_Int(pyo_id); |
| 94 | if (!py_int_id) |
| 95 | return NULL; |
| 96 | gid = PyInt_AS_LONG(py_int_id); |
| 97 | Py_DECREF(py_int_id); |
| 98 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 99 | if ((p = getgrgid(gid)) == NULL) { |
Barry Warsaw | c7a7709 | 2004-01-20 21:06:00 +0000 | [diff] [blame] | 100 | PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid); |
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 * |
Brett Cannon | 4c803f1 | 2006-05-25 22:00:14 +0000 | [diff] [blame] | 107 | grp_getgrnam(PyObject *self, PyObject *pyo_name) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 108 | { |
Brett Cannon | 4c803f1 | 2006-05-25 22:00:14 +0000 | [diff] [blame] | 109 | PyObject *py_str_name; |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 110 | char *name; |
| 111 | struct group *p; |
Brett Cannon | 4c803f1 | 2006-05-25 22:00:14 +0000 | [diff] [blame] | 112 | |
| 113 | py_str_name = PyObject_Str(pyo_name); |
| 114 | if (!py_str_name) |
| 115 | return NULL; |
| 116 | name = PyString_AS_STRING(py_str_name); |
| 117 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 118 | if ((p = getgrnam(name)) == NULL) { |
Barry Warsaw | c7a7709 | 2004-01-20 21:06:00 +0000 | [diff] [blame] | 119 | PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name); |
Brett Cannon | 4c803f1 | 2006-05-25 22:00:14 +0000 | [diff] [blame] | 120 | Py_DECREF(py_str_name); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 121 | return NULL; |
| 122 | } |
Brett Cannon | 4c803f1 | 2006-05-25 22:00:14 +0000 | [diff] [blame] | 123 | |
| 124 | Py_DECREF(py_str_name); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 125 | return mkgrent(p); |
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 * |
Brett Cannon | 4c803f1 | 2006-05-25 22:00:14 +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); |
| 142 | return NULL; |
| 143 | } |
| 144 | Py_DECREF(v); |
| 145 | } |
Fred Drake | 8e68eb6 | 2001-03-11 03:03:07 +0000 | [diff] [blame] | 146 | endgrent(); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 147 | return d; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 150 | static PyMethodDef grp_methods[] = { |
Brett Cannon | 4c803f1 | 2006-05-25 22:00:14 +0000 | [diff] [blame] | 151 | {"getgrgid", grp_getgrgid, METH_O, |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 152 | "getgrgid(id) -> tuple\n\ |
| 153 | Return the group database entry for the given numeric group ID. If\n\ |
| 154 | id is not valid, raise KeyError."}, |
Brett Cannon | 4c803f1 | 2006-05-25 22:00:14 +0000 | [diff] [blame] | 155 | {"getgrnam", grp_getgrnam, METH_O, |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 156 | "getgrnam(name) -> tuple\n\ |
| 157 | Return the group database entry for the given group name. If\n\ |
| 158 | name is not valid, raise KeyError."}, |
Brett Cannon | 4c803f1 | 2006-05-25 22:00:14 +0000 | [diff] [blame] | 159 | {"getgrall", grp_getgrall, METH_NOARGS, |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 160 | "getgrall() -> list of tuples\n\ |
| 161 | Return a list of all available group entries, in arbitrary order."}, |
| 162 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 165 | PyDoc_STRVAR(grp__doc__, |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 166 | "Access to the Unix group database.\n\ |
| 167 | \n\ |
| 168 | Group entries are reported as 4-tuples containing the following fields\n\ |
| 169 | from the group database, in order:\n\ |
| 170 | \n\ |
| 171 | name - name of the group\n\ |
| 172 | passwd - group password (encrypted); often empty\n\ |
| 173 | gid - numeric ID of the group\n\ |
| 174 | mem - list of members\n\ |
| 175 | \n\ |
| 176 | The gid is an integer, name and password are strings. (Note that most\n\ |
| 177 | users are not explicitly listed as members of the groups they are in\n\ |
| 178 | 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] | 179 | complete membership information.)"); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 180 | |
| 181 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 182 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 183 | initgrp(void) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 184 | { |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 185 | PyObject *m, *d; |
| 186 | m = Py_InitModule3("grp", grp_methods, grp__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 187 | if (m == NULL) |
| 188 | return; |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 189 | d = PyModule_GetDict(m); |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 190 | if (!initialized) |
| 191 | PyStructSequence_InitType(&StructGrpType, &struct_group_type_desc); |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 192 | PyDict_SetItemString(d, "struct_group", (PyObject *) &StructGrpType); |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 193 | initialized = 1; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 194 | } |