2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008-2010
5 Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "auth/credentials/pycredentials.h"
27 #include "libcli/security/security.h"
28 #include "lib/events/events.h"
29 #include "param/pyparam.h"
30 #include "auth/gensec/gensec.h"
31 #include "librpc/rpc/pyrpc_util.h"
32 #include "libcli/finddc.h"
33 #include "libcli/resolve/resolve.h"
40 struct libnet_context
*libnet_ctx
;
41 struct tevent_context
*ev
;
44 static PyObject
*py_net_join_member(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
46 struct libnet_Join_member r
;
51 const char *kwnames
[] = { "domain_name", "netbios_name", "level", NULL
};
53 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "ssi:Join", discard_const_p(char *, kwnames
),
54 &r
.in
.domain_name
, &r
.in
.netbios_name
,
60 mem_ctx
= talloc_new(self
->mem_ctx
);
61 if (mem_ctx
== NULL
) {
66 status
= libnet_Join_member(self
->libnet_ctx
, mem_ctx
, &r
);
67 if (NT_STATUS_IS_ERR(status
)) {
68 PyErr_SetString(PyExc_RuntimeError
, r
.out
.error_string
?r
.out
.error_string
:nt_errstr(status
));
73 result
= Py_BuildValue("sss", r
.out
.join_password
,
74 dom_sid_string(mem_ctx
, r
.out
.domain_sid
),
82 static const char py_net_join_member_doc
[] = "join_member(domain_name, netbios_name, level) -> (join_password, domain_sid, domain_name)\n\n" \
83 "Join the domain with the specified name.";
85 static PyObject
*py_net_change_password(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
87 union libnet_ChangePassword r
;
90 struct tevent_context
*ev
;
91 const char *kwnames
[] = { "newpassword", NULL
};
95 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s:change_password",
96 discard_const_p(char *, kwnames
),
97 &r
.generic
.in
.newpassword
)) {
101 r
.generic
.level
= LIBNET_CHANGE_PASSWORD_GENERIC
;
102 r
.generic
.in
.account_name
= cli_credentials_get_username(self
->libnet_ctx
->cred
);
103 r
.generic
.in
.domain_name
= cli_credentials_get_domain(self
->libnet_ctx
->cred
);
104 r
.generic
.in
.oldpassword
= cli_credentials_get_password(self
->libnet_ctx
->cred
);
106 /* FIXME: we really need to get a context from the caller or we may end
107 * up with 2 event contexts */
108 ev
= s4_event_context_init(NULL
);
110 mem_ctx
= talloc_new(ev
);
111 if (mem_ctx
== NULL
) {
116 status
= libnet_ChangePassword(self
->libnet_ctx
, mem_ctx
, &r
);
117 if (NT_STATUS_IS_ERR(status
)) {
118 PyErr_SetString(PyExc_RuntimeError
,
119 r
.generic
.out
.error_string
?r
.generic
.out
.error_string
:nt_errstr(status
));
120 talloc_free(mem_ctx
);
124 talloc_free(mem_ctx
);
129 static const char py_net_change_password_doc
[] = "change_password(newpassword) -> True\n\n" \
130 "Change password for a user. You must supply credential with enough rights to do this.\n\n" \
131 "Sample usage is:\n" \
132 "net.set_password(newpassword=<new_password>\n";
135 static PyObject
*py_net_set_password(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
137 union libnet_SetPassword r
;
140 struct tevent_context
*ev
;
141 const char *kwnames
[] = { "account_name", "domain_name", "newpassword", NULL
};
145 r
.generic
.level
= LIBNET_SET_PASSWORD_GENERIC
;
147 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "sss:set_password",
148 discard_const_p(char *, kwnames
),
149 &r
.generic
.in
.account_name
,
150 &r
.generic
.in
.domain_name
,
151 &r
.generic
.in
.newpassword
)) {
155 /* FIXME: we really need to get a context from the caller or we may end
156 * up with 2 event contexts */
157 ev
= s4_event_context_init(NULL
);
159 mem_ctx
= talloc_new(ev
);
160 if (mem_ctx
== NULL
) {
165 status
= libnet_SetPassword(self
->libnet_ctx
, mem_ctx
, &r
);
166 if (NT_STATUS_IS_ERR(status
)) {
167 PyErr_SetString(PyExc_RuntimeError
,
168 r
.generic
.out
.error_string
?r
.generic
.out
.error_string
:nt_errstr(status
));
169 talloc_free(mem_ctx
);
173 talloc_free(mem_ctx
);
178 static const char py_net_set_password_doc
[] = "set_password(account_name, domain_name, newpassword) -> True\n\n" \
179 "Set password for a user. You must supply credential with enough rights to do this.\n\n" \
180 "Sample usage is:\n" \
181 "net.set_password(account_name=<account_name>,\n" \
182 " domain_name=domain_name,\n" \
183 " newpassword=new_pass)\n";
186 static PyObject
*py_net_export_keytab(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
188 struct libnet_export_keytab r
;
190 const char *kwnames
[] = { "keytab", NULL
};
193 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s:export_keytab", discard_const_p(char *, kwnames
),
194 &r
.in
.keytab_name
)) {
198 mem_ctx
= talloc_new(self
->mem_ctx
);
199 if (mem_ctx
== NULL
) {
204 status
= libnet_export_keytab(self
->libnet_ctx
, mem_ctx
, &r
);
205 if (NT_STATUS_IS_ERR(status
)) {
206 PyErr_SetString(PyExc_RuntimeError
,
207 r
.out
.error_string
?r
.out
.error_string
:nt_errstr(status
));
208 talloc_free(mem_ctx
);
212 talloc_free(mem_ctx
);
217 static const char py_net_export_keytab_doc
[] = "export_keytab(keytab, name)\n\n"
218 "Export the DC keytab to a keytab file.";
220 static PyObject
*py_net_time(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
222 const char *kwnames
[] = { "server_name", NULL
};
223 union libnet_RemoteTOD r
;
230 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s",
231 discard_const_p(char *, kwnames
), &r
.generic
.in
.server_name
))
234 r
.generic
.level
= LIBNET_REMOTE_TOD_GENERIC
;
236 mem_ctx
= talloc_new(NULL
);
237 if (mem_ctx
== NULL
) {
242 status
= libnet_RemoteTOD(self
->libnet_ctx
, mem_ctx
, &r
);
243 if (!NT_STATUS_IS_OK(status
)) {
244 PyErr_SetString(PyExc_RuntimeError
,
245 r
.generic
.out
.error_string
?r
.generic
.out
.error_string
:nt_errstr(status
));
246 talloc_free(mem_ctx
);
250 ZERO_STRUCT(timestr
);
251 tm
= localtime(&r
.generic
.out
.time
);
252 strftime(timestr
, sizeof(timestr
)-1, "%c %Z",tm
);
254 ret
= PyString_FromString(timestr
);
256 talloc_free(mem_ctx
);
261 static const char py_net_time_doc
[] = "time(server_name) -> timestr\n"
262 "Retrieve the remote time on a server";
264 static PyObject
*py_net_user_create(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
266 const char *kwnames
[] = { "username", NULL
};
269 struct libnet_CreateUser r
;
271 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s", discard_const_p(char *, kwnames
),
275 r
.in
.domain_name
= cli_credentials_get_domain(self
->libnet_ctx
->cred
);
277 mem_ctx
= talloc_new(NULL
);
278 if (mem_ctx
== NULL
) {
283 status
= libnet_CreateUser(self
->libnet_ctx
, mem_ctx
, &r
);
284 if (!NT_STATUS_IS_OK(status
)) {
285 PyErr_SetString(PyExc_RuntimeError
, r
.out
.error_string
?r
.out
.error_string
:nt_errstr(status
));
286 talloc_free(mem_ctx
);
290 talloc_free(mem_ctx
);
295 static const char py_net_create_user_doc
[] = "create_user(username)\n"
296 "Create a new user.";
298 static PyObject
*py_net_user_delete(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
300 const char *kwnames
[] = { "username", NULL
};
303 struct libnet_DeleteUser r
;
305 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s", discard_const_p(char *, kwnames
),
309 r
.in
.domain_name
= cli_credentials_get_domain(self
->libnet_ctx
->cred
);
311 mem_ctx
= talloc_new(NULL
);
312 if (mem_ctx
== NULL
) {
317 status
= libnet_DeleteUser(self
->libnet_ctx
, mem_ctx
, &r
);
318 if (!NT_STATUS_IS_OK(status
)) {
319 PyErr_SetString(PyExc_RuntimeError
, r
.out
.error_string
?r
.out
.error_string
:nt_errstr(status
));
320 talloc_free(mem_ctx
);
324 talloc_free(mem_ctx
);
329 static const char py_net_delete_user_doc
[] = "delete_user(username)\n"
332 static PyObject
*py_dom_sid_FromSid(struct dom_sid
*sid
)
334 PyObject
*mod_security
, *dom_sid_Type
;
336 mod_security
= PyImport_ImportModule("samba.dcerpc.security");
337 if (mod_security
== NULL
)
340 dom_sid_Type
= PyObject_GetAttrString(mod_security
, "dom_sid");
341 if (dom_sid_Type
== NULL
)
344 return py_talloc_reference((PyTypeObject
*)dom_sid_Type
, sid
);
347 static PyObject
*py_net_vampire(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
349 const char *kwnames
[] = { "domain", "target_dir", NULL
};
353 struct libnet_Vampire r
;
355 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|z", discard_const_p(char *, kwnames
),
356 &r
.in
.domain_name
, &r
.in
.targetdir
)) {
360 r
.in
.netbios_name
= lpcfg_netbios_name(self
->libnet_ctx
->lp_ctx
);
361 r
.out
.error_string
= NULL
;
363 mem_ctx
= talloc_new(NULL
);
364 if (mem_ctx
== NULL
) {
369 status
= libnet_Vampire(self
->libnet_ctx
, mem_ctx
, &r
);
371 if (!NT_STATUS_IS_OK(status
)) {
372 PyErr_SetString(PyExc_RuntimeError
,
373 r
.out
.error_string
? r
.out
.error_string
: nt_errstr(status
));
374 talloc_free(mem_ctx
);
378 ret
= Py_BuildValue("(sO)", r
.out
.domain_name
, py_dom_sid_FromSid(r
.out
.domain_sid
));
380 talloc_free(mem_ctx
);
385 struct replicate_state
{
387 dcerpc_InterfaceObject
*drs_pipe
;
388 struct libnet_BecomeDC_StoreChunk chunk
;
389 DATA_BLOB gensec_skey
;
390 struct libnet_BecomeDC_Partition partition
;
391 struct libnet_BecomeDC_Forest forest
;
392 struct libnet_BecomeDC_DestDSA dest_dsa
;
396 setup for replicate_chunk() calls
398 static PyObject
*py_net_replicate_init(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
400 const char *kwnames
[] = { "samdb", "lp", "drspipe", NULL
};
401 PyObject
*py_ldb
, *py_lp
, *py_drspipe
;
402 struct ldb_context
*samdb
;
403 struct loadparm_context
*lp
;
404 struct replicate_state
*s
;
407 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OOO",
408 discard_const_p(char *, kwnames
),
409 &py_ldb
, &py_lp
, &py_drspipe
)) {
413 s
= talloc_zero(NULL
, struct replicate_state
);
416 lp
= lpcfg_from_py_object(s
, py_lp
);
418 PyErr_SetString(PyExc_TypeError
, "Expected lp object");
423 samdb
= pyldb_Ldb_AsLdbContext(py_ldb
);
425 PyErr_SetString(PyExc_TypeError
, "Expected ldb object");
430 s
->drs_pipe
= (dcerpc_InterfaceObject
*)(py_drspipe
);
432 s
->vampire_state
= libnet_vampire_replicate_init(s
, samdb
, lp
);
433 if (s
->vampire_state
== NULL
) {
434 PyErr_SetString(PyExc_TypeError
, "Failed to initialise vampire_state");
439 status
= gensec_session_key(s
->drs_pipe
->pipe
->conn
->security_state
.generic_state
,
442 if (!NT_STATUS_IS_OK(status
)) {
443 PyErr_Format(PyExc_RuntimeError
, "Unable to get session key from drspipe: %s",
449 s
->forest
.dns_name
= lpcfg_dnsdomain(lp
);
451 s
->chunk
.gensec_skey
= &s
->gensec_skey
;
452 s
->chunk
.partition
= &s
->partition
;
453 s
->chunk
.forest
= &s
->forest
;
454 s
->chunk
.dest_dsa
= &s
->dest_dsa
;
456 return PyCObject_FromTallocPtr(s
);
461 process one replication chunk
463 static PyObject
*py_net_replicate_chunk(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
465 const char *kwnames
[] = { "state", "level", "ctr", "schema", NULL
};
466 PyObject
*py_state
, *py_ctr
, *py_schema
;
467 struct replicate_state
*s
;
469 NTSTATUS (*chunk_handler
)(void *private_data
, const struct libnet_BecomeDC_StoreChunk
*c
);
472 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OIO|O",
473 discard_const_p(char *, kwnames
),
474 &py_state
, &level
, &py_ctr
, &py_schema
)) {
478 s
= talloc_get_type(PyCObject_AsVoidPtr(py_state
), struct replicate_state
);
480 PyErr_SetString(PyExc_TypeError
, "Expected replication_state");
486 if (!py_check_dcerpc_type(py_ctr
, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr1")) {
489 s
->chunk
.ctr1
= py_talloc_get_ptr(py_ctr
);
490 s
->partition
.nc
= *s
->chunk
.ctr1
->naming_context
;
491 s
->partition
.more_data
= s
->chunk
.ctr1
->more_data
;
492 s
->partition
.source_dsa_guid
= s
->chunk
.ctr1
->source_dsa_guid
;
493 s
->partition
.source_dsa_invocation_id
= s
->chunk
.ctr1
->source_dsa_invocation_id
;
494 s
->partition
.highwatermark
= s
->chunk
.ctr1
->new_highwatermark
;
497 if (!py_check_dcerpc_type(py_ctr
, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr6")) {
500 s
->chunk
.ctr6
= py_talloc_get_ptr(py_ctr
);
501 s
->partition
.nc
= *s
->chunk
.ctr6
->naming_context
;
502 s
->partition
.more_data
= s
->chunk
.ctr6
->more_data
;
503 s
->partition
.source_dsa_guid
= s
->chunk
.ctr6
->source_dsa_guid
;
504 s
->partition
.source_dsa_invocation_id
= s
->chunk
.ctr6
->source_dsa_invocation_id
;
505 s
->partition
.highwatermark
= s
->chunk
.ctr6
->new_highwatermark
;
508 PyErr_Format(PyExc_TypeError
, "Bad level %u in replicate_chunk", level
);
512 chunk_handler
= libnet_vampire_cb_store_chunk
;
514 if (!PyBool_Check(py_schema
)) {
515 PyErr_SetString(PyExc_TypeError
, "Expected boolean schema");
518 if (py_schema
== Py_True
) {
519 chunk_handler
= libnet_vampire_cb_schema_chunk
;
523 s
->chunk
.ctr_level
= level
;
525 status
= chunk_handler(s
->vampire_state
, &s
->chunk
);
526 if (!NT_STATUS_IS_OK(status
)) {
527 PyErr_Format(PyExc_TypeError
, "Failed to process chunk: %s", nt_errstr(status
));
536 find a DC given a domain name and server type
538 static PyObject
*py_net_finddc(py_net_Object
*self
, PyObject
*args
)
540 const char *domain_name
;
541 unsigned server_type
;
547 if (!PyArg_ParseTuple(args
, "sI", &domain_name
, &server_type
)) {
551 mem_ctx
= talloc_new(self
->mem_ctx
);
553 io
= talloc_zero(mem_ctx
, struct finddcs
);
554 io
->in
.domain_name
= domain_name
;
555 io
->in
.minimum_dc_flags
= server_type
;
557 status
= finddcs_cldap(io
, io
,
558 lpcfg_resolve_context(self
->libnet_ctx
->lp_ctx
), self
->ev
);
559 if (NT_STATUS_IS_ERR(status
)) {
560 PyErr_SetString(PyExc_RuntimeError
, nt_errstr(status
));
561 talloc_free(mem_ctx
);
565 ret
= py_return_ndr_struct("samba.dcerpc.nbt", "NETLOGON_SAM_LOGON_RESPONSE_EX",
566 io
, &io
->out
.netlogon
.data
.nt5_ex
);
567 talloc_free(mem_ctx
);
573 static const char py_net_vampire_doc
[] = "vampire(domain, target_dir=None)\n"
576 static const char py_net_replicate_init_doc
[] = "replicate_init(samdb, lp, drspipe)\n"
577 "Setup for replicate_chunk calls.";
579 static const char py_net_replicate_chunk_doc
[] = "replicate_chunk(state, level, ctr, schema)\n"
580 "Process replication for one chunk";
582 static const char py_net_finddc_doc
[] = "finddc(domain, server_type)\n"
583 "find a DC with the specified server_type bits. Return the DNS name";
585 static PyMethodDef net_obj_methods
[] = {
586 {"join_member", (PyCFunction
)py_net_join_member
, METH_VARARGS
|METH_KEYWORDS
, py_net_join_member_doc
},
587 {"change_password", (PyCFunction
)py_net_change_password
, METH_VARARGS
|METH_KEYWORDS
, py_net_change_password_doc
},
588 {"set_password", (PyCFunction
)py_net_set_password
, METH_VARARGS
|METH_KEYWORDS
, py_net_set_password_doc
},
589 {"export_keytab", (PyCFunction
)py_net_export_keytab
, METH_VARARGS
|METH_KEYWORDS
, py_net_export_keytab_doc
},
590 {"time", (PyCFunction
)py_net_time
, METH_VARARGS
|METH_KEYWORDS
, py_net_time_doc
},
591 {"create_user", (PyCFunction
)py_net_user_create
, METH_VARARGS
|METH_KEYWORDS
, py_net_create_user_doc
},
592 {"delete_user", (PyCFunction
)py_net_user_delete
, METH_VARARGS
|METH_KEYWORDS
, py_net_delete_user_doc
},
593 {"vampire", (PyCFunction
)py_net_vampire
, METH_VARARGS
|METH_KEYWORDS
, py_net_vampire_doc
},
594 {"replicate_init", (PyCFunction
)py_net_replicate_init
, METH_VARARGS
|METH_KEYWORDS
, py_net_replicate_init_doc
},
595 {"replicate_chunk", (PyCFunction
)py_net_replicate_chunk
, METH_VARARGS
|METH_KEYWORDS
, py_net_replicate_chunk_doc
},
596 {"finddc", (PyCFunction
)py_net_finddc
, METH_VARARGS
, py_net_finddc_doc
},
600 static void py_net_dealloc(py_net_Object
*self
)
602 talloc_free(self
->mem_ctx
);
606 static PyObject
*net_obj_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
608 PyObject
*py_creds
, *py_lp
= Py_None
;
609 const char *kwnames
[] = { "creds", "lp", "server", NULL
};
611 struct loadparm_context
*lp
;
612 const char *server_address
= NULL
;
614 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|Oz",
615 discard_const_p(char *, kwnames
), &py_creds
, &py_lp
,
619 ret
= PyObject_New(py_net_Object
, type
);
624 /* FIXME: we really need to get a context from the caller or we may end
625 * up with 2 event contexts */
626 ret
->ev
= s4_event_context_init(NULL
);
627 ret
->mem_ctx
= talloc_new(ret
->ev
);
629 lp
= lpcfg_from_py_object(ret
->mem_ctx
, py_lp
);
635 ret
->libnet_ctx
= libnet_context_init(ret
->ev
, lp
);
636 if (ret
->libnet_ctx
== NULL
) {
637 PyErr_SetString(PyExc_RuntimeError
, "Unable to initialize net");
642 ret
->libnet_ctx
->server_address
= server_address
;
644 ret
->libnet_ctx
->cred
= cli_credentials_from_py_object(py_creds
);
645 if (ret
->libnet_ctx
->cred
== NULL
) {
646 PyErr_SetString(PyExc_TypeError
, "Expected credentials object");
651 return (PyObject
*)ret
;
655 PyTypeObject py_net_Type
= {
656 PyObject_HEAD_INIT(NULL
) 0,
657 .tp_name
= "net.Net",
658 .tp_basicsize
= sizeof(py_net_Object
),
659 .tp_dealloc
= (destructor
)py_net_dealloc
,
660 .tp_methods
= net_obj_methods
,
661 .tp_new
= net_obj_new
,
668 if (PyType_Ready(&py_net_Type
) < 0)
671 m
= Py_InitModule3("net", NULL
, NULL
);
675 Py_INCREF(&py_net_Type
);
676 PyModule_AddObject(m
, "Net", (PyObject
*)&py_net_Type
);
677 PyModule_AddObject(m
, "LIBNET_JOINDOMAIN_AUTOMATIC", PyInt_FromLong(LIBNET_JOINDOMAIN_AUTOMATIC
));
678 PyModule_AddObject(m
, "LIBNET_JOINDOMAIN_SPECIFIED", PyInt_FromLong(LIBNET_JOINDOMAIN_SPECIFIED
));
679 PyModule_AddObject(m
, "LIBNET_JOIN_AUTOMATIC", PyInt_FromLong(LIBNET_JOIN_AUTOMATIC
));
680 PyModule_AddObject(m
, "LIBNET_JOIN_SPECIFIED", PyInt_FromLong(LIBNET_JOIN_SPECIFIED
));