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.
24 #include "python/py_lsa.h"
26 PyObject *new_lsa_policy_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
29 lsa_policy_hnd_object *o;
31 o = PyObject_New(lsa_policy_hnd_object, &lsa_policy_hnd_type);
35 memcpy(&o->pol, pol, sizeof(POLICY_HND));
41 * Exceptions raised by this module
44 PyObject *lsa_error; /* This indicates a non-RPC related error
45 such as name lookup failure */
47 PyObject *lsa_ntstatus; /* This exception is raised when a RPC call
48 returns a status code other than
52 * Open/close lsa handles
55 static PyObject *lsa_open_policy(PyObject *self, PyObject *args,
58 static char *kwlist[] = { "servername", "creds", "access", NULL };
60 PyObject *creds = NULL, *result;
61 uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
62 struct cli_state *cli;
67 if (!PyArg_ParseTupleAndKeywords(
68 args, kw, "s|O!i", kwlist, &server_name, &PyDict_Type,
69 &creds, &desired_access))
72 if (!(cli = open_pipe_creds(server_name, creds, cli_lsa_initialise,
74 fprintf(stderr, "could not initialise cli state\n");
78 if (!(mem_ctx = talloc_init())) {
79 fprintf(stderr, "unable to initialise talloc context\n");
83 ntstatus = cli_lsa_open_policy(cli, mem_ctx, True,
84 SEC_RIGHTS_MAXIMUM_ALLOWED, &hnd);
86 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);
98 static PyObject *lsa_close(PyObject *self, PyObject *args, PyObject *kw)
101 lsa_policy_hnd_object *hnd;
104 /* Parse parameters */
106 if (!PyArg_ParseTuple(args, "O!", &lsa_policy_hnd_type, &po))
109 hnd = (lsa_policy_hnd_object *)po;
111 /* Call rpc function */
113 result = cli_lsa_close(hnd->cli, hnd->mem_ctx, &hnd->pol);
115 /* Cleanup samba stuff */
117 cli_shutdown(hnd->cli);
118 talloc_destroy(hnd->mem_ctx);
126 static PyObject *lsa_lookup_names(PyObject *self, PyObject *args)
128 PyObject *py_names, *result;
130 lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
136 if (!PyArg_ParseTuple(args, "O!", &PyList_Type, &py_names))
139 /* Convert dictionary to char ** array */
141 num_names = PyList_Size(py_names);
142 names = (const char **)talloc(
143 hnd->mem_ctx, num_names * sizeof(char *));
145 for (i = 0; i < num_names; i++) {
146 PyObject *obj = PyList_GetItem(py_names, i);
148 names[i] = talloc_strdup(hnd->mem_ctx, PyString_AsString(obj));
151 ntstatus = cli_lsa_lookup_names(hnd->cli, hnd->mem_ctx, &hnd->pol,
152 num_names, names, &sids, &name_types);
154 if (!NT_STATUS_IS_OK(ntstatus) && NT_STATUS_V(ntstatus) != 0x107) {
155 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
159 result = PyList_New(num_names);
161 for (i = 0; i < num_names; i++) {
162 PyObject *sid_obj, *obj;
164 py_from_SID(&sid_obj, &sids[i]);
166 obj = Py_BuildValue("(Oi)", sid_obj, name_types[i]);
168 PyList_SetItem(result, i, obj);
174 static PyObject *lsa_lookup_sids(PyObject *self, PyObject *args,
177 PyObject *py_sids, *result;
180 char **domains, **names;
182 lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
185 if (!PyArg_ParseTuple(args, "O!", &PyList_Type, &py_sids))
188 /* Convert dictionary to char ** array */
190 num_sids = PyList_Size(py_sids);
191 sids = (DOM_SID *)talloc(hnd->mem_ctx, num_sids * sizeof(DOM_SID));
193 memset(sids, 0, num_sids * sizeof(DOM_SID));
195 for (i = 0; i < num_sids; i++) {
196 PyObject *obj = PyList_GetItem(py_sids, i);
198 string_to_sid(&sids[i], PyString_AsString(obj));
201 ntstatus = cli_lsa_lookup_sids(hnd->cli, hnd->mem_ctx, &hnd->pol,
202 num_sids, sids, &domains, &names,
205 if (!NT_STATUS_IS_OK(ntstatus)) {
206 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
210 result = PyList_New(num_sids);
212 for (i = 0; i < num_sids; i++) {
213 PyObject *name_obj, *obj;
215 obj = Py_BuildValue("{sssssi}", "username", names[i],
216 "domain", domains[i], "name_type",
219 PyList_SetItem(result, i, obj);
226 * Method dispatch tables
229 static PyMethodDef lsa_hnd_methods[] = {
231 { "lookup_sids", lsa_lookup_sids, METH_VARARGS | METH_KEYWORDS,
232 "Convert sids to names." },
234 { "lookup_names", lsa_lookup_names, METH_VARARGS | METH_KEYWORDS,
235 "Convert names to sids." },
240 static void py_lsa_policy_hnd_dealloc(PyObject* self)
245 static PyObject *py_lsa_policy_hnd_getattr(PyObject *self, char *attrname)
247 return Py_FindMethod(lsa_hnd_methods, self, attrname);
250 PyTypeObject lsa_policy_hnd_type = {
251 PyObject_HEAD_INIT(NULL)
254 sizeof(lsa_policy_hnd_object),
256 py_lsa_policy_hnd_dealloc, /*tp_dealloc*/
258 py_lsa_policy_hnd_getattr, /*tp_getattr*/
263 0, /*tp_as_sequence*/
268 static PyMethodDef lsa_methods[] = {
270 /* Open/close lsa handles */
272 { "open_policy", lsa_open_policy, METH_VARARGS | METH_KEYWORDS,
273 "Open a policy handle" },
275 { "close", lsa_close, METH_VARARGS, "Close a policy handle" },
281 * Module initialisation
286 PyObject *module, *dict;
288 /* Initialise module */
290 module = Py_InitModule("lsa", lsa_methods);
291 dict = PyModule_GetDict(module);
293 lsa_error = PyErr_NewException("lsa.error", NULL, NULL);
294 PyDict_SetItemString(dict, "error", lsa_error);
296 lsa_ntstatus = PyErr_NewException("lsa.ntstatus", NULL, NULL);
297 PyDict_SetItemString(dict, "ntstatus", lsa_ntstatus);
299 /* Initialise policy handle object */
301 lsa_policy_hnd_type.ob_type = &PyType_Type;
303 /* Initialise constants */
307 /* Do samba initialisation */
311 setup_logging("lsa", True);