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 3 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, see <http://www.gnu.org/licenses/>.
20 #include "python/py_samr.h"
23 * Exceptions raised by this module
26 PyObject
*samr_error
; /* This indicates a non-RPC related error
27 such as name lookup failure */
29 PyObject
*samr_ntstatus
; /* This exception is raised when a RPC call
30 returns a status code other than
33 /* SAMR group handle object */
35 static void py_samr_group_hnd_dealloc(PyObject
* self
)
40 static PyMethodDef samr_group_methods
[] = {
44 static PyObject
*py_samr_group_hnd_getattr(PyObject
*self
, char *attrname
)
46 return Py_FindMethod(samr_group_methods
, self
, attrname
);
49 PyTypeObject samr_group_hnd_type
= {
50 PyObject_HEAD_INIT(NULL
)
53 sizeof(samr_group_hnd_object
),
55 py_samr_group_hnd_dealloc
, /*tp_dealloc*/
57 py_samr_group_hnd_getattr
, /*tp_getattr*/
67 PyObject
*new_samr_group_hnd_object(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
70 samr_group_hnd_object
*o
;
72 o
= PyObject_New(samr_group_hnd_object
, &samr_group_hnd_type
);
76 memcpy(&o
->group_pol
, pol
, sizeof(POLICY_HND
));
81 /* Alias handle object */
83 static void py_samr_alias_hnd_dealloc(PyObject
* self
)
88 static PyMethodDef samr_alias_methods
[] = {
92 static PyObject
*py_samr_alias_hnd_getattr(PyObject
*self
, char *attrname
)
94 return Py_FindMethod(samr_alias_methods
, self
, attrname
);
97 PyTypeObject samr_alias_hnd_type
= {
98 PyObject_HEAD_INIT(NULL
)
101 sizeof(samr_alias_hnd_object
),
103 py_samr_alias_hnd_dealloc
, /*tp_dealloc*/
105 py_samr_alias_hnd_getattr
, /*tp_getattr*/
110 0, /*tp_as_sequence*/
115 PyObject
*new_samr_alias_hnd_object(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
118 samr_alias_hnd_object
*o
;
120 o
= PyObject_New(samr_alias_hnd_object
, &samr_alias_hnd_type
);
123 o
->mem_ctx
= mem_ctx
;
124 memcpy(&o
->alias_pol
, pol
, sizeof(POLICY_HND
));
129 /* SAMR user handle object */
131 static void py_samr_user_hnd_dealloc(PyObject
* self
)
136 static PyObject
*samr_set_user_info2(PyObject
*self
, PyObject
*args
,
139 samr_user_hnd_object
*user_hnd
= (samr_user_hnd_object
*)self
;
140 static char *kwlist
[] = { "dict", NULL
};
141 PyObject
*info
, *result
= NULL
;
142 SAM_USERINFO_CTR ctr
;
148 SAM_USER_INFO_16 id16
;
149 SAM_USER_INFO_21 id21
;
152 if (!PyArg_ParseTupleAndKeywords(
153 args
, kw
, "O!", kwlist
, &PyDict_Type
, &info
))
156 if (!get_level_value(info
, &level
)) {
157 PyErr_SetString(samr_error
, "invalid info level");
163 ctr
.switch_value
= level
;
167 ctr
.info
.id16
= &pinfo
.id16
;
169 if (!py_to_SAM_USER_INFO_16(ctr
.info
.id16
, info
)) {
171 samr_error
, "error converting user info");
177 ctr
.info
.id21
= &pinfo
.id21
;
179 if (!py_to_SAM_USER_INFO_21(ctr
.info
.id21
, info
)) {
181 samr_error
, "error converting user info");
187 PyErr_SetString(samr_error
, "unsupported info level");
191 /* Call RPC function */
193 if (!(mem_ctx
= talloc_init("samr_set_user_info2"))) {
195 samr_error
, "unable to init talloc context\n");
199 ntstatus
= rpccli_samr_set_userinfo2(
200 user_hnd
->cli
, mem_ctx
, &user_hnd
->user_pol
, level
,
203 talloc_destroy(mem_ctx
);
205 if (!NT_STATUS_IS_OK(ntstatus
)) {
206 PyErr_SetObject(samr_ntstatus
, py_ntstatus_tuple(ntstatus
));
217 static PyObject
*samr_delete_dom_user(PyObject
*self
, PyObject
*args
,
220 samr_user_hnd_object
*user_hnd
= (samr_user_hnd_object
*)self
;
221 static char *kwlist
[] = { NULL
};
224 PyObject
*result
= NULL
;
226 if (!PyArg_ParseTupleAndKeywords(
227 args
, kw
, "", kwlist
))
230 if (!(mem_ctx
= talloc_init("samr_delete_dom_user"))) {
231 PyErr_SetString(samr_error
, "unable to init talloc context");
235 ntstatus
= rpccli_samr_delete_dom_user(
236 user_hnd
->cli
, mem_ctx
, &user_hnd
->user_pol
);
238 if (!NT_STATUS_IS_OK(ntstatus
)) {
239 PyErr_SetObject(samr_ntstatus
, py_ntstatus_tuple(ntstatus
));
247 talloc_destroy(mem_ctx
);
252 static PyMethodDef samr_user_methods
[] = {
253 { "delete_domain_user", (PyCFunction
)samr_delete_dom_user
,
254 METH_VARARGS
| METH_KEYWORDS
,
255 "Delete domain user." },
256 { "set_user_info2", (PyCFunction
)samr_set_user_info2
,
257 METH_VARARGS
| METH_KEYWORDS
,
262 static PyObject
*py_samr_user_hnd_getattr(PyObject
*self
, char *attrname
)
264 return Py_FindMethod(samr_user_methods
, self
, attrname
);
267 PyTypeObject samr_user_hnd_type
= {
268 PyObject_HEAD_INIT(NULL
)
271 sizeof(samr_user_hnd_object
),
273 py_samr_user_hnd_dealloc
, /*tp_dealloc*/
275 py_samr_user_hnd_getattr
, /*tp_getattr*/
280 0, /*tp_as_sequence*/
285 PyObject
*new_samr_user_hnd_object(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
288 samr_user_hnd_object
*o
;
290 o
= PyObject_New(samr_user_hnd_object
, &samr_user_hnd_type
);
293 o
->mem_ctx
= mem_ctx
;
294 memcpy(&o
->user_pol
, pol
, sizeof(POLICY_HND
));
299 /* SAMR connect handle object */
301 static void py_samr_connect_hnd_dealloc(PyObject
* self
)
306 PyObject
*new_samr_domain_hnd_object(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
309 samr_domain_hnd_object
*o
;
311 o
= PyObject_New(samr_domain_hnd_object
, &samr_domain_hnd_type
);
314 o
->mem_ctx
= mem_ctx
;
315 memcpy(&o
->domain_pol
, pol
, sizeof(POLICY_HND
));
320 static PyObject
*samr_open_domain(PyObject
*self
, PyObject
*args
, PyObject
*kw
)
322 samr_connect_hnd_object
*connect_hnd
= (samr_connect_hnd_object
*)self
;
323 static char *kwlist
[] = { "sid", "access", NULL
};
324 uint32 desired_access
= MAXIMUM_ALLOWED_ACCESS
;
327 TALLOC_CTX
*mem_ctx
= NULL
;
328 POLICY_HND domain_pol
;
330 PyObject
*result
= NULL
;
332 if (!PyArg_ParseTupleAndKeywords(
333 args
, kw
, "s|i", kwlist
, &sid_str
, &desired_access
))
336 if (!string_to_sid(&sid
, sid_str
)) {
337 PyErr_SetString(PyExc_TypeError
, "string is not a sid");
341 if (!(mem_ctx
= talloc_init("samr_open_domain"))) {
342 PyErr_SetString(samr_error
, "unable to init talloc context");
346 ntstatus
= rpccli_samr_open_domain(
347 connect_hnd
->cli
, mem_ctx
, &connect_hnd
->connect_pol
,
348 desired_access
, &sid
, &domain_pol
);
350 if (!NT_STATUS_IS_OK(ntstatus
)) {
351 PyErr_SetObject(samr_ntstatus
, py_ntstatus_tuple(ntstatus
));
355 result
= new_samr_domain_hnd_object(
356 connect_hnd
->cli
, mem_ctx
, &domain_pol
);
361 talloc_destroy(mem_ctx
);
367 static PyMethodDef samr_connect_methods
[] = {
368 { "open_domain", (PyCFunction
)samr_open_domain
,
369 METH_VARARGS
| METH_KEYWORDS
,
370 "Open a handle on a domain" },
375 static PyObject
*py_samr_connect_hnd_getattr(PyObject
*self
, char *attrname
)
377 return Py_FindMethod(samr_connect_methods
, self
, attrname
);
380 PyTypeObject samr_connect_hnd_type
= {
381 PyObject_HEAD_INIT(NULL
)
383 "SAMR Connect Handle",
384 sizeof(samr_connect_hnd_object
),
386 py_samr_connect_hnd_dealloc
, /*tp_dealloc*/
388 py_samr_connect_hnd_getattr
, /*tp_getattr*/
393 0, /*tp_as_sequence*/
398 PyObject
*new_samr_connect_hnd_object(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
401 samr_connect_hnd_object
*o
;
403 o
= PyObject_New(samr_connect_hnd_object
, &samr_connect_hnd_type
);
406 o
->mem_ctx
= mem_ctx
;
407 memcpy(&o
->connect_pol
, pol
, sizeof(POLICY_HND
));
412 /* SAMR domain handle object */
414 static void py_samr_domain_hnd_dealloc(PyObject
* self
)
419 static PyObject
*samr_enum_dom_groups(PyObject
*self
, PyObject
*args
,
422 samr_domain_hnd_object
*domain_hnd
= (samr_domain_hnd_object
*)self
;
423 static char *kwlist
[] = { NULL
};
425 /* uint32 desired_access = MAXIMUM_ALLOWED_ACCESS; */
426 uint32 start_idx
, size
, num_dom_groups
;
427 struct acct_info
*dom_groups
;
429 PyObject
*py_result
= NULL
;
431 if (!PyArg_ParseTupleAndKeywords(args
, kw
, "", kwlist
))
434 if (!(mem_ctx
= talloc_init("samr_enum_dom_groups"))) {
435 PyErr_SetString(samr_error
, "unable to init talloc context");
443 result
= rpccli_samr_enum_dom_groups(
444 domain_hnd
->cli
, mem_ctx
, &domain_hnd
->domain_pol
,
445 &start_idx
, size
, &dom_groups
, &num_dom_groups
);
447 if (NT_STATUS_IS_OK(result
) ||
448 NT_STATUS_V(result
) == NT_STATUS_V(STATUS_MORE_ENTRIES
)) {
449 py_from_acct_info(&py_result
, dom_groups
,
453 } while (NT_STATUS_V(result
) == NT_STATUS_V(STATUS_MORE_ENTRIES
));
458 static PyObject
*samr_create_dom_user(PyObject
*self
, PyObject
*args
,
461 samr_domain_hnd_object
*domain_hnd
= (samr_domain_hnd_object
*)self
;
462 static char *kwlist
[] = { "account_name", "acb_info", NULL
};
465 uint32 unknown
= 0xe005000b; /* Access mask? */
467 PyObject
*result
= NULL
;
469 uint32 acb_info
= ACB_NORMAL
;
472 if (!PyArg_ParseTupleAndKeywords(
473 args
, kw
, "s|i", kwlist
, &account_name
, &acb_info
))
476 if (!(mem_ctx
= talloc_init("samr_create_dom_user"))) {
477 PyErr_SetString(samr_error
, "unable to init talloc context");
481 ntstatus
= rpccli_samr_create_dom_user(
482 domain_hnd
->cli
, mem_ctx
, &domain_hnd
->domain_pol
,
483 account_name
, acb_info
, unknown
, &user_pol
, &user_rid
);
485 if (!NT_STATUS_IS_OK(ntstatus
)) {
486 PyErr_SetObject(samr_ntstatus
, py_ntstatus_tuple(ntstatus
));
487 talloc_destroy(mem_ctx
);
491 result
= new_samr_user_hnd_object(
492 domain_hnd
->cli
, mem_ctx
, &user_pol
);
499 static PyMethodDef samr_domain_methods
[] = {
500 { "enum_domain_groups", (PyCFunction
)samr_enum_dom_groups
,
501 METH_VARARGS
| METH_KEYWORDS
, "Enumerate domain groups" },
502 { "create_domain_user", (PyCFunction
)samr_create_dom_user
,
503 METH_VARARGS
| METH_KEYWORDS
, "Create domain user" },
507 static PyObject
*py_samr_domain_hnd_getattr(PyObject
*self
, char *attrname
)
509 return Py_FindMethod(samr_domain_methods
, self
, attrname
);
512 PyTypeObject samr_domain_hnd_type
= {
513 PyObject_HEAD_INIT(NULL
)
515 "SAMR Domain Handle",
516 sizeof(samr_domain_hnd_object
),
518 py_samr_domain_hnd_dealloc
, /*tp_dealloc*/
520 py_samr_domain_hnd_getattr
, /*tp_getattr*/
525 0, /*tp_as_sequence*/
530 static PyObject
*samr_connect(PyObject
*self
, PyObject
*args
, PyObject
*kw
)
532 static char *kwlist
[] = { "server", "creds", "access", NULL
};
533 uint32 desired_access
= MAXIMUM_ALLOWED_ACCESS
;
534 char *server
, *errstr
;
535 struct cli_state
*cli
= NULL
;
537 TALLOC_CTX
*mem_ctx
= NULL
;
538 PyObject
*result
= NULL
, *creds
= NULL
;
541 if (!PyArg_ParseTupleAndKeywords(
542 args
, kw
, "s|Oi", kwlist
, &server
, &creds
,
546 if (server
[0] != '\\' || server
[1] != '\\') {
547 PyErr_SetString(PyExc_ValueError
, "UNC name required");
553 if (creds
&& creds
!= Py_None
&& !PyDict_Check(creds
)) {
554 PyErr_SetString(PyExc_TypeError
,
555 "credentials must be dictionary or None");
559 if (!(cli
= open_pipe_creds(server
, creds
, PI_SAMR
, &errstr
))) {
560 PyErr_SetString(samr_error
, errstr
);
565 if (!(mem_ctx
= talloc_init("samr_connect"))) {
566 PyErr_SetString(samr_ntstatus
,
567 "unable to init talloc context\n");
571 ntstatus
= rpccli_samr_connect(cli
->pipe_list
, mem_ctx
, desired_access
, &hnd
);
573 if (!NT_STATUS_IS_OK(ntstatus
)) {
575 PyErr_SetObject(samr_ntstatus
, py_ntstatus_tuple(ntstatus
));
579 result
= new_samr_connect_hnd_object(cli
->pipe_list
, mem_ctx
, &hnd
);
587 talloc_destroy(mem_ctx
);
594 * Module initialisation
597 static PyMethodDef samr_methods
[] = {
599 /* Open/close samr connect handles */
601 { "connect", (PyCFunction
)samr_connect
,
602 METH_VARARGS
| METH_KEYWORDS
,
603 "Open a connect handle" },
608 static struct const_vals
{
611 } module_const_vals
[] = {
613 /* Account control bits */
615 { "ACB_DISABLED", 0x0001 },
616 { "ACB_HOMDIRREQ", 0x0002 },
617 { "ACB_PWNOTREQ", 0x0004 },
618 { "ACB_TEMPDUP", 0x0008 },
619 { "ACB_NORMAL", 0x0010 },
620 { "ACB_MNS", 0x0020 },
621 { "ACB_DOMTRUST", 0x0040 },
622 { "ACB_WSTRUST", 0x0080 },
623 { "ACB_SVRTRUST", 0x0100 },
624 { "ACB_PWNOEXP", 0x0200 },
625 { "ACB_AUTOLOCK", 0x0400 },
630 static void const_init(PyObject
*dict
)
632 struct const_vals
*tmp
;
635 for (tmp
= module_const_vals
; tmp
->name
; tmp
++) {
636 obj
= PyInt_FromLong(tmp
->value
);
637 PyDict_SetItemString(dict
, tmp
->name
, obj
);
644 PyObject
*module
, *dict
;
646 /* Initialise module */
648 module
= Py_InitModule("samr", samr_methods
);
649 dict
= PyModule_GetDict(module
);
651 samr_error
= PyErr_NewException("samr.error", NULL
, NULL
);
652 PyDict_SetItemString(dict
, "error", samr_error
);
654 samr_ntstatus
= PyErr_NewException("samr.ntstatus", NULL
, NULL
);
655 PyDict_SetItemString(dict
, "ntstatus", samr_ntstatus
);
657 /* Initialise policy handle object */
659 samr_connect_hnd_type
.ob_type
= &PyType_Type
;
660 samr_domain_hnd_type
.ob_type
= &PyType_Type
;
661 samr_user_hnd_type
.ob_type
= &PyType_Type
;
662 samr_group_hnd_type
.ob_type
= &PyType_Type
;
663 samr_alias_hnd_type
.ob_type
= &PyType_Type
;
665 /* Initialise constants */
669 /* Do samba initialisation */
673 setup_logging("samr", True
);