blob: 5dbcf9862bc8f49f2b52fceb420693f6d37826be [file] [log] [blame]
Guido van Rossum20882d51994-06-23 11:15:44 +00001
2/* UNIX group file access module */
3
Roger E. Masseb2b44e51996-12-18 19:37:32 +00004#include "Python.h"
Guido van Rossum20882d51994-06-23 11:15:44 +00005
6#include <sys/types.h>
7#include <grp.h>
8
Fred Drake51b6bc52000-07-08 16:56:26 +00009
10static PyObject *
11mkgrent(struct group *p)
Guido van Rossum20882d51994-06-23 11:15:44 +000012{
Fred Drake51b6bc52000-07-08 16:56:26 +000013 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 Rossum03e8ffa1995-02-07 15:38:56 +000030#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 Drake51b6bc52000-07-08 16:56:26 +000033 (long)p->gr_short_pad, /* ugh-NeXT broke the padding */
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000034#else
Fred Drake51b6bc52000-07-08 16:56:26 +000035 (long)p->gr_gid,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000036#endif
Fred Drake51b6bc52000-07-08 16:56:26 +000037 w);
38 Py_DECREF(w);
39 return v;
Guido van Rossum20882d51994-06-23 11:15:44 +000040}
41
Fred Drake51b6bc52000-07-08 16:56:26 +000042static PyObject *
43grp_getgrgid(PyObject *self, PyObject *args)
Guido van Rossum20882d51994-06-23 11:15:44 +000044{
Fred Drake51b6bc52000-07-08 16:56:26 +000045 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 Rossum20882d51994-06-23 11:15:44 +000054}
55
Fred Drake51b6bc52000-07-08 16:56:26 +000056static PyObject *
57grp_getgrnam(PyObject *self, PyObject *args)
Guido van Rossum20882d51994-06-23 11:15:44 +000058{
Fred Drake51b6bc52000-07-08 16:56:26 +000059 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 Rossum20882d51994-06-23 11:15:44 +000068}
69
Fred Drake51b6bc52000-07-08 16:56:26 +000070static PyObject *
71grp_getgrall(PyObject *self, PyObject *args)
Guido van Rossum20882d51994-06-23 11:15:44 +000072{
Fred Drake51b6bc52000-07-08 16:56:26 +000073 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 Drake8e68eb62001-03-11 03:03:07 +000090 endgrent();
Fred Drake51b6bc52000-07-08 16:56:26 +000091 return d;
Guido van Rossum20882d51994-06-23 11:15:44 +000092}
93
Roger E. Masseb2b44e51996-12-18 19:37:32 +000094static PyMethodDef grp_methods[] = {
Fred Drake51b6bc52000-07-08 16:56:26 +000095 {"getgrgid", grp_getgrgid, METH_VARARGS,
96 "getgrgid(id) -> tuple\n\
97Return the group database entry for the given numeric group ID. If\n\
98id is not valid, raise KeyError."},
99 {"getgrnam", grp_getgrnam, METH_VARARGS,
100 "getgrnam(name) -> tuple\n\
101Return the group database entry for the given group name. If\n\
102name is not valid, raise KeyError."},
103 {"getgrall", grp_getgrall, METH_VARARGS,
104 "getgrall() -> list of tuples\n\
105Return a list of all available group entries, in arbitrary order."},
106 {NULL, NULL} /* sentinel */
Guido van Rossum20882d51994-06-23 11:15:44 +0000107};
108
Fred Drake51b6bc52000-07-08 16:56:26 +0000109static char grp__doc__[] =
110"Access to the Unix group database.\n\
111\n\
112Group entries are reported as 4-tuples containing the following fields\n\
113from 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\
120The gid is an integer, name and password are strings. (Note that most\n\
121users are not explicitly listed as members of the groups they are in\n\
122according to the password database. Check both databases to get\n\
123complete membership information.)";
124
125
Guido van Rossum3886bb61998-12-04 18:50:17 +0000126DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000127initgrp(void)
Guido van Rossum20882d51994-06-23 11:15:44 +0000128{
Fred Drake51b6bc52000-07-08 16:56:26 +0000129 Py_InitModule3("grp", grp_methods, grp__doc__);
Guido van Rossum20882d51994-06-23 11:15:44 +0000130}