2 Python wrappers for DCERPC/SMB client routines.
4 Copyright (C) Tim Potter, 2002
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "python/py_lsa.h"
23 PyObject *new_lsa_policy_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
26 lsa_policy_hnd_object *o;
28 o = PyObject_New(lsa_policy_hnd_object, &lsa_policy_hnd_type);
32 memcpy(&o->pol, pol, sizeof(POLICY_HND));
38 * Exceptions raised by this module
41 PyObject *lsa_error; /* This indicates a non-RPC related error
42 such as name lookup failure */
44 PyObject *lsa_ntstatus; /* This exception is raised when a RPC call
45 returns a status code other than
49 * Open/close lsa handles
52 static PyObject *lsa_open_policy(PyObject *self, PyObject *args,
55 static char *kwlist[] = { "servername", "creds", "access", NULL };
56 char *server, *errstr;
57 PyObject *creds = NULL, *result = NULL;
58 uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
59 struct cli_state *cli = NULL;
61 TALLOC_CTX *mem_ctx = NULL;
64 if (!PyArg_ParseTupleAndKeywords(
65 args, kw, "s|Oi", kwlist, &server, &creds, &desired_access))
68 if (creds && creds != Py_None && !PyDict_Check(creds)) {
69 PyErr_SetString(PyExc_TypeError,
70 "credentials must be dictionary or None");
74 if (!(cli = open_pipe_creds(server, creds, PIPE_LSARPC, &errstr))) {
75 PyErr_SetString(lsa_error, errstr);
80 if (!(mem_ctx = talloc_init())) {
81 PyErr_SetString(lsa_error, "unable to init talloc context\n");
85 ntstatus = cli_lsa_open_policy(cli, mem_ctx, True,
86 SEC_RIGHTS_MAXIMUM_ALLOWED, &hnd);
88 if (!NT_STATUS_IS_OK(ntstatus)) {
89 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
93 result = new_lsa_policy_hnd_object(cli, mem_ctx, &hnd);
101 talloc_destroy(mem_ctx);
107 static PyObject *lsa_close(PyObject *self, PyObject *args, PyObject *kw)
110 lsa_policy_hnd_object *hnd;
113 /* Parse parameters */
115 if (!PyArg_ParseTuple(args, "O!", &lsa_policy_hnd_type, &po))
118 hnd = (lsa_policy_hnd_object *)po;
120 /* Call rpc function */
122 result = cli_lsa_close(hnd->cli, hnd->mem_ctx, &hnd->pol);
124 /* Cleanup samba stuff */
126 cli_shutdown(hnd->cli);
127 talloc_destroy(hnd->mem_ctx);
135 static PyObject *lsa_lookup_names(PyObject *self, PyObject *args)
137 PyObject *py_names, *result;
139 lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
145 if (!PyArg_ParseTuple(args, "O", &py_names))
148 if (!PyList_Check(py_names) && !PyString_Check(py_names)) {
149 PyErr_SetString(PyExc_TypeError, "must be list or string");
153 if (PyList_Check(py_names)) {
155 /* Convert list to char ** array */
157 num_names = PyList_Size(py_names);
158 names = (const char **)talloc(
159 hnd->mem_ctx, num_names * sizeof(char *));
161 for (i = 0; i < num_names; i++) {
162 PyObject *obj = PyList_GetItem(py_names, i);
164 names[i] = talloc_strdup(hnd->mem_ctx, PyString_AsString(obj));
169 /* Just a single element */
172 names = (const char **)talloc(hnd->mem_ctx, sizeof(char *));
174 names[0] = PyString_AsString(py_names);
177 ntstatus = cli_lsa_lookup_names(hnd->cli, hnd->mem_ctx, &hnd->pol,
178 num_names, names, &sids, &name_types);
180 if (!NT_STATUS_IS_OK(ntstatus) && NT_STATUS_V(ntstatus) != 0x107) {
181 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
185 result = PyList_New(num_names);
187 for (i = 0; i < num_names; i++) {
188 PyObject *sid_obj, *obj;
190 py_from_SID(&sid_obj, &sids[i]);
192 obj = Py_BuildValue("(Oi)", sid_obj, name_types[i]);
194 PyList_SetItem(result, i, obj);
200 static PyObject *lsa_lookup_sids(PyObject *self, PyObject *args,
203 PyObject *py_sids, *result;
206 char **domains, **names;
208 lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
211 if (!PyArg_ParseTuple(args, "O", &py_sids))
214 if (!PyList_Check(py_sids) && !PyString_Check(py_sids)) {
215 PyErr_SetString(PyExc_TypeError, "must be list or string");
219 if (PyList_Check(py_sids)) {
221 /* Convert dictionary to char ** array */
223 num_sids = PyList_Size(py_sids);
224 sids = (DOM_SID *)talloc(hnd->mem_ctx, num_sids * sizeof(DOM_SID));
226 memset(sids, 0, num_sids * sizeof(DOM_SID));
228 for (i = 0; i < num_sids; i++) {
229 PyObject *obj = PyList_GetItem(py_sids, i);
231 string_to_sid(&sids[i], PyString_AsString(obj));
236 /* Just a single element */
239 sids = (DOM_SID *)talloc(hnd->mem_ctx, sizeof(DOM_SID));
241 string_to_sid(&sids[0], PyString_AsString(py_sids));
244 ntstatus = cli_lsa_lookup_sids(hnd->cli, hnd->mem_ctx, &hnd->pol,
245 num_sids, sids, &domains, &names,
248 if (!NT_STATUS_IS_OK(ntstatus)) {
249 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
253 result = PyList_New(num_sids);
255 for (i = 0; i < num_sids; i++) {
258 obj = Py_BuildValue("{sssssi}", "username", names[i],
259 "domain", domains[i], "name_type",
262 PyList_SetItem(result, i, obj);
268 static PyObject *lsa_enum_trust_dom(PyObject *self, PyObject *args)
270 lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
272 uint32 enum_ctx = 0, num_domains, i, pref_num_domains = 0;
274 DOM_SID *domain_sids;
277 if (!PyArg_ParseTuple(args, ""))
280 ntstatus = cli_lsa_enum_trust_dom(
281 hnd->cli, hnd->mem_ctx, &hnd->pol, &enum_ctx,
282 &pref_num_domains, &num_domains, &domain_names, &domain_sids);
284 if (!NT_STATUS_IS_OK(ntstatus)) {
285 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
289 result = PyList_New(num_domains);
291 for (i = 0; i < num_domains; i++) {
294 sid_to_string(sid_str, &domain_sids[i]);
297 Py_BuildValue("(ss)", domain_names[i], sid_str));
304 * Method dispatch tables
307 static PyMethodDef lsa_hnd_methods[] = {
311 { "lookup_sids", (PyCFunction)lsa_lookup_sids,
312 METH_VARARGS | METH_KEYWORDS,
313 "Convert sids to names." },
315 { "lookup_names", (PyCFunction)lsa_lookup_names,
316 METH_VARARGS | METH_KEYWORDS,
317 "Convert names to sids." },
319 /* Trusted domains */
321 { "enum_trusted_domains", (PyCFunction)lsa_enum_trust_dom,
323 "Enumerate trusted domains." },
328 static void py_lsa_policy_hnd_dealloc(PyObject* self)
333 static PyObject *py_lsa_policy_hnd_getattr(PyObject *self, char *attrname)
335 return Py_FindMethod(lsa_hnd_methods, self, attrname);
338 PyTypeObject lsa_policy_hnd_type = {
339 PyObject_HEAD_INIT(NULL)
342 sizeof(lsa_policy_hnd_object),
344 py_lsa_policy_hnd_dealloc, /*tp_dealloc*/
346 py_lsa_policy_hnd_getattr, /*tp_getattr*/
351 0, /*tp_as_sequence*/
356 static PyMethodDef lsa_methods[] = {
358 /* Open/close lsa handles */
360 { "open_policy", (PyCFunction)lsa_open_policy,
361 METH_VARARGS | METH_KEYWORDS,
362 "Open a policy handle" },
364 { "close", (PyCFunction)lsa_close,
366 "Close a policy handle" },
368 /* Other stuff - this should really go into a samba config module
369 but for the moment let's leave it here. */
371 { "setup_logging", (PyCFunction)py_setup_logging,
372 METH_VARARGS | METH_KEYWORDS,
373 "Set up debug logging.
375 Initialises Samba's debug logging system. One argument is expected which
376 is a boolean specifying whether debugging is interactive and sent to stdout
381 >>> spoolss.setup_logging(interactive = 1)" },
383 { "get_debuglevel", (PyCFunction)get_debuglevel,
385 "Set the current debug level.
389 >>> spoolss.get_debuglevel()
392 { "set_debuglevel", (PyCFunction)set_debuglevel,
394 "Get the current debug level.
398 >>> spoolss.set_debuglevel(10)" },
403 static struct const_vals {
406 } module_const_vals[] = {
410 static void const_init(PyObject *dict)
412 struct const_vals *tmp;
415 for (tmp = module_const_vals; tmp->name; tmp++) {
416 obj = PyInt_FromLong(tmp->value);
417 PyDict_SetItemString(dict, tmp->name, obj);
423 * Module initialisation
428 PyObject *module, *dict;
430 /* Initialise module */
432 module = Py_InitModule("lsa", lsa_methods);
433 dict = PyModule_GetDict(module);
435 lsa_error = PyErr_NewException("lsa.error", NULL, NULL);
436 PyDict_SetItemString(dict, "error", lsa_error);
438 lsa_ntstatus = PyErr_NewException("lsa.ntstatus", NULL, NULL);
439 PyDict_SetItemString(dict, "ntstatus", lsa_ntstatus);
441 /* Initialise policy handle object */
443 lsa_policy_hnd_type.ob_type = &PyType_Type;
445 /* Initialise constants */
449 /* Do samba initialisation */
453 setup_logging("lsa", True);