grpmodule.c 6.21 KB
Newer Older
1 2 3

/* UNIX group file access module */

Roger E. Masse's avatar
Roger E. Masse committed
4
#include "Python.h"
5
#include "posixmodule.h"
6 7 8

#include <grp.h>

9 10 11 12
#include "clinic/grpmodule.c.h"
/*[clinic input]
module grp
[clinic start generated code]*/
13
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=cade63f2ed1bd9f8]*/
14

15 16 17
static PyStructSequence_Field struct_group_type_fields[] = {
   {"gr_name", "group name"},
   {"gr_passwd", "password"},
18
   {"gr_gid", "group id"},
19
   {"gr_mem", "group members"},
20 21 22
   {0}
};

23
PyDoc_STRVAR(struct_group__doc__,
24 25 26
"grp.struct_group: Results from getgr*() routines.\n\n\
This object may be accessed either as a tuple of\n\
  (gr_name,gr_passwd,gr_gid,gr_mem)\n\
27
or via the object attributes as named in the above tuple.\n");
28 29 30 31 32 33 34 35 36

static PyStructSequence_Desc struct_group_type_desc = {
   "grp.struct_group",
   struct_group__doc__,
   struct_group_type_fields,
   4,
};


37
static int initialized;
38
static PyTypeObject StructGrpType;
39 40 41

static PyObject *
mkgrent(struct group *p)
42
{
43 44
    int setIndex = 0;
    PyObject *v = PyStructSequence_New(&StructGrpType), *w;
45
    char **member;
46 47 48 49

    if (v == NULL)
        return NULL;

50
    if ((w = PyList_New(0)) == NULL) {
51
        Py_DECREF(v);
52 53 54
        return NULL;
    }
    for (member = p->gr_mem; *member != NULL; member++) {
55
        PyObject *x = PyUnicode_DecodeFSDefault(*member);
56 57 58
        if (x == NULL || PyList_Append(w, x) != 0) {
            Py_XDECREF(x);
            Py_DECREF(w);
59
            Py_DECREF(v);
60 61 62 63
            return NULL;
        }
        Py_DECREF(x);
    }
64 65

#define SET(i,val) PyStructSequence_SET_ITEM(v, i, val)
66
    SET(setIndex++, PyUnicode_DecodeFSDefault(p->gr_name));
67
    if (p->gr_passwd)
68
            SET(setIndex++, PyUnicode_DecodeFSDefault(p->gr_passwd));
69
    else {
70 71
            SET(setIndex++, Py_None);
            Py_INCREF(Py_None);
72
    }
73
    SET(setIndex++, _PyLong_FromGid(p->gr_gid));
74 75 76 77 78 79 80 81
    SET(setIndex++, w);
#undef SET

    if (PyErr_Occurred()) {
        Py_DECREF(v);
        return NULL;
    }

82
    return v;
83 84
}

85 86 87 88 89 90 91 92 93 94
/*[clinic input]
grp.getgrgid

    id: object

Return the group database entry for the given numeric group ID.

If id is not valid, raise KeyError.
[clinic start generated code]*/

95
static PyObject *
96 97
grp_getgrgid_impl(PyObject *module, PyObject *id)
/*[clinic end generated code: output=30797c289504a1ba input=15fa0e2ccf5cda25]*/
98
{
99
    PyObject *py_int_id;
100
    gid_t gid;
101
    struct group *p;
102

103 104
    if (!_Py_Gid_Converter(id, &gid)) {
        if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
105
            return NULL;
106 107 108 109 110 111 112 113 114 115 116 117 118 119
        }
        PyErr_Clear();
        if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
                             "group id must be int, not %.200",
                             id->ob_type->tp_name) < 0) {
            return NULL;
        }
        py_int_id = PyNumber_Long(id);
        if (!py_int_id)
            return NULL;
        if (!_Py_Gid_Converter(py_int_id, &gid)) {
            Py_DECREF(py_int_id);
            return NULL;
        }
120 121
        Py_DECREF(py_int_id);
    }
122

123
    if ((p = getgrgid(gid)) == NULL) {
124 125 126 127 128
        PyObject *gid_obj = _PyLong_FromGid(gid);
        if (gid_obj == NULL)
            return NULL;
        PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %S", gid_obj);
        Py_DECREF(gid_obj);
129 130 131
        return NULL;
    }
    return mkgrent(p);
132 133
}

134 135 136 137 138 139 140 141 142 143
/*[clinic input]
grp.getgrnam

    name: unicode

Return the group database entry for the given group name.

If name is not valid, raise KeyError.
[clinic start generated code]*/

144
static PyObject *
145 146
grp_getgrnam_impl(PyObject *module, PyObject *name)
/*[clinic end generated code: output=67905086f403c21c input=08ded29affa3c863]*/
147
{
148
    char *name_chars;
149
    struct group *p;
150
    PyObject *bytes, *retval = NULL;
151

152
    if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL)
153
        return NULL;
154
    if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
155
        goto out;
156

157 158
    if ((p = getgrnam(name_chars)) == NULL) {
        PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name_chars);
159
        goto out;
160
    }
161 162 163 164
    retval = mkgrent(p);
out:
    Py_DECREF(bytes);
    return retval;
165 166
}

167 168 169 170 171 172 173 174 175
/*[clinic input]
grp.getgrall

Return a list of all available group entries, in arbitrary order.

An entry whose name starts with '+' or '-' represents an instruction
to use YP/NIS and may not be accessible via getgrnam or getgrgid.
[clinic start generated code]*/

176
static PyObject *
177 178
grp_getgrall_impl(PyObject *module)
/*[clinic end generated code: output=585dad35e2e763d7 input=d7df76c825c367df]*/
179
{
180 181 182 183 184 185 186 187 188 189 190
    PyObject *d;
    struct group *p;

    if ((d = PyList_New(0)) == NULL)
        return NULL;
    setgrent();
    while ((p = getgrent()) != NULL) {
        PyObject *v = mkgrent(p);
        if (v == NULL || PyList_Append(d, v) != 0) {
            Py_XDECREF(v);
            Py_DECREF(d);
191
            endgrent();
192 193 194 195
            return NULL;
        }
        Py_DECREF(v);
    }
196
    endgrent();
197
    return d;
198 199
}

Roger E. Masse's avatar
Roger E. Masse committed
200
static PyMethodDef grp_methods[] = {
201 202 203 204
    GRP_GETGRGID_METHODDEF
    GRP_GETGRNAM_METHODDEF
    GRP_GETGRALL_METHODDEF
    {NULL, NULL}
205 206
};

207
PyDoc_STRVAR(grp__doc__,
208 209 210 211 212
"Access to the Unix group database.\n\
\n\
Group entries are reported as 4-tuples containing the following fields\n\
from the group database, in order:\n\
\n\
213 214 215 216
  gr_name   - name of the group\n\
  gr_passwd - group password (encrypted); often empty\n\
  gr_gid    - numeric ID of the group\n\
  gr_mem    - list of members\n\
217 218 219 220
\n\
The gid is an integer, name and password are strings.  (Note that most\n\
users are not explicitly listed as members of the groups they are in\n\
according to the password database.  Check both databases to get\n\
221
complete membership information.)");
222 223


224 225

static struct PyModuleDef grpmodule = {
226 227 228 229 230 231 232 233 234
        PyModuleDef_HEAD_INIT,
        "grp",
        grp__doc__,
        -1,
        grp_methods,
        NULL,
        NULL,
        NULL,
        NULL
235 236
};

237
PyMODINIT_FUNC
238
PyInit_grp(void)
239
{
240
    PyObject *m, *d;
241
    m = PyModule_Create(&grpmodule);
242
    if (m == NULL)
243
        return NULL;
244
    d = PyModule_GetDict(m);
245 246 247 248 249 250 251 252
    if (!initialized) {
        if (PyStructSequence_InitType2(&StructGrpType,
                                       &struct_group_type_desc) < 0)
            return NULL;
    }
    if (PyDict_SetItemString(d, "struct_group",
                             (PyObject *)&StructGrpType) < 0)
        return NULL;
253
    initialized = 1;
254
    return m;
255
}