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 PyMethodDef samr_domain_methods
[] = {
158 static PyObject
*py_samr_domain_hnd_getattr(PyObject
*self
, char *attrname
)
160 return Py_FindMethod(samr_domain_methods
, self
, attrname
);
163 PyTypeObject samr_domain_hnd_type
= {
164 PyObject_HEAD_INIT(NULL
)
166 "SAMR Domain Handle",
167 sizeof(samr_domain_hnd_object
),
169 py_samr_domain_hnd_dealloc
, /*tp_dealloc*/
171 py_samr_domain_hnd_getattr
, /*tp_getattr*/
176 0, /*tp_as_sequence*/
181 /* SAMR user handle object */
183 static void py_samr_user_hnd_dealloc(PyObject
* self
)
188 static PyMethodDef samr_user_methods
[] = {
192 static PyObject
*py_samr_user_hnd_getattr(PyObject
*self
, char *attrname
)
194 return Py_FindMethod(samr_user_methods
, self
, attrname
);
197 PyTypeObject samr_user_hnd_type
= {
198 PyObject_HEAD_INIT(NULL
)
201 sizeof(samr_user_hnd_object
),
203 py_samr_user_hnd_dealloc
, /*tp_dealloc*/
205 py_samr_user_hnd_getattr
, /*tp_getattr*/
210 0, /*tp_as_sequence*/
215 PyObject
*new_samr_user_hnd_object(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
218 samr_user_hnd_object
*o
;
220 o
= PyObject_New(samr_user_hnd_object
, &samr_user_hnd_type
);
223 o
->mem_ctx
= mem_ctx
;
224 memcpy(&o
->user_pol
, pol
, sizeof(POLICY_HND
));
229 /* SAMR group handle object */
231 static void py_samr_group_hnd_dealloc(PyObject
* self
)
236 static PyMethodDef samr_group_methods
[] = {
240 static PyObject
*py_samr_group_hnd_getattr(PyObject
*self
, char *attrname
)
242 return Py_FindMethod(samr_group_methods
, self
, attrname
);
245 PyTypeObject samr_group_hnd_type
= {
246 PyObject_HEAD_INIT(NULL
)
249 sizeof(samr_group_hnd_object
),
251 py_samr_group_hnd_dealloc
, /*tp_dealloc*/
253 py_samr_group_hnd_getattr
, /*tp_getattr*/
258 0, /*tp_as_sequence*/
263 PyObject
*new_samr_group_hnd_object(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
266 samr_group_hnd_object
*o
;
268 o
= PyObject_New(samr_group_hnd_object
, &samr_group_hnd_type
);
271 o
->mem_ctx
= mem_ctx
;
272 memcpy(&o
->group_pol
, pol
, sizeof(POLICY_HND
));
277 /* Alias handle object */
279 static void py_samr_alias_hnd_dealloc(PyObject
* self
)
284 static PyMethodDef samr_alias_methods
[] = {
288 static PyObject
*py_samr_alias_hnd_getattr(PyObject
*self
, char *attrname
)
290 return Py_FindMethod(samr_alias_methods
, self
, attrname
);
293 PyTypeObject samr_alias_hnd_type
= {
294 PyObject_HEAD_INIT(NULL
)
297 sizeof(samr_alias_hnd_object
),
299 py_samr_alias_hnd_dealloc
, /*tp_dealloc*/
301 py_samr_alias_hnd_getattr
, /*tp_getattr*/
306 0, /*tp_as_sequence*/
311 PyObject
*new_samr_alias_hnd_object(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
314 samr_alias_hnd_object
*o
;
316 o
= PyObject_New(samr_alias_hnd_object
, &samr_alias_hnd_type
);
319 o
->mem_ctx
= mem_ctx
;
320 memcpy(&o
->alias_pol
, pol
, sizeof(POLICY_HND
));
325 static PyObject
*samr_connect(PyObject
*self
, PyObject
*args
, PyObject
*kw
)
327 static char *kwlist
[] = { "server", "creds", "access", NULL
};
328 uint32 desired_access
= MAXIMUM_ALLOWED_ACCESS
;
329 char *server
, *errstr
;
330 struct cli_state
*cli
= NULL
;
332 TALLOC_CTX
*mem_ctx
= NULL
;
333 PyObject
*result
= NULL
, *creds
= NULL
;
336 if (!PyArg_ParseTupleAndKeywords(
337 args
, kw
, "s|Oi", kwlist
, &server
, &creds
,
341 if (server
[0] != '\\' || server
[1] != '\\') {
342 PyErr_SetString(PyExc_ValueError
, "UNC name required");
348 if (creds
&& creds
!= Py_None
&& !PyDict_Check(creds
)) {
349 PyErr_SetString(PyExc_TypeError
,
350 "credentials must be dictionary or None");
354 if (!(cli
= open_pipe_creds(server
, creds
, PIPE_SAMR
, &errstr
))) {
355 PyErr_SetString(samr_error
, errstr
);
360 if (!(mem_ctx
= talloc_init())) {
361 PyErr_SetString(samr_ntstatus
,
362 "unable to init talloc context\n");
366 ntstatus
= cli_samr_connect(cli
, mem_ctx
, desired_access
, &hnd
);
368 if (!NT_STATUS_IS_OK(ntstatus
)) {
371 PyErr_SetObject(samr_ntstatus
, py_ntstatus_tuple(ntstatus
));
375 result
= new_samr_connect_hnd_object(cli
, mem_ctx
, &hnd
);
383 talloc_destroy(mem_ctx
);
390 * Module initialisation
393 static PyMethodDef samr_methods
[] = {
395 /* Open/close samr connect handles */
397 { "connect", (PyCFunction
)samr_connect
,
398 METH_VARARGS
| METH_KEYWORDS
,
399 "Open a connect handle" },
404 static struct const_vals
{
407 } module_const_vals
[] = {
411 static void const_init(PyObject
*dict
)
413 struct const_vals
*tmp
;
416 for (tmp
= module_const_vals
; tmp
->name
; tmp
++) {
417 obj
= PyInt_FromLong(tmp
->value
);
418 PyDict_SetItemString(dict
, tmp
->name
, obj
);
425 PyObject
*module
, *dict
;
427 /* Initialise module */
429 module
= Py_InitModule("samr", samr_methods
);
430 dict
= PyModule_GetDict(module
);
432 samr_error
= PyErr_NewException("samr.error", NULL
, NULL
);
433 PyDict_SetItemString(dict
, "error", samr_error
);
435 samr_ntstatus
= PyErr_NewException("samr.ntstatus", NULL
, NULL
);
436 PyDict_SetItemString(dict
, "ntstatus", samr_ntstatus
);
438 /* Initialise policy handle object */
440 samr_connect_hnd_type
.ob_type
= &PyType_Type
;
441 samr_domain_hnd_type
.ob_type
= &PyType_Type
;
442 samr_user_hnd_type
.ob_type
= &PyType_Type
;
443 samr_group_hnd_type
.ob_type
= &PyType_Type
;
444 samr_alias_hnd_type
.ob_type
= &PyType_Type
;
446 /* Initialise constants */
450 /* Do samba initialisation */
454 setup_logging("samr", True
);