s4-loadparm: 2nd half of lp_ to lpcfg_ conversion
[sfrench/samba-autobuild/.git] / source4 / rpc_server / service_rpc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    smbd-specific dcerpc server code
5
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
9    
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.
14    
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.
19    
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/>.
22 */
23
24 #include "includes.h"
25 #include "librpc/gen_ndr/ndr_dcerpc.h"
26 #include "auth/auth.h"
27 #include "../lib/util/dlinklist.h"
28 #include "rpc_server/dcerpc_server.h"
29 #include "rpc_server/dcerpc_server_proto.h"
30 #include "system/filesys.h"
31 #include "lib/messaging/irpc.h"
32 #include "system/network.h"
33 #include "lib/socket/netif.h"
34 #include "param/param.h"
35 #include "../lib/tsocket/tsocket.h"
36 #include "librpc/rpc/dcerpc_proto.h"
37 #include "../lib/util/tevent_ntstatus.h"
38 #include "libcli/raw/smb.h"
39 #include "../libcli/named_pipe_auth/npa_tstream.h"
40 #include "smbd/process_model.h"
41
42 struct dcesrv_socket_context {
43         const struct dcesrv_endpoint *endpoint;
44         struct dcesrv_context *dcesrv_ctx;
45 };
46
47 static void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char *reason)
48 {
49         struct stream_connection *srv_conn;
50         srv_conn = talloc_get_type(dce_conn->transport.private_data,
51                                    struct stream_connection);
52
53         stream_terminate_connection(srv_conn, reason);
54 }
55
56 static void dcesrv_sock_reply_done(struct tevent_req *subreq);
57
58 struct dcesrv_sock_reply_state {
59         struct dcesrv_connection *dce_conn;
60         struct dcesrv_call_state *call;
61         struct iovec iov;
62 };
63
64 static void dcesrv_sock_report_output_data(struct dcesrv_connection *dce_conn)
65 {
66         struct dcesrv_call_state *call;
67
68         call = dce_conn->call_list;
69         if (!call || !call->replies) {
70                 return;
71         }
72
73         while (call->replies) {
74                 struct data_blob_list_item *rep = call->replies;
75                 struct dcesrv_sock_reply_state *substate;
76                 struct tevent_req *subreq;
77
78                 substate = talloc(call, struct dcesrv_sock_reply_state);
79                 if (!substate) {
80                         dcesrv_terminate_connection(dce_conn, "no memory");
81                         return;
82                 }
83
84                 substate->dce_conn = dce_conn;
85                 substate->call = NULL;
86
87                 DLIST_REMOVE(call->replies, rep);
88
89                 if (call->replies == NULL) {
90                         substate->call = call;
91                 }
92
93                 substate->iov.iov_base = (void *) rep->blob.data;
94                 substate->iov.iov_len = rep->blob.length;
95
96                 subreq = tstream_writev_queue_send(substate,
97                                                    dce_conn->event_ctx,
98                                                    dce_conn->stream,
99                                                    dce_conn->send_queue,
100                                                    &substate->iov, 1);
101                 if (!subreq) {
102                         dcesrv_terminate_connection(dce_conn, "no memory");
103                         return;
104                 }
105                 tevent_req_set_callback(subreq, dcesrv_sock_reply_done,
106                                         substate);
107         }
108
109         DLIST_REMOVE(call->conn->call_list, call);
110         call->list = DCESRV_LIST_NONE;
111 }
112
113 static void dcesrv_sock_reply_done(struct tevent_req *subreq)
114 {
115         struct dcesrv_sock_reply_state *substate = tevent_req_callback_data(subreq,
116                                                 struct dcesrv_sock_reply_state);
117         int ret;
118         int sys_errno;
119         NTSTATUS status;
120         struct dcesrv_call_state *call = substate->call;
121
122         ret = tstream_writev_queue_recv(subreq, &sys_errno);
123         TALLOC_FREE(subreq);
124         if (ret == -1) {
125                 status = map_nt_error_from_unix(sys_errno);
126                 dcesrv_terminate_connection(substate->dce_conn, nt_errstr(status));
127                 return;
128         }
129
130         talloc_free(substate);
131         if (call) {
132                 talloc_free(call);
133         }
134 }
135
136 struct dcerpc_read_ncacn_packet_state {
137 #if 0
138         struct {
139         } caller;
140 #endif
141         DATA_BLOB buffer;
142         struct ncacn_packet *pkt;
143 };
144
145 static int dcerpc_read_ncacn_packet_next_vector(struct tstream_context *stream,
146                                                 void *private_data,
147                                                 TALLOC_CTX *mem_ctx,
148                                                 struct iovec **_vector,
149                                                 size_t *_count);
150 static void dcerpc_read_ncacn_packet_done(struct tevent_req *subreq);
151
152 static struct tevent_req *dcerpc_read_ncacn_packet_send(TALLOC_CTX *mem_ctx,
153                                                  struct tevent_context *ev,
154                                                  struct tstream_context *stream)
155 {
156         struct tevent_req *req;
157         struct dcerpc_read_ncacn_packet_state *state;
158         struct tevent_req *subreq;
159
160         req = tevent_req_create(mem_ctx, &state,
161                                 struct dcerpc_read_ncacn_packet_state);
162         if (req == NULL) {
163                 return NULL;
164         }
165
166         state->buffer = data_blob_const(NULL, 0);
167         state->pkt = talloc(state, struct ncacn_packet);
168         if (tevent_req_nomem(state->pkt, req)) {
169                 goto post;
170         }
171
172         subreq = tstream_readv_pdu_send(state, ev,
173                                         stream,
174                                         dcerpc_read_ncacn_packet_next_vector,
175                                         state);
176         if (tevent_req_nomem(subreq, req)) {
177                 goto post;
178         }
179         tevent_req_set_callback(subreq, dcerpc_read_ncacn_packet_done, req);
180
181         return req;
182  post:
183         tevent_req_post(req, ev);
184         return req;
185 }
186
187 static int dcerpc_read_ncacn_packet_next_vector(struct tstream_context *stream,
188                                                 void *private_data,
189                                                 TALLOC_CTX *mem_ctx,
190                                                 struct iovec **_vector,
191                                                 size_t *_count)
192 {
193         struct dcerpc_read_ncacn_packet_state *state =
194                 talloc_get_type_abort(private_data,
195                 struct dcerpc_read_ncacn_packet_state);
196         struct iovec *vector;
197         off_t ofs = 0;
198
199         if (state->buffer.length == 0) {
200                 /* first get enough to read the fragment length */
201                 ofs = 0;
202                 state->buffer.length = DCERPC_FRAG_LEN_OFFSET + 2;
203                 state->buffer.data = talloc_array(state, uint8_t,
204                                                   state->buffer.length);
205                 if (!state->buffer.data) {
206                         return -1;
207                 }
208         } else if (state->buffer.length == (DCERPC_FRAG_LEN_OFFSET + 2)) {
209                 /* now read the fragment length and allocate the full buffer */
210                 size_t frag_len = dcerpc_get_frag_length(&state->buffer);
211
212                 ofs = state->buffer.length;
213
214                 state->buffer.data = talloc_realloc(state,
215                                                     state->buffer.data,
216                                                     uint8_t, frag_len);
217                 if (!state->buffer.data) {
218                         return -1;
219                 }
220                 state->buffer.length = frag_len;
221         } else {
222                 /* if we reach this we have a full fragment */
223                 *_vector = NULL;
224                 *_count = 0;
225                 return 0;
226         }
227
228         /* now create the vector that we want to be filled */
229         vector = talloc_array(mem_ctx, struct iovec, 1);
230         if (!vector) {
231                 return -1;
232         }
233
234         vector[0].iov_base = (void *) (state->buffer.data + ofs);
235         vector[0].iov_len = state->buffer.length - ofs;
236
237         *_vector = vector;
238         *_count = 1;
239         return 0;
240 }
241
242 static void dcerpc_read_ncacn_packet_done(struct tevent_req *subreq)
243 {
244         struct tevent_req *req = tevent_req_callback_data(subreq,
245                                  struct tevent_req);
246         struct dcerpc_read_ncacn_packet_state *state = tevent_req_data(req,
247                                         struct dcerpc_read_ncacn_packet_state);
248         int ret;
249         int sys_errno;
250         struct ndr_pull *ndr;
251         enum ndr_err_code ndr_err;
252         NTSTATUS status;
253
254         ret = tstream_readv_pdu_recv(subreq, &sys_errno);
255         TALLOC_FREE(subreq);
256         if (ret == -1) {
257                 status = map_nt_error_from_unix(sys_errno);
258                 tevent_req_nterror(req, status);
259                 return;
260         }
261
262         ndr = ndr_pull_init_blob(&state->buffer, state->pkt);
263         if (tevent_req_nomem(ndr, req)) {
264                 return;
265         }
266
267         if (!(CVAL(ndr->data, DCERPC_DREP_OFFSET) & DCERPC_DREP_LE)) {
268                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
269         }
270
271         if (CVAL(ndr->data, DCERPC_PFC_OFFSET) & DCERPC_PFC_FLAG_OBJECT_UUID) {
272                 ndr->flags |= LIBNDR_FLAG_OBJECT_PRESENT;
273         }
274
275         ndr_err = ndr_pull_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, state->pkt);
276         TALLOC_FREE(ndr);
277         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
278                 status = ndr_map_error2ntstatus(ndr_err);
279                 tevent_req_nterror(req, status);
280                 return;
281         }
282
283         tevent_req_done(req);
284 }
285
286 static NTSTATUS dcerpc_read_ncacn_packet_recv(struct tevent_req *req,
287                                        TALLOC_CTX *mem_ctx,
288                                        struct ncacn_packet **pkt,
289                                        DATA_BLOB *buffer)
290 {
291         struct dcerpc_read_ncacn_packet_state *state = tevent_req_data(req,
292                                         struct dcerpc_read_ncacn_packet_state);
293         NTSTATUS status;
294
295         if (tevent_req_is_nterror(req, &status)) {
296                 tevent_req_received(req);
297                 return status;
298         }
299
300         *pkt = talloc_move(mem_ctx, &state->pkt);
301         if (buffer) {
302                 buffer->data = talloc_move(mem_ctx, &state->buffer.data);
303                 buffer->length = state->buffer.length;
304         }
305
306         tevent_req_received(req);
307         return NT_STATUS_OK;
308 }
309
310 static void dcesrv_read_fragment_done(struct tevent_req *subreq);
311
312 static void dcesrv_sock_accept(struct stream_connection *srv_conn)
313 {
314         NTSTATUS status;
315         struct dcesrv_socket_context *dcesrv_sock = 
316                 talloc_get_type(srv_conn->private_data, struct dcesrv_socket_context);
317         struct dcesrv_connection *dcesrv_conn = NULL;
318         int ret;
319         struct tevent_req *subreq;
320         struct loadparm_context *lp_ctx = dcesrv_sock->dcesrv_ctx->lp_ctx;
321
322         if (!srv_conn->session_info) {
323                 status = auth_anonymous_session_info(srv_conn,
324                                                      lp_ctx,
325                                                      &srv_conn->session_info);
326                 if (!NT_STATUS_IS_OK(status)) {
327                         DEBUG(0,("dcesrv_sock_accept: auth_anonymous_session_info failed: %s\n",
328                                 nt_errstr(status)));
329                         stream_terminate_connection(srv_conn, nt_errstr(status));
330                         return;
331                 }
332         }
333
334         status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx,
335                                          srv_conn,
336                                          dcesrv_sock->endpoint,
337                                          srv_conn->session_info,
338                                          srv_conn->event.ctx,
339                                          srv_conn->msg_ctx,
340                                          srv_conn->server_id,
341                                          DCESRV_CALL_STATE_FLAG_MAY_ASYNC,
342                                          &dcesrv_conn);
343         if (!NT_STATUS_IS_OK(status)) {
344                 DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n", 
345                         nt_errstr(status)));
346                 stream_terminate_connection(srv_conn, nt_errstr(status));
347                 return;
348         }
349
350         dcesrv_conn->transport.private_data             = srv_conn;
351         dcesrv_conn->transport.report_output_data       = dcesrv_sock_report_output_data;
352
353         TALLOC_FREE(srv_conn->event.fde);
354
355         dcesrv_conn->send_queue = tevent_queue_create(dcesrv_conn, "dcesrv send queue");
356         if (!dcesrv_conn->send_queue) {
357                 status = NT_STATUS_NO_MEMORY;
358                 DEBUG(0,("dcesrv_sock_accept: tevent_queue_create(%s)\n",
359                         nt_errstr(status)));
360                 stream_terminate_connection(srv_conn, nt_errstr(status));
361                 return;
362         }
363
364         if (dcesrv_sock->endpoint->ep_description->transport == NCACN_NP) {
365                 dcesrv_conn->auth_state.session_key = dcesrv_inherited_session_key;
366                 dcesrv_conn->stream = talloc_move(dcesrv_conn,
367                                                   &srv_conn->tstream);
368         } else {
369                 ret = tstream_bsd_existing_socket(dcesrv_conn,
370                                                   socket_get_fd(srv_conn->socket),
371                                                   &dcesrv_conn->stream);
372                 if (ret == -1) {
373                         status = map_nt_error_from_unix(errno);
374                         DEBUG(0, ("dcesrv_sock_accept: "
375                                   "failed to setup tstream: %s\n",
376                                   nt_errstr(status)));
377                         stream_terminate_connection(srv_conn, nt_errstr(status));
378                         return;
379                 }
380         }
381
382         dcesrv_conn->local_address = srv_conn->local_address;
383         dcesrv_conn->remote_address = srv_conn->remote_address;
384
385         srv_conn->private_data = dcesrv_conn;
386
387         irpc_add_name(srv_conn->msg_ctx, "rpc_server");
388
389         subreq = dcerpc_read_ncacn_packet_send(dcesrv_conn,
390                                                dcesrv_conn->event_ctx,
391                                                dcesrv_conn->stream);
392         if (!subreq) {
393                 status = NT_STATUS_NO_MEMORY;
394                 DEBUG(0,("dcesrv_sock_accept: dcerpc_read_fragment_buffer_send(%s)\n",
395                         nt_errstr(status)));
396                 stream_terminate_connection(srv_conn, nt_errstr(status));
397                 return;
398         }
399         tevent_req_set_callback(subreq, dcesrv_read_fragment_done, dcesrv_conn);
400
401         return;
402 }
403
404 static void dcesrv_read_fragment_done(struct tevent_req *subreq)
405 {
406         struct dcesrv_connection *dce_conn = tevent_req_callback_data(subreq,
407                                              struct dcesrv_connection);
408         struct ncacn_packet *pkt;
409         DATA_BLOB buffer;
410         NTSTATUS status;
411
412         status = dcerpc_read_ncacn_packet_recv(subreq, dce_conn,
413                                                &pkt, &buffer);
414         TALLOC_FREE(subreq);
415         if (!NT_STATUS_IS_OK(status)) {
416                 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
417                 return;
418         }
419
420         status = dcesrv_process_ncacn_packet(dce_conn, pkt, buffer);
421         if (!NT_STATUS_IS_OK(status)) {
422                 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
423                 return;
424         }
425
426         subreq = dcerpc_read_ncacn_packet_send(dce_conn,
427                                                dce_conn->event_ctx,
428                                                dce_conn->stream);
429         if (!subreq) {
430                 status = NT_STATUS_NO_MEMORY;
431                 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
432                 return;
433         }
434         tevent_req_set_callback(subreq, dcesrv_read_fragment_done, dce_conn);
435 }
436
437 static void dcesrv_sock_recv(struct stream_connection *conn, uint16_t flags)
438 {
439         struct dcesrv_connection *dce_conn = talloc_get_type(conn->private_data,
440                                              struct dcesrv_connection);
441         dcesrv_terminate_connection(dce_conn, "dcesrv_sock_recv triggered");
442 }
443
444 static void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags)
445 {
446         struct dcesrv_connection *dce_conn = talloc_get_type(conn->private_data,
447                                              struct dcesrv_connection);
448         dcesrv_terminate_connection(dce_conn, "dcesrv_sock_send triggered");
449 }
450
451
452 static const struct stream_server_ops dcesrv_stream_ops = {
453         .name                   = "rpc",
454         .accept_connection      = dcesrv_sock_accept,
455         .recv_handler           = dcesrv_sock_recv,
456         .send_handler           = dcesrv_sock_send,
457 };
458
459
460
461 static NTSTATUS dcesrv_add_ep_unix(struct dcesrv_context *dce_ctx, 
462                                    struct loadparm_context *lp_ctx,
463                                    struct dcesrv_endpoint *e,
464                             struct tevent_context *event_ctx, const struct model_ops *model_ops)
465 {
466         struct dcesrv_socket_context *dcesrv_sock;
467         uint16_t port = 1;
468         NTSTATUS status;
469
470         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
471         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
472
473         /* remember the endpoint of this socket */
474         dcesrv_sock->endpoint           = e;
475         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
476
477         status = stream_setup_socket(event_ctx, lp_ctx,
478                                      model_ops, &dcesrv_stream_ops, 
479                                      "unix", e->ep_description->endpoint, &port, 
480                                      lpcfg_socket_options(lp_ctx),
481                                      dcesrv_sock);
482         if (!NT_STATUS_IS_OK(status)) {
483                 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
484                          e->ep_description->endpoint, nt_errstr(status)));
485         }
486
487         return status;
488 }
489
490 static NTSTATUS dcesrv_add_ep_ncalrpc(struct dcesrv_context *dce_ctx, 
491                                       struct loadparm_context *lp_ctx,
492                                       struct dcesrv_endpoint *e,
493                                       struct tevent_context *event_ctx, const struct model_ops *model_ops)
494 {
495         struct dcesrv_socket_context *dcesrv_sock;
496         uint16_t port = 1;
497         char *full_path;
498         NTSTATUS status;
499
500         if (!e->ep_description->endpoint) {
501                 /* No identifier specified: use DEFAULT. 
502                  * DO NOT hardcode this value anywhere else. Rather, specify 
503                  * no endpoint and let the epmapper worry about it. */
504                 e->ep_description->endpoint = talloc_strdup(dce_ctx, "DEFAULT");
505         }
506
507         full_path = talloc_asprintf(dce_ctx, "%s/%s", lpcfg_ncalrpc_dir(lp_ctx),
508                                     e->ep_description->endpoint);
509
510         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
511         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
512
513         /* remember the endpoint of this socket */
514         dcesrv_sock->endpoint           = e;
515         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
516
517         status = stream_setup_socket(event_ctx, lp_ctx,
518                                      model_ops, &dcesrv_stream_ops, 
519                                      "unix", full_path, &port, 
520                                      lpcfg_socket_options(lp_ctx),
521                                      dcesrv_sock);
522         if (!NT_STATUS_IS_OK(status)) {
523                 DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n",
524                          e->ep_description->endpoint, full_path, nt_errstr(status)));
525         }
526         return status;
527 }
528
529 static NTSTATUS dcesrv_add_ep_np(struct dcesrv_context *dce_ctx,
530                                  struct loadparm_context *lp_ctx,
531                                  struct dcesrv_endpoint *e,
532                                  struct tevent_context *event_ctx, const struct model_ops *model_ops)
533 {
534         struct dcesrv_socket_context *dcesrv_sock;
535         NTSTATUS status;
536                         
537         if (e->ep_description->endpoint == NULL) {
538                 DEBUG(0, ("Endpoint mandatory for named pipes\n"));
539                 return NT_STATUS_INVALID_PARAMETER;
540         }
541
542         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
543         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
544
545         /* remember the endpoint of this socket */
546         dcesrv_sock->endpoint           = e;
547         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
548
549         status = tstream_setup_named_pipe(event_ctx, lp_ctx,
550                                           model_ops, &dcesrv_stream_ops,
551                                           e->ep_description->endpoint,
552                                           dcesrv_sock);
553         if (!NT_STATUS_IS_OK(status)) {
554                 DEBUG(0,("stream_setup_named_pipe(pipe=%s) failed - %s\n",
555                          e->ep_description->endpoint, nt_errstr(status)));
556                 return status;
557         }
558
559         return NT_STATUS_OK;
560 }
561
562 /*
563   add a socket address to the list of events, one event per dcerpc endpoint
564 */
565 static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
566                                          struct tevent_context *event_ctx, const struct model_ops *model_ops,
567                                          const char *address)
568 {
569         struct dcesrv_socket_context *dcesrv_sock;
570         uint16_t port = 0;
571         NTSTATUS status;
572                         
573         if (e->ep_description->endpoint) {
574                 port = atoi(e->ep_description->endpoint);
575         }
576
577         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
578         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
579
580         /* remember the endpoint of this socket */
581         dcesrv_sock->endpoint           = e;
582         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
583
584         status = stream_setup_socket(event_ctx, dce_ctx->lp_ctx,
585                                      model_ops, &dcesrv_stream_ops, 
586                                      "ipv4", address, &port, 
587                                      lpcfg_socket_options(dce_ctx->lp_ctx),
588                                      dcesrv_sock);
589         if (!NT_STATUS_IS_OK(status)) {
590                 DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n", 
591                          address, port, nt_errstr(status)));
592         }
593
594         if (e->ep_description->endpoint == NULL) {
595                 e->ep_description->endpoint = talloc_asprintf(dce_ctx, "%d", port);
596         }
597
598         return status;
599 }
600
601 static NTSTATUS dcesrv_add_ep_tcp(struct dcesrv_context *dce_ctx, 
602                                   struct loadparm_context *lp_ctx,
603                                   struct dcesrv_endpoint *e,
604                                   struct tevent_context *event_ctx, const struct model_ops *model_ops)
605 {
606         NTSTATUS status;
607
608         /* Add TCP/IP sockets */
609         if (lpcfg_interfaces(lp_ctx) && lpcfg_bind_interfaces_only(lp_ctx)) {
610                 int num_interfaces;
611                 int i;
612                 struct interface *ifaces;
613
614                 load_interfaces(dce_ctx, lpcfg_interfaces(lp_ctx), &ifaces);
615
616                 num_interfaces = iface_count(ifaces);
617                 for(i = 0; i < num_interfaces; i++) {
618                         const char *address = iface_n_ip(ifaces, i);
619                         status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address);
620                         NT_STATUS_NOT_OK_RETURN(status);
621                 }
622         } else {
623                 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, 
624                                                   lpcfg_socket_address(lp_ctx));
625                 NT_STATUS_NOT_OK_RETURN(status);
626         }
627
628         return NT_STATUS_OK;
629 }
630
631 NTSTATUS dcesrv_add_ep(struct dcesrv_context *dce_ctx,
632                        struct loadparm_context *lp_ctx,
633                        struct dcesrv_endpoint *e,
634                        struct tevent_context *event_ctx,
635                        const struct model_ops *model_ops)
636 {
637         switch (e->ep_description->transport) {
638         case NCACN_UNIX_STREAM:
639                 return dcesrv_add_ep_unix(dce_ctx, lp_ctx, e, event_ctx, model_ops);
640
641         case NCALRPC:
642                 return dcesrv_add_ep_ncalrpc(dce_ctx, lp_ctx, e, event_ctx, model_ops);
643
644         case NCACN_IP_TCP:
645                 return dcesrv_add_ep_tcp(dce_ctx, lp_ctx, e, event_ctx, model_ops);
646
647         case NCACN_NP:
648                 return dcesrv_add_ep_np(dce_ctx, lp_ctx, e, event_ctx, model_ops);
649
650         default:
651                 return NT_STATUS_NOT_SUPPORTED;
652         }
653 }
654
655 /*
656   open the dcerpc server sockets
657 */
658 static void dcesrv_task_init(struct task_server *task)
659 {
660         NTSTATUS status;
661         struct dcesrv_context *dce_ctx;
662         struct dcesrv_endpoint *e;
663         const struct model_ops *model_ops;
664
665         dcerpc_server_init(task->lp_ctx);
666
667         task_server_set_title(task, "task[dcesrv]");
668
669         /* run the rpc server as a single process to allow for shard
670          * handles, and sharing of ldb contexts */
671         model_ops = process_model_startup(task->event_ctx, "single");
672         if (!model_ops) goto failed;
673
674         status = dcesrv_init_context(task->event_ctx,
675                                      task->lp_ctx,
676                                      lpcfg_dcerpc_endpoint_servers(task->lp_ctx),
677                                      &dce_ctx);
678         if (!NT_STATUS_IS_OK(status)) goto failed;
679
680         /* Make sure the directory for NCALRPC exists */
681         if (!directory_exist(lpcfg_ncalrpc_dir(task->lp_ctx))) {
682                 mkdir(lpcfg_ncalrpc_dir(task->lp_ctx), 0755);
683         }
684
685         for (e=dce_ctx->endpoint_list;e;e=e->next) {
686                 status = dcesrv_add_ep(dce_ctx, task->lp_ctx, e, task->event_ctx, model_ops);
687                 if (!NT_STATUS_IS_OK(status)) goto failed;
688         }
689
690         return;
691 failed:
692         task_server_terminate(task, "Failed to startup dcerpc server task", true);      
693 }
694
695 NTSTATUS server_service_rpc_init(void)
696 {
697
698         return register_server_service("rpc", dcesrv_task_init);
699 }