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 | |
| 18 | static char struct_group__doc__[] = |
| 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\ |
| 22 | or via the object attributes as named in the above tuple.\n"; |
| 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 | |
| 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++) { |
| 49 | PyObject *x = PyString_FromString(*member); |
| 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) |
| 60 | SET(setIndex++, PyString_FromString(p->gr_name)); |
| 61 | SET(setIndex++, PyString_FromString(p->gr_passwd)); |
| 62 | SET(setIndex++, PyInt_FromLong((long) p->gr_gid)); |
| 63 | SET(setIndex++, w); |
| 64 | #undef SET |
| 65 | |
| 66 | if (PyErr_Occurred()) { |
| 67 | Py_DECREF(v); |
| 68 | Py_DECREF(w); |
| 69 | return NULL; |
| 70 | } |
| 71 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 72 | return v; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 75 | static PyObject * |
| 76 | grp_getgrgid(PyObject *self, PyObject *args) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 77 | { |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 78 | int gid; |
| 79 | struct group *p; |
| 80 | if (!PyArg_ParseTuple(args, "i:getgrgid", &gid)) |
| 81 | return NULL; |
| 82 | if ((p = getgrgid(gid)) == NULL) { |
| 83 | PyErr_SetString(PyExc_KeyError, "getgrgid(): gid not found"); |
| 84 | return NULL; |
| 85 | } |
| 86 | return mkgrent(p); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 89 | static PyObject * |
| 90 | grp_getgrnam(PyObject *self, PyObject *args) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 91 | { |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 92 | char *name; |
| 93 | struct group *p; |
| 94 | if (!PyArg_ParseTuple(args, "s:getgrnam", &name)) |
| 95 | return NULL; |
| 96 | if ((p = getgrnam(name)) == NULL) { |
| 97 | PyErr_SetString(PyExc_KeyError, "getgrnam(): name not found"); |
| 98 | return NULL; |
| 99 | } |
| 100 | return mkgrent(p); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 103 | static PyObject * |
| 104 | grp_getgrall(PyObject *self, PyObject *args) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 105 | { |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 106 | PyObject *d; |
| 107 | struct group *p; |
| 108 | |
| 109 | if (!PyArg_ParseTuple(args, ":getgrall")) |
| 110 | return NULL; |
| 111 | if ((d = PyList_New(0)) == NULL) |
| 112 | return NULL; |
| 113 | setgrent(); |
| 114 | while ((p = getgrent()) != NULL) { |
| 115 | PyObject *v = mkgrent(p); |
| 116 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 117 | Py_XDECREF(v); |
| 118 | Py_DECREF(d); |
| 119 | return NULL; |
| 120 | } |
| 121 | Py_DECREF(v); |
| 122 | } |
Fred Drake | 8e68eb6 | 2001-03-11 03:03:07 +0000 | [diff] [blame] | 123 | endgrent(); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 124 | return d; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 127 | static PyMethodDef grp_methods[] = { |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 128 | {"getgrgid", grp_getgrgid, METH_VARARGS, |
| 129 | "getgrgid(id) -> tuple\n\ |
| 130 | Return the group database entry for the given numeric group ID. If\n\ |
| 131 | id is not valid, raise KeyError."}, |
| 132 | {"getgrnam", grp_getgrnam, METH_VARARGS, |
| 133 | "getgrnam(name) -> tuple\n\ |
| 134 | Return the group database entry for the given group name. If\n\ |
| 135 | name is not valid, raise KeyError."}, |
| 136 | {"getgrall", grp_getgrall, METH_VARARGS, |
| 137 | "getgrall() -> list of tuples\n\ |
| 138 | Return a list of all available group entries, in arbitrary order."}, |
| 139 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 142 | static char grp__doc__[] = |
| 143 | "Access to the Unix group database.\n\ |
| 144 | \n\ |
| 145 | Group entries are reported as 4-tuples containing the following fields\n\ |
| 146 | from the group database, in order:\n\ |
| 147 | \n\ |
| 148 | name - name of the group\n\ |
| 149 | passwd - group password (encrypted); often empty\n\ |
| 150 | gid - numeric ID of the group\n\ |
| 151 | mem - list of members\n\ |
| 152 | \n\ |
| 153 | The gid is an integer, name and password are strings. (Note that most\n\ |
| 154 | users are not explicitly listed as members of the groups they are in\n\ |
| 155 | according to the password database. Check both databases to get\n\ |
| 156 | complete membership information.)"; |
| 157 | |
| 158 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 159 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 160 | initgrp(void) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 161 | { |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 162 | PyObject *m, *d; |
| 163 | m = Py_InitModule3("grp", grp_methods, grp__doc__); |
| 164 | d = PyModule_GetDict(m); |
| 165 | PyStructSequence_InitType(&StructGrpType, &struct_group_type_desc); |
| 166 | PyDict_SetItemString(d, "struct_group", (PyObject *) &StructGrpType); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 167 | } |