fixed typo
[Samba/gebeck_regimport.git] / source / python / py_lsa.c
blob21e6463c5f2a0a40c614294d4f8140eb46787956
1 /*
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,
24 POLICY_HND *pol)
26 lsa_policy_hnd_object *o;
28 o = PyObject_New(lsa_policy_hnd_object, &lsa_policy_hnd_type);
30 o->cli = cli;
31 o->mem_ctx = mem_ctx;
32 memcpy(&o->pol, pol, sizeof(POLICY_HND));
34 return (PyObject*)o;
37 /*
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
46 NT_STATUS_OK */
49 * Open/close lsa handles
52 static PyObject *lsa_open_policy(PyObject *self, PyObject *args,
53 PyObject *kw)
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;
60 NTSTATUS ntstatus;
61 TALLOC_CTX *mem_ctx = NULL;
62 POLICY_HND hnd;
64 if (!PyArg_ParseTupleAndKeywords(
65 args, kw, "s|Oi", kwlist, &server, &creds, &desired_access))
66 return NULL;
68 if (creds && creds != Py_None && !PyDict_Check(creds)) {
69 PyErr_SetString(PyExc_TypeError,
70 "credentials must be dictionary or None");
71 return NULL;
74 if (server[0] != '\\' || server[1] != '\\') {
75 PyErr_SetString(PyExc_ValueError, "UNC name required");
76 return NULL;
79 server += 2;
81 if (!(cli = open_pipe_creds(server, creds, PIPE_LSARPC, &errstr))) {
82 PyErr_SetString(lsa_error, errstr);
83 free(errstr);
84 return NULL;
87 if (!(mem_ctx = talloc_init())) {
88 PyErr_SetString(lsa_error, "unable to init talloc context\n");
89 goto done;
92 ntstatus = cli_lsa_open_policy(cli, mem_ctx, True,
93 SEC_RIGHTS_MAXIMUM_ALLOWED, &hnd);
95 if (!NT_STATUS_IS_OK(ntstatus)) {
96 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
97 goto done;
100 result = new_lsa_policy_hnd_object(cli, mem_ctx, &hnd);
102 done:
103 if (!result) {
104 if (cli)
105 cli_shutdown(cli);
107 if (mem_ctx)
108 talloc_destroy(mem_ctx);
111 return result;
114 static PyObject *lsa_close(PyObject *self, PyObject *args, PyObject *kw)
116 PyObject *po;
117 lsa_policy_hnd_object *hnd;
118 NTSTATUS result;
120 /* Parse parameters */
122 if (!PyArg_ParseTuple(args, "O!", &lsa_policy_hnd_type, &po))
123 return NULL;
125 hnd = (lsa_policy_hnd_object *)po;
127 /* Call rpc function */
129 result = cli_lsa_close(hnd->cli, hnd->mem_ctx, &hnd->pol);
131 /* Cleanup samba stuff */
133 cli_shutdown(hnd->cli);
134 talloc_destroy(hnd->mem_ctx);
136 /* Return value */
138 Py_INCREF(Py_None);
139 return Py_None;
142 static PyObject *lsa_lookup_names(PyObject *self, PyObject *args)
144 PyObject *py_names, *result;
145 NTSTATUS ntstatus;
146 lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
147 int num_names, i;
148 const char **names;
149 DOM_SID *sids;
150 uint32 *name_types;
152 if (!PyArg_ParseTuple(args, "O", &py_names))
153 return NULL;
155 if (!PyList_Check(py_names) && !PyString_Check(py_names)) {
156 PyErr_SetString(PyExc_TypeError, "must be list or string");
157 return NULL;
160 if (PyList_Check(py_names)) {
162 /* Convert list to char ** array */
164 num_names = PyList_Size(py_names);
165 names = (const char **)talloc(
166 hnd->mem_ctx, num_names * sizeof(char *));
168 for (i = 0; i < num_names; i++) {
169 PyObject *obj = PyList_GetItem(py_names, i);
171 names[i] = talloc_strdup(hnd->mem_ctx, PyString_AsString(obj));
174 } else {
176 /* Just a single element */
178 num_names = 1;
179 names = (const char **)talloc(hnd->mem_ctx, sizeof(char *));
181 names[0] = PyString_AsString(py_names);
184 ntstatus = cli_lsa_lookup_names(hnd->cli, hnd->mem_ctx, &hnd->pol,
185 num_names, names, &sids, &name_types);
187 if (!NT_STATUS_IS_OK(ntstatus) && NT_STATUS_V(ntstatus) != 0x107) {
188 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
189 return NULL;
192 result = PyList_New(num_names);
194 for (i = 0; i < num_names; i++) {
195 PyObject *sid_obj, *obj;
197 py_from_SID(&sid_obj, &sids[i]);
199 obj = Py_BuildValue("(Oi)", sid_obj, name_types[i]);
201 PyList_SetItem(result, i, obj);
204 return result;
207 static PyObject *lsa_lookup_sids(PyObject *self, PyObject *args,
208 PyObject *kw)
210 PyObject *py_sids, *result;
211 NTSTATUS ntstatus;
212 int num_sids, i;
213 char **domains, **names;
214 uint32 *types;
215 lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
216 DOM_SID *sids;
218 if (!PyArg_ParseTuple(args, "O", &py_sids))
219 return NULL;
221 if (!PyList_Check(py_sids) && !PyString_Check(py_sids)) {
222 PyErr_SetString(PyExc_TypeError, "must be list or string");
223 return NULL;
226 if (PyList_Check(py_sids)) {
228 /* Convert dictionary to char ** array */
230 num_sids = PyList_Size(py_sids);
231 sids = (DOM_SID *)talloc(hnd->mem_ctx, num_sids * sizeof(DOM_SID));
233 memset(sids, 0, num_sids * sizeof(DOM_SID));
235 for (i = 0; i < num_sids; i++) {
236 PyObject *obj = PyList_GetItem(py_sids, i);
238 string_to_sid(&sids[i], PyString_AsString(obj));
241 } else {
243 /* Just a single element */
245 num_sids = 1;
246 sids = (DOM_SID *)talloc(hnd->mem_ctx, sizeof(DOM_SID));
248 string_to_sid(&sids[0], PyString_AsString(py_sids));
251 ntstatus = cli_lsa_lookup_sids(hnd->cli, hnd->mem_ctx, &hnd->pol,
252 num_sids, sids, &domains, &names,
253 &types);
255 if (!NT_STATUS_IS_OK(ntstatus)) {
256 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
257 return NULL;
260 result = PyList_New(num_sids);
262 for (i = 0; i < num_sids; i++) {
263 PyObject *obj;
265 obj = Py_BuildValue("{sssssi}", "username", names[i],
266 "domain", domains[i], "name_type",
267 types[i]);
269 PyList_SetItem(result, i, obj);
272 return result;
275 static PyObject *lsa_enum_trust_dom(PyObject *self, PyObject *args)
277 lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
278 NTSTATUS ntstatus;
279 uint32 enum_ctx = 0, num_domains, i, pref_num_domains = 0;
280 char **domain_names;
281 DOM_SID *domain_sids;
282 PyObject *result;
284 if (!PyArg_ParseTuple(args, ""))
285 return NULL;
287 ntstatus = cli_lsa_enum_trust_dom(
288 hnd->cli, hnd->mem_ctx, &hnd->pol, &enum_ctx,
289 &pref_num_domains, &num_domains, &domain_names, &domain_sids);
291 if (!NT_STATUS_IS_OK(ntstatus)) {
292 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
293 return NULL;
296 result = PyList_New(num_domains);
298 for (i = 0; i < num_domains; i++) {
299 fstring sid_str;
301 sid_to_string(sid_str, &domain_sids[i]);
302 PyList_SetItem(
303 result, i,
304 Py_BuildValue("(ss)", domain_names[i], sid_str));
307 return result;
311 * Method dispatch tables
314 static PyMethodDef lsa_hnd_methods[] = {
316 /* SIDs<->names */
318 { "lookup_sids", (PyCFunction)lsa_lookup_sids,
319 METH_VARARGS | METH_KEYWORDS,
320 "Convert sids to names." },
322 { "lookup_names", (PyCFunction)lsa_lookup_names,
323 METH_VARARGS | METH_KEYWORDS,
324 "Convert names to sids." },
326 /* Trusted domains */
328 { "enum_trusted_domains", (PyCFunction)lsa_enum_trust_dom,
329 METH_VARARGS,
330 "Enumerate trusted domains." },
332 { NULL }
335 static void py_lsa_policy_hnd_dealloc(PyObject* self)
337 PyObject_Del(self);
340 static PyObject *py_lsa_policy_hnd_getattr(PyObject *self, char *attrname)
342 return Py_FindMethod(lsa_hnd_methods, self, attrname);
345 PyTypeObject lsa_policy_hnd_type = {
346 PyObject_HEAD_INIT(NULL)
348 "LSA Policy Handle",
349 sizeof(lsa_policy_hnd_object),
351 py_lsa_policy_hnd_dealloc, /*tp_dealloc*/
352 0, /*tp_print*/
353 py_lsa_policy_hnd_getattr, /*tp_getattr*/
354 0, /*tp_setattr*/
355 0, /*tp_compare*/
356 0, /*tp_repr*/
357 0, /*tp_as_number*/
358 0, /*tp_as_sequence*/
359 0, /*tp_as_mapping*/
360 0, /*tp_hash */
363 static PyMethodDef lsa_methods[] = {
365 /* Open/close lsa handles */
367 { "open_policy", (PyCFunction)lsa_open_policy,
368 METH_VARARGS | METH_KEYWORDS,
369 "Open a policy handle" },
371 { "close", (PyCFunction)lsa_close,
372 METH_VARARGS,
373 "Close a policy handle" },
375 /* Other stuff - this should really go into a samba config module
376 but for the moment let's leave it here. */
378 { "setup_logging", (PyCFunction)py_setup_logging,
379 METH_VARARGS | METH_KEYWORDS,
380 "Set up debug logging.
382 Initialises Samba's debug logging system. One argument is expected which
383 is a boolean specifying whether debugging is interactive and sent to stdout
384 or logged to a file.
386 Example:
388 >>> spoolss.setup_logging(interactive = 1)" },
390 { "get_debuglevel", (PyCFunction)get_debuglevel,
391 METH_VARARGS,
392 "Set the current debug level.
394 Example:
396 >>> spoolss.get_debuglevel()
397 0" },
399 { "set_debuglevel", (PyCFunction)set_debuglevel,
400 METH_VARARGS,
401 "Get the current debug level.
403 Example:
405 >>> spoolss.set_debuglevel(10)" },
407 { NULL }
410 static struct const_vals {
411 char *name;
412 uint32 value;
413 } module_const_vals[] = {
414 { NULL }
417 static void const_init(PyObject *dict)
419 struct const_vals *tmp;
420 PyObject *obj;
422 for (tmp = module_const_vals; tmp->name; tmp++) {
423 obj = PyInt_FromLong(tmp->value);
424 PyDict_SetItemString(dict, tmp->name, obj);
425 Py_DECREF(obj);
430 * Module initialisation
433 void initlsa(void)
435 PyObject *module, *dict;
437 /* Initialise module */
439 module = Py_InitModule("lsa", lsa_methods);
440 dict = PyModule_GetDict(module);
442 lsa_error = PyErr_NewException("lsa.error", NULL, NULL);
443 PyDict_SetItemString(dict, "error", lsa_error);
445 lsa_ntstatus = PyErr_NewException("lsa.ntstatus", NULL, NULL);
446 PyDict_SetItemString(dict, "ntstatus", lsa_ntstatus);
448 /* Initialise policy handle object */
450 lsa_policy_hnd_type.ob_type = &PyType_Type;
452 /* Initialise constants */
454 const_init(dict);
456 /* Do samba initialisation */
458 py_samba_init();
460 setup_logging("lsa", True);
461 DEBUGLEVEL = 10;