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 static PyMethodDef samr_connect_methods[] = {
45 static PyObject *py_samr_connect_hnd_getattr(PyObject *self, char *attrname)
47 return Py_FindMethod(samr_connect_methods, self, attrname);
50 PyTypeObject samr_connect_hnd_type = {
51 PyObject_HEAD_INIT(NULL)
53 "SAMR Connect Handle",
54 sizeof(samr_connect_hnd_object),
56 py_samr_connect_hnd_dealloc, /*tp_dealloc*/
58 py_samr_connect_hnd_getattr, /*tp_getattr*/
68 PyObject *new_samr_connect_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
71 samr_connect_hnd_object *o;
73 o = PyObject_New(samr_connect_hnd_object, &samr_connect_hnd_type);
77 memcpy(&o->pol, pol, sizeof(POLICY_HND));
82 /* SAMR domain handle object */
84 static void py_samr_domain_hnd_dealloc(PyObject* self)
89 static PyMethodDef samr_domain_methods[] = {
93 static PyObject *py_samr_domain_hnd_getattr(PyObject *self, char *attrname)
95 return Py_FindMethod(samr_domain_methods, self, attrname);
98 PyTypeObject samr_domain_hnd_type = {
99 PyObject_HEAD_INIT(NULL)
101 "SAMR Domain Handle",
102 sizeof(samr_domain_hnd_object),
104 py_samr_domain_hnd_dealloc, /*tp_dealloc*/
106 py_samr_domain_hnd_getattr, /*tp_getattr*/
111 0, /*tp_as_sequence*/
116 PyObject *new_samr_domain_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
119 samr_domain_hnd_object *o;
121 o = PyObject_New(samr_domain_hnd_object, &samr_domain_hnd_type);
124 o->mem_ctx = mem_ctx;
125 memcpy(&o->pol, pol, sizeof(POLICY_HND));
130 /* SAMR user handle object */
132 static void py_samr_user_hnd_dealloc(PyObject* self)
137 static PyMethodDef samr_user_methods[] = {
141 static PyObject *py_samr_user_hnd_getattr(PyObject *self, char *attrname)
143 return Py_FindMethod(samr_user_methods, self, attrname);
146 PyTypeObject samr_user_hnd_type = {
147 PyObject_HEAD_INIT(NULL)
150 sizeof(samr_user_hnd_object),
152 py_samr_user_hnd_dealloc, /*tp_dealloc*/
154 py_samr_user_hnd_getattr, /*tp_getattr*/
159 0, /*tp_as_sequence*/
164 PyObject *new_samr_user_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
167 samr_user_hnd_object *o;
169 o = PyObject_New(samr_user_hnd_object, &samr_user_hnd_type);
172 o->mem_ctx = mem_ctx;
173 memcpy(&o->pol, pol, sizeof(POLICY_HND));
178 /* SAMR group handle object */
180 static void py_samr_group_hnd_dealloc(PyObject* self)
185 static PyMethodDef samr_group_methods[] = {
189 static PyObject *py_samr_group_hnd_getattr(PyObject *self, char *attrname)
191 return Py_FindMethod(samr_group_methods, self, attrname);
194 PyTypeObject samr_group_hnd_type = {
195 PyObject_HEAD_INIT(NULL)
198 sizeof(samr_group_hnd_object),
200 py_samr_group_hnd_dealloc, /*tp_dealloc*/
202 py_samr_group_hnd_getattr, /*tp_getattr*/
207 0, /*tp_as_sequence*/
212 PyObject *new_samr_group_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
215 samr_group_hnd_object *o;
217 o = PyObject_New(samr_group_hnd_object, &samr_group_hnd_type);
220 o->mem_ctx = mem_ctx;
221 memcpy(&o->pol, pol, sizeof(POLICY_HND));
226 /* Alias handle object */
228 static void py_samr_alias_hnd_dealloc(PyObject* self)
233 static PyMethodDef samr_alias_methods[] = {
237 static PyObject *py_samr_alias_hnd_getattr(PyObject *self, char *attrname)
239 return Py_FindMethod(samr_alias_methods, self, attrname);
242 PyTypeObject samr_alias_hnd_type = {
243 PyObject_HEAD_INIT(NULL)
246 sizeof(samr_alias_hnd_object),
248 py_samr_alias_hnd_dealloc, /*tp_dealloc*/
250 py_samr_alias_hnd_getattr, /*tp_getattr*/
255 0, /*tp_as_sequence*/
260 PyObject *new_samr_alias_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
263 samr_alias_hnd_object *o;
265 o = PyObject_New(samr_alias_hnd_object, &samr_alias_hnd_type);
268 o->mem_ctx = mem_ctx;
269 memcpy(&o->pol, pol, sizeof(POLICY_HND));
274 static PyObject *samr_connect(PyObject *self, PyObject *args, PyObject *kw)
276 static char *kwlist[] = { "server", "creds", "access", NULL };
277 uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
279 struct cli_state *cli;
282 PyObject *result = NULL, *creds = NULL;
285 if (!PyArg_ParseTupleAndKeywords(
286 args, kw, "s|O!i", kwlist, &server_name, &PyDict_Type,
287 &creds, &desired_access))
290 if (!(cli = open_pipe_creds(server_name, creds, cli_samr_initialise,
293 /* Error state set in open_pipe_creds() */
298 if (!(mem_ctx = talloc_init())) {
299 PyErr_SetString(samr_ntstatus,
300 "unable to initialise talloc context\n");
304 ntstatus = cli_samr_connect(cli, mem_ctx, desired_access, &hnd);
306 if (!NT_STATUS_IS_OK(ntstatus)) {
309 PyErr_SetObject(samr_ntstatus, py_ntstatus_tuple(ntstatus));
313 result = new_samr_connect_hnd_object(cli, mem_ctx, &hnd);
320 * Module initialisation
323 static PyMethodDef samr_methods[] = {
325 /* Open/close samr connect handles */
327 { "connect", (PyCFunction)samr_connect,
328 METH_VARARGS | METH_KEYWORDS,
329 "Open a connect handle" },
334 static struct const_vals {
337 } module_const_vals[] = {
341 static void const_init(PyObject *dict)
343 struct const_vals *tmp;
346 for (tmp = module_const_vals; tmp->name; tmp++) {
347 obj = PyInt_FromLong(tmp->value);
348 PyDict_SetItemString(dict, tmp->name, obj);
355 PyObject *module, *dict;
357 /* Initialise module */
359 module = Py_InitModule("samr", samr_methods);
360 dict = PyModule_GetDict(module);
362 samr_error = PyErr_NewException("samr.error", NULL, NULL);
363 PyDict_SetItemString(dict, "error", samr_error);
365 samr_ntstatus = PyErr_NewException("samr.ntstatus", NULL, NULL);
366 PyDict_SetItemString(dict, "ntstatus", samr_ntstatus);
368 /* Initialise policy handle object */
370 samr_connect_hnd_type.ob_type = &PyType_Type;
371 samr_domain_hnd_type.ob_type = &PyType_Type;
372 samr_user_hnd_type.ob_type = &PyType_Type;
373 samr_group_hnd_type.ob_type = &PyType_Type;
374 samr_alias_hnd_type.ob_type = &PyType_Type;
376 /* Initialise constants */
380 /* Do samba initialisation */
384 setup_logging("samr", True);