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(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
50 const char *kwnames
[] = { "domain_name", "netbios_name", "join_type", "level", NULL
};
52 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "ssii:Join", discard_const_p(char *, kwnames
),
53 &r
.in
.domain_name
, &r
.in
.netbios_name
,
54 &r
.in
.join_type
, &r
.in
.level
))
57 mem_ctx
= talloc_new(self
->mem_ctx
);
58 if (mem_ctx
== NULL
) {
63 status
= libnet_Join(self
->libnet_ctx
, mem_ctx
, &r
);
64 if (NT_STATUS_IS_ERR(status
)) {
65 PyErr_SetString(PyExc_RuntimeError
, r
.out
.error_string
?r
.out
.error_string
:nt_errstr(status
));
70 result
= Py_BuildValue("sss", r
.out
.join_password
,
71 dom_sid_string(mem_ctx
, r
.out
.domain_sid
),
79 static const char py_net_join_doc
[] = "join(domain_name, netbios_name, join_type, level) -> (join_password, domain_sid, domain_name)\n\n" \
80 "Join the domain with the specified name.";
82 static PyObject
*py_net_set_password(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
84 union libnet_SetPassword r
;
88 struct tevent_context
*ev
;
89 const char *kwnames
[] = { "account_name", "domain_name", "newpassword", "credentials", NULL
};
91 r
.generic
.level
= LIBNET_SET_PASSWORD_GENERIC
;
93 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "sssO:set_password", discard_const_p(char *, kwnames
),
94 &r
.generic
.in
.account_name
, &r
.generic
.in
.domain_name
,
95 &r
.generic
.in
.newpassword
, &py_creds
)) {
99 /* FIXME: we really need to get a context from the caller or we may end
100 * up with 2 event contexts */
101 ev
= s4_event_context_init(NULL
);
103 mem_ctx
= talloc_new(ev
);
104 if (mem_ctx
== NULL
) {
109 status
= libnet_SetPassword(self
->libnet_ctx
, mem_ctx
, &r
);
110 if (NT_STATUS_IS_ERR(status
)) {
111 PyErr_SetString(PyExc_RuntimeError
,
112 r
.generic
.out
.error_string
?r
.generic
.out
.error_string
:nt_errstr(status
));
113 talloc_free(mem_ctx
);
117 talloc_free(mem_ctx
);
122 static const char py_net_set_password_doc
[] = "set_password(account_name, domain_name, newpassword) -> True\n\n" \
123 "Set password for a user. You must supply credential with enough rights to do this.\n\n" \
124 "Sample usage is:\n" \
125 "net.set_password(account_name=<account_name>,\n" \
126 " domain_name=domain_name,\n" \
127 " newpassword=new_pass)\n";
130 static PyObject
*py_net_export_keytab(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
132 struct libnet_export_keytab r
;
134 const char *kwnames
[] = { "keytab", NULL
};
137 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s:export_keytab", discard_const_p(char *, kwnames
),
138 &r
.in
.keytab_name
)) {
142 mem_ctx
= talloc_new(self
->mem_ctx
);
143 if (mem_ctx
== NULL
) {
148 status
= libnet_export_keytab(self
->libnet_ctx
, mem_ctx
, &r
);
149 if (NT_STATUS_IS_ERR(status
)) {
150 PyErr_SetString(PyExc_RuntimeError
,
151 r
.out
.error_string
?r
.out
.error_string
:nt_errstr(status
));
152 talloc_free(mem_ctx
);
156 talloc_free(mem_ctx
);
161 static const char py_net_export_keytab_doc
[] = "export_keytab(keytab, name)\n\n"
162 "Export the DC keytab to a keytab file.";
164 static PyObject
*py_net_time(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
166 const char *kwnames
[] = { "server_name", NULL
};
167 union libnet_RemoteTOD r
;
174 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s",
175 discard_const_p(char *, kwnames
), &r
.generic
.in
.server_name
))
178 r
.generic
.level
= LIBNET_REMOTE_TOD_GENERIC
;
180 mem_ctx
= talloc_new(NULL
);
181 if (mem_ctx
== NULL
) {
186 status
= libnet_RemoteTOD(self
->libnet_ctx
, mem_ctx
, &r
);
187 if (!NT_STATUS_IS_OK(status
)) {
188 PyErr_SetString(PyExc_RuntimeError
,
189 r
.generic
.out
.error_string
?r
.generic
.out
.error_string
:nt_errstr(status
));
190 talloc_free(mem_ctx
);
194 ZERO_STRUCT(timestr
);
195 tm
= localtime(&r
.generic
.out
.time
);
196 strftime(timestr
, sizeof(timestr
)-1, "%c %Z",tm
);
198 ret
= PyString_FromString(timestr
);
200 talloc_free(mem_ctx
);
205 static const char py_net_time_doc
[] = "time(server_name) -> timestr\n"
206 "Retrieve the remote time on a server";
208 static PyObject
*py_net_user_create(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
210 const char *kwnames
[] = { "username", NULL
};
213 struct libnet_CreateUser r
;
215 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s", discard_const_p(char *, kwnames
),
219 r
.in
.domain_name
= cli_credentials_get_domain(self
->libnet_ctx
->cred
);
221 mem_ctx
= talloc_new(NULL
);
222 if (mem_ctx
== NULL
) {
227 status
= libnet_CreateUser(self
->libnet_ctx
, mem_ctx
, &r
);
228 if (!NT_STATUS_IS_OK(status
)) {
229 PyErr_SetString(PyExc_RuntimeError
, r
.out
.error_string
?r
.out
.error_string
:nt_errstr(status
));
230 talloc_free(mem_ctx
);
234 talloc_free(mem_ctx
);
239 static const char py_net_create_user_doc
[] = "create_user(username)\n"
240 "Create a new user.";
242 static PyObject
*py_net_user_delete(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
244 const char *kwnames
[] = { "username", NULL
};
247 struct libnet_DeleteUser r
;
249 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s", discard_const_p(char *, kwnames
),
253 r
.in
.domain_name
= cli_credentials_get_domain(self
->libnet_ctx
->cred
);
255 mem_ctx
= talloc_new(NULL
);
256 if (mem_ctx
== NULL
) {
261 status
= libnet_DeleteUser(self
->libnet_ctx
, mem_ctx
, &r
);
262 if (!NT_STATUS_IS_OK(status
)) {
263 PyErr_SetString(PyExc_RuntimeError
, r
.out
.error_string
?r
.out
.error_string
:nt_errstr(status
));
264 talloc_free(mem_ctx
);
268 talloc_free(mem_ctx
);
273 static const char py_net_delete_user_doc
[] = "delete_user(username)\n"
276 static PyObject
*py_dom_sid_FromSid(struct dom_sid
*sid
)
278 PyObject
*mod_security
, *dom_sid_Type
;
280 mod_security
= PyImport_ImportModule("samba.dcerpc.security");
281 if (mod_security
== NULL
)
284 dom_sid_Type
= PyObject_GetAttrString(mod_security
, "dom_sid");
285 if (dom_sid_Type
== NULL
)
288 return py_talloc_reference((PyTypeObject
*)dom_sid_Type
, sid
);
291 static PyObject
*py_net_vampire(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
293 const char *kwnames
[] = { "domain", "target_dir", NULL
};
297 struct libnet_Vampire r
;
299 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|z", discard_const_p(char *, kwnames
),
300 &r
.in
.domain_name
, &r
.in
.targetdir
)) {
304 r
.in
.netbios_name
= lpcfg_netbios_name(self
->libnet_ctx
->lp_ctx
);
305 r
.out
.error_string
= NULL
;
307 mem_ctx
= talloc_new(NULL
);
308 if (mem_ctx
== NULL
) {
313 status
= libnet_Vampire(self
->libnet_ctx
, mem_ctx
, &r
);
315 if (!NT_STATUS_IS_OK(status
)) {
316 PyErr_SetString(PyExc_RuntimeError
,
317 r
.out
.error_string
? r
.out
.error_string
: nt_errstr(status
));
318 talloc_free(mem_ctx
);
322 ret
= Py_BuildValue("(sO)", r
.out
.domain_name
, py_dom_sid_FromSid(r
.out
.domain_sid
));
324 talloc_free(mem_ctx
);
329 struct replicate_state
{
331 dcerpc_InterfaceObject
*drs_pipe
;
332 struct libnet_BecomeDC_StoreChunk chunk
;
333 DATA_BLOB gensec_skey
;
334 struct libnet_BecomeDC_Partition partition
;
335 struct libnet_BecomeDC_Forest forest
;
336 struct libnet_BecomeDC_DestDSA dest_dsa
;
340 setup for replicate_chunk() calls
342 static PyObject
*py_net_replicate_init(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
344 const char *kwnames
[] = { "samdb", "lp", "drspipe", NULL
};
345 PyObject
*py_ldb
, *py_lp
, *py_drspipe
;
346 struct ldb_context
*samdb
;
347 struct loadparm_context
*lp
;
348 struct replicate_state
*s
;
351 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OOO",
352 discard_const_p(char *, kwnames
),
353 &py_ldb
, &py_lp
, &py_drspipe
)) {
357 s
= talloc_zero(NULL
, struct replicate_state
);
360 lp
= lpcfg_from_py_object(s
, py_lp
);
362 PyErr_SetString(PyExc_TypeError
, "Expected lp object");
367 samdb
= PyLdb_AsLdbContext(py_ldb
);
369 PyErr_SetString(PyExc_TypeError
, "Expected ldb object");
374 s
->drs_pipe
= (dcerpc_InterfaceObject
*)(py_drspipe
);
376 s
->vampire_state
= libnet_vampire_replicate_init(s
, samdb
, lp
);
377 if (s
->vampire_state
== NULL
) {
378 PyErr_SetString(PyExc_TypeError
, "Failed to initialise vampire_state");
383 status
= gensec_session_key(s
->drs_pipe
->pipe
->conn
->security_state
.generic_state
,
385 if (!NT_STATUS_IS_OK(status
)) {
386 PyErr_Format(PyExc_RuntimeError
, "Unable to get session key from drspipe: %s",
392 s
->forest
.dns_name
= lpcfg_dnsdomain(lp
);
394 s
->chunk
.gensec_skey
= &s
->gensec_skey
;
395 s
->chunk
.partition
= &s
->partition
;
396 s
->chunk
.forest
= &s
->forest
;
397 s
->chunk
.dest_dsa
= &s
->dest_dsa
;
399 return PyCObject_FromTallocPtr(s
);
404 process one replication chunk
406 static PyObject
*py_net_replicate_chunk(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
408 const char *kwnames
[] = { "state", "level", "ctr", "schema", NULL
};
409 PyObject
*py_state
, *py_ctr
, *py_schema
;
410 struct replicate_state
*s
;
412 NTSTATUS (*chunk_handler
)(void *private_data
, const struct libnet_BecomeDC_StoreChunk
*c
);
415 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OIO|O",
416 discard_const_p(char *, kwnames
),
417 &py_state
, &level
, &py_ctr
, &py_schema
)) {
421 s
= talloc_get_type(PyCObject_AsVoidPtr(py_state
), struct replicate_state
);
423 PyErr_SetString(PyExc_TypeError
, "Expected replication_state");
429 if (!py_check_dcerpc_type(py_ctr
, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr1")) {
432 s
->chunk
.ctr1
= py_talloc_get_ptr(py_ctr
);
433 s
->partition
.nc
= *s
->chunk
.ctr1
->naming_context
;
434 s
->partition
.more_data
= s
->chunk
.ctr1
->more_data
;
435 s
->partition
.source_dsa_guid
= s
->chunk
.ctr1
->source_dsa_guid
;
436 s
->partition
.source_dsa_invocation_id
= s
->chunk
.ctr1
->source_dsa_invocation_id
;
437 s
->partition
.highwatermark
= s
->chunk
.ctr1
->new_highwatermark
;
440 if (!py_check_dcerpc_type(py_ctr
, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr6")) {
443 s
->chunk
.ctr6
= py_talloc_get_ptr(py_ctr
);
444 s
->partition
.nc
= *s
->chunk
.ctr6
->naming_context
;
445 s
->partition
.more_data
= s
->chunk
.ctr6
->more_data
;
446 s
->partition
.source_dsa_guid
= s
->chunk
.ctr6
->source_dsa_guid
;
447 s
->partition
.source_dsa_invocation_id
= s
->chunk
.ctr6
->source_dsa_invocation_id
;
448 s
->partition
.highwatermark
= s
->chunk
.ctr6
->new_highwatermark
;
451 PyErr_Format(PyExc_TypeError
, "Bad level %u in replicate_chunk", level
);
455 chunk_handler
= libnet_vampire_cb_store_chunk
;
457 if (!PyBool_Check(py_schema
)) {
458 PyErr_SetString(PyExc_TypeError
, "Expected boolean schema");
461 if (py_schema
== Py_True
) {
462 chunk_handler
= libnet_vampire_cb_schema_chunk
;
466 s
->chunk
.ctr_level
= level
;
468 status
= chunk_handler(s
->vampire_state
, &s
->chunk
);
469 if (!NT_STATUS_IS_OK(status
)) {
470 PyErr_Format(PyExc_TypeError
, "Failed to process chunk: %s", nt_errstr(status
));
479 find a DC given a domain name and server type
481 static PyObject
*py_net_finddc(py_net_Object
*self
, PyObject
*args
)
483 const char *domain_name
;
484 unsigned server_type
;
490 if (!PyArg_ParseTuple(args
, "sI", &domain_name
, &server_type
)) {
494 mem_ctx
= talloc_new(self
->mem_ctx
);
496 io
= talloc_zero(mem_ctx
, struct finddcs
);
497 io
->in
.domain_name
= domain_name
;
498 io
->in
.minimum_dc_flags
= server_type
;
500 status
= finddcs_cldap(io
, io
,
501 lpcfg_resolve_context(self
->libnet_ctx
->lp_ctx
), self
->ev
);
502 if (NT_STATUS_IS_ERR(status
)) {
503 PyErr_SetString(PyExc_RuntimeError
, nt_errstr(status
));
504 talloc_free(mem_ctx
);
508 ret
= py_return_ndr_struct("samba.dcerpc.nbt", "NETLOGON_SAM_LOGON_RESPONSE_EX",
509 io
, &io
->out
.netlogon
.data
.nt5_ex
);
510 talloc_free(mem_ctx
);
516 static const char py_net_vampire_doc
[] = "vampire(domain, target_dir=None)\n"
519 static const char py_net_replicate_init_doc
[] = "replicate_init(samdb, lp, drspipe)\n"
520 "Setup for replicate_chunk calls.";
522 static const char py_net_replicate_chunk_doc
[] = "replicate_chunk(state, level, ctr, schema)\n"
523 "Process replication for one chunk";
525 static const char py_net_finddc_doc
[] = "finddc(domain, server_type)\n"
526 "find a DC with the specified server_type bits. Return the DNS name";
528 static PyMethodDef net_obj_methods
[] = {
529 {"join", (PyCFunction
)py_net_join
, METH_VARARGS
|METH_KEYWORDS
, py_net_join_doc
},
530 {"set_password", (PyCFunction
)py_net_set_password
, METH_VARARGS
|METH_KEYWORDS
, py_net_set_password_doc
},
531 {"export_keytab", (PyCFunction
)py_net_export_keytab
, METH_VARARGS
|METH_KEYWORDS
, py_net_export_keytab_doc
},
532 {"time", (PyCFunction
)py_net_time
, METH_VARARGS
|METH_KEYWORDS
, py_net_time_doc
},
533 {"create_user", (PyCFunction
)py_net_user_create
, METH_VARARGS
|METH_KEYWORDS
, py_net_create_user_doc
},
534 {"delete_user", (PyCFunction
)py_net_user_delete
, METH_VARARGS
|METH_KEYWORDS
, py_net_delete_user_doc
},
535 {"vampire", (PyCFunction
)py_net_vampire
, METH_VARARGS
|METH_KEYWORDS
, py_net_vampire_doc
},
536 {"replicate_init", (PyCFunction
)py_net_replicate_init
, METH_VARARGS
|METH_KEYWORDS
, py_net_replicate_init_doc
},
537 {"replicate_chunk", (PyCFunction
)py_net_replicate_chunk
, METH_VARARGS
|METH_KEYWORDS
, py_net_replicate_chunk_doc
},
538 {"finddc", (PyCFunction
)py_net_finddc
, METH_VARARGS
, py_net_finddc_doc
},
542 static void py_net_dealloc(py_net_Object
*self
)
544 talloc_free(self
->mem_ctx
);
548 static PyObject
*net_obj_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
550 PyObject
*py_creds
, *py_lp
= Py_None
;
551 const char *kwnames
[] = { "creds", "lp", "server", NULL
};
553 struct loadparm_context
*lp
;
554 const char *server_address
= NULL
;
556 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|Oz",
557 discard_const_p(char *, kwnames
), &py_creds
, &py_lp
,
561 ret
= PyObject_New(py_net_Object
, type
);
566 /* FIXME: we really need to get a context from the caller or we may end
567 * up with 2 event contexts */
568 ret
->ev
= s4_event_context_init(NULL
);
569 ret
->mem_ctx
= talloc_new(ret
->ev
);
571 lp
= lpcfg_from_py_object(ret
->mem_ctx
, py_lp
);
577 ret
->libnet_ctx
= libnet_context_init(ret
->ev
, lp
);
578 if (ret
->libnet_ctx
== NULL
) {
579 PyErr_SetString(PyExc_RuntimeError
, "Unable to initialize net");
584 ret
->libnet_ctx
->server_address
= server_address
;
586 ret
->libnet_ctx
->cred
= cli_credentials_from_py_object(py_creds
);
587 if (ret
->libnet_ctx
->cred
== NULL
) {
588 PyErr_SetString(PyExc_TypeError
, "Expected credentials object");
593 return (PyObject
*)ret
;
597 PyTypeObject py_net_Type
= {
598 PyObject_HEAD_INIT(NULL
) 0,
599 .tp_name
= "net.Net",
600 .tp_basicsize
= sizeof(py_net_Object
),
601 .tp_dealloc
= (destructor
)py_net_dealloc
,
602 .tp_methods
= net_obj_methods
,
603 .tp_new
= net_obj_new
,
610 if (PyType_Ready(&py_net_Type
) < 0)
613 m
= Py_InitModule3("net", NULL
, NULL
);
617 Py_INCREF(&py_net_Type
);
618 PyModule_AddObject(m
, "Net", (PyObject
*)&py_net_Type
);
619 PyModule_AddObject(m
, "LIBNET_JOINDOMAIN_AUTOMATIC", PyInt_FromLong(LIBNET_JOINDOMAIN_AUTOMATIC
));
620 PyModule_AddObject(m
, "LIBNET_JOINDOMAIN_SPECIFIED", PyInt_FromLong(LIBNET_JOINDOMAIN_SPECIFIED
));
621 PyModule_AddObject(m
, "LIBNET_JOIN_AUTOMATIC", PyInt_FromLong(LIBNET_JOIN_AUTOMATIC
));
622 PyModule_AddObject(m
, "LIBNET_JOIN_SPECIFIED", PyInt_FromLong(LIBNET_JOIN_SPECIFIED
));