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;
58 uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
59 struct cli_state *cli;
64 if (!PyArg_ParseTupleAndKeywords(
65 args, kw, "s|O!i", kwlist, &server, &PyDict_Type,
66 &creds, &desired_access))
69 if (!(cli = open_pipe_creds(
70 server, creds, cli_lsa_initialise, &errstr))) {
71 PyErr_SetString(lsa_error, errstr);
76 if (!(mem_ctx = talloc_init())) {
78 lsa_error, "unable to initialise talloc context\n");
82 ntstatus = cli_lsa_open_policy(cli, mem_ctx, True,
83 SEC_RIGHTS_MAXIMUM_ALLOWED, &hnd);
85 if (!NT_STATUS_IS_OK(ntstatus)) {
88 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
92 result = new_lsa_policy_hnd_object(cli, mem_ctx, &hnd);
97 static PyObject *lsa_close(PyObject *self, PyObject *args, PyObject *kw)
100 lsa_policy_hnd_object *hnd;
103 /* Parse parameters */
105 if (!PyArg_ParseTuple(args, "O!", &lsa_policy_hnd_type, &po))
108 hnd = (lsa_policy_hnd_object *)po;
110 /* Call rpc function */
112 result = cli_lsa_close(hnd->cli, hnd->mem_ctx, &hnd->pol);
114 /* Cleanup samba stuff */
116 cli_shutdown(hnd->cli);
117 talloc_destroy(hnd->mem_ctx);
125 static PyObject *lsa_lookup_names(PyObject *self, PyObject *args)
127 PyObject *py_names, *result;
129 lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
135 if (!PyArg_ParseTuple(args, "O", &py_names))
138 if (!PyList_Check(py_names) && !PyString_Check(py_names)) {
139 PyErr_SetString(PyExc_TypeError, "must be list or string");
143 if (PyList_Check(py_names)) {
145 /* Convert list to char ** array */
147 num_names = PyList_Size(py_names);
148 names = (const char **)talloc(
149 hnd->mem_ctx, num_names * sizeof(char *));
151 for (i = 0; i < num_names; i++) {
152 PyObject *obj = PyList_GetItem(py_names, i);
154 names[i] = talloc_strdup(hnd->mem_ctx, PyString_AsString(obj));
159 /* Just a single element */
162 names = (const char **)talloc(hnd->mem_ctx, sizeof(char *));
164 names[0] = PyString_AsString(py_names);
167 ntstatus = cli_lsa_lookup_names(hnd->cli, hnd->mem_ctx, &hnd->pol,
168 num_names, names, &sids, &name_types);
170 if (!NT_STATUS_IS_OK(ntstatus) && NT_STATUS_V(ntstatus) != 0x107) {
171 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
175 result = PyList_New(num_names);
177 for (i = 0; i < num_names; i++) {
178 PyObject *sid_obj, *obj;
180 py_from_SID(&sid_obj, &sids[i]);
182 obj = Py_BuildValue("(Oi)", sid_obj, name_types[i]);
184 PyList_SetItem(result, i, obj);
190 static PyObject *lsa_lookup_sids(PyObject *self, PyObject *args,
193 PyObject *py_sids, *result;
196 char **domains, **names;
198 lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
201 if (!PyArg_ParseTuple(args, "O", &py_sids))
204 if (!PyList_Check(py_sids) && !PyString_Check(py_sids)) {
205 PyErr_SetString(PyExc_TypeError, "must be list or string");
209 if (PyList_Check(py_sids)) {
211 /* Convert dictionary to char ** array */
213 num_sids = PyList_Size(py_sids);
214 sids = (DOM_SID *)talloc(hnd->mem_ctx, num_sids * sizeof(DOM_SID));
216 memset(sids, 0, num_sids * sizeof(DOM_SID));
218 for (i = 0; i < num_sids; i++) {
219 PyObject *obj = PyList_GetItem(py_sids, i);
221 string_to_sid(&sids[i], PyString_AsString(obj));
226 /* Just a single element */
229 sids = (DOM_SID *)talloc(hnd->mem_ctx, sizeof(DOM_SID));
231 string_to_sid(&sids[0], PyString_AsString(py_sids));
234 ntstatus = cli_lsa_lookup_sids(hnd->cli, hnd->mem_ctx, &hnd->pol,
235 num_sids, sids, &domains, &names,
238 if (!NT_STATUS_IS_OK(ntstatus)) {
239 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
243 result = PyList_New(num_sids);
245 for (i = 0; i < num_sids; i++) {
248 obj = Py_BuildValue("{sssssi}", "username", names[i],
249 "domain", domains[i], "name_type",
252 PyList_SetItem(result, i, obj);
258 static PyObject *lsa_enum_trust_dom(PyObject *self, PyObject *args)
260 lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
262 uint32 enum_ctx = 0, num_domains, i;
264 DOM_SID *domain_sids;
267 if (!PyArg_ParseTuple(args, ""))
270 ntstatus = cli_lsa_enum_trust_dom(hnd->cli, hnd->mem_ctx,
271 &hnd->pol, &enum_ctx,
272 &num_domains, &domain_names,
275 if (!NT_STATUS_IS_OK(ntstatus)) {
276 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
280 result = PyList_New(num_domains);
282 for (i = 0; i < num_domains; i++) {
285 sid_to_string(sid_str, &domain_sids[i]);
288 Py_BuildValue("(ss)", domain_names[i], sid_str));
295 * Method dispatch tables
298 static PyMethodDef lsa_hnd_methods[] = {
302 { "lookup_sids", (PyCFunction)lsa_lookup_sids,
303 METH_VARARGS | METH_KEYWORDS,
304 "Convert sids to names." },
306 { "lookup_names", (PyCFunction)lsa_lookup_names,
307 METH_VARARGS | METH_KEYWORDS,
308 "Convert names to sids." },
310 /* Trusted domains */
312 { "enum_trusted_domains", (PyCFunction)lsa_enum_trust_dom,
314 "Enumerate trusted domains." },
319 static void py_lsa_policy_hnd_dealloc(PyObject* self)
324 static PyObject *py_lsa_policy_hnd_getattr(PyObject *self, char *attrname)
326 return Py_FindMethod(lsa_hnd_methods, self, attrname);
329 PyTypeObject lsa_policy_hnd_type = {
330 PyObject_HEAD_INIT(NULL)
333 sizeof(lsa_policy_hnd_object),
335 py_lsa_policy_hnd_dealloc, /*tp_dealloc*/
337 py_lsa_policy_hnd_getattr, /*tp_getattr*/
342 0, /*tp_as_sequence*/
347 static PyMethodDef lsa_methods[] = {
349 /* Open/close lsa handles */
351 { "open_policy", (PyCFunction)lsa_open_policy,
352 METH_VARARGS | METH_KEYWORDS,
353 "Open a policy handle" },
355 { "close", (PyCFunction)lsa_close,
357 "Close a policy handle" },
362 static struct const_vals {
365 } module_const_vals[] = {
369 static void const_init(PyObject *dict)
371 struct const_vals *tmp;
374 for (tmp = module_const_vals; tmp->name; tmp++) {
375 obj = PyInt_FromLong(tmp->value);
376 PyDict_SetItemString(dict, tmp->name, obj);
382 * Module initialisation
387 PyObject *module, *dict;
389 /* Initialise module */
391 module = Py_InitModule("lsa", lsa_methods);
392 dict = PyModule_GetDict(module);
394 lsa_error = PyErr_NewException("lsa.error", NULL, NULL);
395 PyDict_SetItemString(dict, "error", lsa_error);
397 lsa_ntstatus = PyErr_NewException("lsa.ntstatus", NULL, NULL);
398 PyDict_SetItemString(dict, "ntstatus", lsa_ntstatus);
400 /* Initialise policy handle object */
402 lsa_policy_hnd_type.ob_type = &PyType_Type;
404 /* Initialise constants */
408 /* Do samba initialisation */
412 setup_logging("lsa", True);