s3/s4: smbd, rpc, ldap, cldap, kdc services.
[Samba/wip.git] / source4 / rpc_server / dcerpc_server.c
blob4d5e166961c6028629b2d6868fb09662efe97a43
1 /*
2 Unix SMB/CIFS implementation.
4 server side dcerpc core code
6 Copyright (C) Andrew Tridgell 2003-2005
7 Copyright (C) Stefan (metze) Metzmacher 2004-2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "auth/auth.h"
25 #include "auth/gensec/gensec.h"
26 #include "../lib/util/dlinklist.h"
27 #include "rpc_server/dcerpc_server.h"
28 #include "rpc_server/dcerpc_server_proto.h"
29 #include "rpc_server/common/proto.h"
30 #include "librpc/rpc/dcerpc_proto.h"
31 #include "system/filesys.h"
32 #include "libcli/security/security.h"
33 #include "param/param.h"
34 #include "../lib/tsocket/tsocket.h"
35 #include "../libcli/named_pipe_auth/npa_tstream.h"
36 #include "smbd/service_stream.h"
37 #include "../lib/tsocket/tsocket.h"
38 #include "lib/socket/socket.h"
39 #include "smbd/process_model.h"
40 #include "lib/messaging/irpc.h"
41 #include "librpc/rpc/rpc_common.h"
42 #include "lib/util/samba_modules.h"
43 #include "librpc/gen_ndr/ndr_dcerpc.h"
45 /* this is only used when the client asks for an unknown interface */
46 #define DUMMY_ASSOC_GROUP 0x0FFFFFFF
48 extern const struct dcesrv_interface dcesrv_mgmt_interface;
52 find an association group given a assoc_group_id
54 static struct dcesrv_assoc_group *dcesrv_assoc_group_find(struct dcesrv_context *dce_ctx,
55 uint32_t id)
57 void *id_ptr;
59 id_ptr = idr_find(dce_ctx->assoc_groups_idr, id);
60 if (id_ptr == NULL) {
61 return NULL;
63 return talloc_get_type_abort(id_ptr, struct dcesrv_assoc_group);
67 take a reference to an existing association group
69 static struct dcesrv_assoc_group *dcesrv_assoc_group_reference(TALLOC_CTX *mem_ctx,
70 struct dcesrv_context *dce_ctx,
71 uint32_t id)
73 struct dcesrv_assoc_group *assoc_group;
75 assoc_group = dcesrv_assoc_group_find(dce_ctx, id);
76 if (assoc_group == NULL) {
77 DEBUG(0,(__location__ ": Failed to find assoc_group 0x%08x\n", id));
78 return NULL;
80 return talloc_reference(mem_ctx, assoc_group);
83 static int dcesrv_assoc_group_destructor(struct dcesrv_assoc_group *assoc_group)
85 int ret;
86 ret = idr_remove(assoc_group->dce_ctx->assoc_groups_idr, assoc_group->id);
87 if (ret != 0) {
88 DEBUG(0,(__location__ ": Failed to remove assoc_group 0x%08x\n",
89 assoc_group->id));
91 return 0;
95 allocate a new association group
97 static struct dcesrv_assoc_group *dcesrv_assoc_group_new(TALLOC_CTX *mem_ctx,
98 struct dcesrv_context *dce_ctx)
100 struct dcesrv_assoc_group *assoc_group;
101 int id;
103 assoc_group = talloc_zero(mem_ctx, struct dcesrv_assoc_group);
104 if (assoc_group == NULL) {
105 return NULL;
108 id = idr_get_new_random(dce_ctx->assoc_groups_idr, assoc_group, UINT16_MAX);
109 if (id == -1) {
110 talloc_free(assoc_group);
111 DEBUG(0,(__location__ ": Out of association groups!\n"));
112 return NULL;
115 assoc_group->id = id;
116 assoc_group->dce_ctx = dce_ctx;
118 talloc_set_destructor(assoc_group, dcesrv_assoc_group_destructor);
120 return assoc_group;
125 see if two endpoints match
127 static bool endpoints_match(const struct dcerpc_binding *ep1,
128 const struct dcerpc_binding *ep2)
130 enum dcerpc_transport_t t1;
131 enum dcerpc_transport_t t2;
132 const char *e1;
133 const char *e2;
135 t1 = dcerpc_binding_get_transport(ep1);
136 t2 = dcerpc_binding_get_transport(ep2);
138 e1 = dcerpc_binding_get_string_option(ep1, "endpoint");
139 e2 = dcerpc_binding_get_string_option(ep2, "endpoint");
141 if (t1 != t2) {
142 return false;
145 if (!e1 || !e2) {
146 return e1 == e2;
149 if (strcasecmp(e1, e2) != 0) {
150 return false;
153 return true;
157 find an endpoint in the dcesrv_context
159 static struct dcesrv_endpoint *find_endpoint(struct dcesrv_context *dce_ctx,
160 const struct dcerpc_binding *ep_description)
162 struct dcesrv_endpoint *ep;
163 for (ep=dce_ctx->endpoint_list; ep; ep=ep->next) {
164 if (endpoints_match(ep->ep_description, ep_description)) {
165 return ep;
168 return NULL;
172 find a registered context_id from a bind or alter_context
174 static struct dcesrv_connection_context *dcesrv_find_context(struct dcesrv_connection *conn,
175 uint32_t context_id)
177 struct dcesrv_connection_context *c;
178 for (c=conn->contexts;c;c=c->next) {
179 if (c->context_id == context_id) return c;
181 return NULL;
185 see if a uuid and if_version match to an interface
187 static bool interface_match(const struct dcesrv_interface *if1,
188 const struct dcesrv_interface *if2)
190 return (if1->syntax_id.if_version == if2->syntax_id.if_version &&
191 GUID_equal(&if1->syntax_id.uuid, &if2->syntax_id.uuid));
195 find the interface operations on an endpoint
197 static const struct dcesrv_interface *find_interface(const struct dcesrv_endpoint *endpoint,
198 const struct dcesrv_interface *iface)
200 struct dcesrv_if_list *ifl;
201 for (ifl=endpoint->interface_list; ifl; ifl=ifl->next) {
202 if (interface_match(&(ifl->iface), iface)) {
203 return &(ifl->iface);
206 return NULL;
210 see if a uuid and if_version match to an interface
212 static bool interface_match_by_uuid(const struct dcesrv_interface *iface,
213 const struct GUID *uuid, uint32_t if_version)
215 return (iface->syntax_id.if_version == if_version &&
216 GUID_equal(&iface->syntax_id.uuid, uuid));
220 find the interface operations on an endpoint by uuid
222 static const struct dcesrv_interface *find_interface_by_uuid(const struct dcesrv_endpoint *endpoint,
223 const struct GUID *uuid, uint32_t if_version)
225 struct dcesrv_if_list *ifl;
226 for (ifl=endpoint->interface_list; ifl; ifl=ifl->next) {
227 if (interface_match_by_uuid(&(ifl->iface), uuid, if_version)) {
228 return &(ifl->iface);
231 return NULL;
235 find the earlier parts of a fragmented call awaiting reassembily
237 static struct dcesrv_call_state *dcesrv_find_fragmented_call(struct dcesrv_connection *dce_conn, uint16_t call_id)
239 struct dcesrv_call_state *c;
240 for (c=dce_conn->incoming_fragmented_call_list;c;c=c->next) {
241 if (c->pkt.call_id == call_id) {
242 return c;
245 return NULL;
249 register an interface on an endpoint
251 _PUBLIC_ NTSTATUS dcesrv_interface_register(struct dcesrv_context *dce_ctx,
252 const char *ep_name,
253 const struct dcesrv_interface *iface,
254 const struct security_descriptor *sd)
256 struct dcesrv_endpoint *ep;
257 struct dcesrv_if_list *ifl;
258 struct dcerpc_binding *binding;
259 bool add_ep = false;
260 NTSTATUS status;
262 status = dcerpc_parse_binding(dce_ctx, ep_name, &binding);
264 if (NT_STATUS_IS_ERR(status)) {
265 DEBUG(0, ("Trouble parsing binding string '%s'\n", ep_name));
266 return status;
269 /* check if this endpoint exists
271 if ((ep=find_endpoint(dce_ctx, binding))==NULL) {
272 ep = talloc(dce_ctx, struct dcesrv_endpoint);
273 if (!ep) {
274 return NT_STATUS_NO_MEMORY;
276 ZERO_STRUCTP(ep);
277 ep->ep_description = talloc_move(ep, &binding);
278 add_ep = true;
280 /* add mgmt interface */
281 ifl = talloc(ep, struct dcesrv_if_list);
282 if (!ifl) {
283 return NT_STATUS_NO_MEMORY;
286 memcpy(&(ifl->iface), &dcesrv_mgmt_interface,
287 sizeof(struct dcesrv_interface));
289 DLIST_ADD(ep->interface_list, ifl);
292 /* see if the interface is already registered on te endpoint */
293 if (find_interface(ep, iface)!=NULL) {
294 DEBUG(0,("dcesrv_interface_register: interface '%s' already registered on endpoint '%s'\n",
295 iface->name, ep_name));
296 return NT_STATUS_OBJECT_NAME_COLLISION;
299 /* talloc a new interface list element */
300 ifl = talloc(ep, struct dcesrv_if_list);
301 if (!ifl) {
302 return NT_STATUS_NO_MEMORY;
305 /* copy the given interface struct to the one on the endpoints interface list */
306 memcpy(&(ifl->iface),iface, sizeof(struct dcesrv_interface));
308 /* if we have a security descriptor given,
309 * we should see if we can set it up on the endpoint
311 if (sd != NULL) {
312 /* if there's currently no security descriptor given on the endpoint
313 * we try to set it
315 if (ep->sd == NULL) {
316 ep->sd = security_descriptor_copy(ep, sd);
319 /* if now there's no security descriptor given on the endpoint
320 * something goes wrong, either we failed to copy the security descriptor
321 * or there was already one on the endpoint
323 if (ep->sd != NULL) {
324 DEBUG(0,("dcesrv_interface_register: interface '%s' failed to setup a security descriptor\n"
325 " on endpoint '%s'\n",
326 iface->name, ep_name));
327 if (add_ep) free(ep);
328 free(ifl);
329 return NT_STATUS_OBJECT_NAME_COLLISION;
333 /* finally add the interface on the endpoint */
334 DLIST_ADD(ep->interface_list, ifl);
336 /* if it's a new endpoint add it to the dcesrv_context */
337 if (add_ep) {
338 DLIST_ADD(dce_ctx->endpoint_list, ep);
341 DEBUG(4,("dcesrv_interface_register: interface '%s' registered on endpoint '%s'\n",
342 iface->name, ep_name));
344 return NT_STATUS_OK;
347 NTSTATUS dcesrv_inherited_session_key(struct dcesrv_connection *p,
348 DATA_BLOB *session_key)
350 if (p->auth_state.session_info->session_key.length) {
351 *session_key = p->auth_state.session_info->session_key;
352 return NT_STATUS_OK;
354 return NT_STATUS_NO_USER_SESSION_KEY;
358 fetch the user session key - may be default (above) or the SMB session key
360 The key is always truncated to 16 bytes
362 _PUBLIC_ NTSTATUS dcesrv_fetch_session_key(struct dcesrv_connection *p,
363 DATA_BLOB *session_key)
365 NTSTATUS status = p->auth_state.session_key(p, session_key);
366 if (!NT_STATUS_IS_OK(status)) {
367 return status;
370 session_key->length = MIN(session_key->length, 16);
372 return NT_STATUS_OK;
376 connect to a dcerpc endpoint
378 _PUBLIC_ NTSTATUS dcesrv_endpoint_connect(struct dcesrv_context *dce_ctx,
379 TALLOC_CTX *mem_ctx,
380 const struct dcesrv_endpoint *ep,
381 struct auth_session_info *session_info,
382 struct tevent_context *event_ctx,
383 struct imessaging_context *msg_ctx,
384 struct server_id server_id,
385 uint32_t state_flags,
386 struct dcesrv_connection **_p)
388 struct dcesrv_connection *p;
390 if (!session_info) {
391 return NT_STATUS_ACCESS_DENIED;
394 p = talloc_zero(mem_ctx, struct dcesrv_connection);
395 NT_STATUS_HAVE_NO_MEMORY(p);
397 if (!talloc_reference(p, session_info)) {
398 talloc_free(p);
399 return NT_STATUS_NO_MEMORY;
402 p->dce_ctx = dce_ctx;
403 p->endpoint = ep;
404 p->packet_log_dir = lpcfg_lock_directory(dce_ctx->lp_ctx);
405 p->auth_state.session_info = session_info;
406 p->auth_state.session_key = dcesrv_generic_session_key;
407 p->event_ctx = event_ctx;
408 p->msg_ctx = msg_ctx;
409 p->server_id = server_id;
410 p->state_flags = state_flags;
412 *_p = p;
413 return NT_STATUS_OK;
417 move a call from an existing linked list to the specified list. This
418 prevents bugs where we forget to remove the call from a previous
419 list when moving it.
421 static void dcesrv_call_set_list(struct dcesrv_call_state *call,
422 enum dcesrv_call_list list)
424 switch (call->list) {
425 case DCESRV_LIST_NONE:
426 break;
427 case DCESRV_LIST_CALL_LIST:
428 DLIST_REMOVE(call->conn->call_list, call);
429 break;
430 case DCESRV_LIST_FRAGMENTED_CALL_LIST:
431 DLIST_REMOVE(call->conn->incoming_fragmented_call_list, call);
432 break;
433 case DCESRV_LIST_PENDING_CALL_LIST:
434 DLIST_REMOVE(call->conn->pending_call_list, call);
435 break;
437 call->list = list;
438 switch (list) {
439 case DCESRV_LIST_NONE:
440 break;
441 case DCESRV_LIST_CALL_LIST:
442 DLIST_ADD_END(call->conn->call_list, call, struct dcesrv_call_state *);
443 break;
444 case DCESRV_LIST_FRAGMENTED_CALL_LIST:
445 DLIST_ADD_END(call->conn->incoming_fragmented_call_list, call, struct dcesrv_call_state *);
446 break;
447 case DCESRV_LIST_PENDING_CALL_LIST:
448 DLIST_ADD_END(call->conn->pending_call_list, call, struct dcesrv_call_state *);
449 break;
455 return a dcerpc bind_nak
457 static NTSTATUS dcesrv_bind_nak(struct dcesrv_call_state *call, uint32_t reason)
459 struct ncacn_packet pkt;
460 struct dcerpc_bind_nak_version version;
461 struct data_blob_list_item *rep;
462 NTSTATUS status;
464 /* setup a bind_nak */
465 dcesrv_init_hdr(&pkt, lpcfg_rpc_big_endian(call->conn->dce_ctx->lp_ctx));
466 pkt.auth_length = 0;
467 pkt.call_id = call->pkt.call_id;
468 pkt.ptype = DCERPC_PKT_BIND_NAK;
469 pkt.pfc_flags = DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST;
470 pkt.u.bind_nak.reject_reason = reason;
471 version.rpc_vers = 5;
472 version.rpc_vers_minor = 0;
473 pkt.u.bind_nak.num_versions = 1;
474 pkt.u.bind_nak.versions = &version;
475 pkt.u.bind_nak._pad = data_blob_null;
477 rep = talloc(call, struct data_blob_list_item);
478 if (!rep) {
479 return NT_STATUS_NO_MEMORY;
482 status = ncacn_push_auth(&rep->blob, call, &pkt, NULL);
483 if (!NT_STATUS_IS_OK(status)) {
484 return status;
487 dcerpc_set_frag_length(&rep->blob, rep->blob.length);
489 DLIST_ADD_END(call->replies, rep, struct data_blob_list_item *);
490 dcesrv_call_set_list(call, DCESRV_LIST_CALL_LIST);
492 if (call->conn->call_list && call->conn->call_list->replies) {
493 if (call->conn->transport.report_output_data) {
494 call->conn->transport.report_output_data(call->conn);
498 return NT_STATUS_OK;
501 static int dcesrv_connection_context_destructor(struct dcesrv_connection_context *c)
503 DLIST_REMOVE(c->conn->contexts, c);
505 if (c->iface && c->iface->unbind) {
506 c->iface->unbind(c, c->iface);
509 return 0;
513 handle a bind request
515 static NTSTATUS dcesrv_bind(struct dcesrv_call_state *call)
517 uint32_t if_version, transfer_syntax_version;
518 struct GUID uuid, *transfer_syntax_uuid;
519 struct ncacn_packet pkt;
520 struct data_blob_list_item *rep;
521 NTSTATUS status;
522 uint32_t result=0, reason=0;
523 uint32_t context_id;
524 const struct dcesrv_interface *iface;
525 uint32_t extra_flags = 0;
528 if provided, check the assoc_group is valid
530 if (call->pkt.u.bind.assoc_group_id != 0 &&
531 lpcfg_parm_bool(call->conn->dce_ctx->lp_ctx, NULL, "dcesrv","assoc group checking", true) &&
532 dcesrv_assoc_group_find(call->conn->dce_ctx, call->pkt.u.bind.assoc_group_id) == NULL) {
533 return dcesrv_bind_nak(call, 0);
536 if (call->pkt.u.bind.num_contexts < 1 ||
537 call->pkt.u.bind.ctx_list[0].num_transfer_syntaxes < 1) {
538 return dcesrv_bind_nak(call, 0);
541 context_id = call->pkt.u.bind.ctx_list[0].context_id;
543 /* you can't bind twice on one context */
544 if (dcesrv_find_context(call->conn, context_id) != NULL) {
545 return dcesrv_bind_nak(call, 0);
548 if_version = call->pkt.u.bind.ctx_list[0].abstract_syntax.if_version;
549 uuid = call->pkt.u.bind.ctx_list[0].abstract_syntax.uuid;
551 transfer_syntax_version = call->pkt.u.bind.ctx_list[0].transfer_syntaxes[0].if_version;
552 transfer_syntax_uuid = &call->pkt.u.bind.ctx_list[0].transfer_syntaxes[0].uuid;
553 if (!GUID_equal(&ndr_transfer_syntax_ndr.uuid, transfer_syntax_uuid) != 0 ||
554 ndr_transfer_syntax_ndr.if_version != transfer_syntax_version) {
555 char *uuid_str = GUID_string(call, transfer_syntax_uuid);
556 /* we only do NDR encoded dcerpc */
557 DEBUG(0,("Non NDR transfer syntax requested - %s\n", uuid_str));
558 talloc_free(uuid_str);
559 return dcesrv_bind_nak(call, 0);
562 iface = find_interface_by_uuid(call->conn->endpoint, &uuid, if_version);
563 if (iface == NULL) {
564 char *uuid_str = GUID_string(call, &uuid);
565 DEBUG(2,("Request for unknown dcerpc interface %s/%d\n", uuid_str, if_version));
566 talloc_free(uuid_str);
568 /* we don't know about that interface */
569 result = DCERPC_BIND_PROVIDER_REJECT;
570 reason = DCERPC_BIND_REASON_ASYNTAX;
573 if (iface) {
574 /* add this context to the list of available context_ids */
575 struct dcesrv_connection_context *context = talloc(call->conn,
576 struct dcesrv_connection_context);
577 if (context == NULL) {
578 return dcesrv_bind_nak(call, 0);
580 context->conn = call->conn;
581 context->iface = iface;
582 context->context_id = context_id;
583 if (call->pkt.u.bind.assoc_group_id != 0) {
584 context->assoc_group = dcesrv_assoc_group_reference(context,
585 call->conn->dce_ctx,
586 call->pkt.u.bind.assoc_group_id);
587 } else {
588 context->assoc_group = dcesrv_assoc_group_new(context, call->conn->dce_ctx);
590 if (context->assoc_group == NULL) {
591 talloc_free(context);
592 return dcesrv_bind_nak(call, 0);
594 context->private_data = NULL;
595 DLIST_ADD(call->conn->contexts, context);
596 call->context = context;
597 talloc_set_destructor(context, dcesrv_connection_context_destructor);
599 status = iface->bind(call, iface, if_version);
600 if (!NT_STATUS_IS_OK(status)) {
601 char *uuid_str = GUID_string(call, &uuid);
602 DEBUG(2,("Request for dcerpc interface %s/%d rejected: %s\n",
603 uuid_str, if_version, nt_errstr(status)));
604 talloc_free(uuid_str);
605 /* we don't want to trigger the iface->unbind() hook */
606 context->iface = NULL;
607 talloc_free(call->context);
608 call->context = NULL;
609 return dcesrv_bind_nak(call, 0);
613 if (call->conn->cli_max_recv_frag == 0) {
614 call->conn->cli_max_recv_frag = MIN(0x2000, call->pkt.u.bind.max_recv_frag);
617 /* handle any authentication that is being requested */
618 if (!dcesrv_auth_bind(call)) {
619 talloc_free(call->context);
620 call->context = NULL;
621 return dcesrv_bind_nak(call, DCERPC_BIND_REASON_INVALID_AUTH_TYPE);
624 /* setup a bind_ack */
625 dcesrv_init_hdr(&pkt, lpcfg_rpc_big_endian(call->conn->dce_ctx->lp_ctx));
626 pkt.auth_length = 0;
627 pkt.call_id = call->pkt.call_id;
628 pkt.ptype = DCERPC_PKT_BIND_ACK;
629 pkt.pfc_flags = DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST | extra_flags;
630 pkt.u.bind_ack.max_xmit_frag = call->conn->cli_max_recv_frag;
631 pkt.u.bind_ack.max_recv_frag = 0x2000;
634 make it possible for iface->bind() to specify the assoc_group_id
635 This helps the openchange mapiproxy plugin to work correctly.
637 metze
639 if (call->context) {
640 pkt.u.bind_ack.assoc_group_id = call->context->assoc_group->id;
641 } else {
642 pkt.u.bind_ack.assoc_group_id = DUMMY_ASSOC_GROUP;
645 if (iface) {
646 /* FIXME: Use pipe name as specified by endpoint instead of interface name */
647 pkt.u.bind_ack.secondary_address = talloc_asprintf(call, "\\PIPE\\%s", iface->name);
648 } else {
649 pkt.u.bind_ack.secondary_address = "";
651 pkt.u.bind_ack.num_results = 1;
652 pkt.u.bind_ack.ctx_list = talloc(call, struct dcerpc_ack_ctx);
653 if (!pkt.u.bind_ack.ctx_list) {
654 talloc_free(call->context);
655 call->context = NULL;
656 return NT_STATUS_NO_MEMORY;
658 pkt.u.bind_ack.ctx_list[0].result = result;
659 pkt.u.bind_ack.ctx_list[0].reason.value = reason;
660 pkt.u.bind_ack.ctx_list[0].syntax = ndr_transfer_syntax_ndr;
661 pkt.u.bind_ack.auth_info = data_blob(NULL, 0);
663 status = dcesrv_auth_bind_ack(call, &pkt);
664 if (!NT_STATUS_IS_OK(status)) {
665 talloc_free(call->context);
666 call->context = NULL;
667 return dcesrv_bind_nak(call, 0);
670 rep = talloc(call, struct data_blob_list_item);
671 if (!rep) {
672 talloc_free(call->context);
673 call->context = NULL;
674 return NT_STATUS_NO_MEMORY;
677 status = ncacn_push_auth(&rep->blob, call, &pkt,
678 call->conn->auth_state.auth_info);
679 if (!NT_STATUS_IS_OK(status)) {
680 talloc_free(call->context);
681 call->context = NULL;
682 return status;
685 dcerpc_set_frag_length(&rep->blob, rep->blob.length);
687 DLIST_ADD_END(call->replies, rep, struct data_blob_list_item *);
688 dcesrv_call_set_list(call, DCESRV_LIST_CALL_LIST);
690 if (call->conn->call_list && call->conn->call_list->replies) {
691 if (call->conn->transport.report_output_data) {
692 call->conn->transport.report_output_data(call->conn);
696 return NT_STATUS_OK;
701 handle a auth3 request
703 static NTSTATUS dcesrv_auth3(struct dcesrv_call_state *call)
705 /* handle the auth3 in the auth code */
706 if (!dcesrv_auth_auth3(call)) {
707 return dcesrv_fault(call, DCERPC_FAULT_OTHER);
710 talloc_free(call);
712 /* we don't send a reply to a auth3 request, except by a
713 fault */
714 return NT_STATUS_OK;
719 handle a bind request
721 static NTSTATUS dcesrv_alter_new_context(struct dcesrv_call_state *call, uint32_t context_id)
723 uint32_t if_version, transfer_syntax_version;
724 struct dcesrv_connection_context *context;
725 const struct dcesrv_interface *iface;
726 struct GUID uuid, *transfer_syntax_uuid;
727 NTSTATUS status;
729 if_version = call->pkt.u.alter.ctx_list[0].abstract_syntax.if_version;
730 uuid = call->pkt.u.alter.ctx_list[0].abstract_syntax.uuid;
732 transfer_syntax_version = call->pkt.u.alter.ctx_list[0].transfer_syntaxes[0].if_version;
733 transfer_syntax_uuid = &call->pkt.u.alter.ctx_list[0].transfer_syntaxes[0].uuid;
734 if (!GUID_equal(transfer_syntax_uuid, &ndr_transfer_syntax_ndr.uuid) ||
735 ndr_transfer_syntax_ndr.if_version != transfer_syntax_version) {
736 /* we only do NDR encoded dcerpc */
737 return NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED;
740 iface = find_interface_by_uuid(call->conn->endpoint, &uuid, if_version);
741 if (iface == NULL) {
742 char *uuid_str = GUID_string(call, &uuid);
743 DEBUG(2,("Request for unknown dcerpc interface %s/%d\n", uuid_str, if_version));
744 talloc_free(uuid_str);
745 return NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED;
748 /* add this context to the list of available context_ids */
749 context = talloc(call->conn, struct dcesrv_connection_context);
750 if (context == NULL) {
751 return NT_STATUS_NO_MEMORY;
753 context->conn = call->conn;
754 context->iface = iface;
755 context->context_id = context_id;
756 if (call->pkt.u.alter.assoc_group_id != 0) {
757 context->assoc_group = dcesrv_assoc_group_reference(context,
758 call->conn->dce_ctx,
759 call->pkt.u.alter.assoc_group_id);
760 } else {
761 context->assoc_group = dcesrv_assoc_group_new(context, call->conn->dce_ctx);
763 if (context->assoc_group == NULL) {
764 talloc_free(context);
765 call->context = NULL;
766 return NT_STATUS_NO_MEMORY;
768 context->private_data = NULL;
769 DLIST_ADD(call->conn->contexts, context);
770 call->context = context;
771 talloc_set_destructor(context, dcesrv_connection_context_destructor);
773 status = iface->bind(call, iface, if_version);
774 if (!NT_STATUS_IS_OK(status)) {
775 /* we don't want to trigger the iface->unbind() hook */
776 context->iface = NULL;
777 talloc_free(context);
778 call->context = NULL;
779 return status;
782 return NT_STATUS_OK;
787 handle a alter context request
789 static NTSTATUS dcesrv_alter(struct dcesrv_call_state *call)
791 struct ncacn_packet pkt;
792 struct data_blob_list_item *rep;
793 NTSTATUS status;
794 uint32_t result=0, reason=0;
795 uint32_t context_id;
797 /* handle any authentication that is being requested */
798 if (!dcesrv_auth_alter(call)) {
799 /* TODO: work out the right reject code */
800 result = DCERPC_BIND_PROVIDER_REJECT;
801 reason = DCERPC_BIND_REASON_ASYNTAX;
804 context_id = call->pkt.u.alter.ctx_list[0].context_id;
806 /* see if they are asking for a new interface */
807 if (result == 0) {
808 call->context = dcesrv_find_context(call->conn, context_id);
809 if (!call->context) {
810 status = dcesrv_alter_new_context(call, context_id);
811 if (!NT_STATUS_IS_OK(status)) {
812 result = DCERPC_BIND_PROVIDER_REJECT;
813 reason = DCERPC_BIND_REASON_ASYNTAX;
818 if (result == 0 &&
819 call->pkt.u.alter.assoc_group_id != 0 &&
820 lpcfg_parm_bool(call->conn->dce_ctx->lp_ctx, NULL, "dcesrv","assoc group checking", true) &&
821 call->pkt.u.alter.assoc_group_id != call->context->assoc_group->id) {
822 DEBUG(0,(__location__ ": Failed attempt to use new assoc_group in alter context (0x%08x 0x%08x)\n",
823 call->context->assoc_group->id, call->pkt.u.alter.assoc_group_id));
824 /* TODO: can they ask for a new association group? */
825 result = DCERPC_BIND_PROVIDER_REJECT;
826 reason = DCERPC_BIND_REASON_ASYNTAX;
829 /* setup a alter_resp */
830 dcesrv_init_hdr(&pkt, lpcfg_rpc_big_endian(call->conn->dce_ctx->lp_ctx));
831 pkt.auth_length = 0;
832 pkt.call_id = call->pkt.call_id;
833 pkt.ptype = DCERPC_PKT_ALTER_RESP;
834 pkt.pfc_flags = DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST;
835 pkt.u.alter_resp.max_xmit_frag = 0x2000;
836 pkt.u.alter_resp.max_recv_frag = 0x2000;
837 if (result == 0) {
838 pkt.u.alter_resp.assoc_group_id = call->context->assoc_group->id;
839 } else {
840 pkt.u.alter_resp.assoc_group_id = 0;
842 pkt.u.alter_resp.num_results = 1;
843 pkt.u.alter_resp.ctx_list = talloc_array(call, struct dcerpc_ack_ctx, 1);
844 if (!pkt.u.alter_resp.ctx_list) {
845 return NT_STATUS_NO_MEMORY;
847 pkt.u.alter_resp.ctx_list[0].result = result;
848 pkt.u.alter_resp.ctx_list[0].reason.value = reason;
849 pkt.u.alter_resp.ctx_list[0].syntax = ndr_transfer_syntax_ndr;
850 pkt.u.alter_resp.auth_info = data_blob(NULL, 0);
851 pkt.u.alter_resp.secondary_address = "";
853 status = dcesrv_auth_alter_ack(call, &pkt);
854 if (!NT_STATUS_IS_OK(status)) {
855 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)
856 || NT_STATUS_EQUAL(status, NT_STATUS_LOGON_FAILURE)
857 || NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
858 || NT_STATUS_EQUAL(status, NT_STATUS_WRONG_PASSWORD)) {
859 return dcesrv_fault(call, DCERPC_FAULT_ACCESS_DENIED);
861 return dcesrv_fault(call, 0);
864 rep = talloc(call, struct data_blob_list_item);
865 if (!rep) {
866 return NT_STATUS_NO_MEMORY;
869 status = ncacn_push_auth(&rep->blob, call, &pkt, call->conn->auth_state.auth_info);
870 if (!NT_STATUS_IS_OK(status)) {
871 return status;
874 dcerpc_set_frag_length(&rep->blob, rep->blob.length);
876 DLIST_ADD_END(call->replies, rep, struct data_blob_list_item *);
877 dcesrv_call_set_list(call, DCESRV_LIST_CALL_LIST);
879 if (call->conn->call_list && call->conn->call_list->replies) {
880 if (call->conn->transport.report_output_data) {
881 call->conn->transport.report_output_data(call->conn);
885 return NT_STATUS_OK;
889 possibly save the call for inspection with ndrdump
891 static void dcesrv_save_call(struct dcesrv_call_state *call, const char *why)
893 #ifdef DEVELOPER
894 char *fname;
895 const char *dump_dir;
896 dump_dir = lpcfg_parm_string(call->conn->dce_ctx->lp_ctx, NULL, "dcesrv", "stubs directory");
897 if (!dump_dir) {
898 return;
900 fname = talloc_asprintf(call, "%s/RPC-%s-%u-%s.dat",
901 dump_dir,
902 call->context->iface->name,
903 call->pkt.u.request.opnum,
904 why);
905 if (file_save(fname, call->pkt.u.request.stub_and_verifier.data, call->pkt.u.request.stub_and_verifier.length)) {
906 DEBUG(0,("RPC SAVED %s\n", fname));
908 talloc_free(fname);
909 #endif
912 static NTSTATUS dcesrv_check_verification_trailer(struct dcesrv_call_state *call)
914 TALLOC_CTX *frame = talloc_stackframe();
915 const uint32_t bitmask1 = call->conn->auth_state.client_hdr_signing ?
916 DCERPC_SEC_VT_CLIENT_SUPPORTS_HEADER_SIGNING : 0;
917 const struct dcerpc_sec_vt_pcontext pcontext = {
918 .abstract_syntax = call->context->iface->syntax_id,
919 .transfer_syntax = ndr_transfer_syntax_ndr,
921 const struct dcerpc_sec_vt_header2 header2 =
922 dcerpc_sec_vt_header2_from_ncacn_packet(&call->pkt);
923 enum ndr_err_code ndr_err;
924 struct dcerpc_sec_verification_trailer *vt = NULL;
925 NTSTATUS status = NT_STATUS_OK;
926 bool ok;
928 SMB_ASSERT(call->pkt.ptype == DCERPC_PKT_REQUEST);
930 ndr_err = ndr_pop_dcerpc_sec_verification_trailer(call->ndr_pull,
931 frame, &vt);
932 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
933 status = ndr_map_error2ntstatus(ndr_err);
934 goto done;
937 ok = dcerpc_sec_verification_trailer_check(vt, &bitmask1,
938 &pcontext, &header2);
939 if (!ok) {
940 status = NT_STATUS_ACCESS_DENIED;
941 goto done;
943 done:
944 TALLOC_FREE(frame);
945 return status;
949 handle a dcerpc request packet
951 static NTSTATUS dcesrv_request(struct dcesrv_call_state *call)
953 struct ndr_pull *pull;
954 NTSTATUS status;
955 struct dcesrv_connection_context *context;
957 /* if authenticated, and the mech we use can't do async replies, don't use them... */
958 if (call->conn->auth_state.gensec_security &&
959 !gensec_have_feature(call->conn->auth_state.gensec_security, GENSEC_FEATURE_ASYNC_REPLIES)) {
960 call->state_flags &= ~DCESRV_CALL_STATE_FLAG_MAY_ASYNC;
963 context = dcesrv_find_context(call->conn, call->pkt.u.request.context_id);
964 if (context == NULL) {
965 return dcesrv_fault(call, DCERPC_FAULT_UNK_IF);
968 pull = ndr_pull_init_blob(&call->pkt.u.request.stub_and_verifier, call);
969 NT_STATUS_HAVE_NO_MEMORY(pull);
971 pull->flags |= LIBNDR_FLAG_REF_ALLOC;
973 call->context = context;
974 call->ndr_pull = pull;
976 if (!(call->pkt.drep[0] & DCERPC_DREP_LE)) {
977 pull->flags |= LIBNDR_FLAG_BIGENDIAN;
980 status = dcesrv_check_verification_trailer(call);
981 if (!NT_STATUS_IS_OK(status)) {
982 uint32_t faultcode = DCERPC_FAULT_OTHER;
983 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
984 faultcode = DCERPC_FAULT_ACCESS_DENIED;
986 DEBUG(10, ("dcesrv_check_verification_trailer failed: %s\n",
987 nt_errstr(status)));
988 return dcesrv_fault(call, faultcode);
991 /* unravel the NDR for the packet */
992 status = context->iface->ndr_pull(call, call, pull, &call->r);
993 if (!NT_STATUS_IS_OK(status)) {
994 if (call->fault_code == DCERPC_FAULT_OP_RNG_ERROR) {
995 /* we got an unknown call */
996 DEBUG(3,(__location__ ": Unknown RPC call %u on %s\n",
997 call->pkt.u.request.opnum, context->iface->name));
998 dcesrv_save_call(call, "unknown");
999 } else {
1000 dcesrv_save_call(call, "pullfail");
1002 return dcesrv_fault(call, call->fault_code);
1005 if (pull->offset != pull->data_size) {
1006 dcesrv_save_call(call, "extrabytes");
1007 DEBUG(3,("Warning: %d extra bytes in incoming RPC request\n",
1008 pull->data_size - pull->offset));
1011 /* call the dispatch function */
1012 status = context->iface->dispatch(call, call, call->r);
1013 if (!NT_STATUS_IS_OK(status)) {
1014 DEBUG(5,("dcerpc fault in call %s:%02x - %s\n",
1015 context->iface->name,
1016 call->pkt.u.request.opnum,
1017 dcerpc_errstr(pull, call->fault_code)));
1018 return dcesrv_fault(call, call->fault_code);
1021 /* add the call to the pending list */
1022 dcesrv_call_set_list(call, DCESRV_LIST_PENDING_CALL_LIST);
1024 if (call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {
1025 return NT_STATUS_OK;
1028 return dcesrv_reply(call);
1033 remove the call from the right list when freed
1035 static int dcesrv_call_dequeue(struct dcesrv_call_state *call)
1037 dcesrv_call_set_list(call, DCESRV_LIST_NONE);
1038 return 0;
1041 _PUBLIC_ const struct tsocket_address *dcesrv_connection_get_local_address(struct dcesrv_connection *conn)
1043 return conn->local_address;
1046 _PUBLIC_ const struct tsocket_address *dcesrv_connection_get_remote_address(struct dcesrv_connection *conn)
1048 return conn->remote_address;
1052 process some input to a dcerpc endpoint server.
1054 NTSTATUS dcesrv_process_ncacn_packet(struct dcesrv_connection *dce_conn,
1055 struct ncacn_packet *pkt,
1056 DATA_BLOB blob)
1058 NTSTATUS status;
1059 struct dcesrv_call_state *call;
1061 call = talloc_zero(dce_conn, struct dcesrv_call_state);
1062 if (!call) {
1063 data_blob_free(&blob);
1064 talloc_free(pkt);
1065 return NT_STATUS_NO_MEMORY;
1067 call->conn = dce_conn;
1068 call->event_ctx = dce_conn->event_ctx;
1069 call->msg_ctx = dce_conn->msg_ctx;
1070 call->state_flags = call->conn->state_flags;
1071 call->time = timeval_current();
1072 call->list = DCESRV_LIST_NONE;
1074 talloc_steal(call, pkt);
1075 talloc_steal(call, blob.data);
1076 call->pkt = *pkt;
1078 talloc_set_destructor(call, dcesrv_call_dequeue);
1080 /* we have to check the signing here, before combining the
1081 pdus */
1082 if (call->pkt.ptype == DCERPC_PKT_REQUEST &&
1083 !dcesrv_auth_request(call, &blob)) {
1084 return dcesrv_fault(call, DCERPC_FAULT_ACCESS_DENIED);
1087 /* see if this is a continued packet */
1088 if (call->pkt.ptype == DCERPC_PKT_REQUEST &&
1089 !(call->pkt.pfc_flags & DCERPC_PFC_FLAG_FIRST)) {
1090 struct dcesrv_call_state *call2 = call;
1091 uint32_t alloc_size;
1093 /* we only allow fragmented requests, no other packet types */
1094 if (call->pkt.ptype != DCERPC_PKT_REQUEST) {
1095 return dcesrv_fault(call2, DCERPC_FAULT_OTHER);
1098 /* this is a continuation of an existing call - find the call
1099 then tack it on the end */
1100 call = dcesrv_find_fragmented_call(dce_conn, call2->pkt.call_id);
1101 if (!call) {
1102 return dcesrv_fault(call2, DCERPC_FAULT_OTHER);
1105 if (call->pkt.ptype != call2->pkt.ptype) {
1106 /* trying to play silly buggers are we? */
1107 return dcesrv_fault(call2, DCERPC_NCA_S_PROTO_ERROR);
1109 if (memcmp(call->pkt.drep, call2->pkt.drep, sizeof(pkt->drep)) != 0) {
1110 return dcesrv_fault(call2, DCERPC_NCA_S_PROTO_ERROR);
1112 if (call->pkt.call_id != call2->pkt.call_id) {
1113 return dcesrv_fault(call2, DCERPC_NCA_S_PROTO_ERROR);
1115 if (call->pkt.u.request.context_id != call2->pkt.u.request.context_id) {
1116 return dcesrv_fault(call2, DCERPC_NCA_S_PROTO_ERROR);
1118 if (call->pkt.u.request.opnum != call2->pkt.u.request.opnum) {
1119 return dcesrv_fault(call2, DCERPC_NCA_S_PROTO_ERROR);
1122 alloc_size = call->pkt.u.request.stub_and_verifier.length +
1123 call2->pkt.u.request.stub_and_verifier.length;
1124 if (call->pkt.u.request.alloc_hint > alloc_size) {
1125 alloc_size = call->pkt.u.request.alloc_hint;
1128 call->pkt.u.request.stub_and_verifier.data =
1129 talloc_realloc(call,
1130 call->pkt.u.request.stub_and_verifier.data,
1131 uint8_t, alloc_size);
1132 if (!call->pkt.u.request.stub_and_verifier.data) {
1133 return dcesrv_fault(call2, DCERPC_FAULT_OTHER);
1135 memcpy(call->pkt.u.request.stub_and_verifier.data +
1136 call->pkt.u.request.stub_and_verifier.length,
1137 call2->pkt.u.request.stub_and_verifier.data,
1138 call2->pkt.u.request.stub_and_verifier.length);
1139 call->pkt.u.request.stub_and_verifier.length +=
1140 call2->pkt.u.request.stub_and_verifier.length;
1142 call->pkt.pfc_flags |= (call2->pkt.pfc_flags & DCERPC_PFC_FLAG_LAST);
1144 talloc_free(call2);
1147 /* this may not be the last pdu in the chain - if its isn't then
1148 just put it on the incoming_fragmented_call_list and wait for the rest */
1149 if (call->pkt.ptype == DCERPC_PKT_REQUEST &&
1150 !(call->pkt.pfc_flags & DCERPC_PFC_FLAG_LAST)) {
1151 dcesrv_call_set_list(call, DCESRV_LIST_FRAGMENTED_CALL_LIST);
1152 return NT_STATUS_OK;
1155 /* This removes any fragments we may have had stashed away */
1156 dcesrv_call_set_list(call, DCESRV_LIST_NONE);
1158 switch (call->pkt.ptype) {
1159 case DCERPC_PKT_BIND:
1160 status = dcesrv_bind(call);
1161 break;
1162 case DCERPC_PKT_AUTH3:
1163 status = dcesrv_auth3(call);
1164 break;
1165 case DCERPC_PKT_ALTER:
1166 status = dcesrv_alter(call);
1167 break;
1168 case DCERPC_PKT_REQUEST:
1169 status = dcesrv_request(call);
1170 break;
1171 default:
1172 status = NT_STATUS_INVALID_PARAMETER;
1173 break;
1176 /* if we are going to be sending a reply then add
1177 it to the list of pending calls. We add it to the end to keep the call
1178 list in the order we will answer */
1179 if (!NT_STATUS_IS_OK(status)) {
1180 talloc_free(call);
1183 return status;
1186 _PUBLIC_ NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx,
1187 struct loadparm_context *lp_ctx,
1188 const char **endpoint_servers, struct dcesrv_context **_dce_ctx)
1190 NTSTATUS status;
1191 struct dcesrv_context *dce_ctx;
1192 int i;
1194 if (!endpoint_servers) {
1195 DEBUG(0,("dcesrv_init_context: no endpoint servers configured\n"));
1196 return NT_STATUS_INTERNAL_ERROR;
1199 dce_ctx = talloc(mem_ctx, struct dcesrv_context);
1200 NT_STATUS_HAVE_NO_MEMORY(dce_ctx);
1201 dce_ctx->endpoint_list = NULL;
1202 dce_ctx->lp_ctx = lp_ctx;
1203 dce_ctx->assoc_groups_idr = idr_init(dce_ctx);
1204 NT_STATUS_HAVE_NO_MEMORY(dce_ctx->assoc_groups_idr);
1205 dce_ctx->broken_connections = NULL;
1207 for (i=0;endpoint_servers[i];i++) {
1208 const struct dcesrv_endpoint_server *ep_server;
1210 ep_server = dcesrv_ep_server_byname(endpoint_servers[i]);
1211 if (!ep_server) {
1212 DEBUG(0,("dcesrv_init_context: failed to find endpoint server = '%s'\n", endpoint_servers[i]));
1213 return NT_STATUS_INTERNAL_ERROR;
1216 status = ep_server->init_server(dce_ctx, ep_server);
1217 if (!NT_STATUS_IS_OK(status)) {
1218 DEBUG(0,("dcesrv_init_context: failed to init endpoint server = '%s': %s\n", endpoint_servers[i],
1219 nt_errstr(status)));
1220 return status;
1224 *_dce_ctx = dce_ctx;
1225 return NT_STATUS_OK;
1228 /* the list of currently registered DCERPC endpoint servers.
1230 static struct ep_server {
1231 struct dcesrv_endpoint_server *ep_server;
1232 } *ep_servers = NULL;
1233 static int num_ep_servers;
1236 register a DCERPC endpoint server.
1238 The 'name' can be later used by other backends to find the operations
1239 structure for this backend.
1241 The 'type' is used to specify whether this is for a disk, printer or IPC$ share
1243 _PUBLIC_ NTSTATUS dcerpc_register_ep_server(const void *_ep_server)
1245 const struct dcesrv_endpoint_server *ep_server = _ep_server;
1247 if (dcesrv_ep_server_byname(ep_server->name) != NULL) {
1248 /* its already registered! */
1249 DEBUG(0,("DCERPC endpoint server '%s' already registered\n",
1250 ep_server->name));
1251 return NT_STATUS_OBJECT_NAME_COLLISION;
1254 ep_servers = realloc_p(ep_servers, struct ep_server, num_ep_servers+1);
1255 if (!ep_servers) {
1256 smb_panic("out of memory in dcerpc_register");
1259 ep_servers[num_ep_servers].ep_server = smb_xmemdup(ep_server, sizeof(*ep_server));
1260 ep_servers[num_ep_servers].ep_server->name = smb_xstrdup(ep_server->name);
1262 num_ep_servers++;
1264 DEBUG(3,("DCERPC endpoint server '%s' registered\n",
1265 ep_server->name));
1267 return NT_STATUS_OK;
1271 return the operations structure for a named backend of the specified type
1273 const struct dcesrv_endpoint_server *dcesrv_ep_server_byname(const char *name)
1275 int i;
1277 for (i=0;i<num_ep_servers;i++) {
1278 if (strcmp(ep_servers[i].ep_server->name, name) == 0) {
1279 return ep_servers[i].ep_server;
1283 return NULL;
1286 void dcerpc_server_init(struct loadparm_context *lp_ctx)
1288 static bool initialized;
1289 #define _MODULE_PROTO(init) extern NTSTATUS init(void);
1290 STATIC_dcerpc_server_MODULES_PROTO;
1291 init_module_fn static_init[] = { STATIC_dcerpc_server_MODULES };
1292 init_module_fn *shared_init;
1294 if (initialized) {
1295 return;
1297 initialized = true;
1299 shared_init = load_samba_modules(NULL, "dcerpc_server");
1301 run_init_functions(static_init);
1302 run_init_functions(shared_init);
1304 talloc_free(shared_init);
1308 return the DCERPC module version, and the size of some critical types
1309 This can be used by endpoint server modules to either detect compilation errors, or provide
1310 multiple implementations for different smbd compilation options in one module
1312 const struct dcesrv_critical_sizes *dcerpc_module_version(void)
1314 static const struct dcesrv_critical_sizes critical_sizes = {
1315 DCERPC_MODULE_VERSION,
1316 sizeof(struct dcesrv_context),
1317 sizeof(struct dcesrv_endpoint),
1318 sizeof(struct dcesrv_endpoint_server),
1319 sizeof(struct dcesrv_interface),
1320 sizeof(struct dcesrv_if_list),
1321 sizeof(struct dcesrv_connection),
1322 sizeof(struct dcesrv_call_state),
1323 sizeof(struct dcesrv_auth),
1324 sizeof(struct dcesrv_handle)
1327 return &critical_sizes;
1330 static void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char *reason)
1332 struct dcesrv_context *dce_ctx = dce_conn->dce_ctx;
1333 struct stream_connection *srv_conn;
1334 srv_conn = talloc_get_type(dce_conn->transport.private_data,
1335 struct stream_connection);
1337 if (dce_conn->pending_call_list == NULL) {
1338 char *full_reason = talloc_asprintf(dce_conn, "dcesrv: %s", reason);
1340 DLIST_REMOVE(dce_ctx->broken_connections, dce_conn);
1341 stream_terminate_connection(srv_conn, full_reason ? full_reason : reason);
1342 return;
1345 if (dce_conn->terminate != NULL) {
1346 return;
1349 DEBUG(3,("dcesrv: terminating connection due to '%s' defered due to pending calls\n",
1350 reason));
1351 dce_conn->terminate = talloc_strdup(dce_conn, reason);
1352 if (dce_conn->terminate == NULL) {
1353 dce_conn->terminate = "dcesrv: defered terminating connection - no memory";
1355 DLIST_ADD_END(dce_ctx->broken_connections, dce_conn, NULL);
1358 static void dcesrv_cleanup_broken_connections(struct dcesrv_context *dce_ctx)
1360 struct dcesrv_connection *cur, *next;
1362 next = dce_ctx->broken_connections;
1363 while (next != NULL) {
1364 cur = next;
1365 next = cur->next;
1367 dcesrv_terminate_connection(cur, cur->terminate);
1371 /* We need this include to be able to compile on some plateforms
1372 * (ie. freebsd 7.2) as it seems that <sys/uio.h> is not included
1373 * correctly.
1374 * It has to be that deep because otherwise we have a conflict on
1375 * const struct dcesrv_interface declaration.
1376 * This is mostly due to socket_wrapper defining #define bind swrap_bind
1377 * which conflict with the bind used before.
1379 #include "system/network.h"
1381 struct dcesrv_sock_reply_state {
1382 struct dcesrv_connection *dce_conn;
1383 struct dcesrv_call_state *call;
1384 struct iovec iov;
1387 static void dcesrv_sock_reply_done(struct tevent_req *subreq);
1389 static void dcesrv_sock_report_output_data(struct dcesrv_connection *dce_conn)
1391 struct dcesrv_call_state *call;
1393 call = dce_conn->call_list;
1394 if (!call || !call->replies) {
1395 return;
1398 while (call->replies) {
1399 struct data_blob_list_item *rep = call->replies;
1400 struct dcesrv_sock_reply_state *substate;
1401 struct tevent_req *subreq;
1403 substate = talloc(call, struct dcesrv_sock_reply_state);
1404 if (!substate) {
1405 dcesrv_terminate_connection(dce_conn, "no memory");
1406 return;
1409 substate->dce_conn = dce_conn;
1410 substate->call = NULL;
1412 DLIST_REMOVE(call->replies, rep);
1414 if (call->replies == NULL) {
1415 substate->call = call;
1418 substate->iov.iov_base = (void *) rep->blob.data;
1419 substate->iov.iov_len = rep->blob.length;
1421 subreq = tstream_writev_queue_send(substate,
1422 dce_conn->event_ctx,
1423 dce_conn->stream,
1424 dce_conn->send_queue,
1425 &substate->iov, 1);
1426 if (!subreq) {
1427 dcesrv_terminate_connection(dce_conn, "no memory");
1428 return;
1430 tevent_req_set_callback(subreq, dcesrv_sock_reply_done,
1431 substate);
1434 DLIST_REMOVE(call->conn->call_list, call);
1435 call->list = DCESRV_LIST_NONE;
1438 static void dcesrv_sock_reply_done(struct tevent_req *subreq)
1440 struct dcesrv_sock_reply_state *substate = tevent_req_callback_data(subreq,
1441 struct dcesrv_sock_reply_state);
1442 int ret;
1443 int sys_errno;
1444 NTSTATUS status;
1445 struct dcesrv_call_state *call = substate->call;
1447 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1448 TALLOC_FREE(subreq);
1449 if (ret == -1) {
1450 status = map_nt_error_from_unix_common(sys_errno);
1451 dcesrv_terminate_connection(substate->dce_conn, nt_errstr(status));
1452 return;
1455 talloc_free(substate);
1456 if (call) {
1457 talloc_free(call);
1464 struct dcesrv_socket_context {
1465 const struct dcesrv_endpoint *endpoint;
1466 struct dcesrv_context *dcesrv_ctx;
1470 static void dcesrv_read_fragment_done(struct tevent_req *subreq);
1472 static void dcesrv_sock_accept(struct stream_connection *srv_conn)
1474 NTSTATUS status;
1475 struct dcesrv_socket_context *dcesrv_sock =
1476 talloc_get_type(srv_conn->private_data, struct dcesrv_socket_context);
1477 enum dcerpc_transport_t transport =
1478 dcerpc_binding_get_transport(dcesrv_sock->endpoint->ep_description);
1479 struct dcesrv_connection *dcesrv_conn = NULL;
1480 int ret;
1481 struct tevent_req *subreq;
1482 struct loadparm_context *lp_ctx = dcesrv_sock->dcesrv_ctx->lp_ctx;
1484 dcesrv_cleanup_broken_connections(dcesrv_sock->dcesrv_ctx);
1486 if (!srv_conn->session_info) {
1487 status = auth_anonymous_session_info(srv_conn,
1488 lp_ctx,
1489 &srv_conn->session_info);
1490 if (!NT_STATUS_IS_OK(status)) {
1491 DEBUG(0,("dcesrv_sock_accept: auth_anonymous_session_info failed: %s\n",
1492 nt_errstr(status)));
1493 stream_terminate_connection(srv_conn, nt_errstr(status));
1494 return;
1498 status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx,
1499 srv_conn,
1500 dcesrv_sock->endpoint,
1501 srv_conn->session_info,
1502 srv_conn->event.ctx,
1503 srv_conn->msg_ctx,
1504 srv_conn->server_id,
1505 DCESRV_CALL_STATE_FLAG_MAY_ASYNC,
1506 &dcesrv_conn);
1507 if (!NT_STATUS_IS_OK(status)) {
1508 DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n",
1509 nt_errstr(status)));
1510 stream_terminate_connection(srv_conn, nt_errstr(status));
1511 return;
1514 dcesrv_conn->transport.private_data = srv_conn;
1515 dcesrv_conn->transport.report_output_data = dcesrv_sock_report_output_data;
1517 TALLOC_FREE(srv_conn->event.fde);
1519 dcesrv_conn->send_queue = tevent_queue_create(dcesrv_conn, "dcesrv send queue");
1520 if (!dcesrv_conn->send_queue) {
1521 status = NT_STATUS_NO_MEMORY;
1522 DEBUG(0,("dcesrv_sock_accept: tevent_queue_create(%s)\n",
1523 nt_errstr(status)));
1524 stream_terminate_connection(srv_conn, nt_errstr(status));
1525 return;
1528 if (transport == NCACN_NP) {
1529 dcesrv_conn->auth_state.session_key = dcesrv_inherited_session_key;
1530 dcesrv_conn->stream = talloc_move(dcesrv_conn,
1531 &srv_conn->tstream);
1532 } else {
1533 ret = tstream_bsd_existing_socket(dcesrv_conn,
1534 socket_get_fd(srv_conn->socket),
1535 &dcesrv_conn->stream);
1536 if (ret == -1) {
1537 status = map_nt_error_from_unix_common(errno);
1538 DEBUG(0, ("dcesrv_sock_accept: "
1539 "failed to setup tstream: %s\n",
1540 nt_errstr(status)));
1541 stream_terminate_connection(srv_conn, nt_errstr(status));
1542 return;
1544 socket_set_flags(srv_conn->socket, SOCKET_FLAG_NOCLOSE);
1547 dcesrv_conn->local_address = srv_conn->local_address;
1548 dcesrv_conn->remote_address = srv_conn->remote_address;
1550 srv_conn->private_data = dcesrv_conn;
1552 irpc_add_name(srv_conn->msg_ctx, "rpc_server");
1554 subreq = dcerpc_read_ncacn_packet_send(dcesrv_conn,
1555 dcesrv_conn->event_ctx,
1556 dcesrv_conn->stream);
1557 if (!subreq) {
1558 status = NT_STATUS_NO_MEMORY;
1559 DEBUG(0,("dcesrv_sock_accept: dcerpc_read_fragment_buffer_send(%s)\n",
1560 nt_errstr(status)));
1561 stream_terminate_connection(srv_conn, nt_errstr(status));
1562 return;
1564 tevent_req_set_callback(subreq, dcesrv_read_fragment_done, dcesrv_conn);
1566 return;
1569 static void dcesrv_read_fragment_done(struct tevent_req *subreq)
1571 struct dcesrv_connection *dce_conn = tevent_req_callback_data(subreq,
1572 struct dcesrv_connection);
1573 struct dcesrv_context *dce_ctx = dce_conn->dce_ctx;
1574 struct ncacn_packet *pkt;
1575 DATA_BLOB buffer;
1576 NTSTATUS status;
1578 if (dce_conn->terminate) {
1580 * if the current connection is broken
1581 * we need to clean it up before any other connection
1583 dcesrv_terminate_connection(dce_conn, dce_conn->terminate);
1584 dcesrv_cleanup_broken_connections(dce_ctx);
1585 return;
1588 dcesrv_cleanup_broken_connections(dce_ctx);
1590 status = dcerpc_read_ncacn_packet_recv(subreq, dce_conn,
1591 &pkt, &buffer);
1592 TALLOC_FREE(subreq);
1593 if (!NT_STATUS_IS_OK(status)) {
1594 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
1595 return;
1598 status = dcesrv_process_ncacn_packet(dce_conn, pkt, buffer);
1599 if (!NT_STATUS_IS_OK(status)) {
1600 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
1601 return;
1604 subreq = dcerpc_read_ncacn_packet_send(dce_conn,
1605 dce_conn->event_ctx,
1606 dce_conn->stream);
1607 if (!subreq) {
1608 status = NT_STATUS_NO_MEMORY;
1609 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
1610 return;
1612 tevent_req_set_callback(subreq, dcesrv_read_fragment_done, dce_conn);
1615 static void dcesrv_sock_recv(struct stream_connection *conn, uint16_t flags)
1617 struct dcesrv_connection *dce_conn = talloc_get_type(conn->private_data,
1618 struct dcesrv_connection);
1619 dcesrv_terminate_connection(dce_conn, "dcesrv_sock_recv triggered");
1622 static void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags)
1624 struct dcesrv_connection *dce_conn = talloc_get_type(conn->private_data,
1625 struct dcesrv_connection);
1626 dcesrv_terminate_connection(dce_conn, "dcesrv_sock_send triggered");
1630 static const struct stream_server_ops dcesrv_stream_ops = {
1631 .name = "rpc",
1632 .accept_connection = dcesrv_sock_accept,
1633 .recv_handler = dcesrv_sock_recv,
1634 .send_handler = dcesrv_sock_send,
1637 static NTSTATUS dcesrv_add_ep_unix(struct dcesrv_context *dce_ctx,
1638 struct loadparm_context *lp_ctx,
1639 struct dcesrv_endpoint *e,
1640 struct tevent_context *event_ctx, const struct model_ops *model_ops)
1642 struct dcesrv_socket_context *dcesrv_sock;
1643 uint16_t port = 1;
1644 NTSTATUS status;
1645 const char *endpoint;
1647 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
1648 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
1650 /* remember the endpoint of this socket */
1651 dcesrv_sock->endpoint = e;
1652 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
1654 endpoint = dcerpc_binding_get_string_option(e->ep_description, "endpoint");
1656 status = stream_setup_socket(dcesrv_sock, event_ctx, lp_ctx,
1657 model_ops, &dcesrv_stream_ops,
1658 "unix", endpoint, &port,
1659 lpcfg_socket_options(lp_ctx),
1660 dcesrv_sock);
1661 if (!NT_STATUS_IS_OK(status)) {
1662 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
1663 endpoint, nt_errstr(status)));
1666 return status;
1669 static NTSTATUS dcesrv_add_ep_ncalrpc(struct dcesrv_context *dce_ctx,
1670 struct loadparm_context *lp_ctx,
1671 struct dcesrv_endpoint *e,
1672 struct tevent_context *event_ctx, const struct model_ops *model_ops)
1674 struct dcesrv_socket_context *dcesrv_sock;
1675 uint16_t port = 1;
1676 char *full_path;
1677 NTSTATUS status;
1678 const char *endpoint;
1680 endpoint = dcerpc_binding_get_string_option(e->ep_description, "endpoint");
1682 if (endpoint == NULL) {
1684 * No identifier specified: use DEFAULT.
1686 * TODO: DO NOT hardcode this value anywhere else. Rather, specify
1687 * no endpoint and let the epmapper worry about it.
1689 endpoint = "DEFAULT";
1690 status = dcerpc_binding_set_string_option(e->ep_description,
1691 "endpoint",
1692 endpoint);
1693 if (!NT_STATUS_IS_OK(status)) {
1694 DEBUG(0,("dcerpc_binding_set_string_option() failed - %s\n",
1695 nt_errstr(status)));
1696 return status;
1700 full_path = talloc_asprintf(dce_ctx, "%s/%s", lpcfg_ncalrpc_dir(lp_ctx),
1701 endpoint);
1703 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
1704 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
1706 /* remember the endpoint of this socket */
1707 dcesrv_sock->endpoint = e;
1708 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
1710 status = stream_setup_socket(dcesrv_sock, event_ctx, lp_ctx,
1711 model_ops, &dcesrv_stream_ops,
1712 "unix", full_path, &port,
1713 lpcfg_socket_options(lp_ctx),
1714 dcesrv_sock);
1715 if (!NT_STATUS_IS_OK(status)) {
1716 DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n",
1717 endpoint, full_path, nt_errstr(status)));
1719 return status;
1722 static NTSTATUS dcesrv_add_ep_np(struct dcesrv_context *dce_ctx,
1723 struct loadparm_context *lp_ctx,
1724 struct dcesrv_endpoint *e,
1725 struct tevent_context *event_ctx, const struct model_ops *model_ops)
1727 struct dcesrv_socket_context *dcesrv_sock;
1728 NTSTATUS status;
1729 const char *endpoint;
1731 endpoint = dcerpc_binding_get_string_option(e->ep_description, "endpoint");
1732 if (endpoint == NULL) {
1733 DEBUG(0, ("Endpoint mandatory for named pipes\n"));
1734 return NT_STATUS_INVALID_PARAMETER;
1737 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
1738 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
1740 /* remember the endpoint of this socket */
1741 dcesrv_sock->endpoint = e;
1742 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
1744 status = tstream_setup_named_pipe(dce_ctx, event_ctx, lp_ctx,
1745 model_ops, &dcesrv_stream_ops,
1746 endpoint,
1747 dcesrv_sock);
1748 if (!NT_STATUS_IS_OK(status)) {
1749 DEBUG(0,("stream_setup_named_pipe(pipe=%s) failed - %s\n",
1750 endpoint, nt_errstr(status)));
1751 return status;
1754 return NT_STATUS_OK;
1758 add a socket address to the list of events, one event per dcerpc endpoint
1760 static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
1761 struct tevent_context *event_ctx, const struct model_ops *model_ops,
1762 const char *address)
1764 struct dcesrv_socket_context *dcesrv_sock;
1765 uint16_t port = 0;
1766 NTSTATUS status;
1767 const char *endpoint;
1768 char port_str[6];
1770 endpoint = dcerpc_binding_get_string_option(e->ep_description, "endpoint");
1771 if (endpoint != NULL) {
1772 port = atoi(endpoint);
1775 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
1776 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
1778 /* remember the endpoint of this socket */
1779 dcesrv_sock->endpoint = e;
1780 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
1782 status = stream_setup_socket(dcesrv_sock, event_ctx, dce_ctx->lp_ctx,
1783 model_ops, &dcesrv_stream_ops,
1784 "ip", address, &port,
1785 lpcfg_socket_options(dce_ctx->lp_ctx),
1786 dcesrv_sock);
1787 if (!NT_STATUS_IS_OK(status)) {
1788 DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n",
1789 address, port, nt_errstr(status)));
1790 return status;
1793 snprintf(port_str, sizeof(port_str), "%u", port);
1795 status = dcerpc_binding_set_string_option(e->ep_description,
1796 "endpoint", port_str);
1797 if (!NT_STATUS_IS_OK(status)) {
1798 DEBUG(0,("dcerpc_binding_set_string_option(endpoint, %s) failed - %s\n",
1799 port_str, nt_errstr(status)));
1800 return status;
1803 return NT_STATUS_OK;
1806 #include "lib/socket/netif.h" /* Included here to work around the fact that socket_wrapper redefines bind() */
1808 static NTSTATUS dcesrv_add_ep_tcp(struct dcesrv_context *dce_ctx,
1809 struct loadparm_context *lp_ctx,
1810 struct dcesrv_endpoint *e,
1811 struct tevent_context *event_ctx, const struct model_ops *model_ops)
1813 NTSTATUS status;
1815 /* Add TCP/IP sockets */
1816 if (lpcfg_interfaces(lp_ctx) && lpcfg_bind_interfaces_only(lp_ctx)) {
1817 int num_interfaces;
1818 int i;
1819 struct interface *ifaces;
1821 load_interface_list(dce_ctx, lp_ctx, &ifaces);
1823 num_interfaces = iface_list_count(ifaces);
1824 for(i = 0; i < num_interfaces; i++) {
1825 const char *address = iface_list_n_ip(ifaces, i);
1826 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address);
1827 NT_STATUS_NOT_OK_RETURN(status);
1829 } else {
1830 char **wcard;
1831 int i;
1832 int num_binds = 0;
1833 wcard = iface_list_wildcard(dce_ctx);
1834 NT_STATUS_HAVE_NO_MEMORY(wcard);
1835 for (i=0; wcard[i]; i++) {
1836 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, wcard[i]);
1837 if (NT_STATUS_IS_OK(status)) {
1838 num_binds++;
1841 talloc_free(wcard);
1842 if (num_binds == 0) {
1843 return NT_STATUS_INVALID_PARAMETER_MIX;
1847 return NT_STATUS_OK;
1850 NTSTATUS dcesrv_add_ep(struct dcesrv_context *dce_ctx,
1851 struct loadparm_context *lp_ctx,
1852 struct dcesrv_endpoint *e,
1853 struct tevent_context *event_ctx,
1854 const struct model_ops *model_ops)
1856 enum dcerpc_transport_t transport =
1857 dcerpc_binding_get_transport(e->ep_description);
1859 switch (transport) {
1860 case NCACN_UNIX_STREAM:
1861 return dcesrv_add_ep_unix(dce_ctx, lp_ctx, e, event_ctx, model_ops);
1863 case NCALRPC:
1864 return dcesrv_add_ep_ncalrpc(dce_ctx, lp_ctx, e, event_ctx, model_ops);
1866 case NCACN_IP_TCP:
1867 return dcesrv_add_ep_tcp(dce_ctx, lp_ctx, e, event_ctx, model_ops);
1869 case NCACN_NP:
1870 return dcesrv_add_ep_np(dce_ctx, lp_ctx, e, event_ctx, model_ops);
1872 default:
1873 return NT_STATUS_NOT_SUPPORTED;
1879 * retrieve credentials from a dce_call
1881 _PUBLIC_ struct cli_credentials *dcesrv_call_credentials(struct dcesrv_call_state *dce_call)
1883 return dce_call->conn->auth_state.session_info->credentials;
1887 * returns true if this is an authenticated call
1889 _PUBLIC_ bool dcesrv_call_authenticated(struct dcesrv_call_state *dce_call)
1891 enum security_user_level level;
1892 level = security_session_user_level(dce_call->conn->auth_state.session_info, NULL);
1893 return level >= SECURITY_USER;
1897 * retrieve account_name for a dce_call
1899 _PUBLIC_ const char *dcesrv_call_account_name(struct dcesrv_call_state *dce_call)
1901 return dce_call->context->conn->auth_state.session_info->info->account_name;