2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008-2010
6 Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "python/py3compat.h"
25 #include "python/modules.h"
29 #include "auth/credentials/pycredentials.h"
30 #include "libcli/security/security.h"
31 #include "lib/events/events.h"
32 #include "param/pyparam.h"
33 #include "auth/gensec/gensec.h"
34 #include "librpc/rpc/pyrpc_util.h"
35 #include "libcli/resolve/resolve.h"
36 #include "libcli/finddc.h"
37 #include "dsdb/samdb/samdb.h"
39 #include "librpc/rpc/pyrpc_util.h"
40 #include "libcli/drsuapi/drsuapi.h"
42 static void PyErr_SetDsExtendedError(enum drsuapi_DsExtendedError ext_err
, const char *error_description
)
45 PyObject
*error
= NULL
;
46 mod
= PyImport_ImportModule("samba");
48 error
= PyObject_GetAttrString(mod
, "DsExtendedError");
50 if (error_description
== NULL
) {
52 /* Copied out of ndr_drsuapi.c:ndr_print_drsuapi_DsExtendedError() */
53 case DRSUAPI_EXOP_ERR_NONE
:
54 error_description
= "DRSUAPI_EXOP_ERR_NONE";
56 case DRSUAPI_EXOP_ERR_SUCCESS
:
57 error_description
= "DRSUAPI_EXOP_ERR_SUCCESS";
59 case DRSUAPI_EXOP_ERR_UNKNOWN_OP
:
60 error_description
= "DRSUAPI_EXOP_ERR_UNKNOWN_OP";
62 case DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER
:
63 error_description
= "DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER";
65 case DRSUAPI_EXOP_ERR_UPDATE_ERR
:
66 error_description
= "DRSUAPI_EXOP_ERR_UPDATE_ERR";
68 case DRSUAPI_EXOP_ERR_EXCEPTION
:
69 error_description
= "DRSUAPI_EXOP_ERR_EXCEPTION";
71 case DRSUAPI_EXOP_ERR_UNKNOWN_CALLER
:
72 error_description
= "DRSUAPI_EXOP_ERR_UNKNOWN_CALLER";
74 case DRSUAPI_EXOP_ERR_RID_ALLOC
:
75 error_description
= "DRSUAPI_EXOP_ERR_RID_ALLOC";
77 case DRSUAPI_EXOP_ERR_FSMO_OWNER_DELETED
:
78 error_description
= "DRSUAPI_EXOP_ERR_FSMO_OWNER_DELETED";
80 case DRSUAPI_EXOP_ERR_FMSO_PENDING_OP
:
81 error_description
= "DRSUAPI_EXOP_ERR_FMSO_PENDING_OP";
83 case DRSUAPI_EXOP_ERR_MISMATCH
:
84 error_description
= "DRSUAPI_EXOP_ERR_MISMATCH";
86 case DRSUAPI_EXOP_ERR_COULDNT_CONTACT
:
87 error_description
= "DRSUAPI_EXOP_ERR_COULDNT_CONTACT";
89 case DRSUAPI_EXOP_ERR_FSMO_REFUSING_ROLES
:
90 error_description
= "DRSUAPI_EXOP_ERR_FSMO_REFUSING_ROLES";
92 case DRSUAPI_EXOP_ERR_DIR_ERROR
:
93 error_description
= "DRSUAPI_EXOP_ERR_DIR_ERROR";
95 case DRSUAPI_EXOP_ERR_FSMO_MISSING_SETTINGS
:
96 error_description
= "DRSUAPI_EXOP_ERR_FSMO_MISSING_SETTINGS";
98 case DRSUAPI_EXOP_ERR_ACCESS_DENIED
:
99 error_description
= "DRSUAPI_EXOP_ERR_ACCESS_DENIED";
101 case DRSUAPI_EXOP_ERR_PARAM_ERROR
:
102 error_description
= "DRSUAPI_EXOP_ERR_PARAM_ERROR";
108 Py_BuildValue(discard_const_p(char, "(i,s)"),
111 PyErr_SetObject(error
, value
);
119 static PyObject
*py_net_join_member(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
121 struct libnet_Join_member r
;
126 const char *kwnames
[] = { "domain_name", "netbios_name", "level", "machinepass", NULL
};
130 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "ssi|z:Join", discard_const_p(char *, kwnames
),
131 &r
.in
.domain_name
, &r
.in
.netbios_name
,
133 &r
.in
.account_pass
)) {
138 mem_ctx
= talloc_new(self
->mem_ctx
);
139 if (mem_ctx
== NULL
) {
144 status
= libnet_Join_member(self
->libnet_ctx
, mem_ctx
, &r
);
145 if (NT_STATUS_IS_ERR(status
)) {
146 PyErr_SetNTSTATUS_and_string(status
,
149 : nt_errstr(status
));
150 talloc_free(mem_ctx
);
154 result
= Py_BuildValue("sss", r
.out
.join_password
,
155 dom_sid_string(mem_ctx
, r
.out
.domain_sid
),
158 talloc_free(mem_ctx
);
163 static const char py_net_join_member_doc
[] = "join_member(domain_name, netbios_name, level) -> (join_password, domain_sid, domain_name)\n\n" \
164 "Join the domain with the specified name.";
166 static PyObject
*py_net_change_password(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
168 union libnet_ChangePassword r
;
170 TALLOC_CTX
*mem_ctx
= NULL
;
171 struct tevent_context
*ev
= NULL
;
172 const char *kwnames
[] = { "newpassword", "oldpassword", "domain", "username", NULL
};
173 const char *newpass
= NULL
;
174 const char *oldpass
= NULL
;
176 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, PYARG_STR_UNI
177 "|"PYARG_STR_UNI
"ss:change_password",
178 discard_const_p(char *, kwnames
),
183 &r
.generic
.in
.domain_name
,
184 &r
.generic
.in
.account_name
)) {
188 r
.generic
.in
.newpassword
= newpass
;
189 r
.generic
.in
.oldpassword
= oldpass
;
191 r
.generic
.level
= LIBNET_CHANGE_PASSWORD_GENERIC
;
192 if (r
.generic
.in
.account_name
== NULL
) {
193 r
.generic
.in
.account_name
194 = cli_credentials_get_username(self
->libnet_ctx
->cred
);
196 if (r
.generic
.in
.domain_name
== NULL
) {
197 r
.generic
.in
.domain_name
198 = cli_credentials_get_domain(self
->libnet_ctx
->cred
);
200 if (r
.generic
.in
.oldpassword
== NULL
) {
201 r
.generic
.in
.oldpassword
202 = cli_credentials_get_password(self
->libnet_ctx
->cred
);
205 /* FIXME: we really need to get a context from the caller or we may end
206 * up with 2 event contexts */
207 ev
= s4_event_context_init(NULL
);
209 mem_ctx
= talloc_new(ev
);
210 if (mem_ctx
== NULL
) {
211 PyMem_Free(discard_const_p(char, newpass
));
212 PyMem_Free(discard_const_p(char, oldpass
));
217 status
= libnet_ChangePassword(self
->libnet_ctx
, mem_ctx
, &r
);
219 PyMem_Free(discard_const_p(char, newpass
));
220 PyMem_Free(discard_const_p(char, oldpass
));
222 if (NT_STATUS_IS_ERR(status
)) {
223 PyErr_SetNTSTATUS_and_string(status
,
224 r
.generic
.out
.error_string
225 ? r
.generic
.out
.error_string
226 : nt_errstr(status
));
227 talloc_free(mem_ctx
);
231 talloc_free(mem_ctx
);
235 static const char py_net_change_password_doc
[] = "change_password(newpassword) -> True\n\n" \
236 "Change password for a user. You must supply credential with enough rights to do this.\n\n" \
237 "Sample usage is:\n" \
238 "net.change_password(newpassword=<new_password>)\n";
241 static PyObject
*py_net_set_password(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
243 union libnet_SetPassword r
;
246 struct tevent_context
*ev
;
247 const char *kwnames
[] = { "account_name", "domain_name", "newpassword", NULL
};
251 r
.generic
.level
= LIBNET_SET_PASSWORD_GENERIC
;
253 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "sss:set_password",
254 discard_const_p(char *, kwnames
),
255 &r
.generic
.in
.account_name
,
256 &r
.generic
.in
.domain_name
,
257 &r
.generic
.in
.newpassword
)) {
261 /* FIXME: we really need to get a context from the caller or we may end
262 * up with 2 event contexts */
263 ev
= s4_event_context_init(NULL
);
265 mem_ctx
= talloc_new(ev
);
266 if (mem_ctx
== NULL
) {
271 status
= libnet_SetPassword(self
->libnet_ctx
, mem_ctx
, &r
);
272 if (NT_STATUS_IS_ERR(status
)) {
273 PyErr_SetNTSTATUS_and_string(status
,
274 r
.generic
.out
.error_string
275 ? r
.generic
.out
.error_string
276 : nt_errstr(status
));
277 talloc_free(mem_ctx
);
281 talloc_free(mem_ctx
);
286 static const char py_net_set_password_doc
[] = "set_password(account_name, domain_name, newpassword) -> True\n\n" \
287 "Set password for a user. You must supply credential with enough rights to do this.\n\n" \
288 "Sample usage is:\n" \
289 "net.set_password(account_name=account_name, domain_name=domain_name, newpassword=new_pass)\n";
292 static PyObject
*py_net_time(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
294 const char *kwnames
[] = { "server_name", NULL
};
295 union libnet_RemoteTOD r
;
302 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s",
303 discard_const_p(char *, kwnames
), &r
.generic
.in
.server_name
))
306 r
.generic
.level
= LIBNET_REMOTE_TOD_GENERIC
;
308 mem_ctx
= talloc_new(NULL
);
309 if (mem_ctx
== NULL
) {
314 status
= libnet_RemoteTOD(self
->libnet_ctx
, mem_ctx
, &r
);
315 if (!NT_STATUS_IS_OK(status
)) {
316 PyErr_SetNTSTATUS_and_string(status
,
317 r
.generic
.out
.error_string
318 ? r
.generic
.out
.error_string
319 : nt_errstr(status
));
320 talloc_free(mem_ctx
);
324 ZERO_STRUCT(timestr
);
325 tm
= localtime(&r
.generic
.out
.time
);
326 strftime(timestr
, sizeof(timestr
)-1, "%c %Z",tm
);
328 ret
= PyUnicode_FromString(timestr
);
330 talloc_free(mem_ctx
);
335 static const char py_net_time_doc
[] = "time(server_name) -> timestr\n"
336 "Retrieve the remote time on a server";
338 static PyObject
*py_net_user_create(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
340 const char *kwnames
[] = { "username", NULL
};
343 struct libnet_CreateUser r
;
345 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s", discard_const_p(char *, kwnames
),
349 r
.in
.domain_name
= cli_credentials_get_domain(self
->libnet_ctx
->cred
);
351 mem_ctx
= talloc_new(NULL
);
352 if (mem_ctx
== NULL
) {
357 status
= libnet_CreateUser(self
->libnet_ctx
, mem_ctx
, &r
);
358 if (!NT_STATUS_IS_OK(status
)) {
359 PyErr_SetNTSTATUS_and_string(status
,
362 : nt_errstr(status
));
363 talloc_free(mem_ctx
);
367 talloc_free(mem_ctx
);
372 static const char py_net_create_user_doc
[] = "create_user(username)\n"
373 "Create a new user.";
375 static PyObject
*py_net_user_delete(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
377 const char *kwnames
[] = { "username", NULL
};
380 struct libnet_DeleteUser r
;
382 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s", discard_const_p(char *, kwnames
),
386 r
.in
.domain_name
= cli_credentials_get_domain(self
->libnet_ctx
->cred
);
388 mem_ctx
= talloc_new(NULL
);
389 if (mem_ctx
== NULL
) {
394 status
= libnet_DeleteUser(self
->libnet_ctx
, mem_ctx
, &r
);
395 if (!NT_STATUS_IS_OK(status
)) {
396 PyErr_SetNTSTATUS_and_string(status
,
399 : nt_errstr(status
));
400 talloc_free(mem_ctx
);
404 talloc_free(mem_ctx
);
409 static const char py_net_delete_user_doc
[] = "delete_user(username)\n"
412 struct replicate_state
{
414 dcerpc_InterfaceObject
*drs_pipe
;
415 struct libnet_BecomeDC_StoreChunk chunk
;
416 DATA_BLOB gensec_skey
;
417 struct libnet_BecomeDC_Partition partition
;
418 struct libnet_BecomeDC_Forest forest
;
419 struct libnet_BecomeDC_DestDSA dest_dsa
;
423 setup for replicate_chunk() calls
425 static PyObject
*py_net_replicate_init(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
427 const char *kwnames
[] = { "samdb", "lp", "drspipe", "invocation_id", NULL
};
428 PyObject
*py_ldb
, *py_lp
, *py_drspipe
, *py_invocation_id
;
429 struct ldb_context
*samdb
;
430 struct loadparm_context
*lp
;
431 struct replicate_state
*s
;
434 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OOOO",
435 discard_const_p(char *, kwnames
),
436 &py_ldb
, &py_lp
, &py_drspipe
,
437 &py_invocation_id
)) {
441 s
= talloc_zero(NULL
, struct replicate_state
);
444 lp
= lpcfg_from_py_object(s
, py_lp
);
446 PyErr_SetString(PyExc_TypeError
, "Expected lp object");
451 samdb
= pyldb_Ldb_AsLdbContext(py_ldb
);
453 PyErr_SetString(PyExc_TypeError
, "Expected ldb object");
457 if (!py_check_dcerpc_type(py_invocation_id
, "samba.dcerpc.misc", "GUID")) {
462 s
->dest_dsa
.invocation_id
= *pytalloc_get_type(py_invocation_id
, struct GUID
);
464 s
->drs_pipe
= (dcerpc_InterfaceObject
*)(py_drspipe
);
466 s
->vampire_state
= libnet_vampire_replicate_init(s
, samdb
, lp
);
467 if (s
->vampire_state
== NULL
) {
468 PyErr_SetString(PyExc_TypeError
, "Failed to initialise vampire_state");
473 status
= gensec_session_key(s
->drs_pipe
->pipe
->conn
->security_state
.generic_state
,
476 if (!NT_STATUS_IS_OK(status
)) {
477 char *error_string
= talloc_asprintf(s
,
478 "Unable to get session key from drspipe: %s",
480 PyErr_SetNTSTATUS_and_string(status
, error_string
);
485 s
->forest
.dns_name
= samdb_dn_to_dns_domain(s
, ldb_get_root_basedn(samdb
));
486 s
->forest
.root_dn_str
= ldb_dn_get_linearized(ldb_get_root_basedn(samdb
));
487 s
->forest
.config_dn_str
= ldb_dn_get_linearized(ldb_get_config_basedn(samdb
));
488 s
->forest
.schema_dn_str
= ldb_dn_get_linearized(ldb_get_schema_basedn(samdb
));
490 s
->chunk
.gensec_skey
= &s
->gensec_skey
;
491 s
->chunk
.partition
= &s
->partition
;
492 s
->chunk
.forest
= &s
->forest
;
493 s
->chunk
.dest_dsa
= &s
->dest_dsa
;
495 return pytalloc_GenericObject_steal(s
);
500 process one replication chunk
502 static PyObject
*py_net_replicate_chunk(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
504 const char *kwnames
[] = { "state", "level", "ctr",
505 "schema", "req_level", "req",
507 PyObject
*py_state
, *py_ctr
, *py_schema
= Py_None
, *py_req
= Py_None
;
508 struct replicate_state
*s
;
510 unsigned req_level
= 0;
511 WERROR (*chunk_handler
)(void *private_data
, const struct libnet_BecomeDC_StoreChunk
*c
);
513 enum drsuapi_DsExtendedError extended_ret
= DRSUAPI_EXOP_ERR_NONE
;
514 enum drsuapi_DsExtendedOperation exop
= DRSUAPI_EXOP_NONE
;
516 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OIO|OIO",
517 discard_const_p(char *, kwnames
),
518 &py_state
, &level
, &py_ctr
,
519 &py_schema
, &req_level
, &py_req
)) {
523 s
= pytalloc_get_type(py_state
, struct replicate_state
);
530 if (!py_check_dcerpc_type(py_ctr
, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr1")) {
533 s
->chunk
.ctr1
= pytalloc_get_ptr(py_ctr
);
534 if (s
->chunk
.ctr1
->naming_context
!= NULL
) {
535 s
->partition
.nc
= *s
->chunk
.ctr1
->naming_context
;
537 extended_ret
= s
->chunk
.ctr1
->extended_ret
;
538 s
->partition
.more_data
= s
->chunk
.ctr1
->more_data
;
539 s
->partition
.source_dsa_guid
= s
->chunk
.ctr1
->source_dsa_guid
;
540 s
->partition
.source_dsa_invocation_id
= s
->chunk
.ctr1
->source_dsa_invocation_id
;
541 s
->partition
.highwatermark
= s
->chunk
.ctr1
->new_highwatermark
;
544 if (!py_check_dcerpc_type(py_ctr
, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr6")) {
547 s
->chunk
.ctr6
= pytalloc_get_ptr(py_ctr
);
548 if (s
->chunk
.ctr6
->naming_context
!= NULL
) {
549 s
->partition
.nc
= *s
->chunk
.ctr6
->naming_context
;
551 extended_ret
= s
->chunk
.ctr6
->extended_ret
;
552 s
->partition
.more_data
= s
->chunk
.ctr6
->more_data
;
553 s
->partition
.source_dsa_guid
= s
->chunk
.ctr6
->source_dsa_guid
;
554 s
->partition
.source_dsa_invocation_id
= s
->chunk
.ctr6
->source_dsa_invocation_id
;
555 s
->partition
.highwatermark
= s
->chunk
.ctr6
->new_highwatermark
;
558 PyErr_Format(PyExc_TypeError
, "Bad level %u in replicate_chunk", level
);
562 s
->chunk
.req5
= NULL
;
563 s
->chunk
.req8
= NULL
;
564 s
->chunk
.req10
= NULL
;
565 if (py_req
!= Py_None
) {
570 if (!py_check_dcerpc_type(py_req
, "samba.dcerpc.drsuapi", "DsGetNCChangesRequest5")) {
574 s
->chunk
.req5
= pytalloc_get_ptr(py_req
);
575 exop
= s
->chunk
.req5
->extended_op
;
578 if (!py_check_dcerpc_type(py_req
, "samba.dcerpc.drsuapi", "DsGetNCChangesRequest8")) {
582 s
->chunk
.req8
= pytalloc_get_ptr(py_req
);
583 exop
= s
->chunk
.req8
->extended_op
;
586 if (!py_check_dcerpc_type(py_req
, "samba.dcerpc.drsuapi", "DsGetNCChangesRequest10")) {
590 s
->chunk
.req10
= pytalloc_get_ptr(py_req
);
591 exop
= s
->chunk
.req10
->extended_op
;
594 PyErr_Format(PyExc_TypeError
, "Bad req_level %u in replicate_chunk", req_level
);
599 if (exop
!= DRSUAPI_EXOP_NONE
&& extended_ret
!= DRSUAPI_EXOP_ERR_SUCCESS
) {
600 PyErr_SetDsExtendedError(extended_ret
, NULL
);
604 s
->chunk
.req_level
= req_level
;
606 chunk_handler
= libnet_vampire_cb_store_chunk
;
608 if (!PyBool_Check(py_schema
)) {
609 PyErr_SetString(PyExc_TypeError
, "Expected boolean schema");
612 if (py_schema
== Py_True
) {
613 chunk_handler
= libnet_vampire_cb_schema_chunk
;
617 s
->chunk
.ctr_level
= level
;
619 werr
= chunk_handler(s
->vampire_state
, &s
->chunk
);
620 if (!W_ERROR_IS_OK(werr
)) {
622 = talloc_asprintf(NULL
,
623 "Failed to process 'chunk' of DRS replicated objects: %s",
625 PyErr_SetWERROR_and_string(werr
, error_string
);
626 TALLOC_FREE(error_string
);
635 just do the decryption of a DRS replicated attribute
637 static PyObject
*py_net_replicate_decrypt(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
639 const char *kwnames
[] = { "drspipe", "attribute", "rid", NULL
};
640 PyObject
*py_drspipe
, *py_attribute
;
642 dcerpc_InterfaceObject
*drs_pipe
;
645 DATA_BLOB gensec_skey
;
647 struct drsuapi_DsReplicaAttribute
*attribute
;
650 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OOI",
651 discard_const_p(char *, kwnames
),
653 &py_attribute
, &rid
)) {
657 frame
= talloc_stackframe();
659 if (!py_check_dcerpc_type(py_drspipe
,
661 "ClientConnection")) {
664 drs_pipe
= (dcerpc_InterfaceObject
*)(py_drspipe
);
666 status
= gensec_session_key(drs_pipe
->pipe
->conn
->security_state
.generic_state
,
669 if (!NT_STATUS_IS_OK(status
)) {
671 = talloc_asprintf(frame
,
672 "Unable to get session key from drspipe: %s",
674 PyErr_SetNTSTATUS_and_string(status
, error_string
);
679 if (!py_check_dcerpc_type(py_attribute
, "samba.dcerpc.drsuapi",
680 "DsReplicaAttribute")) {
684 attribute
= pytalloc_get_ptr(py_attribute
);
685 context
= pytalloc_get_mem_ctx(py_attribute
);
686 werr
= drsuapi_decrypt_attribute(context
, &gensec_skey
,
688 if (!W_ERROR_IS_OK(werr
)) {
689 char *error_string
= talloc_asprintf(frame
,
690 "Unable to get decrypt attribute: %s",
692 PyErr_SetWERROR_and_string(werr
, error_string
);
704 find a DC given a domain name and server type
706 static PyObject
*py_net_finddc(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
708 const char *domain
= NULL
, *address
= NULL
;
709 unsigned server_type
;
714 const char * const kwnames
[] = { "flags", "domain", "address", NULL
};
716 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "I|zz",
717 discard_const_p(char *, kwnames
),
718 &server_type
, &domain
, &address
)) {
722 mem_ctx
= talloc_new(self
->mem_ctx
);
723 if (mem_ctx
== NULL
) {
728 io
= talloc_zero(mem_ctx
, struct finddcs
);
730 TALLOC_FREE(mem_ctx
);
735 if (domain
!= NULL
) {
736 io
->in
.domain_name
= domain
;
738 if (address
!= NULL
) {
739 io
->in
.server_address
= address
;
741 io
->in
.minimum_dc_flags
= server_type
;
743 status
= finddcs_cldap(io
, io
,
744 lpcfg_resolve_context(self
->libnet_ctx
->lp_ctx
), self
->ev
);
745 if (NT_STATUS_IS_ERR(status
)) {
746 PyErr_SetNTSTATUS(status
);
747 talloc_free(mem_ctx
);
751 ret
= py_return_ndr_struct("samba.dcerpc.nbt", "NETLOGON_SAM_LOGON_RESPONSE_EX",
752 io
, &io
->out
.netlogon
.data
.nt5_ex
);
753 talloc_free(mem_ctx
);
759 static const char py_net_replicate_init_doc
[] = "replicate_init(samdb, lp, drspipe)\n"
760 "Setup for replicate_chunk calls.";
762 static const char py_net_replicate_chunk_doc
[] = "replicate_chunk(state, level, ctr, schema)\n"
763 "Process replication for one chunk";
765 static const char py_net_replicate_decrypt_doc
[] = "replicate_decrypt(drs, attribute, rid)\n"
766 "Decrypt (in place) a DsReplicaAttribute replicated with drs.GetNCChanges()";
768 static const char py_net_finddc_doc
[] = "finddc(flags=server_type, domain=None, address=None)\n"
769 "Find a DC with the specified 'server_type' bits. The 'domain' and/or 'address' have to be used as additional search criteria. Returns the whole netlogon struct";
771 static PyMethodDef net_obj_methods
[] = {
773 .ml_name
= "join_member",
774 .ml_meth
= PY_DISCARD_FUNC_SIG(PyCFunction
,
776 .ml_flags
= METH_VARARGS
|METH_KEYWORDS
,
777 .ml_doc
= py_net_join_member_doc
780 .ml_name
= "change_password",
781 .ml_meth
= PY_DISCARD_FUNC_SIG(PyCFunction
,
782 py_net_change_password
),
783 .ml_flags
= METH_VARARGS
|METH_KEYWORDS
,
784 .ml_doc
= py_net_change_password_doc
787 .ml_name
= "set_password",
788 .ml_meth
= PY_DISCARD_FUNC_SIG(PyCFunction
,
789 py_net_set_password
),
790 .ml_flags
= METH_VARARGS
|METH_KEYWORDS
,
791 .ml_doc
= py_net_set_password_doc
795 .ml_meth
= PY_DISCARD_FUNC_SIG(PyCFunction
, py_net_time
),
796 .ml_flags
= METH_VARARGS
|METH_KEYWORDS
,
797 .ml_doc
= py_net_time_doc
800 .ml_name
= "create_user",
801 .ml_meth
= PY_DISCARD_FUNC_SIG(PyCFunction
,
803 .ml_flags
= METH_VARARGS
|METH_KEYWORDS
,
804 .ml_doc
= py_net_create_user_doc
807 .ml_name
= "delete_user",
808 .ml_meth
= PY_DISCARD_FUNC_SIG(PyCFunction
,
810 .ml_flags
= METH_VARARGS
|METH_KEYWORDS
,
811 .ml_doc
= py_net_delete_user_doc
814 .ml_name
= "replicate_init",
815 .ml_meth
= PY_DISCARD_FUNC_SIG(PyCFunction
,
816 py_net_replicate_init
),
817 .ml_flags
= METH_VARARGS
|METH_KEYWORDS
,
818 .ml_doc
= py_net_replicate_init_doc
821 .ml_name
= "replicate_chunk",
822 .ml_meth
= PY_DISCARD_FUNC_SIG(PyCFunction
,
823 py_net_replicate_chunk
),
824 .ml_flags
= METH_VARARGS
|METH_KEYWORDS
,
825 .ml_doc
= py_net_replicate_chunk_doc
828 .ml_name
= "replicate_decrypt",
829 .ml_meth
= PY_DISCARD_FUNC_SIG(PyCFunction
,
830 py_net_replicate_decrypt
),
831 .ml_flags
= METH_VARARGS
|METH_KEYWORDS
,
832 .ml_doc
= py_net_replicate_decrypt_doc
836 .ml_meth
= PY_DISCARD_FUNC_SIG(PyCFunction
,
838 .ml_flags
= METH_VARARGS
|METH_KEYWORDS
,
839 .ml_doc
= py_net_finddc_doc
844 static void py_net_dealloc(py_net_Object
*self
)
846 talloc_free(self
->mem_ctx
);
850 static PyObject
*net_obj_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
852 PyObject
*py_creds
, *py_lp
= Py_None
;
853 const char *kwnames
[] = { "creds", "lp", "server", NULL
};
855 struct loadparm_context
*lp
;
856 const char *server_address
= NULL
;
858 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|Oz",
859 discard_const_p(char *, kwnames
), &py_creds
, &py_lp
,
863 ret
= PyObject_New(py_net_Object
, type
);
868 /* FIXME: we really need to get a context from the caller or we may end
869 * up with 2 event contexts */
870 ret
->ev
= s4_event_context_init(NULL
);
871 ret
->mem_ctx
= talloc_new(ret
->ev
);
873 lp
= lpcfg_from_py_object(ret
->mem_ctx
, py_lp
);
879 ret
->libnet_ctx
= libnet_context_init(ret
->ev
, lp
);
880 if (ret
->libnet_ctx
== NULL
) {
881 PyErr_SetString(PyExc_RuntimeError
, "Unable to initialize net");
886 ret
->libnet_ctx
->server_address
= server_address
;
888 ret
->libnet_ctx
->cred
= cli_credentials_from_py_object(py_creds
);
889 if (ret
->libnet_ctx
->cred
== NULL
) {
890 PyErr_SetString(PyExc_TypeError
, "Expected credentials object");
895 return (PyObject
*)ret
;
899 PyTypeObject py_net_Type
= {
900 PyVarObject_HEAD_INIT(NULL
, 0)
901 .tp_name
= "net.Net",
902 .tp_basicsize
= sizeof(py_net_Object
),
903 .tp_dealloc
= (destructor
)py_net_dealloc
,
904 .tp_methods
= net_obj_methods
,
905 .tp_new
= net_obj_new
,
908 static struct PyModuleDef moduledef
= {
909 PyModuleDef_HEAD_INIT
,
914 MODULE_INIT_FUNC(net
)
918 if (PyType_Ready(&py_net_Type
) < 0)
921 m
= PyModule_Create(&moduledef
);
925 Py_INCREF(&py_net_Type
);
926 PyModule_AddObject(m
, "Net", (PyObject
*)&py_net_Type
);
927 PyModule_AddIntConstant(m
, "LIBNET_JOINDOMAIN_AUTOMATIC", LIBNET_JOINDOMAIN_AUTOMATIC
);
928 PyModule_AddIntConstant(m
, "LIBNET_JOINDOMAIN_SPECIFIED", LIBNET_JOINDOMAIN_SPECIFIED
);
929 PyModule_AddIntConstant(m
, "LIBNET_JOIN_AUTOMATIC", LIBNET_JOIN_AUTOMATIC
);
930 PyModule_AddIntConstant(m
, "LIBNET_JOIN_SPECIFIED", LIBNET_JOIN_SPECIFIED
);