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);
38 /* FIXME: grr this needs to be a fn in a library somewhere */
40 static BOOL parse_domain_user(const char *domuser, fstring domain,
43 char *p = strchr(domuser,*lp_winbind_separator());
45 if (!(p || lp_winbind_use_default_domain()))
48 if(!p && lp_winbind_use_default_domain()) {
49 fstrcpy(user, domuser);
50 fstrcpy(domain, lp_workgroup());
53 fstrcpy(domain, domuser);
54 domain[PTR_DIFF(p, domuser)] = 0;
61 * Name <-> SID conversion
64 /* Convert a name to a sid */
66 static PyObject *winbind_name_to_sid(PyObject *self, PyObject *args)
69 struct winbindd_request request;
70 struct winbindd_response response;
74 if (!PyArg_ParseTuple(args, "s", &name))
78 ZERO_STRUCT(response);
80 /* FIXME: use winbind separator */
82 if ((p = strchr(name, '\\'))) {
84 fstrcpy(request.data.name.dom_name, name);
85 fstrcpy(request.data.name.name, p + 1);
87 fstrcpy(request.data.name.dom_name, lp_workgroup());
88 fstrcpy(request.data.name.name, name);
91 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response)
92 != NSS_STATUS_SUCCESS) {
93 PyErr_SetString(winbind_error, "lookup failed");
97 result = PyString_FromString(response.data.sid.sid);
102 /* Convert a sid to a name */
104 static PyObject *winbind_sid_to_name(PyObject *self, PyObject *args)
106 struct winbindd_request request;
107 struct winbindd_response response;
111 if (!PyArg_ParseTuple(args, "s", &sid))
114 ZERO_STRUCT(request);
115 ZERO_STRUCT(response);
117 fstrcpy(request.data.sid, sid);
119 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response)
120 != NSS_STATUS_SUCCESS) {
121 PyErr_SetString(winbind_error, "lookup failed");
125 /* FIXME: use actual winbind separator */
127 asprintf(&name, "%s%c%s", response.data.name.dom_name,
128 '\\', response.data.name.name);
130 result = PyString_FromString(name);
138 * Method dispatch table
141 static PyMethodDef winbind_methods[] = {
143 /* Name <-> SID conversion */
145 { "name_to_sid", winbind_name_to_sid, METH_VARARGS,
146 "Convert a name to a sid" },
148 { "sid_to_name", winbind_sid_to_name, METH_VARARGS,
149 "Convert a sid to a name" },
155 * Module initialisation
158 void initwinbind(void)
160 PyObject *module, *dict;
162 /* Initialise module */
164 module = Py_InitModule("winbind", winbind_methods);
165 dict = PyModule_GetDict(module);
167 winbind_error = PyErr_NewException("winbind.error", NULL, NULL);
168 PyDict_SetItemString(dict, "error", winbind_error);