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"
38 struct libnet_context
*libnet_ctx
;
39 struct tevent_context
*ev
;
42 static PyObject
*py_net_join(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
48 const char *kwnames
[] = { "domain_name", "netbios_name", "join_type", "level", NULL
};
50 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "ssii:Join", discard_const_p(char *, kwnames
),
51 &r
.in
.domain_name
, &r
.in
.netbios_name
,
52 &r
.in
.join_type
, &r
.in
.level
))
55 mem_ctx
= talloc_new(self
->mem_ctx
);
56 if (mem_ctx
== NULL
) {
61 status
= libnet_Join(self
->libnet_ctx
, mem_ctx
, &r
);
62 if (NT_STATUS_IS_ERR(status
)) {
63 PyErr_SetString(PyExc_RuntimeError
, r
.out
.error_string
?r
.out
.error_string
:nt_errstr(status
));
68 result
= Py_BuildValue("sss", r
.out
.join_password
,
69 dom_sid_string(mem_ctx
, r
.out
.domain_sid
),
77 static const char py_net_join_doc
[] = "join(domain_name, netbios_name, join_type, level) -> (join_password, domain_sid, domain_name)\n\n" \
78 "Join the domain with the specified name.";
80 static PyObject
*py_net_set_password(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
82 union libnet_SetPassword r
;
86 struct tevent_context
*ev
;
87 const char *kwnames
[] = { "account_name", "domain_name", "newpassword", "credentials", NULL
};
89 r
.generic
.level
= LIBNET_SET_PASSWORD_GENERIC
;
91 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "sssO:set_password", discard_const_p(char *, kwnames
),
92 &r
.generic
.in
.account_name
, &r
.generic
.in
.domain_name
,
93 &r
.generic
.in
.newpassword
, &py_creds
)) {
97 /* FIXME: we really need to get a context from the caller or we may end
98 * up with 2 event contexts */
99 ev
= s4_event_context_init(NULL
);
101 mem_ctx
= talloc_new(ev
);
102 if (mem_ctx
== NULL
) {
107 status
= libnet_SetPassword(self
->libnet_ctx
, mem_ctx
, &r
);
108 if (NT_STATUS_IS_ERR(status
)) {
109 PyErr_SetString(PyExc_RuntimeError
,
110 r
.generic
.out
.error_string
?r
.generic
.out
.error_string
:nt_errstr(status
));
111 talloc_free(mem_ctx
);
115 talloc_free(mem_ctx
);
120 static const char py_net_set_password_doc
[] = "set_password(account_name, domain_name, newpassword) -> True\n\n" \
121 "Set password for a user. You must supply credential with enough rights to do this.\n\n" \
122 "Sample usage is:\n" \
123 "net.set_password(account_name=<account_name>,\n" \
124 " domain_name=domain_name,\n" \
125 " newpassword=new_pass)\n";
128 static PyObject
*py_net_export_keytab(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
130 struct libnet_export_keytab r
;
132 const char *kwnames
[] = { "keytab", NULL
};
135 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s:export_keytab", discard_const_p(char *, kwnames
),
136 &r
.in
.keytab_name
)) {
140 mem_ctx
= talloc_new(self
->mem_ctx
);
141 if (mem_ctx
== NULL
) {
146 status
= libnet_export_keytab(self
->libnet_ctx
, mem_ctx
, &r
);
147 if (NT_STATUS_IS_ERR(status
)) {
148 PyErr_SetString(PyExc_RuntimeError
,
149 r
.out
.error_string
?r
.out
.error_string
:nt_errstr(status
));
150 talloc_free(mem_ctx
);
154 talloc_free(mem_ctx
);
159 static const char py_net_export_keytab_doc
[] = "export_keytab(keytab, name)\n\n"
160 "Export the DC keytab to a keytab file.";
162 static PyObject
*py_net_time(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
164 const char *kwnames
[] = { "server_name", NULL
};
165 union libnet_RemoteTOD r
;
172 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s",
173 discard_const_p(char *, kwnames
), &r
.generic
.in
.server_name
))
176 r
.generic
.level
= LIBNET_REMOTE_TOD_GENERIC
;
178 mem_ctx
= talloc_new(NULL
);
179 if (mem_ctx
== NULL
) {
184 status
= libnet_RemoteTOD(self
->libnet_ctx
, mem_ctx
, &r
);
185 if (!NT_STATUS_IS_OK(status
)) {
186 PyErr_SetString(PyExc_RuntimeError
,
187 r
.generic
.out
.error_string
?r
.generic
.out
.error_string
:nt_errstr(status
));
188 talloc_free(mem_ctx
);
192 ZERO_STRUCT(timestr
);
193 tm
= localtime(&r
.generic
.out
.time
);
194 strftime(timestr
, sizeof(timestr
)-1, "%c %Z",tm
);
196 ret
= PyString_FromString(timestr
);
198 talloc_free(mem_ctx
);
203 static const char py_net_time_doc
[] = "time(server_name) -> timestr\n"
204 "Retrieve the remote time on a server";
206 static PyObject
*py_net_user_create(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
208 const char *kwnames
[] = { "username", NULL
};
211 struct libnet_CreateUser r
;
213 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s", discard_const_p(char *, kwnames
),
217 r
.in
.domain_name
= cli_credentials_get_domain(self
->libnet_ctx
->cred
);
219 mem_ctx
= talloc_new(NULL
);
220 if (mem_ctx
== NULL
) {
225 status
= libnet_CreateUser(self
->libnet_ctx
, mem_ctx
, &r
);
226 if (!NT_STATUS_IS_OK(status
)) {
227 PyErr_SetString(PyExc_RuntimeError
, r
.out
.error_string
?r
.out
.error_string
:nt_errstr(status
));
228 talloc_free(mem_ctx
);
232 talloc_free(mem_ctx
);
237 static const char py_net_create_user_doc
[] = "create_user(username)\n"
238 "Create a new user.";
240 static PyObject
*py_net_user_delete(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
242 const char *kwnames
[] = { "username", NULL
};
245 struct libnet_DeleteUser r
;
247 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s", discard_const_p(char *, kwnames
),
251 r
.in
.domain_name
= cli_credentials_get_domain(self
->libnet_ctx
->cred
);
253 mem_ctx
= talloc_new(NULL
);
254 if (mem_ctx
== NULL
) {
259 status
= libnet_DeleteUser(self
->libnet_ctx
, mem_ctx
, &r
);
260 if (!NT_STATUS_IS_OK(status
)) {
261 PyErr_SetString(PyExc_RuntimeError
, r
.out
.error_string
?r
.out
.error_string
:nt_errstr(status
));
262 talloc_free(mem_ctx
);
266 talloc_free(mem_ctx
);
271 static const char py_net_delete_user_doc
[] = "delete_user(username)\n"
274 static PyObject
*py_dom_sid_FromSid(struct dom_sid
*sid
)
276 PyObject
*mod_security
, *dom_sid_Type
;
278 mod_security
= PyImport_ImportModule("samba.dcerpc.security");
279 if (mod_security
== NULL
)
282 dom_sid_Type
= PyObject_GetAttrString(mod_security
, "dom_sid");
283 if (dom_sid_Type
== NULL
)
286 return py_talloc_reference((PyTypeObject
*)dom_sid_Type
, sid
);
289 static PyObject
*py_net_vampire(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
291 const char *kwnames
[] = { "domain", "target_dir", NULL
};
295 struct libnet_Vampire r
;
297 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|z", discard_const_p(char *, kwnames
),
298 &r
.in
.domain_name
, &r
.in
.targetdir
)) {
302 r
.in
.netbios_name
= lpcfg_netbios_name(self
->libnet_ctx
->lp_ctx
);
303 r
.out
.error_string
= NULL
;
305 mem_ctx
= talloc_new(NULL
);
306 if (mem_ctx
== NULL
) {
311 status
= libnet_Vampire(self
->libnet_ctx
, mem_ctx
, &r
);
313 if (!NT_STATUS_IS_OK(status
)) {
314 PyErr_SetString(PyExc_RuntimeError
,
315 r
.out
.error_string
? r
.out
.error_string
: nt_errstr(status
));
316 talloc_free(mem_ctx
);
320 ret
= Py_BuildValue("(sO)", r
.out
.domain_name
, py_dom_sid_FromSid(r
.out
.domain_sid
));
322 talloc_free(mem_ctx
);
327 struct replicate_state
{
329 dcerpc_InterfaceObject
*drs_pipe
;
330 struct libnet_BecomeDC_StoreChunk chunk
;
331 DATA_BLOB gensec_skey
;
332 struct libnet_BecomeDC_Partition partition
;
333 struct libnet_BecomeDC_Forest forest
;
334 struct libnet_BecomeDC_DestDSA dest_dsa
;
338 setup for replicate_chunk() calls
340 static PyObject
*py_net_replicate_init(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
342 const char *kwnames
[] = { "samdb", "lp", "drspipe", NULL
};
343 PyObject
*py_ldb
, *py_lp
, *py_drspipe
;
344 struct ldb_context
*samdb
;
345 struct loadparm_context
*lp
;
346 struct replicate_state
*s
;
349 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OOO",
350 discard_const_p(char *, kwnames
),
351 &py_ldb
, &py_lp
, &py_drspipe
)) {
355 s
= talloc_zero(NULL
, struct replicate_state
);
358 lp
= lpcfg_from_py_object(s
, py_lp
);
360 PyErr_SetString(PyExc_TypeError
, "Expected lp object");
365 samdb
= PyLdb_AsLdbContext(py_ldb
);
367 PyErr_SetString(PyExc_TypeError
, "Expected ldb object");
372 s
->drs_pipe
= (dcerpc_InterfaceObject
*)(py_drspipe
);
374 s
->vampire_state
= libnet_vampire_replicate_init(s
, samdb
, lp
);
375 if (s
->vampire_state
== NULL
) {
376 PyErr_SetString(PyExc_TypeError
, "Failed to initialise vampire_state");
381 status
= gensec_session_key(s
->drs_pipe
->pipe
->conn
->security_state
.generic_state
,
383 if (!NT_STATUS_IS_OK(status
)) {
384 PyErr_Format(PyExc_RuntimeError
, "Unable to get session key from drspipe: %s",
390 s
->forest
.dns_name
= lpcfg_dnsdomain(lp
);
392 s
->chunk
.gensec_skey
= &s
->gensec_skey
;
393 s
->chunk
.partition
= &s
->partition
;
394 s
->chunk
.forest
= &s
->forest
;
395 s
->chunk
.dest_dsa
= &s
->dest_dsa
;
397 return PyCObject_FromTallocPtr(s
);
402 process one replication chunk
404 static PyObject
*py_net_replicate_chunk(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
406 const char *kwnames
[] = { "state", "level", "ctr", "schema", NULL
};
407 PyObject
*py_state
, *py_ctr
, *py_schema
;
408 struct replicate_state
*s
;
410 NTSTATUS (*chunk_handler
)(void *private_data
, const struct libnet_BecomeDC_StoreChunk
*c
);
413 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OIO|O",
414 discard_const_p(char *, kwnames
),
415 &py_state
, &level
, &py_ctr
, &py_schema
)) {
419 s
= talloc_get_type(PyCObject_AsVoidPtr(py_state
), struct replicate_state
);
421 PyErr_SetString(PyExc_TypeError
, "Expected replication_state");
427 if (!py_check_dcerpc_type(py_ctr
, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr1")) {
430 s
->chunk
.ctr1
= py_talloc_get_ptr(py_ctr
);
431 s
->partition
.nc
= *s
->chunk
.ctr1
->naming_context
;
432 s
->partition
.more_data
= s
->chunk
.ctr1
->more_data
;
433 s
->partition
.source_dsa_guid
= s
->chunk
.ctr1
->source_dsa_guid
;
434 s
->partition
.source_dsa_invocation_id
= s
->chunk
.ctr1
->source_dsa_invocation_id
;
435 s
->partition
.highwatermark
= s
->chunk
.ctr1
->new_highwatermark
;
438 if (!py_check_dcerpc_type(py_ctr
, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr6")) {
441 s
->chunk
.ctr6
= py_talloc_get_ptr(py_ctr
);
442 s
->partition
.nc
= *s
->chunk
.ctr6
->naming_context
;
443 s
->partition
.more_data
= s
->chunk
.ctr6
->more_data
;
444 s
->partition
.source_dsa_guid
= s
->chunk
.ctr6
->source_dsa_guid
;
445 s
->partition
.source_dsa_invocation_id
= s
->chunk
.ctr6
->source_dsa_invocation_id
;
446 s
->partition
.highwatermark
= s
->chunk
.ctr6
->new_highwatermark
;
449 PyErr_Format(PyExc_TypeError
, "Bad level %u in replicate_chunk", level
);
453 chunk_handler
= libnet_vampire_cb_store_chunk
;
455 if (!PyBool_Check(py_schema
)) {
456 PyErr_SetString(PyExc_TypeError
, "Expected boolean schema");
459 if (py_schema
== Py_True
) {
460 chunk_handler
= libnet_vampire_cb_schema_chunk
;
464 s
->chunk
.ctr_level
= level
;
466 status
= chunk_handler(s
->vampire_state
, &s
->chunk
);
467 if (!NT_STATUS_IS_OK(status
)) {
468 PyErr_Format(PyExc_TypeError
, "Failed to process chunk: %s", nt_errstr(status
));
477 find a DC given a domain name and server type
479 static PyObject
*py_net_finddc(py_net_Object
*self
, PyObject
*args
)
481 const char *domain_name
;
482 unsigned server_type
;
488 if (!PyArg_ParseTuple(args
, "sI", &domain_name
, &server_type
)) {
492 mem_ctx
= talloc_new(self
->mem_ctx
);
494 io
= talloc_zero(mem_ctx
, struct finddcs
);
495 io
->in
.domain_name
= domain_name
;
496 io
->in
.minimum_dc_flags
= server_type
;
498 status
= finddcs_cldap(io
, io
,
499 lpcfg_resolve_context(self
->libnet_ctx
->lp_ctx
), self
->ev
);
500 if (NT_STATUS_IS_ERR(status
)) {
501 PyErr_SetString(PyExc_RuntimeError
, nt_errstr(status
));
502 talloc_free(mem_ctx
);
506 ret
= py_return_ndr_struct("samba.dcerpc.nbt", "NETLOGON_SAM_LOGON_RESPONSE_EX",
507 io
, &io
->out
.netlogon
.data
.nt5_ex
);
508 talloc_free(mem_ctx
);
514 static const char py_net_vampire_doc
[] = "vampire(domain, target_dir=None)\n"
517 static const char py_net_replicate_init_doc
[] = "replicate_init(samdb, lp, drspipe)\n"
518 "Setup for replicate_chunk calls.";
520 static const char py_net_replicate_chunk_doc
[] = "replicate_chunk(state, level, ctr, schema)\n"
521 "Process replication for one chunk";
523 static const char py_net_finddc_doc
[] = "finddc(domain, server_type)\n"
524 "find a DC with the specified server_type bits. Return the DNS name";
526 static PyMethodDef net_obj_methods
[] = {
527 {"join", (PyCFunction
)py_net_join
, METH_VARARGS
|METH_KEYWORDS
, py_net_join_doc
},
528 {"set_password", (PyCFunction
)py_net_set_password
, METH_VARARGS
|METH_KEYWORDS
, py_net_set_password_doc
},
529 {"export_keytab", (PyCFunction
)py_net_export_keytab
, METH_VARARGS
|METH_KEYWORDS
, py_net_export_keytab_doc
},
530 {"time", (PyCFunction
)py_net_time
, METH_VARARGS
|METH_KEYWORDS
, py_net_time_doc
},
531 {"create_user", (PyCFunction
)py_net_user_create
, METH_VARARGS
|METH_KEYWORDS
, py_net_create_user_doc
},
532 {"delete_user", (PyCFunction
)py_net_user_delete
, METH_VARARGS
|METH_KEYWORDS
, py_net_delete_user_doc
},
533 {"vampire", (PyCFunction
)py_net_vampire
, METH_VARARGS
|METH_KEYWORDS
, py_net_vampire_doc
},
534 {"replicate_init", (PyCFunction
)py_net_replicate_init
, METH_VARARGS
|METH_KEYWORDS
, py_net_replicate_init_doc
},
535 {"replicate_chunk", (PyCFunction
)py_net_replicate_chunk
, METH_VARARGS
|METH_KEYWORDS
, py_net_replicate_chunk_doc
},
536 {"finddc", (PyCFunction
)py_net_finddc
, METH_VARARGS
, py_net_finddc_doc
},
540 static void py_net_dealloc(py_net_Object
*self
)
542 talloc_free(self
->mem_ctx
);
545 static PyObject
*net_obj_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
547 PyObject
*py_creds
, *py_lp
= Py_None
;
548 const char *kwnames
[] = { "creds", "lp", "server", NULL
};
550 struct loadparm_context
*lp
;
551 const char *server_address
= NULL
;
553 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|Oz",
554 discard_const_p(char *, kwnames
), &py_creds
, &py_lp
,
558 ret
= PyObject_New(py_net_Object
, type
);
563 /* FIXME: we really need to get a context from the caller or we may end
564 * up with 2 event contexts */
565 ret
->ev
= s4_event_context_init(NULL
);
566 ret
->mem_ctx
= talloc_new(ret
->ev
);
568 lp
= lpcfg_from_py_object(ret
->mem_ctx
, py_lp
);
574 ret
->libnet_ctx
= libnet_context_init(ret
->ev
, lp
);
575 if (ret
->libnet_ctx
== NULL
) {
576 PyErr_SetString(PyExc_RuntimeError
, "Unable to initialize net");
581 ret
->libnet_ctx
->server_address
= server_address
;
583 ret
->libnet_ctx
->cred
= cli_credentials_from_py_object(py_creds
);
584 if (ret
->libnet_ctx
->cred
== NULL
) {
585 PyErr_SetString(PyExc_TypeError
, "Expected credentials object");
590 return (PyObject
*)ret
;
594 PyTypeObject py_net_Type
= {
595 PyObject_HEAD_INIT(NULL
) 0,
596 .tp_name
= "net.Net",
597 .tp_basicsize
= sizeof(py_net_Object
),
598 .tp_dealloc
= (destructor
)py_net_dealloc
,
599 .tp_methods
= net_obj_methods
,
600 .tp_new
= net_obj_new
,
607 if (PyType_Ready(&py_net_Type
) < 0)
610 m
= Py_InitModule3("net", NULL
, NULL
);
614 Py_INCREF(&py_net_Type
);
615 PyModule_AddObject(m
, "Net", (PyObject
*)&py_net_Type
);
616 PyModule_AddObject(m
, "LIBNET_JOINDOMAIN_AUTOMATIC", PyInt_FromLong(LIBNET_JOINDOMAIN_AUTOMATIC
));
617 PyModule_AddObject(m
, "LIBNET_JOINDOMAIN_SPECIFIED", PyInt_FromLong(LIBNET_JOINDOMAIN_SPECIFIED
));
618 PyModule_AddObject(m
, "LIBNET_JOIN_AUTOMATIC", PyInt_FromLong(LIBNET_JOIN_AUTOMATIC
));
619 PyModule_AddObject(m
, "LIBNET_JOIN_SPECIFIED", PyInt_FromLong(LIBNET_JOIN_SPECIFIED
));