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