s4: ran minimal_includes.pl on source4/rpc_server
[ira/wip.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                                                      srv_conn->event.ctx,
346                                                      lp_ctx,
347                                                      &srv_conn->session_info);
348                 if (!NT_STATUS_IS_OK(status)) {
349                         DEBUG(0,("dcesrv_sock_accept: auth_anonymous_session_info failed: %s\n",
350                                 nt_errstr(status)));
351                         stream_terminate_connection(srv_conn, nt_errstr(status));
352                         return;
353                 }
354         }
355
356         status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx,
357                                          srv_conn,
358                                          dcesrv_sock->endpoint,
359                                          srv_conn->session_info,
360                                          srv_conn->event.ctx,
361                                          srv_conn->msg_ctx,
362                                          srv_conn->server_id,
363                                          DCESRV_CALL_STATE_FLAG_MAY_ASYNC,
364                                          &dcesrv_conn);
365         if (!NT_STATUS_IS_OK(status)) {
366                 DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n", 
367                         nt_errstr(status)));
368                 stream_terminate_connection(srv_conn, nt_errstr(status));
369                 return;
370         }
371
372         dcesrv_conn->transport.private_data             = srv_conn;
373         dcesrv_conn->transport.report_output_data       = dcesrv_sock_report_output_data;
374         dcesrv_conn->transport.get_my_addr              = dcesrv_sock_get_my_addr;
375         dcesrv_conn->transport.get_peer_addr            = dcesrv_sock_get_peer_addr;
376
377         TALLOC_FREE(srv_conn->event.fde);
378
379         dcesrv_conn->send_queue = tevent_queue_create(dcesrv_conn, "dcesrv send queue");
380         if (!dcesrv_conn->send_queue) {
381                 status = NT_STATUS_NO_MEMORY;
382                 DEBUG(0,("dcesrv_sock_accept: tevent_queue_create(%s)\n",
383                         nt_errstr(status)));
384                 stream_terminate_connection(srv_conn, nt_errstr(status));
385                 return;
386         }
387
388         if (dcesrv_sock->endpoint->ep_description->transport == NCACN_NP) {
389                 dcesrv_conn->auth_state.session_key = dcesrv_inherited_session_key;
390                 ret = tstream_npa_existing_socket(dcesrv_conn,
391                                                   socket_get_fd(srv_conn->socket),
392                                                   FILE_TYPE_MESSAGE_MODE_PIPE,
393                                                   &dcesrv_conn->stream);
394         } else {
395                 ret = tstream_bsd_existing_socket(dcesrv_conn,
396                                                   socket_get_fd(srv_conn->socket),
397                                                   &dcesrv_conn->stream);
398         }
399         if (ret == -1) {
400                 status = map_nt_error_from_unix(errno);
401                 DEBUG(0,("dcesrv_sock_accept: failed to setup tstream: %s\n",
402                         nt_errstr(status)));
403                 stream_terminate_connection(srv_conn, nt_errstr(status));
404                 return;
405         }
406
407         srv_conn->private_data = dcesrv_conn;
408
409         irpc_add_name(srv_conn->msg_ctx, "rpc_server");
410
411         subreq = dcerpc_read_ncacn_packet_send(dcesrv_conn,
412                                                dcesrv_conn->event_ctx,
413                                                dcesrv_conn->stream,
414                                                lp_iconv_convenience(lp_ctx));
415         if (!subreq) {
416                 status = NT_STATUS_NO_MEMORY;
417                 DEBUG(0,("dcesrv_sock_accept: dcerpc_read_fragment_buffer_send(%s)\n",
418                         nt_errstr(status)));
419                 stream_terminate_connection(srv_conn, nt_errstr(status));
420                 return;
421         }
422         tevent_req_set_callback(subreq, dcesrv_read_fragment_done, dcesrv_conn);
423
424         return;
425 }
426
427 static void dcesrv_read_fragment_done(struct tevent_req *subreq)
428 {
429         struct dcesrv_connection *dce_conn = tevent_req_callback_data(subreq,
430                                              struct dcesrv_connection);
431         struct ncacn_packet *pkt;
432         DATA_BLOB buffer;
433         NTSTATUS status;
434         struct loadparm_context *lp_ctx = dce_conn->dce_ctx->lp_ctx;
435
436         status = dcerpc_read_ncacn_packet_recv(subreq, dce_conn,
437                                                &pkt, &buffer);
438         TALLOC_FREE(subreq);
439         if (!NT_STATUS_IS_OK(status)) {
440                 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
441                 return;
442         }
443
444         status = dcesrv_process_ncacn_packet(dce_conn, pkt, buffer);
445         if (!NT_STATUS_IS_OK(status)) {
446                 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
447                 return;
448         }
449
450         subreq = dcerpc_read_ncacn_packet_send(dce_conn,
451                                                dce_conn->event_ctx,
452                                                dce_conn->stream,
453                                                lp_iconv_convenience(lp_ctx));
454         if (!subreq) {
455                 status = NT_STATUS_NO_MEMORY;
456                 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
457                 return;
458         }
459         tevent_req_set_callback(subreq, dcesrv_read_fragment_done, dce_conn);
460 }
461
462 static void dcesrv_sock_recv(struct stream_connection *conn, uint16_t flags)
463 {
464         struct dcesrv_connection *dce_conn = talloc_get_type(conn->private_data,
465                                              struct dcesrv_connection);
466         dcesrv_terminate_connection(dce_conn, "dcesrv_sock_recv triggered");
467 }
468
469 static void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags)
470 {
471         struct dcesrv_connection *dce_conn = talloc_get_type(conn->private_data,
472                                              struct dcesrv_connection);
473         dcesrv_terminate_connection(dce_conn, "dcesrv_sock_send triggered");
474 }
475
476
477 static const struct stream_server_ops dcesrv_stream_ops = {
478         .name                   = "rpc",
479         .accept_connection      = dcesrv_sock_accept,
480         .recv_handler           = dcesrv_sock_recv,
481         .send_handler           = dcesrv_sock_send,
482 };
483
484
485
486 static NTSTATUS dcesrv_add_ep_unix(struct dcesrv_context *dce_ctx, 
487                                    struct loadparm_context *lp_ctx,
488                                    struct dcesrv_endpoint *e,
489                             struct tevent_context *event_ctx, const struct model_ops *model_ops)
490 {
491         struct dcesrv_socket_context *dcesrv_sock;
492         uint16_t port = 1;
493         NTSTATUS status;
494
495         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
496         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
497
498         /* remember the endpoint of this socket */
499         dcesrv_sock->endpoint           = e;
500         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
501
502         status = stream_setup_socket(event_ctx, lp_ctx,
503                                      model_ops, &dcesrv_stream_ops, 
504                                      "unix", e->ep_description->endpoint, &port, 
505                                      lp_socket_options(lp_ctx), 
506                                      dcesrv_sock);
507         if (!NT_STATUS_IS_OK(status)) {
508                 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
509                          e->ep_description->endpoint, nt_errstr(status)));
510         }
511
512         return status;
513 }
514
515 static NTSTATUS dcesrv_add_ep_ncalrpc(struct dcesrv_context *dce_ctx, 
516                                       struct loadparm_context *lp_ctx,
517                                       struct dcesrv_endpoint *e,
518                                       struct tevent_context *event_ctx, const struct model_ops *model_ops)
519 {
520         struct dcesrv_socket_context *dcesrv_sock;
521         uint16_t port = 1;
522         char *full_path;
523         NTSTATUS status;
524
525         if (!e->ep_description->endpoint) {
526                 /* No identifier specified: use DEFAULT. 
527                  * DO NOT hardcode this value anywhere else. Rather, specify 
528                  * no endpoint and let the epmapper worry about it. */
529                 e->ep_description->endpoint = talloc_strdup(dce_ctx, "DEFAULT");
530         }
531
532         full_path = talloc_asprintf(dce_ctx, "%s/%s", lp_ncalrpc_dir(lp_ctx), 
533                                     e->ep_description->endpoint);
534
535         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
536         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
537
538         /* remember the endpoint of this socket */
539         dcesrv_sock->endpoint           = e;
540         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
541
542         status = stream_setup_socket(event_ctx, lp_ctx,
543                                      model_ops, &dcesrv_stream_ops, 
544                                      "unix", full_path, &port, 
545                                      lp_socket_options(lp_ctx), 
546                                      dcesrv_sock);
547         if (!NT_STATUS_IS_OK(status)) {
548                 DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n",
549                          e->ep_description->endpoint, full_path, nt_errstr(status)));
550         }
551         return status;
552 }
553
554 static NTSTATUS dcesrv_add_ep_np(struct dcesrv_context *dce_ctx,
555                                  struct loadparm_context *lp_ctx,
556                                  struct dcesrv_endpoint *e,
557                                  struct tevent_context *event_ctx, const struct model_ops *model_ops)
558 {
559         struct dcesrv_socket_context *dcesrv_sock;
560         NTSTATUS status;
561                         
562         if (e->ep_description->endpoint == NULL) {
563                 DEBUG(0, ("Endpoint mandatory for named pipes\n"));
564                 return NT_STATUS_INVALID_PARAMETER;
565         }
566
567         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
568         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
569
570         /* remember the endpoint of this socket */
571         dcesrv_sock->endpoint           = e;
572         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
573
574         status = stream_setup_named_pipe(event_ctx, lp_ctx,
575                                          model_ops, &dcesrv_stream_ops,
576                                          e->ep_description->endpoint, dcesrv_sock);
577         if (!NT_STATUS_IS_OK(status)) {
578                 DEBUG(0,("stream_setup_named_pipe(pipe=%s) failed - %s\n",
579                          e->ep_description->endpoint, nt_errstr(status)));
580                 return status;
581         }
582
583         return NT_STATUS_OK;
584 }
585
586 /*
587   add a socket address to the list of events, one event per dcerpc endpoint
588 */
589 static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
590                                          struct tevent_context *event_ctx, const struct model_ops *model_ops,
591                                          const char *address)
592 {
593         struct dcesrv_socket_context *dcesrv_sock;
594         uint16_t port = 0;
595         NTSTATUS status;
596                         
597         if (e->ep_description->endpoint) {
598                 port = atoi(e->ep_description->endpoint);
599         }
600
601         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
602         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
603
604         /* remember the endpoint of this socket */
605         dcesrv_sock->endpoint           = e;
606         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
607
608         status = stream_setup_socket(event_ctx, dce_ctx->lp_ctx,
609                                      model_ops, &dcesrv_stream_ops, 
610                                      "ipv4", address, &port, 
611                                      lp_socket_options(dce_ctx->lp_ctx), 
612                                      dcesrv_sock);
613         if (!NT_STATUS_IS_OK(status)) {
614                 DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n", 
615                          address, port, nt_errstr(status)));
616         }
617
618         if (e->ep_description->endpoint == NULL) {
619                 e->ep_description->endpoint = talloc_asprintf(dce_ctx, "%d", port);
620         }
621
622         return status;
623 }
624
625 static NTSTATUS dcesrv_add_ep_tcp(struct dcesrv_context *dce_ctx, 
626                                   struct loadparm_context *lp_ctx,
627                                   struct dcesrv_endpoint *e,
628                                   struct tevent_context *event_ctx, const struct model_ops *model_ops)
629 {
630         NTSTATUS status;
631
632         /* Add TCP/IP sockets */
633         if (lp_interfaces(lp_ctx) && lp_bind_interfaces_only(lp_ctx)) {
634                 int num_interfaces;
635                 int i;
636                 struct interface *ifaces;
637
638                 load_interfaces(dce_ctx, lp_interfaces(lp_ctx), &ifaces);
639
640                 num_interfaces = iface_count(ifaces);
641                 for(i = 0; i < num_interfaces; i++) {
642                         const char *address = iface_n_ip(ifaces, i);
643                         status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address);
644                         NT_STATUS_NOT_OK_RETURN(status);
645                 }
646         } else {
647                 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, 
648                                                   lp_socket_address(lp_ctx));
649                 NT_STATUS_NOT_OK_RETURN(status);
650         }
651
652         return NT_STATUS_OK;
653 }
654
655 NTSTATUS dcesrv_add_ep(struct dcesrv_context *dce_ctx,
656                        struct loadparm_context *lp_ctx,
657                        struct dcesrv_endpoint *e,
658                        struct tevent_context *event_ctx,
659                        const struct model_ops *model_ops)
660 {
661         switch (e->ep_description->transport) {
662         case NCACN_UNIX_STREAM:
663                 return dcesrv_add_ep_unix(dce_ctx, lp_ctx, e, event_ctx, model_ops);
664
665         case NCALRPC:
666                 return dcesrv_add_ep_ncalrpc(dce_ctx, lp_ctx, e, event_ctx, model_ops);
667
668         case NCACN_IP_TCP:
669                 return dcesrv_add_ep_tcp(dce_ctx, lp_ctx, e, event_ctx, model_ops);
670
671         case NCACN_NP:
672                 return dcesrv_add_ep_np(dce_ctx, lp_ctx, e, event_ctx, model_ops);
673
674         default:
675                 return NT_STATUS_NOT_SUPPORTED;
676         }
677 }
678
679 /*
680   open the dcerpc server sockets
681 */
682 static void dcesrv_task_init(struct task_server *task)
683 {
684         NTSTATUS status;
685         struct dcesrv_context *dce_ctx;
686         struct dcesrv_endpoint *e;
687         const struct model_ops *model_ops;
688
689         dcerpc_server_init(task->lp_ctx);
690
691         task_server_set_title(task, "task[dcesrv]");
692
693         /* run the rpc server as a single process to allow for shard
694          * handles, and sharing of ldb contexts */
695         model_ops = process_model_startup(task->event_ctx, "single");
696         if (!model_ops) goto failed;
697
698         status = dcesrv_init_context(task->event_ctx,
699                                      task->lp_ctx,
700                                      lp_dcerpc_endpoint_servers(task->lp_ctx),
701                                      &dce_ctx);
702         if (!NT_STATUS_IS_OK(status)) goto failed;
703
704         /* Make sure the directory for NCALRPC exists */
705         if (!directory_exist(lp_ncalrpc_dir(task->lp_ctx))) {
706                 mkdir(lp_ncalrpc_dir(task->lp_ctx), 0755);
707         }
708
709         for (e=dce_ctx->endpoint_list;e;e=e->next) {
710                 status = dcesrv_add_ep(dce_ctx, task->lp_ctx, e, task->event_ctx, model_ops);
711                 if (!NT_STATUS_IS_OK(status)) goto failed;
712         }
713
714         return;
715 failed:
716         task_server_terminate(task, "Failed to startup dcerpc server task", true);      
717 }
718
719 NTSTATUS server_service_rpc_init(void)
720 {
721
722         return register_server_service("rpc", dcesrv_task_init);
723 }