python: Port the samba.net module to Python 3
[nivanova/samba-autobuild/.git] / source4 / libnet / py_net.c
index f2fc771413dc5f088fb8e8fab57cc2410fbbf684..bf39415cf32aee951733dbd8c65c51dc4df40267 100644 (file)
@@ -1,6 +1,7 @@
 /*
    Unix SMB/CIFS implementation.
    Samba utility functions
+
    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008-2010
    Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
 
 */
 
 #include <Python.h>
+#include "python/py3compat.h"
 #include "includes.h"
+#include <pyldb.h>
+#include <pytalloc.h>
 #include "libnet.h"
 #include "auth/credentials/pycredentials.h"
 #include "libcli/security/security.h"
 #include "lib/events/events.h"
-#include "param/param.h"
 #include "param/pyparam.h"
+#include "auth/gensec/gensec.h"
+#include "librpc/rpc/pyrpc_util.h"
+#include "libcli/resolve/resolve.h"
+#include "libcli/finddc.h"
+#include "dsdb/samdb/samdb.h"
+#include "py_net.h"
+#include "librpc/rpc/pyrpc_util.h"
+
+static void PyErr_SetDsExtendedError(enum drsuapi_DsExtendedError ext_err, const char *error_description)
+{
+       PyObject *error = PyObject_GetAttrString(PyImport_ImportModule("samba"),
+                                                "DsExtendedError");
+       if (error_description == NULL) {
+               switch (ext_err) {
+                       /* Copied out of ndr_drsuapi.c:ndr_print_drsuapi_DsExtendedError() */
+                       case DRSUAPI_EXOP_ERR_NONE:
+                               error_description = "DRSUAPI_EXOP_ERR_NONE";
+                               break;
+                       case DRSUAPI_EXOP_ERR_SUCCESS:
+                               error_description = "DRSUAPI_EXOP_ERR_SUCCESS";
+                               break;
+                       case DRSUAPI_EXOP_ERR_UNKNOWN_OP:
+                               error_description = "DRSUAPI_EXOP_ERR_UNKNOWN_OP";
+                               break;
+                       case DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER:
+                               error_description = "DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER";
+                               break;
+                       case DRSUAPI_EXOP_ERR_UPDATE_ERR:
+                               error_description = "DRSUAPI_EXOP_ERR_UPDATE_ERR";
+                               break;
+                       case DRSUAPI_EXOP_ERR_EXCEPTION:
+                               error_description = "DRSUAPI_EXOP_ERR_EXCEPTION";
+                               break;
+                       case DRSUAPI_EXOP_ERR_UNKNOWN_CALLER:
+                               error_description = "DRSUAPI_EXOP_ERR_UNKNOWN_CALLER";
+                               break;
+                       case DRSUAPI_EXOP_ERR_RID_ALLOC:
+                               error_description = "DRSUAPI_EXOP_ERR_RID_ALLOC";
+                               break;
+                       case DRSUAPI_EXOP_ERR_FSMO_OWNER_DELETED:
+                               error_description = "DRSUAPI_EXOP_ERR_FSMO_OWNER_DELETED";
+                               break;
+                       case DRSUAPI_EXOP_ERR_FMSO_PENDING_OP:
+                               error_description = "DRSUAPI_EXOP_ERR_FMSO_PENDING_OP";
+                               break;
+                       case DRSUAPI_EXOP_ERR_MISMATCH:
+                               error_description = "DRSUAPI_EXOP_ERR_MISMATCH";
+                               break;
+                       case DRSUAPI_EXOP_ERR_COULDNT_CONTACT:
+                               error_description = "DRSUAPI_EXOP_ERR_COULDNT_CONTACT";
+                               break;
+                       case DRSUAPI_EXOP_ERR_FSMO_REFUSING_ROLES:
+                               error_description = "DRSUAPI_EXOP_ERR_FSMO_REFUSING_ROLES";
+                               break;
+                       case DRSUAPI_EXOP_ERR_DIR_ERROR:
+                               error_description = "DRSUAPI_EXOP_ERR_DIR_ERROR";
+                               break;
+                       case DRSUAPI_EXOP_ERR_FSMO_MISSING_SETTINGS:
+                               error_description = "DRSUAPI_EXOP_ERR_FSMO_MISSING_SETTINGS";
+                               break;
+                       case DRSUAPI_EXOP_ERR_ACCESS_DENIED:
+                               error_description = "DRSUAPI_EXOP_ERR_ACCESS_DENIED";
+                               break;
+                       case DRSUAPI_EXOP_ERR_PARAM_ERROR:
+                               error_description = "DRSUAPI_EXOP_ERR_PARAM_ERROR";
+                               break;
+               }
+       }
+       PyErr_SetObject(error,
+                       Py_BuildValue(discard_const_p(char, "(i,s)"),
+                                     ext_err,
+                                     error_description));
+}
 
-typedef struct {
-       PyObject_HEAD
-       struct libnet_context *libnet_ctx;
-       TALLOC_CTX *mem_ctx;
-       struct tevent_context *ev;
-} py_net_Object;
-
-static PyObject *py_net_join(py_net_Object *self, PyObject *args, PyObject *kwargs)
+static PyObject *py_net_join_member(py_net_Object *self, PyObject *args, PyObject *kwargs)
 {
-       struct libnet_Join r;
+       struct libnet_Join_member r;
+       int _level = 0;
        NTSTATUS status;
        PyObject *result;
        TALLOC_CTX *mem_ctx;
-       PyObject *py_creds;     
-       const char *kwnames[] = { "domain_name", "netbios_name", "join_type", "level", "credentials", NULL };
+       const char *kwnames[] = { "domain_name", "netbios_name", "level", "machinepass", NULL };
+
+       ZERO_STRUCT(r);
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ssiiO:Join", discard_const_p(char *, kwnames), 
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ssi|z:Join", discard_const_p(char *, kwnames),
                                         &r.in.domain_name, &r.in.netbios_name, 
-                                        &r.in.join_type, &r.in.level, &py_creds))
+                                        &_level,
+                                        &r.in.account_pass)) {
                return NULL;
+       }
+       r.in.level = _level;
 
        mem_ctx = talloc_new(self->mem_ctx);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
 
-       status = libnet_Join(self->libnet_ctx, mem_ctx, &r);
+       status = libnet_Join_member(self->libnet_ctx, mem_ctx, &r);
        if (NT_STATUS_IS_ERR(status)) {
-               PyErr_SetString(PyExc_RuntimeError, r.out.error_string);
+               PyErr_SetNTSTATUS_and_string(status,
+                                            r.out.error_string
+                                            ? r.out.error_string
+                                            : nt_errstr(status));
                talloc_free(mem_ctx);
                return NULL;
        }
@@ -66,34 +147,46 @@ static PyObject *py_net_join(py_net_Object *self, PyObject *args, PyObject *kwar
        return result;
 }
 
-static const char py_net_join_doc[] = "join(domain_name, netbios_name, join_type, level) -> (join_password, domain_sid, domain_name)\n\n" \
+static const char py_net_join_member_doc[] = "join_member(domain_name, netbios_name, level) -> (join_password, domain_sid, domain_name)\n\n" \
 "Join the domain with the specified name.";
 
-static PyObject *py_net_set_password(py_net_Object *self, PyObject *args, PyObject *kwargs)
+static PyObject *py_net_change_password(py_net_Object *self, PyObject *args, PyObject *kwargs)
 {
-       union libnet_SetPassword r;
+       union libnet_ChangePassword r;
        NTSTATUS status;
-       PyObject *py_creds;
        TALLOC_CTX *mem_ctx;
        struct tevent_context *ev;
-       const char *kwnames[] = { "account_name", "domain_name", "newpassword", "credentials", NULL };
+       const char *kwnames[] = { "newpassword", NULL };
 
-       r.generic.level = LIBNET_SET_PASSWORD_GENERIC;
+       ZERO_STRUCT(r);
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sssO:set_password", discard_const_p(char *, kwnames),
-                                        &r.generic.in.account_name, &r.generic.in.domain_name,
-                                        &r.generic.in.newpassword, &py_creds)) {
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:change_password",
+                                       discard_const_p(char *, kwnames),
+                                       &r.generic.in.newpassword)) {
                return NULL;
        }
 
+       r.generic.level = LIBNET_CHANGE_PASSWORD_GENERIC;
+       r.generic.in.account_name = cli_credentials_get_username(self->libnet_ctx->cred);
+       r.generic.in.domain_name = cli_credentials_get_domain(self->libnet_ctx->cred);
+       r.generic.in.oldpassword = cli_credentials_get_password(self->libnet_ctx->cred);
+
        /* FIXME: we really need to get a context from the caller or we may end
         * up with 2 event contexts */
        ev = s4_event_context_init(NULL);
+
        mem_ctx = talloc_new(ev);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
 
-       status = libnet_SetPassword(self->libnet_ctx, mem_ctx, &r);
+       status = libnet_ChangePassword(self->libnet_ctx, mem_ctx, &r);
        if (NT_STATUS_IS_ERR(status)) {
-               PyErr_SetString(PyExc_RuntimeError, r.generic.out.error_string);
+               PyErr_SetNTSTATUS_and_string(status,
+                                            r.generic.out.error_string
+                                            ? r.generic.out.error_string
+                                            : nt_errstr(status));
                talloc_free(mem_ctx);
                return NULL;
        }
@@ -103,31 +196,48 @@ static PyObject *py_net_set_password(py_net_Object *self, PyObject *args, PyObje
        Py_RETURN_NONE;
 }
 
-static const char py_net_set_password_doc[] = "set_password(account_name, domain_name, newpassword) -> True\n\n" \
-"Set password for a user. You must supply credential with enough rights to do this.\n\n" \
+static const char py_net_change_password_doc[] = "change_password(newpassword) -> True\n\n" \
+"Change password for a user. You must supply credential with enough rights to do this.\n\n" \
 "Sample usage is:\n" \
-"net.set_password(account_name=<account_name>,\n" \
-"                domain_name=domain_name,\n" \
-"                newpassword=new_pass)\n";
+"net.change_password(newpassword=<new_password>)\n";
 
 
-static PyObject *py_net_export_keytab(py_net_Object *self, PyObject *args, PyObject *kwargs)
+static PyObject *py_net_set_password(py_net_Object *self, PyObject *args, PyObject *kwargs)
 {
-       struct libnet_export_keytab r;
-       TALLOC_CTX *mem_ctx;
-       const char *kwnames[] = { "keytab", NULL };
+       union libnet_SetPassword r;
        NTSTATUS status;
+       TALLOC_CTX *mem_ctx;
+       struct tevent_context *ev;
+       const char *kwnames[] = { "account_name", "domain_name", "newpassword", NULL };
+
+       ZERO_STRUCT(r);
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:export_keytab", discard_const_p(char *, kwnames),
-                                        &r.in.keytab_name)) {
+       r.generic.level = LIBNET_SET_PASSWORD_GENERIC;
+
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sss:set_password",
+                                       discard_const_p(char *, kwnames),
+                                        &r.generic.in.account_name,
+                                        &r.generic.in.domain_name,
+                                        &r.generic.in.newpassword)) {
                return NULL;
        }
 
-       mem_ctx = talloc_new(self->mem_ctx);
+       /* FIXME: we really need to get a context from the caller or we may end
+        * up with 2 event contexts */
+       ev = s4_event_context_init(NULL);
 
-       status = libnet_export_keytab(self->libnet_ctx, mem_ctx, &r);
+       mem_ctx = talloc_new(ev);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+
+       status = libnet_SetPassword(self->libnet_ctx, mem_ctx, &r);
        if (NT_STATUS_IS_ERR(status)) {
-               PyErr_SetString(PyExc_RuntimeError, r.out.error_string);
+               PyErr_SetNTSTATUS_and_string(status,
+                                            r.generic.out.error_string
+                                            ? r.generic.out.error_string
+                                            : nt_errstr(status));
                talloc_free(mem_ctx);
                return NULL;
        }
@@ -137,8 +247,11 @@ static PyObject *py_net_export_keytab(py_net_Object *self, PyObject *args, PyObj
        Py_RETURN_NONE;
 }
 
-static const char py_net_export_keytab_doc[] = "export_keytab(keytab, name)\n\n"
-"Export the DC keytab to a keytab file.";
+static const char py_net_set_password_doc[] = "set_password(account_name, domain_name, newpassword) -> True\n\n" \
+"Set password for a user. You must supply credential with enough rights to do this.\n\n" \
+"Sample usage is:\n" \
+"net.set_password(account_name=account_name, domain_name=domain_name, newpassword=new_pass)\n";
+
 
 static PyObject *py_net_time(py_net_Object *self, PyObject *args, PyObject *kwargs)
 {
@@ -164,7 +277,10 @@ static PyObject *py_net_time(py_net_Object *self, PyObject *args, PyObject *kwar
 
        status = libnet_RemoteTOD(self->libnet_ctx, mem_ctx, &r);
        if (!NT_STATUS_IS_OK(status)) {
-               PyErr_SetString(PyExc_RuntimeError, r.generic.out.error_string);
+               PyErr_SetNTSTATUS_and_string(status,
+                                            r.generic.out.error_string
+                                            ? r.generic.out.error_string
+                                            : nt_errstr(status));
                talloc_free(mem_ctx);
                return NULL;
        }
@@ -173,7 +289,7 @@ static PyObject *py_net_time(py_net_Object *self, PyObject *args, PyObject *kwar
        tm = localtime(&r.generic.out.time);
        strftime(timestr, sizeof(timestr)-1, "%c %Z",tm);
        
-       ret = PyString_FromString(timestr);
+       ret = PyStr_FromString(timestr);
 
        talloc_free(mem_ctx);
 
@@ -204,7 +320,10 @@ static PyObject *py_net_user_create(py_net_Object *self, PyObject *args, PyObjec
 
        status = libnet_CreateUser(self->libnet_ctx, mem_ctx, &r);
        if (!NT_STATUS_IS_OK(status)) {
-               PyErr_SetString(PyExc_RuntimeError, r.out.error_string);
+               PyErr_SetNTSTATUS_and_string(status,
+                                            r.out.error_string
+                                            ? r.out.error_string
+                                            : nt_errstr(status));
                talloc_free(mem_ctx);
                return NULL;
        }
@@ -238,7 +357,10 @@ static PyObject *py_net_user_delete(py_net_Object *self, PyObject *args, PyObjec
 
        status = libnet_DeleteUser(self->libnet_ctx, mem_ctx, &r);
        if (!NT_STATUS_IS_OK(status)) {
-               PyErr_SetString(PyExc_RuntimeError, r.out.error_string);
+               PyErr_SetNTSTATUS_and_string(status,
+                                          r.out.error_string
+                                         ? r.out.error_string
+                                         : nt_errstr(status));
                talloc_free(mem_ctx);
                return NULL;
        }
@@ -251,86 +373,313 @@ static PyObject *py_net_user_delete(py_net_Object *self, PyObject *args, PyObjec
 static const char py_net_delete_user_doc[] = "delete_user(username)\n"
 "Delete a user.";
 
-static PyObject *py_dom_sid_FromSid(struct dom_sid *sid)
+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;
+};
+
+/*
+  setup for replicate_chunk() calls
+ */
+static PyObject *py_net_replicate_init(py_net_Object *self, PyObject *args, PyObject *kwargs)
 {
-       PyObject *mod_security, *dom_sid_Type;
+       const char *kwnames[] = { "samdb", "lp", "drspipe", "invocation_id", NULL };
+       PyObject *py_ldb, *py_lp, *py_drspipe, *py_invocation_id;
+       struct ldb_context *samdb;
+       struct loadparm_context *lp;
+       struct replicate_state *s;
+       NTSTATUS status;
 
-       mod_security = PyImport_ImportModule("samba.dcerpc.security");
-       if (mod_security == NULL)
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOOO",
+                                        discard_const_p(char *, kwnames),
+                                        &py_ldb, &py_lp, &py_drspipe,
+                                        &py_invocation_id)) {
                return NULL;
+       }
 
-       dom_sid_Type = PyObject_GetAttrString(mod_security, "dom_sid");
-       if (dom_sid_Type == NULL)
+       s = talloc_zero(NULL, struct replicate_state);
+       if (!s) return NULL;
+
+       lp = lpcfg_from_py_object(s, py_lp);
+       if (lp == NULL) {
+               PyErr_SetString(PyExc_TypeError, "Expected lp object");
+               talloc_free(s);
                return NULL;
+       }
 
-       return py_talloc_reference((PyTypeObject *)dom_sid_Type, sid);
+       samdb = pyldb_Ldb_AsLdbContext(py_ldb);
+       if (samdb == NULL) {
+               PyErr_SetString(PyExc_TypeError, "Expected ldb object");
+               talloc_free(s);
+               return NULL;
+       }
+       if (!py_check_dcerpc_type(py_invocation_id, "samba.dcerpc.misc", "GUID")) {
+               
+               talloc_free(s);
+               return NULL;
+       }
+       s->dest_dsa.invocation_id = *pytalloc_get_type(py_invocation_id, struct GUID);
+
+       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,
+                                   &s->gensec_skey);
+       if (!NT_STATUS_IS_OK(status)) {
+               char *error_string = talloc_asprintf(s,
+                                                    "Unable to get session key from drspipe: %s",
+                                                    nt_errstr(status));
+               PyErr_SetNTSTATUS_and_string(status, error_string);
+               talloc_free(s);
+               return NULL;
+       }
+
+       s->forest.dns_name = samdb_dn_to_dns_domain(s, ldb_get_root_basedn(samdb));
+       s->forest.root_dn_str = ldb_dn_get_linearized(ldb_get_root_basedn(samdb));
+       s->forest.config_dn_str = ldb_dn_get_linearized(ldb_get_config_basedn(samdb));
+       s->forest.schema_dn_str = ldb_dn_get_linearized(ldb_get_schema_basedn(samdb));
+
+       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 pytalloc_GenericObject_steal(s);
 }
 
-static PyObject *py_net_vampire(py_net_Object *self, PyObject *args, PyObject *kwargs)
+
+/*
+  process one replication chunk
+ */
+static PyObject *py_net_replicate_chunk(py_net_Object *self, PyObject *args, PyObject *kwargs)
 {
-       const char *kwnames[] = { "domain", "target_dir", NULL };
-       NTSTATUS status;
-       TALLOC_CTX *mem_ctx;
-    PyObject *ret;
-       struct libnet_Vampire r;
+       const char *kwnames[] = { "state", "level", "ctr",
+                                 "schema", "req_level", "req",
+                                 NULL };
+       PyObject *py_state, *py_ctr, *py_schema = Py_None, *py_req = Py_None;
+       struct replicate_state *s;
+       unsigned level;
+       unsigned req_level = 0;
+       WERROR (*chunk_handler)(void *private_data, const struct libnet_BecomeDC_StoreChunk *c);
+       WERROR werr;
+       enum drsuapi_DsExtendedError extended_ret = DRSUAPI_EXOP_ERR_NONE;
+       enum drsuapi_DsExtendedOperation exop = DRSUAPI_EXOP_NONE;
+
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OIO|OIO",
+                                        discard_const_p(char *, kwnames),
+                                        &py_state, &level, &py_ctr,
+                                        &py_schema, &req_level, &py_req)) {
+               return NULL;
+       }
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|s", discard_const_p(char *, kwnames), 
-                                                                        &r.in.domain_name, &r.in.targetdir))
+       s = pytalloc_get_type(py_state, struct replicate_state);
+       if (!s) {
                return NULL;
+       }
 
-       r.in.netbios_name  = lp_netbios_name(self->libnet_ctx->lp_ctx);
-       r.out.error_string = NULL;
+       switch (level) {
+       case 1:
+               if (!py_check_dcerpc_type(py_ctr, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr1")) {
+                       return NULL;
+               }
+               s->chunk.ctr1                         = pytalloc_get_ptr(py_ctr);
+               if (s->chunk.ctr1->naming_context != NULL) {
+                       s->partition.nc = *s->chunk.ctr1->naming_context;
+               }
+               extended_ret = s->chunk.ctr1->extended_ret;
+               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 (!py_check_dcerpc_type(py_ctr, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr6")) {
+                       return NULL;
+               }
+               s->chunk.ctr6                         = pytalloc_get_ptr(py_ctr);
+               if (s->chunk.ctr6->naming_context != NULL) {
+                       s->partition.nc = *s->chunk.ctr6->naming_context;
+               }
+               extended_ret = s->chunk.ctr6->extended_ret;
+               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;
+       }
 
-    mem_ctx = talloc_new(NULL);
-    if (mem_ctx == NULL) {
-        PyErr_NoMemory();
-        return NULL;
-    }
+       s->chunk.req5 = NULL;
+       s->chunk.req8 = NULL;
+       s->chunk.req10 = NULL;
+       if (py_req) {
+               switch (req_level) {
+               case 0:
+                       break;
+               case 5:
+                       if (!py_check_dcerpc_type(py_req, "samba.dcerpc.drsuapi", "DsGetNCChangesRequest5")) {
+                               return NULL;
+                       }
+
+                       s->chunk.req5 = pytalloc_get_ptr(py_req);
+                       exop = s->chunk.req5->extended_op;
+                       break;
+               case 8:
+                       if (!py_check_dcerpc_type(py_req, "samba.dcerpc.drsuapi", "DsGetNCChangesRequest8")) {
+                               return NULL;
+                       }
+
+                       s->chunk.req8 = pytalloc_get_ptr(py_req);
+                       exop = s->chunk.req8->extended_op;
+                       break;
+               case 10:
+                       if (!py_check_dcerpc_type(py_req, "samba.dcerpc.drsuapi", "DsGetNCChangesRequest10")) {
+                               return NULL;
+                       }
+
+                       s->chunk.req10 = pytalloc_get_ptr(py_req);
+                       exop = s->chunk.req10->extended_op;
+                       break;
+               default:
+                       PyErr_Format(PyExc_TypeError, "Bad req_level %u in replicate_chunk", req_level);
+                       return NULL;
+               }
+       }
 
-       status = libnet_Vampire(self->libnet_ctx, mem_ctx, &r);
+       if (exop != DRSUAPI_EXOP_NONE && extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
+               PyErr_SetDsExtendedError(extended_ret, NULL);
+               return NULL;
+       }
 
-       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);
+       s->chunk.req_level = req_level;
+
+       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;
+
+       werr = chunk_handler(s->vampire_state, &s->chunk);
+       if (!W_ERROR_IS_OK(werr)) {
+               char *error_string
+                       = talloc_asprintf(NULL,
+                                         "Failed to process 'chunk' of DRS replicated objects: %s",
+                                         win_errstr(werr));
+               PyErr_SetWERROR_and_string(werr, error_string);
+               TALLOC_FREE(error_string);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+
+/*
+  find a DC given a domain name and server type
+ */
+static PyObject *py_net_finddc(py_net_Object *self, PyObject *args, PyObject *kwargs)
+{
+       const char *domain = NULL, *address = NULL;
+       unsigned server_type;
+       NTSTATUS status;
+       struct finddcs *io;
+       TALLOC_CTX *mem_ctx;
+       PyObject *ret;
+       const char * const kwnames[] = { "flags", "domain", "address", NULL };
+
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "I|zz",
+                                        discard_const_p(char *, kwnames),
+                                        &server_type, &domain, &address)) {
                return NULL;
        }
 
-    ret = Py_BuildValue("(sO)", r.out.domain_name, py_dom_sid_FromSid(r.out.domain_sid));
+       mem_ctx = talloc_new(self->mem_ctx);
+
+       io = talloc_zero(mem_ctx, struct finddcs);
+       if (domain != NULL) {
+               io->in.domain_name = domain;
+       }
+       if (address != NULL) {
+               io->in.server_address = address;
+       }
+       io->in.minimum_dc_flags = server_type;
+
+       status = finddcs_cldap(io, io,
+                              lpcfg_resolve_context(self->libnet_ctx->lp_ctx), self->ev);
+       if (NT_STATUS_IS_ERR(status)) {
+               PyErr_SetNTSTATUS(status);
+               talloc_free(mem_ctx);
+               return NULL;
+       }
 
-    talloc_free(mem_ctx);
+       ret = py_return_ndr_struct("samba.dcerpc.nbt", "NETLOGON_SAM_LOGON_RESPONSE_EX",
+                                  io, &io->out.netlogon.data.nt5_ex);
+       talloc_free(mem_ctx);
 
-    return ret;
+       return ret;
 }
 
-static const char py_net_vampire_doc[] = "vampire(domain, target_dir=None)\n"
-"Vampire 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 const char py_net_finddc_doc[] = "finddc(flags=server_type, domain=None, address=None)\n"
+                                        "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";
 
 static PyMethodDef net_obj_methods[] = {
-       {"join", (PyCFunction)py_net_join, METH_VARARGS|METH_KEYWORDS, py_net_join_doc},
+       {"join_member", (PyCFunction)py_net_join_member, METH_VARARGS|METH_KEYWORDS, py_net_join_member_doc},
+       {"change_password", (PyCFunction)py_net_change_password, METH_VARARGS|METH_KEYWORDS, py_net_change_password_doc},
        {"set_password", (PyCFunction)py_net_set_password, METH_VARARGS|METH_KEYWORDS, py_net_set_password_doc},
-       {"export_keytab", (PyCFunction)py_net_export_keytab, METH_VARARGS|METH_KEYWORDS, py_net_export_keytab_doc},
        {"time", (PyCFunction)py_net_time, METH_VARARGS|METH_KEYWORDS, py_net_time_doc},
        {"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_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},
+       {"finddc", (PyCFunction)py_net_finddc, METH_KEYWORDS, py_net_finddc_doc},
        { NULL }
 };
 
 static void py_net_dealloc(py_net_Object *self)
 {
        talloc_free(self->mem_ctx);
+       PyObject_Del(self);
 }
 
 static PyObject *net_obj_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 {
        PyObject *py_creds, *py_lp = Py_None;
-       const char *kwnames[] = { "creds", "lp", NULL };
+       const char *kwnames[] = { "creds", "lp", "server", NULL };
        py_net_Object *ret;
        struct loadparm_context *lp;
+       const char *server_address = NULL;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", 
-                       discard_const_p(char *, kwnames), &py_creds, &py_lp))
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oz",
+                                        discard_const_p(char *, kwnames), &py_creds, &py_lp,
+                                        &server_address))
                return NULL;
 
        ret = PyObject_New(py_net_Object, type);
@@ -343,7 +692,7 @@ static PyObject *net_obj_new(PyTypeObject *type, PyObject *args, PyObject *kwarg
        ret->ev = s4_event_context_init(NULL);
        ret->mem_ctx = talloc_new(ret->ev);
 
-       lp = lp_from_py_object(ret->mem_ctx, py_lp);
+       lp = lpcfg_from_py_object(ret->mem_ctx, py_lp);
        if (lp == NULL) {
                Py_DECREF(ret);
                return NULL;
@@ -356,6 +705,8 @@ static PyObject *net_obj_new(PyTypeObject *type, PyObject *args, PyObject *kwarg
                return NULL;
        }
 
+       ret->libnet_ctx->server_address = server_address;
+
        ret->libnet_ctx->cred = cli_credentials_from_py_object(py_creds);
        if (ret->libnet_ctx->cred == NULL) {
                PyErr_SetString(PyExc_TypeError, "Expected credentials object");
@@ -368,7 +719,7 @@ static PyObject *net_obj_new(PyTypeObject *type, PyObject *args, PyObject *kwarg
 
 
 PyTypeObject py_net_Type = {
-       PyObject_HEAD_INIT(NULL) 0,
+       PyVarObject_HEAD_INIT(NULL, 0)
        .tp_name = "net.Net",
        .tp_basicsize = sizeof(py_net_Object),
        .tp_dealloc = (destructor)py_net_dealloc,
@@ -376,17 +727,29 @@ PyTypeObject py_net_Type = {
        .tp_new = net_obj_new,
 };
 
-void initnet(void)
+static struct PyModuleDef moduledef = {
+       PyModuleDef_HEAD_INIT,
+       .m_name = "net",
+       .m_size = -1,
+};
+
+MODULE_INIT_FUNC(net)
 {
        PyObject *m;
 
        if (PyType_Ready(&py_net_Type) < 0)
-               return;
+               return NULL;
 
-       m = Py_InitModule3("net", NULL, NULL);
+       m = PyModule_Create(&moduledef);
        if (m == NULL)
-               return;
+               return NULL;
 
        Py_INCREF(&py_net_Type);
        PyModule_AddObject(m, "Net", (PyObject *)&py_net_Type);
+       PyModule_AddIntConstant(m, "LIBNET_JOINDOMAIN_AUTOMATIC", LIBNET_JOINDOMAIN_AUTOMATIC);
+       PyModule_AddIntConstant(m, "LIBNET_JOINDOMAIN_SPECIFIED", LIBNET_JOINDOMAIN_SPECIFIED);
+       PyModule_AddIntConstant(m, "LIBNET_JOIN_AUTOMATIC", LIBNET_JOIN_AUTOMATIC);
+       PyModule_AddIntConstant(m, "LIBNET_JOIN_SPECIFIED", LIBNET_JOIN_SPECIFIED);
+
+       return m;
 }