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|O!i", kwlist, &server, &PyDict_Type,
66 &creds, &desired_access))
69 if (!(cli = open_pipe_creds(server, creds, PIPE_LSARPC, &errstr))) {
70 PyErr_SetString(lsa_error, errstr);
75 if (!(mem_ctx = talloc_init())) {
76 PyErr_SetString(lsa_error, "unable to init talloc context\n");
80 ntstatus = cli_lsa_open_policy(cli, mem_ctx, True,
81 SEC_RIGHTS_MAXIMUM_ALLOWED, &hnd);
83 if (!NT_STATUS_IS_OK(ntstatus)) {
84 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
88 result = new_lsa_policy_hnd_object(cli, mem_ctx, &hnd);
96 talloc_destroy(mem_ctx);
102 static PyObject *lsa_close(PyObject *self, PyObject *args, PyObject *kw)
105 lsa_policy_hnd_object *hnd;
108 /* Parse parameters */
110 if (!PyArg_ParseTuple(args, "O!", &lsa_policy_hnd_type, &po))
113 hnd = (lsa_policy_hnd_object *)po;
115 /* Call rpc function */
117 result = cli_lsa_close(hnd->cli, hnd->mem_ctx, &hnd->pol);
119 /* Cleanup samba stuff */
121 cli_shutdown(hnd->cli);
122 talloc_destroy(hnd->mem_ctx);
130 static PyObject *lsa_lookup_names(PyObject *self, PyObject *args)
132 PyObject *py_names, *result;
134 lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
140 if (!PyArg_ParseTuple(args, "O", &py_names))
143 if (!PyList_Check(py_names) && !PyString_Check(py_names)) {
144 PyErr_SetString(PyExc_TypeError, "must be list or string");
148 if (PyList_Check(py_names)) {
150 /* Convert list to char ** array */
152 num_names = PyList_Size(py_names);
153 names = (const char **)talloc(
154 hnd->mem_ctx, num_names * sizeof(char *));
156 for (i = 0; i < num_names; i++) {
157 PyObject *obj = PyList_GetItem(py_names, i);
159 names[i] = talloc_strdup(hnd->mem_ctx, PyString_AsString(obj));
164 /* Just a single element */
167 names = (const char **)talloc(hnd->mem_ctx, sizeof(char *));
169 names[0] = PyString_AsString(py_names);
172 ntstatus = cli_lsa_lookup_names(hnd->cli, hnd->mem_ctx, &hnd->pol,
173 num_names, names, &sids, &name_types);
175 if (!NT_STATUS_IS_OK(ntstatus) && NT_STATUS_V(ntstatus) != 0x107) {
176 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
180 result = PyList_New(num_names);
182 for (i = 0; i < num_names; i++) {
183 PyObject *sid_obj, *obj;
185 py_from_SID(&sid_obj, &sids[i]);
187 obj = Py_BuildValue("(Oi)", sid_obj, name_types[i]);
189 PyList_SetItem(result, i, obj);
195 static PyObject *lsa_lookup_sids(PyObject *self, PyObject *args,
198 PyObject *py_sids, *result;
201 char **domains, **names;
203 lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
206 if (!PyArg_ParseTuple(args, "O", &py_sids))
209 if (!PyList_Check(py_sids) && !PyString_Check(py_sids)) {
210 PyErr_SetString(PyExc_TypeError, "must be list or string");
214 if (PyList_Check(py_sids)) {
216 /* Convert dictionary to char ** array */
218 num_sids = PyList_Size(py_sids);
219 sids = (DOM_SID *)talloc(hnd->mem_ctx, num_sids * sizeof(DOM_SID));
221 memset(sids, 0, num_sids * sizeof(DOM_SID));
223 for (i = 0; i < num_sids; i++) {
224 PyObject *obj = PyList_GetItem(py_sids, i);
226 string_to_sid(&sids[i], PyString_AsString(obj));
231 /* Just a single element */
234 sids = (DOM_SID *)talloc(hnd->mem_ctx, sizeof(DOM_SID));
236 string_to_sid(&sids[0], PyString_AsString(py_sids));
239 ntstatus = cli_lsa_lookup_sids(hnd->cli, hnd->mem_ctx, &hnd->pol,
240 num_sids, sids, &domains, &names,
243 if (!NT_STATUS_IS_OK(ntstatus)) {
244 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
248 result = PyList_New(num_sids);
250 for (i = 0; i < num_sids; i++) {
253 obj = Py_BuildValue("{sssssi}", "username", names[i],
254 "domain", domains[i], "name_type",
257 PyList_SetItem(result, i, obj);
263 static PyObject *lsa_enum_trust_dom(PyObject *self, PyObject *args)
265 lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
267 uint32 enum_ctx = 0, num_domains, i, pref_num_domains = 0;
269 DOM_SID *domain_sids;
272 if (!PyArg_ParseTuple(args, ""))
275 ntstatus = cli_lsa_enum_trust_dom(
276 hnd->cli, hnd->mem_ctx, &hnd->pol, &enum_ctx,
277 &pref_num_domains, &num_domains, &domain_names, &domain_sids);
279 if (!NT_STATUS_IS_OK(ntstatus)) {
280 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
284 result = PyList_New(num_domains);
286 for (i = 0; i < num_domains; i++) {
289 sid_to_string(sid_str, &domain_sids[i]);
292 Py_BuildValue("(ss)", domain_names[i], sid_str));
299 * Method dispatch tables
302 static PyMethodDef lsa_hnd_methods[] = {
306 { "lookup_sids", (PyCFunction)lsa_lookup_sids,
307 METH_VARARGS | METH_KEYWORDS,
308 "Convert sids to names." },
310 { "lookup_names", (PyCFunction)lsa_lookup_names,
311 METH_VARARGS | METH_KEYWORDS,
312 "Convert names to sids." },
314 /* Trusted domains */
316 { "enum_trusted_domains", (PyCFunction)lsa_enum_trust_dom,
318 "Enumerate trusted domains." },
323 static void py_lsa_policy_hnd_dealloc(PyObject* self)
328 static PyObject *py_lsa_policy_hnd_getattr(PyObject *self, char *attrname)
330 return Py_FindMethod(lsa_hnd_methods, self, attrname);
333 PyTypeObject lsa_policy_hnd_type = {
334 PyObject_HEAD_INIT(NULL)
337 sizeof(lsa_policy_hnd_object),
339 py_lsa_policy_hnd_dealloc, /*tp_dealloc*/
341 py_lsa_policy_hnd_getattr, /*tp_getattr*/
346 0, /*tp_as_sequence*/
351 static PyMethodDef lsa_methods[] = {
353 /* Open/close lsa handles */
355 { "open_policy", (PyCFunction)lsa_open_policy,
356 METH_VARARGS | METH_KEYWORDS,
357 "Open a policy handle" },
359 { "close", (PyCFunction)lsa_close,
361 "Close a policy handle" },
366 static struct const_vals {
369 } module_const_vals[] = {
373 static void const_init(PyObject *dict)
375 struct const_vals *tmp;
378 for (tmp = module_const_vals; tmp->name; tmp++) {
379 obj = PyInt_FromLong(tmp->value);
380 PyDict_SetItemString(dict, tmp->name, obj);
386 * Module initialisation
391 PyObject *module, *dict;
393 /* Initialise module */
395 module = Py_InitModule("lsa", lsa_methods);
396 dict = PyModule_GetDict(module);
398 lsa_error = PyErr_NewException("lsa.error", NULL, NULL);
399 PyDict_SetItemString(dict, "error", lsa_error);
401 lsa_ntstatus = PyErr_NewException("lsa.ntstatus", NULL, NULL);
402 PyDict_SetItemString(dict, "ntstatus", lsa_ntstatus);
404 /* Initialise policy handle object */
406 lsa_policy_hnd_type.ob_type = &PyType_Type;
408 /* Initialise constants */
412 /* Do samba initialisation */
416 setup_logging("lsa", True);