Fix spelling mistakes
[bbaumbach/samba-autobuild/.git] / source4 / libnet / libnet_domain.c
index 500d5f8..a7499b8 100644 (file)
@@ -40,16 +40,17 @@ struct domain_open_samr_state {
        uint32_t                  access_mask;
        struct policy_handle      connect_handle;
        struct policy_handle      domain_handle;
+       struct dom_sid2           *domain_sid;
 
        /* information about the progress */
        void (*monitor_fn)(struct monitor_msg*);
 };
 
 
-static void continue_domain_open_close(struct rpc_request *req);
-static void continue_domain_open_connect(struct rpc_request *req);
-static void continue_domain_open_lookup(struct rpc_request *req);
-static void continue_domain_open_open(struct rpc_request *req);
+static void continue_domain_open_close(struct tevent_req *subreq);
+static void continue_domain_open_connect(struct tevent_req *subreq);
+static void continue_domain_open_lookup(struct tevent_req *subreq);
+static void continue_domain_open_open(struct tevent_req *subreq);
 
 
 /**
@@ -59,10 +60,10 @@ static void continue_domain_open_rpc_connect(struct composite_context *ctx)
 {
        struct composite_context *c;
        struct domain_open_samr_state *s;
-       struct rpc_request *conn_req;
+       struct tevent_req *subreq;
 
-       c = talloc_get_type(ctx->async.private_data, struct composite_context);
-       s = talloc_get_type(c->private_data, struct domain_open_samr_state);
+       c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
+       s = talloc_get_type_abort(c->private_data, struct domain_open_samr_state);
 
        c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->rpcconn);
        if (!composite_is_ok(c)) return;
@@ -75,11 +76,13 @@ static void continue_domain_open_rpc_connect(struct composite_context *ctx)
        s->connect.out.connect_handle  = &s->connect_handle;
 
        /* send request */
-       conn_req = dcerpc_samr_Connect_send(s->pipe, c, &s->connect);
-       if (composite_nomem(conn_req, c)) return;
+       subreq = dcerpc_samr_Connect_r_send(s, c->event_ctx,
+                                           s->pipe->binding_handle,
+                                           &s->connect);
+       if (composite_nomem(subreq, c)) return;
 
        /* callback handler */
-       composite_continue_rpc(c, conn_req, continue_domain_open_connect, c);
+       tevent_req_set_callback(subreq, continue_domain_open_connect, c);
 }
 
 
@@ -87,17 +90,17 @@ static void continue_domain_open_rpc_connect(struct composite_context *ctx)
  * Stage 0.5 (optional): Close existing (in libnet context) domain
  * handle
  */
-static void continue_domain_open_close(struct rpc_request *req)
+static void continue_domain_open_close(struct tevent_req *subreq)
 {
        struct composite_context *c;
        struct domain_open_samr_state *s;
-       struct rpc_request *conn_req;
 
-       c = talloc_get_type(req->async.private_data, struct composite_context);
-       s = talloc_get_type(c->private_data, struct domain_open_samr_state);
+       c = tevent_req_callback_data(subreq, struct composite_context);
+       s = talloc_get_type_abort(c->private_data, struct domain_open_samr_state);
 
        /* receive samr_Close reply */
-       c->status = dcerpc_ndr_request_recv(req);
+       c->status = dcerpc_samr_Close_r_recv(subreq, s);
+       TALLOC_FREE(subreq);
        if (!composite_is_ok(c)) return;
 
        if (s->monitor_fn) {
@@ -120,29 +123,31 @@ static void continue_domain_open_close(struct rpc_request *req)
        s->connect.out.connect_handle  = &s->connect_handle;
        
        /* send request */
-       conn_req = dcerpc_samr_Connect_send(s->pipe, c, &s->connect);
-       if (composite_nomem(conn_req, c)) return;
+       subreq = dcerpc_samr_Connect_r_send(s, c->event_ctx,
+                                           s->pipe->binding_handle,
+                                           &s->connect);
+       if (composite_nomem(subreq, c)) return;
 
        /* callback handler */
-       composite_continue_rpc(c, conn_req, continue_domain_open_connect, c);
+       tevent_req_set_callback(subreq, continue_domain_open_connect, c);
 }
 
 
 /**
  * Stage 1: Connect to SAM server.
  */
-static void continue_domain_open_connect(struct rpc_request *req)
+static void continue_domain_open_connect(struct tevent_req *subreq)
 {
        struct composite_context *c;
        struct domain_open_samr_state *s;
-       struct rpc_request *lookup_req;
        struct samr_LookupDomain *r;
        
-       c = talloc_get_type(req->async.private_data, struct composite_context);
-       s = talloc_get_type(c->private_data, struct domain_open_samr_state);
+       c = tevent_req_callback_data(subreq, struct composite_context);
+       s = talloc_get_type_abort(c->private_data, struct domain_open_samr_state);
 
        /* receive samr_Connect reply */
-       c->status = dcerpc_ndr_request_recv(req);
+       c->status = dcerpc_samr_Connect_r_recv(subreq, s);
+       TALLOC_FREE(subreq);
        if (!composite_is_ok(c)) return;
 
        if (s->monitor_fn) {
@@ -159,29 +164,33 @@ static void continue_domain_open_connect(struct rpc_request *req)
        /* prepare for samr_LookupDomain call */
        r->in.connect_handle = &s->connect_handle;
        r->in.domain_name    = &s->domain_name;
+       r->out.sid           = talloc(s, struct dom_sid2 *);
+       if (composite_nomem(r->out.sid, c)) return;
 
-       lookup_req = dcerpc_samr_LookupDomain_send(s->pipe, c, r);
-       if (composite_nomem(lookup_req, c)) return;
+       subreq = dcerpc_samr_LookupDomain_r_send(s, c->event_ctx,
+                                                s->pipe->binding_handle,
+                                                r);
+       if (composite_nomem(subreq, c)) return;
 
-       composite_continue_rpc(c, lookup_req, continue_domain_open_lookup, c);
+       tevent_req_set_callback(subreq, continue_domain_open_lookup, c);
 }
 
 
 /**
  * Stage 2: Lookup domain by name.
  */
-static void continue_domain_open_lookup(struct rpc_request *req)
+static void continue_domain_open_lookup(struct tevent_req *subreq)
 {
        struct composite_context *c;
        struct domain_open_samr_state *s;
-       struct rpc_request *opendom_req;
        struct samr_OpenDomain *r;
 
-       c = talloc_get_type(req->async.private_data, struct composite_context);
-       s = talloc_get_type(c->private_data, struct domain_open_samr_state);
+       c = tevent_req_callback_data(subreq, struct composite_context);
+       s = talloc_get_type_abort(c->private_data, struct domain_open_samr_state);
        
        /* receive samr_LookupDomain reply */
-       c->status = dcerpc_ndr_request_recv(req);
+       c->status = dcerpc_samr_LookupDomain_r_recv(subreq, s);
+       TALLOC_FREE(subreq);
 
        if (s->monitor_fn) {
                struct monitor_msg msg;
@@ -198,7 +207,7 @@ static void continue_domain_open_lookup(struct rpc_request *req)
        r = &s->open;
 
        /* check the rpc layer status */
-       if (!composite_is_ok(c));
+       if (!composite_is_ok(c)) return;
 
        /* check the rpc call itself status */
        if (!NT_STATUS_IS_OK(s->lookup.out.result)) {
@@ -209,29 +218,32 @@ static void continue_domain_open_lookup(struct rpc_request *req)
        /* prepare for samr_OpenDomain call */
        r->in.connect_handle = &s->connect_handle;
        r->in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
-       r->in.sid            = s->lookup.out.sid;
+       r->in.sid            = *s->lookup.out.sid;
        r->out.domain_handle = &s->domain_handle;
 
-       opendom_req = dcerpc_samr_OpenDomain_send(s->pipe, c, r);
-       if (composite_nomem(opendom_req, c)) return;
+       subreq = dcerpc_samr_OpenDomain_r_send(s, c->event_ctx,
+                                              s->pipe->binding_handle,
+                                              r);
+       if (composite_nomem(subreq, c)) return;
 
-       composite_continue_rpc(c, opendom_req, continue_domain_open_open, c);
+       tevent_req_set_callback(subreq, continue_domain_open_open, c);
 }
 
 
 /*
  * Stage 3: Open domain.
  */
-static void continue_domain_open_open(struct rpc_request *req)
+static void continue_domain_open_open(struct tevent_req *subreq)
 {
        struct composite_context *c;
        struct domain_open_samr_state *s;
 
-       c = talloc_get_type(req->async.private_data, struct composite_context);
-       s = talloc_get_type(c->private_data, struct domain_open_samr_state);
+       c = tevent_req_callback_data(subreq, struct composite_context);
+       s = talloc_get_type_abort(c->private_data, struct domain_open_samr_state);
 
        /* receive samr_OpenDomain reply */
-       c->status = dcerpc_ndr_request_recv(req);
+       c->status = dcerpc_samr_OpenDomain_r_recv(subreq, s);
+       TALLOC_FREE(subreq);
        if (!composite_is_ok(c)) return;
 
        if (s->monitor_fn) {
@@ -256,15 +268,16 @@ static void continue_domain_open_open(struct rpc_request *req)
  */
 
 struct composite_context *libnet_DomainOpenSamr_send(struct libnet_context *ctx,
+                                                    TALLOC_CTX *mem_ctx,
                                                     struct libnet_DomainOpen *io,
                                                     void (*monitor)(struct monitor_msg*))
 {
        struct composite_context *c;
        struct domain_open_samr_state *s;
        struct composite_context *rpcconn_req;
-       struct rpc_request *close_req, *conn_req;
+       struct tevent_req *subreq;
 
-       c = composite_create(ctx, ctx->event_ctx);
+       c = composite_create(mem_ctx, ctx->event_ctx);
        if (c == NULL) return NULL;
 
        s = talloc_zero(c, struct domain_open_samr_state);
@@ -284,7 +297,7 @@ struct composite_context *libnet_DomainOpenSamr_send(struct libnet_context *ctx,
                /* attempting to connect a domain controller */
                s->rpcconn.level           = LIBNET_RPC_CONNECT_DC;
                s->rpcconn.in.name         = io->in.domain_name;
-               s->rpcconn.in.dcerpc_iface = &dcerpc_table_samr;
+               s->rpcconn.in.dcerpc_iface = &ndr_table_samr;
                
                /* send rpc pipe connect request */
                rpcconn_req = libnet_RpcConnect_send(ctx, c, &s->rpcconn, s->monitor_fn);
@@ -296,7 +309,7 @@ struct composite_context *libnet_DomainOpenSamr_send(struct libnet_context *ctx,
 
        /* libnet context's domain handle is not empty, so check out what
           was opened first, before doing anything */
-       if (!policy_handle_empty(&ctx->samr.handle)) {
+       if (!ndr_policy_handle_empty(&ctx->samr.handle)) {
                if (strequal(ctx->samr.name, io->in.domain_name) &&
                    ctx->samr.access_mask == io->in.access_mask) {
 
@@ -310,11 +323,13 @@ struct composite_context *libnet_DomainOpenSamr_send(struct libnet_context *ctx,
                        s->close.in.handle = &ctx->samr.handle;
 
                        /* send request to close domain handle */
-                       close_req = dcerpc_samr_Close_send(s->pipe, c, &s->close);
-                       if (composite_nomem(close_req, c)) return c;
+                       subreq = dcerpc_samr_Close_r_send(s, c->event_ctx,
+                                                         s->pipe->binding_handle,
+                                                         &s->close);
+                       if (composite_nomem(subreq, c)) return c;
 
                        /* callback handler */
-                       composite_continue_rpc(c, close_req, continue_domain_open_close, c);
+                       tevent_req_set_callback(subreq, continue_domain_open_close, c);
                        return c;
                }
        }
@@ -325,11 +340,13 @@ struct composite_context *libnet_DomainOpenSamr_send(struct libnet_context *ctx,
        s->connect.out.connect_handle  = &s->connect_handle;
        
        /* send request */
-       conn_req = dcerpc_samr_Connect_send(s->pipe, c, &s->connect);
-       if (composite_nomem(conn_req, c)) return c;
+       subreq = dcerpc_samr_Connect_r_send(s, c->event_ctx,
+                                           s->pipe->binding_handle,
+                                           &s->connect);
+       if (composite_nomem(subreq, c)) return c;
 
        /* callback handler */
-       composite_continue_rpc(c, conn_req, continue_domain_open_connect, c);
+       tevent_req_set_callback(subreq, continue_domain_open_connect, c);
        return c;
 }
 
@@ -354,13 +371,14 @@ NTSTATUS libnet_DomainOpenSamr_recv(struct composite_context *c, struct libnet_c
        status = composite_wait(c);
        
        if (NT_STATUS_IS_OK(status) && io) {
-               s = talloc_get_type(c->private_data, struct domain_open_samr_state);
+               s = talloc_get_type_abort(c->private_data, struct domain_open_samr_state);
                io->out.domain_handle = s->domain_handle;
 
                /* store the resulting handle and related data for use by other
                   libnet functions */
                ctx->samr.connect_handle = s->connect_handle;
                ctx->samr.handle      = s->domain_handle;
+               ctx->samr.sid         = talloc_steal(ctx, *s->lookup.out.sid);
                ctx->samr.name        = talloc_steal(ctx, s->domain_name.string);
                ctx->samr.access_mask = s->access_mask;
        }
@@ -385,7 +403,7 @@ struct domain_open_lsa_state {
 
 
 static void continue_rpc_connect_lsa(struct composite_context *ctx);
-static void continue_lsa_policy_open(struct rpc_request *req);
+static void continue_lsa_policy_open(struct tevent_req *subreq);
 
 
 /**
@@ -397,17 +415,18 @@ static void continue_lsa_policy_open(struct rpc_request *req);
  */
 
 struct composite_context* libnet_DomainOpenLsa_send(struct libnet_context *ctx,
+                                                   TALLOC_CTX *mem_ctx,
                                                    struct libnet_DomainOpen *io,
                                                    void (*monitor)(struct monitor_msg*))
 {
        struct composite_context *c;
        struct domain_open_lsa_state *s;
        struct composite_context *rpcconn_req;
-       struct rpc_request *openpol_req;
+       struct tevent_req *subreq;
        struct lsa_QosInfo *qos;
 
        /* create composite context and state */
-       c = composite_create(ctx, ctx->event_ctx);
+       c = composite_create(mem_ctx, ctx->event_ctx);
        if (c == NULL) return c;
 
        s = talloc_zero(c, struct domain_open_lsa_state);
@@ -423,10 +442,12 @@ struct composite_context* libnet_DomainOpenLsa_send(struct libnet_context *ctx,
        /* check, if there's lsa pipe opened already, before opening a handle */
        if (ctx->lsa.pipe == NULL) {
 
+               ZERO_STRUCT(s->rpcconn);
+
                /* attempting to connect a domain controller */
                s->rpcconn.level           = LIBNET_RPC_CONNECT_DC;
                s->rpcconn.in.name         = talloc_strdup(c, io->in.domain_name);
-               s->rpcconn.in.dcerpc_iface = &dcerpc_table_lsarpc;
+               s->rpcconn.in.dcerpc_iface = &ndr_table_lsarpc;
                
                /* send rpc pipe connect request */
                rpcconn_req = libnet_RpcConnect_send(ctx, c, &s->rpcconn, s->monitor_fn);
@@ -453,10 +474,12 @@ struct composite_context* libnet_DomainOpenLsa_send(struct libnet_context *ctx,
        s->openpol.out.handle       = &s->handle;
        
        /* send rpc request */
-       openpol_req = dcerpc_lsa_OpenPolicy2_send(s->pipe, c, &s->openpol);
-       if (composite_nomem(openpol_req, c)) return c;
+       subreq = dcerpc_lsa_OpenPolicy2_r_send(s, c->event_ctx,
+                                              s->pipe->binding_handle,
+                                              &s->openpol);
+       if (composite_nomem(subreq, c)) return c;
 
-       composite_continue_rpc(c, openpol_req, continue_lsa_policy_open, c);
+       tevent_req_set_callback(subreq, continue_lsa_policy_open, c);
        return c;
 }
 
@@ -469,10 +492,10 @@ static void continue_rpc_connect_lsa(struct composite_context *ctx)
        struct composite_context *c;
        struct domain_open_lsa_state *s;
        struct lsa_QosInfo *qos;
-       struct rpc_request *openpol_req;
+       struct tevent_req *subreq;
 
-       c = talloc_get_type(ctx->async.private_data, struct composite_context);
-       s = talloc_get_type(c->private_data, struct domain_open_lsa_state);
+       c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
+       s = talloc_get_type_abort(c->private_data, struct domain_open_lsa_state);
 
        /* receive rpc connection */
        c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->rpcconn);
@@ -497,25 +520,28 @@ static void continue_rpc_connect_lsa(struct composite_context *ctx)
        s->openpol.out.handle       = &s->handle;
 
        /* send rpc request */
-       openpol_req = dcerpc_lsa_OpenPolicy2_send(s->pipe, c, &s->openpol);
-       if (composite_nomem(openpol_req, c)) return;
+       subreq = dcerpc_lsa_OpenPolicy2_r_send(s, c->event_ctx,
+                                              s->pipe->binding_handle,
+                                              &s->openpol);
+       if (composite_nomem(subreq, c)) return;
 
-       composite_continue_rpc(c, openpol_req, continue_lsa_policy_open, c);
+       tevent_req_set_callback(subreq, continue_lsa_policy_open, c);
 }
 
 
 /*
   Stage 1: Lsa policy opened - we're done, if successfully
  */
-static void continue_lsa_policy_open(struct rpc_request *req)
+static void continue_lsa_policy_open(struct tevent_req *subreq)
 {
        struct composite_context *c;
        struct domain_open_lsa_state *s;
 
-       c = talloc_get_type(req->async.private_data, struct composite_context);
-       s = talloc_get_type(c->private_data, struct domain_open_lsa_state);
+       c = tevent_req_callback_data(subreq, struct composite_context);
+       s = talloc_get_type_abort(c->private_data, struct domain_open_lsa_state);
 
-       c->status = dcerpc_ndr_request_recv(req);
+       c->status = dcerpc_lsa_OpenPolicy2_r_recv(subreq, s);
+       TALLOC_FREE(subreq);
        if (!composite_is_ok(c)) return;
 
        if (s->monitor_fn) {
@@ -552,7 +578,7 @@ NTSTATUS libnet_DomainOpenLsa_recv(struct composite_context *c, struct libnet_co
        if (NT_STATUS_IS_OK(status) && io) {
                /* everything went fine - get the results and
                   return the error string */
-               s = talloc_get_type(c->private_data, struct domain_open_lsa_state);
+               s = talloc_get_type_abort(c->private_data, struct domain_open_lsa_state);
                io->out.domain_handle = s->handle;
 
                ctx->lsa.handle      = s->handle;
@@ -576,12 +602,13 @@ NTSTATUS libnet_DomainOpenLsa_recv(struct composite_context *c, struct libnet_co
 /**
  * Sends a request to open a domain in desired service
  *
- * @param ctx initalised libnet context
+ * @param ctx initialised libnet context
  * @param io arguments and results of the call
  * @param monitor pointer to monitor function that is passed monitor message
  */
 
 struct composite_context* libnet_DomainOpen_send(struct libnet_context *ctx,
+                                                TALLOC_CTX *mem_ctx,
                                                 struct libnet_DomainOpen *io,
                                                 void (*monitor)(struct monitor_msg*))
 {
@@ -590,13 +617,13 @@ struct composite_context* libnet_DomainOpen_send(struct libnet_context *ctx,
        switch (io->in.type) {
        case DOMAIN_LSA:
                /* reques to open a policy handle on \pipe\lsarpc */
-               c = libnet_DomainOpenLsa_send(ctx, io, monitor);
+               c = libnet_DomainOpenLsa_send(ctx, mem_ctx, io, monitor);
                break;
 
        case DOMAIN_SAMR:
        default:
                /* request to open a domain policy handle on \pipe\samr */
-               c = libnet_DomainOpenSamr_send(ctx, io, monitor);
+               c = libnet_DomainOpenSamr_send(ctx, mem_ctx, io, monitor);
                break;
        }
 
@@ -646,7 +673,7 @@ NTSTATUS libnet_DomainOpen(struct libnet_context *ctx,
                           TALLOC_CTX *mem_ctx,
                           struct libnet_DomainOpen *io)
 {
-       struct composite_context *c = libnet_DomainOpen_send(ctx, io, NULL);
+       struct composite_context *c = libnet_DomainOpen_send(ctx, mem_ctx, io, NULL);
        return libnet_DomainOpen_recv(c, ctx, mem_ctx, io);
 }
 
@@ -660,19 +687,20 @@ struct domain_close_lsa_state {
 };
 
 
-static void continue_lsa_close(struct rpc_request *req);
+static void continue_lsa_close(struct tevent_req *subreq);
 
 
 struct composite_context* libnet_DomainCloseLsa_send(struct libnet_context *ctx,
+                                                    TALLOC_CTX *mem_ctx,
                                                     struct libnet_DomainClose *io,
                                                     void (*monitor)(struct monitor_msg*))
 {
        struct composite_context *c;
        struct domain_close_lsa_state *s;
-       struct rpc_request *close_req;
+       struct tevent_req *subreq;
 
        /* composite context and state structure allocation */
-       c = composite_create(ctx, ctx->event_ctx);
+       c = composite_create(mem_ctx, ctx->event_ctx);
        if (c == NULL) return c;
 
        s = talloc_zero(c, struct domain_close_lsa_state);
@@ -696,10 +724,12 @@ struct composite_context* libnet_DomainCloseLsa_send(struct libnet_context *ctx,
        s->close.out.handle = &s->handle;
 
        /* send the request */
-       close_req = dcerpc_lsa_Close_send(s->pipe, c, &s->close);
-       if (composite_nomem(close_req, c)) return c;
+       subreq = dcerpc_lsa_Close_r_send(s, c->event_ctx,
+                                        s->pipe->binding_handle,
+                                        &s->close);
+       if (composite_nomem(subreq, c)) return c;
 
-       composite_continue_rpc(c, close_req, continue_lsa_close, c);
+       tevent_req_set_callback(subreq, continue_lsa_close, c);
        return c;
 }
 
@@ -707,15 +737,16 @@ struct composite_context* libnet_DomainCloseLsa_send(struct libnet_context *ctx,
 /*
   Stage 1: Receive result of lsa close call
 */
-static void continue_lsa_close(struct rpc_request *req)
+static void continue_lsa_close(struct tevent_req *subreq)
 {
        struct composite_context *c;
        struct domain_close_lsa_state *s;
        
-       c = talloc_get_type(req->async.private_data, struct composite_context);
-       s = talloc_get_type(c->private_data, struct domain_close_lsa_state);
+       c = tevent_req_callback_data(subreq, struct composite_context);
+       s = talloc_get_type_abort(c->private_data, struct domain_close_lsa_state);
 
-       c->status = dcerpc_ndr_request_recv(req);
+       c->status = dcerpc_lsa_Close_r_recv(subreq, s);
+       TALLOC_FREE(subreq);
        if (!composite_is_ok(c)) return;
 
        if (s->monitor_fn) {
@@ -764,19 +795,20 @@ struct domain_close_samr_state {
 };
 
 
-static void continue_samr_close(struct rpc_request *req);
+static void continue_samr_close(struct tevent_req *subreq);
 
 
 struct composite_context* libnet_DomainCloseSamr_send(struct libnet_context *ctx,
+                                                     TALLOC_CTX *mem_ctx,
                                                      struct libnet_DomainClose *io,
                                                      void (*monitor)(struct monitor_msg*))
 {
        struct composite_context *c;
        struct domain_close_samr_state *s;
-       struct rpc_request *close_req;
+       struct tevent_req *subreq;
 
        /* composite context and state structure allocation */
-       c = composite_create(ctx, ctx->event_ctx);
+       c = composite_create(mem_ctx, ctx->event_ctx);
        if (c == NULL) return c;
 
        s = talloc_zero(c, struct domain_close_samr_state);
@@ -798,10 +830,12 @@ struct composite_context* libnet_DomainCloseSamr_send(struct libnet_context *ctx
        s->close.out.handle = &s->handle;
 
        /* send the request */
-       close_req = dcerpc_samr_Close_send(ctx->samr.pipe, ctx, &s->close);
-       if (composite_nomem(close_req, c)) return c;
+       subreq = dcerpc_samr_Close_r_send(s, c->event_ctx,
+                                         ctx->samr.pipe->binding_handle,
+                                         &s->close);
+       if (composite_nomem(subreq, c)) return c;
 
-       composite_continue_rpc(c, close_req, continue_samr_close, c);
+       tevent_req_set_callback(subreq, continue_samr_close, c);
        return c;
 }
 
@@ -809,15 +843,16 @@ struct composite_context* libnet_DomainCloseSamr_send(struct libnet_context *ctx
 /*
   Stage 1: Receive result of samr close call
 */
-static void continue_samr_close(struct rpc_request *req)
+static void continue_samr_close(struct tevent_req *subreq)
 {
        struct composite_context *c;
        struct domain_close_samr_state *s;
 
-       c = talloc_get_type(req->async.private_data, struct composite_context);
-       s = talloc_get_type(c->private_data, struct domain_close_samr_state);
+       c = tevent_req_callback_data(subreq, struct composite_context);
+       s = talloc_get_type_abort(c->private_data, struct domain_close_samr_state);
        
-       c->status = dcerpc_ndr_request_recv(req);
+       c->status = dcerpc_samr_Close_r_recv(subreq, s);
+       TALLOC_FREE(subreq);
        if (!composite_is_ok(c)) return;
 
        if (s->monitor_fn) {
@@ -844,7 +879,10 @@ NTSTATUS libnet_DomainCloseSamr_recv(struct composite_context *c, struct libnet_
                /* domain policy handle closed successfully */
 
                ZERO_STRUCT(ctx->samr.handle);
+               talloc_free(discard_const_p(char, ctx->samr.name));
+               talloc_free(ctx->samr.sid);
                ctx->samr.name = NULL;
+               ctx->samr.sid = NULL;
 
                io->out.error_string = talloc_asprintf(mem_ctx, "Success");
 
@@ -859,6 +897,7 @@ NTSTATUS libnet_DomainCloseSamr_recv(struct composite_context *c, struct libnet_
 
 
 struct composite_context* libnet_DomainClose_send(struct libnet_context *ctx,
+                                                 TALLOC_CTX *mem_ctx,
                                                  struct libnet_DomainClose *io,
                                                  void (*monitor)(struct monitor_msg*))
 {
@@ -867,13 +906,13 @@ struct composite_context* libnet_DomainClose_send(struct libnet_context *ctx,
        switch (io->in.type) {
        case DOMAIN_LSA:
                /* request to close policy handle on \pipe\lsarpc */
-               c = libnet_DomainCloseLsa_send(ctx, io, monitor);
+               c = libnet_DomainCloseLsa_send(ctx, mem_ctx, io, monitor);
                break;
 
        case DOMAIN_SAMR:
        default:
                /* request to close domain policy handle on \pipe\samr */
-               c = libnet_DomainCloseSamr_send(ctx, io, monitor);
+               c = libnet_DomainCloseSamr_send(ctx, mem_ctx, io, monitor);
                break;
        }
        
@@ -908,7 +947,7 @@ NTSTATUS libnet_DomainClose(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
 {
        struct composite_context *c;
        
-       c = libnet_DomainClose_send(ctx, io, NULL);
+       c = libnet_DomainClose_send(ctx, mem_ctx, io, NULL);
        return libnet_DomainClose_recv(c, ctx, mem_ctx, io);
 }
 
@@ -931,9 +970,9 @@ struct domain_list_state {
 
 
 static void continue_rpc_connect(struct composite_context *c);
-static void continue_samr_connect(struct rpc_request *c);
-static void continue_samr_enum_domains(struct rpc_request *req);
-static void continue_samr_close_handle(struct rpc_request *req);
+static void continue_samr_connect(struct tevent_req *subreq);
+static void continue_samr_enum_domains(struct tevent_req *subreq);
+static void continue_samr_close_handle(struct tevent_req *subreq);
 
 static struct domainlist* get_domain_list(TALLOC_CTX *mem_ctx, struct domain_list_state *s);
 
@@ -946,10 +985,10 @@ static void continue_rpc_connect(struct composite_context *ctx)
 {
        struct composite_context *c;
        struct domain_list_state *s;
-       struct rpc_request *samrconn_req;
+       struct tevent_req *subreq;
 
-       c = talloc_get_type(ctx->async.private_data, struct composite_context);
-       s = talloc_get_type(c->private_data, struct domain_list_state);
+       c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
+       s = talloc_get_type_abort(c->private_data, struct domain_list_state);
        
        c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->rpcconn);
        if (!composite_is_ok(c)) return;
@@ -958,10 +997,12 @@ static void continue_rpc_connect(struct composite_context *ctx)
        s->samrconn.in.access_mask     = SEC_GENERIC_READ;     /* should be enough */
        s->samrconn.out.connect_handle = &s->connect_handle;
 
-       samrconn_req = dcerpc_samr_Connect_send(s->ctx->samr.pipe, c, &s->samrconn);
-       if (composite_nomem(samrconn_req, c)) return;
+       subreq = dcerpc_samr_Connect_r_send(s, c->event_ctx,
+                                           s->ctx->samr.pipe->binding_handle,
+                                           &s->samrconn);
+       if (composite_nomem(subreq, c)) return;
 
-       composite_continue_rpc(c, samrconn_req, continue_samr_connect, c);
+       tevent_req_set_callback(subreq, continue_samr_connect, c);
 }
 
 
@@ -969,16 +1010,16 @@ static void continue_rpc_connect(struct composite_context *ctx)
   Stage 2: Receive policy handle to the connected SAMR service and issue
   a request to enumerate domain databases available
 */
-static void continue_samr_connect(struct rpc_request *req)
+static void continue_samr_connect(struct tevent_req *subreq)
 {
        struct composite_context *c;
        struct domain_list_state *s;
-       struct rpc_request *enumdom_req;
 
-       c = talloc_get_type(req->async.private_data, struct composite_context);
-       s = talloc_get_type(c->private_data, struct domain_list_state);
+       c = tevent_req_callback_data(subreq, struct composite_context);
+       s = talloc_get_type_abort(c->private_data, struct domain_list_state);
        
-       c->status = dcerpc_ndr_request_recv(req);
+       c->status = dcerpc_samr_Connect_r_recv(subreq, s);
+       TALLOC_FREE(subreq);
        if (!composite_is_ok(c)) return;
 
        if (s->monitor_fn) {
@@ -994,11 +1035,17 @@ static void continue_samr_connect(struct rpc_request *req)
        s->enumdom.in.resume_handle  = &s->resume_handle;
        s->enumdom.in.buf_size       = s->buf_size;
        s->enumdom.out.resume_handle = &s->resume_handle;
+       s->enumdom.out.num_entries   = talloc(s, uint32_t);
+       if (composite_nomem(s->enumdom.out.num_entries, c)) return;
+       s->enumdom.out.sam           = talloc(s, struct samr_SamArray *);
+       if (composite_nomem(s->enumdom.out.sam, c)) return;
 
-       enumdom_req = dcerpc_samr_EnumDomains_send(s->ctx->samr.pipe, c, &s->enumdom);
-       if (composite_nomem(enumdom_req, c)) return;
+       subreq = dcerpc_samr_EnumDomains_r_send(s, c->event_ctx,
+                                               s->ctx->samr.pipe->binding_handle,
+                                               &s->enumdom);
+       if (composite_nomem(subreq, c)) return;
 
-       composite_continue_rpc(c, enumdom_req, continue_samr_enum_domains, c);
+       tevent_req_set_callback(subreq, continue_samr_enum_domains, c);
 }
 
 
@@ -1007,17 +1054,16 @@ static void continue_samr_connect(struct rpc_request *req)
   enumeration is not complete yet. Close samr connection handle
   upon completion.
 */
-static void continue_samr_enum_domains(struct rpc_request *req)
+static void continue_samr_enum_domains(struct tevent_req *subreq)
 {
        struct composite_context *c;
        struct domain_list_state *s;
-       struct rpc_request *enumdom_req;
-       struct rpc_request *samrclose_req;
 
-       c = talloc_get_type(req->async.private_data, struct composite_context);
-       s = talloc_get_type(c->private_data, struct domain_list_state);
+       c = tevent_req_callback_data(subreq, struct composite_context);
+       s = talloc_get_type_abort(c->private_data, struct domain_list_state);
        
-       c->status = dcerpc_ndr_request_recv(req);
+       c->status = dcerpc_samr_EnumDomains_r_recv(subreq, s);
+       TALLOC_FREE(subreq);
        if (!composite_is_ok(c)) return;
 
        if (s->monitor_fn) {
@@ -1044,10 +1090,12 @@ static void continue_samr_enum_domains(struct rpc_request *req)
                s->enumdom.out.resume_handle = &s->resume_handle;
 
                /* send the request */
-               enumdom_req = dcerpc_samr_EnumDomains_send(s->ctx->samr.pipe, c, &s->enumdom);
-               if (composite_nomem(enumdom_req, c)) return;
+               subreq = dcerpc_samr_EnumDomains_r_send(s, c->event_ctx,
+                                                       s->ctx->samr.pipe->binding_handle,
+                                                       &s->enumdom);
+               if (composite_nomem(subreq, c)) return;
 
-               composite_continue_rpc(c, enumdom_req, continue_samr_enum_domains, c);
+               tevent_req_set_callback(subreq, continue_samr_enum_domains, c);
 
        } else {
                composite_error(c, s->enumdom.out.result);
@@ -1059,25 +1107,28 @@ static void continue_samr_enum_domains(struct rpc_request *req)
        s->samrclose.out.handle = &s->connect_handle;
        
        /* send the request */
-       samrclose_req = dcerpc_samr_Close_send(s->ctx->samr.pipe, c, &s->samrclose);
-       if (composite_nomem(samrclose_req, c)) return;
+       subreq = dcerpc_samr_Close_r_send(s, c->event_ctx,
+                                         s->ctx->samr.pipe->binding_handle,
+                                         &s->samrclose);
+       if (composite_nomem(subreq, c)) return;
 
-       composite_continue_rpc(c, samrclose_req, continue_samr_close_handle, c);
+       tevent_req_set_callback(subreq, continue_samr_close_handle, c);
 }
 
 
 /*
   Stage 4: Receive result of closing samr connection handle.
 */
-static void continue_samr_close_handle(struct rpc_request *req)
+static void continue_samr_close_handle(struct tevent_req *subreq)
 {
        struct composite_context *c;
        struct domain_list_state *s;
 
-       c = talloc_get_type(req->async.private_data, struct composite_context);
-       s = talloc_get_type(c->private_data, struct domain_list_state);
+       c = tevent_req_callback_data(subreq, struct composite_context);
+       s = talloc_get_type_abort(c->private_data, struct domain_list_state);
 
-       c->status = dcerpc_ndr_request_recv(req);
+       c->status = dcerpc_samr_Close_r_recv(subreq, s);
+       TALLOC_FREE(subreq);
        if (!composite_is_ok(c)) return;
 
        if (s->monitor_fn) {
@@ -1092,6 +1143,7 @@ static void continue_samr_close_handle(struct rpc_request *req)
        /* did everything go fine ? */
        if (!NT_STATUS_IS_OK(s->samrclose.out.result)) {
                composite_error(c, s->samrclose.out.result);
+               return;
        }
 
        composite_done(c);
@@ -1103,22 +1155,22 @@ static void continue_samr_close_handle(struct rpc_request *req)
 */
 static struct domainlist* get_domain_list(TALLOC_CTX *mem_ctx, struct domain_list_state *s)
 {
-       int i;
+       uint32_t i;
        if (mem_ctx == NULL || s == NULL) return NULL;
 
        /* prepare domains array */
        if (s->domains == NULL) {
                s->domains = talloc_array(mem_ctx, struct domainlist,
-                                         s->enumdom.out.num_entries);
+                                         *s->enumdom.out.num_entries);
        } else {
                s->domains = talloc_realloc(mem_ctx, s->domains, struct domainlist,
-                                           s->count + s->enumdom.out.num_entries);
+                                           s->count + *s->enumdom.out.num_entries);
        }
 
        /* copy domain names returned from samr_EnumDomains call */
-       for (i = s->count; i < s->count + s->enumdom.out.num_entries; i++)
+       for (i = s->count; i < s->count + *s->enumdom.out.num_entries; i++)
        {
-               struct lsa_String *domain_name = &s->enumdom.out.sam->entries[i - s->count].name;
+               struct lsa_String *domain_name = &(*s->enumdom.out.sam)->entries[i - s->count].name;
 
                /* strdup name as a child of allocated array to make it follow the array
                   in case of talloc_steal or talloc_free */
@@ -1127,7 +1179,7 @@ static struct domainlist* get_domain_list(TALLOC_CTX *mem_ctx, struct domain_lis
        }
 
        /* number of entries returned (domains enumerated) */
-       s->count += s->enumdom.out.num_entries;
+       s->count += *s->enumdom.out.num_entries;
        
        return s->domains;
 }
@@ -1136,7 +1188,7 @@ static struct domainlist* get_domain_list(TALLOC_CTX *mem_ctx, struct domain_lis
 /**
  * Sends a request to list domains on given host
  *
- * @param ctx initalised libnet context
+ * @param ctx initialised libnet context
  * @param mem_ctx memory context
  * @param io arguments and results of the call
  * @param monitor pointer to monitor function that is passed monitor messages
@@ -1150,7 +1202,7 @@ struct composite_context* libnet_DomainList_send(struct libnet_context *ctx,
        struct composite_context *c;
        struct domain_list_state *s;
        struct composite_context *rpcconn_req;
-       struct rpc_request *samrconn_req;
+       struct tevent_req *subreq;
 
        /* composite context and state structure allocation */
        c = composite_create(ctx, ctx->event_ctx);
@@ -1168,10 +1220,12 @@ struct composite_context* libnet_DomainList_send(struct libnet_context *ctx,
 
        /* check whether samr pipe has already been opened */
        if (ctx->samr.pipe == NULL) {
+               ZERO_STRUCT(s->rpcconn);
+
                /* prepare rpc connect call */
                s->rpcconn.level           = LIBNET_RPC_CONNECT_SERVER;
                s->rpcconn.in.name         = s->hostname;
-               s->rpcconn.in.dcerpc_iface = &dcerpc_table_samr;
+               s->rpcconn.in.dcerpc_iface = &ndr_table_samr;
 
                rpcconn_req = libnet_RpcConnect_send(ctx, c, &s->rpcconn, s->monitor_fn);
                if (composite_nomem(rpcconn_req, c)) return c;
@@ -1184,10 +1238,12 @@ struct composite_context* libnet_DomainList_send(struct libnet_context *ctx,
                s->samrconn.in.access_mask     = SEC_GENERIC_READ;
                s->samrconn.out.connect_handle = &s->connect_handle;
                
-               samrconn_req = dcerpc_samr_Connect_send(s->ctx->samr.pipe, c, &s->samrconn);
-               if (composite_nomem(samrconn_req, c)) return c;
+               subreq = dcerpc_samr_Connect_r_send(s, c->event_ctx,
+                                                   s->ctx->samr.pipe->binding_handle,
+                                                   &s->samrconn);
+               if (composite_nomem(subreq, c)) return c;
 
-               composite_continue_rpc(c, samrconn_req, continue_samr_connect, c);
+               tevent_req_set_callback(subreq, continue_samr_connect, c);
        }
 
        return c;
@@ -1211,7 +1267,7 @@ NTSTATUS libnet_DomainList_recv(struct composite_context *c, struct libnet_conte
 
        status = composite_wait(c);
 
-       s = talloc_get_type(c->private_data, struct domain_list_state);
+       s = talloc_get_type_abort(c->private_data, struct domain_list_state);
 
        if (NT_STATUS_IS_OK(status) && ctx && mem_ctx && io) {
                /* fetch the results to be returned by io structure */