1 | n/a | |
---|
2 | n/a | /* UNIX group file access module */ |
---|
3 | n/a | |
---|
4 | n/a | #include "Python.h" |
---|
5 | n/a | #include "posixmodule.h" |
---|
6 | n/a | |
---|
7 | n/a | #include <grp.h> |
---|
8 | n/a | |
---|
9 | n/a | #include "clinic/grpmodule.c.h" |
---|
10 | n/a | /*[clinic input] |
---|
11 | n/a | module grp |
---|
12 | n/a | [clinic start generated code]*/ |
---|
13 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=cade63f2ed1bd9f8]*/ |
---|
14 | n/a | |
---|
15 | n/a | static PyStructSequence_Field struct_group_type_fields[] = { |
---|
16 | n/a | {"gr_name", "group name"}, |
---|
17 | n/a | {"gr_passwd", "password"}, |
---|
18 | n/a | {"gr_gid", "group id"}, |
---|
19 | n/a | {"gr_mem", "group members"}, |
---|
20 | n/a | {0} |
---|
21 | n/a | }; |
---|
22 | n/a | |
---|
23 | n/a | PyDoc_STRVAR(struct_group__doc__, |
---|
24 | n/a | "grp.struct_group: Results from getgr*() routines.\n\n\ |
---|
25 | n/a | This object may be accessed either as a tuple of\n\ |
---|
26 | n/a | (gr_name,gr_passwd,gr_gid,gr_mem)\n\ |
---|
27 | n/a | or via the object attributes as named in the above tuple.\n"); |
---|
28 | n/a | |
---|
29 | n/a | static PyStructSequence_Desc struct_group_type_desc = { |
---|
30 | n/a | "grp.struct_group", |
---|
31 | n/a | struct_group__doc__, |
---|
32 | n/a | struct_group_type_fields, |
---|
33 | n/a | 4, |
---|
34 | n/a | }; |
---|
35 | n/a | |
---|
36 | n/a | |
---|
37 | n/a | static int initialized; |
---|
38 | n/a | static PyTypeObject StructGrpType; |
---|
39 | n/a | |
---|
40 | n/a | static PyObject * |
---|
41 | n/a | mkgrent(struct group *p) |
---|
42 | n/a | { |
---|
43 | n/a | int setIndex = 0; |
---|
44 | n/a | PyObject *v = PyStructSequence_New(&StructGrpType), *w; |
---|
45 | n/a | char **member; |
---|
46 | n/a | |
---|
47 | n/a | if (v == NULL) |
---|
48 | n/a | return NULL; |
---|
49 | n/a | |
---|
50 | n/a | if ((w = PyList_New(0)) == NULL) { |
---|
51 | n/a | Py_DECREF(v); |
---|
52 | n/a | return NULL; |
---|
53 | n/a | } |
---|
54 | n/a | for (member = p->gr_mem; *member != NULL; member++) { |
---|
55 | n/a | PyObject *x = PyUnicode_DecodeFSDefault(*member); |
---|
56 | n/a | if (x == NULL || PyList_Append(w, x) != 0) { |
---|
57 | n/a | Py_XDECREF(x); |
---|
58 | n/a | Py_DECREF(w); |
---|
59 | n/a | Py_DECREF(v); |
---|
60 | n/a | return NULL; |
---|
61 | n/a | } |
---|
62 | n/a | Py_DECREF(x); |
---|
63 | n/a | } |
---|
64 | n/a | |
---|
65 | n/a | #define SET(i,val) PyStructSequence_SET_ITEM(v, i, val) |
---|
66 | n/a | SET(setIndex++, PyUnicode_DecodeFSDefault(p->gr_name)); |
---|
67 | n/a | if (p->gr_passwd) |
---|
68 | n/a | SET(setIndex++, PyUnicode_DecodeFSDefault(p->gr_passwd)); |
---|
69 | n/a | else { |
---|
70 | n/a | SET(setIndex++, Py_None); |
---|
71 | n/a | Py_INCREF(Py_None); |
---|
72 | n/a | } |
---|
73 | n/a | SET(setIndex++, _PyLong_FromGid(p->gr_gid)); |
---|
74 | n/a | SET(setIndex++, w); |
---|
75 | n/a | #undef SET |
---|
76 | n/a | |
---|
77 | n/a | if (PyErr_Occurred()) { |
---|
78 | n/a | Py_DECREF(v); |
---|
79 | n/a | return NULL; |
---|
80 | n/a | } |
---|
81 | n/a | |
---|
82 | n/a | return v; |
---|
83 | n/a | } |
---|
84 | n/a | |
---|
85 | n/a | /*[clinic input] |
---|
86 | n/a | grp.getgrgid |
---|
87 | n/a | |
---|
88 | n/a | id: object |
---|
89 | n/a | |
---|
90 | n/a | Return the group database entry for the given numeric group ID. |
---|
91 | n/a | |
---|
92 | n/a | If id is not valid, raise KeyError. |
---|
93 | n/a | [clinic start generated code]*/ |
---|
94 | n/a | |
---|
95 | n/a | static PyObject * |
---|
96 | n/a | grp_getgrgid_impl(PyObject *module, PyObject *id) |
---|
97 | n/a | /*[clinic end generated code: output=30797c289504a1ba input=15fa0e2ccf5cda25]*/ |
---|
98 | n/a | { |
---|
99 | n/a | PyObject *py_int_id; |
---|
100 | n/a | gid_t gid; |
---|
101 | n/a | struct group *p; |
---|
102 | n/a | |
---|
103 | n/a | if (!_Py_Gid_Converter(id, &gid)) { |
---|
104 | n/a | if (!PyErr_ExceptionMatches(PyExc_TypeError)) { |
---|
105 | n/a | return NULL; |
---|
106 | n/a | } |
---|
107 | n/a | PyErr_Clear(); |
---|
108 | n/a | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
---|
109 | n/a | "group id must be int, not %.200", |
---|
110 | n/a | id->ob_type->tp_name) < 0) { |
---|
111 | n/a | return NULL; |
---|
112 | n/a | } |
---|
113 | n/a | py_int_id = PyNumber_Long(id); |
---|
114 | n/a | if (!py_int_id) |
---|
115 | n/a | return NULL; |
---|
116 | n/a | if (!_Py_Gid_Converter(py_int_id, &gid)) { |
---|
117 | n/a | Py_DECREF(py_int_id); |
---|
118 | n/a | return NULL; |
---|
119 | n/a | } |
---|
120 | n/a | Py_DECREF(py_int_id); |
---|
121 | n/a | } |
---|
122 | n/a | |
---|
123 | n/a | if ((p = getgrgid(gid)) == NULL) { |
---|
124 | n/a | PyObject *gid_obj = _PyLong_FromGid(gid); |
---|
125 | n/a | if (gid_obj == NULL) |
---|
126 | n/a | return NULL; |
---|
127 | n/a | PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %S", gid_obj); |
---|
128 | n/a | Py_DECREF(gid_obj); |
---|
129 | n/a | return NULL; |
---|
130 | n/a | } |
---|
131 | n/a | return mkgrent(p); |
---|
132 | n/a | } |
---|
133 | n/a | |
---|
134 | n/a | /*[clinic input] |
---|
135 | n/a | grp.getgrnam |
---|
136 | n/a | |
---|
137 | n/a | name: unicode |
---|
138 | n/a | |
---|
139 | n/a | Return the group database entry for the given group name. |
---|
140 | n/a | |
---|
141 | n/a | If name is not valid, raise KeyError. |
---|
142 | n/a | [clinic start generated code]*/ |
---|
143 | n/a | |
---|
144 | n/a | static PyObject * |
---|
145 | n/a | grp_getgrnam_impl(PyObject *module, PyObject *name) |
---|
146 | n/a | /*[clinic end generated code: output=67905086f403c21c input=08ded29affa3c863]*/ |
---|
147 | n/a | { |
---|
148 | n/a | char *name_chars; |
---|
149 | n/a | struct group *p; |
---|
150 | n/a | PyObject *bytes, *retval = NULL; |
---|
151 | n/a | |
---|
152 | n/a | if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL) |
---|
153 | n/a | return NULL; |
---|
154 | n/a | if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1) |
---|
155 | n/a | goto out; |
---|
156 | n/a | |
---|
157 | n/a | if ((p = getgrnam(name_chars)) == NULL) { |
---|
158 | n/a | PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name_chars); |
---|
159 | n/a | goto out; |
---|
160 | n/a | } |
---|
161 | n/a | retval = mkgrent(p); |
---|
162 | n/a | out: |
---|
163 | n/a | Py_DECREF(bytes); |
---|
164 | n/a | return retval; |
---|
165 | n/a | } |
---|
166 | n/a | |
---|
167 | n/a | /*[clinic input] |
---|
168 | n/a | grp.getgrall |
---|
169 | n/a | |
---|
170 | n/a | Return a list of all available group entries, in arbitrary order. |
---|
171 | n/a | |
---|
172 | n/a | An entry whose name starts with '+' or '-' represents an instruction |
---|
173 | n/a | to use YP/NIS and may not be accessible via getgrnam or getgrgid. |
---|
174 | n/a | [clinic start generated code]*/ |
---|
175 | n/a | |
---|
176 | n/a | static PyObject * |
---|
177 | n/a | grp_getgrall_impl(PyObject *module) |
---|
178 | n/a | /*[clinic end generated code: output=585dad35e2e763d7 input=d7df76c825c367df]*/ |
---|
179 | n/a | { |
---|
180 | n/a | PyObject *d; |
---|
181 | n/a | struct group *p; |
---|
182 | n/a | |
---|
183 | n/a | if ((d = PyList_New(0)) == NULL) |
---|
184 | n/a | return NULL; |
---|
185 | n/a | setgrent(); |
---|
186 | n/a | while ((p = getgrent()) != NULL) { |
---|
187 | n/a | PyObject *v = mkgrent(p); |
---|
188 | n/a | if (v == NULL || PyList_Append(d, v) != 0) { |
---|
189 | n/a | Py_XDECREF(v); |
---|
190 | n/a | Py_DECREF(d); |
---|
191 | n/a | endgrent(); |
---|
192 | n/a | return NULL; |
---|
193 | n/a | } |
---|
194 | n/a | Py_DECREF(v); |
---|
195 | n/a | } |
---|
196 | n/a | endgrent(); |
---|
197 | n/a | return d; |
---|
198 | n/a | } |
---|
199 | n/a | |
---|
200 | n/a | static PyMethodDef grp_methods[] = { |
---|
201 | n/a | GRP_GETGRGID_METHODDEF |
---|
202 | n/a | GRP_GETGRNAM_METHODDEF |
---|
203 | n/a | GRP_GETGRALL_METHODDEF |
---|
204 | n/a | {NULL, NULL} |
---|
205 | n/a | }; |
---|
206 | n/a | |
---|
207 | n/a | PyDoc_STRVAR(grp__doc__, |
---|
208 | n/a | "Access to the Unix group database.\n\ |
---|
209 | n/a | \n\ |
---|
210 | n/a | Group entries are reported as 4-tuples containing the following fields\n\ |
---|
211 | n/a | from the group database, in order:\n\ |
---|
212 | n/a | \n\ |
---|
213 | n/a | gr_name - name of the group\n\ |
---|
214 | n/a | gr_passwd - group password (encrypted); often empty\n\ |
---|
215 | n/a | gr_gid - numeric ID of the group\n\ |
---|
216 | n/a | gr_mem - list of members\n\ |
---|
217 | n/a | \n\ |
---|
218 | n/a | The gid is an integer, name and password are strings. (Note that most\n\ |
---|
219 | n/a | users are not explicitly listed as members of the groups they are in\n\ |
---|
220 | n/a | according to the password database. Check both databases to get\n\ |
---|
221 | n/a | complete membership information.)"); |
---|
222 | n/a | |
---|
223 | n/a | |
---|
224 | n/a | |
---|
225 | n/a | static struct PyModuleDef grpmodule = { |
---|
226 | n/a | PyModuleDef_HEAD_INIT, |
---|
227 | n/a | "grp", |
---|
228 | n/a | grp__doc__, |
---|
229 | n/a | -1, |
---|
230 | n/a | grp_methods, |
---|
231 | n/a | NULL, |
---|
232 | n/a | NULL, |
---|
233 | n/a | NULL, |
---|
234 | n/a | NULL |
---|
235 | n/a | }; |
---|
236 | n/a | |
---|
237 | n/a | PyMODINIT_FUNC |
---|
238 | n/a | PyInit_grp(void) |
---|
239 | n/a | { |
---|
240 | n/a | PyObject *m, *d; |
---|
241 | n/a | m = PyModule_Create(&grpmodule); |
---|
242 | n/a | if (m == NULL) |
---|
243 | n/a | return NULL; |
---|
244 | n/a | d = PyModule_GetDict(m); |
---|
245 | n/a | if (!initialized) { |
---|
246 | n/a | if (PyStructSequence_InitType2(&StructGrpType, |
---|
247 | n/a | &struct_group_type_desc) < 0) |
---|
248 | n/a | return NULL; |
---|
249 | n/a | } |
---|
250 | n/a | if (PyDict_SetItemString(d, "struct_group", |
---|
251 | n/a | (PyObject *)&StructGrpType) < 0) |
---|
252 | n/a | return NULL; |
---|
253 | n/a | initialized = 1; |
---|
254 | n/a | return m; |
---|
255 | n/a | } |
---|