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 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 9 | |
| 10 | static PyObject * |
| 11 | mkgrent(struct group *p) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 12 | { |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 13 | PyObject *v, *w; |
| 14 | char **member; |
| 15 | if ((w = PyList_New(0)) == NULL) { |
| 16 | return NULL; |
| 17 | } |
| 18 | for (member = p->gr_mem; *member != NULL; member++) { |
| 19 | PyObject *x = PyString_FromString(*member); |
| 20 | if (x == NULL || PyList_Append(w, x) != 0) { |
| 21 | Py_XDECREF(x); |
| 22 | Py_DECREF(w); |
| 23 | return NULL; |
| 24 | } |
| 25 | Py_DECREF(x); |
| 26 | } |
| 27 | v = Py_BuildValue("(sslO)", |
| 28 | p->gr_name, |
| 29 | p->gr_passwd, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 30 | #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__) |
| 31 | /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3; |
| 32 | for later versions you may have to remove this */ |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 33 | (long)p->gr_short_pad, /* ugh-NeXT broke the padding */ |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 34 | #else |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 35 | (long)p->gr_gid, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 36 | #endif |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 37 | w); |
| 38 | Py_DECREF(w); |
| 39 | return v; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 42 | static PyObject * |
| 43 | grp_getgrgid(PyObject *self, PyObject *args) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 44 | { |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 45 | int gid; |
| 46 | struct group *p; |
| 47 | if (!PyArg_ParseTuple(args, "i:getgrgid", &gid)) |
| 48 | return NULL; |
| 49 | if ((p = getgrgid(gid)) == NULL) { |
| 50 | PyErr_SetString(PyExc_KeyError, "getgrgid(): gid not found"); |
| 51 | return NULL; |
| 52 | } |
| 53 | return mkgrent(p); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 56 | static PyObject * |
| 57 | grp_getgrnam(PyObject *self, PyObject *args) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 58 | { |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 59 | char *name; |
| 60 | struct group *p; |
| 61 | if (!PyArg_ParseTuple(args, "s:getgrnam", &name)) |
| 62 | return NULL; |
| 63 | if ((p = getgrnam(name)) == NULL) { |
| 64 | PyErr_SetString(PyExc_KeyError, "getgrnam(): name not found"); |
| 65 | return NULL; |
| 66 | } |
| 67 | return mkgrent(p); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 70 | static PyObject * |
| 71 | grp_getgrall(PyObject *self, PyObject *args) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 72 | { |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 73 | PyObject *d; |
| 74 | struct group *p; |
| 75 | |
| 76 | if (!PyArg_ParseTuple(args, ":getgrall")) |
| 77 | return NULL; |
| 78 | if ((d = PyList_New(0)) == NULL) |
| 79 | return NULL; |
| 80 | setgrent(); |
| 81 | while ((p = getgrent()) != NULL) { |
| 82 | PyObject *v = mkgrent(p); |
| 83 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 84 | Py_XDECREF(v); |
| 85 | Py_DECREF(d); |
| 86 | return NULL; |
| 87 | } |
| 88 | Py_DECREF(v); |
| 89 | } |
Fred Drake | 8e68eb6 | 2001-03-11 03:03:07 +0000 | [diff] [blame] | 90 | endgrent(); |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 91 | return d; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 94 | static PyMethodDef grp_methods[] = { |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 95 | {"getgrgid", grp_getgrgid, METH_VARARGS, |
| 96 | "getgrgid(id) -> tuple\n\ |
| 97 | Return the group database entry for the given numeric group ID. If\n\ |
| 98 | id is not valid, raise KeyError."}, |
| 99 | {"getgrnam", grp_getgrnam, METH_VARARGS, |
| 100 | "getgrnam(name) -> tuple\n\ |
| 101 | Return the group database entry for the given group name. If\n\ |
| 102 | name is not valid, raise KeyError."}, |
| 103 | {"getgrall", grp_getgrall, METH_VARARGS, |
| 104 | "getgrall() -> list of tuples\n\ |
| 105 | Return a list of all available group entries, in arbitrary order."}, |
| 106 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 109 | static char grp__doc__[] = |
| 110 | "Access to the Unix group database.\n\ |
| 111 | \n\ |
| 112 | Group entries are reported as 4-tuples containing the following fields\n\ |
| 113 | from the group database, in order:\n\ |
| 114 | \n\ |
| 115 | name - name of the group\n\ |
| 116 | passwd - group password (encrypted); often empty\n\ |
| 117 | gid - numeric ID of the group\n\ |
| 118 | mem - list of members\n\ |
| 119 | \n\ |
| 120 | The gid is an integer, name and password are strings. (Note that most\n\ |
| 121 | users are not explicitly listed as members of the groups they are in\n\ |
| 122 | according to the password database. Check both databases to get\n\ |
| 123 | complete membership information.)"; |
| 124 | |
| 125 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 126 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 127 | initgrp(void) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 128 | { |
Fred Drake | 51b6bc5 | 2000-07-08 16:56:26 +0000 | [diff] [blame] | 129 | Py_InitModule3("grp", grp_methods, grp__doc__); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 130 | } |