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_samr.h"
24 * Exceptions raised by this module
27 PyObject *samr_error; /* This indicates a non-RPC related error
28 such as name lookup failure */
30 PyObject *samr_ntstatus; /* This exception is raised when a RPC call
31 returns a status code other than
34 /* SAMR connect handle object */
36 static void py_samr_connect_hnd_dealloc(PyObject* self)
41 PyObject *new_samr_domain_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
44 samr_domain_hnd_object *o;
46 o = PyObject_New(samr_domain_hnd_object, &samr_domain_hnd_type);
50 memcpy(&o->domain_pol, pol, sizeof(POLICY_HND));
55 static PyObject *samr_open_domain(PyObject *self, PyObject *args, PyObject *kw)
57 samr_connect_hnd_object *connect_hnd = (samr_connect_hnd_object *)self;
58 static char *kwlist[] = { "sid", "access", NULL };
59 uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
62 TALLOC_CTX *mem_ctx = NULL;
63 POLICY_HND domain_pol;
65 PyObject *result = NULL;
67 if (!PyArg_ParseTupleAndKeywords(
68 args, kw, "s|i", kwlist, &sid_str, &desired_access))
71 if (!string_to_sid(&sid, sid_str)) {
72 PyErr_SetString(PyExc_TypeError, "string is not a sid");
76 if (!(mem_ctx = talloc_init())) {
77 PyErr_SetString(samr_error, "unable to init talloc context");
81 ntstatus = cli_samr_open_domain(
82 connect_hnd->cli, mem_ctx, &connect_hnd->connect_pol,
83 desired_access, &sid, &domain_pol);
85 if (!NT_STATUS_IS_OK(ntstatus)) {
86 PyErr_SetObject(samr_ntstatus, py_ntstatus_tuple(ntstatus));
90 result = new_samr_domain_hnd_object(
91 connect_hnd->cli, mem_ctx, &domain_pol);
96 talloc_destroy(mem_ctx);
102 static PyMethodDef samr_connect_methods[] = {
103 { "open_domain", (PyCFunction)samr_open_domain,
104 METH_VARARGS | METH_KEYWORDS,
105 "Open a handle on a domain" },
110 static PyObject *py_samr_connect_hnd_getattr(PyObject *self, char *attrname)
112 return Py_FindMethod(samr_connect_methods, self, attrname);
115 PyTypeObject samr_connect_hnd_type = {
116 PyObject_HEAD_INIT(NULL)
118 "SAMR Connect Handle",
119 sizeof(samr_connect_hnd_object),
121 py_samr_connect_hnd_dealloc, /*tp_dealloc*/
123 py_samr_connect_hnd_getattr, /*tp_getattr*/
128 0, /*tp_as_sequence*/
133 PyObject *new_samr_connect_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
136 samr_connect_hnd_object *o;
138 o = PyObject_New(samr_connect_hnd_object, &samr_connect_hnd_type);
141 o->mem_ctx = mem_ctx;
142 memcpy(&o->connect_pol, pol, sizeof(POLICY_HND));
147 /* SAMR domain handle object */
149 static void py_samr_domain_hnd_dealloc(PyObject* self)
154 static PyObject *samr_enum_dom_groups(PyObject *self, PyObject *args,
157 samr_domain_hnd_object *domain_hnd = (samr_domain_hnd_object *)self;
158 static char *kwlist[] = { NULL };
160 uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
161 uint32 start_idx, size, num_dom_groups;
162 struct acct_info *dom_groups;
164 PyObject *py_result = NULL;
166 if (!PyArg_ParseTupleAndKeywords(
167 args, kw, "", kwlist))
170 if (!(mem_ctx = talloc_init())) {
171 PyErr_SetString(samr_error, "unable to init talloc context");
179 result = cli_samr_enum_dom_groups(
180 domain_hnd->cli, mem_ctx, &domain_hnd->domain_pol,
181 &start_idx, size, &dom_groups, &num_dom_groups);
183 if (NT_STATUS_IS_OK(result) ||
184 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
185 py_from_acct_info(&py_result, dom_groups,
189 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
194 static PyMethodDef samr_domain_methods[] = {
195 { "enum_domain_groups", (PyCFunction)samr_enum_dom_groups,
196 METH_VARARGS | METH_KEYWORDS, "Enumerate domain groups" },
200 static PyObject *py_samr_domain_hnd_getattr(PyObject *self, char *attrname)
202 return Py_FindMethod(samr_domain_methods, self, attrname);
205 PyTypeObject samr_domain_hnd_type = {
206 PyObject_HEAD_INIT(NULL)
208 "SAMR Domain Handle",
209 sizeof(samr_domain_hnd_object),
211 py_samr_domain_hnd_dealloc, /*tp_dealloc*/
213 py_samr_domain_hnd_getattr, /*tp_getattr*/
218 0, /*tp_as_sequence*/
223 /* SAMR user handle object */
225 static void py_samr_user_hnd_dealloc(PyObject* self)
230 static PyMethodDef samr_user_methods[] = {
234 static PyObject *py_samr_user_hnd_getattr(PyObject *self, char *attrname)
236 return Py_FindMethod(samr_user_methods, self, attrname);
239 PyTypeObject samr_user_hnd_type = {
240 PyObject_HEAD_INIT(NULL)
243 sizeof(samr_user_hnd_object),
245 py_samr_user_hnd_dealloc, /*tp_dealloc*/
247 py_samr_user_hnd_getattr, /*tp_getattr*/
252 0, /*tp_as_sequence*/
257 PyObject *new_samr_user_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
260 samr_user_hnd_object *o;
262 o = PyObject_New(samr_user_hnd_object, &samr_user_hnd_type);
265 o->mem_ctx = mem_ctx;
266 memcpy(&o->user_pol, pol, sizeof(POLICY_HND));
271 /* SAMR group handle object */
273 static void py_samr_group_hnd_dealloc(PyObject* self)
278 static PyMethodDef samr_group_methods[] = {
282 static PyObject *py_samr_group_hnd_getattr(PyObject *self, char *attrname)
284 return Py_FindMethod(samr_group_methods, self, attrname);
287 PyTypeObject samr_group_hnd_type = {
288 PyObject_HEAD_INIT(NULL)
291 sizeof(samr_group_hnd_object),
293 py_samr_group_hnd_dealloc, /*tp_dealloc*/
295 py_samr_group_hnd_getattr, /*tp_getattr*/
300 0, /*tp_as_sequence*/
305 PyObject *new_samr_group_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
308 samr_group_hnd_object *o;
310 o = PyObject_New(samr_group_hnd_object, &samr_group_hnd_type);
313 o->mem_ctx = mem_ctx;
314 memcpy(&o->group_pol, pol, sizeof(POLICY_HND));
319 /* Alias handle object */
321 static void py_samr_alias_hnd_dealloc(PyObject* self)
326 static PyMethodDef samr_alias_methods[] = {
330 static PyObject *py_samr_alias_hnd_getattr(PyObject *self, char *attrname)
332 return Py_FindMethod(samr_alias_methods, self, attrname);
335 PyTypeObject samr_alias_hnd_type = {
336 PyObject_HEAD_INIT(NULL)
339 sizeof(samr_alias_hnd_object),
341 py_samr_alias_hnd_dealloc, /*tp_dealloc*/
343 py_samr_alias_hnd_getattr, /*tp_getattr*/
348 0, /*tp_as_sequence*/
353 PyObject *new_samr_alias_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
356 samr_alias_hnd_object *o;
358 o = PyObject_New(samr_alias_hnd_object, &samr_alias_hnd_type);
361 o->mem_ctx = mem_ctx;
362 memcpy(&o->alias_pol, pol, sizeof(POLICY_HND));
367 static PyObject *samr_connect(PyObject *self, PyObject *args, PyObject *kw)
369 static char *kwlist[] = { "server", "creds", "access", NULL };
370 uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
371 char *server, *errstr;
372 struct cli_state *cli = NULL;
374 TALLOC_CTX *mem_ctx = NULL;
375 PyObject *result = NULL, *creds = NULL;
378 if (!PyArg_ParseTupleAndKeywords(
379 args, kw, "s|Oi", kwlist, &server, &creds,
383 if (server[0] != '\\' || server[1] != '\\') {
384 PyErr_SetString(PyExc_ValueError, "UNC name required");
390 if (creds && creds != Py_None && !PyDict_Check(creds)) {
391 PyErr_SetString(PyExc_TypeError,
392 "credentials must be dictionary or None");
396 if (!(cli = open_pipe_creds(server, creds, PIPE_SAMR, &errstr))) {
397 PyErr_SetString(samr_error, errstr);
402 if (!(mem_ctx = talloc_init())) {
403 PyErr_SetString(samr_ntstatus,
404 "unable to init talloc context\n");
408 ntstatus = cli_samr_connect(cli, mem_ctx, desired_access, &hnd);
410 if (!NT_STATUS_IS_OK(ntstatus)) {
413 PyErr_SetObject(samr_ntstatus, py_ntstatus_tuple(ntstatus));
417 result = new_samr_connect_hnd_object(cli, mem_ctx, &hnd);
425 talloc_destroy(mem_ctx);
432 * Module initialisation
435 static PyMethodDef samr_methods[] = {
437 /* Open/close samr connect handles */
439 { "connect", (PyCFunction)samr_connect,
440 METH_VARARGS | METH_KEYWORDS,
441 "Open a connect handle" },
446 static struct const_vals {
449 } module_const_vals[] = {
453 static void const_init(PyObject *dict)
455 struct const_vals *tmp;
458 for (tmp = module_const_vals; tmp->name; tmp++) {
459 obj = PyInt_FromLong(tmp->value);
460 PyDict_SetItemString(dict, tmp->name, obj);
467 PyObject *module, *dict;
469 /* Initialise module */
471 module = Py_InitModule("samr", samr_methods);
472 dict = PyModule_GetDict(module);
474 samr_error = PyErr_NewException("samr.error", NULL, NULL);
475 PyDict_SetItemString(dict, "error", samr_error);
477 samr_ntstatus = PyErr_NewException("samr.ntstatus", NULL, NULL);
478 PyDict_SetItemString(dict, "ntstatus", samr_ntstatus);
480 /* Initialise policy handle object */
482 samr_connect_hnd_type.ob_type = &PyType_Type;
483 samr_domain_hnd_type.ob_type = &PyType_Type;
484 samr_user_hnd_type.ob_type = &PyType_Type;
485 samr_group_hnd_type.ob_type = &PyType_Type;
486 samr_alias_hnd_type.ob_type = &PyType_Type;
488 /* Initialise constants */
492 /* Do samba initialisation */
496 setup_logging("samr", True);