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