Merge branch 'master' of /home/tridge/samba/git/combined
[Samba/aatanasov.git] / source4 / rpc_server / service_rpc.c
blob3d5c364ec92d966659d1d7bd1588efbc7a26d692
1 /*
2 Unix SMB/CIFS implementation.
4 smbd-specific dcerpc server code
6 Copyright (C) Andrew Tridgell 2003-2005
7 Copyright (C) Stefan (metze) Metzmacher 2004-2005
8 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2004,2007
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "librpc/gen_ndr/ndr_dcerpc.h"
26 #include "auth/auth.h"
27 #include "auth/gensec/gensec.h"
28 #include "../lib/util/dlinklist.h"
29 #include "rpc_server/dcerpc_server.h"
30 #include "rpc_server/dcerpc_server_proto.h"
31 #include "rpc_server/service_rpc.h"
32 #include "lib/events/events.h"
33 #include "smbd/service_task.h"
34 #include "smbd/service_stream.h"
35 #include "smbd/service.h"
36 #include "system/filesys.h"
37 #include "libcli/security/security.h"
38 #include "lib/socket/socket.h"
39 #include "lib/messaging/irpc.h"
40 #include "system/network.h"
41 #include "lib/socket/netif.h"
42 #include "param/param.h"
43 #include "../lib/tsocket/tsocket.h"
44 #include "librpc/rpc/dcerpc_proto.h"
45 #include "../lib/util/tevent_ntstatus.h"
46 #include "libcli/raw/smb.h"
47 #include "../libcli/named_pipe_auth/npa_tstream.h"
49 struct dcesrv_socket_context {
50 const struct dcesrv_endpoint *endpoint;
51 struct dcesrv_context *dcesrv_ctx;
54 static void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char *reason)
56 struct stream_connection *srv_conn;
57 srv_conn = talloc_get_type(dce_conn->transport.private_data,
58 struct stream_connection);
60 stream_terminate_connection(srv_conn, reason);
63 static void dcesrv_sock_reply_done(struct tevent_req *subreq);
65 struct dcesrv_sock_reply_state {
66 struct dcesrv_connection *dce_conn;
67 struct dcesrv_call_state *call;
68 struct iovec iov;
71 static void dcesrv_sock_report_output_data(struct dcesrv_connection *dce_conn)
73 struct dcesrv_call_state *call;
75 call = dce_conn->call_list;
76 if (!call || !call->replies) {
77 return;
80 while (call->replies) {
81 struct data_blob_list_item *rep = call->replies;
82 struct dcesrv_sock_reply_state *substate;
83 struct tevent_req *subreq;
85 substate = talloc(call, struct dcesrv_sock_reply_state);
86 if (!substate) {
87 dcesrv_terminate_connection(dce_conn, "no memory");
88 return;
91 substate->dce_conn = dce_conn;
92 substate->call = NULL;
94 DLIST_REMOVE(call->replies, rep);
96 if (call->replies == NULL) {
97 substate->call = call;
100 substate->iov.iov_base = rep->blob.data;
101 substate->iov.iov_len = rep->blob.length;
103 subreq = tstream_writev_queue_send(substate,
104 dce_conn->event_ctx,
105 dce_conn->stream,
106 dce_conn->send_queue,
107 &substate->iov, 1);
108 if (!subreq) {
109 dcesrv_terminate_connection(dce_conn, "no memory");
110 return;
112 tevent_req_set_callback(subreq, dcesrv_sock_reply_done,
113 substate);
116 DLIST_REMOVE(call->conn->call_list, call);
117 call->list = DCESRV_LIST_NONE;
120 static void dcesrv_sock_reply_done(struct tevent_req *subreq)
122 struct dcesrv_sock_reply_state *substate = tevent_req_callback_data(subreq,
123 struct dcesrv_sock_reply_state);
124 int ret;
125 int sys_errno;
126 NTSTATUS status;
127 struct dcesrv_call_state *call = substate->call;
129 ret = tstream_writev_queue_recv(subreq, &sys_errno);
130 TALLOC_FREE(subreq);
131 if (ret == -1) {
132 status = map_nt_error_from_unix(sys_errno);
133 dcesrv_terminate_connection(substate->dce_conn, nt_errstr(status));
134 return;
137 talloc_free(substate);
138 if (call) {
139 talloc_free(call);
143 static struct socket_address *dcesrv_sock_get_my_addr(struct dcesrv_connection *dcesrv_conn, TALLOC_CTX *mem_ctx)
145 struct stream_connection *srv_conn;
146 srv_conn = talloc_get_type(dcesrv_conn->transport.private_data,
147 struct stream_connection);
149 return socket_get_my_addr(srv_conn->socket, mem_ctx);
152 static struct socket_address *dcesrv_sock_get_peer_addr(struct dcesrv_connection *dcesrv_conn, TALLOC_CTX *mem_ctx)
154 struct stream_connection *srv_conn;
155 srv_conn = talloc_get_type(dcesrv_conn->transport.private_data,
156 struct stream_connection);
158 return socket_get_peer_addr(srv_conn->socket, mem_ctx);
161 struct dcerpc_read_ncacn_packet_state {
162 struct {
163 struct smb_iconv_convenience *smb_iconv_c;
164 } caller;
165 DATA_BLOB buffer;
166 struct ncacn_packet *pkt;
169 static int dcerpc_read_ncacn_packet_next_vector(struct tstream_context *stream,
170 void *private_data,
171 TALLOC_CTX *mem_ctx,
172 struct iovec **_vector,
173 size_t *_count);
174 static void dcerpc_read_ncacn_packet_done(struct tevent_req *subreq);
176 static struct tevent_req *dcerpc_read_ncacn_packet_send(TALLOC_CTX *mem_ctx,
177 struct tevent_context *ev,
178 struct tstream_context *stream,
179 struct smb_iconv_convenience *ic)
181 struct tevent_req *req;
182 struct dcerpc_read_ncacn_packet_state *state;
183 struct tevent_req *subreq;
185 req = tevent_req_create(mem_ctx, &state,
186 struct dcerpc_read_ncacn_packet_state);
187 if (req == NULL) {
188 return NULL;
191 state->caller.smb_iconv_c = ic;
192 state->buffer = data_blob_const(NULL, 0);
193 state->pkt = talloc(state, struct ncacn_packet);
194 if (tevent_req_nomem(state->pkt, req)) {
195 goto post;
198 subreq = tstream_readv_pdu_send(state, ev,
199 stream,
200 dcerpc_read_ncacn_packet_next_vector,
201 state);
202 if (tevent_req_nomem(subreq, req)) {
203 goto post;
205 tevent_req_set_callback(subreq, dcerpc_read_ncacn_packet_done, req);
207 return req;
208 post:
209 tevent_req_post(req, ev);
210 return req;
213 static int dcerpc_read_ncacn_packet_next_vector(struct tstream_context *stream,
214 void *private_data,
215 TALLOC_CTX *mem_ctx,
216 struct iovec **_vector,
217 size_t *_count)
219 struct dcerpc_read_ncacn_packet_state *state =
220 talloc_get_type_abort(private_data,
221 struct dcerpc_read_ncacn_packet_state);
222 struct iovec *vector;
223 off_t ofs = 0;
225 if (state->buffer.length == 0) {
226 /* first get enough to read the fragment length */
227 ofs = 0;
228 state->buffer.length = DCERPC_FRAG_LEN_OFFSET + 2;
229 state->buffer.data = talloc_array(state, uint8_t,
230 state->buffer.length);
231 if (!state->buffer.data) {
232 return -1;
234 } else if (state->buffer.length == (DCERPC_FRAG_LEN_OFFSET + 2)) {
235 /* now read the fragment length and allocate the full buffer */
236 size_t frag_len = dcerpc_get_frag_length(&state->buffer);
238 ofs = state->buffer.length;
240 state->buffer.data = talloc_realloc(state,
241 state->buffer.data,
242 uint8_t, frag_len);
243 if (!state->buffer.data) {
244 return -1;
246 state->buffer.length = frag_len;
247 } else {
248 /* if we reach this we have a full fragment */
249 *_vector = NULL;
250 *_count = 0;
251 return 0;
254 /* now create the vector that we want to be filled */
255 vector = talloc_array(mem_ctx, struct iovec, 1);
256 if (!vector) {
257 return -1;
260 vector[0].iov_base = state->buffer.data + ofs;
261 vector[0].iov_len = state->buffer.length - ofs;
263 *_vector = vector;
264 *_count = 1;
265 return 0;
268 static void dcerpc_read_ncacn_packet_done(struct tevent_req *subreq)
270 struct tevent_req *req = tevent_req_callback_data(subreq,
271 struct tevent_req);
272 struct dcerpc_read_ncacn_packet_state *state = tevent_req_data(req,
273 struct dcerpc_read_ncacn_packet_state);
274 int ret;
275 int sys_errno;
276 struct ndr_pull *ndr;
277 enum ndr_err_code ndr_err;
278 NTSTATUS status;
280 ret = tstream_readv_pdu_recv(subreq, &sys_errno);
281 TALLOC_FREE(subreq);
282 if (ret == -1) {
283 status = map_nt_error_from_unix(sys_errno);
284 tevent_req_nterror(req, status);
285 return;
288 ndr = ndr_pull_init_blob(&state->buffer,
289 state->pkt,
290 state->caller.smb_iconv_c);
291 if (tevent_req_nomem(ndr, req)) {
292 return;
295 if (!(CVAL(ndr->data, DCERPC_DREP_OFFSET) & DCERPC_DREP_LE)) {
296 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
299 if (CVAL(ndr->data, DCERPC_PFC_OFFSET) & DCERPC_PFC_FLAG_OBJECT_UUID) {
300 ndr->flags |= LIBNDR_FLAG_OBJECT_PRESENT;
303 ndr_err = ndr_pull_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, state->pkt);
304 TALLOC_FREE(ndr);
305 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
306 status = ndr_map_error2ntstatus(ndr_err);
307 tevent_req_nterror(req, status);
308 return;
311 tevent_req_done(req);
314 static NTSTATUS dcerpc_read_ncacn_packet_recv(struct tevent_req *req,
315 TALLOC_CTX *mem_ctx,
316 struct ncacn_packet **pkt,
317 DATA_BLOB *buffer)
319 struct dcerpc_read_ncacn_packet_state *state = tevent_req_data(req,
320 struct dcerpc_read_ncacn_packet_state);
321 NTSTATUS status;
323 if (tevent_req_is_nterror(req, &status)) {
324 tevent_req_received(req);
325 return status;
328 *pkt = talloc_move(mem_ctx, &state->pkt);
329 if (buffer) {
330 buffer->data = talloc_move(mem_ctx, &state->buffer.data);
331 buffer->length = state->buffer.length;
334 tevent_req_received(req);
335 return NT_STATUS_OK;
338 static void dcesrv_read_fragment_done(struct tevent_req *subreq);
340 static void dcesrv_sock_accept(struct stream_connection *srv_conn)
342 NTSTATUS status;
343 struct dcesrv_socket_context *dcesrv_sock =
344 talloc_get_type(srv_conn->private_data, struct dcesrv_socket_context);
345 struct dcesrv_connection *dcesrv_conn = NULL;
346 int ret;
347 struct tevent_req *subreq;
348 struct loadparm_context *lp_ctx = dcesrv_sock->dcesrv_ctx->lp_ctx;
350 if (!srv_conn->session_info) {
351 status = auth_anonymous_session_info(srv_conn,
352 srv_conn->event.ctx,
353 lp_ctx,
354 &srv_conn->session_info);
355 if (!NT_STATUS_IS_OK(status)) {
356 DEBUG(0,("dcesrv_sock_accept: auth_anonymous_session_info failed: %s\n",
357 nt_errstr(status)));
358 stream_terminate_connection(srv_conn, nt_errstr(status));
359 return;
363 status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx,
364 srv_conn,
365 dcesrv_sock->endpoint,
366 srv_conn->session_info,
367 srv_conn->event.ctx,
368 srv_conn->msg_ctx,
369 srv_conn->server_id,
370 DCESRV_CALL_STATE_FLAG_MAY_ASYNC,
371 &dcesrv_conn);
372 if (!NT_STATUS_IS_OK(status)) {
373 DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n",
374 nt_errstr(status)));
375 stream_terminate_connection(srv_conn, nt_errstr(status));
376 return;
379 dcesrv_conn->transport.private_data = srv_conn;
380 dcesrv_conn->transport.report_output_data = dcesrv_sock_report_output_data;
381 dcesrv_conn->transport.get_my_addr = dcesrv_sock_get_my_addr;
382 dcesrv_conn->transport.get_peer_addr = dcesrv_sock_get_peer_addr;
384 TALLOC_FREE(srv_conn->event.fde);
386 dcesrv_conn->send_queue = tevent_queue_create(dcesrv_conn, "dcesrv send queue");
387 if (!dcesrv_conn->send_queue) {
388 status = NT_STATUS_NO_MEMORY;
389 DEBUG(0,("dcesrv_sock_accept: tevent_queue_create(%s)\n",
390 nt_errstr(status)));
391 stream_terminate_connection(srv_conn, nt_errstr(status));
392 return;
395 if (dcesrv_sock->endpoint->ep_description->transport == NCACN_NP) {
396 dcesrv_conn->auth_state.session_key = dcesrv_inherited_session_key;
397 ret = tstream_npa_existing_socket(dcesrv_conn,
398 socket_get_fd(srv_conn->socket),
399 FILE_TYPE_MESSAGE_MODE_PIPE,
400 &dcesrv_conn->stream);
401 } else {
402 ret = tstream_bsd_existing_socket(dcesrv_conn,
403 socket_get_fd(srv_conn->socket),
404 &dcesrv_conn->stream);
406 if (ret == -1) {
407 status = map_nt_error_from_unix(errno);
408 DEBUG(0,("dcesrv_sock_accept: failed to setup tstream: %s\n",
409 nt_errstr(status)));
410 stream_terminate_connection(srv_conn, nt_errstr(status));
411 return;
414 srv_conn->private_data = dcesrv_conn;
416 irpc_add_name(srv_conn->msg_ctx, "rpc_server");
418 subreq = dcerpc_read_ncacn_packet_send(dcesrv_conn,
419 dcesrv_conn->event_ctx,
420 dcesrv_conn->stream,
421 lp_iconv_convenience(lp_ctx));
422 if (!subreq) {
423 status = NT_STATUS_NO_MEMORY;
424 DEBUG(0,("dcesrv_sock_accept: dcerpc_read_fragment_buffer_send(%s)\n",
425 nt_errstr(status)));
426 stream_terminate_connection(srv_conn, nt_errstr(status));
427 return;
429 tevent_req_set_callback(subreq, dcesrv_read_fragment_done, dcesrv_conn);
431 return;
434 static void dcesrv_read_fragment_done(struct tevent_req *subreq)
436 struct dcesrv_connection *dce_conn = tevent_req_callback_data(subreq,
437 struct dcesrv_connection);
438 struct ncacn_packet *pkt;
439 DATA_BLOB buffer;
440 NTSTATUS status;
441 struct loadparm_context *lp_ctx = dce_conn->dce_ctx->lp_ctx;
443 status = dcerpc_read_ncacn_packet_recv(subreq, dce_conn,
444 &pkt, &buffer);
445 TALLOC_FREE(subreq);
446 if (!NT_STATUS_IS_OK(status)) {
447 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
448 return;
451 status = dcesrv_process_ncacn_packet(dce_conn, pkt, buffer);
452 if (!NT_STATUS_IS_OK(status)) {
453 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
454 return;
457 subreq = dcerpc_read_ncacn_packet_send(dce_conn,
458 dce_conn->event_ctx,
459 dce_conn->stream,
460 lp_iconv_convenience(lp_ctx));
461 if (!subreq) {
462 status = NT_STATUS_NO_MEMORY;
463 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
464 return;
466 tevent_req_set_callback(subreq, dcesrv_read_fragment_done, dce_conn);
469 static void dcesrv_sock_recv(struct stream_connection *conn, uint16_t flags)
471 struct dcesrv_connection *dce_conn = talloc_get_type(conn->private_data,
472 struct dcesrv_connection);
473 dcesrv_terminate_connection(dce_conn, "dcesrv_sock_recv triggered");
476 static void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags)
478 struct dcesrv_connection *dce_conn = talloc_get_type(conn->private_data,
479 struct dcesrv_connection);
480 dcesrv_terminate_connection(dce_conn, "dcesrv_sock_send triggered");
484 static const struct stream_server_ops dcesrv_stream_ops = {
485 .name = "rpc",
486 .accept_connection = dcesrv_sock_accept,
487 .recv_handler = dcesrv_sock_recv,
488 .send_handler = dcesrv_sock_send,
493 static NTSTATUS dcesrv_add_ep_unix(struct dcesrv_context *dce_ctx,
494 struct loadparm_context *lp_ctx,
495 struct dcesrv_endpoint *e,
496 struct tevent_context *event_ctx, const struct model_ops *model_ops)
498 struct dcesrv_socket_context *dcesrv_sock;
499 uint16_t port = 1;
500 NTSTATUS status;
502 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
503 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
505 /* remember the endpoint of this socket */
506 dcesrv_sock->endpoint = e;
507 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
509 status = stream_setup_socket(event_ctx, lp_ctx,
510 model_ops, &dcesrv_stream_ops,
511 "unix", e->ep_description->endpoint, &port,
512 lp_socket_options(lp_ctx),
513 dcesrv_sock);
514 if (!NT_STATUS_IS_OK(status)) {
515 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
516 e->ep_description->endpoint, nt_errstr(status)));
519 return status;
522 static NTSTATUS dcesrv_add_ep_ncalrpc(struct dcesrv_context *dce_ctx,
523 struct loadparm_context *lp_ctx,
524 struct dcesrv_endpoint *e,
525 struct tevent_context *event_ctx, const struct model_ops *model_ops)
527 struct dcesrv_socket_context *dcesrv_sock;
528 uint16_t port = 1;
529 char *full_path;
530 NTSTATUS status;
532 if (!e->ep_description->endpoint) {
533 /* No identifier specified: use DEFAULT.
534 * DO NOT hardcode this value anywhere else. Rather, specify
535 * no endpoint and let the epmapper worry about it. */
536 e->ep_description->endpoint = talloc_strdup(dce_ctx, "DEFAULT");
539 full_path = talloc_asprintf(dce_ctx, "%s/%s", lp_ncalrpc_dir(lp_ctx),
540 e->ep_description->endpoint);
542 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
543 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
545 /* remember the endpoint of this socket */
546 dcesrv_sock->endpoint = e;
547 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
549 status = stream_setup_socket(event_ctx, lp_ctx,
550 model_ops, &dcesrv_stream_ops,
551 "unix", full_path, &port,
552 lp_socket_options(lp_ctx),
553 dcesrv_sock);
554 if (!NT_STATUS_IS_OK(status)) {
555 DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n",
556 e->ep_description->endpoint, full_path, nt_errstr(status)));
558 return status;
561 static NTSTATUS dcesrv_add_ep_np(struct dcesrv_context *dce_ctx,
562 struct loadparm_context *lp_ctx,
563 struct dcesrv_endpoint *e,
564 struct tevent_context *event_ctx, const struct model_ops *model_ops)
566 struct dcesrv_socket_context *dcesrv_sock;
567 NTSTATUS status;
569 if (e->ep_description->endpoint == NULL) {
570 DEBUG(0, ("Endpoint mandatory for named pipes\n"));
571 return NT_STATUS_INVALID_PARAMETER;
574 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
575 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
577 /* remember the endpoint of this socket */
578 dcesrv_sock->endpoint = e;
579 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
581 status = stream_setup_named_pipe(event_ctx, lp_ctx,
582 model_ops, &dcesrv_stream_ops,
583 e->ep_description->endpoint, dcesrv_sock);
584 if (!NT_STATUS_IS_OK(status)) {
585 DEBUG(0,("stream_setup_named_pipe(pipe=%s) failed - %s\n",
586 e->ep_description->endpoint, nt_errstr(status)));
587 return status;
590 return NT_STATUS_OK;
594 add a socket address to the list of events, one event per dcerpc endpoint
596 static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
597 struct tevent_context *event_ctx, const struct model_ops *model_ops,
598 const char *address)
600 struct dcesrv_socket_context *dcesrv_sock;
601 uint16_t port = 0;
602 NTSTATUS status;
604 if (e->ep_description->endpoint) {
605 port = atoi(e->ep_description->endpoint);
608 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
609 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
611 /* remember the endpoint of this socket */
612 dcesrv_sock->endpoint = e;
613 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
615 status = stream_setup_socket(event_ctx, dce_ctx->lp_ctx,
616 model_ops, &dcesrv_stream_ops,
617 "ipv4", address, &port,
618 lp_socket_options(dce_ctx->lp_ctx),
619 dcesrv_sock);
620 if (!NT_STATUS_IS_OK(status)) {
621 DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n",
622 address, port, nt_errstr(status)));
625 if (e->ep_description->endpoint == NULL) {
626 e->ep_description->endpoint = talloc_asprintf(dce_ctx, "%d", port);
629 return status;
632 static NTSTATUS dcesrv_add_ep_tcp(struct dcesrv_context *dce_ctx,
633 struct loadparm_context *lp_ctx,
634 struct dcesrv_endpoint *e,
635 struct tevent_context *event_ctx, const struct model_ops *model_ops)
637 NTSTATUS status;
639 /* Add TCP/IP sockets */
640 if (lp_interfaces(lp_ctx) && lp_bind_interfaces_only(lp_ctx)) {
641 int num_interfaces;
642 int i;
643 struct interface *ifaces;
645 load_interfaces(dce_ctx, lp_interfaces(lp_ctx), &ifaces);
647 num_interfaces = iface_count(ifaces);
648 for(i = 0; i < num_interfaces; i++) {
649 const char *address = iface_n_ip(ifaces, i);
650 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address);
651 NT_STATUS_NOT_OK_RETURN(status);
653 } else {
654 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops,
655 lp_socket_address(lp_ctx));
656 NT_STATUS_NOT_OK_RETURN(status);
659 return NT_STATUS_OK;
662 NTSTATUS dcesrv_add_ep(struct dcesrv_context *dce_ctx,
663 struct loadparm_context *lp_ctx,
664 struct dcesrv_endpoint *e,
665 struct tevent_context *event_ctx,
666 const struct model_ops *model_ops)
668 switch (e->ep_description->transport) {
669 case NCACN_UNIX_STREAM:
670 return dcesrv_add_ep_unix(dce_ctx, lp_ctx, e, event_ctx, model_ops);
672 case NCALRPC:
673 return dcesrv_add_ep_ncalrpc(dce_ctx, lp_ctx, e, event_ctx, model_ops);
675 case NCACN_IP_TCP:
676 return dcesrv_add_ep_tcp(dce_ctx, lp_ctx, e, event_ctx, model_ops);
678 case NCACN_NP:
679 return dcesrv_add_ep_np(dce_ctx, lp_ctx, e, event_ctx, model_ops);
681 default:
682 return NT_STATUS_NOT_SUPPORTED;
687 open the dcerpc server sockets
689 static void dcesrv_task_init(struct task_server *task)
691 NTSTATUS status;
692 struct dcesrv_context *dce_ctx;
693 struct dcesrv_endpoint *e;
695 dcerpc_server_init(task->lp_ctx);
697 task_server_set_title(task, "task[dcesrv]");
699 status = dcesrv_init_context(task->event_ctx,
700 task->lp_ctx,
701 lp_dcerpc_endpoint_servers(task->lp_ctx),
702 &dce_ctx);
703 if (!NT_STATUS_IS_OK(status)) goto failed;
705 /* Make sure the directory for NCALRPC exists */
706 if (!directory_exist(lp_ncalrpc_dir(task->lp_ctx))) {
707 mkdir(lp_ncalrpc_dir(task->lp_ctx), 0755);
710 for (e=dce_ctx->endpoint_list;e;e=e->next) {
711 status = dcesrv_add_ep(dce_ctx, task->lp_ctx, e, task->event_ctx, task->model_ops);
712 if (!NT_STATUS_IS_OK(status)) goto failed;
715 return;
716 failed:
717 task_server_terminate(task, "Failed to startup dcerpc server task");
720 NTSTATUS server_service_rpc_init(void)
723 return register_server_service("rpc", dcesrv_task_init);