Python-2.7.3/Modules/grpmodule.c

No issues found

  1 /* UNIX group file access module */
  2 
  3 #include "Python.h"
  4 #include "structseq.h"
  5 
  6 #include <sys/types.h>
  7 #include <grp.h>
  8 
  9 static PyStructSequence_Field struct_group_type_fields[] = {
 10    {"gr_name", "group name"},
 11    {"gr_passwd", "password"},
 12    {"gr_gid", "group id"}, 
 13    {"gr_mem", "group memebers"}, 
 14    {0}
 15 };
 16 
 17 PyDoc_STRVAR(struct_group__doc__,
 18 "grp.struct_group: Results from getgr*() routines.\n\n\
 19 This object may be accessed either as a tuple of\n\
 20   (gr_name,gr_passwd,gr_gid,gr_mem)\n\
 21 or via the object attributes as named in the above tuple.\n");
 22 
 23 static PyStructSequence_Desc struct_group_type_desc = {
 24    "grp.struct_group",
 25    struct_group__doc__,
 26    struct_group_type_fields,
 27    4,
 28 };
 29 
 30 
 31 static int initialized;
 32 static PyTypeObject StructGrpType;
 33 
 34 static PyObject *
 35 mkgrent(struct group *p)
 36 {
 37     int setIndex = 0;
 38     PyObject *v = PyStructSequence_New(&StructGrpType), *w;
 39     char **member;
 40 
 41     if (v == NULL)
 42         return NULL;
 43 
 44     if ((w = PyList_New(0)) == NULL) {
 45         Py_DECREF(v);
 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);
 53             Py_DECREF(v);
 54             return NULL;
 55         }
 56         Py_DECREF(x);
 57     }
 58 
 59 #define SET(i,val) PyStructSequence_SET_ITEM(v, i, val)
 60     SET(setIndex++, PyString_FromString(p->gr_name));
 61 #ifdef __VMS
 62     SET(setIndex++, Py_None);
 63     Py_INCREF(Py_None);
 64 #else
 65     if (p->gr_passwd)
 66 	    SET(setIndex++, PyString_FromString(p->gr_passwd));
 67     else {
 68 	    SET(setIndex++, Py_None);
 69 	    Py_INCREF(Py_None);
 70     }
 71 #endif
 72     SET(setIndex++, _PyObject_FromGid(p->gr_gid));
 73     SET(setIndex++, w);
 74 #undef SET
 75 
 76     if (PyErr_Occurred()) {
 77         Py_DECREF(v);
 78         return NULL;
 79     }
 80 
 81     return v;
 82 }
 83 
 84 static PyObject *
 85 grp_getgrgid(PyObject *self, PyObject *pyo_id)
 86 {
 87     gid_t gid;
 88     struct group *p;
 89 
 90     if (!_PyArg_ParseGid(pyo_id, &gid)) {
 91         return NULL;
 92     }
 93 
 94     if ((p = getgrgid(gid)) == NULL) {
 95         PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %lu", (unsigned long)gid);
 96         return NULL;
 97     }
 98     return mkgrent(p);
 99 }
100 
101 static PyObject *
102 grp_getgrnam(PyObject *self, PyObject *pyo_name)
103 {
104     PyObject *py_str_name;
105     char *name;
106     struct group *p;
107 
108     py_str_name = PyObject_Str(pyo_name);
109     if (!py_str_name)
110 	    return NULL;
111     name = PyString_AS_STRING(py_str_name);
112     
113     if ((p = getgrnam(name)) == NULL) {
114 	PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name);
115 	Py_DECREF(py_str_name);
116         return NULL;
117     }
118 
119     Py_DECREF(py_str_name);
120     return mkgrent(p);
121 }
122 
123 static PyObject *
124 grp_getgrall(PyObject *self, PyObject *ignore)
125 {
126     PyObject *d;
127     struct group *p;
128 
129     if ((d = PyList_New(0)) == NULL)
130         return NULL;
131     setgrent();
132     while ((p = getgrent()) != NULL) {
133         PyObject *v = mkgrent(p);
134         if (v == NULL || PyList_Append(d, v) != 0) {
135             Py_XDECREF(v);
136             Py_DECREF(d);
137             endgrent();
138             return NULL;
139         }
140         Py_DECREF(v);
141     }
142     endgrent();
143     return d;
144 }
145 
146 static PyMethodDef grp_methods[] = {
147     {"getgrgid",	grp_getgrgid,	METH_O,
148      "getgrgid(id) -> tuple\n\
149 Return the group database entry for the given numeric group ID.  If\n\
150 id is not valid, raise KeyError."},
151     {"getgrnam",	grp_getgrnam,	METH_O,
152      "getgrnam(name) -> tuple\n\
153 Return the group database entry for the given group name.  If\n\
154 name is not valid, raise KeyError."},
155     {"getgrall",	grp_getgrall,	METH_NOARGS,
156      "getgrall() -> list of tuples\n\
157 Return a list of all available group entries, in arbitrary order.\n\
158 An entry whose name starts with '+' or '-' represents an instruction\n\
159 to use YP/NIS and may not be accessible via getgrnam or getgrgid."},
160     {NULL,		NULL}		/* sentinel */
161 };
162 
163 PyDoc_STRVAR(grp__doc__,
164 "Access to the Unix group database.\n\
165 \n\
166 Group entries are reported as 4-tuples containing the following fields\n\
167 from the group database, in order:\n\
168 \n\
169   name   - name of the group\n\
170   passwd - group password (encrypted); often empty\n\
171   gid    - numeric ID of the group\n\
172   mem    - list of members\n\
173 \n\
174 The gid is an integer, name and password are strings.  (Note that most\n\
175 users are not explicitly listed as members of the groups they are in\n\
176 according to the password database.  Check both databases to get\n\
177 complete membership information.)");
178 
179 
180 PyMODINIT_FUNC
181 initgrp(void)
182 {
183     PyObject *m, *d;
184     m = Py_InitModule3("grp", grp_methods, grp__doc__);
185     if (m == NULL)
186         return;
187     d = PyModule_GetDict(m);
188     if (!initialized)
189 	    PyStructSequence_InitType(&StructGrpType, &struct_group_type_desc);
190     PyDict_SetItemString(d, "struct_group", (PyObject *) &StructGrpType);
191     initialized = 1;
192 }