r14860: create libcli/security/security.h
[kai/samba-autobuild/.git] / source4 / rpc_server / dcerpc_server.c
index 575c073e468573e02557c878c61a0c5996a6b0aa..d5012c06095e82ee6c59b8e6774b4be41bd8f472 100644 (file)
 */
 
 #include "includes.h"
-#include "librpc/gen_ndr/ndr_epmapper.h"
-#include "librpc/gen_ndr/ndr_oxidresolver.h"
+#include "librpc/gen_ndr/ndr_dcerpc.h"
 #include "auth/auth.h"
 #include "dlinklist.h"
 #include "rpc_server/dcerpc_server.h"
-#include "events.h"
+#include "lib/events/events.h"
+#include "smbd/service_task.h"
+#include "smbd/service_stream.h"
+#include "smbd/service.h"
+#include "system/filesys.h"
+#include "libcli/security/security.h"
+#include "build.h"
 
 /*
   see if two endpoints match
@@ -57,7 +62,7 @@ static struct dcesrv_endpoint *find_endpoint(struct dcesrv_context *dce_ctx,
 {
        struct dcesrv_endpoint *ep;
        for (ep=dce_ctx->endpoint_list; ep; ep=ep->next) {
-               if (endpoints_match(&ep->ep_description, ep_description)) {
+               if (endpoints_match(ep->ep_description, ep_description)) {
                        return ep;
                }
        }
@@ -83,15 +88,8 @@ static struct dcesrv_connection_context *dcesrv_find_context(struct dcesrv_conne
 static BOOL interface_match(const struct dcesrv_interface *if1,
                                                        const struct dcesrv_interface *if2)
 {
-       if (if1->if_version != if2->if_version) {
-               return False;
-       }
-
-       if (strcmp(if1->uuid, if2->uuid)==0) {
-               return True;
-       }                       
-
-       return False;
+       return (if1->syntax_id.if_version == if2->syntax_id.if_version && 
+                       GUID_equal(&if1->syntax_id.uuid, &if2->syntax_id.uuid));
 }
 
 /*
@@ -113,24 +111,17 @@ static const struct dcesrv_interface *find_interface(const struct dcesrv_endpoin
   see if a uuid and if_version match to an interface
 */
 static BOOL interface_match_by_uuid(const struct dcesrv_interface *iface,
-                                   const char *uuid, uint32_t if_version)
+                                   const struct GUID *uuid, uint32_t if_version)
 {
-       if (iface->if_version != if_version) {
-               return False;
-       }
-
-       if (strcmp(iface->uuid, uuid)==0) {
-               return True;
-       }                       
-
-       return False;
+       return (iface->syntax_id.if_version == if_version && 
+                       GUID_equal(&iface->syntax_id.uuid, uuid));
 }
 
 /*
   find the interface operations on an endpoint by uuid
 */
 static const struct dcesrv_interface *find_interface_by_uuid(const struct dcesrv_endpoint *endpoint,
-                                                            const char *uuid, uint32_t if_version)
+                                                            const struct GUID *uuid, uint32_t if_version)
 {
        struct dcesrv_if_list *ifl;
        for (ifl=endpoint->interface_list; ifl; ifl=ifl->next) {
@@ -165,7 +156,7 @@ NTSTATUS dcesrv_interface_register(struct dcesrv_context *dce_ctx,
 {
        struct dcesrv_endpoint *ep;
        struct dcesrv_if_list *ifl;
-       struct dcerpc_binding binding;
+       struct dcerpc_binding *binding;
        BOOL add_ep = False;
        NTSTATUS status;
        
@@ -178,13 +169,13 @@ NTSTATUS dcesrv_interface_register(struct dcesrv_context *dce_ctx,
 
        /* check if this endpoint exists
         */
-       if ((ep=find_endpoint(dce_ctx, &binding))==NULL) {
+       if ((ep=find_endpoint(dce_ctx, binding))==NULL) {
                ep = talloc(dce_ctx, struct dcesrv_endpoint);
                if (!ep) {
                        return NT_STATUS_NO_MEMORY;
                }
                ZERO_STRUCTP(ep);
-               ep->ep_description = binding;
+               ep->ep_description = talloc_reference(ep, binding);
                add_ep = True;
        }
 
@@ -299,14 +290,25 @@ static int dcesrv_endpoint_destructor(void *ptr)
 NTSTATUS dcesrv_endpoint_connect(struct dcesrv_context *dce_ctx,
                                 TALLOC_CTX *mem_ctx,
                                 const struct dcesrv_endpoint *ep,
-                                struct server_connection *srv_conn,
+                                struct auth_session_info *session_info,
+                                struct event_context *event_ctx,
+                                uint32_t state_flags,
                                 struct dcesrv_connection **_p)
 {
        struct dcesrv_connection *p;
 
+       if (!session_info) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
        p = talloc(mem_ctx, struct dcesrv_connection);
        NT_STATUS_HAVE_NO_MEMORY(p);
 
+       if (!talloc_reference(p, session_info)) {
+               talloc_free(p);
+               return NT_STATUS_NO_MEMORY;
+       }
+
        p->dce_ctx = dce_ctx;
        p->endpoint = ep;
        p->contexts = NULL;
@@ -316,9 +318,12 @@ NTSTATUS dcesrv_endpoint_connect(struct dcesrv_context *dce_ctx,
        p->partial_input = data_blob(NULL, 0);
        p->auth_state.auth_info = NULL;
        p->auth_state.gensec_security = NULL;
-       p->auth_state.session_info = NULL;
+       p->auth_state.session_info = session_info;
        p->auth_state.session_key = dcesrv_generic_session_key;
-       p->srv_conn = srv_conn;
+       p->event_ctx = event_ctx;
+       p->processing = False;
+       p->state_flags = state_flags;
+       ZERO_STRUCT(p->transport);
 
        talloc_set_destructor(p, dcesrv_endpoint_destructor);
 
@@ -333,7 +338,8 @@ NTSTATUS dcesrv_endpoint_search_connect(struct dcesrv_context *dce_ctx,
                                        TALLOC_CTX *mem_ctx,
                                        const struct dcerpc_binding *ep_description,
                                        struct auth_session_info *session_info,
-                                       struct server_connection *srv_conn,
+                                       struct event_context *event_ctx,
+                                       uint32_t state_flags,
                                        struct dcesrv_connection **dce_conn_p)
 {
        NTSTATUS status;
@@ -345,12 +351,9 @@ NTSTATUS dcesrv_endpoint_search_connect(struct dcesrv_context *dce_ctx,
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
-       status = dcesrv_endpoint_connect(dce_ctx, mem_ctx, ep, srv_conn, dce_conn_p);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
+       status = dcesrv_endpoint_connect(dce_ctx, mem_ctx, ep, session_info, event_ctx, state_flags, dce_conn_p);
+       NT_STATUS_NOT_OK_RETURN(status);
 
-       (*dce_conn_p)->auth_state.session_info = talloc_reference((*dce_conn_p), session_info);
        (*dce_conn_p)->auth_state.session_key = dcesrv_inherited_session_key;
 
        /* TODO: check security descriptor of the endpoint here 
@@ -362,7 +365,7 @@ NTSTATUS dcesrv_endpoint_search_connect(struct dcesrv_context *dce_ctx,
 }
 
 
-static void dcesrv_init_hdr(struct dcerpc_packet *pkt)
+static void dcesrv_init_hdr(struct ncacn_packet *pkt)
 {
        pkt->rpc_vers = 5;
        pkt->rpc_vers_minor = 0;
@@ -381,8 +384,8 @@ static void dcesrv_init_hdr(struct dcerpc_packet *pkt)
 */
 static NTSTATUS dcesrv_fault(struct dcesrv_call_state *call, uint32_t fault_code)
 {
-       struct dcerpc_packet pkt;
-       struct dcesrv_call_reply *rep;
+       struct ncacn_packet pkt;
+       struct data_blob_list_item *rep;
        NTSTATUS status;
 
        /* setup a bind_ack */
@@ -396,19 +399,19 @@ static NTSTATUS dcesrv_fault(struct dcesrv_call_state *call, uint32_t fault_code
        pkt.u.fault.cancel_count = 0;
        pkt.u.fault.status = fault_code;
 
-       rep = talloc(call, struct dcesrv_call_reply);
+       rep = talloc(call, struct data_blob_list_item);
        if (!rep) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       status = dcerpc_push_auth(&rep->data, call, &pkt, NULL);
+       status = ncacn_push_auth(&rep->blob, call, &pkt, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       dcerpc_set_frag_length(&rep->data, rep->data.length);
+       dcerpc_set_frag_length(&rep->blob, rep->blob.length);
 
-       DLIST_ADD_END(call->replies, rep, struct dcesrv_call_reply *);
+       DLIST_ADD_END(call->replies, rep, struct data_blob_list_item *);
        DLIST_ADD_END(call->conn->call_list, call, struct dcesrv_call_state *);
 
        return NT_STATUS_OK;    
@@ -420,8 +423,8 @@ static NTSTATUS dcesrv_fault(struct dcesrv_call_state *call, uint32_t fault_code
 */
 static NTSTATUS dcesrv_bind_nak(struct dcesrv_call_state *call, uint32_t reason)
 {
-       struct dcerpc_packet pkt;
-       struct dcesrv_call_reply *rep;
+       struct ncacn_packet pkt;
+       struct data_blob_list_item *rep;
        NTSTATUS status;
 
        /* setup a bind_nak */
@@ -431,21 +434,23 @@ static NTSTATUS dcesrv_bind_nak(struct dcesrv_call_state *call, uint32_t reason)
        pkt.ptype = DCERPC_PKT_BIND_NAK;
        pkt.pfc_flags = DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST;
        pkt.u.bind_nak.reject_reason = reason;
-       pkt.u.bind_nak.num_versions = 0;
+       if (pkt.u.bind_nak.reject_reason == DECRPC_BIND_PROTOCOL_VERSION_NOT_SUPPORTED) {
+               pkt.u.bind_nak.versions.v.num_versions = 0;
+       }
 
-       rep = talloc(call, struct dcesrv_call_reply);
+       rep = talloc(call, struct data_blob_list_item);
        if (!rep) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       status = dcerpc_push_auth(&rep->data, call, &pkt, NULL);
+       status = ncacn_push_auth(&rep->blob, call, &pkt, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       dcerpc_set_frag_length(&rep->data, rep->data.length);
+       dcerpc_set_frag_length(&rep->blob, rep->blob.length);
 
-       DLIST_ADD_END(call->replies, rep, struct dcesrv_call_reply *);
+       DLIST_ADD_END(call->replies, rep, struct data_blob_list_item *);
        DLIST_ADD_END(call->conn->call_list, call, struct dcesrv_call_state *);
 
        return NT_STATUS_OK;    
@@ -457,16 +462,16 @@ static NTSTATUS dcesrv_bind_nak(struct dcesrv_call_state *call, uint32_t reason)
 */
 static NTSTATUS dcesrv_bind(struct dcesrv_call_state *call)
 {
-       const char *uuid, *transfer_syntax;
        uint32_t if_version, transfer_syntax_version;
-       struct dcerpc_packet pkt;
-       struct dcesrv_call_reply *rep;
+       struct GUID uuid, *transfer_syntax_uuid;
+       struct ncacn_packet pkt;
+       struct data_blob_list_item *rep;
        NTSTATUS status;
        uint32_t result=0, reason=0;
        uint32_t context_id;
        const struct dcesrv_interface *iface;
 
-       if (call->pkt.u.bind.num_contexts != 1 ||
+       if (call->pkt.u.bind.num_contexts < 1 ||
            call->pkt.u.bind.ctx_list[0].num_transfer_syntaxes < 1) {
                return dcesrv_bind_nak(call, 0);
        }
@@ -479,24 +484,25 @@ static NTSTATUS dcesrv_bind(struct dcesrv_call_state *call)
        }
 
        if_version = call->pkt.u.bind.ctx_list[0].abstract_syntax.if_version;
-       uuid = GUID_string(call, &call->pkt.u.bind.ctx_list[0].abstract_syntax.uuid);
-       if (!uuid) {
-               return dcesrv_bind_nak(call, 0);
-       }
+       uuid = call->pkt.u.bind.ctx_list[0].abstract_syntax.uuid;
 
        transfer_syntax_version = call->pkt.u.bind.ctx_list[0].transfer_syntaxes[0].if_version;
-       transfer_syntax = GUID_string(call, 
-                                     &call->pkt.u.bind.ctx_list[0].transfer_syntaxes[0].uuid);
-       if (!transfer_syntax ||
-           strcasecmp(NDR_GUID, transfer_syntax) != 0 ||
-           NDR_GUID_VERSION != transfer_syntax_version) {
+       transfer_syntax_uuid = &call->pkt.u.bind.ctx_list[0].transfer_syntaxes[0].uuid;
+       if (!GUID_equal(&ndr_transfer_syntax.uuid, transfer_syntax_uuid) != 0 ||
+           ndr_transfer_syntax.if_version != transfer_syntax_version) {
+               char *uuid_str = GUID_string(call, transfer_syntax_uuid);
                /* we only do NDR encoded dcerpc */
+               DEBUG(0,("Non NDR transfer syntax requested - %s\n", uuid_str));
+               talloc_free(uuid_str);
                return dcesrv_bind_nak(call, 0);
        }
 
-       iface = find_interface_by_uuid(call->conn->endpoint, uuid, if_version);
+       iface = find_interface_by_uuid(call->conn->endpoint, &uuid, if_version);
        if (iface == NULL) {
-               DEBUG(2,("Request for unknown dcerpc interface %s/%d\n", uuid, if_version));
+               char *uuid_str = GUID_string(call, &uuid);
+               DEBUG(2,("Request for unknown dcerpc interface %s/%d\n", uuid_str, if_version));
+               talloc_free(uuid_str);
+
                /* we don't know about that interface */
                result = DCERPC_BIND_PROVIDER_REJECT;
                reason = DCERPC_BIND_REASON_ASYNTAX;            
@@ -524,8 +530,7 @@ static NTSTATUS dcesrv_bind(struct dcesrv_call_state *call)
 
        /* handle any authentication that is being requested */
        if (!dcesrv_auth_bind(call)) {
-               /* TODO: work out the right reject code */
-               return dcesrv_bind_nak(call, 0);
+               return dcesrv_bind_nak(call, DCERPC_BIND_REASON_INVALID_AUTH_TYPE);
        }
 
        /* setup a bind_ack */
@@ -550,8 +555,7 @@ static NTSTATUS dcesrv_bind(struct dcesrv_call_state *call)
        }
        pkt.u.bind_ack.ctx_list[0].result = result;
        pkt.u.bind_ack.ctx_list[0].reason = reason;
-       GUID_from_string(NDR_GUID, &pkt.u.bind_ack.ctx_list[0].syntax.uuid);
-       pkt.u.bind_ack.ctx_list[0].syntax.if_version = NDR_GUID_VERSION;
+       pkt.u.bind_ack.ctx_list[0].syntax = ndr_transfer_syntax;
        pkt.u.bind_ack.auth_info = data_blob(NULL, 0);
 
        if (!dcesrv_auth_bind_ack(call, &pkt)) {
@@ -561,26 +565,28 @@ static NTSTATUS dcesrv_bind(struct dcesrv_call_state *call)
        if (iface) {
                status = iface->bind(call, iface);
                if (!NT_STATUS_IS_OK(status)) {
+                       char *uuid_str = GUID_string(call, &uuid);
                        DEBUG(2,("Request for dcerpc interface %s/%d rejected: %s\n", 
-                                uuid, if_version, nt_errstr(status)));
+                                uuid_str, if_version, nt_errstr(status)));
+                       talloc_free(uuid_str);
                        return dcesrv_bind_nak(call, 0);
                }
        }
 
-       rep = talloc(call, struct dcesrv_call_reply);
+       rep = talloc(call, struct data_blob_list_item);
        if (!rep) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       status = dcerpc_push_auth(&rep->data, call, &pkt, 
+       status = ncacn_push_auth(&rep->blob, call, &pkt, 
                                  call->conn->auth_state.auth_info);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       dcerpc_set_frag_length(&rep->data, rep->data.length);
+       dcerpc_set_frag_length(&rep->blob, rep->blob.length);
 
-       DLIST_ADD_END(call->replies, rep, struct dcesrv_call_reply *);
+       DLIST_ADD_END(call->replies, rep, struct data_blob_list_item *);
        DLIST_ADD_END(call->conn->call_list, call, struct dcesrv_call_state *);
 
        return NT_STATUS_OK;
@@ -608,32 +614,29 @@ static NTSTATUS dcesrv_auth3(struct dcesrv_call_state *call)
 /*
   handle a bind request
 */
-static NTSTATUS dcesrv_alter_new_context(struct dcesrv_call_state *call, uint32 context_id)
+static NTSTATUS dcesrv_alter_new_context(struct dcesrv_call_state *call, uint32_t context_id)
 {
        uint32_t if_version, transfer_syntax_version;
-       const char *uuid, *transfer_syntax;
        struct dcesrv_connection_context *context;
        const struct dcesrv_interface *iface;
+       struct GUID uuid, *transfer_syntax_uuid;
 
        if_version = call->pkt.u.alter.ctx_list[0].abstract_syntax.if_version;
-       uuid = GUID_string(call, &call->pkt.u.alter.ctx_list[0].abstract_syntax.uuid);
-       if (!uuid) {
-               return NT_STATUS_NO_MEMORY;
-       }
+       uuid = call->pkt.u.alter.ctx_list[0].abstract_syntax.uuid;
 
        transfer_syntax_version = call->pkt.u.alter.ctx_list[0].transfer_syntaxes[0].if_version;
-       transfer_syntax = GUID_string(call, 
-                                     &call->pkt.u.alter.ctx_list[0].transfer_syntaxes[0].uuid);
-       if (!transfer_syntax ||
-           strcasecmp(NDR_GUID, transfer_syntax) != 0 ||
-           NDR_GUID_VERSION != transfer_syntax_version) {
+       transfer_syntax_uuid = &call->pkt.u.alter.ctx_list[0].transfer_syntaxes[0].uuid;
+       if (!GUID_equal(transfer_syntax_uuid, &ndr_transfer_syntax.uuid) ||
+           ndr_transfer_syntax.if_version != transfer_syntax_version) {
                /* we only do NDR encoded dcerpc */
-               return NT_STATUS_NO_MEMORY;
+               return NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED;
        }
 
-       iface = find_interface_by_uuid(call->conn->endpoint, uuid, if_version);
+       iface = find_interface_by_uuid(call->conn->endpoint, &uuid, if_version);
        if (iface == NULL) {
-               DEBUG(2,("Request for unknown dcerpc interface %s/%d\n", uuid, if_version));
+               char *uuid_str = GUID_string(call, &uuid);
+               DEBUG(2,("Request for unknown dcerpc interface %s/%d\n", uuid_str, if_version));
+               talloc_free(uuid_str);
                return NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED;
        }
 
@@ -655,12 +658,12 @@ static NTSTATUS dcesrv_alter_new_context(struct dcesrv_call_state *call, uint32
 
 
 /*
-  handle a bind request
+  handle a alter context request
 */
 static NTSTATUS dcesrv_alter(struct dcesrv_call_state *call)
 {
-       struct dcerpc_packet pkt;
-       struct dcesrv_call_reply *rep;
+       struct ncacn_packet pkt;
+       struct data_blob_list_item *rep;
        NTSTATUS status;
        uint32_t result=0, reason=0;
        uint32_t context_id;
@@ -700,8 +703,7 @@ static NTSTATUS dcesrv_alter(struct dcesrv_call_state *call)
        }
        pkt.u.alter_resp.ctx_list[0].result = result;
        pkt.u.alter_resp.ctx_list[0].reason = reason;
-       GUID_from_string(NDR_GUID, &pkt.u.alter_resp.ctx_list[0].syntax.uuid);
-       pkt.u.alter_resp.ctx_list[0].syntax.if_version = NDR_GUID_VERSION;
+       pkt.u.alter_resp.ctx_list[0].syntax = ndr_transfer_syntax;
        pkt.u.alter_resp.auth_info = data_blob(NULL, 0);
        pkt.u.alter_resp.secondary_address = "";
 
@@ -709,20 +711,20 @@ static NTSTATUS dcesrv_alter(struct dcesrv_call_state *call)
                return dcesrv_bind_nak(call, 0);
        }
 
-       rep = talloc(call, struct dcesrv_call_reply);
+       rep = talloc(call, struct data_blob_list_item);
        if (!rep) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       status = dcerpc_push_auth(&rep->data, call, &pkt, 
+       status = ncacn_push_auth(&rep->blob, call, &pkt, 
                                  call->conn->auth_state.auth_info);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       dcerpc_set_frag_length(&rep->data, rep->data.length);
+       dcerpc_set_frag_length(&rep->blob, rep->blob.length);
 
-       DLIST_ADD_END(call->replies, rep, struct dcesrv_call_reply *);
+       DLIST_ADD_END(call->replies, rep, struct data_blob_list_item *);
        DLIST_ADD_END(call->conn->call_list, call, struct dcesrv_call_state *);
 
        return NT_STATUS_OK;
@@ -738,9 +740,15 @@ static NTSTATUS dcesrv_request(struct dcesrv_call_state *call)
        struct dcesrv_connection_context *context;
 
        call->fault_code        = 0;
-       call->state_flags       = call->conn->dce_ctx->state_flags;
+       call->state_flags       = call->conn->state_flags;
        call->time              = timeval_current();
 
+       /* if authenticated, and the mech we use can't do async replies, don't use them... */
+       if (call->conn->auth_state.gensec_security && 
+           !gensec_have_feature(call->conn->auth_state.gensec_security, GENSEC_FEATURE_ASYNC_REPLIES)) {
+               call->state_flags &= ~DCESRV_CALL_STATE_FLAG_MAY_ASYNC;
+       }
+
        context = dcesrv_find_context(call->conn, call->pkt.u.request.context_id);
        if (context == NULL) {
                return dcesrv_fault(call, DCERPC_FAULT_UNK_IF);
@@ -749,8 +757,10 @@ static NTSTATUS dcesrv_request(struct dcesrv_call_state *call)
        pull = ndr_pull_init_blob(&call->pkt.u.request.stub_and_verifier, call);
        NT_STATUS_HAVE_NO_MEMORY(pull);
 
+       pull->flags |= LIBNDR_FLAG_REF_ALLOC;
+
        call->context   = context;
-       call->event_ctx = context->conn->srv_conn->event.ctx;
+       call->event_ctx = context->conn->event_ctx;
        call->ndr_pull  = pull;
 
        if (call->pkt.pfc_flags & DCERPC_PFC_FLAG_ORPC) {
@@ -776,6 +786,10 @@ static NTSTATUS dcesrv_request(struct dcesrv_call_state *call)
        /* call the dispatch function */
        status = context->iface->dispatch(call, call, call->r);
        if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(5,("dcerpc fault in call %s:%02x - %s\n",
+                        context->iface->name, 
+                        call->pkt.u.request.opnum,
+                        dcerpc_errstr(pull, call->fault_code)));
                return dcesrv_fault(call, call->fault_code);
        }
 
@@ -827,10 +841,10 @@ NTSTATUS dcesrv_reply(struct dcesrv_call_state *call)
 
        do {
                uint32_t length;
-               struct dcesrv_call_reply *rep;
-               struct dcerpc_packet pkt;
+               struct data_blob_list_item *rep;
+               struct ncacn_packet pkt;
 
-               rep = talloc(call, struct dcesrv_call_reply);
+               rep = talloc(call, struct data_blob_list_item);
                NT_STATUS_HAVE_NO_MEMORY(rep);
 
                length = stub.length;
@@ -858,13 +872,13 @@ NTSTATUS dcesrv_reply(struct dcesrv_call_state *call)
                pkt.u.response.stub_and_verifier.data = stub.data;
                pkt.u.response.stub_and_verifier.length = length;
 
-               if (!dcesrv_auth_response(call, &rep->data, &pkt)) {
+               if (!dcesrv_auth_response(call, &rep->blob, &pkt)) {
                        return dcesrv_fault(call, DCERPC_FAULT_OTHER);          
                }
 
-               dcerpc_set_frag_length(&rep->data, rep->data.length);
+               dcerpc_set_frag_length(&rep->blob, rep->blob.length);
 
-               DLIST_ADD_END(call->replies, rep, struct dcesrv_call_reply *);
+               DLIST_ADD_END(call->replies, rep, struct data_blob_list_item *);
                
                stub.data += length;
                stub.length -= length;
@@ -875,15 +889,31 @@ NTSTATUS dcesrv_reply(struct dcesrv_call_state *call)
        DLIST_ADD_END(call->conn->call_list, call, struct dcesrv_call_state *);
 
        if (call->conn->call_list && call->conn->call_list->replies) {
-               if (call->conn->srv_conn &&
-                   call->conn->srv_conn->event.fde) {
-                       call->conn->srv_conn->event.fde->flags |= EVENT_FD_WRITE;
+               if (call->conn->transport.report_output_data) {
+                       call->conn->transport.report_output_data(call->conn);
                }
        }
 
        return NT_STATUS_OK;
 }
 
+struct socket_address *dcesrv_connection_get_my_addr(struct dcesrv_connection *conn, TALLOC_CTX *mem_ctx)
+{
+       if (!conn->transport.get_my_addr) {
+               return NULL;
+       }
+
+       return conn->transport.get_my_addr(conn, mem_ctx);
+}
+
+struct socket_address *dcesrv_connection_get_peer_addr(struct dcesrv_connection *conn, TALLOC_CTX *mem_ctx)
+{
+       if (!conn->transport.get_peer_addr) {
+               return NULL;
+       }
+
+       return conn->transport.get_peer_addr(conn, mem_ctx);
+}
 
 /*
   work out if we have a full packet yet
@@ -932,9 +962,10 @@ NTSTATUS dcesrv_input_process(struct dcesrv_connection *dce_conn)
                talloc_free(dce_conn->partial_input.data);
                return NT_STATUS_NO_MEMORY;
        }
-       call->conn = dce_conn;
-       call->replies = NULL;
-       call->context = NULL;
+       call->conn      = dce_conn;
+       call->replies   = NULL;
+       call->context   = NULL;
+       call->event_ctx = dce_conn->event_ctx;
 
        blob = dce_conn->partial_input;
        blob.length = dcerpc_get_frag_length(&blob);
@@ -950,7 +981,7 @@ NTSTATUS dcesrv_input_process(struct dcesrv_connection *dce_conn)
                ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
        }
 
-       status = ndr_pull_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, &call->pkt);
+       status = ndr_pull_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, &call->pkt);
        if (!NT_STATUS_IS_OK(status)) {
                talloc_free(dce_conn->partial_input.data);
                talloc_free(call);
@@ -962,7 +993,7 @@ NTSTATUS dcesrv_input_process(struct dcesrv_connection *dce_conn)
        if (call->pkt.ptype == DCERPC_PKT_REQUEST &&
            !dcesrv_auth_request(call, &blob)) {
                dce_partial_advance(dce_conn, blob.length);
-               return dcesrv_fault(call, DCERPC_FAULT_LOGON_FAILURE);          
+               return dcesrv_fault(call, DCERPC_FAULT_ACCESS_DENIED);          
        }
 
        dce_partial_advance(dce_conn, blob.length);
@@ -1088,20 +1119,19 @@ NTSTATUS dcesrv_input(struct dcesrv_connection *dce_conn, const DATA_BLOB *data)
 
   The first argument to write_fn() will be 'private', the second will
   be a pointer to a buffer containing the data to be sent and the 3rd
-  will be the number of bytes to be sent.
+  will be a pointer to a size_t variable that will be set to the
+  number of bytes that are consumed from the output.
 
-  write_fn() should return the number of bytes successfully written.
-
-  this will return STATUS_BUFFER_OVERFLOW if there is more to be read
   from the current fragment
 */
-NTSTATUS dcesrv_output(struct dcesrv_connection *dce_conn, 
-                      void *private,
-                      ssize_t (*write_fn)(void *, DATA_BLOB *))
+_PUBLIC_ NTSTATUS dcesrv_output(struct dcesrv_connection *dce_conn, 
+                      void *private_data,
+                      NTSTATUS (*write_fn)(void *private_data, DATA_BLOB *output, size_t *nwritten))
 {
+       NTSTATUS status;
        struct dcesrv_call_state *call;
-       struct dcesrv_call_reply *rep;
-       ssize_t nwritten;
+       struct data_blob_list_item *rep;
+       size_t nwritten;
 
        call = dce_conn->call_list;
        if (!call || !call->replies) {
@@ -1116,21 +1146,15 @@ NTSTATUS dcesrv_output(struct dcesrv_connection *dce_conn,
        }
        rep = call->replies;
 
-       nwritten = write_fn(private, &rep->data);
-       if (nwritten == -1) {
-               /* TODO: hmm, how do we cope with this? destroy the
-                  connection perhaps? */
-               return NT_STATUS_UNSUCCESSFUL;
-       }
+       status = write_fn(private_data, &rep->blob, &nwritten);
+       NT_STATUS_IS_ERR_RETURN(status);
 
-       rep->data.length -= nwritten;
-       rep->data.data += nwritten;
+       rep->blob.length -= nwritten;
+       rep->blob.data += nwritten;
 
-       if (rep->data.length == 0) {
+       if (rep->blob.length == 0) {
                /* we're done with this section of the call */
                DLIST_REMOVE(call->replies, rep);
-       } else {
-               return STATUS_BUFFER_OVERFLOW;
        }
 
        if (call->replies == NULL) {
@@ -1139,34 +1163,10 @@ NTSTATUS dcesrv_output(struct dcesrv_connection *dce_conn,
                talloc_free(call);
        }
 
-       return NT_STATUS_OK;
-}
-
-
-/*
-  write_fn() for dcesrv_output_blob()
-*/
-static ssize_t dcesrv_output_blob_write_fn(void *private, DATA_BLOB *out)
-{
-       DATA_BLOB *blob = private;
-       if (out->length < blob->length) {
-               blob->length = out->length;
-       }
-       memcpy(blob->data, out->data, blob->length);
-       return blob->length;
-}
-
-/*
-  a simple wrapper for dcesrv_output() for when we want to output
-  into a data blob
-*/
-NTSTATUS dcesrv_output_blob(struct dcesrv_connection *dce_conn, 
-                           DATA_BLOB *blob)
-{
-       return dcesrv_output(dce_conn, blob, dcesrv_output_blob_write_fn);
+       return status;
 }
 
-static NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx, const char **endpoint_servers, uint32_t state_flags, struct dcesrv_context **_dce_ctx)
+static NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx, const char **endpoint_servers, struct dcesrv_context **_dce_ctx)
 {
        NTSTATUS status;
        struct dcesrv_context *dce_ctx;
@@ -1180,7 +1180,6 @@ static NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx, const char **endpoint_s
        dce_ctx = talloc(mem_ctx, struct dcesrv_context);
        NT_STATUS_HAVE_NO_MEMORY(dce_ctx);
        dce_ctx->endpoint_list  = NULL;
-       dce_ctx->state_flags    = state_flags;
 
        for (i=0;endpoint_servers[i];i++) {
                const struct dcesrv_endpoint_server *ep_server;
@@ -1204,59 +1203,20 @@ static NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx, const char **endpoint_s
 }
 
 /*
-  initialise the dcerpc server context
+  initialise the dcerpc server context for ncacn_np based services
 */
 NTSTATUS dcesrv_init_ipc_context(TALLOC_CTX *mem_ctx, struct dcesrv_context **_dce_ctx)
 {
        NTSTATUS status;
        struct dcesrv_context *dce_ctx;
 
-       status = dcesrv_init_context(mem_ctx, lp_dcerpc_endpoint_servers(), 0, &dce_ctx);
+       status = dcesrv_init_context(mem_ctx, lp_dcerpc_endpoint_servers(), &dce_ctx);
        NT_STATUS_NOT_OK_RETURN(status);
 
        *_dce_ctx = dce_ctx;
        return NT_STATUS_OK;
 }
 
-static void dcesrv_init(struct server_service *service)
-{
-       NTSTATUS status;
-       struct dcesrv_context *dce_ctx;
-
-       DEBUG(1,("dcesrv_init\n"));
-
-       status = dcesrv_init_context(service,
-                                    lp_dcerpc_endpoint_servers(),
-                                    DCESRV_CALL_STATE_FLAG_MAY_ASYNC,
-                                    &dce_ctx);
-       if (!NT_STATUS_IS_OK(status)) {
-               return;
-       }
-
-       service->service.private_data = dce_ctx;
-
-       dcesrv_sock_init(service);
-
-       return; 
-}
-
-static void dcesrv_accept(struct server_connection *srv_conn)
-{
-       dcesrv_sock_accept(srv_conn);
-}
-
-static void dcesrv_recv(struct server_connection *srv_conn, 
-                       struct timeval t, uint16_t flags)
-{
-       dcesrv_sock_recv(srv_conn, t, flags);
-}
-
-static void dcesrv_send(struct server_connection *srv_conn, 
-                       struct timeval t, uint16_t flags)
-{
-       dcesrv_sock_send(srv_conn, t, flags);
-}
-
 /* the list of currently registered DCERPC endpoint servers.
  */
 static struct ep_server {
@@ -1338,32 +1298,79 @@ const struct dcesrv_critical_sizes *dcerpc_module_version(void)
        return &critical_sizes;
 }
 
-static const struct server_stream_ops dcesrv_stream_ops = {
-       .name                   = "rpc",
-       .socket_init            = NULL,
-       .accept_connection      = dcesrv_accept,
-       .recv_handler           = dcesrv_recv,
-       .send_handler           = dcesrv_send,
-       .idle_handler           = NULL,
-       .close_connection       = NULL
-};
-
-const struct server_stream_ops *dcesrv_get_stream_ops(void)
+/*
+  open the dcerpc server sockets
+*/
+static void dcesrv_task_init(struct task_server *task)
 {
-       return &dcesrv_stream_ops;
-}
+       NTSTATUS status;
+       struct dcesrv_context *dce_ctx;
+       struct dcesrv_endpoint *e;
 
-static const struct server_service_ops dcesrv_ops = {
-       .name                   = "rpc",
-       .service_init           = dcesrv_init,
-};
+       task_server_set_title(task, "task[dcesrv]");
 
-const struct server_service_ops *dcesrv_get_ops(void)
-{
-       return &dcesrv_ops;
+       status = dcesrv_init_context(task->event_ctx,
+                                    lp_dcerpc_endpoint_servers(),
+                                    &dce_ctx);
+       if (!NT_STATUS_IS_OK(status)) goto failed;
+
+       /* Make sure the directory for NCALRPC exists */
+       if (!directory_exist(lp_ncalrpc_dir())) {
+               mkdir(lp_ncalrpc_dir(), 0755);
+       }
+
+       for (e=dce_ctx->endpoint_list;e;e=e->next) {
+               switch (e->ep_description->transport) {
+               case NCACN_UNIX_STREAM:
+                       status = dcesrv_add_ep_unix(dce_ctx, e, task->event_ctx, task->model_ops);
+                       if (!NT_STATUS_IS_OK(status)) goto failed;
+                       break;
+               
+               case NCALRPC:
+                       status = dcesrv_add_ep_ncalrpc(dce_ctx, e, task->event_ctx, task->model_ops);
+                       if (!NT_STATUS_IS_OK(status)) goto failed;
+                       break;
+
+               case NCACN_IP_TCP:
+                       status = dcesrv_add_ep_tcp(dce_ctx, e, task->event_ctx, task->model_ops);
+                       if (!NT_STATUS_IS_OK(status)) goto failed;
+                       break;
+                       
+               case NCACN_NP:
+/*                     FIXME: status = dcesrv_add_ep_np(dce_ctx, e, task->event_ctx, task->model_ops);
+                       if (!NT_STATUS_IS_OK(status)) goto failed;
+*/                     break;
+
+               default:
+                       status = NT_STATUS_NOT_SUPPORTED;
+                       if (!NT_STATUS_IS_OK(status)) goto failed;
+               }
+       }
+
+       return;
+failed:
+       task_server_terminate(task, "Failed to startup dcerpc server task");    
+}
+
+/*
+  called on startup of the smb server service It's job is to start
+  listening on all configured sockets
+*/
+static NTSTATUS dcesrv_init(struct event_context *event_context, 
+                           const struct model_ops *model_ops)
+{      
+       return task_server_startup(event_context, model_ops, dcesrv_task_init);
 }
 
 NTSTATUS server_service_rpc_init(void)
 {
-       return NT_STATUS_OK;    
+       init_module_fn static_init[] = STATIC_dcerpc_server_MODULES;
+       init_module_fn *shared_init = load_samba_modules(NULL, "dcerpc_server");
+
+       run_init_functions(static_init);
+       run_init_functions(shared_init);
+
+       talloc_free(shared_init);
+       
+       return register_server_service("rpc", dcesrv_init);
 }