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 rpc_pipe_client
*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
= GENERIC_EXECUTE_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 (server
[0] != '\\' || server
[1] != '\\') {
75 PyErr_SetString(PyExc_ValueError
, "UNC name required");
81 if (!(cli
= open_pipe_creds(server
, creds
, PI_LSARPC
, &errstr
))) {
82 PyErr_SetString(lsa_error
, errstr
);
87 if (!(mem_ctx
= talloc_init("lsa_open_policy"))) {
88 PyErr_SetString(lsa_error
, "unable to init talloc context\n");
92 ntstatus
= rpccli_lsa_open_policy(
93 cli
->pipe_list
, mem_ctx
, True
, desired_access
, &hnd
);
95 if (!NT_STATUS_IS_OK(ntstatus
)) {
96 PyErr_SetObject(lsa_ntstatus
, py_ntstatus_tuple(ntstatus
));
100 result
= new_lsa_policy_hnd_object(cli
->pipe_list
, mem_ctx
, &hnd
);
107 talloc_destroy(mem_ctx
);
113 static PyObject
*lsa_close(PyObject
*self
, PyObject
*args
, PyObject
*kw
)
116 lsa_policy_hnd_object
*hnd
;
119 /* Parse parameters */
121 if (!PyArg_ParseTuple(args
, "O!", &lsa_policy_hnd_type
, &po
))
124 hnd
= (lsa_policy_hnd_object
*)po
;
126 /* Call rpc function */
128 result
= rpccli_lsa_close(hnd
->cli
, hnd
->mem_ctx
, &hnd
->pol
);
130 /* Cleanup samba stuff */
132 cli_shutdown(hnd
->cli
);
133 talloc_destroy(hnd
->mem_ctx
);
141 static PyObject
*lsa_lookup_names(PyObject
*self
, PyObject
*args
)
143 PyObject
*py_names
, *result
= NULL
;
145 lsa_policy_hnd_object
*hnd
= (lsa_policy_hnd_object
*)self
;
149 TALLOC_CTX
*mem_ctx
= NULL
;
150 enum lsa_SidType
*name_types
;
152 if (!PyArg_ParseTuple(args
, "O", &py_names
))
155 if (!PyList_Check(py_names
) && !PyString_Check(py_names
)) {
156 PyErr_SetString(PyExc_TypeError
, "must be list or string");
160 if (!(mem_ctx
= talloc_init("lsa_lookup_names"))) {
161 PyErr_SetString(lsa_error
, "unable to init talloc context\n");
165 if (PyList_Check(py_names
)) {
167 /* Convert list to char ** array */
169 num_names
= PyList_Size(py_names
);
170 names
= (const char **)_talloc(mem_ctx
, num_names
* sizeof(char *));
172 for (i
= 0; i
< num_names
; i
++) {
173 PyObject
*obj
= PyList_GetItem(py_names
, i
);
175 names
[i
] = talloc_strdup(mem_ctx
, PyString_AsString(obj
));
180 /* Just a single element */
183 names
= (const char **)_talloc(mem_ctx
, sizeof(char *));
185 names
[0] = PyString_AsString(py_names
);
188 ntstatus
= rpccli_lsa_lookup_names(
189 hnd
->cli
, mem_ctx
, &hnd
->pol
, num_names
, names
,
190 NULL
, &sids
, &name_types
);
192 if (!NT_STATUS_IS_OK(ntstatus
) && NT_STATUS_V(ntstatus
) != 0x107) {
193 PyErr_SetObject(lsa_ntstatus
, py_ntstatus_tuple(ntstatus
));
197 result
= PyList_New(num_names
);
199 for (i
= 0; i
< num_names
; i
++) {
200 PyObject
*sid_obj
, *obj
;
202 py_from_SID(&sid_obj
, &sids
[i
]);
204 obj
= Py_BuildValue("(Ni)", sid_obj
, name_types
[i
]);
206 PyList_SetItem(result
, i
, obj
);
210 talloc_destroy(mem_ctx
);
215 static PyObject
*lsa_lookup_sids(PyObject
*self
, PyObject
*args
,
218 PyObject
*py_sids
, *result
= NULL
;
221 char **domains
, **names
;
223 lsa_policy_hnd_object
*hnd
= (lsa_policy_hnd_object
*)self
;
224 TALLOC_CTX
*mem_ctx
= NULL
;
227 if (!PyArg_ParseTuple(args
, "O", &py_sids
))
230 if (!PyList_Check(py_sids
) && !PyString_Check(py_sids
)) {
231 PyErr_SetString(PyExc_TypeError
, "must be list or string");
235 if (!(mem_ctx
= talloc_init("lsa_lookup_sids"))) {
236 PyErr_SetString(lsa_error
, "unable to init talloc context\n");
240 if (PyList_Check(py_sids
)) {
242 /* Convert dictionary to char ** array */
244 num_sids
= PyList_Size(py_sids
);
245 sids
= (DOM_SID
*)_talloc(mem_ctx
, num_sids
* sizeof(DOM_SID
));
247 memset(sids
, 0, num_sids
* sizeof(DOM_SID
));
249 for (i
= 0; i
< num_sids
; i
++) {
250 PyObject
*obj
= PyList_GetItem(py_sids
, i
);
252 if (!string_to_sid(&sids
[i
], PyString_AsString(obj
))) {
253 PyErr_SetString(PyExc_ValueError
, "string_to_sid failed");
260 /* Just a single element */
263 sids
= (DOM_SID
*)_talloc(mem_ctx
, sizeof(DOM_SID
));
265 if (!string_to_sid(&sids
[0], PyString_AsString(py_sids
))) {
266 PyErr_SetString(PyExc_ValueError
, "string_to_sid failed");
271 ntstatus
= rpccli_lsa_lookup_sids(
272 hnd
->cli
, mem_ctx
, &hnd
->pol
, num_sids
, sids
, &domains
,
275 if (!NT_STATUS_IS_OK(ntstatus
)) {
276 PyErr_SetObject(lsa_ntstatus
, py_ntstatus_tuple(ntstatus
));
280 result
= PyList_New(num_sids
);
282 for (i
= 0; i
< num_sids
; i
++) {
285 obj
= Py_BuildValue("{sssssi}", "username", names
[i
],
286 "domain", domains
[i
], "name_type",
289 PyList_SetItem(result
, i
, obj
);
293 talloc_destroy(mem_ctx
);
298 static PyObject
*lsa_enum_trust_dom(PyObject
*self
, PyObject
*args
)
300 lsa_policy_hnd_object
*hnd
= (lsa_policy_hnd_object
*)self
;
302 uint32 enum_ctx
= 0, num_domains
, i
;
304 DOM_SID
*domain_sids
;
307 if (!PyArg_ParseTuple(args
, ""))
310 ntstatus
= rpccli_lsa_enum_trust_dom(
311 hnd
->cli
, hnd
->mem_ctx
, &hnd
->pol
, &enum_ctx
,
312 &num_domains
, &domain_names
, &domain_sids
);
314 if (!NT_STATUS_IS_OK(ntstatus
)) {
315 PyErr_SetObject(lsa_ntstatus
, py_ntstatus_tuple(ntstatus
));
319 result
= PyList_New(num_domains
);
321 for (i
= 0; i
< num_domains
; i
++) {
324 sid_to_string(sid_str
, &domain_sids
[i
]);
327 Py_BuildValue("(ss)", domain_names
[i
], sid_str
));
334 * Method dispatch tables
337 static PyMethodDef lsa_hnd_methods
[] = {
341 { "lookup_sids", (PyCFunction
)lsa_lookup_sids
,
342 METH_VARARGS
| METH_KEYWORDS
,
343 "Convert sids to names." },
345 { "lookup_names", (PyCFunction
)lsa_lookup_names
,
346 METH_VARARGS
| METH_KEYWORDS
,
347 "Convert names to sids." },
349 /* Trusted domains */
351 { "enum_trusted_domains", (PyCFunction
)lsa_enum_trust_dom
,
353 "Enumerate trusted domains." },
358 static void py_lsa_policy_hnd_dealloc(PyObject
* self
)
363 static PyObject
*py_lsa_policy_hnd_getattr(PyObject
*self
, char *attrname
)
365 return Py_FindMethod(lsa_hnd_methods
, self
, attrname
);
368 PyTypeObject lsa_policy_hnd_type
= {
369 PyObject_HEAD_INIT(NULL
)
372 sizeof(lsa_policy_hnd_object
),
374 py_lsa_policy_hnd_dealloc
, /*tp_dealloc*/
376 py_lsa_policy_hnd_getattr
, /*tp_getattr*/
381 0, /*tp_as_sequence*/
386 static PyMethodDef lsa_methods
[] = {
388 /* Open/close lsa handles */
390 { "open_policy", (PyCFunction
)lsa_open_policy
,
391 METH_VARARGS
| METH_KEYWORDS
,
392 "Open a policy handle" },
394 { "close", (PyCFunction
)lsa_close
,
396 "Close a policy handle" },
398 /* Other stuff - this should really go into a samba config module
399 but for the moment let's leave it here. */
401 { "setup_logging", (PyCFunction
)py_setup_logging
,
402 METH_VARARGS
| METH_KEYWORDS
,
403 "Set up debug logging.\n"
405 "Initialises Samba's debug logging system. One argument is expected which\n"
406 "is a boolean specifying whether debugging is interactive and sent to stdout\n"
407 "or logged to a file.\n"
411 ">>> lsa.setup_logging(interactive = 1)" },
413 { "get_debuglevel", (PyCFunction
)get_debuglevel
,
415 "Set the current debug level.\n"
419 ">>> lsa.get_debuglevel()\n"
422 { "set_debuglevel", (PyCFunction
)set_debuglevel
,
424 "Get the current debug level.\n"
428 ">>> lsa.set_debuglevel(10)" },
433 static struct const_vals
{
436 } module_const_vals
[] = {
440 static void const_init(PyObject
*dict
)
442 struct const_vals
*tmp
;
445 for (tmp
= module_const_vals
; tmp
->name
; tmp
++) {
446 obj
= PyInt_FromLong(tmp
->value
);
447 PyDict_SetItemString(dict
, tmp
->name
, obj
);
453 * Module initialisation
458 PyObject
*module
, *dict
;
460 /* Initialise module */
462 module
= Py_InitModule("lsa", lsa_methods
);
463 dict
= PyModule_GetDict(module
);
465 lsa_error
= PyErr_NewException("lsa.error", NULL
, NULL
);
466 PyDict_SetItemString(dict
, "error", lsa_error
);
468 lsa_ntstatus
= PyErr_NewException("lsa.ntstatus", NULL
, NULL
);
469 PyDict_SetItemString(dict
, "ntstatus", lsa_ntstatus
);
471 /* Initialise policy handle object */
473 lsa_policy_hnd_type
.ob_type
= &PyType_Type
;
475 /* Initialise constants */
479 /* Do samba initialisation */
483 setup_logging("lsa", True
);