2 Unix SMB/CIFS implementation.
4 Python wrapper for winbind client functions.
6 Copyright (C) Tim Potter 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * Exceptions raised by this module
30 PyObject *winbind_error; /* A winbind call returned WINBINDD_ERROR */
32 /* Prototypes from common.h */
34 NSS_STATUS winbindd_request(int req_type,
35 struct winbindd_request *request,
36 struct winbindd_response *response);
39 * Name <-> SID conversion
42 /* Convert a name to a sid */
44 static PyObject *winbind_name_to_sid(PyObject *self, PyObject *args)
47 struct winbindd_request request;
48 struct winbindd_response response;
52 if (!PyArg_ParseTuple(args, "s", &name))
56 ZERO_STRUCT(response);
58 /* FIXME: use winbind separator */
60 if ((p = strchr(name, '\\'))) {
62 fstrcpy(request.data.name.dom_name, name);
63 fstrcpy(request.data.name.name, p + 1);
65 fstrcpy(request.data.name.dom_name, lp_workgroup());
66 fstrcpy(request.data.name.name, name);
69 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response)
70 != NSS_STATUS_SUCCESS) {
71 PyErr_SetString(winbind_error, "lookup failed");
75 result = PyString_FromString(response.data.sid.sid);
80 /* Convert a sid to a name */
82 static PyObject *winbind_sid_to_name(PyObject *self, PyObject *args)
84 struct winbindd_request request;
85 struct winbindd_response response;
89 if (!PyArg_ParseTuple(args, "s", &sid))
93 ZERO_STRUCT(response);
95 fstrcpy(request.data.sid, sid);
97 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response)
98 != NSS_STATUS_SUCCESS) {
99 PyErr_SetString(winbind_error, "lookup failed");
103 /* FIXME: use actual winbind separator */
105 asprintf(&name, "%s%c%s", response.data.name.dom_name,
106 '\\', response.data.name.name);
108 result = PyString_FromString(name);
116 * Enumerate users/groups
119 /* Enumerate domain users */
121 static PyObject *winbind_enum_domain_users(PyObject *self, PyObject *args)
123 struct winbindd_response response;
126 if (!PyArg_ParseTuple(args, ""))
129 ZERO_STRUCT(response);
131 if (winbindd_request(WINBINDD_LIST_USERS, NULL, &response)
132 != NSS_STATUS_SUCCESS) {
133 PyErr_SetString(winbind_error, "lookup failed");
137 result = PyList_New(0);
139 if (response.extra_data) {
140 char *extra_data = response.extra_data;
143 while (next_token(&extra_data, name, ",", sizeof(fstring)))
144 PyList_Append(result, PyString_FromString(name));
150 /* Enumerate domain groups */
152 static PyObject *winbind_enum_domain_groups(PyObject *self, PyObject *args)
154 struct winbindd_response response;
155 PyObject *result = NULL;
157 if (!PyArg_ParseTuple(args, ""))
160 ZERO_STRUCT(response);
162 if (winbindd_request(WINBINDD_LIST_GROUPS, NULL, &response)
163 != NSS_STATUS_SUCCESS) {
164 PyErr_SetString(winbind_error, "lookup failed");
168 result = PyList_New(0);
170 if (response.extra_data) {
171 char *extra_data = response.extra_data;
174 while (next_token(&extra_data, name, ",", sizeof(fstring)))
175 PyList_Append(result, PyString_FromString(name));
182 * Miscellaneous domain related
185 /* Enumerate domain groups */
187 static PyObject *winbind_enum_trust_dom(PyObject *self, PyObject *args)
189 struct winbindd_response response;
190 PyObject *result = NULL;
192 if (!PyArg_ParseTuple(args, ""))
195 ZERO_STRUCT(response);
197 if (winbindd_request(WINBINDD_LIST_TRUSTDOM, NULL, &response)
198 != NSS_STATUS_SUCCESS) {
199 PyErr_SetString(winbind_error, "lookup failed");
203 result = PyList_New(0);
205 if (response.extra_data) {
206 char *extra_data = response.extra_data;
209 while (next_token(&extra_data, name, ",", sizeof(fstring)))
210 PyList_Append(result, PyString_FromString(name));
216 /* Check machine account password */
218 static PyObject *winbind_check_secret(PyObject *self, PyObject *args)
220 struct winbindd_response response;
222 if (!PyArg_ParseTuple(args, ""))
225 ZERO_STRUCT(response);
227 if (winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response)
228 != NSS_STATUS_SUCCESS) {
229 PyErr_SetString(winbind_error, "lookup failed");
233 return PyInt_FromLong(response.data.num_entries);
237 * Return a dictionary consisting of all the winbind related smb.conf
238 * parameters. This is stored in the module object.
241 static PyObject *winbind_config_dict(void)
247 if (!(result = PyDict_New()))
250 /* Various string parameters */
252 PyDict_SetItemString(result, "workgroup",
253 PyString_FromString(lp_workgroup()));
255 PyDict_SetItemString(result, "separator",
256 PyString_FromString(lp_winbind_separator()));
258 PyDict_SetItemString(result, "template homedir",
259 PyString_FromString(lp_template_homedir()));
261 PyDict_SetItemString(result, "template shell",
262 PyString_FromString(lp_template_shell()));
264 /* Winbind uid/gid range */
266 if (lp_winbind_uid(&ulow, &uhi)) {
267 PyDict_SetItemString(result, "uid low", PyInt_FromLong(ulow));
268 PyDict_SetItemString(result, "uid high", PyInt_FromLong(uhi));
271 if (lp_winbind_gid(&glow, &ghi)) {
272 PyDict_SetItemString(result, "gid low", PyInt_FromLong(glow));
273 PyDict_SetItemString(result, "gid high", PyInt_FromLong(ghi));
280 * Method dispatch table
283 static PyMethodDef winbind_methods[] = {
285 /* Name <-> SID conversion */
287 { "name_to_sid", winbind_name_to_sid, METH_VARARGS,
288 "Convert a name to a sid" },
290 { "sid_to_name", winbind_sid_to_name, METH_VARARGS,
291 "Convert a sid to a name" },
293 /* Enumerate users/groups */
295 { "enum_domain_users", winbind_enum_domain_users, METH_VARARGS,
296 "Enumerate domain users" },
298 { "enum_domain_groups", winbind_enum_domain_groups, METH_VARARGS,
299 "Enumerate domain groups" },
303 { "check_secret", winbind_check_secret, METH_VARARGS,
304 "Check machine account password" },
306 { "enum_trust_dom", winbind_enum_trust_dom, METH_VARARGS,
307 "Enumerate trusted domains" },
313 * Module initialisation
316 void initwinbind(void)
318 PyObject *module, *dict;
320 /* Initialise module */
322 module = Py_InitModule("winbind", winbind_methods);
323 dict = PyModule_GetDict(module);
325 winbind_error = PyErr_NewException("winbind.error", NULL, NULL);
326 PyDict_SetItemString(dict, "error", winbind_error);
328 /* Do samba initialisation */
332 /* Insert configuration dictionary */
334 PyDict_SetItemString(dict, "config", winbind_config_dict());