From 4cacdf00020c808ae8e3ef59378da682ce346133 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 24 Aug 2010 15:41:40 +1000 Subject: s4-pynet: added replicate_init() and replicate_chunk() calls these calls allow python code to pass chunks from DRS replication calls into the code that applies the chunks to a database Pair-Programmed-With: Andrew Bartlett --- source4/libnet/py_net.c | 169 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 134 insertions(+), 35 deletions(-) (limited to 'source4/libnet/py_net.c') diff --git a/source4/libnet/py_net.c b/source4/libnet/py_net.c index d4a4670f13..0b1eb7b916 100644 --- a/source4/libnet/py_net.c +++ b/source4/libnet/py_net.c @@ -26,6 +26,9 @@ #include "lib/events/events.h" #include "param/param.h" #include "param/pyparam.h" +#include "lib/ldb/pyldb.h" +#include "auth/gensec/gensec.h" +#include "librpc/rpc/pyrpc.h" typedef struct { PyObject_HEAD @@ -306,58 +309,150 @@ static PyObject *py_net_vampire(py_net_Object *self, PyObject *args, PyObject *k return ret; } +struct replicate_state { + void *vampire_state; + dcerpc_InterfaceObject *drs_pipe; + struct libnet_BecomeDC_StoreChunk chunk; + DATA_BLOB gensec_skey; + struct libnet_BecomeDC_Partition partition; + struct libnet_BecomeDC_Forest forest; + struct libnet_BecomeDC_DestDSA dest_dsa; +}; -static PyObject *py_net_replicate(py_net_Object *self, PyObject *args, PyObject *kwargs) +/* + setup for replicate_chunk() calls + */ +static PyObject *py_net_replicate_init(py_net_Object *self, PyObject *args, PyObject *kwargs) { - const char *kwnames[] = { "domain", "netbios_name", - "domain_sid", "realm", "server", "join_password", - "kvno", "target_dir", NULL }; + const char *kwnames[] = { "samdb", "lp", "drspipe", NULL }; + PyObject *py_ldb, *py_lp, *py_drspipe; + struct ldb_context *samdb; + struct loadparm_context *lp; + struct replicate_state *s; NTSTATUS status; - TALLOC_CTX *mem_ctx; - struct libnet_Replicate r; - unsigned kvno; - const char *sidstr; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ssssssI|z", + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOO", discard_const_p(char *, kwnames), - &r.in.domain_name, - &r.in.netbios_name, - &sidstr, - &r.in.realm, - &r.in.server, - &r.in.join_password, - &kvno, - &r.in.targetdir)) { + &py_ldb, &py_lp, &py_drspipe)) { return NULL; } - r.out.error_string = NULL; + s = talloc_zero(NULL, struct replicate_state); + if (!s) return NULL; - mem_ctx = talloc_new(NULL); - if (mem_ctx == NULL) { - PyErr_NoMemory(); + lp = lpcfg_from_py_object(s, py_lp); + if (lp == NULL) { + PyErr_SetString(PyExc_TypeError, "Expected lp object"); + talloc_free(s); return NULL; } - r.in.kvno = kvno; - r.in.domain_sid = dom_sid_parse_talloc(mem_ctx, sidstr); - - if (!r.in.domain_sid) { - PyErr_Format(PyExc_RuntimeError, "Bad domain_sid %s", sidstr); - talloc_free(mem_ctx); + samdb = PyLdb_AsLdbContext(py_ldb); + if (samdb == NULL) { + PyErr_SetString(PyExc_TypeError, "Expected ldb object"); + talloc_free(s); return NULL; } - status = libnet_Replicate(self->libnet_ctx, mem_ctx, &r); + s->drs_pipe = (dcerpc_InterfaceObject *)(py_drspipe); + s->vampire_state = libnet_vampire_replicate_init(s, samdb, lp); + if (s->vampire_state == NULL) { + PyErr_SetString(PyExc_TypeError, "Failed to initialise vampire_state"); + talloc_free(s); + return NULL; + } + + status = gensec_session_key(s->drs_pipe->pipe->conn->security_state.generic_state, + &s->gensec_skey); if (!NT_STATUS_IS_OK(status)) { - PyErr_SetString(PyExc_RuntimeError, - r.out.error_string ? r.out.error_string : nt_errstr(status)); - talloc_free(mem_ctx); + PyErr_Format(PyExc_RuntimeError, "Unable to get session key from drspipe: %s", + nt_errstr(status)); + talloc_free(s); return NULL; } - talloc_free(mem_ctx); + s->chunk.gensec_skey = &s->gensec_skey; + s->chunk.partition = &s->partition; + s->chunk.forest = &s->forest; + s->chunk.dest_dsa = &s->dest_dsa; + + return PyCObject_FromTallocPtr(s); +} + + +/* + process one replication chunk + */ +static PyObject *py_net_replicate_chunk(py_net_Object *self, PyObject *args, PyObject *kwargs) +{ + const char *kwnames[] = { "state", "level", "ctr", "schema", NULL }; + PyObject *py_state, *py_ctr, *py_schema; + struct replicate_state *s; + unsigned level; + NTSTATUS (*chunk_handler)(void *private_data, const struct libnet_BecomeDC_StoreChunk *c); + NTSTATUS status; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OIO|O", + discard_const_p(char *, kwnames), + &py_state, &level, &py_ctr, &py_schema)) { + return NULL; + } + + s = talloc_get_type(PyCObject_AsVoidPtr(py_state), struct replicate_state); + if (!s) { + PyErr_SetString(PyExc_TypeError, "Expected replication_state"); + return NULL; + } + + switch (level) { + case 1: + if (strcmp("drsuapi.DsGetNCChangesCtr1", Py_TYPE(py_ctr)->tp_name) != 0) { + PyErr_SetString(PyExc_TypeError, "Expected DsGetNCChangesCtr1 type for ctr"); + return NULL; + } + s->chunk.ctr1 = py_talloc_get_ptr(py_ctr); + s->partition.nc = *s->chunk.ctr1->naming_context; + s->partition.more_data = s->chunk.ctr1->more_data; + s->partition.source_dsa_guid = s->chunk.ctr1->source_dsa_guid; + s->partition.source_dsa_invocation_id = s->chunk.ctr1->source_dsa_invocation_id; + s->partition.highwatermark = s->chunk.ctr1->new_highwatermark; + break; + case 6: + if (strcmp("drsuapi.DsGetNCChangesCtr6", Py_TYPE(py_ctr)->tp_name) != 0) { + PyErr_SetString(PyExc_TypeError, "Expected DsGetNCChangesCtr6 type for ctr"); + return NULL; + } + s->chunk.ctr6 = py_talloc_get_ptr(py_ctr); + s->partition.nc = *s->chunk.ctr6->naming_context; + s->partition.more_data = s->chunk.ctr6->more_data; + s->partition.source_dsa_guid = s->chunk.ctr6->source_dsa_guid; + s->partition.source_dsa_invocation_id = s->chunk.ctr6->source_dsa_invocation_id; + s->partition.highwatermark = s->chunk.ctr6->new_highwatermark; + break; + default: + PyErr_Format(PyExc_TypeError, "Bad level %u in replicate_chunk", level); + return NULL; + } + + chunk_handler = libnet_vampire_cb_store_chunk; + if (py_schema) { + if (!PyBool_Check(py_schema)) { + PyErr_SetString(PyExc_TypeError, "Expected boolean schema"); + return NULL; + } + if (py_schema == Py_True) { + chunk_handler = libnet_vampire_cb_schema_chunk; + } + } + + s->chunk.ctr_level = level; + + status = chunk_handler(s->vampire_state, &s->chunk); + if (!NT_STATUS_IS_OK(status)) { + PyErr_Format(PyExc_TypeError, "Failed to process chunk: %s", nt_errstr(status)); + return NULL; + } return Py_None; } @@ -365,8 +460,11 @@ static PyObject *py_net_replicate(py_net_Object *self, PyObject *args, PyObject static const char py_net_vampire_doc[] = "vampire(domain, target_dir=None)\n" "Vampire a domain."; -static const char py_net_replicate_doc[] = "replicate(domain, netbios_name, domain_sid, realm, server, join_password, kvno, target_dir=None)\n" - "Replicate a domain."; +static const char py_net_replicate_init_doc[] = "replicate_init(samdb, lp, drspipe)\n" + "Setup for replicate_chunk calls."; + +static const char py_net_replicate_chunk_doc[] = "replicate_chunk(state, level, ctr, schema)\n" + "Process replication for one chunk"; static PyMethodDef net_obj_methods[] = { {"join", (PyCFunction)py_net_join, METH_VARARGS|METH_KEYWORDS, py_net_join_doc}, @@ -376,7 +474,8 @@ static PyMethodDef net_obj_methods[] = { {"create_user", (PyCFunction)py_net_user_create, METH_VARARGS|METH_KEYWORDS, py_net_create_user_doc}, {"delete_user", (PyCFunction)py_net_user_delete, METH_VARARGS|METH_KEYWORDS, py_net_delete_user_doc}, {"vampire", (PyCFunction)py_net_vampire, METH_VARARGS|METH_KEYWORDS, py_net_vampire_doc}, - {"replicate", (PyCFunction)py_net_replicate, METH_VARARGS|METH_KEYWORDS, py_net_replicate_doc}, + {"replicate_init", (PyCFunction)py_net_replicate_init, METH_VARARGS|METH_KEYWORDS, py_net_replicate_init_doc}, + {"replicate_chunk", (PyCFunction)py_net_replicate_chunk, METH_VARARGS|METH_KEYWORDS, py_net_replicate_chunk_doc}, { NULL } }; -- cgit